blob: e5fa34e5423f9c1486b2092a0c7318d1ea1b88aa [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;
Oliver Neukume200d6b2013-08-06 14:22:59 +0200236 int responding;
Oliver Neukumafba9372008-05-13 17:01:25 +0200237 int status = urb->status;
238 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200239 struct usb_cdc_notification *dr;
240
241 desc = urb->context;
Oliver Neukumafba9372008-05-13 17:01:25 +0200242 dr = (struct usb_cdc_notification *)desc->sbuf;
243
244 if (status) {
245 switch (status) {
246 case -ESHUTDOWN:
247 case -ENOENT:
248 case -ECONNRESET:
249 return; /* unplug */
250 case -EPIPE:
251 set_bit(WDM_INT_STALL, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700252 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200253 goto sw; /* halt is cleared in work */
254 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700255 dev_err(&desc->intf->dev,
256 "nonzero urb status received: %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200257 break;
258 }
259 }
260
261 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700262 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
263 urb->actual_length);
Oliver Neukumafba9372008-05-13 17:01:25 +0200264 goto exit;
265 }
266
267 switch (dr->bNotificationType) {
268 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
269 dev_dbg(&desc->intf->dev,
270 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
271 dr->wIndex, dr->wLength);
272 break;
273
274 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
275
276 dev_dbg(&desc->intf->dev,
277 "NOTIFY_NETWORK_CONNECTION %s network",
278 dr->wValue ? "connected to" : "disconnected from");
279 goto exit;
280 default:
281 clear_bit(WDM_POLL_RUNNING, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700282 dev_err(&desc->intf->dev,
283 "unknown notification %d received: index %d len %d\n",
Oliver Neukumafba9372008-05-13 17:01:25 +0200284 dr->bNotificationType, dr->wIndex, dr->wLength);
285 goto exit;
286 }
287
Oliver Neukumafba9372008-05-13 17:01:25 +0200288 spin_lock(&desc->iuspin);
289 clear_bit(WDM_READ, &desc->flags);
Oliver Neukume200d6b2013-08-06 14:22:59 +0200290 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
291 if (!responding && !test_bit(WDM_DISCONNECTING, &desc->flags)
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100292 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200293 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
294 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
295 __func__, rv);
296 }
297 spin_unlock(&desc->iuspin);
298 if (rv < 0) {
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100299 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200300 if (rv == -EPERM)
301 return;
302 if (rv == -ENOMEM) {
303sw:
304 rv = schedule_work(&desc->rxwork);
305 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700306 dev_err(&desc->intf->dev,
307 "Cannot schedule work\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200308 }
309 }
310exit:
311 rv = usb_submit_urb(urb, GFP_ATOMIC);
312 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700313 dev_err(&desc->intf->dev,
314 "%s - usb_submit_urb failed with result %d\n",
315 __func__, rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200316
317}
318
319static void kill_urbs(struct wdm_device *desc)
320{
Oliver Neukum17d80d52008-06-24 15:56:10 +0200321 /* the order here is essential */
Oliver Neukumafba9372008-05-13 17:01:25 +0200322 usb_kill_urb(desc->command);
323 usb_kill_urb(desc->validity);
324 usb_kill_urb(desc->response);
325}
326
327static void free_urbs(struct wdm_device *desc)
328{
329 usb_free_urb(desc->validity);
330 usb_free_urb(desc->response);
331 usb_free_urb(desc->command);
332}
333
334static void cleanup(struct wdm_device *desc)
335{
Bjørn Mork8457d992012-01-16 15:12:00 +0100336 kfree(desc->sbuf);
337 kfree(desc->inbuf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200338 kfree(desc->orq);
339 kfree(desc->irq);
340 kfree(desc->ubuf);
341 free_urbs(desc);
342 kfree(desc);
343}
344
345static ssize_t wdm_write
346(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
347{
348 u8 *buf;
349 int rv = -EMSGSIZE, r, we;
350 struct wdm_device *desc = file->private_data;
351 struct usb_ctrlrequest *req;
352
353 if (count > desc->wMaxCommand)
354 count = desc->wMaxCommand;
355
356 spin_lock_irq(&desc->iuspin);
357 we = desc->werr;
358 desc->werr = 0;
359 spin_unlock_irq(&desc->iuspin);
360 if (we < 0)
361 return -EIO;
362
Oliver Neukum5c228372012-04-26 21:59:10 +0200363 buf = kmalloc(count, GFP_KERNEL);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100364 if (!buf) {
365 rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200366 goto outnl;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100367 }
368
369 r = copy_from_user(buf, buffer, count);
370 if (r > 0) {
371 kfree(buf);
372 rv = -EFAULT;
373 goto outnl;
374 }
375
376 /* concurrent writes and disconnect */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100377 r = mutex_lock_interruptible(&desc->wlock);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100378 rv = -ERESTARTSYS;
379 if (r) {
380 kfree(buf);
381 goto outnl;
382 }
383
384 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
385 kfree(buf);
386 rv = -ENODEV;
387 goto outnp;
388 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200389
Oliver Neukum17d80d52008-06-24 15:56:10 +0200390 r = usb_autopm_get_interface(desc->intf);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100391 if (r < 0) {
392 kfree(buf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200393 goto outnp;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100394 }
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200395
David Sterba0cdfb812010-12-27 18:49:58 +0100396 if (!(file->f_flags & O_NONBLOCK))
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200397 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
398 &desc->flags));
399 else
400 if (test_bit(WDM_IN_USE, &desc->flags))
401 r = -EAGAIN;
Bjørn Mork88044202012-02-10 09:44:08 +0100402
403 if (test_bit(WDM_RESETTING, &desc->flags))
404 r = -EIO;
405
Oliver Neukum860e41a2010-02-27 20:54:24 +0100406 if (r < 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200407 kfree(buf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200408 goto out;
409 }
410
411 req = desc->orq;
412 usb_fill_control_urb(
413 desc->command,
414 interface_to_usbdev(desc->intf),
415 /* using common endpoint 0 */
416 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
417 (unsigned char *)req,
418 buf,
419 count,
420 wdm_out_callback,
421 desc
422 );
423
424 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
425 USB_RECIP_INTERFACE);
426 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
427 req->wValue = 0;
428 req->wIndex = desc->inum;
429 req->wLength = cpu_to_le16(count);
430 set_bit(WDM_IN_USE, &desc->flags);
Oliver Neukum5c228372012-04-26 21:59:10 +0200431 desc->outbuf = buf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200432
433 rv = usb_submit_urb(desc->command, GFP_KERNEL);
434 if (rv < 0) {
435 kfree(buf);
Oliver Neukum5c228372012-04-26 21:59:10 +0200436 desc->outbuf = NULL;
Oliver Neukumafba9372008-05-13 17:01:25 +0200437 clear_bit(WDM_IN_USE, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700438 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200439 } else {
440 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
441 req->wIndex);
442 }
443out:
Oliver Neukum17d80d52008-06-24 15:56:10 +0200444 usb_autopm_put_interface(desc->intf);
445outnp:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100446 mutex_unlock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200447outnl:
448 return rv < 0 ? rv : count;
449}
450
451static ssize_t wdm_read
452(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
453{
Ben Hutchings711c68b2012-02-12 06:00:41 +0000454 int rv, cntr;
Oliver Neukumafba9372008-05-13 17:01:25 +0200455 int i = 0;
456 struct wdm_device *desc = file->private_data;
457
458
Bjørn Morke8537bd2012-01-16 12:41:48 +0100459 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
Oliver Neukumafba9372008-05-13 17:01:25 +0200460 if (rv < 0)
461 return -ERESTARTSYS;
462
Ben Hutchings711c68b2012-02-12 06:00:41 +0000463 cntr = ACCESS_ONCE(desc->length);
464 if (cntr == 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200465 desc->read = 0;
466retry:
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200467 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
468 rv = -ENODEV;
469 goto err;
470 }
Oliver Neukum5c6142c2013-03-12 14:52:42 +0100471 if (test_bit(WDM_OVERFLOW, &desc->flags)) {
472 clear_bit(WDM_OVERFLOW, &desc->flags);
473 rv = -ENOBUFS;
474 goto err;
475 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200476 i++;
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200477 if (file->f_flags & O_NONBLOCK) {
478 if (!test_bit(WDM_READ, &desc->flags)) {
479 rv = cntr ? cntr : -EAGAIN;
480 goto err;
481 }
482 rv = 0;
483 } else {
484 rv = wait_event_interruptible(desc->wait,
485 test_bit(WDM_READ, &desc->flags));
486 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200487
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200488 /* may have happened while we slept */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200489 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
490 rv = -ENODEV;
491 goto err;
492 }
Bjørn Mork88044202012-02-10 09:44:08 +0100493 if (test_bit(WDM_RESETTING, &desc->flags)) {
494 rv = -EIO;
495 goto err;
496 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200497 usb_mark_last_busy(interface_to_usbdev(desc->intf));
Oliver Neukumafba9372008-05-13 17:01:25 +0200498 if (rv < 0) {
499 rv = -ERESTARTSYS;
500 goto err;
501 }
502
503 spin_lock_irq(&desc->iuspin);
504
505 if (desc->rerr) { /* read completed, error happened */
Oliver Neukumafba9372008-05-13 17:01:25 +0200506 desc->rerr = 0;
507 spin_unlock_irq(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200508 rv = -EIO;
509 goto err;
510 }
511 /*
512 * recheck whether we've lost the race
513 * against the completion handler
514 */
515 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
516 spin_unlock_irq(&desc->iuspin);
517 goto retry;
518 }
Oliver Neukum5c6142c2013-03-12 14:52:42 +0100519
Oliver Neukumafba9372008-05-13 17:01:25 +0200520 if (!desc->reslength) { /* zero length read */
Bjørn Morkb80f6db2012-07-02 10:33:14 +0200521 dev_dbg(&desc->intf->dev, "%s: zero length - clearing WDM_READ\n", __func__);
522 clear_bit(WDM_READ, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200523 spin_unlock_irq(&desc->iuspin);
524 goto retry;
525 }
Ben Hutchings711c68b2012-02-12 06:00:41 +0000526 cntr = desc->length;
Oliver Neukumafba9372008-05-13 17:01:25 +0200527 spin_unlock_irq(&desc->iuspin);
528 }
529
Ben Hutchings711c68b2012-02-12 06:00:41 +0000530 if (cntr > count)
531 cntr = count;
Oliver Neukumafba9372008-05-13 17:01:25 +0200532 rv = copy_to_user(buffer, desc->ubuf, cntr);
533 if (rv > 0) {
534 rv = -EFAULT;
535 goto err;
536 }
537
Ben Hutchings711c68b2012-02-12 06:00:41 +0000538 spin_lock_irq(&desc->iuspin);
539
Oliver Neukumafba9372008-05-13 17:01:25 +0200540 for (i = 0; i < desc->length - cntr; i++)
541 desc->ubuf[i] = desc->ubuf[i + cntr];
542
543 desc->length -= cntr;
Oliver Neukum87d65e52008-06-19 14:20:18 +0200544 /* in case we had outstanding data */
545 if (!desc->length)
546 clear_bit(WDM_READ, &desc->flags);
Ben Hutchings711c68b2012-02-12 06:00:41 +0000547
548 spin_unlock_irq(&desc->iuspin);
549
Oliver Neukumafba9372008-05-13 17:01:25 +0200550 rv = cntr;
551
552err:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100553 mutex_unlock(&desc->rlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200554 return rv;
555}
556
557static int wdm_flush(struct file *file, fl_owner_t id)
558{
559 struct wdm_device *desc = file->private_data;
560
561 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
Bjørn Mork577bd3e2012-05-09 13:53:22 +0200562
563 /* cannot dereference desc->intf if WDM_DISCONNECTING */
564 if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags))
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700565 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
566 desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200567
Oliver Neukumeae68612012-04-27 14:23:54 +0200568 return usb_translate_errors(desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200569}
570
571static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
572{
573 struct wdm_device *desc = file->private_data;
574 unsigned long flags;
575 unsigned int mask = 0;
576
577 spin_lock_irqsave(&desc->iuspin, flags);
578 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
Bjørn Morkd0765ea2012-05-09 13:53:21 +0200579 mask = POLLHUP | POLLERR;
Oliver Neukumafba9372008-05-13 17:01:25 +0200580 spin_unlock_irqrestore(&desc->iuspin, flags);
581 goto desc_out;
582 }
583 if (test_bit(WDM_READ, &desc->flags))
584 mask = POLLIN | POLLRDNORM;
585 if (desc->rerr || desc->werr)
586 mask |= POLLERR;
587 if (!test_bit(WDM_IN_USE, &desc->flags))
588 mask |= POLLOUT | POLLWRNORM;
589 spin_unlock_irqrestore(&desc->iuspin, flags);
590
591 poll_wait(file, &desc->wait, wait);
592
593desc_out:
594 return mask;
595}
596
597static int wdm_open(struct inode *inode, struct file *file)
598{
599 int minor = iminor(inode);
600 int rv = -ENODEV;
601 struct usb_interface *intf;
602 struct wdm_device *desc;
603
604 mutex_lock(&wdm_mutex);
Bjørn Morkb0c13862012-03-06 17:29:21 +0100605 desc = wdm_find_device_by_minor(minor);
606 if (!desc)
Oliver Neukumafba9372008-05-13 17:01:25 +0200607 goto out;
608
Bjørn Morkb0c13862012-03-06 17:29:21 +0100609 intf = desc->intf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200610 if (test_bit(WDM_DISCONNECTING, &desc->flags))
611 goto out;
Oliver Neukumafba9372008-05-13 17:01:25 +0200612 file->private_data = desc;
613
Oliver Neukum17d80d52008-06-24 15:56:10 +0200614 rv = usb_autopm_get_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200615 if (rv < 0) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700616 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200617 goto out;
618 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200619
Bjørn Morke8537bd2012-01-16 12:41:48 +0100620 /* using write lock to protect desc->count */
621 mutex_lock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200622 if (!desc->count++) {
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200623 desc->werr = 0;
624 desc->rerr = 0;
Oliver Neukum17d80d52008-06-24 15:56:10 +0200625 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
626 if (rv < 0) {
627 desc->count--;
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700628 dev_err(&desc->intf->dev,
629 "Error submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200630 }
631 } else {
632 rv = 0;
633 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100634 mutex_unlock(&desc->wlock);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100635 if (desc->count == 1)
636 desc->manage_power(intf, 1);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200637 usb_autopm_put_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200638out:
639 mutex_unlock(&wdm_mutex);
640 return rv;
641}
642
643static int wdm_release(struct inode *inode, struct file *file)
644{
645 struct wdm_device *desc = file->private_data;
646
647 mutex_lock(&wdm_mutex);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100648
649 /* using write lock to protect desc->count */
650 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200651 desc->count--;
Bjørn Morke8537bd2012-01-16 12:41:48 +0100652 mutex_unlock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200653
Oliver Neukumafba9372008-05-13 17:01:25 +0200654 if (!desc->count) {
Bjørn Mork577bd3e2012-05-09 13:53:22 +0200655 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
656 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
657 kill_urbs(desc);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100658 desc->manage_power(desc->intf, 0);
Bjørn Mork577bd3e2012-05-09 13:53:22 +0200659 } else {
660 /* must avoid dev_printk here as desc->intf is invalid */
661 pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
Oliver Neukumfccfda62012-04-27 14:36:37 +0200662 cleanup(desc);
Bjørn Mork577bd3e2012-05-09 13:53:22 +0200663 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200664 }
665 mutex_unlock(&wdm_mutex);
666 return 0;
667}
668
669static const struct file_operations wdm_fops = {
670 .owner = THIS_MODULE,
671 .read = wdm_read,
672 .write = wdm_write,
673 .open = wdm_open,
674 .flush = wdm_flush,
675 .release = wdm_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200676 .poll = wdm_poll,
677 .llseek = noop_llseek,
Oliver Neukumafba9372008-05-13 17:01:25 +0200678};
679
680static struct usb_class_driver wdm_class = {
681 .name = "cdc-wdm%d",
682 .fops = &wdm_fops,
683 .minor_base = WDM_MINOR_BASE,
684};
685
686/* --- error handling --- */
687static void wdm_rxwork(struct work_struct *work)
688{
689 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
690 unsigned long flags;
Oliver Neukume200d6b2013-08-06 14:22:59 +0200691 int rv = 0;
692 int responding;
Oliver Neukumafba9372008-05-13 17:01:25 +0200693
694 spin_lock_irqsave(&desc->iuspin, flags);
695 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
696 spin_unlock_irqrestore(&desc->iuspin, flags);
697 } else {
Oliver Neukume200d6b2013-08-06 14:22:59 +0200698 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200699 spin_unlock_irqrestore(&desc->iuspin, flags);
Oliver Neukume200d6b2013-08-06 14:22:59 +0200700 if (!responding)
701 rv = usb_submit_urb(desc->response, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200702 if (rv < 0 && rv != -EPERM) {
703 spin_lock_irqsave(&desc->iuspin, flags);
Oliver Neukume200d6b2013-08-06 14:22:59 +0200704 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200705 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
706 schedule_work(&desc->rxwork);
707 spin_unlock_irqrestore(&desc->iuspin, flags);
708 }
709 }
710}
711
712/* --- hotplug --- */
713
Bjørn Mork3cc36152012-03-06 17:29:22 +0100714static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
715 u16 bufsize, int (*manage_power)(struct usb_interface *, int))
Oliver Neukumafba9372008-05-13 17:01:25 +0200716{
Bjørn Mork0dffb482012-03-06 17:29:20 +0100717 int rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200718 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200719
Oliver Neukumafba9372008-05-13 17:01:25 +0200720 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
721 if (!desc)
722 goto out;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100723 INIT_LIST_HEAD(&desc->device_list);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100724 mutex_init(&desc->rlock);
725 mutex_init(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200726 spin_lock_init(&desc->iuspin);
727 init_waitqueue_head(&desc->wait);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100728 desc->wMaxCommand = bufsize;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200729 /* this will be expanded and needed in hardware endianness */
Oliver Neukumafba9372008-05-13 17:01:25 +0200730 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
731 desc->intf = intf;
732 INIT_WORK(&desc->rxwork, wdm_rxwork);
733
Oliver Neukum052fbc02009-04-20 17:24:49 +0200734 rv = -EINVAL;
Bjørn Mork0dffb482012-03-06 17:29:20 +0100735 if (!usb_endpoint_is_int_in(ep))
Oliver Neukum052fbc02009-04-20 17:24:49 +0200736 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200737
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700738 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
Oliver Neukumafba9372008-05-13 17:01:25 +0200739
740 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
741 if (!desc->orq)
742 goto err;
743 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
744 if (!desc->irq)
745 goto err;
746
747 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
748 if (!desc->validity)
749 goto err;
750
751 desc->response = usb_alloc_urb(0, GFP_KERNEL);
752 if (!desc->response)
753 goto err;
754
755 desc->command = usb_alloc_urb(0, GFP_KERNEL);
756 if (!desc->command)
757 goto err;
758
759 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
760 if (!desc->ubuf)
761 goto err;
762
Bjørn Mork8457d992012-01-16 15:12:00 +0100763 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200764 if (!desc->sbuf)
765 goto err;
766
Bjørn Mork8457d992012-01-16 15:12:00 +0100767 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200768 if (!desc->inbuf)
Bjørn Mork8457d992012-01-16 15:12:00 +0100769 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200770
771 usb_fill_int_urb(
772 desc->validity,
773 interface_to_usbdev(intf),
774 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
775 desc->sbuf,
776 desc->wMaxPacketSize,
777 wdm_int_callback,
778 desc,
779 ep->bInterval
780 );
Oliver Neukumafba9372008-05-13 17:01:25 +0200781
Bjørn Mork19b85b32012-01-16 15:11:58 +0100782 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
783 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
784 desc->irq->wValue = 0;
785 desc->irq->wIndex = desc->inum;
786 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
787
788 usb_fill_control_urb(
789 desc->response,
Bjørn Mork8143a892012-01-16 15:12:01 +0100790 interface_to_usbdev(intf),
Bjørn Mork19b85b32012-01-16 15:11:58 +0100791 /* using common endpoint 0 */
792 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
793 (unsigned char *)desc->irq,
794 desc->inbuf,
795 desc->wMaxCommand,
796 wdm_in_callback,
797 desc
798 );
Bjørn Mork19b85b32012-01-16 15:11:58 +0100799
Bjørn Mork3cc36152012-03-06 17:29:22 +0100800 desc->manage_power = manage_power;
801
Bjørn Morkb0c13862012-03-06 17:29:21 +0100802 spin_lock(&wdm_device_list_lock);
803 list_add(&desc->device_list, &wdm_device_list);
804 spin_unlock(&wdm_device_list_lock);
805
Oliver Neukumafba9372008-05-13 17:01:25 +0200806 rv = usb_register_dev(intf, &wdm_class);
Oliver Neukumafba9372008-05-13 17:01:25 +0200807 if (rv < 0)
Bjørn Morkb0c13862012-03-06 17:29:21 +0100808 goto err;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200809 else
Bjørn Mork820c6292012-01-20 04:17:25 +0100810 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
Oliver Neukumafba9372008-05-13 17:01:25 +0200811out:
812 return rv;
Oliver Neukumafba9372008-05-13 17:01:25 +0200813err:
Bjørn Mork9ed2cb72012-05-09 13:53:23 +0200814 spin_lock(&wdm_device_list_lock);
815 list_del(&desc->device_list);
816 spin_unlock(&wdm_device_list_lock);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100817 cleanup(desc);
818 return rv;
819}
820
Bjørn Mork3cc36152012-03-06 17:29:22 +0100821static int wdm_manage_power(struct usb_interface *intf, int on)
822{
823 /* need autopm_get/put here to ensure the usbcore sees the new value */
824 int rv = usb_autopm_get_interface(intf);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100825
826 intf->needs_remote_wakeup = on;
Bjørn Morkbc8a3912013-11-29 20:17:45 +0100827 if (!rv)
828 usb_autopm_put_interface(intf);
829 return 0;
Bjørn Mork3cc36152012-03-06 17:29:22 +0100830}
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 Mork9ed2cb72012-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);
948 mutex_unlock(&wdm_mutex);
949}
950
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100951#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200952static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
953{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100954 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200955 int rv = 0;
956
957 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
958
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100959 /* if this is an autosuspend the caller does the locking */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100960 if (!PMSG_IS_AUTO(message)) {
961 mutex_lock(&desc->rlock);
962 mutex_lock(&desc->wlock);
963 }
Oliver Neukum62e66852010-02-27 20:56:22 +0100964 spin_lock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100965
Alan Stern5b1b0b82011-08-19 23:49:48 +0200966 if (PMSG_IS_AUTO(message) &&
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100967 (test_bit(WDM_IN_USE, &desc->flags)
968 || test_bit(WDM_RESPONDING, &desc->flags))) {
Oliver Neukum62e66852010-02-27 20:56:22 +0100969 spin_unlock_irq(&desc->iuspin);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200970 rv = -EBUSY;
971 } else {
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100972
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100973 set_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100974 spin_unlock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100975 /* callback submits work - order is essential */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200976 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100977 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200978 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100979 if (!PMSG_IS_AUTO(message)) {
980 mutex_unlock(&desc->wlock);
981 mutex_unlock(&desc->rlock);
982 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200983
984 return rv;
985}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100986#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200987
988static int recover_from_urb_loss(struct wdm_device *desc)
989{
990 int rv = 0;
991
992 if (desc->count) {
993 rv = usb_submit_urb(desc->validity, GFP_NOIO);
994 if (rv < 0)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700995 dev_err(&desc->intf->dev,
996 "Error resume submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200997 }
998 return rv;
999}
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001000
1001#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +02001002static int wdm_resume(struct usb_interface *intf)
1003{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001004 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001005 int rv;
1006
1007 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
Oliver Neukum338124c2010-02-27 20:57:12 +01001008
Oliver Neukumbeb1d352010-02-27 20:55:52 +01001009 clear_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +01001010 rv = recover_from_urb_loss(desc);
Oliver Neukum338124c2010-02-27 20:57:12 +01001011
Oliver Neukum17d80d52008-06-24 15:56:10 +02001012 return rv;
1013}
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001014#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +02001015
1016static int wdm_pre_reset(struct usb_interface *intf)
1017{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001018 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001019
Oliver Neukumd771d8a2011-04-29 14:12:21 +02001020 /*
1021 * we notify everybody using poll of
1022 * an exceptional situation
1023 * must be done before recovery lest a spontaneous
1024 * message from the device is lost
1025 */
1026 spin_lock_irq(&desc->iuspin);
Bjørn Mork88044202012-02-10 09:44:08 +01001027 set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
1028 set_bit(WDM_READ, &desc->flags); /* unblock read */
1029 clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
Oliver Neukumd771d8a2011-04-29 14:12:21 +02001030 desc->rerr = -EINTR;
1031 spin_unlock_irq(&desc->iuspin);
1032 wake_up_all(&desc->wait);
Bjørn Mork88044202012-02-10 09:44:08 +01001033 mutex_lock(&desc->rlock);
1034 mutex_lock(&desc->wlock);
1035 kill_urbs(desc);
1036 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001037 return 0;
1038}
1039
1040static int wdm_post_reset(struct usb_interface *intf)
1041{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001042 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001043 int rv;
1044
Oliver Neukum5c6142c2013-03-12 14:52:42 +01001045 clear_bit(WDM_OVERFLOW, &desc->flags);
Bjørn Mork88044202012-02-10 09:44:08 +01001046 clear_bit(WDM_RESETTING, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001047 rv = recover_from_urb_loss(desc);
Bjørn Morke8537bd2012-01-16 12:41:48 +01001048 mutex_unlock(&desc->wlock);
1049 mutex_unlock(&desc->rlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001050 return 0;
1051}
1052
Oliver Neukumafba9372008-05-13 17:01:25 +02001053static struct usb_driver wdm_driver = {
1054 .name = "cdc_wdm",
1055 .probe = wdm_probe,
1056 .disconnect = wdm_disconnect,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001057#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +02001058 .suspend = wdm_suspend,
1059 .resume = wdm_resume,
1060 .reset_resume = wdm_resume,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001061#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +02001062 .pre_reset = wdm_pre_reset,
1063 .post_reset = wdm_post_reset,
Oliver Neukumafba9372008-05-13 17:01:25 +02001064 .id_table = wdm_ids,
Oliver Neukum17d80d52008-06-24 15:56:10 +02001065 .supports_autosuspend = 1,
Oliver Neukumafba9372008-05-13 17:01:25 +02001066};
1067
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001068module_usb_driver(wdm_driver);
Oliver Neukumafba9372008-05-13 17:01:25 +02001069
1070MODULE_AUTHOR(DRIVER_AUTHOR);
Oliver Neukum87d65e52008-06-19 14:20:18 +02001071MODULE_DESCRIPTION(DRIVER_DESC);
Oliver Neukumafba9372008-05-13 17:01:25 +02001072MODULE_LICENSE("GPL");