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