blob: 9f001659807ad6acf0c1fe7a15453c7b3bc770b5 [file] [log] [blame]
Oliver Neukumafba9372008-05-13 17:01:25 +02001/*
2 * cdc-wdm.c
3 *
4 * This driver supports USB CDC WCM Device Management.
5 *
Oliver Neukum052fbc02009-04-20 17:24:49 +02006 * Copyright (c) 2007-2009 Oliver Neukum
Oliver Neukumafba9372008-05-13 17:01:25 +02007 *
8 * Some code taken from cdc-acm.c
9 *
10 * Released under the GPLv2.
11 *
12 * Many thanks to Carl Nordbeck
13 */
14#include <linux/kernel.h>
15#include <linux/errno.h>
Bjørn Mork3edce1c2013-03-17 21:00:06 +010016#include <linux/ioctl.h>
Oliver Neukumafba9372008-05-13 17:01:25 +020017#include <linux/slab.h>
18#include <linux/module.h>
Oliver Neukumafba9372008-05-13 17:01:25 +020019#include <linux/mutex.h>
20#include <linux/uaccess.h>
21#include <linux/bitops.h>
22#include <linux/poll.h>
23#include <linux/usb.h>
24#include <linux/usb/cdc.h>
25#include <asm/byteorder.h>
26#include <asm/unaligned.h>
Bjørn Mork3cc36152012-03-06 17:29:22 +010027#include <linux/usb/cdc-wdm.h>
Oliver Neukumafba9372008-05-13 17:01:25 +020028
29/*
30 * Version Information
31 */
Oliver Neukum87d65e52008-06-19 14:20:18 +020032#define DRIVER_VERSION "v0.03"
Oliver Neukumafba9372008-05-13 17:01:25 +020033#define DRIVER_AUTHOR "Oliver Neukum"
Oliver Neukum87d65e52008-06-19 14:20:18 +020034#define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management"
Oliver Neukumafba9372008-05-13 17:01:25 +020035
Németh Márton6ef48522010-01-10 15:33:45 +010036static const struct usb_device_id wdm_ids[] = {
Oliver Neukumafba9372008-05-13 17:01:25 +020037 {
38 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
39 USB_DEVICE_ID_MATCH_INT_SUBCLASS,
40 .bInterfaceClass = USB_CLASS_COMM,
41 .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM
42 },
43 { }
44};
45
Oliver Neukumaa5380b2008-10-13 14:05:20 +020046MODULE_DEVICE_TABLE (usb, wdm_ids);
47
Oliver Neukumafba9372008-05-13 17:01:25 +020048#define WDM_MINOR_BASE 176
49
50
51#define WDM_IN_USE 1
52#define WDM_DISCONNECTING 2
53#define WDM_RESULT 3
54#define WDM_READ 4
55#define WDM_INT_STALL 5
56#define WDM_POLL_RUNNING 6
Oliver Neukum922a5ea2010-02-27 20:54:59 +010057#define WDM_RESPONDING 7
Oliver Neukumbeb1d352010-02-27 20:55:52 +010058#define WDM_SUSPENDING 8
Bjørn Mork88044202012-02-10 09:44:08 +010059#define WDM_RESETTING 9
Oliver Neukumc0f5ece2013-03-12 14:52:42 +010060#define WDM_OVERFLOW 10
Oliver Neukumafba9372008-05-13 17:01:25 +020061
62#define WDM_MAX 16
63
Bjørn Mork7e3054a2012-01-20 01:49:57 +010064/* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
65#define WDM_DEFAULT_BUFSIZE 256
Oliver Neukumafba9372008-05-13 17:01:25 +020066
67static DEFINE_MUTEX(wdm_mutex);
Bjørn Morkb0c13862012-03-06 17:29:21 +010068static DEFINE_SPINLOCK(wdm_device_list_lock);
69static LIST_HEAD(wdm_device_list);
Oliver Neukumafba9372008-05-13 17:01:25 +020070
71/* --- method tables --- */
72
73struct wdm_device {
74 u8 *inbuf; /* buffer for response */
75 u8 *outbuf; /* buffer for command */
76 u8 *sbuf; /* buffer for status */
77 u8 *ubuf; /* buffer for copy to user space */
78
79 struct urb *command;
80 struct urb *response;
81 struct urb *validity;
82 struct usb_interface *intf;
83 struct usb_ctrlrequest *orq;
84 struct usb_ctrlrequest *irq;
85 spinlock_t iuspin;
86
87 unsigned long flags;
88 u16 bufsize;
89 u16 wMaxCommand;
90 u16 wMaxPacketSize;
Oliver Neukumafba9372008-05-13 17:01:25 +020091 __le16 inum;
92 int reslength;
93 int length;
94 int read;
95 int count;
96 dma_addr_t shandle;
97 dma_addr_t ihandle;
Bjørn Morke8537bd2012-01-16 12:41:48 +010098 struct mutex wlock;
99 struct mutex rlock;
Oliver Neukumafba9372008-05-13 17:01:25 +0200100 wait_queue_head_t wait;
101 struct work_struct rxwork;
102 int werr;
103 int rerr;
Greg Suarez73e06862013-10-29 10:29:10 -0700104 int resp_count;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100105
106 struct list_head device_list;
Bjørn Mork3cc36152012-03-06 17:29:22 +0100107 int (*manage_power)(struct usb_interface *, int);
Oliver Neukumafba9372008-05-13 17:01:25 +0200108};
109
110static struct usb_driver wdm_driver;
111
Bjørn Morkb0c13862012-03-06 17:29:21 +0100112/* return intfdata if we own the interface, else look up intf in the list */
113static struct wdm_device *wdm_find_device(struct usb_interface *intf)
114{
Bjørn Mork6a448862012-09-10 22:17:34 +0200115 struct wdm_device *desc;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100116
117 spin_lock(&wdm_device_list_lock);
118 list_for_each_entry(desc, &wdm_device_list, device_list)
119 if (desc->intf == intf)
Bjørn Mork6a448862012-09-10 22:17:34 +0200120 goto found;
121 desc = NULL;
122found:
Bjørn Morkb0c13862012-03-06 17:29:21 +0100123 spin_unlock(&wdm_device_list_lock);
124
125 return desc;
126}
127
128static struct wdm_device *wdm_find_device_by_minor(int minor)
129{
Bjørn Mork6a448862012-09-10 22:17:34 +0200130 struct wdm_device *desc;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100131
132 spin_lock(&wdm_device_list_lock);
133 list_for_each_entry(desc, &wdm_device_list, device_list)
134 if (desc->intf->minor == minor)
Bjørn Mork6a448862012-09-10 22:17:34 +0200135 goto found;
136 desc = NULL;
137found:
Bjørn Morkb0c13862012-03-06 17:29:21 +0100138 spin_unlock(&wdm_device_list_lock);
139
140 return desc;
141}
142
Oliver Neukumafba9372008-05-13 17:01:25 +0200143/* --- callbacks --- */
144static void wdm_out_callback(struct urb *urb)
145{
146 struct wdm_device *desc;
147 desc = urb->context;
148 spin_lock(&desc->iuspin);
149 desc->werr = urb->status;
150 spin_unlock(&desc->iuspin);
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
Robert Fossc1da59d2016-08-09 10:54:52 -0400157/* forward declaration */
158static int service_outstanding_interrupt(struct wdm_device *desc);
159
Oliver Neukumafba9372008-05-13 17:01:25 +0200160static void wdm_in_callback(struct urb *urb)
161{
162 struct wdm_device *desc = urb->context;
163 int status = urb->status;
Oliver Neukumc0f5ece2013-03-12 14:52:42 +0100164 int length = urb->actual_length;
Oliver Neukumafba9372008-05-13 17:01:25 +0200165
166 spin_lock(&desc->iuspin);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100167 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200168
169 if (status) {
170 switch (status) {
171 case -ENOENT:
172 dev_dbg(&desc->intf->dev,
Oliver Neukumce8bb342016-08-16 15:12:22 +0200173 "nonzero urb status received: -ENOENT\n");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100174 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200175 case -ECONNRESET:
176 dev_dbg(&desc->intf->dev,
Oliver Neukumce8bb342016-08-16 15:12:22 +0200177 "nonzero urb status received: -ECONNRESET\n");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100178 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200179 case -ESHUTDOWN:
180 dev_dbg(&desc->intf->dev,
Oliver Neukumce8bb342016-08-16 15:12:22 +0200181 "nonzero urb status received: -ESHUTDOWN\n");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100182 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200183 case -EPIPE:
Bjørn Mork6312a842017-04-21 10:01:29 +0200184 dev_err(&desc->intf->dev,
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700185 "nonzero urb status received: -EPIPE\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200186 break;
187 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700188 dev_err(&desc->intf->dev,
189 "Unexpected error %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200190 break;
191 }
192 }
193
Robert Fossc1da59d2016-08-09 10:54:52 -0400194 /*
195 * only set a new error if there is no previous error.
196 * Errors are only cleared during read/open
Bjørn Mork12071de2017-09-22 22:18:18 +0200197 * Avoid propagating -EPIPE (stall) to userspace since it is
198 * better handled as an empty read
Robert Fossc1da59d2016-08-09 10:54:52 -0400199 */
Bjørn Mork12071de2017-09-22 22:18:18 +0200200 if (desc->rerr == 0 && status != -EPIPE)
Robert Fossc1da59d2016-08-09 10:54:52 -0400201 desc->rerr = status;
202
Oliver Neukumc0f5ece2013-03-12 14:52:42 +0100203 if (length + desc->length > desc->wMaxCommand) {
204 /* The buffer would overflow */
205 set_bit(WDM_OVERFLOW, &desc->flags);
206 } else {
207 /* we may already be in overflow */
208 if (!test_bit(WDM_OVERFLOW, &desc->flags)) {
209 memmove(desc->ubuf + desc->length, desc->inbuf, length);
210 desc->length += length;
211 desc->reslength = length;
212 }
213 }
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100214skip_error:
Robert Fossc1da59d2016-08-09 10:54:52 -0400215 set_bit(WDM_READ, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200216 wake_up(&desc->wait);
217
Robert Fossc1da59d2016-08-09 10:54:52 -0400218 if (desc->rerr) {
219 /*
220 * Since there was an error, userspace may decide to not read
221 * any data after poll'ing.
222 * We should respond to further attempts from the device to send
223 * data, so that we can get unstuck.
224 */
225 service_outstanding_interrupt(desc);
226 }
227
Oliver Neukumafba9372008-05-13 17:01:25 +0200228 spin_unlock(&desc->iuspin);
229}
230
231static void wdm_int_callback(struct urb *urb)
232{
233 int rv = 0;
Oliver Neukum6dd433e2013-08-06 14:22:59 +0200234 int responding;
Oliver Neukumafba9372008-05-13 17:01:25 +0200235 int status = urb->status;
236 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200237 struct usb_cdc_notification *dr;
238
239 desc = urb->context;
Oliver Neukumafba9372008-05-13 17:01:25 +0200240 dr = (struct usb_cdc_notification *)desc->sbuf;
241
242 if (status) {
243 switch (status) {
244 case -ESHUTDOWN:
245 case -ENOENT:
246 case -ECONNRESET:
247 return; /* unplug */
248 case -EPIPE:
249 set_bit(WDM_INT_STALL, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700250 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200251 goto sw; /* halt is cleared in work */
252 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700253 dev_err(&desc->intf->dev,
254 "nonzero urb status received: %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200255 break;
256 }
257 }
258
259 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700260 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
261 urb->actual_length);
Oliver Neukumafba9372008-05-13 17:01:25 +0200262 goto exit;
263 }
264
265 switch (dr->bNotificationType) {
266 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
267 dev_dbg(&desc->intf->dev,
Oliver Neukumce8bb342016-08-16 15:12:22 +0200268 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d\n",
Oliver Neukum323ece52015-03-20 14:29:34 +0100269 le16_to_cpu(dr->wIndex), le16_to_cpu(dr->wLength));
Oliver Neukumafba9372008-05-13 17:01:25 +0200270 break;
271
272 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
273
274 dev_dbg(&desc->intf->dev,
Oliver Neukumce8bb342016-08-16 15:12:22 +0200275 "NOTIFY_NETWORK_CONNECTION %s network\n",
Oliver Neukumafba9372008-05-13 17:01:25 +0200276 dr->wValue ? "connected to" : "disconnected from");
277 goto exit;
Bjørn Mork9983d6d2013-10-29 09:52:57 +0100278 case USB_CDC_NOTIFY_SPEED_CHANGE:
Oliver Neukumce8bb342016-08-16 15:12:22 +0200279 dev_dbg(&desc->intf->dev, "SPEED_CHANGE received (len %u)\n",
Bjørn Mork9983d6d2013-10-29 09:52:57 +0100280 urb->actual_length);
281 goto exit;
Oliver Neukumafba9372008-05-13 17:01:25 +0200282 default:
283 clear_bit(WDM_POLL_RUNNING, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700284 dev_err(&desc->intf->dev,
285 "unknown notification %d received: index %d len %d\n",
Oliver Neukum323ece52015-03-20 14:29:34 +0100286 dr->bNotificationType,
287 le16_to_cpu(dr->wIndex),
288 le16_to_cpu(dr->wLength));
Oliver Neukumafba9372008-05-13 17:01:25 +0200289 goto exit;
290 }
291
Oliver Neukumafba9372008-05-13 17:01:25 +0200292 spin_lock(&desc->iuspin);
Oliver Neukum6dd433e2013-08-06 14:22:59 +0200293 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
Greg Suarez73e06862013-10-29 10:29:10 -0700294 if (!desc->resp_count++ && !responding
295 && !test_bit(WDM_DISCONNECTING, &desc->flags)
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100296 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200297 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
Oliver Neukumce8bb342016-08-16 15:12:22 +0200298 dev_dbg(&desc->intf->dev, "submit response URB %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200299 }
300 spin_unlock(&desc->iuspin);
301 if (rv < 0) {
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100302 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200303 if (rv == -EPERM)
304 return;
305 if (rv == -ENOMEM) {
306sw:
307 rv = schedule_work(&desc->rxwork);
308 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700309 dev_err(&desc->intf->dev,
310 "Cannot schedule work\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200311 }
312 }
313exit:
314 rv = usb_submit_urb(urb, GFP_ATOMIC);
315 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700316 dev_err(&desc->intf->dev,
317 "%s - usb_submit_urb failed with result %d\n",
318 __func__, rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200319
320}
321
322static void kill_urbs(struct wdm_device *desc)
323{
Oliver Neukum17d80d52008-06-24 15:56:10 +0200324 /* the order here is essential */
Oliver Neukumafba9372008-05-13 17:01:25 +0200325 usb_kill_urb(desc->command);
326 usb_kill_urb(desc->validity);
327 usb_kill_urb(desc->response);
328}
329
330static void free_urbs(struct wdm_device *desc)
331{
332 usb_free_urb(desc->validity);
333 usb_free_urb(desc->response);
334 usb_free_urb(desc->command);
335}
336
337static void cleanup(struct wdm_device *desc)
338{
Bjørn Mork8457d992012-01-16 15:12:00 +0100339 kfree(desc->sbuf);
340 kfree(desc->inbuf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200341 kfree(desc->orq);
342 kfree(desc->irq);
343 kfree(desc->ubuf);
344 free_urbs(desc);
345 kfree(desc);
346}
347
348static ssize_t wdm_write
349(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
350{
351 u8 *buf;
352 int rv = -EMSGSIZE, r, we;
353 struct wdm_device *desc = file->private_data;
354 struct usb_ctrlrequest *req;
355
356 if (count > desc->wMaxCommand)
357 count = desc->wMaxCommand;
358
359 spin_lock_irq(&desc->iuspin);
360 we = desc->werr;
361 desc->werr = 0;
362 spin_unlock_irq(&desc->iuspin);
363 if (we < 0)
Oliver Neukum76cb03e2015-03-20 14:28:56 +0100364 return usb_translate_errors(we);
Oliver Neukumafba9372008-05-13 17:01:25 +0200365
Oliver Neukum5c228372012-04-26 21:59:10 +0200366 buf = kmalloc(count, GFP_KERNEL);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100367 if (!buf) {
368 rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200369 goto outnl;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100370 }
371
372 r = copy_from_user(buf, buffer, count);
373 if (r > 0) {
Oliver Neukum860e41a2010-02-27 20:54:24 +0100374 rv = -EFAULT;
Oliver Neukum28965e12015-03-20 14:29:18 +0100375 goto out_free_mem;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100376 }
377
378 /* concurrent writes and disconnect */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100379 r = mutex_lock_interruptible(&desc->wlock);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100380 rv = -ERESTARTSYS;
Oliver Neukum28965e12015-03-20 14:29:18 +0100381 if (r)
382 goto out_free_mem;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100383
384 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
Oliver Neukum860e41a2010-02-27 20:54:24 +0100385 rv = -ENODEV;
Oliver Neukum28965e12015-03-20 14:29:18 +0100386 goto out_free_mem_lock;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100387 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200388
Oliver Neukum17d80d52008-06-24 15:56:10 +0200389 r = usb_autopm_get_interface(desc->intf);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100390 if (r < 0) {
Oliver Neukum12a98b22012-04-30 09:57:31 +0200391 rv = usb_translate_errors(r);
Oliver Neukum28965e12015-03-20 14:29:18 +0100392 goto out_free_mem_lock;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100393 }
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200394
David Sterba0cdfb812010-12-27 18:49:58 +0100395 if (!(file->f_flags & O_NONBLOCK))
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200396 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
397 &desc->flags));
398 else
399 if (test_bit(WDM_IN_USE, &desc->flags))
400 r = -EAGAIN;
Bjørn Mork88044202012-02-10 09:44:08 +0100401
402 if (test_bit(WDM_RESETTING, &desc->flags))
403 r = -EIO;
404
Oliver Neukum860e41a2010-02-27 20:54:24 +0100405 if (r < 0) {
Oliver Neukum12a98b22012-04-30 09:57:31 +0200406 rv = r;
Oliver Neukum28965e12015-03-20 14:29:18 +0100407 goto out_free_mem_pm;
Oliver Neukumafba9372008-05-13 17:01:25 +0200408 }
409
410 req = desc->orq;
411 usb_fill_control_urb(
412 desc->command,
413 interface_to_usbdev(desc->intf),
414 /* using common endpoint 0 */
415 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
416 (unsigned char *)req,
417 buf,
418 count,
419 wdm_out_callback,
420 desc
421 );
422
423 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
424 USB_RECIP_INTERFACE);
425 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
426 req->wValue = 0;
Oliver Neukum323ece52015-03-20 14:29:34 +0100427 req->wIndex = desc->inum; /* already converted */
Oliver Neukumafba9372008-05-13 17:01:25 +0200428 req->wLength = cpu_to_le16(count);
429 set_bit(WDM_IN_USE, &desc->flags);
Oliver Neukum5c228372012-04-26 21:59:10 +0200430 desc->outbuf = buf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200431
432 rv = usb_submit_urb(desc->command, GFP_KERNEL);
433 if (rv < 0) {
Oliver Neukum5c228372012-04-26 21:59:10 +0200434 desc->outbuf = NULL;
Oliver Neukumafba9372008-05-13 17:01:25 +0200435 clear_bit(WDM_IN_USE, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700436 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
Oliver Neukum12a98b22012-04-30 09:57:31 +0200437 rv = usb_translate_errors(rv);
Oliver Neukum28965e12015-03-20 14:29:18 +0100438 goto out_free_mem_pm;
Oliver Neukumafba9372008-05-13 17:01:25 +0200439 } else {
Oliver Neukumce8bb342016-08-16 15:12:22 +0200440 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d\n",
Oliver Neukum323ece52015-03-20 14:29:34 +0100441 le16_to_cpu(req->wIndex));
Oliver Neukumafba9372008-05-13 17:01:25 +0200442 }
Oliver Neukum28965e12015-03-20 14:29:18 +0100443
Oliver Neukum17d80d52008-06-24 15:56:10 +0200444 usb_autopm_put_interface(desc->intf);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100445 mutex_unlock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200446outnl:
447 return rv < 0 ? rv : count;
Oliver Neukum28965e12015-03-20 14:29:18 +0100448
449out_free_mem_pm:
450 usb_autopm_put_interface(desc->intf);
451out_free_mem_lock:
452 mutex_unlock(&desc->wlock);
453out_free_mem:
454 kfree(buf);
455 return rv;
Oliver Neukumafba9372008-05-13 17:01:25 +0200456}
457
Bjørn Mork8dd5cd52013-12-20 14:07:24 +0100458/*
Robert Fossc1da59d2016-08-09 10:54:52 -0400459 * Submit the read urb if resp_count is non-zero.
Bjørn Mork8dd5cd52013-12-20 14:07:24 +0100460 *
461 * Called with desc->iuspin locked
462 */
Robert Fossc1da59d2016-08-09 10:54:52 -0400463static int service_outstanding_interrupt(struct wdm_device *desc)
Bjørn Mork8dd5cd52013-12-20 14:07:24 +0100464{
465 int rv = 0;
466
Bjørn Mork8dd5cd52013-12-20 14:07:24 +0100467 /* submit read urb only if the device is waiting for it */
Bjørn Morkf5639262014-01-12 21:48:53 +0100468 if (!desc->resp_count || !--desc->resp_count)
Bjørn Mork8dd5cd52013-12-20 14:07:24 +0100469 goto out;
470
471 set_bit(WDM_RESPONDING, &desc->flags);
472 spin_unlock_irq(&desc->iuspin);
Sebastian Andrzej Siewior5c82d4a2018-09-11 10:00:44 +0200473 rv = usb_submit_urb(desc->response, GFP_KERNEL);
Bjørn Mork8dd5cd52013-12-20 14:07:24 +0100474 spin_lock_irq(&desc->iuspin);
475 if (rv) {
476 dev_err(&desc->intf->dev,
477 "usb_submit_urb failed with result %d\n", rv);
478
479 /* make sure the next notification trigger a submit */
480 clear_bit(WDM_RESPONDING, &desc->flags);
481 desc->resp_count = 0;
482 }
483out:
484 return rv;
485}
486
Oliver Neukumafba9372008-05-13 17:01:25 +0200487static ssize_t wdm_read
488(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
489{
Ben Hutchings711c68b2012-02-12 06:00:41 +0000490 int rv, cntr;
Oliver Neukumafba9372008-05-13 17:01:25 +0200491 int i = 0;
492 struct wdm_device *desc = file->private_data;
493
494
Bjørn Morke8537bd2012-01-16 12:41:48 +0100495 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
Oliver Neukumafba9372008-05-13 17:01:25 +0200496 if (rv < 0)
497 return -ERESTARTSYS;
498
Ben Hutchings711c68b2012-02-12 06:00:41 +0000499 cntr = ACCESS_ONCE(desc->length);
500 if (cntr == 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200501 desc->read = 0;
502retry:
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200503 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
504 rv = -ENODEV;
505 goto err;
506 }
Oliver Neukumc0f5ece2013-03-12 14:52:42 +0100507 if (test_bit(WDM_OVERFLOW, &desc->flags)) {
508 clear_bit(WDM_OVERFLOW, &desc->flags);
509 rv = -ENOBUFS;
510 goto err;
511 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200512 i++;
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200513 if (file->f_flags & O_NONBLOCK) {
514 if (!test_bit(WDM_READ, &desc->flags)) {
515 rv = cntr ? cntr : -EAGAIN;
516 goto err;
517 }
518 rv = 0;
519 } else {
520 rv = wait_event_interruptible(desc->wait,
521 test_bit(WDM_READ, &desc->flags));
522 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200523
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200524 /* may have happened while we slept */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200525 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
526 rv = -ENODEV;
527 goto err;
528 }
Bjørn Mork88044202012-02-10 09:44:08 +0100529 if (test_bit(WDM_RESETTING, &desc->flags)) {
530 rv = -EIO;
531 goto err;
532 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200533 usb_mark_last_busy(interface_to_usbdev(desc->intf));
Oliver Neukumafba9372008-05-13 17:01:25 +0200534 if (rv < 0) {
535 rv = -ERESTARTSYS;
536 goto err;
537 }
538
539 spin_lock_irq(&desc->iuspin);
540
541 if (desc->rerr) { /* read completed, error happened */
Oliver Neukum85e8a0b92015-03-23 14:34:43 +0100542 rv = usb_translate_errors(desc->rerr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200543 desc->rerr = 0;
544 spin_unlock_irq(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200545 goto err;
546 }
547 /*
548 * recheck whether we've lost the race
549 * against the completion handler
550 */
551 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
552 spin_unlock_irq(&desc->iuspin);
553 goto retry;
554 }
Oliver Neukumc0f5ece2013-03-12 14:52:42 +0100555
Oliver Neukumafba9372008-05-13 17:01:25 +0200556 if (!desc->reslength) { /* zero length read */
Oliver Neukumce8bb342016-08-16 15:12:22 +0200557 dev_dbg(&desc->intf->dev, "zero length - clearing WDM_READ\n");
Robert Fossc1da59d2016-08-09 10:54:52 -0400558 clear_bit(WDM_READ, &desc->flags);
559 rv = service_outstanding_interrupt(desc);
Oliver Neukumafba9372008-05-13 17:01:25 +0200560 spin_unlock_irq(&desc->iuspin);
Bjørn Mork8dd5cd52013-12-20 14:07:24 +0100561 if (rv < 0)
562 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200563 goto retry;
564 }
Ben Hutchings711c68b2012-02-12 06:00:41 +0000565 cntr = desc->length;
Oliver Neukumafba9372008-05-13 17:01:25 +0200566 spin_unlock_irq(&desc->iuspin);
567 }
568
Ben Hutchings711c68b2012-02-12 06:00:41 +0000569 if (cntr > count)
570 cntr = count;
Oliver Neukumafba9372008-05-13 17:01:25 +0200571 rv = copy_to_user(buffer, desc->ubuf, cntr);
572 if (rv > 0) {
573 rv = -EFAULT;
574 goto err;
575 }
576
Ben Hutchings711c68b2012-02-12 06:00:41 +0000577 spin_lock_irq(&desc->iuspin);
578
Oliver Neukumafba9372008-05-13 17:01:25 +0200579 for (i = 0; i < desc->length - cntr; i++)
580 desc->ubuf[i] = desc->ubuf[i + cntr];
581
582 desc->length -= cntr;
Oliver Neukum87d65e52008-06-19 14:20:18 +0200583 /* in case we had outstanding data */
Robert Fossc1da59d2016-08-09 10:54:52 -0400584 if (!desc->length) {
585 clear_bit(WDM_READ, &desc->flags);
586 service_outstanding_interrupt(desc);
587 }
Bjørn Mork8dd5cd52013-12-20 14:07:24 +0100588 spin_unlock_irq(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200589 rv = cntr;
590
591err:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100592 mutex_unlock(&desc->rlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200593 return rv;
594}
595
596static int wdm_flush(struct file *file, fl_owner_t id)
597{
598 struct wdm_device *desc = file->private_data;
599
600 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
Bjørn Mork6b0b79d2012-05-09 13:53:22 +0200601
602 /* cannot dereference desc->intf if WDM_DISCONNECTING */
603 if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags))
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700604 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
605 desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200606
Oliver Neukum24a85ba2012-04-27 14:23:54 +0200607 return usb_translate_errors(desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200608}
609
610static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
611{
612 struct wdm_device *desc = file->private_data;
613 unsigned long flags;
614 unsigned int mask = 0;
615
616 spin_lock_irqsave(&desc->iuspin, flags);
617 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
Bjørn Mork616b6932012-05-09 13:53:21 +0200618 mask = POLLHUP | POLLERR;
Oliver Neukumafba9372008-05-13 17:01:25 +0200619 spin_unlock_irqrestore(&desc->iuspin, flags);
620 goto desc_out;
621 }
622 if (test_bit(WDM_READ, &desc->flags))
623 mask = POLLIN | POLLRDNORM;
624 if (desc->rerr || desc->werr)
625 mask |= POLLERR;
626 if (!test_bit(WDM_IN_USE, &desc->flags))
627 mask |= POLLOUT | POLLWRNORM;
628 spin_unlock_irqrestore(&desc->iuspin, flags);
629
630 poll_wait(file, &desc->wait, wait);
631
632desc_out:
633 return mask;
634}
635
636static int wdm_open(struct inode *inode, struct file *file)
637{
638 int minor = iminor(inode);
639 int rv = -ENODEV;
640 struct usb_interface *intf;
641 struct wdm_device *desc;
642
643 mutex_lock(&wdm_mutex);
Bjørn Morkb0c13862012-03-06 17:29:21 +0100644 desc = wdm_find_device_by_minor(minor);
645 if (!desc)
Oliver Neukumafba9372008-05-13 17:01:25 +0200646 goto out;
647
Bjørn Morkb0c13862012-03-06 17:29:21 +0100648 intf = desc->intf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200649 if (test_bit(WDM_DISCONNECTING, &desc->flags))
650 goto out;
Oliver Neukumafba9372008-05-13 17:01:25 +0200651 file->private_data = desc;
652
Oliver Neukum17d80d52008-06-24 15:56:10 +0200653 rv = usb_autopm_get_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200654 if (rv < 0) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700655 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200656 goto out;
657 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200658
Bjørn Morke8537bd2012-01-16 12:41:48 +0100659 /* using write lock to protect desc->count */
660 mutex_lock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200661 if (!desc->count++) {
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200662 desc->werr = 0;
663 desc->rerr = 0;
Oliver Neukum17d80d52008-06-24 15:56:10 +0200664 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
665 if (rv < 0) {
666 desc->count--;
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700667 dev_err(&desc->intf->dev,
668 "Error submitting int urb - %d\n", rv);
Oliver Neukum12a98b22012-04-30 09:57:31 +0200669 rv = usb_translate_errors(rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200670 }
671 } else {
672 rv = 0;
673 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100674 mutex_unlock(&desc->wlock);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100675 if (desc->count == 1)
676 desc->manage_power(intf, 1);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200677 usb_autopm_put_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200678out:
679 mutex_unlock(&wdm_mutex);
680 return rv;
681}
682
683static int wdm_release(struct inode *inode, struct file *file)
684{
685 struct wdm_device *desc = file->private_data;
686
687 mutex_lock(&wdm_mutex);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100688
689 /* using write lock to protect desc->count */
690 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200691 desc->count--;
Bjørn Morke8537bd2012-01-16 12:41:48 +0100692 mutex_unlock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200693
Oliver Neukumafba9372008-05-13 17:01:25 +0200694 if (!desc->count) {
Bjørn Mork880bca32012-04-30 09:26:11 +0200695 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
Oliver Neukumce8bb342016-08-16 15:12:22 +0200696 dev_dbg(&desc->intf->dev, "wdm_release: cleanup\n");
Bjørn Mork6b0b79d2012-05-09 13:53:22 +0200697 kill_urbs(desc);
Greg Suarez73e06862013-10-29 10:29:10 -0700698 spin_lock_irq(&desc->iuspin);
699 desc->resp_count = 0;
700 spin_unlock_irq(&desc->iuspin);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100701 desc->manage_power(desc->intf, 0);
Bjørn Mork880bca32012-04-30 09:26:11 +0200702 } else {
Bjørn Mork6b0b79d2012-05-09 13:53:22 +0200703 /* must avoid dev_printk here as desc->intf is invalid */
704 pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
Oliver Neukum2f338c82012-04-27 14:36:37 +0200705 cleanup(desc);
Bjørn Mork880bca32012-04-30 09:26:11 +0200706 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200707 }
708 mutex_unlock(&wdm_mutex);
709 return 0;
710}
711
Bjørn Mork3edce1c2013-03-17 21:00:06 +0100712static long wdm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
713{
714 struct wdm_device *desc = file->private_data;
715 int rv = 0;
716
717 switch (cmd) {
718 case IOCTL_WDM_MAX_COMMAND:
719 if (copy_to_user((void __user *)arg, &desc->wMaxCommand, sizeof(desc->wMaxCommand)))
720 rv = -EFAULT;
721 break;
722 default:
723 rv = -ENOTTY;
724 }
725 return rv;
726}
727
Oliver Neukumafba9372008-05-13 17:01:25 +0200728static const struct file_operations wdm_fops = {
729 .owner = THIS_MODULE,
730 .read = wdm_read,
731 .write = wdm_write,
732 .open = wdm_open,
733 .flush = wdm_flush,
734 .release = wdm_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200735 .poll = wdm_poll,
Bjørn Mork3edce1c2013-03-17 21:00:06 +0100736 .unlocked_ioctl = wdm_ioctl,
737 .compat_ioctl = wdm_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200738 .llseek = noop_llseek,
Oliver Neukumafba9372008-05-13 17:01:25 +0200739};
740
741static struct usb_class_driver wdm_class = {
742 .name = "cdc-wdm%d",
743 .fops = &wdm_fops,
744 .minor_base = WDM_MINOR_BASE,
745};
746
747/* --- error handling --- */
748static void wdm_rxwork(struct work_struct *work)
749{
750 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
751 unsigned long flags;
Oliver Neukum6dd433e2013-08-06 14:22:59 +0200752 int rv = 0;
753 int responding;
Oliver Neukumafba9372008-05-13 17:01:25 +0200754
755 spin_lock_irqsave(&desc->iuspin, flags);
756 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
757 spin_unlock_irqrestore(&desc->iuspin, flags);
758 } else {
Oliver Neukum6dd433e2013-08-06 14:22:59 +0200759 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200760 spin_unlock_irqrestore(&desc->iuspin, flags);
Oliver Neukum6dd433e2013-08-06 14:22:59 +0200761 if (!responding)
762 rv = usb_submit_urb(desc->response, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200763 if (rv < 0 && rv != -EPERM) {
764 spin_lock_irqsave(&desc->iuspin, flags);
Oliver Neukum6dd433e2013-08-06 14:22:59 +0200765 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200766 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
767 schedule_work(&desc->rxwork);
768 spin_unlock_irqrestore(&desc->iuspin, flags);
769 }
770 }
771}
772
773/* --- hotplug --- */
774
Bjørn Mork3cc36152012-03-06 17:29:22 +0100775static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
Bjørn Mork6312a842017-04-21 10:01:29 +0200776 u16 bufsize, int (*manage_power)(struct usb_interface *, int))
Oliver Neukumafba9372008-05-13 17:01:25 +0200777{
Bjørn Mork0dffb482012-03-06 17:29:20 +0100778 int rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200779 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200780
Oliver Neukumafba9372008-05-13 17:01:25 +0200781 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
782 if (!desc)
783 goto out;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100784 INIT_LIST_HEAD(&desc->device_list);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100785 mutex_init(&desc->rlock);
786 mutex_init(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200787 spin_lock_init(&desc->iuspin);
788 init_waitqueue_head(&desc->wait);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100789 desc->wMaxCommand = bufsize;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200790 /* this will be expanded and needed in hardware endianness */
Oliver Neukumafba9372008-05-13 17:01:25 +0200791 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
792 desc->intf = intf;
793 INIT_WORK(&desc->rxwork, wdm_rxwork);
794
Oliver Neukum052fbc02009-04-20 17:24:49 +0200795 rv = -EINVAL;
Bjørn Mork0dffb482012-03-06 17:29:20 +0100796 if (!usb_endpoint_is_int_in(ep))
Oliver Neukum052fbc02009-04-20 17:24:49 +0200797 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200798
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700799 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
Oliver Neukumafba9372008-05-13 17:01:25 +0200800
801 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
802 if (!desc->orq)
803 goto err;
804 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
805 if (!desc->irq)
806 goto err;
807
808 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
809 if (!desc->validity)
810 goto err;
811
812 desc->response = usb_alloc_urb(0, GFP_KERNEL);
813 if (!desc->response)
814 goto err;
815
816 desc->command = usb_alloc_urb(0, GFP_KERNEL);
817 if (!desc->command)
818 goto err;
819
820 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
821 if (!desc->ubuf)
822 goto err;
823
Bjørn Mork8457d992012-01-16 15:12:00 +0100824 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200825 if (!desc->sbuf)
826 goto err;
827
Bjørn Mork8457d992012-01-16 15:12:00 +0100828 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200829 if (!desc->inbuf)
Bjørn Mork8457d992012-01-16 15:12:00 +0100830 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200831
832 usb_fill_int_urb(
833 desc->validity,
834 interface_to_usbdev(intf),
835 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
836 desc->sbuf,
837 desc->wMaxPacketSize,
838 wdm_int_callback,
839 desc,
840 ep->bInterval
841 );
Oliver Neukumafba9372008-05-13 17:01:25 +0200842
Bjørn Mork19b85b32012-01-16 15:11:58 +0100843 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
844 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
845 desc->irq->wValue = 0;
Oliver Neukum323ece52015-03-20 14:29:34 +0100846 desc->irq->wIndex = desc->inum; /* already converted */
Bjørn Mork19b85b32012-01-16 15:11:58 +0100847 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
848
849 usb_fill_control_urb(
850 desc->response,
Bjørn Mork8143a892012-01-16 15:12:01 +0100851 interface_to_usbdev(intf),
Bjørn Mork19b85b32012-01-16 15:11:58 +0100852 /* using common endpoint 0 */
853 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
854 (unsigned char *)desc->irq,
855 desc->inbuf,
856 desc->wMaxCommand,
857 wdm_in_callback,
858 desc
859 );
Bjørn Mork19b85b32012-01-16 15:11:58 +0100860
Bjørn Mork3cc36152012-03-06 17:29:22 +0100861 desc->manage_power = manage_power;
862
Bjørn Morkb0c13862012-03-06 17:29:21 +0100863 spin_lock(&wdm_device_list_lock);
864 list_add(&desc->device_list, &wdm_device_list);
865 spin_unlock(&wdm_device_list_lock);
866
Oliver Neukumafba9372008-05-13 17:01:25 +0200867 rv = usb_register_dev(intf, &wdm_class);
Oliver Neukumafba9372008-05-13 17:01:25 +0200868 if (rv < 0)
Bjørn Morkb0c13862012-03-06 17:29:21 +0100869 goto err;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200870 else
Bjørn Mork820c6292012-01-20 04:17:25 +0100871 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
Oliver Neukumafba9372008-05-13 17:01:25 +0200872out:
873 return rv;
Oliver Neukumafba9372008-05-13 17:01:25 +0200874err:
Bjørn Mork6286d852012-05-09 13:53:23 +0200875 spin_lock(&wdm_device_list_lock);
876 list_del(&desc->device_list);
877 spin_unlock(&wdm_device_list_lock);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100878 cleanup(desc);
879 return rv;
880}
881
Bjørn Mork3cc36152012-03-06 17:29:22 +0100882static int wdm_manage_power(struct usb_interface *intf, int on)
883{
884 /* need autopm_get/put here to ensure the usbcore sees the new value */
885 int rv = usb_autopm_get_interface(intf);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100886
887 intf->needs_remote_wakeup = on;
Bjørn Mork4144bc82013-11-29 20:17:45 +0100888 if (!rv)
889 usb_autopm_put_interface(intf);
890 return 0;
Bjørn Mork3cc36152012-03-06 17:29:22 +0100891}
892
Bjørn Mork0dffb482012-03-06 17:29:20 +0100893static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
894{
895 int rv = -EINVAL;
896 struct usb_host_interface *iface;
897 struct usb_endpoint_descriptor *ep;
Oliver Neukum7fae7bf2016-07-14 15:41:33 +0200898 struct usb_cdc_parsed_header hdr;
Bjørn Mork0dffb482012-03-06 17:29:20 +0100899 u8 *buffer = intf->altsetting->extra;
900 int buflen = intf->altsetting->extralen;
901 u16 maxcom = WDM_DEFAULT_BUFSIZE;
902
903 if (!buffer)
904 goto err;
Bjørn Mork0dffb482012-03-06 17:29:20 +0100905
Oliver Neukum7fae7bf2016-07-14 15:41:33 +0200906 cdc_parse_cdc_header(&hdr, intf, buffer, buflen);
907
908 if (hdr.usb_cdc_dmm_desc)
909 maxcom = le16_to_cpu(hdr.usb_cdc_dmm_desc->wMaxCommand);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100910
911 iface = intf->cur_altsetting;
912 if (iface->desc.bNumEndpoints != 1)
913 goto err;
914 ep = &iface->endpoint[0].desc;
915
Bjørn Mork6312a842017-04-21 10:01:29 +0200916 rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100917
918err:
Oliver Neukumafba9372008-05-13 17:01:25 +0200919 return rv;
920}
921
Bjørn Mork3cc36152012-03-06 17:29:22 +0100922/**
923 * usb_cdc_wdm_register - register a WDM subdriver
924 * @intf: usb interface the subdriver will associate with
925 * @ep: interrupt endpoint to monitor for notifications
926 * @bufsize: maximum message size to support for read/write
927 *
928 * Create WDM usb class character device and associate it with intf
929 * without binding, allowing another driver to manage the interface.
930 *
931 * The subdriver will manage the given interrupt endpoint exclusively
932 * and will issue control requests referring to the given intf. It
933 * will otherwise avoid interferring, and in particular not do
934 * usb_set_intfdata/usb_get_intfdata on intf.
935 *
936 * The return value is a pointer to the subdriver's struct usb_driver.
937 * The registering driver is responsible for calling this subdriver's
938 * disconnect, suspend, resume, pre_reset and post_reset methods from
939 * its own.
940 */
941struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
942 struct usb_endpoint_descriptor *ep,
943 int bufsize,
944 int (*manage_power)(struct usb_interface *, int))
945{
946 int rv = -EINVAL;
947
Bjørn Mork6312a842017-04-21 10:01:29 +0200948 rv = wdm_create(intf, ep, bufsize, manage_power);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100949 if (rv < 0)
950 goto err;
951
952 return &wdm_driver;
953err:
954 return ERR_PTR(rv);
955}
956EXPORT_SYMBOL(usb_cdc_wdm_register);
957
Oliver Neukumafba9372008-05-13 17:01:25 +0200958static void wdm_disconnect(struct usb_interface *intf)
959{
960 struct wdm_device *desc;
961 unsigned long flags;
962
963 usb_deregister_dev(intf, &wdm_class);
Bjørn Morkb0c13862012-03-06 17:29:21 +0100964 desc = wdm_find_device(intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200965 mutex_lock(&wdm_mutex);
Oliver Neukumafba9372008-05-13 17:01:25 +0200966
967 /* the spinlock makes sure no new urbs are generated in the callbacks */
968 spin_lock_irqsave(&desc->iuspin, flags);
969 set_bit(WDM_DISCONNECTING, &desc->flags);
970 set_bit(WDM_READ, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200971 /* to terminate pending flushes */
Oliver Neukumafba9372008-05-13 17:01:25 +0200972 clear_bit(WDM_IN_USE, &desc->flags);
973 spin_unlock_irqrestore(&desc->iuspin, flags);
Bjørn Mork62aaf242012-01-16 15:11:57 +0100974 wake_up_all(&desc->wait);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100975 mutex_lock(&desc->rlock);
976 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200977 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100978 cancel_work_sync(&desc->rxwork);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100979 mutex_unlock(&desc->wlock);
980 mutex_unlock(&desc->rlock);
Bjørn Mork6286d852012-05-09 13:53:23 +0200981
982 /* the desc->intf pointer used as list key is now invalid */
983 spin_lock(&wdm_device_list_lock);
984 list_del(&desc->device_list);
985 spin_unlock(&wdm_device_list_lock);
986
Oliver Neukumafba9372008-05-13 17:01:25 +0200987 if (!desc->count)
988 cleanup(desc);
Bjørn Mork880bca32012-04-30 09:26:11 +0200989 else
Oliver Neukum13a88bf2016-08-15 11:17:55 +0200990 dev_dbg(&intf->dev, "%d open files - postponing cleanup\n", desc->count);
Oliver Neukumafba9372008-05-13 17:01:25 +0200991 mutex_unlock(&wdm_mutex);
992}
993
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100994#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200995static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
996{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100997 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200998 int rv = 0;
999
1000 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
1001
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001002 /* if this is an autosuspend the caller does the locking */
Bjørn Morke8537bd2012-01-16 12:41:48 +01001003 if (!PMSG_IS_AUTO(message)) {
1004 mutex_lock(&desc->rlock);
1005 mutex_lock(&desc->wlock);
1006 }
Oliver Neukum62e66852010-02-27 20:56:22 +01001007 spin_lock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001008
Alan Stern5b1b0b82011-08-19 23:49:48 +02001009 if (PMSG_IS_AUTO(message) &&
Oliver Neukum922a5ea2010-02-27 20:54:59 +01001010 (test_bit(WDM_IN_USE, &desc->flags)
1011 || test_bit(WDM_RESPONDING, &desc->flags))) {
Oliver Neukum62e66852010-02-27 20:56:22 +01001012 spin_unlock_irq(&desc->iuspin);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001013 rv = -EBUSY;
1014 } else {
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001015
Oliver Neukumbeb1d352010-02-27 20:55:52 +01001016 set_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +01001017 spin_unlock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001018 /* callback submits work - order is essential */
Oliver Neukum17d80d52008-06-24 15:56:10 +02001019 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001020 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001021 }
Bjørn Morke8537bd2012-01-16 12:41:48 +01001022 if (!PMSG_IS_AUTO(message)) {
1023 mutex_unlock(&desc->wlock);
1024 mutex_unlock(&desc->rlock);
1025 }
Oliver Neukum17d80d52008-06-24 15:56:10 +02001026
1027 return rv;
1028}
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001029#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +02001030
1031static int recover_from_urb_loss(struct wdm_device *desc)
1032{
1033 int rv = 0;
1034
1035 if (desc->count) {
1036 rv = usb_submit_urb(desc->validity, GFP_NOIO);
1037 if (rv < 0)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -07001038 dev_err(&desc->intf->dev,
1039 "Error resume submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001040 }
1041 return rv;
1042}
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001043
1044#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +02001045static int wdm_resume(struct usb_interface *intf)
1046{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001047 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001048 int rv;
1049
1050 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
Oliver Neukum338124c2010-02-27 20:57:12 +01001051
Oliver Neukumbeb1d352010-02-27 20:55:52 +01001052 clear_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +01001053 rv = recover_from_urb_loss(desc);
Oliver Neukum338124c2010-02-27 20:57:12 +01001054
Oliver Neukum17d80d52008-06-24 15:56:10 +02001055 return rv;
1056}
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001057#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +02001058
1059static int wdm_pre_reset(struct usb_interface *intf)
1060{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001061 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001062
Oliver Neukumd771d8a2011-04-29 14:12:21 +02001063 /*
1064 * we notify everybody using poll of
1065 * an exceptional situation
1066 * must be done before recovery lest a spontaneous
1067 * message from the device is lost
1068 */
1069 spin_lock_irq(&desc->iuspin);
Bjørn Mork88044202012-02-10 09:44:08 +01001070 set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
1071 set_bit(WDM_READ, &desc->flags); /* unblock read */
1072 clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
Oliver Neukumd771d8a2011-04-29 14:12:21 +02001073 desc->rerr = -EINTR;
1074 spin_unlock_irq(&desc->iuspin);
1075 wake_up_all(&desc->wait);
Bjørn Mork88044202012-02-10 09:44:08 +01001076 mutex_lock(&desc->rlock);
1077 mutex_lock(&desc->wlock);
1078 kill_urbs(desc);
1079 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001080 return 0;
1081}
1082
1083static int wdm_post_reset(struct usb_interface *intf)
1084{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001085 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001086 int rv;
1087
Oliver Neukumc0f5ece2013-03-12 14:52:42 +01001088 clear_bit(WDM_OVERFLOW, &desc->flags);
Bjørn Mork88044202012-02-10 09:44:08 +01001089 clear_bit(WDM_RESETTING, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001090 rv = recover_from_urb_loss(desc);
Bjørn Morke8537bd2012-01-16 12:41:48 +01001091 mutex_unlock(&desc->wlock);
1092 mutex_unlock(&desc->rlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001093 return 0;
1094}
1095
Oliver Neukumafba9372008-05-13 17:01:25 +02001096static struct usb_driver wdm_driver = {
1097 .name = "cdc_wdm",
1098 .probe = wdm_probe,
1099 .disconnect = wdm_disconnect,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001100#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +02001101 .suspend = wdm_suspend,
1102 .resume = wdm_resume,
1103 .reset_resume = wdm_resume,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001104#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +02001105 .pre_reset = wdm_pre_reset,
1106 .post_reset = wdm_post_reset,
Oliver Neukumafba9372008-05-13 17:01:25 +02001107 .id_table = wdm_ids,
Oliver Neukum17d80d52008-06-24 15:56:10 +02001108 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07001109 .disable_hub_initiated_lpm = 1,
Oliver Neukumafba9372008-05-13 17:01:25 +02001110};
1111
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001112module_usb_driver(wdm_driver);
Oliver Neukumafba9372008-05-13 17:01:25 +02001113
1114MODULE_AUTHOR(DRIVER_AUTHOR);
Oliver Neukum87d65e52008-06-19 14:20:18 +02001115MODULE_DESCRIPTION(DRIVER_DESC);
Oliver Neukumafba9372008-05-13 17:01:25 +02001116MODULE_LICENSE("GPL");