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