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