blob: d3318a0df8ee503f4f5ee553cda3ff1391298be4 [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>
Bjørn Mork3edce1c2013-03-17 21:00:06 +010016#include <linux/ioctl.h>
Oliver Neukumafba9372008-05-13 17:01:25 +020017#include <linux/slab.h>
18#include <linux/module.h>
Oliver Neukumafba9372008-05-13 17:01:25 +020019#include <linux/mutex.h>
20#include <linux/uaccess.h>
21#include <linux/bitops.h>
22#include <linux/poll.h>
23#include <linux/usb.h>
24#include <linux/usb/cdc.h>
25#include <asm/byteorder.h>
26#include <asm/unaligned.h>
Bjørn Mork3cc36152012-03-06 17:29:22 +010027#include <linux/usb/cdc-wdm.h>
Oliver Neukumafba9372008-05-13 17:01:25 +020028
29/*
30 * Version Information
31 */
Oliver Neukum87d65e52008-06-19 14:20:18 +020032#define DRIVER_VERSION "v0.03"
Oliver Neukumafba9372008-05-13 17:01:25 +020033#define DRIVER_AUTHOR "Oliver Neukum"
Oliver Neukum87d65e52008-06-19 14:20:18 +020034#define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management"
Oliver Neukumafba9372008-05-13 17:01:25 +020035
Németh Márton6ef48522010-01-10 15:33:45 +010036static const struct usb_device_id wdm_ids[] = {
Oliver Neukumafba9372008-05-13 17:01:25 +020037 {
38 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
39 USB_DEVICE_ID_MATCH_INT_SUBCLASS,
40 .bInterfaceClass = USB_CLASS_COMM,
41 .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM
42 },
43 { }
44};
45
Oliver Neukumaa5380b2008-10-13 14:05:20 +020046MODULE_DEVICE_TABLE (usb, wdm_ids);
47
Oliver Neukumafba9372008-05-13 17:01:25 +020048#define WDM_MINOR_BASE 176
49
50
51#define WDM_IN_USE 1
52#define WDM_DISCONNECTING 2
53#define WDM_RESULT 3
54#define WDM_READ 4
55#define WDM_INT_STALL 5
56#define WDM_POLL_RUNNING 6
Oliver Neukum922a5ea2010-02-27 20:54:59 +010057#define WDM_RESPONDING 7
Oliver Neukumbeb1d352010-02-27 20:55:52 +010058#define WDM_SUSPENDING 8
Bjørn Mork88044202012-02-10 09:44:08 +010059#define WDM_RESETTING 9
Oliver Neukumc0f5ece2013-03-12 14:52:42 +010060#define WDM_OVERFLOW 10
Oliver Neukumafba9372008-05-13 17:01:25 +020061
62#define WDM_MAX 16
63
Bjørn Mork7e3054a2012-01-20 01:49:57 +010064/* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
65#define WDM_DEFAULT_BUFSIZE 256
Oliver Neukumafba9372008-05-13 17:01:25 +020066
67static DEFINE_MUTEX(wdm_mutex);
Bjørn Morkb0c13862012-03-06 17:29:21 +010068static DEFINE_SPINLOCK(wdm_device_list_lock);
69static LIST_HEAD(wdm_device_list);
Oliver Neukumafba9372008-05-13 17:01:25 +020070
71/* --- method tables --- */
72
73struct wdm_device {
74 u8 *inbuf; /* buffer for response */
75 u8 *outbuf; /* buffer for command */
76 u8 *sbuf; /* buffer for status */
77 u8 *ubuf; /* buffer for copy to user space */
78
79 struct urb *command;
80 struct urb *response;
81 struct urb *validity;
82 struct usb_interface *intf;
83 struct usb_ctrlrequest *orq;
84 struct usb_ctrlrequest *irq;
85 spinlock_t iuspin;
86
87 unsigned long flags;
88 u16 bufsize;
89 u16 wMaxCommand;
90 u16 wMaxPacketSize;
Oliver Neukumafba9372008-05-13 17:01:25 +020091 __le16 inum;
92 int reslength;
93 int length;
94 int read;
95 int count;
96 dma_addr_t shandle;
97 dma_addr_t ihandle;
Bjørn Morke8537bd2012-01-16 12:41:48 +010098 struct mutex wlock;
99 struct mutex rlock;
Oliver Neukumafba9372008-05-13 17:01:25 +0200100 wait_queue_head_t wait;
101 struct work_struct rxwork;
102 int werr;
103 int rerr;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100104
105 struct list_head device_list;
Bjørn Mork3cc36152012-03-06 17:29:22 +0100106 int (*manage_power)(struct usb_interface *, int);
Oliver Neukumafba9372008-05-13 17:01:25 +0200107};
108
109static struct usb_driver wdm_driver;
110
Bjørn Morkb0c13862012-03-06 17:29:21 +0100111/* return intfdata if we own the interface, else look up intf in the list */
112static struct wdm_device *wdm_find_device(struct usb_interface *intf)
113{
Bjørn Mork6a448862012-09-10 22:17:34 +0200114 struct wdm_device *desc;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100115
116 spin_lock(&wdm_device_list_lock);
117 list_for_each_entry(desc, &wdm_device_list, device_list)
118 if (desc->intf == intf)
Bjørn Mork6a448862012-09-10 22:17:34 +0200119 goto found;
120 desc = NULL;
121found:
Bjørn Morkb0c13862012-03-06 17:29:21 +0100122 spin_unlock(&wdm_device_list_lock);
123
124 return desc;
125}
126
127static struct wdm_device *wdm_find_device_by_minor(int minor)
128{
Bjørn Mork6a448862012-09-10 22:17:34 +0200129 struct wdm_device *desc;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100130
131 spin_lock(&wdm_device_list_lock);
132 list_for_each_entry(desc, &wdm_device_list, device_list)
133 if (desc->intf->minor == minor)
Bjørn Mork6a448862012-09-10 22:17:34 +0200134 goto found;
135 desc = NULL;
136found:
Bjørn Morkb0c13862012-03-06 17:29:21 +0100137 spin_unlock(&wdm_device_list_lock);
138
139 return desc;
140}
141
Oliver Neukumafba9372008-05-13 17:01:25 +0200142/* --- callbacks --- */
143static void wdm_out_callback(struct urb *urb)
144{
145 struct wdm_device *desc;
146 desc = urb->context;
147 spin_lock(&desc->iuspin);
148 desc->werr = urb->status;
149 spin_unlock(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200150 kfree(desc->outbuf);
Oliver Neukum5c228372012-04-26 21:59:10 +0200151 desc->outbuf = NULL;
152 clear_bit(WDM_IN_USE, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200153 wake_up(&desc->wait);
154}
155
156static void wdm_in_callback(struct urb *urb)
157{
158 struct wdm_device *desc = urb->context;
159 int status = urb->status;
Oliver Neukumc0f5ece2013-03-12 14:52:42 +0100160 int length = urb->actual_length;
Oliver Neukumafba9372008-05-13 17:01:25 +0200161
162 spin_lock(&desc->iuspin);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100163 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200164
165 if (status) {
166 switch (status) {
167 case -ENOENT:
168 dev_dbg(&desc->intf->dev,
169 "nonzero urb status received: -ENOENT");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100170 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200171 case -ECONNRESET:
172 dev_dbg(&desc->intf->dev,
173 "nonzero urb status received: -ECONNRESET");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100174 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200175 case -ESHUTDOWN:
176 dev_dbg(&desc->intf->dev,
177 "nonzero urb status received: -ESHUTDOWN");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100178 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200179 case -EPIPE:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700180 dev_err(&desc->intf->dev,
181 "nonzero urb status received: -EPIPE\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200182 break;
183 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700184 dev_err(&desc->intf->dev,
185 "Unexpected error %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200186 break;
187 }
188 }
189
190 desc->rerr = status;
Oliver Neukumc0f5ece2013-03-12 14:52:42 +0100191 if (length + desc->length > desc->wMaxCommand) {
192 /* The buffer would overflow */
193 set_bit(WDM_OVERFLOW, &desc->flags);
194 } else {
195 /* we may already be in overflow */
196 if (!test_bit(WDM_OVERFLOW, &desc->flags)) {
197 memmove(desc->ubuf + desc->length, desc->inbuf, length);
198 desc->length += length;
199 desc->reslength = length;
200 }
201 }
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;
Oliver Neukum6dd433e2013-08-06 14:22:59 +0200212 int responding;
Oliver Neukumafba9372008-05-13 17:01:25 +0200213 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 Neukum6dd433e2013-08-06 14:22:59 +0200266 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
267 if (!responding && !test_bit(WDM_DISCONNECTING, &desc->flags)
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100268 && !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 Mork8457d992012-01-16 15:12:00 +0100312 kfree(desc->sbuf);
313 kfree(desc->inbuf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200314 kfree(desc->orq);
315 kfree(desc->irq);
316 kfree(desc->ubuf);
317 free_urbs(desc);
318 kfree(desc);
319}
320
321static ssize_t wdm_write
322(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
323{
324 u8 *buf;
325 int rv = -EMSGSIZE, r, we;
326 struct wdm_device *desc = file->private_data;
327 struct usb_ctrlrequest *req;
328
329 if (count > desc->wMaxCommand)
330 count = desc->wMaxCommand;
331
332 spin_lock_irq(&desc->iuspin);
333 we = desc->werr;
334 desc->werr = 0;
335 spin_unlock_irq(&desc->iuspin);
336 if (we < 0)
337 return -EIO;
338
Oliver Neukum5c228372012-04-26 21:59:10 +0200339 buf = kmalloc(count, GFP_KERNEL);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100340 if (!buf) {
341 rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200342 goto outnl;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100343 }
344
345 r = copy_from_user(buf, buffer, count);
346 if (r > 0) {
347 kfree(buf);
348 rv = -EFAULT;
349 goto outnl;
350 }
351
352 /* concurrent writes and disconnect */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100353 r = mutex_lock_interruptible(&desc->wlock);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100354 rv = -ERESTARTSYS;
355 if (r) {
356 kfree(buf);
357 goto outnl;
358 }
359
360 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
361 kfree(buf);
362 rv = -ENODEV;
363 goto outnp;
364 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200365
Oliver Neukum17d80d52008-06-24 15:56:10 +0200366 r = usb_autopm_get_interface(desc->intf);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100367 if (r < 0) {
368 kfree(buf);
Oliver Neukum12a98b22012-04-30 09:57:31 +0200369 rv = usb_translate_errors(r);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200370 goto outnp;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100371 }
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200372
David Sterba0cdfb812010-12-27 18:49:58 +0100373 if (!(file->f_flags & O_NONBLOCK))
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200374 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
375 &desc->flags));
376 else
377 if (test_bit(WDM_IN_USE, &desc->flags))
378 r = -EAGAIN;
Bjørn Mork88044202012-02-10 09:44:08 +0100379
380 if (test_bit(WDM_RESETTING, &desc->flags))
381 r = -EIO;
382
Oliver Neukum860e41a2010-02-27 20:54:24 +0100383 if (r < 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200384 kfree(buf);
Oliver Neukum12a98b22012-04-30 09:57:31 +0200385 rv = r;
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);
Oliver Neukum5c228372012-04-26 21:59:10 +0200409 desc->outbuf = buf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200410
411 rv = usb_submit_urb(desc->command, GFP_KERNEL);
412 if (rv < 0) {
413 kfree(buf);
Oliver Neukum5c228372012-04-26 21:59:10 +0200414 desc->outbuf = NULL;
Oliver Neukumafba9372008-05-13 17:01:25 +0200415 clear_bit(WDM_IN_USE, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700416 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
Oliver Neukum12a98b22012-04-30 09:57:31 +0200417 rv = usb_translate_errors(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 Neukumc0f5ece2013-03-12 14:52:42 +0100450 if (test_bit(WDM_OVERFLOW, &desc->flags)) {
451 clear_bit(WDM_OVERFLOW, &desc->flags);
452 rv = -ENOBUFS;
453 goto err;
454 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200455 i++;
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200456 if (file->f_flags & O_NONBLOCK) {
457 if (!test_bit(WDM_READ, &desc->flags)) {
458 rv = cntr ? cntr : -EAGAIN;
459 goto err;
460 }
461 rv = 0;
462 } else {
463 rv = wait_event_interruptible(desc->wait,
464 test_bit(WDM_READ, &desc->flags));
465 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200466
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200467 /* may have happened while we slept */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200468 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
469 rv = -ENODEV;
470 goto err;
471 }
Bjørn Mork88044202012-02-10 09:44:08 +0100472 if (test_bit(WDM_RESETTING, &desc->flags)) {
473 rv = -EIO;
474 goto err;
475 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200476 usb_mark_last_busy(interface_to_usbdev(desc->intf));
Oliver Neukumafba9372008-05-13 17:01:25 +0200477 if (rv < 0) {
478 rv = -ERESTARTSYS;
479 goto err;
480 }
481
482 spin_lock_irq(&desc->iuspin);
483
484 if (desc->rerr) { /* read completed, error happened */
Oliver Neukumafba9372008-05-13 17:01:25 +0200485 desc->rerr = 0;
486 spin_unlock_irq(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200487 rv = -EIO;
488 goto err;
489 }
490 /*
491 * recheck whether we've lost the race
492 * against the completion handler
493 */
494 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
495 spin_unlock_irq(&desc->iuspin);
496 goto retry;
497 }
Oliver Neukumc0f5ece2013-03-12 14:52:42 +0100498
Oliver Neukumafba9372008-05-13 17:01:25 +0200499 if (!desc->reslength) { /* zero length read */
Bjørn Morkb086b6b2012-07-02 10:33:14 +0200500 dev_dbg(&desc->intf->dev, "%s: zero length - clearing WDM_READ\n", __func__);
501 clear_bit(WDM_READ, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200502 spin_unlock_irq(&desc->iuspin);
503 goto retry;
504 }
Ben Hutchings711c68b2012-02-12 06:00:41 +0000505 cntr = desc->length;
Oliver Neukumafba9372008-05-13 17:01:25 +0200506 spin_unlock_irq(&desc->iuspin);
507 }
508
Ben Hutchings711c68b2012-02-12 06:00:41 +0000509 if (cntr > count)
510 cntr = count;
Oliver Neukumafba9372008-05-13 17:01:25 +0200511 rv = copy_to_user(buffer, desc->ubuf, cntr);
512 if (rv > 0) {
513 rv = -EFAULT;
514 goto err;
515 }
516
Ben Hutchings711c68b2012-02-12 06:00:41 +0000517 spin_lock_irq(&desc->iuspin);
518
Oliver Neukumafba9372008-05-13 17:01:25 +0200519 for (i = 0; i < desc->length - cntr; i++)
520 desc->ubuf[i] = desc->ubuf[i + cntr];
521
522 desc->length -= cntr;
Oliver Neukum87d65e52008-06-19 14:20:18 +0200523 /* in case we had outstanding data */
524 if (!desc->length)
525 clear_bit(WDM_READ, &desc->flags);
Ben Hutchings711c68b2012-02-12 06:00:41 +0000526
527 spin_unlock_irq(&desc->iuspin);
528
Oliver Neukumafba9372008-05-13 17:01:25 +0200529 rv = cntr;
530
531err:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100532 mutex_unlock(&desc->rlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200533 return rv;
534}
535
536static int wdm_flush(struct file *file, fl_owner_t id)
537{
538 struct wdm_device *desc = file->private_data;
539
540 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
Bjørn Mork6b0b79d2012-05-09 13:53:22 +0200541
542 /* cannot dereference desc->intf if WDM_DISCONNECTING */
543 if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags))
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700544 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
545 desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200546
Oliver Neukum24a85ba2012-04-27 14:23:54 +0200547 return usb_translate_errors(desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200548}
549
550static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
551{
552 struct wdm_device *desc = file->private_data;
553 unsigned long flags;
554 unsigned int mask = 0;
555
556 spin_lock_irqsave(&desc->iuspin, flags);
557 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
Bjørn Mork616b6932012-05-09 13:53:21 +0200558 mask = POLLHUP | POLLERR;
Oliver Neukumafba9372008-05-13 17:01:25 +0200559 spin_unlock_irqrestore(&desc->iuspin, flags);
560 goto desc_out;
561 }
562 if (test_bit(WDM_READ, &desc->flags))
563 mask = POLLIN | POLLRDNORM;
564 if (desc->rerr || desc->werr)
565 mask |= POLLERR;
566 if (!test_bit(WDM_IN_USE, &desc->flags))
567 mask |= POLLOUT | POLLWRNORM;
568 spin_unlock_irqrestore(&desc->iuspin, flags);
569
570 poll_wait(file, &desc->wait, wait);
571
572desc_out:
573 return mask;
574}
575
576static int wdm_open(struct inode *inode, struct file *file)
577{
578 int minor = iminor(inode);
579 int rv = -ENODEV;
580 struct usb_interface *intf;
581 struct wdm_device *desc;
582
583 mutex_lock(&wdm_mutex);
Bjørn Morkb0c13862012-03-06 17:29:21 +0100584 desc = wdm_find_device_by_minor(minor);
585 if (!desc)
Oliver Neukumafba9372008-05-13 17:01:25 +0200586 goto out;
587
Bjørn Morkb0c13862012-03-06 17:29:21 +0100588 intf = desc->intf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200589 if (test_bit(WDM_DISCONNECTING, &desc->flags))
590 goto out;
Oliver Neukumafba9372008-05-13 17:01:25 +0200591 file->private_data = desc;
592
Oliver Neukum17d80d52008-06-24 15:56:10 +0200593 rv = usb_autopm_get_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200594 if (rv < 0) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700595 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200596 goto out;
597 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200598
Bjørn Morke8537bd2012-01-16 12:41:48 +0100599 /* using write lock to protect desc->count */
600 mutex_lock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200601 if (!desc->count++) {
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200602 desc->werr = 0;
603 desc->rerr = 0;
Oliver Neukum17d80d52008-06-24 15:56:10 +0200604 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
605 if (rv < 0) {
606 desc->count--;
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700607 dev_err(&desc->intf->dev,
608 "Error submitting int urb - %d\n", rv);
Oliver Neukum12a98b22012-04-30 09:57:31 +0200609 rv = usb_translate_errors(rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200610 }
611 } else {
612 rv = 0;
613 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100614 mutex_unlock(&desc->wlock);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100615 if (desc->count == 1)
616 desc->manage_power(intf, 1);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200617 usb_autopm_put_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200618out:
619 mutex_unlock(&wdm_mutex);
620 return rv;
621}
622
623static int wdm_release(struct inode *inode, struct file *file)
624{
625 struct wdm_device *desc = file->private_data;
626
627 mutex_lock(&wdm_mutex);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100628
629 /* using write lock to protect desc->count */
630 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200631 desc->count--;
Bjørn Morke8537bd2012-01-16 12:41:48 +0100632 mutex_unlock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200633
Oliver Neukumafba9372008-05-13 17:01:25 +0200634 if (!desc->count) {
Bjørn Mork880bca32012-04-30 09:26:11 +0200635 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
Bjørn Mork6b0b79d2012-05-09 13:53:22 +0200636 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
637 kill_urbs(desc);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100638 desc->manage_power(desc->intf, 0);
Bjørn Mork880bca32012-04-30 09:26:11 +0200639 } else {
Bjørn Mork6b0b79d2012-05-09 13:53:22 +0200640 /* must avoid dev_printk here as desc->intf is invalid */
641 pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
Oliver Neukum2f338c82012-04-27 14:36:37 +0200642 cleanup(desc);
Bjørn Mork880bca32012-04-30 09:26:11 +0200643 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200644 }
645 mutex_unlock(&wdm_mutex);
646 return 0;
647}
648
Bjørn Mork3edce1c2013-03-17 21:00:06 +0100649static long wdm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
650{
651 struct wdm_device *desc = file->private_data;
652 int rv = 0;
653
654 switch (cmd) {
655 case IOCTL_WDM_MAX_COMMAND:
656 if (copy_to_user((void __user *)arg, &desc->wMaxCommand, sizeof(desc->wMaxCommand)))
657 rv = -EFAULT;
658 break;
659 default:
660 rv = -ENOTTY;
661 }
662 return rv;
663}
664
Oliver Neukumafba9372008-05-13 17:01:25 +0200665static const struct file_operations wdm_fops = {
666 .owner = THIS_MODULE,
667 .read = wdm_read,
668 .write = wdm_write,
669 .open = wdm_open,
670 .flush = wdm_flush,
671 .release = wdm_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200672 .poll = wdm_poll,
Bjørn Mork3edce1c2013-03-17 21:00:06 +0100673 .unlocked_ioctl = wdm_ioctl,
674 .compat_ioctl = wdm_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200675 .llseek = noop_llseek,
Oliver Neukumafba9372008-05-13 17:01:25 +0200676};
677
678static struct usb_class_driver wdm_class = {
679 .name = "cdc-wdm%d",
680 .fops = &wdm_fops,
681 .minor_base = WDM_MINOR_BASE,
682};
683
684/* --- error handling --- */
685static void wdm_rxwork(struct work_struct *work)
686{
687 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
688 unsigned long flags;
Oliver Neukum6dd433e2013-08-06 14:22:59 +0200689 int rv = 0;
690 int responding;
Oliver Neukumafba9372008-05-13 17:01:25 +0200691
692 spin_lock_irqsave(&desc->iuspin, flags);
693 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
694 spin_unlock_irqrestore(&desc->iuspin, flags);
695 } else {
Oliver Neukum6dd433e2013-08-06 14:22:59 +0200696 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200697 spin_unlock_irqrestore(&desc->iuspin, flags);
Oliver Neukum6dd433e2013-08-06 14:22:59 +0200698 if (!responding)
699 rv = usb_submit_urb(desc->response, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200700 if (rv < 0 && rv != -EPERM) {
701 spin_lock_irqsave(&desc->iuspin, flags);
Oliver Neukum6dd433e2013-08-06 14:22:59 +0200702 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200703 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
704 schedule_work(&desc->rxwork);
705 spin_unlock_irqrestore(&desc->iuspin, flags);
706 }
707 }
708}
709
710/* --- hotplug --- */
711
Bjørn Mork3cc36152012-03-06 17:29:22 +0100712static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
713 u16 bufsize, int (*manage_power)(struct usb_interface *, int))
Oliver Neukumafba9372008-05-13 17:01:25 +0200714{
Bjørn Mork0dffb482012-03-06 17:29:20 +0100715 int rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200716 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200717
Oliver Neukumafba9372008-05-13 17:01:25 +0200718 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
719 if (!desc)
720 goto out;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100721 INIT_LIST_HEAD(&desc->device_list);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100722 mutex_init(&desc->rlock);
723 mutex_init(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200724 spin_lock_init(&desc->iuspin);
725 init_waitqueue_head(&desc->wait);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100726 desc->wMaxCommand = bufsize;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200727 /* this will be expanded and needed in hardware endianness */
Oliver Neukumafba9372008-05-13 17:01:25 +0200728 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
729 desc->intf = intf;
730 INIT_WORK(&desc->rxwork, wdm_rxwork);
731
Oliver Neukum052fbc02009-04-20 17:24:49 +0200732 rv = -EINVAL;
Bjørn Mork0dffb482012-03-06 17:29:20 +0100733 if (!usb_endpoint_is_int_in(ep))
Oliver Neukum052fbc02009-04-20 17:24:49 +0200734 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200735
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700736 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
Oliver Neukumafba9372008-05-13 17:01:25 +0200737
738 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
739 if (!desc->orq)
740 goto err;
741 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
742 if (!desc->irq)
743 goto err;
744
745 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
746 if (!desc->validity)
747 goto err;
748
749 desc->response = usb_alloc_urb(0, GFP_KERNEL);
750 if (!desc->response)
751 goto err;
752
753 desc->command = usb_alloc_urb(0, GFP_KERNEL);
754 if (!desc->command)
755 goto err;
756
757 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
758 if (!desc->ubuf)
759 goto err;
760
Bjørn Mork8457d992012-01-16 15:12:00 +0100761 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200762 if (!desc->sbuf)
763 goto err;
764
Bjørn Mork8457d992012-01-16 15:12:00 +0100765 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200766 if (!desc->inbuf)
Bjørn Mork8457d992012-01-16 15:12:00 +0100767 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200768
769 usb_fill_int_urb(
770 desc->validity,
771 interface_to_usbdev(intf),
772 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
773 desc->sbuf,
774 desc->wMaxPacketSize,
775 wdm_int_callback,
776 desc,
777 ep->bInterval
778 );
Oliver Neukumafba9372008-05-13 17:01:25 +0200779
Bjørn Mork19b85b32012-01-16 15:11:58 +0100780 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
781 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
782 desc->irq->wValue = 0;
783 desc->irq->wIndex = desc->inum;
784 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
785
786 usb_fill_control_urb(
787 desc->response,
Bjørn Mork8143a892012-01-16 15:12:01 +0100788 interface_to_usbdev(intf),
Bjørn Mork19b85b32012-01-16 15:11:58 +0100789 /* using common endpoint 0 */
790 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
791 (unsigned char *)desc->irq,
792 desc->inbuf,
793 desc->wMaxCommand,
794 wdm_in_callback,
795 desc
796 );
Bjørn Mork19b85b32012-01-16 15:11:58 +0100797
Bjørn Mork3cc36152012-03-06 17:29:22 +0100798 desc->manage_power = manage_power;
799
Bjørn Morkb0c13862012-03-06 17:29:21 +0100800 spin_lock(&wdm_device_list_lock);
801 list_add(&desc->device_list, &wdm_device_list);
802 spin_unlock(&wdm_device_list_lock);
803
Oliver Neukumafba9372008-05-13 17:01:25 +0200804 rv = usb_register_dev(intf, &wdm_class);
Oliver Neukumafba9372008-05-13 17:01:25 +0200805 if (rv < 0)
Bjørn Morkb0c13862012-03-06 17:29:21 +0100806 goto err;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200807 else
Bjørn Mork820c6292012-01-20 04:17:25 +0100808 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
Oliver Neukumafba9372008-05-13 17:01:25 +0200809out:
810 return rv;
Oliver Neukumafba9372008-05-13 17:01:25 +0200811err:
Bjørn Mork6286d852012-05-09 13:53:23 +0200812 spin_lock(&wdm_device_list_lock);
813 list_del(&desc->device_list);
814 spin_unlock(&wdm_device_list_lock);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100815 cleanup(desc);
816 return rv;
817}
818
Bjørn Mork3cc36152012-03-06 17:29:22 +0100819static int wdm_manage_power(struct usb_interface *intf, int on)
820{
821 /* need autopm_get/put here to ensure the usbcore sees the new value */
822 int rv = usb_autopm_get_interface(intf);
823 if (rv < 0)
824 goto err;
825
826 intf->needs_remote_wakeup = on;
827 usb_autopm_put_interface(intf);
828err:
829 return rv;
830}
831
Bjørn Mork0dffb482012-03-06 17:29:20 +0100832static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
833{
834 int rv = -EINVAL;
835 struct usb_host_interface *iface;
836 struct usb_endpoint_descriptor *ep;
837 struct usb_cdc_dmm_desc *dmhd;
838 u8 *buffer = intf->altsetting->extra;
839 int buflen = intf->altsetting->extralen;
840 u16 maxcom = WDM_DEFAULT_BUFSIZE;
841
842 if (!buffer)
843 goto err;
844 while (buflen > 2) {
845 if (buffer[1] != USB_DT_CS_INTERFACE) {
846 dev_err(&intf->dev, "skipping garbage\n");
847 goto next_desc;
848 }
849
850 switch (buffer[2]) {
851 case USB_CDC_HEADER_TYPE:
852 break;
853 case USB_CDC_DMM_TYPE:
854 dmhd = (struct usb_cdc_dmm_desc *)buffer;
855 maxcom = le16_to_cpu(dmhd->wMaxCommand);
856 dev_dbg(&intf->dev,
857 "Finding maximum buffer length: %d", maxcom);
858 break;
859 default:
860 dev_err(&intf->dev,
861 "Ignoring extra header, type %d, length %d\n",
862 buffer[2], buffer[0]);
863 break;
864 }
865next_desc:
866 buflen -= buffer[0];
867 buffer += buffer[0];
868 }
869
870 iface = intf->cur_altsetting;
871 if (iface->desc.bNumEndpoints != 1)
872 goto err;
873 ep = &iface->endpoint[0].desc;
874
Bjørn Mork3cc36152012-03-06 17:29:22 +0100875 rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100876
877err:
Oliver Neukumafba9372008-05-13 17:01:25 +0200878 return rv;
879}
880
Bjørn Mork3cc36152012-03-06 17:29:22 +0100881/**
882 * usb_cdc_wdm_register - register a WDM subdriver
883 * @intf: usb interface the subdriver will associate with
884 * @ep: interrupt endpoint to monitor for notifications
885 * @bufsize: maximum message size to support for read/write
886 *
887 * Create WDM usb class character device and associate it with intf
888 * without binding, allowing another driver to manage the interface.
889 *
890 * The subdriver will manage the given interrupt endpoint exclusively
891 * and will issue control requests referring to the given intf. It
892 * will otherwise avoid interferring, and in particular not do
893 * usb_set_intfdata/usb_get_intfdata on intf.
894 *
895 * The return value is a pointer to the subdriver's struct usb_driver.
896 * The registering driver is responsible for calling this subdriver's
897 * disconnect, suspend, resume, pre_reset and post_reset methods from
898 * its own.
899 */
900struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
901 struct usb_endpoint_descriptor *ep,
902 int bufsize,
903 int (*manage_power)(struct usb_interface *, int))
904{
905 int rv = -EINVAL;
906
907 rv = wdm_create(intf, ep, bufsize, manage_power);
908 if (rv < 0)
909 goto err;
910
911 return &wdm_driver;
912err:
913 return ERR_PTR(rv);
914}
915EXPORT_SYMBOL(usb_cdc_wdm_register);
916
Oliver Neukumafba9372008-05-13 17:01:25 +0200917static void wdm_disconnect(struct usb_interface *intf)
918{
919 struct wdm_device *desc;
920 unsigned long flags;
921
922 usb_deregister_dev(intf, &wdm_class);
Bjørn Morkb0c13862012-03-06 17:29:21 +0100923 desc = wdm_find_device(intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200924 mutex_lock(&wdm_mutex);
Oliver Neukumafba9372008-05-13 17:01:25 +0200925
926 /* the spinlock makes sure no new urbs are generated in the callbacks */
927 spin_lock_irqsave(&desc->iuspin, flags);
928 set_bit(WDM_DISCONNECTING, &desc->flags);
929 set_bit(WDM_READ, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200930 /* to terminate pending flushes */
Oliver Neukumafba9372008-05-13 17:01:25 +0200931 clear_bit(WDM_IN_USE, &desc->flags);
932 spin_unlock_irqrestore(&desc->iuspin, flags);
Bjørn Mork62aaf242012-01-16 15:11:57 +0100933 wake_up_all(&desc->wait);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100934 mutex_lock(&desc->rlock);
935 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200936 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100937 cancel_work_sync(&desc->rxwork);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100938 mutex_unlock(&desc->wlock);
939 mutex_unlock(&desc->rlock);
Bjørn Mork6286d852012-05-09 13:53:23 +0200940
941 /* the desc->intf pointer used as list key is now invalid */
942 spin_lock(&wdm_device_list_lock);
943 list_del(&desc->device_list);
944 spin_unlock(&wdm_device_list_lock);
945
Oliver Neukumafba9372008-05-13 17:01:25 +0200946 if (!desc->count)
947 cleanup(desc);
Bjørn Mork880bca32012-04-30 09:26:11 +0200948 else
949 dev_dbg(&intf->dev, "%s: %d open files - postponing cleanup\n", __func__, desc->count);
Oliver Neukumafba9372008-05-13 17:01:25 +0200950 mutex_unlock(&wdm_mutex);
951}
952
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100953#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200954static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
955{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100956 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200957 int rv = 0;
958
959 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
960
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100961 /* if this is an autosuspend the caller does the locking */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100962 if (!PMSG_IS_AUTO(message)) {
963 mutex_lock(&desc->rlock);
964 mutex_lock(&desc->wlock);
965 }
Oliver Neukum62e66852010-02-27 20:56:22 +0100966 spin_lock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100967
Alan Stern5b1b0b82011-08-19 23:49:48 +0200968 if (PMSG_IS_AUTO(message) &&
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100969 (test_bit(WDM_IN_USE, &desc->flags)
970 || test_bit(WDM_RESPONDING, &desc->flags))) {
Oliver Neukum62e66852010-02-27 20:56:22 +0100971 spin_unlock_irq(&desc->iuspin);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200972 rv = -EBUSY;
973 } else {
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100974
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100975 set_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100976 spin_unlock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100977 /* callback submits work - order is essential */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200978 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100979 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200980 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100981 if (!PMSG_IS_AUTO(message)) {
982 mutex_unlock(&desc->wlock);
983 mutex_unlock(&desc->rlock);
984 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200985
986 return rv;
987}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100988#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200989
990static int recover_from_urb_loss(struct wdm_device *desc)
991{
992 int rv = 0;
993
994 if (desc->count) {
995 rv = usb_submit_urb(desc->validity, GFP_NOIO);
996 if (rv < 0)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700997 dev_err(&desc->intf->dev,
998 "Error resume submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200999 }
1000 return rv;
1001}
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001002
1003#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +02001004static int wdm_resume(struct usb_interface *intf)
1005{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001006 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001007 int rv;
1008
1009 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
Oliver Neukum338124c2010-02-27 20:57:12 +01001010
Oliver Neukumbeb1d352010-02-27 20:55:52 +01001011 clear_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +01001012 rv = recover_from_urb_loss(desc);
Oliver Neukum338124c2010-02-27 20:57:12 +01001013
Oliver Neukum17d80d52008-06-24 15:56:10 +02001014 return rv;
1015}
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001016#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +02001017
1018static int wdm_pre_reset(struct usb_interface *intf)
1019{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001020 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001021
Oliver Neukumd771d8a2011-04-29 14:12:21 +02001022 /*
1023 * we notify everybody using poll of
1024 * an exceptional situation
1025 * must be done before recovery lest a spontaneous
1026 * message from the device is lost
1027 */
1028 spin_lock_irq(&desc->iuspin);
Bjørn Mork88044202012-02-10 09:44:08 +01001029 set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
1030 set_bit(WDM_READ, &desc->flags); /* unblock read */
1031 clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
Oliver Neukumd771d8a2011-04-29 14:12:21 +02001032 desc->rerr = -EINTR;
1033 spin_unlock_irq(&desc->iuspin);
1034 wake_up_all(&desc->wait);
Bjørn Mork88044202012-02-10 09:44:08 +01001035 mutex_lock(&desc->rlock);
1036 mutex_lock(&desc->wlock);
1037 kill_urbs(desc);
1038 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001039 return 0;
1040}
1041
1042static int wdm_post_reset(struct usb_interface *intf)
1043{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001044 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001045 int rv;
1046
Oliver Neukumc0f5ece2013-03-12 14:52:42 +01001047 clear_bit(WDM_OVERFLOW, &desc->flags);
Bjørn Mork88044202012-02-10 09:44:08 +01001048 clear_bit(WDM_RESETTING, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001049 rv = recover_from_urb_loss(desc);
Bjørn Morke8537bd2012-01-16 12:41:48 +01001050 mutex_unlock(&desc->wlock);
1051 mutex_unlock(&desc->rlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001052 return 0;
1053}
1054
Oliver Neukumafba9372008-05-13 17:01:25 +02001055static struct usb_driver wdm_driver = {
1056 .name = "cdc_wdm",
1057 .probe = wdm_probe,
1058 .disconnect = wdm_disconnect,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001059#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +02001060 .suspend = wdm_suspend,
1061 .resume = wdm_resume,
1062 .reset_resume = wdm_resume,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001063#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +02001064 .pre_reset = wdm_pre_reset,
1065 .post_reset = wdm_post_reset,
Oliver Neukumafba9372008-05-13 17:01:25 +02001066 .id_table = wdm_ids,
Oliver Neukum17d80d52008-06-24 15:56:10 +02001067 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07001068 .disable_hub_initiated_lpm = 1,
Oliver Neukumafba9372008-05-13 17:01:25 +02001069};
1070
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001071module_usb_driver(wdm_driver);
Oliver Neukumafba9372008-05-13 17:01:25 +02001072
1073MODULE_AUTHOR(DRIVER_AUTHOR);
Oliver Neukum87d65e52008-06-19 14:20:18 +02001074MODULE_DESCRIPTION(DRIVER_DESC);
Oliver Neukumafba9372008-05-13 17:01:25 +02001075MODULE_LICENSE("GPL");