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/slab.h> 1052af9545SBill Pemberton #include <linux/tty.h> 1152af9545SBill Pemberton #include <linux/tty_driver.h> 1252af9545SBill Pemberton #include <linux/tty_flip.h> 1352af9545SBill Pemberton #include <linux/module.h> 1452af9545SBill Pemberton #include <linux/serial.h> 1552af9545SBill Pemberton #include <linux/usb.h> 1652af9545SBill Pemberton #include <linux/usb/serial.h> 1779f203a2SBill Pemberton #include <linux/serial_reg.h> 1852af9545SBill Pemberton #include <linux/uaccess.h> 1952af9545SBill Pemberton 2052af9545SBill Pemberton #define QT_OPEN_CLOSE_CHANNEL 0xca 2152af9545SBill Pemberton #define QT_SET_GET_DEVICE 0xc2 2252af9545SBill Pemberton #define QT_SET_GET_REGISTER 0xc0 2352af9545SBill Pemberton #define QT_GET_SET_PREBUF_TRIG_LVL 0xcc 2452af9545SBill Pemberton #define QT_SET_ATF 0xcd 2552af9545SBill Pemberton #define QT_GET_SET_UART 0xc1 2652af9545SBill Pemberton #define QT_TRANSFER_IN 0xc0 2752af9545SBill Pemberton #define QT_HW_FLOW_CONTROL_MASK 0xc5 2852af9545SBill Pemberton #define QT_SW_FLOW_CONTROL_MASK 0xc6 2952af9545SBill Pemberton 3052af9545SBill Pemberton #define SERIAL_MSR_MASK 0xf0 3152af9545SBill Pemberton 3279f203a2SBill Pemberton #define SERIAL_CRTSCTS ((UART_MCR_RTS << 8) | UART_MSR_CTS) 3352af9545SBill Pemberton 3479f203a2SBill Pemberton #define SERIAL_EVEN_PARITY (UART_LCR_PARITY | UART_LCR_EPAR) 3552af9545SBill Pemberton 3652af9545SBill Pemberton #define MAX_BAUD_RATE 460800 3752af9545SBill Pemberton 3852af9545SBill Pemberton #define ATC_DISABLED 0x00 3952af9545SBill Pemberton #define DUPMODE_BITS 0xc0 4052af9545SBill Pemberton #define RR_BITS 0x03 4152af9545SBill Pemberton #define LOOPMODE_BITS 0x41 4252af9545SBill Pemberton #define RS232_MODE 0x00 4352af9545SBill Pemberton #define RTSCTS_TO_CONNECTOR 0x40 4452af9545SBill Pemberton #define CLKS_X4 0x02 4552af9545SBill Pemberton #define FULLPWRBIT 0x00000080 4652af9545SBill Pemberton #define NEXT_BOARD_POWER_BIT 0x00000004 4752af9545SBill Pemberton 4852af9545SBill Pemberton #define DRIVER_DESC "Quatech SSU-100 USB to Serial Driver" 4952af9545SBill Pemberton 5052af9545SBill Pemberton #define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */ 5152af9545SBill Pemberton #define QUATECH_SSU100 0xC020 /* SSU100 */ 5252af9545SBill Pemberton 5352af9545SBill Pemberton static const struct usb_device_id id_table[] = { 5452af9545SBill Pemberton {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU100)}, 5552af9545SBill Pemberton {} /* Terminating entry */ 5652af9545SBill Pemberton }; 5752af9545SBill Pemberton MODULE_DEVICE_TABLE(usb, id_table); 5852af9545SBill Pemberton 5952af9545SBill Pemberton struct ssu100_port_private { 6017523058SBill Pemberton spinlock_t status_lock; 6152af9545SBill Pemberton u8 shadowLSR; 6252af9545SBill Pemberton u8 shadowMSR; 6352af9545SBill Pemberton }; 6452af9545SBill Pemberton 6552af9545SBill Pemberton static inline int ssu100_control_msg(struct usb_device *dev, 6652af9545SBill Pemberton u8 request, u16 data, u16 index) 6752af9545SBill Pemberton { 6852af9545SBill Pemberton return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 6952af9545SBill Pemberton request, 0x40, data, index, 7052af9545SBill Pemberton NULL, 0, 300); 7152af9545SBill Pemberton } 7252af9545SBill Pemberton 7352af9545SBill Pemberton static inline int ssu100_setdevice(struct usb_device *dev, u8 *data) 7452af9545SBill Pemberton { 7552af9545SBill Pemberton u16 x = ((u16)(data[1] << 8) | (u16)(data[0])); 7652af9545SBill Pemberton 7752af9545SBill Pemberton return ssu100_control_msg(dev, QT_SET_GET_DEVICE, x, 0); 7852af9545SBill Pemberton } 7952af9545SBill Pemberton 8052af9545SBill Pemberton 8152af9545SBill Pemberton static inline int ssu100_getdevice(struct usb_device *dev, u8 *data) 8252af9545SBill Pemberton { 83*1eac5c24SJohan Hovold int ret; 84*1eac5c24SJohan Hovold 85*1eac5c24SJohan Hovold ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 8652af9545SBill Pemberton QT_SET_GET_DEVICE, 0xc0, 0, 0, 8752af9545SBill Pemberton data, 3, 300); 88*1eac5c24SJohan Hovold if (ret < 3) { 89*1eac5c24SJohan Hovold if (ret >= 0) 90*1eac5c24SJohan Hovold ret = -EIO; 91*1eac5c24SJohan Hovold } 92*1eac5c24SJohan Hovold 93*1eac5c24SJohan Hovold return ret; 9452af9545SBill Pemberton } 9552af9545SBill Pemberton 9652af9545SBill Pemberton static inline int ssu100_getregister(struct usb_device *dev, 9752af9545SBill Pemberton unsigned short uart, 9852af9545SBill Pemberton unsigned short reg, 9952af9545SBill Pemberton u8 *data) 10052af9545SBill Pemberton { 101*1eac5c24SJohan Hovold int ret; 102*1eac5c24SJohan Hovold 103*1eac5c24SJohan Hovold ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 10452af9545SBill Pemberton QT_SET_GET_REGISTER, 0xc0, reg, 10552af9545SBill Pemberton uart, data, sizeof(*data), 300); 106*1eac5c24SJohan Hovold if (ret < sizeof(*data)) { 107*1eac5c24SJohan Hovold if (ret >= 0) 108*1eac5c24SJohan Hovold ret = -EIO; 109*1eac5c24SJohan Hovold } 11052af9545SBill Pemberton 111*1eac5c24SJohan Hovold return ret; 11252af9545SBill Pemberton } 11352af9545SBill Pemberton 11452af9545SBill Pemberton 11552af9545SBill Pemberton static inline int ssu100_setregister(struct usb_device *dev, 11652af9545SBill Pemberton unsigned short uart, 117556f1a0eSBill Pemberton unsigned short reg, 11852af9545SBill Pemberton u16 data) 11952af9545SBill Pemberton { 120556f1a0eSBill Pemberton u16 value = (data << 8) | reg; 12152af9545SBill Pemberton 12252af9545SBill Pemberton return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 12352af9545SBill Pemberton QT_SET_GET_REGISTER, 0x40, value, uart, 12452af9545SBill Pemberton NULL, 0, 300); 12552af9545SBill Pemberton 12652af9545SBill Pemberton } 12752af9545SBill Pemberton 12852af9545SBill Pemberton #define set_mctrl(dev, set) update_mctrl((dev), (set), 0) 12952af9545SBill Pemberton #define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear)) 13052af9545SBill Pemberton 13152af9545SBill Pemberton /* these do not deal with device that have more than 1 port */ 13252af9545SBill Pemberton static inline int update_mctrl(struct usb_device *dev, unsigned int set, 13352af9545SBill Pemberton unsigned int clear) 13452af9545SBill Pemberton { 13552af9545SBill Pemberton unsigned urb_value; 13652af9545SBill Pemberton int result; 13752af9545SBill Pemberton 13852af9545SBill Pemberton if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) { 1394f0c6412SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - DTR|RTS not being set|cleared\n", __func__); 14052af9545SBill Pemberton return 0; /* no change */ 14152af9545SBill Pemberton } 14252af9545SBill Pemberton 14352af9545SBill Pemberton clear &= ~set; /* 'set' takes precedence over 'clear' */ 14452af9545SBill Pemberton urb_value = 0; 14552af9545SBill Pemberton if (set & TIOCM_DTR) 14679f203a2SBill Pemberton urb_value |= UART_MCR_DTR; 14752af9545SBill Pemberton if (set & TIOCM_RTS) 14879f203a2SBill Pemberton urb_value |= UART_MCR_RTS; 14952af9545SBill Pemberton 150556f1a0eSBill Pemberton result = ssu100_setregister(dev, 0, UART_MCR, urb_value); 15152af9545SBill Pemberton if (result < 0) 1524f0c6412SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s Error from MODEM_CTRL urb\n", __func__); 15352af9545SBill Pemberton 15452af9545SBill Pemberton return result; 15552af9545SBill Pemberton } 15652af9545SBill Pemberton 15752af9545SBill Pemberton static int ssu100_initdevice(struct usb_device *dev) 15852af9545SBill Pemberton { 15952af9545SBill Pemberton u8 *data; 16052af9545SBill Pemberton int result = 0; 16152af9545SBill Pemberton 16252af9545SBill Pemberton data = kzalloc(3, GFP_KERNEL); 16352af9545SBill Pemberton if (!data) 16452af9545SBill Pemberton return -ENOMEM; 16552af9545SBill Pemberton 16652af9545SBill Pemberton result = ssu100_getdevice(dev, data); 16752af9545SBill Pemberton if (result < 0) { 1684f0c6412SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result); 16952af9545SBill Pemberton goto out; 17052af9545SBill Pemberton } 17152af9545SBill Pemberton 17252af9545SBill Pemberton data[1] &= ~FULLPWRBIT; 17352af9545SBill Pemberton 17452af9545SBill Pemberton result = ssu100_setdevice(dev, data); 17552af9545SBill Pemberton if (result < 0) { 1764f0c6412SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result); 17752af9545SBill Pemberton goto out; 17852af9545SBill Pemberton } 17952af9545SBill Pemberton 18052af9545SBill Pemberton result = ssu100_control_msg(dev, QT_GET_SET_PREBUF_TRIG_LVL, 128, 0); 18152af9545SBill Pemberton if (result < 0) { 1824f0c6412SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - set prebuffer level failed %i\n", __func__, result); 18352af9545SBill Pemberton goto out; 18452af9545SBill Pemberton } 18552af9545SBill Pemberton 18652af9545SBill Pemberton result = ssu100_control_msg(dev, QT_SET_ATF, ATC_DISABLED, 0); 18752af9545SBill Pemberton if (result < 0) { 1884f0c6412SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - set ATFprebuffer level failed %i\n", __func__, result); 18952af9545SBill Pemberton goto out; 19052af9545SBill Pemberton } 19152af9545SBill Pemberton 19252af9545SBill Pemberton result = ssu100_getdevice(dev, data); 19352af9545SBill Pemberton if (result < 0) { 1944f0c6412SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result); 19552af9545SBill Pemberton goto out; 19652af9545SBill Pemberton } 19752af9545SBill Pemberton 19852af9545SBill Pemberton data[0] &= ~(RR_BITS | DUPMODE_BITS); 19952af9545SBill Pemberton data[0] |= CLKS_X4; 20052af9545SBill Pemberton data[1] &= ~(LOOPMODE_BITS); 20152af9545SBill Pemberton data[1] |= RS232_MODE; 20252af9545SBill Pemberton 20352af9545SBill Pemberton result = ssu100_setdevice(dev, data); 20452af9545SBill Pemberton if (result < 0) { 2054f0c6412SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result); 20652af9545SBill Pemberton goto out; 20752af9545SBill Pemberton } 20852af9545SBill Pemberton 20952af9545SBill Pemberton out: kfree(data); 21052af9545SBill Pemberton return result; 21152af9545SBill Pemberton 21252af9545SBill Pemberton } 21352af9545SBill Pemberton 21452af9545SBill Pemberton 21552af9545SBill Pemberton static void ssu100_set_termios(struct tty_struct *tty, 21652af9545SBill Pemberton struct usb_serial_port *port, 21752af9545SBill Pemberton struct ktermios *old_termios) 21852af9545SBill Pemberton { 21952af9545SBill Pemberton struct usb_device *dev = port->serial->dev; 220adc8d746SAlan Cox struct ktermios *termios = &tty->termios; 22152af9545SBill Pemberton u16 baud, divisor, remainder; 22252af9545SBill Pemberton unsigned int cflag = termios->c_cflag; 22352af9545SBill Pemberton u16 urb_value = 0; /* will hold the new flags */ 22452af9545SBill Pemberton int result; 22552af9545SBill Pemberton 22652af9545SBill Pemberton if (cflag & PARENB) { 22752af9545SBill Pemberton if (cflag & PARODD) 22879f203a2SBill Pemberton urb_value |= UART_LCR_PARITY; 22952af9545SBill Pemberton else 23052af9545SBill Pemberton urb_value |= SERIAL_EVEN_PARITY; 23152af9545SBill Pemberton } 23252af9545SBill Pemberton 23352af9545SBill Pemberton switch (cflag & CSIZE) { 23452af9545SBill Pemberton case CS5: 23579f203a2SBill Pemberton urb_value |= UART_LCR_WLEN5; 23652af9545SBill Pemberton break; 23752af9545SBill Pemberton case CS6: 23879f203a2SBill Pemberton urb_value |= UART_LCR_WLEN6; 23952af9545SBill Pemberton break; 24052af9545SBill Pemberton case CS7: 24179f203a2SBill Pemberton urb_value |= UART_LCR_WLEN7; 24252af9545SBill Pemberton break; 24352af9545SBill Pemberton default: 24452af9545SBill Pemberton case CS8: 24579f203a2SBill Pemberton urb_value |= UART_LCR_WLEN8; 24652af9545SBill Pemberton break; 24752af9545SBill Pemberton } 24852af9545SBill Pemberton 24952af9545SBill Pemberton baud = tty_get_baud_rate(tty); 25052af9545SBill Pemberton if (!baud) 25152af9545SBill Pemberton baud = 9600; 25252af9545SBill Pemberton 2534f0c6412SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - got baud = %d\n", __func__, baud); 25452af9545SBill Pemberton 25552af9545SBill Pemberton 25652af9545SBill Pemberton divisor = MAX_BAUD_RATE / baud; 25752af9545SBill Pemberton remainder = MAX_BAUD_RATE % baud; 25852af9545SBill Pemberton if (((remainder * 2) >= baud) && (baud != 110)) 25952af9545SBill Pemberton divisor++; 26052af9545SBill Pemberton 26152af9545SBill Pemberton urb_value = urb_value << 8; 26252af9545SBill Pemberton 26352af9545SBill Pemberton result = ssu100_control_msg(dev, QT_GET_SET_UART, divisor, urb_value); 26452af9545SBill Pemberton if (result < 0) 2654f0c6412SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - set uart failed\n", __func__); 26652af9545SBill Pemberton 26752af9545SBill Pemberton if (cflag & CRTSCTS) 26852af9545SBill Pemberton result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK, 26952af9545SBill Pemberton SERIAL_CRTSCTS, 0); 27052af9545SBill Pemberton else 27152af9545SBill Pemberton result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK, 27252af9545SBill Pemberton 0, 0); 27352af9545SBill Pemberton if (result < 0) 2744f0c6412SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - set HW flow control failed\n", __func__); 27552af9545SBill Pemberton 27652af9545SBill Pemberton if (I_IXOFF(tty) || I_IXON(tty)) { 27752af9545SBill Pemberton u16 x = ((u16)(START_CHAR(tty) << 8) | (u16)(STOP_CHAR(tty))); 27852af9545SBill Pemberton 27952af9545SBill Pemberton result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK, 28052af9545SBill Pemberton x, 0); 28152af9545SBill Pemberton } else 28252af9545SBill Pemberton result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK, 28352af9545SBill Pemberton 0, 0); 28452af9545SBill Pemberton 28552af9545SBill Pemberton if (result < 0) 2864f0c6412SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - set SW flow control failed\n", __func__); 28752af9545SBill Pemberton 28852af9545SBill Pemberton } 28952af9545SBill Pemberton 29052af9545SBill Pemberton 29152af9545SBill Pemberton static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port) 29252af9545SBill Pemberton { 29352af9545SBill Pemberton struct usb_device *dev = port->serial->dev; 29452af9545SBill Pemberton struct ssu100_port_private *priv = usb_get_serial_port_data(port); 29552af9545SBill Pemberton u8 *data; 29652af9545SBill Pemberton int result; 29717523058SBill Pemberton unsigned long flags; 29852af9545SBill Pemberton 29952af9545SBill Pemberton data = kzalloc(2, GFP_KERNEL); 30052af9545SBill Pemberton if (!data) 30152af9545SBill Pemberton return -ENOMEM; 30252af9545SBill Pemberton 30352af9545SBill Pemberton result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 30452af9545SBill Pemberton QT_OPEN_CLOSE_CHANNEL, 30552af9545SBill Pemberton QT_TRANSFER_IN, 0x01, 30652af9545SBill Pemberton 0, data, 2, 300); 307*1eac5c24SJohan Hovold if (result < 2) { 3084f0c6412SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - open failed %i\n", __func__, result); 309*1eac5c24SJohan Hovold if (result >= 0) 310*1eac5c24SJohan Hovold result = -EIO; 31152af9545SBill Pemberton kfree(data); 31252af9545SBill Pemberton return result; 31352af9545SBill Pemberton } 31452af9545SBill Pemberton 31517523058SBill Pemberton spin_lock_irqsave(&priv->status_lock, flags); 316f81c83dbSBill Pemberton priv->shadowLSR = data[0]; 317f81c83dbSBill Pemberton priv->shadowMSR = data[1]; 31817523058SBill Pemberton spin_unlock_irqrestore(&priv->status_lock, flags); 31952af9545SBill Pemberton 32052af9545SBill Pemberton kfree(data); 32152af9545SBill Pemberton 32252af9545SBill Pemberton /* set to 9600 */ 32352af9545SBill Pemberton result = ssu100_control_msg(dev, QT_GET_SET_UART, 0x30, 0x0300); 32452af9545SBill Pemberton if (result < 0) 3254f0c6412SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - set uart failed\n", __func__); 32652af9545SBill Pemberton 32752af9545SBill Pemberton if (tty) 328adc8d746SAlan Cox ssu100_set_termios(tty, port, &tty->termios); 32952af9545SBill Pemberton 33052af9545SBill Pemberton return usb_serial_generic_open(tty, port); 33152af9545SBill Pemberton } 33252af9545SBill Pemberton 33352af9545SBill Pemberton static int get_serial_info(struct usb_serial_port *port, 33452af9545SBill Pemberton struct serial_struct __user *retinfo) 33552af9545SBill Pemberton { 33652af9545SBill Pemberton struct serial_struct tmp; 33752af9545SBill Pemberton 33852af9545SBill Pemberton memset(&tmp, 0, sizeof(tmp)); 339e5b1e206SGreg Kroah-Hartman tmp.line = port->minor; 34052af9545SBill Pemberton tmp.port = 0; 34152af9545SBill Pemberton tmp.irq = 0; 34252af9545SBill Pemberton tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; 34352af9545SBill Pemberton tmp.xmit_fifo_size = port->bulk_out_size; 34452af9545SBill Pemberton tmp.baud_base = 9600; 34552af9545SBill Pemberton tmp.close_delay = 5*HZ; 34652af9545SBill Pemberton tmp.closing_wait = 30*HZ; 34752af9545SBill Pemberton 34852af9545SBill Pemberton if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) 34952af9545SBill Pemberton return -EFAULT; 35052af9545SBill Pemberton return 0; 35152af9545SBill Pemberton } 35252af9545SBill Pemberton 35300a0d0d6SAlan Cox static int ssu100_ioctl(struct tty_struct *tty, 35452af9545SBill Pemberton unsigned int cmd, unsigned long arg) 35552af9545SBill Pemberton { 35652af9545SBill Pemberton struct usb_serial_port *port = tty->driver_data; 35752af9545SBill Pemberton 35852af9545SBill Pemberton switch (cmd) { 35952af9545SBill Pemberton case TIOCGSERIAL: 36052af9545SBill Pemberton return get_serial_info(port, 36152af9545SBill Pemberton (struct serial_struct __user *) arg); 36252af9545SBill Pemberton default: 36352af9545SBill Pemberton break; 36452af9545SBill Pemberton } 36552af9545SBill Pemberton 36652af9545SBill Pemberton return -ENOIOCTLCMD; 36752af9545SBill Pemberton } 36852af9545SBill Pemberton 36952af9545SBill Pemberton static int ssu100_attach(struct usb_serial *serial) 37052af9545SBill Pemberton { 371638b9e15SJohan Hovold return ssu100_initdevice(serial->dev); 372638b9e15SJohan Hovold } 373638b9e15SJohan Hovold 374638b9e15SJohan Hovold static int ssu100_port_probe(struct usb_serial_port *port) 375638b9e15SJohan Hovold { 37652af9545SBill Pemberton struct ssu100_port_private *priv; 37752af9545SBill Pemberton 37852af9545SBill Pemberton priv = kzalloc(sizeof(*priv), GFP_KERNEL); 379638b9e15SJohan Hovold if (!priv) 38052af9545SBill Pemberton return -ENOMEM; 38152af9545SBill Pemberton 38217523058SBill Pemberton spin_lock_init(&priv->status_lock); 383638b9e15SJohan Hovold 38452af9545SBill Pemberton usb_set_serial_port_data(port, priv); 38552af9545SBill Pemberton 386638b9e15SJohan Hovold return 0; 387638b9e15SJohan Hovold } 388638b9e15SJohan Hovold 389638b9e15SJohan Hovold static int ssu100_port_remove(struct usb_serial_port *port) 390638b9e15SJohan Hovold { 391638b9e15SJohan Hovold struct ssu100_port_private *priv; 392638b9e15SJohan Hovold 393638b9e15SJohan Hovold priv = usb_get_serial_port_data(port); 394638b9e15SJohan Hovold kfree(priv); 395638b9e15SJohan Hovold 396638b9e15SJohan Hovold return 0; 39752af9545SBill Pemberton } 39852af9545SBill Pemberton 39960b33c13SAlan Cox static int ssu100_tiocmget(struct tty_struct *tty) 40052af9545SBill Pemberton { 40152af9545SBill Pemberton struct usb_serial_port *port = tty->driver_data; 40252af9545SBill Pemberton struct usb_device *dev = port->serial->dev; 40352af9545SBill Pemberton u8 *d; 40452af9545SBill Pemberton int r; 40552af9545SBill Pemberton 40652af9545SBill Pemberton d = kzalloc(2, GFP_KERNEL); 40752af9545SBill Pemberton if (!d) 40852af9545SBill Pemberton return -ENOMEM; 40952af9545SBill Pemberton 41079f203a2SBill Pemberton r = ssu100_getregister(dev, 0, UART_MCR, d); 41152af9545SBill Pemberton if (r < 0) 41252af9545SBill Pemberton goto mget_out; 41352af9545SBill Pemberton 41479f203a2SBill Pemberton r = ssu100_getregister(dev, 0, UART_MSR, d+1); 41552af9545SBill Pemberton if (r < 0) 41652af9545SBill Pemberton goto mget_out; 41752af9545SBill Pemberton 41879f203a2SBill Pemberton r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) | 41979f203a2SBill Pemberton (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) | 42079f203a2SBill Pemberton (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) | 42179f203a2SBill Pemberton (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) | 42279f203a2SBill Pemberton (d[1] & UART_MSR_RI ? TIOCM_RI : 0) | 42379f203a2SBill Pemberton (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0); 42452af9545SBill Pemberton 42552af9545SBill Pemberton mget_out: 42652af9545SBill Pemberton kfree(d); 42752af9545SBill Pemberton return r; 42852af9545SBill Pemberton } 42952af9545SBill Pemberton 43020b9d177SAlan Cox static int ssu100_tiocmset(struct tty_struct *tty, 43152af9545SBill Pemberton unsigned int set, unsigned int clear) 43252af9545SBill Pemberton { 43352af9545SBill Pemberton struct usb_serial_port *port = tty->driver_data; 43452af9545SBill Pemberton struct usb_device *dev = port->serial->dev; 43552af9545SBill Pemberton 43652af9545SBill Pemberton return update_mctrl(dev, set, clear); 43752af9545SBill Pemberton } 43852af9545SBill Pemberton 43952af9545SBill Pemberton static void ssu100_dtr_rts(struct usb_serial_port *port, int on) 44052af9545SBill Pemberton { 44152af9545SBill Pemberton struct usb_device *dev = port->serial->dev; 44252af9545SBill Pemberton 44352af9545SBill Pemberton /* Disable flow control */ 444b2ca6990SJohan Hovold if (!on) { 445b2ca6990SJohan Hovold if (ssu100_setregister(dev, 0, UART_MCR, 0) < 0) 44652af9545SBill Pemberton dev_err(&port->dev, "error from flowcontrol urb\n"); 447b2ca6990SJohan Hovold } 44852af9545SBill Pemberton /* drop RTS and DTR */ 44952af9545SBill Pemberton if (on) 45052af9545SBill Pemberton set_mctrl(dev, TIOCM_DTR | TIOCM_RTS); 45152af9545SBill Pemberton else 45252af9545SBill Pemberton clear_mctrl(dev, TIOCM_DTR | TIOCM_RTS); 45352af9545SBill Pemberton } 45452af9545SBill Pemberton 455f81c83dbSBill Pemberton static void ssu100_update_msr(struct usb_serial_port *port, u8 msr) 456f81c83dbSBill Pemberton { 457f81c83dbSBill Pemberton struct ssu100_port_private *priv = usb_get_serial_port_data(port); 458f81c83dbSBill Pemberton unsigned long flags; 459f81c83dbSBill Pemberton 460f81c83dbSBill Pemberton spin_lock_irqsave(&priv->status_lock, flags); 461f81c83dbSBill Pemberton priv->shadowMSR = msr; 462f81c83dbSBill Pemberton spin_unlock_irqrestore(&priv->status_lock, flags); 463f81c83dbSBill Pemberton 464f81c83dbSBill Pemberton if (msr & UART_MSR_ANY_DELTA) { 465f81c83dbSBill Pemberton /* update input line counters */ 466f81c83dbSBill Pemberton if (msr & UART_MSR_DCTS) 46731ecdb6bSJohan Hovold port->icount.cts++; 468f81c83dbSBill Pemberton if (msr & UART_MSR_DDSR) 46931ecdb6bSJohan Hovold port->icount.dsr++; 470f81c83dbSBill Pemberton if (msr & UART_MSR_DDCD) 47131ecdb6bSJohan Hovold port->icount.dcd++; 472f81c83dbSBill Pemberton if (msr & UART_MSR_TERI) 47331ecdb6bSJohan Hovold port->icount.rng++; 474c24c838eSJohan Hovold wake_up_interruptible(&port->port.delta_msr_wait); 475f81c83dbSBill Pemberton } 476f81c83dbSBill Pemberton } 477f81c83dbSBill Pemberton 4786b8f1ca5SBill Pemberton static void ssu100_update_lsr(struct usb_serial_port *port, u8 lsr, 4796b8f1ca5SBill Pemberton char *tty_flag) 480f81c83dbSBill Pemberton { 481f81c83dbSBill Pemberton struct ssu100_port_private *priv = usb_get_serial_port_data(port); 482f81c83dbSBill Pemberton unsigned long flags; 483f81c83dbSBill Pemberton 484f81c83dbSBill Pemberton spin_lock_irqsave(&priv->status_lock, flags); 485f81c83dbSBill Pemberton priv->shadowLSR = lsr; 486f81c83dbSBill Pemberton spin_unlock_irqrestore(&priv->status_lock, flags); 487f81c83dbSBill Pemberton 4886b8f1ca5SBill Pemberton *tty_flag = TTY_NORMAL; 489f81c83dbSBill Pemberton if (lsr & UART_LSR_BRK_ERROR_BITS) { 4906b8f1ca5SBill Pemberton /* we always want to update icount, but we only want to 4916b8f1ca5SBill Pemberton * update tty_flag for one case */ 4926b8f1ca5SBill Pemberton if (lsr & UART_LSR_BI) { 49331ecdb6bSJohan Hovold port->icount.brk++; 4946b8f1ca5SBill Pemberton *tty_flag = TTY_BREAK; 4956b8f1ca5SBill Pemberton usb_serial_handle_break(port); 496f81c83dbSBill Pemberton } 4976b8f1ca5SBill Pemberton if (lsr & UART_LSR_PE) { 49831ecdb6bSJohan Hovold port->icount.parity++; 4996b8f1ca5SBill Pemberton if (*tty_flag == TTY_NORMAL) 5006b8f1ca5SBill Pemberton *tty_flag = TTY_PARITY; 5016b8f1ca5SBill Pemberton } 5026b8f1ca5SBill Pemberton if (lsr & UART_LSR_FE) { 50331ecdb6bSJohan Hovold port->icount.frame++; 5046b8f1ca5SBill Pemberton if (*tty_flag == TTY_NORMAL) 5056b8f1ca5SBill Pemberton *tty_flag = TTY_FRAME; 5066b8f1ca5SBill Pemberton } 5076b8f1ca5SBill Pemberton if (lsr & UART_LSR_OE) { 50831ecdb6bSJohan Hovold port->icount.overrun++; 50975bcbf29SJohan Hovold tty_insert_flip_char(&port->port, 0, TTY_OVERRUN); 5106b8f1ca5SBill Pemberton } 5116b8f1ca5SBill Pemberton } 5126b8f1ca5SBill Pemberton 513f81c83dbSBill Pemberton } 514f81c83dbSBill Pemberton 5152e124b4aSJiri Slaby static void ssu100_process_read_urb(struct urb *urb) 51652af9545SBill Pemberton { 517f7043ecbSBill Pemberton struct usb_serial_port *port = urb->context; 518f7043ecbSBill Pemberton char *packet = (char *)urb->transfer_buffer; 5196b8f1ca5SBill Pemberton char flag = TTY_NORMAL; 520f7043ecbSBill Pemberton u32 len = urb->actual_length; 521f7043ecbSBill Pemberton int i; 52252af9545SBill Pemberton char *ch; 52352af9545SBill Pemberton 5249b2cef31SBill Pemberton if ((len >= 4) && 5259b2cef31SBill Pemberton (packet[0] == 0x1b) && (packet[1] == 0x1b) && 52652af9545SBill Pemberton ((packet[2] == 0x00) || (packet[2] == 0x01))) { 52775bcbf29SJohan Hovold if (packet[2] == 0x00) 5286b8f1ca5SBill Pemberton ssu100_update_lsr(port, packet[3], &flag); 529f81c83dbSBill Pemberton if (packet[2] == 0x01) 530f81c83dbSBill Pemberton ssu100_update_msr(port, packet[3]); 53152af9545SBill Pemberton 53252af9545SBill Pemberton len -= 4; 53352af9545SBill Pemberton ch = packet + 4; 53452af9545SBill Pemberton } else 53552af9545SBill Pemberton ch = packet; 53652af9545SBill Pemberton 53752af9545SBill Pemberton if (!len) 5382e124b4aSJiri Slaby return; /* status only */ 53952af9545SBill Pemberton 54052af9545SBill Pemberton if (port->port.console && port->sysrq) { 54152af9545SBill Pemberton for (i = 0; i < len; i++, ch++) { 5426ee9f4b4SDmitry Torokhov if (!usb_serial_handle_sysrq_char(port, *ch)) 54392a19f9cSJiri Slaby tty_insert_flip_char(&port->port, *ch, flag); 54452af9545SBill Pemberton } 54552af9545SBill Pemberton } else 5462f693357SJiri Slaby tty_insert_flip_string_fixed_flag(&port->port, ch, flag, len); 54752af9545SBill Pemberton 5482e124b4aSJiri Slaby tty_flip_buffer_push(&port->port); 54952af9545SBill Pemberton } 55052af9545SBill Pemberton 55152af9545SBill Pemberton static struct usb_serial_driver ssu100_device = { 55252af9545SBill Pemberton .driver = { 55352af9545SBill Pemberton .owner = THIS_MODULE, 55452af9545SBill Pemberton .name = "ssu100", 55552af9545SBill Pemberton }, 55652af9545SBill Pemberton .description = DRIVER_DESC, 55752af9545SBill Pemberton .id_table = id_table, 55852af9545SBill Pemberton .num_ports = 1, 55952af9545SBill Pemberton .open = ssu100_open, 56052af9545SBill Pemberton .attach = ssu100_attach, 561638b9e15SJohan Hovold .port_probe = ssu100_port_probe, 562638b9e15SJohan Hovold .port_remove = ssu100_port_remove, 56352af9545SBill Pemberton .dtr_rts = ssu100_dtr_rts, 56452af9545SBill Pemberton .process_read_urb = ssu100_process_read_urb, 56552af9545SBill Pemberton .tiocmget = ssu100_tiocmget, 56652af9545SBill Pemberton .tiocmset = ssu100_tiocmset, 567c24c838eSJohan Hovold .tiocmiwait = usb_serial_generic_tiocmiwait, 56831ecdb6bSJohan Hovold .get_icount = usb_serial_generic_get_icount, 56952af9545SBill Pemberton .ioctl = ssu100_ioctl, 57052af9545SBill Pemberton .set_termios = ssu100_set_termios, 57152af9545SBill Pemberton }; 57252af9545SBill Pemberton 573d860322fSAlan Stern static struct usb_serial_driver * const serial_drivers[] = { 574d860322fSAlan Stern &ssu100_device, NULL 575d860322fSAlan Stern }; 576d860322fSAlan Stern 57768e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table); 57852af9545SBill Pemberton 57952af9545SBill Pemberton MODULE_DESCRIPTION(DRIVER_DESC); 58052af9545SBill Pemberton MODULE_LICENSE("GPL"); 581