blob: 65a55abb791f53dd458f0b23c77af5497142ec6d [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 Neukumafba9372008-05-13 17:01:25 +020059
60#define WDM_MAX 16
61
Bjørn Mork7e3054a2012-01-20 01:49:57 +010062/* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
63#define WDM_DEFAULT_BUFSIZE 256
Oliver Neukumafba9372008-05-13 17:01:25 +020064
65static DEFINE_MUTEX(wdm_mutex);
Bjørn Morkb0c13862012-03-06 17:29:21 +010066static DEFINE_SPINLOCK(wdm_device_list_lock);
67static LIST_HEAD(wdm_device_list);
Oliver Neukumafba9372008-05-13 17:01:25 +020068
69/* --- method tables --- */
70
71struct wdm_device {
72 u8 *inbuf; /* buffer for response */
73 u8 *outbuf; /* buffer for command */
74 u8 *sbuf; /* buffer for status */
75 u8 *ubuf; /* buffer for copy to user space */
76
77 struct urb *command;
78 struct urb *response;
79 struct urb *validity;
80 struct usb_interface *intf;
81 struct usb_ctrlrequest *orq;
82 struct usb_ctrlrequest *irq;
83 spinlock_t iuspin;
84
85 unsigned long flags;
86 u16 bufsize;
87 u16 wMaxCommand;
88 u16 wMaxPacketSize;
Oliver Neukumafba9372008-05-13 17:01:25 +020089 __le16 inum;
90 int reslength;
91 int length;
92 int read;
93 int count;
94 dma_addr_t shandle;
95 dma_addr_t ihandle;
Bjørn Morke8537bd2012-01-16 12:41:48 +010096 struct mutex wlock;
97 struct mutex rlock;
Oliver Neukumafba9372008-05-13 17:01:25 +020098 wait_queue_head_t wait;
99 struct work_struct rxwork;
100 int werr;
101 int rerr;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100102
103 struct list_head device_list;
Bjørn Mork3cc36152012-03-06 17:29:22 +0100104 int (*manage_power)(struct usb_interface *, int);
Oliver Neukumafba9372008-05-13 17:01:25 +0200105};
106
107static struct usb_driver wdm_driver;
108
Bjørn Morkb0c13862012-03-06 17:29:21 +0100109/* return intfdata if we own the interface, else look up intf in the list */
110static struct wdm_device *wdm_find_device(struct usb_interface *intf)
111{
112 struct wdm_device *desc = NULL;
113
114 spin_lock(&wdm_device_list_lock);
115 list_for_each_entry(desc, &wdm_device_list, device_list)
116 if (desc->intf == intf)
117 break;
118 spin_unlock(&wdm_device_list_lock);
119
120 return desc;
121}
122
123static struct wdm_device *wdm_find_device_by_minor(int minor)
124{
125 struct wdm_device *desc = NULL;
126
127 spin_lock(&wdm_device_list_lock);
128 list_for_each_entry(desc, &wdm_device_list, device_list)
129 if (desc->intf->minor == minor)
130 break;
131 spin_unlock(&wdm_device_list_lock);
132
133 return desc;
134}
135
Oliver Neukumafba9372008-05-13 17:01:25 +0200136/* --- callbacks --- */
137static void wdm_out_callback(struct urb *urb)
138{
139 struct wdm_device *desc;
140 desc = urb->context;
141 spin_lock(&desc->iuspin);
142 desc->werr = urb->status;
143 spin_unlock(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200144 kfree(desc->outbuf);
Oliver Neukum5c228372012-04-26 21:59:10 +0200145 desc->outbuf = NULL;
146 clear_bit(WDM_IN_USE, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200147 wake_up(&desc->wait);
148}
149
150static void wdm_in_callback(struct urb *urb)
151{
152 struct wdm_device *desc = urb->context;
153 int status = urb->status;
154
155 spin_lock(&desc->iuspin);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100156 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200157
158 if (status) {
159 switch (status) {
160 case -ENOENT:
161 dev_dbg(&desc->intf->dev,
162 "nonzero urb status received: -ENOENT");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100163 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200164 case -ECONNRESET:
165 dev_dbg(&desc->intf->dev,
166 "nonzero urb status received: -ECONNRESET");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100167 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200168 case -ESHUTDOWN:
169 dev_dbg(&desc->intf->dev,
170 "nonzero urb status received: -ESHUTDOWN");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100171 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200172 case -EPIPE:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700173 dev_err(&desc->intf->dev,
174 "nonzero urb status received: -EPIPE\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200175 break;
176 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700177 dev_err(&desc->intf->dev,
178 "Unexpected error %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200179 break;
180 }
181 }
182
183 desc->rerr = status;
184 desc->reslength = urb->actual_length;
185 memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength);
186 desc->length += desc->reslength;
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100187skip_error:
Oliver Neukumafba9372008-05-13 17:01:25 +0200188 wake_up(&desc->wait);
189
190 set_bit(WDM_READ, &desc->flags);
191 spin_unlock(&desc->iuspin);
192}
193
194static void wdm_int_callback(struct urb *urb)
195{
196 int rv = 0;
197 int status = urb->status;
198 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200199 struct usb_cdc_notification *dr;
200
201 desc = urb->context;
Oliver Neukumafba9372008-05-13 17:01:25 +0200202 dr = (struct usb_cdc_notification *)desc->sbuf;
203
204 if (status) {
205 switch (status) {
206 case -ESHUTDOWN:
207 case -ENOENT:
208 case -ECONNRESET:
209 return; /* unplug */
210 case -EPIPE:
211 set_bit(WDM_INT_STALL, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700212 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200213 goto sw; /* halt is cleared in work */
214 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700215 dev_err(&desc->intf->dev,
216 "nonzero urb status received: %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200217 break;
218 }
219 }
220
221 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700222 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
223 urb->actual_length);
Oliver Neukumafba9372008-05-13 17:01:25 +0200224 goto exit;
225 }
226
227 switch (dr->bNotificationType) {
228 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
229 dev_dbg(&desc->intf->dev,
230 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
231 dr->wIndex, dr->wLength);
232 break;
233
234 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
235
236 dev_dbg(&desc->intf->dev,
237 "NOTIFY_NETWORK_CONNECTION %s network",
238 dr->wValue ? "connected to" : "disconnected from");
239 goto exit;
240 default:
241 clear_bit(WDM_POLL_RUNNING, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700242 dev_err(&desc->intf->dev,
243 "unknown notification %d received: index %d len %d\n",
Oliver Neukumafba9372008-05-13 17:01:25 +0200244 dr->bNotificationType, dr->wIndex, dr->wLength);
245 goto exit;
246 }
247
Oliver Neukumafba9372008-05-13 17:01:25 +0200248 spin_lock(&desc->iuspin);
249 clear_bit(WDM_READ, &desc->flags);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100250 set_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100251 if (!test_bit(WDM_DISCONNECTING, &desc->flags)
252 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200253 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
254 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
255 __func__, rv);
256 }
257 spin_unlock(&desc->iuspin);
258 if (rv < 0) {
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100259 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200260 if (rv == -EPERM)
261 return;
262 if (rv == -ENOMEM) {
263sw:
264 rv = schedule_work(&desc->rxwork);
265 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700266 dev_err(&desc->intf->dev,
267 "Cannot schedule work\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200268 }
269 }
270exit:
271 rv = usb_submit_urb(urb, GFP_ATOMIC);
272 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700273 dev_err(&desc->intf->dev,
274 "%s - usb_submit_urb failed with result %d\n",
275 __func__, rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200276
277}
278
279static void kill_urbs(struct wdm_device *desc)
280{
Oliver Neukum17d80d52008-06-24 15:56:10 +0200281 /* the order here is essential */
Oliver Neukumafba9372008-05-13 17:01:25 +0200282 usb_kill_urb(desc->command);
283 usb_kill_urb(desc->validity);
284 usb_kill_urb(desc->response);
285}
286
287static void free_urbs(struct wdm_device *desc)
288{
289 usb_free_urb(desc->validity);
290 usb_free_urb(desc->response);
291 usb_free_urb(desc->command);
292}
293
294static void cleanup(struct wdm_device *desc)
295{
Bjørn Mork8457d992012-01-16 15:12:00 +0100296 kfree(desc->sbuf);
297 kfree(desc->inbuf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200298 kfree(desc->orq);
299 kfree(desc->irq);
300 kfree(desc->ubuf);
301 free_urbs(desc);
302 kfree(desc);
303}
304
305static ssize_t wdm_write
306(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
307{
308 u8 *buf;
309 int rv = -EMSGSIZE, r, we;
310 struct wdm_device *desc = file->private_data;
311 struct usb_ctrlrequest *req;
312
313 if (count > desc->wMaxCommand)
314 count = desc->wMaxCommand;
315
316 spin_lock_irq(&desc->iuspin);
317 we = desc->werr;
318 desc->werr = 0;
319 spin_unlock_irq(&desc->iuspin);
320 if (we < 0)
321 return -EIO;
322
Oliver Neukum5c228372012-04-26 21:59:10 +0200323 buf = kmalloc(count, GFP_KERNEL);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100324 if (!buf) {
325 rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200326 goto outnl;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100327 }
328
329 r = copy_from_user(buf, buffer, count);
330 if (r > 0) {
331 kfree(buf);
332 rv = -EFAULT;
333 goto outnl;
334 }
335
336 /* concurrent writes and disconnect */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100337 r = mutex_lock_interruptible(&desc->wlock);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100338 rv = -ERESTARTSYS;
339 if (r) {
340 kfree(buf);
341 goto outnl;
342 }
343
344 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
345 kfree(buf);
346 rv = -ENODEV;
347 goto outnp;
348 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200349
Oliver Neukum17d80d52008-06-24 15:56:10 +0200350 r = usb_autopm_get_interface(desc->intf);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100351 if (r < 0) {
352 kfree(buf);
Oliver Neukum12a98b22012-04-30 09:57:31 +0200353 rv = usb_translate_errors(r);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200354 goto outnp;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100355 }
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200356
David Sterba0cdfb812010-12-27 18:49:58 +0100357 if (!(file->f_flags & O_NONBLOCK))
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200358 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
359 &desc->flags));
360 else
361 if (test_bit(WDM_IN_USE, &desc->flags))
362 r = -EAGAIN;
Bjørn Mork88044202012-02-10 09:44:08 +0100363
364 if (test_bit(WDM_RESETTING, &desc->flags))
365 r = -EIO;
366
Oliver Neukum860e41a2010-02-27 20:54:24 +0100367 if (r < 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200368 kfree(buf);
Oliver Neukum12a98b22012-04-30 09:57:31 +0200369 rv = r;
Oliver Neukumafba9372008-05-13 17:01:25 +0200370 goto out;
371 }
372
373 req = desc->orq;
374 usb_fill_control_urb(
375 desc->command,
376 interface_to_usbdev(desc->intf),
377 /* using common endpoint 0 */
378 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
379 (unsigned char *)req,
380 buf,
381 count,
382 wdm_out_callback,
383 desc
384 );
385
386 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
387 USB_RECIP_INTERFACE);
388 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
389 req->wValue = 0;
390 req->wIndex = desc->inum;
391 req->wLength = cpu_to_le16(count);
392 set_bit(WDM_IN_USE, &desc->flags);
Oliver Neukum5c228372012-04-26 21:59:10 +0200393 desc->outbuf = buf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200394
395 rv = usb_submit_urb(desc->command, GFP_KERNEL);
396 if (rv < 0) {
397 kfree(buf);
Oliver Neukum5c228372012-04-26 21:59:10 +0200398 desc->outbuf = NULL;
Oliver Neukumafba9372008-05-13 17:01:25 +0200399 clear_bit(WDM_IN_USE, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700400 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
Oliver Neukum12a98b22012-04-30 09:57:31 +0200401 rv = usb_translate_errors(rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200402 } else {
403 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
404 req->wIndex);
405 }
406out:
Oliver Neukum17d80d52008-06-24 15:56:10 +0200407 usb_autopm_put_interface(desc->intf);
408outnp:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100409 mutex_unlock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200410outnl:
411 return rv < 0 ? rv : count;
412}
413
414static ssize_t wdm_read
415(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
416{
Ben Hutchings711c68b2012-02-12 06:00:41 +0000417 int rv, cntr;
Oliver Neukumafba9372008-05-13 17:01:25 +0200418 int i = 0;
419 struct wdm_device *desc = file->private_data;
420
421
Bjørn Morke8537bd2012-01-16 12:41:48 +0100422 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
Oliver Neukumafba9372008-05-13 17:01:25 +0200423 if (rv < 0)
424 return -ERESTARTSYS;
425
Ben Hutchings711c68b2012-02-12 06:00:41 +0000426 cntr = ACCESS_ONCE(desc->length);
427 if (cntr == 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200428 desc->read = 0;
429retry:
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200430 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
431 rv = -ENODEV;
432 goto err;
433 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200434 i++;
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200435 if (file->f_flags & O_NONBLOCK) {
436 if (!test_bit(WDM_READ, &desc->flags)) {
437 rv = cntr ? cntr : -EAGAIN;
438 goto err;
439 }
440 rv = 0;
441 } else {
442 rv = wait_event_interruptible(desc->wait,
443 test_bit(WDM_READ, &desc->flags));
444 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200445
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200446 /* may have happened while we slept */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200447 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
448 rv = -ENODEV;
449 goto err;
450 }
Bjørn Mork88044202012-02-10 09:44:08 +0100451 if (test_bit(WDM_RESETTING, &desc->flags)) {
452 rv = -EIO;
453 goto err;
454 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200455 usb_mark_last_busy(interface_to_usbdev(desc->intf));
Oliver Neukumafba9372008-05-13 17:01:25 +0200456 if (rv < 0) {
457 rv = -ERESTARTSYS;
458 goto err;
459 }
460
461 spin_lock_irq(&desc->iuspin);
462
463 if (desc->rerr) { /* read completed, error happened */
Oliver Neukumafba9372008-05-13 17:01:25 +0200464 desc->rerr = 0;
465 spin_unlock_irq(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200466 rv = -EIO;
467 goto err;
468 }
469 /*
470 * recheck whether we've lost the race
471 * against the completion handler
472 */
473 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
474 spin_unlock_irq(&desc->iuspin);
475 goto retry;
476 }
477 if (!desc->reslength) { /* zero length read */
Bjørn Morkb086b6b2012-07-02 10:33:14 +0200478 dev_dbg(&desc->intf->dev, "%s: zero length - clearing WDM_READ\n", __func__);
479 clear_bit(WDM_READ, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200480 spin_unlock_irq(&desc->iuspin);
481 goto retry;
482 }
Ben Hutchings711c68b2012-02-12 06:00:41 +0000483 cntr = desc->length;
Oliver Neukumafba9372008-05-13 17:01:25 +0200484 spin_unlock_irq(&desc->iuspin);
485 }
486
Ben Hutchings711c68b2012-02-12 06:00:41 +0000487 if (cntr > count)
488 cntr = count;
Oliver Neukumafba9372008-05-13 17:01:25 +0200489 rv = copy_to_user(buffer, desc->ubuf, cntr);
490 if (rv > 0) {
491 rv = -EFAULT;
492 goto err;
493 }
494
Ben Hutchings711c68b2012-02-12 06:00:41 +0000495 spin_lock_irq(&desc->iuspin);
496
Oliver Neukumafba9372008-05-13 17:01:25 +0200497 for (i = 0; i < desc->length - cntr; i++)
498 desc->ubuf[i] = desc->ubuf[i + cntr];
499
500 desc->length -= cntr;
Oliver Neukum87d65e52008-06-19 14:20:18 +0200501 /* in case we had outstanding data */
502 if (!desc->length)
503 clear_bit(WDM_READ, &desc->flags);
Ben Hutchings711c68b2012-02-12 06:00:41 +0000504
505 spin_unlock_irq(&desc->iuspin);
506
Oliver Neukumafba9372008-05-13 17:01:25 +0200507 rv = cntr;
508
509err:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100510 mutex_unlock(&desc->rlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200511 return rv;
512}
513
514static int wdm_flush(struct file *file, fl_owner_t id)
515{
516 struct wdm_device *desc = file->private_data;
517
518 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
Bjørn Mork6b0b79d2012-05-09 13:53:22 +0200519
520 /* cannot dereference desc->intf if WDM_DISCONNECTING */
521 if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags))
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700522 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
523 desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200524
Oliver Neukum24a85ba2012-04-27 14:23:54 +0200525 return usb_translate_errors(desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200526}
527
528static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
529{
530 struct wdm_device *desc = file->private_data;
531 unsigned long flags;
532 unsigned int mask = 0;
533
534 spin_lock_irqsave(&desc->iuspin, flags);
535 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
Bjørn Mork616b6932012-05-09 13:53:21 +0200536 mask = POLLHUP | POLLERR;
Oliver Neukumafba9372008-05-13 17:01:25 +0200537 spin_unlock_irqrestore(&desc->iuspin, flags);
538 goto desc_out;
539 }
540 if (test_bit(WDM_READ, &desc->flags))
541 mask = POLLIN | POLLRDNORM;
542 if (desc->rerr || desc->werr)
543 mask |= POLLERR;
544 if (!test_bit(WDM_IN_USE, &desc->flags))
545 mask |= POLLOUT | POLLWRNORM;
546 spin_unlock_irqrestore(&desc->iuspin, flags);
547
548 poll_wait(file, &desc->wait, wait);
549
550desc_out:
551 return mask;
552}
553
554static int wdm_open(struct inode *inode, struct file *file)
555{
556 int minor = iminor(inode);
557 int rv = -ENODEV;
558 struct usb_interface *intf;
559 struct wdm_device *desc;
560
561 mutex_lock(&wdm_mutex);
Bjørn Morkb0c13862012-03-06 17:29:21 +0100562 desc = wdm_find_device_by_minor(minor);
563 if (!desc)
Oliver Neukumafba9372008-05-13 17:01:25 +0200564 goto out;
565
Bjørn Morkb0c13862012-03-06 17:29:21 +0100566 intf = desc->intf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200567 if (test_bit(WDM_DISCONNECTING, &desc->flags))
568 goto out;
Oliver Neukumafba9372008-05-13 17:01:25 +0200569 file->private_data = desc;
570
Oliver Neukum17d80d52008-06-24 15:56:10 +0200571 rv = usb_autopm_get_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200572 if (rv < 0) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700573 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200574 goto out;
575 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200576
Bjørn Morke8537bd2012-01-16 12:41:48 +0100577 /* using write lock to protect desc->count */
578 mutex_lock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200579 if (!desc->count++) {
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200580 desc->werr = 0;
581 desc->rerr = 0;
Oliver Neukum17d80d52008-06-24 15:56:10 +0200582 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
583 if (rv < 0) {
584 desc->count--;
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700585 dev_err(&desc->intf->dev,
586 "Error submitting int urb - %d\n", rv);
Oliver Neukum12a98b22012-04-30 09:57:31 +0200587 rv = usb_translate_errors(rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200588 }
589 } else {
590 rv = 0;
591 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100592 mutex_unlock(&desc->wlock);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100593 if (desc->count == 1)
594 desc->manage_power(intf, 1);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200595 usb_autopm_put_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200596out:
597 mutex_unlock(&wdm_mutex);
598 return rv;
599}
600
601static int wdm_release(struct inode *inode, struct file *file)
602{
603 struct wdm_device *desc = file->private_data;
604
605 mutex_lock(&wdm_mutex);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100606
607 /* using write lock to protect desc->count */
608 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200609 desc->count--;
Bjørn Morke8537bd2012-01-16 12:41:48 +0100610 mutex_unlock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200611
Oliver Neukumafba9372008-05-13 17:01:25 +0200612 if (!desc->count) {
Bjørn Mork880bca32012-04-30 09:26:11 +0200613 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
Bjørn Mork6b0b79d2012-05-09 13:53:22 +0200614 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
615 kill_urbs(desc);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100616 desc->manage_power(desc->intf, 0);
Bjørn Mork880bca32012-04-30 09:26:11 +0200617 } else {
Bjørn Mork6b0b79d2012-05-09 13:53:22 +0200618 /* must avoid dev_printk here as desc->intf is invalid */
619 pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
Oliver Neukum2f338c82012-04-27 14:36:37 +0200620 cleanup(desc);
Bjørn Mork880bca32012-04-30 09:26:11 +0200621 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200622 }
623 mutex_unlock(&wdm_mutex);
624 return 0;
625}
626
627static const struct file_operations wdm_fops = {
628 .owner = THIS_MODULE,
629 .read = wdm_read,
630 .write = wdm_write,
631 .open = wdm_open,
632 .flush = wdm_flush,
633 .release = wdm_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200634 .poll = wdm_poll,
635 .llseek = noop_llseek,
Oliver Neukumafba9372008-05-13 17:01:25 +0200636};
637
638static struct usb_class_driver wdm_class = {
639 .name = "cdc-wdm%d",
640 .fops = &wdm_fops,
641 .minor_base = WDM_MINOR_BASE,
642};
643
644/* --- error handling --- */
645static void wdm_rxwork(struct work_struct *work)
646{
647 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
648 unsigned long flags;
649 int rv;
650
651 spin_lock_irqsave(&desc->iuspin, flags);
652 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
653 spin_unlock_irqrestore(&desc->iuspin, flags);
654 } else {
655 spin_unlock_irqrestore(&desc->iuspin, flags);
656 rv = usb_submit_urb(desc->response, GFP_KERNEL);
657 if (rv < 0 && rv != -EPERM) {
658 spin_lock_irqsave(&desc->iuspin, flags);
659 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
660 schedule_work(&desc->rxwork);
661 spin_unlock_irqrestore(&desc->iuspin, flags);
662 }
663 }
664}
665
666/* --- hotplug --- */
667
Bjørn Mork3cc36152012-03-06 17:29:22 +0100668static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
669 u16 bufsize, int (*manage_power)(struct usb_interface *, int))
Oliver Neukumafba9372008-05-13 17:01:25 +0200670{
Bjørn Mork0dffb482012-03-06 17:29:20 +0100671 int rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200672 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200673
Oliver Neukumafba9372008-05-13 17:01:25 +0200674 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
675 if (!desc)
676 goto out;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100677 INIT_LIST_HEAD(&desc->device_list);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100678 mutex_init(&desc->rlock);
679 mutex_init(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200680 spin_lock_init(&desc->iuspin);
681 init_waitqueue_head(&desc->wait);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100682 desc->wMaxCommand = bufsize;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200683 /* this will be expanded and needed in hardware endianness */
Oliver Neukumafba9372008-05-13 17:01:25 +0200684 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
685 desc->intf = intf;
686 INIT_WORK(&desc->rxwork, wdm_rxwork);
687
Oliver Neukum052fbc02009-04-20 17:24:49 +0200688 rv = -EINVAL;
Bjørn Mork0dffb482012-03-06 17:29:20 +0100689 if (!usb_endpoint_is_int_in(ep))
Oliver Neukum052fbc02009-04-20 17:24:49 +0200690 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200691
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700692 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
Oliver Neukumafba9372008-05-13 17:01:25 +0200693
694 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
695 if (!desc->orq)
696 goto err;
697 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
698 if (!desc->irq)
699 goto err;
700
701 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
702 if (!desc->validity)
703 goto err;
704
705 desc->response = usb_alloc_urb(0, GFP_KERNEL);
706 if (!desc->response)
707 goto err;
708
709 desc->command = usb_alloc_urb(0, GFP_KERNEL);
710 if (!desc->command)
711 goto err;
712
713 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
714 if (!desc->ubuf)
715 goto err;
716
Bjørn Mork8457d992012-01-16 15:12:00 +0100717 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200718 if (!desc->sbuf)
719 goto err;
720
Bjørn Mork8457d992012-01-16 15:12:00 +0100721 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200722 if (!desc->inbuf)
Bjørn Mork8457d992012-01-16 15:12:00 +0100723 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200724
725 usb_fill_int_urb(
726 desc->validity,
727 interface_to_usbdev(intf),
728 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
729 desc->sbuf,
730 desc->wMaxPacketSize,
731 wdm_int_callback,
732 desc,
733 ep->bInterval
734 );
Oliver Neukumafba9372008-05-13 17:01:25 +0200735
Bjørn Mork19b85b32012-01-16 15:11:58 +0100736 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
737 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
738 desc->irq->wValue = 0;
739 desc->irq->wIndex = desc->inum;
740 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
741
742 usb_fill_control_urb(
743 desc->response,
Bjørn Mork8143a892012-01-16 15:12:01 +0100744 interface_to_usbdev(intf),
Bjørn Mork19b85b32012-01-16 15:11:58 +0100745 /* using common endpoint 0 */
746 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
747 (unsigned char *)desc->irq,
748 desc->inbuf,
749 desc->wMaxCommand,
750 wdm_in_callback,
751 desc
752 );
Bjørn Mork19b85b32012-01-16 15:11:58 +0100753
Bjørn Mork3cc36152012-03-06 17:29:22 +0100754 desc->manage_power = manage_power;
755
Bjørn Morkb0c13862012-03-06 17:29:21 +0100756 spin_lock(&wdm_device_list_lock);
757 list_add(&desc->device_list, &wdm_device_list);
758 spin_unlock(&wdm_device_list_lock);
759
Oliver Neukumafba9372008-05-13 17:01:25 +0200760 rv = usb_register_dev(intf, &wdm_class);
Oliver Neukumafba9372008-05-13 17:01:25 +0200761 if (rv < 0)
Bjørn Morkb0c13862012-03-06 17:29:21 +0100762 goto err;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200763 else
Bjørn Mork820c6292012-01-20 04:17:25 +0100764 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
Oliver Neukumafba9372008-05-13 17:01:25 +0200765out:
766 return rv;
Oliver Neukumafba9372008-05-13 17:01:25 +0200767err:
Bjørn Mork6286d852012-05-09 13:53:23 +0200768 spin_lock(&wdm_device_list_lock);
769 list_del(&desc->device_list);
770 spin_unlock(&wdm_device_list_lock);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100771 cleanup(desc);
772 return rv;
773}
774
Bjørn Mork3cc36152012-03-06 17:29:22 +0100775static int wdm_manage_power(struct usb_interface *intf, int on)
776{
777 /* need autopm_get/put here to ensure the usbcore sees the new value */
778 int rv = usb_autopm_get_interface(intf);
779 if (rv < 0)
780 goto err;
781
782 intf->needs_remote_wakeup = on;
783 usb_autopm_put_interface(intf);
784err:
785 return rv;
786}
787
Bjørn Mork0dffb482012-03-06 17:29:20 +0100788static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
789{
790 int rv = -EINVAL;
791 struct usb_host_interface *iface;
792 struct usb_endpoint_descriptor *ep;
793 struct usb_cdc_dmm_desc *dmhd;
794 u8 *buffer = intf->altsetting->extra;
795 int buflen = intf->altsetting->extralen;
796 u16 maxcom = WDM_DEFAULT_BUFSIZE;
797
798 if (!buffer)
799 goto err;
800 while (buflen > 2) {
801 if (buffer[1] != USB_DT_CS_INTERFACE) {
802 dev_err(&intf->dev, "skipping garbage\n");
803 goto next_desc;
804 }
805
806 switch (buffer[2]) {
807 case USB_CDC_HEADER_TYPE:
808 break;
809 case USB_CDC_DMM_TYPE:
810 dmhd = (struct usb_cdc_dmm_desc *)buffer;
811 maxcom = le16_to_cpu(dmhd->wMaxCommand);
812 dev_dbg(&intf->dev,
813 "Finding maximum buffer length: %d", maxcom);
814 break;
815 default:
816 dev_err(&intf->dev,
817 "Ignoring extra header, type %d, length %d\n",
818 buffer[2], buffer[0]);
819 break;
820 }
821next_desc:
822 buflen -= buffer[0];
823 buffer += buffer[0];
824 }
825
826 iface = intf->cur_altsetting;
827 if (iface->desc.bNumEndpoints != 1)
828 goto err;
829 ep = &iface->endpoint[0].desc;
830
Bjørn Mork3cc36152012-03-06 17:29:22 +0100831 rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100832
833err:
Oliver Neukumafba9372008-05-13 17:01:25 +0200834 return rv;
835}
836
Bjørn Mork3cc36152012-03-06 17:29:22 +0100837/**
838 * usb_cdc_wdm_register - register a WDM subdriver
839 * @intf: usb interface the subdriver will associate with
840 * @ep: interrupt endpoint to monitor for notifications
841 * @bufsize: maximum message size to support for read/write
842 *
843 * Create WDM usb class character device and associate it with intf
844 * without binding, allowing another driver to manage the interface.
845 *
846 * The subdriver will manage the given interrupt endpoint exclusively
847 * and will issue control requests referring to the given intf. It
848 * will otherwise avoid interferring, and in particular not do
849 * usb_set_intfdata/usb_get_intfdata on intf.
850 *
851 * The return value is a pointer to the subdriver's struct usb_driver.
852 * The registering driver is responsible for calling this subdriver's
853 * disconnect, suspend, resume, pre_reset and post_reset methods from
854 * its own.
855 */
856struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
857 struct usb_endpoint_descriptor *ep,
858 int bufsize,
859 int (*manage_power)(struct usb_interface *, int))
860{
861 int rv = -EINVAL;
862
863 rv = wdm_create(intf, ep, bufsize, manage_power);
864 if (rv < 0)
865 goto err;
866
867 return &wdm_driver;
868err:
869 return ERR_PTR(rv);
870}
871EXPORT_SYMBOL(usb_cdc_wdm_register);
872
Oliver Neukumafba9372008-05-13 17:01:25 +0200873static void wdm_disconnect(struct usb_interface *intf)
874{
875 struct wdm_device *desc;
876 unsigned long flags;
877
878 usb_deregister_dev(intf, &wdm_class);
Bjørn Morkb0c13862012-03-06 17:29:21 +0100879 desc = wdm_find_device(intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200880 mutex_lock(&wdm_mutex);
Oliver Neukumafba9372008-05-13 17:01:25 +0200881
882 /* the spinlock makes sure no new urbs are generated in the callbacks */
883 spin_lock_irqsave(&desc->iuspin, flags);
884 set_bit(WDM_DISCONNECTING, &desc->flags);
885 set_bit(WDM_READ, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200886 /* to terminate pending flushes */
Oliver Neukumafba9372008-05-13 17:01:25 +0200887 clear_bit(WDM_IN_USE, &desc->flags);
888 spin_unlock_irqrestore(&desc->iuspin, flags);
Bjørn Mork62aaf242012-01-16 15:11:57 +0100889 wake_up_all(&desc->wait);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100890 mutex_lock(&desc->rlock);
891 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200892 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100893 cancel_work_sync(&desc->rxwork);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100894 mutex_unlock(&desc->wlock);
895 mutex_unlock(&desc->rlock);
Bjørn Mork6286d852012-05-09 13:53:23 +0200896
897 /* the desc->intf pointer used as list key is now invalid */
898 spin_lock(&wdm_device_list_lock);
899 list_del(&desc->device_list);
900 spin_unlock(&wdm_device_list_lock);
901
Oliver Neukumafba9372008-05-13 17:01:25 +0200902 if (!desc->count)
903 cleanup(desc);
Bjørn Mork880bca32012-04-30 09:26:11 +0200904 else
905 dev_dbg(&intf->dev, "%s: %d open files - postponing cleanup\n", __func__, desc->count);
Oliver Neukumafba9372008-05-13 17:01:25 +0200906 mutex_unlock(&wdm_mutex);
907}
908
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100909#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200910static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
911{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100912 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200913 int rv = 0;
914
915 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
916
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100917 /* if this is an autosuspend the caller does the locking */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100918 if (!PMSG_IS_AUTO(message)) {
919 mutex_lock(&desc->rlock);
920 mutex_lock(&desc->wlock);
921 }
Oliver Neukum62e66852010-02-27 20:56:22 +0100922 spin_lock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100923
Alan Stern5b1b0b82011-08-19 23:49:48 +0200924 if (PMSG_IS_AUTO(message) &&
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100925 (test_bit(WDM_IN_USE, &desc->flags)
926 || test_bit(WDM_RESPONDING, &desc->flags))) {
Oliver Neukum62e66852010-02-27 20:56:22 +0100927 spin_unlock_irq(&desc->iuspin);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200928 rv = -EBUSY;
929 } else {
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100930
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100931 set_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100932 spin_unlock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100933 /* callback submits work - order is essential */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200934 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100935 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200936 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100937 if (!PMSG_IS_AUTO(message)) {
938 mutex_unlock(&desc->wlock);
939 mutex_unlock(&desc->rlock);
940 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200941
942 return rv;
943}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100944#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200945
946static int recover_from_urb_loss(struct wdm_device *desc)
947{
948 int rv = 0;
949
950 if (desc->count) {
951 rv = usb_submit_urb(desc->validity, GFP_NOIO);
952 if (rv < 0)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700953 dev_err(&desc->intf->dev,
954 "Error resume submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200955 }
956 return rv;
957}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100958
959#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200960static int wdm_resume(struct usb_interface *intf)
961{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100962 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200963 int rv;
964
965 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
Oliver Neukum338124c2010-02-27 20:57:12 +0100966
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100967 clear_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100968 rv = recover_from_urb_loss(desc);
Oliver Neukum338124c2010-02-27 20:57:12 +0100969
Oliver Neukum17d80d52008-06-24 15:56:10 +0200970 return rv;
971}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100972#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200973
974static int wdm_pre_reset(struct usb_interface *intf)
975{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100976 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200977
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200978 /*
979 * we notify everybody using poll of
980 * an exceptional situation
981 * must be done before recovery lest a spontaneous
982 * message from the device is lost
983 */
984 spin_lock_irq(&desc->iuspin);
Bjørn Mork88044202012-02-10 09:44:08 +0100985 set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
986 set_bit(WDM_READ, &desc->flags); /* unblock read */
987 clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200988 desc->rerr = -EINTR;
989 spin_unlock_irq(&desc->iuspin);
990 wake_up_all(&desc->wait);
Bjørn Mork88044202012-02-10 09:44:08 +0100991 mutex_lock(&desc->rlock);
992 mutex_lock(&desc->wlock);
993 kill_urbs(desc);
994 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200995 return 0;
996}
997
998static int wdm_post_reset(struct usb_interface *intf)
999{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001000 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001001 int rv;
1002
Bjørn Mork88044202012-02-10 09:44:08 +01001003 clear_bit(WDM_RESETTING, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001004 rv = recover_from_urb_loss(desc);
Bjørn Morke8537bd2012-01-16 12:41:48 +01001005 mutex_unlock(&desc->wlock);
1006 mutex_unlock(&desc->rlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001007 return 0;
1008}
1009
Oliver Neukumafba9372008-05-13 17:01:25 +02001010static struct usb_driver wdm_driver = {
1011 .name = "cdc_wdm",
1012 .probe = wdm_probe,
1013 .disconnect = wdm_disconnect,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001014#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +02001015 .suspend = wdm_suspend,
1016 .resume = wdm_resume,
1017 .reset_resume = wdm_resume,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001018#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +02001019 .pre_reset = wdm_pre_reset,
1020 .post_reset = wdm_post_reset,
Oliver Neukumafba9372008-05-13 17:01:25 +02001021 .id_table = wdm_ids,
Oliver Neukum17d80d52008-06-24 15:56:10 +02001022 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07001023 .disable_hub_initiated_lpm = 1,
Oliver Neukumafba9372008-05-13 17:01:25 +02001024};
1025
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001026module_usb_driver(wdm_driver);
Oliver Neukumafba9372008-05-13 17:01:25 +02001027
1028MODULE_AUTHOR(DRIVER_AUTHOR);
Oliver Neukum87d65e52008-06-19 14:20:18 +02001029MODULE_DESCRIPTION(DRIVER_DESC);
Oliver Neukumafba9372008-05-13 17:01:25 +02001030MODULE_LICENSE("GPL");