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