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