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