1 /* 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 */ 17 18 #define DRIVER_VERSION "v.1.0.6" 19 #define DRIVER_AUTHOR "Kevin Lloyd <linux@sierrawireless.com>" 20 #define DRIVER_DESC "USB Driver for Sierra Wireless USB modems" 21 22 #include <linux/kernel.h> 23 #include <linux/jiffies.h> 24 #include <linux/errno.h> 25 #include <linux/tty.h> 26 #include <linux/tty_flip.h> 27 #include <linux/module.h> 28 #include <linux/usb.h> 29 #include <linux/usb/serial.h> 30 31 32 static struct usb_device_id id_table [] = { 33 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */ 34 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */ 35 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */ 36 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */ 37 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */ 38 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless AirCard 595U */ 39 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */ 40 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */ 41 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */ 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 */ 45 46 { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */ 47 { USB_DEVICE(0x0F3D, 0x0112) }, /* AirPrime/Sierra PC 5220 */ 48 { } 49 }; 50 MODULE_DEVICE_TABLE(usb, id_table); 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 [] = { 59 { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */ 60 { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */ 61 { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */ 62 { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */ 63 { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */ 64 { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless AirCard 595U */ 65 { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */ 66 { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */ 67 { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */ 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 74 static struct usb_driver sierra_driver = { 75 .name = "sierra", 76 .probe = usb_serial_probe, 77 .disconnect = usb_serial_disconnect, 78 .id_table = id_table, 79 .no_dynamic_id = 1, 80 }; 81 82 83 static int debug; 84 85 /* per port private data */ 86 #define N_IN_URB 4 87 #define N_OUT_URB 4 88 #define IN_BUFLEN 4096 89 90 struct sierra_port_private { 91 spinlock_t lock; /* lock the structure */ 92 int outstanding_urbs; /* number of out urbs in flight */ 93 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]; 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; 105 }; 106 107 static int sierra_send_setup(struct usb_serial_port *port) 108 { 109 struct usb_serial *serial = port->serial; 110 struct sierra_port_private *portdata; 111 112 dbg("%s", __FUNCTION__); 113 114 portdata = usb_get_serial_port_data(port); 115 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 } 127 128 return 0; 129 } 130 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, 148 struct ktermios *old_termios) 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 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 220 /* Write */ 221 static int sierra_write(struct usb_serial_port *port, 222 const unsigned char *buf, int count) 223 { 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; 230 231 portdata = usb_get_serial_port_data(port); 232 233 dbg("%s: write (%d chars)", __FUNCTION__, count); 234 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); 243 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; 249 } 250 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); 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; 299 int status = urb->status; 300 301 dbg("%s: %p", __FUNCTION__, urb); 302 303 endpoint = usb_pipeendpoint(urb->pipe); 304 port = (struct usb_serial_port *) urb->context; 305 306 if (status) { 307 dbg("%s: nonzero status: %d on endpoint %02x.", 308 __FUNCTION__, status, endpoint); 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 */ 320 if (port->open_count && status != -ESHUTDOWN) { 321 err = usb_submit_urb(urb, GFP_ATOMIC); 322 if (err) 323 dev_err(&port->dev, "resubmit read urb failed." 324 "(%d)", err); 325 } 326 } 327 return; 328 } 329 330 static void sierra_instat_callback(struct urb *urb) 331 { 332 int err; 333 int status = urb->status; 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 341 if (status == 0) { 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 372 dbg("%s: error %d", __FUNCTION__, status); 373 374 /* Resubmit urb so we continue receiving IRQ data */ 375 if (status != -ESHUTDOWN) { 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 { 386 struct sierra_port_private *portdata = usb_get_serial_port_data(port); 387 unsigned long flags; 388 389 dbg("%s - port %d", __FUNCTION__, port->number); 390 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; 398 } 399 spin_unlock_irqrestore(&portdata->lock, flags); 400 401 return 2048; 402 } 403 404 static int sierra_chars_in_buffer(struct usb_serial_port *port) 405 { 406 dbg("%s - port %d", __FUNCTION__, port->number); 407 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; 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; 422 struct urb *urb; 423 int result; 424 __u16 set_mode_dzero = 0x0000; 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 result = usb_submit_urb(urb, GFP_KERNEL); 452 if (result) { 453 dev_err(&port->dev, "submit urb %d failed (%d) %d", 454 i, result, urb->transfer_buffer_length); 455 } 456 } 457 458 port->tty->low_latency = 1; 459 460 /* set mode to D0 */ 461 result = usb_control_msg(serial->dev, 462 usb_rcvctrlpipe(serial->dev, 0), 463 0x00, 0x40, set_mode_dzero, 0, NULL, 464 0, USB_CTRL_SET_TIMEOUT); 465 466 sierra_send_setup(port); 467 468 /* start up the interrupt endpoint if we have one */ 469 if (port->interrupt_in_urb) { 470 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); 471 if (result) 472 dev_err(&port->dev, "submit irq_in urb failed %d", 473 result); 474 } 475 return 0; 476 } 477 478 static void sierra_close(struct usb_serial_port *port, struct file *filp) 479 { 480 int i; 481 struct usb_serial *serial = port->serial; 482 struct sierra_port_private *portdata; 483 484 dbg("%s", __FUNCTION__); 485 portdata = usb_get_serial_port_data(port); 486 487 portdata->rts_state = 0; 488 portdata->dtr_state = 0; 489 490 if (serial->dev) { 491 sierra_send_setup(port); 492 493 /* Stop reading/writing urbs */ 494 for (i = 0; i < N_IN_URB; i++) 495 usb_kill_urb(portdata->in_urbs[i]); 496 } 497 498 usb_kill_urb(port->interrupt_in_urb); 499 500 port->tty = NULL; 501 } 502 503 static int sierra_startup(struct usb_serial *serial) 504 { 505 struct usb_serial_port *port; 506 struct sierra_port_private *portdata; 507 struct urb *urb; 508 int i; 509 int j; 510 511 dbg("%s", __FUNCTION__); 512 513 /* Now setup per port private data */ 514 for (i = 0; i < serial->num_ports; i++) { 515 port = serial->port[i]; 516 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL); 517 if (!portdata) { 518 dbg("%s: kmalloc for sierra_port_private (%d) failed!.", 519 __FUNCTION__, i); 520 return -ENOMEM; 521 } 522 spin_lock_init(&portdata->lock); 523 524 usb_set_serial_port_data(port, portdata); 525 526 /* initialize the in urbs */ 527 for (j = 0; j < N_IN_URB; ++j) { 528 urb = usb_alloc_urb(0, GFP_KERNEL); 529 if (urb == NULL) { 530 dbg("%s: alloc for in port failed.", 531 __FUNCTION__); 532 continue; 533 } 534 /* Fill URB using supplied data. */ 535 usb_fill_bulk_urb(urb, serial->dev, 536 usb_rcvbulkpipe(serial->dev, 537 port->bulk_in_endpointAddress), 538 portdata->in_buffer[j], IN_BUFLEN, 539 sierra_indat_callback, port); 540 portdata->in_urbs[j] = urb; 541 } 542 } 543 544 return 0; 545 } 546 547 static void sierra_shutdown(struct usb_serial *serial) 548 { 549 int i, j; 550 struct usb_serial_port *port; 551 struct sierra_port_private *portdata; 552 553 dbg("%s", __FUNCTION__); 554 555 for (i = 0; i < serial->num_ports; ++i) { 556 port = serial->port[i]; 557 if (!port) 558 continue; 559 portdata = usb_get_serial_port_data(port); 560 if (!portdata) 561 continue; 562 563 for (j = 0; j < N_IN_URB; j++) { 564 usb_kill_urb(portdata->in_urbs[j]); 565 usb_free_urb(portdata->in_urbs[j]); 566 portdata->in_urbs[j] = NULL; 567 } 568 kfree(portdata); 569 usb_set_serial_port_data(port, NULL); 570 } 571 } 572 573 static struct usb_serial_driver sierra_1port_device = { 574 .driver = { 575 .owner = THIS_MODULE, 576 .name = "sierra1", 577 }, 578 .description = "Sierra USB modem (1 port)", 579 .id_table = id_table_1port, 580 .usb_driver = &sierra_driver, 581 .num_interrupt_in = NUM_DONT_CARE, 582 .num_bulk_in = 1, 583 .num_bulk_out = 1, 584 .num_ports = 1, 585 .open = sierra_open, 586 .close = sierra_close, 587 .write = sierra_write, 588 .write_room = sierra_write_room, 589 .chars_in_buffer = sierra_chars_in_buffer, 590 .throttle = sierra_rx_throttle, 591 .unthrottle = sierra_rx_unthrottle, 592 .ioctl = sierra_ioctl, 593 .set_termios = sierra_set_termios, 594 .break_ctl = sierra_break_ctl, 595 .tiocmget = sierra_tiocmget, 596 .tiocmset = sierra_tiocmset, 597 .attach = sierra_startup, 598 .shutdown = sierra_shutdown, 599 .read_int_callback = sierra_instat_callback, 600 }; 601 602 static struct usb_serial_driver sierra_3port_device = { 603 .driver = { 604 .owner = THIS_MODULE, 605 .name = "sierra3", 606 }, 607 .description = "Sierra USB modem (3 port)", 608 .id_table = id_table_3port, 609 .usb_driver = &sierra_driver, 610 .num_interrupt_in = NUM_DONT_CARE, 611 .num_bulk_in = 3, 612 .num_bulk_out = 3, 613 .num_ports = 3, 614 .open = sierra_open, 615 .close = sierra_close, 616 .write = sierra_write, 617 .write_room = sierra_write_room, 618 .chars_in_buffer = sierra_chars_in_buffer, 619 .throttle = sierra_rx_throttle, 620 .unthrottle = sierra_rx_unthrottle, 621 .ioctl = sierra_ioctl, 622 .set_termios = sierra_set_termios, 623 .break_ctl = sierra_break_ctl, 624 .tiocmget = sierra_tiocmget, 625 .tiocmset = sierra_tiocmset, 626 .attach = sierra_startup, 627 .shutdown = sierra_shutdown, 628 .read_int_callback = sierra_instat_callback, 629 }; 630 631 /* Functions used by new usb-serial code. */ 632 static int __init sierra_init(void) 633 { 634 int retval; 635 retval = usb_serial_register(&sierra_1port_device); 636 if (retval) 637 goto failed_1port_device_register; 638 retval = usb_serial_register(&sierra_3port_device); 639 if (retval) 640 goto failed_3port_device_register; 641 642 643 retval = usb_register(&sierra_driver); 644 if (retval) 645 goto failed_driver_register; 646 647 info(DRIVER_DESC ": " DRIVER_VERSION); 648 649 return 0; 650 651 failed_driver_register: 652 usb_serial_deregister(&sierra_3port_device); 653 failed_3port_device_register: 654 usb_serial_deregister(&sierra_1port_device); 655 failed_1port_device_register: 656 return retval; 657 } 658 659 static void __exit sierra_exit(void) 660 { 661 usb_deregister (&sierra_driver); 662 usb_serial_deregister(&sierra_1port_device); 663 usb_serial_deregister(&sierra_3port_device); 664 } 665 666 module_init(sierra_init); 667 module_exit(sierra_exit); 668 669 MODULE_AUTHOR(DRIVER_AUTHOR); 670 MODULE_DESCRIPTION(DRIVER_DESC); 671 MODULE_VERSION(DRIVER_VERSION); 672 MODULE_LICENSE("GPL"); 673 674 #ifdef CONFIG_USB_DEBUG 675 module_param(debug, bool, S_IRUGO | S_IWUSR); 676 MODULE_PARM_DESC(debug, "Debug messages"); 677 #endif 678 679