Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 2 | /* |
| 3 | * cdc-wdm.c |
| 4 | * |
| 5 | * This driver supports USB CDC WCM Device Management. |
| 6 | * |
Oliver Neukum | 052fbc0 | 2009-04-20 17:24:49 +0200 | [diff] [blame] | 7 | * Copyright (c) 2007-2009 Oliver Neukum |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 8 | * |
| 9 | * Some code taken from cdc-acm.c |
| 10 | * |
| 11 | * Released under the GPLv2. |
| 12 | * |
| 13 | * Many thanks to Carl Nordbeck |
| 14 | */ |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/errno.h> |
Bjørn Mork | 3edce1c | 2013-03-17 21:00:06 +0100 | [diff] [blame] | 17 | #include <linux/ioctl.h> |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 18 | #include <linux/slab.h> |
| 19 | #include <linux/module.h> |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 20 | #include <linux/mutex.h> |
| 21 | #include <linux/uaccess.h> |
| 22 | #include <linux/bitops.h> |
| 23 | #include <linux/poll.h> |
| 24 | #include <linux/usb.h> |
| 25 | #include <linux/usb/cdc.h> |
| 26 | #include <asm/byteorder.h> |
| 27 | #include <asm/unaligned.h> |
Bjørn Mork | 3cc3615 | 2012-03-06 17:29:22 +0100 | [diff] [blame] | 28 | #include <linux/usb/cdc-wdm.h> |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 29 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 30 | #define DRIVER_AUTHOR "Oliver Neukum" |
Oliver Neukum | 87d65e5 | 2008-06-19 14:20:18 +0200 | [diff] [blame] | 31 | #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] | 32 | |
Németh Márton | 6ef4852 | 2010-01-10 15:33:45 +0100 | [diff] [blame] | 33 | static const struct usb_device_id wdm_ids[] = { |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 34 | { |
| 35 | .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS | |
| 36 | USB_DEVICE_ID_MATCH_INT_SUBCLASS, |
| 37 | .bInterfaceClass = USB_CLASS_COMM, |
| 38 | .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM |
| 39 | }, |
| 40 | { } |
| 41 | }; |
| 42 | |
Oliver Neukum | aa5380b | 2008-10-13 14:05:20 +0200 | [diff] [blame] | 43 | MODULE_DEVICE_TABLE (usb, wdm_ids); |
| 44 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 45 | #define WDM_MINOR_BASE 176 |
| 46 | |
| 47 | |
| 48 | #define WDM_IN_USE 1 |
| 49 | #define WDM_DISCONNECTING 2 |
| 50 | #define WDM_RESULT 3 |
| 51 | #define WDM_READ 4 |
| 52 | #define WDM_INT_STALL 5 |
| 53 | #define WDM_POLL_RUNNING 6 |
Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 54 | #define WDM_RESPONDING 7 |
Oliver Neukum | beb1d35 | 2010-02-27 20:55:52 +0100 | [diff] [blame] | 55 | #define WDM_SUSPENDING 8 |
Bjørn Mork | 8804420 | 2012-02-10 09:44:08 +0100 | [diff] [blame] | 56 | #define WDM_RESETTING 9 |
Oliver Neukum | c0f5ece | 2013-03-12 14:52:42 +0100 | [diff] [blame] | 57 | #define WDM_OVERFLOW 10 |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 58 | |
| 59 | #define WDM_MAX 16 |
| 60 | |
Bjørn Mork | 7e3054a | 2012-01-20 01:49:57 +0100 | [diff] [blame] | 61 | /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */ |
| 62 | #define WDM_DEFAULT_BUFSIZE 256 |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 63 | |
| 64 | static DEFINE_MUTEX(wdm_mutex); |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 65 | static DEFINE_SPINLOCK(wdm_device_list_lock); |
| 66 | static LIST_HEAD(wdm_device_list); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 67 | |
| 68 | /* --- method tables --- */ |
| 69 | |
| 70 | struct wdm_device { |
| 71 | u8 *inbuf; /* buffer for response */ |
| 72 | u8 *outbuf; /* buffer for command */ |
| 73 | u8 *sbuf; /* buffer for status */ |
| 74 | u8 *ubuf; /* buffer for copy to user space */ |
| 75 | |
| 76 | struct urb *command; |
| 77 | struct urb *response; |
| 78 | struct urb *validity; |
| 79 | struct usb_interface *intf; |
| 80 | struct usb_ctrlrequest *orq; |
| 81 | struct usb_ctrlrequest *irq; |
| 82 | spinlock_t iuspin; |
| 83 | |
| 84 | unsigned long flags; |
| 85 | u16 bufsize; |
| 86 | u16 wMaxCommand; |
| 87 | u16 wMaxPacketSize; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 88 | __le16 inum; |
| 89 | int reslength; |
| 90 | int length; |
| 91 | int read; |
| 92 | int count; |
| 93 | dma_addr_t shandle; |
| 94 | dma_addr_t ihandle; |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 95 | struct mutex wlock; |
| 96 | struct mutex rlock; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 97 | wait_queue_head_t wait; |
| 98 | struct work_struct rxwork; |
Sebastian Andrzej Siewior | 2df6948 | 2018-06-14 18:36:46 +0200 | [diff] [blame] | 99 | struct work_struct service_outs_intr; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 100 | int werr; |
| 101 | int rerr; |
Greg Suarez | 73e0686 | 2013-10-29 10:29:10 -0700 | [diff] [blame] | 102 | int resp_count; |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 103 | |
| 104 | struct list_head device_list; |
Bjørn Mork | 3cc3615 | 2012-03-06 17:29:22 +0100 | [diff] [blame] | 105 | int (*manage_power)(struct usb_interface *, int); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | static struct usb_driver wdm_driver; |
| 109 | |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 110 | /* return intfdata if we own the interface, else look up intf in the list */ |
| 111 | static struct wdm_device *wdm_find_device(struct usb_interface *intf) |
| 112 | { |
Bjørn Mork | 6a44886 | 2012-09-10 22:17:34 +0200 | [diff] [blame] | 113 | struct wdm_device *desc; |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 114 | |
| 115 | spin_lock(&wdm_device_list_lock); |
| 116 | list_for_each_entry(desc, &wdm_device_list, device_list) |
| 117 | if (desc->intf == intf) |
Bjørn Mork | 6a44886 | 2012-09-10 22:17:34 +0200 | [diff] [blame] | 118 | goto found; |
| 119 | desc = NULL; |
| 120 | found: |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 121 | spin_unlock(&wdm_device_list_lock); |
| 122 | |
| 123 | return desc; |
| 124 | } |
| 125 | |
| 126 | static struct wdm_device *wdm_find_device_by_minor(int minor) |
| 127 | { |
Bjørn Mork | 6a44886 | 2012-09-10 22:17:34 +0200 | [diff] [blame] | 128 | struct wdm_device *desc; |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 129 | |
| 130 | spin_lock(&wdm_device_list_lock); |
| 131 | list_for_each_entry(desc, &wdm_device_list, device_list) |
| 132 | if (desc->intf->minor == minor) |
Bjørn Mork | 6a44886 | 2012-09-10 22:17:34 +0200 | [diff] [blame] | 133 | goto found; |
| 134 | desc = NULL; |
| 135 | found: |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 136 | spin_unlock(&wdm_device_list_lock); |
| 137 | |
| 138 | return desc; |
| 139 | } |
| 140 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 141 | /* --- callbacks --- */ |
| 142 | static void wdm_out_callback(struct urb *urb) |
| 143 | { |
| 144 | struct wdm_device *desc; |
Sebastian Andrzej Siewior | 579b9cca | 2018-06-25 00:08:35 +0200 | [diff] [blame] | 145 | unsigned long flags; |
| 146 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 147 | desc = urb->context; |
Sebastian Andrzej Siewior | 579b9cca | 2018-06-25 00:08:35 +0200 | [diff] [blame] | 148 | spin_lock_irqsave(&desc->iuspin, flags); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 149 | desc->werr = urb->status; |
Sebastian Andrzej Siewior | 579b9cca | 2018-06-25 00:08:35 +0200 | [diff] [blame] | 150 | spin_unlock_irqrestore(&desc->iuspin, flags); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 151 | kfree(desc->outbuf); |
Oliver Neukum | 5c22837 | 2012-04-26 21:59:10 +0200 | [diff] [blame] | 152 | desc->outbuf = NULL; |
| 153 | clear_bit(WDM_IN_USE, &desc->flags); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 154 | wake_up(&desc->wait); |
| 155 | } |
| 156 | |
| 157 | static void wdm_in_callback(struct urb *urb) |
| 158 | { |
Sebastian Andrzej Siewior | 579b9cca | 2018-06-25 00:08:35 +0200 | [diff] [blame] | 159 | unsigned long flags; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 160 | struct wdm_device *desc = urb->context; |
| 161 | int status = urb->status; |
Oliver Neukum | c0f5ece | 2013-03-12 14:52:42 +0100 | [diff] [blame] | 162 | int length = urb->actual_length; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 163 | |
Sebastian Andrzej Siewior | 579b9cca | 2018-06-25 00:08:35 +0200 | [diff] [blame] | 164 | spin_lock_irqsave(&desc->iuspin, flags); |
Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 165 | clear_bit(WDM_RESPONDING, &desc->flags); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 166 | |
| 167 | if (status) { |
| 168 | switch (status) { |
| 169 | case -ENOENT: |
| 170 | dev_dbg(&desc->intf->dev, |
Oliver Neukum | ce8bb34 | 2016-08-16 15:12:22 +0200 | [diff] [blame] | 171 | "nonzero urb status received: -ENOENT\n"); |
Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 172 | goto skip_error; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 173 | case -ECONNRESET: |
| 174 | dev_dbg(&desc->intf->dev, |
Oliver Neukum | ce8bb34 | 2016-08-16 15:12:22 +0200 | [diff] [blame] | 175 | "nonzero urb status received: -ECONNRESET\n"); |
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 -ESHUTDOWN: |
| 178 | dev_dbg(&desc->intf->dev, |
Oliver Neukum | ce8bb34 | 2016-08-16 15:12:22 +0200 | [diff] [blame] | 179 | "nonzero urb status received: -ESHUTDOWN\n"); |
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 -EPIPE: |
Bjørn Mork | 1944581 | 2017-04-21 10:01:29 +0200 | [diff] [blame] | 182 | dev_err(&desc->intf->dev, |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 183 | "nonzero urb status received: -EPIPE\n"); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 184 | break; |
| 185 | default: |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 186 | dev_err(&desc->intf->dev, |
| 187 | "Unexpected error %d\n", status); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 188 | break; |
| 189 | } |
| 190 | } |
| 191 | |
Robert Foss | c1da59d | 2016-08-09 10:54:52 -0400 | [diff] [blame] | 192 | /* |
| 193 | * only set a new error if there is no previous error. |
| 194 | * Errors are only cleared during read/open |
Bjørn Mork | 8fec935 | 2017-09-22 22:18:18 +0200 | [diff] [blame] | 195 | * Avoid propagating -EPIPE (stall) to userspace since it is |
| 196 | * better handled as an empty read |
Robert Foss | c1da59d | 2016-08-09 10:54:52 -0400 | [diff] [blame] | 197 | */ |
Bjørn Mork | 8fec935 | 2017-09-22 22:18:18 +0200 | [diff] [blame] | 198 | if (desc->rerr == 0 && status != -EPIPE) |
Robert Foss | c1da59d | 2016-08-09 10:54:52 -0400 | [diff] [blame] | 199 | desc->rerr = status; |
| 200 | |
Oliver Neukum | c0f5ece | 2013-03-12 14:52:42 +0100 | [diff] [blame] | 201 | if (length + desc->length > desc->wMaxCommand) { |
| 202 | /* The buffer would overflow */ |
| 203 | set_bit(WDM_OVERFLOW, &desc->flags); |
| 204 | } else { |
| 205 | /* we may already be in overflow */ |
| 206 | if (!test_bit(WDM_OVERFLOW, &desc->flags)) { |
| 207 | memmove(desc->ubuf + desc->length, desc->inbuf, length); |
| 208 | desc->length += length; |
| 209 | desc->reslength = length; |
| 210 | } |
| 211 | } |
Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 212 | skip_error: |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 213 | |
Robert Foss | c1da59d | 2016-08-09 10:54:52 -0400 | [diff] [blame] | 214 | if (desc->rerr) { |
| 215 | /* |
| 216 | * Since there was an error, userspace may decide to not read |
| 217 | * any data after poll'ing. |
| 218 | * We should respond to further attempts from the device to send |
| 219 | * data, so that we can get unstuck. |
| 220 | */ |
Sebastian Andrzej Siewior | 2df6948 | 2018-06-14 18:36:46 +0200 | [diff] [blame] | 221 | schedule_work(&desc->service_outs_intr); |
| 222 | } else { |
| 223 | set_bit(WDM_READ, &desc->flags); |
| 224 | wake_up(&desc->wait); |
Robert Foss | c1da59d | 2016-08-09 10:54:52 -0400 | [diff] [blame] | 225 | } |
Sebastian Andrzej Siewior | 579b9cca | 2018-06-25 00:08:35 +0200 | [diff] [blame] | 226 | spin_unlock_irqrestore(&desc->iuspin, flags); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static void wdm_int_callback(struct urb *urb) |
| 230 | { |
Sebastian Andrzej Siewior | 579b9cca | 2018-06-25 00:08:35 +0200 | [diff] [blame] | 231 | unsigned long flags; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 232 | int rv = 0; |
Oliver Neukum | 6dd433e | 2013-08-06 14:22:59 +0200 | [diff] [blame] | 233 | int responding; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 234 | int status = urb->status; |
| 235 | struct wdm_device *desc; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 236 | struct usb_cdc_notification *dr; |
| 237 | |
| 238 | desc = urb->context; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 239 | dr = (struct usb_cdc_notification *)desc->sbuf; |
| 240 | |
| 241 | if (status) { |
| 242 | switch (status) { |
| 243 | case -ESHUTDOWN: |
| 244 | case -ENOENT: |
| 245 | case -ECONNRESET: |
| 246 | return; /* unplug */ |
| 247 | case -EPIPE: |
| 248 | set_bit(WDM_INT_STALL, &desc->flags); |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 249 | dev_err(&desc->intf->dev, "Stall on int endpoint\n"); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 250 | goto sw; /* halt is cleared in work */ |
| 251 | default: |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 252 | dev_err(&desc->intf->dev, |
| 253 | "nonzero urb status received: %d\n", status); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 254 | break; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | if (urb->actual_length < sizeof(struct usb_cdc_notification)) { |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 259 | dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n", |
| 260 | urb->actual_length); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 261 | goto exit; |
| 262 | } |
| 263 | |
| 264 | switch (dr->bNotificationType) { |
| 265 | case USB_CDC_NOTIFY_RESPONSE_AVAILABLE: |
| 266 | dev_dbg(&desc->intf->dev, |
Oliver Neukum | ce8bb34 | 2016-08-16 15:12:22 +0200 | [diff] [blame] | 267 | "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d\n", |
Oliver Neukum | 323ece5 | 2015-03-20 14:29:34 +0100 | [diff] [blame] | 268 | le16_to_cpu(dr->wIndex), le16_to_cpu(dr->wLength)); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 269 | break; |
| 270 | |
| 271 | case USB_CDC_NOTIFY_NETWORK_CONNECTION: |
| 272 | |
| 273 | dev_dbg(&desc->intf->dev, |
Oliver Neukum | ce8bb34 | 2016-08-16 15:12:22 +0200 | [diff] [blame] | 274 | "NOTIFY_NETWORK_CONNECTION %s network\n", |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 275 | dr->wValue ? "connected to" : "disconnected from"); |
| 276 | goto exit; |
Bjørn Mork | 9983d6d | 2013-10-29 09:52:57 +0100 | [diff] [blame] | 277 | case USB_CDC_NOTIFY_SPEED_CHANGE: |
Oliver Neukum | ce8bb34 | 2016-08-16 15:12:22 +0200 | [diff] [blame] | 278 | dev_dbg(&desc->intf->dev, "SPEED_CHANGE received (len %u)\n", |
Bjørn Mork | 9983d6d | 2013-10-29 09:52:57 +0100 | [diff] [blame] | 279 | urb->actual_length); |
| 280 | goto exit; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 281 | default: |
| 282 | clear_bit(WDM_POLL_RUNNING, &desc->flags); |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 283 | dev_err(&desc->intf->dev, |
| 284 | "unknown notification %d received: index %d len %d\n", |
Oliver Neukum | 323ece5 | 2015-03-20 14:29:34 +0100 | [diff] [blame] | 285 | dr->bNotificationType, |
| 286 | le16_to_cpu(dr->wIndex), |
| 287 | le16_to_cpu(dr->wLength)); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 288 | goto exit; |
| 289 | } |
| 290 | |
Sebastian Andrzej Siewior | 579b9cca | 2018-06-25 00:08:35 +0200 | [diff] [blame] | 291 | spin_lock_irqsave(&desc->iuspin, flags); |
Oliver Neukum | 6dd433e | 2013-08-06 14:22:59 +0200 | [diff] [blame] | 292 | responding = test_and_set_bit(WDM_RESPONDING, &desc->flags); |
Greg Suarez | 73e0686 | 2013-10-29 10:29:10 -0700 | [diff] [blame] | 293 | if (!desc->resp_count++ && !responding |
| 294 | && !test_bit(WDM_DISCONNECTING, &desc->flags) |
Oliver Neukum | beb1d35 | 2010-02-27 20:55:52 +0100 | [diff] [blame] | 295 | && !test_bit(WDM_SUSPENDING, &desc->flags)) { |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 296 | rv = usb_submit_urb(desc->response, GFP_ATOMIC); |
Oliver Neukum | ce8bb34 | 2016-08-16 15:12:22 +0200 | [diff] [blame] | 297 | dev_dbg(&desc->intf->dev, "submit response URB %d\n", rv); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 298 | } |
Sebastian Andrzej Siewior | 579b9cca | 2018-06-25 00:08:35 +0200 | [diff] [blame] | 299 | spin_unlock_irqrestore(&desc->iuspin, flags); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 300 | if (rv < 0) { |
Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 301 | clear_bit(WDM_RESPONDING, &desc->flags); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 302 | if (rv == -EPERM) |
| 303 | return; |
| 304 | if (rv == -ENOMEM) { |
| 305 | sw: |
| 306 | rv = schedule_work(&desc->rxwork); |
| 307 | if (rv) |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 308 | dev_err(&desc->intf->dev, |
| 309 | "Cannot schedule work\n"); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | exit: |
| 313 | rv = usb_submit_urb(urb, GFP_ATOMIC); |
| 314 | if (rv) |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 315 | dev_err(&desc->intf->dev, |
| 316 | "%s - usb_submit_urb failed with result %d\n", |
| 317 | __func__, rv); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 318 | |
| 319 | } |
| 320 | |
| 321 | static void kill_urbs(struct wdm_device *desc) |
| 322 | { |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 323 | /* the order here is essential */ |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 324 | usb_kill_urb(desc->command); |
| 325 | usb_kill_urb(desc->validity); |
| 326 | usb_kill_urb(desc->response); |
| 327 | } |
| 328 | |
| 329 | static void free_urbs(struct wdm_device *desc) |
| 330 | { |
| 331 | usb_free_urb(desc->validity); |
| 332 | usb_free_urb(desc->response); |
| 333 | usb_free_urb(desc->command); |
| 334 | } |
| 335 | |
| 336 | static void cleanup(struct wdm_device *desc) |
| 337 | { |
Bjørn Mork | 8457d99 | 2012-01-16 15:12:00 +0100 | [diff] [blame] | 338 | kfree(desc->sbuf); |
| 339 | kfree(desc->inbuf); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 340 | kfree(desc->orq); |
| 341 | kfree(desc->irq); |
| 342 | kfree(desc->ubuf); |
| 343 | free_urbs(desc); |
| 344 | kfree(desc); |
| 345 | } |
| 346 | |
| 347 | static ssize_t wdm_write |
| 348 | (struct file *file, const char __user *buffer, size_t count, loff_t *ppos) |
| 349 | { |
| 350 | u8 *buf; |
| 351 | int rv = -EMSGSIZE, r, we; |
| 352 | struct wdm_device *desc = file->private_data; |
| 353 | struct usb_ctrlrequest *req; |
| 354 | |
| 355 | if (count > desc->wMaxCommand) |
| 356 | count = desc->wMaxCommand; |
| 357 | |
| 358 | spin_lock_irq(&desc->iuspin); |
| 359 | we = desc->werr; |
| 360 | desc->werr = 0; |
| 361 | spin_unlock_irq(&desc->iuspin); |
| 362 | if (we < 0) |
Oliver Neukum | 76cb03e | 2015-03-20 14:28:56 +0100 | [diff] [blame] | 363 | return usb_translate_errors(we); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 364 | |
Geliang Tang | 64b9533 | 2017-05-08 23:14:39 +0800 | [diff] [blame] | 365 | buf = memdup_user(buffer, count); |
| 366 | if (IS_ERR(buf)) |
| 367 | return PTR_ERR(buf); |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 368 | |
| 369 | /* concurrent writes and disconnect */ |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 370 | r = mutex_lock_interruptible(&desc->wlock); |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 371 | rv = -ERESTARTSYS; |
Oliver Neukum | 28965e1 | 2015-03-20 14:29:18 +0100 | [diff] [blame] | 372 | if (r) |
| 373 | goto out_free_mem; |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 374 | |
| 375 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 376 | rv = -ENODEV; |
Oliver Neukum | 28965e1 | 2015-03-20 14:29:18 +0100 | [diff] [blame] | 377 | goto out_free_mem_lock; |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 378 | } |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 379 | |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 380 | r = usb_autopm_get_interface(desc->intf); |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 381 | if (r < 0) { |
Oliver Neukum | 12a98b2 | 2012-04-30 09:57:31 +0200 | [diff] [blame] | 382 | rv = usb_translate_errors(r); |
Oliver Neukum | 28965e1 | 2015-03-20 14:29:18 +0100 | [diff] [blame] | 383 | goto out_free_mem_lock; |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 384 | } |
Oliver Neukum | 7f1dc313d | 2009-09-09 10:12:48 +0200 | [diff] [blame] | 385 | |
David Sterba | 0cdfb81 | 2010-12-27 18:49:58 +0100 | [diff] [blame] | 386 | if (!(file->f_flags & O_NONBLOCK)) |
Oliver Neukum | 7f1dc313d | 2009-09-09 10:12:48 +0200 | [diff] [blame] | 387 | r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE, |
| 388 | &desc->flags)); |
| 389 | else |
| 390 | if (test_bit(WDM_IN_USE, &desc->flags)) |
| 391 | r = -EAGAIN; |
Bjørn Mork | 8804420 | 2012-02-10 09:44:08 +0100 | [diff] [blame] | 392 | |
| 393 | if (test_bit(WDM_RESETTING, &desc->flags)) |
| 394 | r = -EIO; |
| 395 | |
Oliver Neukum | 860e41a | 2010-02-27 20:54:24 +0100 | [diff] [blame] | 396 | if (r < 0) { |
Oliver Neukum | 12a98b2 | 2012-04-30 09:57:31 +0200 | [diff] [blame] | 397 | rv = r; |
Oliver Neukum | 28965e1 | 2015-03-20 14:29:18 +0100 | [diff] [blame] | 398 | goto out_free_mem_pm; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | req = desc->orq; |
| 402 | usb_fill_control_urb( |
| 403 | desc->command, |
| 404 | interface_to_usbdev(desc->intf), |
| 405 | /* using common endpoint 0 */ |
| 406 | usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0), |
| 407 | (unsigned char *)req, |
| 408 | buf, |
| 409 | count, |
| 410 | wdm_out_callback, |
| 411 | desc |
| 412 | ); |
| 413 | |
| 414 | req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS | |
| 415 | USB_RECIP_INTERFACE); |
| 416 | req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND; |
| 417 | req->wValue = 0; |
Oliver Neukum | 323ece5 | 2015-03-20 14:29:34 +0100 | [diff] [blame] | 418 | req->wIndex = desc->inum; /* already converted */ |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 419 | req->wLength = cpu_to_le16(count); |
| 420 | set_bit(WDM_IN_USE, &desc->flags); |
Oliver Neukum | 5c22837 | 2012-04-26 21:59:10 +0200 | [diff] [blame] | 421 | desc->outbuf = buf; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 422 | |
| 423 | rv = usb_submit_urb(desc->command, GFP_KERNEL); |
| 424 | if (rv < 0) { |
Oliver Neukum | 5c22837 | 2012-04-26 21:59:10 +0200 | [diff] [blame] | 425 | desc->outbuf = NULL; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 426 | clear_bit(WDM_IN_USE, &desc->flags); |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 427 | dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv); |
Oliver Neukum | 12a98b2 | 2012-04-30 09:57:31 +0200 | [diff] [blame] | 428 | rv = usb_translate_errors(rv); |
Oliver Neukum | 28965e1 | 2015-03-20 14:29:18 +0100 | [diff] [blame] | 429 | goto out_free_mem_pm; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 430 | } else { |
Oliver Neukum | ce8bb34 | 2016-08-16 15:12:22 +0200 | [diff] [blame] | 431 | dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d\n", |
Oliver Neukum | 323ece5 | 2015-03-20 14:29:34 +0100 | [diff] [blame] | 432 | le16_to_cpu(req->wIndex)); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 433 | } |
Oliver Neukum | 28965e1 | 2015-03-20 14:29:18 +0100 | [diff] [blame] | 434 | |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 435 | usb_autopm_put_interface(desc->intf); |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 436 | mutex_unlock(&desc->wlock); |
Geliang Tang | 64b9533 | 2017-05-08 23:14:39 +0800 | [diff] [blame] | 437 | return count; |
Oliver Neukum | 28965e1 | 2015-03-20 14:29:18 +0100 | [diff] [blame] | 438 | |
| 439 | out_free_mem_pm: |
| 440 | usb_autopm_put_interface(desc->intf); |
| 441 | out_free_mem_lock: |
| 442 | mutex_unlock(&desc->wlock); |
| 443 | out_free_mem: |
| 444 | kfree(buf); |
| 445 | return rv; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 446 | } |
| 447 | |
Bjørn Mork | 8dd5cd5 | 2013-12-20 14:07:24 +0100 | [diff] [blame] | 448 | /* |
Robert Foss | c1da59d | 2016-08-09 10:54:52 -0400 | [diff] [blame] | 449 | * Submit the read urb if resp_count is non-zero. |
Bjørn Mork | 8dd5cd5 | 2013-12-20 14:07:24 +0100 | [diff] [blame] | 450 | * |
| 451 | * Called with desc->iuspin locked |
| 452 | */ |
Robert Foss | c1da59d | 2016-08-09 10:54:52 -0400 | [diff] [blame] | 453 | static int service_outstanding_interrupt(struct wdm_device *desc) |
Bjørn Mork | 8dd5cd5 | 2013-12-20 14:07:24 +0100 | [diff] [blame] | 454 | { |
| 455 | int rv = 0; |
| 456 | |
Bjørn Mork | 8dd5cd5 | 2013-12-20 14:07:24 +0100 | [diff] [blame] | 457 | /* submit read urb only if the device is waiting for it */ |
Bjørn Mork | f563926 | 2014-01-12 21:48:53 +0100 | [diff] [blame] | 458 | if (!desc->resp_count || !--desc->resp_count) |
Bjørn Mork | 8dd5cd5 | 2013-12-20 14:07:24 +0100 | [diff] [blame] | 459 | goto out; |
| 460 | |
| 461 | set_bit(WDM_RESPONDING, &desc->flags); |
| 462 | spin_unlock_irq(&desc->iuspin); |
Sebastian Andrzej Siewior | e871db8 | 2018-09-11 10:00:44 +0200 | [diff] [blame] | 463 | rv = usb_submit_urb(desc->response, GFP_KERNEL); |
Bjørn Mork | 8dd5cd5 | 2013-12-20 14:07:24 +0100 | [diff] [blame] | 464 | spin_lock_irq(&desc->iuspin); |
| 465 | if (rv) { |
| 466 | dev_err(&desc->intf->dev, |
| 467 | "usb_submit_urb failed with result %d\n", rv); |
| 468 | |
| 469 | /* make sure the next notification trigger a submit */ |
| 470 | clear_bit(WDM_RESPONDING, &desc->flags); |
| 471 | desc->resp_count = 0; |
| 472 | } |
| 473 | out: |
| 474 | return rv; |
| 475 | } |
| 476 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 477 | static ssize_t wdm_read |
| 478 | (struct file *file, char __user *buffer, size_t count, loff_t *ppos) |
| 479 | { |
Ben Hutchings | 711c68b | 2012-02-12 06:00:41 +0000 | [diff] [blame] | 480 | int rv, cntr; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 481 | int i = 0; |
| 482 | struct wdm_device *desc = file->private_data; |
| 483 | |
| 484 | |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 485 | rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */ |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 486 | if (rv < 0) |
| 487 | return -ERESTARTSYS; |
| 488 | |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 489 | cntr = READ_ONCE(desc->length); |
Ben Hutchings | 711c68b | 2012-02-12 06:00:41 +0000 | [diff] [blame] | 490 | if (cntr == 0) { |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 491 | desc->read = 0; |
| 492 | retry: |
Oliver Neukum | 7f1dc313d | 2009-09-09 10:12:48 +0200 | [diff] [blame] | 493 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { |
| 494 | rv = -ENODEV; |
| 495 | goto err; |
| 496 | } |
Oliver Neukum | c0f5ece | 2013-03-12 14:52:42 +0100 | [diff] [blame] | 497 | if (test_bit(WDM_OVERFLOW, &desc->flags)) { |
| 498 | clear_bit(WDM_OVERFLOW, &desc->flags); |
| 499 | rv = -ENOBUFS; |
| 500 | goto err; |
| 501 | } |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 502 | i++; |
Oliver Neukum | 7f1dc313d | 2009-09-09 10:12:48 +0200 | [diff] [blame] | 503 | if (file->f_flags & O_NONBLOCK) { |
| 504 | if (!test_bit(WDM_READ, &desc->flags)) { |
Gustavo A. R. Silva | 53b7f7b | 2017-02-14 21:10:52 -0600 | [diff] [blame] | 505 | rv = -EAGAIN; |
Oliver Neukum | 7f1dc313d | 2009-09-09 10:12:48 +0200 | [diff] [blame] | 506 | goto err; |
| 507 | } |
| 508 | rv = 0; |
| 509 | } else { |
| 510 | rv = wait_event_interruptible(desc->wait, |
| 511 | test_bit(WDM_READ, &desc->flags)); |
| 512 | } |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 513 | |
Oliver Neukum | 7f1dc313d | 2009-09-09 10:12:48 +0200 | [diff] [blame] | 514 | /* may have happened while we slept */ |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 515 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { |
| 516 | rv = -ENODEV; |
| 517 | goto err; |
| 518 | } |
Bjørn Mork | 8804420 | 2012-02-10 09:44:08 +0100 | [diff] [blame] | 519 | if (test_bit(WDM_RESETTING, &desc->flags)) { |
| 520 | rv = -EIO; |
| 521 | goto err; |
| 522 | } |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 523 | usb_mark_last_busy(interface_to_usbdev(desc->intf)); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 524 | if (rv < 0) { |
| 525 | rv = -ERESTARTSYS; |
| 526 | goto err; |
| 527 | } |
| 528 | |
| 529 | spin_lock_irq(&desc->iuspin); |
| 530 | |
| 531 | if (desc->rerr) { /* read completed, error happened */ |
Oliver Neukum | 85e8a0b9 | 2015-03-23 14:34:43 +0100 | [diff] [blame] | 532 | rv = usb_translate_errors(desc->rerr); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 533 | desc->rerr = 0; |
| 534 | spin_unlock_irq(&desc->iuspin); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 535 | goto err; |
| 536 | } |
| 537 | /* |
| 538 | * recheck whether we've lost the race |
| 539 | * against the completion handler |
| 540 | */ |
| 541 | if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */ |
| 542 | spin_unlock_irq(&desc->iuspin); |
| 543 | goto retry; |
| 544 | } |
Oliver Neukum | c0f5ece | 2013-03-12 14:52:42 +0100 | [diff] [blame] | 545 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 546 | if (!desc->reslength) { /* zero length read */ |
Oliver Neukum | ce8bb34 | 2016-08-16 15:12:22 +0200 | [diff] [blame] | 547 | dev_dbg(&desc->intf->dev, "zero length - clearing WDM_READ\n"); |
Robert Foss | c1da59d | 2016-08-09 10:54:52 -0400 | [diff] [blame] | 548 | clear_bit(WDM_READ, &desc->flags); |
| 549 | rv = service_outstanding_interrupt(desc); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 550 | spin_unlock_irq(&desc->iuspin); |
Bjørn Mork | 8dd5cd5 | 2013-12-20 14:07:24 +0100 | [diff] [blame] | 551 | if (rv < 0) |
| 552 | goto err; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 553 | goto retry; |
| 554 | } |
Ben Hutchings | 711c68b | 2012-02-12 06:00:41 +0000 | [diff] [blame] | 555 | cntr = desc->length; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 556 | spin_unlock_irq(&desc->iuspin); |
| 557 | } |
| 558 | |
Ben Hutchings | 711c68b | 2012-02-12 06:00:41 +0000 | [diff] [blame] | 559 | if (cntr > count) |
| 560 | cntr = count; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 561 | rv = copy_to_user(buffer, desc->ubuf, cntr); |
| 562 | if (rv > 0) { |
| 563 | rv = -EFAULT; |
| 564 | goto err; |
| 565 | } |
| 566 | |
Ben Hutchings | 711c68b | 2012-02-12 06:00:41 +0000 | [diff] [blame] | 567 | spin_lock_irq(&desc->iuspin); |
| 568 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 569 | for (i = 0; i < desc->length - cntr; i++) |
| 570 | desc->ubuf[i] = desc->ubuf[i + cntr]; |
| 571 | |
| 572 | desc->length -= cntr; |
Oliver Neukum | 87d65e5 | 2008-06-19 14:20:18 +0200 | [diff] [blame] | 573 | /* in case we had outstanding data */ |
Robert Foss | c1da59d | 2016-08-09 10:54:52 -0400 | [diff] [blame] | 574 | if (!desc->length) { |
| 575 | clear_bit(WDM_READ, &desc->flags); |
| 576 | service_outstanding_interrupt(desc); |
| 577 | } |
Bjørn Mork | 8dd5cd5 | 2013-12-20 14:07:24 +0100 | [diff] [blame] | 578 | spin_unlock_irq(&desc->iuspin); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 579 | rv = cntr; |
| 580 | |
| 581 | err: |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 582 | mutex_unlock(&desc->rlock); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 583 | return rv; |
| 584 | } |
| 585 | |
| 586 | static int wdm_flush(struct file *file, fl_owner_t id) |
| 587 | { |
| 588 | struct wdm_device *desc = file->private_data; |
| 589 | |
| 590 | wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags)); |
Bjørn Mork | 6b0b79d | 2012-05-09 13:53:22 +0200 | [diff] [blame] | 591 | |
| 592 | /* cannot dereference desc->intf if WDM_DISCONNECTING */ |
| 593 | if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags)) |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 594 | dev_err(&desc->intf->dev, "Error in flush path: %d\n", |
| 595 | desc->werr); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 596 | |
Oliver Neukum | 24a85ba | 2012-04-27 14:23:54 +0200 | [diff] [blame] | 597 | return usb_translate_errors(desc->werr); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 598 | } |
| 599 | |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 600 | static __poll_t wdm_poll(struct file *file, struct poll_table_struct *wait) |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 601 | { |
| 602 | struct wdm_device *desc = file->private_data; |
| 603 | unsigned long flags; |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 604 | __poll_t mask = 0; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 605 | |
| 606 | spin_lock_irqsave(&desc->iuspin, flags); |
| 607 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 608 | mask = EPOLLHUP | EPOLLERR; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 609 | spin_unlock_irqrestore(&desc->iuspin, flags); |
| 610 | goto desc_out; |
| 611 | } |
| 612 | if (test_bit(WDM_READ, &desc->flags)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 613 | mask = EPOLLIN | EPOLLRDNORM; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 614 | if (desc->rerr || desc->werr) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 615 | mask |= EPOLLERR; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 616 | if (!test_bit(WDM_IN_USE, &desc->flags)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 617 | mask |= EPOLLOUT | EPOLLWRNORM; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 618 | spin_unlock_irqrestore(&desc->iuspin, flags); |
| 619 | |
| 620 | poll_wait(file, &desc->wait, wait); |
| 621 | |
| 622 | desc_out: |
| 623 | return mask; |
| 624 | } |
| 625 | |
| 626 | static int wdm_open(struct inode *inode, struct file *file) |
| 627 | { |
| 628 | int minor = iminor(inode); |
| 629 | int rv = -ENODEV; |
| 630 | struct usb_interface *intf; |
| 631 | struct wdm_device *desc; |
| 632 | |
| 633 | mutex_lock(&wdm_mutex); |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 634 | desc = wdm_find_device_by_minor(minor); |
| 635 | if (!desc) |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 636 | goto out; |
| 637 | |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 638 | intf = desc->intf; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 639 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) |
| 640 | goto out; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 641 | file->private_data = desc; |
| 642 | |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 643 | rv = usb_autopm_get_interface(desc->intf); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 644 | if (rv < 0) { |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 645 | dev_err(&desc->intf->dev, "Error autopm - %d\n", rv); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 646 | goto out; |
| 647 | } |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 648 | |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 649 | /* using write lock to protect desc->count */ |
| 650 | mutex_lock(&desc->wlock); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 651 | if (!desc->count++) { |
Oliver Neukum | d771d8a | 2011-04-29 14:12:21 +0200 | [diff] [blame] | 652 | desc->werr = 0; |
| 653 | desc->rerr = 0; |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 654 | rv = usb_submit_urb(desc->validity, GFP_KERNEL); |
| 655 | if (rv < 0) { |
| 656 | desc->count--; |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 657 | dev_err(&desc->intf->dev, |
| 658 | "Error submitting int urb - %d\n", rv); |
Oliver Neukum | 12a98b2 | 2012-04-30 09:57:31 +0200 | [diff] [blame] | 659 | rv = usb_translate_errors(rv); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 660 | } |
| 661 | } else { |
| 662 | rv = 0; |
| 663 | } |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 664 | mutex_unlock(&desc->wlock); |
Bjørn Mork | 3cc3615 | 2012-03-06 17:29:22 +0100 | [diff] [blame] | 665 | if (desc->count == 1) |
| 666 | desc->manage_power(intf, 1); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 667 | usb_autopm_put_interface(desc->intf); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 668 | out: |
| 669 | mutex_unlock(&wdm_mutex); |
| 670 | return rv; |
| 671 | } |
| 672 | |
| 673 | static int wdm_release(struct inode *inode, struct file *file) |
| 674 | { |
| 675 | struct wdm_device *desc = file->private_data; |
| 676 | |
| 677 | mutex_lock(&wdm_mutex); |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 678 | |
| 679 | /* using write lock to protect desc->count */ |
| 680 | mutex_lock(&desc->wlock); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 681 | desc->count--; |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 682 | mutex_unlock(&desc->wlock); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 683 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 684 | if (!desc->count) { |
Bjørn Mork | 880bca3 | 2012-04-30 09:26:11 +0200 | [diff] [blame] | 685 | if (!test_bit(WDM_DISCONNECTING, &desc->flags)) { |
Oliver Neukum | ce8bb34 | 2016-08-16 15:12:22 +0200 | [diff] [blame] | 686 | dev_dbg(&desc->intf->dev, "wdm_release: cleanup\n"); |
Bjørn Mork | 6b0b79d | 2012-05-09 13:53:22 +0200 | [diff] [blame] | 687 | kill_urbs(desc); |
Greg Suarez | 73e0686 | 2013-10-29 10:29:10 -0700 | [diff] [blame] | 688 | spin_lock_irq(&desc->iuspin); |
| 689 | desc->resp_count = 0; |
| 690 | spin_unlock_irq(&desc->iuspin); |
Bjørn Mork | 3cc3615 | 2012-03-06 17:29:22 +0100 | [diff] [blame] | 691 | desc->manage_power(desc->intf, 0); |
Bjørn Mork | 880bca3 | 2012-04-30 09:26:11 +0200 | [diff] [blame] | 692 | } else { |
Bjørn Mork | 6b0b79d | 2012-05-09 13:53:22 +0200 | [diff] [blame] | 693 | /* must avoid dev_printk here as desc->intf is invalid */ |
| 694 | pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__); |
Oliver Neukum | 2f338c8 | 2012-04-27 14:36:37 +0200 | [diff] [blame] | 695 | cleanup(desc); |
Bjørn Mork | 880bca3 | 2012-04-30 09:26:11 +0200 | [diff] [blame] | 696 | } |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 697 | } |
| 698 | mutex_unlock(&wdm_mutex); |
| 699 | return 0; |
| 700 | } |
| 701 | |
Bjørn Mork | 3edce1c | 2013-03-17 21:00:06 +0100 | [diff] [blame] | 702 | static long wdm_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 703 | { |
| 704 | struct wdm_device *desc = file->private_data; |
| 705 | int rv = 0; |
| 706 | |
| 707 | switch (cmd) { |
| 708 | case IOCTL_WDM_MAX_COMMAND: |
| 709 | if (copy_to_user((void __user *)arg, &desc->wMaxCommand, sizeof(desc->wMaxCommand))) |
| 710 | rv = -EFAULT; |
| 711 | break; |
| 712 | default: |
| 713 | rv = -ENOTTY; |
| 714 | } |
| 715 | return rv; |
| 716 | } |
| 717 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 718 | static const struct file_operations wdm_fops = { |
| 719 | .owner = THIS_MODULE, |
| 720 | .read = wdm_read, |
| 721 | .write = wdm_write, |
| 722 | .open = wdm_open, |
| 723 | .flush = wdm_flush, |
| 724 | .release = wdm_release, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 725 | .poll = wdm_poll, |
Bjørn Mork | 3edce1c | 2013-03-17 21:00:06 +0100 | [diff] [blame] | 726 | .unlocked_ioctl = wdm_ioctl, |
| 727 | .compat_ioctl = wdm_ioctl, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 728 | .llseek = noop_llseek, |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 729 | }; |
| 730 | |
| 731 | static struct usb_class_driver wdm_class = { |
| 732 | .name = "cdc-wdm%d", |
| 733 | .fops = &wdm_fops, |
| 734 | .minor_base = WDM_MINOR_BASE, |
| 735 | }; |
| 736 | |
| 737 | /* --- error handling --- */ |
| 738 | static void wdm_rxwork(struct work_struct *work) |
| 739 | { |
| 740 | struct wdm_device *desc = container_of(work, struct wdm_device, rxwork); |
| 741 | unsigned long flags; |
Oliver Neukum | 6dd433e | 2013-08-06 14:22:59 +0200 | [diff] [blame] | 742 | int rv = 0; |
| 743 | int responding; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 744 | |
| 745 | spin_lock_irqsave(&desc->iuspin, flags); |
| 746 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { |
| 747 | spin_unlock_irqrestore(&desc->iuspin, flags); |
| 748 | } else { |
Oliver Neukum | 6dd433e | 2013-08-06 14:22:59 +0200 | [diff] [blame] | 749 | responding = test_and_set_bit(WDM_RESPONDING, &desc->flags); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 750 | spin_unlock_irqrestore(&desc->iuspin, flags); |
Oliver Neukum | 6dd433e | 2013-08-06 14:22:59 +0200 | [diff] [blame] | 751 | if (!responding) |
| 752 | rv = usb_submit_urb(desc->response, GFP_KERNEL); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 753 | if (rv < 0 && rv != -EPERM) { |
| 754 | spin_lock_irqsave(&desc->iuspin, flags); |
Oliver Neukum | 6dd433e | 2013-08-06 14:22:59 +0200 | [diff] [blame] | 755 | clear_bit(WDM_RESPONDING, &desc->flags); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 756 | if (!test_bit(WDM_DISCONNECTING, &desc->flags)) |
| 757 | schedule_work(&desc->rxwork); |
| 758 | spin_unlock_irqrestore(&desc->iuspin, flags); |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
Sebastian Andrzej Siewior | 2df6948 | 2018-06-14 18:36:46 +0200 | [diff] [blame] | 763 | static void service_interrupt_work(struct work_struct *work) |
| 764 | { |
| 765 | struct wdm_device *desc; |
| 766 | |
| 767 | desc = container_of(work, struct wdm_device, service_outs_intr); |
| 768 | |
| 769 | spin_lock_irq(&desc->iuspin); |
| 770 | service_outstanding_interrupt(desc); |
| 771 | if (!desc->resp_count) { |
| 772 | set_bit(WDM_READ, &desc->flags); |
| 773 | wake_up(&desc->wait); |
| 774 | } |
| 775 | spin_unlock_irq(&desc->iuspin); |
| 776 | } |
| 777 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 778 | /* --- hotplug --- */ |
| 779 | |
Bjørn Mork | 3cc3615 | 2012-03-06 17:29:22 +0100 | [diff] [blame] | 780 | static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep, |
Bjørn Mork | 1944581 | 2017-04-21 10:01:29 +0200 | [diff] [blame] | 781 | u16 bufsize, int (*manage_power)(struct usb_interface *, int)) |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 782 | { |
Bjørn Mork | 0dffb48 | 2012-03-06 17:29:20 +0100 | [diff] [blame] | 783 | int rv = -ENOMEM; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 784 | struct wdm_device *desc; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 785 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 786 | desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL); |
| 787 | if (!desc) |
| 788 | goto out; |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 789 | INIT_LIST_HEAD(&desc->device_list); |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 790 | mutex_init(&desc->rlock); |
| 791 | mutex_init(&desc->wlock); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 792 | spin_lock_init(&desc->iuspin); |
| 793 | init_waitqueue_head(&desc->wait); |
Bjørn Mork | 0dffb48 | 2012-03-06 17:29:20 +0100 | [diff] [blame] | 794 | desc->wMaxCommand = bufsize; |
Oliver Neukum | 052fbc0 | 2009-04-20 17:24:49 +0200 | [diff] [blame] | 795 | /* this will be expanded and needed in hardware endianness */ |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 796 | desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber); |
| 797 | desc->intf = intf; |
| 798 | INIT_WORK(&desc->rxwork, wdm_rxwork); |
Sebastian Andrzej Siewior | 2df6948 | 2018-06-14 18:36:46 +0200 | [diff] [blame] | 799 | INIT_WORK(&desc->service_outs_intr, service_interrupt_work); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 800 | |
Oliver Neukum | 052fbc0 | 2009-04-20 17:24:49 +0200 | [diff] [blame] | 801 | rv = -EINVAL; |
Bjørn Mork | 0dffb48 | 2012-03-06 17:29:20 +0100 | [diff] [blame] | 802 | if (!usb_endpoint_is_int_in(ep)) |
Oliver Neukum | 052fbc0 | 2009-04-20 17:24:49 +0200 | [diff] [blame] | 803 | goto err; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 804 | |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 805 | desc->wMaxPacketSize = usb_endpoint_maxp(ep); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 806 | |
| 807 | desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); |
| 808 | if (!desc->orq) |
| 809 | goto err; |
| 810 | desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); |
| 811 | if (!desc->irq) |
| 812 | goto err; |
| 813 | |
| 814 | desc->validity = usb_alloc_urb(0, GFP_KERNEL); |
| 815 | if (!desc->validity) |
| 816 | goto err; |
| 817 | |
| 818 | desc->response = usb_alloc_urb(0, GFP_KERNEL); |
| 819 | if (!desc->response) |
| 820 | goto err; |
| 821 | |
| 822 | desc->command = usb_alloc_urb(0, GFP_KERNEL); |
| 823 | if (!desc->command) |
| 824 | goto err; |
| 825 | |
| 826 | desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL); |
| 827 | if (!desc->ubuf) |
| 828 | goto err; |
| 829 | |
Bjørn Mork | 8457d99 | 2012-01-16 15:12:00 +0100 | [diff] [blame] | 830 | desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 831 | if (!desc->sbuf) |
| 832 | goto err; |
| 833 | |
Bjørn Mork | 8457d99 | 2012-01-16 15:12:00 +0100 | [diff] [blame] | 834 | desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 835 | if (!desc->inbuf) |
Bjørn Mork | 8457d99 | 2012-01-16 15:12:00 +0100 | [diff] [blame] | 836 | goto err; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 837 | |
| 838 | usb_fill_int_urb( |
| 839 | desc->validity, |
| 840 | interface_to_usbdev(intf), |
| 841 | usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress), |
| 842 | desc->sbuf, |
| 843 | desc->wMaxPacketSize, |
| 844 | wdm_int_callback, |
| 845 | desc, |
| 846 | ep->bInterval |
| 847 | ); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 848 | |
Bjørn Mork | 19b85b3 | 2012-01-16 15:11:58 +0100 | [diff] [blame] | 849 | desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE); |
| 850 | desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE; |
| 851 | desc->irq->wValue = 0; |
Oliver Neukum | 323ece5 | 2015-03-20 14:29:34 +0100 | [diff] [blame] | 852 | desc->irq->wIndex = desc->inum; /* already converted */ |
Bjørn Mork | 19b85b3 | 2012-01-16 15:11:58 +0100 | [diff] [blame] | 853 | desc->irq->wLength = cpu_to_le16(desc->wMaxCommand); |
| 854 | |
| 855 | usb_fill_control_urb( |
| 856 | desc->response, |
Bjørn Mork | 8143a89 | 2012-01-16 15:12:01 +0100 | [diff] [blame] | 857 | interface_to_usbdev(intf), |
Bjørn Mork | 19b85b3 | 2012-01-16 15:11:58 +0100 | [diff] [blame] | 858 | /* using common endpoint 0 */ |
| 859 | usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0), |
| 860 | (unsigned char *)desc->irq, |
| 861 | desc->inbuf, |
| 862 | desc->wMaxCommand, |
| 863 | wdm_in_callback, |
| 864 | desc |
| 865 | ); |
Bjørn Mork | 19b85b3 | 2012-01-16 15:11:58 +0100 | [diff] [blame] | 866 | |
Bjørn Mork | 3cc3615 | 2012-03-06 17:29:22 +0100 | [diff] [blame] | 867 | desc->manage_power = manage_power; |
| 868 | |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 869 | spin_lock(&wdm_device_list_lock); |
| 870 | list_add(&desc->device_list, &wdm_device_list); |
| 871 | spin_unlock(&wdm_device_list_lock); |
| 872 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 873 | rv = usb_register_dev(intf, &wdm_class); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 874 | if (rv < 0) |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 875 | goto err; |
Oliver Neukum | 052fbc0 | 2009-04-20 17:24:49 +0200 | [diff] [blame] | 876 | else |
Bjørn Mork | 820c629 | 2012-01-20 04:17:25 +0100 | [diff] [blame] | 877 | 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] | 878 | out: |
| 879 | return rv; |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 880 | err: |
Bjørn Mork | 6286d85 | 2012-05-09 13:53:23 +0200 | [diff] [blame] | 881 | spin_lock(&wdm_device_list_lock); |
| 882 | list_del(&desc->device_list); |
| 883 | spin_unlock(&wdm_device_list_lock); |
Bjørn Mork | 0dffb48 | 2012-03-06 17:29:20 +0100 | [diff] [blame] | 884 | cleanup(desc); |
| 885 | return rv; |
| 886 | } |
| 887 | |
Bjørn Mork | 3cc3615 | 2012-03-06 17:29:22 +0100 | [diff] [blame] | 888 | static int wdm_manage_power(struct usb_interface *intf, int on) |
| 889 | { |
| 890 | /* need autopm_get/put here to ensure the usbcore sees the new value */ |
| 891 | int rv = usb_autopm_get_interface(intf); |
Bjørn Mork | 3cc3615 | 2012-03-06 17:29:22 +0100 | [diff] [blame] | 892 | |
| 893 | intf->needs_remote_wakeup = on; |
Bjørn Mork | 4144bc8 | 2013-11-29 20:17:45 +0100 | [diff] [blame] | 894 | if (!rv) |
| 895 | usb_autopm_put_interface(intf); |
| 896 | return 0; |
Bjørn Mork | 3cc3615 | 2012-03-06 17:29:22 +0100 | [diff] [blame] | 897 | } |
| 898 | |
Bjørn Mork | 0dffb48 | 2012-03-06 17:29:20 +0100 | [diff] [blame] | 899 | static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id) |
| 900 | { |
| 901 | int rv = -EINVAL; |
| 902 | struct usb_host_interface *iface; |
| 903 | struct usb_endpoint_descriptor *ep; |
Oliver Neukum | 7fae7bf | 2016-07-14 15:41:33 +0200 | [diff] [blame] | 904 | struct usb_cdc_parsed_header hdr; |
Bjørn Mork | 0dffb48 | 2012-03-06 17:29:20 +0100 | [diff] [blame] | 905 | u8 *buffer = intf->altsetting->extra; |
| 906 | int buflen = intf->altsetting->extralen; |
| 907 | u16 maxcom = WDM_DEFAULT_BUFSIZE; |
| 908 | |
| 909 | if (!buffer) |
| 910 | goto err; |
Bjørn Mork | 0dffb48 | 2012-03-06 17:29:20 +0100 | [diff] [blame] | 911 | |
Oliver Neukum | 7fae7bf | 2016-07-14 15:41:33 +0200 | [diff] [blame] | 912 | cdc_parse_cdc_header(&hdr, intf, buffer, buflen); |
| 913 | |
| 914 | if (hdr.usb_cdc_dmm_desc) |
| 915 | maxcom = le16_to_cpu(hdr.usb_cdc_dmm_desc->wMaxCommand); |
Bjørn Mork | 0dffb48 | 2012-03-06 17:29:20 +0100 | [diff] [blame] | 916 | |
| 917 | iface = intf->cur_altsetting; |
| 918 | if (iface->desc.bNumEndpoints != 1) |
| 919 | goto err; |
| 920 | ep = &iface->endpoint[0].desc; |
| 921 | |
Bjørn Mork | 1944581 | 2017-04-21 10:01:29 +0200 | [diff] [blame] | 922 | rv = wdm_create(intf, ep, maxcom, &wdm_manage_power); |
Bjørn Mork | 0dffb48 | 2012-03-06 17:29:20 +0100 | [diff] [blame] | 923 | |
| 924 | err: |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 925 | return rv; |
| 926 | } |
| 927 | |
Bjørn Mork | 3cc3615 | 2012-03-06 17:29:22 +0100 | [diff] [blame] | 928 | /** |
| 929 | * usb_cdc_wdm_register - register a WDM subdriver |
| 930 | * @intf: usb interface the subdriver will associate with |
| 931 | * @ep: interrupt endpoint to monitor for notifications |
| 932 | * @bufsize: maximum message size to support for read/write |
| 933 | * |
| 934 | * Create WDM usb class character device and associate it with intf |
| 935 | * without binding, allowing another driver to manage the interface. |
| 936 | * |
| 937 | * The subdriver will manage the given interrupt endpoint exclusively |
| 938 | * and will issue control requests referring to the given intf. It |
| 939 | * will otherwise avoid interferring, and in particular not do |
| 940 | * usb_set_intfdata/usb_get_intfdata on intf. |
| 941 | * |
| 942 | * The return value is a pointer to the subdriver's struct usb_driver. |
| 943 | * The registering driver is responsible for calling this subdriver's |
| 944 | * disconnect, suspend, resume, pre_reset and post_reset methods from |
| 945 | * its own. |
| 946 | */ |
| 947 | struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf, |
| 948 | struct usb_endpoint_descriptor *ep, |
| 949 | int bufsize, |
| 950 | int (*manage_power)(struct usb_interface *, int)) |
| 951 | { |
| 952 | int rv = -EINVAL; |
| 953 | |
Bjørn Mork | 1944581 | 2017-04-21 10:01:29 +0200 | [diff] [blame] | 954 | rv = wdm_create(intf, ep, bufsize, manage_power); |
Bjørn Mork | 3cc3615 | 2012-03-06 17:29:22 +0100 | [diff] [blame] | 955 | if (rv < 0) |
| 956 | goto err; |
| 957 | |
| 958 | return &wdm_driver; |
| 959 | err: |
| 960 | return ERR_PTR(rv); |
| 961 | } |
| 962 | EXPORT_SYMBOL(usb_cdc_wdm_register); |
| 963 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 964 | static void wdm_disconnect(struct usb_interface *intf) |
| 965 | { |
| 966 | struct wdm_device *desc; |
| 967 | unsigned long flags; |
| 968 | |
| 969 | usb_deregister_dev(intf, &wdm_class); |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 970 | desc = wdm_find_device(intf); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 971 | mutex_lock(&wdm_mutex); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 972 | |
| 973 | /* the spinlock makes sure no new urbs are generated in the callbacks */ |
| 974 | spin_lock_irqsave(&desc->iuspin, flags); |
| 975 | set_bit(WDM_DISCONNECTING, &desc->flags); |
| 976 | set_bit(WDM_READ, &desc->flags); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 977 | /* to terminate pending flushes */ |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 978 | clear_bit(WDM_IN_USE, &desc->flags); |
| 979 | spin_unlock_irqrestore(&desc->iuspin, flags); |
Bjørn Mork | 62aaf24 | 2012-01-16 15:11:57 +0100 | [diff] [blame] | 980 | wake_up_all(&desc->wait); |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 981 | mutex_lock(&desc->rlock); |
| 982 | mutex_lock(&desc->wlock); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 983 | kill_urbs(desc); |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 984 | cancel_work_sync(&desc->rxwork); |
Sebastian Andrzej Siewior | 2df6948 | 2018-06-14 18:36:46 +0200 | [diff] [blame] | 985 | cancel_work_sync(&desc->service_outs_intr); |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 986 | mutex_unlock(&desc->wlock); |
| 987 | mutex_unlock(&desc->rlock); |
Bjørn Mork | 6286d85 | 2012-05-09 13:53:23 +0200 | [diff] [blame] | 988 | |
| 989 | /* the desc->intf pointer used as list key is now invalid */ |
| 990 | spin_lock(&wdm_device_list_lock); |
| 991 | list_del(&desc->device_list); |
| 992 | spin_unlock(&wdm_device_list_lock); |
| 993 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 994 | if (!desc->count) |
| 995 | cleanup(desc); |
Bjørn Mork | 880bca3 | 2012-04-30 09:26:11 +0200 | [diff] [blame] | 996 | else |
Oliver Neukum | 13a88bf | 2016-08-15 11:17:55 +0200 | [diff] [blame] | 997 | dev_dbg(&intf->dev, "%d open files - postponing cleanup\n", desc->count); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 998 | mutex_unlock(&wdm_mutex); |
| 999 | } |
| 1000 | |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 1001 | #ifdef CONFIG_PM |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1002 | static int wdm_suspend(struct usb_interface *intf, pm_message_t message) |
| 1003 | { |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 1004 | struct wdm_device *desc = wdm_find_device(intf); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1005 | int rv = 0; |
| 1006 | |
| 1007 | dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor); |
| 1008 | |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 1009 | /* if this is an autosuspend the caller does the locking */ |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 1010 | if (!PMSG_IS_AUTO(message)) { |
| 1011 | mutex_lock(&desc->rlock); |
| 1012 | mutex_lock(&desc->wlock); |
| 1013 | } |
Oliver Neukum | 62e6685 | 2010-02-27 20:56:22 +0100 | [diff] [blame] | 1014 | spin_lock_irq(&desc->iuspin); |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 1015 | |
Alan Stern | 5b1b0b8 | 2011-08-19 23:49:48 +0200 | [diff] [blame] | 1016 | if (PMSG_IS_AUTO(message) && |
Oliver Neukum | 922a5ea | 2010-02-27 20:54:59 +0100 | [diff] [blame] | 1017 | (test_bit(WDM_IN_USE, &desc->flags) |
| 1018 | || test_bit(WDM_RESPONDING, &desc->flags))) { |
Oliver Neukum | 62e6685 | 2010-02-27 20:56:22 +0100 | [diff] [blame] | 1019 | spin_unlock_irq(&desc->iuspin); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1020 | rv = -EBUSY; |
| 1021 | } else { |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 1022 | |
Oliver Neukum | beb1d35 | 2010-02-27 20:55:52 +0100 | [diff] [blame] | 1023 | set_bit(WDM_SUSPENDING, &desc->flags); |
Oliver Neukum | 62e6685 | 2010-02-27 20:56:22 +0100 | [diff] [blame] | 1024 | spin_unlock_irq(&desc->iuspin); |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 1025 | /* callback submits work - order is essential */ |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1026 | kill_urbs(desc); |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 1027 | cancel_work_sync(&desc->rxwork); |
Sebastian Andrzej Siewior | 2df6948 | 2018-06-14 18:36:46 +0200 | [diff] [blame] | 1028 | cancel_work_sync(&desc->service_outs_intr); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1029 | } |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 1030 | if (!PMSG_IS_AUTO(message)) { |
| 1031 | mutex_unlock(&desc->wlock); |
| 1032 | mutex_unlock(&desc->rlock); |
| 1033 | } |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1034 | |
| 1035 | return rv; |
| 1036 | } |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 1037 | #endif |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1038 | |
| 1039 | static int recover_from_urb_loss(struct wdm_device *desc) |
| 1040 | { |
| 1041 | int rv = 0; |
| 1042 | |
| 1043 | if (desc->count) { |
| 1044 | rv = usb_submit_urb(desc->validity, GFP_NOIO); |
| 1045 | if (rv < 0) |
Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 1046 | dev_err(&desc->intf->dev, |
| 1047 | "Error resume submitting int urb - %d\n", rv); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1048 | } |
| 1049 | return rv; |
| 1050 | } |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 1051 | |
| 1052 | #ifdef CONFIG_PM |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1053 | static int wdm_resume(struct usb_interface *intf) |
| 1054 | { |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 1055 | struct wdm_device *desc = wdm_find_device(intf); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1056 | int rv; |
| 1057 | |
| 1058 | dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor); |
Oliver Neukum | 338124c | 2010-02-27 20:57:12 +0100 | [diff] [blame] | 1059 | |
Oliver Neukum | beb1d35 | 2010-02-27 20:55:52 +0100 | [diff] [blame] | 1060 | clear_bit(WDM_SUSPENDING, &desc->flags); |
Oliver Neukum | 62e6685 | 2010-02-27 20:56:22 +0100 | [diff] [blame] | 1061 | rv = recover_from_urb_loss(desc); |
Oliver Neukum | 338124c | 2010-02-27 20:57:12 +0100 | [diff] [blame] | 1062 | |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1063 | return rv; |
| 1064 | } |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 1065 | #endif |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1066 | |
| 1067 | static int wdm_pre_reset(struct usb_interface *intf) |
| 1068 | { |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 1069 | struct wdm_device *desc = wdm_find_device(intf); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1070 | |
Oliver Neukum | d771d8a | 2011-04-29 14:12:21 +0200 | [diff] [blame] | 1071 | /* |
| 1072 | * we notify everybody using poll of |
| 1073 | * an exceptional situation |
| 1074 | * must be done before recovery lest a spontaneous |
| 1075 | * message from the device is lost |
| 1076 | */ |
| 1077 | spin_lock_irq(&desc->iuspin); |
Bjørn Mork | 8804420 | 2012-02-10 09:44:08 +0100 | [diff] [blame] | 1078 | set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */ |
| 1079 | set_bit(WDM_READ, &desc->flags); /* unblock read */ |
| 1080 | clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */ |
Oliver Neukum | d771d8a | 2011-04-29 14:12:21 +0200 | [diff] [blame] | 1081 | desc->rerr = -EINTR; |
| 1082 | spin_unlock_irq(&desc->iuspin); |
| 1083 | wake_up_all(&desc->wait); |
Bjørn Mork | 8804420 | 2012-02-10 09:44:08 +0100 | [diff] [blame] | 1084 | mutex_lock(&desc->rlock); |
| 1085 | mutex_lock(&desc->wlock); |
| 1086 | kill_urbs(desc); |
| 1087 | cancel_work_sync(&desc->rxwork); |
Sebastian Andrzej Siewior | 2df6948 | 2018-06-14 18:36:46 +0200 | [diff] [blame] | 1088 | cancel_work_sync(&desc->service_outs_intr); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1089 | return 0; |
| 1090 | } |
| 1091 | |
| 1092 | static int wdm_post_reset(struct usb_interface *intf) |
| 1093 | { |
Bjørn Mork | b0c1386 | 2012-03-06 17:29:21 +0100 | [diff] [blame] | 1094 | struct wdm_device *desc = wdm_find_device(intf); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1095 | int rv; |
| 1096 | |
Oliver Neukum | c0f5ece | 2013-03-12 14:52:42 +0100 | [diff] [blame] | 1097 | clear_bit(WDM_OVERFLOW, &desc->flags); |
Bjørn Mork | 8804420 | 2012-02-10 09:44:08 +0100 | [diff] [blame] | 1098 | clear_bit(WDM_RESETTING, &desc->flags); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1099 | rv = recover_from_urb_loss(desc); |
Bjørn Mork | e8537bd | 2012-01-16 12:41:48 +0100 | [diff] [blame] | 1100 | mutex_unlock(&desc->wlock); |
| 1101 | mutex_unlock(&desc->rlock); |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1102 | return 0; |
| 1103 | } |
| 1104 | |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 1105 | static struct usb_driver wdm_driver = { |
| 1106 | .name = "cdc_wdm", |
| 1107 | .probe = wdm_probe, |
| 1108 | .disconnect = wdm_disconnect, |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 1109 | #ifdef CONFIG_PM |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1110 | .suspend = wdm_suspend, |
| 1111 | .resume = wdm_resume, |
| 1112 | .reset_resume = wdm_resume, |
Oliver Neukum | d93d16e | 2010-02-27 20:56:47 +0100 | [diff] [blame] | 1113 | #endif |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1114 | .pre_reset = wdm_pre_reset, |
| 1115 | .post_reset = wdm_post_reset, |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 1116 | .id_table = wdm_ids, |
Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 1117 | .supports_autosuspend = 1, |
Sarah Sharp | e1f12eb | 2012-04-23 10:08:51 -0700 | [diff] [blame] | 1118 | .disable_hub_initiated_lpm = 1, |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 1119 | }; |
| 1120 | |
Greg Kroah-Hartman | 65db430 | 2011-11-18 09:34:02 -0800 | [diff] [blame] | 1121 | module_usb_driver(wdm_driver); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 1122 | |
| 1123 | MODULE_AUTHOR(DRIVER_AUTHOR); |
Oliver Neukum | 87d65e5 | 2008-06-19 14:20:18 +0200 | [diff] [blame] | 1124 | MODULE_DESCRIPTION(DRIVER_DESC); |
Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 1125 | MODULE_LICENSE("GPL"); |