Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * KLSI KL5KUSB105 chip RS232 converter driver |
| 3 | * |
| 4 | * Copyright (C) 2001 Utz-Uwe Haus <haus@uuhaus.de> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * All information about the device was acquired using SniffUSB ans snoopUSB |
| 12 | * on Windows98. |
| 13 | * It was written out of frustration with the PalmConnect USB Serial adapter |
| 14 | * sold by Palm Inc. |
| 15 | * Neither Palm, nor their contractor (MCCI) or their supplier (KLSI) provided |
| 16 | * information that was not already available. |
| 17 | * |
| 18 | * It seems that KLSI bought some silicon-design information from ScanLogic, |
| 19 | * whose SL11R processor is at the core of the KL5KUSB chipset from KLSI. |
| 20 | * KLSI has firmware available for their devices; it is probable that the |
| 21 | * firmware differs from that used by KLSI in their products. If you have an |
| 22 | * original KLSI device and can provide some information on it, I would be |
| 23 | * most interested in adding support for it here. If you have any information |
| 24 | * on the protocol used (or find errors in my reverse-engineered stuff), please |
| 25 | * let me know. |
| 26 | * |
| 27 | * The code was only tested with a PalmConnect USB adapter; if you |
| 28 | * are adventurous, try it with any KLSI-based device and let me know how it |
| 29 | * breaks so that I can fix it! |
| 30 | */ |
| 31 | |
| 32 | /* TODO: |
| 33 | * check modem line signals |
| 34 | * implement handshaking or decide that we do not support it |
| 35 | */ |
| 36 | |
| 37 | /* History: |
| 38 | * 0.3a - implemented pools of write URBs |
| 39 | * 0.3 - alpha version for public testing |
| 40 | * 0.2 - TIOCMGET works, so autopilot(1) can be used! |
| 41 | * 0.1 - can be used to to pilot-xfer -p /dev/ttyUSB0 -l |
| 42 | * |
| 43 | * The driver skeleton is mainly based on mct_u232.c and various other |
| 44 | * pieces of code shamelessly copied from the drivers/usb/serial/ directory. |
| 45 | */ |
| 46 | |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <linux/kernel.h> |
| 49 | #include <linux/errno.h> |
| 50 | #include <linux/init.h> |
| 51 | #include <linux/slab.h> |
| 52 | #include <linux/tty.h> |
| 53 | #include <linux/tty_driver.h> |
| 54 | #include <linux/tty_flip.h> |
| 55 | #include <linux/module.h> |
| 56 | #include <asm/uaccess.h> |
Al Viro | fd05e72 | 2008-04-28 07:00:16 +0100 | [diff] [blame] | 57 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #include <linux/usb.h> |
Greg Kroah-Hartman | a969888 | 2006-07-11 21:22:58 -0700 | [diff] [blame] | 59 | #include <linux/usb/serial.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #include "kl5kusb105.h" |
| 61 | |
| 62 | static int debug; |
| 63 | |
| 64 | /* |
| 65 | * Version Information |
| 66 | */ |
| 67 | #define DRIVER_VERSION "v0.3a" |
| 68 | #define DRIVER_AUTHOR "Utz-Uwe Haus <haus@uuhaus.de>" |
| 69 | #define DRIVER_DESC "KLSI KL5KUSB105 chipset USB->Serial Converter driver" |
| 70 | |
| 71 | |
| 72 | /* |
| 73 | * Function prototypes |
| 74 | */ |
| 75 | static int klsi_105_startup (struct usb_serial *serial); |
| 76 | static void klsi_105_shutdown (struct usb_serial *serial); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 77 | static int klsi_105_open (struct tty_struct *tty, |
| 78 | struct usb_serial_port *port, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | struct file *filp); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 80 | static void klsi_105_close (struct tty_struct *tty, |
| 81 | struct usb_serial_port *port, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | struct file *filp); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 83 | static int klsi_105_write (struct tty_struct *tty, |
| 84 | struct usb_serial_port *port, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | const unsigned char *buf, |
| 86 | int count); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 87 | static void klsi_105_write_bulk_callback (struct urb *urb); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 88 | static int klsi_105_chars_in_buffer (struct tty_struct *tty); |
| 89 | static int klsi_105_write_room (struct tty_struct *tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 91 | static void klsi_105_read_bulk_callback (struct urb *urb); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 92 | static void klsi_105_set_termios (struct tty_struct *tty, |
| 93 | struct usb_serial_port *port, |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 94 | struct ktermios *old); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 95 | static void klsi_105_throttle (struct tty_struct *tty); |
| 96 | static void klsi_105_unthrottle (struct tty_struct *tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | /* |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 98 | static void klsi_105_break_ctl (struct tty_struct *tty, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | int break_state ); |
| 100 | */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 101 | static int klsi_105_tiocmget (struct tty_struct *tty, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | struct file *file); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 103 | static int klsi_105_tiocmset (struct tty_struct *tty, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | struct file *file, unsigned int set, |
| 105 | unsigned int clear); |
| 106 | |
| 107 | /* |
| 108 | * All of the device info needed for the KLSI converters. |
| 109 | */ |
| 110 | static struct usb_device_id id_table [] = { |
| 111 | { USB_DEVICE(PALMCONNECT_VID, PALMCONNECT_PID) }, |
| 112 | { USB_DEVICE(KLSI_VID, KLSI_KL5KUSB105D_PID) }, |
| 113 | { } /* Terminating entry */ |
| 114 | }; |
| 115 | |
| 116 | MODULE_DEVICE_TABLE (usb, id_table); |
| 117 | |
| 118 | static struct usb_driver kl5kusb105d_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | .name = "kl5kusb105d", |
| 120 | .probe = usb_serial_probe, |
| 121 | .disconnect = usb_serial_disconnect, |
| 122 | .id_table = id_table, |
Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 123 | .no_dynamic_id = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 126 | static struct usb_serial_driver kl5kusb105d_device = { |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 127 | .driver = { |
| 128 | .owner = THIS_MODULE, |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 129 | .name = "kl5kusb105d", |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 130 | }, |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 131 | .description = "KL5KUSB105D / PalmConnect", |
Johannes Hölzl | d9b1b78 | 2006-12-17 21:50:24 +0100 | [diff] [blame] | 132 | .usb_driver = &kl5kusb105d_driver, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | .id_table = id_table, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | .num_ports = 1, |
| 135 | .open = klsi_105_open, |
| 136 | .close = klsi_105_close, |
| 137 | .write = klsi_105_write, |
| 138 | .write_bulk_callback = klsi_105_write_bulk_callback, |
| 139 | .chars_in_buffer = klsi_105_chars_in_buffer, |
| 140 | .write_room = klsi_105_write_room, |
| 141 | .read_bulk_callback =klsi_105_read_bulk_callback, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | .set_termios = klsi_105_set_termios, |
| 143 | /*.break_ctl = klsi_105_break_ctl,*/ |
| 144 | .tiocmget = klsi_105_tiocmget, |
| 145 | .tiocmset = klsi_105_tiocmset, |
| 146 | .attach = klsi_105_startup, |
| 147 | .shutdown = klsi_105_shutdown, |
| 148 | .throttle = klsi_105_throttle, |
| 149 | .unthrottle = klsi_105_unthrottle, |
| 150 | }; |
| 151 | |
| 152 | struct klsi_105_port_settings { |
| 153 | __u8 pktlen; /* always 5, it seems */ |
| 154 | __u8 baudrate; |
| 155 | __u8 databits; |
| 156 | __u8 unknown1; |
| 157 | __u8 unknown2; |
| 158 | } __attribute__ ((packed)); |
| 159 | |
| 160 | /* we implement a pool of NUM_URBS urbs per usb_serial */ |
| 161 | #define NUM_URBS 1 |
| 162 | #define URB_TRANSFER_BUFFER_SIZE 64 |
| 163 | struct klsi_105_private { |
| 164 | struct klsi_105_port_settings cfg; |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 165 | struct ktermios termios; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | unsigned long line_state; /* modem line settings */ |
| 167 | /* write pool */ |
| 168 | struct urb * write_urb_pool[NUM_URBS]; |
| 169 | spinlock_t lock; |
| 170 | unsigned long bytes_in; |
| 171 | unsigned long bytes_out; |
| 172 | }; |
| 173 | |
| 174 | |
| 175 | /* |
| 176 | * Handle vendor specific USB requests |
| 177 | */ |
| 178 | |
| 179 | |
| 180 | #define KLSI_TIMEOUT 5000 /* default urb timeout */ |
| 181 | |
| 182 | static int klsi_105_chg_port_settings(struct usb_serial_port *port, |
| 183 | struct klsi_105_port_settings *settings) |
| 184 | { |
| 185 | int rc; |
| 186 | |
| 187 | rc = usb_control_msg(port->serial->dev, |
| 188 | usb_sndctrlpipe(port->serial->dev, 0), |
| 189 | KL5KUSB105A_SIO_SET_DATA, |
| 190 | USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_INTERFACE, |
| 191 | 0, /* value */ |
| 192 | 0, /* index */ |
| 193 | settings, |
| 194 | sizeof(struct klsi_105_port_settings), |
| 195 | KLSI_TIMEOUT); |
| 196 | if (rc < 0) |
| 197 | err("Change port settings failed (error = %d)", rc); |
| 198 | info("%s - %d byte block, baudrate %x, databits %d, u1 %d, u2 %d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 199 | __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | settings->pktlen, |
| 201 | settings->baudrate, settings->databits, |
| 202 | settings->unknown1, settings->unknown2); |
| 203 | return rc; |
| 204 | } /* klsi_105_chg_port_settings */ |
| 205 | |
| 206 | /* translate a 16-bit status value from the device to linux's TIO bits */ |
| 207 | static unsigned long klsi_105_status2linestate(const __u16 status) |
| 208 | { |
| 209 | unsigned long res = 0; |
| 210 | |
| 211 | res = ((status & KL5KUSB105A_DSR) ? TIOCM_DSR : 0) |
| 212 | | ((status & KL5KUSB105A_CTS) ? TIOCM_CTS : 0) |
| 213 | ; |
| 214 | |
| 215 | return res; |
| 216 | } |
| 217 | /* |
| 218 | * Read line control via vendor command and return result through |
| 219 | * *line_state_p |
| 220 | */ |
| 221 | /* It seems that the status buffer has always only 2 bytes length */ |
| 222 | #define KLSI_STATUSBUF_LEN 2 |
| 223 | static int klsi_105_get_line_state(struct usb_serial_port *port, |
| 224 | unsigned long *line_state_p) |
| 225 | { |
| 226 | int rc; |
| 227 | __u8 status_buf[KLSI_STATUSBUF_LEN] = { -1,-1}; |
| 228 | __u16 status; |
| 229 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 230 | info("%s - sending SIO Poll request", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | rc = usb_control_msg(port->serial->dev, |
| 232 | usb_rcvctrlpipe(port->serial->dev, 0), |
| 233 | KL5KUSB105A_SIO_POLL, |
| 234 | USB_TYPE_VENDOR | USB_DIR_IN, |
| 235 | 0, /* value */ |
| 236 | 0, /* index */ |
| 237 | status_buf, KLSI_STATUSBUF_LEN, |
| 238 | 10000 |
| 239 | ); |
| 240 | if (rc < 0) |
| 241 | err("Reading line status failed (error = %d)", rc); |
| 242 | else { |
Harvey Harrison | bd2c784 | 2008-05-01 20:52:57 -0700 | [diff] [blame] | 243 | status = get_unaligned_le16(status_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 245 | info("%s - read status %x %x", __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | status_buf[0], status_buf[1]); |
| 247 | |
| 248 | *line_state_p = klsi_105_status2linestate(status); |
| 249 | } |
| 250 | |
| 251 | return rc; |
| 252 | } |
| 253 | |
| 254 | |
| 255 | /* |
| 256 | * Driver's tty interface functions |
| 257 | */ |
| 258 | |
| 259 | static int klsi_105_startup (struct usb_serial *serial) |
| 260 | { |
| 261 | struct klsi_105_private *priv; |
Oliver Neukum | 9306fff | 2007-03-29 11:23:54 +0200 | [diff] [blame] | 262 | int i, j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
| 264 | /* check if we support the product id (see keyspan.c) |
| 265 | * FIXME |
| 266 | */ |
| 267 | |
| 268 | /* allocate the private data structure */ |
| 269 | for (i=0; i<serial->num_ports; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | priv = kmalloc(sizeof(struct klsi_105_private), |
| 271 | GFP_KERNEL); |
| 272 | if (!priv) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 273 | dbg("%skmalloc for klsi_105_private failed.", __func__); |
Oliver Neukum | 9306fff | 2007-03-29 11:23:54 +0200 | [diff] [blame] | 274 | i--; |
| 275 | goto err_cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | } |
| 277 | /* set initial values for control structures */ |
| 278 | priv->cfg.pktlen = 5; |
| 279 | priv->cfg.baudrate = kl5kusb105a_sio_b9600; |
| 280 | priv->cfg.databits = kl5kusb105a_dtb_8; |
| 281 | priv->cfg.unknown1 = 0; |
| 282 | priv->cfg.unknown2 = 1; |
| 283 | |
| 284 | priv->line_state = 0; |
| 285 | |
| 286 | priv->bytes_in = 0; |
| 287 | priv->bytes_out = 0; |
| 288 | usb_set_serial_port_data(serial->port[i], priv); |
| 289 | |
| 290 | spin_lock_init (&priv->lock); |
| 291 | for (j=0; j<NUM_URBS; j++) { |
| 292 | struct urb* urb = usb_alloc_urb(0, GFP_KERNEL); |
| 293 | |
| 294 | priv->write_urb_pool[j] = urb; |
| 295 | if (urb == NULL) { |
| 296 | err("No more urbs???"); |
Oliver Neukum | 9306fff | 2007-03-29 11:23:54 +0200 | [diff] [blame] | 297 | goto err_cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, |
| 301 | GFP_KERNEL); |
| 302 | if (!urb->transfer_buffer) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 303 | err("%s - out of memory for urb buffers.", __func__); |
Oliver Neukum | 9306fff | 2007-03-29 11:23:54 +0200 | [diff] [blame] | 304 | goto err_cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
| 308 | /* priv->termios is left uninitalized until port opening */ |
| 309 | init_waitqueue_head(&serial->port[i]->write_wait); |
| 310 | } |
| 311 | |
Oliver Neukum | 9306fff | 2007-03-29 11:23:54 +0200 | [diff] [blame] | 312 | return 0; |
| 313 | |
| 314 | err_cleanup: |
| 315 | for (; i >= 0; i--) { |
| 316 | priv = usb_get_serial_port_data(serial->port[i]); |
| 317 | for (j=0; j < NUM_URBS; j++) { |
| 318 | if (priv->write_urb_pool[j]) { |
| 319 | kfree(priv->write_urb_pool[j]->transfer_buffer); |
| 320 | usb_free_urb(priv->write_urb_pool[j]); |
| 321 | } |
| 322 | } |
| 323 | usb_set_serial_port_data(serial->port[i], NULL); |
| 324 | } |
| 325 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | } /* klsi_105_startup */ |
| 327 | |
| 328 | |
| 329 | static void klsi_105_shutdown (struct usb_serial *serial) |
| 330 | { |
| 331 | int i; |
| 332 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 333 | dbg("%s", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
| 335 | /* stop reads and writes on all ports */ |
| 336 | for (i=0; i < serial->num_ports; ++i) { |
| 337 | struct klsi_105_private *priv = usb_get_serial_port_data(serial->port[i]); |
| 338 | unsigned long flags; |
| 339 | |
| 340 | if (priv) { |
| 341 | /* kill our write urb pool */ |
| 342 | int j; |
| 343 | struct urb **write_urbs = priv->write_urb_pool; |
| 344 | spin_lock_irqsave(&priv->lock,flags); |
| 345 | |
| 346 | for (j = 0; j < NUM_URBS; j++) { |
| 347 | if (write_urbs[j]) { |
| 348 | /* FIXME - uncomment the following |
| 349 | * usb_kill_urb call when the host |
| 350 | * controllers get fixed to set |
| 351 | * urb->dev = NULL after the urb is |
| 352 | * finished. Otherwise this call |
| 353 | * oopses. */ |
| 354 | /* usb_kill_urb(write_urbs[j]); */ |
Jesper Juhl | 1bc3c9e | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 355 | kfree(write_urbs[j]->transfer_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | usb_free_urb (write_urbs[j]); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | spin_unlock_irqrestore (&priv->lock, flags); |
| 361 | |
| 362 | kfree(priv); |
| 363 | usb_set_serial_port_data(serial->port[i], NULL); |
| 364 | } |
| 365 | } |
| 366 | } /* klsi_105_shutdown */ |
| 367 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 368 | static int klsi_105_open(struct tty_struct *tty, |
| 369 | struct usb_serial_port *port, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | { |
| 371 | struct klsi_105_private *priv = usb_get_serial_port_data(port); |
| 372 | int retval = 0; |
| 373 | int rc; |
| 374 | int i; |
| 375 | unsigned long line_state; |
| 376 | struct klsi_105_port_settings cfg; |
| 377 | unsigned long flags; |
| 378 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 379 | dbg("%s port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
| 381 | /* force low_latency on so that our tty_push actually forces |
| 382 | * the data through |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 383 | * tty->low_latency = 1; */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
| 385 | /* Do a defined restart: |
| 386 | * Set up sane default baud rate and send the 'READ_ON' |
| 387 | * vendor command. |
| 388 | * FIXME: set modem line control (how?) |
| 389 | * Then read the modem line control and store values in |
| 390 | * priv->line_state. |
| 391 | */ |
| 392 | cfg.pktlen = 5; |
| 393 | cfg.baudrate = kl5kusb105a_sio_b9600; |
| 394 | cfg.databits = kl5kusb105a_dtb_8; |
| 395 | cfg.unknown1 = 0; |
| 396 | cfg.unknown2 = 1; |
| 397 | klsi_105_chg_port_settings(port, &cfg); |
| 398 | |
| 399 | /* set up termios structure */ |
| 400 | spin_lock_irqsave (&priv->lock, flags); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 401 | priv->termios.c_iflag = tty->termios->c_iflag; |
| 402 | priv->termios.c_oflag = tty->termios->c_oflag; |
| 403 | priv->termios.c_cflag = tty->termios->c_cflag; |
| 404 | priv->termios.c_lflag = tty->termios->c_lflag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | for (i=0; i<NCCS; i++) |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 406 | priv->termios.c_cc[i] = tty->termios->c_cc[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | priv->cfg.pktlen = cfg.pktlen; |
| 408 | priv->cfg.baudrate = cfg.baudrate; |
| 409 | priv->cfg.databits = cfg.databits; |
| 410 | priv->cfg.unknown1 = cfg.unknown1; |
| 411 | priv->cfg.unknown2 = cfg.unknown2; |
| 412 | spin_unlock_irqrestore (&priv->lock, flags); |
| 413 | |
| 414 | /* READ_ON and urb submission */ |
| 415 | usb_fill_bulk_urb(port->read_urb, port->serial->dev, |
| 416 | usb_rcvbulkpipe(port->serial->dev, |
| 417 | port->bulk_in_endpointAddress), |
| 418 | port->read_urb->transfer_buffer, |
| 419 | port->read_urb->transfer_buffer_length, |
| 420 | klsi_105_read_bulk_callback, |
| 421 | port); |
| 422 | |
| 423 | rc = usb_submit_urb(port->read_urb, GFP_KERNEL); |
| 424 | if (rc) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 425 | err("%s - failed submitting read urb, error %d", __func__, rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | retval = rc; |
| 427 | goto exit; |
| 428 | } |
| 429 | |
| 430 | rc = usb_control_msg(port->serial->dev, |
| 431 | usb_sndctrlpipe(port->serial->dev,0), |
| 432 | KL5KUSB105A_SIO_CONFIGURE, |
| 433 | USB_TYPE_VENDOR|USB_DIR_OUT|USB_RECIP_INTERFACE, |
| 434 | KL5KUSB105A_SIO_CONFIGURE_READ_ON, |
| 435 | 0, /* index */ |
| 436 | NULL, |
| 437 | 0, |
| 438 | KLSI_TIMEOUT); |
| 439 | if (rc < 0) { |
| 440 | err("Enabling read failed (error = %d)", rc); |
| 441 | retval = rc; |
| 442 | } else |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 443 | dbg("%s - enabled reading", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
| 445 | rc = klsi_105_get_line_state(port, &line_state); |
| 446 | if (rc >= 0) { |
| 447 | spin_lock_irqsave (&priv->lock, flags); |
| 448 | priv->line_state = line_state; |
| 449 | spin_unlock_irqrestore (&priv->lock, flags); |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 450 | dbg("%s - read line state 0x%lx", __func__, line_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | retval = 0; |
| 452 | } else |
| 453 | retval = rc; |
| 454 | |
| 455 | exit: |
| 456 | return retval; |
| 457 | } /* klsi_105_open */ |
| 458 | |
| 459 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 460 | static void klsi_105_close(struct tty_struct *tty, |
| 461 | struct usb_serial_port *port, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | { |
| 463 | struct klsi_105_private *priv = usb_get_serial_port_data(port); |
| 464 | int rc; |
| 465 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 466 | dbg("%s port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | |
Oliver Neukum | 3edbc98 | 2008-01-22 12:47:15 +0100 | [diff] [blame] | 468 | mutex_lock(&port->serial->disc_mutex); |
| 469 | if (!port->serial->disconnected) { |
| 470 | /* send READ_OFF */ |
| 471 | rc = usb_control_msg (port->serial->dev, |
| 472 | usb_sndctrlpipe(port->serial->dev, 0), |
| 473 | KL5KUSB105A_SIO_CONFIGURE, |
| 474 | USB_TYPE_VENDOR | USB_DIR_OUT, |
| 475 | KL5KUSB105A_SIO_CONFIGURE_READ_OFF, |
| 476 | 0, /* index */ |
| 477 | NULL, 0, |
| 478 | KLSI_TIMEOUT); |
| 479 | if (rc < 0) |
| 480 | err("Disabling read failed (error = %d)", rc); |
| 481 | } |
| 482 | mutex_unlock(&port->serial->disc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
| 484 | /* shutdown our bulk reads and writes */ |
| 485 | usb_kill_urb(port->write_urb); |
| 486 | usb_kill_urb(port->read_urb); |
| 487 | /* unlink our write pool */ |
| 488 | /* FIXME */ |
| 489 | /* wgg - do I need this? I think so. */ |
| 490 | usb_kill_urb(port->interrupt_in_urb); |
| 491 | info("kl5kusb105 port stats: %ld bytes in, %ld bytes out", priv->bytes_in, priv->bytes_out); |
| 492 | } /* klsi_105_close */ |
| 493 | |
| 494 | |
| 495 | /* We need to write a complete 64-byte data block and encode the |
| 496 | * number actually sent in the first double-byte, LSB-order. That |
| 497 | * leaves at most 62 bytes of payload. |
| 498 | */ |
| 499 | #define KLSI_105_DATA_OFFSET 2 /* in the bulk urb data block */ |
| 500 | |
| 501 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 502 | static int klsi_105_write(struct tty_struct *tty, |
| 503 | struct usb_serial_port *port, const unsigned char *buf, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | { |
| 505 | struct klsi_105_private *priv = usb_get_serial_port_data(port); |
| 506 | int result, size; |
| 507 | int bytes_sent=0; |
| 508 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 509 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
| 511 | while (count > 0) { |
| 512 | /* try to find a free urb (write 0 bytes if none) */ |
| 513 | struct urb *urb = NULL; |
| 514 | unsigned long flags; |
| 515 | int i; |
| 516 | /* since the pool is per-port we might not need the spin lock !? */ |
| 517 | spin_lock_irqsave (&priv->lock, flags); |
| 518 | for (i=0; i<NUM_URBS; i++) { |
| 519 | if (priv->write_urb_pool[i]->status != -EINPROGRESS) { |
| 520 | urb = priv->write_urb_pool[i]; |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 521 | dbg("%s - using pool URB %d", __func__, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | break; |
| 523 | } |
| 524 | } |
| 525 | spin_unlock_irqrestore (&priv->lock, flags); |
| 526 | |
| 527 | if (urb==NULL) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 528 | dbg("%s - no more free urbs", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | goto exit; |
| 530 | } |
| 531 | |
| 532 | if (urb->transfer_buffer == NULL) { |
| 533 | urb->transfer_buffer = kmalloc (URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC); |
| 534 | if (urb->transfer_buffer == NULL) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 535 | err("%s - no more kernel memory...", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | goto exit; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | size = min (count, port->bulk_out_size - KLSI_105_DATA_OFFSET); |
| 541 | size = min (size, URB_TRANSFER_BUFFER_SIZE - KLSI_105_DATA_OFFSET); |
| 542 | |
| 543 | memcpy (urb->transfer_buffer + KLSI_105_DATA_OFFSET, buf, size); |
| 544 | |
| 545 | /* write payload size into transfer buffer */ |
| 546 | ((__u8 *)urb->transfer_buffer)[0] = (__u8) (size & 0xFF); |
| 547 | ((__u8 *)urb->transfer_buffer)[1] = (__u8) ((size & 0xFF00)>>8); |
| 548 | |
| 549 | /* set up our urb */ |
| 550 | usb_fill_bulk_urb(urb, port->serial->dev, |
| 551 | usb_sndbulkpipe(port->serial->dev, |
| 552 | port->bulk_out_endpointAddress), |
| 553 | urb->transfer_buffer, |
| 554 | URB_TRANSFER_BUFFER_SIZE, |
| 555 | klsi_105_write_bulk_callback, |
| 556 | port); |
| 557 | |
| 558 | /* send the data out the bulk port */ |
| 559 | result = usb_submit_urb(urb, GFP_ATOMIC); |
| 560 | if (result) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 561 | err("%s - failed submitting write urb, error %d", __func__, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | goto exit; |
| 563 | } |
| 564 | buf += size; |
| 565 | bytes_sent += size; |
| 566 | count -= size; |
| 567 | } |
| 568 | exit: |
| 569 | /* lockless, but it's for debug info only... */ |
| 570 | priv->bytes_out+=bytes_sent; |
| 571 | |
| 572 | return bytes_sent; /* that's how much we wrote */ |
| 573 | } /* klsi_105_write */ |
| 574 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 575 | static void klsi_105_write_bulk_callback ( struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | { |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 577 | struct usb_serial_port *port = urb->context; |
Greg Kroah-Hartman | 17c1b35 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 578 | int status = urb->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 580 | dbg("%s - port %d", __func__, port->number); |
Greg Kroah-Hartman | 17c1b35 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 581 | |
| 582 | if (status) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 583 | dbg("%s - nonzero write bulk status received: %d", __func__, |
Greg Kroah-Hartman | 17c1b35 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 584 | status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | return; |
| 586 | } |
| 587 | |
Pete Zaitcev | cf2c748 | 2006-05-22 21:58:49 -0700 | [diff] [blame] | 588 | usb_serial_port_softint(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | } /* klsi_105_write_bulk_completion_callback */ |
| 590 | |
| 591 | |
| 592 | /* return number of characters currently in the writing process */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 593 | static int klsi_105_chars_in_buffer (struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 595 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | int chars = 0; |
| 597 | int i; |
| 598 | unsigned long flags; |
| 599 | struct klsi_105_private *priv = usb_get_serial_port_data(port); |
| 600 | |
| 601 | spin_lock_irqsave (&priv->lock, flags); |
| 602 | |
| 603 | for (i = 0; i < NUM_URBS; ++i) { |
| 604 | if (priv->write_urb_pool[i]->status == -EINPROGRESS) { |
| 605 | chars += URB_TRANSFER_BUFFER_SIZE; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | spin_unlock_irqrestore (&priv->lock, flags); |
| 610 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 611 | dbg("%s - returns %d", __func__, chars); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | return (chars); |
| 613 | } |
| 614 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 615 | static int klsi_105_write_room (struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 617 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | unsigned long flags; |
| 619 | int i; |
| 620 | int room = 0; |
| 621 | struct klsi_105_private *priv = usb_get_serial_port_data(port); |
| 622 | |
| 623 | spin_lock_irqsave (&priv->lock, flags); |
| 624 | for (i = 0; i < NUM_URBS; ++i) { |
| 625 | if (priv->write_urb_pool[i]->status != -EINPROGRESS) { |
| 626 | room += URB_TRANSFER_BUFFER_SIZE; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | spin_unlock_irqrestore (&priv->lock, flags); |
| 631 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 632 | dbg("%s - returns %d", __func__, room); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | return (room); |
| 634 | } |
| 635 | |
| 636 | |
| 637 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 638 | static void klsi_105_read_bulk_callback (struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | { |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 640 | struct usb_serial_port *port = urb->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | struct klsi_105_private *priv = usb_get_serial_port_data(port); |
| 642 | struct tty_struct *tty; |
| 643 | unsigned char *data = urb->transfer_buffer; |
| 644 | int rc; |
Greg Kroah-Hartman | 17c1b35 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 645 | int status = urb->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 647 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | |
| 649 | /* The urb might have been killed. */ |
Greg Kroah-Hartman | 17c1b35 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 650 | if (status) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 651 | dbg("%s - nonzero read bulk status received: %d", __func__, |
Greg Kroah-Hartman | 17c1b35 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 652 | status); |
| 653 | return; |
| 654 | } |
| 655 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | /* The data received is again preceded by a length double-byte in LSB- |
| 657 | * first order (see klsi_105_write() ) |
| 658 | */ |
| 659 | if (urb->actual_length == 0) { |
| 660 | /* empty urbs seem to happen, we ignore them */ |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 661 | /* dbg("%s - emtpy URB", __func__); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | ; |
| 663 | } else if (urb->actual_length <= 2) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 664 | dbg("%s - size %d URB not understood", __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | urb->actual_length); |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 666 | usb_serial_debug_data(debug, &port->dev, __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | urb->actual_length, data); |
| 668 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | int bytes_sent = ((__u8 *) data)[0] + |
| 670 | ((unsigned int) ((__u8 *) data)[1] << 8); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 671 | tty = port->port.tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | /* we should immediately resubmit the URB, before attempting |
| 673 | * to pass the data on to the tty layer. But that needs locking |
| 674 | * against re-entry an then mixed-up data because of |
| 675 | * intermixed tty_flip_buffer_push()s |
| 676 | * FIXME |
| 677 | */ |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 678 | usb_serial_debug_data(debug, &port->dev, __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | urb->actual_length, data); |
| 680 | |
| 681 | if (bytes_sent + 2 > urb->actual_length) { |
| 682 | dbg("%s - trying to read more data than available" |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 683 | " (%d vs. %d)", __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | bytes_sent+2, urb->actual_length); |
| 685 | /* cap at implied limit */ |
| 686 | bytes_sent = urb->actual_length - 2; |
| 687 | } |
| 688 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 689 | tty_buffer_request_room(tty, bytes_sent); |
| 690 | tty_insert_flip_string(tty, data + 2, bytes_sent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | tty_flip_buffer_push(tty); |
| 692 | |
| 693 | /* again lockless, but debug info only */ |
| 694 | priv->bytes_in += bytes_sent; |
| 695 | } |
| 696 | /* Continue trying to always read */ |
| 697 | usb_fill_bulk_urb(port->read_urb, port->serial->dev, |
| 698 | usb_rcvbulkpipe(port->serial->dev, |
| 699 | port->bulk_in_endpointAddress), |
| 700 | port->read_urb->transfer_buffer, |
| 701 | port->read_urb->transfer_buffer_length, |
| 702 | klsi_105_read_bulk_callback, |
| 703 | port); |
| 704 | rc = usb_submit_urb(port->read_urb, GFP_ATOMIC); |
| 705 | if (rc) |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 706 | err("%s - failed resubmitting read urb, error %d", __func__, rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | } /* klsi_105_read_bulk_callback */ |
| 708 | |
| 709 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 710 | static void klsi_105_set_termios (struct tty_struct *tty, |
| 711 | struct usb_serial_port *port, |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 712 | struct ktermios *old_termios) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | { |
| 714 | struct klsi_105_private *priv = usb_get_serial_port_data(port); |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame] | 715 | unsigned int iflag = tty->termios->c_iflag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | unsigned int old_iflag = old_termios->c_iflag; |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame] | 717 | unsigned int cflag = tty->termios->c_cflag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | unsigned int old_cflag = old_termios->c_cflag; |
| 719 | struct klsi_105_port_settings cfg; |
| 720 | unsigned long flags; |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame] | 721 | speed_t baud; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | |
| 723 | /* lock while we are modifying the settings */ |
| 724 | spin_lock_irqsave (&priv->lock, flags); |
| 725 | |
| 726 | /* |
| 727 | * Update baud rate |
| 728 | */ |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame] | 729 | baud = tty_get_baud_rate(tty); |
| 730 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | if( (cflag & CBAUD) != (old_cflag & CBAUD) ) { |
| 732 | /* reassert DTR and (maybe) RTS on transition from B0 */ |
| 733 | if( (old_cflag & CBAUD) == B0 ) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 734 | dbg("%s: baud was B0", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | #if 0 |
| 736 | priv->control_state |= TIOCM_DTR; |
| 737 | /* don't set RTS if using hardware flow control */ |
| 738 | if (!(old_cflag & CRTSCTS)) { |
| 739 | priv->control_state |= TIOCM_RTS; |
| 740 | } |
| 741 | mct_u232_set_modem_ctrl(serial, priv->control_state); |
| 742 | #endif |
| 743 | } |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame] | 744 | } |
| 745 | switch(baud) { |
Alan Cox | 3f6ff6e | 2007-08-10 14:53:34 -0700 | [diff] [blame] | 746 | case 0: /* handled below */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | break; |
Alan Cox | 3f6ff6e | 2007-08-10 14:53:34 -0700 | [diff] [blame] | 748 | case 1200: |
| 749 | priv->cfg.baudrate = kl5kusb105a_sio_b1200; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | break; |
Alan Cox | 3f6ff6e | 2007-08-10 14:53:34 -0700 | [diff] [blame] | 751 | case 2400: |
| 752 | priv->cfg.baudrate = kl5kusb105a_sio_b2400; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | break; |
Alan Cox | 3f6ff6e | 2007-08-10 14:53:34 -0700 | [diff] [blame] | 754 | case 4800: |
| 755 | priv->cfg.baudrate = kl5kusb105a_sio_b4800; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | break; |
Alan Cox | 3f6ff6e | 2007-08-10 14:53:34 -0700 | [diff] [blame] | 757 | case 9600: |
| 758 | priv->cfg.baudrate = kl5kusb105a_sio_b9600; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | break; |
Alan Cox | 3f6ff6e | 2007-08-10 14:53:34 -0700 | [diff] [blame] | 760 | case 19200: |
| 761 | priv->cfg.baudrate = kl5kusb105a_sio_b19200; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | break; |
Alan Cox | 3f6ff6e | 2007-08-10 14:53:34 -0700 | [diff] [blame] | 763 | case 38400: |
| 764 | priv->cfg.baudrate = kl5kusb105a_sio_b38400; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | break; |
Alan Cox | 3f6ff6e | 2007-08-10 14:53:34 -0700 | [diff] [blame] | 766 | case 57600: |
| 767 | priv->cfg.baudrate = kl5kusb105a_sio_b57600; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | break; |
Alan Cox | 3f6ff6e | 2007-08-10 14:53:34 -0700 | [diff] [blame] | 769 | case 115200: |
| 770 | priv->cfg.baudrate = kl5kusb105a_sio_b115200; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | break; |
| 772 | default: |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame] | 773 | dbg("KLSI USB->Serial converter:" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | " unsupported baudrate request, using default" |
| 775 | " of 9600"); |
| 776 | priv->cfg.baudrate = kl5kusb105a_sio_b9600; |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame] | 777 | baud = 9600; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | } |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame] | 780 | if ((cflag & CBAUD) == B0 ) { |
| 781 | dbg("%s: baud is B0", __func__); |
| 782 | /* Drop RTS and DTR */ |
| 783 | /* maybe this should be simulated by sending read |
| 784 | * disable and read enable messages? |
| 785 | */ |
| 786 | ; |
| 787 | #if 0 |
| 788 | priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS); |
| 789 | mct_u232_set_modem_ctrl(serial, priv->control_state); |
| 790 | #endif |
| 791 | } |
| 792 | tty_encode_baud_rate(tty, baud, baud); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | |
| 794 | if ((cflag & CSIZE) != (old_cflag & CSIZE)) { |
| 795 | /* set the number of data bits */ |
| 796 | switch (cflag & CSIZE) { |
| 797 | case CS5: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 798 | dbg("%s - 5 bits/byte not supported", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | spin_unlock_irqrestore (&priv->lock, flags); |
| 800 | return ; |
| 801 | case CS6: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 802 | dbg("%s - 6 bits/byte not supported", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | spin_unlock_irqrestore (&priv->lock, flags); |
| 804 | return ; |
| 805 | case CS7: |
| 806 | priv->cfg.databits = kl5kusb105a_dtb_7; |
| 807 | break; |
| 808 | case CS8: |
| 809 | priv->cfg.databits = kl5kusb105a_dtb_8; |
| 810 | break; |
| 811 | default: |
| 812 | err("CSIZE was not CS5-CS8, using default of 8"); |
| 813 | priv->cfg.databits = kl5kusb105a_dtb_8; |
| 814 | break; |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | /* |
| 819 | * Update line control register (LCR) |
| 820 | */ |
| 821 | if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD)) |
| 822 | || (cflag & CSTOPB) != (old_cflag & CSTOPB) ) { |
| 823 | |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame] | 824 | /* Not currently supported */ |
| 825 | tty->termios->c_cflag &= ~(PARENB|PARODD|CSTOPB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | #if 0 |
| 827 | priv->last_lcr = 0; |
| 828 | |
| 829 | /* set the parity */ |
| 830 | if (cflag & PARENB) |
| 831 | priv->last_lcr |= (cflag & PARODD) ? |
| 832 | MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN; |
| 833 | else |
| 834 | priv->last_lcr |= MCT_U232_PARITY_NONE; |
| 835 | |
| 836 | /* set the number of stop bits */ |
| 837 | priv->last_lcr |= (cflag & CSTOPB) ? |
| 838 | MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1; |
| 839 | |
| 840 | mct_u232_set_line_ctrl(serial, priv->last_lcr); |
| 841 | #endif |
| 842 | ; |
| 843 | } |
| 844 | |
| 845 | /* |
| 846 | * Set flow control: well, I do not really now how to handle DTR/RTS. |
| 847 | * Just do what we have seen with SniffUSB on Win98. |
| 848 | */ |
| 849 | if( (iflag & IXOFF) != (old_iflag & IXOFF) |
| 850 | || (iflag & IXON) != (old_iflag & IXON) |
| 851 | || (cflag & CRTSCTS) != (old_cflag & CRTSCTS) ) { |
| 852 | |
Alan Cox | a5b6f60 | 2008-04-08 17:16:06 +0100 | [diff] [blame] | 853 | /* Not currently supported */ |
| 854 | tty->termios->c_cflag &= ~CRTSCTS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | /* Drop DTR/RTS if no flow control otherwise assert */ |
| 856 | #if 0 |
| 857 | if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS) ) |
| 858 | priv->control_state |= TIOCM_DTR | TIOCM_RTS; |
| 859 | else |
| 860 | priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS); |
| 861 | mct_u232_set_modem_ctrl(serial, priv->control_state); |
| 862 | #endif |
| 863 | ; |
| 864 | } |
| 865 | memcpy (&cfg, &priv->cfg, sizeof(cfg)); |
| 866 | spin_unlock_irqrestore (&priv->lock, flags); |
| 867 | |
| 868 | /* now commit changes to device */ |
| 869 | klsi_105_chg_port_settings(port, &cfg); |
| 870 | } /* klsi_105_set_termios */ |
| 871 | |
| 872 | |
| 873 | #if 0 |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 874 | static void mct_u232_break_ctl( struct tty_struct *tty, int break_state ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 876 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | struct usb_serial *serial = port->serial; |
| 878 | struct mct_u232_private *priv = (struct mct_u232_private *)port->private; |
| 879 | unsigned char lcr = priv->last_lcr; |
| 880 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 881 | dbg("%sstate=%d", __func__, break_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | |
| 883 | if (break_state) |
| 884 | lcr |= MCT_U232_SET_BREAK; |
| 885 | |
| 886 | mct_u232_set_line_ctrl(serial, lcr); |
| 887 | } /* mct_u232_break_ctl */ |
| 888 | #endif |
| 889 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 890 | static int klsi_105_tiocmget (struct tty_struct *tty, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 892 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | struct klsi_105_private *priv = usb_get_serial_port_data(port); |
| 894 | unsigned long flags; |
| 895 | int rc; |
| 896 | unsigned long line_state; |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 897 | dbg("%s - request, just guessing", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | |
| 899 | rc = klsi_105_get_line_state(port, &line_state); |
| 900 | if (rc < 0) { |
| 901 | err("Reading line control failed (error = %d)", rc); |
| 902 | /* better return value? EAGAIN? */ |
| 903 | return rc; |
| 904 | } |
| 905 | |
| 906 | spin_lock_irqsave (&priv->lock, flags); |
| 907 | priv->line_state = line_state; |
| 908 | spin_unlock_irqrestore (&priv->lock, flags); |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 909 | dbg("%s - read line state 0x%lx", __func__, line_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | return (int)line_state; |
| 911 | } |
| 912 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 913 | static int klsi_105_tiocmset (struct tty_struct *tty, struct file *file, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | unsigned int set, unsigned int clear) |
| 915 | { |
| 916 | int retval = -EINVAL; |
| 917 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 918 | dbg("%s", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | |
| 920 | /* if this ever gets implemented, it should be done something like this: |
| 921 | struct usb_serial *serial = port->serial; |
| 922 | struct klsi_105_private *priv = usb_get_serial_port_data(port); |
| 923 | unsigned long flags; |
| 924 | int control; |
| 925 | |
| 926 | spin_lock_irqsave (&priv->lock, flags); |
| 927 | if (set & TIOCM_RTS) |
| 928 | priv->control_state |= TIOCM_RTS; |
| 929 | if (set & TIOCM_DTR) |
| 930 | priv->control_state |= TIOCM_DTR; |
| 931 | if (clear & TIOCM_RTS) |
| 932 | priv->control_state &= ~TIOCM_RTS; |
| 933 | if (clear & TIOCM_DTR) |
| 934 | priv->control_state &= ~TIOCM_DTR; |
| 935 | control = priv->control_state; |
| 936 | spin_unlock_irqrestore (&priv->lock, flags); |
| 937 | retval = mct_u232_set_modem_ctrl(serial, control); |
| 938 | */ |
| 939 | return retval; |
| 940 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 942 | static void klsi_105_throttle (struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 944 | struct usb_serial_port *port = tty->driver_data; |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 945 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | usb_kill_urb(port->read_urb); |
| 947 | } |
| 948 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 949 | static void klsi_105_unthrottle (struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 951 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | int result; |
| 953 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 954 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | |
| 956 | port->read_urb->dev = port->serial->dev; |
| 957 | result = usb_submit_urb(port->read_urb, GFP_ATOMIC); |
| 958 | if (result) |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 959 | err("%s - failed submitting read urb, error %d", __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | result); |
| 961 | } |
| 962 | |
| 963 | |
| 964 | |
| 965 | static int __init klsi_105_init (void) |
| 966 | { |
| 967 | int retval; |
| 968 | retval = usb_serial_register(&kl5kusb105d_device); |
| 969 | if (retval) |
| 970 | goto failed_usb_serial_register; |
| 971 | retval = usb_register(&kl5kusb105d_driver); |
| 972 | if (retval) |
| 973 | goto failed_usb_register; |
| 974 | |
| 975 | info(DRIVER_DESC " " DRIVER_VERSION); |
| 976 | return 0; |
| 977 | failed_usb_register: |
| 978 | usb_serial_deregister(&kl5kusb105d_device); |
| 979 | failed_usb_serial_register: |
| 980 | return retval; |
| 981 | } |
| 982 | |
| 983 | |
| 984 | static void __exit klsi_105_exit (void) |
| 985 | { |
| 986 | usb_deregister (&kl5kusb105d_driver); |
| 987 | usb_serial_deregister (&kl5kusb105d_device); |
| 988 | } |
| 989 | |
| 990 | |
| 991 | module_init (klsi_105_init); |
| 992 | module_exit (klsi_105_exit); |
| 993 | |
| 994 | MODULE_AUTHOR( DRIVER_AUTHOR ); |
| 995 | MODULE_DESCRIPTION( DRIVER_DESC ); |
| 996 | MODULE_LICENSE("GPL"); |
| 997 | |
| 998 | |
| 999 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
| 1000 | MODULE_PARM_DESC(debug, "enable extensive debugging messages"); |
| 1001 | |
| 1002 | /* vim: set sts=8 ts=8 sw=8: */ |