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