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