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/mutex.h>
19 #include <linux/uaccess.h>
20 #include <linux/bitops.h>
21 #include <linux/poll.h>
22 #include <linux/usb.h>
23 #include <linux/usb/cdc.h>
24 #include <asm/byteorder.h>
25 #include <asm/unaligned.h>
30 #define DRIVER_VERSION "v0.03"
31 #define DRIVER_AUTHOR "Oliver Neukum"
32 #define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management"
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 MODULE_DEVICE_TABLE (usb
, wdm_ids
);
46 #define WDM_MINOR_BASE 176
50 #define WDM_DISCONNECTING 2
53 #define WDM_INT_STALL 5
54 #define WDM_POLL_RUNNING 6
60 static DEFINE_MUTEX(wdm_mutex
);
62 /* --- method tables --- */
65 u8
*inbuf
; /* buffer for response */
66 u8
*outbuf
; /* buffer for command */
67 u8
*sbuf
; /* buffer for status */
68 u8
*ubuf
; /* buffer for copy to user space */
73 struct usb_interface
*intf
;
74 struct usb_ctrlrequest
*orq
;
75 struct usb_ctrlrequest
*irq
;
93 wait_queue_head_t wait
;
94 struct work_struct rxwork
;
99 static struct usb_driver wdm_driver
;
101 /* --- callbacks --- */
102 static void wdm_out_callback(struct urb
*urb
)
104 struct wdm_device
*desc
;
106 spin_lock(&desc
->iuspin
);
107 desc
->werr
= urb
->status
;
108 spin_unlock(&desc
->iuspin
);
109 clear_bit(WDM_IN_USE
, &desc
->flags
);
111 wake_up(&desc
->wait
);
114 static void wdm_in_callback(struct urb
*urb
)
116 struct wdm_device
*desc
= urb
->context
;
117 int status
= urb
->status
;
119 spin_lock(&desc
->iuspin
);
124 dev_dbg(&desc
->intf
->dev
,
125 "nonzero urb status received: -ENOENT");
128 dev_dbg(&desc
->intf
->dev
,
129 "nonzero urb status received: -ECONNRESET");
132 dev_dbg(&desc
->intf
->dev
,
133 "nonzero urb status received: -ESHUTDOWN");
136 dev_err(&desc
->intf
->dev
,
137 "nonzero urb status received: -EPIPE\n");
140 dev_err(&desc
->intf
->dev
,
141 "Unexpected error %d\n", status
);
147 desc
->reslength
= urb
->actual_length
;
148 memmove(desc
->ubuf
+ desc
->length
, desc
->inbuf
, desc
->reslength
);
149 desc
->length
+= desc
->reslength
;
150 wake_up(&desc
->wait
);
152 set_bit(WDM_READ
, &desc
->flags
);
153 spin_unlock(&desc
->iuspin
);
156 static void wdm_int_callback(struct urb
*urb
)
159 int status
= urb
->status
;
160 struct wdm_device
*desc
;
161 struct usb_ctrlrequest
*req
;
162 struct usb_cdc_notification
*dr
;
166 dr
= (struct usb_cdc_notification
*)desc
->sbuf
;
175 set_bit(WDM_INT_STALL
, &desc
->flags
);
176 dev_err(&desc
->intf
->dev
, "Stall on int endpoint\n");
177 goto sw
; /* halt is cleared in work */
179 dev_err(&desc
->intf
->dev
,
180 "nonzero urb status received: %d\n", status
);
185 if (urb
->actual_length
< sizeof(struct usb_cdc_notification
)) {
186 dev_err(&desc
->intf
->dev
, "wdm_int_callback - %d bytes\n",
191 switch (dr
->bNotificationType
) {
192 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE
:
193 dev_dbg(&desc
->intf
->dev
,
194 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
195 dr
->wIndex
, dr
->wLength
);
198 case USB_CDC_NOTIFY_NETWORK_CONNECTION
:
200 dev_dbg(&desc
->intf
->dev
,
201 "NOTIFY_NETWORK_CONNECTION %s network",
202 dr
->wValue
? "connected to" : "disconnected from");
205 clear_bit(WDM_POLL_RUNNING
, &desc
->flags
);
206 dev_err(&desc
->intf
->dev
,
207 "unknown notification %d received: index %d len %d\n",
208 dr
->bNotificationType
, dr
->wIndex
, dr
->wLength
);
212 req
->bRequestType
= (USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
);
213 req
->bRequest
= USB_CDC_GET_ENCAPSULATED_RESPONSE
;
215 req
->wIndex
= desc
->inum
;
216 req
->wLength
= cpu_to_le16(desc
->wMaxCommand
);
218 usb_fill_control_urb(
220 interface_to_usbdev(desc
->intf
),
221 /* using common endpoint 0 */
222 usb_rcvctrlpipe(interface_to_usbdev(desc
->intf
), 0),
223 (unsigned char *)req
,
229 desc
->response
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
230 spin_lock(&desc
->iuspin
);
231 clear_bit(WDM_READ
, &desc
->flags
);
232 if (!test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
233 rv
= usb_submit_urb(desc
->response
, GFP_ATOMIC
);
234 dev_dbg(&desc
->intf
->dev
, "%s: usb_submit_urb %d",
237 spin_unlock(&desc
->iuspin
);
243 rv
= schedule_work(&desc
->rxwork
);
245 dev_err(&desc
->intf
->dev
,
246 "Cannot schedule work\n");
250 rv
= usb_submit_urb(urb
, GFP_ATOMIC
);
252 dev_err(&desc
->intf
->dev
,
253 "%s - usb_submit_urb failed with result %d\n",
258 static void kill_urbs(struct wdm_device
*desc
)
260 /* the order here is essential */
261 usb_kill_urb(desc
->command
);
262 usb_kill_urb(desc
->validity
);
263 usb_kill_urb(desc
->response
);
266 static void free_urbs(struct wdm_device
*desc
)
268 usb_free_urb(desc
->validity
);
269 usb_free_urb(desc
->response
);
270 usb_free_urb(desc
->command
);
273 static void cleanup(struct wdm_device
*desc
)
275 usb_buffer_free(interface_to_usbdev(desc
->intf
),
276 desc
->wMaxPacketSize
,
278 desc
->validity
->transfer_dma
);
279 usb_buffer_free(interface_to_usbdev(desc
->intf
),
282 desc
->response
->transfer_dma
);
290 static ssize_t wdm_write
291 (struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
294 int rv
= -EMSGSIZE
, r
, we
;
295 struct wdm_device
*desc
= file
->private_data
;
296 struct usb_ctrlrequest
*req
;
298 if (count
> desc
->wMaxCommand
)
299 count
= desc
->wMaxCommand
;
301 spin_lock_irq(&desc
->iuspin
);
304 spin_unlock_irq(&desc
->iuspin
);
308 r
= mutex_lock_interruptible(&desc
->wlock
); /* concurrent writes */
313 r
= usb_autopm_get_interface(desc
->intf
);
316 r
= wait_event_interruptible(desc
->wait
, !test_bit(WDM_IN_USE
,
321 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
326 desc
->outbuf
= buf
= kmalloc(count
, GFP_KERNEL
);
332 r
= copy_from_user(buf
, buffer
, count
);
340 usb_fill_control_urb(
342 interface_to_usbdev(desc
->intf
),
343 /* using common endpoint 0 */
344 usb_sndctrlpipe(interface_to_usbdev(desc
->intf
), 0),
345 (unsigned char *)req
,
352 req
->bRequestType
= (USB_DIR_OUT
| USB_TYPE_CLASS
|
353 USB_RECIP_INTERFACE
);
354 req
->bRequest
= USB_CDC_SEND_ENCAPSULATED_COMMAND
;
356 req
->wIndex
= desc
->inum
;
357 req
->wLength
= cpu_to_le16(count
);
358 set_bit(WDM_IN_USE
, &desc
->flags
);
360 rv
= usb_submit_urb(desc
->command
, GFP_KERNEL
);
363 clear_bit(WDM_IN_USE
, &desc
->flags
);
364 dev_err(&desc
->intf
->dev
, "Tx URB error: %d\n", rv
);
366 dev_dbg(&desc
->intf
->dev
, "Tx URB has been submitted index=%d",
370 usb_autopm_put_interface(desc
->intf
);
372 mutex_unlock(&desc
->wlock
);
374 return rv
< 0 ? rv
: count
;
377 static ssize_t wdm_read
378 (struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
382 struct wdm_device
*desc
= file
->private_data
;
385 rv
= mutex_lock_interruptible(&desc
->rlock
); /*concurrent reads */
389 if (desc
->length
== 0) {
393 rv
= wait_event_interruptible(desc
->wait
,
394 test_bit(WDM_READ
, &desc
->flags
));
396 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
400 usb_mark_last_busy(interface_to_usbdev(desc
->intf
));
406 spin_lock_irq(&desc
->iuspin
);
408 if (desc
->rerr
) { /* read completed, error happened */
411 spin_unlock_irq(&desc
->iuspin
);
412 dev_err(&desc
->intf
->dev
,
413 "reading had resulted in %d\n", t
);
418 * recheck whether we've lost the race
419 * against the completion handler
421 if (!test_bit(WDM_READ
, &desc
->flags
)) { /* lost race */
422 spin_unlock_irq(&desc
->iuspin
);
425 if (!desc
->reslength
) { /* zero length read */
426 spin_unlock_irq(&desc
->iuspin
);
429 clear_bit(WDM_READ
, &desc
->flags
);
430 spin_unlock_irq(&desc
->iuspin
);
433 cntr
= count
> desc
->length
? desc
->length
: count
;
434 rv
= copy_to_user(buffer
, desc
->ubuf
, cntr
);
440 for (i
= 0; i
< desc
->length
- cntr
; i
++)
441 desc
->ubuf
[i
] = desc
->ubuf
[i
+ cntr
];
443 desc
->length
-= cntr
;
444 /* in case we had outstanding data */
446 clear_bit(WDM_READ
, &desc
->flags
);
450 mutex_unlock(&desc
->rlock
);
452 dev_err(&desc
->intf
->dev
, "wdm_read: exit error\n");
456 static int wdm_flush(struct file
*file
, fl_owner_t id
)
458 struct wdm_device
*desc
= file
->private_data
;
460 wait_event(desc
->wait
, !test_bit(WDM_IN_USE
, &desc
->flags
));
462 dev_err(&desc
->intf
->dev
, "Error in flush path: %d\n",
468 static unsigned int wdm_poll(struct file
*file
, struct poll_table_struct
*wait
)
470 struct wdm_device
*desc
= file
->private_data
;
472 unsigned int mask
= 0;
474 spin_lock_irqsave(&desc
->iuspin
, flags
);
475 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
477 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
480 if (test_bit(WDM_READ
, &desc
->flags
))
481 mask
= POLLIN
| POLLRDNORM
;
482 if (desc
->rerr
|| desc
->werr
)
484 if (!test_bit(WDM_IN_USE
, &desc
->flags
))
485 mask
|= POLLOUT
| POLLWRNORM
;
486 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
488 poll_wait(file
, &desc
->wait
, wait
);
494 static int wdm_open(struct inode
*inode
, struct file
*file
)
496 int minor
= iminor(inode
);
498 struct usb_interface
*intf
;
499 struct wdm_device
*desc
;
501 mutex_lock(&wdm_mutex
);
502 intf
= usb_find_interface(&wdm_driver
, minor
);
506 desc
= usb_get_intfdata(intf
);
507 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
))
511 file
->private_data
= desc
;
513 rv
= usb_autopm_get_interface(desc
->intf
);
515 dev_err(&desc
->intf
->dev
, "Error autopm - %d\n", rv
);
518 intf
->needs_remote_wakeup
= 1;
520 mutex_lock(&desc
->plock
);
521 if (!desc
->count
++) {
522 rv
= usb_submit_urb(desc
->validity
, GFP_KERNEL
);
525 dev_err(&desc
->intf
->dev
,
526 "Error submitting int urb - %d\n", rv
);
531 mutex_unlock(&desc
->plock
);
532 usb_autopm_put_interface(desc
->intf
);
534 mutex_unlock(&wdm_mutex
);
538 static int wdm_release(struct inode
*inode
, struct file
*file
)
540 struct wdm_device
*desc
= file
->private_data
;
542 mutex_lock(&wdm_mutex
);
543 mutex_lock(&desc
->plock
);
545 mutex_unlock(&desc
->plock
);
548 dev_dbg(&desc
->intf
->dev
, "wdm_release: cleanup");
550 if (!test_bit(WDM_DISCONNECTING
, &desc
->flags
))
551 desc
->intf
->needs_remote_wakeup
= 0;
553 mutex_unlock(&wdm_mutex
);
557 static const struct file_operations wdm_fops
= {
558 .owner
= THIS_MODULE
,
563 .release
= wdm_release
,
567 static struct usb_class_driver wdm_class
= {
570 .minor_base
= WDM_MINOR_BASE
,
573 /* --- error handling --- */
574 static void wdm_rxwork(struct work_struct
*work
)
576 struct wdm_device
*desc
= container_of(work
, struct wdm_device
, rxwork
);
580 spin_lock_irqsave(&desc
->iuspin
, flags
);
581 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
582 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
584 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
585 rv
= usb_submit_urb(desc
->response
, GFP_KERNEL
);
586 if (rv
< 0 && rv
!= -EPERM
) {
587 spin_lock_irqsave(&desc
->iuspin
, flags
);
588 if (!test_bit(WDM_DISCONNECTING
, &desc
->flags
))
589 schedule_work(&desc
->rxwork
);
590 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
595 /* --- hotplug --- */
597 static int wdm_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
600 struct usb_device
*udev
= interface_to_usbdev(intf
);
601 struct wdm_device
*desc
;
602 struct usb_host_interface
*iface
;
603 struct usb_endpoint_descriptor
*ep
;
604 struct usb_cdc_dmm_desc
*dmhd
;
605 u8
*buffer
= intf
->altsetting
->extra
;
606 int buflen
= intf
->altsetting
->extralen
;
613 if (buffer
[1] != USB_DT_CS_INTERFACE
) {
614 dev_err(&intf
->dev
, "skipping garbage\n");
618 switch (buffer
[2]) {
619 case USB_CDC_HEADER_TYPE
:
621 case USB_CDC_DMM_TYPE
:
622 dmhd
= (struct usb_cdc_dmm_desc
*)buffer
;
623 maxcom
= le16_to_cpu(dmhd
->wMaxCommand
);
625 "Finding maximum buffer length: %d", maxcom
);
629 "Ignoring extra header, type %d, length %d\n",
630 buffer
[2], buffer
[0]);
639 desc
= kzalloc(sizeof(struct wdm_device
), GFP_KERNEL
);
642 mutex_init(&desc
->wlock
);
643 mutex_init(&desc
->rlock
);
644 mutex_init(&desc
->plock
);
645 spin_lock_init(&desc
->iuspin
);
646 init_waitqueue_head(&desc
->wait
);
647 desc
->wMaxCommand
= maxcom
;
648 /* this will be expanded and needed in hardware endianness */
649 desc
->inum
= cpu_to_le16((u16
)intf
->cur_altsetting
->desc
.bInterfaceNumber
);
651 INIT_WORK(&desc
->rxwork
, wdm_rxwork
);
654 iface
= intf
->cur_altsetting
;
655 if (iface
->desc
.bNumEndpoints
!= 1)
657 ep
= &iface
->endpoint
[0].desc
;
658 if (!ep
|| !usb_endpoint_is_int_in(ep
))
661 desc
->wMaxPacketSize
= le16_to_cpu(ep
->wMaxPacketSize
);
662 desc
->bMaxPacketSize0
= udev
->descriptor
.bMaxPacketSize0
;
664 desc
->orq
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_KERNEL
);
667 desc
->irq
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_KERNEL
);
671 desc
->validity
= usb_alloc_urb(0, GFP_KERNEL
);
675 desc
->response
= usb_alloc_urb(0, GFP_KERNEL
);
679 desc
->command
= usb_alloc_urb(0, GFP_KERNEL
);
683 desc
->ubuf
= kmalloc(desc
->wMaxCommand
, GFP_KERNEL
);
687 desc
->sbuf
= usb_buffer_alloc(interface_to_usbdev(intf
),
688 desc
->wMaxPacketSize
,
690 &desc
->validity
->transfer_dma
);
694 desc
->inbuf
= usb_buffer_alloc(interface_to_usbdev(intf
),
695 desc
->bMaxPacketSize0
,
697 &desc
->response
->transfer_dma
);
703 interface_to_usbdev(intf
),
704 usb_rcvintpipe(interface_to_usbdev(intf
), ep
->bEndpointAddress
),
706 desc
->wMaxPacketSize
,
711 desc
->validity
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
713 usb_set_intfdata(intf
, desc
);
714 rv
= usb_register_dev(intf
, &wdm_class
);
718 dev_info(&intf
->dev
, "cdc-wdm%d: USB WDM device\n",
719 intf
->minor
- WDM_MINOR_BASE
);
723 usb_set_intfdata(intf
, NULL
);
724 usb_buffer_free(interface_to_usbdev(desc
->intf
),
725 desc
->bMaxPacketSize0
,
727 desc
->response
->transfer_dma
);
729 usb_buffer_free(interface_to_usbdev(desc
->intf
),
730 desc
->wMaxPacketSize
,
732 desc
->validity
->transfer_dma
);
742 static void wdm_disconnect(struct usb_interface
*intf
)
744 struct wdm_device
*desc
;
747 usb_deregister_dev(intf
, &wdm_class
);
748 mutex_lock(&wdm_mutex
);
749 desc
= usb_get_intfdata(intf
);
751 /* the spinlock makes sure no new urbs are generated in the callbacks */
752 spin_lock_irqsave(&desc
->iuspin
, flags
);
753 set_bit(WDM_DISCONNECTING
, &desc
->flags
);
754 set_bit(WDM_READ
, &desc
->flags
);
755 /* to terminate pending flushes */
756 clear_bit(WDM_IN_USE
, &desc
->flags
);
757 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
758 cancel_work_sync(&desc
->rxwork
);
760 wake_up_all(&desc
->wait
);
763 mutex_unlock(&wdm_mutex
);
766 static int wdm_suspend(struct usb_interface
*intf
, pm_message_t message
)
768 struct wdm_device
*desc
= usb_get_intfdata(intf
);
771 dev_dbg(&desc
->intf
->dev
, "wdm%d_suspend\n", intf
->minor
);
773 mutex_lock(&desc
->plock
);
775 if ((message
.event
& PM_EVENT_AUTO
) &&
776 test_bit(WDM_IN_USE
, &desc
->flags
)) {
780 cancel_work_sync(&desc
->rxwork
);
785 mutex_unlock(&desc
->plock
);
790 static int recover_from_urb_loss(struct wdm_device
*desc
)
795 rv
= usb_submit_urb(desc
->validity
, GFP_NOIO
);
797 dev_err(&desc
->intf
->dev
,
798 "Error resume submitting int urb - %d\n", rv
);
802 static int wdm_resume(struct usb_interface
*intf
)
804 struct wdm_device
*desc
= usb_get_intfdata(intf
);
807 dev_dbg(&desc
->intf
->dev
, "wdm%d_resume\n", intf
->minor
);
808 mutex_lock(&desc
->plock
);
809 rv
= recover_from_urb_loss(desc
);
810 mutex_unlock(&desc
->plock
);
814 static int wdm_pre_reset(struct usb_interface
*intf
)
816 struct wdm_device
*desc
= usb_get_intfdata(intf
);
818 mutex_lock(&desc
->plock
);
822 static int wdm_post_reset(struct usb_interface
*intf
)
824 struct wdm_device
*desc
= usb_get_intfdata(intf
);
827 rv
= recover_from_urb_loss(desc
);
828 mutex_unlock(&desc
->plock
);
832 static struct usb_driver wdm_driver
= {
835 .disconnect
= wdm_disconnect
,
836 .suspend
= wdm_suspend
,
837 .resume
= wdm_resume
,
838 .reset_resume
= wdm_resume
,
839 .pre_reset
= wdm_pre_reset
,
840 .post_reset
= wdm_post_reset
,
842 .supports_autosuspend
= 1,
845 /* --- low level module stuff --- */
847 static int __init
wdm_init(void)
851 rv
= usb_register(&wdm_driver
);
856 static void __exit
wdm_exit(void)
858 usb_deregister(&wdm_driver
);
861 module_init(wdm_init
);
862 module_exit(wdm_exit
);
864 MODULE_AUTHOR(DRIVER_AUTHOR
);
865 MODULE_DESCRIPTION(DRIVER_DESC
);
866 MODULE_LICENSE("GPL");