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