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