Linux 3.12.39
[linux/fpc-iii.git] / drivers / usb / class / cdc-acm.c
blobe2b4ea7fb2b190e924678683ce25cb76bd2b554f
1 /*
2 * cdc-acm.c
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>
12 * USB Abstract Control Model driver for USB modems and ISDN adapters
14 * Sponsored by SuSE
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.
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.
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
31 #undef DEBUG
32 #undef VERBOSE_DEBUG
34 #include <linux/kernel.h>
35 #include <linux/errno.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
38 #include <linux/tty.h>
39 #include <linux/serial.h>
40 #include <linux/tty_driver.h>
41 #include <linux/tty_flip.h>
42 #include <linux/module.h>
43 #include <linux/mutex.h>
44 #include <linux/uaccess.h>
45 #include <linux/usb.h>
46 #include <linux/usb/cdc.h>
47 #include <asm/byteorder.h>
48 #include <asm/unaligned.h>
49 #include <linux/list.h>
51 #include "cdc-acm.h"
54 #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek, Johan Hovold"
55 #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
57 static struct usb_driver acm_driver;
58 static struct tty_driver *acm_tty_driver;
59 static struct acm *acm_table[ACM_TTY_MINORS];
61 static DEFINE_MUTEX(acm_table_lock);
64 * acm_table accessors
68 * Look up an ACM structure by index. If found and not disconnected, increment
69 * its refcount and return it with its mutex held.
71 static struct acm *acm_get_by_index(unsigned index)
73 struct acm *acm;
75 mutex_lock(&acm_table_lock);
76 acm = acm_table[index];
77 if (acm) {
78 mutex_lock(&acm->mutex);
79 if (acm->disconnected) {
80 mutex_unlock(&acm->mutex);
81 acm = NULL;
82 } else {
83 tty_port_get(&acm->port);
84 mutex_unlock(&acm->mutex);
87 mutex_unlock(&acm_table_lock);
88 return acm;
92 * Try to find an available minor number and if found, associate it with 'acm'.
94 static int acm_alloc_minor(struct acm *acm)
96 int minor;
98 mutex_lock(&acm_table_lock);
99 for (minor = 0; minor < ACM_TTY_MINORS; minor++) {
100 if (!acm_table[minor]) {
101 acm_table[minor] = acm;
102 break;
105 mutex_unlock(&acm_table_lock);
107 return minor;
110 /* Release the minor number associated with 'acm'. */
111 static void acm_release_minor(struct acm *acm)
113 mutex_lock(&acm_table_lock);
114 acm_table[acm->minor] = NULL;
115 mutex_unlock(&acm_table_lock);
119 * Functions for ACM control messages.
122 static int acm_ctrl_msg(struct acm *acm, int request, int value,
123 void *buf, int len)
125 int retval;
127 retval = usb_autopm_get_interface(acm->control);
128 if (retval)
129 return retval;
131 retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
132 request, USB_RT_ACM, value,
133 acm->control->altsetting[0].desc.bInterfaceNumber,
134 buf, len, 5000);
136 dev_dbg(&acm->control->dev,
137 "%s - rq 0x%02x, val %#x, len %#x, result %d\n",
138 __func__, request, value, len, retval);
140 usb_autopm_put_interface(acm->control);
142 return retval < 0 ? retval : 0;
145 /* devices aren't required to support these requests.
146 * the cdc acm descriptor tells whether they do...
148 #define acm_set_control(acm, control) \
149 acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0)
150 #define acm_set_line(acm, line) \
151 acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
152 #define acm_send_break(acm, ms) \
153 acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
156 * Write buffer management.
157 * All of these assume proper locks taken by the caller.
160 static int acm_wb_alloc(struct acm *acm)
162 int i, wbn;
163 struct acm_wb *wb;
165 wbn = 0;
166 i = 0;
167 for (;;) {
168 wb = &acm->wb[wbn];
169 if (!wb->use) {
170 wb->use = 1;
171 return wbn;
173 wbn = (wbn + 1) % ACM_NW;
174 if (++i >= ACM_NW)
175 return -1;
179 static int acm_wb_is_avail(struct acm *acm)
181 int i, n;
182 unsigned long flags;
184 n = ACM_NW;
185 spin_lock_irqsave(&acm->write_lock, flags);
186 for (i = 0; i < ACM_NW; i++)
187 n -= acm->wb[i].use;
188 spin_unlock_irqrestore(&acm->write_lock, flags);
189 return n;
193 * Finish write. Caller must hold acm->write_lock
195 static void acm_write_done(struct acm *acm, struct acm_wb *wb)
197 wb->use = 0;
198 acm->transmitting--;
199 usb_autopm_put_interface_async(acm->control);
203 * Poke write.
205 * the caller is responsible for locking
208 static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
210 int rc;
212 acm->transmitting++;
214 wb->urb->transfer_buffer = wb->buf;
215 wb->urb->transfer_dma = wb->dmah;
216 wb->urb->transfer_buffer_length = wb->len;
217 wb->urb->dev = acm->dev;
219 rc = usb_submit_urb(wb->urb, GFP_ATOMIC);
220 if (rc < 0) {
221 dev_err(&acm->data->dev,
222 "%s - usb_submit_urb(write bulk) failed: %d\n",
223 __func__, rc);
224 acm_write_done(acm, wb);
226 return rc;
230 * attributes exported through sysfs
232 static ssize_t show_caps
233 (struct device *dev, struct device_attribute *attr, char *buf)
235 struct usb_interface *intf = to_usb_interface(dev);
236 struct acm *acm = usb_get_intfdata(intf);
238 return sprintf(buf, "%d", acm->ctrl_caps);
240 static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
242 static ssize_t show_country_codes
243 (struct device *dev, struct device_attribute *attr, char *buf)
245 struct usb_interface *intf = to_usb_interface(dev);
246 struct acm *acm = usb_get_intfdata(intf);
248 memcpy(buf, acm->country_codes, acm->country_code_size);
249 return acm->country_code_size;
252 static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
254 static ssize_t show_country_rel_date
255 (struct device *dev, struct device_attribute *attr, char *buf)
257 struct usb_interface *intf = to_usb_interface(dev);
258 struct acm *acm = usb_get_intfdata(intf);
260 return sprintf(buf, "%d", acm->country_rel_date);
263 static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
265 * Interrupt handlers for various ACM device responses
268 /* control interface reports status changes with "interrupt" transfers */
269 static void acm_ctrl_irq(struct urb *urb)
271 struct acm *acm = urb->context;
272 struct usb_cdc_notification *dr = urb->transfer_buffer;
273 unsigned char *data;
274 int newctrl;
275 int retval;
276 int status = urb->status;
278 switch (status) {
279 case 0:
280 /* success */
281 break;
282 case -ECONNRESET:
283 case -ENOENT:
284 case -ESHUTDOWN:
285 /* this urb is terminated, clean up */
286 dev_dbg(&acm->control->dev,
287 "%s - urb shutting down with status: %d\n",
288 __func__, status);
289 return;
290 default:
291 dev_dbg(&acm->control->dev,
292 "%s - nonzero urb status received: %d\n",
293 __func__, status);
294 goto exit;
297 usb_mark_last_busy(acm->dev);
299 data = (unsigned char *)(dr + 1);
300 switch (dr->bNotificationType) {
301 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
302 dev_dbg(&acm->control->dev, "%s - network connection: %d\n",
303 __func__, dr->wValue);
304 break;
306 case USB_CDC_NOTIFY_SERIAL_STATE:
307 newctrl = get_unaligned_le16(data);
309 if (!acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
310 dev_dbg(&acm->control->dev, "%s - calling hangup\n",
311 __func__);
312 tty_port_tty_hangup(&acm->port, false);
315 acm->ctrlin = newctrl;
317 dev_dbg(&acm->control->dev,
318 "%s - input control lines: dcd%c dsr%c break%c "
319 "ring%c framing%c parity%c overrun%c\n",
320 __func__,
321 acm->ctrlin & ACM_CTRL_DCD ? '+' : '-',
322 acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
323 acm->ctrlin & ACM_CTRL_BRK ? '+' : '-',
324 acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
325 acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-',
326 acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
327 acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
328 break;
330 default:
331 dev_dbg(&acm->control->dev,
332 "%s - unknown notification %d received: index %d "
333 "len %d data0 %d data1 %d\n",
334 __func__,
335 dr->bNotificationType, dr->wIndex,
336 dr->wLength, data[0], data[1]);
337 break;
339 exit:
340 retval = usb_submit_urb(urb, GFP_ATOMIC);
341 if (retval)
342 dev_err(&acm->control->dev, "%s - usb_submit_urb failed: %d\n",
343 __func__, retval);
346 static int acm_submit_read_urb(struct acm *acm, int index, gfp_t mem_flags)
348 int res;
350 if (!test_and_clear_bit(index, &acm->read_urbs_free))
351 return 0;
353 dev_vdbg(&acm->data->dev, "%s - urb %d\n", __func__, index);
355 res = usb_submit_urb(acm->read_urbs[index], mem_flags);
356 if (res) {
357 if (res != -EPERM) {
358 dev_err(&acm->data->dev,
359 "%s - usb_submit_urb failed: %d\n",
360 __func__, res);
362 set_bit(index, &acm->read_urbs_free);
363 return res;
366 return 0;
369 static int acm_submit_read_urbs(struct acm *acm, gfp_t mem_flags)
371 int res;
372 int i;
374 for (i = 0; i < acm->rx_buflimit; ++i) {
375 res = acm_submit_read_urb(acm, i, mem_flags);
376 if (res)
377 return res;
380 return 0;
383 static void acm_process_read_urb(struct acm *acm, struct urb *urb)
385 if (!urb->actual_length)
386 return;
388 tty_insert_flip_string(&acm->port, urb->transfer_buffer,
389 urb->actual_length);
390 tty_flip_buffer_push(&acm->port);
393 static void acm_read_bulk_callback(struct urb *urb)
395 struct acm_rb *rb = urb->context;
396 struct acm *acm = rb->instance;
397 unsigned long flags;
399 dev_vdbg(&acm->data->dev, "%s - urb %d, len %d\n", __func__,
400 rb->index, urb->actual_length);
401 set_bit(rb->index, &acm->read_urbs_free);
403 if (!acm->dev) {
404 dev_dbg(&acm->data->dev, "%s - disconnected\n", __func__);
405 return;
407 usb_mark_last_busy(acm->dev);
409 if (urb->status) {
410 dev_dbg(&acm->data->dev, "%s - non-zero urb status: %d\n",
411 __func__, urb->status);
412 return;
414 acm_process_read_urb(acm, urb);
416 /* throttle device if requested by tty */
417 spin_lock_irqsave(&acm->read_lock, flags);
418 acm->throttled = acm->throttle_req;
419 if (!acm->throttled && !acm->susp_count) {
420 spin_unlock_irqrestore(&acm->read_lock, flags);
421 acm_submit_read_urb(acm, rb->index, GFP_ATOMIC);
422 } else {
423 spin_unlock_irqrestore(&acm->read_lock, flags);
427 /* data interface wrote those outgoing bytes */
428 static void acm_write_bulk(struct urb *urb)
430 struct acm_wb *wb = urb->context;
431 struct acm *acm = wb->instance;
432 unsigned long flags;
434 if (urb->status || (urb->actual_length != urb->transfer_buffer_length))
435 dev_vdbg(&acm->data->dev, "%s - len %d/%d, status %d\n",
436 __func__,
437 urb->actual_length,
438 urb->transfer_buffer_length,
439 urb->status);
441 spin_lock_irqsave(&acm->write_lock, flags);
442 acm_write_done(acm, wb);
443 spin_unlock_irqrestore(&acm->write_lock, flags);
444 schedule_work(&acm->work);
447 static void acm_softint(struct work_struct *work)
449 struct acm *acm = container_of(work, struct acm, work);
451 dev_vdbg(&acm->data->dev, "%s\n", __func__);
453 tty_port_tty_wakeup(&acm->port);
457 * TTY handlers
460 static int acm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
462 struct acm *acm;
463 int retval;
465 dev_dbg(tty->dev, "%s\n", __func__);
467 acm = acm_get_by_index(tty->index);
468 if (!acm)
469 return -ENODEV;
471 retval = tty_standard_install(driver, tty);
472 if (retval)
473 goto error_init_termios;
475 tty->driver_data = acm;
477 return 0;
479 error_init_termios:
480 tty_port_put(&acm->port);
481 return retval;
484 static int acm_tty_open(struct tty_struct *tty, struct file *filp)
486 struct acm *acm = tty->driver_data;
488 dev_dbg(tty->dev, "%s\n", __func__);
490 return tty_port_open(&acm->port, tty, filp);
493 static int acm_port_activate(struct tty_port *port, struct tty_struct *tty)
495 struct acm *acm = container_of(port, struct acm, port);
496 int retval = -ENODEV;
497 int i;
499 dev_dbg(&acm->control->dev, "%s\n", __func__);
501 mutex_lock(&acm->mutex);
502 if (acm->disconnected)
503 goto disconnected;
505 retval = usb_autopm_get_interface(acm->control);
506 if (retval)
507 goto error_get_interface;
510 * FIXME: Why do we need this? Allocating 64K of physically contiguous
511 * memory is really nasty...
513 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
514 acm->control->needs_remote_wakeup = 1;
516 acm->ctrlurb->dev = acm->dev;
517 if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
518 dev_err(&acm->control->dev,
519 "%s - usb_submit_urb(ctrl irq) failed\n", __func__);
520 goto error_submit_urb;
523 acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS;
524 if (acm_set_control(acm, acm->ctrlout) < 0 &&
525 (acm->ctrl_caps & USB_CDC_CAP_LINE))
526 goto error_set_control;
528 usb_autopm_put_interface(acm->control);
531 * Unthrottle device in case the TTY was closed while throttled.
533 spin_lock_irq(&acm->read_lock);
534 acm->throttled = 0;
535 acm->throttle_req = 0;
536 spin_unlock_irq(&acm->read_lock);
538 if (acm_submit_read_urbs(acm, GFP_KERNEL))
539 goto error_submit_read_urbs;
541 mutex_unlock(&acm->mutex);
543 return 0;
545 error_submit_read_urbs:
546 for (i = 0; i < acm->rx_buflimit; i++)
547 usb_kill_urb(acm->read_urbs[i]);
548 acm->ctrlout = 0;
549 acm_set_control(acm, acm->ctrlout);
550 error_set_control:
551 usb_kill_urb(acm->ctrlurb);
552 error_submit_urb:
553 usb_autopm_put_interface(acm->control);
554 error_get_interface:
555 disconnected:
556 mutex_unlock(&acm->mutex);
557 return retval;
560 static void acm_port_destruct(struct tty_port *port)
562 struct acm *acm = container_of(port, struct acm, port);
564 dev_dbg(&acm->control->dev, "%s\n", __func__);
566 acm_release_minor(acm);
567 usb_put_intf(acm->control);
568 kfree(acm->country_codes);
569 kfree(acm);
572 static void acm_port_shutdown(struct tty_port *port)
574 struct acm *acm = container_of(port, struct acm, port);
575 struct urb *urb;
576 struct acm_wb *wb;
577 int i;
578 int pm_err;
580 dev_dbg(&acm->control->dev, "%s\n", __func__);
582 mutex_lock(&acm->mutex);
583 if (!acm->disconnected) {
584 pm_err = usb_autopm_get_interface(acm->control);
585 acm_set_control(acm, acm->ctrlout = 0);
587 for (;;) {
588 urb = usb_get_from_anchor(&acm->delayed);
589 if (!urb)
590 break;
591 wb = urb->context;
592 wb->use = 0;
593 usb_autopm_put_interface_async(acm->control);
596 usb_kill_urb(acm->ctrlurb);
597 for (i = 0; i < ACM_NW; i++)
598 usb_kill_urb(acm->wb[i].urb);
599 for (i = 0; i < acm->rx_buflimit; i++)
600 usb_kill_urb(acm->read_urbs[i]);
601 acm->control->needs_remote_wakeup = 0;
602 if (!pm_err)
603 usb_autopm_put_interface(acm->control);
605 mutex_unlock(&acm->mutex);
608 static void acm_tty_cleanup(struct tty_struct *tty)
610 struct acm *acm = tty->driver_data;
611 dev_dbg(&acm->control->dev, "%s\n", __func__);
612 tty_port_put(&acm->port);
615 static void acm_tty_hangup(struct tty_struct *tty)
617 struct acm *acm = tty->driver_data;
618 dev_dbg(&acm->control->dev, "%s\n", __func__);
619 tty_port_hangup(&acm->port);
622 static void acm_tty_close(struct tty_struct *tty, struct file *filp)
624 struct acm *acm = tty->driver_data;
625 dev_dbg(&acm->control->dev, "%s\n", __func__);
626 tty_port_close(&acm->port, tty, filp);
629 static int acm_tty_write(struct tty_struct *tty,
630 const unsigned char *buf, int count)
632 struct acm *acm = tty->driver_data;
633 int stat;
634 unsigned long flags;
635 int wbn;
636 struct acm_wb *wb;
638 if (!count)
639 return 0;
641 dev_vdbg(&acm->data->dev, "%s - count %d\n", __func__, count);
643 spin_lock_irqsave(&acm->write_lock, flags);
644 wbn = acm_wb_alloc(acm);
645 if (wbn < 0) {
646 spin_unlock_irqrestore(&acm->write_lock, flags);
647 return 0;
649 wb = &acm->wb[wbn];
651 if (!acm->dev) {
652 wb->use = 0;
653 spin_unlock_irqrestore(&acm->write_lock, flags);
654 return -ENODEV;
657 count = (count > acm->writesize) ? acm->writesize : count;
658 dev_vdbg(&acm->data->dev, "%s - write %d\n", __func__, count);
659 memcpy(wb->buf, buf, count);
660 wb->len = count;
662 stat = usb_autopm_get_interface_async(acm->control);
663 if (stat) {
664 wb->use = 0;
665 spin_unlock_irqrestore(&acm->write_lock, flags);
666 return stat;
669 if (acm->susp_count) {
670 usb_anchor_urb(wb->urb, &acm->delayed);
671 spin_unlock_irqrestore(&acm->write_lock, flags);
672 return count;
674 usb_mark_last_busy(acm->dev);
676 stat = acm_start_wb(acm, wb);
677 spin_unlock_irqrestore(&acm->write_lock, flags);
679 if (stat < 0)
680 return stat;
681 return count;
684 static int acm_tty_write_room(struct tty_struct *tty)
686 struct acm *acm = tty->driver_data;
688 * Do not let the line discipline to know that we have a reserve,
689 * or it might get too enthusiastic.
691 return acm_wb_is_avail(acm) ? acm->writesize : 0;
694 static int acm_tty_chars_in_buffer(struct tty_struct *tty)
696 struct acm *acm = tty->driver_data;
698 * if the device was unplugged then any remaining characters fell out
699 * of the connector ;)
701 if (acm->disconnected)
702 return 0;
704 * This is inaccurate (overcounts), but it works.
706 return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
709 static void acm_tty_throttle(struct tty_struct *tty)
711 struct acm *acm = tty->driver_data;
713 spin_lock_irq(&acm->read_lock);
714 acm->throttle_req = 1;
715 spin_unlock_irq(&acm->read_lock);
718 static void acm_tty_unthrottle(struct tty_struct *tty)
720 struct acm *acm = tty->driver_data;
721 unsigned int was_throttled;
723 spin_lock_irq(&acm->read_lock);
724 was_throttled = acm->throttled;
725 acm->throttled = 0;
726 acm->throttle_req = 0;
727 spin_unlock_irq(&acm->read_lock);
729 if (was_throttled)
730 acm_submit_read_urbs(acm, GFP_KERNEL);
733 static int acm_tty_break_ctl(struct tty_struct *tty, int state)
735 struct acm *acm = tty->driver_data;
736 int retval;
738 retval = acm_send_break(acm, state ? 0xffff : 0);
739 if (retval < 0)
740 dev_dbg(&acm->control->dev, "%s - send break failed\n",
741 __func__);
742 return retval;
745 static int acm_tty_tiocmget(struct tty_struct *tty)
747 struct acm *acm = tty->driver_data;
749 return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
750 (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
751 (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
752 (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
753 (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
754 TIOCM_CTS;
757 static int acm_tty_tiocmset(struct tty_struct *tty,
758 unsigned int set, unsigned int clear)
760 struct acm *acm = tty->driver_data;
761 unsigned int newctrl;
763 newctrl = acm->ctrlout;
764 set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
765 (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
766 clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
767 (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
769 newctrl = (newctrl & ~clear) | set;
771 if (acm->ctrlout == newctrl)
772 return 0;
773 return acm_set_control(acm, acm->ctrlout = newctrl);
776 static int get_serial_info(struct acm *acm, struct serial_struct __user *info)
778 struct serial_struct tmp;
780 if (!info)
781 return -EINVAL;
783 memset(&tmp, 0, sizeof(tmp));
784 tmp.flags = ASYNC_LOW_LATENCY;
785 tmp.xmit_fifo_size = acm->writesize;
786 tmp.baud_base = le32_to_cpu(acm->line.dwDTERate);
787 tmp.close_delay = acm->port.close_delay / 10;
788 tmp.closing_wait = acm->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
789 ASYNC_CLOSING_WAIT_NONE :
790 acm->port.closing_wait / 10;
792 if (copy_to_user(info, &tmp, sizeof(tmp)))
793 return -EFAULT;
794 else
795 return 0;
798 static int set_serial_info(struct acm *acm,
799 struct serial_struct __user *newinfo)
801 struct serial_struct new_serial;
802 unsigned int closing_wait, close_delay;
803 int retval = 0;
805 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
806 return -EFAULT;
808 close_delay = new_serial.close_delay * 10;
809 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
810 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
812 mutex_lock(&acm->port.mutex);
814 if (!capable(CAP_SYS_ADMIN)) {
815 if ((close_delay != acm->port.close_delay) ||
816 (closing_wait != acm->port.closing_wait))
817 retval = -EPERM;
818 else
819 retval = -EOPNOTSUPP;
820 } else {
821 acm->port.close_delay = close_delay;
822 acm->port.closing_wait = closing_wait;
825 mutex_unlock(&acm->port.mutex);
826 return retval;
829 static int acm_tty_ioctl(struct tty_struct *tty,
830 unsigned int cmd, unsigned long arg)
832 struct acm *acm = tty->driver_data;
833 int rv = -ENOIOCTLCMD;
835 switch (cmd) {
836 case TIOCGSERIAL: /* gets serial port data */
837 rv = get_serial_info(acm, (struct serial_struct __user *) arg);
838 break;
839 case TIOCSSERIAL:
840 rv = set_serial_info(acm, (struct serial_struct __user *) arg);
841 break;
844 return rv;
847 static void acm_tty_set_termios(struct tty_struct *tty,
848 struct ktermios *termios_old)
850 struct acm *acm = tty->driver_data;
851 struct ktermios *termios = &tty->termios;
852 struct usb_cdc_line_coding newline;
853 int newctrl = acm->ctrlout;
855 newline.dwDTERate = cpu_to_le32(tty_get_baud_rate(tty));
856 newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
857 newline.bParityType = termios->c_cflag & PARENB ?
858 (termios->c_cflag & PARODD ? 1 : 2) +
859 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
860 switch (termios->c_cflag & CSIZE) {
861 case CS5:
862 newline.bDataBits = 5;
863 break;
864 case CS6:
865 newline.bDataBits = 6;
866 break;
867 case CS7:
868 newline.bDataBits = 7;
869 break;
870 case CS8:
871 default:
872 newline.bDataBits = 8;
873 break;
875 /* FIXME: Needs to clear unsupported bits in the termios */
876 acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
878 if (C_BAUD(tty) == B0) {
879 newline.dwDTERate = acm->line.dwDTERate;
880 newctrl &= ~ACM_CTRL_DTR;
881 } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) {
882 newctrl |= ACM_CTRL_DTR;
885 if (newctrl != acm->ctrlout)
886 acm_set_control(acm, acm->ctrlout = newctrl);
888 if (memcmp(&acm->line, &newline, sizeof newline)) {
889 memcpy(&acm->line, &newline, sizeof newline);
890 dev_dbg(&acm->control->dev, "%s - set line: %d %d %d %d\n",
891 __func__,
892 le32_to_cpu(newline.dwDTERate),
893 newline.bCharFormat, newline.bParityType,
894 newline.bDataBits);
895 acm_set_line(acm, &acm->line);
899 static const struct tty_port_operations acm_port_ops = {
900 .shutdown = acm_port_shutdown,
901 .activate = acm_port_activate,
902 .destruct = acm_port_destruct,
906 * USB probe and disconnect routines.
909 /* Little helpers: write/read buffers free */
910 static void acm_write_buffers_free(struct acm *acm)
912 int i;
913 struct acm_wb *wb;
914 struct usb_device *usb_dev = interface_to_usbdev(acm->control);
916 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++)
917 usb_free_coherent(usb_dev, acm->writesize, wb->buf, wb->dmah);
920 static void acm_read_buffers_free(struct acm *acm)
922 struct usb_device *usb_dev = interface_to_usbdev(acm->control);
923 int i;
925 for (i = 0; i < acm->rx_buflimit; i++)
926 usb_free_coherent(usb_dev, acm->readsize,
927 acm->read_buffers[i].base, acm->read_buffers[i].dma);
930 /* Little helper: write buffers allocate */
931 static int acm_write_buffers_alloc(struct acm *acm)
933 int i;
934 struct acm_wb *wb;
936 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
937 wb->buf = usb_alloc_coherent(acm->dev, acm->writesize, GFP_KERNEL,
938 &wb->dmah);
939 if (!wb->buf) {
940 while (i != 0) {
941 --i;
942 --wb;
943 usb_free_coherent(acm->dev, acm->writesize,
944 wb->buf, wb->dmah);
946 return -ENOMEM;
949 return 0;
952 static int acm_probe(struct usb_interface *intf,
953 const struct usb_device_id *id)
955 struct usb_cdc_union_desc *union_header = NULL;
956 struct usb_cdc_country_functional_desc *cfd = NULL;
957 unsigned char *buffer = intf->altsetting->extra;
958 int buflen = intf->altsetting->extralen;
959 struct usb_interface *control_interface;
960 struct usb_interface *data_interface;
961 struct usb_endpoint_descriptor *epctrl = NULL;
962 struct usb_endpoint_descriptor *epread = NULL;
963 struct usb_endpoint_descriptor *epwrite = NULL;
964 struct usb_device *usb_dev = interface_to_usbdev(intf);
965 struct acm *acm;
966 int minor;
967 int ctrlsize, readsize;
968 u8 *buf;
969 u8 ac_management_function = 0;
970 u8 call_management_function = 0;
971 int call_interface_num = -1;
972 int data_interface_num = -1;
973 unsigned long quirks;
974 int num_rx_buf;
975 int i;
976 int combined_interfaces = 0;
977 struct device *tty_dev;
978 int rv = -ENOMEM;
980 /* normal quirks */
981 quirks = (unsigned long)id->driver_info;
983 if (quirks == IGNORE_DEVICE)
984 return -ENODEV;
986 num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
988 /* handle quirks deadly to normal probing*/
989 if (quirks == NO_UNION_NORMAL) {
990 data_interface = usb_ifnum_to_if(usb_dev, 1);
991 control_interface = usb_ifnum_to_if(usb_dev, 0);
992 goto skip_normal_probe;
995 /* normal probing*/
996 if (!buffer) {
997 dev_err(&intf->dev, "Weird descriptor references\n");
998 return -EINVAL;
1001 if (!buflen) {
1002 if (intf->cur_altsetting->endpoint &&
1003 intf->cur_altsetting->endpoint->extralen &&
1004 intf->cur_altsetting->endpoint->extra) {
1005 dev_dbg(&intf->dev,
1006 "Seeking extra descriptors on endpoint\n");
1007 buflen = intf->cur_altsetting->endpoint->extralen;
1008 buffer = intf->cur_altsetting->endpoint->extra;
1009 } else {
1010 dev_err(&intf->dev,
1011 "Zero length descriptor references\n");
1012 return -EINVAL;
1016 while (buflen > 0) {
1017 if (buffer[1] != USB_DT_CS_INTERFACE) {
1018 dev_err(&intf->dev, "skipping garbage\n");
1019 goto next_desc;
1022 switch (buffer[2]) {
1023 case USB_CDC_UNION_TYPE: /* we've found it */
1024 if (union_header) {
1025 dev_err(&intf->dev, "More than one "
1026 "union descriptor, skipping ...\n");
1027 goto next_desc;
1029 union_header = (struct usb_cdc_union_desc *)buffer;
1030 break;
1031 case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
1032 cfd = (struct usb_cdc_country_functional_desc *)buffer;
1033 break;
1034 case USB_CDC_HEADER_TYPE: /* maybe check version */
1035 break; /* for now we ignore it */
1036 case USB_CDC_ACM_TYPE:
1037 ac_management_function = buffer[3];
1038 break;
1039 case USB_CDC_CALL_MANAGEMENT_TYPE:
1040 call_management_function = buffer[3];
1041 call_interface_num = buffer[4];
1042 if ((quirks & NOT_A_MODEM) == 0 && (call_management_function & 3) != 3)
1043 dev_err(&intf->dev, "This device cannot do calls on its own. It is not a modem.\n");
1044 break;
1045 default:
1046 /* there are LOTS more CDC descriptors that
1047 * could legitimately be found here.
1049 dev_dbg(&intf->dev, "Ignoring descriptor: "
1050 "type %02x, length %d\n",
1051 buffer[2], buffer[0]);
1052 break;
1054 next_desc:
1055 buflen -= buffer[0];
1056 buffer += buffer[0];
1059 if (!union_header) {
1060 if (call_interface_num > 0) {
1061 dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n");
1062 /* quirks for Droids MuIn LCD */
1063 if (quirks & NO_DATA_INTERFACE)
1064 data_interface = usb_ifnum_to_if(usb_dev, 0);
1065 else
1066 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
1067 control_interface = intf;
1068 } else {
1069 if (intf->cur_altsetting->desc.bNumEndpoints != 3) {
1070 dev_dbg(&intf->dev,"No union descriptor, giving up\n");
1071 return -ENODEV;
1072 } else {
1073 dev_warn(&intf->dev,"No union descriptor, testing for castrated device\n");
1074 combined_interfaces = 1;
1075 control_interface = data_interface = intf;
1076 goto look_for_collapsed_interface;
1079 } else {
1080 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
1081 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
1084 if (!control_interface || !data_interface) {
1085 dev_dbg(&intf->dev, "no interfaces\n");
1086 return -ENODEV;
1089 if (data_interface_num != call_interface_num)
1090 dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n");
1092 if (control_interface == data_interface) {
1093 /* some broken devices designed for windows work this way */
1094 dev_warn(&intf->dev,"Control and data interfaces are not separated!\n");
1095 combined_interfaces = 1;
1096 /* a popular other OS doesn't use it */
1097 quirks |= NO_CAP_LINE;
1098 if (data_interface->cur_altsetting->desc.bNumEndpoints != 3) {
1099 dev_err(&intf->dev, "This needs exactly 3 endpoints\n");
1100 return -EINVAL;
1102 look_for_collapsed_interface:
1103 for (i = 0; i < 3; i++) {
1104 struct usb_endpoint_descriptor *ep;
1105 ep = &data_interface->cur_altsetting->endpoint[i].desc;
1107 if (usb_endpoint_is_int_in(ep))
1108 epctrl = ep;
1109 else if (usb_endpoint_is_bulk_out(ep))
1110 epwrite = ep;
1111 else if (usb_endpoint_is_bulk_in(ep))
1112 epread = ep;
1113 else
1114 return -EINVAL;
1116 if (!epctrl || !epread || !epwrite)
1117 return -ENODEV;
1118 else
1119 goto made_compressed_probe;
1122 skip_normal_probe:
1124 /*workaround for switched interfaces */
1125 if (data_interface->cur_altsetting->desc.bInterfaceClass
1126 != CDC_DATA_INTERFACE_TYPE) {
1127 if (control_interface->cur_altsetting->desc.bInterfaceClass
1128 == CDC_DATA_INTERFACE_TYPE) {
1129 struct usb_interface *t;
1130 dev_dbg(&intf->dev,
1131 "Your device has switched interfaces.\n");
1132 t = control_interface;
1133 control_interface = data_interface;
1134 data_interface = t;
1135 } else {
1136 return -EINVAL;
1140 /* Accept probe requests only for the control interface */
1141 if (!combined_interfaces && intf != control_interface)
1142 return -ENODEV;
1144 if (!combined_interfaces && usb_interface_claimed(data_interface)) {
1145 /* valid in this context */
1146 dev_dbg(&intf->dev, "The data interface isn't available\n");
1147 return -EBUSY;
1151 if (data_interface->cur_altsetting->desc.bNumEndpoints < 2 ||
1152 control_interface->cur_altsetting->desc.bNumEndpoints == 0)
1153 return -EINVAL;
1155 epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
1156 epread = &data_interface->cur_altsetting->endpoint[0].desc;
1157 epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
1160 /* workaround for switched endpoints */
1161 if (!usb_endpoint_dir_in(epread)) {
1162 /* descriptors are swapped */
1163 struct usb_endpoint_descriptor *t;
1164 dev_dbg(&intf->dev,
1165 "The data interface has switched endpoints\n");
1166 t = epread;
1167 epread = epwrite;
1168 epwrite = t;
1170 made_compressed_probe:
1171 dev_dbg(&intf->dev, "interfaces are valid\n");
1173 acm = kzalloc(sizeof(struct acm), GFP_KERNEL);
1174 if (acm == NULL) {
1175 dev_err(&intf->dev, "out of memory (acm kzalloc)\n");
1176 goto alloc_fail;
1179 minor = acm_alloc_minor(acm);
1180 if (minor == ACM_TTY_MINORS) {
1181 dev_err(&intf->dev, "no more free acm devices\n");
1182 kfree(acm);
1183 return -ENODEV;
1186 ctrlsize = usb_endpoint_maxp(epctrl);
1187 readsize = usb_endpoint_maxp(epread) *
1188 (quirks == SINGLE_RX_URB ? 1 : 2);
1189 acm->combined_interfaces = combined_interfaces;
1190 acm->writesize = usb_endpoint_maxp(epwrite) * 20;
1191 acm->control = control_interface;
1192 acm->data = data_interface;
1193 acm->minor = minor;
1194 acm->dev = usb_dev;
1195 acm->ctrl_caps = ac_management_function;
1196 if (quirks & NO_CAP_LINE)
1197 acm->ctrl_caps &= ~USB_CDC_CAP_LINE;
1198 acm->ctrlsize = ctrlsize;
1199 acm->readsize = readsize;
1200 acm->rx_buflimit = num_rx_buf;
1201 INIT_WORK(&acm->work, acm_softint);
1202 spin_lock_init(&acm->write_lock);
1203 spin_lock_init(&acm->read_lock);
1204 mutex_init(&acm->mutex);
1205 acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
1206 acm->is_int_ep = usb_endpoint_xfer_int(epread);
1207 if (acm->is_int_ep)
1208 acm->bInterval = epread->bInterval;
1209 tty_port_init(&acm->port);
1210 acm->port.ops = &acm_port_ops;
1211 init_usb_anchor(&acm->delayed);
1213 buf = usb_alloc_coherent(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
1214 if (!buf) {
1215 dev_err(&intf->dev, "out of memory (ctrl buffer alloc)\n");
1216 goto alloc_fail2;
1218 acm->ctrl_buffer = buf;
1220 if (acm_write_buffers_alloc(acm) < 0) {
1221 dev_err(&intf->dev, "out of memory (write buffer alloc)\n");
1222 goto alloc_fail4;
1225 acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
1226 if (!acm->ctrlurb) {
1227 dev_err(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
1228 goto alloc_fail5;
1230 for (i = 0; i < num_rx_buf; i++) {
1231 struct acm_rb *rb = &(acm->read_buffers[i]);
1232 struct urb *urb;
1234 rb->base = usb_alloc_coherent(acm->dev, readsize, GFP_KERNEL,
1235 &rb->dma);
1236 if (!rb->base) {
1237 dev_err(&intf->dev, "out of memory "
1238 "(read bufs usb_alloc_coherent)\n");
1239 goto alloc_fail6;
1241 rb->index = i;
1242 rb->instance = acm;
1244 urb = usb_alloc_urb(0, GFP_KERNEL);
1245 if (!urb) {
1246 dev_err(&intf->dev,
1247 "out of memory (read urbs usb_alloc_urb)\n");
1248 goto alloc_fail6;
1250 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1251 urb->transfer_dma = rb->dma;
1252 if (acm->is_int_ep) {
1253 usb_fill_int_urb(urb, acm->dev,
1254 acm->rx_endpoint,
1255 rb->base,
1256 acm->readsize,
1257 acm_read_bulk_callback, rb,
1258 acm->bInterval);
1259 } else {
1260 usb_fill_bulk_urb(urb, acm->dev,
1261 acm->rx_endpoint,
1262 rb->base,
1263 acm->readsize,
1264 acm_read_bulk_callback, rb);
1267 acm->read_urbs[i] = urb;
1268 __set_bit(i, &acm->read_urbs_free);
1270 for (i = 0; i < ACM_NW; i++) {
1271 struct acm_wb *snd = &(acm->wb[i]);
1273 snd->urb = usb_alloc_urb(0, GFP_KERNEL);
1274 if (snd->urb == NULL) {
1275 dev_err(&intf->dev,
1276 "out of memory (write urbs usb_alloc_urb)\n");
1277 goto alloc_fail7;
1280 if (usb_endpoint_xfer_int(epwrite))
1281 usb_fill_int_urb(snd->urb, usb_dev,
1282 usb_sndintpipe(usb_dev, epwrite->bEndpointAddress),
1283 NULL, acm->writesize, acm_write_bulk, snd, epwrite->bInterval);
1284 else
1285 usb_fill_bulk_urb(snd->urb, usb_dev,
1286 usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
1287 NULL, acm->writesize, acm_write_bulk, snd);
1288 snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1289 snd->instance = acm;
1292 usb_set_intfdata(intf, acm);
1294 i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
1295 if (i < 0)
1296 goto alloc_fail7;
1298 if (cfd) { /* export the country data */
1299 acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
1300 if (!acm->country_codes)
1301 goto skip_countries;
1302 acm->country_code_size = cfd->bLength - 4;
1303 memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0,
1304 cfd->bLength - 4);
1305 acm->country_rel_date = cfd->iCountryCodeRelDate;
1307 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
1308 if (i < 0) {
1309 kfree(acm->country_codes);
1310 acm->country_codes = NULL;
1311 acm->country_code_size = 0;
1312 goto skip_countries;
1315 i = device_create_file(&intf->dev,
1316 &dev_attr_iCountryCodeRelDate);
1317 if (i < 0) {
1318 device_remove_file(&intf->dev, &dev_attr_wCountryCodes);
1319 kfree(acm->country_codes);
1320 acm->country_codes = NULL;
1321 acm->country_code_size = 0;
1322 goto skip_countries;
1326 skip_countries:
1327 usb_fill_int_urb(acm->ctrlurb, usb_dev,
1328 usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
1329 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm,
1330 /* works around buggy devices */
1331 epctrl->bInterval ? epctrl->bInterval : 16);
1332 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1333 acm->ctrlurb->transfer_dma = acm->ctrl_dma;
1335 dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
1337 acm_set_control(acm, acm->ctrlout);
1339 acm->line.dwDTERate = cpu_to_le32(9600);
1340 acm->line.bDataBits = 8;
1341 acm_set_line(acm, &acm->line);
1343 usb_driver_claim_interface(&acm_driver, data_interface, acm);
1344 usb_set_intfdata(data_interface, acm);
1346 usb_get_intf(control_interface);
1347 tty_dev = tty_port_register_device(&acm->port, acm_tty_driver, minor,
1348 &control_interface->dev);
1349 if (IS_ERR(tty_dev)) {
1350 rv = PTR_ERR(tty_dev);
1351 goto alloc_fail8;
1354 return 0;
1355 alloc_fail8:
1356 if (acm->country_codes) {
1357 device_remove_file(&acm->control->dev,
1358 &dev_attr_wCountryCodes);
1359 device_remove_file(&acm->control->dev,
1360 &dev_attr_iCountryCodeRelDate);
1361 kfree(acm->country_codes);
1363 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
1364 alloc_fail7:
1365 usb_set_intfdata(intf, NULL);
1366 for (i = 0; i < ACM_NW; i++)
1367 usb_free_urb(acm->wb[i].urb);
1368 alloc_fail6:
1369 for (i = 0; i < num_rx_buf; i++)
1370 usb_free_urb(acm->read_urbs[i]);
1371 acm_read_buffers_free(acm);
1372 usb_free_urb(acm->ctrlurb);
1373 alloc_fail5:
1374 acm_write_buffers_free(acm);
1375 alloc_fail4:
1376 usb_free_coherent(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1377 alloc_fail2:
1378 acm_release_minor(acm);
1379 kfree(acm);
1380 alloc_fail:
1381 return rv;
1384 static void stop_data_traffic(struct acm *acm)
1386 int i;
1388 dev_dbg(&acm->control->dev, "%s\n", __func__);
1390 usb_kill_urb(acm->ctrlurb);
1391 for (i = 0; i < ACM_NW; i++)
1392 usb_kill_urb(acm->wb[i].urb);
1393 for (i = 0; i < acm->rx_buflimit; i++)
1394 usb_kill_urb(acm->read_urbs[i]);
1396 cancel_work_sync(&acm->work);
1399 static void acm_disconnect(struct usb_interface *intf)
1401 struct acm *acm = usb_get_intfdata(intf);
1402 struct usb_device *usb_dev = interface_to_usbdev(intf);
1403 struct tty_struct *tty;
1404 int i;
1406 dev_dbg(&intf->dev, "%s\n", __func__);
1408 /* sibling interface is already cleaning up */
1409 if (!acm)
1410 return;
1412 mutex_lock(&acm->mutex);
1413 acm->disconnected = true;
1414 if (acm->country_codes) {
1415 device_remove_file(&acm->control->dev,
1416 &dev_attr_wCountryCodes);
1417 device_remove_file(&acm->control->dev,
1418 &dev_attr_iCountryCodeRelDate);
1420 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
1421 usb_set_intfdata(acm->control, NULL);
1422 usb_set_intfdata(acm->data, NULL);
1423 mutex_unlock(&acm->mutex);
1425 tty = tty_port_tty_get(&acm->port);
1426 if (tty) {
1427 tty_vhangup(tty);
1428 tty_kref_put(tty);
1431 stop_data_traffic(acm);
1433 tty_unregister_device(acm_tty_driver, acm->minor);
1435 usb_free_urb(acm->ctrlurb);
1436 for (i = 0; i < ACM_NW; i++)
1437 usb_free_urb(acm->wb[i].urb);
1438 for (i = 0; i < acm->rx_buflimit; i++)
1439 usb_free_urb(acm->read_urbs[i]);
1440 acm_write_buffers_free(acm);
1441 usb_free_coherent(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1442 acm_read_buffers_free(acm);
1444 if (!acm->combined_interfaces)
1445 usb_driver_release_interface(&acm_driver, intf == acm->control ?
1446 acm->data : acm->control);
1448 tty_port_put(&acm->port);
1451 #ifdef CONFIG_PM
1452 static int acm_suspend(struct usb_interface *intf, pm_message_t message)
1454 struct acm *acm = usb_get_intfdata(intf);
1455 int cnt;
1457 spin_lock_irq(&acm->read_lock);
1458 spin_lock(&acm->write_lock);
1459 if (PMSG_IS_AUTO(message)) {
1460 if (acm->transmitting) {
1461 spin_unlock(&acm->write_lock);
1462 spin_unlock_irq(&acm->read_lock);
1463 return -EBUSY;
1466 cnt = acm->susp_count++;
1467 spin_unlock(&acm->write_lock);
1468 spin_unlock_irq(&acm->read_lock);
1470 if (cnt)
1471 return 0;
1473 stop_data_traffic(acm);
1475 return 0;
1478 static int acm_resume(struct usb_interface *intf)
1480 struct acm *acm = usb_get_intfdata(intf);
1481 struct urb *urb;
1482 int rv = 0;
1484 spin_lock_irq(&acm->read_lock);
1485 spin_lock(&acm->write_lock);
1487 if (--acm->susp_count)
1488 goto out;
1490 if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags)) {
1491 rv = usb_submit_urb(acm->ctrlurb, GFP_ATOMIC);
1493 for (;;) {
1494 urb = usb_get_from_anchor(&acm->delayed);
1495 if (!urb)
1496 break;
1498 acm_start_wb(acm, urb->context);
1502 * delayed error checking because we must
1503 * do the write path at all cost
1505 if (rv < 0)
1506 goto out;
1508 rv = acm_submit_read_urbs(acm, GFP_ATOMIC);
1510 out:
1511 spin_unlock(&acm->write_lock);
1512 spin_unlock_irq(&acm->read_lock);
1514 return rv;
1517 static int acm_reset_resume(struct usb_interface *intf)
1519 struct acm *acm = usb_get_intfdata(intf);
1521 if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags))
1522 tty_port_tty_hangup(&acm->port, false);
1524 return acm_resume(intf);
1527 #endif /* CONFIG_PM */
1529 #define NOKIA_PCSUITE_ACM_INFO(x) \
1530 USB_DEVICE_AND_INTERFACE_INFO(0x0421, x, \
1531 USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
1532 USB_CDC_ACM_PROTO_VENDOR)
1534 #define SAMSUNG_PCSUITE_ACM_INFO(x) \
1535 USB_DEVICE_AND_INTERFACE_INFO(0x04e7, x, \
1536 USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
1537 USB_CDC_ACM_PROTO_VENDOR)
1540 * USB driver structure.
1543 static const struct usb_device_id acm_ids[] = {
1544 /* quirky and broken devices */
1545 { USB_DEVICE(0x076d, 0x0006), /* Denso Cradle CU-321 */
1546 .driver_info = NO_UNION_NORMAL, },/* has no union descriptor */
1547 { USB_DEVICE(0x17ef, 0x7000), /* Lenovo USB modem */
1548 .driver_info = NO_UNION_NORMAL, },/* has no union descriptor */
1549 { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
1550 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1552 { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
1553 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1555 { USB_DEVICE(0x0e8d, 0x3329), /* MediaTek Inc GPS */
1556 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1558 { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
1559 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1561 { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
1562 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1564 { USB_DEVICE(0x0ace, 0x1602), /* ZyDAS 56K USB MODEM */
1565 .driver_info = SINGLE_RX_URB,
1567 { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
1568 .driver_info = SINGLE_RX_URB, /* firmware bug */
1570 { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
1571 .driver_info = SINGLE_RX_URB, /* firmware bug */
1573 { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
1574 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1576 { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
1577 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1579 { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */
1580 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1582 { USB_DEVICE(0x0572, 0x1324), /* Conexant USB MODEM RD02-D400 */
1583 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1585 { USB_DEVICE(0x0572, 0x1328), /* Shiro / Aztech USB MODEM UM-3100 */
1586 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1588 { USB_DEVICE(0x2184, 0x001c) }, /* GW Instek AFG-2225 */
1589 { USB_DEVICE(0x22b8, 0x6425), /* Motorola MOTOMAGX phones */
1591 /* Motorola H24 HSPA module: */
1592 { USB_DEVICE(0x22b8, 0x2d91) }, /* modem */
1593 { USB_DEVICE(0x22b8, 0x2d92), /* modem + diagnostics */
1594 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1596 { USB_DEVICE(0x22b8, 0x2d93), /* modem + AT port */
1597 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1599 { USB_DEVICE(0x22b8, 0x2d95), /* modem + AT port + diagnostics */
1600 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1602 { USB_DEVICE(0x22b8, 0x2d96), /* modem + NMEA */
1603 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1605 { USB_DEVICE(0x22b8, 0x2d97), /* modem + diagnostics + NMEA */
1606 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1608 { USB_DEVICE(0x22b8, 0x2d99), /* modem + AT port + NMEA */
1609 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1611 { USB_DEVICE(0x22b8, 0x2d9a), /* modem + AT port + diagnostics + NMEA */
1612 .driver_info = NO_UNION_NORMAL, /* handle only modem interface */
1615 { USB_DEVICE(0x0572, 0x1329), /* Hummingbird huc56s (Conexant) */
1616 .driver_info = NO_UNION_NORMAL, /* union descriptor misplaced on
1617 data interface instead of
1618 communications interface.
1619 Maybe we should define a new
1620 quirk for this. */
1622 { USB_DEVICE(0x0572, 0x1340), /* Conexant CX93010-2x UCMxx */
1623 .driver_info = NO_UNION_NORMAL,
1625 { USB_DEVICE(0x05f9, 0x4002), /* PSC Scanning, Magellan 800i */
1626 .driver_info = NO_UNION_NORMAL,
1628 { USB_DEVICE(0x1bbb, 0x0003), /* Alcatel OT-I650 */
1629 .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
1631 { USB_DEVICE(0x1576, 0x03b1), /* Maretron USB100 */
1632 .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
1635 /* Nokia S60 phones expose two ACM channels. The first is
1636 * a modem and is picked up by the standard AT-command
1637 * information below. The second is 'vendor-specific' but
1638 * is treated as a serial device at the S60 end, so we want
1639 * to expose it on Linux too. */
1640 { NOKIA_PCSUITE_ACM_INFO(0x042D), }, /* Nokia 3250 */
1641 { NOKIA_PCSUITE_ACM_INFO(0x04D8), }, /* Nokia 5500 Sport */
1642 { NOKIA_PCSUITE_ACM_INFO(0x04C9), }, /* Nokia E50 */
1643 { NOKIA_PCSUITE_ACM_INFO(0x0419), }, /* Nokia E60 */
1644 { NOKIA_PCSUITE_ACM_INFO(0x044D), }, /* Nokia E61 */
1645 { NOKIA_PCSUITE_ACM_INFO(0x0001), }, /* Nokia E61i */
1646 { NOKIA_PCSUITE_ACM_INFO(0x0475), }, /* Nokia E62 */
1647 { NOKIA_PCSUITE_ACM_INFO(0x0508), }, /* Nokia E65 */
1648 { NOKIA_PCSUITE_ACM_INFO(0x0418), }, /* Nokia E70 */
1649 { NOKIA_PCSUITE_ACM_INFO(0x0425), }, /* Nokia N71 */
1650 { NOKIA_PCSUITE_ACM_INFO(0x0486), }, /* Nokia N73 */
1651 { NOKIA_PCSUITE_ACM_INFO(0x04DF), }, /* Nokia N75 */
1652 { NOKIA_PCSUITE_ACM_INFO(0x000e), }, /* Nokia N77 */
1653 { NOKIA_PCSUITE_ACM_INFO(0x0445), }, /* Nokia N80 */
1654 { NOKIA_PCSUITE_ACM_INFO(0x042F), }, /* Nokia N91 & N91 8GB */
1655 { NOKIA_PCSUITE_ACM_INFO(0x048E), }, /* Nokia N92 */
1656 { NOKIA_PCSUITE_ACM_INFO(0x0420), }, /* Nokia N93 */
1657 { NOKIA_PCSUITE_ACM_INFO(0x04E6), }, /* Nokia N93i */
1658 { NOKIA_PCSUITE_ACM_INFO(0x04B2), }, /* Nokia 5700 XpressMusic */
1659 { NOKIA_PCSUITE_ACM_INFO(0x0134), }, /* Nokia 6110 Navigator (China) */
1660 { NOKIA_PCSUITE_ACM_INFO(0x046E), }, /* Nokia 6110 Navigator */
1661 { NOKIA_PCSUITE_ACM_INFO(0x002f), }, /* Nokia 6120 classic & */
1662 { NOKIA_PCSUITE_ACM_INFO(0x0088), }, /* Nokia 6121 classic */
1663 { NOKIA_PCSUITE_ACM_INFO(0x00fc), }, /* Nokia 6124 classic */
1664 { NOKIA_PCSUITE_ACM_INFO(0x0042), }, /* Nokia E51 */
1665 { NOKIA_PCSUITE_ACM_INFO(0x00b0), }, /* Nokia E66 */
1666 { NOKIA_PCSUITE_ACM_INFO(0x00ab), }, /* Nokia E71 */
1667 { NOKIA_PCSUITE_ACM_INFO(0x0481), }, /* Nokia N76 */
1668 { NOKIA_PCSUITE_ACM_INFO(0x0007), }, /* Nokia N81 & N81 8GB */
1669 { NOKIA_PCSUITE_ACM_INFO(0x0071), }, /* Nokia N82 */
1670 { NOKIA_PCSUITE_ACM_INFO(0x04F0), }, /* Nokia N95 & N95-3 NAM */
1671 { NOKIA_PCSUITE_ACM_INFO(0x0070), }, /* Nokia N95 8GB */
1672 { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
1673 { NOKIA_PCSUITE_ACM_INFO(0x0099), }, /* Nokia 6210 Navigator, RM-367 */
1674 { NOKIA_PCSUITE_ACM_INFO(0x0128), }, /* Nokia 6210 Navigator, RM-419 */
1675 { NOKIA_PCSUITE_ACM_INFO(0x008f), }, /* Nokia 6220 Classic */
1676 { NOKIA_PCSUITE_ACM_INFO(0x00a0), }, /* Nokia 6650 */
1677 { NOKIA_PCSUITE_ACM_INFO(0x007b), }, /* Nokia N78 */
1678 { NOKIA_PCSUITE_ACM_INFO(0x0094), }, /* Nokia N85 */
1679 { NOKIA_PCSUITE_ACM_INFO(0x003a), }, /* Nokia N96 & N96-3 */
1680 { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
1681 { NOKIA_PCSUITE_ACM_INFO(0x0108), }, /* Nokia 5320 XpressMusic 2G */
1682 { NOKIA_PCSUITE_ACM_INFO(0x01f5), }, /* Nokia N97, RM-505 */
1683 { NOKIA_PCSUITE_ACM_INFO(0x02e3), }, /* Nokia 5230, RM-588 */
1684 { NOKIA_PCSUITE_ACM_INFO(0x0178), }, /* Nokia E63 */
1685 { NOKIA_PCSUITE_ACM_INFO(0x010e), }, /* Nokia E75 */
1686 { NOKIA_PCSUITE_ACM_INFO(0x02d9), }, /* Nokia 6760 Slide */
1687 { NOKIA_PCSUITE_ACM_INFO(0x01d0), }, /* Nokia E52 */
1688 { NOKIA_PCSUITE_ACM_INFO(0x0223), }, /* Nokia E72 */
1689 { NOKIA_PCSUITE_ACM_INFO(0x0275), }, /* Nokia X6 */
1690 { NOKIA_PCSUITE_ACM_INFO(0x026c), }, /* Nokia N97 Mini */
1691 { NOKIA_PCSUITE_ACM_INFO(0x0154), }, /* Nokia 5800 XpressMusic */
1692 { NOKIA_PCSUITE_ACM_INFO(0x04ce), }, /* Nokia E90 */
1693 { NOKIA_PCSUITE_ACM_INFO(0x01d4), }, /* Nokia E55 */
1694 { NOKIA_PCSUITE_ACM_INFO(0x0302), }, /* Nokia N8 */
1695 { NOKIA_PCSUITE_ACM_INFO(0x0335), }, /* Nokia E7 */
1696 { NOKIA_PCSUITE_ACM_INFO(0x03cd), }, /* Nokia C7 */
1697 { SAMSUNG_PCSUITE_ACM_INFO(0x6651), }, /* Samsung GTi8510 (INNOV8) */
1699 /* Support for Owen devices */
1700 { USB_DEVICE(0x03eb, 0x0030), }, /* Owen SI30 */
1702 /* NOTE: non-Nokia COMM/ACM/0xff is likely MSFT RNDIS... NOT a modem! */
1704 /* Support Lego NXT using pbLua firmware */
1705 { USB_DEVICE(0x0694, 0xff00),
1706 .driver_info = NOT_A_MODEM,
1709 /* Support for Droids MuIn LCD */
1710 { USB_DEVICE(0x04d8, 0x000b),
1711 .driver_info = NO_DATA_INTERFACE,
1714 #if IS_ENABLED(CONFIG_INPUT_IMS_PCU)
1715 { USB_DEVICE(0x04d8, 0x0082), /* Application mode */
1716 .driver_info = IGNORE_DEVICE,
1718 { USB_DEVICE(0x04d8, 0x0083), /* Bootloader mode */
1719 .driver_info = IGNORE_DEVICE,
1721 #endif
1723 /* control interfaces without any protocol set */
1724 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1725 USB_CDC_PROTO_NONE) },
1727 /* control interfaces with various AT-command sets */
1728 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1729 USB_CDC_ACM_PROTO_AT_V25TER) },
1730 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1731 USB_CDC_ACM_PROTO_AT_PCCA101) },
1732 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1733 USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
1734 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1735 USB_CDC_ACM_PROTO_AT_GSM) },
1736 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1737 USB_CDC_ACM_PROTO_AT_3G) },
1738 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1739 USB_CDC_ACM_PROTO_AT_CDMA) },
1744 MODULE_DEVICE_TABLE(usb, acm_ids);
1746 static struct usb_driver acm_driver = {
1747 .name = "cdc_acm",
1748 .probe = acm_probe,
1749 .disconnect = acm_disconnect,
1750 #ifdef CONFIG_PM
1751 .suspend = acm_suspend,
1752 .resume = acm_resume,
1753 .reset_resume = acm_reset_resume,
1754 #endif
1755 .id_table = acm_ids,
1756 #ifdef CONFIG_PM
1757 .supports_autosuspend = 1,
1758 #endif
1759 .disable_hub_initiated_lpm = 1,
1763 * TTY driver structures.
1766 static const struct tty_operations acm_ops = {
1767 .install = acm_tty_install,
1768 .open = acm_tty_open,
1769 .close = acm_tty_close,
1770 .cleanup = acm_tty_cleanup,
1771 .hangup = acm_tty_hangup,
1772 .write = acm_tty_write,
1773 .write_room = acm_tty_write_room,
1774 .ioctl = acm_tty_ioctl,
1775 .throttle = acm_tty_throttle,
1776 .unthrottle = acm_tty_unthrottle,
1777 .chars_in_buffer = acm_tty_chars_in_buffer,
1778 .break_ctl = acm_tty_break_ctl,
1779 .set_termios = acm_tty_set_termios,
1780 .tiocmget = acm_tty_tiocmget,
1781 .tiocmset = acm_tty_tiocmset,
1785 * Init / exit.
1788 static int __init acm_init(void)
1790 int retval;
1791 acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
1792 if (!acm_tty_driver)
1793 return -ENOMEM;
1794 acm_tty_driver->driver_name = "acm",
1795 acm_tty_driver->name = "ttyACM",
1796 acm_tty_driver->major = ACM_TTY_MAJOR,
1797 acm_tty_driver->minor_start = 0,
1798 acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
1799 acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
1800 acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1801 acm_tty_driver->init_termios = tty_std_termios;
1802 acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD |
1803 HUPCL | CLOCAL;
1804 tty_set_operations(acm_tty_driver, &acm_ops);
1806 retval = tty_register_driver(acm_tty_driver);
1807 if (retval) {
1808 put_tty_driver(acm_tty_driver);
1809 return retval;
1812 retval = usb_register(&acm_driver);
1813 if (retval) {
1814 tty_unregister_driver(acm_tty_driver);
1815 put_tty_driver(acm_tty_driver);
1816 return retval;
1819 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
1821 return 0;
1824 static void __exit acm_exit(void)
1826 usb_deregister(&acm_driver);
1827 tty_unregister_driver(acm_tty_driver);
1828 put_tty_driver(acm_tty_driver);
1831 module_init(acm_init);
1832 module_exit(acm_exit);
1834 MODULE_AUTHOR(DRIVER_AUTHOR);
1835 MODULE_DESCRIPTION(DRIVER_DESC);
1836 MODULE_LICENSE("GPL");
1837 MODULE_ALIAS_CHARDEV_MAJOR(ACM_TTY_MAJOR);