USB: remove use of err() in drivers/usb/serial
[deliverable/linux.git] / drivers / usb / class / cdc-acm.c
CommitLineData
1da177e4
LT
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>
61a87adf 9 * Copyright (c) 2005 David Kubicek <dave@awk.cz>
1da177e4
LT
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 kmalloced
20 * v0.13 - added termios, added hangup
21 * v0.14 - sized down struct acm
22 * v0.15 - fixed flow control again - characters could be lost
23 * v0.16 - added code for modems with swapped data and control interfaces
24 * v0.17 - added new style probing
25 * v0.18 - fixed new style probing for devices with more configurations
26 * v0.19 - fixed CLOCAL handling (thanks to Richard Shih-Ping Chan)
27 * v0.20 - switched to probing on interface (rather than device) class
28 * v0.21 - revert to probing on device for devices with multiple configs
29 * v0.22 - probe only the control interface. if usbcore doesn't choose the
30 * config we want, sysadmin changes bConfigurationValue in sysfs.
31 * v0.23 - use softirq for rx processing, as needed by tty layer
32 * v0.24 - change probe method to evaluate CDC union descriptor
61a87adf 33 * v0.25 - downstream tasks paralelized to maximize throughput
e4cf3aa8 34 * v0.26 - multiple write urbs, writesize increased
1da177e4
LT
35 */
36
37/*
38 * This program is free software; you can redistribute it and/or modify
39 * it under the terms of the GNU General Public License as published by
40 * the Free Software Foundation; either version 2 of the License, or
41 * (at your option) any later version.
42 *
43 * This program is distributed in the hope that it will be useful,
44 * but WITHOUT ANY WARRANTY; without even the implied warranty of
45 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 * GNU General Public License for more details.
47 *
48 * You should have received a copy of the GNU General Public License
49 * along with this program; if not, write to the Free Software
50 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
51 */
52
53#undef DEBUG
e5fbab51 54#undef VERBOSE_DEBUG
1da177e4
LT
55
56#include <linux/kernel.h>
57#include <linux/errno.h>
58#include <linux/init.h>
59#include <linux/slab.h>
60#include <linux/tty.h>
61#include <linux/tty_driver.h>
62#include <linux/tty_flip.h>
63#include <linux/module.h>
4186ecf8 64#include <linux/mutex.h>
1da177e4
LT
65#include <asm/uaccess.h>
66#include <linux/usb.h>
a8c28f23 67#include <linux/usb/cdc.h>
1da177e4
LT
68#include <asm/byteorder.h>
69#include <asm/unaligned.h>
61a87adf 70#include <linux/list.h>
1da177e4
LT
71
72#include "cdc-acm.h"
73
e5fbab51
DB
74
75#define ACM_CLOSE_TIMEOUT 15 /* seconds to let writes drain */
76
1da177e4
LT
77/*
78 * Version Information
79 */
e4cf3aa8 80#define DRIVER_VERSION "v0.26"
61a87adf 81#define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek"
1da177e4
LT
82#define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
83
84static struct usb_driver acm_driver;
85static struct tty_driver *acm_tty_driver;
86static struct acm *acm_table[ACM_TTY_MINORS];
87
4186ecf8 88static DEFINE_MUTEX(open_mutex);
1da177e4
LT
89
90#define ACM_READY(acm) (acm && acm->dev && acm->used)
91
e5fbab51
DB
92#ifdef VERBOSE_DEBUG
93#define verbose 1
94#else
95#define verbose 0
96#endif
97
1da177e4
LT
98/*
99 * Functions for ACM control messages.
100 */
101
102static int acm_ctrl_msg(struct acm *acm, int request, int value, void *buf, int len)
103{
104 int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
105 request, USB_RT_ACM, value,
106 acm->control->altsetting[0].desc.bInterfaceNumber,
107 buf, len, 5000);
108 dbg("acm_control_msg: rq: 0x%02x val: %#x len: %#x result: %d", request, value, len, retval);
109 return retval < 0 ? retval : 0;
110}
111
112/* devices aren't required to support these requests.
113 * the cdc acm descriptor tells whether they do...
114 */
115#define acm_set_control(acm, control) \
116 acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0)
117#define acm_set_line(acm, line) \
118 acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
119#define acm_send_break(acm, ms) \
120 acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
121
884b600f
ON
122/*
123 * Write buffer management.
124 * All of these assume proper locks taken by the caller.
125 */
126
127static int acm_wb_alloc(struct acm *acm)
128{
129 int i, wbn;
130 struct acm_wb *wb;
131
e4cf3aa8 132 wbn = 0;
884b600f
ON
133 i = 0;
134 for (;;) {
135 wb = &acm->wb[wbn];
136 if (!wb->use) {
137 wb->use = 1;
138 return wbn;
139 }
86478944
ON
140 wbn = (wbn + 1) % ACM_NW;
141 if (++i >= ACM_NW)
884b600f
ON
142 return -1;
143 }
144}
145
884b600f
ON
146static int acm_wb_is_avail(struct acm *acm)
147{
148 int i, n;
e5fbab51 149 unsigned long flags;
884b600f 150
86478944 151 n = ACM_NW;
e5fbab51 152 spin_lock_irqsave(&acm->write_lock, flags);
86478944
ON
153 for (i = 0; i < ACM_NW; i++) {
154 n -= acm->wb[i].use;
884b600f 155 }
e5fbab51 156 spin_unlock_irqrestore(&acm->write_lock, flags);
884b600f
ON
157 return n;
158}
159
884b600f
ON
160/*
161 * Finish write.
162 */
e4cf3aa8 163static void acm_write_done(struct acm *acm, struct acm_wb *wb)
884b600f
ON
164{
165 unsigned long flags;
884b600f
ON
166
167 spin_lock_irqsave(&acm->write_lock, flags);
e4cf3aa8 168 wb->use = 0;
11ea859d 169 acm->transmitting--;
884b600f
ON
170 spin_unlock_irqrestore(&acm->write_lock, flags);
171}
172
173/*
174 * Poke write.
11ea859d
ON
175 *
176 * the caller is responsible for locking
884b600f 177 */
11ea859d
ON
178
179static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
180{
181 int rc;
182
183 acm->transmitting++;
184
185 wb->urb->transfer_buffer = wb->buf;
186 wb->urb->transfer_dma = wb->dmah;
187 wb->urb->transfer_buffer_length = wb->len;
188 wb->urb->dev = acm->dev;
189
190 if ((rc = usb_submit_urb(wb->urb, GFP_ATOMIC)) < 0) {
191 dbg("usb_submit_urb(write bulk) failed: %d", rc);
192 acm_write_done(acm, wb);
193 }
194 return rc;
195}
196
e4cf3aa8 197static int acm_write_start(struct acm *acm, int wbn)
884b600f
ON
198{
199 unsigned long flags;
934da463 200 struct acm_wb *wb = &acm->wb[wbn];
884b600f
ON
201 int rc;
202
203 spin_lock_irqsave(&acm->write_lock, flags);
204 if (!acm->dev) {
934da463 205 wb->use = 0;
884b600f
ON
206 spin_unlock_irqrestore(&acm->write_lock, flags);
207 return -ENODEV;
208 }
209
11ea859d
ON
210 dbg("%s susp_count: %d", __func__, acm->susp_count);
211 if (acm->susp_count) {
11ea859d 212 acm->delayed_wb = wb;
11ea859d
ON
213 schedule_work(&acm->waker);
214 spin_unlock_irqrestore(&acm->write_lock, flags);
215 return 0; /* A white lie */
216 }
217 usb_mark_last_busy(acm->dev);
218
11ea859d 219 rc = acm_start_wb(acm, wb);
884b600f
ON
220 spin_unlock_irqrestore(&acm->write_lock, flags);
221
884b600f 222 return rc;
11ea859d 223
884b600f 224}
c4cabd28
ON
225/*
226 * attributes exported through sysfs
227 */
228static ssize_t show_caps
229(struct device *dev, struct device_attribute *attr, char *buf)
230{
231 struct usb_interface *intf = to_usb_interface(dev);
232 struct acm *acm = usb_get_intfdata(intf);
233
234 return sprintf(buf, "%d", acm->ctrl_caps);
235}
236static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
237
238static ssize_t show_country_codes
239(struct device *dev, struct device_attribute *attr, char *buf)
240{
241 struct usb_interface *intf = to_usb_interface(dev);
242 struct acm *acm = usb_get_intfdata(intf);
243
244 memcpy(buf, acm->country_codes, acm->country_code_size);
245 return acm->country_code_size;
246}
247
248static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
249
250static ssize_t show_country_rel_date
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 return sprintf(buf, "%d", acm->country_rel_date);
257}
884b600f 258
c4cabd28 259static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
1da177e4
LT
260/*
261 * Interrupt handlers for various ACM device responses
262 */
263
264/* control interface reports status changes with "interrupt" transfers */
7d12e780 265static void acm_ctrl_irq(struct urb *urb)
1da177e4
LT
266{
267 struct acm *acm = urb->context;
268 struct usb_cdc_notification *dr = urb->transfer_buffer;
269 unsigned char *data;
270 int newctrl;
185d4058
GKH
271 int retval;
272 int status = urb->status;
1da177e4 273
185d4058 274 switch (status) {
1da177e4
LT
275 case 0:
276 /* success */
277 break;
278 case -ECONNRESET:
279 case -ENOENT:
280 case -ESHUTDOWN:
281 /* this urb is terminated, clean up */
441b62c1 282 dbg("%s - urb shutting down with status: %d", __func__, status);
1da177e4
LT
283 return;
284 default:
441b62c1 285 dbg("%s - nonzero urb status received: %d", __func__, status);
1da177e4
LT
286 goto exit;
287 }
288
289 if (!ACM_READY(acm))
290 goto exit;
291
292 data = (unsigned char *)(dr + 1);
293 switch (dr->bNotificationType) {
294
295 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
296
297 dbg("%s network", dr->wValue ? "connected to" : "disconnected from");
298 break;
299
300 case USB_CDC_NOTIFY_SERIAL_STATE:
301
a5abdeaf 302 newctrl = get_unaligned_le16(data);
1da177e4
LT
303
304 if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
305 dbg("calling hangup");
306 tty_hangup(acm->tty);
307 }
308
309 acm->ctrlin = newctrl;
310
311 dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c",
312 acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
313 acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
314 acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-', acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
315 acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
316
317 break;
318
319 default:
320 dbg("unknown notification %d received: index %d len %d data0 %d data1 %d",
321 dr->bNotificationType, dr->wIndex,
322 dr->wLength, data[0], data[1]);
323 break;
324 }
325exit:
11ea859d 326 usb_mark_last_busy(acm->dev);
185d4058
GKH
327 retval = usb_submit_urb (urb, GFP_ATOMIC);
328 if (retval)
1da177e4 329 err ("%s - usb_submit_urb failed with result %d",
441b62c1 330 __func__, retval);
1da177e4
LT
331}
332
333/* data interface returns incoming bytes, or we got unthrottled */
7d12e780 334static void acm_read_bulk(struct urb *urb)
1da177e4 335{
61a87adf
DK
336 struct acm_rb *buf;
337 struct acm_ru *rcv = urb->context;
338 struct acm *acm = rcv->instance;
86478944 339 int status = urb->status;
185d4058
GKH
340
341 dbg("Entering acm_read_bulk with status %d", status);
1da177e4 342
11ea859d
ON
343 if (!ACM_READY(acm)) {
344 dev_dbg(&acm->data->dev, "Aborting, acm not ready");
1da177e4 345 return;
11ea859d
ON
346 }
347 usb_mark_last_busy(acm->dev);
1da177e4 348
86478944 349 if (status)
898eb71c 350 dev_dbg(&acm->data->dev, "bulk rx status %d\n", status);
1da177e4 351
61a87adf
DK
352 buf = rcv->buffer;
353 buf->size = urb->actual_length;
354
86478944
ON
355 if (likely(status == 0)) {
356 spin_lock(&acm->read_lock);
11ea859d 357 acm->processing++;
86478944
ON
358 list_add_tail(&rcv->list, &acm->spare_read_urbs);
359 list_add_tail(&buf->list, &acm->filled_read_bufs);
360 spin_unlock(&acm->read_lock);
361 } else {
362 /* we drop the buffer due to an error */
363 spin_lock(&acm->read_lock);
364 list_add_tail(&rcv->list, &acm->spare_read_urbs);
365 list_add(&buf->list, &acm->spare_read_bufs);
366 spin_unlock(&acm->read_lock);
367 /* nevertheless the tasklet must be kicked unconditionally
368 so the queue cannot dry up */
369 }
11ea859d
ON
370 if (likely(!acm->susp_count))
371 tasklet_schedule(&acm->urb_task);
1da177e4
LT
372}
373
374static void acm_rx_tasklet(unsigned long _acm)
375{
376 struct acm *acm = (void *)_acm;
61a87adf 377 struct acm_rb *buf;
1da177e4 378 struct tty_struct *tty = acm->tty;
61a87adf 379 struct acm_ru *rcv;
762f007b 380 unsigned long flags;
ca79b7b4 381 unsigned char throttled;
11ea859d 382
1da177e4
LT
383 dbg("Entering acm_rx_tasklet");
384
ca79b7b4 385 if (!ACM_READY(acm))
11ea859d
ON
386 {
387 dbg("acm_rx_tasklet: ACM not ready");
ca79b7b4 388 return;
11ea859d 389 }
ca79b7b4 390
834dbca5 391 spin_lock_irqsave(&acm->throttle_lock, flags);
ca79b7b4 392 throttled = acm->throttle;
834dbca5 393 spin_unlock_irqrestore(&acm->throttle_lock, flags);
ca79b7b4 394 if (throttled)
11ea859d
ON
395 {
396 dbg("acm_rx_tasklet: throttled");
61a87adf 397 return;
11ea859d 398 }
61a87adf
DK
399
400next_buffer:
762f007b 401 spin_lock_irqsave(&acm->read_lock, flags);
61a87adf 402 if (list_empty(&acm->filled_read_bufs)) {
762f007b 403 spin_unlock_irqrestore(&acm->read_lock, flags);
61a87adf 404 goto urbs;
1da177e4 405 }
61a87adf
DK
406 buf = list_entry(acm->filled_read_bufs.next,
407 struct acm_rb, list);
408 list_del(&buf->list);
762f007b 409 spin_unlock_irqrestore(&acm->read_lock, flags);
61a87adf 410
3dd2ae81 411 dbg("acm_rx_tasklet: procesing buf 0x%p, size = %d", buf, buf->size);
61a87adf 412
33f0f88f 413 tty_buffer_request_room(tty, buf->size);
834dbca5 414 spin_lock_irqsave(&acm->throttle_lock, flags);
ca79b7b4 415 throttled = acm->throttle;
834dbca5 416 spin_unlock_irqrestore(&acm->throttle_lock, flags);
ca79b7b4 417 if (!throttled)
33f0f88f 418 tty_insert_flip_string(tty, buf->base, buf->size);
61a87adf 419 tty_flip_buffer_push(tty);
1da177e4 420
ca79b7b4
ON
421 if (throttled) {
422 dbg("Throttling noticed");
762f007b 423 spin_lock_irqsave(&acm->read_lock, flags);
61a87adf 424 list_add(&buf->list, &acm->filled_read_bufs);
762f007b 425 spin_unlock_irqrestore(&acm->read_lock, flags);
1da177e4
LT
426 return;
427 }
1da177e4 428
762f007b 429 spin_lock_irqsave(&acm->read_lock, flags);
61a87adf 430 list_add(&buf->list, &acm->spare_read_bufs);
762f007b 431 spin_unlock_irqrestore(&acm->read_lock, flags);
61a87adf
DK
432 goto next_buffer;
433
434urbs:
435 while (!list_empty(&acm->spare_read_bufs)) {
762f007b 436 spin_lock_irqsave(&acm->read_lock, flags);
61a87adf 437 if (list_empty(&acm->spare_read_urbs)) {
11ea859d 438 acm->processing = 0;
762f007b 439 spin_unlock_irqrestore(&acm->read_lock, flags);
61a87adf
DK
440 return;
441 }
442 rcv = list_entry(acm->spare_read_urbs.next,
443 struct acm_ru, list);
444 list_del(&rcv->list);
762f007b 445 spin_unlock_irqrestore(&acm->read_lock, flags);
61a87adf
DK
446
447 buf = list_entry(acm->spare_read_bufs.next,
448 struct acm_rb, list);
449 list_del(&buf->list);
450
451 rcv->buffer = buf;
452
453 usb_fill_bulk_urb(rcv->urb, acm->dev,
454 acm->rx_endpoint,
455 buf->base,
456 acm->readsize,
457 acm_read_bulk, rcv);
458 rcv->urb->transfer_dma = buf->dma;
459 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
460
61a87adf
DK
461 /* This shouldn't kill the driver as unsuccessful URBs are returned to the
462 free-urbs-pool and resubmited ASAP */
11ea859d
ON
463 spin_lock_irqsave(&acm->read_lock, flags);
464 if (acm->susp_count || usb_submit_urb(rcv->urb, GFP_ATOMIC) < 0) {
61a87adf 465 list_add(&buf->list, &acm->spare_read_bufs);
61a87adf 466 list_add(&rcv->list, &acm->spare_read_urbs);
11ea859d 467 acm->processing = 0;
762f007b 468 spin_unlock_irqrestore(&acm->read_lock, flags);
61a87adf 469 return;
11ea859d
ON
470 } else {
471 spin_unlock_irqrestore(&acm->read_lock, flags);
472 dbg("acm_rx_tasklet: sending urb 0x%p, rcv 0x%p, buf 0x%p", rcv->urb, rcv, buf);
61a87adf
DK
473 }
474 }
11ea859d
ON
475 spin_lock_irqsave(&acm->read_lock, flags);
476 acm->processing = 0;
477 spin_unlock_irqrestore(&acm->read_lock, flags);
1da177e4
LT
478}
479
480/* data interface wrote those outgoing bytes */
7d12e780 481static void acm_write_bulk(struct urb *urb)
1da177e4 482{
cdc97792 483 struct acm_wb *wb = urb->context;
e5fbab51 484 struct acm *acm = wb->instance;
1da177e4 485
e5fbab51
DB
486 if (verbose || urb->status
487 || (urb->actual_length != urb->transfer_buffer_length))
488 dev_dbg(&acm->data->dev, "tx %d/%d bytes -- > %d\n",
489 urb->actual_length,
490 urb->transfer_buffer_length,
491 urb->status);
1da177e4 492
e4cf3aa8 493 acm_write_done(acm, wb);
884b600f
ON
494 if (ACM_READY(acm))
495 schedule_work(&acm->work);
e5fbab51
DB
496 else
497 wake_up_interruptible(&acm->drain_wait);
1da177e4
LT
498}
499
c4028958 500static void acm_softint(struct work_struct *work)
1da177e4 501{
c4028958 502 struct acm *acm = container_of(work, struct acm, work);
e5fbab51
DB
503
504 dev_vdbg(&acm->data->dev, "tx work\n");
1da177e4
LT
505 if (!ACM_READY(acm))
506 return;
507 tty_wakeup(acm->tty);
508}
509
11ea859d
ON
510static void acm_waker(struct work_struct *waker)
511{
512 struct acm *acm = container_of(waker, struct acm, waker);
11ea859d
ON
513 int rv;
514
515 rv = usb_autopm_get_interface(acm->control);
516 if (rv < 0) {
517 err("Autopm failure in %s", __func__);
518 return;
519 }
520 if (acm->delayed_wb) {
521 acm_start_wb(acm, acm->delayed_wb);
522 acm->delayed_wb = NULL;
523 }
11ea859d
ON
524 usb_autopm_put_interface(acm->control);
525}
526
1da177e4
LT
527/*
528 * TTY handlers
529 */
530
531static int acm_tty_open(struct tty_struct *tty, struct file *filp)
532{
533 struct acm *acm;
534 int rv = -EINVAL;
61a87adf 535 int i;
3dd2ae81 536 dbg("Entering acm_tty_open.");
4186ecf8
AV
537
538 mutex_lock(&open_mutex);
1da177e4
LT
539
540 acm = acm_table[tty->index];
541 if (!acm || !acm->dev)
542 goto err_out;
543 else
544 rv = 0;
545
28d1dfad 546 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
1da177e4
LT
547 tty->driver_data = acm;
548 acm->tty = tty;
549
61a87adf
DK
550 /* force low_latency on so that our tty_push actually forces the data through,
551 otherwise it is scheduled, and with high data rates data can get lost. */
552 tty->low_latency = 1;
1da177e4 553
94409cc1
ON
554 if (usb_autopm_get_interface(acm->control) < 0)
555 goto early_bail;
11ea859d
ON
556 else
557 acm->control->needs_remote_wakeup = 1;
1365baf7
ON
558
559 mutex_lock(&acm->mutex);
1da177e4 560 if (acm->used++) {
1365baf7 561 usb_autopm_put_interface(acm->control);
1da177e4
LT
562 goto done;
563 }
564
1365baf7 565
1da177e4
LT
566 acm->ctrlurb->dev = acm->dev;
567 if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
568 dbg("usb_submit_urb(ctrl irq) failed");
569 goto bail_out;
570 }
571
ca79b7b4
ON
572 if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS) &&
573 (acm->ctrl_caps & USB_CDC_CAP_LINE))
1da177e4 574 goto full_bailout;
11ea859d 575 usb_autopm_put_interface(acm->control);
1da177e4 576
61a87adf
DK
577 INIT_LIST_HEAD(&acm->spare_read_urbs);
578 INIT_LIST_HEAD(&acm->spare_read_bufs);
579 INIT_LIST_HEAD(&acm->filled_read_bufs);
86478944 580 for (i = 0; i < acm->rx_buflimit; i++) {
61a87adf
DK
581 list_add(&(acm->ru[i].list), &acm->spare_read_urbs);
582 }
86478944 583 for (i = 0; i < acm->rx_buflimit; i++) {
61a87adf
DK
584 list_add(&(acm->rb[i].list), &acm->spare_read_bufs);
585 }
586
ca79b7b4
ON
587 acm->throttle = 0;
588
61a87adf 589 tasklet_schedule(&acm->urb_task);
1da177e4
LT
590
591done:
1365baf7 592 mutex_unlock(&acm->mutex);
74573ee7 593err_out:
94409cc1 594 mutex_unlock(&open_mutex);
1da177e4
LT
595 return rv;
596
597full_bailout:
1da177e4
LT
598 usb_kill_urb(acm->ctrlurb);
599bail_out:
1365baf7 600 usb_autopm_put_interface(acm->control);
1da177e4 601 acm->used--;
1365baf7 602 mutex_unlock(&acm->mutex);
94409cc1
ON
603early_bail:
604 mutex_unlock(&open_mutex);
1da177e4
LT
605 return -EIO;
606}
607
83ef344a 608static void acm_tty_unregister(struct acm *acm)
609{
86478944 610 int i,nr;
61a87adf 611
86478944 612 nr = acm->rx_buflimit;
83ef344a 613 tty_unregister_device(acm_tty_driver, acm->minor);
614 usb_put_intf(acm->control);
615 acm_table[acm->minor] = NULL;
616 usb_free_urb(acm->ctrlurb);
e4cf3aa8
DE
617 for (i = 0; i < ACM_NW; i++)
618 usb_free_urb(acm->wb[i].urb);
86478944 619 for (i = 0; i < nr; i++)
61a87adf 620 usb_free_urb(acm->ru[i].urb);
c4cabd28 621 kfree(acm->country_codes);
83ef344a 622 kfree(acm);
623}
624
e5fbab51
DB
625static int acm_tty_chars_in_buffer(struct tty_struct *tty);
626
1da177e4
LT
627static void acm_tty_close(struct tty_struct *tty, struct file *filp)
628{
629 struct acm *acm = tty->driver_data;
86478944 630 int i,nr;
1da177e4
LT
631
632 if (!acm || !acm->used)
633 return;
634
86478944 635 nr = acm->rx_buflimit;
4186ecf8 636 mutex_lock(&open_mutex);
1da177e4
LT
637 if (!--acm->used) {
638 if (acm->dev) {
11ea859d 639 usb_autopm_get_interface(acm->control);
1da177e4 640 acm_set_control(acm, acm->ctrlout = 0);
e5fbab51
DB
641
642 /* try letting the last writes drain naturally */
643 wait_event_interruptible_timeout(acm->drain_wait,
644 (ACM_NW == acm_wb_is_avail(acm))
645 || !acm->dev,
646 ACM_CLOSE_TIMEOUT * HZ);
647
1da177e4 648 usb_kill_urb(acm->ctrlurb);
e4cf3aa8
DE
649 for (i = 0; i < ACM_NW; i++)
650 usb_kill_urb(acm->wb[i].urb);
86478944 651 for (i = 0; i < nr; i++)
61a87adf 652 usb_kill_urb(acm->ru[i].urb);
11ea859d 653 acm->control->needs_remote_wakeup = 0;
1365baf7 654 usb_autopm_put_interface(acm->control);
83ef344a 655 } else
656 acm_tty_unregister(acm);
1da177e4 657 }
4186ecf8 658 mutex_unlock(&open_mutex);
1da177e4
LT
659}
660
661static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
662{
663 struct acm *acm = tty->driver_data;
664 int stat;
884b600f
ON
665 unsigned long flags;
666 int wbn;
667 struct acm_wb *wb;
668
3dd2ae81 669 dbg("Entering acm_tty_write to write %d bytes,", count);
1da177e4
LT
670
671 if (!ACM_READY(acm))
672 return -EINVAL;
1da177e4
LT
673 if (!count)
674 return 0;
675
884b600f
ON
676 spin_lock_irqsave(&acm->write_lock, flags);
677 if ((wbn = acm_wb_alloc(acm)) < 0) {
678 spin_unlock_irqrestore(&acm->write_lock, flags);
884b600f
ON
679 return 0;
680 }
681 wb = &acm->wb[wbn];
1da177e4 682
884b600f 683 count = (count > acm->writesize) ? acm->writesize : count;
1da177e4 684 dbg("Get %d bytes...", count);
884b600f
ON
685 memcpy(wb->buf, buf, count);
686 wb->len = count;
687 spin_unlock_irqrestore(&acm->write_lock, flags);
1da177e4 688
e4cf3aa8 689 if ((stat = acm_write_start(acm, wbn)) < 0)
1da177e4 690 return stat;
1da177e4
LT
691 return count;
692}
693
694static int acm_tty_write_room(struct tty_struct *tty)
695{
696 struct acm *acm = tty->driver_data;
697 if (!ACM_READY(acm))
698 return -EINVAL;
884b600f
ON
699 /*
700 * Do not let the line discipline to know that we have a reserve,
701 * or it might get too enthusiastic.
702 */
934da463 703 return acm_wb_is_avail(acm) ? acm->writesize : 0;
1da177e4
LT
704}
705
706static int acm_tty_chars_in_buffer(struct tty_struct *tty)
707{
708 struct acm *acm = tty->driver_data;
709 if (!ACM_READY(acm))
710 return -EINVAL;
884b600f
ON
711 /*
712 * This is inaccurate (overcounts), but it works.
713 */
86478944 714 return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
1da177e4
LT
715}
716
717static void acm_tty_throttle(struct tty_struct *tty)
718{
719 struct acm *acm = tty->driver_data;
720 if (!ACM_READY(acm))
721 return;
722 spin_lock_bh(&acm->throttle_lock);
723 acm->throttle = 1;
724 spin_unlock_bh(&acm->throttle_lock);
725}
726
727static void acm_tty_unthrottle(struct tty_struct *tty)
728{
729 struct acm *acm = tty->driver_data;
730 if (!ACM_READY(acm))
731 return;
732 spin_lock_bh(&acm->throttle_lock);
733 acm->throttle = 0;
734 spin_unlock_bh(&acm->throttle_lock);
61a87adf 735 tasklet_schedule(&acm->urb_task);
1da177e4
LT
736}
737
9e98966c 738static int acm_tty_break_ctl(struct tty_struct *tty, int state)
1da177e4
LT
739{
740 struct acm *acm = tty->driver_data;
9e98966c 741 int retval;
1da177e4 742 if (!ACM_READY(acm))
9e98966c
AC
743 return -EINVAL;
744 retval = acm_send_break(acm, state ? 0xffff : 0);
745 if (retval < 0)
1da177e4 746 dbg("send break failed");
9e98966c 747 return retval;
1da177e4
LT
748}
749
750static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file)
751{
752 struct acm *acm = tty->driver_data;
753
754 if (!ACM_READY(acm))
755 return -EINVAL;
756
757 return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
758 (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
759 (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
760 (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
761 (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
762 TIOCM_CTS;
763}
764
765static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file,
766 unsigned int set, unsigned int clear)
767{
768 struct acm *acm = tty->driver_data;
769 unsigned int newctrl;
770
771 if (!ACM_READY(acm))
772 return -EINVAL;
773
774 newctrl = acm->ctrlout;
775 set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
776 clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
777
778 newctrl = (newctrl & ~clear) | set;
779
780 if (acm->ctrlout == newctrl)
781 return 0;
782 return acm_set_control(acm, acm->ctrlout = newctrl);
783}
784
785static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
786{
787 struct acm *acm = tty->driver_data;
788
789 if (!ACM_READY(acm))
790 return -EINVAL;
791
792 return -ENOIOCTLCMD;
793}
794
4c4c9432 795static const __u32 acm_tty_speed[] = {
1da177e4
LT
796 0, 50, 75, 110, 134, 150, 200, 300, 600,
797 1200, 1800, 2400, 4800, 9600, 19200, 38400,
798 57600, 115200, 230400, 460800, 500000, 576000,
799 921600, 1000000, 1152000, 1500000, 2000000,
800 2500000, 3000000, 3500000, 4000000
801};
802
4c4c9432 803static const __u8 acm_tty_size[] = {
1da177e4
LT
804 5, 6, 7, 8
805};
806
606d099c 807static void acm_tty_set_termios(struct tty_struct *tty, struct ktermios *termios_old)
1da177e4
LT
808{
809 struct acm *acm = tty->driver_data;
606d099c 810 struct ktermios *termios = tty->termios;
1da177e4
LT
811 struct usb_cdc_line_coding newline;
812 int newctrl = acm->ctrlout;
813
814 if (!ACM_READY(acm))
815 return;
816
817 newline.dwDTERate = cpu_to_le32p(acm_tty_speed +
818 (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0));
819 newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
820 newline.bParityType = termios->c_cflag & PARENB ?
821 (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
822 newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
823
824 acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
825
826 if (!newline.dwDTERate) {
827 newline.dwDTERate = acm->line.dwDTERate;
828 newctrl &= ~ACM_CTRL_DTR;
829 } else newctrl |= ACM_CTRL_DTR;
830
831 if (newctrl != acm->ctrlout)
832 acm_set_control(acm, acm->ctrlout = newctrl);
833
834 if (memcmp(&acm->line, &newline, sizeof newline)) {
835 memcpy(&acm->line, &newline, sizeof newline);
836 dbg("set line: %d %d %d %d", le32_to_cpu(newline.dwDTERate),
837 newline.bCharFormat, newline.bParityType,
838 newline.bDataBits);
839 acm_set_line(acm, &acm->line);
840 }
841}
842
843/*
844 * USB probe and disconnect routines.
845 */
846
830f4021 847/* Little helpers: write/read buffers free */
884b600f
ON
848static void acm_write_buffers_free(struct acm *acm)
849{
850 int i;
851 struct acm_wb *wb;
852
86478944 853 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
884b600f
ON
854 usb_buffer_free(acm->dev, acm->writesize, wb->buf, wb->dmah);
855 }
856}
857
830f4021
ON
858static void acm_read_buffers_free(struct acm *acm)
859{
860 struct usb_device *usb_dev = interface_to_usbdev(acm->control);
861 int i, n = acm->rx_buflimit;
862
863 for (i = 0; i < n; i++)
864 usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma);
865}
866
884b600f
ON
867/* Little helper: write buffers allocate */
868static int acm_write_buffers_alloc(struct acm *acm)
869{
870 int i;
871 struct acm_wb *wb;
872
86478944 873 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
884b600f
ON
874 wb->buf = usb_buffer_alloc(acm->dev, acm->writesize, GFP_KERNEL,
875 &wb->dmah);
876 if (!wb->buf) {
877 while (i != 0) {
878 --i;
879 --wb;
880 usb_buffer_free(acm->dev, acm->writesize,
881 wb->buf, wb->dmah);
882 }
883 return -ENOMEM;
884 }
885 }
886 return 0;
887}
888
1da177e4
LT
889static int acm_probe (struct usb_interface *intf,
890 const struct usb_device_id *id)
891{
892 struct usb_cdc_union_desc *union_header = NULL;
c4cabd28 893 struct usb_cdc_country_functional_desc *cfd = NULL;
c6dbf554 894 unsigned char *buffer = intf->altsetting->extra;
1da177e4
LT
895 int buflen = intf->altsetting->extralen;
896 struct usb_interface *control_interface;
897 struct usb_interface *data_interface;
898 struct usb_endpoint_descriptor *epctrl;
899 struct usb_endpoint_descriptor *epread;
900 struct usb_endpoint_descriptor *epwrite;
901 struct usb_device *usb_dev = interface_to_usbdev(intf);
902 struct acm *acm;
903 int minor;
904 int ctrlsize,readsize;
905 u8 *buf;
906 u8 ac_management_function = 0;
907 u8 call_management_function = 0;
908 int call_interface_num = -1;
909 int data_interface_num;
910 unsigned long quirks;
86478944 911 int num_rx_buf;
61a87adf 912 int i;
1da177e4 913
86478944 914 /* normal quirks */
1da177e4 915 quirks = (unsigned long)id->driver_info;
86478944
ON
916 num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
917
918 /* handle quirks deadly to normal probing*/
1da177e4
LT
919 if (quirks == NO_UNION_NORMAL) {
920 data_interface = usb_ifnum_to_if(usb_dev, 1);
921 control_interface = usb_ifnum_to_if(usb_dev, 0);
922 goto skip_normal_probe;
923 }
924
925 /* normal probing*/
926 if (!buffer) {
898eb71c 927 err("Weird descriptor references\n");
1da177e4
LT
928 return -EINVAL;
929 }
930
931 if (!buflen) {
932 if (intf->cur_altsetting->endpoint->extralen && intf->cur_altsetting->endpoint->extra) {
898eb71c 933 dev_dbg(&intf->dev,"Seeking extra descriptors on endpoint\n");
1da177e4
LT
934 buflen = intf->cur_altsetting->endpoint->extralen;
935 buffer = intf->cur_altsetting->endpoint->extra;
936 } else {
937 err("Zero length descriptor references\n");
938 return -EINVAL;
939 }
940 }
941
942 while (buflen > 0) {
943 if (buffer [1] != USB_DT_CS_INTERFACE) {
944 err("skipping garbage\n");
945 goto next_desc;
946 }
947
948 switch (buffer [2]) {
949 case USB_CDC_UNION_TYPE: /* we've found it */
950 if (union_header) {
951 err("More than one union descriptor, skipping ...");
952 goto next_desc;
953 }
954 union_header = (struct usb_cdc_union_desc *)
955 buffer;
956 break;
c4cabd28
ON
957 case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
958 cfd = (struct usb_cdc_country_functional_desc *)buffer;
959 break;
1da177e4
LT
960 case USB_CDC_HEADER_TYPE: /* maybe check version */
961 break; /* for now we ignore it */
962 case USB_CDC_ACM_TYPE:
963 ac_management_function = buffer[3];
964 break;
965 case USB_CDC_CALL_MANAGEMENT_TYPE:
966 call_management_function = buffer[3];
967 call_interface_num = buffer[4];
968 if ((call_management_function & 3) != 3)
969 err("This device cannot do calls on its own. It is no modem.");
970 break;
1da177e4 971 default:
c6dbf554
DB
972 /* there are LOTS more CDC descriptors that
973 * could legitimately be found here.
974 */
975 dev_dbg(&intf->dev, "Ignoring descriptor: "
976 "type %02x, length %d\n",
977 buffer[2], buffer[0]);
1da177e4
LT
978 break;
979 }
980next_desc:
981 buflen -= buffer[0];
982 buffer += buffer[0];
983 }
984
985 if (!union_header) {
986 if (call_interface_num > 0) {
898eb71c 987 dev_dbg(&intf->dev,"No union descriptor, using call management descriptor\n");
1da177e4
LT
988 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
989 control_interface = intf;
990 } else {
898eb71c 991 dev_dbg(&intf->dev,"No union descriptor, giving up\n");
1da177e4
LT
992 return -ENODEV;
993 }
994 } else {
995 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
996 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
997 if (!control_interface || !data_interface) {
898eb71c 998 dev_dbg(&intf->dev,"no interfaces\n");
1da177e4
LT
999 return -ENODEV;
1000 }
1001 }
1002
1003 if (data_interface_num != call_interface_num)
dc0d5c1e 1004 dev_dbg(&intf->dev,"Separate call control interface. That is not fully supported.\n");
1da177e4
LT
1005
1006skip_normal_probe:
1007
1008 /*workaround for switched interfaces */
1009 if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) {
1010 if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) {
1011 struct usb_interface *t;
898eb71c 1012 dev_dbg(&intf->dev,"Your device has switched interfaces.\n");
1da177e4
LT
1013
1014 t = control_interface;
1015 control_interface = data_interface;
1016 data_interface = t;
1017 } else {
1018 return -EINVAL;
1019 }
1020 }
74da5d68
AS
1021
1022 /* Accept probe requests only for the control interface */
1023 if (intf != control_interface)
1024 return -ENODEV;
1da177e4
LT
1025
1026 if (usb_interface_claimed(data_interface)) { /* valid in this context */
898eb71c 1027 dev_dbg(&intf->dev,"The data interface isn't available\n");
1da177e4
LT
1028 return -EBUSY;
1029 }
1030
1031
1032 if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
1033 return -EINVAL;
1034
1035 epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
1036 epread = &data_interface->cur_altsetting->endpoint[0].desc;
1037 epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
1038
1039
1040 /* workaround for switched endpoints */
45aea704 1041 if (!usb_endpoint_dir_in(epread)) {
1da177e4
LT
1042 /* descriptors are swapped */
1043 struct usb_endpoint_descriptor *t;
898eb71c 1044 dev_dbg(&intf->dev,"The data interface has switched endpoints\n");
1da177e4
LT
1045
1046 t = epread;
1047 epread = epwrite;
1048 epwrite = t;
1049 }
1050 dbg("interfaces are valid");
1051 for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
1052
1053 if (minor == ACM_TTY_MINORS) {
1054 err("no more free acm devices");
1055 return -ENODEV;
1056 }
1057
46f116ea 1058 if (!(acm = kzalloc(sizeof(struct acm), GFP_KERNEL))) {
898eb71c 1059 dev_dbg(&intf->dev, "out of memory (acm kzalloc)\n");
1da177e4
LT
1060 goto alloc_fail;
1061 }
1da177e4
LT
1062
1063 ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize);
86478944 1064 readsize = le16_to_cpu(epread->wMaxPacketSize)* ( quirks == SINGLE_RX_URB ? 1 : 2);
e4cf3aa8 1065 acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize) * 20;
1da177e4
LT
1066 acm->control = control_interface;
1067 acm->data = data_interface;
1068 acm->minor = minor;
1069 acm->dev = usb_dev;
1070 acm->ctrl_caps = ac_management_function;
1071 acm->ctrlsize = ctrlsize;
1072 acm->readsize = readsize;
86478944 1073 acm->rx_buflimit = num_rx_buf;
61a87adf
DK
1074 acm->urb_task.func = acm_rx_tasklet;
1075 acm->urb_task.data = (unsigned long) acm;
c4028958 1076 INIT_WORK(&acm->work, acm_softint);
11ea859d 1077 INIT_WORK(&acm->waker, acm_waker);
e5fbab51 1078 init_waitqueue_head(&acm->drain_wait);
1da177e4 1079 spin_lock_init(&acm->throttle_lock);
884b600f 1080 spin_lock_init(&acm->write_lock);
61a87adf 1081 spin_lock_init(&acm->read_lock);
1365baf7 1082 mutex_init(&acm->mutex);
61a87adf 1083 acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
1da177e4
LT
1084
1085 buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
1086 if (!buf) {
898eb71c 1087 dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)\n");
1da177e4
LT
1088 goto alloc_fail2;
1089 }
1090 acm->ctrl_buffer = buf;
1091
884b600f 1092 if (acm_write_buffers_alloc(acm) < 0) {
898eb71c 1093 dev_dbg(&intf->dev, "out of memory (write buffer alloc)\n");
1da177e4
LT
1094 goto alloc_fail4;
1095 }
1da177e4
LT
1096
1097 acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
1098 if (!acm->ctrlurb) {
898eb71c 1099 dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
1da177e4
LT
1100 goto alloc_fail5;
1101 }
86478944 1102 for (i = 0; i < num_rx_buf; i++) {
61a87adf
DK
1103 struct acm_ru *rcv = &(acm->ru[i]);
1104
1105 if (!(rcv->urb = usb_alloc_urb(0, GFP_KERNEL))) {
898eb71c 1106 dev_dbg(&intf->dev, "out of memory (read urbs usb_alloc_urb)\n");
61a87adf
DK
1107 goto alloc_fail7;
1108 }
1109
1110 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1111 rcv->instance = acm;
1112 }
86478944 1113 for (i = 0; i < num_rx_buf; i++) {
672c4e18 1114 struct acm_rb *rb = &(acm->rb[i]);
61a87adf 1115
672c4e18
DB
1116 rb->base = usb_buffer_alloc(acm->dev, readsize,
1117 GFP_KERNEL, &rb->dma);
1118 if (!rb->base) {
898eb71c 1119 dev_dbg(&intf->dev, "out of memory (read bufs usb_buffer_alloc)\n");
61a87adf
DK
1120 goto alloc_fail7;
1121 }
1da177e4 1122 }
e4cf3aa8
DE
1123 for(i = 0; i < ACM_NW; i++)
1124 {
1125 struct acm_wb *snd = &(acm->wb[i]);
1126
1127 if (!(snd->urb = usb_alloc_urb(0, GFP_KERNEL))) {
1128 dev_dbg(&intf->dev, "out of memory (write urbs usb_alloc_urb)");
1129 goto alloc_fail7;
1130 }
1131
1132 usb_fill_bulk_urb(snd->urb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
1133 NULL, acm->writesize, acm_write_bulk, snd);
1134 snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1135 snd->instance = acm;
1da177e4
LT
1136 }
1137
c4cabd28
ON
1138 usb_set_intfdata (intf, acm);
1139
1140 i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
1141 if (i < 0)
1142 goto alloc_fail8;
1143
1144 if (cfd) { /* export the country data */
1145 acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
1146 if (!acm->country_codes)
1147 goto skip_countries;
1148 acm->country_code_size = cfd->bLength - 4;
1149 memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0, cfd->bLength - 4);
1150 acm->country_rel_date = cfd->iCountryCodeRelDate;
1151
1152 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
1153 if (i < 0) {
1154 kfree(acm->country_codes);
1155 goto skip_countries;
1156 }
1157
1158 i = device_create_file(&intf->dev, &dev_attr_iCountryCodeRelDate);
1159 if (i < 0) {
1160 kfree(acm->country_codes);
1161 goto skip_countries;
1162 }
1163 }
1164
1165skip_countries:
1da177e4
LT
1166 usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
1167 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval);
1168 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1169 acm->ctrlurb->transfer_dma = acm->ctrl_dma;
1170
1da177e4
LT
1171 dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
1172
1173 acm_set_control(acm, acm->ctrlout);
1174
1175 acm->line.dwDTERate = cpu_to_le32(9600);
1176 acm->line.bDataBits = 8;
1177 acm_set_line(acm, &acm->line);
1178
1179 usb_driver_claim_interface(&acm_driver, data_interface, acm);
672c4e18 1180 usb_set_intfdata(data_interface, acm);
1da177e4 1181
83ef344a 1182 usb_get_intf(control_interface);
1183 tty_register_device(acm_tty_driver, minor, &control_interface->dev);
1da177e4
LT
1184
1185 acm_table[minor] = acm;
1da177e4 1186
c4cabd28
ON
1187 return 0;
1188alloc_fail8:
e4cf3aa8
DE
1189 for (i = 0; i < ACM_NW; i++)
1190 usb_free_urb(acm->wb[i].urb);
1da177e4 1191alloc_fail7:
830f4021 1192 acm_read_buffers_free(acm);
86478944 1193 for (i = 0; i < num_rx_buf; i++)
61a87adf 1194 usb_free_urb(acm->ru[i].urb);
1da177e4
LT
1195 usb_free_urb(acm->ctrlurb);
1196alloc_fail5:
884b600f 1197 acm_write_buffers_free(acm);
1da177e4 1198alloc_fail4:
1da177e4
LT
1199 usb_buffer_free(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1200alloc_fail2:
1201 kfree(acm);
1202alloc_fail:
1203 return -ENOMEM;
1204}
1205
1365baf7
ON
1206static void stop_data_traffic(struct acm *acm)
1207{
1208 int i;
11ea859d 1209 dbg("Entering stop_data_traffic");
1365baf7
ON
1210
1211 tasklet_disable(&acm->urb_task);
1212
1213 usb_kill_urb(acm->ctrlurb);
e4cf3aa8
DE
1214 for(i = 0; i < ACM_NW; i++)
1215 usb_kill_urb(acm->wb[i].urb);
1365baf7
ON
1216 for (i = 0; i < acm->rx_buflimit; i++)
1217 usb_kill_urb(acm->ru[i].urb);
1218
1365baf7
ON
1219 tasklet_enable(&acm->urb_task);
1220
1221 cancel_work_sync(&acm->work);
11ea859d 1222 cancel_work_sync(&acm->waker);
1365baf7
ON
1223}
1224
1da177e4
LT
1225static void acm_disconnect(struct usb_interface *intf)
1226{
c4cabd28 1227 struct acm *acm = usb_get_intfdata(intf);
1da177e4 1228 struct usb_device *usb_dev = interface_to_usbdev(intf);
1da177e4 1229
672c4e18
DB
1230 /* sibling interface is already cleaning up */
1231 if (!acm)
86067eea 1232 return;
672c4e18
DB
1233
1234 mutex_lock(&open_mutex);
c4cabd28 1235 if (acm->country_codes){
74da5d68
AS
1236 device_remove_file(&acm->control->dev,
1237 &dev_attr_wCountryCodes);
1238 device_remove_file(&acm->control->dev,
1239 &dev_attr_iCountryCodeRelDate);
c4cabd28 1240 }
74da5d68 1241 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
1da177e4 1242 acm->dev = NULL;
86067eea
ON
1243 usb_set_intfdata(acm->control, NULL);
1244 usb_set_intfdata(acm->data, NULL);
1da177e4 1245
1365baf7 1246 stop_data_traffic(acm);
1da177e4 1247
884b600f 1248 acm_write_buffers_free(acm);
1da177e4 1249 usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
830f4021 1250 acm_read_buffers_free(acm);
1da177e4 1251
830f4021
ON
1252 usb_driver_release_interface(&acm_driver, intf == acm->control ?
1253 acm->data : acm->control);
1da177e4
LT
1254
1255 if (!acm->used) {
83ef344a 1256 acm_tty_unregister(acm);
4186ecf8 1257 mutex_unlock(&open_mutex);
1da177e4
LT
1258 return;
1259 }
1260
4186ecf8 1261 mutex_unlock(&open_mutex);
1da177e4
LT
1262
1263 if (acm->tty)
1264 tty_hangup(acm->tty);
1265}
1266
35758589 1267#ifdef CONFIG_PM
1365baf7
ON
1268static int acm_suspend(struct usb_interface *intf, pm_message_t message)
1269{
1270 struct acm *acm = usb_get_intfdata(intf);
11ea859d
ON
1271 int cnt;
1272
1273 if (acm->dev->auto_pm) {
1274 int b;
1275
1276 spin_lock_irq(&acm->read_lock);
1277 spin_lock(&acm->write_lock);
1278 b = acm->processing + acm->transmitting;
1279 spin_unlock(&acm->write_lock);
1280 spin_unlock_irq(&acm->read_lock);
1281 if (b)
1282 return -EBUSY;
1283 }
1284
1285 spin_lock_irq(&acm->read_lock);
1286 spin_lock(&acm->write_lock);
1287 cnt = acm->susp_count++;
1288 spin_unlock(&acm->write_lock);
1289 spin_unlock_irq(&acm->read_lock);
1365baf7 1290
11ea859d 1291 if (cnt)
1365baf7
ON
1292 return 0;
1293 /*
1294 we treat opened interfaces differently,
1295 we must guard against open
1296 */
1297 mutex_lock(&acm->mutex);
1298
1299 if (acm->used)
1300 stop_data_traffic(acm);
1301
1302 mutex_unlock(&acm->mutex);
1303 return 0;
1304}
1305
1306static int acm_resume(struct usb_interface *intf)
1307{
1308 struct acm *acm = usb_get_intfdata(intf);
1309 int rv = 0;
11ea859d 1310 int cnt;
1365baf7 1311
11ea859d
ON
1312 spin_lock_irq(&acm->read_lock);
1313 acm->susp_count -= 1;
1314 cnt = acm->susp_count;
1315 spin_unlock_irq(&acm->read_lock);
1316
1317 if (cnt)
1365baf7
ON
1318 return 0;
1319
1320 mutex_lock(&acm->mutex);
1321 if (acm->used) {
1322 rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO);
1323 if (rv < 0)
11ea859d 1324 goto err_out;
1365baf7
ON
1325
1326 tasklet_schedule(&acm->urb_task);
1327 }
1328
1329err_out:
1330 mutex_unlock(&acm->mutex);
1331 return rv;
1332}
35758589
ON
1333
1334#endif /* CONFIG_PM */
1da177e4
LT
1335/*
1336 * USB driver structure.
1337 */
1338
1339static struct usb_device_id acm_ids[] = {
1340 /* quirky and broken devices */
1341 { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
1342 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1343 },
b0e2a705
AA
1344 { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
1345 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1346 },
8753e65e
MO
1347 { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
1348 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1349 },
91a9c921
CM
1350 { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
1351 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1352 },
86478944
ON
1353 { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
1354 .driver_info = SINGLE_RX_URB, /* firmware bug */
1355 },
3dd2ae81
ON
1356 { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
1357 .driver_info = SINGLE_RX_URB, /* firmware bug */
1358 },
9be8456c
ON
1359 { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
1360 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1361 },
6149ed5e
IM
1362 { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
1363 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1364 },
c8fd2c37
ES
1365 { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */
1366 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1367 },
9be8456c 1368
1da177e4
LT
1369 /* control interfaces with various AT-command sets */
1370 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1371 USB_CDC_ACM_PROTO_AT_V25TER) },
1372 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1373 USB_CDC_ACM_PROTO_AT_PCCA101) },
1374 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1375 USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
1376 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1377 USB_CDC_ACM_PROTO_AT_GSM) },
1378 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1379 USB_CDC_ACM_PROTO_AT_3G ) },
1380 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1381 USB_CDC_ACM_PROTO_AT_CDMA) },
1382
1383 /* NOTE: COMM/ACM/0xff is likely MSFT RNDIS ... NOT a modem!! */
1384 { }
1385};
1386
1387MODULE_DEVICE_TABLE (usb, acm_ids);
1388
1389static struct usb_driver acm_driver = {
1da177e4
LT
1390 .name = "cdc_acm",
1391 .probe = acm_probe,
1392 .disconnect = acm_disconnect,
35758589 1393#ifdef CONFIG_PM
1365baf7
ON
1394 .suspend = acm_suspend,
1395 .resume = acm_resume,
35758589 1396#endif
1da177e4 1397 .id_table = acm_ids,
35758589 1398#ifdef CONFIG_PM
1365baf7 1399 .supports_autosuspend = 1,
35758589 1400#endif
1da177e4
LT
1401};
1402
1403/*
1404 * TTY driver structures.
1405 */
1406
b68e31d0 1407static const struct tty_operations acm_ops = {
1da177e4
LT
1408 .open = acm_tty_open,
1409 .close = acm_tty_close,
1410 .write = acm_tty_write,
1411 .write_room = acm_tty_write_room,
1412 .ioctl = acm_tty_ioctl,
1413 .throttle = acm_tty_throttle,
1414 .unthrottle = acm_tty_unthrottle,
1415 .chars_in_buffer = acm_tty_chars_in_buffer,
1416 .break_ctl = acm_tty_break_ctl,
1417 .set_termios = acm_tty_set_termios,
1418 .tiocmget = acm_tty_tiocmget,
1419 .tiocmset = acm_tty_tiocmset,
1420};
1421
1422/*
1423 * Init / exit.
1424 */
1425
1426static int __init acm_init(void)
1427{
1428 int retval;
1429 acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
1430 if (!acm_tty_driver)
1431 return -ENOMEM;
1432 acm_tty_driver->owner = THIS_MODULE,
1433 acm_tty_driver->driver_name = "acm",
1434 acm_tty_driver->name = "ttyACM",
1da177e4
LT
1435 acm_tty_driver->major = ACM_TTY_MAJOR,
1436 acm_tty_driver->minor_start = 0,
1437 acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
1438 acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
331b8319 1439 acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1da177e4
LT
1440 acm_tty_driver->init_termios = tty_std_termios;
1441 acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1442 tty_set_operations(acm_tty_driver, &acm_ops);
1443
1444 retval = tty_register_driver(acm_tty_driver);
1445 if (retval) {
1446 put_tty_driver(acm_tty_driver);
1447 return retval;
1448 }
1449
1450 retval = usb_register(&acm_driver);
1451 if (retval) {
1452 tty_unregister_driver(acm_tty_driver);
1453 put_tty_driver(acm_tty_driver);
1454 return retval;
1455 }
1456
5909f6ea
GKH
1457 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1458 DRIVER_DESC "\n");
1da177e4
LT
1459
1460 return 0;
1461}
1462
1463static void __exit acm_exit(void)
1464{
1465 usb_deregister(&acm_driver);
1466 tty_unregister_driver(acm_tty_driver);
1467 put_tty_driver(acm_tty_driver);
1468}
1469
1470module_init(acm_init);
1471module_exit(acm_exit);
1472
1473MODULE_AUTHOR( DRIVER_AUTHOR );
1474MODULE_DESCRIPTION( DRIVER_DESC );
1475MODULE_LICENSE("GPL");
1476
This page took 0.533829 seconds and 5 git commands to generate.