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> |
| 26 | |
| 27 | /* |
| 28 | * Version Information |
| 29 | */ |
Oliver Neukum | 87d65e5 | 2008-06-19 14:20:18 +0200 | [diff] [blame] | 30 | #define DRIVER_VERSION "v0.03" |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 31 | #define DRIVER_AUTHOR "Oliver Neukum" |
Oliver Neukum | 87d65e5 | 2008-06-19 14:20:18 +0200 | [diff] [blame] | 32 | #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] | 33 | |
Németh Márton | 6ef4852 | 2010-01-10 15:33:45 +0100 | [diff] [blame] | 34 | static const struct usb_device_id wdm_ids[] = { |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 35 | { |
| 36 | .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS | |
| 37 | USB_DEVICE_ID_MATCH_INT_SUBCLASS, |
| 38 | .bInterfaceClass = USB_CLASS_COMM, |
| 39 | .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM |
| 40 | }, |
| 41 | { } |
| 42 | }; |
| 43 | |
Oliver Neukum | aa5380b | 2008-10-13 14:05:20 +0200 | [diff] [blame] | 44 | MODULE_DEVICE_TABLE (usb, wdm_ids); |
| 45 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 46 | #define WDM_MINOR_BASE 176 |
| 47 | |
| 48 | |
| 49 | #define WDM_IN_USE 1 |
| 50 | #define WDM_DISCONNECTING 2 |
| 51 | #define WDM_RESULT 3 |
| 52 | #define WDM_READ 4 |
| 53 | #define WDM_INT_STALL 5 |
| 54 | #define WDM_POLL_RUNNING 6 |
Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 55 | #define WDM_RESPONDING 7 |
Oliver Neukum | beb1d35 | 2010-02-27 20:55:52 +0100 | [diff] [blame] | 56 | #define WDM_SUSPENDING 8 |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 57 | |
| 58 | #define WDM_MAX 16 |
| 59 | |
Bjørn Mork | 7e3054a | 2012-01-20 01:49:57 +0100 | [diff] [blame] | 60 | /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */ |
| 61 | #define WDM_DEFAULT_BUFSIZE 256 |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 62 | |
| 63 | static DEFINE_MUTEX(wdm_mutex); |
| 64 | |
| 65 | /* --- method tables --- */ |
| 66 | |
| 67 | struct wdm_device { |
| 68 | u8 *inbuf; /* buffer for response */ |
| 69 | u8 *outbuf; /* buffer for command */ |
| 70 | u8 *sbuf; /* buffer for status */ |
| 71 | u8 *ubuf; /* buffer for copy to user space */ |
| 72 | |
| 73 | struct urb *command; |
| 74 | struct urb *response; |
| 75 | struct urb *validity; |
| 76 | struct usb_interface *intf; |
| 77 | struct usb_ctrlrequest *orq; |
| 78 | struct usb_ctrlrequest *irq; |
| 79 | spinlock_t iuspin; |
| 80 | |
| 81 | unsigned long flags; |
| 82 | u16 bufsize; |
| 83 | u16 wMaxCommand; |
| 84 | u16 wMaxPacketSize; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 85 | __le16 inum; |
| 86 | int reslength; |
| 87 | int length; |
| 88 | int read; |
| 89 | int count; |
| 90 | dma_addr_t shandle; |
| 91 | dma_addr_t ihandle; |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 92 | struct mutex lock; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 93 | wait_queue_head_t wait; |
| 94 | struct work_struct rxwork; |
| 95 | int werr; |
| 96 | int rerr; |
| 97 | }; |
| 98 | |
| 99 | static struct usb_driver wdm_driver; |
| 100 | |
| 101 | /* --- callbacks --- */ |
| 102 | static void wdm_out_callback(struct urb *urb) |
| 103 | { |
| 104 | struct wdm_device *desc; |
| 105 | desc = urb->context; |
| 106 | spin_lock(&desc->iuspin); |
| 107 | desc->werr = urb->status; |
| 108 | spin_unlock(&desc->iuspin); |
| 109 | clear_bit(WDM_IN_USE, &desc->flags); |
| 110 | kfree(desc->outbuf); |
| 111 | wake_up(&desc->wait); |
| 112 | } |
| 113 | |
| 114 | static void wdm_in_callback(struct urb *urb) |
| 115 | { |
| 116 | struct wdm_device *desc = urb->context; |
| 117 | int status = urb->status; |
| 118 | |
| 119 | spin_lock(&desc->iuspin); |
Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 120 | clear_bit(WDM_RESPONDING, &desc->flags); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 121 | |
| 122 | if (status) { |
| 123 | switch (status) { |
| 124 | case -ENOENT: |
| 125 | dev_dbg(&desc->intf->dev, |
| 126 | "nonzero urb status received: -ENOENT"); |
Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 127 | goto skip_error; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 128 | case -ECONNRESET: |
| 129 | dev_dbg(&desc->intf->dev, |
| 130 | "nonzero urb status received: -ECONNRESET"); |
Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 131 | goto skip_error; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 132 | case -ESHUTDOWN: |
| 133 | dev_dbg(&desc->intf->dev, |
| 134 | "nonzero urb status received: -ESHUTDOWN"); |
Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 135 | goto skip_error; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 136 | case -EPIPE: |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 137 | dev_err(&desc->intf->dev, |
| 138 | "nonzero urb status received: -EPIPE\n"); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 139 | break; |
| 140 | default: |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 141 | dev_err(&desc->intf->dev, |
| 142 | "Unexpected error %d\n", status); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 143 | break; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | desc->rerr = status; |
| 148 | desc->reslength = urb->actual_length; |
| 149 | memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength); |
| 150 | desc->length += desc->reslength; |
Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 151 | skip_error: |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 152 | wake_up(&desc->wait); |
| 153 | |
| 154 | set_bit(WDM_READ, &desc->flags); |
| 155 | spin_unlock(&desc->iuspin); |
| 156 | } |
| 157 | |
| 158 | static void wdm_int_callback(struct urb *urb) |
| 159 | { |
| 160 | int rv = 0; |
| 161 | int status = urb->status; |
| 162 | struct wdm_device *desc; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 163 | struct usb_cdc_notification *dr; |
| 164 | |
| 165 | desc = urb->context; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 166 | dr = (struct usb_cdc_notification *)desc->sbuf; |
| 167 | |
| 168 | if (status) { |
| 169 | switch (status) { |
| 170 | case -ESHUTDOWN: |
| 171 | case -ENOENT: |
| 172 | case -ECONNRESET: |
| 173 | return; /* unplug */ |
| 174 | case -EPIPE: |
| 175 | set_bit(WDM_INT_STALL, &desc->flags); |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 176 | dev_err(&desc->intf->dev, "Stall on int endpoint\n"); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 177 | goto sw; /* halt is cleared in work */ |
| 178 | default: |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 179 | dev_err(&desc->intf->dev, |
| 180 | "nonzero urb status received: %d\n", status); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | if (urb->actual_length < sizeof(struct usb_cdc_notification)) { |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 186 | dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n", |
| 187 | urb->actual_length); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 188 | goto exit; |
| 189 | } |
| 190 | |
| 191 | switch (dr->bNotificationType) { |
| 192 | case USB_CDC_NOTIFY_RESPONSE_AVAILABLE: |
| 193 | dev_dbg(&desc->intf->dev, |
| 194 | "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d", |
| 195 | dr->wIndex, dr->wLength); |
| 196 | break; |
| 197 | |
| 198 | case USB_CDC_NOTIFY_NETWORK_CONNECTION: |
| 199 | |
| 200 | dev_dbg(&desc->intf->dev, |
| 201 | "NOTIFY_NETWORK_CONNECTION %s network", |
| 202 | dr->wValue ? "connected to" : "disconnected from"); |
| 203 | goto exit; |
| 204 | default: |
| 205 | clear_bit(WDM_POLL_RUNNING, &desc->flags); |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 206 | dev_err(&desc->intf->dev, |
| 207 | "unknown notification %d received: index %d len %d\n", |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 208 | dr->bNotificationType, dr->wIndex, dr->wLength); |
| 209 | goto exit; |
| 210 | } |
| 211 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 212 | spin_lock(&desc->iuspin); |
| 213 | clear_bit(WDM_READ, &desc->flags); |
Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 214 | set_bit(WDM_RESPONDING, &desc->flags); |
Oliver Neukum | beb1d35 | 2010-02-27 20:55:52 +0100 | [diff] [blame] | 215 | if (!test_bit(WDM_DISCONNECTING, &desc->flags) |
| 216 | && !test_bit(WDM_SUSPENDING, &desc->flags)) { |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 217 | rv = usb_submit_urb(desc->response, GFP_ATOMIC); |
| 218 | dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d", |
| 219 | __func__, rv); |
| 220 | } |
| 221 | spin_unlock(&desc->iuspin); |
| 222 | if (rv < 0) { |
Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 223 | clear_bit(WDM_RESPONDING, &desc->flags); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 224 | if (rv == -EPERM) |
| 225 | return; |
| 226 | if (rv == -ENOMEM) { |
| 227 | sw: |
| 228 | rv = schedule_work(&desc->rxwork); |
| 229 | if (rv) |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 230 | dev_err(&desc->intf->dev, |
| 231 | "Cannot schedule work\n"); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | exit: |
| 235 | rv = usb_submit_urb(urb, GFP_ATOMIC); |
| 236 | if (rv) |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 237 | dev_err(&desc->intf->dev, |
| 238 | "%s - usb_submit_urb failed with result %d\n", |
| 239 | __func__, rv); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 240 | |
| 241 | } |
| 242 | |
| 243 | static void kill_urbs(struct wdm_device *desc) |
| 244 | { |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 245 | /* the order here is essential */ |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 246 | usb_kill_urb(desc->command); |
| 247 | usb_kill_urb(desc->validity); |
| 248 | usb_kill_urb(desc->response); |
| 249 | } |
| 250 | |
| 251 | static void free_urbs(struct wdm_device *desc) |
| 252 | { |
| 253 | usb_free_urb(desc->validity); |
| 254 | usb_free_urb(desc->response); |
| 255 | usb_free_urb(desc->command); |
| 256 | } |
| 257 | |
| 258 | static void cleanup(struct wdm_device *desc) |
| 259 | { |
Bjørn Mork | 8457d99 | 2012-01-16 15:12:00 +0100 | [diff] [blame] | 260 | kfree(desc->sbuf); |
| 261 | kfree(desc->inbuf); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 262 | kfree(desc->orq); |
| 263 | kfree(desc->irq); |
| 264 | kfree(desc->ubuf); |
| 265 | free_urbs(desc); |
| 266 | kfree(desc); |
| 267 | } |
| 268 | |
| 269 | static ssize_t wdm_write |
| 270 | (struct file *file, const char __user *buffer, size_t count, loff_t *ppos) |
| 271 | { |
| 272 | u8 *buf; |
| 273 | int rv = -EMSGSIZE, r, we; |
| 274 | struct wdm_device *desc = file->private_data; |
| 275 | struct usb_ctrlrequest *req; |
| 276 | |
| 277 | if (count > desc->wMaxCommand) |
| 278 | count = desc->wMaxCommand; |
| 279 | |
| 280 | spin_lock_irq(&desc->iuspin); |
| 281 | we = desc->werr; |
| 282 | desc->werr = 0; |
| 283 | spin_unlock_irq(&desc->iuspin); |
| 284 | if (we < 0) |
| 285 | return -EIO; |
| 286 | |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 287 | desc->outbuf = buf = kmalloc(count, GFP_KERNEL); |
| 288 | if (!buf) { |
| 289 | rv = -ENOMEM; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 290 | goto outnl; |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | r = copy_from_user(buf, buffer, count); |
| 294 | if (r > 0) { |
| 295 | kfree(buf); |
| 296 | rv = -EFAULT; |
| 297 | goto outnl; |
| 298 | } |
| 299 | |
| 300 | /* concurrent writes and disconnect */ |
| 301 | r = mutex_lock_interruptible(&desc->lock); |
| 302 | rv = -ERESTARTSYS; |
| 303 | if (r) { |
| 304 | kfree(buf); |
| 305 | goto outnl; |
| 306 | } |
| 307 | |
| 308 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { |
| 309 | kfree(buf); |
| 310 | rv = -ENODEV; |
| 311 | goto outnp; |
| 312 | } |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 313 | |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 314 | r = usb_autopm_get_interface(desc->intf); |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 315 | if (r < 0) { |
| 316 | kfree(buf); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 317 | goto outnp; |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 318 | } |
Oliver Neukum | 7f1dc313d | 2009-09-09 10:12:48 +0200 | [diff] [blame] | 319 | |
David Sterba | 0cdfb81 | 2010-12-27 18:49:58 +0100 | [diff] [blame] | 320 | if (!(file->f_flags & O_NONBLOCK)) |
Oliver Neukum | 7f1dc313d | 2009-09-09 10:12:48 +0200 | [diff] [blame] | 321 | r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE, |
| 322 | &desc->flags)); |
| 323 | else |
| 324 | if (test_bit(WDM_IN_USE, &desc->flags)) |
| 325 | r = -EAGAIN; |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 326 | if (r < 0) { |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 327 | kfree(buf); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 328 | goto out; |
| 329 | } |
| 330 | |
| 331 | req = desc->orq; |
| 332 | usb_fill_control_urb( |
| 333 | desc->command, |
| 334 | interface_to_usbdev(desc->intf), |
| 335 | /* using common endpoint 0 */ |
| 336 | usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0), |
| 337 | (unsigned char *)req, |
| 338 | buf, |
| 339 | count, |
| 340 | wdm_out_callback, |
| 341 | desc |
| 342 | ); |
| 343 | |
| 344 | req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS | |
| 345 | USB_RECIP_INTERFACE); |
| 346 | req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND; |
| 347 | req->wValue = 0; |
| 348 | req->wIndex = desc->inum; |
| 349 | req->wLength = cpu_to_le16(count); |
| 350 | set_bit(WDM_IN_USE, &desc->flags); |
| 351 | |
| 352 | rv = usb_submit_urb(desc->command, GFP_KERNEL); |
| 353 | if (rv < 0) { |
| 354 | kfree(buf); |
| 355 | clear_bit(WDM_IN_USE, &desc->flags); |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 356 | dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 357 | } else { |
| 358 | dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d", |
| 359 | req->wIndex); |
| 360 | } |
| 361 | out: |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 362 | usb_autopm_put_interface(desc->intf); |
| 363 | outnp: |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 364 | mutex_unlock(&desc->lock); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 365 | outnl: |
| 366 | return rv < 0 ? rv : count; |
| 367 | } |
| 368 | |
| 369 | static ssize_t wdm_read |
| 370 | (struct file *file, char __user *buffer, size_t count, loff_t *ppos) |
| 371 | { |
Oliver Neukum | 7f1dc313d | 2009-09-09 10:12:48 +0200 | [diff] [blame] | 372 | int rv, cntr = 0; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 373 | int i = 0; |
| 374 | struct wdm_device *desc = file->private_data; |
| 375 | |
| 376 | |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 377 | rv = mutex_lock_interruptible(&desc->lock); /*concurrent reads */ |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 378 | if (rv < 0) |
| 379 | return -ERESTARTSYS; |
| 380 | |
| 381 | if (desc->length == 0) { |
| 382 | desc->read = 0; |
| 383 | retry: |
Oliver Neukum | 7f1dc313d | 2009-09-09 10:12:48 +0200 | [diff] [blame] | 384 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { |
| 385 | rv = -ENODEV; |
| 386 | goto err; |
| 387 | } |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 388 | i++; |
Oliver Neukum | 7f1dc313d | 2009-09-09 10:12:48 +0200 | [diff] [blame] | 389 | if (file->f_flags & O_NONBLOCK) { |
| 390 | if (!test_bit(WDM_READ, &desc->flags)) { |
| 391 | rv = cntr ? cntr : -EAGAIN; |
| 392 | goto err; |
| 393 | } |
| 394 | rv = 0; |
| 395 | } else { |
| 396 | rv = wait_event_interruptible(desc->wait, |
| 397 | test_bit(WDM_READ, &desc->flags)); |
| 398 | } |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 399 | |
Oliver Neukum | 7f1dc313d | 2009-09-09 10:12:48 +0200 | [diff] [blame] | 400 | /* may have happened while we slept */ |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 401 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { |
| 402 | rv = -ENODEV; |
| 403 | goto err; |
| 404 | } |
| 405 | usb_mark_last_busy(interface_to_usbdev(desc->intf)); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 406 | if (rv < 0) { |
| 407 | rv = -ERESTARTSYS; |
| 408 | goto err; |
| 409 | } |
| 410 | |
| 411 | spin_lock_irq(&desc->iuspin); |
| 412 | |
| 413 | if (desc->rerr) { /* read completed, error happened */ |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 414 | desc->rerr = 0; |
| 415 | spin_unlock_irq(&desc->iuspin); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 416 | rv = -EIO; |
| 417 | goto err; |
| 418 | } |
| 419 | /* |
| 420 | * recheck whether we've lost the race |
| 421 | * against the completion handler |
| 422 | */ |
| 423 | if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */ |
| 424 | spin_unlock_irq(&desc->iuspin); |
| 425 | goto retry; |
| 426 | } |
| 427 | if (!desc->reslength) { /* zero length read */ |
| 428 | spin_unlock_irq(&desc->iuspin); |
| 429 | goto retry; |
| 430 | } |
| 431 | clear_bit(WDM_READ, &desc->flags); |
| 432 | spin_unlock_irq(&desc->iuspin); |
| 433 | } |
| 434 | |
| 435 | cntr = count > desc->length ? desc->length : count; |
| 436 | rv = copy_to_user(buffer, desc->ubuf, cntr); |
| 437 | if (rv > 0) { |
| 438 | rv = -EFAULT; |
| 439 | goto err; |
| 440 | } |
| 441 | |
| 442 | for (i = 0; i < desc->length - cntr; i++) |
| 443 | desc->ubuf[i] = desc->ubuf[i + cntr]; |
| 444 | |
| 445 | desc->length -= cntr; |
Oliver Neukum | 87d65e5 | 2008-06-19 14:20:18 +0200 | [diff] [blame] | 446 | /* in case we had outstanding data */ |
| 447 | if (!desc->length) |
| 448 | clear_bit(WDM_READ, &desc->flags); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 449 | rv = cntr; |
| 450 | |
| 451 | err: |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 452 | mutex_unlock(&desc->lock); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 453 | return rv; |
| 454 | } |
| 455 | |
| 456 | static int wdm_flush(struct file *file, fl_owner_t id) |
| 457 | { |
| 458 | struct wdm_device *desc = file->private_data; |
| 459 | |
| 460 | wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags)); |
| 461 | if (desc->werr < 0) |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 462 | dev_err(&desc->intf->dev, "Error in flush path: %d\n", |
| 463 | desc->werr); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 464 | |
| 465 | return desc->werr; |
| 466 | } |
| 467 | |
| 468 | static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait) |
| 469 | { |
| 470 | struct wdm_device *desc = file->private_data; |
| 471 | unsigned long flags; |
| 472 | unsigned int mask = 0; |
| 473 | |
| 474 | spin_lock_irqsave(&desc->iuspin, flags); |
| 475 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { |
| 476 | mask = POLLERR; |
| 477 | spin_unlock_irqrestore(&desc->iuspin, flags); |
| 478 | goto desc_out; |
| 479 | } |
| 480 | if (test_bit(WDM_READ, &desc->flags)) |
| 481 | mask = POLLIN | POLLRDNORM; |
| 482 | if (desc->rerr || desc->werr) |
| 483 | mask |= POLLERR; |
| 484 | if (!test_bit(WDM_IN_USE, &desc->flags)) |
| 485 | mask |= POLLOUT | POLLWRNORM; |
| 486 | spin_unlock_irqrestore(&desc->iuspin, flags); |
| 487 | |
| 488 | poll_wait(file, &desc->wait, wait); |
| 489 | |
| 490 | desc_out: |
| 491 | return mask; |
| 492 | } |
| 493 | |
| 494 | static int wdm_open(struct inode *inode, struct file *file) |
| 495 | { |
| 496 | int minor = iminor(inode); |
| 497 | int rv = -ENODEV; |
| 498 | struct usb_interface *intf; |
| 499 | struct wdm_device *desc; |
| 500 | |
| 501 | mutex_lock(&wdm_mutex); |
| 502 | intf = usb_find_interface(&wdm_driver, minor); |
| 503 | if (!intf) |
| 504 | goto out; |
| 505 | |
| 506 | desc = usb_get_intfdata(intf); |
| 507 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) |
| 508 | goto out; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 509 | file->private_data = desc; |
| 510 | |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 511 | rv = usb_autopm_get_interface(desc->intf); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 512 | if (rv < 0) { |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 513 | dev_err(&desc->intf->dev, "Error autopm - %d\n", rv); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 514 | goto out; |
| 515 | } |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 516 | intf->needs_remote_wakeup = 1; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 517 | |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 518 | mutex_lock(&desc->lock); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 519 | if (!desc->count++) { |
Oliver Neukum | d771d8a | 2011-04-29 14:12:21 +0200 | [diff] [blame] | 520 | desc->werr = 0; |
| 521 | desc->rerr = 0; |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 522 | rv = usb_submit_urb(desc->validity, GFP_KERNEL); |
| 523 | if (rv < 0) { |
| 524 | desc->count--; |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 525 | dev_err(&desc->intf->dev, |
| 526 | "Error submitting int urb - %d\n", rv); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 527 | } |
| 528 | } else { |
| 529 | rv = 0; |
| 530 | } |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 531 | mutex_unlock(&desc->lock); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 532 | usb_autopm_put_interface(desc->intf); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 533 | out: |
| 534 | mutex_unlock(&wdm_mutex); |
| 535 | return rv; |
| 536 | } |
| 537 | |
| 538 | static int wdm_release(struct inode *inode, struct file *file) |
| 539 | { |
| 540 | struct wdm_device *desc = file->private_data; |
| 541 | |
| 542 | mutex_lock(&wdm_mutex); |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 543 | mutex_lock(&desc->lock); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 544 | desc->count--; |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 545 | mutex_unlock(&desc->lock); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 546 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 547 | if (!desc->count) { |
| 548 | dev_dbg(&desc->intf->dev, "wdm_release: cleanup"); |
| 549 | kill_urbs(desc); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 550 | if (!test_bit(WDM_DISCONNECTING, &desc->flags)) |
| 551 | desc->intf->needs_remote_wakeup = 0; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 552 | } |
| 553 | mutex_unlock(&wdm_mutex); |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | static const struct file_operations wdm_fops = { |
| 558 | .owner = THIS_MODULE, |
| 559 | .read = wdm_read, |
| 560 | .write = wdm_write, |
| 561 | .open = wdm_open, |
| 562 | .flush = wdm_flush, |
| 563 | .release = wdm_release, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 564 | .poll = wdm_poll, |
| 565 | .llseek = noop_llseek, |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 566 | }; |
| 567 | |
| 568 | static struct usb_class_driver wdm_class = { |
| 569 | .name = "cdc-wdm%d", |
| 570 | .fops = &wdm_fops, |
| 571 | .minor_base = WDM_MINOR_BASE, |
| 572 | }; |
| 573 | |
| 574 | /* --- error handling --- */ |
| 575 | static void wdm_rxwork(struct work_struct *work) |
| 576 | { |
| 577 | struct wdm_device *desc = container_of(work, struct wdm_device, rxwork); |
| 578 | unsigned long flags; |
| 579 | int rv; |
| 580 | |
| 581 | spin_lock_irqsave(&desc->iuspin, flags); |
| 582 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { |
| 583 | spin_unlock_irqrestore(&desc->iuspin, flags); |
| 584 | } else { |
| 585 | spin_unlock_irqrestore(&desc->iuspin, flags); |
| 586 | rv = usb_submit_urb(desc->response, GFP_KERNEL); |
| 587 | if (rv < 0 && rv != -EPERM) { |
| 588 | spin_lock_irqsave(&desc->iuspin, flags); |
| 589 | if (!test_bit(WDM_DISCONNECTING, &desc->flags)) |
| 590 | schedule_work(&desc->rxwork); |
| 591 | spin_unlock_irqrestore(&desc->iuspin, flags); |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | /* --- hotplug --- */ |
| 597 | |
| 598 | static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id) |
| 599 | { |
| 600 | int rv = -EINVAL; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 601 | struct wdm_device *desc; |
| 602 | struct usb_host_interface *iface; |
| 603 | struct usb_endpoint_descriptor *ep; |
| 604 | struct usb_cdc_dmm_desc *dmhd; |
| 605 | u8 *buffer = intf->altsetting->extra; |
| 606 | int buflen = intf->altsetting->extralen; |
Bjørn Mork | 7e3054a | 2012-01-20 01:49:57 +0100 | [diff] [blame] | 607 | u16 maxcom = WDM_DEFAULT_BUFSIZE; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 608 | |
| 609 | if (!buffer) |
| 610 | goto out; |
| 611 | |
Oliver Neukum | 052fbc0 | 2009-04-20 17:24:49 +0200 | [diff] [blame] | 612 | while (buflen > 2) { |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 613 | if (buffer [1] != USB_DT_CS_INTERFACE) { |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 614 | dev_err(&intf->dev, "skipping garbage\n"); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 615 | goto next_desc; |
| 616 | } |
| 617 | |
| 618 | switch (buffer [2]) { |
| 619 | case USB_CDC_HEADER_TYPE: |
| 620 | break; |
| 621 | case USB_CDC_DMM_TYPE: |
| 622 | dmhd = (struct usb_cdc_dmm_desc *)buffer; |
| 623 | maxcom = le16_to_cpu(dmhd->wMaxCommand); |
| 624 | dev_dbg(&intf->dev, |
| 625 | "Finding maximum buffer length: %d", maxcom); |
| 626 | break; |
| 627 | default: |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 628 | dev_err(&intf->dev, |
| 629 | "Ignoring extra header, type %d, length %d\n", |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 630 | buffer[2], buffer[0]); |
| 631 | break; |
| 632 | } |
| 633 | next_desc: |
| 634 | buflen -= buffer[0]; |
| 635 | buffer += buffer[0]; |
| 636 | } |
| 637 | |
| 638 | rv = -ENOMEM; |
| 639 | desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL); |
| 640 | if (!desc) |
| 641 | goto out; |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 642 | mutex_init(&desc->lock); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 643 | spin_lock_init(&desc->iuspin); |
| 644 | init_waitqueue_head(&desc->wait); |
| 645 | desc->wMaxCommand = maxcom; |
Oliver Neukum | 052fbc0 | 2009-04-20 17:24:49 +0200 | [diff] [blame] | 646 | /* this will be expanded and needed in hardware endianness */ |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 647 | desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber); |
| 648 | desc->intf = intf; |
| 649 | INIT_WORK(&desc->rxwork, wdm_rxwork); |
| 650 | |
Oliver Neukum | 052fbc0 | 2009-04-20 17:24:49 +0200 | [diff] [blame] | 651 | rv = -EINVAL; |
| 652 | iface = intf->cur_altsetting; |
| 653 | if (iface->desc.bNumEndpoints != 1) |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 654 | goto err; |
Oliver Neukum | 052fbc0 | 2009-04-20 17:24:49 +0200 | [diff] [blame] | 655 | ep = &iface->endpoint[0].desc; |
| 656 | if (!ep || !usb_endpoint_is_int_in(ep)) |
| 657 | goto err; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 658 | |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 659 | desc->wMaxPacketSize = usb_endpoint_maxp(ep); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 660 | |
| 661 | desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); |
| 662 | if (!desc->orq) |
| 663 | goto err; |
| 664 | desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); |
| 665 | if (!desc->irq) |
| 666 | goto err; |
| 667 | |
| 668 | desc->validity = usb_alloc_urb(0, GFP_KERNEL); |
| 669 | if (!desc->validity) |
| 670 | goto err; |
| 671 | |
| 672 | desc->response = usb_alloc_urb(0, GFP_KERNEL); |
| 673 | if (!desc->response) |
| 674 | goto err; |
| 675 | |
| 676 | desc->command = usb_alloc_urb(0, GFP_KERNEL); |
| 677 | if (!desc->command) |
| 678 | goto err; |
| 679 | |
| 680 | desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL); |
| 681 | if (!desc->ubuf) |
| 682 | goto err; |
| 683 | |
Bjørn Mork | 8457d99 | 2012-01-16 15:12:00 +0100 | [diff] [blame] | 684 | desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 685 | if (!desc->sbuf) |
| 686 | goto err; |
| 687 | |
Bjørn Mork | 8457d99 | 2012-01-16 15:12:00 +0100 | [diff] [blame] | 688 | desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 689 | if (!desc->inbuf) |
Bjørn Mork | 8457d99 | 2012-01-16 15:12:00 +0100 | [diff] [blame] | 690 | goto err; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 691 | |
| 692 | usb_fill_int_urb( |
| 693 | desc->validity, |
| 694 | interface_to_usbdev(intf), |
| 695 | usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress), |
| 696 | desc->sbuf, |
| 697 | desc->wMaxPacketSize, |
| 698 | wdm_int_callback, |
| 699 | desc, |
| 700 | ep->bInterval |
| 701 | ); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 702 | |
Bjørn Mork | 19b85b3 | 2012-01-16 15:11:58 +0100 | [diff] [blame] | 703 | desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE); |
| 704 | desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE; |
| 705 | desc->irq->wValue = 0; |
| 706 | desc->irq->wIndex = desc->inum; |
| 707 | desc->irq->wLength = cpu_to_le16(desc->wMaxCommand); |
| 708 | |
| 709 | usb_fill_control_urb( |
| 710 | desc->response, |
Bjørn Mork | 8143a89 | 2012-01-16 15:12:01 +0100 | [diff] [blame] | 711 | interface_to_usbdev(intf), |
Bjørn Mork | 19b85b3 | 2012-01-16 15:11:58 +0100 | [diff] [blame] | 712 | /* using common endpoint 0 */ |
| 713 | usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0), |
| 714 | (unsigned char *)desc->irq, |
| 715 | desc->inbuf, |
| 716 | desc->wMaxCommand, |
| 717 | wdm_in_callback, |
| 718 | desc |
| 719 | ); |
Bjørn Mork | 19b85b3 | 2012-01-16 15:11:58 +0100 | [diff] [blame] | 720 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 721 | usb_set_intfdata(intf, desc); |
| 722 | rv = usb_register_dev(intf, &wdm_class); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 723 | if (rv < 0) |
Bjørn Mork | 8457d99 | 2012-01-16 15:12:00 +0100 | [diff] [blame] | 724 | goto err2; |
Oliver Neukum | 052fbc0 | 2009-04-20 17:24:49 +0200 | [diff] [blame] | 725 | else |
Bjørn Mork | 820c629 | 2012-01-20 04:17:25 +0100 | [diff] [blame^] | 726 | 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] | 727 | out: |
| 728 | return rv; |
| 729 | err2: |
Bjørn Mork | 8457d99 | 2012-01-16 15:12:00 +0100 | [diff] [blame] | 730 | usb_set_intfdata(intf, NULL); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 731 | err: |
| 732 | free_urbs(desc); |
Bjørn Mork | 8457d99 | 2012-01-16 15:12:00 +0100 | [diff] [blame] | 733 | kfree(desc->inbuf); |
| 734 | kfree(desc->sbuf); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 735 | kfree(desc->ubuf); |
| 736 | kfree(desc->orq); |
| 737 | kfree(desc->irq); |
| 738 | kfree(desc); |
| 739 | return rv; |
| 740 | } |
| 741 | |
| 742 | static void wdm_disconnect(struct usb_interface *intf) |
| 743 | { |
| 744 | struct wdm_device *desc; |
| 745 | unsigned long flags; |
| 746 | |
| 747 | usb_deregister_dev(intf, &wdm_class); |
| 748 | mutex_lock(&wdm_mutex); |
| 749 | desc = usb_get_intfdata(intf); |
| 750 | |
| 751 | /* the spinlock makes sure no new urbs are generated in the callbacks */ |
| 752 | spin_lock_irqsave(&desc->iuspin, flags); |
| 753 | set_bit(WDM_DISCONNECTING, &desc->flags); |
| 754 | set_bit(WDM_READ, &desc->flags); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 755 | /* to terminate pending flushes */ |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 756 | clear_bit(WDM_IN_USE, &desc->flags); |
| 757 | spin_unlock_irqrestore(&desc->iuspin, flags); |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 758 | mutex_lock(&desc->lock); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 759 | kill_urbs(desc); |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 760 | cancel_work_sync(&desc->rxwork); |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 761 | mutex_unlock(&desc->lock); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 762 | wake_up_all(&desc->wait); |
| 763 | if (!desc->count) |
| 764 | cleanup(desc); |
| 765 | mutex_unlock(&wdm_mutex); |
| 766 | } |
| 767 | |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 768 | #ifdef CONFIG_PM |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 769 | static int wdm_suspend(struct usb_interface *intf, pm_message_t message) |
| 770 | { |
| 771 | struct wdm_device *desc = usb_get_intfdata(intf); |
| 772 | int rv = 0; |
| 773 | |
| 774 | dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor); |
| 775 | |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 776 | /* if this is an autosuspend the caller does the locking */ |
Alan Stern | 5b1b0b8 | 2011-08-19 23:49:48 +0200 | [diff] [blame] | 777 | if (!PMSG_IS_AUTO(message)) |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 778 | mutex_lock(&desc->lock); |
Oliver Neukum | 62e6685 | 2010-02-27 20:56:22 +0100 | [diff] [blame] | 779 | spin_lock_irq(&desc->iuspin); |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 780 | |
Alan Stern | 5b1b0b8 | 2011-08-19 23:49:48 +0200 | [diff] [blame] | 781 | if (PMSG_IS_AUTO(message) && |
Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 782 | (test_bit(WDM_IN_USE, &desc->flags) |
| 783 | || test_bit(WDM_RESPONDING, &desc->flags))) { |
Oliver Neukum | 62e6685 | 2010-02-27 20:56:22 +0100 | [diff] [blame] | 784 | spin_unlock_irq(&desc->iuspin); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 785 | rv = -EBUSY; |
| 786 | } else { |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 787 | |
Oliver Neukum | beb1d35 | 2010-02-27 20:55:52 +0100 | [diff] [blame] | 788 | set_bit(WDM_SUSPENDING, &desc->flags); |
Oliver Neukum | 62e6685 | 2010-02-27 20:56:22 +0100 | [diff] [blame] | 789 | spin_unlock_irq(&desc->iuspin); |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 790 | /* callback submits work - order is essential */ |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 791 | kill_urbs(desc); |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 792 | cancel_work_sync(&desc->rxwork); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 793 | } |
Alan Stern | 5b1b0b8 | 2011-08-19 23:49:48 +0200 | [diff] [blame] | 794 | if (!PMSG_IS_AUTO(message)) |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 795 | mutex_unlock(&desc->lock); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 796 | |
| 797 | return rv; |
| 798 | } |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 799 | #endif |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 800 | |
| 801 | static int recover_from_urb_loss(struct wdm_device *desc) |
| 802 | { |
| 803 | int rv = 0; |
| 804 | |
| 805 | if (desc->count) { |
| 806 | rv = usb_submit_urb(desc->validity, GFP_NOIO); |
| 807 | if (rv < 0) |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 808 | dev_err(&desc->intf->dev, |
| 809 | "Error resume submitting int urb - %d\n", rv); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 810 | } |
| 811 | return rv; |
| 812 | } |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 813 | |
| 814 | #ifdef CONFIG_PM |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 815 | static int wdm_resume(struct usb_interface *intf) |
| 816 | { |
| 817 | struct wdm_device *desc = usb_get_intfdata(intf); |
| 818 | int rv; |
| 819 | |
| 820 | dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor); |
Oliver Neukum | 338124c | 2010-02-27 20:57:12 +0100 | [diff] [blame] | 821 | |
Oliver Neukum | beb1d35 | 2010-02-27 20:55:52 +0100 | [diff] [blame] | 822 | clear_bit(WDM_SUSPENDING, &desc->flags); |
Oliver Neukum | 62e6685 | 2010-02-27 20:56:22 +0100 | [diff] [blame] | 823 | rv = recover_from_urb_loss(desc); |
Oliver Neukum | 338124c | 2010-02-27 20:57:12 +0100 | [diff] [blame] | 824 | |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 825 | return rv; |
| 826 | } |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 827 | #endif |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 828 | |
| 829 | static int wdm_pre_reset(struct usb_interface *intf) |
| 830 | { |
| 831 | struct wdm_device *desc = usb_get_intfdata(intf); |
| 832 | |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 833 | mutex_lock(&desc->lock); |
Oliver Neukum | d771d8a | 2011-04-29 14:12:21 +0200 | [diff] [blame] | 834 | kill_urbs(desc); |
| 835 | |
| 836 | /* |
| 837 | * we notify everybody using poll of |
| 838 | * an exceptional situation |
| 839 | * must be done before recovery lest a spontaneous |
| 840 | * message from the device is lost |
| 841 | */ |
| 842 | spin_lock_irq(&desc->iuspin); |
| 843 | desc->rerr = -EINTR; |
| 844 | spin_unlock_irq(&desc->iuspin); |
| 845 | wake_up_all(&desc->wait); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 846 | return 0; |
| 847 | } |
| 848 | |
| 849 | static int wdm_post_reset(struct usb_interface *intf) |
| 850 | { |
| 851 | struct wdm_device *desc = usb_get_intfdata(intf); |
| 852 | int rv; |
| 853 | |
| 854 | rv = recover_from_urb_loss(desc); |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 855 | mutex_unlock(&desc->lock); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 856 | return 0; |
| 857 | } |
| 858 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 859 | static struct usb_driver wdm_driver = { |
| 860 | .name = "cdc_wdm", |
| 861 | .probe = wdm_probe, |
| 862 | .disconnect = wdm_disconnect, |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 863 | #ifdef CONFIG_PM |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 864 | .suspend = wdm_suspend, |
| 865 | .resume = wdm_resume, |
| 866 | .reset_resume = wdm_resume, |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 867 | #endif |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 868 | .pre_reset = wdm_pre_reset, |
| 869 | .post_reset = wdm_post_reset, |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 870 | .id_table = wdm_ids, |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 871 | .supports_autosuspend = 1, |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 872 | }; |
| 873 | |
Greg Kroah-Hartman | 65db430 | 2011-11-18 09:34:02 -0800 | [diff] [blame] | 874 | module_usb_driver(wdm_driver); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 875 | |
| 876 | MODULE_AUTHOR(DRIVER_AUTHOR); |
Oliver Neukum | 87d65e5 | 2008-06-19 14:20:18 +0200 | [diff] [blame] | 877 | MODULE_DESCRIPTION(DRIVER_DESC); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 878 | MODULE_LICENSE("GPL"); |