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 | |
| 49 | static int debug = 1; |
| 50 | |
| 51 | /* Version Information */ |
| 52 | #define DRIVER_VERSION "v0.1" |
| 53 | #define DRIVER_DESC "Quatech SSU-100 USB to Serial Driver" |
| 54 | |
| 55 | #define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */ |
| 56 | #define QUATECH_SSU100 0xC020 /* SSU100 */ |
| 57 | |
| 58 | static const struct usb_device_id id_table[] = { |
| 59 | {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU100)}, |
| 60 | {} /* Terminating entry */ |
| 61 | }; |
| 62 | |
| 63 | MODULE_DEVICE_TABLE(usb, id_table); |
| 64 | |
| 65 | |
| 66 | static struct usb_driver ssu100_driver = { |
| 67 | .name = "ssu100", |
| 68 | .probe = usb_serial_probe, |
| 69 | .disconnect = usb_serial_disconnect, |
| 70 | .id_table = id_table, |
| 71 | .suspend = usb_serial_suspend, |
| 72 | .resume = usb_serial_resume, |
| 73 | .no_dynamic_id = 1, |
| 74 | .supports_autosuspend = 1, |
| 75 | }; |
| 76 | |
| 77 | struct ssu100_port_private { |
Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 78 | spinlock_t status_lock; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 79 | u8 shadowLSR; |
| 80 | u8 shadowMSR; |
| 81 | wait_queue_head_t delta_msr_wait; /* Used for TIOCMIWAIT */ |
| 82 | unsigned short max_packet_size; |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame^] | 83 | struct async_icount icount; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | static void ssu100_release(struct usb_serial *serial) |
| 87 | { |
| 88 | struct ssu100_port_private *priv = usb_get_serial_port_data(*serial->port); |
| 89 | |
| 90 | dbg("%s", __func__); |
| 91 | kfree(priv); |
| 92 | } |
| 93 | |
| 94 | static inline int ssu100_control_msg(struct usb_device *dev, |
| 95 | u8 request, u16 data, u16 index) |
| 96 | { |
| 97 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 98 | request, 0x40, data, index, |
| 99 | NULL, 0, 300); |
| 100 | } |
| 101 | |
| 102 | static inline int ssu100_setdevice(struct usb_device *dev, u8 *data) |
| 103 | { |
| 104 | u16 x = ((u16)(data[1] << 8) | (u16)(data[0])); |
| 105 | |
| 106 | return ssu100_control_msg(dev, QT_SET_GET_DEVICE, x, 0); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | static inline int ssu100_getdevice(struct usb_device *dev, u8 *data) |
| 111 | { |
| 112 | return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 113 | QT_SET_GET_DEVICE, 0xc0, 0, 0, |
| 114 | data, 3, 300); |
| 115 | } |
| 116 | |
| 117 | static inline int ssu100_getregister(struct usb_device *dev, |
| 118 | unsigned short uart, |
| 119 | unsigned short reg, |
| 120 | u8 *data) |
| 121 | { |
| 122 | return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 123 | QT_SET_GET_REGISTER, 0xc0, reg, |
| 124 | uart, data, sizeof(*data), 300); |
| 125 | |
| 126 | } |
| 127 | |
| 128 | |
| 129 | static inline int ssu100_setregister(struct usb_device *dev, |
| 130 | unsigned short uart, |
Bill Pemberton | 556f1a0 | 2010-08-05 17:01:08 -0400 | [diff] [blame] | 131 | unsigned short reg, |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 132 | u16 data) |
| 133 | { |
Bill Pemberton | 556f1a0 | 2010-08-05 17:01:08 -0400 | [diff] [blame] | 134 | u16 value = (data << 8) | reg; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 135 | |
| 136 | return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 137 | QT_SET_GET_REGISTER, 0x40, value, uart, |
| 138 | NULL, 0, 300); |
| 139 | |
| 140 | } |
| 141 | |
| 142 | #define set_mctrl(dev, set) update_mctrl((dev), (set), 0) |
| 143 | #define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear)) |
| 144 | |
| 145 | /* these do not deal with device that have more than 1 port */ |
| 146 | static inline int update_mctrl(struct usb_device *dev, unsigned int set, |
| 147 | unsigned int clear) |
| 148 | { |
| 149 | unsigned urb_value; |
| 150 | int result; |
| 151 | |
| 152 | if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) { |
| 153 | dbg("%s - DTR|RTS not being set|cleared", __func__); |
| 154 | return 0; /* no change */ |
| 155 | } |
| 156 | |
| 157 | clear &= ~set; /* 'set' takes precedence over 'clear' */ |
| 158 | urb_value = 0; |
| 159 | if (set & TIOCM_DTR) |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 160 | urb_value |= UART_MCR_DTR; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 161 | if (set & TIOCM_RTS) |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 162 | urb_value |= UART_MCR_RTS; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 163 | |
Bill Pemberton | 556f1a0 | 2010-08-05 17:01:08 -0400 | [diff] [blame] | 164 | result = ssu100_setregister(dev, 0, UART_MCR, urb_value); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 165 | if (result < 0) |
| 166 | dbg("%s Error from MODEM_CTRL urb", __func__); |
| 167 | |
| 168 | return result; |
| 169 | } |
| 170 | |
| 171 | static int ssu100_initdevice(struct usb_device *dev) |
| 172 | { |
| 173 | u8 *data; |
| 174 | int result = 0; |
| 175 | |
| 176 | dbg("%s", __func__); |
| 177 | |
| 178 | data = kzalloc(3, GFP_KERNEL); |
| 179 | if (!data) |
| 180 | return -ENOMEM; |
| 181 | |
| 182 | result = ssu100_getdevice(dev, data); |
| 183 | if (result < 0) { |
| 184 | dbg("%s - get_device failed %i", __func__, result); |
| 185 | goto out; |
| 186 | } |
| 187 | |
| 188 | data[1] &= ~FULLPWRBIT; |
| 189 | |
| 190 | result = ssu100_setdevice(dev, data); |
| 191 | if (result < 0) { |
| 192 | dbg("%s - setdevice failed %i", __func__, result); |
| 193 | goto out; |
| 194 | } |
| 195 | |
| 196 | result = ssu100_control_msg(dev, QT_GET_SET_PREBUF_TRIG_LVL, 128, 0); |
| 197 | if (result < 0) { |
| 198 | dbg("%s - set prebuffer level failed %i", __func__, result); |
| 199 | goto out; |
| 200 | } |
| 201 | |
| 202 | result = ssu100_control_msg(dev, QT_SET_ATF, ATC_DISABLED, 0); |
| 203 | if (result < 0) { |
| 204 | dbg("%s - set ATFprebuffer level failed %i", __func__, result); |
| 205 | goto out; |
| 206 | } |
| 207 | |
| 208 | result = ssu100_getdevice(dev, data); |
| 209 | if (result < 0) { |
| 210 | dbg("%s - get_device failed %i", __func__, result); |
| 211 | goto out; |
| 212 | } |
| 213 | |
| 214 | data[0] &= ~(RR_BITS | DUPMODE_BITS); |
| 215 | data[0] |= CLKS_X4; |
| 216 | data[1] &= ~(LOOPMODE_BITS); |
| 217 | data[1] |= RS232_MODE; |
| 218 | |
| 219 | result = ssu100_setdevice(dev, data); |
| 220 | if (result < 0) { |
| 221 | dbg("%s - setdevice failed %i", __func__, result); |
| 222 | goto out; |
| 223 | } |
| 224 | |
| 225 | out: kfree(data); |
| 226 | return result; |
| 227 | |
| 228 | } |
| 229 | |
| 230 | |
| 231 | static void ssu100_set_termios(struct tty_struct *tty, |
| 232 | struct usb_serial_port *port, |
| 233 | struct ktermios *old_termios) |
| 234 | { |
| 235 | struct usb_device *dev = port->serial->dev; |
| 236 | struct ktermios *termios = tty->termios; |
| 237 | u16 baud, divisor, remainder; |
| 238 | unsigned int cflag = termios->c_cflag; |
| 239 | u16 urb_value = 0; /* will hold the new flags */ |
| 240 | int result; |
| 241 | |
| 242 | dbg("%s", __func__); |
| 243 | |
| 244 | if (cflag & PARENB) { |
| 245 | if (cflag & PARODD) |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 246 | urb_value |= UART_LCR_PARITY; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 247 | else |
| 248 | urb_value |= SERIAL_EVEN_PARITY; |
| 249 | } |
| 250 | |
| 251 | switch (cflag & CSIZE) { |
| 252 | case CS5: |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 253 | urb_value |= UART_LCR_WLEN5; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 254 | break; |
| 255 | case CS6: |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 256 | urb_value |= UART_LCR_WLEN6; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 257 | break; |
| 258 | case CS7: |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 259 | urb_value |= UART_LCR_WLEN7; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 260 | break; |
| 261 | default: |
| 262 | case CS8: |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 263 | urb_value |= UART_LCR_WLEN8; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 264 | break; |
| 265 | } |
| 266 | |
| 267 | baud = tty_get_baud_rate(tty); |
| 268 | if (!baud) |
| 269 | baud = 9600; |
| 270 | |
| 271 | dbg("%s - got baud = %d\n", __func__, baud); |
| 272 | |
| 273 | |
| 274 | divisor = MAX_BAUD_RATE / baud; |
| 275 | remainder = MAX_BAUD_RATE % baud; |
| 276 | if (((remainder * 2) >= baud) && (baud != 110)) |
| 277 | divisor++; |
| 278 | |
| 279 | urb_value = urb_value << 8; |
| 280 | |
| 281 | result = ssu100_control_msg(dev, QT_GET_SET_UART, divisor, urb_value); |
| 282 | if (result < 0) |
| 283 | dbg("%s - set uart failed", __func__); |
| 284 | |
| 285 | if (cflag & CRTSCTS) |
| 286 | result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK, |
| 287 | SERIAL_CRTSCTS, 0); |
| 288 | else |
| 289 | result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK, |
| 290 | 0, 0); |
| 291 | if (result < 0) |
| 292 | dbg("%s - set HW flow control failed", __func__); |
| 293 | |
| 294 | if (I_IXOFF(tty) || I_IXON(tty)) { |
| 295 | u16 x = ((u16)(START_CHAR(tty) << 8) | (u16)(STOP_CHAR(tty))); |
| 296 | |
| 297 | result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK, |
| 298 | x, 0); |
| 299 | } else |
| 300 | result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK, |
| 301 | 0, 0); |
| 302 | |
| 303 | if (result < 0) |
| 304 | dbg("%s - set SW flow control failed", __func__); |
| 305 | |
| 306 | } |
| 307 | |
| 308 | |
| 309 | static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port) |
| 310 | { |
| 311 | struct usb_device *dev = port->serial->dev; |
| 312 | struct ssu100_port_private *priv = usb_get_serial_port_data(port); |
| 313 | u8 *data; |
| 314 | int result; |
Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 315 | unsigned long flags; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 316 | |
| 317 | dbg("%s - port %d", __func__, port->number); |
| 318 | |
| 319 | data = kzalloc(2, GFP_KERNEL); |
| 320 | if (!data) |
| 321 | return -ENOMEM; |
| 322 | |
| 323 | result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 324 | QT_OPEN_CLOSE_CHANNEL, |
| 325 | QT_TRANSFER_IN, 0x01, |
| 326 | 0, data, 2, 300); |
| 327 | if (result < 0) { |
| 328 | dbg("%s - open failed %i", __func__, result); |
| 329 | kfree(data); |
| 330 | return result; |
| 331 | } |
| 332 | |
Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 333 | spin_lock_irqsave(&priv->status_lock, flags); |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame^] | 334 | priv->shadowLSR = data[0]; |
| 335 | priv->shadowMSR = data[1]; |
Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 336 | spin_unlock_irqrestore(&priv->status_lock, flags); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 337 | |
| 338 | kfree(data); |
| 339 | |
| 340 | /* set to 9600 */ |
| 341 | result = ssu100_control_msg(dev, QT_GET_SET_UART, 0x30, 0x0300); |
| 342 | if (result < 0) |
| 343 | dbg("%s - set uart failed", __func__); |
| 344 | |
| 345 | if (tty) |
| 346 | ssu100_set_termios(tty, port, tty->termios); |
| 347 | |
| 348 | return usb_serial_generic_open(tty, port); |
| 349 | } |
| 350 | |
| 351 | static void ssu100_close(struct usb_serial_port *port) |
| 352 | { |
| 353 | dbg("%s", __func__); |
| 354 | usb_serial_generic_close(port); |
| 355 | } |
| 356 | |
| 357 | static int get_serial_info(struct usb_serial_port *port, |
| 358 | struct serial_struct __user *retinfo) |
| 359 | { |
| 360 | struct serial_struct tmp; |
| 361 | |
| 362 | if (!retinfo) |
| 363 | return -EFAULT; |
| 364 | |
| 365 | memset(&tmp, 0, sizeof(tmp)); |
| 366 | tmp.line = port->serial->minor; |
| 367 | tmp.port = 0; |
| 368 | tmp.irq = 0; |
| 369 | tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; |
| 370 | tmp.xmit_fifo_size = port->bulk_out_size; |
| 371 | tmp.baud_base = 9600; |
| 372 | tmp.close_delay = 5*HZ; |
| 373 | tmp.closing_wait = 30*HZ; |
| 374 | |
| 375 | if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) |
| 376 | return -EFAULT; |
| 377 | return 0; |
| 378 | } |
| 379 | |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame^] | 380 | static int wait_modem_info(struct usb_serial_port *port, unsigned int arg) |
| 381 | { |
| 382 | struct ssu100_port_private *priv = usb_get_serial_port_data(port); |
| 383 | struct async_icount prev, cur; |
| 384 | unsigned long flags; |
| 385 | |
| 386 | spin_lock_irqsave(&priv->status_lock, flags); |
| 387 | prev = priv->icount; |
| 388 | spin_unlock_irqrestore(&priv->status_lock, flags); |
| 389 | |
| 390 | while (1) { |
| 391 | wait_event_interruptible(priv->delta_msr_wait, |
| 392 | ((priv->icount.rng != prev.rng) || |
| 393 | (priv->icount.dsr != prev.dsr) || |
| 394 | (priv->icount.dcd != prev.dcd) || |
| 395 | (priv->icount.cts != prev.cts))); |
| 396 | |
| 397 | if (signal_pending(current)) |
| 398 | return -ERESTARTSYS; |
| 399 | |
| 400 | spin_lock_irqsave(&priv->status_lock, flags); |
| 401 | cur = priv->icount; |
| 402 | spin_unlock_irqrestore(&priv->status_lock, flags); |
| 403 | |
| 404 | if ((prev.rng == cur.rng) && |
| 405 | (prev.dsr == cur.dsr) && |
| 406 | (prev.dcd == cur.dcd) && |
| 407 | (prev.cts == cur.cts)) |
| 408 | return -EIO; |
| 409 | |
| 410 | if ((arg & TIOCM_RNG && (prev.rng != cur.rng)) || |
| 411 | (arg & TIOCM_DSR && (prev.dsr != cur.dsr)) || |
| 412 | (arg & TIOCM_CD && (prev.dcd != cur.dcd)) || |
| 413 | (arg & TIOCM_CTS && (prev.cts != cur.cts))) |
| 414 | return 0; |
| 415 | } |
| 416 | return 0; |
| 417 | } |
| 418 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 419 | static int ssu100_ioctl(struct tty_struct *tty, struct file *file, |
| 420 | unsigned int cmd, unsigned long arg) |
| 421 | { |
| 422 | struct usb_serial_port *port = tty->driver_data; |
| 423 | struct ssu100_port_private *priv = usb_get_serial_port_data(port); |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame^] | 424 | void __user *user_arg = (void __user *)arg; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 425 | |
| 426 | dbg("%s cmd 0x%04x", __func__, cmd); |
| 427 | |
| 428 | switch (cmd) { |
| 429 | case TIOCGSERIAL: |
| 430 | return get_serial_info(port, |
| 431 | (struct serial_struct __user *) arg); |
| 432 | |
| 433 | case TIOCMIWAIT: |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame^] | 434 | return wait_modem_info(port, arg); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 435 | |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame^] | 436 | case TIOCGICOUNT: |
| 437 | { |
| 438 | struct serial_icounter_struct icount; |
| 439 | struct async_icount cnow = priv->icount; |
| 440 | memset(&icount, 0, sizeof(icount)); |
| 441 | icount.cts = cnow.cts; |
| 442 | icount.dsr = cnow.dsr; |
| 443 | icount.rng = cnow.rng; |
| 444 | icount.dcd = cnow.dcd; |
| 445 | icount.rx = cnow.rx; |
| 446 | icount.tx = cnow.tx; |
| 447 | icount.frame = cnow.frame; |
| 448 | icount.overrun = cnow.overrun; |
| 449 | icount.parity = cnow.parity; |
| 450 | icount.brk = cnow.brk; |
| 451 | icount.buf_overrun = cnow.buf_overrun; |
| 452 | if (copy_to_user(user_arg, &icount, sizeof(icount))) |
| 453 | return -EFAULT; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 454 | return 0; |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame^] | 455 | } |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 456 | |
| 457 | default: |
| 458 | break; |
| 459 | } |
| 460 | |
| 461 | dbg("%s arg not supported", __func__); |
| 462 | |
| 463 | return -ENOIOCTLCMD; |
| 464 | } |
| 465 | |
| 466 | static void ssu100_set_max_packet_size(struct usb_serial_port *port) |
| 467 | { |
| 468 | struct ssu100_port_private *priv = usb_get_serial_port_data(port); |
| 469 | struct usb_serial *serial = port->serial; |
| 470 | struct usb_device *udev = serial->dev; |
| 471 | |
| 472 | struct usb_interface *interface = serial->interface; |
| 473 | struct usb_endpoint_descriptor *ep_desc = &interface->cur_altsetting->endpoint[1].desc; |
| 474 | |
| 475 | unsigned num_endpoints; |
| 476 | int i; |
Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 477 | unsigned long flags; |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 478 | |
| 479 | num_endpoints = interface->cur_altsetting->desc.bNumEndpoints; |
| 480 | dev_info(&udev->dev, "Number of endpoints %d\n", num_endpoints); |
| 481 | |
| 482 | for (i = 0; i < num_endpoints; i++) { |
| 483 | dev_info(&udev->dev, "Endpoint %d MaxPacketSize %d\n", i+1, |
| 484 | interface->cur_altsetting->endpoint[i].desc.wMaxPacketSize); |
| 485 | ep_desc = &interface->cur_altsetting->endpoint[i].desc; |
| 486 | } |
| 487 | |
| 488 | /* set max packet size based on descriptor */ |
Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 489 | spin_lock_irqsave(&priv->status_lock, flags); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 490 | priv->max_packet_size = ep_desc->wMaxPacketSize; |
Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 491 | spin_unlock_irqrestore(&priv->status_lock, flags); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 492 | |
| 493 | dev_info(&udev->dev, "Setting MaxPacketSize %d\n", priv->max_packet_size); |
| 494 | } |
| 495 | |
| 496 | static int ssu100_attach(struct usb_serial *serial) |
| 497 | { |
| 498 | struct ssu100_port_private *priv; |
| 499 | struct usb_serial_port *port = *serial->port; |
| 500 | |
| 501 | dbg("%s", __func__); |
| 502 | |
| 503 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 504 | if (!priv) { |
| 505 | dev_err(&port->dev, "%s- kmalloc(%Zd) failed.\n", __func__, |
| 506 | sizeof(*priv)); |
| 507 | return -ENOMEM; |
| 508 | } |
| 509 | |
Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 510 | spin_lock_init(&priv->status_lock); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 511 | init_waitqueue_head(&priv->delta_msr_wait); |
| 512 | usb_set_serial_port_data(port, priv); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 513 | ssu100_set_max_packet_size(port); |
| 514 | |
| 515 | return ssu100_initdevice(serial->dev); |
| 516 | } |
| 517 | |
| 518 | static int ssu100_tiocmget(struct tty_struct *tty, struct file *file) |
| 519 | { |
| 520 | struct usb_serial_port *port = tty->driver_data; |
| 521 | struct usb_device *dev = port->serial->dev; |
| 522 | u8 *d; |
| 523 | int r; |
| 524 | |
| 525 | dbg("%s\n", __func__); |
| 526 | |
| 527 | d = kzalloc(2, GFP_KERNEL); |
| 528 | if (!d) |
| 529 | return -ENOMEM; |
| 530 | |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 531 | r = ssu100_getregister(dev, 0, UART_MCR, d); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 532 | if (r < 0) |
| 533 | goto mget_out; |
| 534 | |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 535 | r = ssu100_getregister(dev, 0, UART_MSR, d+1); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 536 | if (r < 0) |
| 537 | goto mget_out; |
| 538 | |
Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 539 | r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) | |
| 540 | (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) | |
| 541 | (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) | |
| 542 | (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) | |
| 543 | (d[1] & UART_MSR_RI ? TIOCM_RI : 0) | |
| 544 | (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 545 | |
| 546 | mget_out: |
| 547 | kfree(d); |
| 548 | return r; |
| 549 | } |
| 550 | |
| 551 | static int ssu100_tiocmset(struct tty_struct *tty, struct file *file, |
| 552 | unsigned int set, unsigned int clear) |
| 553 | { |
| 554 | struct usb_serial_port *port = tty->driver_data; |
| 555 | struct usb_device *dev = port->serial->dev; |
| 556 | |
| 557 | dbg("%s\n", __func__); |
| 558 | return update_mctrl(dev, set, clear); |
| 559 | } |
| 560 | |
| 561 | static void ssu100_dtr_rts(struct usb_serial_port *port, int on) |
| 562 | { |
| 563 | struct usb_device *dev = port->serial->dev; |
| 564 | |
| 565 | dbg("%s\n", __func__); |
| 566 | |
| 567 | mutex_lock(&port->serial->disc_mutex); |
| 568 | if (!port->serial->disconnected) { |
| 569 | /* Disable flow control */ |
| 570 | if (!on && |
Bill Pemberton | 556f1a0 | 2010-08-05 17:01:08 -0400 | [diff] [blame] | 571 | ssu100_setregister(dev, 0, UART_MCR, 0) < 0) |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 572 | dev_err(&port->dev, "error from flowcontrol urb\n"); |
| 573 | /* drop RTS and DTR */ |
| 574 | if (on) |
| 575 | set_mctrl(dev, TIOCM_DTR | TIOCM_RTS); |
| 576 | else |
| 577 | clear_mctrl(dev, TIOCM_DTR | TIOCM_RTS); |
| 578 | } |
| 579 | mutex_unlock(&port->serial->disc_mutex); |
| 580 | } |
| 581 | |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame^] | 582 | static void ssu100_update_msr(struct usb_serial_port *port, u8 msr) |
| 583 | { |
| 584 | struct ssu100_port_private *priv = usb_get_serial_port_data(port); |
| 585 | unsigned long flags; |
| 586 | |
| 587 | spin_lock_irqsave(&priv->status_lock, flags); |
| 588 | priv->shadowMSR = msr; |
| 589 | spin_unlock_irqrestore(&priv->status_lock, flags); |
| 590 | |
| 591 | if (msr & UART_MSR_ANY_DELTA) { |
| 592 | /* update input line counters */ |
| 593 | if (msr & UART_MSR_DCTS) |
| 594 | priv->icount.cts++; |
| 595 | if (msr & UART_MSR_DDSR) |
| 596 | priv->icount.dsr++; |
| 597 | if (msr & UART_MSR_DDCD) |
| 598 | priv->icount.dcd++; |
| 599 | if (msr & UART_MSR_TERI) |
| 600 | priv->icount.rng++; |
| 601 | wake_up_interruptible(&priv->delta_msr_wait); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | static void ssu100_update_lsr(struct usb_serial_port *port, u8 lsr) |
| 606 | { |
| 607 | struct ssu100_port_private *priv = usb_get_serial_port_data(port); |
| 608 | unsigned long flags; |
| 609 | |
| 610 | spin_lock_irqsave(&priv->status_lock, flags); |
| 611 | priv->shadowLSR = lsr; |
| 612 | spin_unlock_irqrestore(&priv->status_lock, flags); |
| 613 | |
| 614 | if (lsr & UART_LSR_BRK_ERROR_BITS) { |
| 615 | if (lsr & UART_LSR_BI) |
| 616 | priv->icount.brk++; |
| 617 | if (lsr & UART_LSR_FE) |
| 618 | priv->icount.frame++; |
| 619 | if (lsr & UART_LSR_PE) |
| 620 | priv->icount.parity++; |
| 621 | if (lsr & UART_LSR_OE) |
| 622 | priv->icount.overrun++; |
| 623 | } |
| 624 | } |
| 625 | |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 626 | static int ssu100_process_packet(struct tty_struct *tty, |
| 627 | struct usb_serial_port *port, |
| 628 | struct ssu100_port_private *priv, |
| 629 | char *packet, int len) |
| 630 | { |
| 631 | int i; |
| 632 | char flag; |
| 633 | char *ch; |
| 634 | |
| 635 | dbg("%s - port %d", __func__, port->number); |
| 636 | |
Bill Pemberton | 9b2cef3 | 2010-08-05 17:01:06 -0400 | [diff] [blame] | 637 | if ((len >= 4) && |
| 638 | (packet[0] == 0x1b) && (packet[1] == 0x1b) && |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 639 | ((packet[2] == 0x00) || (packet[2] == 0x01))) { |
| 640 | if (packet[2] == 0x00) |
Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame^] | 641 | ssu100_update_lsr(port, packet[3]); |
| 642 | if (packet[2] == 0x01) |
| 643 | ssu100_update_msr(port, packet[3]); |
Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 644 | |
| 645 | len -= 4; |
| 646 | ch = packet + 4; |
| 647 | } else |
| 648 | ch = packet; |
| 649 | |
| 650 | if (!len) |
| 651 | return 0; /* status only */ |
| 652 | |
| 653 | if (port->port.console && port->sysrq) { |
| 654 | for (i = 0; i < len; i++, ch++) { |
| 655 | if (!usb_serial_handle_sysrq_char(tty, port, *ch)) |
| 656 | tty_insert_flip_char(tty, *ch, flag); |
| 657 | } |
| 658 | } else |
| 659 | tty_insert_flip_string_fixed_flag(tty, ch, flag, len); |
| 660 | |
| 661 | return len; |
| 662 | } |
| 663 | |
| 664 | static void ssu100_process_read_urb(struct urb *urb) |
| 665 | { |
| 666 | struct usb_serial_port *port = urb->context; |
| 667 | struct ssu100_port_private *priv = usb_get_serial_port_data(port); |
| 668 | char *data = (char *)urb->transfer_buffer; |
| 669 | struct tty_struct *tty; |
| 670 | int count = 0; |
| 671 | int i; |
| 672 | int len; |
| 673 | |
| 674 | dbg("%s", __func__); |
| 675 | |
| 676 | tty = tty_port_tty_get(&port->port); |
| 677 | if (!tty) |
| 678 | return; |
| 679 | |
| 680 | for (i = 0; i < urb->actual_length; i += priv->max_packet_size) { |
| 681 | len = min_t(int, urb->actual_length - i, priv->max_packet_size); |
| 682 | count += ssu100_process_packet(tty, port, priv, &data[i], len); |
| 683 | } |
| 684 | |
| 685 | if (count) |
| 686 | tty_flip_buffer_push(tty); |
| 687 | tty_kref_put(tty); |
| 688 | } |
| 689 | |
| 690 | |
| 691 | static struct usb_serial_driver ssu100_device = { |
| 692 | .driver = { |
| 693 | .owner = THIS_MODULE, |
| 694 | .name = "ssu100", |
| 695 | }, |
| 696 | .description = DRIVER_DESC, |
| 697 | .id_table = id_table, |
| 698 | .usb_driver = &ssu100_driver, |
| 699 | .num_ports = 1, |
| 700 | .bulk_in_size = 256, |
| 701 | .bulk_out_size = 256, |
| 702 | .open = ssu100_open, |
| 703 | .close = ssu100_close, |
| 704 | .attach = ssu100_attach, |
| 705 | .release = ssu100_release, |
| 706 | .dtr_rts = ssu100_dtr_rts, |
| 707 | .process_read_urb = ssu100_process_read_urb, |
| 708 | .tiocmget = ssu100_tiocmget, |
| 709 | .tiocmset = ssu100_tiocmset, |
| 710 | .ioctl = ssu100_ioctl, |
| 711 | .set_termios = ssu100_set_termios, |
| 712 | }; |
| 713 | |
| 714 | static int __init ssu100_init(void) |
| 715 | { |
| 716 | int retval; |
| 717 | |
| 718 | dbg("%s", __func__); |
| 719 | |
| 720 | /* register with usb-serial */ |
| 721 | retval = usb_serial_register(&ssu100_device); |
| 722 | |
| 723 | if (retval) |
| 724 | goto failed_usb_sio_register; |
| 725 | |
| 726 | retval = usb_register(&ssu100_driver); |
| 727 | if (retval) |
| 728 | goto failed_usb_register; |
| 729 | |
| 730 | printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":" |
| 731 | DRIVER_DESC "\n"); |
| 732 | |
| 733 | return 0; |
| 734 | |
| 735 | failed_usb_register: |
| 736 | usb_serial_deregister(&ssu100_device); |
| 737 | failed_usb_sio_register: |
| 738 | return retval; |
| 739 | } |
| 740 | |
| 741 | static void __exit ssu100_exit(void) |
| 742 | { |
| 743 | usb_deregister(&ssu100_driver); |
| 744 | usb_serial_deregister(&ssu100_device); |
| 745 | } |
| 746 | |
| 747 | module_init(ssu100_init); |
| 748 | module_exit(ssu100_exit); |
| 749 | |
| 750 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 751 | MODULE_LICENSE("GPL"); |
| 752 | |
| 753 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
| 754 | MODULE_PARM_DESC(debug, "Debug enabled or not"); |