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