This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / drivers / usb / class / cdc-acm.c
1 /*
2  * cdc-acm.c
3  *
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  *
9  * USB Abstract Control Model driver for USB modems and ISDN adapters
10  *
11  * Sponsored by SuSE
12  *
13  * ChangeLog:
14  *      v0.9  - thorough cleaning, URBification, almost a rewrite
15  *      v0.10 - some more cleanups
16  *      v0.11 - fixed flow control, read error doesn't stop reads
17  *      v0.12 - added TIOCM ioctls, added break handling, made struct acm kmalloced
18  *      v0.13 - added termios, added hangup
19  *      v0.14 - sized down struct acm
20  *      v0.15 - fixed flow control again - characters could be lost
21  *      v0.16 - added code for modems with swapped data and control interfaces
22  *      v0.17 - added new style probing
23  *      v0.18 - fixed new style probing for devices with more configurations
24  *      v0.19 - fixed CLOCAL handling (thanks to Richard Shih-Ping Chan)
25  *      v0.20 - switched to probing on interface (rather than device) class
26  *      v0.21 - revert to probing on device for devices with multiple configs
27  *      v0.22 - probe only the control interface. if usbcore doesn't choose the
28  *              config we want, sysadmin changes bConfigurationValue in sysfs.
29  *      v0.23 - use softirq for rx processing, as needed by tty layer
30  *      v0.24 - change probe method to evaluate CDC union descriptor
31  */
32
33 /*
34  * This program is free software; you can redistribute it and/or modify
35  * it under the terms of the GNU General Public License as published by
36  * the Free Software Foundation; either version 2 of the License, or
37  * (at your option) any later version.
38  *
39  * This program is distributed in the hope that it will be useful,
40  * but WITHOUT ANY WARRANTY; without even the implied warranty of
41  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42  * GNU General Public License for more details.
43  *
44  * You should have received a copy of the GNU General Public License
45  * along with this program; if not, write to the Free Software
46  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
47  */
48
49 #undef DEBUG
50
51 #include <linux/kernel.h>
52 #include <linux/errno.h>
53 #include <linux/init.h>
54 #include <linux/slab.h>
55 #include <linux/tty.h>
56 #include <linux/tty_driver.h>
57 #include <linux/tty_flip.h>
58 #include <linux/module.h>
59 #include <linux/smp_lock.h>
60 #include <asm/uaccess.h>
61 #include <linux/usb.h>
62 #include <asm/byteorder.h>
63
64 #include "cdc-acm.h"
65
66 /*
67  * Version Information
68  */
69 #define DRIVER_VERSION "v0.23"
70 #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik"
71 #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
72
73 static struct usb_driver acm_driver;
74 static struct tty_driver *acm_tty_driver;
75 static struct acm *acm_table[ACM_TTY_MINORS];
76
77 static DECLARE_MUTEX(open_sem);
78
79 #define ACM_READY(acm)  (acm && acm->dev && acm->used)
80
81 /*
82  * Functions for ACM control messages.
83  */
84
85 static int acm_ctrl_msg(struct acm *acm, int request, int value, void *buf, int len)
86 {
87         int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
88                 request, USB_RT_ACM, value,
89                 acm->control->altsetting[0].desc.bInterfaceNumber,
90                 buf, len, HZ * 5);
91         dbg("acm_control_msg: rq: 0x%02x val: %#x len: %#x result: %d", request, value, len, retval);
92         return retval < 0 ? retval : 0;
93 }
94
95 /* devices aren't required to support these requests.
96  * the cdc acm descriptor tells whether they do...
97  */
98 #define acm_set_control(acm, control)   acm_ctrl_msg(acm, ACM_REQ_SET_CONTROL, control, NULL, 0)
99 #define acm_set_line(acm, line)         acm_ctrl_msg(acm, ACM_REQ_SET_LINE, 0, line, sizeof(struct acm_line))
100 #define acm_send_break(acm, ms)         acm_ctrl_msg(acm, ACM_REQ_SEND_BREAK, ms, NULL, 0)
101
102 /*
103  * Interrupt handlers for various ACM device responses
104  */
105
106 /* control interface reports status changes with "interrupt" transfers */
107 static void acm_ctrl_irq(struct urb *urb, struct pt_regs *regs)
108 {
109         struct acm *acm = urb->context;
110         struct usb_ctrlrequest *dr = urb->transfer_buffer;
111         unsigned char *data = (unsigned char *)(dr + 1);
112         int newctrl;
113         int status;
114
115         switch (urb->status) {
116         case 0:
117                 /* success */
118                 break;
119         case -ECONNRESET:
120         case -ENOENT:
121         case -ESHUTDOWN:
122                 /* this urb is terminated, clean up */
123                 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
124                 return;
125         default:
126                 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
127                 goto exit;
128         }
129
130         if (!ACM_READY(acm))
131                 goto exit;
132
133         switch (dr->bRequest) {
134
135                 case ACM_IRQ_NETWORK:
136
137                         dbg("%s network", dr->wValue ? "connected to" : "disconnected from");
138                         break;
139
140                 case ACM_IRQ_LINE_STATE:
141
142                         newctrl = le16_to_cpup((__u16 *) data);
143
144                         if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
145                                 dbg("calling hangup");
146                                 tty_hangup(acm->tty);
147                         }
148
149                         acm->ctrlin = newctrl;
150
151                         dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c",
152                                 acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
153                                 acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', acm->ctrlin & ACM_CTRL_RI  ? '+' : '-',
154                                 acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-',     acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
155                                 acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
156
157                         break;
158
159                 default:
160                         dbg("unknown control event received: request %d index %d len %d data0 %d data1 %d",
161                                 dr->bRequest, dr->wIndex, dr->wLength, data[0], data[1]);
162                         break;
163         }
164 exit:
165         status = usb_submit_urb (urb, GFP_ATOMIC);
166         if (status)
167                 err ("%s - usb_submit_urb failed with result %d",
168                      __FUNCTION__, status);
169 }
170
171 /* data interface returns incoming bytes, or we got unthrottled */
172 static void acm_read_bulk(struct urb *urb, struct pt_regs *regs)
173 {
174         struct acm *acm = urb->context;
175
176         if (!ACM_READY(acm))
177                 return;
178
179         if (urb->status)
180                 dev_dbg(&acm->data->dev, "bulk rx status %d\n", urb->status);
181
182         /* calling tty_flip_buffer_push() in_irq() isn't allowed */
183         tasklet_schedule(&acm->bh);
184 }
185
186 static void acm_rx_tasklet(unsigned long _acm)
187 {
188         struct acm *acm = (void *)_acm;
189         struct urb *urb = acm->readurb;
190         struct tty_struct *tty = acm->tty;
191         unsigned char *data = urb->transfer_buffer;
192         int i = 0;
193
194         if (urb->actual_length > 0 && !acm->throttle)  {
195                 for (i = 0; i < urb->actual_length && !acm->throttle; i++) {
196                         /* if we insert more than TTY_FLIPBUF_SIZE characters,
197                          * we drop them. */
198                         if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
199                                 tty_flip_buffer_push(tty);
200                         }
201                         tty_insert_flip_char(tty, data[i], 0);
202                 }
203                 tty_flip_buffer_push(tty);
204         }
205
206         if (acm->throttle) {
207                 memmove(data, data + i, urb->actual_length - i);
208                 urb->actual_length -= i;
209                 return;
210         }
211
212         urb->actual_length = 0;
213         urb->dev = acm->dev;
214
215         i = usb_submit_urb(urb, GFP_ATOMIC);
216         if (i)
217                 dev_dbg(&acm->data->dev, "bulk rx resubmit %d\n", i);
218 }
219
220 /* data interface wrote those outgoing bytes */
221 static void acm_write_bulk(struct urb *urb, struct pt_regs *regs)
222 {
223         struct acm *acm = (struct acm *)urb->context;
224
225         if (!ACM_READY(acm))
226                 goto out;
227
228         if (urb->status)
229                 dbg("nonzero write bulk status received: %d", urb->status);
230
231         schedule_work(&acm->work);
232 out:
233         acm->ready_for_write = 1;
234 }
235
236 static void acm_softint(void *private)
237 {
238         struct acm *acm = private;
239         struct tty_struct *tty = acm->tty;
240
241         if (!ACM_READY(acm))
242                 return;
243
244         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
245                 (tty->ldisc.write_wakeup)(tty);
246
247         wake_up_interruptible(&tty->write_wait);
248 }
249
250 /*
251  * TTY handlers
252  */
253
254 static int acm_tty_open(struct tty_struct *tty, struct file *filp)
255 {
256         struct acm *acm = acm_table[tty->index];
257
258         if (!acm || !acm->dev)
259                 return -EINVAL;
260
261         tty->driver_data = acm;
262         acm->tty = tty;
263
264         down(&open_sem);
265
266         if (acm->used) {
267                 goto done;
268         }
269
270         acm->ctrlurb->dev = acm->dev;
271         if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
272                 dbg("usb_submit_urb(ctrl irq) failed");
273                 goto bail_out;
274         }
275
276         acm->readurb->dev = acm->dev;
277         if (usb_submit_urb(acm->readurb, GFP_KERNEL)) {
278                 dbg("usb_submit_urb(read bulk) failed");
279                 goto bail_out_and_unlink;
280         }
281
282         acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS);
283
284         /* force low_latency on so that our tty_push actually forces the data through, 
285            otherwise it is scheduled, and with high data rates data can get lost. */
286         tty->low_latency = 1;
287
288 done:
289         acm->used++;
290         up(&open_sem);
291         return 0;
292
293 bail_out_and_unlink:
294         usb_unlink_urb(acm->ctrlurb);
295 bail_out:
296         up(&open_sem);
297         return -EIO;
298 }
299
300 static void acm_tty_close(struct tty_struct *tty, struct file *filp)
301 {
302         struct acm *acm = tty->driver_data;
303
304         if (!acm || !acm->used)
305                 return;
306
307         down(&open_sem);
308         if (!--acm->used) {
309                 if (acm->dev) {
310                         acm_set_control(acm, acm->ctrlout = 0);
311                         usb_unlink_urb(acm->ctrlurb);
312                         usb_unlink_urb(acm->writeurb);
313                         usb_unlink_urb(acm->readurb);
314                 } else {
315                         tty_unregister_device(acm_tty_driver, acm->minor);
316                         acm_table[acm->minor] = NULL;
317                         usb_free_urb(acm->ctrlurb);
318                         usb_free_urb(acm->readurb);
319                         usb_free_urb(acm->writeurb);
320                         kfree(acm);
321                 }
322         }
323         up(&open_sem);
324 }
325
326 static int acm_tty_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count)
327 {
328         struct acm *acm = tty->driver_data;
329         int stat;
330
331         if (!ACM_READY(acm))
332                 return -EINVAL;
333         if (!acm->ready_for_write)
334                 return 0;
335         if (!count)
336                 return 0;
337
338         count = (count > acm->writesize) ? acm->writesize : count;
339
340         if (from_user) {
341                 if (copy_from_user(acm->writeurb->transfer_buffer, (void __user *)buf, count))
342                         return -EFAULT;
343         } else
344                 memcpy(acm->writeurb->transfer_buffer, buf, count);
345
346         acm->writeurb->transfer_buffer_length = count;
347         acm->writeurb->dev = acm->dev;
348
349         acm->ready_for_write = 0;
350         stat = usb_submit_urb(acm->writeurb, GFP_NOIO);
351         if (stat < 0) {
352                 dbg("usb_submit_urb(write bulk) failed");
353                 acm->ready_for_write = 1;
354                 return stat;
355         }
356
357         return count;
358 }
359
360 static int acm_tty_write_room(struct tty_struct *tty)
361 {
362         struct acm *acm = tty->driver_data;
363         if (!ACM_READY(acm))
364                 return -EINVAL;
365         return !acm->ready_for_write ? 0 : acm->writesize;
366 }
367
368 static int acm_tty_chars_in_buffer(struct tty_struct *tty)
369 {
370         struct acm *acm = tty->driver_data;
371         if (!ACM_READY(acm))
372                 return -EINVAL;
373         return !acm->ready_for_write ? acm->writeurb->transfer_buffer_length : 0;
374 }
375
376 static void acm_tty_throttle(struct tty_struct *tty)
377 {
378         struct acm *acm = tty->driver_data;
379         if (!ACM_READY(acm))
380                 return;
381         acm->throttle = 1;
382 }
383
384 static void acm_tty_unthrottle(struct tty_struct *tty)
385 {
386         struct acm *acm = tty->driver_data;
387         if (!ACM_READY(acm))
388                 return;
389         acm->throttle = 0;
390         if (acm->readurb->status != -EINPROGRESS)
391                 acm_read_bulk(acm->readurb, NULL);
392 }
393
394 static void acm_tty_break_ctl(struct tty_struct *tty, int state)
395 {
396         struct acm *acm = tty->driver_data;
397         if (!ACM_READY(acm))
398                 return;
399         if (acm_send_break(acm, state ? 0xffff : 0))
400                 dbg("send break failed");
401 }
402
403 static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file)
404 {
405         struct acm *acm = tty->driver_data;
406
407         if (!ACM_READY(acm))
408                 return -EINVAL;
409
410         return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
411                (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
412                (acm->ctrlin  & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
413                (acm->ctrlin  & ACM_CTRL_RI  ? TIOCM_RI  : 0) |
414                (acm->ctrlin  & ACM_CTRL_DCD ? TIOCM_CD  : 0) |
415                TIOCM_CTS;
416 }
417
418 static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file,
419                             unsigned int set, unsigned int clear)
420 {
421         struct acm *acm = tty->driver_data;
422         unsigned int newctrl;
423
424         if (!ACM_READY(acm))
425                 return -EINVAL;
426
427         newctrl = acm->ctrlout;
428         set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
429         clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
430
431         newctrl = (newctrl & ~clear) | set;
432
433         if (acm->ctrlout == newctrl)
434                 return 0;
435         return acm_set_control(acm, acm->ctrlout = newctrl);
436 }
437
438 static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
439 {
440         struct acm *acm = tty->driver_data;
441
442         if (!ACM_READY(acm))
443                 return -EINVAL;
444
445         return -ENOIOCTLCMD;
446 }
447
448 static __u32 acm_tty_speed[] = {
449         0, 50, 75, 110, 134, 150, 200, 300, 600,
450         1200, 1800, 2400, 4800, 9600, 19200, 38400,
451         57600, 115200, 230400, 460800, 500000, 576000,
452         921600, 1000000, 1152000, 1500000, 2000000,
453         2500000, 3000000, 3500000, 4000000
454 };
455
456 static __u8 acm_tty_size[] = {
457         5, 6, 7, 8
458 };
459
460 static void acm_tty_set_termios(struct tty_struct *tty, struct termios *termios_old)
461 {
462         struct acm *acm = tty->driver_data;
463         struct termios *termios = tty->termios;
464         struct acm_line newline;
465         int newctrl = acm->ctrlout;
466
467         if (!ACM_READY(acm))
468                 return;
469
470         newline.speed = cpu_to_le32p(acm_tty_speed +
471                 (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0));
472         newline.stopbits = termios->c_cflag & CSTOPB ? 2 : 0;
473         newline.parity = termios->c_cflag & PARENB ?
474                 (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
475         newline.databits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
476
477         acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
478
479         if (!newline.speed) {
480                 newline.speed = acm->line.speed;
481                 newctrl &= ~ACM_CTRL_DTR;
482         } else  newctrl |=  ACM_CTRL_DTR;
483
484         if (newctrl != acm->ctrlout)
485                 acm_set_control(acm, acm->ctrlout = newctrl);
486
487         if (memcmp(&acm->line, &newline, sizeof(struct acm_line))) {
488                 memcpy(&acm->line, &newline, sizeof(struct acm_line));
489                 dbg("set line: %d %d %d %d", newline.speed, newline.stopbits, newline.parity, newline.databits);
490                 acm_set_line(acm, &acm->line);
491         }
492 }
493
494 /*
495  * USB probe and disconnect routines.
496  */
497
498 static int acm_probe (struct usb_interface *intf,
499                       const struct usb_device_id *id)
500 {
501         struct union_desc *union_header = NULL;
502         char *buffer = intf->altsetting->extra;
503         int buflen = intf->altsetting->extralen;
504         struct usb_interface *control_interface;
505         struct usb_interface *data_interface;
506         struct usb_endpoint_descriptor *epctrl;
507         struct usb_endpoint_descriptor *epread;
508         struct usb_endpoint_descriptor *epwrite;
509         struct usb_device *usb_dev = interface_to_usbdev(intf);
510         struct acm *acm;
511         int minor;
512         int ctrlsize,readsize;
513         char *buf;
514
515         if (!buffer) {
516                 err("Wierd descriptor references");
517                 return -EINVAL;
518         }
519
520         while (buflen > 0) {
521                 if (buffer [1] != USB_DT_CS_INTERFACE) {
522                         err("skipping garbage");
523                         goto next_desc;
524                 }
525
526                 switch (buffer [2]) {
527                         case CDC_UNION_TYPE: /* we've found it */
528                                 if (union_header) {
529                                         err("More than one union descriptor, skipping ...");
530                                         goto next_desc;
531                                 }
532                                 union_header = (struct union_desc *)buffer;
533                                 break;
534                         default:
535                                 err("Ignoring extra header");
536                                 break;
537                         }
538 next_desc:
539                 buflen -= buffer[0];
540                 buffer += buffer[0];
541         }
542
543         if (!union_header) {
544                 dev_dbg(&intf->dev,"No union descriptor, giving up\n");
545                 return -ENODEV;
546         }
547
548         control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
549         data_interface = usb_ifnum_to_if(usb_dev, union_header->bSlaveInterface0);
550         if (!control_interface || !data_interface) {
551                 dev_dbg(&intf->dev,"no interfaces\n");
552                 return -ENODEV;
553         }
554
555         if (usb_interface_claimed(data_interface)) { /* valid in this context */
556                 dev_dbg(&intf->dev,"The data interface isn't available\n");
557                 return -EBUSY;
558         }
559
560         /*workaround for switched interfaces */
561         if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) {
562                 if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) {
563                         struct usb_interface *t;
564                         dev_dbg(&intf->dev,"Your device has switched interfaces.\n");
565
566                         t = control_interface;
567                         control_interface = data_interface;
568                         data_interface = t;
569                 } else {
570                         return -EINVAL;
571                 }
572         }
573         if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
574                 return -EINVAL;
575
576         epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
577         epread = &data_interface->cur_altsetting->endpoint[0].desc;
578         epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
579
580
581         /* workaround for switched endpoints */
582         if ((epread->bEndpointAddress & USB_DIR_IN) != USB_DIR_IN) {
583                 /* descriptors are swapped */
584                 struct usb_endpoint_descriptor *t;
585                 dev_dbg(&intf->dev,"The data interface has switched endpoints\n");
586                 
587                 t = epread;
588                 epread = epwrite;
589                 epwrite = t;
590         }
591         dbg("interfaces are valid");
592         for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
593
594         if (acm_table[minor]) {
595                 err("no more free acm devices");
596                 return -ENODEV;
597         }
598
599         if (!(acm = kmalloc(sizeof(struct acm), GFP_KERNEL))) {
600                 dev_dbg(&intf->dev, "out of memory (acm kmalloc)\n");
601                 return -ENOMEM;
602         }
603         memset(acm, 0, sizeof(struct acm));
604
605         ctrlsize = epctrl->wMaxPacketSize;
606         readsize = epread->wMaxPacketSize;
607         acm->writesize = epwrite->wMaxPacketSize;
608         acm->control = control_interface;
609         acm->data = data_interface;
610         acm->minor = minor;
611         acm->dev = usb_dev;
612
613         acm->bh.func = acm_rx_tasklet;
614         acm->bh.data = (unsigned long) acm;
615         INIT_WORK(&acm->work, acm_softint, acm);
616         acm->ready_for_write = 1;
617
618
619         if (!(buf = kmalloc(ctrlsize + readsize + acm->writesize, GFP_KERNEL))) {
620                 dev_dbg(&intf->dev, "out of memory (buf kmalloc)\n");
621                 kfree(acm);
622                 return -ENOMEM;
623         }
624
625         acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
626         if (!acm->ctrlurb) {
627                 dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
628                 kfree(acm);
629                 kfree(buf);
630                 return -ENOMEM;
631         }
632         acm->readurb = usb_alloc_urb(0, GFP_KERNEL);
633         if (!acm->readurb) {
634                 dev_dbg(&intf->dev, "out of memory (readurb kmalloc)\n");
635                 usb_free_urb(acm->ctrlurb);
636                 kfree(acm);
637                 kfree(buf);
638                 return -ENOMEM;
639         }
640         acm->writeurb = usb_alloc_urb(0, GFP_KERNEL);
641         if (!acm->writeurb) {
642                 dev_dbg(&intf->dev, "out of memory (writeurb kmalloc)\n");
643                 usb_free_urb(acm->readurb);
644                 usb_free_urb(acm->ctrlurb);
645                 kfree(acm);
646                 kfree(buf);
647                 return -ENOMEM;
648         }
649
650         usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
651                          buf, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval);
652
653         usb_fill_bulk_urb(acm->readurb, usb_dev, usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress),
654                           buf += ctrlsize, readsize, acm_read_bulk, acm);
655         acm->readurb->transfer_flags |= URB_NO_FSBR;
656
657         usb_fill_bulk_urb(acm->writeurb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
658                           buf += readsize, acm->writesize, acm_write_bulk, acm);
659         acm->writeurb->transfer_flags |= URB_NO_FSBR;
660
661         dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
662
663         acm_set_control(acm, acm->ctrlout);
664
665         acm->line.speed = cpu_to_le32(9600);
666         acm->line.databits = 8;
667         acm_set_line(acm, &acm->line);
668
669         usb_driver_claim_interface(&acm_driver, data_interface, acm);
670
671         tty_register_device(acm_tty_driver, minor, &intf->dev);
672
673         acm_table[minor] = acm;
674         usb_set_intfdata (intf, acm);
675         return 0;
676 }
677
678 static void acm_disconnect(struct usb_interface *intf)
679 {
680         struct acm *acm = usb_get_intfdata (intf);
681
682         if (!acm || !acm->dev) {
683                 dbg("disconnect on nonexisting interface");
684                 return;
685         }
686
687         acm->dev = NULL;
688         usb_set_intfdata (intf, NULL);
689
690         usb_unlink_urb(acm->ctrlurb);
691         usb_unlink_urb(acm->readurb);
692         usb_unlink_urb(acm->writeurb);
693
694         flush_scheduled_work(); /* wait for acm_softint */
695
696         kfree(acm->ctrlurb->transfer_buffer);
697
698         usb_driver_release_interface(&acm_driver, acm->data);
699
700         if (!acm->used) {
701                 tty_unregister_device(acm_tty_driver, acm->minor);
702                 acm_table[acm->minor] = NULL;
703                 usb_free_urb(acm->ctrlurb);
704                 usb_free_urb(acm->readurb);
705                 usb_free_urb(acm->writeurb);
706                 kfree(acm);
707                 return;
708         }
709
710         if (acm->tty)
711                 tty_hangup(acm->tty);
712 }
713
714 /*
715  * USB driver structure.
716  */
717
718 static struct usb_device_id acm_ids[] = {
719         /* control interfaces with various AT-command sets */
720         { USB_INTERFACE_INFO(USB_CLASS_COMM, 2, 1) },
721         { USB_INTERFACE_INFO(USB_CLASS_COMM, 2, 2) },
722         { USB_INTERFACE_INFO(USB_CLASS_COMM, 2, 3) },
723         { USB_INTERFACE_INFO(USB_CLASS_COMM, 2, 4) },
724         { USB_INTERFACE_INFO(USB_CLASS_COMM, 2, 5) },
725         { USB_INTERFACE_INFO(USB_CLASS_COMM, 2, 6) },
726
727         /* NOTE:  COMM/2/0xff is likely MSFT RNDIS ... NOT a modem!! */
728         { }
729 };
730
731 MODULE_DEVICE_TABLE (usb, acm_ids);
732
733 static struct usb_driver acm_driver = {
734         .owner =        THIS_MODULE,
735         .name =         "cdc_acm",
736         .probe =        acm_probe,
737         .disconnect =   acm_disconnect,
738         .id_table =     acm_ids,
739 };
740
741 /*
742  * TTY driver structures.
743  */
744
745 static struct tty_operations acm_ops = {
746         .open =                 acm_tty_open,
747         .close =                acm_tty_close,
748         .write =                acm_tty_write,
749         .write_room =           acm_tty_write_room,
750         .ioctl =                acm_tty_ioctl,
751         .throttle =             acm_tty_throttle,
752         .unthrottle =           acm_tty_unthrottle,
753         .chars_in_buffer =      acm_tty_chars_in_buffer,
754         .break_ctl =            acm_tty_break_ctl,
755         .set_termios =          acm_tty_set_termios,
756         .tiocmget =             acm_tty_tiocmget,
757         .tiocmset =             acm_tty_tiocmset,
758 };
759
760 /*
761  * Init / exit.
762  */
763
764 static int __init acm_init(void)
765 {
766         int retval;
767         acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
768         if (!acm_tty_driver)
769                 return -ENOMEM;
770         acm_tty_driver->owner = THIS_MODULE,
771         acm_tty_driver->driver_name = "acm",
772         acm_tty_driver->name = "ttyACM",
773         acm_tty_driver->devfs_name = "usb/acm/",
774         acm_tty_driver->major = ACM_TTY_MAJOR,
775         acm_tty_driver->minor_start = 0,
776         acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
777         acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
778         acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
779         acm_tty_driver->init_termios = tty_std_termios;
780         acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
781         tty_set_operations(acm_tty_driver, &acm_ops);
782
783         retval = tty_register_driver(acm_tty_driver);
784         if (retval) {
785                 put_tty_driver(acm_tty_driver);
786                 return retval;
787         }
788
789         retval = usb_register(&acm_driver);
790         if (retval) {
791                 tty_unregister_driver(acm_tty_driver);
792                 put_tty_driver(acm_tty_driver);
793                 return retval;
794         }
795
796         info(DRIVER_VERSION ":" DRIVER_DESC);
797
798         return 0;
799 }
800
801 static void __exit acm_exit(void)
802 {
803         usb_deregister(&acm_driver);
804         tty_unregister_driver(acm_tty_driver);
805         put_tty_driver(acm_tty_driver);
806 }
807
808 module_init(acm_init);
809 module_exit(acm_exit);
810
811 MODULE_AUTHOR( DRIVER_AUTHOR );
812 MODULE_DESCRIPTION( DRIVER_DESC );
813 MODULE_LICENSE("GPL");
814