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