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