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;
162 spin_unlock_irqrestore(&acm->write_lock, flags);
168 static int acm_write_start(struct acm *acm, int wbn)
174 spin_lock_irqsave(&acm->write_lock, flags);
176 spin_unlock_irqrestore(&acm->write_lock, flags);
180 if (!acm->write_ready) {
181 spin_unlock_irqrestore(&acm->write_lock, flags);
182 return 0; /* A white lie */
185 if (!acm_wb_is_used(acm, wbn)) {
186 spin_unlock_irqrestore(&acm->write_lock, flags);
191 if(acm_wb_is_avail(acm) <= 1)
192 acm->write_ready = 0;
193 spin_unlock_irqrestore(&acm->write_lock, flags);
195 wb->urb->transfer_buffer = wb->buf;
196 wb->urb->transfer_dma = wb->dmah;
197 wb->urb->transfer_buffer_length = wb->len;
198 wb->urb->dev = acm->dev;
200 if ((rc = usb_submit_urb(wb->urb, GFP_ATOMIC)) < 0) {
201 dbg("usb_submit_urb(write bulk) failed: %d", rc);
202 acm_write_done(acm, wb);
207 * attributes exported through sysfs
209 static ssize_t show_caps
210 (struct device *dev, struct device_attribute *attr, char *buf)
212 struct usb_interface *intf = to_usb_interface(dev);
213 struct acm *acm = usb_get_intfdata(intf);
215 return sprintf(buf, "%d", acm->ctrl_caps);
217 static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
219 static ssize_t show_country_codes
220 (struct device *dev, struct device_attribute *attr, char *buf)
222 struct usb_interface *intf = to_usb_interface(dev);
223 struct acm *acm = usb_get_intfdata(intf);
225 memcpy(buf, acm->country_codes, acm->country_code_size);
226 return acm->country_code_size;
229 static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
231 static ssize_t show_country_rel_date
232 (struct device *dev, struct device_attribute *attr, char *buf)
234 struct usb_interface *intf = to_usb_interface(dev);
235 struct acm *acm = usb_get_intfdata(intf);
237 return sprintf(buf, "%d", acm->country_rel_date);
240 static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
242 * Interrupt handlers for various ACM device responses
245 /* control interface reports status changes with "interrupt" transfers */
246 static void acm_ctrl_irq(struct urb *urb)
248 struct acm *acm = urb->context;
249 struct usb_cdc_notification *dr = urb->transfer_buffer;
253 int status = urb->status;
262 /* this urb is terminated, clean up */
263 dbg("%s - urb shutting down with status: %d", __FUNCTION__, status);
266 dbg("%s - nonzero urb status received: %d", __FUNCTION__, status);
273 data = (unsigned char *)(dr + 1);
274 switch (dr->bNotificationType) {
276 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
278 dbg("%s network", dr->wValue ? "connected to" : "disconnected from");
281 case USB_CDC_NOTIFY_SERIAL_STATE:
283 newctrl = le16_to_cpu(get_unaligned((__le16 *) data));
285 if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
286 dbg("calling hangup");
287 tty_hangup(acm->tty);
290 acm->ctrlin = newctrl;
292 dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c",
293 acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
294 acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
295 acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-', acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
296 acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
301 dbg("unknown notification %d received: index %d len %d data0 %d data1 %d",
302 dr->bNotificationType, dr->wIndex,
303 dr->wLength, data[0], data[1]);
307 retval = usb_submit_urb (urb, GFP_ATOMIC);
309 err ("%s - usb_submit_urb failed with result %d",
310 __FUNCTION__, retval);
313 /* data interface returns incoming bytes, or we got unthrottled */
314 static void acm_read_bulk(struct urb *urb)
317 struct acm_ru *rcv = urb->context;
318 struct acm *acm = rcv->instance;
319 int status = urb->status;
321 dbg("Entering acm_read_bulk with status %d", status);
327 dev_dbg(&acm->data->dev, "bulk rx status %d\n", status);
330 buf->size = urb->actual_length;
332 if (likely(status == 0)) {
333 spin_lock(&acm->read_lock);
334 list_add_tail(&rcv->list, &acm->spare_read_urbs);
335 list_add_tail(&buf->list, &acm->filled_read_bufs);
336 spin_unlock(&acm->read_lock);
338 /* we drop the buffer due to an error */
339 spin_lock(&acm->read_lock);
340 list_add_tail(&rcv->list, &acm->spare_read_urbs);
341 list_add(&buf->list, &acm->spare_read_bufs);
342 spin_unlock(&acm->read_lock);
343 /* nevertheless the tasklet must be kicked unconditionally
344 so the queue cannot dry up */
346 tasklet_schedule(&acm->urb_task);
349 static void acm_rx_tasklet(unsigned long _acm)
351 struct acm *acm = (void *)_acm;
353 struct tty_struct *tty = acm->tty;
356 unsigned char throttled;
357 dbg("Entering acm_rx_tasklet");
362 spin_lock_irqsave(&acm->throttle_lock, flags);
363 throttled = acm->throttle;
364 spin_unlock_irqrestore(&acm->throttle_lock, flags);
369 spin_lock_irqsave(&acm->read_lock, flags);
370 if (list_empty(&acm->filled_read_bufs)) {
371 spin_unlock_irqrestore(&acm->read_lock, flags);
374 buf = list_entry(acm->filled_read_bufs.next,
375 struct acm_rb, list);
376 list_del(&buf->list);
377 spin_unlock_irqrestore(&acm->read_lock, flags);
379 dbg("acm_rx_tasklet: procesing buf 0x%p, size = %d", buf, buf->size);
381 tty_buffer_request_room(tty, buf->size);
382 spin_lock_irqsave(&acm->throttle_lock, flags);
383 throttled = acm->throttle;
384 spin_unlock_irqrestore(&acm->throttle_lock, flags);
386 tty_insert_flip_string(tty, buf->base, buf->size);
387 tty_flip_buffer_push(tty);
390 dbg("Throttling noticed");
391 spin_lock_irqsave(&acm->read_lock, flags);
392 list_add(&buf->list, &acm->filled_read_bufs);
393 spin_unlock_irqrestore(&acm->read_lock, flags);
397 spin_lock_irqsave(&acm->read_lock, flags);
398 list_add(&buf->list, &acm->spare_read_bufs);
399 spin_unlock_irqrestore(&acm->read_lock, flags);
403 while (!list_empty(&acm->spare_read_bufs)) {
404 spin_lock_irqsave(&acm->read_lock, flags);
405 if (list_empty(&acm->spare_read_urbs)) {
406 spin_unlock_irqrestore(&acm->read_lock, flags);
409 rcv = list_entry(acm->spare_read_urbs.next,
410 struct acm_ru, list);
411 list_del(&rcv->list);
412 spin_unlock_irqrestore(&acm->read_lock, flags);
414 buf = list_entry(acm->spare_read_bufs.next,
415 struct acm_rb, list);
416 list_del(&buf->list);
420 usb_fill_bulk_urb(rcv->urb, acm->dev,
425 rcv->urb->transfer_dma = buf->dma;
426 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
428 dbg("acm_rx_tasklet: sending urb 0x%p, rcv 0x%p, buf 0x%p", rcv->urb, rcv, buf);
430 /* This shouldn't kill the driver as unsuccessful URBs are returned to the
431 free-urbs-pool and resubmited ASAP */
432 if (usb_submit_urb(rcv->urb, GFP_ATOMIC) < 0) {
433 list_add(&buf->list, &acm->spare_read_bufs);
434 spin_lock_irqsave(&acm->read_lock, flags);
435 list_add(&rcv->list, &acm->spare_read_urbs);
436 spin_unlock_irqrestore(&acm->read_lock, flags);
442 /* data interface wrote those outgoing bytes */
443 static void acm_write_bulk(struct urb *urb)
446 struct acm_wb *wb = (struct acm_wb *)urb->context;
448 dbg("Entering acm_write_bulk with status %d", urb->status);
451 acm_write_done(acm, wb);
453 schedule_work(&acm->work);
456 static void acm_softint(struct work_struct *work)
458 struct acm *acm = container_of(work, struct acm, work);
459 dbg("Entering acm_softint.");
463 tty_wakeup(acm->tty);
470 static int acm_tty_open(struct tty_struct *tty, struct file *filp)
475 dbg("Entering acm_tty_open.");
477 mutex_lock(&open_mutex);
479 acm = acm_table[tty->index];
480 if (!acm || !acm->dev)
485 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
486 tty->driver_data = acm;
489 /* force low_latency on so that our tty_push actually forces the data through,
490 otherwise it is scheduled, and with high data rates data can get lost. */
491 tty->low_latency = 1;
493 if (usb_autopm_get_interface(acm->control) < 0)
496 mutex_lock(&acm->mutex);
498 usb_autopm_put_interface(acm->control);
503 acm->ctrlurb->dev = acm->dev;
504 if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
505 dbg("usb_submit_urb(ctrl irq) failed");
509 if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS) &&
510 (acm->ctrl_caps & USB_CDC_CAP_LINE))
513 INIT_LIST_HEAD(&acm->spare_read_urbs);
514 INIT_LIST_HEAD(&acm->spare_read_bufs);
515 INIT_LIST_HEAD(&acm->filled_read_bufs);
516 for (i = 0; i < acm->rx_buflimit; i++) {
517 list_add(&(acm->ru[i].list), &acm->spare_read_urbs);
519 for (i = 0; i < acm->rx_buflimit; i++) {
520 list_add(&(acm->rb[i].list), &acm->spare_read_bufs);
525 tasklet_schedule(&acm->urb_task);
529 mutex_unlock(&acm->mutex);
530 mutex_unlock(&open_mutex);
534 usb_kill_urb(acm->ctrlurb);
536 usb_autopm_put_interface(acm->control);
538 mutex_unlock(&acm->mutex);
540 mutex_unlock(&open_mutex);
544 static void acm_tty_unregister(struct acm *acm)
548 nr = acm->rx_buflimit;
549 tty_unregister_device(acm_tty_driver, acm->minor);
550 usb_put_intf(acm->control);
551 acm_table[acm->minor] = NULL;
552 usb_free_urb(acm->ctrlurb);
553 for (i = 0; i < ACM_NW; i++)
554 usb_free_urb(acm->wb[i].urb);
555 for (i = 0; i < nr; i++)
556 usb_free_urb(acm->ru[i].urb);
557 kfree(acm->country_codes);
561 static void acm_tty_close(struct tty_struct *tty, struct file *filp)
563 struct acm *acm = tty->driver_data;
566 if (!acm || !acm->used)
569 nr = acm->rx_buflimit;
570 mutex_lock(&open_mutex);
573 acm_set_control(acm, acm->ctrlout = 0);
574 usb_kill_urb(acm->ctrlurb);
575 for (i = 0; i < ACM_NW; i++)
576 usb_kill_urb(acm->wb[i].urb);
577 for (i = 0; i < nr; i++)
578 usb_kill_urb(acm->ru[i].urb);
579 usb_autopm_put_interface(acm->control);
581 acm_tty_unregister(acm);
583 mutex_unlock(&open_mutex);
586 static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
588 struct acm *acm = tty->driver_data;
594 dbg("Entering acm_tty_write to write %d bytes,", count);
601 spin_lock_irqsave(&acm->write_lock, flags);
602 if ((wbn = acm_wb_alloc(acm)) < 0) {
603 spin_unlock_irqrestore(&acm->write_lock, flags);
608 count = (count > acm->writesize) ? acm->writesize : count;
609 dbg("Get %d bytes...", count);
610 memcpy(wb->buf, buf, count);
612 spin_unlock_irqrestore(&acm->write_lock, flags);
614 if ((stat = acm_write_start(acm, wbn)) < 0)
619 static int acm_tty_write_room(struct tty_struct *tty)
621 struct acm *acm = tty->driver_data;
625 * Do not let the line discipline to know that we have a reserve,
626 * or it might get too enthusiastic.
628 return (acm->write_ready && acm_wb_is_avail(acm)) ? acm->writesize : 0;
631 static int acm_tty_chars_in_buffer(struct tty_struct *tty)
633 struct acm *acm = tty->driver_data;
637 * This is inaccurate (overcounts), but it works.
639 return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
642 static void acm_tty_throttle(struct tty_struct *tty)
644 struct acm *acm = tty->driver_data;
647 spin_lock_bh(&acm->throttle_lock);
649 spin_unlock_bh(&acm->throttle_lock);
652 static void acm_tty_unthrottle(struct tty_struct *tty)
654 struct acm *acm = tty->driver_data;
657 spin_lock_bh(&acm->throttle_lock);
659 spin_unlock_bh(&acm->throttle_lock);
660 tasklet_schedule(&acm->urb_task);
663 static void acm_tty_break_ctl(struct tty_struct *tty, int state)
665 struct acm *acm = tty->driver_data;
668 if (acm_send_break(acm, state ? 0xffff : 0))
669 dbg("send break failed");
672 static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file)
674 struct acm *acm = tty->driver_data;
679 return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
680 (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
681 (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
682 (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
683 (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
687 static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file,
688 unsigned int set, unsigned int clear)
690 struct acm *acm = tty->driver_data;
691 unsigned int newctrl;
696 newctrl = acm->ctrlout;
697 set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
698 clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
700 newctrl = (newctrl & ~clear) | set;
702 if (acm->ctrlout == newctrl)
704 return acm_set_control(acm, acm->ctrlout = newctrl);
707 static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
709 struct acm *acm = tty->driver_data;
717 static const __u32 acm_tty_speed[] = {
718 0, 50, 75, 110, 134, 150, 200, 300, 600,
719 1200, 1800, 2400, 4800, 9600, 19200, 38400,
720 57600, 115200, 230400, 460800, 500000, 576000,
721 921600, 1000000, 1152000, 1500000, 2000000,
722 2500000, 3000000, 3500000, 4000000
725 static const __u8 acm_tty_size[] = {
729 static void acm_tty_set_termios(struct tty_struct *tty, struct ktermios *termios_old)
731 struct acm *acm = tty->driver_data;
732 struct ktermios *termios = tty->termios;
733 struct usb_cdc_line_coding newline;
734 int newctrl = acm->ctrlout;
739 newline.dwDTERate = cpu_to_le32p(acm_tty_speed +
740 (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0));
741 newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
742 newline.bParityType = termios->c_cflag & PARENB ?
743 (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
744 newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
746 acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
748 if (!newline.dwDTERate) {
749 newline.dwDTERate = acm->line.dwDTERate;
750 newctrl &= ~ACM_CTRL_DTR;
751 } else newctrl |= ACM_CTRL_DTR;
753 if (newctrl != acm->ctrlout)
754 acm_set_control(acm, acm->ctrlout = newctrl);
756 if (memcmp(&acm->line, &newline, sizeof newline)) {
757 memcpy(&acm->line, &newline, sizeof newline);
758 dbg("set line: %d %d %d %d", le32_to_cpu(newline.dwDTERate),
759 newline.bCharFormat, newline.bParityType,
761 acm_set_line(acm, &acm->line);
766 * USB probe and disconnect routines.
769 /* Little helper: write buffers free */
770 static void acm_write_buffers_free(struct acm *acm)
775 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
776 usb_buffer_free(acm->dev, acm->writesize, wb->buf, wb->dmah);
780 /* Little helper: write buffers allocate */
781 static int acm_write_buffers_alloc(struct acm *acm)
786 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
787 wb->buf = usb_buffer_alloc(acm->dev, acm->writesize, GFP_KERNEL,
793 usb_buffer_free(acm->dev, acm->writesize,
802 static int acm_probe (struct usb_interface *intf,
803 const struct usb_device_id *id)
805 struct usb_cdc_union_desc *union_header = NULL;
806 struct usb_cdc_country_functional_desc *cfd = NULL;
807 char *buffer = intf->altsetting->extra;
808 int buflen = intf->altsetting->extralen;
809 struct usb_interface *control_interface;
810 struct usb_interface *data_interface;
811 struct usb_endpoint_descriptor *epctrl;
812 struct usb_endpoint_descriptor *epread;
813 struct usb_endpoint_descriptor *epwrite;
814 struct usb_device *usb_dev = interface_to_usbdev(intf);
817 int ctrlsize,readsize;
819 u8 ac_management_function = 0;
820 u8 call_management_function = 0;
821 int call_interface_num = -1;
822 int data_interface_num;
823 unsigned long quirks;
828 quirks = (unsigned long)id->driver_info;
829 num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
831 /* handle quirks deadly to normal probing*/
832 if (quirks == NO_UNION_NORMAL) {
833 data_interface = usb_ifnum_to_if(usb_dev, 1);
834 control_interface = usb_ifnum_to_if(usb_dev, 0);
835 goto skip_normal_probe;
840 err("Weird descriptor references\n");
845 if (intf->cur_altsetting->endpoint->extralen && intf->cur_altsetting->endpoint->extra) {
846 dev_dbg(&intf->dev,"Seeking extra descriptors on endpoint\n");
847 buflen = intf->cur_altsetting->endpoint->extralen;
848 buffer = intf->cur_altsetting->endpoint->extra;
850 err("Zero length descriptor references\n");
856 if (buffer [1] != USB_DT_CS_INTERFACE) {
857 err("skipping garbage\n");
861 switch (buffer [2]) {
862 case USB_CDC_UNION_TYPE: /* we've found it */
864 err("More than one union descriptor, skipping ...");
867 union_header = (struct usb_cdc_union_desc *)
870 case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
871 cfd = (struct usb_cdc_country_functional_desc *)buffer;
873 case USB_CDC_HEADER_TYPE: /* maybe check version */
874 break; /* for now we ignore it */
875 case USB_CDC_ACM_TYPE:
876 ac_management_function = buffer[3];
878 case USB_CDC_CALL_MANAGEMENT_TYPE:
879 call_management_function = buffer[3];
880 call_interface_num = buffer[4];
881 if ((call_management_function & 3) != 3)
882 err("This device cannot do calls on its own. It is no modem.");
886 err("Ignoring extra header, type %d, length %d", buffer[2], buffer[0]);
895 if (call_interface_num > 0) {
896 dev_dbg(&intf->dev,"No union descriptor, using call management descriptor\n");
897 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
898 control_interface = intf;
900 dev_dbg(&intf->dev,"No union descriptor, giving up\n");
904 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
905 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
906 if (!control_interface || !data_interface) {
907 dev_dbg(&intf->dev,"no interfaces\n");
912 if (data_interface_num != call_interface_num)
913 dev_dbg(&intf->dev,"Separate call control interface. That is not fully supported.\n");
917 /*workaround for switched interfaces */
918 if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) {
919 if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) {
920 struct usb_interface *t;
921 dev_dbg(&intf->dev,"Your device has switched interfaces.\n");
923 t = control_interface;
924 control_interface = data_interface;
931 /* Accept probe requests only for the control interface */
932 if (intf != control_interface)
935 if (usb_interface_claimed(data_interface)) { /* valid in this context */
936 dev_dbg(&intf->dev,"The data interface isn't available\n");
941 if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
944 epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
945 epread = &data_interface->cur_altsetting->endpoint[0].desc;
946 epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
949 /* workaround for switched endpoints */
950 if (!usb_endpoint_dir_in(epread)) {
951 /* descriptors are swapped */
952 struct usb_endpoint_descriptor *t;
953 dev_dbg(&intf->dev,"The data interface has switched endpoints\n");
959 dbg("interfaces are valid");
960 for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
962 if (minor == ACM_TTY_MINORS) {
963 err("no more free acm devices");
967 if (!(acm = kzalloc(sizeof(struct acm), GFP_KERNEL))) {
968 dev_dbg(&intf->dev, "out of memory (acm kzalloc)\n");
972 ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize);
973 readsize = le16_to_cpu(epread->wMaxPacketSize)* ( quirks == SINGLE_RX_URB ? 1 : 2);
974 acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize) * 20;
975 acm->control = control_interface;
976 acm->data = data_interface;
979 acm->ctrl_caps = ac_management_function;
980 acm->ctrlsize = ctrlsize;
981 acm->readsize = readsize;
982 acm->rx_buflimit = num_rx_buf;
983 acm->urb_task.func = acm_rx_tasklet;
984 acm->urb_task.data = (unsigned long) acm;
985 INIT_WORK(&acm->work, acm_softint);
986 spin_lock_init(&acm->throttle_lock);
987 spin_lock_init(&acm->write_lock);
988 spin_lock_init(&acm->read_lock);
989 mutex_init(&acm->mutex);
990 acm->write_ready = 1;
991 acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
993 buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
995 dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)\n");
998 acm->ctrl_buffer = buf;
1000 if (acm_write_buffers_alloc(acm) < 0) {
1001 dev_dbg(&intf->dev, "out of memory (write buffer alloc)\n");
1005 acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
1006 if (!acm->ctrlurb) {
1007 dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
1010 for (i = 0; i < num_rx_buf; i++) {
1011 struct acm_ru *rcv = &(acm->ru[i]);
1013 if (!(rcv->urb = usb_alloc_urb(0, GFP_KERNEL))) {
1014 dev_dbg(&intf->dev, "out of memory (read urbs usb_alloc_urb)\n");
1018 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1019 rcv->instance = acm;
1021 for (i = 0; i < num_rx_buf; i++) {
1022 struct acm_rb *buf = &(acm->rb[i]);
1024 if (!(buf->base = usb_buffer_alloc(acm->dev, readsize, GFP_KERNEL, &buf->dma))) {
1025 dev_dbg(&intf->dev, "out of memory (read bufs usb_buffer_alloc)\n");
1029 for(i = 0; i < ACM_NW; i++)
1031 struct acm_wb *snd = &(acm->wb[i]);
1033 if (!(snd->urb = usb_alloc_urb(0, GFP_KERNEL))) {
1034 dev_dbg(&intf->dev, "out of memory (write urbs usb_alloc_urb)");
1038 usb_fill_bulk_urb(snd->urb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
1039 NULL, acm->writesize, acm_write_bulk, snd);
1040 snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1041 snd->instance = acm;
1044 usb_set_intfdata (intf, acm);
1046 i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
1050 if (cfd) { /* export the country data */
1051 acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
1052 if (!acm->country_codes)
1053 goto skip_countries;
1054 acm->country_code_size = cfd->bLength - 4;
1055 memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0, cfd->bLength - 4);
1056 acm->country_rel_date = cfd->iCountryCodeRelDate;
1058 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
1060 kfree(acm->country_codes);
1061 goto skip_countries;
1064 i = device_create_file(&intf->dev, &dev_attr_iCountryCodeRelDate);
1066 kfree(acm->country_codes);
1067 goto skip_countries;
1072 usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
1073 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval);
1074 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1075 acm->ctrlurb->transfer_dma = acm->ctrl_dma;
1077 dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
1079 acm_set_control(acm, acm->ctrlout);
1081 acm->line.dwDTERate = cpu_to_le32(9600);
1082 acm->line.bDataBits = 8;
1083 acm_set_line(acm, &acm->line);
1085 usb_driver_claim_interface(&acm_driver, data_interface, acm);
1087 usb_get_intf(control_interface);
1088 tty_register_device(acm_tty_driver, minor, &control_interface->dev);
1090 acm_table[minor] = acm;
1094 for (i = 0; i < ACM_NW; i++)
1095 usb_free_urb(acm->wb[i].urb);
1097 for (i = 0; i < num_rx_buf; i++)
1098 usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma);
1099 for (i = 0; i < num_rx_buf; i++)
1100 usb_free_urb(acm->ru[i].urb);
1101 usb_free_urb(acm->ctrlurb);
1103 acm_write_buffers_free(acm);
1105 usb_buffer_free(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1112 static void stop_data_traffic(struct acm *acm)
1116 tasklet_disable(&acm->urb_task);
1118 usb_kill_urb(acm->ctrlurb);
1119 for(i = 0; i < ACM_NW; i++)
1120 usb_kill_urb(acm->wb[i].urb);
1121 for (i = 0; i < acm->rx_buflimit; i++)
1122 usb_kill_urb(acm->ru[i].urb);
1124 INIT_LIST_HEAD(&acm->filled_read_bufs);
1125 INIT_LIST_HEAD(&acm->spare_read_bufs);
1127 tasklet_enable(&acm->urb_task);
1129 cancel_work_sync(&acm->work);
1132 static void acm_disconnect(struct usb_interface *intf)
1134 struct acm *acm = usb_get_intfdata(intf);
1135 struct usb_device *usb_dev = interface_to_usbdev(intf);
1138 if (!acm || !acm->dev) {
1139 dbg("disconnect on nonexisting interface");
1143 mutex_lock(&open_mutex);
1144 if (!usb_get_intfdata(intf)) {
1145 mutex_unlock(&open_mutex);
1148 if (acm->country_codes){
1149 device_remove_file(&acm->control->dev,
1150 &dev_attr_wCountryCodes);
1151 device_remove_file(&acm->control->dev,
1152 &dev_attr_iCountryCodeRelDate);
1154 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
1156 usb_set_intfdata(acm->control, NULL);
1157 usb_set_intfdata(acm->data, NULL);
1159 stop_data_traffic(acm);
1161 acm_write_buffers_free(acm);
1162 usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1163 for (i = 0; i < acm->rx_buflimit; i++)
1164 usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma);
1166 usb_driver_release_interface(&acm_driver, intf == acm->control ? acm->data : intf);
1169 acm_tty_unregister(acm);
1170 mutex_unlock(&open_mutex);
1174 mutex_unlock(&open_mutex);
1177 tty_hangup(acm->tty);
1180 static int acm_suspend(struct usb_interface *intf, pm_message_t message)
1182 struct acm *acm = usb_get_intfdata(intf);
1184 if (acm->susp_count++)
1187 we treat opened interfaces differently,
1188 we must guard against open
1190 mutex_lock(&acm->mutex);
1193 stop_data_traffic(acm);
1195 mutex_unlock(&acm->mutex);
1199 static int acm_resume(struct usb_interface *intf)
1201 struct acm *acm = usb_get_intfdata(intf);
1204 if (--acm->susp_count)
1207 mutex_lock(&acm->mutex);
1209 rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO);
1213 tasklet_schedule(&acm->urb_task);
1217 mutex_unlock(&acm->mutex);
1221 * USB driver structure.
1224 static struct usb_device_id acm_ids[] = {
1225 /* quirky and broken devices */
1226 { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
1227 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1229 { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
1230 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1232 { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
1233 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1235 { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
1236 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1238 { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
1239 .driver_info = SINGLE_RX_URB, /* firmware bug */
1241 { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
1242 .driver_info = SINGLE_RX_URB, /* firmware bug */
1244 { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
1245 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1248 /* control interfaces with various AT-command sets */
1249 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1250 USB_CDC_ACM_PROTO_AT_V25TER) },
1251 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1252 USB_CDC_ACM_PROTO_AT_PCCA101) },
1253 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1254 USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
1255 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1256 USB_CDC_ACM_PROTO_AT_GSM) },
1257 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1258 USB_CDC_ACM_PROTO_AT_3G ) },
1259 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1260 USB_CDC_ACM_PROTO_AT_CDMA) },
1262 /* NOTE: COMM/ACM/0xff is likely MSFT RNDIS ... NOT a modem!! */
1266 MODULE_DEVICE_TABLE (usb, acm_ids);
1268 static struct usb_driver acm_driver = {
1271 .disconnect = acm_disconnect,
1272 .suspend = acm_suspend,
1273 .resume = acm_resume,
1274 .id_table = acm_ids,
1275 .supports_autosuspend = 1,
1279 * TTY driver structures.
1282 static const struct tty_operations acm_ops = {
1283 .open = acm_tty_open,
1284 .close = acm_tty_close,
1285 .write = acm_tty_write,
1286 .write_room = acm_tty_write_room,
1287 .ioctl = acm_tty_ioctl,
1288 .throttle = acm_tty_throttle,
1289 .unthrottle = acm_tty_unthrottle,
1290 .chars_in_buffer = acm_tty_chars_in_buffer,
1291 .break_ctl = acm_tty_break_ctl,
1292 .set_termios = acm_tty_set_termios,
1293 .tiocmget = acm_tty_tiocmget,
1294 .tiocmset = acm_tty_tiocmset,
1301 static int __init acm_init(void)
1304 acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
1305 if (!acm_tty_driver)
1307 acm_tty_driver->owner = THIS_MODULE,
1308 acm_tty_driver->driver_name = "acm",
1309 acm_tty_driver->name = "ttyACM",
1310 acm_tty_driver->major = ACM_TTY_MAJOR,
1311 acm_tty_driver->minor_start = 0,
1312 acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
1313 acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
1314 acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1315 acm_tty_driver->init_termios = tty_std_termios;
1316 acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1317 tty_set_operations(acm_tty_driver, &acm_ops);
1319 retval = tty_register_driver(acm_tty_driver);
1321 put_tty_driver(acm_tty_driver);
1325 retval = usb_register(&acm_driver);
1327 tty_unregister_driver(acm_tty_driver);
1328 put_tty_driver(acm_tty_driver);
1332 info(DRIVER_VERSION ":" DRIVER_DESC);
1337 static void __exit acm_exit(void)
1339 usb_deregister(&acm_driver);
1340 tty_unregister_driver(acm_tty_driver);
1341 put_tty_driver(acm_tty_driver);
1344 module_init(acm_init);
1345 module_exit(acm_exit);
1347 MODULE_AUTHOR( DRIVER_AUTHOR );
1348 MODULE_DESCRIPTION( DRIVER_DESC );
1349 MODULE_LICENSE("GPL");