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