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