1 /* 2 * cdc-acm.c 3 * 4 * Copyright (c) 1999 Armin Fuerst <fuerst@in.tum.de> 5 * Copyright (c) 1999 Pavel Machek <pavel@ucw.cz> 6 * Copyright (c) 1999 Johannes Erdfelt <johannes@erdfelt.com> 7 * Copyright (c) 2000 Vojtech Pavlik <vojtech@suse.cz> 8 * Copyright (c) 2004 Oliver Neukum <oliver@neukum.name> 9 * Copyright (c) 2005 David Kubicek <dave@awk.cz> 10 * Copyright (c) 2011 Johan Hovold <jhovold@gmail.com> 11 * 12 * USB Abstract Control Model driver for USB modems and ISDN adapters 13 * 14 * Sponsored by SuSE 15 * 16 * This program is free software; you can redistribute it and/or modify 17 * it under the terms of the GNU General Public License as published by 18 * the Free Software Foundation; either version 2 of the License, or 19 * (at your option) any later version. 20 * 21 * This program is distributed in the hope that it will be useful, 22 * but WITHOUT ANY WARRANTY; without even the implied warranty of 23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24 * GNU General Public License for more details. 25 * 26 * You should have received a copy of the GNU General Public License 27 * along with this program; if not, write to the Free Software 28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 29 */ 30 31 #undef DEBUG 32 #undef VERBOSE_DEBUG 33 34 #include <linux/kernel.h> 35 #include <linux/errno.h> 36 #include <linux/init.h> 37 #include <linux/slab.h> 38 #include <linux/tty.h> 39 #include <linux/serial.h> 40 #include <linux/tty_driver.h> 41 #include <linux/tty_flip.h> 42 #include <linux/module.h> 43 #include <linux/mutex.h> 44 #include <linux/uaccess.h> 45 #include <linux/usb.h> 46 #include <linux/usb/cdc.h> 47 #include <asm/byteorder.h> 48 #include <asm/unaligned.h> 49 #include <linux/list.h> 50 51 #include "cdc-acm.h" 52 53 54 #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek, Johan Hovold" 55 #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters" 56 57 static struct usb_driver acm_driver; 58 static struct tty_driver *acm_tty_driver; 59 static struct acm *acm_table[ACM_TTY_MINORS]; 60 61 static DEFINE_MUTEX(acm_table_lock); 62 63 /* 64 * acm_table accessors 65 */ 66 67 /* 68 * Look up an ACM structure by index. If found and not disconnected, increment 69 * its refcount and return it with its mutex held. 70 */ 71 static struct acm *acm_get_by_index(unsigned index) 72 { 73 struct acm *acm; 74 75 mutex_lock(&acm_table_lock); 76 acm = acm_table[index]; 77 if (acm) { 78 mutex_lock(&acm->mutex); 79 if (acm->disconnected) { 80 mutex_unlock(&acm->mutex); 81 acm = NULL; 82 } else { 83 tty_port_get(&acm->port); 84 mutex_unlock(&acm->mutex); 85 } 86 } 87 mutex_unlock(&acm_table_lock); 88 return acm; 89 } 90 91 /* 92 * Try to find an available minor number and if found, associate it with 'acm'. 93 */ 94 static int acm_alloc_minor(struct acm *acm) 95 { 96 int minor; 97 98 mutex_lock(&acm_table_lock); 99 for (minor = 0; minor < ACM_TTY_MINORS; minor++) { 100 if (!acm_table[minor]) { 101 acm_table[minor] = acm; 102 break; 103 } 104 } 105 mutex_unlock(&acm_table_lock); 106 107 return minor; 108 } 109 110 /* Release the minor number associated with 'acm'. */ 111 static void acm_release_minor(struct acm *acm) 112 { 113 mutex_lock(&acm_table_lock); 114 acm_table[acm->minor] = NULL; 115 mutex_unlock(&acm_table_lock); 116 } 117 118 /* 119 * Functions for ACM control messages. 120 */ 121 122 static int acm_ctrl_msg(struct acm *acm, int request, int value, 123 void *buf, int len) 124 { 125 int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0), 126 request, USB_RT_ACM, value, 127 acm->control->altsetting[0].desc.bInterfaceNumber, 128 buf, len, 5000); 129 dev_dbg(&acm->control->dev, 130 "%s - rq 0x%02x, val %#x, len %#x, result %d\n", 131 __func__, request, value, len, retval); 132 return retval < 0 ? retval : 0; 133 } 134 135 /* devices aren't required to support these requests. 136 * the cdc acm descriptor tells whether they do... 137 */ 138 #define acm_set_control(acm, control) \ 139 acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0) 140 #define acm_set_line(acm, line) \ 141 acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line)) 142 #define acm_send_break(acm, ms) \ 143 acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0) 144 145 /* 146 * Write buffer management. 147 * All of these assume proper locks taken by the caller. 148 */ 149 150 static int acm_wb_alloc(struct acm *acm) 151 { 152 int i, wbn; 153 struct acm_wb *wb; 154 155 wbn = 0; 156 i = 0; 157 for (;;) { 158 wb = &acm->wb[wbn]; 159 if (!wb->use) { 160 wb->use = 1; 161 return wbn; 162 } 163 wbn = (wbn + 1) % ACM_NW; 164 if (++i >= ACM_NW) 165 return -1; 166 } 167 } 168 169 static int acm_wb_is_avail(struct acm *acm) 170 { 171 int i, n; 172 unsigned long flags; 173 174 n = ACM_NW; 175 spin_lock_irqsave(&acm->write_lock, flags); 176 for (i = 0; i < ACM_NW; i++) 177 n -= acm->wb[i].use; 178 spin_unlock_irqrestore(&acm->write_lock, flags); 179 return n; 180 } 181 182 /* 183 * Finish write. Caller must hold acm->write_lock 184 */ 185 static void acm_write_done(struct acm *acm, struct acm_wb *wb) 186 { 187 wb->use = 0; 188 acm->transmitting--; 189 usb_autopm_put_interface_async(acm->control); 190 } 191 192 /* 193 * Poke write. 194 * 195 * the caller is responsible for locking 196 */ 197 198 static int acm_start_wb(struct acm *acm, struct acm_wb *wb) 199 { 200 int rc; 201 202 acm->transmitting++; 203 204 wb->urb->transfer_buffer = wb->buf; 205 wb->urb->transfer_dma = wb->dmah; 206 wb->urb->transfer_buffer_length = wb->len; 207 wb->urb->dev = acm->dev; 208 209 rc = usb_submit_urb(wb->urb, GFP_ATOMIC); 210 if (rc < 0) { 211 dev_err(&acm->data->dev, 212 "%s - usb_submit_urb(write bulk) failed: %d\n", 213 __func__, rc); 214 acm_write_done(acm, wb); 215 } 216 return rc; 217 } 218 219 /* 220 * attributes exported through sysfs 221 */ 222 static ssize_t show_caps 223 (struct device *dev, struct device_attribute *attr, char *buf) 224 { 225 struct usb_interface *intf = to_usb_interface(dev); 226 struct acm *acm = usb_get_intfdata(intf); 227 228 return sprintf(buf, "%d", acm->ctrl_caps); 229 } 230 static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL); 231 232 static ssize_t show_country_codes 233 (struct device *dev, struct device_attribute *attr, char *buf) 234 { 235 struct usb_interface *intf = to_usb_interface(dev); 236 struct acm *acm = usb_get_intfdata(intf); 237 238 memcpy(buf, acm->country_codes, acm->country_code_size); 239 return acm->country_code_size; 240 } 241 242 static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL); 243 244 static ssize_t show_country_rel_date 245 (struct device *dev, struct device_attribute *attr, char *buf) 246 { 247 struct usb_interface *intf = to_usb_interface(dev); 248 struct acm *acm = usb_get_intfdata(intf); 249 250 return sprintf(buf, "%d", acm->country_rel_date); 251 } 252 253 static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL); 254 /* 255 * Interrupt handlers for various ACM device responses 256 */ 257 258 /* control interface reports status changes with "interrupt" transfers */ 259 static void acm_ctrl_irq(struct urb *urb) 260 { 261 struct acm *acm = urb->context; 262 struct usb_cdc_notification *dr = urb->transfer_buffer; 263 unsigned char *data; 264 int newctrl; 265 int retval; 266 int status = urb->status; 267 268 switch (status) { 269 case 0: 270 /* success */ 271 break; 272 case -ECONNRESET: 273 case -ENOENT: 274 case -ESHUTDOWN: 275 /* this urb is terminated, clean up */ 276 dev_dbg(&acm->control->dev, 277 "%s - urb shutting down with status: %d\n", 278 __func__, status); 279 return; 280 default: 281 dev_dbg(&acm->control->dev, 282 "%s - nonzero urb status received: %d\n", 283 __func__, status); 284 goto exit; 285 } 286 287 usb_mark_last_busy(acm->dev); 288 289 data = (unsigned char *)(dr + 1); 290 switch (dr->bNotificationType) { 291 case USB_CDC_NOTIFY_NETWORK_CONNECTION: 292 dev_dbg(&acm->control->dev, "%s - network connection: %d\n", 293 __func__, dr->wValue); 294 break; 295 296 case USB_CDC_NOTIFY_SERIAL_STATE: 297 newctrl = get_unaligned_le16(data); 298 299 if (!acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) { 300 dev_dbg(&acm->control->dev, "%s - calling hangup\n", 301 __func__); 302 tty_port_tty_hangup(&acm->port, false); 303 } 304 305 acm->ctrlin = newctrl; 306 307 dev_dbg(&acm->control->dev, 308 "%s - input control lines: dcd%c dsr%c break%c " 309 "ring%c framing%c parity%c overrun%c\n", 310 __func__, 311 acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', 312 acm->ctrlin & ACM_CTRL_DSR ? '+' : '-', 313 acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', 314 acm->ctrlin & ACM_CTRL_RI ? '+' : '-', 315 acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-', 316 acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-', 317 acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-'); 318 break; 319 320 default: 321 dev_dbg(&acm->control->dev, 322 "%s - unknown notification %d received: index %d " 323 "len %d data0 %d data1 %d\n", 324 __func__, 325 dr->bNotificationType, dr->wIndex, 326 dr->wLength, data[0], data[1]); 327 break; 328 } 329 exit: 330 retval = usb_submit_urb(urb, GFP_ATOMIC); 331 if (retval) 332 dev_err(&acm->control->dev, "%s - usb_submit_urb failed: %d\n", 333 __func__, retval); 334 } 335 336 static int acm_submit_read_urb(struct acm *acm, int index, gfp_t mem_flags) 337 { 338 int res; 339 340 if (!test_and_clear_bit(index, &acm->read_urbs_free)) 341 return 0; 342 343 dev_vdbg(&acm->data->dev, "%s - urb %d\n", __func__, index); 344 345 res = usb_submit_urb(acm->read_urbs[index], mem_flags); 346 if (res) { 347 if (res != -EPERM) { 348 dev_err(&acm->data->dev, 349 "%s - usb_submit_urb failed: %d\n", 350 __func__, res); 351 } 352 set_bit(index, &acm->read_urbs_free); 353 return res; 354 } 355 356 return 0; 357 } 358 359 static int acm_submit_read_urbs(struct acm *acm, gfp_t mem_flags) 360 { 361 int res; 362 int i; 363 364 for (i = 0; i < acm->rx_buflimit; ++i) { 365 res = acm_submit_read_urb(acm, i, mem_flags); 366 if (res) 367 return res; 368 } 369 370 return 0; 371 } 372 373 static void acm_process_read_urb(struct acm *acm, struct urb *urb) 374 { 375 if (!urb->actual_length) 376 return; 377 378 tty_insert_flip_string(&acm->port, urb->transfer_buffer, 379 urb->actual_length); 380 tty_flip_buffer_push(&acm->port); 381 } 382 383 static void acm_read_bulk_callback(struct urb *urb) 384 { 385 struct acm_rb *rb = urb->context; 386 struct acm *acm = rb->instance; 387 unsigned long flags; 388 389 dev_vdbg(&acm->data->dev, "%s - urb %d, len %d\n", __func__, 390 rb->index, urb->actual_length); 391 set_bit(rb->index, &acm->read_urbs_free); 392 393 if (!acm->dev) { 394 dev_dbg(&acm->data->dev, "%s - disconnected\n", __func__); 395 return; 396 } 397 usb_mark_last_busy(acm->dev); 398 399 if (urb->status) { 400 dev_dbg(&acm->data->dev, "%s - non-zero urb status: %d\n", 401 __func__, urb->status); 402 return; 403 } 404 acm_process_read_urb(acm, urb); 405 406 /* throttle device if requested by tty */ 407 spin_lock_irqsave(&acm->read_lock, flags); 408 acm->throttled = acm->throttle_req; 409 if (!acm->throttled && !acm->susp_count) { 410 spin_unlock_irqrestore(&acm->read_lock, flags); 411 acm_submit_read_urb(acm, rb->index, GFP_ATOMIC); 412 } else { 413 spin_unlock_irqrestore(&acm->read_lock, flags); 414 } 415 } 416 417 /* data interface wrote those outgoing bytes */ 418 static void acm_write_bulk(struct urb *urb) 419 { 420 struct acm_wb *wb = urb->context; 421 struct acm *acm = wb->instance; 422 unsigned long flags; 423 424 if (urb->status || (urb->actual_length != urb->transfer_buffer_length)) 425 dev_vdbg(&acm->data->dev, "%s - len %d/%d, status %d\n", 426 __func__, 427 urb->actual_length, 428 urb->transfer_buffer_length, 429 urb->status); 430 431 spin_lock_irqsave(&acm->write_lock, flags); 432 acm_write_done(acm, wb); 433 spin_unlock_irqrestore(&acm->write_lock, flags); 434 schedule_work(&acm->work); 435 } 436 437 static void acm_softint(struct work_struct *work) 438 { 439 struct acm *acm = container_of(work, struct acm, work); 440 441 dev_vdbg(&acm->data->dev, "%s\n", __func__); 442 443 tty_port_tty_wakeup(&acm->port); 444 } 445 446 /* 447 * TTY handlers 448 */ 449 450 static int acm_tty_install(struct tty_driver *driver, struct tty_struct *tty) 451 { 452 struct acm *acm; 453 int retval; 454 455 dev_dbg(tty->dev, "%s\n", __func__); 456 457 acm = acm_get_by_index(tty->index); 458 if (!acm) 459 return -ENODEV; 460 461 retval = tty_standard_install(driver, tty); 462 if (retval) 463 goto error_init_termios; 464 465 tty->driver_data = acm; 466 467 return 0; 468 469 error_init_termios: 470 tty_port_put(&acm->port); 471 return retval; 472 } 473 474 static int acm_tty_open(struct tty_struct *tty, struct file *filp) 475 { 476 struct acm *acm = tty->driver_data; 477 478 dev_dbg(tty->dev, "%s\n", __func__); 479 480 return tty_port_open(&acm->port, tty, filp); 481 } 482 483 static int acm_port_activate(struct tty_port *port, struct tty_struct *tty) 484 { 485 struct acm *acm = container_of(port, struct acm, port); 486 int retval = -ENODEV; 487 488 dev_dbg(&acm->control->dev, "%s\n", __func__); 489 490 mutex_lock(&acm->mutex); 491 if (acm->disconnected) 492 goto disconnected; 493 494 retval = usb_autopm_get_interface(acm->control); 495 if (retval) 496 goto error_get_interface; 497 498 /* 499 * FIXME: Why do we need this? Allocating 64K of physically contiguous 500 * memory is really nasty... 501 */ 502 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags); 503 acm->control->needs_remote_wakeup = 1; 504 505 acm->ctrlurb->dev = acm->dev; 506 if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) { 507 dev_err(&acm->control->dev, 508 "%s - usb_submit_urb(ctrl irq) failed\n", __func__); 509 goto error_submit_urb; 510 } 511 512 acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS; 513 if (acm_set_control(acm, acm->ctrlout) < 0 && 514 (acm->ctrl_caps & USB_CDC_CAP_LINE)) 515 goto error_set_control; 516 517 usb_autopm_put_interface(acm->control); 518 519 /* 520 * Unthrottle device in case the TTY was closed while throttled. 521 */ 522 spin_lock_irq(&acm->read_lock); 523 acm->throttled = 0; 524 acm->throttle_req = 0; 525 spin_unlock_irq(&acm->read_lock); 526 527 if (acm_submit_read_urbs(acm, GFP_KERNEL)) 528 goto error_submit_read_urbs; 529 530 mutex_unlock(&acm->mutex); 531 532 return 0; 533 534 error_submit_read_urbs: 535 acm->ctrlout = 0; 536 acm_set_control(acm, acm->ctrlout); 537 error_set_control: 538 usb_kill_urb(acm->ctrlurb); 539 error_submit_urb: 540 usb_autopm_put_interface(acm->control); 541 error_get_interface: 542 disconnected: 543 mutex_unlock(&acm->mutex); 544 return retval; 545 } 546 547 static void acm_port_destruct(struct tty_port *port) 548 { 549 struct acm *acm = container_of(port, struct acm, port); 550 551 dev_dbg(&acm->control->dev, "%s\n", __func__); 552 553 acm_release_minor(acm); 554 usb_put_intf(acm->control); 555 kfree(acm->country_codes); 556 kfree(acm); 557 } 558 559 static void acm_port_shutdown(struct tty_port *port) 560 { 561 struct acm *acm = container_of(port, struct acm, port); 562 int i; 563 564 dev_dbg(&acm->control->dev, "%s\n", __func__); 565 566 mutex_lock(&acm->mutex); 567 if (!acm->disconnected) { 568 usb_autopm_get_interface(acm->control); 569 acm_set_control(acm, acm->ctrlout = 0); 570 usb_kill_urb(acm->ctrlurb); 571 for (i = 0; i < ACM_NW; i++) 572 usb_kill_urb(acm->wb[i].urb); 573 for (i = 0; i < acm->rx_buflimit; i++) 574 usb_kill_urb(acm->read_urbs[i]); 575 acm->control->needs_remote_wakeup = 0; 576 usb_autopm_put_interface(acm->control); 577 } 578 mutex_unlock(&acm->mutex); 579 } 580 581 static void acm_tty_cleanup(struct tty_struct *tty) 582 { 583 struct acm *acm = tty->driver_data; 584 dev_dbg(&acm->control->dev, "%s\n", __func__); 585 tty_port_put(&acm->port); 586 } 587 588 static void acm_tty_hangup(struct tty_struct *tty) 589 { 590 struct acm *acm = tty->driver_data; 591 dev_dbg(&acm->control->dev, "%s\n", __func__); 592 tty_port_hangup(&acm->port); 593 } 594 595 static void acm_tty_close(struct tty_struct *tty, struct file *filp) 596 { 597 struct acm *acm = tty->driver_data; 598 dev_dbg(&acm->control->dev, "%s\n", __func__); 599 tty_port_close(&acm->port, tty, filp); 600 } 601 602 static int acm_tty_write(struct tty_struct *tty, 603 const unsigned char *buf, int count) 604 { 605 struct acm *acm = tty->driver_data; 606 int stat; 607 unsigned long flags; 608 int wbn; 609 struct acm_wb *wb; 610 611 if (!count) 612 return 0; 613 614 dev_vdbg(&acm->data->dev, "%s - count %d\n", __func__, count); 615 616 spin_lock_irqsave(&acm->write_lock, flags); 617 wbn = acm_wb_alloc(acm); 618 if (wbn < 0) { 619 spin_unlock_irqrestore(&acm->write_lock, flags); 620 return 0; 621 } 622 wb = &acm->wb[wbn]; 623 624 if (!acm->dev) { 625 wb->use = 0; 626 spin_unlock_irqrestore(&acm->write_lock, flags); 627 return -ENODEV; 628 } 629 630 count = (count > acm->writesize) ? acm->writesize : count; 631 dev_vdbg(&acm->data->dev, "%s - write %d\n", __func__, count); 632 memcpy(wb->buf, buf, count); 633 wb->len = count; 634 635 usb_autopm_get_interface_async(acm->control); 636 if (acm->susp_count) { 637 if (!acm->delayed_wb) 638 acm->delayed_wb = wb; 639 else 640 usb_autopm_put_interface_async(acm->control); 641 spin_unlock_irqrestore(&acm->write_lock, flags); 642 return count; /* A white lie */ 643 } 644 usb_mark_last_busy(acm->dev); 645 646 stat = acm_start_wb(acm, wb); 647 spin_unlock_irqrestore(&acm->write_lock, flags); 648 649 if (stat < 0) 650 return stat; 651 return count; 652 } 653 654 static int acm_tty_write_room(struct tty_struct *tty) 655 { 656 struct acm *acm = tty->driver_data; 657 /* 658 * Do not let the line discipline to know that we have a reserve, 659 * or it might get too enthusiastic. 660 */ 661 return acm_wb_is_avail(acm) ? acm->writesize : 0; 662 } 663 664 static int acm_tty_chars_in_buffer(struct tty_struct *tty) 665 { 666 struct acm *acm = tty->driver_data; 667 /* 668 * if the device was unplugged then any remaining characters fell out 669 * of the connector ;) 670 */ 671 if (acm->disconnected) 672 return 0; 673 /* 674 * This is inaccurate (overcounts), but it works. 675 */ 676 return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize; 677 } 678 679 static void acm_tty_throttle(struct tty_struct *tty) 680 { 681 struct acm *acm = tty->driver_data; 682 683 spin_lock_irq(&acm->read_lock); 684 acm->throttle_req = 1; 685 spin_unlock_irq(&acm->read_lock); 686 } 687 688 static void acm_tty_unthrottle(struct tty_struct *tty) 689 { 690 struct acm *acm = tty->driver_data; 691 unsigned int was_throttled; 692 693 spin_lock_irq(&acm->read_lock); 694 was_throttled = acm->throttled; 695 acm->throttled = 0; 696 acm->throttle_req = 0; 697 spin_unlock_irq(&acm->read_lock); 698 699 if (was_throttled) 700 acm_submit_read_urbs(acm, GFP_KERNEL); 701 } 702 703 static int acm_tty_break_ctl(struct tty_struct *tty, int state) 704 { 705 struct acm *acm = tty->driver_data; 706 int retval; 707 708 retval = acm_send_break(acm, state ? 0xffff : 0); 709 if (retval < 0) 710 dev_dbg(&acm->control->dev, "%s - send break failed\n", 711 __func__); 712 return retval; 713 } 714 715 static int acm_tty_tiocmget(struct tty_struct *tty) 716 { 717 struct acm *acm = tty->driver_data; 718 719 return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) | 720 (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) | 721 (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) | 722 (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) | 723 (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) | 724 TIOCM_CTS; 725 } 726 727 static int acm_tty_tiocmset(struct tty_struct *tty, 728 unsigned int set, unsigned int clear) 729 { 730 struct acm *acm = tty->driver_data; 731 unsigned int newctrl; 732 733 newctrl = acm->ctrlout; 734 set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | 735 (set & TIOCM_RTS ? ACM_CTRL_RTS : 0); 736 clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | 737 (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0); 738 739 newctrl = (newctrl & ~clear) | set; 740 741 if (acm->ctrlout == newctrl) 742 return 0; 743 return acm_set_control(acm, acm->ctrlout = newctrl); 744 } 745 746 static int get_serial_info(struct acm *acm, struct serial_struct __user *info) 747 { 748 struct serial_struct tmp; 749 750 if (!info) 751 return -EINVAL; 752 753 memset(&tmp, 0, sizeof(tmp)); 754 tmp.flags = ASYNC_LOW_LATENCY; 755 tmp.xmit_fifo_size = acm->writesize; 756 tmp.baud_base = le32_to_cpu(acm->line.dwDTERate); 757 tmp.close_delay = acm->port.close_delay / 10; 758 tmp.closing_wait = acm->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ? 759 ASYNC_CLOSING_WAIT_NONE : 760 acm->port.closing_wait / 10; 761 762 if (copy_to_user(info, &tmp, sizeof(tmp))) 763 return -EFAULT; 764 else 765 return 0; 766 } 767 768 static int set_serial_info(struct acm *acm, 769 struct serial_struct __user *newinfo) 770 { 771 struct serial_struct new_serial; 772 unsigned int closing_wait, close_delay; 773 int retval = 0; 774 775 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial))) 776 return -EFAULT; 777 778 close_delay = new_serial.close_delay * 10; 779 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ? 780 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10; 781 782 mutex_lock(&acm->port.mutex); 783 784 if (!capable(CAP_SYS_ADMIN)) { 785 if ((close_delay != acm->port.close_delay) || 786 (closing_wait != acm->port.closing_wait)) 787 retval = -EPERM; 788 else 789 retval = -EOPNOTSUPP; 790 } else { 791 acm->port.close_delay = close_delay; 792 acm->port.closing_wait = closing_wait; 793 } 794 795 mutex_unlock(&acm->port.mutex); 796 return retval; 797 } 798 799 static int acm_tty_ioctl(struct tty_struct *tty, 800 unsigned int cmd, unsigned long arg) 801 { 802 struct acm *acm = tty->driver_data; 803 int rv = -ENOIOCTLCMD; 804 805 switch (cmd) { 806 case TIOCGSERIAL: /* gets serial port data */ 807 rv = get_serial_info(acm, (struct serial_struct __user *) arg); 808 break; 809 case TIOCSSERIAL: 810 rv = set_serial_info(acm, (struct serial_struct __user *) arg); 811 break; 812 } 813 814 return rv; 815 } 816 817 static void acm_tty_set_termios(struct tty_struct *tty, 818 struct ktermios *termios_old) 819 { 820 struct acm *acm = tty->driver_data; 821 struct ktermios *termios = &tty->termios; 822 struct usb_cdc_line_coding newline; 823 int newctrl = acm->ctrlout; 824 825 newline.dwDTERate = cpu_to_le32(tty_get_baud_rate(tty)); 826 newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0; 827 newline.bParityType = termios->c_cflag & PARENB ? 828 (termios->c_cflag & PARODD ? 1 : 2) + 829 (termios->c_cflag & CMSPAR ? 2 : 0) : 0; 830 switch (termios->c_cflag & CSIZE) { 831 case CS5: 832 newline.bDataBits = 5; 833 break; 834 case CS6: 835 newline.bDataBits = 6; 836 break; 837 case CS7: 838 newline.bDataBits = 7; 839 break; 840 case CS8: 841 default: 842 newline.bDataBits = 8; 843 break; 844 } 845 /* FIXME: Needs to clear unsupported bits in the termios */ 846 acm->clocal = ((termios->c_cflag & CLOCAL) != 0); 847 848 if (!newline.dwDTERate) { 849 newline.dwDTERate = acm->line.dwDTERate; 850 newctrl &= ~ACM_CTRL_DTR; 851 } else 852 newctrl |= ACM_CTRL_DTR; 853 854 if (newctrl != acm->ctrlout) 855 acm_set_control(acm, acm->ctrlout = newctrl); 856 857 if (memcmp(&acm->line, &newline, sizeof newline)) { 858 memcpy(&acm->line, &newline, sizeof newline); 859 dev_dbg(&acm->control->dev, "%s - set line: %d %d %d %d\n", 860 __func__, 861 le32_to_cpu(newline.dwDTERate), 862 newline.bCharFormat, newline.bParityType, 863 newline.bDataBits); 864 acm_set_line(acm, &acm->line); 865 } 866 } 867 868 static const struct tty_port_operations acm_port_ops = { 869 .shutdown = acm_port_shutdown, 870 .activate = acm_port_activate, 871 .destruct = acm_port_destruct, 872 }; 873 874 /* 875 * USB probe and disconnect routines. 876 */ 877 878 /* Little helpers: write/read buffers free */ 879 static void acm_write_buffers_free(struct acm *acm) 880 { 881 int i; 882 struct acm_wb *wb; 883 struct usb_device *usb_dev = interface_to_usbdev(acm->control); 884 885 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) 886 usb_free_coherent(usb_dev, acm->writesize, wb->buf, wb->dmah); 887 } 888 889 static void acm_read_buffers_free(struct acm *acm) 890 { 891 struct usb_device *usb_dev = interface_to_usbdev(acm->control); 892 int i; 893 894 for (i = 0; i < acm->rx_buflimit; i++) 895 usb_free_coherent(usb_dev, acm->readsize, 896 acm->read_buffers[i].base, acm->read_buffers[i].dma); 897 } 898 899 /* Little helper: write buffers allocate */ 900 static int acm_write_buffers_alloc(struct acm *acm) 901 { 902 int i; 903 struct acm_wb *wb; 904 905 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) { 906 wb->buf = usb_alloc_coherent(acm->dev, acm->writesize, GFP_KERNEL, 907 &wb->dmah); 908 if (!wb->buf) { 909 while (i != 0) { 910 --i; 911 --wb; 912 usb_free_coherent(acm->dev, acm->writesize, 913 wb->buf, wb->dmah); 914 } 915 return -ENOMEM; 916 } 917 } 918 return 0; 919 } 920 921 static int acm_probe(struct usb_interface *intf, 922 const struct usb_device_id *id) 923 { 924 struct usb_cdc_union_desc *union_header = NULL; 925 struct usb_cdc_country_functional_desc *cfd = NULL; 926 unsigned char *buffer = intf->altsetting->extra; 927 int buflen = intf->altsetting->extralen; 928 struct usb_interface *control_interface; 929 struct usb_interface *data_interface; 930 struct usb_endpoint_descriptor *epctrl = NULL; 931 struct usb_endpoint_descriptor *epread = NULL; 932 struct usb_endpoint_descriptor *epwrite = NULL; 933 struct usb_device *usb_dev = interface_to_usbdev(intf); 934 struct acm *acm; 935 int minor; 936 int ctrlsize, readsize; 937 u8 *buf; 938 u8 ac_management_function = 0; 939 u8 call_management_function = 0; 940 int call_interface_num = -1; 941 int data_interface_num = -1; 942 unsigned long quirks; 943 int num_rx_buf; 944 int i; 945 int combined_interfaces = 0; 946 struct device *tty_dev; 947 int rv = -ENOMEM; 948 949 /* normal quirks */ 950 quirks = (unsigned long)id->driver_info; 951 952 if (quirks == IGNORE_DEVICE) 953 return -ENODEV; 954 955 num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR; 956 957 /* handle quirks deadly to normal probing*/ 958 if (quirks == NO_UNION_NORMAL) { 959 data_interface = usb_ifnum_to_if(usb_dev, 1); 960 control_interface = usb_ifnum_to_if(usb_dev, 0); 961 goto skip_normal_probe; 962 } 963 964 /* normal probing*/ 965 if (!buffer) { 966 dev_err(&intf->dev, "Weird descriptor references\n"); 967 return -EINVAL; 968 } 969 970 if (!buflen) { 971 if (intf->cur_altsetting->endpoint && 972 intf->cur_altsetting->endpoint->extralen && 973 intf->cur_altsetting->endpoint->extra) { 974 dev_dbg(&intf->dev, 975 "Seeking extra descriptors on endpoint\n"); 976 buflen = intf->cur_altsetting->endpoint->extralen; 977 buffer = intf->cur_altsetting->endpoint->extra; 978 } else { 979 dev_err(&intf->dev, 980 "Zero length descriptor references\n"); 981 return -EINVAL; 982 } 983 } 984 985 while (buflen > 0) { 986 if (buffer[1] != USB_DT_CS_INTERFACE) { 987 dev_err(&intf->dev, "skipping garbage\n"); 988 goto next_desc; 989 } 990 991 switch (buffer[2]) { 992 case USB_CDC_UNION_TYPE: /* we've found it */ 993 if (union_header) { 994 dev_err(&intf->dev, "More than one " 995 "union descriptor, skipping ...\n"); 996 goto next_desc; 997 } 998 union_header = (struct usb_cdc_union_desc *)buffer; 999 break; 1000 case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/ 1001 cfd = (struct usb_cdc_country_functional_desc *)buffer; 1002 break; 1003 case USB_CDC_HEADER_TYPE: /* maybe check version */ 1004 break; /* for now we ignore it */ 1005 case USB_CDC_ACM_TYPE: 1006 ac_management_function = buffer[3]; 1007 break; 1008 case USB_CDC_CALL_MANAGEMENT_TYPE: 1009 call_management_function = buffer[3]; 1010 call_interface_num = buffer[4]; 1011 if ((quirks & NOT_A_MODEM) == 0 && (call_management_function & 3) != 3) 1012 dev_err(&intf->dev, "This device cannot do calls on its own. It is not a modem.\n"); 1013 break; 1014 default: 1015 /* there are LOTS more CDC descriptors that 1016 * could legitimately be found here. 1017 */ 1018 dev_dbg(&intf->dev, "Ignoring descriptor: " 1019 "type %02x, length %d\n", 1020 buffer[2], buffer[0]); 1021 break; 1022 } 1023 next_desc: 1024 buflen -= buffer[0]; 1025 buffer += buffer[0]; 1026 } 1027 1028 if (!union_header) { 1029 if (call_interface_num > 0) { 1030 dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n"); 1031 /* quirks for Droids MuIn LCD */ 1032 if (quirks & NO_DATA_INTERFACE) 1033 data_interface = usb_ifnum_to_if(usb_dev, 0); 1034 else 1035 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num)); 1036 control_interface = intf; 1037 } else { 1038 if (intf->cur_altsetting->desc.bNumEndpoints != 3) { 1039 dev_dbg(&intf->dev,"No union descriptor, giving up\n"); 1040 return -ENODEV; 1041 } else { 1042 dev_warn(&intf->dev,"No union descriptor, testing for castrated device\n"); 1043 combined_interfaces = 1; 1044 control_interface = data_interface = intf; 1045 goto look_for_collapsed_interface; 1046 } 1047 } 1048 } else { 1049 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0); 1050 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0)); 1051 if (!control_interface || !data_interface) { 1052 dev_dbg(&intf->dev, "no interfaces\n"); 1053 return -ENODEV; 1054 } 1055 } 1056 1057 if (data_interface_num != call_interface_num) 1058 dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n"); 1059 1060 if (control_interface == data_interface) { 1061 /* some broken devices designed for windows work this way */ 1062 dev_warn(&intf->dev,"Control and data interfaces are not separated!\n"); 1063 combined_interfaces = 1; 1064 /* a popular other OS doesn't use it */ 1065 quirks |= NO_CAP_LINE; 1066 if (data_interface->cur_altsetting->desc.bNumEndpoints != 3) { 1067 dev_err(&intf->dev, "This needs exactly 3 endpoints\n"); 1068 return -EINVAL; 1069 } 1070 look_for_collapsed_interface: 1071 for (i = 0; i < 3; i++) { 1072 struct usb_endpoint_descriptor *ep; 1073 ep = &data_interface->cur_altsetting->endpoint[i].desc; 1074 1075 if (usb_endpoint_is_int_in(ep)) 1076 epctrl = ep; 1077 else if (usb_endpoint_is_bulk_out(ep)) 1078 epwrite = ep; 1079 else if (usb_endpoint_is_bulk_in(ep)) 1080 epread = ep; 1081 else 1082 return -EINVAL; 1083 } 1084 if (!epctrl || !epread || !epwrite) 1085 return -ENODEV; 1086 else 1087 goto made_compressed_probe; 1088 } 1089 1090 skip_normal_probe: 1091 1092 /*workaround for switched interfaces */ 1093 if (data_interface->cur_altsetting->desc.bInterfaceClass 1094 != CDC_DATA_INTERFACE_TYPE) { 1095 if (control_interface->cur_altsetting->desc.bInterfaceClass 1096 == CDC_DATA_INTERFACE_TYPE) { 1097 struct usb_interface *t; 1098 dev_dbg(&intf->dev, 1099 "Your device has switched interfaces.\n"); 1100 t = control_interface; 1101 control_interface = data_interface; 1102 data_interface = t; 1103 } else { 1104 return -EINVAL; 1105 } 1106 } 1107 1108 /* Accept probe requests only for the control interface */ 1109 if (!combined_interfaces && intf != control_interface) 1110 return -ENODEV; 1111 1112 if (!combined_interfaces && usb_interface_claimed(data_interface)) { 1113 /* valid in this context */ 1114 dev_dbg(&intf->dev, "The data interface isn't available\n"); 1115 return -EBUSY; 1116 } 1117 1118 1119 if (data_interface->cur_altsetting->desc.bNumEndpoints < 2 || 1120 control_interface->cur_altsetting->desc.bNumEndpoints == 0) 1121 return -EINVAL; 1122 1123 epctrl = &control_interface->cur_altsetting->endpoint[0].desc; 1124 epread = &data_interface->cur_altsetting->endpoint[0].desc; 1125 epwrite = &data_interface->cur_altsetting->endpoint[1].desc; 1126 1127 1128 /* workaround for switched endpoints */ 1129 if (!usb_endpoint_dir_in(epread)) { 1130 /* descriptors are swapped */ 1131 struct usb_endpoint_descriptor *t; 1132 dev_dbg(&intf->dev, 1133 "The data interface has switched endpoints\n"); 1134 t = epread; 1135 epread = epwrite; 1136 epwrite = t; 1137 } 1138 made_compressed_probe: 1139 dev_dbg(&intf->dev, "interfaces are valid\n"); 1140 1141 acm = kzalloc(sizeof(struct acm), GFP_KERNEL); 1142 if (acm == NULL) { 1143 dev_err(&intf->dev, "out of memory (acm kzalloc)\n"); 1144 goto alloc_fail; 1145 } 1146 1147 minor = acm_alloc_minor(acm); 1148 if (minor == ACM_TTY_MINORS) { 1149 dev_err(&intf->dev, "no more free acm devices\n"); 1150 kfree(acm); 1151 return -ENODEV; 1152 } 1153 1154 ctrlsize = usb_endpoint_maxp(epctrl); 1155 readsize = usb_endpoint_maxp(epread) * 1156 (quirks == SINGLE_RX_URB ? 1 : 2); 1157 acm->combined_interfaces = combined_interfaces; 1158 acm->writesize = usb_endpoint_maxp(epwrite) * 20; 1159 acm->control = control_interface; 1160 acm->data = data_interface; 1161 acm->minor = minor; 1162 acm->dev = usb_dev; 1163 acm->ctrl_caps = ac_management_function; 1164 if (quirks & NO_CAP_LINE) 1165 acm->ctrl_caps &= ~USB_CDC_CAP_LINE; 1166 acm->ctrlsize = ctrlsize; 1167 acm->readsize = readsize; 1168 acm->rx_buflimit = num_rx_buf; 1169 INIT_WORK(&acm->work, acm_softint); 1170 spin_lock_init(&acm->write_lock); 1171 spin_lock_init(&acm->read_lock); 1172 mutex_init(&acm->mutex); 1173 acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress); 1174 acm->is_int_ep = usb_endpoint_xfer_int(epread); 1175 if (acm->is_int_ep) 1176 acm->bInterval = epread->bInterval; 1177 tty_port_init(&acm->port); 1178 acm->port.ops = &acm_port_ops; 1179 1180 buf = usb_alloc_coherent(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma); 1181 if (!buf) { 1182 dev_err(&intf->dev, "out of memory (ctrl buffer alloc)\n"); 1183 goto alloc_fail2; 1184 } 1185 acm->ctrl_buffer = buf; 1186 1187 if (acm_write_buffers_alloc(acm) < 0) { 1188 dev_err(&intf->dev, "out of memory (write buffer alloc)\n"); 1189 goto alloc_fail4; 1190 } 1191 1192 acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL); 1193 if (!acm->ctrlurb) { 1194 dev_err(&intf->dev, "out of memory (ctrlurb kmalloc)\n"); 1195 goto alloc_fail5; 1196 } 1197 for (i = 0; i < num_rx_buf; i++) { 1198 struct acm_rb *rb = &(acm->read_buffers[i]); 1199 struct urb *urb; 1200 1201 rb->base = usb_alloc_coherent(acm->dev, readsize, GFP_KERNEL, 1202 &rb->dma); 1203 if (!rb->base) { 1204 dev_err(&intf->dev, "out of memory " 1205 "(read bufs usb_alloc_coherent)\n"); 1206 goto alloc_fail6; 1207 } 1208 rb->index = i; 1209 rb->instance = acm; 1210 1211 urb = usb_alloc_urb(0, GFP_KERNEL); 1212 if (!urb) { 1213 dev_err(&intf->dev, 1214 "out of memory (read urbs usb_alloc_urb)\n"); 1215 goto alloc_fail6; 1216 } 1217 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 1218 urb->transfer_dma = rb->dma; 1219 if (acm->is_int_ep) { 1220 usb_fill_int_urb(urb, acm->dev, 1221 acm->rx_endpoint, 1222 rb->base, 1223 acm->readsize, 1224 acm_read_bulk_callback, rb, 1225 acm->bInterval); 1226 } else { 1227 usb_fill_bulk_urb(urb, acm->dev, 1228 acm->rx_endpoint, 1229 rb->base, 1230 acm->readsize, 1231 acm_read_bulk_callback, rb); 1232 } 1233 1234 acm->read_urbs[i] = urb; 1235 __set_bit(i, &acm->read_urbs_free); 1236 } 1237 for (i = 0; i < ACM_NW; i++) { 1238 struct acm_wb *snd = &(acm->wb[i]); 1239 1240 snd->urb = usb_alloc_urb(0, GFP_KERNEL); 1241 if (snd->urb == NULL) { 1242 dev_err(&intf->dev, 1243 "out of memory (write urbs usb_alloc_urb)\n"); 1244 goto alloc_fail7; 1245 } 1246 1247 if (usb_endpoint_xfer_int(epwrite)) 1248 usb_fill_int_urb(snd->urb, usb_dev, 1249 usb_sndintpipe(usb_dev, epwrite->bEndpointAddress), 1250 NULL, acm->writesize, acm_write_bulk, snd, epwrite->bInterval); 1251 else 1252 usb_fill_bulk_urb(snd->urb, usb_dev, 1253 usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress), 1254 NULL, acm->writesize, acm_write_bulk, snd); 1255 snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 1256 snd->instance = acm; 1257 } 1258 1259 usb_set_intfdata(intf, acm); 1260 1261 i = device_create_file(&intf->dev, &dev_attr_bmCapabilities); 1262 if (i < 0) 1263 goto alloc_fail7; 1264 1265 if (cfd) { /* export the country data */ 1266 acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL); 1267 if (!acm->country_codes) 1268 goto skip_countries; 1269 acm->country_code_size = cfd->bLength - 4; 1270 memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0, 1271 cfd->bLength - 4); 1272 acm->country_rel_date = cfd->iCountryCodeRelDate; 1273 1274 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes); 1275 if (i < 0) { 1276 kfree(acm->country_codes); 1277 acm->country_codes = NULL; 1278 acm->country_code_size = 0; 1279 goto skip_countries; 1280 } 1281 1282 i = device_create_file(&intf->dev, 1283 &dev_attr_iCountryCodeRelDate); 1284 if (i < 0) { 1285 device_remove_file(&intf->dev, &dev_attr_wCountryCodes); 1286 kfree(acm->country_codes); 1287 acm->country_codes = NULL; 1288 acm->country_code_size = 0; 1289 goto skip_countries; 1290 } 1291 } 1292 1293 skip_countries: 1294 usb_fill_int_urb(acm->ctrlurb, usb_dev, 1295 usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress), 1296 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, 1297 /* works around buggy devices */ 1298 epctrl->bInterval ? epctrl->bInterval : 16); 1299 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 1300 acm->ctrlurb->transfer_dma = acm->ctrl_dma; 1301 1302 dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor); 1303 1304 acm_set_control(acm, acm->ctrlout); 1305 1306 acm->line.dwDTERate = cpu_to_le32(9600); 1307 acm->line.bDataBits = 8; 1308 acm_set_line(acm, &acm->line); 1309 1310 usb_driver_claim_interface(&acm_driver, data_interface, acm); 1311 usb_set_intfdata(data_interface, acm); 1312 1313 usb_get_intf(control_interface); 1314 tty_dev = tty_port_register_device(&acm->port, acm_tty_driver, minor, 1315 &control_interface->dev); 1316 if (IS_ERR(tty_dev)) { 1317 rv = PTR_ERR(tty_dev); 1318 goto alloc_fail8; 1319 } 1320 1321 return 0; 1322 alloc_fail8: 1323 if (acm->country_codes) { 1324 device_remove_file(&acm->control->dev, 1325 &dev_attr_wCountryCodes); 1326 device_remove_file(&acm->control->dev, 1327 &dev_attr_iCountryCodeRelDate); 1328 } 1329 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities); 1330 alloc_fail7: 1331 usb_set_intfdata(intf, NULL); 1332 for (i = 0; i < ACM_NW; i++) 1333 usb_free_urb(acm->wb[i].urb); 1334 alloc_fail6: 1335 for (i = 0; i < num_rx_buf; i++) 1336 usb_free_urb(acm->read_urbs[i]); 1337 acm_read_buffers_free(acm); 1338 usb_free_urb(acm->ctrlurb); 1339 alloc_fail5: 1340 acm_write_buffers_free(acm); 1341 alloc_fail4: 1342 usb_free_coherent(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma); 1343 alloc_fail2: 1344 acm_release_minor(acm); 1345 kfree(acm); 1346 alloc_fail: 1347 return rv; 1348 } 1349 1350 static void stop_data_traffic(struct acm *acm) 1351 { 1352 int i; 1353 1354 dev_dbg(&acm->control->dev, "%s\n", __func__); 1355 1356 usb_kill_urb(acm->ctrlurb); 1357 for (i = 0; i < ACM_NW; i++) 1358 usb_kill_urb(acm->wb[i].urb); 1359 for (i = 0; i < acm->rx_buflimit; i++) 1360 usb_kill_urb(acm->read_urbs[i]); 1361 1362 cancel_work_sync(&acm->work); 1363 } 1364 1365 static void acm_disconnect(struct usb_interface *intf) 1366 { 1367 struct acm *acm = usb_get_intfdata(intf); 1368 struct usb_device *usb_dev = interface_to_usbdev(intf); 1369 struct tty_struct *tty; 1370 int i; 1371 1372 dev_dbg(&intf->dev, "%s\n", __func__); 1373 1374 /* sibling interface is already cleaning up */ 1375 if (!acm) 1376 return; 1377 1378 mutex_lock(&acm->mutex); 1379 acm->disconnected = true; 1380 if (acm->country_codes) { 1381 device_remove_file(&acm->control->dev, 1382 &dev_attr_wCountryCodes); 1383 device_remove_file(&acm->control->dev, 1384 &dev_attr_iCountryCodeRelDate); 1385 } 1386 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities); 1387 usb_set_intfdata(acm->control, NULL); 1388 usb_set_intfdata(acm->data, NULL); 1389 mutex_unlock(&acm->mutex); 1390 1391 tty = tty_port_tty_get(&acm->port); 1392 if (tty) { 1393 tty_vhangup(tty); 1394 tty_kref_put(tty); 1395 } 1396 1397 stop_data_traffic(acm); 1398 1399 tty_unregister_device(acm_tty_driver, acm->minor); 1400 1401 usb_free_urb(acm->ctrlurb); 1402 for (i = 0; i < ACM_NW; i++) 1403 usb_free_urb(acm->wb[i].urb); 1404 for (i = 0; i < acm->rx_buflimit; i++) 1405 usb_free_urb(acm->read_urbs[i]); 1406 acm_write_buffers_free(acm); 1407 usb_free_coherent(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma); 1408 acm_read_buffers_free(acm); 1409 1410 if (!acm->combined_interfaces) 1411 usb_driver_release_interface(&acm_driver, intf == acm->control ? 1412 acm->data : acm->control); 1413 1414 tty_port_put(&acm->port); 1415 } 1416 1417 #ifdef CONFIG_PM 1418 static int acm_suspend(struct usb_interface *intf, pm_message_t message) 1419 { 1420 struct acm *acm = usb_get_intfdata(intf); 1421 int cnt; 1422 1423 if (PMSG_IS_AUTO(message)) { 1424 int b; 1425 1426 spin_lock_irq(&acm->write_lock); 1427 b = acm->transmitting; 1428 spin_unlock_irq(&acm->write_lock); 1429 if (b) 1430 return -EBUSY; 1431 } 1432 1433 spin_lock_irq(&acm->read_lock); 1434 spin_lock(&acm->write_lock); 1435 cnt = acm->susp_count++; 1436 spin_unlock(&acm->write_lock); 1437 spin_unlock_irq(&acm->read_lock); 1438 1439 if (cnt) 1440 return 0; 1441 1442 if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags)) 1443 stop_data_traffic(acm); 1444 1445 return 0; 1446 } 1447 1448 static int acm_resume(struct usb_interface *intf) 1449 { 1450 struct acm *acm = usb_get_intfdata(intf); 1451 struct acm_wb *wb; 1452 int rv = 0; 1453 int cnt; 1454 1455 spin_lock_irq(&acm->read_lock); 1456 acm->susp_count -= 1; 1457 cnt = acm->susp_count; 1458 spin_unlock_irq(&acm->read_lock); 1459 1460 if (cnt) 1461 return 0; 1462 1463 if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags)) { 1464 rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO); 1465 1466 spin_lock_irq(&acm->write_lock); 1467 if (acm->delayed_wb) { 1468 wb = acm->delayed_wb; 1469 acm->delayed_wb = NULL; 1470 spin_unlock_irq(&acm->write_lock); 1471 acm_start_wb(acm, wb); 1472 } else { 1473 spin_unlock_irq(&acm->write_lock); 1474 } 1475 1476 /* 1477 * delayed error checking because we must 1478 * do the write path at all cost 1479 */ 1480 if (rv < 0) 1481 goto err_out; 1482 1483 rv = acm_submit_read_urbs(acm, GFP_NOIO); 1484 } 1485 1486 err_out: 1487 return rv; 1488 } 1489 1490 static int acm_reset_resume(struct usb_interface *intf) 1491 { 1492 struct acm *acm = usb_get_intfdata(intf); 1493 1494 if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags)) 1495 tty_port_tty_hangup(&acm->port, false); 1496 1497 return acm_resume(intf); 1498 } 1499 1500 #endif /* CONFIG_PM */ 1501 1502 #define NOKIA_PCSUITE_ACM_INFO(x) \ 1503 USB_DEVICE_AND_INTERFACE_INFO(0x0421, x, \ 1504 USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \ 1505 USB_CDC_ACM_PROTO_VENDOR) 1506 1507 #define SAMSUNG_PCSUITE_ACM_INFO(x) \ 1508 USB_DEVICE_AND_INTERFACE_INFO(0x04e7, x, \ 1509 USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \ 1510 USB_CDC_ACM_PROTO_VENDOR) 1511 1512 /* 1513 * USB driver structure. 1514 */ 1515 1516 static const struct usb_device_id acm_ids[] = { 1517 /* quirky and broken devices */ 1518 { USB_DEVICE(0x17ef, 0x7000), /* Lenovo USB modem */ 1519 .driver_info = NO_UNION_NORMAL, },/* has no union descriptor */ 1520 { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */ 1521 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1522 }, 1523 { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */ 1524 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1525 }, 1526 { USB_DEVICE(0x0e8d, 0x3329), /* MediaTek Inc GPS */ 1527 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1528 }, 1529 { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */ 1530 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1531 }, 1532 { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */ 1533 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1534 }, 1535 { USB_DEVICE(0x0ace, 0x1602), /* ZyDAS 56K USB MODEM */ 1536 .driver_info = SINGLE_RX_URB, 1537 }, 1538 { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */ 1539 .driver_info = SINGLE_RX_URB, /* firmware bug */ 1540 }, 1541 { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */ 1542 .driver_info = SINGLE_RX_URB, /* firmware bug */ 1543 }, 1544 { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */ 1545 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1546 }, 1547 { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */ 1548 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1549 }, 1550 { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */ 1551 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1552 }, 1553 { USB_DEVICE(0x0572, 0x1324), /* Conexant USB MODEM RD02-D400 */ 1554 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1555 }, 1556 { USB_DEVICE(0x0572, 0x1328), /* Shiro / Aztech USB MODEM UM-3100 */ 1557 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1558 }, 1559 { USB_DEVICE(0x22b8, 0x6425), /* Motorola MOTOMAGX phones */ 1560 }, 1561 /* Motorola H24 HSPA module: */ 1562 { USB_DEVICE(0x22b8, 0x2d91) }, /* modem */ 1563 { USB_DEVICE(0x22b8, 0x2d92) }, /* modem + diagnostics */ 1564 { USB_DEVICE(0x22b8, 0x2d93) }, /* modem + AT port */ 1565 { USB_DEVICE(0x22b8, 0x2d95) }, /* modem + AT port + diagnostics */ 1566 { USB_DEVICE(0x22b8, 0x2d96) }, /* modem + NMEA */ 1567 { USB_DEVICE(0x22b8, 0x2d97) }, /* modem + diagnostics + NMEA */ 1568 { USB_DEVICE(0x22b8, 0x2d99) }, /* modem + AT port + NMEA */ 1569 { USB_DEVICE(0x22b8, 0x2d9a) }, /* modem + AT port + diagnostics + NMEA */ 1570 1571 { USB_DEVICE(0x0572, 0x1329), /* Hummingbird huc56s (Conexant) */ 1572 .driver_info = NO_UNION_NORMAL, /* union descriptor misplaced on 1573 data interface instead of 1574 communications interface. 1575 Maybe we should define a new 1576 quirk for this. */ 1577 }, 1578 { USB_DEVICE(0x0572, 0x1340), /* Conexant CX93010-2x UCMxx */ 1579 .driver_info = NO_UNION_NORMAL, 1580 }, 1581 { USB_DEVICE(0x05f9, 0x4002), /* PSC Scanning, Magellan 800i */ 1582 .driver_info = NO_UNION_NORMAL, 1583 }, 1584 { USB_DEVICE(0x1bbb, 0x0003), /* Alcatel OT-I650 */ 1585 .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */ 1586 }, 1587 { USB_DEVICE(0x1576, 0x03b1), /* Maretron USB100 */ 1588 .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */ 1589 }, 1590 1591 /* Nokia S60 phones expose two ACM channels. The first is 1592 * a modem and is picked up by the standard AT-command 1593 * information below. The second is 'vendor-specific' but 1594 * is treated as a serial device at the S60 end, so we want 1595 * to expose it on Linux too. */ 1596 { NOKIA_PCSUITE_ACM_INFO(0x042D), }, /* Nokia 3250 */ 1597 { NOKIA_PCSUITE_ACM_INFO(0x04D8), }, /* Nokia 5500 Sport */ 1598 { NOKIA_PCSUITE_ACM_INFO(0x04C9), }, /* Nokia E50 */ 1599 { NOKIA_PCSUITE_ACM_INFO(0x0419), }, /* Nokia E60 */ 1600 { NOKIA_PCSUITE_ACM_INFO(0x044D), }, /* Nokia E61 */ 1601 { NOKIA_PCSUITE_ACM_INFO(0x0001), }, /* Nokia E61i */ 1602 { NOKIA_PCSUITE_ACM_INFO(0x0475), }, /* Nokia E62 */ 1603 { NOKIA_PCSUITE_ACM_INFO(0x0508), }, /* Nokia E65 */ 1604 { NOKIA_PCSUITE_ACM_INFO(0x0418), }, /* Nokia E70 */ 1605 { NOKIA_PCSUITE_ACM_INFO(0x0425), }, /* Nokia N71 */ 1606 { NOKIA_PCSUITE_ACM_INFO(0x0486), }, /* Nokia N73 */ 1607 { NOKIA_PCSUITE_ACM_INFO(0x04DF), }, /* Nokia N75 */ 1608 { NOKIA_PCSUITE_ACM_INFO(0x000e), }, /* Nokia N77 */ 1609 { NOKIA_PCSUITE_ACM_INFO(0x0445), }, /* Nokia N80 */ 1610 { NOKIA_PCSUITE_ACM_INFO(0x042F), }, /* Nokia N91 & N91 8GB */ 1611 { NOKIA_PCSUITE_ACM_INFO(0x048E), }, /* Nokia N92 */ 1612 { NOKIA_PCSUITE_ACM_INFO(0x0420), }, /* Nokia N93 */ 1613 { NOKIA_PCSUITE_ACM_INFO(0x04E6), }, /* Nokia N93i */ 1614 { NOKIA_PCSUITE_ACM_INFO(0x04B2), }, /* Nokia 5700 XpressMusic */ 1615 { NOKIA_PCSUITE_ACM_INFO(0x0134), }, /* Nokia 6110 Navigator (China) */ 1616 { NOKIA_PCSUITE_ACM_INFO(0x046E), }, /* Nokia 6110 Navigator */ 1617 { NOKIA_PCSUITE_ACM_INFO(0x002f), }, /* Nokia 6120 classic & */ 1618 { NOKIA_PCSUITE_ACM_INFO(0x0088), }, /* Nokia 6121 classic */ 1619 { NOKIA_PCSUITE_ACM_INFO(0x00fc), }, /* Nokia 6124 classic */ 1620 { NOKIA_PCSUITE_ACM_INFO(0x0042), }, /* Nokia E51 */ 1621 { NOKIA_PCSUITE_ACM_INFO(0x00b0), }, /* Nokia E66 */ 1622 { NOKIA_PCSUITE_ACM_INFO(0x00ab), }, /* Nokia E71 */ 1623 { NOKIA_PCSUITE_ACM_INFO(0x0481), }, /* Nokia N76 */ 1624 { NOKIA_PCSUITE_ACM_INFO(0x0007), }, /* Nokia N81 & N81 8GB */ 1625 { NOKIA_PCSUITE_ACM_INFO(0x0071), }, /* Nokia N82 */ 1626 { NOKIA_PCSUITE_ACM_INFO(0x04F0), }, /* Nokia N95 & N95-3 NAM */ 1627 { NOKIA_PCSUITE_ACM_INFO(0x0070), }, /* Nokia N95 8GB */ 1628 { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */ 1629 { NOKIA_PCSUITE_ACM_INFO(0x0099), }, /* Nokia 6210 Navigator, RM-367 */ 1630 { NOKIA_PCSUITE_ACM_INFO(0x0128), }, /* Nokia 6210 Navigator, RM-419 */ 1631 { NOKIA_PCSUITE_ACM_INFO(0x008f), }, /* Nokia 6220 Classic */ 1632 { NOKIA_PCSUITE_ACM_INFO(0x00a0), }, /* Nokia 6650 */ 1633 { NOKIA_PCSUITE_ACM_INFO(0x007b), }, /* Nokia N78 */ 1634 { NOKIA_PCSUITE_ACM_INFO(0x0094), }, /* Nokia N85 */ 1635 { NOKIA_PCSUITE_ACM_INFO(0x003a), }, /* Nokia N96 & N96-3 */ 1636 { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */ 1637 { NOKIA_PCSUITE_ACM_INFO(0x0108), }, /* Nokia 5320 XpressMusic 2G */ 1638 { NOKIA_PCSUITE_ACM_INFO(0x01f5), }, /* Nokia N97, RM-505 */ 1639 { NOKIA_PCSUITE_ACM_INFO(0x02e3), }, /* Nokia 5230, RM-588 */ 1640 { NOKIA_PCSUITE_ACM_INFO(0x0178), }, /* Nokia E63 */ 1641 { NOKIA_PCSUITE_ACM_INFO(0x010e), }, /* Nokia E75 */ 1642 { NOKIA_PCSUITE_ACM_INFO(0x02d9), }, /* Nokia 6760 Slide */ 1643 { NOKIA_PCSUITE_ACM_INFO(0x01d0), }, /* Nokia E52 */ 1644 { NOKIA_PCSUITE_ACM_INFO(0x0223), }, /* Nokia E72 */ 1645 { NOKIA_PCSUITE_ACM_INFO(0x0275), }, /* Nokia X6 */ 1646 { NOKIA_PCSUITE_ACM_INFO(0x026c), }, /* Nokia N97 Mini */ 1647 { NOKIA_PCSUITE_ACM_INFO(0x0154), }, /* Nokia 5800 XpressMusic */ 1648 { NOKIA_PCSUITE_ACM_INFO(0x04ce), }, /* Nokia E90 */ 1649 { NOKIA_PCSUITE_ACM_INFO(0x01d4), }, /* Nokia E55 */ 1650 { NOKIA_PCSUITE_ACM_INFO(0x0302), }, /* Nokia N8 */ 1651 { NOKIA_PCSUITE_ACM_INFO(0x0335), }, /* Nokia E7 */ 1652 { NOKIA_PCSUITE_ACM_INFO(0x03cd), }, /* Nokia C7 */ 1653 { SAMSUNG_PCSUITE_ACM_INFO(0x6651), }, /* Samsung GTi8510 (INNOV8) */ 1654 1655 /* Support for Owen devices */ 1656 { USB_DEVICE(0x03eb, 0x0030), }, /* Owen SI30 */ 1657 1658 /* NOTE: non-Nokia COMM/ACM/0xff is likely MSFT RNDIS... NOT a modem! */ 1659 1660 /* Support Lego NXT using pbLua firmware */ 1661 { USB_DEVICE(0x0694, 0xff00), 1662 .driver_info = NOT_A_MODEM, 1663 }, 1664 1665 /* Support for Droids MuIn LCD */ 1666 { USB_DEVICE(0x04d8, 0x000b), 1667 .driver_info = NO_DATA_INTERFACE, 1668 }, 1669 1670 #if IS_ENABLED(CONFIG_INPUT_IMS_PCU) 1671 { USB_DEVICE(0x04d8, 0x0082), /* Application mode */ 1672 .driver_info = IGNORE_DEVICE, 1673 }, 1674 { USB_DEVICE(0x04d8, 0x0083), /* Bootloader mode */ 1675 .driver_info = IGNORE_DEVICE, 1676 }, 1677 #endif 1678 1679 /* control interfaces without any protocol set */ 1680 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1681 USB_CDC_PROTO_NONE) }, 1682 1683 /* control interfaces with various AT-command sets */ 1684 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1685 USB_CDC_ACM_PROTO_AT_V25TER) }, 1686 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1687 USB_CDC_ACM_PROTO_AT_PCCA101) }, 1688 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1689 USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) }, 1690 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1691 USB_CDC_ACM_PROTO_AT_GSM) }, 1692 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1693 USB_CDC_ACM_PROTO_AT_3G) }, 1694 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1695 USB_CDC_ACM_PROTO_AT_CDMA) }, 1696 1697 { } 1698 }; 1699 1700 MODULE_DEVICE_TABLE(usb, acm_ids); 1701 1702 static struct usb_driver acm_driver = { 1703 .name = "cdc_acm", 1704 .probe = acm_probe, 1705 .disconnect = acm_disconnect, 1706 #ifdef CONFIG_PM 1707 .suspend = acm_suspend, 1708 .resume = acm_resume, 1709 .reset_resume = acm_reset_resume, 1710 #endif 1711 .id_table = acm_ids, 1712 #ifdef CONFIG_PM 1713 .supports_autosuspend = 1, 1714 #endif 1715 .disable_hub_initiated_lpm = 1, 1716 }; 1717 1718 /* 1719 * TTY driver structures. 1720 */ 1721 1722 static const struct tty_operations acm_ops = { 1723 .install = acm_tty_install, 1724 .open = acm_tty_open, 1725 .close = acm_tty_close, 1726 .cleanup = acm_tty_cleanup, 1727 .hangup = acm_tty_hangup, 1728 .write = acm_tty_write, 1729 .write_room = acm_tty_write_room, 1730 .ioctl = acm_tty_ioctl, 1731 .throttle = acm_tty_throttle, 1732 .unthrottle = acm_tty_unthrottle, 1733 .chars_in_buffer = acm_tty_chars_in_buffer, 1734 .break_ctl = acm_tty_break_ctl, 1735 .set_termios = acm_tty_set_termios, 1736 .tiocmget = acm_tty_tiocmget, 1737 .tiocmset = acm_tty_tiocmset, 1738 }; 1739 1740 /* 1741 * Init / exit. 1742 */ 1743 1744 static int __init acm_init(void) 1745 { 1746 int retval; 1747 acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS); 1748 if (!acm_tty_driver) 1749 return -ENOMEM; 1750 acm_tty_driver->driver_name = "acm", 1751 acm_tty_driver->name = "ttyACM", 1752 acm_tty_driver->major = ACM_TTY_MAJOR, 1753 acm_tty_driver->minor_start = 0, 1754 acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL, 1755 acm_tty_driver->subtype = SERIAL_TYPE_NORMAL, 1756 acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; 1757 acm_tty_driver->init_termios = tty_std_termios; 1758 acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | 1759 HUPCL | CLOCAL; 1760 tty_set_operations(acm_tty_driver, &acm_ops); 1761 1762 retval = tty_register_driver(acm_tty_driver); 1763 if (retval) { 1764 put_tty_driver(acm_tty_driver); 1765 return retval; 1766 } 1767 1768 retval = usb_register(&acm_driver); 1769 if (retval) { 1770 tty_unregister_driver(acm_tty_driver); 1771 put_tty_driver(acm_tty_driver); 1772 return retval; 1773 } 1774 1775 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n"); 1776 1777 return 0; 1778 } 1779 1780 static void __exit acm_exit(void) 1781 { 1782 usb_deregister(&acm_driver); 1783 tty_unregister_driver(acm_tty_driver); 1784 put_tty_driver(acm_tty_driver); 1785 } 1786 1787 module_init(acm_init); 1788 module_exit(acm_exit); 1789 1790 MODULE_AUTHOR(DRIVER_AUTHOR); 1791 MODULE_DESCRIPTION(DRIVER_DESC); 1792 MODULE_LICENSE("GPL"); 1793 MODULE_ALIAS_CHARDEV_MAJOR(ACM_TTY_MAJOR); 1794