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