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 const 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
55 #define WDM_RESPONDING 7
56 #define WDM_SUSPENDING 8
60 /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
61 #define WDM_DEFAULT_BUFSIZE 256
63 static DEFINE_MUTEX(wdm_mutex
);
65 /* --- method tables --- */
68 u8
*inbuf
; /* buffer for response */
69 u8
*outbuf
; /* buffer for command */
70 u8
*sbuf
; /* buffer for status */
71 u8
*ubuf
; /* buffer for copy to user space */
76 struct usb_interface
*intf
;
77 struct usb_ctrlrequest
*orq
;
78 struct usb_ctrlrequest
*irq
;
95 wait_queue_head_t wait
;
96 struct work_struct rxwork
;
101 static struct usb_driver wdm_driver
;
103 /* --- callbacks --- */
104 static void wdm_out_callback(struct urb
*urb
)
106 struct wdm_device
*desc
;
108 spin_lock(&desc
->iuspin
);
109 desc
->werr
= urb
->status
;
110 spin_unlock(&desc
->iuspin
);
113 clear_bit(WDM_IN_USE
, &desc
->flags
);
114 wake_up(&desc
->wait
);
117 static void wdm_in_callback(struct urb
*urb
)
119 struct wdm_device
*desc
= urb
->context
;
120 int status
= urb
->status
;
122 spin_lock(&desc
->iuspin
);
123 clear_bit(WDM_RESPONDING
, &desc
->flags
);
128 dev_dbg(&desc
->intf
->dev
,
129 "nonzero urb status received: -ENOENT");
132 dev_dbg(&desc
->intf
->dev
,
133 "nonzero urb status received: -ECONNRESET");
136 dev_dbg(&desc
->intf
->dev
,
137 "nonzero urb status received: -ESHUTDOWN");
140 dev_err(&desc
->intf
->dev
,
141 "nonzero urb status received: -EPIPE\n");
144 dev_err(&desc
->intf
->dev
,
145 "Unexpected error %d\n", status
);
151 desc
->reslength
= urb
->actual_length
;
152 memmove(desc
->ubuf
+ desc
->length
, desc
->inbuf
, desc
->reslength
);
153 desc
->length
+= desc
->reslength
;
155 wake_up(&desc
->wait
);
157 set_bit(WDM_READ
, &desc
->flags
);
158 spin_unlock(&desc
->iuspin
);
161 static void wdm_int_callback(struct urb
*urb
)
164 int status
= urb
->status
;
165 struct wdm_device
*desc
;
166 struct usb_ctrlrequest
*req
;
167 struct usb_cdc_notification
*dr
;
171 dr
= (struct usb_cdc_notification
*)desc
->sbuf
;
180 set_bit(WDM_INT_STALL
, &desc
->flags
);
181 dev_err(&desc
->intf
->dev
, "Stall on int endpoint\n");
182 goto sw
; /* halt is cleared in work */
184 dev_err(&desc
->intf
->dev
,
185 "nonzero urb status received: %d\n", status
);
190 if (urb
->actual_length
< sizeof(struct usb_cdc_notification
)) {
191 dev_err(&desc
->intf
->dev
, "wdm_int_callback - %d bytes\n",
196 switch (dr
->bNotificationType
) {
197 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE
:
198 dev_dbg(&desc
->intf
->dev
,
199 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
200 dr
->wIndex
, dr
->wLength
);
203 case USB_CDC_NOTIFY_NETWORK_CONNECTION
:
205 dev_dbg(&desc
->intf
->dev
,
206 "NOTIFY_NETWORK_CONNECTION %s network",
207 dr
->wValue
? "connected to" : "disconnected from");
210 clear_bit(WDM_POLL_RUNNING
, &desc
->flags
);
211 dev_err(&desc
->intf
->dev
,
212 "unknown notification %d received: index %d len %d\n",
213 dr
->bNotificationType
, dr
->wIndex
, dr
->wLength
);
217 req
->bRequestType
= (USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
);
218 req
->bRequest
= USB_CDC_GET_ENCAPSULATED_RESPONSE
;
220 req
->wIndex
= desc
->inum
;
221 req
->wLength
= cpu_to_le16(desc
->wMaxCommand
);
223 usb_fill_control_urb(
225 interface_to_usbdev(desc
->intf
),
226 /* using common endpoint 0 */
227 usb_rcvctrlpipe(interface_to_usbdev(desc
->intf
), 0),
228 (unsigned char *)req
,
234 desc
->response
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
235 spin_lock(&desc
->iuspin
);
236 clear_bit(WDM_READ
, &desc
->flags
);
237 set_bit(WDM_RESPONDING
, &desc
->flags
);
238 if (!test_bit(WDM_DISCONNECTING
, &desc
->flags
)
239 && !test_bit(WDM_SUSPENDING
, &desc
->flags
)) {
240 rv
= usb_submit_urb(desc
->response
, GFP_ATOMIC
);
241 dev_dbg(&desc
->intf
->dev
, "%s: usb_submit_urb %d",
244 spin_unlock(&desc
->iuspin
);
246 clear_bit(WDM_RESPONDING
, &desc
->flags
);
251 rv
= schedule_work(&desc
->rxwork
);
253 dev_err(&desc
->intf
->dev
,
254 "Cannot schedule work\n");
258 rv
= usb_submit_urb(urb
, GFP_ATOMIC
);
260 dev_err(&desc
->intf
->dev
,
261 "%s - usb_submit_urb failed with result %d\n",
266 static void kill_urbs(struct wdm_device
*desc
)
268 /* the order here is essential */
269 usb_kill_urb(desc
->command
);
270 usb_kill_urb(desc
->validity
);
271 usb_kill_urb(desc
->response
);
274 static void free_urbs(struct wdm_device
*desc
)
276 usb_free_urb(desc
->validity
);
277 usb_free_urb(desc
->response
);
278 usb_free_urb(desc
->command
);
281 static void cleanup(struct wdm_device
*desc
)
283 usb_free_coherent(interface_to_usbdev(desc
->intf
),
284 desc
->wMaxPacketSize
,
286 desc
->validity
->transfer_dma
);
287 usb_free_coherent(interface_to_usbdev(desc
->intf
),
288 desc
->bMaxPacketSize0
,
290 desc
->response
->transfer_dma
);
298 static ssize_t wdm_write
299 (struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
302 int rv
= -EMSGSIZE
, r
, we
;
303 struct wdm_device
*desc
= file
->private_data
;
304 struct usb_ctrlrequest
*req
;
306 if (count
> desc
->wMaxCommand
)
307 count
= desc
->wMaxCommand
;
309 spin_lock_irq(&desc
->iuspin
);
312 spin_unlock_irq(&desc
->iuspin
);
316 buf
= kmalloc(count
, GFP_KERNEL
);
322 r
= copy_from_user(buf
, buffer
, count
);
329 /* concurrent writes and disconnect */
330 r
= mutex_lock_interruptible(&desc
->wlock
);
337 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
343 r
= usb_autopm_get_interface(desc
->intf
);
349 if (!(file
->f_flags
& O_NONBLOCK
))
350 r
= wait_event_interruptible(desc
->wait
, !test_bit(WDM_IN_USE
,
353 if (test_bit(WDM_IN_USE
, &desc
->flags
))
361 usb_fill_control_urb(
363 interface_to_usbdev(desc
->intf
),
364 /* using common endpoint 0 */
365 usb_sndctrlpipe(interface_to_usbdev(desc
->intf
), 0),
366 (unsigned char *)req
,
373 req
->bRequestType
= (USB_DIR_OUT
| USB_TYPE_CLASS
|
374 USB_RECIP_INTERFACE
);
375 req
->bRequest
= USB_CDC_SEND_ENCAPSULATED_COMMAND
;
377 req
->wIndex
= desc
->inum
;
378 req
->wLength
= cpu_to_le16(count
);
379 set_bit(WDM_IN_USE
, &desc
->flags
);
382 rv
= usb_submit_urb(desc
->command
, GFP_KERNEL
);
386 clear_bit(WDM_IN_USE
, &desc
->flags
);
387 dev_err(&desc
->intf
->dev
, "Tx URB error: %d\n", rv
);
389 dev_dbg(&desc
->intf
->dev
, "Tx URB has been submitted index=%d",
393 usb_autopm_put_interface(desc
->intf
);
395 mutex_unlock(&desc
->wlock
);
397 return rv
< 0 ? rv
: count
;
400 static ssize_t wdm_read
401 (struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
405 struct wdm_device
*desc
= file
->private_data
;
408 rv
= mutex_lock_interruptible(&desc
->rlock
); /*concurrent reads */
412 cntr
= ACCESS_ONCE(desc
->length
);
416 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
421 if (file
->f_flags
& O_NONBLOCK
) {
422 if (!test_bit(WDM_READ
, &desc
->flags
)) {
423 rv
= cntr
? cntr
: -EAGAIN
;
428 rv
= wait_event_interruptible(desc
->wait
,
429 test_bit(WDM_READ
, &desc
->flags
));
432 /* may have happened while we slept */
433 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
437 usb_mark_last_busy(interface_to_usbdev(desc
->intf
));
443 spin_lock_irq(&desc
->iuspin
);
445 if (desc
->rerr
) { /* read completed, error happened */
447 spin_unlock_irq(&desc
->iuspin
);
452 * recheck whether we've lost the race
453 * against the completion handler
455 if (!test_bit(WDM_READ
, &desc
->flags
)) { /* lost race */
456 spin_unlock_irq(&desc
->iuspin
);
459 if (!desc
->reslength
) { /* zero length read */
460 spin_unlock_irq(&desc
->iuspin
);
464 spin_unlock_irq(&desc
->iuspin
);
469 rv
= copy_to_user(buffer
, desc
->ubuf
, cntr
);
475 spin_lock_irq(&desc
->iuspin
);
477 for (i
= 0; i
< desc
->length
- cntr
; i
++)
478 desc
->ubuf
[i
] = desc
->ubuf
[i
+ cntr
];
480 desc
->length
-= cntr
;
481 /* in case we had outstanding data */
483 clear_bit(WDM_READ
, &desc
->flags
);
485 spin_unlock_irq(&desc
->iuspin
);
490 mutex_unlock(&desc
->rlock
);
494 static int wdm_flush(struct file
*file
, fl_owner_t id
)
496 struct wdm_device
*desc
= file
->private_data
;
498 wait_event(desc
->wait
, !test_bit(WDM_IN_USE
, &desc
->flags
));
500 dev_err(&desc
->intf
->dev
, "Error in flush path: %d\n",
506 static unsigned int wdm_poll(struct file
*file
, struct poll_table_struct
*wait
)
508 struct wdm_device
*desc
= file
->private_data
;
510 unsigned int mask
= 0;
512 spin_lock_irqsave(&desc
->iuspin
, flags
);
513 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
515 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
518 if (test_bit(WDM_READ
, &desc
->flags
))
519 mask
= POLLIN
| POLLRDNORM
;
520 if (desc
->rerr
|| desc
->werr
)
522 if (!test_bit(WDM_IN_USE
, &desc
->flags
))
523 mask
|= POLLOUT
| POLLWRNORM
;
524 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
526 poll_wait(file
, &desc
->wait
, wait
);
532 static int wdm_open(struct inode
*inode
, struct file
*file
)
534 int minor
= iminor(inode
);
536 struct usb_interface
*intf
;
537 struct wdm_device
*desc
;
539 mutex_lock(&wdm_mutex
);
540 intf
= usb_find_interface(&wdm_driver
, minor
);
544 desc
= usb_get_intfdata(intf
);
545 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
))
547 file
->private_data
= desc
;
549 rv
= usb_autopm_get_interface(desc
->intf
);
551 dev_err(&desc
->intf
->dev
, "Error autopm - %d\n", rv
);
554 intf
->needs_remote_wakeup
= 1;
556 /* using write lock to protect desc->count */
557 mutex_lock(&desc
->wlock
);
558 if (!desc
->count
++) {
561 rv
= usb_submit_urb(desc
->validity
, GFP_KERNEL
);
564 dev_err(&desc
->intf
->dev
,
565 "Error submitting int urb - %d\n", rv
);
570 mutex_unlock(&desc
->wlock
);
571 usb_autopm_put_interface(desc
->intf
);
573 mutex_unlock(&wdm_mutex
);
577 static int wdm_release(struct inode
*inode
, struct file
*file
)
579 struct wdm_device
*desc
= file
->private_data
;
581 mutex_lock(&wdm_mutex
);
583 /* using write lock to protect desc->count */
584 mutex_lock(&desc
->wlock
);
586 mutex_unlock(&desc
->wlock
);
589 dev_dbg(&desc
->intf
->dev
, "wdm_release: cleanup");
591 if (!test_bit(WDM_DISCONNECTING
, &desc
->flags
))
592 desc
->intf
->needs_remote_wakeup
= 0;
594 mutex_unlock(&wdm_mutex
);
598 static const struct file_operations wdm_fops
= {
599 .owner
= THIS_MODULE
,
604 .release
= wdm_release
,
606 .llseek
= noop_llseek
,
609 static struct usb_class_driver wdm_class
= {
612 .minor_base
= WDM_MINOR_BASE
,
615 /* --- error handling --- */
616 static void wdm_rxwork(struct work_struct
*work
)
618 struct wdm_device
*desc
= container_of(work
, struct wdm_device
, rxwork
);
622 spin_lock_irqsave(&desc
->iuspin
, flags
);
623 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
624 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
626 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
627 rv
= usb_submit_urb(desc
->response
, GFP_KERNEL
);
628 if (rv
< 0 && rv
!= -EPERM
) {
629 spin_lock_irqsave(&desc
->iuspin
, flags
);
630 if (!test_bit(WDM_DISCONNECTING
, &desc
->flags
))
631 schedule_work(&desc
->rxwork
);
632 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
637 /* --- hotplug --- */
639 static int wdm_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
642 struct usb_device
*udev
= interface_to_usbdev(intf
);
643 struct wdm_device
*desc
;
644 struct usb_host_interface
*iface
;
645 struct usb_endpoint_descriptor
*ep
;
646 struct usb_cdc_dmm_desc
*dmhd
;
647 u8
*buffer
= intf
->altsetting
->extra
;
648 int buflen
= intf
->altsetting
->extralen
;
649 u16 maxcom
= WDM_DEFAULT_BUFSIZE
;
655 if (buffer
[1] != USB_DT_CS_INTERFACE
) {
656 dev_err(&intf
->dev
, "skipping garbage\n");
660 switch (buffer
[2]) {
661 case USB_CDC_HEADER_TYPE
:
663 case USB_CDC_DMM_TYPE
:
664 dmhd
= (struct usb_cdc_dmm_desc
*)buffer
;
665 maxcom
= le16_to_cpu(dmhd
->wMaxCommand
);
667 "Finding maximum buffer length: %d", maxcom
);
671 "Ignoring extra header, type %d, length %d\n",
672 buffer
[2], buffer
[0]);
681 desc
= kzalloc(sizeof(struct wdm_device
), GFP_KERNEL
);
684 mutex_init(&desc
->rlock
);
685 mutex_init(&desc
->wlock
);
686 spin_lock_init(&desc
->iuspin
);
687 init_waitqueue_head(&desc
->wait
);
688 desc
->wMaxCommand
= maxcom
;
689 /* this will be expanded and needed in hardware endianness */
690 desc
->inum
= cpu_to_le16((u16
)intf
->cur_altsetting
->desc
.bInterfaceNumber
);
692 INIT_WORK(&desc
->rxwork
, wdm_rxwork
);
695 iface
= intf
->cur_altsetting
;
696 if (iface
->desc
.bNumEndpoints
!= 1)
698 ep
= &iface
->endpoint
[0].desc
;
699 if (!ep
|| !usb_endpoint_is_int_in(ep
))
702 desc
->wMaxPacketSize
= usb_endpoint_maxp(ep
);
703 desc
->bMaxPacketSize0
= udev
->descriptor
.bMaxPacketSize0
;
705 desc
->orq
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_KERNEL
);
708 desc
->irq
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_KERNEL
);
712 desc
->validity
= usb_alloc_urb(0, GFP_KERNEL
);
716 desc
->response
= usb_alloc_urb(0, GFP_KERNEL
);
720 desc
->command
= usb_alloc_urb(0, GFP_KERNEL
);
724 desc
->ubuf
= kmalloc(desc
->wMaxCommand
, GFP_KERNEL
);
728 desc
->sbuf
= usb_alloc_coherent(interface_to_usbdev(intf
),
729 desc
->wMaxPacketSize
,
731 &desc
->validity
->transfer_dma
);
735 desc
->inbuf
= usb_alloc_coherent(interface_to_usbdev(intf
),
738 &desc
->response
->transfer_dma
);
744 interface_to_usbdev(intf
),
745 usb_rcvintpipe(interface_to_usbdev(intf
), ep
->bEndpointAddress
),
747 desc
->wMaxPacketSize
,
752 desc
->validity
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
754 usb_set_intfdata(intf
, desc
);
755 rv
= usb_register_dev(intf
, &wdm_class
);
759 dev_info(&intf
->dev
, "cdc-wdm%d: USB WDM device\n",
760 intf
->minor
- WDM_MINOR_BASE
);
764 usb_set_intfdata(intf
, NULL
);
765 usb_free_coherent(interface_to_usbdev(desc
->intf
),
766 desc
->bMaxPacketSize0
,
768 desc
->response
->transfer_dma
);
770 usb_free_coherent(interface_to_usbdev(desc
->intf
),
771 desc
->wMaxPacketSize
,
773 desc
->validity
->transfer_dma
);
783 static void wdm_disconnect(struct usb_interface
*intf
)
785 struct wdm_device
*desc
;
788 usb_deregister_dev(intf
, &wdm_class
);
789 mutex_lock(&wdm_mutex
);
790 desc
= usb_get_intfdata(intf
);
792 /* the spinlock makes sure no new urbs are generated in the callbacks */
793 spin_lock_irqsave(&desc
->iuspin
, flags
);
794 set_bit(WDM_DISCONNECTING
, &desc
->flags
);
795 set_bit(WDM_READ
, &desc
->flags
);
796 /* to terminate pending flushes */
797 clear_bit(WDM_IN_USE
, &desc
->flags
);
798 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
799 wake_up_all(&desc
->wait
);
800 mutex_lock(&desc
->rlock
);
801 mutex_lock(&desc
->wlock
);
803 cancel_work_sync(&desc
->rxwork
);
804 mutex_unlock(&desc
->wlock
);
805 mutex_unlock(&desc
->rlock
);
808 mutex_unlock(&wdm_mutex
);
812 static int wdm_suspend(struct usb_interface
*intf
, pm_message_t message
)
814 struct wdm_device
*desc
= usb_get_intfdata(intf
);
817 dev_dbg(&desc
->intf
->dev
, "wdm%d_suspend\n", intf
->minor
);
819 /* if this is an autosuspend the caller does the locking */
820 if (!PMSG_IS_AUTO(message
)) {
821 mutex_lock(&desc
->rlock
);
822 mutex_lock(&desc
->wlock
);
824 spin_lock_irq(&desc
->iuspin
);
826 if (PMSG_IS_AUTO(message
) &&
827 (test_bit(WDM_IN_USE
, &desc
->flags
)
828 || test_bit(WDM_RESPONDING
, &desc
->flags
))) {
829 spin_unlock_irq(&desc
->iuspin
);
833 set_bit(WDM_SUSPENDING
, &desc
->flags
);
834 spin_unlock_irq(&desc
->iuspin
);
835 /* callback submits work - order is essential */
837 cancel_work_sync(&desc
->rxwork
);
839 if (!PMSG_IS_AUTO(message
)) {
840 mutex_unlock(&desc
->wlock
);
841 mutex_unlock(&desc
->rlock
);
848 static int recover_from_urb_loss(struct wdm_device
*desc
)
853 rv
= usb_submit_urb(desc
->validity
, GFP_NOIO
);
855 dev_err(&desc
->intf
->dev
,
856 "Error resume submitting int urb - %d\n", rv
);
862 static int wdm_resume(struct usb_interface
*intf
)
864 struct wdm_device
*desc
= usb_get_intfdata(intf
);
867 dev_dbg(&desc
->intf
->dev
, "wdm%d_resume\n", intf
->minor
);
869 clear_bit(WDM_SUSPENDING
, &desc
->flags
);
870 rv
= recover_from_urb_loss(desc
);
876 static int wdm_pre_reset(struct usb_interface
*intf
)
878 struct wdm_device
*desc
= usb_get_intfdata(intf
);
880 mutex_lock(&desc
->rlock
);
881 mutex_lock(&desc
->wlock
);
885 * we notify everybody using poll of
886 * an exceptional situation
887 * must be done before recovery lest a spontaneous
888 * message from the device is lost
890 spin_lock_irq(&desc
->iuspin
);
892 spin_unlock_irq(&desc
->iuspin
);
893 wake_up_all(&desc
->wait
);
897 static int wdm_post_reset(struct usb_interface
*intf
)
899 struct wdm_device
*desc
= usb_get_intfdata(intf
);
902 rv
= recover_from_urb_loss(desc
);
903 mutex_unlock(&desc
->wlock
);
904 mutex_unlock(&desc
->rlock
);
908 static struct usb_driver wdm_driver
= {
911 .disconnect
= wdm_disconnect
,
913 .suspend
= wdm_suspend
,
914 .resume
= wdm_resume
,
915 .reset_resume
= wdm_resume
,
917 .pre_reset
= wdm_pre_reset
,
918 .post_reset
= wdm_post_reset
,
920 .supports_autosuspend
= 1,
923 module_usb_driver(wdm_driver
);
925 MODULE_AUTHOR(DRIVER_AUTHOR
);
926 MODULE_DESCRIPTION(DRIVER_DESC
);
927 MODULE_LICENSE("GPL");