Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * MCT (Magic Control Technology Corp.) USB RS232 Converter Driver |
| 3 | * |
| 4 | * Copyright (C) 2000 Wolfgang Grandegger (wolfgang@ces.ch) |
| 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 | * This program is largely derived from the Belkin USB Serial Adapter Driver |
| 12 | * (see belkin_sa.[ch]). All of the information about the device was acquired |
| 13 | * by using SniffUSB on Windows98. For technical details see mct_u232.h. |
| 14 | * |
| 15 | * William G. Greathouse and Greg Kroah-Hartman provided great help on how to |
| 16 | * do the reverse engineering and how to write a USB serial device driver. |
| 17 | * |
| 18 | * TO BE DONE, TO BE CHECKED: |
| 19 | * DTR/RTS signal handling may be incomplete or incorrect. I have mainly |
| 20 | * implemented what I have seen with SniffUSB or found in belkin_sa.c. |
| 21 | * For further TODOs check also belkin_sa.c. |
| 22 | * |
| 23 | * TEST STATUS: |
| 24 | * Basic tests have been performed with minicom/zmodem transfers and |
| 25 | * modem dialing under Linux 2.4.0-test10 (for me it works fine). |
| 26 | * |
| 27 | * 04-Nov-2003 Bill Marr <marr at flex dot com> |
| 28 | * - Mimic Windows driver by sending 2 USB 'device request' messages |
| 29 | * following normal 'baud rate change' message. This allows data to be |
| 30 | * transmitted to RS-232 devices which don't assert the 'CTS' signal. |
| 31 | * |
| 32 | * 10-Nov-2001 Wolfgang Grandegger |
| 33 | * - Fixed an endianess problem with the baudrate selection for PowerPC. |
| 34 | * |
| 35 | * 06-Dec-2001 Martin Hamilton <martinh@gnu.org> |
| 36 | * Added support for the Belkin F5U109 DB9 adaptor |
| 37 | * |
| 38 | * 30-May-2001 Greg Kroah-Hartman |
| 39 | * switched from using spinlock to a semaphore, which fixes lots of problems. |
| 40 | * |
| 41 | * 04-May-2001 Stelian Pop |
| 42 | * - Set the maximum bulk output size for Sitecom U232-P25 model to 16 bytes |
| 43 | * instead of the device reported 32 (using 32 bytes causes many data |
| 44 | * loss, Windows driver uses 16 too). |
| 45 | * |
| 46 | * 02-May-2001 Stelian Pop |
| 47 | * - Fixed the baud calculation for Sitecom U232-P25 model |
| 48 | * |
| 49 | * 08-Apr-2001 gb |
| 50 | * - Identify version on module load. |
| 51 | * |
| 52 | * 06-Jan-2001 Cornel Ciocirlan |
| 53 | * - Added support for Sitecom U232-P25 model (Product Id 0x0230) |
| 54 | * - Added support for D-Link DU-H3SP USB BAY (Product Id 0x0200) |
| 55 | * |
| 56 | * 29-Nov-2000 Greg Kroah-Hartman |
| 57 | * - Added device id table to fit with 2.4.0-test11 structure. |
| 58 | * - took out DEAL_WITH_TWO_INT_IN_ENDPOINTS #define as it's not needed |
| 59 | * (lots of things will change if/when the usb-serial core changes to |
| 60 | * handle these issues. |
| 61 | * |
| 62 | * 27-Nov-2000 Wolfgang Grandegger |
| 63 | * A version for kernel 2.4.0-test10 released to the Linux community |
| 64 | * (via linux-usb-devel). |
| 65 | */ |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | #include <linux/kernel.h> |
| 68 | #include <linux/errno.h> |
| 69 | #include <linux/init.h> |
| 70 | #include <linux/slab.h> |
| 71 | #include <linux/tty.h> |
| 72 | #include <linux/tty_driver.h> |
| 73 | #include <linux/tty_flip.h> |
| 74 | #include <linux/module.h> |
| 75 | #include <linux/spinlock.h> |
| 76 | #include <asm/uaccess.h> |
| 77 | #include <linux/usb.h> |
Greg Kroah-Hartman | a969888 | 2006-07-11 21:22:58 -0700 | [diff] [blame] | 78 | #include <linux/usb/serial.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | #include "mct_u232.h" |
| 80 | |
| 81 | /* |
| 82 | * Version Information |
| 83 | */ |
| 84 | #define DRIVER_VERSION "z2.0" /* Linux in-kernel version */ |
| 85 | #define DRIVER_AUTHOR "Wolfgang Grandegger <wolfgang@ces.ch>" |
| 86 | #define DRIVER_DESC "Magic Control Technology USB-RS232 converter driver" |
| 87 | |
| 88 | static int debug; |
| 89 | |
| 90 | /* |
| 91 | * Function prototypes |
| 92 | */ |
| 93 | static int mct_u232_startup (struct usb_serial *serial); |
| 94 | static void mct_u232_shutdown (struct usb_serial *serial); |
| 95 | static int mct_u232_open (struct usb_serial_port *port, |
| 96 | struct file *filp); |
| 97 | static void mct_u232_close (struct usb_serial_port *port, |
| 98 | struct file *filp); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 99 | static void mct_u232_read_int_callback (struct urb *urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | static void mct_u232_set_termios (struct usb_serial_port *port, |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame^] | 101 | struct ktermios * old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | static int mct_u232_ioctl (struct usb_serial_port *port, |
| 103 | struct file * file, |
| 104 | unsigned int cmd, |
| 105 | unsigned long arg); |
| 106 | static void mct_u232_break_ctl (struct usb_serial_port *port, |
| 107 | int break_state ); |
| 108 | static int mct_u232_tiocmget (struct usb_serial_port *port, |
| 109 | struct file *file); |
| 110 | static int mct_u232_tiocmset (struct usb_serial_port *port, |
| 111 | struct file *file, unsigned int set, |
| 112 | unsigned int clear); |
| 113 | /* |
| 114 | * All of the device info needed for the MCT USB-RS232 converter. |
| 115 | */ |
| 116 | static struct usb_device_id id_table_combined [] = { |
| 117 | { USB_DEVICE(MCT_U232_VID, MCT_U232_PID) }, |
| 118 | { USB_DEVICE(MCT_U232_VID, MCT_U232_SITECOM_PID) }, |
| 119 | { USB_DEVICE(MCT_U232_VID, MCT_U232_DU_H3SP_PID) }, |
| 120 | { USB_DEVICE(MCT_U232_BELKIN_F5U109_VID, MCT_U232_BELKIN_F5U109_PID) }, |
| 121 | { } /* Terminating entry */ |
| 122 | }; |
| 123 | |
| 124 | MODULE_DEVICE_TABLE (usb, id_table_combined); |
| 125 | |
| 126 | static struct usb_driver mct_u232_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | .name = "mct_u232", |
| 128 | .probe = usb_serial_probe, |
| 129 | .disconnect = usb_serial_disconnect, |
| 130 | .id_table = id_table_combined, |
Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 131 | .no_dynamic_id = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | }; |
| 133 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 134 | static struct usb_serial_driver mct_u232_device = { |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 135 | .driver = { |
| 136 | .owner = THIS_MODULE, |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 137 | .name = "mct_u232", |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 138 | }, |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 139 | .description = "MCT U232", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | .id_table = id_table_combined, |
| 141 | .num_interrupt_in = 2, |
| 142 | .num_bulk_in = 0, |
| 143 | .num_bulk_out = 1, |
| 144 | .num_ports = 1, |
| 145 | .open = mct_u232_open, |
| 146 | .close = mct_u232_close, |
| 147 | .read_int_callback = mct_u232_read_int_callback, |
| 148 | .ioctl = mct_u232_ioctl, |
| 149 | .set_termios = mct_u232_set_termios, |
| 150 | .break_ctl = mct_u232_break_ctl, |
| 151 | .tiocmget = mct_u232_tiocmget, |
| 152 | .tiocmset = mct_u232_tiocmset, |
| 153 | .attach = mct_u232_startup, |
| 154 | .shutdown = mct_u232_shutdown, |
| 155 | }; |
| 156 | |
| 157 | |
| 158 | struct mct_u232_private { |
| 159 | spinlock_t lock; |
| 160 | unsigned int control_state; /* Modem Line Setting (TIOCM) */ |
| 161 | unsigned char last_lcr; /* Line Control Register */ |
| 162 | unsigned char last_lsr; /* Line Status Register */ |
| 163 | unsigned char last_msr; /* Modem Status Register */ |
| 164 | }; |
| 165 | |
| 166 | /* |
| 167 | * Handle vendor specific USB requests |
| 168 | */ |
| 169 | |
| 170 | #define WDR_TIMEOUT 5000 /* default urb timeout */ |
| 171 | |
| 172 | /* |
| 173 | * Later day 2.6.0-test kernels have new baud rates like B230400 which |
| 174 | * we do not know how to support. We ignore them for the moment. |
| 175 | * XXX Rate-limit the error message, it's user triggerable. |
| 176 | */ |
| 177 | static int mct_u232_calculate_baud_rate(struct usb_serial *serial, int value) |
| 178 | { |
| 179 | if (le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_SITECOM_PID |
| 180 | || le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_BELKIN_F5U109_PID) { |
| 181 | switch (value) { |
| 182 | case B300: return 0x01; |
| 183 | case B600: return 0x02; /* this one not tested */ |
| 184 | case B1200: return 0x03; |
| 185 | case B2400: return 0x04; |
| 186 | case B4800: return 0x06; |
| 187 | case B9600: return 0x08; |
| 188 | case B19200: return 0x09; |
| 189 | case B38400: return 0x0a; |
| 190 | case B57600: return 0x0b; |
| 191 | case B115200: return 0x0c; |
| 192 | default: |
| 193 | err("MCT USB-RS232: unsupported baudrate request 0x%x," |
| 194 | " using default of B9600", value); |
| 195 | return 0x08; |
| 196 | } |
| 197 | } else { |
| 198 | switch (value) { |
| 199 | case B300: value = 300; break; |
| 200 | case B600: value = 600; break; |
| 201 | case B1200: value = 1200; break; |
| 202 | case B2400: value = 2400; break; |
| 203 | case B4800: value = 4800; break; |
| 204 | case B9600: value = 9600; break; |
| 205 | case B19200: value = 19200; break; |
| 206 | case B38400: value = 38400; break; |
| 207 | case B57600: value = 57600; break; |
| 208 | case B115200: value = 115200; break; |
| 209 | default: |
| 210 | err("MCT USB-RS232: unsupported baudrate request 0x%x," |
| 211 | " using default of B9600", value); |
| 212 | value = 9600; |
| 213 | } |
| 214 | return 115200/value; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | static int mct_u232_set_baud_rate(struct usb_serial *serial, int value) |
| 219 | { |
| 220 | __le32 divisor; |
| 221 | int rc; |
| 222 | unsigned char zero_byte = 0; |
| 223 | |
| 224 | divisor = cpu_to_le32(mct_u232_calculate_baud_rate(serial, value)); |
| 225 | |
| 226 | rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), |
| 227 | MCT_U232_SET_BAUD_RATE_REQUEST, |
| 228 | MCT_U232_SET_REQUEST_TYPE, |
| 229 | 0, 0, &divisor, MCT_U232_SET_BAUD_RATE_SIZE, |
| 230 | WDR_TIMEOUT); |
| 231 | if (rc < 0) |
| 232 | err("Set BAUD RATE %d failed (error = %d)", value, rc); |
| 233 | dbg("set_baud_rate: value: 0x%x, divisor: 0x%x", value, divisor); |
| 234 | |
| 235 | /* Mimic the MCT-supplied Windows driver (version 1.21P.0104), which |
| 236 | always sends two extra USB 'device request' messages after the |
| 237 | 'baud rate change' message. The actual functionality of the |
| 238 | request codes in these messages is not fully understood but these |
| 239 | particular codes are never seen in any operation besides a baud |
| 240 | rate change. Both of these messages send a single byte of data |
| 241 | whose value is always zero. The second of these two extra messages |
| 242 | is required in order for data to be properly written to an RS-232 |
| 243 | device which does not assert the 'CTS' signal. */ |
| 244 | |
| 245 | rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), |
| 246 | MCT_U232_SET_UNKNOWN1_REQUEST, |
| 247 | MCT_U232_SET_REQUEST_TYPE, |
| 248 | 0, 0, &zero_byte, MCT_U232_SET_UNKNOWN1_SIZE, |
| 249 | WDR_TIMEOUT); |
| 250 | if (rc < 0) |
| 251 | err("Sending USB device request code %d failed (error = %d)", |
| 252 | MCT_U232_SET_UNKNOWN1_REQUEST, rc); |
| 253 | |
| 254 | rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), |
| 255 | MCT_U232_SET_UNKNOWN2_REQUEST, |
| 256 | MCT_U232_SET_REQUEST_TYPE, |
| 257 | 0, 0, &zero_byte, MCT_U232_SET_UNKNOWN2_SIZE, |
| 258 | WDR_TIMEOUT); |
| 259 | if (rc < 0) |
| 260 | err("Sending USB device request code %d failed (error = %d)", |
| 261 | MCT_U232_SET_UNKNOWN2_REQUEST, rc); |
| 262 | |
| 263 | return rc; |
| 264 | } /* mct_u232_set_baud_rate */ |
| 265 | |
| 266 | static int mct_u232_set_line_ctrl(struct usb_serial *serial, unsigned char lcr) |
| 267 | { |
| 268 | int rc; |
| 269 | rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), |
| 270 | MCT_U232_SET_LINE_CTRL_REQUEST, |
| 271 | MCT_U232_SET_REQUEST_TYPE, |
| 272 | 0, 0, &lcr, MCT_U232_SET_LINE_CTRL_SIZE, |
| 273 | WDR_TIMEOUT); |
| 274 | if (rc < 0) |
| 275 | err("Set LINE CTRL 0x%x failed (error = %d)", lcr, rc); |
| 276 | dbg("set_line_ctrl: 0x%x", lcr); |
| 277 | return rc; |
| 278 | } /* mct_u232_set_line_ctrl */ |
| 279 | |
| 280 | static int mct_u232_set_modem_ctrl(struct usb_serial *serial, |
| 281 | unsigned int control_state) |
| 282 | { |
| 283 | int rc; |
| 284 | unsigned char mcr = MCT_U232_MCR_NONE; |
| 285 | |
| 286 | if (control_state & TIOCM_DTR) |
| 287 | mcr |= MCT_U232_MCR_DTR; |
| 288 | if (control_state & TIOCM_RTS) |
| 289 | mcr |= MCT_U232_MCR_RTS; |
| 290 | |
| 291 | rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), |
| 292 | MCT_U232_SET_MODEM_CTRL_REQUEST, |
| 293 | MCT_U232_SET_REQUEST_TYPE, |
| 294 | 0, 0, &mcr, MCT_U232_SET_MODEM_CTRL_SIZE, |
| 295 | WDR_TIMEOUT); |
| 296 | if (rc < 0) |
| 297 | err("Set MODEM CTRL 0x%x failed (error = %d)", mcr, rc); |
| 298 | dbg("set_modem_ctrl: state=0x%x ==> mcr=0x%x", control_state, mcr); |
| 299 | |
| 300 | return rc; |
| 301 | } /* mct_u232_set_modem_ctrl */ |
| 302 | |
| 303 | static int mct_u232_get_modem_stat(struct usb_serial *serial, unsigned char *msr) |
| 304 | { |
| 305 | int rc; |
| 306 | rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), |
| 307 | MCT_U232_GET_MODEM_STAT_REQUEST, |
| 308 | MCT_U232_GET_REQUEST_TYPE, |
| 309 | 0, 0, msr, MCT_U232_GET_MODEM_STAT_SIZE, |
| 310 | WDR_TIMEOUT); |
| 311 | if (rc < 0) { |
| 312 | err("Get MODEM STATus failed (error = %d)", rc); |
| 313 | *msr = 0; |
| 314 | } |
| 315 | dbg("get_modem_stat: 0x%x", *msr); |
| 316 | return rc; |
| 317 | } /* mct_u232_get_modem_stat */ |
| 318 | |
| 319 | static void mct_u232_msr_to_state(unsigned int *control_state, unsigned char msr) |
| 320 | { |
| 321 | /* Translate Control Line states */ |
| 322 | if (msr & MCT_U232_MSR_DSR) |
| 323 | *control_state |= TIOCM_DSR; |
| 324 | else |
| 325 | *control_state &= ~TIOCM_DSR; |
| 326 | if (msr & MCT_U232_MSR_CTS) |
| 327 | *control_state |= TIOCM_CTS; |
| 328 | else |
| 329 | *control_state &= ~TIOCM_CTS; |
| 330 | if (msr & MCT_U232_MSR_RI) |
| 331 | *control_state |= TIOCM_RI; |
| 332 | else |
| 333 | *control_state &= ~TIOCM_RI; |
| 334 | if (msr & MCT_U232_MSR_CD) |
| 335 | *control_state |= TIOCM_CD; |
| 336 | else |
| 337 | *control_state &= ~TIOCM_CD; |
| 338 | dbg("msr_to_state: msr=0x%x ==> state=0x%x", msr, *control_state); |
| 339 | } /* mct_u232_msr_to_state */ |
| 340 | |
| 341 | /* |
| 342 | * Driver's tty interface functions |
| 343 | */ |
| 344 | |
| 345 | static int mct_u232_startup (struct usb_serial *serial) |
| 346 | { |
| 347 | struct mct_u232_private *priv; |
| 348 | struct usb_serial_port *port, *rport; |
| 349 | |
Eric Sesterhenn | 80b6ca4 | 2006-02-27 21:29:43 +0100 | [diff] [blame] | 350 | priv = kzalloc(sizeof(struct mct_u232_private), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | if (!priv) |
| 352 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | spin_lock_init(&priv->lock); |
| 354 | usb_set_serial_port_data(serial->port[0], priv); |
| 355 | |
| 356 | init_waitqueue_head(&serial->port[0]->write_wait); |
| 357 | |
| 358 | /* Puh, that's dirty */ |
| 359 | port = serial->port[0]; |
| 360 | rport = serial->port[1]; |
Mariusz Kozlowski | 73135bb | 2006-11-08 15:36:42 +0100 | [diff] [blame] | 361 | /* No unlinking, it wasn't submitted yet. */ |
| 362 | usb_free_urb(port->read_urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | port->read_urb = rport->interrupt_in_urb; |
| 364 | rport->interrupt_in_urb = NULL; |
| 365 | port->read_urb->context = port; |
| 366 | |
| 367 | return (0); |
| 368 | } /* mct_u232_startup */ |
| 369 | |
| 370 | |
| 371 | static void mct_u232_shutdown (struct usb_serial *serial) |
| 372 | { |
| 373 | struct mct_u232_private *priv; |
| 374 | int i; |
| 375 | |
| 376 | dbg("%s", __FUNCTION__); |
| 377 | |
| 378 | for (i=0; i < serial->num_ports; ++i) { |
| 379 | /* My special items, the standard routines free my urbs */ |
| 380 | priv = usb_get_serial_port_data(serial->port[i]); |
| 381 | if (priv) { |
| 382 | usb_set_serial_port_data(serial->port[i], NULL); |
| 383 | kfree(priv); |
| 384 | } |
| 385 | } |
| 386 | } /* mct_u232_shutdown */ |
| 387 | |
| 388 | static int mct_u232_open (struct usb_serial_port *port, struct file *filp) |
| 389 | { |
| 390 | struct usb_serial *serial = port->serial; |
| 391 | struct mct_u232_private *priv = usb_get_serial_port_data(port); |
| 392 | int retval = 0; |
| 393 | unsigned int control_state; |
| 394 | unsigned long flags; |
| 395 | unsigned char last_lcr; |
| 396 | unsigned char last_msr; |
| 397 | |
| 398 | dbg("%s port %d", __FUNCTION__, port->number); |
| 399 | |
| 400 | /* Compensate for a hardware bug: although the Sitecom U232-P25 |
| 401 | * device reports a maximum output packet size of 32 bytes, |
| 402 | * it seems to be able to accept only 16 bytes (and that's what |
| 403 | * SniffUSB says too...) |
| 404 | */ |
| 405 | if (le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_SITECOM_PID) |
| 406 | port->bulk_out_size = 16; |
| 407 | |
| 408 | /* Do a defined restart: the normal serial device seems to |
| 409 | * always turn on DTR and RTS here, so do the same. I'm not |
| 410 | * sure if this is really necessary. But it should not harm |
| 411 | * either. |
| 412 | */ |
| 413 | spin_lock_irqsave(&priv->lock, flags); |
| 414 | if (port->tty->termios->c_cflag & CBAUD) |
| 415 | priv->control_state = TIOCM_DTR | TIOCM_RTS; |
| 416 | else |
| 417 | priv->control_state = 0; |
| 418 | |
| 419 | priv->last_lcr = (MCT_U232_DATA_BITS_8 | |
| 420 | MCT_U232_PARITY_NONE | |
| 421 | MCT_U232_STOP_BITS_1); |
| 422 | control_state = priv->control_state; |
| 423 | last_lcr = priv->last_lcr; |
| 424 | spin_unlock_irqrestore(&priv->lock, flags); |
| 425 | mct_u232_set_modem_ctrl(serial, control_state); |
| 426 | mct_u232_set_line_ctrl(serial, last_lcr); |
| 427 | |
| 428 | /* Read modem status and update control state */ |
| 429 | mct_u232_get_modem_stat(serial, &last_msr); |
| 430 | spin_lock_irqsave(&priv->lock, flags); |
| 431 | priv->last_msr = last_msr; |
| 432 | mct_u232_msr_to_state(&priv->control_state, priv->last_msr); |
| 433 | spin_unlock_irqrestore(&priv->lock, flags); |
| 434 | |
| 435 | port->read_urb->dev = port->serial->dev; |
| 436 | retval = usb_submit_urb(port->read_urb, GFP_KERNEL); |
| 437 | if (retval) { |
| 438 | err("usb_submit_urb(read bulk) failed pipe 0x%x err %d", |
| 439 | port->read_urb->pipe, retval); |
| 440 | goto exit; |
| 441 | } |
| 442 | |
| 443 | port->interrupt_in_urb->dev = port->serial->dev; |
| 444 | retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); |
| 445 | if (retval) |
| 446 | err(" usb_submit_urb(read int) failed pipe 0x%x err %d", |
| 447 | port->interrupt_in_urb->pipe, retval); |
| 448 | |
| 449 | exit: |
| 450 | return 0; |
| 451 | } /* mct_u232_open */ |
| 452 | |
| 453 | |
| 454 | static void mct_u232_close (struct usb_serial_port *port, struct file *filp) |
| 455 | { |
| 456 | dbg("%s port %d", __FUNCTION__, port->number); |
| 457 | |
| 458 | if (port->serial->dev) { |
| 459 | /* shutdown our urbs */ |
| 460 | usb_kill_urb(port->write_urb); |
| 461 | usb_kill_urb(port->read_urb); |
| 462 | usb_kill_urb(port->interrupt_in_urb); |
| 463 | } |
| 464 | } /* mct_u232_close */ |
| 465 | |
| 466 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 467 | static void mct_u232_read_int_callback (struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | { |
| 469 | struct usb_serial_port *port = (struct usb_serial_port *)urb->context; |
| 470 | struct mct_u232_private *priv = usb_get_serial_port_data(port); |
| 471 | struct usb_serial *serial = port->serial; |
| 472 | struct tty_struct *tty; |
| 473 | unsigned char *data = urb->transfer_buffer; |
| 474 | int status; |
| 475 | unsigned long flags; |
| 476 | |
| 477 | switch (urb->status) { |
| 478 | case 0: |
| 479 | /* success */ |
| 480 | break; |
| 481 | case -ECONNRESET: |
| 482 | case -ENOENT: |
| 483 | case -ESHUTDOWN: |
| 484 | /* this urb is terminated, clean up */ |
| 485 | dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status); |
| 486 | return; |
| 487 | default: |
| 488 | dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status); |
| 489 | goto exit; |
| 490 | } |
| 491 | |
| 492 | if (!serial) { |
| 493 | dbg("%s - bad serial pointer, exiting", __FUNCTION__); |
| 494 | return; |
| 495 | } |
| 496 | |
| 497 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 498 | usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data); |
| 499 | |
| 500 | /* |
| 501 | * Work-a-round: handle the 'usual' bulk-in pipe here |
| 502 | */ |
| 503 | if (urb->transfer_buffer_length > 2) { |
| 504 | int i; |
| 505 | tty = port->tty; |
| 506 | if (urb->actual_length) { |
| 507 | for (i = 0; i < urb->actual_length ; ++i) { |
| 508 | tty_insert_flip_char(tty, data[i], 0); |
| 509 | } |
| 510 | tty_flip_buffer_push(tty); |
| 511 | } |
| 512 | goto exit; |
| 513 | } |
| 514 | |
| 515 | /* |
| 516 | * The interrupt-in pipe signals exceptional conditions (modem line |
| 517 | * signal changes and errors). data[0] holds MSR, data[1] holds LSR. |
| 518 | */ |
| 519 | spin_lock_irqsave(&priv->lock, flags); |
| 520 | priv->last_msr = data[MCT_U232_MSR_INDEX]; |
| 521 | |
| 522 | /* Record Control Line states */ |
| 523 | mct_u232_msr_to_state(&priv->control_state, priv->last_msr); |
| 524 | |
| 525 | #if 0 |
| 526 | /* Not yet handled. See belin_sa.c for further information */ |
| 527 | /* Now to report any errors */ |
| 528 | priv->last_lsr = data[MCT_U232_LSR_INDEX]; |
| 529 | /* |
| 530 | * fill in the flip buffer here, but I do not know the relation |
| 531 | * to the current/next receive buffer or characters. I need |
| 532 | * to look in to this before committing any code. |
| 533 | */ |
| 534 | if (priv->last_lsr & MCT_U232_LSR_ERR) { |
| 535 | tty = port->tty; |
| 536 | /* Overrun Error */ |
| 537 | if (priv->last_lsr & MCT_U232_LSR_OE) { |
| 538 | } |
| 539 | /* Parity Error */ |
| 540 | if (priv->last_lsr & MCT_U232_LSR_PE) { |
| 541 | } |
| 542 | /* Framing Error */ |
| 543 | if (priv->last_lsr & MCT_U232_LSR_FE) { |
| 544 | } |
| 545 | /* Break Indicator */ |
| 546 | if (priv->last_lsr & MCT_U232_LSR_BI) { |
| 547 | } |
| 548 | } |
| 549 | #endif |
| 550 | spin_unlock_irqrestore(&priv->lock, flags); |
| 551 | exit: |
| 552 | status = usb_submit_urb (urb, GFP_ATOMIC); |
| 553 | if (status) |
| 554 | err ("%s - usb_submit_urb failed with result %d", |
| 555 | __FUNCTION__, status); |
| 556 | } /* mct_u232_read_int_callback */ |
| 557 | |
| 558 | static void mct_u232_set_termios (struct usb_serial_port *port, |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame^] | 559 | struct ktermios *old_termios) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | { |
| 561 | struct usb_serial *serial = port->serial; |
| 562 | struct mct_u232_private *priv = usb_get_serial_port_data(port); |
| 563 | unsigned int iflag = port->tty->termios->c_iflag; |
| 564 | unsigned int cflag = port->tty->termios->c_cflag; |
| 565 | unsigned int old_cflag = old_termios->c_cflag; |
| 566 | unsigned long flags; |
| 567 | unsigned int control_state, new_state; |
| 568 | unsigned char last_lcr; |
| 569 | |
| 570 | /* get a local copy of the current port settings */ |
| 571 | spin_lock_irqsave(&priv->lock, flags); |
| 572 | control_state = priv->control_state; |
| 573 | spin_unlock_irqrestore(&priv->lock, flags); |
| 574 | last_lcr = 0; |
| 575 | |
| 576 | /* |
| 577 | * Update baud rate. |
| 578 | * Do not attempt to cache old rates and skip settings, |
| 579 | * disconnects screw such tricks up completely. |
| 580 | * Premature optimization is the root of all evil. |
| 581 | */ |
| 582 | |
| 583 | /* reassert DTR and (maybe) RTS on transition from B0 */ |
| 584 | if ((old_cflag & CBAUD) == B0) { |
| 585 | dbg("%s: baud was B0", __FUNCTION__); |
| 586 | control_state |= TIOCM_DTR; |
| 587 | /* don't set RTS if using hardware flow control */ |
| 588 | if (!(old_cflag & CRTSCTS)) { |
| 589 | control_state |= TIOCM_RTS; |
| 590 | } |
| 591 | mct_u232_set_modem_ctrl(serial, control_state); |
| 592 | } |
| 593 | |
| 594 | mct_u232_set_baud_rate(serial, cflag & CBAUD); |
| 595 | |
| 596 | if ((cflag & CBAUD) == B0 ) { |
| 597 | dbg("%s: baud is B0", __FUNCTION__); |
| 598 | /* Drop RTS and DTR */ |
| 599 | control_state &= ~(TIOCM_DTR | TIOCM_RTS); |
| 600 | mct_u232_set_modem_ctrl(serial, control_state); |
| 601 | } |
| 602 | |
| 603 | /* |
| 604 | * Update line control register (LCR) |
| 605 | */ |
| 606 | |
| 607 | /* set the parity */ |
| 608 | if (cflag & PARENB) |
| 609 | last_lcr |= (cflag & PARODD) ? |
| 610 | MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN; |
| 611 | else |
| 612 | last_lcr |= MCT_U232_PARITY_NONE; |
| 613 | |
| 614 | /* set the number of data bits */ |
| 615 | switch (cflag & CSIZE) { |
| 616 | case CS5: |
| 617 | last_lcr |= MCT_U232_DATA_BITS_5; break; |
| 618 | case CS6: |
| 619 | last_lcr |= MCT_U232_DATA_BITS_6; break; |
| 620 | case CS7: |
| 621 | last_lcr |= MCT_U232_DATA_BITS_7; break; |
| 622 | case CS8: |
| 623 | last_lcr |= MCT_U232_DATA_BITS_8; break; |
| 624 | default: |
| 625 | err("CSIZE was not CS5-CS8, using default of 8"); |
| 626 | last_lcr |= MCT_U232_DATA_BITS_8; |
| 627 | break; |
| 628 | } |
| 629 | |
| 630 | /* set the number of stop bits */ |
| 631 | last_lcr |= (cflag & CSTOPB) ? |
| 632 | MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1; |
| 633 | |
| 634 | mct_u232_set_line_ctrl(serial, last_lcr); |
| 635 | |
| 636 | /* |
| 637 | * Set flow control: well, I do not really now how to handle DTR/RTS. |
| 638 | * Just do what we have seen with SniffUSB on Win98. |
| 639 | */ |
| 640 | /* Drop DTR/RTS if no flow control otherwise assert */ |
| 641 | new_state = control_state; |
| 642 | if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS)) |
| 643 | new_state |= TIOCM_DTR | TIOCM_RTS; |
| 644 | else |
| 645 | new_state &= ~(TIOCM_DTR | TIOCM_RTS); |
| 646 | if (new_state != control_state) { |
| 647 | mct_u232_set_modem_ctrl(serial, new_state); |
| 648 | control_state = new_state; |
| 649 | } |
| 650 | |
| 651 | /* save off the modified port settings */ |
| 652 | spin_lock_irqsave(&priv->lock, flags); |
| 653 | priv->control_state = control_state; |
| 654 | priv->last_lcr = last_lcr; |
| 655 | spin_unlock_irqrestore(&priv->lock, flags); |
| 656 | } /* mct_u232_set_termios */ |
| 657 | |
| 658 | static void mct_u232_break_ctl( struct usb_serial_port *port, int break_state ) |
| 659 | { |
| 660 | struct usb_serial *serial = port->serial; |
| 661 | struct mct_u232_private *priv = usb_get_serial_port_data(port); |
| 662 | unsigned char lcr; |
| 663 | unsigned long flags; |
| 664 | |
| 665 | dbg("%sstate=%d", __FUNCTION__, break_state); |
| 666 | |
| 667 | spin_lock_irqsave(&priv->lock, flags); |
| 668 | lcr = priv->last_lcr; |
| 669 | spin_unlock_irqrestore(&priv->lock, flags); |
| 670 | |
| 671 | if (break_state) |
| 672 | lcr |= MCT_U232_SET_BREAK; |
| 673 | |
| 674 | mct_u232_set_line_ctrl(serial, lcr); |
| 675 | } /* mct_u232_break_ctl */ |
| 676 | |
| 677 | |
| 678 | static int mct_u232_tiocmget (struct usb_serial_port *port, struct file *file) |
| 679 | { |
| 680 | struct mct_u232_private *priv = usb_get_serial_port_data(port); |
| 681 | unsigned int control_state; |
| 682 | unsigned long flags; |
| 683 | |
| 684 | dbg("%s", __FUNCTION__); |
| 685 | |
| 686 | spin_lock_irqsave(&priv->lock, flags); |
| 687 | control_state = priv->control_state; |
| 688 | spin_unlock_irqrestore(&priv->lock, flags); |
| 689 | |
| 690 | return control_state; |
| 691 | } |
| 692 | |
| 693 | static int mct_u232_tiocmset (struct usb_serial_port *port, struct file *file, |
| 694 | unsigned int set, unsigned int clear) |
| 695 | { |
| 696 | struct usb_serial *serial = port->serial; |
| 697 | struct mct_u232_private *priv = usb_get_serial_port_data(port); |
| 698 | unsigned int control_state; |
| 699 | unsigned long flags; |
| 700 | |
| 701 | dbg("%s", __FUNCTION__); |
| 702 | |
| 703 | spin_lock_irqsave(&priv->lock, flags); |
| 704 | control_state = priv->control_state; |
| 705 | |
| 706 | if (set & TIOCM_RTS) |
| 707 | control_state |= TIOCM_RTS; |
| 708 | if (set & TIOCM_DTR) |
| 709 | control_state |= TIOCM_DTR; |
| 710 | if (clear & TIOCM_RTS) |
| 711 | control_state &= ~TIOCM_RTS; |
| 712 | if (clear & TIOCM_DTR) |
| 713 | control_state &= ~TIOCM_DTR; |
| 714 | |
| 715 | priv->control_state = control_state; |
| 716 | spin_unlock_irqrestore(&priv->lock, flags); |
| 717 | return mct_u232_set_modem_ctrl(serial, control_state); |
| 718 | } |
| 719 | |
| 720 | static int mct_u232_ioctl (struct usb_serial_port *port, struct file * file, |
| 721 | unsigned int cmd, unsigned long arg) |
| 722 | { |
| 723 | dbg("%scmd=0x%x", __FUNCTION__, cmd); |
| 724 | |
| 725 | /* Based on code from acm.c and others */ |
| 726 | switch (cmd) { |
| 727 | case TIOCMIWAIT: |
| 728 | /* wait for any of the 4 modem inputs (DCD,RI,DSR,CTS)*/ |
| 729 | /* TODO */ |
| 730 | return( 0 ); |
| 731 | |
| 732 | case TIOCGICOUNT: |
| 733 | /* return count of modemline transitions */ |
| 734 | /* TODO */ |
| 735 | return 0; |
| 736 | |
| 737 | default: |
| 738 | dbg("%s: arg not supported - 0x%04x", __FUNCTION__,cmd); |
| 739 | return(-ENOIOCTLCMD); |
| 740 | break; |
| 741 | } |
| 742 | return 0; |
| 743 | } /* mct_u232_ioctl */ |
| 744 | |
| 745 | |
| 746 | static int __init mct_u232_init (void) |
| 747 | { |
| 748 | int retval; |
| 749 | retval = usb_serial_register(&mct_u232_device); |
| 750 | if (retval) |
| 751 | goto failed_usb_serial_register; |
| 752 | retval = usb_register(&mct_u232_driver); |
| 753 | if (retval) |
| 754 | goto failed_usb_register; |
| 755 | info(DRIVER_DESC " " DRIVER_VERSION); |
| 756 | return 0; |
| 757 | failed_usb_register: |
| 758 | usb_serial_deregister(&mct_u232_device); |
| 759 | failed_usb_serial_register: |
| 760 | return retval; |
| 761 | } |
| 762 | |
| 763 | |
| 764 | static void __exit mct_u232_exit (void) |
| 765 | { |
| 766 | usb_deregister (&mct_u232_driver); |
| 767 | usb_serial_deregister (&mct_u232_device); |
| 768 | } |
| 769 | |
| 770 | |
| 771 | module_init (mct_u232_init); |
| 772 | module_exit(mct_u232_exit); |
| 773 | |
| 774 | MODULE_AUTHOR( DRIVER_AUTHOR ); |
| 775 | MODULE_DESCRIPTION( DRIVER_DESC ); |
| 776 | MODULE_LICENSE("GPL"); |
| 777 | |
| 778 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
| 779 | MODULE_PARM_DESC(debug, "Debug enabled or not"); |