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