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