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 #define HUAWEI_VENDOR_ID 0x12D1
37 static const struct usb_device_id wdm_ids
[] = {
39 .match_flags
= USB_DEVICE_ID_MATCH_INT_CLASS
|
40 USB_DEVICE_ID_MATCH_INT_SUBCLASS
,
41 .bInterfaceClass
= USB_CLASS_COMM
,
42 .bInterfaceSubClass
= USB_CDC_SUBCLASS_DMM
46 * Huawei E392, E398 and possibly other Qualcomm based modems
47 * embed the Qualcomm QMI protocol inside CDC on CDC ECM like
48 * control interfaces. Userspace access to this is required
49 * to configure the accompanying data interface
51 .match_flags
= USB_DEVICE_ID_MATCH_VENDOR
|
52 USB_DEVICE_ID_MATCH_INT_INFO
,
53 .idVendor
= HUAWEI_VENDOR_ID
,
54 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
55 .bInterfaceSubClass
= 1,
56 .bInterfaceProtocol
= 9, /* NOTE: CDC ECM control interface! */
61 MODULE_DEVICE_TABLE (usb
, wdm_ids
);
63 #define WDM_MINOR_BASE 176
67 #define WDM_DISCONNECTING 2
70 #define WDM_INT_STALL 5
71 #define WDM_POLL_RUNNING 6
72 #define WDM_RESPONDING 7
73 #define WDM_SUSPENDING 8
74 #define WDM_RESETTING 9
78 /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
79 #define WDM_DEFAULT_BUFSIZE 256
81 static DEFINE_MUTEX(wdm_mutex
);
82 static DEFINE_SPINLOCK(wdm_device_list_lock
);
83 static LIST_HEAD(wdm_device_list
);
85 /* --- method tables --- */
88 u8
*inbuf
; /* buffer for response */
89 u8
*outbuf
; /* buffer for command */
90 u8
*sbuf
; /* buffer for status */
91 u8
*ubuf
; /* buffer for copy to user space */
96 struct usb_interface
*intf
;
97 struct usb_ctrlrequest
*orq
;
98 struct usb_ctrlrequest
*irq
;
114 wait_queue_head_t wait
;
115 struct work_struct rxwork
;
119 struct list_head device_list
;
120 int (*manage_power
)(struct usb_interface
*, int);
123 static struct usb_driver wdm_driver
;
125 /* return intfdata if we own the interface, else look up intf in the list */
126 static struct wdm_device
*wdm_find_device(struct usb_interface
*intf
)
128 struct wdm_device
*desc
= NULL
;
130 spin_lock(&wdm_device_list_lock
);
131 list_for_each_entry(desc
, &wdm_device_list
, device_list
)
132 if (desc
->intf
== intf
)
134 spin_unlock(&wdm_device_list_lock
);
139 static struct wdm_device
*wdm_find_device_by_minor(int minor
)
141 struct wdm_device
*desc
= NULL
;
143 spin_lock(&wdm_device_list_lock
);
144 list_for_each_entry(desc
, &wdm_device_list
, device_list
)
145 if (desc
->intf
->minor
== minor
)
147 spin_unlock(&wdm_device_list_lock
);
152 /* --- callbacks --- */
153 static void wdm_out_callback(struct urb
*urb
)
155 struct wdm_device
*desc
;
157 spin_lock(&desc
->iuspin
);
158 desc
->werr
= urb
->status
;
159 spin_unlock(&desc
->iuspin
);
162 clear_bit(WDM_IN_USE
, &desc
->flags
);
163 wake_up(&desc
->wait
);
166 static void wdm_in_callback(struct urb
*urb
)
168 struct wdm_device
*desc
= urb
->context
;
169 int status
= urb
->status
;
171 spin_lock(&desc
->iuspin
);
172 clear_bit(WDM_RESPONDING
, &desc
->flags
);
177 dev_dbg(&desc
->intf
->dev
,
178 "nonzero urb status received: -ENOENT");
181 dev_dbg(&desc
->intf
->dev
,
182 "nonzero urb status received: -ECONNRESET");
185 dev_dbg(&desc
->intf
->dev
,
186 "nonzero urb status received: -ESHUTDOWN");
189 dev_err(&desc
->intf
->dev
,
190 "nonzero urb status received: -EPIPE\n");
193 dev_err(&desc
->intf
->dev
,
194 "Unexpected error %d\n", status
);
200 desc
->reslength
= urb
->actual_length
;
201 memmove(desc
->ubuf
+ desc
->length
, desc
->inbuf
, desc
->reslength
);
202 desc
->length
+= desc
->reslength
;
204 wake_up(&desc
->wait
);
206 set_bit(WDM_READ
, &desc
->flags
);
207 spin_unlock(&desc
->iuspin
);
210 static void wdm_int_callback(struct urb
*urb
)
213 int status
= urb
->status
;
214 struct wdm_device
*desc
;
215 struct usb_cdc_notification
*dr
;
218 dr
= (struct usb_cdc_notification
*)desc
->sbuf
;
227 set_bit(WDM_INT_STALL
, &desc
->flags
);
228 dev_err(&desc
->intf
->dev
, "Stall on int endpoint\n");
229 goto sw
; /* halt is cleared in work */
231 dev_err(&desc
->intf
->dev
,
232 "nonzero urb status received: %d\n", status
);
237 if (urb
->actual_length
< sizeof(struct usb_cdc_notification
)) {
238 dev_err(&desc
->intf
->dev
, "wdm_int_callback - %d bytes\n",
243 switch (dr
->bNotificationType
) {
244 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE
:
245 dev_dbg(&desc
->intf
->dev
,
246 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
247 dr
->wIndex
, dr
->wLength
);
250 case USB_CDC_NOTIFY_NETWORK_CONNECTION
:
252 dev_dbg(&desc
->intf
->dev
,
253 "NOTIFY_NETWORK_CONNECTION %s network",
254 dr
->wValue
? "connected to" : "disconnected from");
257 clear_bit(WDM_POLL_RUNNING
, &desc
->flags
);
258 dev_err(&desc
->intf
->dev
,
259 "unknown notification %d received: index %d len %d\n",
260 dr
->bNotificationType
, dr
->wIndex
, dr
->wLength
);
264 spin_lock(&desc
->iuspin
);
265 clear_bit(WDM_READ
, &desc
->flags
);
266 set_bit(WDM_RESPONDING
, &desc
->flags
);
267 if (!test_bit(WDM_DISCONNECTING
, &desc
->flags
)
268 && !test_bit(WDM_SUSPENDING
, &desc
->flags
)) {
269 rv
= usb_submit_urb(desc
->response
, GFP_ATOMIC
);
270 dev_dbg(&desc
->intf
->dev
, "%s: usb_submit_urb %d",
273 spin_unlock(&desc
->iuspin
);
275 clear_bit(WDM_RESPONDING
, &desc
->flags
);
280 rv
= schedule_work(&desc
->rxwork
);
282 dev_err(&desc
->intf
->dev
,
283 "Cannot schedule work\n");
287 rv
= usb_submit_urb(urb
, GFP_ATOMIC
);
289 dev_err(&desc
->intf
->dev
,
290 "%s - usb_submit_urb failed with result %d\n",
295 static void kill_urbs(struct wdm_device
*desc
)
297 /* the order here is essential */
298 usb_kill_urb(desc
->command
);
299 usb_kill_urb(desc
->validity
);
300 usb_kill_urb(desc
->response
);
303 static void free_urbs(struct wdm_device
*desc
)
305 usb_free_urb(desc
->validity
);
306 usb_free_urb(desc
->response
);
307 usb_free_urb(desc
->command
);
310 static void cleanup(struct wdm_device
*desc
)
312 spin_lock(&wdm_device_list_lock
);
313 list_del(&desc
->device_list
);
314 spin_unlock(&wdm_device_list_lock
);
324 static ssize_t wdm_write
325 (struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
328 int rv
= -EMSGSIZE
, r
, we
;
329 struct wdm_device
*desc
= file
->private_data
;
330 struct usb_ctrlrequest
*req
;
332 if (count
> desc
->wMaxCommand
)
333 count
= desc
->wMaxCommand
;
335 spin_lock_irq(&desc
->iuspin
);
338 spin_unlock_irq(&desc
->iuspin
);
342 buf
= kmalloc(count
, GFP_KERNEL
);
348 r
= copy_from_user(buf
, buffer
, count
);
355 /* concurrent writes and disconnect */
356 r
= mutex_lock_interruptible(&desc
->wlock
);
363 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
369 r
= usb_autopm_get_interface(desc
->intf
);
375 if (!(file
->f_flags
& O_NONBLOCK
))
376 r
= wait_event_interruptible(desc
->wait
, !test_bit(WDM_IN_USE
,
379 if (test_bit(WDM_IN_USE
, &desc
->flags
))
382 if (test_bit(WDM_RESETTING
, &desc
->flags
))
391 usb_fill_control_urb(
393 interface_to_usbdev(desc
->intf
),
394 /* using common endpoint 0 */
395 usb_sndctrlpipe(interface_to_usbdev(desc
->intf
), 0),
396 (unsigned char *)req
,
403 req
->bRequestType
= (USB_DIR_OUT
| USB_TYPE_CLASS
|
404 USB_RECIP_INTERFACE
);
405 req
->bRequest
= USB_CDC_SEND_ENCAPSULATED_COMMAND
;
407 req
->wIndex
= desc
->inum
;
408 req
->wLength
= cpu_to_le16(count
);
409 set_bit(WDM_IN_USE
, &desc
->flags
);
412 rv
= usb_submit_urb(desc
->command
, GFP_KERNEL
);
416 clear_bit(WDM_IN_USE
, &desc
->flags
);
417 dev_err(&desc
->intf
->dev
, "Tx URB error: %d\n", rv
);
419 dev_dbg(&desc
->intf
->dev
, "Tx URB has been submitted index=%d",
423 usb_autopm_put_interface(desc
->intf
);
425 mutex_unlock(&desc
->wlock
);
427 return rv
< 0 ? rv
: count
;
430 static ssize_t wdm_read
431 (struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
435 struct wdm_device
*desc
= file
->private_data
;
438 rv
= mutex_lock_interruptible(&desc
->rlock
); /*concurrent reads */
442 cntr
= ACCESS_ONCE(desc
->length
);
446 if (test_bit(WDM_DISCONNECTING
, &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
);
493 if (!desc
->reslength
) { /* zero length read */
494 spin_unlock_irq(&desc
->iuspin
);
498 spin_unlock_irq(&desc
->iuspin
);
503 rv
= copy_to_user(buffer
, desc
->ubuf
, cntr
);
509 spin_lock_irq(&desc
->iuspin
);
511 for (i
= 0; i
< desc
->length
- cntr
; i
++)
512 desc
->ubuf
[i
] = desc
->ubuf
[i
+ cntr
];
514 desc
->length
-= cntr
;
515 /* in case we had outstanding data */
517 clear_bit(WDM_READ
, &desc
->flags
);
519 spin_unlock_irq(&desc
->iuspin
);
524 mutex_unlock(&desc
->rlock
);
528 static int wdm_flush(struct file
*file
, fl_owner_t id
)
530 struct wdm_device
*desc
= file
->private_data
;
532 wait_event(desc
->wait
, !test_bit(WDM_IN_USE
, &desc
->flags
));
534 dev_err(&desc
->intf
->dev
, "Error in flush path: %d\n",
540 static unsigned int wdm_poll(struct file
*file
, struct poll_table_struct
*wait
)
542 struct wdm_device
*desc
= file
->private_data
;
544 unsigned int mask
= 0;
546 spin_lock_irqsave(&desc
->iuspin
, flags
);
547 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
549 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
552 if (test_bit(WDM_READ
, &desc
->flags
))
553 mask
= POLLIN
| POLLRDNORM
;
554 if (desc
->rerr
|| desc
->werr
)
556 if (!test_bit(WDM_IN_USE
, &desc
->flags
))
557 mask
|= POLLOUT
| POLLWRNORM
;
558 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
560 poll_wait(file
, &desc
->wait
, wait
);
566 static int wdm_open(struct inode
*inode
, struct file
*file
)
568 int minor
= iminor(inode
);
570 struct usb_interface
*intf
;
571 struct wdm_device
*desc
;
573 mutex_lock(&wdm_mutex
);
574 desc
= wdm_find_device_by_minor(minor
);
579 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
))
581 file
->private_data
= desc
;
583 rv
= usb_autopm_get_interface(desc
->intf
);
585 dev_err(&desc
->intf
->dev
, "Error autopm - %d\n", rv
);
589 /* using write lock to protect desc->count */
590 mutex_lock(&desc
->wlock
);
591 if (!desc
->count
++) {
594 rv
= usb_submit_urb(desc
->validity
, GFP_KERNEL
);
597 dev_err(&desc
->intf
->dev
,
598 "Error submitting int urb - %d\n", rv
);
603 mutex_unlock(&desc
->wlock
);
604 if (desc
->count
== 1)
605 desc
->manage_power(intf
, 1);
606 usb_autopm_put_interface(desc
->intf
);
608 mutex_unlock(&wdm_mutex
);
612 static int wdm_release(struct inode
*inode
, struct file
*file
)
614 struct wdm_device
*desc
= file
->private_data
;
616 mutex_lock(&wdm_mutex
);
618 /* using write lock to protect desc->count */
619 mutex_lock(&desc
->wlock
);
621 mutex_unlock(&desc
->wlock
);
624 dev_dbg(&desc
->intf
->dev
, "wdm_release: cleanup");
626 if (!test_bit(WDM_DISCONNECTING
, &desc
->flags
))
627 desc
->manage_power(desc
->intf
, 0);
629 mutex_unlock(&wdm_mutex
);
633 static const struct file_operations wdm_fops
= {
634 .owner
= THIS_MODULE
,
639 .release
= wdm_release
,
641 .llseek
= noop_llseek
,
644 static struct usb_class_driver wdm_class
= {
647 .minor_base
= WDM_MINOR_BASE
,
650 /* --- error handling --- */
651 static void wdm_rxwork(struct work_struct
*work
)
653 struct wdm_device
*desc
= container_of(work
, struct wdm_device
, rxwork
);
657 spin_lock_irqsave(&desc
->iuspin
, flags
);
658 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
659 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
661 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
662 rv
= usb_submit_urb(desc
->response
, GFP_KERNEL
);
663 if (rv
< 0 && rv
!= -EPERM
) {
664 spin_lock_irqsave(&desc
->iuspin
, flags
);
665 if (!test_bit(WDM_DISCONNECTING
, &desc
->flags
))
666 schedule_work(&desc
->rxwork
);
667 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
672 /* --- hotplug --- */
674 static int wdm_create(struct usb_interface
*intf
, struct usb_endpoint_descriptor
*ep
,
675 u16 bufsize
, int (*manage_power
)(struct usb_interface
*, int))
678 struct wdm_device
*desc
;
680 desc
= kzalloc(sizeof(struct wdm_device
), GFP_KERNEL
);
683 INIT_LIST_HEAD(&desc
->device_list
);
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
= bufsize
;
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 if (!usb_endpoint_is_int_in(ep
))
698 desc
->wMaxPacketSize
= usb_endpoint_maxp(ep
);
700 desc
->orq
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_KERNEL
);
703 desc
->irq
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_KERNEL
);
707 desc
->validity
= usb_alloc_urb(0, GFP_KERNEL
);
711 desc
->response
= usb_alloc_urb(0, GFP_KERNEL
);
715 desc
->command
= usb_alloc_urb(0, GFP_KERNEL
);
719 desc
->ubuf
= kmalloc(desc
->wMaxCommand
, GFP_KERNEL
);
723 desc
->sbuf
= kmalloc(desc
->wMaxPacketSize
, GFP_KERNEL
);
727 desc
->inbuf
= kmalloc(desc
->wMaxCommand
, GFP_KERNEL
);
733 interface_to_usbdev(intf
),
734 usb_rcvintpipe(interface_to_usbdev(intf
), ep
->bEndpointAddress
),
736 desc
->wMaxPacketSize
,
742 desc
->irq
->bRequestType
= (USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
);
743 desc
->irq
->bRequest
= USB_CDC_GET_ENCAPSULATED_RESPONSE
;
744 desc
->irq
->wValue
= 0;
745 desc
->irq
->wIndex
= desc
->inum
;
746 desc
->irq
->wLength
= cpu_to_le16(desc
->wMaxCommand
);
748 usb_fill_control_urb(
750 interface_to_usbdev(intf
),
751 /* using common endpoint 0 */
752 usb_rcvctrlpipe(interface_to_usbdev(desc
->intf
), 0),
753 (unsigned char *)desc
->irq
,
760 desc
->manage_power
= manage_power
;
762 spin_lock(&wdm_device_list_lock
);
763 list_add(&desc
->device_list
, &wdm_device_list
);
764 spin_unlock(&wdm_device_list_lock
);
766 rv
= usb_register_dev(intf
, &wdm_class
);
770 dev_info(&intf
->dev
, "%s: USB WDM device\n", dev_name(intf
->usb_dev
));
778 static int wdm_manage_power(struct usb_interface
*intf
, int on
)
780 /* need autopm_get/put here to ensure the usbcore sees the new value */
781 int rv
= usb_autopm_get_interface(intf
);
785 intf
->needs_remote_wakeup
= on
;
786 usb_autopm_put_interface(intf
);
791 static int wdm_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
794 struct usb_host_interface
*iface
;
795 struct usb_endpoint_descriptor
*ep
;
796 struct usb_cdc_dmm_desc
*dmhd
;
797 u8
*buffer
= intf
->altsetting
->extra
;
798 int buflen
= intf
->altsetting
->extralen
;
799 u16 maxcom
= WDM_DEFAULT_BUFSIZE
;
804 if (buffer
[1] != USB_DT_CS_INTERFACE
) {
805 dev_err(&intf
->dev
, "skipping garbage\n");
810 case USB_CDC_HEADER_TYPE
:
812 case USB_CDC_DMM_TYPE
:
813 dmhd
= (struct usb_cdc_dmm_desc
*)buffer
;
814 maxcom
= le16_to_cpu(dmhd
->wMaxCommand
);
816 "Finding maximum buffer length: %d", maxcom
);
820 "Ignoring extra header, type %d, length %d\n",
821 buffer
[2], buffer
[0]);
829 iface
= intf
->cur_altsetting
;
830 if (iface
->desc
.bNumEndpoints
!= 1)
832 ep
= &iface
->endpoint
[0].desc
;
834 rv
= wdm_create(intf
, ep
, maxcom
, &wdm_manage_power
);
841 * usb_cdc_wdm_register - register a WDM subdriver
842 * @intf: usb interface the subdriver will associate with
843 * @ep: interrupt endpoint to monitor for notifications
844 * @bufsize: maximum message size to support for read/write
846 * Create WDM usb class character device and associate it with intf
847 * without binding, allowing another driver to manage the interface.
849 * The subdriver will manage the given interrupt endpoint exclusively
850 * and will issue control requests referring to the given intf. It
851 * will otherwise avoid interferring, and in particular not do
852 * usb_set_intfdata/usb_get_intfdata on intf.
854 * The return value is a pointer to the subdriver's struct usb_driver.
855 * The registering driver is responsible for calling this subdriver's
856 * disconnect, suspend, resume, pre_reset and post_reset methods from
859 struct usb_driver
*usb_cdc_wdm_register(struct usb_interface
*intf
,
860 struct usb_endpoint_descriptor
*ep
,
862 int (*manage_power
)(struct usb_interface
*, int))
866 rv
= wdm_create(intf
, ep
, bufsize
, manage_power
);
874 EXPORT_SYMBOL(usb_cdc_wdm_register
);
876 static void wdm_disconnect(struct usb_interface
*intf
)
878 struct wdm_device
*desc
;
881 usb_deregister_dev(intf
, &wdm_class
);
882 desc
= wdm_find_device(intf
);
883 mutex_lock(&wdm_mutex
);
885 /* the spinlock makes sure no new urbs are generated in the callbacks */
886 spin_lock_irqsave(&desc
->iuspin
, flags
);
887 set_bit(WDM_DISCONNECTING
, &desc
->flags
);
888 set_bit(WDM_READ
, &desc
->flags
);
889 /* to terminate pending flushes */
890 clear_bit(WDM_IN_USE
, &desc
->flags
);
891 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
892 wake_up_all(&desc
->wait
);
893 mutex_lock(&desc
->rlock
);
894 mutex_lock(&desc
->wlock
);
896 cancel_work_sync(&desc
->rxwork
);
897 mutex_unlock(&desc
->wlock
);
898 mutex_unlock(&desc
->rlock
);
901 mutex_unlock(&wdm_mutex
);
905 static int wdm_suspend(struct usb_interface
*intf
, pm_message_t message
)
907 struct wdm_device
*desc
= wdm_find_device(intf
);
910 dev_dbg(&desc
->intf
->dev
, "wdm%d_suspend\n", intf
->minor
);
912 /* if this is an autosuspend the caller does the locking */
913 if (!PMSG_IS_AUTO(message
)) {
914 mutex_lock(&desc
->rlock
);
915 mutex_lock(&desc
->wlock
);
917 spin_lock_irq(&desc
->iuspin
);
919 if (PMSG_IS_AUTO(message
) &&
920 (test_bit(WDM_IN_USE
, &desc
->flags
)
921 || test_bit(WDM_RESPONDING
, &desc
->flags
))) {
922 spin_unlock_irq(&desc
->iuspin
);
926 set_bit(WDM_SUSPENDING
, &desc
->flags
);
927 spin_unlock_irq(&desc
->iuspin
);
928 /* callback submits work - order is essential */
930 cancel_work_sync(&desc
->rxwork
);
932 if (!PMSG_IS_AUTO(message
)) {
933 mutex_unlock(&desc
->wlock
);
934 mutex_unlock(&desc
->rlock
);
941 static int recover_from_urb_loss(struct wdm_device
*desc
)
946 rv
= usb_submit_urb(desc
->validity
, GFP_NOIO
);
948 dev_err(&desc
->intf
->dev
,
949 "Error resume submitting int urb - %d\n", rv
);
955 static int wdm_resume(struct usb_interface
*intf
)
957 struct wdm_device
*desc
= wdm_find_device(intf
);
960 dev_dbg(&desc
->intf
->dev
, "wdm%d_resume\n", intf
->minor
);
962 clear_bit(WDM_SUSPENDING
, &desc
->flags
);
963 rv
= recover_from_urb_loss(desc
);
969 static int wdm_pre_reset(struct usb_interface
*intf
)
971 struct wdm_device
*desc
= wdm_find_device(intf
);
974 * we notify everybody using poll of
975 * an exceptional situation
976 * must be done before recovery lest a spontaneous
977 * message from the device is lost
979 spin_lock_irq(&desc
->iuspin
);
980 set_bit(WDM_RESETTING
, &desc
->flags
); /* inform read/write */
981 set_bit(WDM_READ
, &desc
->flags
); /* unblock read */
982 clear_bit(WDM_IN_USE
, &desc
->flags
); /* unblock write */
984 spin_unlock_irq(&desc
->iuspin
);
985 wake_up_all(&desc
->wait
);
986 mutex_lock(&desc
->rlock
);
987 mutex_lock(&desc
->wlock
);
989 cancel_work_sync(&desc
->rxwork
);
993 static int wdm_post_reset(struct usb_interface
*intf
)
995 struct wdm_device
*desc
= wdm_find_device(intf
);
998 clear_bit(WDM_RESETTING
, &desc
->flags
);
999 rv
= recover_from_urb_loss(desc
);
1000 mutex_unlock(&desc
->wlock
);
1001 mutex_unlock(&desc
->rlock
);
1005 static struct usb_driver wdm_driver
= {
1008 .disconnect
= wdm_disconnect
,
1010 .suspend
= wdm_suspend
,
1011 .resume
= wdm_resume
,
1012 .reset_resume
= wdm_resume
,
1014 .pre_reset
= wdm_pre_reset
,
1015 .post_reset
= wdm_post_reset
,
1016 .id_table
= wdm_ids
,
1017 .supports_autosuspend
= 1,
1020 module_usb_driver(wdm_driver
);
1022 MODULE_AUTHOR(DRIVER_AUTHOR
);
1023 MODULE_DESCRIPTION(DRIVER_DESC
);
1024 MODULE_LICENSE("GPL");