blob: 01d247e88081196b83f6b580ef09f78a56041319 [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
Bjørn Morkfec67b42012-01-25 13:03:29 +010035#define HUAWEI_VENDOR_ID 0x12D1
36
Németh Márton6ef48522010-01-10 15:33:45 +010037static const struct usb_device_id wdm_ids[] = {
Oliver Neukumafba9372008-05-13 17:01:25 +020038 {
39 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
40 USB_DEVICE_ID_MATCH_INT_SUBCLASS,
41 .bInterfaceClass = USB_CLASS_COMM,
42 .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM
43 },
Bjørn Morkfec67b42012-01-25 13:03:29 +010044 {
45 /*
46 * Huawei E392, E398 and possibly other Qualcomm based modems
47 * embed the Qualcomm QMI protocol inside CDC on CDC ECM like
48 * control interfaces. Userspace access to this is required
49 * to configure the accompanying data interface
50 */
51 .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
52 USB_DEVICE_ID_MATCH_INT_INFO,
53 .idVendor = HUAWEI_VENDOR_ID,
54 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
55 .bInterfaceSubClass = 1,
56 .bInterfaceProtocol = 9, /* NOTE: CDC ECM control interface! */
57 },
Bjørn Mork9b814052012-05-19 19:19:48 +020058 {
59 /* Vodafone/Huawei K5005 (12d1:14c8) and similar modems */
60 .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
61 USB_DEVICE_ID_MATCH_INT_INFO,
62 .idVendor = HUAWEI_VENDOR_ID,
63 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
64 .bInterfaceSubClass = 1,
65 .bInterfaceProtocol = 57, /* NOTE: CDC ECM control interface! */
66 },
Oliver Neukumafba9372008-05-13 17:01:25 +020067 { }
68};
69
Oliver Neukumaa5380b2008-10-13 14:05:20 +020070MODULE_DEVICE_TABLE (usb, wdm_ids);
71
Oliver Neukumafba9372008-05-13 17:01:25 +020072#define WDM_MINOR_BASE 176
73
74
75#define WDM_IN_USE 1
76#define WDM_DISCONNECTING 2
77#define WDM_RESULT 3
78#define WDM_READ 4
79#define WDM_INT_STALL 5
80#define WDM_POLL_RUNNING 6
Oliver Neukum922a5ea2010-02-27 20:54:59 +010081#define WDM_RESPONDING 7
Oliver Neukumbeb1d352010-02-27 20:55:52 +010082#define WDM_SUSPENDING 8
Bjørn Mork88044202012-02-10 09:44:08 +010083#define WDM_RESETTING 9
Oliver Neukumafba9372008-05-13 17:01:25 +020084
85#define WDM_MAX 16
86
Bjørn Mork7e3054a2012-01-20 01:49:57 +010087/* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
88#define WDM_DEFAULT_BUFSIZE 256
Oliver Neukumafba9372008-05-13 17:01:25 +020089
90static DEFINE_MUTEX(wdm_mutex);
Bjørn Morkb0c13862012-03-06 17:29:21 +010091static DEFINE_SPINLOCK(wdm_device_list_lock);
92static LIST_HEAD(wdm_device_list);
Oliver Neukumafba9372008-05-13 17:01:25 +020093
94/* --- method tables --- */
95
96struct wdm_device {
97 u8 *inbuf; /* buffer for response */
98 u8 *outbuf; /* buffer for command */
99 u8 *sbuf; /* buffer for status */
100 u8 *ubuf; /* buffer for copy to user space */
101
102 struct urb *command;
103 struct urb *response;
104 struct urb *validity;
105 struct usb_interface *intf;
106 struct usb_ctrlrequest *orq;
107 struct usb_ctrlrequest *irq;
108 spinlock_t iuspin;
109
110 unsigned long flags;
111 u16 bufsize;
112 u16 wMaxCommand;
113 u16 wMaxPacketSize;
Oliver Neukumafba9372008-05-13 17:01:25 +0200114 __le16 inum;
115 int reslength;
116 int length;
117 int read;
118 int count;
119 dma_addr_t shandle;
120 dma_addr_t ihandle;
Bjørn Morke8537bd2012-01-16 12:41:48 +0100121 struct mutex wlock;
122 struct mutex rlock;
Oliver Neukumafba9372008-05-13 17:01:25 +0200123 wait_queue_head_t wait;
124 struct work_struct rxwork;
125 int werr;
126 int rerr;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100127
128 struct list_head device_list;
Bjørn Mork3cc36152012-03-06 17:29:22 +0100129 int (*manage_power)(struct usb_interface *, int);
Oliver Neukumafba9372008-05-13 17:01:25 +0200130};
131
132static struct usb_driver wdm_driver;
133
Bjørn Morkb0c13862012-03-06 17:29:21 +0100134/* return intfdata if we own the interface, else look up intf in the list */
135static struct wdm_device *wdm_find_device(struct usb_interface *intf)
136{
137 struct wdm_device *desc = NULL;
138
139 spin_lock(&wdm_device_list_lock);
140 list_for_each_entry(desc, &wdm_device_list, device_list)
141 if (desc->intf == intf)
142 break;
143 spin_unlock(&wdm_device_list_lock);
144
145 return desc;
146}
147
148static struct wdm_device *wdm_find_device_by_minor(int minor)
149{
150 struct wdm_device *desc = NULL;
151
152 spin_lock(&wdm_device_list_lock);
153 list_for_each_entry(desc, &wdm_device_list, device_list)
154 if (desc->intf->minor == minor)
155 break;
156 spin_unlock(&wdm_device_list_lock);
157
158 return desc;
159}
160
Oliver Neukumafba9372008-05-13 17:01:25 +0200161/* --- callbacks --- */
162static void wdm_out_callback(struct urb *urb)
163{
164 struct wdm_device *desc;
165 desc = urb->context;
166 spin_lock(&desc->iuspin);
167 desc->werr = urb->status;
168 spin_unlock(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200169 kfree(desc->outbuf);
Oliver Neukum5c228372012-04-26 21:59:10 +0200170 desc->outbuf = NULL;
171 clear_bit(WDM_IN_USE, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200172 wake_up(&desc->wait);
173}
174
175static void wdm_in_callback(struct urb *urb)
176{
177 struct wdm_device *desc = urb->context;
178 int status = urb->status;
179
180 spin_lock(&desc->iuspin);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100181 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200182
183 if (status) {
184 switch (status) {
185 case -ENOENT:
186 dev_dbg(&desc->intf->dev,
187 "nonzero urb status received: -ENOENT");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100188 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200189 case -ECONNRESET:
190 dev_dbg(&desc->intf->dev,
191 "nonzero urb status received: -ECONNRESET");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100192 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200193 case -ESHUTDOWN:
194 dev_dbg(&desc->intf->dev,
195 "nonzero urb status received: -ESHUTDOWN");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100196 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200197 case -EPIPE:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700198 dev_err(&desc->intf->dev,
199 "nonzero urb status received: -EPIPE\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200200 break;
201 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700202 dev_err(&desc->intf->dev,
203 "Unexpected error %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200204 break;
205 }
206 }
207
208 desc->rerr = status;
209 desc->reslength = urb->actual_length;
210 memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength);
211 desc->length += desc->reslength;
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100212skip_error:
Oliver Neukumafba9372008-05-13 17:01:25 +0200213 wake_up(&desc->wait);
214
215 set_bit(WDM_READ, &desc->flags);
216 spin_unlock(&desc->iuspin);
217}
218
219static void wdm_int_callback(struct urb *urb)
220{
221 int rv = 0;
222 int status = urb->status;
223 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200224 struct usb_cdc_notification *dr;
225
226 desc = urb->context;
Oliver Neukumafba9372008-05-13 17:01:25 +0200227 dr = (struct usb_cdc_notification *)desc->sbuf;
228
229 if (status) {
230 switch (status) {
231 case -ESHUTDOWN:
232 case -ENOENT:
233 case -ECONNRESET:
234 return; /* unplug */
235 case -EPIPE:
236 set_bit(WDM_INT_STALL, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700237 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200238 goto sw; /* halt is cleared in work */
239 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700240 dev_err(&desc->intf->dev,
241 "nonzero urb status received: %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200242 break;
243 }
244 }
245
246 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700247 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
248 urb->actual_length);
Oliver Neukumafba9372008-05-13 17:01:25 +0200249 goto exit;
250 }
251
252 switch (dr->bNotificationType) {
253 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
254 dev_dbg(&desc->intf->dev,
255 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
256 dr->wIndex, dr->wLength);
257 break;
258
259 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
260
261 dev_dbg(&desc->intf->dev,
262 "NOTIFY_NETWORK_CONNECTION %s network",
263 dr->wValue ? "connected to" : "disconnected from");
264 goto exit;
265 default:
266 clear_bit(WDM_POLL_RUNNING, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700267 dev_err(&desc->intf->dev,
268 "unknown notification %d received: index %d len %d\n",
Oliver Neukumafba9372008-05-13 17:01:25 +0200269 dr->bNotificationType, dr->wIndex, dr->wLength);
270 goto exit;
271 }
272
Oliver Neukumafba9372008-05-13 17:01:25 +0200273 spin_lock(&desc->iuspin);
274 clear_bit(WDM_READ, &desc->flags);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100275 set_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100276 if (!test_bit(WDM_DISCONNECTING, &desc->flags)
277 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200278 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
279 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
280 __func__, rv);
281 }
282 spin_unlock(&desc->iuspin);
283 if (rv < 0) {
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100284 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200285 if (rv == -EPERM)
286 return;
287 if (rv == -ENOMEM) {
288sw:
289 rv = schedule_work(&desc->rxwork);
290 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700291 dev_err(&desc->intf->dev,
292 "Cannot schedule work\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200293 }
294 }
295exit:
296 rv = usb_submit_urb(urb, GFP_ATOMIC);
297 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700298 dev_err(&desc->intf->dev,
299 "%s - usb_submit_urb failed with result %d\n",
300 __func__, rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200301
302}
303
304static void kill_urbs(struct wdm_device *desc)
305{
Oliver Neukum17d80d52008-06-24 15:56:10 +0200306 /* the order here is essential */
Oliver Neukumafba9372008-05-13 17:01:25 +0200307 usb_kill_urb(desc->command);
308 usb_kill_urb(desc->validity);
309 usb_kill_urb(desc->response);
310}
311
312static void free_urbs(struct wdm_device *desc)
313{
314 usb_free_urb(desc->validity);
315 usb_free_urb(desc->response);
316 usb_free_urb(desc->command);
317}
318
319static void cleanup(struct wdm_device *desc)
320{
Bjørn Mork8457d992012-01-16 15:12:00 +0100321 kfree(desc->sbuf);
322 kfree(desc->inbuf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200323 kfree(desc->orq);
324 kfree(desc->irq);
325 kfree(desc->ubuf);
326 free_urbs(desc);
327 kfree(desc);
328}
329
330static ssize_t wdm_write
331(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
332{
333 u8 *buf;
334 int rv = -EMSGSIZE, r, we;
335 struct wdm_device *desc = file->private_data;
336 struct usb_ctrlrequest *req;
337
338 if (count > desc->wMaxCommand)
339 count = desc->wMaxCommand;
340
341 spin_lock_irq(&desc->iuspin);
342 we = desc->werr;
343 desc->werr = 0;
344 spin_unlock_irq(&desc->iuspin);
345 if (we < 0)
346 return -EIO;
347
Oliver Neukum5c228372012-04-26 21:59:10 +0200348 buf = kmalloc(count, GFP_KERNEL);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100349 if (!buf) {
350 rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200351 goto outnl;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100352 }
353
354 r = copy_from_user(buf, buffer, count);
355 if (r > 0) {
356 kfree(buf);
357 rv = -EFAULT;
358 goto outnl;
359 }
360
361 /* concurrent writes and disconnect */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100362 r = mutex_lock_interruptible(&desc->wlock);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100363 rv = -ERESTARTSYS;
364 if (r) {
365 kfree(buf);
366 goto outnl;
367 }
368
369 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
370 kfree(buf);
371 rv = -ENODEV;
372 goto outnp;
373 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200374
Oliver Neukum17d80d52008-06-24 15:56:10 +0200375 r = usb_autopm_get_interface(desc->intf);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100376 if (r < 0) {
377 kfree(buf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200378 goto outnp;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100379 }
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200380
David Sterba0cdfb812010-12-27 18:49:58 +0100381 if (!(file->f_flags & O_NONBLOCK))
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200382 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
383 &desc->flags));
384 else
385 if (test_bit(WDM_IN_USE, &desc->flags))
386 r = -EAGAIN;
Bjørn Mork88044202012-02-10 09:44:08 +0100387
388 if (test_bit(WDM_RESETTING, &desc->flags))
389 r = -EIO;
390
Oliver Neukum860e41a2010-02-27 20:54:24 +0100391 if (r < 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200392 kfree(buf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200393 goto out;
394 }
395
396 req = desc->orq;
397 usb_fill_control_urb(
398 desc->command,
399 interface_to_usbdev(desc->intf),
400 /* using common endpoint 0 */
401 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
402 (unsigned char *)req,
403 buf,
404 count,
405 wdm_out_callback,
406 desc
407 );
408
409 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
410 USB_RECIP_INTERFACE);
411 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
412 req->wValue = 0;
413 req->wIndex = desc->inum;
414 req->wLength = cpu_to_le16(count);
415 set_bit(WDM_IN_USE, &desc->flags);
Oliver Neukum5c228372012-04-26 21:59:10 +0200416 desc->outbuf = buf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200417
418 rv = usb_submit_urb(desc->command, GFP_KERNEL);
419 if (rv < 0) {
420 kfree(buf);
Oliver Neukum5c228372012-04-26 21:59:10 +0200421 desc->outbuf = NULL;
Oliver Neukumafba9372008-05-13 17:01:25 +0200422 clear_bit(WDM_IN_USE, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700423 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200424 } else {
425 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
426 req->wIndex);
427 }
428out:
Oliver Neukum17d80d52008-06-24 15:56:10 +0200429 usb_autopm_put_interface(desc->intf);
430outnp:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100431 mutex_unlock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200432outnl:
433 return rv < 0 ? rv : count;
434}
435
436static ssize_t wdm_read
437(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
438{
Ben Hutchings711c68b2012-02-12 06:00:41 +0000439 int rv, cntr;
Oliver Neukumafba9372008-05-13 17:01:25 +0200440 int i = 0;
441 struct wdm_device *desc = file->private_data;
442
443
Bjørn Morke8537bd2012-01-16 12:41:48 +0100444 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
Oliver Neukumafba9372008-05-13 17:01:25 +0200445 if (rv < 0)
446 return -ERESTARTSYS;
447
Ben Hutchings711c68b2012-02-12 06:00:41 +0000448 cntr = ACCESS_ONCE(desc->length);
449 if (cntr == 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200450 desc->read = 0;
451retry:
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200452 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
453 rv = -ENODEV;
454 goto err;
455 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200456 i++;
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200457 if (file->f_flags & O_NONBLOCK) {
458 if (!test_bit(WDM_READ, &desc->flags)) {
459 rv = cntr ? cntr : -EAGAIN;
460 goto err;
461 }
462 rv = 0;
463 } else {
464 rv = wait_event_interruptible(desc->wait,
465 test_bit(WDM_READ, &desc->flags));
466 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200467
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200468 /* may have happened while we slept */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200469 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
470 rv = -ENODEV;
471 goto err;
472 }
Bjørn Mork88044202012-02-10 09:44:08 +0100473 if (test_bit(WDM_RESETTING, &desc->flags)) {
474 rv = -EIO;
475 goto err;
476 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200477 usb_mark_last_busy(interface_to_usbdev(desc->intf));
Oliver Neukumafba9372008-05-13 17:01:25 +0200478 if (rv < 0) {
479 rv = -ERESTARTSYS;
480 goto err;
481 }
482
483 spin_lock_irq(&desc->iuspin);
484
485 if (desc->rerr) { /* read completed, error happened */
Oliver Neukumafba9372008-05-13 17:01:25 +0200486 desc->rerr = 0;
487 spin_unlock_irq(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200488 rv = -EIO;
489 goto err;
490 }
491 /*
492 * recheck whether we've lost the race
493 * against the completion handler
494 */
495 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
496 spin_unlock_irq(&desc->iuspin);
497 goto retry;
498 }
499 if (!desc->reslength) { /* zero length read */
Bjørn Morkb80f6db2012-07-02 10:33:14 +0200500 dev_dbg(&desc->intf->dev, "%s: zero length - clearing WDM_READ\n", __func__);
501 clear_bit(WDM_READ, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200502 spin_unlock_irq(&desc->iuspin);
503 goto retry;
504 }
Ben Hutchings711c68b2012-02-12 06:00:41 +0000505 cntr = desc->length;
Oliver Neukumafba9372008-05-13 17:01:25 +0200506 spin_unlock_irq(&desc->iuspin);
507 }
508
Ben Hutchings711c68b2012-02-12 06:00:41 +0000509 if (cntr > count)
510 cntr = count;
Oliver Neukumafba9372008-05-13 17:01:25 +0200511 rv = copy_to_user(buffer, desc->ubuf, cntr);
512 if (rv > 0) {
513 rv = -EFAULT;
514 goto err;
515 }
516
Ben Hutchings711c68b2012-02-12 06:00:41 +0000517 spin_lock_irq(&desc->iuspin);
518
Oliver Neukumafba9372008-05-13 17:01:25 +0200519 for (i = 0; i < desc->length - cntr; i++)
520 desc->ubuf[i] = desc->ubuf[i + cntr];
521
522 desc->length -= cntr;
Oliver Neukum87d65e52008-06-19 14:20:18 +0200523 /* in case we had outstanding data */
524 if (!desc->length)
525 clear_bit(WDM_READ, &desc->flags);
Ben Hutchings711c68b2012-02-12 06:00:41 +0000526
527 spin_unlock_irq(&desc->iuspin);
528
Oliver Neukumafba9372008-05-13 17:01:25 +0200529 rv = cntr;
530
531err:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100532 mutex_unlock(&desc->rlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200533 return rv;
534}
535
536static int wdm_flush(struct file *file, fl_owner_t id)
537{
538 struct wdm_device *desc = file->private_data;
539
540 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
Bjørn Mork577bd3e2012-05-09 13:53:22 +0200541
542 /* cannot dereference desc->intf if WDM_DISCONNECTING */
543 if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags))
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700544 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
545 desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200546
Oliver Neukumeae68612012-04-27 14:23:54 +0200547 return usb_translate_errors(desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200548}
549
550static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
551{
552 struct wdm_device *desc = file->private_data;
553 unsigned long flags;
554 unsigned int mask = 0;
555
556 spin_lock_irqsave(&desc->iuspin, flags);
557 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
Bjørn Morkd0765ea2012-05-09 13:53:21 +0200558 mask = POLLHUP | POLLERR;
Oliver Neukumafba9372008-05-13 17:01:25 +0200559 spin_unlock_irqrestore(&desc->iuspin, flags);
560 goto desc_out;
561 }
562 if (test_bit(WDM_READ, &desc->flags))
563 mask = POLLIN | POLLRDNORM;
564 if (desc->rerr || desc->werr)
565 mask |= POLLERR;
566 if (!test_bit(WDM_IN_USE, &desc->flags))
567 mask |= POLLOUT | POLLWRNORM;
568 spin_unlock_irqrestore(&desc->iuspin, flags);
569
570 poll_wait(file, &desc->wait, wait);
571
572desc_out:
573 return mask;
574}
575
576static int wdm_open(struct inode *inode, struct file *file)
577{
578 int minor = iminor(inode);
579 int rv = -ENODEV;
580 struct usb_interface *intf;
581 struct wdm_device *desc;
582
583 mutex_lock(&wdm_mutex);
Bjørn Morkb0c13862012-03-06 17:29:21 +0100584 desc = wdm_find_device_by_minor(minor);
585 if (!desc)
Oliver Neukumafba9372008-05-13 17:01:25 +0200586 goto out;
587
Bjørn Morkb0c13862012-03-06 17:29:21 +0100588 intf = desc->intf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200589 if (test_bit(WDM_DISCONNECTING, &desc->flags))
590 goto out;
Oliver Neukumafba9372008-05-13 17:01:25 +0200591 file->private_data = desc;
592
Oliver Neukum17d80d52008-06-24 15:56:10 +0200593 rv = usb_autopm_get_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200594 if (rv < 0) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700595 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200596 goto out;
597 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200598
Bjørn Morke8537bd2012-01-16 12:41:48 +0100599 /* using write lock to protect desc->count */
600 mutex_lock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200601 if (!desc->count++) {
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200602 desc->werr = 0;
603 desc->rerr = 0;
Oliver Neukum17d80d52008-06-24 15:56:10 +0200604 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
605 if (rv < 0) {
606 desc->count--;
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700607 dev_err(&desc->intf->dev,
608 "Error submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200609 }
610 } else {
611 rv = 0;
612 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100613 mutex_unlock(&desc->wlock);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100614 if (desc->count == 1)
615 desc->manage_power(intf, 1);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200616 usb_autopm_put_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200617out:
618 mutex_unlock(&wdm_mutex);
619 return rv;
620}
621
622static int wdm_release(struct inode *inode, struct file *file)
623{
624 struct wdm_device *desc = file->private_data;
625
626 mutex_lock(&wdm_mutex);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100627
628 /* using write lock to protect desc->count */
629 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200630 desc->count--;
Bjørn Morke8537bd2012-01-16 12:41:48 +0100631 mutex_unlock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200632
Oliver Neukumafba9372008-05-13 17:01:25 +0200633 if (!desc->count) {
Bjørn Mork577bd3e2012-05-09 13:53:22 +0200634 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
635 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
636 kill_urbs(desc);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100637 desc->manage_power(desc->intf, 0);
Bjørn Mork577bd3e2012-05-09 13:53:22 +0200638 } else {
639 /* must avoid dev_printk here as desc->intf is invalid */
640 pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
Oliver Neukumfccfda62012-04-27 14:36:37 +0200641 cleanup(desc);
Bjørn Mork577bd3e2012-05-09 13:53:22 +0200642 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200643 }
644 mutex_unlock(&wdm_mutex);
645 return 0;
646}
647
648static const struct file_operations wdm_fops = {
649 .owner = THIS_MODULE,
650 .read = wdm_read,
651 .write = wdm_write,
652 .open = wdm_open,
653 .flush = wdm_flush,
654 .release = wdm_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200655 .poll = wdm_poll,
656 .llseek = noop_llseek,
Oliver Neukumafba9372008-05-13 17:01:25 +0200657};
658
659static struct usb_class_driver wdm_class = {
660 .name = "cdc-wdm%d",
661 .fops = &wdm_fops,
662 .minor_base = WDM_MINOR_BASE,
663};
664
665/* --- error handling --- */
666static void wdm_rxwork(struct work_struct *work)
667{
668 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
669 unsigned long flags;
670 int rv;
671
672 spin_lock_irqsave(&desc->iuspin, flags);
673 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
674 spin_unlock_irqrestore(&desc->iuspin, flags);
675 } else {
676 spin_unlock_irqrestore(&desc->iuspin, flags);
677 rv = usb_submit_urb(desc->response, GFP_KERNEL);
678 if (rv < 0 && rv != -EPERM) {
679 spin_lock_irqsave(&desc->iuspin, flags);
680 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
681 schedule_work(&desc->rxwork);
682 spin_unlock_irqrestore(&desc->iuspin, flags);
683 }
684 }
685}
686
687/* --- hotplug --- */
688
Bjørn Mork3cc36152012-03-06 17:29:22 +0100689static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
690 u16 bufsize, int (*manage_power)(struct usb_interface *, int))
Oliver Neukumafba9372008-05-13 17:01:25 +0200691{
Bjørn Mork0dffb482012-03-06 17:29:20 +0100692 int rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200693 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200694
Oliver Neukumafba9372008-05-13 17:01:25 +0200695 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
696 if (!desc)
697 goto out;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100698 INIT_LIST_HEAD(&desc->device_list);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100699 mutex_init(&desc->rlock);
700 mutex_init(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200701 spin_lock_init(&desc->iuspin);
702 init_waitqueue_head(&desc->wait);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100703 desc->wMaxCommand = bufsize;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200704 /* this will be expanded and needed in hardware endianness */
Oliver Neukumafba9372008-05-13 17:01:25 +0200705 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
706 desc->intf = intf;
707 INIT_WORK(&desc->rxwork, wdm_rxwork);
708
Oliver Neukum052fbc02009-04-20 17:24:49 +0200709 rv = -EINVAL;
Bjørn Mork0dffb482012-03-06 17:29:20 +0100710 if (!usb_endpoint_is_int_in(ep))
Oliver Neukum052fbc02009-04-20 17:24:49 +0200711 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200712
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700713 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
Oliver Neukumafba9372008-05-13 17:01:25 +0200714
715 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
716 if (!desc->orq)
717 goto err;
718 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
719 if (!desc->irq)
720 goto err;
721
722 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
723 if (!desc->validity)
724 goto err;
725
726 desc->response = usb_alloc_urb(0, GFP_KERNEL);
727 if (!desc->response)
728 goto err;
729
730 desc->command = usb_alloc_urb(0, GFP_KERNEL);
731 if (!desc->command)
732 goto err;
733
734 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
735 if (!desc->ubuf)
736 goto err;
737
Bjørn Mork8457d992012-01-16 15:12:00 +0100738 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200739 if (!desc->sbuf)
740 goto err;
741
Bjørn Mork8457d992012-01-16 15:12:00 +0100742 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200743 if (!desc->inbuf)
Bjørn Mork8457d992012-01-16 15:12:00 +0100744 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200745
746 usb_fill_int_urb(
747 desc->validity,
748 interface_to_usbdev(intf),
749 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
750 desc->sbuf,
751 desc->wMaxPacketSize,
752 wdm_int_callback,
753 desc,
754 ep->bInterval
755 );
Oliver Neukumafba9372008-05-13 17:01:25 +0200756
Bjørn Mork19b85b32012-01-16 15:11:58 +0100757 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
758 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
759 desc->irq->wValue = 0;
760 desc->irq->wIndex = desc->inum;
761 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
762
763 usb_fill_control_urb(
764 desc->response,
Bjørn Mork8143a892012-01-16 15:12:01 +0100765 interface_to_usbdev(intf),
Bjørn Mork19b85b32012-01-16 15:11:58 +0100766 /* using common endpoint 0 */
767 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
768 (unsigned char *)desc->irq,
769 desc->inbuf,
770 desc->wMaxCommand,
771 wdm_in_callback,
772 desc
773 );
Bjørn Mork19b85b32012-01-16 15:11:58 +0100774
Bjørn Mork3cc36152012-03-06 17:29:22 +0100775 desc->manage_power = manage_power;
776
Bjørn Morkb0c13862012-03-06 17:29:21 +0100777 spin_lock(&wdm_device_list_lock);
778 list_add(&desc->device_list, &wdm_device_list);
779 spin_unlock(&wdm_device_list_lock);
780
Oliver Neukumafba9372008-05-13 17:01:25 +0200781 rv = usb_register_dev(intf, &wdm_class);
Oliver Neukumafba9372008-05-13 17:01:25 +0200782 if (rv < 0)
Bjørn Morkb0c13862012-03-06 17:29:21 +0100783 goto err;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200784 else
Bjørn Mork820c6292012-01-20 04:17:25 +0100785 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
Oliver Neukumafba9372008-05-13 17:01:25 +0200786out:
787 return rv;
Oliver Neukumafba9372008-05-13 17:01:25 +0200788err:
Bjørn Mork9ed2cb72012-05-09 13:53:23 +0200789 spin_lock(&wdm_device_list_lock);
790 list_del(&desc->device_list);
791 spin_unlock(&wdm_device_list_lock);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100792 cleanup(desc);
793 return rv;
794}
795
Bjørn Mork3cc36152012-03-06 17:29:22 +0100796static int wdm_manage_power(struct usb_interface *intf, int on)
797{
798 /* need autopm_get/put here to ensure the usbcore sees the new value */
799 int rv = usb_autopm_get_interface(intf);
800 if (rv < 0)
801 goto err;
802
803 intf->needs_remote_wakeup = on;
804 usb_autopm_put_interface(intf);
805err:
806 return rv;
807}
808
Bjørn Mork0dffb482012-03-06 17:29:20 +0100809static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
810{
811 int rv = -EINVAL;
812 struct usb_host_interface *iface;
813 struct usb_endpoint_descriptor *ep;
814 struct usb_cdc_dmm_desc *dmhd;
815 u8 *buffer = intf->altsetting->extra;
816 int buflen = intf->altsetting->extralen;
817 u16 maxcom = WDM_DEFAULT_BUFSIZE;
818
819 if (!buffer)
820 goto err;
821 while (buflen > 2) {
822 if (buffer[1] != USB_DT_CS_INTERFACE) {
823 dev_err(&intf->dev, "skipping garbage\n");
824 goto next_desc;
825 }
826
827 switch (buffer[2]) {
828 case USB_CDC_HEADER_TYPE:
829 break;
830 case USB_CDC_DMM_TYPE:
831 dmhd = (struct usb_cdc_dmm_desc *)buffer;
832 maxcom = le16_to_cpu(dmhd->wMaxCommand);
833 dev_dbg(&intf->dev,
834 "Finding maximum buffer length: %d", maxcom);
835 break;
836 default:
837 dev_err(&intf->dev,
838 "Ignoring extra header, type %d, length %d\n",
839 buffer[2], buffer[0]);
840 break;
841 }
842next_desc:
843 buflen -= buffer[0];
844 buffer += buffer[0];
845 }
846
847 iface = intf->cur_altsetting;
848 if (iface->desc.bNumEndpoints != 1)
849 goto err;
850 ep = &iface->endpoint[0].desc;
851
Bjørn Mork3cc36152012-03-06 17:29:22 +0100852 rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100853
854err:
Oliver Neukumafba9372008-05-13 17:01:25 +0200855 return rv;
856}
857
Bjørn Mork3cc36152012-03-06 17:29:22 +0100858/**
859 * usb_cdc_wdm_register - register a WDM subdriver
860 * @intf: usb interface the subdriver will associate with
861 * @ep: interrupt endpoint to monitor for notifications
862 * @bufsize: maximum message size to support for read/write
863 *
864 * Create WDM usb class character device and associate it with intf
865 * without binding, allowing another driver to manage the interface.
866 *
867 * The subdriver will manage the given interrupt endpoint exclusively
868 * and will issue control requests referring to the given intf. It
869 * will otherwise avoid interferring, and in particular not do
870 * usb_set_intfdata/usb_get_intfdata on intf.
871 *
872 * The return value is a pointer to the subdriver's struct usb_driver.
873 * The registering driver is responsible for calling this subdriver's
874 * disconnect, suspend, resume, pre_reset and post_reset methods from
875 * its own.
876 */
877struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
878 struct usb_endpoint_descriptor *ep,
879 int bufsize,
880 int (*manage_power)(struct usb_interface *, int))
881{
882 int rv = -EINVAL;
883
884 rv = wdm_create(intf, ep, bufsize, manage_power);
885 if (rv < 0)
886 goto err;
887
888 return &wdm_driver;
889err:
890 return ERR_PTR(rv);
891}
892EXPORT_SYMBOL(usb_cdc_wdm_register);
893
Oliver Neukumafba9372008-05-13 17:01:25 +0200894static void wdm_disconnect(struct usb_interface *intf)
895{
896 struct wdm_device *desc;
897 unsigned long flags;
898
899 usb_deregister_dev(intf, &wdm_class);
Bjørn Morkb0c13862012-03-06 17:29:21 +0100900 desc = wdm_find_device(intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200901 mutex_lock(&wdm_mutex);
Oliver Neukumafba9372008-05-13 17:01:25 +0200902
903 /* the spinlock makes sure no new urbs are generated in the callbacks */
904 spin_lock_irqsave(&desc->iuspin, flags);
905 set_bit(WDM_DISCONNECTING, &desc->flags);
906 set_bit(WDM_READ, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200907 /* to terminate pending flushes */
Oliver Neukumafba9372008-05-13 17:01:25 +0200908 clear_bit(WDM_IN_USE, &desc->flags);
909 spin_unlock_irqrestore(&desc->iuspin, flags);
Bjørn Mork62aaf242012-01-16 15:11:57 +0100910 wake_up_all(&desc->wait);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100911 mutex_lock(&desc->rlock);
912 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200913 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100914 cancel_work_sync(&desc->rxwork);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100915 mutex_unlock(&desc->wlock);
916 mutex_unlock(&desc->rlock);
Bjørn Mork9ed2cb72012-05-09 13:53:23 +0200917
918 /* the desc->intf pointer used as list key is now invalid */
919 spin_lock(&wdm_device_list_lock);
920 list_del(&desc->device_list);
921 spin_unlock(&wdm_device_list_lock);
922
Oliver Neukumafba9372008-05-13 17:01:25 +0200923 if (!desc->count)
924 cleanup(desc);
925 mutex_unlock(&wdm_mutex);
926}
927
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100928#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200929static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
930{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100931 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200932 int rv = 0;
933
934 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
935
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100936 /* if this is an autosuspend the caller does the locking */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100937 if (!PMSG_IS_AUTO(message)) {
938 mutex_lock(&desc->rlock);
939 mutex_lock(&desc->wlock);
940 }
Oliver Neukum62e66852010-02-27 20:56:22 +0100941 spin_lock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100942
Alan Stern5b1b0b82011-08-19 23:49:48 +0200943 if (PMSG_IS_AUTO(message) &&
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100944 (test_bit(WDM_IN_USE, &desc->flags)
945 || test_bit(WDM_RESPONDING, &desc->flags))) {
Oliver Neukum62e66852010-02-27 20:56:22 +0100946 spin_unlock_irq(&desc->iuspin);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200947 rv = -EBUSY;
948 } else {
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100949
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100950 set_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100951 spin_unlock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100952 /* callback submits work - order is essential */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200953 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100954 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200955 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100956 if (!PMSG_IS_AUTO(message)) {
957 mutex_unlock(&desc->wlock);
958 mutex_unlock(&desc->rlock);
959 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200960
961 return rv;
962}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100963#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200964
965static int recover_from_urb_loss(struct wdm_device *desc)
966{
967 int rv = 0;
968
969 if (desc->count) {
970 rv = usb_submit_urb(desc->validity, GFP_NOIO);
971 if (rv < 0)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700972 dev_err(&desc->intf->dev,
973 "Error resume submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200974 }
975 return rv;
976}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100977
978#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200979static int wdm_resume(struct usb_interface *intf)
980{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100981 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200982 int rv;
983
984 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
Oliver Neukum338124c2010-02-27 20:57:12 +0100985
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100986 clear_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100987 rv = recover_from_urb_loss(desc);
Oliver Neukum338124c2010-02-27 20:57:12 +0100988
Oliver Neukum17d80d52008-06-24 15:56:10 +0200989 return rv;
990}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100991#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200992
993static int wdm_pre_reset(struct usb_interface *intf)
994{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100995 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200996
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200997 /*
998 * we notify everybody using poll of
999 * an exceptional situation
1000 * must be done before recovery lest a spontaneous
1001 * message from the device is lost
1002 */
1003 spin_lock_irq(&desc->iuspin);
Bjørn Mork88044202012-02-10 09:44:08 +01001004 set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
1005 set_bit(WDM_READ, &desc->flags); /* unblock read */
1006 clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
Oliver Neukumd771d8a2011-04-29 14:12:21 +02001007 desc->rerr = -EINTR;
1008 spin_unlock_irq(&desc->iuspin);
1009 wake_up_all(&desc->wait);
Bjørn Mork88044202012-02-10 09:44:08 +01001010 mutex_lock(&desc->rlock);
1011 mutex_lock(&desc->wlock);
1012 kill_urbs(desc);
1013 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001014 return 0;
1015}
1016
1017static int wdm_post_reset(struct usb_interface *intf)
1018{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001019 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001020 int rv;
1021
Bjørn Mork88044202012-02-10 09:44:08 +01001022 clear_bit(WDM_RESETTING, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001023 rv = recover_from_urb_loss(desc);
Bjørn Morke8537bd2012-01-16 12:41:48 +01001024 mutex_unlock(&desc->wlock);
1025 mutex_unlock(&desc->rlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001026 return 0;
1027}
1028
Oliver Neukumafba9372008-05-13 17:01:25 +02001029static struct usb_driver wdm_driver = {
1030 .name = "cdc_wdm",
1031 .probe = wdm_probe,
1032 .disconnect = wdm_disconnect,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001033#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +02001034 .suspend = wdm_suspend,
1035 .resume = wdm_resume,
1036 .reset_resume = wdm_resume,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001037#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +02001038 .pre_reset = wdm_pre_reset,
1039 .post_reset = wdm_post_reset,
Oliver Neukumafba9372008-05-13 17:01:25 +02001040 .id_table = wdm_ids,
Oliver Neukum17d80d52008-06-24 15:56:10 +02001041 .supports_autosuspend = 1,
Oliver Neukumafba9372008-05-13 17:01:25 +02001042};
1043
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001044module_usb_driver(wdm_driver);
Oliver Neukumafba9372008-05-13 17:01:25 +02001045
1046MODULE_AUTHOR(DRIVER_AUTHOR);
Oliver Neukum87d65e52008-06-19 14:20:18 +02001047MODULE_DESCRIPTION(DRIVER_DESC);
Oliver Neukumafba9372008-05-13 17:01:25 +02001048MODULE_LICENSE("GPL");