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 4952af9545SBill Pemberton #define DRIVER_DESC "Quatech SSU-100 USB to Serial Driver" 5052af9545SBill Pemberton 5152af9545SBill Pemberton #define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */ 5252af9545SBill Pemberton #define QUATECH_SSU100 0xC020 /* SSU100 */ 5352af9545SBill Pemberton 5452af9545SBill Pemberton static const struct usb_device_id id_table[] = { 5552af9545SBill Pemberton {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU100)}, 5652af9545SBill Pemberton {} /* Terminating entry */ 5752af9545SBill Pemberton }; 5852af9545SBill Pemberton MODULE_DEVICE_TABLE(usb, id_table); 5952af9545SBill Pemberton 6052af9545SBill Pemberton struct ssu100_port_private { 6117523058SBill Pemberton spinlock_t status_lock; 6252af9545SBill Pemberton u8 shadowLSR; 6352af9545SBill Pemberton u8 shadowMSR; 6452af9545SBill Pemberton wait_queue_head_t delta_msr_wait; /* Used for TIOCMIWAIT */ 65f81c83dbSBill Pemberton struct async_icount icount; 6652af9545SBill Pemberton }; 6752af9545SBill Pemberton 6852af9545SBill Pemberton static inline int ssu100_control_msg(struct usb_device *dev, 6952af9545SBill Pemberton u8 request, u16 data, u16 index) 7052af9545SBill Pemberton { 7152af9545SBill Pemberton return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 7252af9545SBill Pemberton request, 0x40, data, index, 7352af9545SBill Pemberton NULL, 0, 300); 7452af9545SBill Pemberton } 7552af9545SBill Pemberton 7652af9545SBill Pemberton static inline int ssu100_setdevice(struct usb_device *dev, u8 *data) 7752af9545SBill Pemberton { 7852af9545SBill Pemberton u16 x = ((u16)(data[1] << 8) | (u16)(data[0])); 7952af9545SBill Pemberton 8052af9545SBill Pemberton return ssu100_control_msg(dev, QT_SET_GET_DEVICE, x, 0); 8152af9545SBill Pemberton } 8252af9545SBill Pemberton 8352af9545SBill Pemberton 8452af9545SBill Pemberton static inline int ssu100_getdevice(struct usb_device *dev, u8 *data) 8552af9545SBill Pemberton { 8652af9545SBill Pemberton return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 8752af9545SBill Pemberton QT_SET_GET_DEVICE, 0xc0, 0, 0, 8852af9545SBill Pemberton data, 3, 300); 8952af9545SBill Pemberton } 9052af9545SBill Pemberton 9152af9545SBill Pemberton static inline int ssu100_getregister(struct usb_device *dev, 9252af9545SBill Pemberton unsigned short uart, 9352af9545SBill Pemberton unsigned short reg, 9452af9545SBill Pemberton u8 *data) 9552af9545SBill Pemberton { 9652af9545SBill Pemberton return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 9752af9545SBill Pemberton QT_SET_GET_REGISTER, 0xc0, reg, 9852af9545SBill Pemberton uart, data, sizeof(*data), 300); 9952af9545SBill Pemberton 10052af9545SBill Pemberton } 10152af9545SBill Pemberton 10252af9545SBill Pemberton 10352af9545SBill Pemberton static inline int ssu100_setregister(struct usb_device *dev, 10452af9545SBill Pemberton unsigned short uart, 105556f1a0eSBill Pemberton unsigned short reg, 10652af9545SBill Pemberton u16 data) 10752af9545SBill Pemberton { 108556f1a0eSBill Pemberton u16 value = (data << 8) | reg; 10952af9545SBill Pemberton 11052af9545SBill Pemberton return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 11152af9545SBill Pemberton QT_SET_GET_REGISTER, 0x40, value, uart, 11252af9545SBill Pemberton NULL, 0, 300); 11352af9545SBill Pemberton 11452af9545SBill Pemberton } 11552af9545SBill Pemberton 11652af9545SBill Pemberton #define set_mctrl(dev, set) update_mctrl((dev), (set), 0) 11752af9545SBill Pemberton #define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear)) 11852af9545SBill Pemberton 11952af9545SBill Pemberton /* these do not deal with device that have more than 1 port */ 12052af9545SBill Pemberton static inline int update_mctrl(struct usb_device *dev, unsigned int set, 12152af9545SBill Pemberton unsigned int clear) 12252af9545SBill Pemberton { 12352af9545SBill Pemberton unsigned urb_value; 12452af9545SBill Pemberton int result; 12552af9545SBill Pemberton 12652af9545SBill Pemberton if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) { 1274f0c6412SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - DTR|RTS not being set|cleared\n", __func__); 12852af9545SBill Pemberton return 0; /* no change */ 12952af9545SBill Pemberton } 13052af9545SBill Pemberton 13152af9545SBill Pemberton clear &= ~set; /* 'set' takes precedence over 'clear' */ 13252af9545SBill Pemberton urb_value = 0; 13352af9545SBill Pemberton if (set & TIOCM_DTR) 13479f203a2SBill Pemberton urb_value |= UART_MCR_DTR; 13552af9545SBill Pemberton if (set & TIOCM_RTS) 13679f203a2SBill Pemberton urb_value |= UART_MCR_RTS; 13752af9545SBill Pemberton 138556f1a0eSBill Pemberton result = ssu100_setregister(dev, 0, UART_MCR, urb_value); 13952af9545SBill Pemberton if (result < 0) 1404f0c6412SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s Error from MODEM_CTRL urb\n", __func__); 14152af9545SBill Pemberton 14252af9545SBill Pemberton return result; 14352af9545SBill Pemberton } 14452af9545SBill Pemberton 14552af9545SBill Pemberton static int ssu100_initdevice(struct usb_device *dev) 14652af9545SBill Pemberton { 14752af9545SBill Pemberton u8 *data; 14852af9545SBill Pemberton int result = 0; 14952af9545SBill Pemberton 15052af9545SBill Pemberton data = kzalloc(3, GFP_KERNEL); 15152af9545SBill Pemberton if (!data) 15252af9545SBill Pemberton return -ENOMEM; 15352af9545SBill Pemberton 15452af9545SBill Pemberton result = ssu100_getdevice(dev, data); 15552af9545SBill Pemberton if (result < 0) { 1564f0c6412SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result); 15752af9545SBill Pemberton goto out; 15852af9545SBill Pemberton } 15952af9545SBill Pemberton 16052af9545SBill Pemberton data[1] &= ~FULLPWRBIT; 16152af9545SBill Pemberton 16252af9545SBill Pemberton result = ssu100_setdevice(dev, data); 16352af9545SBill Pemberton if (result < 0) { 1644f0c6412SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result); 16552af9545SBill Pemberton goto out; 16652af9545SBill Pemberton } 16752af9545SBill Pemberton 16852af9545SBill Pemberton result = ssu100_control_msg(dev, QT_GET_SET_PREBUF_TRIG_LVL, 128, 0); 16952af9545SBill Pemberton if (result < 0) { 1704f0c6412SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - set prebuffer level failed %i\n", __func__, result); 17152af9545SBill Pemberton goto out; 17252af9545SBill Pemberton } 17352af9545SBill Pemberton 17452af9545SBill Pemberton result = ssu100_control_msg(dev, QT_SET_ATF, ATC_DISABLED, 0); 17552af9545SBill Pemberton if (result < 0) { 1764f0c6412SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - set ATFprebuffer level failed %i\n", __func__, result); 17752af9545SBill Pemberton goto out; 17852af9545SBill Pemberton } 17952af9545SBill Pemberton 18052af9545SBill Pemberton result = ssu100_getdevice(dev, data); 18152af9545SBill Pemberton if (result < 0) { 1824f0c6412SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result); 18352af9545SBill Pemberton goto out; 18452af9545SBill Pemberton } 18552af9545SBill Pemberton 18652af9545SBill Pemberton data[0] &= ~(RR_BITS | DUPMODE_BITS); 18752af9545SBill Pemberton data[0] |= CLKS_X4; 18852af9545SBill Pemberton data[1] &= ~(LOOPMODE_BITS); 18952af9545SBill Pemberton data[1] |= RS232_MODE; 19052af9545SBill Pemberton 19152af9545SBill Pemberton result = ssu100_setdevice(dev, data); 19252af9545SBill Pemberton if (result < 0) { 1934f0c6412SGreg Kroah-Hartman dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result); 19452af9545SBill Pemberton goto out; 19552af9545SBill Pemberton } 19652af9545SBill Pemberton 19752af9545SBill Pemberton out: kfree(data); 19852af9545SBill Pemberton return result; 19952af9545SBill Pemberton 20052af9545SBill Pemberton } 20152af9545SBill Pemberton 20252af9545SBill Pemberton 20352af9545SBill Pemberton static void ssu100_set_termios(struct tty_struct *tty, 20452af9545SBill Pemberton struct usb_serial_port *port, 20552af9545SBill Pemberton struct ktermios *old_termios) 20652af9545SBill Pemberton { 20752af9545SBill Pemberton struct usb_device *dev = port->serial->dev; 208adc8d746SAlan Cox struct ktermios *termios = &tty->termios; 20952af9545SBill Pemberton u16 baud, divisor, remainder; 21052af9545SBill Pemberton unsigned int cflag = termios->c_cflag; 21152af9545SBill Pemberton u16 urb_value = 0; /* will hold the new flags */ 21252af9545SBill Pemberton int result; 21352af9545SBill Pemberton 21452af9545SBill Pemberton if (cflag & PARENB) { 21552af9545SBill Pemberton if (cflag & PARODD) 21679f203a2SBill Pemberton urb_value |= UART_LCR_PARITY; 21752af9545SBill Pemberton else 21852af9545SBill Pemberton urb_value |= SERIAL_EVEN_PARITY; 21952af9545SBill Pemberton } 22052af9545SBill Pemberton 22152af9545SBill Pemberton switch (cflag & CSIZE) { 22252af9545SBill Pemberton case CS5: 22379f203a2SBill Pemberton urb_value |= UART_LCR_WLEN5; 22452af9545SBill Pemberton break; 22552af9545SBill Pemberton case CS6: 22679f203a2SBill Pemberton urb_value |= UART_LCR_WLEN6; 22752af9545SBill Pemberton break; 22852af9545SBill Pemberton case CS7: 22979f203a2SBill Pemberton urb_value |= UART_LCR_WLEN7; 23052af9545SBill Pemberton break; 23152af9545SBill Pemberton default: 23252af9545SBill Pemberton case CS8: 23379f203a2SBill Pemberton urb_value |= UART_LCR_WLEN8; 23452af9545SBill Pemberton break; 23552af9545SBill Pemberton } 23652af9545SBill Pemberton 23752af9545SBill Pemberton baud = tty_get_baud_rate(tty); 23852af9545SBill Pemberton if (!baud) 23952af9545SBill Pemberton baud = 9600; 24052af9545SBill Pemberton 2414f0c6412SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - got baud = %d\n", __func__, baud); 24252af9545SBill Pemberton 24352af9545SBill Pemberton 24452af9545SBill Pemberton divisor = MAX_BAUD_RATE / baud; 24552af9545SBill Pemberton remainder = MAX_BAUD_RATE % baud; 24652af9545SBill Pemberton if (((remainder * 2) >= baud) && (baud != 110)) 24752af9545SBill Pemberton divisor++; 24852af9545SBill Pemberton 24952af9545SBill Pemberton urb_value = urb_value << 8; 25052af9545SBill Pemberton 25152af9545SBill Pemberton result = ssu100_control_msg(dev, QT_GET_SET_UART, divisor, urb_value); 25252af9545SBill Pemberton if (result < 0) 2534f0c6412SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - set uart failed\n", __func__); 25452af9545SBill Pemberton 25552af9545SBill Pemberton if (cflag & CRTSCTS) 25652af9545SBill Pemberton result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK, 25752af9545SBill Pemberton SERIAL_CRTSCTS, 0); 25852af9545SBill Pemberton else 25952af9545SBill Pemberton result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK, 26052af9545SBill Pemberton 0, 0); 26152af9545SBill Pemberton if (result < 0) 2624f0c6412SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - set HW flow control failed\n", __func__); 26352af9545SBill Pemberton 26452af9545SBill Pemberton if (I_IXOFF(tty) || I_IXON(tty)) { 26552af9545SBill Pemberton u16 x = ((u16)(START_CHAR(tty) << 8) | (u16)(STOP_CHAR(tty))); 26652af9545SBill Pemberton 26752af9545SBill Pemberton result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK, 26852af9545SBill Pemberton x, 0); 26952af9545SBill Pemberton } else 27052af9545SBill Pemberton result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK, 27152af9545SBill Pemberton 0, 0); 27252af9545SBill Pemberton 27352af9545SBill Pemberton if (result < 0) 2744f0c6412SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - set SW flow control failed\n", __func__); 27552af9545SBill Pemberton 27652af9545SBill Pemberton } 27752af9545SBill Pemberton 27852af9545SBill Pemberton 27952af9545SBill Pemberton static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port) 28052af9545SBill Pemberton { 28152af9545SBill Pemberton struct usb_device *dev = port->serial->dev; 28252af9545SBill Pemberton struct ssu100_port_private *priv = usb_get_serial_port_data(port); 28352af9545SBill Pemberton u8 *data; 28452af9545SBill Pemberton int result; 28517523058SBill Pemberton unsigned long flags; 28652af9545SBill Pemberton 28752af9545SBill Pemberton data = kzalloc(2, GFP_KERNEL); 28852af9545SBill Pemberton if (!data) 28952af9545SBill Pemberton return -ENOMEM; 29052af9545SBill Pemberton 29152af9545SBill Pemberton result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 29252af9545SBill Pemberton QT_OPEN_CLOSE_CHANNEL, 29352af9545SBill Pemberton QT_TRANSFER_IN, 0x01, 29452af9545SBill Pemberton 0, data, 2, 300); 29552af9545SBill Pemberton if (result < 0) { 2964f0c6412SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - open failed %i\n", __func__, result); 29752af9545SBill Pemberton kfree(data); 29852af9545SBill Pemberton return result; 29952af9545SBill Pemberton } 30052af9545SBill Pemberton 30117523058SBill Pemberton spin_lock_irqsave(&priv->status_lock, flags); 302f81c83dbSBill Pemberton priv->shadowLSR = data[0]; 303f81c83dbSBill Pemberton priv->shadowMSR = data[1]; 30417523058SBill Pemberton spin_unlock_irqrestore(&priv->status_lock, flags); 30552af9545SBill Pemberton 30652af9545SBill Pemberton kfree(data); 30752af9545SBill Pemberton 30852af9545SBill Pemberton /* set to 9600 */ 30952af9545SBill Pemberton result = ssu100_control_msg(dev, QT_GET_SET_UART, 0x30, 0x0300); 31052af9545SBill Pemberton if (result < 0) 3114f0c6412SGreg Kroah-Hartman dev_dbg(&port->dev, "%s - set uart failed\n", __func__); 31252af9545SBill Pemberton 31352af9545SBill Pemberton if (tty) 314adc8d746SAlan Cox ssu100_set_termios(tty, port, &tty->termios); 31552af9545SBill Pemberton 31652af9545SBill Pemberton return usb_serial_generic_open(tty, port); 31752af9545SBill Pemberton } 31852af9545SBill Pemberton 31952af9545SBill Pemberton static void ssu100_close(struct usb_serial_port *port) 32052af9545SBill Pemberton { 32152af9545SBill Pemberton usb_serial_generic_close(port); 32252af9545SBill Pemberton } 32352af9545SBill Pemberton 32452af9545SBill Pemberton static int get_serial_info(struct usb_serial_port *port, 32552af9545SBill Pemberton struct serial_struct __user *retinfo) 32652af9545SBill Pemberton { 32752af9545SBill Pemberton struct serial_struct tmp; 32852af9545SBill Pemberton 32952af9545SBill Pemberton if (!retinfo) 33052af9545SBill Pemberton return -EFAULT; 33152af9545SBill Pemberton 33252af9545SBill Pemberton memset(&tmp, 0, sizeof(tmp)); 33352af9545SBill Pemberton tmp.line = port->serial->minor; 33452af9545SBill Pemberton tmp.port = 0; 33552af9545SBill Pemberton tmp.irq = 0; 33652af9545SBill Pemberton tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; 33752af9545SBill Pemberton tmp.xmit_fifo_size = port->bulk_out_size; 33852af9545SBill Pemberton tmp.baud_base = 9600; 33952af9545SBill Pemberton tmp.close_delay = 5*HZ; 34052af9545SBill Pemberton tmp.closing_wait = 30*HZ; 34152af9545SBill Pemberton 34252af9545SBill Pemberton if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) 34352af9545SBill Pemberton return -EFAULT; 34452af9545SBill Pemberton return 0; 34552af9545SBill Pemberton } 34652af9545SBill Pemberton 347f81c83dbSBill Pemberton static int wait_modem_info(struct usb_serial_port *port, unsigned int arg) 348f81c83dbSBill Pemberton { 349f81c83dbSBill Pemberton struct ssu100_port_private *priv = usb_get_serial_port_data(port); 350f81c83dbSBill Pemberton struct async_icount prev, cur; 351f81c83dbSBill Pemberton unsigned long flags; 352f81c83dbSBill Pemberton 353f81c83dbSBill Pemberton spin_lock_irqsave(&priv->status_lock, flags); 354f81c83dbSBill Pemberton prev = priv->icount; 355f81c83dbSBill Pemberton spin_unlock_irqrestore(&priv->status_lock, flags); 356f81c83dbSBill Pemberton 357f81c83dbSBill Pemberton while (1) { 358f81c83dbSBill Pemberton wait_event_interruptible(priv->delta_msr_wait, 359f81c83dbSBill Pemberton ((priv->icount.rng != prev.rng) || 360f81c83dbSBill Pemberton (priv->icount.dsr != prev.dsr) || 361f81c83dbSBill Pemberton (priv->icount.dcd != prev.dcd) || 362f81c83dbSBill Pemberton (priv->icount.cts != prev.cts))); 363f81c83dbSBill Pemberton 364f81c83dbSBill Pemberton if (signal_pending(current)) 365f81c83dbSBill Pemberton return -ERESTARTSYS; 366f81c83dbSBill Pemberton 367f81c83dbSBill Pemberton spin_lock_irqsave(&priv->status_lock, flags); 368f81c83dbSBill Pemberton cur = priv->icount; 369f81c83dbSBill Pemberton spin_unlock_irqrestore(&priv->status_lock, flags); 370f81c83dbSBill Pemberton 371f81c83dbSBill Pemberton if ((prev.rng == cur.rng) && 372f81c83dbSBill Pemberton (prev.dsr == cur.dsr) && 373f81c83dbSBill Pemberton (prev.dcd == cur.dcd) && 374f81c83dbSBill Pemberton (prev.cts == cur.cts)) 375f81c83dbSBill Pemberton return -EIO; 376f81c83dbSBill Pemberton 377f81c83dbSBill Pemberton if ((arg & TIOCM_RNG && (prev.rng != cur.rng)) || 378f81c83dbSBill Pemberton (arg & TIOCM_DSR && (prev.dsr != cur.dsr)) || 379f81c83dbSBill Pemberton (arg & TIOCM_CD && (prev.dcd != cur.dcd)) || 380f81c83dbSBill Pemberton (arg & TIOCM_CTS && (prev.cts != cur.cts))) 381f81c83dbSBill Pemberton return 0; 382f81c83dbSBill Pemberton } 383f81c83dbSBill Pemberton return 0; 384f81c83dbSBill Pemberton } 385f81c83dbSBill Pemberton 3860bca1b91SAlan Cox static int ssu100_get_icount(struct tty_struct *tty, 3870bca1b91SAlan Cox struct serial_icounter_struct *icount) 3880bca1b91SAlan Cox { 3890bca1b91SAlan Cox struct usb_serial_port *port = tty->driver_data; 3900bca1b91SAlan Cox struct ssu100_port_private *priv = usb_get_serial_port_data(port); 3910bca1b91SAlan Cox struct async_icount cnow = priv->icount; 3920bca1b91SAlan Cox 3930bca1b91SAlan Cox icount->cts = cnow.cts; 3940bca1b91SAlan Cox icount->dsr = cnow.dsr; 3950bca1b91SAlan Cox icount->rng = cnow.rng; 3960bca1b91SAlan Cox icount->dcd = cnow.dcd; 3970bca1b91SAlan Cox icount->rx = cnow.rx; 3980bca1b91SAlan Cox icount->tx = cnow.tx; 3990bca1b91SAlan Cox icount->frame = cnow.frame; 4000bca1b91SAlan Cox icount->overrun = cnow.overrun; 4010bca1b91SAlan Cox icount->parity = cnow.parity; 4020bca1b91SAlan Cox icount->brk = cnow.brk; 4030bca1b91SAlan Cox icount->buf_overrun = cnow.buf_overrun; 4040bca1b91SAlan Cox 4050bca1b91SAlan Cox return 0; 4060bca1b91SAlan Cox } 4070bca1b91SAlan Cox 4080bca1b91SAlan Cox 4090bca1b91SAlan Cox 41000a0d0d6SAlan Cox static int ssu100_ioctl(struct tty_struct *tty, 41152af9545SBill Pemberton unsigned int cmd, unsigned long arg) 41252af9545SBill Pemberton { 41352af9545SBill Pemberton struct usb_serial_port *port = tty->driver_data; 41452af9545SBill Pemberton 4154f0c6412SGreg Kroah-Hartman dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd); 41652af9545SBill Pemberton 41752af9545SBill Pemberton switch (cmd) { 41852af9545SBill Pemberton case TIOCGSERIAL: 41952af9545SBill Pemberton return get_serial_info(port, 42052af9545SBill Pemberton (struct serial_struct __user *) arg); 42152af9545SBill Pemberton 42252af9545SBill Pemberton case TIOCMIWAIT: 423f81c83dbSBill Pemberton return wait_modem_info(port, arg); 42452af9545SBill Pemberton 42552af9545SBill Pemberton default: 42652af9545SBill Pemberton break; 42752af9545SBill Pemberton } 42852af9545SBill Pemberton 4294f0c6412SGreg Kroah-Hartman dev_dbg(&port->dev, "%s arg not supported\n", __func__); 43052af9545SBill Pemberton 43152af9545SBill Pemberton return -ENOIOCTLCMD; 43252af9545SBill Pemberton } 43352af9545SBill Pemberton 43452af9545SBill Pemberton static int ssu100_attach(struct usb_serial *serial) 43552af9545SBill Pemberton { 436638b9e15SJohan Hovold return ssu100_initdevice(serial->dev); 437638b9e15SJohan Hovold } 438638b9e15SJohan Hovold 439638b9e15SJohan Hovold static int ssu100_port_probe(struct usb_serial_port *port) 440638b9e15SJohan Hovold { 44152af9545SBill Pemberton struct ssu100_port_private *priv; 44252af9545SBill Pemberton 44352af9545SBill Pemberton priv = kzalloc(sizeof(*priv), GFP_KERNEL); 444638b9e15SJohan Hovold if (!priv) 44552af9545SBill Pemberton return -ENOMEM; 44652af9545SBill Pemberton 44717523058SBill Pemberton spin_lock_init(&priv->status_lock); 44852af9545SBill Pemberton init_waitqueue_head(&priv->delta_msr_wait); 449638b9e15SJohan Hovold 45052af9545SBill Pemberton usb_set_serial_port_data(port, priv); 45152af9545SBill Pemberton 452638b9e15SJohan Hovold return 0; 453638b9e15SJohan Hovold } 454638b9e15SJohan Hovold 455638b9e15SJohan Hovold static int ssu100_port_remove(struct usb_serial_port *port) 456638b9e15SJohan Hovold { 457638b9e15SJohan Hovold struct ssu100_port_private *priv; 458638b9e15SJohan Hovold 459638b9e15SJohan Hovold priv = usb_get_serial_port_data(port); 460638b9e15SJohan Hovold kfree(priv); 461638b9e15SJohan Hovold 462638b9e15SJohan Hovold return 0; 46352af9545SBill Pemberton } 46452af9545SBill Pemberton 46560b33c13SAlan Cox static int ssu100_tiocmget(struct tty_struct *tty) 46652af9545SBill Pemberton { 46752af9545SBill Pemberton struct usb_serial_port *port = tty->driver_data; 46852af9545SBill Pemberton struct usb_device *dev = port->serial->dev; 46952af9545SBill Pemberton u8 *d; 47052af9545SBill Pemberton int r; 47152af9545SBill Pemberton 47252af9545SBill Pemberton d = kzalloc(2, GFP_KERNEL); 47352af9545SBill Pemberton if (!d) 47452af9545SBill Pemberton return -ENOMEM; 47552af9545SBill Pemberton 47679f203a2SBill Pemberton r = ssu100_getregister(dev, 0, UART_MCR, d); 47752af9545SBill Pemberton if (r < 0) 47852af9545SBill Pemberton goto mget_out; 47952af9545SBill Pemberton 48079f203a2SBill Pemberton r = ssu100_getregister(dev, 0, UART_MSR, d+1); 48152af9545SBill Pemberton if (r < 0) 48252af9545SBill Pemberton goto mget_out; 48352af9545SBill Pemberton 48479f203a2SBill Pemberton r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) | 48579f203a2SBill Pemberton (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) | 48679f203a2SBill Pemberton (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) | 48779f203a2SBill Pemberton (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) | 48879f203a2SBill Pemberton (d[1] & UART_MSR_RI ? TIOCM_RI : 0) | 48979f203a2SBill Pemberton (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0); 49052af9545SBill Pemberton 49152af9545SBill Pemberton mget_out: 49252af9545SBill Pemberton kfree(d); 49352af9545SBill Pemberton return r; 49452af9545SBill Pemberton } 49552af9545SBill Pemberton 49620b9d177SAlan Cox static int ssu100_tiocmset(struct tty_struct *tty, 49752af9545SBill Pemberton unsigned int set, unsigned int clear) 49852af9545SBill Pemberton { 49952af9545SBill Pemberton struct usb_serial_port *port = tty->driver_data; 50052af9545SBill Pemberton struct usb_device *dev = port->serial->dev; 50152af9545SBill Pemberton 50252af9545SBill Pemberton return update_mctrl(dev, set, clear); 50352af9545SBill Pemberton } 50452af9545SBill Pemberton 50552af9545SBill Pemberton static void ssu100_dtr_rts(struct usb_serial_port *port, int on) 50652af9545SBill Pemberton { 50752af9545SBill Pemberton struct usb_device *dev = port->serial->dev; 50852af9545SBill Pemberton 50952af9545SBill Pemberton /* Disable flow control */ 510*b2ca6990SJohan Hovold if (!on) { 511*b2ca6990SJohan Hovold if (ssu100_setregister(dev, 0, UART_MCR, 0) < 0) 51252af9545SBill Pemberton dev_err(&port->dev, "error from flowcontrol urb\n"); 513*b2ca6990SJohan Hovold } 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 521f81c83dbSBill Pemberton static void ssu100_update_msr(struct usb_serial_port *port, u8 msr) 522f81c83dbSBill Pemberton { 523f81c83dbSBill Pemberton struct ssu100_port_private *priv = usb_get_serial_port_data(port); 524f81c83dbSBill Pemberton unsigned long flags; 525f81c83dbSBill Pemberton 526f81c83dbSBill Pemberton spin_lock_irqsave(&priv->status_lock, flags); 527f81c83dbSBill Pemberton priv->shadowMSR = msr; 528f81c83dbSBill Pemberton spin_unlock_irqrestore(&priv->status_lock, flags); 529f81c83dbSBill Pemberton 530f81c83dbSBill Pemberton if (msr & UART_MSR_ANY_DELTA) { 531f81c83dbSBill Pemberton /* update input line counters */ 532f81c83dbSBill Pemberton if (msr & UART_MSR_DCTS) 533f81c83dbSBill Pemberton priv->icount.cts++; 534f81c83dbSBill Pemberton if (msr & UART_MSR_DDSR) 535f81c83dbSBill Pemberton priv->icount.dsr++; 536f81c83dbSBill Pemberton if (msr & UART_MSR_DDCD) 537f81c83dbSBill Pemberton priv->icount.dcd++; 538f81c83dbSBill Pemberton if (msr & UART_MSR_TERI) 539f81c83dbSBill Pemberton priv->icount.rng++; 540f81c83dbSBill Pemberton wake_up_interruptible(&priv->delta_msr_wait); 541f81c83dbSBill Pemberton } 542f81c83dbSBill Pemberton } 543f81c83dbSBill Pemberton 5446b8f1ca5SBill Pemberton static void ssu100_update_lsr(struct usb_serial_port *port, u8 lsr, 5456b8f1ca5SBill Pemberton char *tty_flag) 546f81c83dbSBill Pemberton { 547f81c83dbSBill Pemberton struct ssu100_port_private *priv = usb_get_serial_port_data(port); 548f81c83dbSBill Pemberton unsigned long flags; 549f81c83dbSBill Pemberton 550f81c83dbSBill Pemberton spin_lock_irqsave(&priv->status_lock, flags); 551f81c83dbSBill Pemberton priv->shadowLSR = lsr; 552f81c83dbSBill Pemberton spin_unlock_irqrestore(&priv->status_lock, flags); 553f81c83dbSBill Pemberton 5546b8f1ca5SBill Pemberton *tty_flag = TTY_NORMAL; 555f81c83dbSBill Pemberton if (lsr & UART_LSR_BRK_ERROR_BITS) { 5566b8f1ca5SBill Pemberton /* we always want to update icount, but we only want to 5576b8f1ca5SBill Pemberton * update tty_flag for one case */ 5586b8f1ca5SBill Pemberton if (lsr & UART_LSR_BI) { 559f81c83dbSBill Pemberton priv->icount.brk++; 5606b8f1ca5SBill Pemberton *tty_flag = TTY_BREAK; 5616b8f1ca5SBill Pemberton usb_serial_handle_break(port); 562f81c83dbSBill Pemberton } 5636b8f1ca5SBill Pemberton if (lsr & UART_LSR_PE) { 5646b8f1ca5SBill Pemberton priv->icount.parity++; 5656b8f1ca5SBill Pemberton if (*tty_flag == TTY_NORMAL) 5666b8f1ca5SBill Pemberton *tty_flag = TTY_PARITY; 5676b8f1ca5SBill Pemberton } 5686b8f1ca5SBill Pemberton if (lsr & UART_LSR_FE) { 5696b8f1ca5SBill Pemberton priv->icount.frame++; 5706b8f1ca5SBill Pemberton if (*tty_flag == TTY_NORMAL) 5716b8f1ca5SBill Pemberton *tty_flag = TTY_FRAME; 5726b8f1ca5SBill Pemberton } 5736b8f1ca5SBill Pemberton if (lsr & UART_LSR_OE){ 5746b8f1ca5SBill Pemberton priv->icount.overrun++; 5756b8f1ca5SBill Pemberton if (*tty_flag == TTY_NORMAL) 5766b8f1ca5SBill Pemberton *tty_flag = TTY_OVERRUN; 5776b8f1ca5SBill Pemberton } 5786b8f1ca5SBill Pemberton } 5796b8f1ca5SBill Pemberton 580f81c83dbSBill Pemberton } 581f81c83dbSBill Pemberton 582f7043ecbSBill Pemberton static int ssu100_process_packet(struct urb *urb, 583f7043ecbSBill Pemberton struct tty_struct *tty) 58452af9545SBill Pemberton { 585f7043ecbSBill Pemberton struct usb_serial_port *port = urb->context; 586f7043ecbSBill Pemberton char *packet = (char *)urb->transfer_buffer; 5876b8f1ca5SBill Pemberton char flag = TTY_NORMAL; 588f7043ecbSBill Pemberton u32 len = urb->actual_length; 589f7043ecbSBill Pemberton int i; 59052af9545SBill Pemberton char *ch; 59152af9545SBill Pemberton 5929b2cef31SBill Pemberton if ((len >= 4) && 5939b2cef31SBill Pemberton (packet[0] == 0x1b) && (packet[1] == 0x1b) && 59452af9545SBill Pemberton ((packet[2] == 0x00) || (packet[2] == 0x01))) { 5956b8f1ca5SBill Pemberton if (packet[2] == 0x00) { 5966b8f1ca5SBill Pemberton ssu100_update_lsr(port, packet[3], &flag); 5976b8f1ca5SBill Pemberton if (flag == TTY_OVERRUN) 5986b8f1ca5SBill Pemberton tty_insert_flip_char(tty, 0, TTY_OVERRUN); 5996b8f1ca5SBill Pemberton } 600f81c83dbSBill Pemberton if (packet[2] == 0x01) 601f81c83dbSBill Pemberton ssu100_update_msr(port, packet[3]); 60252af9545SBill Pemberton 60352af9545SBill Pemberton len -= 4; 60452af9545SBill Pemberton ch = packet + 4; 60552af9545SBill Pemberton } else 60652af9545SBill Pemberton ch = packet; 60752af9545SBill Pemberton 60852af9545SBill Pemberton if (!len) 60952af9545SBill Pemberton return 0; /* status only */ 61052af9545SBill Pemberton 61152af9545SBill Pemberton if (port->port.console && port->sysrq) { 61252af9545SBill Pemberton for (i = 0; i < len; i++, ch++) { 6136ee9f4b4SDmitry Torokhov if (!usb_serial_handle_sysrq_char(port, *ch)) 61452af9545SBill Pemberton tty_insert_flip_char(tty, *ch, flag); 61552af9545SBill Pemberton } 61652af9545SBill Pemberton } else 61752af9545SBill Pemberton tty_insert_flip_string_fixed_flag(tty, ch, flag, len); 61852af9545SBill Pemberton 61952af9545SBill Pemberton return len; 62052af9545SBill Pemberton } 62152af9545SBill Pemberton 62252af9545SBill Pemberton static void ssu100_process_read_urb(struct urb *urb) 62352af9545SBill Pemberton { 62452af9545SBill Pemberton struct usb_serial_port *port = urb->context; 62552af9545SBill Pemberton struct tty_struct *tty; 626f7043ecbSBill Pemberton int count; 62752af9545SBill Pemberton 62852af9545SBill Pemberton tty = tty_port_tty_get(&port->port); 62952af9545SBill Pemberton if (!tty) 63052af9545SBill Pemberton return; 63152af9545SBill Pemberton 632f7043ecbSBill Pemberton count = ssu100_process_packet(urb, tty); 63352af9545SBill Pemberton 63452af9545SBill Pemberton if (count) 63552af9545SBill Pemberton tty_flip_buffer_push(tty); 63652af9545SBill Pemberton tty_kref_put(tty); 63752af9545SBill Pemberton } 63852af9545SBill Pemberton 63952af9545SBill Pemberton static struct usb_serial_driver ssu100_device = { 64052af9545SBill Pemberton .driver = { 64152af9545SBill Pemberton .owner = THIS_MODULE, 64252af9545SBill Pemberton .name = "ssu100", 64352af9545SBill Pemberton }, 64452af9545SBill Pemberton .description = DRIVER_DESC, 64552af9545SBill Pemberton .id_table = id_table, 64652af9545SBill Pemberton .num_ports = 1, 64752af9545SBill Pemberton .open = ssu100_open, 64852af9545SBill Pemberton .close = ssu100_close, 64952af9545SBill Pemberton .attach = ssu100_attach, 650638b9e15SJohan Hovold .port_probe = ssu100_port_probe, 651638b9e15SJohan Hovold .port_remove = ssu100_port_remove, 65252af9545SBill Pemberton .dtr_rts = ssu100_dtr_rts, 65352af9545SBill Pemberton .process_read_urb = ssu100_process_read_urb, 65452af9545SBill Pemberton .tiocmget = ssu100_tiocmget, 65552af9545SBill Pemberton .tiocmset = ssu100_tiocmset, 6560bca1b91SAlan Cox .get_icount = ssu100_get_icount, 65752af9545SBill Pemberton .ioctl = ssu100_ioctl, 65852af9545SBill Pemberton .set_termios = ssu100_set_termios, 65985dee135SBill Pemberton .disconnect = usb_serial_generic_disconnect, 66052af9545SBill Pemberton }; 66152af9545SBill Pemberton 662d860322fSAlan Stern static struct usb_serial_driver * const serial_drivers[] = { 663d860322fSAlan Stern &ssu100_device, NULL 664d860322fSAlan Stern }; 665d860322fSAlan Stern 66668e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table); 66752af9545SBill Pemberton 66852af9545SBill Pemberton MODULE_DESCRIPTION(DRIVER_DESC); 66952af9545SBill Pemberton MODULE_LICENSE("GPL"); 670