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