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++) {
150 static void acm_write_done(struct acm *acm, struct acm_wb *wb)
154 spin_lock_irqsave(&acm->write_lock, flags);
157 spin_unlock_irqrestore(&acm->write_lock, flags);
163 * the caller is responsible for locking
166 static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
172 wb->urb->transfer_buffer = wb->buf;
173 wb->urb->transfer_dma = wb->dmah;
174 wb->urb->transfer_buffer_length = wb->len;
175 wb->urb->dev = acm->dev;
177 if ((rc = usb_submit_urb(wb->urb, GFP_ATOMIC)) < 0) {
178 dbg("usb_submit_urb(write bulk) failed: %d", rc);
179 acm_write_done(acm, wb);
184 static int acm_write_start(struct acm *acm, int wbn)
187 struct acm_wb *wb = &acm->wb[wbn];
190 spin_lock_irqsave(&acm->write_lock, flags);
193 spin_unlock_irqrestore(&acm->write_lock, flags);
197 dbg("%s susp_count: %d", __func__, acm->susp_count);
198 if (acm->susp_count) {
199 acm->delayed_wb = wb;
200 schedule_work(&acm->waker);
201 spin_unlock_irqrestore(&acm->write_lock, flags);
202 return 0; /* A white lie */
204 usb_mark_last_busy(acm->dev);
206 rc = acm_start_wb(acm, wb);
207 spin_unlock_irqrestore(&acm->write_lock, flags);
213 * attributes exported through sysfs
215 static ssize_t show_caps
216 (struct device *dev, struct device_attribute *attr, char *buf)
218 struct usb_interface *intf = to_usb_interface(dev);
219 struct acm *acm = usb_get_intfdata(intf);
221 return sprintf(buf, "%d", acm->ctrl_caps);
223 static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
225 static ssize_t show_country_codes
226 (struct device *dev, struct device_attribute *attr, char *buf)
228 struct usb_interface *intf = to_usb_interface(dev);
229 struct acm *acm = usb_get_intfdata(intf);
231 memcpy(buf, acm->country_codes, acm->country_code_size);
232 return acm->country_code_size;
235 static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
237 static ssize_t show_country_rel_date
238 (struct device *dev, struct device_attribute *attr, char *buf)
240 struct usb_interface *intf = to_usb_interface(dev);
241 struct acm *acm = usb_get_intfdata(intf);
243 return sprintf(buf, "%d", acm->country_rel_date);
246 static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
248 * Interrupt handlers for various ACM device responses
251 /* control interface reports status changes with "interrupt" transfers */
252 static void acm_ctrl_irq(struct urb *urb)
254 struct acm *acm = urb->context;
255 struct usb_cdc_notification *dr = urb->transfer_buffer;
259 int status = urb->status;
268 /* this urb is terminated, clean up */
269 dbg("%s - urb shutting down with status: %d", __func__, status);
272 dbg("%s - nonzero urb status received: %d", __func__, status);
279 data = (unsigned char *)(dr + 1);
280 switch (dr->bNotificationType) {
282 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
284 dbg("%s network", dr->wValue ? "connected to" : "disconnected from");
287 case USB_CDC_NOTIFY_SERIAL_STATE:
289 newctrl = get_unaligned_le16(data);
291 if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
292 dbg("calling hangup");
293 tty_hangup(acm->tty);
296 acm->ctrlin = newctrl;
298 dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c",
299 acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
300 acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
301 acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-', acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
302 acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
307 dbg("unknown notification %d received: index %d len %d data0 %d data1 %d",
308 dr->bNotificationType, dr->wIndex,
309 dr->wLength, data[0], data[1]);
313 usb_mark_last_busy(acm->dev);
314 retval = usb_submit_urb (urb, GFP_ATOMIC);
316 err ("%s - usb_submit_urb failed with result %d",
320 /* data interface returns incoming bytes, or we got unthrottled */
321 static void acm_read_bulk(struct urb *urb)
324 struct acm_ru *rcv = urb->context;
325 struct acm *acm = rcv->instance;
326 int status = urb->status;
328 dbg("Entering acm_read_bulk with status %d", status);
330 if (!ACM_READY(acm)) {
331 dev_dbg(&acm->data->dev, "Aborting, acm not ready");
334 usb_mark_last_busy(acm->dev);
337 dev_dbg(&acm->data->dev, "bulk rx status %d\n", status);
340 buf->size = urb->actual_length;
342 if (likely(status == 0)) {
343 spin_lock(&acm->read_lock);
345 list_add_tail(&rcv->list, &acm->spare_read_urbs);
346 list_add_tail(&buf->list, &acm->filled_read_bufs);
347 spin_unlock(&acm->read_lock);
349 /* we drop the buffer due to an error */
350 spin_lock(&acm->read_lock);
351 list_add_tail(&rcv->list, &acm->spare_read_urbs);
352 list_add(&buf->list, &acm->spare_read_bufs);
353 spin_unlock(&acm->read_lock);
354 /* nevertheless the tasklet must be kicked unconditionally
355 so the queue cannot dry up */
357 if (likely(!acm->susp_count))
358 tasklet_schedule(&acm->urb_task);
361 static void acm_rx_tasklet(unsigned long _acm)
363 struct acm *acm = (void *)_acm;
365 struct tty_struct *tty = acm->tty;
368 unsigned char throttled;
370 dbg("Entering acm_rx_tasklet");
374 dbg("acm_rx_tasklet: ACM not ready");
378 spin_lock_irqsave(&acm->throttle_lock, flags);
379 throttled = acm->throttle;
380 spin_unlock_irqrestore(&acm->throttle_lock, flags);
383 dbg("acm_rx_tasklet: throttled");
388 spin_lock_irqsave(&acm->read_lock, flags);
389 if (list_empty(&acm->filled_read_bufs)) {
390 spin_unlock_irqrestore(&acm->read_lock, flags);
393 buf = list_entry(acm->filled_read_bufs.next,
394 struct acm_rb, list);
395 list_del(&buf->list);
396 spin_unlock_irqrestore(&acm->read_lock, flags);
398 dbg("acm_rx_tasklet: procesing buf 0x%p, size = %d", buf, buf->size);
400 tty_buffer_request_room(tty, buf->size);
401 spin_lock_irqsave(&acm->throttle_lock, flags);
402 throttled = acm->throttle;
403 spin_unlock_irqrestore(&acm->throttle_lock, flags);
405 tty_insert_flip_string(tty, buf->base, buf->size);
406 tty_flip_buffer_push(tty);
409 dbg("Throttling noticed");
410 spin_lock_irqsave(&acm->read_lock, flags);
411 list_add(&buf->list, &acm->filled_read_bufs);
412 spin_unlock_irqrestore(&acm->read_lock, flags);
416 spin_lock_irqsave(&acm->read_lock, flags);
417 list_add(&buf->list, &acm->spare_read_bufs);
418 spin_unlock_irqrestore(&acm->read_lock, flags);
422 while (!list_empty(&acm->spare_read_bufs)) {
423 spin_lock_irqsave(&acm->read_lock, flags);
424 if (list_empty(&acm->spare_read_urbs)) {
426 spin_unlock_irqrestore(&acm->read_lock, flags);
429 rcv = list_entry(acm->spare_read_urbs.next,
430 struct acm_ru, list);
431 list_del(&rcv->list);
432 spin_unlock_irqrestore(&acm->read_lock, flags);
434 buf = list_entry(acm->spare_read_bufs.next,
435 struct acm_rb, list);
436 list_del(&buf->list);
440 usb_fill_bulk_urb(rcv->urb, acm->dev,
445 rcv->urb->transfer_dma = buf->dma;
446 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
448 /* This shouldn't kill the driver as unsuccessful URBs are returned to the
449 free-urbs-pool and resubmited ASAP */
450 spin_lock_irqsave(&acm->read_lock, flags);
451 if (acm->susp_count || usb_submit_urb(rcv->urb, GFP_ATOMIC) < 0) {
452 list_add(&buf->list, &acm->spare_read_bufs);
453 list_add(&rcv->list, &acm->spare_read_urbs);
455 spin_unlock_irqrestore(&acm->read_lock, flags);
458 spin_unlock_irqrestore(&acm->read_lock, flags);
459 dbg("acm_rx_tasklet: sending urb 0x%p, rcv 0x%p, buf 0x%p", rcv->urb, rcv, buf);
462 spin_lock_irqsave(&acm->read_lock, flags);
464 spin_unlock_irqrestore(&acm->read_lock, flags);
467 /* data interface wrote those outgoing bytes */
468 static void acm_write_bulk(struct urb *urb)
471 struct acm_wb *wb = urb->context;
473 dbg("Entering acm_write_bulk with status %d", urb->status);
476 acm_write_done(acm, wb);
478 schedule_work(&acm->work);
481 static void acm_softint(struct work_struct *work)
483 struct acm *acm = container_of(work, struct acm, work);
484 dbg("Entering acm_softint.");
488 tty_wakeup(acm->tty);
491 static void acm_waker(struct work_struct *waker)
493 struct acm *acm = container_of(waker, struct acm, waker);
496 rv = usb_autopm_get_interface(acm->control);
498 err("Autopm failure in %s", __func__);
501 if (acm->delayed_wb) {
502 acm_start_wb(acm, acm->delayed_wb);
503 acm->delayed_wb = NULL;
505 usb_autopm_put_interface(acm->control);
512 static int acm_tty_open(struct tty_struct *tty, struct file *filp)
517 dbg("Entering acm_tty_open.");
519 mutex_lock(&open_mutex);
521 acm = acm_table[tty->index];
522 if (!acm || !acm->dev)
527 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
528 tty->driver_data = acm;
531 /* force low_latency on so that our tty_push actually forces the data through,
532 otherwise it is scheduled, and with high data rates data can get lost. */
533 tty->low_latency = 1;
535 if (usb_autopm_get_interface(acm->control) < 0)
538 acm->control->needs_remote_wakeup = 1;
540 mutex_lock(&acm->mutex);
542 usb_autopm_put_interface(acm->control);
547 acm->ctrlurb->dev = acm->dev;
548 if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
549 dbg("usb_submit_urb(ctrl irq) failed");
553 if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS) &&
554 (acm->ctrl_caps & USB_CDC_CAP_LINE))
556 usb_autopm_put_interface(acm->control);
558 INIT_LIST_HEAD(&acm->spare_read_urbs);
559 INIT_LIST_HEAD(&acm->spare_read_bufs);
560 INIT_LIST_HEAD(&acm->filled_read_bufs);
561 for (i = 0; i < acm->rx_buflimit; i++) {
562 list_add(&(acm->ru[i].list), &acm->spare_read_urbs);
564 for (i = 0; i < acm->rx_buflimit; i++) {
565 list_add(&(acm->rb[i].list), &acm->spare_read_bufs);
570 tasklet_schedule(&acm->urb_task);
574 mutex_unlock(&acm->mutex);
575 mutex_unlock(&open_mutex);
579 usb_kill_urb(acm->ctrlurb);
581 usb_autopm_put_interface(acm->control);
583 mutex_unlock(&acm->mutex);
585 mutex_unlock(&open_mutex);
589 static void acm_tty_unregister(struct acm *acm)
593 nr = acm->rx_buflimit;
594 tty_unregister_device(acm_tty_driver, acm->minor);
595 usb_put_intf(acm->control);
596 acm_table[acm->minor] = NULL;
597 usb_free_urb(acm->ctrlurb);
598 for (i = 0; i < ACM_NW; i++)
599 usb_free_urb(acm->wb[i].urb);
600 for (i = 0; i < nr; i++)
601 usb_free_urb(acm->ru[i].urb);
602 kfree(acm->country_codes);
606 static void acm_tty_close(struct tty_struct *tty, struct file *filp)
608 struct acm *acm = tty->driver_data;
611 if (!acm || !acm->used)
614 nr = acm->rx_buflimit;
615 mutex_lock(&open_mutex);
618 usb_autopm_get_interface(acm->control);
619 acm_set_control(acm, acm->ctrlout = 0);
620 usb_kill_urb(acm->ctrlurb);
621 for (i = 0; i < ACM_NW; i++)
622 usb_kill_urb(acm->wb[i].urb);
623 for (i = 0; i < nr; i++)
624 usb_kill_urb(acm->ru[i].urb);
625 acm->control->needs_remote_wakeup = 0;
626 usb_autopm_put_interface(acm->control);
628 acm_tty_unregister(acm);
630 mutex_unlock(&open_mutex);
633 static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
635 struct acm *acm = tty->driver_data;
641 dbg("Entering acm_tty_write to write %d bytes,", count);
648 spin_lock_irqsave(&acm->write_lock, flags);
649 if ((wbn = acm_wb_alloc(acm)) < 0) {
650 spin_unlock_irqrestore(&acm->write_lock, flags);
655 count = (count > acm->writesize) ? acm->writesize : count;
656 dbg("Get %d bytes...", count);
657 memcpy(wb->buf, buf, count);
659 spin_unlock_irqrestore(&acm->write_lock, flags);
661 if ((stat = acm_write_start(acm, wbn)) < 0)
666 static int acm_tty_write_room(struct tty_struct *tty)
668 struct acm *acm = tty->driver_data;
672 * Do not let the line discipline to know that we have a reserve,
673 * or it might get too enthusiastic.
675 return acm_wb_is_avail(acm) ? acm->writesize : 0;
678 static int acm_tty_chars_in_buffer(struct tty_struct *tty)
680 struct acm *acm = tty->driver_data;
684 * This is inaccurate (overcounts), but it works.
686 return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
689 static void acm_tty_throttle(struct tty_struct *tty)
691 struct acm *acm = tty->driver_data;
694 spin_lock_bh(&acm->throttle_lock);
696 spin_unlock_bh(&acm->throttle_lock);
699 static void acm_tty_unthrottle(struct tty_struct *tty)
701 struct acm *acm = tty->driver_data;
704 spin_lock_bh(&acm->throttle_lock);
706 spin_unlock_bh(&acm->throttle_lock);
707 tasklet_schedule(&acm->urb_task);
710 static int acm_tty_break_ctl(struct tty_struct *tty, int state)
712 struct acm *acm = tty->driver_data;
716 retval = acm_send_break(acm, state ? 0xffff : 0);
718 dbg("send break failed");
722 static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file)
724 struct acm *acm = tty->driver_data;
729 return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
730 (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
731 (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
732 (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
733 (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
737 static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file,
738 unsigned int set, unsigned int clear)
740 struct acm *acm = tty->driver_data;
741 unsigned int newctrl;
746 newctrl = acm->ctrlout;
747 set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
748 clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
750 newctrl = (newctrl & ~clear) | set;
752 if (acm->ctrlout == newctrl)
754 return acm_set_control(acm, acm->ctrlout = newctrl);
757 static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
759 struct acm *acm = tty->driver_data;
767 static const __u32 acm_tty_speed[] = {
768 0, 50, 75, 110, 134, 150, 200, 300, 600,
769 1200, 1800, 2400, 4800, 9600, 19200, 38400,
770 57600, 115200, 230400, 460800, 500000, 576000,
771 921600, 1000000, 1152000, 1500000, 2000000,
772 2500000, 3000000, 3500000, 4000000
775 static const __u8 acm_tty_size[] = {
779 static void acm_tty_set_termios(struct tty_struct *tty, struct ktermios *termios_old)
781 struct acm *acm = tty->driver_data;
782 struct ktermios *termios = tty->termios;
783 struct usb_cdc_line_coding newline;
784 int newctrl = acm->ctrlout;
789 newline.dwDTERate = cpu_to_le32p(acm_tty_speed +
790 (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0));
791 newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
792 newline.bParityType = termios->c_cflag & PARENB ?
793 (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
794 newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
796 acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
798 if (!newline.dwDTERate) {
799 newline.dwDTERate = acm->line.dwDTERate;
800 newctrl &= ~ACM_CTRL_DTR;
801 } else newctrl |= ACM_CTRL_DTR;
803 if (newctrl != acm->ctrlout)
804 acm_set_control(acm, acm->ctrlout = newctrl);
806 if (memcmp(&acm->line, &newline, sizeof newline)) {
807 memcpy(&acm->line, &newline, sizeof newline);
808 dbg("set line: %d %d %d %d", le32_to_cpu(newline.dwDTERate),
809 newline.bCharFormat, newline.bParityType,
811 acm_set_line(acm, &acm->line);
816 * USB probe and disconnect routines.
819 /* Little helpers: write/read buffers free */
820 static void acm_write_buffers_free(struct acm *acm)
825 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
826 usb_buffer_free(acm->dev, acm->writesize, wb->buf, wb->dmah);
830 static void acm_read_buffers_free(struct acm *acm)
832 struct usb_device *usb_dev = interface_to_usbdev(acm->control);
833 int i, n = acm->rx_buflimit;
835 for (i = 0; i < n; i++)
836 usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma);
839 /* Little helper: write buffers allocate */
840 static int acm_write_buffers_alloc(struct acm *acm)
845 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
846 wb->buf = usb_buffer_alloc(acm->dev, acm->writesize, GFP_KERNEL,
852 usb_buffer_free(acm->dev, acm->writesize,
861 static int acm_probe (struct usb_interface *intf,
862 const struct usb_device_id *id)
864 struct usb_cdc_union_desc *union_header = NULL;
865 struct usb_cdc_country_functional_desc *cfd = NULL;
866 unsigned char *buffer = intf->altsetting->extra;
867 int buflen = intf->altsetting->extralen;
868 struct usb_interface *control_interface;
869 struct usb_interface *data_interface;
870 struct usb_endpoint_descriptor *epctrl;
871 struct usb_endpoint_descriptor *epread;
872 struct usb_endpoint_descriptor *epwrite;
873 struct usb_device *usb_dev = interface_to_usbdev(intf);
876 int ctrlsize,readsize;
878 u8 ac_management_function = 0;
879 u8 call_management_function = 0;
880 int call_interface_num = -1;
881 int data_interface_num;
882 unsigned long quirks;
887 quirks = (unsigned long)id->driver_info;
888 num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
890 /* handle quirks deadly to normal probing*/
891 if (quirks == NO_UNION_NORMAL) {
892 data_interface = usb_ifnum_to_if(usb_dev, 1);
893 control_interface = usb_ifnum_to_if(usb_dev, 0);
894 goto skip_normal_probe;
899 err("Weird descriptor references\n");
904 if (intf->cur_altsetting->endpoint->extralen && intf->cur_altsetting->endpoint->extra) {
905 dev_dbg(&intf->dev,"Seeking extra descriptors on endpoint\n");
906 buflen = intf->cur_altsetting->endpoint->extralen;
907 buffer = intf->cur_altsetting->endpoint->extra;
909 err("Zero length descriptor references\n");
915 if (buffer [1] != USB_DT_CS_INTERFACE) {
916 err("skipping garbage\n");
920 switch (buffer [2]) {
921 case USB_CDC_UNION_TYPE: /* we've found it */
923 err("More than one union descriptor, skipping ...");
926 union_header = (struct usb_cdc_union_desc *)
929 case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
930 cfd = (struct usb_cdc_country_functional_desc *)buffer;
932 case USB_CDC_HEADER_TYPE: /* maybe check version */
933 break; /* for now we ignore it */
934 case USB_CDC_ACM_TYPE:
935 ac_management_function = buffer[3];
937 case USB_CDC_CALL_MANAGEMENT_TYPE:
938 call_management_function = buffer[3];
939 call_interface_num = buffer[4];
940 if ((call_management_function & 3) != 3)
941 err("This device cannot do calls on its own. It is no modem.");
944 /* there are LOTS more CDC descriptors that
945 * could legitimately be found here.
947 dev_dbg(&intf->dev, "Ignoring descriptor: "
948 "type %02x, length %d\n",
949 buffer[2], buffer[0]);
958 if (call_interface_num > 0) {
959 dev_dbg(&intf->dev,"No union descriptor, using call management descriptor\n");
960 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
961 control_interface = intf;
963 dev_dbg(&intf->dev,"No union descriptor, giving up\n");
967 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
968 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
969 if (!control_interface || !data_interface) {
970 dev_dbg(&intf->dev,"no interfaces\n");
975 if (data_interface_num != call_interface_num)
976 dev_dbg(&intf->dev,"Separate call control interface. That is not fully supported.\n");
980 /*workaround for switched interfaces */
981 if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) {
982 if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) {
983 struct usb_interface *t;
984 dev_dbg(&intf->dev,"Your device has switched interfaces.\n");
986 t = control_interface;
987 control_interface = data_interface;
994 /* Accept probe requests only for the control interface */
995 if (intf != control_interface)
998 if (usb_interface_claimed(data_interface)) { /* valid in this context */
999 dev_dbg(&intf->dev,"The data interface isn't available\n");
1004 if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
1007 epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
1008 epread = &data_interface->cur_altsetting->endpoint[0].desc;
1009 epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
1012 /* workaround for switched endpoints */
1013 if (!usb_endpoint_dir_in(epread)) {
1014 /* descriptors are swapped */
1015 struct usb_endpoint_descriptor *t;
1016 dev_dbg(&intf->dev,"The data interface has switched endpoints\n");
1022 dbg("interfaces are valid");
1023 for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
1025 if (minor == ACM_TTY_MINORS) {
1026 err("no more free acm devices");
1030 if (!(acm = kzalloc(sizeof(struct acm), GFP_KERNEL))) {
1031 dev_dbg(&intf->dev, "out of memory (acm kzalloc)\n");
1035 ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize);
1036 readsize = le16_to_cpu(epread->wMaxPacketSize)* ( quirks == SINGLE_RX_URB ? 1 : 2);
1037 acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize) * 20;
1038 acm->control = control_interface;
1039 acm->data = data_interface;
1042 acm->ctrl_caps = ac_management_function;
1043 acm->ctrlsize = ctrlsize;
1044 acm->readsize = readsize;
1045 acm->rx_buflimit = num_rx_buf;
1046 acm->urb_task.func = acm_rx_tasklet;
1047 acm->urb_task.data = (unsigned long) acm;
1048 INIT_WORK(&acm->work, acm_softint);
1049 INIT_WORK(&acm->waker, acm_waker);
1050 spin_lock_init(&acm->throttle_lock);
1051 spin_lock_init(&acm->write_lock);
1052 spin_lock_init(&acm->read_lock);
1053 mutex_init(&acm->mutex);
1054 acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
1056 buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
1058 dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)\n");
1061 acm->ctrl_buffer = buf;
1063 if (acm_write_buffers_alloc(acm) < 0) {
1064 dev_dbg(&intf->dev, "out of memory (write buffer alloc)\n");
1068 acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
1069 if (!acm->ctrlurb) {
1070 dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
1073 for (i = 0; i < num_rx_buf; i++) {
1074 struct acm_ru *rcv = &(acm->ru[i]);
1076 if (!(rcv->urb = usb_alloc_urb(0, GFP_KERNEL))) {
1077 dev_dbg(&intf->dev, "out of memory (read urbs usb_alloc_urb)\n");
1081 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1082 rcv->instance = acm;
1084 for (i = 0; i < num_rx_buf; i++) {
1085 struct acm_rb *rb = &(acm->rb[i]);
1087 rb->base = usb_buffer_alloc(acm->dev, readsize,
1088 GFP_KERNEL, &rb->dma);
1090 dev_dbg(&intf->dev, "out of memory (read bufs usb_buffer_alloc)\n");
1094 for(i = 0; i < ACM_NW; i++)
1096 struct acm_wb *snd = &(acm->wb[i]);
1098 if (!(snd->urb = usb_alloc_urb(0, GFP_KERNEL))) {
1099 dev_dbg(&intf->dev, "out of memory (write urbs usb_alloc_urb)");
1103 usb_fill_bulk_urb(snd->urb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
1104 NULL, acm->writesize, acm_write_bulk, snd);
1105 snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1106 snd->instance = acm;
1109 usb_set_intfdata (intf, acm);
1111 i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
1115 if (cfd) { /* export the country data */
1116 acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
1117 if (!acm->country_codes)
1118 goto skip_countries;
1119 acm->country_code_size = cfd->bLength - 4;
1120 memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0, cfd->bLength - 4);
1121 acm->country_rel_date = cfd->iCountryCodeRelDate;
1123 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
1125 kfree(acm->country_codes);
1126 goto skip_countries;
1129 i = device_create_file(&intf->dev, &dev_attr_iCountryCodeRelDate);
1131 kfree(acm->country_codes);
1132 goto skip_countries;
1137 usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
1138 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval);
1139 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1140 acm->ctrlurb->transfer_dma = acm->ctrl_dma;
1142 dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
1144 acm_set_control(acm, acm->ctrlout);
1146 acm->line.dwDTERate = cpu_to_le32(9600);
1147 acm->line.bDataBits = 8;
1148 acm_set_line(acm, &acm->line);
1150 usb_driver_claim_interface(&acm_driver, data_interface, acm);
1151 usb_set_intfdata(data_interface, acm);
1153 usb_get_intf(control_interface);
1154 tty_register_device(acm_tty_driver, minor, &control_interface->dev);
1156 acm_table[minor] = acm;
1160 for (i = 0; i < ACM_NW; i++)
1161 usb_free_urb(acm->wb[i].urb);
1163 acm_read_buffers_free(acm);
1164 for (i = 0; i < num_rx_buf; i++)
1165 usb_free_urb(acm->ru[i].urb);
1166 usb_free_urb(acm->ctrlurb);
1168 acm_write_buffers_free(acm);
1170 usb_buffer_free(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1177 static void stop_data_traffic(struct acm *acm)
1180 dbg("Entering stop_data_traffic");
1182 tasklet_disable(&acm->urb_task);
1184 usb_kill_urb(acm->ctrlurb);
1185 for(i = 0; i < ACM_NW; i++)
1186 usb_kill_urb(acm->wb[i].urb);
1187 for (i = 0; i < acm->rx_buflimit; i++)
1188 usb_kill_urb(acm->ru[i].urb);
1190 tasklet_enable(&acm->urb_task);
1192 cancel_work_sync(&acm->work);
1193 cancel_work_sync(&acm->waker);
1196 static void acm_disconnect(struct usb_interface *intf)
1198 struct acm *acm = usb_get_intfdata(intf);
1199 struct usb_device *usb_dev = interface_to_usbdev(intf);
1201 /* sibling interface is already cleaning up */
1205 mutex_lock(&open_mutex);
1206 if (acm->country_codes){
1207 device_remove_file(&acm->control->dev,
1208 &dev_attr_wCountryCodes);
1209 device_remove_file(&acm->control->dev,
1210 &dev_attr_iCountryCodeRelDate);
1212 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
1214 usb_set_intfdata(acm->control, NULL);
1215 usb_set_intfdata(acm->data, NULL);
1217 stop_data_traffic(acm);
1219 acm_write_buffers_free(acm);
1220 usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1221 acm_read_buffers_free(acm);
1223 usb_driver_release_interface(&acm_driver, intf == acm->control ?
1224 acm->data : acm->control);
1227 acm_tty_unregister(acm);
1228 mutex_unlock(&open_mutex);
1232 mutex_unlock(&open_mutex);
1235 tty_hangup(acm->tty);
1239 static int acm_suspend(struct usb_interface *intf, pm_message_t message)
1241 struct acm *acm = usb_get_intfdata(intf);
1244 if (acm->dev->auto_pm) {
1247 spin_lock_irq(&acm->read_lock);
1248 spin_lock(&acm->write_lock);
1249 b = acm->processing + acm->transmitting;
1250 spin_unlock(&acm->write_lock);
1251 spin_unlock_irq(&acm->read_lock);
1256 spin_lock_irq(&acm->read_lock);
1257 spin_lock(&acm->write_lock);
1258 cnt = acm->susp_count++;
1259 spin_unlock(&acm->write_lock);
1260 spin_unlock_irq(&acm->read_lock);
1265 we treat opened interfaces differently,
1266 we must guard against open
1268 mutex_lock(&acm->mutex);
1271 stop_data_traffic(acm);
1273 mutex_unlock(&acm->mutex);
1277 static int acm_resume(struct usb_interface *intf)
1279 struct acm *acm = usb_get_intfdata(intf);
1283 spin_lock_irq(&acm->read_lock);
1284 acm->susp_count -= 1;
1285 cnt = acm->susp_count;
1286 spin_unlock_irq(&acm->read_lock);
1291 mutex_lock(&acm->mutex);
1293 rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO);
1297 tasklet_schedule(&acm->urb_task);
1301 mutex_unlock(&acm->mutex);
1305 #endif /* CONFIG_PM */
1307 * USB driver structure.
1310 static struct usb_device_id acm_ids[] = {
1311 /* quirky and broken devices */
1312 { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
1313 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1315 { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
1316 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1318 { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
1319 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1321 { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
1322 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1324 { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
1325 .driver_info = SINGLE_RX_URB, /* firmware bug */
1327 { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
1328 .driver_info = SINGLE_RX_URB, /* firmware bug */
1330 { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
1331 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1333 { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
1334 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1337 /* control interfaces with various AT-command sets */
1338 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1339 USB_CDC_ACM_PROTO_AT_V25TER) },
1340 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1341 USB_CDC_ACM_PROTO_AT_PCCA101) },
1342 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1343 USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
1344 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1345 USB_CDC_ACM_PROTO_AT_GSM) },
1346 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1347 USB_CDC_ACM_PROTO_AT_3G ) },
1348 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1349 USB_CDC_ACM_PROTO_AT_CDMA) },
1351 /* NOTE: COMM/ACM/0xff is likely MSFT RNDIS ... NOT a modem!! */
1355 MODULE_DEVICE_TABLE (usb, acm_ids);
1357 static struct usb_driver acm_driver = {
1360 .disconnect = acm_disconnect,
1362 .suspend = acm_suspend,
1363 .resume = acm_resume,
1365 .id_table = acm_ids,
1367 .supports_autosuspend = 1,
1372 * TTY driver structures.
1375 static const struct tty_operations acm_ops = {
1376 .open = acm_tty_open,
1377 .close = acm_tty_close,
1378 .write = acm_tty_write,
1379 .write_room = acm_tty_write_room,
1380 .ioctl = acm_tty_ioctl,
1381 .throttle = acm_tty_throttle,
1382 .unthrottle = acm_tty_unthrottle,
1383 .chars_in_buffer = acm_tty_chars_in_buffer,
1384 .break_ctl = acm_tty_break_ctl,
1385 .set_termios = acm_tty_set_termios,
1386 .tiocmget = acm_tty_tiocmget,
1387 .tiocmset = acm_tty_tiocmset,
1394 static int __init acm_init(void)
1397 acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
1398 if (!acm_tty_driver)
1400 acm_tty_driver->owner = THIS_MODULE,
1401 acm_tty_driver->driver_name = "acm",
1402 acm_tty_driver->name = "ttyACM",
1403 acm_tty_driver->major = ACM_TTY_MAJOR,
1404 acm_tty_driver->minor_start = 0,
1405 acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
1406 acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
1407 acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1408 acm_tty_driver->init_termios = tty_std_termios;
1409 acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1410 tty_set_operations(acm_tty_driver, &acm_ops);
1412 retval = tty_register_driver(acm_tty_driver);
1414 put_tty_driver(acm_tty_driver);
1418 retval = usb_register(&acm_driver);
1420 tty_unregister_driver(acm_tty_driver);
1421 put_tty_driver(acm_tty_driver);
1425 info(DRIVER_VERSION ":" DRIVER_DESC);
1430 static void __exit acm_exit(void)
1432 usb_deregister(&acm_driver);
1433 tty_unregister_driver(acm_tty_driver);
1434 put_tty_driver(acm_tty_driver);
1437 module_init(acm_init);
1438 module_exit(acm_exit);
1440 MODULE_AUTHOR( DRIVER_AUTHOR );
1441 MODULE_DESCRIPTION( DRIVER_DESC );
1442 MODULE_LICENSE("GPL");