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