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