Merge 3.9-rc5 into tty-next
[linux/fpc-iii.git] / drivers / usb / class / cdc-acm.c
blobc77f7ae48f1c1ed93260bea354e6c6b148a235f5
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;
219 static int acm_write_start(struct acm *acm, int wbn)
221 unsigned long flags;
222 struct acm_wb *wb = &acm->wb[wbn];
223 int rc;
225 spin_lock_irqsave(&acm->write_lock, flags);
226 if (!acm->dev) {
227 wb->use = 0;
228 spin_unlock_irqrestore(&acm->write_lock, flags);
229 return -ENODEV;
232 dev_vdbg(&acm->data->dev, "%s - susp_count %d\n", __func__,
233 acm->susp_count);
234 usb_autopm_get_interface_async(acm->control);
235 if (acm->susp_count) {
236 if (!acm->delayed_wb)
237 acm->delayed_wb = wb;
238 else
239 usb_autopm_put_interface_async(acm->control);
240 spin_unlock_irqrestore(&acm->write_lock, flags);
241 return 0; /* A white lie */
243 usb_mark_last_busy(acm->dev);
245 rc = acm_start_wb(acm, wb);
246 spin_unlock_irqrestore(&acm->write_lock, flags);
248 return rc;
252 * attributes exported through sysfs
254 static ssize_t show_caps
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->ctrl_caps);
262 static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
264 static ssize_t show_country_codes
265 (struct device *dev, struct device_attribute *attr, char *buf)
267 struct usb_interface *intf = to_usb_interface(dev);
268 struct acm *acm = usb_get_intfdata(intf);
270 memcpy(buf, acm->country_codes, acm->country_code_size);
271 return acm->country_code_size;
274 static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
276 static ssize_t show_country_rel_date
277 (struct device *dev, struct device_attribute *attr, char *buf)
279 struct usb_interface *intf = to_usb_interface(dev);
280 struct acm *acm = usb_get_intfdata(intf);
282 return sprintf(buf, "%d", acm->country_rel_date);
285 static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
287 * Interrupt handlers for various ACM device responses
290 /* control interface reports status changes with "interrupt" transfers */
291 static void acm_ctrl_irq(struct urb *urb)
293 struct acm *acm = urb->context;
294 struct usb_cdc_notification *dr = urb->transfer_buffer;
295 unsigned char *data;
296 int newctrl;
297 int retval;
298 int status = urb->status;
300 switch (status) {
301 case 0:
302 /* success */
303 break;
304 case -ECONNRESET:
305 case -ENOENT:
306 case -ESHUTDOWN:
307 /* this urb is terminated, clean up */
308 dev_dbg(&acm->control->dev,
309 "%s - urb shutting down with status: %d\n",
310 __func__, status);
311 return;
312 default:
313 dev_dbg(&acm->control->dev,
314 "%s - nonzero urb status received: %d\n",
315 __func__, status);
316 goto exit;
319 usb_mark_last_busy(acm->dev);
321 data = (unsigned char *)(dr + 1);
322 switch (dr->bNotificationType) {
323 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
324 dev_dbg(&acm->control->dev, "%s - network connection: %d\n",
325 __func__, dr->wValue);
326 break;
328 case USB_CDC_NOTIFY_SERIAL_STATE:
329 newctrl = get_unaligned_le16(data);
331 if (!acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
332 dev_dbg(&acm->control->dev, "%s - calling hangup\n",
333 __func__);
334 tty_port_tty_hangup(&acm->port, false);
337 acm->ctrlin = newctrl;
339 dev_dbg(&acm->control->dev,
340 "%s - input control lines: dcd%c dsr%c break%c "
341 "ring%c framing%c parity%c overrun%c\n",
342 __func__,
343 acm->ctrlin & ACM_CTRL_DCD ? '+' : '-',
344 acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
345 acm->ctrlin & ACM_CTRL_BRK ? '+' : '-',
346 acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
347 acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-',
348 acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
349 acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
350 break;
352 default:
353 dev_dbg(&acm->control->dev,
354 "%s - unknown notification %d received: index %d "
355 "len %d data0 %d data1 %d\n",
356 __func__,
357 dr->bNotificationType, dr->wIndex,
358 dr->wLength, data[0], data[1]);
359 break;
361 exit:
362 retval = usb_submit_urb(urb, GFP_ATOMIC);
363 if (retval)
364 dev_err(&acm->control->dev, "%s - usb_submit_urb failed: %d\n",
365 __func__, retval);
368 static int acm_submit_read_urb(struct acm *acm, int index, gfp_t mem_flags)
370 int res;
372 if (!test_and_clear_bit(index, &acm->read_urbs_free))
373 return 0;
375 dev_vdbg(&acm->data->dev, "%s - urb %d\n", __func__, index);
377 res = usb_submit_urb(acm->read_urbs[index], mem_flags);
378 if (res) {
379 if (res != -EPERM) {
380 dev_err(&acm->data->dev,
381 "%s - usb_submit_urb failed: %d\n",
382 __func__, res);
384 set_bit(index, &acm->read_urbs_free);
385 return res;
388 return 0;
391 static int acm_submit_read_urbs(struct acm *acm, gfp_t mem_flags)
393 int res;
394 int i;
396 for (i = 0; i < acm->rx_buflimit; ++i) {
397 res = acm_submit_read_urb(acm, i, mem_flags);
398 if (res)
399 return res;
402 return 0;
405 static void acm_process_read_urb(struct acm *acm, struct urb *urb)
407 if (!urb->actual_length)
408 return;
410 tty_insert_flip_string(&acm->port, urb->transfer_buffer,
411 urb->actual_length);
412 tty_flip_buffer_push(&acm->port);
415 static void acm_read_bulk_callback(struct urb *urb)
417 struct acm_rb *rb = urb->context;
418 struct acm *acm = rb->instance;
419 unsigned long flags;
421 dev_vdbg(&acm->data->dev, "%s - urb %d, len %d\n", __func__,
422 rb->index, urb->actual_length);
423 set_bit(rb->index, &acm->read_urbs_free);
425 if (!acm->dev) {
426 dev_dbg(&acm->data->dev, "%s - disconnected\n", __func__);
427 return;
429 usb_mark_last_busy(acm->dev);
431 if (urb->status) {
432 dev_dbg(&acm->data->dev, "%s - non-zero urb status: %d\n",
433 __func__, urb->status);
434 return;
436 acm_process_read_urb(acm, urb);
438 /* throttle device if requested by tty */
439 spin_lock_irqsave(&acm->read_lock, flags);
440 acm->throttled = acm->throttle_req;
441 if (!acm->throttled && !acm->susp_count) {
442 spin_unlock_irqrestore(&acm->read_lock, flags);
443 acm_submit_read_urb(acm, rb->index, GFP_ATOMIC);
444 } else {
445 spin_unlock_irqrestore(&acm->read_lock, flags);
449 /* data interface wrote those outgoing bytes */
450 static void acm_write_bulk(struct urb *urb)
452 struct acm_wb *wb = urb->context;
453 struct acm *acm = wb->instance;
454 unsigned long flags;
456 if (urb->status || (urb->actual_length != urb->transfer_buffer_length))
457 dev_vdbg(&acm->data->dev, "%s - len %d/%d, status %d\n",
458 __func__,
459 urb->actual_length,
460 urb->transfer_buffer_length,
461 urb->status);
463 spin_lock_irqsave(&acm->write_lock, flags);
464 acm_write_done(acm, wb);
465 spin_unlock_irqrestore(&acm->write_lock, flags);
466 schedule_work(&acm->work);
469 static void acm_softint(struct work_struct *work)
471 struct acm *acm = container_of(work, struct acm, work);
473 dev_vdbg(&acm->data->dev, "%s\n", __func__);
475 tty_port_tty_wakeup(&acm->port);
479 * TTY handlers
482 static int acm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
484 struct acm *acm;
485 int retval;
487 dev_dbg(tty->dev, "%s\n", __func__);
489 acm = acm_get_by_index(tty->index);
490 if (!acm)
491 return -ENODEV;
493 retval = tty_standard_install(driver, tty);
494 if (retval)
495 goto error_init_termios;
497 tty->driver_data = acm;
499 return 0;
501 error_init_termios:
502 tty_port_put(&acm->port);
503 return retval;
506 static int acm_tty_open(struct tty_struct *tty, struct file *filp)
508 struct acm *acm = tty->driver_data;
510 dev_dbg(tty->dev, "%s\n", __func__);
512 return tty_port_open(&acm->port, tty, filp);
515 static int acm_port_activate(struct tty_port *port, struct tty_struct *tty)
517 struct acm *acm = container_of(port, struct acm, port);
518 int retval = -ENODEV;
520 dev_dbg(&acm->control->dev, "%s\n", __func__);
522 mutex_lock(&acm->mutex);
523 if (acm->disconnected)
524 goto disconnected;
526 retval = usb_autopm_get_interface(acm->control);
527 if (retval)
528 goto error_get_interface;
531 * FIXME: Why do we need this? Allocating 64K of physically contiguous
532 * memory is really nasty...
534 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
535 acm->control->needs_remote_wakeup = 1;
537 acm->ctrlurb->dev = acm->dev;
538 if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
539 dev_err(&acm->control->dev,
540 "%s - usb_submit_urb(ctrl irq) failed\n", __func__);
541 goto error_submit_urb;
544 acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS;
545 if (acm_set_control(acm, acm->ctrlout) < 0 &&
546 (acm->ctrl_caps & USB_CDC_CAP_LINE))
547 goto error_set_control;
549 usb_autopm_put_interface(acm->control);
552 * Unthrottle device in case the TTY was closed while throttled.
554 spin_lock_irq(&acm->read_lock);
555 acm->throttled = 0;
556 acm->throttle_req = 0;
557 spin_unlock_irq(&acm->read_lock);
559 if (acm_submit_read_urbs(acm, GFP_KERNEL))
560 goto error_submit_read_urbs;
562 mutex_unlock(&acm->mutex);
564 return 0;
566 error_submit_read_urbs:
567 acm->ctrlout = 0;
568 acm_set_control(acm, acm->ctrlout);
569 error_set_control:
570 usb_kill_urb(acm->ctrlurb);
571 error_submit_urb:
572 usb_autopm_put_interface(acm->control);
573 error_get_interface:
574 disconnected:
575 mutex_unlock(&acm->mutex);
576 return retval;
579 static void acm_port_destruct(struct tty_port *port)
581 struct acm *acm = container_of(port, struct acm, port);
583 dev_dbg(&acm->control->dev, "%s\n", __func__);
585 acm_release_minor(acm);
586 usb_put_intf(acm->control);
587 kfree(acm->country_codes);
588 kfree(acm);
591 static void acm_port_shutdown(struct tty_port *port)
593 struct acm *acm = container_of(port, struct acm, port);
594 int i;
596 dev_dbg(&acm->control->dev, "%s\n", __func__);
598 mutex_lock(&acm->mutex);
599 if (!acm->disconnected) {
600 usb_autopm_get_interface(acm->control);
601 acm_set_control(acm, acm->ctrlout = 0);
602 usb_kill_urb(acm->ctrlurb);
603 for (i = 0; i < ACM_NW; i++)
604 usb_kill_urb(acm->wb[i].urb);
605 for (i = 0; i < acm->rx_buflimit; i++)
606 usb_kill_urb(acm->read_urbs[i]);
607 acm->control->needs_remote_wakeup = 0;
608 usb_autopm_put_interface(acm->control);
610 mutex_unlock(&acm->mutex);
613 static void acm_tty_cleanup(struct tty_struct *tty)
615 struct acm *acm = tty->driver_data;
616 dev_dbg(&acm->control->dev, "%s\n", __func__);
617 tty_port_put(&acm->port);
620 static void acm_tty_hangup(struct tty_struct *tty)
622 struct acm *acm = tty->driver_data;
623 dev_dbg(&acm->control->dev, "%s\n", __func__);
624 tty_port_hangup(&acm->port);
627 static void acm_tty_close(struct tty_struct *tty, struct file *filp)
629 struct acm *acm = tty->driver_data;
630 dev_dbg(&acm->control->dev, "%s\n", __func__);
631 tty_port_close(&acm->port, tty, filp);
634 static int acm_tty_write(struct tty_struct *tty,
635 const unsigned char *buf, int count)
637 struct acm *acm = tty->driver_data;
638 int stat;
639 unsigned long flags;
640 int wbn;
641 struct acm_wb *wb;
643 if (!count)
644 return 0;
646 dev_vdbg(&acm->data->dev, "%s - count %d\n", __func__, count);
648 spin_lock_irqsave(&acm->write_lock, flags);
649 wbn = acm_wb_alloc(acm);
650 if (wbn < 0) {
651 spin_unlock_irqrestore(&acm->write_lock, flags);
652 return 0;
654 wb = &acm->wb[wbn];
656 count = (count > acm->writesize) ? acm->writesize : count;
657 dev_vdbg(&acm->data->dev, "%s - write %d\n", __func__, count);
658 memcpy(wb->buf, buf, count);
659 wb->len = count;
660 spin_unlock_irqrestore(&acm->write_lock, flags);
662 stat = acm_write_start(acm, wbn);
663 if (stat < 0)
664 return stat;
665 return count;
668 static int acm_tty_write_room(struct tty_struct *tty)
670 struct acm *acm = tty->driver_data;
672 * Do not let the line discipline to know that we have a reserve,
673 * or it might get too enthusiastic.
675 return acm_wb_is_avail(acm) ? acm->writesize : 0;
678 static int acm_tty_chars_in_buffer(struct tty_struct *tty)
680 struct acm *acm = tty->driver_data;
682 * if the device was unplugged then any remaining characters fell out
683 * of the connector ;)
685 if (acm->disconnected)
686 return 0;
688 * This is inaccurate (overcounts), but it works.
690 return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
693 static void acm_tty_throttle(struct tty_struct *tty)
695 struct acm *acm = tty->driver_data;
697 spin_lock_irq(&acm->read_lock);
698 acm->throttle_req = 1;
699 spin_unlock_irq(&acm->read_lock);
702 static void acm_tty_unthrottle(struct tty_struct *tty)
704 struct acm *acm = tty->driver_data;
705 unsigned int was_throttled;
707 spin_lock_irq(&acm->read_lock);
708 was_throttled = acm->throttled;
709 acm->throttled = 0;
710 acm->throttle_req = 0;
711 spin_unlock_irq(&acm->read_lock);
713 if (was_throttled)
714 acm_submit_read_urbs(acm, GFP_KERNEL);
717 static int acm_tty_break_ctl(struct tty_struct *tty, int state)
719 struct acm *acm = tty->driver_data;
720 int retval;
722 retval = acm_send_break(acm, state ? 0xffff : 0);
723 if (retval < 0)
724 dev_dbg(&acm->control->dev, "%s - send break failed\n",
725 __func__);
726 return retval;
729 static int acm_tty_tiocmget(struct tty_struct *tty)
731 struct acm *acm = tty->driver_data;
733 return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
734 (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
735 (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
736 (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
737 (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
738 TIOCM_CTS;
741 static int acm_tty_tiocmset(struct tty_struct *tty,
742 unsigned int set, unsigned int clear)
744 struct acm *acm = tty->driver_data;
745 unsigned int newctrl;
747 newctrl = acm->ctrlout;
748 set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
749 (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
750 clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
751 (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
753 newctrl = (newctrl & ~clear) | set;
755 if (acm->ctrlout == newctrl)
756 return 0;
757 return acm_set_control(acm, acm->ctrlout = newctrl);
760 static int get_serial_info(struct acm *acm, struct serial_struct __user *info)
762 struct serial_struct tmp;
764 if (!info)
765 return -EINVAL;
767 memset(&tmp, 0, sizeof(tmp));
768 tmp.flags = ASYNC_LOW_LATENCY;
769 tmp.xmit_fifo_size = acm->writesize;
770 tmp.baud_base = le32_to_cpu(acm->line.dwDTERate);
771 tmp.close_delay = acm->port.close_delay / 10;
772 tmp.closing_wait = acm->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
773 ASYNC_CLOSING_WAIT_NONE :
774 acm->port.closing_wait / 10;
776 if (copy_to_user(info, &tmp, sizeof(tmp)))
777 return -EFAULT;
778 else
779 return 0;
782 static int set_serial_info(struct acm *acm,
783 struct serial_struct __user *newinfo)
785 struct serial_struct new_serial;
786 unsigned int closing_wait, close_delay;
787 int retval = 0;
789 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
790 return -EFAULT;
792 close_delay = new_serial.close_delay * 10;
793 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
794 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
796 mutex_lock(&acm->port.mutex);
798 if (!capable(CAP_SYS_ADMIN)) {
799 if ((close_delay != acm->port.close_delay) ||
800 (closing_wait != acm->port.closing_wait))
801 retval = -EPERM;
802 else
803 retval = -EOPNOTSUPP;
804 } else {
805 acm->port.close_delay = close_delay;
806 acm->port.closing_wait = closing_wait;
809 mutex_unlock(&acm->port.mutex);
810 return retval;
813 static int acm_tty_ioctl(struct tty_struct *tty,
814 unsigned int cmd, unsigned long arg)
816 struct acm *acm = tty->driver_data;
817 int rv = -ENOIOCTLCMD;
819 switch (cmd) {
820 case TIOCGSERIAL: /* gets serial port data */
821 rv = get_serial_info(acm, (struct serial_struct __user *) arg);
822 break;
823 case TIOCSSERIAL:
824 rv = set_serial_info(acm, (struct serial_struct __user *) arg);
825 break;
828 return rv;
831 static const __u32 acm_tty_speed[] = {
832 0, 50, 75, 110, 134, 150, 200, 300, 600,
833 1200, 1800, 2400, 4800, 9600, 19200, 38400,
834 57600, 115200, 230400, 460800, 500000, 576000,
835 921600, 1000000, 1152000, 1500000, 2000000,
836 2500000, 3000000, 3500000, 4000000
839 static void acm_tty_set_termios(struct tty_struct *tty,
840 struct ktermios *termios_old)
842 struct acm *acm = tty->driver_data;
843 struct ktermios *termios = &tty->termios;
844 struct usb_cdc_line_coding newline;
845 int newctrl = acm->ctrlout;
847 newline.dwDTERate = cpu_to_le32(tty_get_baud_rate(tty));
848 newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
849 newline.bParityType = termios->c_cflag & PARENB ?
850 (termios->c_cflag & PARODD ? 1 : 2) +
851 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
852 switch (termios->c_cflag & CSIZE) {
853 case CS5:
854 newline.bDataBits = 5;
855 break;
856 case CS6:
857 newline.bDataBits = 6;
858 break;
859 case CS7:
860 newline.bDataBits = 7;
861 break;
862 case CS8:
863 default:
864 newline.bDataBits = 8;
865 break;
867 /* FIXME: Needs to clear unsupported bits in the termios */
868 acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
870 if (!newline.dwDTERate) {
871 newline.dwDTERate = acm->line.dwDTERate;
872 newctrl &= ~ACM_CTRL_DTR;
873 } else
874 newctrl |= ACM_CTRL_DTR;
876 if (newctrl != acm->ctrlout)
877 acm_set_control(acm, acm->ctrlout = newctrl);
879 if (memcmp(&acm->line, &newline, sizeof newline)) {
880 memcpy(&acm->line, &newline, sizeof newline);
881 dev_dbg(&acm->control->dev, "%s - set line: %d %d %d %d\n",
882 __func__,
883 le32_to_cpu(newline.dwDTERate),
884 newline.bCharFormat, newline.bParityType,
885 newline.bDataBits);
886 acm_set_line(acm, &acm->line);
890 static const struct tty_port_operations acm_port_ops = {
891 .shutdown = acm_port_shutdown,
892 .activate = acm_port_activate,
893 .destruct = acm_port_destruct,
897 * USB probe and disconnect routines.
900 /* Little helpers: write/read buffers free */
901 static void acm_write_buffers_free(struct acm *acm)
903 int i;
904 struct acm_wb *wb;
905 struct usb_device *usb_dev = interface_to_usbdev(acm->control);
907 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++)
908 usb_free_coherent(usb_dev, acm->writesize, wb->buf, wb->dmah);
911 static void acm_read_buffers_free(struct acm *acm)
913 struct usb_device *usb_dev = interface_to_usbdev(acm->control);
914 int i;
916 for (i = 0; i < acm->rx_buflimit; i++)
917 usb_free_coherent(usb_dev, acm->readsize,
918 acm->read_buffers[i].base, acm->read_buffers[i].dma);
921 /* Little helper: write buffers allocate */
922 static int acm_write_buffers_alloc(struct acm *acm)
924 int i;
925 struct acm_wb *wb;
927 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
928 wb->buf = usb_alloc_coherent(acm->dev, acm->writesize, GFP_KERNEL,
929 &wb->dmah);
930 if (!wb->buf) {
931 while (i != 0) {
932 --i;
933 --wb;
934 usb_free_coherent(acm->dev, acm->writesize,
935 wb->buf, wb->dmah);
937 return -ENOMEM;
940 return 0;
943 static int acm_probe(struct usb_interface *intf,
944 const struct usb_device_id *id)
946 struct usb_cdc_union_desc *union_header = NULL;
947 struct usb_cdc_country_functional_desc *cfd = NULL;
948 unsigned char *buffer = intf->altsetting->extra;
949 int buflen = intf->altsetting->extralen;
950 struct usb_interface *control_interface;
951 struct usb_interface *data_interface;
952 struct usb_endpoint_descriptor *epctrl = NULL;
953 struct usb_endpoint_descriptor *epread = NULL;
954 struct usb_endpoint_descriptor *epwrite = NULL;
955 struct usb_device *usb_dev = interface_to_usbdev(intf);
956 struct acm *acm;
957 int minor;
958 int ctrlsize, readsize;
959 u8 *buf;
960 u8 ac_management_function = 0;
961 u8 call_management_function = 0;
962 int call_interface_num = -1;
963 int data_interface_num = -1;
964 unsigned long quirks;
965 int num_rx_buf;
966 int i;
967 int combined_interfaces = 0;
968 struct device *tty_dev;
969 int rv = -ENOMEM;
971 /* normal quirks */
972 quirks = (unsigned long)id->driver_info;
973 num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
975 /* handle quirks deadly to normal probing*/
976 if (quirks == NO_UNION_NORMAL) {
977 data_interface = usb_ifnum_to_if(usb_dev, 1);
978 control_interface = usb_ifnum_to_if(usb_dev, 0);
979 goto skip_normal_probe;
982 /* normal probing*/
983 if (!buffer) {
984 dev_err(&intf->dev, "Weird descriptor references\n");
985 return -EINVAL;
988 if (!buflen) {
989 if (intf->cur_altsetting->endpoint &&
990 intf->cur_altsetting->endpoint->extralen &&
991 intf->cur_altsetting->endpoint->extra) {
992 dev_dbg(&intf->dev,
993 "Seeking extra descriptors on endpoint\n");
994 buflen = intf->cur_altsetting->endpoint->extralen;
995 buffer = intf->cur_altsetting->endpoint->extra;
996 } else {
997 dev_err(&intf->dev,
998 "Zero length descriptor references\n");
999 return -EINVAL;
1003 while (buflen > 0) {
1004 if (buffer[1] != USB_DT_CS_INTERFACE) {
1005 dev_err(&intf->dev, "skipping garbage\n");
1006 goto next_desc;
1009 switch (buffer[2]) {
1010 case USB_CDC_UNION_TYPE: /* we've found it */
1011 if (union_header) {
1012 dev_err(&intf->dev, "More than one "
1013 "union descriptor, skipping ...\n");
1014 goto next_desc;
1016 union_header = (struct usb_cdc_union_desc *)buffer;
1017 break;
1018 case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
1019 cfd = (struct usb_cdc_country_functional_desc *)buffer;
1020 break;
1021 case USB_CDC_HEADER_TYPE: /* maybe check version */
1022 break; /* for now we ignore it */
1023 case USB_CDC_ACM_TYPE:
1024 ac_management_function = buffer[3];
1025 break;
1026 case USB_CDC_CALL_MANAGEMENT_TYPE:
1027 call_management_function = buffer[3];
1028 call_interface_num = buffer[4];
1029 if ((quirks & NOT_A_MODEM) == 0 && (call_management_function & 3) != 3)
1030 dev_err(&intf->dev, "This device cannot do calls on its own. It is not a modem.\n");
1031 break;
1032 default:
1033 /* there are LOTS more CDC descriptors that
1034 * could legitimately be found here.
1036 dev_dbg(&intf->dev, "Ignoring descriptor: "
1037 "type %02x, length %d\n",
1038 buffer[2], buffer[0]);
1039 break;
1041 next_desc:
1042 buflen -= buffer[0];
1043 buffer += buffer[0];
1046 if (!union_header) {
1047 if (call_interface_num > 0) {
1048 dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n");
1049 /* quirks for Droids MuIn LCD */
1050 if (quirks & NO_DATA_INTERFACE)
1051 data_interface = usb_ifnum_to_if(usb_dev, 0);
1052 else
1053 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
1054 control_interface = intf;
1055 } else {
1056 if (intf->cur_altsetting->desc.bNumEndpoints != 3) {
1057 dev_dbg(&intf->dev,"No union descriptor, giving up\n");
1058 return -ENODEV;
1059 } else {
1060 dev_warn(&intf->dev,"No union descriptor, testing for castrated device\n");
1061 combined_interfaces = 1;
1062 control_interface = data_interface = intf;
1063 goto look_for_collapsed_interface;
1066 } else {
1067 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
1068 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
1069 if (!control_interface || !data_interface) {
1070 dev_dbg(&intf->dev, "no interfaces\n");
1071 return -ENODEV;
1075 if (data_interface_num != call_interface_num)
1076 dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n");
1078 if (control_interface == data_interface) {
1079 /* some broken devices designed for windows work this way */
1080 dev_warn(&intf->dev,"Control and data interfaces are not separated!\n");
1081 combined_interfaces = 1;
1082 /* a popular other OS doesn't use it */
1083 quirks |= NO_CAP_LINE;
1084 if (data_interface->cur_altsetting->desc.bNumEndpoints != 3) {
1085 dev_err(&intf->dev, "This needs exactly 3 endpoints\n");
1086 return -EINVAL;
1088 look_for_collapsed_interface:
1089 for (i = 0; i < 3; i++) {
1090 struct usb_endpoint_descriptor *ep;
1091 ep = &data_interface->cur_altsetting->endpoint[i].desc;
1093 if (usb_endpoint_is_int_in(ep))
1094 epctrl = ep;
1095 else if (usb_endpoint_is_bulk_out(ep))
1096 epwrite = ep;
1097 else if (usb_endpoint_is_bulk_in(ep))
1098 epread = ep;
1099 else
1100 return -EINVAL;
1102 if (!epctrl || !epread || !epwrite)
1103 return -ENODEV;
1104 else
1105 goto made_compressed_probe;
1108 skip_normal_probe:
1110 /*workaround for switched interfaces */
1111 if (data_interface->cur_altsetting->desc.bInterfaceClass
1112 != CDC_DATA_INTERFACE_TYPE) {
1113 if (control_interface->cur_altsetting->desc.bInterfaceClass
1114 == CDC_DATA_INTERFACE_TYPE) {
1115 struct usb_interface *t;
1116 dev_dbg(&intf->dev,
1117 "Your device has switched interfaces.\n");
1118 t = control_interface;
1119 control_interface = data_interface;
1120 data_interface = t;
1121 } else {
1122 return -EINVAL;
1126 /* Accept probe requests only for the control interface */
1127 if (!combined_interfaces && intf != control_interface)
1128 return -ENODEV;
1130 if (!combined_interfaces && usb_interface_claimed(data_interface)) {
1131 /* valid in this context */
1132 dev_dbg(&intf->dev, "The data interface isn't available\n");
1133 return -EBUSY;
1137 if (data_interface->cur_altsetting->desc.bNumEndpoints < 2 ||
1138 control_interface->cur_altsetting->desc.bNumEndpoints == 0)
1139 return -EINVAL;
1141 epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
1142 epread = &data_interface->cur_altsetting->endpoint[0].desc;
1143 epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
1146 /* workaround for switched endpoints */
1147 if (!usb_endpoint_dir_in(epread)) {
1148 /* descriptors are swapped */
1149 struct usb_endpoint_descriptor *t;
1150 dev_dbg(&intf->dev,
1151 "The data interface has switched endpoints\n");
1152 t = epread;
1153 epread = epwrite;
1154 epwrite = t;
1156 made_compressed_probe:
1157 dev_dbg(&intf->dev, "interfaces are valid\n");
1159 acm = kzalloc(sizeof(struct acm), GFP_KERNEL);
1160 if (acm == NULL) {
1161 dev_err(&intf->dev, "out of memory (acm kzalloc)\n");
1162 goto alloc_fail;
1165 minor = acm_alloc_minor(acm);
1166 if (minor == ACM_TTY_MINORS) {
1167 dev_err(&intf->dev, "no more free acm devices\n");
1168 kfree(acm);
1169 return -ENODEV;
1172 ctrlsize = usb_endpoint_maxp(epctrl);
1173 readsize = usb_endpoint_maxp(epread) *
1174 (quirks == SINGLE_RX_URB ? 1 : 2);
1175 acm->combined_interfaces = combined_interfaces;
1176 acm->writesize = usb_endpoint_maxp(epwrite) * 20;
1177 acm->control = control_interface;
1178 acm->data = data_interface;
1179 acm->minor = minor;
1180 acm->dev = usb_dev;
1181 acm->ctrl_caps = ac_management_function;
1182 if (quirks & NO_CAP_LINE)
1183 acm->ctrl_caps &= ~USB_CDC_CAP_LINE;
1184 acm->ctrlsize = ctrlsize;
1185 acm->readsize = readsize;
1186 acm->rx_buflimit = num_rx_buf;
1187 INIT_WORK(&acm->work, acm_softint);
1188 spin_lock_init(&acm->write_lock);
1189 spin_lock_init(&acm->read_lock);
1190 mutex_init(&acm->mutex);
1191 acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
1192 acm->is_int_ep = usb_endpoint_xfer_int(epread);
1193 if (acm->is_int_ep)
1194 acm->bInterval = epread->bInterval;
1195 tty_port_init(&acm->port);
1196 acm->port.ops = &acm_port_ops;
1198 buf = usb_alloc_coherent(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
1199 if (!buf) {
1200 dev_err(&intf->dev, "out of memory (ctrl buffer alloc)\n");
1201 goto alloc_fail2;
1203 acm->ctrl_buffer = buf;
1205 if (acm_write_buffers_alloc(acm) < 0) {
1206 dev_err(&intf->dev, "out of memory (write buffer alloc)\n");
1207 goto alloc_fail4;
1210 acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
1211 if (!acm->ctrlurb) {
1212 dev_err(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
1213 goto alloc_fail5;
1215 for (i = 0; i < num_rx_buf; i++) {
1216 struct acm_rb *rb = &(acm->read_buffers[i]);
1217 struct urb *urb;
1219 rb->base = usb_alloc_coherent(acm->dev, readsize, GFP_KERNEL,
1220 &rb->dma);
1221 if (!rb->base) {
1222 dev_err(&intf->dev, "out of memory "
1223 "(read bufs usb_alloc_coherent)\n");
1224 goto alloc_fail6;
1226 rb->index = i;
1227 rb->instance = acm;
1229 urb = usb_alloc_urb(0, GFP_KERNEL);
1230 if (!urb) {
1231 dev_err(&intf->dev,
1232 "out of memory (read urbs usb_alloc_urb)\n");
1233 goto alloc_fail6;
1235 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1236 urb->transfer_dma = rb->dma;
1237 if (acm->is_int_ep) {
1238 usb_fill_int_urb(urb, acm->dev,
1239 acm->rx_endpoint,
1240 rb->base,
1241 acm->readsize,
1242 acm_read_bulk_callback, rb,
1243 acm->bInterval);
1244 } else {
1245 usb_fill_bulk_urb(urb, acm->dev,
1246 acm->rx_endpoint,
1247 rb->base,
1248 acm->readsize,
1249 acm_read_bulk_callback, rb);
1252 acm->read_urbs[i] = urb;
1253 __set_bit(i, &acm->read_urbs_free);
1255 for (i = 0; i < ACM_NW; i++) {
1256 struct acm_wb *snd = &(acm->wb[i]);
1258 snd->urb = usb_alloc_urb(0, GFP_KERNEL);
1259 if (snd->urb == NULL) {
1260 dev_err(&intf->dev,
1261 "out of memory (write urbs usb_alloc_urb)\n");
1262 goto alloc_fail7;
1265 if (usb_endpoint_xfer_int(epwrite))
1266 usb_fill_int_urb(snd->urb, usb_dev,
1267 usb_sndintpipe(usb_dev, epwrite->bEndpointAddress),
1268 NULL, acm->writesize, acm_write_bulk, snd, epwrite->bInterval);
1269 else
1270 usb_fill_bulk_urb(snd->urb, usb_dev,
1271 usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
1272 NULL, acm->writesize, acm_write_bulk, snd);
1273 snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1274 snd->instance = acm;
1277 usb_set_intfdata(intf, acm);
1279 i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
1280 if (i < 0)
1281 goto alloc_fail7;
1283 if (cfd) { /* export the country data */
1284 acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
1285 if (!acm->country_codes)
1286 goto skip_countries;
1287 acm->country_code_size = cfd->bLength - 4;
1288 memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0,
1289 cfd->bLength - 4);
1290 acm->country_rel_date = cfd->iCountryCodeRelDate;
1292 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
1293 if (i < 0) {
1294 kfree(acm->country_codes);
1295 acm->country_codes = NULL;
1296 acm->country_code_size = 0;
1297 goto skip_countries;
1300 i = device_create_file(&intf->dev,
1301 &dev_attr_iCountryCodeRelDate);
1302 if (i < 0) {
1303 device_remove_file(&intf->dev, &dev_attr_wCountryCodes);
1304 kfree(acm->country_codes);
1305 acm->country_codes = NULL;
1306 acm->country_code_size = 0;
1307 goto skip_countries;
1311 skip_countries:
1312 usb_fill_int_urb(acm->ctrlurb, usb_dev,
1313 usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
1314 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm,
1315 /* works around buggy devices */
1316 epctrl->bInterval ? epctrl->bInterval : 0xff);
1317 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1318 acm->ctrlurb->transfer_dma = acm->ctrl_dma;
1320 dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
1322 acm_set_control(acm, acm->ctrlout);
1324 acm->line.dwDTERate = cpu_to_le32(9600);
1325 acm->line.bDataBits = 8;
1326 acm_set_line(acm, &acm->line);
1328 usb_driver_claim_interface(&acm_driver, data_interface, acm);
1329 usb_set_intfdata(data_interface, acm);
1331 usb_get_intf(control_interface);
1332 tty_dev = tty_port_register_device(&acm->port, acm_tty_driver, minor,
1333 &control_interface->dev);
1334 if (IS_ERR(tty_dev)) {
1335 rv = PTR_ERR(tty_dev);
1336 goto alloc_fail8;
1339 return 0;
1340 alloc_fail8:
1341 if (acm->country_codes) {
1342 device_remove_file(&acm->control->dev,
1343 &dev_attr_wCountryCodes);
1344 device_remove_file(&acm->control->dev,
1345 &dev_attr_iCountryCodeRelDate);
1347 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
1348 alloc_fail7:
1349 usb_set_intfdata(intf, NULL);
1350 for (i = 0; i < ACM_NW; i++)
1351 usb_free_urb(acm->wb[i].urb);
1352 alloc_fail6:
1353 for (i = 0; i < num_rx_buf; i++)
1354 usb_free_urb(acm->read_urbs[i]);
1355 acm_read_buffers_free(acm);
1356 usb_free_urb(acm->ctrlurb);
1357 alloc_fail5:
1358 acm_write_buffers_free(acm);
1359 alloc_fail4:
1360 usb_free_coherent(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1361 alloc_fail2:
1362 acm_release_minor(acm);
1363 kfree(acm);
1364 alloc_fail:
1365 return rv;
1368 static void stop_data_traffic(struct acm *acm)
1370 int i;
1372 dev_dbg(&acm->control->dev, "%s\n", __func__);
1374 usb_kill_urb(acm->ctrlurb);
1375 for (i = 0; i < ACM_NW; i++)
1376 usb_kill_urb(acm->wb[i].urb);
1377 for (i = 0; i < acm->rx_buflimit; i++)
1378 usb_kill_urb(acm->read_urbs[i]);
1380 cancel_work_sync(&acm->work);
1383 static void acm_disconnect(struct usb_interface *intf)
1385 struct acm *acm = usb_get_intfdata(intf);
1386 struct usb_device *usb_dev = interface_to_usbdev(intf);
1387 struct tty_struct *tty;
1388 int i;
1390 dev_dbg(&intf->dev, "%s\n", __func__);
1392 /* sibling interface is already cleaning up */
1393 if (!acm)
1394 return;
1396 mutex_lock(&acm->mutex);
1397 acm->disconnected = true;
1398 if (acm->country_codes) {
1399 device_remove_file(&acm->control->dev,
1400 &dev_attr_wCountryCodes);
1401 device_remove_file(&acm->control->dev,
1402 &dev_attr_iCountryCodeRelDate);
1404 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
1405 usb_set_intfdata(acm->control, NULL);
1406 usb_set_intfdata(acm->data, NULL);
1407 mutex_unlock(&acm->mutex);
1409 tty = tty_port_tty_get(&acm->port);
1410 if (tty) {
1411 tty_vhangup(tty);
1412 tty_kref_put(tty);
1415 stop_data_traffic(acm);
1417 tty_unregister_device(acm_tty_driver, acm->minor);
1419 usb_free_urb(acm->ctrlurb);
1420 for (i = 0; i < ACM_NW; i++)
1421 usb_free_urb(acm->wb[i].urb);
1422 for (i = 0; i < acm->rx_buflimit; i++)
1423 usb_free_urb(acm->read_urbs[i]);
1424 acm_write_buffers_free(acm);
1425 usb_free_coherent(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1426 acm_read_buffers_free(acm);
1428 if (!acm->combined_interfaces)
1429 usb_driver_release_interface(&acm_driver, intf == acm->control ?
1430 acm->data : acm->control);
1432 tty_port_put(&acm->port);
1435 #ifdef CONFIG_PM
1436 static int acm_suspend(struct usb_interface *intf, pm_message_t message)
1438 struct acm *acm = usb_get_intfdata(intf);
1439 int cnt;
1441 if (PMSG_IS_AUTO(message)) {
1442 int b;
1444 spin_lock_irq(&acm->write_lock);
1445 b = acm->transmitting;
1446 spin_unlock_irq(&acm->write_lock);
1447 if (b)
1448 return -EBUSY;
1451 spin_lock_irq(&acm->read_lock);
1452 spin_lock(&acm->write_lock);
1453 cnt = acm->susp_count++;
1454 spin_unlock(&acm->write_lock);
1455 spin_unlock_irq(&acm->read_lock);
1457 if (cnt)
1458 return 0;
1460 if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags))
1461 stop_data_traffic(acm);
1463 return 0;
1466 static int acm_resume(struct usb_interface *intf)
1468 struct acm *acm = usb_get_intfdata(intf);
1469 struct acm_wb *wb;
1470 int rv = 0;
1471 int cnt;
1473 spin_lock_irq(&acm->read_lock);
1474 acm->susp_count -= 1;
1475 cnt = acm->susp_count;
1476 spin_unlock_irq(&acm->read_lock);
1478 if (cnt)
1479 return 0;
1481 if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags)) {
1482 rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO);
1484 spin_lock_irq(&acm->write_lock);
1485 if (acm->delayed_wb) {
1486 wb = acm->delayed_wb;
1487 acm->delayed_wb = NULL;
1488 spin_unlock_irq(&acm->write_lock);
1489 acm_start_wb(acm, wb);
1490 } else {
1491 spin_unlock_irq(&acm->write_lock);
1495 * delayed error checking because we must
1496 * do the write path at all cost
1498 if (rv < 0)
1499 goto err_out;
1501 rv = acm_submit_read_urbs(acm, GFP_NOIO);
1504 err_out:
1505 return rv;
1508 static int acm_reset_resume(struct usb_interface *intf)
1510 struct acm *acm = usb_get_intfdata(intf);
1512 if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags))
1513 tty_port_tty_hangup(&acm->port, false);
1515 return acm_resume(intf);
1518 #endif /* CONFIG_PM */
1520 #define NOKIA_PCSUITE_ACM_INFO(x) \
1521 USB_DEVICE_AND_INTERFACE_INFO(0x0421, x, \
1522 USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
1523 USB_CDC_ACM_PROTO_VENDOR)
1525 #define SAMSUNG_PCSUITE_ACM_INFO(x) \
1526 USB_DEVICE_AND_INTERFACE_INFO(0x04e7, x, \
1527 USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
1528 USB_CDC_ACM_PROTO_VENDOR)
1531 * USB driver structure.
1534 static const struct usb_device_id acm_ids[] = {
1535 /* quirky and broken devices */
1536 { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
1537 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1539 { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
1540 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1542 { USB_DEVICE(0x0e8d, 0x3329), /* MediaTek Inc GPS */
1543 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1545 { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
1546 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1548 { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
1549 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1551 { USB_DEVICE(0x0ace, 0x1602), /* ZyDAS 56K USB MODEM */
1552 .driver_info = SINGLE_RX_URB,
1554 { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
1555 .driver_info = SINGLE_RX_URB, /* firmware bug */
1557 { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
1558 .driver_info = SINGLE_RX_URB, /* firmware bug */
1560 { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
1561 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1563 { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
1564 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1566 { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */
1567 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1569 { USB_DEVICE(0x0572, 0x1324), /* Conexant USB MODEM RD02-D400 */
1570 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1572 { USB_DEVICE(0x0572, 0x1328), /* Shiro / Aztech USB MODEM UM-3100 */
1573 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1575 { USB_DEVICE(0x22b8, 0x6425), /* Motorola MOTOMAGX phones */
1577 /* Motorola H24 HSPA module: */
1578 { USB_DEVICE(0x22b8, 0x2d91) }, /* modem */
1579 { USB_DEVICE(0x22b8, 0x2d92) }, /* modem + diagnostics */
1580 { USB_DEVICE(0x22b8, 0x2d93) }, /* modem + AT port */
1581 { USB_DEVICE(0x22b8, 0x2d95) }, /* modem + AT port + diagnostics */
1582 { USB_DEVICE(0x22b8, 0x2d96) }, /* modem + NMEA */
1583 { USB_DEVICE(0x22b8, 0x2d97) }, /* modem + diagnostics + NMEA */
1584 { USB_DEVICE(0x22b8, 0x2d99) }, /* modem + AT port + NMEA */
1585 { USB_DEVICE(0x22b8, 0x2d9a) }, /* modem + AT port + diagnostics + NMEA */
1587 { USB_DEVICE(0x0572, 0x1329), /* Hummingbird huc56s (Conexant) */
1588 .driver_info = NO_UNION_NORMAL, /* union descriptor misplaced on
1589 data interface instead of
1590 communications interface.
1591 Maybe we should define a new
1592 quirk for this. */
1594 { USB_DEVICE(0x0572, 0x1340), /* Conexant CX93010-2x UCMxx */
1595 .driver_info = NO_UNION_NORMAL,
1597 { USB_DEVICE(0x05f9, 0x4002), /* PSC Scanning, Magellan 800i */
1598 .driver_info = NO_UNION_NORMAL,
1600 { USB_DEVICE(0x1bbb, 0x0003), /* Alcatel OT-I650 */
1601 .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
1603 { USB_DEVICE(0x1576, 0x03b1), /* Maretron USB100 */
1604 .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
1607 /* Nokia S60 phones expose two ACM channels. The first is
1608 * a modem and is picked up by the standard AT-command
1609 * information below. The second is 'vendor-specific' but
1610 * is treated as a serial device at the S60 end, so we want
1611 * to expose it on Linux too. */
1612 { NOKIA_PCSUITE_ACM_INFO(0x042D), }, /* Nokia 3250 */
1613 { NOKIA_PCSUITE_ACM_INFO(0x04D8), }, /* Nokia 5500 Sport */
1614 { NOKIA_PCSUITE_ACM_INFO(0x04C9), }, /* Nokia E50 */
1615 { NOKIA_PCSUITE_ACM_INFO(0x0419), }, /* Nokia E60 */
1616 { NOKIA_PCSUITE_ACM_INFO(0x044D), }, /* Nokia E61 */
1617 { NOKIA_PCSUITE_ACM_INFO(0x0001), }, /* Nokia E61i */
1618 { NOKIA_PCSUITE_ACM_INFO(0x0475), }, /* Nokia E62 */
1619 { NOKIA_PCSUITE_ACM_INFO(0x0508), }, /* Nokia E65 */
1620 { NOKIA_PCSUITE_ACM_INFO(0x0418), }, /* Nokia E70 */
1621 { NOKIA_PCSUITE_ACM_INFO(0x0425), }, /* Nokia N71 */
1622 { NOKIA_PCSUITE_ACM_INFO(0x0486), }, /* Nokia N73 */
1623 { NOKIA_PCSUITE_ACM_INFO(0x04DF), }, /* Nokia N75 */
1624 { NOKIA_PCSUITE_ACM_INFO(0x000e), }, /* Nokia N77 */
1625 { NOKIA_PCSUITE_ACM_INFO(0x0445), }, /* Nokia N80 */
1626 { NOKIA_PCSUITE_ACM_INFO(0x042F), }, /* Nokia N91 & N91 8GB */
1627 { NOKIA_PCSUITE_ACM_INFO(0x048E), }, /* Nokia N92 */
1628 { NOKIA_PCSUITE_ACM_INFO(0x0420), }, /* Nokia N93 */
1629 { NOKIA_PCSUITE_ACM_INFO(0x04E6), }, /* Nokia N93i */
1630 { NOKIA_PCSUITE_ACM_INFO(0x04B2), }, /* Nokia 5700 XpressMusic */
1631 { NOKIA_PCSUITE_ACM_INFO(0x0134), }, /* Nokia 6110 Navigator (China) */
1632 { NOKIA_PCSUITE_ACM_INFO(0x046E), }, /* Nokia 6110 Navigator */
1633 { NOKIA_PCSUITE_ACM_INFO(0x002f), }, /* Nokia 6120 classic & */
1634 { NOKIA_PCSUITE_ACM_INFO(0x0088), }, /* Nokia 6121 classic */
1635 { NOKIA_PCSUITE_ACM_INFO(0x00fc), }, /* Nokia 6124 classic */
1636 { NOKIA_PCSUITE_ACM_INFO(0x0042), }, /* Nokia E51 */
1637 { NOKIA_PCSUITE_ACM_INFO(0x00b0), }, /* Nokia E66 */
1638 { NOKIA_PCSUITE_ACM_INFO(0x00ab), }, /* Nokia E71 */
1639 { NOKIA_PCSUITE_ACM_INFO(0x0481), }, /* Nokia N76 */
1640 { NOKIA_PCSUITE_ACM_INFO(0x0007), }, /* Nokia N81 & N81 8GB */
1641 { NOKIA_PCSUITE_ACM_INFO(0x0071), }, /* Nokia N82 */
1642 { NOKIA_PCSUITE_ACM_INFO(0x04F0), }, /* Nokia N95 & N95-3 NAM */
1643 { NOKIA_PCSUITE_ACM_INFO(0x0070), }, /* Nokia N95 8GB */
1644 { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
1645 { NOKIA_PCSUITE_ACM_INFO(0x0099), }, /* Nokia 6210 Navigator, RM-367 */
1646 { NOKIA_PCSUITE_ACM_INFO(0x0128), }, /* Nokia 6210 Navigator, RM-419 */
1647 { NOKIA_PCSUITE_ACM_INFO(0x008f), }, /* Nokia 6220 Classic */
1648 { NOKIA_PCSUITE_ACM_INFO(0x00a0), }, /* Nokia 6650 */
1649 { NOKIA_PCSUITE_ACM_INFO(0x007b), }, /* Nokia N78 */
1650 { NOKIA_PCSUITE_ACM_INFO(0x0094), }, /* Nokia N85 */
1651 { NOKIA_PCSUITE_ACM_INFO(0x003a), }, /* Nokia N96 & N96-3 */
1652 { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
1653 { NOKIA_PCSUITE_ACM_INFO(0x0108), }, /* Nokia 5320 XpressMusic 2G */
1654 { NOKIA_PCSUITE_ACM_INFO(0x01f5), }, /* Nokia N97, RM-505 */
1655 { NOKIA_PCSUITE_ACM_INFO(0x02e3), }, /* Nokia 5230, RM-588 */
1656 { NOKIA_PCSUITE_ACM_INFO(0x0178), }, /* Nokia E63 */
1657 { NOKIA_PCSUITE_ACM_INFO(0x010e), }, /* Nokia E75 */
1658 { NOKIA_PCSUITE_ACM_INFO(0x02d9), }, /* Nokia 6760 Slide */
1659 { NOKIA_PCSUITE_ACM_INFO(0x01d0), }, /* Nokia E52 */
1660 { NOKIA_PCSUITE_ACM_INFO(0x0223), }, /* Nokia E72 */
1661 { NOKIA_PCSUITE_ACM_INFO(0x0275), }, /* Nokia X6 */
1662 { NOKIA_PCSUITE_ACM_INFO(0x026c), }, /* Nokia N97 Mini */
1663 { NOKIA_PCSUITE_ACM_INFO(0x0154), }, /* Nokia 5800 XpressMusic */
1664 { NOKIA_PCSUITE_ACM_INFO(0x04ce), }, /* Nokia E90 */
1665 { NOKIA_PCSUITE_ACM_INFO(0x01d4), }, /* Nokia E55 */
1666 { NOKIA_PCSUITE_ACM_INFO(0x0302), }, /* Nokia N8 */
1667 { NOKIA_PCSUITE_ACM_INFO(0x0335), }, /* Nokia E7 */
1668 { NOKIA_PCSUITE_ACM_INFO(0x03cd), }, /* Nokia C7 */
1669 { SAMSUNG_PCSUITE_ACM_INFO(0x6651), }, /* Samsung GTi8510 (INNOV8) */
1671 /* Support for Owen devices */
1672 { USB_DEVICE(0x03eb, 0x0030), }, /* Owen SI30 */
1674 /* NOTE: non-Nokia COMM/ACM/0xff is likely MSFT RNDIS... NOT a modem! */
1676 /* Support Lego NXT using pbLua firmware */
1677 { USB_DEVICE(0x0694, 0xff00),
1678 .driver_info = NOT_A_MODEM,
1681 /* Support for Droids MuIn LCD */
1682 { USB_DEVICE(0x04d8, 0x000b),
1683 .driver_info = NO_DATA_INTERFACE,
1686 /* control interfaces without any protocol set */
1687 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1688 USB_CDC_PROTO_NONE) },
1690 /* control interfaces with various AT-command sets */
1691 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1692 USB_CDC_ACM_PROTO_AT_V25TER) },
1693 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1694 USB_CDC_ACM_PROTO_AT_PCCA101) },
1695 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1696 USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
1697 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1698 USB_CDC_ACM_PROTO_AT_GSM) },
1699 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1700 USB_CDC_ACM_PROTO_AT_3G) },
1701 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1702 USB_CDC_ACM_PROTO_AT_CDMA) },
1707 MODULE_DEVICE_TABLE(usb, acm_ids);
1709 static struct usb_driver acm_driver = {
1710 .name = "cdc_acm",
1711 .probe = acm_probe,
1712 .disconnect = acm_disconnect,
1713 #ifdef CONFIG_PM
1714 .suspend = acm_suspend,
1715 .resume = acm_resume,
1716 .reset_resume = acm_reset_resume,
1717 #endif
1718 .id_table = acm_ids,
1719 #ifdef CONFIG_PM
1720 .supports_autosuspend = 1,
1721 #endif
1722 .disable_hub_initiated_lpm = 1,
1726 * TTY driver structures.
1729 static const struct tty_operations acm_ops = {
1730 .install = acm_tty_install,
1731 .open = acm_tty_open,
1732 .close = acm_tty_close,
1733 .cleanup = acm_tty_cleanup,
1734 .hangup = acm_tty_hangup,
1735 .write = acm_tty_write,
1736 .write_room = acm_tty_write_room,
1737 .ioctl = acm_tty_ioctl,
1738 .throttle = acm_tty_throttle,
1739 .unthrottle = acm_tty_unthrottle,
1740 .chars_in_buffer = acm_tty_chars_in_buffer,
1741 .break_ctl = acm_tty_break_ctl,
1742 .set_termios = acm_tty_set_termios,
1743 .tiocmget = acm_tty_tiocmget,
1744 .tiocmset = acm_tty_tiocmset,
1748 * Init / exit.
1751 static int __init acm_init(void)
1753 int retval;
1754 acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
1755 if (!acm_tty_driver)
1756 return -ENOMEM;
1757 acm_tty_driver->driver_name = "acm",
1758 acm_tty_driver->name = "ttyACM",
1759 acm_tty_driver->major = ACM_TTY_MAJOR,
1760 acm_tty_driver->minor_start = 0,
1761 acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
1762 acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
1763 acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1764 acm_tty_driver->init_termios = tty_std_termios;
1765 acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD |
1766 HUPCL | CLOCAL;
1767 tty_set_operations(acm_tty_driver, &acm_ops);
1769 retval = tty_register_driver(acm_tty_driver);
1770 if (retval) {
1771 put_tty_driver(acm_tty_driver);
1772 return retval;
1775 retval = usb_register(&acm_driver);
1776 if (retval) {
1777 tty_unregister_driver(acm_tty_driver);
1778 put_tty_driver(acm_tty_driver);
1779 return retval;
1782 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
1784 return 0;
1787 static void __exit acm_exit(void)
1789 usb_deregister(&acm_driver);
1790 tty_unregister_driver(acm_tty_driver);
1791 put_tty_driver(acm_tty_driver);
1794 module_init(acm_init);
1795 module_exit(acm_exit);
1797 MODULE_AUTHOR(DRIVER_AUTHOR);
1798 MODULE_DESCRIPTION(DRIVER_DESC);
1799 MODULE_LICENSE("GPL");
1800 MODULE_ALIAS_CHARDEV_MAJOR(ACM_TTY_MAJOR);