blob: cf1c5fb918dc78d37b3fd5eaa748df03ed93c4b1 [file] [log] [blame]
Oliver Neukumafba9372008-05-13 17:01:25 +02001/*
2 * cdc-wdm.c
3 *
4 * This driver supports USB CDC WCM Device Management.
5 *
Oliver Neukum052fbc02009-04-20 17:24:49 +02006 * Copyright (c) 2007-2009 Oliver Neukum
Oliver Neukumafba9372008-05-13 17:01:25 +02007 *
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>
Oliver Neukumafba9372008-05-13 17:01:25 +020018#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 */
Oliver Neukum87d65e52008-06-19 14:20:18 +020030#define DRIVER_VERSION "v0.03"
Oliver Neukumafba9372008-05-13 17:01:25 +020031#define DRIVER_AUTHOR "Oliver Neukum"
Oliver Neukum87d65e52008-06-19 14:20:18 +020032#define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management"
Oliver Neukumafba9372008-05-13 17:01:25 +020033
Németh Márton6ef48522010-01-10 15:33:45 +010034static const struct usb_device_id wdm_ids[] = {
Oliver Neukumafba9372008-05-13 17:01:25 +020035 {
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
Oliver Neukumaa5380b2008-10-13 14:05:20 +020044MODULE_DEVICE_TABLE (usb, wdm_ids);
45
Oliver Neukumafba9372008-05-13 17:01:25 +020046#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
55
56
57#define WDM_MAX 16
58
59
60static DEFINE_MUTEX(wdm_mutex);
61
62/* --- method tables --- */
63
64struct wdm_device {
65 u8 *inbuf; /* buffer for response */
66 u8 *outbuf; /* buffer for command */
67 u8 *sbuf; /* buffer for status */
68 u8 *ubuf; /* buffer for copy to user space */
69
70 struct urb *command;
71 struct urb *response;
72 struct urb *validity;
73 struct usb_interface *intf;
74 struct usb_ctrlrequest *orq;
75 struct usb_ctrlrequest *irq;
76 spinlock_t iuspin;
77
78 unsigned long flags;
79 u16 bufsize;
80 u16 wMaxCommand;
81 u16 wMaxPacketSize;
82 u16 bMaxPacketSize0;
83 __le16 inum;
84 int reslength;
85 int length;
86 int read;
87 int count;
88 dma_addr_t shandle;
89 dma_addr_t ihandle;
Oliver Neukum860e41a2010-02-27 20:54:24 +010090 struct mutex lock;
Oliver Neukumafba9372008-05-13 17:01:25 +020091 wait_queue_head_t wait;
92 struct work_struct rxwork;
93 int werr;
94 int rerr;
95};
96
97static struct usb_driver wdm_driver;
98
99/* --- callbacks --- */
100static void wdm_out_callback(struct urb *urb)
101{
102 struct wdm_device *desc;
103 desc = urb->context;
104 spin_lock(&desc->iuspin);
105 desc->werr = urb->status;
106 spin_unlock(&desc->iuspin);
107 clear_bit(WDM_IN_USE, &desc->flags);
108 kfree(desc->outbuf);
109 wake_up(&desc->wait);
110}
111
112static void wdm_in_callback(struct urb *urb)
113{
114 struct wdm_device *desc = urb->context;
115 int status = urb->status;
116
117 spin_lock(&desc->iuspin);
118
119 if (status) {
120 switch (status) {
121 case -ENOENT:
122 dev_dbg(&desc->intf->dev,
123 "nonzero urb status received: -ENOENT");
124 break;
125 case -ECONNRESET:
126 dev_dbg(&desc->intf->dev,
127 "nonzero urb status received: -ECONNRESET");
128 break;
129 case -ESHUTDOWN:
130 dev_dbg(&desc->intf->dev,
131 "nonzero urb status received: -ESHUTDOWN");
132 break;
133 case -EPIPE:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700134 dev_err(&desc->intf->dev,
135 "nonzero urb status received: -EPIPE\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200136 break;
137 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700138 dev_err(&desc->intf->dev,
139 "Unexpected error %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200140 break;
141 }
142 }
143
144 desc->rerr = status;
145 desc->reslength = urb->actual_length;
146 memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength);
147 desc->length += desc->reslength;
148 wake_up(&desc->wait);
149
150 set_bit(WDM_READ, &desc->flags);
151 spin_unlock(&desc->iuspin);
152}
153
154static void wdm_int_callback(struct urb *urb)
155{
156 int rv = 0;
157 int status = urb->status;
158 struct wdm_device *desc;
159 struct usb_ctrlrequest *req;
160 struct usb_cdc_notification *dr;
161
162 desc = urb->context;
163 req = desc->irq;
164 dr = (struct usb_cdc_notification *)desc->sbuf;
165
166 if (status) {
167 switch (status) {
168 case -ESHUTDOWN:
169 case -ENOENT:
170 case -ECONNRESET:
171 return; /* unplug */
172 case -EPIPE:
173 set_bit(WDM_INT_STALL, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700174 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200175 goto sw; /* halt is cleared in work */
176 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700177 dev_err(&desc->intf->dev,
178 "nonzero urb status received: %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200179 break;
180 }
181 }
182
183 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700184 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
185 urb->actual_length);
Oliver Neukumafba9372008-05-13 17:01:25 +0200186 goto exit;
187 }
188
189 switch (dr->bNotificationType) {
190 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
191 dev_dbg(&desc->intf->dev,
192 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
193 dr->wIndex, dr->wLength);
194 break;
195
196 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
197
198 dev_dbg(&desc->intf->dev,
199 "NOTIFY_NETWORK_CONNECTION %s network",
200 dr->wValue ? "connected to" : "disconnected from");
201 goto exit;
202 default:
203 clear_bit(WDM_POLL_RUNNING, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700204 dev_err(&desc->intf->dev,
205 "unknown notification %d received: index %d len %d\n",
Oliver Neukumafba9372008-05-13 17:01:25 +0200206 dr->bNotificationType, dr->wIndex, dr->wLength);
207 goto exit;
208 }
209
210 req->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
211 req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
212 req->wValue = 0;
213 req->wIndex = desc->inum;
Oliver Neukum87d65e52008-06-19 14:20:18 +0200214 req->wLength = cpu_to_le16(desc->wMaxCommand);
Oliver Neukumafba9372008-05-13 17:01:25 +0200215
216 usb_fill_control_urb(
217 desc->response,
218 interface_to_usbdev(desc->intf),
219 /* using common endpoint 0 */
220 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
221 (unsigned char *)req,
222 desc->inbuf,
Oliver Neukum87d65e52008-06-19 14:20:18 +0200223 desc->wMaxCommand,
Oliver Neukumafba9372008-05-13 17:01:25 +0200224 wdm_in_callback,
225 desc
226 );
227 desc->response->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
228 spin_lock(&desc->iuspin);
229 clear_bit(WDM_READ, &desc->flags);
230 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
231 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
232 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
233 __func__, rv);
234 }
235 spin_unlock(&desc->iuspin);
236 if (rv < 0) {
237 if (rv == -EPERM)
238 return;
239 if (rv == -ENOMEM) {
240sw:
241 rv = schedule_work(&desc->rxwork);
242 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700243 dev_err(&desc->intf->dev,
244 "Cannot schedule work\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200245 }
246 }
247exit:
248 rv = usb_submit_urb(urb, GFP_ATOMIC);
249 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700250 dev_err(&desc->intf->dev,
251 "%s - usb_submit_urb failed with result %d\n",
252 __func__, rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200253
254}
255
256static void kill_urbs(struct wdm_device *desc)
257{
Oliver Neukum17d80d52008-06-24 15:56:10 +0200258 /* the order here is essential */
Oliver Neukumafba9372008-05-13 17:01:25 +0200259 usb_kill_urb(desc->command);
260 usb_kill_urb(desc->validity);
261 usb_kill_urb(desc->response);
262}
263
264static void free_urbs(struct wdm_device *desc)
265{
266 usb_free_urb(desc->validity);
267 usb_free_urb(desc->response);
268 usb_free_urb(desc->command);
269}
270
271static void cleanup(struct wdm_device *desc)
272{
273 usb_buffer_free(interface_to_usbdev(desc->intf),
274 desc->wMaxPacketSize,
275 desc->sbuf,
276 desc->validity->transfer_dma);
277 usb_buffer_free(interface_to_usbdev(desc->intf),
Oliver Neukum87d65e52008-06-19 14:20:18 +0200278 desc->wMaxCommand,
Oliver Neukumafba9372008-05-13 17:01:25 +0200279 desc->inbuf,
280 desc->response->transfer_dma);
281 kfree(desc->orq);
282 kfree(desc->irq);
283 kfree(desc->ubuf);
284 free_urbs(desc);
285 kfree(desc);
286}
287
288static ssize_t wdm_write
289(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
290{
291 u8 *buf;
292 int rv = -EMSGSIZE, r, we;
293 struct wdm_device *desc = file->private_data;
294 struct usb_ctrlrequest *req;
295
296 if (count > desc->wMaxCommand)
297 count = desc->wMaxCommand;
298
299 spin_lock_irq(&desc->iuspin);
300 we = desc->werr;
301 desc->werr = 0;
302 spin_unlock_irq(&desc->iuspin);
303 if (we < 0)
304 return -EIO;
305
Oliver Neukum860e41a2010-02-27 20:54:24 +0100306 desc->outbuf = buf = kmalloc(count, GFP_KERNEL);
307 if (!buf) {
308 rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200309 goto outnl;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100310 }
311
312 r = copy_from_user(buf, buffer, count);
313 if (r > 0) {
314 kfree(buf);
315 rv = -EFAULT;
316 goto outnl;
317 }
318
319 /* concurrent writes and disconnect */
320 r = mutex_lock_interruptible(&desc->lock);
321 rv = -ERESTARTSYS;
322 if (r) {
323 kfree(buf);
324 goto outnl;
325 }
326
327 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
328 kfree(buf);
329 rv = -ENODEV;
330 goto outnp;
331 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200332
Oliver Neukum17d80d52008-06-24 15:56:10 +0200333 r = usb_autopm_get_interface(desc->intf);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100334 if (r < 0) {
335 kfree(buf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200336 goto outnp;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100337 }
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200338
339 if (!file->f_flags && O_NONBLOCK)
340 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
341 &desc->flags));
342 else
343 if (test_bit(WDM_IN_USE, &desc->flags))
344 r = -EAGAIN;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100345 if (r < 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200346 kfree(buf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200347 goto out;
348 }
349
350 req = desc->orq;
351 usb_fill_control_urb(
352 desc->command,
353 interface_to_usbdev(desc->intf),
354 /* using common endpoint 0 */
355 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
356 (unsigned char *)req,
357 buf,
358 count,
359 wdm_out_callback,
360 desc
361 );
362
363 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
364 USB_RECIP_INTERFACE);
365 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
366 req->wValue = 0;
367 req->wIndex = desc->inum;
368 req->wLength = cpu_to_le16(count);
369 set_bit(WDM_IN_USE, &desc->flags);
370
371 rv = usb_submit_urb(desc->command, GFP_KERNEL);
372 if (rv < 0) {
373 kfree(buf);
374 clear_bit(WDM_IN_USE, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700375 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200376 } else {
377 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
378 req->wIndex);
379 }
380out:
Oliver Neukum17d80d52008-06-24 15:56:10 +0200381 usb_autopm_put_interface(desc->intf);
382outnp:
Oliver Neukum860e41a2010-02-27 20:54:24 +0100383 mutex_unlock(&desc->lock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200384outnl:
385 return rv < 0 ? rv : count;
386}
387
388static ssize_t wdm_read
389(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
390{
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200391 int rv, cntr = 0;
Oliver Neukumafba9372008-05-13 17:01:25 +0200392 int i = 0;
393 struct wdm_device *desc = file->private_data;
394
395
Oliver Neukum860e41a2010-02-27 20:54:24 +0100396 rv = mutex_lock_interruptible(&desc->lock); /*concurrent reads */
Oliver Neukumafba9372008-05-13 17:01:25 +0200397 if (rv < 0)
398 return -ERESTARTSYS;
399
400 if (desc->length == 0) {
401 desc->read = 0;
402retry:
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200403 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
404 rv = -ENODEV;
405 goto err;
406 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200407 i++;
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200408 if (file->f_flags & O_NONBLOCK) {
409 if (!test_bit(WDM_READ, &desc->flags)) {
410 rv = cntr ? cntr : -EAGAIN;
411 goto err;
412 }
413 rv = 0;
414 } else {
415 rv = wait_event_interruptible(desc->wait,
416 test_bit(WDM_READ, &desc->flags));
417 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200418
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200419 /* may have happened while we slept */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200420 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
421 rv = -ENODEV;
422 goto err;
423 }
424 usb_mark_last_busy(interface_to_usbdev(desc->intf));
Oliver Neukumafba9372008-05-13 17:01:25 +0200425 if (rv < 0) {
426 rv = -ERESTARTSYS;
427 goto err;
428 }
429
430 spin_lock_irq(&desc->iuspin);
431
432 if (desc->rerr) { /* read completed, error happened */
433 int t = desc->rerr;
434 desc->rerr = 0;
435 spin_unlock_irq(&desc->iuspin);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700436 dev_err(&desc->intf->dev,
437 "reading had resulted in %d\n", t);
Oliver Neukumafba9372008-05-13 17:01:25 +0200438 rv = -EIO;
439 goto err;
440 }
441 /*
442 * recheck whether we've lost the race
443 * against the completion handler
444 */
445 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
446 spin_unlock_irq(&desc->iuspin);
447 goto retry;
448 }
449 if (!desc->reslength) { /* zero length read */
450 spin_unlock_irq(&desc->iuspin);
451 goto retry;
452 }
453 clear_bit(WDM_READ, &desc->flags);
454 spin_unlock_irq(&desc->iuspin);
455 }
456
457 cntr = count > desc->length ? desc->length : count;
458 rv = copy_to_user(buffer, desc->ubuf, cntr);
459 if (rv > 0) {
460 rv = -EFAULT;
461 goto err;
462 }
463
464 for (i = 0; i < desc->length - cntr; i++)
465 desc->ubuf[i] = desc->ubuf[i + cntr];
466
467 desc->length -= cntr;
Oliver Neukum87d65e52008-06-19 14:20:18 +0200468 /* in case we had outstanding data */
469 if (!desc->length)
470 clear_bit(WDM_READ, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200471 rv = cntr;
472
473err:
Oliver Neukum860e41a2010-02-27 20:54:24 +0100474 mutex_unlock(&desc->lock);
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200475 if (rv < 0 && rv != -EAGAIN)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700476 dev_err(&desc->intf->dev, "wdm_read: exit error\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200477 return rv;
478}
479
480static int wdm_flush(struct file *file, fl_owner_t id)
481{
482 struct wdm_device *desc = file->private_data;
483
484 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
485 if (desc->werr < 0)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700486 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
487 desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200488
489 return desc->werr;
490}
491
492static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
493{
494 struct wdm_device *desc = file->private_data;
495 unsigned long flags;
496 unsigned int mask = 0;
497
498 spin_lock_irqsave(&desc->iuspin, flags);
499 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
500 mask = POLLERR;
501 spin_unlock_irqrestore(&desc->iuspin, flags);
502 goto desc_out;
503 }
504 if (test_bit(WDM_READ, &desc->flags))
505 mask = POLLIN | POLLRDNORM;
506 if (desc->rerr || desc->werr)
507 mask |= POLLERR;
508 if (!test_bit(WDM_IN_USE, &desc->flags))
509 mask |= POLLOUT | POLLWRNORM;
510 spin_unlock_irqrestore(&desc->iuspin, flags);
511
512 poll_wait(file, &desc->wait, wait);
513
514desc_out:
515 return mask;
516}
517
518static int wdm_open(struct inode *inode, struct file *file)
519{
520 int minor = iminor(inode);
521 int rv = -ENODEV;
522 struct usb_interface *intf;
523 struct wdm_device *desc;
524
525 mutex_lock(&wdm_mutex);
526 intf = usb_find_interface(&wdm_driver, minor);
527 if (!intf)
528 goto out;
529
530 desc = usb_get_intfdata(intf);
531 if (test_bit(WDM_DISCONNECTING, &desc->flags))
532 goto out;
Oliver Neukumafba9372008-05-13 17:01:25 +0200533 file->private_data = desc;
534
Oliver Neukum17d80d52008-06-24 15:56:10 +0200535 rv = usb_autopm_get_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200536 if (rv < 0) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700537 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200538 goto out;
539 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200540 intf->needs_remote_wakeup = 1;
Oliver Neukumafba9372008-05-13 17:01:25 +0200541
Oliver Neukum860e41a2010-02-27 20:54:24 +0100542 mutex_lock(&desc->lock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200543 if (!desc->count++) {
544 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
545 if (rv < 0) {
546 desc->count--;
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700547 dev_err(&desc->intf->dev,
548 "Error submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200549 }
550 } else {
551 rv = 0;
552 }
Oliver Neukum860e41a2010-02-27 20:54:24 +0100553 mutex_unlock(&desc->lock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200554 usb_autopm_put_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200555out:
556 mutex_unlock(&wdm_mutex);
557 return rv;
558}
559
560static int wdm_release(struct inode *inode, struct file *file)
561{
562 struct wdm_device *desc = file->private_data;
563
564 mutex_lock(&wdm_mutex);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100565 mutex_lock(&desc->lock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200566 desc->count--;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100567 mutex_unlock(&desc->lock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200568
Oliver Neukumafba9372008-05-13 17:01:25 +0200569 if (!desc->count) {
570 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
571 kill_urbs(desc);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200572 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
573 desc->intf->needs_remote_wakeup = 0;
Oliver Neukumafba9372008-05-13 17:01:25 +0200574 }
575 mutex_unlock(&wdm_mutex);
576 return 0;
577}
578
579static const struct file_operations wdm_fops = {
580 .owner = THIS_MODULE,
581 .read = wdm_read,
582 .write = wdm_write,
583 .open = wdm_open,
584 .flush = wdm_flush,
585 .release = wdm_release,
586 .poll = wdm_poll
587};
588
589static struct usb_class_driver wdm_class = {
590 .name = "cdc-wdm%d",
591 .fops = &wdm_fops,
592 .minor_base = WDM_MINOR_BASE,
593};
594
595/* --- error handling --- */
596static void wdm_rxwork(struct work_struct *work)
597{
598 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
599 unsigned long flags;
600 int rv;
601
602 spin_lock_irqsave(&desc->iuspin, flags);
603 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
604 spin_unlock_irqrestore(&desc->iuspin, flags);
605 } else {
606 spin_unlock_irqrestore(&desc->iuspin, flags);
607 rv = usb_submit_urb(desc->response, GFP_KERNEL);
608 if (rv < 0 && rv != -EPERM) {
609 spin_lock_irqsave(&desc->iuspin, flags);
610 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
611 schedule_work(&desc->rxwork);
612 spin_unlock_irqrestore(&desc->iuspin, flags);
613 }
614 }
615}
616
617/* --- hotplug --- */
618
619static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
620{
621 int rv = -EINVAL;
622 struct usb_device *udev = interface_to_usbdev(intf);
623 struct wdm_device *desc;
624 struct usb_host_interface *iface;
625 struct usb_endpoint_descriptor *ep;
626 struct usb_cdc_dmm_desc *dmhd;
627 u8 *buffer = intf->altsetting->extra;
628 int buflen = intf->altsetting->extralen;
629 u16 maxcom = 0;
630
631 if (!buffer)
632 goto out;
633
Oliver Neukum052fbc02009-04-20 17:24:49 +0200634 while (buflen > 2) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200635 if (buffer [1] != USB_DT_CS_INTERFACE) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700636 dev_err(&intf->dev, "skipping garbage\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200637 goto next_desc;
638 }
639
640 switch (buffer [2]) {
641 case USB_CDC_HEADER_TYPE:
642 break;
643 case USB_CDC_DMM_TYPE:
644 dmhd = (struct usb_cdc_dmm_desc *)buffer;
645 maxcom = le16_to_cpu(dmhd->wMaxCommand);
646 dev_dbg(&intf->dev,
647 "Finding maximum buffer length: %d", maxcom);
648 break;
649 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700650 dev_err(&intf->dev,
651 "Ignoring extra header, type %d, length %d\n",
Oliver Neukumafba9372008-05-13 17:01:25 +0200652 buffer[2], buffer[0]);
653 break;
654 }
655next_desc:
656 buflen -= buffer[0];
657 buffer += buffer[0];
658 }
659
660 rv = -ENOMEM;
661 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
662 if (!desc)
663 goto out;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100664 mutex_init(&desc->lock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200665 spin_lock_init(&desc->iuspin);
666 init_waitqueue_head(&desc->wait);
667 desc->wMaxCommand = maxcom;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200668 /* this will be expanded and needed in hardware endianness */
Oliver Neukumafba9372008-05-13 17:01:25 +0200669 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
670 desc->intf = intf;
671 INIT_WORK(&desc->rxwork, wdm_rxwork);
672
Oliver Neukum052fbc02009-04-20 17:24:49 +0200673 rv = -EINVAL;
674 iface = intf->cur_altsetting;
675 if (iface->desc.bNumEndpoints != 1)
Oliver Neukumafba9372008-05-13 17:01:25 +0200676 goto err;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200677 ep = &iface->endpoint[0].desc;
678 if (!ep || !usb_endpoint_is_int_in(ep))
679 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200680
Al Virofa4144b2008-06-02 10:59:02 +0100681 desc->wMaxPacketSize = le16_to_cpu(ep->wMaxPacketSize);
682 desc->bMaxPacketSize0 = udev->descriptor.bMaxPacketSize0;
Oliver Neukumafba9372008-05-13 17:01:25 +0200683
684 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
685 if (!desc->orq)
686 goto err;
687 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
688 if (!desc->irq)
689 goto err;
690
691 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
692 if (!desc->validity)
693 goto err;
694
695 desc->response = usb_alloc_urb(0, GFP_KERNEL);
696 if (!desc->response)
697 goto err;
698
699 desc->command = usb_alloc_urb(0, GFP_KERNEL);
700 if (!desc->command)
701 goto err;
702
703 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
704 if (!desc->ubuf)
705 goto err;
706
707 desc->sbuf = usb_buffer_alloc(interface_to_usbdev(intf),
708 desc->wMaxPacketSize,
709 GFP_KERNEL,
710 &desc->validity->transfer_dma);
711 if (!desc->sbuf)
712 goto err;
713
714 desc->inbuf = usb_buffer_alloc(interface_to_usbdev(intf),
715 desc->bMaxPacketSize0,
716 GFP_KERNEL,
717 &desc->response->transfer_dma);
718 if (!desc->inbuf)
719 goto err2;
720
721 usb_fill_int_urb(
722 desc->validity,
723 interface_to_usbdev(intf),
724 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
725 desc->sbuf,
726 desc->wMaxPacketSize,
727 wdm_int_callback,
728 desc,
729 ep->bInterval
730 );
731 desc->validity->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
732
733 usb_set_intfdata(intf, desc);
734 rv = usb_register_dev(intf, &wdm_class);
Oliver Neukumafba9372008-05-13 17:01:25 +0200735 if (rv < 0)
Oliver Neukum052fbc02009-04-20 17:24:49 +0200736 goto err3;
737 else
738 dev_info(&intf->dev, "cdc-wdm%d: USB WDM device\n",
739 intf->minor - WDM_MINOR_BASE);
Oliver Neukumafba9372008-05-13 17:01:25 +0200740out:
741 return rv;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200742err3:
743 usb_set_intfdata(intf, NULL);
744 usb_buffer_free(interface_to_usbdev(desc->intf),
745 desc->bMaxPacketSize0,
746 desc->inbuf,
747 desc->response->transfer_dma);
Oliver Neukumafba9372008-05-13 17:01:25 +0200748err2:
749 usb_buffer_free(interface_to_usbdev(desc->intf),
750 desc->wMaxPacketSize,
751 desc->sbuf,
752 desc->validity->transfer_dma);
753err:
754 free_urbs(desc);
755 kfree(desc->ubuf);
756 kfree(desc->orq);
757 kfree(desc->irq);
758 kfree(desc);
759 return rv;
760}
761
762static void wdm_disconnect(struct usb_interface *intf)
763{
764 struct wdm_device *desc;
765 unsigned long flags;
766
767 usb_deregister_dev(intf, &wdm_class);
768 mutex_lock(&wdm_mutex);
769 desc = usb_get_intfdata(intf);
770
771 /* the spinlock makes sure no new urbs are generated in the callbacks */
772 spin_lock_irqsave(&desc->iuspin, flags);
773 set_bit(WDM_DISCONNECTING, &desc->flags);
774 set_bit(WDM_READ, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200775 /* to terminate pending flushes */
Oliver Neukumafba9372008-05-13 17:01:25 +0200776 clear_bit(WDM_IN_USE, &desc->flags);
777 spin_unlock_irqrestore(&desc->iuspin, flags);
778 cancel_work_sync(&desc->rxwork);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100779 mutex_lock(&desc->lock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200780 kill_urbs(desc);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100781 mutex_unlock(&desc->lock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200782 wake_up_all(&desc->wait);
783 if (!desc->count)
784 cleanup(desc);
785 mutex_unlock(&wdm_mutex);
786}
787
Oliver Neukum17d80d52008-06-24 15:56:10 +0200788static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
789{
790 struct wdm_device *desc = usb_get_intfdata(intf);
791 int rv = 0;
792
793 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
794
Oliver Neukum860e41a2010-02-27 20:54:24 +0100795 mutex_lock(&desc->lock);
Oliver Neukum35758582008-07-01 19:10:08 +0200796#ifdef CONFIG_PM
Alan Stern65bfd292008-11-25 16:39:18 -0500797 if ((message.event & PM_EVENT_AUTO) &&
798 test_bit(WDM_IN_USE, &desc->flags)) {
Oliver Neukum17d80d52008-06-24 15:56:10 +0200799 rv = -EBUSY;
800 } else {
Oliver Neukum35758582008-07-01 19:10:08 +0200801#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200802 cancel_work_sync(&desc->rxwork);
803 kill_urbs(desc);
Oliver Neukum35758582008-07-01 19:10:08 +0200804#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200805 }
Oliver Neukum35758582008-07-01 19:10:08 +0200806#endif
Oliver Neukum860e41a2010-02-27 20:54:24 +0100807 mutex_unlock(&desc->lock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200808
809 return rv;
810}
811
812static int recover_from_urb_loss(struct wdm_device *desc)
813{
814 int rv = 0;
815
816 if (desc->count) {
817 rv = usb_submit_urb(desc->validity, GFP_NOIO);
818 if (rv < 0)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700819 dev_err(&desc->intf->dev,
820 "Error resume submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200821 }
822 return rv;
823}
824static int wdm_resume(struct usb_interface *intf)
825{
826 struct wdm_device *desc = usb_get_intfdata(intf);
827 int rv;
828
829 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100830 mutex_lock(&desc->lock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200831 rv = recover_from_urb_loss(desc);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100832 mutex_unlock(&desc->lock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200833 return rv;
834}
835
836static int wdm_pre_reset(struct usb_interface *intf)
837{
838 struct wdm_device *desc = usb_get_intfdata(intf);
839
Oliver Neukum860e41a2010-02-27 20:54:24 +0100840 mutex_lock(&desc->lock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200841 return 0;
842}
843
844static int wdm_post_reset(struct usb_interface *intf)
845{
846 struct wdm_device *desc = usb_get_intfdata(intf);
847 int rv;
848
849 rv = recover_from_urb_loss(desc);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100850 mutex_unlock(&desc->lock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200851 return 0;
852}
853
Oliver Neukumafba9372008-05-13 17:01:25 +0200854static struct usb_driver wdm_driver = {
855 .name = "cdc_wdm",
856 .probe = wdm_probe,
857 .disconnect = wdm_disconnect,
Oliver Neukum17d80d52008-06-24 15:56:10 +0200858 .suspend = wdm_suspend,
859 .resume = wdm_resume,
860 .reset_resume = wdm_resume,
861 .pre_reset = wdm_pre_reset,
862 .post_reset = wdm_post_reset,
Oliver Neukumafba9372008-05-13 17:01:25 +0200863 .id_table = wdm_ids,
Oliver Neukum17d80d52008-06-24 15:56:10 +0200864 .supports_autosuspend = 1,
Oliver Neukumafba9372008-05-13 17:01:25 +0200865};
866
867/* --- low level module stuff --- */
868
869static int __init wdm_init(void)
870{
871 int rv;
872
873 rv = usb_register(&wdm_driver);
874
875 return rv;
876}
877
878static void __exit wdm_exit(void)
879{
880 usb_deregister(&wdm_driver);
881}
882
883module_init(wdm_init);
884module_exit(wdm_exit);
885
886MODULE_AUTHOR(DRIVER_AUTHOR);
Oliver Neukum87d65e52008-06-19 14:20:18 +0200887MODULE_DESCRIPTION(DRIVER_DESC);
Oliver Neukumafba9372008-05-13 17:01:25 +0200888MODULE_LICENSE("GPL");