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>
10 * USB Abstract Control Model driver for USB modems and ISDN adapters
15 * v0.9 - thorough cleaning, URBification, almost a rewrite
16 * v0.10 - some more cleanups
17 * v0.11 - fixed flow control, read error doesn't stop reads
18 * v0.12 - added TIOCM ioctls, added break handling, made struct acm kmalloced
19 * v0.13 - added termios, added hangup
20 * v0.14 - sized down struct acm
21 * v0.15 - fixed flow control again - characters could be lost
22 * v0.16 - added code for modems with swapped data and control interfaces
23 * v0.17 - added new style probing
24 * v0.18 - fixed new style probing for devices with more configurations
25 * v0.19 - fixed CLOCAL handling (thanks to Richard Shih-Ping Chan)
26 * v0.20 - switched to probing on interface (rather than device) class
27 * v0.21 - revert to probing on device for devices with multiple configs
28 * v0.22 - probe only the control interface. if usbcore doesn't choose the
29 * config we want, sysadmin changes bConfigurationValue in sysfs.
30 * v0.23 - use softirq for rx processing, as needed by tty layer
31 * v0.24 - change probe method to evaluate CDC union descriptor
35 * This program is free software; you can redistribute it and/or modify
36 * it under the terms of the GNU General Public License as published by
37 * the Free Software Foundation; either version 2 of the License, or
38 * (at your option) any later version.
40 * This program is distributed in the hope that it will be useful,
41 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 * GNU General Public License for more details.
45 * You should have received a copy of the GNU General Public License
46 * along with this program; if not, write to the Free Software
47 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
52 #include <linux/kernel.h>
53 #include <linux/errno.h>
54 #include <linux/init.h>
55 #include <linux/slab.h>
56 #include <linux/tty.h>
57 #include <linux/tty_driver.h>
58 #include <linux/tty_flip.h>
59 #include <linux/module.h>
60 #include <linux/smp_lock.h>
61 #include <asm/uaccess.h>
62 #include <linux/usb.h>
63 #include <linux/usb_cdc.h>
64 #include <asm/byteorder.h>
65 #include <asm/unaligned.h>
72 #define DRIVER_VERSION "v0.23"
73 #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik"
74 #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
76 static struct usb_driver acm_driver;
77 static struct tty_driver *acm_tty_driver;
78 static struct acm *acm_table[ACM_TTY_MINORS];
80 static DECLARE_MUTEX(open_sem);
82 #define ACM_READY(acm) (acm && acm->dev && acm->used)
85 * Functions for ACM control messages.
88 static int acm_ctrl_msg(struct acm *acm, int request, int value, void *buf, int len)
90 int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
91 request, USB_RT_ACM, value,
92 acm->control->altsetting[0].desc.bInterfaceNumber,
94 dbg("acm_control_msg: rq: 0x%02x val: %#x len: %#x result: %d", request, value, len, retval);
95 return retval < 0 ? retval : 0;
98 /* devices aren't required to support these requests.
99 * the cdc acm descriptor tells whether they do...
101 #define acm_set_control(acm, control) \
102 acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0)
103 #define acm_set_line(acm, line) \
104 acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
105 #define acm_send_break(acm, ms) \
106 acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
109 * Write buffer management.
110 * All of these assume proper locks taken by the caller.
113 static int acm_wb_alloc(struct acm *acm)
118 wbn = acm->write_current;
126 wbn = (wbn + 1) % ACM_NWB;
132 static void acm_wb_free(struct acm *acm, int wbn)
134 acm->wb[wbn].use = 0;
137 static int acm_wb_is_avail(struct acm *acm)
142 for (i = 0; i < ACM_NWB; i++) {
149 static inline int acm_wb_is_used(struct acm *acm, int wbn)
151 return acm->wb[wbn].use;
157 static void acm_write_done(struct acm *acm)
162 spin_lock_irqsave(&acm->write_lock, flags);
163 acm->write_ready = 1;
164 wbn = acm->write_current;
165 acm_wb_free(acm, wbn);
166 acm->write_current = (wbn + 1) % ACM_NWB;
167 spin_unlock_irqrestore(&acm->write_lock, flags);
173 static int acm_write_start(struct acm *acm)
180 spin_lock_irqsave(&acm->write_lock, flags);
182 spin_unlock_irqrestore(&acm->write_lock, flags);
186 if (!acm->write_ready) {
187 spin_unlock_irqrestore(&acm->write_lock, flags);
188 return 0; /* A white lie */
191 wbn = acm->write_current;
192 if (!acm_wb_is_used(acm, wbn)) {
193 spin_unlock_irqrestore(&acm->write_lock, flags);
198 acm->write_ready = 0;
199 spin_unlock_irqrestore(&acm->write_lock, flags);
201 acm->writeurb->transfer_buffer = wb->buf;
202 acm->writeurb->transfer_dma = wb->dmah;
203 acm->writeurb->transfer_buffer_length = wb->len;
204 acm->writeurb->dev = acm->dev;
206 if ((rc = usb_submit_urb(acm->writeurb, GFP_ATOMIC)) < 0) {
207 dbg("usb_submit_urb(write bulk) failed: %d", rc);
214 * Interrupt handlers for various ACM device responses
217 /* control interface reports status changes with "interrupt" transfers */
218 static void acm_ctrl_irq(struct urb *urb, struct pt_regs *regs)
220 struct acm *acm = urb->context;
221 struct usb_cdc_notification *dr = urb->transfer_buffer;
226 switch (urb->status) {
233 /* this urb is terminated, clean up */
234 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
237 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
244 data = (unsigned char *)(dr + 1);
245 switch (dr->bNotificationType) {
247 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
249 dbg("%s network", dr->wValue ? "connected to" : "disconnected from");
252 case USB_CDC_NOTIFY_SERIAL_STATE:
254 newctrl = le16_to_cpu(get_unaligned((__le16 *) data));
256 if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
257 dbg("calling hangup");
258 tty_hangup(acm->tty);
261 acm->ctrlin = newctrl;
263 dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c",
264 acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
265 acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
266 acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-', acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
267 acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
272 dbg("unknown notification %d received: index %d len %d data0 %d data1 %d",
273 dr->bNotificationType, dr->wIndex,
274 dr->wLength, data[0], data[1]);
278 status = usb_submit_urb (urb, GFP_ATOMIC);
280 err ("%s - usb_submit_urb failed with result %d",
281 __FUNCTION__, status);
284 /* data interface returns incoming bytes, or we got unthrottled */
285 static void acm_read_bulk(struct urb *urb, struct pt_regs *regs)
287 struct acm *acm = urb->context;
288 dbg("Entering acm_read_bulk with status %d\n", urb->status);
294 dev_dbg(&acm->data->dev, "bulk rx status %d\n", urb->status);
296 /* calling tty_flip_buffer_push() in_irq() isn't allowed */
297 tasklet_schedule(&acm->bh);
300 static void acm_rx_tasklet(unsigned long _acm)
302 struct acm *acm = (void *)_acm;
303 struct urb *urb = acm->readurb;
304 struct tty_struct *tty = acm->tty;
305 unsigned char *data = urb->transfer_buffer;
307 dbg("Entering acm_rx_tasklet");
309 if (urb->actual_length > 0 && !acm->throttle) {
310 for (i = 0; i < urb->actual_length && !acm->throttle; i++) {
311 /* if we insert more than TTY_FLIPBUF_SIZE characters,
313 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
314 tty_flip_buffer_push(tty);
316 tty_insert_flip_char(tty, data[i], 0);
318 dbg("Handed %d bytes to tty layer", i+1);
319 tty_flip_buffer_push(tty);
322 spin_lock(&acm->throttle_lock);
324 dbg("Throtteling noticed");
325 memmove(data, data + i, urb->actual_length - i);
326 urb->actual_length -= i;
327 acm->resubmit_to_unthrottle = 1;
328 spin_unlock(&acm->throttle_lock);
331 spin_unlock(&acm->throttle_lock);
333 urb->actual_length = 0;
336 i = usb_submit_urb(urb, GFP_ATOMIC);
338 dev_dbg(&acm->data->dev, "bulk rx resubmit %d\n", i);
341 /* data interface wrote those outgoing bytes */
342 static void acm_write_bulk(struct urb *urb, struct pt_regs *regs)
344 struct acm *acm = (struct acm *)urb->context;
346 dbg("Entering acm_write_bulk with status %d\n", urb->status);
349 acm_write_start(acm);
351 schedule_work(&acm->work);
354 static void acm_softint(void *private)
356 struct acm *acm = private;
357 dbg("Entering acm_softint.\n");
361 tty_wakeup(acm->tty);
368 static int acm_tty_open(struct tty_struct *tty, struct file *filp)
372 dbg("Entering acm_tty_open.\n");
376 acm = acm_table[tty->index];
377 if (!acm || !acm->dev)
382 tty->driver_data = acm;
391 acm->ctrlurb->dev = acm->dev;
392 if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
393 dbg("usb_submit_urb(ctrl irq) failed");
397 acm->readurb->dev = acm->dev;
398 if (usb_submit_urb(acm->readurb, GFP_KERNEL)) {
399 dbg("usb_submit_urb(read bulk) failed");
400 goto bail_out_and_unlink;
403 if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS))
406 /* force low_latency on so that our tty_push actually forces the data through,
407 otherwise it is scheduled, and with high data rates data can get lost. */
408 tty->low_latency = 1;
416 usb_kill_urb(acm->readurb);
418 usb_kill_urb(acm->ctrlurb);
425 static void acm_tty_close(struct tty_struct *tty, struct file *filp)
427 struct acm *acm = tty->driver_data;
429 if (!acm || !acm->used)
435 acm_set_control(acm, acm->ctrlout = 0);
436 usb_kill_urb(acm->ctrlurb);
437 usb_kill_urb(acm->writeurb);
438 usb_kill_urb(acm->readurb);
440 tty_unregister_device(acm_tty_driver, acm->minor);
441 acm_table[acm->minor] = NULL;
442 usb_free_urb(acm->ctrlurb);
443 usb_free_urb(acm->readurb);
444 usb_free_urb(acm->writeurb);
451 static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
453 struct acm *acm = tty->driver_data;
459 dbg("Entering acm_tty_write to write %d bytes,\n", count);
466 spin_lock_irqsave(&acm->write_lock, flags);
467 if ((wbn = acm_wb_alloc(acm)) < 0) {
468 spin_unlock_irqrestore(&acm->write_lock, flags);
469 acm_write_start(acm);
474 count = (count > acm->writesize) ? acm->writesize : count;
475 dbg("Get %d bytes...", count);
476 memcpy(wb->buf, buf, count);
478 spin_unlock_irqrestore(&acm->write_lock, flags);
480 if ((stat = acm_write_start(acm)) < 0)
485 static int acm_tty_write_room(struct tty_struct *tty)
487 struct acm *acm = tty->driver_data;
491 * Do not let the line discipline to know that we have a reserve,
492 * or it might get too enthusiastic.
494 return (acm->write_ready && acm_wb_is_avail(acm)) ? acm->writesize : 0;
497 static int acm_tty_chars_in_buffer(struct tty_struct *tty)
499 struct acm *acm = tty->driver_data;
503 * This is inaccurate (overcounts), but it works.
505 return (ACM_NWB - acm_wb_is_avail(acm)) * acm->writesize;
508 static void acm_tty_throttle(struct tty_struct *tty)
510 struct acm *acm = tty->driver_data;
513 spin_lock_bh(&acm->throttle_lock);
515 spin_unlock_bh(&acm->throttle_lock);
518 static void acm_tty_unthrottle(struct tty_struct *tty)
520 struct acm *acm = tty->driver_data;
523 spin_lock_bh(&acm->throttle_lock);
525 spin_unlock_bh(&acm->throttle_lock);
526 if (acm->resubmit_to_unthrottle) {
527 acm->resubmit_to_unthrottle = 0;
528 acm_read_bulk(acm->readurb, NULL);
532 static void acm_tty_break_ctl(struct tty_struct *tty, int state)
534 struct acm *acm = tty->driver_data;
537 if (acm_send_break(acm, state ? 0xffff : 0))
538 dbg("send break failed");
541 static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file)
543 struct acm *acm = tty->driver_data;
548 return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
549 (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
550 (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
551 (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
552 (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
556 static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file,
557 unsigned int set, unsigned int clear)
559 struct acm *acm = tty->driver_data;
560 unsigned int newctrl;
565 newctrl = acm->ctrlout;
566 set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
567 clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
569 newctrl = (newctrl & ~clear) | set;
571 if (acm->ctrlout == newctrl)
573 return acm_set_control(acm, acm->ctrlout = newctrl);
576 static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
578 struct acm *acm = tty->driver_data;
586 static __u32 acm_tty_speed[] = {
587 0, 50, 75, 110, 134, 150, 200, 300, 600,
588 1200, 1800, 2400, 4800, 9600, 19200, 38400,
589 57600, 115200, 230400, 460800, 500000, 576000,
590 921600, 1000000, 1152000, 1500000, 2000000,
591 2500000, 3000000, 3500000, 4000000
594 static __u8 acm_tty_size[] = {
598 static void acm_tty_set_termios(struct tty_struct *tty, struct termios *termios_old)
600 struct acm *acm = tty->driver_data;
601 struct termios *termios = tty->termios;
602 struct usb_cdc_line_coding newline;
603 int newctrl = acm->ctrlout;
608 newline.dwDTERate = cpu_to_le32p(acm_tty_speed +
609 (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0));
610 newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
611 newline.bParityType = termios->c_cflag & PARENB ?
612 (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
613 newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
615 acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
617 if (!newline.dwDTERate) {
618 newline.dwDTERate = acm->line.dwDTERate;
619 newctrl &= ~ACM_CTRL_DTR;
620 } else newctrl |= ACM_CTRL_DTR;
622 if (newctrl != acm->ctrlout)
623 acm_set_control(acm, acm->ctrlout = newctrl);
625 if (memcmp(&acm->line, &newline, sizeof newline)) {
626 memcpy(&acm->line, &newline, sizeof newline);
627 dbg("set line: %d %d %d %d", le32_to_cpu(newline.dwDTERate),
628 newline.bCharFormat, newline.bParityType,
630 acm_set_line(acm, &acm->line);
635 * USB probe and disconnect routines.
638 /* Little helper: write buffers free */
639 static void acm_write_buffers_free(struct acm *acm)
644 for (wb = &acm->wb[0], i = 0; i < ACM_NWB; i++, wb++) {
645 usb_buffer_free(acm->dev, acm->writesize, wb->buf, wb->dmah);
649 /* Little helper: write buffers allocate */
650 static int acm_write_buffers_alloc(struct acm *acm)
655 for (wb = &acm->wb[0], i = 0; i < ACM_NWB; i++, wb++) {
656 wb->buf = usb_buffer_alloc(acm->dev, acm->writesize, GFP_KERNEL,
662 usb_buffer_free(acm->dev, acm->writesize,
671 static int acm_probe (struct usb_interface *intf,
672 const struct usb_device_id *id)
674 struct usb_cdc_union_desc *union_header = NULL;
675 char *buffer = intf->altsetting->extra;
676 int buflen = intf->altsetting->extralen;
677 struct usb_interface *control_interface;
678 struct usb_interface *data_interface;
679 struct usb_endpoint_descriptor *epctrl;
680 struct usb_endpoint_descriptor *epread;
681 struct usb_endpoint_descriptor *epwrite;
682 struct usb_device *usb_dev = interface_to_usbdev(intf);
685 int ctrlsize,readsize;
687 u8 ac_management_function = 0;
688 u8 call_management_function = 0;
689 int call_interface_num = -1;
690 int data_interface_num;
691 unsigned long quirks;
693 /* handle quirks deadly to normal probing*/
694 quirks = (unsigned long)id->driver_info;
695 if (quirks == NO_UNION_NORMAL) {
696 data_interface = usb_ifnum_to_if(usb_dev, 1);
697 control_interface = usb_ifnum_to_if(usb_dev, 0);
698 goto skip_normal_probe;
703 err("Wierd descriptor references\n");
708 if (intf->cur_altsetting->endpoint->extralen && intf->cur_altsetting->endpoint->extra) {
709 dev_dbg(&intf->dev,"Seeking extra descriptors on endpoint\n");
710 buflen = intf->cur_altsetting->endpoint->extralen;
711 buffer = intf->cur_altsetting->endpoint->extra;
713 err("Zero length descriptor references\n");
719 if (buffer [1] != USB_DT_CS_INTERFACE) {
720 err("skipping garbage\n");
724 switch (buffer [2]) {
725 case USB_CDC_UNION_TYPE: /* we've found it */
727 err("More than one union descriptor, skipping ...");
730 union_header = (struct usb_cdc_union_desc *)
733 case USB_CDC_COUNTRY_TYPE: /* maybe somehow export */
734 break; /* for now we ignore it */
735 case USB_CDC_HEADER_TYPE: /* maybe check version */
736 break; /* for now we ignore it */
737 case USB_CDC_ACM_TYPE:
738 ac_management_function = buffer[3];
740 case USB_CDC_CALL_MANAGEMENT_TYPE:
741 call_management_function = buffer[3];
742 call_interface_num = buffer[4];
743 if ((call_management_function & 3) != 3)
744 err("This device cannot do calls on its own. It is no modem.");
748 err("Ignoring extra header, type %d, length %d", buffer[2], buffer[0]);
757 if (call_interface_num > 0) {
758 dev_dbg(&intf->dev,"No union descriptor, using call management descriptor\n");
759 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
760 control_interface = intf;
762 dev_dbg(&intf->dev,"No union descriptor, giving up\n");
766 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
767 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
768 if (!control_interface || !data_interface) {
769 dev_dbg(&intf->dev,"no interfaces\n");
774 if (data_interface_num != call_interface_num)
775 dev_dbg(&intf->dev,"Seperate call control interface. That is not fully supported.\n");
779 /*workaround for switched interfaces */
780 if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) {
781 if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) {
782 struct usb_interface *t;
783 dev_dbg(&intf->dev,"Your device has switched interfaces.\n");
785 t = control_interface;
786 control_interface = data_interface;
793 if (usb_interface_claimed(data_interface)) { /* valid in this context */
794 dev_dbg(&intf->dev,"The data interface isn't available\n");
799 if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
802 epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
803 epread = &data_interface->cur_altsetting->endpoint[0].desc;
804 epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
807 /* workaround for switched endpoints */
808 if ((epread->bEndpointAddress & USB_DIR_IN) != USB_DIR_IN) {
809 /* descriptors are swapped */
810 struct usb_endpoint_descriptor *t;
811 dev_dbg(&intf->dev,"The data interface has switched endpoints\n");
817 dbg("interfaces are valid");
818 for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
820 if (minor == ACM_TTY_MINORS) {
821 err("no more free acm devices");
825 if (!(acm = kmalloc(sizeof(struct acm), GFP_KERNEL))) {
826 dev_dbg(&intf->dev, "out of memory (acm kmalloc)\n");
829 memset(acm, 0, sizeof(struct acm));
831 ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize);
832 readsize = le16_to_cpu(epread->wMaxPacketSize);
833 acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize);
834 acm->control = control_interface;
835 acm->data = data_interface;
838 acm->ctrl_caps = ac_management_function;
839 acm->ctrlsize = ctrlsize;
840 acm->readsize = readsize;
841 acm->bh.func = acm_rx_tasklet;
842 acm->bh.data = (unsigned long) acm;
843 INIT_WORK(&acm->work, acm_softint, acm);
844 spin_lock_init(&acm->throttle_lock);
845 spin_lock_init(&acm->write_lock);
846 acm->write_ready = 1;
848 buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
850 dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)\n");
853 acm->ctrl_buffer = buf;
855 buf = usb_buffer_alloc(usb_dev, readsize, GFP_KERNEL, &acm->read_dma);
857 dev_dbg(&intf->dev, "out of memory (read buffer alloc)\n");
860 acm->read_buffer = buf;
862 if (acm_write_buffers_alloc(acm) < 0) {
863 dev_dbg(&intf->dev, "out of memory (write buffer alloc)\n");
867 acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
869 dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
872 acm->readurb = usb_alloc_urb(0, GFP_KERNEL);
874 dev_dbg(&intf->dev, "out of memory (readurb kmalloc)\n");
877 acm->writeurb = usb_alloc_urb(0, GFP_KERNEL);
878 if (!acm->writeurb) {
879 dev_dbg(&intf->dev, "out of memory (writeurb kmalloc)\n");
883 usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
884 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval);
885 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
886 acm->ctrlurb->transfer_dma = acm->ctrl_dma;
888 usb_fill_bulk_urb(acm->readurb, usb_dev, usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress),
889 acm->read_buffer, readsize, acm_read_bulk, acm);
890 acm->readurb->transfer_flags |= URB_NO_FSBR | URB_NO_TRANSFER_DMA_MAP;
891 acm->readurb->transfer_dma = acm->read_dma;
893 usb_fill_bulk_urb(acm->writeurb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
894 NULL, acm->writesize, acm_write_bulk, acm);
895 acm->writeurb->transfer_flags |= URB_NO_FSBR | URB_NO_TRANSFER_DMA_MAP;
896 /* acm->writeurb->transfer_dma = 0; */
898 dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
900 acm_set_control(acm, acm->ctrlout);
902 acm->line.dwDTERate = cpu_to_le32(9600);
903 acm->line.bDataBits = 8;
904 acm_set_line(acm, &acm->line);
906 usb_driver_claim_interface(&acm_driver, data_interface, acm);
908 tty_register_device(acm_tty_driver, minor, &intf->dev);
910 acm_table[minor] = acm;
911 usb_set_intfdata (intf, acm);
915 usb_free_urb(acm->readurb);
917 usb_free_urb(acm->ctrlurb);
919 acm_write_buffers_free(acm);
921 usb_buffer_free(usb_dev, readsize, acm->read_buffer, acm->read_dma);
923 usb_buffer_free(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
930 static void acm_disconnect(struct usb_interface *intf)
932 struct acm *acm = usb_get_intfdata (intf);
933 struct usb_device *usb_dev = interface_to_usbdev(intf);
935 if (!acm || !acm->dev) {
936 dbg("disconnect on nonexisting interface");
942 usb_set_intfdata (intf, NULL);
944 usb_kill_urb(acm->ctrlurb);
945 usb_kill_urb(acm->readurb);
946 usb_kill_urb(acm->writeurb);
948 flush_scheduled_work(); /* wait for acm_softint */
950 acm_write_buffers_free(acm);
951 usb_buffer_free(usb_dev, acm->readsize, acm->read_buffer, acm->read_dma);
952 usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
954 usb_driver_release_interface(&acm_driver, acm->data);
957 tty_unregister_device(acm_tty_driver, acm->minor);
958 acm_table[acm->minor] = NULL;
959 usb_free_urb(acm->ctrlurb);
960 usb_free_urb(acm->readurb);
961 usb_free_urb(acm->writeurb);
970 tty_hangup(acm->tty);
974 * USB driver structure.
977 static struct usb_device_id acm_ids[] = {
978 /* quirky and broken devices */
979 { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
980 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
982 /* control interfaces with various AT-command sets */
983 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
984 USB_CDC_ACM_PROTO_AT_V25TER) },
985 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
986 USB_CDC_ACM_PROTO_AT_PCCA101) },
987 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
988 USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
989 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
990 USB_CDC_ACM_PROTO_AT_GSM) },
991 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
992 USB_CDC_ACM_PROTO_AT_3G ) },
993 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
994 USB_CDC_ACM_PROTO_AT_CDMA) },
996 /* NOTE: COMM/ACM/0xff is likely MSFT RNDIS ... NOT a modem!! */
1000 MODULE_DEVICE_TABLE (usb, acm_ids);
1002 static struct usb_driver acm_driver = {
1003 .owner = THIS_MODULE,
1006 .disconnect = acm_disconnect,
1007 .id_table = acm_ids,
1011 * TTY driver structures.
1014 static struct tty_operations acm_ops = {
1015 .open = acm_tty_open,
1016 .close = acm_tty_close,
1017 .write = acm_tty_write,
1018 .write_room = acm_tty_write_room,
1019 .ioctl = acm_tty_ioctl,
1020 .throttle = acm_tty_throttle,
1021 .unthrottle = acm_tty_unthrottle,
1022 .chars_in_buffer = acm_tty_chars_in_buffer,
1023 .break_ctl = acm_tty_break_ctl,
1024 .set_termios = acm_tty_set_termios,
1025 .tiocmget = acm_tty_tiocmget,
1026 .tiocmset = acm_tty_tiocmset,
1033 static int __init acm_init(void)
1036 acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
1037 if (!acm_tty_driver)
1039 acm_tty_driver->owner = THIS_MODULE,
1040 acm_tty_driver->driver_name = "acm",
1041 acm_tty_driver->name = "ttyACM",
1042 acm_tty_driver->devfs_name = "usb/acm/",
1043 acm_tty_driver->major = ACM_TTY_MAJOR,
1044 acm_tty_driver->minor_start = 0,
1045 acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
1046 acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
1047 acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
1048 acm_tty_driver->init_termios = tty_std_termios;
1049 acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1050 tty_set_operations(acm_tty_driver, &acm_ops);
1052 retval = tty_register_driver(acm_tty_driver);
1054 put_tty_driver(acm_tty_driver);
1058 retval = usb_register(&acm_driver);
1060 tty_unregister_driver(acm_tty_driver);
1061 put_tty_driver(acm_tty_driver);
1065 info(DRIVER_VERSION ":" DRIVER_DESC);
1070 static void __exit acm_exit(void)
1072 usb_deregister(&acm_driver);
1073 tty_unregister_driver(acm_tty_driver);
1074 put_tty_driver(acm_tty_driver);
1077 module_init(acm_init);
1078 module_exit(acm_exit);
1080 MODULE_AUTHOR( DRIVER_AUTHOR );
1081 MODULE_DESCRIPTION( DRIVER_DESC );
1082 MODULE_LICENSE("GPL");