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