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! */
59 /* Vodafone/Huawei K5005 (12d1:14c8) and similar modems */
60 .match_flags
= USB_DEVICE_ID_MATCH_VENDOR
|
61 USB_DEVICE_ID_MATCH_INT_INFO
,
62 .idVendor
= HUAWEI_VENDOR_ID
,
63 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
64 .bInterfaceSubClass
= 1,
65 .bInterfaceProtocol
= 57, /* NOTE: CDC ECM control interface! */
70 MODULE_DEVICE_TABLE (usb
, wdm_ids
);
72 #define WDM_MINOR_BASE 176
76 #define WDM_DISCONNECTING 2
79 #define WDM_INT_STALL 5
80 #define WDM_POLL_RUNNING 6
81 #define WDM_RESPONDING 7
82 #define WDM_SUSPENDING 8
83 #define WDM_RESETTING 9
87 /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
88 #define WDM_DEFAULT_BUFSIZE 256
90 static DEFINE_MUTEX(wdm_mutex
);
91 static DEFINE_SPINLOCK(wdm_device_list_lock
);
92 static LIST_HEAD(wdm_device_list
);
94 /* --- method tables --- */
97 u8
*inbuf
; /* buffer for response */
98 u8
*outbuf
; /* buffer for command */
99 u8
*sbuf
; /* buffer for status */
100 u8
*ubuf
; /* buffer for copy to user space */
103 struct urb
*response
;
104 struct urb
*validity
;
105 struct usb_interface
*intf
;
106 struct usb_ctrlrequest
*orq
;
107 struct usb_ctrlrequest
*irq
;
123 wait_queue_head_t wait
;
124 struct work_struct rxwork
;
128 struct list_head device_list
;
129 int (*manage_power
)(struct usb_interface
*, int);
132 static struct usb_driver wdm_driver
;
134 /* return intfdata if we own the interface, else look up intf in the list */
135 static struct wdm_device
*wdm_find_device(struct usb_interface
*intf
)
137 struct wdm_device
*desc
= NULL
;
139 spin_lock(&wdm_device_list_lock
);
140 list_for_each_entry(desc
, &wdm_device_list
, device_list
)
141 if (desc
->intf
== intf
)
143 spin_unlock(&wdm_device_list_lock
);
148 static struct wdm_device
*wdm_find_device_by_minor(int minor
)
150 struct wdm_device
*desc
= NULL
;
152 spin_lock(&wdm_device_list_lock
);
153 list_for_each_entry(desc
, &wdm_device_list
, device_list
)
154 if (desc
->intf
->minor
== minor
)
156 spin_unlock(&wdm_device_list_lock
);
161 /* --- callbacks --- */
162 static void wdm_out_callback(struct urb
*urb
)
164 struct wdm_device
*desc
;
166 spin_lock(&desc
->iuspin
);
167 desc
->werr
= urb
->status
;
168 spin_unlock(&desc
->iuspin
);
171 clear_bit(WDM_IN_USE
, &desc
->flags
);
172 wake_up(&desc
->wait
);
175 static void wdm_in_callback(struct urb
*urb
)
177 struct wdm_device
*desc
= urb
->context
;
178 int status
= urb
->status
;
180 spin_lock(&desc
->iuspin
);
181 clear_bit(WDM_RESPONDING
, &desc
->flags
);
186 dev_dbg(&desc
->intf
->dev
,
187 "nonzero urb status received: -ENOENT");
190 dev_dbg(&desc
->intf
->dev
,
191 "nonzero urb status received: -ECONNRESET");
194 dev_dbg(&desc
->intf
->dev
,
195 "nonzero urb status received: -ESHUTDOWN");
198 dev_err(&desc
->intf
->dev
,
199 "nonzero urb status received: -EPIPE\n");
202 dev_err(&desc
->intf
->dev
,
203 "Unexpected error %d\n", status
);
209 desc
->reslength
= urb
->actual_length
;
210 memmove(desc
->ubuf
+ desc
->length
, desc
->inbuf
, desc
->reslength
);
211 desc
->length
+= desc
->reslength
;
213 wake_up(&desc
->wait
);
215 set_bit(WDM_READ
, &desc
->flags
);
216 spin_unlock(&desc
->iuspin
);
219 static void wdm_int_callback(struct urb
*urb
)
222 int status
= urb
->status
;
223 struct wdm_device
*desc
;
224 struct usb_cdc_notification
*dr
;
227 dr
= (struct usb_cdc_notification
*)desc
->sbuf
;
236 set_bit(WDM_INT_STALL
, &desc
->flags
);
237 dev_err(&desc
->intf
->dev
, "Stall on int endpoint\n");
238 goto sw
; /* halt is cleared in work */
240 dev_err(&desc
->intf
->dev
,
241 "nonzero urb status received: %d\n", status
);
246 if (urb
->actual_length
< sizeof(struct usb_cdc_notification
)) {
247 dev_err(&desc
->intf
->dev
, "wdm_int_callback - %d bytes\n",
252 switch (dr
->bNotificationType
) {
253 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE
:
254 dev_dbg(&desc
->intf
->dev
,
255 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
256 dr
->wIndex
, dr
->wLength
);
259 case USB_CDC_NOTIFY_NETWORK_CONNECTION
:
261 dev_dbg(&desc
->intf
->dev
,
262 "NOTIFY_NETWORK_CONNECTION %s network",
263 dr
->wValue
? "connected to" : "disconnected from");
266 clear_bit(WDM_POLL_RUNNING
, &desc
->flags
);
267 dev_err(&desc
->intf
->dev
,
268 "unknown notification %d received: index %d len %d\n",
269 dr
->bNotificationType
, dr
->wIndex
, dr
->wLength
);
273 spin_lock(&desc
->iuspin
);
274 clear_bit(WDM_READ
, &desc
->flags
);
275 set_bit(WDM_RESPONDING
, &desc
->flags
);
276 if (!test_bit(WDM_DISCONNECTING
, &desc
->flags
)
277 && !test_bit(WDM_SUSPENDING
, &desc
->flags
)) {
278 rv
= usb_submit_urb(desc
->response
, GFP_ATOMIC
);
279 dev_dbg(&desc
->intf
->dev
, "%s: usb_submit_urb %d",
282 spin_unlock(&desc
->iuspin
);
284 clear_bit(WDM_RESPONDING
, &desc
->flags
);
289 rv
= schedule_work(&desc
->rxwork
);
291 dev_err(&desc
->intf
->dev
,
292 "Cannot schedule work\n");
296 rv
= usb_submit_urb(urb
, GFP_ATOMIC
);
298 dev_err(&desc
->intf
->dev
,
299 "%s - usb_submit_urb failed with result %d\n",
304 static void kill_urbs(struct wdm_device
*desc
)
306 /* the order here is essential */
307 usb_kill_urb(desc
->command
);
308 usb_kill_urb(desc
->validity
);
309 usb_kill_urb(desc
->response
);
312 static void free_urbs(struct wdm_device
*desc
)
314 usb_free_urb(desc
->validity
);
315 usb_free_urb(desc
->response
);
316 usb_free_urb(desc
->command
);
319 static void cleanup(struct wdm_device
*desc
)
330 static ssize_t wdm_write
331 (struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
334 int rv
= -EMSGSIZE
, r
, we
;
335 struct wdm_device
*desc
= file
->private_data
;
336 struct usb_ctrlrequest
*req
;
338 if (count
> desc
->wMaxCommand
)
339 count
= desc
->wMaxCommand
;
341 spin_lock_irq(&desc
->iuspin
);
344 spin_unlock_irq(&desc
->iuspin
);
348 buf
= kmalloc(count
, GFP_KERNEL
);
354 r
= copy_from_user(buf
, buffer
, count
);
361 /* concurrent writes and disconnect */
362 r
= mutex_lock_interruptible(&desc
->wlock
);
369 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
375 r
= usb_autopm_get_interface(desc
->intf
);
378 rv
= usb_translate_errors(r
);
382 if (!(file
->f_flags
& O_NONBLOCK
))
383 r
= wait_event_interruptible(desc
->wait
, !test_bit(WDM_IN_USE
,
386 if (test_bit(WDM_IN_USE
, &desc
->flags
))
389 if (test_bit(WDM_RESETTING
, &desc
->flags
))
399 usb_fill_control_urb(
401 interface_to_usbdev(desc
->intf
),
402 /* using common endpoint 0 */
403 usb_sndctrlpipe(interface_to_usbdev(desc
->intf
), 0),
404 (unsigned char *)req
,
411 req
->bRequestType
= (USB_DIR_OUT
| USB_TYPE_CLASS
|
412 USB_RECIP_INTERFACE
);
413 req
->bRequest
= USB_CDC_SEND_ENCAPSULATED_COMMAND
;
415 req
->wIndex
= desc
->inum
;
416 req
->wLength
= cpu_to_le16(count
);
417 set_bit(WDM_IN_USE
, &desc
->flags
);
420 rv
= usb_submit_urb(desc
->command
, GFP_KERNEL
);
424 clear_bit(WDM_IN_USE
, &desc
->flags
);
425 dev_err(&desc
->intf
->dev
, "Tx URB error: %d\n", rv
);
426 rv
= usb_translate_errors(rv
);
428 dev_dbg(&desc
->intf
->dev
, "Tx URB has been submitted index=%d",
432 usb_autopm_put_interface(desc
->intf
);
434 mutex_unlock(&desc
->wlock
);
436 return rv
< 0 ? rv
: count
;
439 static ssize_t wdm_read
440 (struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
444 struct wdm_device
*desc
= file
->private_data
;
447 rv
= mutex_lock_interruptible(&desc
->rlock
); /*concurrent reads */
451 cntr
= ACCESS_ONCE(desc
->length
);
455 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
460 if (file
->f_flags
& O_NONBLOCK
) {
461 if (!test_bit(WDM_READ
, &desc
->flags
)) {
462 rv
= cntr
? cntr
: -EAGAIN
;
467 rv
= wait_event_interruptible(desc
->wait
,
468 test_bit(WDM_READ
, &desc
->flags
));
471 /* may have happened while we slept */
472 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
476 if (test_bit(WDM_RESETTING
, &desc
->flags
)) {
480 usb_mark_last_busy(interface_to_usbdev(desc
->intf
));
486 spin_lock_irq(&desc
->iuspin
);
488 if (desc
->rerr
) { /* read completed, error happened */
490 spin_unlock_irq(&desc
->iuspin
);
495 * recheck whether we've lost the race
496 * against the completion handler
498 if (!test_bit(WDM_READ
, &desc
->flags
)) { /* lost race */
499 spin_unlock_irq(&desc
->iuspin
);
502 if (!desc
->reslength
) { /* zero length read */
503 dev_dbg(&desc
->intf
->dev
, "%s: zero length - clearing WDM_READ\n", __func__
);
504 clear_bit(WDM_READ
, &desc
->flags
);
505 spin_unlock_irq(&desc
->iuspin
);
509 spin_unlock_irq(&desc
->iuspin
);
514 rv
= copy_to_user(buffer
, desc
->ubuf
, cntr
);
520 spin_lock_irq(&desc
->iuspin
);
522 for (i
= 0; i
< desc
->length
- cntr
; i
++)
523 desc
->ubuf
[i
] = desc
->ubuf
[i
+ cntr
];
525 desc
->length
-= cntr
;
526 /* in case we had outstanding data */
528 clear_bit(WDM_READ
, &desc
->flags
);
530 spin_unlock_irq(&desc
->iuspin
);
535 mutex_unlock(&desc
->rlock
);
539 static int wdm_flush(struct file
*file
, fl_owner_t id
)
541 struct wdm_device
*desc
= file
->private_data
;
543 wait_event(desc
->wait
, !test_bit(WDM_IN_USE
, &desc
->flags
));
545 /* cannot dereference desc->intf if WDM_DISCONNECTING */
546 if (desc
->werr
< 0 && !test_bit(WDM_DISCONNECTING
, &desc
->flags
))
547 dev_err(&desc
->intf
->dev
, "Error in flush path: %d\n",
550 return usb_translate_errors(desc
->werr
);
553 static unsigned int wdm_poll(struct file
*file
, struct poll_table_struct
*wait
)
555 struct wdm_device
*desc
= file
->private_data
;
557 unsigned int mask
= 0;
559 spin_lock_irqsave(&desc
->iuspin
, flags
);
560 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
561 mask
= POLLHUP
| POLLERR
;
562 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
565 if (test_bit(WDM_READ
, &desc
->flags
))
566 mask
= POLLIN
| POLLRDNORM
;
567 if (desc
->rerr
|| desc
->werr
)
569 if (!test_bit(WDM_IN_USE
, &desc
->flags
))
570 mask
|= POLLOUT
| POLLWRNORM
;
571 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
573 poll_wait(file
, &desc
->wait
, wait
);
579 static int wdm_open(struct inode
*inode
, struct file
*file
)
581 int minor
= iminor(inode
);
583 struct usb_interface
*intf
;
584 struct wdm_device
*desc
;
586 mutex_lock(&wdm_mutex
);
587 desc
= wdm_find_device_by_minor(minor
);
592 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
))
594 file
->private_data
= desc
;
596 rv
= usb_autopm_get_interface(desc
->intf
);
598 dev_err(&desc
->intf
->dev
, "Error autopm - %d\n", rv
);
602 /* using write lock to protect desc->count */
603 mutex_lock(&desc
->wlock
);
604 if (!desc
->count
++) {
607 rv
= usb_submit_urb(desc
->validity
, GFP_KERNEL
);
610 dev_err(&desc
->intf
->dev
,
611 "Error submitting int urb - %d\n", rv
);
612 rv
= usb_translate_errors(rv
);
617 mutex_unlock(&desc
->wlock
);
618 if (desc
->count
== 1)
619 desc
->manage_power(intf
, 1);
620 usb_autopm_put_interface(desc
->intf
);
622 mutex_unlock(&wdm_mutex
);
626 static int wdm_release(struct inode
*inode
, struct file
*file
)
628 struct wdm_device
*desc
= file
->private_data
;
630 mutex_lock(&wdm_mutex
);
632 /* using write lock to protect desc->count */
633 mutex_lock(&desc
->wlock
);
635 mutex_unlock(&desc
->wlock
);
638 if (!test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
639 dev_dbg(&desc
->intf
->dev
, "wdm_release: cleanup");
641 desc
->manage_power(desc
->intf
, 0);
643 /* must avoid dev_printk here as desc->intf is invalid */
644 pr_debug(KBUILD_MODNAME
" %s: device gone - cleaning up\n", __func__
);
648 mutex_unlock(&wdm_mutex
);
652 static const struct file_operations wdm_fops
= {
653 .owner
= THIS_MODULE
,
658 .release
= wdm_release
,
660 .llseek
= noop_llseek
,
663 static struct usb_class_driver wdm_class
= {
666 .minor_base
= WDM_MINOR_BASE
,
669 /* --- error handling --- */
670 static void wdm_rxwork(struct work_struct
*work
)
672 struct wdm_device
*desc
= container_of(work
, struct wdm_device
, rxwork
);
676 spin_lock_irqsave(&desc
->iuspin
, flags
);
677 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
678 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
680 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
681 rv
= usb_submit_urb(desc
->response
, GFP_KERNEL
);
682 if (rv
< 0 && rv
!= -EPERM
) {
683 spin_lock_irqsave(&desc
->iuspin
, flags
);
684 if (!test_bit(WDM_DISCONNECTING
, &desc
->flags
))
685 schedule_work(&desc
->rxwork
);
686 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
691 /* --- hotplug --- */
693 static int wdm_create(struct usb_interface
*intf
, struct usb_endpoint_descriptor
*ep
,
694 u16 bufsize
, int (*manage_power
)(struct usb_interface
*, int))
697 struct wdm_device
*desc
;
699 desc
= kzalloc(sizeof(struct wdm_device
), GFP_KERNEL
);
702 INIT_LIST_HEAD(&desc
->device_list
);
703 mutex_init(&desc
->rlock
);
704 mutex_init(&desc
->wlock
);
705 spin_lock_init(&desc
->iuspin
);
706 init_waitqueue_head(&desc
->wait
);
707 desc
->wMaxCommand
= bufsize
;
708 /* this will be expanded and needed in hardware endianness */
709 desc
->inum
= cpu_to_le16((u16
)intf
->cur_altsetting
->desc
.bInterfaceNumber
);
711 INIT_WORK(&desc
->rxwork
, wdm_rxwork
);
714 if (!usb_endpoint_is_int_in(ep
))
717 desc
->wMaxPacketSize
= usb_endpoint_maxp(ep
);
719 desc
->orq
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_KERNEL
);
722 desc
->irq
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_KERNEL
);
726 desc
->validity
= usb_alloc_urb(0, GFP_KERNEL
);
730 desc
->response
= usb_alloc_urb(0, GFP_KERNEL
);
734 desc
->command
= usb_alloc_urb(0, GFP_KERNEL
);
738 desc
->ubuf
= kmalloc(desc
->wMaxCommand
, GFP_KERNEL
);
742 desc
->sbuf
= kmalloc(desc
->wMaxPacketSize
, GFP_KERNEL
);
746 desc
->inbuf
= kmalloc(desc
->wMaxCommand
, GFP_KERNEL
);
752 interface_to_usbdev(intf
),
753 usb_rcvintpipe(interface_to_usbdev(intf
), ep
->bEndpointAddress
),
755 desc
->wMaxPacketSize
,
761 desc
->irq
->bRequestType
= (USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
);
762 desc
->irq
->bRequest
= USB_CDC_GET_ENCAPSULATED_RESPONSE
;
763 desc
->irq
->wValue
= 0;
764 desc
->irq
->wIndex
= desc
->inum
;
765 desc
->irq
->wLength
= cpu_to_le16(desc
->wMaxCommand
);
767 usb_fill_control_urb(
769 interface_to_usbdev(intf
),
770 /* using common endpoint 0 */
771 usb_rcvctrlpipe(interface_to_usbdev(desc
->intf
), 0),
772 (unsigned char *)desc
->irq
,
779 desc
->manage_power
= manage_power
;
781 spin_lock(&wdm_device_list_lock
);
782 list_add(&desc
->device_list
, &wdm_device_list
);
783 spin_unlock(&wdm_device_list_lock
);
785 rv
= usb_register_dev(intf
, &wdm_class
);
789 dev_info(&intf
->dev
, "%s: USB WDM device\n", dev_name(intf
->usb_dev
));
793 spin_lock(&wdm_device_list_lock
);
794 list_del(&desc
->device_list
);
795 spin_unlock(&wdm_device_list_lock
);
800 static int wdm_manage_power(struct usb_interface
*intf
, int on
)
802 /* need autopm_get/put here to ensure the usbcore sees the new value */
803 int rv
= usb_autopm_get_interface(intf
);
807 intf
->needs_remote_wakeup
= on
;
808 usb_autopm_put_interface(intf
);
813 static int wdm_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
816 struct usb_host_interface
*iface
;
817 struct usb_endpoint_descriptor
*ep
;
818 struct usb_cdc_dmm_desc
*dmhd
;
819 u8
*buffer
= intf
->altsetting
->extra
;
820 int buflen
= intf
->altsetting
->extralen
;
821 u16 maxcom
= WDM_DEFAULT_BUFSIZE
;
826 if (buffer
[1] != USB_DT_CS_INTERFACE
) {
827 dev_err(&intf
->dev
, "skipping garbage\n");
832 case USB_CDC_HEADER_TYPE
:
834 case USB_CDC_DMM_TYPE
:
835 dmhd
= (struct usb_cdc_dmm_desc
*)buffer
;
836 maxcom
= le16_to_cpu(dmhd
->wMaxCommand
);
838 "Finding maximum buffer length: %d", maxcom
);
842 "Ignoring extra header, type %d, length %d\n",
843 buffer
[2], buffer
[0]);
851 iface
= intf
->cur_altsetting
;
852 if (iface
->desc
.bNumEndpoints
!= 1)
854 ep
= &iface
->endpoint
[0].desc
;
856 rv
= wdm_create(intf
, ep
, maxcom
, &wdm_manage_power
);
863 * usb_cdc_wdm_register - register a WDM subdriver
864 * @intf: usb interface the subdriver will associate with
865 * @ep: interrupt endpoint to monitor for notifications
866 * @bufsize: maximum message size to support for read/write
868 * Create WDM usb class character device and associate it with intf
869 * without binding, allowing another driver to manage the interface.
871 * The subdriver will manage the given interrupt endpoint exclusively
872 * and will issue control requests referring to the given intf. It
873 * will otherwise avoid interferring, and in particular not do
874 * usb_set_intfdata/usb_get_intfdata on intf.
876 * The return value is a pointer to the subdriver's struct usb_driver.
877 * The registering driver is responsible for calling this subdriver's
878 * disconnect, suspend, resume, pre_reset and post_reset methods from
881 struct usb_driver
*usb_cdc_wdm_register(struct usb_interface
*intf
,
882 struct usb_endpoint_descriptor
*ep
,
884 int (*manage_power
)(struct usb_interface
*, int))
888 rv
= wdm_create(intf
, ep
, bufsize
, manage_power
);
896 EXPORT_SYMBOL(usb_cdc_wdm_register
);
898 static void wdm_disconnect(struct usb_interface
*intf
)
900 struct wdm_device
*desc
;
903 usb_deregister_dev(intf
, &wdm_class
);
904 desc
= wdm_find_device(intf
);
905 mutex_lock(&wdm_mutex
);
907 /* the spinlock makes sure no new urbs are generated in the callbacks */
908 spin_lock_irqsave(&desc
->iuspin
, flags
);
909 set_bit(WDM_DISCONNECTING
, &desc
->flags
);
910 set_bit(WDM_READ
, &desc
->flags
);
911 /* to terminate pending flushes */
912 clear_bit(WDM_IN_USE
, &desc
->flags
);
913 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
914 wake_up_all(&desc
->wait
);
915 mutex_lock(&desc
->rlock
);
916 mutex_lock(&desc
->wlock
);
918 cancel_work_sync(&desc
->rxwork
);
919 mutex_unlock(&desc
->wlock
);
920 mutex_unlock(&desc
->rlock
);
922 /* the desc->intf pointer used as list key is now invalid */
923 spin_lock(&wdm_device_list_lock
);
924 list_del(&desc
->device_list
);
925 spin_unlock(&wdm_device_list_lock
);
930 dev_dbg(&intf
->dev
, "%s: %d open files - postponing cleanup\n", __func__
, desc
->count
);
931 mutex_unlock(&wdm_mutex
);
935 static int wdm_suspend(struct usb_interface
*intf
, pm_message_t message
)
937 struct wdm_device
*desc
= wdm_find_device(intf
);
940 dev_dbg(&desc
->intf
->dev
, "wdm%d_suspend\n", intf
->minor
);
942 /* if this is an autosuspend the caller does the locking */
943 if (!PMSG_IS_AUTO(message
)) {
944 mutex_lock(&desc
->rlock
);
945 mutex_lock(&desc
->wlock
);
947 spin_lock_irq(&desc
->iuspin
);
949 if (PMSG_IS_AUTO(message
) &&
950 (test_bit(WDM_IN_USE
, &desc
->flags
)
951 || test_bit(WDM_RESPONDING
, &desc
->flags
))) {
952 spin_unlock_irq(&desc
->iuspin
);
956 set_bit(WDM_SUSPENDING
, &desc
->flags
);
957 spin_unlock_irq(&desc
->iuspin
);
958 /* callback submits work - order is essential */
960 cancel_work_sync(&desc
->rxwork
);
962 if (!PMSG_IS_AUTO(message
)) {
963 mutex_unlock(&desc
->wlock
);
964 mutex_unlock(&desc
->rlock
);
971 static int recover_from_urb_loss(struct wdm_device
*desc
)
976 rv
= usb_submit_urb(desc
->validity
, GFP_NOIO
);
978 dev_err(&desc
->intf
->dev
,
979 "Error resume submitting int urb - %d\n", rv
);
985 static int wdm_resume(struct usb_interface
*intf
)
987 struct wdm_device
*desc
= wdm_find_device(intf
);
990 dev_dbg(&desc
->intf
->dev
, "wdm%d_resume\n", intf
->minor
);
992 clear_bit(WDM_SUSPENDING
, &desc
->flags
);
993 rv
= recover_from_urb_loss(desc
);
999 static int wdm_pre_reset(struct usb_interface
*intf
)
1001 struct wdm_device
*desc
= wdm_find_device(intf
);
1004 * we notify everybody using poll of
1005 * an exceptional situation
1006 * must be done before recovery lest a spontaneous
1007 * message from the device is lost
1009 spin_lock_irq(&desc
->iuspin
);
1010 set_bit(WDM_RESETTING
, &desc
->flags
); /* inform read/write */
1011 set_bit(WDM_READ
, &desc
->flags
); /* unblock read */
1012 clear_bit(WDM_IN_USE
, &desc
->flags
); /* unblock write */
1013 desc
->rerr
= -EINTR
;
1014 spin_unlock_irq(&desc
->iuspin
);
1015 wake_up_all(&desc
->wait
);
1016 mutex_lock(&desc
->rlock
);
1017 mutex_lock(&desc
->wlock
);
1019 cancel_work_sync(&desc
->rxwork
);
1023 static int wdm_post_reset(struct usb_interface
*intf
)
1025 struct wdm_device
*desc
= wdm_find_device(intf
);
1028 clear_bit(WDM_RESETTING
, &desc
->flags
);
1029 rv
= recover_from_urb_loss(desc
);
1030 mutex_unlock(&desc
->wlock
);
1031 mutex_unlock(&desc
->rlock
);
1035 static struct usb_driver wdm_driver
= {
1038 .disconnect
= wdm_disconnect
,
1040 .suspend
= wdm_suspend
,
1041 .resume
= wdm_resume
,
1042 .reset_resume
= wdm_resume
,
1044 .pre_reset
= wdm_pre_reset
,
1045 .post_reset
= wdm_post_reset
,
1046 .id_table
= wdm_ids
,
1047 .supports_autosuspend
= 1,
1048 .disable_hub_initiated_lpm
= 1,
1051 module_usb_driver(wdm_driver
);
1053 MODULE_AUTHOR(DRIVER_AUTHOR
);
1054 MODULE_DESCRIPTION(DRIVER_DESC
);
1055 MODULE_LICENSE("GPL");