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