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