blob: 8909058b1bb1f5af4e85357a4fa6dd92e82ca8ca [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
Oliver Neukum922a5ea2010-02-27 20:54:59 +010055#define WDM_RESPONDING 7
Oliver Neukumbeb1d352010-02-27 20:55:52 +010056#define WDM_SUSPENDING 8
Oliver Neukumafba9372008-05-13 17:01:25 +020057
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;
Oliver Neukum860e41a2010-02-27 20:54:24 +010091 struct mutex lock;
Oliver Neukumafba9372008-05-13 17:01:25 +020092 wait_queue_head_t wait;
93 struct work_struct rxwork;
94 int werr;
95 int rerr;
96};
97
98static struct usb_driver wdm_driver;
99
100/* --- callbacks --- */
101static void wdm_out_callback(struct urb *urb)
102{
103 struct wdm_device *desc;
104 desc = urb->context;
105 spin_lock(&desc->iuspin);
106 desc->werr = urb->status;
107 spin_unlock(&desc->iuspin);
108 clear_bit(WDM_IN_USE, &desc->flags);
109 kfree(desc->outbuf);
110 wake_up(&desc->wait);
111}
112
113static void wdm_in_callback(struct urb *urb)
114{
115 struct wdm_device *desc = urb->context;
116 int status = urb->status;
117
118 spin_lock(&desc->iuspin);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100119 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200120
121 if (status) {
122 switch (status) {
123 case -ENOENT:
124 dev_dbg(&desc->intf->dev,
125 "nonzero urb status received: -ENOENT");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100126 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200127 case -ECONNRESET:
128 dev_dbg(&desc->intf->dev,
129 "nonzero urb status received: -ECONNRESET");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100130 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200131 case -ESHUTDOWN:
132 dev_dbg(&desc->intf->dev,
133 "nonzero urb status received: -ESHUTDOWN");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100134 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200135 case -EPIPE:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700136 dev_err(&desc->intf->dev,
137 "nonzero urb status received: -EPIPE\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200138 break;
139 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700140 dev_err(&desc->intf->dev,
141 "Unexpected error %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200142 break;
143 }
144 }
145
146 desc->rerr = status;
147 desc->reslength = urb->actual_length;
148 memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength);
149 desc->length += desc->reslength;
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100150skip_error:
Oliver Neukumafba9372008-05-13 17:01:25 +0200151 wake_up(&desc->wait);
152
153 set_bit(WDM_READ, &desc->flags);
154 spin_unlock(&desc->iuspin);
155}
156
157static void wdm_int_callback(struct urb *urb)
158{
159 int rv = 0;
160 int status = urb->status;
161 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200162 struct usb_cdc_notification *dr;
163
164 desc = urb->context;
Oliver Neukumafba9372008-05-13 17:01:25 +0200165 dr = (struct usb_cdc_notification *)desc->sbuf;
166
167 if (status) {
168 switch (status) {
169 case -ESHUTDOWN:
170 case -ENOENT:
171 case -ECONNRESET:
172 return; /* unplug */
173 case -EPIPE:
174 set_bit(WDM_INT_STALL, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700175 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200176 goto sw; /* halt is cleared in work */
177 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700178 dev_err(&desc->intf->dev,
179 "nonzero urb status received: %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200180 break;
181 }
182 }
183
184 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700185 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
186 urb->actual_length);
Oliver Neukumafba9372008-05-13 17:01:25 +0200187 goto exit;
188 }
189
190 switch (dr->bNotificationType) {
191 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
192 dev_dbg(&desc->intf->dev,
193 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
194 dr->wIndex, dr->wLength);
195 break;
196
197 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
198
199 dev_dbg(&desc->intf->dev,
200 "NOTIFY_NETWORK_CONNECTION %s network",
201 dr->wValue ? "connected to" : "disconnected from");
202 goto exit;
203 default:
204 clear_bit(WDM_POLL_RUNNING, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700205 dev_err(&desc->intf->dev,
206 "unknown notification %d received: index %d len %d\n",
Oliver Neukumafba9372008-05-13 17:01:25 +0200207 dr->bNotificationType, dr->wIndex, dr->wLength);
208 goto exit;
209 }
210
Oliver Neukumafba9372008-05-13 17:01:25 +0200211 spin_lock(&desc->iuspin);
212 clear_bit(WDM_READ, &desc->flags);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100213 set_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100214 if (!test_bit(WDM_DISCONNECTING, &desc->flags)
215 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200216 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
217 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
218 __func__, rv);
219 }
220 spin_unlock(&desc->iuspin);
221 if (rv < 0) {
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100222 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200223 if (rv == -EPERM)
224 return;
225 if (rv == -ENOMEM) {
226sw:
227 rv = schedule_work(&desc->rxwork);
228 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700229 dev_err(&desc->intf->dev,
230 "Cannot schedule work\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200231 }
232 }
233exit:
234 rv = usb_submit_urb(urb, GFP_ATOMIC);
235 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700236 dev_err(&desc->intf->dev,
237 "%s - usb_submit_urb failed with result %d\n",
238 __func__, rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200239
240}
241
242static void kill_urbs(struct wdm_device *desc)
243{
Oliver Neukum17d80d52008-06-24 15:56:10 +0200244 /* the order here is essential */
Oliver Neukumafba9372008-05-13 17:01:25 +0200245 usb_kill_urb(desc->command);
246 usb_kill_urb(desc->validity);
247 usb_kill_urb(desc->response);
248}
249
250static void free_urbs(struct wdm_device *desc)
251{
252 usb_free_urb(desc->validity);
253 usb_free_urb(desc->response);
254 usb_free_urb(desc->command);
255}
256
257static void cleanup(struct wdm_device *desc)
258{
Bjørn Mork8457d992012-01-16 15:12:00 +0100259 kfree(desc->sbuf);
260 kfree(desc->inbuf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200261 kfree(desc->orq);
262 kfree(desc->irq);
263 kfree(desc->ubuf);
264 free_urbs(desc);
265 kfree(desc);
266}
267
268static ssize_t wdm_write
269(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
270{
271 u8 *buf;
272 int rv = -EMSGSIZE, r, we;
273 struct wdm_device *desc = file->private_data;
274 struct usb_ctrlrequest *req;
275
276 if (count > desc->wMaxCommand)
277 count = desc->wMaxCommand;
278
279 spin_lock_irq(&desc->iuspin);
280 we = desc->werr;
281 desc->werr = 0;
282 spin_unlock_irq(&desc->iuspin);
283 if (we < 0)
284 return -EIO;
285
Oliver Neukum860e41a2010-02-27 20:54:24 +0100286 desc->outbuf = buf = kmalloc(count, GFP_KERNEL);
287 if (!buf) {
288 rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200289 goto outnl;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100290 }
291
292 r = copy_from_user(buf, buffer, count);
293 if (r > 0) {
294 kfree(buf);
295 rv = -EFAULT;
296 goto outnl;
297 }
298
299 /* concurrent writes and disconnect */
300 r = mutex_lock_interruptible(&desc->lock);
301 rv = -ERESTARTSYS;
302 if (r) {
303 kfree(buf);
304 goto outnl;
305 }
306
307 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
308 kfree(buf);
309 rv = -ENODEV;
310 goto outnp;
311 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200312
Oliver Neukum17d80d52008-06-24 15:56:10 +0200313 r = usb_autopm_get_interface(desc->intf);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100314 if (r < 0) {
315 kfree(buf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200316 goto outnp;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100317 }
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200318
David Sterba0cdfb812010-12-27 18:49:58 +0100319 if (!(file->f_flags & O_NONBLOCK))
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200320 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
321 &desc->flags));
322 else
323 if (test_bit(WDM_IN_USE, &desc->flags))
324 r = -EAGAIN;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100325 if (r < 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200326 kfree(buf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200327 goto out;
328 }
329
330 req = desc->orq;
331 usb_fill_control_urb(
332 desc->command,
333 interface_to_usbdev(desc->intf),
334 /* using common endpoint 0 */
335 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
336 (unsigned char *)req,
337 buf,
338 count,
339 wdm_out_callback,
340 desc
341 );
342
343 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
344 USB_RECIP_INTERFACE);
345 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
346 req->wValue = 0;
347 req->wIndex = desc->inum;
348 req->wLength = cpu_to_le16(count);
349 set_bit(WDM_IN_USE, &desc->flags);
350
351 rv = usb_submit_urb(desc->command, GFP_KERNEL);
352 if (rv < 0) {
353 kfree(buf);
354 clear_bit(WDM_IN_USE, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700355 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200356 } else {
357 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
358 req->wIndex);
359 }
360out:
Oliver Neukum17d80d52008-06-24 15:56:10 +0200361 usb_autopm_put_interface(desc->intf);
362outnp:
Oliver Neukum860e41a2010-02-27 20:54:24 +0100363 mutex_unlock(&desc->lock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200364outnl:
365 return rv < 0 ? rv : count;
366}
367
368static ssize_t wdm_read
369(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
370{
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200371 int rv, cntr = 0;
Oliver Neukumafba9372008-05-13 17:01:25 +0200372 int i = 0;
373 struct wdm_device *desc = file->private_data;
374
375
Oliver Neukum860e41a2010-02-27 20:54:24 +0100376 rv = mutex_lock_interruptible(&desc->lock); /*concurrent reads */
Oliver Neukumafba9372008-05-13 17:01:25 +0200377 if (rv < 0)
378 return -ERESTARTSYS;
379
380 if (desc->length == 0) {
381 desc->read = 0;
382retry:
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200383 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
384 rv = -ENODEV;
385 goto err;
386 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200387 i++;
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200388 if (file->f_flags & O_NONBLOCK) {
389 if (!test_bit(WDM_READ, &desc->flags)) {
390 rv = cntr ? cntr : -EAGAIN;
391 goto err;
392 }
393 rv = 0;
394 } else {
395 rv = wait_event_interruptible(desc->wait,
396 test_bit(WDM_READ, &desc->flags));
397 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200398
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200399 /* may have happened while we slept */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200400 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
401 rv = -ENODEV;
402 goto err;
403 }
404 usb_mark_last_busy(interface_to_usbdev(desc->intf));
Oliver Neukumafba9372008-05-13 17:01:25 +0200405 if (rv < 0) {
406 rv = -ERESTARTSYS;
407 goto err;
408 }
409
410 spin_lock_irq(&desc->iuspin);
411
412 if (desc->rerr) { /* read completed, error happened */
Oliver Neukumafba9372008-05-13 17:01:25 +0200413 desc->rerr = 0;
414 spin_unlock_irq(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200415 rv = -EIO;
416 goto err;
417 }
418 /*
419 * recheck whether we've lost the race
420 * against the completion handler
421 */
422 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
423 spin_unlock_irq(&desc->iuspin);
424 goto retry;
425 }
426 if (!desc->reslength) { /* zero length read */
427 spin_unlock_irq(&desc->iuspin);
428 goto retry;
429 }
430 clear_bit(WDM_READ, &desc->flags);
431 spin_unlock_irq(&desc->iuspin);
432 }
433
434 cntr = count > desc->length ? desc->length : count;
435 rv = copy_to_user(buffer, desc->ubuf, cntr);
436 if (rv > 0) {
437 rv = -EFAULT;
438 goto err;
439 }
440
441 for (i = 0; i < desc->length - cntr; i++)
442 desc->ubuf[i] = desc->ubuf[i + cntr];
443
444 desc->length -= cntr;
Oliver Neukum87d65e52008-06-19 14:20:18 +0200445 /* in case we had outstanding data */
446 if (!desc->length)
447 clear_bit(WDM_READ, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200448 rv = cntr;
449
450err:
Oliver Neukum860e41a2010-02-27 20:54:24 +0100451 mutex_unlock(&desc->lock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200452 return rv;
453}
454
455static int wdm_flush(struct file *file, fl_owner_t id)
456{
457 struct wdm_device *desc = file->private_data;
458
459 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
460 if (desc->werr < 0)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700461 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
462 desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200463
464 return desc->werr;
465}
466
467static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
468{
469 struct wdm_device *desc = file->private_data;
470 unsigned long flags;
471 unsigned int mask = 0;
472
473 spin_lock_irqsave(&desc->iuspin, flags);
474 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
475 mask = POLLERR;
476 spin_unlock_irqrestore(&desc->iuspin, flags);
477 goto desc_out;
478 }
479 if (test_bit(WDM_READ, &desc->flags))
480 mask = POLLIN | POLLRDNORM;
481 if (desc->rerr || desc->werr)
482 mask |= POLLERR;
483 if (!test_bit(WDM_IN_USE, &desc->flags))
484 mask |= POLLOUT | POLLWRNORM;
485 spin_unlock_irqrestore(&desc->iuspin, flags);
486
487 poll_wait(file, &desc->wait, wait);
488
489desc_out:
490 return mask;
491}
492
493static int wdm_open(struct inode *inode, struct file *file)
494{
495 int minor = iminor(inode);
496 int rv = -ENODEV;
497 struct usb_interface *intf;
498 struct wdm_device *desc;
499
500 mutex_lock(&wdm_mutex);
501 intf = usb_find_interface(&wdm_driver, minor);
502 if (!intf)
503 goto out;
504
505 desc = usb_get_intfdata(intf);
506 if (test_bit(WDM_DISCONNECTING, &desc->flags))
507 goto out;
Oliver Neukumafba9372008-05-13 17:01:25 +0200508 file->private_data = desc;
509
Oliver Neukum17d80d52008-06-24 15:56:10 +0200510 rv = usb_autopm_get_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200511 if (rv < 0) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700512 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200513 goto out;
514 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200515 intf->needs_remote_wakeup = 1;
Oliver Neukumafba9372008-05-13 17:01:25 +0200516
Oliver Neukum860e41a2010-02-27 20:54:24 +0100517 mutex_lock(&desc->lock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200518 if (!desc->count++) {
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200519 desc->werr = 0;
520 desc->rerr = 0;
Oliver Neukum17d80d52008-06-24 15:56:10 +0200521 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
522 if (rv < 0) {
523 desc->count--;
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700524 dev_err(&desc->intf->dev,
525 "Error submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200526 }
527 } else {
528 rv = 0;
529 }
Oliver Neukum860e41a2010-02-27 20:54:24 +0100530 mutex_unlock(&desc->lock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200531 usb_autopm_put_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200532out:
533 mutex_unlock(&wdm_mutex);
534 return rv;
535}
536
537static int wdm_release(struct inode *inode, struct file *file)
538{
539 struct wdm_device *desc = file->private_data;
540
541 mutex_lock(&wdm_mutex);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100542 mutex_lock(&desc->lock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200543 desc->count--;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100544 mutex_unlock(&desc->lock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200545
Oliver Neukumafba9372008-05-13 17:01:25 +0200546 if (!desc->count) {
547 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
548 kill_urbs(desc);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200549 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
550 desc->intf->needs_remote_wakeup = 0;
Oliver Neukumafba9372008-05-13 17:01:25 +0200551 }
552 mutex_unlock(&wdm_mutex);
553 return 0;
554}
555
556static const struct file_operations wdm_fops = {
557 .owner = THIS_MODULE,
558 .read = wdm_read,
559 .write = wdm_write,
560 .open = wdm_open,
561 .flush = wdm_flush,
562 .release = wdm_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200563 .poll = wdm_poll,
564 .llseek = noop_llseek,
Oliver Neukumafba9372008-05-13 17:01:25 +0200565};
566
567static struct usb_class_driver wdm_class = {
568 .name = "cdc-wdm%d",
569 .fops = &wdm_fops,
570 .minor_base = WDM_MINOR_BASE,
571};
572
573/* --- error handling --- */
574static void wdm_rxwork(struct work_struct *work)
575{
576 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
577 unsigned long flags;
578 int rv;
579
580 spin_lock_irqsave(&desc->iuspin, flags);
581 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
582 spin_unlock_irqrestore(&desc->iuspin, flags);
583 } else {
584 spin_unlock_irqrestore(&desc->iuspin, flags);
585 rv = usb_submit_urb(desc->response, GFP_KERNEL);
586 if (rv < 0 && rv != -EPERM) {
587 spin_lock_irqsave(&desc->iuspin, flags);
588 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
589 schedule_work(&desc->rxwork);
590 spin_unlock_irqrestore(&desc->iuspin, flags);
591 }
592 }
593}
594
595/* --- hotplug --- */
596
597static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
598{
599 int rv = -EINVAL;
600 struct usb_device *udev = interface_to_usbdev(intf);
601 struct wdm_device *desc;
602 struct usb_host_interface *iface;
603 struct usb_endpoint_descriptor *ep;
604 struct usb_cdc_dmm_desc *dmhd;
605 u8 *buffer = intf->altsetting->extra;
606 int buflen = intf->altsetting->extralen;
607 u16 maxcom = 0;
608
609 if (!buffer)
610 goto out;
611
Oliver Neukum052fbc02009-04-20 17:24:49 +0200612 while (buflen > 2) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200613 if (buffer [1] != USB_DT_CS_INTERFACE) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700614 dev_err(&intf->dev, "skipping garbage\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200615 goto next_desc;
616 }
617
618 switch (buffer [2]) {
619 case USB_CDC_HEADER_TYPE:
620 break;
621 case USB_CDC_DMM_TYPE:
622 dmhd = (struct usb_cdc_dmm_desc *)buffer;
623 maxcom = le16_to_cpu(dmhd->wMaxCommand);
624 dev_dbg(&intf->dev,
625 "Finding maximum buffer length: %d", maxcom);
626 break;
627 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700628 dev_err(&intf->dev,
629 "Ignoring extra header, type %d, length %d\n",
Oliver Neukumafba9372008-05-13 17:01:25 +0200630 buffer[2], buffer[0]);
631 break;
632 }
633next_desc:
634 buflen -= buffer[0];
635 buffer += buffer[0];
636 }
637
638 rv = -ENOMEM;
639 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
640 if (!desc)
641 goto out;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100642 mutex_init(&desc->lock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200643 spin_lock_init(&desc->iuspin);
644 init_waitqueue_head(&desc->wait);
645 desc->wMaxCommand = maxcom;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200646 /* this will be expanded and needed in hardware endianness */
Oliver Neukumafba9372008-05-13 17:01:25 +0200647 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
648 desc->intf = intf;
649 INIT_WORK(&desc->rxwork, wdm_rxwork);
650
Oliver Neukum052fbc02009-04-20 17:24:49 +0200651 rv = -EINVAL;
652 iface = intf->cur_altsetting;
653 if (iface->desc.bNumEndpoints != 1)
Oliver Neukumafba9372008-05-13 17:01:25 +0200654 goto err;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200655 ep = &iface->endpoint[0].desc;
656 if (!ep || !usb_endpoint_is_int_in(ep))
657 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200658
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700659 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
Al Virofa4144b2008-06-02 10:59:02 +0100660 desc->bMaxPacketSize0 = udev->descriptor.bMaxPacketSize0;
Oliver Neukumafba9372008-05-13 17:01:25 +0200661
662 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
663 if (!desc->orq)
664 goto err;
665 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
666 if (!desc->irq)
667 goto err;
668
669 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
670 if (!desc->validity)
671 goto err;
672
673 desc->response = usb_alloc_urb(0, GFP_KERNEL);
674 if (!desc->response)
675 goto err;
676
677 desc->command = usb_alloc_urb(0, GFP_KERNEL);
678 if (!desc->command)
679 goto err;
680
681 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
682 if (!desc->ubuf)
683 goto err;
684
Bjørn Mork8457d992012-01-16 15:12:00 +0100685 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200686 if (!desc->sbuf)
687 goto err;
688
Bjørn Mork8457d992012-01-16 15:12:00 +0100689 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200690 if (!desc->inbuf)
Bjørn Mork8457d992012-01-16 15:12:00 +0100691 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200692
693 usb_fill_int_urb(
694 desc->validity,
695 interface_to_usbdev(intf),
696 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
697 desc->sbuf,
698 desc->wMaxPacketSize,
699 wdm_int_callback,
700 desc,
701 ep->bInterval
702 );
Oliver Neukumafba9372008-05-13 17:01:25 +0200703
Bjørn Mork19b85b32012-01-16 15:11:58 +0100704 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
705 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
706 desc->irq->wValue = 0;
707 desc->irq->wIndex = desc->inum;
708 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
709
710 usb_fill_control_urb(
711 desc->response,
712 interface_to_usbdev(desc->intf),
713 /* using common endpoint 0 */
714 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
715 (unsigned char *)desc->irq,
716 desc->inbuf,
717 desc->wMaxCommand,
718 wdm_in_callback,
719 desc
720 );
Bjørn Mork19b85b32012-01-16 15:11:58 +0100721
Oliver Neukumafba9372008-05-13 17:01:25 +0200722 usb_set_intfdata(intf, desc);
723 rv = usb_register_dev(intf, &wdm_class);
Oliver Neukumafba9372008-05-13 17:01:25 +0200724 if (rv < 0)
Bjørn Mork8457d992012-01-16 15:12:00 +0100725 goto err2;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200726 else
727 dev_info(&intf->dev, "cdc-wdm%d: USB WDM device\n",
728 intf->minor - WDM_MINOR_BASE);
Oliver Neukumafba9372008-05-13 17:01:25 +0200729out:
730 return rv;
731err2:
Bjørn Mork8457d992012-01-16 15:12:00 +0100732 usb_set_intfdata(intf, NULL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200733err:
734 free_urbs(desc);
Bjørn Mork8457d992012-01-16 15:12:00 +0100735 kfree(desc->inbuf);
736 kfree(desc->sbuf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200737 kfree(desc->ubuf);
738 kfree(desc->orq);
739 kfree(desc->irq);
740 kfree(desc);
741 return rv;
742}
743
744static void wdm_disconnect(struct usb_interface *intf)
745{
746 struct wdm_device *desc;
747 unsigned long flags;
748
749 usb_deregister_dev(intf, &wdm_class);
750 mutex_lock(&wdm_mutex);
751 desc = usb_get_intfdata(intf);
752
753 /* the spinlock makes sure no new urbs are generated in the callbacks */
754 spin_lock_irqsave(&desc->iuspin, flags);
755 set_bit(WDM_DISCONNECTING, &desc->flags);
756 set_bit(WDM_READ, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200757 /* to terminate pending flushes */
Oliver Neukumafba9372008-05-13 17:01:25 +0200758 clear_bit(WDM_IN_USE, &desc->flags);
759 spin_unlock_irqrestore(&desc->iuspin, flags);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100760 mutex_lock(&desc->lock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200761 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100762 cancel_work_sync(&desc->rxwork);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100763 mutex_unlock(&desc->lock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200764 wake_up_all(&desc->wait);
765 if (!desc->count)
766 cleanup(desc);
767 mutex_unlock(&wdm_mutex);
768}
769
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100770#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200771static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
772{
773 struct wdm_device *desc = usb_get_intfdata(intf);
774 int rv = 0;
775
776 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
777
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100778 /* if this is an autosuspend the caller does the locking */
Alan Stern5b1b0b82011-08-19 23:49:48 +0200779 if (!PMSG_IS_AUTO(message))
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100780 mutex_lock(&desc->lock);
Oliver Neukum62e66852010-02-27 20:56:22 +0100781 spin_lock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100782
Alan Stern5b1b0b82011-08-19 23:49:48 +0200783 if (PMSG_IS_AUTO(message) &&
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100784 (test_bit(WDM_IN_USE, &desc->flags)
785 || test_bit(WDM_RESPONDING, &desc->flags))) {
Oliver Neukum62e66852010-02-27 20:56:22 +0100786 spin_unlock_irq(&desc->iuspin);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200787 rv = -EBUSY;
788 } else {
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100789
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100790 set_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100791 spin_unlock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100792 /* callback submits work - order is essential */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200793 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100794 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200795 }
Alan Stern5b1b0b82011-08-19 23:49:48 +0200796 if (!PMSG_IS_AUTO(message))
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100797 mutex_unlock(&desc->lock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200798
799 return rv;
800}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100801#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200802
803static int recover_from_urb_loss(struct wdm_device *desc)
804{
805 int rv = 0;
806
807 if (desc->count) {
808 rv = usb_submit_urb(desc->validity, GFP_NOIO);
809 if (rv < 0)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700810 dev_err(&desc->intf->dev,
811 "Error resume submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200812 }
813 return rv;
814}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100815
816#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200817static int wdm_resume(struct usb_interface *intf)
818{
819 struct wdm_device *desc = usb_get_intfdata(intf);
820 int rv;
821
822 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
Oliver Neukum338124c2010-02-27 20:57:12 +0100823
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100824 clear_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100825 rv = recover_from_urb_loss(desc);
Oliver Neukum338124c2010-02-27 20:57:12 +0100826
Oliver Neukum17d80d52008-06-24 15:56:10 +0200827 return rv;
828}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100829#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200830
831static int wdm_pre_reset(struct usb_interface *intf)
832{
833 struct wdm_device *desc = usb_get_intfdata(intf);
834
Oliver Neukum860e41a2010-02-27 20:54:24 +0100835 mutex_lock(&desc->lock);
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200836 kill_urbs(desc);
837
838 /*
839 * we notify everybody using poll of
840 * an exceptional situation
841 * must be done before recovery lest a spontaneous
842 * message from the device is lost
843 */
844 spin_lock_irq(&desc->iuspin);
845 desc->rerr = -EINTR;
846 spin_unlock_irq(&desc->iuspin);
847 wake_up_all(&desc->wait);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200848 return 0;
849}
850
851static int wdm_post_reset(struct usb_interface *intf)
852{
853 struct wdm_device *desc = usb_get_intfdata(intf);
854 int rv;
855
856 rv = recover_from_urb_loss(desc);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100857 mutex_unlock(&desc->lock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200858 return 0;
859}
860
Oliver Neukumafba9372008-05-13 17:01:25 +0200861static struct usb_driver wdm_driver = {
862 .name = "cdc_wdm",
863 .probe = wdm_probe,
864 .disconnect = wdm_disconnect,
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100865#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200866 .suspend = wdm_suspend,
867 .resume = wdm_resume,
868 .reset_resume = wdm_resume,
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100869#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200870 .pre_reset = wdm_pre_reset,
871 .post_reset = wdm_post_reset,
Oliver Neukumafba9372008-05-13 17:01:25 +0200872 .id_table = wdm_ids,
Oliver Neukum17d80d52008-06-24 15:56:10 +0200873 .supports_autosuspend = 1,
Oliver Neukumafba9372008-05-13 17:01:25 +0200874};
875
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -0800876module_usb_driver(wdm_driver);
Oliver Neukumafba9372008-05-13 17:01:25 +0200877
878MODULE_AUTHOR(DRIVER_AUTHOR);
Oliver Neukum87d65e52008-06-19 14:20:18 +0200879MODULE_DESCRIPTION(DRIVER_DESC);
Oliver Neukumafba9372008-05-13 17:01:25 +0200880MODULE_LICENSE("GPL");