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