oti6858.c 25.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Ours Technology Inc. OTi-6858 USB to serial adapter driver.
 *
 * Copyleft  (C) 2007 Kees Lemmens (adapted for kernel 2.6.20)
 * Copyright (C) 2006 Tomasz Michal Lukaszewski (FIXME: add e-mail)
 * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
 * Copyright (C) 2003 IBM Corp.
 *
 * Many thanks to the authors of pl2303 driver: all functions in this file
 * are heavily based on pl2303 code, buffering code is a 1-to-1 copy.
 *
 * Warning! You use this driver on your own risk! The only official
 * description of this device I have is datasheet from manufacturer,
 * and it doesn't contain almost any information needed to write a driver.
 * Almost all knowlegde used while writing this driver was gathered by:
 *  - analyzing traffic between device and the M$ Windows 2000 driver,
 *  - trying different bit combinations and checking pin states
 *    with a voltmeter,
 *  - receiving malformed frames and producing buffer overflows
 *    to learn how errors are reported,
 * So, THIS CODE CAN DESTROY OTi-6858 AND ANY OTHER DEVICES, THAT ARE
 * CONNECTED TO IT!
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License.
 *
A
Alan Cox 已提交
28 29
 * See Documentation/usb/usb-serial.txt for more information on using this
 * driver
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
 *
 * TODO:
 *  - implement correct flushing for ioctls and oti6858_close()
 *  - check how errors (rx overflow, parity error, framing error) are reported
 *  - implement oti6858_break_ctl()
 *  - implement more ioctls
 *  - test/implement flow control
 *  - allow setting custom baud rates
 */

#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/serial.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/spinlock.h>
#include <linux/usb.h>
#include <linux/usb/serial.h>
A
Alan Cox 已提交
53
#include <linux/uaccess.h>
54
#include <linux/kfifo.h>
55 56 57 58 59
#include "oti6858.h"

#define OTI6858_DESCRIPTION \
	"Ours Technology Inc. OTi-6858 USB to serial adapter driver"
#define OTI6858_AUTHOR "Tomasz Michal Lukaszewski <FIXME@FIXME>"
60
#define OTI6858_VERSION "0.2"
61

62
static const struct usb_device_id id_table[] = {
63 64 65 66 67 68
	{ USB_DEVICE(OTI6858_VENDOR_ID, OTI6858_PRODUCT_ID) },
	{ }
};

MODULE_DEVICE_TABLE(usb, id_table);

69
static bool debug;
70 71 72 73 74 75 76 77 78 79 80 81 82

/* requests */
#define	OTI6858_REQ_GET_STATUS		(USB_DIR_IN | USB_TYPE_VENDOR | 0x00)
#define	OTI6858_REQ_T_GET_STATUS	0x01

#define	OTI6858_REQ_SET_LINE		(USB_DIR_OUT | USB_TYPE_VENDOR | 0x00)
#define	OTI6858_REQ_T_SET_LINE		0x00

#define	OTI6858_REQ_CHECK_TXBUFF	(USB_DIR_IN | USB_TYPE_VENDOR | 0x01)
#define	OTI6858_REQ_T_CHECK_TXBUFF	0x00

/* format of the control packet */
struct oti6858_control_pkt {
A
Al Viro 已提交
83
	__le16	divisor;	/* baud rate = 96000000 / (16 * divisor), LE */
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
#define OTI6858_MAX_BAUD_RATE	3000000
	u8	frame_fmt;
#define FMT_STOP_BITS_MASK	0xc0
#define FMT_STOP_BITS_1		0x00
#define FMT_STOP_BITS_2		0x40	/* 1.5 stop bits if FMT_DATA_BITS_5 */
#define FMT_PARITY_MASK		0x38
#define FMT_PARITY_NONE		0x00
#define FMT_PARITY_ODD		0x08
#define FMT_PARITY_EVEN		0x18
#define FMT_PARITY_MARK		0x28
#define FMT_PARITY_SPACE	0x38
#define FMT_DATA_BITS_MASK	0x03
#define FMT_DATA_BITS_5		0x00
#define FMT_DATA_BITS_6		0x01
#define FMT_DATA_BITS_7		0x02
#define FMT_DATA_BITS_8		0x03
	u8	something;	/* always equals 0x43 */
	u8	control;	/* settings of flow control lines */
#define CONTROL_MASK		0x0c
#define CONTROL_DTR_HIGH	0x08
#define CONTROL_RTS_HIGH	0x04
	u8	tx_status;
#define	TX_BUFFER_EMPTIED	0x09
	u8	pin_state;
#define PIN_MASK		0x3f
#define PIN_RTS			0x20	/* output pin */
#define PIN_CTS			0x10	/* input pin, active low */
#define PIN_DSR			0x08	/* input pin, active low */
#define PIN_DTR			0x04	/* output pin */
#define PIN_RI			0x02	/* input pin, active low */
#define PIN_DCD			0x01	/* input pin, active low */
	u8	rx_bytes_avail;		/* number of bytes in rx buffer */;
};

#define OTI6858_CTRL_PKT_SIZE	sizeof(struct oti6858_control_pkt)
#define OTI6858_CTRL_EQUALS_PENDING(a, priv) \
A
Alan Cox 已提交
120
	(((a)->divisor == (priv)->pending_setup.divisor) \
121
	  && ((a)->control == (priv)->pending_setup.control) \
A
Alan Cox 已提交
122
	  && ((a)->frame_fmt == (priv)->pending_setup.frame_fmt))
123 124

/* function prototypes */
125
static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port);
126
static void oti6858_close(struct usb_serial_port *port);
A
Alan Cox 已提交
127 128
static void oti6858_set_termios(struct tty_struct *tty,
			struct usb_serial_port *port, struct ktermios *old);
A
Alan Cox 已提交
129
static void oti6858_init_termios(struct tty_struct *tty);
130
static int oti6858_ioctl(struct tty_struct *tty,
131 132 133 134
			unsigned int cmd, unsigned long arg);
static void oti6858_read_int_callback(struct urb *urb);
static void oti6858_read_bulk_callback(struct urb *urb);
static void oti6858_write_bulk_callback(struct urb *urb);
A
Alan Cox 已提交
135
static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
136
			const unsigned char *buf, int count);
A
Alan Cox 已提交
137 138
static int oti6858_write_room(struct tty_struct *tty);
static int oti6858_chars_in_buffer(struct tty_struct *tty);
139
static int oti6858_tiocmget(struct tty_struct *tty);
140
static int oti6858_tiocmset(struct tty_struct *tty,
141 142
				unsigned int set, unsigned int clear);
static int oti6858_startup(struct usb_serial *serial);
143
static void oti6858_release(struct usb_serial *serial);
144 145 146 147 148 149 150 151 152 153 154 155 156 157

/* device info */
static struct usb_serial_driver oti6858_device = {
	.driver = {
		.owner =	THIS_MODULE,
		.name =		"oti6858",
	},
	.id_table =		id_table,
	.num_ports =		1,
	.open =			oti6858_open,
	.close =		oti6858_close,
	.write =		oti6858_write,
	.ioctl =		oti6858_ioctl,
	.set_termios =		oti6858_set_termios,
A
Alan Cox 已提交
158
	.init_termios = 	oti6858_init_termios,
159 160 161 162 163 164 165 166
	.tiocmget =		oti6858_tiocmget,
	.tiocmset =		oti6858_tiocmset,
	.read_bulk_callback =	oti6858_read_bulk_callback,
	.read_int_callback =	oti6858_read_int_callback,
	.write_bulk_callback =	oti6858_write_bulk_callback,
	.write_room =		oti6858_write_room,
	.chars_in_buffer =	oti6858_chars_in_buffer,
	.attach =		oti6858_startup,
167
	.release =		oti6858_release,
168 169
};

170 171 172 173
static struct usb_serial_driver * const serial_drivers[] = {
	&oti6858_device, NULL
};

174 175 176 177 178 179 180 181 182 183 184 185
struct oti6858_private {
	spinlock_t lock;

	struct oti6858_control_pkt status;

	struct {
		u8 read_urb_in_use;
		u8 write_urb_in_use;
	} flags;
	struct delayed_work delayed_write_work;

	struct {
A
Al Viro 已提交
186
		__le16 divisor;
187 188 189 190 191 192 193 194
		u8 frame_fmt;
		u8 control;
	} pending_setup;
	u8 transient;
	u8 setup_done;
	struct delayed_work delayed_setup_work;

	wait_queue_head_t intr_wait;
A
Alan Cox 已提交
195
	struct usb_serial_port *port;   /* USB port with which associated */
196 197 198 199
};

static void setup_line(struct work_struct *work)
{
A
Alan Cox 已提交
200 201
	struct oti6858_private *priv = container_of(work,
			struct oti6858_private, delayed_setup_work.work);
202 203 204 205 206
	struct usb_serial_port *port = priv->port;
	struct oti6858_control_pkt *new_setup;
	unsigned long flags;
	int result;

A
Alan Cox 已提交
207 208
	new_setup = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
	if (new_setup == NULL) {
209
		dev_err(&port->dev, "%s(): out of memory!\n", __func__);
210
		/* we will try again */
A
Alan Cox 已提交
211 212
		schedule_delayed_work(&priv->delayed_setup_work,
						msecs_to_jiffies(2));
213 214 215 216 217 218 219 220 221 222 223 224
		return;
	}

	result = usb_control_msg(port->serial->dev,
				usb_rcvctrlpipe(port->serial->dev, 0),
				OTI6858_REQ_T_GET_STATUS,
				OTI6858_REQ_GET_STATUS,
				0, 0,
				new_setup, OTI6858_CTRL_PKT_SIZE,
				100);

	if (result != OTI6858_CTRL_PKT_SIZE) {
225
		dev_err(&port->dev, "%s(): error reading status\n", __func__);
226 227
		kfree(new_setup);
		/* we will try again */
A
Alan Cox 已提交
228 229
		schedule_delayed_work(&priv->delayed_setup_work,
							msecs_to_jiffies(2));
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
		return;
	}

	spin_lock_irqsave(&priv->lock, flags);
	if (!OTI6858_CTRL_EQUALS_PENDING(new_setup, priv)) {
		new_setup->divisor = priv->pending_setup.divisor;
		new_setup->control = priv->pending_setup.control;
		new_setup->frame_fmt = priv->pending_setup.frame_fmt;

		spin_unlock_irqrestore(&priv->lock, flags);
		result = usb_control_msg(port->serial->dev,
					usb_sndctrlpipe(port->serial->dev, 0),
					OTI6858_REQ_T_SET_LINE,
					OTI6858_REQ_SET_LINE,
					0, 0,
					new_setup, OTI6858_CTRL_PKT_SIZE,
					100);
	} else {
		spin_unlock_irqrestore(&priv->lock, flags);
		result = 0;
	}
	kfree(new_setup);

	spin_lock_irqsave(&priv->lock, flags);
	if (result != OTI6858_CTRL_PKT_SIZE)
		priv->transient = 0;
	priv->setup_done = 1;
	spin_unlock_irqrestore(&priv->lock, flags);

259
	dbg("%s(): submitting interrupt urb", __func__);
260
	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
261 262
	if (result != 0) {
		dev_err(&port->dev, "%s(): usb_submit_urb() failed"
263
				" with error %d\n", __func__, result);
264 265 266
	}
}

267
static void send_data(struct work_struct *work)
268
{
A
Alan Cox 已提交
269 270
	struct oti6858_private *priv = container_of(work,
			struct oti6858_private, delayed_write_work.work);
271 272 273
	struct usb_serial_port *port = priv->port;
	int count = 0, result;
	unsigned long flags;
274
	u8 *allow;
275 276 277 278

	spin_lock_irqsave(&priv->lock, flags);
	if (priv->flags.write_urb_in_use) {
		spin_unlock_irqrestore(&priv->lock, flags);
A
Alan Cox 已提交
279 280
		schedule_delayed_work(&priv->delayed_write_work,
						msecs_to_jiffies(2));
281 282 283 284
		return;
	}
	priv->flags.write_urb_in_use = 1;
	spin_unlock_irqrestore(&priv->lock, flags);
J
Johan Hovold 已提交
285 286 287 288 289

	spin_lock_irqsave(&port->lock, flags);
	count = kfifo_len(&port->write_fifo);
	spin_unlock_irqrestore(&port->lock, flags);

290 291 292 293
	if (count > port->bulk_out_size)
		count = port->bulk_out_size;

	if (count != 0) {
294 295
		allow = kmalloc(1, GFP_KERNEL);
		if (!allow) {
296
			dev_err_console(port, "%s(): kmalloc failed\n",
297 298 299
					__func__);
			return;
		}
300 301 302 303
		result = usb_control_msg(port->serial->dev,
				usb_rcvctrlpipe(port->serial->dev, 0),
				OTI6858_REQ_T_CHECK_TXBUFF,
				OTI6858_REQ_CHECK_TXBUFF,
304 305
				count, 0, allow, 1, 100);
		if (result != 1 || *allow != 0)
306
			count = 0;
307
		kfree(allow);
308 309 310 311 312
	}

	if (count == 0) {
		priv->flags.write_urb_in_use = 0;

313
		dbg("%s(): submitting interrupt urb", __func__);
314
		result = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
315 316
		if (result != 0) {
			dev_err(&port->dev, "%s(): usb_submit_urb() failed"
317
				" with error %d\n", __func__, result);
318 319 320 321
		}
		return;
	}

J
Johan Hovold 已提交
322
	count = kfifo_out_locked(&port->write_fifo,
323
					port->write_urb->transfer_buffer,
J
Johan Hovold 已提交
324
					count, &port->lock);
325
	port->write_urb->transfer_buffer_length = count;
326
	result = usb_submit_urb(port->write_urb, GFP_NOIO);
327
	if (result != 0) {
328
		dev_err_console(port, "%s(): usb_submit_urb() failed"
329
			       " with error %d\n", __func__, result);
330 331 332 333 334 335 336 337
		priv->flags.write_urb_in_use = 0;
	}

	usb_serial_port_softint(port);
}

static int oti6858_startup(struct usb_serial *serial)
{
A
Alan Cox 已提交
338 339
	struct usb_serial_port *port = serial->port[0];
	struct oti6858_private *priv;
340 341 342 343 344 345 346 347 348
	int i;

	for (i = 0; i < serial->num_ports; ++i) {
		priv = kzalloc(sizeof(struct oti6858_private), GFP_KERNEL);
		if (!priv)
			break;

		spin_lock_init(&priv->lock);
		init_waitqueue_head(&priv->intr_wait);
A
Alan Cox 已提交
349 350
/*		INIT_WORK(&priv->setup_work, setup_line, serial->port[i]); */
/*		INIT_WORK(&priv->write_work, send_data, serial->port[i]); */
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
		priv->port = port;
		INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line);
		INIT_DELAYED_WORK(&priv->delayed_write_work, send_data);

		usb_set_serial_port_data(serial->port[i], priv);
	}
	if (i == serial->num_ports)
		return 0;

	for (--i; i >= 0; --i) {
		priv = usb_get_serial_port_data(serial->port[i]);
		kfree(priv);
		usb_set_serial_port_data(serial->port[i], NULL);
	}
	return -ENOMEM;
}

A
Alan Cox 已提交
368
static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
369 370 371 372 373
			const unsigned char *buf, int count)
{
	if (!count)
		return count;

J
Johan Hovold 已提交
374
	count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
375 376 377 378

	return count;
}

A
Alan Cox 已提交
379
static int oti6858_write_room(struct tty_struct *tty)
380
{
A
Alan Cox 已提交
381
	struct usb_serial_port *port = tty->driver_data;
382 383 384
	int room = 0;
	unsigned long flags;

J
Johan Hovold 已提交
385 386 387
	spin_lock_irqsave(&port->lock, flags);
	room = kfifo_avail(&port->write_fifo);
	spin_unlock_irqrestore(&port->lock, flags);
388 389 390 391

	return room;
}

A
Alan Cox 已提交
392
static int oti6858_chars_in_buffer(struct tty_struct *tty)
393
{
A
Alan Cox 已提交
394
	struct usb_serial_port *port = tty->driver_data;
395 396 397
	int chars = 0;
	unsigned long flags;

J
Johan Hovold 已提交
398 399 400
	spin_lock_irqsave(&port->lock, flags);
	chars = kfifo_len(&port->write_fifo);
	spin_unlock_irqrestore(&port->lock, flags);
401 402 403 404

	return chars;
}

A
Alan Cox 已提交
405 406 407 408 409 410 411 412
static void oti6858_init_termios(struct tty_struct *tty)
{
	*(tty->termios) = tty_std_termios;
	tty->termios->c_cflag = B38400 | CS8 | CREAD | HUPCL | CLOCAL;
	tty->termios->c_ispeed = 38400;
	tty->termios->c_ospeed = 38400;
}

A
Alan Cox 已提交
413 414
static void oti6858_set_termios(struct tty_struct *tty,
		struct usb_serial_port *port, struct ktermios *old_termios)
415 416 417 418 419
{
	struct oti6858_private *priv = usb_get_serial_port_data(port);
	unsigned long flags;
	unsigned int cflag;
	u8 frame_fmt, control;
A
Al Viro 已提交
420
	__le16 divisor;
421 422
	int br;

A
Alan Cox 已提交
423
	if (!tty) {
424
		dbg("%s(): no tty structures", __func__);
425 426 427
		return;
	}

A
Alan Cox 已提交
428
	cflag = tty->termios->c_cflag;
429 430 431 432 433 434 435 436 437

	spin_lock_irqsave(&priv->lock, flags);
	divisor = priv->pending_setup.divisor;
	frame_fmt = priv->pending_setup.frame_fmt;
	control = priv->pending_setup.control;
	spin_unlock_irqrestore(&priv->lock, flags);

	frame_fmt &= ~FMT_DATA_BITS_MASK;
	switch (cflag & CSIZE) {
A
Alan Cox 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450
	case CS5:
		frame_fmt |= FMT_DATA_BITS_5;
		break;
	case CS6:
		frame_fmt |= FMT_DATA_BITS_6;
		break;
	case CS7:
		frame_fmt |= FMT_DATA_BITS_7;
		break;
	default:
	case CS8:
		frame_fmt |= FMT_DATA_BITS_8;
		break;
451 452 453 454 455 456 457
	}

	/* manufacturer claims that this device can work with baud rates
	 * up to 3 Mbps; I've tested it only on 115200 bps, so I can't
	 * guarantee that any other baud rate will work (especially
	 * the higher ones)
	 */
A
Alan Cox 已提交
458
	br = tty_get_baud_rate(tty);
459 460
	if (br == 0) {
		divisor = 0;
A
Alan Cox 已提交
461
	} else {
462
		int real_br;
A
Al Viro 已提交
463
		int new_divisor;
A
Alan Cox 已提交
464
		br = min(br, OTI6858_MAX_BAUD_RATE);
465

A
Al Viro 已提交
466 467 468
		new_divisor = (96000000 + 8 * br) / (16 * br);
		real_br = 96000000 / (16 * new_divisor);
		divisor = cpu_to_le16(new_divisor);
A
Alan Cox 已提交
469
		tty_encode_baud_rate(tty, real_br, real_br);
470 471 472
	}

	frame_fmt &= ~FMT_STOP_BITS_MASK;
A
Alan Cox 已提交
473
	if ((cflag & CSTOPB) != 0)
474
		frame_fmt |= FMT_STOP_BITS_2;
A
Alan Cox 已提交
475
	else
476 477 478 479
		frame_fmt |= FMT_STOP_BITS_1;

	frame_fmt &= ~FMT_PARITY_MASK;
	if ((cflag & PARENB) != 0) {
A
Alan Cox 已提交
480
		if ((cflag & PARODD) != 0)
481
			frame_fmt |= FMT_PARITY_ODD;
A
Alan Cox 已提交
482
		else
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
			frame_fmt |= FMT_PARITY_EVEN;
	} else {
		frame_fmt |= FMT_PARITY_NONE;
	}

	control &= ~CONTROL_MASK;
	if ((cflag & CRTSCTS) != 0)
		control |= (CONTROL_DTR_HIGH | CONTROL_RTS_HIGH);

	/* change control lines if we are switching to or from B0 */
	/* FIXME:
	spin_lock_irqsave(&priv->lock, flags);
	control = priv->line_control;
	if ((cflag & CBAUD) == B0)
		priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
	else
		priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
	if (control != priv->line_control) {
		control = priv->line_control;
		spin_unlock_irqrestore(&priv->lock, flags);
		set_control_lines(serial->dev, control);
	} else {
		spin_unlock_irqrestore(&priv->lock, flags);
	}
	*/

	spin_lock_irqsave(&priv->lock, flags);
	if (divisor != priv->pending_setup.divisor
			|| control != priv->pending_setup.control
			|| frame_fmt != priv->pending_setup.frame_fmt) {
		priv->pending_setup.divisor = divisor;
		priv->pending_setup.control = control;
		priv->pending_setup.frame_fmt = frame_fmt;
	}
	spin_unlock_irqrestore(&priv->lock, flags);
}

520
static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port)
521 522 523 524 525 526 527 528 529 530 531
{
	struct oti6858_private *priv = usb_get_serial_port_data(port);
	struct ktermios tmp_termios;
	struct usb_serial *serial = port->serial;
	struct oti6858_control_pkt *buf;
	unsigned long flags;
	int result;

	usb_clear_halt(serial->dev, port->write_urb->pipe);
	usb_clear_halt(serial->dev, port->read_urb->pipe);

A
Alan Cox 已提交
532 533
	buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
	if (buf == NULL) {
534
		dev_err(&port->dev, "%s(): out of memory!\n", __func__);
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
		return -ENOMEM;
	}

	result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
				OTI6858_REQ_T_GET_STATUS,
				OTI6858_REQ_GET_STATUS,
				0, 0,
				buf, OTI6858_CTRL_PKT_SIZE,
				100);
	if (result != OTI6858_CTRL_PKT_SIZE) {
		/* assume default (after power-on reset) values */
		buf->divisor = cpu_to_le16(0x009c);	/* 38400 bps */
		buf->frame_fmt = 0x03;	/* 8N1 */
		buf->something = 0x43;
		buf->control = 0x4c;	/* DTR, RTS */
		buf->tx_status = 0x00;
		buf->pin_state = 0x5b;	/* RTS, CTS, DSR, DTR, RI, DCD */
		buf->rx_bytes_avail = 0x00;
	}

	spin_lock_irqsave(&priv->lock, flags);
	memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE);
	priv->pending_setup.divisor = buf->divisor;
	priv->pending_setup.frame_fmt = buf->frame_fmt;
	priv->pending_setup.control = buf->control;
	spin_unlock_irqrestore(&priv->lock, flags);
	kfree(buf);

563
	dbg("%s(): submitting interrupt urb", __func__);
564 565 566
	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
	if (result != 0) {
		dev_err(&port->dev, "%s(): usb_submit_urb() failed"
567
			       " with error %d\n", __func__, result);
568
		oti6858_close(port);
569
		return result;
570 571 572
	}

	/* setup termios */
A
Alan Cox 已提交
573 574
	if (tty)
		oti6858_set_termios(tty, port, &tmp_termios);
575
	port->port.drain_delay = 256;	/* FIXME: check the FIFO length */
576 577 578
	return 0;
}

579
static void oti6858_close(struct usb_serial_port *port)
580 581 582 583
{
	struct oti6858_private *priv = usb_get_serial_port_data(port);
	unsigned long flags;

J
Johan Hovold 已提交
584
	spin_lock_irqsave(&port->lock, flags);
585
	/* clear out any remaining data in the buffer */
J
Johan Hovold 已提交
586 587
	kfifo_reset_out(&port->write_fifo);
	spin_unlock_irqrestore(&port->lock, flags);
588

589
	dbg("%s(): after buf_clear()", __func__);
590 591

	/* cancel scheduled setup */
T
Tejun Heo 已提交
592 593
	cancel_delayed_work_sync(&priv->delayed_setup_work);
	cancel_delayed_work_sync(&priv->delayed_write_work);
594 595

	/* shutdown our urbs */
596
	dbg("%s(): shutting down urbs", __func__);
597 598 599 600 601
	usb_kill_urb(port->write_urb);
	usb_kill_urb(port->read_urb);
	usb_kill_urb(port->interrupt_in_urb);
}

602
static int oti6858_tiocmset(struct tty_struct *tty,
603 604
				unsigned int set, unsigned int clear)
{
A
Alan Cox 已提交
605
	struct usb_serial_port *port = tty->driver_data;
606 607 608 609 610
	struct oti6858_private *priv = usb_get_serial_port_data(port);
	unsigned long flags;
	u8 control;

	dbg("%s(port = %d, set = 0x%08x, clear = 0x%08x)",
611
				__func__, port->number, set, clear);
612 613 614 615 616 617 618 619 620 621 622 623 624

	/* FIXME: check if this is correct (active high/low) */
	spin_lock_irqsave(&priv->lock, flags);
	control = priv->pending_setup.control;
	if ((set & TIOCM_RTS) != 0)
		control |= CONTROL_RTS_HIGH;
	if ((set & TIOCM_DTR) != 0)
		control |= CONTROL_DTR_HIGH;
	if ((clear & TIOCM_RTS) != 0)
		control &= ~CONTROL_RTS_HIGH;
	if ((clear & TIOCM_DTR) != 0)
		control &= ~CONTROL_DTR_HIGH;

A
Alan Cox 已提交
625
	if (control != priv->pending_setup.control)
626 627
		priv->pending_setup.control = control;

A
Alan Cox 已提交
628
	spin_unlock_irqrestore(&priv->lock, flags);
629 630 631
	return 0;
}

632
static int oti6858_tiocmget(struct tty_struct *tty)
633
{
A
Alan Cox 已提交
634
	struct usb_serial_port *port = tty->driver_data;
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
	struct oti6858_private *priv = usb_get_serial_port_data(port);
	unsigned long flags;
	unsigned pin_state;
	unsigned result = 0;

	spin_lock_irqsave(&priv->lock, flags);
	pin_state = priv->status.pin_state & PIN_MASK;
	spin_unlock_irqrestore(&priv->lock, flags);

	/* FIXME: check if this is correct (active high/low) */
	if ((pin_state & PIN_RTS) != 0)
		result |= TIOCM_RTS;
	if ((pin_state & PIN_CTS) != 0)
		result |= TIOCM_CTS;
	if ((pin_state & PIN_DSR) != 0)
		result |= TIOCM_DSR;
	if ((pin_state & PIN_DTR) != 0)
		result |= TIOCM_DTR;
	if ((pin_state & PIN_RI) != 0)
		result |= TIOCM_RI;
	if ((pin_state & PIN_DCD) != 0)
		result |= TIOCM_CD;

658
	dbg("%s() = 0x%08x", __func__, result);
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674

	return result;
}

static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
{
	struct oti6858_private *priv = usb_get_serial_port_data(port);
	unsigned long flags;
	unsigned int prev, status;
	unsigned int changed;

	spin_lock_irqsave(&priv->lock, flags);
	prev = priv->status.pin_state;
	spin_unlock_irqrestore(&priv->lock, flags);

	while (1) {
A
Alan Cox 已提交
675 676
		wait_event_interruptible(priv->intr_wait,
					priv->status.pin_state != prev);
677 678 679 680 681 682 683 684 685
		if (signal_pending(current))
			return -ERESTARTSYS;

		spin_lock_irqsave(&priv->lock, flags);
		status = priv->status.pin_state & PIN_MASK;
		spin_unlock_irqrestore(&priv->lock, flags);

		changed = prev ^ status;
		/* FIXME: check if this is correct (active high/low) */
A
Alan Cox 已提交
686 687 688 689 690
		if (((arg & TIOCM_RNG) && (changed & PIN_RI)) ||
		    ((arg & TIOCM_DSR) && (changed & PIN_DSR)) ||
		    ((arg & TIOCM_CD)  && (changed & PIN_DCD)) ||
		    ((arg & TIOCM_CTS) && (changed & PIN_CTS)))
			return 0;
691 692 693 694 695 696 697
		prev = status;
	}

	/* NOTREACHED */
	return 0;
}

698
static int oti6858_ioctl(struct tty_struct *tty,
699 700
			unsigned int cmd, unsigned long arg)
{
A
Alan Cox 已提交
701
	struct usb_serial_port *port = tty->driver_data;
702 703

	dbg("%s(port = %d, cmd = 0x%04x, arg = 0x%08lx)",
704
				__func__, port->number, cmd, arg);
705 706

	switch (cmd) {
A
Alan Cox 已提交
707 708 709 710 711 712
	case TIOCMIWAIT:
		dbg("%s(): TIOCMIWAIT", __func__);
		return wait_modem_info(port, arg);
	default:
		dbg("%s(): 0x%04x not supported", __func__, cmd);
		break;
713 714 715 716 717
	}
	return -ENOIOCTLCMD;
}


718
static void oti6858_release(struct usb_serial *serial)
719 720 721
{
	int i;

J
Johan Hovold 已提交
722 723
	for (i = 0; i < serial->num_ports; ++i)
		kfree(usb_get_serial_port_data(serial->port[i]));
724 725 726 727
}

static void oti6858_read_int_callback(struct urb *urb)
{
728
	struct usb_serial_port *port =  urb->context;
729 730
	struct oti6858_private *priv = usb_get_serial_port_data(port);
	int transient = 0, can_recv = 0, resubmit = 1;
731
	int status = urb->status;
732

733
	switch (status) {
734 735 736 737 738 739 740 741
	case 0:
		/* success */
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
		dbg("%s(): urb shutting down with status: %d",
742
					__func__, status);
743 744 745
		return;
	default:
		dbg("%s(): nonzero urb status received: %d",
746
					__func__, status);
747 748 749
		break;
	}

750
	if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
751 752 753 754 755 756 757 758 759 760 761 762
		struct oti6858_control_pkt *xs = urb->transfer_buffer;
		unsigned long flags;

		spin_lock_irqsave(&priv->lock, flags);

		if (!priv->transient) {
			if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
				if (xs->rx_bytes_avail == 0) {
					priv->transient = 4;
					priv->setup_done = 0;
					resubmit = 0;
					dbg("%s(): scheduling setup_line()",
763
					    __func__);
764 765 766 767 768 769 770 771 772 773 774 775 776 777
					schedule_delayed_work(&priv->delayed_setup_work, 0);
				}
			}
		} else {
			if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
				priv->transient = 0;
			} else if (!priv->setup_done) {
				resubmit = 0;
			} else if (--priv->transient == 0) {
				if (xs->rx_bytes_avail == 0) {
					priv->transient = 4;
					priv->setup_done = 0;
					resubmit = 0;
					dbg("%s(): scheduling setup_line()",
778
					    __func__);
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
					schedule_delayed_work(&priv->delayed_setup_work, 0);
				}
			}
		}

		if (!priv->transient) {
			if (xs->pin_state != priv->status.pin_state)
				wake_up_interruptible(&priv->intr_wait);
			memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
		}

		if (!priv->transient && xs->rx_bytes_avail != 0) {
			can_recv = xs->rx_bytes_avail;
			priv->flags.read_urb_in_use = 1;
		}

		transient = priv->transient;
		spin_unlock_irqrestore(&priv->lock, flags);
	}

	if (can_recv) {
		int result;

		result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
		if (result != 0) {
			priv->flags.read_urb_in_use = 0;
			dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
806
					" error %d\n", __func__, result);
807 808 809 810 811
		} else {
			resubmit = 0;
		}
	} else if (!transient) {
		unsigned long flags;
J
Johan Hovold 已提交
812 813 814 815 816
		int count;

		spin_lock_irqsave(&port->lock, flags);
		count = kfifo_len(&port->write_fifo);
		spin_unlock_irqrestore(&port->lock, flags);
817 818

		spin_lock_irqsave(&priv->lock, flags);
J
Johan Hovold 已提交
819
		if (priv->flags.write_urb_in_use == 0 && count != 0) {
A
Alan Cox 已提交
820
			schedule_delayed_work(&priv->delayed_write_work, 0);
821 822 823 824 825 826 827 828
			resubmit = 0;
		}
		spin_unlock_irqrestore(&priv->lock, flags);
	}

	if (resubmit) {
		int result;

A
Alan Cox 已提交
829
/*		dbg("%s(): submitting interrupt urb", __func__); */
830 831 832 833
		result = usb_submit_urb(urb, GFP_ATOMIC);
		if (result != 0) {
			dev_err(&urb->dev->dev,
					"%s(): usb_submit_urb() failed with"
834
					" error %d\n", __func__, result);
835 836 837 838 839 840
		}
	}
}

static void oti6858_read_bulk_callback(struct urb *urb)
{
841
	struct usb_serial_port *port =  urb->context;
842 843 844 845
	struct oti6858_private *priv = usb_get_serial_port_data(port);
	struct tty_struct *tty;
	unsigned char *data = urb->transfer_buffer;
	unsigned long flags;
846
	int status = urb->status;
A
Alan Cox 已提交
847
	int result;
848 849 850 851 852

	spin_lock_irqsave(&priv->lock, flags);
	priv->flags.read_urb_in_use = 0;
	spin_unlock_irqrestore(&priv->lock, flags);

853
	if (status != 0) {
854
		dbg("%s(): unable to handle the error, exiting", __func__);
855 856 857
		return;
	}

A
Alan Cox 已提交
858
	tty = tty_port_tty_get(&port->port);
859
	if (tty != NULL && urb->actual_length > 0) {
A
Alan Cox 已提交
860
		tty_insert_flip_string(tty, data, urb->actual_length);
861 862
		tty_flip_buffer_push(tty);
	}
A
Alan Cox 已提交
863
	tty_kref_put(tty);
864

865 866 867 868 869
	/* schedule the interrupt urb */
	result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
	if (result != 0 && result != -EPERM) {
		dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
				" error %d\n", __func__, result);
870 871 872 873 874
	}
}

static void oti6858_write_bulk_callback(struct urb *urb)
{
875
	struct usb_serial_port *port =  urb->context;
876
	struct oti6858_private *priv = usb_get_serial_port_data(port);
877
	int status = urb->status;
878 879
	int result;

880
	switch (status) {
881 882 883 884 885 886 887 888
	case 0:
		/* success */
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
		dbg("%s(): urb shutting down with status: %d",
889
					__func__, status);
890 891 892 893 894
		priv->flags.write_urb_in_use = 0;
		return;
	default:
		/* error in the urb, so we have to resubmit it */
		dbg("%s(): nonzero write bulk status received: %d",
895 896
					__func__, status);
		dbg("%s(): overflow in write", __func__);
897 898 899 900

		port->write_urb->transfer_buffer_length = 1;
		result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
		if (result) {
901
			dev_err_console(port, "%s(): usb_submit_urb() failed,"
902
					" error %d\n", __func__, result);
903 904 905 906 907 908 909
		} else {
			return;
		}
	}

	priv->flags.write_urb_in_use = 0;

A
Alan Cox 已提交
910
	/* schedule the interrupt urb if we are still open */
911
	dbg("%s(): submitting interrupt urb", __func__);
912 913 914
	result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
	if (result != 0) {
		dev_err(&port->dev, "%s(): failed submitting int urb,"
915
					" error %d\n", __func__, result);
916 917 918
	}
}

919
module_usb_serial_driver(serial_drivers, id_table);
920 921 922 923 924 925 926 927 928

MODULE_DESCRIPTION(OTI6858_DESCRIPTION);
MODULE_AUTHOR(OTI6858_AUTHOR);
MODULE_VERSION(OTI6858_VERSION);
MODULE_LICENSE("GPL");

module_param(debug, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(debug, "enable debug output");