Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 1 | /* |
| 2 | * cdc-wdm.c |
| 3 | * |
| 4 | * This driver supports USB CDC WCM Device Management. |
| 5 | * |
Oliver Neukum | 052fbc0 | 2009-04-20 17:24:49 +0200 | [diff] [blame] | 6 | * Copyright (c) 2007-2009 Oliver Neukum |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 7 | * |
| 8 | * Some code taken from cdc-acm.c |
| 9 | * |
| 10 | * Released under the GPLv2. |
| 11 | * |
| 12 | * Many thanks to Carl Nordbeck |
| 13 | */ |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/module.h> |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 18 | #include <linux/mutex.h> |
| 19 | #include <linux/uaccess.h> |
| 20 | #include <linux/bitops.h> |
| 21 | #include <linux/poll.h> |
| 22 | #include <linux/usb.h> |
| 23 | #include <linux/usb/cdc.h> |
| 24 | #include <asm/byteorder.h> |
| 25 | #include <asm/unaligned.h> |
Bjørn Mork | 3cc3615 | 2012-03-06 17:29:22 +0100 | [diff] [blame] | 26 | #include <linux/usb/cdc-wdm.h> |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * Version Information |
| 30 | */ |
Oliver Neukum | 87d65e5 | 2008-06-19 14:20:18 +0200 | [diff] [blame] | 31 | #define DRIVER_VERSION "v0.03" |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 32 | #define DRIVER_AUTHOR "Oliver Neukum" |
Oliver Neukum | 87d65e5 | 2008-06-19 14:20:18 +0200 | [diff] [blame] | 33 | #define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management" |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 34 | |
Bjørn Mork | fec67b4 | 2012-01-25 13:03:29 +0100 | [diff] [blame] | 35 | #define HUAWEI_VENDOR_ID 0x12D1 |
| 36 | |
Németh Márton | 6ef4852 | 2010-01-10 15:33:45 +0100 | [diff] [blame] | 37 | static const struct usb_device_id wdm_ids[] = { |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 38 | { |
| 39 | .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS | |
| 40 | USB_DEVICE_ID_MATCH_INT_SUBCLASS, |
| 41 | .bInterfaceClass = USB_CLASS_COMM, |
| 42 | .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM |
| 43 | }, |
Bjørn Mork | fec67b4 | 2012-01-25 13:03:29 +0100 | [diff] [blame] | 44 | { |
| 45 | /* |
| 46 | * Huawei E392, E398 and possibly other Qualcomm based modems |
| 47 | * embed the Qualcomm QMI protocol inside CDC on CDC ECM like |
| 48 | * control interfaces. Userspace access to this is required |
| 49 | * to configure the accompanying data interface |
| 50 | */ |
| 51 | .match_flags = USB_DEVICE_ID_MATCH_VENDOR | |
| 52 | USB_DEVICE_ID_MATCH_INT_INFO, |
| 53 | .idVendor = HUAWEI_VENDOR_ID, |
| 54 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, |
| 55 | .bInterfaceSubClass = 1, |
| 56 | .bInterfaceProtocol = 9, /* NOTE: CDC ECM control interface! */ |
| 57 | }, |
Bjørn Mork | 9b81405 | 2012-05-19 19:19:48 +0200 | [diff] [blame] | 58 | { |
| 59 | /* Vodafone/Huawei K5005 (12d1:14c8) and similar modems */ |
| 60 | .match_flags = USB_DEVICE_ID_MATCH_VENDOR | |
| 61 | USB_DEVICE_ID_MATCH_INT_INFO, |
| 62 | .idVendor = HUAWEI_VENDOR_ID, |
| 63 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, |
| 64 | .bInterfaceSubClass = 1, |
| 65 | .bInterfaceProtocol = 57, /* NOTE: CDC ECM control interface! */ |
| 66 | }, |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 67 | { } |
| 68 | }; |
| 69 | |
Oliver Neukum | aa5380b | 2008-10-13 14:05:20 +0200 | [diff] [blame] | 70 | MODULE_DEVICE_TABLE (usb, wdm_ids); |
| 71 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 72 | #define WDM_MINOR_BASE 176 |
| 73 | |
| 74 | |
| 75 | #define WDM_IN_USE 1 |
| 76 | #define WDM_DISCONNECTING 2 |
| 77 | #define WDM_RESULT 3 |
| 78 | #define WDM_READ 4 |
| 79 | #define WDM_INT_STALL 5 |
| 80 | #define WDM_POLL_RUNNING 6 |
Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 81 | #define WDM_RESPONDING 7 |
Oliver Neukum | beb1d35 | 2010-02-27 20:55:52 +0100 | [diff] [blame] | 82 | #define WDM_SUSPENDING 8 |
Bjørn Mork | 8804420 | 2012-02-10 09:44:08 +0100 | [diff] [blame] | 83 | #define WDM_RESETTING 9 |
Oliver Neukum | 5c6142c | 2013-03-12 14:52:42 +0100 | [diff] [blame] | 84 | #define WDM_OVERFLOW 10 |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 85 | |
| 86 | #define WDM_MAX 16 |
| 87 | |
Bjørn Mork | 7e3054a | 2012-01-20 01:49:57 +0100 | [diff] [blame] | 88 | /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */ |
| 89 | #define WDM_DEFAULT_BUFSIZE 256 |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 90 | |
| 91 | static DEFINE_MUTEX(wdm_mutex); |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 92 | static DEFINE_SPINLOCK(wdm_device_list_lock); |
| 93 | static LIST_HEAD(wdm_device_list); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 94 | |
| 95 | /* --- method tables --- */ |
| 96 | |
| 97 | struct wdm_device { |
| 98 | u8 *inbuf; /* buffer for response */ |
| 99 | u8 *outbuf; /* buffer for command */ |
| 100 | u8 *sbuf; /* buffer for status */ |
| 101 | u8 *ubuf; /* buffer for copy to user space */ |
| 102 | |
| 103 | struct urb *command; |
| 104 | struct urb *response; |
| 105 | struct urb *validity; |
| 106 | struct usb_interface *intf; |
| 107 | struct usb_ctrlrequest *orq; |
| 108 | struct usb_ctrlrequest *irq; |
| 109 | spinlock_t iuspin; |
| 110 | |
| 111 | unsigned long flags; |
| 112 | u16 bufsize; |
| 113 | u16 wMaxCommand; |
| 114 | u16 wMaxPacketSize; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 115 | __le16 inum; |
| 116 | int reslength; |
| 117 | int length; |
| 118 | int read; |
| 119 | int count; |
| 120 | dma_addr_t shandle; |
| 121 | dma_addr_t ihandle; |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 122 | struct mutex wlock; |
| 123 | struct mutex rlock; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 124 | wait_queue_head_t wait; |
| 125 | struct work_struct rxwork; |
| 126 | int werr; |
| 127 | int rerr; |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 128 | |
| 129 | struct list_head device_list; |
Bjørn Mork | 3cc3615 | 2012-03-06 17:29:22 +0100 | [diff] [blame] | 130 | int (*manage_power)(struct usb_interface *, int); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | static struct usb_driver wdm_driver; |
| 134 | |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 135 | /* return intfdata if we own the interface, else look up intf in the list */ |
| 136 | static struct wdm_device *wdm_find_device(struct usb_interface *intf) |
| 137 | { |
Bjørn Mork | 5527f13 | 2012-09-10 22:17:34 +0200 | [diff] [blame] | 138 | struct wdm_device *desc; |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 139 | |
| 140 | spin_lock(&wdm_device_list_lock); |
| 141 | list_for_each_entry(desc, &wdm_device_list, device_list) |
| 142 | if (desc->intf == intf) |
Bjørn Mork | 5527f13 | 2012-09-10 22:17:34 +0200 | [diff] [blame] | 143 | goto found; |
| 144 | desc = NULL; |
| 145 | found: |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 146 | spin_unlock(&wdm_device_list_lock); |
| 147 | |
| 148 | return desc; |
| 149 | } |
| 150 | |
| 151 | static struct wdm_device *wdm_find_device_by_minor(int minor) |
| 152 | { |
Bjørn Mork | 5527f13 | 2012-09-10 22:17:34 +0200 | [diff] [blame] | 153 | struct wdm_device *desc; |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 154 | |
| 155 | spin_lock(&wdm_device_list_lock); |
| 156 | list_for_each_entry(desc, &wdm_device_list, device_list) |
| 157 | if (desc->intf->minor == minor) |
Bjørn Mork | 5527f13 | 2012-09-10 22:17:34 +0200 | [diff] [blame] | 158 | goto found; |
| 159 | desc = NULL; |
| 160 | found: |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 161 | spin_unlock(&wdm_device_list_lock); |
| 162 | |
| 163 | return desc; |
| 164 | } |
| 165 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 166 | /* --- callbacks --- */ |
| 167 | static void wdm_out_callback(struct urb *urb) |
| 168 | { |
| 169 | struct wdm_device *desc; |
| 170 | desc = urb->context; |
| 171 | spin_lock(&desc->iuspin); |
| 172 | desc->werr = urb->status; |
| 173 | spin_unlock(&desc->iuspin); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 174 | kfree(desc->outbuf); |
Oliver Neukum | 5c22837 | 2012-04-26 21:59:10 +0200 | [diff] [blame] | 175 | desc->outbuf = NULL; |
| 176 | clear_bit(WDM_IN_USE, &desc->flags); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 177 | wake_up(&desc->wait); |
| 178 | } |
| 179 | |
| 180 | static void wdm_in_callback(struct urb *urb) |
| 181 | { |
| 182 | struct wdm_device *desc = urb->context; |
| 183 | int status = urb->status; |
Oliver Neukum | 5c6142c | 2013-03-12 14:52:42 +0100 | [diff] [blame] | 184 | int length = urb->actual_length; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 185 | |
| 186 | spin_lock(&desc->iuspin); |
Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 187 | clear_bit(WDM_RESPONDING, &desc->flags); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 188 | |
| 189 | if (status) { |
| 190 | switch (status) { |
| 191 | case -ENOENT: |
| 192 | dev_dbg(&desc->intf->dev, |
| 193 | "nonzero urb status received: -ENOENT"); |
Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 194 | goto skip_error; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 195 | case -ECONNRESET: |
| 196 | dev_dbg(&desc->intf->dev, |
| 197 | "nonzero urb status received: -ECONNRESET"); |
Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 198 | goto skip_error; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 199 | case -ESHUTDOWN: |
| 200 | dev_dbg(&desc->intf->dev, |
| 201 | "nonzero urb status received: -ESHUTDOWN"); |
Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 202 | goto skip_error; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 203 | case -EPIPE: |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 204 | dev_err(&desc->intf->dev, |
| 205 | "nonzero urb status received: -EPIPE\n"); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 206 | break; |
| 207 | default: |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 208 | dev_err(&desc->intf->dev, |
| 209 | "Unexpected error %d\n", status); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 210 | break; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | desc->rerr = status; |
Oliver Neukum | 5c6142c | 2013-03-12 14:52:42 +0100 | [diff] [blame] | 215 | if (length + desc->length > desc->wMaxCommand) { |
| 216 | /* The buffer would overflow */ |
| 217 | set_bit(WDM_OVERFLOW, &desc->flags); |
| 218 | } else { |
| 219 | /* we may already be in overflow */ |
| 220 | if (!test_bit(WDM_OVERFLOW, &desc->flags)) { |
| 221 | memmove(desc->ubuf + desc->length, desc->inbuf, length); |
| 222 | desc->length += length; |
| 223 | desc->reslength = length; |
| 224 | } |
| 225 | } |
Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 226 | skip_error: |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 227 | wake_up(&desc->wait); |
| 228 | |
| 229 | set_bit(WDM_READ, &desc->flags); |
| 230 | spin_unlock(&desc->iuspin); |
| 231 | } |
| 232 | |
| 233 | static void wdm_int_callback(struct urb *urb) |
| 234 | { |
| 235 | int rv = 0; |
Oliver Neukum | e200d6b | 2013-08-06 14:22:59 +0200 | [diff] [blame] | 236 | int responding; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 237 | int status = urb->status; |
| 238 | struct wdm_device *desc; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 239 | struct usb_cdc_notification *dr; |
| 240 | |
| 241 | desc = urb->context; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 242 | dr = (struct usb_cdc_notification *)desc->sbuf; |
| 243 | |
| 244 | if (status) { |
| 245 | switch (status) { |
| 246 | case -ESHUTDOWN: |
| 247 | case -ENOENT: |
| 248 | case -ECONNRESET: |
| 249 | return; /* unplug */ |
| 250 | case -EPIPE: |
| 251 | set_bit(WDM_INT_STALL, &desc->flags); |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 252 | dev_err(&desc->intf->dev, "Stall on int endpoint\n"); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 253 | goto sw; /* halt is cleared in work */ |
| 254 | default: |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 255 | dev_err(&desc->intf->dev, |
| 256 | "nonzero urb status received: %d\n", status); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 257 | break; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | if (urb->actual_length < sizeof(struct usb_cdc_notification)) { |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 262 | dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n", |
| 263 | urb->actual_length); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 264 | goto exit; |
| 265 | } |
| 266 | |
| 267 | switch (dr->bNotificationType) { |
| 268 | case USB_CDC_NOTIFY_RESPONSE_AVAILABLE: |
| 269 | dev_dbg(&desc->intf->dev, |
| 270 | "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d", |
| 271 | dr->wIndex, dr->wLength); |
| 272 | break; |
| 273 | |
| 274 | case USB_CDC_NOTIFY_NETWORK_CONNECTION: |
| 275 | |
| 276 | dev_dbg(&desc->intf->dev, |
| 277 | "NOTIFY_NETWORK_CONNECTION %s network", |
| 278 | dr->wValue ? "connected to" : "disconnected from"); |
| 279 | goto exit; |
| 280 | default: |
| 281 | clear_bit(WDM_POLL_RUNNING, &desc->flags); |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 282 | dev_err(&desc->intf->dev, |
| 283 | "unknown notification %d received: index %d len %d\n", |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 284 | dr->bNotificationType, dr->wIndex, dr->wLength); |
| 285 | goto exit; |
| 286 | } |
| 287 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 288 | spin_lock(&desc->iuspin); |
| 289 | clear_bit(WDM_READ, &desc->flags); |
Oliver Neukum | e200d6b | 2013-08-06 14:22:59 +0200 | [diff] [blame] | 290 | responding = test_and_set_bit(WDM_RESPONDING, &desc->flags); |
| 291 | if (!responding && !test_bit(WDM_DISCONNECTING, &desc->flags) |
Oliver Neukum | beb1d35 | 2010-02-27 20:55:52 +0100 | [diff] [blame] | 292 | && !test_bit(WDM_SUSPENDING, &desc->flags)) { |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 293 | rv = usb_submit_urb(desc->response, GFP_ATOMIC); |
| 294 | dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d", |
| 295 | __func__, rv); |
| 296 | } |
| 297 | spin_unlock(&desc->iuspin); |
| 298 | if (rv < 0) { |
Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 299 | clear_bit(WDM_RESPONDING, &desc->flags); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 300 | if (rv == -EPERM) |
| 301 | return; |
| 302 | if (rv == -ENOMEM) { |
| 303 | sw: |
| 304 | rv = schedule_work(&desc->rxwork); |
| 305 | if (rv) |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 306 | dev_err(&desc->intf->dev, |
| 307 | "Cannot schedule work\n"); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | exit: |
| 311 | rv = usb_submit_urb(urb, GFP_ATOMIC); |
| 312 | if (rv) |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 313 | dev_err(&desc->intf->dev, |
| 314 | "%s - usb_submit_urb failed with result %d\n", |
| 315 | __func__, rv); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 316 | |
| 317 | } |
| 318 | |
| 319 | static void kill_urbs(struct wdm_device *desc) |
| 320 | { |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 321 | /* the order here is essential */ |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 322 | usb_kill_urb(desc->command); |
| 323 | usb_kill_urb(desc->validity); |
| 324 | usb_kill_urb(desc->response); |
| 325 | } |
| 326 | |
| 327 | static void free_urbs(struct wdm_device *desc) |
| 328 | { |
| 329 | usb_free_urb(desc->validity); |
| 330 | usb_free_urb(desc->response); |
| 331 | usb_free_urb(desc->command); |
| 332 | } |
| 333 | |
| 334 | static void cleanup(struct wdm_device *desc) |
| 335 | { |
Bjørn Mork | 8457d99 | 2012-01-16 15:12:00 +0100 | [diff] [blame] | 336 | kfree(desc->sbuf); |
| 337 | kfree(desc->inbuf); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 338 | kfree(desc->orq); |
| 339 | kfree(desc->irq); |
| 340 | kfree(desc->ubuf); |
| 341 | free_urbs(desc); |
| 342 | kfree(desc); |
| 343 | } |
| 344 | |
| 345 | static ssize_t wdm_write |
| 346 | (struct file *file, const char __user *buffer, size_t count, loff_t *ppos) |
| 347 | { |
| 348 | u8 *buf; |
| 349 | int rv = -EMSGSIZE, r, we; |
| 350 | struct wdm_device *desc = file->private_data; |
| 351 | struct usb_ctrlrequest *req; |
| 352 | |
| 353 | if (count > desc->wMaxCommand) |
| 354 | count = desc->wMaxCommand; |
| 355 | |
| 356 | spin_lock_irq(&desc->iuspin); |
| 357 | we = desc->werr; |
| 358 | desc->werr = 0; |
| 359 | spin_unlock_irq(&desc->iuspin); |
| 360 | if (we < 0) |
| 361 | return -EIO; |
| 362 | |
Oliver Neukum | 5c22837 | 2012-04-26 21:59:10 +0200 | [diff] [blame] | 363 | buf = kmalloc(count, GFP_KERNEL); |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 364 | if (!buf) { |
| 365 | rv = -ENOMEM; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 366 | goto outnl; |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | r = copy_from_user(buf, buffer, count); |
| 370 | if (r > 0) { |
| 371 | kfree(buf); |
| 372 | rv = -EFAULT; |
| 373 | goto outnl; |
| 374 | } |
| 375 | |
| 376 | /* concurrent writes and disconnect */ |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 377 | r = mutex_lock_interruptible(&desc->wlock); |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 378 | rv = -ERESTARTSYS; |
| 379 | if (r) { |
| 380 | kfree(buf); |
| 381 | goto outnl; |
| 382 | } |
| 383 | |
| 384 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { |
| 385 | kfree(buf); |
| 386 | rv = -ENODEV; |
| 387 | goto outnp; |
| 388 | } |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 389 | |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 390 | r = usb_autopm_get_interface(desc->intf); |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 391 | if (r < 0) { |
| 392 | kfree(buf); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 393 | goto outnp; |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 394 | } |
Oliver Neukum | 7f1dc31 | 2009-09-09 10:12:48 +0200 | [diff] [blame] | 395 | |
David Sterba | 0cdfb81 | 2010-12-27 18:49:58 +0100 | [diff] [blame] | 396 | if (!(file->f_flags & O_NONBLOCK)) |
Oliver Neukum | 7f1dc31 | 2009-09-09 10:12:48 +0200 | [diff] [blame] | 397 | r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE, |
| 398 | &desc->flags)); |
| 399 | else |
| 400 | if (test_bit(WDM_IN_USE, &desc->flags)) |
| 401 | r = -EAGAIN; |
Bjørn Mork | 8804420 | 2012-02-10 09:44:08 +0100 | [diff] [blame] | 402 | |
| 403 | if (test_bit(WDM_RESETTING, &desc->flags)) |
| 404 | r = -EIO; |
| 405 | |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 406 | if (r < 0) { |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 407 | kfree(buf); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 408 | goto out; |
| 409 | } |
| 410 | |
| 411 | req = desc->orq; |
| 412 | usb_fill_control_urb( |
| 413 | desc->command, |
| 414 | interface_to_usbdev(desc->intf), |
| 415 | /* using common endpoint 0 */ |
| 416 | usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0), |
| 417 | (unsigned char *)req, |
| 418 | buf, |
| 419 | count, |
| 420 | wdm_out_callback, |
| 421 | desc |
| 422 | ); |
| 423 | |
| 424 | req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS | |
| 425 | USB_RECIP_INTERFACE); |
| 426 | req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND; |
| 427 | req->wValue = 0; |
| 428 | req->wIndex = desc->inum; |
| 429 | req->wLength = cpu_to_le16(count); |
| 430 | set_bit(WDM_IN_USE, &desc->flags); |
Oliver Neukum | 5c22837 | 2012-04-26 21:59:10 +0200 | [diff] [blame] | 431 | desc->outbuf = buf; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 432 | |
| 433 | rv = usb_submit_urb(desc->command, GFP_KERNEL); |
| 434 | if (rv < 0) { |
| 435 | kfree(buf); |
Oliver Neukum | 5c22837 | 2012-04-26 21:59:10 +0200 | [diff] [blame] | 436 | desc->outbuf = NULL; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 437 | clear_bit(WDM_IN_USE, &desc->flags); |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 438 | dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 439 | } else { |
| 440 | dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d", |
| 441 | req->wIndex); |
| 442 | } |
| 443 | out: |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 444 | usb_autopm_put_interface(desc->intf); |
| 445 | outnp: |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 446 | mutex_unlock(&desc->wlock); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 447 | outnl: |
| 448 | return rv < 0 ? rv : count; |
| 449 | } |
| 450 | |
| 451 | static ssize_t wdm_read |
| 452 | (struct file *file, char __user *buffer, size_t count, loff_t *ppos) |
| 453 | { |
Ben Hutchings | 711c68b | 2012-02-12 06:00:41 +0000 | [diff] [blame] | 454 | int rv, cntr; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 455 | int i = 0; |
| 456 | struct wdm_device *desc = file->private_data; |
| 457 | |
| 458 | |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 459 | rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */ |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 460 | if (rv < 0) |
| 461 | return -ERESTARTSYS; |
| 462 | |
Ben Hutchings | 711c68b | 2012-02-12 06:00:41 +0000 | [diff] [blame] | 463 | cntr = ACCESS_ONCE(desc->length); |
| 464 | if (cntr == 0) { |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 465 | desc->read = 0; |
| 466 | retry: |
Oliver Neukum | 7f1dc31 | 2009-09-09 10:12:48 +0200 | [diff] [blame] | 467 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { |
| 468 | rv = -ENODEV; |
| 469 | goto err; |
| 470 | } |
Oliver Neukum | 5c6142c | 2013-03-12 14:52:42 +0100 | [diff] [blame] | 471 | if (test_bit(WDM_OVERFLOW, &desc->flags)) { |
| 472 | clear_bit(WDM_OVERFLOW, &desc->flags); |
| 473 | rv = -ENOBUFS; |
| 474 | goto err; |
| 475 | } |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 476 | i++; |
Oliver Neukum | 7f1dc31 | 2009-09-09 10:12:48 +0200 | [diff] [blame] | 477 | if (file->f_flags & O_NONBLOCK) { |
| 478 | if (!test_bit(WDM_READ, &desc->flags)) { |
| 479 | rv = cntr ? cntr : -EAGAIN; |
| 480 | goto err; |
| 481 | } |
| 482 | rv = 0; |
| 483 | } else { |
| 484 | rv = wait_event_interruptible(desc->wait, |
| 485 | test_bit(WDM_READ, &desc->flags)); |
| 486 | } |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 487 | |
Oliver Neukum | 7f1dc31 | 2009-09-09 10:12:48 +0200 | [diff] [blame] | 488 | /* may have happened while we slept */ |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 489 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { |
| 490 | rv = -ENODEV; |
| 491 | goto err; |
| 492 | } |
Bjørn Mork | 8804420 | 2012-02-10 09:44:08 +0100 | [diff] [blame] | 493 | if (test_bit(WDM_RESETTING, &desc->flags)) { |
| 494 | rv = -EIO; |
| 495 | goto err; |
| 496 | } |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 497 | usb_mark_last_busy(interface_to_usbdev(desc->intf)); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 498 | if (rv < 0) { |
| 499 | rv = -ERESTARTSYS; |
| 500 | goto err; |
| 501 | } |
| 502 | |
| 503 | spin_lock_irq(&desc->iuspin); |
| 504 | |
| 505 | if (desc->rerr) { /* read completed, error happened */ |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 506 | desc->rerr = 0; |
| 507 | spin_unlock_irq(&desc->iuspin); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 508 | rv = -EIO; |
| 509 | goto err; |
| 510 | } |
| 511 | /* |
| 512 | * recheck whether we've lost the race |
| 513 | * against the completion handler |
| 514 | */ |
| 515 | if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */ |
| 516 | spin_unlock_irq(&desc->iuspin); |
| 517 | goto retry; |
| 518 | } |
Oliver Neukum | 5c6142c | 2013-03-12 14:52:42 +0100 | [diff] [blame] | 519 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 520 | if (!desc->reslength) { /* zero length read */ |
Bjørn Mork | b80f6db | 2012-07-02 10:33:14 +0200 | [diff] [blame] | 521 | dev_dbg(&desc->intf->dev, "%s: zero length - clearing WDM_READ\n", __func__); |
| 522 | clear_bit(WDM_READ, &desc->flags); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 523 | spin_unlock_irq(&desc->iuspin); |
| 524 | goto retry; |
| 525 | } |
Ben Hutchings | 711c68b | 2012-02-12 06:00:41 +0000 | [diff] [blame] | 526 | cntr = desc->length; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 527 | spin_unlock_irq(&desc->iuspin); |
| 528 | } |
| 529 | |
Ben Hutchings | 711c68b | 2012-02-12 06:00:41 +0000 | [diff] [blame] | 530 | if (cntr > count) |
| 531 | cntr = count; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 532 | rv = copy_to_user(buffer, desc->ubuf, cntr); |
| 533 | if (rv > 0) { |
| 534 | rv = -EFAULT; |
| 535 | goto err; |
| 536 | } |
| 537 | |
Ben Hutchings | 711c68b | 2012-02-12 06:00:41 +0000 | [diff] [blame] | 538 | spin_lock_irq(&desc->iuspin); |
| 539 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 540 | for (i = 0; i < desc->length - cntr; i++) |
| 541 | desc->ubuf[i] = desc->ubuf[i + cntr]; |
| 542 | |
| 543 | desc->length -= cntr; |
Oliver Neukum | 87d65e5 | 2008-06-19 14:20:18 +0200 | [diff] [blame] | 544 | /* in case we had outstanding data */ |
| 545 | if (!desc->length) |
| 546 | clear_bit(WDM_READ, &desc->flags); |
Ben Hutchings | 711c68b | 2012-02-12 06:00:41 +0000 | [diff] [blame] | 547 | |
| 548 | spin_unlock_irq(&desc->iuspin); |
| 549 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 550 | rv = cntr; |
| 551 | |
| 552 | err: |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 553 | mutex_unlock(&desc->rlock); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 554 | return rv; |
| 555 | } |
| 556 | |
| 557 | static int wdm_flush(struct file *file, fl_owner_t id) |
| 558 | { |
| 559 | struct wdm_device *desc = file->private_data; |
| 560 | |
| 561 | wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags)); |
Bjørn Mork | 577bd3e | 2012-05-09 13:53:22 +0200 | [diff] [blame] | 562 | |
| 563 | /* cannot dereference desc->intf if WDM_DISCONNECTING */ |
| 564 | if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags)) |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 565 | dev_err(&desc->intf->dev, "Error in flush path: %d\n", |
| 566 | desc->werr); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 567 | |
Oliver Neukum | eae6861 | 2012-04-27 14:23:54 +0200 | [diff] [blame] | 568 | return usb_translate_errors(desc->werr); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait) |
| 572 | { |
| 573 | struct wdm_device *desc = file->private_data; |
| 574 | unsigned long flags; |
| 575 | unsigned int mask = 0; |
| 576 | |
| 577 | spin_lock_irqsave(&desc->iuspin, flags); |
| 578 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { |
Bjørn Mork | d0765ea | 2012-05-09 13:53:21 +0200 | [diff] [blame] | 579 | mask = POLLHUP | POLLERR; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 580 | spin_unlock_irqrestore(&desc->iuspin, flags); |
| 581 | goto desc_out; |
| 582 | } |
| 583 | if (test_bit(WDM_READ, &desc->flags)) |
| 584 | mask = POLLIN | POLLRDNORM; |
| 585 | if (desc->rerr || desc->werr) |
| 586 | mask |= POLLERR; |
| 587 | if (!test_bit(WDM_IN_USE, &desc->flags)) |
| 588 | mask |= POLLOUT | POLLWRNORM; |
| 589 | spin_unlock_irqrestore(&desc->iuspin, flags); |
| 590 | |
| 591 | poll_wait(file, &desc->wait, wait); |
| 592 | |
| 593 | desc_out: |
| 594 | return mask; |
| 595 | } |
| 596 | |
| 597 | static int wdm_open(struct inode *inode, struct file *file) |
| 598 | { |
| 599 | int minor = iminor(inode); |
| 600 | int rv = -ENODEV; |
| 601 | struct usb_interface *intf; |
| 602 | struct wdm_device *desc; |
| 603 | |
| 604 | mutex_lock(&wdm_mutex); |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 605 | desc = wdm_find_device_by_minor(minor); |
| 606 | if (!desc) |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 607 | goto out; |
| 608 | |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 609 | intf = desc->intf; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 610 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) |
| 611 | goto out; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 612 | file->private_data = desc; |
| 613 | |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 614 | rv = usb_autopm_get_interface(desc->intf); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 615 | if (rv < 0) { |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 616 | dev_err(&desc->intf->dev, "Error autopm - %d\n", rv); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 617 | goto out; |
| 618 | } |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 619 | |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 620 | /* using write lock to protect desc->count */ |
| 621 | mutex_lock(&desc->wlock); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 622 | if (!desc->count++) { |
Oliver Neukum | d771d8a | 2011-04-29 14:12:21 +0200 | [diff] [blame] | 623 | desc->werr = 0; |
| 624 | desc->rerr = 0; |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 625 | rv = usb_submit_urb(desc->validity, GFP_KERNEL); |
| 626 | if (rv < 0) { |
| 627 | desc->count--; |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 628 | dev_err(&desc->intf->dev, |
| 629 | "Error submitting int urb - %d\n", rv); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 630 | } |
| 631 | } else { |
| 632 | rv = 0; |
| 633 | } |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 634 | mutex_unlock(&desc->wlock); |
Bjørn Mork | 3cc3615 | 2012-03-06 17:29:22 +0100 | [diff] [blame] | 635 | if (desc->count == 1) |
| 636 | desc->manage_power(intf, 1); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 637 | usb_autopm_put_interface(desc->intf); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 638 | out: |
| 639 | mutex_unlock(&wdm_mutex); |
| 640 | return rv; |
| 641 | } |
| 642 | |
| 643 | static int wdm_release(struct inode *inode, struct file *file) |
| 644 | { |
| 645 | struct wdm_device *desc = file->private_data; |
| 646 | |
| 647 | mutex_lock(&wdm_mutex); |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 648 | |
| 649 | /* using write lock to protect desc->count */ |
| 650 | mutex_lock(&desc->wlock); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 651 | desc->count--; |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 652 | mutex_unlock(&desc->wlock); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 653 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 654 | if (!desc->count) { |
Bjørn Mork | 577bd3e | 2012-05-09 13:53:22 +0200 | [diff] [blame] | 655 | if (!test_bit(WDM_DISCONNECTING, &desc->flags)) { |
| 656 | dev_dbg(&desc->intf->dev, "wdm_release: cleanup"); |
| 657 | kill_urbs(desc); |
Bjørn Mork | 3cc3615 | 2012-03-06 17:29:22 +0100 | [diff] [blame] | 658 | desc->manage_power(desc->intf, 0); |
Bjørn Mork | 577bd3e | 2012-05-09 13:53:22 +0200 | [diff] [blame] | 659 | } else { |
| 660 | /* must avoid dev_printk here as desc->intf is invalid */ |
| 661 | pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__); |
Oliver Neukum | fccfda6 | 2012-04-27 14:36:37 +0200 | [diff] [blame] | 662 | cleanup(desc); |
Bjørn Mork | 577bd3e | 2012-05-09 13:53:22 +0200 | [diff] [blame] | 663 | } |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 664 | } |
| 665 | mutex_unlock(&wdm_mutex); |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | static const struct file_operations wdm_fops = { |
| 670 | .owner = THIS_MODULE, |
| 671 | .read = wdm_read, |
| 672 | .write = wdm_write, |
| 673 | .open = wdm_open, |
| 674 | .flush = wdm_flush, |
| 675 | .release = wdm_release, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 676 | .poll = wdm_poll, |
| 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 | e200d6b | 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 | e200d6b | 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 | e200d6b | 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 | e200d6b | 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; |
| 785 | desc->irq->wIndex = desc->inum; |
| 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 | 9ed2cb7 | 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 | bc8a391 | 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 | 9ed2cb7 | 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); |
| 948 | mutex_unlock(&wdm_mutex); |
| 949 | } |
| 950 | |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 951 | #ifdef CONFIG_PM |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 952 | static int wdm_suspend(struct usb_interface *intf, pm_message_t message) |
| 953 | { |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 954 | struct wdm_device *desc = wdm_find_device(intf); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 955 | int rv = 0; |
| 956 | |
| 957 | dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor); |
| 958 | |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 959 | /* if this is an autosuspend the caller does the locking */ |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 960 | if (!PMSG_IS_AUTO(message)) { |
| 961 | mutex_lock(&desc->rlock); |
| 962 | mutex_lock(&desc->wlock); |
| 963 | } |
Oliver Neukum | 62e6685 | 2010-02-27 20:56:22 +0100 | [diff] [blame] | 964 | spin_lock_irq(&desc->iuspin); |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 965 | |
Alan Stern | 5b1b0b8 | 2011-08-19 23:49:48 +0200 | [diff] [blame] | 966 | if (PMSG_IS_AUTO(message) && |
Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 967 | (test_bit(WDM_IN_USE, &desc->flags) |
| 968 | || test_bit(WDM_RESPONDING, &desc->flags))) { |
Oliver Neukum | 62e6685 | 2010-02-27 20:56:22 +0100 | [diff] [blame] | 969 | spin_unlock_irq(&desc->iuspin); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 970 | rv = -EBUSY; |
| 971 | } else { |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 972 | |
Oliver Neukum | beb1d35 | 2010-02-27 20:55:52 +0100 | [diff] [blame] | 973 | set_bit(WDM_SUSPENDING, &desc->flags); |
Oliver Neukum | 62e6685 | 2010-02-27 20:56:22 +0100 | [diff] [blame] | 974 | spin_unlock_irq(&desc->iuspin); |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 975 | /* callback submits work - order is essential */ |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 976 | kill_urbs(desc); |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 977 | cancel_work_sync(&desc->rxwork); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 978 | } |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 979 | if (!PMSG_IS_AUTO(message)) { |
| 980 | mutex_unlock(&desc->wlock); |
| 981 | mutex_unlock(&desc->rlock); |
| 982 | } |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 983 | |
| 984 | return rv; |
| 985 | } |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 986 | #endif |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 987 | |
| 988 | static int recover_from_urb_loss(struct wdm_device *desc) |
| 989 | { |
| 990 | int rv = 0; |
| 991 | |
| 992 | if (desc->count) { |
| 993 | rv = usb_submit_urb(desc->validity, GFP_NOIO); |
| 994 | if (rv < 0) |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 995 | dev_err(&desc->intf->dev, |
| 996 | "Error resume submitting int urb - %d\n", rv); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 997 | } |
| 998 | return rv; |
| 999 | } |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 1000 | |
| 1001 | #ifdef CONFIG_PM |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1002 | static int wdm_resume(struct usb_interface *intf) |
| 1003 | { |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 1004 | struct wdm_device *desc = wdm_find_device(intf); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1005 | int rv; |
| 1006 | |
| 1007 | dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor); |
Oliver Neukum | 338124c | 2010-02-27 20:57:12 +0100 | [diff] [blame] | 1008 | |
Oliver Neukum | beb1d35 | 2010-02-27 20:55:52 +0100 | [diff] [blame] | 1009 | clear_bit(WDM_SUSPENDING, &desc->flags); |
Oliver Neukum | 62e6685 | 2010-02-27 20:56:22 +0100 | [diff] [blame] | 1010 | rv = recover_from_urb_loss(desc); |
Oliver Neukum | 338124c | 2010-02-27 20:57:12 +0100 | [diff] [blame] | 1011 | |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1012 | return rv; |
| 1013 | } |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 1014 | #endif |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1015 | |
| 1016 | static int wdm_pre_reset(struct usb_interface *intf) |
| 1017 | { |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 1018 | struct wdm_device *desc = wdm_find_device(intf); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1019 | |
Oliver Neukum | d771d8a | 2011-04-29 14:12:21 +0200 | [diff] [blame] | 1020 | /* |
| 1021 | * we notify everybody using poll of |
| 1022 | * an exceptional situation |
| 1023 | * must be done before recovery lest a spontaneous |
| 1024 | * message from the device is lost |
| 1025 | */ |
| 1026 | spin_lock_irq(&desc->iuspin); |
Bjørn Mork | 8804420 | 2012-02-10 09:44:08 +0100 | [diff] [blame] | 1027 | set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */ |
| 1028 | set_bit(WDM_READ, &desc->flags); /* unblock read */ |
| 1029 | clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */ |
Oliver Neukum | d771d8a | 2011-04-29 14:12:21 +0200 | [diff] [blame] | 1030 | desc->rerr = -EINTR; |
| 1031 | spin_unlock_irq(&desc->iuspin); |
| 1032 | wake_up_all(&desc->wait); |
Bjørn Mork | 8804420 | 2012-02-10 09:44:08 +0100 | [diff] [blame] | 1033 | mutex_lock(&desc->rlock); |
| 1034 | mutex_lock(&desc->wlock); |
| 1035 | kill_urbs(desc); |
| 1036 | cancel_work_sync(&desc->rxwork); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1037 | return 0; |
| 1038 | } |
| 1039 | |
| 1040 | static int wdm_post_reset(struct usb_interface *intf) |
| 1041 | { |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 1042 | struct wdm_device *desc = wdm_find_device(intf); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1043 | int rv; |
| 1044 | |
Oliver Neukum | 5c6142c | 2013-03-12 14:52:42 +0100 | [diff] [blame] | 1045 | clear_bit(WDM_OVERFLOW, &desc->flags); |
Bjørn Mork | 8804420 | 2012-02-10 09:44:08 +0100 | [diff] [blame] | 1046 | clear_bit(WDM_RESETTING, &desc->flags); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1047 | rv = recover_from_urb_loss(desc); |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 1048 | mutex_unlock(&desc->wlock); |
| 1049 | mutex_unlock(&desc->rlock); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1050 | return 0; |
| 1051 | } |
| 1052 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 1053 | static struct usb_driver wdm_driver = { |
| 1054 | .name = "cdc_wdm", |
| 1055 | .probe = wdm_probe, |
| 1056 | .disconnect = wdm_disconnect, |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 1057 | #ifdef CONFIG_PM |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1058 | .suspend = wdm_suspend, |
| 1059 | .resume = wdm_resume, |
| 1060 | .reset_resume = wdm_resume, |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 1061 | #endif |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1062 | .pre_reset = wdm_pre_reset, |
| 1063 | .post_reset = wdm_post_reset, |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 1064 | .id_table = wdm_ids, |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1065 | .supports_autosuspend = 1, |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 1066 | }; |
| 1067 | |
Greg Kroah-Hartman | 65db430 | 2011-11-18 09:34:02 -0800 | [diff] [blame] | 1068 | module_usb_driver(wdm_driver); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 1069 | |
| 1070 | MODULE_AUTHOR(DRIVER_AUTHOR); |
Oliver Neukum | 87d65e5 | 2008-06-19 14:20:18 +0200 | [diff] [blame] | 1071 | MODULE_DESCRIPTION(DRIVER_DESC); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 1072 | MODULE_LICENSE("GPL"); |