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