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