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/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 MODULE_DEVICE_TABLE (usb, wdm_ids);
47 #define WDM_MINOR_BASE 176
51 #define WDM_DISCONNECTING 2
54 #define WDM_INT_STALL 5
55 #define WDM_POLL_RUNNING 6
61 static DEFINE_MUTEX(wdm_mutex);
63 /* --- method tables --- */
66 u8 *inbuf; /* buffer for response */
67 u8 *outbuf; /* buffer for command */
68 u8 *sbuf; /* buffer for status */
69 u8 *ubuf; /* buffer for copy to user space */
74 struct usb_interface *intf;
75 struct usb_ctrlrequest *orq;
76 struct usb_ctrlrequest *irq;
94 wait_queue_head_t wait;
95 struct work_struct rxwork;
100 static struct usb_driver wdm_driver;
102 /* --- callbacks --- */
103 static void wdm_out_callback(struct urb *urb)
105 struct wdm_device *desc;
107 spin_lock(&desc->iuspin);
108 desc->werr = urb->status;
109 spin_unlock(&desc->iuspin);
110 clear_bit(WDM_IN_USE, &desc->flags);
112 wake_up(&desc->wait);
115 static void wdm_in_callback(struct urb *urb)
117 struct wdm_device *desc = urb->context;
118 int status = urb->status;
120 spin_lock(&desc->iuspin);
125 dev_dbg(&desc->intf->dev,
126 "nonzero urb status received: -ENOENT");
129 dev_dbg(&desc->intf->dev,
130 "nonzero urb status received: -ECONNRESET");
133 dev_dbg(&desc->intf->dev,
134 "nonzero urb status received: -ESHUTDOWN");
137 dev_err(&desc->intf->dev,
138 "nonzero urb status received: -EPIPE\n");
141 dev_err(&desc->intf->dev,
142 "Unexpected error %d\n", status);
148 desc->reslength = urb->actual_length;
149 memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength);
150 desc->length += desc->reslength;
151 wake_up(&desc->wait);
153 set_bit(WDM_READ, &desc->flags);
154 spin_unlock(&desc->iuspin);
157 static void wdm_int_callback(struct urb *urb)
160 int status = urb->status;
161 struct wdm_device *desc;
162 struct usb_ctrlrequest *req;
163 struct usb_cdc_notification *dr;
167 dr = (struct usb_cdc_notification *)desc->sbuf;
176 set_bit(WDM_INT_STALL, &desc->flags);
177 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
178 goto sw; /* halt is cleared in work */
180 dev_err(&desc->intf->dev,
181 "nonzero urb status received: %d\n", status);
186 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
187 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
192 switch (dr->bNotificationType) {
193 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
194 dev_dbg(&desc->intf->dev,
195 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
196 dr->wIndex, dr->wLength);
199 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
201 dev_dbg(&desc->intf->dev,
202 "NOTIFY_NETWORK_CONNECTION %s network",
203 dr->wValue ? "connected to" : "disconnected from");
206 clear_bit(WDM_POLL_RUNNING, &desc->flags);
207 dev_err(&desc->intf->dev,
208 "unknown notification %d received: index %d len %d\n",
209 dr->bNotificationType, dr->wIndex, dr->wLength);
213 req->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
214 req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
216 req->wIndex = desc->inum;
217 req->wLength = cpu_to_le16(desc->wMaxCommand);
219 usb_fill_control_urb(
221 interface_to_usbdev(desc->intf),
222 /* using common endpoint 0 */
223 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
224 (unsigned char *)req,
230 desc->response->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
231 spin_lock(&desc->iuspin);
232 clear_bit(WDM_READ, &desc->flags);
233 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
234 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
235 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
238 spin_unlock(&desc->iuspin);
244 rv = schedule_work(&desc->rxwork);
246 dev_err(&desc->intf->dev,
247 "Cannot schedule work\n");
251 rv = usb_submit_urb(urb, GFP_ATOMIC);
253 dev_err(&desc->intf->dev,
254 "%s - usb_submit_urb failed with result %d\n",
259 static void kill_urbs(struct wdm_device *desc)
261 /* the order here is essential */
262 usb_kill_urb(desc->command);
263 usb_kill_urb(desc->validity);
264 usb_kill_urb(desc->response);
267 static void free_urbs(struct wdm_device *desc)
269 usb_free_urb(desc->validity);
270 usb_free_urb(desc->response);
271 usb_free_urb(desc->command);
274 static void cleanup(struct wdm_device *desc)
276 usb_buffer_free(interface_to_usbdev(desc->intf),
277 desc->wMaxPacketSize,
279 desc->validity->transfer_dma);
280 usb_buffer_free(interface_to_usbdev(desc->intf),
283 desc->response->transfer_dma);
291 static ssize_t wdm_write
292 (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
295 int rv = -EMSGSIZE, r, we;
296 struct wdm_device *desc = file->private_data;
297 struct usb_ctrlrequest *req;
299 if (count > desc->wMaxCommand)
300 count = desc->wMaxCommand;
302 spin_lock_irq(&desc->iuspin);
305 spin_unlock_irq(&desc->iuspin);
309 r = mutex_lock_interruptible(&desc->wlock); /* concurrent writes */
314 r = usb_autopm_get_interface(desc->intf);
317 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
322 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
327 desc->outbuf = buf = kmalloc(count, GFP_KERNEL);
333 r = copy_from_user(buf, buffer, count);
341 usb_fill_control_urb(
343 interface_to_usbdev(desc->intf),
344 /* using common endpoint 0 */
345 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
346 (unsigned char *)req,
353 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
354 USB_RECIP_INTERFACE);
355 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
357 req->wIndex = desc->inum;
358 req->wLength = cpu_to_le16(count);
359 set_bit(WDM_IN_USE, &desc->flags);
361 rv = usb_submit_urb(desc->command, GFP_KERNEL);
364 clear_bit(WDM_IN_USE, &desc->flags);
365 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
367 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
371 usb_autopm_put_interface(desc->intf);
373 mutex_unlock(&desc->wlock);
375 return rv < 0 ? rv : count;
378 static ssize_t wdm_read
379 (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
383 struct wdm_device *desc = file->private_data;
386 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
390 if (desc->length == 0) {
394 rv = wait_event_interruptible(desc->wait,
395 test_bit(WDM_READ, &desc->flags));
397 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
401 usb_mark_last_busy(interface_to_usbdev(desc->intf));
407 spin_lock_irq(&desc->iuspin);
409 if (desc->rerr) { /* read completed, error happened */
412 spin_unlock_irq(&desc->iuspin);
413 dev_err(&desc->intf->dev,
414 "reading had resulted in %d\n", t);
419 * recheck whether we've lost the race
420 * against the completion handler
422 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
423 spin_unlock_irq(&desc->iuspin);
426 if (!desc->reslength) { /* zero length read */
427 spin_unlock_irq(&desc->iuspin);
430 clear_bit(WDM_READ, &desc->flags);
431 spin_unlock_irq(&desc->iuspin);
434 cntr = count > desc->length ? desc->length : count;
435 rv = copy_to_user(buffer, desc->ubuf, cntr);
441 for (i = 0; i < desc->length - cntr; i++)
442 desc->ubuf[i] = desc->ubuf[i + cntr];
444 desc->length -= cntr;
445 /* in case we had outstanding data */
447 clear_bit(WDM_READ, &desc->flags);
451 mutex_unlock(&desc->rlock);
453 dev_err(&desc->intf->dev, "wdm_read: exit error\n");
457 static int wdm_flush(struct file *file, fl_owner_t id)
459 struct wdm_device *desc = file->private_data;
461 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
463 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
469 static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
471 struct wdm_device *desc = file->private_data;
473 unsigned int mask = 0;
475 spin_lock_irqsave(&desc->iuspin, flags);
476 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
478 spin_unlock_irqrestore(&desc->iuspin, flags);
481 if (test_bit(WDM_READ, &desc->flags))
482 mask = POLLIN | POLLRDNORM;
483 if (desc->rerr || desc->werr)
485 if (!test_bit(WDM_IN_USE, &desc->flags))
486 mask |= POLLOUT | POLLWRNORM;
487 spin_unlock_irqrestore(&desc->iuspin, flags);
489 poll_wait(file, &desc->wait, wait);
495 static int wdm_open(struct inode *inode, struct file *file)
497 int minor = iminor(inode);
499 struct usb_interface *intf;
500 struct wdm_device *desc;
502 mutex_lock(&wdm_mutex);
503 intf = usb_find_interface(&wdm_driver, minor);
507 desc = usb_get_intfdata(intf);
508 if (test_bit(WDM_DISCONNECTING, &desc->flags))
512 file->private_data = desc;
514 rv = usb_autopm_get_interface(desc->intf);
516 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
519 intf->needs_remote_wakeup = 1;
521 mutex_lock(&desc->plock);
522 if (!desc->count++) {
523 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
526 dev_err(&desc->intf->dev,
527 "Error submitting int urb - %d\n", rv);
532 mutex_unlock(&desc->plock);
533 usb_autopm_put_interface(desc->intf);
535 mutex_unlock(&wdm_mutex);
539 static int wdm_release(struct inode *inode, struct file *file)
541 struct wdm_device *desc = file->private_data;
543 mutex_lock(&wdm_mutex);
544 mutex_lock(&desc->plock);
546 mutex_unlock(&desc->plock);
549 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
551 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
552 desc->intf->needs_remote_wakeup = 0;
554 mutex_unlock(&wdm_mutex);
558 static const struct file_operations wdm_fops = {
559 .owner = THIS_MODULE,
564 .release = wdm_release,
568 static struct usb_class_driver wdm_class = {
571 .minor_base = WDM_MINOR_BASE,
574 /* --- error handling --- */
575 static void wdm_rxwork(struct work_struct *work)
577 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
581 spin_lock_irqsave(&desc->iuspin, flags);
582 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
583 spin_unlock_irqrestore(&desc->iuspin, flags);
585 spin_unlock_irqrestore(&desc->iuspin, flags);
586 rv = usb_submit_urb(desc->response, GFP_KERNEL);
587 if (rv < 0 && rv != -EPERM) {
588 spin_lock_irqsave(&desc->iuspin, flags);
589 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
590 schedule_work(&desc->rxwork);
591 spin_unlock_irqrestore(&desc->iuspin, flags);
596 /* --- hotplug --- */
598 static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
601 struct usb_device *udev = interface_to_usbdev(intf);
602 struct wdm_device *desc;
603 struct usb_host_interface *iface;
604 struct usb_endpoint_descriptor *ep;
605 struct usb_cdc_dmm_desc *dmhd;
606 u8 *buffer = intf->altsetting->extra;
607 int buflen = intf->altsetting->extralen;
614 if (buffer [1] != USB_DT_CS_INTERFACE) {
615 dev_err(&intf->dev, "skipping garbage\n");
619 switch (buffer [2]) {
620 case USB_CDC_HEADER_TYPE:
622 case USB_CDC_DMM_TYPE:
623 dmhd = (struct usb_cdc_dmm_desc *)buffer;
624 maxcom = le16_to_cpu(dmhd->wMaxCommand);
626 "Finding maximum buffer length: %d", maxcom);
630 "Ignoring extra header, type %d, length %d\n",
631 buffer[2], buffer[0]);
640 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
643 mutex_init(&desc->wlock);
644 mutex_init(&desc->rlock);
645 mutex_init(&desc->plock);
646 spin_lock_init(&desc->iuspin);
647 init_waitqueue_head(&desc->wait);
648 desc->wMaxCommand = maxcom;
649 /* this will be expanded and needed in hardware endianness */
650 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
652 INIT_WORK(&desc->rxwork, wdm_rxwork);
655 iface = intf->cur_altsetting;
656 if (iface->desc.bNumEndpoints != 1)
658 ep = &iface->endpoint[0].desc;
659 if (!ep || !usb_endpoint_is_int_in(ep))
662 desc->wMaxPacketSize = le16_to_cpu(ep->wMaxPacketSize);
663 desc->bMaxPacketSize0 = udev->descriptor.bMaxPacketSize0;
665 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
668 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
672 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
676 desc->response = usb_alloc_urb(0, GFP_KERNEL);
680 desc->command = usb_alloc_urb(0, GFP_KERNEL);
684 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
688 desc->sbuf = usb_buffer_alloc(interface_to_usbdev(intf),
689 desc->wMaxPacketSize,
691 &desc->validity->transfer_dma);
695 desc->inbuf = usb_buffer_alloc(interface_to_usbdev(intf),
696 desc->bMaxPacketSize0,
698 &desc->response->transfer_dma);
704 interface_to_usbdev(intf),
705 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
707 desc->wMaxPacketSize,
712 desc->validity->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
714 usb_set_intfdata(intf, desc);
715 rv = usb_register_dev(intf, &wdm_class);
719 dev_info(&intf->dev, "cdc-wdm%d: USB WDM device\n",
720 intf->minor - WDM_MINOR_BASE);
724 usb_set_intfdata(intf, NULL);
725 usb_buffer_free(interface_to_usbdev(desc->intf),
726 desc->bMaxPacketSize0,
728 desc->response->transfer_dma);
730 usb_buffer_free(interface_to_usbdev(desc->intf),
731 desc->wMaxPacketSize,
733 desc->validity->transfer_dma);
743 static void wdm_disconnect(struct usb_interface *intf)
745 struct wdm_device *desc;
748 usb_deregister_dev(intf, &wdm_class);
749 mutex_lock(&wdm_mutex);
750 desc = usb_get_intfdata(intf);
752 /* the spinlock makes sure no new urbs are generated in the callbacks */
753 spin_lock_irqsave(&desc->iuspin, flags);
754 set_bit(WDM_DISCONNECTING, &desc->flags);
755 set_bit(WDM_READ, &desc->flags);
756 /* to terminate pending flushes */
757 clear_bit(WDM_IN_USE, &desc->flags);
758 spin_unlock_irqrestore(&desc->iuspin, flags);
759 cancel_work_sync(&desc->rxwork);
761 wake_up_all(&desc->wait);
764 mutex_unlock(&wdm_mutex);
767 static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
769 struct wdm_device *desc = usb_get_intfdata(intf);
772 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
774 mutex_lock(&desc->plock);
776 if ((message.event & PM_EVENT_AUTO) &&
777 test_bit(WDM_IN_USE, &desc->flags)) {
781 cancel_work_sync(&desc->rxwork);
786 mutex_unlock(&desc->plock);
791 static int recover_from_urb_loss(struct wdm_device *desc)
796 rv = usb_submit_urb(desc->validity, GFP_NOIO);
798 dev_err(&desc->intf->dev,
799 "Error resume submitting int urb - %d\n", rv);
803 static int wdm_resume(struct usb_interface *intf)
805 struct wdm_device *desc = usb_get_intfdata(intf);
808 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
809 mutex_lock(&desc->plock);
810 rv = recover_from_urb_loss(desc);
811 mutex_unlock(&desc->plock);
815 static int wdm_pre_reset(struct usb_interface *intf)
817 struct wdm_device *desc = usb_get_intfdata(intf);
819 mutex_lock(&desc->plock);
823 static int wdm_post_reset(struct usb_interface *intf)
825 struct wdm_device *desc = usb_get_intfdata(intf);
828 rv = recover_from_urb_loss(desc);
829 mutex_unlock(&desc->plock);
833 static struct usb_driver wdm_driver = {
836 .disconnect = wdm_disconnect,
837 .suspend = wdm_suspend,
838 .resume = wdm_resume,
839 .reset_resume = wdm_resume,
840 .pre_reset = wdm_pre_reset,
841 .post_reset = wdm_post_reset,
843 .supports_autosuspend = 1,
846 /* --- low level module stuff --- */
848 static int __init wdm_init(void)
852 rv = usb_register(&wdm_driver);
857 static void __exit wdm_exit(void)
859 usb_deregister(&wdm_driver);
862 module_init(wdm_init);
863 module_exit(wdm_exit);
865 MODULE_AUTHOR(DRIVER_AUTHOR);
866 MODULE_DESCRIPTION(DRIVER_DESC);
867 MODULE_LICENSE("GPL");