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 493c35b002SBill Pemberton static int 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 6352af9545SBill Pemberton MODULE_DEVICE_TABLE(usb, id_table); 6452af9545SBill Pemberton 6552af9545SBill Pemberton 6652af9545SBill Pemberton static struct usb_driver ssu100_driver = { 6752af9545SBill Pemberton .name = "ssu100", 6852af9545SBill Pemberton .probe = usb_serial_probe, 6952af9545SBill Pemberton .disconnect = usb_serial_disconnect, 7052af9545SBill Pemberton .id_table = id_table, 7152af9545SBill Pemberton .suspend = usb_serial_suspend, 7252af9545SBill Pemberton .resume = usb_serial_resume, 7352af9545SBill Pemberton .no_dynamic_id = 1, 7452af9545SBill Pemberton .supports_autosuspend = 1, 7552af9545SBill Pemberton }; 7652af9545SBill Pemberton 7752af9545SBill Pemberton struct ssu100_port_private { 7817523058SBill Pemberton spinlock_t status_lock; 7952af9545SBill Pemberton u8 shadowLSR; 8052af9545SBill Pemberton u8 shadowMSR; 8152af9545SBill Pemberton wait_queue_head_t delta_msr_wait; /* Used for TIOCMIWAIT */ 82f81c83dbSBill Pemberton struct async_icount icount; 8352af9545SBill Pemberton }; 8452af9545SBill Pemberton 8552af9545SBill Pemberton static void ssu100_release(struct usb_serial *serial) 8652af9545SBill Pemberton { 8752af9545SBill Pemberton struct ssu100_port_private *priv = usb_get_serial_port_data(*serial->port); 8852af9545SBill Pemberton 8952af9545SBill Pemberton dbg("%s", __func__); 9052af9545SBill Pemberton kfree(priv); 9152af9545SBill Pemberton } 9252af9545SBill Pemberton 9352af9545SBill Pemberton static inline int ssu100_control_msg(struct usb_device *dev, 9452af9545SBill Pemberton u8 request, u16 data, u16 index) 9552af9545SBill Pemberton { 9652af9545SBill Pemberton return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 9752af9545SBill Pemberton request, 0x40, data, index, 9852af9545SBill Pemberton NULL, 0, 300); 9952af9545SBill Pemberton } 10052af9545SBill Pemberton 10152af9545SBill Pemberton static inline int ssu100_setdevice(struct usb_device *dev, u8 *data) 10252af9545SBill Pemberton { 10352af9545SBill Pemberton u16 x = ((u16)(data[1] << 8) | (u16)(data[0])); 10452af9545SBill Pemberton 10552af9545SBill Pemberton return ssu100_control_msg(dev, QT_SET_GET_DEVICE, x, 0); 10652af9545SBill Pemberton } 10752af9545SBill Pemberton 10852af9545SBill Pemberton 10952af9545SBill Pemberton static inline int ssu100_getdevice(struct usb_device *dev, u8 *data) 11052af9545SBill Pemberton { 11152af9545SBill Pemberton return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 11252af9545SBill Pemberton QT_SET_GET_DEVICE, 0xc0, 0, 0, 11352af9545SBill Pemberton data, 3, 300); 11452af9545SBill Pemberton } 11552af9545SBill Pemberton 11652af9545SBill Pemberton static inline int ssu100_getregister(struct usb_device *dev, 11752af9545SBill Pemberton unsigned short uart, 11852af9545SBill Pemberton unsigned short reg, 11952af9545SBill Pemberton u8 *data) 12052af9545SBill Pemberton { 12152af9545SBill Pemberton return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 12252af9545SBill Pemberton QT_SET_GET_REGISTER, 0xc0, reg, 12352af9545SBill Pemberton uart, data, sizeof(*data), 300); 12452af9545SBill Pemberton 12552af9545SBill Pemberton } 12652af9545SBill Pemberton 12752af9545SBill Pemberton 12852af9545SBill Pemberton static inline int ssu100_setregister(struct usb_device *dev, 12952af9545SBill Pemberton unsigned short uart, 130556f1a0eSBill Pemberton unsigned short reg, 13152af9545SBill Pemberton u16 data) 13252af9545SBill Pemberton { 133556f1a0eSBill Pemberton u16 value = (data << 8) | reg; 13452af9545SBill Pemberton 13552af9545SBill Pemberton return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 13652af9545SBill Pemberton QT_SET_GET_REGISTER, 0x40, value, uart, 13752af9545SBill Pemberton NULL, 0, 300); 13852af9545SBill Pemberton 13952af9545SBill Pemberton } 14052af9545SBill Pemberton 14152af9545SBill Pemberton #define set_mctrl(dev, set) update_mctrl((dev), (set), 0) 14252af9545SBill Pemberton #define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear)) 14352af9545SBill Pemberton 14452af9545SBill Pemberton /* these do not deal with device that have more than 1 port */ 14552af9545SBill Pemberton static inline int update_mctrl(struct usb_device *dev, unsigned int set, 14652af9545SBill Pemberton unsigned int clear) 14752af9545SBill Pemberton { 14852af9545SBill Pemberton unsigned urb_value; 14952af9545SBill Pemberton int result; 15052af9545SBill Pemberton 15152af9545SBill Pemberton if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) { 15252af9545SBill Pemberton dbg("%s - DTR|RTS not being set|cleared", __func__); 15352af9545SBill Pemberton return 0; /* no change */ 15452af9545SBill Pemberton } 15552af9545SBill Pemberton 15652af9545SBill Pemberton clear &= ~set; /* 'set' takes precedence over 'clear' */ 15752af9545SBill Pemberton urb_value = 0; 15852af9545SBill Pemberton if (set & TIOCM_DTR) 15979f203a2SBill Pemberton urb_value |= UART_MCR_DTR; 16052af9545SBill Pemberton if (set & TIOCM_RTS) 16179f203a2SBill Pemberton urb_value |= UART_MCR_RTS; 16252af9545SBill Pemberton 163556f1a0eSBill Pemberton result = ssu100_setregister(dev, 0, UART_MCR, urb_value); 16452af9545SBill Pemberton if (result < 0) 16552af9545SBill Pemberton dbg("%s Error from MODEM_CTRL urb", __func__); 16652af9545SBill Pemberton 16752af9545SBill Pemberton return result; 16852af9545SBill Pemberton } 16952af9545SBill Pemberton 17052af9545SBill Pemberton static int ssu100_initdevice(struct usb_device *dev) 17152af9545SBill Pemberton { 17252af9545SBill Pemberton u8 *data; 17352af9545SBill Pemberton int result = 0; 17452af9545SBill Pemberton 17552af9545SBill Pemberton dbg("%s", __func__); 17652af9545SBill Pemberton 17752af9545SBill Pemberton data = kzalloc(3, GFP_KERNEL); 17852af9545SBill Pemberton if (!data) 17952af9545SBill Pemberton return -ENOMEM; 18052af9545SBill Pemberton 18152af9545SBill Pemberton result = ssu100_getdevice(dev, data); 18252af9545SBill Pemberton if (result < 0) { 18352af9545SBill Pemberton dbg("%s - get_device failed %i", __func__, result); 18452af9545SBill Pemberton goto out; 18552af9545SBill Pemberton } 18652af9545SBill Pemberton 18752af9545SBill Pemberton data[1] &= ~FULLPWRBIT; 18852af9545SBill Pemberton 18952af9545SBill Pemberton result = ssu100_setdevice(dev, data); 19052af9545SBill Pemberton if (result < 0) { 19152af9545SBill Pemberton dbg("%s - setdevice failed %i", __func__, result); 19252af9545SBill Pemberton goto out; 19352af9545SBill Pemberton } 19452af9545SBill Pemberton 19552af9545SBill Pemberton result = ssu100_control_msg(dev, QT_GET_SET_PREBUF_TRIG_LVL, 128, 0); 19652af9545SBill Pemberton if (result < 0) { 19752af9545SBill Pemberton dbg("%s - set prebuffer level failed %i", __func__, result); 19852af9545SBill Pemberton goto out; 19952af9545SBill Pemberton } 20052af9545SBill Pemberton 20152af9545SBill Pemberton result = ssu100_control_msg(dev, QT_SET_ATF, ATC_DISABLED, 0); 20252af9545SBill Pemberton if (result < 0) { 20352af9545SBill Pemberton dbg("%s - set ATFprebuffer level failed %i", __func__, result); 20452af9545SBill Pemberton goto out; 20552af9545SBill Pemberton } 20652af9545SBill Pemberton 20752af9545SBill Pemberton result = ssu100_getdevice(dev, data); 20852af9545SBill Pemberton if (result < 0) { 20952af9545SBill Pemberton dbg("%s - get_device failed %i", __func__, result); 21052af9545SBill Pemberton goto out; 21152af9545SBill Pemberton } 21252af9545SBill Pemberton 21352af9545SBill Pemberton data[0] &= ~(RR_BITS | DUPMODE_BITS); 21452af9545SBill Pemberton data[0] |= CLKS_X4; 21552af9545SBill Pemberton data[1] &= ~(LOOPMODE_BITS); 21652af9545SBill Pemberton data[1] |= RS232_MODE; 21752af9545SBill Pemberton 21852af9545SBill Pemberton result = ssu100_setdevice(dev, data); 21952af9545SBill Pemberton if (result < 0) { 22052af9545SBill Pemberton dbg("%s - setdevice failed %i", __func__, result); 22152af9545SBill Pemberton goto out; 22252af9545SBill Pemberton } 22352af9545SBill Pemberton 22452af9545SBill Pemberton out: kfree(data); 22552af9545SBill Pemberton return result; 22652af9545SBill Pemberton 22752af9545SBill Pemberton } 22852af9545SBill Pemberton 22952af9545SBill Pemberton 23052af9545SBill Pemberton static void ssu100_set_termios(struct tty_struct *tty, 23152af9545SBill Pemberton struct usb_serial_port *port, 23252af9545SBill Pemberton struct ktermios *old_termios) 23352af9545SBill Pemberton { 23452af9545SBill Pemberton struct usb_device *dev = port->serial->dev; 23552af9545SBill Pemberton struct ktermios *termios = tty->termios; 23652af9545SBill Pemberton u16 baud, divisor, remainder; 23752af9545SBill Pemberton unsigned int cflag = termios->c_cflag; 23852af9545SBill Pemberton u16 urb_value = 0; /* will hold the new flags */ 23952af9545SBill Pemberton int result; 24052af9545SBill Pemberton 24152af9545SBill Pemberton dbg("%s", __func__); 24252af9545SBill Pemberton 24352af9545SBill Pemberton if (cflag & PARENB) { 24452af9545SBill Pemberton if (cflag & PARODD) 24579f203a2SBill Pemberton urb_value |= UART_LCR_PARITY; 24652af9545SBill Pemberton else 24752af9545SBill Pemberton urb_value |= SERIAL_EVEN_PARITY; 24852af9545SBill Pemberton } 24952af9545SBill Pemberton 25052af9545SBill Pemberton switch (cflag & CSIZE) { 25152af9545SBill Pemberton case CS5: 25279f203a2SBill Pemberton urb_value |= UART_LCR_WLEN5; 25352af9545SBill Pemberton break; 25452af9545SBill Pemberton case CS6: 25579f203a2SBill Pemberton urb_value |= UART_LCR_WLEN6; 25652af9545SBill Pemberton break; 25752af9545SBill Pemberton case CS7: 25879f203a2SBill Pemberton urb_value |= UART_LCR_WLEN7; 25952af9545SBill Pemberton break; 26052af9545SBill Pemberton default: 26152af9545SBill Pemberton case CS8: 26279f203a2SBill Pemberton urb_value |= UART_LCR_WLEN8; 26352af9545SBill Pemberton break; 26452af9545SBill Pemberton } 26552af9545SBill Pemberton 26652af9545SBill Pemberton baud = tty_get_baud_rate(tty); 26752af9545SBill Pemberton if (!baud) 26852af9545SBill Pemberton baud = 9600; 26952af9545SBill Pemberton 27052af9545SBill Pemberton dbg("%s - got baud = %d\n", __func__, baud); 27152af9545SBill Pemberton 27252af9545SBill Pemberton 27352af9545SBill Pemberton divisor = MAX_BAUD_RATE / baud; 27452af9545SBill Pemberton remainder = MAX_BAUD_RATE % baud; 27552af9545SBill Pemberton if (((remainder * 2) >= baud) && (baud != 110)) 27652af9545SBill Pemberton divisor++; 27752af9545SBill Pemberton 27852af9545SBill Pemberton urb_value = urb_value << 8; 27952af9545SBill Pemberton 28052af9545SBill Pemberton result = ssu100_control_msg(dev, QT_GET_SET_UART, divisor, urb_value); 28152af9545SBill Pemberton if (result < 0) 28252af9545SBill Pemberton dbg("%s - set uart failed", __func__); 28352af9545SBill Pemberton 28452af9545SBill Pemberton if (cflag & CRTSCTS) 28552af9545SBill Pemberton result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK, 28652af9545SBill Pemberton SERIAL_CRTSCTS, 0); 28752af9545SBill Pemberton else 28852af9545SBill Pemberton result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK, 28952af9545SBill Pemberton 0, 0); 29052af9545SBill Pemberton if (result < 0) 29152af9545SBill Pemberton dbg("%s - set HW flow control failed", __func__); 29252af9545SBill Pemberton 29352af9545SBill Pemberton if (I_IXOFF(tty) || I_IXON(tty)) { 29452af9545SBill Pemberton u16 x = ((u16)(START_CHAR(tty) << 8) | (u16)(STOP_CHAR(tty))); 29552af9545SBill Pemberton 29652af9545SBill Pemberton result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK, 29752af9545SBill Pemberton x, 0); 29852af9545SBill Pemberton } else 29952af9545SBill Pemberton result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK, 30052af9545SBill Pemberton 0, 0); 30152af9545SBill Pemberton 30252af9545SBill Pemberton if (result < 0) 30352af9545SBill Pemberton dbg("%s - set SW flow control failed", __func__); 30452af9545SBill Pemberton 30552af9545SBill Pemberton } 30652af9545SBill Pemberton 30752af9545SBill Pemberton 30852af9545SBill Pemberton static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port) 30952af9545SBill Pemberton { 31052af9545SBill Pemberton struct usb_device *dev = port->serial->dev; 31152af9545SBill Pemberton struct ssu100_port_private *priv = usb_get_serial_port_data(port); 31252af9545SBill Pemberton u8 *data; 31352af9545SBill Pemberton int result; 31417523058SBill Pemberton unsigned long flags; 31552af9545SBill Pemberton 31652af9545SBill Pemberton dbg("%s - port %d", __func__, port->number); 31752af9545SBill Pemberton 31852af9545SBill Pemberton data = kzalloc(2, GFP_KERNEL); 31952af9545SBill Pemberton if (!data) 32052af9545SBill Pemberton return -ENOMEM; 32152af9545SBill Pemberton 32252af9545SBill Pemberton result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 32352af9545SBill Pemberton QT_OPEN_CLOSE_CHANNEL, 32452af9545SBill Pemberton QT_TRANSFER_IN, 0x01, 32552af9545SBill Pemberton 0, data, 2, 300); 32652af9545SBill Pemberton if (result < 0) { 32752af9545SBill Pemberton dbg("%s - open failed %i", __func__, result); 32852af9545SBill Pemberton kfree(data); 32952af9545SBill Pemberton return result; 33052af9545SBill Pemberton } 33152af9545SBill Pemberton 33217523058SBill Pemberton spin_lock_irqsave(&priv->status_lock, flags); 333f81c83dbSBill Pemberton priv->shadowLSR = data[0]; 334f81c83dbSBill Pemberton priv->shadowMSR = data[1]; 33517523058SBill Pemberton spin_unlock_irqrestore(&priv->status_lock, flags); 33652af9545SBill Pemberton 33752af9545SBill Pemberton kfree(data); 33852af9545SBill Pemberton 33952af9545SBill Pemberton /* set to 9600 */ 34052af9545SBill Pemberton result = ssu100_control_msg(dev, QT_GET_SET_UART, 0x30, 0x0300); 34152af9545SBill Pemberton if (result < 0) 34252af9545SBill Pemberton dbg("%s - set uart failed", __func__); 34352af9545SBill Pemberton 34452af9545SBill Pemberton if (tty) 34552af9545SBill Pemberton ssu100_set_termios(tty, port, tty->termios); 34652af9545SBill Pemberton 34752af9545SBill Pemberton return usb_serial_generic_open(tty, port); 34852af9545SBill Pemberton } 34952af9545SBill Pemberton 35052af9545SBill Pemberton static void ssu100_close(struct usb_serial_port *port) 35152af9545SBill Pemberton { 35252af9545SBill Pemberton dbg("%s", __func__); 35352af9545SBill Pemberton usb_serial_generic_close(port); 35452af9545SBill Pemberton } 35552af9545SBill Pemberton 35652af9545SBill Pemberton static int get_serial_info(struct usb_serial_port *port, 35752af9545SBill Pemberton struct serial_struct __user *retinfo) 35852af9545SBill Pemberton { 35952af9545SBill Pemberton struct serial_struct tmp; 36052af9545SBill Pemberton 36152af9545SBill Pemberton if (!retinfo) 36252af9545SBill Pemberton return -EFAULT; 36352af9545SBill Pemberton 36452af9545SBill Pemberton memset(&tmp, 0, sizeof(tmp)); 36552af9545SBill Pemberton tmp.line = port->serial->minor; 36652af9545SBill Pemberton tmp.port = 0; 36752af9545SBill Pemberton tmp.irq = 0; 36852af9545SBill Pemberton tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; 36952af9545SBill Pemberton tmp.xmit_fifo_size = port->bulk_out_size; 37052af9545SBill Pemberton tmp.baud_base = 9600; 37152af9545SBill Pemberton tmp.close_delay = 5*HZ; 37252af9545SBill Pemberton tmp.closing_wait = 30*HZ; 37352af9545SBill Pemberton 37452af9545SBill Pemberton if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) 37552af9545SBill Pemberton return -EFAULT; 37652af9545SBill Pemberton return 0; 37752af9545SBill Pemberton } 37852af9545SBill Pemberton 379f81c83dbSBill Pemberton static int wait_modem_info(struct usb_serial_port *port, unsigned int arg) 380f81c83dbSBill Pemberton { 381f81c83dbSBill Pemberton struct ssu100_port_private *priv = usb_get_serial_port_data(port); 382f81c83dbSBill Pemberton struct async_icount prev, cur; 383f81c83dbSBill Pemberton unsigned long flags; 384f81c83dbSBill Pemberton 385f81c83dbSBill Pemberton spin_lock_irqsave(&priv->status_lock, flags); 386f81c83dbSBill Pemberton prev = priv->icount; 387f81c83dbSBill Pemberton spin_unlock_irqrestore(&priv->status_lock, flags); 388f81c83dbSBill Pemberton 389f81c83dbSBill Pemberton while (1) { 390f81c83dbSBill Pemberton wait_event_interruptible(priv->delta_msr_wait, 391f81c83dbSBill Pemberton ((priv->icount.rng != prev.rng) || 392f81c83dbSBill Pemberton (priv->icount.dsr != prev.dsr) || 393f81c83dbSBill Pemberton (priv->icount.dcd != prev.dcd) || 394f81c83dbSBill Pemberton (priv->icount.cts != prev.cts))); 395f81c83dbSBill Pemberton 396f81c83dbSBill Pemberton if (signal_pending(current)) 397f81c83dbSBill Pemberton return -ERESTARTSYS; 398f81c83dbSBill Pemberton 399f81c83dbSBill Pemberton spin_lock_irqsave(&priv->status_lock, flags); 400f81c83dbSBill Pemberton cur = priv->icount; 401f81c83dbSBill Pemberton spin_unlock_irqrestore(&priv->status_lock, flags); 402f81c83dbSBill Pemberton 403f81c83dbSBill Pemberton if ((prev.rng == cur.rng) && 404f81c83dbSBill Pemberton (prev.dsr == cur.dsr) && 405f81c83dbSBill Pemberton (prev.dcd == cur.dcd) && 406f81c83dbSBill Pemberton (prev.cts == cur.cts)) 407f81c83dbSBill Pemberton return -EIO; 408f81c83dbSBill Pemberton 409f81c83dbSBill Pemberton if ((arg & TIOCM_RNG && (prev.rng != cur.rng)) || 410f81c83dbSBill Pemberton (arg & TIOCM_DSR && (prev.dsr != cur.dsr)) || 411f81c83dbSBill Pemberton (arg & TIOCM_CD && (prev.dcd != cur.dcd)) || 412f81c83dbSBill Pemberton (arg & TIOCM_CTS && (prev.cts != cur.cts))) 413f81c83dbSBill Pemberton return 0; 414f81c83dbSBill Pemberton } 415f81c83dbSBill Pemberton return 0; 416f81c83dbSBill Pemberton } 417f81c83dbSBill Pemberton 4180bca1b91SAlan Cox static int ssu100_get_icount(struct tty_struct *tty, 4190bca1b91SAlan Cox struct serial_icounter_struct *icount) 4200bca1b91SAlan Cox { 4210bca1b91SAlan Cox struct usb_serial_port *port = tty->driver_data; 4220bca1b91SAlan Cox struct ssu100_port_private *priv = usb_get_serial_port_data(port); 4230bca1b91SAlan Cox struct async_icount cnow = priv->icount; 4240bca1b91SAlan Cox 4250bca1b91SAlan Cox icount->cts = cnow.cts; 4260bca1b91SAlan Cox icount->dsr = cnow.dsr; 4270bca1b91SAlan Cox icount->rng = cnow.rng; 4280bca1b91SAlan Cox icount->dcd = cnow.dcd; 4290bca1b91SAlan Cox icount->rx = cnow.rx; 4300bca1b91SAlan Cox icount->tx = cnow.tx; 4310bca1b91SAlan Cox icount->frame = cnow.frame; 4320bca1b91SAlan Cox icount->overrun = cnow.overrun; 4330bca1b91SAlan Cox icount->parity = cnow.parity; 4340bca1b91SAlan Cox icount->brk = cnow.brk; 4350bca1b91SAlan Cox icount->buf_overrun = cnow.buf_overrun; 4360bca1b91SAlan Cox 4370bca1b91SAlan Cox return 0; 4380bca1b91SAlan Cox } 4390bca1b91SAlan Cox 4400bca1b91SAlan Cox 4410bca1b91SAlan Cox 442*00a0d0d6SAlan Cox static int ssu100_ioctl(struct tty_struct *tty, 44352af9545SBill Pemberton unsigned int cmd, unsigned long arg) 44452af9545SBill Pemberton { 44552af9545SBill Pemberton struct usb_serial_port *port = tty->driver_data; 44652af9545SBill Pemberton 44752af9545SBill Pemberton dbg("%s cmd 0x%04x", __func__, cmd); 44852af9545SBill Pemberton 44952af9545SBill Pemberton switch (cmd) { 45052af9545SBill Pemberton case TIOCGSERIAL: 45152af9545SBill Pemberton return get_serial_info(port, 45252af9545SBill Pemberton (struct serial_struct __user *) arg); 45352af9545SBill Pemberton 45452af9545SBill Pemberton case TIOCMIWAIT: 455f81c83dbSBill Pemberton return wait_modem_info(port, arg); 45652af9545SBill Pemberton 45752af9545SBill Pemberton default: 45852af9545SBill Pemberton break; 45952af9545SBill Pemberton } 46052af9545SBill Pemberton 46152af9545SBill Pemberton dbg("%s arg not supported", __func__); 46252af9545SBill Pemberton 46352af9545SBill Pemberton return -ENOIOCTLCMD; 46452af9545SBill Pemberton } 46552af9545SBill Pemberton 46652af9545SBill Pemberton static int ssu100_attach(struct usb_serial *serial) 46752af9545SBill Pemberton { 46852af9545SBill Pemberton struct ssu100_port_private *priv; 46952af9545SBill Pemberton struct usb_serial_port *port = *serial->port; 47052af9545SBill Pemberton 47152af9545SBill Pemberton dbg("%s", __func__); 47252af9545SBill Pemberton 47352af9545SBill Pemberton priv = kzalloc(sizeof(*priv), GFP_KERNEL); 47452af9545SBill Pemberton if (!priv) { 47552af9545SBill Pemberton dev_err(&port->dev, "%s- kmalloc(%Zd) failed.\n", __func__, 47652af9545SBill Pemberton sizeof(*priv)); 47752af9545SBill Pemberton return -ENOMEM; 47852af9545SBill Pemberton } 47952af9545SBill Pemberton 48017523058SBill Pemberton spin_lock_init(&priv->status_lock); 48152af9545SBill Pemberton init_waitqueue_head(&priv->delta_msr_wait); 48252af9545SBill Pemberton usb_set_serial_port_data(port, priv); 48352af9545SBill Pemberton 48452af9545SBill Pemberton return ssu100_initdevice(serial->dev); 48552af9545SBill Pemberton } 48652af9545SBill Pemberton 48760b33c13SAlan Cox static int ssu100_tiocmget(struct tty_struct *tty) 48852af9545SBill Pemberton { 48952af9545SBill Pemberton struct usb_serial_port *port = tty->driver_data; 49052af9545SBill Pemberton struct usb_device *dev = port->serial->dev; 49152af9545SBill Pemberton u8 *d; 49252af9545SBill Pemberton int r; 49352af9545SBill Pemberton 49452af9545SBill Pemberton dbg("%s\n", __func__); 49552af9545SBill Pemberton 49652af9545SBill Pemberton d = kzalloc(2, GFP_KERNEL); 49752af9545SBill Pemberton if (!d) 49852af9545SBill Pemberton return -ENOMEM; 49952af9545SBill Pemberton 50079f203a2SBill Pemberton r = ssu100_getregister(dev, 0, UART_MCR, d); 50152af9545SBill Pemberton if (r < 0) 50252af9545SBill Pemberton goto mget_out; 50352af9545SBill Pemberton 50479f203a2SBill Pemberton r = ssu100_getregister(dev, 0, UART_MSR, d+1); 50552af9545SBill Pemberton if (r < 0) 50652af9545SBill Pemberton goto mget_out; 50752af9545SBill Pemberton 50879f203a2SBill Pemberton r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) | 50979f203a2SBill Pemberton (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) | 51079f203a2SBill Pemberton (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) | 51179f203a2SBill Pemberton (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) | 51279f203a2SBill Pemberton (d[1] & UART_MSR_RI ? TIOCM_RI : 0) | 51379f203a2SBill Pemberton (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0); 51452af9545SBill Pemberton 51552af9545SBill Pemberton mget_out: 51652af9545SBill Pemberton kfree(d); 51752af9545SBill Pemberton return r; 51852af9545SBill Pemberton } 51952af9545SBill Pemberton 52020b9d177SAlan Cox static int ssu100_tiocmset(struct tty_struct *tty, 52152af9545SBill Pemberton unsigned int set, unsigned int clear) 52252af9545SBill Pemberton { 52352af9545SBill Pemberton struct usb_serial_port *port = tty->driver_data; 52452af9545SBill Pemberton struct usb_device *dev = port->serial->dev; 52552af9545SBill Pemberton 52652af9545SBill Pemberton dbg("%s\n", __func__); 52752af9545SBill Pemberton return update_mctrl(dev, set, clear); 52852af9545SBill Pemberton } 52952af9545SBill Pemberton 53052af9545SBill Pemberton static void ssu100_dtr_rts(struct usb_serial_port *port, int on) 53152af9545SBill Pemberton { 53252af9545SBill Pemberton struct usb_device *dev = port->serial->dev; 53352af9545SBill Pemberton 53452af9545SBill Pemberton dbg("%s\n", __func__); 53552af9545SBill Pemberton 53652af9545SBill Pemberton mutex_lock(&port->serial->disc_mutex); 53752af9545SBill Pemberton if (!port->serial->disconnected) { 53852af9545SBill Pemberton /* Disable flow control */ 53952af9545SBill Pemberton if (!on && 540556f1a0eSBill Pemberton ssu100_setregister(dev, 0, UART_MCR, 0) < 0) 54152af9545SBill Pemberton dev_err(&port->dev, "error from flowcontrol urb\n"); 54252af9545SBill Pemberton /* drop RTS and DTR */ 54352af9545SBill Pemberton if (on) 54452af9545SBill Pemberton set_mctrl(dev, TIOCM_DTR | TIOCM_RTS); 54552af9545SBill Pemberton else 54652af9545SBill Pemberton clear_mctrl(dev, TIOCM_DTR | TIOCM_RTS); 54752af9545SBill Pemberton } 54852af9545SBill Pemberton mutex_unlock(&port->serial->disc_mutex); 54952af9545SBill Pemberton } 55052af9545SBill Pemberton 551f81c83dbSBill Pemberton static void ssu100_update_msr(struct usb_serial_port *port, u8 msr) 552f81c83dbSBill Pemberton { 553f81c83dbSBill Pemberton struct ssu100_port_private *priv = usb_get_serial_port_data(port); 554f81c83dbSBill Pemberton unsigned long flags; 555f81c83dbSBill Pemberton 556f81c83dbSBill Pemberton spin_lock_irqsave(&priv->status_lock, flags); 557f81c83dbSBill Pemberton priv->shadowMSR = msr; 558f81c83dbSBill Pemberton spin_unlock_irqrestore(&priv->status_lock, flags); 559f81c83dbSBill Pemberton 560f81c83dbSBill Pemberton if (msr & UART_MSR_ANY_DELTA) { 561f81c83dbSBill Pemberton /* update input line counters */ 562f81c83dbSBill Pemberton if (msr & UART_MSR_DCTS) 563f81c83dbSBill Pemberton priv->icount.cts++; 564f81c83dbSBill Pemberton if (msr & UART_MSR_DDSR) 565f81c83dbSBill Pemberton priv->icount.dsr++; 566f81c83dbSBill Pemberton if (msr & UART_MSR_DDCD) 567f81c83dbSBill Pemberton priv->icount.dcd++; 568f81c83dbSBill Pemberton if (msr & UART_MSR_TERI) 569f81c83dbSBill Pemberton priv->icount.rng++; 570f81c83dbSBill Pemberton wake_up_interruptible(&priv->delta_msr_wait); 571f81c83dbSBill Pemberton } 572f81c83dbSBill Pemberton } 573f81c83dbSBill Pemberton 5746b8f1ca5SBill Pemberton static void ssu100_update_lsr(struct usb_serial_port *port, u8 lsr, 5756b8f1ca5SBill Pemberton char *tty_flag) 576f81c83dbSBill Pemberton { 577f81c83dbSBill Pemberton struct ssu100_port_private *priv = usb_get_serial_port_data(port); 578f81c83dbSBill Pemberton unsigned long flags; 579f81c83dbSBill Pemberton 580f81c83dbSBill Pemberton spin_lock_irqsave(&priv->status_lock, flags); 581f81c83dbSBill Pemberton priv->shadowLSR = lsr; 582f81c83dbSBill Pemberton spin_unlock_irqrestore(&priv->status_lock, flags); 583f81c83dbSBill Pemberton 5846b8f1ca5SBill Pemberton *tty_flag = TTY_NORMAL; 585f81c83dbSBill Pemberton if (lsr & UART_LSR_BRK_ERROR_BITS) { 5866b8f1ca5SBill Pemberton /* we always want to update icount, but we only want to 5876b8f1ca5SBill Pemberton * update tty_flag for one case */ 5886b8f1ca5SBill Pemberton if (lsr & UART_LSR_BI) { 589f81c83dbSBill Pemberton priv->icount.brk++; 5906b8f1ca5SBill Pemberton *tty_flag = TTY_BREAK; 5916b8f1ca5SBill Pemberton usb_serial_handle_break(port); 592f81c83dbSBill Pemberton } 5936b8f1ca5SBill Pemberton if (lsr & UART_LSR_PE) { 5946b8f1ca5SBill Pemberton priv->icount.parity++; 5956b8f1ca5SBill Pemberton if (*tty_flag == TTY_NORMAL) 5966b8f1ca5SBill Pemberton *tty_flag = TTY_PARITY; 5976b8f1ca5SBill Pemberton } 5986b8f1ca5SBill Pemberton if (lsr & UART_LSR_FE) { 5996b8f1ca5SBill Pemberton priv->icount.frame++; 6006b8f1ca5SBill Pemberton if (*tty_flag == TTY_NORMAL) 6016b8f1ca5SBill Pemberton *tty_flag = TTY_FRAME; 6026b8f1ca5SBill Pemberton } 6036b8f1ca5SBill Pemberton if (lsr & UART_LSR_OE){ 6046b8f1ca5SBill Pemberton priv->icount.overrun++; 6056b8f1ca5SBill Pemberton if (*tty_flag == TTY_NORMAL) 6066b8f1ca5SBill Pemberton *tty_flag = TTY_OVERRUN; 6076b8f1ca5SBill Pemberton } 6086b8f1ca5SBill Pemberton } 6096b8f1ca5SBill Pemberton 610f81c83dbSBill Pemberton } 611f81c83dbSBill Pemberton 612f7043ecbSBill Pemberton static int ssu100_process_packet(struct urb *urb, 613f7043ecbSBill Pemberton struct tty_struct *tty) 61452af9545SBill Pemberton { 615f7043ecbSBill Pemberton struct usb_serial_port *port = urb->context; 616f7043ecbSBill Pemberton char *packet = (char *)urb->transfer_buffer; 6176b8f1ca5SBill Pemberton char flag = TTY_NORMAL; 618f7043ecbSBill Pemberton u32 len = urb->actual_length; 619f7043ecbSBill Pemberton int i; 62052af9545SBill Pemberton char *ch; 62152af9545SBill Pemberton 62252af9545SBill Pemberton dbg("%s - port %d", __func__, port->number); 62352af9545SBill Pemberton 6249b2cef31SBill Pemberton if ((len >= 4) && 6259b2cef31SBill Pemberton (packet[0] == 0x1b) && (packet[1] == 0x1b) && 62652af9545SBill Pemberton ((packet[2] == 0x00) || (packet[2] == 0x01))) { 6276b8f1ca5SBill Pemberton if (packet[2] == 0x00) { 6286b8f1ca5SBill Pemberton ssu100_update_lsr(port, packet[3], &flag); 6296b8f1ca5SBill Pemberton if (flag == TTY_OVERRUN) 6306b8f1ca5SBill Pemberton tty_insert_flip_char(tty, 0, TTY_OVERRUN); 6316b8f1ca5SBill Pemberton } 632f81c83dbSBill Pemberton if (packet[2] == 0x01) 633f81c83dbSBill Pemberton ssu100_update_msr(port, packet[3]); 63452af9545SBill Pemberton 63552af9545SBill Pemberton len -= 4; 63652af9545SBill Pemberton ch = packet + 4; 63752af9545SBill Pemberton } else 63852af9545SBill Pemberton ch = packet; 63952af9545SBill Pemberton 64052af9545SBill Pemberton if (!len) 64152af9545SBill Pemberton return 0; /* status only */ 64252af9545SBill Pemberton 64352af9545SBill Pemberton if (port->port.console && port->sysrq) { 64452af9545SBill Pemberton for (i = 0; i < len; i++, ch++) { 6456ee9f4b4SDmitry Torokhov if (!usb_serial_handle_sysrq_char(port, *ch)) 64652af9545SBill Pemberton tty_insert_flip_char(tty, *ch, flag); 64752af9545SBill Pemberton } 64852af9545SBill Pemberton } else 64952af9545SBill Pemberton tty_insert_flip_string_fixed_flag(tty, ch, flag, len); 65052af9545SBill Pemberton 65152af9545SBill Pemberton return len; 65252af9545SBill Pemberton } 65352af9545SBill Pemberton 65452af9545SBill Pemberton static void ssu100_process_read_urb(struct urb *urb) 65552af9545SBill Pemberton { 65652af9545SBill Pemberton struct usb_serial_port *port = urb->context; 65752af9545SBill Pemberton struct tty_struct *tty; 658f7043ecbSBill Pemberton int count; 65952af9545SBill Pemberton 66052af9545SBill Pemberton dbg("%s", __func__); 66152af9545SBill Pemberton 66252af9545SBill Pemberton tty = tty_port_tty_get(&port->port); 66352af9545SBill Pemberton if (!tty) 66452af9545SBill Pemberton return; 66552af9545SBill Pemberton 666f7043ecbSBill Pemberton count = ssu100_process_packet(urb, tty); 66752af9545SBill Pemberton 66852af9545SBill Pemberton if (count) 66952af9545SBill Pemberton tty_flip_buffer_push(tty); 67052af9545SBill Pemberton tty_kref_put(tty); 67152af9545SBill Pemberton } 67252af9545SBill Pemberton 67352af9545SBill Pemberton static struct usb_serial_driver ssu100_device = { 67452af9545SBill Pemberton .driver = { 67552af9545SBill Pemberton .owner = THIS_MODULE, 67652af9545SBill Pemberton .name = "ssu100", 67752af9545SBill Pemberton }, 67852af9545SBill Pemberton .description = DRIVER_DESC, 67952af9545SBill Pemberton .id_table = id_table, 68052af9545SBill Pemberton .usb_driver = &ssu100_driver, 68152af9545SBill Pemberton .num_ports = 1, 68252af9545SBill Pemberton .open = ssu100_open, 68352af9545SBill Pemberton .close = ssu100_close, 68452af9545SBill Pemberton .attach = ssu100_attach, 68552af9545SBill Pemberton .release = ssu100_release, 68652af9545SBill Pemberton .dtr_rts = ssu100_dtr_rts, 68752af9545SBill Pemberton .process_read_urb = ssu100_process_read_urb, 68852af9545SBill Pemberton .tiocmget = ssu100_tiocmget, 68952af9545SBill Pemberton .tiocmset = ssu100_tiocmset, 6900bca1b91SAlan Cox .get_icount = ssu100_get_icount, 69152af9545SBill Pemberton .ioctl = ssu100_ioctl, 69252af9545SBill Pemberton .set_termios = ssu100_set_termios, 69385dee135SBill Pemberton .disconnect = usb_serial_generic_disconnect, 69452af9545SBill Pemberton }; 69552af9545SBill Pemberton 69652af9545SBill Pemberton static int __init ssu100_init(void) 69752af9545SBill Pemberton { 69852af9545SBill Pemberton int retval; 69952af9545SBill Pemberton 70052af9545SBill Pemberton dbg("%s", __func__); 70152af9545SBill Pemberton 70252af9545SBill Pemberton /* register with usb-serial */ 70352af9545SBill Pemberton retval = usb_serial_register(&ssu100_device); 70452af9545SBill Pemberton 70552af9545SBill Pemberton if (retval) 70652af9545SBill Pemberton goto failed_usb_sio_register; 70752af9545SBill Pemberton 70852af9545SBill Pemberton retval = usb_register(&ssu100_driver); 70952af9545SBill Pemberton if (retval) 71052af9545SBill Pemberton goto failed_usb_register; 71152af9545SBill Pemberton 71252af9545SBill Pemberton printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":" 71352af9545SBill Pemberton DRIVER_DESC "\n"); 71452af9545SBill Pemberton 71552af9545SBill Pemberton return 0; 71652af9545SBill Pemberton 71752af9545SBill Pemberton failed_usb_register: 71852af9545SBill Pemberton usb_serial_deregister(&ssu100_device); 71952af9545SBill Pemberton failed_usb_sio_register: 72052af9545SBill Pemberton return retval; 72152af9545SBill Pemberton } 72252af9545SBill Pemberton 72352af9545SBill Pemberton static void __exit ssu100_exit(void) 72452af9545SBill Pemberton { 72552af9545SBill Pemberton usb_deregister(&ssu100_driver); 72652af9545SBill Pemberton usb_serial_deregister(&ssu100_device); 72752af9545SBill Pemberton } 72852af9545SBill Pemberton 72952af9545SBill Pemberton module_init(ssu100_init); 73052af9545SBill Pemberton module_exit(ssu100_exit); 73152af9545SBill Pemberton 73252af9545SBill Pemberton MODULE_DESCRIPTION(DRIVER_DESC); 73352af9545SBill Pemberton MODULE_LICENSE("GPL"); 73452af9545SBill Pemberton 73552af9545SBill Pemberton module_param(debug, bool, S_IRUGO | S_IWUSR); 73652af9545SBill Pemberton MODULE_PARM_DESC(debug, "Debug enabled or not"); 737