Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 1 | /* |
| 2 | * usb-serial driver for Quatech SSU-100 |
| 3 | * |
| 4 | * based on ftdi_sio.c and the original serqt_usb.c from Quatech |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <linux/errno.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/tty.h> |
| 12 | #include <linux/tty_driver.h> |
| 13 | #include <linux/tty_flip.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/serial.h> |
| 16 | #include <linux/usb.h> |
| 17 | #include <linux/usb/serial.h> |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 18 | #include <linux/serial_reg.h> |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 19 | #include <linux/uaccess.h> |
| 20 | |
| 21 | #define QT_OPEN_CLOSE_CHANNEL 0xca |
| 22 | #define QT_SET_GET_DEVICE 0xc2 |
| 23 | #define QT_SET_GET_REGISTER 0xc0 |
| 24 | #define QT_GET_SET_PREBUF_TRIG_LVL 0xcc |
| 25 | #define QT_SET_ATF 0xcd |
| 26 | #define QT_GET_SET_UART 0xc1 |
| 27 | #define QT_TRANSFER_IN 0xc0 |
| 28 | #define QT_HW_FLOW_CONTROL_MASK 0xc5 |
| 29 | #define QT_SW_FLOW_CONTROL_MASK 0xc6 |
| 30 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 31 | #define SERIAL_MSR_MASK 0xf0 |
| 32 | |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 33 | #define SERIAL_CRTSCTS ((UART_MCR_RTS << 8) | UART_MSR_CTS) |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 34 | |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 35 | #define SERIAL_EVEN_PARITY (UART_LCR_PARITY | UART_LCR_EPAR) |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 36 | |
| 37 | #define MAX_BAUD_RATE 460800 |
| 38 | |
| 39 | #define ATC_DISABLED 0x00 |
| 40 | #define DUPMODE_BITS 0xc0 |
| 41 | #define RR_BITS 0x03 |
| 42 | #define LOOPMODE_BITS 0x41 |
| 43 | #define RS232_MODE 0x00 |
| 44 | #define RTSCTS_TO_CONNECTOR 0x40 |
| 45 | #define CLKS_X4 0x02 |
| 46 | #define FULLPWRBIT 0x00000080 |
| 47 | #define NEXT_BOARD_POWER_BIT 0x00000004 |
| 48 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 49 | #define DRIVER_DESC "Quatech SSU-100 USB to Serial Driver" |
| 50 | |
| 51 | #define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */ |
| 52 | #define QUATECH_SSU100 0xC020 /* SSU100 */ |
| 53 | |
| 54 | static const struct usb_device_id id_table[] = { |
| 55 | {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU100)}, |
| 56 | {} /* Terminating entry */ |
| 57 | }; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 58 | MODULE_DEVICE_TABLE(usb, id_table); |
| 59 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 60 | struct ssu100_port_private { |
Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 61 | spinlock_t status_lock; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 62 | u8 shadowLSR; |
| 63 | u8 shadowMSR; |
| 64 | wait_queue_head_t delta_msr_wait; /* Used for TIOCMIWAIT */ |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 65 | struct async_icount icount; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 66 | }; |
| 67 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 68 | static inline int ssu100_control_msg(struct usb_device *dev, |
| 69 | u8 request, u16 data, u16 index) |
| 70 | { |
| 71 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 72 | request, 0x40, data, index, |
| 73 | NULL, 0, 300); |
| 74 | } |
| 75 | |
| 76 | static inline int ssu100_setdevice(struct usb_device *dev, u8 *data) |
| 77 | { |
| 78 | u16 x = ((u16)(data[1] << 8) | (u16)(data[0])); |
| 79 | |
| 80 | return ssu100_control_msg(dev, QT_SET_GET_DEVICE, x, 0); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | static inline int ssu100_getdevice(struct usb_device *dev, u8 *data) |
| 85 | { |
| 86 | return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 87 | QT_SET_GET_DEVICE, 0xc0, 0, 0, |
| 88 | data, 3, 300); |
| 89 | } |
| 90 | |
| 91 | static inline int ssu100_getregister(struct usb_device *dev, |
| 92 | unsigned short uart, |
| 93 | unsigned short reg, |
| 94 | u8 *data) |
| 95 | { |
| 96 | return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 97 | QT_SET_GET_REGISTER, 0xc0, reg, |
| 98 | uart, data, sizeof(*data), 300); |
| 99 | |
| 100 | } |
| 101 | |
| 102 | |
| 103 | static inline int ssu100_setregister(struct usb_device *dev, |
| 104 | unsigned short uart, |
Bill Pemberton | 556f1a0 | 2010-08-05 17:01:08 -0400 | [diff] [blame] | 105 | unsigned short reg, |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 106 | u16 data) |
| 107 | { |
Bill Pemberton | 556f1a0 | 2010-08-05 17:01:08 -0400 | [diff] [blame] | 108 | u16 value = (data << 8) | reg; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 109 | |
| 110 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 111 | QT_SET_GET_REGISTER, 0x40, value, uart, |
| 112 | NULL, 0, 300); |
| 113 | |
| 114 | } |
| 115 | |
| 116 | #define set_mctrl(dev, set) update_mctrl((dev), (set), 0) |
| 117 | #define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear)) |
| 118 | |
| 119 | /* these do not deal with device that have more than 1 port */ |
| 120 | static inline int update_mctrl(struct usb_device *dev, unsigned int set, |
| 121 | unsigned int clear) |
| 122 | { |
| 123 | unsigned urb_value; |
| 124 | int result; |
| 125 | |
| 126 | if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) { |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 127 | dev_dbg(&dev->dev, "%s - DTR|RTS not being set|cleared\n", __func__); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 128 | return 0; /* no change */ |
| 129 | } |
| 130 | |
| 131 | clear &= ~set; /* 'set' takes precedence over 'clear' */ |
| 132 | urb_value = 0; |
| 133 | if (set & TIOCM_DTR) |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 134 | urb_value |= UART_MCR_DTR; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 135 | if (set & TIOCM_RTS) |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 136 | urb_value |= UART_MCR_RTS; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 137 | |
Bill Pemberton | 556f1a0 | 2010-08-05 17:01:08 -0400 | [diff] [blame] | 138 | result = ssu100_setregister(dev, 0, UART_MCR, urb_value); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 139 | if (result < 0) |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 140 | dev_dbg(&dev->dev, "%s Error from MODEM_CTRL urb\n", __func__); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 141 | |
| 142 | return result; |
| 143 | } |
| 144 | |
| 145 | static int ssu100_initdevice(struct usb_device *dev) |
| 146 | { |
| 147 | u8 *data; |
| 148 | int result = 0; |
| 149 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 150 | data = kzalloc(3, GFP_KERNEL); |
| 151 | if (!data) |
| 152 | return -ENOMEM; |
| 153 | |
| 154 | result = ssu100_getdevice(dev, data); |
| 155 | if (result < 0) { |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 156 | dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 157 | goto out; |
| 158 | } |
| 159 | |
| 160 | data[1] &= ~FULLPWRBIT; |
| 161 | |
| 162 | result = ssu100_setdevice(dev, data); |
| 163 | if (result < 0) { |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 164 | dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 165 | goto out; |
| 166 | } |
| 167 | |
| 168 | result = ssu100_control_msg(dev, QT_GET_SET_PREBUF_TRIG_LVL, 128, 0); |
| 169 | if (result < 0) { |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 170 | dev_dbg(&dev->dev, "%s - set prebuffer level failed %i\n", __func__, result); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 171 | goto out; |
| 172 | } |
| 173 | |
| 174 | result = ssu100_control_msg(dev, QT_SET_ATF, ATC_DISABLED, 0); |
| 175 | if (result < 0) { |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 176 | dev_dbg(&dev->dev, "%s - set ATFprebuffer level failed %i\n", __func__, result); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 177 | goto out; |
| 178 | } |
| 179 | |
| 180 | result = ssu100_getdevice(dev, data); |
| 181 | if (result < 0) { |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 182 | dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 183 | goto out; |
| 184 | } |
| 185 | |
| 186 | data[0] &= ~(RR_BITS | DUPMODE_BITS); |
| 187 | data[0] |= CLKS_X4; |
| 188 | data[1] &= ~(LOOPMODE_BITS); |
| 189 | data[1] |= RS232_MODE; |
| 190 | |
| 191 | result = ssu100_setdevice(dev, data); |
| 192 | if (result < 0) { |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 193 | dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 194 | goto out; |
| 195 | } |
| 196 | |
| 197 | out: kfree(data); |
| 198 | return result; |
| 199 | |
| 200 | } |
| 201 | |
| 202 | |
| 203 | static void ssu100_set_termios(struct tty_struct *tty, |
| 204 | struct usb_serial_port *port, |
| 205 | struct ktermios *old_termios) |
| 206 | { |
| 207 | struct usb_device *dev = port->serial->dev; |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 208 | struct ktermios *termios = &tty->termios; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 209 | u16 baud, divisor, remainder; |
| 210 | unsigned int cflag = termios->c_cflag; |
| 211 | u16 urb_value = 0; /* will hold the new flags */ |
| 212 | int result; |
| 213 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 214 | if (cflag & PARENB) { |
| 215 | if (cflag & PARODD) |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 216 | urb_value |= UART_LCR_PARITY; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 217 | else |
| 218 | urb_value |= SERIAL_EVEN_PARITY; |
| 219 | } |
| 220 | |
| 221 | switch (cflag & CSIZE) { |
| 222 | case CS5: |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 223 | urb_value |= UART_LCR_WLEN5; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 224 | break; |
| 225 | case CS6: |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 226 | urb_value |= UART_LCR_WLEN6; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 227 | break; |
| 228 | case CS7: |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 229 | urb_value |= UART_LCR_WLEN7; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 230 | break; |
| 231 | default: |
| 232 | case CS8: |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 233 | urb_value |= UART_LCR_WLEN8; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 234 | break; |
| 235 | } |
| 236 | |
| 237 | baud = tty_get_baud_rate(tty); |
| 238 | if (!baud) |
| 239 | baud = 9600; |
| 240 | |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 241 | dev_dbg(&port->dev, "%s - got baud = %d\n", __func__, baud); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 242 | |
| 243 | |
| 244 | divisor = MAX_BAUD_RATE / baud; |
| 245 | remainder = MAX_BAUD_RATE % baud; |
| 246 | if (((remainder * 2) >= baud) && (baud != 110)) |
| 247 | divisor++; |
| 248 | |
| 249 | urb_value = urb_value << 8; |
| 250 | |
| 251 | result = ssu100_control_msg(dev, QT_GET_SET_UART, divisor, urb_value); |
| 252 | if (result < 0) |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 253 | dev_dbg(&port->dev, "%s - set uart failed\n", __func__); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 254 | |
| 255 | if (cflag & CRTSCTS) |
| 256 | result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK, |
| 257 | SERIAL_CRTSCTS, 0); |
| 258 | else |
| 259 | result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK, |
| 260 | 0, 0); |
| 261 | if (result < 0) |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 262 | dev_dbg(&port->dev, "%s - set HW flow control failed\n", __func__); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 263 | |
| 264 | if (I_IXOFF(tty) || I_IXON(tty)) { |
| 265 | u16 x = ((u16)(START_CHAR(tty) << 8) | (u16)(STOP_CHAR(tty))); |
| 266 | |
| 267 | result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK, |
| 268 | x, 0); |
| 269 | } else |
| 270 | result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK, |
| 271 | 0, 0); |
| 272 | |
| 273 | if (result < 0) |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 274 | dev_dbg(&port->dev, "%s - set SW flow control failed\n", __func__); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 275 | |
| 276 | } |
| 277 | |
| 278 | |
| 279 | static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port) |
| 280 | { |
| 281 | struct usb_device *dev = port->serial->dev; |
| 282 | struct ssu100_port_private *priv = usb_get_serial_port_data(port); |
| 283 | u8 *data; |
| 284 | int result; |
Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 285 | unsigned long flags; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 286 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 287 | data = kzalloc(2, GFP_KERNEL); |
| 288 | if (!data) |
| 289 | return -ENOMEM; |
| 290 | |
| 291 | result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 292 | QT_OPEN_CLOSE_CHANNEL, |
| 293 | QT_TRANSFER_IN, 0x01, |
| 294 | 0, data, 2, 300); |
| 295 | if (result < 0) { |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 296 | dev_dbg(&port->dev, "%s - open failed %i\n", __func__, result); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 297 | kfree(data); |
| 298 | return result; |
| 299 | } |
| 300 | |
Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 301 | spin_lock_irqsave(&priv->status_lock, flags); |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 302 | priv->shadowLSR = data[0]; |
| 303 | priv->shadowMSR = data[1]; |
Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 304 | spin_unlock_irqrestore(&priv->status_lock, flags); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 305 | |
| 306 | kfree(data); |
| 307 | |
| 308 | /* set to 9600 */ |
| 309 | result = ssu100_control_msg(dev, QT_GET_SET_UART, 0x30, 0x0300); |
| 310 | if (result < 0) |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 311 | dev_dbg(&port->dev, "%s - set uart failed\n", __func__); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 312 | |
| 313 | if (tty) |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 314 | ssu100_set_termios(tty, port, &tty->termios); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 315 | |
| 316 | return usb_serial_generic_open(tty, port); |
| 317 | } |
| 318 | |
| 319 | static void ssu100_close(struct usb_serial_port *port) |
| 320 | { |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 321 | usb_serial_generic_close(port); |
| 322 | } |
| 323 | |
| 324 | static int get_serial_info(struct usb_serial_port *port, |
| 325 | struct serial_struct __user *retinfo) |
| 326 | { |
| 327 | struct serial_struct tmp; |
| 328 | |
| 329 | if (!retinfo) |
| 330 | return -EFAULT; |
| 331 | |
| 332 | memset(&tmp, 0, sizeof(tmp)); |
| 333 | tmp.line = port->serial->minor; |
| 334 | tmp.port = 0; |
| 335 | tmp.irq = 0; |
| 336 | tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; |
| 337 | tmp.xmit_fifo_size = port->bulk_out_size; |
| 338 | tmp.baud_base = 9600; |
| 339 | tmp.close_delay = 5*HZ; |
| 340 | tmp.closing_wait = 30*HZ; |
| 341 | |
| 342 | if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) |
| 343 | return -EFAULT; |
| 344 | return 0; |
| 345 | } |
| 346 | |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 347 | static int wait_modem_info(struct usb_serial_port *port, unsigned int arg) |
| 348 | { |
| 349 | struct ssu100_port_private *priv = usb_get_serial_port_data(port); |
| 350 | struct async_icount prev, cur; |
| 351 | unsigned long flags; |
| 352 | |
| 353 | spin_lock_irqsave(&priv->status_lock, flags); |
| 354 | prev = priv->icount; |
| 355 | spin_unlock_irqrestore(&priv->status_lock, flags); |
| 356 | |
| 357 | while (1) { |
| 358 | wait_event_interruptible(priv->delta_msr_wait, |
| 359 | ((priv->icount.rng != prev.rng) || |
| 360 | (priv->icount.dsr != prev.dsr) || |
| 361 | (priv->icount.dcd != prev.dcd) || |
| 362 | (priv->icount.cts != prev.cts))); |
| 363 | |
| 364 | if (signal_pending(current)) |
| 365 | return -ERESTARTSYS; |
| 366 | |
| 367 | spin_lock_irqsave(&priv->status_lock, flags); |
| 368 | cur = priv->icount; |
| 369 | spin_unlock_irqrestore(&priv->status_lock, flags); |
| 370 | |
| 371 | if ((prev.rng == cur.rng) && |
| 372 | (prev.dsr == cur.dsr) && |
| 373 | (prev.dcd == cur.dcd) && |
| 374 | (prev.cts == cur.cts)) |
| 375 | return -EIO; |
| 376 | |
| 377 | if ((arg & TIOCM_RNG && (prev.rng != cur.rng)) || |
| 378 | (arg & TIOCM_DSR && (prev.dsr != cur.dsr)) || |
| 379 | (arg & TIOCM_CD && (prev.dcd != cur.dcd)) || |
| 380 | (arg & TIOCM_CTS && (prev.cts != cur.cts))) |
| 381 | return 0; |
| 382 | } |
| 383 | return 0; |
| 384 | } |
| 385 | |
Alan Cox | 0bca1b9 | 2010-09-16 18:21:40 +0100 | [diff] [blame] | 386 | static int ssu100_get_icount(struct tty_struct *tty, |
| 387 | struct serial_icounter_struct *icount) |
| 388 | { |
| 389 | struct usb_serial_port *port = tty->driver_data; |
| 390 | struct ssu100_port_private *priv = usb_get_serial_port_data(port); |
| 391 | struct async_icount cnow = priv->icount; |
| 392 | |
| 393 | icount->cts = cnow.cts; |
| 394 | icount->dsr = cnow.dsr; |
| 395 | icount->rng = cnow.rng; |
| 396 | icount->dcd = cnow.dcd; |
| 397 | icount->rx = cnow.rx; |
| 398 | icount->tx = cnow.tx; |
| 399 | icount->frame = cnow.frame; |
| 400 | icount->overrun = cnow.overrun; |
| 401 | icount->parity = cnow.parity; |
| 402 | icount->brk = cnow.brk; |
| 403 | icount->buf_overrun = cnow.buf_overrun; |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | |
| 409 | |
Alan Cox | 00a0d0d | 2011-02-14 16:27:06 +0000 | [diff] [blame] | 410 | static int ssu100_ioctl(struct tty_struct *tty, |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 411 | unsigned int cmd, unsigned long arg) |
| 412 | { |
| 413 | struct usb_serial_port *port = tty->driver_data; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 414 | |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 415 | dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 416 | |
| 417 | switch (cmd) { |
| 418 | case TIOCGSERIAL: |
| 419 | return get_serial_info(port, |
| 420 | (struct serial_struct __user *) arg); |
| 421 | |
| 422 | case TIOCMIWAIT: |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 423 | return wait_modem_info(port, arg); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 424 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 425 | default: |
| 426 | break; |
| 427 | } |
| 428 | |
Greg Kroah-Hartman | 4f0c641 | 2012-09-14 11:50:35 -0700 | [diff] [blame] | 429 | dev_dbg(&port->dev, "%s arg not supported\n", __func__); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 430 | |
| 431 | return -ENOIOCTLCMD; |
| 432 | } |
| 433 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 434 | static int ssu100_attach(struct usb_serial *serial) |
| 435 | { |
Johan Hovold | 638b9e1 | 2012-10-17 16:31:34 +0200 | [diff] [blame] | 436 | return ssu100_initdevice(serial->dev); |
| 437 | } |
| 438 | |
| 439 | static int ssu100_port_probe(struct usb_serial_port *port) |
| 440 | { |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 441 | struct ssu100_port_private *priv; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 442 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 443 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
Johan Hovold | 638b9e1 | 2012-10-17 16:31:34 +0200 | [diff] [blame] | 444 | if (!priv) |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 445 | return -ENOMEM; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 446 | |
Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 447 | spin_lock_init(&priv->status_lock); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 448 | init_waitqueue_head(&priv->delta_msr_wait); |
Johan Hovold | 638b9e1 | 2012-10-17 16:31:34 +0200 | [diff] [blame] | 449 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 450 | usb_set_serial_port_data(port, priv); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 451 | |
Johan Hovold | 638b9e1 | 2012-10-17 16:31:34 +0200 | [diff] [blame] | 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | static int ssu100_port_remove(struct usb_serial_port *port) |
| 456 | { |
| 457 | struct ssu100_port_private *priv; |
| 458 | |
| 459 | priv = usb_get_serial_port_data(port); |
| 460 | kfree(priv); |
| 461 | |
| 462 | return 0; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 463 | } |
| 464 | |
Alan Cox | 60b33c1 | 2011-02-14 16:26:14 +0000 | [diff] [blame] | 465 | static int ssu100_tiocmget(struct tty_struct *tty) |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 466 | { |
| 467 | struct usb_serial_port *port = tty->driver_data; |
| 468 | struct usb_device *dev = port->serial->dev; |
| 469 | u8 *d; |
| 470 | int r; |
| 471 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 472 | d = kzalloc(2, GFP_KERNEL); |
| 473 | if (!d) |
| 474 | return -ENOMEM; |
| 475 | |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 476 | r = ssu100_getregister(dev, 0, UART_MCR, d); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 477 | if (r < 0) |
| 478 | goto mget_out; |
| 479 | |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 480 | r = ssu100_getregister(dev, 0, UART_MSR, d+1); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 481 | if (r < 0) |
| 482 | goto mget_out; |
| 483 | |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 484 | r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) | |
| 485 | (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) | |
| 486 | (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) | |
| 487 | (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) | |
| 488 | (d[1] & UART_MSR_RI ? TIOCM_RI : 0) | |
| 489 | (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 490 | |
| 491 | mget_out: |
| 492 | kfree(d); |
| 493 | return r; |
| 494 | } |
| 495 | |
Alan Cox | 20b9d17 | 2011-02-14 16:26:50 +0000 | [diff] [blame] | 496 | static int ssu100_tiocmset(struct tty_struct *tty, |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 497 | unsigned int set, unsigned int clear) |
| 498 | { |
| 499 | struct usb_serial_port *port = tty->driver_data; |
| 500 | struct usb_device *dev = port->serial->dev; |
| 501 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 502 | return update_mctrl(dev, set, clear); |
| 503 | } |
| 504 | |
| 505 | static void ssu100_dtr_rts(struct usb_serial_port *port, int on) |
| 506 | { |
| 507 | struct usb_device *dev = port->serial->dev; |
| 508 | |
Johan Hovold | b2ca699 | 2013-02-13 17:53:28 +0100 | [diff] [blame^] | 509 | /* Disable flow control */ |
| 510 | if (!on) { |
| 511 | if (ssu100_setregister(dev, 0, UART_MCR, 0) < 0) |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 512 | dev_err(&port->dev, "error from flowcontrol urb\n"); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 513 | } |
Johan Hovold | b2ca699 | 2013-02-13 17:53:28 +0100 | [diff] [blame^] | 514 | /* drop RTS and DTR */ |
| 515 | if (on) |
| 516 | set_mctrl(dev, TIOCM_DTR | TIOCM_RTS); |
| 517 | else |
| 518 | clear_mctrl(dev, TIOCM_DTR | TIOCM_RTS); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 519 | } |
| 520 | |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 521 | static void ssu100_update_msr(struct usb_serial_port *port, u8 msr) |
| 522 | { |
| 523 | struct ssu100_port_private *priv = usb_get_serial_port_data(port); |
| 524 | unsigned long flags; |
| 525 | |
| 526 | spin_lock_irqsave(&priv->status_lock, flags); |
| 527 | priv->shadowMSR = msr; |
| 528 | spin_unlock_irqrestore(&priv->status_lock, flags); |
| 529 | |
| 530 | if (msr & UART_MSR_ANY_DELTA) { |
| 531 | /* update input line counters */ |
| 532 | if (msr & UART_MSR_DCTS) |
| 533 | priv->icount.cts++; |
| 534 | if (msr & UART_MSR_DDSR) |
| 535 | priv->icount.dsr++; |
| 536 | if (msr & UART_MSR_DDCD) |
| 537 | priv->icount.dcd++; |
| 538 | if (msr & UART_MSR_TERI) |
| 539 | priv->icount.rng++; |
| 540 | wake_up_interruptible(&priv->delta_msr_wait); |
| 541 | } |
| 542 | } |
| 543 | |
Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 544 | static void ssu100_update_lsr(struct usb_serial_port *port, u8 lsr, |
| 545 | char *tty_flag) |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 546 | { |
| 547 | struct ssu100_port_private *priv = usb_get_serial_port_data(port); |
| 548 | unsigned long flags; |
| 549 | |
| 550 | spin_lock_irqsave(&priv->status_lock, flags); |
| 551 | priv->shadowLSR = lsr; |
| 552 | spin_unlock_irqrestore(&priv->status_lock, flags); |
| 553 | |
Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 554 | *tty_flag = TTY_NORMAL; |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 555 | if (lsr & UART_LSR_BRK_ERROR_BITS) { |
Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 556 | /* we always want to update icount, but we only want to |
| 557 | * update tty_flag for one case */ |
| 558 | if (lsr & UART_LSR_BI) { |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 559 | priv->icount.brk++; |
Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 560 | *tty_flag = TTY_BREAK; |
| 561 | usb_serial_handle_break(port); |
| 562 | } |
| 563 | if (lsr & UART_LSR_PE) { |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 564 | priv->icount.parity++; |
Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 565 | if (*tty_flag == TTY_NORMAL) |
| 566 | *tty_flag = TTY_PARITY; |
| 567 | } |
| 568 | if (lsr & UART_LSR_FE) { |
| 569 | priv->icount.frame++; |
| 570 | if (*tty_flag == TTY_NORMAL) |
| 571 | *tty_flag = TTY_FRAME; |
| 572 | } |
| 573 | if (lsr & UART_LSR_OE){ |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 574 | priv->icount.overrun++; |
Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 575 | if (*tty_flag == TTY_NORMAL) |
| 576 | *tty_flag = TTY_OVERRUN; |
| 577 | } |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 578 | } |
Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 579 | |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 580 | } |
| 581 | |
Bill Pemberton | f7043ec | 2010-10-21 14:43:05 -0400 | [diff] [blame] | 582 | static int ssu100_process_packet(struct urb *urb, |
| 583 | struct tty_struct *tty) |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 584 | { |
Bill Pemberton | f7043ec | 2010-10-21 14:43:05 -0400 | [diff] [blame] | 585 | struct usb_serial_port *port = urb->context; |
| 586 | char *packet = (char *)urb->transfer_buffer; |
Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 587 | char flag = TTY_NORMAL; |
Bill Pemberton | f7043ec | 2010-10-21 14:43:05 -0400 | [diff] [blame] | 588 | u32 len = urb->actual_length; |
| 589 | int i; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 590 | char *ch; |
| 591 | |
Bill Pemberton | 9b2cef3 | 2010-08-05 17:01:06 -0400 | [diff] [blame] | 592 | if ((len >= 4) && |
| 593 | (packet[0] == 0x1b) && (packet[1] == 0x1b) && |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 594 | ((packet[2] == 0x00) || (packet[2] == 0x01))) { |
Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 595 | if (packet[2] == 0x00) { |
| 596 | ssu100_update_lsr(port, packet[3], &flag); |
| 597 | if (flag == TTY_OVERRUN) |
| 598 | tty_insert_flip_char(tty, 0, TTY_OVERRUN); |
| 599 | } |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 600 | if (packet[2] == 0x01) |
| 601 | ssu100_update_msr(port, packet[3]); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 602 | |
| 603 | len -= 4; |
| 604 | ch = packet + 4; |
| 605 | } else |
| 606 | ch = packet; |
| 607 | |
| 608 | if (!len) |
| 609 | return 0; /* status only */ |
| 610 | |
| 611 | if (port->port.console && port->sysrq) { |
| 612 | for (i = 0; i < len; i++, ch++) { |
Dmitry Torokhov | 6ee9f4b | 2010-08-17 21:15:47 -0700 | [diff] [blame] | 613 | if (!usb_serial_handle_sysrq_char(port, *ch)) |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 614 | tty_insert_flip_char(tty, *ch, flag); |
| 615 | } |
| 616 | } else |
| 617 | tty_insert_flip_string_fixed_flag(tty, ch, flag, len); |
| 618 | |
| 619 | return len; |
| 620 | } |
| 621 | |
| 622 | static void ssu100_process_read_urb(struct urb *urb) |
| 623 | { |
| 624 | struct usb_serial_port *port = urb->context; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 625 | struct tty_struct *tty; |
Bill Pemberton | f7043ec | 2010-10-21 14:43:05 -0400 | [diff] [blame] | 626 | int count; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 627 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 628 | tty = tty_port_tty_get(&port->port); |
| 629 | if (!tty) |
| 630 | return; |
| 631 | |
Bill Pemberton | f7043ec | 2010-10-21 14:43:05 -0400 | [diff] [blame] | 632 | count = ssu100_process_packet(urb, tty); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 633 | |
| 634 | if (count) |
| 635 | tty_flip_buffer_push(tty); |
| 636 | tty_kref_put(tty); |
| 637 | } |
| 638 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 639 | static struct usb_serial_driver ssu100_device = { |
| 640 | .driver = { |
| 641 | .owner = THIS_MODULE, |
| 642 | .name = "ssu100", |
| 643 | }, |
| 644 | .description = DRIVER_DESC, |
| 645 | .id_table = id_table, |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 646 | .num_ports = 1, |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 647 | .open = ssu100_open, |
| 648 | .close = ssu100_close, |
| 649 | .attach = ssu100_attach, |
Johan Hovold | 638b9e1 | 2012-10-17 16:31:34 +0200 | [diff] [blame] | 650 | .port_probe = ssu100_port_probe, |
| 651 | .port_remove = ssu100_port_remove, |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 652 | .dtr_rts = ssu100_dtr_rts, |
| 653 | .process_read_urb = ssu100_process_read_urb, |
| 654 | .tiocmget = ssu100_tiocmget, |
| 655 | .tiocmset = ssu100_tiocmset, |
Alan Cox | 0bca1b9 | 2010-09-16 18:21:40 +0100 | [diff] [blame] | 656 | .get_icount = ssu100_get_icount, |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 657 | .ioctl = ssu100_ioctl, |
| 658 | .set_termios = ssu100_set_termios, |
Bill Pemberton | 85dee13 | 2010-08-05 17:01:11 -0400 | [diff] [blame] | 659 | .disconnect = usb_serial_generic_disconnect, |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 660 | }; |
| 661 | |
Alan Stern | d860322 | 2012-02-23 14:57:25 -0500 | [diff] [blame] | 662 | static struct usb_serial_driver * const serial_drivers[] = { |
| 663 | &ssu100_device, NULL |
| 664 | }; |
| 665 | |
Greg Kroah-Hartman | 68e2411 | 2012-05-08 15:46:14 -0700 | [diff] [blame] | 666 | module_usb_serial_driver(serial_drivers, id_table); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 667 | |
| 668 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 669 | MODULE_LICENSE("GPL"); |