1 /* 2 USB Driver layer for GSM modems 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: see the git log. 13 14 Work sponsored by: Sigos GmbH, Germany <info@sigos.de> 15 16 This driver exists because the "normal" serial driver doesn't work too well 17 with GSM modems. Issues: 18 - data loss -- one single Receive URB is not nearly enough 19 - controlling the baud rate doesn't make sense 20 */ 21 22 #define DRIVER_VERSION "v0.7.2" 23 #define DRIVER_AUTHOR "Matthias Urlichs <smurf@smurf.noris.de>" 24 #define DRIVER_DESC "USB Driver for GSM modems" 25 26 #include <linux/kernel.h> 27 #include <linux/jiffies.h> 28 #include <linux/errno.h> 29 #include <linux/slab.h> 30 #include <linux/tty.h> 31 #include <linux/tty_flip.h> 32 #include <linux/module.h> 33 #include <linux/bitops.h> 34 #include <linux/usb.h> 35 #include <linux/usb/serial.h> 36 #include "usb-wwan.h" 37 38 static int debug; 39 40 void usb_wwan_dtr_rts(struct usb_serial_port *port, int on) 41 { 42 struct usb_serial *serial = port->serial; 43 struct usb_wwan_port_private *portdata; 44 45 struct usb_wwan_intf_private *intfdata; 46 47 dbg("%s", __func__); 48 49 intfdata = port->serial->private; 50 51 if (!intfdata->send_setup) 52 return; 53 54 portdata = usb_get_serial_port_data(port); 55 mutex_lock(&serial->disc_mutex); 56 portdata->rts_state = on; 57 portdata->dtr_state = on; 58 if (serial->dev) 59 intfdata->send_setup(port); 60 mutex_unlock(&serial->disc_mutex); 61 } 62 EXPORT_SYMBOL(usb_wwan_dtr_rts); 63 64 void usb_wwan_set_termios(struct tty_struct *tty, 65 struct usb_serial_port *port, 66 struct ktermios *old_termios) 67 { 68 struct usb_wwan_intf_private *intfdata = port->serial->private; 69 70 dbg("%s", __func__); 71 72 /* Doesn't support option setting */ 73 tty_termios_copy_hw(tty->termios, old_termios); 74 75 if (intfdata->send_setup) 76 intfdata->send_setup(port); 77 } 78 EXPORT_SYMBOL(usb_wwan_set_termios); 79 80 int usb_wwan_tiocmget(struct tty_struct *tty, struct file *file) 81 { 82 struct usb_serial_port *port = tty->driver_data; 83 unsigned int value; 84 struct usb_wwan_port_private *portdata; 85 86 portdata = usb_get_serial_port_data(port); 87 88 value = ((portdata->rts_state) ? TIOCM_RTS : 0) | 89 ((portdata->dtr_state) ? TIOCM_DTR : 0) | 90 ((portdata->cts_state) ? TIOCM_CTS : 0) | 91 ((portdata->dsr_state) ? TIOCM_DSR : 0) | 92 ((portdata->dcd_state) ? TIOCM_CAR : 0) | 93 ((portdata->ri_state) ? TIOCM_RNG : 0); 94 95 return value; 96 } 97 EXPORT_SYMBOL(usb_wwan_tiocmget); 98 99 int usb_wwan_tiocmset(struct tty_struct *tty, struct file *file, 100 unsigned int set, unsigned int clear) 101 { 102 struct usb_serial_port *port = tty->driver_data; 103 struct usb_wwan_port_private *portdata; 104 struct usb_wwan_intf_private *intfdata; 105 106 portdata = usb_get_serial_port_data(port); 107 intfdata = port->serial->private; 108 109 if (!intfdata->send_setup) 110 return -EINVAL; 111 112 /* FIXME: what locks portdata fields ? */ 113 if (set & TIOCM_RTS) 114 portdata->rts_state = 1; 115 if (set & TIOCM_DTR) 116 portdata->dtr_state = 1; 117 118 if (clear & TIOCM_RTS) 119 portdata->rts_state = 0; 120 if (clear & TIOCM_DTR) 121 portdata->dtr_state = 0; 122 return intfdata->send_setup(port); 123 } 124 EXPORT_SYMBOL(usb_wwan_tiocmset); 125 126 /* Write */ 127 int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port, 128 const unsigned char *buf, int count) 129 { 130 struct usb_wwan_port_private *portdata; 131 struct usb_wwan_intf_private *intfdata; 132 int i; 133 int left, todo; 134 struct urb *this_urb = NULL; /* spurious */ 135 int err; 136 unsigned long flags; 137 138 portdata = usb_get_serial_port_data(port); 139 intfdata = port->serial->private; 140 141 dbg("%s: write (%d chars)", __func__, count); 142 143 i = 0; 144 left = count; 145 for (i = 0; left > 0 && i < N_OUT_URB; i++) { 146 todo = left; 147 if (todo > OUT_BUFLEN) 148 todo = OUT_BUFLEN; 149 150 this_urb = portdata->out_urbs[i]; 151 if (test_and_set_bit(i, &portdata->out_busy)) { 152 if (time_before(jiffies, 153 portdata->tx_start_time[i] + 10 * HZ)) 154 continue; 155 usb_unlink_urb(this_urb); 156 continue; 157 } 158 dbg("%s: endpoint %d buf %d", __func__, 159 usb_pipeendpoint(this_urb->pipe), i); 160 161 err = usb_autopm_get_interface_async(port->serial->interface); 162 if (err < 0) 163 break; 164 165 /* send the data */ 166 memcpy(this_urb->transfer_buffer, buf, todo); 167 this_urb->transfer_buffer_length = todo; 168 169 spin_lock_irqsave(&intfdata->susp_lock, flags); 170 if (intfdata->suspended) { 171 usb_anchor_urb(this_urb, &portdata->delayed); 172 spin_unlock_irqrestore(&intfdata->susp_lock, flags); 173 } else { 174 intfdata->in_flight++; 175 spin_unlock_irqrestore(&intfdata->susp_lock, flags); 176 err = usb_submit_urb(this_urb, GFP_ATOMIC); 177 if (err) { 178 dbg("usb_submit_urb %p (write bulk) failed " 179 "(%d)", this_urb, err); 180 clear_bit(i, &portdata->out_busy); 181 spin_lock_irqsave(&intfdata->susp_lock, flags); 182 intfdata->in_flight--; 183 spin_unlock_irqrestore(&intfdata->susp_lock, 184 flags); 185 continue; 186 } 187 } 188 189 portdata->tx_start_time[i] = jiffies; 190 buf += todo; 191 left -= todo; 192 } 193 194 count -= left; 195 dbg("%s: wrote (did %d)", __func__, count); 196 return count; 197 } 198 EXPORT_SYMBOL(usb_wwan_write); 199 200 static void usb_wwan_indat_callback(struct urb *urb) 201 { 202 int err; 203 int endpoint; 204 struct usb_serial_port *port; 205 struct tty_struct *tty; 206 unsigned char *data = urb->transfer_buffer; 207 int status = urb->status; 208 209 dbg("%s: %p", __func__, urb); 210 211 endpoint = usb_pipeendpoint(urb->pipe); 212 port = urb->context; 213 214 if (status) { 215 dbg("%s: nonzero status: %d on endpoint %02x.", 216 __func__, status, endpoint); 217 } else { 218 tty = tty_port_tty_get(&port->port); 219 if (urb->actual_length) { 220 tty_insert_flip_string(tty, data, urb->actual_length); 221 tty_flip_buffer_push(tty); 222 } else 223 dbg("%s: empty read urb received", __func__); 224 tty_kref_put(tty); 225 226 /* Resubmit urb so we continue receiving */ 227 if (status != -ESHUTDOWN) { 228 err = usb_submit_urb(urb, GFP_ATOMIC); 229 if (err && err != -EPERM) 230 printk(KERN_ERR "%s: resubmit read urb failed. " 231 "(%d)", __func__, err); 232 else 233 usb_mark_last_busy(port->serial->dev); 234 } 235 236 } 237 } 238 239 static void usb_wwan_outdat_callback(struct urb *urb) 240 { 241 struct usb_serial_port *port; 242 struct usb_wwan_port_private *portdata; 243 struct usb_wwan_intf_private *intfdata; 244 int i; 245 246 dbg("%s", __func__); 247 248 port = urb->context; 249 intfdata = port->serial->private; 250 251 usb_serial_port_softint(port); 252 usb_autopm_put_interface_async(port->serial->interface); 253 portdata = usb_get_serial_port_data(port); 254 spin_lock(&intfdata->susp_lock); 255 intfdata->in_flight--; 256 spin_unlock(&intfdata->susp_lock); 257 258 for (i = 0; i < N_OUT_URB; ++i) { 259 if (portdata->out_urbs[i] == urb) { 260 smp_mb__before_clear_bit(); 261 clear_bit(i, &portdata->out_busy); 262 break; 263 } 264 } 265 } 266 267 int usb_wwan_write_room(struct tty_struct *tty) 268 { 269 struct usb_serial_port *port = tty->driver_data; 270 struct usb_wwan_port_private *portdata; 271 int i; 272 int data_len = 0; 273 struct urb *this_urb; 274 275 portdata = usb_get_serial_port_data(port); 276 277 for (i = 0; i < N_OUT_URB; i++) { 278 this_urb = portdata->out_urbs[i]; 279 if (this_urb && !test_bit(i, &portdata->out_busy)) 280 data_len += OUT_BUFLEN; 281 } 282 283 dbg("%s: %d", __func__, data_len); 284 return data_len; 285 } 286 EXPORT_SYMBOL(usb_wwan_write_room); 287 288 int usb_wwan_chars_in_buffer(struct tty_struct *tty) 289 { 290 struct usb_serial_port *port = tty->driver_data; 291 struct usb_wwan_port_private *portdata; 292 int i; 293 int data_len = 0; 294 struct urb *this_urb; 295 296 portdata = usb_get_serial_port_data(port); 297 298 for (i = 0; i < N_OUT_URB; i++) { 299 this_urb = portdata->out_urbs[i]; 300 /* FIXME: This locking is insufficient as this_urb may 301 go unused during the test */ 302 if (this_urb && test_bit(i, &portdata->out_busy)) 303 data_len += this_urb->transfer_buffer_length; 304 } 305 dbg("%s: %d", __func__, data_len); 306 return data_len; 307 } 308 EXPORT_SYMBOL(usb_wwan_chars_in_buffer); 309 310 int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port) 311 { 312 struct usb_wwan_port_private *portdata; 313 struct usb_wwan_intf_private *intfdata; 314 struct usb_serial *serial = port->serial; 315 int i, err; 316 struct urb *urb; 317 318 portdata = usb_get_serial_port_data(port); 319 intfdata = serial->private; 320 321 dbg("%s", __func__); 322 323 /* Start reading from the IN endpoint */ 324 for (i = 0; i < N_IN_URB; i++) { 325 urb = portdata->in_urbs[i]; 326 if (!urb) 327 continue; 328 err = usb_submit_urb(urb, GFP_KERNEL); 329 if (err) { 330 dbg("%s: submit urb %d failed (%d) %d", 331 __func__, i, err, urb->transfer_buffer_length); 332 } 333 } 334 335 if (intfdata->send_setup) 336 intfdata->send_setup(port); 337 338 serial->interface->needs_remote_wakeup = 1; 339 spin_lock_irq(&intfdata->susp_lock); 340 portdata->opened = 1; 341 spin_unlock_irq(&intfdata->susp_lock); 342 usb_autopm_put_interface(serial->interface); 343 344 return 0; 345 } 346 EXPORT_SYMBOL(usb_wwan_open); 347 348 void usb_wwan_close(struct usb_serial_port *port) 349 { 350 int i; 351 struct usb_serial *serial = port->serial; 352 struct usb_wwan_port_private *portdata; 353 struct usb_wwan_intf_private *intfdata = port->serial->private; 354 355 dbg("%s", __func__); 356 portdata = usb_get_serial_port_data(port); 357 358 if (serial->dev) { 359 /* Stop reading/writing urbs */ 360 spin_lock_irq(&intfdata->susp_lock); 361 portdata->opened = 0; 362 spin_unlock_irq(&intfdata->susp_lock); 363 364 for (i = 0; i < N_IN_URB; i++) 365 usb_kill_urb(portdata->in_urbs[i]); 366 for (i = 0; i < N_OUT_URB; i++) 367 usb_kill_urb(portdata->out_urbs[i]); 368 usb_autopm_get_interface(serial->interface); 369 serial->interface->needs_remote_wakeup = 0; 370 } 371 } 372 EXPORT_SYMBOL(usb_wwan_close); 373 374 /* Helper functions used by usb_wwan_setup_urbs */ 375 static struct urb *usb_wwan_setup_urb(struct usb_serial *serial, int endpoint, 376 int dir, void *ctx, char *buf, int len, 377 void (*callback) (struct urb *)) 378 { 379 struct urb *urb; 380 381 if (endpoint == -1) 382 return NULL; /* endpoint not needed */ 383 384 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */ 385 if (urb == NULL) { 386 dbg("%s: alloc for endpoint %d failed.", __func__, endpoint); 387 return NULL; 388 } 389 390 /* Fill URB using supplied data. */ 391 usb_fill_bulk_urb(urb, serial->dev, 392 usb_sndbulkpipe(serial->dev, endpoint) | dir, 393 buf, len, callback, ctx); 394 395 return urb; 396 } 397 398 /* Setup urbs */ 399 static void usb_wwan_setup_urbs(struct usb_serial *serial) 400 { 401 int i, j; 402 struct usb_serial_port *port; 403 struct usb_wwan_port_private *portdata; 404 405 dbg("%s", __func__); 406 407 for (i = 0; i < serial->num_ports; i++) { 408 port = serial->port[i]; 409 portdata = usb_get_serial_port_data(port); 410 411 /* Do indat endpoints first */ 412 for (j = 0; j < N_IN_URB; ++j) { 413 portdata->in_urbs[j] = usb_wwan_setup_urb(serial, 414 port-> 415 bulk_in_endpointAddress, 416 USB_DIR_IN, 417 port, 418 portdata-> 419 in_buffer[j], 420 IN_BUFLEN, 421 usb_wwan_indat_callback); 422 } 423 424 /* outdat endpoints */ 425 for (j = 0; j < N_OUT_URB; ++j) { 426 portdata->out_urbs[j] = usb_wwan_setup_urb(serial, 427 port-> 428 bulk_out_endpointAddress, 429 USB_DIR_OUT, 430 port, 431 portdata-> 432 out_buffer 433 [j], 434 OUT_BUFLEN, 435 usb_wwan_outdat_callback); 436 } 437 } 438 } 439 440 int usb_wwan_startup(struct usb_serial *serial) 441 { 442 int i, j, err; 443 struct usb_serial_port *port; 444 struct usb_wwan_port_private *portdata; 445 u8 *buffer; 446 447 dbg("%s", __func__); 448 449 /* Now setup per port private data */ 450 for (i = 0; i < serial->num_ports; i++) { 451 port = serial->port[i]; 452 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL); 453 if (!portdata) { 454 dbg("%s: kmalloc for usb_wwan_port_private (%d) failed!.", 455 __func__, i); 456 return 1; 457 } 458 init_usb_anchor(&portdata->delayed); 459 460 for (j = 0; j < N_IN_URB; j++) { 461 buffer = (u8 *) __get_free_page(GFP_KERNEL); 462 if (!buffer) 463 goto bail_out_error; 464 portdata->in_buffer[j] = buffer; 465 } 466 467 for (j = 0; j < N_OUT_URB; j++) { 468 buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL); 469 if (!buffer) 470 goto bail_out_error2; 471 portdata->out_buffer[j] = buffer; 472 } 473 474 usb_set_serial_port_data(port, portdata); 475 476 if (!port->interrupt_in_urb) 477 continue; 478 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); 479 if (err) 480 dbg("%s: submit irq_in urb failed %d", __func__, err); 481 } 482 usb_wwan_setup_urbs(serial); 483 return 0; 484 485 bail_out_error2: 486 for (j = 0; j < N_OUT_URB; j++) 487 kfree(portdata->out_buffer[j]); 488 bail_out_error: 489 for (j = 0; j < N_IN_URB; j++) 490 if (portdata->in_buffer[j]) 491 free_page((unsigned long)portdata->in_buffer[j]); 492 kfree(portdata); 493 return 1; 494 } 495 EXPORT_SYMBOL(usb_wwan_startup); 496 497 static void stop_read_write_urbs(struct usb_serial *serial) 498 { 499 int i, j; 500 struct usb_serial_port *port; 501 struct usb_wwan_port_private *portdata; 502 503 /* Stop reading/writing urbs */ 504 for (i = 0; i < serial->num_ports; ++i) { 505 port = serial->port[i]; 506 portdata = usb_get_serial_port_data(port); 507 for (j = 0; j < N_IN_URB; j++) 508 usb_kill_urb(portdata->in_urbs[j]); 509 for (j = 0; j < N_OUT_URB; j++) 510 usb_kill_urb(portdata->out_urbs[j]); 511 } 512 } 513 514 void usb_wwan_disconnect(struct usb_serial *serial) 515 { 516 dbg("%s", __func__); 517 518 stop_read_write_urbs(serial); 519 } 520 EXPORT_SYMBOL(usb_wwan_disconnect); 521 522 void usb_wwan_release(struct usb_serial *serial) 523 { 524 int i, j; 525 struct usb_serial_port *port; 526 struct usb_wwan_port_private *portdata; 527 528 dbg("%s", __func__); 529 530 /* Now free them */ 531 for (i = 0; i < serial->num_ports; ++i) { 532 port = serial->port[i]; 533 portdata = usb_get_serial_port_data(port); 534 535 for (j = 0; j < N_IN_URB; j++) { 536 usb_free_urb(portdata->in_urbs[j]); 537 free_page((unsigned long) 538 portdata->in_buffer[j]); 539 portdata->in_urbs[j] = NULL; 540 } 541 for (j = 0; j < N_OUT_URB; j++) { 542 usb_free_urb(portdata->out_urbs[j]); 543 kfree(portdata->out_buffer[j]); 544 portdata->out_urbs[j] = NULL; 545 } 546 } 547 548 /* Now free per port private data */ 549 for (i = 0; i < serial->num_ports; i++) { 550 port = serial->port[i]; 551 kfree(usb_get_serial_port_data(port)); 552 } 553 } 554 EXPORT_SYMBOL(usb_wwan_release); 555 556 #ifdef CONFIG_PM 557 int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message) 558 { 559 struct usb_wwan_intf_private *intfdata = serial->private; 560 int b; 561 562 dbg("%s entered", __func__); 563 564 if (message.event & PM_EVENT_AUTO) { 565 spin_lock_irq(&intfdata->susp_lock); 566 b = intfdata->in_flight; 567 spin_unlock_irq(&intfdata->susp_lock); 568 569 if (b) 570 return -EBUSY; 571 } 572 573 spin_lock_irq(&intfdata->susp_lock); 574 intfdata->suspended = 1; 575 spin_unlock_irq(&intfdata->susp_lock); 576 stop_read_write_urbs(serial); 577 578 return 0; 579 } 580 EXPORT_SYMBOL(usb_wwan_suspend); 581 582 static void play_delayed(struct usb_serial_port *port) 583 { 584 struct usb_wwan_intf_private *data; 585 struct usb_wwan_port_private *portdata; 586 struct urb *urb; 587 int err; 588 589 portdata = usb_get_serial_port_data(port); 590 data = port->serial->private; 591 while ((urb = usb_get_from_anchor(&portdata->delayed))) { 592 err = usb_submit_urb(urb, GFP_ATOMIC); 593 if (!err) 594 data->in_flight++; 595 } 596 } 597 598 int usb_wwan_resume(struct usb_serial *serial) 599 { 600 int i, j; 601 struct usb_serial_port *port; 602 struct usb_wwan_intf_private *intfdata = serial->private; 603 struct usb_wwan_port_private *portdata; 604 struct urb *urb; 605 int err = 0; 606 607 dbg("%s entered", __func__); 608 /* get the interrupt URBs resubmitted unconditionally */ 609 for (i = 0; i < serial->num_ports; i++) { 610 port = serial->port[i]; 611 if (!port->interrupt_in_urb) { 612 dbg("%s: No interrupt URB for port %d", __func__, i); 613 continue; 614 } 615 err = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO); 616 dbg("Submitted interrupt URB for port %d (result %d)", i, err); 617 if (err < 0) { 618 err("%s: Error %d for interrupt URB of port%d", 619 __func__, err, i); 620 goto err_out; 621 } 622 } 623 624 for (i = 0; i < serial->num_ports; i++) { 625 /* walk all ports */ 626 port = serial->port[i]; 627 portdata = usb_get_serial_port_data(port); 628 629 /* skip closed ports */ 630 spin_lock_irq(&intfdata->susp_lock); 631 if (!portdata->opened) { 632 spin_unlock_irq(&intfdata->susp_lock); 633 continue; 634 } 635 636 for (j = 0; j < N_IN_URB; j++) { 637 urb = portdata->in_urbs[j]; 638 err = usb_submit_urb(urb, GFP_ATOMIC); 639 if (err < 0) { 640 err("%s: Error %d for bulk URB %d", 641 __func__, err, i); 642 spin_unlock_irq(&intfdata->susp_lock); 643 goto err_out; 644 } 645 } 646 play_delayed(port); 647 spin_unlock_irq(&intfdata->susp_lock); 648 } 649 spin_lock_irq(&intfdata->susp_lock); 650 intfdata->suspended = 0; 651 spin_unlock_irq(&intfdata->susp_lock); 652 err_out: 653 return err; 654 } 655 EXPORT_SYMBOL(usb_wwan_resume); 656 #endif 657 658 MODULE_AUTHOR(DRIVER_AUTHOR); 659 MODULE_DESCRIPTION(DRIVER_DESC); 660 MODULE_VERSION(DRIVER_VERSION); 661 MODULE_LICENSE("GPL"); 662 663 module_param(debug, bool, S_IRUGO | S_IWUSR); 664 MODULE_PARM_DESC(debug, "Debug messages"); 665