1 /*
2 * cdc-wdm.c
3 *
4 * This driver supports USB CDC WCM Device Management.
5 *
6 * Copyright (c) 2007-2009 Oliver Neukum
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/ioctl.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/uaccess.h>
21 #include <linux/bitops.h>
22 #include <linux/poll.h>
23 #include <linux/usb.h>
24 #include <linux/usb/cdc.h>
25 #include <asm/byteorder.h>
26 #include <asm/unaligned.h>
27 #include <linux/usb/cdc-wdm.h>
28
29 /*
30 * Version Information
31 */
32 #define DRIVER_VERSION "v0.03"
33 #define DRIVER_AUTHOR "Oliver Neukum"
34 #define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management"
35
36 static const struct usb_device_id wdm_ids[] = {
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 },
43 { }
44 };
45
46 MODULE_DEVICE_TABLE (usb, wdm_ids);
47
48 #define WDM_MINOR_BASE 176
49
50
51 #define WDM_IN_USE 1
52 #define WDM_DISCONNECTING 2
53 #define WDM_RESULT 3
54 #define WDM_READ 4
55 #define WDM_INT_STALL 5
56 #define WDM_POLL_RUNNING 6
57 #define WDM_RESPONDING 7
58 #define WDM_SUSPENDING 8
59 #define WDM_RESETTING 9
60 #define WDM_OVERFLOW 10
61
62 #define WDM_MAX 16
63
64 /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
65 #define WDM_DEFAULT_BUFSIZE 256
66
67 static DEFINE_MUTEX(wdm_mutex);
68 static DEFINE_SPINLOCK(wdm_device_list_lock);
69 static LIST_HEAD(wdm_device_list);
70
71 /* --- method tables --- */
72
73 struct wdm_device {
74 u8 *inbuf; /* buffer for response */
75 u8 *outbuf; /* buffer for command */
76 u8 *sbuf; /* buffer for status */
77 u8 *ubuf; /* buffer for copy to user space */
78
79 struct urb *command;
80 struct urb *response;
81 struct urb *validity;
82 struct usb_interface *intf;
83 struct usb_ctrlrequest *orq;
84 struct usb_ctrlrequest *irq;
85 spinlock_t iuspin;
86
87 unsigned long flags;
88 u16 bufsize;
89 u16 wMaxCommand;
90 u16 wMaxPacketSize;
91 __le16 inum;
92 int reslength;
93 int length;
94 int read;
95 int count;
96 dma_addr_t shandle;
97 dma_addr_t ihandle;
98 struct mutex wlock;
99 struct mutex rlock;
100 wait_queue_head_t wait;
101 struct work_struct rxwork;
102 int werr;
103 int rerr;
104 int resp_count;
105
106 struct list_head device_list;
107 int (*manage_power)(struct usb_interface *, int);
108 };
109
110 static struct usb_driver wdm_driver;
111
112 /* return intfdata if we own the interface, else look up intf in the list */
wdm_find_device(struct usb_interface * intf)113 static struct wdm_device *wdm_find_device(struct usb_interface *intf)
114 {
115 struct wdm_device *desc;
116
117 spin_lock(&wdm_device_list_lock);
118 list_for_each_entry(desc, &wdm_device_list, device_list)
119 if (desc->intf == intf)
120 goto found;
121 desc = NULL;
122 found:
123 spin_unlock(&wdm_device_list_lock);
124
125 return desc;
126 }
127
wdm_find_device_by_minor(int minor)128 static struct wdm_device *wdm_find_device_by_minor(int minor)
129 {
130 struct wdm_device *desc;
131
132 spin_lock(&wdm_device_list_lock);
133 list_for_each_entry(desc, &wdm_device_list, device_list)
134 if (desc->intf->minor == minor)
135 goto found;
136 desc = NULL;
137 found:
138 spin_unlock(&wdm_device_list_lock);
139
140 return desc;
141 }
142
143 /* --- callbacks --- */
wdm_out_callback(struct urb * urb)144 static void wdm_out_callback(struct urb *urb)
145 {
146 struct wdm_device *desc;
147 desc = urb->context;
148 spin_lock(&desc->iuspin);
149 desc->werr = urb->status;
150 spin_unlock(&desc->iuspin);
151 kfree(desc->outbuf);
152 desc->outbuf = NULL;
153 clear_bit(WDM_IN_USE, &desc->flags);
154 wake_up(&desc->wait);
155 }
156
wdm_in_callback(struct urb * urb)157 static void wdm_in_callback(struct urb *urb)
158 {
159 struct wdm_device *desc = urb->context;
160 int status = urb->status;
161 int length = urb->actual_length;
162
163 spin_lock(&desc->iuspin);
164 clear_bit(WDM_RESPONDING, &desc->flags);
165
166 if (status) {
167 switch (status) {
168 case -ENOENT:
169 dev_dbg(&desc->intf->dev,
170 "nonzero urb status received: -ENOENT");
171 goto skip_error;
172 case -ECONNRESET:
173 dev_dbg(&desc->intf->dev,
174 "nonzero urb status received: -ECONNRESET");
175 goto skip_error;
176 case -ESHUTDOWN:
177 dev_dbg(&desc->intf->dev,
178 "nonzero urb status received: -ESHUTDOWN");
179 goto skip_error;
180 case -EPIPE:
181 dev_err(&desc->intf->dev,
182 "nonzero urb status received: -EPIPE\n");
183 break;
184 default:
185 dev_err(&desc->intf->dev,
186 "Unexpected error %d\n", status);
187 break;
188 }
189 }
190
191 desc->rerr = status;
192 if (length + desc->length > desc->wMaxCommand) {
193 /* The buffer would overflow */
194 set_bit(WDM_OVERFLOW, &desc->flags);
195 } else {
196 /* we may already be in overflow */
197 if (!test_bit(WDM_OVERFLOW, &desc->flags)) {
198 memmove(desc->ubuf + desc->length, desc->inbuf, length);
199 desc->length += length;
200 desc->reslength = length;
201 }
202 }
203 skip_error:
204 wake_up(&desc->wait);
205
206 set_bit(WDM_READ, &desc->flags);
207 spin_unlock(&desc->iuspin);
208 }
209
wdm_int_callback(struct urb * urb)210 static void wdm_int_callback(struct urb *urb)
211 {
212 int rv = 0;
213 int responding;
214 int status = urb->status;
215 struct wdm_device *desc;
216 struct usb_cdc_notification *dr;
217
218 desc = urb->context;
219 dr = (struct usb_cdc_notification *)desc->sbuf;
220
221 if (status) {
222 switch (status) {
223 case -ESHUTDOWN:
224 case -ENOENT:
225 case -ECONNRESET:
226 return; /* unplug */
227 case -EPIPE:
228 set_bit(WDM_INT_STALL, &desc->flags);
229 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
230 goto sw; /* halt is cleared in work */
231 default:
232 dev_err(&desc->intf->dev,
233 "nonzero urb status received: %d\n", status);
234 break;
235 }
236 }
237
238 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
239 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
240 urb->actual_length);
241 goto exit;
242 }
243
244 switch (dr->bNotificationType) {
245 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
246 dev_dbg(&desc->intf->dev,
247 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
248 le16_to_cpu(dr->wIndex), le16_to_cpu(dr->wLength));
249 break;
250
251 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
252
253 dev_dbg(&desc->intf->dev,
254 "NOTIFY_NETWORK_CONNECTION %s network",
255 dr->wValue ? "connected to" : "disconnected from");
256 goto exit;
257 case USB_CDC_NOTIFY_SPEED_CHANGE:
258 dev_dbg(&desc->intf->dev, "SPEED_CHANGE received (len %u)",
259 urb->actual_length);
260 goto exit;
261 default:
262 clear_bit(WDM_POLL_RUNNING, &desc->flags);
263 dev_err(&desc->intf->dev,
264 "unknown notification %d received: index %d len %d\n",
265 dr->bNotificationType,
266 le16_to_cpu(dr->wIndex),
267 le16_to_cpu(dr->wLength));
268 goto exit;
269 }
270
271 spin_lock(&desc->iuspin);
272 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
273 if (!desc->resp_count++ && !responding
274 && !test_bit(WDM_DISCONNECTING, &desc->flags)
275 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
276 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
277 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
278 __func__, rv);
279 }
280 spin_unlock(&desc->iuspin);
281 if (rv < 0) {
282 clear_bit(WDM_RESPONDING, &desc->flags);
283 if (rv == -EPERM)
284 return;
285 if (rv == -ENOMEM) {
286 sw:
287 rv = schedule_work(&desc->rxwork);
288 if (rv)
289 dev_err(&desc->intf->dev,
290 "Cannot schedule work\n");
291 }
292 }
293 exit:
294 rv = usb_submit_urb(urb, GFP_ATOMIC);
295 if (rv)
296 dev_err(&desc->intf->dev,
297 "%s - usb_submit_urb failed with result %d\n",
298 __func__, rv);
299
300 }
301
kill_urbs(struct wdm_device * desc)302 static void kill_urbs(struct wdm_device *desc)
303 {
304 /* the order here is essential */
305 usb_kill_urb(desc->command);
306 usb_kill_urb(desc->validity);
307 usb_kill_urb(desc->response);
308 }
309
free_urbs(struct wdm_device * desc)310 static void free_urbs(struct wdm_device *desc)
311 {
312 usb_free_urb(desc->validity);
313 usb_free_urb(desc->response);
314 usb_free_urb(desc->command);
315 }
316
cleanup(struct wdm_device * desc)317 static void cleanup(struct wdm_device *desc)
318 {
319 kfree(desc->sbuf);
320 kfree(desc->inbuf);
321 kfree(desc->orq);
322 kfree(desc->irq);
323 kfree(desc->ubuf);
324 free_urbs(desc);
325 kfree(desc);
326 }
327
wdm_write(struct file * file,const char __user * buffer,size_t count,loff_t * ppos)328 static ssize_t wdm_write
329 (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
330 {
331 u8 *buf;
332 int rv = -EMSGSIZE, r, we;
333 struct wdm_device *desc = file->private_data;
334 struct usb_ctrlrequest *req;
335
336 if (count > desc->wMaxCommand)
337 count = desc->wMaxCommand;
338
339 spin_lock_irq(&desc->iuspin);
340 we = desc->werr;
341 desc->werr = 0;
342 spin_unlock_irq(&desc->iuspin);
343 if (we < 0)
344 return -EIO;
345
346 buf = kmalloc(count, GFP_KERNEL);
347 if (!buf) {
348 rv = -ENOMEM;
349 goto outnl;
350 }
351
352 r = copy_from_user(buf, buffer, count);
353 if (r > 0) {
354 kfree(buf);
355 rv = -EFAULT;
356 goto outnl;
357 }
358
359 /* concurrent writes and disconnect */
360 r = mutex_lock_interruptible(&desc->wlock);
361 rv = -ERESTARTSYS;
362 if (r) {
363 kfree(buf);
364 goto outnl;
365 }
366
367 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
368 kfree(buf);
369 rv = -ENODEV;
370 goto outnp;
371 }
372
373 r = usb_autopm_get_interface(desc->intf);
374 if (r < 0) {
375 kfree(buf);
376 rv = usb_translate_errors(r);
377 goto outnp;
378 }
379
380 if (!(file->f_flags & O_NONBLOCK))
381 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
382 &desc->flags));
383 else
384 if (test_bit(WDM_IN_USE, &desc->flags))
385 r = -EAGAIN;
386
387 if (test_bit(WDM_RESETTING, &desc->flags))
388 r = -EIO;
389
390 if (r < 0) {
391 kfree(buf);
392 rv = r;
393 goto out;
394 }
395
396 req = desc->orq;
397 usb_fill_control_urb(
398 desc->command,
399 interface_to_usbdev(desc->intf),
400 /* using common endpoint 0 */
401 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
402 (unsigned char *)req,
403 buf,
404 count,
405 wdm_out_callback,
406 desc
407 );
408
409 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
410 USB_RECIP_INTERFACE);
411 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
412 req->wValue = 0;
413 req->wIndex = desc->inum; /* already converted */
414 req->wLength = cpu_to_le16(count);
415 set_bit(WDM_IN_USE, &desc->flags);
416 desc->outbuf = buf;
417
418 rv = usb_submit_urb(desc->command, GFP_KERNEL);
419 if (rv < 0) {
420 kfree(buf);
421 desc->outbuf = NULL;
422 clear_bit(WDM_IN_USE, &desc->flags);
423 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
424 rv = usb_translate_errors(rv);
425 } else {
426 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
427 le16_to_cpu(req->wIndex));
428 }
429 out:
430 usb_autopm_put_interface(desc->intf);
431 outnp:
432 mutex_unlock(&desc->wlock);
433 outnl:
434 return rv < 0 ? rv : count;
435 }
436
437 /*
438 * clear WDM_READ flag and possibly submit the read urb if resp_count
439 * is non-zero.
440 *
441 * Called with desc->iuspin locked
442 */
clear_wdm_read_flag(struct wdm_device * desc)443 static int clear_wdm_read_flag(struct wdm_device *desc)
444 {
445 int rv = 0;
446
447 clear_bit(WDM_READ, &desc->flags);
448
449 /* submit read urb only if the device is waiting for it */
450 if (!desc->resp_count || !--desc->resp_count)
451 goto out;
452
453 set_bit(WDM_RESPONDING, &desc->flags);
454 spin_unlock_irq(&desc->iuspin);
455 rv = usb_submit_urb(desc->response, GFP_KERNEL);
456 spin_lock_irq(&desc->iuspin);
457 if (rv) {
458 dev_err(&desc->intf->dev,
459 "usb_submit_urb failed with result %d\n", rv);
460
461 /* make sure the next notification trigger a submit */
462 clear_bit(WDM_RESPONDING, &desc->flags);
463 desc->resp_count = 0;
464 }
465 out:
466 return rv;
467 }
468
wdm_read(struct file * file,char __user * buffer,size_t count,loff_t * ppos)469 static ssize_t wdm_read
470 (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
471 {
472 int rv, cntr;
473 int i = 0;
474 struct wdm_device *desc = file->private_data;
475
476
477 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
478 if (rv < 0)
479 return -ERESTARTSYS;
480
481 cntr = ACCESS_ONCE(desc->length);
482 if (cntr == 0) {
483 desc->read = 0;
484 retry:
485 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
486 rv = -ENODEV;
487 goto err;
488 }
489 if (test_bit(WDM_OVERFLOW, &desc->flags)) {
490 clear_bit(WDM_OVERFLOW, &desc->flags);
491 rv = -ENOBUFS;
492 goto err;
493 }
494 i++;
495 if (file->f_flags & O_NONBLOCK) {
496 if (!test_bit(WDM_READ, &desc->flags)) {
497 rv = cntr ? cntr : -EAGAIN;
498 goto err;
499 }
500 rv = 0;
501 } else {
502 rv = wait_event_interruptible(desc->wait,
503 test_bit(WDM_READ, &desc->flags));
504 }
505
506 /* may have happened while we slept */
507 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
508 rv = -ENODEV;
509 goto err;
510 }
511 if (test_bit(WDM_RESETTING, &desc->flags)) {
512 rv = -EIO;
513 goto err;
514 }
515 usb_mark_last_busy(interface_to_usbdev(desc->intf));
516 if (rv < 0) {
517 rv = -ERESTARTSYS;
518 goto err;
519 }
520
521 spin_lock_irq(&desc->iuspin);
522
523 if (desc->rerr) { /* read completed, error happened */
524 desc->rerr = 0;
525 spin_unlock_irq(&desc->iuspin);
526 rv = -EIO;
527 goto err;
528 }
529 /*
530 * recheck whether we've lost the race
531 * against the completion handler
532 */
533 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
534 spin_unlock_irq(&desc->iuspin);
535 goto retry;
536 }
537
538 if (!desc->reslength) { /* zero length read */
539 dev_dbg(&desc->intf->dev, "%s: zero length - clearing WDM_READ\n", __func__);
540 rv = clear_wdm_read_flag(desc);
541 spin_unlock_irq(&desc->iuspin);
542 if (rv < 0)
543 goto err;
544 goto retry;
545 }
546 cntr = desc->length;
547 spin_unlock_irq(&desc->iuspin);
548 }
549
550 if (cntr > count)
551 cntr = count;
552 rv = copy_to_user(buffer, desc->ubuf, cntr);
553 if (rv > 0) {
554 rv = -EFAULT;
555 goto err;
556 }
557
558 spin_lock_irq(&desc->iuspin);
559
560 for (i = 0; i < desc->length - cntr; i++)
561 desc->ubuf[i] = desc->ubuf[i + cntr];
562
563 desc->length -= cntr;
564 /* in case we had outstanding data */
565 if (!desc->length)
566 clear_wdm_read_flag(desc);
567 spin_unlock_irq(&desc->iuspin);
568 rv = cntr;
569
570 err:
571 mutex_unlock(&desc->rlock);
572 return rv;
573 }
574
wdm_flush(struct file * file,fl_owner_t id)575 static int wdm_flush(struct file *file, fl_owner_t id)
576 {
577 struct wdm_device *desc = file->private_data;
578
579 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
580
581 /* cannot dereference desc->intf if WDM_DISCONNECTING */
582 if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags))
583 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
584 desc->werr);
585
586 return usb_translate_errors(desc->werr);
587 }
588
wdm_poll(struct file * file,struct poll_table_struct * wait)589 static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
590 {
591 struct wdm_device *desc = file->private_data;
592 unsigned long flags;
593 unsigned int mask = 0;
594
595 spin_lock_irqsave(&desc->iuspin, flags);
596 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
597 mask = POLLHUP | POLLERR;
598 spin_unlock_irqrestore(&desc->iuspin, flags);
599 goto desc_out;
600 }
601 if (test_bit(WDM_READ, &desc->flags))
602 mask = POLLIN | POLLRDNORM;
603 if (desc->rerr || desc->werr)
604 mask |= POLLERR;
605 if (!test_bit(WDM_IN_USE, &desc->flags))
606 mask |= POLLOUT | POLLWRNORM;
607 spin_unlock_irqrestore(&desc->iuspin, flags);
608
609 poll_wait(file, &desc->wait, wait);
610
611 desc_out:
612 return mask;
613 }
614
wdm_open(struct inode * inode,struct file * file)615 static int wdm_open(struct inode *inode, struct file *file)
616 {
617 int minor = iminor(inode);
618 int rv = -ENODEV;
619 struct usb_interface *intf;
620 struct wdm_device *desc;
621
622 mutex_lock(&wdm_mutex);
623 desc = wdm_find_device_by_minor(minor);
624 if (!desc)
625 goto out;
626
627 intf = desc->intf;
628 if (test_bit(WDM_DISCONNECTING, &desc->flags))
629 goto out;
630 file->private_data = desc;
631
632 rv = usb_autopm_get_interface(desc->intf);
633 if (rv < 0) {
634 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
635 goto out;
636 }
637
638 /* using write lock to protect desc->count */
639 mutex_lock(&desc->wlock);
640 if (!desc->count++) {
641 desc->werr = 0;
642 desc->rerr = 0;
643 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
644 if (rv < 0) {
645 desc->count--;
646 dev_err(&desc->intf->dev,
647 "Error submitting int urb - %d\n", rv);
648 rv = usb_translate_errors(rv);
649 }
650 } else {
651 rv = 0;
652 }
653 mutex_unlock(&desc->wlock);
654 if (desc->count == 1)
655 desc->manage_power(intf, 1);
656 usb_autopm_put_interface(desc->intf);
657 out:
658 mutex_unlock(&wdm_mutex);
659 return rv;
660 }
661
wdm_release(struct inode * inode,struct file * file)662 static int wdm_release(struct inode *inode, struct file *file)
663 {
664 struct wdm_device *desc = file->private_data;
665
666 mutex_lock(&wdm_mutex);
667
668 /* using write lock to protect desc->count */
669 mutex_lock(&desc->wlock);
670 desc->count--;
671 mutex_unlock(&desc->wlock);
672
673 if (!desc->count) {
674 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
675 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
676 kill_urbs(desc);
677 spin_lock_irq(&desc->iuspin);
678 desc->resp_count = 0;
679 spin_unlock_irq(&desc->iuspin);
680 desc->manage_power(desc->intf, 0);
681 } else {
682 /* must avoid dev_printk here as desc->intf is invalid */
683 pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
684 cleanup(desc);
685 }
686 }
687 mutex_unlock(&wdm_mutex);
688 return 0;
689 }
690
wdm_ioctl(struct file * file,unsigned int cmd,unsigned long arg)691 static long wdm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
692 {
693 struct wdm_device *desc = file->private_data;
694 int rv = 0;
695
696 switch (cmd) {
697 case IOCTL_WDM_MAX_COMMAND:
698 if (copy_to_user((void __user *)arg, &desc->wMaxCommand, sizeof(desc->wMaxCommand)))
699 rv = -EFAULT;
700 break;
701 default:
702 rv = -ENOTTY;
703 }
704 return rv;
705 }
706
707 static const struct file_operations wdm_fops = {
708 .owner = THIS_MODULE,
709 .read = wdm_read,
710 .write = wdm_write,
711 .open = wdm_open,
712 .flush = wdm_flush,
713 .release = wdm_release,
714 .poll = wdm_poll,
715 .unlocked_ioctl = wdm_ioctl,
716 .compat_ioctl = wdm_ioctl,
717 .llseek = noop_llseek,
718 };
719
720 static struct usb_class_driver wdm_class = {
721 .name = "cdc-wdm%d",
722 .fops = &wdm_fops,
723 .minor_base = WDM_MINOR_BASE,
724 };
725
726 /* --- error handling --- */
wdm_rxwork(struct work_struct * work)727 static void wdm_rxwork(struct work_struct *work)
728 {
729 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
730 unsigned long flags;
731 int rv = 0;
732 int responding;
733
734 spin_lock_irqsave(&desc->iuspin, flags);
735 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
736 spin_unlock_irqrestore(&desc->iuspin, flags);
737 } else {
738 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
739 spin_unlock_irqrestore(&desc->iuspin, flags);
740 if (!responding)
741 rv = usb_submit_urb(desc->response, GFP_KERNEL);
742 if (rv < 0 && rv != -EPERM) {
743 spin_lock_irqsave(&desc->iuspin, flags);
744 clear_bit(WDM_RESPONDING, &desc->flags);
745 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
746 schedule_work(&desc->rxwork);
747 spin_unlock_irqrestore(&desc->iuspin, flags);
748 }
749 }
750 }
751
752 /* --- hotplug --- */
753
wdm_create(struct usb_interface * intf,struct usb_endpoint_descriptor * ep,u16 bufsize,int (* manage_power)(struct usb_interface *,int))754 static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
755 u16 bufsize, int (*manage_power)(struct usb_interface *, int))
756 {
757 int rv = -ENOMEM;
758 struct wdm_device *desc;
759
760 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
761 if (!desc)
762 goto out;
763 INIT_LIST_HEAD(&desc->device_list);
764 mutex_init(&desc->rlock);
765 mutex_init(&desc->wlock);
766 spin_lock_init(&desc->iuspin);
767 init_waitqueue_head(&desc->wait);
768 desc->wMaxCommand = bufsize;
769 /* this will be expanded and needed in hardware endianness */
770 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
771 desc->intf = intf;
772 INIT_WORK(&desc->rxwork, wdm_rxwork);
773
774 rv = -EINVAL;
775 if (!usb_endpoint_is_int_in(ep))
776 goto err;
777
778 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
779
780 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
781 if (!desc->orq)
782 goto err;
783 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
784 if (!desc->irq)
785 goto err;
786
787 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
788 if (!desc->validity)
789 goto err;
790
791 desc->response = usb_alloc_urb(0, GFP_KERNEL);
792 if (!desc->response)
793 goto err;
794
795 desc->command = usb_alloc_urb(0, GFP_KERNEL);
796 if (!desc->command)
797 goto err;
798
799 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
800 if (!desc->ubuf)
801 goto err;
802
803 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
804 if (!desc->sbuf)
805 goto err;
806
807 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
808 if (!desc->inbuf)
809 goto err;
810
811 usb_fill_int_urb(
812 desc->validity,
813 interface_to_usbdev(intf),
814 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
815 desc->sbuf,
816 desc->wMaxPacketSize,
817 wdm_int_callback,
818 desc,
819 ep->bInterval
820 );
821
822 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
823 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
824 desc->irq->wValue = 0;
825 desc->irq->wIndex = desc->inum; /* already converted */
826 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
827
828 usb_fill_control_urb(
829 desc->response,
830 interface_to_usbdev(intf),
831 /* using common endpoint 0 */
832 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
833 (unsigned char *)desc->irq,
834 desc->inbuf,
835 desc->wMaxCommand,
836 wdm_in_callback,
837 desc
838 );
839
840 desc->manage_power = manage_power;
841
842 spin_lock(&wdm_device_list_lock);
843 list_add(&desc->device_list, &wdm_device_list);
844 spin_unlock(&wdm_device_list_lock);
845
846 rv = usb_register_dev(intf, &wdm_class);
847 if (rv < 0)
848 goto err;
849 else
850 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
851 out:
852 return rv;
853 err:
854 spin_lock(&wdm_device_list_lock);
855 list_del(&desc->device_list);
856 spin_unlock(&wdm_device_list_lock);
857 cleanup(desc);
858 return rv;
859 }
860
wdm_manage_power(struct usb_interface * intf,int on)861 static int wdm_manage_power(struct usb_interface *intf, int on)
862 {
863 /* need autopm_get/put here to ensure the usbcore sees the new value */
864 int rv = usb_autopm_get_interface(intf);
865
866 intf->needs_remote_wakeup = on;
867 if (!rv)
868 usb_autopm_put_interface(intf);
869 return 0;
870 }
871
wdm_probe(struct usb_interface * intf,const struct usb_device_id * id)872 static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
873 {
874 int rv = -EINVAL;
875 struct usb_host_interface *iface;
876 struct usb_endpoint_descriptor *ep;
877 struct usb_cdc_dmm_desc *dmhd;
878 u8 *buffer = intf->altsetting->extra;
879 int buflen = intf->altsetting->extralen;
880 u16 maxcom = WDM_DEFAULT_BUFSIZE;
881
882 if (!buffer)
883 goto err;
884 while (buflen > 2) {
885 if (buffer[1] != USB_DT_CS_INTERFACE) {
886 dev_err(&intf->dev, "skipping garbage\n");
887 goto next_desc;
888 }
889
890 switch (buffer[2]) {
891 case USB_CDC_HEADER_TYPE:
892 break;
893 case USB_CDC_DMM_TYPE:
894 if (buflen < sizeof(struct usb_cdc_dmm_desc))
895 break;
896 dmhd = (struct usb_cdc_dmm_desc *)buffer;
897 maxcom = le16_to_cpu(dmhd->wMaxCommand);
898 dev_dbg(&intf->dev,
899 "Finding maximum buffer length: %d", maxcom);
900 break;
901 default:
902 dev_err(&intf->dev,
903 "Ignoring extra header, type %d, length %d\n",
904 buffer[2], buffer[0]);
905 break;
906 }
907 next_desc:
908 buflen -= buffer[0];
909 buffer += buffer[0];
910 }
911
912 iface = intf->cur_altsetting;
913 if (iface->desc.bNumEndpoints != 1)
914 goto err;
915 ep = &iface->endpoint[0].desc;
916
917 rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
918
919 err:
920 return rv;
921 }
922
923 /**
924 * usb_cdc_wdm_register - register a WDM subdriver
925 * @intf: usb interface the subdriver will associate with
926 * @ep: interrupt endpoint to monitor for notifications
927 * @bufsize: maximum message size to support for read/write
928 *
929 * Create WDM usb class character device and associate it with intf
930 * without binding, allowing another driver to manage the interface.
931 *
932 * The subdriver will manage the given interrupt endpoint exclusively
933 * and will issue control requests referring to the given intf. It
934 * will otherwise avoid interferring, and in particular not do
935 * usb_set_intfdata/usb_get_intfdata on intf.
936 *
937 * The return value is a pointer to the subdriver's struct usb_driver.
938 * The registering driver is responsible for calling this subdriver's
939 * disconnect, suspend, resume, pre_reset and post_reset methods from
940 * its own.
941 */
usb_cdc_wdm_register(struct usb_interface * intf,struct usb_endpoint_descriptor * ep,int bufsize,int (* manage_power)(struct usb_interface *,int))942 struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
943 struct usb_endpoint_descriptor *ep,
944 int bufsize,
945 int (*manage_power)(struct usb_interface *, int))
946 {
947 int rv = -EINVAL;
948
949 rv = wdm_create(intf, ep, bufsize, manage_power);
950 if (rv < 0)
951 goto err;
952
953 return &wdm_driver;
954 err:
955 return ERR_PTR(rv);
956 }
957 EXPORT_SYMBOL(usb_cdc_wdm_register);
958
wdm_disconnect(struct usb_interface * intf)959 static void wdm_disconnect(struct usb_interface *intf)
960 {
961 struct wdm_device *desc;
962 unsigned long flags;
963
964 usb_deregister_dev(intf, &wdm_class);
965 desc = wdm_find_device(intf);
966 mutex_lock(&wdm_mutex);
967
968 /* the spinlock makes sure no new urbs are generated in the callbacks */
969 spin_lock_irqsave(&desc->iuspin, flags);
970 set_bit(WDM_DISCONNECTING, &desc->flags);
971 set_bit(WDM_READ, &desc->flags);
972 /* to terminate pending flushes */
973 clear_bit(WDM_IN_USE, &desc->flags);
974 spin_unlock_irqrestore(&desc->iuspin, flags);
975 wake_up_all(&desc->wait);
976 mutex_lock(&desc->rlock);
977 mutex_lock(&desc->wlock);
978 kill_urbs(desc);
979 cancel_work_sync(&desc->rxwork);
980 mutex_unlock(&desc->wlock);
981 mutex_unlock(&desc->rlock);
982
983 /* the desc->intf pointer used as list key is now invalid */
984 spin_lock(&wdm_device_list_lock);
985 list_del(&desc->device_list);
986 spin_unlock(&wdm_device_list_lock);
987
988 if (!desc->count)
989 cleanup(desc);
990 else
991 dev_dbg(&intf->dev, "%s: %d open files - postponing cleanup\n", __func__, desc->count);
992 mutex_unlock(&wdm_mutex);
993 }
994
995 #ifdef CONFIG_PM
wdm_suspend(struct usb_interface * intf,pm_message_t message)996 static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
997 {
998 struct wdm_device *desc = wdm_find_device(intf);
999 int rv = 0;
1000
1001 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
1002
1003 /* if this is an autosuspend the caller does the locking */
1004 if (!PMSG_IS_AUTO(message)) {
1005 mutex_lock(&desc->rlock);
1006 mutex_lock(&desc->wlock);
1007 }
1008 spin_lock_irq(&desc->iuspin);
1009
1010 if (PMSG_IS_AUTO(message) &&
1011 (test_bit(WDM_IN_USE, &desc->flags)
1012 || test_bit(WDM_RESPONDING, &desc->flags))) {
1013 spin_unlock_irq(&desc->iuspin);
1014 rv = -EBUSY;
1015 } else {
1016
1017 set_bit(WDM_SUSPENDING, &desc->flags);
1018 spin_unlock_irq(&desc->iuspin);
1019 /* callback submits work - order is essential */
1020 kill_urbs(desc);
1021 cancel_work_sync(&desc->rxwork);
1022 }
1023 if (!PMSG_IS_AUTO(message)) {
1024 mutex_unlock(&desc->wlock);
1025 mutex_unlock(&desc->rlock);
1026 }
1027
1028 return rv;
1029 }
1030 #endif
1031
recover_from_urb_loss(struct wdm_device * desc)1032 static int recover_from_urb_loss(struct wdm_device *desc)
1033 {
1034 int rv = 0;
1035
1036 if (desc->count) {
1037 rv = usb_submit_urb(desc->validity, GFP_NOIO);
1038 if (rv < 0)
1039 dev_err(&desc->intf->dev,
1040 "Error resume submitting int urb - %d\n", rv);
1041 }
1042 return rv;
1043 }
1044
1045 #ifdef CONFIG_PM
wdm_resume(struct usb_interface * intf)1046 static int wdm_resume(struct usb_interface *intf)
1047 {
1048 struct wdm_device *desc = wdm_find_device(intf);
1049 int rv;
1050
1051 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
1052
1053 clear_bit(WDM_SUSPENDING, &desc->flags);
1054 rv = recover_from_urb_loss(desc);
1055
1056 return rv;
1057 }
1058 #endif
1059
wdm_pre_reset(struct usb_interface * intf)1060 static int wdm_pre_reset(struct usb_interface *intf)
1061 {
1062 struct wdm_device *desc = wdm_find_device(intf);
1063
1064 /*
1065 * we notify everybody using poll of
1066 * an exceptional situation
1067 * must be done before recovery lest a spontaneous
1068 * message from the device is lost
1069 */
1070 spin_lock_irq(&desc->iuspin);
1071 set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
1072 set_bit(WDM_READ, &desc->flags); /* unblock read */
1073 clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
1074 desc->rerr = -EINTR;
1075 spin_unlock_irq(&desc->iuspin);
1076 wake_up_all(&desc->wait);
1077 mutex_lock(&desc->rlock);
1078 mutex_lock(&desc->wlock);
1079 kill_urbs(desc);
1080 cancel_work_sync(&desc->rxwork);
1081 return 0;
1082 }
1083
wdm_post_reset(struct usb_interface * intf)1084 static int wdm_post_reset(struct usb_interface *intf)
1085 {
1086 struct wdm_device *desc = wdm_find_device(intf);
1087 int rv;
1088
1089 clear_bit(WDM_OVERFLOW, &desc->flags);
1090 clear_bit(WDM_RESETTING, &desc->flags);
1091 rv = recover_from_urb_loss(desc);
1092 mutex_unlock(&desc->wlock);
1093 mutex_unlock(&desc->rlock);
1094 return 0;
1095 }
1096
1097 static struct usb_driver wdm_driver = {
1098 .name = "cdc_wdm",
1099 .probe = wdm_probe,
1100 .disconnect = wdm_disconnect,
1101 #ifdef CONFIG_PM
1102 .suspend = wdm_suspend,
1103 .resume = wdm_resume,
1104 .reset_resume = wdm_resume,
1105 #endif
1106 .pre_reset = wdm_pre_reset,
1107 .post_reset = wdm_post_reset,
1108 .id_table = wdm_ids,
1109 .supports_autosuspend = 1,
1110 .disable_hub_initiated_lpm = 1,
1111 };
1112
1113 module_usb_driver(wdm_driver);
1114
1115 MODULE_AUTHOR(DRIVER_AUTHOR);
1116 MODULE_DESCRIPTION(DRIVER_DESC);
1117 MODULE_LICENSE("GPL");
1118