Commit | Line | Data |
---|---|---|
afba937e ON |
1 | /* |
2 | * cdc-wdm.c | |
3 | * | |
4 | * This driver supports USB CDC WCM Device Management. | |
5 | * | |
052fbc0d | 6 | * Copyright (c) 2007-2009 Oliver Neukum |
afba937e ON |
7 | * |
8 | * Some code taken from cdc-acm.c | |
9 | * | |
10 | * Released under the GPLv2. | |
11 | * | |
12 | * Many thanks to Carl Nordbeck | |
13 | */ | |
14 | #include <linux/kernel.h> | |
15 | #include <linux/errno.h> | |
16 | #include <linux/slab.h> | |
17 | #include <linux/module.h> | |
afba937e ON |
18 | #include <linux/mutex.h> |
19 | #include <linux/uaccess.h> | |
20 | #include <linux/bitops.h> | |
21 | #include <linux/poll.h> | |
22 | #include <linux/usb.h> | |
23 | #include <linux/usb/cdc.h> | |
24 | #include <asm/byteorder.h> | |
25 | #include <asm/unaligned.h> | |
26 | ||
27 | /* | |
28 | * Version Information | |
29 | */ | |
87d65e54 | 30 | #define DRIVER_VERSION "v0.03" |
afba937e | 31 | #define DRIVER_AUTHOR "Oliver Neukum" |
87d65e54 | 32 | #define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management" |
afba937e | 33 | |
fec67b45 BM |
34 | #define HUAWEI_VENDOR_ID 0x12D1 |
35 | ||
6ef4852b | 36 | static const struct usb_device_id wdm_ids[] = { |
afba937e ON |
37 | { |
38 | .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS | | |
39 | USB_DEVICE_ID_MATCH_INT_SUBCLASS, | |
40 | .bInterfaceClass = USB_CLASS_COMM, | |
41 | .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM | |
42 | }, | |
fec67b45 BM |
43 | { |
44 | /* | |
45 | * Huawei E392, E398 and possibly other Qualcomm based modems | |
46 | * embed the Qualcomm QMI protocol inside CDC on CDC ECM like | |
47 | * control interfaces. Userspace access to this is required | |
48 | * to configure the accompanying data interface | |
49 | */ | |
50 | .match_flags = USB_DEVICE_ID_MATCH_VENDOR | | |
51 | USB_DEVICE_ID_MATCH_INT_INFO, | |
52 | .idVendor = HUAWEI_VENDOR_ID, | |
53 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, | |
54 | .bInterfaceSubClass = 1, | |
55 | .bInterfaceProtocol = 9, /* NOTE: CDC ECM control interface! */ | |
56 | }, | |
afba937e ON |
57 | { } |
58 | }; | |
59 | ||
aa5380b9 ON |
60 | MODULE_DEVICE_TABLE (usb, wdm_ids); |
61 | ||
afba937e ON |
62 | #define WDM_MINOR_BASE 176 |
63 | ||
64 | ||
65 | #define WDM_IN_USE 1 | |
66 | #define WDM_DISCONNECTING 2 | |
67 | #define WDM_RESULT 3 | |
68 | #define WDM_READ 4 | |
69 | #define WDM_INT_STALL 5 | |
70 | #define WDM_POLL_RUNNING 6 | |
922a5ead | 71 | #define WDM_RESPONDING 7 |
beb1d35f | 72 | #define WDM_SUSPENDING 8 |
afba937e ON |
73 | |
74 | #define WDM_MAX 16 | |
75 | ||
7e3054a0 BM |
76 | /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */ |
77 | #define WDM_DEFAULT_BUFSIZE 256 | |
afba937e ON |
78 | |
79 | static DEFINE_MUTEX(wdm_mutex); | |
80 | ||
81 | /* --- method tables --- */ | |
82 | ||
83 | struct wdm_device { | |
84 | u8 *inbuf; /* buffer for response */ | |
85 | u8 *outbuf; /* buffer for command */ | |
86 | u8 *sbuf; /* buffer for status */ | |
87 | u8 *ubuf; /* buffer for copy to user space */ | |
88 | ||
89 | struct urb *command; | |
90 | struct urb *response; | |
91 | struct urb *validity; | |
92 | struct usb_interface *intf; | |
93 | struct usb_ctrlrequest *orq; | |
94 | struct usb_ctrlrequest *irq; | |
95 | spinlock_t iuspin; | |
96 | ||
97 | unsigned long flags; | |
98 | u16 bufsize; | |
99 | u16 wMaxCommand; | |
100 | u16 wMaxPacketSize; | |
afba937e ON |
101 | __le16 inum; |
102 | int reslength; | |
103 | int length; | |
104 | int read; | |
105 | int count; | |
106 | dma_addr_t shandle; | |
107 | dma_addr_t ihandle; | |
e8537bd2 BM |
108 | struct mutex wlock; |
109 | struct mutex rlock; | |
afba937e ON |
110 | wait_queue_head_t wait; |
111 | struct work_struct rxwork; | |
112 | int werr; | |
113 | int rerr; | |
114 | }; | |
115 | ||
116 | static struct usb_driver wdm_driver; | |
117 | ||
118 | /* --- callbacks --- */ | |
119 | static void wdm_out_callback(struct urb *urb) | |
120 | { | |
121 | struct wdm_device *desc; | |
122 | desc = urb->context; | |
123 | spin_lock(&desc->iuspin); | |
124 | desc->werr = urb->status; | |
125 | spin_unlock(&desc->iuspin); | |
126 | clear_bit(WDM_IN_USE, &desc->flags); | |
127 | kfree(desc->outbuf); | |
128 | wake_up(&desc->wait); | |
129 | } | |
130 | ||
131 | static void wdm_in_callback(struct urb *urb) | |
132 | { | |
133 | struct wdm_device *desc = urb->context; | |
134 | int status = urb->status; | |
135 | ||
136 | spin_lock(&desc->iuspin); | |
922a5ead | 137 | clear_bit(WDM_RESPONDING, &desc->flags); |
afba937e ON |
138 | |
139 | if (status) { | |
140 | switch (status) { | |
141 | case -ENOENT: | |
142 | dev_dbg(&desc->intf->dev, | |
143 | "nonzero urb status received: -ENOENT"); | |
922a5ead | 144 | goto skip_error; |
afba937e ON |
145 | case -ECONNRESET: |
146 | dev_dbg(&desc->intf->dev, | |
147 | "nonzero urb status received: -ECONNRESET"); | |
922a5ead | 148 | goto skip_error; |
afba937e ON |
149 | case -ESHUTDOWN: |
150 | dev_dbg(&desc->intf->dev, | |
151 | "nonzero urb status received: -ESHUTDOWN"); | |
922a5ead | 152 | goto skip_error; |
afba937e | 153 | case -EPIPE: |
9908a32e GKH |
154 | dev_err(&desc->intf->dev, |
155 | "nonzero urb status received: -EPIPE\n"); | |
afba937e ON |
156 | break; |
157 | default: | |
9908a32e GKH |
158 | dev_err(&desc->intf->dev, |
159 | "Unexpected error %d\n", status); | |
afba937e ON |
160 | break; |
161 | } | |
162 | } | |
163 | ||
164 | desc->rerr = status; | |
165 | desc->reslength = urb->actual_length; | |
166 | memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength); | |
167 | desc->length += desc->reslength; | |
922a5ead | 168 | skip_error: |
afba937e ON |
169 | wake_up(&desc->wait); |
170 | ||
171 | set_bit(WDM_READ, &desc->flags); | |
172 | spin_unlock(&desc->iuspin); | |
173 | } | |
174 | ||
175 | static void wdm_int_callback(struct urb *urb) | |
176 | { | |
177 | int rv = 0; | |
178 | int status = urb->status; | |
179 | struct wdm_device *desc; | |
afba937e ON |
180 | struct usb_cdc_notification *dr; |
181 | ||
182 | desc = urb->context; | |
afba937e ON |
183 | dr = (struct usb_cdc_notification *)desc->sbuf; |
184 | ||
185 | if (status) { | |
186 | switch (status) { | |
187 | case -ESHUTDOWN: | |
188 | case -ENOENT: | |
189 | case -ECONNRESET: | |
190 | return; /* unplug */ | |
191 | case -EPIPE: | |
192 | set_bit(WDM_INT_STALL, &desc->flags); | |
9908a32e | 193 | dev_err(&desc->intf->dev, "Stall on int endpoint\n"); |
afba937e ON |
194 | goto sw; /* halt is cleared in work */ |
195 | default: | |
9908a32e GKH |
196 | dev_err(&desc->intf->dev, |
197 | "nonzero urb status received: %d\n", status); | |
afba937e ON |
198 | break; |
199 | } | |
200 | } | |
201 | ||
202 | if (urb->actual_length < sizeof(struct usb_cdc_notification)) { | |
9908a32e GKH |
203 | dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n", |
204 | urb->actual_length); | |
afba937e ON |
205 | goto exit; |
206 | } | |
207 | ||
208 | switch (dr->bNotificationType) { | |
209 | case USB_CDC_NOTIFY_RESPONSE_AVAILABLE: | |
210 | dev_dbg(&desc->intf->dev, | |
211 | "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d", | |
212 | dr->wIndex, dr->wLength); | |
213 | break; | |
214 | ||
215 | case USB_CDC_NOTIFY_NETWORK_CONNECTION: | |
216 | ||
217 | dev_dbg(&desc->intf->dev, | |
218 | "NOTIFY_NETWORK_CONNECTION %s network", | |
219 | dr->wValue ? "connected to" : "disconnected from"); | |
220 | goto exit; | |
221 | default: | |
222 | clear_bit(WDM_POLL_RUNNING, &desc->flags); | |
9908a32e GKH |
223 | dev_err(&desc->intf->dev, |
224 | "unknown notification %d received: index %d len %d\n", | |
afba937e ON |
225 | dr->bNotificationType, dr->wIndex, dr->wLength); |
226 | goto exit; | |
227 | } | |
228 | ||
afba937e ON |
229 | spin_lock(&desc->iuspin); |
230 | clear_bit(WDM_READ, &desc->flags); | |
922a5ead | 231 | set_bit(WDM_RESPONDING, &desc->flags); |
beb1d35f ON |
232 | if (!test_bit(WDM_DISCONNECTING, &desc->flags) |
233 | && !test_bit(WDM_SUSPENDING, &desc->flags)) { | |
afba937e ON |
234 | rv = usb_submit_urb(desc->response, GFP_ATOMIC); |
235 | dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d", | |
236 | __func__, rv); | |
237 | } | |
238 | spin_unlock(&desc->iuspin); | |
239 | if (rv < 0) { | |
922a5ead | 240 | clear_bit(WDM_RESPONDING, &desc->flags); |
afba937e ON |
241 | if (rv == -EPERM) |
242 | return; | |
243 | if (rv == -ENOMEM) { | |
244 | sw: | |
245 | rv = schedule_work(&desc->rxwork); | |
246 | if (rv) | |
9908a32e GKH |
247 | dev_err(&desc->intf->dev, |
248 | "Cannot schedule work\n"); | |
afba937e ON |
249 | } |
250 | } | |
251 | exit: | |
252 | rv = usb_submit_urb(urb, GFP_ATOMIC); | |
253 | if (rv) | |
9908a32e GKH |
254 | dev_err(&desc->intf->dev, |
255 | "%s - usb_submit_urb failed with result %d\n", | |
256 | __func__, rv); | |
afba937e ON |
257 | |
258 | } | |
259 | ||
260 | static void kill_urbs(struct wdm_device *desc) | |
261 | { | |
17d80d56 | 262 | /* the order here is essential */ |
afba937e ON |
263 | usb_kill_urb(desc->command); |
264 | usb_kill_urb(desc->validity); | |
265 | usb_kill_urb(desc->response); | |
266 | } | |
267 | ||
268 | static void free_urbs(struct wdm_device *desc) | |
269 | { | |
270 | usb_free_urb(desc->validity); | |
271 | usb_free_urb(desc->response); | |
272 | usb_free_urb(desc->command); | |
273 | } | |
274 | ||
275 | static void cleanup(struct wdm_device *desc) | |
276 | { | |
8457d99c BM |
277 | kfree(desc->sbuf); |
278 | kfree(desc->inbuf); | |
afba937e ON |
279 | kfree(desc->orq); |
280 | kfree(desc->irq); | |
281 | kfree(desc->ubuf); | |
282 | free_urbs(desc); | |
283 | kfree(desc); | |
284 | } | |
285 | ||
286 | static ssize_t wdm_write | |
287 | (struct file *file, const char __user *buffer, size_t count, loff_t *ppos) | |
288 | { | |
289 | u8 *buf; | |
290 | int rv = -EMSGSIZE, r, we; | |
291 | struct wdm_device *desc = file->private_data; | |
292 | struct usb_ctrlrequest *req; | |
293 | ||
294 | if (count > desc->wMaxCommand) | |
295 | count = desc->wMaxCommand; | |
296 | ||
297 | spin_lock_irq(&desc->iuspin); | |
298 | we = desc->werr; | |
299 | desc->werr = 0; | |
300 | spin_unlock_irq(&desc->iuspin); | |
301 | if (we < 0) | |
302 | return -EIO; | |
303 | ||
860e41a7 ON |
304 | desc->outbuf = buf = kmalloc(count, GFP_KERNEL); |
305 | if (!buf) { | |
306 | rv = -ENOMEM; | |
307 | goto outnl; | |
308 | } | |
309 | ||
310 | r = copy_from_user(buf, buffer, count); | |
311 | if (r > 0) { | |
312 | kfree(buf); | |
313 | rv = -EFAULT; | |
314 | goto outnl; | |
315 | } | |
316 | ||
317 | /* concurrent writes and disconnect */ | |
e8537bd2 | 318 | r = mutex_lock_interruptible(&desc->wlock); |
afba937e | 319 | rv = -ERESTARTSYS; |
860e41a7 ON |
320 | if (r) { |
321 | kfree(buf); | |
afba937e | 322 | goto outnl; |
860e41a7 ON |
323 | } |
324 | ||
325 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { | |
326 | kfree(buf); | |
327 | rv = -ENODEV; | |
328 | goto outnp; | |
329 | } | |
afba937e | 330 | |
17d80d56 | 331 | r = usb_autopm_get_interface(desc->intf); |
860e41a7 ON |
332 | if (r < 0) { |
333 | kfree(buf); | |
17d80d56 | 334 | goto outnp; |
860e41a7 | 335 | } |
7f1dc313 | 336 | |
0cdfb819 | 337 | if (!(file->f_flags & O_NONBLOCK)) |
7f1dc313 ON |
338 | r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE, |
339 | &desc->flags)); | |
340 | else | |
341 | if (test_bit(WDM_IN_USE, &desc->flags)) | |
342 | r = -EAGAIN; | |
860e41a7 | 343 | if (r < 0) { |
afba937e | 344 | kfree(buf); |
afba937e ON |
345 | goto out; |
346 | } | |
347 | ||
348 | req = desc->orq; | |
349 | usb_fill_control_urb( | |
350 | desc->command, | |
351 | interface_to_usbdev(desc->intf), | |
352 | /* using common endpoint 0 */ | |
353 | usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0), | |
354 | (unsigned char *)req, | |
355 | buf, | |
356 | count, | |
357 | wdm_out_callback, | |
358 | desc | |
359 | ); | |
360 | ||
361 | req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS | | |
362 | USB_RECIP_INTERFACE); | |
363 | req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND; | |
364 | req->wValue = 0; | |
365 | req->wIndex = desc->inum; | |
366 | req->wLength = cpu_to_le16(count); | |
367 | set_bit(WDM_IN_USE, &desc->flags); | |
368 | ||
369 | rv = usb_submit_urb(desc->command, GFP_KERNEL); | |
370 | if (rv < 0) { | |
371 | kfree(buf); | |
372 | clear_bit(WDM_IN_USE, &desc->flags); | |
9908a32e | 373 | dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv); |
afba937e ON |
374 | } else { |
375 | dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d", | |
376 | req->wIndex); | |
377 | } | |
378 | out: | |
17d80d56 ON |
379 | usb_autopm_put_interface(desc->intf); |
380 | outnp: | |
e8537bd2 | 381 | mutex_unlock(&desc->wlock); |
afba937e ON |
382 | outnl: |
383 | return rv < 0 ? rv : count; | |
384 | } | |
385 | ||
386 | static ssize_t wdm_read | |
387 | (struct file *file, char __user *buffer, size_t count, loff_t *ppos) | |
388 | { | |
7f1dc313 | 389 | int rv, cntr = 0; |
afba937e ON |
390 | int i = 0; |
391 | struct wdm_device *desc = file->private_data; | |
392 | ||
393 | ||
e8537bd2 | 394 | rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */ |
afba937e ON |
395 | if (rv < 0) |
396 | return -ERESTARTSYS; | |
397 | ||
398 | if (desc->length == 0) { | |
399 | desc->read = 0; | |
400 | retry: | |
7f1dc313 ON |
401 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { |
402 | rv = -ENODEV; | |
403 | goto err; | |
404 | } | |
afba937e | 405 | i++; |
7f1dc313 ON |
406 | if (file->f_flags & O_NONBLOCK) { |
407 | if (!test_bit(WDM_READ, &desc->flags)) { | |
408 | rv = cntr ? cntr : -EAGAIN; | |
409 | goto err; | |
410 | } | |
411 | rv = 0; | |
412 | } else { | |
413 | rv = wait_event_interruptible(desc->wait, | |
414 | test_bit(WDM_READ, &desc->flags)); | |
415 | } | |
afba937e | 416 | |
7f1dc313 | 417 | /* may have happened while we slept */ |
17d80d56 ON |
418 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { |
419 | rv = -ENODEV; | |
420 | goto err; | |
421 | } | |
422 | usb_mark_last_busy(interface_to_usbdev(desc->intf)); | |
afba937e ON |
423 | if (rv < 0) { |
424 | rv = -ERESTARTSYS; | |
425 | goto err; | |
426 | } | |
427 | ||
428 | spin_lock_irq(&desc->iuspin); | |
429 | ||
430 | if (desc->rerr) { /* read completed, error happened */ | |
afba937e ON |
431 | desc->rerr = 0; |
432 | spin_unlock_irq(&desc->iuspin); | |
afba937e ON |
433 | rv = -EIO; |
434 | goto err; | |
435 | } | |
436 | /* | |
437 | * recheck whether we've lost the race | |
438 | * against the completion handler | |
439 | */ | |
440 | if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */ | |
441 | spin_unlock_irq(&desc->iuspin); | |
442 | goto retry; | |
443 | } | |
444 | if (!desc->reslength) { /* zero length read */ | |
445 | spin_unlock_irq(&desc->iuspin); | |
446 | goto retry; | |
447 | } | |
448 | clear_bit(WDM_READ, &desc->flags); | |
449 | spin_unlock_irq(&desc->iuspin); | |
450 | } | |
451 | ||
452 | cntr = count > desc->length ? desc->length : count; | |
453 | rv = copy_to_user(buffer, desc->ubuf, cntr); | |
454 | if (rv > 0) { | |
455 | rv = -EFAULT; | |
456 | goto err; | |
457 | } | |
458 | ||
459 | for (i = 0; i < desc->length - cntr; i++) | |
460 | desc->ubuf[i] = desc->ubuf[i + cntr]; | |
461 | ||
c428b70c | 462 | spin_lock_irq(&desc->iuspin); |
afba937e | 463 | desc->length -= cntr; |
c428b70c | 464 | spin_unlock_irq(&desc->iuspin); |
87d65e54 ON |
465 | /* in case we had outstanding data */ |
466 | if (!desc->length) | |
467 | clear_bit(WDM_READ, &desc->flags); | |
afba937e ON |
468 | rv = cntr; |
469 | ||
470 | err: | |
e8537bd2 | 471 | mutex_unlock(&desc->rlock); |
afba937e ON |
472 | return rv; |
473 | } | |
474 | ||
475 | static int wdm_flush(struct file *file, fl_owner_t id) | |
476 | { | |
477 | struct wdm_device *desc = file->private_data; | |
478 | ||
479 | wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags)); | |
480 | if (desc->werr < 0) | |
9908a32e GKH |
481 | dev_err(&desc->intf->dev, "Error in flush path: %d\n", |
482 | desc->werr); | |
afba937e ON |
483 | |
484 | return desc->werr; | |
485 | } | |
486 | ||
487 | static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait) | |
488 | { | |
489 | struct wdm_device *desc = file->private_data; | |
490 | unsigned long flags; | |
491 | unsigned int mask = 0; | |
492 | ||
493 | spin_lock_irqsave(&desc->iuspin, flags); | |
494 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { | |
495 | mask = POLLERR; | |
496 | spin_unlock_irqrestore(&desc->iuspin, flags); | |
497 | goto desc_out; | |
498 | } | |
499 | if (test_bit(WDM_READ, &desc->flags)) | |
500 | mask = POLLIN | POLLRDNORM; | |
501 | if (desc->rerr || desc->werr) | |
502 | mask |= POLLERR; | |
503 | if (!test_bit(WDM_IN_USE, &desc->flags)) | |
504 | mask |= POLLOUT | POLLWRNORM; | |
505 | spin_unlock_irqrestore(&desc->iuspin, flags); | |
506 | ||
507 | poll_wait(file, &desc->wait, wait); | |
508 | ||
509 | desc_out: | |
510 | return mask; | |
511 | } | |
512 | ||
513 | static int wdm_open(struct inode *inode, struct file *file) | |
514 | { | |
515 | int minor = iminor(inode); | |
516 | int rv = -ENODEV; | |
517 | struct usb_interface *intf; | |
518 | struct wdm_device *desc; | |
519 | ||
520 | mutex_lock(&wdm_mutex); | |
521 | intf = usb_find_interface(&wdm_driver, minor); | |
522 | if (!intf) | |
523 | goto out; | |
524 | ||
525 | desc = usb_get_intfdata(intf); | |
526 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) | |
527 | goto out; | |
afba937e ON |
528 | file->private_data = desc; |
529 | ||
17d80d56 | 530 | rv = usb_autopm_get_interface(desc->intf); |
afba937e | 531 | if (rv < 0) { |
9908a32e | 532 | dev_err(&desc->intf->dev, "Error autopm - %d\n", rv); |
afba937e ON |
533 | goto out; |
534 | } | |
17d80d56 | 535 | intf->needs_remote_wakeup = 1; |
afba937e | 536 | |
e8537bd2 BM |
537 | /* using write lock to protect desc->count */ |
538 | mutex_lock(&desc->wlock); | |
17d80d56 | 539 | if (!desc->count++) { |
d771d8aa ON |
540 | desc->werr = 0; |
541 | desc->rerr = 0; | |
17d80d56 ON |
542 | rv = usb_submit_urb(desc->validity, GFP_KERNEL); |
543 | if (rv < 0) { | |
544 | desc->count--; | |
9908a32e GKH |
545 | dev_err(&desc->intf->dev, |
546 | "Error submitting int urb - %d\n", rv); | |
17d80d56 ON |
547 | } |
548 | } else { | |
549 | rv = 0; | |
550 | } | |
e8537bd2 | 551 | mutex_unlock(&desc->wlock); |
17d80d56 | 552 | usb_autopm_put_interface(desc->intf); |
afba937e ON |
553 | out: |
554 | mutex_unlock(&wdm_mutex); | |
555 | return rv; | |
556 | } | |
557 | ||
558 | static int wdm_release(struct inode *inode, struct file *file) | |
559 | { | |
560 | struct wdm_device *desc = file->private_data; | |
561 | ||
562 | mutex_lock(&wdm_mutex); | |
e8537bd2 BM |
563 | |
564 | /* using write lock to protect desc->count */ | |
565 | mutex_lock(&desc->wlock); | |
afba937e | 566 | desc->count--; |
e8537bd2 | 567 | mutex_unlock(&desc->wlock); |
17d80d56 | 568 | |
afba937e ON |
569 | if (!desc->count) { |
570 | dev_dbg(&desc->intf->dev, "wdm_release: cleanup"); | |
571 | kill_urbs(desc); | |
17d80d56 ON |
572 | if (!test_bit(WDM_DISCONNECTING, &desc->flags)) |
573 | desc->intf->needs_remote_wakeup = 0; | |
afba937e ON |
574 | } |
575 | mutex_unlock(&wdm_mutex); | |
576 | return 0; | |
577 | } | |
578 | ||
579 | static const struct file_operations wdm_fops = { | |
580 | .owner = THIS_MODULE, | |
581 | .read = wdm_read, | |
582 | .write = wdm_write, | |
583 | .open = wdm_open, | |
584 | .flush = wdm_flush, | |
585 | .release = wdm_release, | |
6038f373 AB |
586 | .poll = wdm_poll, |
587 | .llseek = noop_llseek, | |
afba937e ON |
588 | }; |
589 | ||
590 | static struct usb_class_driver wdm_class = { | |
591 | .name = "cdc-wdm%d", | |
592 | .fops = &wdm_fops, | |
593 | .minor_base = WDM_MINOR_BASE, | |
594 | }; | |
595 | ||
596 | /* --- error handling --- */ | |
597 | static void wdm_rxwork(struct work_struct *work) | |
598 | { | |
599 | struct wdm_device *desc = container_of(work, struct wdm_device, rxwork); | |
600 | unsigned long flags; | |
601 | int rv; | |
602 | ||
603 | spin_lock_irqsave(&desc->iuspin, flags); | |
604 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { | |
605 | spin_unlock_irqrestore(&desc->iuspin, flags); | |
606 | } else { | |
607 | spin_unlock_irqrestore(&desc->iuspin, flags); | |
608 | rv = usb_submit_urb(desc->response, GFP_KERNEL); | |
609 | if (rv < 0 && rv != -EPERM) { | |
610 | spin_lock_irqsave(&desc->iuspin, flags); | |
611 | if (!test_bit(WDM_DISCONNECTING, &desc->flags)) | |
612 | schedule_work(&desc->rxwork); | |
613 | spin_unlock_irqrestore(&desc->iuspin, flags); | |
614 | } | |
615 | } | |
616 | } | |
617 | ||
618 | /* --- hotplug --- */ | |
619 | ||
620 | static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id) | |
621 | { | |
622 | int rv = -EINVAL; | |
afba937e ON |
623 | struct wdm_device *desc; |
624 | struct usb_host_interface *iface; | |
625 | struct usb_endpoint_descriptor *ep; | |
626 | struct usb_cdc_dmm_desc *dmhd; | |
627 | u8 *buffer = intf->altsetting->extra; | |
628 | int buflen = intf->altsetting->extralen; | |
7e3054a0 | 629 | u16 maxcom = WDM_DEFAULT_BUFSIZE; |
afba937e ON |
630 | |
631 | if (!buffer) | |
632 | goto out; | |
633 | ||
052fbc0d | 634 | while (buflen > 2) { |
afba937e | 635 | if (buffer [1] != USB_DT_CS_INTERFACE) { |
9908a32e | 636 | dev_err(&intf->dev, "skipping garbage\n"); |
afba937e ON |
637 | goto next_desc; |
638 | } | |
639 | ||
640 | switch (buffer [2]) { | |
641 | case USB_CDC_HEADER_TYPE: | |
642 | break; | |
643 | case USB_CDC_DMM_TYPE: | |
644 | dmhd = (struct usb_cdc_dmm_desc *)buffer; | |
645 | maxcom = le16_to_cpu(dmhd->wMaxCommand); | |
646 | dev_dbg(&intf->dev, | |
647 | "Finding maximum buffer length: %d", maxcom); | |
648 | break; | |
649 | default: | |
9908a32e GKH |
650 | dev_err(&intf->dev, |
651 | "Ignoring extra header, type %d, length %d\n", | |
afba937e ON |
652 | buffer[2], buffer[0]); |
653 | break; | |
654 | } | |
655 | next_desc: | |
656 | buflen -= buffer[0]; | |
657 | buffer += buffer[0]; | |
658 | } | |
659 | ||
660 | rv = -ENOMEM; | |
661 | desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL); | |
662 | if (!desc) | |
663 | goto out; | |
e8537bd2 BM |
664 | mutex_init(&desc->rlock); |
665 | mutex_init(&desc->wlock); | |
afba937e ON |
666 | spin_lock_init(&desc->iuspin); |
667 | init_waitqueue_head(&desc->wait); | |
668 | desc->wMaxCommand = maxcom; | |
052fbc0d | 669 | /* this will be expanded and needed in hardware endianness */ |
afba937e ON |
670 | desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber); |
671 | desc->intf = intf; | |
672 | INIT_WORK(&desc->rxwork, wdm_rxwork); | |
673 | ||
052fbc0d ON |
674 | rv = -EINVAL; |
675 | iface = intf->cur_altsetting; | |
676 | if (iface->desc.bNumEndpoints != 1) | |
677 | goto err; | |
afba937e | 678 | ep = &iface->endpoint[0].desc; |
052fbc0d | 679 | if (!ep || !usb_endpoint_is_int_in(ep)) |
afba937e | 680 | goto err; |
afba937e | 681 | |
29cc8897 | 682 | desc->wMaxPacketSize = usb_endpoint_maxp(ep); |
afba937e ON |
683 | |
684 | desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); | |
685 | if (!desc->orq) | |
686 | goto err; | |
687 | desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); | |
688 | if (!desc->irq) | |
689 | goto err; | |
690 | ||
691 | desc->validity = usb_alloc_urb(0, GFP_KERNEL); | |
692 | if (!desc->validity) | |
693 | goto err; | |
694 | ||
695 | desc->response = usb_alloc_urb(0, GFP_KERNEL); | |
696 | if (!desc->response) | |
697 | goto err; | |
698 | ||
699 | desc->command = usb_alloc_urb(0, GFP_KERNEL); | |
700 | if (!desc->command) | |
701 | goto err; | |
702 | ||
703 | desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL); | |
704 | if (!desc->ubuf) | |
705 | goto err; | |
706 | ||
8457d99c | 707 | desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL); |
afba937e ON |
708 | if (!desc->sbuf) |
709 | goto err; | |
710 | ||
8457d99c | 711 | desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL); |
afba937e | 712 | if (!desc->inbuf) |
8457d99c | 713 | goto err; |
afba937e ON |
714 | |
715 | usb_fill_int_urb( | |
716 | desc->validity, | |
717 | interface_to_usbdev(intf), | |
718 | usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress), | |
719 | desc->sbuf, | |
720 | desc->wMaxPacketSize, | |
721 | wdm_int_callback, | |
722 | desc, | |
723 | ep->bInterval | |
724 | ); | |
afba937e | 725 | |
19b85b3b BM |
726 | desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE); |
727 | desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE; | |
728 | desc->irq->wValue = 0; | |
729 | desc->irq->wIndex = desc->inum; | |
730 | desc->irq->wLength = cpu_to_le16(desc->wMaxCommand); | |
731 | ||
732 | usb_fill_control_urb( | |
733 | desc->response, | |
8143a896 | 734 | interface_to_usbdev(intf), |
19b85b3b BM |
735 | /* using common endpoint 0 */ |
736 | usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0), | |
737 | (unsigned char *)desc->irq, | |
738 | desc->inbuf, | |
739 | desc->wMaxCommand, | |
740 | wdm_in_callback, | |
741 | desc | |
742 | ); | |
19b85b3b | 743 | |
afba937e ON |
744 | usb_set_intfdata(intf, desc); |
745 | rv = usb_register_dev(intf, &wdm_class); | |
afba937e | 746 | if (rv < 0) |
8457d99c | 747 | goto err2; |
052fbc0d | 748 | else |
820c629a | 749 | dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev)); |
afba937e ON |
750 | out: |
751 | return rv; | |
752 | err2: | |
8457d99c | 753 | usb_set_intfdata(intf, NULL); |
afba937e ON |
754 | err: |
755 | free_urbs(desc); | |
8457d99c BM |
756 | kfree(desc->inbuf); |
757 | kfree(desc->sbuf); | |
afba937e ON |
758 | kfree(desc->ubuf); |
759 | kfree(desc->orq); | |
760 | kfree(desc->irq); | |
761 | kfree(desc); | |
762 | return rv; | |
763 | } | |
764 | ||
765 | static void wdm_disconnect(struct usb_interface *intf) | |
766 | { | |
767 | struct wdm_device *desc; | |
768 | unsigned long flags; | |
769 | ||
770 | usb_deregister_dev(intf, &wdm_class); | |
771 | mutex_lock(&wdm_mutex); | |
772 | desc = usb_get_intfdata(intf); | |
773 | ||
774 | /* the spinlock makes sure no new urbs are generated in the callbacks */ | |
775 | spin_lock_irqsave(&desc->iuspin, flags); | |
776 | set_bit(WDM_DISCONNECTING, &desc->flags); | |
777 | set_bit(WDM_READ, &desc->flags); | |
17d80d56 | 778 | /* to terminate pending flushes */ |
afba937e ON |
779 | clear_bit(WDM_IN_USE, &desc->flags); |
780 | spin_unlock_irqrestore(&desc->iuspin, flags); | |
62aaf24d | 781 | wake_up_all(&desc->wait); |
e8537bd2 BM |
782 | mutex_lock(&desc->rlock); |
783 | mutex_lock(&desc->wlock); | |
afba937e | 784 | kill_urbs(desc); |
d93d16e9 | 785 | cancel_work_sync(&desc->rxwork); |
e8537bd2 BM |
786 | mutex_unlock(&desc->wlock); |
787 | mutex_unlock(&desc->rlock); | |
afba937e ON |
788 | if (!desc->count) |
789 | cleanup(desc); | |
790 | mutex_unlock(&wdm_mutex); | |
791 | } | |
792 | ||
d93d16e9 | 793 | #ifdef CONFIG_PM |
17d80d56 ON |
794 | static int wdm_suspend(struct usb_interface *intf, pm_message_t message) |
795 | { | |
796 | struct wdm_device *desc = usb_get_intfdata(intf); | |
797 | int rv = 0; | |
798 | ||
799 | dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor); | |
800 | ||
d93d16e9 | 801 | /* if this is an autosuspend the caller does the locking */ |
e8537bd2 BM |
802 | if (!PMSG_IS_AUTO(message)) { |
803 | mutex_lock(&desc->rlock); | |
804 | mutex_lock(&desc->wlock); | |
805 | } | |
62e66854 | 806 | spin_lock_irq(&desc->iuspin); |
d93d16e9 | 807 | |
5b1b0b81 | 808 | if (PMSG_IS_AUTO(message) && |
922a5ead ON |
809 | (test_bit(WDM_IN_USE, &desc->flags) |
810 | || test_bit(WDM_RESPONDING, &desc->flags))) { | |
62e66854 | 811 | spin_unlock_irq(&desc->iuspin); |
17d80d56 ON |
812 | rv = -EBUSY; |
813 | } else { | |
d93d16e9 | 814 | |
beb1d35f | 815 | set_bit(WDM_SUSPENDING, &desc->flags); |
62e66854 | 816 | spin_unlock_irq(&desc->iuspin); |
d93d16e9 | 817 | /* callback submits work - order is essential */ |
17d80d56 | 818 | kill_urbs(desc); |
d93d16e9 | 819 | cancel_work_sync(&desc->rxwork); |
17d80d56 | 820 | } |
e8537bd2 BM |
821 | if (!PMSG_IS_AUTO(message)) { |
822 | mutex_unlock(&desc->wlock); | |
823 | mutex_unlock(&desc->rlock); | |
824 | } | |
17d80d56 ON |
825 | |
826 | return rv; | |
827 | } | |
d93d16e9 | 828 | #endif |
17d80d56 ON |
829 | |
830 | static int recover_from_urb_loss(struct wdm_device *desc) | |
831 | { | |
832 | int rv = 0; | |
833 | ||
834 | if (desc->count) { | |
835 | rv = usb_submit_urb(desc->validity, GFP_NOIO); | |
836 | if (rv < 0) | |
9908a32e GKH |
837 | dev_err(&desc->intf->dev, |
838 | "Error resume submitting int urb - %d\n", rv); | |
17d80d56 ON |
839 | } |
840 | return rv; | |
841 | } | |
d93d16e9 ON |
842 | |
843 | #ifdef CONFIG_PM | |
17d80d56 ON |
844 | static int wdm_resume(struct usb_interface *intf) |
845 | { | |
846 | struct wdm_device *desc = usb_get_intfdata(intf); | |
847 | int rv; | |
848 | ||
849 | dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor); | |
338124c1 | 850 | |
beb1d35f | 851 | clear_bit(WDM_SUSPENDING, &desc->flags); |
62e66854 | 852 | rv = recover_from_urb_loss(desc); |
338124c1 | 853 | |
17d80d56 ON |
854 | return rv; |
855 | } | |
d93d16e9 | 856 | #endif |
17d80d56 ON |
857 | |
858 | static int wdm_pre_reset(struct usb_interface *intf) | |
859 | { | |
860 | struct wdm_device *desc = usb_get_intfdata(intf); | |
861 | ||
e8537bd2 BM |
862 | mutex_lock(&desc->rlock); |
863 | mutex_lock(&desc->wlock); | |
d771d8aa ON |
864 | kill_urbs(desc); |
865 | ||
866 | /* | |
867 | * we notify everybody using poll of | |
868 | * an exceptional situation | |
869 | * must be done before recovery lest a spontaneous | |
870 | * message from the device is lost | |
871 | */ | |
872 | spin_lock_irq(&desc->iuspin); | |
873 | desc->rerr = -EINTR; | |
874 | spin_unlock_irq(&desc->iuspin); | |
875 | wake_up_all(&desc->wait); | |
17d80d56 ON |
876 | return 0; |
877 | } | |
878 | ||
879 | static int wdm_post_reset(struct usb_interface *intf) | |
880 | { | |
881 | struct wdm_device *desc = usb_get_intfdata(intf); | |
882 | int rv; | |
883 | ||
884 | rv = recover_from_urb_loss(desc); | |
e8537bd2 BM |
885 | mutex_unlock(&desc->wlock); |
886 | mutex_unlock(&desc->rlock); | |
17d80d56 ON |
887 | return 0; |
888 | } | |
889 | ||
afba937e ON |
890 | static struct usb_driver wdm_driver = { |
891 | .name = "cdc_wdm", | |
892 | .probe = wdm_probe, | |
893 | .disconnect = wdm_disconnect, | |
d93d16e9 | 894 | #ifdef CONFIG_PM |
17d80d56 ON |
895 | .suspend = wdm_suspend, |
896 | .resume = wdm_resume, | |
897 | .reset_resume = wdm_resume, | |
d93d16e9 | 898 | #endif |
17d80d56 ON |
899 | .pre_reset = wdm_pre_reset, |
900 | .post_reset = wdm_post_reset, | |
afba937e | 901 | .id_table = wdm_ids, |
17d80d56 | 902 | .supports_autosuspend = 1, |
afba937e ON |
903 | }; |
904 | ||
65db4305 | 905 | module_usb_driver(wdm_driver); |
afba937e ON |
906 | |
907 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
87d65e54 | 908 | MODULE_DESCRIPTION(DRIVER_DESC); |
afba937e | 909 | MODULE_LICENSE("GPL"); |