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