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
);
317 if (!file
->f_flags
&& O_NONBLOCK
)
318 r
= wait_event_interruptible(desc
->wait
, !test_bit(WDM_IN_USE
,
321 if (test_bit(WDM_IN_USE
, &desc
->flags
))
326 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
331 desc
->outbuf
= buf
= kmalloc(count
, GFP_KERNEL
);
337 r
= copy_from_user(buf
, buffer
, count
);
345 usb_fill_control_urb(
347 interface_to_usbdev(desc
->intf
),
348 /* using common endpoint 0 */
349 usb_sndctrlpipe(interface_to_usbdev(desc
->intf
), 0),
350 (unsigned char *)req
,
357 req
->bRequestType
= (USB_DIR_OUT
| USB_TYPE_CLASS
|
358 USB_RECIP_INTERFACE
);
359 req
->bRequest
= USB_CDC_SEND_ENCAPSULATED_COMMAND
;
361 req
->wIndex
= desc
->inum
;
362 req
->wLength
= cpu_to_le16(count
);
363 set_bit(WDM_IN_USE
, &desc
->flags
);
365 rv
= usb_submit_urb(desc
->command
, GFP_KERNEL
);
368 clear_bit(WDM_IN_USE
, &desc
->flags
);
369 dev_err(&desc
->intf
->dev
, "Tx URB error: %d\n", rv
);
371 dev_dbg(&desc
->intf
->dev
, "Tx URB has been submitted index=%d",
375 usb_autopm_put_interface(desc
->intf
);
377 mutex_unlock(&desc
->wlock
);
379 return rv
< 0 ? rv
: count
;
382 static ssize_t wdm_read
383 (struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
387 struct wdm_device
*desc
= file
->private_data
;
390 rv
= mutex_lock_interruptible(&desc
->rlock
); /*concurrent reads */
394 if (desc
->length
== 0) {
397 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
402 if (file
->f_flags
& O_NONBLOCK
) {
403 if (!test_bit(WDM_READ
, &desc
->flags
)) {
404 rv
= cntr
? cntr
: -EAGAIN
;
409 rv
= wait_event_interruptible(desc
->wait
,
410 test_bit(WDM_READ
, &desc
->flags
));
413 /* may have happened while we slept */
414 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
418 usb_mark_last_busy(interface_to_usbdev(desc
->intf
));
424 spin_lock_irq(&desc
->iuspin
);
426 if (desc
->rerr
) { /* read completed, error happened */
429 spin_unlock_irq(&desc
->iuspin
);
430 dev_err(&desc
->intf
->dev
,
431 "reading had resulted in %d\n", t
);
436 * recheck whether we've lost the race
437 * against the completion handler
439 if (!test_bit(WDM_READ
, &desc
->flags
)) { /* lost race */
440 spin_unlock_irq(&desc
->iuspin
);
443 if (!desc
->reslength
) { /* zero length read */
444 spin_unlock_irq(&desc
->iuspin
);
447 clear_bit(WDM_READ
, &desc
->flags
);
448 spin_unlock_irq(&desc
->iuspin
);
451 cntr
= count
> desc
->length
? desc
->length
: count
;
452 rv
= copy_to_user(buffer
, desc
->ubuf
, cntr
);
458 for (i
= 0; i
< desc
->length
- cntr
; i
++)
459 desc
->ubuf
[i
] = desc
->ubuf
[i
+ cntr
];
461 desc
->length
-= cntr
;
462 /* in case we had outstanding data */
464 clear_bit(WDM_READ
, &desc
->flags
);
468 mutex_unlock(&desc
->rlock
);
469 if (rv
< 0 && rv
!= -EAGAIN
)
470 dev_err(&desc
->intf
->dev
, "wdm_read: exit error\n");
474 static int wdm_flush(struct file
*file
, fl_owner_t id
)
476 struct wdm_device
*desc
= file
->private_data
;
478 wait_event(desc
->wait
, !test_bit(WDM_IN_USE
, &desc
->flags
));
480 dev_err(&desc
->intf
->dev
, "Error in flush path: %d\n",
486 static unsigned int wdm_poll(struct file
*file
, struct poll_table_struct
*wait
)
488 struct wdm_device
*desc
= file
->private_data
;
490 unsigned int mask
= 0;
492 spin_lock_irqsave(&desc
->iuspin
, flags
);
493 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
495 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
498 if (test_bit(WDM_READ
, &desc
->flags
))
499 mask
= POLLIN
| POLLRDNORM
;
500 if (desc
->rerr
|| desc
->werr
)
502 if (!test_bit(WDM_IN_USE
, &desc
->flags
))
503 mask
|= POLLOUT
| POLLWRNORM
;
504 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
506 poll_wait(file
, &desc
->wait
, wait
);
512 static int wdm_open(struct inode
*inode
, struct file
*file
)
514 int minor
= iminor(inode
);
516 struct usb_interface
*intf
;
517 struct wdm_device
*desc
;
519 mutex_lock(&wdm_mutex
);
520 intf
= usb_find_interface(&wdm_driver
, minor
);
524 desc
= usb_get_intfdata(intf
);
525 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
))
527 file
->private_data
= desc
;
529 rv
= usb_autopm_get_interface(desc
->intf
);
531 dev_err(&desc
->intf
->dev
, "Error autopm - %d\n", rv
);
534 intf
->needs_remote_wakeup
= 1;
536 mutex_lock(&desc
->plock
);
537 if (!desc
->count
++) {
538 rv
= usb_submit_urb(desc
->validity
, GFP_KERNEL
);
541 dev_err(&desc
->intf
->dev
,
542 "Error submitting int urb - %d\n", rv
);
547 mutex_unlock(&desc
->plock
);
548 usb_autopm_put_interface(desc
->intf
);
550 mutex_unlock(&wdm_mutex
);
554 static int wdm_release(struct inode
*inode
, struct file
*file
)
556 struct wdm_device
*desc
= file
->private_data
;
558 mutex_lock(&wdm_mutex
);
559 mutex_lock(&desc
->plock
);
561 mutex_unlock(&desc
->plock
);
564 dev_dbg(&desc
->intf
->dev
, "wdm_release: cleanup");
566 if (!test_bit(WDM_DISCONNECTING
, &desc
->flags
))
567 desc
->intf
->needs_remote_wakeup
= 0;
569 mutex_unlock(&wdm_mutex
);
573 static const struct file_operations wdm_fops
= {
574 .owner
= THIS_MODULE
,
579 .release
= wdm_release
,
583 static struct usb_class_driver wdm_class
= {
586 .minor_base
= WDM_MINOR_BASE
,
589 /* --- error handling --- */
590 static void wdm_rxwork(struct work_struct
*work
)
592 struct wdm_device
*desc
= container_of(work
, struct wdm_device
, rxwork
);
596 spin_lock_irqsave(&desc
->iuspin
, flags
);
597 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
598 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
600 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
601 rv
= usb_submit_urb(desc
->response
, GFP_KERNEL
);
602 if (rv
< 0 && rv
!= -EPERM
) {
603 spin_lock_irqsave(&desc
->iuspin
, flags
);
604 if (!test_bit(WDM_DISCONNECTING
, &desc
->flags
))
605 schedule_work(&desc
->rxwork
);
606 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
611 /* --- hotplug --- */
613 static int wdm_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
616 struct usb_device
*udev
= interface_to_usbdev(intf
);
617 struct wdm_device
*desc
;
618 struct usb_host_interface
*iface
;
619 struct usb_endpoint_descriptor
*ep
;
620 struct usb_cdc_dmm_desc
*dmhd
;
621 u8
*buffer
= intf
->altsetting
->extra
;
622 int buflen
= intf
->altsetting
->extralen
;
629 if (buffer
[1] != USB_DT_CS_INTERFACE
) {
630 dev_err(&intf
->dev
, "skipping garbage\n");
634 switch (buffer
[2]) {
635 case USB_CDC_HEADER_TYPE
:
637 case USB_CDC_DMM_TYPE
:
638 dmhd
= (struct usb_cdc_dmm_desc
*)buffer
;
639 maxcom
= le16_to_cpu(dmhd
->wMaxCommand
);
641 "Finding maximum buffer length: %d", maxcom
);
645 "Ignoring extra header, type %d, length %d\n",
646 buffer
[2], buffer
[0]);
655 desc
= kzalloc(sizeof(struct wdm_device
), GFP_KERNEL
);
658 mutex_init(&desc
->wlock
);
659 mutex_init(&desc
->rlock
);
660 mutex_init(&desc
->plock
);
661 spin_lock_init(&desc
->iuspin
);
662 init_waitqueue_head(&desc
->wait
);
663 desc
->wMaxCommand
= maxcom
;
664 /* this will be expanded and needed in hardware endianness */
665 desc
->inum
= cpu_to_le16((u16
)intf
->cur_altsetting
->desc
.bInterfaceNumber
);
667 INIT_WORK(&desc
->rxwork
, wdm_rxwork
);
670 iface
= intf
->cur_altsetting
;
671 if (iface
->desc
.bNumEndpoints
!= 1)
673 ep
= &iface
->endpoint
[0].desc
;
674 if (!ep
|| !usb_endpoint_is_int_in(ep
))
677 desc
->wMaxPacketSize
= le16_to_cpu(ep
->wMaxPacketSize
);
678 desc
->bMaxPacketSize0
= udev
->descriptor
.bMaxPacketSize0
;
680 desc
->orq
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_KERNEL
);
683 desc
->irq
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_KERNEL
);
687 desc
->validity
= usb_alloc_urb(0, GFP_KERNEL
);
691 desc
->response
= usb_alloc_urb(0, GFP_KERNEL
);
695 desc
->command
= usb_alloc_urb(0, GFP_KERNEL
);
699 desc
->ubuf
= kmalloc(desc
->wMaxCommand
, GFP_KERNEL
);
703 desc
->sbuf
= usb_buffer_alloc(interface_to_usbdev(intf
),
704 desc
->wMaxPacketSize
,
706 &desc
->validity
->transfer_dma
);
710 desc
->inbuf
= usb_buffer_alloc(interface_to_usbdev(intf
),
711 desc
->bMaxPacketSize0
,
713 &desc
->response
->transfer_dma
);
719 interface_to_usbdev(intf
),
720 usb_rcvintpipe(interface_to_usbdev(intf
), ep
->bEndpointAddress
),
722 desc
->wMaxPacketSize
,
727 desc
->validity
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
729 usb_set_intfdata(intf
, desc
);
730 rv
= usb_register_dev(intf
, &wdm_class
);
734 dev_info(&intf
->dev
, "cdc-wdm%d: USB WDM device\n",
735 intf
->minor
- WDM_MINOR_BASE
);
739 usb_set_intfdata(intf
, NULL
);
740 usb_buffer_free(interface_to_usbdev(desc
->intf
),
741 desc
->bMaxPacketSize0
,
743 desc
->response
->transfer_dma
);
745 usb_buffer_free(interface_to_usbdev(desc
->intf
),
746 desc
->wMaxPacketSize
,
748 desc
->validity
->transfer_dma
);
758 static void wdm_disconnect(struct usb_interface
*intf
)
760 struct wdm_device
*desc
;
763 usb_deregister_dev(intf
, &wdm_class
);
764 mutex_lock(&wdm_mutex
);
765 desc
= usb_get_intfdata(intf
);
767 /* the spinlock makes sure no new urbs are generated in the callbacks */
768 spin_lock_irqsave(&desc
->iuspin
, flags
);
769 set_bit(WDM_DISCONNECTING
, &desc
->flags
);
770 set_bit(WDM_READ
, &desc
->flags
);
771 /* to terminate pending flushes */
772 clear_bit(WDM_IN_USE
, &desc
->flags
);
773 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
774 cancel_work_sync(&desc
->rxwork
);
776 wake_up_all(&desc
->wait
);
779 mutex_unlock(&wdm_mutex
);
782 static int wdm_suspend(struct usb_interface
*intf
, pm_message_t message
)
784 struct wdm_device
*desc
= usb_get_intfdata(intf
);
787 dev_dbg(&desc
->intf
->dev
, "wdm%d_suspend\n", intf
->minor
);
789 mutex_lock(&desc
->plock
);
791 if ((message
.event
& PM_EVENT_AUTO
) &&
792 test_bit(WDM_IN_USE
, &desc
->flags
)) {
796 cancel_work_sync(&desc
->rxwork
);
801 mutex_unlock(&desc
->plock
);
806 static int recover_from_urb_loss(struct wdm_device
*desc
)
811 rv
= usb_submit_urb(desc
->validity
, GFP_NOIO
);
813 dev_err(&desc
->intf
->dev
,
814 "Error resume submitting int urb - %d\n", rv
);
818 static int wdm_resume(struct usb_interface
*intf
)
820 struct wdm_device
*desc
= usb_get_intfdata(intf
);
823 dev_dbg(&desc
->intf
->dev
, "wdm%d_resume\n", intf
->minor
);
824 mutex_lock(&desc
->plock
);
825 rv
= recover_from_urb_loss(desc
);
826 mutex_unlock(&desc
->plock
);
830 static int wdm_pre_reset(struct usb_interface
*intf
)
832 struct wdm_device
*desc
= usb_get_intfdata(intf
);
834 mutex_lock(&desc
->plock
);
838 static int wdm_post_reset(struct usb_interface
*intf
)
840 struct wdm_device
*desc
= usb_get_intfdata(intf
);
843 rv
= recover_from_urb_loss(desc
);
844 mutex_unlock(&desc
->plock
);
848 static struct usb_driver wdm_driver
= {
851 .disconnect
= wdm_disconnect
,
852 .suspend
= wdm_suspend
,
853 .resume
= wdm_resume
,
854 .reset_resume
= wdm_resume
,
855 .pre_reset
= wdm_pre_reset
,
856 .post_reset
= wdm_post_reset
,
858 .supports_autosuspend
= 1,
861 /* --- low level module stuff --- */
863 static int __init
wdm_init(void)
867 rv
= usb_register(&wdm_driver
);
872 static void __exit
wdm_exit(void)
874 usb_deregister(&wdm_driver
);
877 module_init(wdm_init
);
878 module_exit(wdm_exit
);
880 MODULE_AUTHOR(DRIVER_AUTHOR
);
881 MODULE_DESCRIPTION(DRIVER_DESC
);
882 MODULE_LICENSE("GPL");