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