xref: /linux/drivers/usb/serial/oti6858.c (revision 63c3a1a9be06f8f8c75d54ef9331e35adb2e7919)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Ours Technology Inc. OTi-6858 USB to serial adapter driver.
4  *
5  * Copyleft  (C) 2007 Kees Lemmens (adapted for kernel 2.6.20)
6  * Copyright (C) 2006 Tomasz Michal Lukaszewski (FIXME: add e-mail)
7  * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
8  * Copyright (C) 2003 IBM Corp.
9  *
10  * Many thanks to the authors of pl2303 driver: all functions in this file
11  * are heavily based on pl2303 code, buffering code is a 1-to-1 copy.
12  *
13  * Warning! You use this driver on your own risk! The only official
14  * description of this device I have is datasheet from manufacturer,
15  * and it doesn't contain almost any information needed to write a driver.
16  * Almost all knowlegde used while writing this driver was gathered by:
17  *  - analyzing traffic between device and the M$ Windows 2000 driver,
18  *  - trying different bit combinations and checking pin states
19  *    with a voltmeter,
20  *  - receiving malformed frames and producing buffer overflows
21  *    to learn how errors are reported,
22  * So, THIS CODE CAN DESTROY OTi-6858 AND ANY OTHER DEVICES, THAT ARE
23  * CONNECTED TO IT!
24  *
25  * See Documentation/usb/usb-serial.rst for more information on using this
26  * driver
27  *
28  * TODO:
29  *  - implement correct flushing for ioctls and oti6858_close()
30  *  - check how errors (rx overflow, parity error, framing error) are reported
31  *  - implement oti6858_break_ctl()
32  *  - implement more ioctls
33  *  - test/implement flow control
34  *  - allow setting custom baud rates
35  */
36 
37 #include <linux/kernel.h>
38 #include <linux/errno.h>
39 #include <linux/slab.h>
40 #include <linux/tty.h>
41 #include <linux/tty_flip.h>
42 #include <linux/serial.h>
43 #include <linux/module.h>
44 #include <linux/spinlock.h>
45 #include <linux/usb.h>
46 #include <linux/usb/serial.h>
47 #include <linux/kfifo.h>
48 #include "oti6858.h"
49 
50 #define OTI6858_DESCRIPTION \
51 	"Ours Technology Inc. OTi-6858 USB to serial adapter driver"
52 #define OTI6858_AUTHOR "Tomasz Michal Lukaszewski <FIXME@FIXME>"
53 
54 static const struct usb_device_id id_table[] = {
55 	{ USB_DEVICE(OTI6858_VENDOR_ID, OTI6858_PRODUCT_ID) },
56 	{ }
57 };
58 
59 MODULE_DEVICE_TABLE(usb, id_table);
60 
61 /* requests */
62 #define	OTI6858_REQ_GET_STATUS		(USB_DIR_IN | USB_TYPE_VENDOR | 0x00)
63 #define	OTI6858_REQ_T_GET_STATUS	0x01
64 
65 #define	OTI6858_REQ_SET_LINE		(USB_DIR_OUT | USB_TYPE_VENDOR | 0x00)
66 #define	OTI6858_REQ_T_SET_LINE		0x00
67 
68 #define	OTI6858_REQ_CHECK_TXBUFF	(USB_DIR_IN | USB_TYPE_VENDOR | 0x01)
69 #define	OTI6858_REQ_T_CHECK_TXBUFF	0x00
70 
71 /* format of the control packet */
72 struct oti6858_control_pkt {
73 	__le16	divisor;	/* baud rate = 96000000 / (16 * divisor), LE */
74 #define OTI6858_MAX_BAUD_RATE	3000000
75 	u8	frame_fmt;
76 #define FMT_STOP_BITS_MASK	0xc0
77 #define FMT_STOP_BITS_1		0x00
78 #define FMT_STOP_BITS_2		0x40	/* 1.5 stop bits if FMT_DATA_BITS_5 */
79 #define FMT_PARITY_MASK		0x38
80 #define FMT_PARITY_NONE		0x00
81 #define FMT_PARITY_ODD		0x08
82 #define FMT_PARITY_EVEN		0x18
83 #define FMT_PARITY_MARK		0x28
84 #define FMT_PARITY_SPACE	0x38
85 #define FMT_DATA_BITS_MASK	0x03
86 #define FMT_DATA_BITS_5		0x00
87 #define FMT_DATA_BITS_6		0x01
88 #define FMT_DATA_BITS_7		0x02
89 #define FMT_DATA_BITS_8		0x03
90 	u8	something;	/* always equals 0x43 */
91 	u8	control;	/* settings of flow control lines */
92 #define CONTROL_MASK		0x0c
93 #define CONTROL_DTR_HIGH	0x08
94 #define CONTROL_RTS_HIGH	0x04
95 	u8	tx_status;
96 #define	TX_BUFFER_EMPTIED	0x09
97 	u8	pin_state;
98 #define PIN_MASK		0x3f
99 #define PIN_MSR_MASK		0x1b
100 #define PIN_RTS			0x20	/* output pin */
101 #define PIN_CTS			0x10	/* input pin, active low */
102 #define PIN_DSR			0x08	/* input pin, active low */
103 #define PIN_DTR			0x04	/* output pin */
104 #define PIN_RI			0x02	/* input pin, active low */
105 #define PIN_DCD			0x01	/* input pin, active low */
106 	u8	rx_bytes_avail;		/* number of bytes in rx buffer */
107 };
108 
109 #define OTI6858_CTRL_PKT_SIZE	sizeof(struct oti6858_control_pkt)
110 #define OTI6858_CTRL_EQUALS_PENDING(a, priv) \
111 	(((a)->divisor == (priv)->pending_setup.divisor) \
112 	  && ((a)->control == (priv)->pending_setup.control) \
113 	  && ((a)->frame_fmt == (priv)->pending_setup.frame_fmt))
114 
115 /* function prototypes */
116 static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port);
117 static void oti6858_close(struct usb_serial_port *port);
118 static void oti6858_set_termios(struct tty_struct *tty,
119 				struct usb_serial_port *port,
120 				const struct ktermios *old_termios);
121 static void oti6858_init_termios(struct tty_struct *tty);
122 static void oti6858_read_int_callback(struct urb *urb);
123 static void oti6858_read_bulk_callback(struct urb *urb);
124 static void oti6858_write_bulk_callback(struct urb *urb);
125 static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
126 			const unsigned char *buf, int count);
127 static unsigned int oti6858_write_room(struct tty_struct *tty);
128 static unsigned int oti6858_chars_in_buffer(struct tty_struct *tty);
129 static int oti6858_tiocmget(struct tty_struct *tty);
130 static int oti6858_tiocmset(struct tty_struct *tty,
131 				unsigned int set, unsigned int clear);
132 static int oti6858_port_probe(struct usb_serial_port *port);
133 static void oti6858_port_remove(struct usb_serial_port *port);
134 
135 /* device info */
136 static struct usb_serial_driver oti6858_device = {
137 	.driver = {
138 		.name =		"oti6858",
139 	},
140 	.id_table =		id_table,
141 	.num_ports =		1,
142 	.num_bulk_in =		1,
143 	.num_bulk_out =		1,
144 	.num_interrupt_in =	1,
145 	.open =			oti6858_open,
146 	.close =		oti6858_close,
147 	.write =		oti6858_write,
148 	.set_termios =		oti6858_set_termios,
149 	.init_termios = 	oti6858_init_termios,
150 	.tiocmget =		oti6858_tiocmget,
151 	.tiocmset =		oti6858_tiocmset,
152 	.tiocmiwait =		usb_serial_generic_tiocmiwait,
153 	.read_bulk_callback =	oti6858_read_bulk_callback,
154 	.read_int_callback =	oti6858_read_int_callback,
155 	.write_bulk_callback =	oti6858_write_bulk_callback,
156 	.write_room =		oti6858_write_room,
157 	.chars_in_buffer =	oti6858_chars_in_buffer,
158 	.port_probe =		oti6858_port_probe,
159 	.port_remove =		oti6858_port_remove,
160 };
161 
162 static struct usb_serial_driver * const serial_drivers[] = {
163 	&oti6858_device, NULL
164 };
165 
166 struct oti6858_private {
167 	spinlock_t lock;
168 
169 	struct oti6858_control_pkt status;
170 
171 	struct {
172 		u8 read_urb_in_use;
173 		u8 write_urb_in_use;
174 	} flags;
175 	struct delayed_work delayed_write_work;
176 
177 	struct {
178 		__le16 divisor;
179 		u8 frame_fmt;
180 		u8 control;
181 	} pending_setup;
182 	u8 transient;
183 	u8 setup_done;
184 	struct delayed_work delayed_setup_work;
185 
186 	struct usb_serial_port *port;   /* USB port with which associated */
187 };
188 
189 static void setup_line(struct work_struct *work)
190 {
191 	struct oti6858_private *priv = container_of(work,
192 			struct oti6858_private, delayed_setup_work.work);
193 	struct usb_serial_port *port = priv->port;
194 	struct oti6858_control_pkt *new_setup;
195 	unsigned long flags;
196 	int result;
197 
198 	new_setup = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
199 	if (!new_setup) {
200 		/* we will try again */
201 		schedule_delayed_work(&priv->delayed_setup_work,
202 						msecs_to_jiffies(2));
203 		return;
204 	}
205 
206 	result = usb_control_msg(port->serial->dev,
207 				usb_rcvctrlpipe(port->serial->dev, 0),
208 				OTI6858_REQ_T_GET_STATUS,
209 				OTI6858_REQ_GET_STATUS,
210 				0, 0,
211 				new_setup, OTI6858_CTRL_PKT_SIZE,
212 				100);
213 
214 	if (result != OTI6858_CTRL_PKT_SIZE) {
215 		dev_err(&port->dev, "%s(): error reading status\n", __func__);
216 		kfree(new_setup);
217 		/* we will try again */
218 		schedule_delayed_work(&priv->delayed_setup_work,
219 							msecs_to_jiffies(2));
220 		return;
221 	}
222 
223 	spin_lock_irqsave(&priv->lock, flags);
224 	if (!OTI6858_CTRL_EQUALS_PENDING(new_setup, priv)) {
225 		new_setup->divisor = priv->pending_setup.divisor;
226 		new_setup->control = priv->pending_setup.control;
227 		new_setup->frame_fmt = priv->pending_setup.frame_fmt;
228 
229 		spin_unlock_irqrestore(&priv->lock, flags);
230 		result = usb_control_msg(port->serial->dev,
231 					usb_sndctrlpipe(port->serial->dev, 0),
232 					OTI6858_REQ_T_SET_LINE,
233 					OTI6858_REQ_SET_LINE,
234 					0, 0,
235 					new_setup, OTI6858_CTRL_PKT_SIZE,
236 					100);
237 	} else {
238 		spin_unlock_irqrestore(&priv->lock, flags);
239 		result = 0;
240 	}
241 	kfree(new_setup);
242 
243 	spin_lock_irqsave(&priv->lock, flags);
244 	if (result != OTI6858_CTRL_PKT_SIZE)
245 		priv->transient = 0;
246 	priv->setup_done = 1;
247 	spin_unlock_irqrestore(&priv->lock, flags);
248 
249 	dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
250 	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
251 	if (result != 0) {
252 		dev_err(&port->dev, "%s(): usb_submit_urb() failed with error %d\n",
253 			__func__, result);
254 	}
255 }
256 
257 static void send_data(struct work_struct *work)
258 {
259 	struct oti6858_private *priv = container_of(work,
260 			struct oti6858_private, delayed_write_work.work);
261 	struct usb_serial_port *port = priv->port;
262 	int count = 0, result;
263 	unsigned long flags;
264 	u8 *allow;
265 
266 	spin_lock_irqsave(&priv->lock, flags);
267 	if (priv->flags.write_urb_in_use) {
268 		spin_unlock_irqrestore(&priv->lock, flags);
269 		schedule_delayed_work(&priv->delayed_write_work,
270 						msecs_to_jiffies(2));
271 		return;
272 	}
273 	priv->flags.write_urb_in_use = 1;
274 	spin_unlock_irqrestore(&priv->lock, flags);
275 
276 	spin_lock_irqsave(&port->lock, flags);
277 	count = kfifo_len(&port->write_fifo);
278 	spin_unlock_irqrestore(&port->lock, flags);
279 
280 	if (count > port->bulk_out_size)
281 		count = port->bulk_out_size;
282 
283 	if (count != 0) {
284 		allow = kmalloc(1, GFP_KERNEL);
285 		if (!allow)
286 			return;
287 
288 		result = usb_control_msg(port->serial->dev,
289 				usb_rcvctrlpipe(port->serial->dev, 0),
290 				OTI6858_REQ_T_CHECK_TXBUFF,
291 				OTI6858_REQ_CHECK_TXBUFF,
292 				count, 0, allow, 1, 100);
293 		if (result != 1 || *allow != 0)
294 			count = 0;
295 		kfree(allow);
296 	}
297 
298 	if (count == 0) {
299 		priv->flags.write_urb_in_use = 0;
300 
301 		dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
302 		result = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
303 		if (result != 0) {
304 			dev_err(&port->dev, "%s(): usb_submit_urb() failed with error %d\n",
305 				__func__, result);
306 		}
307 		return;
308 	}
309 
310 	count = kfifo_out_locked(&port->write_fifo,
311 					port->write_urb->transfer_buffer,
312 					count, &port->lock);
313 	port->write_urb->transfer_buffer_length = count;
314 	result = usb_submit_urb(port->write_urb, GFP_NOIO);
315 	if (result != 0) {
316 		dev_err_console(port, "%s(): usb_submit_urb() failed with error %d\n",
317 				__func__, result);
318 		priv->flags.write_urb_in_use = 0;
319 	}
320 
321 	usb_serial_port_softint(port);
322 }
323 
324 static int oti6858_port_probe(struct usb_serial_port *port)
325 {
326 	struct oti6858_private *priv;
327 
328 	priv = kzalloc_obj(*priv);
329 	if (!priv)
330 		return -ENOMEM;
331 
332 	spin_lock_init(&priv->lock);
333 	priv->port = port;
334 	INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line);
335 	INIT_DELAYED_WORK(&priv->delayed_write_work, send_data);
336 
337 	usb_set_serial_port_data(port, priv);
338 
339 	port->port.drain_delay = 256;	/* FIXME: check the FIFO length */
340 
341 	return 0;
342 }
343 
344 static void oti6858_port_remove(struct usb_serial_port *port)
345 {
346 	struct oti6858_private *priv;
347 
348 	priv = usb_get_serial_port_data(port);
349 	kfree(priv);
350 }
351 
352 static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
353 			const unsigned char *buf, int count)
354 {
355 	if (!count)
356 		return count;
357 
358 	count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
359 
360 	return count;
361 }
362 
363 static unsigned int oti6858_write_room(struct tty_struct *tty)
364 {
365 	struct usb_serial_port *port = tty->driver_data;
366 	unsigned int room;
367 	unsigned long flags;
368 
369 	spin_lock_irqsave(&port->lock, flags);
370 	room = kfifo_avail(&port->write_fifo);
371 	spin_unlock_irqrestore(&port->lock, flags);
372 
373 	return room;
374 }
375 
376 static unsigned int oti6858_chars_in_buffer(struct tty_struct *tty)
377 {
378 	struct usb_serial_port *port = tty->driver_data;
379 	unsigned int chars;
380 	unsigned long flags;
381 
382 	spin_lock_irqsave(&port->lock, flags);
383 	chars = kfifo_len(&port->write_fifo);
384 	spin_unlock_irqrestore(&port->lock, flags);
385 
386 	return chars;
387 }
388 
389 static void oti6858_init_termios(struct tty_struct *tty)
390 {
391 	tty_encode_baud_rate(tty, 38400, 38400);
392 }
393 
394 static void oti6858_set_termios(struct tty_struct *tty,
395 				struct usb_serial_port *port,
396 				const struct ktermios *old_termios)
397 {
398 	struct oti6858_private *priv = usb_get_serial_port_data(port);
399 	unsigned long flags;
400 	unsigned int cflag;
401 	u8 frame_fmt, control;
402 	__le16 divisor;
403 	int br;
404 
405 	cflag = tty->termios.c_cflag;
406 
407 	spin_lock_irqsave(&priv->lock, flags);
408 	frame_fmt = priv->pending_setup.frame_fmt;
409 	control = priv->pending_setup.control;
410 	spin_unlock_irqrestore(&priv->lock, flags);
411 
412 	frame_fmt &= ~FMT_DATA_BITS_MASK;
413 	switch (cflag & CSIZE) {
414 	case CS5:
415 		frame_fmt |= FMT_DATA_BITS_5;
416 		break;
417 	case CS6:
418 		frame_fmt |= FMT_DATA_BITS_6;
419 		break;
420 	case CS7:
421 		frame_fmt |= FMT_DATA_BITS_7;
422 		break;
423 	default:
424 	case CS8:
425 		frame_fmt |= FMT_DATA_BITS_8;
426 		break;
427 	}
428 
429 	/* manufacturer claims that this device can work with baud rates
430 	 * up to 3 Mbps; I've tested it only on 115200 bps, so I can't
431 	 * guarantee that any other baud rate will work (especially
432 	 * the higher ones)
433 	 */
434 	br = tty_get_baud_rate(tty);
435 	if (br == 0) {
436 		divisor = 0;
437 	} else {
438 		int real_br;
439 		int new_divisor;
440 		br = min(br, OTI6858_MAX_BAUD_RATE);
441 
442 		new_divisor = (96000000 + 8 * br) / (16 * br);
443 		real_br = 96000000 / (16 * new_divisor);
444 		divisor = cpu_to_le16(new_divisor);
445 		tty_encode_baud_rate(tty, real_br, real_br);
446 	}
447 
448 	frame_fmt &= ~FMT_STOP_BITS_MASK;
449 	if ((cflag & CSTOPB) != 0)
450 		frame_fmt |= FMT_STOP_BITS_2;
451 	else
452 		frame_fmt |= FMT_STOP_BITS_1;
453 
454 	frame_fmt &= ~FMT_PARITY_MASK;
455 	if ((cflag & PARENB) != 0) {
456 		if ((cflag & PARODD) != 0)
457 			frame_fmt |= FMT_PARITY_ODD;
458 		else
459 			frame_fmt |= FMT_PARITY_EVEN;
460 	} else {
461 		frame_fmt |= FMT_PARITY_NONE;
462 	}
463 
464 	control &= ~CONTROL_MASK;
465 	if ((cflag & CRTSCTS) != 0)
466 		control |= (CONTROL_DTR_HIGH | CONTROL_RTS_HIGH);
467 
468 	/* change control lines if we are switching to or from B0 */
469 	/* FIXME:
470 	spin_lock_irqsave(&priv->lock, flags);
471 	control = priv->line_control;
472 	if ((cflag & CBAUD) == B0)
473 		priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
474 	else
475 		priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
476 	if (control != priv->line_control) {
477 		control = priv->line_control;
478 		spin_unlock_irqrestore(&priv->lock, flags);
479 		set_control_lines(serial->dev, control);
480 	} else {
481 		spin_unlock_irqrestore(&priv->lock, flags);
482 	}
483 	*/
484 
485 	spin_lock_irqsave(&priv->lock, flags);
486 	if (divisor != priv->pending_setup.divisor
487 			|| control != priv->pending_setup.control
488 			|| frame_fmt != priv->pending_setup.frame_fmt) {
489 		priv->pending_setup.divisor = divisor;
490 		priv->pending_setup.control = control;
491 		priv->pending_setup.frame_fmt = frame_fmt;
492 	}
493 	spin_unlock_irqrestore(&priv->lock, flags);
494 }
495 
496 static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port)
497 {
498 	struct oti6858_private *priv = usb_get_serial_port_data(port);
499 	struct usb_serial *serial = port->serial;
500 	struct oti6858_control_pkt *buf;
501 	unsigned long flags;
502 	int result;
503 
504 	usb_clear_halt(serial->dev, port->write_urb->pipe);
505 	usb_clear_halt(serial->dev, port->read_urb->pipe);
506 
507 	buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
508 	if (!buf)
509 		return -ENOMEM;
510 
511 	result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
512 				OTI6858_REQ_T_GET_STATUS,
513 				OTI6858_REQ_GET_STATUS,
514 				0, 0,
515 				buf, OTI6858_CTRL_PKT_SIZE,
516 				100);
517 	if (result != OTI6858_CTRL_PKT_SIZE) {
518 		/* assume default (after power-on reset) values */
519 		buf->divisor = cpu_to_le16(0x009c);	/* 38400 bps */
520 		buf->frame_fmt = 0x03;	/* 8N1 */
521 		buf->something = 0x43;
522 		buf->control = 0x4c;	/* DTR, RTS */
523 		buf->tx_status = 0x00;
524 		buf->pin_state = 0x5b;	/* RTS, CTS, DSR, DTR, RI, DCD */
525 		buf->rx_bytes_avail = 0x00;
526 	}
527 
528 	spin_lock_irqsave(&priv->lock, flags);
529 	memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE);
530 	priv->pending_setup.divisor = buf->divisor;
531 	priv->pending_setup.frame_fmt = buf->frame_fmt;
532 	priv->pending_setup.control = buf->control;
533 	spin_unlock_irqrestore(&priv->lock, flags);
534 	kfree(buf);
535 
536 	dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
537 	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
538 	if (result != 0) {
539 		dev_err(&port->dev, "%s(): usb_submit_urb() failed with error %d\n",
540 			__func__, result);
541 		oti6858_close(port);
542 		return result;
543 	}
544 
545 	/* setup termios */
546 	if (tty)
547 		oti6858_set_termios(tty, port, NULL);
548 
549 	return 0;
550 }
551 
552 static void oti6858_close(struct usb_serial_port *port)
553 {
554 	struct oti6858_private *priv = usb_get_serial_port_data(port);
555 	unsigned long flags;
556 
557 	spin_lock_irqsave(&port->lock, flags);
558 	/* clear out any remaining data in the buffer */
559 	kfifo_reset_out(&port->write_fifo);
560 	spin_unlock_irqrestore(&port->lock, flags);
561 
562 	dev_dbg(&port->dev, "%s(): after buf_clear()\n", __func__);
563 
564 	/* cancel scheduled setup */
565 	cancel_delayed_work_sync(&priv->delayed_setup_work);
566 	cancel_delayed_work_sync(&priv->delayed_write_work);
567 
568 	/* shutdown our urbs */
569 	dev_dbg(&port->dev, "%s(): shutting down urbs\n", __func__);
570 	usb_kill_urb(port->write_urb);
571 	usb_kill_urb(port->read_urb);
572 	usb_kill_urb(port->interrupt_in_urb);
573 }
574 
575 static int oti6858_tiocmset(struct tty_struct *tty,
576 				unsigned int set, unsigned int clear)
577 {
578 	struct usb_serial_port *port = tty->driver_data;
579 	struct oti6858_private *priv = usb_get_serial_port_data(port);
580 	unsigned long flags;
581 	u8 control;
582 
583 	dev_dbg(&port->dev, "%s(set = 0x%08x, clear = 0x%08x)\n",
584 		__func__, set, clear);
585 
586 	/* FIXME: check if this is correct (active high/low) */
587 	spin_lock_irqsave(&priv->lock, flags);
588 	control = priv->pending_setup.control;
589 	if ((set & TIOCM_RTS) != 0)
590 		control |= CONTROL_RTS_HIGH;
591 	if ((set & TIOCM_DTR) != 0)
592 		control |= CONTROL_DTR_HIGH;
593 	if ((clear & TIOCM_RTS) != 0)
594 		control &= ~CONTROL_RTS_HIGH;
595 	if ((clear & TIOCM_DTR) != 0)
596 		control &= ~CONTROL_DTR_HIGH;
597 
598 	if (control != priv->pending_setup.control)
599 		priv->pending_setup.control = control;
600 
601 	spin_unlock_irqrestore(&priv->lock, flags);
602 	return 0;
603 }
604 
605 static int oti6858_tiocmget(struct tty_struct *tty)
606 {
607 	struct usb_serial_port *port = tty->driver_data;
608 	struct oti6858_private *priv = usb_get_serial_port_data(port);
609 	unsigned long flags;
610 	unsigned pin_state;
611 	unsigned result = 0;
612 
613 	spin_lock_irqsave(&priv->lock, flags);
614 	pin_state = priv->status.pin_state & PIN_MASK;
615 	spin_unlock_irqrestore(&priv->lock, flags);
616 
617 	/* FIXME: check if this is correct (active high/low) */
618 	if ((pin_state & PIN_RTS) != 0)
619 		result |= TIOCM_RTS;
620 	if ((pin_state & PIN_CTS) != 0)
621 		result |= TIOCM_CTS;
622 	if ((pin_state & PIN_DSR) != 0)
623 		result |= TIOCM_DSR;
624 	if ((pin_state & PIN_DTR) != 0)
625 		result |= TIOCM_DTR;
626 	if ((pin_state & PIN_RI) != 0)
627 		result |= TIOCM_RI;
628 	if ((pin_state & PIN_DCD) != 0)
629 		result |= TIOCM_CD;
630 
631 	dev_dbg(&port->dev, "%s() = 0x%08x\n", __func__, result);
632 
633 	return result;
634 }
635 
636 static void oti6858_read_int_callback(struct urb *urb)
637 {
638 	struct usb_serial_port *port =  urb->context;
639 	struct oti6858_private *priv = usb_get_serial_port_data(port);
640 	int transient = 0, can_recv = 0, resubmit = 1;
641 	int status = urb->status;
642 
643 	switch (status) {
644 	case 0:
645 		/* success */
646 		break;
647 	case -ECONNRESET:
648 	case -ENOENT:
649 	case -ESHUTDOWN:
650 		/* this urb is terminated, clean up */
651 		dev_dbg(&urb->dev->dev, "%s(): urb shutting down with status: %d\n",
652 			__func__, status);
653 		return;
654 	default:
655 		dev_dbg(&urb->dev->dev, "%s(): nonzero urb status received: %d\n",
656 			__func__, status);
657 		break;
658 	}
659 
660 	if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
661 		struct oti6858_control_pkt *xs = urb->transfer_buffer;
662 		unsigned long flags;
663 
664 		spin_lock_irqsave(&priv->lock, flags);
665 
666 		if (!priv->transient) {
667 			if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
668 				if (xs->rx_bytes_avail == 0) {
669 					priv->transient = 4;
670 					priv->setup_done = 0;
671 					resubmit = 0;
672 					dev_dbg(&port->dev, "%s(): scheduling setup_line()\n", __func__);
673 					schedule_delayed_work(&priv->delayed_setup_work, 0);
674 				}
675 			}
676 		} else {
677 			if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
678 				priv->transient = 0;
679 			} else if (!priv->setup_done) {
680 				resubmit = 0;
681 			} else if (--priv->transient == 0) {
682 				if (xs->rx_bytes_avail == 0) {
683 					priv->transient = 4;
684 					priv->setup_done = 0;
685 					resubmit = 0;
686 					dev_dbg(&port->dev, "%s(): scheduling setup_line()\n", __func__);
687 					schedule_delayed_work(&priv->delayed_setup_work, 0);
688 				}
689 			}
690 		}
691 
692 		if (!priv->transient) {
693 			u8 delta = xs->pin_state ^ priv->status.pin_state;
694 
695 			if (delta & PIN_MSR_MASK) {
696 				if (delta & PIN_CTS)
697 					port->icount.cts++;
698 				if (delta & PIN_DSR)
699 					port->icount.dsr++;
700 				if (delta & PIN_RI)
701 					port->icount.rng++;
702 				if (delta & PIN_DCD)
703 					port->icount.dcd++;
704 
705 				wake_up_interruptible(&port->port.delta_msr_wait);
706 			}
707 
708 			memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
709 		}
710 
711 		if (!priv->transient && xs->rx_bytes_avail != 0) {
712 			can_recv = xs->rx_bytes_avail;
713 			priv->flags.read_urb_in_use = 1;
714 		}
715 
716 		transient = priv->transient;
717 		spin_unlock_irqrestore(&priv->lock, flags);
718 	}
719 
720 	if (can_recv) {
721 		int result;
722 
723 		result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
724 		if (result != 0) {
725 			priv->flags.read_urb_in_use = 0;
726 			dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
727 					" error %d\n", __func__, result);
728 		} else {
729 			resubmit = 0;
730 		}
731 	} else if (!transient) {
732 		unsigned long flags;
733 		int count;
734 
735 		spin_lock_irqsave(&port->lock, flags);
736 		count = kfifo_len(&port->write_fifo);
737 		spin_unlock_irqrestore(&port->lock, flags);
738 
739 		spin_lock_irqsave(&priv->lock, flags);
740 		if (priv->flags.write_urb_in_use == 0 && count != 0) {
741 			schedule_delayed_work(&priv->delayed_write_work, 0);
742 			resubmit = 0;
743 		}
744 		spin_unlock_irqrestore(&priv->lock, flags);
745 	}
746 
747 	if (resubmit) {
748 		int result;
749 
750 /*		dev_dbg(&urb->dev->dev, "%s(): submitting interrupt urb\n", __func__); */
751 		result = usb_submit_urb(urb, GFP_ATOMIC);
752 		if (result != 0) {
753 			dev_err(&urb->dev->dev,
754 					"%s(): usb_submit_urb() failed with"
755 					" error %d\n", __func__, result);
756 		}
757 	}
758 }
759 
760 static void oti6858_read_bulk_callback(struct urb *urb)
761 {
762 	struct usb_serial_port *port =  urb->context;
763 	struct oti6858_private *priv = usb_get_serial_port_data(port);
764 	unsigned char *data = urb->transfer_buffer;
765 	unsigned long flags;
766 	int status = urb->status;
767 	int result;
768 
769 	spin_lock_irqsave(&priv->lock, flags);
770 	priv->flags.read_urb_in_use = 0;
771 	spin_unlock_irqrestore(&priv->lock, flags);
772 
773 	if (status != 0) {
774 		dev_dbg(&urb->dev->dev, "%s(): unable to handle the error, exiting\n", __func__);
775 		return;
776 	}
777 
778 	if (urb->actual_length > 0) {
779 		tty_insert_flip_string(&port->port, data, urb->actual_length);
780 		tty_flip_buffer_push(&port->port);
781 	}
782 
783 	/* schedule the interrupt urb */
784 	result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
785 	if (result != 0 && result != -EPERM) {
786 		dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
787 				" error %d\n", __func__, result);
788 	}
789 }
790 
791 static void oti6858_write_bulk_callback(struct urb *urb)
792 {
793 	struct usb_serial_port *port =  urb->context;
794 	struct oti6858_private *priv = usb_get_serial_port_data(port);
795 	int status = urb->status;
796 	int result;
797 
798 	switch (status) {
799 	case 0:
800 		/* success */
801 		break;
802 	case -ECONNRESET:
803 	case -ENOENT:
804 	case -ESHUTDOWN:
805 		/* this urb is terminated, clean up */
806 		dev_dbg(&urb->dev->dev, "%s(): urb shutting down with status: %d\n", __func__, status);
807 		priv->flags.write_urb_in_use = 0;
808 		return;
809 	default:
810 		/* error in the urb, so we have to resubmit it */
811 		dev_dbg(&urb->dev->dev, "%s(): nonzero write bulk status received: %d\n", __func__, status);
812 		dev_dbg(&urb->dev->dev, "%s(): overflow in write\n", __func__);
813 
814 		port->write_urb->transfer_buffer_length = 1;
815 		result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
816 		if (result) {
817 			dev_err_console(port, "%s(): usb_submit_urb() failed,"
818 					" error %d\n", __func__, result);
819 		} else {
820 			return;
821 		}
822 	}
823 
824 	priv->flags.write_urb_in_use = 0;
825 
826 	/* schedule the interrupt urb if we are still open */
827 	dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
828 	result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
829 	if (result != 0) {
830 		dev_err(&port->dev, "%s(): failed submitting int urb,"
831 					" error %d\n", __func__, result);
832 	}
833 }
834 
835 module_usb_serial_driver(serial_drivers, id_table);
836 
837 MODULE_DESCRIPTION(OTI6858_DESCRIPTION);
838 MODULE_AUTHOR(OTI6858_AUTHOR);
839 MODULE_LICENSE("GPL v2");
840