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