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