Merge git://www.linux-watchdog.org/linux-watchdog
[linux/fpc-iii.git] / drivers / usb / class / cdc-acm.c
blob9f49bfe4c6f4a24e112a71ecabee13b6e59aec0a
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 = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
126 request, USB_RT_ACM, value,
127 acm->control->altsetting[0].desc.bInterfaceNumber,
128 buf, len, 5000);
129 dev_dbg(&acm->control->dev,
130 "%s - rq 0x%02x, val %#x, len %#x, result %d\n",
131 __func__, request, value, len, retval);
132 return retval < 0 ? retval : 0;
135 /* devices aren't required to support these requests.
136 * the cdc acm descriptor tells whether they do...
138 #define acm_set_control(acm, control) \
139 acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0)
140 #define acm_set_line(acm, line) \
141 acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
142 #define acm_send_break(acm, ms) \
143 acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
146 * Write buffer management.
147 * All of these assume proper locks taken by the caller.
150 static int acm_wb_alloc(struct acm *acm)
152 int i, wbn;
153 struct acm_wb *wb;
155 wbn = 0;
156 i = 0;
157 for (;;) {
158 wb = &acm->wb[wbn];
159 if (!wb->use) {
160 wb->use = 1;
161 return wbn;
163 wbn = (wbn + 1) % ACM_NW;
164 if (++i >= ACM_NW)
165 return -1;
169 static int acm_wb_is_avail(struct acm *acm)
171 int i, n;
172 unsigned long flags;
174 n = ACM_NW;
175 spin_lock_irqsave(&acm->write_lock, flags);
176 for (i = 0; i < ACM_NW; i++)
177 n -= acm->wb[i].use;
178 spin_unlock_irqrestore(&acm->write_lock, flags);
179 return n;
183 * Finish write. Caller must hold acm->write_lock
185 static void acm_write_done(struct acm *acm, struct acm_wb *wb)
187 wb->use = 0;
188 acm->transmitting--;
189 usb_autopm_put_interface_async(acm->control);
193 * Poke write.
195 * the caller is responsible for locking
198 static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
200 int rc;
202 acm->transmitting++;
204 wb->urb->transfer_buffer = wb->buf;
205 wb->urb->transfer_dma = wb->dmah;
206 wb->urb->transfer_buffer_length = wb->len;
207 wb->urb->dev = acm->dev;
209 rc = usb_submit_urb(wb->urb, GFP_ATOMIC);
210 if (rc < 0) {
211 dev_err(&acm->data->dev,
212 "%s - usb_submit_urb(write bulk) failed: %d\n",
213 __func__, rc);
214 acm_write_done(acm, wb);
216 return rc;
220 * attributes exported through sysfs
222 static ssize_t show_caps
223 (struct device *dev, struct device_attribute *attr, char *buf)
225 struct usb_interface *intf = to_usb_interface(dev);
226 struct acm *acm = usb_get_intfdata(intf);
228 return sprintf(buf, "%d", acm->ctrl_caps);
230 static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
232 static ssize_t show_country_codes
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 memcpy(buf, acm->country_codes, acm->country_code_size);
239 return acm->country_code_size;
242 static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
244 static ssize_t show_country_rel_date
245 (struct device *dev, struct device_attribute *attr, char *buf)
247 struct usb_interface *intf = to_usb_interface(dev);
248 struct acm *acm = usb_get_intfdata(intf);
250 return sprintf(buf, "%d", acm->country_rel_date);
253 static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
255 * Interrupt handlers for various ACM device responses
258 /* control interface reports status changes with "interrupt" transfers */
259 static void acm_ctrl_irq(struct urb *urb)
261 struct acm *acm = urb->context;
262 struct usb_cdc_notification *dr = urb->transfer_buffer;
263 unsigned char *data;
264 int newctrl;
265 int retval;
266 int status = urb->status;
268 switch (status) {
269 case 0:
270 /* success */
271 break;
272 case -ECONNRESET:
273 case -ENOENT:
274 case -ESHUTDOWN:
275 /* this urb is terminated, clean up */
276 dev_dbg(&acm->control->dev,
277 "%s - urb shutting down with status: %d\n",
278 __func__, status);
279 return;
280 default:
281 dev_dbg(&acm->control->dev,
282 "%s - nonzero urb status received: %d\n",
283 __func__, status);
284 goto exit;
287 usb_mark_last_busy(acm->dev);
289 data = (unsigned char *)(dr + 1);
290 switch (dr->bNotificationType) {
291 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
292 dev_dbg(&acm->control->dev, "%s - network connection: %d\n",
293 __func__, dr->wValue);
294 break;
296 case USB_CDC_NOTIFY_SERIAL_STATE:
297 newctrl = get_unaligned_le16(data);
299 if (!acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
300 dev_dbg(&acm->control->dev, "%s - calling hangup\n",
301 __func__);
302 tty_port_tty_hangup(&acm->port, false);
305 acm->ctrlin = newctrl;
307 dev_dbg(&acm->control->dev,
308 "%s - input control lines: dcd%c dsr%c break%c "
309 "ring%c framing%c parity%c overrun%c\n",
310 __func__,
311 acm->ctrlin & ACM_CTRL_DCD ? '+' : '-',
312 acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
313 acm->ctrlin & ACM_CTRL_BRK ? '+' : '-',
314 acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
315 acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-',
316 acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
317 acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
318 break;
320 default:
321 dev_dbg(&acm->control->dev,
322 "%s - unknown notification %d received: index %d "
323 "len %d data0 %d data1 %d\n",
324 __func__,
325 dr->bNotificationType, dr->wIndex,
326 dr->wLength, data[0], data[1]);
327 break;
329 exit:
330 retval = usb_submit_urb(urb, GFP_ATOMIC);
331 if (retval)
332 dev_err(&acm->control->dev, "%s - usb_submit_urb failed: %d\n",
333 __func__, retval);
336 static int acm_submit_read_urb(struct acm *acm, int index, gfp_t mem_flags)
338 int res;
340 if (!test_and_clear_bit(index, &acm->read_urbs_free))
341 return 0;
343 dev_vdbg(&acm->data->dev, "%s - urb %d\n", __func__, index);
345 res = usb_submit_urb(acm->read_urbs[index], mem_flags);
346 if (res) {
347 if (res != -EPERM) {
348 dev_err(&acm->data->dev,
349 "%s - usb_submit_urb failed: %d\n",
350 __func__, res);
352 set_bit(index, &acm->read_urbs_free);
353 return res;
356 return 0;
359 static int acm_submit_read_urbs(struct acm *acm, gfp_t mem_flags)
361 int res;
362 int i;
364 for (i = 0; i < acm->rx_buflimit; ++i) {
365 res = acm_submit_read_urb(acm, i, mem_flags);
366 if (res)
367 return res;
370 return 0;
373 static void acm_process_read_urb(struct acm *acm, struct urb *urb)
375 if (!urb->actual_length)
376 return;
378 tty_insert_flip_string(&acm->port, urb->transfer_buffer,
379 urb->actual_length);
380 tty_flip_buffer_push(&acm->port);
383 static void acm_read_bulk_callback(struct urb *urb)
385 struct acm_rb *rb = urb->context;
386 struct acm *acm = rb->instance;
387 unsigned long flags;
389 dev_vdbg(&acm->data->dev, "%s - urb %d, len %d\n", __func__,
390 rb->index, urb->actual_length);
391 set_bit(rb->index, &acm->read_urbs_free);
393 if (!acm->dev) {
394 dev_dbg(&acm->data->dev, "%s - disconnected\n", __func__);
395 return;
397 usb_mark_last_busy(acm->dev);
399 if (urb->status) {
400 dev_dbg(&acm->data->dev, "%s - non-zero urb status: %d\n",
401 __func__, urb->status);
402 return;
404 acm_process_read_urb(acm, urb);
406 /* throttle device if requested by tty */
407 spin_lock_irqsave(&acm->read_lock, flags);
408 acm->throttled = acm->throttle_req;
409 if (!acm->throttled && !acm->susp_count) {
410 spin_unlock_irqrestore(&acm->read_lock, flags);
411 acm_submit_read_urb(acm, rb->index, GFP_ATOMIC);
412 } else {
413 spin_unlock_irqrestore(&acm->read_lock, flags);
417 /* data interface wrote those outgoing bytes */
418 static void acm_write_bulk(struct urb *urb)
420 struct acm_wb *wb = urb->context;
421 struct acm *acm = wb->instance;
422 unsigned long flags;
424 if (urb->status || (urb->actual_length != urb->transfer_buffer_length))
425 dev_vdbg(&acm->data->dev, "%s - len %d/%d, status %d\n",
426 __func__,
427 urb->actual_length,
428 urb->transfer_buffer_length,
429 urb->status);
431 spin_lock_irqsave(&acm->write_lock, flags);
432 acm_write_done(acm, wb);
433 spin_unlock_irqrestore(&acm->write_lock, flags);
434 schedule_work(&acm->work);
437 static void acm_softint(struct work_struct *work)
439 struct acm *acm = container_of(work, struct acm, work);
441 dev_vdbg(&acm->data->dev, "%s\n", __func__);
443 tty_port_tty_wakeup(&acm->port);
447 * TTY handlers
450 static int acm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
452 struct acm *acm;
453 int retval;
455 dev_dbg(tty->dev, "%s\n", __func__);
457 acm = acm_get_by_index(tty->index);
458 if (!acm)
459 return -ENODEV;
461 retval = tty_standard_install(driver, tty);
462 if (retval)
463 goto error_init_termios;
465 tty->driver_data = acm;
467 return 0;
469 error_init_termios:
470 tty_port_put(&acm->port);
471 return retval;
474 static int acm_tty_open(struct tty_struct *tty, struct file *filp)
476 struct acm *acm = tty->driver_data;
478 dev_dbg(tty->dev, "%s\n", __func__);
480 return tty_port_open(&acm->port, tty, filp);
483 static int acm_port_activate(struct tty_port *port, struct tty_struct *tty)
485 struct acm *acm = container_of(port, struct acm, port);
486 int retval = -ENODEV;
488 dev_dbg(&acm->control->dev, "%s\n", __func__);
490 mutex_lock(&acm->mutex);
491 if (acm->disconnected)
492 goto disconnected;
494 retval = usb_autopm_get_interface(acm->control);
495 if (retval)
496 goto error_get_interface;
499 * FIXME: Why do we need this? Allocating 64K of physically contiguous
500 * memory is really nasty...
502 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
503 acm->control->needs_remote_wakeup = 1;
505 acm->ctrlurb->dev = acm->dev;
506 if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
507 dev_err(&acm->control->dev,
508 "%s - usb_submit_urb(ctrl irq) failed\n", __func__);
509 goto error_submit_urb;
512 acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS;
513 if (acm_set_control(acm, acm->ctrlout) < 0 &&
514 (acm->ctrl_caps & USB_CDC_CAP_LINE))
515 goto error_set_control;
517 usb_autopm_put_interface(acm->control);
520 * Unthrottle device in case the TTY was closed while throttled.
522 spin_lock_irq(&acm->read_lock);
523 acm->throttled = 0;
524 acm->throttle_req = 0;
525 spin_unlock_irq(&acm->read_lock);
527 if (acm_submit_read_urbs(acm, GFP_KERNEL))
528 goto error_submit_read_urbs;
530 mutex_unlock(&acm->mutex);
532 return 0;
534 error_submit_read_urbs:
535 acm->ctrlout = 0;
536 acm_set_control(acm, acm->ctrlout);
537 error_set_control:
538 usb_kill_urb(acm->ctrlurb);
539 error_submit_urb:
540 usb_autopm_put_interface(acm->control);
541 error_get_interface:
542 disconnected:
543 mutex_unlock(&acm->mutex);
544 return retval;
547 static void acm_port_destruct(struct tty_port *port)
549 struct acm *acm = container_of(port, struct acm, port);
551 dev_dbg(&acm->control->dev, "%s\n", __func__);
553 acm_release_minor(acm);
554 usb_put_intf(acm->control);
555 kfree(acm->country_codes);
556 kfree(acm);
559 static void acm_port_shutdown(struct tty_port *port)
561 struct acm *acm = container_of(port, struct acm, port);
562 int i;
564 dev_dbg(&acm->control->dev, "%s\n", __func__);
566 mutex_lock(&acm->mutex);
567 if (!acm->disconnected) {
568 usb_autopm_get_interface(acm->control);
569 acm_set_control(acm, acm->ctrlout = 0);
570 usb_kill_urb(acm->ctrlurb);
571 for (i = 0; i < ACM_NW; i++)
572 usb_kill_urb(acm->wb[i].urb);
573 for (i = 0; i < acm->rx_buflimit; i++)
574 usb_kill_urb(acm->read_urbs[i]);
575 acm->control->needs_remote_wakeup = 0;
576 usb_autopm_put_interface(acm->control);
578 mutex_unlock(&acm->mutex);
581 static void acm_tty_cleanup(struct tty_struct *tty)
583 struct acm *acm = tty->driver_data;
584 dev_dbg(&acm->control->dev, "%s\n", __func__);
585 tty_port_put(&acm->port);
588 static void acm_tty_hangup(struct tty_struct *tty)
590 struct acm *acm = tty->driver_data;
591 dev_dbg(&acm->control->dev, "%s\n", __func__);
592 tty_port_hangup(&acm->port);
595 static void acm_tty_close(struct tty_struct *tty, struct file *filp)
597 struct acm *acm = tty->driver_data;
598 dev_dbg(&acm->control->dev, "%s\n", __func__);
599 tty_port_close(&acm->port, tty, filp);
602 static int acm_tty_write(struct tty_struct *tty,
603 const unsigned char *buf, int count)
605 struct acm *acm = tty->driver_data;
606 int stat;
607 unsigned long flags;
608 int wbn;
609 struct acm_wb *wb;
611 if (!count)
612 return 0;
614 dev_vdbg(&acm->data->dev, "%s - count %d\n", __func__, count);
616 spin_lock_irqsave(&acm->write_lock, flags);
617 wbn = acm_wb_alloc(acm);
618 if (wbn < 0) {
619 spin_unlock_irqrestore(&acm->write_lock, flags);
620 return 0;
622 wb = &acm->wb[wbn];
624 if (!acm->dev) {
625 wb->use = 0;
626 spin_unlock_irqrestore(&acm->write_lock, flags);
627 return -ENODEV;
630 count = (count > acm->writesize) ? acm->writesize : count;
631 dev_vdbg(&acm->data->dev, "%s - write %d\n", __func__, count);
632 memcpy(wb->buf, buf, count);
633 wb->len = count;
635 usb_autopm_get_interface_async(acm->control);
636 if (acm->susp_count) {
637 if (!acm->delayed_wb)
638 acm->delayed_wb = wb;
639 else
640 usb_autopm_put_interface_async(acm->control);
641 spin_unlock_irqrestore(&acm->write_lock, flags);
642 return count; /* A white lie */
644 usb_mark_last_busy(acm->dev);
646 stat = acm_start_wb(acm, wb);
647 spin_unlock_irqrestore(&acm->write_lock, flags);
649 if (stat < 0)
650 return stat;
651 return count;
654 static int acm_tty_write_room(struct tty_struct *tty)
656 struct acm *acm = tty->driver_data;
658 * Do not let the line discipline to know that we have a reserve,
659 * or it might get too enthusiastic.
661 return acm_wb_is_avail(acm) ? acm->writesize : 0;
664 static int acm_tty_chars_in_buffer(struct tty_struct *tty)
666 struct acm *acm = tty->driver_data;
668 * if the device was unplugged then any remaining characters fell out
669 * of the connector ;)
671 if (acm->disconnected)
672 return 0;
674 * This is inaccurate (overcounts), but it works.
676 return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
679 static void acm_tty_throttle(struct tty_struct *tty)
681 struct acm *acm = tty->driver_data;
683 spin_lock_irq(&acm->read_lock);
684 acm->throttle_req = 1;
685 spin_unlock_irq(&acm->read_lock);
688 static void acm_tty_unthrottle(struct tty_struct *tty)
690 struct acm *acm = tty->driver_data;
691 unsigned int was_throttled;
693 spin_lock_irq(&acm->read_lock);
694 was_throttled = acm->throttled;
695 acm->throttled = 0;
696 acm->throttle_req = 0;
697 spin_unlock_irq(&acm->read_lock);
699 if (was_throttled)
700 acm_submit_read_urbs(acm, GFP_KERNEL);
703 static int acm_tty_break_ctl(struct tty_struct *tty, int state)
705 struct acm *acm = tty->driver_data;
706 int retval;
708 retval = acm_send_break(acm, state ? 0xffff : 0);
709 if (retval < 0)
710 dev_dbg(&acm->control->dev, "%s - send break failed\n",
711 __func__);
712 return retval;
715 static int acm_tty_tiocmget(struct tty_struct *tty)
717 struct acm *acm = tty->driver_data;
719 return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
720 (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
721 (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
722 (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
723 (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
724 TIOCM_CTS;
727 static int acm_tty_tiocmset(struct tty_struct *tty,
728 unsigned int set, unsigned int clear)
730 struct acm *acm = tty->driver_data;
731 unsigned int newctrl;
733 newctrl = acm->ctrlout;
734 set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
735 (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
736 clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
737 (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
739 newctrl = (newctrl & ~clear) | set;
741 if (acm->ctrlout == newctrl)
742 return 0;
743 return acm_set_control(acm, acm->ctrlout = newctrl);
746 static int get_serial_info(struct acm *acm, struct serial_struct __user *info)
748 struct serial_struct tmp;
750 if (!info)
751 return -EINVAL;
753 memset(&tmp, 0, sizeof(tmp));
754 tmp.flags = ASYNC_LOW_LATENCY;
755 tmp.xmit_fifo_size = acm->writesize;
756 tmp.baud_base = le32_to_cpu(acm->line.dwDTERate);
757 tmp.close_delay = acm->port.close_delay / 10;
758 tmp.closing_wait = acm->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
759 ASYNC_CLOSING_WAIT_NONE :
760 acm->port.closing_wait / 10;
762 if (copy_to_user(info, &tmp, sizeof(tmp)))
763 return -EFAULT;
764 else
765 return 0;
768 static int set_serial_info(struct acm *acm,
769 struct serial_struct __user *newinfo)
771 struct serial_struct new_serial;
772 unsigned int closing_wait, close_delay;
773 int retval = 0;
775 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
776 return -EFAULT;
778 close_delay = new_serial.close_delay * 10;
779 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
780 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
782 mutex_lock(&acm->port.mutex);
784 if (!capable(CAP_SYS_ADMIN)) {
785 if ((close_delay != acm->port.close_delay) ||
786 (closing_wait != acm->port.closing_wait))
787 retval = -EPERM;
788 else
789 retval = -EOPNOTSUPP;
790 } else {
791 acm->port.close_delay = close_delay;
792 acm->port.closing_wait = closing_wait;
795 mutex_unlock(&acm->port.mutex);
796 return retval;
799 static int acm_tty_ioctl(struct tty_struct *tty,
800 unsigned int cmd, unsigned long arg)
802 struct acm *acm = tty->driver_data;
803 int rv = -ENOIOCTLCMD;
805 switch (cmd) {
806 case TIOCGSERIAL: /* gets serial port data */
807 rv = get_serial_info(acm, (struct serial_struct __user *) arg);
808 break;
809 case TIOCSSERIAL:
810 rv = set_serial_info(acm, (struct serial_struct __user *) arg);
811 break;
814 return rv;
817 static void acm_tty_set_termios(struct tty_struct *tty,
818 struct ktermios *termios_old)
820 struct acm *acm = tty->driver_data;
821 struct ktermios *termios = &tty->termios;
822 struct usb_cdc_line_coding newline;
823 int newctrl = acm->ctrlout;
825 newline.dwDTERate = cpu_to_le32(tty_get_baud_rate(tty));
826 newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
827 newline.bParityType = termios->c_cflag & PARENB ?
828 (termios->c_cflag & PARODD ? 1 : 2) +
829 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
830 switch (termios->c_cflag & CSIZE) {
831 case CS5:
832 newline.bDataBits = 5;
833 break;
834 case CS6:
835 newline.bDataBits = 6;
836 break;
837 case CS7:
838 newline.bDataBits = 7;
839 break;
840 case CS8:
841 default:
842 newline.bDataBits = 8;
843 break;
845 /* FIXME: Needs to clear unsupported bits in the termios */
846 acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
848 if (!newline.dwDTERate) {
849 newline.dwDTERate = acm->line.dwDTERate;
850 newctrl &= ~ACM_CTRL_DTR;
851 } else
852 newctrl |= ACM_CTRL_DTR;
854 if (newctrl != acm->ctrlout)
855 acm_set_control(acm, acm->ctrlout = newctrl);
857 if (memcmp(&acm->line, &newline, sizeof newline)) {
858 memcpy(&acm->line, &newline, sizeof newline);
859 dev_dbg(&acm->control->dev, "%s - set line: %d %d %d %d\n",
860 __func__,
861 le32_to_cpu(newline.dwDTERate),
862 newline.bCharFormat, newline.bParityType,
863 newline.bDataBits);
864 acm_set_line(acm, &acm->line);
868 static const struct tty_port_operations acm_port_ops = {
869 .shutdown = acm_port_shutdown,
870 .activate = acm_port_activate,
871 .destruct = acm_port_destruct,
875 * USB probe and disconnect routines.
878 /* Little helpers: write/read buffers free */
879 static void acm_write_buffers_free(struct acm *acm)
881 int i;
882 struct acm_wb *wb;
883 struct usb_device *usb_dev = interface_to_usbdev(acm->control);
885 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++)
886 usb_free_coherent(usb_dev, acm->writesize, wb->buf, wb->dmah);
889 static void acm_read_buffers_free(struct acm *acm)
891 struct usb_device *usb_dev = interface_to_usbdev(acm->control);
892 int i;
894 for (i = 0; i < acm->rx_buflimit; i++)
895 usb_free_coherent(usb_dev, acm->readsize,
896 acm->read_buffers[i].base, acm->read_buffers[i].dma);
899 /* Little helper: write buffers allocate */
900 static int acm_write_buffers_alloc(struct acm *acm)
902 int i;
903 struct acm_wb *wb;
905 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
906 wb->buf = usb_alloc_coherent(acm->dev, acm->writesize, GFP_KERNEL,
907 &wb->dmah);
908 if (!wb->buf) {
909 while (i != 0) {
910 --i;
911 --wb;
912 usb_free_coherent(acm->dev, acm->writesize,
913 wb->buf, wb->dmah);
915 return -ENOMEM;
918 return 0;
921 static int acm_probe(struct usb_interface *intf,
922 const struct usb_device_id *id)
924 struct usb_cdc_union_desc *union_header = NULL;
925 struct usb_cdc_country_functional_desc *cfd = NULL;
926 unsigned char *buffer = intf->altsetting->extra;
927 int buflen = intf->altsetting->extralen;
928 struct usb_interface *control_interface;
929 struct usb_interface *data_interface;
930 struct usb_endpoint_descriptor *epctrl = NULL;
931 struct usb_endpoint_descriptor *epread = NULL;
932 struct usb_endpoint_descriptor *epwrite = NULL;
933 struct usb_device *usb_dev = interface_to_usbdev(intf);
934 struct acm *acm;
935 int minor;
936 int ctrlsize, readsize;
937 u8 *buf;
938 u8 ac_management_function = 0;
939 u8 call_management_function = 0;
940 int call_interface_num = -1;
941 int data_interface_num = -1;
942 unsigned long quirks;
943 int num_rx_buf;
944 int i;
945 int combined_interfaces = 0;
946 struct device *tty_dev;
947 int rv = -ENOMEM;
949 /* normal quirks */
950 quirks = (unsigned long)id->driver_info;
952 if (quirks == IGNORE_DEVICE)
953 return -ENODEV;
955 num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
957 /* handle quirks deadly to normal probing*/
958 if (quirks == NO_UNION_NORMAL) {
959 data_interface = usb_ifnum_to_if(usb_dev, 1);
960 control_interface = usb_ifnum_to_if(usb_dev, 0);
961 goto skip_normal_probe;
964 /* normal probing*/
965 if (!buffer) {
966 dev_err(&intf->dev, "Weird descriptor references\n");
967 return -EINVAL;
970 if (!buflen) {
971 if (intf->cur_altsetting->endpoint &&
972 intf->cur_altsetting->endpoint->extralen &&
973 intf->cur_altsetting->endpoint->extra) {
974 dev_dbg(&intf->dev,
975 "Seeking extra descriptors on endpoint\n");
976 buflen = intf->cur_altsetting->endpoint->extralen;
977 buffer = intf->cur_altsetting->endpoint->extra;
978 } else {
979 dev_err(&intf->dev,
980 "Zero length descriptor references\n");
981 return -EINVAL;
985 while (buflen > 0) {
986 if (buffer[1] != USB_DT_CS_INTERFACE) {
987 dev_err(&intf->dev, "skipping garbage\n");
988 goto next_desc;
991 switch (buffer[2]) {
992 case USB_CDC_UNION_TYPE: /* we've found it */
993 if (union_header) {
994 dev_err(&intf->dev, "More than one "
995 "union descriptor, skipping ...\n");
996 goto next_desc;
998 union_header = (struct usb_cdc_union_desc *)buffer;
999 break;
1000 case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
1001 cfd = (struct usb_cdc_country_functional_desc *)buffer;
1002 break;
1003 case USB_CDC_HEADER_TYPE: /* maybe check version */
1004 break; /* for now we ignore it */
1005 case USB_CDC_ACM_TYPE:
1006 ac_management_function = buffer[3];
1007 break;
1008 case USB_CDC_CALL_MANAGEMENT_TYPE:
1009 call_management_function = buffer[3];
1010 call_interface_num = buffer[4];
1011 if ((quirks & NOT_A_MODEM) == 0 && (call_management_function & 3) != 3)
1012 dev_err(&intf->dev, "This device cannot do calls on its own. It is not a modem.\n");
1013 break;
1014 default:
1015 /* there are LOTS more CDC descriptors that
1016 * could legitimately be found here.
1018 dev_dbg(&intf->dev, "Ignoring descriptor: "
1019 "type %02x, length %d\n",
1020 buffer[2], buffer[0]);
1021 break;
1023 next_desc:
1024 buflen -= buffer[0];
1025 buffer += buffer[0];
1028 if (!union_header) {
1029 if (call_interface_num > 0) {
1030 dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n");
1031 /* quirks for Droids MuIn LCD */
1032 if (quirks & NO_DATA_INTERFACE)
1033 data_interface = usb_ifnum_to_if(usb_dev, 0);
1034 else
1035 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
1036 control_interface = intf;
1037 } else {
1038 if (intf->cur_altsetting->desc.bNumEndpoints != 3) {
1039 dev_dbg(&intf->dev,"No union descriptor, giving up\n");
1040 return -ENODEV;
1041 } else {
1042 dev_warn(&intf->dev,"No union descriptor, testing for castrated device\n");
1043 combined_interfaces = 1;
1044 control_interface = data_interface = intf;
1045 goto look_for_collapsed_interface;
1048 } else {
1049 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
1050 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
1051 if (!control_interface || !data_interface) {
1052 dev_dbg(&intf->dev, "no interfaces\n");
1053 return -ENODEV;
1057 if (data_interface_num != call_interface_num)
1058 dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n");
1060 if (control_interface == data_interface) {
1061 /* some broken devices designed for windows work this way */
1062 dev_warn(&intf->dev,"Control and data interfaces are not separated!\n");
1063 combined_interfaces = 1;
1064 /* a popular other OS doesn't use it */
1065 quirks |= NO_CAP_LINE;
1066 if (data_interface->cur_altsetting->desc.bNumEndpoints != 3) {
1067 dev_err(&intf->dev, "This needs exactly 3 endpoints\n");
1068 return -EINVAL;
1070 look_for_collapsed_interface:
1071 for (i = 0; i < 3; i++) {
1072 struct usb_endpoint_descriptor *ep;
1073 ep = &data_interface->cur_altsetting->endpoint[i].desc;
1075 if (usb_endpoint_is_int_in(ep))
1076 epctrl = ep;
1077 else if (usb_endpoint_is_bulk_out(ep))
1078 epwrite = ep;
1079 else if (usb_endpoint_is_bulk_in(ep))
1080 epread = ep;
1081 else
1082 return -EINVAL;
1084 if (!epctrl || !epread || !epwrite)
1085 return -ENODEV;
1086 else
1087 goto made_compressed_probe;
1090 skip_normal_probe:
1092 /*workaround for switched interfaces */
1093 if (data_interface->cur_altsetting->desc.bInterfaceClass
1094 != CDC_DATA_INTERFACE_TYPE) {
1095 if (control_interface->cur_altsetting->desc.bInterfaceClass
1096 == CDC_DATA_INTERFACE_TYPE) {
1097 struct usb_interface *t;
1098 dev_dbg(&intf->dev,
1099 "Your device has switched interfaces.\n");
1100 t = control_interface;
1101 control_interface = data_interface;
1102 data_interface = t;
1103 } else {
1104 return -EINVAL;
1108 /* Accept probe requests only for the control interface */
1109 if (!combined_interfaces && intf != control_interface)
1110 return -ENODEV;
1112 if (!combined_interfaces && usb_interface_claimed(data_interface)) {
1113 /* valid in this context */
1114 dev_dbg(&intf->dev, "The data interface isn't available\n");
1115 return -EBUSY;
1119 if (data_interface->cur_altsetting->desc.bNumEndpoints < 2 ||
1120 control_interface->cur_altsetting->desc.bNumEndpoints == 0)
1121 return -EINVAL;
1123 epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
1124 epread = &data_interface->cur_altsetting->endpoint[0].desc;
1125 epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
1128 /* workaround for switched endpoints */
1129 if (!usb_endpoint_dir_in(epread)) {
1130 /* descriptors are swapped */
1131 struct usb_endpoint_descriptor *t;
1132 dev_dbg(&intf->dev,
1133 "The data interface has switched endpoints\n");
1134 t = epread;
1135 epread = epwrite;
1136 epwrite = t;
1138 made_compressed_probe:
1139 dev_dbg(&intf->dev, "interfaces are valid\n");
1141 acm = kzalloc(sizeof(struct acm), GFP_KERNEL);
1142 if (acm == NULL) {
1143 dev_err(&intf->dev, "out of memory (acm kzalloc)\n");
1144 goto alloc_fail;
1147 minor = acm_alloc_minor(acm);
1148 if (minor == ACM_TTY_MINORS) {
1149 dev_err(&intf->dev, "no more free acm devices\n");
1150 kfree(acm);
1151 return -ENODEV;
1154 ctrlsize = usb_endpoint_maxp(epctrl);
1155 readsize = usb_endpoint_maxp(epread) *
1156 (quirks == SINGLE_RX_URB ? 1 : 2);
1157 acm->combined_interfaces = combined_interfaces;
1158 acm->writesize = usb_endpoint_maxp(epwrite) * 20;
1159 acm->control = control_interface;
1160 acm->data = data_interface;
1161 acm->minor = minor;
1162 acm->dev = usb_dev;
1163 acm->ctrl_caps = ac_management_function;
1164 if (quirks & NO_CAP_LINE)
1165 acm->ctrl_caps &= ~USB_CDC_CAP_LINE;
1166 acm->ctrlsize = ctrlsize;
1167 acm->readsize = readsize;
1168 acm->rx_buflimit = num_rx_buf;
1169 INIT_WORK(&acm->work, acm_softint);
1170 spin_lock_init(&acm->write_lock);
1171 spin_lock_init(&acm->read_lock);
1172 mutex_init(&acm->mutex);
1173 acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
1174 acm->is_int_ep = usb_endpoint_xfer_int(epread);
1175 if (acm->is_int_ep)
1176 acm->bInterval = epread->bInterval;
1177 tty_port_init(&acm->port);
1178 acm->port.ops = &acm_port_ops;
1180 buf = usb_alloc_coherent(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
1181 if (!buf) {
1182 dev_err(&intf->dev, "out of memory (ctrl buffer alloc)\n");
1183 goto alloc_fail2;
1185 acm->ctrl_buffer = buf;
1187 if (acm_write_buffers_alloc(acm) < 0) {
1188 dev_err(&intf->dev, "out of memory (write buffer alloc)\n");
1189 goto alloc_fail4;
1192 acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
1193 if (!acm->ctrlurb) {
1194 dev_err(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
1195 goto alloc_fail5;
1197 for (i = 0; i < num_rx_buf; i++) {
1198 struct acm_rb *rb = &(acm->read_buffers[i]);
1199 struct urb *urb;
1201 rb->base = usb_alloc_coherent(acm->dev, readsize, GFP_KERNEL,
1202 &rb->dma);
1203 if (!rb->base) {
1204 dev_err(&intf->dev, "out of memory "
1205 "(read bufs usb_alloc_coherent)\n");
1206 goto alloc_fail6;
1208 rb->index = i;
1209 rb->instance = acm;
1211 urb = usb_alloc_urb(0, GFP_KERNEL);
1212 if (!urb) {
1213 dev_err(&intf->dev,
1214 "out of memory (read urbs usb_alloc_urb)\n");
1215 goto alloc_fail6;
1217 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1218 urb->transfer_dma = rb->dma;
1219 if (acm->is_int_ep) {
1220 usb_fill_int_urb(urb, acm->dev,
1221 acm->rx_endpoint,
1222 rb->base,
1223 acm->readsize,
1224 acm_read_bulk_callback, rb,
1225 acm->bInterval);
1226 } else {
1227 usb_fill_bulk_urb(urb, acm->dev,
1228 acm->rx_endpoint,
1229 rb->base,
1230 acm->readsize,
1231 acm_read_bulk_callback, rb);
1234 acm->read_urbs[i] = urb;
1235 __set_bit(i, &acm->read_urbs_free);
1237 for (i = 0; i < ACM_NW; i++) {
1238 struct acm_wb *snd = &(acm->wb[i]);
1240 snd->urb = usb_alloc_urb(0, GFP_KERNEL);
1241 if (snd->urb == NULL) {
1242 dev_err(&intf->dev,
1243 "out of memory (write urbs usb_alloc_urb)\n");
1244 goto alloc_fail7;
1247 if (usb_endpoint_xfer_int(epwrite))
1248 usb_fill_int_urb(snd->urb, usb_dev,
1249 usb_sndintpipe(usb_dev, epwrite->bEndpointAddress),
1250 NULL, acm->writesize, acm_write_bulk, snd, epwrite->bInterval);
1251 else
1252 usb_fill_bulk_urb(snd->urb, usb_dev,
1253 usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
1254 NULL, acm->writesize, acm_write_bulk, snd);
1255 snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1256 snd->instance = acm;
1259 usb_set_intfdata(intf, acm);
1261 i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
1262 if (i < 0)
1263 goto alloc_fail7;
1265 if (cfd) { /* export the country data */
1266 acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
1267 if (!acm->country_codes)
1268 goto skip_countries;
1269 acm->country_code_size = cfd->bLength - 4;
1270 memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0,
1271 cfd->bLength - 4);
1272 acm->country_rel_date = cfd->iCountryCodeRelDate;
1274 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
1275 if (i < 0) {
1276 kfree(acm->country_codes);
1277 acm->country_codes = NULL;
1278 acm->country_code_size = 0;
1279 goto skip_countries;
1282 i = device_create_file(&intf->dev,
1283 &dev_attr_iCountryCodeRelDate);
1284 if (i < 0) {
1285 device_remove_file(&intf->dev, &dev_attr_wCountryCodes);
1286 kfree(acm->country_codes);
1287 acm->country_codes = NULL;
1288 acm->country_code_size = 0;
1289 goto skip_countries;
1293 skip_countries:
1294 usb_fill_int_urb(acm->ctrlurb, usb_dev,
1295 usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
1296 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm,
1297 /* works around buggy devices */
1298 epctrl->bInterval ? epctrl->bInterval : 0xff);
1299 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1300 acm->ctrlurb->transfer_dma = acm->ctrl_dma;
1302 dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
1304 acm_set_control(acm, acm->ctrlout);
1306 acm->line.dwDTERate = cpu_to_le32(9600);
1307 acm->line.bDataBits = 8;
1308 acm_set_line(acm, &acm->line);
1310 usb_driver_claim_interface(&acm_driver, data_interface, acm);
1311 usb_set_intfdata(data_interface, acm);
1313 usb_get_intf(control_interface);
1314 tty_dev = tty_port_register_device(&acm->port, acm_tty_driver, minor,
1315 &control_interface->dev);
1316 if (IS_ERR(tty_dev)) {
1317 rv = PTR_ERR(tty_dev);
1318 goto alloc_fail8;
1321 return 0;
1322 alloc_fail8:
1323 if (acm->country_codes) {
1324 device_remove_file(&acm->control->dev,
1325 &dev_attr_wCountryCodes);
1326 device_remove_file(&acm->control->dev,
1327 &dev_attr_iCountryCodeRelDate);
1329 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
1330 alloc_fail7:
1331 usb_set_intfdata(intf, NULL);
1332 for (i = 0; i < ACM_NW; i++)
1333 usb_free_urb(acm->wb[i].urb);
1334 alloc_fail6:
1335 for (i = 0; i < num_rx_buf; i++)
1336 usb_free_urb(acm->read_urbs[i]);
1337 acm_read_buffers_free(acm);
1338 usb_free_urb(acm->ctrlurb);
1339 alloc_fail5:
1340 acm_write_buffers_free(acm);
1341 alloc_fail4:
1342 usb_free_coherent(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1343 alloc_fail2:
1344 acm_release_minor(acm);
1345 kfree(acm);
1346 alloc_fail:
1347 return rv;
1350 static void stop_data_traffic(struct acm *acm)
1352 int i;
1354 dev_dbg(&acm->control->dev, "%s\n", __func__);
1356 usb_kill_urb(acm->ctrlurb);
1357 for (i = 0; i < ACM_NW; i++)
1358 usb_kill_urb(acm->wb[i].urb);
1359 for (i = 0; i < acm->rx_buflimit; i++)
1360 usb_kill_urb(acm->read_urbs[i]);
1362 cancel_work_sync(&acm->work);
1365 static void acm_disconnect(struct usb_interface *intf)
1367 struct acm *acm = usb_get_intfdata(intf);
1368 struct usb_device *usb_dev = interface_to_usbdev(intf);
1369 struct tty_struct *tty;
1370 int i;
1372 dev_dbg(&intf->dev, "%s\n", __func__);
1374 /* sibling interface is already cleaning up */
1375 if (!acm)
1376 return;
1378 mutex_lock(&acm->mutex);
1379 acm->disconnected = true;
1380 if (acm->country_codes) {
1381 device_remove_file(&acm->control->dev,
1382 &dev_attr_wCountryCodes);
1383 device_remove_file(&acm->control->dev,
1384 &dev_attr_iCountryCodeRelDate);
1386 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
1387 usb_set_intfdata(acm->control, NULL);
1388 usb_set_intfdata(acm->data, NULL);
1389 mutex_unlock(&acm->mutex);
1391 tty = tty_port_tty_get(&acm->port);
1392 if (tty) {
1393 tty_vhangup(tty);
1394 tty_kref_put(tty);
1397 stop_data_traffic(acm);
1399 tty_unregister_device(acm_tty_driver, acm->minor);
1401 usb_free_urb(acm->ctrlurb);
1402 for (i = 0; i < ACM_NW; i++)
1403 usb_free_urb(acm->wb[i].urb);
1404 for (i = 0; i < acm->rx_buflimit; i++)
1405 usb_free_urb(acm->read_urbs[i]);
1406 acm_write_buffers_free(acm);
1407 usb_free_coherent(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1408 acm_read_buffers_free(acm);
1410 if (!acm->combined_interfaces)
1411 usb_driver_release_interface(&acm_driver, intf == acm->control ?
1412 acm->data : acm->control);
1414 tty_port_put(&acm->port);
1417 #ifdef CONFIG_PM
1418 static int acm_suspend(struct usb_interface *intf, pm_message_t message)
1420 struct acm *acm = usb_get_intfdata(intf);
1421 int cnt;
1423 if (PMSG_IS_AUTO(message)) {
1424 int b;
1426 spin_lock_irq(&acm->write_lock);
1427 b = acm->transmitting;
1428 spin_unlock_irq(&acm->write_lock);
1429 if (b)
1430 return -EBUSY;
1433 spin_lock_irq(&acm->read_lock);
1434 spin_lock(&acm->write_lock);
1435 cnt = acm->susp_count++;
1436 spin_unlock(&acm->write_lock);
1437 spin_unlock_irq(&acm->read_lock);
1439 if (cnt)
1440 return 0;
1442 if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags))
1443 stop_data_traffic(acm);
1445 return 0;
1448 static int acm_resume(struct usb_interface *intf)
1450 struct acm *acm = usb_get_intfdata(intf);
1451 struct acm_wb *wb;
1452 int rv = 0;
1453 int cnt;
1455 spin_lock_irq(&acm->read_lock);
1456 acm->susp_count -= 1;
1457 cnt = acm->susp_count;
1458 spin_unlock_irq(&acm->read_lock);
1460 if (cnt)
1461 return 0;
1463 if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags)) {
1464 rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO);
1466 spin_lock_irq(&acm->write_lock);
1467 if (acm->delayed_wb) {
1468 wb = acm->delayed_wb;
1469 acm->delayed_wb = NULL;
1470 spin_unlock_irq(&acm->write_lock);
1471 acm_start_wb(acm, wb);
1472 } else {
1473 spin_unlock_irq(&acm->write_lock);
1477 * delayed error checking because we must
1478 * do the write path at all cost
1480 if (rv < 0)
1481 goto err_out;
1483 rv = acm_submit_read_urbs(acm, GFP_NOIO);
1486 err_out:
1487 return rv;
1490 static int acm_reset_resume(struct usb_interface *intf)
1492 struct acm *acm = usb_get_intfdata(intf);
1494 if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags))
1495 tty_port_tty_hangup(&acm->port, false);
1497 return acm_resume(intf);
1500 #endif /* CONFIG_PM */
1502 #define NOKIA_PCSUITE_ACM_INFO(x) \
1503 USB_DEVICE_AND_INTERFACE_INFO(0x0421, x, \
1504 USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
1505 USB_CDC_ACM_PROTO_VENDOR)
1507 #define SAMSUNG_PCSUITE_ACM_INFO(x) \
1508 USB_DEVICE_AND_INTERFACE_INFO(0x04e7, x, \
1509 USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
1510 USB_CDC_ACM_PROTO_VENDOR)
1513 * USB driver structure.
1516 static const struct usb_device_id acm_ids[] = {
1517 /* quirky and broken devices */
1518 { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
1519 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1521 { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
1522 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1524 { USB_DEVICE(0x0e8d, 0x3329), /* MediaTek Inc GPS */
1525 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1527 { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
1528 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1530 { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
1531 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1533 { USB_DEVICE(0x0ace, 0x1602), /* ZyDAS 56K USB MODEM */
1534 .driver_info = SINGLE_RX_URB,
1536 { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
1537 .driver_info = SINGLE_RX_URB, /* firmware bug */
1539 { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
1540 .driver_info = SINGLE_RX_URB, /* firmware bug */
1542 { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
1543 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1545 { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
1546 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1548 { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */
1549 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1551 { USB_DEVICE(0x0572, 0x1324), /* Conexant USB MODEM RD02-D400 */
1552 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1554 { USB_DEVICE(0x0572, 0x1328), /* Shiro / Aztech USB MODEM UM-3100 */
1555 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1557 { USB_DEVICE(0x22b8, 0x6425), /* Motorola MOTOMAGX phones */
1559 /* Motorola H24 HSPA module: */
1560 { USB_DEVICE(0x22b8, 0x2d91) }, /* modem */
1561 { USB_DEVICE(0x22b8, 0x2d92) }, /* modem + diagnostics */
1562 { USB_DEVICE(0x22b8, 0x2d93) }, /* modem + AT port */
1563 { USB_DEVICE(0x22b8, 0x2d95) }, /* modem + AT port + diagnostics */
1564 { USB_DEVICE(0x22b8, 0x2d96) }, /* modem + NMEA */
1565 { USB_DEVICE(0x22b8, 0x2d97) }, /* modem + diagnostics + NMEA */
1566 { USB_DEVICE(0x22b8, 0x2d99) }, /* modem + AT port + NMEA */
1567 { USB_DEVICE(0x22b8, 0x2d9a) }, /* modem + AT port + diagnostics + NMEA */
1569 { USB_DEVICE(0x0572, 0x1329), /* Hummingbird huc56s (Conexant) */
1570 .driver_info = NO_UNION_NORMAL, /* union descriptor misplaced on
1571 data interface instead of
1572 communications interface.
1573 Maybe we should define a new
1574 quirk for this. */
1576 { USB_DEVICE(0x0572, 0x1340), /* Conexant CX93010-2x UCMxx */
1577 .driver_info = NO_UNION_NORMAL,
1579 { USB_DEVICE(0x05f9, 0x4002), /* PSC Scanning, Magellan 800i */
1580 .driver_info = NO_UNION_NORMAL,
1582 { USB_DEVICE(0x1bbb, 0x0003), /* Alcatel OT-I650 */
1583 .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
1585 { USB_DEVICE(0x1576, 0x03b1), /* Maretron USB100 */
1586 .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
1589 /* Nokia S60 phones expose two ACM channels. The first is
1590 * a modem and is picked up by the standard AT-command
1591 * information below. The second is 'vendor-specific' but
1592 * is treated as a serial device at the S60 end, so we want
1593 * to expose it on Linux too. */
1594 { NOKIA_PCSUITE_ACM_INFO(0x042D), }, /* Nokia 3250 */
1595 { NOKIA_PCSUITE_ACM_INFO(0x04D8), }, /* Nokia 5500 Sport */
1596 { NOKIA_PCSUITE_ACM_INFO(0x04C9), }, /* Nokia E50 */
1597 { NOKIA_PCSUITE_ACM_INFO(0x0419), }, /* Nokia E60 */
1598 { NOKIA_PCSUITE_ACM_INFO(0x044D), }, /* Nokia E61 */
1599 { NOKIA_PCSUITE_ACM_INFO(0x0001), }, /* Nokia E61i */
1600 { NOKIA_PCSUITE_ACM_INFO(0x0475), }, /* Nokia E62 */
1601 { NOKIA_PCSUITE_ACM_INFO(0x0508), }, /* Nokia E65 */
1602 { NOKIA_PCSUITE_ACM_INFO(0x0418), }, /* Nokia E70 */
1603 { NOKIA_PCSUITE_ACM_INFO(0x0425), }, /* Nokia N71 */
1604 { NOKIA_PCSUITE_ACM_INFO(0x0486), }, /* Nokia N73 */
1605 { NOKIA_PCSUITE_ACM_INFO(0x04DF), }, /* Nokia N75 */
1606 { NOKIA_PCSUITE_ACM_INFO(0x000e), }, /* Nokia N77 */
1607 { NOKIA_PCSUITE_ACM_INFO(0x0445), }, /* Nokia N80 */
1608 { NOKIA_PCSUITE_ACM_INFO(0x042F), }, /* Nokia N91 & N91 8GB */
1609 { NOKIA_PCSUITE_ACM_INFO(0x048E), }, /* Nokia N92 */
1610 { NOKIA_PCSUITE_ACM_INFO(0x0420), }, /* Nokia N93 */
1611 { NOKIA_PCSUITE_ACM_INFO(0x04E6), }, /* Nokia N93i */
1612 { NOKIA_PCSUITE_ACM_INFO(0x04B2), }, /* Nokia 5700 XpressMusic */
1613 { NOKIA_PCSUITE_ACM_INFO(0x0134), }, /* Nokia 6110 Navigator (China) */
1614 { NOKIA_PCSUITE_ACM_INFO(0x046E), }, /* Nokia 6110 Navigator */
1615 { NOKIA_PCSUITE_ACM_INFO(0x002f), }, /* Nokia 6120 classic & */
1616 { NOKIA_PCSUITE_ACM_INFO(0x0088), }, /* Nokia 6121 classic */
1617 { NOKIA_PCSUITE_ACM_INFO(0x00fc), }, /* Nokia 6124 classic */
1618 { NOKIA_PCSUITE_ACM_INFO(0x0042), }, /* Nokia E51 */
1619 { NOKIA_PCSUITE_ACM_INFO(0x00b0), }, /* Nokia E66 */
1620 { NOKIA_PCSUITE_ACM_INFO(0x00ab), }, /* Nokia E71 */
1621 { NOKIA_PCSUITE_ACM_INFO(0x0481), }, /* Nokia N76 */
1622 { NOKIA_PCSUITE_ACM_INFO(0x0007), }, /* Nokia N81 & N81 8GB */
1623 { NOKIA_PCSUITE_ACM_INFO(0x0071), }, /* Nokia N82 */
1624 { NOKIA_PCSUITE_ACM_INFO(0x04F0), }, /* Nokia N95 & N95-3 NAM */
1625 { NOKIA_PCSUITE_ACM_INFO(0x0070), }, /* Nokia N95 8GB */
1626 { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
1627 { NOKIA_PCSUITE_ACM_INFO(0x0099), }, /* Nokia 6210 Navigator, RM-367 */
1628 { NOKIA_PCSUITE_ACM_INFO(0x0128), }, /* Nokia 6210 Navigator, RM-419 */
1629 { NOKIA_PCSUITE_ACM_INFO(0x008f), }, /* Nokia 6220 Classic */
1630 { NOKIA_PCSUITE_ACM_INFO(0x00a0), }, /* Nokia 6650 */
1631 { NOKIA_PCSUITE_ACM_INFO(0x007b), }, /* Nokia N78 */
1632 { NOKIA_PCSUITE_ACM_INFO(0x0094), }, /* Nokia N85 */
1633 { NOKIA_PCSUITE_ACM_INFO(0x003a), }, /* Nokia N96 & N96-3 */
1634 { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
1635 { NOKIA_PCSUITE_ACM_INFO(0x0108), }, /* Nokia 5320 XpressMusic 2G */
1636 { NOKIA_PCSUITE_ACM_INFO(0x01f5), }, /* Nokia N97, RM-505 */
1637 { NOKIA_PCSUITE_ACM_INFO(0x02e3), }, /* Nokia 5230, RM-588 */
1638 { NOKIA_PCSUITE_ACM_INFO(0x0178), }, /* Nokia E63 */
1639 { NOKIA_PCSUITE_ACM_INFO(0x010e), }, /* Nokia E75 */
1640 { NOKIA_PCSUITE_ACM_INFO(0x02d9), }, /* Nokia 6760 Slide */
1641 { NOKIA_PCSUITE_ACM_INFO(0x01d0), }, /* Nokia E52 */
1642 { NOKIA_PCSUITE_ACM_INFO(0x0223), }, /* Nokia E72 */
1643 { NOKIA_PCSUITE_ACM_INFO(0x0275), }, /* Nokia X6 */
1644 { NOKIA_PCSUITE_ACM_INFO(0x026c), }, /* Nokia N97 Mini */
1645 { NOKIA_PCSUITE_ACM_INFO(0x0154), }, /* Nokia 5800 XpressMusic */
1646 { NOKIA_PCSUITE_ACM_INFO(0x04ce), }, /* Nokia E90 */
1647 { NOKIA_PCSUITE_ACM_INFO(0x01d4), }, /* Nokia E55 */
1648 { NOKIA_PCSUITE_ACM_INFO(0x0302), }, /* Nokia N8 */
1649 { NOKIA_PCSUITE_ACM_INFO(0x0335), }, /* Nokia E7 */
1650 { NOKIA_PCSUITE_ACM_INFO(0x03cd), }, /* Nokia C7 */
1651 { SAMSUNG_PCSUITE_ACM_INFO(0x6651), }, /* Samsung GTi8510 (INNOV8) */
1653 /* Support for Owen devices */
1654 { USB_DEVICE(0x03eb, 0x0030), }, /* Owen SI30 */
1656 /* NOTE: non-Nokia COMM/ACM/0xff is likely MSFT RNDIS... NOT a modem! */
1658 /* Support Lego NXT using pbLua firmware */
1659 { USB_DEVICE(0x0694, 0xff00),
1660 .driver_info = NOT_A_MODEM,
1663 /* Support for Droids MuIn LCD */
1664 { USB_DEVICE(0x04d8, 0x000b),
1665 .driver_info = NO_DATA_INTERFACE,
1668 #if IS_ENABLED(CONFIG_INPUT_IMS_PCU)
1669 { USB_DEVICE(0x04d8, 0x0082), /* Application mode */
1670 .driver_info = IGNORE_DEVICE,
1672 { USB_DEVICE(0x04d8, 0x0083), /* Bootloader mode */
1673 .driver_info = IGNORE_DEVICE,
1675 #endif
1677 /* control interfaces without any protocol set */
1678 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1679 USB_CDC_PROTO_NONE) },
1681 /* control interfaces with various AT-command sets */
1682 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1683 USB_CDC_ACM_PROTO_AT_V25TER) },
1684 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1685 USB_CDC_ACM_PROTO_AT_PCCA101) },
1686 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1687 USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
1688 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1689 USB_CDC_ACM_PROTO_AT_GSM) },
1690 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1691 USB_CDC_ACM_PROTO_AT_3G) },
1692 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1693 USB_CDC_ACM_PROTO_AT_CDMA) },
1698 MODULE_DEVICE_TABLE(usb, acm_ids);
1700 static struct usb_driver acm_driver = {
1701 .name = "cdc_acm",
1702 .probe = acm_probe,
1703 .disconnect = acm_disconnect,
1704 #ifdef CONFIG_PM
1705 .suspend = acm_suspend,
1706 .resume = acm_resume,
1707 .reset_resume = acm_reset_resume,
1708 #endif
1709 .id_table = acm_ids,
1710 #ifdef CONFIG_PM
1711 .supports_autosuspend = 1,
1712 #endif
1713 .disable_hub_initiated_lpm = 1,
1717 * TTY driver structures.
1720 static const struct tty_operations acm_ops = {
1721 .install = acm_tty_install,
1722 .open = acm_tty_open,
1723 .close = acm_tty_close,
1724 .cleanup = acm_tty_cleanup,
1725 .hangup = acm_tty_hangup,
1726 .write = acm_tty_write,
1727 .write_room = acm_tty_write_room,
1728 .ioctl = acm_tty_ioctl,
1729 .throttle = acm_tty_throttle,
1730 .unthrottle = acm_tty_unthrottle,
1731 .chars_in_buffer = acm_tty_chars_in_buffer,
1732 .break_ctl = acm_tty_break_ctl,
1733 .set_termios = acm_tty_set_termios,
1734 .tiocmget = acm_tty_tiocmget,
1735 .tiocmset = acm_tty_tiocmset,
1739 * Init / exit.
1742 static int __init acm_init(void)
1744 int retval;
1745 acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
1746 if (!acm_tty_driver)
1747 return -ENOMEM;
1748 acm_tty_driver->driver_name = "acm",
1749 acm_tty_driver->name = "ttyACM",
1750 acm_tty_driver->major = ACM_TTY_MAJOR,
1751 acm_tty_driver->minor_start = 0,
1752 acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
1753 acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
1754 acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1755 acm_tty_driver->init_termios = tty_std_termios;
1756 acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD |
1757 HUPCL | CLOCAL;
1758 tty_set_operations(acm_tty_driver, &acm_ops);
1760 retval = tty_register_driver(acm_tty_driver);
1761 if (retval) {
1762 put_tty_driver(acm_tty_driver);
1763 return retval;
1766 retval = usb_register(&acm_driver);
1767 if (retval) {
1768 tty_unregister_driver(acm_tty_driver);
1769 put_tty_driver(acm_tty_driver);
1770 return retval;
1773 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
1775 return 0;
1778 static void __exit acm_exit(void)
1780 usb_deregister(&acm_driver);
1781 tty_unregister_driver(acm_tty_driver);
1782 put_tty_driver(acm_tty_driver);
1785 module_init(acm_init);
1786 module_exit(acm_exit);
1788 MODULE_AUTHOR(DRIVER_AUTHOR);
1789 MODULE_DESCRIPTION(DRIVER_DESC);
1790 MODULE_LICENSE("GPL");
1791 MODULE_ALIAS_CHARDEV_MAJOR(ACM_TTY_MAJOR);