[PATCH] USB: fix acm trouble with terminals
[safe/jmp/linux-2.6] / 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 <linux/usb_cdc.h>
64 #include <asm/byteorder.h>
65 #include <asm/unaligned.h>
66
67 #include "cdc-acm.h"
68
69 /*
70  * Version Information
71  */
72 #define DRIVER_VERSION "v0.23"
73 #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik"
74 #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
75
76 static struct usb_driver acm_driver;
77 static struct tty_driver *acm_tty_driver;
78 static struct acm *acm_table[ACM_TTY_MINORS];
79
80 static DECLARE_MUTEX(open_sem);
81
82 #define ACM_READY(acm)  (acm && acm->dev && acm->used)
83
84 /*
85  * Functions for ACM control messages.
86  */
87
88 static int acm_ctrl_msg(struct acm *acm, int request, int value, void *buf, int len)
89 {
90         int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
91                 request, USB_RT_ACM, value,
92                 acm->control->altsetting[0].desc.bInterfaceNumber,
93                 buf, len, 5000);
94         dbg("acm_control_msg: rq: 0x%02x val: %#x len: %#x result: %d", request, value, len, retval);
95         return retval < 0 ? retval : 0;
96 }
97
98 /* devices aren't required to support these requests.
99  * the cdc acm descriptor tells whether they do...
100  */
101 #define acm_set_control(acm, control) \
102         acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0)
103 #define acm_set_line(acm, line) \
104         acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
105 #define acm_send_break(acm, ms) \
106         acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
107
108 /*
109  * Write buffer management.
110  * All of these assume proper locks taken by the caller.
111  */
112
113 static int acm_wb_alloc(struct acm *acm)
114 {
115         int i, wbn;
116         struct acm_wb *wb;
117
118         wbn = acm->write_current;
119         i = 0;
120         for (;;) {
121                 wb = &acm->wb[wbn];
122                 if (!wb->use) {
123                         wb->use = 1;
124                         return wbn;
125                 }
126                 wbn = (wbn + 1) % ACM_NWB;
127                 if (++i >= ACM_NWB)
128                         return -1;
129         }
130 }
131
132 static void acm_wb_free(struct acm *acm, int wbn)
133 {
134         acm->wb[wbn].use = 0;
135 }
136
137 static int acm_wb_is_avail(struct acm *acm)
138 {
139         int i, n;
140
141         n = 0;
142         for (i = 0; i < ACM_NWB; i++) {
143                 if (!acm->wb[i].use)
144                         n++;
145         }
146         return n;
147 }
148
149 static inline int acm_wb_is_used(struct acm *acm, int wbn)
150 {
151         return acm->wb[wbn].use;
152 }
153
154 /*
155  * Finish write.
156  */
157 static void acm_write_done(struct acm *acm)
158 {
159         unsigned long flags;
160         int wbn;
161
162         spin_lock_irqsave(&acm->write_lock, flags);
163         acm->write_ready = 1;
164         wbn = acm->write_current;
165         acm_wb_free(acm, wbn);
166         acm->write_current = (wbn + 1) % ACM_NWB;
167         spin_unlock_irqrestore(&acm->write_lock, flags);
168 }
169
170 /*
171  * Poke write.
172  */
173 static int acm_write_start(struct acm *acm)
174 {
175         unsigned long flags;
176         int wbn;
177         struct acm_wb *wb;
178         int rc;
179
180         spin_lock_irqsave(&acm->write_lock, flags);
181         if (!acm->dev) {
182                 spin_unlock_irqrestore(&acm->write_lock, flags);
183                 return -ENODEV;
184         }
185
186         if (!acm->write_ready) {
187                 spin_unlock_irqrestore(&acm->write_lock, flags);
188                 return 0;       /* A white lie */
189         }
190
191         wbn = acm->write_current;
192         if (!acm_wb_is_used(acm, wbn)) {
193                 spin_unlock_irqrestore(&acm->write_lock, flags);
194                 return 0;
195         }
196         wb = &acm->wb[wbn];
197
198         acm->write_ready = 0;
199         spin_unlock_irqrestore(&acm->write_lock, flags);
200
201         acm->writeurb->transfer_buffer = wb->buf;
202         acm->writeurb->transfer_dma = wb->dmah;
203         acm->writeurb->transfer_buffer_length = wb->len;
204         acm->writeurb->dev = acm->dev;
205
206         if ((rc = usb_submit_urb(acm->writeurb, GFP_ATOMIC)) < 0) {
207                 dbg("usb_submit_urb(write bulk) failed: %d", rc);
208                 acm_write_done(acm);
209         }
210         return rc;
211 }
212
213 /*
214  * Interrupt handlers for various ACM device responses
215  */
216
217 /* control interface reports status changes with "interrupt" transfers */
218 static void acm_ctrl_irq(struct urb *urb, struct pt_regs *regs)
219 {
220         struct acm *acm = urb->context;
221         struct usb_cdc_notification *dr = urb->transfer_buffer;
222         unsigned char *data;
223         int newctrl;
224         int status;
225
226         switch (urb->status) {
227         case 0:
228                 /* success */
229                 break;
230         case -ECONNRESET:
231         case -ENOENT:
232         case -ESHUTDOWN:
233                 /* this urb is terminated, clean up */
234                 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
235                 return;
236         default:
237                 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
238                 goto exit;
239         }
240
241         if (!ACM_READY(acm))
242                 goto exit;
243
244         data = (unsigned char *)(dr + 1);
245         switch (dr->bNotificationType) {
246
247                 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
248
249                         dbg("%s network", dr->wValue ? "connected to" : "disconnected from");
250                         break;
251
252                 case USB_CDC_NOTIFY_SERIAL_STATE:
253
254                         newctrl = le16_to_cpu(get_unaligned((__le16 *) data));
255
256                         if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
257                                 dbg("calling hangup");
258                                 tty_hangup(acm->tty);
259                         }
260
261                         acm->ctrlin = newctrl;
262
263                         dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c",
264                                 acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
265                                 acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', acm->ctrlin & ACM_CTRL_RI  ? '+' : '-',
266                                 acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-',     acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
267                                 acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
268
269                         break;
270
271                 default:
272                         dbg("unknown notification %d received: index %d len %d data0 %d data1 %d",
273                                 dr->bNotificationType, dr->wIndex,
274                                 dr->wLength, data[0], data[1]);
275                         break;
276         }
277 exit:
278         status = usb_submit_urb (urb, GFP_ATOMIC);
279         if (status)
280                 err ("%s - usb_submit_urb failed with result %d",
281                      __FUNCTION__, status);
282 }
283
284 /* data interface returns incoming bytes, or we got unthrottled */
285 static void acm_read_bulk(struct urb *urb, struct pt_regs *regs)
286 {
287         struct acm *acm = urb->context;
288         dbg("Entering acm_read_bulk with status %d\n", urb->status);
289
290         if (!ACM_READY(acm))
291                 return;
292
293         if (urb->status)
294                 dev_dbg(&acm->data->dev, "bulk rx status %d\n", urb->status);
295
296         /* calling tty_flip_buffer_push() in_irq() isn't allowed */
297         tasklet_schedule(&acm->bh);
298 }
299
300 static void acm_rx_tasklet(unsigned long _acm)
301 {
302         struct acm *acm = (void *)_acm;
303         struct urb *urb = acm->readurb;
304         struct tty_struct *tty = acm->tty;
305         unsigned char *data = urb->transfer_buffer;
306         int i = 0;
307         dbg("Entering acm_rx_tasklet");
308
309         if (urb->actual_length > 0 && !acm->throttle)  {
310                 for (i = 0; i < urb->actual_length && !acm->throttle; i++) {
311                         /* if we insert more than TTY_FLIPBUF_SIZE characters,
312                          * we drop them. */
313                         if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
314                                 tty_flip_buffer_push(tty);
315                         }
316                         tty_insert_flip_char(tty, data[i], 0);
317                 }
318                 dbg("Handed %d bytes to tty layer", i+1);
319                 tty_flip_buffer_push(tty);
320         }
321
322         spin_lock(&acm->throttle_lock);
323         if (acm->throttle) {
324                 dbg("Throtteling noticed");
325                 memmove(data, data + i, urb->actual_length - i);
326                 urb->actual_length -= i;
327                 acm->resubmit_to_unthrottle = 1;
328                 spin_unlock(&acm->throttle_lock);
329                 return;
330         }
331         spin_unlock(&acm->throttle_lock);
332
333         urb->actual_length = 0;
334         urb->dev = acm->dev;
335
336         i = usb_submit_urb(urb, GFP_ATOMIC);
337         if (i)
338                 dev_dbg(&acm->data->dev, "bulk rx resubmit %d\n", i);
339 }
340
341 /* data interface wrote those outgoing bytes */
342 static void acm_write_bulk(struct urb *urb, struct pt_regs *regs)
343 {
344         struct acm *acm = (struct acm *)urb->context;
345
346         dbg("Entering acm_write_bulk with status %d\n", urb->status);
347
348         acm_write_done(acm);
349         acm_write_start(acm);
350         if (ACM_READY(acm))
351                 schedule_work(&acm->work);
352 }
353
354 static void acm_softint(void *private)
355 {
356         struct acm *acm = private;
357         dbg("Entering acm_softint.\n");
358         
359         if (!ACM_READY(acm))
360                 return;
361         tty_wakeup(acm->tty);
362 }
363
364 /*
365  * TTY handlers
366  */
367
368 static int acm_tty_open(struct tty_struct *tty, struct file *filp)
369 {
370         struct acm *acm;
371         int rv = -EINVAL;
372         dbg("Entering acm_tty_open.\n");
373         
374         down(&open_sem);
375
376         acm = acm_table[tty->index];
377         if (!acm || !acm->dev)
378                 goto err_out;
379         else
380                 rv = 0;
381
382         tty->driver_data = acm;
383         acm->tty = tty;
384
385
386
387         if (acm->used++) {
388                 goto done;
389         }
390
391         acm->ctrlurb->dev = acm->dev;
392         if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
393                 dbg("usb_submit_urb(ctrl irq) failed");
394                 goto bail_out;
395         }
396
397         acm->readurb->dev = acm->dev;
398         if (usb_submit_urb(acm->readurb, GFP_KERNEL)) {
399                 dbg("usb_submit_urb(read bulk) failed");
400                 goto bail_out_and_unlink;
401         }
402
403         if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS))
404                 goto full_bailout;
405
406         /* force low_latency on so that our tty_push actually forces the data through, 
407            otherwise it is scheduled, and with high data rates data can get lost. */
408         tty->low_latency = 1;
409
410 done:
411 err_out:
412         up(&open_sem);
413         return rv;
414
415 full_bailout:
416         usb_kill_urb(acm->readurb);
417 bail_out_and_unlink:
418         usb_kill_urb(acm->ctrlurb);
419 bail_out:
420         acm->used--;
421         up(&open_sem);
422         return -EIO;
423 }
424
425 static void acm_tty_close(struct tty_struct *tty, struct file *filp)
426 {
427         struct acm *acm = tty->driver_data;
428
429         if (!acm || !acm->used)
430                 return;
431
432         down(&open_sem);
433         if (!--acm->used) {
434                 if (acm->dev) {
435                         acm_set_control(acm, acm->ctrlout = 0);
436                         usb_kill_urb(acm->ctrlurb);
437                         usb_kill_urb(acm->writeurb);
438                         usb_kill_urb(acm->readurb);
439                 } else {
440                         tty_unregister_device(acm_tty_driver, acm->minor);
441                         acm_table[acm->minor] = NULL;
442                         usb_free_urb(acm->ctrlurb);
443                         usb_free_urb(acm->readurb);
444                         usb_free_urb(acm->writeurb);
445                         kfree(acm);
446                 }
447         }
448         up(&open_sem);
449 }
450
451 static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
452 {
453         struct acm *acm = tty->driver_data;
454         int stat;
455         unsigned long flags;
456         int wbn;
457         struct acm_wb *wb;
458
459         dbg("Entering acm_tty_write to write %d bytes,\n", count);
460
461         if (!ACM_READY(acm))
462                 return -EINVAL;
463         if (!count)
464                 return 0;
465
466         spin_lock_irqsave(&acm->write_lock, flags);
467         if ((wbn = acm_wb_alloc(acm)) < 0) {
468                 spin_unlock_irqrestore(&acm->write_lock, flags);
469                 acm_write_start(acm);
470                 return 0;
471         }
472         wb = &acm->wb[wbn];
473
474         count = (count > acm->writesize) ? acm->writesize : count;
475         dbg("Get %d bytes...", count);
476         memcpy(wb->buf, buf, count);
477         wb->len = count;
478         spin_unlock_irqrestore(&acm->write_lock, flags);
479
480         if ((stat = acm_write_start(acm)) < 0)
481                 return stat;
482         return count;
483 }
484
485 static int acm_tty_write_room(struct tty_struct *tty)
486 {
487         struct acm *acm = tty->driver_data;
488         if (!ACM_READY(acm))
489                 return -EINVAL;
490         /*
491          * Do not let the line discipline to know that we have a reserve,
492          * or it might get too enthusiastic.
493          */
494         return (acm->write_ready && acm_wb_is_avail(acm)) ? acm->writesize : 0;
495 }
496
497 static int acm_tty_chars_in_buffer(struct tty_struct *tty)
498 {
499         struct acm *acm = tty->driver_data;
500         if (!ACM_READY(acm))
501                 return -EINVAL;
502         /*
503          * This is inaccurate (overcounts), but it works.
504          */
505         return (ACM_NWB - acm_wb_is_avail(acm)) * acm->writesize;
506 }
507
508 static void acm_tty_throttle(struct tty_struct *tty)
509 {
510         struct acm *acm = tty->driver_data;
511         if (!ACM_READY(acm))
512                 return;
513         spin_lock_bh(&acm->throttle_lock);
514         acm->throttle = 1;
515         spin_unlock_bh(&acm->throttle_lock);
516 }
517
518 static void acm_tty_unthrottle(struct tty_struct *tty)
519 {
520         struct acm *acm = tty->driver_data;
521         if (!ACM_READY(acm))
522                 return;
523         spin_lock_bh(&acm->throttle_lock);
524         acm->throttle = 0;
525         spin_unlock_bh(&acm->throttle_lock);
526         if (acm->resubmit_to_unthrottle) {
527                 acm->resubmit_to_unthrottle = 0;
528                 acm_read_bulk(acm->readurb, NULL);
529         }
530 }
531
532 static void acm_tty_break_ctl(struct tty_struct *tty, int state)
533 {
534         struct acm *acm = tty->driver_data;
535         if (!ACM_READY(acm))
536                 return;
537         if (acm_send_break(acm, state ? 0xffff : 0))
538                 dbg("send break failed");
539 }
540
541 static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file)
542 {
543         struct acm *acm = tty->driver_data;
544
545         if (!ACM_READY(acm))
546                 return -EINVAL;
547
548         return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
549                (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
550                (acm->ctrlin  & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
551                (acm->ctrlin  & ACM_CTRL_RI  ? TIOCM_RI  : 0) |
552                (acm->ctrlin  & ACM_CTRL_DCD ? TIOCM_CD  : 0) |
553                TIOCM_CTS;
554 }
555
556 static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file,
557                             unsigned int set, unsigned int clear)
558 {
559         struct acm *acm = tty->driver_data;
560         unsigned int newctrl;
561
562         if (!ACM_READY(acm))
563                 return -EINVAL;
564
565         newctrl = acm->ctrlout;
566         set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
567         clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
568
569         newctrl = (newctrl & ~clear) | set;
570
571         if (acm->ctrlout == newctrl)
572                 return 0;
573         return acm_set_control(acm, acm->ctrlout = newctrl);
574 }
575
576 static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
577 {
578         struct acm *acm = tty->driver_data;
579
580         if (!ACM_READY(acm))
581                 return -EINVAL;
582
583         return -ENOIOCTLCMD;
584 }
585
586 static __u32 acm_tty_speed[] = {
587         0, 50, 75, 110, 134, 150, 200, 300, 600,
588         1200, 1800, 2400, 4800, 9600, 19200, 38400,
589         57600, 115200, 230400, 460800, 500000, 576000,
590         921600, 1000000, 1152000, 1500000, 2000000,
591         2500000, 3000000, 3500000, 4000000
592 };
593
594 static __u8 acm_tty_size[] = {
595         5, 6, 7, 8
596 };
597
598 static void acm_tty_set_termios(struct tty_struct *tty, struct termios *termios_old)
599 {
600         struct acm *acm = tty->driver_data;
601         struct termios *termios = tty->termios;
602         struct usb_cdc_line_coding newline;
603         int newctrl = acm->ctrlout;
604
605         if (!ACM_READY(acm))
606                 return;
607
608         newline.dwDTERate = cpu_to_le32p(acm_tty_speed +
609                 (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0));
610         newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
611         newline.bParityType = termios->c_cflag & PARENB ?
612                 (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
613         newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
614
615         acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
616
617         if (!newline.dwDTERate) {
618                 newline.dwDTERate = acm->line.dwDTERate;
619                 newctrl &= ~ACM_CTRL_DTR;
620         } else  newctrl |=  ACM_CTRL_DTR;
621
622         if (newctrl != acm->ctrlout)
623                 acm_set_control(acm, acm->ctrlout = newctrl);
624
625         if (memcmp(&acm->line, &newline, sizeof newline)) {
626                 memcpy(&acm->line, &newline, sizeof newline);
627                 dbg("set line: %d %d %d %d", le32_to_cpu(newline.dwDTERate),
628                         newline.bCharFormat, newline.bParityType,
629                         newline.bDataBits);
630                 acm_set_line(acm, &acm->line);
631         }
632 }
633
634 /*
635  * USB probe and disconnect routines.
636  */
637
638 /* Little helper: write buffers free */
639 static void acm_write_buffers_free(struct acm *acm)
640 {
641         int i;
642         struct acm_wb *wb;
643
644         for (wb = &acm->wb[0], i = 0; i < ACM_NWB; i++, wb++) {
645                 usb_buffer_free(acm->dev, acm->writesize, wb->buf, wb->dmah);
646         }
647 }
648
649 /* Little helper: write buffers allocate */
650 static int acm_write_buffers_alloc(struct acm *acm)
651 {
652         int i;
653         struct acm_wb *wb;
654
655         for (wb = &acm->wb[0], i = 0; i < ACM_NWB; i++, wb++) {
656                 wb->buf = usb_buffer_alloc(acm->dev, acm->writesize, GFP_KERNEL,
657                     &wb->dmah);
658                 if (!wb->buf) {
659                         while (i != 0) {
660                                 --i;
661                                 --wb;
662                                 usb_buffer_free(acm->dev, acm->writesize,
663                                     wb->buf, wb->dmah);
664                         }
665                         return -ENOMEM;
666                 }
667         }
668         return 0;
669 }
670
671 static int acm_probe (struct usb_interface *intf,
672                       const struct usb_device_id *id)
673 {
674         struct usb_cdc_union_desc *union_header = NULL;
675         char *buffer = intf->altsetting->extra;
676         int buflen = intf->altsetting->extralen;
677         struct usb_interface *control_interface;
678         struct usb_interface *data_interface;
679         struct usb_endpoint_descriptor *epctrl;
680         struct usb_endpoint_descriptor *epread;
681         struct usb_endpoint_descriptor *epwrite;
682         struct usb_device *usb_dev = interface_to_usbdev(intf);
683         struct acm *acm;
684         int minor;
685         int ctrlsize,readsize;
686         u8 *buf;
687         u8 ac_management_function = 0;
688         u8 call_management_function = 0;
689         int call_interface_num = -1;
690         int data_interface_num;
691         unsigned long quirks;
692
693         /* handle quirks deadly to normal probing*/
694         quirks = (unsigned long)id->driver_info;
695         if (quirks == NO_UNION_NORMAL) {
696                 data_interface = usb_ifnum_to_if(usb_dev, 1);
697                 control_interface = usb_ifnum_to_if(usb_dev, 0);
698                 goto skip_normal_probe;
699         }
700         
701         /* normal probing*/
702         if (!buffer) {
703                 err("Wierd descriptor references\n");
704                 return -EINVAL;
705         }
706
707         if (!buflen) {
708                 if (intf->cur_altsetting->endpoint->extralen && intf->cur_altsetting->endpoint->extra) {
709                         dev_dbg(&intf->dev,"Seeking extra descriptors on endpoint\n");
710                         buflen = intf->cur_altsetting->endpoint->extralen;
711                         buffer = intf->cur_altsetting->endpoint->extra;
712                 } else {
713                         err("Zero length descriptor references\n");
714                         return -EINVAL;
715                 }
716         }
717
718         while (buflen > 0) {
719                 if (buffer [1] != USB_DT_CS_INTERFACE) {
720                         err("skipping garbage\n");
721                         goto next_desc;
722                 }
723
724                 switch (buffer [2]) {
725                         case USB_CDC_UNION_TYPE: /* we've found it */
726                                 if (union_header) {
727                                         err("More than one union descriptor, skipping ...");
728                                         goto next_desc;
729                                 }
730                                 union_header = (struct usb_cdc_union_desc *)
731                                                         buffer;
732                                 break;
733                         case USB_CDC_COUNTRY_TYPE: /* maybe somehow export */
734                                 break; /* for now we ignore it */
735                         case USB_CDC_HEADER_TYPE: /* maybe check version */ 
736                                 break; /* for now we ignore it */ 
737                         case USB_CDC_ACM_TYPE:
738                                 ac_management_function = buffer[3];
739                                 break;
740                         case USB_CDC_CALL_MANAGEMENT_TYPE:
741                                 call_management_function = buffer[3];
742                                 call_interface_num = buffer[4];
743                                 if ((call_management_function & 3) != 3)
744                                         err("This device cannot do calls on its own. It is no modem.");
745                                 break;
746                                 
747                         default:
748                                 err("Ignoring extra header, type %d, length %d", buffer[2], buffer[0]);
749                                 break;
750                         }
751 next_desc:
752                 buflen -= buffer[0];
753                 buffer += buffer[0];
754         }
755
756         if (!union_header) {
757                 if (call_interface_num > 0) {
758                         dev_dbg(&intf->dev,"No union descriptor, using call management descriptor\n");
759                         data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
760                         control_interface = intf;
761                 } else {
762                         dev_dbg(&intf->dev,"No union descriptor, giving up\n");
763                         return -ENODEV;
764                 }
765         } else {
766                 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
767                 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
768                 if (!control_interface || !data_interface) {
769                         dev_dbg(&intf->dev,"no interfaces\n");
770                         return -ENODEV;
771                 }
772         }
773         
774         if (data_interface_num != call_interface_num)
775                 dev_dbg(&intf->dev,"Seperate call control interface. That is not fully supported.\n");
776
777 skip_normal_probe:
778
779         /*workaround for switched interfaces */
780         if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) {
781                 if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) {
782                         struct usb_interface *t;
783                         dev_dbg(&intf->dev,"Your device has switched interfaces.\n");
784
785                         t = control_interface;
786                         control_interface = data_interface;
787                         data_interface = t;
788                 } else {
789                         return -EINVAL;
790                 }
791         }
792         
793         if (usb_interface_claimed(data_interface)) { /* valid in this context */
794                 dev_dbg(&intf->dev,"The data interface isn't available\n");
795                 return -EBUSY;
796         }
797
798
799         if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
800                 return -EINVAL;
801
802         epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
803         epread = &data_interface->cur_altsetting->endpoint[0].desc;
804         epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
805
806
807         /* workaround for switched endpoints */
808         if ((epread->bEndpointAddress & USB_DIR_IN) != USB_DIR_IN) {
809                 /* descriptors are swapped */
810                 struct usb_endpoint_descriptor *t;
811                 dev_dbg(&intf->dev,"The data interface has switched endpoints\n");
812                 
813                 t = epread;
814                 epread = epwrite;
815                 epwrite = t;
816         }
817         dbg("interfaces are valid");
818         for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
819
820         if (minor == ACM_TTY_MINORS) {
821                 err("no more free acm devices");
822                 return -ENODEV;
823         }
824
825         if (!(acm = kmalloc(sizeof(struct acm), GFP_KERNEL))) {
826                 dev_dbg(&intf->dev, "out of memory (acm kmalloc)\n");
827                 goto alloc_fail;
828         }
829         memset(acm, 0, sizeof(struct acm));
830
831         ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize);
832         readsize = le16_to_cpu(epread->wMaxPacketSize);
833         acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize);
834         acm->control = control_interface;
835         acm->data = data_interface;
836         acm->minor = minor;
837         acm->dev = usb_dev;
838         acm->ctrl_caps = ac_management_function;
839         acm->ctrlsize = ctrlsize;
840         acm->readsize = readsize;
841         acm->bh.func = acm_rx_tasklet;
842         acm->bh.data = (unsigned long) acm;
843         INIT_WORK(&acm->work, acm_softint, acm);
844         spin_lock_init(&acm->throttle_lock);
845         spin_lock_init(&acm->write_lock);
846         acm->write_ready = 1;
847
848         buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
849         if (!buf) {
850                 dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)\n");
851                 goto alloc_fail2;
852         }
853         acm->ctrl_buffer = buf;
854
855         buf = usb_buffer_alloc(usb_dev, readsize, GFP_KERNEL, &acm->read_dma);
856         if (!buf) {
857                 dev_dbg(&intf->dev, "out of memory (read buffer alloc)\n");
858                 goto alloc_fail3;
859         }
860         acm->read_buffer = buf;
861
862         if (acm_write_buffers_alloc(acm) < 0) {
863                 dev_dbg(&intf->dev, "out of memory (write buffer alloc)\n");
864                 goto alloc_fail4;
865         }
866
867         acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
868         if (!acm->ctrlurb) {
869                 dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
870                 goto alloc_fail5;
871         }
872         acm->readurb = usb_alloc_urb(0, GFP_KERNEL);
873         if (!acm->readurb) {
874                 dev_dbg(&intf->dev, "out of memory (readurb kmalloc)\n");
875                 goto alloc_fail6;
876         }
877         acm->writeurb = usb_alloc_urb(0, GFP_KERNEL);
878         if (!acm->writeurb) {
879                 dev_dbg(&intf->dev, "out of memory (writeurb kmalloc)\n");
880                 goto alloc_fail7;
881         }
882
883         usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
884                          acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval);
885         acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
886         acm->ctrlurb->transfer_dma = acm->ctrl_dma;
887
888         usb_fill_bulk_urb(acm->readurb, usb_dev, usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress),
889                           acm->read_buffer, readsize, acm_read_bulk, acm);
890         acm->readurb->transfer_flags |= URB_NO_FSBR | URB_NO_TRANSFER_DMA_MAP;
891         acm->readurb->transfer_dma = acm->read_dma;
892
893         usb_fill_bulk_urb(acm->writeurb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
894                           NULL, acm->writesize, acm_write_bulk, acm);
895         acm->writeurb->transfer_flags |= URB_NO_FSBR | URB_NO_TRANSFER_DMA_MAP;
896         /* acm->writeurb->transfer_dma = 0; */
897
898         dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
899
900         acm_set_control(acm, acm->ctrlout);
901
902         acm->line.dwDTERate = cpu_to_le32(9600);
903         acm->line.bDataBits = 8;
904         acm_set_line(acm, &acm->line);
905
906         usb_driver_claim_interface(&acm_driver, data_interface, acm);
907
908         tty_register_device(acm_tty_driver, minor, &intf->dev);
909
910         acm_table[minor] = acm;
911         usb_set_intfdata (intf, acm);
912         return 0;
913
914 alloc_fail7:
915         usb_free_urb(acm->readurb);
916 alloc_fail6:
917         usb_free_urb(acm->ctrlurb);
918 alloc_fail5:
919         acm_write_buffers_free(acm);
920 alloc_fail4:
921         usb_buffer_free(usb_dev, readsize, acm->read_buffer, acm->read_dma);
922 alloc_fail3:
923         usb_buffer_free(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
924 alloc_fail2:
925         kfree(acm);
926 alloc_fail:
927         return -ENOMEM;
928 }
929
930 static void acm_disconnect(struct usb_interface *intf)
931 {
932         struct acm *acm = usb_get_intfdata (intf);
933         struct usb_device *usb_dev = interface_to_usbdev(intf);
934
935         if (!acm || !acm->dev) {
936                 dbg("disconnect on nonexisting interface");
937                 return;
938         }
939
940         down(&open_sem);
941         acm->dev = NULL;
942         usb_set_intfdata (intf, NULL);
943
944         usb_kill_urb(acm->ctrlurb);
945         usb_kill_urb(acm->readurb);
946         usb_kill_urb(acm->writeurb);
947
948         flush_scheduled_work(); /* wait for acm_softint */
949
950         acm_write_buffers_free(acm);
951         usb_buffer_free(usb_dev, acm->readsize, acm->read_buffer, acm->read_dma);
952         usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
953
954         usb_driver_release_interface(&acm_driver, acm->data);
955
956         if (!acm->used) {
957                 tty_unregister_device(acm_tty_driver, acm->minor);
958                 acm_table[acm->minor] = NULL;
959                 usb_free_urb(acm->ctrlurb);
960                 usb_free_urb(acm->readurb);
961                 usb_free_urb(acm->writeurb);
962                 kfree(acm);
963                 up(&open_sem);
964                 return;
965         }
966
967         up(&open_sem);
968
969         if (acm->tty)
970                 tty_hangup(acm->tty);
971 }
972
973 /*
974  * USB driver structure.
975  */
976
977 static struct usb_device_id acm_ids[] = {
978         /* quirky and broken devices */
979         { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
980         .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
981         },
982         /* control interfaces with various AT-command sets */
983         { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
984                 USB_CDC_ACM_PROTO_AT_V25TER) },
985         { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
986                 USB_CDC_ACM_PROTO_AT_PCCA101) },
987         { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
988                 USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
989         { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
990                 USB_CDC_ACM_PROTO_AT_GSM) },
991         { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
992                 USB_CDC_ACM_PROTO_AT_3G ) },
993         { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
994                 USB_CDC_ACM_PROTO_AT_CDMA) },
995
996         /* NOTE:  COMM/ACM/0xff is likely MSFT RNDIS ... NOT a modem!! */
997         { }
998 };
999
1000 MODULE_DEVICE_TABLE (usb, acm_ids);
1001
1002 static struct usb_driver acm_driver = {
1003         .owner =        THIS_MODULE,
1004         .name =         "cdc_acm",
1005         .probe =        acm_probe,
1006         .disconnect =   acm_disconnect,
1007         .id_table =     acm_ids,
1008 };
1009
1010 /*
1011  * TTY driver structures.
1012  */
1013
1014 static struct tty_operations acm_ops = {
1015         .open =                 acm_tty_open,
1016         .close =                acm_tty_close,
1017         .write =                acm_tty_write,
1018         .write_room =           acm_tty_write_room,
1019         .ioctl =                acm_tty_ioctl,
1020         .throttle =             acm_tty_throttle,
1021         .unthrottle =           acm_tty_unthrottle,
1022         .chars_in_buffer =      acm_tty_chars_in_buffer,
1023         .break_ctl =            acm_tty_break_ctl,
1024         .set_termios =          acm_tty_set_termios,
1025         .tiocmget =             acm_tty_tiocmget,
1026         .tiocmset =             acm_tty_tiocmset,
1027 };
1028
1029 /*
1030  * Init / exit.
1031  */
1032
1033 static int __init acm_init(void)
1034 {
1035         int retval;
1036         acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
1037         if (!acm_tty_driver)
1038                 return -ENOMEM;
1039         acm_tty_driver->owner = THIS_MODULE,
1040         acm_tty_driver->driver_name = "acm",
1041         acm_tty_driver->name = "ttyACM",
1042         acm_tty_driver->devfs_name = "usb/acm/",
1043         acm_tty_driver->major = ACM_TTY_MAJOR,
1044         acm_tty_driver->minor_start = 0,
1045         acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
1046         acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
1047         acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
1048         acm_tty_driver->init_termios = tty_std_termios;
1049         acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1050         tty_set_operations(acm_tty_driver, &acm_ops);
1051
1052         retval = tty_register_driver(acm_tty_driver);
1053         if (retval) {
1054                 put_tty_driver(acm_tty_driver);
1055                 return retval;
1056         }
1057
1058         retval = usb_register(&acm_driver);
1059         if (retval) {
1060                 tty_unregister_driver(acm_tty_driver);
1061                 put_tty_driver(acm_tty_driver);
1062                 return retval;
1063         }
1064
1065         info(DRIVER_VERSION ":" DRIVER_DESC);
1066
1067         return 0;
1068 }
1069
1070 static void __exit acm_exit(void)
1071 {
1072         usb_deregister(&acm_driver);
1073         tty_unregister_driver(acm_tty_driver);
1074         put_tty_driver(acm_tty_driver);
1075 }
1076
1077 module_init(acm_init);
1078 module_exit(acm_exit);
1079
1080 MODULE_AUTHOR( DRIVER_AUTHOR );
1081 MODULE_DESCRIPTION( DRIVER_DESC );
1082 MODULE_LICENSE("GPL");
1083