unknown changes from android-tegra-nv-3.4
[linux-3.10.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@ucw.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  * Copyright (c) 2011 Johan Hovold      <jhovold@gmail.com>
11  *
12  * USB Abstract Control Model driver for USB modems and ISDN adapters
13  *
14  * Sponsored by SuSE
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29  */
30
31 #undef DEBUG
32 #undef VERBOSE_DEBUG
33
34 #include <linux/kernel.h>
35 #include <linux/errno.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
38 #include <linux/tty.h>
39 #include <linux/serial.h>
40 #include <linux/tty_driver.h>
41 #include <linux/tty_flip.h>
42 #include <linux/module.h>
43 #include <linux/mutex.h>
44 #include <linux/uaccess.h>
45 #include <linux/usb.h>
46 #include <linux/usb/cdc.h>
47 #include <asm/byteorder.h>
48 #include <asm/unaligned.h>
49 #include <linux/list.h>
50
51 #include "cdc-acm.h"
52
53
54 #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek, Johan Hovold"
55 #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
56
57 static struct usb_driver acm_driver;
58 static struct tty_driver *acm_tty_driver;
59 static struct acm *acm_table[ACM_TTY_MINORS];
60
61 static DEFINE_MUTEX(acm_table_lock);
62
63 /*
64  * acm_table accessors
65  */
66
67 /*
68  * Look up an ACM structure by index. If found and not disconnected, increment
69  * its refcount and return it with its mutex held.
70  */
71 static struct acm *acm_get_by_index(unsigned index)
72 {
73         struct acm *acm;
74
75         mutex_lock(&acm_table_lock);
76         acm = acm_table[index];
77         if (acm) {
78                 mutex_lock(&acm->mutex);
79                 if (acm->disconnected) {
80                         mutex_unlock(&acm->mutex);
81                         acm = NULL;
82                 } else {
83                         tty_port_get(&acm->port);
84                         mutex_unlock(&acm->mutex);
85                 }
86         }
87         mutex_unlock(&acm_table_lock);
88         return acm;
89 }
90
91 /*
92  * Try to find an available minor number and if found, associate it with 'acm'.
93  */
94 static int acm_alloc_minor(struct acm *acm)
95 {
96         int minor;
97
98         mutex_lock(&acm_table_lock);
99         for (minor = 0; minor < ACM_TTY_MINORS; minor++) {
100                 if (!acm_table[minor]) {
101                         acm_table[minor] = acm;
102                         break;
103                 }
104         }
105         mutex_unlock(&acm_table_lock);
106
107         return minor;
108 }
109
110 /* Release the minor number associated with 'acm'.  */
111 static void acm_release_minor(struct acm *acm)
112 {
113         mutex_lock(&acm_table_lock);
114         acm_table[acm->minor] = NULL;
115         mutex_unlock(&acm_table_lock);
116 }
117
118 /*
119  * Functions for ACM control messages.
120  */
121
122 static int acm_ctrl_msg(struct acm *acm, int request, int value,
123                                                         void *buf, int len)
124 {
125         int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
126                 request, USB_RT_ACM, value,
127                 acm->control->altsetting[0].desc.bInterfaceNumber,
128                 buf, len, 5000);
129         dev_dbg(&acm->control->dev,
130                         "%s - rq 0x%02x, val %#x, len %#x, result %d\n",
131                         __func__, request, value, len, retval);
132         return retval < 0 ? retval : 0;
133 }
134
135 /* devices aren't required to support these requests.
136  * the cdc acm descriptor tells whether they do...
137  */
138 #define acm_set_control(acm, control) \
139         acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0)
140 #define acm_set_line(acm, line) \
141         acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
142 #define acm_send_break(acm, ms) \
143         acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
144
145 /*
146  * Write buffer management.
147  * All of these assume proper locks taken by the caller.
148  */
149
150 static int acm_wb_alloc(struct acm *acm)
151 {
152         int i, wbn;
153         struct acm_wb *wb;
154
155         wbn = 0;
156         i = 0;
157         for (;;) {
158                 wb = &acm->wb[wbn];
159                 if (!wb->use) {
160                         wb->use = 1;
161                         return wbn;
162                 }
163                 wbn = (wbn + 1) % ACM_NW;
164                 if (++i >= ACM_NW)
165                         return -1;
166         }
167 }
168
169 static int acm_wb_is_avail(struct acm *acm)
170 {
171         int i, n;
172         unsigned long flags;
173
174         n = ACM_NW;
175         spin_lock_irqsave(&acm->write_lock, flags);
176         for (i = 0; i < ACM_NW; i++)
177                 n -= acm->wb[i].use;
178         spin_unlock_irqrestore(&acm->write_lock, flags);
179         return n;
180 }
181
182 /*
183  * Finish write. Caller must hold acm->write_lock
184  */
185 static void acm_write_done(struct acm *acm, struct acm_wb *wb)
186 {
187         wb->use = 0;
188         acm->transmitting--;
189         usb_autopm_put_interface_async(acm->control);
190 }
191
192 /*
193  * Poke write.
194  *
195  * the caller is responsible for locking
196  */
197
198 static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
199 {
200         int rc;
201
202         acm->transmitting++;
203
204         wb->urb->transfer_buffer = wb->buf;
205         wb->urb->transfer_dma = wb->dmah;
206         wb->urb->transfer_buffer_length = wb->len;
207         wb->urb->dev = acm->dev;
208         rc = usb_submit_urb(wb->urb, GFP_ATOMIC);
209         if (rc < 0) {
210                 dev_err(&acm->data->dev,
211                         "%s - usb_submit_urb(write bulk) failed: %d\n",
212                         __func__, rc);
213                 acm_write_done(acm, wb);
214         }
215         return rc;
216 }
217
218 static int acm_write_start(struct acm *acm, int wbn)
219 {
220         unsigned long flags;
221         struct acm_wb *wb = &acm->wb[wbn];
222         int rc;
223 #ifdef CONFIG_PM
224         struct urb *res;
225 #endif
226
227         spin_lock_irqsave(&acm->write_lock, flags);
228         if (!acm->dev) {
229                 wb->use = 0;
230                 spin_unlock_irqrestore(&acm->write_lock, flags);
231                 return -ENODEV;
232         }
233
234         dev_vdbg(&acm->data->dev, "%s - susp_count %d\n", __func__,
235                                                         acm->susp_count);
236         usb_autopm_get_interface_async(acm->control);
237         if (acm->susp_count) {
238 #ifdef CONFIG_PM
239                 acm->transmitting++;
240                 wb->urb->transfer_buffer = wb->buf;
241                 wb->urb->transfer_dma = wb->dmah;
242                 wb->urb->transfer_buffer_length = wb->len;
243                 wb->urb->dev = acm->dev;
244                 usb_anchor_urb(wb->urb, &acm->deferred);
245 #else
246                 if (!acm->delayed_wb)
247                         acm->delayed_wb = wb;
248                 else
249                         usb_autopm_put_interface_async(acm->control);
250 #endif
251                 spin_unlock_irqrestore(&acm->write_lock, flags);
252                 return 0;       /* A white lie */
253         }
254         usb_mark_last_busy(acm->dev);
255 #ifdef CONFIG_PM
256         while ((res = usb_get_from_anchor(&acm->deferred))) {
257                 /* decrement ref count*/
258                 usb_put_urb(res);
259                 rc = usb_submit_urb(res, GFP_ATOMIC);
260                 if (rc < 0) {
261                         dbg("usb_submit_urb(pending request) failed: %d", rc);
262                         usb_unanchor_urb(res);
263                         acm_write_done(acm, res->context);
264                 }
265         }
266 #endif
267         rc = acm_start_wb(acm, wb);
268         spin_unlock_irqrestore(&acm->write_lock, flags);
269
270         return rc;
271
272 }
273 /*
274  * attributes exported through sysfs
275  */
276 static ssize_t show_caps
277 (struct device *dev, struct device_attribute *attr, char *buf)
278 {
279         struct usb_interface *intf = to_usb_interface(dev);
280         struct acm *acm = usb_get_intfdata(intf);
281
282         return sprintf(buf, "%d", acm->ctrl_caps);
283 }
284 static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
285
286 static ssize_t show_country_codes
287 (struct device *dev, struct device_attribute *attr, char *buf)
288 {
289         struct usb_interface *intf = to_usb_interface(dev);
290         struct acm *acm = usb_get_intfdata(intf);
291
292         memcpy(buf, acm->country_codes, acm->country_code_size);
293         return acm->country_code_size;
294 }
295
296 static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
297
298 static ssize_t show_country_rel_date
299 (struct device *dev, struct device_attribute *attr, char *buf)
300 {
301         struct usb_interface *intf = to_usb_interface(dev);
302         struct acm *acm = usb_get_intfdata(intf);
303
304         return sprintf(buf, "%d", acm->country_rel_date);
305 }
306
307 static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
308 /*
309  * Interrupt handlers for various ACM device responses
310  */
311
312 /* control interface reports status changes with "interrupt" transfers */
313 static void acm_ctrl_irq(struct urb *urb)
314 {
315         struct acm *acm = urb->context;
316         struct usb_cdc_notification *dr = urb->transfer_buffer;
317         unsigned char *data;
318         int newctrl;
319         int retval;
320         int status = urb->status;
321
322         switch (status) {
323         case 0:
324                 /* success */
325                 break;
326         case -ECONNRESET:
327         case -ENOENT:
328         case -ESHUTDOWN:
329                 /* this urb is terminated, clean up */
330                 dev_dbg(&acm->control->dev,
331                                 "%s - urb shutting down with status: %d\n",
332                                 __func__, status);
333                 return;
334         default:
335                 dev_dbg(&acm->control->dev,
336                                 "%s - nonzero urb status received: %d\n",
337                                 __func__, status);
338                 goto exit;
339         }
340
341         usb_mark_last_busy(acm->dev);
342
343         data = (unsigned char *)(dr + 1);
344         switch (dr->bNotificationType) {
345         case USB_CDC_NOTIFY_NETWORK_CONNECTION:
346                 dev_dbg(&acm->control->dev, "%s - network connection: %d\n",
347                                                         __func__, dr->wValue);
348                 break;
349
350         case USB_CDC_NOTIFY_SERIAL_STATE:
351                 newctrl = get_unaligned_le16(data);
352
353                 if (!acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
354                         dev_dbg(&acm->control->dev, "%s - calling hangup\n",
355                                         __func__);
356                         tty_port_tty_hangup(&acm->port, false);
357                 }
358
359                 acm->ctrlin = newctrl;
360
361                 dev_dbg(&acm->control->dev,
362                         "%s - input control lines: dcd%c dsr%c break%c "
363                         "ring%c framing%c parity%c overrun%c\n",
364                         __func__,
365                         acm->ctrlin & ACM_CTRL_DCD ? '+' : '-',
366                         acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
367                         acm->ctrlin & ACM_CTRL_BRK ? '+' : '-',
368                         acm->ctrlin & ACM_CTRL_RI  ? '+' : '-',
369                         acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-',
370                         acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
371                         acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
372                         break;
373
374         default:
375                 dev_dbg(&acm->control->dev,
376                         "%s - unknown notification %d received: index %d "
377                         "len %d data0 %d data1 %d\n",
378                         __func__,
379                         dr->bNotificationType, dr->wIndex,
380                         dr->wLength, data[0], data[1]);
381                 break;
382         }
383 exit:
384         retval = usb_submit_urb(urb, GFP_ATOMIC);
385         if (retval)
386                 dev_err(&acm->control->dev, "%s - usb_submit_urb failed: %d\n",
387                                                         __func__, retval);
388 }
389
390 static int acm_submit_read_urb(struct acm *acm, int index, gfp_t mem_flags)
391 {
392         int res;
393
394         if (!test_and_clear_bit(index, &acm->read_urbs_free))
395                 return 0;
396
397         dev_vdbg(&acm->data->dev, "%s - urb %d\n", __func__, index);
398
399         res = usb_submit_urb(acm->read_urbs[index], mem_flags);
400         if (res) {
401                 if (res != -EPERM) {
402                         dev_err(&acm->data->dev,
403                                         "%s - usb_submit_urb failed: %d\n",
404                                         __func__, res);
405                 }
406                 set_bit(index, &acm->read_urbs_free);
407                 return res;
408         }
409
410         return 0;
411 }
412
413 static int acm_submit_read_urbs(struct acm *acm, gfp_t mem_flags)
414 {
415         int res;
416         int i;
417
418         for (i = 0; i < acm->rx_buflimit; ++i) {
419                 res = acm_submit_read_urb(acm, i, mem_flags);
420                 if (res)
421                         return res;
422         }
423
424         return 0;
425 }
426
427 static void acm_process_read_urb(struct acm *acm, struct urb *urb)
428 {
429         if (!urb->actual_length)
430                 return;
431
432         tty_insert_flip_string(&acm->port, urb->transfer_buffer,
433                         urb->actual_length);
434         tty_flip_buffer_push(&acm->port);
435 }
436
437 static void acm_read_bulk_callback(struct urb *urb)
438 {
439         struct acm_rb *rb = urb->context;
440         struct acm *acm = rb->instance;
441         unsigned long flags;
442
443         dev_vdbg(&acm->data->dev, "%s - urb %d, len %d\n", __func__,
444                                         rb->index, urb->actual_length);
445         set_bit(rb->index, &acm->read_urbs_free);
446
447         if (!acm->dev) {
448                 dev_dbg(&acm->data->dev, "%s - disconnected\n", __func__);
449                 return;
450         }
451         usb_mark_last_busy(acm->dev);
452
453         if (urb->status) {
454                 dev_dbg(&acm->data->dev, "%s - non-zero urb status: %d\n",
455                                                         __func__, urb->status);
456                 return;
457         }
458         acm_process_read_urb(acm, urb);
459
460         /* throttle device if requested by tty */
461         spin_lock_irqsave(&acm->read_lock, flags);
462         acm->throttled = acm->throttle_req;
463         if (!acm->throttled && !acm->susp_count) {
464                 spin_unlock_irqrestore(&acm->read_lock, flags);
465                 acm_submit_read_urb(acm, rb->index, GFP_ATOMIC);
466         } else {
467                 spin_unlock_irqrestore(&acm->read_lock, flags);
468         }
469 }
470
471 /* data interface wrote those outgoing bytes */
472 static void acm_write_bulk(struct urb *urb)
473 {
474         struct acm_wb *wb = urb->context;
475         struct acm *acm = wb->instance;
476         unsigned long flags;
477
478         if (urb->status || (urb->actual_length != urb->transfer_buffer_length))
479                 dev_vdbg(&acm->data->dev, "%s - len %d/%d, status %d\n",
480                         __func__,
481                         urb->actual_length,
482                         urb->transfer_buffer_length,
483                         urb->status);
484
485         spin_lock_irqsave(&acm->write_lock, flags);
486         acm_write_done(acm, wb);
487         spin_unlock_irqrestore(&acm->write_lock, flags);
488         schedule_work(&acm->work);
489 }
490
491 static void acm_softint(struct work_struct *work)
492 {
493         struct acm *acm = container_of(work, struct acm, work);
494
495         dev_vdbg(&acm->data->dev, "%s\n", __func__);
496
497         tty_port_tty_wakeup(&acm->port);
498 }
499
500 /*
501  * TTY handlers
502  */
503
504 static int acm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
505 {
506         struct acm *acm;
507         int retval;
508
509         dev_dbg(tty->dev, "%s\n", __func__);
510
511         acm = acm_get_by_index(tty->index);
512         if (!acm)
513                 return -ENODEV;
514
515         retval = tty_standard_install(driver, tty);
516         if (retval)
517                 goto error_init_termios;
518
519         tty->driver_data = acm;
520
521         return 0;
522
523 error_init_termios:
524         tty_port_put(&acm->port);
525         return retval;
526 }
527
528 static int acm_tty_open(struct tty_struct *tty, struct file *filp)
529 {
530         struct acm *acm = tty->driver_data;
531
532         dev_dbg(tty->dev, "%s\n", __func__);
533
534         return tty_port_open(&acm->port, tty, filp);
535 }
536
537 static int acm_port_activate(struct tty_port *port, struct tty_struct *tty)
538 {
539         struct acm *acm = container_of(port, struct acm, port);
540         int retval = -ENODEV;
541
542         dev_dbg(&acm->control->dev, "%s\n", __func__);
543
544         mutex_lock(&acm->mutex);
545         if (acm->disconnected)
546                 goto disconnected;
547
548         retval = usb_autopm_get_interface(acm->control);
549         if (retval)
550                 goto error_get_interface;
551
552         /*
553          * FIXME: Why do we need this? Allocating 64K of physically contiguous
554          * memory is really nasty...
555          */
556         set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
557         acm->control->needs_remote_wakeup = 0;
558
559         if (acm_submit_read_urbs(acm, GFP_KERNEL))
560                 goto error_submit_urb;
561
562         acm->ctrlurb->dev = acm->dev;
563         if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
564                 dev_err(&acm->control->dev,
565                         "%s - usb_submit_urb(ctrl irq) failed\n", __func__);
566                 goto error_submit_urb;
567         }
568
569         acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS;
570         if (acm_set_control(acm, acm->ctrlout) < 0 &&
571             (acm->ctrl_caps & USB_CDC_CAP_LINE))
572                 goto error_set_control;
573
574         usb_autopm_put_interface(acm->control);
575
576         /*
577          * Unthrottle device in case the TTY was closed while throttled.
578          */
579         spin_lock_irq(&acm->read_lock);
580         acm->throttled = 0;
581         acm->throttle_req = 0;
582         spin_unlock_irq(&acm->read_lock);
583
584         if (acm_submit_read_urbs(acm, GFP_KERNEL))
585                 goto error_submit_read_urbs;
586
587         mutex_unlock(&acm->mutex);
588
589         return 0;
590
591 error_submit_read_urbs:
592         acm->ctrlout = 0;
593         acm_set_control(acm, acm->ctrlout);
594 error_set_control:
595         usb_kill_urb(acm->ctrlurb);
596 error_submit_urb:
597         usb_autopm_put_interface(acm->control);
598 error_get_interface:
599 disconnected:
600         mutex_unlock(&acm->mutex);
601         return retval;
602 }
603
604 static void acm_port_destruct(struct tty_port *port)
605 {
606         struct acm *acm = container_of(port, struct acm, port);
607
608         dev_dbg(&acm->control->dev, "%s\n", __func__);
609
610         acm_release_minor(acm);
611         usb_put_intf(acm->control);
612         kfree(acm->country_codes);
613         kfree(acm);
614 }
615
616 static void acm_port_shutdown(struct tty_port *port)
617 {
618         struct acm *acm = container_of(port, struct acm, port);
619         int i;
620
621         dev_dbg(&acm->control->dev, "%s\n", __func__);
622
623         mutex_lock(&acm->mutex);
624         if (!acm->disconnected) {
625                 usb_autopm_get_interface(acm->control);
626                 acm_set_control(acm, acm->ctrlout = 0);
627                 usb_kill_urb(acm->ctrlurb);
628                 for (i = 0; i < ACM_NW; i++)
629                         usb_kill_urb(acm->wb[i].urb);
630                 for (i = 0; i < acm->rx_buflimit; i++)
631                         usb_kill_urb(acm->read_urbs[i]);
632                 acm->control->needs_remote_wakeup = 0;
633                 usb_autopm_put_interface(acm->control);
634         }
635         mutex_unlock(&acm->mutex);
636 }
637
638 static void acm_tty_cleanup(struct tty_struct *tty)
639 {
640         struct acm *acm = tty->driver_data;
641         dev_dbg(&acm->control->dev, "%s\n", __func__);
642         tty_port_put(&acm->port);
643 }
644
645 static void acm_tty_hangup(struct tty_struct *tty)
646 {
647         struct acm *acm = tty->driver_data;
648         dev_dbg(&acm->control->dev, "%s\n", __func__);
649         tty_port_hangup(&acm->port);
650 }
651
652 static void acm_tty_close(struct tty_struct *tty, struct file *filp)
653 {
654         struct acm *acm = tty->driver_data;
655         dev_dbg(&acm->control->dev, "%s\n", __func__);
656         tty_port_close(&acm->port, tty, filp);
657 }
658
659 static int acm_tty_write(struct tty_struct *tty,
660                                         const unsigned char *buf, int count)
661 {
662         struct acm *acm = tty->driver_data;
663         int stat;
664         unsigned long flags;
665         int wbn;
666         struct acm_wb *wb;
667
668         if (!count)
669                 return 0;
670
671         dev_vdbg(&acm->data->dev, "%s - count %d\n", __func__, count);
672
673         spin_lock_irqsave(&acm->write_lock, flags);
674         wbn = acm_wb_alloc(acm);
675         if (wbn < 0) {
676                 spin_unlock_irqrestore(&acm->write_lock, flags);
677                 return 0;
678         }
679         wb = &acm->wb[wbn];
680
681         count = (count > acm->writesize) ? acm->writesize : count;
682         dev_vdbg(&acm->data->dev, "%s - write %d\n", __func__, count);
683         memcpy(wb->buf, buf, count);
684         wb->len = count;
685         spin_unlock_irqrestore(&acm->write_lock, flags);
686
687         stat = acm_write_start(acm, wbn);
688         if (stat < 0)
689                 return stat;
690         return count;
691 }
692
693 static int acm_tty_write_room(struct tty_struct *tty)
694 {
695         struct acm *acm = tty->driver_data;
696         /*
697          * Do not let the line discipline to know that we have a reserve,
698          * or it might get too enthusiastic.
699          */
700         return acm_wb_is_avail(acm) ? acm->writesize : 0;
701 }
702
703 static int acm_tty_chars_in_buffer(struct tty_struct *tty)
704 {
705         struct acm *acm = tty->driver_data;
706         /*
707          * if the device was unplugged then any remaining characters fell out
708          * of the connector ;)
709          */
710         if (acm->disconnected)
711                 return 0;
712         /*
713          * This is inaccurate (overcounts), but it works.
714          */
715         return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
716 }
717
718 static void acm_tty_throttle(struct tty_struct *tty)
719 {
720         struct acm *acm = tty->driver_data;
721
722         spin_lock_irq(&acm->read_lock);
723         acm->throttle_req = 1;
724         spin_unlock_irq(&acm->read_lock);
725 }
726
727 static void acm_tty_unthrottle(struct tty_struct *tty)
728 {
729         struct acm *acm = tty->driver_data;
730         unsigned int was_throttled;
731
732         spin_lock_irq(&acm->read_lock);
733         was_throttled = acm->throttled;
734         acm->throttled = 0;
735         acm->throttle_req = 0;
736         spin_unlock_irq(&acm->read_lock);
737
738         if (was_throttled)
739                 acm_submit_read_urbs(acm, GFP_KERNEL);
740 }
741
742 static int acm_tty_break_ctl(struct tty_struct *tty, int state)
743 {
744         struct acm *acm = tty->driver_data;
745         int retval;
746
747         retval = acm_send_break(acm, state ? 0xffff : 0);
748         if (retval < 0)
749                 dev_dbg(&acm->control->dev, "%s - send break failed\n",
750                                                                 __func__);
751         return retval;
752 }
753
754 static int acm_tty_tiocmget(struct tty_struct *tty)
755 {
756         struct acm *acm = tty->driver_data;
757
758         return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
759                (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
760                (acm->ctrlin  & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
761                (acm->ctrlin  & ACM_CTRL_RI  ? TIOCM_RI  : 0) |
762                (acm->ctrlin  & ACM_CTRL_DCD ? TIOCM_CD  : 0) |
763                TIOCM_CTS;
764 }
765
766 static int acm_tty_tiocmset(struct tty_struct *tty,
767                             unsigned int set, unsigned int clear)
768 {
769         struct acm *acm = tty->driver_data;
770         unsigned int newctrl;
771
772         newctrl = acm->ctrlout;
773         set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
774                                         (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
775         clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
776                                         (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
777
778         newctrl = (newctrl & ~clear) | set;
779
780         if (acm->ctrlout == newctrl)
781                 return 0;
782         return acm_set_control(acm, acm->ctrlout = newctrl);
783 }
784
785 static int get_serial_info(struct acm *acm, struct serial_struct __user *info)
786 {
787         struct serial_struct tmp;
788
789         if (!info)
790                 return -EINVAL;
791
792         memset(&tmp, 0, sizeof(tmp));
793         tmp.flags = ASYNC_LOW_LATENCY;
794         tmp.xmit_fifo_size = acm->writesize;
795         tmp.baud_base = le32_to_cpu(acm->line.dwDTERate);
796         tmp.close_delay = acm->port.close_delay / 10;
797         tmp.closing_wait = acm->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
798                                 ASYNC_CLOSING_WAIT_NONE :
799                                 acm->port.closing_wait / 10;
800
801         if (copy_to_user(info, &tmp, sizeof(tmp)))
802                 return -EFAULT;
803         else
804                 return 0;
805 }
806
807 static int set_serial_info(struct acm *acm,
808                                 struct serial_struct __user *newinfo)
809 {
810         struct serial_struct new_serial;
811         unsigned int closing_wait, close_delay;
812         int retval = 0;
813
814         if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
815                 return -EFAULT;
816
817         close_delay = new_serial.close_delay * 10;
818         closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
819                         ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
820
821         mutex_lock(&acm->port.mutex);
822
823         if (!capable(CAP_SYS_ADMIN)) {
824                 if ((close_delay != acm->port.close_delay) ||
825                     (closing_wait != acm->port.closing_wait))
826                         retval = -EPERM;
827                 else
828                         retval = -EOPNOTSUPP;
829         } else {
830                 acm->port.close_delay  = close_delay;
831                 acm->port.closing_wait = closing_wait;
832         }
833
834         mutex_unlock(&acm->port.mutex);
835         return retval;
836 }
837
838 static int acm_tty_ioctl(struct tty_struct *tty,
839                                         unsigned int cmd, unsigned long arg)
840 {
841         struct acm *acm = tty->driver_data;
842         int rv = -ENOIOCTLCMD;
843
844         switch (cmd) {
845         case TIOCGSERIAL: /* gets serial port data */
846                 rv = get_serial_info(acm, (struct serial_struct __user *) arg);
847                 break;
848         case TIOCSSERIAL:
849                 rv = set_serial_info(acm, (struct serial_struct __user *) arg);
850                 break;
851         }
852
853         return rv;
854 }
855
856 static void acm_tty_set_termios(struct tty_struct *tty,
857                                                 struct ktermios *termios_old)
858 {
859         struct acm *acm = tty->driver_data;
860         struct ktermios *termios = &tty->termios;
861         struct usb_cdc_line_coding newline;
862         int newctrl = acm->ctrlout;
863
864         newline.dwDTERate = cpu_to_le32(tty_get_baud_rate(tty));
865         newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
866         newline.bParityType = termios->c_cflag & PARENB ?
867                                 (termios->c_cflag & PARODD ? 1 : 2) +
868                                 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
869         switch (termios->c_cflag & CSIZE) {
870         case CS5:
871                 newline.bDataBits = 5;
872                 break;
873         case CS6:
874                 newline.bDataBits = 6;
875                 break;
876         case CS7:
877                 newline.bDataBits = 7;
878                 break;
879         case CS8:
880         default:
881                 newline.bDataBits = 8;
882                 break;
883         }
884         /* FIXME: Needs to clear unsupported bits in the termios */
885         acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
886
887         if (!newline.dwDTERate) {
888                 newline.dwDTERate = acm->line.dwDTERate;
889                 newctrl &= ~ACM_CTRL_DTR;
890         } else
891                 newctrl |=  ACM_CTRL_DTR;
892
893         if (newctrl != acm->ctrlout)
894                 acm_set_control(acm, acm->ctrlout = newctrl);
895
896         if (memcmp(&acm->line, &newline, sizeof newline)) {
897                 memcpy(&acm->line, &newline, sizeof newline);
898                 dev_dbg(&acm->control->dev, "%s - set line: %d %d %d %d\n",
899                         __func__,
900                         le32_to_cpu(newline.dwDTERate),
901                         newline.bCharFormat, newline.bParityType,
902                         newline.bDataBits);
903                 acm_set_line(acm, &acm->line);
904         }
905 }
906
907 static const struct tty_port_operations acm_port_ops = {
908         .shutdown = acm_port_shutdown,
909         .activate = acm_port_activate,
910         .destruct = acm_port_destruct,
911 };
912
913 /*
914  * USB probe and disconnect routines.
915  */
916
917 /* Little helpers: write/read buffers free */
918 static void acm_write_buffers_free(struct acm *acm)
919 {
920         int i;
921         struct acm_wb *wb;
922         struct usb_device *usb_dev = interface_to_usbdev(acm->control);
923
924         for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++)
925                 usb_free_coherent(usb_dev, acm->writesize, wb->buf, wb->dmah);
926 }
927
928 static void acm_read_buffers_free(struct acm *acm)
929 {
930         struct usb_device *usb_dev = interface_to_usbdev(acm->control);
931         int i;
932
933         for (i = 0; i < acm->rx_buflimit; i++)
934                 usb_free_coherent(usb_dev, acm->readsize,
935                           acm->read_buffers[i].base, acm->read_buffers[i].dma);
936 }
937
938 /* Little helper: write buffers allocate */
939 static int acm_write_buffers_alloc(struct acm *acm)
940 {
941         int i;
942         struct acm_wb *wb;
943
944         for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
945                 wb->buf = usb_alloc_coherent(acm->dev, acm->writesize, GFP_KERNEL,
946                     &wb->dmah);
947                 if (!wb->buf) {
948                         while (i != 0) {
949                                 --i;
950                                 --wb;
951                                 usb_free_coherent(acm->dev, acm->writesize,
952                                     wb->buf, wb->dmah);
953                         }
954                         return -ENOMEM;
955                 }
956         }
957         return 0;
958 }
959
960 static int acm_probe(struct usb_interface *intf,
961                      const struct usb_device_id *id)
962 {
963         struct usb_cdc_union_desc *union_header = NULL;
964         struct usb_cdc_country_functional_desc *cfd = NULL;
965         unsigned char *buffer = intf->altsetting->extra;
966         int buflen = intf->altsetting->extralen;
967         struct usb_interface *control_interface;
968         struct usb_interface *data_interface;
969         struct usb_endpoint_descriptor *epctrl = NULL;
970         struct usb_endpoint_descriptor *epread = NULL;
971         struct usb_endpoint_descriptor *epwrite = NULL;
972         struct usb_device *usb_dev = interface_to_usbdev(intf);
973         struct acm *acm;
974         int minor;
975         int ctrlsize, readsize;
976         u8 *buf;
977         u8 ac_management_function = 0;
978         u8 call_management_function = 0;
979         int call_interface_num = -1;
980         int data_interface_num = -1;
981         unsigned long quirks;
982         int num_rx_buf;
983         int i;
984         int combined_interfaces = 0;
985         struct device *tty_dev;
986         int rv = -ENOMEM;
987
988         /* normal quirks */
989         quirks = (unsigned long)id->driver_info;
990
991         if (quirks & IGNORE_DEVICE)
992                 return -ENODEV;
993
994         num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
995
996         /* handle quirks deadly to normal probing*/
997         if (quirks & NO_UNION_NORMAL) {
998                 data_interface = usb_ifnum_to_if(usb_dev, 1);
999                 control_interface = usb_ifnum_to_if(usb_dev, 0);
1000                 goto skip_normal_probe;
1001         }
1002
1003         /* normal probing*/
1004         if (!buffer) {
1005                 dev_err(&intf->dev, "Weird descriptor references\n");
1006                 return -EINVAL;
1007         }
1008
1009         if (!buflen) {
1010                 if (intf->cur_altsetting->endpoint &&
1011                                 intf->cur_altsetting->endpoint->extralen &&
1012                                 intf->cur_altsetting->endpoint->extra) {
1013                         dev_dbg(&intf->dev,
1014                                 "Seeking extra descriptors on endpoint\n");
1015                         buflen = intf->cur_altsetting->endpoint->extralen;
1016                         buffer = intf->cur_altsetting->endpoint->extra;
1017                 } else {
1018                         dev_err(&intf->dev,
1019                                 "Zero length descriptor references\n");
1020                         return -EINVAL;
1021                 }
1022         }
1023
1024         while (buflen > 0) {
1025                 if (buffer[1] != USB_DT_CS_INTERFACE) {
1026                         dev_err(&intf->dev, "skipping garbage\n");
1027                         goto next_desc;
1028                 }
1029
1030                 switch (buffer[2]) {
1031                 case USB_CDC_UNION_TYPE: /* we've found it */
1032                         if (union_header) {
1033                                 dev_err(&intf->dev, "More than one "
1034                                         "union descriptor, skipping ...\n");
1035                                 goto next_desc;
1036                         }
1037                         union_header = (struct usb_cdc_union_desc *)buffer;
1038                         break;
1039                 case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
1040                         cfd = (struct usb_cdc_country_functional_desc *)buffer;
1041                         break;
1042                 case USB_CDC_HEADER_TYPE: /* maybe check version */
1043                         break; /* for now we ignore it */
1044                 case USB_CDC_ACM_TYPE:
1045                         ac_management_function = buffer[3];
1046                         break;
1047                 case USB_CDC_CALL_MANAGEMENT_TYPE:
1048                         call_management_function = buffer[3];
1049                         call_interface_num = buffer[4];
1050                         if ((quirks & NOT_A_MODEM) == 0 && (call_management_function & 3) != 3)
1051                                 dev_err(&intf->dev, "This device cannot do calls on its own. It is not a modem.\n");
1052                         break;
1053                 default:
1054                         /* there are LOTS more CDC descriptors that
1055                          * could legitimately be found here.
1056                          */
1057                         dev_dbg(&intf->dev, "Ignoring descriptor: "
1058                                         "type %02x, length %d\n",
1059                                         buffer[2], buffer[0]);
1060                         break;
1061                 }
1062 next_desc:
1063                 buflen -= buffer[0];
1064                 buffer += buffer[0];
1065         }
1066
1067         if (!union_header) {
1068                 if (call_interface_num > 0) {
1069                         dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n");
1070                         /* quirks for Droids MuIn LCD */
1071                         if (quirks & NO_DATA_INTERFACE)
1072                                 data_interface = usb_ifnum_to_if(usb_dev, 0);
1073                         else
1074                                 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
1075                         control_interface = intf;
1076                 } else {
1077                         if (intf->cur_altsetting->desc.bNumEndpoints != 3) {
1078                                 dev_dbg(&intf->dev,"No union descriptor, giving up\n");
1079                                 return -ENODEV;
1080                         } else {
1081                                 dev_warn(&intf->dev,"No union descriptor, testing for castrated device\n");
1082                                 combined_interfaces = 1;
1083                                 control_interface = data_interface = intf;
1084                                 goto look_for_collapsed_interface;
1085                         }
1086                 }
1087         } else {
1088                 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
1089                 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
1090                 if (!control_interface || !data_interface) {
1091                         dev_dbg(&intf->dev, "no interfaces\n");
1092                         return -ENODEV;
1093                 }
1094         }
1095
1096         if (data_interface_num != call_interface_num)
1097                 dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n");
1098
1099         if (control_interface == data_interface) {
1100                 /* some broken devices designed for windows work this way */
1101                 dev_warn(&intf->dev,"Control and data interfaces are not separated!\n");
1102                 combined_interfaces = 1;
1103                 /* a popular other OS doesn't use it */
1104                 quirks |= NO_CAP_LINE;
1105                 if (data_interface->cur_altsetting->desc.bNumEndpoints != 3) {
1106                         dev_err(&intf->dev, "This needs exactly 3 endpoints\n");
1107                         return -EINVAL;
1108                 }
1109 look_for_collapsed_interface:
1110                 for (i = 0; i < 3; i++) {
1111                         struct usb_endpoint_descriptor *ep;
1112                         ep = &data_interface->cur_altsetting->endpoint[i].desc;
1113
1114                         if (usb_endpoint_is_int_in(ep))
1115                                 epctrl = ep;
1116                         else if (usb_endpoint_is_bulk_out(ep))
1117                                 epwrite = ep;
1118                         else if (usb_endpoint_is_bulk_in(ep))
1119                                 epread = ep;
1120                         else
1121                                 return -EINVAL;
1122                 }
1123                 if (!epctrl || !epread || !epwrite)
1124                         return -ENODEV;
1125                 else
1126                         goto made_compressed_probe;
1127         }
1128
1129 skip_normal_probe:
1130
1131         /*workaround for switched interfaces */
1132         if (data_interface->cur_altsetting->desc.bInterfaceClass
1133                                                 != CDC_DATA_INTERFACE_TYPE) {
1134                 if (control_interface->cur_altsetting->desc.bInterfaceClass
1135                                                 == CDC_DATA_INTERFACE_TYPE) {
1136                         struct usb_interface *t;
1137                         dev_dbg(&intf->dev,
1138                                 "Your device has switched interfaces.\n");
1139                         t = control_interface;
1140                         control_interface = data_interface;
1141                         data_interface = t;
1142                 } else {
1143                         return -EINVAL;
1144                 }
1145         }
1146
1147         /* Accept probe requests only for the control interface */
1148         if (!combined_interfaces && intf != control_interface)
1149                 return -ENODEV;
1150
1151         if (!combined_interfaces && usb_interface_claimed(data_interface)) {
1152                 /* valid in this context */
1153                 dev_dbg(&intf->dev, "The data interface isn't available\n");
1154                 return -EBUSY;
1155         }
1156
1157
1158         if (data_interface->cur_altsetting->desc.bNumEndpoints < 2 ||
1159             control_interface->cur_altsetting->desc.bNumEndpoints == 0)
1160                 return -EINVAL;
1161
1162         epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
1163         epread = &data_interface->cur_altsetting->endpoint[0].desc;
1164         epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
1165
1166
1167         /* workaround for switched endpoints */
1168         if (!usb_endpoint_dir_in(epread)) {
1169                 /* descriptors are swapped */
1170                 struct usb_endpoint_descriptor *t;
1171                 dev_dbg(&intf->dev,
1172                         "The data interface has switched endpoints\n");
1173                 t = epread;
1174                 epread = epwrite;
1175                 epwrite = t;
1176         }
1177 made_compressed_probe:
1178         dev_dbg(&intf->dev, "interfaces are valid\n");
1179
1180         acm = kzalloc(sizeof(struct acm), GFP_KERNEL);
1181         if (acm == NULL) {
1182                 dev_err(&intf->dev, "out of memory (acm kzalloc)\n");
1183                 goto alloc_fail;
1184         }
1185
1186         minor = acm_alloc_minor(acm);
1187         if (minor == ACM_TTY_MINORS) {
1188                 dev_err(&intf->dev, "no more free acm devices\n");
1189                 kfree(acm);
1190                 return -ENODEV;
1191         }
1192
1193         ctrlsize = usb_endpoint_maxp(epctrl);
1194         readsize = usb_endpoint_maxp(epread) *
1195                                 (quirks == SINGLE_RX_URB ? 1 : 2);
1196         acm->combined_interfaces = combined_interfaces;
1197         acm->writesize = usb_endpoint_maxp(epwrite) * 20;
1198         acm->control = control_interface;
1199         acm->data = data_interface;
1200         acm->minor = minor;
1201         acm->dev = usb_dev;
1202         acm->ctrl_caps = ac_management_function;
1203         if (quirks & NO_CAP_LINE)
1204                 acm->ctrl_caps &= ~USB_CDC_CAP_LINE;
1205         acm->ctrlsize = ctrlsize;
1206         acm->readsize = readsize;
1207         acm->rx_buflimit = num_rx_buf;
1208         INIT_WORK(&acm->work, acm_softint);
1209         init_usb_anchor(&acm->deferred);
1210         spin_lock_init(&acm->write_lock);
1211         spin_lock_init(&acm->read_lock);
1212         mutex_init(&acm->mutex);
1213         acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
1214         acm->is_int_ep = usb_endpoint_xfer_int(epread);
1215         if (acm->is_int_ep)
1216                 acm->bInterval = epread->bInterval;
1217         if (quirks & NO_HANGUP_IN_RESET_RESUME)
1218                 acm->no_hangup_in_reset_resume = 1;
1219         tty_port_init(&acm->port);
1220         acm->port.ops = &acm_port_ops;
1221
1222         buf = usb_alloc_coherent(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
1223         if (!buf) {
1224                 dev_err(&intf->dev, "out of memory (ctrl buffer alloc)\n");
1225                 goto alloc_fail2;
1226         }
1227         acm->ctrl_buffer = buf;
1228
1229         if (acm_write_buffers_alloc(acm) < 0) {
1230                 dev_err(&intf->dev, "out of memory (write buffer alloc)\n");
1231                 goto alloc_fail4;
1232         }
1233
1234         acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
1235         if (!acm->ctrlurb) {
1236                 dev_err(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
1237                 goto alloc_fail5;
1238         }
1239         for (i = 0; i < num_rx_buf; i++) {
1240                 struct acm_rb *rb = &(acm->read_buffers[i]);
1241                 struct urb *urb;
1242
1243                 rb->base = usb_alloc_coherent(acm->dev, readsize, GFP_KERNEL,
1244                                                                 &rb->dma);
1245                 if (!rb->base) {
1246                         dev_err(&intf->dev, "out of memory "
1247                                         "(read bufs usb_alloc_coherent)\n");
1248                         goto alloc_fail6;
1249                 }
1250                 rb->index = i;
1251                 rb->instance = acm;
1252
1253                 urb = usb_alloc_urb(0, GFP_KERNEL);
1254                 if (!urb) {
1255                         dev_err(&intf->dev,
1256                                 "out of memory (read urbs usb_alloc_urb)\n");
1257                         goto alloc_fail6;
1258                 }
1259                 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1260                 urb->transfer_dma = rb->dma;
1261                 if (acm->is_int_ep) {
1262                         usb_fill_int_urb(urb, acm->dev,
1263                                          acm->rx_endpoint,
1264                                          rb->base,
1265                                          acm->readsize,
1266                                          acm_read_bulk_callback, rb,
1267                                          acm->bInterval);
1268                 } else {
1269                         usb_fill_bulk_urb(urb, acm->dev,
1270                                           acm->rx_endpoint,
1271                                           rb->base,
1272                                           acm->readsize,
1273                                           acm_read_bulk_callback, rb);
1274                 }
1275
1276                 acm->read_urbs[i] = urb;
1277                 __set_bit(i, &acm->read_urbs_free);
1278         }
1279         for (i = 0; i < ACM_NW; i++) {
1280                 struct acm_wb *snd = &(acm->wb[i]);
1281
1282                 snd->urb = usb_alloc_urb(0, GFP_KERNEL);
1283                 if (snd->urb == NULL) {
1284                         dev_err(&intf->dev,
1285                                 "out of memory (write urbs usb_alloc_urb)\n");
1286                         goto alloc_fail7;
1287                 }
1288
1289                 if (usb_endpoint_xfer_int(epwrite))
1290                         usb_fill_int_urb(snd->urb, usb_dev,
1291                                 usb_sndintpipe(usb_dev, epwrite->bEndpointAddress),
1292                                 NULL, acm->writesize, acm_write_bulk, snd, epwrite->bInterval);
1293                 else
1294                         usb_fill_bulk_urb(snd->urb, usb_dev,
1295                                 usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
1296                                 NULL, acm->writesize, acm_write_bulk, snd);
1297                 snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1298                 snd->instance = acm;
1299         }
1300
1301         usb_set_intfdata(intf, acm);
1302
1303         i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
1304         if (i < 0)
1305                 goto alloc_fail7;
1306
1307         if (cfd) { /* export the country data */
1308                 acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
1309                 if (!acm->country_codes)
1310                         goto skip_countries;
1311                 acm->country_code_size = cfd->bLength - 4;
1312                 memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0,
1313                                                         cfd->bLength - 4);
1314                 acm->country_rel_date = cfd->iCountryCodeRelDate;
1315
1316                 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
1317                 if (i < 0) {
1318                         kfree(acm->country_codes);
1319                         acm->country_codes = NULL;
1320                         acm->country_code_size = 0;
1321                         goto skip_countries;
1322                 }
1323
1324                 i = device_create_file(&intf->dev,
1325                                                 &dev_attr_iCountryCodeRelDate);
1326                 if (i < 0) {
1327                         device_remove_file(&intf->dev, &dev_attr_wCountryCodes);
1328                         kfree(acm->country_codes);
1329                         acm->country_codes = NULL;
1330                         acm->country_code_size = 0;
1331                         goto skip_countries;
1332                 }
1333         }
1334
1335 skip_countries:
1336         usb_fill_int_urb(acm->ctrlurb, usb_dev,
1337                          usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
1338                          acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm,
1339                          /* works around buggy devices */
1340                          epctrl->bInterval ? epctrl->bInterval : 0xff);
1341         acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1342         acm->ctrlurb->transfer_dma = acm->ctrl_dma;
1343
1344         dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
1345
1346         acm_set_control(acm, acm->ctrlout);
1347
1348         acm->line.dwDTERate = cpu_to_le32(9600);
1349         acm->line.bDataBits = 8;
1350         acm_set_line(acm, &acm->line);
1351
1352         usb_driver_claim_interface(&acm_driver, data_interface, acm);
1353         usb_set_intfdata(data_interface, acm);
1354
1355         usb_get_intf(control_interface);
1356         tty_dev = tty_port_register_device(&acm->port, acm_tty_driver, minor,
1357                         &control_interface->dev);
1358         if (IS_ERR(tty_dev)) {
1359                 rv = PTR_ERR(tty_dev);
1360                 goto alloc_fail8;
1361         }
1362
1363         return 0;
1364 alloc_fail8:
1365         if (acm->country_codes) {
1366                 device_remove_file(&acm->control->dev,
1367                                 &dev_attr_wCountryCodes);
1368                 device_remove_file(&acm->control->dev,
1369                                 &dev_attr_iCountryCodeRelDate);
1370         }
1371         device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
1372 alloc_fail7:
1373         usb_set_intfdata(intf, NULL);
1374         for (i = 0; i < ACM_NW; i++)
1375                 usb_free_urb(acm->wb[i].urb);
1376 alloc_fail6:
1377         for (i = 0; i < num_rx_buf; i++)
1378                 usb_free_urb(acm->read_urbs[i]);
1379         acm_read_buffers_free(acm);
1380         usb_free_urb(acm->ctrlurb);
1381 alloc_fail5:
1382         acm_write_buffers_free(acm);
1383 alloc_fail4:
1384         usb_free_coherent(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1385 alloc_fail2:
1386         acm_release_minor(acm);
1387         kfree(acm);
1388 alloc_fail:
1389         return rv;
1390 }
1391
1392 static void stop_data_traffic(struct acm *acm)
1393 {
1394         int i;
1395
1396         if (!acm) {
1397                 pr_err("%s: !acm\n", __func__);
1398                 return;
1399         }
1400
1401         dev_dbg(&acm->control->dev, "%s\n", __func__);
1402
1403         usb_kill_urb(acm->ctrlurb);
1404         for (i = 0; i < ACM_NW; i++)
1405                 usb_kill_urb(acm->wb[i].urb);
1406         for (i = 0; i < acm->rx_buflimit; i++)
1407                 usb_kill_urb(acm->read_urbs[i]);
1408
1409         cancel_work_sync(&acm->work);
1410 }
1411
1412 static void acm_disconnect(struct usb_interface *intf)
1413 {
1414         struct acm *acm = usb_get_intfdata(intf);
1415         struct usb_device *usb_dev = interface_to_usbdev(intf);
1416         struct tty_struct *tty;
1417         struct urb *res;
1418         int i;
1419
1420         dev_dbg(&intf->dev, "%s\n", __func__);
1421
1422         /* sibling interface is already cleaning up */
1423         if (!acm)
1424                 return;
1425
1426         mutex_lock(&acm->mutex);
1427         acm->disconnected = true;
1428         if (acm->country_codes) {
1429                 device_remove_file(&acm->control->dev,
1430                                 &dev_attr_wCountryCodes);
1431                 device_remove_file(&acm->control->dev,
1432                                 &dev_attr_iCountryCodeRelDate);
1433         }
1434         device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
1435         usb_set_intfdata(acm->control, NULL);
1436         usb_set_intfdata(acm->data, NULL);
1437         mutex_unlock(&acm->mutex);
1438
1439         tty = tty_port_tty_get(&acm->port);
1440         if (tty) {
1441                 tty_vhangup(tty);
1442                 tty_kref_put(tty);
1443         }
1444
1445         stop_data_traffic(acm);
1446
1447         tty_unregister_device(acm_tty_driver, acm->minor);
1448
1449         /* decrement ref count of anchored urbs */
1450         while ((res = usb_get_from_anchor(&acm->deferred)))
1451                 usb_put_urb(res);
1452         usb_free_urb(acm->ctrlurb);
1453         for (i = 0; i < ACM_NW; i++)
1454                 usb_free_urb(acm->wb[i].urb);
1455         for (i = 0; i < acm->rx_buflimit; i++)
1456                 usb_free_urb(acm->read_urbs[i]);
1457         acm_write_buffers_free(acm);
1458         usb_free_coherent(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1459         acm_read_buffers_free(acm);
1460
1461         if (!acm->combined_interfaces)
1462                 usb_driver_release_interface(&acm_driver, intf == acm->control ?
1463                                         acm->data : acm->control);
1464
1465         tty_port_put(&acm->port);
1466 }
1467
1468 #ifdef CONFIG_PM
1469 static int acm_suspend(struct usb_interface *intf, pm_message_t message)
1470 {
1471         struct acm *acm = usb_get_intfdata(intf);
1472         int cnt;
1473
1474         if (!acm) {
1475                 pr_err("%s: !acm\n", __func__);
1476                 return -ENODEV;
1477         }
1478
1479         if (PMSG_IS_AUTO(message)) {
1480                 int b;
1481
1482                 spin_lock_irq(&acm->write_lock);
1483                 b = acm->transmitting;
1484                 spin_unlock_irq(&acm->write_lock);
1485                 if (b)
1486                         return -EBUSY;
1487         }
1488
1489         spin_lock_irq(&acm->read_lock);
1490         spin_lock(&acm->write_lock);
1491         cnt = acm->susp_count++;
1492         spin_unlock(&acm->write_lock);
1493         spin_unlock_irq(&acm->read_lock);
1494
1495         if (cnt)
1496                 return 0;
1497
1498         if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags))
1499                 stop_data_traffic(acm);
1500
1501         return 0;
1502 }
1503
1504 static int acm_resume(struct usb_interface *intf)
1505 {
1506         struct acm *acm = usb_get_intfdata(intf);
1507         int rv = 0;
1508         int cnt;
1509 #ifdef CONFIG_PM
1510         struct urb *res;
1511 #else
1512         struct acm_wb *wb;
1513 #endif
1514
1515         if (!acm) {
1516                 pr_err("%s: !acm\n", __func__);
1517                 return -ENODEV;
1518         }
1519
1520         spin_lock_irq(&acm->read_lock);
1521         if (acm->susp_count > 0) {
1522                 acm->susp_count -= 1;
1523                 cnt = acm->susp_count;
1524         } else {
1525                 spin_unlock_irq(&acm->read_lock);
1526                 return 0;
1527         }
1528         spin_unlock_irq(&acm->read_lock);
1529
1530         if (cnt)
1531                 return 0;
1532
1533         if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags)) {
1534                 rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO);
1535                 spin_lock_irq(&acm->write_lock);
1536 #ifdef CONFIG_PM
1537                 while ((res = usb_get_from_anchor(&acm->deferred))) {
1538                         /* decrement ref count*/
1539                         usb_put_urb(res);
1540                         rv = usb_submit_urb(res, GFP_ATOMIC);
1541                         if (rv < 0) {
1542                                 dbg("usb_submit_urb(pending request)"
1543                                         " failed: %d", rv);
1544                                 usb_unanchor_urb(res);
1545                                 acm_write_done(acm, res->context);
1546                         }
1547                 }
1548                 spin_unlock_irq(&acm->write_lock);
1549 #else
1550                 if (acm->delayed_wb) {
1551                         wb = acm->delayed_wb;
1552                         acm->delayed_wb = NULL;
1553                         spin_unlock_irq(&acm->write_lock);
1554                         acm_start_wb(acm, wb);
1555                 } else {
1556                         spin_unlock_irq(&acm->write_lock);
1557                 }
1558 #endif
1559
1560                 /*
1561                  * delayed error checking because we must
1562                  * do the write path at all cost
1563                  */
1564                 if (rv < 0)
1565                         goto err_out;
1566
1567                 rv = acm_submit_read_urbs(acm, GFP_NOIO);
1568         }
1569
1570 err_out:
1571         return rv;
1572 }
1573
1574 static int acm_reset_resume(struct usb_interface *intf)
1575 {
1576         struct acm *acm = usb_get_intfdata(intf);
1577
1578         if (!acm) {
1579                 pr_err("%s: !acm\n", __func__);
1580                 return -ENODEV;
1581         }
1582
1583         if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags)) {
1584                 tty = tty_port_tty_get(&acm->port);
1585                 if (tty) {
1586                         if (!acm->no_hangup_in_reset_resume)
1587                                 tty_hangup(tty);
1588                         tty_kref_put(tty);
1589                 }
1590         }
1591
1592         return acm_resume(intf);
1593 }
1594
1595 #endif /* CONFIG_PM */
1596
1597 #define NOKIA_PCSUITE_ACM_INFO(x) \
1598                 USB_DEVICE_AND_INTERFACE_INFO(0x0421, x, \
1599                 USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
1600                 USB_CDC_ACM_PROTO_VENDOR)
1601
1602 #define SAMSUNG_PCSUITE_ACM_INFO(x) \
1603                 USB_DEVICE_AND_INTERFACE_INFO(0x04e7, x, \
1604                 USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
1605                 USB_CDC_ACM_PROTO_VENDOR)
1606
1607 /*
1608  * USB driver structure.
1609  */
1610
1611 static const struct usb_device_id acm_ids[] = {
1612         /* quirky and broken devices */
1613         { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
1614         .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1615         },
1616         { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
1617         .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1618         },
1619         { USB_DEVICE(0x0e8d, 0x3329), /* MediaTek Inc GPS */
1620         .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1621         },
1622         { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
1623         .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1624         },
1625         { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
1626         .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1627         },
1628         { USB_DEVICE(0x0ace, 0x1602), /* ZyDAS 56K USB MODEM */
1629         .driver_info = SINGLE_RX_URB,
1630         },
1631         { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
1632         .driver_info = SINGLE_RX_URB, /* firmware bug */
1633         },
1634         { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
1635         .driver_info = SINGLE_RX_URB, /* firmware bug */
1636         },
1637         { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
1638         .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1639         },
1640         { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
1641         .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1642         },
1643         { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */
1644         .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1645         },
1646         { USB_DEVICE(0x0572, 0x1324), /* Conexant USB MODEM RD02-D400 */
1647         .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1648         },
1649         { USB_DEVICE(0x0572, 0x1328), /* Shiro / Aztech USB MODEM UM-3100 */
1650         .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1651         },
1652         { USB_DEVICE(0x22b8, 0x6425), /* Motorola MOTOMAGX phones */
1653         },
1654         /* Motorola H24 HSPA module: */
1655         { USB_DEVICE(0x22b8, 0x2d91) }, /* modem                                */
1656         { USB_DEVICE(0x22b8, 0x2d92) }, /* modem           + diagnostics        */
1657         { USB_DEVICE(0x22b8, 0x2d93) }, /* modem + AT port                      */
1658         { USB_DEVICE(0x22b8, 0x2d95) }, /* modem + AT port + diagnostics        */
1659         { USB_DEVICE(0x22b8, 0x2d96) }, /* modem                         + NMEA */
1660         { USB_DEVICE(0x22b8, 0x2d97) }, /* modem           + diagnostics + NMEA */
1661         { USB_DEVICE(0x22b8, 0x2d99) }, /* modem + AT port               + NMEA */
1662         { USB_DEVICE(0x22b8, 0x2d9a) }, /* modem + AT port + diagnostics + NMEA */
1663
1664         { USB_DEVICE(0x0572, 0x1329), /* Hummingbird huc56s (Conexant) */
1665         .driver_info = NO_UNION_NORMAL, /* union descriptor misplaced on
1666                                            data interface instead of
1667                                            communications interface.
1668                                            Maybe we should define a new
1669                                            quirk for this. */
1670         },
1671         { USB_DEVICE(0x0572, 0x1340), /* Conexant CX93010-2x UCMxx */
1672         .driver_info = NO_UNION_NORMAL,
1673         },
1674         { USB_DEVICE(0x05f9, 0x4002), /* PSC Scanning, Magellan 800i */
1675         .driver_info = NO_UNION_NORMAL,
1676         },
1677         { USB_DEVICE(0x1bbb, 0x0003), /* Alcatel OT-I650 */
1678         .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
1679         },
1680         { USB_DEVICE(0x1576, 0x03b1), /* Maretron USB100 */
1681         .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
1682         },
1683         { USB_DEVICE(0x1519, 0x0020),
1684         .driver_info = NO_UNION_NORMAL | NO_HANGUP_IN_RESET_RESUME, /* has no union descriptor */
1685         },
1686
1687         /* Nokia S60 phones expose two ACM channels. The first is
1688          * a modem and is picked up by the standard AT-command
1689          * information below. The second is 'vendor-specific' but
1690          * is treated as a serial device at the S60 end, so we want
1691          * to expose it on Linux too. */
1692         { NOKIA_PCSUITE_ACM_INFO(0x042D), }, /* Nokia 3250 */
1693         { NOKIA_PCSUITE_ACM_INFO(0x04D8), }, /* Nokia 5500 Sport */
1694         { NOKIA_PCSUITE_ACM_INFO(0x04C9), }, /* Nokia E50 */
1695         { NOKIA_PCSUITE_ACM_INFO(0x0419), }, /* Nokia E60 */
1696         { NOKIA_PCSUITE_ACM_INFO(0x044D), }, /* Nokia E61 */
1697         { NOKIA_PCSUITE_ACM_INFO(0x0001), }, /* Nokia E61i */
1698         { NOKIA_PCSUITE_ACM_INFO(0x0475), }, /* Nokia E62 */
1699         { NOKIA_PCSUITE_ACM_INFO(0x0508), }, /* Nokia E65 */
1700         { NOKIA_PCSUITE_ACM_INFO(0x0418), }, /* Nokia E70 */
1701         { NOKIA_PCSUITE_ACM_INFO(0x0425), }, /* Nokia N71 */
1702         { NOKIA_PCSUITE_ACM_INFO(0x0486), }, /* Nokia N73 */
1703         { NOKIA_PCSUITE_ACM_INFO(0x04DF), }, /* Nokia N75 */
1704         { NOKIA_PCSUITE_ACM_INFO(0x000e), }, /* Nokia N77 */
1705         { NOKIA_PCSUITE_ACM_INFO(0x0445), }, /* Nokia N80 */
1706         { NOKIA_PCSUITE_ACM_INFO(0x042F), }, /* Nokia N91 & N91 8GB */
1707         { NOKIA_PCSUITE_ACM_INFO(0x048E), }, /* Nokia N92 */
1708         { NOKIA_PCSUITE_ACM_INFO(0x0420), }, /* Nokia N93 */
1709         { NOKIA_PCSUITE_ACM_INFO(0x04E6), }, /* Nokia N93i  */
1710         { NOKIA_PCSUITE_ACM_INFO(0x04B2), }, /* Nokia 5700 XpressMusic */
1711         { NOKIA_PCSUITE_ACM_INFO(0x0134), }, /* Nokia 6110 Navigator (China) */
1712         { NOKIA_PCSUITE_ACM_INFO(0x046E), }, /* Nokia 6110 Navigator */
1713         { NOKIA_PCSUITE_ACM_INFO(0x002f), }, /* Nokia 6120 classic &  */
1714         { NOKIA_PCSUITE_ACM_INFO(0x0088), }, /* Nokia 6121 classic */
1715         { NOKIA_PCSUITE_ACM_INFO(0x00fc), }, /* Nokia 6124 classic */
1716         { NOKIA_PCSUITE_ACM_INFO(0x0042), }, /* Nokia E51 */
1717         { NOKIA_PCSUITE_ACM_INFO(0x00b0), }, /* Nokia E66 */
1718         { NOKIA_PCSUITE_ACM_INFO(0x00ab), }, /* Nokia E71 */
1719         { NOKIA_PCSUITE_ACM_INFO(0x0481), }, /* Nokia N76 */
1720         { NOKIA_PCSUITE_ACM_INFO(0x0007), }, /* Nokia N81 & N81 8GB */
1721         { NOKIA_PCSUITE_ACM_INFO(0x0071), }, /* Nokia N82 */
1722         { NOKIA_PCSUITE_ACM_INFO(0x04F0), }, /* Nokia N95 & N95-3 NAM */
1723         { NOKIA_PCSUITE_ACM_INFO(0x0070), }, /* Nokia N95 8GB  */
1724         { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
1725         { NOKIA_PCSUITE_ACM_INFO(0x0099), }, /* Nokia 6210 Navigator, RM-367 */
1726         { NOKIA_PCSUITE_ACM_INFO(0x0128), }, /* Nokia 6210 Navigator, RM-419 */
1727         { NOKIA_PCSUITE_ACM_INFO(0x008f), }, /* Nokia 6220 Classic */
1728         { NOKIA_PCSUITE_ACM_INFO(0x00a0), }, /* Nokia 6650 */
1729         { NOKIA_PCSUITE_ACM_INFO(0x007b), }, /* Nokia N78 */
1730         { NOKIA_PCSUITE_ACM_INFO(0x0094), }, /* Nokia N85 */
1731         { NOKIA_PCSUITE_ACM_INFO(0x003a), }, /* Nokia N96 & N96-3  */
1732         { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
1733         { NOKIA_PCSUITE_ACM_INFO(0x0108), }, /* Nokia 5320 XpressMusic 2G */
1734         { NOKIA_PCSUITE_ACM_INFO(0x01f5), }, /* Nokia N97, RM-505 */
1735         { NOKIA_PCSUITE_ACM_INFO(0x02e3), }, /* Nokia 5230, RM-588 */
1736         { NOKIA_PCSUITE_ACM_INFO(0x0178), }, /* Nokia E63 */
1737         { NOKIA_PCSUITE_ACM_INFO(0x010e), }, /* Nokia E75 */
1738         { NOKIA_PCSUITE_ACM_INFO(0x02d9), }, /* Nokia 6760 Slide */
1739         { NOKIA_PCSUITE_ACM_INFO(0x01d0), }, /* Nokia E52 */
1740         { NOKIA_PCSUITE_ACM_INFO(0x0223), }, /* Nokia E72 */
1741         { NOKIA_PCSUITE_ACM_INFO(0x0275), }, /* Nokia X6 */
1742         { NOKIA_PCSUITE_ACM_INFO(0x026c), }, /* Nokia N97 Mini */
1743         { NOKIA_PCSUITE_ACM_INFO(0x0154), }, /* Nokia 5800 XpressMusic */
1744         { NOKIA_PCSUITE_ACM_INFO(0x04ce), }, /* Nokia E90 */
1745         { NOKIA_PCSUITE_ACM_INFO(0x01d4), }, /* Nokia E55 */
1746         { NOKIA_PCSUITE_ACM_INFO(0x0302), }, /* Nokia N8 */
1747         { NOKIA_PCSUITE_ACM_INFO(0x0335), }, /* Nokia E7 */
1748         { NOKIA_PCSUITE_ACM_INFO(0x03cd), }, /* Nokia C7 */
1749         { SAMSUNG_PCSUITE_ACM_INFO(0x6651), }, /* Samsung GTi8510 (INNOV8) */
1750
1751         /* Support for Owen devices */
1752         { USB_DEVICE(0x03eb, 0x0030), }, /* Owen SI30 */
1753
1754         /* NOTE: non-Nokia COMM/ACM/0xff is likely MSFT RNDIS... NOT a modem! */
1755
1756         /* Support Lego NXT using pbLua firmware */
1757         { USB_DEVICE(0x0694, 0xff00),
1758         .driver_info = NOT_A_MODEM,
1759         },
1760
1761         /* Support for Droids MuIn LCD */
1762         { USB_DEVICE(0x04d8, 0x000b),
1763         .driver_info = NO_DATA_INTERFACE,
1764         },
1765
1766 #if IS_ENABLED(CONFIG_INPUT_IMS_PCU)
1767         { USB_DEVICE(0x04d8, 0x0082),   /* Application mode */
1768         .driver_info = IGNORE_DEVICE,
1769         },
1770         { USB_DEVICE(0x04d8, 0x0083),   /* Bootloader mode */
1771         .driver_info = IGNORE_DEVICE,
1772         },
1773 #endif
1774
1775         /* Exclude XMM6260 boot rom (not running modem software yet) */
1776         { USB_DEVICE(0x058b, 0x0041),
1777         .driver_info = IGNORE_DEVICE,
1778         },
1779
1780         /* Icera 450 */
1781         { USB_DEVICE(0x1983, 0x0321),
1782         .driver_info = NO_HANGUP_IN_RESET_RESUME,
1783         },
1784
1785         /* control interfaces without any protocol set */
1786         { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1787                 USB_CDC_PROTO_NONE) },
1788
1789         /* control interfaces with various AT-command sets */
1790         { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1791                 USB_CDC_ACM_PROTO_AT_V25TER) },
1792         { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1793                 USB_CDC_ACM_PROTO_AT_PCCA101) },
1794         { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1795                 USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
1796         { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1797                 USB_CDC_ACM_PROTO_AT_GSM) },
1798         { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1799                 USB_CDC_ACM_PROTO_AT_3G) },
1800         { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1801                 USB_CDC_ACM_PROTO_AT_CDMA) },
1802
1803         { }
1804 };
1805
1806 MODULE_DEVICE_TABLE(usb, acm_ids);
1807
1808 static struct usb_driver acm_driver = {
1809         .name =         "cdc_acm",
1810         .probe =        acm_probe,
1811         .disconnect =   acm_disconnect,
1812 #ifdef CONFIG_PM
1813         .suspend =      acm_suspend,
1814         .resume =       acm_resume,
1815         .reset_resume = acm_reset_resume,
1816 #endif
1817         .id_table =     acm_ids,
1818 #ifdef CONFIG_PM
1819         .supports_autosuspend = 1,
1820 #endif
1821         .disable_hub_initiated_lpm = 1,
1822 };
1823
1824 /*
1825  * TTY driver structures.
1826  */
1827
1828 static const struct tty_operations acm_ops = {
1829         .install =              acm_tty_install,
1830         .open =                 acm_tty_open,
1831         .close =                acm_tty_close,
1832         .cleanup =              acm_tty_cleanup,
1833         .hangup =               acm_tty_hangup,
1834         .write =                acm_tty_write,
1835         .write_room =           acm_tty_write_room,
1836         .ioctl =                acm_tty_ioctl,
1837         .throttle =             acm_tty_throttle,
1838         .unthrottle =           acm_tty_unthrottle,
1839         .chars_in_buffer =      acm_tty_chars_in_buffer,
1840         .break_ctl =            acm_tty_break_ctl,
1841         .set_termios =          acm_tty_set_termios,
1842         .tiocmget =             acm_tty_tiocmget,
1843         .tiocmset =             acm_tty_tiocmset,
1844 };
1845
1846 /*
1847  * Init / exit.
1848  */
1849
1850 static int __init acm_init(void)
1851 {
1852         int retval;
1853         acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
1854         if (!acm_tty_driver)
1855                 return -ENOMEM;
1856         acm_tty_driver->driver_name = "acm",
1857         acm_tty_driver->name = "ttyACM",
1858         acm_tty_driver->major = ACM_TTY_MAJOR,
1859         acm_tty_driver->minor_start = 0,
1860         acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
1861         acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
1862         acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1863         acm_tty_driver->init_termios = tty_std_termios;
1864         acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD |
1865                                                                 HUPCL | CLOCAL;
1866         tty_set_operations(acm_tty_driver, &acm_ops);
1867
1868         retval = tty_register_driver(acm_tty_driver);
1869         if (retval) {
1870                 put_tty_driver(acm_tty_driver);
1871                 return retval;
1872         }
1873
1874         retval = usb_register(&acm_driver);
1875         if (retval) {
1876                 tty_unregister_driver(acm_tty_driver);
1877                 put_tty_driver(acm_tty_driver);
1878                 return retval;
1879         }
1880
1881         printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
1882
1883         return 0;
1884 }
1885
1886 static void __exit acm_exit(void)
1887 {
1888         usb_deregister(&acm_driver);
1889         tty_unregister_driver(acm_tty_driver);
1890         put_tty_driver(acm_tty_driver);
1891 }
1892
1893 module_init(acm_init);
1894 module_exit(acm_exit);
1895
1896 MODULE_AUTHOR(DRIVER_AUTHOR);
1897 MODULE_DESCRIPTION(DRIVER_DESC);
1898 MODULE_LICENSE("GPL");
1899 MODULE_ALIAS_CHARDEV_MAJOR(ACM_TTY_MAJOR);