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