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