blob: 9dd51f7fb64b0561678125f4b283b320d9636641 [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 Neukum5c6142c2013-03-12 14:52:42 +010084#define WDM_OVERFLOW 10
Oliver Neukumafba9372008-05-13 17:01:25 +020085
86#define WDM_MAX 16
87
Bjørn Mork7e3054a2012-01-20 01:49:57 +010088/* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
89#define WDM_DEFAULT_BUFSIZE 256
Oliver Neukumafba9372008-05-13 17:01:25 +020090
91static DEFINE_MUTEX(wdm_mutex);
Bjørn Morkb0c13862012-03-06 17:29:21 +010092static DEFINE_SPINLOCK(wdm_device_list_lock);
93static LIST_HEAD(wdm_device_list);
Oliver Neukumafba9372008-05-13 17:01:25 +020094
95/* --- method tables --- */
96
97struct wdm_device {
98 u8 *inbuf; /* buffer for response */
99 u8 *outbuf; /* buffer for command */
100 u8 *sbuf; /* buffer for status */
101 u8 *ubuf; /* buffer for copy to user space */
102
103 struct urb *command;
104 struct urb *response;
105 struct urb *validity;
106 struct usb_interface *intf;
107 struct usb_ctrlrequest *orq;
108 struct usb_ctrlrequest *irq;
109 spinlock_t iuspin;
110
111 unsigned long flags;
112 u16 bufsize;
113 u16 wMaxCommand;
114 u16 wMaxPacketSize;
Oliver Neukumafba9372008-05-13 17:01:25 +0200115 __le16 inum;
116 int reslength;
117 int length;
118 int read;
119 int count;
120 dma_addr_t shandle;
121 dma_addr_t ihandle;
Bjørn Morke8537bd2012-01-16 12:41:48 +0100122 struct mutex wlock;
123 struct mutex rlock;
Oliver Neukumafba9372008-05-13 17:01:25 +0200124 wait_queue_head_t wait;
125 struct work_struct rxwork;
126 int werr;
127 int rerr;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100128
129 struct list_head device_list;
Bjørn Mork3cc36152012-03-06 17:29:22 +0100130 int (*manage_power)(struct usb_interface *, int);
Oliver Neukumafba9372008-05-13 17:01:25 +0200131};
132
133static struct usb_driver wdm_driver;
134
Bjørn Morkb0c13862012-03-06 17:29:21 +0100135/* return intfdata if we own the interface, else look up intf in the list */
136static struct wdm_device *wdm_find_device(struct usb_interface *intf)
137{
Bjørn Mork5527f132012-09-10 22:17:34 +0200138 struct wdm_device *desc;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100139
140 spin_lock(&wdm_device_list_lock);
141 list_for_each_entry(desc, &wdm_device_list, device_list)
142 if (desc->intf == intf)
Bjørn Mork5527f132012-09-10 22:17:34 +0200143 goto found;
144 desc = NULL;
145found:
Bjørn Morkb0c13862012-03-06 17:29:21 +0100146 spin_unlock(&wdm_device_list_lock);
147
148 return desc;
149}
150
151static struct wdm_device *wdm_find_device_by_minor(int minor)
152{
Bjørn Mork5527f132012-09-10 22:17:34 +0200153 struct wdm_device *desc;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100154
155 spin_lock(&wdm_device_list_lock);
156 list_for_each_entry(desc, &wdm_device_list, device_list)
157 if (desc->intf->minor == minor)
Bjørn Mork5527f132012-09-10 22:17:34 +0200158 goto found;
159 desc = NULL;
160found:
Bjørn Morkb0c13862012-03-06 17:29:21 +0100161 spin_unlock(&wdm_device_list_lock);
162
163 return desc;
164}
165
Oliver Neukumafba9372008-05-13 17:01:25 +0200166/* --- callbacks --- */
167static void wdm_out_callback(struct urb *urb)
168{
169 struct wdm_device *desc;
170 desc = urb->context;
171 spin_lock(&desc->iuspin);
172 desc->werr = urb->status;
173 spin_unlock(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200174 kfree(desc->outbuf);
Oliver Neukum5c228372012-04-26 21:59:10 +0200175 desc->outbuf = NULL;
176 clear_bit(WDM_IN_USE, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200177 wake_up(&desc->wait);
178}
179
180static void wdm_in_callback(struct urb *urb)
181{
182 struct wdm_device *desc = urb->context;
183 int status = urb->status;
Oliver Neukum5c6142c2013-03-12 14:52:42 +0100184 int length = urb->actual_length;
Oliver Neukumafba9372008-05-13 17:01:25 +0200185
186 spin_lock(&desc->iuspin);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100187 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200188
189 if (status) {
190 switch (status) {
191 case -ENOENT:
192 dev_dbg(&desc->intf->dev,
193 "nonzero urb status received: -ENOENT");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100194 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200195 case -ECONNRESET:
196 dev_dbg(&desc->intf->dev,
197 "nonzero urb status received: -ECONNRESET");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100198 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200199 case -ESHUTDOWN:
200 dev_dbg(&desc->intf->dev,
201 "nonzero urb status received: -ESHUTDOWN");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100202 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200203 case -EPIPE:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700204 dev_err(&desc->intf->dev,
205 "nonzero urb status received: -EPIPE\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200206 break;
207 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700208 dev_err(&desc->intf->dev,
209 "Unexpected error %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200210 break;
211 }
212 }
213
214 desc->rerr = status;
Oliver Neukum5c6142c2013-03-12 14:52:42 +0100215 if (length + desc->length > desc->wMaxCommand) {
216 /* The buffer would overflow */
217 set_bit(WDM_OVERFLOW, &desc->flags);
218 } else {
219 /* we may already be in overflow */
220 if (!test_bit(WDM_OVERFLOW, &desc->flags)) {
221 memmove(desc->ubuf + desc->length, desc->inbuf, length);
222 desc->length += length;
223 desc->reslength = length;
224 }
225 }
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100226skip_error:
Oliver Neukumafba9372008-05-13 17:01:25 +0200227 wake_up(&desc->wait);
228
229 set_bit(WDM_READ, &desc->flags);
230 spin_unlock(&desc->iuspin);
231}
232
233static void wdm_int_callback(struct urb *urb)
234{
235 int rv = 0;
236 int status = urb->status;
237 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200238 struct usb_cdc_notification *dr;
239
240 desc = urb->context;
Oliver Neukumafba9372008-05-13 17:01:25 +0200241 dr = (struct usb_cdc_notification *)desc->sbuf;
242
243 if (status) {
244 switch (status) {
245 case -ESHUTDOWN:
246 case -ENOENT:
247 case -ECONNRESET:
248 return; /* unplug */
249 case -EPIPE:
250 set_bit(WDM_INT_STALL, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700251 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200252 goto sw; /* halt is cleared in work */
253 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700254 dev_err(&desc->intf->dev,
255 "nonzero urb status received: %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200256 break;
257 }
258 }
259
260 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700261 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
262 urb->actual_length);
Oliver Neukumafba9372008-05-13 17:01:25 +0200263 goto exit;
264 }
265
266 switch (dr->bNotificationType) {
267 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
268 dev_dbg(&desc->intf->dev,
269 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
270 dr->wIndex, dr->wLength);
271 break;
272
273 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
274
275 dev_dbg(&desc->intf->dev,
276 "NOTIFY_NETWORK_CONNECTION %s network",
277 dr->wValue ? "connected to" : "disconnected from");
278 goto exit;
279 default:
280 clear_bit(WDM_POLL_RUNNING, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700281 dev_err(&desc->intf->dev,
282 "unknown notification %d received: index %d len %d\n",
Oliver Neukumafba9372008-05-13 17:01:25 +0200283 dr->bNotificationType, dr->wIndex, dr->wLength);
284 goto exit;
285 }
286
Oliver Neukumafba9372008-05-13 17:01:25 +0200287 spin_lock(&desc->iuspin);
288 clear_bit(WDM_READ, &desc->flags);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100289 set_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100290 if (!test_bit(WDM_DISCONNECTING, &desc->flags)
291 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200292 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
293 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
294 __func__, rv);
295 }
296 spin_unlock(&desc->iuspin);
297 if (rv < 0) {
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100298 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200299 if (rv == -EPERM)
300 return;
301 if (rv == -ENOMEM) {
302sw:
303 rv = schedule_work(&desc->rxwork);
304 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700305 dev_err(&desc->intf->dev,
306 "Cannot schedule work\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200307 }
308 }
309exit:
310 rv = usb_submit_urb(urb, GFP_ATOMIC);
311 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700312 dev_err(&desc->intf->dev,
313 "%s - usb_submit_urb failed with result %d\n",
314 __func__, rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200315
316}
317
318static void kill_urbs(struct wdm_device *desc)
319{
Oliver Neukum17d80d52008-06-24 15:56:10 +0200320 /* the order here is essential */
Oliver Neukumafba9372008-05-13 17:01:25 +0200321 usb_kill_urb(desc->command);
322 usb_kill_urb(desc->validity);
323 usb_kill_urb(desc->response);
324}
325
326static void free_urbs(struct wdm_device *desc)
327{
328 usb_free_urb(desc->validity);
329 usb_free_urb(desc->response);
330 usb_free_urb(desc->command);
331}
332
333static void cleanup(struct wdm_device *desc)
334{
Bjørn Mork8457d992012-01-16 15:12:00 +0100335 kfree(desc->sbuf);
336 kfree(desc->inbuf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200337 kfree(desc->orq);
338 kfree(desc->irq);
339 kfree(desc->ubuf);
340 free_urbs(desc);
341 kfree(desc);
342}
343
344static ssize_t wdm_write
345(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
346{
347 u8 *buf;
348 int rv = -EMSGSIZE, r, we;
349 struct wdm_device *desc = file->private_data;
350 struct usb_ctrlrequest *req;
351
352 if (count > desc->wMaxCommand)
353 count = desc->wMaxCommand;
354
355 spin_lock_irq(&desc->iuspin);
356 we = desc->werr;
357 desc->werr = 0;
358 spin_unlock_irq(&desc->iuspin);
359 if (we < 0)
360 return -EIO;
361
Oliver Neukum5c228372012-04-26 21:59:10 +0200362 buf = kmalloc(count, GFP_KERNEL);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100363 if (!buf) {
364 rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200365 goto outnl;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100366 }
367
368 r = copy_from_user(buf, buffer, count);
369 if (r > 0) {
370 kfree(buf);
371 rv = -EFAULT;
372 goto outnl;
373 }
374
375 /* concurrent writes and disconnect */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100376 r = mutex_lock_interruptible(&desc->wlock);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100377 rv = -ERESTARTSYS;
378 if (r) {
379 kfree(buf);
380 goto outnl;
381 }
382
383 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
384 kfree(buf);
385 rv = -ENODEV;
386 goto outnp;
387 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200388
Oliver Neukum17d80d52008-06-24 15:56:10 +0200389 r = usb_autopm_get_interface(desc->intf);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100390 if (r < 0) {
391 kfree(buf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200392 goto outnp;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100393 }
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200394
David Sterba0cdfb812010-12-27 18:49:58 +0100395 if (!(file->f_flags & O_NONBLOCK))
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200396 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
397 &desc->flags));
398 else
399 if (test_bit(WDM_IN_USE, &desc->flags))
400 r = -EAGAIN;
Bjørn Mork88044202012-02-10 09:44:08 +0100401
402 if (test_bit(WDM_RESETTING, &desc->flags))
403 r = -EIO;
404
Oliver Neukum860e41a2010-02-27 20:54:24 +0100405 if (r < 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200406 kfree(buf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200407 goto out;
408 }
409
410 req = desc->orq;
411 usb_fill_control_urb(
412 desc->command,
413 interface_to_usbdev(desc->intf),
414 /* using common endpoint 0 */
415 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
416 (unsigned char *)req,
417 buf,
418 count,
419 wdm_out_callback,
420 desc
421 );
422
423 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
424 USB_RECIP_INTERFACE);
425 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
426 req->wValue = 0;
427 req->wIndex = desc->inum;
428 req->wLength = cpu_to_le16(count);
429 set_bit(WDM_IN_USE, &desc->flags);
Oliver Neukum5c228372012-04-26 21:59:10 +0200430 desc->outbuf = buf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200431
432 rv = usb_submit_urb(desc->command, GFP_KERNEL);
433 if (rv < 0) {
434 kfree(buf);
Oliver Neukum5c228372012-04-26 21:59:10 +0200435 desc->outbuf = NULL;
Oliver Neukumafba9372008-05-13 17:01:25 +0200436 clear_bit(WDM_IN_USE, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700437 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200438 } else {
439 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
440 req->wIndex);
441 }
442out:
Oliver Neukum17d80d52008-06-24 15:56:10 +0200443 usb_autopm_put_interface(desc->intf);
444outnp:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100445 mutex_unlock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200446outnl:
447 return rv < 0 ? rv : count;
448}
449
450static ssize_t wdm_read
451(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
452{
Ben Hutchings711c68b2012-02-12 06:00:41 +0000453 int rv, cntr;
Oliver Neukumafba9372008-05-13 17:01:25 +0200454 int i = 0;
455 struct wdm_device *desc = file->private_data;
456
457
Bjørn Morke8537bd2012-01-16 12:41:48 +0100458 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
Oliver Neukumafba9372008-05-13 17:01:25 +0200459 if (rv < 0)
460 return -ERESTARTSYS;
461
Ben Hutchings711c68b2012-02-12 06:00:41 +0000462 cntr = ACCESS_ONCE(desc->length);
463 if (cntr == 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200464 desc->read = 0;
465retry:
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200466 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
467 rv = -ENODEV;
468 goto err;
469 }
Oliver Neukum5c6142c2013-03-12 14:52:42 +0100470 if (test_bit(WDM_OVERFLOW, &desc->flags)) {
471 clear_bit(WDM_OVERFLOW, &desc->flags);
472 rv = -ENOBUFS;
473 goto err;
474 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200475 i++;
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200476 if (file->f_flags & O_NONBLOCK) {
477 if (!test_bit(WDM_READ, &desc->flags)) {
478 rv = cntr ? cntr : -EAGAIN;
479 goto err;
480 }
481 rv = 0;
482 } else {
483 rv = wait_event_interruptible(desc->wait,
484 test_bit(WDM_READ, &desc->flags));
485 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200486
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200487 /* may have happened while we slept */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200488 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
489 rv = -ENODEV;
490 goto err;
491 }
Bjørn Mork88044202012-02-10 09:44:08 +0100492 if (test_bit(WDM_RESETTING, &desc->flags)) {
493 rv = -EIO;
494 goto err;
495 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200496 usb_mark_last_busy(interface_to_usbdev(desc->intf));
Oliver Neukumafba9372008-05-13 17:01:25 +0200497 if (rv < 0) {
498 rv = -ERESTARTSYS;
499 goto err;
500 }
501
502 spin_lock_irq(&desc->iuspin);
503
504 if (desc->rerr) { /* read completed, error happened */
Oliver Neukumafba9372008-05-13 17:01:25 +0200505 desc->rerr = 0;
506 spin_unlock_irq(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200507 rv = -EIO;
508 goto err;
509 }
510 /*
511 * recheck whether we've lost the race
512 * against the completion handler
513 */
514 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
515 spin_unlock_irq(&desc->iuspin);
516 goto retry;
517 }
Oliver Neukum5c6142c2013-03-12 14:52:42 +0100518
Oliver Neukumafba9372008-05-13 17:01:25 +0200519 if (!desc->reslength) { /* zero length read */
Bjørn Morkb80f6db2012-07-02 10:33:14 +0200520 dev_dbg(&desc->intf->dev, "%s: zero length - clearing WDM_READ\n", __func__);
521 clear_bit(WDM_READ, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200522 spin_unlock_irq(&desc->iuspin);
523 goto retry;
524 }
Ben Hutchings711c68b2012-02-12 06:00:41 +0000525 cntr = desc->length;
Oliver Neukumafba9372008-05-13 17:01:25 +0200526 spin_unlock_irq(&desc->iuspin);
527 }
528
Ben Hutchings711c68b2012-02-12 06:00:41 +0000529 if (cntr > count)
530 cntr = count;
Oliver Neukumafba9372008-05-13 17:01:25 +0200531 rv = copy_to_user(buffer, desc->ubuf, cntr);
532 if (rv > 0) {
533 rv = -EFAULT;
534 goto err;
535 }
536
Ben Hutchings711c68b2012-02-12 06:00:41 +0000537 spin_lock_irq(&desc->iuspin);
538
Oliver Neukumafba9372008-05-13 17:01:25 +0200539 for (i = 0; i < desc->length - cntr; i++)
540 desc->ubuf[i] = desc->ubuf[i + cntr];
541
542 desc->length -= cntr;
Oliver Neukum87d65e52008-06-19 14:20:18 +0200543 /* in case we had outstanding data */
544 if (!desc->length)
545 clear_bit(WDM_READ, &desc->flags);
Ben Hutchings711c68b2012-02-12 06:00:41 +0000546
547 spin_unlock_irq(&desc->iuspin);
548
Oliver Neukumafba9372008-05-13 17:01:25 +0200549 rv = cntr;
550
551err:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100552 mutex_unlock(&desc->rlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200553 return rv;
554}
555
556static int wdm_flush(struct file *file, fl_owner_t id)
557{
558 struct wdm_device *desc = file->private_data;
559
560 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
Bjørn Mork577bd3e2012-05-09 13:53:22 +0200561
562 /* cannot dereference desc->intf if WDM_DISCONNECTING */
563 if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags))
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700564 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
565 desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200566
Oliver Neukumeae68612012-04-27 14:23:54 +0200567 return usb_translate_errors(desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200568}
569
570static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
571{
572 struct wdm_device *desc = file->private_data;
573 unsigned long flags;
574 unsigned int mask = 0;
575
576 spin_lock_irqsave(&desc->iuspin, flags);
577 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
Bjørn Morkd0765ea2012-05-09 13:53:21 +0200578 mask = POLLHUP | POLLERR;
Oliver Neukumafba9372008-05-13 17:01:25 +0200579 spin_unlock_irqrestore(&desc->iuspin, flags);
580 goto desc_out;
581 }
582 if (test_bit(WDM_READ, &desc->flags))
583 mask = POLLIN | POLLRDNORM;
584 if (desc->rerr || desc->werr)
585 mask |= POLLERR;
586 if (!test_bit(WDM_IN_USE, &desc->flags))
587 mask |= POLLOUT | POLLWRNORM;
588 spin_unlock_irqrestore(&desc->iuspin, flags);
589
590 poll_wait(file, &desc->wait, wait);
591
592desc_out:
593 return mask;
594}
595
596static int wdm_open(struct inode *inode, struct file *file)
597{
598 int minor = iminor(inode);
599 int rv = -ENODEV;
600 struct usb_interface *intf;
601 struct wdm_device *desc;
602
603 mutex_lock(&wdm_mutex);
Bjørn Morkb0c13862012-03-06 17:29:21 +0100604 desc = wdm_find_device_by_minor(minor);
605 if (!desc)
Oliver Neukumafba9372008-05-13 17:01:25 +0200606 goto out;
607
Bjørn Morkb0c13862012-03-06 17:29:21 +0100608 intf = desc->intf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200609 if (test_bit(WDM_DISCONNECTING, &desc->flags))
610 goto out;
Oliver Neukumafba9372008-05-13 17:01:25 +0200611 file->private_data = desc;
612
Oliver Neukum17d80d52008-06-24 15:56:10 +0200613 rv = usb_autopm_get_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200614 if (rv < 0) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700615 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200616 goto out;
617 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200618
Bjørn Morke8537bd2012-01-16 12:41:48 +0100619 /* using write lock to protect desc->count */
620 mutex_lock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200621 if (!desc->count++) {
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200622 desc->werr = 0;
623 desc->rerr = 0;
Oliver Neukum17d80d52008-06-24 15:56:10 +0200624 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
625 if (rv < 0) {
626 desc->count--;
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700627 dev_err(&desc->intf->dev,
628 "Error submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200629 }
630 } else {
631 rv = 0;
632 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100633 mutex_unlock(&desc->wlock);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100634 if (desc->count == 1)
635 desc->manage_power(intf, 1);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200636 usb_autopm_put_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200637out:
638 mutex_unlock(&wdm_mutex);
639 return rv;
640}
641
642static int wdm_release(struct inode *inode, struct file *file)
643{
644 struct wdm_device *desc = file->private_data;
645
646 mutex_lock(&wdm_mutex);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100647
648 /* using write lock to protect desc->count */
649 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200650 desc->count--;
Bjørn Morke8537bd2012-01-16 12:41:48 +0100651 mutex_unlock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200652
Oliver Neukumafba9372008-05-13 17:01:25 +0200653 if (!desc->count) {
Bjørn Mork577bd3e2012-05-09 13:53:22 +0200654 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
655 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
656 kill_urbs(desc);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100657 desc->manage_power(desc->intf, 0);
Bjørn Mork577bd3e2012-05-09 13:53:22 +0200658 } else {
659 /* must avoid dev_printk here as desc->intf is invalid */
660 pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
Oliver Neukumfccfda62012-04-27 14:36:37 +0200661 cleanup(desc);
Bjørn Mork577bd3e2012-05-09 13:53:22 +0200662 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200663 }
664 mutex_unlock(&wdm_mutex);
665 return 0;
666}
667
668static const struct file_operations wdm_fops = {
669 .owner = THIS_MODULE,
670 .read = wdm_read,
671 .write = wdm_write,
672 .open = wdm_open,
673 .flush = wdm_flush,
674 .release = wdm_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200675 .poll = wdm_poll,
676 .llseek = noop_llseek,
Oliver Neukumafba9372008-05-13 17:01:25 +0200677};
678
679static struct usb_class_driver wdm_class = {
680 .name = "cdc-wdm%d",
681 .fops = &wdm_fops,
682 .minor_base = WDM_MINOR_BASE,
683};
684
685/* --- error handling --- */
686static void wdm_rxwork(struct work_struct *work)
687{
688 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
689 unsigned long flags;
690 int rv;
691
692 spin_lock_irqsave(&desc->iuspin, flags);
693 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
694 spin_unlock_irqrestore(&desc->iuspin, flags);
695 } else {
696 spin_unlock_irqrestore(&desc->iuspin, flags);
697 rv = usb_submit_urb(desc->response, GFP_KERNEL);
698 if (rv < 0 && rv != -EPERM) {
699 spin_lock_irqsave(&desc->iuspin, flags);
700 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
701 schedule_work(&desc->rxwork);
702 spin_unlock_irqrestore(&desc->iuspin, flags);
703 }
704 }
705}
706
707/* --- hotplug --- */
708
Bjørn Mork3cc36152012-03-06 17:29:22 +0100709static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
710 u16 bufsize, int (*manage_power)(struct usb_interface *, int))
Oliver Neukumafba9372008-05-13 17:01:25 +0200711{
Bjørn Mork0dffb482012-03-06 17:29:20 +0100712 int rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200713 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200714
Oliver Neukumafba9372008-05-13 17:01:25 +0200715 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
716 if (!desc)
717 goto out;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100718 INIT_LIST_HEAD(&desc->device_list);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100719 mutex_init(&desc->rlock);
720 mutex_init(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200721 spin_lock_init(&desc->iuspin);
722 init_waitqueue_head(&desc->wait);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100723 desc->wMaxCommand = bufsize;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200724 /* this will be expanded and needed in hardware endianness */
Oliver Neukumafba9372008-05-13 17:01:25 +0200725 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
726 desc->intf = intf;
727 INIT_WORK(&desc->rxwork, wdm_rxwork);
728
Oliver Neukum052fbc02009-04-20 17:24:49 +0200729 rv = -EINVAL;
Bjørn Mork0dffb482012-03-06 17:29:20 +0100730 if (!usb_endpoint_is_int_in(ep))
Oliver Neukum052fbc02009-04-20 17:24:49 +0200731 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200732
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700733 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
Oliver Neukumafba9372008-05-13 17:01:25 +0200734
735 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
736 if (!desc->orq)
737 goto err;
738 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
739 if (!desc->irq)
740 goto err;
741
742 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
743 if (!desc->validity)
744 goto err;
745
746 desc->response = usb_alloc_urb(0, GFP_KERNEL);
747 if (!desc->response)
748 goto err;
749
750 desc->command = usb_alloc_urb(0, GFP_KERNEL);
751 if (!desc->command)
752 goto err;
753
754 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
755 if (!desc->ubuf)
756 goto err;
757
Bjørn Mork8457d992012-01-16 15:12:00 +0100758 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200759 if (!desc->sbuf)
760 goto err;
761
Bjørn Mork8457d992012-01-16 15:12:00 +0100762 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200763 if (!desc->inbuf)
Bjørn Mork8457d992012-01-16 15:12:00 +0100764 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200765
766 usb_fill_int_urb(
767 desc->validity,
768 interface_to_usbdev(intf),
769 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
770 desc->sbuf,
771 desc->wMaxPacketSize,
772 wdm_int_callback,
773 desc,
774 ep->bInterval
775 );
Oliver Neukumafba9372008-05-13 17:01:25 +0200776
Bjørn Mork19b85b32012-01-16 15:11:58 +0100777 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
778 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
779 desc->irq->wValue = 0;
780 desc->irq->wIndex = desc->inum;
781 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
782
783 usb_fill_control_urb(
784 desc->response,
Bjørn Mork8143a892012-01-16 15:12:01 +0100785 interface_to_usbdev(intf),
Bjørn Mork19b85b32012-01-16 15:11:58 +0100786 /* using common endpoint 0 */
787 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
788 (unsigned char *)desc->irq,
789 desc->inbuf,
790 desc->wMaxCommand,
791 wdm_in_callback,
792 desc
793 );
Bjørn Mork19b85b32012-01-16 15:11:58 +0100794
Bjørn Mork3cc36152012-03-06 17:29:22 +0100795 desc->manage_power = manage_power;
796
Bjørn Morkb0c13862012-03-06 17:29:21 +0100797 spin_lock(&wdm_device_list_lock);
798 list_add(&desc->device_list, &wdm_device_list);
799 spin_unlock(&wdm_device_list_lock);
800
Oliver Neukumafba9372008-05-13 17:01:25 +0200801 rv = usb_register_dev(intf, &wdm_class);
Oliver Neukumafba9372008-05-13 17:01:25 +0200802 if (rv < 0)
Bjørn Morkb0c13862012-03-06 17:29:21 +0100803 goto err;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200804 else
Bjørn Mork820c6292012-01-20 04:17:25 +0100805 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
Oliver Neukumafba9372008-05-13 17:01:25 +0200806out:
807 return rv;
Oliver Neukumafba9372008-05-13 17:01:25 +0200808err:
Bjørn Mork9ed2cb72012-05-09 13:53:23 +0200809 spin_lock(&wdm_device_list_lock);
810 list_del(&desc->device_list);
811 spin_unlock(&wdm_device_list_lock);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100812 cleanup(desc);
813 return rv;
814}
815
Bjørn Mork3cc36152012-03-06 17:29:22 +0100816static int wdm_manage_power(struct usb_interface *intf, int on)
817{
818 /* need autopm_get/put here to ensure the usbcore sees the new value */
819 int rv = usb_autopm_get_interface(intf);
820 if (rv < 0)
821 goto err;
822
823 intf->needs_remote_wakeup = on;
824 usb_autopm_put_interface(intf);
825err:
826 return rv;
827}
828
Bjørn Mork0dffb482012-03-06 17:29:20 +0100829static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
830{
831 int rv = -EINVAL;
832 struct usb_host_interface *iface;
833 struct usb_endpoint_descriptor *ep;
834 struct usb_cdc_dmm_desc *dmhd;
835 u8 *buffer = intf->altsetting->extra;
836 int buflen = intf->altsetting->extralen;
837 u16 maxcom = WDM_DEFAULT_BUFSIZE;
838
839 if (!buffer)
840 goto err;
841 while (buflen > 2) {
842 if (buffer[1] != USB_DT_CS_INTERFACE) {
843 dev_err(&intf->dev, "skipping garbage\n");
844 goto next_desc;
845 }
846
847 switch (buffer[2]) {
848 case USB_CDC_HEADER_TYPE:
849 break;
850 case USB_CDC_DMM_TYPE:
851 dmhd = (struct usb_cdc_dmm_desc *)buffer;
852 maxcom = le16_to_cpu(dmhd->wMaxCommand);
853 dev_dbg(&intf->dev,
854 "Finding maximum buffer length: %d", maxcom);
855 break;
856 default:
857 dev_err(&intf->dev,
858 "Ignoring extra header, type %d, length %d\n",
859 buffer[2], buffer[0]);
860 break;
861 }
862next_desc:
863 buflen -= buffer[0];
864 buffer += buffer[0];
865 }
866
867 iface = intf->cur_altsetting;
868 if (iface->desc.bNumEndpoints != 1)
869 goto err;
870 ep = &iface->endpoint[0].desc;
871
Bjørn Mork3cc36152012-03-06 17:29:22 +0100872 rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100873
874err:
Oliver Neukumafba9372008-05-13 17:01:25 +0200875 return rv;
876}
877
Bjørn Mork3cc36152012-03-06 17:29:22 +0100878/**
879 * usb_cdc_wdm_register - register a WDM subdriver
880 * @intf: usb interface the subdriver will associate with
881 * @ep: interrupt endpoint to monitor for notifications
882 * @bufsize: maximum message size to support for read/write
883 *
884 * Create WDM usb class character device and associate it with intf
885 * without binding, allowing another driver to manage the interface.
886 *
887 * The subdriver will manage the given interrupt endpoint exclusively
888 * and will issue control requests referring to the given intf. It
889 * will otherwise avoid interferring, and in particular not do
890 * usb_set_intfdata/usb_get_intfdata on intf.
891 *
892 * The return value is a pointer to the subdriver's struct usb_driver.
893 * The registering driver is responsible for calling this subdriver's
894 * disconnect, suspend, resume, pre_reset and post_reset methods from
895 * its own.
896 */
897struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
898 struct usb_endpoint_descriptor *ep,
899 int bufsize,
900 int (*manage_power)(struct usb_interface *, int))
901{
902 int rv = -EINVAL;
903
904 rv = wdm_create(intf, ep, bufsize, manage_power);
905 if (rv < 0)
906 goto err;
907
908 return &wdm_driver;
909err:
910 return ERR_PTR(rv);
911}
912EXPORT_SYMBOL(usb_cdc_wdm_register);
913
Oliver Neukumafba9372008-05-13 17:01:25 +0200914static void wdm_disconnect(struct usb_interface *intf)
915{
916 struct wdm_device *desc;
917 unsigned long flags;
918
919 usb_deregister_dev(intf, &wdm_class);
Bjørn Morkb0c13862012-03-06 17:29:21 +0100920 desc = wdm_find_device(intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200921 mutex_lock(&wdm_mutex);
Oliver Neukumafba9372008-05-13 17:01:25 +0200922
923 /* the spinlock makes sure no new urbs are generated in the callbacks */
924 spin_lock_irqsave(&desc->iuspin, flags);
925 set_bit(WDM_DISCONNECTING, &desc->flags);
926 set_bit(WDM_READ, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200927 /* to terminate pending flushes */
Oliver Neukumafba9372008-05-13 17:01:25 +0200928 clear_bit(WDM_IN_USE, &desc->flags);
929 spin_unlock_irqrestore(&desc->iuspin, flags);
Bjørn Mork62aaf242012-01-16 15:11:57 +0100930 wake_up_all(&desc->wait);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100931 mutex_lock(&desc->rlock);
932 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200933 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100934 cancel_work_sync(&desc->rxwork);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100935 mutex_unlock(&desc->wlock);
936 mutex_unlock(&desc->rlock);
Bjørn Mork9ed2cb72012-05-09 13:53:23 +0200937
938 /* the desc->intf pointer used as list key is now invalid */
939 spin_lock(&wdm_device_list_lock);
940 list_del(&desc->device_list);
941 spin_unlock(&wdm_device_list_lock);
942
Oliver Neukumafba9372008-05-13 17:01:25 +0200943 if (!desc->count)
944 cleanup(desc);
945 mutex_unlock(&wdm_mutex);
946}
947
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100948#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200949static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
950{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100951 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200952 int rv = 0;
953
954 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
955
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100956 /* if this is an autosuspend the caller does the locking */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100957 if (!PMSG_IS_AUTO(message)) {
958 mutex_lock(&desc->rlock);
959 mutex_lock(&desc->wlock);
960 }
Oliver Neukum62e66852010-02-27 20:56:22 +0100961 spin_lock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100962
Alan Stern5b1b0b82011-08-19 23:49:48 +0200963 if (PMSG_IS_AUTO(message) &&
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100964 (test_bit(WDM_IN_USE, &desc->flags)
965 || test_bit(WDM_RESPONDING, &desc->flags))) {
Oliver Neukum62e66852010-02-27 20:56:22 +0100966 spin_unlock_irq(&desc->iuspin);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200967 rv = -EBUSY;
968 } else {
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100969
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100970 set_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100971 spin_unlock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100972 /* callback submits work - order is essential */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200973 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100974 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200975 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100976 if (!PMSG_IS_AUTO(message)) {
977 mutex_unlock(&desc->wlock);
978 mutex_unlock(&desc->rlock);
979 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200980
981 return rv;
982}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100983#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200984
985static int recover_from_urb_loss(struct wdm_device *desc)
986{
987 int rv = 0;
988
989 if (desc->count) {
990 rv = usb_submit_urb(desc->validity, GFP_NOIO);
991 if (rv < 0)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700992 dev_err(&desc->intf->dev,
993 "Error resume submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200994 }
995 return rv;
996}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100997
998#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200999static int wdm_resume(struct usb_interface *intf)
1000{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001001 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001002 int rv;
1003
1004 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
Oliver Neukum338124c2010-02-27 20:57:12 +01001005
Oliver Neukumbeb1d352010-02-27 20:55:52 +01001006 clear_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +01001007 rv = recover_from_urb_loss(desc);
Oliver Neukum338124c2010-02-27 20:57:12 +01001008
Oliver Neukum17d80d52008-06-24 15:56:10 +02001009 return rv;
1010}
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001011#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +02001012
1013static int wdm_pre_reset(struct usb_interface *intf)
1014{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001015 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001016
Oliver Neukumd771d8a2011-04-29 14:12:21 +02001017 /*
1018 * we notify everybody using poll of
1019 * an exceptional situation
1020 * must be done before recovery lest a spontaneous
1021 * message from the device is lost
1022 */
1023 spin_lock_irq(&desc->iuspin);
Bjørn Mork88044202012-02-10 09:44:08 +01001024 set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
1025 set_bit(WDM_READ, &desc->flags); /* unblock read */
1026 clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
Oliver Neukumd771d8a2011-04-29 14:12:21 +02001027 desc->rerr = -EINTR;
1028 spin_unlock_irq(&desc->iuspin);
1029 wake_up_all(&desc->wait);
Bjørn Mork88044202012-02-10 09:44:08 +01001030 mutex_lock(&desc->rlock);
1031 mutex_lock(&desc->wlock);
1032 kill_urbs(desc);
1033 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001034 return 0;
1035}
1036
1037static int wdm_post_reset(struct usb_interface *intf)
1038{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001039 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001040 int rv;
1041
Oliver Neukum5c6142c2013-03-12 14:52:42 +01001042 clear_bit(WDM_OVERFLOW, &desc->flags);
Bjørn Mork88044202012-02-10 09:44:08 +01001043 clear_bit(WDM_RESETTING, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001044 rv = recover_from_urb_loss(desc);
Bjørn Morke8537bd2012-01-16 12:41:48 +01001045 mutex_unlock(&desc->wlock);
1046 mutex_unlock(&desc->rlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001047 return 0;
1048}
1049
Oliver Neukumafba9372008-05-13 17:01:25 +02001050static struct usb_driver wdm_driver = {
1051 .name = "cdc_wdm",
1052 .probe = wdm_probe,
1053 .disconnect = wdm_disconnect,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001054#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +02001055 .suspend = wdm_suspend,
1056 .resume = wdm_resume,
1057 .reset_resume = wdm_resume,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001058#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +02001059 .pre_reset = wdm_pre_reset,
1060 .post_reset = wdm_post_reset,
Oliver Neukumafba9372008-05-13 17:01:25 +02001061 .id_table = wdm_ids,
Oliver Neukum17d80d52008-06-24 15:56:10 +02001062 .supports_autosuspend = 1,
Oliver Neukumafba9372008-05-13 17:01:25 +02001063};
1064
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001065module_usb_driver(wdm_driver);
Oliver Neukumafba9372008-05-13 17:01:25 +02001066
1067MODULE_AUTHOR(DRIVER_AUTHOR);
Oliver Neukum87d65e52008-06-19 14:20:18 +02001068MODULE_DESCRIPTION(DRIVER_DESC);
Oliver Neukumafba9372008-05-13 17:01:25 +02001069MODULE_LICENSE("GPL");