4 * This driver supports USB CDC WCM Device Management.
6 * Copyright (c) 2007-2008 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/slab.h>
17 #include <linux/module.h>
18 #include <linux/smp_lock.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>
31 #define DRIVER_VERSION "v0.03"
32 #define DRIVER_AUTHOR "Oliver Neukum"
33 #define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management"
35 static struct usb_device_id wdm_ids[] = {
37 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
38 USB_DEVICE_ID_MATCH_INT_SUBCLASS,
39 .bInterfaceClass = USB_CLASS_COMM,
40 .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM
45 #define WDM_MINOR_BASE 176
49 #define WDM_DISCONNECTING 2
52 #define WDM_INT_STALL 5
53 #define WDM_POLL_RUNNING 6
59 static DEFINE_MUTEX(wdm_mutex);
61 /* --- method tables --- */
64 u8 *inbuf; /* buffer for response */
65 u8 *outbuf; /* buffer for command */
66 u8 *sbuf; /* buffer for status */
67 u8 *ubuf; /* buffer for copy to user space */
72 struct usb_interface *intf;
73 struct usb_ctrlrequest *orq;
74 struct usb_ctrlrequest *irq;
92 wait_queue_head_t wait;
93 struct work_struct rxwork;
98 static struct usb_driver wdm_driver;
100 /* --- callbacks --- */
101 static void wdm_out_callback(struct urb *urb)
103 struct wdm_device *desc;
105 spin_lock(&desc->iuspin);
106 desc->werr = urb->status;
107 spin_unlock(&desc->iuspin);
108 clear_bit(WDM_IN_USE, &desc->flags);
110 wake_up(&desc->wait);
113 static void wdm_in_callback(struct urb *urb)
115 struct wdm_device *desc = urb->context;
116 int status = urb->status;
118 spin_lock(&desc->iuspin);
123 dev_dbg(&desc->intf->dev,
124 "nonzero urb status received: -ENOENT");
127 dev_dbg(&desc->intf->dev,
128 "nonzero urb status received: -ECONNRESET");
131 dev_dbg(&desc->intf->dev,
132 "nonzero urb status received: -ESHUTDOWN");
135 err("nonzero urb status received: -EPIPE");
138 err("Unexpected error %d", status);
144 desc->reslength = urb->actual_length;
145 memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength);
146 desc->length += desc->reslength;
147 wake_up(&desc->wait);
149 set_bit(WDM_READ, &desc->flags);
150 spin_unlock(&desc->iuspin);
153 static void wdm_int_callback(struct urb *urb)
156 int status = urb->status;
157 struct wdm_device *desc;
158 struct usb_ctrlrequest *req;
159 struct usb_cdc_notification *dr;
163 dr = (struct usb_cdc_notification *)desc->sbuf;
172 set_bit(WDM_INT_STALL, &desc->flags);
173 err("Stall on int endpoint");
174 goto sw; /* halt is cleared in work */
176 err("nonzero urb status received: %d", status);
181 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
182 err("wdm_int_callback - %d bytes", urb->actual_length);
186 switch (dr->bNotificationType) {
187 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
188 dev_dbg(&desc->intf->dev,
189 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
190 dr->wIndex, dr->wLength);
193 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
195 dev_dbg(&desc->intf->dev,
196 "NOTIFY_NETWORK_CONNECTION %s network",
197 dr->wValue ? "connected to" : "disconnected from");
200 clear_bit(WDM_POLL_RUNNING, &desc->flags);
201 err("unknown notification %d received: index %d len %d",
202 dr->bNotificationType, dr->wIndex, dr->wLength);
206 req->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
207 req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
209 req->wIndex = desc->inum;
210 req->wLength = cpu_to_le16(desc->wMaxCommand);
212 usb_fill_control_urb(
214 interface_to_usbdev(desc->intf),
215 /* using common endpoint 0 */
216 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
217 (unsigned char *)req,
223 desc->response->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
224 spin_lock(&desc->iuspin);
225 clear_bit(WDM_READ, &desc->flags);
226 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
227 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
228 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
231 spin_unlock(&desc->iuspin);
237 rv = schedule_work(&desc->rxwork);
239 err("Cannot schedule work");
243 rv = usb_submit_urb(urb, GFP_ATOMIC);
245 err("%s - usb_submit_urb failed with result %d",
250 static void kill_urbs(struct wdm_device *desc)
252 /* the order here is essential */
253 usb_kill_urb(desc->command);
254 usb_kill_urb(desc->validity);
255 usb_kill_urb(desc->response);
258 static void free_urbs(struct wdm_device *desc)
260 usb_free_urb(desc->validity);
261 usb_free_urb(desc->response);
262 usb_free_urb(desc->command);
265 static void cleanup(struct wdm_device *desc)
267 usb_buffer_free(interface_to_usbdev(desc->intf),
268 desc->wMaxPacketSize,
270 desc->validity->transfer_dma);
271 usb_buffer_free(interface_to_usbdev(desc->intf),
274 desc->response->transfer_dma);
282 static ssize_t wdm_write
283 (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
286 int rv = -EMSGSIZE, r, we;
287 struct wdm_device *desc = file->private_data;
288 struct usb_ctrlrequest *req;
290 if (count > desc->wMaxCommand)
291 count = desc->wMaxCommand;
293 spin_lock_irq(&desc->iuspin);
296 spin_unlock_irq(&desc->iuspin);
300 r = mutex_lock_interruptible(&desc->wlock); /* concurrent writes */
305 r = usb_autopm_get_interface(desc->intf);
308 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
313 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
318 desc->outbuf = buf = kmalloc(count, GFP_KERNEL);
324 r = copy_from_user(buf, buffer, count);
332 usb_fill_control_urb(
334 interface_to_usbdev(desc->intf),
335 /* using common endpoint 0 */
336 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
337 (unsigned char *)req,
344 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
345 USB_RECIP_INTERFACE);
346 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
348 req->wIndex = desc->inum;
349 req->wLength = cpu_to_le16(count);
350 set_bit(WDM_IN_USE, &desc->flags);
352 rv = usb_submit_urb(desc->command, GFP_KERNEL);
355 clear_bit(WDM_IN_USE, &desc->flags);
356 err("Tx URB error: %d", rv);
358 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
362 usb_autopm_put_interface(desc->intf);
364 mutex_unlock(&desc->wlock);
366 return rv < 0 ? rv : count;
369 static ssize_t wdm_read
370 (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
374 struct wdm_device *desc = file->private_data;
377 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
381 if (desc->length == 0) {
385 rv = wait_event_interruptible(desc->wait,
386 test_bit(WDM_READ, &desc->flags));
388 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
392 usb_mark_last_busy(interface_to_usbdev(desc->intf));
398 spin_lock_irq(&desc->iuspin);
400 if (desc->rerr) { /* read completed, error happened */
403 spin_unlock_irq(&desc->iuspin);
404 err("reading had resulted in %d", t);
409 * recheck whether we've lost the race
410 * against the completion handler
412 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
413 spin_unlock_irq(&desc->iuspin);
416 if (!desc->reslength) { /* zero length read */
417 spin_unlock_irq(&desc->iuspin);
420 clear_bit(WDM_READ, &desc->flags);
421 spin_unlock_irq(&desc->iuspin);
424 cntr = count > desc->length ? desc->length : count;
425 rv = copy_to_user(buffer, desc->ubuf, cntr);
431 for (i = 0; i < desc->length - cntr; i++)
432 desc->ubuf[i] = desc->ubuf[i + cntr];
434 desc->length -= cntr;
435 /* in case we had outstanding data */
437 clear_bit(WDM_READ, &desc->flags);
441 mutex_unlock(&desc->rlock);
443 err("wdm_read: exit error");
447 static int wdm_flush(struct file *file, fl_owner_t id)
449 struct wdm_device *desc = file->private_data;
451 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
453 err("Error in flush path: %d", desc->werr);
458 static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
460 struct wdm_device *desc = file->private_data;
462 unsigned int mask = 0;
464 spin_lock_irqsave(&desc->iuspin, flags);
465 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
467 spin_unlock_irqrestore(&desc->iuspin, flags);
470 if (test_bit(WDM_READ, &desc->flags))
471 mask = POLLIN | POLLRDNORM;
472 if (desc->rerr || desc->werr)
474 if (!test_bit(WDM_IN_USE, &desc->flags))
475 mask |= POLLOUT | POLLWRNORM;
476 spin_unlock_irqrestore(&desc->iuspin, flags);
478 poll_wait(file, &desc->wait, wait);
484 static int wdm_open(struct inode *inode, struct file *file)
486 int minor = iminor(inode);
488 struct usb_interface *intf;
489 struct wdm_device *desc;
491 mutex_lock(&wdm_mutex);
492 intf = usb_find_interface(&wdm_driver, minor);
496 desc = usb_get_intfdata(intf);
497 if (test_bit(WDM_DISCONNECTING, &desc->flags))
501 file->private_data = desc;
503 rv = usb_autopm_get_interface(desc->intf);
505 err("Error autopm - %d", rv);
508 intf->needs_remote_wakeup = 1;
510 mutex_lock(&desc->plock);
511 if (!desc->count++) {
512 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
515 err("Error submitting int urb - %d", rv);
520 mutex_unlock(&desc->plock);
521 usb_autopm_put_interface(desc->intf);
523 mutex_unlock(&wdm_mutex);
527 static int wdm_release(struct inode *inode, struct file *file)
529 struct wdm_device *desc = file->private_data;
531 mutex_lock(&wdm_mutex);
532 mutex_lock(&desc->plock);
534 mutex_unlock(&desc->plock);
537 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
539 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
540 desc->intf->needs_remote_wakeup = 0;
542 mutex_unlock(&wdm_mutex);
546 static const struct file_operations wdm_fops = {
547 .owner = THIS_MODULE,
552 .release = wdm_release,
556 static struct usb_class_driver wdm_class = {
559 .minor_base = WDM_MINOR_BASE,
562 /* --- error handling --- */
563 static void wdm_rxwork(struct work_struct *work)
565 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
569 spin_lock_irqsave(&desc->iuspin, flags);
570 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
571 spin_unlock_irqrestore(&desc->iuspin, flags);
573 spin_unlock_irqrestore(&desc->iuspin, flags);
574 rv = usb_submit_urb(desc->response, GFP_KERNEL);
575 if (rv < 0 && rv != -EPERM) {
576 spin_lock_irqsave(&desc->iuspin, flags);
577 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
578 schedule_work(&desc->rxwork);
579 spin_unlock_irqrestore(&desc->iuspin, flags);
584 /* --- hotplug --- */
586 static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
589 struct usb_device *udev = interface_to_usbdev(intf);
590 struct wdm_device *desc;
591 struct usb_host_interface *iface;
592 struct usb_endpoint_descriptor *ep;
593 struct usb_cdc_dmm_desc *dmhd;
594 u8 *buffer = intf->altsetting->extra;
595 int buflen = intf->altsetting->extralen;
602 if (buffer [1] != USB_DT_CS_INTERFACE) {
603 err("skipping garbage");
607 switch (buffer [2]) {
608 case USB_CDC_HEADER_TYPE:
610 case USB_CDC_DMM_TYPE:
611 dmhd = (struct usb_cdc_dmm_desc *)buffer;
612 maxcom = le16_to_cpu(dmhd->wMaxCommand);
614 "Finding maximum buffer length: %d", maxcom);
617 err("Ignoring extra header, type %d, length %d",
618 buffer[2], buffer[0]);
627 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
630 mutex_init(&desc->wlock);
631 mutex_init(&desc->rlock);
632 mutex_init(&desc->plock);
633 spin_lock_init(&desc->iuspin);
634 init_waitqueue_head(&desc->wait);
635 desc->wMaxCommand = maxcom;
636 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
638 INIT_WORK(&desc->rxwork, wdm_rxwork);
640 iface = &intf->altsetting[0];
641 ep = &iface->endpoint[0].desc;
642 if (!usb_endpoint_is_int_in(ep)) {
647 desc->wMaxPacketSize = le16_to_cpu(ep->wMaxPacketSize);
648 desc->bMaxPacketSize0 = udev->descriptor.bMaxPacketSize0;
650 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
653 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
657 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
661 desc->response = usb_alloc_urb(0, GFP_KERNEL);
665 desc->command = usb_alloc_urb(0, GFP_KERNEL);
669 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
673 desc->sbuf = usb_buffer_alloc(interface_to_usbdev(intf),
674 desc->wMaxPacketSize,
676 &desc->validity->transfer_dma);
680 desc->inbuf = usb_buffer_alloc(interface_to_usbdev(intf),
681 desc->bMaxPacketSize0,
683 &desc->response->transfer_dma);
689 interface_to_usbdev(intf),
690 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
692 desc->wMaxPacketSize,
697 desc->validity->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
699 usb_set_intfdata(intf, desc);
700 rv = usb_register_dev(intf, &wdm_class);
701 dev_info(&intf->dev, "cdc-wdm%d: USB WDM device\n",
702 intf->minor - WDM_MINOR_BASE);
708 usb_buffer_free(interface_to_usbdev(desc->intf),
709 desc->wMaxPacketSize,
711 desc->validity->transfer_dma);
721 static void wdm_disconnect(struct usb_interface *intf)
723 struct wdm_device *desc;
726 usb_deregister_dev(intf, &wdm_class);
727 mutex_lock(&wdm_mutex);
728 desc = usb_get_intfdata(intf);
730 /* the spinlock makes sure no new urbs are generated in the callbacks */
731 spin_lock_irqsave(&desc->iuspin, flags);
732 set_bit(WDM_DISCONNECTING, &desc->flags);
733 set_bit(WDM_READ, &desc->flags);
734 /* to terminate pending flushes */
735 clear_bit(WDM_IN_USE, &desc->flags);
736 spin_unlock_irqrestore(&desc->iuspin, flags);
737 cancel_work_sync(&desc->rxwork);
739 wake_up_all(&desc->wait);
742 mutex_unlock(&wdm_mutex);
745 static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
747 struct wdm_device *desc = usb_get_intfdata(intf);
750 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
752 mutex_lock(&desc->plock);
754 if (interface_to_usbdev(desc->intf)->auto_pm && test_bit(WDM_IN_USE, &desc->flags)) {
758 cancel_work_sync(&desc->rxwork);
763 mutex_unlock(&desc->plock);
768 static int recover_from_urb_loss(struct wdm_device *desc)
773 rv = usb_submit_urb(desc->validity, GFP_NOIO);
775 err("Error resume submitting int urb - %d", rv);
779 static int wdm_resume(struct usb_interface *intf)
781 struct wdm_device *desc = usb_get_intfdata(intf);
784 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
785 mutex_lock(&desc->plock);
786 rv = recover_from_urb_loss(desc);
787 mutex_unlock(&desc->plock);
791 static int wdm_pre_reset(struct usb_interface *intf)
793 struct wdm_device *desc = usb_get_intfdata(intf);
795 mutex_lock(&desc->plock);
799 static int wdm_post_reset(struct usb_interface *intf)
801 struct wdm_device *desc = usb_get_intfdata(intf);
804 rv = recover_from_urb_loss(desc);
805 mutex_unlock(&desc->plock);
809 static struct usb_driver wdm_driver = {
812 .disconnect = wdm_disconnect,
813 .suspend = wdm_suspend,
814 .resume = wdm_resume,
815 .reset_resume = wdm_resume,
816 .pre_reset = wdm_pre_reset,
817 .post_reset = wdm_post_reset,
819 .supports_autosuspend = 1,
822 /* --- low level module stuff --- */
824 static int __init wdm_init(void)
828 rv = usb_register(&wdm_driver);
833 static void __exit wdm_exit(void)
835 usb_deregister(&wdm_driver);
838 module_init(wdm_init);
839 module_exit(wdm_exit);
841 MODULE_AUTHOR(DRIVER_AUTHOR);
842 MODULE_DESCRIPTION(DRIVER_DESC);
843 MODULE_LICENSE("GPL");