blob: bec581fb7c6361891a81a3a0aa88d3863b686f58 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Oliver Neukumafba9372008-05-13 17:01:25 +02002/*
3 * cdc-wdm.c
4 *
5 * This driver supports USB CDC WCM Device Management.
6 *
Oliver Neukum052fbc02009-04-20 17:24:49 +02007 * Copyright (c) 2007-2009 Oliver Neukum
Oliver Neukumafba9372008-05-13 17:01:25 +02008 *
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 Mork3edce1c2013-03-17 21:00:06 +010017#include <linux/ioctl.h>
Oliver Neukumafba9372008-05-13 17:01:25 +020018#include <linux/slab.h>
19#include <linux/module.h>
Oliver Neukumafba9372008-05-13 17:01:25 +020020#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 Mork3cc36152012-03-06 17:29:22 +010028#include <linux/usb/cdc-wdm.h>
Oliver Neukumafba9372008-05-13 17:01:25 +020029
Oliver Neukumafba9372008-05-13 17:01:25 +020030#define DRIVER_AUTHOR "Oliver Neukum"
Oliver Neukum87d65e52008-06-19 14:20:18 +020031#define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management"
Oliver Neukumafba9372008-05-13 17:01:25 +020032
Németh Márton6ef48522010-01-10 15:33:45 +010033static const struct usb_device_id wdm_ids[] = {
Oliver Neukumafba9372008-05-13 17:01:25 +020034 {
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 Neukumaa5380b2008-10-13 14:05:20 +020043MODULE_DEVICE_TABLE (usb, wdm_ids);
44
Oliver Neukumafba9372008-05-13 17:01:25 +020045#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 Neukum922a5ea2010-02-27 20:54:59 +010054#define WDM_RESPONDING 7
Oliver Neukumbeb1d352010-02-27 20:55:52 +010055#define WDM_SUSPENDING 8
Bjørn Mork88044202012-02-10 09:44:08 +010056#define WDM_RESETTING 9
Oliver Neukumc0f5ece2013-03-12 14:52:42 +010057#define WDM_OVERFLOW 10
Oliver Neukumafba9372008-05-13 17:01:25 +020058
59#define WDM_MAX 16
60
Bjørn Mork7e3054a2012-01-20 01:49:57 +010061/* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
62#define WDM_DEFAULT_BUFSIZE 256
Oliver Neukumafba9372008-05-13 17:01:25 +020063
64static DEFINE_MUTEX(wdm_mutex);
Bjørn Morkb0c13862012-03-06 17:29:21 +010065static DEFINE_SPINLOCK(wdm_device_list_lock);
66static LIST_HEAD(wdm_device_list);
Oliver Neukumafba9372008-05-13 17:01:25 +020067
68/* --- method tables --- */
69
70struct 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 Neukumafba9372008-05-13 17:01:25 +020088 __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 Morke8537bd2012-01-16 12:41:48 +010095 struct mutex wlock;
96 struct mutex rlock;
Oliver Neukumafba9372008-05-13 17:01:25 +020097 wait_queue_head_t wait;
98 struct work_struct rxwork;
Sebastian Andrzej Siewior2df69482018-06-14 18:36:46 +020099 struct work_struct service_outs_intr;
Oliver Neukumafba9372008-05-13 17:01:25 +0200100 int werr;
101 int rerr;
Greg Suarez73e06862013-10-29 10:29:10 -0700102 int resp_count;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100103
104 struct list_head device_list;
Bjørn Mork3cc36152012-03-06 17:29:22 +0100105 int (*manage_power)(struct usb_interface *, int);
Oliver Neukumafba9372008-05-13 17:01:25 +0200106};
107
108static struct usb_driver wdm_driver;
109
Bjørn Morkb0c13862012-03-06 17:29:21 +0100110/* return intfdata if we own the interface, else look up intf in the list */
111static struct wdm_device *wdm_find_device(struct usb_interface *intf)
112{
Bjørn Mork6a448862012-09-10 22:17:34 +0200113 struct wdm_device *desc;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100114
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 Mork6a448862012-09-10 22:17:34 +0200118 goto found;
119 desc = NULL;
120found:
Bjørn Morkb0c13862012-03-06 17:29:21 +0100121 spin_unlock(&wdm_device_list_lock);
122
123 return desc;
124}
125
126static struct wdm_device *wdm_find_device_by_minor(int minor)
127{
Bjørn Mork6a448862012-09-10 22:17:34 +0200128 struct wdm_device *desc;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100129
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 Mork6a448862012-09-10 22:17:34 +0200133 goto found;
134 desc = NULL;
135found:
Bjørn Morkb0c13862012-03-06 17:29:21 +0100136 spin_unlock(&wdm_device_list_lock);
137
138 return desc;
139}
140
Oliver Neukumafba9372008-05-13 17:01:25 +0200141/* --- callbacks --- */
142static void wdm_out_callback(struct urb *urb)
143{
144 struct wdm_device *desc;
Sebastian Andrzej Siewior579b9cca2018-06-25 00:08:35 +0200145 unsigned long flags;
146
Oliver Neukumafba9372008-05-13 17:01:25 +0200147 desc = urb->context;
Sebastian Andrzej Siewior579b9cca2018-06-25 00:08:35 +0200148 spin_lock_irqsave(&desc->iuspin, flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200149 desc->werr = urb->status;
Sebastian Andrzej Siewior579b9cca2018-06-25 00:08:35 +0200150 spin_unlock_irqrestore(&desc->iuspin, flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200151 kfree(desc->outbuf);
Oliver Neukum5c228372012-04-26 21:59:10 +0200152 desc->outbuf = NULL;
153 clear_bit(WDM_IN_USE, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200154 wake_up(&desc->wait);
155}
156
157static void wdm_in_callback(struct urb *urb)
158{
Sebastian Andrzej Siewior579b9cca2018-06-25 00:08:35 +0200159 unsigned long flags;
Oliver Neukumafba9372008-05-13 17:01:25 +0200160 struct wdm_device *desc = urb->context;
161 int status = urb->status;
Oliver Neukumc0f5ece2013-03-12 14:52:42 +0100162 int length = urb->actual_length;
Oliver Neukumafba9372008-05-13 17:01:25 +0200163
Sebastian Andrzej Siewior579b9cca2018-06-25 00:08:35 +0200164 spin_lock_irqsave(&desc->iuspin, flags);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100165 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200166
167 if (status) {
168 switch (status) {
169 case -ENOENT:
170 dev_dbg(&desc->intf->dev,
Oliver Neukumce8bb342016-08-16 15:12:22 +0200171 "nonzero urb status received: -ENOENT\n");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100172 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200173 case -ECONNRESET:
174 dev_dbg(&desc->intf->dev,
Oliver Neukumce8bb342016-08-16 15:12:22 +0200175 "nonzero urb status received: -ECONNRESET\n");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100176 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200177 case -ESHUTDOWN:
178 dev_dbg(&desc->intf->dev,
Oliver Neukumce8bb342016-08-16 15:12:22 +0200179 "nonzero urb status received: -ESHUTDOWN\n");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100180 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200181 case -EPIPE:
Bjørn Mork19445812017-04-21 10:01:29 +0200182 dev_err(&desc->intf->dev,
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700183 "nonzero urb status received: -EPIPE\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200184 break;
185 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700186 dev_err(&desc->intf->dev,
187 "Unexpected error %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200188 break;
189 }
190 }
191
Robert Fossc1da59d2016-08-09 10:54:52 -0400192 /*
193 * only set a new error if there is no previous error.
194 * Errors are only cleared during read/open
Bjørn Mork8fec9352017-09-22 22:18:18 +0200195 * Avoid propagating -EPIPE (stall) to userspace since it is
196 * better handled as an empty read
Robert Fossc1da59d2016-08-09 10:54:52 -0400197 */
Bjørn Mork8fec9352017-09-22 22:18:18 +0200198 if (desc->rerr == 0 && status != -EPIPE)
Robert Fossc1da59d2016-08-09 10:54:52 -0400199 desc->rerr = status;
200
Oliver Neukumc0f5ece2013-03-12 14:52:42 +0100201 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 Neukum922a5ea2010-02-27 20:54:59 +0100212skip_error:
Oliver Neukumafba9372008-05-13 17:01:25 +0200213
Robert Fossc1da59d2016-08-09 10:54:52 -0400214 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 Siewior2df69482018-06-14 18:36:46 +0200221 schedule_work(&desc->service_outs_intr);
222 } else {
223 set_bit(WDM_READ, &desc->flags);
224 wake_up(&desc->wait);
Robert Fossc1da59d2016-08-09 10:54:52 -0400225 }
Sebastian Andrzej Siewior579b9cca2018-06-25 00:08:35 +0200226 spin_unlock_irqrestore(&desc->iuspin, flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200227}
228
229static void wdm_int_callback(struct urb *urb)
230{
Sebastian Andrzej Siewior579b9cca2018-06-25 00:08:35 +0200231 unsigned long flags;
Oliver Neukumafba9372008-05-13 17:01:25 +0200232 int rv = 0;
Oliver Neukum6dd433e2013-08-06 14:22:59 +0200233 int responding;
Oliver Neukumafba9372008-05-13 17:01:25 +0200234 int status = urb->status;
235 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200236 struct usb_cdc_notification *dr;
237
238 desc = urb->context;
Oliver Neukumafba9372008-05-13 17:01:25 +0200239 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-Hartman9908a322008-08-14 09:37:34 -0700249 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200250 goto sw; /* halt is cleared in work */
251 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700252 dev_err(&desc->intf->dev,
253 "nonzero urb status received: %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200254 break;
255 }
256 }
257
258 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700259 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
260 urb->actual_length);
Oliver Neukumafba9372008-05-13 17:01:25 +0200261 goto exit;
262 }
263
264 switch (dr->bNotificationType) {
265 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
266 dev_dbg(&desc->intf->dev,
Oliver Neukumce8bb342016-08-16 15:12:22 +0200267 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d\n",
Oliver Neukum323ece52015-03-20 14:29:34 +0100268 le16_to_cpu(dr->wIndex), le16_to_cpu(dr->wLength));
Oliver Neukumafba9372008-05-13 17:01:25 +0200269 break;
270
271 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
272
273 dev_dbg(&desc->intf->dev,
Oliver Neukumce8bb342016-08-16 15:12:22 +0200274 "NOTIFY_NETWORK_CONNECTION %s network\n",
Oliver Neukumafba9372008-05-13 17:01:25 +0200275 dr->wValue ? "connected to" : "disconnected from");
276 goto exit;
Bjørn Mork9983d6d2013-10-29 09:52:57 +0100277 case USB_CDC_NOTIFY_SPEED_CHANGE:
Oliver Neukumce8bb342016-08-16 15:12:22 +0200278 dev_dbg(&desc->intf->dev, "SPEED_CHANGE received (len %u)\n",
Bjørn Mork9983d6d2013-10-29 09:52:57 +0100279 urb->actual_length);
280 goto exit;
Oliver Neukumafba9372008-05-13 17:01:25 +0200281 default:
282 clear_bit(WDM_POLL_RUNNING, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700283 dev_err(&desc->intf->dev,
284 "unknown notification %d received: index %d len %d\n",
Oliver Neukum323ece52015-03-20 14:29:34 +0100285 dr->bNotificationType,
286 le16_to_cpu(dr->wIndex),
287 le16_to_cpu(dr->wLength));
Oliver Neukumafba9372008-05-13 17:01:25 +0200288 goto exit;
289 }
290
Sebastian Andrzej Siewior579b9cca2018-06-25 00:08:35 +0200291 spin_lock_irqsave(&desc->iuspin, flags);
Oliver Neukum6dd433e2013-08-06 14:22:59 +0200292 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
Greg Suarez73e06862013-10-29 10:29:10 -0700293 if (!desc->resp_count++ && !responding
294 && !test_bit(WDM_DISCONNECTING, &desc->flags)
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100295 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200296 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
Oliver Neukumce8bb342016-08-16 15:12:22 +0200297 dev_dbg(&desc->intf->dev, "submit response URB %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200298 }
Sebastian Andrzej Siewior579b9cca2018-06-25 00:08:35 +0200299 spin_unlock_irqrestore(&desc->iuspin, flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200300 if (rv < 0) {
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100301 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200302 if (rv == -EPERM)
303 return;
304 if (rv == -ENOMEM) {
305sw:
306 rv = schedule_work(&desc->rxwork);
307 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700308 dev_err(&desc->intf->dev,
309 "Cannot schedule work\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200310 }
311 }
312exit:
313 rv = usb_submit_urb(urb, GFP_ATOMIC);
314 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700315 dev_err(&desc->intf->dev,
316 "%s - usb_submit_urb failed with result %d\n",
317 __func__, rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200318
319}
320
321static void kill_urbs(struct wdm_device *desc)
322{
Oliver Neukum17d80d52008-06-24 15:56:10 +0200323 /* the order here is essential */
Oliver Neukumafba9372008-05-13 17:01:25 +0200324 usb_kill_urb(desc->command);
325 usb_kill_urb(desc->validity);
326 usb_kill_urb(desc->response);
327}
328
329static 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
336static void cleanup(struct wdm_device *desc)
337{
Bjørn Mork8457d992012-01-16 15:12:00 +0100338 kfree(desc->sbuf);
339 kfree(desc->inbuf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200340 kfree(desc->orq);
341 kfree(desc->irq);
342 kfree(desc->ubuf);
343 free_urbs(desc);
344 kfree(desc);
345}
346
347static 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 Neukum76cb03e2015-03-20 14:28:56 +0100363 return usb_translate_errors(we);
Oliver Neukumafba9372008-05-13 17:01:25 +0200364
Geliang Tang64b95332017-05-08 23:14:39 +0800365 buf = memdup_user(buffer, count);
366 if (IS_ERR(buf))
367 return PTR_ERR(buf);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100368
369 /* concurrent writes and disconnect */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100370 r = mutex_lock_interruptible(&desc->wlock);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100371 rv = -ERESTARTSYS;
Oliver Neukum28965e12015-03-20 14:29:18 +0100372 if (r)
373 goto out_free_mem;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100374
375 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
Oliver Neukum860e41a2010-02-27 20:54:24 +0100376 rv = -ENODEV;
Oliver Neukum28965e12015-03-20 14:29:18 +0100377 goto out_free_mem_lock;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100378 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200379
Oliver Neukum17d80d52008-06-24 15:56:10 +0200380 r = usb_autopm_get_interface(desc->intf);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100381 if (r < 0) {
Oliver Neukum12a98b22012-04-30 09:57:31 +0200382 rv = usb_translate_errors(r);
Oliver Neukum28965e12015-03-20 14:29:18 +0100383 goto out_free_mem_lock;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100384 }
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200385
David Sterba0cdfb812010-12-27 18:49:58 +0100386 if (!(file->f_flags & O_NONBLOCK))
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200387 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 Mork88044202012-02-10 09:44:08 +0100392
393 if (test_bit(WDM_RESETTING, &desc->flags))
394 r = -EIO;
395
Oliver Neukum860e41a2010-02-27 20:54:24 +0100396 if (r < 0) {
Oliver Neukum12a98b22012-04-30 09:57:31 +0200397 rv = r;
Oliver Neukum28965e12015-03-20 14:29:18 +0100398 goto out_free_mem_pm;
Oliver Neukumafba9372008-05-13 17:01:25 +0200399 }
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 Neukum323ece52015-03-20 14:29:34 +0100418 req->wIndex = desc->inum; /* already converted */
Oliver Neukumafba9372008-05-13 17:01:25 +0200419 req->wLength = cpu_to_le16(count);
420 set_bit(WDM_IN_USE, &desc->flags);
Oliver Neukum5c228372012-04-26 21:59:10 +0200421 desc->outbuf = buf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200422
423 rv = usb_submit_urb(desc->command, GFP_KERNEL);
424 if (rv < 0) {
Oliver Neukum5c228372012-04-26 21:59:10 +0200425 desc->outbuf = NULL;
Oliver Neukumafba9372008-05-13 17:01:25 +0200426 clear_bit(WDM_IN_USE, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700427 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
Oliver Neukum12a98b22012-04-30 09:57:31 +0200428 rv = usb_translate_errors(rv);
Oliver Neukum28965e12015-03-20 14:29:18 +0100429 goto out_free_mem_pm;
Oliver Neukumafba9372008-05-13 17:01:25 +0200430 } else {
Oliver Neukumce8bb342016-08-16 15:12:22 +0200431 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d\n",
Oliver Neukum323ece52015-03-20 14:29:34 +0100432 le16_to_cpu(req->wIndex));
Oliver Neukumafba9372008-05-13 17:01:25 +0200433 }
Oliver Neukum28965e12015-03-20 14:29:18 +0100434
Oliver Neukum17d80d52008-06-24 15:56:10 +0200435 usb_autopm_put_interface(desc->intf);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100436 mutex_unlock(&desc->wlock);
Geliang Tang64b95332017-05-08 23:14:39 +0800437 return count;
Oliver Neukum28965e12015-03-20 14:29:18 +0100438
439out_free_mem_pm:
440 usb_autopm_put_interface(desc->intf);
441out_free_mem_lock:
442 mutex_unlock(&desc->wlock);
443out_free_mem:
444 kfree(buf);
445 return rv;
Oliver Neukumafba9372008-05-13 17:01:25 +0200446}
447
Bjørn Mork8dd5cd52013-12-20 14:07:24 +0100448/*
Robert Fossc1da59d2016-08-09 10:54:52 -0400449 * Submit the read urb if resp_count is non-zero.
Bjørn Mork8dd5cd52013-12-20 14:07:24 +0100450 *
451 * Called with desc->iuspin locked
452 */
Robert Fossc1da59d2016-08-09 10:54:52 -0400453static int service_outstanding_interrupt(struct wdm_device *desc)
Bjørn Mork8dd5cd52013-12-20 14:07:24 +0100454{
455 int rv = 0;
456
Bjørn Mork8dd5cd52013-12-20 14:07:24 +0100457 /* submit read urb only if the device is waiting for it */
Bjørn Morkf5639262014-01-12 21:48:53 +0100458 if (!desc->resp_count || !--desc->resp_count)
Bjørn Mork8dd5cd52013-12-20 14:07:24 +0100459 goto out;
460
461 set_bit(WDM_RESPONDING, &desc->flags);
462 spin_unlock_irq(&desc->iuspin);
Sebastian Andrzej Siewiore871db82018-09-11 10:00:44 +0200463 rv = usb_submit_urb(desc->response, GFP_KERNEL);
Bjørn Mork8dd5cd52013-12-20 14:07:24 +0100464 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 }
473out:
474 return rv;
475}
476
Oliver Neukumafba9372008-05-13 17:01:25 +0200477static ssize_t wdm_read
478(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
479{
Ben Hutchings711c68b2012-02-12 06:00:41 +0000480 int rv, cntr;
Oliver Neukumafba9372008-05-13 17:01:25 +0200481 int i = 0;
482 struct wdm_device *desc = file->private_data;
483
484
Bjørn Morke8537bd2012-01-16 12:41:48 +0100485 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
Oliver Neukumafba9372008-05-13 17:01:25 +0200486 if (rv < 0)
487 return -ERESTARTSYS;
488
Mark Rutland6aa7de02017-10-23 14:07:29 -0700489 cntr = READ_ONCE(desc->length);
Ben Hutchings711c68b2012-02-12 06:00:41 +0000490 if (cntr == 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200491 desc->read = 0;
492retry:
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200493 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
494 rv = -ENODEV;
495 goto err;
496 }
Oliver Neukumc0f5ece2013-03-12 14:52:42 +0100497 if (test_bit(WDM_OVERFLOW, &desc->flags)) {
498 clear_bit(WDM_OVERFLOW, &desc->flags);
499 rv = -ENOBUFS;
500 goto err;
501 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200502 i++;
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200503 if (file->f_flags & O_NONBLOCK) {
504 if (!test_bit(WDM_READ, &desc->flags)) {
Gustavo A. R. Silva53b7f7b2017-02-14 21:10:52 -0600505 rv = -EAGAIN;
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200506 goto err;
507 }
508 rv = 0;
509 } else {
510 rv = wait_event_interruptible(desc->wait,
511 test_bit(WDM_READ, &desc->flags));
512 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200513
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200514 /* may have happened while we slept */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200515 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
516 rv = -ENODEV;
517 goto err;
518 }
Bjørn Mork88044202012-02-10 09:44:08 +0100519 if (test_bit(WDM_RESETTING, &desc->flags)) {
520 rv = -EIO;
521 goto err;
522 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200523 usb_mark_last_busy(interface_to_usbdev(desc->intf));
Oliver Neukumafba9372008-05-13 17:01:25 +0200524 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 Neukum85e8a0b92015-03-23 14:34:43 +0100532 rv = usb_translate_errors(desc->rerr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200533 desc->rerr = 0;
534 spin_unlock_irq(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200535 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 Neukumc0f5ece2013-03-12 14:52:42 +0100545
Oliver Neukumafba9372008-05-13 17:01:25 +0200546 if (!desc->reslength) { /* zero length read */
Oliver Neukumce8bb342016-08-16 15:12:22 +0200547 dev_dbg(&desc->intf->dev, "zero length - clearing WDM_READ\n");
Robert Fossc1da59d2016-08-09 10:54:52 -0400548 clear_bit(WDM_READ, &desc->flags);
549 rv = service_outstanding_interrupt(desc);
Oliver Neukumafba9372008-05-13 17:01:25 +0200550 spin_unlock_irq(&desc->iuspin);
Bjørn Mork8dd5cd52013-12-20 14:07:24 +0100551 if (rv < 0)
552 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200553 goto retry;
554 }
Ben Hutchings711c68b2012-02-12 06:00:41 +0000555 cntr = desc->length;
Oliver Neukumafba9372008-05-13 17:01:25 +0200556 spin_unlock_irq(&desc->iuspin);
557 }
558
Ben Hutchings711c68b2012-02-12 06:00:41 +0000559 if (cntr > count)
560 cntr = count;
Oliver Neukumafba9372008-05-13 17:01:25 +0200561 rv = copy_to_user(buffer, desc->ubuf, cntr);
562 if (rv > 0) {
563 rv = -EFAULT;
564 goto err;
565 }
566
Ben Hutchings711c68b2012-02-12 06:00:41 +0000567 spin_lock_irq(&desc->iuspin);
568
Oliver Neukumafba9372008-05-13 17:01:25 +0200569 for (i = 0; i < desc->length - cntr; i++)
570 desc->ubuf[i] = desc->ubuf[i + cntr];
571
572 desc->length -= cntr;
Oliver Neukum87d65e52008-06-19 14:20:18 +0200573 /* in case we had outstanding data */
Robert Fossc1da59d2016-08-09 10:54:52 -0400574 if (!desc->length) {
575 clear_bit(WDM_READ, &desc->flags);
576 service_outstanding_interrupt(desc);
577 }
Bjørn Mork8dd5cd52013-12-20 14:07:24 +0100578 spin_unlock_irq(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200579 rv = cntr;
580
581err:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100582 mutex_unlock(&desc->rlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200583 return rv;
584}
585
586static 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 Mork6b0b79d2012-05-09 13:53:22 +0200591
592 /* cannot dereference desc->intf if WDM_DISCONNECTING */
593 if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags))
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700594 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
595 desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200596
Oliver Neukum24a85ba2012-04-27 14:23:54 +0200597 return usb_translate_errors(desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200598}
599
Al Viroafc9a422017-07-03 06:39:46 -0400600static __poll_t wdm_poll(struct file *file, struct poll_table_struct *wait)
Oliver Neukumafba9372008-05-13 17:01:25 +0200601{
602 struct wdm_device *desc = file->private_data;
603 unsigned long flags;
Al Viroafc9a422017-07-03 06:39:46 -0400604 __poll_t mask = 0;
Oliver Neukumafba9372008-05-13 17:01:25 +0200605
606 spin_lock_irqsave(&desc->iuspin, flags);
607 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800608 mask = EPOLLHUP | EPOLLERR;
Oliver Neukumafba9372008-05-13 17:01:25 +0200609 spin_unlock_irqrestore(&desc->iuspin, flags);
610 goto desc_out;
611 }
612 if (test_bit(WDM_READ, &desc->flags))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800613 mask = EPOLLIN | EPOLLRDNORM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200614 if (desc->rerr || desc->werr)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800615 mask |= EPOLLERR;
Oliver Neukumafba9372008-05-13 17:01:25 +0200616 if (!test_bit(WDM_IN_USE, &desc->flags))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800617 mask |= EPOLLOUT | EPOLLWRNORM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200618 spin_unlock_irqrestore(&desc->iuspin, flags);
619
620 poll_wait(file, &desc->wait, wait);
621
622desc_out:
623 return mask;
624}
625
626static 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 Morkb0c13862012-03-06 17:29:21 +0100634 desc = wdm_find_device_by_minor(minor);
635 if (!desc)
Oliver Neukumafba9372008-05-13 17:01:25 +0200636 goto out;
637
Bjørn Morkb0c13862012-03-06 17:29:21 +0100638 intf = desc->intf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200639 if (test_bit(WDM_DISCONNECTING, &desc->flags))
640 goto out;
Oliver Neukumafba9372008-05-13 17:01:25 +0200641 file->private_data = desc;
642
Oliver Neukum17d80d52008-06-24 15:56:10 +0200643 rv = usb_autopm_get_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200644 if (rv < 0) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700645 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200646 goto out;
647 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200648
Bjørn Morke8537bd2012-01-16 12:41:48 +0100649 /* using write lock to protect desc->count */
650 mutex_lock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200651 if (!desc->count++) {
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200652 desc->werr = 0;
653 desc->rerr = 0;
Oliver Neukum17d80d52008-06-24 15:56:10 +0200654 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
655 if (rv < 0) {
656 desc->count--;
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700657 dev_err(&desc->intf->dev,
658 "Error submitting int urb - %d\n", rv);
Oliver Neukum12a98b22012-04-30 09:57:31 +0200659 rv = usb_translate_errors(rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200660 }
661 } else {
662 rv = 0;
663 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100664 mutex_unlock(&desc->wlock);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100665 if (desc->count == 1)
666 desc->manage_power(intf, 1);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200667 usb_autopm_put_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200668out:
669 mutex_unlock(&wdm_mutex);
670 return rv;
671}
672
673static 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 Morke8537bd2012-01-16 12:41:48 +0100678
679 /* using write lock to protect desc->count */
680 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200681 desc->count--;
Bjørn Morke8537bd2012-01-16 12:41:48 +0100682 mutex_unlock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200683
Oliver Neukumafba9372008-05-13 17:01:25 +0200684 if (!desc->count) {
Bjørn Mork880bca32012-04-30 09:26:11 +0200685 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
Oliver Neukumce8bb342016-08-16 15:12:22 +0200686 dev_dbg(&desc->intf->dev, "wdm_release: cleanup\n");
Bjørn Mork6b0b79d2012-05-09 13:53:22 +0200687 kill_urbs(desc);
Greg Suarez73e06862013-10-29 10:29:10 -0700688 spin_lock_irq(&desc->iuspin);
689 desc->resp_count = 0;
690 spin_unlock_irq(&desc->iuspin);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100691 desc->manage_power(desc->intf, 0);
Bjørn Mork880bca32012-04-30 09:26:11 +0200692 } else {
Bjørn Mork6b0b79d2012-05-09 13:53:22 +0200693 /* must avoid dev_printk here as desc->intf is invalid */
694 pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
Oliver Neukum2f338c82012-04-27 14:36:37 +0200695 cleanup(desc);
Bjørn Mork880bca32012-04-30 09:26:11 +0200696 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200697 }
698 mutex_unlock(&wdm_mutex);
699 return 0;
700}
701
Bjørn Mork3edce1c2013-03-17 21:00:06 +0100702static 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 Neukumafba9372008-05-13 17:01:25 +0200718static 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 Bergmann6038f372010-08-15 18:52:59 +0200725 .poll = wdm_poll,
Bjørn Mork3edce1c2013-03-17 21:00:06 +0100726 .unlocked_ioctl = wdm_ioctl,
727 .compat_ioctl = wdm_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200728 .llseek = noop_llseek,
Oliver Neukumafba9372008-05-13 17:01:25 +0200729};
730
731static 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 --- */
738static 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 Neukum6dd433e2013-08-06 14:22:59 +0200742 int rv = 0;
743 int responding;
Oliver Neukumafba9372008-05-13 17:01:25 +0200744
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 Neukum6dd433e2013-08-06 14:22:59 +0200749 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200750 spin_unlock_irqrestore(&desc->iuspin, flags);
Oliver Neukum6dd433e2013-08-06 14:22:59 +0200751 if (!responding)
752 rv = usb_submit_urb(desc->response, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200753 if (rv < 0 && rv != -EPERM) {
754 spin_lock_irqsave(&desc->iuspin, flags);
Oliver Neukum6dd433e2013-08-06 14:22:59 +0200755 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200756 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 Siewior2df69482018-06-14 18:36:46 +0200763static 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 Neukumafba9372008-05-13 17:01:25 +0200778/* --- hotplug --- */
779
Bjørn Mork3cc36152012-03-06 17:29:22 +0100780static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
Bjørn Mork19445812017-04-21 10:01:29 +0200781 u16 bufsize, int (*manage_power)(struct usb_interface *, int))
Oliver Neukumafba9372008-05-13 17:01:25 +0200782{
Bjørn Mork0dffb482012-03-06 17:29:20 +0100783 int rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200784 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200785
Oliver Neukumafba9372008-05-13 17:01:25 +0200786 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
787 if (!desc)
788 goto out;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100789 INIT_LIST_HEAD(&desc->device_list);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100790 mutex_init(&desc->rlock);
791 mutex_init(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200792 spin_lock_init(&desc->iuspin);
793 init_waitqueue_head(&desc->wait);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100794 desc->wMaxCommand = bufsize;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200795 /* this will be expanded and needed in hardware endianness */
Oliver Neukumafba9372008-05-13 17:01:25 +0200796 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
797 desc->intf = intf;
798 INIT_WORK(&desc->rxwork, wdm_rxwork);
Sebastian Andrzej Siewior2df69482018-06-14 18:36:46 +0200799 INIT_WORK(&desc->service_outs_intr, service_interrupt_work);
Oliver Neukumafba9372008-05-13 17:01:25 +0200800
Oliver Neukum052fbc02009-04-20 17:24:49 +0200801 rv = -EINVAL;
Bjørn Mork0dffb482012-03-06 17:29:20 +0100802 if (!usb_endpoint_is_int_in(ep))
Oliver Neukum052fbc02009-04-20 17:24:49 +0200803 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200804
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700805 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
Oliver Neukumafba9372008-05-13 17:01:25 +0200806
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 Mork8457d992012-01-16 15:12:00 +0100830 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200831 if (!desc->sbuf)
832 goto err;
833
Bjørn Mork8457d992012-01-16 15:12:00 +0100834 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200835 if (!desc->inbuf)
Bjørn Mork8457d992012-01-16 15:12:00 +0100836 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200837
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 Neukumafba9372008-05-13 17:01:25 +0200848
Bjørn Mork19b85b32012-01-16 15:11:58 +0100849 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 Neukum323ece52015-03-20 14:29:34 +0100852 desc->irq->wIndex = desc->inum; /* already converted */
Bjørn Mork19b85b32012-01-16 15:11:58 +0100853 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
854
855 usb_fill_control_urb(
856 desc->response,
Bjørn Mork8143a892012-01-16 15:12:01 +0100857 interface_to_usbdev(intf),
Bjørn Mork19b85b32012-01-16 15:11:58 +0100858 /* 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 Mork19b85b32012-01-16 15:11:58 +0100866
Bjørn Mork3cc36152012-03-06 17:29:22 +0100867 desc->manage_power = manage_power;
868
Bjørn Morkb0c13862012-03-06 17:29:21 +0100869 spin_lock(&wdm_device_list_lock);
870 list_add(&desc->device_list, &wdm_device_list);
871 spin_unlock(&wdm_device_list_lock);
872
Oliver Neukumafba9372008-05-13 17:01:25 +0200873 rv = usb_register_dev(intf, &wdm_class);
Oliver Neukumafba9372008-05-13 17:01:25 +0200874 if (rv < 0)
Bjørn Morkb0c13862012-03-06 17:29:21 +0100875 goto err;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200876 else
Bjørn Mork820c6292012-01-20 04:17:25 +0100877 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
Oliver Neukumafba9372008-05-13 17:01:25 +0200878out:
879 return rv;
Oliver Neukumafba9372008-05-13 17:01:25 +0200880err:
Bjørn Mork6286d852012-05-09 13:53:23 +0200881 spin_lock(&wdm_device_list_lock);
882 list_del(&desc->device_list);
883 spin_unlock(&wdm_device_list_lock);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100884 cleanup(desc);
885 return rv;
886}
887
Bjørn Mork3cc36152012-03-06 17:29:22 +0100888static 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 Mork3cc36152012-03-06 17:29:22 +0100892
893 intf->needs_remote_wakeup = on;
Bjørn Mork4144bc82013-11-29 20:17:45 +0100894 if (!rv)
895 usb_autopm_put_interface(intf);
896 return 0;
Bjørn Mork3cc36152012-03-06 17:29:22 +0100897}
898
Bjørn Mork0dffb482012-03-06 17:29:20 +0100899static 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 Neukum7fae7bf2016-07-14 15:41:33 +0200904 struct usb_cdc_parsed_header hdr;
Bjørn Mork0dffb482012-03-06 17:29:20 +0100905 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 Mork0dffb482012-03-06 17:29:20 +0100911
Oliver Neukum7fae7bf2016-07-14 15:41:33 +0200912 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 Mork0dffb482012-03-06 17:29:20 +0100916
917 iface = intf->cur_altsetting;
918 if (iface->desc.bNumEndpoints != 1)
919 goto err;
920 ep = &iface->endpoint[0].desc;
921
Bjørn Mork19445812017-04-21 10:01:29 +0200922 rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100923
924err:
Oliver Neukumafba9372008-05-13 17:01:25 +0200925 return rv;
926}
927
Bjørn Mork3cc36152012-03-06 17:29:22 +0100928/**
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 */
947struct 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 Mork19445812017-04-21 10:01:29 +0200954 rv = wdm_create(intf, ep, bufsize, manage_power);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100955 if (rv < 0)
956 goto err;
957
958 return &wdm_driver;
959err:
960 return ERR_PTR(rv);
961}
962EXPORT_SYMBOL(usb_cdc_wdm_register);
963
Oliver Neukumafba9372008-05-13 17:01:25 +0200964static 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 Morkb0c13862012-03-06 17:29:21 +0100970 desc = wdm_find_device(intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200971 mutex_lock(&wdm_mutex);
Oliver Neukumafba9372008-05-13 17:01:25 +0200972
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 Neukum17d80d52008-06-24 15:56:10 +0200977 /* to terminate pending flushes */
Oliver Neukumafba9372008-05-13 17:01:25 +0200978 clear_bit(WDM_IN_USE, &desc->flags);
979 spin_unlock_irqrestore(&desc->iuspin, flags);
Bjørn Mork62aaf242012-01-16 15:11:57 +0100980 wake_up_all(&desc->wait);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100981 mutex_lock(&desc->rlock);
982 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200983 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100984 cancel_work_sync(&desc->rxwork);
Sebastian Andrzej Siewior2df69482018-06-14 18:36:46 +0200985 cancel_work_sync(&desc->service_outs_intr);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100986 mutex_unlock(&desc->wlock);
987 mutex_unlock(&desc->rlock);
Bjørn Mork6286d852012-05-09 13:53:23 +0200988
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 Neukumafba9372008-05-13 17:01:25 +0200994 if (!desc->count)
995 cleanup(desc);
Bjørn Mork880bca32012-04-30 09:26:11 +0200996 else
Oliver Neukum13a88bf2016-08-15 11:17:55 +0200997 dev_dbg(&intf->dev, "%d open files - postponing cleanup\n", desc->count);
Oliver Neukumafba9372008-05-13 17:01:25 +0200998 mutex_unlock(&wdm_mutex);
999}
1000
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001001#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +02001002static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
1003{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001004 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001005 int rv = 0;
1006
1007 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
1008
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001009 /* if this is an autosuspend the caller does the locking */
Bjørn Morke8537bd2012-01-16 12:41:48 +01001010 if (!PMSG_IS_AUTO(message)) {
1011 mutex_lock(&desc->rlock);
1012 mutex_lock(&desc->wlock);
1013 }
Oliver Neukum62e66852010-02-27 20:56:22 +01001014 spin_lock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001015
Alan Stern5b1b0b82011-08-19 23:49:48 +02001016 if (PMSG_IS_AUTO(message) &&
Oliver Neukum922a5ea2010-02-27 20:54:59 +01001017 (test_bit(WDM_IN_USE, &desc->flags)
1018 || test_bit(WDM_RESPONDING, &desc->flags))) {
Oliver Neukum62e66852010-02-27 20:56:22 +01001019 spin_unlock_irq(&desc->iuspin);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001020 rv = -EBUSY;
1021 } else {
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001022
Oliver Neukumbeb1d352010-02-27 20:55:52 +01001023 set_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +01001024 spin_unlock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001025 /* callback submits work - order is essential */
Oliver Neukum17d80d52008-06-24 15:56:10 +02001026 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001027 cancel_work_sync(&desc->rxwork);
Sebastian Andrzej Siewior2df69482018-06-14 18:36:46 +02001028 cancel_work_sync(&desc->service_outs_intr);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001029 }
Bjørn Morke8537bd2012-01-16 12:41:48 +01001030 if (!PMSG_IS_AUTO(message)) {
1031 mutex_unlock(&desc->wlock);
1032 mutex_unlock(&desc->rlock);
1033 }
Oliver Neukum17d80d52008-06-24 15:56:10 +02001034
1035 return rv;
1036}
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001037#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +02001038
1039static 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-Hartman9908a322008-08-14 09:37:34 -07001046 dev_err(&desc->intf->dev,
1047 "Error resume submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001048 }
1049 return rv;
1050}
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001051
1052#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +02001053static int wdm_resume(struct usb_interface *intf)
1054{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001055 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001056 int rv;
1057
1058 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
Oliver Neukum338124c2010-02-27 20:57:12 +01001059
Oliver Neukumbeb1d352010-02-27 20:55:52 +01001060 clear_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +01001061 rv = recover_from_urb_loss(desc);
Oliver Neukum338124c2010-02-27 20:57:12 +01001062
Oliver Neukum17d80d52008-06-24 15:56:10 +02001063 return rv;
1064}
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001065#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +02001066
1067static int wdm_pre_reset(struct usb_interface *intf)
1068{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001069 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001070
Oliver Neukumd771d8a2011-04-29 14:12:21 +02001071 /*
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 Mork88044202012-02-10 09:44:08 +01001078 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 Neukumd771d8a2011-04-29 14:12:21 +02001081 desc->rerr = -EINTR;
1082 spin_unlock_irq(&desc->iuspin);
1083 wake_up_all(&desc->wait);
Bjørn Mork88044202012-02-10 09:44:08 +01001084 mutex_lock(&desc->rlock);
1085 mutex_lock(&desc->wlock);
1086 kill_urbs(desc);
1087 cancel_work_sync(&desc->rxwork);
Sebastian Andrzej Siewior2df69482018-06-14 18:36:46 +02001088 cancel_work_sync(&desc->service_outs_intr);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001089 return 0;
1090}
1091
1092static int wdm_post_reset(struct usb_interface *intf)
1093{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001094 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001095 int rv;
1096
Oliver Neukumc0f5ece2013-03-12 14:52:42 +01001097 clear_bit(WDM_OVERFLOW, &desc->flags);
Bjørn Mork88044202012-02-10 09:44:08 +01001098 clear_bit(WDM_RESETTING, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001099 rv = recover_from_urb_loss(desc);
Bjørn Morke8537bd2012-01-16 12:41:48 +01001100 mutex_unlock(&desc->wlock);
1101 mutex_unlock(&desc->rlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001102 return 0;
1103}
1104
Oliver Neukumafba9372008-05-13 17:01:25 +02001105static struct usb_driver wdm_driver = {
1106 .name = "cdc_wdm",
1107 .probe = wdm_probe,
1108 .disconnect = wdm_disconnect,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001109#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +02001110 .suspend = wdm_suspend,
1111 .resume = wdm_resume,
1112 .reset_resume = wdm_resume,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001113#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +02001114 .pre_reset = wdm_pre_reset,
1115 .post_reset = wdm_post_reset,
Oliver Neukumafba9372008-05-13 17:01:25 +02001116 .id_table = wdm_ids,
Oliver Neukum17d80d52008-06-24 15:56:10 +02001117 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07001118 .disable_hub_initiated_lpm = 1,
Oliver Neukumafba9372008-05-13 17:01:25 +02001119};
1120
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001121module_usb_driver(wdm_driver);
Oliver Neukumafba9372008-05-13 17:01:25 +02001122
1123MODULE_AUTHOR(DRIVER_AUTHOR);
Oliver Neukum87d65e52008-06-19 14:20:18 +02001124MODULE_DESCRIPTION(DRIVER_DESC);
Oliver Neukumafba9372008-05-13 17:01:25 +02001125MODULE_LICENSE("GPL");