| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 1 | /* | 
|  | 2 | * cdc-wdm.c | 
|  | 3 | * | 
|  | 4 | * This driver supports USB CDC WCM Device Management. | 
|  | 5 | * | 
| Oliver Neukum | 052fbc0 | 2009-04-20 17:24:49 +0200 | [diff] [blame] | 6 | * Copyright (c) 2007-2009 Oliver Neukum | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 7 | * | 
|  | 8 | * Some code taken from cdc-acm.c | 
|  | 9 | * | 
|  | 10 | * Released under the GPLv2. | 
|  | 11 | * | 
|  | 12 | * Many thanks to Carl Nordbeck | 
|  | 13 | */ | 
|  | 14 | #include <linux/kernel.h> | 
|  | 15 | #include <linux/errno.h> | 
|  | 16 | #include <linux/slab.h> | 
|  | 17 | #include <linux/module.h> | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 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> | 
| Bjørn Mork | 3cc3615 | 2012-03-06 17:29:22 +0100 | [diff] [blame] | 26 | #include <linux/usb/cdc-wdm.h> | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 27 |  | 
|  | 28 | /* | 
|  | 29 | * Version Information | 
|  | 30 | */ | 
| Oliver Neukum | 87d65e5 | 2008-06-19 14:20:18 +0200 | [diff] [blame] | 31 | #define DRIVER_VERSION "v0.03" | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 32 | #define DRIVER_AUTHOR "Oliver Neukum" | 
| Oliver Neukum | 87d65e5 | 2008-06-19 14:20:18 +0200 | [diff] [blame] | 33 | #define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management" | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 34 |  | 
| Németh Márton | 6ef4852 | 2010-01-10 15:33:45 +0100 | [diff] [blame] | 35 | static const struct usb_device_id wdm_ids[] = { | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 36 | { | 
|  | 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 | 
|  | 41 | }, | 
|  | 42 | { } | 
|  | 43 | }; | 
|  | 44 |  | 
| Oliver Neukum | aa5380b | 2008-10-13 14:05:20 +0200 | [diff] [blame] | 45 | MODULE_DEVICE_TABLE (usb, wdm_ids); | 
|  | 46 |  | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 47 | #define WDM_MINOR_BASE	176 | 
|  | 48 |  | 
|  | 49 |  | 
|  | 50 | #define WDM_IN_USE		1 | 
|  | 51 | #define WDM_DISCONNECTING	2 | 
|  | 52 | #define WDM_RESULT		3 | 
|  | 53 | #define WDM_READ		4 | 
|  | 54 | #define WDM_INT_STALL		5 | 
|  | 55 | #define WDM_POLL_RUNNING	6 | 
| Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 56 | #define WDM_RESPONDING		7 | 
| Oliver Neukum | beb1d35 | 2010-02-27 20:55:52 +0100 | [diff] [blame] | 57 | #define WDM_SUSPENDING		8 | 
| Bjørn Mork | 8804420 | 2012-02-10 09:44:08 +0100 | [diff] [blame] | 58 | #define WDM_RESETTING		9 | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 59 |  | 
|  | 60 | #define WDM_MAX			16 | 
|  | 61 |  | 
| Bjørn Mork | 7e3054a | 2012-01-20 01:49:57 +0100 | [diff] [blame] | 62 | /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */ | 
|  | 63 | #define WDM_DEFAULT_BUFSIZE	256 | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 64 |  | 
|  | 65 | static DEFINE_MUTEX(wdm_mutex); | 
| Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 66 | static DEFINE_SPINLOCK(wdm_device_list_lock); | 
|  | 67 | static LIST_HEAD(wdm_device_list); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 68 |  | 
|  | 69 | /* --- method tables --- */ | 
|  | 70 |  | 
|  | 71 | struct wdm_device { | 
|  | 72 | u8			*inbuf; /* buffer for response */ | 
|  | 73 | u8			*outbuf; /* buffer for command */ | 
|  | 74 | u8			*sbuf; /* buffer for status */ | 
|  | 75 | u8			*ubuf; /* buffer for copy to user space */ | 
|  | 76 |  | 
|  | 77 | struct urb		*command; | 
|  | 78 | struct urb		*response; | 
|  | 79 | struct urb		*validity; | 
|  | 80 | struct usb_interface	*intf; | 
|  | 81 | struct usb_ctrlrequest	*orq; | 
|  | 82 | struct usb_ctrlrequest	*irq; | 
|  | 83 | spinlock_t		iuspin; | 
|  | 84 |  | 
|  | 85 | unsigned long		flags; | 
|  | 86 | u16			bufsize; | 
|  | 87 | u16			wMaxCommand; | 
|  | 88 | u16			wMaxPacketSize; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 89 | __le16			inum; | 
|  | 90 | int			reslength; | 
|  | 91 | int			length; | 
|  | 92 | int			read; | 
|  | 93 | int			count; | 
|  | 94 | dma_addr_t		shandle; | 
|  | 95 | dma_addr_t		ihandle; | 
| Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 96 | struct mutex		wlock; | 
|  | 97 | struct mutex		rlock; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 98 | wait_queue_head_t	wait; | 
|  | 99 | struct work_struct	rxwork; | 
|  | 100 | int			werr; | 
|  | 101 | int			rerr; | 
| Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 102 |  | 
|  | 103 | struct list_head	device_list; | 
| Bjørn Mork | 3cc3615 | 2012-03-06 17:29:22 +0100 | [diff] [blame] | 104 | int			(*manage_power)(struct usb_interface *, int); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 105 | }; | 
|  | 106 |  | 
|  | 107 | static struct usb_driver wdm_driver; | 
|  | 108 |  | 
| Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 109 | /* return intfdata if we own the interface, else look up intf in the list */ | 
|  | 110 | static struct wdm_device *wdm_find_device(struct usb_interface *intf) | 
|  | 111 | { | 
| Bjørn Mork | 6a44886 | 2012-09-10 22:17:34 +0200 | [diff] [blame] | 112 | struct wdm_device *desc; | 
| Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 113 |  | 
|  | 114 | spin_lock(&wdm_device_list_lock); | 
|  | 115 | list_for_each_entry(desc, &wdm_device_list, device_list) | 
|  | 116 | if (desc->intf == intf) | 
| Bjørn Mork | 6a44886 | 2012-09-10 22:17:34 +0200 | [diff] [blame] | 117 | goto found; | 
|  | 118 | desc = NULL; | 
|  | 119 | found: | 
| Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 120 | spin_unlock(&wdm_device_list_lock); | 
|  | 121 |  | 
|  | 122 | return desc; | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | static struct wdm_device *wdm_find_device_by_minor(int minor) | 
|  | 126 | { | 
| Bjørn Mork | 6a44886 | 2012-09-10 22:17:34 +0200 | [diff] [blame] | 127 | struct wdm_device *desc; | 
| Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 128 |  | 
|  | 129 | spin_lock(&wdm_device_list_lock); | 
|  | 130 | list_for_each_entry(desc, &wdm_device_list, device_list) | 
|  | 131 | if (desc->intf->minor == minor) | 
| Bjørn Mork | 6a44886 | 2012-09-10 22:17:34 +0200 | [diff] [blame] | 132 | goto found; | 
|  | 133 | desc = NULL; | 
|  | 134 | found: | 
| Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 135 | spin_unlock(&wdm_device_list_lock); | 
|  | 136 |  | 
|  | 137 | return desc; | 
|  | 138 | } | 
|  | 139 |  | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 140 | /* --- callbacks --- */ | 
|  | 141 | static void wdm_out_callback(struct urb *urb) | 
|  | 142 | { | 
|  | 143 | struct wdm_device *desc; | 
|  | 144 | desc = urb->context; | 
|  | 145 | spin_lock(&desc->iuspin); | 
|  | 146 | desc->werr = urb->status; | 
|  | 147 | spin_unlock(&desc->iuspin); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 148 | kfree(desc->outbuf); | 
| Oliver Neukum | 5c22837 | 2012-04-26 21:59:10 +0200 | [diff] [blame] | 149 | desc->outbuf = NULL; | 
|  | 150 | clear_bit(WDM_IN_USE, &desc->flags); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 151 | wake_up(&desc->wait); | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | static void wdm_in_callback(struct urb *urb) | 
|  | 155 | { | 
|  | 156 | struct wdm_device *desc = urb->context; | 
|  | 157 | int status = urb->status; | 
|  | 158 |  | 
|  | 159 | spin_lock(&desc->iuspin); | 
| Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 160 | clear_bit(WDM_RESPONDING, &desc->flags); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 161 |  | 
|  | 162 | if (status) { | 
|  | 163 | switch (status) { | 
|  | 164 | case -ENOENT: | 
|  | 165 | dev_dbg(&desc->intf->dev, | 
|  | 166 | "nonzero urb status received: -ENOENT"); | 
| Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 167 | goto skip_error; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 168 | case -ECONNRESET: | 
|  | 169 | dev_dbg(&desc->intf->dev, | 
|  | 170 | "nonzero urb status received: -ECONNRESET"); | 
| Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 171 | goto skip_error; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 172 | case -ESHUTDOWN: | 
|  | 173 | dev_dbg(&desc->intf->dev, | 
|  | 174 | "nonzero urb status received: -ESHUTDOWN"); | 
| Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 175 | goto skip_error; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 176 | case -EPIPE: | 
| Greg Kroah-Hartman | 9908a32e9 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 177 | dev_err(&desc->intf->dev, | 
|  | 178 | "nonzero urb status received: -EPIPE\n"); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 179 | break; | 
|  | 180 | default: | 
| Greg Kroah-Hartman | 9908a32e9 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 181 | dev_err(&desc->intf->dev, | 
|  | 182 | "Unexpected error %d\n", status); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 183 | break; | 
|  | 184 | } | 
|  | 185 | } | 
|  | 186 |  | 
|  | 187 | desc->rerr = status; | 
|  | 188 | desc->reslength = urb->actual_length; | 
|  | 189 | memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength); | 
|  | 190 | desc->length += desc->reslength; | 
| Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 191 | skip_error: | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 192 | wake_up(&desc->wait); | 
|  | 193 |  | 
|  | 194 | set_bit(WDM_READ, &desc->flags); | 
|  | 195 | spin_unlock(&desc->iuspin); | 
|  | 196 | } | 
|  | 197 |  | 
|  | 198 | static void wdm_int_callback(struct urb *urb) | 
|  | 199 | { | 
|  | 200 | int rv = 0; | 
|  | 201 | int status = urb->status; | 
|  | 202 | struct wdm_device *desc; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 203 | struct usb_cdc_notification *dr; | 
|  | 204 |  | 
|  | 205 | desc = urb->context; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 206 | dr = (struct usb_cdc_notification *)desc->sbuf; | 
|  | 207 |  | 
|  | 208 | if (status) { | 
|  | 209 | switch (status) { | 
|  | 210 | case -ESHUTDOWN: | 
|  | 211 | case -ENOENT: | 
|  | 212 | case -ECONNRESET: | 
|  | 213 | return; /* unplug */ | 
|  | 214 | case -EPIPE: | 
|  | 215 | set_bit(WDM_INT_STALL, &desc->flags); | 
| Greg Kroah-Hartman | 9908a32e9 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 216 | dev_err(&desc->intf->dev, "Stall on int endpoint\n"); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 217 | goto sw; /* halt is cleared in work */ | 
|  | 218 | default: | 
| Greg Kroah-Hartman | 9908a32e9 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 219 | dev_err(&desc->intf->dev, | 
|  | 220 | "nonzero urb status received: %d\n", status); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 221 | break; | 
|  | 222 | } | 
|  | 223 | } | 
|  | 224 |  | 
|  | 225 | if (urb->actual_length < sizeof(struct usb_cdc_notification)) { | 
| Greg Kroah-Hartman | 9908a32e9 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 226 | dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n", | 
|  | 227 | urb->actual_length); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 228 | goto exit; | 
|  | 229 | } | 
|  | 230 |  | 
|  | 231 | switch (dr->bNotificationType) { | 
|  | 232 | case USB_CDC_NOTIFY_RESPONSE_AVAILABLE: | 
|  | 233 | dev_dbg(&desc->intf->dev, | 
|  | 234 | "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d", | 
|  | 235 | dr->wIndex, dr->wLength); | 
|  | 236 | break; | 
|  | 237 |  | 
|  | 238 | case USB_CDC_NOTIFY_NETWORK_CONNECTION: | 
|  | 239 |  | 
|  | 240 | dev_dbg(&desc->intf->dev, | 
|  | 241 | "NOTIFY_NETWORK_CONNECTION %s network", | 
|  | 242 | dr->wValue ? "connected to" : "disconnected from"); | 
|  | 243 | goto exit; | 
|  | 244 | default: | 
|  | 245 | clear_bit(WDM_POLL_RUNNING, &desc->flags); | 
| Greg Kroah-Hartman | 9908a32e9 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 246 | dev_err(&desc->intf->dev, | 
|  | 247 | "unknown notification %d received: index %d len %d\n", | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 248 | dr->bNotificationType, dr->wIndex, dr->wLength); | 
|  | 249 | goto exit; | 
|  | 250 | } | 
|  | 251 |  | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 252 | spin_lock(&desc->iuspin); | 
|  | 253 | clear_bit(WDM_READ, &desc->flags); | 
| Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 254 | set_bit(WDM_RESPONDING, &desc->flags); | 
| Oliver Neukum | beb1d35 | 2010-02-27 20:55:52 +0100 | [diff] [blame] | 255 | if (!test_bit(WDM_DISCONNECTING, &desc->flags) | 
|  | 256 | && !test_bit(WDM_SUSPENDING, &desc->flags)) { | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 257 | rv = usb_submit_urb(desc->response, GFP_ATOMIC); | 
|  | 258 | dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d", | 
|  | 259 | __func__, rv); | 
|  | 260 | } | 
|  | 261 | spin_unlock(&desc->iuspin); | 
|  | 262 | if (rv < 0) { | 
| Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 263 | clear_bit(WDM_RESPONDING, &desc->flags); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 264 | if (rv == -EPERM) | 
|  | 265 | return; | 
|  | 266 | if (rv == -ENOMEM) { | 
|  | 267 | sw: | 
|  | 268 | rv = schedule_work(&desc->rxwork); | 
|  | 269 | if (rv) | 
| Greg Kroah-Hartman | 9908a32e9 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 270 | dev_err(&desc->intf->dev, | 
|  | 271 | "Cannot schedule work\n"); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 272 | } | 
|  | 273 | } | 
|  | 274 | exit: | 
|  | 275 | rv = usb_submit_urb(urb, GFP_ATOMIC); | 
|  | 276 | if (rv) | 
| Greg Kroah-Hartman | 9908a32e9 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 277 | dev_err(&desc->intf->dev, | 
|  | 278 | "%s - usb_submit_urb failed with result %d\n", | 
|  | 279 | __func__, rv); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 280 |  | 
|  | 281 | } | 
|  | 282 |  | 
|  | 283 | static void kill_urbs(struct wdm_device *desc) | 
|  | 284 | { | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 285 | /* the order here is essential */ | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 286 | usb_kill_urb(desc->command); | 
|  | 287 | usb_kill_urb(desc->validity); | 
|  | 288 | usb_kill_urb(desc->response); | 
|  | 289 | } | 
|  | 290 |  | 
|  | 291 | static void free_urbs(struct wdm_device *desc) | 
|  | 292 | { | 
|  | 293 | usb_free_urb(desc->validity); | 
|  | 294 | usb_free_urb(desc->response); | 
|  | 295 | usb_free_urb(desc->command); | 
|  | 296 | } | 
|  | 297 |  | 
|  | 298 | static void cleanup(struct wdm_device *desc) | 
|  | 299 | { | 
| Bjørn Mork | 8457d99 | 2012-01-16 15:12:00 +0100 | [diff] [blame] | 300 | kfree(desc->sbuf); | 
|  | 301 | kfree(desc->inbuf); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 302 | kfree(desc->orq); | 
|  | 303 | kfree(desc->irq); | 
|  | 304 | kfree(desc->ubuf); | 
|  | 305 | free_urbs(desc); | 
|  | 306 | kfree(desc); | 
|  | 307 | } | 
|  | 308 |  | 
|  | 309 | static ssize_t wdm_write | 
|  | 310 | (struct file *file, const char __user *buffer, size_t count, loff_t *ppos) | 
|  | 311 | { | 
|  | 312 | u8 *buf; | 
|  | 313 | int rv = -EMSGSIZE, r, we; | 
|  | 314 | struct wdm_device *desc = file->private_data; | 
|  | 315 | struct usb_ctrlrequest *req; | 
|  | 316 |  | 
|  | 317 | if (count > desc->wMaxCommand) | 
|  | 318 | count = desc->wMaxCommand; | 
|  | 319 |  | 
|  | 320 | spin_lock_irq(&desc->iuspin); | 
|  | 321 | we = desc->werr; | 
|  | 322 | desc->werr = 0; | 
|  | 323 | spin_unlock_irq(&desc->iuspin); | 
|  | 324 | if (we < 0) | 
|  | 325 | return -EIO; | 
|  | 326 |  | 
| Oliver Neukum | 5c22837 | 2012-04-26 21:59:10 +0200 | [diff] [blame] | 327 | buf = kmalloc(count, GFP_KERNEL); | 
| Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 328 | if (!buf) { | 
|  | 329 | rv = -ENOMEM; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 330 | goto outnl; | 
| Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 331 | } | 
|  | 332 |  | 
|  | 333 | r = copy_from_user(buf, buffer, count); | 
|  | 334 | if (r > 0) { | 
|  | 335 | kfree(buf); | 
|  | 336 | rv = -EFAULT; | 
|  | 337 | goto outnl; | 
|  | 338 | } | 
|  | 339 |  | 
|  | 340 | /* concurrent writes and disconnect */ | 
| Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 341 | r = mutex_lock_interruptible(&desc->wlock); | 
| Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 342 | rv = -ERESTARTSYS; | 
|  | 343 | if (r) { | 
|  | 344 | kfree(buf); | 
|  | 345 | goto outnl; | 
|  | 346 | } | 
|  | 347 |  | 
|  | 348 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { | 
|  | 349 | kfree(buf); | 
|  | 350 | rv = -ENODEV; | 
|  | 351 | goto outnp; | 
|  | 352 | } | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 353 |  | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 354 | r = usb_autopm_get_interface(desc->intf); | 
| Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 355 | if (r < 0) { | 
|  | 356 | kfree(buf); | 
| Oliver Neukum | 12a98b2 | 2012-04-30 09:57:31 +0200 | [diff] [blame] | 357 | rv = usb_translate_errors(r); | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 358 | goto outnp; | 
| Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 359 | } | 
| Oliver Neukum | 7f1dc31 | 2009-09-09 10:12:48 +0200 | [diff] [blame] | 360 |  | 
| David Sterba | 0cdfb81 | 2010-12-27 18:49:58 +0100 | [diff] [blame] | 361 | if (!(file->f_flags & O_NONBLOCK)) | 
| Oliver Neukum | 7f1dc31 | 2009-09-09 10:12:48 +0200 | [diff] [blame] | 362 | r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE, | 
|  | 363 | &desc->flags)); | 
|  | 364 | else | 
|  | 365 | if (test_bit(WDM_IN_USE, &desc->flags)) | 
|  | 366 | r = -EAGAIN; | 
| Bjørn Mork | 8804420 | 2012-02-10 09:44:08 +0100 | [diff] [blame] | 367 |  | 
|  | 368 | if (test_bit(WDM_RESETTING, &desc->flags)) | 
|  | 369 | r = -EIO; | 
|  | 370 |  | 
| Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 371 | if (r < 0) { | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 372 | kfree(buf); | 
| Oliver Neukum | 12a98b2 | 2012-04-30 09:57:31 +0200 | [diff] [blame] | 373 | rv = r; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 374 | goto out; | 
|  | 375 | } | 
|  | 376 |  | 
|  | 377 | req = desc->orq; | 
|  | 378 | usb_fill_control_urb( | 
|  | 379 | desc->command, | 
|  | 380 | interface_to_usbdev(desc->intf), | 
|  | 381 | /* using common endpoint 0 */ | 
|  | 382 | usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0), | 
|  | 383 | (unsigned char *)req, | 
|  | 384 | buf, | 
|  | 385 | count, | 
|  | 386 | wdm_out_callback, | 
|  | 387 | desc | 
|  | 388 | ); | 
|  | 389 |  | 
|  | 390 | req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS | | 
|  | 391 | USB_RECIP_INTERFACE); | 
|  | 392 | req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND; | 
|  | 393 | req->wValue = 0; | 
|  | 394 | req->wIndex = desc->inum; | 
|  | 395 | req->wLength = cpu_to_le16(count); | 
|  | 396 | set_bit(WDM_IN_USE, &desc->flags); | 
| Oliver Neukum | 5c22837 | 2012-04-26 21:59:10 +0200 | [diff] [blame] | 397 | desc->outbuf = buf; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 398 |  | 
|  | 399 | rv = usb_submit_urb(desc->command, GFP_KERNEL); | 
|  | 400 | if (rv < 0) { | 
|  | 401 | kfree(buf); | 
| Oliver Neukum | 5c22837 | 2012-04-26 21:59:10 +0200 | [diff] [blame] | 402 | desc->outbuf = NULL; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 403 | clear_bit(WDM_IN_USE, &desc->flags); | 
| Greg Kroah-Hartman | 9908a32e9 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 404 | dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv); | 
| Oliver Neukum | 12a98b2 | 2012-04-30 09:57:31 +0200 | [diff] [blame] | 405 | rv = usb_translate_errors(rv); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 406 | } else { | 
|  | 407 | dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d", | 
|  | 408 | req->wIndex); | 
|  | 409 | } | 
|  | 410 | out: | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 411 | usb_autopm_put_interface(desc->intf); | 
|  | 412 | outnp: | 
| Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 413 | mutex_unlock(&desc->wlock); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 414 | outnl: | 
|  | 415 | return rv < 0 ? rv : count; | 
|  | 416 | } | 
|  | 417 |  | 
|  | 418 | static ssize_t wdm_read | 
|  | 419 | (struct file *file, char __user *buffer, size_t count, loff_t *ppos) | 
|  | 420 | { | 
| Ben Hutchings | 711c68b | 2012-02-12 06:00:41 +0000 | [diff] [blame] | 421 | int rv, cntr; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 422 | int i = 0; | 
|  | 423 | struct wdm_device *desc = file->private_data; | 
|  | 424 |  | 
|  | 425 |  | 
| Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 426 | rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */ | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 427 | if (rv < 0) | 
|  | 428 | return -ERESTARTSYS; | 
|  | 429 |  | 
| Ben Hutchings | 711c68b | 2012-02-12 06:00:41 +0000 | [diff] [blame] | 430 | cntr = ACCESS_ONCE(desc->length); | 
|  | 431 | if (cntr == 0) { | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 432 | desc->read = 0; | 
|  | 433 | retry: | 
| Oliver Neukum | 7f1dc31 | 2009-09-09 10:12:48 +0200 | [diff] [blame] | 434 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { | 
|  | 435 | rv = -ENODEV; | 
|  | 436 | goto err; | 
|  | 437 | } | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 438 | i++; | 
| Oliver Neukum | 7f1dc31 | 2009-09-09 10:12:48 +0200 | [diff] [blame] | 439 | if (file->f_flags & O_NONBLOCK) { | 
|  | 440 | if (!test_bit(WDM_READ, &desc->flags)) { | 
|  | 441 | rv = cntr ? cntr : -EAGAIN; | 
|  | 442 | goto err; | 
|  | 443 | } | 
|  | 444 | rv = 0; | 
|  | 445 | } else { | 
|  | 446 | rv = wait_event_interruptible(desc->wait, | 
|  | 447 | test_bit(WDM_READ, &desc->flags)); | 
|  | 448 | } | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 449 |  | 
| Oliver Neukum | 7f1dc31 | 2009-09-09 10:12:48 +0200 | [diff] [blame] | 450 | /* may have happened while we slept */ | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 451 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { | 
|  | 452 | rv = -ENODEV; | 
|  | 453 | goto err; | 
|  | 454 | } | 
| Bjørn Mork | 8804420 | 2012-02-10 09:44:08 +0100 | [diff] [blame] | 455 | if (test_bit(WDM_RESETTING, &desc->flags)) { | 
|  | 456 | rv = -EIO; | 
|  | 457 | goto err; | 
|  | 458 | } | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 459 | usb_mark_last_busy(interface_to_usbdev(desc->intf)); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 460 | if (rv < 0) { | 
|  | 461 | rv = -ERESTARTSYS; | 
|  | 462 | goto err; | 
|  | 463 | } | 
|  | 464 |  | 
|  | 465 | spin_lock_irq(&desc->iuspin); | 
|  | 466 |  | 
|  | 467 | if (desc->rerr) { /* read completed, error happened */ | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 468 | desc->rerr = 0; | 
|  | 469 | spin_unlock_irq(&desc->iuspin); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 470 | rv = -EIO; | 
|  | 471 | goto err; | 
|  | 472 | } | 
|  | 473 | /* | 
|  | 474 | * recheck whether we've lost the race | 
|  | 475 | * against the completion handler | 
|  | 476 | */ | 
|  | 477 | if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */ | 
|  | 478 | spin_unlock_irq(&desc->iuspin); | 
|  | 479 | goto retry; | 
|  | 480 | } | 
|  | 481 | if (!desc->reslength) { /* zero length read */ | 
| Bjørn Mork | b086b6b | 2012-07-02 10:33:14 +0200 | [diff] [blame] | 482 | dev_dbg(&desc->intf->dev, "%s: zero length - clearing WDM_READ\n", __func__); | 
|  | 483 | clear_bit(WDM_READ, &desc->flags); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 484 | spin_unlock_irq(&desc->iuspin); | 
|  | 485 | goto retry; | 
|  | 486 | } | 
| Ben Hutchings | 711c68b | 2012-02-12 06:00:41 +0000 | [diff] [blame] | 487 | cntr = desc->length; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 488 | spin_unlock_irq(&desc->iuspin); | 
|  | 489 | } | 
|  | 490 |  | 
| Ben Hutchings | 711c68b | 2012-02-12 06:00:41 +0000 | [diff] [blame] | 491 | if (cntr > count) | 
|  | 492 | cntr = count; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 493 | rv = copy_to_user(buffer, desc->ubuf, cntr); | 
|  | 494 | if (rv > 0) { | 
|  | 495 | rv = -EFAULT; | 
|  | 496 | goto err; | 
|  | 497 | } | 
|  | 498 |  | 
| Ben Hutchings | 711c68b | 2012-02-12 06:00:41 +0000 | [diff] [blame] | 499 | spin_lock_irq(&desc->iuspin); | 
|  | 500 |  | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 501 | for (i = 0; i < desc->length - cntr; i++) | 
|  | 502 | desc->ubuf[i] = desc->ubuf[i + cntr]; | 
|  | 503 |  | 
|  | 504 | desc->length -= cntr; | 
| Oliver Neukum | 87d65e5 | 2008-06-19 14:20:18 +0200 | [diff] [blame] | 505 | /* in case we had outstanding data */ | 
|  | 506 | if (!desc->length) | 
|  | 507 | clear_bit(WDM_READ, &desc->flags); | 
| Ben Hutchings | 711c68b | 2012-02-12 06:00:41 +0000 | [diff] [blame] | 508 |  | 
|  | 509 | spin_unlock_irq(&desc->iuspin); | 
|  | 510 |  | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 511 | rv = cntr; | 
|  | 512 |  | 
|  | 513 | err: | 
| Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 514 | mutex_unlock(&desc->rlock); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 515 | return rv; | 
|  | 516 | } | 
|  | 517 |  | 
|  | 518 | static int wdm_flush(struct file *file, fl_owner_t id) | 
|  | 519 | { | 
|  | 520 | struct wdm_device *desc = file->private_data; | 
|  | 521 |  | 
|  | 522 | wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags)); | 
| Bjørn Mork | 6b0b79d | 2012-05-09 13:53:22 +0200 | [diff] [blame] | 523 |  | 
|  | 524 | /* cannot dereference desc->intf if WDM_DISCONNECTING */ | 
|  | 525 | if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags)) | 
| Greg Kroah-Hartman | 9908a32e9 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 526 | dev_err(&desc->intf->dev, "Error in flush path: %d\n", | 
|  | 527 | desc->werr); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 528 |  | 
| Oliver Neukum | 24a85ba | 2012-04-27 14:23:54 +0200 | [diff] [blame] | 529 | return usb_translate_errors(desc->werr); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 530 | } | 
|  | 531 |  | 
|  | 532 | static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait) | 
|  | 533 | { | 
|  | 534 | struct wdm_device *desc = file->private_data; | 
|  | 535 | unsigned long flags; | 
|  | 536 | unsigned int mask = 0; | 
|  | 537 |  | 
|  | 538 | spin_lock_irqsave(&desc->iuspin, flags); | 
|  | 539 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { | 
| Bjørn Mork | 616b693 | 2012-05-09 13:53:21 +0200 | [diff] [blame] | 540 | mask = POLLHUP | POLLERR; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 541 | spin_unlock_irqrestore(&desc->iuspin, flags); | 
|  | 542 | goto desc_out; | 
|  | 543 | } | 
|  | 544 | if (test_bit(WDM_READ, &desc->flags)) | 
|  | 545 | mask = POLLIN | POLLRDNORM; | 
|  | 546 | if (desc->rerr || desc->werr) | 
|  | 547 | mask |= POLLERR; | 
|  | 548 | if (!test_bit(WDM_IN_USE, &desc->flags)) | 
|  | 549 | mask |= POLLOUT | POLLWRNORM; | 
|  | 550 | spin_unlock_irqrestore(&desc->iuspin, flags); | 
|  | 551 |  | 
|  | 552 | poll_wait(file, &desc->wait, wait); | 
|  | 553 |  | 
|  | 554 | desc_out: | 
|  | 555 | return mask; | 
|  | 556 | } | 
|  | 557 |  | 
|  | 558 | static int wdm_open(struct inode *inode, struct file *file) | 
|  | 559 | { | 
|  | 560 | int minor = iminor(inode); | 
|  | 561 | int rv = -ENODEV; | 
|  | 562 | struct usb_interface *intf; | 
|  | 563 | struct wdm_device *desc; | 
|  | 564 |  | 
|  | 565 | mutex_lock(&wdm_mutex); | 
| Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 566 | desc = wdm_find_device_by_minor(minor); | 
|  | 567 | if (!desc) | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 568 | goto out; | 
|  | 569 |  | 
| Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 570 | intf = desc->intf; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 571 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) | 
|  | 572 | goto out; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 573 | file->private_data = desc; | 
|  | 574 |  | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 575 | rv = usb_autopm_get_interface(desc->intf); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 576 | if (rv < 0) { | 
| Greg Kroah-Hartman | 9908a32e9 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 577 | dev_err(&desc->intf->dev, "Error autopm - %d\n", rv); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 578 | goto out; | 
|  | 579 | } | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 580 |  | 
| Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 581 | /* using write lock to protect desc->count */ | 
|  | 582 | mutex_lock(&desc->wlock); | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 583 | if (!desc->count++) { | 
| Oliver Neukum | d771d8a | 2011-04-29 14:12:21 +0200 | [diff] [blame] | 584 | desc->werr = 0; | 
|  | 585 | desc->rerr = 0; | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 586 | rv = usb_submit_urb(desc->validity, GFP_KERNEL); | 
|  | 587 | if (rv < 0) { | 
|  | 588 | desc->count--; | 
| Greg Kroah-Hartman | 9908a32e9 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 589 | dev_err(&desc->intf->dev, | 
|  | 590 | "Error submitting int urb - %d\n", rv); | 
| Oliver Neukum | 12a98b2 | 2012-04-30 09:57:31 +0200 | [diff] [blame] | 591 | rv = usb_translate_errors(rv); | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 592 | } | 
|  | 593 | } else { | 
|  | 594 | rv = 0; | 
|  | 595 | } | 
| Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 596 | mutex_unlock(&desc->wlock); | 
| Bjørn Mork | 3cc3615 | 2012-03-06 17:29:22 +0100 | [diff] [blame] | 597 | if (desc->count == 1) | 
|  | 598 | desc->manage_power(intf, 1); | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 599 | usb_autopm_put_interface(desc->intf); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 600 | out: | 
|  | 601 | mutex_unlock(&wdm_mutex); | 
|  | 602 | return rv; | 
|  | 603 | } | 
|  | 604 |  | 
|  | 605 | static int wdm_release(struct inode *inode, struct file *file) | 
|  | 606 | { | 
|  | 607 | struct wdm_device *desc = file->private_data; | 
|  | 608 |  | 
|  | 609 | mutex_lock(&wdm_mutex); | 
| Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 610 |  | 
|  | 611 | /* using write lock to protect desc->count */ | 
|  | 612 | mutex_lock(&desc->wlock); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 613 | desc->count--; | 
| Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 614 | mutex_unlock(&desc->wlock); | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 615 |  | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 616 | if (!desc->count) { | 
| Bjørn Mork | 880bca3 | 2012-04-30 09:26:11 +0200 | [diff] [blame] | 617 | if (!test_bit(WDM_DISCONNECTING, &desc->flags)) { | 
| Bjørn Mork | 6b0b79d | 2012-05-09 13:53:22 +0200 | [diff] [blame] | 618 | dev_dbg(&desc->intf->dev, "wdm_release: cleanup"); | 
|  | 619 | kill_urbs(desc); | 
| Bjørn Mork | 3cc3615 | 2012-03-06 17:29:22 +0100 | [diff] [blame] | 620 | desc->manage_power(desc->intf, 0); | 
| Bjørn Mork | 880bca3 | 2012-04-30 09:26:11 +0200 | [diff] [blame] | 621 | } else { | 
| Bjørn Mork | 6b0b79d | 2012-05-09 13:53:22 +0200 | [diff] [blame] | 622 | /* must avoid dev_printk here as desc->intf is invalid */ | 
|  | 623 | pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__); | 
| Oliver Neukum | 2f338c8 | 2012-04-27 14:36:37 +0200 | [diff] [blame] | 624 | cleanup(desc); | 
| Bjørn Mork | 880bca3 | 2012-04-30 09:26:11 +0200 | [diff] [blame] | 625 | } | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 626 | } | 
|  | 627 | mutex_unlock(&wdm_mutex); | 
|  | 628 | return 0; | 
|  | 629 | } | 
|  | 630 |  | 
|  | 631 | static const struct file_operations wdm_fops = { | 
|  | 632 | .owner =	THIS_MODULE, | 
|  | 633 | .read =		wdm_read, | 
|  | 634 | .write =	wdm_write, | 
|  | 635 | .open =		wdm_open, | 
|  | 636 | .flush =	wdm_flush, | 
|  | 637 | .release =	wdm_release, | 
| Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 638 | .poll =		wdm_poll, | 
|  | 639 | .llseek =	noop_llseek, | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 640 | }; | 
|  | 641 |  | 
|  | 642 | static struct usb_class_driver wdm_class = { | 
|  | 643 | .name =		"cdc-wdm%d", | 
|  | 644 | .fops =		&wdm_fops, | 
|  | 645 | .minor_base =	WDM_MINOR_BASE, | 
|  | 646 | }; | 
|  | 647 |  | 
|  | 648 | /* --- error handling --- */ | 
|  | 649 | static void wdm_rxwork(struct work_struct *work) | 
|  | 650 | { | 
|  | 651 | struct wdm_device *desc = container_of(work, struct wdm_device, rxwork); | 
|  | 652 | unsigned long flags; | 
|  | 653 | int rv; | 
|  | 654 |  | 
|  | 655 | spin_lock_irqsave(&desc->iuspin, flags); | 
|  | 656 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { | 
|  | 657 | spin_unlock_irqrestore(&desc->iuspin, flags); | 
|  | 658 | } else { | 
|  | 659 | spin_unlock_irqrestore(&desc->iuspin, flags); | 
|  | 660 | rv = usb_submit_urb(desc->response, GFP_KERNEL); | 
|  | 661 | if (rv < 0 && rv != -EPERM) { | 
|  | 662 | spin_lock_irqsave(&desc->iuspin, flags); | 
|  | 663 | if (!test_bit(WDM_DISCONNECTING, &desc->flags)) | 
|  | 664 | schedule_work(&desc->rxwork); | 
|  | 665 | spin_unlock_irqrestore(&desc->iuspin, flags); | 
|  | 666 | } | 
|  | 667 | } | 
|  | 668 | } | 
|  | 669 |  | 
|  | 670 | /* --- hotplug --- */ | 
|  | 671 |  | 
| Bjørn Mork | 3cc3615 | 2012-03-06 17:29:22 +0100 | [diff] [blame] | 672 | static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep, | 
|  | 673 | u16 bufsize, int (*manage_power)(struct usb_interface *, int)) | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 674 | { | 
| Bjørn Mork | 0dffb48 | 2012-03-06 17:29:20 +0100 | [diff] [blame] | 675 | int rv = -ENOMEM; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 676 | struct wdm_device *desc; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 677 |  | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 678 | desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL); | 
|  | 679 | if (!desc) | 
|  | 680 | goto out; | 
| Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 681 | INIT_LIST_HEAD(&desc->device_list); | 
| Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 682 | mutex_init(&desc->rlock); | 
|  | 683 | mutex_init(&desc->wlock); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 684 | spin_lock_init(&desc->iuspin); | 
|  | 685 | init_waitqueue_head(&desc->wait); | 
| Bjørn Mork | 0dffb48 | 2012-03-06 17:29:20 +0100 | [diff] [blame] | 686 | desc->wMaxCommand = bufsize; | 
| Oliver Neukum | 052fbc0 | 2009-04-20 17:24:49 +0200 | [diff] [blame] | 687 | /* this will be expanded and needed in hardware endianness */ | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 688 | desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber); | 
|  | 689 | desc->intf = intf; | 
|  | 690 | INIT_WORK(&desc->rxwork, wdm_rxwork); | 
|  | 691 |  | 
| Oliver Neukum | 052fbc0 | 2009-04-20 17:24:49 +0200 | [diff] [blame] | 692 | rv = -EINVAL; | 
| Bjørn Mork | 0dffb48 | 2012-03-06 17:29:20 +0100 | [diff] [blame] | 693 | if (!usb_endpoint_is_int_in(ep)) | 
| Oliver Neukum | 052fbc0 | 2009-04-20 17:24:49 +0200 | [diff] [blame] | 694 | goto err; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 695 |  | 
| Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 696 | desc->wMaxPacketSize = usb_endpoint_maxp(ep); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 697 |  | 
|  | 698 | desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); | 
|  | 699 | if (!desc->orq) | 
|  | 700 | goto err; | 
|  | 701 | desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); | 
|  | 702 | if (!desc->irq) | 
|  | 703 | goto err; | 
|  | 704 |  | 
|  | 705 | desc->validity = usb_alloc_urb(0, GFP_KERNEL); | 
|  | 706 | if (!desc->validity) | 
|  | 707 | goto err; | 
|  | 708 |  | 
|  | 709 | desc->response = usb_alloc_urb(0, GFP_KERNEL); | 
|  | 710 | if (!desc->response) | 
|  | 711 | goto err; | 
|  | 712 |  | 
|  | 713 | desc->command = usb_alloc_urb(0, GFP_KERNEL); | 
|  | 714 | if (!desc->command) | 
|  | 715 | goto err; | 
|  | 716 |  | 
|  | 717 | desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL); | 
|  | 718 | if (!desc->ubuf) | 
|  | 719 | goto err; | 
|  | 720 |  | 
| Bjørn Mork | 8457d99 | 2012-01-16 15:12:00 +0100 | [diff] [blame] | 721 | desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 722 | if (!desc->sbuf) | 
|  | 723 | goto err; | 
|  | 724 |  | 
| Bjørn Mork | 8457d99 | 2012-01-16 15:12:00 +0100 | [diff] [blame] | 725 | desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 726 | if (!desc->inbuf) | 
| Bjørn Mork | 8457d99 | 2012-01-16 15:12:00 +0100 | [diff] [blame] | 727 | goto err; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 728 |  | 
|  | 729 | usb_fill_int_urb( | 
|  | 730 | desc->validity, | 
|  | 731 | interface_to_usbdev(intf), | 
|  | 732 | usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress), | 
|  | 733 | desc->sbuf, | 
|  | 734 | desc->wMaxPacketSize, | 
|  | 735 | wdm_int_callback, | 
|  | 736 | desc, | 
|  | 737 | ep->bInterval | 
|  | 738 | ); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 739 |  | 
| Bjørn Mork | 19b85b3 | 2012-01-16 15:11:58 +0100 | [diff] [blame] | 740 | desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE); | 
|  | 741 | desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE; | 
|  | 742 | desc->irq->wValue = 0; | 
|  | 743 | desc->irq->wIndex = desc->inum; | 
|  | 744 | desc->irq->wLength = cpu_to_le16(desc->wMaxCommand); | 
|  | 745 |  | 
|  | 746 | usb_fill_control_urb( | 
|  | 747 | desc->response, | 
| Bjørn Mork | 8143a89 | 2012-01-16 15:12:01 +0100 | [diff] [blame] | 748 | interface_to_usbdev(intf), | 
| Bjørn Mork | 19b85b3 | 2012-01-16 15:11:58 +0100 | [diff] [blame] | 749 | /* using common endpoint 0 */ | 
|  | 750 | usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0), | 
|  | 751 | (unsigned char *)desc->irq, | 
|  | 752 | desc->inbuf, | 
|  | 753 | desc->wMaxCommand, | 
|  | 754 | wdm_in_callback, | 
|  | 755 | desc | 
|  | 756 | ); | 
| Bjørn Mork | 19b85b3 | 2012-01-16 15:11:58 +0100 | [diff] [blame] | 757 |  | 
| Bjørn Mork | 3cc3615 | 2012-03-06 17:29:22 +0100 | [diff] [blame] | 758 | desc->manage_power = manage_power; | 
|  | 759 |  | 
| Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 760 | spin_lock(&wdm_device_list_lock); | 
|  | 761 | list_add(&desc->device_list, &wdm_device_list); | 
|  | 762 | spin_unlock(&wdm_device_list_lock); | 
|  | 763 |  | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 764 | rv = usb_register_dev(intf, &wdm_class); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 765 | if (rv < 0) | 
| Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 766 | goto err; | 
| Oliver Neukum | 052fbc0 | 2009-04-20 17:24:49 +0200 | [diff] [blame] | 767 | else | 
| Bjørn Mork | 820c629 | 2012-01-20 04:17:25 +0100 | [diff] [blame] | 768 | dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev)); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 769 | out: | 
|  | 770 | return rv; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 771 | err: | 
| Bjørn Mork | 6286d85 | 2012-05-09 13:53:23 +0200 | [diff] [blame] | 772 | spin_lock(&wdm_device_list_lock); | 
|  | 773 | list_del(&desc->device_list); | 
|  | 774 | spin_unlock(&wdm_device_list_lock); | 
| Bjørn Mork | 0dffb48 | 2012-03-06 17:29:20 +0100 | [diff] [blame] | 775 | cleanup(desc); | 
|  | 776 | return rv; | 
|  | 777 | } | 
|  | 778 |  | 
| Bjørn Mork | 3cc3615 | 2012-03-06 17:29:22 +0100 | [diff] [blame] | 779 | static int wdm_manage_power(struct usb_interface *intf, int on) | 
|  | 780 | { | 
|  | 781 | /* need autopm_get/put here to ensure the usbcore sees the new value */ | 
|  | 782 | int rv = usb_autopm_get_interface(intf); | 
|  | 783 | if (rv < 0) | 
|  | 784 | goto err; | 
|  | 785 |  | 
|  | 786 | intf->needs_remote_wakeup = on; | 
|  | 787 | usb_autopm_put_interface(intf); | 
|  | 788 | err: | 
|  | 789 | return rv; | 
|  | 790 | } | 
|  | 791 |  | 
| Bjørn Mork | 0dffb48 | 2012-03-06 17:29:20 +0100 | [diff] [blame] | 792 | static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id) | 
|  | 793 | { | 
|  | 794 | int rv = -EINVAL; | 
|  | 795 | struct usb_host_interface *iface; | 
|  | 796 | struct usb_endpoint_descriptor *ep; | 
|  | 797 | struct usb_cdc_dmm_desc *dmhd; | 
|  | 798 | u8 *buffer = intf->altsetting->extra; | 
|  | 799 | int buflen = intf->altsetting->extralen; | 
|  | 800 | u16 maxcom = WDM_DEFAULT_BUFSIZE; | 
|  | 801 |  | 
|  | 802 | if (!buffer) | 
|  | 803 | goto err; | 
|  | 804 | while (buflen > 2) { | 
|  | 805 | if (buffer[1] != USB_DT_CS_INTERFACE) { | 
|  | 806 | dev_err(&intf->dev, "skipping garbage\n"); | 
|  | 807 | goto next_desc; | 
|  | 808 | } | 
|  | 809 |  | 
|  | 810 | switch (buffer[2]) { | 
|  | 811 | case USB_CDC_HEADER_TYPE: | 
|  | 812 | break; | 
|  | 813 | case USB_CDC_DMM_TYPE: | 
|  | 814 | dmhd = (struct usb_cdc_dmm_desc *)buffer; | 
|  | 815 | maxcom = le16_to_cpu(dmhd->wMaxCommand); | 
|  | 816 | dev_dbg(&intf->dev, | 
|  | 817 | "Finding maximum buffer length: %d", maxcom); | 
|  | 818 | break; | 
|  | 819 | default: | 
|  | 820 | dev_err(&intf->dev, | 
|  | 821 | "Ignoring extra header, type %d, length %d\n", | 
|  | 822 | buffer[2], buffer[0]); | 
|  | 823 | break; | 
|  | 824 | } | 
|  | 825 | next_desc: | 
|  | 826 | buflen -= buffer[0]; | 
|  | 827 | buffer += buffer[0]; | 
|  | 828 | } | 
|  | 829 |  | 
|  | 830 | iface = intf->cur_altsetting; | 
|  | 831 | if (iface->desc.bNumEndpoints != 1) | 
|  | 832 | goto err; | 
|  | 833 | ep = &iface->endpoint[0].desc; | 
|  | 834 |  | 
| Bjørn Mork | 3cc3615 | 2012-03-06 17:29:22 +0100 | [diff] [blame] | 835 | rv = wdm_create(intf, ep, maxcom, &wdm_manage_power); | 
| Bjørn Mork | 0dffb48 | 2012-03-06 17:29:20 +0100 | [diff] [blame] | 836 |  | 
|  | 837 | err: | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 838 | return rv; | 
|  | 839 | } | 
|  | 840 |  | 
| Bjørn Mork | 3cc3615 | 2012-03-06 17:29:22 +0100 | [diff] [blame] | 841 | /** | 
|  | 842 | * usb_cdc_wdm_register - register a WDM subdriver | 
|  | 843 | * @intf: usb interface the subdriver will associate with | 
|  | 844 | * @ep: interrupt endpoint to monitor for notifications | 
|  | 845 | * @bufsize: maximum message size to support for read/write | 
|  | 846 | * | 
|  | 847 | * Create WDM usb class character device and associate it with intf | 
|  | 848 | * without binding, allowing another driver to manage the interface. | 
|  | 849 | * | 
|  | 850 | * The subdriver will manage the given interrupt endpoint exclusively | 
|  | 851 | * and will issue control requests referring to the given intf. It | 
|  | 852 | * will otherwise avoid interferring, and in particular not do | 
|  | 853 | * usb_set_intfdata/usb_get_intfdata on intf. | 
|  | 854 | * | 
|  | 855 | * The return value is a pointer to the subdriver's struct usb_driver. | 
|  | 856 | * The registering driver is responsible for calling this subdriver's | 
|  | 857 | * disconnect, suspend, resume, pre_reset and post_reset methods from | 
|  | 858 | * its own. | 
|  | 859 | */ | 
|  | 860 | struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf, | 
|  | 861 | struct usb_endpoint_descriptor *ep, | 
|  | 862 | int bufsize, | 
|  | 863 | int (*manage_power)(struct usb_interface *, int)) | 
|  | 864 | { | 
|  | 865 | int rv = -EINVAL; | 
|  | 866 |  | 
|  | 867 | rv = wdm_create(intf, ep, bufsize, manage_power); | 
|  | 868 | if (rv < 0) | 
|  | 869 | goto err; | 
|  | 870 |  | 
|  | 871 | return &wdm_driver; | 
|  | 872 | err: | 
|  | 873 | return ERR_PTR(rv); | 
|  | 874 | } | 
|  | 875 | EXPORT_SYMBOL(usb_cdc_wdm_register); | 
|  | 876 |  | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 877 | static void wdm_disconnect(struct usb_interface *intf) | 
|  | 878 | { | 
|  | 879 | struct wdm_device *desc; | 
|  | 880 | unsigned long flags; | 
|  | 881 |  | 
|  | 882 | usb_deregister_dev(intf, &wdm_class); | 
| Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 883 | desc = wdm_find_device(intf); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 884 | mutex_lock(&wdm_mutex); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 885 |  | 
|  | 886 | /* the spinlock makes sure no new urbs are generated in the callbacks */ | 
|  | 887 | spin_lock_irqsave(&desc->iuspin, flags); | 
|  | 888 | set_bit(WDM_DISCONNECTING, &desc->flags); | 
|  | 889 | set_bit(WDM_READ, &desc->flags); | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 890 | /* to terminate pending flushes */ | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 891 | clear_bit(WDM_IN_USE, &desc->flags); | 
|  | 892 | spin_unlock_irqrestore(&desc->iuspin, flags); | 
| Bjørn Mork | 62aaf24 | 2012-01-16 15:11:57 +0100 | [diff] [blame] | 893 | wake_up_all(&desc->wait); | 
| Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 894 | mutex_lock(&desc->rlock); | 
|  | 895 | mutex_lock(&desc->wlock); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 896 | kill_urbs(desc); | 
| Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 897 | cancel_work_sync(&desc->rxwork); | 
| Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 898 | mutex_unlock(&desc->wlock); | 
|  | 899 | mutex_unlock(&desc->rlock); | 
| Bjørn Mork | 6286d85 | 2012-05-09 13:53:23 +0200 | [diff] [blame] | 900 |  | 
|  | 901 | /* the desc->intf pointer used as list key is now invalid */ | 
|  | 902 | spin_lock(&wdm_device_list_lock); | 
|  | 903 | list_del(&desc->device_list); | 
|  | 904 | spin_unlock(&wdm_device_list_lock); | 
|  | 905 |  | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 906 | if (!desc->count) | 
|  | 907 | cleanup(desc); | 
| Bjørn Mork | 880bca3 | 2012-04-30 09:26:11 +0200 | [diff] [blame] | 908 | else | 
|  | 909 | dev_dbg(&intf->dev, "%s: %d open files - postponing cleanup\n", __func__, desc->count); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 910 | mutex_unlock(&wdm_mutex); | 
|  | 911 | } | 
|  | 912 |  | 
| Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 913 | #ifdef CONFIG_PM | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 914 | static int wdm_suspend(struct usb_interface *intf, pm_message_t message) | 
|  | 915 | { | 
| Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 916 | struct wdm_device *desc = wdm_find_device(intf); | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 917 | int rv = 0; | 
|  | 918 |  | 
|  | 919 | dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor); | 
|  | 920 |  | 
| Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 921 | /* if this is an autosuspend the caller does the locking */ | 
| Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 922 | if (!PMSG_IS_AUTO(message)) { | 
|  | 923 | mutex_lock(&desc->rlock); | 
|  | 924 | mutex_lock(&desc->wlock); | 
|  | 925 | } | 
| Oliver Neukum | 62e6685 | 2010-02-27 20:56:22 +0100 | [diff] [blame] | 926 | spin_lock_irq(&desc->iuspin); | 
| Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 927 |  | 
| Alan Stern | 5b1b0b8 | 2011-08-19 23:49:48 +0200 | [diff] [blame] | 928 | if (PMSG_IS_AUTO(message) && | 
| Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 929 | (test_bit(WDM_IN_USE, &desc->flags) | 
|  | 930 | || test_bit(WDM_RESPONDING, &desc->flags))) { | 
| Oliver Neukum | 62e6685 | 2010-02-27 20:56:22 +0100 | [diff] [blame] | 931 | spin_unlock_irq(&desc->iuspin); | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 932 | rv = -EBUSY; | 
|  | 933 | } else { | 
| Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 934 |  | 
| Oliver Neukum | beb1d35 | 2010-02-27 20:55:52 +0100 | [diff] [blame] | 935 | set_bit(WDM_SUSPENDING, &desc->flags); | 
| Oliver Neukum | 62e6685 | 2010-02-27 20:56:22 +0100 | [diff] [blame] | 936 | spin_unlock_irq(&desc->iuspin); | 
| Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 937 | /* callback submits work - order is essential */ | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 938 | kill_urbs(desc); | 
| Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 939 | cancel_work_sync(&desc->rxwork); | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 940 | } | 
| Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 941 | if (!PMSG_IS_AUTO(message)) { | 
|  | 942 | mutex_unlock(&desc->wlock); | 
|  | 943 | mutex_unlock(&desc->rlock); | 
|  | 944 | } | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 945 |  | 
|  | 946 | return rv; | 
|  | 947 | } | 
| Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 948 | #endif | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 949 |  | 
|  | 950 | static int recover_from_urb_loss(struct wdm_device *desc) | 
|  | 951 | { | 
|  | 952 | int rv = 0; | 
|  | 953 |  | 
|  | 954 | if (desc->count) { | 
|  | 955 | rv = usb_submit_urb(desc->validity, GFP_NOIO); | 
|  | 956 | if (rv < 0) | 
| Greg Kroah-Hartman | 9908a32e9 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 957 | dev_err(&desc->intf->dev, | 
|  | 958 | "Error resume submitting int urb - %d\n", rv); | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 959 | } | 
|  | 960 | return rv; | 
|  | 961 | } | 
| Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 962 |  | 
|  | 963 | #ifdef CONFIG_PM | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 964 | static int wdm_resume(struct usb_interface *intf) | 
|  | 965 | { | 
| Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 966 | struct wdm_device *desc = wdm_find_device(intf); | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 967 | int rv; | 
|  | 968 |  | 
|  | 969 | dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor); | 
| Oliver Neukum | 338124c | 2010-02-27 20:57:12 +0100 | [diff] [blame] | 970 |  | 
| Oliver Neukum | beb1d35 | 2010-02-27 20:55:52 +0100 | [diff] [blame] | 971 | clear_bit(WDM_SUSPENDING, &desc->flags); | 
| Oliver Neukum | 62e6685 | 2010-02-27 20:56:22 +0100 | [diff] [blame] | 972 | rv = recover_from_urb_loss(desc); | 
| Oliver Neukum | 338124c | 2010-02-27 20:57:12 +0100 | [diff] [blame] | 973 |  | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 974 | return rv; | 
|  | 975 | } | 
| Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 976 | #endif | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 977 |  | 
|  | 978 | static int wdm_pre_reset(struct usb_interface *intf) | 
|  | 979 | { | 
| Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 980 | struct wdm_device *desc = wdm_find_device(intf); | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 981 |  | 
| Oliver Neukum | d771d8a | 2011-04-29 14:12:21 +0200 | [diff] [blame] | 982 | /* | 
|  | 983 | * we notify everybody using poll of | 
|  | 984 | * an exceptional situation | 
|  | 985 | * must be done before recovery lest a spontaneous | 
|  | 986 | * message from the device is lost | 
|  | 987 | */ | 
|  | 988 | spin_lock_irq(&desc->iuspin); | 
| Bjørn Mork | 8804420 | 2012-02-10 09:44:08 +0100 | [diff] [blame] | 989 | set_bit(WDM_RESETTING, &desc->flags);	/* inform read/write */ | 
|  | 990 | set_bit(WDM_READ, &desc->flags);	/* unblock read */ | 
|  | 991 | clear_bit(WDM_IN_USE, &desc->flags);	/* unblock write */ | 
| Oliver Neukum | d771d8a | 2011-04-29 14:12:21 +0200 | [diff] [blame] | 992 | desc->rerr = -EINTR; | 
|  | 993 | spin_unlock_irq(&desc->iuspin); | 
|  | 994 | wake_up_all(&desc->wait); | 
| Bjørn Mork | 8804420 | 2012-02-10 09:44:08 +0100 | [diff] [blame] | 995 | mutex_lock(&desc->rlock); | 
|  | 996 | mutex_lock(&desc->wlock); | 
|  | 997 | kill_urbs(desc); | 
|  | 998 | cancel_work_sync(&desc->rxwork); | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 999 | return 0; | 
|  | 1000 | } | 
|  | 1001 |  | 
|  | 1002 | static int wdm_post_reset(struct usb_interface *intf) | 
|  | 1003 | { | 
| Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 1004 | struct wdm_device *desc = wdm_find_device(intf); | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1005 | int rv; | 
|  | 1006 |  | 
| Bjørn Mork | 8804420 | 2012-02-10 09:44:08 +0100 | [diff] [blame] | 1007 | clear_bit(WDM_RESETTING, &desc->flags); | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1008 | rv = recover_from_urb_loss(desc); | 
| Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 1009 | mutex_unlock(&desc->wlock); | 
|  | 1010 | mutex_unlock(&desc->rlock); | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1011 | return 0; | 
|  | 1012 | } | 
|  | 1013 |  | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 1014 | static struct usb_driver wdm_driver = { | 
|  | 1015 | .name =		"cdc_wdm", | 
|  | 1016 | .probe =	wdm_probe, | 
|  | 1017 | .disconnect =	wdm_disconnect, | 
| Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 1018 | #ifdef CONFIG_PM | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1019 | .suspend =	wdm_suspend, | 
|  | 1020 | .resume =	wdm_resume, | 
|  | 1021 | .reset_resume =	wdm_resume, | 
| Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 1022 | #endif | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1023 | .pre_reset =	wdm_pre_reset, | 
|  | 1024 | .post_reset =	wdm_post_reset, | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 1025 | .id_table =	wdm_ids, | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1026 | .supports_autosuspend = 1, | 
| Sarah Sharp | e1f12eb | 2012-04-23 10:08:51 -0700 | [diff] [blame] | 1027 | .disable_hub_initiated_lpm = 1, | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 1028 | }; | 
|  | 1029 |  | 
| Greg Kroah-Hartman | 65db430 | 2011-11-18 09:34:02 -0800 | [diff] [blame] | 1030 | module_usb_driver(wdm_driver); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 1031 |  | 
|  | 1032 | MODULE_AUTHOR(DRIVER_AUTHOR); | 
| Oliver Neukum | 87d65e5 | 2008-06-19 14:20:18 +0200 | [diff] [blame] | 1033 | MODULE_DESCRIPTION(DRIVER_DESC); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 1034 | MODULE_LICENSE("GPL"); |