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