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