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