blob: 4e8451ca2d6c81b4296816fa2c51ea58c95506ec [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 },
Oliver Neukumafba9372008-05-13 17:01:25 +020058 { }
59};
60
Oliver Neukumaa5380b2008-10-13 14:05:20 +020061MODULE_DEVICE_TABLE (usb, wdm_ids);
62
Oliver Neukumafba9372008-05-13 17:01:25 +020063#define WDM_MINOR_BASE 176
64
65
66#define WDM_IN_USE 1
67#define WDM_DISCONNECTING 2
68#define WDM_RESULT 3
69#define WDM_READ 4
70#define WDM_INT_STALL 5
71#define WDM_POLL_RUNNING 6
Oliver Neukum922a5ea2010-02-27 20:54:59 +010072#define WDM_RESPONDING 7
Oliver Neukumbeb1d352010-02-27 20:55:52 +010073#define WDM_SUSPENDING 8
Bjørn Mork88044202012-02-10 09:44:08 +010074#define WDM_RESETTING 9
Oliver Neukumafba9372008-05-13 17:01:25 +020075
76#define WDM_MAX 16
77
Bjørn Mork7e3054a2012-01-20 01:49:57 +010078/* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
79#define WDM_DEFAULT_BUFSIZE 256
Oliver Neukumafba9372008-05-13 17:01:25 +020080
81static DEFINE_MUTEX(wdm_mutex);
Bjørn Morkb0c13862012-03-06 17:29:21 +010082static DEFINE_SPINLOCK(wdm_device_list_lock);
83static LIST_HEAD(wdm_device_list);
Oliver Neukumafba9372008-05-13 17:01:25 +020084
85/* --- method tables --- */
86
87struct wdm_device {
88 u8 *inbuf; /* buffer for response */
89 u8 *outbuf; /* buffer for command */
90 u8 *sbuf; /* buffer for status */
91 u8 *ubuf; /* buffer for copy to user space */
92
93 struct urb *command;
94 struct urb *response;
95 struct urb *validity;
96 struct usb_interface *intf;
97 struct usb_ctrlrequest *orq;
98 struct usb_ctrlrequest *irq;
99 spinlock_t iuspin;
100
101 unsigned long flags;
102 u16 bufsize;
103 u16 wMaxCommand;
104 u16 wMaxPacketSize;
Oliver Neukumafba9372008-05-13 17:01:25 +0200105 __le16 inum;
106 int reslength;
107 int length;
108 int read;
109 int count;
110 dma_addr_t shandle;
111 dma_addr_t ihandle;
Bjørn Morke8537bd2012-01-16 12:41:48 +0100112 struct mutex wlock;
113 struct mutex rlock;
Oliver Neukumafba9372008-05-13 17:01:25 +0200114 wait_queue_head_t wait;
115 struct work_struct rxwork;
116 int werr;
117 int rerr;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100118
119 struct list_head device_list;
Bjørn Mork3cc36152012-03-06 17:29:22 +0100120 int (*manage_power)(struct usb_interface *, int);
Oliver Neukumafba9372008-05-13 17:01:25 +0200121};
122
123static struct usb_driver wdm_driver;
124
Bjørn Morkb0c13862012-03-06 17:29:21 +0100125/* return intfdata if we own the interface, else look up intf in the list */
126static struct wdm_device *wdm_find_device(struct usb_interface *intf)
127{
128 struct wdm_device *desc = NULL;
129
130 spin_lock(&wdm_device_list_lock);
131 list_for_each_entry(desc, &wdm_device_list, device_list)
132 if (desc->intf == intf)
133 break;
134 spin_unlock(&wdm_device_list_lock);
135
136 return desc;
137}
138
139static struct wdm_device *wdm_find_device_by_minor(int minor)
140{
141 struct wdm_device *desc = NULL;
142
143 spin_lock(&wdm_device_list_lock);
144 list_for_each_entry(desc, &wdm_device_list, device_list)
145 if (desc->intf->minor == minor)
146 break;
147 spin_unlock(&wdm_device_list_lock);
148
149 return desc;
150}
151
Oliver Neukumafba9372008-05-13 17:01:25 +0200152/* --- callbacks --- */
153static void wdm_out_callback(struct urb *urb)
154{
155 struct wdm_device *desc;
156 desc = urb->context;
157 spin_lock(&desc->iuspin);
158 desc->werr = urb->status;
159 spin_unlock(&desc->iuspin);
160 clear_bit(WDM_IN_USE, &desc->flags);
161 kfree(desc->outbuf);
162 wake_up(&desc->wait);
163}
164
165static void wdm_in_callback(struct urb *urb)
166{
167 struct wdm_device *desc = urb->context;
168 int status = urb->status;
169
170 spin_lock(&desc->iuspin);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100171 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200172
173 if (status) {
174 switch (status) {
175 case -ENOENT:
176 dev_dbg(&desc->intf->dev,
177 "nonzero urb status received: -ENOENT");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100178 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200179 case -ECONNRESET:
180 dev_dbg(&desc->intf->dev,
181 "nonzero urb status received: -ECONNRESET");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100182 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200183 case -ESHUTDOWN:
184 dev_dbg(&desc->intf->dev,
185 "nonzero urb status received: -ESHUTDOWN");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100186 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200187 case -EPIPE:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700188 dev_err(&desc->intf->dev,
189 "nonzero urb status received: -EPIPE\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200190 break;
191 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700192 dev_err(&desc->intf->dev,
193 "Unexpected error %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200194 break;
195 }
196 }
197
198 desc->rerr = status;
199 desc->reslength = urb->actual_length;
200 memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength);
201 desc->length += desc->reslength;
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100202skip_error:
Oliver Neukumafba9372008-05-13 17:01:25 +0200203 wake_up(&desc->wait);
204
205 set_bit(WDM_READ, &desc->flags);
206 spin_unlock(&desc->iuspin);
207}
208
209static void wdm_int_callback(struct urb *urb)
210{
211 int rv = 0;
212 int status = urb->status;
213 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200214 struct usb_cdc_notification *dr;
215
216 desc = urb->context;
Oliver Neukumafba9372008-05-13 17:01:25 +0200217 dr = (struct usb_cdc_notification *)desc->sbuf;
218
219 if (status) {
220 switch (status) {
221 case -ESHUTDOWN:
222 case -ENOENT:
223 case -ECONNRESET:
224 return; /* unplug */
225 case -EPIPE:
226 set_bit(WDM_INT_STALL, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700227 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200228 goto sw; /* halt is cleared in work */
229 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700230 dev_err(&desc->intf->dev,
231 "nonzero urb status received: %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200232 break;
233 }
234 }
235
236 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700237 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
238 urb->actual_length);
Oliver Neukumafba9372008-05-13 17:01:25 +0200239 goto exit;
240 }
241
242 switch (dr->bNotificationType) {
243 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
244 dev_dbg(&desc->intf->dev,
245 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
246 dr->wIndex, dr->wLength);
247 break;
248
249 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
250
251 dev_dbg(&desc->intf->dev,
252 "NOTIFY_NETWORK_CONNECTION %s network",
253 dr->wValue ? "connected to" : "disconnected from");
254 goto exit;
255 default:
256 clear_bit(WDM_POLL_RUNNING, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700257 dev_err(&desc->intf->dev,
258 "unknown notification %d received: index %d len %d\n",
Oliver Neukumafba9372008-05-13 17:01:25 +0200259 dr->bNotificationType, dr->wIndex, dr->wLength);
260 goto exit;
261 }
262
Oliver Neukumafba9372008-05-13 17:01:25 +0200263 spin_lock(&desc->iuspin);
264 clear_bit(WDM_READ, &desc->flags);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100265 set_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100266 if (!test_bit(WDM_DISCONNECTING, &desc->flags)
267 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200268 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
269 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
270 __func__, rv);
271 }
272 spin_unlock(&desc->iuspin);
273 if (rv < 0) {
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100274 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200275 if (rv == -EPERM)
276 return;
277 if (rv == -ENOMEM) {
278sw:
279 rv = schedule_work(&desc->rxwork);
280 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700281 dev_err(&desc->intf->dev,
282 "Cannot schedule work\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200283 }
284 }
285exit:
286 rv = usb_submit_urb(urb, GFP_ATOMIC);
287 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700288 dev_err(&desc->intf->dev,
289 "%s - usb_submit_urb failed with result %d\n",
290 __func__, rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200291
292}
293
294static void kill_urbs(struct wdm_device *desc)
295{
Oliver Neukum17d80d52008-06-24 15:56:10 +0200296 /* the order here is essential */
Oliver Neukumafba9372008-05-13 17:01:25 +0200297 usb_kill_urb(desc->command);
298 usb_kill_urb(desc->validity);
299 usb_kill_urb(desc->response);
300}
301
302static void free_urbs(struct wdm_device *desc)
303{
304 usb_free_urb(desc->validity);
305 usb_free_urb(desc->response);
306 usb_free_urb(desc->command);
307}
308
309static void cleanup(struct wdm_device *desc)
310{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100311 spin_lock(&wdm_device_list_lock);
312 list_del(&desc->device_list);
313 spin_unlock(&wdm_device_list_lock);
Bjørn Mork8457d992012-01-16 15:12:00 +0100314 kfree(desc->sbuf);
315 kfree(desc->inbuf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200316 kfree(desc->orq);
317 kfree(desc->irq);
318 kfree(desc->ubuf);
319 free_urbs(desc);
320 kfree(desc);
321}
322
323static ssize_t wdm_write
324(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
325{
326 u8 *buf;
327 int rv = -EMSGSIZE, r, we;
328 struct wdm_device *desc = file->private_data;
329 struct usb_ctrlrequest *req;
330
331 if (count > desc->wMaxCommand)
332 count = desc->wMaxCommand;
333
334 spin_lock_irq(&desc->iuspin);
335 we = desc->werr;
336 desc->werr = 0;
337 spin_unlock_irq(&desc->iuspin);
338 if (we < 0)
339 return -EIO;
340
Oliver Neukum860e41a2010-02-27 20:54:24 +0100341 desc->outbuf = buf = kmalloc(count, GFP_KERNEL);
342 if (!buf) {
343 rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200344 goto outnl;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100345 }
346
347 r = copy_from_user(buf, buffer, count);
348 if (r > 0) {
349 kfree(buf);
350 rv = -EFAULT;
351 goto outnl;
352 }
353
354 /* concurrent writes and disconnect */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100355 r = mutex_lock_interruptible(&desc->wlock);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100356 rv = -ERESTARTSYS;
357 if (r) {
358 kfree(buf);
359 goto outnl;
360 }
361
362 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
363 kfree(buf);
364 rv = -ENODEV;
365 goto outnp;
366 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200367
Oliver Neukum17d80d52008-06-24 15:56:10 +0200368 r = usb_autopm_get_interface(desc->intf);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100369 if (r < 0) {
370 kfree(buf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200371 goto outnp;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100372 }
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200373
David Sterba0cdfb812010-12-27 18:49:58 +0100374 if (!(file->f_flags & O_NONBLOCK))
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200375 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
376 &desc->flags));
377 else
378 if (test_bit(WDM_IN_USE, &desc->flags))
379 r = -EAGAIN;
Bjørn Mork88044202012-02-10 09:44:08 +0100380
381 if (test_bit(WDM_RESETTING, &desc->flags))
382 r = -EIO;
383
Oliver Neukum860e41a2010-02-27 20:54:24 +0100384 if (r < 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200385 kfree(buf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200386 goto out;
387 }
388
389 req = desc->orq;
390 usb_fill_control_urb(
391 desc->command,
392 interface_to_usbdev(desc->intf),
393 /* using common endpoint 0 */
394 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
395 (unsigned char *)req,
396 buf,
397 count,
398 wdm_out_callback,
399 desc
400 );
401
402 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
403 USB_RECIP_INTERFACE);
404 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
405 req->wValue = 0;
406 req->wIndex = desc->inum;
407 req->wLength = cpu_to_le16(count);
408 set_bit(WDM_IN_USE, &desc->flags);
409
410 rv = usb_submit_urb(desc->command, GFP_KERNEL);
411 if (rv < 0) {
412 kfree(buf);
413 clear_bit(WDM_IN_USE, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700414 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200415 } else {
416 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
417 req->wIndex);
418 }
419out:
Oliver Neukum17d80d52008-06-24 15:56:10 +0200420 usb_autopm_put_interface(desc->intf);
421outnp:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100422 mutex_unlock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200423outnl:
424 return rv < 0 ? rv : count;
425}
426
427static ssize_t wdm_read
428(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
429{
Ben Hutchings711c68b2012-02-12 06:00:41 +0000430 int rv, cntr;
Oliver Neukumafba9372008-05-13 17:01:25 +0200431 int i = 0;
432 struct wdm_device *desc = file->private_data;
433
434
Bjørn Morke8537bd2012-01-16 12:41:48 +0100435 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
Oliver Neukumafba9372008-05-13 17:01:25 +0200436 if (rv < 0)
437 return -ERESTARTSYS;
438
Ben Hutchings711c68b2012-02-12 06:00:41 +0000439 cntr = ACCESS_ONCE(desc->length);
440 if (cntr == 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200441 desc->read = 0;
442retry:
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200443 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
444 rv = -ENODEV;
445 goto err;
446 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200447 i++;
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200448 if (file->f_flags & O_NONBLOCK) {
449 if (!test_bit(WDM_READ, &desc->flags)) {
450 rv = cntr ? cntr : -EAGAIN;
451 goto err;
452 }
453 rv = 0;
454 } else {
455 rv = wait_event_interruptible(desc->wait,
456 test_bit(WDM_READ, &desc->flags));
457 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200458
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200459 /* may have happened while we slept */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200460 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
461 rv = -ENODEV;
462 goto err;
463 }
Bjørn Mork88044202012-02-10 09:44:08 +0100464 if (test_bit(WDM_RESETTING, &desc->flags)) {
465 rv = -EIO;
466 goto err;
467 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200468 usb_mark_last_busy(interface_to_usbdev(desc->intf));
Oliver Neukumafba9372008-05-13 17:01:25 +0200469 if (rv < 0) {
470 rv = -ERESTARTSYS;
471 goto err;
472 }
473
474 spin_lock_irq(&desc->iuspin);
475
476 if (desc->rerr) { /* read completed, error happened */
Oliver Neukumafba9372008-05-13 17:01:25 +0200477 desc->rerr = 0;
478 spin_unlock_irq(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200479 rv = -EIO;
480 goto err;
481 }
482 /*
483 * recheck whether we've lost the race
484 * against the completion handler
485 */
486 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
487 spin_unlock_irq(&desc->iuspin);
488 goto retry;
489 }
490 if (!desc->reslength) { /* zero length read */
491 spin_unlock_irq(&desc->iuspin);
492 goto retry;
493 }
Ben Hutchings711c68b2012-02-12 06:00:41 +0000494 cntr = desc->length;
Oliver Neukumafba9372008-05-13 17:01:25 +0200495 spin_unlock_irq(&desc->iuspin);
496 }
497
Ben Hutchings711c68b2012-02-12 06:00:41 +0000498 if (cntr > count)
499 cntr = count;
Oliver Neukumafba9372008-05-13 17:01:25 +0200500 rv = copy_to_user(buffer, desc->ubuf, cntr);
501 if (rv > 0) {
502 rv = -EFAULT;
503 goto err;
504 }
505
Ben Hutchings711c68b2012-02-12 06:00:41 +0000506 spin_lock_irq(&desc->iuspin);
507
Oliver Neukumafba9372008-05-13 17:01:25 +0200508 for (i = 0; i < desc->length - cntr; i++)
509 desc->ubuf[i] = desc->ubuf[i + cntr];
510
511 desc->length -= cntr;
Oliver Neukum87d65e52008-06-19 14:20:18 +0200512 /* in case we had outstanding data */
513 if (!desc->length)
514 clear_bit(WDM_READ, &desc->flags);
Ben Hutchings711c68b2012-02-12 06:00:41 +0000515
516 spin_unlock_irq(&desc->iuspin);
517
Oliver Neukumafba9372008-05-13 17:01:25 +0200518 rv = cntr;
519
520err:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100521 mutex_unlock(&desc->rlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200522 return rv;
523}
524
525static int wdm_flush(struct file *file, fl_owner_t id)
526{
527 struct wdm_device *desc = file->private_data;
528
529 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
530 if (desc->werr < 0)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700531 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
532 desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200533
Oliver Neukum24a85ba2012-04-27 14:23:54 +0200534 return usb_translate_errors(desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200535}
536
537static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
538{
539 struct wdm_device *desc = file->private_data;
540 unsigned long flags;
541 unsigned int mask = 0;
542
543 spin_lock_irqsave(&desc->iuspin, flags);
544 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
545 mask = POLLERR;
546 spin_unlock_irqrestore(&desc->iuspin, flags);
547 goto desc_out;
548 }
549 if (test_bit(WDM_READ, &desc->flags))
550 mask = POLLIN | POLLRDNORM;
551 if (desc->rerr || desc->werr)
552 mask |= POLLERR;
553 if (!test_bit(WDM_IN_USE, &desc->flags))
554 mask |= POLLOUT | POLLWRNORM;
555 spin_unlock_irqrestore(&desc->iuspin, flags);
556
557 poll_wait(file, &desc->wait, wait);
558
559desc_out:
560 return mask;
561}
562
563static int wdm_open(struct inode *inode, struct file *file)
564{
565 int minor = iminor(inode);
566 int rv = -ENODEV;
567 struct usb_interface *intf;
568 struct wdm_device *desc;
569
570 mutex_lock(&wdm_mutex);
Bjørn Morkb0c13862012-03-06 17:29:21 +0100571 desc = wdm_find_device_by_minor(minor);
572 if (!desc)
Oliver Neukumafba9372008-05-13 17:01:25 +0200573 goto out;
574
Bjørn Morkb0c13862012-03-06 17:29:21 +0100575 intf = desc->intf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200576 if (test_bit(WDM_DISCONNECTING, &desc->flags))
577 goto out;
Oliver Neukumafba9372008-05-13 17:01:25 +0200578 file->private_data = desc;
579
Oliver Neukum17d80d52008-06-24 15:56:10 +0200580 rv = usb_autopm_get_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200581 if (rv < 0) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700582 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200583 goto out;
584 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200585
Bjørn Morke8537bd2012-01-16 12:41:48 +0100586 /* using write lock to protect desc->count */
587 mutex_lock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200588 if (!desc->count++) {
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200589 desc->werr = 0;
590 desc->rerr = 0;
Oliver Neukum17d80d52008-06-24 15:56:10 +0200591 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
592 if (rv < 0) {
593 desc->count--;
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700594 dev_err(&desc->intf->dev,
595 "Error submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200596 }
597 } else {
598 rv = 0;
599 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100600 mutex_unlock(&desc->wlock);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100601 if (desc->count == 1)
602 desc->manage_power(intf, 1);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200603 usb_autopm_put_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200604out:
605 mutex_unlock(&wdm_mutex);
606 return rv;
607}
608
609static int wdm_release(struct inode *inode, struct file *file)
610{
611 struct wdm_device *desc = file->private_data;
612
613 mutex_lock(&wdm_mutex);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100614
615 /* using write lock to protect desc->count */
616 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200617 desc->count--;
Bjørn Morke8537bd2012-01-16 12:41:48 +0100618 mutex_unlock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200619
Oliver Neukumafba9372008-05-13 17:01:25 +0200620 if (!desc->count) {
621 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
622 kill_urbs(desc);
Bjørn Mork880bca32012-04-30 09:26:11 +0200623 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
Bjørn Mork3cc36152012-03-06 17:29:22 +0100624 desc->manage_power(desc->intf, 0);
Bjørn Mork880bca32012-04-30 09:26:11 +0200625 } else {
626 dev_dbg(&desc->intf->dev, "%s: device gone - cleaning up\n", __func__);
Oliver Neukum2f338c82012-04-27 14:36:37 +0200627 cleanup(desc);
Bjørn Mork880bca32012-04-30 09:26:11 +0200628 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200629 }
630 mutex_unlock(&wdm_mutex);
631 return 0;
632}
633
634static const struct file_operations wdm_fops = {
635 .owner = THIS_MODULE,
636 .read = wdm_read,
637 .write = wdm_write,
638 .open = wdm_open,
639 .flush = wdm_flush,
640 .release = wdm_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200641 .poll = wdm_poll,
642 .llseek = noop_llseek,
Oliver Neukumafba9372008-05-13 17:01:25 +0200643};
644
645static struct usb_class_driver wdm_class = {
646 .name = "cdc-wdm%d",
647 .fops = &wdm_fops,
648 .minor_base = WDM_MINOR_BASE,
649};
650
651/* --- error handling --- */
652static void wdm_rxwork(struct work_struct *work)
653{
654 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
655 unsigned long flags;
656 int rv;
657
658 spin_lock_irqsave(&desc->iuspin, flags);
659 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
660 spin_unlock_irqrestore(&desc->iuspin, flags);
661 } else {
662 spin_unlock_irqrestore(&desc->iuspin, flags);
663 rv = usb_submit_urb(desc->response, GFP_KERNEL);
664 if (rv < 0 && rv != -EPERM) {
665 spin_lock_irqsave(&desc->iuspin, flags);
666 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
667 schedule_work(&desc->rxwork);
668 spin_unlock_irqrestore(&desc->iuspin, flags);
669 }
670 }
671}
672
673/* --- hotplug --- */
674
Bjørn Mork3cc36152012-03-06 17:29:22 +0100675static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
676 u16 bufsize, int (*manage_power)(struct usb_interface *, int))
Oliver Neukumafba9372008-05-13 17:01:25 +0200677{
Bjørn Mork0dffb482012-03-06 17:29:20 +0100678 int rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200679 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200680
Oliver Neukumafba9372008-05-13 17:01:25 +0200681 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
682 if (!desc)
683 goto out;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100684 INIT_LIST_HEAD(&desc->device_list);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100685 mutex_init(&desc->rlock);
686 mutex_init(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200687 spin_lock_init(&desc->iuspin);
688 init_waitqueue_head(&desc->wait);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100689 desc->wMaxCommand = bufsize;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200690 /* this will be expanded and needed in hardware endianness */
Oliver Neukumafba9372008-05-13 17:01:25 +0200691 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
692 desc->intf = intf;
693 INIT_WORK(&desc->rxwork, wdm_rxwork);
694
Oliver Neukum052fbc02009-04-20 17:24:49 +0200695 rv = -EINVAL;
Bjørn Mork0dffb482012-03-06 17:29:20 +0100696 if (!usb_endpoint_is_int_in(ep))
Oliver Neukum052fbc02009-04-20 17:24:49 +0200697 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200698
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700699 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
Oliver Neukumafba9372008-05-13 17:01:25 +0200700
701 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
702 if (!desc->orq)
703 goto err;
704 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
705 if (!desc->irq)
706 goto err;
707
708 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
709 if (!desc->validity)
710 goto err;
711
712 desc->response = usb_alloc_urb(0, GFP_KERNEL);
713 if (!desc->response)
714 goto err;
715
716 desc->command = usb_alloc_urb(0, GFP_KERNEL);
717 if (!desc->command)
718 goto err;
719
720 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
721 if (!desc->ubuf)
722 goto err;
723
Bjørn Mork8457d992012-01-16 15:12:00 +0100724 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200725 if (!desc->sbuf)
726 goto err;
727
Bjørn Mork8457d992012-01-16 15:12:00 +0100728 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200729 if (!desc->inbuf)
Bjørn Mork8457d992012-01-16 15:12:00 +0100730 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200731
732 usb_fill_int_urb(
733 desc->validity,
734 interface_to_usbdev(intf),
735 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
736 desc->sbuf,
737 desc->wMaxPacketSize,
738 wdm_int_callback,
739 desc,
740 ep->bInterval
741 );
Oliver Neukumafba9372008-05-13 17:01:25 +0200742
Bjørn Mork19b85b32012-01-16 15:11:58 +0100743 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
744 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
745 desc->irq->wValue = 0;
746 desc->irq->wIndex = desc->inum;
747 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
748
749 usb_fill_control_urb(
750 desc->response,
Bjørn Mork8143a892012-01-16 15:12:01 +0100751 interface_to_usbdev(intf),
Bjørn Mork19b85b32012-01-16 15:11:58 +0100752 /* using common endpoint 0 */
753 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
754 (unsigned char *)desc->irq,
755 desc->inbuf,
756 desc->wMaxCommand,
757 wdm_in_callback,
758 desc
759 );
Bjørn Mork19b85b32012-01-16 15:11:58 +0100760
Bjørn Mork3cc36152012-03-06 17:29:22 +0100761 desc->manage_power = manage_power;
762
Bjørn Morkb0c13862012-03-06 17:29:21 +0100763 spin_lock(&wdm_device_list_lock);
764 list_add(&desc->device_list, &wdm_device_list);
765 spin_unlock(&wdm_device_list_lock);
766
Oliver Neukumafba9372008-05-13 17:01:25 +0200767 rv = usb_register_dev(intf, &wdm_class);
Oliver Neukumafba9372008-05-13 17:01:25 +0200768 if (rv < 0)
Bjørn Morkb0c13862012-03-06 17:29:21 +0100769 goto err;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200770 else
Bjørn Mork820c6292012-01-20 04:17:25 +0100771 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
Oliver Neukumafba9372008-05-13 17:01:25 +0200772out:
773 return rv;
Oliver Neukumafba9372008-05-13 17:01:25 +0200774err:
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);
Oliver Neukumafba9372008-05-13 17:01:25 +0200900 if (!desc->count)
901 cleanup(desc);
Bjørn Mork880bca32012-04-30 09:26:11 +0200902 else
903 dev_dbg(&intf->dev, "%s: %d open files - postponing cleanup\n", __func__, desc->count);
Oliver Neukumafba9372008-05-13 17:01:25 +0200904 mutex_unlock(&wdm_mutex);
905}
906
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100907#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200908static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
909{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100910 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200911 int rv = 0;
912
913 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
914
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100915 /* if this is an autosuspend the caller does the locking */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100916 if (!PMSG_IS_AUTO(message)) {
917 mutex_lock(&desc->rlock);
918 mutex_lock(&desc->wlock);
919 }
Oliver Neukum62e66852010-02-27 20:56:22 +0100920 spin_lock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100921
Alan Stern5b1b0b82011-08-19 23:49:48 +0200922 if (PMSG_IS_AUTO(message) &&
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100923 (test_bit(WDM_IN_USE, &desc->flags)
924 || test_bit(WDM_RESPONDING, &desc->flags))) {
Oliver Neukum62e66852010-02-27 20:56:22 +0100925 spin_unlock_irq(&desc->iuspin);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200926 rv = -EBUSY;
927 } else {
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100928
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100929 set_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100930 spin_unlock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100931 /* callback submits work - order is essential */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200932 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100933 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200934 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100935 if (!PMSG_IS_AUTO(message)) {
936 mutex_unlock(&desc->wlock);
937 mutex_unlock(&desc->rlock);
938 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200939
940 return rv;
941}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100942#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200943
944static int recover_from_urb_loss(struct wdm_device *desc)
945{
946 int rv = 0;
947
948 if (desc->count) {
949 rv = usb_submit_urb(desc->validity, GFP_NOIO);
950 if (rv < 0)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700951 dev_err(&desc->intf->dev,
952 "Error resume submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200953 }
954 return rv;
955}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100956
957#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200958static int wdm_resume(struct usb_interface *intf)
959{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100960 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200961 int rv;
962
963 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
Oliver Neukum338124c2010-02-27 20:57:12 +0100964
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100965 clear_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100966 rv = recover_from_urb_loss(desc);
Oliver Neukum338124c2010-02-27 20:57:12 +0100967
Oliver Neukum17d80d52008-06-24 15:56:10 +0200968 return rv;
969}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100970#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200971
972static int wdm_pre_reset(struct usb_interface *intf)
973{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100974 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200975
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200976 /*
977 * we notify everybody using poll of
978 * an exceptional situation
979 * must be done before recovery lest a spontaneous
980 * message from the device is lost
981 */
982 spin_lock_irq(&desc->iuspin);
Bjørn Mork88044202012-02-10 09:44:08 +0100983 set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
984 set_bit(WDM_READ, &desc->flags); /* unblock read */
985 clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200986 desc->rerr = -EINTR;
987 spin_unlock_irq(&desc->iuspin);
988 wake_up_all(&desc->wait);
Bjørn Mork88044202012-02-10 09:44:08 +0100989 mutex_lock(&desc->rlock);
990 mutex_lock(&desc->wlock);
991 kill_urbs(desc);
992 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200993 return 0;
994}
995
996static int wdm_post_reset(struct usb_interface *intf)
997{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100998 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200999 int rv;
1000
Bjørn Mork88044202012-02-10 09:44:08 +01001001 clear_bit(WDM_RESETTING, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001002 rv = recover_from_urb_loss(desc);
Bjørn Morke8537bd2012-01-16 12:41:48 +01001003 mutex_unlock(&desc->wlock);
1004 mutex_unlock(&desc->rlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001005 return 0;
1006}
1007
Oliver Neukumafba9372008-05-13 17:01:25 +02001008static struct usb_driver wdm_driver = {
1009 .name = "cdc_wdm",
1010 .probe = wdm_probe,
1011 .disconnect = wdm_disconnect,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001012#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +02001013 .suspend = wdm_suspend,
1014 .resume = wdm_resume,
1015 .reset_resume = wdm_resume,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001016#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +02001017 .pre_reset = wdm_pre_reset,
1018 .post_reset = wdm_post_reset,
Oliver Neukumafba9372008-05-13 17:01:25 +02001019 .id_table = wdm_ids,
Oliver Neukum17d80d52008-06-24 15:56:10 +02001020 .supports_autosuspend = 1,
Oliver Neukumafba9372008-05-13 17:01:25 +02001021};
1022
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001023module_usb_driver(wdm_driver);
Oliver Neukumafba9372008-05-13 17:01:25 +02001024
1025MODULE_AUTHOR(DRIVER_AUTHOR);
Oliver Neukum87d65e52008-06-19 14:20:18 +02001026MODULE_DESCRIPTION(DRIVER_DESC);
Oliver Neukumafba9372008-05-13 17:01:25 +02001027MODULE_LICENSE("GPL");