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