]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/usb/class/cdc-acm.c
usb: cdc-acm: stop dropping tx buffers
[linux-2.6-omap-h63xx.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  * Copyright (c) 2005 David Kubicek     <dave@awk.cz>
10  *
11  * USB Abstract Control Model driver for USB modems and ISDN adapters
12  *
13  * Sponsored by SuSE
14  *
15  * ChangeLog:
16  *      v0.9  - thorough cleaning, URBification, almost a rewrite
17  *      v0.10 - some more cleanups
18  *      v0.11 - fixed flow control, read error doesn't stop reads
19  *      v0.12 - added TIOCM ioctls, added break handling, made struct acm kmalloced
20  *      v0.13 - added termios, added hangup
21  *      v0.14 - sized down struct acm
22  *      v0.15 - fixed flow control again - characters could be lost
23  *      v0.16 - added code for modems with swapped data and control interfaces
24  *      v0.17 - added new style probing
25  *      v0.18 - fixed new style probing for devices with more configurations
26  *      v0.19 - fixed CLOCAL handling (thanks to Richard Shih-Ping Chan)
27  *      v0.20 - switched to probing on interface (rather than device) class
28  *      v0.21 - revert to probing on device for devices with multiple configs
29  *      v0.22 - probe only the control interface. if usbcore doesn't choose the
30  *              config we want, sysadmin changes bConfigurationValue in sysfs.
31  *      v0.23 - use softirq for rx processing, as needed by tty layer
32  *      v0.24 - change probe method to evaluate CDC union descriptor
33  *      v0.25 - downstream tasks paralelized to maximize throughput
34  *      v0.26 - multiple write urbs, writesize increased
35  */
36
37 /*
38  * This program is free software; you can redistribute it and/or modify
39  * it under the terms of the GNU General Public License as published by
40  * the Free Software Foundation; either version 2 of the License, or
41  * (at your option) any later version.
42  *
43  * This program is distributed in the hope that it will be useful,
44  * but WITHOUT ANY WARRANTY; without even the implied warranty of
45  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
46  * GNU General Public License for more details.
47  *
48  * You should have received a copy of the GNU General Public License
49  * along with this program; if not, write to the Free Software
50  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
51  */
52
53 #undef DEBUG
54
55 #include <linux/kernel.h>
56 #include <linux/errno.h>
57 #include <linux/init.h>
58 #include <linux/slab.h>
59 #include <linux/tty.h>
60 #include <linux/tty_driver.h>
61 #include <linux/tty_flip.h>
62 #include <linux/module.h>
63 #include <linux/mutex.h>
64 #include <asm/uaccess.h>
65 #include <linux/usb.h>
66 #include <linux/usb/cdc.h>
67 #include <asm/byteorder.h>
68 #include <asm/unaligned.h>
69 #include <linux/list.h>
70
71 #include "cdc-acm.h"
72
73 /*
74  * Version Information
75  */
76 #define DRIVER_VERSION "v0.26"
77 #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek"
78 #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
79
80 static struct usb_driver acm_driver;
81 static struct tty_driver *acm_tty_driver;
82 static struct acm *acm_table[ACM_TTY_MINORS];
83
84 static DEFINE_MUTEX(open_mutex);
85
86 #define ACM_READY(acm)  (acm && acm->dev && acm->used)
87
88 /*
89  * Functions for ACM control messages.
90  */
91
92 static int acm_ctrl_msg(struct acm *acm, int request, int value, void *buf, int len)
93 {
94         int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
95                 request, USB_RT_ACM, value,
96                 acm->control->altsetting[0].desc.bInterfaceNumber,
97                 buf, len, 5000);
98         dbg("acm_control_msg: rq: 0x%02x val: %#x len: %#x result: %d", request, value, len, retval);
99         return retval < 0 ? retval : 0;
100 }
101
102 /* devices aren't required to support these requests.
103  * the cdc acm descriptor tells whether they do...
104  */
105 #define acm_set_control(acm, control) \
106         acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0)
107 #define acm_set_line(acm, line) \
108         acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
109 #define acm_send_break(acm, ms) \
110         acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
111
112 /*
113  * Write buffer management.
114  * All of these assume proper locks taken by the caller.
115  */
116
117 static int acm_wb_alloc(struct acm *acm)
118 {
119         int i, wbn;
120         struct acm_wb *wb;
121
122         wbn = 0;
123         i = 0;
124         for (;;) {
125                 wb = &acm->wb[wbn];
126                 if (!wb->use) {
127                         wb->use = 1;
128                         return wbn;
129                 }
130                 wbn = (wbn + 1) % ACM_NW;
131                 if (++i >= ACM_NW)
132                         return -1;
133         }
134 }
135
136 static int acm_wb_is_avail(struct acm *acm)
137 {
138         int i, n;
139
140         n = ACM_NW;
141         for (i = 0; i < ACM_NW; i++) {
142                 n -= acm->wb[i].use;
143         }
144         return n;
145 }
146
147 /*
148  * Finish write.
149  */
150 static void acm_write_done(struct acm *acm, struct acm_wb *wb)
151 {
152         unsigned long flags;
153
154         spin_lock_irqsave(&acm->write_lock, flags);
155         wb->use = 0;
156         acm->transmitting--;
157         spin_unlock_irqrestore(&acm->write_lock, flags);
158 }
159
160 /*
161  * Poke write.
162  *
163  * the caller is responsible for locking
164  */
165
166 static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
167 {
168         int rc;
169
170         acm->transmitting++;
171
172         wb->urb->transfer_buffer = wb->buf;
173         wb->urb->transfer_dma = wb->dmah;
174         wb->urb->transfer_buffer_length = wb->len;
175         wb->urb->dev = acm->dev;
176
177         if ((rc = usb_submit_urb(wb->urb, GFP_ATOMIC)) < 0) {
178                 dbg("usb_submit_urb(write bulk) failed: %d", rc);
179                 acm_write_done(acm, wb);
180         }
181         return rc;
182 }
183
184 static int acm_write_start(struct acm *acm, int wbn)
185 {
186         unsigned long flags;
187         struct acm_wb *wb = &acm->wb[wbn];
188         int rc;
189
190         spin_lock_irqsave(&acm->write_lock, flags);
191         if (!acm->dev) {
192                 wb->use = 0;
193                 spin_unlock_irqrestore(&acm->write_lock, flags);
194                 return -ENODEV;
195         }
196
197         dbg("%s susp_count: %d", __func__, acm->susp_count);
198         if (acm->susp_count) {
199                 acm->delayed_wb = wb;
200                 schedule_work(&acm->waker);
201                 spin_unlock_irqrestore(&acm->write_lock, flags);
202                 return 0;       /* A white lie */
203         }
204         usb_mark_last_busy(acm->dev);
205
206         rc = acm_start_wb(acm, wb);
207         spin_unlock_irqrestore(&acm->write_lock, flags);
208
209         return rc;
210
211 }
212 /*
213  * attributes exported through sysfs
214  */
215 static ssize_t show_caps
216 (struct device *dev, struct device_attribute *attr, char *buf)
217 {
218         struct usb_interface *intf = to_usb_interface(dev);
219         struct acm *acm = usb_get_intfdata(intf);
220
221         return sprintf(buf, "%d", acm->ctrl_caps);
222 }
223 static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
224
225 static ssize_t show_country_codes
226 (struct device *dev, struct device_attribute *attr, char *buf)
227 {
228         struct usb_interface *intf = to_usb_interface(dev);
229         struct acm *acm = usb_get_intfdata(intf);
230
231         memcpy(buf, acm->country_codes, acm->country_code_size);
232         return acm->country_code_size;
233 }
234
235 static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
236
237 static ssize_t show_country_rel_date
238 (struct device *dev, struct device_attribute *attr, char *buf)
239 {
240         struct usb_interface *intf = to_usb_interface(dev);
241         struct acm *acm = usb_get_intfdata(intf);
242
243         return sprintf(buf, "%d", acm->country_rel_date);
244 }
245
246 static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
247 /*
248  * Interrupt handlers for various ACM device responses
249  */
250
251 /* control interface reports status changes with "interrupt" transfers */
252 static void acm_ctrl_irq(struct urb *urb)
253 {
254         struct acm *acm = urb->context;
255         struct usb_cdc_notification *dr = urb->transfer_buffer;
256         unsigned char *data;
257         int newctrl;
258         int retval;
259         int status = urb->status;
260
261         switch (status) {
262         case 0:
263                 /* success */
264                 break;
265         case -ECONNRESET:
266         case -ENOENT:
267         case -ESHUTDOWN:
268                 /* this urb is terminated, clean up */
269                 dbg("%s - urb shutting down with status: %d", __func__, status);
270                 return;
271         default:
272                 dbg("%s - nonzero urb status received: %d", __func__, status);
273                 goto exit;
274         }
275
276         if (!ACM_READY(acm))
277                 goto exit;
278
279         data = (unsigned char *)(dr + 1);
280         switch (dr->bNotificationType) {
281
282                 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
283
284                         dbg("%s network", dr->wValue ? "connected to" : "disconnected from");
285                         break;
286
287                 case USB_CDC_NOTIFY_SERIAL_STATE:
288
289                         newctrl = get_unaligned_le16(data);
290
291                         if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
292                                 dbg("calling hangup");
293                                 tty_hangup(acm->tty);
294                         }
295
296                         acm->ctrlin = newctrl;
297
298                         dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c",
299                                 acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
300                                 acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', acm->ctrlin & ACM_CTRL_RI  ? '+' : '-',
301                                 acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-',     acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
302                                 acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
303
304                         break;
305
306                 default:
307                         dbg("unknown notification %d received: index %d len %d data0 %d data1 %d",
308                                 dr->bNotificationType, dr->wIndex,
309                                 dr->wLength, data[0], data[1]);
310                         break;
311         }
312 exit:
313         usb_mark_last_busy(acm->dev);
314         retval = usb_submit_urb (urb, GFP_ATOMIC);
315         if (retval)
316                 err ("%s - usb_submit_urb failed with result %d",
317                      __func__, retval);
318 }
319
320 /* data interface returns incoming bytes, or we got unthrottled */
321 static void acm_read_bulk(struct urb *urb)
322 {
323         struct acm_rb *buf;
324         struct acm_ru *rcv = urb->context;
325         struct acm *acm = rcv->instance;
326         int status = urb->status;
327
328         dbg("Entering acm_read_bulk with status %d", status);
329
330         if (!ACM_READY(acm)) {
331                 dev_dbg(&acm->data->dev, "Aborting, acm not ready");
332                 return;
333         }
334         usb_mark_last_busy(acm->dev);
335
336         if (status)
337                 dev_dbg(&acm->data->dev, "bulk rx status %d\n", status);
338
339         buf = rcv->buffer;
340         buf->size = urb->actual_length;
341
342         if (likely(status == 0)) {
343                 spin_lock(&acm->read_lock);
344                 acm->processing++;
345                 list_add_tail(&rcv->list, &acm->spare_read_urbs);
346                 list_add_tail(&buf->list, &acm->filled_read_bufs);
347                 spin_unlock(&acm->read_lock);
348         } else {
349                 /* we drop the buffer due to an error */
350                 spin_lock(&acm->read_lock);
351                 list_add_tail(&rcv->list, &acm->spare_read_urbs);
352                 list_add(&buf->list, &acm->spare_read_bufs);
353                 spin_unlock(&acm->read_lock);
354                 /* nevertheless the tasklet must be kicked unconditionally
355                 so the queue cannot dry up */
356         }
357         if (likely(!acm->susp_count))
358                 tasklet_schedule(&acm->urb_task);
359 }
360
361 static void acm_rx_tasklet(unsigned long _acm)
362 {
363         struct acm *acm = (void *)_acm;
364         struct acm_rb *buf;
365         struct tty_struct *tty = acm->tty;
366         struct acm_ru *rcv;
367         unsigned long flags;
368         unsigned char throttled;
369
370         dbg("Entering acm_rx_tasklet");
371
372         if (!ACM_READY(acm))
373         {
374                 dbg("acm_rx_tasklet: ACM not ready");
375                 return;
376         }
377
378         spin_lock_irqsave(&acm->throttle_lock, flags);
379         throttled = acm->throttle;
380         spin_unlock_irqrestore(&acm->throttle_lock, flags);
381         if (throttled)
382         {
383                 dbg("acm_rx_tasklet: throttled");
384                 return;
385         }
386
387 next_buffer:
388         spin_lock_irqsave(&acm->read_lock, flags);
389         if (list_empty(&acm->filled_read_bufs)) {
390                 spin_unlock_irqrestore(&acm->read_lock, flags);
391                 goto urbs;
392         }
393         buf = list_entry(acm->filled_read_bufs.next,
394                          struct acm_rb, list);
395         list_del(&buf->list);
396         spin_unlock_irqrestore(&acm->read_lock, flags);
397
398         dbg("acm_rx_tasklet: procesing buf 0x%p, size = %d", buf, buf->size);
399
400         tty_buffer_request_room(tty, buf->size);
401         spin_lock_irqsave(&acm->throttle_lock, flags);
402         throttled = acm->throttle;
403         spin_unlock_irqrestore(&acm->throttle_lock, flags);
404         if (!throttled)
405                 tty_insert_flip_string(tty, buf->base, buf->size);
406         tty_flip_buffer_push(tty);
407
408         if (throttled) {
409                 dbg("Throttling noticed");
410                 spin_lock_irqsave(&acm->read_lock, flags);
411                 list_add(&buf->list, &acm->filled_read_bufs);
412                 spin_unlock_irqrestore(&acm->read_lock, flags);
413                 return;
414         }
415
416         spin_lock_irqsave(&acm->read_lock, flags);
417         list_add(&buf->list, &acm->spare_read_bufs);
418         spin_unlock_irqrestore(&acm->read_lock, flags);
419         goto next_buffer;
420
421 urbs:
422         while (!list_empty(&acm->spare_read_bufs)) {
423                 spin_lock_irqsave(&acm->read_lock, flags);
424                 if (list_empty(&acm->spare_read_urbs)) {
425                         acm->processing = 0;
426                         spin_unlock_irqrestore(&acm->read_lock, flags);
427                         return;
428                 }
429                 rcv = list_entry(acm->spare_read_urbs.next,
430                                  struct acm_ru, list);
431                 list_del(&rcv->list);
432                 spin_unlock_irqrestore(&acm->read_lock, flags);
433
434                 buf = list_entry(acm->spare_read_bufs.next,
435                                  struct acm_rb, list);
436                 list_del(&buf->list);
437
438                 rcv->buffer = buf;
439
440                 usb_fill_bulk_urb(rcv->urb, acm->dev,
441                                   acm->rx_endpoint,
442                                   buf->base,
443                                   acm->readsize,
444                                   acm_read_bulk, rcv);
445                 rcv->urb->transfer_dma = buf->dma;
446                 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
447
448                 /* This shouldn't kill the driver as unsuccessful URBs are returned to the
449                    free-urbs-pool and resubmited ASAP */
450                 spin_lock_irqsave(&acm->read_lock, flags);
451                 if (acm->susp_count || usb_submit_urb(rcv->urb, GFP_ATOMIC) < 0) {
452                         list_add(&buf->list, &acm->spare_read_bufs);
453                         list_add(&rcv->list, &acm->spare_read_urbs);
454                         acm->processing = 0;
455                         spin_unlock_irqrestore(&acm->read_lock, flags);
456                         return;
457                 } else {
458                         spin_unlock_irqrestore(&acm->read_lock, flags);
459                         dbg("acm_rx_tasklet: sending urb 0x%p, rcv 0x%p, buf 0x%p", rcv->urb, rcv, buf);
460                 }
461         }
462         spin_lock_irqsave(&acm->read_lock, flags);
463         acm->processing = 0;
464         spin_unlock_irqrestore(&acm->read_lock, flags);
465 }
466
467 /* data interface wrote those outgoing bytes */
468 static void acm_write_bulk(struct urb *urb)
469 {
470         struct acm *acm;
471         struct acm_wb *wb = urb->context;
472
473         dbg("Entering acm_write_bulk with status %d", urb->status);
474
475         acm = wb->instance;
476         acm_write_done(acm, wb);
477         if (ACM_READY(acm))
478                 schedule_work(&acm->work);
479 }
480
481 static void acm_softint(struct work_struct *work)
482 {
483         struct acm *acm = container_of(work, struct acm, work);
484         dbg("Entering acm_softint.");
485         
486         if (!ACM_READY(acm))
487                 return;
488         tty_wakeup(acm->tty);
489 }
490
491 static void acm_waker(struct work_struct *waker)
492 {
493         struct acm *acm = container_of(waker, struct acm, waker);
494         int rv;
495
496         rv = usb_autopm_get_interface(acm->control);
497         if (rv < 0) {
498                 err("Autopm failure in %s", __func__);
499                 return;
500         }
501         if (acm->delayed_wb) {
502                 acm_start_wb(acm, acm->delayed_wb);
503                 acm->delayed_wb = NULL;
504         }
505         usb_autopm_put_interface(acm->control);
506 }
507
508 /*
509  * TTY handlers
510  */
511
512 static int acm_tty_open(struct tty_struct *tty, struct file *filp)
513 {
514         struct acm *acm;
515         int rv = -EINVAL;
516         int i;
517         dbg("Entering acm_tty_open.");
518
519         mutex_lock(&open_mutex);
520
521         acm = acm_table[tty->index];
522         if (!acm || !acm->dev)
523                 goto err_out;
524         else
525                 rv = 0;
526
527         set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
528         tty->driver_data = acm;
529         acm->tty = tty;
530
531         /* force low_latency on so that our tty_push actually forces the data through,
532            otherwise it is scheduled, and with high data rates data can get lost. */
533         tty->low_latency = 1;
534
535         if (usb_autopm_get_interface(acm->control) < 0)
536                 goto early_bail;
537         else
538                 acm->control->needs_remote_wakeup = 1;
539
540         mutex_lock(&acm->mutex);
541         if (acm->used++) {
542                 usb_autopm_put_interface(acm->control);
543                 goto done;
544         }
545
546
547         acm->ctrlurb->dev = acm->dev;
548         if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
549                 dbg("usb_submit_urb(ctrl irq) failed");
550                 goto bail_out;
551         }
552
553         if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS) &&
554             (acm->ctrl_caps & USB_CDC_CAP_LINE))
555                 goto full_bailout;
556         usb_autopm_put_interface(acm->control);
557
558         INIT_LIST_HEAD(&acm->spare_read_urbs);
559         INIT_LIST_HEAD(&acm->spare_read_bufs);
560         INIT_LIST_HEAD(&acm->filled_read_bufs);
561         for (i = 0; i < acm->rx_buflimit; i++) {
562                 list_add(&(acm->ru[i].list), &acm->spare_read_urbs);
563         }
564         for (i = 0; i < acm->rx_buflimit; i++) {
565                 list_add(&(acm->rb[i].list), &acm->spare_read_bufs);
566         }
567
568         acm->throttle = 0;
569
570         tasklet_schedule(&acm->urb_task);
571
572 done:
573 err_out:
574         mutex_unlock(&acm->mutex);
575         mutex_unlock(&open_mutex);
576         return rv;
577
578 full_bailout:
579         usb_kill_urb(acm->ctrlurb);
580 bail_out:
581         usb_autopm_put_interface(acm->control);
582         acm->used--;
583         mutex_unlock(&acm->mutex);
584 early_bail:
585         mutex_unlock(&open_mutex);
586         return -EIO;
587 }
588
589 static void acm_tty_unregister(struct acm *acm)
590 {
591         int i,nr;
592
593         nr = acm->rx_buflimit;
594         tty_unregister_device(acm_tty_driver, acm->minor);
595         usb_put_intf(acm->control);
596         acm_table[acm->minor] = NULL;
597         usb_free_urb(acm->ctrlurb);
598         for (i = 0; i < ACM_NW; i++)
599                 usb_free_urb(acm->wb[i].urb);
600         for (i = 0; i < nr; i++)
601                 usb_free_urb(acm->ru[i].urb);
602         kfree(acm->country_codes);
603         kfree(acm);
604 }
605
606 static void acm_tty_close(struct tty_struct *tty, struct file *filp)
607 {
608         struct acm *acm = tty->driver_data;
609         int i,nr;
610
611         if (!acm || !acm->used)
612                 return;
613
614         nr = acm->rx_buflimit;
615         mutex_lock(&open_mutex);
616         if (!--acm->used) {
617                 if (acm->dev) {
618                         usb_autopm_get_interface(acm->control);
619                         acm_set_control(acm, acm->ctrlout = 0);
620                         usb_kill_urb(acm->ctrlurb);
621                         for (i = 0; i < ACM_NW; i++)
622                                 usb_kill_urb(acm->wb[i].urb);
623                         for (i = 0; i < nr; i++)
624                                 usb_kill_urb(acm->ru[i].urb);
625                         acm->control->needs_remote_wakeup = 0;
626                         usb_autopm_put_interface(acm->control);
627                 } else
628                         acm_tty_unregister(acm);
629         }
630         mutex_unlock(&open_mutex);
631 }
632
633 static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
634 {
635         struct acm *acm = tty->driver_data;
636         int stat;
637         unsigned long flags;
638         int wbn;
639         struct acm_wb *wb;
640
641         dbg("Entering acm_tty_write to write %d bytes,", count);
642
643         if (!ACM_READY(acm))
644                 return -EINVAL;
645         if (!count)
646                 return 0;
647
648         spin_lock_irqsave(&acm->write_lock, flags);
649         if ((wbn = acm_wb_alloc(acm)) < 0) {
650                 spin_unlock_irqrestore(&acm->write_lock, flags);
651                 return 0;
652         }
653         wb = &acm->wb[wbn];
654
655         count = (count > acm->writesize) ? acm->writesize : count;
656         dbg("Get %d bytes...", count);
657         memcpy(wb->buf, buf, count);
658         wb->len = count;
659         spin_unlock_irqrestore(&acm->write_lock, flags);
660
661         if ((stat = acm_write_start(acm, wbn)) < 0)
662                 return stat;
663         return count;
664 }
665
666 static int acm_tty_write_room(struct tty_struct *tty)
667 {
668         struct acm *acm = tty->driver_data;
669         if (!ACM_READY(acm))
670                 return -EINVAL;
671         /*
672          * Do not let the line discipline to know that we have a reserve,
673          * or it might get too enthusiastic.
674          */
675         return acm_wb_is_avail(acm) ? acm->writesize : 0;
676 }
677
678 static int acm_tty_chars_in_buffer(struct tty_struct *tty)
679 {
680         struct acm *acm = tty->driver_data;
681         if (!ACM_READY(acm))
682                 return -EINVAL;
683         /*
684          * This is inaccurate (overcounts), but it works.
685          */
686         return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
687 }
688
689 static void acm_tty_throttle(struct tty_struct *tty)
690 {
691         struct acm *acm = tty->driver_data;
692         if (!ACM_READY(acm))
693                 return;
694         spin_lock_bh(&acm->throttle_lock);
695         acm->throttle = 1;
696         spin_unlock_bh(&acm->throttle_lock);
697 }
698
699 static void acm_tty_unthrottle(struct tty_struct *tty)
700 {
701         struct acm *acm = tty->driver_data;
702         if (!ACM_READY(acm))
703                 return;
704         spin_lock_bh(&acm->throttle_lock);
705         acm->throttle = 0;
706         spin_unlock_bh(&acm->throttle_lock);
707         tasklet_schedule(&acm->urb_task);
708 }
709
710 static int acm_tty_break_ctl(struct tty_struct *tty, int state)
711 {
712         struct acm *acm = tty->driver_data;
713         int retval;
714         if (!ACM_READY(acm))
715                 return -EINVAL;
716         retval = acm_send_break(acm, state ? 0xffff : 0);
717         if (retval < 0)
718                 dbg("send break failed");
719         return retval;
720 }
721
722 static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file)
723 {
724         struct acm *acm = tty->driver_data;
725
726         if (!ACM_READY(acm))
727                 return -EINVAL;
728
729         return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
730                (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
731                (acm->ctrlin  & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
732                (acm->ctrlin  & ACM_CTRL_RI  ? TIOCM_RI  : 0) |
733                (acm->ctrlin  & ACM_CTRL_DCD ? TIOCM_CD  : 0) |
734                TIOCM_CTS;
735 }
736
737 static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file,
738                             unsigned int set, unsigned int clear)
739 {
740         struct acm *acm = tty->driver_data;
741         unsigned int newctrl;
742
743         if (!ACM_READY(acm))
744                 return -EINVAL;
745
746         newctrl = acm->ctrlout;
747         set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
748         clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
749
750         newctrl = (newctrl & ~clear) | set;
751
752         if (acm->ctrlout == newctrl)
753                 return 0;
754         return acm_set_control(acm, acm->ctrlout = newctrl);
755 }
756
757 static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
758 {
759         struct acm *acm = tty->driver_data;
760
761         if (!ACM_READY(acm))
762                 return -EINVAL;
763
764         return -ENOIOCTLCMD;
765 }
766
767 static const __u32 acm_tty_speed[] = {
768         0, 50, 75, 110, 134, 150, 200, 300, 600,
769         1200, 1800, 2400, 4800, 9600, 19200, 38400,
770         57600, 115200, 230400, 460800, 500000, 576000,
771         921600, 1000000, 1152000, 1500000, 2000000,
772         2500000, 3000000, 3500000, 4000000
773 };
774
775 static const __u8 acm_tty_size[] = {
776         5, 6, 7, 8
777 };
778
779 static void acm_tty_set_termios(struct tty_struct *tty, struct ktermios *termios_old)
780 {
781         struct acm *acm = tty->driver_data;
782         struct ktermios *termios = tty->termios;
783         struct usb_cdc_line_coding newline;
784         int newctrl = acm->ctrlout;
785
786         if (!ACM_READY(acm))
787                 return;
788
789         newline.dwDTERate = cpu_to_le32p(acm_tty_speed +
790                 (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0));
791         newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
792         newline.bParityType = termios->c_cflag & PARENB ?
793                 (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
794         newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
795
796         acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
797
798         if (!newline.dwDTERate) {
799                 newline.dwDTERate = acm->line.dwDTERate;
800                 newctrl &= ~ACM_CTRL_DTR;
801         } else  newctrl |=  ACM_CTRL_DTR;
802
803         if (newctrl != acm->ctrlout)
804                 acm_set_control(acm, acm->ctrlout = newctrl);
805
806         if (memcmp(&acm->line, &newline, sizeof newline)) {
807                 memcpy(&acm->line, &newline, sizeof newline);
808                 dbg("set line: %d %d %d %d", le32_to_cpu(newline.dwDTERate),
809                         newline.bCharFormat, newline.bParityType,
810                         newline.bDataBits);
811                 acm_set_line(acm, &acm->line);
812         }
813 }
814
815 /*
816  * USB probe and disconnect routines.
817  */
818
819 /* Little helpers: write/read buffers free */
820 static void acm_write_buffers_free(struct acm *acm)
821 {
822         int i;
823         struct acm_wb *wb;
824
825         for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
826                 usb_buffer_free(acm->dev, acm->writesize, wb->buf, wb->dmah);
827         }
828 }
829
830 static void acm_read_buffers_free(struct acm *acm)
831 {
832         struct usb_device *usb_dev = interface_to_usbdev(acm->control);
833         int i, n = acm->rx_buflimit;
834
835         for (i = 0; i < n; i++)
836                 usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma);
837 }
838
839 /* Little helper: write buffers allocate */
840 static int acm_write_buffers_alloc(struct acm *acm)
841 {
842         int i;
843         struct acm_wb *wb;
844
845         for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
846                 wb->buf = usb_buffer_alloc(acm->dev, acm->writesize, GFP_KERNEL,
847                     &wb->dmah);
848                 if (!wb->buf) {
849                         while (i != 0) {
850                                 --i;
851                                 --wb;
852                                 usb_buffer_free(acm->dev, acm->writesize,
853                                     wb->buf, wb->dmah);
854                         }
855                         return -ENOMEM;
856                 }
857         }
858         return 0;
859 }
860
861 static int acm_probe (struct usb_interface *intf,
862                       const struct usb_device_id *id)
863 {
864         struct usb_cdc_union_desc *union_header = NULL;
865         struct usb_cdc_country_functional_desc *cfd = NULL;
866         unsigned char *buffer = intf->altsetting->extra;
867         int buflen = intf->altsetting->extralen;
868         struct usb_interface *control_interface;
869         struct usb_interface *data_interface;
870         struct usb_endpoint_descriptor *epctrl;
871         struct usb_endpoint_descriptor *epread;
872         struct usb_endpoint_descriptor *epwrite;
873         struct usb_device *usb_dev = interface_to_usbdev(intf);
874         struct acm *acm;
875         int minor;
876         int ctrlsize,readsize;
877         u8 *buf;
878         u8 ac_management_function = 0;
879         u8 call_management_function = 0;
880         int call_interface_num = -1;
881         int data_interface_num;
882         unsigned long quirks;
883         int num_rx_buf;
884         int i;
885
886         /* normal quirks */
887         quirks = (unsigned long)id->driver_info;
888         num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
889
890         /* handle quirks deadly to normal probing*/
891         if (quirks == NO_UNION_NORMAL) {
892                 data_interface = usb_ifnum_to_if(usb_dev, 1);
893                 control_interface = usb_ifnum_to_if(usb_dev, 0);
894                 goto skip_normal_probe;
895         }
896         
897         /* normal probing*/
898         if (!buffer) {
899                 err("Weird descriptor references\n");
900                 return -EINVAL;
901         }
902
903         if (!buflen) {
904                 if (intf->cur_altsetting->endpoint->extralen && intf->cur_altsetting->endpoint->extra) {
905                         dev_dbg(&intf->dev,"Seeking extra descriptors on endpoint\n");
906                         buflen = intf->cur_altsetting->endpoint->extralen;
907                         buffer = intf->cur_altsetting->endpoint->extra;
908                 } else {
909                         err("Zero length descriptor references\n");
910                         return -EINVAL;
911                 }
912         }
913
914         while (buflen > 0) {
915                 if (buffer [1] != USB_DT_CS_INTERFACE) {
916                         err("skipping garbage\n");
917                         goto next_desc;
918                 }
919
920                 switch (buffer [2]) {
921                         case USB_CDC_UNION_TYPE: /* we've found it */
922                                 if (union_header) {
923                                         err("More than one union descriptor, skipping ...");
924                                         goto next_desc;
925                                 }
926                                 union_header = (struct usb_cdc_union_desc *)
927                                                         buffer;
928                                 break;
929                         case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
930                                 cfd = (struct usb_cdc_country_functional_desc *)buffer;
931                                 break;
932                         case USB_CDC_HEADER_TYPE: /* maybe check version */ 
933                                 break; /* for now we ignore it */ 
934                         case USB_CDC_ACM_TYPE:
935                                 ac_management_function = buffer[3];
936                                 break;
937                         case USB_CDC_CALL_MANAGEMENT_TYPE:
938                                 call_management_function = buffer[3];
939                                 call_interface_num = buffer[4];
940                                 if ((call_management_function & 3) != 3)
941                                         err("This device cannot do calls on its own. It is no modem.");
942                                 break;
943                         default:
944                                 /* there are LOTS more CDC descriptors that
945                                  * could legitimately be found here.
946                                  */
947                                 dev_dbg(&intf->dev, "Ignoring descriptor: "
948                                                 "type %02x, length %d\n",
949                                                 buffer[2], buffer[0]);
950                                 break;
951                         }
952 next_desc:
953                 buflen -= buffer[0];
954                 buffer += buffer[0];
955         }
956
957         if (!union_header) {
958                 if (call_interface_num > 0) {
959                         dev_dbg(&intf->dev,"No union descriptor, using call management descriptor\n");
960                         data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
961                         control_interface = intf;
962                 } else {
963                         dev_dbg(&intf->dev,"No union descriptor, giving up\n");
964                         return -ENODEV;
965                 }
966         } else {
967                 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
968                 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
969                 if (!control_interface || !data_interface) {
970                         dev_dbg(&intf->dev,"no interfaces\n");
971                         return -ENODEV;
972                 }
973         }
974         
975         if (data_interface_num != call_interface_num)
976                 dev_dbg(&intf->dev,"Separate call control interface. That is not fully supported.\n");
977
978 skip_normal_probe:
979
980         /*workaround for switched interfaces */
981         if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) {
982                 if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) {
983                         struct usb_interface *t;
984                         dev_dbg(&intf->dev,"Your device has switched interfaces.\n");
985
986                         t = control_interface;
987                         control_interface = data_interface;
988                         data_interface = t;
989                 } else {
990                         return -EINVAL;
991                 }
992         }
993
994         /* Accept probe requests only for the control interface */
995         if (intf != control_interface)
996                 return -ENODEV;
997         
998         if (usb_interface_claimed(data_interface)) { /* valid in this context */
999                 dev_dbg(&intf->dev,"The data interface isn't available\n");
1000                 return -EBUSY;
1001         }
1002
1003
1004         if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
1005                 return -EINVAL;
1006
1007         epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
1008         epread = &data_interface->cur_altsetting->endpoint[0].desc;
1009         epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
1010
1011
1012         /* workaround for switched endpoints */
1013         if (!usb_endpoint_dir_in(epread)) {
1014                 /* descriptors are swapped */
1015                 struct usb_endpoint_descriptor *t;
1016                 dev_dbg(&intf->dev,"The data interface has switched endpoints\n");
1017                 
1018                 t = epread;
1019                 epread = epwrite;
1020                 epwrite = t;
1021         }
1022         dbg("interfaces are valid");
1023         for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
1024
1025         if (minor == ACM_TTY_MINORS) {
1026                 err("no more free acm devices");
1027                 return -ENODEV;
1028         }
1029
1030         if (!(acm = kzalloc(sizeof(struct acm), GFP_KERNEL))) {
1031                 dev_dbg(&intf->dev, "out of memory (acm kzalloc)\n");
1032                 goto alloc_fail;
1033         }
1034
1035         ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize);
1036         readsize = le16_to_cpu(epread->wMaxPacketSize)* ( quirks == SINGLE_RX_URB ? 1 : 2);
1037         acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize) * 20;
1038         acm->control = control_interface;
1039         acm->data = data_interface;
1040         acm->minor = minor;
1041         acm->dev = usb_dev;
1042         acm->ctrl_caps = ac_management_function;
1043         acm->ctrlsize = ctrlsize;
1044         acm->readsize = readsize;
1045         acm->rx_buflimit = num_rx_buf;
1046         acm->urb_task.func = acm_rx_tasklet;
1047         acm->urb_task.data = (unsigned long) acm;
1048         INIT_WORK(&acm->work, acm_softint);
1049         INIT_WORK(&acm->waker, acm_waker);
1050         spin_lock_init(&acm->throttle_lock);
1051         spin_lock_init(&acm->write_lock);
1052         spin_lock_init(&acm->read_lock);
1053         mutex_init(&acm->mutex);
1054         acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
1055
1056         buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
1057         if (!buf) {
1058                 dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)\n");
1059                 goto alloc_fail2;
1060         }
1061         acm->ctrl_buffer = buf;
1062
1063         if (acm_write_buffers_alloc(acm) < 0) {
1064                 dev_dbg(&intf->dev, "out of memory (write buffer alloc)\n");
1065                 goto alloc_fail4;
1066         }
1067
1068         acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
1069         if (!acm->ctrlurb) {
1070                 dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
1071                 goto alloc_fail5;
1072         }
1073         for (i = 0; i < num_rx_buf; i++) {
1074                 struct acm_ru *rcv = &(acm->ru[i]);
1075
1076                 if (!(rcv->urb = usb_alloc_urb(0, GFP_KERNEL))) {
1077                         dev_dbg(&intf->dev, "out of memory (read urbs usb_alloc_urb)\n");
1078                         goto alloc_fail7;
1079                 }
1080
1081                 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1082                 rcv->instance = acm;
1083         }
1084         for (i = 0; i < num_rx_buf; i++) {
1085                 struct acm_rb *rb = &(acm->rb[i]);
1086
1087                 rb->base = usb_buffer_alloc(acm->dev, readsize,
1088                                 GFP_KERNEL, &rb->dma);
1089                 if (!rb->base) {
1090                         dev_dbg(&intf->dev, "out of memory (read bufs usb_buffer_alloc)\n");
1091                         goto alloc_fail7;
1092                 }
1093         }
1094         for(i = 0; i < ACM_NW; i++)
1095         {
1096                 struct acm_wb *snd = &(acm->wb[i]);
1097
1098                 if (!(snd->urb = usb_alloc_urb(0, GFP_KERNEL))) {
1099                         dev_dbg(&intf->dev, "out of memory (write urbs usb_alloc_urb)");
1100                         goto alloc_fail7;
1101                 }
1102
1103                 usb_fill_bulk_urb(snd->urb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
1104                                 NULL, acm->writesize, acm_write_bulk, snd);
1105                 snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1106                 snd->instance = acm;
1107         }
1108
1109         usb_set_intfdata (intf, acm);
1110
1111         i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
1112         if (i < 0)
1113                 goto alloc_fail8;
1114
1115         if (cfd) { /* export the country data */
1116                 acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
1117                 if (!acm->country_codes)
1118                         goto skip_countries;
1119                 acm->country_code_size = cfd->bLength - 4;
1120                 memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0, cfd->bLength - 4);
1121                 acm->country_rel_date = cfd->iCountryCodeRelDate;
1122
1123                 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
1124                 if (i < 0) {
1125                         kfree(acm->country_codes);
1126                         goto skip_countries;
1127                 }
1128
1129                 i = device_create_file(&intf->dev, &dev_attr_iCountryCodeRelDate);
1130                 if (i < 0) {
1131                         kfree(acm->country_codes);
1132                         goto skip_countries;
1133                 }
1134         }
1135
1136 skip_countries:
1137         usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
1138                          acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval);
1139         acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1140         acm->ctrlurb->transfer_dma = acm->ctrl_dma;
1141
1142         dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
1143
1144         acm_set_control(acm, acm->ctrlout);
1145
1146         acm->line.dwDTERate = cpu_to_le32(9600);
1147         acm->line.bDataBits = 8;
1148         acm_set_line(acm, &acm->line);
1149
1150         usb_driver_claim_interface(&acm_driver, data_interface, acm);
1151         usb_set_intfdata(data_interface, acm);
1152
1153         usb_get_intf(control_interface);
1154         tty_register_device(acm_tty_driver, minor, &control_interface->dev);
1155
1156         acm_table[minor] = acm;
1157
1158         return 0;
1159 alloc_fail8:
1160         for (i = 0; i < ACM_NW; i++)
1161                 usb_free_urb(acm->wb[i].urb);
1162 alloc_fail7:
1163         acm_read_buffers_free(acm);
1164         for (i = 0; i < num_rx_buf; i++)
1165                 usb_free_urb(acm->ru[i].urb);
1166         usb_free_urb(acm->ctrlurb);
1167 alloc_fail5:
1168         acm_write_buffers_free(acm);
1169 alloc_fail4:
1170         usb_buffer_free(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1171 alloc_fail2:
1172         kfree(acm);
1173 alloc_fail:
1174         return -ENOMEM;
1175 }
1176
1177 static void stop_data_traffic(struct acm *acm)
1178 {
1179         int i;
1180         dbg("Entering stop_data_traffic");
1181
1182         tasklet_disable(&acm->urb_task);
1183
1184         usb_kill_urb(acm->ctrlurb);
1185         for(i = 0; i < ACM_NW; i++)
1186                 usb_kill_urb(acm->wb[i].urb);
1187         for (i = 0; i < acm->rx_buflimit; i++)
1188                 usb_kill_urb(acm->ru[i].urb);
1189
1190         tasklet_enable(&acm->urb_task);
1191
1192         cancel_work_sync(&acm->work);
1193         cancel_work_sync(&acm->waker);
1194 }
1195
1196 static void acm_disconnect(struct usb_interface *intf)
1197 {
1198         struct acm *acm = usb_get_intfdata(intf);
1199         struct usb_device *usb_dev = interface_to_usbdev(intf);
1200
1201         /* sibling interface is already cleaning up */
1202         if (!acm)
1203                 return;
1204
1205         mutex_lock(&open_mutex);
1206         if (acm->country_codes){
1207                 device_remove_file(&acm->control->dev,
1208                                 &dev_attr_wCountryCodes);
1209                 device_remove_file(&acm->control->dev,
1210                                 &dev_attr_iCountryCodeRelDate);
1211         }
1212         device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
1213         acm->dev = NULL;
1214         usb_set_intfdata(acm->control, NULL);
1215         usb_set_intfdata(acm->data, NULL);
1216
1217         stop_data_traffic(acm);
1218
1219         acm_write_buffers_free(acm);
1220         usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1221         acm_read_buffers_free(acm);
1222
1223         usb_driver_release_interface(&acm_driver, intf == acm->control ?
1224                                         acm->data : acm->control);
1225
1226         if (!acm->used) {
1227                 acm_tty_unregister(acm);
1228                 mutex_unlock(&open_mutex);
1229                 return;
1230         }
1231
1232         mutex_unlock(&open_mutex);
1233
1234         if (acm->tty)
1235                 tty_hangup(acm->tty);
1236 }
1237
1238 #ifdef CONFIG_PM
1239 static int acm_suspend(struct usb_interface *intf, pm_message_t message)
1240 {
1241         struct acm *acm = usb_get_intfdata(intf);
1242         int cnt;
1243
1244         if (acm->dev->auto_pm) {
1245                 int b;
1246
1247                 spin_lock_irq(&acm->read_lock);
1248                 spin_lock(&acm->write_lock);
1249                 b = acm->processing + acm->transmitting;
1250                 spin_unlock(&acm->write_lock);
1251                 spin_unlock_irq(&acm->read_lock);
1252                 if (b)
1253                         return -EBUSY;
1254         }
1255
1256         spin_lock_irq(&acm->read_lock);
1257         spin_lock(&acm->write_lock);
1258         cnt = acm->susp_count++;
1259         spin_unlock(&acm->write_lock);
1260         spin_unlock_irq(&acm->read_lock);
1261
1262         if (cnt)
1263                 return 0;
1264         /*
1265         we treat opened interfaces differently,
1266         we must guard against open
1267         */
1268         mutex_lock(&acm->mutex);
1269
1270         if (acm->used)
1271                 stop_data_traffic(acm);
1272
1273         mutex_unlock(&acm->mutex);
1274         return 0;
1275 }
1276
1277 static int acm_resume(struct usb_interface *intf)
1278 {
1279         struct acm *acm = usb_get_intfdata(intf);
1280         int rv = 0;
1281         int cnt;
1282
1283         spin_lock_irq(&acm->read_lock);
1284         acm->susp_count -= 1;
1285         cnt = acm->susp_count;
1286         spin_unlock_irq(&acm->read_lock);
1287
1288         if (cnt)
1289                 return 0;
1290
1291         mutex_lock(&acm->mutex);
1292         if (acm->used) {
1293                 rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO);
1294                 if (rv < 0)
1295                         goto err_out;
1296
1297                 tasklet_schedule(&acm->urb_task);
1298         }
1299
1300 err_out:
1301         mutex_unlock(&acm->mutex);
1302         return rv;
1303 }
1304
1305 #endif /* CONFIG_PM */
1306 /*
1307  * USB driver structure.
1308  */
1309
1310 static struct usb_device_id acm_ids[] = {
1311         /* quirky and broken devices */
1312         { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
1313         .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1314         },
1315         { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
1316         .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1317         },
1318         { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
1319         .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1320         },
1321         { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
1322         .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1323         },
1324         { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
1325         .driver_info = SINGLE_RX_URB, /* firmware bug */
1326         },
1327         { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
1328         .driver_info = SINGLE_RX_URB, /* firmware bug */
1329         },
1330         { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
1331         .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1332         },
1333         { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
1334         .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1335         },
1336
1337         /* control interfaces with various AT-command sets */
1338         { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1339                 USB_CDC_ACM_PROTO_AT_V25TER) },
1340         { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1341                 USB_CDC_ACM_PROTO_AT_PCCA101) },
1342         { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1343                 USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
1344         { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1345                 USB_CDC_ACM_PROTO_AT_GSM) },
1346         { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1347                 USB_CDC_ACM_PROTO_AT_3G ) },
1348         { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1349                 USB_CDC_ACM_PROTO_AT_CDMA) },
1350
1351         /* NOTE:  COMM/ACM/0xff is likely MSFT RNDIS ... NOT a modem!! */
1352         { }
1353 };
1354
1355 MODULE_DEVICE_TABLE (usb, acm_ids);
1356
1357 static struct usb_driver acm_driver = {
1358         .name =         "cdc_acm",
1359         .probe =        acm_probe,
1360         .disconnect =   acm_disconnect,
1361 #ifdef CONFIG_PM
1362         .suspend =      acm_suspend,
1363         .resume =       acm_resume,
1364 #endif
1365         .id_table =     acm_ids,
1366 #ifdef CONFIG_PM
1367         .supports_autosuspend = 1,
1368 #endif
1369 };
1370
1371 /*
1372  * TTY driver structures.
1373  */
1374
1375 static const struct tty_operations acm_ops = {
1376         .open =                 acm_tty_open,
1377         .close =                acm_tty_close,
1378         .write =                acm_tty_write,
1379         .write_room =           acm_tty_write_room,
1380         .ioctl =                acm_tty_ioctl,
1381         .throttle =             acm_tty_throttle,
1382         .unthrottle =           acm_tty_unthrottle,
1383         .chars_in_buffer =      acm_tty_chars_in_buffer,
1384         .break_ctl =            acm_tty_break_ctl,
1385         .set_termios =          acm_tty_set_termios,
1386         .tiocmget =             acm_tty_tiocmget,
1387         .tiocmset =             acm_tty_tiocmset,
1388 };
1389
1390 /*
1391  * Init / exit.
1392  */
1393
1394 static int __init acm_init(void)
1395 {
1396         int retval;
1397         acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
1398         if (!acm_tty_driver)
1399                 return -ENOMEM;
1400         acm_tty_driver->owner = THIS_MODULE,
1401         acm_tty_driver->driver_name = "acm",
1402         acm_tty_driver->name = "ttyACM",
1403         acm_tty_driver->major = ACM_TTY_MAJOR,
1404         acm_tty_driver->minor_start = 0,
1405         acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
1406         acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
1407         acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1408         acm_tty_driver->init_termios = tty_std_termios;
1409         acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1410         tty_set_operations(acm_tty_driver, &acm_ops);
1411
1412         retval = tty_register_driver(acm_tty_driver);
1413         if (retval) {
1414                 put_tty_driver(acm_tty_driver);
1415                 return retval;
1416         }
1417
1418         retval = usb_register(&acm_driver);
1419         if (retval) {
1420                 tty_unregister_driver(acm_tty_driver);
1421                 put_tty_driver(acm_tty_driver);
1422                 return retval;
1423         }
1424
1425         info(DRIVER_VERSION ":" DRIVER_DESC);
1426
1427         return 0;
1428 }
1429
1430 static void __exit acm_exit(void)
1431 {
1432         usb_deregister(&acm_driver);
1433         tty_unregister_driver(acm_tty_driver);
1434         put_tty_driver(acm_tty_driver);
1435 }
1436
1437 module_init(acm_init);
1438 module_exit(acm_exit);
1439
1440 MODULE_AUTHOR( DRIVER_AUTHOR );
1441 MODULE_DESCRIPTION( DRIVER_DESC );
1442 MODULE_LICENSE("GPL");
1443