Linux 3.12.39
[linux/fpc-iii.git] / drivers / usb / class / cdc-wdm.c
blob6463ca3bcfbacee84d0155291f10df812f650940
1 /*
2 * cdc-wdm.c
4 * This driver supports USB CDC WCM Device Management.
6 * Copyright (c) 2007-2009 Oliver Neukum
8 * Some code taken from cdc-acm.c
10 * Released under the GPLv2.
12 * Many thanks to Carl Nordbeck
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>
30 * Version Information
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"
36 static const struct usb_device_id wdm_ids[] = {
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
43 { }
46 MODULE_DEVICE_TABLE (usb, wdm_ids);
48 #define WDM_MINOR_BASE 176
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
62 #define WDM_MAX 16
64 /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
65 #define WDM_DEFAULT_BUFSIZE 256
67 static DEFINE_MUTEX(wdm_mutex);
68 static DEFINE_SPINLOCK(wdm_device_list_lock);
69 static LIST_HEAD(wdm_device_list);
71 /* --- method tables --- */
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 */
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;
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;
105 struct list_head device_list;
106 int (*manage_power)(struct usb_interface *, int);
109 static struct usb_driver wdm_driver;
111 /* return intfdata if we own the interface, else look up intf in the list */
112 static struct wdm_device *wdm_find_device(struct usb_interface *intf)
114 struct wdm_device *desc;
116 spin_lock(&wdm_device_list_lock);
117 list_for_each_entry(desc, &wdm_device_list, device_list)
118 if (desc->intf == intf)
119 goto found;
120 desc = NULL;
121 found:
122 spin_unlock(&wdm_device_list_lock);
124 return desc;
127 static struct wdm_device *wdm_find_device_by_minor(int minor)
129 struct wdm_device *desc;
131 spin_lock(&wdm_device_list_lock);
132 list_for_each_entry(desc, &wdm_device_list, device_list)
133 if (desc->intf->minor == minor)
134 goto found;
135 desc = NULL;
136 found:
137 spin_unlock(&wdm_device_list_lock);
139 return desc;
142 /* --- callbacks --- */
143 static void wdm_out_callback(struct urb *urb)
145 struct wdm_device *desc;
146 desc = urb->context;
147 spin_lock(&desc->iuspin);
148 desc->werr = urb->status;
149 spin_unlock(&desc->iuspin);
150 kfree(desc->outbuf);
151 desc->outbuf = NULL;
152 clear_bit(WDM_IN_USE, &desc->flags);
153 wake_up(&desc->wait);
156 static void wdm_in_callback(struct urb *urb)
158 struct wdm_device *desc = urb->context;
159 int status = urb->status;
160 int length = urb->actual_length;
162 spin_lock(&desc->iuspin);
163 clear_bit(WDM_RESPONDING, &desc->flags);
165 if (status) {
166 switch (status) {
167 case -ENOENT:
168 dev_dbg(&desc->intf->dev,
169 "nonzero urb status received: -ENOENT");
170 goto skip_error;
171 case -ECONNRESET:
172 dev_dbg(&desc->intf->dev,
173 "nonzero urb status received: -ECONNRESET");
174 goto skip_error;
175 case -ESHUTDOWN:
176 dev_dbg(&desc->intf->dev,
177 "nonzero urb status received: -ESHUTDOWN");
178 goto skip_error;
179 case -EPIPE:
180 dev_err(&desc->intf->dev,
181 "nonzero urb status received: -EPIPE\n");
182 break;
183 default:
184 dev_err(&desc->intf->dev,
185 "Unexpected error %d\n", status);
186 break;
190 desc->rerr = status;
191 if (length + desc->length > desc->wMaxCommand) {
192 /* The buffer would overflow */
193 set_bit(WDM_OVERFLOW, &desc->flags);
194 } else {
195 /* we may already be in overflow */
196 if (!test_bit(WDM_OVERFLOW, &desc->flags)) {
197 memmove(desc->ubuf + desc->length, desc->inbuf, length);
198 desc->length += length;
199 desc->reslength = length;
202 skip_error:
203 wake_up(&desc->wait);
205 set_bit(WDM_READ, &desc->flags);
206 spin_unlock(&desc->iuspin);
209 static void wdm_int_callback(struct urb *urb)
211 int rv = 0;
212 int responding;
213 int status = urb->status;
214 struct wdm_device *desc;
215 struct usb_cdc_notification *dr;
217 desc = urb->context;
218 dr = (struct usb_cdc_notification *)desc->sbuf;
220 if (status) {
221 switch (status) {
222 case -ESHUTDOWN:
223 case -ENOENT:
224 case -ECONNRESET:
225 return; /* unplug */
226 case -EPIPE:
227 set_bit(WDM_INT_STALL, &desc->flags);
228 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
229 goto sw; /* halt is cleared in work */
230 default:
231 dev_err(&desc->intf->dev,
232 "nonzero urb status received: %d\n", status);
233 break;
237 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
238 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
239 urb->actual_length);
240 goto exit;
243 switch (dr->bNotificationType) {
244 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
245 dev_dbg(&desc->intf->dev,
246 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
247 dr->wIndex, dr->wLength);
248 break;
250 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
252 dev_dbg(&desc->intf->dev,
253 "NOTIFY_NETWORK_CONNECTION %s network",
254 dr->wValue ? "connected to" : "disconnected from");
255 goto exit;
256 default:
257 clear_bit(WDM_POLL_RUNNING, &desc->flags);
258 dev_err(&desc->intf->dev,
259 "unknown notification %d received: index %d len %d\n",
260 dr->bNotificationType, dr->wIndex, dr->wLength);
261 goto exit;
264 spin_lock(&desc->iuspin);
265 clear_bit(WDM_READ, &desc->flags);
266 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
267 if (!responding && !test_bit(WDM_DISCONNECTING, &desc->flags)
268 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
269 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
270 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
271 __func__, rv);
273 spin_unlock(&desc->iuspin);
274 if (rv < 0) {
275 clear_bit(WDM_RESPONDING, &desc->flags);
276 if (rv == -EPERM)
277 return;
278 if (rv == -ENOMEM) {
280 rv = schedule_work(&desc->rxwork);
281 if (rv)
282 dev_err(&desc->intf->dev,
283 "Cannot schedule work\n");
286 exit:
287 rv = usb_submit_urb(urb, GFP_ATOMIC);
288 if (rv)
289 dev_err(&desc->intf->dev,
290 "%s - usb_submit_urb failed with result %d\n",
291 __func__, rv);
295 static void kill_urbs(struct wdm_device *desc)
297 /* the order here is essential */
298 usb_kill_urb(desc->command);
299 usb_kill_urb(desc->validity);
300 usb_kill_urb(desc->response);
303 static void free_urbs(struct wdm_device *desc)
305 usb_free_urb(desc->validity);
306 usb_free_urb(desc->response);
307 usb_free_urb(desc->command);
310 static void cleanup(struct wdm_device *desc)
312 kfree(desc->sbuf);
313 kfree(desc->inbuf);
314 kfree(desc->orq);
315 kfree(desc->irq);
316 kfree(desc->ubuf);
317 free_urbs(desc);
318 kfree(desc);
321 static ssize_t wdm_write
322 (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
324 u8 *buf;
325 int rv = -EMSGSIZE, r, we;
326 struct wdm_device *desc = file->private_data;
327 struct usb_ctrlrequest *req;
329 if (count > desc->wMaxCommand)
330 count = desc->wMaxCommand;
332 spin_lock_irq(&desc->iuspin);
333 we = desc->werr;
334 desc->werr = 0;
335 spin_unlock_irq(&desc->iuspin);
336 if (we < 0)
337 return -EIO;
339 buf = kmalloc(count, GFP_KERNEL);
340 if (!buf) {
341 rv = -ENOMEM;
342 goto outnl;
345 r = copy_from_user(buf, buffer, count);
346 if (r > 0) {
347 kfree(buf);
348 rv = -EFAULT;
349 goto outnl;
352 /* concurrent writes and disconnect */
353 r = mutex_lock_interruptible(&desc->wlock);
354 rv = -ERESTARTSYS;
355 if (r) {
356 kfree(buf);
357 goto outnl;
360 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
361 kfree(buf);
362 rv = -ENODEV;
363 goto outnp;
366 r = usb_autopm_get_interface(desc->intf);
367 if (r < 0) {
368 kfree(buf);
369 rv = usb_translate_errors(r);
370 goto outnp;
373 if (!(file->f_flags & O_NONBLOCK))
374 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
375 &desc->flags));
376 else
377 if (test_bit(WDM_IN_USE, &desc->flags))
378 r = -EAGAIN;
380 if (test_bit(WDM_RESETTING, &desc->flags))
381 r = -EIO;
383 if (r < 0) {
384 kfree(buf);
385 rv = r;
386 goto out;
389 req = desc->orq;
390 usb_fill_control_urb(
391 desc->command,
392 interface_to_usbdev(desc->intf),
393 /* using common endpoint 0 */
394 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
395 (unsigned char *)req,
396 buf,
397 count,
398 wdm_out_callback,
399 desc
402 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
403 USB_RECIP_INTERFACE);
404 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
405 req->wValue = 0;
406 req->wIndex = desc->inum;
407 req->wLength = cpu_to_le16(count);
408 set_bit(WDM_IN_USE, &desc->flags);
409 desc->outbuf = buf;
411 rv = usb_submit_urb(desc->command, GFP_KERNEL);
412 if (rv < 0) {
413 kfree(buf);
414 desc->outbuf = NULL;
415 clear_bit(WDM_IN_USE, &desc->flags);
416 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
417 rv = usb_translate_errors(rv);
418 } else {
419 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
420 req->wIndex);
422 out:
423 usb_autopm_put_interface(desc->intf);
424 outnp:
425 mutex_unlock(&desc->wlock);
426 outnl:
427 return rv < 0 ? rv : count;
430 static ssize_t wdm_read
431 (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
433 int rv, cntr;
434 int i = 0;
435 struct wdm_device *desc = file->private_data;
438 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
439 if (rv < 0)
440 return -ERESTARTSYS;
442 cntr = ACCESS_ONCE(desc->length);
443 if (cntr == 0) {
444 desc->read = 0;
445 retry:
446 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
447 rv = -ENODEV;
448 goto err;
450 if (test_bit(WDM_OVERFLOW, &desc->flags)) {
451 clear_bit(WDM_OVERFLOW, &desc->flags);
452 rv = -ENOBUFS;
453 goto err;
455 i++;
456 if (file->f_flags & O_NONBLOCK) {
457 if (!test_bit(WDM_READ, &desc->flags)) {
458 rv = cntr ? cntr : -EAGAIN;
459 goto err;
461 rv = 0;
462 } else {
463 rv = wait_event_interruptible(desc->wait,
464 test_bit(WDM_READ, &desc->flags));
467 /* may have happened while we slept */
468 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
469 rv = -ENODEV;
470 goto err;
472 if (test_bit(WDM_RESETTING, &desc->flags)) {
473 rv = -EIO;
474 goto err;
476 usb_mark_last_busy(interface_to_usbdev(desc->intf));
477 if (rv < 0) {
478 rv = -ERESTARTSYS;
479 goto err;
482 spin_lock_irq(&desc->iuspin);
484 if (desc->rerr) { /* read completed, error happened */
485 desc->rerr = 0;
486 spin_unlock_irq(&desc->iuspin);
487 rv = -EIO;
488 goto err;
491 * recheck whether we've lost the race
492 * against the completion handler
494 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
495 spin_unlock_irq(&desc->iuspin);
496 goto retry;
499 if (!desc->reslength) { /* zero length read */
500 dev_dbg(&desc->intf->dev, "%s: zero length - clearing WDM_READ\n", __func__);
501 clear_bit(WDM_READ, &desc->flags);
502 spin_unlock_irq(&desc->iuspin);
503 goto retry;
505 cntr = desc->length;
506 spin_unlock_irq(&desc->iuspin);
509 if (cntr > count)
510 cntr = count;
511 rv = copy_to_user(buffer, desc->ubuf, cntr);
512 if (rv > 0) {
513 rv = -EFAULT;
514 goto err;
517 spin_lock_irq(&desc->iuspin);
519 for (i = 0; i < desc->length - cntr; i++)
520 desc->ubuf[i] = desc->ubuf[i + cntr];
522 desc->length -= cntr;
523 /* in case we had outstanding data */
524 if (!desc->length)
525 clear_bit(WDM_READ, &desc->flags);
527 spin_unlock_irq(&desc->iuspin);
529 rv = cntr;
531 err:
532 mutex_unlock(&desc->rlock);
533 return rv;
536 static int wdm_flush(struct file *file, fl_owner_t id)
538 struct wdm_device *desc = file->private_data;
540 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
542 /* cannot dereference desc->intf if WDM_DISCONNECTING */
543 if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags))
544 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
545 desc->werr);
547 return usb_translate_errors(desc->werr);
550 static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
552 struct wdm_device *desc = file->private_data;
553 unsigned long flags;
554 unsigned int mask = 0;
556 spin_lock_irqsave(&desc->iuspin, flags);
557 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
558 mask = POLLHUP | POLLERR;
559 spin_unlock_irqrestore(&desc->iuspin, flags);
560 goto desc_out;
562 if (test_bit(WDM_READ, &desc->flags))
563 mask = POLLIN | POLLRDNORM;
564 if (desc->rerr || desc->werr)
565 mask |= POLLERR;
566 if (!test_bit(WDM_IN_USE, &desc->flags))
567 mask |= POLLOUT | POLLWRNORM;
568 spin_unlock_irqrestore(&desc->iuspin, flags);
570 poll_wait(file, &desc->wait, wait);
572 desc_out:
573 return mask;
576 static int wdm_open(struct inode *inode, struct file *file)
578 int minor = iminor(inode);
579 int rv = -ENODEV;
580 struct usb_interface *intf;
581 struct wdm_device *desc;
583 mutex_lock(&wdm_mutex);
584 desc = wdm_find_device_by_minor(minor);
585 if (!desc)
586 goto out;
588 intf = desc->intf;
589 if (test_bit(WDM_DISCONNECTING, &desc->flags))
590 goto out;
591 file->private_data = desc;
593 rv = usb_autopm_get_interface(desc->intf);
594 if (rv < 0) {
595 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
596 goto out;
599 /* using write lock to protect desc->count */
600 mutex_lock(&desc->wlock);
601 if (!desc->count++) {
602 desc->werr = 0;
603 desc->rerr = 0;
604 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
605 if (rv < 0) {
606 desc->count--;
607 dev_err(&desc->intf->dev,
608 "Error submitting int urb - %d\n", rv);
609 rv = usb_translate_errors(rv);
611 } else {
612 rv = 0;
614 mutex_unlock(&desc->wlock);
615 if (desc->count == 1)
616 desc->manage_power(intf, 1);
617 usb_autopm_put_interface(desc->intf);
618 out:
619 mutex_unlock(&wdm_mutex);
620 return rv;
623 static int wdm_release(struct inode *inode, struct file *file)
625 struct wdm_device *desc = file->private_data;
627 mutex_lock(&wdm_mutex);
629 /* using write lock to protect desc->count */
630 mutex_lock(&desc->wlock);
631 desc->count--;
632 mutex_unlock(&desc->wlock);
634 if (!desc->count) {
635 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
636 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
637 kill_urbs(desc);
638 desc->manage_power(desc->intf, 0);
639 } else {
640 /* must avoid dev_printk here as desc->intf is invalid */
641 pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
642 cleanup(desc);
645 mutex_unlock(&wdm_mutex);
646 return 0;
649 static long wdm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
651 struct wdm_device *desc = file->private_data;
652 int rv = 0;
654 switch (cmd) {
655 case IOCTL_WDM_MAX_COMMAND:
656 if (copy_to_user((void __user *)arg, &desc->wMaxCommand, sizeof(desc->wMaxCommand)))
657 rv = -EFAULT;
658 break;
659 default:
660 rv = -ENOTTY;
662 return rv;
665 static const struct file_operations wdm_fops = {
666 .owner = THIS_MODULE,
667 .read = wdm_read,
668 .write = wdm_write,
669 .open = wdm_open,
670 .flush = wdm_flush,
671 .release = wdm_release,
672 .poll = wdm_poll,
673 .unlocked_ioctl = wdm_ioctl,
674 .compat_ioctl = wdm_ioctl,
675 .llseek = noop_llseek,
678 static struct usb_class_driver wdm_class = {
679 .name = "cdc-wdm%d",
680 .fops = &wdm_fops,
681 .minor_base = WDM_MINOR_BASE,
684 /* --- error handling --- */
685 static void wdm_rxwork(struct work_struct *work)
687 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
688 unsigned long flags;
689 int rv = 0;
690 int responding;
692 spin_lock_irqsave(&desc->iuspin, flags);
693 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
694 spin_unlock_irqrestore(&desc->iuspin, flags);
695 } else {
696 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
697 spin_unlock_irqrestore(&desc->iuspin, flags);
698 if (!responding)
699 rv = usb_submit_urb(desc->response, GFP_KERNEL);
700 if (rv < 0 && rv != -EPERM) {
701 spin_lock_irqsave(&desc->iuspin, flags);
702 clear_bit(WDM_RESPONDING, &desc->flags);
703 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
704 schedule_work(&desc->rxwork);
705 spin_unlock_irqrestore(&desc->iuspin, flags);
710 /* --- hotplug --- */
712 static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
713 u16 bufsize, int (*manage_power)(struct usb_interface *, int))
715 int rv = -ENOMEM;
716 struct wdm_device *desc;
718 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
719 if (!desc)
720 goto out;
721 INIT_LIST_HEAD(&desc->device_list);
722 mutex_init(&desc->rlock);
723 mutex_init(&desc->wlock);
724 spin_lock_init(&desc->iuspin);
725 init_waitqueue_head(&desc->wait);
726 desc->wMaxCommand = bufsize;
727 /* this will be expanded and needed in hardware endianness */
728 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
729 desc->intf = intf;
730 INIT_WORK(&desc->rxwork, wdm_rxwork);
732 rv = -EINVAL;
733 if (!usb_endpoint_is_int_in(ep))
734 goto err;
736 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
738 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
739 if (!desc->orq)
740 goto err;
741 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
742 if (!desc->irq)
743 goto err;
745 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
746 if (!desc->validity)
747 goto err;
749 desc->response = usb_alloc_urb(0, GFP_KERNEL);
750 if (!desc->response)
751 goto err;
753 desc->command = usb_alloc_urb(0, GFP_KERNEL);
754 if (!desc->command)
755 goto err;
757 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
758 if (!desc->ubuf)
759 goto err;
761 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
762 if (!desc->sbuf)
763 goto err;
765 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
766 if (!desc->inbuf)
767 goto err;
769 usb_fill_int_urb(
770 desc->validity,
771 interface_to_usbdev(intf),
772 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
773 desc->sbuf,
774 desc->wMaxPacketSize,
775 wdm_int_callback,
776 desc,
777 ep->bInterval
780 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
781 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
782 desc->irq->wValue = 0;
783 desc->irq->wIndex = desc->inum;
784 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
786 usb_fill_control_urb(
787 desc->response,
788 interface_to_usbdev(intf),
789 /* using common endpoint 0 */
790 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
791 (unsigned char *)desc->irq,
792 desc->inbuf,
793 desc->wMaxCommand,
794 wdm_in_callback,
795 desc
798 desc->manage_power = manage_power;
800 spin_lock(&wdm_device_list_lock);
801 list_add(&desc->device_list, &wdm_device_list);
802 spin_unlock(&wdm_device_list_lock);
804 rv = usb_register_dev(intf, &wdm_class);
805 if (rv < 0)
806 goto err;
807 else
808 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
809 out:
810 return rv;
811 err:
812 spin_lock(&wdm_device_list_lock);
813 list_del(&desc->device_list);
814 spin_unlock(&wdm_device_list_lock);
815 cleanup(desc);
816 return rv;
819 static int wdm_manage_power(struct usb_interface *intf, int on)
821 /* need autopm_get/put here to ensure the usbcore sees the new value */
822 int rv = usb_autopm_get_interface(intf);
824 intf->needs_remote_wakeup = on;
825 if (!rv)
826 usb_autopm_put_interface(intf);
827 return 0;
830 static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
832 int rv = -EINVAL;
833 struct usb_host_interface *iface;
834 struct usb_endpoint_descriptor *ep;
835 struct usb_cdc_dmm_desc *dmhd;
836 u8 *buffer = intf->altsetting->extra;
837 int buflen = intf->altsetting->extralen;
838 u16 maxcom = WDM_DEFAULT_BUFSIZE;
840 if (!buffer)
841 goto err;
842 while (buflen > 2) {
843 if (buffer[1] != USB_DT_CS_INTERFACE) {
844 dev_err(&intf->dev, "skipping garbage\n");
845 goto next_desc;
848 switch (buffer[2]) {
849 case USB_CDC_HEADER_TYPE:
850 break;
851 case USB_CDC_DMM_TYPE:
852 dmhd = (struct usb_cdc_dmm_desc *)buffer;
853 maxcom = le16_to_cpu(dmhd->wMaxCommand);
854 dev_dbg(&intf->dev,
855 "Finding maximum buffer length: %d", maxcom);
856 break;
857 default:
858 dev_err(&intf->dev,
859 "Ignoring extra header, type %d, length %d\n",
860 buffer[2], buffer[0]);
861 break;
863 next_desc:
864 buflen -= buffer[0];
865 buffer += buffer[0];
868 iface = intf->cur_altsetting;
869 if (iface->desc.bNumEndpoints != 1)
870 goto err;
871 ep = &iface->endpoint[0].desc;
873 rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
875 err:
876 return rv;
880 * usb_cdc_wdm_register - register a WDM subdriver
881 * @intf: usb interface the subdriver will associate with
882 * @ep: interrupt endpoint to monitor for notifications
883 * @bufsize: maximum message size to support for read/write
885 * Create WDM usb class character device and associate it with intf
886 * without binding, allowing another driver to manage the interface.
888 * The subdriver will manage the given interrupt endpoint exclusively
889 * and will issue control requests referring to the given intf. It
890 * will otherwise avoid interferring, and in particular not do
891 * usb_set_intfdata/usb_get_intfdata on intf.
893 * The return value is a pointer to the subdriver's struct usb_driver.
894 * The registering driver is responsible for calling this subdriver's
895 * disconnect, suspend, resume, pre_reset and post_reset methods from
896 * its own.
898 struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
899 struct usb_endpoint_descriptor *ep,
900 int bufsize,
901 int (*manage_power)(struct usb_interface *, int))
903 int rv = -EINVAL;
905 rv = wdm_create(intf, ep, bufsize, manage_power);
906 if (rv < 0)
907 goto err;
909 return &wdm_driver;
910 err:
911 return ERR_PTR(rv);
913 EXPORT_SYMBOL(usb_cdc_wdm_register);
915 static void wdm_disconnect(struct usb_interface *intf)
917 struct wdm_device *desc;
918 unsigned long flags;
920 usb_deregister_dev(intf, &wdm_class);
921 desc = wdm_find_device(intf);
922 mutex_lock(&wdm_mutex);
924 /* the spinlock makes sure no new urbs are generated in the callbacks */
925 spin_lock_irqsave(&desc->iuspin, flags);
926 set_bit(WDM_DISCONNECTING, &desc->flags);
927 set_bit(WDM_READ, &desc->flags);
928 /* to terminate pending flushes */
929 clear_bit(WDM_IN_USE, &desc->flags);
930 spin_unlock_irqrestore(&desc->iuspin, flags);
931 wake_up_all(&desc->wait);
932 mutex_lock(&desc->rlock);
933 mutex_lock(&desc->wlock);
934 kill_urbs(desc);
935 cancel_work_sync(&desc->rxwork);
936 mutex_unlock(&desc->wlock);
937 mutex_unlock(&desc->rlock);
939 /* the desc->intf pointer used as list key is now invalid */
940 spin_lock(&wdm_device_list_lock);
941 list_del(&desc->device_list);
942 spin_unlock(&wdm_device_list_lock);
944 if (!desc->count)
945 cleanup(desc);
946 else
947 dev_dbg(&intf->dev, "%s: %d open files - postponing cleanup\n", __func__, desc->count);
948 mutex_unlock(&wdm_mutex);
951 #ifdef CONFIG_PM
952 static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
954 struct wdm_device *desc = wdm_find_device(intf);
955 int rv = 0;
957 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
959 /* if this is an autosuspend the caller does the locking */
960 if (!PMSG_IS_AUTO(message)) {
961 mutex_lock(&desc->rlock);
962 mutex_lock(&desc->wlock);
964 spin_lock_irq(&desc->iuspin);
966 if (PMSG_IS_AUTO(message) &&
967 (test_bit(WDM_IN_USE, &desc->flags)
968 || test_bit(WDM_RESPONDING, &desc->flags))) {
969 spin_unlock_irq(&desc->iuspin);
970 rv = -EBUSY;
971 } else {
973 set_bit(WDM_SUSPENDING, &desc->flags);
974 spin_unlock_irq(&desc->iuspin);
975 /* callback submits work - order is essential */
976 kill_urbs(desc);
977 cancel_work_sync(&desc->rxwork);
979 if (!PMSG_IS_AUTO(message)) {
980 mutex_unlock(&desc->wlock);
981 mutex_unlock(&desc->rlock);
984 return rv;
986 #endif
988 static int recover_from_urb_loss(struct wdm_device *desc)
990 int rv = 0;
992 if (desc->count) {
993 rv = usb_submit_urb(desc->validity, GFP_NOIO);
994 if (rv < 0)
995 dev_err(&desc->intf->dev,
996 "Error resume submitting int urb - %d\n", rv);
998 return rv;
1001 #ifdef CONFIG_PM
1002 static int wdm_resume(struct usb_interface *intf)
1004 struct wdm_device *desc = wdm_find_device(intf);
1005 int rv;
1007 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
1009 clear_bit(WDM_SUSPENDING, &desc->flags);
1010 rv = recover_from_urb_loss(desc);
1012 return rv;
1014 #endif
1016 static int wdm_pre_reset(struct usb_interface *intf)
1018 struct wdm_device *desc = wdm_find_device(intf);
1021 * we notify everybody using poll of
1022 * an exceptional situation
1023 * must be done before recovery lest a spontaneous
1024 * message from the device is lost
1026 spin_lock_irq(&desc->iuspin);
1027 set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
1028 set_bit(WDM_READ, &desc->flags); /* unblock read */
1029 clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
1030 desc->rerr = -EINTR;
1031 spin_unlock_irq(&desc->iuspin);
1032 wake_up_all(&desc->wait);
1033 mutex_lock(&desc->rlock);
1034 mutex_lock(&desc->wlock);
1035 kill_urbs(desc);
1036 cancel_work_sync(&desc->rxwork);
1037 return 0;
1040 static int wdm_post_reset(struct usb_interface *intf)
1042 struct wdm_device *desc = wdm_find_device(intf);
1043 int rv;
1045 clear_bit(WDM_OVERFLOW, &desc->flags);
1046 clear_bit(WDM_RESETTING, &desc->flags);
1047 rv = recover_from_urb_loss(desc);
1048 mutex_unlock(&desc->wlock);
1049 mutex_unlock(&desc->rlock);
1050 return 0;
1053 static struct usb_driver wdm_driver = {
1054 .name = "cdc_wdm",
1055 .probe = wdm_probe,
1056 .disconnect = wdm_disconnect,
1057 #ifdef CONFIG_PM
1058 .suspend = wdm_suspend,
1059 .resume = wdm_resume,
1060 .reset_resume = wdm_resume,
1061 #endif
1062 .pre_reset = wdm_pre_reset,
1063 .post_reset = wdm_post_reset,
1064 .id_table = wdm_ids,
1065 .supports_autosuspend = 1,
1066 .disable_hub_initiated_lpm = 1,
1069 module_usb_driver(wdm_driver);
1071 MODULE_AUTHOR(DRIVER_AUTHOR);
1072 MODULE_DESCRIPTION(DRIVER_DESC);
1073 MODULE_LICENSE("GPL");