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