update/add new 3G modem modules
[tomato.git] / release / src-rt / linux / linux-2.6 / drivers / usb / class / cdc-wdm.c
blob9f93d26aca3480e30fc2dde14619ca7f795d89fb
1 /*
2 * cdc-wdm.c
4 * This driver supports USB CDC WCM Device Management.
6 * Copyright (c) 2007-2009 Oliver Neukum
8 * Some code taken from cdc-acm.c
10 * Released under the GPLv2.
12 * Many thanks to Carl Nordbeck
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>
29 * Version Information
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"
35 static const struct usb_device_id wdm_ids[] = {
37 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
38 USB_DEVICE_ID_MATCH_INT_SUBCLASS,
39 .bInterfaceClass = USB_CLASS_COMM,
40 .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM
42 { }
45 MODULE_DEVICE_TABLE (usb, wdm_ids);
47 #define WDM_MINOR_BASE 176
50 #define WDM_IN_USE 1
51 #define WDM_DISCONNECTING 2
52 #define WDM_RESULT 3
53 #define WDM_READ 4
54 #define WDM_INT_STALL 5
55 #define WDM_POLL_RUNNING 6
56 #define WDM_RESPONDING 7
57 #define WDM_SUSPENDING 8
58 #define WDM_RESETTING 9
59 #define WDM_OVERFLOW 10
61 #define WDM_MAX 16
63 /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
64 #define WDM_DEFAULT_BUFSIZE 256
66 static DEFINE_MUTEX(wdm_mutex);
67 static DEFINE_SPINLOCK(wdm_device_list_lock);
68 static LIST_HEAD(wdm_device_list);
70 /* --- method tables --- */
72 struct wdm_device {
73 u8 *inbuf; /* buffer for response */
74 u8 *outbuf; /* buffer for command */
75 u8 *sbuf; /* buffer for status */
76 u8 *ubuf; /* buffer for copy to user space */
78 struct urb *command;
79 struct urb *response;
80 struct urb *validity;
81 struct usb_interface *intf;
82 struct usb_ctrlrequest *orq;
83 struct usb_ctrlrequest *irq;
84 spinlock_t iuspin;
86 unsigned long flags;
87 u16 bufsize;
88 u16 wMaxCommand;
89 u16 wMaxPacketSize;
90 __le16 inum;
91 int reslength;
92 int length;
93 int read;
94 int count;
95 dma_addr_t shandle;
96 dma_addr_t ihandle;
97 struct mutex wlock;
98 struct mutex rlock;
99 wait_queue_head_t wait;
100 struct work_struct rxwork;
101 int werr;
102 int rerr;
104 struct list_head device_list;
105 int (*manage_power)(struct usb_interface *, int);
108 static struct usb_driver wdm_driver;
110 /* return intfdata if we own the interface, else look up intf in the list */
111 static struct wdm_device *wdm_find_device(struct usb_interface *intf)
113 struct wdm_device *desc;
115 spin_lock(&wdm_device_list_lock);
116 list_for_each_entry(desc, &wdm_device_list, device_list)
117 if (desc->intf == intf)
118 goto found;
119 desc = NULL;
120 found:
121 spin_unlock(&wdm_device_list_lock);
123 return desc;
126 static struct wdm_device *wdm_find_device_by_minor(int minor)
128 struct wdm_device *desc;
130 spin_lock(&wdm_device_list_lock);
131 list_for_each_entry(desc, &wdm_device_list, device_list)
132 if (desc->intf->minor == minor)
133 goto found;
134 desc = NULL;
135 found:
136 spin_unlock(&wdm_device_list_lock);
138 return desc;
141 /* --- callbacks --- */
142 static void wdm_out_callback(struct urb *urb)
144 struct wdm_device *desc;
145 desc = urb->context;
146 spin_lock(&desc->iuspin);
147 desc->werr = urb->status;
148 spin_unlock(&desc->iuspin);
149 kfree(desc->outbuf);
150 desc->outbuf = NULL;
151 clear_bit(WDM_IN_USE, &desc->flags);
152 wake_up(&desc->wait);
155 static void wdm_in_callback(struct urb *urb)
157 struct wdm_device *desc = urb->context;
158 int status = urb->status;
159 int length = urb->actual_length;
161 spin_lock(&desc->iuspin);
162 clear_bit(WDM_RESPONDING, &desc->flags);
164 if (status) {
165 switch (status) {
166 case -ENOENT:
167 dev_dbg(&desc->intf->dev,
168 "nonzero urb status received: -ENOENT");
169 goto skip_error;
170 case -ECONNRESET:
171 dev_dbg(&desc->intf->dev,
172 "nonzero urb status received: -ECONNRESET");
173 goto skip_error;
174 case -ESHUTDOWN:
175 dev_dbg(&desc->intf->dev,
176 "nonzero urb status received: -ESHUTDOWN");
177 goto skip_error;
178 case -EPIPE:
179 dev_err(&desc->intf->dev,
180 "nonzero urb status received: -EPIPE\n");
181 break;
182 default:
183 dev_err(&desc->intf->dev,
184 "Unexpected error %d\n", status);
185 break;
189 desc->rerr = status;
190 if (length + desc->length > desc->wMaxCommand) {
191 /* The buffer would overflow */
192 set_bit(WDM_OVERFLOW, &desc->flags);
193 } else {
194 /* we may already be in overflow */
195 if (!test_bit(WDM_OVERFLOW, &desc->flags)) {
196 memmove(desc->ubuf + desc->length, desc->inbuf, length);
197 desc->length += length;
198 desc->reslength = length;
201 skip_error:
202 wake_up(&desc->wait);
204 set_bit(WDM_READ, &desc->flags);
205 spin_unlock(&desc->iuspin);
208 static void wdm_int_callback(struct urb *urb)
210 int rv = 0;
211 int status = urb->status;
212 struct wdm_device *desc;
213 struct usb_cdc_notification *dr;
215 desc = urb->context;
216 dr = (struct usb_cdc_notification *)desc->sbuf;
218 if (status) {
219 switch (status) {
220 case -ESHUTDOWN:
221 case -ENOENT:
222 case -ECONNRESET:
223 return; /* unplug */
224 case -EPIPE:
225 set_bit(WDM_INT_STALL, &desc->flags);
226 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
227 goto sw; /* halt is cleared in work */
228 default:
229 dev_err(&desc->intf->dev,
230 "nonzero urb status received: %d\n", status);
231 break;
235 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
236 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
237 urb->actual_length);
238 goto exit;
241 switch (dr->bNotificationType) {
242 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
243 dev_dbg(&desc->intf->dev,
244 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
245 dr->wIndex, dr->wLength);
246 break;
248 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
250 dev_dbg(&desc->intf->dev,
251 "NOTIFY_NETWORK_CONNECTION %s network",
252 dr->wValue ? "connected to" : "disconnected from");
253 goto exit;
254 default:
255 clear_bit(WDM_POLL_RUNNING, &desc->flags);
256 dev_err(&desc->intf->dev,
257 "unknown notification %d received: index %d len %d\n",
258 dr->bNotificationType, dr->wIndex, dr->wLength);
259 goto exit;
262 spin_lock(&desc->iuspin);
263 clear_bit(WDM_READ, &desc->flags);
264 set_bit(WDM_RESPONDING, &desc->flags);
265 if (!test_bit(WDM_DISCONNECTING, &desc->flags)
266 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
267 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
268 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
269 __func__, rv);
271 spin_unlock(&desc->iuspin);
272 if (rv < 0) {
273 clear_bit(WDM_RESPONDING, &desc->flags);
274 if (rv == -EPERM)
275 return;
276 if (rv == -ENOMEM) {
278 rv = schedule_work(&desc->rxwork);
279 if (rv)
280 dev_err(&desc->intf->dev,
281 "Cannot schedule work\n");
284 exit:
285 rv = usb_submit_urb(urb, GFP_ATOMIC);
286 if (rv)
287 dev_err(&desc->intf->dev,
288 "%s - usb_submit_urb failed with result %d\n",
289 __func__, rv);
293 static void kill_urbs(struct wdm_device *desc)
295 /* the order here is essential */
296 usb_kill_urb(desc->command);
297 usb_kill_urb(desc->validity);
298 usb_kill_urb(desc->response);
301 static void free_urbs(struct wdm_device *desc)
303 usb_free_urb(desc->validity);
304 usb_free_urb(desc->response);
305 usb_free_urb(desc->command);
308 static void cleanup(struct wdm_device *desc)
310 kfree(desc->sbuf);
311 kfree(desc->inbuf);
312 kfree(desc->orq);
313 kfree(desc->irq);
314 kfree(desc->ubuf);
315 free_urbs(desc);
316 kfree(desc);
319 static ssize_t wdm_write
320 (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
322 u8 *buf;
323 int rv = -EMSGSIZE, r, we;
324 struct wdm_device *desc = file->private_data;
325 struct usb_ctrlrequest *req;
327 if (count > desc->wMaxCommand)
328 count = desc->wMaxCommand;
330 spin_lock_irq(&desc->iuspin);
331 we = desc->werr;
332 desc->werr = 0;
333 spin_unlock_irq(&desc->iuspin);
334 if (we < 0)
335 return -EIO;
337 buf = kmalloc(count, GFP_KERNEL);
338 if (!buf) {
339 rv = -ENOMEM;
340 goto outnl;
343 r = copy_from_user(buf, buffer, count);
344 if (r > 0) {
345 kfree(buf);
346 rv = -EFAULT;
347 goto outnl;
350 /* concurrent writes and disconnect */
351 r = mutex_lock_interruptible(&desc->wlock);
352 rv = -ERESTARTSYS;
353 if (r) {
354 kfree(buf);
355 goto outnl;
358 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
359 kfree(buf);
360 rv = -ENODEV;
361 goto outnp;
364 r = usb_autopm_get_interface(desc->intf);
365 if (r < 0) {
366 kfree(buf);
367 goto outnp;
370 if (!(file->f_flags & O_NONBLOCK))
371 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
372 &desc->flags));
373 else
374 if (test_bit(WDM_IN_USE, &desc->flags))
375 r = -EAGAIN;
377 if (test_bit(WDM_RESETTING, &desc->flags))
378 r = -EIO;
380 if (r < 0) {
381 kfree(buf);
382 goto out;
385 req = desc->orq;
386 usb_fill_control_urb(
387 desc->command,
388 interface_to_usbdev(desc->intf),
389 /* using common endpoint 0 */
390 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
391 (unsigned char *)req,
392 buf,
393 count,
394 wdm_out_callback,
395 desc
398 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
399 USB_RECIP_INTERFACE);
400 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
401 req->wValue = 0;
402 req->wIndex = desc->inum;
403 req->wLength = cpu_to_le16(count);
404 set_bit(WDM_IN_USE, &desc->flags);
405 desc->outbuf = buf;
407 rv = usb_submit_urb(desc->command, GFP_KERNEL);
408 if (rv < 0) {
409 kfree(buf);
410 desc->outbuf = NULL;
411 clear_bit(WDM_IN_USE, &desc->flags);
412 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
413 } else {
414 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
415 req->wIndex);
417 out:
418 usb_autopm_put_interface(desc->intf);
419 outnp:
420 mutex_unlock(&desc->wlock);
421 outnl:
422 return rv < 0 ? rv : count;
425 static ssize_t wdm_read
426 (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
428 int rv, cntr;
429 int i = 0;
430 struct wdm_device *desc = file->private_data;
433 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
434 if (rv < 0)
435 return -ERESTARTSYS;
437 cntr = ACCESS_ONCE(desc->length);
438 if (cntr == 0) {
439 desc->read = 0;
440 retry:
441 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
442 rv = -ENODEV;
443 goto err;
445 if (test_bit(WDM_OVERFLOW, &desc->flags)) {
446 clear_bit(WDM_OVERFLOW, &desc->flags);
447 rv = -ENOBUFS;
448 goto err;
450 i++;
451 if (file->f_flags & O_NONBLOCK) {
452 if (!test_bit(WDM_READ, &desc->flags)) {
453 rv = cntr ? cntr : -EAGAIN;
454 goto err;
456 rv = 0;
457 } else {
458 rv = wait_event_interruptible(desc->wait,
459 test_bit(WDM_READ, &desc->flags));
462 /* may have happened while we slept */
463 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
464 rv = -ENODEV;
465 goto err;
467 if (test_bit(WDM_RESETTING, &desc->flags)) {
468 rv = -EIO;
469 goto err;
471 usb_mark_last_busy(interface_to_usbdev(desc->intf));
472 if (rv < 0) {
473 rv = -ERESTARTSYS;
474 goto err;
477 spin_lock_irq(&desc->iuspin);
479 if (desc->rerr) { /* read completed, error happened */
480 desc->rerr = 0;
481 spin_unlock_irq(&desc->iuspin);
482 rv = -EIO;
483 goto err;
486 * recheck whether we've lost the race
487 * against the completion handler
489 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
490 spin_unlock_irq(&desc->iuspin);
491 goto retry;
494 if (!desc->reslength) { /* zero length read */
495 dev_dbg(&desc->intf->dev, "%s: zero length - clearing WDM_READ\n", __func__);
496 clear_bit(WDM_READ, &desc->flags);
497 spin_unlock_irq(&desc->iuspin);
498 goto retry;
500 cntr = desc->length;
501 spin_unlock_irq(&desc->iuspin);
504 if (cntr > count)
505 cntr = count;
506 rv = copy_to_user(buffer, desc->ubuf, cntr);
507 if (rv > 0) {
508 rv = -EFAULT;
509 goto err;
512 spin_lock_irq(&desc->iuspin);
514 for (i = 0; i < desc->length - cntr; i++)
515 desc->ubuf[i] = desc->ubuf[i + cntr];
517 desc->length -= cntr;
518 /* in case we had outstanding data */
519 if (!desc->length)
520 clear_bit(WDM_READ, &desc->flags);
522 spin_unlock_irq(&desc->iuspin);
524 rv = cntr;
526 err:
527 mutex_unlock(&desc->rlock);
528 return rv;
531 static int wdm_flush(struct file *file, fl_owner_t id)
533 struct wdm_device *desc = file->private_data;
535 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
537 /* cannot dereference desc->intf if WDM_DISCONNECTING */
538 if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags)) {
539 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
540 desc->werr);
543 return usb_translate_errors(desc->werr);
546 static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
548 struct wdm_device *desc = file->private_data;
549 unsigned long flags;
550 unsigned int mask = 0;
552 spin_lock_irqsave(&desc->iuspin, flags);
553 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
554 mask = POLLHUP | POLLERR;
555 spin_unlock_irqrestore(&desc->iuspin, flags);
556 goto desc_out;
558 if (test_bit(WDM_READ, &desc->flags))
559 mask = POLLIN | POLLRDNORM;
560 if (desc->rerr || desc->werr)
561 mask |= POLLERR;
562 if (!test_bit(WDM_IN_USE, &desc->flags))
563 mask |= POLLOUT | POLLWRNORM;
564 spin_unlock_irqrestore(&desc->iuspin, flags);
566 poll_wait(file, &desc->wait, wait);
568 desc_out:
569 return mask;
572 static int wdm_open(struct inode *inode, struct file *file)
574 int minor = iminor(inode);
575 int rv = -ENODEV;
576 struct usb_interface *intf;
577 struct wdm_device *desc;
579 mutex_lock(&wdm_mutex);
580 desc = wdm_find_device_by_minor(minor);
581 if (!desc)
582 goto out;
584 intf = desc->intf;
585 if (test_bit(WDM_DISCONNECTING, &desc->flags))
586 goto out;
587 file->private_data = desc;
589 rv = usb_autopm_get_interface(desc->intf);
590 if (rv < 0) {
591 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
592 goto out;
595 /* using write lock to protect desc->count */
596 mutex_lock(&desc->wlock);
597 if (!desc->count++) {
598 desc->werr = 0;
599 desc->rerr = 0;
600 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
601 if (rv < 0) {
602 desc->count--;
603 dev_err(&desc->intf->dev,
604 "Error submitting int urb - %d\n", rv);
606 } else {
607 rv = 0;
609 mutex_unlock(&desc->wlock);
610 if (desc->count == 1)
611 desc->manage_power(intf, 1);
612 usb_autopm_put_interface(desc->intf);
613 out:
614 mutex_unlock(&wdm_mutex);
615 return rv;
618 static int wdm_release(struct inode *inode, struct file *file)
620 struct wdm_device *desc = file->private_data;
622 mutex_lock(&wdm_mutex);
624 /* using write lock to protect desc->count */
625 mutex_lock(&desc->wlock);
626 desc->count--;
627 mutex_unlock(&desc->wlock);
629 if (!desc->count) {
630 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
631 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
632 kill_urbs(desc);
633 desc->manage_power(desc->intf, 0);
634 } else {
635 /* must avoid dev_printk here as desc->intf is invalid */
636 pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
637 cleanup(desc);
640 mutex_unlock(&wdm_mutex);
641 return 0;
644 static const struct file_operations wdm_fops = {
645 .owner = THIS_MODULE,
646 .read = wdm_read,
647 .write = wdm_write,
648 .open = wdm_open,
649 .flush = wdm_flush,
650 .release = wdm_release,
651 .poll = wdm_poll,
654 static struct usb_class_driver wdm_class = {
655 .name = "cdc-wdm%d",
656 .fops = &wdm_fops,
657 .minor_base = WDM_MINOR_BASE,
660 /* --- error handling --- */
661 static void wdm_rxwork(struct work_struct *work)
663 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
664 unsigned long flags;
665 int rv;
667 spin_lock_irqsave(&desc->iuspin, flags);
668 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
669 spin_unlock_irqrestore(&desc->iuspin, flags);
670 } else {
671 spin_unlock_irqrestore(&desc->iuspin, flags);
672 rv = usb_submit_urb(desc->response, GFP_KERNEL);
673 if (rv < 0 && rv != -EPERM) {
674 spin_lock_irqsave(&desc->iuspin, flags);
675 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
676 schedule_work(&desc->rxwork);
677 spin_unlock_irqrestore(&desc->iuspin, flags);
682 /* --- hotplug --- */
684 static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
685 u16 bufsize, int (*manage_power)(struct usb_interface *, int))
687 int rv = -ENOMEM;
688 struct wdm_device *desc;
690 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
691 if (!desc)
692 goto out;
693 INIT_LIST_HEAD(&desc->device_list);
694 mutex_init(&desc->rlock);
695 mutex_init(&desc->wlock);
696 spin_lock_init(&desc->iuspin);
697 init_waitqueue_head(&desc->wait);
698 desc->wMaxCommand = bufsize;
699 /* this will be expanded and needed in hardware endianness */
700 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
701 desc->intf = intf;
702 INIT_WORK(&desc->rxwork, wdm_rxwork);
704 rv = -EINVAL;
705 if (!usb_endpoint_is_int_in(ep))
706 goto err;
708 desc->wMaxPacketSize = le16_to_cpu(ep->wMaxPacketSize);
710 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
711 if (!desc->orq)
712 goto err;
713 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
714 if (!desc->irq)
715 goto err;
717 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
718 if (!desc->validity)
719 goto err;
721 desc->response = usb_alloc_urb(0, GFP_KERNEL);
722 if (!desc->response)
723 goto err;
725 desc->command = usb_alloc_urb(0, GFP_KERNEL);
726 if (!desc->command)
727 goto err;
729 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
730 if (!desc->ubuf)
731 goto err;
733 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
734 if (!desc->sbuf)
735 goto err;
737 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
738 if (!desc->inbuf)
739 goto err;
741 usb_fill_int_urb(
742 desc->validity,
743 interface_to_usbdev(intf),
744 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
745 desc->sbuf,
746 desc->wMaxPacketSize,
747 wdm_int_callback,
748 desc,
749 ep->bInterval
752 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
753 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
754 desc->irq->wValue = 0;
755 desc->irq->wIndex = desc->inum;
756 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
758 usb_fill_control_urb(
759 desc->response,
760 interface_to_usbdev(intf),
761 /* using common endpoint 0 */
762 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
763 (unsigned char *)desc->irq,
764 desc->inbuf,
765 desc->wMaxCommand,
766 wdm_in_callback,
767 desc
770 desc->manage_power = manage_power;
772 spin_lock(&wdm_device_list_lock);
773 list_add(&desc->device_list, &wdm_device_list);
774 spin_unlock(&wdm_device_list_lock);
776 rv = usb_register_dev(intf, &wdm_class);
777 if (rv < 0)
778 goto err;
779 else
780 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
781 out:
782 return rv;
783 err:
784 spin_lock(&wdm_device_list_lock);
785 list_del(&desc->device_list);
786 spin_unlock(&wdm_device_list_lock);
787 cleanup(desc);
788 return rv;
791 static int wdm_manage_power(struct usb_interface *intf, int on)
793 /* need autopm_get/put here to ensure the usbcore sees the new value */
794 int rv = usb_autopm_get_interface(intf);
795 if (rv < 0)
796 goto err;
798 intf->needs_remote_wakeup = on;
799 usb_autopm_put_interface(intf);
800 err:
801 return rv;
804 static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
806 int rv = -EINVAL;
807 struct usb_host_interface *iface;
808 struct usb_endpoint_descriptor *ep;
809 struct usb_cdc_dmm_desc *dmhd;
810 u8 *buffer = intf->altsetting->extra;
811 int buflen = intf->altsetting->extralen;
812 u16 maxcom = WDM_DEFAULT_BUFSIZE;
814 if (!buffer)
815 goto err;
816 while (buflen > 2) {
817 if (buffer[1] != USB_DT_CS_INTERFACE) {
818 dev_err(&intf->dev, "skipping garbage\n");
819 goto next_desc;
822 switch (buffer[2]) {
823 case USB_CDC_HEADER_TYPE:
824 break;
825 case USB_CDC_DMM_TYPE:
826 dmhd = (struct usb_cdc_dmm_desc *)buffer;
827 maxcom = le16_to_cpu(dmhd->wMaxCommand);
828 dev_dbg(&intf->dev,
829 "Finding maximum buffer length: %d", maxcom);
830 break;
831 default:
832 dev_err(&intf->dev,
833 "Ignoring extra header, type %d, length %d\n",
834 buffer[2], buffer[0]);
835 break;
837 next_desc:
838 buflen -= buffer[0];
839 buffer += buffer[0];
842 iface = intf->cur_altsetting;
843 if (iface->desc.bNumEndpoints != 1)
844 goto err;
845 ep = &iface->endpoint[0].desc;
847 rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
849 err:
850 return rv;
854 * usb_cdc_wdm_register - register a WDM subdriver
855 * @intf: usb interface the subdriver will associate with
856 * @ep: interrupt endpoint to monitor for notifications
857 * @bufsize: maximum message size to support for read/write
859 * Create WDM usb class character device and associate it with intf
860 * without binding, allowing another driver to manage the interface.
862 * The subdriver will manage the given interrupt endpoint exclusively
863 * and will issue control requests referring to the given intf. It
864 * will otherwise avoid interferring, and in particular not do
865 * usb_set_intfdata/usb_get_intfdata on intf.
867 * The return value is a pointer to the subdriver's struct usb_driver.
868 * The registering driver is responsible for calling this subdriver's
869 * disconnect, suspend, resume, pre_reset and post_reset methods from
870 * its own.
872 struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
873 struct usb_endpoint_descriptor *ep,
874 int bufsize,
875 int (*manage_power)(struct usb_interface *, int))
877 int rv = -EINVAL;
879 rv = wdm_create(intf, ep, bufsize, manage_power);
880 if (rv < 0)
881 goto err;
883 return &wdm_driver;
884 err:
885 return ERR_PTR(rv);
887 EXPORT_SYMBOL(usb_cdc_wdm_register);
889 static void wdm_disconnect(struct usb_interface *intf)
891 struct wdm_device *desc;
892 unsigned long flags;
894 usb_deregister_dev(intf, &wdm_class);
895 desc = wdm_find_device(intf);
896 mutex_lock(&wdm_mutex);
898 /* the spinlock makes sure no new urbs are generated in the callbacks */
899 spin_lock_irqsave(&desc->iuspin, flags);
900 set_bit(WDM_DISCONNECTING, &desc->flags);
901 set_bit(WDM_READ, &desc->flags);
902 /* to terminate pending flushes */
903 clear_bit(WDM_IN_USE, &desc->flags);
904 spin_unlock_irqrestore(&desc->iuspin, flags);
905 wake_up_all(&desc->wait);
906 mutex_lock(&desc->rlock);
907 mutex_lock(&desc->wlock);
908 kill_urbs(desc);
909 cancel_work_sync(&desc->rxwork);
910 mutex_unlock(&desc->wlock);
911 mutex_unlock(&desc->rlock);
913 /* the desc->intf pointer used as list key is now invalid */
914 spin_lock(&wdm_device_list_lock);
915 list_del(&desc->device_list);
916 spin_unlock(&wdm_device_list_lock);
918 if (!desc->count)
919 cleanup(desc);
920 mutex_unlock(&wdm_mutex);
923 #ifdef CONFIG_PM
924 static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
926 struct wdm_device *desc = wdm_find_device(intf);
927 int rv = 0;
929 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
931 /* if this is an autosuspend the caller does the locking */
932 if (!(message.event & PM_EVENT_AUTO))
933 mutex_lock(&desc->rlock);
934 mutex_lock(&desc->wlock);
936 spin_lock_irq(&desc->iuspin);
938 if ((message.event & PM_EVENT_AUTO) &&
939 (test_bit(WDM_IN_USE, &desc->flags)
940 || test_bit(WDM_RESPONDING, &desc->flags))) {
941 spin_unlock_irq(&desc->iuspin);
942 rv = -EBUSY;
943 } else {
945 set_bit(WDM_SUSPENDING, &desc->flags);
946 spin_unlock_irq(&desc->iuspin);
947 /* callback submits work - order is essential */
948 kill_urbs(desc);
949 cancel_work_sync(&desc->rxwork);
951 if (!(message.event & PM_EVENT_AUTO))
952 mutex_unlock(&desc->wlock);
953 mutex_unlock(&desc->rlock);
956 return rv;
958 #endif
960 static int recover_from_urb_loss(struct wdm_device *desc)
962 int rv = 0;
964 if (desc->count) {
965 rv = usb_submit_urb(desc->validity, GFP_NOIO);
966 if (rv < 0)
967 dev_err(&desc->intf->dev,
968 "Error resume submitting int urb - %d\n", rv);
970 return rv;
973 #ifdef CONFIG_PM
974 static int wdm_resume(struct usb_interface *intf)
976 struct wdm_device *desc = wdm_find_device(intf);
977 int rv;
979 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
981 clear_bit(WDM_SUSPENDING, &desc->flags);
982 rv = recover_from_urb_loss(desc);
984 return rv;
986 #endif
988 static int wdm_pre_reset(struct usb_interface *intf)
990 struct wdm_device *desc = wdm_find_device(intf);
993 * we notify everybody using poll of
994 * an exceptional situation
995 * must be done before recovery lest a spontaneous
996 * message from the device is lost
998 spin_lock_irq(&desc->iuspin);
999 set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
1000 set_bit(WDM_READ, &desc->flags); /* unblock read */
1001 clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
1002 desc->rerr = -EINTR;
1003 spin_unlock_irq(&desc->iuspin);
1004 wake_up_all(&desc->wait);
1005 mutex_lock(&desc->rlock);
1006 mutex_lock(&desc->wlock);
1007 kill_urbs(desc);
1008 cancel_work_sync(&desc->rxwork);
1009 return 0;
1012 static int wdm_post_reset(struct usb_interface *intf)
1014 struct wdm_device *desc = wdm_find_device(intf);
1015 int rv;
1017 clear_bit(WDM_OVERFLOW, &desc->flags);
1018 clear_bit(WDM_RESETTING, &desc->flags);
1019 rv = recover_from_urb_loss(desc);
1020 mutex_unlock(&desc->wlock);
1021 mutex_unlock(&desc->rlock);
1022 return 0;
1025 static struct usb_driver wdm_driver = {
1026 .name = "cdc_wdm",
1027 .probe = wdm_probe,
1028 .disconnect = wdm_disconnect,
1029 #ifdef CONFIG_PM
1030 .suspend = wdm_suspend,
1031 .resume = wdm_resume,
1032 .reset_resume = wdm_resume,
1033 #endif
1034 .pre_reset = wdm_pre_reset,
1035 .post_reset = wdm_post_reset,
1036 .id_table = wdm_ids,
1037 .supports_autosuspend = 1,
1040 /* --- low level module stuff --- */
1042 static int __init wdm_init(void)
1044 int rv;
1046 rv = usb_register(&wdm_driver);
1048 return rv;
1051 static void __exit wdm_exit(void)
1053 usb_deregister(&wdm_driver);
1056 module_init(wdm_init);
1057 module_exit(wdm_exit);
1059 MODULE_AUTHOR(DRIVER_AUTHOR);
1060 MODULE_DESCRIPTION(DRIVER_DESC);
1061 MODULE_LICENSE("GPL");