1 // SPDX-License-Identifier: GPL-2.0
5 * This driver supports USB CDC WCM Device Management.
7 * Copyright (c) 2007-2009 Oliver Neukum
9 * Some code taken from cdc-acm.c
11 * Released under the GPLv2.
13 * Many thanks to Carl Nordbeck
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/ioctl.h>
18 #include <linux/slab.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/uaccess.h>
22 #include <linux/bitops.h>
23 #include <linux/poll.h>
24 #include <linux/usb.h>
25 #include <linux/usb/cdc.h>
26 #include <asm/byteorder.h>
27 #include <asm/unaligned.h>
28 #include <linux/usb/cdc-wdm.h>
30 #define DRIVER_AUTHOR "Oliver Neukum"
31 #define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management"
33 static const struct usb_device_id wdm_ids
[] = {
35 .match_flags
= USB_DEVICE_ID_MATCH_INT_CLASS
|
36 USB_DEVICE_ID_MATCH_INT_SUBCLASS
,
37 .bInterfaceClass
= USB_CLASS_COMM
,
38 .bInterfaceSubClass
= USB_CDC_SUBCLASS_DMM
43 MODULE_DEVICE_TABLE (usb
, wdm_ids
);
45 #define WDM_MINOR_BASE 176
49 #define WDM_DISCONNECTING 2
52 #define WDM_INT_STALL 5
53 #define WDM_POLL_RUNNING 6
54 #define WDM_RESPONDING 7
55 #define WDM_SUSPENDING 8
56 #define WDM_RESETTING 9
57 #define WDM_OVERFLOW 10
61 /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
62 #define WDM_DEFAULT_BUFSIZE 256
64 static DEFINE_MUTEX(wdm_mutex
);
65 static DEFINE_SPINLOCK(wdm_device_list_lock
);
66 static LIST_HEAD(wdm_device_list
);
68 /* --- method tables --- */
71 u8
*inbuf
; /* buffer for response */
72 u8
*outbuf
; /* buffer for command */
73 u8
*sbuf
; /* buffer for status */
74 u8
*ubuf
; /* buffer for copy to user space */
79 struct usb_interface
*intf
;
80 struct usb_ctrlrequest
*orq
;
81 struct usb_ctrlrequest
*irq
;
97 wait_queue_head_t wait
;
98 struct work_struct rxwork
;
99 struct work_struct service_outs_intr
;
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
;
148 spin_lock_irqsave(&desc
->iuspin
, flags
);
149 desc
->werr
= urb
->status
;
150 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
153 clear_bit(WDM_IN_USE
, &desc
->flags
);
154 wake_up(&desc
->wait
);
157 static void wdm_in_callback(struct urb
*urb
)
160 struct wdm_device
*desc
= urb
->context
;
161 int status
= urb
->status
;
162 int length
= urb
->actual_length
;
164 spin_lock_irqsave(&desc
->iuspin
, flags
);
165 clear_bit(WDM_RESPONDING
, &desc
->flags
);
170 dev_dbg(&desc
->intf
->dev
,
171 "nonzero urb status received: -ENOENT\n");
174 dev_dbg(&desc
->intf
->dev
,
175 "nonzero urb status received: -ECONNRESET\n");
178 dev_dbg(&desc
->intf
->dev
,
179 "nonzero urb status received: -ESHUTDOWN\n");
182 dev_err(&desc
->intf
->dev
,
183 "nonzero urb status received: -EPIPE\n");
186 dev_err(&desc
->intf
->dev
,
187 "Unexpected error %d\n", status
);
193 * only set a new error if there is no previous error.
194 * Errors are only cleared during read/open
195 * Avoid propagating -EPIPE (stall) to userspace since it is
196 * better handled as an empty read
198 if (desc
->rerr
== 0 && status
!= -EPIPE
)
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
;
216 * Since there was an error, userspace may decide to not read
217 * any data after poll'ing.
218 * We should respond to further attempts from the device to send
219 * data, so that we can get unstuck.
221 schedule_work(&desc
->service_outs_intr
);
223 set_bit(WDM_READ
, &desc
->flags
);
224 wake_up(&desc
->wait
);
226 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
229 static void wdm_int_callback(struct urb
*urb
)
234 int status
= urb
->status
;
235 struct wdm_device
*desc
;
236 struct usb_cdc_notification
*dr
;
239 dr
= (struct usb_cdc_notification
*)desc
->sbuf
;
248 set_bit(WDM_INT_STALL
, &desc
->flags
);
249 dev_err(&desc
->intf
->dev
, "Stall on int endpoint\n");
250 goto sw
; /* halt is cleared in work */
252 dev_err(&desc
->intf
->dev
,
253 "nonzero urb status received: %d\n", status
);
258 if (urb
->actual_length
< sizeof(struct usb_cdc_notification
)) {
259 dev_err(&desc
->intf
->dev
, "wdm_int_callback - %d bytes\n",
264 switch (dr
->bNotificationType
) {
265 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE
:
266 dev_dbg(&desc
->intf
->dev
,
267 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d\n",
268 le16_to_cpu(dr
->wIndex
), le16_to_cpu(dr
->wLength
));
271 case USB_CDC_NOTIFY_NETWORK_CONNECTION
:
273 dev_dbg(&desc
->intf
->dev
,
274 "NOTIFY_NETWORK_CONNECTION %s network\n",
275 dr
->wValue
? "connected to" : "disconnected from");
277 case USB_CDC_NOTIFY_SPEED_CHANGE
:
278 dev_dbg(&desc
->intf
->dev
, "SPEED_CHANGE received (len %u)\n",
282 clear_bit(WDM_POLL_RUNNING
, &desc
->flags
);
283 dev_err(&desc
->intf
->dev
,
284 "unknown notification %d received: index %d len %d\n",
285 dr
->bNotificationType
,
286 le16_to_cpu(dr
->wIndex
),
287 le16_to_cpu(dr
->wLength
));
291 spin_lock_irqsave(&desc
->iuspin
, flags
);
292 responding
= test_and_set_bit(WDM_RESPONDING
, &desc
->flags
);
293 if (!desc
->resp_count
++ && !responding
294 && !test_bit(WDM_DISCONNECTING
, &desc
->flags
)
295 && !test_bit(WDM_SUSPENDING
, &desc
->flags
)) {
296 rv
= usb_submit_urb(desc
->response
, GFP_ATOMIC
);
297 dev_dbg(&desc
->intf
->dev
, "submit response URB %d\n", rv
);
299 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
301 clear_bit(WDM_RESPONDING
, &desc
->flags
);
306 rv
= schedule_work(&desc
->rxwork
);
308 dev_err(&desc
->intf
->dev
,
309 "Cannot schedule work\n");
313 rv
= usb_submit_urb(urb
, GFP_ATOMIC
);
315 dev_err(&desc
->intf
->dev
,
316 "%s - usb_submit_urb failed with result %d\n",
321 static void kill_urbs(struct wdm_device
*desc
)
323 /* the order here is essential */
324 usb_kill_urb(desc
->command
);
325 usb_kill_urb(desc
->validity
);
326 usb_kill_urb(desc
->response
);
329 static void free_urbs(struct wdm_device
*desc
)
331 usb_free_urb(desc
->validity
);
332 usb_free_urb(desc
->response
);
333 usb_free_urb(desc
->command
);
336 static void cleanup(struct wdm_device
*desc
)
347 static ssize_t wdm_write
348 (struct file
*file
, const char __user
*buffer
, size_t count
, loff_t
*ppos
)
351 int rv
= -EMSGSIZE
, r
, we
;
352 struct wdm_device
*desc
= file
->private_data
;
353 struct usb_ctrlrequest
*req
;
355 if (count
> desc
->wMaxCommand
)
356 count
= desc
->wMaxCommand
;
358 spin_lock_irq(&desc
->iuspin
);
361 spin_unlock_irq(&desc
->iuspin
);
363 return usb_translate_errors(we
);
365 buf
= memdup_user(buffer
, count
);
369 /* concurrent writes and disconnect */
370 r
= mutex_lock_interruptible(&desc
->wlock
);
375 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
377 goto out_free_mem_lock
;
380 r
= usb_autopm_get_interface(desc
->intf
);
382 rv
= usb_translate_errors(r
);
383 goto out_free_mem_lock
;
386 if (!(file
->f_flags
& O_NONBLOCK
))
387 r
= wait_event_interruptible(desc
->wait
, !test_bit(WDM_IN_USE
,
390 if (test_bit(WDM_IN_USE
, &desc
->flags
))
393 if (test_bit(WDM_RESETTING
, &desc
->flags
))
398 goto out_free_mem_pm
;
402 usb_fill_control_urb(
404 interface_to_usbdev(desc
->intf
),
405 /* using common endpoint 0 */
406 usb_sndctrlpipe(interface_to_usbdev(desc
->intf
), 0),
407 (unsigned char *)req
,
414 req
->bRequestType
= (USB_DIR_OUT
| USB_TYPE_CLASS
|
415 USB_RECIP_INTERFACE
);
416 req
->bRequest
= USB_CDC_SEND_ENCAPSULATED_COMMAND
;
418 req
->wIndex
= desc
->inum
; /* already converted */
419 req
->wLength
= cpu_to_le16(count
);
420 set_bit(WDM_IN_USE
, &desc
->flags
);
423 rv
= usb_submit_urb(desc
->command
, GFP_KERNEL
);
426 clear_bit(WDM_IN_USE
, &desc
->flags
);
427 dev_err(&desc
->intf
->dev
, "Tx URB error: %d\n", rv
);
428 rv
= usb_translate_errors(rv
);
429 goto out_free_mem_pm
;
431 dev_dbg(&desc
->intf
->dev
, "Tx URB has been submitted index=%d\n",
432 le16_to_cpu(req
->wIndex
));
435 usb_autopm_put_interface(desc
->intf
);
436 mutex_unlock(&desc
->wlock
);
440 usb_autopm_put_interface(desc
->intf
);
442 mutex_unlock(&desc
->wlock
);
449 * Submit the read urb if resp_count is non-zero.
451 * Called with desc->iuspin locked
453 static int service_outstanding_interrupt(struct wdm_device
*desc
)
457 /* submit read urb only if the device is waiting for it */
458 if (!desc
->resp_count
|| !--desc
->resp_count
)
461 set_bit(WDM_RESPONDING
, &desc
->flags
);
462 spin_unlock_irq(&desc
->iuspin
);
463 rv
= usb_submit_urb(desc
->response
, GFP_KERNEL
);
464 spin_lock_irq(&desc
->iuspin
);
466 dev_err(&desc
->intf
->dev
,
467 "usb_submit_urb failed with result %d\n", rv
);
469 /* make sure the next notification trigger a submit */
470 clear_bit(WDM_RESPONDING
, &desc
->flags
);
471 desc
->resp_count
= 0;
477 static ssize_t wdm_read
478 (struct file
*file
, char __user
*buffer
, size_t count
, loff_t
*ppos
)
482 struct wdm_device
*desc
= file
->private_data
;
485 rv
= mutex_lock_interruptible(&desc
->rlock
); /*concurrent reads */
489 cntr
= READ_ONCE(desc
->length
);
493 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
497 if (test_bit(WDM_OVERFLOW
, &desc
->flags
)) {
498 clear_bit(WDM_OVERFLOW
, &desc
->flags
);
503 if (file
->f_flags
& O_NONBLOCK
) {
504 if (!test_bit(WDM_READ
, &desc
->flags
)) {
510 rv
= wait_event_interruptible(desc
->wait
,
511 test_bit(WDM_READ
, &desc
->flags
));
514 /* may have happened while we slept */
515 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
519 if (test_bit(WDM_RESETTING
, &desc
->flags
)) {
523 usb_mark_last_busy(interface_to_usbdev(desc
->intf
));
529 spin_lock_irq(&desc
->iuspin
);
531 if (desc
->rerr
) { /* read completed, error happened */
532 rv
= usb_translate_errors(desc
->rerr
);
534 spin_unlock_irq(&desc
->iuspin
);
538 * recheck whether we've lost the race
539 * against the completion handler
541 if (!test_bit(WDM_READ
, &desc
->flags
)) { /* lost race */
542 spin_unlock_irq(&desc
->iuspin
);
546 if (!desc
->reslength
) { /* zero length read */
547 dev_dbg(&desc
->intf
->dev
, "zero length - clearing WDM_READ\n");
548 clear_bit(WDM_READ
, &desc
->flags
);
549 rv
= service_outstanding_interrupt(desc
);
550 spin_unlock_irq(&desc
->iuspin
);
556 spin_unlock_irq(&desc
->iuspin
);
561 rv
= copy_to_user(buffer
, desc
->ubuf
, cntr
);
567 spin_lock_irq(&desc
->iuspin
);
569 for (i
= 0; i
< desc
->length
- cntr
; i
++)
570 desc
->ubuf
[i
] = desc
->ubuf
[i
+ cntr
];
572 desc
->length
-= cntr
;
573 /* in case we had outstanding data */
575 clear_bit(WDM_READ
, &desc
->flags
);
576 service_outstanding_interrupt(desc
);
578 spin_unlock_irq(&desc
->iuspin
);
582 mutex_unlock(&desc
->rlock
);
586 static int wdm_flush(struct file
*file
, fl_owner_t id
)
588 struct wdm_device
*desc
= file
->private_data
;
590 wait_event(desc
->wait
, !test_bit(WDM_IN_USE
, &desc
->flags
));
592 /* cannot dereference desc->intf if WDM_DISCONNECTING */
593 if (desc
->werr
< 0 && !test_bit(WDM_DISCONNECTING
, &desc
->flags
))
594 dev_err(&desc
->intf
->dev
, "Error in flush path: %d\n",
597 return usb_translate_errors(desc
->werr
);
600 static __poll_t
wdm_poll(struct file
*file
, struct poll_table_struct
*wait
)
602 struct wdm_device
*desc
= file
->private_data
;
606 spin_lock_irqsave(&desc
->iuspin
, flags
);
607 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
608 mask
= EPOLLHUP
| EPOLLERR
;
609 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
612 if (test_bit(WDM_READ
, &desc
->flags
))
613 mask
= EPOLLIN
| EPOLLRDNORM
;
614 if (desc
->rerr
|| desc
->werr
)
616 if (!test_bit(WDM_IN_USE
, &desc
->flags
))
617 mask
|= EPOLLOUT
| EPOLLWRNORM
;
618 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
620 poll_wait(file
, &desc
->wait
, wait
);
626 static int wdm_open(struct inode
*inode
, struct file
*file
)
628 int minor
= iminor(inode
);
630 struct usb_interface
*intf
;
631 struct wdm_device
*desc
;
633 mutex_lock(&wdm_mutex
);
634 desc
= wdm_find_device_by_minor(minor
);
639 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
))
641 file
->private_data
= desc
;
643 rv
= usb_autopm_get_interface(desc
->intf
);
645 dev_err(&desc
->intf
->dev
, "Error autopm - %d\n", rv
);
649 /* using write lock to protect desc->count */
650 mutex_lock(&desc
->wlock
);
651 if (!desc
->count
++) {
654 rv
= usb_submit_urb(desc
->validity
, GFP_KERNEL
);
657 dev_err(&desc
->intf
->dev
,
658 "Error submitting int urb - %d\n", rv
);
659 rv
= usb_translate_errors(rv
);
664 mutex_unlock(&desc
->wlock
);
665 if (desc
->count
== 1)
666 desc
->manage_power(intf
, 1);
667 usb_autopm_put_interface(desc
->intf
);
669 mutex_unlock(&wdm_mutex
);
673 static int wdm_release(struct inode
*inode
, struct file
*file
)
675 struct wdm_device
*desc
= file
->private_data
;
677 mutex_lock(&wdm_mutex
);
679 /* using write lock to protect desc->count */
680 mutex_lock(&desc
->wlock
);
682 mutex_unlock(&desc
->wlock
);
685 if (!test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
686 dev_dbg(&desc
->intf
->dev
, "wdm_release: cleanup\n");
688 spin_lock_irq(&desc
->iuspin
);
689 desc
->resp_count
= 0;
690 spin_unlock_irq(&desc
->iuspin
);
691 desc
->manage_power(desc
->intf
, 0);
693 /* must avoid dev_printk here as desc->intf is invalid */
694 pr_debug(KBUILD_MODNAME
" %s: device gone - cleaning up\n", __func__
);
698 mutex_unlock(&wdm_mutex
);
702 static long wdm_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
704 struct wdm_device
*desc
= file
->private_data
;
708 case IOCTL_WDM_MAX_COMMAND
:
709 if (copy_to_user((void __user
*)arg
, &desc
->wMaxCommand
, sizeof(desc
->wMaxCommand
)))
718 static const struct file_operations wdm_fops
= {
719 .owner
= THIS_MODULE
,
724 .release
= wdm_release
,
726 .unlocked_ioctl
= wdm_ioctl
,
727 .compat_ioctl
= wdm_ioctl
,
728 .llseek
= noop_llseek
,
731 static struct usb_class_driver wdm_class
= {
734 .minor_base
= WDM_MINOR_BASE
,
737 /* --- error handling --- */
738 static void wdm_rxwork(struct work_struct
*work
)
740 struct wdm_device
*desc
= container_of(work
, struct wdm_device
, rxwork
);
745 spin_lock_irqsave(&desc
->iuspin
, flags
);
746 if (test_bit(WDM_DISCONNECTING
, &desc
->flags
)) {
747 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
749 responding
= test_and_set_bit(WDM_RESPONDING
, &desc
->flags
);
750 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
752 rv
= usb_submit_urb(desc
->response
, GFP_KERNEL
);
753 if (rv
< 0 && rv
!= -EPERM
) {
754 spin_lock_irqsave(&desc
->iuspin
, flags
);
755 clear_bit(WDM_RESPONDING
, &desc
->flags
);
756 if (!test_bit(WDM_DISCONNECTING
, &desc
->flags
))
757 schedule_work(&desc
->rxwork
);
758 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
763 static void service_interrupt_work(struct work_struct
*work
)
765 struct wdm_device
*desc
;
767 desc
= container_of(work
, struct wdm_device
, service_outs_intr
);
769 spin_lock_irq(&desc
->iuspin
);
770 service_outstanding_interrupt(desc
);
771 if (!desc
->resp_count
) {
772 set_bit(WDM_READ
, &desc
->flags
);
773 wake_up(&desc
->wait
);
775 spin_unlock_irq(&desc
->iuspin
);
778 /* --- hotplug --- */
780 static int wdm_create(struct usb_interface
*intf
, struct usb_endpoint_descriptor
*ep
,
781 u16 bufsize
, int (*manage_power
)(struct usb_interface
*, int))
784 struct wdm_device
*desc
;
786 desc
= kzalloc(sizeof(struct wdm_device
), GFP_KERNEL
);
789 INIT_LIST_HEAD(&desc
->device_list
);
790 mutex_init(&desc
->rlock
);
791 mutex_init(&desc
->wlock
);
792 spin_lock_init(&desc
->iuspin
);
793 init_waitqueue_head(&desc
->wait
);
794 desc
->wMaxCommand
= bufsize
;
795 /* this will be expanded and needed in hardware endianness */
796 desc
->inum
= cpu_to_le16((u16
)intf
->cur_altsetting
->desc
.bInterfaceNumber
);
798 INIT_WORK(&desc
->rxwork
, wdm_rxwork
);
799 INIT_WORK(&desc
->service_outs_intr
, service_interrupt_work
);
802 if (!usb_endpoint_is_int_in(ep
))
805 desc
->wMaxPacketSize
= usb_endpoint_maxp(ep
);
807 desc
->orq
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_KERNEL
);
810 desc
->irq
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_KERNEL
);
814 desc
->validity
= usb_alloc_urb(0, GFP_KERNEL
);
818 desc
->response
= usb_alloc_urb(0, GFP_KERNEL
);
822 desc
->command
= usb_alloc_urb(0, GFP_KERNEL
);
826 desc
->ubuf
= kmalloc(desc
->wMaxCommand
, GFP_KERNEL
);
830 desc
->sbuf
= kmalloc(desc
->wMaxPacketSize
, GFP_KERNEL
);
834 desc
->inbuf
= kmalloc(desc
->wMaxCommand
, GFP_KERNEL
);
840 interface_to_usbdev(intf
),
841 usb_rcvintpipe(interface_to_usbdev(intf
), ep
->bEndpointAddress
),
843 desc
->wMaxPacketSize
,
849 desc
->irq
->bRequestType
= (USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
);
850 desc
->irq
->bRequest
= USB_CDC_GET_ENCAPSULATED_RESPONSE
;
851 desc
->irq
->wValue
= 0;
852 desc
->irq
->wIndex
= desc
->inum
; /* already converted */
853 desc
->irq
->wLength
= cpu_to_le16(desc
->wMaxCommand
);
855 usb_fill_control_urb(
857 interface_to_usbdev(intf
),
858 /* using common endpoint 0 */
859 usb_rcvctrlpipe(interface_to_usbdev(desc
->intf
), 0),
860 (unsigned char *)desc
->irq
,
867 desc
->manage_power
= manage_power
;
869 spin_lock(&wdm_device_list_lock
);
870 list_add(&desc
->device_list
, &wdm_device_list
);
871 spin_unlock(&wdm_device_list_lock
);
873 rv
= usb_register_dev(intf
, &wdm_class
);
877 dev_info(&intf
->dev
, "%s: USB WDM device\n", dev_name(intf
->usb_dev
));
881 spin_lock(&wdm_device_list_lock
);
882 list_del(&desc
->device_list
);
883 spin_unlock(&wdm_device_list_lock
);
888 static int wdm_manage_power(struct usb_interface
*intf
, int on
)
890 /* need autopm_get/put here to ensure the usbcore sees the new value */
891 int rv
= usb_autopm_get_interface(intf
);
893 intf
->needs_remote_wakeup
= on
;
895 usb_autopm_put_interface(intf
);
899 static int wdm_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
902 struct usb_host_interface
*iface
;
903 struct usb_endpoint_descriptor
*ep
;
904 struct usb_cdc_parsed_header hdr
;
905 u8
*buffer
= intf
->altsetting
->extra
;
906 int buflen
= intf
->altsetting
->extralen
;
907 u16 maxcom
= WDM_DEFAULT_BUFSIZE
;
912 cdc_parse_cdc_header(&hdr
, intf
, buffer
, buflen
);
914 if (hdr
.usb_cdc_dmm_desc
)
915 maxcom
= le16_to_cpu(hdr
.usb_cdc_dmm_desc
->wMaxCommand
);
917 iface
= intf
->cur_altsetting
;
918 if (iface
->desc
.bNumEndpoints
!= 1)
920 ep
= &iface
->endpoint
[0].desc
;
922 rv
= wdm_create(intf
, ep
, maxcom
, &wdm_manage_power
);
929 * usb_cdc_wdm_register - register a WDM subdriver
930 * @intf: usb interface the subdriver will associate with
931 * @ep: interrupt endpoint to monitor for notifications
932 * @bufsize: maximum message size to support for read/write
934 * Create WDM usb class character device and associate it with intf
935 * without binding, allowing another driver to manage the interface.
937 * The subdriver will manage the given interrupt endpoint exclusively
938 * and will issue control requests referring to the given intf. It
939 * will otherwise avoid interferring, and in particular not do
940 * usb_set_intfdata/usb_get_intfdata on intf.
942 * The return value is a pointer to the subdriver's struct usb_driver.
943 * The registering driver is responsible for calling this subdriver's
944 * disconnect, suspend, resume, pre_reset and post_reset methods from
947 struct usb_driver
*usb_cdc_wdm_register(struct usb_interface
*intf
,
948 struct usb_endpoint_descriptor
*ep
,
950 int (*manage_power
)(struct usb_interface
*, int))
954 rv
= wdm_create(intf
, ep
, bufsize
, manage_power
);
962 EXPORT_SYMBOL(usb_cdc_wdm_register
);
964 static void wdm_disconnect(struct usb_interface
*intf
)
966 struct wdm_device
*desc
;
969 usb_deregister_dev(intf
, &wdm_class
);
970 desc
= wdm_find_device(intf
);
971 mutex_lock(&wdm_mutex
);
973 /* the spinlock makes sure no new urbs are generated in the callbacks */
974 spin_lock_irqsave(&desc
->iuspin
, flags
);
975 set_bit(WDM_DISCONNECTING
, &desc
->flags
);
976 set_bit(WDM_READ
, &desc
->flags
);
977 /* to terminate pending flushes */
978 clear_bit(WDM_IN_USE
, &desc
->flags
);
979 spin_unlock_irqrestore(&desc
->iuspin
, flags
);
980 wake_up_all(&desc
->wait
);
981 mutex_lock(&desc
->rlock
);
982 mutex_lock(&desc
->wlock
);
984 cancel_work_sync(&desc
->rxwork
);
985 cancel_work_sync(&desc
->service_outs_intr
);
986 mutex_unlock(&desc
->wlock
);
987 mutex_unlock(&desc
->rlock
);
989 /* the desc->intf pointer used as list key is now invalid */
990 spin_lock(&wdm_device_list_lock
);
991 list_del(&desc
->device_list
);
992 spin_unlock(&wdm_device_list_lock
);
997 dev_dbg(&intf
->dev
, "%d open files - postponing cleanup\n", desc
->count
);
998 mutex_unlock(&wdm_mutex
);
1002 static int wdm_suspend(struct usb_interface
*intf
, pm_message_t message
)
1004 struct wdm_device
*desc
= wdm_find_device(intf
);
1007 dev_dbg(&desc
->intf
->dev
, "wdm%d_suspend\n", intf
->minor
);
1009 /* if this is an autosuspend the caller does the locking */
1010 if (!PMSG_IS_AUTO(message
)) {
1011 mutex_lock(&desc
->rlock
);
1012 mutex_lock(&desc
->wlock
);
1014 spin_lock_irq(&desc
->iuspin
);
1016 if (PMSG_IS_AUTO(message
) &&
1017 (test_bit(WDM_IN_USE
, &desc
->flags
)
1018 || test_bit(WDM_RESPONDING
, &desc
->flags
))) {
1019 spin_unlock_irq(&desc
->iuspin
);
1023 set_bit(WDM_SUSPENDING
, &desc
->flags
);
1024 spin_unlock_irq(&desc
->iuspin
);
1025 /* callback submits work - order is essential */
1027 cancel_work_sync(&desc
->rxwork
);
1028 cancel_work_sync(&desc
->service_outs_intr
);
1030 if (!PMSG_IS_AUTO(message
)) {
1031 mutex_unlock(&desc
->wlock
);
1032 mutex_unlock(&desc
->rlock
);
1039 static int recover_from_urb_loss(struct wdm_device
*desc
)
1044 rv
= usb_submit_urb(desc
->validity
, GFP_NOIO
);
1046 dev_err(&desc
->intf
->dev
,
1047 "Error resume submitting int urb - %d\n", rv
);
1053 static int wdm_resume(struct usb_interface
*intf
)
1055 struct wdm_device
*desc
= wdm_find_device(intf
);
1058 dev_dbg(&desc
->intf
->dev
, "wdm%d_resume\n", intf
->minor
);
1060 clear_bit(WDM_SUSPENDING
, &desc
->flags
);
1061 rv
= recover_from_urb_loss(desc
);
1067 static int wdm_pre_reset(struct usb_interface
*intf
)
1069 struct wdm_device
*desc
= wdm_find_device(intf
);
1072 * we notify everybody using poll of
1073 * an exceptional situation
1074 * must be done before recovery lest a spontaneous
1075 * message from the device is lost
1077 spin_lock_irq(&desc
->iuspin
);
1078 set_bit(WDM_RESETTING
, &desc
->flags
); /* inform read/write */
1079 set_bit(WDM_READ
, &desc
->flags
); /* unblock read */
1080 clear_bit(WDM_IN_USE
, &desc
->flags
); /* unblock write */
1081 desc
->rerr
= -EINTR
;
1082 spin_unlock_irq(&desc
->iuspin
);
1083 wake_up_all(&desc
->wait
);
1084 mutex_lock(&desc
->rlock
);
1085 mutex_lock(&desc
->wlock
);
1087 cancel_work_sync(&desc
->rxwork
);
1088 cancel_work_sync(&desc
->service_outs_intr
);
1092 static int wdm_post_reset(struct usb_interface
*intf
)
1094 struct wdm_device
*desc
= wdm_find_device(intf
);
1097 clear_bit(WDM_OVERFLOW
, &desc
->flags
);
1098 clear_bit(WDM_RESETTING
, &desc
->flags
);
1099 rv
= recover_from_urb_loss(desc
);
1100 mutex_unlock(&desc
->wlock
);
1101 mutex_unlock(&desc
->rlock
);
1105 static struct usb_driver wdm_driver
= {
1108 .disconnect
= wdm_disconnect
,
1110 .suspend
= wdm_suspend
,
1111 .resume
= wdm_resume
,
1112 .reset_resume
= wdm_resume
,
1114 .pre_reset
= wdm_pre_reset
,
1115 .post_reset
= wdm_post_reset
,
1116 .id_table
= wdm_ids
,
1117 .supports_autosuspend
= 1,
1118 .disable_hub_initiated_lpm
= 1,
1121 module_usb_driver(wdm_driver
);
1123 MODULE_AUTHOR(DRIVER_AUTHOR
);
1124 MODULE_DESCRIPTION(DRIVER_DESC
);
1125 MODULE_LICENSE("GPL");