Kevin Lloyd | 69de51f | 2006-06-30 11:17:55 -0700 | [diff] [blame] | 1 | /* |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 2 | USB Driver for Sierra Wireless |
| 3 | |
| 4 | Copyright (C) 2006 Kevin Lloyd <linux@sierrawireless.com> |
| 5 | |
| 6 | IMPORTANT DISCLAIMER: This driver is not commercially supported by |
| 7 | Sierra Wireless. Use at your own risk. |
| 8 | |
| 9 | This driver is free software; you can redistribute it and/or modify |
| 10 | it under the terms of Version 2 of the GNU General Public License as |
| 11 | published by the Free Software Foundation. |
| 12 | |
| 13 | Portions based on the option driver by Matthias Urlichs <smurf@smurf.noris.de> |
| 14 | Whom based his on the Keyspan driver by Hugh Blemings <hugh@blemings.org> |
| 15 | |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 16 | */ |
| 17 | |
Kevin Lloyd | e43062d | 2007-01-17 16:04:18 -0800 | [diff] [blame] | 18 | #define DRIVER_VERSION "v.1.0.6" |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 19 | #define DRIVER_AUTHOR "Kevin Lloyd <linux@sierrawireless.com>" |
| 20 | #define DRIVER_DESC "USB Driver for Sierra Wireless USB modems" |
Kevin Lloyd | 69de51f | 2006-06-30 11:17:55 -0700 | [diff] [blame] | 21 | |
| 22 | #include <linux/kernel.h> |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 23 | #include <linux/jiffies.h> |
| 24 | #include <linux/errno.h> |
Kevin Lloyd | 69de51f | 2006-06-30 11:17:55 -0700 | [diff] [blame] | 25 | #include <linux/tty.h> |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 26 | #include <linux/tty_flip.h> |
Kevin Lloyd | 69de51f | 2006-06-30 11:17:55 -0700 | [diff] [blame] | 27 | #include <linux/module.h> |
| 28 | #include <linux/usb.h> |
Greg Kroah-Hartman | a969888 | 2006-07-11 21:22:58 -0700 | [diff] [blame] | 29 | #include <linux/usb/serial.h> |
Kevin Lloyd | 69de51f | 2006-06-30 11:17:55 -0700 | [diff] [blame] | 30 | |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 31 | |
Kevin Lloyd | 69de51f | 2006-06-30 11:17:55 -0700 | [diff] [blame] | 32 | static struct usb_device_id id_table [] = { |
Kevin Lloyd | 69de51f | 2006-06-30 11:17:55 -0700 | [diff] [blame] | 33 | { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */ |
Kevin Lloyd | e43062d | 2007-01-17 16:04:18 -0800 | [diff] [blame] | 34 | { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */ |
Greg Kroah-Hartman | 90ac3c8 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 35 | { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */ |
Kevin Lloyd | e43062d | 2007-01-17 16:04:18 -0800 | [diff] [blame] | 36 | { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */ |
| 37 | { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */ |
Danny Budik | 5b7da8f | 2007-05-04 16:09:37 -0400 | [diff] [blame] | 38 | { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless AirCard 595U */ |
Kevin Lloyd | e43062d | 2007-01-17 16:04:18 -0800 | [diff] [blame] | 39 | { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */ |
Kevin Lloyd | 69de51f | 2006-06-30 11:17:55 -0700 | [diff] [blame] | 40 | { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */ |
Kevin Lloyd | e43062d | 2007-01-17 16:04:18 -0800 | [diff] [blame] | 41 | { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */ |
Kevin Lloyd | 69de51f | 2006-06-30 11:17:55 -0700 | [diff] [blame] | 42 | { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */ |
| 43 | { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 */ |
| 44 | { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */ |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 45 | |
| 46 | { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */ |
| 47 | { USB_DEVICE(0x0F3D, 0x0112) }, /* AirPrime/Sierra PC 5220 */ |
Kevin Lloyd | 69de51f | 2006-06-30 11:17:55 -0700 | [diff] [blame] | 48 | { } |
| 49 | }; |
Greg Kroah-Hartman | 964ee1d | 2006-10-17 10:17:58 -0700 | [diff] [blame] | 50 | MODULE_DEVICE_TABLE(usb, id_table); |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 51 | |
| 52 | static struct usb_device_id id_table_1port [] = { |
| 53 | { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */ |
| 54 | { USB_DEVICE(0x0F3D, 0x0112) }, /* AirPrime/Sierra PC 5220 */ |
| 55 | { } |
| 56 | }; |
| 57 | |
| 58 | static struct usb_device_id id_table_3port [] = { |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 59 | { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */ |
Kevin Lloyd | e43062d | 2007-01-17 16:04:18 -0800 | [diff] [blame] | 60 | { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */ |
Greg Kroah-Hartman | 90ac3c8 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 61 | { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */ |
Kevin Lloyd | e43062d | 2007-01-17 16:04:18 -0800 | [diff] [blame] | 62 | { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */ |
| 63 | { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */ |
Danny Budik | 5b7da8f | 2007-05-04 16:09:37 -0400 | [diff] [blame] | 64 | { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless AirCard 595U */ |
Kevin Lloyd | e43062d | 2007-01-17 16:04:18 -0800 | [diff] [blame] | 65 | { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */ |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 66 | { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */ |
Kevin Lloyd | e43062d | 2007-01-17 16:04:18 -0800 | [diff] [blame] | 67 | { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */ |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 68 | { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */ |
| 69 | { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 */ |
| 70 | { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */ |
| 71 | { } |
| 72 | }; |
| 73 | |
Kevin Lloyd | 69de51f | 2006-06-30 11:17:55 -0700 | [diff] [blame] | 74 | static struct usb_driver sierra_driver = { |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 75 | .name = "sierra", |
| 76 | .probe = usb_serial_probe, |
| 77 | .disconnect = usb_serial_disconnect, |
| 78 | .id_table = id_table, |
Greg Kroah-Hartman | 964ee1d | 2006-10-17 10:17:58 -0700 | [diff] [blame] | 79 | .no_dynamic_id = 1, |
Kevin Lloyd | 69de51f | 2006-06-30 11:17:55 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 82 | |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 83 | static int debug; |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 84 | |
| 85 | /* per port private data */ |
Greg Kroah-Hartman | 964ee1d | 2006-10-17 10:17:58 -0700 | [diff] [blame] | 86 | #define N_IN_URB 4 |
Kevin Lloyd | e43062d | 2007-01-17 16:04:18 -0800 | [diff] [blame] | 87 | #define N_OUT_URB 4 |
Greg Kroah-Hartman | 964ee1d | 2006-10-17 10:17:58 -0700 | [diff] [blame] | 88 | #define IN_BUFLEN 4096 |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 89 | |
| 90 | struct sierra_port_private { |
Greg Kroah-Hartman | 17c2327 | 2007-06-20 14:22:23 +0900 | [diff] [blame^] | 91 | spinlock_t lock; /* lock the structure */ |
| 92 | int outstanding_urbs; /* number of out urbs in flight */ |
| 93 | |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 94 | /* Input endpoints and buffer for this port */ |
| 95 | struct urb *in_urbs[N_IN_URB]; |
| 96 | char in_buffer[N_IN_URB][IN_BUFLEN]; |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 97 | |
| 98 | /* Settings for the port */ |
| 99 | int rts_state; /* Handshaking pins (outputs) */ |
| 100 | int dtr_state; |
| 101 | int cts_state; /* Handshaking pins (inputs) */ |
| 102 | int dsr_state; |
| 103 | int dcd_state; |
| 104 | int ri_state; |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
Greg Kroah-Hartman | 964ee1d | 2006-10-17 10:17:58 -0700 | [diff] [blame] | 107 | static int sierra_send_setup(struct usb_serial_port *port) |
Kevin Lloyd | 69de51f | 2006-06-30 11:17:55 -0700 | [diff] [blame] | 108 | { |
Greg Kroah-Hartman | 964ee1d | 2006-10-17 10:17:58 -0700 | [diff] [blame] | 109 | struct usb_serial *serial = port->serial; |
| 110 | struct sierra_port_private *portdata; |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 111 | |
Greg Kroah-Hartman | 964ee1d | 2006-10-17 10:17:58 -0700 | [diff] [blame] | 112 | dbg("%s", __FUNCTION__); |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 113 | |
Greg Kroah-Hartman | 964ee1d | 2006-10-17 10:17:58 -0700 | [diff] [blame] | 114 | portdata = usb_get_serial_port_data(port); |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 115 | |
Greg Kroah-Hartman | 964ee1d | 2006-10-17 10:17:58 -0700 | [diff] [blame] | 116 | if (port->tty) { |
| 117 | int val = 0; |
| 118 | if (portdata->dtr_state) |
| 119 | val |= 0x01; |
| 120 | if (portdata->rts_state) |
| 121 | val |= 0x02; |
| 122 | |
| 123 | return usb_control_msg(serial->dev, |
| 124 | usb_rcvctrlpipe(serial->dev, 0), |
| 125 | 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT); |
| 126 | } |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 127 | |
| 128 | return 0; |
Kevin Lloyd | 69de51f | 2006-06-30 11:17:55 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 131 | static void sierra_rx_throttle(struct usb_serial_port *port) |
| 132 | { |
| 133 | dbg("%s", __FUNCTION__); |
| 134 | } |
| 135 | |
| 136 | static void sierra_rx_unthrottle(struct usb_serial_port *port) |
| 137 | { |
| 138 | dbg("%s", __FUNCTION__); |
| 139 | } |
| 140 | |
| 141 | static void sierra_break_ctl(struct usb_serial_port *port, int break_state) |
| 142 | { |
| 143 | /* Unfortunately, I don't know how to send a break */ |
| 144 | dbg("%s", __FUNCTION__); |
| 145 | } |
| 146 | |
| 147 | static void sierra_set_termios(struct usb_serial_port *port, |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 148 | struct ktermios *old_termios) |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 149 | { |
| 150 | dbg("%s", __FUNCTION__); |
| 151 | |
| 152 | sierra_send_setup(port); |
| 153 | } |
| 154 | |
| 155 | static int sierra_tiocmget(struct usb_serial_port *port, struct file *file) |
| 156 | { |
| 157 | unsigned int value; |
| 158 | struct sierra_port_private *portdata; |
| 159 | |
| 160 | portdata = usb_get_serial_port_data(port); |
| 161 | |
| 162 | value = ((portdata->rts_state) ? TIOCM_RTS : 0) | |
| 163 | ((portdata->dtr_state) ? TIOCM_DTR : 0) | |
| 164 | ((portdata->cts_state) ? TIOCM_CTS : 0) | |
| 165 | ((portdata->dsr_state) ? TIOCM_DSR : 0) | |
| 166 | ((portdata->dcd_state) ? TIOCM_CAR : 0) | |
| 167 | ((portdata->ri_state) ? TIOCM_RNG : 0); |
| 168 | |
| 169 | return value; |
| 170 | } |
| 171 | |
| 172 | static int sierra_tiocmset(struct usb_serial_port *port, struct file *file, |
| 173 | unsigned int set, unsigned int clear) |
| 174 | { |
| 175 | struct sierra_port_private *portdata; |
| 176 | |
| 177 | portdata = usb_get_serial_port_data(port); |
| 178 | |
| 179 | if (set & TIOCM_RTS) |
| 180 | portdata->rts_state = 1; |
| 181 | if (set & TIOCM_DTR) |
| 182 | portdata->dtr_state = 1; |
| 183 | |
| 184 | if (clear & TIOCM_RTS) |
| 185 | portdata->rts_state = 0; |
| 186 | if (clear & TIOCM_DTR) |
| 187 | portdata->dtr_state = 0; |
| 188 | return sierra_send_setup(port); |
| 189 | } |
| 190 | |
| 191 | static int sierra_ioctl(struct usb_serial_port *port, struct file *file, |
| 192 | unsigned int cmd, unsigned long arg) |
| 193 | { |
| 194 | return -ENOIOCTLCMD; |
| 195 | } |
| 196 | |
Greg Kroah-Hartman | 17c2327 | 2007-06-20 14:22:23 +0900 | [diff] [blame^] | 197 | static void sierra_outdat_callback(struct urb *urb) |
| 198 | { |
| 199 | struct usb_serial_port *port = urb->context; |
| 200 | struct sierra_port_private *portdata = usb_get_serial_port_data(port); |
| 201 | int status = urb->status; |
| 202 | unsigned long flags; |
| 203 | |
| 204 | dbg("%s - port %d", __FUNCTION__, port->number); |
| 205 | |
| 206 | /* free up the transfer buffer, as usb_free_urb() does not do this */ |
| 207 | kfree(urb->transfer_buffer); |
| 208 | |
| 209 | if (status) |
| 210 | dbg("%s - nonzero write bulk status received: %d", |
| 211 | __FUNCTION__, status); |
| 212 | |
| 213 | spin_lock_irqsave(&portdata->lock, flags); |
| 214 | --portdata->outstanding_urbs; |
| 215 | spin_unlock_irqrestore(&portdata->lock, flags); |
| 216 | |
| 217 | usb_serial_port_softint(port); |
| 218 | } |
| 219 | |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 220 | /* Write */ |
| 221 | static int sierra_write(struct usb_serial_port *port, |
| 222 | const unsigned char *buf, int count) |
| 223 | { |
Greg Kroah-Hartman | 17c2327 | 2007-06-20 14:22:23 +0900 | [diff] [blame^] | 224 | struct sierra_port_private *portdata = usb_get_serial_port_data(port); |
| 225 | struct usb_serial *serial = port->serial; |
| 226 | unsigned long flags; |
| 227 | unsigned char *buffer; |
| 228 | struct urb *urb; |
| 229 | int status; |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 230 | |
| 231 | portdata = usb_get_serial_port_data(port); |
| 232 | |
| 233 | dbg("%s: write (%d chars)", __FUNCTION__, count); |
| 234 | |
Greg Kroah-Hartman | 17c2327 | 2007-06-20 14:22:23 +0900 | [diff] [blame^] | 235 | spin_lock_irqsave(&portdata->lock, flags); |
| 236 | if (portdata->outstanding_urbs > N_OUT_URB) { |
| 237 | spin_unlock_irqrestore(&portdata->lock, flags); |
| 238 | dbg("%s - write limit hit\n", __FUNCTION__); |
| 239 | return 0; |
| 240 | } |
| 241 | portdata->outstanding_urbs++; |
| 242 | spin_unlock_irqrestore(&portdata->lock, flags); |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 243 | |
Greg Kroah-Hartman | 17c2327 | 2007-06-20 14:22:23 +0900 | [diff] [blame^] | 244 | buffer = kmalloc(count, GFP_ATOMIC); |
| 245 | if (!buffer) { |
| 246 | dev_err(&port->dev, "out of memory\n"); |
| 247 | count = -ENOMEM; |
| 248 | goto error_no_buffer; |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Greg Kroah-Hartman | 17c2327 | 2007-06-20 14:22:23 +0900 | [diff] [blame^] | 251 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 252 | if (!urb) { |
| 253 | dev_err(&port->dev, "no more free urbs\n"); |
| 254 | count = -ENOMEM; |
| 255 | goto error_no_urb; |
| 256 | } |
| 257 | |
| 258 | memcpy(buffer, buf, count); |
| 259 | |
| 260 | usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer); |
| 261 | |
| 262 | usb_fill_bulk_urb(urb, serial->dev, |
| 263 | usb_sndbulkpipe(serial->dev, |
| 264 | port->bulk_out_endpointAddress), |
| 265 | buffer, count, sierra_outdat_callback, port); |
| 266 | |
| 267 | /* send it down the pipe */ |
| 268 | status = usb_submit_urb(urb, GFP_ATOMIC); |
| 269 | if (status) { |
| 270 | dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed " |
| 271 | "with status = %d\n", __FUNCTION__, status); |
| 272 | count = status; |
| 273 | goto error; |
| 274 | } |
| 275 | |
| 276 | /* we are done with this urb, so let the host driver |
| 277 | * really free it when it is finished with it */ |
| 278 | usb_free_urb(urb); |
| 279 | |
| 280 | return count; |
| 281 | error: |
| 282 | usb_free_urb(urb); |
| 283 | error_no_urb: |
| 284 | kfree(buffer); |
| 285 | error_no_buffer: |
| 286 | spin_lock_irqsave(&portdata->lock, flags); |
| 287 | --portdata->outstanding_urbs; |
| 288 | spin_unlock_irqrestore(&portdata->lock, flags); |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 289 | return count; |
| 290 | } |
| 291 | |
| 292 | static void sierra_indat_callback(struct urb *urb) |
| 293 | { |
| 294 | int err; |
| 295 | int endpoint; |
| 296 | struct usb_serial_port *port; |
| 297 | struct tty_struct *tty; |
| 298 | unsigned char *data = urb->transfer_buffer; |
Greg Kroah-Hartman | 17dd221 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 299 | int status = urb->status; |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 300 | |
| 301 | dbg("%s: %p", __FUNCTION__, urb); |
| 302 | |
| 303 | endpoint = usb_pipeendpoint(urb->pipe); |
| 304 | port = (struct usb_serial_port *) urb->context; |
| 305 | |
Greg Kroah-Hartman | 17dd221 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 306 | if (status) { |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 307 | dbg("%s: nonzero status: %d on endpoint %02x.", |
Greg Kroah-Hartman | 17dd221 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 308 | __FUNCTION__, status, endpoint); |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 309 | } else { |
| 310 | tty = port->tty; |
| 311 | if (urb->actual_length) { |
| 312 | tty_buffer_request_room(tty, urb->actual_length); |
| 313 | tty_insert_flip_string(tty, data, urb->actual_length); |
| 314 | tty_flip_buffer_push(tty); |
| 315 | } else { |
| 316 | dbg("%s: empty read urb received", __FUNCTION__); |
| 317 | } |
| 318 | |
| 319 | /* Resubmit urb so we continue receiving */ |
Greg Kroah-Hartman | 17dd221 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 320 | if (port->open_count && status != -ESHUTDOWN) { |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 321 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 322 | if (err) |
Greg Kroah-Hartman | 17c2327 | 2007-06-20 14:22:23 +0900 | [diff] [blame^] | 323 | dev_err(&port->dev, "resubmit read urb failed." |
| 324 | "(%d)", err); |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | return; |
| 328 | } |
| 329 | |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 330 | static void sierra_instat_callback(struct urb *urb) |
| 331 | { |
| 332 | int err; |
Greg Kroah-Hartman | 17dd221 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 333 | int status = urb->status; |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 334 | struct usb_serial_port *port = (struct usb_serial_port *) urb->context; |
| 335 | struct sierra_port_private *portdata = usb_get_serial_port_data(port); |
| 336 | struct usb_serial *serial = port->serial; |
| 337 | |
| 338 | dbg("%s", __FUNCTION__); |
| 339 | dbg("%s: urb %p port %p has data %p", __FUNCTION__,urb,port,portdata); |
| 340 | |
Greg Kroah-Hartman | 17dd221 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 341 | if (status == 0) { |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 342 | struct usb_ctrlrequest *req_pkt = |
| 343 | (struct usb_ctrlrequest *)urb->transfer_buffer; |
| 344 | |
| 345 | if (!req_pkt) { |
| 346 | dbg("%s: NULL req_pkt\n", __FUNCTION__); |
| 347 | return; |
| 348 | } |
| 349 | if ((req_pkt->bRequestType == 0xA1) && |
| 350 | (req_pkt->bRequest == 0x20)) { |
| 351 | int old_dcd_state; |
| 352 | unsigned char signals = *((unsigned char *) |
| 353 | urb->transfer_buffer + |
| 354 | sizeof(struct usb_ctrlrequest)); |
| 355 | |
| 356 | dbg("%s: signal x%x", __FUNCTION__, signals); |
| 357 | |
| 358 | old_dcd_state = portdata->dcd_state; |
| 359 | portdata->cts_state = 1; |
| 360 | portdata->dcd_state = ((signals & 0x01) ? 1 : 0); |
| 361 | portdata->dsr_state = ((signals & 0x02) ? 1 : 0); |
| 362 | portdata->ri_state = ((signals & 0x08) ? 1 : 0); |
| 363 | |
| 364 | if (port->tty && !C_CLOCAL(port->tty) && |
| 365 | old_dcd_state && !portdata->dcd_state) |
| 366 | tty_hangup(port->tty); |
| 367 | } else { |
| 368 | dbg("%s: type %x req %x", __FUNCTION__, |
| 369 | req_pkt->bRequestType,req_pkt->bRequest); |
| 370 | } |
| 371 | } else |
Greg Kroah-Hartman | 17dd221 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 372 | dbg("%s: error %d", __FUNCTION__, status); |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 373 | |
| 374 | /* Resubmit urb so we continue receiving IRQ data */ |
Greg Kroah-Hartman | 17dd221 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 375 | if (status != -ESHUTDOWN) { |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 376 | urb->dev = serial->dev; |
| 377 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 378 | if (err) |
| 379 | dbg("%s: resubmit intr urb failed. (%d)", |
| 380 | __FUNCTION__, err); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | static int sierra_write_room(struct usb_serial_port *port) |
| 385 | { |
Greg Kroah-Hartman | 17c2327 | 2007-06-20 14:22:23 +0900 | [diff] [blame^] | 386 | struct sierra_port_private *portdata = usb_get_serial_port_data(port); |
| 387 | unsigned long flags; |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 388 | |
Greg Kroah-Hartman | 17c2327 | 2007-06-20 14:22:23 +0900 | [diff] [blame^] | 389 | dbg("%s - port %d", __FUNCTION__, port->number); |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 390 | |
Greg Kroah-Hartman | 17c2327 | 2007-06-20 14:22:23 +0900 | [diff] [blame^] | 391 | /* try to give a good number back based on if we have any free urbs at |
| 392 | * this point in time */ |
| 393 | spin_lock_irqsave(&portdata->lock, flags); |
| 394 | if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) { |
| 395 | spin_unlock_irqrestore(&portdata->lock, flags); |
| 396 | dbg("%s - write limit hit\n", __FUNCTION__); |
| 397 | return 0; |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 398 | } |
Greg Kroah-Hartman | 17c2327 | 2007-06-20 14:22:23 +0900 | [diff] [blame^] | 399 | spin_unlock_irqrestore(&portdata->lock, flags); |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 400 | |
Greg Kroah-Hartman | 17c2327 | 2007-06-20 14:22:23 +0900 | [diff] [blame^] | 401 | return 2048; |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | static int sierra_chars_in_buffer(struct usb_serial_port *port) |
| 405 | { |
Greg Kroah-Hartman | 17c2327 | 2007-06-20 14:22:23 +0900 | [diff] [blame^] | 406 | dbg("%s - port %d", __FUNCTION__, port->number); |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 407 | |
Greg Kroah-Hartman | 17c2327 | 2007-06-20 14:22:23 +0900 | [diff] [blame^] | 408 | /* |
| 409 | * We can't really account for how much data we |
| 410 | * have sent out, but hasn't made it through to the |
| 411 | * device as we can't see the backend here, so just |
| 412 | * tell the tty layer that everything is flushed. |
| 413 | */ |
| 414 | return 0; |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | static int sierra_open(struct usb_serial_port *port, struct file *filp) |
| 418 | { |
| 419 | struct sierra_port_private *portdata; |
| 420 | struct usb_serial *serial = port->serial; |
| 421 | int i, err; |
| 422 | struct urb *urb; |
Kevin Lloyd | e43062d | 2007-01-17 16:04:18 -0800 | [diff] [blame] | 423 | int result; |
| 424 | __u16 set_mode_dzero = 0x0000; |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 425 | |
| 426 | portdata = usb_get_serial_port_data(port); |
| 427 | |
| 428 | dbg("%s", __FUNCTION__); |
| 429 | |
| 430 | /* Set some sane defaults */ |
| 431 | portdata->rts_state = 1; |
| 432 | portdata->dtr_state = 1; |
| 433 | |
| 434 | /* Reset low level data toggle and start reading from endpoints */ |
| 435 | for (i = 0; i < N_IN_URB; i++) { |
| 436 | urb = portdata->in_urbs[i]; |
| 437 | if (! urb) |
| 438 | continue; |
| 439 | if (urb->dev != serial->dev) { |
| 440 | dbg("%s: dev %p != %p", __FUNCTION__, |
| 441 | urb->dev, serial->dev); |
| 442 | continue; |
| 443 | } |
| 444 | |
| 445 | /* |
| 446 | * make sure endpoint data toggle is synchronized with the |
| 447 | * device |
| 448 | */ |
| 449 | usb_clear_halt(urb->dev, urb->pipe); |
| 450 | |
| 451 | err = usb_submit_urb(urb, GFP_KERNEL); |
| 452 | if (err) { |
| 453 | dbg("%s: submit urb %d failed (%d) %d", |
| 454 | __FUNCTION__, i, err, |
| 455 | urb->transfer_buffer_length); |
| 456 | } |
| 457 | } |
| 458 | |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 459 | port->tty->low_latency = 1; |
| 460 | |
Kevin Lloyd | e43062d | 2007-01-17 16:04:18 -0800 | [diff] [blame] | 461 | /* set mode to D0 */ |
| 462 | result = usb_control_msg(serial->dev, |
| 463 | usb_rcvctrlpipe(serial->dev, 0), |
| 464 | 0x00, 0x40, set_mode_dzero, 0, NULL, |
| 465 | 0, USB_CTRL_SET_TIMEOUT); |
| 466 | |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 467 | sierra_send_setup(port); |
| 468 | |
| 469 | return (0); |
| 470 | } |
| 471 | |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 472 | static void sierra_close(struct usb_serial_port *port, struct file *filp) |
| 473 | { |
| 474 | int i; |
| 475 | struct usb_serial *serial = port->serial; |
| 476 | struct sierra_port_private *portdata; |
| 477 | |
| 478 | dbg("%s", __FUNCTION__); |
| 479 | portdata = usb_get_serial_port_data(port); |
| 480 | |
| 481 | portdata->rts_state = 0; |
| 482 | portdata->dtr_state = 0; |
| 483 | |
| 484 | if (serial->dev) { |
| 485 | sierra_send_setup(port); |
| 486 | |
| 487 | /* Stop reading/writing urbs */ |
| 488 | for (i = 0; i < N_IN_URB; i++) |
Oliver Neukum | ebcf3ed | 2007-03-20 13:54:05 +0100 | [diff] [blame] | 489 | usb_unlink_urb(portdata->in_urbs[i]); |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 490 | } |
| 491 | port->tty = NULL; |
| 492 | } |
| 493 | |
| 494 | /* Helper functions used by sierra_setup_urbs */ |
| 495 | static struct urb *sierra_setup_urb(struct usb_serial *serial, int endpoint, |
Greg Kroah-Hartman | 964ee1d | 2006-10-17 10:17:58 -0700 | [diff] [blame] | 496 | int dir, void *ctx, char *buf, int len, |
| 497 | usb_complete_t callback) |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 498 | { |
| 499 | struct urb *urb; |
| 500 | |
| 501 | if (endpoint == -1) |
| 502 | return NULL; /* endpoint not needed */ |
| 503 | |
| 504 | urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */ |
| 505 | if (urb == NULL) { |
| 506 | dbg("%s: alloc for endpoint %d failed.", __FUNCTION__, endpoint); |
| 507 | return NULL; |
| 508 | } |
| 509 | |
| 510 | /* Fill URB using supplied data. */ |
| 511 | usb_fill_bulk_urb(urb, serial->dev, |
| 512 | usb_sndbulkpipe(serial->dev, endpoint) | dir, |
| 513 | buf, len, callback, ctx); |
| 514 | |
| 515 | return urb; |
| 516 | } |
| 517 | |
| 518 | /* Setup urbs */ |
| 519 | static void sierra_setup_urbs(struct usb_serial *serial) |
| 520 | { |
| 521 | int i,j; |
| 522 | struct usb_serial_port *port; |
| 523 | struct sierra_port_private *portdata; |
| 524 | |
| 525 | dbg("%s", __FUNCTION__); |
| 526 | |
| 527 | for (i = 0; i < serial->num_ports; i++) { |
| 528 | port = serial->port[i]; |
| 529 | portdata = usb_get_serial_port_data(port); |
| 530 | |
| 531 | /* Do indat endpoints first */ |
| 532 | for (j = 0; j < N_IN_URB; ++j) { |
| 533 | portdata->in_urbs[j] = sierra_setup_urb (serial, |
| 534 | port->bulk_in_endpointAddress, USB_DIR_IN, port, |
| 535 | portdata->in_buffer[j], IN_BUFLEN, sierra_indat_callback); |
| 536 | } |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 540 | static int sierra_startup(struct usb_serial *serial) |
| 541 | { |
| 542 | int i, err; |
| 543 | struct usb_serial_port *port; |
| 544 | struct sierra_port_private *portdata; |
| 545 | |
| 546 | dbg("%s", __FUNCTION__); |
| 547 | |
| 548 | /* Now setup per port private data */ |
| 549 | for (i = 0; i < serial->num_ports; i++) { |
| 550 | port = serial->port[i]; |
| 551 | portdata = kzalloc(sizeof(*portdata), GFP_KERNEL); |
| 552 | if (!portdata) { |
| 553 | dbg("%s: kmalloc for sierra_port_private (%d) failed!.", |
| 554 | __FUNCTION__, i); |
Greg Kroah-Hartman | 17c2327 | 2007-06-20 14:22:23 +0900 | [diff] [blame^] | 555 | return -ENOMEM; |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 556 | } |
Greg Kroah-Hartman | 17c2327 | 2007-06-20 14:22:23 +0900 | [diff] [blame^] | 557 | spin_lock_init(&portdata->lock); |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 558 | |
| 559 | usb_set_serial_port_data(port, portdata); |
| 560 | |
| 561 | if (! port->interrupt_in_urb) |
| 562 | continue; |
| 563 | err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); |
| 564 | if (err) |
| 565 | dbg("%s: submit irq_in urb failed %d", |
| 566 | __FUNCTION__, err); |
| 567 | } |
| 568 | |
| 569 | sierra_setup_urbs(serial); |
| 570 | |
Greg Kroah-Hartman | 17c2327 | 2007-06-20 14:22:23 +0900 | [diff] [blame^] | 571 | return 0; |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | static void sierra_shutdown(struct usb_serial *serial) |
| 575 | { |
| 576 | int i, j; |
| 577 | struct usb_serial_port *port; |
| 578 | struct sierra_port_private *portdata; |
| 579 | |
| 580 | dbg("%s", __FUNCTION__); |
| 581 | |
| 582 | /* Stop reading/writing urbs */ |
| 583 | for (i = 0; i < serial->num_ports; ++i) { |
| 584 | port = serial->port[i]; |
Greg Kroah-Hartman | f094e4f | 2007-04-26 00:12:01 -0700 | [diff] [blame] | 585 | if (!port) |
| 586 | continue; |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 587 | portdata = usb_get_serial_port_data(port); |
Greg Kroah-Hartman | f094e4f | 2007-04-26 00:12:01 -0700 | [diff] [blame] | 588 | if (!portdata) |
| 589 | continue; |
| 590 | |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 591 | for (j = 0; j < N_IN_URB; j++) |
Oliver Neukum | ebcf3ed | 2007-03-20 13:54:05 +0100 | [diff] [blame] | 592 | usb_unlink_urb(portdata->in_urbs[j]); |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | /* Now free them */ |
| 596 | for (i = 0; i < serial->num_ports; ++i) { |
| 597 | port = serial->port[i]; |
Greg Kroah-Hartman | f094e4f | 2007-04-26 00:12:01 -0700 | [diff] [blame] | 598 | if (!port) |
| 599 | continue; |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 600 | portdata = usb_get_serial_port_data(port); |
Greg Kroah-Hartman | f094e4f | 2007-04-26 00:12:01 -0700 | [diff] [blame] | 601 | if (!portdata) |
| 602 | continue; |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 603 | |
| 604 | for (j = 0; j < N_IN_URB; j++) { |
| 605 | if (portdata->in_urbs[j]) { |
| 606 | usb_free_urb(portdata->in_urbs[j]); |
| 607 | portdata->in_urbs[j] = NULL; |
| 608 | } |
| 609 | } |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | /* Now free per port private data */ |
| 613 | for (i = 0; i < serial->num_ports; i++) { |
| 614 | port = serial->port[i]; |
Greg Kroah-Hartman | f094e4f | 2007-04-26 00:12:01 -0700 | [diff] [blame] | 615 | if (!port) |
| 616 | continue; |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 617 | kfree(usb_get_serial_port_data(port)); |
| 618 | } |
| 619 | } |
| 620 | |
Greg Kroah-Hartman | 964ee1d | 2006-10-17 10:17:58 -0700 | [diff] [blame] | 621 | static struct usb_serial_driver sierra_1port_device = { |
| 622 | .driver = { |
| 623 | .owner = THIS_MODULE, |
| 624 | .name = "sierra1", |
| 625 | }, |
| 626 | .description = "Sierra USB modem (1 port)", |
| 627 | .id_table = id_table_1port, |
Johannes Hölzl | d9b1b78 | 2006-12-17 21:50:24 +0100 | [diff] [blame] | 628 | .usb_driver = &sierra_driver, |
Greg Kroah-Hartman | 964ee1d | 2006-10-17 10:17:58 -0700 | [diff] [blame] | 629 | .num_interrupt_in = NUM_DONT_CARE, |
| 630 | .num_bulk_in = 1, |
| 631 | .num_bulk_out = 1, |
| 632 | .num_ports = 1, |
| 633 | .open = sierra_open, |
| 634 | .close = sierra_close, |
| 635 | .write = sierra_write, |
| 636 | .write_room = sierra_write_room, |
| 637 | .chars_in_buffer = sierra_chars_in_buffer, |
| 638 | .throttle = sierra_rx_throttle, |
| 639 | .unthrottle = sierra_rx_unthrottle, |
| 640 | .ioctl = sierra_ioctl, |
| 641 | .set_termios = sierra_set_termios, |
| 642 | .break_ctl = sierra_break_ctl, |
| 643 | .tiocmget = sierra_tiocmget, |
| 644 | .tiocmset = sierra_tiocmset, |
| 645 | .attach = sierra_startup, |
| 646 | .shutdown = sierra_shutdown, |
| 647 | .read_int_callback = sierra_instat_callback, |
| 648 | }; |
| 649 | |
| 650 | static struct usb_serial_driver sierra_3port_device = { |
| 651 | .driver = { |
| 652 | .owner = THIS_MODULE, |
| 653 | .name = "sierra3", |
| 654 | }, |
| 655 | .description = "Sierra USB modem (3 port)", |
| 656 | .id_table = id_table_3port, |
Johannes Hölzl | d9b1b78 | 2006-12-17 21:50:24 +0100 | [diff] [blame] | 657 | .usb_driver = &sierra_driver, |
Greg Kroah-Hartman | 964ee1d | 2006-10-17 10:17:58 -0700 | [diff] [blame] | 658 | .num_interrupt_in = NUM_DONT_CARE, |
| 659 | .num_bulk_in = 3, |
| 660 | .num_bulk_out = 3, |
| 661 | .num_ports = 3, |
| 662 | .open = sierra_open, |
| 663 | .close = sierra_close, |
| 664 | .write = sierra_write, |
| 665 | .write_room = sierra_write_room, |
| 666 | .chars_in_buffer = sierra_chars_in_buffer, |
| 667 | .throttle = sierra_rx_throttle, |
| 668 | .unthrottle = sierra_rx_unthrottle, |
| 669 | .ioctl = sierra_ioctl, |
| 670 | .set_termios = sierra_set_termios, |
| 671 | .break_ctl = sierra_break_ctl, |
| 672 | .tiocmget = sierra_tiocmget, |
| 673 | .tiocmset = sierra_tiocmset, |
| 674 | .attach = sierra_startup, |
| 675 | .shutdown = sierra_shutdown, |
| 676 | .read_int_callback = sierra_instat_callback, |
| 677 | }; |
| 678 | |
| 679 | /* Functions used by new usb-serial code. */ |
| 680 | static int __init sierra_init(void) |
| 681 | { |
| 682 | int retval; |
| 683 | retval = usb_serial_register(&sierra_1port_device); |
| 684 | if (retval) |
| 685 | goto failed_1port_device_register; |
| 686 | retval = usb_serial_register(&sierra_3port_device); |
| 687 | if (retval) |
| 688 | goto failed_3port_device_register; |
| 689 | |
| 690 | |
| 691 | retval = usb_register(&sierra_driver); |
| 692 | if (retval) |
| 693 | goto failed_driver_register; |
| 694 | |
| 695 | info(DRIVER_DESC ": " DRIVER_VERSION); |
| 696 | |
| 697 | return 0; |
| 698 | |
| 699 | failed_driver_register: |
| 700 | usb_serial_deregister(&sierra_3port_device); |
| 701 | failed_3port_device_register: |
| 702 | usb_serial_deregister(&sierra_1port_device); |
| 703 | failed_1port_device_register: |
| 704 | return retval; |
| 705 | } |
| 706 | |
| 707 | static void __exit sierra_exit(void) |
| 708 | { |
| 709 | usb_deregister (&sierra_driver); |
| 710 | usb_serial_deregister(&sierra_1port_device); |
| 711 | usb_serial_deregister(&sierra_3port_device); |
| 712 | } |
| 713 | |
| 714 | module_init(sierra_init); |
| 715 | module_exit(sierra_exit); |
| 716 | |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 717 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 718 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 719 | MODULE_VERSION(DRIVER_VERSION); |
Kevin Lloyd | 69de51f | 2006-06-30 11:17:55 -0700 | [diff] [blame] | 720 | MODULE_LICENSE("GPL"); |
Kevin Lloyd | 033a3fb | 2006-10-13 23:53:21 -0700 | [diff] [blame] | 721 | |
| 722 | #ifdef CONFIG_USB_DEBUG |
| 723 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
| 724 | MODULE_PARM_DESC(debug, "Debug messages"); |
| 725 | #endif |
| 726 | |