4 * Copyright (c) 1999 Armin Fuerst <fuerst@in.tum.de>
5 * Copyright (c) 1999 Pavel Machek <pavel@suse.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>
11 * USB Abstract Control Model driver for USB modems and ISDN adapters
16 * v0.9 - thorough cleaning, URBification, almost a rewrite
17 * v0.10 - some more cleanups
18 * v0.11 - fixed flow control, read error doesn't stop reads
19 * v0.12 - added TIOCM ioctls, added break handling, made struct acm kmalloced
20 * v0.13 - added termios, added hangup
21 * v0.14 - sized down struct acm
22 * v0.15 - fixed flow control again - characters could be lost
23 * v0.16 - added code for modems with swapped data and control interfaces
24 * v0.17 - added new style probing
25 * v0.18 - fixed new style probing for devices with more configurations
26 * v0.19 - fixed CLOCAL handling (thanks to Richard Shih-Ping Chan)
27 * v0.20 - switched to probing on interface (rather than device) class
28 * v0.21 - revert to probing on device for devices with multiple configs
29 * v0.22 - probe only the control interface. if usbcore doesn't choose the
30 * config we want, sysadmin changes bConfigurationValue in sysfs.
31 * v0.23 - use softirq for rx processing, as needed by tty layer
32 * v0.24 - change probe method to evaluate CDC union descriptor
33 * v0.25 - downstream tasks paralelized to maximize throughput
34 * v0.26 - multiple write urbs, writesize increased
38 * This program is free software; you can redistribute it and/or modify
39 * it under the terms of the GNU General Public License as published by
40 * the Free Software Foundation; either version 2 of the License, or
41 * (at your option) any later version.
43 * This program is distributed in the hope that it will be useful,
44 * but WITHOUT ANY WARRANTY; without even the implied warranty of
45 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 * GNU General Public License for more details.
48 * You should have received a copy of the GNU General Public License
49 * along with this program; if not, write to the Free Software
50 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
56 #include <linux/kernel.h>
57 #include <linux/errno.h>
58 #include <linux/init.h>
59 #include <linux/slab.h>
60 #include <linux/tty.h>
61 #include <linux/tty_driver.h>
62 #include <linux/tty_flip.h>
63 #include <linux/module.h>
64 #include <linux/mutex.h>
65 #include <asm/uaccess.h>
66 #include <linux/usb.h>
67 #include <linux/usb/cdc.h>
68 #include <asm/byteorder.h>
69 #include <asm/unaligned.h>
70 #include <linux/list.h>
75 #define ACM_CLOSE_TIMEOUT 15 /* seconds to let writes drain */
80 #define DRIVER_VERSION "v0.26"
81 #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek"
82 #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
84 static struct usb_driver acm_driver;
85 static struct tty_driver *acm_tty_driver;
86 static struct acm *acm_table[ACM_TTY_MINORS];
88 static DEFINE_MUTEX(open_mutex);
90 #define ACM_READY(acm) (acm && acm->dev && acm->used)
99 * Functions for ACM control messages.
102 static int acm_ctrl_msg(struct acm *acm, int request, int value, void *buf, int len)
104 int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
105 request, USB_RT_ACM, value,
106 acm->control->altsetting[0].desc.bInterfaceNumber,
108 dbg("acm_control_msg: rq: 0x%02x val: %#x len: %#x result: %d", request, value, len, retval);
109 return retval < 0 ? retval : 0;
112 /* devices aren't required to support these requests.
113 * the cdc acm descriptor tells whether they do...
115 #define acm_set_control(acm, control) \
116 acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0)
117 #define acm_set_line(acm, line) \
118 acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
119 #define acm_send_break(acm, ms) \
120 acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
123 * Write buffer management.
124 * All of these assume proper locks taken by the caller.
127 static int acm_wb_alloc(struct acm *acm)
140 wbn = (wbn + 1) % ACM_NW;
146 static int acm_wb_is_avail(struct acm *acm)
152 spin_lock_irqsave(&acm->write_lock, flags);
153 for (i = 0; i < ACM_NW; i++) {
156 spin_unlock_irqrestore(&acm->write_lock, flags);
163 static void acm_write_done(struct acm *acm, struct acm_wb *wb)
167 spin_lock_irqsave(&acm->write_lock, flags);
170 spin_unlock_irqrestore(&acm->write_lock, flags);
176 * the caller is responsible for locking
179 static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
185 wb->urb->transfer_buffer = wb->buf;
186 wb->urb->transfer_dma = wb->dmah;
187 wb->urb->transfer_buffer_length = wb->len;
188 wb->urb->dev = acm->dev;
190 if ((rc = usb_submit_urb(wb->urb, GFP_ATOMIC)) < 0) {
191 dbg("usb_submit_urb(write bulk) failed: %d", rc);
192 acm_write_done(acm, wb);
197 static int acm_write_start(struct acm *acm, int wbn)
200 struct acm_wb *wb = &acm->wb[wbn];
203 spin_lock_irqsave(&acm->write_lock, flags);
206 spin_unlock_irqrestore(&acm->write_lock, flags);
210 dbg("%s susp_count: %d", __func__, acm->susp_count);
211 if (acm->susp_count) {
212 acm->delayed_wb = wb;
213 schedule_work(&acm->waker);
214 spin_unlock_irqrestore(&acm->write_lock, flags);
215 return 0; /* A white lie */
217 usb_mark_last_busy(acm->dev);
219 rc = acm_start_wb(acm, wb);
220 spin_unlock_irqrestore(&acm->write_lock, flags);
226 * attributes exported through sysfs
228 static ssize_t show_caps
229 (struct device *dev, struct device_attribute *attr, char *buf)
231 struct usb_interface *intf = to_usb_interface(dev);
232 struct acm *acm = usb_get_intfdata(intf);
234 return sprintf(buf, "%d", acm->ctrl_caps);
236 static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
238 static ssize_t show_country_codes
239 (struct device *dev, struct device_attribute *attr, char *buf)
241 struct usb_interface *intf = to_usb_interface(dev);
242 struct acm *acm = usb_get_intfdata(intf);
244 memcpy(buf, acm->country_codes, acm->country_code_size);
245 return acm->country_code_size;
248 static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
250 static ssize_t show_country_rel_date
251 (struct device *dev, struct device_attribute *attr, char *buf)
253 struct usb_interface *intf = to_usb_interface(dev);
254 struct acm *acm = usb_get_intfdata(intf);
256 return sprintf(buf, "%d", acm->country_rel_date);
259 static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
261 * Interrupt handlers for various ACM device responses
264 /* control interface reports status changes with "interrupt" transfers */
265 static void acm_ctrl_irq(struct urb *urb)
267 struct acm *acm = urb->context;
268 struct usb_cdc_notification *dr = urb->transfer_buffer;
272 int status = urb->status;
281 /* this urb is terminated, clean up */
282 dbg("%s - urb shutting down with status: %d", __func__, status);
285 dbg("%s - nonzero urb status received: %d", __func__, status);
292 data = (unsigned char *)(dr + 1);
293 switch (dr->bNotificationType) {
295 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
297 dbg("%s network", dr->wValue ? "connected to" : "disconnected from");
300 case USB_CDC_NOTIFY_SERIAL_STATE:
302 newctrl = get_unaligned_le16(data);
304 if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
305 dbg("calling hangup");
306 tty_hangup(acm->tty);
309 acm->ctrlin = newctrl;
311 dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c",
312 acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
313 acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
314 acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-', acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
315 acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
320 dbg("unknown notification %d received: index %d len %d data0 %d data1 %d",
321 dr->bNotificationType, dr->wIndex,
322 dr->wLength, data[0], data[1]);
326 usb_mark_last_busy(acm->dev);
327 retval = usb_submit_urb (urb, GFP_ATOMIC);
329 dev_err(&urb->dev->dev, "%s - usb_submit_urb failed with "
330 "result %d", __func__, retval);
333 /* data interface returns incoming bytes, or we got unthrottled */
334 static void acm_read_bulk(struct urb *urb)
337 struct acm_ru *rcv = urb->context;
338 struct acm *acm = rcv->instance;
339 int status = urb->status;
341 dbg("Entering acm_read_bulk with status %d", status);
343 if (!ACM_READY(acm)) {
344 dev_dbg(&acm->data->dev, "Aborting, acm not ready");
347 usb_mark_last_busy(acm->dev);
350 dev_dbg(&acm->data->dev, "bulk rx status %d\n", status);
353 buf->size = urb->actual_length;
355 if (likely(status == 0)) {
356 spin_lock(&acm->read_lock);
358 list_add_tail(&rcv->list, &acm->spare_read_urbs);
359 list_add_tail(&buf->list, &acm->filled_read_bufs);
360 spin_unlock(&acm->read_lock);
362 /* we drop the buffer due to an error */
363 spin_lock(&acm->read_lock);
364 list_add_tail(&rcv->list, &acm->spare_read_urbs);
365 list_add(&buf->list, &acm->spare_read_bufs);
366 spin_unlock(&acm->read_lock);
367 /* nevertheless the tasklet must be kicked unconditionally
368 so the queue cannot dry up */
370 if (likely(!acm->susp_count))
371 tasklet_schedule(&acm->urb_task);
374 static void acm_rx_tasklet(unsigned long _acm)
376 struct acm *acm = (void *)_acm;
378 struct tty_struct *tty = acm->tty;
381 unsigned char throttled;
383 dbg("Entering acm_rx_tasklet");
387 dbg("acm_rx_tasklet: ACM not ready");
391 spin_lock_irqsave(&acm->throttle_lock, flags);
392 throttled = acm->throttle;
393 spin_unlock_irqrestore(&acm->throttle_lock, flags);
396 dbg("acm_rx_tasklet: throttled");
401 spin_lock_irqsave(&acm->read_lock, flags);
402 if (list_empty(&acm->filled_read_bufs)) {
403 spin_unlock_irqrestore(&acm->read_lock, flags);
406 buf = list_entry(acm->filled_read_bufs.next,
407 struct acm_rb, list);
408 list_del(&buf->list);
409 spin_unlock_irqrestore(&acm->read_lock, flags);
411 dbg("acm_rx_tasklet: procesing buf 0x%p, size = %d", buf, buf->size);
413 tty_buffer_request_room(tty, buf->size);
414 spin_lock_irqsave(&acm->throttle_lock, flags);
415 throttled = acm->throttle;
416 spin_unlock_irqrestore(&acm->throttle_lock, flags);
418 tty_insert_flip_string(tty, buf->base, buf->size);
419 tty_flip_buffer_push(tty);
422 dbg("Throttling noticed");
423 spin_lock_irqsave(&acm->read_lock, flags);
424 list_add(&buf->list, &acm->filled_read_bufs);
425 spin_unlock_irqrestore(&acm->read_lock, flags);
429 spin_lock_irqsave(&acm->read_lock, flags);
430 list_add(&buf->list, &acm->spare_read_bufs);
431 spin_unlock_irqrestore(&acm->read_lock, flags);
435 while (!list_empty(&acm->spare_read_bufs)) {
436 spin_lock_irqsave(&acm->read_lock, flags);
437 if (list_empty(&acm->spare_read_urbs)) {
439 spin_unlock_irqrestore(&acm->read_lock, flags);
442 rcv = list_entry(acm->spare_read_urbs.next,
443 struct acm_ru, list);
444 list_del(&rcv->list);
445 spin_unlock_irqrestore(&acm->read_lock, flags);
447 buf = list_entry(acm->spare_read_bufs.next,
448 struct acm_rb, list);
449 list_del(&buf->list);
453 usb_fill_bulk_urb(rcv->urb, acm->dev,
458 rcv->urb->transfer_dma = buf->dma;
459 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
461 /* This shouldn't kill the driver as unsuccessful URBs are returned to the
462 free-urbs-pool and resubmited ASAP */
463 spin_lock_irqsave(&acm->read_lock, flags);
464 if (acm->susp_count || usb_submit_urb(rcv->urb, GFP_ATOMIC) < 0) {
465 list_add(&buf->list, &acm->spare_read_bufs);
466 list_add(&rcv->list, &acm->spare_read_urbs);
468 spin_unlock_irqrestore(&acm->read_lock, flags);
471 spin_unlock_irqrestore(&acm->read_lock, flags);
472 dbg("acm_rx_tasklet: sending urb 0x%p, rcv 0x%p, buf 0x%p", rcv->urb, rcv, buf);
475 spin_lock_irqsave(&acm->read_lock, flags);
477 spin_unlock_irqrestore(&acm->read_lock, flags);
480 /* data interface wrote those outgoing bytes */
481 static void acm_write_bulk(struct urb *urb)
483 struct acm_wb *wb = urb->context;
484 struct acm *acm = wb->instance;
486 if (verbose || urb->status
487 || (urb->actual_length != urb->transfer_buffer_length))
488 dev_dbg(&acm->data->dev, "tx %d/%d bytes -- > %d\n",
490 urb->transfer_buffer_length,
493 acm_write_done(acm, wb);
495 schedule_work(&acm->work);
497 wake_up_interruptible(&acm->drain_wait);
500 static void acm_softint(struct work_struct *work)
502 struct acm *acm = container_of(work, struct acm, work);
504 dev_vdbg(&acm->data->dev, "tx work\n");
507 tty_wakeup(acm->tty);
510 static void acm_waker(struct work_struct *waker)
512 struct acm *acm = container_of(waker, struct acm, waker);
515 rv = usb_autopm_get_interface(acm->control);
517 dev_err(&acm->dev->dev, "Autopm failure in %s\n", __func__);
520 if (acm->delayed_wb) {
521 acm_start_wb(acm, acm->delayed_wb);
522 acm->delayed_wb = NULL;
524 usb_autopm_put_interface(acm->control);
531 static int acm_tty_open(struct tty_struct *tty, struct file *filp)
536 dbg("Entering acm_tty_open.");
538 mutex_lock(&open_mutex);
540 acm = acm_table[tty->index];
541 if (!acm || !acm->dev)
546 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
547 tty->driver_data = acm;
550 /* force low_latency on so that our tty_push actually forces the data through,
551 otherwise it is scheduled, and with high data rates data can get lost. */
552 tty->low_latency = 1;
554 if (usb_autopm_get_interface(acm->control) < 0)
557 acm->control->needs_remote_wakeup = 1;
559 mutex_lock(&acm->mutex);
561 usb_autopm_put_interface(acm->control);
566 acm->ctrlurb->dev = acm->dev;
567 if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
568 dbg("usb_submit_urb(ctrl irq) failed");
572 if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS) &&
573 (acm->ctrl_caps & USB_CDC_CAP_LINE))
575 usb_autopm_put_interface(acm->control);
577 INIT_LIST_HEAD(&acm->spare_read_urbs);
578 INIT_LIST_HEAD(&acm->spare_read_bufs);
579 INIT_LIST_HEAD(&acm->filled_read_bufs);
580 for (i = 0; i < acm->rx_buflimit; i++) {
581 list_add(&(acm->ru[i].list), &acm->spare_read_urbs);
583 for (i = 0; i < acm->rx_buflimit; i++) {
584 list_add(&(acm->rb[i].list), &acm->spare_read_bufs);
589 tasklet_schedule(&acm->urb_task);
592 mutex_unlock(&acm->mutex);
594 mutex_unlock(&open_mutex);
598 usb_kill_urb(acm->ctrlurb);
600 usb_autopm_put_interface(acm->control);
602 mutex_unlock(&acm->mutex);
604 mutex_unlock(&open_mutex);
608 static void acm_tty_unregister(struct acm *acm)
612 nr = acm->rx_buflimit;
613 tty_unregister_device(acm_tty_driver, acm->minor);
614 usb_put_intf(acm->control);
615 acm_table[acm->minor] = NULL;
616 usb_free_urb(acm->ctrlurb);
617 for (i = 0; i < ACM_NW; i++)
618 usb_free_urb(acm->wb[i].urb);
619 for (i = 0; i < nr; i++)
620 usb_free_urb(acm->ru[i].urb);
621 kfree(acm->country_codes);
625 static int acm_tty_chars_in_buffer(struct tty_struct *tty);
627 static void acm_tty_close(struct tty_struct *tty, struct file *filp)
629 struct acm *acm = tty->driver_data;
632 if (!acm || !acm->used)
635 nr = acm->rx_buflimit;
636 mutex_lock(&open_mutex);
639 usb_autopm_get_interface(acm->control);
640 acm_set_control(acm, acm->ctrlout = 0);
642 /* try letting the last writes drain naturally */
643 wait_event_interruptible_timeout(acm->drain_wait,
644 (ACM_NW == acm_wb_is_avail(acm))
646 ACM_CLOSE_TIMEOUT * HZ);
648 usb_kill_urb(acm->ctrlurb);
649 for (i = 0; i < ACM_NW; i++)
650 usb_kill_urb(acm->wb[i].urb);
651 for (i = 0; i < nr; i++)
652 usb_kill_urb(acm->ru[i].urb);
653 acm->control->needs_remote_wakeup = 0;
654 usb_autopm_put_interface(acm->control);
656 acm_tty_unregister(acm);
658 mutex_unlock(&open_mutex);
661 static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
663 struct acm *acm = tty->driver_data;
669 dbg("Entering acm_tty_write to write %d bytes,", count);
676 spin_lock_irqsave(&acm->write_lock, flags);
677 if ((wbn = acm_wb_alloc(acm)) < 0) {
678 spin_unlock_irqrestore(&acm->write_lock, flags);
683 count = (count > acm->writesize) ? acm->writesize : count;
684 dbg("Get %d bytes...", count);
685 memcpy(wb->buf, buf, count);
687 spin_unlock_irqrestore(&acm->write_lock, flags);
689 if ((stat = acm_write_start(acm, wbn)) < 0)
694 static int acm_tty_write_room(struct tty_struct *tty)
696 struct acm *acm = tty->driver_data;
700 * Do not let the line discipline to know that we have a reserve,
701 * or it might get too enthusiastic.
703 return acm_wb_is_avail(acm) ? acm->writesize : 0;
706 static int acm_tty_chars_in_buffer(struct tty_struct *tty)
708 struct acm *acm = tty->driver_data;
712 * This is inaccurate (overcounts), but it works.
714 return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
717 static void acm_tty_throttle(struct tty_struct *tty)
719 struct acm *acm = tty->driver_data;
722 spin_lock_bh(&acm->throttle_lock);
724 spin_unlock_bh(&acm->throttle_lock);
727 static void acm_tty_unthrottle(struct tty_struct *tty)
729 struct acm *acm = tty->driver_data;
732 spin_lock_bh(&acm->throttle_lock);
734 spin_unlock_bh(&acm->throttle_lock);
735 tasklet_schedule(&acm->urb_task);
738 static int acm_tty_break_ctl(struct tty_struct *tty, int state)
740 struct acm *acm = tty->driver_data;
744 retval = acm_send_break(acm, state ? 0xffff : 0);
746 dbg("send break failed");
750 static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file)
752 struct acm *acm = tty->driver_data;
757 return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
758 (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
759 (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
760 (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
761 (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
765 static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file,
766 unsigned int set, unsigned int clear)
768 struct acm *acm = tty->driver_data;
769 unsigned int newctrl;
774 newctrl = acm->ctrlout;
775 set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
776 clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
778 newctrl = (newctrl & ~clear) | set;
780 if (acm->ctrlout == newctrl)
782 return acm_set_control(acm, acm->ctrlout = newctrl);
785 static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
787 struct acm *acm = tty->driver_data;
795 static const __u32 acm_tty_speed[] = {
796 0, 50, 75, 110, 134, 150, 200, 300, 600,
797 1200, 1800, 2400, 4800, 9600, 19200, 38400,
798 57600, 115200, 230400, 460800, 500000, 576000,
799 921600, 1000000, 1152000, 1500000, 2000000,
800 2500000, 3000000, 3500000, 4000000
803 static const __u8 acm_tty_size[] = {
807 static void acm_tty_set_termios(struct tty_struct *tty, struct ktermios *termios_old)
809 struct acm *acm = tty->driver_data;
810 struct ktermios *termios = tty->termios;
811 struct usb_cdc_line_coding newline;
812 int newctrl = acm->ctrlout;
817 newline.dwDTERate = cpu_to_le32p(acm_tty_speed +
818 (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0));
819 newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
820 newline.bParityType = termios->c_cflag & PARENB ?
821 (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
822 newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
824 acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
826 if (!newline.dwDTERate) {
827 newline.dwDTERate = acm->line.dwDTERate;
828 newctrl &= ~ACM_CTRL_DTR;
829 } else newctrl |= ACM_CTRL_DTR;
831 if (newctrl != acm->ctrlout)
832 acm_set_control(acm, acm->ctrlout = newctrl);
834 if (memcmp(&acm->line, &newline, sizeof newline)) {
835 memcpy(&acm->line, &newline, sizeof newline);
836 dbg("set line: %d %d %d %d", le32_to_cpu(newline.dwDTERate),
837 newline.bCharFormat, newline.bParityType,
839 acm_set_line(acm, &acm->line);
844 * USB probe and disconnect routines.
847 /* Little helpers: write/read buffers free */
848 static void acm_write_buffers_free(struct acm *acm)
852 struct usb_device *usb_dev = interface_to_usbdev(acm->control);
854 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
855 usb_buffer_free(usb_dev, acm->writesize, wb->buf, wb->dmah);
859 static void acm_read_buffers_free(struct acm *acm)
861 struct usb_device *usb_dev = interface_to_usbdev(acm->control);
862 int i, n = acm->rx_buflimit;
864 for (i = 0; i < n; i++)
865 usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma);
868 /* Little helper: write buffers allocate */
869 static int acm_write_buffers_alloc(struct acm *acm)
874 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
875 wb->buf = usb_buffer_alloc(acm->dev, acm->writesize, GFP_KERNEL,
881 usb_buffer_free(acm->dev, acm->writesize,
890 static int acm_probe (struct usb_interface *intf,
891 const struct usb_device_id *id)
893 struct usb_cdc_union_desc *union_header = NULL;
894 struct usb_cdc_country_functional_desc *cfd = NULL;
895 unsigned char *buffer = intf->altsetting->extra;
896 int buflen = intf->altsetting->extralen;
897 struct usb_interface *control_interface;
898 struct usb_interface *data_interface;
899 struct usb_endpoint_descriptor *epctrl;
900 struct usb_endpoint_descriptor *epread;
901 struct usb_endpoint_descriptor *epwrite;
902 struct usb_device *usb_dev = interface_to_usbdev(intf);
905 int ctrlsize,readsize;
907 u8 ac_management_function = 0;
908 u8 call_management_function = 0;
909 int call_interface_num = -1;
910 int data_interface_num;
911 unsigned long quirks;
916 quirks = (unsigned long)id->driver_info;
917 num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
919 /* handle quirks deadly to normal probing*/
920 if (quirks == NO_UNION_NORMAL) {
921 data_interface = usb_ifnum_to_if(usb_dev, 1);
922 control_interface = usb_ifnum_to_if(usb_dev, 0);
923 goto skip_normal_probe;
928 dev_err(&intf->dev, "Weird descriptor references\n");
933 if (intf->cur_altsetting->endpoint->extralen && intf->cur_altsetting->endpoint->extra) {
934 dev_dbg(&intf->dev,"Seeking extra descriptors on endpoint\n");
935 buflen = intf->cur_altsetting->endpoint->extralen;
936 buffer = intf->cur_altsetting->endpoint->extra;
939 "Zero length descriptor references\n");
945 if (buffer [1] != USB_DT_CS_INTERFACE) {
946 dev_err(&intf->dev, "skipping garbage\n");
950 switch (buffer [2]) {
951 case USB_CDC_UNION_TYPE: /* we've found it */
953 dev_err(&intf->dev, "More than one "
958 union_header = (struct usb_cdc_union_desc *)
961 case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
962 cfd = (struct usb_cdc_country_functional_desc *)buffer;
964 case USB_CDC_HEADER_TYPE: /* maybe check version */
965 break; /* for now we ignore it */
966 case USB_CDC_ACM_TYPE:
967 ac_management_function = buffer[3];
969 case USB_CDC_CALL_MANAGEMENT_TYPE:
970 call_management_function = buffer[3];
971 call_interface_num = buffer[4];
972 if ((call_management_function & 3) != 3)
973 dev_err(&intf->dev, "This device "
974 "cannot do calls on its own. "
975 "It is no modem.\n");
978 /* there are LOTS more CDC descriptors that
979 * could legitimately be found here.
981 dev_dbg(&intf->dev, "Ignoring descriptor: "
982 "type %02x, length %d\n",
983 buffer[2], buffer[0]);
992 if (call_interface_num > 0) {
993 dev_dbg(&intf->dev,"No union descriptor, using call management descriptor\n");
994 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
995 control_interface = intf;
997 dev_dbg(&intf->dev,"No union descriptor, giving up\n");
1001 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
1002 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
1003 if (!control_interface || !data_interface) {
1004 dev_dbg(&intf->dev,"no interfaces\n");
1009 if (data_interface_num != call_interface_num)
1010 dev_dbg(&intf->dev,"Separate call control interface. That is not fully supported.\n");
1014 /*workaround for switched interfaces */
1015 if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) {
1016 if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) {
1017 struct usb_interface *t;
1018 dev_dbg(&intf->dev,"Your device has switched interfaces.\n");
1020 t = control_interface;
1021 control_interface = data_interface;
1028 /* Accept probe requests only for the control interface */
1029 if (intf != control_interface)
1032 if (usb_interface_claimed(data_interface)) { /* valid in this context */
1033 dev_dbg(&intf->dev,"The data interface isn't available\n");
1038 if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
1041 epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
1042 epread = &data_interface->cur_altsetting->endpoint[0].desc;
1043 epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
1046 /* workaround for switched endpoints */
1047 if (!usb_endpoint_dir_in(epread)) {
1048 /* descriptors are swapped */
1049 struct usb_endpoint_descriptor *t;
1050 dev_dbg(&intf->dev,"The data interface has switched endpoints\n");
1056 dbg("interfaces are valid");
1057 for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
1059 if (minor == ACM_TTY_MINORS) {
1060 dev_err(&intf->dev, "no more free acm devices\n");
1064 if (!(acm = kzalloc(sizeof(struct acm), GFP_KERNEL))) {
1065 dev_dbg(&intf->dev, "out of memory (acm kzalloc)\n");
1069 ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize);
1070 readsize = le16_to_cpu(epread->wMaxPacketSize)* ( quirks == SINGLE_RX_URB ? 1 : 2);
1071 acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize) * 20;
1072 acm->control = control_interface;
1073 acm->data = data_interface;
1076 acm->ctrl_caps = ac_management_function;
1077 acm->ctrlsize = ctrlsize;
1078 acm->readsize = readsize;
1079 acm->rx_buflimit = num_rx_buf;
1080 acm->urb_task.func = acm_rx_tasklet;
1081 acm->urb_task.data = (unsigned long) acm;
1082 INIT_WORK(&acm->work, acm_softint);
1083 INIT_WORK(&acm->waker, acm_waker);
1084 init_waitqueue_head(&acm->drain_wait);
1085 spin_lock_init(&acm->throttle_lock);
1086 spin_lock_init(&acm->write_lock);
1087 spin_lock_init(&acm->read_lock);
1088 mutex_init(&acm->mutex);
1089 acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
1091 buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
1093 dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)\n");
1096 acm->ctrl_buffer = buf;
1098 if (acm_write_buffers_alloc(acm) < 0) {
1099 dev_dbg(&intf->dev, "out of memory (write buffer alloc)\n");
1103 acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
1104 if (!acm->ctrlurb) {
1105 dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
1108 for (i = 0; i < num_rx_buf; i++) {
1109 struct acm_ru *rcv = &(acm->ru[i]);
1111 if (!(rcv->urb = usb_alloc_urb(0, GFP_KERNEL))) {
1112 dev_dbg(&intf->dev, "out of memory (read urbs usb_alloc_urb)\n");
1116 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1117 rcv->instance = acm;
1119 for (i = 0; i < num_rx_buf; i++) {
1120 struct acm_rb *rb = &(acm->rb[i]);
1122 rb->base = usb_buffer_alloc(acm->dev, readsize,
1123 GFP_KERNEL, &rb->dma);
1125 dev_dbg(&intf->dev, "out of memory (read bufs usb_buffer_alloc)\n");
1129 for(i = 0; i < ACM_NW; i++)
1131 struct acm_wb *snd = &(acm->wb[i]);
1133 if (!(snd->urb = usb_alloc_urb(0, GFP_KERNEL))) {
1134 dev_dbg(&intf->dev, "out of memory (write urbs usb_alloc_urb)");
1138 usb_fill_bulk_urb(snd->urb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
1139 NULL, acm->writesize, acm_write_bulk, snd);
1140 snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1141 snd->instance = acm;
1144 usb_set_intfdata (intf, acm);
1146 i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
1150 if (cfd) { /* export the country data */
1151 acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
1152 if (!acm->country_codes)
1153 goto skip_countries;
1154 acm->country_code_size = cfd->bLength - 4;
1155 memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0, cfd->bLength - 4);
1156 acm->country_rel_date = cfd->iCountryCodeRelDate;
1158 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
1160 kfree(acm->country_codes);
1161 goto skip_countries;
1164 i = device_create_file(&intf->dev, &dev_attr_iCountryCodeRelDate);
1166 kfree(acm->country_codes);
1167 goto skip_countries;
1172 usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
1173 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval);
1174 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1175 acm->ctrlurb->transfer_dma = acm->ctrl_dma;
1177 dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
1179 acm_set_control(acm, acm->ctrlout);
1181 acm->line.dwDTERate = cpu_to_le32(9600);
1182 acm->line.bDataBits = 8;
1183 acm_set_line(acm, &acm->line);
1185 usb_driver_claim_interface(&acm_driver, data_interface, acm);
1186 usb_set_intfdata(data_interface, acm);
1188 usb_get_intf(control_interface);
1189 tty_register_device(acm_tty_driver, minor, &control_interface->dev);
1191 acm_table[minor] = acm;
1195 for (i = 0; i < ACM_NW; i++)
1196 usb_free_urb(acm->wb[i].urb);
1198 acm_read_buffers_free(acm);
1199 for (i = 0; i < num_rx_buf; i++)
1200 usb_free_urb(acm->ru[i].urb);
1201 usb_free_urb(acm->ctrlurb);
1203 acm_write_buffers_free(acm);
1205 usb_buffer_free(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1212 static void stop_data_traffic(struct acm *acm)
1215 dbg("Entering stop_data_traffic");
1217 tasklet_disable(&acm->urb_task);
1219 usb_kill_urb(acm->ctrlurb);
1220 for(i = 0; i < ACM_NW; i++)
1221 usb_kill_urb(acm->wb[i].urb);
1222 for (i = 0; i < acm->rx_buflimit; i++)
1223 usb_kill_urb(acm->ru[i].urb);
1225 tasklet_enable(&acm->urb_task);
1227 cancel_work_sync(&acm->work);
1228 cancel_work_sync(&acm->waker);
1231 static void acm_disconnect(struct usb_interface *intf)
1233 struct acm *acm = usb_get_intfdata(intf);
1234 struct usb_device *usb_dev = interface_to_usbdev(intf);
1236 /* sibling interface is already cleaning up */
1240 mutex_lock(&open_mutex);
1241 if (acm->country_codes){
1242 device_remove_file(&acm->control->dev,
1243 &dev_attr_wCountryCodes);
1244 device_remove_file(&acm->control->dev,
1245 &dev_attr_iCountryCodeRelDate);
1247 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
1249 usb_set_intfdata(acm->control, NULL);
1250 usb_set_intfdata(acm->data, NULL);
1252 stop_data_traffic(acm);
1254 acm_write_buffers_free(acm);
1255 usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1256 acm_read_buffers_free(acm);
1258 usb_driver_release_interface(&acm_driver, intf == acm->control ?
1259 acm->data : acm->control);
1262 acm_tty_unregister(acm);
1263 mutex_unlock(&open_mutex);
1267 mutex_unlock(&open_mutex);
1270 tty_hangup(acm->tty);
1274 static int acm_suspend(struct usb_interface *intf, pm_message_t message)
1276 struct acm *acm = usb_get_intfdata(intf);
1279 if (acm->dev->auto_pm) {
1282 spin_lock_irq(&acm->read_lock);
1283 spin_lock(&acm->write_lock);
1284 b = acm->processing + acm->transmitting;
1285 spin_unlock(&acm->write_lock);
1286 spin_unlock_irq(&acm->read_lock);
1291 spin_lock_irq(&acm->read_lock);
1292 spin_lock(&acm->write_lock);
1293 cnt = acm->susp_count++;
1294 spin_unlock(&acm->write_lock);
1295 spin_unlock_irq(&acm->read_lock);
1300 we treat opened interfaces differently,
1301 we must guard against open
1303 mutex_lock(&acm->mutex);
1306 stop_data_traffic(acm);
1308 mutex_unlock(&acm->mutex);
1312 static int acm_resume(struct usb_interface *intf)
1314 struct acm *acm = usb_get_intfdata(intf);
1318 spin_lock_irq(&acm->read_lock);
1319 acm->susp_count -= 1;
1320 cnt = acm->susp_count;
1321 spin_unlock_irq(&acm->read_lock);
1326 mutex_lock(&acm->mutex);
1328 rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO);
1332 tasklet_schedule(&acm->urb_task);
1336 mutex_unlock(&acm->mutex);
1340 #endif /* CONFIG_PM */
1342 * USB driver structure.
1345 static struct usb_device_id acm_ids[] = {
1346 /* quirky and broken devices */
1347 { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
1348 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1350 { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
1351 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1353 { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
1354 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1356 { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
1357 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1359 { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
1360 .driver_info = SINGLE_RX_URB, /* firmware bug */
1362 { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
1363 .driver_info = SINGLE_RX_URB, /* firmware bug */
1365 { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
1366 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1368 { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
1369 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1371 { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */
1372 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1375 /* control interfaces with various AT-command sets */
1376 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1377 USB_CDC_ACM_PROTO_AT_V25TER) },
1378 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1379 USB_CDC_ACM_PROTO_AT_PCCA101) },
1380 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1381 USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
1382 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1383 USB_CDC_ACM_PROTO_AT_GSM) },
1384 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1385 USB_CDC_ACM_PROTO_AT_3G ) },
1386 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1387 USB_CDC_ACM_PROTO_AT_CDMA) },
1389 /* NOTE: COMM/ACM/0xff is likely MSFT RNDIS ... NOT a modem!! */
1393 MODULE_DEVICE_TABLE (usb, acm_ids);
1395 static struct usb_driver acm_driver = {
1398 .disconnect = acm_disconnect,
1400 .suspend = acm_suspend,
1401 .resume = acm_resume,
1403 .id_table = acm_ids,
1405 .supports_autosuspend = 1,
1410 * TTY driver structures.
1413 static const struct tty_operations acm_ops = {
1414 .open = acm_tty_open,
1415 .close = acm_tty_close,
1416 .write = acm_tty_write,
1417 .write_room = acm_tty_write_room,
1418 .ioctl = acm_tty_ioctl,
1419 .throttle = acm_tty_throttle,
1420 .unthrottle = acm_tty_unthrottle,
1421 .chars_in_buffer = acm_tty_chars_in_buffer,
1422 .break_ctl = acm_tty_break_ctl,
1423 .set_termios = acm_tty_set_termios,
1424 .tiocmget = acm_tty_tiocmget,
1425 .tiocmset = acm_tty_tiocmset,
1432 static int __init acm_init(void)
1435 acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
1436 if (!acm_tty_driver)
1438 acm_tty_driver->owner = THIS_MODULE,
1439 acm_tty_driver->driver_name = "acm",
1440 acm_tty_driver->name = "ttyACM",
1441 acm_tty_driver->major = ACM_TTY_MAJOR,
1442 acm_tty_driver->minor_start = 0,
1443 acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
1444 acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
1445 acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1446 acm_tty_driver->init_termios = tty_std_termios;
1447 acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1448 tty_set_operations(acm_tty_driver, &acm_ops);
1450 retval = tty_register_driver(acm_tty_driver);
1452 put_tty_driver(acm_tty_driver);
1456 retval = usb_register(&acm_driver);
1458 tty_unregister_driver(acm_tty_driver);
1459 put_tty_driver(acm_tty_driver);
1463 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1469 static void __exit acm_exit(void)
1471 usb_deregister(&acm_driver);
1472 tty_unregister_driver(acm_tty_driver);
1473 put_tty_driver(acm_tty_driver);
1476 module_init(acm_init);
1477 module_exit(acm_exit);
1479 MODULE_AUTHOR( DRIVER_AUTHOR );
1480 MODULE_DESCRIPTION( DRIVER_DESC );
1481 MODULE_LICENSE("GPL");