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