1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2009 by Bart Hartgers (bart.hartgers+ark3116@gmail.com)
6 * Simon Schulz (ark3116_driver <at> auctionant.de)
9 * - implements a driver for the arkmicro ark3116 chipset (vendor=0x6547,
10 * productid=0x0232) (used in a datacable called KQ-U8A)
12 * Supports full modem status lines, break, hardware flow control. Does not
13 * support software flow control, since I do not know how to enable it in hw.
15 * This driver is a essentially new implementation. I initially dug
16 * into the old ark3116.c driver and suddenly realized the ark3116 is
17 * a 16450 with a USB interface glued to it. See comments at the
18 * bottom of this file.
21 #include <linux/kernel.h>
22 #include <linux/ioctl.h>
23 #include <linux/tty.h>
24 #include <linux/slab.h>
25 #include <linux/tty_flip.h>
26 #include <linux/module.h>
27 #include <linux/usb.h>
28 #include <linux/usb/serial.h>
29 #include <linux/serial.h>
30 #include <linux/serial_reg.h>
31 #include <linux/uaccess.h>
32 #include <linux/mutex.h>
33 #include <linux/spinlock.h>
35 #define DRIVER_AUTHOR "Bart Hartgers <bart.hartgers+ark3116@gmail.com>"
36 #define DRIVER_DESC "USB ARK3116 serial/IrDA driver"
37 #define DRIVER_DEV_DESC "ARK3116 RS232/IrDA"
38 #define DRIVER_NAME "ark3116"
40 /* usb timeout of 1 second */
41 #define ARK_TIMEOUT 1000
43 static const struct usb_device_id id_table[] = {
44 { USB_DEVICE(0x6547, 0x0232) },
45 { USB_DEVICE(0x18ec, 0x3118) }, /* USB to IrDA adapter */
48 MODULE_DEVICE_TABLE(usb, id_table);
50 static int is_irda(struct usb_serial *serial)
52 struct usb_device *dev = serial->dev;
53 if (le16_to_cpu(dev->descriptor.idVendor) == 0x18ec &&
54 le16_to_cpu(dev->descriptor.idProduct) == 0x3118)
59 struct ark3116_private {
60 int irda; /* 1 for irda device */
62 /* protects hw register updates */
65 int quot; /* baudrate divisor */
66 __u32 lcr; /* line control register value */
67 __u32 hcr; /* handshake control register (0x8)
69 __u32 mcr; /* modem control register value */
71 /* protects the status values below */
72 spinlock_t status_lock;
73 __u32 msr; /* modem status register value */
74 __u32 lsr; /* line status register value */
77 static int ark3116_write_reg(struct usb_serial *serial,
78 unsigned reg, __u8 val)
81 /* 0xfe 0x40 are magic values taken from original driver */
82 result = usb_control_msg(serial->dev,
83 usb_sndctrlpipe(serial->dev, 0),
85 NULL, 0, ARK_TIMEOUT);
92 static int ark3116_read_reg(struct usb_serial *serial,
93 unsigned reg, unsigned char *buf)
96 /* 0xfe 0xc0 are magic values taken from original driver */
97 result = usb_control_msg(serial->dev,
98 usb_rcvctrlpipe(serial->dev, 0),
100 buf, 1, ARK_TIMEOUT);
102 dev_err(&serial->interface->dev,
103 "failed to read register %u: %d\n",
114 static inline int calc_divisor(int bps)
116 /* Original ark3116 made some exceptions in rounding here
117 * because windows did the same. Assume that is not really
119 * Crystal is 12MHz, probably because of USB, but we divide by 4?
121 return (12000000 + 2*bps) / (4*bps);
124 static int ark3116_port_probe(struct usb_serial_port *port)
126 struct usb_serial *serial = port->serial;
127 struct ark3116_private *priv;
129 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
133 mutex_init(&priv->hw_lock);
134 spin_lock_init(&priv->status_lock);
136 priv->irda = is_irda(serial);
138 usb_set_serial_port_data(port, priv);
140 /* setup the hardware */
141 ark3116_write_reg(serial, UART_IER, 0);
143 ark3116_write_reg(serial, UART_FCR, 0);
144 /* handshake control */
146 ark3116_write_reg(serial, 0x8 , 0);
149 ark3116_write_reg(serial, UART_MCR, 0);
152 ark3116_write_reg(serial, 0xb , 0);
154 ark3116_write_reg(serial, 0xb , 1);
155 ark3116_write_reg(serial, 0xc , 0);
156 ark3116_write_reg(serial, 0xd , 0x41);
157 ark3116_write_reg(serial, 0xa , 1);
161 ark3116_write_reg(serial, UART_LCR, UART_LCR_DLAB);
163 /* setup for 9600 8N1 */
164 priv->quot = calc_divisor(9600);
165 ark3116_write_reg(serial, UART_DLL, priv->quot & 0xff);
166 ark3116_write_reg(serial, UART_DLM, (priv->quot>>8) & 0xff);
168 priv->lcr = UART_LCR_WLEN8;
169 ark3116_write_reg(serial, UART_LCR, UART_LCR_WLEN8);
171 ark3116_write_reg(serial, 0xe, 0);
174 ark3116_write_reg(serial, 0x9, 0);
176 dev_info(&port->dev, "using %s mode\n", priv->irda ? "IrDA" : "RS232");
181 static int ark3116_port_remove(struct usb_serial_port *port)
183 struct ark3116_private *priv = usb_get_serial_port_data(port);
185 /* device is closed, so URBs and DMA should be down */
186 mutex_destroy(&priv->hw_lock);
192 static void ark3116_init_termios(struct tty_struct *tty)
194 struct ktermios *termios = &tty->termios;
195 *termios = tty_std_termios;
196 termios->c_cflag = B9600 | CS8
197 | CREAD | HUPCL | CLOCAL;
198 termios->c_ispeed = 9600;
199 termios->c_ospeed = 9600;
202 static void ark3116_set_termios(struct tty_struct *tty,
203 struct usb_serial_port *port,
204 struct ktermios *old_termios)
206 struct usb_serial *serial = port->serial;
207 struct ark3116_private *priv = usb_get_serial_port_data(port);
208 struct ktermios *termios = &tty->termios;
209 unsigned int cflag = termios->c_cflag;
210 int bps = tty_get_baud_rate(tty);
214 /* set data bit count */
215 switch (cflag & CSIZE) {
217 lcr = UART_LCR_WLEN5;
220 lcr = UART_LCR_WLEN6;
223 lcr = UART_LCR_WLEN7;
227 lcr = UART_LCR_WLEN8;
231 lcr |= UART_LCR_STOP;
233 lcr |= UART_LCR_PARITY;
234 if (!(cflag & PARODD))
235 lcr |= UART_LCR_EPAR;
238 lcr |= UART_LCR_SPAR;
240 /* handshake control */
241 hcr = (cflag & CRTSCTS) ? 0x03 : 0x00;
244 dev_dbg(&port->dev, "%s - setting bps to %d\n", __func__, bps);
248 quot = calc_divisor(9600);
251 if ((bps < 75) || (bps > 3000000))
253 quot = calc_divisor(bps);
257 quot = calc_divisor(bps);
261 quot = calc_divisor(bps);
265 /* Update state: synchronize */
266 mutex_lock(&priv->hw_lock);
268 /* keep old LCR_SBC bit */
269 lcr |= (priv->lcr & UART_LCR_SBC);
271 dev_dbg(&port->dev, "%s - setting hcr:0x%02x,lcr:0x%02x,quot:%d\n",
272 __func__, hcr, lcr, quot);
274 /* handshake control */
275 if (priv->hcr != hcr) {
277 ark3116_write_reg(serial, 0x8, hcr);
281 if (priv->quot != quot) {
283 priv->lcr = lcr; /* need to write lcr anyway */
285 /* disable DMA since transmit/receive is
286 * shadowed by UART_DLL
288 ark3116_write_reg(serial, UART_FCR, 0);
290 ark3116_write_reg(serial, UART_LCR,
292 ark3116_write_reg(serial, UART_DLL, quot & 0xff);
293 ark3116_write_reg(serial, UART_DLM, (quot>>8) & 0xff);
296 ark3116_write_reg(serial, UART_LCR, lcr);
297 /* magic baudrate thingy: not sure what it does,
298 * but windows does this as well.
300 ark3116_write_reg(serial, 0xe, eval);
303 ark3116_write_reg(serial, UART_FCR, UART_FCR_DMA_SELECT);
304 } else if (priv->lcr != lcr) {
306 ark3116_write_reg(serial, UART_LCR, lcr);
309 mutex_unlock(&priv->hw_lock);
311 /* check for software flow control */
312 if (I_IXOFF(tty) || I_IXON(tty)) {
314 "software flow control not implemented\n");
317 /* Don't rewrite B0 */
318 if (tty_termios_baud_rate(termios))
319 tty_termios_encode_baud_rate(termios, bps, bps);
322 static void ark3116_close(struct usb_serial_port *port)
324 struct usb_serial *serial = port->serial;
327 ark3116_write_reg(serial, UART_FCR, 0);
329 /* deactivate interrupts */
330 ark3116_write_reg(serial, UART_IER, 0);
332 usb_serial_generic_close(port);
334 usb_kill_urb(port->interrupt_in_urb);
337 static int ark3116_open(struct tty_struct *tty, struct usb_serial_port *port)
339 struct ark3116_private *priv = usb_get_serial_port_data(port);
340 struct usb_serial *serial = port->serial;
344 buf = kmalloc(1, GFP_KERNEL);
348 result = usb_serial_generic_open(tty, port);
351 "%s - usb_serial_generic_open failed: %d\n",
356 /* remove any data still left: also clears error state */
357 ark3116_read_reg(serial, UART_RX, buf);
359 /* read modem status */
360 result = ark3116_read_reg(serial, UART_MSR, buf);
365 /* read line status */
366 result = ark3116_read_reg(serial, UART_LSR, buf);
371 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
373 dev_err(&port->dev, "submit irq_in urb failed %d\n",
378 /* activate interrupts */
379 ark3116_write_reg(port->serial, UART_IER, UART_IER_MSI|UART_IER_RLSI);
382 ark3116_write_reg(port->serial, UART_FCR, UART_FCR_DMA_SELECT);
386 ark3116_set_termios(tty, port, NULL);
393 usb_serial_generic_close(port);
400 static int ark3116_get_serial_info(struct usb_serial_port *port,
401 struct serial_struct __user *retinfo)
403 struct serial_struct tmp;
405 memset(&tmp, 0, sizeof(tmp));
407 tmp.type = PORT_16654;
408 tmp.line = port->minor;
409 tmp.port = port->port_number;
410 tmp.baud_base = 460800;
412 if (copy_to_user(retinfo, &tmp, sizeof(tmp)))
418 static int ark3116_ioctl(struct tty_struct *tty,
419 unsigned int cmd, unsigned long arg)
421 struct usb_serial_port *port = tty->driver_data;
422 void __user *user_arg = (void __user *)arg;
426 return ark3116_get_serial_info(port, user_arg);
434 static int ark3116_tiocmget(struct tty_struct *tty)
436 struct usb_serial_port *port = tty->driver_data;
437 struct ark3116_private *priv = usb_get_serial_port_data(port);
442 mutex_lock(&priv->hw_lock);
444 mutex_unlock(&priv->hw_lock);
446 spin_lock_irqsave(&priv->status_lock, flags);
448 spin_unlock_irqrestore(&priv->status_lock, flags);
450 return (status & UART_MSR_DSR ? TIOCM_DSR : 0) |
451 (status & UART_MSR_CTS ? TIOCM_CTS : 0) |
452 (status & UART_MSR_RI ? TIOCM_RI : 0) |
453 (status & UART_MSR_DCD ? TIOCM_CD : 0) |
454 (ctrl & UART_MCR_DTR ? TIOCM_DTR : 0) |
455 (ctrl & UART_MCR_RTS ? TIOCM_RTS : 0) |
456 (ctrl & UART_MCR_OUT1 ? TIOCM_OUT1 : 0) |
457 (ctrl & UART_MCR_OUT2 ? TIOCM_OUT2 : 0);
460 static int ark3116_tiocmset(struct tty_struct *tty,
461 unsigned set, unsigned clr)
463 struct usb_serial_port *port = tty->driver_data;
464 struct ark3116_private *priv = usb_get_serial_port_data(port);
466 /* we need to take the mutex here, to make sure that the value
467 * in priv->mcr is actually the one that is in the hardware
470 mutex_lock(&priv->hw_lock);
473 priv->mcr |= UART_MCR_RTS;
475 priv->mcr |= UART_MCR_DTR;
476 if (set & TIOCM_OUT1)
477 priv->mcr |= UART_MCR_OUT1;
478 if (set & TIOCM_OUT2)
479 priv->mcr |= UART_MCR_OUT2;
481 priv->mcr &= ~UART_MCR_RTS;
483 priv->mcr &= ~UART_MCR_DTR;
484 if (clr & TIOCM_OUT1)
485 priv->mcr &= ~UART_MCR_OUT1;
486 if (clr & TIOCM_OUT2)
487 priv->mcr &= ~UART_MCR_OUT2;
489 ark3116_write_reg(port->serial, UART_MCR, priv->mcr);
491 mutex_unlock(&priv->hw_lock);
496 static void ark3116_break_ctl(struct tty_struct *tty, int break_state)
498 struct usb_serial_port *port = tty->driver_data;
499 struct ark3116_private *priv = usb_get_serial_port_data(port);
501 /* LCR is also used for other things: protect access */
502 mutex_lock(&priv->hw_lock);
505 priv->lcr |= UART_LCR_SBC;
507 priv->lcr &= ~UART_LCR_SBC;
509 ark3116_write_reg(port->serial, UART_LCR, priv->lcr);
511 mutex_unlock(&priv->hw_lock);
514 static void ark3116_update_msr(struct usb_serial_port *port, __u8 msr)
516 struct ark3116_private *priv = usb_get_serial_port_data(port);
519 spin_lock_irqsave(&priv->status_lock, flags);
521 spin_unlock_irqrestore(&priv->status_lock, flags);
523 if (msr & UART_MSR_ANY_DELTA) {
524 /* update input line counters */
525 if (msr & UART_MSR_DCTS)
527 if (msr & UART_MSR_DDSR)
529 if (msr & UART_MSR_DDCD)
531 if (msr & UART_MSR_TERI)
533 wake_up_interruptible(&port->port.delta_msr_wait);
537 static void ark3116_update_lsr(struct usb_serial_port *port, __u8 lsr)
539 struct ark3116_private *priv = usb_get_serial_port_data(port);
542 spin_lock_irqsave(&priv->status_lock, flags);
545 spin_unlock_irqrestore(&priv->status_lock, flags);
547 if (lsr&UART_LSR_BRK_ERROR_BITS) {
548 if (lsr & UART_LSR_BI)
550 if (lsr & UART_LSR_FE)
551 port->icount.frame++;
552 if (lsr & UART_LSR_PE)
553 port->icount.parity++;
554 if (lsr & UART_LSR_OE)
555 port->icount.overrun++;
559 static void ark3116_read_int_callback(struct urb *urb)
561 struct usb_serial_port *port = urb->context;
562 int status = urb->status;
563 const __u8 *data = urb->transfer_buffer;
570 /* this urb is terminated, clean up */
571 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
575 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
578 case 0: /* success */
579 /* discovered this by trail and error... */
580 if ((urb->actual_length == 4) && (data[0] == 0xe8)) {
581 const __u8 id = data[1]&UART_IIR_ID;
582 dev_dbg(&port->dev, "%s: iir=%02x\n", __func__, data[1]);
583 if (id == UART_IIR_MSI) {
584 dev_dbg(&port->dev, "%s: msr=%02x\n",
586 ark3116_update_msr(port, data[3]);
588 } else if (id == UART_IIR_RLSI) {
589 dev_dbg(&port->dev, "%s: lsr=%02x\n",
591 ark3116_update_lsr(port, data[2]);
596 * Not sure what this data meant...
598 usb_serial_debug_data(&port->dev, __func__,
600 urb->transfer_buffer);
604 result = usb_submit_urb(urb, GFP_ATOMIC);
606 dev_err(&port->dev, "failed to resubmit interrupt urb: %d\n",
611 /* Data comes in via the bulk (data) URB, errors/interrupts via the int URB.
612 * This means that we cannot be sure which data byte has an associated error
613 * condition, so we report an error for all data in the next bulk read.
615 * Actually, there might even be a window between the bulk data leaving the
616 * ark and reading/resetting the lsr in the read_bulk_callback where an
617 * interrupt for the next data block could come in.
618 * Without somekind of ordering on the ark, we would have to report the
619 * error for the next block of data as well...
620 * For now, let's pretend this can't happen.
622 static void ark3116_process_read_urb(struct urb *urb)
624 struct usb_serial_port *port = urb->context;
625 struct ark3116_private *priv = usb_get_serial_port_data(port);
626 unsigned char *data = urb->transfer_buffer;
627 char tty_flag = TTY_NORMAL;
631 /* update line status */
632 spin_lock_irqsave(&priv->status_lock, flags);
634 priv->lsr &= ~UART_LSR_BRK_ERROR_BITS;
635 spin_unlock_irqrestore(&priv->status_lock, flags);
637 if (!urb->actual_length)
640 if (lsr & UART_LSR_BRK_ERROR_BITS) {
641 if (lsr & UART_LSR_BI)
642 tty_flag = TTY_BREAK;
643 else if (lsr & UART_LSR_PE)
644 tty_flag = TTY_PARITY;
645 else if (lsr & UART_LSR_FE)
646 tty_flag = TTY_FRAME;
648 /* overrun is special, not associated with a char */
649 if (lsr & UART_LSR_OE)
650 tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
652 tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
654 tty_flip_buffer_push(&port->port);
657 static struct usb_serial_driver ark3116_device = {
659 .owner = THIS_MODULE,
662 .id_table = id_table,
666 .num_interrupt_in = 1,
667 .port_probe = ark3116_port_probe,
668 .port_remove = ark3116_port_remove,
669 .set_termios = ark3116_set_termios,
670 .init_termios = ark3116_init_termios,
671 .ioctl = ark3116_ioctl,
672 .tiocmget = ark3116_tiocmget,
673 .tiocmset = ark3116_tiocmset,
674 .tiocmiwait = usb_serial_generic_tiocmiwait,
675 .get_icount = usb_serial_generic_get_icount,
676 .open = ark3116_open,
677 .close = ark3116_close,
678 .break_ctl = ark3116_break_ctl,
679 .read_int_callback = ark3116_read_int_callback,
680 .process_read_urb = ark3116_process_read_urb,
683 static struct usb_serial_driver * const serial_drivers[] = {
684 &ark3116_device, NULL
687 module_usb_serial_driver(serial_drivers, id_table);
689 MODULE_LICENSE("GPL");
691 MODULE_AUTHOR(DRIVER_AUTHOR);
692 MODULE_DESCRIPTION(DRIVER_DESC);
695 * The following describes what I learned from studying the old
696 * ark3116.c driver, disassembling the windows driver, and some lucky
697 * guesses. Since I do not have any datasheet or other
698 * documentation, inaccuracies are almost guaranteed.
700 * Some specs for the ARK3116 can be found here:
701 * http://web.archive.org/web/20060318000438/
702 * www.arkmicro.com/en/products/view.php?id=10
703 * On that page, 2 GPIO pins are mentioned: I assume these are the
704 * OUT1 and OUT2 pins of the UART, so I added support for those
705 * through the MCR. Since the pins are not available on my hardware,
706 * I could not verify this.
707 * Also, it states there is "on-chip hardware flow control". I have
708 * discovered how to enable that. Unfortunately, I do not know how to
709 * enable XON/XOFF (software) flow control, which would need support
710 * from the chip as well to work. Because of the wording on the web
711 * page there is a real possibility the chip simply does not support
712 * software flow control.
714 * I got my ark3116 as part of a mobile phone adapter cable. On the
715 * PCB, the following numbered contacts are present:
728 * On my chip, all signals seem to be 3.3V, but 5V tolerant. But that
729 * may be different for the one you have ;-).
731 * The windows driver limits the registers to 0-F, so I assume there
732 * are actually 16 present on the device.
734 * On an UART interrupt, 4 bytes of data come in on the interrupt
735 * endpoint. The bytes are 0xe8 IIR LSR MSR.
737 * The baudrate seems to be generated from the 12MHz crystal, using
738 * 4-times subsampling. So quot=12e6/(4*baud). Also see description
742 * These seem to be the same as for a regular 16450. The FCR is set
743 * to UART_FCR_DMA_SELECT (0x8), I guess to enable transfers between
744 * the UART and the USB bridge/DMA engine.
747 * By trial and error, I found out that bit 0 enables hardware CTS,
748 * stopping TX when CTS is +5V. Bit 1 does the same for RTS, making
749 * RTS +5V when the 3116 cannot transfer the data to the USB bus
750 * (verified by disabling the reading URB). Note that as far as I can
751 * tell, the windows driver does NOT use this, so there might be some
752 * hardware bug or something.
754 * According to a patch provided here
755 * (http://lkml.org/lkml/2009/7/26/56), the ARK3116 can also be used
756 * as an IrDA dongle. Since I do not have such a thing, I could not
757 * investigate that aspect. However, I can speculate ;-).
759 * - IrDA encodes data differently than RS232. Most likely, one of
760 * the bits in registers 9..E enables the IR ENDEC (encoder/decoder).
761 * - Depending on the IR transceiver, the input and output need to be
762 * inverted, so there are probably bits for that as well.
763 * - IrDA is half-duplex, so there should be a bit for selecting that.
765 * This still leaves at least two registers unaccounted for. Perhaps
766 * The chip can do XON/XOFF or CRC in HW?
769 * Set to 0x00 for IrDA, when the baudrate is initialised.
772 * Set to 0x01 for IrDA, at init.
775 * Set to 0x01 for IrDA, 0x00 for RS232, at init.
778 * Set to 00 for IrDA, at init.
781 * Set to 0x41 for IrDA, at init.
784 * Somekind of baudrate override. The windows driver seems to set
785 * this to 0x00 for normal baudrates, 0x01 for 460800, 0x02 for 921600.
786 * Since 460800 and 921600 cannot be obtained by dividing 3MHz by an integer,
787 * it could be somekind of subdivisor thingy.
788 * However,it does not seem to do anything: selecting 921600 (divisor 3,
789 * reg E=2), still gets 1 MHz. I also checked if registers 9, C or F would
790 * work, but they don't.
792 * Register F: unknown