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