blob: 0bb2b3248dad99b0e114b83dc1028edd5fadb4a1 [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);
Oliver Neukumafba9372008-05-13 17:01:25 +0200160 kfree(desc->outbuf);
Oliver Neukum5c228372012-04-26 21:59:10 +0200161 desc->outbuf = NULL;
162 clear_bit(WDM_IN_USE, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200163 wake_up(&desc->wait);
164}
165
166static void wdm_in_callback(struct urb *urb)
167{
168 struct wdm_device *desc = urb->context;
169 int status = urb->status;
170
171 spin_lock(&desc->iuspin);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100172 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200173
174 if (status) {
175 switch (status) {
176 case -ENOENT:
177 dev_dbg(&desc->intf->dev,
178 "nonzero urb status received: -ENOENT");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100179 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200180 case -ECONNRESET:
181 dev_dbg(&desc->intf->dev,
182 "nonzero urb status received: -ECONNRESET");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100183 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200184 case -ESHUTDOWN:
185 dev_dbg(&desc->intf->dev,
186 "nonzero urb status received: -ESHUTDOWN");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100187 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200188 case -EPIPE:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700189 dev_err(&desc->intf->dev,
190 "nonzero urb status received: -EPIPE\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200191 break;
192 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700193 dev_err(&desc->intf->dev,
194 "Unexpected error %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200195 break;
196 }
197 }
198
199 desc->rerr = status;
200 desc->reslength = urb->actual_length;
201 memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength);
202 desc->length += desc->reslength;
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100203skip_error:
Oliver Neukumafba9372008-05-13 17:01:25 +0200204 wake_up(&desc->wait);
205
206 set_bit(WDM_READ, &desc->flags);
207 spin_unlock(&desc->iuspin);
208}
209
210static void wdm_int_callback(struct urb *urb)
211{
212 int rv = 0;
213 int status = urb->status;
214 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200215 struct usb_cdc_notification *dr;
216
217 desc = urb->context;
Oliver Neukumafba9372008-05-13 17:01:25 +0200218 dr = (struct usb_cdc_notification *)desc->sbuf;
219
220 if (status) {
221 switch (status) {
222 case -ESHUTDOWN:
223 case -ENOENT:
224 case -ECONNRESET:
225 return; /* unplug */
226 case -EPIPE:
227 set_bit(WDM_INT_STALL, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700228 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200229 goto sw; /* halt is cleared in work */
230 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700231 dev_err(&desc->intf->dev,
232 "nonzero urb status received: %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200233 break;
234 }
235 }
236
237 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700238 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
239 urb->actual_length);
Oliver Neukumafba9372008-05-13 17:01:25 +0200240 goto exit;
241 }
242
243 switch (dr->bNotificationType) {
244 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
245 dev_dbg(&desc->intf->dev,
246 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
247 dr->wIndex, dr->wLength);
248 break;
249
250 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
251
252 dev_dbg(&desc->intf->dev,
253 "NOTIFY_NETWORK_CONNECTION %s network",
254 dr->wValue ? "connected to" : "disconnected from");
255 goto exit;
256 default:
257 clear_bit(WDM_POLL_RUNNING, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700258 dev_err(&desc->intf->dev,
259 "unknown notification %d received: index %d len %d\n",
Oliver Neukumafba9372008-05-13 17:01:25 +0200260 dr->bNotificationType, dr->wIndex, dr->wLength);
261 goto exit;
262 }
263
Oliver Neukumafba9372008-05-13 17:01:25 +0200264 spin_lock(&desc->iuspin);
265 clear_bit(WDM_READ, &desc->flags);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100266 set_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100267 if (!test_bit(WDM_DISCONNECTING, &desc->flags)
268 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200269 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
270 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
271 __func__, rv);
272 }
273 spin_unlock(&desc->iuspin);
274 if (rv < 0) {
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100275 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200276 if (rv == -EPERM)
277 return;
278 if (rv == -ENOMEM) {
279sw:
280 rv = schedule_work(&desc->rxwork);
281 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700282 dev_err(&desc->intf->dev,
283 "Cannot schedule work\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200284 }
285 }
286exit:
287 rv = usb_submit_urb(urb, GFP_ATOMIC);
288 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700289 dev_err(&desc->intf->dev,
290 "%s - usb_submit_urb failed with result %d\n",
291 __func__, rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200292
293}
294
295static void kill_urbs(struct wdm_device *desc)
296{
Oliver Neukum17d80d52008-06-24 15:56:10 +0200297 /* the order here is essential */
Oliver Neukumafba9372008-05-13 17:01:25 +0200298 usb_kill_urb(desc->command);
299 usb_kill_urb(desc->validity);
300 usb_kill_urb(desc->response);
301}
302
303static void free_urbs(struct wdm_device *desc)
304{
305 usb_free_urb(desc->validity);
306 usb_free_urb(desc->response);
307 usb_free_urb(desc->command);
308}
309
310static void cleanup(struct wdm_device *desc)
311{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100312 spin_lock(&wdm_device_list_lock);
313 list_del(&desc->device_list);
314 spin_unlock(&wdm_device_list_lock);
Bjørn Mork8457d992012-01-16 15:12:00 +0100315 kfree(desc->sbuf);
316 kfree(desc->inbuf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200317 kfree(desc->orq);
318 kfree(desc->irq);
319 kfree(desc->ubuf);
320 free_urbs(desc);
321 kfree(desc);
322}
323
324static ssize_t wdm_write
325(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
326{
327 u8 *buf;
328 int rv = -EMSGSIZE, r, we;
329 struct wdm_device *desc = file->private_data;
330 struct usb_ctrlrequest *req;
331
332 if (count > desc->wMaxCommand)
333 count = desc->wMaxCommand;
334
335 spin_lock_irq(&desc->iuspin);
336 we = desc->werr;
337 desc->werr = 0;
338 spin_unlock_irq(&desc->iuspin);
339 if (we < 0)
340 return -EIO;
341
Oliver Neukum5c228372012-04-26 21:59:10 +0200342 buf = kmalloc(count, GFP_KERNEL);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100343 if (!buf) {
344 rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200345 goto outnl;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100346 }
347
348 r = copy_from_user(buf, buffer, count);
349 if (r > 0) {
350 kfree(buf);
351 rv = -EFAULT;
352 goto outnl;
353 }
354
355 /* concurrent writes and disconnect */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100356 r = mutex_lock_interruptible(&desc->wlock);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100357 rv = -ERESTARTSYS;
358 if (r) {
359 kfree(buf);
360 goto outnl;
361 }
362
363 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
364 kfree(buf);
365 rv = -ENODEV;
366 goto outnp;
367 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200368
Oliver Neukum17d80d52008-06-24 15:56:10 +0200369 r = usb_autopm_get_interface(desc->intf);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100370 if (r < 0) {
371 kfree(buf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200372 goto outnp;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100373 }
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200374
David Sterba0cdfb812010-12-27 18:49:58 +0100375 if (!(file->f_flags & O_NONBLOCK))
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200376 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
377 &desc->flags));
378 else
379 if (test_bit(WDM_IN_USE, &desc->flags))
380 r = -EAGAIN;
Bjørn Mork88044202012-02-10 09:44:08 +0100381
382 if (test_bit(WDM_RESETTING, &desc->flags))
383 r = -EIO;
384
Oliver Neukum860e41a2010-02-27 20:54:24 +0100385 if (r < 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200386 kfree(buf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200387 goto out;
388 }
389
390 req = desc->orq;
391 usb_fill_control_urb(
392 desc->command,
393 interface_to_usbdev(desc->intf),
394 /* using common endpoint 0 */
395 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
396 (unsigned char *)req,
397 buf,
398 count,
399 wdm_out_callback,
400 desc
401 );
402
403 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
404 USB_RECIP_INTERFACE);
405 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
406 req->wValue = 0;
407 req->wIndex = desc->inum;
408 req->wLength = cpu_to_le16(count);
409 set_bit(WDM_IN_USE, &desc->flags);
Oliver Neukum5c228372012-04-26 21:59:10 +0200410 desc->outbuf = buf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200411
412 rv = usb_submit_urb(desc->command, GFP_KERNEL);
413 if (rv < 0) {
414 kfree(buf);
Oliver Neukum5c228372012-04-26 21:59:10 +0200415 desc->outbuf = NULL;
Oliver Neukumafba9372008-05-13 17:01:25 +0200416 clear_bit(WDM_IN_USE, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700417 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200418 } else {
419 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
420 req->wIndex);
421 }
422out:
Oliver Neukum17d80d52008-06-24 15:56:10 +0200423 usb_autopm_put_interface(desc->intf);
424outnp:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100425 mutex_unlock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200426outnl:
427 return rv < 0 ? rv : count;
428}
429
430static ssize_t wdm_read
431(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
432{
Ben Hutchings711c68b2012-02-12 06:00:41 +0000433 int rv, cntr;
Oliver Neukumafba9372008-05-13 17:01:25 +0200434 int i = 0;
435 struct wdm_device *desc = file->private_data;
436
437
Bjørn Morke8537bd2012-01-16 12:41:48 +0100438 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
Oliver Neukumafba9372008-05-13 17:01:25 +0200439 if (rv < 0)
440 return -ERESTARTSYS;
441
Ben Hutchings711c68b2012-02-12 06:00:41 +0000442 cntr = ACCESS_ONCE(desc->length);
443 if (cntr == 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200444 desc->read = 0;
445retry:
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200446 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
447 rv = -ENODEV;
448 goto err;
449 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200450 i++;
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200451 if (file->f_flags & O_NONBLOCK) {
452 if (!test_bit(WDM_READ, &desc->flags)) {
453 rv = cntr ? cntr : -EAGAIN;
454 goto err;
455 }
456 rv = 0;
457 } else {
458 rv = wait_event_interruptible(desc->wait,
459 test_bit(WDM_READ, &desc->flags));
460 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200461
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200462 /* may have happened while we slept */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200463 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
464 rv = -ENODEV;
465 goto err;
466 }
Bjørn Mork88044202012-02-10 09:44:08 +0100467 if (test_bit(WDM_RESETTING, &desc->flags)) {
468 rv = -EIO;
469 goto err;
470 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200471 usb_mark_last_busy(interface_to_usbdev(desc->intf));
Oliver Neukumafba9372008-05-13 17:01:25 +0200472 if (rv < 0) {
473 rv = -ERESTARTSYS;
474 goto err;
475 }
476
477 spin_lock_irq(&desc->iuspin);
478
479 if (desc->rerr) { /* read completed, error happened */
Oliver Neukumafba9372008-05-13 17:01:25 +0200480 desc->rerr = 0;
481 spin_unlock_irq(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200482 rv = -EIO;
483 goto err;
484 }
485 /*
486 * recheck whether we've lost the race
487 * against the completion handler
488 */
489 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
490 spin_unlock_irq(&desc->iuspin);
491 goto retry;
492 }
493 if (!desc->reslength) { /* zero length read */
494 spin_unlock_irq(&desc->iuspin);
495 goto retry;
496 }
Ben Hutchings711c68b2012-02-12 06:00:41 +0000497 cntr = desc->length;
Oliver Neukumafba9372008-05-13 17:01:25 +0200498 spin_unlock_irq(&desc->iuspin);
499 }
500
Ben Hutchings711c68b2012-02-12 06:00:41 +0000501 if (cntr > count)
502 cntr = count;
Oliver Neukumafba9372008-05-13 17:01:25 +0200503 rv = copy_to_user(buffer, desc->ubuf, cntr);
504 if (rv > 0) {
505 rv = -EFAULT;
506 goto err;
507 }
508
Ben Hutchings711c68b2012-02-12 06:00:41 +0000509 spin_lock_irq(&desc->iuspin);
510
Oliver Neukumafba9372008-05-13 17:01:25 +0200511 for (i = 0; i < desc->length - cntr; i++)
512 desc->ubuf[i] = desc->ubuf[i + cntr];
513
514 desc->length -= cntr;
Oliver Neukum87d65e52008-06-19 14:20:18 +0200515 /* in case we had outstanding data */
516 if (!desc->length)
517 clear_bit(WDM_READ, &desc->flags);
Ben Hutchings711c68b2012-02-12 06:00:41 +0000518
519 spin_unlock_irq(&desc->iuspin);
520
Oliver Neukumafba9372008-05-13 17:01:25 +0200521 rv = cntr;
522
523err:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100524 mutex_unlock(&desc->rlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200525 return rv;
526}
527
528static int wdm_flush(struct file *file, fl_owner_t id)
529{
530 struct wdm_device *desc = file->private_data;
531
532 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
533 if (desc->werr < 0)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700534 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
535 desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200536
537 return desc->werr;
538}
539
540static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
541{
542 struct wdm_device *desc = file->private_data;
543 unsigned long flags;
544 unsigned int mask = 0;
545
546 spin_lock_irqsave(&desc->iuspin, flags);
547 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
548 mask = POLLERR;
549 spin_unlock_irqrestore(&desc->iuspin, flags);
550 goto desc_out;
551 }
552 if (test_bit(WDM_READ, &desc->flags))
553 mask = POLLIN | POLLRDNORM;
554 if (desc->rerr || desc->werr)
555 mask |= POLLERR;
556 if (!test_bit(WDM_IN_USE, &desc->flags))
557 mask |= POLLOUT | POLLWRNORM;
558 spin_unlock_irqrestore(&desc->iuspin, flags);
559
560 poll_wait(file, &desc->wait, wait);
561
562desc_out:
563 return mask;
564}
565
566static int wdm_open(struct inode *inode, struct file *file)
567{
568 int minor = iminor(inode);
569 int rv = -ENODEV;
570 struct usb_interface *intf;
571 struct wdm_device *desc;
572
573 mutex_lock(&wdm_mutex);
Bjørn Morkb0c13862012-03-06 17:29:21 +0100574 desc = wdm_find_device_by_minor(minor);
575 if (!desc)
Oliver Neukumafba9372008-05-13 17:01:25 +0200576 goto out;
577
Bjørn Morkb0c13862012-03-06 17:29:21 +0100578 intf = desc->intf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200579 if (test_bit(WDM_DISCONNECTING, &desc->flags))
580 goto out;
Oliver Neukumafba9372008-05-13 17:01:25 +0200581 file->private_data = desc;
582
Oliver Neukum17d80d52008-06-24 15:56:10 +0200583 rv = usb_autopm_get_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200584 if (rv < 0) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700585 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200586 goto out;
587 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200588
Bjørn Morke8537bd2012-01-16 12:41:48 +0100589 /* using write lock to protect desc->count */
590 mutex_lock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200591 if (!desc->count++) {
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200592 desc->werr = 0;
593 desc->rerr = 0;
Oliver Neukum17d80d52008-06-24 15:56:10 +0200594 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
595 if (rv < 0) {
596 desc->count--;
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700597 dev_err(&desc->intf->dev,
598 "Error submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200599 }
600 } else {
601 rv = 0;
602 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100603 mutex_unlock(&desc->wlock);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100604 if (desc->count == 1)
605 desc->manage_power(intf, 1);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200606 usb_autopm_put_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200607out:
608 mutex_unlock(&wdm_mutex);
609 return rv;
610}
611
612static int wdm_release(struct inode *inode, struct file *file)
613{
614 struct wdm_device *desc = file->private_data;
615
616 mutex_lock(&wdm_mutex);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100617
618 /* using write lock to protect desc->count */
619 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200620 desc->count--;
Bjørn Morke8537bd2012-01-16 12:41:48 +0100621 mutex_unlock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200622
Oliver Neukumafba9372008-05-13 17:01:25 +0200623 if (!desc->count) {
624 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
625 kill_urbs(desc);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200626 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
Bjørn Mork3cc36152012-03-06 17:29:22 +0100627 desc->manage_power(desc->intf, 0);
Oliver Neukumafba9372008-05-13 17:01:25 +0200628 }
629 mutex_unlock(&wdm_mutex);
630 return 0;
631}
632
633static const struct file_operations wdm_fops = {
634 .owner = THIS_MODULE,
635 .read = wdm_read,
636 .write = wdm_write,
637 .open = wdm_open,
638 .flush = wdm_flush,
639 .release = wdm_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200640 .poll = wdm_poll,
641 .llseek = noop_llseek,
Oliver Neukumafba9372008-05-13 17:01:25 +0200642};
643
644static struct usb_class_driver wdm_class = {
645 .name = "cdc-wdm%d",
646 .fops = &wdm_fops,
647 .minor_base = WDM_MINOR_BASE,
648};
649
650/* --- error handling --- */
651static void wdm_rxwork(struct work_struct *work)
652{
653 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
654 unsigned long flags;
655 int rv;
656
657 spin_lock_irqsave(&desc->iuspin, flags);
658 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
659 spin_unlock_irqrestore(&desc->iuspin, flags);
660 } else {
661 spin_unlock_irqrestore(&desc->iuspin, flags);
662 rv = usb_submit_urb(desc->response, GFP_KERNEL);
663 if (rv < 0 && rv != -EPERM) {
664 spin_lock_irqsave(&desc->iuspin, flags);
665 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
666 schedule_work(&desc->rxwork);
667 spin_unlock_irqrestore(&desc->iuspin, flags);
668 }
669 }
670}
671
672/* --- hotplug --- */
673
Bjørn Mork3cc36152012-03-06 17:29:22 +0100674static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
675 u16 bufsize, int (*manage_power)(struct usb_interface *, int))
Oliver Neukumafba9372008-05-13 17:01:25 +0200676{
Bjørn Mork0dffb482012-03-06 17:29:20 +0100677 int rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200678 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200679
Oliver Neukumafba9372008-05-13 17:01:25 +0200680 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
681 if (!desc)
682 goto out;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100683 INIT_LIST_HEAD(&desc->device_list);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100684 mutex_init(&desc->rlock);
685 mutex_init(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200686 spin_lock_init(&desc->iuspin);
687 init_waitqueue_head(&desc->wait);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100688 desc->wMaxCommand = bufsize;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200689 /* this will be expanded and needed in hardware endianness */
Oliver Neukumafba9372008-05-13 17:01:25 +0200690 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
691 desc->intf = intf;
692 INIT_WORK(&desc->rxwork, wdm_rxwork);
693
Oliver Neukum052fbc02009-04-20 17:24:49 +0200694 rv = -EINVAL;
Bjørn Mork0dffb482012-03-06 17:29:20 +0100695 if (!usb_endpoint_is_int_in(ep))
Oliver Neukum052fbc02009-04-20 17:24:49 +0200696 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200697
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700698 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
Oliver Neukumafba9372008-05-13 17:01:25 +0200699
700 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
701 if (!desc->orq)
702 goto err;
703 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
704 if (!desc->irq)
705 goto err;
706
707 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
708 if (!desc->validity)
709 goto err;
710
711 desc->response = usb_alloc_urb(0, GFP_KERNEL);
712 if (!desc->response)
713 goto err;
714
715 desc->command = usb_alloc_urb(0, GFP_KERNEL);
716 if (!desc->command)
717 goto err;
718
719 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
720 if (!desc->ubuf)
721 goto err;
722
Bjørn Mork8457d992012-01-16 15:12:00 +0100723 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200724 if (!desc->sbuf)
725 goto err;
726
Bjørn Mork8457d992012-01-16 15:12:00 +0100727 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200728 if (!desc->inbuf)
Bjørn Mork8457d992012-01-16 15:12:00 +0100729 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200730
731 usb_fill_int_urb(
732 desc->validity,
733 interface_to_usbdev(intf),
734 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
735 desc->sbuf,
736 desc->wMaxPacketSize,
737 wdm_int_callback,
738 desc,
739 ep->bInterval
740 );
Oliver Neukumafba9372008-05-13 17:01:25 +0200741
Bjørn Mork19b85b32012-01-16 15:11:58 +0100742 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
743 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
744 desc->irq->wValue = 0;
745 desc->irq->wIndex = desc->inum;
746 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
747
748 usb_fill_control_urb(
749 desc->response,
Bjørn Mork8143a892012-01-16 15:12:01 +0100750 interface_to_usbdev(intf),
Bjørn Mork19b85b32012-01-16 15:11:58 +0100751 /* using common endpoint 0 */
752 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
753 (unsigned char *)desc->irq,
754 desc->inbuf,
755 desc->wMaxCommand,
756 wdm_in_callback,
757 desc
758 );
Bjørn Mork19b85b32012-01-16 15:11:58 +0100759
Bjørn Mork3cc36152012-03-06 17:29:22 +0100760 desc->manage_power = manage_power;
761
Bjørn Morkb0c13862012-03-06 17:29:21 +0100762 spin_lock(&wdm_device_list_lock);
763 list_add(&desc->device_list, &wdm_device_list);
764 spin_unlock(&wdm_device_list_lock);
765
Oliver Neukumafba9372008-05-13 17:01:25 +0200766 rv = usb_register_dev(intf, &wdm_class);
Oliver Neukumafba9372008-05-13 17:01:25 +0200767 if (rv < 0)
Bjørn Morkb0c13862012-03-06 17:29:21 +0100768 goto err;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200769 else
Bjørn Mork820c6292012-01-20 04:17:25 +0100770 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
Oliver Neukumafba9372008-05-13 17:01:25 +0200771out:
772 return rv;
Oliver Neukumafba9372008-05-13 17:01:25 +0200773err:
Bjørn Mork0dffb482012-03-06 17:29:20 +0100774 cleanup(desc);
775 return rv;
776}
777
Bjørn Mork3cc36152012-03-06 17:29:22 +0100778static int wdm_manage_power(struct usb_interface *intf, int on)
779{
780 /* need autopm_get/put here to ensure the usbcore sees the new value */
781 int rv = usb_autopm_get_interface(intf);
782 if (rv < 0)
783 goto err;
784
785 intf->needs_remote_wakeup = on;
786 usb_autopm_put_interface(intf);
787err:
788 return rv;
789}
790
Bjørn Mork0dffb482012-03-06 17:29:20 +0100791static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
792{
793 int rv = -EINVAL;
794 struct usb_host_interface *iface;
795 struct usb_endpoint_descriptor *ep;
796 struct usb_cdc_dmm_desc *dmhd;
797 u8 *buffer = intf->altsetting->extra;
798 int buflen = intf->altsetting->extralen;
799 u16 maxcom = WDM_DEFAULT_BUFSIZE;
800
801 if (!buffer)
802 goto err;
803 while (buflen > 2) {
804 if (buffer[1] != USB_DT_CS_INTERFACE) {
805 dev_err(&intf->dev, "skipping garbage\n");
806 goto next_desc;
807 }
808
809 switch (buffer[2]) {
810 case USB_CDC_HEADER_TYPE:
811 break;
812 case USB_CDC_DMM_TYPE:
813 dmhd = (struct usb_cdc_dmm_desc *)buffer;
814 maxcom = le16_to_cpu(dmhd->wMaxCommand);
815 dev_dbg(&intf->dev,
816 "Finding maximum buffer length: %d", maxcom);
817 break;
818 default:
819 dev_err(&intf->dev,
820 "Ignoring extra header, type %d, length %d\n",
821 buffer[2], buffer[0]);
822 break;
823 }
824next_desc:
825 buflen -= buffer[0];
826 buffer += buffer[0];
827 }
828
829 iface = intf->cur_altsetting;
830 if (iface->desc.bNumEndpoints != 1)
831 goto err;
832 ep = &iface->endpoint[0].desc;
833
Bjørn Mork3cc36152012-03-06 17:29:22 +0100834 rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100835
836err:
Oliver Neukumafba9372008-05-13 17:01:25 +0200837 return rv;
838}
839
Bjørn Mork3cc36152012-03-06 17:29:22 +0100840/**
841 * usb_cdc_wdm_register - register a WDM subdriver
842 * @intf: usb interface the subdriver will associate with
843 * @ep: interrupt endpoint to monitor for notifications
844 * @bufsize: maximum message size to support for read/write
845 *
846 * Create WDM usb class character device and associate it with intf
847 * without binding, allowing another driver to manage the interface.
848 *
849 * The subdriver will manage the given interrupt endpoint exclusively
850 * and will issue control requests referring to the given intf. It
851 * will otherwise avoid interferring, and in particular not do
852 * usb_set_intfdata/usb_get_intfdata on intf.
853 *
854 * The return value is a pointer to the subdriver's struct usb_driver.
855 * The registering driver is responsible for calling this subdriver's
856 * disconnect, suspend, resume, pre_reset and post_reset methods from
857 * its own.
858 */
859struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
860 struct usb_endpoint_descriptor *ep,
861 int bufsize,
862 int (*manage_power)(struct usb_interface *, int))
863{
864 int rv = -EINVAL;
865
866 rv = wdm_create(intf, ep, bufsize, manage_power);
867 if (rv < 0)
868 goto err;
869
870 return &wdm_driver;
871err:
872 return ERR_PTR(rv);
873}
874EXPORT_SYMBOL(usb_cdc_wdm_register);
875
Oliver Neukumafba9372008-05-13 17:01:25 +0200876static void wdm_disconnect(struct usb_interface *intf)
877{
878 struct wdm_device *desc;
879 unsigned long flags;
880
881 usb_deregister_dev(intf, &wdm_class);
Bjørn Morkb0c13862012-03-06 17:29:21 +0100882 desc = wdm_find_device(intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200883 mutex_lock(&wdm_mutex);
Oliver Neukumafba9372008-05-13 17:01:25 +0200884
885 /* the spinlock makes sure no new urbs are generated in the callbacks */
886 spin_lock_irqsave(&desc->iuspin, flags);
887 set_bit(WDM_DISCONNECTING, &desc->flags);
888 set_bit(WDM_READ, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200889 /* to terminate pending flushes */
Oliver Neukumafba9372008-05-13 17:01:25 +0200890 clear_bit(WDM_IN_USE, &desc->flags);
891 spin_unlock_irqrestore(&desc->iuspin, flags);
Bjørn Mork62aaf242012-01-16 15:11:57 +0100892 wake_up_all(&desc->wait);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100893 mutex_lock(&desc->rlock);
894 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200895 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100896 cancel_work_sync(&desc->rxwork);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100897 mutex_unlock(&desc->wlock);
898 mutex_unlock(&desc->rlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200899 if (!desc->count)
900 cleanup(desc);
901 mutex_unlock(&wdm_mutex);
902}
903
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100904#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200905static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
906{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100907 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200908 int rv = 0;
909
910 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
911
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100912 /* if this is an autosuspend the caller does the locking */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100913 if (!PMSG_IS_AUTO(message)) {
914 mutex_lock(&desc->rlock);
915 mutex_lock(&desc->wlock);
916 }
Oliver Neukum62e66852010-02-27 20:56:22 +0100917 spin_lock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100918
Alan Stern5b1b0b82011-08-19 23:49:48 +0200919 if (PMSG_IS_AUTO(message) &&
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100920 (test_bit(WDM_IN_USE, &desc->flags)
921 || test_bit(WDM_RESPONDING, &desc->flags))) {
Oliver Neukum62e66852010-02-27 20:56:22 +0100922 spin_unlock_irq(&desc->iuspin);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200923 rv = -EBUSY;
924 } else {
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100925
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100926 set_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100927 spin_unlock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100928 /* callback submits work - order is essential */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200929 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100930 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200931 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100932 if (!PMSG_IS_AUTO(message)) {
933 mutex_unlock(&desc->wlock);
934 mutex_unlock(&desc->rlock);
935 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200936
937 return rv;
938}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100939#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200940
941static int recover_from_urb_loss(struct wdm_device *desc)
942{
943 int rv = 0;
944
945 if (desc->count) {
946 rv = usb_submit_urb(desc->validity, GFP_NOIO);
947 if (rv < 0)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700948 dev_err(&desc->intf->dev,
949 "Error resume submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200950 }
951 return rv;
952}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100953
954#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200955static int wdm_resume(struct usb_interface *intf)
956{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100957 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200958 int rv;
959
960 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
Oliver Neukum338124c2010-02-27 20:57:12 +0100961
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100962 clear_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100963 rv = recover_from_urb_loss(desc);
Oliver Neukum338124c2010-02-27 20:57:12 +0100964
Oliver Neukum17d80d52008-06-24 15:56:10 +0200965 return rv;
966}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100967#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200968
969static int wdm_pre_reset(struct usb_interface *intf)
970{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100971 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200972
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200973 /*
974 * we notify everybody using poll of
975 * an exceptional situation
976 * must be done before recovery lest a spontaneous
977 * message from the device is lost
978 */
979 spin_lock_irq(&desc->iuspin);
Bjørn Mork88044202012-02-10 09:44:08 +0100980 set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
981 set_bit(WDM_READ, &desc->flags); /* unblock read */
982 clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200983 desc->rerr = -EINTR;
984 spin_unlock_irq(&desc->iuspin);
985 wake_up_all(&desc->wait);
Bjørn Mork88044202012-02-10 09:44:08 +0100986 mutex_lock(&desc->rlock);
987 mutex_lock(&desc->wlock);
988 kill_urbs(desc);
989 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200990 return 0;
991}
992
993static int wdm_post_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 int rv;
997
Bjørn Mork88044202012-02-10 09:44:08 +0100998 clear_bit(WDM_RESETTING, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200999 rv = recover_from_urb_loss(desc);
Bjørn Morke8537bd2012-01-16 12:41:48 +01001000 mutex_unlock(&desc->wlock);
1001 mutex_unlock(&desc->rlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001002 return 0;
1003}
1004
Oliver Neukumafba9372008-05-13 17:01:25 +02001005static struct usb_driver wdm_driver = {
1006 .name = "cdc_wdm",
1007 .probe = wdm_probe,
1008 .disconnect = wdm_disconnect,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001009#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +02001010 .suspend = wdm_suspend,
1011 .resume = wdm_resume,
1012 .reset_resume = wdm_resume,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001013#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +02001014 .pre_reset = wdm_pre_reset,
1015 .post_reset = wdm_post_reset,
Oliver Neukumafba9372008-05-13 17:01:25 +02001016 .id_table = wdm_ids,
Oliver Neukum17d80d52008-06-24 15:56:10 +02001017 .supports_autosuspend = 1,
Oliver Neukumafba9372008-05-13 17:01:25 +02001018};
1019
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001020module_usb_driver(wdm_driver);
Oliver Neukumafba9372008-05-13 17:01:25 +02001021
1022MODULE_AUTHOR(DRIVER_AUTHOR);
Oliver Neukum87d65e52008-06-19 14:20:18 +02001023MODULE_DESCRIPTION(DRIVER_DESC);
Oliver Neukumafba9372008-05-13 17:01:25 +02001024MODULE_LICENSE("GPL");