blob: 5f0cb417b736bb4b61c73afc52672029af59cbda [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{
Bjørn Mork6a448862012-09-10 22:17:34 +0200112 struct wdm_device *desc;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100113
114 spin_lock(&wdm_device_list_lock);
115 list_for_each_entry(desc, &wdm_device_list, device_list)
116 if (desc->intf == intf)
Bjørn Mork6a448862012-09-10 22:17:34 +0200117 goto found;
118 desc = NULL;
119found:
Bjørn Morkb0c13862012-03-06 17:29:21 +0100120 spin_unlock(&wdm_device_list_lock);
121
122 return desc;
123}
124
125static struct wdm_device *wdm_find_device_by_minor(int minor)
126{
Bjørn Mork6a448862012-09-10 22:17:34 +0200127 struct wdm_device *desc;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100128
129 spin_lock(&wdm_device_list_lock);
130 list_for_each_entry(desc, &wdm_device_list, device_list)
131 if (desc->intf->minor == minor)
Bjørn Mork6a448862012-09-10 22:17:34 +0200132 goto found;
133 desc = NULL;
134found:
Bjørn Morkb0c13862012-03-06 17:29:21 +0100135 spin_unlock(&wdm_device_list_lock);
136
137 return desc;
138}
139
Oliver Neukumafba9372008-05-13 17:01:25 +0200140/* --- callbacks --- */
141static void wdm_out_callback(struct urb *urb)
142{
143 struct wdm_device *desc;
144 desc = urb->context;
145 spin_lock(&desc->iuspin);
146 desc->werr = urb->status;
147 spin_unlock(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200148 kfree(desc->outbuf);
Oliver Neukum5c228372012-04-26 21:59:10 +0200149 desc->outbuf = NULL;
150 clear_bit(WDM_IN_USE, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200151 wake_up(&desc->wait);
152}
153
154static void wdm_in_callback(struct urb *urb)
155{
156 struct wdm_device *desc = urb->context;
157 int status = urb->status;
158
159 spin_lock(&desc->iuspin);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100160 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200161
162 if (status) {
163 switch (status) {
164 case -ENOENT:
165 dev_dbg(&desc->intf->dev,
166 "nonzero urb status received: -ENOENT");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100167 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200168 case -ECONNRESET:
169 dev_dbg(&desc->intf->dev,
170 "nonzero urb status received: -ECONNRESET");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100171 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200172 case -ESHUTDOWN:
173 dev_dbg(&desc->intf->dev,
174 "nonzero urb status received: -ESHUTDOWN");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100175 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200176 case -EPIPE:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700177 dev_err(&desc->intf->dev,
178 "nonzero urb status received: -EPIPE\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200179 break;
180 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700181 dev_err(&desc->intf->dev,
182 "Unexpected error %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200183 break;
184 }
185 }
186
187 desc->rerr = status;
188 desc->reslength = urb->actual_length;
189 memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength);
190 desc->length += desc->reslength;
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100191skip_error:
Oliver Neukumafba9372008-05-13 17:01:25 +0200192 wake_up(&desc->wait);
193
194 set_bit(WDM_READ, &desc->flags);
195 spin_unlock(&desc->iuspin);
196}
197
198static void wdm_int_callback(struct urb *urb)
199{
200 int rv = 0;
201 int status = urb->status;
202 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200203 struct usb_cdc_notification *dr;
204
205 desc = urb->context;
Oliver Neukumafba9372008-05-13 17:01:25 +0200206 dr = (struct usb_cdc_notification *)desc->sbuf;
207
208 if (status) {
209 switch (status) {
210 case -ESHUTDOWN:
211 case -ENOENT:
212 case -ECONNRESET:
213 return; /* unplug */
214 case -EPIPE:
215 set_bit(WDM_INT_STALL, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700216 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200217 goto sw; /* halt is cleared in work */
218 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700219 dev_err(&desc->intf->dev,
220 "nonzero urb status received: %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200221 break;
222 }
223 }
224
225 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700226 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
227 urb->actual_length);
Oliver Neukumafba9372008-05-13 17:01:25 +0200228 goto exit;
229 }
230
231 switch (dr->bNotificationType) {
232 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
233 dev_dbg(&desc->intf->dev,
234 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
235 dr->wIndex, dr->wLength);
236 break;
237
238 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
239
240 dev_dbg(&desc->intf->dev,
241 "NOTIFY_NETWORK_CONNECTION %s network",
242 dr->wValue ? "connected to" : "disconnected from");
243 goto exit;
244 default:
245 clear_bit(WDM_POLL_RUNNING, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700246 dev_err(&desc->intf->dev,
247 "unknown notification %d received: index %d len %d\n",
Oliver Neukumafba9372008-05-13 17:01:25 +0200248 dr->bNotificationType, dr->wIndex, dr->wLength);
249 goto exit;
250 }
251
Oliver Neukumafba9372008-05-13 17:01:25 +0200252 spin_lock(&desc->iuspin);
253 clear_bit(WDM_READ, &desc->flags);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100254 set_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100255 if (!test_bit(WDM_DISCONNECTING, &desc->flags)
256 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200257 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
258 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
259 __func__, rv);
260 }
261 spin_unlock(&desc->iuspin);
262 if (rv < 0) {
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100263 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200264 if (rv == -EPERM)
265 return;
266 if (rv == -ENOMEM) {
267sw:
268 rv = schedule_work(&desc->rxwork);
269 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700270 dev_err(&desc->intf->dev,
271 "Cannot schedule work\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200272 }
273 }
274exit:
275 rv = usb_submit_urb(urb, GFP_ATOMIC);
276 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700277 dev_err(&desc->intf->dev,
278 "%s - usb_submit_urb failed with result %d\n",
279 __func__, rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200280
281}
282
283static void kill_urbs(struct wdm_device *desc)
284{
Oliver Neukum17d80d52008-06-24 15:56:10 +0200285 /* the order here is essential */
Oliver Neukumafba9372008-05-13 17:01:25 +0200286 usb_kill_urb(desc->command);
287 usb_kill_urb(desc->validity);
288 usb_kill_urb(desc->response);
289}
290
291static void free_urbs(struct wdm_device *desc)
292{
293 usb_free_urb(desc->validity);
294 usb_free_urb(desc->response);
295 usb_free_urb(desc->command);
296}
297
298static void cleanup(struct wdm_device *desc)
299{
Bjørn Mork8457d992012-01-16 15:12:00 +0100300 kfree(desc->sbuf);
301 kfree(desc->inbuf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200302 kfree(desc->orq);
303 kfree(desc->irq);
304 kfree(desc->ubuf);
305 free_urbs(desc);
306 kfree(desc);
307}
308
309static ssize_t wdm_write
310(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
311{
312 u8 *buf;
313 int rv = -EMSGSIZE, r, we;
314 struct wdm_device *desc = file->private_data;
315 struct usb_ctrlrequest *req;
316
317 if (count > desc->wMaxCommand)
318 count = desc->wMaxCommand;
319
320 spin_lock_irq(&desc->iuspin);
321 we = desc->werr;
322 desc->werr = 0;
323 spin_unlock_irq(&desc->iuspin);
324 if (we < 0)
325 return -EIO;
326
Oliver Neukum5c228372012-04-26 21:59:10 +0200327 buf = kmalloc(count, GFP_KERNEL);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100328 if (!buf) {
329 rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200330 goto outnl;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100331 }
332
333 r = copy_from_user(buf, buffer, count);
334 if (r > 0) {
335 kfree(buf);
336 rv = -EFAULT;
337 goto outnl;
338 }
339
340 /* concurrent writes and disconnect */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100341 r = mutex_lock_interruptible(&desc->wlock);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100342 rv = -ERESTARTSYS;
343 if (r) {
344 kfree(buf);
345 goto outnl;
346 }
347
348 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
349 kfree(buf);
350 rv = -ENODEV;
351 goto outnp;
352 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200353
Oliver Neukum17d80d52008-06-24 15:56:10 +0200354 r = usb_autopm_get_interface(desc->intf);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100355 if (r < 0) {
356 kfree(buf);
Oliver Neukum12a98b22012-04-30 09:57:31 +0200357 rv = usb_translate_errors(r);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200358 goto outnp;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100359 }
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200360
David Sterba0cdfb812010-12-27 18:49:58 +0100361 if (!(file->f_flags & O_NONBLOCK))
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200362 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
363 &desc->flags));
364 else
365 if (test_bit(WDM_IN_USE, &desc->flags))
366 r = -EAGAIN;
Bjørn Mork88044202012-02-10 09:44:08 +0100367
368 if (test_bit(WDM_RESETTING, &desc->flags))
369 r = -EIO;
370
Oliver Neukum860e41a2010-02-27 20:54:24 +0100371 if (r < 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200372 kfree(buf);
Oliver Neukum12a98b22012-04-30 09:57:31 +0200373 rv = r;
Oliver Neukumafba9372008-05-13 17:01:25 +0200374 goto out;
375 }
376
377 req = desc->orq;
378 usb_fill_control_urb(
379 desc->command,
380 interface_to_usbdev(desc->intf),
381 /* using common endpoint 0 */
382 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
383 (unsigned char *)req,
384 buf,
385 count,
386 wdm_out_callback,
387 desc
388 );
389
390 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
391 USB_RECIP_INTERFACE);
392 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
393 req->wValue = 0;
394 req->wIndex = desc->inum;
395 req->wLength = cpu_to_le16(count);
396 set_bit(WDM_IN_USE, &desc->flags);
Oliver Neukum5c228372012-04-26 21:59:10 +0200397 desc->outbuf = buf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200398
399 rv = usb_submit_urb(desc->command, GFP_KERNEL);
400 if (rv < 0) {
401 kfree(buf);
Oliver Neukum5c228372012-04-26 21:59:10 +0200402 desc->outbuf = NULL;
Oliver Neukumafba9372008-05-13 17:01:25 +0200403 clear_bit(WDM_IN_USE, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700404 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
Oliver Neukum12a98b22012-04-30 09:57:31 +0200405 rv = usb_translate_errors(rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200406 } else {
407 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
408 req->wIndex);
409 }
410out:
Oliver Neukum17d80d52008-06-24 15:56:10 +0200411 usb_autopm_put_interface(desc->intf);
412outnp:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100413 mutex_unlock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200414outnl:
415 return rv < 0 ? rv : count;
416}
417
418static ssize_t wdm_read
419(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
420{
Ben Hutchings711c68b2012-02-12 06:00:41 +0000421 int rv, cntr;
Oliver Neukumafba9372008-05-13 17:01:25 +0200422 int i = 0;
423 struct wdm_device *desc = file->private_data;
424
425
Bjørn Morke8537bd2012-01-16 12:41:48 +0100426 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
Oliver Neukumafba9372008-05-13 17:01:25 +0200427 if (rv < 0)
428 return -ERESTARTSYS;
429
Ben Hutchings711c68b2012-02-12 06:00:41 +0000430 cntr = ACCESS_ONCE(desc->length);
431 if (cntr == 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200432 desc->read = 0;
433retry:
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200434 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
435 rv = -ENODEV;
436 goto err;
437 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200438 i++;
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200439 if (file->f_flags & O_NONBLOCK) {
440 if (!test_bit(WDM_READ, &desc->flags)) {
441 rv = cntr ? cntr : -EAGAIN;
442 goto err;
443 }
444 rv = 0;
445 } else {
446 rv = wait_event_interruptible(desc->wait,
447 test_bit(WDM_READ, &desc->flags));
448 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200449
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200450 /* may have happened while we slept */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200451 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
452 rv = -ENODEV;
453 goto err;
454 }
Bjørn Mork88044202012-02-10 09:44:08 +0100455 if (test_bit(WDM_RESETTING, &desc->flags)) {
456 rv = -EIO;
457 goto err;
458 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200459 usb_mark_last_busy(interface_to_usbdev(desc->intf));
Oliver Neukumafba9372008-05-13 17:01:25 +0200460 if (rv < 0) {
461 rv = -ERESTARTSYS;
462 goto err;
463 }
464
465 spin_lock_irq(&desc->iuspin);
466
467 if (desc->rerr) { /* read completed, error happened */
Oliver Neukumafba9372008-05-13 17:01:25 +0200468 desc->rerr = 0;
469 spin_unlock_irq(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200470 rv = -EIO;
471 goto err;
472 }
473 /*
474 * recheck whether we've lost the race
475 * against the completion handler
476 */
477 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
478 spin_unlock_irq(&desc->iuspin);
479 goto retry;
480 }
481 if (!desc->reslength) { /* zero length read */
Bjørn Morkb086b6b2012-07-02 10:33:14 +0200482 dev_dbg(&desc->intf->dev, "%s: zero length - clearing WDM_READ\n", __func__);
483 clear_bit(WDM_READ, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200484 spin_unlock_irq(&desc->iuspin);
485 goto retry;
486 }
Ben Hutchings711c68b2012-02-12 06:00:41 +0000487 cntr = desc->length;
Oliver Neukumafba9372008-05-13 17:01:25 +0200488 spin_unlock_irq(&desc->iuspin);
489 }
490
Ben Hutchings711c68b2012-02-12 06:00:41 +0000491 if (cntr > count)
492 cntr = count;
Oliver Neukumafba9372008-05-13 17:01:25 +0200493 rv = copy_to_user(buffer, desc->ubuf, cntr);
494 if (rv > 0) {
495 rv = -EFAULT;
496 goto err;
497 }
498
Ben Hutchings711c68b2012-02-12 06:00:41 +0000499 spin_lock_irq(&desc->iuspin);
500
Oliver Neukumafba9372008-05-13 17:01:25 +0200501 for (i = 0; i < desc->length - cntr; i++)
502 desc->ubuf[i] = desc->ubuf[i + cntr];
503
504 desc->length -= cntr;
Oliver Neukum87d65e52008-06-19 14:20:18 +0200505 /* in case we had outstanding data */
506 if (!desc->length)
507 clear_bit(WDM_READ, &desc->flags);
Ben Hutchings711c68b2012-02-12 06:00:41 +0000508
509 spin_unlock_irq(&desc->iuspin);
510
Oliver Neukumafba9372008-05-13 17:01:25 +0200511 rv = cntr;
512
513err:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100514 mutex_unlock(&desc->rlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200515 return rv;
516}
517
518static int wdm_flush(struct file *file, fl_owner_t id)
519{
520 struct wdm_device *desc = file->private_data;
521
522 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
Bjørn Mork6b0b79d2012-05-09 13:53:22 +0200523
524 /* cannot dereference desc->intf if WDM_DISCONNECTING */
525 if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags))
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700526 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
527 desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200528
Oliver Neukum24a85ba2012-04-27 14:23:54 +0200529 return usb_translate_errors(desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200530}
531
532static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
533{
534 struct wdm_device *desc = file->private_data;
535 unsigned long flags;
536 unsigned int mask = 0;
537
538 spin_lock_irqsave(&desc->iuspin, flags);
539 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
Bjørn Mork616b6932012-05-09 13:53:21 +0200540 mask = POLLHUP | POLLERR;
Oliver Neukumafba9372008-05-13 17:01:25 +0200541 spin_unlock_irqrestore(&desc->iuspin, flags);
542 goto desc_out;
543 }
544 if (test_bit(WDM_READ, &desc->flags))
545 mask = POLLIN | POLLRDNORM;
546 if (desc->rerr || desc->werr)
547 mask |= POLLERR;
548 if (!test_bit(WDM_IN_USE, &desc->flags))
549 mask |= POLLOUT | POLLWRNORM;
550 spin_unlock_irqrestore(&desc->iuspin, flags);
551
552 poll_wait(file, &desc->wait, wait);
553
554desc_out:
555 return mask;
556}
557
558static int wdm_open(struct inode *inode, struct file *file)
559{
560 int minor = iminor(inode);
561 int rv = -ENODEV;
562 struct usb_interface *intf;
563 struct wdm_device *desc;
564
565 mutex_lock(&wdm_mutex);
Bjørn Morkb0c13862012-03-06 17:29:21 +0100566 desc = wdm_find_device_by_minor(minor);
567 if (!desc)
Oliver Neukumafba9372008-05-13 17:01:25 +0200568 goto out;
569
Bjørn Morkb0c13862012-03-06 17:29:21 +0100570 intf = desc->intf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200571 if (test_bit(WDM_DISCONNECTING, &desc->flags))
572 goto out;
Oliver Neukumafba9372008-05-13 17:01:25 +0200573 file->private_data = desc;
574
Oliver Neukum17d80d52008-06-24 15:56:10 +0200575 rv = usb_autopm_get_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200576 if (rv < 0) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700577 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200578 goto out;
579 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200580
Bjørn Morke8537bd2012-01-16 12:41:48 +0100581 /* using write lock to protect desc->count */
582 mutex_lock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200583 if (!desc->count++) {
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200584 desc->werr = 0;
585 desc->rerr = 0;
Oliver Neukum17d80d52008-06-24 15:56:10 +0200586 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
587 if (rv < 0) {
588 desc->count--;
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700589 dev_err(&desc->intf->dev,
590 "Error submitting int urb - %d\n", rv);
Oliver Neukum12a98b22012-04-30 09:57:31 +0200591 rv = usb_translate_errors(rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200592 }
593 } else {
594 rv = 0;
595 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100596 mutex_unlock(&desc->wlock);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100597 if (desc->count == 1)
598 desc->manage_power(intf, 1);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200599 usb_autopm_put_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200600out:
601 mutex_unlock(&wdm_mutex);
602 return rv;
603}
604
605static int wdm_release(struct inode *inode, struct file *file)
606{
607 struct wdm_device *desc = file->private_data;
608
609 mutex_lock(&wdm_mutex);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100610
611 /* using write lock to protect desc->count */
612 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200613 desc->count--;
Bjørn Morke8537bd2012-01-16 12:41:48 +0100614 mutex_unlock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200615
Oliver Neukumafba9372008-05-13 17:01:25 +0200616 if (!desc->count) {
Bjørn Mork880bca32012-04-30 09:26:11 +0200617 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
Bjørn Mork6b0b79d2012-05-09 13:53:22 +0200618 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
619 kill_urbs(desc);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100620 desc->manage_power(desc->intf, 0);
Bjørn Mork880bca32012-04-30 09:26:11 +0200621 } else {
Bjørn Mork6b0b79d2012-05-09 13:53:22 +0200622 /* must avoid dev_printk here as desc->intf is invalid */
623 pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
Oliver Neukum2f338c82012-04-27 14:36:37 +0200624 cleanup(desc);
Bjørn Mork880bca32012-04-30 09:26:11 +0200625 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200626 }
627 mutex_unlock(&wdm_mutex);
628 return 0;
629}
630
631static const struct file_operations wdm_fops = {
632 .owner = THIS_MODULE,
633 .read = wdm_read,
634 .write = wdm_write,
635 .open = wdm_open,
636 .flush = wdm_flush,
637 .release = wdm_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200638 .poll = wdm_poll,
639 .llseek = noop_llseek,
Oliver Neukumafba9372008-05-13 17:01:25 +0200640};
641
642static struct usb_class_driver wdm_class = {
643 .name = "cdc-wdm%d",
644 .fops = &wdm_fops,
645 .minor_base = WDM_MINOR_BASE,
646};
647
648/* --- error handling --- */
649static void wdm_rxwork(struct work_struct *work)
650{
651 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
652 unsigned long flags;
653 int rv;
654
655 spin_lock_irqsave(&desc->iuspin, flags);
656 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
657 spin_unlock_irqrestore(&desc->iuspin, flags);
658 } else {
659 spin_unlock_irqrestore(&desc->iuspin, flags);
660 rv = usb_submit_urb(desc->response, GFP_KERNEL);
661 if (rv < 0 && rv != -EPERM) {
662 spin_lock_irqsave(&desc->iuspin, flags);
663 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
664 schedule_work(&desc->rxwork);
665 spin_unlock_irqrestore(&desc->iuspin, flags);
666 }
667 }
668}
669
670/* --- hotplug --- */
671
Bjørn Mork3cc36152012-03-06 17:29:22 +0100672static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
673 u16 bufsize, int (*manage_power)(struct usb_interface *, int))
Oliver Neukumafba9372008-05-13 17:01:25 +0200674{
Bjørn Mork0dffb482012-03-06 17:29:20 +0100675 int rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200676 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200677
Oliver Neukumafba9372008-05-13 17:01:25 +0200678 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
679 if (!desc)
680 goto out;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100681 INIT_LIST_HEAD(&desc->device_list);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100682 mutex_init(&desc->rlock);
683 mutex_init(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200684 spin_lock_init(&desc->iuspin);
685 init_waitqueue_head(&desc->wait);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100686 desc->wMaxCommand = bufsize;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200687 /* this will be expanded and needed in hardware endianness */
Oliver Neukumafba9372008-05-13 17:01:25 +0200688 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
689 desc->intf = intf;
690 INIT_WORK(&desc->rxwork, wdm_rxwork);
691
Oliver Neukum052fbc02009-04-20 17:24:49 +0200692 rv = -EINVAL;
Bjørn Mork0dffb482012-03-06 17:29:20 +0100693 if (!usb_endpoint_is_int_in(ep))
Oliver Neukum052fbc02009-04-20 17:24:49 +0200694 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200695
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700696 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
Oliver Neukumafba9372008-05-13 17:01:25 +0200697
698 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
699 if (!desc->orq)
700 goto err;
701 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
702 if (!desc->irq)
703 goto err;
704
705 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
706 if (!desc->validity)
707 goto err;
708
709 desc->response = usb_alloc_urb(0, GFP_KERNEL);
710 if (!desc->response)
711 goto err;
712
713 desc->command = usb_alloc_urb(0, GFP_KERNEL);
714 if (!desc->command)
715 goto err;
716
717 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
718 if (!desc->ubuf)
719 goto err;
720
Bjørn Mork8457d992012-01-16 15:12:00 +0100721 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200722 if (!desc->sbuf)
723 goto err;
724
Bjørn Mork8457d992012-01-16 15:12:00 +0100725 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200726 if (!desc->inbuf)
Bjørn Mork8457d992012-01-16 15:12:00 +0100727 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200728
729 usb_fill_int_urb(
730 desc->validity,
731 interface_to_usbdev(intf),
732 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
733 desc->sbuf,
734 desc->wMaxPacketSize,
735 wdm_int_callback,
736 desc,
737 ep->bInterval
738 );
Oliver Neukumafba9372008-05-13 17:01:25 +0200739
Bjørn Mork19b85b32012-01-16 15:11:58 +0100740 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
741 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
742 desc->irq->wValue = 0;
743 desc->irq->wIndex = desc->inum;
744 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
745
746 usb_fill_control_urb(
747 desc->response,
Bjørn Mork8143a892012-01-16 15:12:01 +0100748 interface_to_usbdev(intf),
Bjørn Mork19b85b32012-01-16 15:11:58 +0100749 /* using common endpoint 0 */
750 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
751 (unsigned char *)desc->irq,
752 desc->inbuf,
753 desc->wMaxCommand,
754 wdm_in_callback,
755 desc
756 );
Bjørn Mork19b85b32012-01-16 15:11:58 +0100757
Bjørn Mork3cc36152012-03-06 17:29:22 +0100758 desc->manage_power = manage_power;
759
Bjørn Morkb0c13862012-03-06 17:29:21 +0100760 spin_lock(&wdm_device_list_lock);
761 list_add(&desc->device_list, &wdm_device_list);
762 spin_unlock(&wdm_device_list_lock);
763
Oliver Neukumafba9372008-05-13 17:01:25 +0200764 rv = usb_register_dev(intf, &wdm_class);
Oliver Neukumafba9372008-05-13 17:01:25 +0200765 if (rv < 0)
Bjørn Morkb0c13862012-03-06 17:29:21 +0100766 goto err;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200767 else
Bjørn Mork820c6292012-01-20 04:17:25 +0100768 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
Oliver Neukumafba9372008-05-13 17:01:25 +0200769out:
770 return rv;
Oliver Neukumafba9372008-05-13 17:01:25 +0200771err:
Bjørn Mork6286d852012-05-09 13:53:23 +0200772 spin_lock(&wdm_device_list_lock);
773 list_del(&desc->device_list);
774 spin_unlock(&wdm_device_list_lock);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100775 cleanup(desc);
776 return rv;
777}
778
Bjørn Mork3cc36152012-03-06 17:29:22 +0100779static int wdm_manage_power(struct usb_interface *intf, int on)
780{
781 /* need autopm_get/put here to ensure the usbcore sees the new value */
782 int rv = usb_autopm_get_interface(intf);
783 if (rv < 0)
784 goto err;
785
786 intf->needs_remote_wakeup = on;
787 usb_autopm_put_interface(intf);
788err:
789 return rv;
790}
791
Bjørn Mork0dffb482012-03-06 17:29:20 +0100792static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
793{
794 int rv = -EINVAL;
795 struct usb_host_interface *iface;
796 struct usb_endpoint_descriptor *ep;
797 struct usb_cdc_dmm_desc *dmhd;
798 u8 *buffer = intf->altsetting->extra;
799 int buflen = intf->altsetting->extralen;
800 u16 maxcom = WDM_DEFAULT_BUFSIZE;
801
802 if (!buffer)
803 goto err;
804 while (buflen > 2) {
805 if (buffer[1] != USB_DT_CS_INTERFACE) {
806 dev_err(&intf->dev, "skipping garbage\n");
807 goto next_desc;
808 }
809
810 switch (buffer[2]) {
811 case USB_CDC_HEADER_TYPE:
812 break;
813 case USB_CDC_DMM_TYPE:
814 dmhd = (struct usb_cdc_dmm_desc *)buffer;
815 maxcom = le16_to_cpu(dmhd->wMaxCommand);
816 dev_dbg(&intf->dev,
817 "Finding maximum buffer length: %d", maxcom);
818 break;
819 default:
820 dev_err(&intf->dev,
821 "Ignoring extra header, type %d, length %d\n",
822 buffer[2], buffer[0]);
823 break;
824 }
825next_desc:
826 buflen -= buffer[0];
827 buffer += buffer[0];
828 }
829
830 iface = intf->cur_altsetting;
831 if (iface->desc.bNumEndpoints != 1)
832 goto err;
833 ep = &iface->endpoint[0].desc;
834
Bjørn Mork3cc36152012-03-06 17:29:22 +0100835 rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100836
837err:
Oliver Neukumafba9372008-05-13 17:01:25 +0200838 return rv;
839}
840
Bjørn Mork3cc36152012-03-06 17:29:22 +0100841/**
842 * usb_cdc_wdm_register - register a WDM subdriver
843 * @intf: usb interface the subdriver will associate with
844 * @ep: interrupt endpoint to monitor for notifications
845 * @bufsize: maximum message size to support for read/write
846 *
847 * Create WDM usb class character device and associate it with intf
848 * without binding, allowing another driver to manage the interface.
849 *
850 * The subdriver will manage the given interrupt endpoint exclusively
851 * and will issue control requests referring to the given intf. It
852 * will otherwise avoid interferring, and in particular not do
853 * usb_set_intfdata/usb_get_intfdata on intf.
854 *
855 * The return value is a pointer to the subdriver's struct usb_driver.
856 * The registering driver is responsible for calling this subdriver's
857 * disconnect, suspend, resume, pre_reset and post_reset methods from
858 * its own.
859 */
860struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
861 struct usb_endpoint_descriptor *ep,
862 int bufsize,
863 int (*manage_power)(struct usb_interface *, int))
864{
865 int rv = -EINVAL;
866
867 rv = wdm_create(intf, ep, bufsize, manage_power);
868 if (rv < 0)
869 goto err;
870
871 return &wdm_driver;
872err:
873 return ERR_PTR(rv);
874}
875EXPORT_SYMBOL(usb_cdc_wdm_register);
876
Oliver Neukumafba9372008-05-13 17:01:25 +0200877static void wdm_disconnect(struct usb_interface *intf)
878{
879 struct wdm_device *desc;
880 unsigned long flags;
881
882 usb_deregister_dev(intf, &wdm_class);
Bjørn Morkb0c13862012-03-06 17:29:21 +0100883 desc = wdm_find_device(intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200884 mutex_lock(&wdm_mutex);
Oliver Neukumafba9372008-05-13 17:01:25 +0200885
886 /* the spinlock makes sure no new urbs are generated in the callbacks */
887 spin_lock_irqsave(&desc->iuspin, flags);
888 set_bit(WDM_DISCONNECTING, &desc->flags);
889 set_bit(WDM_READ, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200890 /* to terminate pending flushes */
Oliver Neukumafba9372008-05-13 17:01:25 +0200891 clear_bit(WDM_IN_USE, &desc->flags);
892 spin_unlock_irqrestore(&desc->iuspin, flags);
Bjørn Mork62aaf242012-01-16 15:11:57 +0100893 wake_up_all(&desc->wait);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100894 mutex_lock(&desc->rlock);
895 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200896 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100897 cancel_work_sync(&desc->rxwork);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100898 mutex_unlock(&desc->wlock);
899 mutex_unlock(&desc->rlock);
Bjørn Mork6286d852012-05-09 13:53:23 +0200900
901 /* the desc->intf pointer used as list key is now invalid */
902 spin_lock(&wdm_device_list_lock);
903 list_del(&desc->device_list);
904 spin_unlock(&wdm_device_list_lock);
905
Oliver Neukumafba9372008-05-13 17:01:25 +0200906 if (!desc->count)
907 cleanup(desc);
Bjørn Mork880bca32012-04-30 09:26:11 +0200908 else
909 dev_dbg(&intf->dev, "%s: %d open files - postponing cleanup\n", __func__, desc->count);
Oliver Neukumafba9372008-05-13 17:01:25 +0200910 mutex_unlock(&wdm_mutex);
911}
912
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100913#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200914static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
915{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100916 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200917 int rv = 0;
918
919 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
920
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100921 /* if this is an autosuspend the caller does the locking */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100922 if (!PMSG_IS_AUTO(message)) {
923 mutex_lock(&desc->rlock);
924 mutex_lock(&desc->wlock);
925 }
Oliver Neukum62e66852010-02-27 20:56:22 +0100926 spin_lock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100927
Alan Stern5b1b0b82011-08-19 23:49:48 +0200928 if (PMSG_IS_AUTO(message) &&
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100929 (test_bit(WDM_IN_USE, &desc->flags)
930 || test_bit(WDM_RESPONDING, &desc->flags))) {
Oliver Neukum62e66852010-02-27 20:56:22 +0100931 spin_unlock_irq(&desc->iuspin);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200932 rv = -EBUSY;
933 } else {
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100934
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100935 set_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100936 spin_unlock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100937 /* callback submits work - order is essential */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200938 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100939 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200940 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100941 if (!PMSG_IS_AUTO(message)) {
942 mutex_unlock(&desc->wlock);
943 mutex_unlock(&desc->rlock);
944 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200945
946 return rv;
947}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100948#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200949
950static int recover_from_urb_loss(struct wdm_device *desc)
951{
952 int rv = 0;
953
954 if (desc->count) {
955 rv = usb_submit_urb(desc->validity, GFP_NOIO);
956 if (rv < 0)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700957 dev_err(&desc->intf->dev,
958 "Error resume submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200959 }
960 return rv;
961}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100962
963#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200964static int wdm_resume(struct usb_interface *intf)
965{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100966 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200967 int rv;
968
969 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
Oliver Neukum338124c2010-02-27 20:57:12 +0100970
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100971 clear_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100972 rv = recover_from_urb_loss(desc);
Oliver Neukum338124c2010-02-27 20:57:12 +0100973
Oliver Neukum17d80d52008-06-24 15:56:10 +0200974 return rv;
975}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100976#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200977
978static int wdm_pre_reset(struct usb_interface *intf)
979{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100980 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200981
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200982 /*
983 * we notify everybody using poll of
984 * an exceptional situation
985 * must be done before recovery lest a spontaneous
986 * message from the device is lost
987 */
988 spin_lock_irq(&desc->iuspin);
Bjørn Mork88044202012-02-10 09:44:08 +0100989 set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
990 set_bit(WDM_READ, &desc->flags); /* unblock read */
991 clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200992 desc->rerr = -EINTR;
993 spin_unlock_irq(&desc->iuspin);
994 wake_up_all(&desc->wait);
Bjørn Mork88044202012-02-10 09:44:08 +0100995 mutex_lock(&desc->rlock);
996 mutex_lock(&desc->wlock);
997 kill_urbs(desc);
998 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200999 return 0;
1000}
1001
1002static int wdm_post_reset(struct usb_interface *intf)
1003{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001004 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001005 int rv;
1006
Bjørn Mork88044202012-02-10 09:44:08 +01001007 clear_bit(WDM_RESETTING, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001008 rv = recover_from_urb_loss(desc);
Bjørn Morke8537bd2012-01-16 12:41:48 +01001009 mutex_unlock(&desc->wlock);
1010 mutex_unlock(&desc->rlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001011 return 0;
1012}
1013
Oliver Neukumafba9372008-05-13 17:01:25 +02001014static struct usb_driver wdm_driver = {
1015 .name = "cdc_wdm",
1016 .probe = wdm_probe,
1017 .disconnect = wdm_disconnect,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001018#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +02001019 .suspend = wdm_suspend,
1020 .resume = wdm_resume,
1021 .reset_resume = wdm_resume,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001022#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +02001023 .pre_reset = wdm_pre_reset,
1024 .post_reset = wdm_post_reset,
Oliver Neukumafba9372008-05-13 17:01:25 +02001025 .id_table = wdm_ids,
Oliver Neukum17d80d52008-06-24 15:56:10 +02001026 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07001027 .disable_hub_initiated_lpm = 1,
Oliver Neukumafba9372008-05-13 17:01:25 +02001028};
1029
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001030module_usb_driver(wdm_driver);
Oliver Neukumafba9372008-05-13 17:01:25 +02001031
1032MODULE_AUTHOR(DRIVER_AUTHOR);
Oliver Neukum87d65e52008-06-19 14:20:18 +02001033MODULE_DESCRIPTION(DRIVER_DESC);
Oliver Neukumafba9372008-05-13 17:01:25 +02001034MODULE_LICENSE("GPL");