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