blob: 8a230f0ef77c77b9acef90b86ab902e9cd4999eb [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;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100104
105 struct list_head device_list;
Bjørn Mork3cc36152012-03-06 17:29:22 +0100106 int (*manage_power)(struct usb_interface *, int);
Oliver Neukumafba9372008-05-13 17:01:25 +0200107};
108
109static struct usb_driver wdm_driver;
110
Bjørn Morkb0c13862012-03-06 17:29:21 +0100111/* return intfdata if we own the interface, else look up intf in the list */
112static struct wdm_device *wdm_find_device(struct usb_interface *intf)
113{
Bjørn Mork6a448862012-09-10 22:17:34 +0200114 struct wdm_device *desc;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100115
116 spin_lock(&wdm_device_list_lock);
117 list_for_each_entry(desc, &wdm_device_list, device_list)
118 if (desc->intf == intf)
Bjørn Mork6a448862012-09-10 22:17:34 +0200119 goto found;
120 desc = NULL;
121found:
Bjørn Morkb0c13862012-03-06 17:29:21 +0100122 spin_unlock(&wdm_device_list_lock);
123
124 return desc;
125}
126
127static struct wdm_device *wdm_find_device_by_minor(int minor)
128{
Bjørn Mork6a448862012-09-10 22:17:34 +0200129 struct wdm_device *desc;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100130
131 spin_lock(&wdm_device_list_lock);
132 list_for_each_entry(desc, &wdm_device_list, device_list)
133 if (desc->intf->minor == minor)
Bjørn Mork6a448862012-09-10 22:17:34 +0200134 goto found;
135 desc = NULL;
136found:
Bjørn Morkb0c13862012-03-06 17:29:21 +0100137 spin_unlock(&wdm_device_list_lock);
138
139 return desc;
140}
141
Oliver Neukumafba9372008-05-13 17:01:25 +0200142/* --- callbacks --- */
143static void wdm_out_callback(struct urb *urb)
144{
145 struct wdm_device *desc;
146 desc = urb->context;
147 spin_lock(&desc->iuspin);
148 desc->werr = urb->status;
149 spin_unlock(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200150 kfree(desc->outbuf);
Oliver Neukum5c228372012-04-26 21:59:10 +0200151 desc->outbuf = NULL;
152 clear_bit(WDM_IN_USE, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200153 wake_up(&desc->wait);
154}
155
156static void wdm_in_callback(struct urb *urb)
157{
158 struct wdm_device *desc = urb->context;
159 int status = urb->status;
Oliver Neukumc0f5ece2013-03-12 14:52:42 +0100160 int length = urb->actual_length;
Oliver Neukumafba9372008-05-13 17:01:25 +0200161
162 spin_lock(&desc->iuspin);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100163 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200164
165 if (status) {
166 switch (status) {
167 case -ENOENT:
168 dev_dbg(&desc->intf->dev,
169 "nonzero urb status received: -ENOENT");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100170 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200171 case -ECONNRESET:
172 dev_dbg(&desc->intf->dev,
173 "nonzero urb status received: -ECONNRESET");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100174 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200175 case -ESHUTDOWN:
176 dev_dbg(&desc->intf->dev,
177 "nonzero urb status received: -ESHUTDOWN");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100178 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200179 case -EPIPE:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700180 dev_err(&desc->intf->dev,
181 "nonzero urb status received: -EPIPE\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200182 break;
183 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700184 dev_err(&desc->intf->dev,
185 "Unexpected error %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200186 break;
187 }
188 }
189
190 desc->rerr = status;
Oliver Neukumc0f5ece2013-03-12 14:52:42 +0100191 if (length + desc->length > desc->wMaxCommand) {
192 /* The buffer would overflow */
193 set_bit(WDM_OVERFLOW, &desc->flags);
194 } else {
195 /* we may already be in overflow */
196 if (!test_bit(WDM_OVERFLOW, &desc->flags)) {
197 memmove(desc->ubuf + desc->length, desc->inbuf, length);
198 desc->length += length;
199 desc->reslength = length;
200 }
201 }
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100202skip_error:
Oliver Neukumafba9372008-05-13 17:01:25 +0200203 wake_up(&desc->wait);
204
205 set_bit(WDM_READ, &desc->flags);
206 spin_unlock(&desc->iuspin);
207}
208
209static void wdm_int_callback(struct urb *urb)
210{
211 int rv = 0;
212 int status = urb->status;
213 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200214 struct usb_cdc_notification *dr;
215
216 desc = urb->context;
Oliver Neukumafba9372008-05-13 17:01:25 +0200217 dr = (struct usb_cdc_notification *)desc->sbuf;
218
219 if (status) {
220 switch (status) {
221 case -ESHUTDOWN:
222 case -ENOENT:
223 case -ECONNRESET:
224 return; /* unplug */
225 case -EPIPE:
226 set_bit(WDM_INT_STALL, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700227 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200228 goto sw; /* halt is cleared in work */
229 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700230 dev_err(&desc->intf->dev,
231 "nonzero urb status received: %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200232 break;
233 }
234 }
235
236 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700237 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
238 urb->actual_length);
Oliver Neukumafba9372008-05-13 17:01:25 +0200239 goto exit;
240 }
241
242 switch (dr->bNotificationType) {
243 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
244 dev_dbg(&desc->intf->dev,
245 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
246 dr->wIndex, dr->wLength);
247 break;
248
249 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
250
251 dev_dbg(&desc->intf->dev,
252 "NOTIFY_NETWORK_CONNECTION %s network",
253 dr->wValue ? "connected to" : "disconnected from");
254 goto exit;
255 default:
256 clear_bit(WDM_POLL_RUNNING, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700257 dev_err(&desc->intf->dev,
258 "unknown notification %d received: index %d len %d\n",
Oliver Neukumafba9372008-05-13 17:01:25 +0200259 dr->bNotificationType, dr->wIndex, dr->wLength);
260 goto exit;
261 }
262
Oliver Neukumafba9372008-05-13 17:01:25 +0200263 spin_lock(&desc->iuspin);
264 clear_bit(WDM_READ, &desc->flags);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100265 set_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100266 if (!test_bit(WDM_DISCONNECTING, &desc->flags)
267 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200268 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
269 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
270 __func__, rv);
271 }
272 spin_unlock(&desc->iuspin);
273 if (rv < 0) {
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100274 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200275 if (rv == -EPERM)
276 return;
277 if (rv == -ENOMEM) {
278sw:
279 rv = schedule_work(&desc->rxwork);
280 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700281 dev_err(&desc->intf->dev,
282 "Cannot schedule work\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200283 }
284 }
285exit:
286 rv = usb_submit_urb(urb, GFP_ATOMIC);
287 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700288 dev_err(&desc->intf->dev,
289 "%s - usb_submit_urb failed with result %d\n",
290 __func__, rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200291
292}
293
294static void kill_urbs(struct wdm_device *desc)
295{
Oliver Neukum17d80d52008-06-24 15:56:10 +0200296 /* the order here is essential */
Oliver Neukumafba9372008-05-13 17:01:25 +0200297 usb_kill_urb(desc->command);
298 usb_kill_urb(desc->validity);
299 usb_kill_urb(desc->response);
300}
301
302static void free_urbs(struct wdm_device *desc)
303{
304 usb_free_urb(desc->validity);
305 usb_free_urb(desc->response);
306 usb_free_urb(desc->command);
307}
308
309static void cleanup(struct wdm_device *desc)
310{
Bjørn Mork8457d992012-01-16 15:12:00 +0100311 kfree(desc->sbuf);
312 kfree(desc->inbuf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200313 kfree(desc->orq);
314 kfree(desc->irq);
315 kfree(desc->ubuf);
316 free_urbs(desc);
317 kfree(desc);
318}
319
320static ssize_t wdm_write
321(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
322{
323 u8 *buf;
324 int rv = -EMSGSIZE, r, we;
325 struct wdm_device *desc = file->private_data;
326 struct usb_ctrlrequest *req;
327
328 if (count > desc->wMaxCommand)
329 count = desc->wMaxCommand;
330
331 spin_lock_irq(&desc->iuspin);
332 we = desc->werr;
333 desc->werr = 0;
334 spin_unlock_irq(&desc->iuspin);
335 if (we < 0)
336 return -EIO;
337
Oliver Neukum5c228372012-04-26 21:59:10 +0200338 buf = kmalloc(count, GFP_KERNEL);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100339 if (!buf) {
340 rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200341 goto outnl;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100342 }
343
344 r = copy_from_user(buf, buffer, count);
345 if (r > 0) {
346 kfree(buf);
347 rv = -EFAULT;
348 goto outnl;
349 }
350
351 /* concurrent writes and disconnect */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100352 r = mutex_lock_interruptible(&desc->wlock);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100353 rv = -ERESTARTSYS;
354 if (r) {
355 kfree(buf);
356 goto outnl;
357 }
358
359 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
360 kfree(buf);
361 rv = -ENODEV;
362 goto outnp;
363 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200364
Oliver Neukum17d80d52008-06-24 15:56:10 +0200365 r = usb_autopm_get_interface(desc->intf);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100366 if (r < 0) {
367 kfree(buf);
Oliver Neukum12a98b22012-04-30 09:57:31 +0200368 rv = usb_translate_errors(r);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200369 goto outnp;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100370 }
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200371
David Sterba0cdfb812010-12-27 18:49:58 +0100372 if (!(file->f_flags & O_NONBLOCK))
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200373 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
374 &desc->flags));
375 else
376 if (test_bit(WDM_IN_USE, &desc->flags))
377 r = -EAGAIN;
Bjørn Mork88044202012-02-10 09:44:08 +0100378
379 if (test_bit(WDM_RESETTING, &desc->flags))
380 r = -EIO;
381
Oliver Neukum860e41a2010-02-27 20:54:24 +0100382 if (r < 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200383 kfree(buf);
Oliver Neukum12a98b22012-04-30 09:57:31 +0200384 rv = r;
Oliver Neukumafba9372008-05-13 17:01:25 +0200385 goto out;
386 }
387
388 req = desc->orq;
389 usb_fill_control_urb(
390 desc->command,
391 interface_to_usbdev(desc->intf),
392 /* using common endpoint 0 */
393 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
394 (unsigned char *)req,
395 buf,
396 count,
397 wdm_out_callback,
398 desc
399 );
400
401 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
402 USB_RECIP_INTERFACE);
403 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
404 req->wValue = 0;
405 req->wIndex = desc->inum;
406 req->wLength = cpu_to_le16(count);
407 set_bit(WDM_IN_USE, &desc->flags);
Oliver Neukum5c228372012-04-26 21:59:10 +0200408 desc->outbuf = buf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200409
410 rv = usb_submit_urb(desc->command, GFP_KERNEL);
411 if (rv < 0) {
412 kfree(buf);
Oliver Neukum5c228372012-04-26 21:59:10 +0200413 desc->outbuf = NULL;
Oliver Neukumafba9372008-05-13 17:01:25 +0200414 clear_bit(WDM_IN_USE, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700415 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
Oliver Neukum12a98b22012-04-30 09:57:31 +0200416 rv = usb_translate_errors(rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200417 } else {
418 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
419 req->wIndex);
420 }
421out:
Oliver Neukum17d80d52008-06-24 15:56:10 +0200422 usb_autopm_put_interface(desc->intf);
423outnp:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100424 mutex_unlock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200425outnl:
426 return rv < 0 ? rv : count;
427}
428
429static ssize_t wdm_read
430(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
431{
Ben Hutchings711c68b2012-02-12 06:00:41 +0000432 int rv, cntr;
Oliver Neukumafba9372008-05-13 17:01:25 +0200433 int i = 0;
434 struct wdm_device *desc = file->private_data;
435
436
Bjørn Morke8537bd2012-01-16 12:41:48 +0100437 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
Oliver Neukumafba9372008-05-13 17:01:25 +0200438 if (rv < 0)
439 return -ERESTARTSYS;
440
Ben Hutchings711c68b2012-02-12 06:00:41 +0000441 cntr = ACCESS_ONCE(desc->length);
442 if (cntr == 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200443 desc->read = 0;
444retry:
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200445 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
446 rv = -ENODEV;
447 goto err;
448 }
Oliver Neukumc0f5ece2013-03-12 14:52:42 +0100449 if (test_bit(WDM_OVERFLOW, &desc->flags)) {
450 clear_bit(WDM_OVERFLOW, &desc->flags);
451 rv = -ENOBUFS;
452 goto err;
453 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200454 i++;
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200455 if (file->f_flags & O_NONBLOCK) {
456 if (!test_bit(WDM_READ, &desc->flags)) {
457 rv = cntr ? cntr : -EAGAIN;
458 goto err;
459 }
460 rv = 0;
461 } else {
462 rv = wait_event_interruptible(desc->wait,
463 test_bit(WDM_READ, &desc->flags));
464 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200465
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200466 /* may have happened while we slept */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200467 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
468 rv = -ENODEV;
469 goto err;
470 }
Bjørn Mork88044202012-02-10 09:44:08 +0100471 if (test_bit(WDM_RESETTING, &desc->flags)) {
472 rv = -EIO;
473 goto err;
474 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200475 usb_mark_last_busy(interface_to_usbdev(desc->intf));
Oliver Neukumafba9372008-05-13 17:01:25 +0200476 if (rv < 0) {
477 rv = -ERESTARTSYS;
478 goto err;
479 }
480
481 spin_lock_irq(&desc->iuspin);
482
483 if (desc->rerr) { /* read completed, error happened */
Oliver Neukumafba9372008-05-13 17:01:25 +0200484 desc->rerr = 0;
485 spin_unlock_irq(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200486 rv = -EIO;
487 goto err;
488 }
489 /*
490 * recheck whether we've lost the race
491 * against the completion handler
492 */
493 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
494 spin_unlock_irq(&desc->iuspin);
495 goto retry;
496 }
Oliver Neukumc0f5ece2013-03-12 14:52:42 +0100497
Oliver Neukumafba9372008-05-13 17:01:25 +0200498 if (!desc->reslength) { /* zero length read */
Bjørn Morkb086b6b2012-07-02 10:33:14 +0200499 dev_dbg(&desc->intf->dev, "%s: zero length - clearing WDM_READ\n", __func__);
500 clear_bit(WDM_READ, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200501 spin_unlock_irq(&desc->iuspin);
502 goto retry;
503 }
Ben Hutchings711c68b2012-02-12 06:00:41 +0000504 cntr = desc->length;
Oliver Neukumafba9372008-05-13 17:01:25 +0200505 spin_unlock_irq(&desc->iuspin);
506 }
507
Ben Hutchings711c68b2012-02-12 06:00:41 +0000508 if (cntr > count)
509 cntr = count;
Oliver Neukumafba9372008-05-13 17:01:25 +0200510 rv = copy_to_user(buffer, desc->ubuf, cntr);
511 if (rv > 0) {
512 rv = -EFAULT;
513 goto err;
514 }
515
Ben Hutchings711c68b2012-02-12 06:00:41 +0000516 spin_lock_irq(&desc->iuspin);
517
Oliver Neukumafba9372008-05-13 17:01:25 +0200518 for (i = 0; i < desc->length - cntr; i++)
519 desc->ubuf[i] = desc->ubuf[i + cntr];
520
521 desc->length -= cntr;
Oliver Neukum87d65e52008-06-19 14:20:18 +0200522 /* in case we had outstanding data */
523 if (!desc->length)
524 clear_bit(WDM_READ, &desc->flags);
Ben Hutchings711c68b2012-02-12 06:00:41 +0000525
526 spin_unlock_irq(&desc->iuspin);
527
Oliver Neukumafba9372008-05-13 17:01:25 +0200528 rv = cntr;
529
530err:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100531 mutex_unlock(&desc->rlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200532 return rv;
533}
534
535static int wdm_flush(struct file *file, fl_owner_t id)
536{
537 struct wdm_device *desc = file->private_data;
538
539 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
Bjørn Mork6b0b79d2012-05-09 13:53:22 +0200540
541 /* cannot dereference desc->intf if WDM_DISCONNECTING */
542 if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags))
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700543 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
544 desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200545
Oliver Neukum24a85ba2012-04-27 14:23:54 +0200546 return usb_translate_errors(desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200547}
548
549static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
550{
551 struct wdm_device *desc = file->private_data;
552 unsigned long flags;
553 unsigned int mask = 0;
554
555 spin_lock_irqsave(&desc->iuspin, flags);
556 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
Bjørn Mork616b6932012-05-09 13:53:21 +0200557 mask = POLLHUP | POLLERR;
Oliver Neukumafba9372008-05-13 17:01:25 +0200558 spin_unlock_irqrestore(&desc->iuspin, flags);
559 goto desc_out;
560 }
561 if (test_bit(WDM_READ, &desc->flags))
562 mask = POLLIN | POLLRDNORM;
563 if (desc->rerr || desc->werr)
564 mask |= POLLERR;
565 if (!test_bit(WDM_IN_USE, &desc->flags))
566 mask |= POLLOUT | POLLWRNORM;
567 spin_unlock_irqrestore(&desc->iuspin, flags);
568
569 poll_wait(file, &desc->wait, wait);
570
571desc_out:
572 return mask;
573}
574
575static int wdm_open(struct inode *inode, struct file *file)
576{
577 int minor = iminor(inode);
578 int rv = -ENODEV;
579 struct usb_interface *intf;
580 struct wdm_device *desc;
581
582 mutex_lock(&wdm_mutex);
Bjørn Morkb0c13862012-03-06 17:29:21 +0100583 desc = wdm_find_device_by_minor(minor);
584 if (!desc)
Oliver Neukumafba9372008-05-13 17:01:25 +0200585 goto out;
586
Bjørn Morkb0c13862012-03-06 17:29:21 +0100587 intf = desc->intf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200588 if (test_bit(WDM_DISCONNECTING, &desc->flags))
589 goto out;
Oliver Neukumafba9372008-05-13 17:01:25 +0200590 file->private_data = desc;
591
Oliver Neukum17d80d52008-06-24 15:56:10 +0200592 rv = usb_autopm_get_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200593 if (rv < 0) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700594 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200595 goto out;
596 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200597
Bjørn Morke8537bd2012-01-16 12:41:48 +0100598 /* using write lock to protect desc->count */
599 mutex_lock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200600 if (!desc->count++) {
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200601 desc->werr = 0;
602 desc->rerr = 0;
Oliver Neukum17d80d52008-06-24 15:56:10 +0200603 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
604 if (rv < 0) {
605 desc->count--;
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700606 dev_err(&desc->intf->dev,
607 "Error submitting int urb - %d\n", rv);
Oliver Neukum12a98b22012-04-30 09:57:31 +0200608 rv = usb_translate_errors(rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200609 }
610 } else {
611 rv = 0;
612 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100613 mutex_unlock(&desc->wlock);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100614 if (desc->count == 1)
615 desc->manage_power(intf, 1);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200616 usb_autopm_put_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200617out:
618 mutex_unlock(&wdm_mutex);
619 return rv;
620}
621
622static int wdm_release(struct inode *inode, struct file *file)
623{
624 struct wdm_device *desc = file->private_data;
625
626 mutex_lock(&wdm_mutex);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100627
628 /* using write lock to protect desc->count */
629 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200630 desc->count--;
Bjørn Morke8537bd2012-01-16 12:41:48 +0100631 mutex_unlock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200632
Oliver Neukumafba9372008-05-13 17:01:25 +0200633 if (!desc->count) {
Bjørn Mork880bca32012-04-30 09:26:11 +0200634 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
Bjørn Mork6b0b79d2012-05-09 13:53:22 +0200635 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
636 kill_urbs(desc);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100637 desc->manage_power(desc->intf, 0);
Bjørn Mork880bca32012-04-30 09:26:11 +0200638 } else {
Bjørn Mork6b0b79d2012-05-09 13:53:22 +0200639 /* must avoid dev_printk here as desc->intf is invalid */
640 pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
Oliver Neukum2f338c82012-04-27 14:36:37 +0200641 cleanup(desc);
Bjørn Mork880bca32012-04-30 09:26:11 +0200642 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200643 }
644 mutex_unlock(&wdm_mutex);
645 return 0;
646}
647
Bjørn Mork3edce1c2013-03-17 21:00:06 +0100648static long wdm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
649{
650 struct wdm_device *desc = file->private_data;
651 int rv = 0;
652
653 switch (cmd) {
654 case IOCTL_WDM_MAX_COMMAND:
655 if (copy_to_user((void __user *)arg, &desc->wMaxCommand, sizeof(desc->wMaxCommand)))
656 rv = -EFAULT;
657 break;
658 default:
659 rv = -ENOTTY;
660 }
661 return rv;
662}
663
Oliver Neukumafba9372008-05-13 17:01:25 +0200664static const struct file_operations wdm_fops = {
665 .owner = THIS_MODULE,
666 .read = wdm_read,
667 .write = wdm_write,
668 .open = wdm_open,
669 .flush = wdm_flush,
670 .release = wdm_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200671 .poll = wdm_poll,
Bjørn Mork3edce1c2013-03-17 21:00:06 +0100672 .unlocked_ioctl = wdm_ioctl,
673 .compat_ioctl = wdm_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200674 .llseek = noop_llseek,
Oliver Neukumafba9372008-05-13 17:01:25 +0200675};
676
677static struct usb_class_driver wdm_class = {
678 .name = "cdc-wdm%d",
679 .fops = &wdm_fops,
680 .minor_base = WDM_MINOR_BASE,
681};
682
683/* --- error handling --- */
684static void wdm_rxwork(struct work_struct *work)
685{
686 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
687 unsigned long flags;
688 int rv;
689
690 spin_lock_irqsave(&desc->iuspin, flags);
691 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
692 spin_unlock_irqrestore(&desc->iuspin, flags);
693 } else {
694 spin_unlock_irqrestore(&desc->iuspin, flags);
695 rv = usb_submit_urb(desc->response, GFP_KERNEL);
696 if (rv < 0 && rv != -EPERM) {
697 spin_lock_irqsave(&desc->iuspin, flags);
698 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
699 schedule_work(&desc->rxwork);
700 spin_unlock_irqrestore(&desc->iuspin, flags);
701 }
702 }
703}
704
705/* --- hotplug --- */
706
Bjørn Mork3cc36152012-03-06 17:29:22 +0100707static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
708 u16 bufsize, int (*manage_power)(struct usb_interface *, int))
Oliver Neukumafba9372008-05-13 17:01:25 +0200709{
Bjørn Mork0dffb482012-03-06 17:29:20 +0100710 int rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200711 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200712
Oliver Neukumafba9372008-05-13 17:01:25 +0200713 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
714 if (!desc)
715 goto out;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100716 INIT_LIST_HEAD(&desc->device_list);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100717 mutex_init(&desc->rlock);
718 mutex_init(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200719 spin_lock_init(&desc->iuspin);
720 init_waitqueue_head(&desc->wait);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100721 desc->wMaxCommand = bufsize;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200722 /* this will be expanded and needed in hardware endianness */
Oliver Neukumafba9372008-05-13 17:01:25 +0200723 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
724 desc->intf = intf;
725 INIT_WORK(&desc->rxwork, wdm_rxwork);
726
Oliver Neukum052fbc02009-04-20 17:24:49 +0200727 rv = -EINVAL;
Bjørn Mork0dffb482012-03-06 17:29:20 +0100728 if (!usb_endpoint_is_int_in(ep))
Oliver Neukum052fbc02009-04-20 17:24:49 +0200729 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200730
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700731 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
Oliver Neukumafba9372008-05-13 17:01:25 +0200732
733 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
734 if (!desc->orq)
735 goto err;
736 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
737 if (!desc->irq)
738 goto err;
739
740 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
741 if (!desc->validity)
742 goto err;
743
744 desc->response = usb_alloc_urb(0, GFP_KERNEL);
745 if (!desc->response)
746 goto err;
747
748 desc->command = usb_alloc_urb(0, GFP_KERNEL);
749 if (!desc->command)
750 goto err;
751
752 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
753 if (!desc->ubuf)
754 goto err;
755
Bjørn Mork8457d992012-01-16 15:12:00 +0100756 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200757 if (!desc->sbuf)
758 goto err;
759
Bjørn Mork8457d992012-01-16 15:12:00 +0100760 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200761 if (!desc->inbuf)
Bjørn Mork8457d992012-01-16 15:12:00 +0100762 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200763
764 usb_fill_int_urb(
765 desc->validity,
766 interface_to_usbdev(intf),
767 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
768 desc->sbuf,
769 desc->wMaxPacketSize,
770 wdm_int_callback,
771 desc,
772 ep->bInterval
773 );
Oliver Neukumafba9372008-05-13 17:01:25 +0200774
Bjørn Mork19b85b32012-01-16 15:11:58 +0100775 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
776 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
777 desc->irq->wValue = 0;
778 desc->irq->wIndex = desc->inum;
779 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
780
781 usb_fill_control_urb(
782 desc->response,
Bjørn Mork8143a892012-01-16 15:12:01 +0100783 interface_to_usbdev(intf),
Bjørn Mork19b85b32012-01-16 15:11:58 +0100784 /* using common endpoint 0 */
785 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
786 (unsigned char *)desc->irq,
787 desc->inbuf,
788 desc->wMaxCommand,
789 wdm_in_callback,
790 desc
791 );
Bjørn Mork19b85b32012-01-16 15:11:58 +0100792
Bjørn Mork3cc36152012-03-06 17:29:22 +0100793 desc->manage_power = manage_power;
794
Bjørn Morkb0c13862012-03-06 17:29:21 +0100795 spin_lock(&wdm_device_list_lock);
796 list_add(&desc->device_list, &wdm_device_list);
797 spin_unlock(&wdm_device_list_lock);
798
Oliver Neukumafba9372008-05-13 17:01:25 +0200799 rv = usb_register_dev(intf, &wdm_class);
Oliver Neukumafba9372008-05-13 17:01:25 +0200800 if (rv < 0)
Bjørn Morkb0c13862012-03-06 17:29:21 +0100801 goto err;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200802 else
Bjørn Mork820c6292012-01-20 04:17:25 +0100803 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
Oliver Neukumafba9372008-05-13 17:01:25 +0200804out:
805 return rv;
Oliver Neukumafba9372008-05-13 17:01:25 +0200806err:
Bjørn Mork6286d852012-05-09 13:53:23 +0200807 spin_lock(&wdm_device_list_lock);
808 list_del(&desc->device_list);
809 spin_unlock(&wdm_device_list_lock);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100810 cleanup(desc);
811 return rv;
812}
813
Bjørn Mork3cc36152012-03-06 17:29:22 +0100814static int wdm_manage_power(struct usb_interface *intf, int on)
815{
816 /* need autopm_get/put here to ensure the usbcore sees the new value */
817 int rv = usb_autopm_get_interface(intf);
818 if (rv < 0)
819 goto err;
820
821 intf->needs_remote_wakeup = on;
822 usb_autopm_put_interface(intf);
823err:
824 return rv;
825}
826
Bjørn Mork0dffb482012-03-06 17:29:20 +0100827static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
828{
829 int rv = -EINVAL;
830 struct usb_host_interface *iface;
831 struct usb_endpoint_descriptor *ep;
832 struct usb_cdc_dmm_desc *dmhd;
833 u8 *buffer = intf->altsetting->extra;
834 int buflen = intf->altsetting->extralen;
835 u16 maxcom = WDM_DEFAULT_BUFSIZE;
836
837 if (!buffer)
838 goto err;
839 while (buflen > 2) {
840 if (buffer[1] != USB_DT_CS_INTERFACE) {
841 dev_err(&intf->dev, "skipping garbage\n");
842 goto next_desc;
843 }
844
845 switch (buffer[2]) {
846 case USB_CDC_HEADER_TYPE:
847 break;
848 case USB_CDC_DMM_TYPE:
849 dmhd = (struct usb_cdc_dmm_desc *)buffer;
850 maxcom = le16_to_cpu(dmhd->wMaxCommand);
851 dev_dbg(&intf->dev,
852 "Finding maximum buffer length: %d", maxcom);
853 break;
854 default:
855 dev_err(&intf->dev,
856 "Ignoring extra header, type %d, length %d\n",
857 buffer[2], buffer[0]);
858 break;
859 }
860next_desc:
861 buflen -= buffer[0];
862 buffer += buffer[0];
863 }
864
865 iface = intf->cur_altsetting;
866 if (iface->desc.bNumEndpoints != 1)
867 goto err;
868 ep = &iface->endpoint[0].desc;
869
Bjørn Mork3cc36152012-03-06 17:29:22 +0100870 rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100871
872err:
Oliver Neukumafba9372008-05-13 17:01:25 +0200873 return rv;
874}
875
Bjørn Mork3cc36152012-03-06 17:29:22 +0100876/**
877 * usb_cdc_wdm_register - register a WDM subdriver
878 * @intf: usb interface the subdriver will associate with
879 * @ep: interrupt endpoint to monitor for notifications
880 * @bufsize: maximum message size to support for read/write
881 *
882 * Create WDM usb class character device and associate it with intf
883 * without binding, allowing another driver to manage the interface.
884 *
885 * The subdriver will manage the given interrupt endpoint exclusively
886 * and will issue control requests referring to the given intf. It
887 * will otherwise avoid interferring, and in particular not do
888 * usb_set_intfdata/usb_get_intfdata on intf.
889 *
890 * The return value is a pointer to the subdriver's struct usb_driver.
891 * The registering driver is responsible for calling this subdriver's
892 * disconnect, suspend, resume, pre_reset and post_reset methods from
893 * its own.
894 */
895struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
896 struct usb_endpoint_descriptor *ep,
897 int bufsize,
898 int (*manage_power)(struct usb_interface *, int))
899{
900 int rv = -EINVAL;
901
902 rv = wdm_create(intf, ep, bufsize, manage_power);
903 if (rv < 0)
904 goto err;
905
906 return &wdm_driver;
907err:
908 return ERR_PTR(rv);
909}
910EXPORT_SYMBOL(usb_cdc_wdm_register);
911
Oliver Neukumafba9372008-05-13 17:01:25 +0200912static void wdm_disconnect(struct usb_interface *intf)
913{
914 struct wdm_device *desc;
915 unsigned long flags;
916
917 usb_deregister_dev(intf, &wdm_class);
Bjørn Morkb0c13862012-03-06 17:29:21 +0100918 desc = wdm_find_device(intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200919 mutex_lock(&wdm_mutex);
Oliver Neukumafba9372008-05-13 17:01:25 +0200920
921 /* the spinlock makes sure no new urbs are generated in the callbacks */
922 spin_lock_irqsave(&desc->iuspin, flags);
923 set_bit(WDM_DISCONNECTING, &desc->flags);
924 set_bit(WDM_READ, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200925 /* to terminate pending flushes */
Oliver Neukumafba9372008-05-13 17:01:25 +0200926 clear_bit(WDM_IN_USE, &desc->flags);
927 spin_unlock_irqrestore(&desc->iuspin, flags);
Bjørn Mork62aaf242012-01-16 15:11:57 +0100928 wake_up_all(&desc->wait);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100929 mutex_lock(&desc->rlock);
930 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200931 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100932 cancel_work_sync(&desc->rxwork);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100933 mutex_unlock(&desc->wlock);
934 mutex_unlock(&desc->rlock);
Bjørn Mork6286d852012-05-09 13:53:23 +0200935
936 /* the desc->intf pointer used as list key is now invalid */
937 spin_lock(&wdm_device_list_lock);
938 list_del(&desc->device_list);
939 spin_unlock(&wdm_device_list_lock);
940
Oliver Neukumafba9372008-05-13 17:01:25 +0200941 if (!desc->count)
942 cleanup(desc);
Bjørn Mork880bca32012-04-30 09:26:11 +0200943 else
944 dev_dbg(&intf->dev, "%s: %d open files - postponing cleanup\n", __func__, desc->count);
Oliver Neukumafba9372008-05-13 17:01:25 +0200945 mutex_unlock(&wdm_mutex);
946}
947
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100948#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200949static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
950{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100951 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200952 int rv = 0;
953
954 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
955
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100956 /* if this is an autosuspend the caller does the locking */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100957 if (!PMSG_IS_AUTO(message)) {
958 mutex_lock(&desc->rlock);
959 mutex_lock(&desc->wlock);
960 }
Oliver Neukum62e66852010-02-27 20:56:22 +0100961 spin_lock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100962
Alan Stern5b1b0b82011-08-19 23:49:48 +0200963 if (PMSG_IS_AUTO(message) &&
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100964 (test_bit(WDM_IN_USE, &desc->flags)
965 || test_bit(WDM_RESPONDING, &desc->flags))) {
Oliver Neukum62e66852010-02-27 20:56:22 +0100966 spin_unlock_irq(&desc->iuspin);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200967 rv = -EBUSY;
968 } else {
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100969
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100970 set_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100971 spin_unlock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100972 /* callback submits work - order is essential */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200973 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100974 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200975 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100976 if (!PMSG_IS_AUTO(message)) {
977 mutex_unlock(&desc->wlock);
978 mutex_unlock(&desc->rlock);
979 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200980
981 return rv;
982}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100983#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200984
985static int recover_from_urb_loss(struct wdm_device *desc)
986{
987 int rv = 0;
988
989 if (desc->count) {
990 rv = usb_submit_urb(desc->validity, GFP_NOIO);
991 if (rv < 0)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700992 dev_err(&desc->intf->dev,
993 "Error resume submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200994 }
995 return rv;
996}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100997
998#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200999static int wdm_resume(struct usb_interface *intf)
1000{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001001 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001002 int rv;
1003
1004 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
Oliver Neukum338124c2010-02-27 20:57:12 +01001005
Oliver Neukumbeb1d352010-02-27 20:55:52 +01001006 clear_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +01001007 rv = recover_from_urb_loss(desc);
Oliver Neukum338124c2010-02-27 20:57:12 +01001008
Oliver Neukum17d80d52008-06-24 15:56:10 +02001009 return rv;
1010}
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001011#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +02001012
1013static int wdm_pre_reset(struct usb_interface *intf)
1014{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001015 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001016
Oliver Neukumd771d8a2011-04-29 14:12:21 +02001017 /*
1018 * we notify everybody using poll of
1019 * an exceptional situation
1020 * must be done before recovery lest a spontaneous
1021 * message from the device is lost
1022 */
1023 spin_lock_irq(&desc->iuspin);
Bjørn Mork88044202012-02-10 09:44:08 +01001024 set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
1025 set_bit(WDM_READ, &desc->flags); /* unblock read */
1026 clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
Oliver Neukumd771d8a2011-04-29 14:12:21 +02001027 desc->rerr = -EINTR;
1028 spin_unlock_irq(&desc->iuspin);
1029 wake_up_all(&desc->wait);
Bjørn Mork88044202012-02-10 09:44:08 +01001030 mutex_lock(&desc->rlock);
1031 mutex_lock(&desc->wlock);
1032 kill_urbs(desc);
1033 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001034 return 0;
1035}
1036
1037static int wdm_post_reset(struct usb_interface *intf)
1038{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001039 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001040 int rv;
1041
Oliver Neukumc0f5ece2013-03-12 14:52:42 +01001042 clear_bit(WDM_OVERFLOW, &desc->flags);
Bjørn Mork88044202012-02-10 09:44:08 +01001043 clear_bit(WDM_RESETTING, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001044 rv = recover_from_urb_loss(desc);
Bjørn Morke8537bd2012-01-16 12:41:48 +01001045 mutex_unlock(&desc->wlock);
1046 mutex_unlock(&desc->rlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001047 return 0;
1048}
1049
Oliver Neukumafba9372008-05-13 17:01:25 +02001050static struct usb_driver wdm_driver = {
1051 .name = "cdc_wdm",
1052 .probe = wdm_probe,
1053 .disconnect = wdm_disconnect,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001054#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +02001055 .suspend = wdm_suspend,
1056 .resume = wdm_resume,
1057 .reset_resume = wdm_resume,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001058#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +02001059 .pre_reset = wdm_pre_reset,
1060 .post_reset = wdm_post_reset,
Oliver Neukumafba9372008-05-13 17:01:25 +02001061 .id_table = wdm_ids,
Oliver Neukum17d80d52008-06-24 15:56:10 +02001062 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07001063 .disable_hub_initiated_lpm = 1,
Oliver Neukumafba9372008-05-13 17:01:25 +02001064};
1065
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001066module_usb_driver(wdm_driver);
Oliver Neukumafba9372008-05-13 17:01:25 +02001067
1068MODULE_AUTHOR(DRIVER_AUTHOR);
Oliver Neukum87d65e52008-06-19 14:20:18 +02001069MODULE_DESCRIPTION(DRIVER_DESC);
Oliver Neukumafba9372008-05-13 17:01:25 +02001070MODULE_LICENSE("GPL");