Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Copyright (C) 2009 by Bart Hartgers (bart.hartgers+ark3116@gmail.com)
0004  * Original version:
0005  * Copyright (C) 2006
0006  *   Simon Schulz (ark3116_driver <at> auctionant.de)
0007  *
0008  * ark3116
0009  * - implements a driver for the arkmicro ark3116 chipset (vendor=0x6547,
0010  *   productid=0x0232) (used in a datacable called KQ-U8A)
0011  *
0012  * Supports full modem status lines, break, hardware flow control. Does not
0013  * support software flow control, since I do not know how to enable it in hw.
0014  *
0015  * This driver is a essentially new implementation. I initially dug
0016  * into the old ark3116.c driver and suddenly realized the ark3116 is
0017  * a 16450 with a USB interface glued to it. See comments at the
0018  * bottom of this file.
0019  */
0020 
0021 #include <linux/kernel.h>
0022 #include <linux/ioctl.h>
0023 #include <linux/tty.h>
0024 #include <linux/slab.h>
0025 #include <linux/tty_flip.h>
0026 #include <linux/module.h>
0027 #include <linux/usb.h>
0028 #include <linux/usb/serial.h>
0029 #include <linux/serial.h>
0030 #include <linux/serial_reg.h>
0031 #include <linux/uaccess.h>
0032 #include <linux/mutex.h>
0033 #include <linux/spinlock.h>
0034 
0035 #define DRIVER_AUTHOR "Bart Hartgers <bart.hartgers+ark3116@gmail.com>"
0036 #define DRIVER_DESC "USB ARK3116 serial/IrDA driver"
0037 #define DRIVER_DEV_DESC "ARK3116 RS232/IrDA"
0038 #define DRIVER_NAME "ark3116"
0039 
0040 /* usb timeout of 1 second */
0041 #define ARK_TIMEOUT 1000
0042 
0043 static const struct usb_device_id id_table[] = {
0044     { USB_DEVICE(0x6547, 0x0232) },
0045     { USB_DEVICE(0x18ec, 0x3118) },     /* USB to IrDA adapter */
0046     { },
0047 };
0048 MODULE_DEVICE_TABLE(usb, id_table);
0049 
0050 static int is_irda(struct usb_serial *serial)
0051 {
0052     struct usb_device *dev = serial->dev;
0053     if (le16_to_cpu(dev->descriptor.idVendor) == 0x18ec &&
0054             le16_to_cpu(dev->descriptor.idProduct) == 0x3118)
0055         return 1;
0056     return 0;
0057 }
0058 
0059 struct ark3116_private {
0060     int         irda;   /* 1 for irda device */
0061 
0062     /* protects hw register updates */
0063     struct mutex        hw_lock;
0064 
0065     int         quot;   /* baudrate divisor */
0066     __u32           lcr;    /* line control register value */
0067     __u32           hcr;    /* handshake control register (0x8)
0068                      * value */
0069     __u32           mcr;    /* modem control register value */
0070 
0071     /* protects the status values below */
0072     spinlock_t      status_lock;
0073     __u32           msr;    /* modem status register value */
0074     __u32           lsr;    /* line status register value */
0075 };
0076 
0077 static int ark3116_write_reg(struct usb_serial *serial,
0078                  unsigned reg, __u8 val)
0079 {
0080     int result;
0081      /* 0xfe 0x40 are magic values taken from original driver */
0082     result = usb_control_msg(serial->dev,
0083                  usb_sndctrlpipe(serial->dev, 0),
0084                  0xfe, 0x40, val, reg,
0085                  NULL, 0, ARK_TIMEOUT);
0086     if (result)
0087         return result;
0088 
0089     return 0;
0090 }
0091 
0092 static int ark3116_read_reg(struct usb_serial *serial,
0093                 unsigned reg, unsigned char *buf)
0094 {
0095     int result;
0096     /* 0xfe 0xc0 are magic values taken from original driver */
0097     result = usb_control_msg(serial->dev,
0098                  usb_rcvctrlpipe(serial->dev, 0),
0099                  0xfe, 0xc0, 0, reg,
0100                  buf, 1, ARK_TIMEOUT);
0101     if (result < 1) {
0102         dev_err(&serial->interface->dev,
0103                 "failed to read register %u: %d\n",
0104                 reg, result);
0105         if (result >= 0)
0106             result = -EIO;
0107 
0108         return result;
0109     }
0110 
0111     return 0;
0112 }
0113 
0114 static inline int calc_divisor(int bps)
0115 {
0116     /* Original ark3116 made some exceptions in rounding here
0117      * because windows did the same. Assume that is not really
0118      * necessary.
0119      * Crystal is 12MHz, probably because of USB, but we divide by 4?
0120      */
0121     return (12000000 + 2*bps) / (4*bps);
0122 }
0123 
0124 static int ark3116_port_probe(struct usb_serial_port *port)
0125 {
0126     struct usb_serial *serial = port->serial;
0127     struct ark3116_private *priv;
0128 
0129     priv = kzalloc(sizeof(*priv), GFP_KERNEL);
0130     if (!priv)
0131         return -ENOMEM;
0132 
0133     mutex_init(&priv->hw_lock);
0134     spin_lock_init(&priv->status_lock);
0135 
0136     priv->irda = is_irda(serial);
0137 
0138     usb_set_serial_port_data(port, priv);
0139 
0140     /* setup the hardware */
0141     ark3116_write_reg(serial, UART_IER, 0);
0142     /* disable DMA */
0143     ark3116_write_reg(serial, UART_FCR, 0);
0144     /* handshake control */
0145     priv->hcr = 0;
0146     ark3116_write_reg(serial, 0x8     , 0);
0147     /* modem control */
0148     priv->mcr = 0;
0149     ark3116_write_reg(serial, UART_MCR, 0);
0150 
0151     if (!(priv->irda)) {
0152         ark3116_write_reg(serial, 0xb , 0);
0153     } else {
0154         ark3116_write_reg(serial, 0xb , 1);
0155         ark3116_write_reg(serial, 0xc , 0);
0156         ark3116_write_reg(serial, 0xd , 0x41);
0157         ark3116_write_reg(serial, 0xa , 1);
0158     }
0159 
0160     /* setup baudrate */
0161     ark3116_write_reg(serial, UART_LCR, UART_LCR_DLAB);
0162 
0163     /* setup for 9600 8N1 */
0164     priv->quot = calc_divisor(9600);
0165     ark3116_write_reg(serial, UART_DLL, priv->quot & 0xff);
0166     ark3116_write_reg(serial, UART_DLM, (priv->quot>>8) & 0xff);
0167 
0168     priv->lcr = UART_LCR_WLEN8;
0169     ark3116_write_reg(serial, UART_LCR, UART_LCR_WLEN8);
0170 
0171     ark3116_write_reg(serial, 0xe, 0);
0172 
0173     if (priv->irda)
0174         ark3116_write_reg(serial, 0x9, 0);
0175 
0176     dev_info(&port->dev, "using %s mode\n", priv->irda ? "IrDA" : "RS232");
0177 
0178     return 0;
0179 }
0180 
0181 static void ark3116_port_remove(struct usb_serial_port *port)
0182 {
0183     struct ark3116_private *priv = usb_get_serial_port_data(port);
0184 
0185     /* device is closed, so URBs and DMA should be down */
0186     mutex_destroy(&priv->hw_lock);
0187     kfree(priv);
0188 }
0189 
0190 static void ark3116_set_termios(struct tty_struct *tty,
0191                 struct usb_serial_port *port,
0192                 struct ktermios *old_termios)
0193 {
0194     struct usb_serial *serial = port->serial;
0195     struct ark3116_private *priv = usb_get_serial_port_data(port);
0196     struct ktermios *termios = &tty->termios;
0197     unsigned int cflag = termios->c_cflag;
0198     int bps = tty_get_baud_rate(tty);
0199     int quot;
0200     __u8 lcr, hcr, eval;
0201 
0202     /* set data bit count */
0203     lcr = UART_LCR_WLEN(tty_get_char_size(cflag));
0204 
0205     if (cflag & CSTOPB)
0206         lcr |= UART_LCR_STOP;
0207     if (cflag & PARENB)
0208         lcr |= UART_LCR_PARITY;
0209     if (!(cflag & PARODD))
0210         lcr |= UART_LCR_EPAR;
0211     if (cflag & CMSPAR)
0212         lcr |= UART_LCR_SPAR;
0213 
0214     /* handshake control */
0215     hcr = (cflag & CRTSCTS) ? 0x03 : 0x00;
0216 
0217     /* calc baudrate */
0218     dev_dbg(&port->dev, "%s - setting bps to %d\n", __func__, bps);
0219     eval = 0;
0220     switch (bps) {
0221     case 0:
0222         quot = calc_divisor(9600);
0223         break;
0224     default:
0225         if ((bps < 75) || (bps > 3000000))
0226             bps = 9600;
0227         quot = calc_divisor(bps);
0228         break;
0229     case 460800:
0230         eval = 1;
0231         quot = calc_divisor(bps);
0232         break;
0233     case 921600:
0234         eval = 2;
0235         quot = calc_divisor(bps);
0236         break;
0237     }
0238 
0239     /* Update state: synchronize */
0240     mutex_lock(&priv->hw_lock);
0241 
0242     /* keep old LCR_SBC bit */
0243     lcr |= (priv->lcr & UART_LCR_SBC);
0244 
0245     dev_dbg(&port->dev, "%s - setting hcr:0x%02x,lcr:0x%02x,quot:%d\n",
0246         __func__, hcr, lcr, quot);
0247 
0248     /* handshake control */
0249     if (priv->hcr != hcr) {
0250         priv->hcr = hcr;
0251         ark3116_write_reg(serial, 0x8, hcr);
0252     }
0253 
0254     /* baudrate */
0255     if (priv->quot != quot) {
0256         priv->quot = quot;
0257         priv->lcr = lcr; /* need to write lcr anyway */
0258 
0259         /* disable DMA since transmit/receive is
0260          * shadowed by UART_DLL
0261          */
0262         ark3116_write_reg(serial, UART_FCR, 0);
0263 
0264         ark3116_write_reg(serial, UART_LCR,
0265                   lcr|UART_LCR_DLAB);
0266         ark3116_write_reg(serial, UART_DLL, quot & 0xff);
0267         ark3116_write_reg(serial, UART_DLM, (quot>>8) & 0xff);
0268 
0269         /* restore lcr */
0270         ark3116_write_reg(serial, UART_LCR, lcr);
0271         /* magic baudrate thingy: not sure what it does,
0272          * but windows does this as well.
0273          */
0274         ark3116_write_reg(serial, 0xe, eval);
0275 
0276         /* enable DMA */
0277         ark3116_write_reg(serial, UART_FCR, UART_FCR_DMA_SELECT);
0278     } else if (priv->lcr != lcr) {
0279         priv->lcr = lcr;
0280         ark3116_write_reg(serial, UART_LCR, lcr);
0281     }
0282 
0283     mutex_unlock(&priv->hw_lock);
0284 
0285     /* check for software flow control */
0286     if (I_IXOFF(tty) || I_IXON(tty)) {
0287         dev_warn(&port->dev,
0288                 "software flow control not implemented\n");
0289     }
0290 
0291     /* Don't rewrite B0 */
0292     if (tty_termios_baud_rate(termios))
0293         tty_termios_encode_baud_rate(termios, bps, bps);
0294 }
0295 
0296 static void ark3116_close(struct usb_serial_port *port)
0297 {
0298     struct usb_serial *serial = port->serial;
0299 
0300     /* disable DMA */
0301     ark3116_write_reg(serial, UART_FCR, 0);
0302 
0303     /* deactivate interrupts */
0304     ark3116_write_reg(serial, UART_IER, 0);
0305 
0306     usb_serial_generic_close(port);
0307 
0308     usb_kill_urb(port->interrupt_in_urb);
0309 }
0310 
0311 static int ark3116_open(struct tty_struct *tty, struct usb_serial_port *port)
0312 {
0313     struct ark3116_private *priv = usb_get_serial_port_data(port);
0314     struct usb_serial *serial = port->serial;
0315     unsigned char *buf;
0316     int result;
0317 
0318     buf = kmalloc(1, GFP_KERNEL);
0319     if (buf == NULL)
0320         return -ENOMEM;
0321 
0322     result = usb_serial_generic_open(tty, port);
0323     if (result) {
0324         dev_dbg(&port->dev,
0325             "%s - usb_serial_generic_open failed: %d\n",
0326             __func__, result);
0327         goto err_free;
0328     }
0329 
0330     /* remove any data still left: also clears error state */
0331     ark3116_read_reg(serial, UART_RX, buf);
0332 
0333     /* read modem status */
0334     result = ark3116_read_reg(serial, UART_MSR, buf);
0335     if (result)
0336         goto err_close;
0337     priv->msr = *buf;
0338 
0339     /* read line status */
0340     result = ark3116_read_reg(serial, UART_LSR, buf);
0341     if (result)
0342         goto err_close;
0343     priv->lsr = *buf;
0344 
0345     result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
0346     if (result) {
0347         dev_err(&port->dev, "submit irq_in urb failed %d\n",
0348             result);
0349         goto err_close;
0350     }
0351 
0352     /* activate interrupts */
0353     ark3116_write_reg(port->serial, UART_IER, UART_IER_MSI|UART_IER_RLSI);
0354 
0355     /* enable DMA */
0356     ark3116_write_reg(port->serial, UART_FCR, UART_FCR_DMA_SELECT);
0357 
0358     /* setup termios */
0359     if (tty)
0360         ark3116_set_termios(tty, port, NULL);
0361 
0362     kfree(buf);
0363 
0364     return 0;
0365 
0366 err_close:
0367     usb_serial_generic_close(port);
0368 err_free:
0369     kfree(buf);
0370 
0371     return result;
0372 }
0373 
0374 static int ark3116_tiocmget(struct tty_struct *tty)
0375 {
0376     struct usb_serial_port *port = tty->driver_data;
0377     struct ark3116_private *priv = usb_get_serial_port_data(port);
0378     __u32 status;
0379     __u32 ctrl;
0380     unsigned long flags;
0381 
0382     mutex_lock(&priv->hw_lock);
0383     ctrl = priv->mcr;
0384     mutex_unlock(&priv->hw_lock);
0385 
0386     spin_lock_irqsave(&priv->status_lock, flags);
0387     status = priv->msr;
0388     spin_unlock_irqrestore(&priv->status_lock, flags);
0389 
0390     return  (status & UART_MSR_DSR  ? TIOCM_DSR  : 0) |
0391         (status & UART_MSR_CTS  ? TIOCM_CTS  : 0) |
0392         (status & UART_MSR_RI   ? TIOCM_RI   : 0) |
0393         (status & UART_MSR_DCD  ? TIOCM_CD   : 0) |
0394         (ctrl   & UART_MCR_DTR  ? TIOCM_DTR  : 0) |
0395         (ctrl   & UART_MCR_RTS  ? TIOCM_RTS  : 0) |
0396         (ctrl   & UART_MCR_OUT1 ? TIOCM_OUT1 : 0) |
0397         (ctrl   & UART_MCR_OUT2 ? TIOCM_OUT2 : 0);
0398 }
0399 
0400 static int ark3116_tiocmset(struct tty_struct *tty,
0401             unsigned set, unsigned clr)
0402 {
0403     struct usb_serial_port *port = tty->driver_data;
0404     struct ark3116_private *priv = usb_get_serial_port_data(port);
0405 
0406     /* we need to take the mutex here, to make sure that the value
0407      * in priv->mcr is actually the one that is in the hardware
0408      */
0409 
0410     mutex_lock(&priv->hw_lock);
0411 
0412     if (set & TIOCM_RTS)
0413         priv->mcr |= UART_MCR_RTS;
0414     if (set & TIOCM_DTR)
0415         priv->mcr |= UART_MCR_DTR;
0416     if (set & TIOCM_OUT1)
0417         priv->mcr |= UART_MCR_OUT1;
0418     if (set & TIOCM_OUT2)
0419         priv->mcr |= UART_MCR_OUT2;
0420     if (clr & TIOCM_RTS)
0421         priv->mcr &= ~UART_MCR_RTS;
0422     if (clr & TIOCM_DTR)
0423         priv->mcr &= ~UART_MCR_DTR;
0424     if (clr & TIOCM_OUT1)
0425         priv->mcr &= ~UART_MCR_OUT1;
0426     if (clr & TIOCM_OUT2)
0427         priv->mcr &= ~UART_MCR_OUT2;
0428 
0429     ark3116_write_reg(port->serial, UART_MCR, priv->mcr);
0430 
0431     mutex_unlock(&priv->hw_lock);
0432 
0433     return 0;
0434 }
0435 
0436 static void ark3116_break_ctl(struct tty_struct *tty, int break_state)
0437 {
0438     struct usb_serial_port *port = tty->driver_data;
0439     struct ark3116_private *priv = usb_get_serial_port_data(port);
0440 
0441     /* LCR is also used for other things: protect access */
0442     mutex_lock(&priv->hw_lock);
0443 
0444     if (break_state)
0445         priv->lcr |= UART_LCR_SBC;
0446     else
0447         priv->lcr &= ~UART_LCR_SBC;
0448 
0449     ark3116_write_reg(port->serial, UART_LCR, priv->lcr);
0450 
0451     mutex_unlock(&priv->hw_lock);
0452 }
0453 
0454 static void ark3116_update_msr(struct usb_serial_port *port, __u8 msr)
0455 {
0456     struct ark3116_private *priv = usb_get_serial_port_data(port);
0457     unsigned long flags;
0458 
0459     spin_lock_irqsave(&priv->status_lock, flags);
0460     priv->msr = msr;
0461     spin_unlock_irqrestore(&priv->status_lock, flags);
0462 
0463     if (msr & UART_MSR_ANY_DELTA) {
0464         /* update input line counters */
0465         if (msr & UART_MSR_DCTS)
0466             port->icount.cts++;
0467         if (msr & UART_MSR_DDSR)
0468             port->icount.dsr++;
0469         if (msr & UART_MSR_DDCD)
0470             port->icount.dcd++;
0471         if (msr & UART_MSR_TERI)
0472             port->icount.rng++;
0473         wake_up_interruptible(&port->port.delta_msr_wait);
0474     }
0475 }
0476 
0477 static void ark3116_update_lsr(struct usb_serial_port *port, __u8 lsr)
0478 {
0479     struct ark3116_private *priv = usb_get_serial_port_data(port);
0480     unsigned long flags;
0481 
0482     spin_lock_irqsave(&priv->status_lock, flags);
0483     /* combine bits */
0484     priv->lsr |= lsr;
0485     spin_unlock_irqrestore(&priv->status_lock, flags);
0486 
0487     if (lsr&UART_LSR_BRK_ERROR_BITS) {
0488         if (lsr & UART_LSR_BI)
0489             port->icount.brk++;
0490         if (lsr & UART_LSR_FE)
0491             port->icount.frame++;
0492         if (lsr & UART_LSR_PE)
0493             port->icount.parity++;
0494         if (lsr & UART_LSR_OE)
0495             port->icount.overrun++;
0496     }
0497 }
0498 
0499 static void ark3116_read_int_callback(struct urb *urb)
0500 {
0501     struct usb_serial_port *port = urb->context;
0502     int status = urb->status;
0503     const __u8 *data = urb->transfer_buffer;
0504     int result;
0505 
0506     switch (status) {
0507     case -ECONNRESET:
0508     case -ENOENT:
0509     case -ESHUTDOWN:
0510         /* this urb is terminated, clean up */
0511         dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
0512             __func__, status);
0513         return;
0514     default:
0515         dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
0516             __func__, status);
0517         break;
0518     case 0: /* success */
0519         /* discovered this by trail and error... */
0520         if ((urb->actual_length == 4) && (data[0] == 0xe8)) {
0521             const __u8 id = data[1]&UART_IIR_ID;
0522             dev_dbg(&port->dev, "%s: iir=%02x\n", __func__, data[1]);
0523             if (id == UART_IIR_MSI) {
0524                 dev_dbg(&port->dev, "%s: msr=%02x\n",
0525                     __func__, data[3]);
0526                 ark3116_update_msr(port, data[3]);
0527                 break;
0528             } else if (id == UART_IIR_RLSI) {
0529                 dev_dbg(&port->dev, "%s: lsr=%02x\n",
0530                     __func__, data[2]);
0531                 ark3116_update_lsr(port, data[2]);
0532                 break;
0533             }
0534         }
0535         /*
0536          * Not sure what this data meant...
0537          */
0538         usb_serial_debug_data(&port->dev, __func__,
0539                       urb->actual_length,
0540                       urb->transfer_buffer);
0541         break;
0542     }
0543 
0544     result = usb_submit_urb(urb, GFP_ATOMIC);
0545     if (result)
0546         dev_err(&port->dev, "failed to resubmit interrupt urb: %d\n",
0547             result);
0548 }
0549 
0550 
0551 /* Data comes in via the bulk (data) URB, errors/interrupts via the int URB.
0552  * This means that we cannot be sure which data byte has an associated error
0553  * condition, so we report an error for all data in the next bulk read.
0554  *
0555  * Actually, there might even be a window between the bulk data leaving the
0556  * ark and reading/resetting the lsr in the read_bulk_callback where an
0557  * interrupt for the next data block could come in.
0558  * Without somekind of ordering on the ark, we would have to report the
0559  * error for the next block of data as well...
0560  * For now, let's pretend this can't happen.
0561  */
0562 static void ark3116_process_read_urb(struct urb *urb)
0563 {
0564     struct usb_serial_port *port = urb->context;
0565     struct ark3116_private *priv = usb_get_serial_port_data(port);
0566     unsigned char *data = urb->transfer_buffer;
0567     char tty_flag = TTY_NORMAL;
0568     unsigned long flags;
0569     __u32 lsr;
0570 
0571     /* update line status */
0572     spin_lock_irqsave(&priv->status_lock, flags);
0573     lsr = priv->lsr;
0574     priv->lsr &= ~UART_LSR_BRK_ERROR_BITS;
0575     spin_unlock_irqrestore(&priv->status_lock, flags);
0576 
0577     if (!urb->actual_length)
0578         return;
0579 
0580     if (lsr & UART_LSR_BRK_ERROR_BITS) {
0581         if (lsr & UART_LSR_BI)
0582             tty_flag = TTY_BREAK;
0583         else if (lsr & UART_LSR_PE)
0584             tty_flag = TTY_PARITY;
0585         else if (lsr & UART_LSR_FE)
0586             tty_flag = TTY_FRAME;
0587 
0588         /* overrun is special, not associated with a char */
0589         if (lsr & UART_LSR_OE)
0590             tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
0591     }
0592     tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
0593                             urb->actual_length);
0594     tty_flip_buffer_push(&port->port);
0595 }
0596 
0597 static struct usb_serial_driver ark3116_device = {
0598     .driver = {
0599         .owner =    THIS_MODULE,
0600         .name =     "ark3116",
0601     },
0602     .id_table =     id_table,
0603     .num_ports =        1,
0604     .num_bulk_in =      1,
0605     .num_bulk_out =     1,
0606     .num_interrupt_in = 1,
0607     .port_probe =       ark3116_port_probe,
0608     .port_remove =      ark3116_port_remove,
0609     .set_termios =      ark3116_set_termios,
0610     .tiocmget =     ark3116_tiocmget,
0611     .tiocmset =     ark3116_tiocmset,
0612     .tiocmiwait =       usb_serial_generic_tiocmiwait,
0613     .get_icount =       usb_serial_generic_get_icount,
0614     .open =         ark3116_open,
0615     .close =        ark3116_close,
0616     .break_ctl =        ark3116_break_ctl,
0617     .read_int_callback =    ark3116_read_int_callback,
0618     .process_read_urb = ark3116_process_read_urb,
0619 };
0620 
0621 static struct usb_serial_driver * const serial_drivers[] = {
0622     &ark3116_device, NULL
0623 };
0624 
0625 module_usb_serial_driver(serial_drivers, id_table);
0626 
0627 MODULE_LICENSE("GPL");
0628 
0629 MODULE_AUTHOR(DRIVER_AUTHOR);
0630 MODULE_DESCRIPTION(DRIVER_DESC);
0631 
0632 /*
0633  * The following describes what I learned from studying the old
0634  * ark3116.c driver, disassembling the windows driver, and some lucky
0635  * guesses. Since I do not have any datasheet or other
0636  * documentation, inaccuracies are almost guaranteed.
0637  *
0638  * Some specs for the ARK3116 can be found here:
0639  * http://web.archive.org/web/20060318000438/
0640  *   www.arkmicro.com/en/products/view.php?id=10
0641  * On that page, 2 GPIO pins are mentioned: I assume these are the
0642  * OUT1 and OUT2 pins of the UART, so I added support for those
0643  * through the MCR. Since the pins are not available on my hardware,
0644  * I could not verify this.
0645  * Also, it states there is "on-chip hardware flow control". I have
0646  * discovered how to enable that. Unfortunately, I do not know how to
0647  * enable XON/XOFF (software) flow control, which would need support
0648  * from the chip as well to work. Because of the wording on the web
0649  * page there is a real possibility the chip simply does not support
0650  * software flow control.
0651  *
0652  * I got my ark3116 as part of a mobile phone adapter cable. On the
0653  * PCB, the following numbered contacts are present:
0654  *
0655  *  1:- +5V
0656  *  2:o DTR
0657  *  3:i RX
0658  *  4:i DCD
0659  *  5:o RTS
0660  *  6:o TX
0661  *  7:i RI
0662  *  8:i DSR
0663  * 10:- 0V
0664  * 11:i CTS
0665  *
0666  * On my chip, all signals seem to be 3.3V, but 5V tolerant. But that
0667  * may be different for the one you have ;-).
0668  *
0669  * The windows driver limits the registers to 0-F, so I assume there
0670  * are actually 16 present on the device.
0671  *
0672  * On an UART interrupt, 4 bytes of data come in on the interrupt
0673  * endpoint. The bytes are 0xe8 IIR LSR MSR.
0674  *
0675  * The baudrate seems to be generated from the 12MHz crystal, using
0676  * 4-times subsampling. So quot=12e6/(4*baud). Also see description
0677  * of register E.
0678  *
0679  * Registers 0-7:
0680  * These seem to be the same as for a regular 16450. The FCR is set
0681  * to UART_FCR_DMA_SELECT (0x8), I guess to enable transfers between
0682  * the UART and the USB bridge/DMA engine.
0683  *
0684  * Register 8:
0685  * By trial and error, I found out that bit 0 enables hardware CTS,
0686  * stopping TX when CTS is +5V. Bit 1 does the same for RTS, making
0687  * RTS +5V when the 3116 cannot transfer the data to the USB bus
0688  * (verified by disabling the reading URB). Note that as far as I can
0689  * tell, the windows driver does NOT use this, so there might be some
0690  * hardware bug or something.
0691  *
0692  * According to a patch provided here
0693  * https://lore.kernel.org/lkml/200907261419.50702.linux@rainbow-software.org
0694  * the ARK3116 can also be used as an IrDA dongle. Since I do not have
0695  * such a thing, I could not investigate that aspect. However, I can
0696  * speculate ;-).
0697  *
0698  * - IrDA encodes data differently than RS232. Most likely, one of
0699  *   the bits in registers 9..E enables the IR ENDEC (encoder/decoder).
0700  * - Depending on the IR transceiver, the input and output need to be
0701  *   inverted, so there are probably bits for that as well.
0702  * - IrDA is half-duplex, so there should be a bit for selecting that.
0703  *
0704  * This still leaves at least two registers unaccounted for. Perhaps
0705  * The chip can do XON/XOFF or CRC in HW?
0706  *
0707  * Register 9:
0708  * Set to 0x00 for IrDA, when the baudrate is initialised.
0709  *
0710  * Register A:
0711  * Set to 0x01 for IrDA, at init.
0712  *
0713  * Register B:
0714  * Set to 0x01 for IrDA, 0x00 for RS232, at init.
0715  *
0716  * Register C:
0717  * Set to 00 for IrDA, at init.
0718  *
0719  * Register D:
0720  * Set to 0x41 for IrDA, at init.
0721  *
0722  * Register E:
0723  * Somekind of baudrate override. The windows driver seems to set
0724  * this to 0x00 for normal baudrates, 0x01 for 460800, 0x02 for 921600.
0725  * Since 460800 and 921600 cannot be obtained by dividing 3MHz by an integer,
0726  * it could be somekind of subdivisor thingy.
0727  * However,it does not seem to do anything: selecting 921600 (divisor 3,
0728  * reg E=2), still gets 1 MHz. I also checked if registers 9, C or F would
0729  * work, but they don't.
0730  *
0731  * Register F: unknown
0732  */