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>
26 #include <linux/usb/cdc-wdm.h>
31 #define DRIVER_VERSION "v0.03"
32 #define DRIVER_AUTHOR "Oliver Neukum"
33 #define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management"
35 static const struct usb_device_id wdm_ids
[] = {
37 .match_flags
= USB_DEVICE_ID_MATCH_INT_CLASS
|
38 USB_DEVICE_ID_MATCH_INT_SUBCLASS
,
39 .bInterfaceClass
= USB_CLASS_COMM
,
40 .bInterfaceSubClass
= USB_CDC_SUBCLASS_DMM
45 MODULE_DEVICE_TABLE (usb
, wdm_ids
);
47 #define WDM_MINOR_BASE 176
51 #define WDM_DISCONNECTING 2
54 #define WDM_INT_STALL 5
55 #define WDM_POLL_RUNNING 6
56 #define WDM_RESPONDING 7
57 #define WDM_SUSPENDING 8
58 #define WDM_RESETTING 9
59 #define WDM_OVERFLOW 10
63 /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
64 #define WDM_DEFAULT_BUFSIZE 256
66 static DEFINE_MUTEX(wdm_mutex
);
67 static DEFINE_SPINLOCK(wdm_device_list_lock
);
68 static LIST_HEAD(wdm_device_list
);
70 /* --- method tables --- */
73 u8
*inbuf
; /* buffer for response */
74 u8
*outbuf
; /* buffer for command */
75 u8
*sbuf
; /* buffer for status */
76 u8
*ubuf
; /* buffer for copy to user space */
81 struct usb_interface
*intf
;
82 struct usb_ctrlrequest
*orq
;
83 struct usb_ctrlrequest
*irq
;
99 wait_queue_head_t wait
;
100 struct work_struct rxwork
;
104 struct list_head device_list
;
105 int (*manage_power
)(struct usb_interface
*, int);
108 static struct usb_driver wdm_driver
;
110 /* return intfdata if we own the interface, else look up intf in the list */
111 static struct wdm_device
*wdm_find_device(struct usb_interface
*intf
)
113 struct wdm_device
*desc
;
115 spin_lock(&wdm_device_list_lock
);
116 list_for_each_entry(desc
, &wdm_device_list
, device_list
)
117 if (desc
->intf
== intf
)
121 spin_unlock(&wdm_device_list_lock
);
126 static struct wdm_device
*wdm_find_device_by_minor(int minor
)
128 struct wdm_device
*desc
;
130 spin_lock(&wdm_device_list_lock
);
131 list_for_each_entry(desc
, &wdm_device_list
, device_list
)
132 if (desc
->intf
->minor
== minor
)
136 spin_unlock(&wdm_device_list_lock
);
141 /* --- callbacks --- */
142 static void wdm_out_callback(struct urb
*urb
)
144 struct wdm_device
*desc
;
146 spin_lock(&desc
->iuspin
);
147 desc
->werr
= urb
->status
;
148 spin_unlock(&desc
->iuspin
);
151 clear_bit(WDM_IN_USE
, &desc
->flags
);
152 wake_up(&desc
->wait
);
155 static void wdm_in_callback(struct urb
*urb
)
157 struct wdm_device
*desc
= urb
->context
;
158 int status
= urb
->status
;
159 int length
= urb
->actual_length
;
161 spin_lock(&desc
->iuspin
);
162 clear_bit(WDM_RESPONDING
, &desc
->flags
);
167 dev_dbg(&desc
->intf
->dev
,
168 "nonzero urb status received: -ENOENT");
171 dev_dbg(&desc
->intf
->dev
,
172 "nonzero urb status received: -ECONNRESET");
175 dev_dbg(&desc
->intf
->dev
,
176 "nonzero urb status received: -ESHUTDOWN");
179 dev_err(&desc
->intf
->dev
,
180 "nonzero urb status received: -EPIPE\n");
183 dev_err(&desc
->intf
->dev
,
184 "Unexpected error %d\n", status
);
190 if (length
+ desc
->length
> desc
->wMaxCommand
) {
191 /* The buffer would overflow */
192 set_bit(WDM_OVERFLOW
, &desc
->flags
);
194 /* we may already be in overflow */
195 if (!test_bit(WDM_OVERFLOW
, &desc
->flags
)) {
196 memmove(desc
->ubuf
+ desc
->length
, desc
->inbuf
, length
);
197 desc
->length
+= length
;
198 desc
->reslength
= length
;
202 wake_up(&desc
->wait
);
204 set_bit(WDM_READ
, &desc
->flags
);
205 spin_unlock(&desc
->iuspin
);
208 static void wdm_int_callback(struct urb
*urb
)
211 int status
= urb
->status
;
212 struct wdm_device
*desc
;
213 struct usb_cdc_notification
*dr
;
216 dr
= (struct usb_cdc_notification
*)desc
->sbuf
;
225 set_bit(WDM_INT_STALL
, &desc
->flags
);
226 dev_err(&desc
->intf
->dev
, "Stall on int endpoint\n");
227 goto sw
; /* halt is cleared in work */
229 dev_err(&desc
->intf
->dev
,
230 "nonzero urb status received: %d\n", status
);
235 if (urb
->actual_length
< sizeof(struct usb_cdc_notification
)) {
236 dev_err(&desc
->intf
->dev
, "wdm_int_callback - %d bytes\n",
241 switch (dr
->bNotificationType
) {
242 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE
:
243 dev_dbg(&desc
->intf
->dev
,
244 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
245 dr
->wIndex
, dr
->wLength
);
248 case USB_CDC_NOTIFY_NETWORK_CONNECTION
:
250 dev_dbg(&desc
->intf
->dev
,
251 "NOTIFY_NETWORK_CONNECTION %s network",
252 dr
->wValue
? "connected to" : "disconnected from");
255 clear_bit(WDM_POLL_RUNNING
, &desc
->flags
);
256 dev_err(&desc
->intf
->dev
,
257 "unknown notification %d received: index %d len %d\n",
258 dr
->bNotificationType
, dr
->wIndex
, dr
->wLength
);
262 spin_lock(&desc
->iuspin
);
263 clear_bit(WDM_READ
, &desc
->flags
);
264 set_bit(WDM_RESPONDING
, &desc
->flags
);
265 if (!test_bit(WDM_DISCONNECTING
, &desc
->flags
)
266 && !test_bit(WDM_SUSPENDING
, &desc
->flags
)) {
267 rv
= usb_submit_urb(desc
->response
, GFP_ATOMIC
);
268 dev_dbg(&desc
->intf
->dev
, "%s: usb_submit_urb %d",
271 spin_unlock(&desc
->iuspin
);
273 clear_bit(WDM_RESPONDING
, &desc
->flags
);
278 rv
= schedule_work(&desc
->rxwork
);
280 dev_err(&desc
->intf
->dev
,
281 "Cannot schedule work\n");
285 rv
= usb_submit_urb(urb
, GFP_ATOMIC
);
287 dev_err(&desc
->intf
->dev
,
288 "%s - usb_submit_urb failed with result %d\n",
293 static void kill_urbs(struct wdm_device
*desc
)
295 /* the order here is essential */
296 usb_kill_urb(desc
->command
);
297 usb_kill_urb(desc
->validity
);
298 usb_kill_urb(desc
->response
);
301 static void free_urbs(struct wdm_device
*desc
)
303 usb_free_urb(desc
->validity
);
304 usb_free_urb(desc
->response
);
305 usb_free_urb(desc
->command
);
308 static void cleanup(struct wdm_device
*desc
)
319 static ssize_t wdm_write
320 (struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
323 int rv
= -EMSGSIZE
, r
, we
;
324 struct wdm_device
*desc
= file
->private_data
;
325 struct usb_ctrlrequest
*req
;
327 if (count
> desc
->wMaxCommand
)
328 count
= desc
->wMaxCommand
;
330 spin_lock_irq(&desc
->iuspin
);
333 spin_unlock_irq(&desc
->iuspin
);
337 buf
= kmalloc(count
, GFP_KERNEL
);
343 r
= copy_from_user(buf
, buffer
, count
);
350 /* concurrent writes and disconnect */
351 r
= mutex_lock_interruptible(&desc
->wlock
);
358 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
364 r
= usb_autopm_get_interface(desc
->intf
);
370 if (!(file
->f_flags
& O_NONBLOCK
))
371 r
= wait_event_interruptible(desc
->wait
, !test_bit(WDM_IN_USE
,
374 if (test_bit(WDM_IN_USE
, &desc
->flags
))
377 if (test_bit(WDM_RESETTING
, &desc
->flags
))
386 usb_fill_control_urb(
388 interface_to_usbdev(desc
->intf
),
389 /* using common endpoint 0 */
390 usb_sndctrlpipe(interface_to_usbdev(desc
->intf
), 0),
391 (unsigned char *)req
,
398 req
->bRequestType
= (USB_DIR_OUT
| USB_TYPE_CLASS
|
399 USB_RECIP_INTERFACE
);
400 req
->bRequest
= USB_CDC_SEND_ENCAPSULATED_COMMAND
;
402 req
->wIndex
= desc
->inum
;
403 req
->wLength
= cpu_to_le16(count
);
404 set_bit(WDM_IN_USE
, &desc
->flags
);
407 rv
= usb_submit_urb(desc
->command
, GFP_KERNEL
);
411 clear_bit(WDM_IN_USE
, &desc
->flags
);
412 dev_err(&desc
->intf
->dev
, "Tx URB error: %d\n", rv
);
414 dev_dbg(&desc
->intf
->dev
, "Tx URB has been submitted index=%d",
418 usb_autopm_put_interface(desc
->intf
);
420 mutex_unlock(&desc
->wlock
);
422 return rv
< 0 ? rv
: count
;
425 static ssize_t wdm_read
426 (struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
430 struct wdm_device
*desc
= file
->private_data
;
433 rv
= mutex_lock_interruptible(&desc
->rlock
); /*concurrent reads */
437 cntr
= ACCESS_ONCE(desc
->length
);
441 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
445 if (test_bit(WDM_OVERFLOW
, &desc
->flags
)) {
446 clear_bit(WDM_OVERFLOW
, &desc
->flags
);
451 if (file
->f_flags
& O_NONBLOCK
) {
452 if (!test_bit(WDM_READ
, &desc
->flags
)) {
453 rv
= cntr
? cntr
: -EAGAIN
;
458 rv
= wait_event_interruptible(desc
->wait
,
459 test_bit(WDM_READ
, &desc
->flags
));
462 /* may have happened while we slept */
463 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
467 if (test_bit(WDM_RESETTING
, &desc
->flags
)) {
471 usb_mark_last_busy(interface_to_usbdev(desc
->intf
));
477 spin_lock_irq(&desc
->iuspin
);
479 if (desc
->rerr
) { /* read completed, error happened */
481 spin_unlock_irq(&desc
->iuspin
);
486 * recheck whether we've lost the race
487 * against the completion handler
489 if (!test_bit(WDM_READ
, &desc
->flags
)) { /* lost race */
490 spin_unlock_irq(&desc
->iuspin
);
494 if (!desc
->reslength
) { /* zero length read */
495 dev_dbg(&desc
->intf
->dev
, "%s: zero length - clearing WDM_READ\n", __func__
);
496 clear_bit(WDM_READ
, &desc
->flags
);
497 spin_unlock_irq(&desc
->iuspin
);
501 spin_unlock_irq(&desc
->iuspin
);
506 rv
= copy_to_user(buffer
, desc
->ubuf
, cntr
);
512 spin_lock_irq(&desc
->iuspin
);
514 for (i
= 0; i
< desc
->length
- cntr
; i
++)
515 desc
->ubuf
[i
] = desc
->ubuf
[i
+ cntr
];
517 desc
->length
-= cntr
;
518 /* in case we had outstanding data */
520 clear_bit(WDM_READ
, &desc
->flags
);
522 spin_unlock_irq(&desc
->iuspin
);
527 mutex_unlock(&desc
->rlock
);
531 static int wdm_flush(struct file
*file
, fl_owner_t id
)
533 struct wdm_device
*desc
= file
->private_data
;
535 wait_event(desc
->wait
, !test_bit(WDM_IN_USE
, &desc
->flags
));
537 /* cannot dereference desc->intf if WDM_DISCONNECTING */
538 if (desc
->werr
< 0 && !test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
539 dev_err(&desc
->intf
->dev
, "Error in flush path: %d\n",
543 return usb_translate_errors(desc
->werr
);
546 static unsigned int wdm_poll(struct file
*file
, struct poll_table_struct
*wait
)
548 struct wdm_device
*desc
= file
->private_data
;
550 unsigned int mask
= 0;
552 spin_lock_irqsave(&desc
->iuspin
, flags
);
553 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
554 mask
= POLLHUP
| POLLERR
;
555 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
558 if (test_bit(WDM_READ
, &desc
->flags
))
559 mask
= POLLIN
| POLLRDNORM
;
560 if (desc
->rerr
|| desc
->werr
)
562 if (!test_bit(WDM_IN_USE
, &desc
->flags
))
563 mask
|= POLLOUT
| POLLWRNORM
;
564 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
566 poll_wait(file
, &desc
->wait
, wait
);
572 static int wdm_open(struct inode
*inode
, struct file
*file
)
574 int minor
= iminor(inode
);
576 struct usb_interface
*intf
;
577 struct wdm_device
*desc
;
579 mutex_lock(&wdm_mutex
);
580 desc
= wdm_find_device_by_minor(minor
);
585 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
))
587 file
->private_data
= desc
;
589 rv
= usb_autopm_get_interface(desc
->intf
);
591 dev_err(&desc
->intf
->dev
, "Error autopm - %d\n", rv
);
595 /* using write lock to protect desc->count */
596 mutex_lock(&desc
->wlock
);
597 if (!desc
->count
++) {
600 rv
= usb_submit_urb(desc
->validity
, GFP_KERNEL
);
603 dev_err(&desc
->intf
->dev
,
604 "Error submitting int urb - %d\n", rv
);
609 mutex_unlock(&desc
->wlock
);
610 if (desc
->count
== 1)
611 desc
->manage_power(intf
, 1);
612 usb_autopm_put_interface(desc
->intf
);
614 mutex_unlock(&wdm_mutex
);
618 static int wdm_release(struct inode
*inode
, struct file
*file
)
620 struct wdm_device
*desc
= file
->private_data
;
622 mutex_lock(&wdm_mutex
);
624 /* using write lock to protect desc->count */
625 mutex_lock(&desc
->wlock
);
627 mutex_unlock(&desc
->wlock
);
630 if (!test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
631 dev_dbg(&desc
->intf
->dev
, "wdm_release: cleanup");
633 desc
->manage_power(desc
->intf
, 0);
635 /* must avoid dev_printk here as desc->intf is invalid */
636 pr_debug(KBUILD_MODNAME
" %s: device gone - cleaning up\n", __func__
);
640 mutex_unlock(&wdm_mutex
);
644 static const struct file_operations wdm_fops
= {
645 .owner
= THIS_MODULE
,
650 .release
= wdm_release
,
654 static struct usb_class_driver wdm_class
= {
657 .minor_base
= WDM_MINOR_BASE
,
660 /* --- error handling --- */
661 static void wdm_rxwork(struct work_struct
*work
)
663 struct wdm_device
*desc
= container_of(work
, struct wdm_device
, rxwork
);
667 spin_lock_irqsave(&desc
->iuspin
, flags
);
668 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
669 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
671 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
672 rv
= usb_submit_urb(desc
->response
, GFP_KERNEL
);
673 if (rv
< 0 && rv
!= -EPERM
) {
674 spin_lock_irqsave(&desc
->iuspin
, flags
);
675 if (!test_bit(WDM_DISCONNECTING
, &desc
->flags
))
676 schedule_work(&desc
->rxwork
);
677 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
682 /* --- hotplug --- */
684 static int wdm_create(struct usb_interface
*intf
, struct usb_endpoint_descriptor
*ep
,
685 u16 bufsize
, int (*manage_power
)(struct usb_interface
*, int))
688 struct wdm_device
*desc
;
690 desc
= kzalloc(sizeof(struct wdm_device
), GFP_KERNEL
);
693 INIT_LIST_HEAD(&desc
->device_list
);
694 mutex_init(&desc
->rlock
);
695 mutex_init(&desc
->wlock
);
696 spin_lock_init(&desc
->iuspin
);
697 init_waitqueue_head(&desc
->wait
);
698 desc
->wMaxCommand
= bufsize
;
699 /* this will be expanded and needed in hardware endianness */
700 desc
->inum
= cpu_to_le16((u16
)intf
->cur_altsetting
->desc
.bInterfaceNumber
);
702 INIT_WORK(&desc
->rxwork
, wdm_rxwork
);
705 if (!usb_endpoint_is_int_in(ep
))
708 desc
->wMaxPacketSize
= le16_to_cpu(ep
->wMaxPacketSize
);
710 desc
->orq
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_KERNEL
);
713 desc
->irq
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_KERNEL
);
717 desc
->validity
= usb_alloc_urb(0, GFP_KERNEL
);
721 desc
->response
= usb_alloc_urb(0, GFP_KERNEL
);
725 desc
->command
= usb_alloc_urb(0, GFP_KERNEL
);
729 desc
->ubuf
= kmalloc(desc
->wMaxCommand
, GFP_KERNEL
);
733 desc
->sbuf
= kmalloc(desc
->wMaxPacketSize
, GFP_KERNEL
);
737 desc
->inbuf
= kmalloc(desc
->wMaxCommand
, GFP_KERNEL
);
743 interface_to_usbdev(intf
),
744 usb_rcvintpipe(interface_to_usbdev(intf
), ep
->bEndpointAddress
),
746 desc
->wMaxPacketSize
,
752 desc
->irq
->bRequestType
= (USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
);
753 desc
->irq
->bRequest
= USB_CDC_GET_ENCAPSULATED_RESPONSE
;
754 desc
->irq
->wValue
= 0;
755 desc
->irq
->wIndex
= desc
->inum
;
756 desc
->irq
->wLength
= cpu_to_le16(desc
->wMaxCommand
);
758 usb_fill_control_urb(
760 interface_to_usbdev(intf
),
761 /* using common endpoint 0 */
762 usb_rcvctrlpipe(interface_to_usbdev(desc
->intf
), 0),
763 (unsigned char *)desc
->irq
,
770 desc
->manage_power
= manage_power
;
772 spin_lock(&wdm_device_list_lock
);
773 list_add(&desc
->device_list
, &wdm_device_list
);
774 spin_unlock(&wdm_device_list_lock
);
776 rv
= usb_register_dev(intf
, &wdm_class
);
780 dev_info(&intf
->dev
, "%s: USB WDM device\n", dev_name(intf
->usb_dev
));
784 spin_lock(&wdm_device_list_lock
);
785 list_del(&desc
->device_list
);
786 spin_unlock(&wdm_device_list_lock
);
791 static int wdm_manage_power(struct usb_interface
*intf
, int on
)
793 /* need autopm_get/put here to ensure the usbcore sees the new value */
794 int rv
= usb_autopm_get_interface(intf
);
798 intf
->needs_remote_wakeup
= on
;
799 usb_autopm_put_interface(intf
);
804 static int wdm_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
807 struct usb_host_interface
*iface
;
808 struct usb_endpoint_descriptor
*ep
;
809 struct usb_cdc_dmm_desc
*dmhd
;
810 u8
*buffer
= intf
->altsetting
->extra
;
811 int buflen
= intf
->altsetting
->extralen
;
812 u16 maxcom
= WDM_DEFAULT_BUFSIZE
;
817 if (buffer
[1] != USB_DT_CS_INTERFACE
) {
818 dev_err(&intf
->dev
, "skipping garbage\n");
823 case USB_CDC_HEADER_TYPE
:
825 case USB_CDC_DMM_TYPE
:
826 dmhd
= (struct usb_cdc_dmm_desc
*)buffer
;
827 maxcom
= le16_to_cpu(dmhd
->wMaxCommand
);
829 "Finding maximum buffer length: %d", maxcom
);
833 "Ignoring extra header, type %d, length %d\n",
834 buffer
[2], buffer
[0]);
842 iface
= intf
->cur_altsetting
;
843 if (iface
->desc
.bNumEndpoints
!= 1)
845 ep
= &iface
->endpoint
[0].desc
;
847 rv
= wdm_create(intf
, ep
, maxcom
, &wdm_manage_power
);
854 * usb_cdc_wdm_register - register a WDM subdriver
855 * @intf: usb interface the subdriver will associate with
856 * @ep: interrupt endpoint to monitor for notifications
857 * @bufsize: maximum message size to support for read/write
859 * Create WDM usb class character device and associate it with intf
860 * without binding, allowing another driver to manage the interface.
862 * The subdriver will manage the given interrupt endpoint exclusively
863 * and will issue control requests referring to the given intf. It
864 * will otherwise avoid interferring, and in particular not do
865 * usb_set_intfdata/usb_get_intfdata on intf.
867 * The return value is a pointer to the subdriver's struct usb_driver.
868 * The registering driver is responsible for calling this subdriver's
869 * disconnect, suspend, resume, pre_reset and post_reset methods from
872 struct usb_driver
*usb_cdc_wdm_register(struct usb_interface
*intf
,
873 struct usb_endpoint_descriptor
*ep
,
875 int (*manage_power
)(struct usb_interface
*, int))
879 rv
= wdm_create(intf
, ep
, bufsize
, manage_power
);
887 EXPORT_SYMBOL(usb_cdc_wdm_register
);
889 static void wdm_disconnect(struct usb_interface
*intf
)
891 struct wdm_device
*desc
;
894 usb_deregister_dev(intf
, &wdm_class
);
895 desc
= wdm_find_device(intf
);
896 mutex_lock(&wdm_mutex
);
898 /* the spinlock makes sure no new urbs are generated in the callbacks */
899 spin_lock_irqsave(&desc
->iuspin
, flags
);
900 set_bit(WDM_DISCONNECTING
, &desc
->flags
);
901 set_bit(WDM_READ
, &desc
->flags
);
902 /* to terminate pending flushes */
903 clear_bit(WDM_IN_USE
, &desc
->flags
);
904 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
905 wake_up_all(&desc
->wait
);
906 mutex_lock(&desc
->rlock
);
907 mutex_lock(&desc
->wlock
);
909 cancel_work_sync(&desc
->rxwork
);
910 mutex_unlock(&desc
->wlock
);
911 mutex_unlock(&desc
->rlock
);
913 /* the desc->intf pointer used as list key is now invalid */
914 spin_lock(&wdm_device_list_lock
);
915 list_del(&desc
->device_list
);
916 spin_unlock(&wdm_device_list_lock
);
920 mutex_unlock(&wdm_mutex
);
924 static int wdm_suspend(struct usb_interface
*intf
, pm_message_t message
)
926 struct wdm_device
*desc
= wdm_find_device(intf
);
929 dev_dbg(&desc
->intf
->dev
, "wdm%d_suspend\n", intf
->minor
);
931 /* if this is an autosuspend the caller does the locking */
932 if (!(message
.event
& PM_EVENT_AUTO
))
933 mutex_lock(&desc
->rlock
);
934 mutex_lock(&desc
->wlock
);
936 spin_lock_irq(&desc
->iuspin
);
938 if ((message
.event
& PM_EVENT_AUTO
) &&
939 (test_bit(WDM_IN_USE
, &desc
->flags
)
940 || test_bit(WDM_RESPONDING
, &desc
->flags
))) {
941 spin_unlock_irq(&desc
->iuspin
);
945 set_bit(WDM_SUSPENDING
, &desc
->flags
);
946 spin_unlock_irq(&desc
->iuspin
);
947 /* callback submits work - order is essential */
949 cancel_work_sync(&desc
->rxwork
);
951 if (!(message
.event
& PM_EVENT_AUTO
))
952 mutex_unlock(&desc
->wlock
);
953 mutex_unlock(&desc
->rlock
);
960 static int recover_from_urb_loss(struct wdm_device
*desc
)
965 rv
= usb_submit_urb(desc
->validity
, GFP_NOIO
);
967 dev_err(&desc
->intf
->dev
,
968 "Error resume submitting int urb - %d\n", rv
);
974 static int wdm_resume(struct usb_interface
*intf
)
976 struct wdm_device
*desc
= wdm_find_device(intf
);
979 dev_dbg(&desc
->intf
->dev
, "wdm%d_resume\n", intf
->minor
);
981 clear_bit(WDM_SUSPENDING
, &desc
->flags
);
982 rv
= recover_from_urb_loss(desc
);
988 static int wdm_pre_reset(struct usb_interface
*intf
)
990 struct wdm_device
*desc
= wdm_find_device(intf
);
993 * we notify everybody using poll of
994 * an exceptional situation
995 * must be done before recovery lest a spontaneous
996 * message from the device is lost
998 spin_lock_irq(&desc
->iuspin
);
999 set_bit(WDM_RESETTING
, &desc
->flags
); /* inform read/write */
1000 set_bit(WDM_READ
, &desc
->flags
); /* unblock read */
1001 clear_bit(WDM_IN_USE
, &desc
->flags
); /* unblock write */
1002 desc
->rerr
= -EINTR
;
1003 spin_unlock_irq(&desc
->iuspin
);
1004 wake_up_all(&desc
->wait
);
1005 mutex_lock(&desc
->rlock
);
1006 mutex_lock(&desc
->wlock
);
1008 cancel_work_sync(&desc
->rxwork
);
1012 static int wdm_post_reset(struct usb_interface
*intf
)
1014 struct wdm_device
*desc
= wdm_find_device(intf
);
1017 clear_bit(WDM_OVERFLOW
, &desc
->flags
);
1018 clear_bit(WDM_RESETTING
, &desc
->flags
);
1019 rv
= recover_from_urb_loss(desc
);
1020 mutex_unlock(&desc
->wlock
);
1021 mutex_unlock(&desc
->rlock
);
1025 static struct usb_driver wdm_driver
= {
1028 .disconnect
= wdm_disconnect
,
1030 .suspend
= wdm_suspend
,
1031 .resume
= wdm_resume
,
1032 .reset_resume
= wdm_resume
,
1034 .pre_reset
= wdm_pre_reset
,
1035 .post_reset
= wdm_post_reset
,
1036 .id_table
= wdm_ids
,
1037 .supports_autosuspend
= 1,
1040 /* --- low level module stuff --- */
1042 static int __init
wdm_init(void)
1046 rv
= usb_register(&wdm_driver
);
1051 static void __exit
wdm_exit(void)
1053 usb_deregister(&wdm_driver
);
1056 module_init(wdm_init
);
1057 module_exit(wdm_exit
);
1059 MODULE_AUTHOR(DRIVER_AUTHOR
);
1060 MODULE_DESCRIPTION(DRIVER_DESC
);
1061 MODULE_LICENSE("GPL");