blob: 20104443081ad1fc260d4ba74f17a0c9828eca69 [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
David Engrafe4cf3aa2008-03-20 10:01:34 +010034 * v0.26 - multiple write urbs, writesize increased
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 */
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
David Brownelle5fbab52008-08-06 18:46:10 -070054#undef VERBOSE_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#include <linux/kernel.h>
57#include <linux/errno.h>
58#include <linux/init.h>
59#include <linux/slab.h>
60#include <linux/tty.h>
61#include <linux/tty_driver.h>
62#include <linux/tty_flip.h>
63#include <linux/module.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010064#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <asm/uaccess.h>
66#include <linux/usb.h>
David Brownella8c28f22006-06-13 09:57:47 -070067#include <linux/usb/cdc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <asm/byteorder.h>
69#include <asm/unaligned.h>
David Kubicek61a87ad2005-11-01 18:51:34 +010070#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72#include "cdc-acm.h"
73
David Brownelle5fbab52008-08-06 18:46:10 -070074
75#define ACM_CLOSE_TIMEOUT 15 /* seconds to let writes drain */
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/*
78 * Version Information
79 */
David Engrafe4cf3aa2008-03-20 10:01:34 +010080#define DRIVER_VERSION "v0.26"
David Kubicek61a87ad2005-11-01 18:51:34 +010081#define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek"
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
83
84static struct usb_driver acm_driver;
85static struct tty_driver *acm_tty_driver;
86static struct acm *acm_table[ACM_TTY_MINORS];
87
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010088static DEFINE_MUTEX(open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90#define ACM_READY(acm) (acm && acm->dev && acm->used)
91
David Brownelle5fbab52008-08-06 18:46:10 -070092#ifdef VERBOSE_DEBUG
93#define verbose 1
94#else
95#define verbose 0
96#endif
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/*
99 * Functions for ACM control messages.
100 */
101
102static int acm_ctrl_msg(struct acm *acm, int request, int value, void *buf, int len)
103{
104 int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
105 request, USB_RT_ACM, value,
106 acm->control->altsetting[0].desc.bInterfaceNumber,
107 buf, len, 5000);
108 dbg("acm_control_msg: rq: 0x%02x val: %#x len: %#x result: %d", request, value, len, retval);
109 return retval < 0 ? retval : 0;
110}
111
112/* devices aren't required to support these requests.
113 * the cdc acm descriptor tells whether they do...
114 */
115#define acm_set_control(acm, control) \
116 acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0)
117#define acm_set_line(acm, line) \
118 acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
119#define acm_send_break(acm, ms) \
120 acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
121
122/*
Oliver Neukum884b6002005-04-21 21:28:02 +0200123 * Write buffer management.
124 * All of these assume proper locks taken by the caller.
125 */
126
127static int acm_wb_alloc(struct acm *acm)
128{
129 int i, wbn;
130 struct acm_wb *wb;
131
David Engrafe4cf3aa2008-03-20 10:01:34 +0100132 wbn = 0;
Oliver Neukum884b6002005-04-21 21:28:02 +0200133 i = 0;
134 for (;;) {
135 wb = &acm->wb[wbn];
136 if (!wb->use) {
137 wb->use = 1;
138 return wbn;
139 }
Oliver Neukum86478942006-05-13 22:50:47 +0200140 wbn = (wbn + 1) % ACM_NW;
141 if (++i >= ACM_NW)
Oliver Neukum884b6002005-04-21 21:28:02 +0200142 return -1;
143 }
144}
145
Oliver Neukum884b6002005-04-21 21:28:02 +0200146static int acm_wb_is_avail(struct acm *acm)
147{
148 int i, n;
David Brownelle5fbab52008-08-06 18:46:10 -0700149 unsigned long flags;
Oliver Neukum884b6002005-04-21 21:28:02 +0200150
Oliver Neukum86478942006-05-13 22:50:47 +0200151 n = ACM_NW;
David Brownelle5fbab52008-08-06 18:46:10 -0700152 spin_lock_irqsave(&acm->write_lock, flags);
Oliver Neukum86478942006-05-13 22:50:47 +0200153 for (i = 0; i < ACM_NW; i++) {
154 n -= acm->wb[i].use;
Oliver Neukum884b6002005-04-21 21:28:02 +0200155 }
David Brownelle5fbab52008-08-06 18:46:10 -0700156 spin_unlock_irqrestore(&acm->write_lock, flags);
Oliver Neukum884b6002005-04-21 21:28:02 +0200157 return n;
158}
159
Oliver Neukum884b6002005-04-21 21:28:02 +0200160/*
161 * Finish write.
162 */
David Engrafe4cf3aa2008-03-20 10:01:34 +0100163static void acm_write_done(struct acm *acm, struct acm_wb *wb)
Oliver Neukum884b6002005-04-21 21:28:02 +0200164{
165 unsigned long flags;
Oliver Neukum884b6002005-04-21 21:28:02 +0200166
167 spin_lock_irqsave(&acm->write_lock, flags);
David Engrafe4cf3aa2008-03-20 10:01:34 +0100168 wb->use = 0;
Oliver Neukum11ea8592008-06-20 11:25:57 +0200169 acm->transmitting--;
Oliver Neukum884b6002005-04-21 21:28:02 +0200170 spin_unlock_irqrestore(&acm->write_lock, flags);
171}
172
173/*
174 * Poke write.
Oliver Neukum11ea8592008-06-20 11:25:57 +0200175 *
176 * the caller is responsible for locking
Oliver Neukum884b6002005-04-21 21:28:02 +0200177 */
Oliver Neukum11ea8592008-06-20 11:25:57 +0200178
179static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
180{
181 int rc;
182
183 acm->transmitting++;
184
185 wb->urb->transfer_buffer = wb->buf;
186 wb->urb->transfer_dma = wb->dmah;
187 wb->urb->transfer_buffer_length = wb->len;
188 wb->urb->dev = acm->dev;
189
190 if ((rc = usb_submit_urb(wb->urb, GFP_ATOMIC)) < 0) {
191 dbg("usb_submit_urb(write bulk) failed: %d", rc);
192 acm_write_done(acm, wb);
193 }
194 return rc;
195}
196
David Engrafe4cf3aa2008-03-20 10:01:34 +0100197static int acm_write_start(struct acm *acm, int wbn)
Oliver Neukum884b6002005-04-21 21:28:02 +0200198{
199 unsigned long flags;
David Brownell934da462008-08-06 18:44:12 -0700200 struct acm_wb *wb = &acm->wb[wbn];
Oliver Neukum884b6002005-04-21 21:28:02 +0200201 int rc;
202
203 spin_lock_irqsave(&acm->write_lock, flags);
204 if (!acm->dev) {
David Brownell934da462008-08-06 18:44:12 -0700205 wb->use = 0;
Oliver Neukum884b6002005-04-21 21:28:02 +0200206 spin_unlock_irqrestore(&acm->write_lock, flags);
207 return -ENODEV;
208 }
209
Oliver Neukum11ea8592008-06-20 11:25:57 +0200210 dbg("%s susp_count: %d", __func__, acm->susp_count);
211 if (acm->susp_count) {
Oliver Neukum11ea8592008-06-20 11:25:57 +0200212 acm->delayed_wb = wb;
Oliver Neukum11ea8592008-06-20 11:25:57 +0200213 schedule_work(&acm->waker);
214 spin_unlock_irqrestore(&acm->write_lock, flags);
215 return 0; /* A white lie */
216 }
217 usb_mark_last_busy(acm->dev);
218
Oliver Neukum11ea8592008-06-20 11:25:57 +0200219 rc = acm_start_wb(acm, wb);
Oliver Neukum884b6002005-04-21 21:28:02 +0200220 spin_unlock_irqrestore(&acm->write_lock, flags);
221
Oliver Neukum884b6002005-04-21 21:28:02 +0200222 return rc;
Oliver Neukum11ea8592008-06-20 11:25:57 +0200223
Oliver Neukum884b6002005-04-21 21:28:02 +0200224}
Oliver Neukumc4cabd22007-02-27 15:28:55 +0100225/*
226 * attributes exported through sysfs
227 */
228static ssize_t show_caps
229(struct device *dev, struct device_attribute *attr, char *buf)
230{
231 struct usb_interface *intf = to_usb_interface(dev);
232 struct acm *acm = usb_get_intfdata(intf);
Oliver Neukum884b6002005-04-21 21:28:02 +0200233
Oliver Neukumc4cabd22007-02-27 15:28:55 +0100234 return sprintf(buf, "%d", acm->ctrl_caps);
235}
236static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
237
238static ssize_t show_country_codes
239(struct device *dev, struct device_attribute *attr, char *buf)
240{
241 struct usb_interface *intf = to_usb_interface(dev);
242 struct acm *acm = usb_get_intfdata(intf);
243
244 memcpy(buf, acm->country_codes, acm->country_code_size);
245 return acm->country_code_size;
246}
247
248static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
249
250static ssize_t show_country_rel_date
251(struct device *dev, struct device_attribute *attr, char *buf)
252{
253 struct usb_interface *intf = to_usb_interface(dev);
254 struct acm *acm = usb_get_intfdata(intf);
255
256 return sprintf(buf, "%d", acm->country_rel_date);
257}
258
259static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
Oliver Neukum884b6002005-04-21 21:28:02 +0200260/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 * Interrupt handlers for various ACM device responses
262 */
263
264/* control interface reports status changes with "interrupt" transfers */
David Howells7d12e782006-10-05 14:55:46 +0100265static void acm_ctrl_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 struct acm *acm = urb->context;
268 struct usb_cdc_notification *dr = urb->transfer_buffer;
269 unsigned char *data;
270 int newctrl;
Greg Kroah-Hartman185d4052007-07-18 10:58:02 -0700271 int retval;
272 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Greg Kroah-Hartman185d4052007-07-18 10:58:02 -0700274 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 case 0:
276 /* success */
277 break;
278 case -ECONNRESET:
279 case -ENOENT:
280 case -ESHUTDOWN:
281 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800282 dbg("%s - urb shutting down with status: %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return;
284 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800285 dbg("%s - nonzero urb status received: %d", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 goto exit;
287 }
288
289 if (!ACM_READY(acm))
290 goto exit;
291
292 data = (unsigned char *)(dr + 1);
293 switch (dr->bNotificationType) {
294
295 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
296
297 dbg("%s network", dr->wValue ? "connected to" : "disconnected from");
298 break;
299
300 case USB_CDC_NOTIFY_SERIAL_STATE:
301
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700302 newctrl = get_unaligned_le16(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 if (acm->tty && !acm->clocal && (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
305 dbg("calling hangup");
306 tty_hangup(acm->tty);
307 }
308
309 acm->ctrlin = newctrl;
310
311 dbg("input control lines: dcd%c dsr%c break%c ring%c framing%c parity%c overrun%c",
312 acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
313 acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
314 acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-', acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
315 acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
316
317 break;
318
319 default:
320 dbg("unknown notification %d received: index %d len %d data0 %d data1 %d",
321 dr->bNotificationType, dr->wIndex,
322 dr->wLength, data[0], data[1]);
323 break;
324 }
325exit:
Oliver Neukum11ea8592008-06-20 11:25:57 +0200326 usb_mark_last_busy(acm->dev);
Greg Kroah-Hartman185d4052007-07-18 10:58:02 -0700327 retval = usb_submit_urb (urb, GFP_ATOMIC);
328 if (retval)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700329 dev_err(&urb->dev->dev, "%s - usb_submit_urb failed with "
330 "result %d", __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
333/* data interface returns incoming bytes, or we got unthrottled */
David Howells7d12e782006-10-05 14:55:46 +0100334static void acm_read_bulk(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
David Kubicek61a87ad2005-11-01 18:51:34 +0100336 struct acm_rb *buf;
337 struct acm_ru *rcv = urb->context;
338 struct acm *acm = rcv->instance;
Oliver Neukum86478942006-05-13 22:50:47 +0200339 int status = urb->status;
Greg Kroah-Hartman185d4052007-07-18 10:58:02 -0700340
341 dbg("Entering acm_read_bulk with status %d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Oliver Neukum11ea8592008-06-20 11:25:57 +0200343 if (!ACM_READY(acm)) {
344 dev_dbg(&acm->data->dev, "Aborting, acm not ready");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return;
Oliver Neukum11ea8592008-06-20 11:25:57 +0200346 }
347 usb_mark_last_busy(acm->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Oliver Neukum86478942006-05-13 22:50:47 +0200349 if (status)
Joe Perches898eb712007-10-18 03:06:30 -0700350 dev_dbg(&acm->data->dev, "bulk rx status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
David Kubicek61a87ad2005-11-01 18:51:34 +0100352 buf = rcv->buffer;
353 buf->size = urb->actual_length;
354
Oliver Neukum86478942006-05-13 22:50:47 +0200355 if (likely(status == 0)) {
356 spin_lock(&acm->read_lock);
Oliver Neukum11ea8592008-06-20 11:25:57 +0200357 acm->processing++;
Oliver Neukum86478942006-05-13 22:50:47 +0200358 list_add_tail(&rcv->list, &acm->spare_read_urbs);
359 list_add_tail(&buf->list, &acm->filled_read_bufs);
360 spin_unlock(&acm->read_lock);
361 } else {
362 /* we drop the buffer due to an error */
363 spin_lock(&acm->read_lock);
364 list_add_tail(&rcv->list, &acm->spare_read_urbs);
365 list_add(&buf->list, &acm->spare_read_bufs);
366 spin_unlock(&acm->read_lock);
367 /* nevertheless the tasklet must be kicked unconditionally
368 so the queue cannot dry up */
369 }
Oliver Neukum11ea8592008-06-20 11:25:57 +0200370 if (likely(!acm->susp_count))
371 tasklet_schedule(&acm->urb_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
374static void acm_rx_tasklet(unsigned long _acm)
375{
376 struct acm *acm = (void *)_acm;
David Kubicek61a87ad2005-11-01 18:51:34 +0100377 struct acm_rb *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 struct tty_struct *tty = acm->tty;
David Kubicek61a87ad2005-11-01 18:51:34 +0100379 struct acm_ru *rcv;
Jarek Poplawski762f0072006-10-06 07:23:11 +0200380 unsigned long flags;
Oliver Neukumca79b7b2007-02-12 08:41:35 +0100381 unsigned char throttled;
Oliver Neukum11ea8592008-06-20 11:25:57 +0200382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 dbg("Entering acm_rx_tasklet");
384
Oliver Neukumca79b7b2007-02-12 08:41:35 +0100385 if (!ACM_READY(acm))
Oliver Neukum11ea8592008-06-20 11:25:57 +0200386 {
387 dbg("acm_rx_tasklet: ACM not ready");
Oliver Neukumca79b7b2007-02-12 08:41:35 +0100388 return;
Oliver Neukum11ea8592008-06-20 11:25:57 +0200389 }
Oliver Neukumca79b7b2007-02-12 08:41:35 +0100390
Oliver Neukum834dbca2007-03-06 10:47:04 +0100391 spin_lock_irqsave(&acm->throttle_lock, flags);
Oliver Neukumca79b7b2007-02-12 08:41:35 +0100392 throttled = acm->throttle;
Oliver Neukum834dbca2007-03-06 10:47:04 +0100393 spin_unlock_irqrestore(&acm->throttle_lock, flags);
Oliver Neukumca79b7b2007-02-12 08:41:35 +0100394 if (throttled)
Oliver Neukum11ea8592008-06-20 11:25:57 +0200395 {
396 dbg("acm_rx_tasklet: throttled");
David Kubicek61a87ad2005-11-01 18:51:34 +0100397 return;
Oliver Neukum11ea8592008-06-20 11:25:57 +0200398 }
David Kubicek61a87ad2005-11-01 18:51:34 +0100399
400next_buffer:
Jarek Poplawski762f0072006-10-06 07:23:11 +0200401 spin_lock_irqsave(&acm->read_lock, flags);
David Kubicek61a87ad2005-11-01 18:51:34 +0100402 if (list_empty(&acm->filled_read_bufs)) {
Jarek Poplawski762f0072006-10-06 07:23:11 +0200403 spin_unlock_irqrestore(&acm->read_lock, flags);
David Kubicek61a87ad2005-11-01 18:51:34 +0100404 goto urbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
David Kubicek61a87ad2005-11-01 18:51:34 +0100406 buf = list_entry(acm->filled_read_bufs.next,
407 struct acm_rb, list);
408 list_del(&buf->list);
Jarek Poplawski762f0072006-10-06 07:23:11 +0200409 spin_unlock_irqrestore(&acm->read_lock, flags);
David Kubicek61a87ad2005-11-01 18:51:34 +0100410
Oliver Neukum3dd2ae82006-06-23 09:14:17 +0200411 dbg("acm_rx_tasklet: procesing buf 0x%p, size = %d", buf, buf->size);
David Kubicek61a87ad2005-11-01 18:51:34 +0100412
Alan Cox33f0f882006-01-09 20:54:13 -0800413 tty_buffer_request_room(tty, buf->size);
Oliver Neukum834dbca2007-03-06 10:47:04 +0100414 spin_lock_irqsave(&acm->throttle_lock, flags);
Oliver Neukumca79b7b2007-02-12 08:41:35 +0100415 throttled = acm->throttle;
Oliver Neukum834dbca2007-03-06 10:47:04 +0100416 spin_unlock_irqrestore(&acm->throttle_lock, flags);
Oliver Neukumca79b7b2007-02-12 08:41:35 +0100417 if (!throttled)
Alan Cox33f0f882006-01-09 20:54:13 -0800418 tty_insert_flip_string(tty, buf->base, buf->size);
David Kubicek61a87ad2005-11-01 18:51:34 +0100419 tty_flip_buffer_push(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Oliver Neukumca79b7b2007-02-12 08:41:35 +0100421 if (throttled) {
422 dbg("Throttling noticed");
Jarek Poplawski762f0072006-10-06 07:23:11 +0200423 spin_lock_irqsave(&acm->read_lock, flags);
David Kubicek61a87ad2005-11-01 18:51:34 +0100424 list_add(&buf->list, &acm->filled_read_bufs);
Jarek Poplawski762f0072006-10-06 07:23:11 +0200425 spin_unlock_irqrestore(&acm->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return;
427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Jarek Poplawski762f0072006-10-06 07:23:11 +0200429 spin_lock_irqsave(&acm->read_lock, flags);
David Kubicek61a87ad2005-11-01 18:51:34 +0100430 list_add(&buf->list, &acm->spare_read_bufs);
Jarek Poplawski762f0072006-10-06 07:23:11 +0200431 spin_unlock_irqrestore(&acm->read_lock, flags);
David Kubicek61a87ad2005-11-01 18:51:34 +0100432 goto next_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
David Kubicek61a87ad2005-11-01 18:51:34 +0100434urbs:
435 while (!list_empty(&acm->spare_read_bufs)) {
Jarek Poplawski762f0072006-10-06 07:23:11 +0200436 spin_lock_irqsave(&acm->read_lock, flags);
David Kubicek61a87ad2005-11-01 18:51:34 +0100437 if (list_empty(&acm->spare_read_urbs)) {
Oliver Neukum11ea8592008-06-20 11:25:57 +0200438 acm->processing = 0;
Jarek Poplawski762f0072006-10-06 07:23:11 +0200439 spin_unlock_irqrestore(&acm->read_lock, flags);
David Kubicek61a87ad2005-11-01 18:51:34 +0100440 return;
441 }
442 rcv = list_entry(acm->spare_read_urbs.next,
443 struct acm_ru, list);
444 list_del(&rcv->list);
Jarek Poplawski762f0072006-10-06 07:23:11 +0200445 spin_unlock_irqrestore(&acm->read_lock, flags);
David Kubicek61a87ad2005-11-01 18:51:34 +0100446
447 buf = list_entry(acm->spare_read_bufs.next,
448 struct acm_rb, list);
449 list_del(&buf->list);
450
451 rcv->buffer = buf;
452
453 usb_fill_bulk_urb(rcv->urb, acm->dev,
454 acm->rx_endpoint,
455 buf->base,
456 acm->readsize,
457 acm_read_bulk, rcv);
458 rcv->urb->transfer_dma = buf->dma;
459 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
460
David Kubicek61a87ad2005-11-01 18:51:34 +0100461 /* This shouldn't kill the driver as unsuccessful URBs are returned to the
462 free-urbs-pool and resubmited ASAP */
Oliver Neukum11ea8592008-06-20 11:25:57 +0200463 spin_lock_irqsave(&acm->read_lock, flags);
464 if (acm->susp_count || usb_submit_urb(rcv->urb, GFP_ATOMIC) < 0) {
David Kubicek61a87ad2005-11-01 18:51:34 +0100465 list_add(&buf->list, &acm->spare_read_bufs);
David Kubicek61a87ad2005-11-01 18:51:34 +0100466 list_add(&rcv->list, &acm->spare_read_urbs);
Oliver Neukum11ea8592008-06-20 11:25:57 +0200467 acm->processing = 0;
Jarek Poplawski762f0072006-10-06 07:23:11 +0200468 spin_unlock_irqrestore(&acm->read_lock, flags);
David Kubicek61a87ad2005-11-01 18:51:34 +0100469 return;
Oliver Neukum11ea8592008-06-20 11:25:57 +0200470 } else {
471 spin_unlock_irqrestore(&acm->read_lock, flags);
472 dbg("acm_rx_tasklet: sending urb 0x%p, rcv 0x%p, buf 0x%p", rcv->urb, rcv, buf);
David Kubicek61a87ad2005-11-01 18:51:34 +0100473 }
474 }
Oliver Neukum11ea8592008-06-20 11:25:57 +0200475 spin_lock_irqsave(&acm->read_lock, flags);
476 acm->processing = 0;
477 spin_unlock_irqrestore(&acm->read_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
480/* data interface wrote those outgoing bytes */
David Howells7d12e782006-10-05 14:55:46 +0100481static void acm_write_bulk(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Ming Leicdc97792008-02-24 18:41:47 +0800483 struct acm_wb *wb = urb->context;
David Brownelle5fbab52008-08-06 18:46:10 -0700484 struct acm *acm = wb->instance;
Oliver Neukum884b6002005-04-21 21:28:02 +0200485
David Brownelle5fbab52008-08-06 18:46:10 -0700486 if (verbose || urb->status
487 || (urb->actual_length != urb->transfer_buffer_length))
488 dev_dbg(&acm->data->dev, "tx %d/%d bytes -- > %d\n",
489 urb->actual_length,
490 urb->transfer_buffer_length,
491 urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
David Engrafe4cf3aa2008-03-20 10:01:34 +0100493 acm_write_done(acm, wb);
Oliver Neukum884b6002005-04-21 21:28:02 +0200494 if (ACM_READY(acm))
495 schedule_work(&acm->work);
David Brownelle5fbab52008-08-06 18:46:10 -0700496 else
497 wake_up_interruptible(&acm->drain_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
David Howellsc4028952006-11-22 14:57:56 +0000500static void acm_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
David Howellsc4028952006-11-22 14:57:56 +0000502 struct acm *acm = container_of(work, struct acm, work);
David Brownelle5fbab52008-08-06 18:46:10 -0700503
504 dev_vdbg(&acm->data->dev, "tx work\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (!ACM_READY(acm))
506 return;
507 tty_wakeup(acm->tty);
508}
509
Oliver Neukum11ea8592008-06-20 11:25:57 +0200510static void acm_waker(struct work_struct *waker)
511{
512 struct acm *acm = container_of(waker, struct acm, waker);
Oliver Neukum11ea8592008-06-20 11:25:57 +0200513 int rv;
514
515 rv = usb_autopm_get_interface(acm->control);
516 if (rv < 0) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700517 dev_err(&acm->dev->dev, "Autopm failure in %s\n", __func__);
Oliver Neukum11ea8592008-06-20 11:25:57 +0200518 return;
519 }
520 if (acm->delayed_wb) {
521 acm_start_wb(acm, acm->delayed_wb);
522 acm->delayed_wb = NULL;
523 }
Oliver Neukum11ea8592008-06-20 11:25:57 +0200524 usb_autopm_put_interface(acm->control);
525}
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527/*
528 * TTY handlers
529 */
530
531static int acm_tty_open(struct tty_struct *tty, struct file *filp)
532{
533 struct acm *acm;
534 int rv = -EINVAL;
David Kubicek61a87ad2005-11-01 18:51:34 +0100535 int i;
Oliver Neukum3dd2ae82006-06-23 09:14:17 +0200536 dbg("Entering acm_tty_open.");
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100537
538 mutex_lock(&open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 acm = acm_table[tty->index];
541 if (!acm || !acm->dev)
542 goto err_out;
543 else
544 rv = 0;
545
David Engraf28d1dfa2008-03-20 10:53:52 +0100546 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 tty->driver_data = acm;
548 acm->tty = tty;
549
David Kubicek61a87ad2005-11-01 18:51:34 +0100550 /* force low_latency on so that our tty_push actually forces the data through,
551 otherwise it is scheduled, and with high data rates data can get lost. */
552 tty->low_latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Oliver Neukum94409cc2008-02-11 15:22:29 +0100554 if (usb_autopm_get_interface(acm->control) < 0)
555 goto early_bail;
Oliver Neukum11ea8592008-06-20 11:25:57 +0200556 else
557 acm->control->needs_remote_wakeup = 1;
Oliver Neukum1365baf2007-10-12 17:24:28 +0200558
559 mutex_lock(&acm->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (acm->used++) {
Oliver Neukum1365baf2007-10-12 17:24:28 +0200561 usb_autopm_put_interface(acm->control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 goto done;
563 }
564
Oliver Neukum1365baf2007-10-12 17:24:28 +0200565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 acm->ctrlurb->dev = acm->dev;
567 if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
568 dbg("usb_submit_urb(ctrl irq) failed");
569 goto bail_out;
570 }
571
Oliver Neukumca79b7b2007-02-12 08:41:35 +0100572 if (0 > acm_set_control(acm, acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS) &&
573 (acm->ctrl_caps & USB_CDC_CAP_LINE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 goto full_bailout;
Oliver Neukum11ea8592008-06-20 11:25:57 +0200575 usb_autopm_put_interface(acm->control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
David Kubicek61a87ad2005-11-01 18:51:34 +0100577 INIT_LIST_HEAD(&acm->spare_read_urbs);
578 INIT_LIST_HEAD(&acm->spare_read_bufs);
579 INIT_LIST_HEAD(&acm->filled_read_bufs);
Oliver Neukum86478942006-05-13 22:50:47 +0200580 for (i = 0; i < acm->rx_buflimit; i++) {
David Kubicek61a87ad2005-11-01 18:51:34 +0100581 list_add(&(acm->ru[i].list), &acm->spare_read_urbs);
582 }
Oliver Neukum86478942006-05-13 22:50:47 +0200583 for (i = 0; i < acm->rx_buflimit; i++) {
David Kubicek61a87ad2005-11-01 18:51:34 +0100584 list_add(&(acm->rb[i].list), &acm->spare_read_bufs);
585 }
586
Oliver Neukumca79b7b2007-02-12 08:41:35 +0100587 acm->throttle = 0;
588
David Kubicek61a87ad2005-11-01 18:51:34 +0100589 tasklet_schedule(&acm->urb_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591done:
Oliver Neukum1365baf2007-10-12 17:24:28 +0200592 mutex_unlock(&acm->mutex);
Alexey Dobriyan74573ee2008-08-20 16:56:04 -0700593err_out:
Oliver Neukum94409cc2008-02-11 15:22:29 +0100594 mutex_unlock(&open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return rv;
596
597full_bailout:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 usb_kill_urb(acm->ctrlurb);
599bail_out:
Oliver Neukum1365baf2007-10-12 17:24:28 +0200600 usb_autopm_put_interface(acm->control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 acm->used--;
Oliver Neukum1365baf2007-10-12 17:24:28 +0200602 mutex_unlock(&acm->mutex);
Oliver Neukum94409cc2008-02-11 15:22:29 +0100603early_bail:
604 mutex_unlock(&open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return -EIO;
606}
607
brian@murphy.dk83ef3442005-06-29 16:53:29 -0700608static void acm_tty_unregister(struct acm *acm)
609{
Oliver Neukum86478942006-05-13 22:50:47 +0200610 int i,nr;
David Kubicek61a87ad2005-11-01 18:51:34 +0100611
Oliver Neukum86478942006-05-13 22:50:47 +0200612 nr = acm->rx_buflimit;
brian@murphy.dk83ef3442005-06-29 16:53:29 -0700613 tty_unregister_device(acm_tty_driver, acm->minor);
614 usb_put_intf(acm->control);
615 acm_table[acm->minor] = NULL;
616 usb_free_urb(acm->ctrlurb);
David Engrafe4cf3aa2008-03-20 10:01:34 +0100617 for (i = 0; i < ACM_NW; i++)
618 usb_free_urb(acm->wb[i].urb);
Oliver Neukum86478942006-05-13 22:50:47 +0200619 for (i = 0; i < nr; i++)
David Kubicek61a87ad2005-11-01 18:51:34 +0100620 usb_free_urb(acm->ru[i].urb);
Oliver Neukumc4cabd22007-02-27 15:28:55 +0100621 kfree(acm->country_codes);
brian@murphy.dk83ef3442005-06-29 16:53:29 -0700622 kfree(acm);
623}
624
David Brownelle5fbab52008-08-06 18:46:10 -0700625static int acm_tty_chars_in_buffer(struct tty_struct *tty);
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627static void acm_tty_close(struct tty_struct *tty, struct file *filp)
628{
629 struct acm *acm = tty->driver_data;
Oliver Neukum86478942006-05-13 22:50:47 +0200630 int i,nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 if (!acm || !acm->used)
633 return;
634
Oliver Neukum86478942006-05-13 22:50:47 +0200635 nr = acm->rx_buflimit;
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100636 mutex_lock(&open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (!--acm->used) {
638 if (acm->dev) {
Oliver Neukum11ea8592008-06-20 11:25:57 +0200639 usb_autopm_get_interface(acm->control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 acm_set_control(acm, acm->ctrlout = 0);
David Brownelle5fbab52008-08-06 18:46:10 -0700641
642 /* try letting the last writes drain naturally */
643 wait_event_interruptible_timeout(acm->drain_wait,
644 (ACM_NW == acm_wb_is_avail(acm))
645 || !acm->dev,
646 ACM_CLOSE_TIMEOUT * HZ);
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 usb_kill_urb(acm->ctrlurb);
David Engrafe4cf3aa2008-03-20 10:01:34 +0100649 for (i = 0; i < ACM_NW; i++)
650 usb_kill_urb(acm->wb[i].urb);
Oliver Neukum86478942006-05-13 22:50:47 +0200651 for (i = 0; i < nr; i++)
David Kubicek61a87ad2005-11-01 18:51:34 +0100652 usb_kill_urb(acm->ru[i].urb);
Oliver Neukum11ea8592008-06-20 11:25:57 +0200653 acm->control->needs_remote_wakeup = 0;
Oliver Neukum1365baf2007-10-12 17:24:28 +0200654 usb_autopm_put_interface(acm->control);
brian@murphy.dk83ef3442005-06-29 16:53:29 -0700655 } else
656 acm_tty_unregister(acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100658 mutex_unlock(&open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
661static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
662{
663 struct acm *acm = tty->driver_data;
664 int stat;
Oliver Neukum884b6002005-04-21 21:28:02 +0200665 unsigned long flags;
666 int wbn;
667 struct acm_wb *wb;
668
Oliver Neukum3dd2ae82006-06-23 09:14:17 +0200669 dbg("Entering acm_tty_write to write %d bytes,", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 if (!ACM_READY(acm))
672 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (!count)
674 return 0;
675
Oliver Neukum884b6002005-04-21 21:28:02 +0200676 spin_lock_irqsave(&acm->write_lock, flags);
677 if ((wbn = acm_wb_alloc(acm)) < 0) {
678 spin_unlock_irqrestore(&acm->write_lock, flags);
Oliver Neukum884b6002005-04-21 21:28:02 +0200679 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
Oliver Neukum884b6002005-04-21 21:28:02 +0200681 wb = &acm->wb[wbn];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Oliver Neukum884b6002005-04-21 21:28:02 +0200683 count = (count > acm->writesize) ? acm->writesize : count;
684 dbg("Get %d bytes...", count);
685 memcpy(wb->buf, buf, count);
686 wb->len = count;
687 spin_unlock_irqrestore(&acm->write_lock, flags);
688
David Engrafe4cf3aa2008-03-20 10:01:34 +0100689 if ((stat = acm_write_start(acm, wbn)) < 0)
Oliver Neukum884b6002005-04-21 21:28:02 +0200690 return stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return count;
692}
693
694static int acm_tty_write_room(struct tty_struct *tty)
695{
696 struct acm *acm = tty->driver_data;
697 if (!ACM_READY(acm))
698 return -EINVAL;
Oliver Neukum884b6002005-04-21 21:28:02 +0200699 /*
700 * Do not let the line discipline to know that we have a reserve,
701 * or it might get too enthusiastic.
702 */
David Brownell934da462008-08-06 18:44:12 -0700703 return acm_wb_is_avail(acm) ? acm->writesize : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
706static int acm_tty_chars_in_buffer(struct tty_struct *tty)
707{
708 struct acm *acm = tty->driver_data;
709 if (!ACM_READY(acm))
710 return -EINVAL;
Oliver Neukum884b6002005-04-21 21:28:02 +0200711 /*
712 * This is inaccurate (overcounts), but it works.
713 */
Oliver Neukum86478942006-05-13 22:50:47 +0200714 return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
717static void acm_tty_throttle(struct tty_struct *tty)
718{
719 struct acm *acm = tty->driver_data;
720 if (!ACM_READY(acm))
721 return;
722 spin_lock_bh(&acm->throttle_lock);
723 acm->throttle = 1;
724 spin_unlock_bh(&acm->throttle_lock);
725}
726
727static void acm_tty_unthrottle(struct tty_struct *tty)
728{
729 struct acm *acm = tty->driver_data;
730 if (!ACM_READY(acm))
731 return;
732 spin_lock_bh(&acm->throttle_lock);
733 acm->throttle = 0;
734 spin_unlock_bh(&acm->throttle_lock);
David Kubicek61a87ad2005-11-01 18:51:34 +0100735 tasklet_schedule(&acm->urb_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Alan Cox9e989662008-07-22 11:18:03 +0100738static int acm_tty_break_ctl(struct tty_struct *tty, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
740 struct acm *acm = tty->driver_data;
Alan Cox9e989662008-07-22 11:18:03 +0100741 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (!ACM_READY(acm))
Alan Cox9e989662008-07-22 11:18:03 +0100743 return -EINVAL;
744 retval = acm_send_break(acm, state ? 0xffff : 0);
745 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 dbg("send break failed");
Alan Cox9e989662008-07-22 11:18:03 +0100747 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
750static int acm_tty_tiocmget(struct tty_struct *tty, struct file *file)
751{
752 struct acm *acm = tty->driver_data;
753
754 if (!ACM_READY(acm))
755 return -EINVAL;
756
757 return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
758 (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
759 (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
760 (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
761 (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
762 TIOCM_CTS;
763}
764
765static int acm_tty_tiocmset(struct tty_struct *tty, struct file *file,
766 unsigned int set, unsigned int clear)
767{
768 struct acm *acm = tty->driver_data;
769 unsigned int newctrl;
770
771 if (!ACM_READY(acm))
772 return -EINVAL;
773
774 newctrl = acm->ctrlout;
775 set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
776 clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
777
778 newctrl = (newctrl & ~clear) | set;
779
780 if (acm->ctrlout == newctrl)
781 return 0;
782 return acm_set_control(acm, acm->ctrlout = newctrl);
783}
784
785static int acm_tty_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
786{
787 struct acm *acm = tty->driver_data;
788
789 if (!ACM_READY(acm))
790 return -EINVAL;
791
792 return -ENOIOCTLCMD;
793}
794
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100795static const __u32 acm_tty_speed[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 0, 50, 75, 110, 134, 150, 200, 300, 600,
797 1200, 1800, 2400, 4800, 9600, 19200, 38400,
798 57600, 115200, 230400, 460800, 500000, 576000,
799 921600, 1000000, 1152000, 1500000, 2000000,
800 2500000, 3000000, 3500000, 4000000
801};
802
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100803static const __u8 acm_tty_size[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 5, 6, 7, 8
805};
806
Alan Cox606d0992006-12-08 02:38:45 -0800807static void acm_tty_set_termios(struct tty_struct *tty, struct ktermios *termios_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
809 struct acm *acm = tty->driver_data;
Alan Cox606d0992006-12-08 02:38:45 -0800810 struct ktermios *termios = tty->termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 struct usb_cdc_line_coding newline;
812 int newctrl = acm->ctrlout;
813
814 if (!ACM_READY(acm))
815 return;
816
817 newline.dwDTERate = cpu_to_le32p(acm_tty_speed +
818 (termios->c_cflag & CBAUD & ~CBAUDEX) + (termios->c_cflag & CBAUDEX ? 15 : 0));
819 newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
820 newline.bParityType = termios->c_cflag & PARENB ?
821 (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
822 newline.bDataBits = acm_tty_size[(termios->c_cflag & CSIZE) >> 4];
823
824 acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
825
826 if (!newline.dwDTERate) {
827 newline.dwDTERate = acm->line.dwDTERate;
828 newctrl &= ~ACM_CTRL_DTR;
829 } else newctrl |= ACM_CTRL_DTR;
830
831 if (newctrl != acm->ctrlout)
832 acm_set_control(acm, acm->ctrlout = newctrl);
833
834 if (memcmp(&acm->line, &newline, sizeof newline)) {
835 memcpy(&acm->line, &newline, sizeof newline);
836 dbg("set line: %d %d %d %d", le32_to_cpu(newline.dwDTERate),
837 newline.bCharFormat, newline.bParityType,
838 newline.bDataBits);
839 acm_set_line(acm, &acm->line);
840 }
841}
842
843/*
844 * USB probe and disconnect routines.
845 */
846
Oliver Neukum830f4022008-06-25 14:17:16 +0200847/* Little helpers: write/read buffers free */
Oliver Neukum884b6002005-04-21 21:28:02 +0200848static void acm_write_buffers_free(struct acm *acm)
849{
850 int i;
851 struct acm_wb *wb;
Oliver Neukuma496c642008-10-21 10:39:04 +0200852 struct usb_device *usb_dev = interface_to_usbdev(acm->control);
Oliver Neukum884b6002005-04-21 21:28:02 +0200853
Oliver Neukum86478942006-05-13 22:50:47 +0200854 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
Oliver Neukuma496c642008-10-21 10:39:04 +0200855 usb_buffer_free(usb_dev, acm->writesize, wb->buf, wb->dmah);
Oliver Neukum884b6002005-04-21 21:28:02 +0200856 }
857}
858
Oliver Neukum830f4022008-06-25 14:17:16 +0200859static void acm_read_buffers_free(struct acm *acm)
860{
861 struct usb_device *usb_dev = interface_to_usbdev(acm->control);
862 int i, n = acm->rx_buflimit;
863
864 for (i = 0; i < n; i++)
865 usb_buffer_free(usb_dev, acm->readsize, acm->rb[i].base, acm->rb[i].dma);
866}
867
Oliver Neukum884b6002005-04-21 21:28:02 +0200868/* Little helper: write buffers allocate */
869static int acm_write_buffers_alloc(struct acm *acm)
870{
871 int i;
872 struct acm_wb *wb;
873
Oliver Neukum86478942006-05-13 22:50:47 +0200874 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
Oliver Neukum884b6002005-04-21 21:28:02 +0200875 wb->buf = usb_buffer_alloc(acm->dev, acm->writesize, GFP_KERNEL,
876 &wb->dmah);
877 if (!wb->buf) {
878 while (i != 0) {
879 --i;
880 --wb;
881 usb_buffer_free(acm->dev, acm->writesize,
882 wb->buf, wb->dmah);
883 }
884 return -ENOMEM;
885 }
886 }
887 return 0;
888}
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890static int acm_probe (struct usb_interface *intf,
891 const struct usb_device_id *id)
892{
893 struct usb_cdc_union_desc *union_header = NULL;
Oliver Neukumc4cabd22007-02-27 15:28:55 +0100894 struct usb_cdc_country_functional_desc *cfd = NULL;
David Brownellc6dbf552008-04-13 14:00:44 -0700895 unsigned char *buffer = intf->altsetting->extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 int buflen = intf->altsetting->extralen;
897 struct usb_interface *control_interface;
898 struct usb_interface *data_interface;
899 struct usb_endpoint_descriptor *epctrl;
900 struct usb_endpoint_descriptor *epread;
901 struct usb_endpoint_descriptor *epwrite;
902 struct usb_device *usb_dev = interface_to_usbdev(intf);
903 struct acm *acm;
904 int minor;
905 int ctrlsize,readsize;
906 u8 *buf;
907 u8 ac_management_function = 0;
908 u8 call_management_function = 0;
909 int call_interface_num = -1;
910 int data_interface_num;
911 unsigned long quirks;
Oliver Neukum86478942006-05-13 22:50:47 +0200912 int num_rx_buf;
David Kubicek61a87ad2005-11-01 18:51:34 +0100913 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Oliver Neukum86478942006-05-13 22:50:47 +0200915 /* normal quirks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 quirks = (unsigned long)id->driver_info;
Oliver Neukum86478942006-05-13 22:50:47 +0200917 num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
918
919 /* handle quirks deadly to normal probing*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (quirks == NO_UNION_NORMAL) {
921 data_interface = usb_ifnum_to_if(usb_dev, 1);
922 control_interface = usb_ifnum_to_if(usb_dev, 0);
923 goto skip_normal_probe;
924 }
925
926 /* normal probing*/
927 if (!buffer) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700928 dev_err(&intf->dev, "Weird descriptor references\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return -EINVAL;
930 }
931
932 if (!buflen) {
933 if (intf->cur_altsetting->endpoint->extralen && intf->cur_altsetting->endpoint->extra) {
Joe Perches898eb712007-10-18 03:06:30 -0700934 dev_dbg(&intf->dev,"Seeking extra descriptors on endpoint\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 buflen = intf->cur_altsetting->endpoint->extralen;
936 buffer = intf->cur_altsetting->endpoint->extra;
937 } else {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700938 dev_err(&intf->dev,
939 "Zero length descriptor references\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return -EINVAL;
941 }
942 }
943
944 while (buflen > 0) {
945 if (buffer [1] != USB_DT_CS_INTERFACE) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700946 dev_err(&intf->dev, "skipping garbage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 goto next_desc;
948 }
949
950 switch (buffer [2]) {
951 case USB_CDC_UNION_TYPE: /* we've found it */
952 if (union_header) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700953 dev_err(&intf->dev, "More than one "
954 "union descriptor, "
955 "skipping ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 goto next_desc;
957 }
958 union_header = (struct usb_cdc_union_desc *)
959 buffer;
960 break;
Oliver Neukumc4cabd22007-02-27 15:28:55 +0100961 case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
962 cfd = (struct usb_cdc_country_functional_desc *)buffer;
963 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 case USB_CDC_HEADER_TYPE: /* maybe check version */
965 break; /* for now we ignore it */
966 case USB_CDC_ACM_TYPE:
967 ac_management_function = buffer[3];
968 break;
969 case USB_CDC_CALL_MANAGEMENT_TYPE:
970 call_management_function = buffer[3];
971 call_interface_num = buffer[4];
972 if ((call_management_function & 3) != 3)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700973 dev_err(&intf->dev, "This device "
974 "cannot do calls on its own. "
975 "It is no modem.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 default:
David Brownellc6dbf552008-04-13 14:00:44 -0700978 /* there are LOTS more CDC descriptors that
979 * could legitimately be found here.
980 */
981 dev_dbg(&intf->dev, "Ignoring descriptor: "
982 "type %02x, length %d\n",
983 buffer[2], buffer[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 break;
985 }
986next_desc:
987 buflen -= buffer[0];
988 buffer += buffer[0];
989 }
990
991 if (!union_header) {
992 if (call_interface_num > 0) {
Joe Perches898eb712007-10-18 03:06:30 -0700993 dev_dbg(&intf->dev,"No union descriptor, using call management descriptor\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
995 control_interface = intf;
996 } else {
Joe Perches898eb712007-10-18 03:06:30 -0700997 dev_dbg(&intf->dev,"No union descriptor, giving up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return -ENODEV;
999 }
1000 } else {
1001 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
1002 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
1003 if (!control_interface || !data_interface) {
Joe Perches898eb712007-10-18 03:06:30 -07001004 dev_dbg(&intf->dev,"no interfaces\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return -ENODEV;
1006 }
1007 }
1008
1009 if (data_interface_num != call_interface_num)
Joe Perchesdc0d5c12007-12-17 11:40:18 -08001010 dev_dbg(&intf->dev,"Separate call control interface. That is not fully supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012skip_normal_probe:
1013
1014 /*workaround for switched interfaces */
1015 if (data_interface->cur_altsetting->desc.bInterfaceClass != CDC_DATA_INTERFACE_TYPE) {
1016 if (control_interface->cur_altsetting->desc.bInterfaceClass == CDC_DATA_INTERFACE_TYPE) {
1017 struct usb_interface *t;
Joe Perches898eb712007-10-18 03:06:30 -07001018 dev_dbg(&intf->dev,"Your device has switched interfaces.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 t = control_interface;
1021 control_interface = data_interface;
1022 data_interface = t;
1023 } else {
1024 return -EINVAL;
1025 }
1026 }
Alan Stern74da5d62007-08-02 13:29:10 -04001027
1028 /* Accept probe requests only for the control interface */
1029 if (intf != control_interface)
1030 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 if (usb_interface_claimed(data_interface)) { /* valid in this context */
Joe Perches898eb712007-10-18 03:06:30 -07001033 dev_dbg(&intf->dev,"The data interface isn't available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return -EBUSY;
1035 }
1036
1037
1038 if (data_interface->cur_altsetting->desc.bNumEndpoints < 2)
1039 return -EINVAL;
1040
1041 epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
1042 epread = &data_interface->cur_altsetting->endpoint[0].desc;
1043 epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
1044
1045
1046 /* workaround for switched endpoints */
Luiz Fernando N. Capitulino45aea702006-10-26 13:02:48 -03001047 if (!usb_endpoint_dir_in(epread)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 /* descriptors are swapped */
1049 struct usb_endpoint_descriptor *t;
Joe Perches898eb712007-10-18 03:06:30 -07001050 dev_dbg(&intf->dev,"The data interface has switched endpoints\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 t = epread;
1053 epread = epwrite;
1054 epwrite = t;
1055 }
1056 dbg("interfaces are valid");
1057 for (minor = 0; minor < ACM_TTY_MINORS && acm_table[minor]; minor++);
1058
1059 if (minor == ACM_TTY_MINORS) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -07001060 dev_err(&intf->dev, "no more free acm devices\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return -ENODEV;
1062 }
1063
Oliver Neukum46f116e2005-10-24 22:42:35 +02001064 if (!(acm = kzalloc(sizeof(struct acm), GFP_KERNEL))) {
Joe Perches898eb712007-10-18 03:06:30 -07001065 dev_dbg(&intf->dev, "out of memory (acm kzalloc)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 goto alloc_fail;
1067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 ctrlsize = le16_to_cpu(epctrl->wMaxPacketSize);
Oliver Neukum86478942006-05-13 22:50:47 +02001070 readsize = le16_to_cpu(epread->wMaxPacketSize)* ( quirks == SINGLE_RX_URB ? 1 : 2);
David Engrafe4cf3aa2008-03-20 10:01:34 +01001071 acm->writesize = le16_to_cpu(epwrite->wMaxPacketSize) * 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 acm->control = control_interface;
1073 acm->data = data_interface;
1074 acm->minor = minor;
1075 acm->dev = usb_dev;
1076 acm->ctrl_caps = ac_management_function;
1077 acm->ctrlsize = ctrlsize;
1078 acm->readsize = readsize;
Oliver Neukum86478942006-05-13 22:50:47 +02001079 acm->rx_buflimit = num_rx_buf;
David Kubicek61a87ad2005-11-01 18:51:34 +01001080 acm->urb_task.func = acm_rx_tasklet;
1081 acm->urb_task.data = (unsigned long) acm;
David Howellsc4028952006-11-22 14:57:56 +00001082 INIT_WORK(&acm->work, acm_softint);
Oliver Neukum11ea8592008-06-20 11:25:57 +02001083 INIT_WORK(&acm->waker, acm_waker);
David Brownelle5fbab52008-08-06 18:46:10 -07001084 init_waitqueue_head(&acm->drain_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 spin_lock_init(&acm->throttle_lock);
Oliver Neukum884b6002005-04-21 21:28:02 +02001086 spin_lock_init(&acm->write_lock);
David Kubicek61a87ad2005-11-01 18:51:34 +01001087 spin_lock_init(&acm->read_lock);
Oliver Neukum1365baf2007-10-12 17:24:28 +02001088 mutex_init(&acm->mutex);
David Kubicek61a87ad2005-11-01 18:51:34 +01001089 acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 buf = usb_buffer_alloc(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
1092 if (!buf) {
Joe Perches898eb712007-10-18 03:06:30 -07001093 dev_dbg(&intf->dev, "out of memory (ctrl buffer alloc)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 goto alloc_fail2;
1095 }
1096 acm->ctrl_buffer = buf;
1097
Oliver Neukum884b6002005-04-21 21:28:02 +02001098 if (acm_write_buffers_alloc(acm) < 0) {
Joe Perches898eb712007-10-18 03:06:30 -07001099 dev_dbg(&intf->dev, "out of memory (write buffer alloc)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 goto alloc_fail4;
1101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
1104 if (!acm->ctrlurb) {
Joe Perches898eb712007-10-18 03:06:30 -07001105 dev_dbg(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 goto alloc_fail5;
1107 }
Oliver Neukum86478942006-05-13 22:50:47 +02001108 for (i = 0; i < num_rx_buf; i++) {
David Kubicek61a87ad2005-11-01 18:51:34 +01001109 struct acm_ru *rcv = &(acm->ru[i]);
1110
1111 if (!(rcv->urb = usb_alloc_urb(0, GFP_KERNEL))) {
Joe Perches898eb712007-10-18 03:06:30 -07001112 dev_dbg(&intf->dev, "out of memory (read urbs usb_alloc_urb)\n");
David Kubicek61a87ad2005-11-01 18:51:34 +01001113 goto alloc_fail7;
1114 }
1115
1116 rcv->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1117 rcv->instance = acm;
1118 }
Oliver Neukum86478942006-05-13 22:50:47 +02001119 for (i = 0; i < num_rx_buf; i++) {
David Brownell672c4e12008-08-06 18:41:12 -07001120 struct acm_rb *rb = &(acm->rb[i]);
David Kubicek61a87ad2005-11-01 18:51:34 +01001121
David Brownell672c4e12008-08-06 18:41:12 -07001122 rb->base = usb_buffer_alloc(acm->dev, readsize,
1123 GFP_KERNEL, &rb->dma);
1124 if (!rb->base) {
Joe Perches898eb712007-10-18 03:06:30 -07001125 dev_dbg(&intf->dev, "out of memory (read bufs usb_buffer_alloc)\n");
David Kubicek61a87ad2005-11-01 18:51:34 +01001126 goto alloc_fail7;
1127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
David Engrafe4cf3aa2008-03-20 10:01:34 +01001129 for(i = 0; i < ACM_NW; i++)
1130 {
1131 struct acm_wb *snd = &(acm->wb[i]);
1132
1133 if (!(snd->urb = usb_alloc_urb(0, GFP_KERNEL))) {
1134 dev_dbg(&intf->dev, "out of memory (write urbs usb_alloc_urb)");
1135 goto alloc_fail7;
1136 }
1137
1138 usb_fill_bulk_urb(snd->urb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
1139 NULL, acm->writesize, acm_write_bulk, snd);
1140 snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1141 snd->instance = acm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001144 usb_set_intfdata (intf, acm);
1145
1146 i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
1147 if (i < 0)
1148 goto alloc_fail8;
1149
1150 if (cfd) { /* export the country data */
1151 acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
1152 if (!acm->country_codes)
1153 goto skip_countries;
1154 acm->country_code_size = cfd->bLength - 4;
1155 memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0, cfd->bLength - 4);
1156 acm->country_rel_date = cfd->iCountryCodeRelDate;
1157
1158 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
1159 if (i < 0) {
1160 kfree(acm->country_codes);
1161 goto skip_countries;
1162 }
1163
1164 i = device_create_file(&intf->dev, &dev_attr_iCountryCodeRelDate);
1165 if (i < 0) {
1166 kfree(acm->country_codes);
1167 goto skip_countries;
1168 }
1169 }
1170
1171skip_countries:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 usb_fill_int_urb(acm->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
1173 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, epctrl->bInterval);
1174 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1175 acm->ctrlurb->transfer_dma = acm->ctrl_dma;
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
1178
1179 acm_set_control(acm, acm->ctrlout);
1180
1181 acm->line.dwDTERate = cpu_to_le32(9600);
1182 acm->line.bDataBits = 8;
1183 acm_set_line(acm, &acm->line);
1184
1185 usb_driver_claim_interface(&acm_driver, data_interface, acm);
David Brownell672c4e12008-08-06 18:41:12 -07001186 usb_set_intfdata(data_interface, acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
brian@murphy.dk83ef3442005-06-29 16:53:29 -07001188 usb_get_intf(control_interface);
1189 tty_register_device(acm_tty_driver, minor, &control_interface->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 acm_table[minor] = acm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001193 return 0;
1194alloc_fail8:
David Engrafe4cf3aa2008-03-20 10:01:34 +01001195 for (i = 0; i < ACM_NW; i++)
1196 usb_free_urb(acm->wb[i].urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197alloc_fail7:
Oliver Neukum830f4022008-06-25 14:17:16 +02001198 acm_read_buffers_free(acm);
Oliver Neukum86478942006-05-13 22:50:47 +02001199 for (i = 0; i < num_rx_buf; i++)
David Kubicek61a87ad2005-11-01 18:51:34 +01001200 usb_free_urb(acm->ru[i].urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 usb_free_urb(acm->ctrlurb);
1202alloc_fail5:
Oliver Neukum884b6002005-04-21 21:28:02 +02001203 acm_write_buffers_free(acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204alloc_fail4:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 usb_buffer_free(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1206alloc_fail2:
1207 kfree(acm);
1208alloc_fail:
1209 return -ENOMEM;
1210}
1211
Oliver Neukum1365baf2007-10-12 17:24:28 +02001212static void stop_data_traffic(struct acm *acm)
1213{
1214 int i;
Oliver Neukum11ea8592008-06-20 11:25:57 +02001215 dbg("Entering stop_data_traffic");
Oliver Neukum1365baf2007-10-12 17:24:28 +02001216
1217 tasklet_disable(&acm->urb_task);
1218
1219 usb_kill_urb(acm->ctrlurb);
David Engrafe4cf3aa2008-03-20 10:01:34 +01001220 for(i = 0; i < ACM_NW; i++)
1221 usb_kill_urb(acm->wb[i].urb);
Oliver Neukum1365baf2007-10-12 17:24:28 +02001222 for (i = 0; i < acm->rx_buflimit; i++)
1223 usb_kill_urb(acm->ru[i].urb);
1224
Oliver Neukum1365baf2007-10-12 17:24:28 +02001225 tasklet_enable(&acm->urb_task);
1226
1227 cancel_work_sync(&acm->work);
Oliver Neukum11ea8592008-06-20 11:25:57 +02001228 cancel_work_sync(&acm->waker);
Oliver Neukum1365baf2007-10-12 17:24:28 +02001229}
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231static void acm_disconnect(struct usb_interface *intf)
1232{
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001233 struct acm *acm = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 struct usb_device *usb_dev = interface_to_usbdev(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
David Brownell672c4e12008-08-06 18:41:12 -07001236 /* sibling interface is already cleaning up */
1237 if (!acm)
Oliver Neukum86067eea2006-01-08 12:39:13 +01001238 return;
David Brownell672c4e12008-08-06 18:41:12 -07001239
1240 mutex_lock(&open_mutex);
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001241 if (acm->country_codes){
Alan Stern74da5d62007-08-02 13:29:10 -04001242 device_remove_file(&acm->control->dev,
1243 &dev_attr_wCountryCodes);
1244 device_remove_file(&acm->control->dev,
1245 &dev_attr_iCountryCodeRelDate);
Oliver Neukumc4cabd22007-02-27 15:28:55 +01001246 }
Alan Stern74da5d62007-08-02 13:29:10 -04001247 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 acm->dev = NULL;
Oliver Neukum86067eea2006-01-08 12:39:13 +01001249 usb_set_intfdata(acm->control, NULL);
1250 usb_set_intfdata(acm->data, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Oliver Neukum1365baf2007-10-12 17:24:28 +02001252 stop_data_traffic(acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Oliver Neukum884b6002005-04-21 21:28:02 +02001254 acm_write_buffers_free(acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 usb_buffer_free(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
Oliver Neukum830f4022008-06-25 14:17:16 +02001256 acm_read_buffers_free(acm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Oliver Neukum830f4022008-06-25 14:17:16 +02001258 usb_driver_release_interface(&acm_driver, intf == acm->control ?
1259 acm->data : acm->control);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 if (!acm->used) {
brian@murphy.dk83ef3442005-06-29 16:53:29 -07001262 acm_tty_unregister(acm);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01001263 mutex_unlock(&open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return;
1265 }
1266
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01001267 mutex_unlock(&open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 if (acm->tty)
1270 tty_hangup(acm->tty);
1271}
1272
Oliver Neukum35758582008-07-01 19:10:08 +02001273#ifdef CONFIG_PM
Oliver Neukum1365baf2007-10-12 17:24:28 +02001274static int acm_suspend(struct usb_interface *intf, pm_message_t message)
1275{
1276 struct acm *acm = usb_get_intfdata(intf);
Oliver Neukum11ea8592008-06-20 11:25:57 +02001277 int cnt;
Oliver Neukum1365baf2007-10-12 17:24:28 +02001278
Oliver Neukum11ea8592008-06-20 11:25:57 +02001279 if (acm->dev->auto_pm) {
1280 int b;
1281
1282 spin_lock_irq(&acm->read_lock);
1283 spin_lock(&acm->write_lock);
1284 b = acm->processing + acm->transmitting;
1285 spin_unlock(&acm->write_lock);
1286 spin_unlock_irq(&acm->read_lock);
1287 if (b)
1288 return -EBUSY;
1289 }
1290
1291 spin_lock_irq(&acm->read_lock);
1292 spin_lock(&acm->write_lock);
1293 cnt = acm->susp_count++;
1294 spin_unlock(&acm->write_lock);
1295 spin_unlock_irq(&acm->read_lock);
1296
1297 if (cnt)
Oliver Neukum1365baf2007-10-12 17:24:28 +02001298 return 0;
1299 /*
1300 we treat opened interfaces differently,
1301 we must guard against open
1302 */
1303 mutex_lock(&acm->mutex);
1304
1305 if (acm->used)
1306 stop_data_traffic(acm);
1307
1308 mutex_unlock(&acm->mutex);
1309 return 0;
1310}
1311
1312static int acm_resume(struct usb_interface *intf)
1313{
1314 struct acm *acm = usb_get_intfdata(intf);
1315 int rv = 0;
Oliver Neukum11ea8592008-06-20 11:25:57 +02001316 int cnt;
Oliver Neukum1365baf2007-10-12 17:24:28 +02001317
Oliver Neukum11ea8592008-06-20 11:25:57 +02001318 spin_lock_irq(&acm->read_lock);
1319 acm->susp_count -= 1;
1320 cnt = acm->susp_count;
1321 spin_unlock_irq(&acm->read_lock);
1322
1323 if (cnt)
Oliver Neukum1365baf2007-10-12 17:24:28 +02001324 return 0;
1325
1326 mutex_lock(&acm->mutex);
1327 if (acm->used) {
1328 rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO);
1329 if (rv < 0)
Oliver Neukum11ea8592008-06-20 11:25:57 +02001330 goto err_out;
Oliver Neukum1365baf2007-10-12 17:24:28 +02001331
1332 tasklet_schedule(&acm->urb_task);
1333 }
1334
1335err_out:
1336 mutex_unlock(&acm->mutex);
1337 return rv;
1338}
Oliver Neukum35758582008-07-01 19:10:08 +02001339
1340#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341/*
1342 * USB driver structure.
1343 */
1344
1345static struct usb_device_id acm_ids[] = {
1346 /* quirky and broken devices */
1347 { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
1348 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1349 },
Andrey Arapovb0e2a702007-07-04 17:11:42 +02001350 { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
1351 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1352 },
Masahito Omote8753e652005-07-29 12:17:25 -07001353 { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
1354 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1355 },
Chris Malley91a9c922006-10-03 10:08:28 +01001356 { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
1357 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1358 },
Oliver Neukum86478942006-05-13 22:50:47 +02001359 { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
1360 .driver_info = SINGLE_RX_URB, /* firmware bug */
1361 },
Oliver Neukum3dd2ae82006-06-23 09:14:17 +02001362 { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
1363 .driver_info = SINGLE_RX_URB, /* firmware bug */
1364 },
Oliver Neukum9be84562007-02-12 08:50:03 +01001365 { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
1366 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1367 },
Iain McFarlane6149ed52008-05-04 00:13:49 +01001368 { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
1369 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1370 },
Eric Sandeenc8fd2c32008-08-14 08:25:40 -05001371 { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */
1372 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1373 },
Oliver Neukum9be84562007-02-12 08:50:03 +01001374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 /* control interfaces with various AT-command sets */
1376 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1377 USB_CDC_ACM_PROTO_AT_V25TER) },
1378 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1379 USB_CDC_ACM_PROTO_AT_PCCA101) },
1380 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1381 USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
1382 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1383 USB_CDC_ACM_PROTO_AT_GSM) },
1384 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1385 USB_CDC_ACM_PROTO_AT_3G ) },
1386 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1387 USB_CDC_ACM_PROTO_AT_CDMA) },
1388
1389 /* NOTE: COMM/ACM/0xff is likely MSFT RNDIS ... NOT a modem!! */
1390 { }
1391};
1392
1393MODULE_DEVICE_TABLE (usb, acm_ids);
1394
1395static struct usb_driver acm_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 .name = "cdc_acm",
1397 .probe = acm_probe,
1398 .disconnect = acm_disconnect,
Oliver Neukum35758582008-07-01 19:10:08 +02001399#ifdef CONFIG_PM
Oliver Neukum1365baf2007-10-12 17:24:28 +02001400 .suspend = acm_suspend,
1401 .resume = acm_resume,
Oliver Neukum35758582008-07-01 19:10:08 +02001402#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 .id_table = acm_ids,
Oliver Neukum35758582008-07-01 19:10:08 +02001404#ifdef CONFIG_PM
Oliver Neukum1365baf2007-10-12 17:24:28 +02001405 .supports_autosuspend = 1,
Oliver Neukum35758582008-07-01 19:10:08 +02001406#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407};
1408
1409/*
1410 * TTY driver structures.
1411 */
1412
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001413static const struct tty_operations acm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 .open = acm_tty_open,
1415 .close = acm_tty_close,
1416 .write = acm_tty_write,
1417 .write_room = acm_tty_write_room,
1418 .ioctl = acm_tty_ioctl,
1419 .throttle = acm_tty_throttle,
1420 .unthrottle = acm_tty_unthrottle,
1421 .chars_in_buffer = acm_tty_chars_in_buffer,
1422 .break_ctl = acm_tty_break_ctl,
1423 .set_termios = acm_tty_set_termios,
1424 .tiocmget = acm_tty_tiocmget,
1425 .tiocmset = acm_tty_tiocmset,
1426};
1427
1428/*
1429 * Init / exit.
1430 */
1431
1432static int __init acm_init(void)
1433{
1434 int retval;
1435 acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
1436 if (!acm_tty_driver)
1437 return -ENOMEM;
1438 acm_tty_driver->owner = THIS_MODULE,
1439 acm_tty_driver->driver_name = "acm",
1440 acm_tty_driver->name = "ttyACM",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 acm_tty_driver->major = ACM_TTY_MAJOR,
1442 acm_tty_driver->minor_start = 0,
1443 acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
1444 acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07001445 acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 acm_tty_driver->init_termios = tty_std_termios;
1447 acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1448 tty_set_operations(acm_tty_driver, &acm_ops);
1449
1450 retval = tty_register_driver(acm_tty_driver);
1451 if (retval) {
1452 put_tty_driver(acm_tty_driver);
1453 return retval;
1454 }
1455
1456 retval = usb_register(&acm_driver);
1457 if (retval) {
1458 tty_unregister_driver(acm_tty_driver);
1459 put_tty_driver(acm_tty_driver);
1460 return retval;
1461 }
1462
Greg Kroah-Hartman5909f6e2008-08-18 13:21:04 -07001463 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1464 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 return 0;
1467}
1468
1469static void __exit acm_exit(void)
1470{
1471 usb_deregister(&acm_driver);
1472 tty_unregister_driver(acm_tty_driver);
1473 put_tty_driver(acm_tty_driver);
1474}
1475
1476module_init(acm_init);
1477module_exit(acm_exit);
1478
1479MODULE_AUTHOR( DRIVER_AUTHOR );
1480MODULE_DESCRIPTION( DRIVER_DESC );
1481MODULE_LICENSE("GPL");
1482