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