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