blob: 122d056d96d570c2d59762fda22e0e973ac081b5 [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>
16#include <linux/slab.h>
17#include <linux/module.h>
Oliver Neukumafba9372008-05-13 17:01:25 +020018#include <linux/mutex.h>
19#include <linux/uaccess.h>
20#include <linux/bitops.h>
21#include <linux/poll.h>
22#include <linux/usb.h>
23#include <linux/usb/cdc.h>
24#include <asm/byteorder.h>
25#include <asm/unaligned.h>
Bjørn Mork3cc36152012-03-06 17:29:22 +010026#include <linux/usb/cdc-wdm.h>
Oliver Neukumafba9372008-05-13 17:01:25 +020027
28/*
29 * Version Information
30 */
Oliver Neukum87d65e52008-06-19 14:20:18 +020031#define DRIVER_VERSION "v0.03"
Oliver Neukumafba9372008-05-13 17:01:25 +020032#define DRIVER_AUTHOR "Oliver Neukum"
Oliver Neukum87d65e52008-06-19 14:20:18 +020033#define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management"
Oliver Neukumafba9372008-05-13 17:01:25 +020034
Németh Márton6ef48522010-01-10 15:33:45 +010035static const struct usb_device_id wdm_ids[] = {
Oliver Neukumafba9372008-05-13 17:01:25 +020036 {
37 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
38 USB_DEVICE_ID_MATCH_INT_SUBCLASS,
39 .bInterfaceClass = USB_CLASS_COMM,
40 .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM
41 },
42 { }
43};
44
Oliver Neukumaa5380b2008-10-13 14:05:20 +020045MODULE_DEVICE_TABLE (usb, wdm_ids);
46
Oliver Neukumafba9372008-05-13 17:01:25 +020047#define WDM_MINOR_BASE 176
48
49
50#define WDM_IN_USE 1
51#define WDM_DISCONNECTING 2
52#define WDM_RESULT 3
53#define WDM_READ 4
54#define WDM_INT_STALL 5
55#define WDM_POLL_RUNNING 6
Oliver Neukum922a5ea2010-02-27 20:54:59 +010056#define WDM_RESPONDING 7
Oliver Neukumbeb1d352010-02-27 20:55:52 +010057#define WDM_SUSPENDING 8
Bjørn Mork88044202012-02-10 09:44:08 +010058#define WDM_RESETTING 9
Oliver Neukumc0f5ece2013-03-12 14:52:42 +010059#define WDM_OVERFLOW 10
Oliver Neukumafba9372008-05-13 17:01:25 +020060
61#define WDM_MAX 16
62
Bjørn Mork7e3054a2012-01-20 01:49:57 +010063/* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
64#define WDM_DEFAULT_BUFSIZE 256
Oliver Neukumafba9372008-05-13 17:01:25 +020065
66static DEFINE_MUTEX(wdm_mutex);
Bjørn Morkb0c13862012-03-06 17:29:21 +010067static DEFINE_SPINLOCK(wdm_device_list_lock);
68static LIST_HEAD(wdm_device_list);
Oliver Neukumafba9372008-05-13 17:01:25 +020069
70/* --- method tables --- */
71
72struct wdm_device {
73 u8 *inbuf; /* buffer for response */
74 u8 *outbuf; /* buffer for command */
75 u8 *sbuf; /* buffer for status */
76 u8 *ubuf; /* buffer for copy to user space */
77
78 struct urb *command;
79 struct urb *response;
80 struct urb *validity;
81 struct usb_interface *intf;
82 struct usb_ctrlrequest *orq;
83 struct usb_ctrlrequest *irq;
84 spinlock_t iuspin;
85
86 unsigned long flags;
87 u16 bufsize;
88 u16 wMaxCommand;
89 u16 wMaxPacketSize;
Oliver Neukumafba9372008-05-13 17:01:25 +020090 __le16 inum;
91 int reslength;
92 int length;
93 int read;
94 int count;
95 dma_addr_t shandle;
96 dma_addr_t ihandle;
Bjørn Morke8537bd2012-01-16 12:41:48 +010097 struct mutex wlock;
98 struct mutex rlock;
Oliver Neukumafba9372008-05-13 17:01:25 +020099 wait_queue_head_t wait;
100 struct work_struct rxwork;
101 int werr;
102 int rerr;
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;
145 desc = urb->context;
146 spin_lock(&desc->iuspin);
147 desc->werr = urb->status;
148 spin_unlock(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200149 kfree(desc->outbuf);
Oliver Neukum5c228372012-04-26 21:59:10 +0200150 desc->outbuf = NULL;
151 clear_bit(WDM_IN_USE, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200152 wake_up(&desc->wait);
153}
154
155static void wdm_in_callback(struct urb *urb)
156{
157 struct wdm_device *desc = urb->context;
158 int status = urb->status;
Oliver Neukumc0f5ece2013-03-12 14:52:42 +0100159 int length = urb->actual_length;
Oliver Neukumafba9372008-05-13 17:01:25 +0200160
161 spin_lock(&desc->iuspin);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100162 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200163
164 if (status) {
165 switch (status) {
166 case -ENOENT:
167 dev_dbg(&desc->intf->dev,
168 "nonzero urb status received: -ENOENT");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100169 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200170 case -ECONNRESET:
171 dev_dbg(&desc->intf->dev,
172 "nonzero urb status received: -ECONNRESET");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100173 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200174 case -ESHUTDOWN:
175 dev_dbg(&desc->intf->dev,
176 "nonzero urb status received: -ESHUTDOWN");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100177 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200178 case -EPIPE:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700179 dev_err(&desc->intf->dev,
180 "nonzero urb status received: -EPIPE\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200181 break;
182 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700183 dev_err(&desc->intf->dev,
184 "Unexpected error %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200185 break;
186 }
187 }
188
189 desc->rerr = status;
Oliver Neukumc0f5ece2013-03-12 14:52:42 +0100190 if (length + desc->length > desc->wMaxCommand) {
191 /* The buffer would overflow */
192 set_bit(WDM_OVERFLOW, &desc->flags);
193 } else {
194 /* we may already be in overflow */
195 if (!test_bit(WDM_OVERFLOW, &desc->flags)) {
196 memmove(desc->ubuf + desc->length, desc->inbuf, length);
197 desc->length += length;
198 desc->reslength = length;
199 }
200 }
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100201skip_error:
Oliver Neukumafba9372008-05-13 17:01:25 +0200202 wake_up(&desc->wait);
203
204 set_bit(WDM_READ, &desc->flags);
205 spin_unlock(&desc->iuspin);
206}
207
208static void wdm_int_callback(struct urb *urb)
209{
210 int rv = 0;
211 int status = urb->status;
212 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200213 struct usb_cdc_notification *dr;
214
215 desc = urb->context;
Oliver Neukumafba9372008-05-13 17:01:25 +0200216 dr = (struct usb_cdc_notification *)desc->sbuf;
217
218 if (status) {
219 switch (status) {
220 case -ESHUTDOWN:
221 case -ENOENT:
222 case -ECONNRESET:
223 return; /* unplug */
224 case -EPIPE:
225 set_bit(WDM_INT_STALL, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700226 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200227 goto sw; /* halt is cleared in work */
228 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700229 dev_err(&desc->intf->dev,
230 "nonzero urb status received: %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200231 break;
232 }
233 }
234
235 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700236 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
237 urb->actual_length);
Oliver Neukumafba9372008-05-13 17:01:25 +0200238 goto exit;
239 }
240
241 switch (dr->bNotificationType) {
242 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
243 dev_dbg(&desc->intf->dev,
244 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
245 dr->wIndex, dr->wLength);
246 break;
247
248 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
249
250 dev_dbg(&desc->intf->dev,
251 "NOTIFY_NETWORK_CONNECTION %s network",
252 dr->wValue ? "connected to" : "disconnected from");
253 goto exit;
254 default:
255 clear_bit(WDM_POLL_RUNNING, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700256 dev_err(&desc->intf->dev,
257 "unknown notification %d received: index %d len %d\n",
Oliver Neukumafba9372008-05-13 17:01:25 +0200258 dr->bNotificationType, dr->wIndex, dr->wLength);
259 goto exit;
260 }
261
Oliver Neukumafba9372008-05-13 17:01:25 +0200262 spin_lock(&desc->iuspin);
263 clear_bit(WDM_READ, &desc->flags);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100264 set_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100265 if (!test_bit(WDM_DISCONNECTING, &desc->flags)
266 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200267 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
268 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
269 __func__, rv);
270 }
271 spin_unlock(&desc->iuspin);
272 if (rv < 0) {
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100273 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200274 if (rv == -EPERM)
275 return;
276 if (rv == -ENOMEM) {
277sw:
278 rv = schedule_work(&desc->rxwork);
279 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700280 dev_err(&desc->intf->dev,
281 "Cannot schedule work\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200282 }
283 }
284exit:
285 rv = usb_submit_urb(urb, GFP_ATOMIC);
286 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700287 dev_err(&desc->intf->dev,
288 "%s - usb_submit_urb failed with result %d\n",
289 __func__, rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200290
291}
292
293static void kill_urbs(struct wdm_device *desc)
294{
Oliver Neukum17d80d52008-06-24 15:56:10 +0200295 /* the order here is essential */
Oliver Neukumafba9372008-05-13 17:01:25 +0200296 usb_kill_urb(desc->command);
297 usb_kill_urb(desc->validity);
298 usb_kill_urb(desc->response);
299}
300
301static void free_urbs(struct wdm_device *desc)
302{
303 usb_free_urb(desc->validity);
304 usb_free_urb(desc->response);
305 usb_free_urb(desc->command);
306}
307
308static void cleanup(struct wdm_device *desc)
309{
Bjørn Mork8457d992012-01-16 15:12:00 +0100310 kfree(desc->sbuf);
311 kfree(desc->inbuf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200312 kfree(desc->orq);
313 kfree(desc->irq);
314 kfree(desc->ubuf);
315 free_urbs(desc);
316 kfree(desc);
317}
318
319static ssize_t wdm_write
320(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
321{
322 u8 *buf;
323 int rv = -EMSGSIZE, r, we;
324 struct wdm_device *desc = file->private_data;
325 struct usb_ctrlrequest *req;
326
327 if (count > desc->wMaxCommand)
328 count = desc->wMaxCommand;
329
330 spin_lock_irq(&desc->iuspin);
331 we = desc->werr;
332 desc->werr = 0;
333 spin_unlock_irq(&desc->iuspin);
334 if (we < 0)
335 return -EIO;
336
Oliver Neukum5c228372012-04-26 21:59:10 +0200337 buf = kmalloc(count, GFP_KERNEL);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100338 if (!buf) {
339 rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200340 goto outnl;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100341 }
342
343 r = copy_from_user(buf, buffer, count);
344 if (r > 0) {
345 kfree(buf);
346 rv = -EFAULT;
347 goto outnl;
348 }
349
350 /* concurrent writes and disconnect */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100351 r = mutex_lock_interruptible(&desc->wlock);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100352 rv = -ERESTARTSYS;
353 if (r) {
354 kfree(buf);
355 goto outnl;
356 }
357
358 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
359 kfree(buf);
360 rv = -ENODEV;
361 goto outnp;
362 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200363
Oliver Neukum17d80d52008-06-24 15:56:10 +0200364 r = usb_autopm_get_interface(desc->intf);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100365 if (r < 0) {
366 kfree(buf);
Oliver Neukum12a98b22012-04-30 09:57:31 +0200367 rv = usb_translate_errors(r);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200368 goto outnp;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100369 }
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200370
David Sterba0cdfb812010-12-27 18:49:58 +0100371 if (!(file->f_flags & O_NONBLOCK))
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200372 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
373 &desc->flags));
374 else
375 if (test_bit(WDM_IN_USE, &desc->flags))
376 r = -EAGAIN;
Bjørn Mork88044202012-02-10 09:44:08 +0100377
378 if (test_bit(WDM_RESETTING, &desc->flags))
379 r = -EIO;
380
Oliver Neukum860e41a2010-02-27 20:54:24 +0100381 if (r < 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200382 kfree(buf);
Oliver Neukum12a98b22012-04-30 09:57:31 +0200383 rv = r;
Oliver Neukumafba9372008-05-13 17:01:25 +0200384 goto out;
385 }
386
387 req = desc->orq;
388 usb_fill_control_urb(
389 desc->command,
390 interface_to_usbdev(desc->intf),
391 /* using common endpoint 0 */
392 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
393 (unsigned char *)req,
394 buf,
395 count,
396 wdm_out_callback,
397 desc
398 );
399
400 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
401 USB_RECIP_INTERFACE);
402 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
403 req->wValue = 0;
404 req->wIndex = desc->inum;
405 req->wLength = cpu_to_le16(count);
406 set_bit(WDM_IN_USE, &desc->flags);
Oliver Neukum5c228372012-04-26 21:59:10 +0200407 desc->outbuf = buf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200408
409 rv = usb_submit_urb(desc->command, GFP_KERNEL);
410 if (rv < 0) {
411 kfree(buf);
Oliver Neukum5c228372012-04-26 21:59:10 +0200412 desc->outbuf = NULL;
Oliver Neukumafba9372008-05-13 17:01:25 +0200413 clear_bit(WDM_IN_USE, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700414 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
Oliver Neukum12a98b22012-04-30 09:57:31 +0200415 rv = usb_translate_errors(rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200416 } else {
417 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
418 req->wIndex);
419 }
420out:
Oliver Neukum17d80d52008-06-24 15:56:10 +0200421 usb_autopm_put_interface(desc->intf);
422outnp:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100423 mutex_unlock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200424outnl:
425 return rv < 0 ? rv : count;
426}
427
428static ssize_t wdm_read
429(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
430{
Ben Hutchings711c68b2012-02-12 06:00:41 +0000431 int rv, cntr;
Oliver Neukumafba9372008-05-13 17:01:25 +0200432 int i = 0;
433 struct wdm_device *desc = file->private_data;
434
435
Bjørn Morke8537bd2012-01-16 12:41:48 +0100436 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
Oliver Neukumafba9372008-05-13 17:01:25 +0200437 if (rv < 0)
438 return -ERESTARTSYS;
439
Ben Hutchings711c68b2012-02-12 06:00:41 +0000440 cntr = ACCESS_ONCE(desc->length);
441 if (cntr == 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200442 desc->read = 0;
443retry:
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200444 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
445 rv = -ENODEV;
446 goto err;
447 }
Oliver Neukumc0f5ece2013-03-12 14:52:42 +0100448 if (test_bit(WDM_OVERFLOW, &desc->flags)) {
449 clear_bit(WDM_OVERFLOW, &desc->flags);
450 rv = -ENOBUFS;
451 goto err;
452 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200453 i++;
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200454 if (file->f_flags & O_NONBLOCK) {
455 if (!test_bit(WDM_READ, &desc->flags)) {
456 rv = cntr ? cntr : -EAGAIN;
457 goto err;
458 }
459 rv = 0;
460 } else {
461 rv = wait_event_interruptible(desc->wait,
462 test_bit(WDM_READ, &desc->flags));
463 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200464
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200465 /* may have happened while we slept */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200466 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
467 rv = -ENODEV;
468 goto err;
469 }
Bjørn Mork88044202012-02-10 09:44:08 +0100470 if (test_bit(WDM_RESETTING, &desc->flags)) {
471 rv = -EIO;
472 goto err;
473 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200474 usb_mark_last_busy(interface_to_usbdev(desc->intf));
Oliver Neukumafba9372008-05-13 17:01:25 +0200475 if (rv < 0) {
476 rv = -ERESTARTSYS;
477 goto err;
478 }
479
480 spin_lock_irq(&desc->iuspin);
481
482 if (desc->rerr) { /* read completed, error happened */
Oliver Neukumafba9372008-05-13 17:01:25 +0200483 desc->rerr = 0;
484 spin_unlock_irq(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200485 rv = -EIO;
486 goto err;
487 }
488 /*
489 * recheck whether we've lost the race
490 * against the completion handler
491 */
492 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
493 spin_unlock_irq(&desc->iuspin);
494 goto retry;
495 }
Oliver Neukumc0f5ece2013-03-12 14:52:42 +0100496
Oliver Neukumafba9372008-05-13 17:01:25 +0200497 if (!desc->reslength) { /* zero length read */
Bjørn Morkb086b6b2012-07-02 10:33:14 +0200498 dev_dbg(&desc->intf->dev, "%s: zero length - clearing WDM_READ\n", __func__);
499 clear_bit(WDM_READ, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200500 spin_unlock_irq(&desc->iuspin);
501 goto retry;
502 }
Ben Hutchings711c68b2012-02-12 06:00:41 +0000503 cntr = desc->length;
Oliver Neukumafba9372008-05-13 17:01:25 +0200504 spin_unlock_irq(&desc->iuspin);
505 }
506
Ben Hutchings711c68b2012-02-12 06:00:41 +0000507 if (cntr > count)
508 cntr = count;
Oliver Neukumafba9372008-05-13 17:01:25 +0200509 rv = copy_to_user(buffer, desc->ubuf, cntr);
510 if (rv > 0) {
511 rv = -EFAULT;
512 goto err;
513 }
514
Ben Hutchings711c68b2012-02-12 06:00:41 +0000515 spin_lock_irq(&desc->iuspin);
516
Oliver Neukumafba9372008-05-13 17:01:25 +0200517 for (i = 0; i < desc->length - cntr; i++)
518 desc->ubuf[i] = desc->ubuf[i + cntr];
519
520 desc->length -= cntr;
Oliver Neukum87d65e52008-06-19 14:20:18 +0200521 /* in case we had outstanding data */
522 if (!desc->length)
523 clear_bit(WDM_READ, &desc->flags);
Ben Hutchings711c68b2012-02-12 06:00:41 +0000524
525 spin_unlock_irq(&desc->iuspin);
526
Oliver Neukumafba9372008-05-13 17:01:25 +0200527 rv = cntr;
528
529err:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100530 mutex_unlock(&desc->rlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200531 return rv;
532}
533
534static int wdm_flush(struct file *file, fl_owner_t id)
535{
536 struct wdm_device *desc = file->private_data;
537
538 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
Bjørn Mork6b0b79d2012-05-09 13:53:22 +0200539
540 /* cannot dereference desc->intf if WDM_DISCONNECTING */
541 if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags))
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700542 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
543 desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200544
Oliver Neukum24a85ba2012-04-27 14:23:54 +0200545 return usb_translate_errors(desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200546}
547
548static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
549{
550 struct wdm_device *desc = file->private_data;
551 unsigned long flags;
552 unsigned int mask = 0;
553
554 spin_lock_irqsave(&desc->iuspin, flags);
555 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
Bjørn Mork616b6932012-05-09 13:53:21 +0200556 mask = POLLHUP | POLLERR;
Oliver Neukumafba9372008-05-13 17:01:25 +0200557 spin_unlock_irqrestore(&desc->iuspin, flags);
558 goto desc_out;
559 }
560 if (test_bit(WDM_READ, &desc->flags))
561 mask = POLLIN | POLLRDNORM;
562 if (desc->rerr || desc->werr)
563 mask |= POLLERR;
564 if (!test_bit(WDM_IN_USE, &desc->flags))
565 mask |= POLLOUT | POLLWRNORM;
566 spin_unlock_irqrestore(&desc->iuspin, flags);
567
568 poll_wait(file, &desc->wait, wait);
569
570desc_out:
571 return mask;
572}
573
574static int wdm_open(struct inode *inode, struct file *file)
575{
576 int minor = iminor(inode);
577 int rv = -ENODEV;
578 struct usb_interface *intf;
579 struct wdm_device *desc;
580
581 mutex_lock(&wdm_mutex);
Bjørn Morkb0c13862012-03-06 17:29:21 +0100582 desc = wdm_find_device_by_minor(minor);
583 if (!desc)
Oliver Neukumafba9372008-05-13 17:01:25 +0200584 goto out;
585
Bjørn Morkb0c13862012-03-06 17:29:21 +0100586 intf = desc->intf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200587 if (test_bit(WDM_DISCONNECTING, &desc->flags))
588 goto out;
Oliver Neukumafba9372008-05-13 17:01:25 +0200589 file->private_data = desc;
590
Oliver Neukum17d80d52008-06-24 15:56:10 +0200591 rv = usb_autopm_get_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200592 if (rv < 0) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700593 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200594 goto out;
595 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200596
Bjørn Morke8537bd2012-01-16 12:41:48 +0100597 /* using write lock to protect desc->count */
598 mutex_lock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200599 if (!desc->count++) {
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200600 desc->werr = 0;
601 desc->rerr = 0;
Oliver Neukum17d80d52008-06-24 15:56:10 +0200602 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
603 if (rv < 0) {
604 desc->count--;
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700605 dev_err(&desc->intf->dev,
606 "Error submitting int urb - %d\n", rv);
Oliver Neukum12a98b22012-04-30 09:57:31 +0200607 rv = usb_translate_errors(rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200608 }
609 } else {
610 rv = 0;
611 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100612 mutex_unlock(&desc->wlock);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100613 if (desc->count == 1)
614 desc->manage_power(intf, 1);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200615 usb_autopm_put_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200616out:
617 mutex_unlock(&wdm_mutex);
618 return rv;
619}
620
621static int wdm_release(struct inode *inode, struct file *file)
622{
623 struct wdm_device *desc = file->private_data;
624
625 mutex_lock(&wdm_mutex);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100626
627 /* using write lock to protect desc->count */
628 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200629 desc->count--;
Bjørn Morke8537bd2012-01-16 12:41:48 +0100630 mutex_unlock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200631
Oliver Neukumafba9372008-05-13 17:01:25 +0200632 if (!desc->count) {
Bjørn Mork880bca32012-04-30 09:26:11 +0200633 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
Bjørn Mork6b0b79d2012-05-09 13:53:22 +0200634 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
635 kill_urbs(desc);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100636 desc->manage_power(desc->intf, 0);
Bjørn Mork880bca32012-04-30 09:26:11 +0200637 } else {
Bjørn Mork6b0b79d2012-05-09 13:53:22 +0200638 /* must avoid dev_printk here as desc->intf is invalid */
639 pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
Oliver Neukum2f338c82012-04-27 14:36:37 +0200640 cleanup(desc);
Bjørn Mork880bca32012-04-30 09:26:11 +0200641 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200642 }
643 mutex_unlock(&wdm_mutex);
644 return 0;
645}
646
647static const struct file_operations wdm_fops = {
648 .owner = THIS_MODULE,
649 .read = wdm_read,
650 .write = wdm_write,
651 .open = wdm_open,
652 .flush = wdm_flush,
653 .release = wdm_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200654 .poll = wdm_poll,
655 .llseek = noop_llseek,
Oliver Neukumafba9372008-05-13 17:01:25 +0200656};
657
658static struct usb_class_driver wdm_class = {
659 .name = "cdc-wdm%d",
660 .fops = &wdm_fops,
661 .minor_base = WDM_MINOR_BASE,
662};
663
664/* --- error handling --- */
665static void wdm_rxwork(struct work_struct *work)
666{
667 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
668 unsigned long flags;
669 int rv;
670
671 spin_lock_irqsave(&desc->iuspin, flags);
672 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
673 spin_unlock_irqrestore(&desc->iuspin, flags);
674 } else {
675 spin_unlock_irqrestore(&desc->iuspin, flags);
676 rv = usb_submit_urb(desc->response, GFP_KERNEL);
677 if (rv < 0 && rv != -EPERM) {
678 spin_lock_irqsave(&desc->iuspin, flags);
679 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
680 schedule_work(&desc->rxwork);
681 spin_unlock_irqrestore(&desc->iuspin, flags);
682 }
683 }
684}
685
686/* --- hotplug --- */
687
Bjørn Mork3cc36152012-03-06 17:29:22 +0100688static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
689 u16 bufsize, int (*manage_power)(struct usb_interface *, int))
Oliver Neukumafba9372008-05-13 17:01:25 +0200690{
Bjørn Mork0dffb482012-03-06 17:29:20 +0100691 int rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200692 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200693
Oliver Neukumafba9372008-05-13 17:01:25 +0200694 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
695 if (!desc)
696 goto out;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100697 INIT_LIST_HEAD(&desc->device_list);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100698 mutex_init(&desc->rlock);
699 mutex_init(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200700 spin_lock_init(&desc->iuspin);
701 init_waitqueue_head(&desc->wait);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100702 desc->wMaxCommand = bufsize;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200703 /* this will be expanded and needed in hardware endianness */
Oliver Neukumafba9372008-05-13 17:01:25 +0200704 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
705 desc->intf = intf;
706 INIT_WORK(&desc->rxwork, wdm_rxwork);
707
Oliver Neukum052fbc02009-04-20 17:24:49 +0200708 rv = -EINVAL;
Bjørn Mork0dffb482012-03-06 17:29:20 +0100709 if (!usb_endpoint_is_int_in(ep))
Oliver Neukum052fbc02009-04-20 17:24:49 +0200710 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200711
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700712 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
Oliver Neukumafba9372008-05-13 17:01:25 +0200713
714 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
715 if (!desc->orq)
716 goto err;
717 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
718 if (!desc->irq)
719 goto err;
720
721 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
722 if (!desc->validity)
723 goto err;
724
725 desc->response = usb_alloc_urb(0, GFP_KERNEL);
726 if (!desc->response)
727 goto err;
728
729 desc->command = usb_alloc_urb(0, GFP_KERNEL);
730 if (!desc->command)
731 goto err;
732
733 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
734 if (!desc->ubuf)
735 goto err;
736
Bjørn Mork8457d992012-01-16 15:12:00 +0100737 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200738 if (!desc->sbuf)
739 goto err;
740
Bjørn Mork8457d992012-01-16 15:12:00 +0100741 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200742 if (!desc->inbuf)
Bjørn Mork8457d992012-01-16 15:12:00 +0100743 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200744
745 usb_fill_int_urb(
746 desc->validity,
747 interface_to_usbdev(intf),
748 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
749 desc->sbuf,
750 desc->wMaxPacketSize,
751 wdm_int_callback,
752 desc,
753 ep->bInterval
754 );
Oliver Neukumafba9372008-05-13 17:01:25 +0200755
Bjørn Mork19b85b32012-01-16 15:11:58 +0100756 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
757 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
758 desc->irq->wValue = 0;
759 desc->irq->wIndex = desc->inum;
760 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
761
762 usb_fill_control_urb(
763 desc->response,
Bjørn Mork8143a892012-01-16 15:12:01 +0100764 interface_to_usbdev(intf),
Bjørn Mork19b85b32012-01-16 15:11:58 +0100765 /* using common endpoint 0 */
766 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
767 (unsigned char *)desc->irq,
768 desc->inbuf,
769 desc->wMaxCommand,
770 wdm_in_callback,
771 desc
772 );
Bjørn Mork19b85b32012-01-16 15:11:58 +0100773
Bjørn Mork3cc36152012-03-06 17:29:22 +0100774 desc->manage_power = manage_power;
775
Bjørn Morkb0c13862012-03-06 17:29:21 +0100776 spin_lock(&wdm_device_list_lock);
777 list_add(&desc->device_list, &wdm_device_list);
778 spin_unlock(&wdm_device_list_lock);
779
Oliver Neukumafba9372008-05-13 17:01:25 +0200780 rv = usb_register_dev(intf, &wdm_class);
Oliver Neukumafba9372008-05-13 17:01:25 +0200781 if (rv < 0)
Bjørn Morkb0c13862012-03-06 17:29:21 +0100782 goto err;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200783 else
Bjørn Mork820c6292012-01-20 04:17:25 +0100784 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
Oliver Neukumafba9372008-05-13 17:01:25 +0200785out:
786 return rv;
Oliver Neukumafba9372008-05-13 17:01:25 +0200787err:
Bjørn Mork6286d852012-05-09 13:53:23 +0200788 spin_lock(&wdm_device_list_lock);
789 list_del(&desc->device_list);
790 spin_unlock(&wdm_device_list_lock);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100791 cleanup(desc);
792 return rv;
793}
794
Bjørn Mork3cc36152012-03-06 17:29:22 +0100795static int wdm_manage_power(struct usb_interface *intf, int on)
796{
797 /* need autopm_get/put here to ensure the usbcore sees the new value */
798 int rv = usb_autopm_get_interface(intf);
799 if (rv < 0)
800 goto err;
801
802 intf->needs_remote_wakeup = on;
803 usb_autopm_put_interface(intf);
804err:
805 return rv;
806}
807
Bjørn Mork0dffb482012-03-06 17:29:20 +0100808static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
809{
810 int rv = -EINVAL;
811 struct usb_host_interface *iface;
812 struct usb_endpoint_descriptor *ep;
813 struct usb_cdc_dmm_desc *dmhd;
814 u8 *buffer = intf->altsetting->extra;
815 int buflen = intf->altsetting->extralen;
816 u16 maxcom = WDM_DEFAULT_BUFSIZE;
817
818 if (!buffer)
819 goto err;
820 while (buflen > 2) {
821 if (buffer[1] != USB_DT_CS_INTERFACE) {
822 dev_err(&intf->dev, "skipping garbage\n");
823 goto next_desc;
824 }
825
826 switch (buffer[2]) {
827 case USB_CDC_HEADER_TYPE:
828 break;
829 case USB_CDC_DMM_TYPE:
830 dmhd = (struct usb_cdc_dmm_desc *)buffer;
831 maxcom = le16_to_cpu(dmhd->wMaxCommand);
832 dev_dbg(&intf->dev,
833 "Finding maximum buffer length: %d", maxcom);
834 break;
835 default:
836 dev_err(&intf->dev,
837 "Ignoring extra header, type %d, length %d\n",
838 buffer[2], buffer[0]);
839 break;
840 }
841next_desc:
842 buflen -= buffer[0];
843 buffer += buffer[0];
844 }
845
846 iface = intf->cur_altsetting;
847 if (iface->desc.bNumEndpoints != 1)
848 goto err;
849 ep = &iface->endpoint[0].desc;
850
Bjørn Mork3cc36152012-03-06 17:29:22 +0100851 rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100852
853err:
Oliver Neukumafba9372008-05-13 17:01:25 +0200854 return rv;
855}
856
Bjørn Mork3cc36152012-03-06 17:29:22 +0100857/**
858 * usb_cdc_wdm_register - register a WDM subdriver
859 * @intf: usb interface the subdriver will associate with
860 * @ep: interrupt endpoint to monitor for notifications
861 * @bufsize: maximum message size to support for read/write
862 *
863 * Create WDM usb class character device and associate it with intf
864 * without binding, allowing another driver to manage the interface.
865 *
866 * The subdriver will manage the given interrupt endpoint exclusively
867 * and will issue control requests referring to the given intf. It
868 * will otherwise avoid interferring, and in particular not do
869 * usb_set_intfdata/usb_get_intfdata on intf.
870 *
871 * The return value is a pointer to the subdriver's struct usb_driver.
872 * The registering driver is responsible for calling this subdriver's
873 * disconnect, suspend, resume, pre_reset and post_reset methods from
874 * its own.
875 */
876struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
877 struct usb_endpoint_descriptor *ep,
878 int bufsize,
879 int (*manage_power)(struct usb_interface *, int))
880{
881 int rv = -EINVAL;
882
883 rv = wdm_create(intf, ep, bufsize, manage_power);
884 if (rv < 0)
885 goto err;
886
887 return &wdm_driver;
888err:
889 return ERR_PTR(rv);
890}
891EXPORT_SYMBOL(usb_cdc_wdm_register);
892
Oliver Neukumafba9372008-05-13 17:01:25 +0200893static void wdm_disconnect(struct usb_interface *intf)
894{
895 struct wdm_device *desc;
896 unsigned long flags;
897
898 usb_deregister_dev(intf, &wdm_class);
Bjørn Morkb0c13862012-03-06 17:29:21 +0100899 desc = wdm_find_device(intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200900 mutex_lock(&wdm_mutex);
Oliver Neukumafba9372008-05-13 17:01:25 +0200901
902 /* the spinlock makes sure no new urbs are generated in the callbacks */
903 spin_lock_irqsave(&desc->iuspin, flags);
904 set_bit(WDM_DISCONNECTING, &desc->flags);
905 set_bit(WDM_READ, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200906 /* to terminate pending flushes */
Oliver Neukumafba9372008-05-13 17:01:25 +0200907 clear_bit(WDM_IN_USE, &desc->flags);
908 spin_unlock_irqrestore(&desc->iuspin, flags);
Bjørn Mork62aaf242012-01-16 15:11:57 +0100909 wake_up_all(&desc->wait);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100910 mutex_lock(&desc->rlock);
911 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200912 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100913 cancel_work_sync(&desc->rxwork);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100914 mutex_unlock(&desc->wlock);
915 mutex_unlock(&desc->rlock);
Bjørn Mork6286d852012-05-09 13:53:23 +0200916
917 /* the desc->intf pointer used as list key is now invalid */
918 spin_lock(&wdm_device_list_lock);
919 list_del(&desc->device_list);
920 spin_unlock(&wdm_device_list_lock);
921
Oliver Neukumafba9372008-05-13 17:01:25 +0200922 if (!desc->count)
923 cleanup(desc);
Bjørn Mork880bca32012-04-30 09:26:11 +0200924 else
925 dev_dbg(&intf->dev, "%s: %d open files - postponing cleanup\n", __func__, desc->count);
Oliver Neukumafba9372008-05-13 17:01:25 +0200926 mutex_unlock(&wdm_mutex);
927}
928
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100929#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200930static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
931{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100932 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200933 int rv = 0;
934
935 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
936
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100937 /* if this is an autosuspend the caller does the locking */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100938 if (!PMSG_IS_AUTO(message)) {
939 mutex_lock(&desc->rlock);
940 mutex_lock(&desc->wlock);
941 }
Oliver Neukum62e66852010-02-27 20:56:22 +0100942 spin_lock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100943
Alan Stern5b1b0b82011-08-19 23:49:48 +0200944 if (PMSG_IS_AUTO(message) &&
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100945 (test_bit(WDM_IN_USE, &desc->flags)
946 || test_bit(WDM_RESPONDING, &desc->flags))) {
Oliver Neukum62e66852010-02-27 20:56:22 +0100947 spin_unlock_irq(&desc->iuspin);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200948 rv = -EBUSY;
949 } else {
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100950
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100951 set_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100952 spin_unlock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100953 /* callback submits work - order is essential */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200954 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100955 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200956 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100957 if (!PMSG_IS_AUTO(message)) {
958 mutex_unlock(&desc->wlock);
959 mutex_unlock(&desc->rlock);
960 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200961
962 return rv;
963}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100964#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200965
966static int recover_from_urb_loss(struct wdm_device *desc)
967{
968 int rv = 0;
969
970 if (desc->count) {
971 rv = usb_submit_urb(desc->validity, GFP_NOIO);
972 if (rv < 0)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700973 dev_err(&desc->intf->dev,
974 "Error resume submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200975 }
976 return rv;
977}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100978
979#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200980static int wdm_resume(struct usb_interface *intf)
981{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100982 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200983 int rv;
984
985 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
Oliver Neukum338124c2010-02-27 20:57:12 +0100986
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100987 clear_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100988 rv = recover_from_urb_loss(desc);
Oliver Neukum338124c2010-02-27 20:57:12 +0100989
Oliver Neukum17d80d52008-06-24 15:56:10 +0200990 return rv;
991}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100992#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200993
994static int wdm_pre_reset(struct usb_interface *intf)
995{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100996 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200997
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200998 /*
999 * we notify everybody using poll of
1000 * an exceptional situation
1001 * must be done before recovery lest a spontaneous
1002 * message from the device is lost
1003 */
1004 spin_lock_irq(&desc->iuspin);
Bjørn Mork88044202012-02-10 09:44:08 +01001005 set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
1006 set_bit(WDM_READ, &desc->flags); /* unblock read */
1007 clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
Oliver Neukumd771d8a2011-04-29 14:12:21 +02001008 desc->rerr = -EINTR;
1009 spin_unlock_irq(&desc->iuspin);
1010 wake_up_all(&desc->wait);
Bjørn Mork88044202012-02-10 09:44:08 +01001011 mutex_lock(&desc->rlock);
1012 mutex_lock(&desc->wlock);
1013 kill_urbs(desc);
1014 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001015 return 0;
1016}
1017
1018static int wdm_post_reset(struct usb_interface *intf)
1019{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001020 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001021 int rv;
1022
Oliver Neukumc0f5ece2013-03-12 14:52:42 +01001023 clear_bit(WDM_OVERFLOW, &desc->flags);
Bjørn Mork88044202012-02-10 09:44:08 +01001024 clear_bit(WDM_RESETTING, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001025 rv = recover_from_urb_loss(desc);
Bjørn Morke8537bd2012-01-16 12:41:48 +01001026 mutex_unlock(&desc->wlock);
1027 mutex_unlock(&desc->rlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001028 return 0;
1029}
1030
Oliver Neukumafba9372008-05-13 17:01:25 +02001031static struct usb_driver wdm_driver = {
1032 .name = "cdc_wdm",
1033 .probe = wdm_probe,
1034 .disconnect = wdm_disconnect,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001035#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +02001036 .suspend = wdm_suspend,
1037 .resume = wdm_resume,
1038 .reset_resume = wdm_resume,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001039#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +02001040 .pre_reset = wdm_pre_reset,
1041 .post_reset = wdm_post_reset,
Oliver Neukumafba9372008-05-13 17:01:25 +02001042 .id_table = wdm_ids,
Oliver Neukum17d80d52008-06-24 15:56:10 +02001043 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07001044 .disable_hub_initiated_lpm = 1,
Oliver Neukumafba9372008-05-13 17:01:25 +02001045};
1046
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001047module_usb_driver(wdm_driver);
Oliver Neukumafba9372008-05-13 17:01:25 +02001048
1049MODULE_AUTHOR(DRIVER_AUTHOR);
Oliver Neukum87d65e52008-06-19 14:20:18 +02001050MODULE_DESCRIPTION(DRIVER_DESC);
Oliver Neukumafba9372008-05-13 17:01:25 +02001051MODULE_LICENSE("GPL");