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