155714Skris// SPDX-License-Identifier: GPL-2.0+ 255714Skris/* 355714Skris * Copyright (C) 2009 by Bart Hartgers (bart.hartgers+ark3116@gmail.com) 455714Skris * Original version: 555714Skris * Copyright (C) 2006 655714Skris * Simon Schulz (ark3116_driver <at> auctionant.de) 755714Skris * 855714Skris * ark3116 955714Skris * - implements a driver for the arkmicro ark3116 chipset (vendor=0x6547, 1055714Skris * productid=0x0232) (used in a datacable called KQ-U8A) 1155714Skris * 1255714Skris * Supports full modem status lines, break, hardware flow control. Does not 1355714Skris * support software flow control, since I do not know how to enable it in hw. 1455714Skris * 1555714Skris * This driver is a essentially new implementation. I initially dug 1655714Skris * into the old ark3116.c driver and suddenly realized the ark3116 is 1755714Skris * a 16450 with a USB interface glued to it. See comments at the 1855714Skris * bottom of this file. 1955714Skris */ 2055714Skris 2155714Skris#include <linux/kernel.h> 2255714Skris#include <linux/ioctl.h> 2355714Skris#include <linux/tty.h> 2455714Skris#include <linux/slab.h> 2555714Skris#include <linux/tty_flip.h> 2655714Skris#include <linux/module.h> 2755714Skris#include <linux/usb.h> 2855714Skris#include <linux/usb/serial.h> 2955714Skris#include <linux/serial.h> 3055714Skris#include <linux/serial_reg.h> 3155714Skris#include <linux/uaccess.h> 3255714Skris#include <linux/mutex.h> 3355714Skris#include <linux/spinlock.h> 3455714Skris 3555714Skris#define DRIVER_AUTHOR "Bart Hartgers <bart.hartgers+ark3116@gmail.com>" 3655714Skris#define DRIVER_DESC "USB ARK3116 serial/IrDA driver" 3755714Skris#define DRIVER_DEV_DESC "ARK3116 RS232/IrDA" 3855714Skris#define DRIVER_NAME "ark3116" 3955714Skris 4055714Skris/* usb timeout of 1 second */ 4155714Skris#define ARK_TIMEOUT 1000 4255714Skris 4355714Skrisstatic const struct usb_device_id id_table[] = { 4455714Skris { USB_DEVICE(0x6547, 0x0232) }, 4555714Skris { USB_DEVICE(0x18ec, 0x3118) }, /* USB to IrDA adapter */ 4655714Skris { }, 4755714Skris}; 4855714SkrisMODULE_DEVICE_TABLE(usb, id_table); 4955714Skris 5055714Skrisstatic int is_irda(struct usb_serial *serial) 5155714Skris{ 5255714Skris struct usb_device *dev = serial->dev; 5355714Skris if (le16_to_cpu(dev->descriptor.idVendor) == 0x18ec && 5455714Skris le16_to_cpu(dev->descriptor.idProduct) == 0x3118) 5555714Skris return 1; 5655714Skris return 0; 5755714Skris} 5855714Skris 5955714Skrisstruct ark3116_private { 6055714Skris int irda; /* 1 for irda device */ 6155714Skris 6255714Skris /* protects hw register updates */ 6355714Skris struct mutex hw_lock; 6455714Skris 6555714Skris int quot; /* baudrate divisor */ 6655714Skris __u32 lcr; /* line control register value */ 6755714Skris __u32 hcr; /* handshake control register (0x8) 6855714Skris * value */ 6955714Skris __u32 mcr; /* modem control register value */ 7055714Skris 7155714Skris /* protects the status values below */ 7255714Skris spinlock_t status_lock; 7355714Skris __u32 msr; /* modem status register value */ 7455714Skris __u32 lsr; /* line status register value */ 7555714Skris}; 7655714Skris 7755714Skrisstatic int ark3116_write_reg(struct usb_serial *serial, 7855714Skris unsigned reg, __u8 val) 7955714Skris{ 8055714Skris int result; 8155714Skris /* 0xfe 0x40 are magic values taken from original driver */ 8255714Skris result = usb_control_msg(serial->dev, 8355714Skris usb_sndctrlpipe(serial->dev, 0), 8455714Skris 0xfe, 0x40, val, reg, 8555714Skris NULL, 0, ARK_TIMEOUT); 8655714Skris if (result) 8755714Skris return result; 8855714Skris 8955714Skris return 0; 9055714Skris} 9155714Skris 9255714Skrisstatic int ark3116_read_reg(struct usb_serial *serial, 9355714Skris unsigned reg, unsigned char *buf) 9455714Skris{ 9555714Skris int result; 9655714Skris /* 0xfe 0xc0 are magic values taken from original driver */ 9755714Skris result = usb_control_msg(serial->dev, 9855714Skris usb_rcvctrlpipe(serial->dev, 0), 9955714Skris 0xfe, 0xc0, 0, reg, 10055714Skris buf, 1, ARK_TIMEOUT); 10155714Skris if (result < 1) { 10255714Skris dev_err(&serial->interface->dev, 10355714Skris "failed to read register %u: %d\n", 10455714Skris reg, result); 10555714Skris if (result >= 0) 10655714Skris result = -EIO; 10755714Skris 10855714Skris return result; 10959191Skris } 11059191Skris 11159191Skris return 0; 11255714Skris} 11359191Skris 11459191Skrisstatic inline int calc_divisor(int bps) 11559191Skris{ 11659191Skris /* Original ark3116 made some exceptions in rounding here 11759191Skris * because windows did the same. Assume that is not really 11859191Skris * necessary. 11959191Skris * Crystal is 12MHz, probably because of USB, but we divide by 4? 12059191Skris */ 12159191Skris return (12000000 + 2*bps) / (4*bps); 12259191Skris} 12359191Skris 12455714Skrisstatic int ark3116_port_probe(struct usb_serial_port *port) 12555714Skris{ 12655714Skris struct usb_serial *serial = port->serial; 12755714Skris struct ark3116_private *priv; 12855714Skris 12955714Skris priv = kzalloc(sizeof(*priv), GFP_KERNEL); 13055714Skris if (!priv) 13155714Skris return -ENOMEM; 13255714Skris 13355714Skris mutex_init(&priv->hw_lock); 13455714Skris spin_lock_init(&priv->status_lock); 13555714Skris 13655714Skris priv->irda = is_irda(serial); 13755714Skris 13855714Skris usb_set_serial_port_data(port, priv); 13955714Skris 14055714Skris /* setup the hardware */ 14155714Skris ark3116_write_reg(serial, UART_IER, 0); 14255714Skris /* disable DMA */ 14355714Skris ark3116_write_reg(serial, UART_FCR, 0); 14455714Skris /* handshake control */ 14555714Skris priv->hcr = 0; 14655714Skris ark3116_write_reg(serial, 0x8 , 0); 14755714Skris /* modem control */ 14855714Skris priv->mcr = 0; 14955714Skris ark3116_write_reg(serial, UART_MCR, 0); 15055714Skris 15155714Skris if (!(priv->irda)) { 15255714Skris ark3116_write_reg(serial, 0xb , 0); 15355714Skris } else { 15455714Skris ark3116_write_reg(serial, 0xb , 1); 15555714Skris ark3116_write_reg(serial, 0xc , 0); 15655714Skris ark3116_write_reg(serial, 0xd , 0x41); 15755714Skris ark3116_write_reg(serial, 0xa , 1); 15855714Skris } 15955714Skris 16055714Skris /* setup baudrate */ 16155714Skris ark3116_write_reg(serial, UART_LCR, UART_LCR_DLAB); 16255714Skris 16355714Skris /* setup for 9600 8N1 */ 16455714Skris priv->quot = calc_divisor(9600); 16555714Skris ark3116_write_reg(serial, UART_DLL, priv->quot & 0xff); 16655714Skris ark3116_write_reg(serial, UART_DLM, (priv->quot>>8) & 0xff); 16755714Skris 16855714Skris priv->lcr = UART_LCR_WLEN8; 16955714Skris ark3116_write_reg(serial, UART_LCR, UART_LCR_WLEN8); 17055714Skris 17155714Skris ark3116_write_reg(serial, 0xe, 0); 17255714Skris 17355714Skris if (priv->irda) 17455714Skris ark3116_write_reg(serial, 0x9, 0); 17555714Skris 17655714Skris dev_info(&port->dev, "using %s mode\n", priv->irda ? "IrDA" : "RS232"); 17755714Skris 17855714Skris return 0; 17955714Skris} 18055714Skris 18155714Skrisstatic void ark3116_port_remove(struct usb_serial_port *port) 18255714Skris{ 18355714Skris struct ark3116_private *priv = usb_get_serial_port_data(port); 18455714Skris 18555714Skris /* device is closed, so URBs and DMA should be down */ 18655714Skris mutex_destroy(&priv->hw_lock); 18755714Skris kfree(priv); 18855714Skris} 18955714Skris 19055714Skrisstatic void ark3116_set_termios(struct tty_struct *tty, 19155714Skris struct usb_serial_port *port, 19255714Skris const struct ktermios *old_termios) 19355714Skris{ 19455714Skris struct usb_serial *serial = port->serial; 19555714Skris struct ark3116_private *priv = usb_get_serial_port_data(port); 19655714Skris struct ktermios *termios = &tty->termios; 19755714Skris unsigned int cflag = termios->c_cflag; 19855714Skris int bps = tty_get_baud_rate(tty); 19955714Skris int quot; 20055714Skris __u8 lcr, hcr, eval; 20155714Skris 20255714Skris /* set data bit count */ 20355714Skris lcr = UART_LCR_WLEN(tty_get_char_size(cflag)); 20455714Skris 20555714Skris if (cflag & CSTOPB) 20655714Skris lcr |= UART_LCR_STOP; 20755714Skris if (cflag & PARENB) 20855714Skris lcr |= UART_LCR_PARITY; 20955714Skris if (!(cflag & PARODD)) 21055714Skris lcr |= UART_LCR_EPAR; 21155714Skris if (cflag & CMSPAR) 21255714Skris lcr |= UART_LCR_SPAR; 21355714Skris 21455714Skris /* handshake control */ 21555714Skris hcr = (cflag & CRTSCTS) ? 0x03 : 0x00; 21655714Skris 21755714Skris /* calc baudrate */ 21855714Skris dev_dbg(&port->dev, "%s - setting bps to %d\n", __func__, bps); 21955714Skris eval = 0; 22055714Skris switch (bps) { 22155714Skris case 0: 22255714Skris quot = calc_divisor(9600); 22355714Skris break; 22455714Skris default: 22555714Skris if ((bps < 75) || (bps > 3000000)) 22655714Skris bps = 9600; 22755714Skris quot = calc_divisor(bps); 22855714Skris break; 22955714Skris case 460800: 23055714Skris eval = 1; 23155714Skris quot = calc_divisor(bps); 23255714Skris break; 23355714Skris case 921600: 23455714Skris eval = 2; 23555714Skris quot = calc_divisor(bps); 23655714Skris break; 23755714Skris } 23855714Skris 23955714Skris /* Update state: synchronize */ 24055714Skris mutex_lock(&priv->hw_lock); 24155714Skris 24255714Skris /* keep old LCR_SBC bit */ 24355714Skris lcr |= (priv->lcr & UART_LCR_SBC); 24455714Skris 24555714Skris dev_dbg(&port->dev, "%s - setting hcr:0x%02x,lcr:0x%02x,quot:%d\n", 24655714Skris __func__, hcr, lcr, quot); 24755714Skris 24855714Skris /* handshake control */ 24955714Skris if (priv->hcr != hcr) { 25055714Skris priv->hcr = hcr; 25155714Skris ark3116_write_reg(serial, 0x8, hcr); 25255714Skris } 25355714Skris 25455714Skris /* baudrate */ 25555714Skris if (priv->quot != quot) { 25655714Skris priv->quot = quot; 25755714Skris priv->lcr = lcr; /* need to write lcr anyway */ 25855714Skris 25955714Skris /* disable DMA since transmit/receive is 26055714Skris * shadowed by UART_DLL 26155714Skris */ 26255714Skris ark3116_write_reg(serial, UART_FCR, 0); 26355714Skris 26455714Skris ark3116_write_reg(serial, UART_LCR, 26555714Skris lcr|UART_LCR_DLAB); 26655714Skris ark3116_write_reg(serial, UART_DLL, quot & 0xff); 26755714Skris ark3116_write_reg(serial, UART_DLM, (quot>>8) & 0xff); 26855714Skris 26955714Skris /* restore lcr */ 27055714Skris ark3116_write_reg(serial, UART_LCR, lcr); 27155714Skris /* magic baudrate thingy: not sure what it does, 27255714Skris * but windows does this as well. 27355714Skris */ 27455714Skris ark3116_write_reg(serial, 0xe, eval); 27555714Skris 27655714Skris /* enable DMA */ 27755714Skris ark3116_write_reg(serial, UART_FCR, UART_FCR_DMA_SELECT); 27855714Skris } else if (priv->lcr != lcr) { 27955714Skris priv->lcr = lcr; 28055714Skris ark3116_write_reg(serial, UART_LCR, lcr); 28155714Skris } 28255714Skris 28355714Skris mutex_unlock(&priv->hw_lock); 28455714Skris 28555714Skris /* check for software flow control */ 28655714Skris if (I_IXOFF(tty) || I_IXON(tty)) { 28755714Skris dev_warn(&port->dev, 28855714Skris "software flow control not implemented\n"); 28955714Skris } 29055714Skris 29155714Skris /* Don't rewrite B0 */ 29255714Skris if (tty_termios_baud_rate(termios)) 29355714Skris tty_termios_encode_baud_rate(termios, bps, bps); 29455714Skris} 29555714Skris 29655714Skrisstatic void ark3116_close(struct usb_serial_port *port) 29755714Skris{ 29855714Skris struct usb_serial *serial = port->serial; 29955714Skris 30055714Skris /* disable DMA */ 30155714Skris ark3116_write_reg(serial, UART_FCR, 0); 30255714Skris 30355714Skris /* deactivate interrupts */ 30455714Skris ark3116_write_reg(serial, UART_IER, 0); 30555714Skris 30655714Skris usb_serial_generic_close(port); 30755714Skris 30855714Skris usb_kill_urb(port->interrupt_in_urb); 30955714Skris} 31055714Skris 31155714Skrisstatic int ark3116_open(struct tty_struct *tty, struct usb_serial_port *port) 31255714Skris{ 31355714Skris struct ark3116_private *priv = usb_get_serial_port_data(port); 31455714Skris struct usb_serial *serial = port->serial; 31555714Skris unsigned char *buf; 31655714Skris int result; 31755714Skris 31855714Skris buf = kmalloc(1, GFP_KERNEL); 31955714Skris if (buf == NULL) 32055714Skris return -ENOMEM; 32155714Skris 32255714Skris result = usb_serial_generic_open(tty, port); 32355714Skris if (result) { 32455714Skris dev_dbg(&port->dev, 32555714Skris "%s - usb_serial_generic_open failed: %d\n", 32655714Skris __func__, result); 32755714Skris goto err_free; 32855714Skris } 32955714Skris 33055714Skris /* remove any data still left: also clears error state */ 33155714Skris ark3116_read_reg(serial, UART_RX, buf); 33255714Skris 33355714Skris /* read modem status */ 33455714Skris result = ark3116_read_reg(serial, UART_MSR, buf); 33555714Skris if (result) 33655714Skris goto err_close; 33755714Skris priv->msr = *buf; 33855714Skris 33955714Skris /* read line status */ 34055714Skris result = ark3116_read_reg(serial, UART_LSR, buf); 34155714Skris if (result) 34255714Skris goto err_close; 34355714Skris priv->lsr = *buf; 34455714Skris 34555714Skris result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); 34655714Skris if (result) { 34755714Skris dev_err(&port->dev, "submit irq_in urb failed %d\n", 34855714Skris result); 34955714Skris goto err_close; 35055714Skris } 35155714Skris 35255714Skris /* activate interrupts */ 35355714Skris ark3116_write_reg(port->serial, UART_IER, UART_IER_MSI|UART_IER_RLSI); 35455714Skris 35555714Skris /* enable DMA */ 35655714Skris ark3116_write_reg(port->serial, UART_FCR, UART_FCR_DMA_SELECT); 35755714Skris 35855714Skris /* setup termios */ 35955714Skris if (tty) 36055714Skris ark3116_set_termios(tty, port, NULL); 36155714Skris 36255714Skris kfree(buf); 36355714Skris 36455714Skris return 0; 36555714Skris 36655714Skriserr_close: 36755714Skris usb_serial_generic_close(port); 36855714Skriserr_free: 36955714Skris kfree(buf); 37055714Skris 37155714Skris return result; 37255714Skris} 37355714Skris 37455714Skrisstatic int ark3116_tiocmget(struct tty_struct *tty) 37555714Skris{ 37655714Skris struct usb_serial_port *port = tty->driver_data; 37755714Skris struct ark3116_private *priv = usb_get_serial_port_data(port); 37855714Skris __u32 status; 37955714Skris __u32 ctrl; 38055714Skris unsigned long flags; 38155714Skris 38255714Skris mutex_lock(&priv->hw_lock); 38355714Skris ctrl = priv->mcr; 38455714Skris mutex_unlock(&priv->hw_lock); 38555714Skris 38655714Skris spin_lock_irqsave(&priv->status_lock, flags); 38755714Skris status = priv->msr; 38855714Skris spin_unlock_irqrestore(&priv->status_lock, flags); 38955714Skris 39055714Skris return (status & UART_MSR_DSR ? TIOCM_DSR : 0) | 39155714Skris (status & UART_MSR_CTS ? TIOCM_CTS : 0) | 39255714Skris (status & UART_MSR_RI ? TIOCM_RI : 0) | 39355714Skris (status & UART_MSR_DCD ? TIOCM_CD : 0) | 39455714Skris (ctrl & UART_MCR_DTR ? TIOCM_DTR : 0) | 39555714Skris (ctrl & UART_MCR_RTS ? TIOCM_RTS : 0) | 39655714Skris (ctrl & UART_MCR_OUT1 ? TIOCM_OUT1 : 0) | 39755714Skris (ctrl & UART_MCR_OUT2 ? TIOCM_OUT2 : 0); 39855714Skris} 39955714Skris 40055714Skrisstatic int ark3116_tiocmset(struct tty_struct *tty, 40155714Skris unsigned set, unsigned clr) 40255714Skris{ 40355714Skris struct usb_serial_port *port = tty->driver_data; 40455714Skris struct ark3116_private *priv = usb_get_serial_port_data(port); 40555714Skris 40655714Skris /* we need to take the mutex here, to make sure that the value 40755714Skris * in priv->mcr is actually the one that is in the hardware 40855714Skris */ 40955714Skris 41055714Skris mutex_lock(&priv->hw_lock); 41155714Skris 41255714Skris if (set & TIOCM_RTS) 41355714Skris priv->mcr |= UART_MCR_RTS; 41455714Skris if (set & TIOCM_DTR) 41555714Skris priv->mcr |= UART_MCR_DTR; 41655714Skris if (set & TIOCM_OUT1) 41755714Skris priv->mcr |= UART_MCR_OUT1; 41855714Skris if (set & TIOCM_OUT2) 41955714Skris priv->mcr |= UART_MCR_OUT2; 42055714Skris if (clr & TIOCM_RTS) 42155714Skris priv->mcr &= ~UART_MCR_RTS; 42255714Skris if (clr & TIOCM_DTR) 42355714Skris priv->mcr &= ~UART_MCR_DTR; 42455714Skris if (clr & TIOCM_OUT1) 42555714Skris priv->mcr &= ~UART_MCR_OUT1; 42655714Skris if (clr & TIOCM_OUT2) 42755714Skris priv->mcr &= ~UART_MCR_OUT2; 42855714Skris 42955714Skris ark3116_write_reg(port->serial, UART_MCR, priv->mcr); 43055714Skris 43155714Skris mutex_unlock(&priv->hw_lock); 43255714Skris 43355714Skris return 0; 43455714Skris} 43555714Skris 43655714Skrisstatic int ark3116_break_ctl(struct tty_struct *tty, int break_state) 43755714Skris{ 43855714Skris struct usb_serial_port *port = tty->driver_data; 43955714Skris struct ark3116_private *priv = usb_get_serial_port_data(port); 44055714Skris int ret; 44155714Skris 44255714Skris /* LCR is also used for other things: protect access */ 44355714Skris mutex_lock(&priv->hw_lock); 44455714Skris 44555714Skris if (break_state) 44655714Skris priv->lcr |= UART_LCR_SBC; 44755714Skris else 44855714Skris priv->lcr &= ~UART_LCR_SBC; 44955714Skris 45055714Skris ret = ark3116_write_reg(port->serial, UART_LCR, priv->lcr); 45155714Skris 45255714Skris mutex_unlock(&priv->hw_lock); 45355714Skris 45455714Skris return ret; 45555714Skris} 45655714Skris 45755714Skrisstatic void ark3116_update_msr(struct usb_serial_port *port, __u8 msr) 45855714Skris{ 45955714Skris struct ark3116_private *priv = usb_get_serial_port_data(port); 46055714Skris unsigned long flags; 46155714Skris 46255714Skris spin_lock_irqsave(&priv->status_lock, flags); 46355714Skris priv->msr = msr; 46455714Skris spin_unlock_irqrestore(&priv->status_lock, flags); 46555714Skris 46655714Skris if (msr & UART_MSR_ANY_DELTA) { 46755714Skris /* update input line counters */ 46855714Skris if (msr & UART_MSR_DCTS) 46955714Skris port->icount.cts++; 47055714Skris if (msr & UART_MSR_DDSR) 47155714Skris port->icount.dsr++; 47255714Skris if (msr & UART_MSR_DDCD) 47355714Skris port->icount.dcd++; 47455714Skris if (msr & UART_MSR_TERI) 47555714Skris port->icount.rng++; 47655714Skris wake_up_interruptible(&port->port.delta_msr_wait); 47755714Skris } 47855714Skris} 47955714Skris 48055714Skrisstatic void ark3116_update_lsr(struct usb_serial_port *port, __u8 lsr) 48155714Skris{ 48255714Skris struct ark3116_private *priv = usb_get_serial_port_data(port); 48355714Skris unsigned long flags; 48455714Skris 48555714Skris spin_lock_irqsave(&priv->status_lock, flags); 48655714Skris /* combine bits */ 48755714Skris priv->lsr |= lsr; 48855714Skris spin_unlock_irqrestore(&priv->status_lock, flags); 48955714Skris 49055714Skris if (lsr&UART_LSR_BRK_ERROR_BITS) { 49155714Skris if (lsr & UART_LSR_BI) 49255714Skris port->icount.brk++; 49355714Skris if (lsr & UART_LSR_FE) 49455714Skris port->icount.frame++; 49555714Skris if (lsr & UART_LSR_PE) 49655714Skris port->icount.parity++; 49755714Skris if (lsr & UART_LSR_OE) 49855714Skris port->icount.overrun++; 49955714Skris } 50055714Skris} 50155714Skris 50255714Skrisstatic void ark3116_read_int_callback(struct urb *urb) 50355714Skris{ 50455714Skris struct usb_serial_port *port = urb->context; 50555714Skris int status = urb->status; 50655714Skris const __u8 *data = urb->transfer_buffer; 50755714Skris int result; 50855714Skris 50955714Skris switch (status) { 51055714Skris case -ECONNRESET: 51155714Skris case -ENOENT: 51255714Skris case -ESHUTDOWN: 51355714Skris /* this urb is terminated, clean up */ 51455714Skris dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n", 51555714Skris __func__, status); 51655714Skris return; 51755714Skris default: 51855714Skris dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n", 51955714Skris __func__, status); 52055714Skris break; 52155714Skris case 0: /* success */ 52255714Skris /* discovered this by trail and error... */ 52355714Skris if ((urb->actual_length == 4) && (data[0] == 0xe8)) { 52455714Skris const __u8 id = data[1]&UART_IIR_ID; 52555714Skris dev_dbg(&port->dev, "%s: iir=%02x\n", __func__, data[1]); 52655714Skris if (id == UART_IIR_MSI) { 52755714Skris dev_dbg(&port->dev, "%s: msr=%02x\n", 52855714Skris __func__, data[3]); 52955714Skris ark3116_update_msr(port, data[3]); 53055714Skris break; 53155714Skris } else if (id == UART_IIR_RLSI) { 53255714Skris dev_dbg(&port->dev, "%s: lsr=%02x\n", 53355714Skris __func__, data[2]); 53455714Skris ark3116_update_lsr(port, data[2]); 53555714Skris break; 53655714Skris } 53755714Skris } 53855714Skris /* 53955714Skris * Not sure what this data meant... 54055714Skris */ 54155714Skris usb_serial_debug_data(&port->dev, __func__, 54255714Skris urb->actual_length, 54355714Skris urb->transfer_buffer); 54455714Skris break; 54555714Skris } 54655714Skris 54755714Skris result = usb_submit_urb(urb, GFP_ATOMIC); 54855714Skris if (result) 54955714Skris dev_err(&port->dev, "failed to resubmit interrupt urb: %d\n", 55055714Skris result); 55155714Skris} 55255714Skris 55355714Skris 55455714Skris/* Data comes in via the bulk (data) URB, errors/interrupts via the int URB. 55555714Skris * This means that we cannot be sure which data byte has an associated error 55655714Skris * condition, so we report an error for all data in the next bulk read. 55755714Skris * 55855714Skris * Actually, there might even be a window between the bulk data leaving the 55955714Skris * ark and reading/resetting the lsr in the read_bulk_callback where an 56055714Skris * interrupt for the next data block could come in. 561 * Without somekind of ordering on the ark, we would have to report the 562 * error for the next block of data as well... 563 * For now, let's pretend this can't happen. 564 */ 565static void ark3116_process_read_urb(struct urb *urb) 566{ 567 struct usb_serial_port *port = urb->context; 568 struct ark3116_private *priv = usb_get_serial_port_data(port); 569 unsigned char *data = urb->transfer_buffer; 570 char tty_flag = TTY_NORMAL; 571 unsigned long flags; 572 __u32 lsr; 573 574 /* update line status */ 575 spin_lock_irqsave(&priv->status_lock, flags); 576 lsr = priv->lsr; 577 priv->lsr &= ~UART_LSR_BRK_ERROR_BITS; 578 spin_unlock_irqrestore(&priv->status_lock, flags); 579 580 if (!urb->actual_length) 581 return; 582 583 if (lsr & UART_LSR_BRK_ERROR_BITS) { 584 if (lsr & UART_LSR_BI) 585 tty_flag = TTY_BREAK; 586 else if (lsr & UART_LSR_PE) 587 tty_flag = TTY_PARITY; 588 else if (lsr & UART_LSR_FE) 589 tty_flag = TTY_FRAME; 590 591 /* overrun is special, not associated with a char */ 592 if (lsr & UART_LSR_OE) 593 tty_insert_flip_char(&port->port, 0, TTY_OVERRUN); 594 } 595 tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag, 596 urb->actual_length); 597 tty_flip_buffer_push(&port->port); 598} 599 600static struct usb_serial_driver ark3116_device = { 601 .driver = { 602 .owner = THIS_MODULE, 603 .name = "ark3116", 604 }, 605 .id_table = id_table, 606 .num_ports = 1, 607 .num_bulk_in = 1, 608 .num_bulk_out = 1, 609 .num_interrupt_in = 1, 610 .port_probe = ark3116_port_probe, 611 .port_remove = ark3116_port_remove, 612 .set_termios = ark3116_set_termios, 613 .tiocmget = ark3116_tiocmget, 614 .tiocmset = ark3116_tiocmset, 615 .tiocmiwait = usb_serial_generic_tiocmiwait, 616 .get_icount = usb_serial_generic_get_icount, 617 .open = ark3116_open, 618 .close = ark3116_close, 619 .break_ctl = ark3116_break_ctl, 620 .read_int_callback = ark3116_read_int_callback, 621 .process_read_urb = ark3116_process_read_urb, 622}; 623 624static struct usb_serial_driver * const serial_drivers[] = { 625 &ark3116_device, NULL 626}; 627 628module_usb_serial_driver(serial_drivers, id_table); 629 630MODULE_LICENSE("GPL"); 631 632MODULE_AUTHOR(DRIVER_AUTHOR); 633MODULE_DESCRIPTION(DRIVER_DESC); 634 635/* 636 * The following describes what I learned from studying the old 637 * ark3116.c driver, disassembling the windows driver, and some lucky 638 * guesses. Since I do not have any datasheet or other 639 * documentation, inaccuracies are almost guaranteed. 640 * 641 * Some specs for the ARK3116 can be found here: 642 * http://web.archive.org/web/20060318000438/ 643 * www.arkmicro.com/en/products/view.php?id=10 644 * On that page, 2 GPIO pins are mentioned: I assume these are the 645 * OUT1 and OUT2 pins of the UART, so I added support for those 646 * through the MCR. Since the pins are not available on my hardware, 647 * I could not verify this. 648 * Also, it states there is "on-chip hardware flow control". I have 649 * discovered how to enable that. Unfortunately, I do not know how to 650 * enable XON/XOFF (software) flow control, which would need support 651 * from the chip as well to work. Because of the wording on the web 652 * page there is a real possibility the chip simply does not support 653 * software flow control. 654 * 655 * I got my ark3116 as part of a mobile phone adapter cable. On the 656 * PCB, the following numbered contacts are present: 657 * 658 * 1:- +5V 659 * 2:o DTR 660 * 3:i RX 661 * 4:i DCD 662 * 5:o RTS 663 * 6:o TX 664 * 7:i RI 665 * 8:i DSR 666 * 10:- 0V 667 * 11:i CTS 668 * 669 * On my chip, all signals seem to be 3.3V, but 5V tolerant. But that 670 * may be different for the one you have ;-). 671 * 672 * The windows driver limits the registers to 0-F, so I assume there 673 * are actually 16 present on the device. 674 * 675 * On an UART interrupt, 4 bytes of data come in on the interrupt 676 * endpoint. The bytes are 0xe8 IIR LSR MSR. 677 * 678 * The baudrate seems to be generated from the 12MHz crystal, using 679 * 4-times subsampling. So quot=12e6/(4*baud). Also see description 680 * of register E. 681 * 682 * Registers 0-7: 683 * These seem to be the same as for a regular 16450. The FCR is set 684 * to UART_FCR_DMA_SELECT (0x8), I guess to enable transfers between 685 * the UART and the USB bridge/DMA engine. 686 * 687 * Register 8: 688 * By trial and error, I found out that bit 0 enables hardware CTS, 689 * stopping TX when CTS is +5V. Bit 1 does the same for RTS, making 690 * RTS +5V when the 3116 cannot transfer the data to the USB bus 691 * (verified by disabling the reading URB). Note that as far as I can 692 * tell, the windows driver does NOT use this, so there might be some 693 * hardware bug or something. 694 * 695 * According to a patch provided here 696 * https://lore.kernel.org/lkml/200907261419.50702.linux@rainbow-software.org 697 * the ARK3116 can also be used as an IrDA dongle. Since I do not have 698 * such a thing, I could not investigate that aspect. However, I can 699 * speculate ;-). 700 * 701 * - IrDA encodes data differently than RS232. Most likely, one of 702 * the bits in registers 9..E enables the IR ENDEC (encoder/decoder). 703 * - Depending on the IR transceiver, the input and output need to be 704 * inverted, so there are probably bits for that as well. 705 * - IrDA is half-duplex, so there should be a bit for selecting that. 706 * 707 * This still leaves at least two registers unaccounted for. Perhaps 708 * The chip can do XON/XOFF or CRC in HW? 709 * 710 * Register 9: 711 * Set to 0x00 for IrDA, when the baudrate is initialised. 712 * 713 * Register A: 714 * Set to 0x01 for IrDA, at init. 715 * 716 * Register B: 717 * Set to 0x01 for IrDA, 0x00 for RS232, at init. 718 * 719 * Register C: 720 * Set to 00 for IrDA, at init. 721 * 722 * Register D: 723 * Set to 0x41 for IrDA, at init. 724 * 725 * Register E: 726 * Somekind of baudrate override. The windows driver seems to set 727 * this to 0x00 for normal baudrates, 0x01 for 460800, 0x02 for 921600. 728 * Since 460800 and 921600 cannot be obtained by dividing 3MHz by an integer, 729 * it could be somekind of subdivisor thingy. 730 * However,it does not seem to do anything: selecting 921600 (divisor 3, 731 * reg E=2), still gets 1 MHz. I also checked if registers 9, C or F would 732 * work, but they don't. 733 * 734 * Register F: unknown 735 */ 736