]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/usb/class/cdc-wdm.c
USB: cdc-wdm: poll must return POLLHUP if device is gone
[mirror_ubuntu-artful-kernel.git] / drivers / usb / class / cdc-wdm.c
CommitLineData
afba937e
ON
1/*
2 * cdc-wdm.c
3 *
4 * This driver supports USB CDC WCM Device Management.
5 *
052fbc0d 6 * Copyright (c) 2007-2009 Oliver Neukum
afba937e
ON
7 *
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>
afba937e
ON
18#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>
3cc36157 26#include <linux/usb/cdc-wdm.h>
afba937e
ON
27
28/*
29 * Version Information
30 */
87d65e54 31#define DRIVER_VERSION "v0.03"
afba937e 32#define DRIVER_AUTHOR "Oliver Neukum"
87d65e54 33#define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management"
afba937e 34
fec67b45
BM
35#define HUAWEI_VENDOR_ID 0x12D1
36
6ef4852b 37static const struct usb_device_id wdm_ids[] = {
afba937e
ON
38 {
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 },
fec67b45
BM
44 {
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 },
afba937e
ON
58 { }
59};
60
aa5380b9
ON
61MODULE_DEVICE_TABLE (usb, wdm_ids);
62
afba937e
ON
63#define WDM_MINOR_BASE 176
64
65
66#define WDM_IN_USE 1
67#define WDM_DISCONNECTING 2
68#define WDM_RESULT 3
69#define WDM_READ 4
70#define WDM_INT_STALL 5
71#define WDM_POLL_RUNNING 6
922a5ead 72#define WDM_RESPONDING 7
beb1d35f 73#define WDM_SUSPENDING 8
88044202 74#define WDM_RESETTING 9
afba937e
ON
75
76#define WDM_MAX 16
77
7e3054a0
BM
78/* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
79#define WDM_DEFAULT_BUFSIZE 256
afba937e
ON
80
81static DEFINE_MUTEX(wdm_mutex);
b0c13860
BM
82static DEFINE_SPINLOCK(wdm_device_list_lock);
83static LIST_HEAD(wdm_device_list);
afba937e
ON
84
85/* --- method tables --- */
86
87struct wdm_device {
88 u8 *inbuf; /* buffer for response */
89 u8 *outbuf; /* buffer for command */
90 u8 *sbuf; /* buffer for status */
91 u8 *ubuf; /* buffer for copy to user space */
92
93 struct urb *command;
94 struct urb *response;
95 struct urb *validity;
96 struct usb_interface *intf;
97 struct usb_ctrlrequest *orq;
98 struct usb_ctrlrequest *irq;
99 spinlock_t iuspin;
100
101 unsigned long flags;
102 u16 bufsize;
103 u16 wMaxCommand;
104 u16 wMaxPacketSize;
afba937e
ON
105 __le16 inum;
106 int reslength;
107 int length;
108 int read;
109 int count;
110 dma_addr_t shandle;
111 dma_addr_t ihandle;
e8537bd2
BM
112 struct mutex wlock;
113 struct mutex rlock;
afba937e
ON
114 wait_queue_head_t wait;
115 struct work_struct rxwork;
116 int werr;
117 int rerr;
b0c13860
BM
118
119 struct list_head device_list;
3cc36157 120 int (*manage_power)(struct usb_interface *, int);
afba937e
ON
121};
122
123static struct usb_driver wdm_driver;
124
b0c13860
BM
125/* return intfdata if we own the interface, else look up intf in the list */
126static struct wdm_device *wdm_find_device(struct usb_interface *intf)
127{
128 struct wdm_device *desc = NULL;
129
130 spin_lock(&wdm_device_list_lock);
131 list_for_each_entry(desc, &wdm_device_list, device_list)
132 if (desc->intf == intf)
133 break;
134 spin_unlock(&wdm_device_list_lock);
135
136 return desc;
137}
138
139static struct wdm_device *wdm_find_device_by_minor(int minor)
140{
141 struct wdm_device *desc = NULL;
142
143 spin_lock(&wdm_device_list_lock);
144 list_for_each_entry(desc, &wdm_device_list, device_list)
145 if (desc->intf->minor == minor)
146 break;
147 spin_unlock(&wdm_device_list_lock);
148
149 return desc;
150}
151
afba937e
ON
152/* --- callbacks --- */
153static void wdm_out_callback(struct urb *urb)
154{
155 struct wdm_device *desc;
156 desc = urb->context;
157 spin_lock(&desc->iuspin);
158 desc->werr = urb->status;
159 spin_unlock(&desc->iuspin);
afba937e 160 kfree(desc->outbuf);
5c22837a
ON
161 desc->outbuf = NULL;
162 clear_bit(WDM_IN_USE, &desc->flags);
afba937e
ON
163 wake_up(&desc->wait);
164}
165
166static void wdm_in_callback(struct urb *urb)
167{
168 struct wdm_device *desc = urb->context;
169 int status = urb->status;
170
171 spin_lock(&desc->iuspin);
922a5ead 172 clear_bit(WDM_RESPONDING, &desc->flags);
afba937e
ON
173
174 if (status) {
175 switch (status) {
176 case -ENOENT:
177 dev_dbg(&desc->intf->dev,
178 "nonzero urb status received: -ENOENT");
922a5ead 179 goto skip_error;
afba937e
ON
180 case -ECONNRESET:
181 dev_dbg(&desc->intf->dev,
182 "nonzero urb status received: -ECONNRESET");
922a5ead 183 goto skip_error;
afba937e
ON
184 case -ESHUTDOWN:
185 dev_dbg(&desc->intf->dev,
186 "nonzero urb status received: -ESHUTDOWN");
922a5ead 187 goto skip_error;
afba937e 188 case -EPIPE:
9908a32e
GKH
189 dev_err(&desc->intf->dev,
190 "nonzero urb status received: -EPIPE\n");
afba937e
ON
191 break;
192 default:
9908a32e
GKH
193 dev_err(&desc->intf->dev,
194 "Unexpected error %d\n", status);
afba937e
ON
195 break;
196 }
197 }
198
199 desc->rerr = status;
200 desc->reslength = urb->actual_length;
201 memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength);
202 desc->length += desc->reslength;
922a5ead 203skip_error:
afba937e
ON
204 wake_up(&desc->wait);
205
206 set_bit(WDM_READ, &desc->flags);
207 spin_unlock(&desc->iuspin);
208}
209
210static void wdm_int_callback(struct urb *urb)
211{
212 int rv = 0;
213 int status = urb->status;
214 struct wdm_device *desc;
afba937e
ON
215 struct usb_cdc_notification *dr;
216
217 desc = urb->context;
afba937e
ON
218 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);
9908a32e 228 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
afba937e
ON
229 goto sw; /* halt is cleared in work */
230 default:
9908a32e
GKH
231 dev_err(&desc->intf->dev,
232 "nonzero urb status received: %d\n", status);
afba937e
ON
233 break;
234 }
235 }
236
237 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
9908a32e
GKH
238 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
239 urb->actual_length);
afba937e
ON
240 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);
9908a32e
GKH
258 dev_err(&desc->intf->dev,
259 "unknown notification %d received: index %d len %d\n",
afba937e
ON
260 dr->bNotificationType, dr->wIndex, dr->wLength);
261 goto exit;
262 }
263
afba937e
ON
264 spin_lock(&desc->iuspin);
265 clear_bit(WDM_READ, &desc->flags);
922a5ead 266 set_bit(WDM_RESPONDING, &desc->flags);
beb1d35f
ON
267 if (!test_bit(WDM_DISCONNECTING, &desc->flags)
268 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
afba937e
ON
269 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) {
922a5ead 275 clear_bit(WDM_RESPONDING, &desc->flags);
afba937e
ON
276 if (rv == -EPERM)
277 return;
278 if (rv == -ENOMEM) {
279sw:
280 rv = schedule_work(&desc->rxwork);
281 if (rv)
9908a32e
GKH
282 dev_err(&desc->intf->dev,
283 "Cannot schedule work\n");
afba937e
ON
284 }
285 }
286exit:
287 rv = usb_submit_urb(urb, GFP_ATOMIC);
288 if (rv)
9908a32e
GKH
289 dev_err(&desc->intf->dev,
290 "%s - usb_submit_urb failed with result %d\n",
291 __func__, rv);
afba937e
ON
292
293}
294
295static void kill_urbs(struct wdm_device *desc)
296{
17d80d56 297 /* the order here is essential */
afba937e
ON
298 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{
b0c13860
BM
312 spin_lock(&wdm_device_list_lock);
313 list_del(&desc->device_list);
314 spin_unlock(&wdm_device_list_lock);
8457d99c
BM
315 kfree(desc->sbuf);
316 kfree(desc->inbuf);
afba937e
ON
317 kfree(desc->orq);
318 kfree(desc->irq);
319 kfree(desc->ubuf);
320 free_urbs(desc);
321 kfree(desc);
322}
323
324static ssize_t wdm_write
325(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
326{
327 u8 *buf;
328 int rv = -EMSGSIZE, r, we;
329 struct wdm_device *desc = file->private_data;
330 struct usb_ctrlrequest *req;
331
332 if (count > desc->wMaxCommand)
333 count = desc->wMaxCommand;
334
335 spin_lock_irq(&desc->iuspin);
336 we = desc->werr;
337 desc->werr = 0;
338 spin_unlock_irq(&desc->iuspin);
339 if (we < 0)
340 return -EIO;
341
5c22837a 342 buf = kmalloc(count, GFP_KERNEL);
860e41a7
ON
343 if (!buf) {
344 rv = -ENOMEM;
345 goto outnl;
346 }
347
348 r = copy_from_user(buf, buffer, count);
349 if (r > 0) {
350 kfree(buf);
351 rv = -EFAULT;
352 goto outnl;
353 }
354
355 /* concurrent writes and disconnect */
e8537bd2 356 r = mutex_lock_interruptible(&desc->wlock);
afba937e 357 rv = -ERESTARTSYS;
860e41a7
ON
358 if (r) {
359 kfree(buf);
afba937e 360 goto outnl;
860e41a7
ON
361 }
362
363 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
364 kfree(buf);
365 rv = -ENODEV;
366 goto outnp;
367 }
afba937e 368
17d80d56 369 r = usb_autopm_get_interface(desc->intf);
860e41a7
ON
370 if (r < 0) {
371 kfree(buf);
12a98b2b 372 rv = usb_translate_errors(r);
17d80d56 373 goto outnp;
860e41a7 374 }
7f1dc313 375
0cdfb819 376 if (!(file->f_flags & O_NONBLOCK))
7f1dc313
ON
377 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
378 &desc->flags));
379 else
380 if (test_bit(WDM_IN_USE, &desc->flags))
381 r = -EAGAIN;
88044202
BM
382
383 if (test_bit(WDM_RESETTING, &desc->flags))
384 r = -EIO;
385
860e41a7 386 if (r < 0) {
afba937e 387 kfree(buf);
12a98b2b 388 rv = r;
afba937e
ON
389 goto out;
390 }
391
392 req = desc->orq;
393 usb_fill_control_urb(
394 desc->command,
395 interface_to_usbdev(desc->intf),
396 /* using common endpoint 0 */
397 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
398 (unsigned char *)req,
399 buf,
400 count,
401 wdm_out_callback,
402 desc
403 );
404
405 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
406 USB_RECIP_INTERFACE);
407 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
408 req->wValue = 0;
409 req->wIndex = desc->inum;
410 req->wLength = cpu_to_le16(count);
411 set_bit(WDM_IN_USE, &desc->flags);
5c22837a 412 desc->outbuf = buf;
afba937e
ON
413
414 rv = usb_submit_urb(desc->command, GFP_KERNEL);
415 if (rv < 0) {
416 kfree(buf);
5c22837a 417 desc->outbuf = NULL;
afba937e 418 clear_bit(WDM_IN_USE, &desc->flags);
9908a32e 419 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
12a98b2b 420 rv = usb_translate_errors(rv);
afba937e
ON
421 } else {
422 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
423 req->wIndex);
424 }
425out:
17d80d56
ON
426 usb_autopm_put_interface(desc->intf);
427outnp:
e8537bd2 428 mutex_unlock(&desc->wlock);
afba937e
ON
429outnl:
430 return rv < 0 ? rv : count;
431}
432
433static ssize_t wdm_read
434(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
435{
711c68b3 436 int rv, cntr;
afba937e
ON
437 int i = 0;
438 struct wdm_device *desc = file->private_data;
439
440
e8537bd2 441 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
afba937e
ON
442 if (rv < 0)
443 return -ERESTARTSYS;
444
711c68b3
BH
445 cntr = ACCESS_ONCE(desc->length);
446 if (cntr == 0) {
afba937e
ON
447 desc->read = 0;
448retry:
7f1dc313
ON
449 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
450 rv = -ENODEV;
451 goto err;
452 }
afba937e 453 i++;
7f1dc313
ON
454 if (file->f_flags & O_NONBLOCK) {
455 if (!test_bit(WDM_READ, &desc->flags)) {
456 rv = cntr ? cntr : -EAGAIN;
457 goto err;
458 }
459 rv = 0;
460 } else {
461 rv = wait_event_interruptible(desc->wait,
462 test_bit(WDM_READ, &desc->flags));
463 }
afba937e 464
7f1dc313 465 /* may have happened while we slept */
17d80d56
ON
466 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
467 rv = -ENODEV;
468 goto err;
469 }
88044202
BM
470 if (test_bit(WDM_RESETTING, &desc->flags)) {
471 rv = -EIO;
472 goto err;
473 }
17d80d56 474 usb_mark_last_busy(interface_to_usbdev(desc->intf));
afba937e
ON
475 if (rv < 0) {
476 rv = -ERESTARTSYS;
477 goto err;
478 }
479
480 spin_lock_irq(&desc->iuspin);
481
482 if (desc->rerr) { /* read completed, error happened */
afba937e
ON
483 desc->rerr = 0;
484 spin_unlock_irq(&desc->iuspin);
afba937e
ON
485 rv = -EIO;
486 goto err;
487 }
488 /*
489 * recheck whether we've lost the race
490 * against the completion handler
491 */
492 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
493 spin_unlock_irq(&desc->iuspin);
494 goto retry;
495 }
496 if (!desc->reslength) { /* zero length read */
497 spin_unlock_irq(&desc->iuspin);
498 goto retry;
499 }
711c68b3 500 cntr = desc->length;
afba937e
ON
501 spin_unlock_irq(&desc->iuspin);
502 }
503
711c68b3
BH
504 if (cntr > count)
505 cntr = count;
afba937e
ON
506 rv = copy_to_user(buffer, desc->ubuf, cntr);
507 if (rv > 0) {
508 rv = -EFAULT;
509 goto err;
510 }
511
711c68b3
BH
512 spin_lock_irq(&desc->iuspin);
513
afba937e
ON
514 for (i = 0; i < desc->length - cntr; i++)
515 desc->ubuf[i] = desc->ubuf[i + cntr];
516
517 desc->length -= cntr;
87d65e54
ON
518 /* in case we had outstanding data */
519 if (!desc->length)
520 clear_bit(WDM_READ, &desc->flags);
711c68b3
BH
521
522 spin_unlock_irq(&desc->iuspin);
523
afba937e
ON
524 rv = cntr;
525
526err:
e8537bd2 527 mutex_unlock(&desc->rlock);
afba937e
ON
528 return rv;
529}
530
531static int wdm_flush(struct file *file, fl_owner_t id)
532{
533 struct wdm_device *desc = file->private_data;
534
535 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
536 if (desc->werr < 0)
9908a32e
GKH
537 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
538 desc->werr);
afba937e 539
24a85bae 540 return usb_translate_errors(desc->werr);
afba937e
ON
541}
542
543static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
544{
545 struct wdm_device *desc = file->private_data;
546 unsigned long flags;
547 unsigned int mask = 0;
548
549 spin_lock_irqsave(&desc->iuspin, flags);
550 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
616b6937 551 mask = POLLHUP | POLLERR;
afba937e
ON
552 spin_unlock_irqrestore(&desc->iuspin, flags);
553 goto desc_out;
554 }
555 if (test_bit(WDM_READ, &desc->flags))
556 mask = POLLIN | POLLRDNORM;
557 if (desc->rerr || desc->werr)
558 mask |= POLLERR;
559 if (!test_bit(WDM_IN_USE, &desc->flags))
560 mask |= POLLOUT | POLLWRNORM;
561 spin_unlock_irqrestore(&desc->iuspin, flags);
562
563 poll_wait(file, &desc->wait, wait);
564
565desc_out:
566 return mask;
567}
568
569static int wdm_open(struct inode *inode, struct file *file)
570{
571 int minor = iminor(inode);
572 int rv = -ENODEV;
573 struct usb_interface *intf;
574 struct wdm_device *desc;
575
576 mutex_lock(&wdm_mutex);
b0c13860
BM
577 desc = wdm_find_device_by_minor(minor);
578 if (!desc)
afba937e
ON
579 goto out;
580
b0c13860 581 intf = desc->intf;
afba937e
ON
582 if (test_bit(WDM_DISCONNECTING, &desc->flags))
583 goto out;
afba937e
ON
584 file->private_data = desc;
585
17d80d56 586 rv = usb_autopm_get_interface(desc->intf);
afba937e 587 if (rv < 0) {
9908a32e 588 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
afba937e
ON
589 goto out;
590 }
afba937e 591
e8537bd2
BM
592 /* using write lock to protect desc->count */
593 mutex_lock(&desc->wlock);
17d80d56 594 if (!desc->count++) {
d771d8aa
ON
595 desc->werr = 0;
596 desc->rerr = 0;
17d80d56
ON
597 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
598 if (rv < 0) {
599 desc->count--;
9908a32e
GKH
600 dev_err(&desc->intf->dev,
601 "Error submitting int urb - %d\n", rv);
12a98b2b 602 rv = usb_translate_errors(rv);
17d80d56
ON
603 }
604 } else {
605 rv = 0;
606 }
e8537bd2 607 mutex_unlock(&desc->wlock);
3cc36157
BM
608 if (desc->count == 1)
609 desc->manage_power(intf, 1);
17d80d56 610 usb_autopm_put_interface(desc->intf);
afba937e
ON
611out:
612 mutex_unlock(&wdm_mutex);
613 return rv;
614}
615
616static int wdm_release(struct inode *inode, struct file *file)
617{
618 struct wdm_device *desc = file->private_data;
619
620 mutex_lock(&wdm_mutex);
e8537bd2
BM
621
622 /* using write lock to protect desc->count */
623 mutex_lock(&desc->wlock);
afba937e 624 desc->count--;
e8537bd2 625 mutex_unlock(&desc->wlock);
17d80d56 626
afba937e
ON
627 if (!desc->count) {
628 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
629 kill_urbs(desc);
880bca3a 630 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
3cc36157 631 desc->manage_power(desc->intf, 0);
880bca3a
BM
632 } else {
633 dev_dbg(&desc->intf->dev, "%s: device gone - cleaning up\n", __func__);
2f338c8a 634 cleanup(desc);
880bca3a 635 }
afba937e
ON
636 }
637 mutex_unlock(&wdm_mutex);
638 return 0;
639}
640
641static const struct file_operations wdm_fops = {
642 .owner = THIS_MODULE,
643 .read = wdm_read,
644 .write = wdm_write,
645 .open = wdm_open,
646 .flush = wdm_flush,
647 .release = wdm_release,
6038f373
AB
648 .poll = wdm_poll,
649 .llseek = noop_llseek,
afba937e
ON
650};
651
652static struct usb_class_driver wdm_class = {
653 .name = "cdc-wdm%d",
654 .fops = &wdm_fops,
655 .minor_base = WDM_MINOR_BASE,
656};
657
658/* --- error handling --- */
659static void wdm_rxwork(struct work_struct *work)
660{
661 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
662 unsigned long flags;
663 int rv;
664
665 spin_lock_irqsave(&desc->iuspin, flags);
666 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
667 spin_unlock_irqrestore(&desc->iuspin, flags);
668 } else {
669 spin_unlock_irqrestore(&desc->iuspin, flags);
670 rv = usb_submit_urb(desc->response, GFP_KERNEL);
671 if (rv < 0 && rv != -EPERM) {
672 spin_lock_irqsave(&desc->iuspin, flags);
673 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
674 schedule_work(&desc->rxwork);
675 spin_unlock_irqrestore(&desc->iuspin, flags);
676 }
677 }
678}
679
680/* --- hotplug --- */
681
3cc36157
BM
682static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
683 u16 bufsize, int (*manage_power)(struct usb_interface *, int))
afba937e 684{
0dffb486 685 int rv = -ENOMEM;
afba937e 686 struct wdm_device *desc;
afba937e 687
afba937e
ON
688 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
689 if (!desc)
690 goto out;
b0c13860 691 INIT_LIST_HEAD(&desc->device_list);
e8537bd2
BM
692 mutex_init(&desc->rlock);
693 mutex_init(&desc->wlock);
afba937e
ON
694 spin_lock_init(&desc->iuspin);
695 init_waitqueue_head(&desc->wait);
0dffb486 696 desc->wMaxCommand = bufsize;
052fbc0d 697 /* this will be expanded and needed in hardware endianness */
afba937e
ON
698 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
699 desc->intf = intf;
700 INIT_WORK(&desc->rxwork, wdm_rxwork);
701
052fbc0d 702 rv = -EINVAL;
0dffb486 703 if (!usb_endpoint_is_int_in(ep))
afba937e 704 goto err;
afba937e 705
29cc8897 706 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
afba937e
ON
707
708 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
709 if (!desc->orq)
710 goto err;
711 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
712 if (!desc->irq)
713 goto err;
714
715 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
716 if (!desc->validity)
717 goto err;
718
719 desc->response = usb_alloc_urb(0, GFP_KERNEL);
720 if (!desc->response)
721 goto err;
722
723 desc->command = usb_alloc_urb(0, GFP_KERNEL);
724 if (!desc->command)
725 goto err;
726
727 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
728 if (!desc->ubuf)
729 goto err;
730
8457d99c 731 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
afba937e
ON
732 if (!desc->sbuf)
733 goto err;
734
8457d99c 735 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
afba937e 736 if (!desc->inbuf)
8457d99c 737 goto err;
afba937e
ON
738
739 usb_fill_int_urb(
740 desc->validity,
741 interface_to_usbdev(intf),
742 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
743 desc->sbuf,
744 desc->wMaxPacketSize,
745 wdm_int_callback,
746 desc,
747 ep->bInterval
748 );
afba937e 749
19b85b3b
BM
750 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
751 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
752 desc->irq->wValue = 0;
753 desc->irq->wIndex = desc->inum;
754 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
755
756 usb_fill_control_urb(
757 desc->response,
8143a896 758 interface_to_usbdev(intf),
19b85b3b
BM
759 /* using common endpoint 0 */
760 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
761 (unsigned char *)desc->irq,
762 desc->inbuf,
763 desc->wMaxCommand,
764 wdm_in_callback,
765 desc
766 );
19b85b3b 767
3cc36157
BM
768 desc->manage_power = manage_power;
769
b0c13860
BM
770 spin_lock(&wdm_device_list_lock);
771 list_add(&desc->device_list, &wdm_device_list);
772 spin_unlock(&wdm_device_list_lock);
773
afba937e 774 rv = usb_register_dev(intf, &wdm_class);
afba937e 775 if (rv < 0)
b0c13860 776 goto err;
052fbc0d 777 else
820c629a 778 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
afba937e
ON
779out:
780 return rv;
afba937e 781err:
0dffb486
BM
782 cleanup(desc);
783 return rv;
784}
785
3cc36157
BM
786static int wdm_manage_power(struct usb_interface *intf, int on)
787{
788 /* need autopm_get/put here to ensure the usbcore sees the new value */
789 int rv = usb_autopm_get_interface(intf);
790 if (rv < 0)
791 goto err;
792
793 intf->needs_remote_wakeup = on;
794 usb_autopm_put_interface(intf);
795err:
796 return rv;
797}
798
0dffb486
BM
799static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
800{
801 int rv = -EINVAL;
802 struct usb_host_interface *iface;
803 struct usb_endpoint_descriptor *ep;
804 struct usb_cdc_dmm_desc *dmhd;
805 u8 *buffer = intf->altsetting->extra;
806 int buflen = intf->altsetting->extralen;
807 u16 maxcom = WDM_DEFAULT_BUFSIZE;
808
809 if (!buffer)
810 goto err;
811 while (buflen > 2) {
812 if (buffer[1] != USB_DT_CS_INTERFACE) {
813 dev_err(&intf->dev, "skipping garbage\n");
814 goto next_desc;
815 }
816
817 switch (buffer[2]) {
818 case USB_CDC_HEADER_TYPE:
819 break;
820 case USB_CDC_DMM_TYPE:
821 dmhd = (struct usb_cdc_dmm_desc *)buffer;
822 maxcom = le16_to_cpu(dmhd->wMaxCommand);
823 dev_dbg(&intf->dev,
824 "Finding maximum buffer length: %d", maxcom);
825 break;
826 default:
827 dev_err(&intf->dev,
828 "Ignoring extra header, type %d, length %d\n",
829 buffer[2], buffer[0]);
830 break;
831 }
832next_desc:
833 buflen -= buffer[0];
834 buffer += buffer[0];
835 }
836
837 iface = intf->cur_altsetting;
838 if (iface->desc.bNumEndpoints != 1)
839 goto err;
840 ep = &iface->endpoint[0].desc;
841
3cc36157 842 rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
0dffb486
BM
843
844err:
afba937e
ON
845 return rv;
846}
847
3cc36157
BM
848/**
849 * usb_cdc_wdm_register - register a WDM subdriver
850 * @intf: usb interface the subdriver will associate with
851 * @ep: interrupt endpoint to monitor for notifications
852 * @bufsize: maximum message size to support for read/write
853 *
854 * Create WDM usb class character device and associate it with intf
855 * without binding, allowing another driver to manage the interface.
856 *
857 * The subdriver will manage the given interrupt endpoint exclusively
858 * and will issue control requests referring to the given intf. It
859 * will otherwise avoid interferring, and in particular not do
860 * usb_set_intfdata/usb_get_intfdata on intf.
861 *
862 * The return value is a pointer to the subdriver's struct usb_driver.
863 * The registering driver is responsible for calling this subdriver's
864 * disconnect, suspend, resume, pre_reset and post_reset methods from
865 * its own.
866 */
867struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
868 struct usb_endpoint_descriptor *ep,
869 int bufsize,
870 int (*manage_power)(struct usb_interface *, int))
871{
872 int rv = -EINVAL;
873
874 rv = wdm_create(intf, ep, bufsize, manage_power);
875 if (rv < 0)
876 goto err;
877
878 return &wdm_driver;
879err:
880 return ERR_PTR(rv);
881}
882EXPORT_SYMBOL(usb_cdc_wdm_register);
883
afba937e
ON
884static void wdm_disconnect(struct usb_interface *intf)
885{
886 struct wdm_device *desc;
887 unsigned long flags;
888
889 usb_deregister_dev(intf, &wdm_class);
b0c13860 890 desc = wdm_find_device(intf);
afba937e 891 mutex_lock(&wdm_mutex);
afba937e
ON
892
893 /* the spinlock makes sure no new urbs are generated in the callbacks */
894 spin_lock_irqsave(&desc->iuspin, flags);
895 set_bit(WDM_DISCONNECTING, &desc->flags);
896 set_bit(WDM_READ, &desc->flags);
17d80d56 897 /* to terminate pending flushes */
afba937e
ON
898 clear_bit(WDM_IN_USE, &desc->flags);
899 spin_unlock_irqrestore(&desc->iuspin, flags);
62aaf24d 900 wake_up_all(&desc->wait);
e8537bd2
BM
901 mutex_lock(&desc->rlock);
902 mutex_lock(&desc->wlock);
afba937e 903 kill_urbs(desc);
d93d16e9 904 cancel_work_sync(&desc->rxwork);
e8537bd2
BM
905 mutex_unlock(&desc->wlock);
906 mutex_unlock(&desc->rlock);
afba937e
ON
907 if (!desc->count)
908 cleanup(desc);
880bca3a
BM
909 else
910 dev_dbg(&intf->dev, "%s: %d open files - postponing cleanup\n", __func__, desc->count);
afba937e
ON
911 mutex_unlock(&wdm_mutex);
912}
913
d93d16e9 914#ifdef CONFIG_PM
17d80d56
ON
915static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
916{
b0c13860 917 struct wdm_device *desc = wdm_find_device(intf);
17d80d56
ON
918 int rv = 0;
919
920 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
921
d93d16e9 922 /* if this is an autosuspend the caller does the locking */
e8537bd2
BM
923 if (!PMSG_IS_AUTO(message)) {
924 mutex_lock(&desc->rlock);
925 mutex_lock(&desc->wlock);
926 }
62e66854 927 spin_lock_irq(&desc->iuspin);
d93d16e9 928
5b1b0b81 929 if (PMSG_IS_AUTO(message) &&
922a5ead
ON
930 (test_bit(WDM_IN_USE, &desc->flags)
931 || test_bit(WDM_RESPONDING, &desc->flags))) {
62e66854 932 spin_unlock_irq(&desc->iuspin);
17d80d56
ON
933 rv = -EBUSY;
934 } else {
d93d16e9 935
beb1d35f 936 set_bit(WDM_SUSPENDING, &desc->flags);
62e66854 937 spin_unlock_irq(&desc->iuspin);
d93d16e9 938 /* callback submits work - order is essential */
17d80d56 939 kill_urbs(desc);
d93d16e9 940 cancel_work_sync(&desc->rxwork);
17d80d56 941 }
e8537bd2
BM
942 if (!PMSG_IS_AUTO(message)) {
943 mutex_unlock(&desc->wlock);
944 mutex_unlock(&desc->rlock);
945 }
17d80d56
ON
946
947 return rv;
948}
d93d16e9 949#endif
17d80d56
ON
950
951static int recover_from_urb_loss(struct wdm_device *desc)
952{
953 int rv = 0;
954
955 if (desc->count) {
956 rv = usb_submit_urb(desc->validity, GFP_NOIO);
957 if (rv < 0)
9908a32e
GKH
958 dev_err(&desc->intf->dev,
959 "Error resume submitting int urb - %d\n", rv);
17d80d56
ON
960 }
961 return rv;
962}
d93d16e9
ON
963
964#ifdef CONFIG_PM
17d80d56
ON
965static int wdm_resume(struct usb_interface *intf)
966{
b0c13860 967 struct wdm_device *desc = wdm_find_device(intf);
17d80d56
ON
968 int rv;
969
970 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
338124c1 971
beb1d35f 972 clear_bit(WDM_SUSPENDING, &desc->flags);
62e66854 973 rv = recover_from_urb_loss(desc);
338124c1 974
17d80d56
ON
975 return rv;
976}
d93d16e9 977#endif
17d80d56
ON
978
979static int wdm_pre_reset(struct usb_interface *intf)
980{
b0c13860 981 struct wdm_device *desc = wdm_find_device(intf);
17d80d56 982
d771d8aa
ON
983 /*
984 * we notify everybody using poll of
985 * an exceptional situation
986 * must be done before recovery lest a spontaneous
987 * message from the device is lost
988 */
989 spin_lock_irq(&desc->iuspin);
88044202
BM
990 set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
991 set_bit(WDM_READ, &desc->flags); /* unblock read */
992 clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
d771d8aa
ON
993 desc->rerr = -EINTR;
994 spin_unlock_irq(&desc->iuspin);
995 wake_up_all(&desc->wait);
88044202
BM
996 mutex_lock(&desc->rlock);
997 mutex_lock(&desc->wlock);
998 kill_urbs(desc);
999 cancel_work_sync(&desc->rxwork);
17d80d56
ON
1000 return 0;
1001}
1002
1003static int wdm_post_reset(struct usb_interface *intf)
1004{
b0c13860 1005 struct wdm_device *desc = wdm_find_device(intf);
17d80d56
ON
1006 int rv;
1007
88044202 1008 clear_bit(WDM_RESETTING, &desc->flags);
17d80d56 1009 rv = recover_from_urb_loss(desc);
e8537bd2
BM
1010 mutex_unlock(&desc->wlock);
1011 mutex_unlock(&desc->rlock);
17d80d56
ON
1012 return 0;
1013}
1014
afba937e
ON
1015static struct usb_driver wdm_driver = {
1016 .name = "cdc_wdm",
1017 .probe = wdm_probe,
1018 .disconnect = wdm_disconnect,
d93d16e9 1019#ifdef CONFIG_PM
17d80d56
ON
1020 .suspend = wdm_suspend,
1021 .resume = wdm_resume,
1022 .reset_resume = wdm_resume,
d93d16e9 1023#endif
17d80d56
ON
1024 .pre_reset = wdm_pre_reset,
1025 .post_reset = wdm_post_reset,
afba937e 1026 .id_table = wdm_ids,
17d80d56 1027 .supports_autosuspend = 1,
afba937e
ON
1028};
1029
65db4305 1030module_usb_driver(wdm_driver);
afba937e
ON
1031
1032MODULE_AUTHOR(DRIVER_AUTHOR);
87d65e54 1033MODULE_DESCRIPTION(DRIVER_DESC);
afba937e 1034MODULE_LICENSE("GPL");