vserver 1.9.5.x5
[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  * Copyright (c) 2004 Oliver Neukum     <oliver@neukum.name>
9  *
10  * USB Abstract Control Model driver for USB modems and ISDN adapters
11  *
12  * Sponsored by SuSE
13  *
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
32  */
33
34 /*
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.
39  *
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.
44  *
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
48  */
49
50 #undef DEBUG
51
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>
65
66 #include "cdc-acm.h"
67
68 /*
69  * Version Information
70  */
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"
74
75 static struct usb_driver acm_driver;
76 static struct tty_driver *acm_tty_driver;
77 static struct acm *acm_table[ACM_TTY_MINORS];
78
79 static DECLARE_MUTEX(open_sem);
80
81 #define ACM_READY(acm)  (acm && acm->dev && acm->used)
82
83 /*
84  * Functions for ACM control messages.
85  */
86
87 static int acm_ctrl_msg(struct acm *acm, int request, int value, void *buf, int len)
88 {
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;
95 }
96
97 /* devices aren't required to support these requests.
98  * the cdc acm descriptor tells whether they do...
99  */
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)
103
104 /*
105  * Interrupt handlers for various ACM device responses
106  */
107
108 /* control interface reports status changes with "interrupt" transfers */
109 static void acm_ctrl_irq(struct urb *urb, struct pt_regs *regs)
110 {
111         struct acm *acm = urb->context;
112         struct usb_ctrlrequest *dr = urb->transfer_buffer;
113         unsigned char *data;
114         int newctrl;
115         int status;
116
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;
130         }
131
132         if (!ACM_READY(acm))
133                 goto exit;
134
135         data = (unsigned char *)(dr + 1);
136         switch (dr->bRequest) {
137
138                 case ACM_IRQ_NETWORK:
139
140                         dbg("%s network", dr->wValue ? "connected to" : "disconnected from");
141                         break;
142
143                 case ACM_IRQ_LINE_STATE:
144
145                         newctrl = le16_to_cpu(get_unaligned((__le16 *) data));
146
147                         if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
148                                 dbg("calling hangup");
149                                 tty_hangup(acm->tty);
150                         }
151
152                         acm->ctrlin = newctrl;
153
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 ? '+' : '-');
159
160                         break;
161
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;
166         }
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);
172 }
173
174 /* data interface returns incoming bytes, or we got unthrottled */
175 static void acm_read_bulk(struct urb *urb, struct pt_regs *regs)
176 {
177         struct acm *acm = urb->context;
178         dbg("Entering acm_read_bulk with status %d\n", urb->status);
179
180         if (!ACM_READY(acm))
181                 return;
182
183         if (urb->status)
184                 dev_dbg(&acm->data->dev, "bulk rx status %d\n", urb->status);
185
186         /* calling tty_flip_buffer_push() in_irq() isn't allowed */
187         tasklet_schedule(&acm->bh);
188 }
189
190 static void acm_rx_tasklet(unsigned long _acm)
191 {
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");
198
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);
205                         }
206                         tty_insert_flip_char(tty, data[i], 0);
207                 }
208                 dbg("Handed %d bytes to tty layer", i+1);
209                 tty_flip_buffer_push(tty);
210         }
211
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;
220         }
221         spin_unlock(&acm->throttle_lock);
222
223         urb->actual_length = 0;
224         urb->dev = acm->dev;
225
226         i = usb_submit_urb(urb, GFP_ATOMIC);
227         if (i)
228                 dev_dbg(&acm->data->dev, "bulk rx resubmit %d\n", i);
229 }
230
231 /* data interface wrote those outgoing bytes */
232 static void acm_write_bulk(struct urb *urb, struct pt_regs *regs)
233 {
234         struct acm *acm = (struct acm *)urb->context;
235         dbg("Entering acm_write_bulk with status %d\n", urb->status);
236
237         if (!ACM_READY(acm))
238                 goto out;
239
240         if (urb->status)
241                 dbg("nonzero write bulk status received: %d", urb->status);
242
243         schedule_work(&acm->work);
244 out:
245         acm->ready_for_write = 1;
246 }
247
248 static void acm_softint(void *private)
249 {
250         struct acm *acm = private;
251         dbg("Entering acm_softint.\n");
252         
253         if (!ACM_READY(acm))
254                 return;
255         tty_wakeup(acm->tty);
256 }
257
258 /*
259  * TTY handlers
260  */
261
262 static int acm_tty_open(struct tty_struct *tty, struct file *filp)
263 {
264         struct acm *acm;
265         int rv = -EINVAL;
266         dbg("Entering acm_tty_open.\n");
267         
268         down(&open_sem);
269
270         acm = acm_table[tty->index];
271         if (!acm || !acm->dev)
272                 goto err_out;
273         else
274                 rv = 0;
275
276         tty->driver_data = acm;
277         acm->tty = tty;
278
279
280
281         if (acm->used++) {
282                 goto done;
283         }
284
285         acm->ctrlurb->dev = acm->dev;
286         if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
287                 dbg("usb_submit_urb(ctrl irq) failed");
288                 goto bail_out;
289         }
290
291         acm->readurb->dev = acm->dev;
292         if (usb_submit_urb(acm->readurb, GFP_KERNEL)) {
293                 dbg("usb_submit_urb(read bulk) failed");
294                 goto bail_out_and_unlink;
295         }
296
297         if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS))
298                 goto full_bailout;
299
300         /* force low_latency on so that our tty_push actually forces the data through, 
301            otherwise it is scheduled, and with high data rates data can get lost. */
302         tty->low_latency = 1;
303
304 done:
305 err_out:
306         up(&open_sem);
307         return rv;
308
309 full_bailout:
310         usb_kill_urb(acm->readurb);
311 bail_out_and_unlink:
312         usb_kill_urb(acm->ctrlurb);
313 bail_out:
314         acm->used--;
315         up(&open_sem);
316         return -EIO;
317 }
318
319 static void acm_tty_close(struct tty_struct *tty, struct file *filp)
320 {
321         struct acm *acm = tty->driver_data;
322
323         if (!acm || !acm->used)
324                 return;
325
326         down(&open_sem);
327         if (!--acm->used) {
328                 if (acm->dev) {
329                         acm_set_control(acm, acm->ctrlout = 0);
330                         usb_kill_urb(acm->ctrlurb);
331                         usb_kill_urb(acm->writeurb);
332                         usb_kill_urb(acm->readurb);
333                 } else {
334                         tty_unregister_device(acm_tty_driver, acm->minor);
335                         acm_table[acm->minor] = NULL;
336                         usb_free_urb(acm->ctrlurb);
337                         usb_free_urb(acm->readurb);
338                         usb_free_urb(acm->writeurb);
339                         kfree(acm);
340                 }
341         }
342         up(&open_sem);
343 }
344
345 static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
346 {
347         struct acm *acm = tty->driver_data;
348         int stat;
349         dbg("Entering acm_tty_write to write %d bytes,\n", count);
350
351         if (!ACM_READY(acm))
352                 return -EINVAL;
353         if (!acm->ready_for_write)
354                 return 0;
355         if (!count)
356                 return 0;
357
358         count = (count > acm->writesize) ? acm->writesize : count;
359
360         dbg("Get %d bytes...", count);
361         memcpy(acm->write_buffer, buf, count);
362         dbg("  Successfully copied.\n");
363
364         acm->writeurb->transfer_buffer_length = count;
365         acm->writeurb->dev = acm->dev;
366
367         acm->ready_for_write = 0;
368         stat = usb_submit_urb(acm->writeurb, GFP_ATOMIC);
369         if (stat < 0) {
370                 dbg("usb_submit_urb(write bulk) failed");
371                 acm->ready_for_write = 1;
372                 return stat;
373         }
374
375         return count;
376 }
377
378 static int acm_tty_write_room(struct tty_struct *tty)
379 {
380         struct acm *acm = tty->driver_data;
381         if (!ACM_READY(acm))
382                 return -EINVAL;
383         return !acm->ready_for_write ? 0 : acm->writesize;
384 }
385
386 static int acm_tty_chars_in_buffer(struct tty_struct *tty)
387 {
388         struct acm *acm = tty->driver_data;
389         if (!ACM_READY(acm))
390                 return -EINVAL;
391         return !acm->ready_for_write ? acm->writeurb->transfer_buffer_length : 0;
392 }
393
394 static void acm_tty_throttle(struct tty_struct *tty)
395 {
396         struct acm *acm = tty->driver_data;
397         if (!ACM_READY(acm))
398                 return;
399         spin_lock_bh(&acm->throttle_lock);
400         acm->throttle = 1;
401         spin_unlock_bh(&acm->throttle_lock);
402 }
403
404 static void acm_tty_unthrottle(struct tty_struct *tty)
405 {
406         struct acm *acm = tty->driver_data;
407         if (!ACM_READY(acm))
408                 return;
409         spin_lock_bh(&acm->throttle_lock);
410         acm->throttle = 0;
411         spin_unlock_bh(&acm->throttle_lock);
412         if (acm->resubmit_to_unthrottle) {
413                 acm->resubmit_to_unthrottle = 0;
414                 acm_read_bulk(acm->readurb, NULL);
415         }
416 }
417
418 static void acm_tty_break_ctl(struct tty_struct *tty, int state)
419 {
420         struct acm *acm = tty->driver_data;
421         if (!ACM_READY(acm))
422                 return;
423         if (acm_send_break(acm, state ? 0xffff : 0))
424                 dbg("send break failed");
425 }
426
427 static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file)
428 {
429         struct acm *acm = tty->driver_data;
430
431         if (!ACM_READY(acm))
432                 return -EINVAL;
433
434         return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
435                (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
436                (acm->ctrlin  & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
437                (acm->ctrlin  & ACM_CTRL_RI  ? TIOCM_RI  : 0) |
438                (acm->ctrlin  & ACM_CTRL_DCD ? TIOCM_CD  : 0) |
439                TIOCM_CTS;
440 }
441
442 static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file,
443                             unsigned int set, unsigned int clear)
444 {
445         struct acm *acm = tty->driver_data;
446         unsigned int newctrl;
447
448         if (!ACM_READY(acm))
449                 return -EINVAL;
450
451         newctrl = acm->ctrlout;
452         set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
453         clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
454
455         newctrl = (newctrl & ~clear) | set;
456
457         if (acm->ctrlout == newctrl)
458                 return 0;
459         return acm_set_control(acm, acm->ctrlout = newctrl);
460 }
461
462 static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
463 {
464         struct acm *acm = tty->driver_data;
465
466         if (!ACM_READY(acm))
467                 return -EINVAL;
468
469         return -ENOIOCTLCMD;
470 }
471
472 static __u32 acm_tty_speed[] = {
473         0, 50, 75, 110, 134, 150, 200, 300, 600,
474         1200, 1800, 2400, 4800, 9600, 19200, 38400,
475         57600, 115200, 230400, 460800, 500000, 576000,
476         921600, 1000000, 1152000, 1500000, 2000000,
477         2500000, 3000000, 3500000, 4000000
478 };
479
480 static __u8 acm_tty_size[] = {
481         5, 6, 7, 8
482 };
483
484 static void acm_tty_set_termios(struct tty_struct *tty, struct termios *termios_old)
485 {
486         struct acm *acm = tty->driver_data;
487         struct termios *termios = tty->termios;
488         struct acm_line newline;
489         int newctrl = acm->ctrlout;
490
491         if (!ACM_READY(acm))
492                 return;
493
494         newline.speed = cpu_to_le32p(acm_tty_speed +
495                 (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0));
496         newline.stopbits = termios->c_cflag & CSTOPB ? 2 : 0;
497         newline.parity = termios->c_cflag & PARENB ?
498                 (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
499         newline.databits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
500
501         acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
502
503         if (!newline.speed) {
504                 newline.speed = acm->line.speed;
505                 newctrl &= ~ACM_CTRL_DTR;
506         } else  newctrl |=  ACM_CTRL_DTR;
507
508         if (newctrl != acm->ctrlout)
509                 acm_set_control(acm, acm->ctrlout = newctrl);
510
511         if (memcmp(&acm->line, &newline, sizeof(struct acm_line))) {
512                 memcpy(&acm->line, &newline, sizeof(struct acm_line));
513                 dbg("set line: %d %d %d %d", newline.speed, newline.stopbits, newline.parity, newline.databits);
514                 acm_set_line(acm, &acm->line);
515         }
516 }
517
518 /*
519  * USB probe and disconnect routines.
520  */
521
522 static int acm_probe (struct usb_interface *intf,
523                       const struct usb_device_id *id)
524 {
525         struct union_desc *union_header = NULL;
526         char *buffer = intf->altsetting->extra;
527         int buflen = intf->altsetting->extralen;
528         struct usb_interface *control_interface;
529         struct usb_interface *data_interface;
530         struct usb_endpoint_descriptor *epctrl;
531         struct usb_endpoint_descriptor *epread;
532         struct usb_endpoint_descriptor *epwrite;
533         struct usb_device *usb_dev = interface_to_usbdev(intf);
534         struct acm *acm;
535         int minor;
536         int ctrlsize,readsize;
537         u8 *buf;
538         u8 ac_management_function = 0;
539         u8 call_management_function = 0;
540         int call_interface_num = -1;
541         int data_interface_num;
542         unsigned long quirks;
543
544         /* handle quirks deadly to normal probing*/
545         quirks = (unsigned long)id->driver_info;
546         if (quirks == NO_UNION_NORMAL) {
547                 data_interface = usb_ifnum_to_if(usb_dev, 1);
548                 control_interface = usb_ifnum_to_if(usb_dev, 0);
549                 goto skip_normal_probe;
550         }
551         
552         /* normal probing*/
553         if (!buffer) {
554                 err("Wierd descriptor references\n");
555                 return -EINVAL;
556         }
557
558         if (!buflen) {
559                 if (intf->cur_altsetting->endpoint->extralen && intf->cur_altsetting->endpoint->extra) {
560                         dev_dbg(&intf->dev,"Seeking extra descriptors on endpoint\n");
561                         buflen = intf->cur_altsetting->endpoint->extralen;
562                         buffer = intf->cur_altsetting->endpoint->extra;
563                 } else {
564                         err("Zero length descriptor references\n");
565                         return -EINVAL;
566                 }
567         }
568
569         while (buflen > 0) {
570                 if (buffer [1] != USB_DT_CS_INTERFACE) {
571                         err("skipping garbage\n");
572                         goto next_desc;
573                 }
574
575                 switch (buffer [2]) {
576                         case CDC_UNION_TYPE: /* we've found it */
577                                 if (union_header) {
578                                         err("More than one union descriptor, skipping ...");
579                                         goto next_desc;
580                                 }
581                                 union_header = (struct union_desc *)buffer;
582                                 break;
583                         case CDC_COUNTRY_TYPE: /* maybe somehow export */
584                                 break; /* for now we ignore it */
585                         case CDC_HEADER_TYPE: /* maybe check version */ 
586                                 break; /* for now we ignore it */ 
587                         case CDC_AC_MANAGEMENT_TYPE:
588                                 ac_management_function = buffer[3];
589                                 break;
590                         case CDC_CALL_MANAGEMENT_TYPE:
591                                 call_management_function = buffer[3];
592                                 call_interface_num = buffer[4];
593                                 if ((call_management_function & 3) != 3)
594                                         err("This device cannot do calls on its own. It is no modem.");
595                                 break;
596                                 
597                         default:
598                                 err("Ignoring extra header, type %d, length %d", buffer[2], buffer[0]);
599                                 break;
600                         }
601 next_desc:
602                 buflen -= buffer[0];
603                 buffer += buffer[0];
604         }
605
606         if (!union_header) {
607                 if (call_interface_num > 0) {
608                         dev_dbg(&intf->dev,"No union descriptor, using call management descriptor\n");
609                         data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
610                         control_interface = intf;
611                 } else {
612                         dev_dbg(&intf->dev,"No union descriptor, giving up\n");
613                         return -ENODEV;
614                 }
615         } else {
616                 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
617                 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
618                 if (!control_interface || !data_interface) {
619                         dev_dbg(&intf->dev,"no interfaces\n");
620                         return -ENODEV;
621                 }
622         }
623         
624         if (data_interface_num != call_interface_num)
625                 dev_dbg(&intf->dev,"Seperate call control interface. That is not fully supported.\n");
626
627 skip_normal_probe:
628
629         /*workaround for switched interfaces */
630         if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) {
631                 if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) {
632                         struct usb_interface *t;
633                         dev_dbg(&intf->dev,"Your device has switched interfaces.\n");
634
635                         t = control_interface;
636                         control_interface = data_interface;
637                         data_interface = t;
638                 } else {
639                         return -EINVAL;
640                 }
641         }
642         
643         if (usb_interface_claimed(data_interface)) { /* valid in this context */
644                 dev_dbg(&intf->dev,"The data interface isn't available\n");
645                 return -EBUSY;
646         }
647
648
649         if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
650                 return -EINVAL;
651
652         epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
653         epread = &data_interface->cur_altsetting->endpoint[0].desc;
654         epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
655
656
657         /* workaround for switched endpoints */
658         if ((epread->bEndpointAddress & USB_DIR_IN) != USB_DIR_IN) {
659                 /* descriptors are swapped */
660                 struct usb_endpoint_descriptor *t;
661                 dev_dbg(&intf->dev,"The data interface has switched endpoints\n");
662                 
663                 t = epread;
664                 epread = epwrite;
665                 epwrite = t;
666         }
667         dbg("interfaces are valid");
668         for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
669
670         if (minor == ACM_TTY_MINORS) {
671                 err("no more free acm devices");
672                 return -ENODEV;
673         }
674
675         if (!(acm = kmalloc(sizeof(struct acm), GFP_KERNEL))) {
676                 dev_dbg(&intf->dev, "out of memory (acm kmalloc)\n");
677                 goto alloc_fail;
678         }
679         memset(acm, 0, sizeof(struct acm));
680
681         ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize);
682         readsize = le16_to_cpu(epread->wMaxPacketSize);
683         acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize);
684         acm->control = control_interface;
685         acm->data = data_interface;
686         acm->minor = minor;
687         acm->dev = usb_dev;
688         acm->ctrl_caps = ac_management_function;
689         acm->ctrlsize = ctrlsize;
690         acm->readsize = readsize;
691         acm->bh.func = acm_rx_tasklet;
692         acm->bh.data = (unsigned long) acm;
693         INIT_WORK(&acm->work, acm_softint, acm);
694         spin_lock_init(&acm->throttle_lock);
695         acm->ready_for_write = 1;
696
697         buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
698         if (!buf) {
699                 dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)\n");
700                 goto alloc_fail2;
701         }
702         acm->ctrl_buffer = buf;
703
704         buf = usb_buffer_alloc(usb_dev, readsize, GFP_KERNEL, &acm->read_dma);
705         if (!buf) {
706                 dev_dbg(&intf->dev, "out of memory (read buffer alloc)\n");
707                 goto alloc_fail3;
708         }
709         acm->read_buffer = buf;
710
711         buf = usb_buffer_alloc(usb_dev, acm->writesize, GFP_KERNEL, &acm->write_dma);
712         if (!buf) {
713                 dev_dbg(&intf->dev, "out of memory (write buffer alloc)\n");
714                 goto alloc_fail4;
715         }
716         acm->write_buffer = buf;        
717
718         acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
719         if (!acm->ctrlurb) {
720                 dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
721                 goto alloc_fail5;
722         }
723         acm->readurb = usb_alloc_urb(0, GFP_KERNEL);
724         if (!acm->readurb) {
725                 dev_dbg(&intf->dev, "out of memory (readurb kmalloc)\n");
726                 goto alloc_fail6;
727         }
728         acm->writeurb = usb_alloc_urb(0, GFP_KERNEL);
729         if (!acm->writeurb) {
730                 dev_dbg(&intf->dev, "out of memory (writeurb kmalloc)\n");
731                 goto alloc_fail7;
732         }
733
734         usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
735                          acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval);
736         acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
737         acm->ctrlurb->transfer_dma = acm->ctrl_dma;
738
739         usb_fill_bulk_urb(acm->readurb, usb_dev, usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress),
740                           acm->read_buffer, readsize, acm_read_bulk, acm);
741         acm->readurb->transfer_flags |= URB_NO_FSBR | URB_NO_TRANSFER_DMA_MAP;
742         acm->readurb->transfer_dma = acm->read_dma;
743
744         usb_fill_bulk_urb(acm->writeurb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
745                           acm->write_buffer, acm->writesize, acm_write_bulk, acm);
746         acm->writeurb->transfer_flags |= URB_NO_FSBR | URB_NO_TRANSFER_DMA_MAP;
747         acm->writeurb->transfer_dma = acm->write_dma;
748
749         dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
750
751         acm_set_control(acm, acm->ctrlout);
752
753         acm->line.speed = cpu_to_le32(9600);
754         acm->line.databits = 8;
755         acm_set_line(acm, &acm->line);
756
757         usb_driver_claim_interface(&acm_driver, data_interface, acm);
758
759         tty_register_device(acm_tty_driver, minor, &intf->dev);
760
761         acm_table[minor] = acm;
762         usb_set_intfdata (intf, acm);
763         return 0;
764
765 alloc_fail7:
766         usb_free_urb(acm->readurb);
767 alloc_fail6:
768         usb_free_urb(acm->ctrlurb);
769 alloc_fail5:
770         usb_buffer_free(usb_dev, acm->writesize, acm->write_buffer, acm->write_dma);
771 alloc_fail4:
772         usb_buffer_free(usb_dev, readsize, acm->read_buffer, acm->read_dma);
773 alloc_fail3:
774         usb_buffer_free(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
775 alloc_fail2:
776         kfree(acm);
777 alloc_fail:
778         return -ENOMEM;
779 }
780
781 static void acm_disconnect(struct usb_interface *intf)
782 {
783         struct acm *acm = usb_get_intfdata (intf);
784         struct usb_device *usb_dev = interface_to_usbdev(intf);
785
786         if (!acm || !acm->dev) {
787                 dbg("disconnect on nonexisting interface");
788                 return;
789         }
790
791         down(&open_sem);
792         acm->dev = NULL;
793         usb_set_intfdata (intf, NULL);
794
795         usb_kill_urb(acm->ctrlurb);
796         usb_kill_urb(acm->readurb);
797         usb_kill_urb(acm->writeurb);
798
799         flush_scheduled_work(); /* wait for acm_softint */
800
801         usb_buffer_free(usb_dev, acm->writesize, acm->write_buffer, acm->write_dma);
802         usb_buffer_free(usb_dev, acm->readsize, acm->read_buffer, acm->read_dma);
803         usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
804
805         usb_driver_release_interface(&acm_driver, acm->data);
806
807         if (!acm->used) {
808                 tty_unregister_device(acm_tty_driver, acm->minor);
809                 acm_table[acm->minor] = NULL;
810                 usb_free_urb(acm->ctrlurb);
811                 usb_free_urb(acm->readurb);
812                 usb_free_urb(acm->writeurb);
813                 kfree(acm);
814                 up(&open_sem);
815                 return;
816         }
817
818         up(&open_sem);
819
820         if (acm->tty)
821                 tty_hangup(acm->tty);
822 }
823
824 /*
825  * USB driver structure.
826  */
827
828 static struct usb_device_id acm_ids[] = {
829         /* quirky and broken devices */
830         { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
831         .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
832         },
833         /* control interfaces with various AT-command sets */
834         { USB_INTERFACE_INFO(USB_CLASS_COMM, 2, 1) },
835         { USB_INTERFACE_INFO(USB_CLASS_COMM, 2, 2) },
836         { USB_INTERFACE_INFO(USB_CLASS_COMM, 2, 3) },
837         { USB_INTERFACE_INFO(USB_CLASS_COMM, 2, 4) },
838         { USB_INTERFACE_INFO(USB_CLASS_COMM, 2, 5) },
839         { USB_INTERFACE_INFO(USB_CLASS_COMM, 2, 6) },
840
841         /* NOTE:  COMM/2/0xff is likely MSFT RNDIS ... NOT a modem!! */
842         { }
843 };
844
845 MODULE_DEVICE_TABLE (usb, acm_ids);
846
847 static struct usb_driver acm_driver = {
848         .owner =        THIS_MODULE,
849         .name =         "cdc_acm",
850         .probe =        acm_probe,
851         .disconnect =   acm_disconnect,
852         .id_table =     acm_ids,
853 };
854
855 /*
856  * TTY driver structures.
857  */
858
859 static struct tty_operations acm_ops = {
860         .open =                 acm_tty_open,
861         .close =                acm_tty_close,
862         .write =                acm_tty_write,
863         .write_room =           acm_tty_write_room,
864         .ioctl =                acm_tty_ioctl,
865         .throttle =             acm_tty_throttle,
866         .unthrottle =           acm_tty_unthrottle,
867         .chars_in_buffer =      acm_tty_chars_in_buffer,
868         .break_ctl =            acm_tty_break_ctl,
869         .set_termios =          acm_tty_set_termios,
870         .tiocmget =             acm_tty_tiocmget,
871         .tiocmset =             acm_tty_tiocmset,
872 };
873
874 /*
875  * Init / exit.
876  */
877
878 static int __init acm_init(void)
879 {
880         int retval;
881         acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
882         if (!acm_tty_driver)
883                 return -ENOMEM;
884         acm_tty_driver->owner = THIS_MODULE,
885         acm_tty_driver->driver_name = "acm",
886         acm_tty_driver->name = "ttyACM",
887         acm_tty_driver->devfs_name = "usb/acm/",
888         acm_tty_driver->major = ACM_TTY_MAJOR,
889         acm_tty_driver->minor_start = 0,
890         acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
891         acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
892         acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
893         acm_tty_driver->init_termios = tty_std_termios;
894         acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
895         tty_set_operations(acm_tty_driver, &acm_ops);
896
897         retval = tty_register_driver(acm_tty_driver);
898         if (retval) {
899                 put_tty_driver(acm_tty_driver);
900                 return retval;
901         }
902
903         retval = usb_register(&acm_driver);
904         if (retval) {
905                 tty_unregister_driver(acm_tty_driver);
906                 put_tty_driver(acm_tty_driver);
907                 return retval;
908         }
909
910         info(DRIVER_VERSION ":" DRIVER_DESC);
911
912         return 0;
913 }
914
915 static void __exit acm_exit(void)
916 {
917         usb_deregister(&acm_driver);
918         tty_unregister_driver(acm_tty_driver);
919         put_tty_driver(acm_tty_driver);
920 }
921
922 module_init(acm_init);
923 module_exit(acm_exit);
924
925 MODULE_AUTHOR( DRIVER_AUTHOR );
926 MODULE_DESCRIPTION( DRIVER_DESC );
927 MODULE_LICENSE("GPL");
928