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