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/ioctl.h>
17 #include <linux/slab.h>
18 #include <linux/module.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>
27 #include <linux/usb/cdc-wdm.h>
32 #define DRIVER_VERSION "v0.03"
33 #define DRIVER_AUTHOR "Oliver Neukum"
34 #define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management"
36 static const struct usb_device_id wdm_ids
[] = {
38 .match_flags
= USB_DEVICE_ID_MATCH_INT_CLASS
|
39 USB_DEVICE_ID_MATCH_INT_SUBCLASS
,
40 .bInterfaceClass
= USB_CLASS_COMM
,
41 .bInterfaceSubClass
= USB_CDC_SUBCLASS_DMM
46 MODULE_DEVICE_TABLE (usb
, wdm_ids
);
48 #define WDM_MINOR_BASE 176
52 #define WDM_DISCONNECTING 2
55 #define WDM_INT_STALL 5
56 #define WDM_POLL_RUNNING 6
57 #define WDM_RESPONDING 7
58 #define WDM_SUSPENDING 8
59 #define WDM_RESETTING 9
60 #define WDM_OVERFLOW 10
64 /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
65 #define WDM_DEFAULT_BUFSIZE 256
67 static DEFINE_MUTEX(wdm_mutex
);
68 static DEFINE_SPINLOCK(wdm_device_list_lock
);
69 static LIST_HEAD(wdm_device_list
);
71 /* --- method tables --- */
74 u8
*inbuf
; /* buffer for response */
75 u8
*outbuf
; /* buffer for command */
76 u8
*sbuf
; /* buffer for status */
77 u8
*ubuf
; /* buffer for copy to user space */
82 struct usb_interface
*intf
;
83 struct usb_ctrlrequest
*orq
;
84 struct usb_ctrlrequest
*irq
;
100 wait_queue_head_t wait
;
101 struct work_struct rxwork
;
105 struct list_head device_list
;
106 int (*manage_power
)(struct usb_interface
*, int);
109 static struct usb_driver wdm_driver
;
111 /* return intfdata if we own the interface, else look up intf in the list */
112 static struct wdm_device
*wdm_find_device(struct usb_interface
*intf
)
114 struct wdm_device
*desc
;
116 spin_lock(&wdm_device_list_lock
);
117 list_for_each_entry(desc
, &wdm_device_list
, device_list
)
118 if (desc
->intf
== intf
)
122 spin_unlock(&wdm_device_list_lock
);
127 static struct wdm_device
*wdm_find_device_by_minor(int minor
)
129 struct wdm_device
*desc
;
131 spin_lock(&wdm_device_list_lock
);
132 list_for_each_entry(desc
, &wdm_device_list
, device_list
)
133 if (desc
->intf
->minor
== minor
)
137 spin_unlock(&wdm_device_list_lock
);
142 /* --- callbacks --- */
143 static void wdm_out_callback(struct urb
*urb
)
145 struct wdm_device
*desc
;
147 spin_lock(&desc
->iuspin
);
148 desc
->werr
= urb
->status
;
149 spin_unlock(&desc
->iuspin
);
152 clear_bit(WDM_IN_USE
, &desc
->flags
);
153 wake_up(&desc
->wait
);
156 static void wdm_in_callback(struct urb
*urb
)
158 struct wdm_device
*desc
= urb
->context
;
159 int status
= urb
->status
;
160 int length
= urb
->actual_length
;
162 spin_lock(&desc
->iuspin
);
163 clear_bit(WDM_RESPONDING
, &desc
->flags
);
168 dev_dbg(&desc
->intf
->dev
,
169 "nonzero urb status received: -ENOENT");
172 dev_dbg(&desc
->intf
->dev
,
173 "nonzero urb status received: -ECONNRESET");
176 dev_dbg(&desc
->intf
->dev
,
177 "nonzero urb status received: -ESHUTDOWN");
180 dev_err(&desc
->intf
->dev
,
181 "nonzero urb status received: -EPIPE\n");
184 dev_err(&desc
->intf
->dev
,
185 "Unexpected error %d\n", status
);
191 if (length
+ desc
->length
> desc
->wMaxCommand
) {
192 /* The buffer would overflow */
193 set_bit(WDM_OVERFLOW
, &desc
->flags
);
195 /* we may already be in overflow */
196 if (!test_bit(WDM_OVERFLOW
, &desc
->flags
)) {
197 memmove(desc
->ubuf
+ desc
->length
, desc
->inbuf
, length
);
198 desc
->length
+= length
;
199 desc
->reslength
= length
;
203 wake_up(&desc
->wait
);
205 set_bit(WDM_READ
, &desc
->flags
);
206 spin_unlock(&desc
->iuspin
);
209 static void wdm_int_callback(struct urb
*urb
)
212 int status
= urb
->status
;
213 struct wdm_device
*desc
;
214 struct usb_cdc_notification
*dr
;
217 dr
= (struct usb_cdc_notification
*)desc
->sbuf
;
226 set_bit(WDM_INT_STALL
, &desc
->flags
);
227 dev_err(&desc
->intf
->dev
, "Stall on int endpoint\n");
228 goto sw
; /* halt is cleared in work */
230 dev_err(&desc
->intf
->dev
,
231 "nonzero urb status received: %d\n", status
);
236 if (urb
->actual_length
< sizeof(struct usb_cdc_notification
)) {
237 dev_err(&desc
->intf
->dev
, "wdm_int_callback - %d bytes\n",
242 switch (dr
->bNotificationType
) {
243 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE
:
244 dev_dbg(&desc
->intf
->dev
,
245 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
246 dr
->wIndex
, dr
->wLength
);
249 case USB_CDC_NOTIFY_NETWORK_CONNECTION
:
251 dev_dbg(&desc
->intf
->dev
,
252 "NOTIFY_NETWORK_CONNECTION %s network",
253 dr
->wValue
? "connected to" : "disconnected from");
256 clear_bit(WDM_POLL_RUNNING
, &desc
->flags
);
257 dev_err(&desc
->intf
->dev
,
258 "unknown notification %d received: index %d len %d\n",
259 dr
->bNotificationType
, dr
->wIndex
, dr
->wLength
);
263 spin_lock(&desc
->iuspin
);
264 clear_bit(WDM_READ
, &desc
->flags
);
265 set_bit(WDM_RESPONDING
, &desc
->flags
);
266 if (!test_bit(WDM_DISCONNECTING
, &desc
->flags
)
267 && !test_bit(WDM_SUSPENDING
, &desc
->flags
)) {
268 rv
= usb_submit_urb(desc
->response
, GFP_ATOMIC
);
269 dev_dbg(&desc
->intf
->dev
, "%s: usb_submit_urb %d",
272 spin_unlock(&desc
->iuspin
);
274 clear_bit(WDM_RESPONDING
, &desc
->flags
);
279 rv
= schedule_work(&desc
->rxwork
);
281 dev_err(&desc
->intf
->dev
,
282 "Cannot schedule work\n");
286 rv
= usb_submit_urb(urb
, GFP_ATOMIC
);
288 dev_err(&desc
->intf
->dev
,
289 "%s - usb_submit_urb failed with result %d\n",
294 static void kill_urbs(struct wdm_device
*desc
)
296 /* the order here is essential */
297 usb_kill_urb(desc
->command
);
298 usb_kill_urb(desc
->validity
);
299 usb_kill_urb(desc
->response
);
302 static void free_urbs(struct wdm_device
*desc
)
304 usb_free_urb(desc
->validity
);
305 usb_free_urb(desc
->response
);
306 usb_free_urb(desc
->command
);
309 static void cleanup(struct wdm_device
*desc
)
320 static ssize_t wdm_write
321 (struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
324 int rv
= -EMSGSIZE
, r
, we
;
325 struct wdm_device
*desc
= file
->private_data
;
326 struct usb_ctrlrequest
*req
;
328 if (count
> desc
->wMaxCommand
)
329 count
= desc
->wMaxCommand
;
331 spin_lock_irq(&desc
->iuspin
);
334 spin_unlock_irq(&desc
->iuspin
);
338 buf
= kmalloc(count
, GFP_KERNEL
);
344 r
= copy_from_user(buf
, buffer
, count
);
351 /* concurrent writes and disconnect */
352 r
= mutex_lock_interruptible(&desc
->wlock
);
359 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
365 r
= usb_autopm_get_interface(desc
->intf
);
368 rv
= usb_translate_errors(r
);
372 if (!(file
->f_flags
& O_NONBLOCK
))
373 r
= wait_event_interruptible(desc
->wait
, !test_bit(WDM_IN_USE
,
376 if (test_bit(WDM_IN_USE
, &desc
->flags
))
379 if (test_bit(WDM_RESETTING
, &desc
->flags
))
389 usb_fill_control_urb(
391 interface_to_usbdev(desc
->intf
),
392 /* using common endpoint 0 */
393 usb_sndctrlpipe(interface_to_usbdev(desc
->intf
), 0),
394 (unsigned char *)req
,
401 req
->bRequestType
= (USB_DIR_OUT
| USB_TYPE_CLASS
|
402 USB_RECIP_INTERFACE
);
403 req
->bRequest
= USB_CDC_SEND_ENCAPSULATED_COMMAND
;
405 req
->wIndex
= desc
->inum
;
406 req
->wLength
= cpu_to_le16(count
);
407 set_bit(WDM_IN_USE
, &desc
->flags
);
410 rv
= usb_submit_urb(desc
->command
, GFP_KERNEL
);
414 clear_bit(WDM_IN_USE
, &desc
->flags
);
415 dev_err(&desc
->intf
->dev
, "Tx URB error: %d\n", rv
);
416 rv
= usb_translate_errors(rv
);
418 dev_dbg(&desc
->intf
->dev
, "Tx URB has been submitted index=%d",
422 usb_autopm_put_interface(desc
->intf
);
424 mutex_unlock(&desc
->wlock
);
426 return rv
< 0 ? rv
: count
;
429 static ssize_t wdm_read
430 (struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
434 struct wdm_device
*desc
= file
->private_data
;
437 rv
= mutex_lock_interruptible(&desc
->rlock
); /*concurrent reads */
441 cntr
= ACCESS_ONCE(desc
->length
);
445 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
449 if (test_bit(WDM_OVERFLOW
, &desc
->flags
)) {
450 clear_bit(WDM_OVERFLOW
, &desc
->flags
);
455 if (file
->f_flags
& O_NONBLOCK
) {
456 if (!test_bit(WDM_READ
, &desc
->flags
)) {
457 rv
= cntr
? cntr
: -EAGAIN
;
462 rv
= wait_event_interruptible(desc
->wait
,
463 test_bit(WDM_READ
, &desc
->flags
));
466 /* may have happened while we slept */
467 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
471 if (test_bit(WDM_RESETTING
, &desc
->flags
)) {
475 usb_mark_last_busy(interface_to_usbdev(desc
->intf
));
481 spin_lock_irq(&desc
->iuspin
);
483 if (desc
->rerr
) { /* read completed, error happened */
485 spin_unlock_irq(&desc
->iuspin
);
490 * recheck whether we've lost the race
491 * against the completion handler
493 if (!test_bit(WDM_READ
, &desc
->flags
)) { /* lost race */
494 spin_unlock_irq(&desc
->iuspin
);
498 if (!desc
->reslength
) { /* zero length read */
499 dev_dbg(&desc
->intf
->dev
, "%s: zero length - clearing WDM_READ\n", __func__
);
500 clear_bit(WDM_READ
, &desc
->flags
);
501 spin_unlock_irq(&desc
->iuspin
);
505 spin_unlock_irq(&desc
->iuspin
);
510 rv
= copy_to_user(buffer
, desc
->ubuf
, cntr
);
516 spin_lock_irq(&desc
->iuspin
);
518 for (i
= 0; i
< desc
->length
- cntr
; i
++)
519 desc
->ubuf
[i
] = desc
->ubuf
[i
+ cntr
];
521 desc
->length
-= cntr
;
522 /* in case we had outstanding data */
524 clear_bit(WDM_READ
, &desc
->flags
);
526 spin_unlock_irq(&desc
->iuspin
);
531 mutex_unlock(&desc
->rlock
);
535 static int wdm_flush(struct file
*file
, fl_owner_t id
)
537 struct wdm_device
*desc
= file
->private_data
;
539 wait_event(desc
->wait
, !test_bit(WDM_IN_USE
, &desc
->flags
));
541 /* cannot dereference desc->intf if WDM_DISCONNECTING */
542 if (desc
->werr
< 0 && !test_bit(WDM_DISCONNECTING
, &desc
->flags
))
543 dev_err(&desc
->intf
->dev
, "Error in flush path: %d\n",
546 return usb_translate_errors(desc
->werr
);
549 static unsigned int wdm_poll(struct file
*file
, struct poll_table_struct
*wait
)
551 struct wdm_device
*desc
= file
->private_data
;
553 unsigned int mask
= 0;
555 spin_lock_irqsave(&desc
->iuspin
, flags
);
556 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
557 mask
= POLLHUP
| POLLERR
;
558 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
561 if (test_bit(WDM_READ
, &desc
->flags
))
562 mask
= POLLIN
| POLLRDNORM
;
563 if (desc
->rerr
|| desc
->werr
)
565 if (!test_bit(WDM_IN_USE
, &desc
->flags
))
566 mask
|= POLLOUT
| POLLWRNORM
;
567 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
569 poll_wait(file
, &desc
->wait
, wait
);
575 static int wdm_open(struct inode
*inode
, struct file
*file
)
577 int minor
= iminor(inode
);
579 struct usb_interface
*intf
;
580 struct wdm_device
*desc
;
582 mutex_lock(&wdm_mutex
);
583 desc
= wdm_find_device_by_minor(minor
);
588 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
))
590 file
->private_data
= desc
;
592 rv
= usb_autopm_get_interface(desc
->intf
);
594 dev_err(&desc
->intf
->dev
, "Error autopm - %d\n", rv
);
598 /* using write lock to protect desc->count */
599 mutex_lock(&desc
->wlock
);
600 if (!desc
->count
++) {
603 rv
= usb_submit_urb(desc
->validity
, GFP_KERNEL
);
606 dev_err(&desc
->intf
->dev
,
607 "Error submitting int urb - %d\n", rv
);
608 rv
= usb_translate_errors(rv
);
613 mutex_unlock(&desc
->wlock
);
614 if (desc
->count
== 1)
615 desc
->manage_power(intf
, 1);
616 usb_autopm_put_interface(desc
->intf
);
618 mutex_unlock(&wdm_mutex
);
622 static int wdm_release(struct inode
*inode
, struct file
*file
)
624 struct wdm_device
*desc
= file
->private_data
;
626 mutex_lock(&wdm_mutex
);
628 /* using write lock to protect desc->count */
629 mutex_lock(&desc
->wlock
);
631 mutex_unlock(&desc
->wlock
);
634 if (!test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
635 dev_dbg(&desc
->intf
->dev
, "wdm_release: cleanup");
637 desc
->manage_power(desc
->intf
, 0);
639 /* must avoid dev_printk here as desc->intf is invalid */
640 pr_debug(KBUILD_MODNAME
" %s: device gone - cleaning up\n", __func__
);
644 mutex_unlock(&wdm_mutex
);
648 static long wdm_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
650 struct wdm_device
*desc
= file
->private_data
;
654 case IOCTL_WDM_MAX_COMMAND
:
655 if (copy_to_user((void __user
*)arg
, &desc
->wMaxCommand
, sizeof(desc
->wMaxCommand
)))
664 static const struct file_operations wdm_fops
= {
665 .owner
= THIS_MODULE
,
670 .release
= wdm_release
,
672 .unlocked_ioctl
= wdm_ioctl
,
673 .compat_ioctl
= wdm_ioctl
,
674 .llseek
= noop_llseek
,
677 static struct usb_class_driver wdm_class
= {
680 .minor_base
= WDM_MINOR_BASE
,
683 /* --- error handling --- */
684 static void wdm_rxwork(struct work_struct
*work
)
686 struct wdm_device
*desc
= container_of(work
, struct wdm_device
, rxwork
);
690 spin_lock_irqsave(&desc
->iuspin
, flags
);
691 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
692 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
694 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
695 rv
= usb_submit_urb(desc
->response
, GFP_KERNEL
);
696 if (rv
< 0 && rv
!= -EPERM
) {
697 spin_lock_irqsave(&desc
->iuspin
, flags
);
698 if (!test_bit(WDM_DISCONNECTING
, &desc
->flags
))
699 schedule_work(&desc
->rxwork
);
700 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
705 /* --- hotplug --- */
707 static int wdm_create(struct usb_interface
*intf
, struct usb_endpoint_descriptor
*ep
,
708 u16 bufsize
, int (*manage_power
)(struct usb_interface
*, int))
711 struct wdm_device
*desc
;
713 desc
= kzalloc(sizeof(struct wdm_device
), GFP_KERNEL
);
716 INIT_LIST_HEAD(&desc
->device_list
);
717 mutex_init(&desc
->rlock
);
718 mutex_init(&desc
->wlock
);
719 spin_lock_init(&desc
->iuspin
);
720 init_waitqueue_head(&desc
->wait
);
721 desc
->wMaxCommand
= bufsize
;
722 /* this will be expanded and needed in hardware endianness */
723 desc
->inum
= cpu_to_le16((u16
)intf
->cur_altsetting
->desc
.bInterfaceNumber
);
725 INIT_WORK(&desc
->rxwork
, wdm_rxwork
);
728 if (!usb_endpoint_is_int_in(ep
))
731 desc
->wMaxPacketSize
= usb_endpoint_maxp(ep
);
733 desc
->orq
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_KERNEL
);
736 desc
->irq
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_KERNEL
);
740 desc
->validity
= usb_alloc_urb(0, GFP_KERNEL
);
744 desc
->response
= usb_alloc_urb(0, GFP_KERNEL
);
748 desc
->command
= usb_alloc_urb(0, GFP_KERNEL
);
752 desc
->ubuf
= kmalloc(desc
->wMaxCommand
, GFP_KERNEL
);
756 desc
->sbuf
= kmalloc(desc
->wMaxPacketSize
, GFP_KERNEL
);
760 desc
->inbuf
= kmalloc(desc
->wMaxCommand
, GFP_KERNEL
);
766 interface_to_usbdev(intf
),
767 usb_rcvintpipe(interface_to_usbdev(intf
), ep
->bEndpointAddress
),
769 desc
->wMaxPacketSize
,
775 desc
->irq
->bRequestType
= (USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
);
776 desc
->irq
->bRequest
= USB_CDC_GET_ENCAPSULATED_RESPONSE
;
777 desc
->irq
->wValue
= 0;
778 desc
->irq
->wIndex
= desc
->inum
;
779 desc
->irq
->wLength
= cpu_to_le16(desc
->wMaxCommand
);
781 usb_fill_control_urb(
783 interface_to_usbdev(intf
),
784 /* using common endpoint 0 */
785 usb_rcvctrlpipe(interface_to_usbdev(desc
->intf
), 0),
786 (unsigned char *)desc
->irq
,
793 desc
->manage_power
= manage_power
;
795 spin_lock(&wdm_device_list_lock
);
796 list_add(&desc
->device_list
, &wdm_device_list
);
797 spin_unlock(&wdm_device_list_lock
);
799 rv
= usb_register_dev(intf
, &wdm_class
);
803 dev_info(&intf
->dev
, "%s: USB WDM device\n", dev_name(intf
->usb_dev
));
807 spin_lock(&wdm_device_list_lock
);
808 list_del(&desc
->device_list
);
809 spin_unlock(&wdm_device_list_lock
);
814 static int wdm_manage_power(struct usb_interface
*intf
, int on
)
816 /* need autopm_get/put here to ensure the usbcore sees the new value */
817 int rv
= usb_autopm_get_interface(intf
);
821 intf
->needs_remote_wakeup
= on
;
822 usb_autopm_put_interface(intf
);
827 static int wdm_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
830 struct usb_host_interface
*iface
;
831 struct usb_endpoint_descriptor
*ep
;
832 struct usb_cdc_dmm_desc
*dmhd
;
833 u8
*buffer
= intf
->altsetting
->extra
;
834 int buflen
= intf
->altsetting
->extralen
;
835 u16 maxcom
= WDM_DEFAULT_BUFSIZE
;
840 if (buffer
[1] != USB_DT_CS_INTERFACE
) {
841 dev_err(&intf
->dev
, "skipping garbage\n");
846 case USB_CDC_HEADER_TYPE
:
848 case USB_CDC_DMM_TYPE
:
849 dmhd
= (struct usb_cdc_dmm_desc
*)buffer
;
850 maxcom
= le16_to_cpu(dmhd
->wMaxCommand
);
852 "Finding maximum buffer length: %d", maxcom
);
856 "Ignoring extra header, type %d, length %d\n",
857 buffer
[2], buffer
[0]);
865 iface
= intf
->cur_altsetting
;
866 if (iface
->desc
.bNumEndpoints
!= 1)
868 ep
= &iface
->endpoint
[0].desc
;
870 rv
= wdm_create(intf
, ep
, maxcom
, &wdm_manage_power
);
877 * usb_cdc_wdm_register - register a WDM subdriver
878 * @intf: usb interface the subdriver will associate with
879 * @ep: interrupt endpoint to monitor for notifications
880 * @bufsize: maximum message size to support for read/write
882 * Create WDM usb class character device and associate it with intf
883 * without binding, allowing another driver to manage the interface.
885 * The subdriver will manage the given interrupt endpoint exclusively
886 * and will issue control requests referring to the given intf. It
887 * will otherwise avoid interferring, and in particular not do
888 * usb_set_intfdata/usb_get_intfdata on intf.
890 * The return value is a pointer to the subdriver's struct usb_driver.
891 * The registering driver is responsible for calling this subdriver's
892 * disconnect, suspend, resume, pre_reset and post_reset methods from
895 struct usb_driver
*usb_cdc_wdm_register(struct usb_interface
*intf
,
896 struct usb_endpoint_descriptor
*ep
,
898 int (*manage_power
)(struct usb_interface
*, int))
902 rv
= wdm_create(intf
, ep
, bufsize
, manage_power
);
910 EXPORT_SYMBOL(usb_cdc_wdm_register
);
912 static void wdm_disconnect(struct usb_interface
*intf
)
914 struct wdm_device
*desc
;
917 usb_deregister_dev(intf
, &wdm_class
);
918 desc
= wdm_find_device(intf
);
919 mutex_lock(&wdm_mutex
);
921 /* the spinlock makes sure no new urbs are generated in the callbacks */
922 spin_lock_irqsave(&desc
->iuspin
, flags
);
923 set_bit(WDM_DISCONNECTING
, &desc
->flags
);
924 set_bit(WDM_READ
, &desc
->flags
);
925 /* to terminate pending flushes */
926 clear_bit(WDM_IN_USE
, &desc
->flags
);
927 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
928 wake_up_all(&desc
->wait
);
929 mutex_lock(&desc
->rlock
);
930 mutex_lock(&desc
->wlock
);
932 cancel_work_sync(&desc
->rxwork
);
933 mutex_unlock(&desc
->wlock
);
934 mutex_unlock(&desc
->rlock
);
936 /* the desc->intf pointer used as list key is now invalid */
937 spin_lock(&wdm_device_list_lock
);
938 list_del(&desc
->device_list
);
939 spin_unlock(&wdm_device_list_lock
);
944 dev_dbg(&intf
->dev
, "%s: %d open files - postponing cleanup\n", __func__
, desc
->count
);
945 mutex_unlock(&wdm_mutex
);
949 static int wdm_suspend(struct usb_interface
*intf
, pm_message_t message
)
951 struct wdm_device
*desc
= wdm_find_device(intf
);
954 dev_dbg(&desc
->intf
->dev
, "wdm%d_suspend\n", intf
->minor
);
956 /* if this is an autosuspend the caller does the locking */
957 if (!PMSG_IS_AUTO(message
)) {
958 mutex_lock(&desc
->rlock
);
959 mutex_lock(&desc
->wlock
);
961 spin_lock_irq(&desc
->iuspin
);
963 if (PMSG_IS_AUTO(message
) &&
964 (test_bit(WDM_IN_USE
, &desc
->flags
)
965 || test_bit(WDM_RESPONDING
, &desc
->flags
))) {
966 spin_unlock_irq(&desc
->iuspin
);
970 set_bit(WDM_SUSPENDING
, &desc
->flags
);
971 spin_unlock_irq(&desc
->iuspin
);
972 /* callback submits work - order is essential */
974 cancel_work_sync(&desc
->rxwork
);
976 if (!PMSG_IS_AUTO(message
)) {
977 mutex_unlock(&desc
->wlock
);
978 mutex_unlock(&desc
->rlock
);
985 static int recover_from_urb_loss(struct wdm_device
*desc
)
990 rv
= usb_submit_urb(desc
->validity
, GFP_NOIO
);
992 dev_err(&desc
->intf
->dev
,
993 "Error resume submitting int urb - %d\n", rv
);
999 static int wdm_resume(struct usb_interface
*intf
)
1001 struct wdm_device
*desc
= wdm_find_device(intf
);
1004 dev_dbg(&desc
->intf
->dev
, "wdm%d_resume\n", intf
->minor
);
1006 clear_bit(WDM_SUSPENDING
, &desc
->flags
);
1007 rv
= recover_from_urb_loss(desc
);
1013 static int wdm_pre_reset(struct usb_interface
*intf
)
1015 struct wdm_device
*desc
= wdm_find_device(intf
);
1018 * we notify everybody using poll of
1019 * an exceptional situation
1020 * must be done before recovery lest a spontaneous
1021 * message from the device is lost
1023 spin_lock_irq(&desc
->iuspin
);
1024 set_bit(WDM_RESETTING
, &desc
->flags
); /* inform read/write */
1025 set_bit(WDM_READ
, &desc
->flags
); /* unblock read */
1026 clear_bit(WDM_IN_USE
, &desc
->flags
); /* unblock write */
1027 desc
->rerr
= -EINTR
;
1028 spin_unlock_irq(&desc
->iuspin
);
1029 wake_up_all(&desc
->wait
);
1030 mutex_lock(&desc
->rlock
);
1031 mutex_lock(&desc
->wlock
);
1033 cancel_work_sync(&desc
->rxwork
);
1037 static int wdm_post_reset(struct usb_interface
*intf
)
1039 struct wdm_device
*desc
= wdm_find_device(intf
);
1042 clear_bit(WDM_OVERFLOW
, &desc
->flags
);
1043 clear_bit(WDM_RESETTING
, &desc
->flags
);
1044 rv
= recover_from_urb_loss(desc
);
1045 mutex_unlock(&desc
->wlock
);
1046 mutex_unlock(&desc
->rlock
);
1050 static struct usb_driver wdm_driver
= {
1053 .disconnect
= wdm_disconnect
,
1055 .suspend
= wdm_suspend
,
1056 .resume
= wdm_resume
,
1057 .reset_resume
= wdm_resume
,
1059 .pre_reset
= wdm_pre_reset
,
1060 .post_reset
= wdm_post_reset
,
1061 .id_table
= wdm_ids
,
1062 .supports_autosuspend
= 1,
1063 .disable_hub_initiated_lpm
= 1,
1066 module_usb_driver(wdm_driver
);
1068 MODULE_AUTHOR(DRIVER_AUTHOR
);
1069 MODULE_DESCRIPTION(DRIVER_DESC
);
1070 MODULE_LICENSE("GPL");