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
55 #include <linux/kernel.h>
56 #include <linux/errno.h>
57 #include <linux/init.h>
58 #include <linux/slab.h>
59 #include <linux/tty.h>
60 #include <linux/tty_driver.h>
61 #include <linux/tty_flip.h>
62 #include <linux/module.h>
63 #include <linux/mutex.h>
64 #include <asm/uaccess.h>
65 #include <linux/usb.h>
66 #include <linux/usb/cdc.h>
67 #include <asm/byteorder.h>
68 #include <asm/unaligned.h>
69 #include <linux/list.h>
76 #define DRIVER_VERSION "v0.26"
77 #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek"
78 #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
80 static struct usb_driver acm_driver;
81 static struct tty_driver *acm_tty_driver;
82 static struct acm *acm_table[ACM_TTY_MINORS];
84 static DEFINE_MUTEX(open_mutex);
86 #define ACM_READY(acm) (acm && acm->dev && acm->used)
89 * Functions for ACM control messages.
92 static int acm_ctrl_msg(struct acm *acm, int request, int value, void *buf, int len)
94 int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
95 request, USB_RT_ACM, value,
96 acm->control->altsetting[0].desc.bInterfaceNumber,
98 dbg("acm_control_msg: rq: 0x%02x val: %#x len: %#x result: %d", request, value, len, retval);
99 return retval < 0 ? retval : 0;
102 /* devices aren't required to support these requests.
103 * the cdc acm descriptor tells whether they do...
105 #define acm_set_control(acm, control) \
106 acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0)
107 #define acm_set_line(acm, line) \
108 acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
109 #define acm_send_break(acm, ms) \
110 acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
113 * Write buffer management.
114 * All of these assume proper locks taken by the caller.
117 static int acm_wb_alloc(struct acm *acm)
130 wbn = (wbn + 1) % ACM_NW;
136 static int acm_wb_is_avail(struct acm *acm)
141 for (i = 0; i < ACM_NW; i++) {
147 static inline int acm_wb_is_used(struct acm *acm, int wbn)
149 return acm->wb[wbn].use;
155 static void acm_write_done(struct acm *acm, struct acm_wb *wb)
159 spin_lock_irqsave(&acm->write_lock, flags);
160 acm->write_ready = 1;
163 spin_unlock_irqrestore(&acm->write_lock, flags);
169 * the caller is responsible for locking
172 static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
178 wb->urb->transfer_buffer = wb->buf;
179 wb->urb->transfer_dma = wb->dmah;
180 wb->urb->transfer_buffer_length = wb->len;
181 wb->urb->dev = acm->dev;
183 if ((rc = usb_submit_urb(wb->urb, GFP_ATOMIC)) < 0) {
184 dbg("usb_submit_urb(write bulk) failed: %d", rc);
185 acm_write_done(acm, wb);
190 static int acm_write_start(struct acm *acm, int wbn)
196 spin_lock_irqsave(&acm->write_lock, flags);
198 spin_unlock_irqrestore(&acm->write_lock, flags);
202 if (!acm->write_ready) {
203 spin_unlock_irqrestore(&acm->write_lock, flags);
204 return 0; /* A white lie */
208 if(acm_wb_is_avail(acm) <= 1)
209 acm->write_ready = 0;
211 dbg("%s susp_count: %d", __func__, acm->susp_count);
212 if (acm->susp_count) {
213 acm->old_ready = acm->write_ready;
214 acm->delayed_wb = wb;
215 acm->write_ready = 0;
216 schedule_work(&acm->waker);
217 spin_unlock_irqrestore(&acm->write_lock, flags);
218 return 0; /* A white lie */
220 usb_mark_last_busy(acm->dev);
222 if (!acm_wb_is_used(acm, wbn)) {
223 spin_unlock_irqrestore(&acm->write_lock, flags);
227 rc = acm_start_wb(acm, wb);
228 spin_unlock_irqrestore(&acm->write_lock, flags);
234 * attributes exported through sysfs
236 static ssize_t show_caps
237 (struct device *dev, struct device_attribute *attr, char *buf)
239 struct usb_interface *intf = to_usb_interface(dev);
240 struct acm *acm = usb_get_intfdata(intf);
242 return sprintf(buf, "%d", acm->ctrl_caps);
244 static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
246 static ssize_t show_country_codes
247 (struct device *dev, struct device_attribute *attr, char *buf)
249 struct usb_interface *intf = to_usb_interface(dev);
250 struct acm *acm = usb_get_intfdata(intf);
252 memcpy(buf, acm->country_codes, acm->country_code_size);
253 return acm->country_code_size;
256 static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
258 static ssize_t show_country_rel_date
259 (struct device *dev, struct device_attribute *attr, char *buf)
261 struct usb_interface *intf = to_usb_interface(dev);
262 struct acm *acm = usb_get_intfdata(intf);
264 return sprintf(buf, "%d", acm->country_rel_date);
267 static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
269 * Interrupt handlers for various ACM device responses
272 /* control interface reports status changes with "interrupt" transfers */
273 static void acm_ctrl_irq(struct urb *urb)
275 struct acm *acm = urb->context;
276 struct usb_cdc_notification *dr = urb->transfer_buffer;
280 int status = urb->status;
289 /* this urb is terminated, clean up */
290 dbg("%s - urb shutting down with status: %d", __func__, status);
293 dbg("%s - nonzero urb status received: %d", __func__, status);
300 data = (unsigned char *)(dr + 1);
301 switch (dr->bNotificationType) {
303 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
305 dbg("%s network", dr->wValue ? "connected to" : "disconnected from");
308 case USB_CDC_NOTIFY_SERIAL_STATE:
310 newctrl = get_unaligned_le16(data);
312 if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
313 dbg("calling hangup");
314 tty_hangup(acm->tty);
317 acm->ctrlin = newctrl;
319 dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c",
320 acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
321 acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
322 acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-', acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
323 acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
328 dbg("unknown notification %d received: index %d len %d data0 %d data1 %d",
329 dr->bNotificationType, dr->wIndex,
330 dr->wLength, data[0], data[1]);
334 usb_mark_last_busy(acm->dev);
335 retval = usb_submit_urb (urb, GFP_ATOMIC);
337 err ("%s - usb_submit_urb failed with result %d",
341 /* data interface returns incoming bytes, or we got unthrottled */
342 static void acm_read_bulk(struct urb *urb)
345 struct acm_ru *rcv = urb->context;
346 struct acm *acm = rcv->instance;
347 int status = urb->status;
349 dbg("Entering acm_read_bulk with status %d", status);
351 if (!ACM_READY(acm)) {
352 dev_dbg(&acm->data->dev, "Aborting, acm not ready");
355 usb_mark_last_busy(acm->dev);
358 dev_dbg(&acm->data->dev, "bulk rx status %d\n", status);
361 buf->size = urb->actual_length;
363 if (likely(status == 0)) {
364 spin_lock(&acm->read_lock);
366 list_add_tail(&rcv->list, &acm->spare_read_urbs);
367 list_add_tail(&buf->list, &acm->filled_read_bufs);
368 spin_unlock(&acm->read_lock);
370 /* we drop the buffer due to an error */
371 spin_lock(&acm->read_lock);
372 list_add_tail(&rcv->list, &acm->spare_read_urbs);
373 list_add(&buf->list, &acm->spare_read_bufs);
374 spin_unlock(&acm->read_lock);
375 /* nevertheless the tasklet must be kicked unconditionally
376 so the queue cannot dry up */
378 if (likely(!acm->susp_count))
379 tasklet_schedule(&acm->urb_task);
382 static void acm_rx_tasklet(unsigned long _acm)
384 struct acm *acm = (void *)_acm;
386 struct tty_struct *tty = acm->tty;
389 unsigned char throttled;
391 dbg("Entering acm_rx_tasklet");
395 dbg("acm_rx_tasklet: ACM not ready");
399 spin_lock_irqsave(&acm->throttle_lock, flags);
400 throttled = acm->throttle;
401 spin_unlock_irqrestore(&acm->throttle_lock, flags);
404 dbg("acm_rx_tasklet: throttled");
409 spin_lock_irqsave(&acm->read_lock, flags);
410 if (list_empty(&acm->filled_read_bufs)) {
411 spin_unlock_irqrestore(&acm->read_lock, flags);
414 buf = list_entry(acm->filled_read_bufs.next,
415 struct acm_rb, list);
416 list_del(&buf->list);
417 spin_unlock_irqrestore(&acm->read_lock, flags);
419 dbg("acm_rx_tasklet: procesing buf 0x%p, size = %d", buf, buf->size);
421 tty_buffer_request_room(tty, buf->size);
422 spin_lock_irqsave(&acm->throttle_lock, flags);
423 throttled = acm->throttle;
424 spin_unlock_irqrestore(&acm->throttle_lock, flags);
426 tty_insert_flip_string(tty, buf->base, buf->size);
427 tty_flip_buffer_push(tty);
430 dbg("Throttling noticed");
431 spin_lock_irqsave(&acm->read_lock, flags);
432 list_add(&buf->list, &acm->filled_read_bufs);
433 spin_unlock_irqrestore(&acm->read_lock, flags);
437 spin_lock_irqsave(&acm->read_lock, flags);
438 list_add(&buf->list, &acm->spare_read_bufs);
439 spin_unlock_irqrestore(&acm->read_lock, flags);
443 while (!list_empty(&acm->spare_read_bufs)) {
444 spin_lock_irqsave(&acm->read_lock, flags);
445 if (list_empty(&acm->spare_read_urbs)) {
447 spin_unlock_irqrestore(&acm->read_lock, flags);
450 rcv = list_entry(acm->spare_read_urbs.next,
451 struct acm_ru, list);
452 list_del(&rcv->list);
453 spin_unlock_irqrestore(&acm->read_lock, flags);
455 buf = list_entry(acm->spare_read_bufs.next,
456 struct acm_rb, list);
457 list_del(&buf->list);
461 usb_fill_bulk_urb(rcv->urb, acm->dev,
466 rcv->urb->transfer_dma = buf->dma;
467 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
469 /* This shouldn't kill the driver as unsuccessful URBs are returned to the
470 free-urbs-pool and resubmited ASAP */
471 spin_lock_irqsave(&acm->read_lock, flags);
472 if (acm->susp_count || usb_submit_urb(rcv->urb, GFP_ATOMIC) < 0) {
473 list_add(&buf->list, &acm->spare_read_bufs);
474 list_add(&rcv->list, &acm->spare_read_urbs);
476 spin_unlock_irqrestore(&acm->read_lock, flags);
479 spin_unlock_irqrestore(&acm->read_lock, flags);
480 dbg("acm_rx_tasklet: sending urb 0x%p, rcv 0x%p, buf 0x%p", rcv->urb, rcv, buf);
483 spin_lock_irqsave(&acm->read_lock, flags);
485 spin_unlock_irqrestore(&acm->read_lock, flags);
488 /* data interface wrote those outgoing bytes */
489 static void acm_write_bulk(struct urb *urb)
492 struct acm_wb *wb = urb->context;
494 dbg("Entering acm_write_bulk with status %d", urb->status);
497 acm_write_done(acm, wb);
499 schedule_work(&acm->work);
502 static void acm_softint(struct work_struct *work)
504 struct acm *acm = container_of(work, struct acm, work);
505 dbg("Entering acm_softint.");
509 tty_wakeup(acm->tty);
512 static void acm_waker(struct work_struct *waker)
514 struct acm *acm = container_of(waker, struct acm, waker);
518 rv = usb_autopm_get_interface(acm->control);
520 err("Autopm failure in %s", __func__);
523 if (acm->delayed_wb) {
524 acm_start_wb(acm, acm->delayed_wb);
525 acm->delayed_wb = NULL;
527 spin_lock_irqsave(&acm->write_lock, flags);
528 acm->write_ready = acm->old_ready;
529 spin_unlock_irqrestore(&acm->write_lock, flags);
530 usb_autopm_put_interface(acm->control);
537 static int acm_tty_open(struct tty_struct *tty, struct file *filp)
542 dbg("Entering acm_tty_open.");
544 mutex_lock(&open_mutex);
546 acm = acm_table[tty->index];
547 if (!acm || !acm->dev)
552 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
553 tty->driver_data = acm;
556 /* force low_latency on so that our tty_push actually forces the data through,
557 otherwise it is scheduled, and with high data rates data can get lost. */
558 tty->low_latency = 1;
560 if (usb_autopm_get_interface(acm->control) < 0)
563 acm->control->needs_remote_wakeup = 1;
565 mutex_lock(&acm->mutex);
567 usb_autopm_put_interface(acm->control);
572 acm->ctrlurb->dev = acm->dev;
573 if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
574 dbg("usb_submit_urb(ctrl irq) failed");
578 if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS) &&
579 (acm->ctrl_caps & USB_CDC_CAP_LINE))
581 usb_autopm_put_interface(acm->control);
583 INIT_LIST_HEAD(&acm->spare_read_urbs);
584 INIT_LIST_HEAD(&acm->spare_read_bufs);
585 INIT_LIST_HEAD(&acm->filled_read_bufs);
586 for (i = 0; i < acm->rx_buflimit; i++) {
587 list_add(&(acm->ru[i].list), &acm->spare_read_urbs);
589 for (i = 0; i < acm->rx_buflimit; i++) {
590 list_add(&(acm->rb[i].list), &acm->spare_read_bufs);
595 tasklet_schedule(&acm->urb_task);
599 mutex_unlock(&acm->mutex);
600 mutex_unlock(&open_mutex);
604 usb_kill_urb(acm->ctrlurb);
606 usb_autopm_put_interface(acm->control);
608 mutex_unlock(&acm->mutex);
610 mutex_unlock(&open_mutex);
614 static void acm_tty_unregister(struct acm *acm)
618 nr = acm->rx_buflimit;
619 tty_unregister_device(acm_tty_driver, acm->minor);
620 usb_put_intf(acm->control);
621 acm_table[acm->minor] = NULL;
622 usb_free_urb(acm->ctrlurb);
623 for (i = 0; i < ACM_NW; i++)
624 usb_free_urb(acm->wb[i].urb);
625 for (i = 0; i < nr; i++)
626 usb_free_urb(acm->ru[i].urb);
627 kfree(acm->country_codes);
631 static void acm_tty_close(struct tty_struct *tty, struct file *filp)
633 struct acm *acm = tty->driver_data;
636 if (!acm || !acm->used)
639 nr = acm->rx_buflimit;
640 mutex_lock(&open_mutex);
643 usb_autopm_get_interface(acm->control);
644 acm_set_control(acm, acm->ctrlout = 0);
645 usb_kill_urb(acm->ctrlurb);
646 for (i = 0; i < ACM_NW; i++)
647 usb_kill_urb(acm->wb[i].urb);
648 for (i = 0; i < nr; i++)
649 usb_kill_urb(acm->ru[i].urb);
650 acm->control->needs_remote_wakeup = 0;
651 usb_autopm_put_interface(acm->control);
653 acm_tty_unregister(acm);
655 mutex_unlock(&open_mutex);
658 static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
660 struct acm *acm = tty->driver_data;
666 dbg("Entering acm_tty_write to write %d bytes,", count);
673 spin_lock_irqsave(&acm->write_lock, flags);
674 if ((wbn = acm_wb_alloc(acm)) < 0) {
675 spin_unlock_irqrestore(&acm->write_lock, flags);
680 count = (count > acm->writesize) ? acm->writesize : count;
681 dbg("Get %d bytes...", count);
682 memcpy(wb->buf, buf, count);
684 spin_unlock_irqrestore(&acm->write_lock, flags);
686 if ((stat = acm_write_start(acm, wbn)) < 0)
691 static int acm_tty_write_room(struct tty_struct *tty)
693 struct acm *acm = tty->driver_data;
697 * Do not let the line discipline to know that we have a reserve,
698 * or it might get too enthusiastic.
700 return (acm->write_ready && acm_wb_is_avail(acm)) ? acm->writesize : 0;
703 static int acm_tty_chars_in_buffer(struct tty_struct *tty)
705 struct acm *acm = tty->driver_data;
709 * This is inaccurate (overcounts), but it works.
711 return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
714 static void acm_tty_throttle(struct tty_struct *tty)
716 struct acm *acm = tty->driver_data;
719 spin_lock_bh(&acm->throttle_lock);
721 spin_unlock_bh(&acm->throttle_lock);
724 static void acm_tty_unthrottle(struct tty_struct *tty)
726 struct acm *acm = tty->driver_data;
729 spin_lock_bh(&acm->throttle_lock);
731 spin_unlock_bh(&acm->throttle_lock);
732 tasklet_schedule(&acm->urb_task);
735 static int acm_tty_break_ctl(struct tty_struct *tty, int state)
737 struct acm *acm = tty->driver_data;
741 retval = acm_send_break(acm, state ? 0xffff : 0);
743 dbg("send break failed");
747 static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file)
749 struct acm *acm = tty->driver_data;
754 return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
755 (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
756 (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
757 (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
758 (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
762 static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file,
763 unsigned int set, unsigned int clear)
765 struct acm *acm = tty->driver_data;
766 unsigned int newctrl;
771 newctrl = acm->ctrlout;
772 set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
773 clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
775 newctrl = (newctrl & ~clear) | set;
777 if (acm->ctrlout == newctrl)
779 return acm_set_control(acm, acm->ctrlout = newctrl);
782 static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
784 struct acm *acm = tty->driver_data;
792 static const __u32 acm_tty_speed[] = {
793 0, 50, 75, 110, 134, 150, 200, 300, 600,
794 1200, 1800, 2400, 4800, 9600, 19200, 38400,
795 57600, 115200, 230400, 460800, 500000, 576000,
796 921600, 1000000, 1152000, 1500000, 2000000,
797 2500000, 3000000, 3500000, 4000000
800 static const __u8 acm_tty_size[] = {
804 static void acm_tty_set_termios(struct tty_struct *tty, struct ktermios *termios_old)
806 struct acm *acm = tty->driver_data;
807 struct ktermios *termios = tty->termios;
808 struct usb_cdc_line_coding newline;
809 int newctrl = acm->ctrlout;
814 newline.dwDTERate = cpu_to_le32p(acm_tty_speed +
815 (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0));
816 newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
817 newline.bParityType = termios->c_cflag & PARENB ?
818 (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
819 newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
821 acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
823 if (!newline.dwDTERate) {
824 newline.dwDTERate = acm->line.dwDTERate;
825 newctrl &= ~ACM_CTRL_DTR;
826 } else newctrl |= ACM_CTRL_DTR;
828 if (newctrl != acm->ctrlout)
829 acm_set_control(acm, acm->ctrlout = newctrl);
831 if (memcmp(&acm->line, &newline, sizeof newline)) {
832 memcpy(&acm->line, &newline, sizeof newline);
833 dbg("set line: %d %d %d %d", le32_to_cpu(newline.dwDTERate),
834 newline.bCharFormat, newline.bParityType,
836 acm_set_line(acm, &acm->line);
841 * USB probe and disconnect routines.
844 /* Little helpers: write/read buffers free */
845 static void acm_write_buffers_free(struct acm *acm)
850 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
851 usb_buffer_free(acm->dev, acm->writesize, wb->buf, wb->dmah);
855 static void acm_read_buffers_free(struct acm *acm)
857 struct usb_device *usb_dev = interface_to_usbdev(acm->control);
858 int i, n = acm->rx_buflimit;
860 for (i = 0; i < n; i++)
861 usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma);
864 /* Little helper: write buffers allocate */
865 static int acm_write_buffers_alloc(struct acm *acm)
870 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
871 wb->buf = usb_buffer_alloc(acm->dev, acm->writesize, GFP_KERNEL,
877 usb_buffer_free(acm->dev, acm->writesize,
886 static int acm_probe (struct usb_interface *intf,
887 const struct usb_device_id *id)
889 struct usb_cdc_union_desc *union_header = NULL;
890 struct usb_cdc_country_functional_desc *cfd = NULL;
891 unsigned char *buffer = intf->altsetting->extra;
892 int buflen = intf->altsetting->extralen;
893 struct usb_interface *control_interface;
894 struct usb_interface *data_interface;
895 struct usb_endpoint_descriptor *epctrl;
896 struct usb_endpoint_descriptor *epread;
897 struct usb_endpoint_descriptor *epwrite;
898 struct usb_device *usb_dev = interface_to_usbdev(intf);
901 int ctrlsize,readsize;
903 u8 ac_management_function = 0;
904 u8 call_management_function = 0;
905 int call_interface_num = -1;
906 int data_interface_num;
907 unsigned long quirks;
912 quirks = (unsigned long)id->driver_info;
913 num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
915 /* handle quirks deadly to normal probing*/
916 if (quirks == NO_UNION_NORMAL) {
917 data_interface = usb_ifnum_to_if(usb_dev, 1);
918 control_interface = usb_ifnum_to_if(usb_dev, 0);
919 goto skip_normal_probe;
924 err("Weird descriptor references\n");
929 if (intf->cur_altsetting->endpoint->extralen && intf->cur_altsetting->endpoint->extra) {
930 dev_dbg(&intf->dev,"Seeking extra descriptors on endpoint\n");
931 buflen = intf->cur_altsetting->endpoint->extralen;
932 buffer = intf->cur_altsetting->endpoint->extra;
934 err("Zero length descriptor references\n");
940 if (buffer [1] != USB_DT_CS_INTERFACE) {
941 err("skipping garbage\n");
945 switch (buffer [2]) {
946 case USB_CDC_UNION_TYPE: /* we've found it */
948 err("More than one union descriptor, skipping ...");
951 union_header = (struct usb_cdc_union_desc *)
954 case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
955 cfd = (struct usb_cdc_country_functional_desc *)buffer;
957 case USB_CDC_HEADER_TYPE: /* maybe check version */
958 break; /* for now we ignore it */
959 case USB_CDC_ACM_TYPE:
960 ac_management_function = buffer[3];
962 case USB_CDC_CALL_MANAGEMENT_TYPE:
963 call_management_function = buffer[3];
964 call_interface_num = buffer[4];
965 if ((call_management_function & 3) != 3)
966 err("This device cannot do calls on its own. It is no modem.");
969 /* there are LOTS more CDC descriptors that
970 * could legitimately be found here.
972 dev_dbg(&intf->dev, "Ignoring descriptor: "
973 "type %02x, length %d\n",
974 buffer[2], buffer[0]);
983 if (call_interface_num > 0) {
984 dev_dbg(&intf->dev,"No union descriptor, using call management descriptor\n");
985 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
986 control_interface = intf;
988 dev_dbg(&intf->dev,"No union descriptor, giving up\n");
992 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
993 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
994 if (!control_interface || !data_interface) {
995 dev_dbg(&intf->dev,"no interfaces\n");
1000 if (data_interface_num != call_interface_num)
1001 dev_dbg(&intf->dev,"Separate call control interface. That is not fully supported.\n");
1005 /*workaround for switched interfaces */
1006 if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) {
1007 if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) {
1008 struct usb_interface *t;
1009 dev_dbg(&intf->dev,"Your device has switched interfaces.\n");
1011 t = control_interface;
1012 control_interface = data_interface;
1019 /* Accept probe requests only for the control interface */
1020 if (intf != control_interface)
1023 if (usb_interface_claimed(data_interface)) { /* valid in this context */
1024 dev_dbg(&intf->dev,"The data interface isn't available\n");
1029 if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
1032 epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
1033 epread = &data_interface->cur_altsetting->endpoint[0].desc;
1034 epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
1037 /* workaround for switched endpoints */
1038 if (!usb_endpoint_dir_in(epread)) {
1039 /* descriptors are swapped */
1040 struct usb_endpoint_descriptor *t;
1041 dev_dbg(&intf->dev,"The data interface has switched endpoints\n");
1047 dbg("interfaces are valid");
1048 for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
1050 if (minor == ACM_TTY_MINORS) {
1051 err("no more free acm devices");
1055 if (!(acm = kzalloc(sizeof(struct acm), GFP_KERNEL))) {
1056 dev_dbg(&intf->dev, "out of memory (acm kzalloc)\n");
1060 ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize);
1061 readsize = le16_to_cpu(epread->wMaxPacketSize)* ( quirks == SINGLE_RX_URB ? 1 : 2);
1062 acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize) * 20;
1063 acm->control = control_interface;
1064 acm->data = data_interface;
1067 acm->ctrl_caps = ac_management_function;
1068 acm->ctrlsize = ctrlsize;
1069 acm->readsize = readsize;
1070 acm->rx_buflimit = num_rx_buf;
1071 acm->urb_task.func = acm_rx_tasklet;
1072 acm->urb_task.data = (unsigned long) acm;
1073 INIT_WORK(&acm->work, acm_softint);
1074 INIT_WORK(&acm->waker, acm_waker);
1075 spin_lock_init(&acm->throttle_lock);
1076 spin_lock_init(&acm->write_lock);
1077 spin_lock_init(&acm->read_lock);
1078 mutex_init(&acm->mutex);
1079 acm->write_ready = 1;
1080 acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
1082 buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
1084 dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)\n");
1087 acm->ctrl_buffer = buf;
1089 if (acm_write_buffers_alloc(acm) < 0) {
1090 dev_dbg(&intf->dev, "out of memory (write buffer alloc)\n");
1094 acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
1095 if (!acm->ctrlurb) {
1096 dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
1099 for (i = 0; i < num_rx_buf; i++) {
1100 struct acm_ru *rcv = &(acm->ru[i]);
1102 if (!(rcv->urb = usb_alloc_urb(0, GFP_KERNEL))) {
1103 dev_dbg(&intf->dev, "out of memory (read urbs usb_alloc_urb)\n");
1107 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1108 rcv->instance = acm;
1110 for (i = 0; i < num_rx_buf; i++) {
1111 struct acm_rb *buf = &(acm->rb[i]);
1113 if (!(buf->base = usb_buffer_alloc(acm->dev, readsize, GFP_KERNEL, &buf->dma))) {
1114 dev_dbg(&intf->dev, "out of memory (read bufs usb_buffer_alloc)\n");
1118 for(i = 0; i < ACM_NW; i++)
1120 struct acm_wb *snd = &(acm->wb[i]);
1122 if (!(snd->urb = usb_alloc_urb(0, GFP_KERNEL))) {
1123 dev_dbg(&intf->dev, "out of memory (write urbs usb_alloc_urb)");
1127 usb_fill_bulk_urb(snd->urb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
1128 NULL, acm->writesize, acm_write_bulk, snd);
1129 snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1130 snd->instance = acm;
1133 usb_set_intfdata (intf, acm);
1135 i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
1139 if (cfd) { /* export the country data */
1140 acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
1141 if (!acm->country_codes)
1142 goto skip_countries;
1143 acm->country_code_size = cfd->bLength - 4;
1144 memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0, cfd->bLength - 4);
1145 acm->country_rel_date = cfd->iCountryCodeRelDate;
1147 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
1149 kfree(acm->country_codes);
1150 goto skip_countries;
1153 i = device_create_file(&intf->dev, &dev_attr_iCountryCodeRelDate);
1155 kfree(acm->country_codes);
1156 goto skip_countries;
1161 usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
1162 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval);
1163 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1164 acm->ctrlurb->transfer_dma = acm->ctrl_dma;
1166 dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
1168 acm_set_control(acm, acm->ctrlout);
1170 acm->line.dwDTERate = cpu_to_le32(9600);
1171 acm->line.bDataBits = 8;
1172 acm_set_line(acm, &acm->line);
1174 usb_driver_claim_interface(&acm_driver, data_interface, acm);
1176 usb_get_intf(control_interface);
1177 tty_register_device(acm_tty_driver, minor, &control_interface->dev);
1179 acm_table[minor] = acm;
1183 for (i = 0; i < ACM_NW; i++)
1184 usb_free_urb(acm->wb[i].urb);
1186 acm_read_buffers_free(acm);
1187 for (i = 0; i < num_rx_buf; i++)
1188 usb_free_urb(acm->ru[i].urb);
1189 usb_free_urb(acm->ctrlurb);
1191 acm_write_buffers_free(acm);
1193 usb_buffer_free(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1200 static void stop_data_traffic(struct acm *acm)
1203 dbg("Entering stop_data_traffic");
1205 tasklet_disable(&acm->urb_task);
1207 usb_kill_urb(acm->ctrlurb);
1208 for(i = 0; i < ACM_NW; i++)
1209 usb_kill_urb(acm->wb[i].urb);
1210 for (i = 0; i < acm->rx_buflimit; i++)
1211 usb_kill_urb(acm->ru[i].urb);
1213 tasklet_enable(&acm->urb_task);
1215 cancel_work_sync(&acm->work);
1216 cancel_work_sync(&acm->waker);
1219 static void acm_disconnect(struct usb_interface *intf)
1221 struct acm *acm = usb_get_intfdata(intf);
1222 struct usb_device *usb_dev = interface_to_usbdev(intf);
1224 mutex_lock(&open_mutex);
1225 if (!acm || !acm->dev) {
1226 mutex_unlock(&open_mutex);
1229 if (acm->country_codes){
1230 device_remove_file(&acm->control->dev,
1231 &dev_attr_wCountryCodes);
1232 device_remove_file(&acm->control->dev,
1233 &dev_attr_iCountryCodeRelDate);
1235 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
1237 usb_set_intfdata(acm->control, NULL);
1238 usb_set_intfdata(acm->data, NULL);
1240 stop_data_traffic(acm);
1242 acm_write_buffers_free(acm);
1243 usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1244 acm_read_buffers_free(acm);
1246 usb_driver_release_interface(&acm_driver, intf == acm->control ?
1247 acm->data : acm->control);
1250 acm_tty_unregister(acm);
1251 mutex_unlock(&open_mutex);
1255 mutex_unlock(&open_mutex);
1258 tty_hangup(acm->tty);
1262 static int acm_suspend(struct usb_interface *intf, pm_message_t message)
1264 struct acm *acm = usb_get_intfdata(intf);
1267 if (acm->dev->auto_pm) {
1270 spin_lock_irq(&acm->read_lock);
1271 spin_lock(&acm->write_lock);
1272 b = acm->processing + acm->transmitting;
1273 spin_unlock(&acm->write_lock);
1274 spin_unlock_irq(&acm->read_lock);
1279 spin_lock_irq(&acm->read_lock);
1280 spin_lock(&acm->write_lock);
1281 cnt = acm->susp_count++;
1282 spin_unlock(&acm->write_lock);
1283 spin_unlock_irq(&acm->read_lock);
1288 we treat opened interfaces differently,
1289 we must guard against open
1291 mutex_lock(&acm->mutex);
1294 stop_data_traffic(acm);
1296 mutex_unlock(&acm->mutex);
1300 static int acm_resume(struct usb_interface *intf)
1302 struct acm *acm = usb_get_intfdata(intf);
1306 spin_lock_irq(&acm->read_lock);
1307 acm->susp_count -= 1;
1308 cnt = acm->susp_count;
1309 spin_unlock_irq(&acm->read_lock);
1314 mutex_lock(&acm->mutex);
1316 rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO);
1320 tasklet_schedule(&acm->urb_task);
1324 mutex_unlock(&acm->mutex);
1328 #endif /* CONFIG_PM */
1330 * USB driver structure.
1333 static struct usb_device_id acm_ids[] = {
1334 /* quirky and broken devices */
1335 { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
1336 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1338 { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
1339 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1341 { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
1342 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1344 { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
1345 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1347 { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
1348 .driver_info = SINGLE_RX_URB, /* firmware bug */
1350 { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
1351 .driver_info = SINGLE_RX_URB, /* firmware bug */
1353 { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
1354 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1356 { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
1357 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1360 /* control interfaces with various AT-command sets */
1361 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1362 USB_CDC_ACM_PROTO_AT_V25TER) },
1363 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1364 USB_CDC_ACM_PROTO_AT_PCCA101) },
1365 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1366 USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
1367 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1368 USB_CDC_ACM_PROTO_AT_GSM) },
1369 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1370 USB_CDC_ACM_PROTO_AT_3G ) },
1371 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1372 USB_CDC_ACM_PROTO_AT_CDMA) },
1374 /* NOTE: COMM/ACM/0xff is likely MSFT RNDIS ... NOT a modem!! */
1378 MODULE_DEVICE_TABLE (usb, acm_ids);
1380 static struct usb_driver acm_driver = {
1383 .disconnect = acm_disconnect,
1385 .suspend = acm_suspend,
1386 .resume = acm_resume,
1388 .id_table = acm_ids,
1390 .supports_autosuspend = 1,
1395 * TTY driver structures.
1398 static const struct tty_operations acm_ops = {
1399 .open = acm_tty_open,
1400 .close = acm_tty_close,
1401 .write = acm_tty_write,
1402 .write_room = acm_tty_write_room,
1403 .ioctl = acm_tty_ioctl,
1404 .throttle = acm_tty_throttle,
1405 .unthrottle = acm_tty_unthrottle,
1406 .chars_in_buffer = acm_tty_chars_in_buffer,
1407 .break_ctl = acm_tty_break_ctl,
1408 .set_termios = acm_tty_set_termios,
1409 .tiocmget = acm_tty_tiocmget,
1410 .tiocmset = acm_tty_tiocmset,
1417 static int __init acm_init(void)
1420 acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
1421 if (!acm_tty_driver)
1423 acm_tty_driver->owner = THIS_MODULE,
1424 acm_tty_driver->driver_name = "acm",
1425 acm_tty_driver->name = "ttyACM",
1426 acm_tty_driver->major = ACM_TTY_MAJOR,
1427 acm_tty_driver->minor_start = 0,
1428 acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
1429 acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
1430 acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1431 acm_tty_driver->init_termios = tty_std_termios;
1432 acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1433 tty_set_operations(acm_tty_driver, &acm_ops);
1435 retval = tty_register_driver(acm_tty_driver);
1437 put_tty_driver(acm_tty_driver);
1441 retval = usb_register(&acm_driver);
1443 tty_unregister_driver(acm_tty_driver);
1444 put_tty_driver(acm_tty_driver);
1448 info(DRIVER_VERSION ":" DRIVER_DESC);
1453 static void __exit acm_exit(void)
1455 usb_deregister(&acm_driver);
1456 tty_unregister_driver(acm_tty_driver);
1457 put_tty_driver(acm_tty_driver);
1460 module_init(acm_init);
1461 module_exit(acm_exit);
1463 MODULE_AUTHOR( DRIVER_AUTHOR );
1464 MODULE_DESCRIPTION( DRIVER_DESC );
1465 MODULE_LICENSE("GPL");