MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / usb / class / cdc-acm.c
bloba929a521242b8aefd299d43091dda5cbadb9e693
1 /*
2 * cdc-acm.c
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
12 * Sponsored by SuSE
14 * ChangeLog:
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
50 #undef DEBUG
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 <asm/byteorder.h>
64 #include <asm/unaligned.h>
66 #include "cdc-acm.h"
69 * Version Information
71 #define DRIVER_VERSION "v0.23"
72 #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik"
73 #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
75 static struct usb_driver acm_driver;
76 static struct tty_driver *acm_tty_driver;
77 static struct acm *acm_table[ACM_TTY_MINORS];
79 static DECLARE_MUTEX(open_sem);
81 #define ACM_READY(acm) (acm && acm->dev && acm->used)
84 * Functions for ACM control messages.
87 static int acm_ctrl_msg(struct acm *acm, int request, int value, void *buf, int len)
89 int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
90 request, USB_RT_ACM, value,
91 acm->control->altsetting[0].desc.bInterfaceNumber,
92 buf, len, HZ * 5);
93 dbg("acm_control_msg: rq: 0x%02x val: %#x len: %#x result: %d", request, value, len, retval);
94 return retval < 0 ? retval : 0;
97 /* devices aren't required to support these requests.
98 * the cdc acm descriptor tells whether they do...
100 #define acm_set_control(acm, control) acm_ctrl_msg(acm, ACM_REQ_SET_CONTROL, control, NULL, 0)
101 #define acm_set_line(acm, line) acm_ctrl_msg(acm, ACM_REQ_SET_LINE, 0, line, sizeof(struct acm_line))
102 #define acm_send_break(acm, ms) acm_ctrl_msg(acm, ACM_REQ_SEND_BREAK, ms, NULL, 0)
105 * Interrupt handlers for various ACM device responses
108 /* control interface reports status changes with "interrupt" transfers */
109 static void acm_ctrl_irq(struct urb *urb, struct pt_regs *regs)
111 struct acm *acm = urb->context;
112 struct usb_ctrlrequest *dr = urb->transfer_buffer;
113 unsigned char *data;
114 int newctrl;
115 int status;
117 switch (urb->status) {
118 case 0:
119 /* success */
120 break;
121 case -ECONNRESET:
122 case -ENOENT:
123 case -ESHUTDOWN:
124 /* this urb is terminated, clean up */
125 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
126 return;
127 default:
128 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
129 goto exit;
132 if (!ACM_READY(acm))
133 goto exit;
135 data = (unsigned char *)(dr + 1);
136 switch (dr->bRequest) {
138 case ACM_IRQ_NETWORK:
140 dbg("%s network", dr->wValue ? "connected to" : "disconnected from");
141 break;
143 case ACM_IRQ_LINE_STATE:
145 newctrl = le16_to_cpu(get_unaligned((__le16 *) data));
147 if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
148 dbg("calling hangup");
149 tty_hangup(acm->tty);
152 acm->ctrlin = newctrl;
154 dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c",
155 acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
156 acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
157 acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-', acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
158 acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
160 break;
162 default:
163 dbg("unknown control event received: request %d index %d len %d data0 %d data1 %d",
164 dr->bRequest, dr->wIndex, dr->wLength, data[0], data[1]);
165 break;
167 exit:
168 status = usb_submit_urb (urb, GFP_ATOMIC);
169 if (status)
170 err ("%s - usb_submit_urb failed with result %d",
171 __FUNCTION__, status);
174 /* data interface returns incoming bytes, or we got unthrottled */
175 static void acm_read_bulk(struct urb *urb, struct pt_regs *regs)
177 struct acm *acm = urb->context;
178 dbg("Entering acm_read_bulk with status %d\n", urb->status);
180 if (!ACM_READY(acm))
181 return;
183 if (urb->status)
184 dev_dbg(&acm->data->dev, "bulk rx status %d\n", urb->status);
186 /* calling tty_flip_buffer_push() in_irq() isn't allowed */
187 tasklet_schedule(&acm->bh);
190 static void acm_rx_tasklet(unsigned long _acm)
192 struct acm *acm = (void *)_acm;
193 struct urb *urb = acm->readurb;
194 struct tty_struct *tty = acm->tty;
195 unsigned char *data = urb->transfer_buffer;
196 int i = 0;
197 dbg("Entering acm_rx_tasklet");
199 if (urb->actual_length > 0 && !acm->throttle) {
200 for (i = 0; i < urb->actual_length && !acm->throttle; i++) {
201 /* if we insert more than TTY_FLIPBUF_SIZE characters,
202 * we drop them. */
203 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
204 tty_flip_buffer_push(tty);
206 tty_insert_flip_char(tty, data[i], 0);
208 dbg("Handed %d bytes to tty layer", i+1);
209 tty_flip_buffer_push(tty);
212 spin_lock(&acm->throttle_lock);
213 if (acm->throttle) {
214 dbg("Throtteling noticed");
215 memmove(data, data + i, urb->actual_length - i);
216 urb->actual_length -= i;
217 acm->resubmit_to_unthrottle = 1;
218 spin_unlock(&acm->throttle_lock);
219 return;
221 spin_unlock(&acm->throttle_lock);
223 urb->actual_length = 0;
224 urb->dev = acm->dev;
226 i = usb_submit_urb(urb, GFP_ATOMIC);
227 if (i)
228 dev_dbg(&acm->data->dev, "bulk rx resubmit %d\n", i);
231 /* data interface wrote those outgoing bytes */
232 static void acm_write_bulk(struct urb *urb, struct pt_regs *regs)
234 struct acm *acm = (struct acm *)urb->context;
235 dbg("Entering acm_write_bulk with status %d\n", urb->status);
237 if (!ACM_READY(acm))
238 goto out;
240 if (urb->status)
241 dbg("nonzero write bulk status received: %d", urb->status);
243 schedule_work(&acm->work);
244 out:
245 acm->ready_for_write = 1;
248 static void acm_softint(void *private)
250 struct acm *acm = private;
251 dbg("Entering acm_softint.\n");
253 if (!ACM_READY(acm))
254 return;
255 tty_wakeup(acm->tty);
259 * TTY handlers
262 static int acm_tty_open(struct tty_struct *tty, struct file *filp)
264 struct acm *acm = acm_table[tty->index];
265 dbg("Entering acm_tty_open.\n");
267 if (!acm || !acm->dev)
268 return -EINVAL;
270 tty->driver_data = acm;
271 acm->tty = tty;
273 down(&open_sem);
275 if (acm->used) {
276 goto done;
279 acm->ctrlurb->dev = acm->dev;
280 if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
281 dbg("usb_submit_urb(ctrl irq) failed");
282 goto bail_out;
285 acm->readurb->dev = acm->dev;
286 if (usb_submit_urb(acm->readurb, GFP_KERNEL)) {
287 dbg("usb_submit_urb(read bulk) failed");
288 goto bail_out_and_unlink;
291 if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS))
292 goto full_bailout;
294 /* force low_latency on so that our tty_push actually forces the data through,
295 otherwise it is scheduled, and with high data rates data can get lost. */
296 tty->low_latency = 1;
298 done:
299 acm->used++;
300 up(&open_sem);
301 return 0;
303 full_bailout:
304 usb_unlink_urb(acm->readurb);
305 bail_out_and_unlink:
306 usb_unlink_urb(acm->ctrlurb);
307 bail_out:
308 up(&open_sem);
309 return -EIO;
312 static void acm_tty_close(struct tty_struct *tty, struct file *filp)
314 struct acm *acm = tty->driver_data;
316 if (!acm || !acm->used)
317 return;
319 down(&open_sem);
320 if (!--acm->used) {
321 if (acm->dev) {
322 acm_set_control(acm, acm->ctrlout = 0);
323 usb_unlink_urb(acm->ctrlurb);
324 usb_unlink_urb(acm->writeurb);
325 usb_unlink_urb(acm->readurb);
326 } else {
327 tty_unregister_device(acm_tty_driver, acm->minor);
328 acm_table[acm->minor] = NULL;
329 usb_free_urb(acm->ctrlurb);
330 usb_free_urb(acm->readurb);
331 usb_free_urb(acm->writeurb);
332 kfree(acm);
335 up(&open_sem);
338 static int acm_tty_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count)
340 struct acm *acm = tty->driver_data;
341 int stat;
342 dbg("Entering acm_tty_write to write %d bytes from %s space,\n", count, from_user ? "user" : "kernel");
344 if (!ACM_READY(acm))
345 return -EINVAL;
346 if (!acm->ready_for_write)
347 return 0;
348 if (!count)
349 return 0;
351 count = (count > acm->writesize) ? acm->writesize : count;
353 dbg("Get %d bytes from %s space...", count, from_user ? "user" : "kernel");
354 if (from_user) {
355 if (copy_from_user(acm->write_buffer, (void __user *)buf, count))
356 return -EFAULT;
357 } else
358 memcpy(acm->write_buffer, buf, count);
359 dbg(" Successfully copied.\n");
361 acm->writeurb->transfer_buffer_length = count;
362 acm->writeurb->dev = acm->dev;
364 acm->ready_for_write = 0;
365 stat = usb_submit_urb(acm->writeurb, from_user ? GFP_KERNEL : GFP_ATOMIC);
366 if (stat < 0) {
367 dbg("usb_submit_urb(write bulk) failed");
368 acm->ready_for_write = 1;
369 return stat;
372 return count;
375 static int acm_tty_write_room(struct tty_struct *tty)
377 struct acm *acm = tty->driver_data;
378 if (!ACM_READY(acm))
379 return -EINVAL;
380 return !acm->ready_for_write ? 0 : acm->writesize;
383 static int acm_tty_chars_in_buffer(struct tty_struct *tty)
385 struct acm *acm = tty->driver_data;
386 if (!ACM_READY(acm))
387 return -EINVAL;
388 return !acm->ready_for_write ? acm->writeurb->transfer_buffer_length : 0;
391 static void acm_tty_throttle(struct tty_struct *tty)
393 struct acm *acm = tty->driver_data;
394 if (!ACM_READY(acm))
395 return;
396 spin_lock_bh(&acm->throttle_lock);
397 acm->throttle = 1;
398 spin_unlock_bh(&acm->throttle_lock);
401 static void acm_tty_unthrottle(struct tty_struct *tty)
403 struct acm *acm = tty->driver_data;
404 if (!ACM_READY(acm))
405 return;
406 spin_lock_bh(&acm->throttle_lock);
407 acm->throttle = 0;
408 spin_unlock_bh(&acm->throttle_lock);
409 if (acm->resubmit_to_unthrottle) {
410 acm->resubmit_to_unthrottle = 0;
411 acm_read_bulk(acm->readurb, NULL);
415 static void acm_tty_break_ctl(struct tty_struct *tty, int state)
417 struct acm *acm = tty->driver_data;
418 if (!ACM_READY(acm))
419 return;
420 if (acm_send_break(acm, state ? 0xffff : 0))
421 dbg("send break failed");
424 static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file)
426 struct acm *acm = tty->driver_data;
428 if (!ACM_READY(acm))
429 return -EINVAL;
431 return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
432 (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
433 (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
434 (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
435 (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
436 TIOCM_CTS;
439 static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file,
440 unsigned int set, unsigned int clear)
442 struct acm *acm = tty->driver_data;
443 unsigned int newctrl;
445 if (!ACM_READY(acm))
446 return -EINVAL;
448 newctrl = acm->ctrlout;
449 set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
450 clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
452 newctrl = (newctrl & ~clear) | set;
454 if (acm->ctrlout == newctrl)
455 return 0;
456 return acm_set_control(acm, acm->ctrlout = newctrl);
459 static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
461 struct acm *acm = tty->driver_data;
463 if (!ACM_READY(acm))
464 return -EINVAL;
466 return -ENOIOCTLCMD;
469 static __u32 acm_tty_speed[] = {
470 0, 50, 75, 110, 134, 150, 200, 300, 600,
471 1200, 1800, 2400, 4800, 9600, 19200, 38400,
472 57600, 115200, 230400, 460800, 500000, 576000,
473 921600, 1000000, 1152000, 1500000, 2000000,
474 2500000, 3000000, 3500000, 4000000
477 static __u8 acm_tty_size[] = {
478 5, 6, 7, 8
481 static void acm_tty_set_termios(struct tty_struct *tty, struct termios *termios_old)
483 struct acm *acm = tty->driver_data;
484 struct termios *termios = tty->termios;
485 struct acm_line newline;
486 int newctrl = acm->ctrlout;
488 if (!ACM_READY(acm))
489 return;
491 newline.speed = cpu_to_le32p(acm_tty_speed +
492 (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0));
493 newline.stopbits = termios->c_cflag & CSTOPB ? 2 : 0;
494 newline.parity = termios->c_cflag & PARENB ?
495 (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
496 newline.databits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
498 acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
500 if (!newline.speed) {
501 newline.speed = acm->line.speed;
502 newctrl &= ~ACM_CTRL_DTR;
503 } else newctrl |= ACM_CTRL_DTR;
505 if (newctrl != acm->ctrlout)
506 acm_set_control(acm, acm->ctrlout = newctrl);
508 if (memcmp(&acm->line, &newline, sizeof(struct acm_line))) {
509 memcpy(&acm->line, &newline, sizeof(struct acm_line));
510 dbg("set line: %d %d %d %d", newline.speed, newline.stopbits, newline.parity, newline.databits);
511 acm_set_line(acm, &acm->line);
516 * USB probe and disconnect routines.
519 static int acm_probe (struct usb_interface *intf,
520 const struct usb_device_id *id)
522 struct union_desc *union_header = NULL;
523 char *buffer = intf->altsetting->extra;
524 int buflen = intf->altsetting->extralen;
525 struct usb_interface *control_interface;
526 struct usb_interface *data_interface;
527 struct usb_endpoint_descriptor *epctrl;
528 struct usb_endpoint_descriptor *epread;
529 struct usb_endpoint_descriptor *epwrite;
530 struct usb_device *usb_dev = interface_to_usbdev(intf);
531 struct acm *acm;
532 int minor;
533 int ctrlsize,readsize;
534 u8 *buf;
535 u8 ac_management_function = 0;
536 u8 call_management_function = 0;
537 int call_interface_num = -1;
538 int data_interface_num;
540 if (!buffer) {
541 err("Wierd descriptor references");
542 return -EINVAL;
545 while (buflen > 0) {
546 if (buffer [1] != USB_DT_CS_INTERFACE) {
547 err("skipping garbage");
548 goto next_desc;
551 switch (buffer [2]) {
552 case CDC_UNION_TYPE: /* we've found it */
553 if (union_header) {
554 err("More than one union descriptor, skipping ...");
555 goto next_desc;
557 union_header = (struct union_desc *)buffer;
558 break;
559 case CDC_COUNTRY_TYPE: /* maybe somehow export */
560 break; /* for now we ignore it */
561 case CDC_AC_MANAGEMENT_TYPE:
562 ac_management_function = buffer[3];
563 break;
564 case CDC_CALL_MANAGEMENT_TYPE:
565 call_management_function = buffer[3];
566 call_interface_num = buffer[4];
567 if ((call_management_function & 3) != 3)
568 err("This device cannot do calls on its own. It is no modem.");
569 break;
571 default:
572 err("Ignoring extra header");
573 break;
575 next_desc:
576 buflen -= buffer[0];
577 buffer += buffer[0];
580 if (!union_header) {
581 if (call_interface_num > 0) {
582 dev_dbg(&intf->dev,"No union descriptor, using call management descriptor\n");
583 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
584 control_interface = intf;
585 } else {
586 dev_dbg(&intf->dev,"No union descriptor, giving up\n");
587 return -ENODEV;
589 } else {
590 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
591 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
592 if (!control_interface || !data_interface) {
593 dev_dbg(&intf->dev,"no interfaces\n");
594 return -ENODEV;
598 if (data_interface_num != call_interface_num)
599 dev_dbg(&intf->dev,"Seperate call control interface. That is not fully supported.");
601 if (usb_interface_claimed(data_interface)) { /* valid in this context */
602 dev_dbg(&intf->dev,"The data interface isn't available\n");
603 return -EBUSY;
606 /*workaround for switched interfaces */
607 if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) {
608 if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) {
609 struct usb_interface *t;
610 dev_dbg(&intf->dev,"Your device has switched interfaces.\n");
612 t = control_interface;
613 control_interface = data_interface;
614 data_interface = t;
615 } else {
616 return -EINVAL;
619 if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
620 return -EINVAL;
622 epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
623 epread = &data_interface->cur_altsetting->endpoint[0].desc;
624 epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
627 /* workaround for switched endpoints */
628 if ((epread->bEndpointAddress & USB_DIR_IN) != USB_DIR_IN) {
629 /* descriptors are swapped */
630 struct usb_endpoint_descriptor *t;
631 dev_dbg(&intf->dev,"The data interface has switched endpoints\n");
633 t = epread;
634 epread = epwrite;
635 epwrite = t;
637 dbg("interfaces are valid");
638 for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
640 if (acm_table[minor]) {
641 err("no more free acm devices");
642 return -ENODEV;
645 if (!(acm = kmalloc(sizeof(struct acm), GFP_KERNEL))) {
646 dev_dbg(&intf->dev, "out of memory (acm kmalloc)\n");
647 goto alloc_fail;
649 memset(acm, 0, sizeof(struct acm));
651 ctrlsize = epctrl->wMaxPacketSize;
652 readsize = epread->wMaxPacketSize;
653 acm->writesize = epwrite->wMaxPacketSize;
654 acm->control = control_interface;
655 acm->data = data_interface;
656 acm->minor = minor;
657 acm->dev = usb_dev;
658 acm->ctrl_caps = ac_management_function;
659 acm->ctrlsize = ctrlsize;
660 acm->readsize = readsize;
661 acm->bh.func = acm_rx_tasklet;
662 acm->bh.data = (unsigned long) acm;
663 INIT_WORK(&acm->work, acm_softint, acm);
664 spin_lock_init(&acm->throttle_lock);
665 acm->ready_for_write = 1;
667 buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
668 if (!buf) {
669 dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)\n");
670 goto alloc_fail2;
672 acm->ctrl_buffer = buf;
674 buf = usb_buffer_alloc(usb_dev, readsize, GFP_KERNEL, &acm->read_dma);
675 if (!buf) {
676 dev_dbg(&intf->dev, "out of memory (read buffer alloc)\n");
677 goto alloc_fail3;
679 acm->read_buffer = buf;
681 buf = usb_buffer_alloc(usb_dev, acm->writesize, GFP_KERNEL, &acm->write_dma);
682 if (!buf) {
683 dev_dbg(&intf->dev, "out of memory (write buffer alloc)\n");
684 goto alloc_fail4;
686 acm->write_buffer = buf;
688 acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
689 if (!acm->ctrlurb) {
690 dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
691 goto alloc_fail5;
693 acm->readurb = usb_alloc_urb(0, GFP_KERNEL);
694 if (!acm->readurb) {
695 dev_dbg(&intf->dev, "out of memory (readurb kmalloc)\n");
696 goto alloc_fail6;
698 acm->writeurb = usb_alloc_urb(0, GFP_KERNEL);
699 if (!acm->writeurb) {
700 dev_dbg(&intf->dev, "out of memory (writeurb kmalloc)\n");
701 goto alloc_fail7;
704 usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
705 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval);
706 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
707 acm->ctrlurb->transfer_dma = acm->ctrl_dma;
709 usb_fill_bulk_urb(acm->readurb, usb_dev, usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress),
710 acm->read_buffer, readsize, acm_read_bulk, acm);
711 acm->readurb->transfer_flags |= URB_NO_FSBR | URB_NO_TRANSFER_DMA_MAP;
712 acm->readurb->transfer_dma = acm->read_dma;
714 usb_fill_bulk_urb(acm->writeurb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
715 acm->write_buffer, acm->writesize, acm_write_bulk, acm);
716 acm->writeurb->transfer_flags |= URB_NO_FSBR | URB_NO_TRANSFER_DMA_MAP;
717 acm->writeurb->transfer_dma = acm->write_dma;
719 dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
721 acm_set_control(acm, acm->ctrlout);
723 acm->line.speed = cpu_to_le32(9600);
724 acm->line.databits = 8;
725 acm_set_line(acm, &acm->line);
727 usb_driver_claim_interface(&acm_driver, data_interface, acm);
729 tty_register_device(acm_tty_driver, minor, &intf->dev);
731 acm_table[minor] = acm;
732 usb_set_intfdata (intf, acm);
733 return 0;
735 alloc_fail7:
736 usb_free_urb(acm->readurb);
737 alloc_fail6:
738 usb_free_urb(acm->ctrlurb);
739 alloc_fail5:
740 usb_buffer_free(usb_dev, acm->writesize, acm->write_buffer, acm->write_dma);
741 alloc_fail4:
742 usb_buffer_free(usb_dev, readsize, acm->read_buffer, acm->read_dma);
743 alloc_fail3:
744 usb_buffer_free(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
745 alloc_fail2:
746 kfree(acm);
747 alloc_fail:
748 return -ENOMEM;
751 static void acm_disconnect(struct usb_interface *intf)
753 struct acm *acm = usb_get_intfdata (intf);
754 struct usb_device *usb_dev = interface_to_usbdev(intf);
756 if (!acm || !acm->dev) {
757 dbg("disconnect on nonexisting interface");
758 return;
761 down(&open_sem);
762 acm->dev = NULL;
763 usb_set_intfdata (intf, NULL);
765 usb_unlink_urb(acm->ctrlurb);
766 usb_unlink_urb(acm->readurb);
767 usb_unlink_urb(acm->writeurb);
769 flush_scheduled_work(); /* wait for acm_softint */
771 usb_buffer_free(usb_dev, acm->writesize, acm->write_buffer, acm->write_dma);
772 usb_buffer_free(usb_dev, acm->readsize, acm->read_buffer, acm->read_dma);
773 usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
775 usb_driver_release_interface(&acm_driver, acm->data);
777 if (!acm->used) {
778 tty_unregister_device(acm_tty_driver, acm->minor);
779 acm_table[acm->minor] = NULL;
780 usb_free_urb(acm->ctrlurb);
781 usb_free_urb(acm->readurb);
782 usb_free_urb(acm->writeurb);
783 kfree(acm);
784 up(&open_sem);
785 return;
788 up(&open_sem);
790 if (acm->tty)
791 tty_hangup(acm->tty);
795 * USB driver structure.
798 static struct usb_device_id acm_ids[] = {
799 /* control interfaces with various AT-command sets */
800 { USB_INTERFACE_INFO(USB_CLASS_COMM, 2, 1) },
801 { USB_INTERFACE_INFO(USB_CLASS_COMM, 2, 2) },
802 { USB_INTERFACE_INFO(USB_CLASS_COMM, 2, 3) },
803 { USB_INTERFACE_INFO(USB_CLASS_COMM, 2, 4) },
804 { USB_INTERFACE_INFO(USB_CLASS_COMM, 2, 5) },
805 { USB_INTERFACE_INFO(USB_CLASS_COMM, 2, 6) },
807 /* NOTE: COMM/2/0xff is likely MSFT RNDIS ... NOT a modem!! */
811 MODULE_DEVICE_TABLE (usb, acm_ids);
813 static struct usb_driver acm_driver = {
814 .owner = THIS_MODULE,
815 .name = "cdc_acm",
816 .probe = acm_probe,
817 .disconnect = acm_disconnect,
818 .id_table = acm_ids,
822 * TTY driver structures.
825 static struct tty_operations acm_ops = {
826 .open = acm_tty_open,
827 .close = acm_tty_close,
828 .write = acm_tty_write,
829 .write_room = acm_tty_write_room,
830 .ioctl = acm_tty_ioctl,
831 .throttle = acm_tty_throttle,
832 .unthrottle = acm_tty_unthrottle,
833 .chars_in_buffer = acm_tty_chars_in_buffer,
834 .break_ctl = acm_tty_break_ctl,
835 .set_termios = acm_tty_set_termios,
836 .tiocmget = acm_tty_tiocmget,
837 .tiocmset = acm_tty_tiocmset,
841 * Init / exit.
844 static int __init acm_init(void)
846 int retval;
847 acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
848 if (!acm_tty_driver)
849 return -ENOMEM;
850 acm_tty_driver->owner = THIS_MODULE,
851 acm_tty_driver->driver_name = "acm",
852 acm_tty_driver->name = "ttyACM",
853 acm_tty_driver->devfs_name = "usb/acm/",
854 acm_tty_driver->major = ACM_TTY_MAJOR,
855 acm_tty_driver->minor_start = 0,
856 acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
857 acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
858 acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
859 acm_tty_driver->init_termios = tty_std_termios;
860 acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
861 tty_set_operations(acm_tty_driver, &acm_ops);
863 retval = tty_register_driver(acm_tty_driver);
864 if (retval) {
865 put_tty_driver(acm_tty_driver);
866 return retval;
869 retval = usb_register(&acm_driver);
870 if (retval) {
871 tty_unregister_driver(acm_tty_driver);
872 put_tty_driver(acm_tty_driver);
873 return retval;
876 info(DRIVER_VERSION ":" DRIVER_DESC);
878 return 0;
881 static void __exit acm_exit(void)
883 usb_deregister(&acm_driver);
884 tty_unregister_driver(acm_tty_driver);
885 put_tty_driver(acm_tty_driver);
888 module_init(acm_init);
889 module_exit(acm_exit);
891 MODULE_AUTHOR( DRIVER_AUTHOR );
892 MODULE_DESCRIPTION( DRIVER_DESC );
893 MODULE_LICENSE("GPL");