blob: 189209fa6a7e22ef9c4937cd51a1451363bb4c17 [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 */
flintmanc6dc72f2014-08-11 21:52:25 -040031#define DRIVER_VERSION "v0.03-mbm"
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
flintmanc6dc72f2014-08-11 21:52:25 -040037#ifndef PMSG_IS_AUTO
38#define PMSG_IS_AUTO(msg) (((msg).event & PM_EVENT_AUTO) != 0)
39#endif
40
Németh Márton6ef48522010-01-10 15:33:45 +010041static const struct usb_device_id wdm_ids[] = {
Oliver Neukumafba9372008-05-13 17:01:25 +020042 {
43 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
44 USB_DEVICE_ID_MATCH_INT_SUBCLASS,
45 .bInterfaceClass = USB_CLASS_COMM,
46 .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM
47 },
Bjørn Morkfec67b42012-01-25 13:03:29 +010048 {
49 /*
50 * Huawei E392, E398 and possibly other Qualcomm based modems
51 * embed the Qualcomm QMI protocol inside CDC on CDC ECM like
52 * control interfaces. Userspace access to this is required
53 * to configure the accompanying data interface
54 */
55 .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
56 USB_DEVICE_ID_MATCH_INT_INFO,
57 .idVendor = HUAWEI_VENDOR_ID,
58 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
59 .bInterfaceSubClass = 1,
60 .bInterfaceProtocol = 9, /* NOTE: CDC ECM control interface! */
61 },
Bjørn Mork9b814052012-05-19 19:19:48 +020062 {
63 /* Vodafone/Huawei K5005 (12d1:14c8) and similar modems */
64 .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
65 USB_DEVICE_ID_MATCH_INT_INFO,
66 .idVendor = HUAWEI_VENDOR_ID,
67 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
68 .bInterfaceSubClass = 1,
69 .bInterfaceProtocol = 57, /* NOTE: CDC ECM control interface! */
70 },
Oliver Neukumafba9372008-05-13 17:01:25 +020071 { }
72};
73
Oliver Neukumaa5380b2008-10-13 14:05:20 +020074MODULE_DEVICE_TABLE (usb, wdm_ids);
75
Oliver Neukumafba9372008-05-13 17:01:25 +020076#define WDM_MINOR_BASE 176
77
78
79#define WDM_IN_USE 1
80#define WDM_DISCONNECTING 2
81#define WDM_RESULT 3
82#define WDM_READ 4
83#define WDM_INT_STALL 5
84#define WDM_POLL_RUNNING 6
Oliver Neukum922a5ea2010-02-27 20:54:59 +010085#define WDM_RESPONDING 7
Oliver Neukumbeb1d352010-02-27 20:55:52 +010086#define WDM_SUSPENDING 8
Bjørn Mork88044202012-02-10 09:44:08 +010087#define WDM_RESETTING 9
Oliver Neukum5c6142c2013-03-12 14:52:42 +010088#define WDM_OVERFLOW 10
Oliver Neukumafba9372008-05-13 17:01:25 +020089
90#define WDM_MAX 16
91
Bjørn Mork7e3054a2012-01-20 01:49:57 +010092/* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
93#define WDM_DEFAULT_BUFSIZE 256
Oliver Neukumafba9372008-05-13 17:01:25 +020094
95static DEFINE_MUTEX(wdm_mutex);
Bjørn Morkb0c13862012-03-06 17:29:21 +010096static DEFINE_SPINLOCK(wdm_device_list_lock);
97static LIST_HEAD(wdm_device_list);
Oliver Neukumafba9372008-05-13 17:01:25 +020098
99/* --- method tables --- */
100
101struct wdm_device {
102 u8 *inbuf; /* buffer for response */
103 u8 *outbuf; /* buffer for command */
104 u8 *sbuf; /* buffer for status */
105 u8 *ubuf; /* buffer for copy to user space */
106
107 struct urb *command;
108 struct urb *response;
109 struct urb *validity;
110 struct usb_interface *intf;
111 struct usb_ctrlrequest *orq;
112 struct usb_ctrlrequest *irq;
113 spinlock_t iuspin;
114
115 unsigned long flags;
116 u16 bufsize;
117 u16 wMaxCommand;
118 u16 wMaxPacketSize;
Oliver Neukumafba9372008-05-13 17:01:25 +0200119 __le16 inum;
120 int reslength;
121 int length;
122 int read;
123 int count;
124 dma_addr_t shandle;
125 dma_addr_t ihandle;
Bjørn Morke8537bd2012-01-16 12:41:48 +0100126 struct mutex wlock;
127 struct mutex rlock;
Oliver Neukumafba9372008-05-13 17:01:25 +0200128 wait_queue_head_t wait;
129 struct work_struct rxwork;
130 int werr;
131 int rerr;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100132
133 struct list_head device_list;
Bjørn Mork3cc36152012-03-06 17:29:22 +0100134 int (*manage_power)(struct usb_interface *, int);
Oliver Neukumafba9372008-05-13 17:01:25 +0200135};
136
137static struct usb_driver wdm_driver;
138
Bjørn Morkb0c13862012-03-06 17:29:21 +0100139/* return intfdata if we own the interface, else look up intf in the list */
140static struct wdm_device *wdm_find_device(struct usb_interface *intf)
141{
Bjørn Mork5527f132012-09-10 22:17:34 +0200142 struct wdm_device *desc;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100143
144 spin_lock(&wdm_device_list_lock);
145 list_for_each_entry(desc, &wdm_device_list, device_list)
146 if (desc->intf == intf)
Bjørn Mork5527f132012-09-10 22:17:34 +0200147 goto found;
148 desc = NULL;
149found:
Bjørn Morkb0c13862012-03-06 17:29:21 +0100150 spin_unlock(&wdm_device_list_lock);
151
152 return desc;
153}
154
155static struct wdm_device *wdm_find_device_by_minor(int minor)
156{
Bjørn Mork5527f132012-09-10 22:17:34 +0200157 struct wdm_device *desc;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100158
159 spin_lock(&wdm_device_list_lock);
160 list_for_each_entry(desc, &wdm_device_list, device_list)
161 if (desc->intf->minor == minor)
Bjørn Mork5527f132012-09-10 22:17:34 +0200162 goto found;
163 desc = NULL;
164found:
Bjørn Morkb0c13862012-03-06 17:29:21 +0100165 spin_unlock(&wdm_device_list_lock);
166
167 return desc;
168}
169
Oliver Neukumafba9372008-05-13 17:01:25 +0200170/* --- callbacks --- */
171static void wdm_out_callback(struct urb *urb)
172{
173 struct wdm_device *desc;
174 desc = urb->context;
175 spin_lock(&desc->iuspin);
176 desc->werr = urb->status;
177 spin_unlock(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200178 kfree(desc->outbuf);
Oliver Neukum5c228372012-04-26 21:59:10 +0200179 desc->outbuf = NULL;
180 clear_bit(WDM_IN_USE, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200181 wake_up(&desc->wait);
182}
183
184static void wdm_in_callback(struct urb *urb)
185{
186 struct wdm_device *desc = urb->context;
187 int status = urb->status;
Oliver Neukum5c6142c2013-03-12 14:52:42 +0100188 int length = urb->actual_length;
Oliver Neukumafba9372008-05-13 17:01:25 +0200189
190 spin_lock(&desc->iuspin);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100191 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200192
193 if (status) {
194 switch (status) {
195 case -ENOENT:
196 dev_dbg(&desc->intf->dev,
197 "nonzero urb status received: -ENOENT");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100198 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200199 case -ECONNRESET:
200 dev_dbg(&desc->intf->dev,
201 "nonzero urb status received: -ECONNRESET");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100202 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200203 case -ESHUTDOWN:
204 dev_dbg(&desc->intf->dev,
205 "nonzero urb status received: -ESHUTDOWN");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100206 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200207 case -EPIPE:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700208 dev_err(&desc->intf->dev,
209 "nonzero urb status received: -EPIPE\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200210 break;
211 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700212 dev_err(&desc->intf->dev,
213 "Unexpected error %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200214 break;
215 }
216 }
217
218 desc->rerr = status;
Oliver Neukum5c6142c2013-03-12 14:52:42 +0100219 if (length + desc->length > desc->wMaxCommand) {
220 /* The buffer would overflow */
221 set_bit(WDM_OVERFLOW, &desc->flags);
222 } else {
223 /* we may already be in overflow */
224 if (!test_bit(WDM_OVERFLOW, &desc->flags)) {
225 memmove(desc->ubuf + desc->length, desc->inbuf, length);
226 desc->length += length;
227 desc->reslength = length;
228 }
229 }
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100230skip_error:
Oliver Neukumafba9372008-05-13 17:01:25 +0200231 wake_up(&desc->wait);
232
233 set_bit(WDM_READ, &desc->flags);
234 spin_unlock(&desc->iuspin);
235}
236
237static void wdm_int_callback(struct urb *urb)
238{
239 int rv = 0;
Oliver Neukume200d6b2013-08-06 14:22:59 +0200240 int responding;
Oliver Neukumafba9372008-05-13 17:01:25 +0200241 int status = urb->status;
242 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200243 struct usb_cdc_notification *dr;
244
245 desc = urb->context;
Oliver Neukumafba9372008-05-13 17:01:25 +0200246 dr = (struct usb_cdc_notification *)desc->sbuf;
247
248 if (status) {
249 switch (status) {
250 case -ESHUTDOWN:
251 case -ENOENT:
252 case -ECONNRESET:
253 return; /* unplug */
254 case -EPIPE:
255 set_bit(WDM_INT_STALL, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700256 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200257 goto sw; /* halt is cleared in work */
258 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700259 dev_err(&desc->intf->dev,
260 "nonzero urb status received: %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200261 break;
262 }
263 }
264
265 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700266 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
267 urb->actual_length);
Oliver Neukumafba9372008-05-13 17:01:25 +0200268 goto exit;
269 }
270
271 switch (dr->bNotificationType) {
272 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
273 dev_dbg(&desc->intf->dev,
274 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
275 dr->wIndex, dr->wLength);
276 break;
277
278 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
279
280 dev_dbg(&desc->intf->dev,
281 "NOTIFY_NETWORK_CONNECTION %s network",
282 dr->wValue ? "connected to" : "disconnected from");
283 goto exit;
284 default:
285 clear_bit(WDM_POLL_RUNNING, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700286 dev_err(&desc->intf->dev,
287 "unknown notification %d received: index %d len %d\n",
Oliver Neukumafba9372008-05-13 17:01:25 +0200288 dr->bNotificationType, dr->wIndex, dr->wLength);
289 goto exit;
290 }
291
Oliver Neukumafba9372008-05-13 17:01:25 +0200292 spin_lock(&desc->iuspin);
293 clear_bit(WDM_READ, &desc->flags);
Oliver Neukume200d6b2013-08-06 14:22:59 +0200294 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
295 if (!responding && !test_bit(WDM_DISCONNECTING, &desc->flags)
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100296 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200297 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
298 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
299 __func__, rv);
300 }
301 spin_unlock(&desc->iuspin);
302 if (rv < 0) {
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100303 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200304 if (rv == -EPERM)
305 return;
306 if (rv == -ENOMEM) {
307sw:
308 rv = schedule_work(&desc->rxwork);
309 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700310 dev_err(&desc->intf->dev,
311 "Cannot schedule work\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200312 }
313 }
314exit:
315 rv = usb_submit_urb(urb, GFP_ATOMIC);
316 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700317 dev_err(&desc->intf->dev,
318 "%s - usb_submit_urb failed with result %d\n",
319 __func__, rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200320
321}
322
323static void kill_urbs(struct wdm_device *desc)
324{
Oliver Neukum17d80d52008-06-24 15:56:10 +0200325 /* the order here is essential */
Oliver Neukumafba9372008-05-13 17:01:25 +0200326 usb_kill_urb(desc->command);
327 usb_kill_urb(desc->validity);
328 usb_kill_urb(desc->response);
329}
330
331static void free_urbs(struct wdm_device *desc)
332{
333 usb_free_urb(desc->validity);
334 usb_free_urb(desc->response);
335 usb_free_urb(desc->command);
336}
337
338static void cleanup(struct wdm_device *desc)
339{
Bjørn Mork8457d992012-01-16 15:12:00 +0100340 kfree(desc->sbuf);
341 kfree(desc->inbuf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200342 kfree(desc->orq);
343 kfree(desc->irq);
344 kfree(desc->ubuf);
345 free_urbs(desc);
346 kfree(desc);
347}
348
349static ssize_t wdm_write
350(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
351{
352 u8 *buf;
353 int rv = -EMSGSIZE, r, we;
354 struct wdm_device *desc = file->private_data;
355 struct usb_ctrlrequest *req;
356
357 if (count > desc->wMaxCommand)
358 count = desc->wMaxCommand;
359
360 spin_lock_irq(&desc->iuspin);
361 we = desc->werr;
362 desc->werr = 0;
363 spin_unlock_irq(&desc->iuspin);
364 if (we < 0)
365 return -EIO;
366
Oliver Neukum5c228372012-04-26 21:59:10 +0200367 buf = kmalloc(count, GFP_KERNEL);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100368 if (!buf) {
369 rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200370 goto outnl;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100371 }
372
373 r = copy_from_user(buf, buffer, count);
374 if (r > 0) {
375 kfree(buf);
376 rv = -EFAULT;
377 goto outnl;
378 }
379
380 /* concurrent writes and disconnect */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100381 r = mutex_lock_interruptible(&desc->wlock);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100382 rv = -ERESTARTSYS;
383 if (r) {
384 kfree(buf);
385 goto outnl;
386 }
387
388 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
389 kfree(buf);
390 rv = -ENODEV;
391 goto outnp;
392 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200393
Oliver Neukum17d80d52008-06-24 15:56:10 +0200394 r = usb_autopm_get_interface(desc->intf);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100395 if (r < 0) {
396 kfree(buf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200397 goto outnp;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100398 }
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200399
David Sterba0cdfb812010-12-27 18:49:58 +0100400 if (!(file->f_flags & O_NONBLOCK))
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200401 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
402 &desc->flags));
403 else
404 if (test_bit(WDM_IN_USE, &desc->flags))
405 r = -EAGAIN;
Bjørn Mork88044202012-02-10 09:44:08 +0100406
407 if (test_bit(WDM_RESETTING, &desc->flags))
408 r = -EIO;
409
Oliver Neukum860e41a2010-02-27 20:54:24 +0100410 if (r < 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200411 kfree(buf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200412 goto out;
413 }
414
415 req = desc->orq;
416 usb_fill_control_urb(
417 desc->command,
418 interface_to_usbdev(desc->intf),
419 /* using common endpoint 0 */
420 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
421 (unsigned char *)req,
422 buf,
423 count,
424 wdm_out_callback,
425 desc
426 );
427
428 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
429 USB_RECIP_INTERFACE);
430 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
431 req->wValue = 0;
432 req->wIndex = desc->inum;
433 req->wLength = cpu_to_le16(count);
434 set_bit(WDM_IN_USE, &desc->flags);
Oliver Neukum5c228372012-04-26 21:59:10 +0200435 desc->outbuf = buf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200436
437 rv = usb_submit_urb(desc->command, GFP_KERNEL);
438 if (rv < 0) {
439 kfree(buf);
Oliver Neukum5c228372012-04-26 21:59:10 +0200440 desc->outbuf = NULL;
Oliver Neukumafba9372008-05-13 17:01:25 +0200441 clear_bit(WDM_IN_USE, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700442 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200443 } else {
444 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
445 req->wIndex);
446 }
447out:
Oliver Neukum17d80d52008-06-24 15:56:10 +0200448 usb_autopm_put_interface(desc->intf);
449outnp:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100450 mutex_unlock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200451outnl:
452 return rv < 0 ? rv : count;
453}
454
455static ssize_t wdm_read
456(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
457{
Ben Hutchings711c68b2012-02-12 06:00:41 +0000458 int rv, cntr;
Oliver Neukumafba9372008-05-13 17:01:25 +0200459 int i = 0;
460 struct wdm_device *desc = file->private_data;
461
462
Bjørn Morke8537bd2012-01-16 12:41:48 +0100463 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
Oliver Neukumafba9372008-05-13 17:01:25 +0200464 if (rv < 0)
465 return -ERESTARTSYS;
466
Ben Hutchings711c68b2012-02-12 06:00:41 +0000467 cntr = ACCESS_ONCE(desc->length);
468 if (cntr == 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200469 desc->read = 0;
470retry:
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200471 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
472 rv = -ENODEV;
473 goto err;
474 }
Oliver Neukum5c6142c2013-03-12 14:52:42 +0100475 if (test_bit(WDM_OVERFLOW, &desc->flags)) {
476 clear_bit(WDM_OVERFLOW, &desc->flags);
477 rv = -ENOBUFS;
478 goto err;
479 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200480 i++;
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200481 if (file->f_flags & O_NONBLOCK) {
482 if (!test_bit(WDM_READ, &desc->flags)) {
483 rv = cntr ? cntr : -EAGAIN;
484 goto err;
485 }
486 rv = 0;
487 } else {
488 rv = wait_event_interruptible(desc->wait,
489 test_bit(WDM_READ, &desc->flags));
490 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200491
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200492 /* may have happened while we slept */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200493 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
494 rv = -ENODEV;
495 goto err;
496 }
Bjørn Mork88044202012-02-10 09:44:08 +0100497 if (test_bit(WDM_RESETTING, &desc->flags)) {
498 rv = -EIO;
499 goto err;
500 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200501 usb_mark_last_busy(interface_to_usbdev(desc->intf));
Oliver Neukumafba9372008-05-13 17:01:25 +0200502 if (rv < 0) {
503 rv = -ERESTARTSYS;
504 goto err;
505 }
506
507 spin_lock_irq(&desc->iuspin);
508
509 if (desc->rerr) { /* read completed, error happened */
Oliver Neukumafba9372008-05-13 17:01:25 +0200510 desc->rerr = 0;
511 spin_unlock_irq(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200512 rv = -EIO;
513 goto err;
514 }
515 /*
516 * recheck whether we've lost the race
517 * against the completion handler
518 */
519 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
520 spin_unlock_irq(&desc->iuspin);
521 goto retry;
522 }
Oliver Neukum5c6142c2013-03-12 14:52:42 +0100523
Oliver Neukumafba9372008-05-13 17:01:25 +0200524 if (!desc->reslength) { /* zero length read */
Bjørn Morkb80f6db2012-07-02 10:33:14 +0200525 dev_dbg(&desc->intf->dev, "%s: zero length - clearing WDM_READ\n", __func__);
526 clear_bit(WDM_READ, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200527 spin_unlock_irq(&desc->iuspin);
528 goto retry;
529 }
Ben Hutchings711c68b2012-02-12 06:00:41 +0000530 cntr = desc->length;
Oliver Neukumafba9372008-05-13 17:01:25 +0200531 spin_unlock_irq(&desc->iuspin);
532 }
533
Ben Hutchings711c68b2012-02-12 06:00:41 +0000534 if (cntr > count)
535 cntr = count;
Oliver Neukumafba9372008-05-13 17:01:25 +0200536 rv = copy_to_user(buffer, desc->ubuf, cntr);
537 if (rv > 0) {
538 rv = -EFAULT;
539 goto err;
540 }
541
Ben Hutchings711c68b2012-02-12 06:00:41 +0000542 spin_lock_irq(&desc->iuspin);
543
Oliver Neukumafba9372008-05-13 17:01:25 +0200544 for (i = 0; i < desc->length - cntr; i++)
545 desc->ubuf[i] = desc->ubuf[i + cntr];
546
547 desc->length -= cntr;
Oliver Neukum87d65e52008-06-19 14:20:18 +0200548 /* in case we had outstanding data */
549 if (!desc->length)
550 clear_bit(WDM_READ, &desc->flags);
Ben Hutchings711c68b2012-02-12 06:00:41 +0000551
552 spin_unlock_irq(&desc->iuspin);
553
Oliver Neukumafba9372008-05-13 17:01:25 +0200554 rv = cntr;
555
556err:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100557 mutex_unlock(&desc->rlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200558 return rv;
559}
560
561static int wdm_flush(struct file *file, fl_owner_t id)
562{
563 struct wdm_device *desc = file->private_data;
564
565 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
Bjørn Mork577bd3e2012-05-09 13:53:22 +0200566
567 /* cannot dereference desc->intf if WDM_DISCONNECTING */
568 if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags))
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700569 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
570 desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200571
Oliver Neukumeae68612012-04-27 14:23:54 +0200572 return usb_translate_errors(desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200573}
574
575static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
576{
577 struct wdm_device *desc = file->private_data;
578 unsigned long flags;
579 unsigned int mask = 0;
580
581 spin_lock_irqsave(&desc->iuspin, flags);
flintmanc6dc72f2014-08-11 21:52:25 -0400582#ifdef CONFIG_MACH_TENDERLOIN
583 /* MBM, fixes Select() */
584 poll_wait(file, &desc->wait, wait);
585#endif
Oliver Neukumafba9372008-05-13 17:01:25 +0200586 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
Bjørn Morkd0765ea2012-05-09 13:53:21 +0200587 mask = POLLHUP | POLLERR;
Oliver Neukumafba9372008-05-13 17:01:25 +0200588 spin_unlock_irqrestore(&desc->iuspin, flags);
589 goto desc_out;
590 }
591 if (test_bit(WDM_READ, &desc->flags))
592 mask = POLLIN | POLLRDNORM;
593 if (desc->rerr || desc->werr)
594 mask |= POLLERR;
595 if (!test_bit(WDM_IN_USE, &desc->flags))
596 mask |= POLLOUT | POLLWRNORM;
597 spin_unlock_irqrestore(&desc->iuspin, flags);
flintmanc6dc72f2014-08-11 21:52:25 -0400598#ifndef CONFIG_MACH_TENDERLOIN
Oliver Neukumafba9372008-05-13 17:01:25 +0200599 poll_wait(file, &desc->wait, wait);
flintmanc6dc72f2014-08-11 21:52:25 -0400600#endif
Oliver Neukumafba9372008-05-13 17:01:25 +0200601
602desc_out:
603 return mask;
604}
605
606static int wdm_open(struct inode *inode, struct file *file)
607{
608 int minor = iminor(inode);
609 int rv = -ENODEV;
610 struct usb_interface *intf;
611 struct wdm_device *desc;
612
613 mutex_lock(&wdm_mutex);
Bjørn Morkb0c13862012-03-06 17:29:21 +0100614 desc = wdm_find_device_by_minor(minor);
615 if (!desc)
Oliver Neukumafba9372008-05-13 17:01:25 +0200616 goto out;
617
Bjørn Morkb0c13862012-03-06 17:29:21 +0100618 intf = desc->intf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200619 if (test_bit(WDM_DISCONNECTING, &desc->flags))
620 goto out;
Oliver Neukumafba9372008-05-13 17:01:25 +0200621 file->private_data = desc;
622
Oliver Neukum17d80d52008-06-24 15:56:10 +0200623 rv = usb_autopm_get_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200624 if (rv < 0) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700625 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200626 goto out;
627 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200628
Bjørn Morke8537bd2012-01-16 12:41:48 +0100629 /* using write lock to protect desc->count */
630 mutex_lock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200631 if (!desc->count++) {
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200632 desc->werr = 0;
633 desc->rerr = 0;
Oliver Neukum17d80d52008-06-24 15:56:10 +0200634 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
635 if (rv < 0) {
636 desc->count--;
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700637 dev_err(&desc->intf->dev,
638 "Error submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200639 }
640 } else {
641 rv = 0;
642 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100643 mutex_unlock(&desc->wlock);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100644 if (desc->count == 1)
645 desc->manage_power(intf, 1);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200646 usb_autopm_put_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200647out:
648 mutex_unlock(&wdm_mutex);
649 return rv;
650}
651
652static int wdm_release(struct inode *inode, struct file *file)
653{
654 struct wdm_device *desc = file->private_data;
655
656 mutex_lock(&wdm_mutex);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100657
658 /* using write lock to protect desc->count */
659 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200660 desc->count--;
Bjørn Morke8537bd2012-01-16 12:41:48 +0100661 mutex_unlock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200662
Oliver Neukumafba9372008-05-13 17:01:25 +0200663 if (!desc->count) {
Bjørn Mork577bd3e2012-05-09 13:53:22 +0200664 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
665 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
666 kill_urbs(desc);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100667 desc->manage_power(desc->intf, 0);
Bjørn Mork577bd3e2012-05-09 13:53:22 +0200668 } else {
669 /* must avoid dev_printk here as desc->intf is invalid */
670 pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
Oliver Neukumfccfda62012-04-27 14:36:37 +0200671 cleanup(desc);
Bjørn Mork577bd3e2012-05-09 13:53:22 +0200672 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200673 }
674 mutex_unlock(&wdm_mutex);
675 return 0;
676}
677
678static const struct file_operations wdm_fops = {
679 .owner = THIS_MODULE,
680 .read = wdm_read,
681 .write = wdm_write,
682 .open = wdm_open,
683 .flush = wdm_flush,
684 .release = wdm_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200685 .poll = wdm_poll,
686 .llseek = noop_llseek,
Oliver Neukumafba9372008-05-13 17:01:25 +0200687};
688
689static struct usb_class_driver wdm_class = {
690 .name = "cdc-wdm%d",
691 .fops = &wdm_fops,
692 .minor_base = WDM_MINOR_BASE,
693};
694
695/* --- error handling --- */
696static void wdm_rxwork(struct work_struct *work)
697{
698 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
699 unsigned long flags;
Oliver Neukume200d6b2013-08-06 14:22:59 +0200700 int rv = 0;
701 int responding;
Oliver Neukumafba9372008-05-13 17:01:25 +0200702
703 spin_lock_irqsave(&desc->iuspin, flags);
704 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
705 spin_unlock_irqrestore(&desc->iuspin, flags);
706 } else {
Oliver Neukume200d6b2013-08-06 14:22:59 +0200707 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200708 spin_unlock_irqrestore(&desc->iuspin, flags);
Oliver Neukume200d6b2013-08-06 14:22:59 +0200709 if (!responding)
710 rv = usb_submit_urb(desc->response, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200711 if (rv < 0 && rv != -EPERM) {
712 spin_lock_irqsave(&desc->iuspin, flags);
Oliver Neukume200d6b2013-08-06 14:22:59 +0200713 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200714 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
715 schedule_work(&desc->rxwork);
716 spin_unlock_irqrestore(&desc->iuspin, flags);
717 }
718 }
719}
720
721/* --- hotplug --- */
722
Bjørn Mork3cc36152012-03-06 17:29:22 +0100723static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
724 u16 bufsize, int (*manage_power)(struct usb_interface *, int))
Oliver Neukumafba9372008-05-13 17:01:25 +0200725{
Bjørn Mork0dffb482012-03-06 17:29:20 +0100726 int rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200727 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200728
Oliver Neukumafba9372008-05-13 17:01:25 +0200729 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
730 if (!desc)
731 goto out;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100732 INIT_LIST_HEAD(&desc->device_list);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100733 mutex_init(&desc->rlock);
734 mutex_init(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200735 spin_lock_init(&desc->iuspin);
736 init_waitqueue_head(&desc->wait);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100737 desc->wMaxCommand = bufsize;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200738 /* this will be expanded and needed in hardware endianness */
Oliver Neukumafba9372008-05-13 17:01:25 +0200739 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
740 desc->intf = intf;
741 INIT_WORK(&desc->rxwork, wdm_rxwork);
742
Oliver Neukum052fbc02009-04-20 17:24:49 +0200743 rv = -EINVAL;
Bjørn Mork0dffb482012-03-06 17:29:20 +0100744 if (!usb_endpoint_is_int_in(ep))
Oliver Neukum052fbc02009-04-20 17:24:49 +0200745 goto err;
flintmanc6dc72f2014-08-11 21:52:25 -0400746#ifndef CONFIG_MACH_TENDERLOIN
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700747 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
flintmanc6dc72f2014-08-11 21:52:25 -0400748#else
749 desc->wMaxPacketSize = le16_to_cpu(ep->wMaxPacketSize);
750#endif
Oliver Neukumafba9372008-05-13 17:01:25 +0200751 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
752 if (!desc->orq)
753 goto err;
754 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
755 if (!desc->irq)
756 goto err;
757
758 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
759 if (!desc->validity)
760 goto err;
761
762 desc->response = usb_alloc_urb(0, GFP_KERNEL);
763 if (!desc->response)
764 goto err;
765
766 desc->command = usb_alloc_urb(0, GFP_KERNEL);
767 if (!desc->command)
768 goto err;
769
770 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
771 if (!desc->ubuf)
772 goto err;
773
Bjørn Mork8457d992012-01-16 15:12:00 +0100774 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200775 if (!desc->sbuf)
776 goto err;
777
Bjørn Mork8457d992012-01-16 15:12:00 +0100778 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200779 if (!desc->inbuf)
Bjørn Mork8457d992012-01-16 15:12:00 +0100780 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200781
782 usb_fill_int_urb(
783 desc->validity,
784 interface_to_usbdev(intf),
785 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
786 desc->sbuf,
787 desc->wMaxPacketSize,
788 wdm_int_callback,
789 desc,
790 ep->bInterval
791 );
Oliver Neukumafba9372008-05-13 17:01:25 +0200792
Bjørn Mork19b85b32012-01-16 15:11:58 +0100793 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
794 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
795 desc->irq->wValue = 0;
796 desc->irq->wIndex = desc->inum;
797 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
798
799 usb_fill_control_urb(
800 desc->response,
Bjørn Mork8143a892012-01-16 15:12:01 +0100801 interface_to_usbdev(intf),
Bjørn Mork19b85b32012-01-16 15:11:58 +0100802 /* using common endpoint 0 */
803 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
804 (unsigned char *)desc->irq,
805 desc->inbuf,
806 desc->wMaxCommand,
807 wdm_in_callback,
808 desc
809 );
Bjørn Mork19b85b32012-01-16 15:11:58 +0100810
Bjørn Mork3cc36152012-03-06 17:29:22 +0100811 desc->manage_power = manage_power;
812
Bjørn Morkb0c13862012-03-06 17:29:21 +0100813 spin_lock(&wdm_device_list_lock);
814 list_add(&desc->device_list, &wdm_device_list);
815 spin_unlock(&wdm_device_list_lock);
816
Oliver Neukumafba9372008-05-13 17:01:25 +0200817 rv = usb_register_dev(intf, &wdm_class);
Oliver Neukumafba9372008-05-13 17:01:25 +0200818 if (rv < 0)
Bjørn Morkb0c13862012-03-06 17:29:21 +0100819 goto err;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200820 else
Bjørn Mork820c6292012-01-20 04:17:25 +0100821 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
Oliver Neukumafba9372008-05-13 17:01:25 +0200822out:
823 return rv;
Oliver Neukumafba9372008-05-13 17:01:25 +0200824err:
Bjørn Mork9ed2cb72012-05-09 13:53:23 +0200825 spin_lock(&wdm_device_list_lock);
826 list_del(&desc->device_list);
827 spin_unlock(&wdm_device_list_lock);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100828 cleanup(desc);
829 return rv;
830}
831
Bjørn Mork3cc36152012-03-06 17:29:22 +0100832static int wdm_manage_power(struct usb_interface *intf, int on)
833{
834 /* need autopm_get/put here to ensure the usbcore sees the new value */
835 int rv = usb_autopm_get_interface(intf);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100836
837 intf->needs_remote_wakeup = on;
Bjørn Morkbc8a3912013-11-29 20:17:45 +0100838 if (!rv)
839 usb_autopm_put_interface(intf);
840 return 0;
Bjørn Mork3cc36152012-03-06 17:29:22 +0100841}
842
Bjørn Mork0dffb482012-03-06 17:29:20 +0100843static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
844{
845 int rv = -EINVAL;
846 struct usb_host_interface *iface;
847 struct usb_endpoint_descriptor *ep;
848 struct usb_cdc_dmm_desc *dmhd;
849 u8 *buffer = intf->altsetting->extra;
850 int buflen = intf->altsetting->extralen;
851 u16 maxcom = WDM_DEFAULT_BUFSIZE;
852
853 if (!buffer)
854 goto err;
855 while (buflen > 2) {
856 if (buffer[1] != USB_DT_CS_INTERFACE) {
857 dev_err(&intf->dev, "skipping garbage\n");
858 goto next_desc;
859 }
860
861 switch (buffer[2]) {
862 case USB_CDC_HEADER_TYPE:
863 break;
864 case USB_CDC_DMM_TYPE:
865 dmhd = (struct usb_cdc_dmm_desc *)buffer;
866 maxcom = le16_to_cpu(dmhd->wMaxCommand);
867 dev_dbg(&intf->dev,
868 "Finding maximum buffer length: %d", maxcom);
869 break;
870 default:
871 dev_err(&intf->dev,
872 "Ignoring extra header, type %d, length %d\n",
873 buffer[2], buffer[0]);
874 break;
875 }
876next_desc:
877 buflen -= buffer[0];
878 buffer += buffer[0];
879 }
880
881 iface = intf->cur_altsetting;
882 if (iface->desc.bNumEndpoints != 1)
883 goto err;
884 ep = &iface->endpoint[0].desc;
885
Bjørn Mork3cc36152012-03-06 17:29:22 +0100886 rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100887
888err:
Oliver Neukumafba9372008-05-13 17:01:25 +0200889 return rv;
890}
891
Bjørn Mork3cc36152012-03-06 17:29:22 +0100892/**
893 * usb_cdc_wdm_register - register a WDM subdriver
894 * @intf: usb interface the subdriver will associate with
895 * @ep: interrupt endpoint to monitor for notifications
896 * @bufsize: maximum message size to support for read/write
897 *
898 * Create WDM usb class character device and associate it with intf
899 * without binding, allowing another driver to manage the interface.
900 *
901 * The subdriver will manage the given interrupt endpoint exclusively
902 * and will issue control requests referring to the given intf. It
903 * will otherwise avoid interferring, and in particular not do
904 * usb_set_intfdata/usb_get_intfdata on intf.
905 *
906 * The return value is a pointer to the subdriver's struct usb_driver.
907 * The registering driver is responsible for calling this subdriver's
908 * disconnect, suspend, resume, pre_reset and post_reset methods from
909 * its own.
910 */
911struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
912 struct usb_endpoint_descriptor *ep,
913 int bufsize,
914 int (*manage_power)(struct usb_interface *, int))
915{
916 int rv = -EINVAL;
917
918 rv = wdm_create(intf, ep, bufsize, manage_power);
919 if (rv < 0)
920 goto err;
921
922 return &wdm_driver;
923err:
924 return ERR_PTR(rv);
925}
926EXPORT_SYMBOL(usb_cdc_wdm_register);
927
Oliver Neukumafba9372008-05-13 17:01:25 +0200928static void wdm_disconnect(struct usb_interface *intf)
929{
930 struct wdm_device *desc;
931 unsigned long flags;
932
933 usb_deregister_dev(intf, &wdm_class);
Bjørn Morkb0c13862012-03-06 17:29:21 +0100934 desc = wdm_find_device(intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200935 mutex_lock(&wdm_mutex);
Oliver Neukumafba9372008-05-13 17:01:25 +0200936
937 /* the spinlock makes sure no new urbs are generated in the callbacks */
938 spin_lock_irqsave(&desc->iuspin, flags);
939 set_bit(WDM_DISCONNECTING, &desc->flags);
940 set_bit(WDM_READ, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200941 /* to terminate pending flushes */
Oliver Neukumafba9372008-05-13 17:01:25 +0200942 clear_bit(WDM_IN_USE, &desc->flags);
943 spin_unlock_irqrestore(&desc->iuspin, flags);
Bjørn Mork62aaf242012-01-16 15:11:57 +0100944 wake_up_all(&desc->wait);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100945 mutex_lock(&desc->rlock);
946 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200947 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100948 cancel_work_sync(&desc->rxwork);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100949 mutex_unlock(&desc->wlock);
950 mutex_unlock(&desc->rlock);
Bjørn Mork9ed2cb72012-05-09 13:53:23 +0200951
952 /* the desc->intf pointer used as list key is now invalid */
953 spin_lock(&wdm_device_list_lock);
954 list_del(&desc->device_list);
955 spin_unlock(&wdm_device_list_lock);
956
Oliver Neukumafba9372008-05-13 17:01:25 +0200957 if (!desc->count)
958 cleanup(desc);
959 mutex_unlock(&wdm_mutex);
960}
961
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100962#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200963static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
964{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100965 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200966 int rv = 0;
967
968 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
969
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100970 /* if this is an autosuspend the caller does the locking */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100971 if (!PMSG_IS_AUTO(message)) {
972 mutex_lock(&desc->rlock);
973 mutex_lock(&desc->wlock);
974 }
Oliver Neukum62e66852010-02-27 20:56:22 +0100975 spin_lock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100976
Alan Stern5b1b0b82011-08-19 23:49:48 +0200977 if (PMSG_IS_AUTO(message) &&
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100978 (test_bit(WDM_IN_USE, &desc->flags)
979 || test_bit(WDM_RESPONDING, &desc->flags))) {
Oliver Neukum62e66852010-02-27 20:56:22 +0100980 spin_unlock_irq(&desc->iuspin);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200981 rv = -EBUSY;
982 } else {
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100983
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100984 set_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100985 spin_unlock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100986 /* callback submits work - order is essential */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200987 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100988 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200989 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100990 if (!PMSG_IS_AUTO(message)) {
991 mutex_unlock(&desc->wlock);
992 mutex_unlock(&desc->rlock);
993 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200994
995 return rv;
996}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100997#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200998
999static int recover_from_urb_loss(struct wdm_device *desc)
1000{
1001 int rv = 0;
1002
1003 if (desc->count) {
1004 rv = usb_submit_urb(desc->validity, GFP_NOIO);
1005 if (rv < 0)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -07001006 dev_err(&desc->intf->dev,
1007 "Error resume submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001008 }
1009 return rv;
1010}
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001011
1012#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +02001013static int wdm_resume(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 int rv;
1017
1018 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
Oliver Neukum338124c2010-02-27 20:57:12 +01001019
Oliver Neukumbeb1d352010-02-27 20:55:52 +01001020 clear_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +01001021 rv = recover_from_urb_loss(desc);
Oliver Neukum338124c2010-02-27 20:57:12 +01001022
Oliver Neukum17d80d52008-06-24 15:56:10 +02001023 return rv;
1024}
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001025#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +02001026
1027static int wdm_pre_reset(struct usb_interface *intf)
1028{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001029 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001030
Oliver Neukumd771d8a2011-04-29 14:12:21 +02001031 /*
1032 * we notify everybody using poll of
1033 * an exceptional situation
1034 * must be done before recovery lest a spontaneous
1035 * message from the device is lost
1036 */
1037 spin_lock_irq(&desc->iuspin);
Bjørn Mork88044202012-02-10 09:44:08 +01001038 set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
1039 set_bit(WDM_READ, &desc->flags); /* unblock read */
1040 clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
Oliver Neukumd771d8a2011-04-29 14:12:21 +02001041 desc->rerr = -EINTR;
1042 spin_unlock_irq(&desc->iuspin);
1043 wake_up_all(&desc->wait);
Bjørn Mork88044202012-02-10 09:44:08 +01001044 mutex_lock(&desc->rlock);
1045 mutex_lock(&desc->wlock);
1046 kill_urbs(desc);
1047 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001048 return 0;
1049}
1050
1051static int wdm_post_reset(struct usb_interface *intf)
1052{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001053 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001054 int rv;
1055
flintmanc6dc72f2014-08-11 21:52:25 -04001056#ifndef CONFIG_MACH_TENDERLOIN
Oliver Neukum5c6142c2013-03-12 14:52:42 +01001057 clear_bit(WDM_OVERFLOW, &desc->flags);
flintmanc6dc72f2014-08-11 21:52:25 -04001058#endif
Bjørn Mork88044202012-02-10 09:44:08 +01001059 clear_bit(WDM_RESETTING, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001060 rv = recover_from_urb_loss(desc);
Bjørn Morke8537bd2012-01-16 12:41:48 +01001061 mutex_unlock(&desc->wlock);
1062 mutex_unlock(&desc->rlock);
flintmanc6dc72f2014-08-11 21:52:25 -04001063 return rv;
Oliver Neukum17d80d52008-06-24 15:56:10 +02001064}
1065
Oliver Neukumafba9372008-05-13 17:01:25 +02001066static struct usb_driver wdm_driver = {
1067 .name = "cdc_wdm",
1068 .probe = wdm_probe,
1069 .disconnect = wdm_disconnect,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001070#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +02001071 .suspend = wdm_suspend,
1072 .resume = wdm_resume,
1073 .reset_resume = wdm_resume,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001074#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +02001075 .pre_reset = wdm_pre_reset,
1076 .post_reset = wdm_post_reset,
Oliver Neukumafba9372008-05-13 17:01:25 +02001077 .id_table = wdm_ids,
Oliver Neukum17d80d52008-06-24 15:56:10 +02001078 .supports_autosuspend = 1,
Oliver Neukumafba9372008-05-13 17:01:25 +02001079};
1080
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001081module_usb_driver(wdm_driver);
Oliver Neukumafba9372008-05-13 17:01:25 +02001082
1083MODULE_AUTHOR(DRIVER_AUTHOR);
Oliver Neukum87d65e52008-06-19 14:20:18 +02001084MODULE_DESCRIPTION(DRIVER_DESC);
Oliver Neukumafba9372008-05-13 17:01:25 +02001085MODULE_LICENSE("GPL");