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