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
;
106 struct list_head device_list
;
107 int (*manage_power
)(struct usb_interface
*, int);
110 static struct usb_driver wdm_driver
;
112 /* return intfdata if we own the interface, else look up intf in the list */
113 static struct wdm_device
*wdm_find_device(struct usb_interface
*intf
)
115 struct wdm_device
*desc
;
117 spin_lock(&wdm_device_list_lock
);
118 list_for_each_entry(desc
, &wdm_device_list
, device_list
)
119 if (desc
->intf
== intf
)
123 spin_unlock(&wdm_device_list_lock
);
128 static struct wdm_device
*wdm_find_device_by_minor(int minor
)
130 struct wdm_device
*desc
;
132 spin_lock(&wdm_device_list_lock
);
133 list_for_each_entry(desc
, &wdm_device_list
, device_list
)
134 if (desc
->intf
->minor
== minor
)
138 spin_unlock(&wdm_device_list_lock
);
143 /* --- callbacks --- */
144 static void wdm_out_callback(struct urb
*urb
)
146 struct wdm_device
*desc
;
148 spin_lock(&desc
->iuspin
);
149 desc
->werr
= urb
->status
;
150 spin_unlock(&desc
->iuspin
);
153 clear_bit(WDM_IN_USE
, &desc
->flags
);
154 wake_up(&desc
->wait
);
157 /* forward declaration */
158 static int service_outstanding_interrupt(struct wdm_device
*desc
);
160 static void wdm_in_callback(struct urb
*urb
)
162 struct wdm_device
*desc
= urb
->context
;
163 int status
= urb
->status
;
164 int length
= urb
->actual_length
;
166 spin_lock(&desc
->iuspin
);
167 clear_bit(WDM_RESPONDING
, &desc
->flags
);
172 dev_dbg(&desc
->intf
->dev
,
173 "nonzero urb status received: -ENOENT\n");
176 dev_dbg(&desc
->intf
->dev
,
177 "nonzero urb status received: -ECONNRESET\n");
180 dev_dbg(&desc
->intf
->dev
,
181 "nonzero urb status received: -ESHUTDOWN\n");
184 dev_err(&desc
->intf
->dev
,
185 "nonzero urb status received: -EPIPE\n");
188 dev_err(&desc
->intf
->dev
,
189 "Unexpected error %d\n", status
);
195 * only set a new error if there is no previous error.
196 * Errors are only cleared during read/open
201 if (length
+ desc
->length
> desc
->wMaxCommand
) {
202 /* The buffer would overflow */
203 set_bit(WDM_OVERFLOW
, &desc
->flags
);
205 /* we may already be in overflow */
206 if (!test_bit(WDM_OVERFLOW
, &desc
->flags
)) {
207 memmove(desc
->ubuf
+ desc
->length
, desc
->inbuf
, length
);
208 desc
->length
+= length
;
209 desc
->reslength
= length
;
213 set_bit(WDM_READ
, &desc
->flags
);
214 wake_up(&desc
->wait
);
218 * Since there was an error, userspace may decide to not read
219 * any data after poll'ing.
220 * We should respond to further attempts from the device to send
221 * data, so that we can get unstuck.
223 service_outstanding_interrupt(desc
);
226 spin_unlock(&desc
->iuspin
);
229 static void wdm_int_callback(struct urb
*urb
)
233 int status
= urb
->status
;
234 struct wdm_device
*desc
;
235 struct usb_cdc_notification
*dr
;
238 dr
= (struct usb_cdc_notification
*)desc
->sbuf
;
247 set_bit(WDM_INT_STALL
, &desc
->flags
);
248 dev_err(&desc
->intf
->dev
, "Stall on int endpoint\n");
249 goto sw
; /* halt is cleared in work */
251 dev_err(&desc
->intf
->dev
,
252 "nonzero urb status received: %d\n", status
);
257 if (urb
->actual_length
< sizeof(struct usb_cdc_notification
)) {
258 dev_err(&desc
->intf
->dev
, "wdm_int_callback - %d bytes\n",
263 switch (dr
->bNotificationType
) {
264 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE
:
265 dev_dbg(&desc
->intf
->dev
,
266 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d\n",
267 le16_to_cpu(dr
->wIndex
), le16_to_cpu(dr
->wLength
));
270 case USB_CDC_NOTIFY_NETWORK_CONNECTION
:
272 dev_dbg(&desc
->intf
->dev
,
273 "NOTIFY_NETWORK_CONNECTION %s network\n",
274 dr
->wValue
? "connected to" : "disconnected from");
276 case USB_CDC_NOTIFY_SPEED_CHANGE
:
277 dev_dbg(&desc
->intf
->dev
, "SPEED_CHANGE received (len %u)\n",
281 clear_bit(WDM_POLL_RUNNING
, &desc
->flags
);
282 dev_err(&desc
->intf
->dev
,
283 "unknown notification %d received: index %d len %d\n",
284 dr
->bNotificationType
,
285 le16_to_cpu(dr
->wIndex
),
286 le16_to_cpu(dr
->wLength
));
290 spin_lock(&desc
->iuspin
);
291 responding
= test_and_set_bit(WDM_RESPONDING
, &desc
->flags
);
292 if (!desc
->resp_count
++ && !responding
293 && !test_bit(WDM_DISCONNECTING
, &desc
->flags
)
294 && !test_bit(WDM_SUSPENDING
, &desc
->flags
)) {
295 rv
= usb_submit_urb(desc
->response
, GFP_ATOMIC
);
296 dev_dbg(&desc
->intf
->dev
, "submit response URB %d\n", rv
);
298 spin_unlock(&desc
->iuspin
);
300 clear_bit(WDM_RESPONDING
, &desc
->flags
);
305 rv
= schedule_work(&desc
->rxwork
);
307 dev_err(&desc
->intf
->dev
,
308 "Cannot schedule work\n");
312 rv
= usb_submit_urb(urb
, GFP_ATOMIC
);
314 dev_err(&desc
->intf
->dev
,
315 "%s - usb_submit_urb failed with result %d\n",
320 static void kill_urbs(struct wdm_device
*desc
)
322 /* the order here is essential */
323 usb_kill_urb(desc
->command
);
324 usb_kill_urb(desc
->validity
);
325 usb_kill_urb(desc
->response
);
328 static void free_urbs(struct wdm_device
*desc
)
330 usb_free_urb(desc
->validity
);
331 usb_free_urb(desc
->response
);
332 usb_free_urb(desc
->command
);
335 static void cleanup(struct wdm_device
*desc
)
346 static ssize_t wdm_write
347 (struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
350 int rv
= -EMSGSIZE
, r
, we
;
351 struct wdm_device
*desc
= file
->private_data
;
352 struct usb_ctrlrequest
*req
;
354 if (count
> desc
->wMaxCommand
)
355 count
= desc
->wMaxCommand
;
357 spin_lock_irq(&desc
->iuspin
);
360 spin_unlock_irq(&desc
->iuspin
);
362 return usb_translate_errors(we
);
364 buf
= memdup_user(buffer
, count
);
368 /* concurrent writes and disconnect */
369 r
= mutex_lock_interruptible(&desc
->wlock
);
374 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
376 goto out_free_mem_lock
;
379 r
= usb_autopm_get_interface(desc
->intf
);
381 rv
= usb_translate_errors(r
);
382 goto out_free_mem_lock
;
385 if (!(file
->f_flags
& O_NONBLOCK
))
386 r
= wait_event_interruptible(desc
->wait
, !test_bit(WDM_IN_USE
,
389 if (test_bit(WDM_IN_USE
, &desc
->flags
))
392 if (test_bit(WDM_RESETTING
, &desc
->flags
))
397 goto out_free_mem_pm
;
401 usb_fill_control_urb(
403 interface_to_usbdev(desc
->intf
),
404 /* using common endpoint 0 */
405 usb_sndctrlpipe(interface_to_usbdev(desc
->intf
), 0),
406 (unsigned char *)req
,
413 req
->bRequestType
= (USB_DIR_OUT
| USB_TYPE_CLASS
|
414 USB_RECIP_INTERFACE
);
415 req
->bRequest
= USB_CDC_SEND_ENCAPSULATED_COMMAND
;
417 req
->wIndex
= desc
->inum
; /* already converted */
418 req
->wLength
= cpu_to_le16(count
);
419 set_bit(WDM_IN_USE
, &desc
->flags
);
422 rv
= usb_submit_urb(desc
->command
, GFP_KERNEL
);
425 clear_bit(WDM_IN_USE
, &desc
->flags
);
426 dev_err(&desc
->intf
->dev
, "Tx URB error: %d\n", rv
);
427 rv
= usb_translate_errors(rv
);
428 goto out_free_mem_pm
;
430 dev_dbg(&desc
->intf
->dev
, "Tx URB has been submitted index=%d\n",
431 le16_to_cpu(req
->wIndex
));
434 usb_autopm_put_interface(desc
->intf
);
435 mutex_unlock(&desc
->wlock
);
439 usb_autopm_put_interface(desc
->intf
);
441 mutex_unlock(&desc
->wlock
);
448 * Submit the read urb if resp_count is non-zero.
450 * Called with desc->iuspin locked
452 static int service_outstanding_interrupt(struct wdm_device
*desc
)
456 /* submit read urb only if the device is waiting for it */
457 if (!desc
->resp_count
|| !--desc
->resp_count
)
460 set_bit(WDM_RESPONDING
, &desc
->flags
);
461 spin_unlock_irq(&desc
->iuspin
);
462 rv
= usb_submit_urb(desc
->response
, GFP_KERNEL
);
463 spin_lock_irq(&desc
->iuspin
);
465 dev_err(&desc
->intf
->dev
,
466 "usb_submit_urb failed with result %d\n", rv
);
468 /* make sure the next notification trigger a submit */
469 clear_bit(WDM_RESPONDING
, &desc
->flags
);
470 desc
->resp_count
= 0;
476 static ssize_t wdm_read
477 (struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
481 struct wdm_device
*desc
= file
->private_data
;
484 rv
= mutex_lock_interruptible(&desc
->rlock
); /*concurrent reads */
488 cntr
= ACCESS_ONCE(desc
->length
);
492 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
496 if (test_bit(WDM_OVERFLOW
, &desc
->flags
)) {
497 clear_bit(WDM_OVERFLOW
, &desc
->flags
);
502 if (file
->f_flags
& O_NONBLOCK
) {
503 if (!test_bit(WDM_READ
, &desc
->flags
)) {
509 rv
= wait_event_interruptible(desc
->wait
,
510 test_bit(WDM_READ
, &desc
->flags
));
513 /* may have happened while we slept */
514 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
518 if (test_bit(WDM_RESETTING
, &desc
->flags
)) {
522 usb_mark_last_busy(interface_to_usbdev(desc
->intf
));
528 spin_lock_irq(&desc
->iuspin
);
530 if (desc
->rerr
) { /* read completed, error happened */
531 rv
= usb_translate_errors(desc
->rerr
);
533 spin_unlock_irq(&desc
->iuspin
);
537 * recheck whether we've lost the race
538 * against the completion handler
540 if (!test_bit(WDM_READ
, &desc
->flags
)) { /* lost race */
541 spin_unlock_irq(&desc
->iuspin
);
545 if (!desc
->reslength
) { /* zero length read */
546 dev_dbg(&desc
->intf
->dev
, "zero length - clearing WDM_READ\n");
547 clear_bit(WDM_READ
, &desc
->flags
);
548 rv
= service_outstanding_interrupt(desc
);
549 spin_unlock_irq(&desc
->iuspin
);
555 spin_unlock_irq(&desc
->iuspin
);
560 rv
= copy_to_user(buffer
, desc
->ubuf
, cntr
);
566 spin_lock_irq(&desc
->iuspin
);
568 for (i
= 0; i
< desc
->length
- cntr
; i
++)
569 desc
->ubuf
[i
] = desc
->ubuf
[i
+ cntr
];
571 desc
->length
-= cntr
;
572 /* in case we had outstanding data */
574 clear_bit(WDM_READ
, &desc
->flags
);
575 service_outstanding_interrupt(desc
);
577 spin_unlock_irq(&desc
->iuspin
);
581 mutex_unlock(&desc
->rlock
);
585 static int wdm_flush(struct file
*file
, fl_owner_t id
)
587 struct wdm_device
*desc
= file
->private_data
;
589 wait_event(desc
->wait
, !test_bit(WDM_IN_USE
, &desc
->flags
));
591 /* cannot dereference desc->intf if WDM_DISCONNECTING */
592 if (desc
->werr
< 0 && !test_bit(WDM_DISCONNECTING
, &desc
->flags
))
593 dev_err(&desc
->intf
->dev
, "Error in flush path: %d\n",
596 return usb_translate_errors(desc
->werr
);
599 static unsigned int wdm_poll(struct file
*file
, struct poll_table_struct
*wait
)
601 struct wdm_device
*desc
= file
->private_data
;
603 unsigned int mask
= 0;
605 spin_lock_irqsave(&desc
->iuspin
, flags
);
606 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
607 mask
= POLLHUP
| POLLERR
;
608 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
611 if (test_bit(WDM_READ
, &desc
->flags
))
612 mask
= POLLIN
| POLLRDNORM
;
613 if (desc
->rerr
|| desc
->werr
)
615 if (!test_bit(WDM_IN_USE
, &desc
->flags
))
616 mask
|= POLLOUT
| POLLWRNORM
;
617 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
619 poll_wait(file
, &desc
->wait
, wait
);
625 static int wdm_open(struct inode
*inode
, struct file
*file
)
627 int minor
= iminor(inode
);
629 struct usb_interface
*intf
;
630 struct wdm_device
*desc
;
632 mutex_lock(&wdm_mutex
);
633 desc
= wdm_find_device_by_minor(minor
);
638 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
))
640 file
->private_data
= desc
;
642 rv
= usb_autopm_get_interface(desc
->intf
);
644 dev_err(&desc
->intf
->dev
, "Error autopm - %d\n", rv
);
648 /* using write lock to protect desc->count */
649 mutex_lock(&desc
->wlock
);
650 if (!desc
->count
++) {
653 rv
= usb_submit_urb(desc
->validity
, GFP_KERNEL
);
656 dev_err(&desc
->intf
->dev
,
657 "Error submitting int urb - %d\n", rv
);
658 rv
= usb_translate_errors(rv
);
663 mutex_unlock(&desc
->wlock
);
664 if (desc
->count
== 1)
665 desc
->manage_power(intf
, 1);
666 usb_autopm_put_interface(desc
->intf
);
668 mutex_unlock(&wdm_mutex
);
672 static int wdm_release(struct inode
*inode
, struct file
*file
)
674 struct wdm_device
*desc
= file
->private_data
;
676 mutex_lock(&wdm_mutex
);
678 /* using write lock to protect desc->count */
679 mutex_lock(&desc
->wlock
);
681 mutex_unlock(&desc
->wlock
);
684 if (!test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
685 dev_dbg(&desc
->intf
->dev
, "wdm_release: cleanup\n");
687 spin_lock_irq(&desc
->iuspin
);
688 desc
->resp_count
= 0;
689 spin_unlock_irq(&desc
->iuspin
);
690 desc
->manage_power(desc
->intf
, 0);
692 /* must avoid dev_printk here as desc->intf is invalid */
693 pr_debug(KBUILD_MODNAME
" %s: device gone - cleaning up\n", __func__
);
697 mutex_unlock(&wdm_mutex
);
701 static long wdm_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
703 struct wdm_device
*desc
= file
->private_data
;
707 case IOCTL_WDM_MAX_COMMAND
:
708 if (copy_to_user((void __user
*)arg
, &desc
->wMaxCommand
, sizeof(desc
->wMaxCommand
)))
717 static const struct file_operations wdm_fops
= {
718 .owner
= THIS_MODULE
,
723 .release
= wdm_release
,
725 .unlocked_ioctl
= wdm_ioctl
,
726 .compat_ioctl
= wdm_ioctl
,
727 .llseek
= noop_llseek
,
730 static struct usb_class_driver wdm_class
= {
733 .minor_base
= WDM_MINOR_BASE
,
736 /* --- error handling --- */
737 static void wdm_rxwork(struct work_struct
*work
)
739 struct wdm_device
*desc
= container_of(work
, struct wdm_device
, rxwork
);
744 spin_lock_irqsave(&desc
->iuspin
, flags
);
745 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
746 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
748 responding
= test_and_set_bit(WDM_RESPONDING
, &desc
->flags
);
749 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
751 rv
= usb_submit_urb(desc
->response
, GFP_KERNEL
);
752 if (rv
< 0 && rv
!= -EPERM
) {
753 spin_lock_irqsave(&desc
->iuspin
, flags
);
754 clear_bit(WDM_RESPONDING
, &desc
->flags
);
755 if (!test_bit(WDM_DISCONNECTING
, &desc
->flags
))
756 schedule_work(&desc
->rxwork
);
757 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
762 /* --- hotplug --- */
764 static int wdm_create(struct usb_interface
*intf
, struct usb_endpoint_descriptor
*ep
,
765 u16 bufsize
, int (*manage_power
)(struct usb_interface
*, int))
768 struct wdm_device
*desc
;
770 desc
= kzalloc(sizeof(struct wdm_device
), GFP_KERNEL
);
773 INIT_LIST_HEAD(&desc
->device_list
);
774 mutex_init(&desc
->rlock
);
775 mutex_init(&desc
->wlock
);
776 spin_lock_init(&desc
->iuspin
);
777 init_waitqueue_head(&desc
->wait
);
778 desc
->wMaxCommand
= bufsize
;
779 /* this will be expanded and needed in hardware endianness */
780 desc
->inum
= cpu_to_le16((u16
)intf
->cur_altsetting
->desc
.bInterfaceNumber
);
782 INIT_WORK(&desc
->rxwork
, wdm_rxwork
);
785 if (!usb_endpoint_is_int_in(ep
))
788 desc
->wMaxPacketSize
= usb_endpoint_maxp(ep
);
790 desc
->orq
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_KERNEL
);
793 desc
->irq
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_KERNEL
);
797 desc
->validity
= usb_alloc_urb(0, GFP_KERNEL
);
801 desc
->response
= usb_alloc_urb(0, GFP_KERNEL
);
805 desc
->command
= usb_alloc_urb(0, GFP_KERNEL
);
809 desc
->ubuf
= kmalloc(desc
->wMaxCommand
, GFP_KERNEL
);
813 desc
->sbuf
= kmalloc(desc
->wMaxPacketSize
, GFP_KERNEL
);
817 desc
->inbuf
= kmalloc(desc
->wMaxCommand
, GFP_KERNEL
);
823 interface_to_usbdev(intf
),
824 usb_rcvintpipe(interface_to_usbdev(intf
), ep
->bEndpointAddress
),
826 desc
->wMaxPacketSize
,
832 desc
->irq
->bRequestType
= (USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
);
833 desc
->irq
->bRequest
= USB_CDC_GET_ENCAPSULATED_RESPONSE
;
834 desc
->irq
->wValue
= 0;
835 desc
->irq
->wIndex
= desc
->inum
; /* already converted */
836 desc
->irq
->wLength
= cpu_to_le16(desc
->wMaxCommand
);
838 usb_fill_control_urb(
840 interface_to_usbdev(intf
),
841 /* using common endpoint 0 */
842 usb_rcvctrlpipe(interface_to_usbdev(desc
->intf
), 0),
843 (unsigned char *)desc
->irq
,
850 desc
->manage_power
= manage_power
;
852 spin_lock(&wdm_device_list_lock
);
853 list_add(&desc
->device_list
, &wdm_device_list
);
854 spin_unlock(&wdm_device_list_lock
);
856 rv
= usb_register_dev(intf
, &wdm_class
);
860 dev_info(&intf
->dev
, "%s: USB WDM device\n", dev_name(intf
->usb_dev
));
864 spin_lock(&wdm_device_list_lock
);
865 list_del(&desc
->device_list
);
866 spin_unlock(&wdm_device_list_lock
);
871 static int wdm_manage_power(struct usb_interface
*intf
, int on
)
873 /* need autopm_get/put here to ensure the usbcore sees the new value */
874 int rv
= usb_autopm_get_interface(intf
);
876 intf
->needs_remote_wakeup
= on
;
878 usb_autopm_put_interface(intf
);
882 static int wdm_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
885 struct usb_host_interface
*iface
;
886 struct usb_endpoint_descriptor
*ep
;
887 struct usb_cdc_parsed_header hdr
;
888 u8
*buffer
= intf
->altsetting
->extra
;
889 int buflen
= intf
->altsetting
->extralen
;
890 u16 maxcom
= WDM_DEFAULT_BUFSIZE
;
895 cdc_parse_cdc_header(&hdr
, intf
, buffer
, buflen
);
897 if (hdr
.usb_cdc_dmm_desc
)
898 maxcom
= le16_to_cpu(hdr
.usb_cdc_dmm_desc
->wMaxCommand
);
900 iface
= intf
->cur_altsetting
;
901 if (iface
->desc
.bNumEndpoints
!= 1)
903 ep
= &iface
->endpoint
[0].desc
;
905 rv
= wdm_create(intf
, ep
, maxcom
, &wdm_manage_power
);
912 * usb_cdc_wdm_register - register a WDM subdriver
913 * @intf: usb interface the subdriver will associate with
914 * @ep: interrupt endpoint to monitor for notifications
915 * @bufsize: maximum message size to support for read/write
917 * Create WDM usb class character device and associate it with intf
918 * without binding, allowing another driver to manage the interface.
920 * The subdriver will manage the given interrupt endpoint exclusively
921 * and will issue control requests referring to the given intf. It
922 * will otherwise avoid interferring, and in particular not do
923 * usb_set_intfdata/usb_get_intfdata on intf.
925 * The return value is a pointer to the subdriver's struct usb_driver.
926 * The registering driver is responsible for calling this subdriver's
927 * disconnect, suspend, resume, pre_reset and post_reset methods from
930 struct usb_driver
*usb_cdc_wdm_register(struct usb_interface
*intf
,
931 struct usb_endpoint_descriptor
*ep
,
933 int (*manage_power
)(struct usb_interface
*, int))
937 rv
= wdm_create(intf
, ep
, bufsize
, manage_power
);
945 EXPORT_SYMBOL(usb_cdc_wdm_register
);
947 static void wdm_disconnect(struct usb_interface
*intf
)
949 struct wdm_device
*desc
;
952 usb_deregister_dev(intf
, &wdm_class
);
953 desc
= wdm_find_device(intf
);
954 mutex_lock(&wdm_mutex
);
956 /* the spinlock makes sure no new urbs are generated in the callbacks */
957 spin_lock_irqsave(&desc
->iuspin
, flags
);
958 set_bit(WDM_DISCONNECTING
, &desc
->flags
);
959 set_bit(WDM_READ
, &desc
->flags
);
960 /* to terminate pending flushes */
961 clear_bit(WDM_IN_USE
, &desc
->flags
);
962 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
963 wake_up_all(&desc
->wait
);
964 mutex_lock(&desc
->rlock
);
965 mutex_lock(&desc
->wlock
);
967 cancel_work_sync(&desc
->rxwork
);
968 mutex_unlock(&desc
->wlock
);
969 mutex_unlock(&desc
->rlock
);
971 /* the desc->intf pointer used as list key is now invalid */
972 spin_lock(&wdm_device_list_lock
);
973 list_del(&desc
->device_list
);
974 spin_unlock(&wdm_device_list_lock
);
979 dev_dbg(&intf
->dev
, "%d open files - postponing cleanup\n", desc
->count
);
980 mutex_unlock(&wdm_mutex
);
984 static int wdm_suspend(struct usb_interface
*intf
, pm_message_t message
)
986 struct wdm_device
*desc
= wdm_find_device(intf
);
989 dev_dbg(&desc
->intf
->dev
, "wdm%d_suspend\n", intf
->minor
);
991 /* if this is an autosuspend the caller does the locking */
992 if (!PMSG_IS_AUTO(message
)) {
993 mutex_lock(&desc
->rlock
);
994 mutex_lock(&desc
->wlock
);
996 spin_lock_irq(&desc
->iuspin
);
998 if (PMSG_IS_AUTO(message
) &&
999 (test_bit(WDM_IN_USE
, &desc
->flags
)
1000 || test_bit(WDM_RESPONDING
, &desc
->flags
))) {
1001 spin_unlock_irq(&desc
->iuspin
);
1005 set_bit(WDM_SUSPENDING
, &desc
->flags
);
1006 spin_unlock_irq(&desc
->iuspin
);
1007 /* callback submits work - order is essential */
1009 cancel_work_sync(&desc
->rxwork
);
1011 if (!PMSG_IS_AUTO(message
)) {
1012 mutex_unlock(&desc
->wlock
);
1013 mutex_unlock(&desc
->rlock
);
1020 static int recover_from_urb_loss(struct wdm_device
*desc
)
1025 rv
= usb_submit_urb(desc
->validity
, GFP_NOIO
);
1027 dev_err(&desc
->intf
->dev
,
1028 "Error resume submitting int urb - %d\n", rv
);
1034 static int wdm_resume(struct usb_interface
*intf
)
1036 struct wdm_device
*desc
= wdm_find_device(intf
);
1039 dev_dbg(&desc
->intf
->dev
, "wdm%d_resume\n", intf
->minor
);
1041 clear_bit(WDM_SUSPENDING
, &desc
->flags
);
1042 rv
= recover_from_urb_loss(desc
);
1048 static int wdm_pre_reset(struct usb_interface
*intf
)
1050 struct wdm_device
*desc
= wdm_find_device(intf
);
1053 * we notify everybody using poll of
1054 * an exceptional situation
1055 * must be done before recovery lest a spontaneous
1056 * message from the device is lost
1058 spin_lock_irq(&desc
->iuspin
);
1059 set_bit(WDM_RESETTING
, &desc
->flags
); /* inform read/write */
1060 set_bit(WDM_READ
, &desc
->flags
); /* unblock read */
1061 clear_bit(WDM_IN_USE
, &desc
->flags
); /* unblock write */
1062 desc
->rerr
= -EINTR
;
1063 spin_unlock_irq(&desc
->iuspin
);
1064 wake_up_all(&desc
->wait
);
1065 mutex_lock(&desc
->rlock
);
1066 mutex_lock(&desc
->wlock
);
1068 cancel_work_sync(&desc
->rxwork
);
1072 static int wdm_post_reset(struct usb_interface
*intf
)
1074 struct wdm_device
*desc
= wdm_find_device(intf
);
1077 clear_bit(WDM_OVERFLOW
, &desc
->flags
);
1078 clear_bit(WDM_RESETTING
, &desc
->flags
);
1079 rv
= recover_from_urb_loss(desc
);
1080 mutex_unlock(&desc
->wlock
);
1081 mutex_unlock(&desc
->rlock
);
1085 static struct usb_driver wdm_driver
= {
1088 .disconnect
= wdm_disconnect
,
1090 .suspend
= wdm_suspend
,
1091 .resume
= wdm_resume
,
1092 .reset_resume
= wdm_resume
,
1094 .pre_reset
= wdm_pre_reset
,
1095 .post_reset
= wdm_post_reset
,
1096 .id_table
= wdm_ids
,
1097 .supports_autosuspend
= 1,
1098 .disable_hub_initiated_lpm
= 1,
1101 module_usb_driver(wdm_driver
);
1103 MODULE_AUTHOR(DRIVER_AUTHOR
);
1104 MODULE_DESCRIPTION(DRIVER_DESC
);
1105 MODULE_LICENSE("GPL");