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