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