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