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