xref: /linux/drivers/usb/serial/ark3116.c (revision c75c5ab575af7db707689cdbb5a5c458e9a034bb)
1 /*
2  * Copyright (C) 2009 by Bart Hartgers (bart.hartgers+ark3116@gmail.com)
3  * Original version:
4  * Copyright (C) 2006
5  *   Simon Schulz (ark3116_driver <at> auctionant.de)
6  *
7  * ark3116
8  * - implements a driver for the arkmicro ark3116 chipset (vendor=0x6547,
9  *   productid=0x0232) (used in a datacable called KQ-U8A)
10  *
11  * Supports full modem status lines, break, hardware flow control. Does not
12  * support software flow control, since I do not know how to enable it in hw.
13  *
14  * This driver is a essentially new implementation. I initially dug
15  * into the old ark3116.c driver and suddenly realized the ark3116 is
16  * a 16450 with a USB interface glued to it. See comments at the
17  * bottom of this file.
18  *
19  * This program is free software; you can redistribute it and/or modify it
20  * under the terms of the GNU General Public License as published by the
21  * Free Software Foundation; either version 2 of the License, or (at your
22  * option) any later version.
23  */
24 
25 #include <linux/kernel.h>
26 #include <linux/init.h>
27 #include <linux/ioctl.h>
28 #include <linux/tty.h>
29 #include <linux/slab.h>
30 #include <linux/tty_flip.h>
31 #include <linux/module.h>
32 #include <linux/usb.h>
33 #include <linux/usb/serial.h>
34 #include <linux/serial.h>
35 #include <linux/serial_reg.h>
36 #include <linux/uaccess.h>
37 #include <linux/mutex.h>
38 #include <linux/spinlock.h>
39 
40 #define DRIVER_AUTHOR "Bart Hartgers <bart.hartgers+ark3116@gmail.com>"
41 #define DRIVER_DESC "USB ARK3116 serial/IrDA driver"
42 #define DRIVER_DEV_DESC "ARK3116 RS232/IrDA"
43 #define DRIVER_NAME "ark3116"
44 
45 /* usb timeout of 1 second */
46 #define ARK_TIMEOUT (1*HZ)
47 
48 static const struct usb_device_id id_table[] = {
49 	{ USB_DEVICE(0x6547, 0x0232) },
50 	{ USB_DEVICE(0x18ec, 0x3118) },		/* USB to IrDA adapter */
51 	{ },
52 };
53 MODULE_DEVICE_TABLE(usb, id_table);
54 
55 static int is_irda(struct usb_serial *serial)
56 {
57 	struct usb_device *dev = serial->dev;
58 	if (le16_to_cpu(dev->descriptor.idVendor) == 0x18ec &&
59 			le16_to_cpu(dev->descriptor.idProduct) == 0x3118)
60 		return 1;
61 	return 0;
62 }
63 
64 struct ark3116_private {
65 	struct async_icount	icount;
66 	int			irda;	/* 1 for irda device */
67 
68 	/* protects hw register updates */
69 	struct mutex		hw_lock;
70 
71 	int			quot;	/* baudrate divisor */
72 	__u32			lcr;	/* line control register value */
73 	__u32			hcr;	/* handshake control register (0x8)
74 					 * value */
75 	__u32			mcr;	/* modem contol register value */
76 
77 	/* protects the status values below */
78 	spinlock_t		status_lock;
79 	__u32			msr;	/* modem status register value */
80 	__u32			lsr;	/* line status register value */
81 };
82 
83 static int ark3116_write_reg(struct usb_serial *serial,
84 			     unsigned reg, __u8 val)
85 {
86 	int result;
87 	 /* 0xfe 0x40 are magic values taken from original driver */
88 	result = usb_control_msg(serial->dev,
89 				 usb_sndctrlpipe(serial->dev, 0),
90 				 0xfe, 0x40, val, reg,
91 				 NULL, 0, ARK_TIMEOUT);
92 	return result;
93 }
94 
95 static int ark3116_read_reg(struct usb_serial *serial,
96 			    unsigned reg, unsigned char *buf)
97 {
98 	int result;
99 	/* 0xfe 0xc0 are magic values taken from original driver */
100 	result = usb_control_msg(serial->dev,
101 				 usb_rcvctrlpipe(serial->dev, 0),
102 				 0xfe, 0xc0, 0, reg,
103 				 buf, 1, ARK_TIMEOUT);
104 	if (result < 0)
105 		return result;
106 	else
107 		return buf[0];
108 }
109 
110 static inline int calc_divisor(int bps)
111 {
112 	/* Original ark3116 made some exceptions in rounding here
113 	 * because windows did the same. Assume that is not really
114 	 * necessary.
115 	 * Crystal is 12MHz, probably because of USB, but we divide by 4?
116 	 */
117 	return (12000000 + 2*bps) / (4*bps);
118 }
119 
120 static int ark3116_attach(struct usb_serial *serial)
121 {
122 	/* make sure we have our end-points */
123 	if ((serial->num_bulk_in == 0) ||
124 	    (serial->num_bulk_out == 0) ||
125 	    (serial->num_interrupt_in == 0)) {
126 		dev_err(&serial->dev->dev,
127 			"%s - missing endpoint - "
128 			"bulk in: %d, bulk out: %d, int in %d\n",
129 			KBUILD_MODNAME,
130 			serial->num_bulk_in,
131 			serial->num_bulk_out,
132 			serial->num_interrupt_in);
133 		return -EINVAL;
134 	}
135 
136 	return 0;
137 }
138 
139 static int ark3116_port_probe(struct usb_serial_port *port)
140 {
141 	struct usb_serial *serial = port->serial;
142 	struct ark3116_private *priv;
143 
144 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
145 	if (!priv)
146 		return -ENOMEM;
147 
148 	mutex_init(&priv->hw_lock);
149 	spin_lock_init(&priv->status_lock);
150 
151 	priv->irda = is_irda(serial);
152 
153 	usb_set_serial_port_data(port, priv);
154 
155 	/* setup the hardware */
156 	ark3116_write_reg(serial, UART_IER, 0);
157 	/* disable DMA */
158 	ark3116_write_reg(serial, UART_FCR, 0);
159 	/* handshake control */
160 	priv->hcr = 0;
161 	ark3116_write_reg(serial, 0x8     , 0);
162 	/* modem control */
163 	priv->mcr = 0;
164 	ark3116_write_reg(serial, UART_MCR, 0);
165 
166 	if (!(priv->irda)) {
167 		ark3116_write_reg(serial, 0xb , 0);
168 	} else {
169 		ark3116_write_reg(serial, 0xb , 1);
170 		ark3116_write_reg(serial, 0xc , 0);
171 		ark3116_write_reg(serial, 0xd , 0x41);
172 		ark3116_write_reg(serial, 0xa , 1);
173 	}
174 
175 	/* setup baudrate */
176 	ark3116_write_reg(serial, UART_LCR, UART_LCR_DLAB);
177 
178 	/* setup for 9600 8N1 */
179 	priv->quot = calc_divisor(9600);
180 	ark3116_write_reg(serial, UART_DLL, priv->quot & 0xff);
181 	ark3116_write_reg(serial, UART_DLM, (priv->quot>>8) & 0xff);
182 
183 	priv->lcr = UART_LCR_WLEN8;
184 	ark3116_write_reg(serial, UART_LCR, UART_LCR_WLEN8);
185 
186 	ark3116_write_reg(serial, 0xe, 0);
187 
188 	if (priv->irda)
189 		ark3116_write_reg(serial, 0x9, 0);
190 
191 	dev_info(&serial->dev->dev,
192 		"%s using %s mode\n",
193 		KBUILD_MODNAME,
194 		priv->irda ? "IrDA" : "RS232");
195 	return 0;
196 }
197 
198 static int ark3116_port_remove(struct usb_serial_port *port)
199 {
200 	struct ark3116_private *priv = usb_get_serial_port_data(port);
201 
202 	/* device is closed, so URBs and DMA should be down */
203 	mutex_destroy(&priv->hw_lock);
204 	kfree(priv);
205 
206 	return 0;
207 }
208 
209 static void ark3116_init_termios(struct tty_struct *tty)
210 {
211 	struct ktermios *termios = &tty->termios;
212 	*termios = tty_std_termios;
213 	termios->c_cflag = B9600 | CS8
214 				      | CREAD | HUPCL | CLOCAL;
215 	termios->c_ispeed = 9600;
216 	termios->c_ospeed = 9600;
217 }
218 
219 static void ark3116_set_termios(struct tty_struct *tty,
220 				struct usb_serial_port *port,
221 				struct ktermios *old_termios)
222 {
223 	struct usb_serial *serial = port->serial;
224 	struct ark3116_private *priv = usb_get_serial_port_data(port);
225 	struct ktermios *termios = &tty->termios;
226 	unsigned int cflag = termios->c_cflag;
227 	int bps = tty_get_baud_rate(tty);
228 	int quot;
229 	__u8 lcr, hcr, eval;
230 
231 	/* set data bit count */
232 	switch (cflag & CSIZE) {
233 	case CS5:
234 		lcr = UART_LCR_WLEN5;
235 		break;
236 	case CS6:
237 		lcr = UART_LCR_WLEN6;
238 		break;
239 	case CS7:
240 		lcr = UART_LCR_WLEN7;
241 		break;
242 	default:
243 	case CS8:
244 		lcr = UART_LCR_WLEN8;
245 		break;
246 	}
247 	if (cflag & CSTOPB)
248 		lcr |= UART_LCR_STOP;
249 	if (cflag & PARENB)
250 		lcr |= UART_LCR_PARITY;
251 	if (!(cflag & PARODD))
252 		lcr |= UART_LCR_EPAR;
253 #ifdef CMSPAR
254 	if (cflag & CMSPAR)
255 		lcr |= UART_LCR_SPAR;
256 #endif
257 	/* handshake control */
258 	hcr = (cflag & CRTSCTS) ? 0x03 : 0x00;
259 
260 	/* calc baudrate */
261 	dev_dbg(&port->dev, "%s - setting bps to %d\n", __func__, bps);
262 	eval = 0;
263 	switch (bps) {
264 	case 0:
265 		quot = calc_divisor(9600);
266 		break;
267 	default:
268 		if ((bps < 75) || (bps > 3000000))
269 			bps = 9600;
270 		quot = calc_divisor(bps);
271 		break;
272 	case 460800:
273 		eval = 1;
274 		quot = calc_divisor(bps);
275 		break;
276 	case 921600:
277 		eval = 2;
278 		quot = calc_divisor(bps);
279 		break;
280 	}
281 
282 	/* Update state: synchronize */
283 	mutex_lock(&priv->hw_lock);
284 
285 	/* keep old LCR_SBC bit */
286 	lcr |= (priv->lcr & UART_LCR_SBC);
287 
288 	dev_dbg(&port->dev, "%s - setting hcr:0x%02x,lcr:0x%02x,quot:%d\n",
289 		__func__, hcr, lcr, quot);
290 
291 	/* handshake control */
292 	if (priv->hcr != hcr) {
293 		priv->hcr = hcr;
294 		ark3116_write_reg(serial, 0x8, hcr);
295 	}
296 
297 	/* baudrate */
298 	if (priv->quot != quot) {
299 		priv->quot = quot;
300 		priv->lcr = lcr; /* need to write lcr anyway */
301 
302 		/* disable DMA since transmit/receive is
303 		 * shadowed by UART_DLL
304 		 */
305 		ark3116_write_reg(serial, UART_FCR, 0);
306 
307 		ark3116_write_reg(serial, UART_LCR,
308 				  lcr|UART_LCR_DLAB);
309 		ark3116_write_reg(serial, UART_DLL, quot & 0xff);
310 		ark3116_write_reg(serial, UART_DLM, (quot>>8) & 0xff);
311 
312 		/* restore lcr */
313 		ark3116_write_reg(serial, UART_LCR, lcr);
314 		/* magic baudrate thingy: not sure what it does,
315 		 * but windows does this as well.
316 		 */
317 		ark3116_write_reg(serial, 0xe, eval);
318 
319 		/* enable DMA */
320 		ark3116_write_reg(serial, UART_FCR, UART_FCR_DMA_SELECT);
321 	} else if (priv->lcr != lcr) {
322 		priv->lcr = lcr;
323 		ark3116_write_reg(serial, UART_LCR, lcr);
324 	}
325 
326 	mutex_unlock(&priv->hw_lock);
327 
328 	/* check for software flow control */
329 	if (I_IXOFF(tty) || I_IXON(tty)) {
330 		dev_warn(&serial->dev->dev,
331 			 "%s: don't know how to do software flow control\n",
332 			 KBUILD_MODNAME);
333 	}
334 
335 	/* Don't rewrite B0 */
336 	if (tty_termios_baud_rate(termios))
337 		tty_termios_encode_baud_rate(termios, bps, bps);
338 }
339 
340 static void ark3116_close(struct usb_serial_port *port)
341 {
342 	struct usb_serial *serial = port->serial;
343 
344 	if (serial->dev) {
345 		/* disable DMA */
346 		ark3116_write_reg(serial, UART_FCR, 0);
347 
348 		/* deactivate interrupts */
349 		ark3116_write_reg(serial, UART_IER, 0);
350 
351 		usb_serial_generic_close(port);
352 		if (serial->num_interrupt_in)
353 			usb_kill_urb(port->interrupt_in_urb);
354 	}
355 
356 }
357 
358 static int ark3116_open(struct tty_struct *tty, struct usb_serial_port *port)
359 {
360 	struct ark3116_private *priv = usb_get_serial_port_data(port);
361 	struct usb_serial *serial = port->serial;
362 	unsigned char *buf;
363 	int result;
364 
365 	buf = kmalloc(1, GFP_KERNEL);
366 	if (buf == NULL)
367 		return -ENOMEM;
368 
369 	result = usb_serial_generic_open(tty, port);
370 	if (result) {
371 		dev_dbg(&port->dev,
372 			"%s - usb_serial_generic_open failed: %d\n",
373 			__func__, result);
374 		goto err_out;
375 	}
376 
377 	/* remove any data still left: also clears error state */
378 	ark3116_read_reg(serial, UART_RX, buf);
379 
380 	/* read modem status */
381 	priv->msr = ark3116_read_reg(serial, UART_MSR, buf);
382 	/* read line status */
383 	priv->lsr = ark3116_read_reg(serial, UART_LSR, buf);
384 
385 	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
386 	if (result) {
387 		dev_err(&port->dev, "submit irq_in urb failed %d\n",
388 			result);
389 		ark3116_close(port);
390 		goto err_out;
391 	}
392 
393 	/* activate interrupts */
394 	ark3116_write_reg(port->serial, UART_IER, UART_IER_MSI|UART_IER_RLSI);
395 
396 	/* enable DMA */
397 	ark3116_write_reg(port->serial, UART_FCR, UART_FCR_DMA_SELECT);
398 
399 	/* setup termios */
400 	if (tty)
401 		ark3116_set_termios(tty, port, NULL);
402 
403 err_out:
404 	kfree(buf);
405 	return result;
406 }
407 
408 static int ark3116_get_icount(struct tty_struct *tty,
409 					struct serial_icounter_struct *icount)
410 {
411 	struct usb_serial_port *port = tty->driver_data;
412 	struct ark3116_private *priv = usb_get_serial_port_data(port);
413 	struct async_icount cnow = priv->icount;
414 	icount->cts = cnow.cts;
415 	icount->dsr = cnow.dsr;
416 	icount->rng = cnow.rng;
417 	icount->dcd = cnow.dcd;
418 	icount->rx = cnow.rx;
419 	icount->tx = cnow.tx;
420 	icount->frame = cnow.frame;
421 	icount->overrun = cnow.overrun;
422 	icount->parity = cnow.parity;
423 	icount->brk = cnow.brk;
424 	icount->buf_overrun = cnow.buf_overrun;
425 	return 0;
426 }
427 
428 static int ark3116_ioctl(struct tty_struct *tty,
429 			 unsigned int cmd, unsigned long arg)
430 {
431 	struct usb_serial_port *port = tty->driver_data;
432 	struct ark3116_private *priv = usb_get_serial_port_data(port);
433 	struct serial_struct serstruct;
434 	void __user *user_arg = (void __user *)arg;
435 
436 	switch (cmd) {
437 	case TIOCGSERIAL:
438 		/* XXX: Some of these values are probably wrong. */
439 		memset(&serstruct, 0, sizeof(serstruct));
440 		serstruct.type = PORT_16654;
441 		serstruct.line = port->serial->minor;
442 		serstruct.port = port->number;
443 		serstruct.custom_divisor = 0;
444 		serstruct.baud_base = 460800;
445 
446 		if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
447 			return -EFAULT;
448 
449 		return 0;
450 	case TIOCSSERIAL:
451 		if (copy_from_user(&serstruct, user_arg, sizeof(serstruct)))
452 			return -EFAULT;
453 		return 0;
454 	case TIOCMIWAIT:
455 		for (;;) {
456 			struct async_icount prev = priv->icount;
457 			interruptible_sleep_on(&port->delta_msr_wait);
458 			/* see if a signal did it */
459 			if (signal_pending(current))
460 				return -ERESTARTSYS;
461 
462 			if (port->serial->disconnected)
463 				return -EIO;
464 
465 			if ((prev.rng == priv->icount.rng) &&
466 			    (prev.dsr == priv->icount.dsr) &&
467 			    (prev.dcd == priv->icount.dcd) &&
468 			    (prev.cts == priv->icount.cts))
469 				return -EIO;
470 			if ((arg & TIOCM_RNG &&
471 			     (prev.rng != priv->icount.rng)) ||
472 			    (arg & TIOCM_DSR &&
473 			     (prev.dsr != priv->icount.dsr)) ||
474 			    (arg & TIOCM_CD  &&
475 			     (prev.dcd != priv->icount.dcd)) ||
476 			    (arg & TIOCM_CTS &&
477 			     (prev.cts != priv->icount.cts)))
478 				return 0;
479 		}
480 		break;
481 	}
482 
483 	return -ENOIOCTLCMD;
484 }
485 
486 static int ark3116_tiocmget(struct tty_struct *tty)
487 {
488 	struct usb_serial_port *port = tty->driver_data;
489 	struct ark3116_private *priv = usb_get_serial_port_data(port);
490 	__u32 status;
491 	__u32 ctrl;
492 	unsigned long flags;
493 
494 	mutex_lock(&priv->hw_lock);
495 	ctrl = priv->mcr;
496 	mutex_unlock(&priv->hw_lock);
497 
498 	spin_lock_irqsave(&priv->status_lock, flags);
499 	status = priv->msr;
500 	spin_unlock_irqrestore(&priv->status_lock, flags);
501 
502 	return  (status & UART_MSR_DSR  ? TIOCM_DSR  : 0) |
503 		(status & UART_MSR_CTS  ? TIOCM_CTS  : 0) |
504 		(status & UART_MSR_RI   ? TIOCM_RI   : 0) |
505 		(status & UART_MSR_DCD  ? TIOCM_CD   : 0) |
506 		(ctrl   & UART_MCR_DTR  ? TIOCM_DTR  : 0) |
507 		(ctrl   & UART_MCR_RTS  ? TIOCM_RTS  : 0) |
508 		(ctrl   & UART_MCR_OUT1 ? TIOCM_OUT1 : 0) |
509 		(ctrl   & UART_MCR_OUT2 ? TIOCM_OUT2 : 0);
510 }
511 
512 static int ark3116_tiocmset(struct tty_struct *tty,
513 			unsigned set, unsigned clr)
514 {
515 	struct usb_serial_port *port = tty->driver_data;
516 	struct ark3116_private *priv = usb_get_serial_port_data(port);
517 
518 	/* we need to take the mutex here, to make sure that the value
519 	 * in priv->mcr is actually the one that is in the hardware
520 	 */
521 
522 	mutex_lock(&priv->hw_lock);
523 
524 	if (set & TIOCM_RTS)
525 		priv->mcr |= UART_MCR_RTS;
526 	if (set & TIOCM_DTR)
527 		priv->mcr |= UART_MCR_DTR;
528 	if (set & TIOCM_OUT1)
529 		priv->mcr |= UART_MCR_OUT1;
530 	if (set & TIOCM_OUT2)
531 		priv->mcr |= UART_MCR_OUT2;
532 	if (clr & TIOCM_RTS)
533 		priv->mcr &= ~UART_MCR_RTS;
534 	if (clr & TIOCM_DTR)
535 		priv->mcr &= ~UART_MCR_DTR;
536 	if (clr & TIOCM_OUT1)
537 		priv->mcr &= ~UART_MCR_OUT1;
538 	if (clr & TIOCM_OUT2)
539 		priv->mcr &= ~UART_MCR_OUT2;
540 
541 	ark3116_write_reg(port->serial, UART_MCR, priv->mcr);
542 
543 	mutex_unlock(&priv->hw_lock);
544 
545 	return 0;
546 }
547 
548 static void ark3116_break_ctl(struct tty_struct *tty, int break_state)
549 {
550 	struct usb_serial_port *port = tty->driver_data;
551 	struct ark3116_private *priv = usb_get_serial_port_data(port);
552 
553 	/* LCR is also used for other things: protect access */
554 	mutex_lock(&priv->hw_lock);
555 
556 	if (break_state)
557 		priv->lcr |= UART_LCR_SBC;
558 	else
559 		priv->lcr &= ~UART_LCR_SBC;
560 
561 	ark3116_write_reg(port->serial, UART_LCR, priv->lcr);
562 
563 	mutex_unlock(&priv->hw_lock);
564 }
565 
566 static void ark3116_update_msr(struct usb_serial_port *port, __u8 msr)
567 {
568 	struct ark3116_private *priv = usb_get_serial_port_data(port);
569 	unsigned long flags;
570 
571 	spin_lock_irqsave(&priv->status_lock, flags);
572 	priv->msr = msr;
573 	spin_unlock_irqrestore(&priv->status_lock, flags);
574 
575 	if (msr & UART_MSR_ANY_DELTA) {
576 		/* update input line counters */
577 		if (msr & UART_MSR_DCTS)
578 			priv->icount.cts++;
579 		if (msr & UART_MSR_DDSR)
580 			priv->icount.dsr++;
581 		if (msr & UART_MSR_DDCD)
582 			priv->icount.dcd++;
583 		if (msr & UART_MSR_TERI)
584 			priv->icount.rng++;
585 		wake_up_interruptible(&port->delta_msr_wait);
586 	}
587 }
588 
589 static void ark3116_update_lsr(struct usb_serial_port *port, __u8 lsr)
590 {
591 	struct ark3116_private *priv = usb_get_serial_port_data(port);
592 	unsigned long flags;
593 
594 	spin_lock_irqsave(&priv->status_lock, flags);
595 	/* combine bits */
596 	priv->lsr |= lsr;
597 	spin_unlock_irqrestore(&priv->status_lock, flags);
598 
599 	if (lsr&UART_LSR_BRK_ERROR_BITS) {
600 		if (lsr & UART_LSR_BI)
601 			priv->icount.brk++;
602 		if (lsr & UART_LSR_FE)
603 			priv->icount.frame++;
604 		if (lsr & UART_LSR_PE)
605 			priv->icount.parity++;
606 		if (lsr & UART_LSR_OE)
607 			priv->icount.overrun++;
608 	}
609 }
610 
611 static void ark3116_read_int_callback(struct urb *urb)
612 {
613 	struct usb_serial_port *port = urb->context;
614 	int status = urb->status;
615 	const __u8 *data = urb->transfer_buffer;
616 	int result;
617 
618 	switch (status) {
619 	case -ECONNRESET:
620 	case -ENOENT:
621 	case -ESHUTDOWN:
622 		/* this urb is terminated, clean up */
623 		dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
624 			__func__, status);
625 		return;
626 	default:
627 		dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
628 			__func__, status);
629 		break;
630 	case 0: /* success */
631 		/* discovered this by trail and error... */
632 		if ((urb->actual_length == 4) && (data[0] == 0xe8)) {
633 			const __u8 id = data[1]&UART_IIR_ID;
634 			dev_dbg(&port->dev, "%s: iir=%02x\n", __func__, data[1]);
635 			if (id == UART_IIR_MSI) {
636 				dev_dbg(&port->dev, "%s: msr=%02x\n",
637 					__func__, data[3]);
638 				ark3116_update_msr(port, data[3]);
639 				break;
640 			} else if (id == UART_IIR_RLSI) {
641 				dev_dbg(&port->dev, "%s: lsr=%02x\n",
642 					__func__, data[2]);
643 				ark3116_update_lsr(port, data[2]);
644 				break;
645 			}
646 		}
647 		/*
648 		 * Not sure what this data meant...
649 		 */
650 		usb_serial_debug_data(&port->dev, __func__,
651 				      urb->actual_length,
652 				      urb->transfer_buffer);
653 		break;
654 	}
655 
656 	result = usb_submit_urb(urb, GFP_ATOMIC);
657 	if (result)
658 		dev_err(&urb->dev->dev,
659 			"%s - Error %d submitting interrupt urb\n",
660 			__func__, result);
661 }
662 
663 
664 /* Data comes in via the bulk (data) URB, erors/interrupts via the int URB.
665  * This means that we cannot be sure which data byte has an associated error
666  * condition, so we report an error for all data in the next bulk read.
667  *
668  * Actually, there might even be a window between the bulk data leaving the
669  * ark and reading/resetting the lsr in the read_bulk_callback where an
670  * interrupt for the next data block could come in.
671  * Without somekind of ordering on the ark, we would have to report the
672  * error for the next block of data as well...
673  * For now, let's pretend this can't happen.
674  */
675 static void ark3116_process_read_urb(struct urb *urb)
676 {
677 	struct usb_serial_port *port = urb->context;
678 	struct ark3116_private *priv = usb_get_serial_port_data(port);
679 	unsigned char *data = urb->transfer_buffer;
680 	char tty_flag = TTY_NORMAL;
681 	unsigned long flags;
682 	__u32 lsr;
683 
684 	/* update line status */
685 	spin_lock_irqsave(&priv->status_lock, flags);
686 	lsr = priv->lsr;
687 	priv->lsr &= ~UART_LSR_BRK_ERROR_BITS;
688 	spin_unlock_irqrestore(&priv->status_lock, flags);
689 
690 	if (!urb->actual_length)
691 		return;
692 
693 	if (lsr & UART_LSR_BRK_ERROR_BITS) {
694 		if (lsr & UART_LSR_BI)
695 			tty_flag = TTY_BREAK;
696 		else if (lsr & UART_LSR_PE)
697 			tty_flag = TTY_PARITY;
698 		else if (lsr & UART_LSR_FE)
699 			tty_flag = TTY_FRAME;
700 
701 		/* overrun is special, not associated with a char */
702 		if (lsr & UART_LSR_OE)
703 			tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
704 	}
705 	tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
706 							urb->actual_length);
707 	tty_flip_buffer_push(&port->port);
708 }
709 
710 static struct usb_serial_driver ark3116_device = {
711 	.driver = {
712 		.owner =	THIS_MODULE,
713 		.name =		"ark3116",
714 	},
715 	.id_table =		id_table,
716 	.num_ports =		1,
717 	.attach =		ark3116_attach,
718 	.port_probe =		ark3116_port_probe,
719 	.port_remove =		ark3116_port_remove,
720 	.set_termios =		ark3116_set_termios,
721 	.init_termios =		ark3116_init_termios,
722 	.ioctl =		ark3116_ioctl,
723 	.tiocmget =		ark3116_tiocmget,
724 	.tiocmset =		ark3116_tiocmset,
725 	.get_icount =		ark3116_get_icount,
726 	.open =			ark3116_open,
727 	.close =		ark3116_close,
728 	.break_ctl = 		ark3116_break_ctl,
729 	.read_int_callback = 	ark3116_read_int_callback,
730 	.process_read_urb =	ark3116_process_read_urb,
731 };
732 
733 static struct usb_serial_driver * const serial_drivers[] = {
734 	&ark3116_device, NULL
735 };
736 
737 module_usb_serial_driver(serial_drivers, id_table);
738 
739 MODULE_LICENSE("GPL");
740 
741 MODULE_AUTHOR(DRIVER_AUTHOR);
742 MODULE_DESCRIPTION(DRIVER_DESC);
743 
744 /*
745  * The following describes what I learned from studying the old
746  * ark3116.c driver, disassembling the windows driver, and some lucky
747  * guesses. Since I do not have any datasheet or other
748  * documentation, inaccuracies are almost guaranteed.
749  *
750  * Some specs for the ARK3116 can be found here:
751  * http://web.archive.org/web/20060318000438/
752  *   www.arkmicro.com/en/products/view.php?id=10
753  * On that page, 2 GPIO pins are mentioned: I assume these are the
754  * OUT1 and OUT2 pins of the UART, so I added support for those
755  * through the MCR. Since the pins are not available on my hardware,
756  * I could not verify this.
757  * Also, it states there is "on-chip hardware flow control". I have
758  * discovered how to enable that. Unfortunately, I do not know how to
759  * enable XON/XOFF (software) flow control, which would need support
760  * from the chip as well to work. Because of the wording on the web
761  * page there is a real possibility the chip simply does not support
762  * software flow control.
763  *
764  * I got my ark3116 as part of a mobile phone adapter cable. On the
765  * PCB, the following numbered contacts are present:
766  *
767  *  1:- +5V
768  *  2:o DTR
769  *  3:i RX
770  *  4:i DCD
771  *  5:o RTS
772  *  6:o TX
773  *  7:i RI
774  *  8:i DSR
775  * 10:- 0V
776  * 11:i CTS
777  *
778  * On my chip, all signals seem to be 3.3V, but 5V tolerant. But that
779  * may be different for the one you have ;-).
780  *
781  * The windows driver limits the registers to 0-F, so I assume there
782  * are actually 16 present on the device.
783  *
784  * On an UART interrupt, 4 bytes of data come in on the interrupt
785  * endpoint. The bytes are 0xe8 IIR LSR MSR.
786  *
787  * The baudrate seems to be generated from the 12MHz crystal, using
788  * 4-times subsampling. So quot=12e6/(4*baud). Also see description
789  * of register E.
790  *
791  * Registers 0-7:
792  * These seem to be the same as for a regular 16450. The FCR is set
793  * to UART_FCR_DMA_SELECT (0x8), I guess to enable transfers between
794  * the UART and the USB bridge/DMA engine.
795  *
796  * Register 8:
797  * By trial and error, I found out that bit 0 enables hardware CTS,
798  * stopping TX when CTS is +5V. Bit 1 does the same for RTS, making
799  * RTS +5V when the 3116 cannot transfer the data to the USB bus
800  * (verified by disabling the reading URB). Note that as far as I can
801  * tell, the windows driver does NOT use this, so there might be some
802  * hardware bug or something.
803  *
804  * According to a patch provided here
805  * (http://lkml.org/lkml/2009/7/26/56), the ARK3116 can also be used
806  * as an IrDA dongle. Since I do not have such a thing, I could not
807  * investigate that aspect. However, I can speculate ;-).
808  *
809  * - IrDA encodes data differently than RS232. Most likely, one of
810  *   the bits in registers 9..E enables the IR ENDEC (encoder/decoder).
811  * - Depending on the IR transceiver, the input and output need to be
812  *   inverted, so there are probably bits for that as well.
813  * - IrDA is half-duplex, so there should be a bit for selecting that.
814  *
815  * This still leaves at least two registers unaccounted for. Perhaps
816  * The chip can do XON/XOFF or CRC in HW?
817  *
818  * Register 9:
819  * Set to 0x00 for IrDA, when the baudrate is initialised.
820  *
821  * Register A:
822  * Set to 0x01 for IrDA, at init.
823  *
824  * Register B:
825  * Set to 0x01 for IrDA, 0x00 for RS232, at init.
826  *
827  * Register C:
828  * Set to 00 for IrDA, at init.
829  *
830  * Register D:
831  * Set to 0x41 for IrDA, at init.
832  *
833  * Register E:
834  * Somekind of baudrate override. The windows driver seems to set
835  * this to 0x00 for normal baudrates, 0x01 for 460800, 0x02 for 921600.
836  * Since 460800 and 921600 cannot be obtained by dividing 3MHz by an integer,
837  * it could be somekind of subdivisor thingy.
838  * However,it does not seem to do anything: selecting 921600 (divisor 3,
839  * reg E=2), still gets 1 MHz. I also checked if registers 9, C or F would
840  * work, but they don't.
841  *
842  * Register F: unknown
843  */
844