152af9545SBill Pemberton /* 252af9545SBill Pemberton * usb-serial driver for Quatech SSU-100 352af9545SBill Pemberton * 452af9545SBill Pemberton * based on ftdi_sio.c and the original serqt_usb.c from Quatech 552af9545SBill Pemberton * 652af9545SBill Pemberton */ 752af9545SBill Pemberton 852af9545SBill Pemberton #include <linux/errno.h> 952af9545SBill Pemberton #include <linux/init.h> 1052af9545SBill Pemberton #include <linux/slab.h> 1152af9545SBill Pemberton #include <linux/tty.h> 1252af9545SBill Pemberton #include <linux/tty_driver.h> 1352af9545SBill Pemberton #include <linux/tty_flip.h> 1452af9545SBill Pemberton #include <linux/module.h> 1552af9545SBill Pemberton #include <linux/serial.h> 1652af9545SBill Pemberton #include <linux/usb.h> 1752af9545SBill Pemberton #include <linux/usb/serial.h> 1879f203a2SBill Pemberton #include <linux/serial_reg.h> 1952af9545SBill Pemberton #include <linux/uaccess.h> 2052af9545SBill Pemberton 2152af9545SBill Pemberton #define QT_OPEN_CLOSE_CHANNEL 0xca 2252af9545SBill Pemberton #define QT_SET_GET_DEVICE 0xc2 2352af9545SBill Pemberton #define QT_SET_GET_REGISTER 0xc0 2452af9545SBill Pemberton #define QT_GET_SET_PREBUF_TRIG_LVL 0xcc 2552af9545SBill Pemberton #define QT_SET_ATF 0xcd 2652af9545SBill Pemberton #define QT_GET_SET_UART 0xc1 2752af9545SBill Pemberton #define QT_TRANSFER_IN 0xc0 2852af9545SBill Pemberton #define QT_HW_FLOW_CONTROL_MASK 0xc5 2952af9545SBill Pemberton #define QT_SW_FLOW_CONTROL_MASK 0xc6 3052af9545SBill Pemberton 3152af9545SBill Pemberton #define SERIAL_MSR_MASK 0xf0 3252af9545SBill Pemberton 3379f203a2SBill Pemberton #define SERIAL_CRTSCTS ((UART_MCR_RTS << 8) | UART_MSR_CTS) 3452af9545SBill Pemberton 3579f203a2SBill Pemberton #define SERIAL_EVEN_PARITY (UART_LCR_PARITY | UART_LCR_EPAR) 3652af9545SBill Pemberton 3752af9545SBill Pemberton #define MAX_BAUD_RATE 460800 3852af9545SBill Pemberton 3952af9545SBill Pemberton #define ATC_DISABLED 0x00 4052af9545SBill Pemberton #define DUPMODE_BITS 0xc0 4152af9545SBill Pemberton #define RR_BITS 0x03 4252af9545SBill Pemberton #define LOOPMODE_BITS 0x41 4352af9545SBill Pemberton #define RS232_MODE 0x00 4452af9545SBill Pemberton #define RTSCTS_TO_CONNECTOR 0x40 4552af9545SBill Pemberton #define CLKS_X4 0x02 4652af9545SBill Pemberton #define FULLPWRBIT 0x00000080 4752af9545SBill Pemberton #define NEXT_BOARD_POWER_BIT 0x00000004 4852af9545SBill Pemberton 4990ab5ee9SRusty Russell static bool debug; 5052af9545SBill Pemberton 5152af9545SBill Pemberton /* Version Information */ 5252af9545SBill Pemberton #define DRIVER_VERSION "v0.1" 5352af9545SBill Pemberton #define DRIVER_DESC "Quatech SSU-100 USB to Serial Driver" 5452af9545SBill Pemberton 5552af9545SBill Pemberton #define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */ 5652af9545SBill Pemberton #define QUATECH_SSU100 0xC020 /* SSU100 */ 5752af9545SBill Pemberton 5852af9545SBill Pemberton static const struct usb_device_id id_table[] = { 5952af9545SBill Pemberton {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU100)}, 6052af9545SBill Pemberton {} /* Terminating entry */ 6152af9545SBill Pemberton }; 6252af9545SBill Pemberton MODULE_DEVICE_TABLE(usb, id_table); 6352af9545SBill Pemberton 6452af9545SBill Pemberton struct ssu100_port_private { 6517523058SBill Pemberton spinlock_t status_lock; 6652af9545SBill Pemberton u8 shadowLSR; 6752af9545SBill Pemberton u8 shadowMSR; 6852af9545SBill Pemberton wait_queue_head_t delta_msr_wait; /* Used for TIOCMIWAIT */ 69f81c83dbSBill Pemberton struct async_icount icount; 7052af9545SBill Pemberton }; 7152af9545SBill Pemberton 7252af9545SBill Pemberton static void ssu100_release(struct usb_serial *serial) 7352af9545SBill Pemberton { 7452af9545SBill Pemberton struct ssu100_port_private *priv = usb_get_serial_port_data(*serial->port); 7552af9545SBill Pemberton 7652af9545SBill Pemberton kfree(priv); 7752af9545SBill Pemberton } 7852af9545SBill Pemberton 7952af9545SBill Pemberton static inline int ssu100_control_msg(struct usb_device *dev, 8052af9545SBill Pemberton u8 request, u16 data, u16 index) 8152af9545SBill Pemberton { 8252af9545SBill Pemberton return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 8352af9545SBill Pemberton request, 0x40, data, index, 8452af9545SBill Pemberton NULL, 0, 300); 8552af9545SBill Pemberton } 8652af9545SBill Pemberton 8752af9545SBill Pemberton static inline int ssu100_setdevice(struct usb_device *dev, u8 *data) 8852af9545SBill Pemberton { 8952af9545SBill Pemberton u16 x = ((u16)(data[1] << 8) | (u16)(data[0])); 9052af9545SBill Pemberton 9152af9545SBill Pemberton return ssu100_control_msg(dev, QT_SET_GET_DEVICE, x, 0); 9252af9545SBill Pemberton } 9352af9545SBill Pemberton 9452af9545SBill Pemberton 9552af9545SBill Pemberton static inline int ssu100_getdevice(struct usb_device *dev, u8 *data) 9652af9545SBill Pemberton { 9752af9545SBill Pemberton return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 9852af9545SBill Pemberton QT_SET_GET_DEVICE, 0xc0, 0, 0, 9952af9545SBill Pemberton data, 3, 300); 10052af9545SBill Pemberton } 10152af9545SBill Pemberton 10252af9545SBill Pemberton static inline int ssu100_getregister(struct usb_device *dev, 10352af9545SBill Pemberton unsigned short uart, 10452af9545SBill Pemberton unsigned short reg, 10552af9545SBill Pemberton u8 *data) 10652af9545SBill Pemberton { 10752af9545SBill Pemberton return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 10852af9545SBill Pemberton QT_SET_GET_REGISTER, 0xc0, reg, 10952af9545SBill Pemberton uart, data, sizeof(*data), 300); 11052af9545SBill Pemberton 11152af9545SBill Pemberton } 11252af9545SBill Pemberton 11352af9545SBill Pemberton 11452af9545SBill Pemberton static inline int ssu100_setregister(struct usb_device *dev, 11552af9545SBill Pemberton unsigned short uart, 116556f1a0eSBill Pemberton unsigned short reg, 11752af9545SBill Pemberton u16 data) 11852af9545SBill Pemberton { 119556f1a0eSBill Pemberton u16 value = (data << 8) | reg; 12052af9545SBill Pemberton 12152af9545SBill Pemberton return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 12252af9545SBill Pemberton QT_SET_GET_REGISTER, 0x40, value, uart, 12352af9545SBill Pemberton NULL, 0, 300); 12452af9545SBill Pemberton 12552af9545SBill Pemberton } 12652af9545SBill Pemberton 12752af9545SBill Pemberton #define set_mctrl(dev, set) update_mctrl((dev), (set), 0) 12852af9545SBill Pemberton #define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear)) 12952af9545SBill Pemberton 13052af9545SBill Pemberton /* these do not deal with device that have more than 1 port */ 13152af9545SBill Pemberton static inline int update_mctrl(struct usb_device *dev, unsigned int set, 13252af9545SBill Pemberton unsigned int clear) 13352af9545SBill Pemberton { 13452af9545SBill Pemberton unsigned urb_value; 13552af9545SBill Pemberton int result; 13652af9545SBill Pemberton 13752af9545SBill Pemberton if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) { 13852af9545SBill Pemberton dbg("%s - DTR|RTS not being set|cleared", __func__); 13952af9545SBill Pemberton return 0; /* no change */ 14052af9545SBill Pemberton } 14152af9545SBill Pemberton 14252af9545SBill Pemberton clear &= ~set; /* 'set' takes precedence over 'clear' */ 14352af9545SBill Pemberton urb_value = 0; 14452af9545SBill Pemberton if (set & TIOCM_DTR) 14579f203a2SBill Pemberton urb_value |= UART_MCR_DTR; 14652af9545SBill Pemberton if (set & TIOCM_RTS) 14779f203a2SBill Pemberton urb_value |= UART_MCR_RTS; 14852af9545SBill Pemberton 149556f1a0eSBill Pemberton result = ssu100_setregister(dev, 0, UART_MCR, urb_value); 15052af9545SBill Pemberton if (result < 0) 15152af9545SBill Pemberton dbg("%s Error from MODEM_CTRL urb", __func__); 15252af9545SBill Pemberton 15352af9545SBill Pemberton return result; 15452af9545SBill Pemberton } 15552af9545SBill Pemberton 15652af9545SBill Pemberton static int ssu100_initdevice(struct usb_device *dev) 15752af9545SBill Pemberton { 15852af9545SBill Pemberton u8 *data; 15952af9545SBill Pemberton int result = 0; 16052af9545SBill Pemberton 16152af9545SBill Pemberton data = kzalloc(3, GFP_KERNEL); 16252af9545SBill Pemberton if (!data) 16352af9545SBill Pemberton return -ENOMEM; 16452af9545SBill Pemberton 16552af9545SBill Pemberton result = ssu100_getdevice(dev, data); 16652af9545SBill Pemberton if (result < 0) { 16752af9545SBill Pemberton dbg("%s - get_device failed %i", __func__, result); 16852af9545SBill Pemberton goto out; 16952af9545SBill Pemberton } 17052af9545SBill Pemberton 17152af9545SBill Pemberton data[1] &= ~FULLPWRBIT; 17252af9545SBill Pemberton 17352af9545SBill Pemberton result = ssu100_setdevice(dev, data); 17452af9545SBill Pemberton if (result < 0) { 17552af9545SBill Pemberton dbg("%s - setdevice failed %i", __func__, result); 17652af9545SBill Pemberton goto out; 17752af9545SBill Pemberton } 17852af9545SBill Pemberton 17952af9545SBill Pemberton result = ssu100_control_msg(dev, QT_GET_SET_PREBUF_TRIG_LVL, 128, 0); 18052af9545SBill Pemberton if (result < 0) { 18152af9545SBill Pemberton dbg("%s - set prebuffer level failed %i", __func__, result); 18252af9545SBill Pemberton goto out; 18352af9545SBill Pemberton } 18452af9545SBill Pemberton 18552af9545SBill Pemberton result = ssu100_control_msg(dev, QT_SET_ATF, ATC_DISABLED, 0); 18652af9545SBill Pemberton if (result < 0) { 18752af9545SBill Pemberton dbg("%s - set ATFprebuffer level failed %i", __func__, result); 18852af9545SBill Pemberton goto out; 18952af9545SBill Pemberton } 19052af9545SBill Pemberton 19152af9545SBill Pemberton result = ssu100_getdevice(dev, data); 19252af9545SBill Pemberton if (result < 0) { 19352af9545SBill Pemberton dbg("%s - get_device failed %i", __func__, result); 19452af9545SBill Pemberton goto out; 19552af9545SBill Pemberton } 19652af9545SBill Pemberton 19752af9545SBill Pemberton data[0] &= ~(RR_BITS | DUPMODE_BITS); 19852af9545SBill Pemberton data[0] |= CLKS_X4; 19952af9545SBill Pemberton data[1] &= ~(LOOPMODE_BITS); 20052af9545SBill Pemberton data[1] |= RS232_MODE; 20152af9545SBill Pemberton 20252af9545SBill Pemberton result = ssu100_setdevice(dev, data); 20352af9545SBill Pemberton if (result < 0) { 20452af9545SBill Pemberton dbg("%s - setdevice failed %i", __func__, result); 20552af9545SBill Pemberton goto out; 20652af9545SBill Pemberton } 20752af9545SBill Pemberton 20852af9545SBill Pemberton out: kfree(data); 20952af9545SBill Pemberton return result; 21052af9545SBill Pemberton 21152af9545SBill Pemberton } 21252af9545SBill Pemberton 21352af9545SBill Pemberton 21452af9545SBill Pemberton static void ssu100_set_termios(struct tty_struct *tty, 21552af9545SBill Pemberton struct usb_serial_port *port, 21652af9545SBill Pemberton struct ktermios *old_termios) 21752af9545SBill Pemberton { 21852af9545SBill Pemberton struct usb_device *dev = port->serial->dev; 21952af9545SBill Pemberton struct ktermios *termios = tty->termios; 22052af9545SBill Pemberton u16 baud, divisor, remainder; 22152af9545SBill Pemberton unsigned int cflag = termios->c_cflag; 22252af9545SBill Pemberton u16 urb_value = 0; /* will hold the new flags */ 22352af9545SBill Pemberton int result; 22452af9545SBill Pemberton 22552af9545SBill Pemberton if (cflag & PARENB) { 22652af9545SBill Pemberton if (cflag & PARODD) 22779f203a2SBill Pemberton urb_value |= UART_LCR_PARITY; 22852af9545SBill Pemberton else 22952af9545SBill Pemberton urb_value |= SERIAL_EVEN_PARITY; 23052af9545SBill Pemberton } 23152af9545SBill Pemberton 23252af9545SBill Pemberton switch (cflag & CSIZE) { 23352af9545SBill Pemberton case CS5: 23479f203a2SBill Pemberton urb_value |= UART_LCR_WLEN5; 23552af9545SBill Pemberton break; 23652af9545SBill Pemberton case CS6: 23779f203a2SBill Pemberton urb_value |= UART_LCR_WLEN6; 23852af9545SBill Pemberton break; 23952af9545SBill Pemberton case CS7: 24079f203a2SBill Pemberton urb_value |= UART_LCR_WLEN7; 24152af9545SBill Pemberton break; 24252af9545SBill Pemberton default: 24352af9545SBill Pemberton case CS8: 24479f203a2SBill Pemberton urb_value |= UART_LCR_WLEN8; 24552af9545SBill Pemberton break; 24652af9545SBill Pemberton } 24752af9545SBill Pemberton 24852af9545SBill Pemberton baud = tty_get_baud_rate(tty); 24952af9545SBill Pemberton if (!baud) 25052af9545SBill Pemberton baud = 9600; 25152af9545SBill Pemberton 25252af9545SBill Pemberton dbg("%s - got baud = %d\n", __func__, baud); 25352af9545SBill Pemberton 25452af9545SBill Pemberton 25552af9545SBill Pemberton divisor = MAX_BAUD_RATE / baud; 25652af9545SBill Pemberton remainder = MAX_BAUD_RATE % baud; 25752af9545SBill Pemberton if (((remainder * 2) >= baud) && (baud != 110)) 25852af9545SBill Pemberton divisor++; 25952af9545SBill Pemberton 26052af9545SBill Pemberton urb_value = urb_value << 8; 26152af9545SBill Pemberton 26252af9545SBill Pemberton result = ssu100_control_msg(dev, QT_GET_SET_UART, divisor, urb_value); 26352af9545SBill Pemberton if (result < 0) 26452af9545SBill Pemberton dbg("%s - set uart failed", __func__); 26552af9545SBill Pemberton 26652af9545SBill Pemberton if (cflag & CRTSCTS) 26752af9545SBill Pemberton result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK, 26852af9545SBill Pemberton SERIAL_CRTSCTS, 0); 26952af9545SBill Pemberton else 27052af9545SBill Pemberton result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK, 27152af9545SBill Pemberton 0, 0); 27252af9545SBill Pemberton if (result < 0) 27352af9545SBill Pemberton dbg("%s - set HW flow control failed", __func__); 27452af9545SBill Pemberton 27552af9545SBill Pemberton if (I_IXOFF(tty) || I_IXON(tty)) { 27652af9545SBill Pemberton u16 x = ((u16)(START_CHAR(tty) << 8) | (u16)(STOP_CHAR(tty))); 27752af9545SBill Pemberton 27852af9545SBill Pemberton result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK, 27952af9545SBill Pemberton x, 0); 28052af9545SBill Pemberton } else 28152af9545SBill Pemberton result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK, 28252af9545SBill Pemberton 0, 0); 28352af9545SBill Pemberton 28452af9545SBill Pemberton if (result < 0) 28552af9545SBill Pemberton dbg("%s - set SW flow control failed", __func__); 28652af9545SBill Pemberton 28752af9545SBill Pemberton } 28852af9545SBill Pemberton 28952af9545SBill Pemberton 29052af9545SBill Pemberton static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port) 29152af9545SBill Pemberton { 29252af9545SBill Pemberton struct usb_device *dev = port->serial->dev; 29352af9545SBill Pemberton struct ssu100_port_private *priv = usb_get_serial_port_data(port); 29452af9545SBill Pemberton u8 *data; 29552af9545SBill Pemberton int result; 29617523058SBill Pemberton unsigned long flags; 29752af9545SBill Pemberton 29852af9545SBill Pemberton data = kzalloc(2, GFP_KERNEL); 29952af9545SBill Pemberton if (!data) 30052af9545SBill Pemberton return -ENOMEM; 30152af9545SBill Pemberton 30252af9545SBill Pemberton result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 30352af9545SBill Pemberton QT_OPEN_CLOSE_CHANNEL, 30452af9545SBill Pemberton QT_TRANSFER_IN, 0x01, 30552af9545SBill Pemberton 0, data, 2, 300); 30652af9545SBill Pemberton if (result < 0) { 30752af9545SBill Pemberton dbg("%s - open failed %i", __func__, result); 30852af9545SBill Pemberton kfree(data); 30952af9545SBill Pemberton return result; 31052af9545SBill Pemberton } 31152af9545SBill Pemberton 31217523058SBill Pemberton spin_lock_irqsave(&priv->status_lock, flags); 313f81c83dbSBill Pemberton priv->shadowLSR = data[0]; 314f81c83dbSBill Pemberton priv->shadowMSR = data[1]; 31517523058SBill Pemberton spin_unlock_irqrestore(&priv->status_lock, flags); 31652af9545SBill Pemberton 31752af9545SBill Pemberton kfree(data); 31852af9545SBill Pemberton 31952af9545SBill Pemberton /* set to 9600 */ 32052af9545SBill Pemberton result = ssu100_control_msg(dev, QT_GET_SET_UART, 0x30, 0x0300); 32152af9545SBill Pemberton if (result < 0) 32252af9545SBill Pemberton dbg("%s - set uart failed", __func__); 32352af9545SBill Pemberton 32452af9545SBill Pemberton if (tty) 32552af9545SBill Pemberton ssu100_set_termios(tty, port, tty->termios); 32652af9545SBill Pemberton 32752af9545SBill Pemberton return usb_serial_generic_open(tty, port); 32852af9545SBill Pemberton } 32952af9545SBill Pemberton 33052af9545SBill Pemberton static void ssu100_close(struct usb_serial_port *port) 33152af9545SBill Pemberton { 33252af9545SBill Pemberton usb_serial_generic_close(port); 33352af9545SBill Pemberton } 33452af9545SBill Pemberton 33552af9545SBill Pemberton static int get_serial_info(struct usb_serial_port *port, 33652af9545SBill Pemberton struct serial_struct __user *retinfo) 33752af9545SBill Pemberton { 33852af9545SBill Pemberton struct serial_struct tmp; 33952af9545SBill Pemberton 34052af9545SBill Pemberton if (!retinfo) 34152af9545SBill Pemberton return -EFAULT; 34252af9545SBill Pemberton 34352af9545SBill Pemberton memset(&tmp, 0, sizeof(tmp)); 34452af9545SBill Pemberton tmp.line = port->serial->minor; 34552af9545SBill Pemberton tmp.port = 0; 34652af9545SBill Pemberton tmp.irq = 0; 34752af9545SBill Pemberton tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; 34852af9545SBill Pemberton tmp.xmit_fifo_size = port->bulk_out_size; 34952af9545SBill Pemberton tmp.baud_base = 9600; 35052af9545SBill Pemberton tmp.close_delay = 5*HZ; 35152af9545SBill Pemberton tmp.closing_wait = 30*HZ; 35252af9545SBill Pemberton 35352af9545SBill Pemberton if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) 35452af9545SBill Pemberton return -EFAULT; 35552af9545SBill Pemberton return 0; 35652af9545SBill Pemberton } 35752af9545SBill Pemberton 358f81c83dbSBill Pemberton static int wait_modem_info(struct usb_serial_port *port, unsigned int arg) 359f81c83dbSBill Pemberton { 360f81c83dbSBill Pemberton struct ssu100_port_private *priv = usb_get_serial_port_data(port); 361f81c83dbSBill Pemberton struct async_icount prev, cur; 362f81c83dbSBill Pemberton unsigned long flags; 363f81c83dbSBill Pemberton 364f81c83dbSBill Pemberton spin_lock_irqsave(&priv->status_lock, flags); 365f81c83dbSBill Pemberton prev = priv->icount; 366f81c83dbSBill Pemberton spin_unlock_irqrestore(&priv->status_lock, flags); 367f81c83dbSBill Pemberton 368f81c83dbSBill Pemberton while (1) { 369f81c83dbSBill Pemberton wait_event_interruptible(priv->delta_msr_wait, 370f81c83dbSBill Pemberton ((priv->icount.rng != prev.rng) || 371f81c83dbSBill Pemberton (priv->icount.dsr != prev.dsr) || 372f81c83dbSBill Pemberton (priv->icount.dcd != prev.dcd) || 373f81c83dbSBill Pemberton (priv->icount.cts != prev.cts))); 374f81c83dbSBill Pemberton 375f81c83dbSBill Pemberton if (signal_pending(current)) 376f81c83dbSBill Pemberton return -ERESTARTSYS; 377f81c83dbSBill Pemberton 378f81c83dbSBill Pemberton spin_lock_irqsave(&priv->status_lock, flags); 379f81c83dbSBill Pemberton cur = priv->icount; 380f81c83dbSBill Pemberton spin_unlock_irqrestore(&priv->status_lock, flags); 381f81c83dbSBill Pemberton 382f81c83dbSBill Pemberton if ((prev.rng == cur.rng) && 383f81c83dbSBill Pemberton (prev.dsr == cur.dsr) && 384f81c83dbSBill Pemberton (prev.dcd == cur.dcd) && 385f81c83dbSBill Pemberton (prev.cts == cur.cts)) 386f81c83dbSBill Pemberton return -EIO; 387f81c83dbSBill Pemberton 388f81c83dbSBill Pemberton if ((arg & TIOCM_RNG && (prev.rng != cur.rng)) || 389f81c83dbSBill Pemberton (arg & TIOCM_DSR && (prev.dsr != cur.dsr)) || 390f81c83dbSBill Pemberton (arg & TIOCM_CD && (prev.dcd != cur.dcd)) || 391f81c83dbSBill Pemberton (arg & TIOCM_CTS && (prev.cts != cur.cts))) 392f81c83dbSBill Pemberton return 0; 393f81c83dbSBill Pemberton } 394f81c83dbSBill Pemberton return 0; 395f81c83dbSBill Pemberton } 396f81c83dbSBill Pemberton 3970bca1b91SAlan Cox static int ssu100_get_icount(struct tty_struct *tty, 3980bca1b91SAlan Cox struct serial_icounter_struct *icount) 3990bca1b91SAlan Cox { 4000bca1b91SAlan Cox struct usb_serial_port *port = tty->driver_data; 4010bca1b91SAlan Cox struct ssu100_port_private *priv = usb_get_serial_port_data(port); 4020bca1b91SAlan Cox struct async_icount cnow = priv->icount; 4030bca1b91SAlan Cox 4040bca1b91SAlan Cox icount->cts = cnow.cts; 4050bca1b91SAlan Cox icount->dsr = cnow.dsr; 4060bca1b91SAlan Cox icount->rng = cnow.rng; 4070bca1b91SAlan Cox icount->dcd = cnow.dcd; 4080bca1b91SAlan Cox icount->rx = cnow.rx; 4090bca1b91SAlan Cox icount->tx = cnow.tx; 4100bca1b91SAlan Cox icount->frame = cnow.frame; 4110bca1b91SAlan Cox icount->overrun = cnow.overrun; 4120bca1b91SAlan Cox icount->parity = cnow.parity; 4130bca1b91SAlan Cox icount->brk = cnow.brk; 4140bca1b91SAlan Cox icount->buf_overrun = cnow.buf_overrun; 4150bca1b91SAlan Cox 4160bca1b91SAlan Cox return 0; 4170bca1b91SAlan Cox } 4180bca1b91SAlan Cox 4190bca1b91SAlan Cox 4200bca1b91SAlan Cox 42100a0d0d6SAlan Cox static int ssu100_ioctl(struct tty_struct *tty, 42252af9545SBill Pemberton unsigned int cmd, unsigned long arg) 42352af9545SBill Pemberton { 42452af9545SBill Pemberton struct usb_serial_port *port = tty->driver_data; 42552af9545SBill Pemberton 42652af9545SBill Pemberton dbg("%s cmd 0x%04x", __func__, cmd); 42752af9545SBill Pemberton 42852af9545SBill Pemberton switch (cmd) { 42952af9545SBill Pemberton case TIOCGSERIAL: 43052af9545SBill Pemberton return get_serial_info(port, 43152af9545SBill Pemberton (struct serial_struct __user *) arg); 43252af9545SBill Pemberton 43352af9545SBill Pemberton case TIOCMIWAIT: 434f81c83dbSBill Pemberton return wait_modem_info(port, arg); 43552af9545SBill Pemberton 43652af9545SBill Pemberton default: 43752af9545SBill Pemberton break; 43852af9545SBill Pemberton } 43952af9545SBill Pemberton 44052af9545SBill Pemberton dbg("%s arg not supported", __func__); 44152af9545SBill Pemberton 44252af9545SBill Pemberton return -ENOIOCTLCMD; 44352af9545SBill Pemberton } 44452af9545SBill Pemberton 44552af9545SBill Pemberton static int ssu100_attach(struct usb_serial *serial) 44652af9545SBill Pemberton { 44752af9545SBill Pemberton struct ssu100_port_private *priv; 44852af9545SBill Pemberton struct usb_serial_port *port = *serial->port; 44952af9545SBill Pemberton 45052af9545SBill Pemberton priv = kzalloc(sizeof(*priv), GFP_KERNEL); 45152af9545SBill Pemberton if (!priv) { 45252af9545SBill Pemberton dev_err(&port->dev, "%s- kmalloc(%Zd) failed.\n", __func__, 45352af9545SBill Pemberton sizeof(*priv)); 45452af9545SBill Pemberton return -ENOMEM; 45552af9545SBill Pemberton } 45652af9545SBill Pemberton 45717523058SBill Pemberton spin_lock_init(&priv->status_lock); 45852af9545SBill Pemberton init_waitqueue_head(&priv->delta_msr_wait); 45952af9545SBill Pemberton usb_set_serial_port_data(port, priv); 46052af9545SBill Pemberton 46152af9545SBill Pemberton return ssu100_initdevice(serial->dev); 46252af9545SBill Pemberton } 46352af9545SBill Pemberton 46460b33c13SAlan Cox static int ssu100_tiocmget(struct tty_struct *tty) 46552af9545SBill Pemberton { 46652af9545SBill Pemberton struct usb_serial_port *port = tty->driver_data; 46752af9545SBill Pemberton struct usb_device *dev = port->serial->dev; 46852af9545SBill Pemberton u8 *d; 46952af9545SBill Pemberton int r; 47052af9545SBill Pemberton 47152af9545SBill Pemberton d = kzalloc(2, GFP_KERNEL); 47252af9545SBill Pemberton if (!d) 47352af9545SBill Pemberton return -ENOMEM; 47452af9545SBill Pemberton 47579f203a2SBill Pemberton r = ssu100_getregister(dev, 0, UART_MCR, d); 47652af9545SBill Pemberton if (r < 0) 47752af9545SBill Pemberton goto mget_out; 47852af9545SBill Pemberton 47979f203a2SBill Pemberton r = ssu100_getregister(dev, 0, UART_MSR, d+1); 48052af9545SBill Pemberton if (r < 0) 48152af9545SBill Pemberton goto mget_out; 48252af9545SBill Pemberton 48379f203a2SBill Pemberton r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) | 48479f203a2SBill Pemberton (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) | 48579f203a2SBill Pemberton (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) | 48679f203a2SBill Pemberton (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) | 48779f203a2SBill Pemberton (d[1] & UART_MSR_RI ? TIOCM_RI : 0) | 48879f203a2SBill Pemberton (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0); 48952af9545SBill Pemberton 49052af9545SBill Pemberton mget_out: 49152af9545SBill Pemberton kfree(d); 49252af9545SBill Pemberton return r; 49352af9545SBill Pemberton } 49452af9545SBill Pemberton 49520b9d177SAlan Cox static int ssu100_tiocmset(struct tty_struct *tty, 49652af9545SBill Pemberton unsigned int set, unsigned int clear) 49752af9545SBill Pemberton { 49852af9545SBill Pemberton struct usb_serial_port *port = tty->driver_data; 49952af9545SBill Pemberton struct usb_device *dev = port->serial->dev; 50052af9545SBill Pemberton 50152af9545SBill Pemberton return update_mctrl(dev, set, clear); 50252af9545SBill Pemberton } 50352af9545SBill Pemberton 50452af9545SBill Pemberton static void ssu100_dtr_rts(struct usb_serial_port *port, int on) 50552af9545SBill Pemberton { 50652af9545SBill Pemberton struct usb_device *dev = port->serial->dev; 50752af9545SBill Pemberton 50852af9545SBill Pemberton mutex_lock(&port->serial->disc_mutex); 50952af9545SBill Pemberton if (!port->serial->disconnected) { 51052af9545SBill Pemberton /* Disable flow control */ 51152af9545SBill Pemberton if (!on && 512556f1a0eSBill Pemberton ssu100_setregister(dev, 0, UART_MCR, 0) < 0) 51352af9545SBill Pemberton dev_err(&port->dev, "error from flowcontrol urb\n"); 51452af9545SBill Pemberton /* drop RTS and DTR */ 51552af9545SBill Pemberton if (on) 51652af9545SBill Pemberton set_mctrl(dev, TIOCM_DTR | TIOCM_RTS); 51752af9545SBill Pemberton else 51852af9545SBill Pemberton clear_mctrl(dev, TIOCM_DTR | TIOCM_RTS); 51952af9545SBill Pemberton } 52052af9545SBill Pemberton mutex_unlock(&port->serial->disc_mutex); 52152af9545SBill Pemberton } 52252af9545SBill Pemberton 523f81c83dbSBill Pemberton static void ssu100_update_msr(struct usb_serial_port *port, u8 msr) 524f81c83dbSBill Pemberton { 525f81c83dbSBill Pemberton struct ssu100_port_private *priv = usb_get_serial_port_data(port); 526f81c83dbSBill Pemberton unsigned long flags; 527f81c83dbSBill Pemberton 528f81c83dbSBill Pemberton spin_lock_irqsave(&priv->status_lock, flags); 529f81c83dbSBill Pemberton priv->shadowMSR = msr; 530f81c83dbSBill Pemberton spin_unlock_irqrestore(&priv->status_lock, flags); 531f81c83dbSBill Pemberton 532f81c83dbSBill Pemberton if (msr & UART_MSR_ANY_DELTA) { 533f81c83dbSBill Pemberton /* update input line counters */ 534f81c83dbSBill Pemberton if (msr & UART_MSR_DCTS) 535f81c83dbSBill Pemberton priv->icount.cts++; 536f81c83dbSBill Pemberton if (msr & UART_MSR_DDSR) 537f81c83dbSBill Pemberton priv->icount.dsr++; 538f81c83dbSBill Pemberton if (msr & UART_MSR_DDCD) 539f81c83dbSBill Pemberton priv->icount.dcd++; 540f81c83dbSBill Pemberton if (msr & UART_MSR_TERI) 541f81c83dbSBill Pemberton priv->icount.rng++; 542f81c83dbSBill Pemberton wake_up_interruptible(&priv->delta_msr_wait); 543f81c83dbSBill Pemberton } 544f81c83dbSBill Pemberton } 545f81c83dbSBill Pemberton 5466b8f1ca5SBill Pemberton static void ssu100_update_lsr(struct usb_serial_port *port, u8 lsr, 5476b8f1ca5SBill Pemberton char *tty_flag) 548f81c83dbSBill Pemberton { 549f81c83dbSBill Pemberton struct ssu100_port_private *priv = usb_get_serial_port_data(port); 550f81c83dbSBill Pemberton unsigned long flags; 551f81c83dbSBill Pemberton 552f81c83dbSBill Pemberton spin_lock_irqsave(&priv->status_lock, flags); 553f81c83dbSBill Pemberton priv->shadowLSR = lsr; 554f81c83dbSBill Pemberton spin_unlock_irqrestore(&priv->status_lock, flags); 555f81c83dbSBill Pemberton 5566b8f1ca5SBill Pemberton *tty_flag = TTY_NORMAL; 557f81c83dbSBill Pemberton if (lsr & UART_LSR_BRK_ERROR_BITS) { 5586b8f1ca5SBill Pemberton /* we always want to update icount, but we only want to 5596b8f1ca5SBill Pemberton * update tty_flag for one case */ 5606b8f1ca5SBill Pemberton if (lsr & UART_LSR_BI) { 561f81c83dbSBill Pemberton priv->icount.brk++; 5626b8f1ca5SBill Pemberton *tty_flag = TTY_BREAK; 5636b8f1ca5SBill Pemberton usb_serial_handle_break(port); 564f81c83dbSBill Pemberton } 5656b8f1ca5SBill Pemberton if (lsr & UART_LSR_PE) { 5666b8f1ca5SBill Pemberton priv->icount.parity++; 5676b8f1ca5SBill Pemberton if (*tty_flag == TTY_NORMAL) 5686b8f1ca5SBill Pemberton *tty_flag = TTY_PARITY; 5696b8f1ca5SBill Pemberton } 5706b8f1ca5SBill Pemberton if (lsr & UART_LSR_FE) { 5716b8f1ca5SBill Pemberton priv->icount.frame++; 5726b8f1ca5SBill Pemberton if (*tty_flag == TTY_NORMAL) 5736b8f1ca5SBill Pemberton *tty_flag = TTY_FRAME; 5746b8f1ca5SBill Pemberton } 5756b8f1ca5SBill Pemberton if (lsr & UART_LSR_OE){ 5766b8f1ca5SBill Pemberton priv->icount.overrun++; 5776b8f1ca5SBill Pemberton if (*tty_flag == TTY_NORMAL) 5786b8f1ca5SBill Pemberton *tty_flag = TTY_OVERRUN; 5796b8f1ca5SBill Pemberton } 5806b8f1ca5SBill Pemberton } 5816b8f1ca5SBill Pemberton 582f81c83dbSBill Pemberton } 583f81c83dbSBill Pemberton 584f7043ecbSBill Pemberton static int ssu100_process_packet(struct urb *urb, 585f7043ecbSBill Pemberton struct tty_struct *tty) 58652af9545SBill Pemberton { 587f7043ecbSBill Pemberton struct usb_serial_port *port = urb->context; 588f7043ecbSBill Pemberton char *packet = (char *)urb->transfer_buffer; 5896b8f1ca5SBill Pemberton char flag = TTY_NORMAL; 590f7043ecbSBill Pemberton u32 len = urb->actual_length; 591f7043ecbSBill Pemberton int i; 59252af9545SBill Pemberton char *ch; 59352af9545SBill Pemberton 5949b2cef31SBill Pemberton if ((len >= 4) && 5959b2cef31SBill Pemberton (packet[0] == 0x1b) && (packet[1] == 0x1b) && 59652af9545SBill Pemberton ((packet[2] == 0x00) || (packet[2] == 0x01))) { 5976b8f1ca5SBill Pemberton if (packet[2] == 0x00) { 5986b8f1ca5SBill Pemberton ssu100_update_lsr(port, packet[3], &flag); 5996b8f1ca5SBill Pemberton if (flag == TTY_OVERRUN) 6006b8f1ca5SBill Pemberton tty_insert_flip_char(tty, 0, TTY_OVERRUN); 6016b8f1ca5SBill Pemberton } 602f81c83dbSBill Pemberton if (packet[2] == 0x01) 603f81c83dbSBill Pemberton ssu100_update_msr(port, packet[3]); 60452af9545SBill Pemberton 60552af9545SBill Pemberton len -= 4; 60652af9545SBill Pemberton ch = packet + 4; 60752af9545SBill Pemberton } else 60852af9545SBill Pemberton ch = packet; 60952af9545SBill Pemberton 61052af9545SBill Pemberton if (!len) 61152af9545SBill Pemberton return 0; /* status only */ 61252af9545SBill Pemberton 61352af9545SBill Pemberton if (port->port.console && port->sysrq) { 61452af9545SBill Pemberton for (i = 0; i < len; i++, ch++) { 6156ee9f4b4SDmitry Torokhov if (!usb_serial_handle_sysrq_char(port, *ch)) 61652af9545SBill Pemberton tty_insert_flip_char(tty, *ch, flag); 61752af9545SBill Pemberton } 61852af9545SBill Pemberton } else 61952af9545SBill Pemberton tty_insert_flip_string_fixed_flag(tty, ch, flag, len); 62052af9545SBill Pemberton 62152af9545SBill Pemberton return len; 62252af9545SBill Pemberton } 62352af9545SBill Pemberton 62452af9545SBill Pemberton static void ssu100_process_read_urb(struct urb *urb) 62552af9545SBill Pemberton { 62652af9545SBill Pemberton struct usb_serial_port *port = urb->context; 62752af9545SBill Pemberton struct tty_struct *tty; 628f7043ecbSBill Pemberton int count; 62952af9545SBill Pemberton 63052af9545SBill Pemberton tty = tty_port_tty_get(&port->port); 63152af9545SBill Pemberton if (!tty) 63252af9545SBill Pemberton return; 63352af9545SBill Pemberton 634f7043ecbSBill Pemberton count = ssu100_process_packet(urb, tty); 63552af9545SBill Pemberton 63652af9545SBill Pemberton if (count) 63752af9545SBill Pemberton tty_flip_buffer_push(tty); 63852af9545SBill Pemberton tty_kref_put(tty); 63952af9545SBill Pemberton } 64052af9545SBill Pemberton 64152af9545SBill Pemberton static struct usb_serial_driver ssu100_device = { 64252af9545SBill Pemberton .driver = { 64352af9545SBill Pemberton .owner = THIS_MODULE, 64452af9545SBill Pemberton .name = "ssu100", 64552af9545SBill Pemberton }, 64652af9545SBill Pemberton .description = DRIVER_DESC, 64752af9545SBill Pemberton .id_table = id_table, 64852af9545SBill Pemberton .num_ports = 1, 64952af9545SBill Pemberton .open = ssu100_open, 65052af9545SBill Pemberton .close = ssu100_close, 65152af9545SBill Pemberton .attach = ssu100_attach, 65252af9545SBill Pemberton .release = ssu100_release, 65352af9545SBill Pemberton .dtr_rts = ssu100_dtr_rts, 65452af9545SBill Pemberton .process_read_urb = ssu100_process_read_urb, 65552af9545SBill Pemberton .tiocmget = ssu100_tiocmget, 65652af9545SBill Pemberton .tiocmset = ssu100_tiocmset, 6570bca1b91SAlan Cox .get_icount = ssu100_get_icount, 65852af9545SBill Pemberton .ioctl = ssu100_ioctl, 65952af9545SBill Pemberton .set_termios = ssu100_set_termios, 66085dee135SBill Pemberton .disconnect = usb_serial_generic_disconnect, 66152af9545SBill Pemberton }; 66252af9545SBill Pemberton 663d860322fSAlan Stern static struct usb_serial_driver * const serial_drivers[] = { 664d860322fSAlan Stern &ssu100_device, NULL 665d860322fSAlan Stern }; 666d860322fSAlan Stern 667*68e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table); 66852af9545SBill Pemberton 66952af9545SBill Pemberton MODULE_DESCRIPTION(DRIVER_DESC); 67052af9545SBill Pemberton MODULE_LICENSE("GPL"); 67152af9545SBill Pemberton 67252af9545SBill Pemberton module_param(debug, bool, S_IRUGO | S_IWUSR); 67352af9545SBill Pemberton MODULE_PARM_DESC(debug, "Debug enabled or not"); 674