Merge branch 'for-3.11' of git://linux-nfs.org/~bfields/linux
[linux-2.6.git] / drivers / usb / class / cdc-wdm.c
blob8a230f0ef77c77b9acef90b86ab902e9cd4999eb
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 status = urb->status;
213 struct wdm_device *desc;
214 struct usb_cdc_notification *dr;
216 desc = urb->context;
217 dr = (struct usb_cdc_notification *)desc->sbuf;
219 if (status) {
220 switch (status) {
221 case -ESHUTDOWN:
222 case -ENOENT:
223 case -ECONNRESET:
224 return; /* unplug */
225 case -EPIPE:
226 set_bit(WDM_INT_STALL, &desc->flags);
227 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
228 goto sw; /* halt is cleared in work */
229 default:
230 dev_err(&desc->intf->dev,
231 "nonzero urb status received: %d\n", status);
232 break;
236 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
237 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
238 urb->actual_length);
239 goto exit;
242 switch (dr->bNotificationType) {
243 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
244 dev_dbg(&desc->intf->dev,
245 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
246 dr->wIndex, dr->wLength);
247 break;
249 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
251 dev_dbg(&desc->intf->dev,
252 "NOTIFY_NETWORK_CONNECTION %s network",
253 dr->wValue ? "connected to" : "disconnected from");
254 goto exit;
255 default:
256 clear_bit(WDM_POLL_RUNNING, &desc->flags);
257 dev_err(&desc->intf->dev,
258 "unknown notification %d received: index %d len %d\n",
259 dr->bNotificationType, dr->wIndex, dr->wLength);
260 goto exit;
263 spin_lock(&desc->iuspin);
264 clear_bit(WDM_READ, &desc->flags);
265 set_bit(WDM_RESPONDING, &desc->flags);
266 if (!test_bit(WDM_DISCONNECTING, &desc->flags)
267 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
268 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
269 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
270 __func__, rv);
272 spin_unlock(&desc->iuspin);
273 if (rv < 0) {
274 clear_bit(WDM_RESPONDING, &desc->flags);
275 if (rv == -EPERM)
276 return;
277 if (rv == -ENOMEM) {
279 rv = schedule_work(&desc->rxwork);
280 if (rv)
281 dev_err(&desc->intf->dev,
282 "Cannot schedule work\n");
285 exit:
286 rv = usb_submit_urb(urb, GFP_ATOMIC);
287 if (rv)
288 dev_err(&desc->intf->dev,
289 "%s - usb_submit_urb failed with result %d\n",
290 __func__, rv);
294 static void kill_urbs(struct wdm_device *desc)
296 /* the order here is essential */
297 usb_kill_urb(desc->command);
298 usb_kill_urb(desc->validity);
299 usb_kill_urb(desc->response);
302 static void free_urbs(struct wdm_device *desc)
304 usb_free_urb(desc->validity);
305 usb_free_urb(desc->response);
306 usb_free_urb(desc->command);
309 static void cleanup(struct wdm_device *desc)
311 kfree(desc->sbuf);
312 kfree(desc->inbuf);
313 kfree(desc->orq);
314 kfree(desc->irq);
315 kfree(desc->ubuf);
316 free_urbs(desc);
317 kfree(desc);
320 static ssize_t wdm_write
321 (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
323 u8 *buf;
324 int rv = -EMSGSIZE, r, we;
325 struct wdm_device *desc = file->private_data;
326 struct usb_ctrlrequest *req;
328 if (count > desc->wMaxCommand)
329 count = desc->wMaxCommand;
331 spin_lock_irq(&desc->iuspin);
332 we = desc->werr;
333 desc->werr = 0;
334 spin_unlock_irq(&desc->iuspin);
335 if (we < 0)
336 return -EIO;
338 buf = kmalloc(count, GFP_KERNEL);
339 if (!buf) {
340 rv = -ENOMEM;
341 goto outnl;
344 r = copy_from_user(buf, buffer, count);
345 if (r > 0) {
346 kfree(buf);
347 rv = -EFAULT;
348 goto outnl;
351 /* concurrent writes and disconnect */
352 r = mutex_lock_interruptible(&desc->wlock);
353 rv = -ERESTARTSYS;
354 if (r) {
355 kfree(buf);
356 goto outnl;
359 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
360 kfree(buf);
361 rv = -ENODEV;
362 goto outnp;
365 r = usb_autopm_get_interface(desc->intf);
366 if (r < 0) {
367 kfree(buf);
368 rv = usb_translate_errors(r);
369 goto outnp;
372 if (!(file->f_flags & O_NONBLOCK))
373 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
374 &desc->flags));
375 else
376 if (test_bit(WDM_IN_USE, &desc->flags))
377 r = -EAGAIN;
379 if (test_bit(WDM_RESETTING, &desc->flags))
380 r = -EIO;
382 if (r < 0) {
383 kfree(buf);
384 rv = r;
385 goto out;
388 req = desc->orq;
389 usb_fill_control_urb(
390 desc->command,
391 interface_to_usbdev(desc->intf),
392 /* using common endpoint 0 */
393 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
394 (unsigned char *)req,
395 buf,
396 count,
397 wdm_out_callback,
398 desc
401 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
402 USB_RECIP_INTERFACE);
403 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
404 req->wValue = 0;
405 req->wIndex = desc->inum;
406 req->wLength = cpu_to_le16(count);
407 set_bit(WDM_IN_USE, &desc->flags);
408 desc->outbuf = buf;
410 rv = usb_submit_urb(desc->command, GFP_KERNEL);
411 if (rv < 0) {
412 kfree(buf);
413 desc->outbuf = NULL;
414 clear_bit(WDM_IN_USE, &desc->flags);
415 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
416 rv = usb_translate_errors(rv);
417 } else {
418 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
419 req->wIndex);
421 out:
422 usb_autopm_put_interface(desc->intf);
423 outnp:
424 mutex_unlock(&desc->wlock);
425 outnl:
426 return rv < 0 ? rv : count;
429 static ssize_t wdm_read
430 (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
432 int rv, cntr;
433 int i = 0;
434 struct wdm_device *desc = file->private_data;
437 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
438 if (rv < 0)
439 return -ERESTARTSYS;
441 cntr = ACCESS_ONCE(desc->length);
442 if (cntr == 0) {
443 desc->read = 0;
444 retry:
445 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
446 rv = -ENODEV;
447 goto err;
449 if (test_bit(WDM_OVERFLOW, &desc->flags)) {
450 clear_bit(WDM_OVERFLOW, &desc->flags);
451 rv = -ENOBUFS;
452 goto err;
454 i++;
455 if (file->f_flags & O_NONBLOCK) {
456 if (!test_bit(WDM_READ, &desc->flags)) {
457 rv = cntr ? cntr : -EAGAIN;
458 goto err;
460 rv = 0;
461 } else {
462 rv = wait_event_interruptible(desc->wait,
463 test_bit(WDM_READ, &desc->flags));
466 /* may have happened while we slept */
467 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
468 rv = -ENODEV;
469 goto err;
471 if (test_bit(WDM_RESETTING, &desc->flags)) {
472 rv = -EIO;
473 goto err;
475 usb_mark_last_busy(interface_to_usbdev(desc->intf));
476 if (rv < 0) {
477 rv = -ERESTARTSYS;
478 goto err;
481 spin_lock_irq(&desc->iuspin);
483 if (desc->rerr) { /* read completed, error happened */
484 desc->rerr = 0;
485 spin_unlock_irq(&desc->iuspin);
486 rv = -EIO;
487 goto err;
490 * recheck whether we've lost the race
491 * against the completion handler
493 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
494 spin_unlock_irq(&desc->iuspin);
495 goto retry;
498 if (!desc->reslength) { /* zero length read */
499 dev_dbg(&desc->intf->dev, "%s: zero length - clearing WDM_READ\n", __func__);
500 clear_bit(WDM_READ, &desc->flags);
501 spin_unlock_irq(&desc->iuspin);
502 goto retry;
504 cntr = desc->length;
505 spin_unlock_irq(&desc->iuspin);
508 if (cntr > count)
509 cntr = count;
510 rv = copy_to_user(buffer, desc->ubuf, cntr);
511 if (rv > 0) {
512 rv = -EFAULT;
513 goto err;
516 spin_lock_irq(&desc->iuspin);
518 for (i = 0; i < desc->length - cntr; i++)
519 desc->ubuf[i] = desc->ubuf[i + cntr];
521 desc->length -= cntr;
522 /* in case we had outstanding data */
523 if (!desc->length)
524 clear_bit(WDM_READ, &desc->flags);
526 spin_unlock_irq(&desc->iuspin);
528 rv = cntr;
530 err:
531 mutex_unlock(&desc->rlock);
532 return rv;
535 static int wdm_flush(struct file *file, fl_owner_t id)
537 struct wdm_device *desc = file->private_data;
539 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
541 /* cannot dereference desc->intf if WDM_DISCONNECTING */
542 if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags))
543 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
544 desc->werr);
546 return usb_translate_errors(desc->werr);
549 static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
551 struct wdm_device *desc = file->private_data;
552 unsigned long flags;
553 unsigned int mask = 0;
555 spin_lock_irqsave(&desc->iuspin, flags);
556 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
557 mask = POLLHUP | POLLERR;
558 spin_unlock_irqrestore(&desc->iuspin, flags);
559 goto desc_out;
561 if (test_bit(WDM_READ, &desc->flags))
562 mask = POLLIN | POLLRDNORM;
563 if (desc->rerr || desc->werr)
564 mask |= POLLERR;
565 if (!test_bit(WDM_IN_USE, &desc->flags))
566 mask |= POLLOUT | POLLWRNORM;
567 spin_unlock_irqrestore(&desc->iuspin, flags);
569 poll_wait(file, &desc->wait, wait);
571 desc_out:
572 return mask;
575 static int wdm_open(struct inode *inode, struct file *file)
577 int minor = iminor(inode);
578 int rv = -ENODEV;
579 struct usb_interface *intf;
580 struct wdm_device *desc;
582 mutex_lock(&wdm_mutex);
583 desc = wdm_find_device_by_minor(minor);
584 if (!desc)
585 goto out;
587 intf = desc->intf;
588 if (test_bit(WDM_DISCONNECTING, &desc->flags))
589 goto out;
590 file->private_data = desc;
592 rv = usb_autopm_get_interface(desc->intf);
593 if (rv < 0) {
594 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
595 goto out;
598 /* using write lock to protect desc->count */
599 mutex_lock(&desc->wlock);
600 if (!desc->count++) {
601 desc->werr = 0;
602 desc->rerr = 0;
603 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
604 if (rv < 0) {
605 desc->count--;
606 dev_err(&desc->intf->dev,
607 "Error submitting int urb - %d\n", rv);
608 rv = usb_translate_errors(rv);
610 } else {
611 rv = 0;
613 mutex_unlock(&desc->wlock);
614 if (desc->count == 1)
615 desc->manage_power(intf, 1);
616 usb_autopm_put_interface(desc->intf);
617 out:
618 mutex_unlock(&wdm_mutex);
619 return rv;
622 static int wdm_release(struct inode *inode, struct file *file)
624 struct wdm_device *desc = file->private_data;
626 mutex_lock(&wdm_mutex);
628 /* using write lock to protect desc->count */
629 mutex_lock(&desc->wlock);
630 desc->count--;
631 mutex_unlock(&desc->wlock);
633 if (!desc->count) {
634 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
635 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
636 kill_urbs(desc);
637 desc->manage_power(desc->intf, 0);
638 } else {
639 /* must avoid dev_printk here as desc->intf is invalid */
640 pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
641 cleanup(desc);
644 mutex_unlock(&wdm_mutex);
645 return 0;
648 static long wdm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
650 struct wdm_device *desc = file->private_data;
651 int rv = 0;
653 switch (cmd) {
654 case IOCTL_WDM_MAX_COMMAND:
655 if (copy_to_user((void __user *)arg, &desc->wMaxCommand, sizeof(desc->wMaxCommand)))
656 rv = -EFAULT;
657 break;
658 default:
659 rv = -ENOTTY;
661 return rv;
664 static const struct file_operations wdm_fops = {
665 .owner = THIS_MODULE,
666 .read = wdm_read,
667 .write = wdm_write,
668 .open = wdm_open,
669 .flush = wdm_flush,
670 .release = wdm_release,
671 .poll = wdm_poll,
672 .unlocked_ioctl = wdm_ioctl,
673 .compat_ioctl = wdm_ioctl,
674 .llseek = noop_llseek,
677 static struct usb_class_driver wdm_class = {
678 .name = "cdc-wdm%d",
679 .fops = &wdm_fops,
680 .minor_base = WDM_MINOR_BASE,
683 /* --- error handling --- */
684 static void wdm_rxwork(struct work_struct *work)
686 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
687 unsigned long flags;
688 int rv;
690 spin_lock_irqsave(&desc->iuspin, flags);
691 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
692 spin_unlock_irqrestore(&desc->iuspin, flags);
693 } else {
694 spin_unlock_irqrestore(&desc->iuspin, flags);
695 rv = usb_submit_urb(desc->response, GFP_KERNEL);
696 if (rv < 0 && rv != -EPERM) {
697 spin_lock_irqsave(&desc->iuspin, flags);
698 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
699 schedule_work(&desc->rxwork);
700 spin_unlock_irqrestore(&desc->iuspin, flags);
705 /* --- hotplug --- */
707 static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
708 u16 bufsize, int (*manage_power)(struct usb_interface *, int))
710 int rv = -ENOMEM;
711 struct wdm_device *desc;
713 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
714 if (!desc)
715 goto out;
716 INIT_LIST_HEAD(&desc->device_list);
717 mutex_init(&desc->rlock);
718 mutex_init(&desc->wlock);
719 spin_lock_init(&desc->iuspin);
720 init_waitqueue_head(&desc->wait);
721 desc->wMaxCommand = bufsize;
722 /* this will be expanded and needed in hardware endianness */
723 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
724 desc->intf = intf;
725 INIT_WORK(&desc->rxwork, wdm_rxwork);
727 rv = -EINVAL;
728 if (!usb_endpoint_is_int_in(ep))
729 goto err;
731 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
733 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
734 if (!desc->orq)
735 goto err;
736 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
737 if (!desc->irq)
738 goto err;
740 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
741 if (!desc->validity)
742 goto err;
744 desc->response = usb_alloc_urb(0, GFP_KERNEL);
745 if (!desc->response)
746 goto err;
748 desc->command = usb_alloc_urb(0, GFP_KERNEL);
749 if (!desc->command)
750 goto err;
752 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
753 if (!desc->ubuf)
754 goto err;
756 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
757 if (!desc->sbuf)
758 goto err;
760 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
761 if (!desc->inbuf)
762 goto err;
764 usb_fill_int_urb(
765 desc->validity,
766 interface_to_usbdev(intf),
767 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
768 desc->sbuf,
769 desc->wMaxPacketSize,
770 wdm_int_callback,
771 desc,
772 ep->bInterval
775 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
776 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
777 desc->irq->wValue = 0;
778 desc->irq->wIndex = desc->inum;
779 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
781 usb_fill_control_urb(
782 desc->response,
783 interface_to_usbdev(intf),
784 /* using common endpoint 0 */
785 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
786 (unsigned char *)desc->irq,
787 desc->inbuf,
788 desc->wMaxCommand,
789 wdm_in_callback,
790 desc
793 desc->manage_power = manage_power;
795 spin_lock(&wdm_device_list_lock);
796 list_add(&desc->device_list, &wdm_device_list);
797 spin_unlock(&wdm_device_list_lock);
799 rv = usb_register_dev(intf, &wdm_class);
800 if (rv < 0)
801 goto err;
802 else
803 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
804 out:
805 return rv;
806 err:
807 spin_lock(&wdm_device_list_lock);
808 list_del(&desc->device_list);
809 spin_unlock(&wdm_device_list_lock);
810 cleanup(desc);
811 return rv;
814 static int wdm_manage_power(struct usb_interface *intf, int on)
816 /* need autopm_get/put here to ensure the usbcore sees the new value */
817 int rv = usb_autopm_get_interface(intf);
818 if (rv < 0)
819 goto err;
821 intf->needs_remote_wakeup = on;
822 usb_autopm_put_interface(intf);
823 err:
824 return rv;
827 static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
829 int rv = -EINVAL;
830 struct usb_host_interface *iface;
831 struct usb_endpoint_descriptor *ep;
832 struct usb_cdc_dmm_desc *dmhd;
833 u8 *buffer = intf->altsetting->extra;
834 int buflen = intf->altsetting->extralen;
835 u16 maxcom = WDM_DEFAULT_BUFSIZE;
837 if (!buffer)
838 goto err;
839 while (buflen > 2) {
840 if (buffer[1] != USB_DT_CS_INTERFACE) {
841 dev_err(&intf->dev, "skipping garbage\n");
842 goto next_desc;
845 switch (buffer[2]) {
846 case USB_CDC_HEADER_TYPE:
847 break;
848 case USB_CDC_DMM_TYPE:
849 dmhd = (struct usb_cdc_dmm_desc *)buffer;
850 maxcom = le16_to_cpu(dmhd->wMaxCommand);
851 dev_dbg(&intf->dev,
852 "Finding maximum buffer length: %d", maxcom);
853 break;
854 default:
855 dev_err(&intf->dev,
856 "Ignoring extra header, type %d, length %d\n",
857 buffer[2], buffer[0]);
858 break;
860 next_desc:
861 buflen -= buffer[0];
862 buffer += buffer[0];
865 iface = intf->cur_altsetting;
866 if (iface->desc.bNumEndpoints != 1)
867 goto err;
868 ep = &iface->endpoint[0].desc;
870 rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
872 err:
873 return rv;
877 * usb_cdc_wdm_register - register a WDM subdriver
878 * @intf: usb interface the subdriver will associate with
879 * @ep: interrupt endpoint to monitor for notifications
880 * @bufsize: maximum message size to support for read/write
882 * Create WDM usb class character device and associate it with intf
883 * without binding, allowing another driver to manage the interface.
885 * The subdriver will manage the given interrupt endpoint exclusively
886 * and will issue control requests referring to the given intf. It
887 * will otherwise avoid interferring, and in particular not do
888 * usb_set_intfdata/usb_get_intfdata on intf.
890 * The return value is a pointer to the subdriver's struct usb_driver.
891 * The registering driver is responsible for calling this subdriver's
892 * disconnect, suspend, resume, pre_reset and post_reset methods from
893 * its own.
895 struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
896 struct usb_endpoint_descriptor *ep,
897 int bufsize,
898 int (*manage_power)(struct usb_interface *, int))
900 int rv = -EINVAL;
902 rv = wdm_create(intf, ep, bufsize, manage_power);
903 if (rv < 0)
904 goto err;
906 return &wdm_driver;
907 err:
908 return ERR_PTR(rv);
910 EXPORT_SYMBOL(usb_cdc_wdm_register);
912 static void wdm_disconnect(struct usb_interface *intf)
914 struct wdm_device *desc;
915 unsigned long flags;
917 usb_deregister_dev(intf, &wdm_class);
918 desc = wdm_find_device(intf);
919 mutex_lock(&wdm_mutex);
921 /* the spinlock makes sure no new urbs are generated in the callbacks */
922 spin_lock_irqsave(&desc->iuspin, flags);
923 set_bit(WDM_DISCONNECTING, &desc->flags);
924 set_bit(WDM_READ, &desc->flags);
925 /* to terminate pending flushes */
926 clear_bit(WDM_IN_USE, &desc->flags);
927 spin_unlock_irqrestore(&desc->iuspin, flags);
928 wake_up_all(&desc->wait);
929 mutex_lock(&desc->rlock);
930 mutex_lock(&desc->wlock);
931 kill_urbs(desc);
932 cancel_work_sync(&desc->rxwork);
933 mutex_unlock(&desc->wlock);
934 mutex_unlock(&desc->rlock);
936 /* the desc->intf pointer used as list key is now invalid */
937 spin_lock(&wdm_device_list_lock);
938 list_del(&desc->device_list);
939 spin_unlock(&wdm_device_list_lock);
941 if (!desc->count)
942 cleanup(desc);
943 else
944 dev_dbg(&intf->dev, "%s: %d open files - postponing cleanup\n", __func__, desc->count);
945 mutex_unlock(&wdm_mutex);
948 #ifdef CONFIG_PM
949 static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
951 struct wdm_device *desc = wdm_find_device(intf);
952 int rv = 0;
954 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
956 /* if this is an autosuspend the caller does the locking */
957 if (!PMSG_IS_AUTO(message)) {
958 mutex_lock(&desc->rlock);
959 mutex_lock(&desc->wlock);
961 spin_lock_irq(&desc->iuspin);
963 if (PMSG_IS_AUTO(message) &&
964 (test_bit(WDM_IN_USE, &desc->flags)
965 || test_bit(WDM_RESPONDING, &desc->flags))) {
966 spin_unlock_irq(&desc->iuspin);
967 rv = -EBUSY;
968 } else {
970 set_bit(WDM_SUSPENDING, &desc->flags);
971 spin_unlock_irq(&desc->iuspin);
972 /* callback submits work - order is essential */
973 kill_urbs(desc);
974 cancel_work_sync(&desc->rxwork);
976 if (!PMSG_IS_AUTO(message)) {
977 mutex_unlock(&desc->wlock);
978 mutex_unlock(&desc->rlock);
981 return rv;
983 #endif
985 static int recover_from_urb_loss(struct wdm_device *desc)
987 int rv = 0;
989 if (desc->count) {
990 rv = usb_submit_urb(desc->validity, GFP_NOIO);
991 if (rv < 0)
992 dev_err(&desc->intf->dev,
993 "Error resume submitting int urb - %d\n", rv);
995 return rv;
998 #ifdef CONFIG_PM
999 static int wdm_resume(struct usb_interface *intf)
1001 struct wdm_device *desc = wdm_find_device(intf);
1002 int rv;
1004 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
1006 clear_bit(WDM_SUSPENDING, &desc->flags);
1007 rv = recover_from_urb_loss(desc);
1009 return rv;
1011 #endif
1013 static int wdm_pre_reset(struct usb_interface *intf)
1015 struct wdm_device *desc = wdm_find_device(intf);
1018 * we notify everybody using poll of
1019 * an exceptional situation
1020 * must be done before recovery lest a spontaneous
1021 * message from the device is lost
1023 spin_lock_irq(&desc->iuspin);
1024 set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
1025 set_bit(WDM_READ, &desc->flags); /* unblock read */
1026 clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
1027 desc->rerr = -EINTR;
1028 spin_unlock_irq(&desc->iuspin);
1029 wake_up_all(&desc->wait);
1030 mutex_lock(&desc->rlock);
1031 mutex_lock(&desc->wlock);
1032 kill_urbs(desc);
1033 cancel_work_sync(&desc->rxwork);
1034 return 0;
1037 static int wdm_post_reset(struct usb_interface *intf)
1039 struct wdm_device *desc = wdm_find_device(intf);
1040 int rv;
1042 clear_bit(WDM_OVERFLOW, &desc->flags);
1043 clear_bit(WDM_RESETTING, &desc->flags);
1044 rv = recover_from_urb_loss(desc);
1045 mutex_unlock(&desc->wlock);
1046 mutex_unlock(&desc->rlock);
1047 return 0;
1050 static struct usb_driver wdm_driver = {
1051 .name = "cdc_wdm",
1052 .probe = wdm_probe,
1053 .disconnect = wdm_disconnect,
1054 #ifdef CONFIG_PM
1055 .suspend = wdm_suspend,
1056 .resume = wdm_resume,
1057 .reset_resume = wdm_resume,
1058 #endif
1059 .pre_reset = wdm_pre_reset,
1060 .post_reset = wdm_post_reset,
1061 .id_table = wdm_ids,
1062 .supports_autosuspend = 1,
1063 .disable_hub_initiated_lpm = 1,
1066 module_usb_driver(wdm_driver);
1068 MODULE_AUTHOR(DRIVER_AUTHOR);
1069 MODULE_DESCRIPTION(DRIVER_DESC);
1070 MODULE_LICENSE("GPL");