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