Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | Option Card (PCMCIA to) USB to Serial Driver |
| 3 | |
| 4 | Copyright (C) 2005 Matthias Urlichs <smurf@smurf.noris.de> |
| 5 | |
| 6 | This driver is free software; you can redistribute it and/or modify |
| 7 | it under the terms of Version 2 of the GNU General Public License as |
| 8 | published by the Free Software Foundation. |
| 9 | |
| 10 | Portions copied from the Keyspan driver by Hugh Blemings <hugh@blemings.org> |
| 11 | |
| 12 | History: |
| 13 | |
| 14 | 2005-05-19 v0.1 Initial version, based on incomplete docs |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 15 | and analysis of misbehavior with the standard driver |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 16 | 2005-05-20 v0.2 Extended the input buffer to avoid losing |
| 17 | random 64-byte chunks of data |
| 18 | 2005-05-21 v0.3 implemented chars_in_buffer() |
| 19 | turned on low_latency |
| 20 | simplified the code somewhat |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 21 | 2005-05-24 v0.4 option_write() sometimes deadlocked under heavy load |
| 22 | removed some dead code |
| 23 | added sponsor notice |
| 24 | coding style clean-up |
| 25 | 2005-06-20 v0.4.1 add missing braces :-/ |
| 26 | killed end-of-line whitespace |
| 27 | 2005-07-15 v0.4.2 rename WLAN product to FUSION, add FUSION2 |
| 28 | |
| 29 | Work sponsored by: Sigos GmbH, Germany <info@sigos.de> |
| 30 | |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 31 | */ |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 32 | |
| 33 | #define DRIVER_VERSION "v0.4" |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 34 | #define DRIVER_AUTHOR "Matthias Urlichs <smurf@smurf.noris.de>" |
| 35 | #define DRIVER_DESC "Option Card (PC-Card to) USB to Serial Driver" |
| 36 | |
| 37 | #include <linux/config.h> |
| 38 | #include <linux/kernel.h> |
| 39 | #include <linux/jiffies.h> |
| 40 | #include <linux/errno.h> |
| 41 | #include <linux/tty.h> |
| 42 | #include <linux/tty_flip.h> |
| 43 | #include <linux/module.h> |
| 44 | #include <linux/usb.h> |
| 45 | #include "usb-serial.h" |
| 46 | |
| 47 | /* Function prototypes */ |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 48 | static int option_open(struct usb_serial_port *port, struct file *filp); |
| 49 | static void option_close(struct usb_serial_port *port, struct file *filp); |
| 50 | static int option_startup(struct usb_serial *serial); |
| 51 | static void option_shutdown(struct usb_serial *serial); |
| 52 | static void option_rx_throttle(struct usb_serial_port *port); |
| 53 | static void option_rx_unthrottle(struct usb_serial_port *port); |
| 54 | static int option_write_room(struct usb_serial_port *port); |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 55 | |
| 56 | static void option_instat_callback(struct urb *urb, struct pt_regs *regs); |
| 57 | |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 58 | static int option_write(struct usb_serial_port *port, |
| 59 | const unsigned char *buf, int count); |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 60 | |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 61 | static int option_chars_in_buffer(struct usb_serial_port *port); |
| 62 | static int option_ioctl(struct usb_serial_port *port, struct file *file, |
| 63 | unsigned int cmd, unsigned long arg); |
| 64 | static void option_set_termios(struct usb_serial_port *port, |
| 65 | struct termios *old); |
| 66 | static void option_break_ctl(struct usb_serial_port *port, int break_state); |
| 67 | static int option_tiocmget(struct usb_serial_port *port, struct file *file); |
| 68 | static int option_tiocmset(struct usb_serial_port *port, struct file *file, |
| 69 | unsigned int set, unsigned int clear); |
| 70 | static int option_send_setup(struct usb_serial_port *port); |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 71 | |
| 72 | /* Vendor and product IDs */ |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 73 | #define OPTION_VENDOR_ID 0x0AF0 |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 74 | |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 75 | #define OPTION_PRODUCT_OLD 0x5000 |
| 76 | #define OPTION_PRODUCT_FUSION 0x6000 |
| 77 | #define OPTION_PRODUCT_FUSION2 0x6300 |
| 78 | |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 79 | static struct usb_device_id option_ids[] = { |
| 80 | { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_OLD) }, |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 81 | { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION) }, |
| 82 | { USB_DEVICE(OPTION_VENDOR_ID, OPTION_PRODUCT_FUSION2) }, |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 83 | { } /* Terminating entry */ |
| 84 | }; |
| 85 | |
| 86 | MODULE_DEVICE_TABLE(usb, option_ids); |
| 87 | |
| 88 | static struct usb_driver option_driver = { |
| 89 | .owner = THIS_MODULE, |
| 90 | .name = "option", |
| 91 | .probe = usb_serial_probe, |
| 92 | .disconnect = usb_serial_disconnect, |
| 93 | .id_table = option_ids, |
| 94 | }; |
| 95 | |
| 96 | /* The card has three separate interfaces, wich the serial driver |
| 97 | * recognizes separately, thus num_port=1. |
| 98 | */ |
| 99 | static struct usb_serial_device_type option_3port_device = { |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 100 | .owner = THIS_MODULE, |
| 101 | .name = "Option 3G data card", |
| 102 | .short_name = "option", |
| 103 | .id_table = option_ids, |
| 104 | .num_interrupt_in = NUM_DONT_CARE, |
| 105 | .num_bulk_in = NUM_DONT_CARE, |
| 106 | .num_bulk_out = NUM_DONT_CARE, |
| 107 | .num_ports = 1, /* 3, but the card reports its ports separately */ |
| 108 | .open = option_open, |
| 109 | .close = option_close, |
| 110 | .write = option_write, |
| 111 | .write_room = option_write_room, |
| 112 | .chars_in_buffer = option_chars_in_buffer, |
| 113 | .throttle = option_rx_throttle, |
| 114 | .unthrottle = option_rx_unthrottle, |
| 115 | .ioctl = option_ioctl, |
| 116 | .set_termios = option_set_termios, |
| 117 | .break_ctl = option_break_ctl, |
| 118 | .tiocmget = option_tiocmget, |
| 119 | .tiocmset = option_tiocmset, |
| 120 | .attach = option_startup, |
| 121 | .shutdown = option_shutdown, |
| 122 | .read_int_callback = option_instat_callback, |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 125 | #ifdef CONFIG_USB_DEBUG |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 126 | static int debug; |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 127 | #else |
| 128 | #define debug 0 |
| 129 | #endif |
| 130 | |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 131 | /* per port private data */ |
| 132 | |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 133 | #define N_IN_URB 4 |
| 134 | #define N_OUT_URB 1 |
| 135 | #define IN_BUFLEN 1024 |
| 136 | #define OUT_BUFLEN 128 |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 137 | |
| 138 | struct option_port_private { |
| 139 | /* Input endpoints and buffer for this port */ |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 140 | struct urb *in_urbs[N_IN_URB]; |
| 141 | char in_buffer[N_IN_URB][IN_BUFLEN]; |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 142 | /* Output endpoints and buffer for this port */ |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 143 | struct urb *out_urbs[N_OUT_URB]; |
| 144 | char out_buffer[N_OUT_URB][OUT_BUFLEN]; |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 145 | |
| 146 | /* Settings for the port */ |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 147 | int rts_state; /* Handshaking pins (outputs) */ |
| 148 | int dtr_state; |
| 149 | int cts_state; /* Handshaking pins (inputs) */ |
| 150 | int dsr_state; |
| 151 | int dcd_state; |
| 152 | int ri_state; |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 153 | |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 154 | unsigned long tx_start_time[N_OUT_URB]; |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 157 | /* Functions used by new usb-serial code. */ |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 158 | static int __init option_init(void) |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 159 | { |
| 160 | int retval; |
| 161 | retval = usb_serial_register(&option_3port_device); |
| 162 | if (retval) |
| 163 | goto failed_3port_device_register; |
| 164 | retval = usb_register(&option_driver); |
| 165 | if (retval) |
| 166 | goto failed_driver_register; |
| 167 | |
| 168 | info(DRIVER_DESC ": " DRIVER_VERSION); |
| 169 | |
| 170 | return 0; |
| 171 | |
| 172 | failed_driver_register: |
| 173 | usb_serial_deregister (&option_3port_device); |
| 174 | failed_3port_device_register: |
| 175 | return retval; |
| 176 | } |
| 177 | |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 178 | static void __exit option_exit(void) |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 179 | { |
| 180 | usb_deregister (&option_driver); |
| 181 | usb_serial_deregister (&option_3port_device); |
| 182 | } |
| 183 | |
| 184 | module_init(option_init); |
| 185 | module_exit(option_exit); |
| 186 | |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 187 | static void option_rx_throttle(struct usb_serial_port *port) |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 188 | { |
| 189 | dbg("%s", __FUNCTION__); |
| 190 | } |
| 191 | |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 192 | static void option_rx_unthrottle(struct usb_serial_port *port) |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 193 | { |
| 194 | dbg("%s", __FUNCTION__); |
| 195 | } |
| 196 | |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 197 | static void option_break_ctl(struct usb_serial_port *port, int break_state) |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 198 | { |
| 199 | /* Unfortunately, I don't know how to send a break */ |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 200 | dbg("%s", __FUNCTION__); |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 203 | static void option_set_termios(struct usb_serial_port *port, |
| 204 | struct termios *old_termios) |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 205 | { |
| 206 | dbg("%s", __FUNCTION__); |
| 207 | |
| 208 | option_send_setup(port); |
| 209 | } |
| 210 | |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 211 | static int option_tiocmget(struct usb_serial_port *port, struct file *file) |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 212 | { |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 213 | unsigned int value; |
| 214 | struct option_port_private *portdata; |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 215 | |
| 216 | portdata = usb_get_serial_port_data(port); |
| 217 | |
| 218 | value = ((portdata->rts_state) ? TIOCM_RTS : 0) | |
| 219 | ((portdata->dtr_state) ? TIOCM_DTR : 0) | |
| 220 | ((portdata->cts_state) ? TIOCM_CTS : 0) | |
| 221 | ((portdata->dsr_state) ? TIOCM_DSR : 0) | |
| 222 | ((portdata->dcd_state) ? TIOCM_CAR : 0) | |
| 223 | ((portdata->ri_state) ? TIOCM_RNG : 0); |
| 224 | |
| 225 | return value; |
| 226 | } |
| 227 | |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 228 | static int option_tiocmset(struct usb_serial_port *port, struct file *file, |
| 229 | unsigned int set, unsigned int clear) |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 230 | { |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 231 | struct option_port_private *portdata; |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 232 | |
| 233 | portdata = usb_get_serial_port_data(port); |
| 234 | |
| 235 | if (set & TIOCM_RTS) |
| 236 | portdata->rts_state = 1; |
| 237 | if (set & TIOCM_DTR) |
| 238 | portdata->dtr_state = 1; |
| 239 | |
| 240 | if (clear & TIOCM_RTS) |
| 241 | portdata->rts_state = 0; |
| 242 | if (clear & TIOCM_DTR) |
| 243 | portdata->dtr_state = 0; |
| 244 | return option_send_setup(port); |
| 245 | } |
| 246 | |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 247 | static int option_ioctl(struct usb_serial_port *port, struct file *file, |
| 248 | unsigned int cmd, unsigned long arg) |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 249 | { |
| 250 | return -ENOIOCTLCMD; |
| 251 | } |
| 252 | |
| 253 | /* Write */ |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 254 | static int option_write(struct usb_serial_port *port, |
| 255 | const unsigned char *buf, int count) |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 256 | { |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 257 | struct option_port_private *portdata; |
| 258 | int i; |
| 259 | int left, todo; |
| 260 | struct urb *this_urb = NULL; /* spurious */ |
| 261 | int err; |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 262 | |
| 263 | portdata = usb_get_serial_port_data(port); |
| 264 | |
| 265 | dbg("%s: write (%d chars)", __FUNCTION__, count); |
| 266 | |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 267 | i = 0; |
| 268 | left = count; |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 269 | for (i=0; left > 0 && i < N_OUT_URB; i++) { |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 270 | todo = left; |
| 271 | if (todo > OUT_BUFLEN) |
| 272 | todo = OUT_BUFLEN; |
| 273 | |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 274 | this_urb = portdata->out_urbs[i]; |
| 275 | if (this_urb->status == -EINPROGRESS) { |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 276 | if (this_urb->transfer_flags & URB_ASYNC_UNLINK) |
| 277 | continue; |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 278 | if (time_before(jiffies, |
| 279 | portdata->tx_start_time[i] + 10 * HZ)) |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 280 | continue; |
| 281 | this_urb->transfer_flags |= URB_ASYNC_UNLINK; |
| 282 | usb_unlink_urb(this_urb); |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 283 | continue; |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 284 | } |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 285 | if (this_urb->status != 0) |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 286 | dbg("usb_write %p failed (err=%d)", |
| 287 | this_urb, this_urb->status); |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 288 | |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 289 | dbg("%s: endpoint %d buf %d", __FUNCTION__, |
| 290 | usb_pipeendpoint(this_urb->pipe), i); |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 291 | |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 292 | /* send the data */ |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 293 | memcpy (this_urb->transfer_buffer, buf, todo); |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 294 | this_urb->transfer_buffer_length = todo; |
| 295 | |
| 296 | this_urb->transfer_flags &= ~URB_ASYNC_UNLINK; |
| 297 | this_urb->dev = port->serial->dev; |
| 298 | err = usb_submit_urb(this_urb, GFP_ATOMIC); |
| 299 | if (err) { |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 300 | dbg("usb_submit_urb %p (write bulk) failed " |
| 301 | "(%d, has %d)", this_urb, |
| 302 | err, this_urb->status); |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 303 | continue; |
| 304 | } |
| 305 | portdata->tx_start_time[i] = jiffies; |
| 306 | buf += todo; |
| 307 | left -= todo; |
| 308 | } |
| 309 | |
| 310 | count -= left; |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 311 | dbg("%s: wrote (did %d)", __FUNCTION__, count); |
| 312 | return count; |
| 313 | } |
| 314 | |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 315 | static void option_indat_callback(struct urb *urb, struct pt_regs *regs) |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 316 | { |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 317 | int i, err; |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 318 | int endpoint; |
| 319 | struct usb_serial_port *port; |
| 320 | struct tty_struct *tty; |
| 321 | unsigned char *data = urb->transfer_buffer; |
| 322 | |
| 323 | dbg("%s: %p", __FUNCTION__, urb); |
| 324 | |
| 325 | endpoint = usb_pipeendpoint(urb->pipe); |
| 326 | port = (struct usb_serial_port *) urb->context; |
| 327 | |
| 328 | if (urb->status) { |
| 329 | dbg("%s: nonzero status: %d on endpoint %02x.", |
| 330 | __FUNCTION__, urb->status, endpoint); |
| 331 | } else { |
| 332 | tty = port->tty; |
| 333 | if (urb->actual_length) { |
| 334 | for (i = 0; i < urb->actual_length ; ++i) { |
| 335 | if (tty->flip.count >= TTY_FLIPBUF_SIZE) |
| 336 | tty_flip_buffer_push(tty); |
| 337 | tty_insert_flip_char(tty, data[i], 0); |
| 338 | } |
| 339 | tty_flip_buffer_push(tty); |
| 340 | } else { |
| 341 | dbg("%s: empty read urb received", __FUNCTION__); |
| 342 | } |
| 343 | |
| 344 | /* Resubmit urb so we continue receiving */ |
| 345 | if (port->open_count && urb->status != -ESHUTDOWN) { |
| 346 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 347 | if (err) |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 348 | printk(KERN_ERR "%s: resubmit read urb failed. " |
| 349 | "(%d)", __FUNCTION__, err); |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | return; |
| 353 | } |
| 354 | |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 355 | static void option_outdat_callback(struct urb *urb, struct pt_regs *regs) |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 356 | { |
| 357 | struct usb_serial_port *port; |
| 358 | |
| 359 | dbg("%s", __FUNCTION__); |
| 360 | |
| 361 | port = (struct usb_serial_port *) urb->context; |
| 362 | |
| 363 | if (port->open_count) |
| 364 | schedule_work(&port->work); |
| 365 | } |
| 366 | |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 367 | static void option_instat_callback(struct urb *urb, struct pt_regs *regs) |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 368 | { |
| 369 | int err; |
| 370 | struct usb_serial_port *port = (struct usb_serial_port *) urb->context; |
| 371 | struct option_port_private *portdata = usb_get_serial_port_data(port); |
| 372 | struct usb_serial *serial = port->serial; |
| 373 | |
| 374 | dbg("%s", __FUNCTION__); |
| 375 | dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata); |
| 376 | |
| 377 | if (urb->status == 0) { |
| 378 | struct usb_ctrlrequest *req_pkt = |
| 379 | (struct usb_ctrlrequest *)urb->transfer_buffer; |
| 380 | |
| 381 | if (!req_pkt) { |
| 382 | dbg("%s: NULL req_pkt\n", __FUNCTION__); |
| 383 | return; |
| 384 | } |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 385 | if ((req_pkt->bRequestType == 0xA1) && |
| 386 | (req_pkt->bRequest == 0x20)) { |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 387 | int old_dcd_state; |
| 388 | unsigned char signals = *((unsigned char *) |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 389 | urb->transfer_buffer + |
| 390 | sizeof(struct usb_ctrlrequest)); |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 391 | |
| 392 | dbg("%s: signal x%x", __FUNCTION__, signals); |
| 393 | |
| 394 | old_dcd_state = portdata->dcd_state; |
| 395 | portdata->cts_state = 1; |
| 396 | portdata->dcd_state = ((signals & 0x01) ? 1 : 0); |
| 397 | portdata->dsr_state = ((signals & 0x02) ? 1 : 0); |
| 398 | portdata->ri_state = ((signals & 0x08) ? 1 : 0); |
| 399 | |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 400 | if (port->tty && !C_CLOCAL(port->tty) && |
| 401 | old_dcd_state && !portdata->dcd_state) |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 402 | tty_hangup(port->tty); |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 403 | } else { |
| 404 | dbg("%s: type %x req %x", __FUNCTION__, |
| 405 | req_pkt->bRequestType,req_pkt->bRequest); |
| 406 | } |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 407 | } else |
| 408 | dbg("%s: error %d", __FUNCTION__, urb->status); |
| 409 | |
| 410 | /* Resubmit urb so we continue receiving IRQ data */ |
| 411 | if (urb->status != -ESHUTDOWN) { |
| 412 | urb->dev = serial->dev; |
| 413 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 414 | if (err) |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 415 | dbg("%s: resubmit intr urb failed. (%d)", |
| 416 | __FUNCTION__, err); |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 420 | static int option_write_room(struct usb_serial_port *port) |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 421 | { |
| 422 | struct option_port_private *portdata; |
| 423 | int i; |
| 424 | int data_len = 0; |
| 425 | struct urb *this_urb; |
| 426 | |
| 427 | portdata = usb_get_serial_port_data(port); |
| 428 | |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 429 | for (i=0; i < N_OUT_URB; i++) { |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 430 | this_urb = portdata->out_urbs[i]; |
| 431 | if (this_urb && this_urb->status != -EINPROGRESS) |
| 432 | data_len += OUT_BUFLEN; |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 433 | } |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 434 | |
| 435 | dbg("%s: %d", __FUNCTION__, data_len); |
| 436 | return data_len; |
| 437 | } |
| 438 | |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 439 | static int option_chars_in_buffer(struct usb_serial_port *port) |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 440 | { |
| 441 | struct option_port_private *portdata; |
| 442 | int i; |
| 443 | int data_len = 0; |
| 444 | struct urb *this_urb; |
| 445 | |
| 446 | portdata = usb_get_serial_port_data(port); |
| 447 | |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 448 | for (i=0; i < N_OUT_URB; i++) { |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 449 | this_urb = portdata->out_urbs[i]; |
| 450 | if (this_urb && this_urb->status == -EINPROGRESS) |
| 451 | data_len += this_urb->transfer_buffer_length; |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 452 | } |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 453 | dbg("%s: %d", __FUNCTION__, data_len); |
| 454 | return data_len; |
| 455 | } |
| 456 | |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 457 | static int option_open(struct usb_serial_port *port, struct file *filp) |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 458 | { |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 459 | struct option_port_private *portdata; |
| 460 | struct usb_serial *serial = port->serial; |
| 461 | int i, err; |
| 462 | struct urb *urb; |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 463 | |
| 464 | portdata = usb_get_serial_port_data(port); |
| 465 | |
| 466 | dbg("%s", __FUNCTION__); |
| 467 | |
| 468 | /* Set some sane defaults */ |
| 469 | portdata->rts_state = 1; |
| 470 | portdata->dtr_state = 1; |
| 471 | |
| 472 | /* Reset low level data toggle and start reading from endpoints */ |
| 473 | for (i = 0; i < N_IN_URB; i++) { |
| 474 | urb = portdata->in_urbs[i]; |
| 475 | if (! urb) |
| 476 | continue; |
| 477 | if (urb->dev != serial->dev) { |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 478 | dbg("%s: dev %p != %p", __FUNCTION__, |
| 479 | urb->dev, serial->dev); |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 480 | continue; |
| 481 | } |
| 482 | |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 483 | /* |
| 484 | * make sure endpoint data toggle is synchronized with the |
| 485 | * device |
| 486 | */ |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 487 | usb_clear_halt(urb->dev, urb->pipe); |
| 488 | |
| 489 | err = usb_submit_urb(urb, GFP_KERNEL); |
| 490 | if (err) { |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 491 | dbg("%s: submit urb %d failed (%d) %d", |
| 492 | __FUNCTION__, i, err, |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 493 | urb->transfer_buffer_length); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | /* Reset low level data toggle on out endpoints */ |
| 498 | for (i = 0; i < N_OUT_URB; i++) { |
| 499 | urb = portdata->out_urbs[i]; |
| 500 | if (! urb) |
| 501 | continue; |
| 502 | urb->dev = serial->dev; |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 503 | /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), |
| 504 | usb_pipeout(urb->pipe), 0); */ |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | port->tty->low_latency = 1; |
| 508 | |
| 509 | option_send_setup(port); |
| 510 | |
| 511 | return (0); |
| 512 | } |
| 513 | |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 514 | static inline void stop_urb(struct urb *urb) |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 515 | { |
| 516 | if (urb && urb->status == -EINPROGRESS) { |
| 517 | urb->transfer_flags &= ~URB_ASYNC_UNLINK; |
| 518 | usb_kill_urb(urb); |
| 519 | } |
| 520 | } |
| 521 | |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 522 | static void option_close(struct usb_serial_port *port, struct file *filp) |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 523 | { |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 524 | int i; |
| 525 | struct usb_serial *serial = port->serial; |
| 526 | struct option_port_private *portdata; |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 527 | |
| 528 | dbg("%s", __FUNCTION__); |
| 529 | portdata = usb_get_serial_port_data(port); |
| 530 | |
| 531 | portdata->rts_state = 0; |
| 532 | portdata->dtr_state = 0; |
| 533 | |
| 534 | if (serial->dev) { |
| 535 | option_send_setup(port); |
| 536 | |
| 537 | /* Stop reading/writing urbs */ |
| 538 | for (i = 0; i < N_IN_URB; i++) |
| 539 | stop_urb(portdata->in_urbs[i]); |
| 540 | for (i = 0; i < N_OUT_URB; i++) |
| 541 | stop_urb(portdata->out_urbs[i]); |
| 542 | } |
| 543 | port->tty = NULL; |
| 544 | } |
| 545 | |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 546 | /* Helper functions used by option_setup_urbs */ |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 547 | static struct urb *option_setup_urb(struct usb_serial *serial, int endpoint, |
| 548 | int dir, void *ctx, char *buf, int len, |
| 549 | void (*callback)(struct urb *, struct pt_regs *regs)) |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 550 | { |
| 551 | struct urb *urb; |
| 552 | |
| 553 | if (endpoint == -1) |
| 554 | return NULL; /* endpoint not needed */ |
| 555 | |
| 556 | urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */ |
| 557 | if (urb == NULL) { |
| 558 | dbg("%s: alloc for endpoint %d failed.", __FUNCTION__, endpoint); |
| 559 | return NULL; |
| 560 | } |
| 561 | |
| 562 | /* Fill URB using supplied data. */ |
| 563 | usb_fill_bulk_urb(urb, serial->dev, |
| 564 | usb_sndbulkpipe(serial->dev, endpoint) | dir, |
| 565 | buf, len, callback, ctx); |
| 566 | |
| 567 | return urb; |
| 568 | } |
| 569 | |
| 570 | /* Setup urbs */ |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 571 | static void option_setup_urbs(struct usb_serial *serial) |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 572 | { |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 573 | int j; |
| 574 | struct usb_serial_port *port; |
| 575 | struct option_port_private *portdata; |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 576 | |
| 577 | dbg("%s", __FUNCTION__); |
| 578 | |
| 579 | port = serial->port[0]; |
| 580 | portdata = usb_get_serial_port_data(port); |
| 581 | |
| 582 | /* Do indat endpoints first */ |
| 583 | for (j = 0; j <= N_IN_URB; ++j) { |
| 584 | portdata->in_urbs[j] = option_setup_urb (serial, |
| 585 | port->bulk_in_endpointAddress, USB_DIR_IN, port, |
| 586 | portdata->in_buffer[j], IN_BUFLEN, option_indat_callback); |
| 587 | } |
| 588 | |
| 589 | /* outdat endpoints */ |
| 590 | for (j = 0; j <= N_OUT_URB; ++j) { |
| 591 | portdata->out_urbs[j] = option_setup_urb (serial, |
| 592 | port->bulk_out_endpointAddress, USB_DIR_OUT, port, |
| 593 | portdata->out_buffer[j], OUT_BUFLEN, option_outdat_callback); |
| 594 | } |
| 595 | } |
| 596 | |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 597 | static int option_send_setup(struct usb_serial_port *port) |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 598 | { |
| 599 | struct usb_serial *serial = port->serial; |
| 600 | struct option_port_private *portdata; |
| 601 | |
| 602 | dbg("%s", __FUNCTION__); |
| 603 | |
| 604 | portdata = usb_get_serial_port_data(port); |
| 605 | |
| 606 | if (port->tty) { |
| 607 | int val = 0; |
| 608 | if (portdata->dtr_state) |
| 609 | val |= 0x01; |
| 610 | if (portdata->rts_state) |
| 611 | val |= 0x02; |
| 612 | |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 613 | return usb_control_msg(serial->dev, |
| 614 | usb_rcvctrlpipe(serial->dev, 0), |
| 615 | 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT); |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | return 0; |
| 619 | } |
| 620 | |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 621 | static int option_startup(struct usb_serial *serial) |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 622 | { |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 623 | int i, err; |
| 624 | struct usb_serial_port *port; |
| 625 | struct option_port_private *portdata; |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 626 | |
| 627 | dbg("%s", __FUNCTION__); |
| 628 | |
| 629 | /* Now setup per port private data */ |
| 630 | for (i = 0; i < serial->num_ports; i++) { |
| 631 | port = serial->port[i]; |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 632 | portdata = kmalloc(sizeof(*portdata), GFP_KERNEL); |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 633 | if (!portdata) { |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 634 | dbg("%s: kmalloc for option_port_private (%d) failed!.", |
| 635 | __FUNCTION__, i); |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 636 | return (1); |
| 637 | } |
| 638 | memset(portdata, 0, sizeof(struct option_port_private)); |
| 639 | |
| 640 | usb_set_serial_port_data(port, portdata); |
| 641 | |
| 642 | if (! port->interrupt_in_urb) |
| 643 | continue; |
| 644 | err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); |
| 645 | if (err) |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 646 | dbg("%s: submit irq_in urb failed %d", |
| 647 | __FUNCTION__, err); |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | option_setup_urbs(serial); |
| 651 | |
| 652 | return (0); |
| 653 | } |
| 654 | |
Andrew Morton | 7bb75ae | 2005-07-27 01:08:30 -0700 | [diff] [blame^] | 655 | static void option_shutdown(struct usb_serial *serial) |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 656 | { |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 657 | int i, j; |
| 658 | struct usb_serial_port *port; |
| 659 | struct option_port_private *portdata; |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 660 | |
| 661 | dbg("%s", __FUNCTION__); |
| 662 | |
| 663 | /* Stop reading/writing urbs */ |
| 664 | for (i = 0; i < serial->num_ports; ++i) { |
| 665 | port = serial->port[i]; |
| 666 | portdata = usb_get_serial_port_data(port); |
| 667 | for (j = 0; j < N_IN_URB; j++) |
| 668 | stop_urb(portdata->in_urbs[j]); |
| 669 | for (j = 0; j < N_OUT_URB; j++) |
| 670 | stop_urb(portdata->out_urbs[j]); |
| 671 | } |
| 672 | |
| 673 | /* Now free them */ |
| 674 | for (i = 0; i < serial->num_ports; ++i) { |
| 675 | port = serial->port[i]; |
| 676 | portdata = usb_get_serial_port_data(port); |
| 677 | |
| 678 | for (j = 0; j < N_IN_URB; j++) { |
| 679 | if (portdata->in_urbs[j]) { |
| 680 | usb_free_urb(portdata->in_urbs[j]); |
| 681 | portdata->in_urbs[j] = NULL; |
| 682 | } |
| 683 | } |
| 684 | for (j = 0; j < N_OUT_URB; j++) { |
| 685 | if (portdata->out_urbs[j]) { |
| 686 | usb_free_urb(portdata->out_urbs[j]); |
| 687 | portdata->out_urbs[j] = NULL; |
| 688 | } |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | /* Now free per port private data */ |
| 693 | for (i = 0; i < serial->num_ports; i++) { |
| 694 | port = serial->port[i]; |
| 695 | kfree(usb_get_serial_port_data(port)); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 700 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 701 | MODULE_VERSION(DRIVER_VERSION); |
| 702 | MODULE_LICENSE("GPL"); |
| 703 | |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 704 | #ifdef CONFIG_USB_DEBUG |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 705 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
| 706 | MODULE_PARM_DESC(debug, "Debug messages"); |
Matthias Urlichs | ba460e4 | 2005-07-14 00:33:47 -0700 | [diff] [blame] | 707 | #endif |
Matthias Urlichs | 58cfe91 | 2005-05-23 17:00:48 -0700 | [diff] [blame] | 708 | |