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
37 * This program is free software; you can redistribute it and/or modify
38 * it under the terms of the GNU General Public License as published by
39 * the Free Software Foundation; either version 2 of the License, or
40 * (at your option) any later version.
42 * This program is distributed in the hope that it will be useful,
43 * but WITHOUT ANY WARRANTY; without even the implied warranty of
44 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 * GNU General Public License for more details.
47 * You should have received a copy of the GNU General Public License
48 * along with this program; if not, write to the Free Software
49 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
54 #include <linux/kernel.h>
55 #include <linux/errno.h>
56 #include <linux/init.h>
57 #include <linux/slab.h>
58 #include <linux/tty.h>
59 #include <linux/tty_driver.h>
60 #include <linux/tty_flip.h>
61 #include <linux/module.h>
62 #include <linux/smp_lock.h>
63 #include <asm/uaccess.h>
64 #include <linux/usb.h>
65 #include <linux/usb_cdc.h>
66 #include <asm/byteorder.h>
67 #include <asm/unaligned.h>
68 #include <linux/list.h>
75 #define DRIVER_VERSION "v0.25"
76 #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek"
77 #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
79 static struct usb_driver acm_driver;
80 static struct tty_driver *acm_tty_driver;
81 static struct acm *acm_table[ACM_TTY_MINORS];
83 static DECLARE_MUTEX(open_sem);
85 #define ACM_READY(acm) (acm && acm->dev && acm->used)
88 * Functions for ACM control messages.
91 static int acm_ctrl_msg(struct acm *acm, int request, int value, void *buf, int len)
93 int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
94 request, USB_RT_ACM, value,
95 acm->control->altsetting[0].desc.bInterfaceNumber,
97 dbg("acm_control_msg: rq: 0x%02x val: %#x len: %#x result: %d", request, value, len, retval);
98 return retval < 0 ? retval : 0;
101 /* devices aren't required to support these requests.
102 * the cdc acm descriptor tells whether they do...
104 #define acm_set_control(acm, control) \
105 acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0)
106 #define acm_set_line(acm, line) \
107 acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
108 #define acm_send_break(acm, ms) \
109 acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
112 * Write buffer management.
113 * All of these assume proper locks taken by the caller.
116 static int acm_wb_alloc(struct acm *acm)
121 wbn = acm->write_current;
129 wbn = (wbn + 1) % ACM_NWB;
135 static void acm_wb_free(struct acm *acm, int wbn)
137 acm->wb[wbn].use = 0;
140 static int acm_wb_is_avail(struct acm *acm)
145 for (i = 0; i < ACM_NWB; i++) {
152 static inline int acm_wb_is_used(struct acm *acm, int wbn)
154 return acm->wb[wbn].use;
160 static void acm_write_done(struct acm *acm)
165 spin_lock_irqsave(&acm->write_lock, flags);
166 acm->write_ready = 1;
167 wbn = acm->write_current;
168 acm_wb_free(acm, wbn);
169 acm->write_current = (wbn + 1) % ACM_NWB;
170 spin_unlock_irqrestore(&acm->write_lock, flags);
176 static int acm_write_start(struct acm *acm)
183 spin_lock_irqsave(&acm->write_lock, flags);
185 spin_unlock_irqrestore(&acm->write_lock, flags);
189 if (!acm->write_ready) {
190 spin_unlock_irqrestore(&acm->write_lock, flags);
191 return 0; /* A white lie */
194 wbn = acm->write_current;
195 if (!acm_wb_is_used(acm, wbn)) {
196 spin_unlock_irqrestore(&acm->write_lock, flags);
201 acm->write_ready = 0;
202 spin_unlock_irqrestore(&acm->write_lock, flags);
204 acm->writeurb->transfer_buffer = wb->buf;
205 acm->writeurb->transfer_dma = wb->dmah;
206 acm->writeurb->transfer_buffer_length = wb->len;
207 acm->writeurb->dev = acm->dev;
209 if ((rc = usb_submit_urb(acm->writeurb, GFP_ATOMIC)) < 0) {
210 dbg("usb_submit_urb(write bulk) failed: %d", rc);
217 * Interrupt handlers for various ACM device responses
220 /* control interface reports status changes with "interrupt" transfers */
221 static void acm_ctrl_irq(struct urb *urb, struct pt_regs *regs)
223 struct acm *acm = urb->context;
224 struct usb_cdc_notification *dr = urb->transfer_buffer;
229 switch (urb->status) {
236 /* this urb is terminated, clean up */
237 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
240 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
247 data = (unsigned char *)(dr + 1);
248 switch (dr->bNotificationType) {
250 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
252 dbg("%s network", dr->wValue ? "connected to" : "disconnected from");
255 case USB_CDC_NOTIFY_SERIAL_STATE:
257 newctrl = le16_to_cpu(get_unaligned((__le16 *) data));
259 if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
260 dbg("calling hangup");
261 tty_hangup(acm->tty);
264 acm->ctrlin = newctrl;
266 dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c",
267 acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
268 acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
269 acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-', acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
270 acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
275 dbg("unknown notification %d received: index %d len %d data0 %d data1 %d",
276 dr->bNotificationType, dr->wIndex,
277 dr->wLength, data[0], data[1]);
281 status = usb_submit_urb (urb, GFP_ATOMIC);
283 err ("%s - usb_submit_urb failed with result %d",
284 __FUNCTION__, status);
287 /* data interface returns incoming bytes, or we got unthrottled */
288 static void acm_read_bulk(struct urb *urb, struct pt_regs *regs)
291 struct acm_ru *rcv = urb->context;
292 struct acm *acm = rcv->instance;
293 dbg("Entering acm_read_bulk with status %d\n", urb->status);
299 dev_dbg(&acm->data->dev, "bulk rx status %d\n", urb->status);
302 buf->size = urb->actual_length;
304 spin_lock(&acm->read_lock);
305 list_add_tail(&rcv->list, &acm->spare_read_urbs);
306 list_add_tail(&buf->list, &acm->filled_read_bufs);
307 spin_unlock(&acm->read_lock);
309 tasklet_schedule(&acm->urb_task);
312 static void acm_rx_tasklet(unsigned long _acm)
314 struct acm *acm = (void *)_acm;
316 struct tty_struct *tty = acm->tty;
318 //unsigned long flags;
320 dbg("Entering acm_rx_tasklet");
322 if (!ACM_READY(acm) || acm->throttle)
326 spin_lock(&acm->read_lock);
327 if (list_empty(&acm->filled_read_bufs)) {
328 spin_unlock(&acm->read_lock);
331 buf = list_entry(acm->filled_read_bufs.next,
332 struct acm_rb, list);
333 list_del(&buf->list);
334 spin_unlock(&acm->read_lock);
336 dbg("acm_rx_tasklet: procesing buf 0x%p, size = %d\n", buf, buf->size);
338 tty_buffer_request_room(tty, buf->size);
340 tty_insert_flip_string(tty, buf->base, buf->size);
341 tty_flip_buffer_push(tty);
343 spin_lock(&acm->throttle_lock);
345 dbg("Throtteling noticed");
346 memmove(buf->base, buf->base + i, buf->size - i);
348 spin_unlock(&acm->throttle_lock);
349 spin_lock(&acm->read_lock);
350 list_add(&buf->list, &acm->filled_read_bufs);
351 spin_unlock(&acm->read_lock);
354 spin_unlock(&acm->throttle_lock);
356 spin_lock(&acm->read_lock);
357 list_add(&buf->list, &acm->spare_read_bufs);
358 spin_unlock(&acm->read_lock);
362 while (!list_empty(&acm->spare_read_bufs)) {
363 spin_lock(&acm->read_lock);
364 if (list_empty(&acm->spare_read_urbs)) {
365 spin_unlock(&acm->read_lock);
368 rcv = list_entry(acm->spare_read_urbs.next,
369 struct acm_ru, list);
370 list_del(&rcv->list);
371 spin_unlock(&acm->read_lock);
373 buf = list_entry(acm->spare_read_bufs.next,
374 struct acm_rb, list);
375 list_del(&buf->list);
379 usb_fill_bulk_urb(rcv->urb, acm->dev,
384 rcv->urb->transfer_dma = buf->dma;
385 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
387 dbg("acm_rx_tasklet: sending urb 0x%p, rcv 0x%p, buf 0x%p\n", rcv->urb, rcv, buf);
389 /* This shouldn't kill the driver as unsuccessful URBs are returned to the
390 free-urbs-pool and resubmited ASAP */
391 if (usb_submit_urb(rcv->urb, GFP_ATOMIC) < 0) {
392 list_add(&buf->list, &acm->spare_read_bufs);
393 spin_lock(&acm->read_lock);
394 list_add(&rcv->list, &acm->spare_read_urbs);
395 spin_unlock(&acm->read_lock);
401 /* data interface wrote those outgoing bytes */
402 static void acm_write_bulk(struct urb *urb, struct pt_regs *regs)
404 struct acm *acm = (struct acm *)urb->context;
406 dbg("Entering acm_write_bulk with status %d\n", urb->status);
409 acm_write_start(acm);
411 schedule_work(&acm->work);
414 static void acm_softint(void *private)
416 struct acm *acm = private;
417 dbg("Entering acm_softint.\n");
421 tty_wakeup(acm->tty);
428 static int acm_tty_open(struct tty_struct *tty, struct file *filp)
433 dbg("Entering acm_tty_open.\n");
437 acm = acm_table[tty->index];
438 if (!acm || !acm->dev)
443 tty->driver_data = acm;
446 /* force low_latency on so that our tty_push actually forces the data through,
447 otherwise it is scheduled, and with high data rates data can get lost. */
448 tty->low_latency = 1;
454 acm->ctrlurb->dev = acm->dev;
455 if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
456 dbg("usb_submit_urb(ctrl irq) failed");
460 if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS))
463 INIT_LIST_HEAD(&acm->spare_read_urbs);
464 INIT_LIST_HEAD(&acm->spare_read_bufs);
465 INIT_LIST_HEAD(&acm->filled_read_bufs);
466 for (i = 0; i < ACM_NRU; i++) {
467 list_add(&(acm->ru[i].list), &acm->spare_read_urbs);
469 for (i = 0; i < ACM_NRB; i++) {
470 list_add(&(acm->rb[i].list), &acm->spare_read_bufs);
473 tasklet_schedule(&acm->urb_task);
481 usb_kill_urb(acm->ctrlurb);
488 static void acm_tty_unregister(struct acm *acm)
492 tty_unregister_device(acm_tty_driver, acm->minor);
493 usb_put_intf(acm->control);
494 acm_table[acm->minor] = NULL;
495 usb_free_urb(acm->ctrlurb);
496 usb_free_urb(acm->writeurb);
497 for (i = 0; i < ACM_NRU; i++)
498 usb_free_urb(acm->ru[i].urb);
502 static void acm_tty_close(struct tty_struct *tty, struct file *filp)
504 struct acm *acm = tty->driver_data;
507 if (!acm || !acm->used)
513 acm_set_control(acm, acm->ctrlout = 0);
514 usb_kill_urb(acm->ctrlurb);
515 usb_kill_urb(acm->writeurb);
516 for (i = 0; i < ACM_NRU; i++)
517 usb_kill_urb(acm->ru[i].urb);
519 acm_tty_unregister(acm);
524 static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
526 struct acm *acm = tty->driver_data;
532 dbg("Entering acm_tty_write to write %d bytes,\n", count);
539 spin_lock_irqsave(&acm->write_lock, flags);
540 if ((wbn = acm_wb_alloc(acm)) < 0) {
541 spin_unlock_irqrestore(&acm->write_lock, flags);
542 acm_write_start(acm);
547 count = (count > acm->writesize) ? acm->writesize : count;
548 dbg("Get %d bytes...", count);
549 memcpy(wb->buf, buf, count);
551 spin_unlock_irqrestore(&acm->write_lock, flags);
553 if ((stat = acm_write_start(acm)) < 0)
558 static int acm_tty_write_room(struct tty_struct *tty)
560 struct acm *acm = tty->driver_data;
564 * Do not let the line discipline to know that we have a reserve,
565 * or it might get too enthusiastic.
567 return (acm->write_ready && acm_wb_is_avail(acm)) ? acm->writesize : 0;
570 static int acm_tty_chars_in_buffer(struct tty_struct *tty)
572 struct acm *acm = tty->driver_data;
576 * This is inaccurate (overcounts), but it works.
578 return (ACM_NWB - acm_wb_is_avail(acm)) * acm->writesize;
581 static void acm_tty_throttle(struct tty_struct *tty)
583 struct acm *acm = tty->driver_data;
586 spin_lock_bh(&acm->throttle_lock);
588 spin_unlock_bh(&acm->throttle_lock);
591 static void acm_tty_unthrottle(struct tty_struct *tty)
593 struct acm *acm = tty->driver_data;
596 spin_lock_bh(&acm->throttle_lock);
598 spin_unlock_bh(&acm->throttle_lock);
599 tasklet_schedule(&acm->urb_task);
602 static void acm_tty_break_ctl(struct tty_struct *tty, int state)
604 struct acm *acm = tty->driver_data;
607 if (acm_send_break(acm, state ? 0xffff : 0))
608 dbg("send break failed");
611 static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file)
613 struct acm *acm = tty->driver_data;
618 return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
619 (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
620 (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
621 (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
622 (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
626 static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file,
627 unsigned int set, unsigned int clear)
629 struct acm *acm = tty->driver_data;
630 unsigned int newctrl;
635 newctrl = acm->ctrlout;
636 set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
637 clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
639 newctrl = (newctrl & ~clear) | set;
641 if (acm->ctrlout == newctrl)
643 return acm_set_control(acm, acm->ctrlout = newctrl);
646 static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
648 struct acm *acm = tty->driver_data;
656 static const __u32 acm_tty_speed[] = {
657 0, 50, 75, 110, 134, 150, 200, 300, 600,
658 1200, 1800, 2400, 4800, 9600, 19200, 38400,
659 57600, 115200, 230400, 460800, 500000, 576000,
660 921600, 1000000, 1152000, 1500000, 2000000,
661 2500000, 3000000, 3500000, 4000000
664 static const __u8 acm_tty_size[] = {
668 static void acm_tty_set_termios(struct tty_struct *tty, struct termios *termios_old)
670 struct acm *acm = tty->driver_data;
671 struct termios *termios = tty->termios;
672 struct usb_cdc_line_coding newline;
673 int newctrl = acm->ctrlout;
678 newline.dwDTERate = cpu_to_le32p(acm_tty_speed +
679 (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0));
680 newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
681 newline.bParityType = termios->c_cflag & PARENB ?
682 (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
683 newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
685 acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
687 if (!newline.dwDTERate) {
688 newline.dwDTERate = acm->line.dwDTERate;
689 newctrl &= ~ACM_CTRL_DTR;
690 } else newctrl |= ACM_CTRL_DTR;
692 if (newctrl != acm->ctrlout)
693 acm_set_control(acm, acm->ctrlout = newctrl);
695 if (memcmp(&acm->line, &newline, sizeof newline)) {
696 memcpy(&acm->line, &newline, sizeof newline);
697 dbg("set line: %d %d %d %d", le32_to_cpu(newline.dwDTERate),
698 newline.bCharFormat, newline.bParityType,
700 acm_set_line(acm, &acm->line);
705 * USB probe and disconnect routines.
708 /* Little helper: write buffers free */
709 static void acm_write_buffers_free(struct acm *acm)
714 for (wb = &acm->wb[0], i = 0; i < ACM_NWB; i++, wb++) {
715 usb_buffer_free(acm->dev, acm->writesize, wb->buf, wb->dmah);
719 /* Little helper: write buffers allocate */
720 static int acm_write_buffers_alloc(struct acm *acm)
725 for (wb = &acm->wb[0], i = 0; i < ACM_NWB; i++, wb++) {
726 wb->buf = usb_buffer_alloc(acm->dev, acm->writesize, GFP_KERNEL,
732 usb_buffer_free(acm->dev, acm->writesize,
741 static int acm_probe (struct usb_interface *intf,
742 const struct usb_device_id *id)
744 struct usb_cdc_union_desc *union_header = NULL;
745 char *buffer = intf->altsetting->extra;
746 int buflen = intf->altsetting->extralen;
747 struct usb_interface *control_interface;
748 struct usb_interface *data_interface;
749 struct usb_endpoint_descriptor *epctrl;
750 struct usb_endpoint_descriptor *epread;
751 struct usb_endpoint_descriptor *epwrite;
752 struct usb_device *usb_dev = interface_to_usbdev(intf);
755 int ctrlsize,readsize;
757 u8 ac_management_function = 0;
758 u8 call_management_function = 0;
759 int call_interface_num = -1;
760 int data_interface_num;
761 unsigned long quirks;
764 /* handle quirks deadly to normal probing*/
765 quirks = (unsigned long)id->driver_info;
766 if (quirks == NO_UNION_NORMAL) {
767 data_interface = usb_ifnum_to_if(usb_dev, 1);
768 control_interface = usb_ifnum_to_if(usb_dev, 0);
769 goto skip_normal_probe;
774 err("Wierd descriptor references\n");
779 if (intf->cur_altsetting->endpoint->extralen && intf->cur_altsetting->endpoint->extra) {
780 dev_dbg(&intf->dev,"Seeking extra descriptors on endpoint\n");
781 buflen = intf->cur_altsetting->endpoint->extralen;
782 buffer = intf->cur_altsetting->endpoint->extra;
784 err("Zero length descriptor references\n");
790 if (buffer [1] != USB_DT_CS_INTERFACE) {
791 err("skipping garbage\n");
795 switch (buffer [2]) {
796 case USB_CDC_UNION_TYPE: /* we've found it */
798 err("More than one union descriptor, skipping ...");
801 union_header = (struct usb_cdc_union_desc *)
804 case USB_CDC_COUNTRY_TYPE: /* maybe somehow export */
805 break; /* for now we ignore it */
806 case USB_CDC_HEADER_TYPE: /* maybe check version */
807 break; /* for now we ignore it */
808 case USB_CDC_ACM_TYPE:
809 ac_management_function = buffer[3];
811 case USB_CDC_CALL_MANAGEMENT_TYPE:
812 call_management_function = buffer[3];
813 call_interface_num = buffer[4];
814 if ((call_management_function & 3) != 3)
815 err("This device cannot do calls on its own. It is no modem.");
819 err("Ignoring extra header, type %d, length %d", buffer[2], buffer[0]);
828 if (call_interface_num > 0) {
829 dev_dbg(&intf->dev,"No union descriptor, using call management descriptor\n");
830 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
831 control_interface = intf;
833 dev_dbg(&intf->dev,"No union descriptor, giving up\n");
837 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
838 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
839 if (!control_interface || !data_interface) {
840 dev_dbg(&intf->dev,"no interfaces\n");
845 if (data_interface_num != call_interface_num)
846 dev_dbg(&intf->dev,"Seperate call control interface. That is not fully supported.\n");
850 /*workaround for switched interfaces */
851 if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) {
852 if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) {
853 struct usb_interface *t;
854 dev_dbg(&intf->dev,"Your device has switched interfaces.\n");
856 t = control_interface;
857 control_interface = data_interface;
864 if (usb_interface_claimed(data_interface)) { /* valid in this context */
865 dev_dbg(&intf->dev,"The data interface isn't available\n");
870 if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
873 epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
874 epread = &data_interface->cur_altsetting->endpoint[0].desc;
875 epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
878 /* workaround for switched endpoints */
879 if ((epread->bEndpointAddress & USB_DIR_IN) != USB_DIR_IN) {
880 /* descriptors are swapped */
881 struct usb_endpoint_descriptor *t;
882 dev_dbg(&intf->dev,"The data interface has switched endpoints\n");
888 dbg("interfaces are valid");
889 for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
891 if (minor == ACM_TTY_MINORS) {
892 err("no more free acm devices");
896 if (!(acm = kzalloc(sizeof(struct acm), GFP_KERNEL))) {
897 dev_dbg(&intf->dev, "out of memory (acm kzalloc)\n");
901 ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize);
902 readsize = le16_to_cpu(epread->wMaxPacketSize)*2;
903 acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize);
904 acm->control = control_interface;
905 acm->data = data_interface;
908 acm->ctrl_caps = ac_management_function;
909 acm->ctrlsize = ctrlsize;
910 acm->readsize = readsize;
911 acm->urb_task.func = acm_rx_tasklet;
912 acm->urb_task.data = (unsigned long) acm;
913 INIT_WORK(&acm->work, acm_softint, acm);
914 spin_lock_init(&acm->throttle_lock);
915 spin_lock_init(&acm->write_lock);
916 spin_lock_init(&acm->read_lock);
917 acm->write_ready = 1;
918 acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
920 buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
922 dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)\n");
925 acm->ctrl_buffer = buf;
927 if (acm_write_buffers_alloc(acm) < 0) {
928 dev_dbg(&intf->dev, "out of memory (write buffer alloc)\n");
932 acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
934 dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
937 for (i = 0; i < ACM_NRU; i++) {
938 struct acm_ru *rcv = &(acm->ru[i]);
940 if (!(rcv->urb = usb_alloc_urb(0, GFP_KERNEL))) {
941 dev_dbg(&intf->dev, "out of memory (read urbs usb_alloc_urb)\n");
945 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
948 for (i = 0; i < ACM_NRB; i++) {
949 struct acm_rb *buf = &(acm->rb[i]);
951 // Using usb_buffer_alloc instead of kmalloc as Oliver suggested
952 if (!(buf->base = usb_buffer_alloc(acm->dev, readsize, GFP_KERNEL, &buf->dma))) {
953 dev_dbg(&intf->dev, "out of memory (read bufs usb_buffer_alloc)\n");
957 acm->writeurb = usb_alloc_urb(0, GFP_KERNEL);
958 if (!acm->writeurb) {
959 dev_dbg(&intf->dev, "out of memory (writeurb kmalloc)\n");
963 usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
964 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval);
965 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
966 acm->ctrlurb->transfer_dma = acm->ctrl_dma;
968 usb_fill_bulk_urb(acm->writeurb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
969 NULL, acm->writesize, acm_write_bulk, acm);
970 acm->writeurb->transfer_flags |= URB_NO_FSBR | URB_NO_TRANSFER_DMA_MAP;
972 dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
974 acm_set_control(acm, acm->ctrlout);
976 acm->line.dwDTERate = cpu_to_le32(9600);
977 acm->line.bDataBits = 8;
978 acm_set_line(acm, &acm->line);
980 usb_driver_claim_interface(&acm_driver, data_interface, acm);
982 usb_get_intf(control_interface);
983 tty_register_device(acm_tty_driver, minor, &control_interface->dev);
985 acm_table[minor] = acm;
986 usb_set_intfdata (intf, acm);
990 for (i = 0; i < ACM_NRB; i++)
991 usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma);
992 for (i = 0; i < ACM_NRU; i++)
993 usb_free_urb(acm->ru[i].urb);
994 usb_free_urb(acm->ctrlurb);
996 acm_write_buffers_free(acm);
998 usb_buffer_free(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1005 static void acm_disconnect(struct usb_interface *intf)
1007 struct acm *acm = usb_get_intfdata (intf);
1008 struct usb_device *usb_dev = interface_to_usbdev(intf);
1011 if (!acm || !acm->dev) {
1012 dbg("disconnect on nonexisting interface");
1017 if (!usb_get_intfdata(intf)) {
1022 usb_set_intfdata(acm->control, NULL);
1023 usb_set_intfdata(acm->data, NULL);
1025 tasklet_disable(&acm->urb_task);
1027 usb_kill_urb(acm->ctrlurb);
1028 usb_kill_urb(acm->writeurb);
1029 for (i = 0; i < ACM_NRU; i++)
1030 usb_kill_urb(acm->ru[i].urb);
1032 INIT_LIST_HEAD(&acm->filled_read_bufs);
1033 INIT_LIST_HEAD(&acm->spare_read_bufs);
1035 tasklet_enable(&acm->urb_task);
1037 flush_scheduled_work(); /* wait for acm_softint */
1039 acm_write_buffers_free(acm);
1040 usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1041 for (i = 0; i < ACM_NRB; i++)
1042 usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma);
1044 usb_driver_release_interface(&acm_driver, intf == acm->control ? acm->data : intf);
1047 acm_tty_unregister(acm);
1055 tty_hangup(acm->tty);
1059 * USB driver structure.
1062 static struct usb_device_id acm_ids[] = {
1063 /* quirky and broken devices */
1064 { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
1065 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1067 { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
1068 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1070 /* control interfaces with various AT-command sets */
1071 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1072 USB_CDC_ACM_PROTO_AT_V25TER) },
1073 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1074 USB_CDC_ACM_PROTO_AT_PCCA101) },
1075 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1076 USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
1077 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1078 USB_CDC_ACM_PROTO_AT_GSM) },
1079 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1080 USB_CDC_ACM_PROTO_AT_3G ) },
1081 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1082 USB_CDC_ACM_PROTO_AT_CDMA) },
1084 /* NOTE: COMM/ACM/0xff is likely MSFT RNDIS ... NOT a modem!! */
1088 MODULE_DEVICE_TABLE (usb, acm_ids);
1090 static struct usb_driver acm_driver = {
1093 .disconnect = acm_disconnect,
1094 .id_table = acm_ids,
1098 * TTY driver structures.
1101 static struct tty_operations acm_ops = {
1102 .open = acm_tty_open,
1103 .close = acm_tty_close,
1104 .write = acm_tty_write,
1105 .write_room = acm_tty_write_room,
1106 .ioctl = acm_tty_ioctl,
1107 .throttle = acm_tty_throttle,
1108 .unthrottle = acm_tty_unthrottle,
1109 .chars_in_buffer = acm_tty_chars_in_buffer,
1110 .break_ctl = acm_tty_break_ctl,
1111 .set_termios = acm_tty_set_termios,
1112 .tiocmget = acm_tty_tiocmget,
1113 .tiocmset = acm_tty_tiocmset,
1120 static int __init acm_init(void)
1123 acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
1124 if (!acm_tty_driver)
1126 acm_tty_driver->owner = THIS_MODULE,
1127 acm_tty_driver->driver_name = "acm",
1128 acm_tty_driver->name = "ttyACM",
1129 acm_tty_driver->devfs_name = "usb/acm/",
1130 acm_tty_driver->major = ACM_TTY_MAJOR,
1131 acm_tty_driver->minor_start = 0,
1132 acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
1133 acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
1134 acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
1135 acm_tty_driver->init_termios = tty_std_termios;
1136 acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1137 tty_set_operations(acm_tty_driver, &acm_ops);
1139 retval = tty_register_driver(acm_tty_driver);
1141 put_tty_driver(acm_tty_driver);
1145 retval = usb_register(&acm_driver);
1147 tty_unregister_driver(acm_tty_driver);
1148 put_tty_driver(acm_tty_driver);
1152 info(DRIVER_VERSION ":" DRIVER_DESC);
1157 static void __exit acm_exit(void)
1159 usb_deregister(&acm_driver);
1160 tty_unregister_driver(acm_tty_driver);
1161 put_tty_driver(acm_tty_driver);
1164 module_init(acm_init);
1165 module_exit(acm_exit);
1167 MODULE_AUTHOR( DRIVER_AUTHOR );
1168 MODULE_DESCRIPTION( DRIVER_DESC );
1169 MODULE_LICENSE("GPL");