blob: b0f9873ce6b72f5f9e257edd16dfde71eeea9ae6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
David Kubicek61a87ad2005-11-01 18:51:34 +01009 * Copyright (c) 2005 David Kubicek <dave@awk.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
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
David Kubicek61a87ad2005-11-01 18:51:34 +010033 * v0.25 - downstream tasks paralelized to maximize throughput
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 */
35
36/*
37 * This program is free software; you can redistribute it and/or modify
38 * it under the terms of the GNU General Public License as published by
39 * the Free Software Foundation; either version 2 of the License, or
40 * (at your option) any later version.
41 *
42 * This program is distributed in the hope that it will be useful,
43 * but WITHOUT ANY WARRANTY; without even the implied warranty of
44 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 * GNU General Public License for more details.
46 *
47 * You should have received a copy of the GNU General Public License
48 * along with this program; if not, write to the Free Software
49 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
50 */
51
52#undef DEBUG
53
54#include <linux/kernel.h>
55#include <linux/errno.h>
56#include <linux/init.h>
57#include <linux/slab.h>
58#include <linux/tty.h>
59#include <linux/tty_driver.h>
60#include <linux/tty_flip.h>
61#include <linux/module.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010062#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <asm/uaccess.h>
64#include <linux/usb.h>
David Brownella8c28f22006-06-13 09:57:47 -070065#include <linux/usb/cdc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <asm/byteorder.h>
67#include <asm/unaligned.h>
David Kubicek61a87ad2005-11-01 18:51:34 +010068#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#include "cdc-acm.h"
71
72/*
73 * Version Information
74 */
David Kubicek61a87ad2005-11-01 18:51:34 +010075#define DRIVER_VERSION "v0.25"
76#define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek"
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
78
79static struct usb_driver acm_driver;
80static struct tty_driver *acm_tty_driver;
81static struct acm *acm_table[ACM_TTY_MINORS];
82
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010083static DEFINE_MUTEX(open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85#define ACM_READY(acm) (acm && acm->dev && acm->used)
86
87/*
88 * Functions for ACM control messages.
89 */
90
91static int acm_ctrl_msg(struct acm *acm, int request, int value, void *buf, int len)
92{
93 int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
94 request, USB_RT_ACM, value,
95 acm->control->altsetting[0].desc.bInterfaceNumber,
96 buf, len, 5000);
97 dbg("acm_control_msg: rq: 0x%02x val: %#x len: %#x result: %d", request, value, len, retval);
98 return retval < 0 ? retval : 0;
99}
100
101/* devices aren't required to support these requests.
102 * the cdc acm descriptor tells whether they do...
103 */
104#define acm_set_control(acm, control) \
105 acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0)
106#define acm_set_line(acm, line) \
107 acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
108#define acm_send_break(acm, ms) \
109 acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
110
111/*
Oliver Neukum884b6002005-04-21 21:28:02 +0200112 * Write buffer management.
113 * All of these assume proper locks taken by the caller.
114 */
115
116static int acm_wb_alloc(struct acm *acm)
117{
118 int i, wbn;
119 struct acm_wb *wb;
120
121 wbn = acm->write_current;
122 i = 0;
123 for (;;) {
124 wb = &acm->wb[wbn];
125 if (!wb->use) {
126 wb->use = 1;
127 return wbn;
128 }
Oliver Neukum86478942006-05-13 22:50:47 +0200129 wbn = (wbn + 1) % ACM_NW;
130 if (++i >= ACM_NW)
Oliver Neukum884b6002005-04-21 21:28:02 +0200131 return -1;
132 }
133}
134
135static void acm_wb_free(struct acm *acm, int wbn)
136{
137 acm->wb[wbn].use = 0;
138}
139
140static int acm_wb_is_avail(struct acm *acm)
141{
142 int i, n;
143
Oliver Neukum86478942006-05-13 22:50:47 +0200144 n = ACM_NW;
145 for (i = 0; i < ACM_NW; i++) {
146 n -= acm->wb[i].use;
Oliver Neukum884b6002005-04-21 21:28:02 +0200147 }
148 return n;
149}
150
151static inline int acm_wb_is_used(struct acm *acm, int wbn)
152{
153 return acm->wb[wbn].use;
154}
155
156/*
157 * Finish write.
158 */
159static void acm_write_done(struct acm *acm)
160{
161 unsigned long flags;
162 int wbn;
163
164 spin_lock_irqsave(&acm->write_lock, flags);
165 acm->write_ready = 1;
166 wbn = acm->write_current;
167 acm_wb_free(acm, wbn);
Oliver Neukum86478942006-05-13 22:50:47 +0200168 acm->write_current = (wbn + 1) % ACM_NW;
Oliver Neukum884b6002005-04-21 21:28:02 +0200169 spin_unlock_irqrestore(&acm->write_lock, flags);
170}
171
172/*
173 * Poke write.
174 */
175static int acm_write_start(struct acm *acm)
176{
177 unsigned long flags;
178 int wbn;
179 struct acm_wb *wb;
180 int rc;
181
182 spin_lock_irqsave(&acm->write_lock, flags);
183 if (!acm->dev) {
184 spin_unlock_irqrestore(&acm->write_lock, flags);
185 return -ENODEV;
186 }
187
188 if (!acm->write_ready) {
189 spin_unlock_irqrestore(&acm->write_lock, flags);
190 return 0; /* A white lie */
191 }
192
193 wbn = acm->write_current;
194 if (!acm_wb_is_used(acm, wbn)) {
195 spin_unlock_irqrestore(&acm->write_lock, flags);
196 return 0;
197 }
198 wb = &acm->wb[wbn];
199
200 acm->write_ready = 0;
201 spin_unlock_irqrestore(&acm->write_lock, flags);
202
203 acm->writeurb->transfer_buffer = wb->buf;
204 acm->writeurb->transfer_dma = wb->dmah;
205 acm->writeurb->transfer_buffer_length = wb->len;
206 acm->writeurb->dev = acm->dev;
207
208 if ((rc = usb_submit_urb(acm->writeurb, GFP_ATOMIC)) < 0) {
209 dbg("usb_submit_urb(write bulk) failed: %d", rc);
210 acm_write_done(acm);
211 }
212 return rc;
213}
Oliver Neukumc4cabd22007-02-27 15:28:55 +0100214/*
215 * attributes exported through sysfs
216 */
217static ssize_t show_caps
218(struct device *dev, struct device_attribute *attr, char *buf)
219{
220 struct usb_interface *intf = to_usb_interface(dev);
221 struct acm *acm = usb_get_intfdata(intf);
Oliver Neukum884b6002005-04-21 21:28:02 +0200222
Oliver Neukumc4cabd22007-02-27 15:28:55 +0100223 return sprintf(buf, "%d", acm->ctrl_caps);
224}
225static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
226
227static ssize_t show_country_codes
228(struct device *dev, struct device_attribute *attr, char *buf)
229{
230 struct usb_interface *intf = to_usb_interface(dev);
231 struct acm *acm = usb_get_intfdata(intf);
232
233 memcpy(buf, acm->country_codes, acm->country_code_size);
234 return acm->country_code_size;
235}
236
237static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
238
239static ssize_t show_country_rel_date
240(struct device *dev, struct device_attribute *attr, char *buf)
241{
242 struct usb_interface *intf = to_usb_interface(dev);
243 struct acm *acm = usb_get_intfdata(intf);
244
245 return sprintf(buf, "%d", acm->country_rel_date);
246}
247
248static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
Oliver Neukum884b6002005-04-21 21:28:02 +0200249/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 * Interrupt handlers for various ACM device responses
251 */
252
253/* control interface reports status changes with "interrupt" transfers */
David Howells7d12e782006-10-05 14:55:46 +0100254static void acm_ctrl_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 struct acm *acm = urb->context;
257 struct usb_cdc_notification *dr = urb->transfer_buffer;
258 unsigned char *data;
259 int newctrl;
Greg Kroah-Hartman185d4052007-07-18 10:58:02 -0700260 int retval;
261 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Greg Kroah-Hartman185d4052007-07-18 10:58:02 -0700263 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 case 0:
265 /* success */
266 break;
267 case -ECONNRESET:
268 case -ENOENT:
269 case -ESHUTDOWN:
270 /* this urb is terminated, clean up */
Greg Kroah-Hartman185d4052007-07-18 10:58:02 -0700271 dbg("%s - urb shutting down with status: %d", __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return;
273 default:
Greg Kroah-Hartman185d4052007-07-18 10:58:02 -0700274 dbg("%s - nonzero urb status received: %d", __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 goto exit;
276 }
277
278 if (!ACM_READY(acm))
279 goto exit;
280
281 data = (unsigned char *)(dr + 1);
282 switch (dr->bNotificationType) {
283
284 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
285
286 dbg("%s network", dr->wValue ? "connected to" : "disconnected from");
287 break;
288
289 case USB_CDC_NOTIFY_SERIAL_STATE:
290
291 newctrl = le16_to_cpu(get_unaligned((__le16 *) data));
292
293 if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
294 dbg("calling hangup");
295 tty_hangup(acm->tty);
296 }
297
298 acm->ctrlin = newctrl;
299
300 dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c",
301 acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
302 acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
303 acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-', acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
304 acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
305
306 break;
307
308 default:
309 dbg("unknown notification %d received: index %d len %d data0 %d data1 %d",
310 dr->bNotificationType, dr->wIndex,
311 dr->wLength, data[0], data[1]);
312 break;
313 }
314exit:
Greg Kroah-Hartman185d4052007-07-18 10:58:02 -0700315 retval = usb_submit_urb (urb, GFP_ATOMIC);
316 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 err ("%s - usb_submit_urb failed with result %d",
Greg Kroah-Hartman185d4052007-07-18 10:58:02 -0700318 __FUNCTION__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
321/* data interface returns incoming bytes, or we got unthrottled */
David Howells7d12e782006-10-05 14:55:46 +0100322static void acm_read_bulk(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
David Kubicek61a87ad2005-11-01 18:51:34 +0100324 struct acm_rb *buf;
325 struct acm_ru *rcv = urb->context;
326 struct acm *acm = rcv->instance;
Oliver Neukum86478942006-05-13 22:50:47 +0200327 int status = urb->status;
Greg Kroah-Hartman185d4052007-07-18 10:58:02 -0700328
329 dbg("Entering acm_read_bulk with status %d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 if (!ACM_READY(acm))
332 return;
333
Oliver Neukum86478942006-05-13 22:50:47 +0200334 if (status)
Joe Perches898eb712007-10-18 03:06:30 -0700335 dev_dbg(&acm->data->dev, "bulk rx status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
David Kubicek61a87ad2005-11-01 18:51:34 +0100337 buf = rcv->buffer;
338 buf->size = urb->actual_length;
339
Oliver Neukum86478942006-05-13 22:50:47 +0200340 if (likely(status == 0)) {
341 spin_lock(&acm->read_lock);
342 list_add_tail(&rcv->list, &acm->spare_read_urbs);
343 list_add_tail(&buf->list, &acm->filled_read_bufs);
344 spin_unlock(&acm->read_lock);
345 } else {
346 /* we drop the buffer due to an error */
347 spin_lock(&acm->read_lock);
348 list_add_tail(&rcv->list, &acm->spare_read_urbs);
349 list_add(&buf->list, &acm->spare_read_bufs);
350 spin_unlock(&acm->read_lock);
351 /* nevertheless the tasklet must be kicked unconditionally
352 so the queue cannot dry up */
353 }
David Kubicek61a87ad2005-11-01 18:51:34 +0100354 tasklet_schedule(&acm->urb_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357static void acm_rx_tasklet(unsigned long _acm)
358{
359 struct acm *acm = (void *)_acm;
David Kubicek61a87ad2005-11-01 18:51:34 +0100360 struct acm_rb *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 struct tty_struct *tty = acm->tty;
David Kubicek61a87ad2005-11-01 18:51:34 +0100362 struct acm_ru *rcv;
Jarek Poplawski762f0072006-10-06 07:23:11 +0200363 unsigned long flags;
Oliver Neukumca79b7b2007-02-12 08:41:35 +0100364 unsigned char throttled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 dbg("Entering acm_rx_tasklet");
366
Oliver Neukumca79b7b2007-02-12 08:41:35 +0100367 if (!ACM_READY(acm))
368 return;
369
Oliver Neukum834dbca2007-03-06 10:47:04 +0100370 spin_lock_irqsave(&acm->throttle_lock, flags);
Oliver Neukumca79b7b2007-02-12 08:41:35 +0100371 throttled = acm->throttle;
Oliver Neukum834dbca2007-03-06 10:47:04 +0100372 spin_unlock_irqrestore(&acm->throttle_lock, flags);
Oliver Neukumca79b7b2007-02-12 08:41:35 +0100373 if (throttled)
David Kubicek61a87ad2005-11-01 18:51:34 +0100374 return;
375
376next_buffer:
Jarek Poplawski762f0072006-10-06 07:23:11 +0200377 spin_lock_irqsave(&acm->read_lock, flags);
David Kubicek61a87ad2005-11-01 18:51:34 +0100378 if (list_empty(&acm->filled_read_bufs)) {
Jarek Poplawski762f0072006-10-06 07:23:11 +0200379 spin_unlock_irqrestore(&acm->read_lock, flags);
David Kubicek61a87ad2005-11-01 18:51:34 +0100380 goto urbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
David Kubicek61a87ad2005-11-01 18:51:34 +0100382 buf = list_entry(acm->filled_read_bufs.next,
383 struct acm_rb, list);
384 list_del(&buf->list);
Jarek Poplawski762f0072006-10-06 07:23:11 +0200385 spin_unlock_irqrestore(&acm->read_lock, flags);
David Kubicek61a87ad2005-11-01 18:51:34 +0100386
Oliver Neukum3dd2ae82006-06-23 09:14:17 +0200387 dbg("acm_rx_tasklet: procesing buf 0x%p, size = %d", buf, buf->size);
David Kubicek61a87ad2005-11-01 18:51:34 +0100388
Alan Cox33f0f882006-01-09 20:54:13 -0800389 tty_buffer_request_room(tty, buf->size);
Oliver Neukum834dbca2007-03-06 10:47:04 +0100390 spin_lock_irqsave(&acm->throttle_lock, flags);
Oliver Neukumca79b7b2007-02-12 08:41:35 +0100391 throttled = acm->throttle;
Oliver Neukum834dbca2007-03-06 10:47:04 +0100392 spin_unlock_irqrestore(&acm->throttle_lock, flags);
Oliver Neukumca79b7b2007-02-12 08:41:35 +0100393 if (!throttled)
Alan Cox33f0f882006-01-09 20:54:13 -0800394 tty_insert_flip_string(tty, buf->base, buf->size);
David Kubicek61a87ad2005-11-01 18:51:34 +0100395 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Oliver Neukumca79b7b2007-02-12 08:41:35 +0100397 if (throttled) {
398 dbg("Throttling noticed");
Jarek Poplawski762f0072006-10-06 07:23:11 +0200399 spin_lock_irqsave(&acm->read_lock, flags);
David Kubicek61a87ad2005-11-01 18:51:34 +0100400 list_add(&buf->list, &acm->filled_read_bufs);
Jarek Poplawski762f0072006-10-06 07:23:11 +0200401 spin_unlock_irqrestore(&acm->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return;
403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Jarek Poplawski762f0072006-10-06 07:23:11 +0200405 spin_lock_irqsave(&acm->read_lock, flags);
David Kubicek61a87ad2005-11-01 18:51:34 +0100406 list_add(&buf->list, &acm->spare_read_bufs);
Jarek Poplawski762f0072006-10-06 07:23:11 +0200407 spin_unlock_irqrestore(&acm->read_lock, flags);
David Kubicek61a87ad2005-11-01 18:51:34 +0100408 goto next_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
David Kubicek61a87ad2005-11-01 18:51:34 +0100410urbs:
411 while (!list_empty(&acm->spare_read_bufs)) {
Jarek Poplawski762f0072006-10-06 07:23:11 +0200412 spin_lock_irqsave(&acm->read_lock, flags);
David Kubicek61a87ad2005-11-01 18:51:34 +0100413 if (list_empty(&acm->spare_read_urbs)) {
Jarek Poplawski762f0072006-10-06 07:23:11 +0200414 spin_unlock_irqrestore(&acm->read_lock, flags);
David Kubicek61a87ad2005-11-01 18:51:34 +0100415 return;
416 }
417 rcv = list_entry(acm->spare_read_urbs.next,
418 struct acm_ru, list);
419 list_del(&rcv->list);
Jarek Poplawski762f0072006-10-06 07:23:11 +0200420 spin_unlock_irqrestore(&acm->read_lock, flags);
David Kubicek61a87ad2005-11-01 18:51:34 +0100421
422 buf = list_entry(acm->spare_read_bufs.next,
423 struct acm_rb, list);
424 list_del(&buf->list);
425
426 rcv->buffer = buf;
427
428 usb_fill_bulk_urb(rcv->urb, acm->dev,
429 acm->rx_endpoint,
430 buf->base,
431 acm->readsize,
432 acm_read_bulk, rcv);
433 rcv->urb->transfer_dma = buf->dma;
434 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
435
Oliver Neukum3dd2ae82006-06-23 09:14:17 +0200436 dbg("acm_rx_tasklet: sending urb 0x%p, rcv 0x%p, buf 0x%p", rcv->urb, rcv, buf);
David Kubicek61a87ad2005-11-01 18:51:34 +0100437
438 /* This shouldn't kill the driver as unsuccessful URBs are returned to the
439 free-urbs-pool and resubmited ASAP */
440 if (usb_submit_urb(rcv->urb, GFP_ATOMIC) < 0) {
441 list_add(&buf->list, &acm->spare_read_bufs);
Jarek Poplawski762f0072006-10-06 07:23:11 +0200442 spin_lock_irqsave(&acm->read_lock, flags);
David Kubicek61a87ad2005-11-01 18:51:34 +0100443 list_add(&rcv->list, &acm->spare_read_urbs);
Jarek Poplawski762f0072006-10-06 07:23:11 +0200444 spin_unlock_irqrestore(&acm->read_lock, flags);
David Kubicek61a87ad2005-11-01 18:51:34 +0100445 return;
446 }
447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
450/* data interface wrote those outgoing bytes */
David Howells7d12e782006-10-05 14:55:46 +0100451static void acm_write_bulk(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 struct acm *acm = (struct acm *)urb->context;
Oliver Neukum884b6002005-04-21 21:28:02 +0200454
Oliver Neukum3dd2ae82006-06-23 09:14:17 +0200455 dbg("Entering acm_write_bulk with status %d", urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Oliver Neukum884b6002005-04-21 21:28:02 +0200457 acm_write_done(acm);
458 acm_write_start(acm);
459 if (ACM_READY(acm))
460 schedule_work(&acm->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
David Howellsc4028952006-11-22 14:57:56 +0000463static void acm_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
David Howellsc4028952006-11-22 14:57:56 +0000465 struct acm *acm = container_of(work, struct acm, work);
Oliver Neukum3dd2ae82006-06-23 09:14:17 +0200466 dbg("Entering acm_softint.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 if (!ACM_READY(acm))
469 return;
470 tty_wakeup(acm->tty);
471}
472
473/*
474 * TTY handlers
475 */
476
477static int acm_tty_open(struct tty_struct *tty, struct file *filp)
478{
479 struct acm *acm;
480 int rv = -EINVAL;
David Kubicek61a87ad2005-11-01 18:51:34 +0100481 int i;
Oliver Neukum3dd2ae82006-06-23 09:14:17 +0200482 dbg("Entering acm_tty_open.");
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100483
484 mutex_lock(&open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 acm = acm_table[tty->index];
487 if (!acm || !acm->dev)
488 goto err_out;
489 else
490 rv = 0;
491
492 tty->driver_data = acm;
493 acm->tty = tty;
494
David Kubicek61a87ad2005-11-01 18:51:34 +0100495 /* force low_latency on so that our tty_push actually forces the data through,
496 otherwise it is scheduled, and with high data rates data can get lost. */
497 tty->low_latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Oliver Neukum1365baf2007-10-12 17:24:28 +0200499 if (usb_autopm_get_interface(acm->control)) {
500 mutex_unlock(&open_mutex);
501 return -EIO;
502 }
503
504 mutex_lock(&acm->mutex);
505 mutex_unlock(&open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (acm->used++) {
Oliver Neukum1365baf2007-10-12 17:24:28 +0200507 usb_autopm_put_interface(acm->control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 goto done;
509 }
510
Oliver Neukum1365baf2007-10-12 17:24:28 +0200511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 acm->ctrlurb->dev = acm->dev;
513 if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
514 dbg("usb_submit_urb(ctrl irq) failed");
515 goto bail_out;
516 }
517
Oliver Neukumca79b7b2007-02-12 08:41:35 +0100518 if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS) &&
519 (acm->ctrl_caps & USB_CDC_CAP_LINE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 goto full_bailout;
521
David Kubicek61a87ad2005-11-01 18:51:34 +0100522 INIT_LIST_HEAD(&acm->spare_read_urbs);
523 INIT_LIST_HEAD(&acm->spare_read_bufs);
524 INIT_LIST_HEAD(&acm->filled_read_bufs);
Oliver Neukum86478942006-05-13 22:50:47 +0200525 for (i = 0; i < acm->rx_buflimit; i++) {
David Kubicek61a87ad2005-11-01 18:51:34 +0100526 list_add(&(acm->ru[i].list), &acm->spare_read_urbs);
527 }
Oliver Neukum86478942006-05-13 22:50:47 +0200528 for (i = 0; i < acm->rx_buflimit; i++) {
David Kubicek61a87ad2005-11-01 18:51:34 +0100529 list_add(&(acm->rb[i].list), &acm->spare_read_bufs);
530 }
531
Oliver Neukumca79b7b2007-02-12 08:41:35 +0100532 acm->throttle = 0;
533
David Kubicek61a87ad2005-11-01 18:51:34 +0100534 tasklet_schedule(&acm->urb_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536done:
537err_out:
Oliver Neukum1365baf2007-10-12 17:24:28 +0200538 mutex_unlock(&acm->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return rv;
540
541full_bailout:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 usb_kill_urb(acm->ctrlurb);
543bail_out:
Oliver Neukum1365baf2007-10-12 17:24:28 +0200544 usb_autopm_put_interface(acm->control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 acm->used--;
Oliver Neukum1365baf2007-10-12 17:24:28 +0200546 mutex_unlock(&acm->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return -EIO;
548}
549
brian@murphy.dk83ef3442005-06-29 16:53:29 -0700550static void acm_tty_unregister(struct acm *acm)
551{
Oliver Neukum86478942006-05-13 22:50:47 +0200552 int i,nr;
David Kubicek61a87ad2005-11-01 18:51:34 +0100553
Oliver Neukum86478942006-05-13 22:50:47 +0200554 nr = acm->rx_buflimit;
brian@murphy.dk83ef3442005-06-29 16:53:29 -0700555 tty_unregister_device(acm_tty_driver, acm->minor);
556 usb_put_intf(acm->control);
557 acm_table[acm->minor] = NULL;
558 usb_free_urb(acm->ctrlurb);
brian@murphy.dk83ef3442005-06-29 16:53:29 -0700559 usb_free_urb(acm->writeurb);
Oliver Neukum86478942006-05-13 22:50:47 +0200560 for (i = 0; i < nr; i++)
David Kubicek61a87ad2005-11-01 18:51:34 +0100561 usb_free_urb(acm->ru[i].urb);
Oliver Neukumc4cabd22007-02-27 15:28:55 +0100562 kfree(acm->country_codes);
brian@murphy.dk83ef3442005-06-29 16:53:29 -0700563 kfree(acm);
564}
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566static void acm_tty_close(struct tty_struct *tty, struct file *filp)
567{
568 struct acm *acm = tty->driver_data;
Oliver Neukum86478942006-05-13 22:50:47 +0200569 int i,nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 if (!acm || !acm->used)
572 return;
573
Oliver Neukum86478942006-05-13 22:50:47 +0200574 nr = acm->rx_buflimit;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100575 mutex_lock(&open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (!--acm->used) {
577 if (acm->dev) {
578 acm_set_control(acm, acm->ctrlout = 0);
579 usb_kill_urb(acm->ctrlurb);
580 usb_kill_urb(acm->writeurb);
Oliver Neukum86478942006-05-13 22:50:47 +0200581 for (i = 0; i < nr; i++)
David Kubicek61a87ad2005-11-01 18:51:34 +0100582 usb_kill_urb(acm->ru[i].urb);
Oliver Neukum1365baf2007-10-12 17:24:28 +0200583 usb_autopm_put_interface(acm->control);
brian@murphy.dk83ef3442005-06-29 16:53:29 -0700584 } else
585 acm_tty_unregister(acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100587 mutex_unlock(&open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
590static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
591{
592 struct acm *acm = tty->driver_data;
593 int stat;
Oliver Neukum884b6002005-04-21 21:28:02 +0200594 unsigned long flags;
595 int wbn;
596 struct acm_wb *wb;
597
Oliver Neukum3dd2ae82006-06-23 09:14:17 +0200598 dbg("Entering acm_tty_write to write %d bytes,", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 if (!ACM_READY(acm))
601 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (!count)
603 return 0;
604
Oliver Neukum884b6002005-04-21 21:28:02 +0200605 spin_lock_irqsave(&acm->write_lock, flags);
606 if ((wbn = acm_wb_alloc(acm)) < 0) {
607 spin_unlock_irqrestore(&acm->write_lock, flags);
608 acm_write_start(acm);
609 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
Oliver Neukum884b6002005-04-21 21:28:02 +0200611 wb = &acm->wb[wbn];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Oliver Neukum884b6002005-04-21 21:28:02 +0200613 count = (count > acm->writesize) ? acm->writesize : count;
614 dbg("Get %d bytes...", count);
615 memcpy(wb->buf, buf, count);
616 wb->len = count;
617 spin_unlock_irqrestore(&acm->write_lock, flags);
618
619 if ((stat = acm_write_start(acm)) < 0)
620 return stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return count;
622}
623
624static int acm_tty_write_room(struct tty_struct *tty)
625{
626 struct acm *acm = tty->driver_data;
627 if (!ACM_READY(acm))
628 return -EINVAL;
Oliver Neukum884b6002005-04-21 21:28:02 +0200629 /*
630 * Do not let the line discipline to know that we have a reserve,
631 * or it might get too enthusiastic.
632 */
633 return (acm->write_ready && acm_wb_is_avail(acm)) ? acm->writesize : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
636static int acm_tty_chars_in_buffer(struct tty_struct *tty)
637{
638 struct acm *acm = tty->driver_data;
639 if (!ACM_READY(acm))
640 return -EINVAL;
Oliver Neukum884b6002005-04-21 21:28:02 +0200641 /*
642 * This is inaccurate (overcounts), but it works.
643 */
Oliver Neukum86478942006-05-13 22:50:47 +0200644 return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
647static void acm_tty_throttle(struct tty_struct *tty)
648{
649 struct acm *acm = tty->driver_data;
650 if (!ACM_READY(acm))
651 return;
652 spin_lock_bh(&acm->throttle_lock);
653 acm->throttle = 1;
654 spin_unlock_bh(&acm->throttle_lock);
655}
656
657static void acm_tty_unthrottle(struct tty_struct *tty)
658{
659 struct acm *acm = tty->driver_data;
660 if (!ACM_READY(acm))
661 return;
662 spin_lock_bh(&acm->throttle_lock);
663 acm->throttle = 0;
664 spin_unlock_bh(&acm->throttle_lock);
David Kubicek61a87ad2005-11-01 18:51:34 +0100665 tasklet_schedule(&acm->urb_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
668static void acm_tty_break_ctl(struct tty_struct *tty, int state)
669{
670 struct acm *acm = tty->driver_data;
671 if (!ACM_READY(acm))
672 return;
673 if (acm_send_break(acm, state ? 0xffff : 0))
674 dbg("send break failed");
675}
676
677static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file)
678{
679 struct acm *acm = tty->driver_data;
680
681 if (!ACM_READY(acm))
682 return -EINVAL;
683
684 return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
685 (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
686 (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
687 (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
688 (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
689 TIOCM_CTS;
690}
691
692static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file,
693 unsigned int set, unsigned int clear)
694{
695 struct acm *acm = tty->driver_data;
696 unsigned int newctrl;
697
698 if (!ACM_READY(acm))
699 return -EINVAL;
700
701 newctrl = acm->ctrlout;
702 set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
703 clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
704
705 newctrl = (newctrl & ~clear) | set;
706
707 if (acm->ctrlout == newctrl)
708 return 0;
709 return acm_set_control(acm, acm->ctrlout = newctrl);
710}
711
712static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
713{
714 struct acm *acm = tty->driver_data;
715
716 if (!ACM_READY(acm))
717 return -EINVAL;
718
719 return -ENOIOCTLCMD;
720}
721
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100722static const __u32 acm_tty_speed[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 0, 50, 75, 110, 134, 150, 200, 300, 600,
724 1200, 1800, 2400, 4800, 9600, 19200, 38400,
725 57600, 115200, 230400, 460800, 500000, 576000,
726 921600, 1000000, 1152000, 1500000, 2000000,
727 2500000, 3000000, 3500000, 4000000
728};
729
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100730static const __u8 acm_tty_size[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 5, 6, 7, 8
732};
733
Alan Cox606d0992006-12-08 02:38:45 -0800734static void acm_tty_set_termios(struct tty_struct *tty, struct ktermios *termios_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
736 struct acm *acm = tty->driver_data;
Alan Cox606d0992006-12-08 02:38:45 -0800737 struct ktermios *termios = tty->termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 struct usb_cdc_line_coding newline;
739 int newctrl = acm->ctrlout;
740
741 if (!ACM_READY(acm))
742 return;
743
744 newline.dwDTERate = cpu_to_le32p(acm_tty_speed +
745 (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0));
746 newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
747 newline.bParityType = termios->c_cflag & PARENB ?
748 (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
749 newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
750
751 acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
752
753 if (!newline.dwDTERate) {
754 newline.dwDTERate = acm->line.dwDTERate;
755 newctrl &= ~ACM_CTRL_DTR;
756 } else newctrl |= ACM_CTRL_DTR;
757
758 if (newctrl != acm->ctrlout)
759 acm_set_control(acm, acm->ctrlout = newctrl);
760
761 if (memcmp(&acm->line, &newline, sizeof newline)) {
762 memcpy(&acm->line, &newline, sizeof newline);
763 dbg("set line: %d %d %d %d", le32_to_cpu(newline.dwDTERate),
764 newline.bCharFormat, newline.bParityType,
765 newline.bDataBits);
766 acm_set_line(acm, &acm->line);
767 }
768}
769
770/*
771 * USB probe and disconnect routines.
772 */
773
Oliver Neukum884b6002005-04-21 21:28:02 +0200774/* Little helper: write buffers free */
775static void acm_write_buffers_free(struct acm *acm)
776{
777 int i;
778 struct acm_wb *wb;
779
Oliver Neukum86478942006-05-13 22:50:47 +0200780 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
Oliver Neukum884b6002005-04-21 21:28:02 +0200781 usb_buffer_free(acm->dev, acm->writesize, wb->buf, wb->dmah);
782 }
783}
784
785/* Little helper: write buffers allocate */
786static int acm_write_buffers_alloc(struct acm *acm)
787{
788 int i;
789 struct acm_wb *wb;
790
Oliver Neukum86478942006-05-13 22:50:47 +0200791 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
Oliver Neukum884b6002005-04-21 21:28:02 +0200792 wb->buf = usb_buffer_alloc(acm->dev, acm->writesize, GFP_KERNEL,
793 &wb->dmah);
794 if (!wb->buf) {
795 while (i != 0) {
796 --i;
797 --wb;
798 usb_buffer_free(acm->dev, acm->writesize,
799 wb->buf, wb->dmah);
800 }
801 return -ENOMEM;
802 }
803 }
804 return 0;
805}
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807static int acm_probe (struct usb_interface *intf,
808 const struct usb_device_id *id)
809{
810 struct usb_cdc_union_desc *union_header = NULL;
Oliver Neukumc4cabd22007-02-27 15:28:55 +0100811 struct usb_cdc_country_functional_desc *cfd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 char *buffer = intf->altsetting->extra;
813 int buflen = intf->altsetting->extralen;
814 struct usb_interface *control_interface;
815 struct usb_interface *data_interface;
816 struct usb_endpoint_descriptor *epctrl;
817 struct usb_endpoint_descriptor *epread;
818 struct usb_endpoint_descriptor *epwrite;
819 struct usb_device *usb_dev = interface_to_usbdev(intf);
820 struct acm *acm;
821 int minor;
822 int ctrlsize,readsize;
823 u8 *buf;
824 u8 ac_management_function = 0;
825 u8 call_management_function = 0;
826 int call_interface_num = -1;
827 int data_interface_num;
828 unsigned long quirks;
Oliver Neukum86478942006-05-13 22:50:47 +0200829 int num_rx_buf;
David Kubicek61a87ad2005-11-01 18:51:34 +0100830 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Oliver Neukum86478942006-05-13 22:50:47 +0200832 /* normal quirks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 quirks = (unsigned long)id->driver_info;
Oliver Neukum86478942006-05-13 22:50:47 +0200834 num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
835
836 /* handle quirks deadly to normal probing*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (quirks == NO_UNION_NORMAL) {
838 data_interface = usb_ifnum_to_if(usb_dev, 1);
839 control_interface = usb_ifnum_to_if(usb_dev, 0);
840 goto skip_normal_probe;
841 }
842
843 /* normal probing*/
844 if (!buffer) {
Joe Perches898eb712007-10-18 03:06:30 -0700845 err("Weird descriptor references\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return -EINVAL;
847 }
848
849 if (!buflen) {
850 if (intf->cur_altsetting->endpoint->extralen && intf->cur_altsetting->endpoint->extra) {
Joe Perches898eb712007-10-18 03:06:30 -0700851 dev_dbg(&intf->dev,"Seeking extra descriptors on endpoint\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 buflen = intf->cur_altsetting->endpoint->extralen;
853 buffer = intf->cur_altsetting->endpoint->extra;
854 } else {
855 err("Zero length descriptor references\n");
856 return -EINVAL;
857 }
858 }
859
860 while (buflen > 0) {
861 if (buffer [1] != USB_DT_CS_INTERFACE) {
862 err("skipping garbage\n");
863 goto next_desc;
864 }
865
866 switch (buffer [2]) {
867 case USB_CDC_UNION_TYPE: /* we've found it */
868 if (union_header) {
869 err("More than one union descriptor, skipping ...");
870 goto next_desc;
871 }
872 union_header = (struct usb_cdc_union_desc *)
873 buffer;
874 break;
Oliver Neukumc4cabd22007-02-27 15:28:55 +0100875 case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
876 cfd = (struct usb_cdc_country_functional_desc *)buffer;
877 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 case USB_CDC_HEADER_TYPE: /* maybe check version */
879 break; /* for now we ignore it */
880 case USB_CDC_ACM_TYPE:
881 ac_management_function = buffer[3];
882 break;
883 case USB_CDC_CALL_MANAGEMENT_TYPE:
884 call_management_function = buffer[3];
885 call_interface_num = buffer[4];
886 if ((call_management_function & 3) != 3)
887 err("This device cannot do calls on its own. It is no modem.");
888 break;
889
890 default:
891 err("Ignoring extra header, type %d, length %d", buffer[2], buffer[0]);
892 break;
893 }
894next_desc:
895 buflen -= buffer[0];
896 buffer += buffer[0];
897 }
898
899 if (!union_header) {
900 if (call_interface_num > 0) {
Joe Perches898eb712007-10-18 03:06:30 -0700901 dev_dbg(&intf->dev,"No union descriptor, using call management descriptor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
903 control_interface = intf;
904 } else {
Joe Perches898eb712007-10-18 03:06:30 -0700905 dev_dbg(&intf->dev,"No union descriptor, giving up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return -ENODEV;
907 }
908 } else {
909 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
910 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
911 if (!control_interface || !data_interface) {
Joe Perches898eb712007-10-18 03:06:30 -0700912 dev_dbg(&intf->dev,"no interfaces\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return -ENODEV;
914 }
915 }
916
917 if (data_interface_num != call_interface_num)
Joe Perches898eb712007-10-18 03:06:30 -0700918 dev_dbg(&intf->dev,"Seperate call control interface. That is not fully supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920skip_normal_probe:
921
922 /*workaround for switched interfaces */
923 if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) {
924 if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) {
925 struct usb_interface *t;
Joe Perches898eb712007-10-18 03:06:30 -0700926 dev_dbg(&intf->dev,"Your device has switched interfaces.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 t = control_interface;
929 control_interface = data_interface;
930 data_interface = t;
931 } else {
932 return -EINVAL;
933 }
934 }
Alan Stern74da5d62007-08-02 13:29:10 -0400935
936 /* Accept probe requests only for the control interface */
937 if (intf != control_interface)
938 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 if (usb_interface_claimed(data_interface)) { /* valid in this context */
Joe Perches898eb712007-10-18 03:06:30 -0700941 dev_dbg(&intf->dev,"The data interface isn't available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return -EBUSY;
943 }
944
945
946 if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
947 return -EINVAL;
948
949 epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
950 epread = &data_interface->cur_altsetting->endpoint[0].desc;
951 epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
952
953
954 /* workaround for switched endpoints */
Luiz Fernando N. Capitulino45aea702006-10-26 13:02:48 -0300955 if (!usb_endpoint_dir_in(epread)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 /* descriptors are swapped */
957 struct usb_endpoint_descriptor *t;
Joe Perches898eb712007-10-18 03:06:30 -0700958 dev_dbg(&intf->dev,"The data interface has switched endpoints\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 t = epread;
961 epread = epwrite;
962 epwrite = t;
963 }
964 dbg("interfaces are valid");
965 for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
966
967 if (minor == ACM_TTY_MINORS) {
968 err("no more free acm devices");
969 return -ENODEV;
970 }
971
Oliver Neukum46f116e2005-10-24 22:42:35 +0200972 if (!(acm = kzalloc(sizeof(struct acm), GFP_KERNEL))) {
Joe Perches898eb712007-10-18 03:06:30 -0700973 dev_dbg(&intf->dev, "out of memory (acm kzalloc)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 goto alloc_fail;
975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize);
Oliver Neukum86478942006-05-13 22:50:47 +0200978 readsize = le16_to_cpu(epread->wMaxPacketSize)* ( quirks == SINGLE_RX_URB ? 1 : 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize);
980 acm->control = control_interface;
981 acm->data = data_interface;
982 acm->minor = minor;
983 acm->dev = usb_dev;
984 acm->ctrl_caps = ac_management_function;
985 acm->ctrlsize = ctrlsize;
986 acm->readsize = readsize;
Oliver Neukum86478942006-05-13 22:50:47 +0200987 acm->rx_buflimit = num_rx_buf;
David Kubicek61a87ad2005-11-01 18:51:34 +0100988 acm->urb_task.func = acm_rx_tasklet;
989 acm->urb_task.data = (unsigned long) acm;
David Howellsc4028952006-11-22 14:57:56 +0000990 INIT_WORK(&acm->work, acm_softint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 spin_lock_init(&acm->throttle_lock);
Oliver Neukum884b6002005-04-21 21:28:02 +0200992 spin_lock_init(&acm->write_lock);
David Kubicek61a87ad2005-11-01 18:51:34 +0100993 spin_lock_init(&acm->read_lock);
Oliver Neukum1365baf2007-10-12 17:24:28 +0200994 mutex_init(&acm->mutex);
Oliver Neukum884b6002005-04-21 21:28:02 +0200995 acm->write_ready = 1;
David Kubicek61a87ad2005-11-01 18:51:34 +0100996 acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
999 if (!buf) {
Joe Perches898eb712007-10-18 03:06:30 -07001000 dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 goto alloc_fail2;
1002 }
1003 acm->ctrl_buffer = buf;
1004
Oliver Neukum884b6002005-04-21 21:28:02 +02001005 if (acm_write_buffers_alloc(acm) < 0) {
Joe Perches898eb712007-10-18 03:06:30 -07001006 dev_dbg(&intf->dev, "out of memory (write buffer alloc)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 goto alloc_fail4;
1008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
1011 if (!acm->ctrlurb) {
Joe Perches898eb712007-10-18 03:06:30 -07001012 dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 goto alloc_fail5;
1014 }
Oliver Neukum86478942006-05-13 22:50:47 +02001015 for (i = 0; i < num_rx_buf; i++) {
David Kubicek61a87ad2005-11-01 18:51:34 +01001016 struct acm_ru *rcv = &(acm->ru[i]);
1017
1018 if (!(rcv->urb = usb_alloc_urb(0, GFP_KERNEL))) {
Joe Perches898eb712007-10-18 03:06:30 -07001019 dev_dbg(&intf->dev, "out of memory (read urbs usb_alloc_urb)\n");
David Kubicek61a87ad2005-11-01 18:51:34 +01001020 goto alloc_fail7;
1021 }
1022
1023 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1024 rcv->instance = acm;
1025 }
Oliver Neukum86478942006-05-13 22:50:47 +02001026 for (i = 0; i < num_rx_buf; i++) {
David Kubicek61a87ad2005-11-01 18:51:34 +01001027 struct acm_rb *buf = &(acm->rb[i]);
1028
David Kubicek61a87ad2005-11-01 18:51:34 +01001029 if (!(buf->base = usb_buffer_alloc(acm->dev, readsize, GFP_KERNEL, &buf->dma))) {
Joe Perches898eb712007-10-18 03:06:30 -07001030 dev_dbg(&intf->dev, "out of memory (read bufs usb_buffer_alloc)\n");
David Kubicek61a87ad2005-11-01 18:51:34 +01001031 goto alloc_fail7;
1032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
1034 acm->writeurb = usb_alloc_urb(0, GFP_KERNEL);
1035 if (!acm->writeurb) {
Joe Perches898eb712007-10-18 03:06:30 -07001036 dev_dbg(&intf->dev, "out of memory (writeurb kmalloc)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 goto alloc_fail7;
1038 }
1039
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001040 usb_set_intfdata (intf, acm);
1041
1042 i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
1043 if (i < 0)
1044 goto alloc_fail8;
1045
1046 if (cfd) { /* export the country data */
1047 acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
1048 if (!acm->country_codes)
1049 goto skip_countries;
1050 acm->country_code_size = cfd->bLength - 4;
1051 memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0, cfd->bLength - 4);
1052 acm->country_rel_date = cfd->iCountryCodeRelDate;
1053
1054 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
1055 if (i < 0) {
1056 kfree(acm->country_codes);
1057 goto skip_countries;
1058 }
1059
1060 i = device_create_file(&intf->dev, &dev_attr_iCountryCodeRelDate);
1061 if (i < 0) {
1062 kfree(acm->country_codes);
1063 goto skip_countries;
1064 }
1065 }
1066
1067skip_countries:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
1069 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval);
1070 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1071 acm->ctrlurb->transfer_dma = acm->ctrl_dma;
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 usb_fill_bulk_urb(acm->writeurb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
Oliver Neukum884b6002005-04-21 21:28:02 +02001074 NULL, acm->writesize, acm_write_bulk, acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 acm->writeurb->transfer_flags |= URB_NO_FSBR | URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
1078
1079 acm_set_control(acm, acm->ctrlout);
1080
1081 acm->line.dwDTERate = cpu_to_le32(9600);
1082 acm->line.bDataBits = 8;
1083 acm_set_line(acm, &acm->line);
1084
1085 usb_driver_claim_interface(&acm_driver, data_interface, acm);
1086
brian@murphy.dk83ef3442005-06-29 16:53:29 -07001087 usb_get_intf(control_interface);
1088 tty_register_device(acm_tty_driver, minor, &control_interface->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 acm_table[minor] = acm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001092 return 0;
1093alloc_fail8:
1094 usb_free_urb(acm->writeurb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095alloc_fail7:
Oliver Neukum86478942006-05-13 22:50:47 +02001096 for (i = 0; i < num_rx_buf; i++)
David Kubicek61a87ad2005-11-01 18:51:34 +01001097 usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma);
Oliver Neukum86478942006-05-13 22:50:47 +02001098 for (i = 0; i < num_rx_buf; i++)
David Kubicek61a87ad2005-11-01 18:51:34 +01001099 usb_free_urb(acm->ru[i].urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 usb_free_urb(acm->ctrlurb);
1101alloc_fail5:
Oliver Neukum884b6002005-04-21 21:28:02 +02001102 acm_write_buffers_free(acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103alloc_fail4:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 usb_buffer_free(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1105alloc_fail2:
1106 kfree(acm);
1107alloc_fail:
1108 return -ENOMEM;
1109}
1110
Oliver Neukum1365baf2007-10-12 17:24:28 +02001111static void stop_data_traffic(struct acm *acm)
1112{
1113 int i;
1114
1115 tasklet_disable(&acm->urb_task);
1116
1117 usb_kill_urb(acm->ctrlurb);
1118 usb_kill_urb(acm->writeurb);
1119 for (i = 0; i < acm->rx_buflimit; i++)
1120 usb_kill_urb(acm->ru[i].urb);
1121
1122 INIT_LIST_HEAD(&acm->filled_read_bufs);
1123 INIT_LIST_HEAD(&acm->spare_read_bufs);
1124
1125 tasklet_enable(&acm->urb_task);
1126
1127 cancel_work_sync(&acm->work);
1128}
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130static void acm_disconnect(struct usb_interface *intf)
1131{
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001132 struct acm *acm = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 struct usb_device *usb_dev = interface_to_usbdev(intf);
David Kubicek61a87ad2005-11-01 18:51:34 +01001134 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 if (!acm || !acm->dev) {
1137 dbg("disconnect on nonexisting interface");
1138 return;
1139 }
1140
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01001141 mutex_lock(&open_mutex);
Oliver Neukum86067eea2006-01-08 12:39:13 +01001142 if (!usb_get_intfdata(intf)) {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01001143 mutex_unlock(&open_mutex);
Oliver Neukum86067eea2006-01-08 12:39:13 +01001144 return;
1145 }
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001146 if (acm->country_codes){
Alan Stern74da5d62007-08-02 13:29:10 -04001147 device_remove_file(&acm->control->dev,
1148 &dev_attr_wCountryCodes);
1149 device_remove_file(&acm->control->dev,
1150 &dev_attr_iCountryCodeRelDate);
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001151 }
Alan Stern74da5d62007-08-02 13:29:10 -04001152 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 acm->dev = NULL;
Oliver Neukum86067eea2006-01-08 12:39:13 +01001154 usb_set_intfdata(acm->control, NULL);
1155 usb_set_intfdata(acm->data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Oliver Neukum1365baf2007-10-12 17:24:28 +02001157 stop_data_traffic(acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Oliver Neukum884b6002005-04-21 21:28:02 +02001159 acm_write_buffers_free(acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
Oliver Neukum86478942006-05-13 22:50:47 +02001161 for (i = 0; i < acm->rx_buflimit; i++)
David Kubicek61a87ad2005-11-01 18:51:34 +01001162 usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Oliver Neukum86067eea2006-01-08 12:39:13 +01001164 usb_driver_release_interface(&acm_driver, intf == acm->control ? acm->data : intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 if (!acm->used) {
brian@murphy.dk83ef3442005-06-29 16:53:29 -07001167 acm_tty_unregister(acm);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01001168 mutex_unlock(&open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return;
1170 }
1171
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01001172 mutex_unlock(&open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 if (acm->tty)
1175 tty_hangup(acm->tty);
1176}
1177
Oliver Neukum1365baf2007-10-12 17:24:28 +02001178static int acm_suspend(struct usb_interface *intf, pm_message_t message)
1179{
1180 struct acm *acm = usb_get_intfdata(intf);
1181
1182 if (acm->susp_count++)
1183 return 0;
1184 /*
1185 we treat opened interfaces differently,
1186 we must guard against open
1187 */
1188 mutex_lock(&acm->mutex);
1189
1190 if (acm->used)
1191 stop_data_traffic(acm);
1192
1193 mutex_unlock(&acm->mutex);
1194 return 0;
1195}
1196
1197static int acm_resume(struct usb_interface *intf)
1198{
1199 struct acm *acm = usb_get_intfdata(intf);
1200 int rv = 0;
1201
1202 if (--acm->susp_count)
1203 return 0;
1204
1205 mutex_lock(&acm->mutex);
1206 if (acm->used) {
1207 rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO);
1208 if (rv < 0)
1209 goto err_out;
1210
1211 tasklet_schedule(&acm->urb_task);
1212 }
1213
1214err_out:
1215 mutex_unlock(&acm->mutex);
1216 return rv;
1217}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218/*
1219 * USB driver structure.
1220 */
1221
1222static struct usb_device_id acm_ids[] = {
1223 /* quirky and broken devices */
1224 { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
1225 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1226 },
Andrey Arapovb0e2a702007-07-04 17:11:42 +02001227 { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
1228 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1229 },
Masahito Omote8753e652005-07-29 12:17:25 -07001230 { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
1231 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1232 },
Chris Malley91a9c922006-10-03 10:08:28 +01001233 { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
1234 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1235 },
Oliver Neukum86478942006-05-13 22:50:47 +02001236 { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
1237 .driver_info = SINGLE_RX_URB, /* firmware bug */
1238 },
Oliver Neukum3dd2ae82006-06-23 09:14:17 +02001239 { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
1240 .driver_info = SINGLE_RX_URB, /* firmware bug */
1241 },
Oliver Neukum9be84562007-02-12 08:50:03 +01001242 { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
1243 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1244 },
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 /* control interfaces with various AT-command sets */
1247 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1248 USB_CDC_ACM_PROTO_AT_V25TER) },
1249 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1250 USB_CDC_ACM_PROTO_AT_PCCA101) },
1251 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1252 USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
1253 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1254 USB_CDC_ACM_PROTO_AT_GSM) },
1255 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1256 USB_CDC_ACM_PROTO_AT_3G ) },
1257 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1258 USB_CDC_ACM_PROTO_AT_CDMA) },
1259
1260 /* NOTE: COMM/ACM/0xff is likely MSFT RNDIS ... NOT a modem!! */
1261 { }
1262};
1263
1264MODULE_DEVICE_TABLE (usb, acm_ids);
1265
1266static struct usb_driver acm_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 .name = "cdc_acm",
1268 .probe = acm_probe,
1269 .disconnect = acm_disconnect,
Oliver Neukum1365baf2007-10-12 17:24:28 +02001270 .suspend = acm_suspend,
1271 .resume = acm_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 .id_table = acm_ids,
Oliver Neukum1365baf2007-10-12 17:24:28 +02001273 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274};
1275
1276/*
1277 * TTY driver structures.
1278 */
1279
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001280static const struct tty_operations acm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 .open = acm_tty_open,
1282 .close = acm_tty_close,
1283 .write = acm_tty_write,
1284 .write_room = acm_tty_write_room,
1285 .ioctl = acm_tty_ioctl,
1286 .throttle = acm_tty_throttle,
1287 .unthrottle = acm_tty_unthrottle,
1288 .chars_in_buffer = acm_tty_chars_in_buffer,
1289 .break_ctl = acm_tty_break_ctl,
1290 .set_termios = acm_tty_set_termios,
1291 .tiocmget = acm_tty_tiocmget,
1292 .tiocmset = acm_tty_tiocmset,
1293};
1294
1295/*
1296 * Init / exit.
1297 */
1298
1299static int __init acm_init(void)
1300{
1301 int retval;
1302 acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
1303 if (!acm_tty_driver)
1304 return -ENOMEM;
1305 acm_tty_driver->owner = THIS_MODULE,
1306 acm_tty_driver->driver_name = "acm",
1307 acm_tty_driver->name = "ttyACM",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 acm_tty_driver->major = ACM_TTY_MAJOR,
1309 acm_tty_driver->minor_start = 0,
1310 acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
1311 acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07001312 acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 acm_tty_driver->init_termios = tty_std_termios;
1314 acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1315 tty_set_operations(acm_tty_driver, &acm_ops);
1316
1317 retval = tty_register_driver(acm_tty_driver);
1318 if (retval) {
1319 put_tty_driver(acm_tty_driver);
1320 return retval;
1321 }
1322
1323 retval = usb_register(&acm_driver);
1324 if (retval) {
1325 tty_unregister_driver(acm_tty_driver);
1326 put_tty_driver(acm_tty_driver);
1327 return retval;
1328 }
1329
1330 info(DRIVER_VERSION ":" DRIVER_DESC);
1331
1332 return 0;
1333}
1334
1335static void __exit acm_exit(void)
1336{
1337 usb_deregister(&acm_driver);
1338 tty_unregister_driver(acm_tty_driver);
1339 put_tty_driver(acm_tty_driver);
1340}
1341
1342module_init(acm_init);
1343module_exit(acm_exit);
1344
1345MODULE_AUTHOR( DRIVER_AUTHOR );
1346MODULE_DESCRIPTION( DRIVER_DESC );
1347MODULE_LICENSE("GPL");
1348