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.02"
32 #define DRIVER_AUTHOR "Oliver Neukum"
34 static struct usb_device_id wdm_ids[] = {
36 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
37 USB_DEVICE_ID_MATCH_INT_SUBCLASS,
38 .bInterfaceClass = USB_CLASS_COMM,
39 .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM
44 #define WDM_MINOR_BASE 176
48 #define WDM_DISCONNECTING 2
51 #define WDM_INT_STALL 5
52 #define WDM_POLL_RUNNING 6
58 static DEFINE_MUTEX(wdm_mutex);
60 /* --- method tables --- */
63 u8 *inbuf; /* buffer for response */
64 u8 *outbuf; /* buffer for command */
65 u8 *sbuf; /* buffer for status */
66 u8 *ubuf; /* buffer for copy to user space */
71 struct usb_interface *intf;
72 struct usb_ctrlrequest *orq;
73 struct usb_ctrlrequest *irq;
90 wait_queue_head_t wait;
91 struct work_struct rxwork;
96 static struct usb_driver wdm_driver;
98 /* --- callbacks --- */
99 static void wdm_out_callback(struct urb *urb)
101 struct wdm_device *desc;
103 spin_lock(&desc->iuspin);
104 desc->werr = urb->status;
105 spin_unlock(&desc->iuspin);
106 clear_bit(WDM_IN_USE, &desc->flags);
108 wake_up(&desc->wait);
111 static void wdm_in_callback(struct urb *urb)
113 struct wdm_device *desc = urb->context;
114 int status = urb->status;
116 spin_lock(&desc->iuspin);
121 dev_dbg(&desc->intf->dev,
122 "nonzero urb status received: -ENOENT");
125 dev_dbg(&desc->intf->dev,
126 "nonzero urb status received: -ECONNRESET");
129 dev_dbg(&desc->intf->dev,
130 "nonzero urb status received: -ESHUTDOWN");
133 err("nonzero urb status received: -EPIPE");
136 err("Unexpected error %d", status);
142 desc->reslength = urb->actual_length;
143 memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength);
144 desc->length += desc->reslength;
145 wake_up(&desc->wait);
147 set_bit(WDM_READ, &desc->flags);
148 spin_unlock(&desc->iuspin);
151 static void wdm_int_callback(struct urb *urb)
154 int status = urb->status;
155 struct wdm_device *desc;
156 struct usb_ctrlrequest *req;
157 struct usb_cdc_notification *dr;
161 dr = (struct usb_cdc_notification *)desc->sbuf;
170 set_bit(WDM_INT_STALL, &desc->flags);
171 err("Stall on int endpoint");
172 goto sw; /* halt is cleared in work */
174 err("nonzero urb status received: %d", status);
179 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
180 err("wdm_int_callback - %d bytes", urb->actual_length);
184 switch (dr->bNotificationType) {
185 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
186 dev_dbg(&desc->intf->dev,
187 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
188 dr->wIndex, dr->wLength);
191 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
193 dev_dbg(&desc->intf->dev,
194 "NOTIFY_NETWORK_CONNECTION %s network",
195 dr->wValue ? "connected to" : "disconnected from");
198 clear_bit(WDM_POLL_RUNNING, &desc->flags);
199 err("unknown notification %d received: index %d len %d",
200 dr->bNotificationType, dr->wIndex, dr->wLength);
204 req->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
205 req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
207 req->wIndex = desc->inum;
208 req->wLength = cpu_to_le16(desc->bMaxPacketSize0);
210 usb_fill_control_urb(
212 interface_to_usbdev(desc->intf),
213 /* using common endpoint 0 */
214 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
215 (unsigned char *)req,
217 desc->bMaxPacketSize0,
221 desc->response->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
222 spin_lock(&desc->iuspin);
223 clear_bit(WDM_READ, &desc->flags);
224 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
225 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
226 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
229 spin_unlock(&desc->iuspin);
235 rv = schedule_work(&desc->rxwork);
237 err("Cannot schedule work");
241 rv = usb_submit_urb(urb, GFP_ATOMIC);
243 err("%s - usb_submit_urb failed with result %d",
248 static void kill_urbs(struct wdm_device *desc)
250 usb_kill_urb(desc->command);
251 usb_kill_urb(desc->validity);
252 usb_kill_urb(desc->response);
255 static void free_urbs(struct wdm_device *desc)
257 usb_free_urb(desc->validity);
258 usb_free_urb(desc->response);
259 usb_free_urb(desc->command);
262 static void cleanup(struct wdm_device *desc)
264 usb_buffer_free(interface_to_usbdev(desc->intf),
265 desc->wMaxPacketSize,
267 desc->validity->transfer_dma);
268 usb_buffer_free(interface_to_usbdev(desc->intf),
269 desc->wMaxPacketSize,
271 desc->response->transfer_dma);
279 static ssize_t wdm_write
280 (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
283 int rv = -EMSGSIZE, r, we;
284 struct wdm_device *desc = file->private_data;
285 struct usb_ctrlrequest *req;
287 if (count > desc->wMaxCommand)
288 count = desc->wMaxCommand;
290 spin_lock_irq(&desc->iuspin);
293 spin_unlock_irq(&desc->iuspin);
297 r = mutex_lock_interruptible(&desc->wlock); /* concurrent writes */
302 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
307 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
312 desc->outbuf = buf = kmalloc(count, GFP_KERNEL);
318 r = copy_from_user(buf, buffer, count);
326 usb_fill_control_urb(
328 interface_to_usbdev(desc->intf),
329 /* using common endpoint 0 */
330 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
331 (unsigned char *)req,
338 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
339 USB_RECIP_INTERFACE);
340 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
342 req->wIndex = desc->inum;
343 req->wLength = cpu_to_le16(count);
344 set_bit(WDM_IN_USE, &desc->flags);
346 rv = usb_submit_urb(desc->command, GFP_KERNEL);
349 clear_bit(WDM_IN_USE, &desc->flags);
351 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
355 mutex_unlock(&desc->wlock);
357 return rv < 0 ? rv : count;
360 static ssize_t wdm_read
361 (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
365 struct wdm_device *desc = file->private_data;
368 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
372 if (desc->length == 0) {
376 rv = wait_event_interruptible(desc->wait,
377 test_bit(WDM_READ, &desc->flags));
384 spin_lock_irq(&desc->iuspin);
386 if (desc->rerr) { /* read completed, error happened */
389 spin_unlock_irq(&desc->iuspin);
390 err("reading had resulted in %d", t);
395 * recheck whether we've lost the race
396 * against the completion handler
398 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
399 spin_unlock_irq(&desc->iuspin);
402 if (!desc->reslength) { /* zero length read */
403 spin_unlock_irq(&desc->iuspin);
406 clear_bit(WDM_READ, &desc->flags);
407 spin_unlock_irq(&desc->iuspin);
410 cntr = count > desc->length ? desc->length : count;
411 rv = copy_to_user(buffer, desc->ubuf, cntr);
417 for (i = 0; i < desc->length - cntr; i++)
418 desc->ubuf[i] = desc->ubuf[i + cntr];
420 desc->length -= cntr;
424 mutex_unlock(&desc->rlock);
426 err("wdm_read: exit error");
430 static int wdm_flush(struct file *file, fl_owner_t id)
432 struct wdm_device *desc = file->private_data;
434 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
436 err("Error in flush path: %d", desc->werr);
441 static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
443 struct wdm_device *desc = file->private_data;
445 unsigned int mask = 0;
447 spin_lock_irqsave(&desc->iuspin, flags);
448 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
450 spin_unlock_irqrestore(&desc->iuspin, flags);
453 if (test_bit(WDM_READ, &desc->flags))
454 mask = POLLIN | POLLRDNORM;
455 if (desc->rerr || desc->werr)
457 if (!test_bit(WDM_IN_USE, &desc->flags))
458 mask |= POLLOUT | POLLWRNORM;
459 spin_unlock_irqrestore(&desc->iuspin, flags);
461 poll_wait(file, &desc->wait, wait);
467 static int wdm_open(struct inode *inode, struct file *file)
469 int minor = iminor(inode);
471 struct usb_interface *intf;
472 struct wdm_device *desc;
474 mutex_lock(&wdm_mutex);
475 intf = usb_find_interface(&wdm_driver, minor);
479 desc = usb_get_intfdata(intf);
480 if (test_bit(WDM_DISCONNECTING, &desc->flags))
484 file->private_data = desc;
486 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
490 err("Error submitting int urb - %d", rv);
496 mutex_unlock(&wdm_mutex);
500 static int wdm_release(struct inode *inode, struct file *file)
502 struct wdm_device *desc = file->private_data;
504 mutex_lock(&wdm_mutex);
507 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
510 mutex_unlock(&wdm_mutex);
514 static const struct file_operations wdm_fops = {
515 .owner = THIS_MODULE,
520 .release = wdm_release,
524 static struct usb_class_driver wdm_class = {
527 .minor_base = WDM_MINOR_BASE,
530 /* --- error handling --- */
531 static void wdm_rxwork(struct work_struct *work)
533 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
537 spin_lock_irqsave(&desc->iuspin, flags);
538 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
539 spin_unlock_irqrestore(&desc->iuspin, flags);
541 spin_unlock_irqrestore(&desc->iuspin, flags);
542 rv = usb_submit_urb(desc->response, GFP_KERNEL);
543 if (rv < 0 && rv != -EPERM) {
544 spin_lock_irqsave(&desc->iuspin, flags);
545 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
546 schedule_work(&desc->rxwork);
547 spin_unlock_irqrestore(&desc->iuspin, flags);
552 /* --- hotplug --- */
554 static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
557 struct usb_device *udev = interface_to_usbdev(intf);
558 struct wdm_device *desc;
559 struct usb_host_interface *iface;
560 struct usb_endpoint_descriptor *ep;
561 struct usb_cdc_dmm_desc *dmhd;
562 u8 *buffer = intf->altsetting->extra;
563 int buflen = intf->altsetting->extralen;
570 if (buffer [1] != USB_DT_CS_INTERFACE) {
571 err("skipping garbage");
575 switch (buffer [2]) {
576 case USB_CDC_HEADER_TYPE:
578 case USB_CDC_DMM_TYPE:
579 dmhd = (struct usb_cdc_dmm_desc *)buffer;
580 maxcom = le16_to_cpu(dmhd->wMaxCommand);
582 "Finding maximum buffer length: %d", maxcom);
585 err("Ignoring extra header, type %d, length %d",
586 buffer[2], buffer[0]);
595 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
598 mutex_init(&desc->wlock);
599 mutex_init(&desc->rlock);
600 spin_lock_init(&desc->iuspin);
601 init_waitqueue_head(&desc->wait);
602 desc->wMaxCommand = maxcom;
603 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
605 INIT_WORK(&desc->rxwork, wdm_rxwork);
607 iface = &intf->altsetting[0];
608 ep = &iface->endpoint[0].desc;
609 if (!usb_endpoint_is_int_in(ep)) {
614 desc->wMaxPacketSize = le16_to_cpu(ep->wMaxPacketSize);
615 desc->bMaxPacketSize0 = udev->descriptor.bMaxPacketSize0;
617 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
620 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
624 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
628 desc->response = usb_alloc_urb(0, GFP_KERNEL);
632 desc->command = usb_alloc_urb(0, GFP_KERNEL);
636 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
640 desc->sbuf = usb_buffer_alloc(interface_to_usbdev(intf),
641 desc->wMaxPacketSize,
643 &desc->validity->transfer_dma);
647 desc->inbuf = usb_buffer_alloc(interface_to_usbdev(intf),
648 desc->bMaxPacketSize0,
650 &desc->response->transfer_dma);
656 interface_to_usbdev(intf),
657 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
659 desc->wMaxPacketSize,
664 desc->validity->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
666 usb_set_intfdata(intf, desc);
667 rv = usb_register_dev(intf, &wdm_class);
668 dev_info(&intf->dev, "cdc-wdm%d: USB WDM device\n",
669 intf->minor - WDM_MINOR_BASE);
675 usb_buffer_free(interface_to_usbdev(desc->intf),
676 desc->wMaxPacketSize,
678 desc->validity->transfer_dma);
688 static void wdm_disconnect(struct usb_interface *intf)
690 struct wdm_device *desc;
693 usb_deregister_dev(intf, &wdm_class);
694 mutex_lock(&wdm_mutex);
695 desc = usb_get_intfdata(intf);
697 /* the spinlock makes sure no new urbs are generated in the callbacks */
698 spin_lock_irqsave(&desc->iuspin, flags);
699 set_bit(WDM_DISCONNECTING, &desc->flags);
700 set_bit(WDM_READ, &desc->flags);
701 clear_bit(WDM_IN_USE, &desc->flags);
702 spin_unlock_irqrestore(&desc->iuspin, flags);
703 cancel_work_sync(&desc->rxwork);
705 wake_up_all(&desc->wait);
708 mutex_unlock(&wdm_mutex);
711 static struct usb_driver wdm_driver = {
714 .disconnect = wdm_disconnect,
718 /* --- low level module stuff --- */
720 static int __init wdm_init(void)
724 rv = usb_register(&wdm_driver);
729 static void __exit wdm_exit(void)
731 usb_deregister(&wdm_driver);
734 module_init(wdm_init);
735 module_exit(wdm_exit);
737 MODULE_AUTHOR(DRIVER_AUTHOR);
738 MODULE_DESCRIPTION("USB Abstract Control Model driver for "
739 "USB WCM Device Management");
740 MODULE_LICENSE("GPL");