blob: bb8208a13a534324bb51c884b788906b120702da [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;
Oliver Neukumafba9372008-05-13 17:01:25 +020083 __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);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100118 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200119
120 if (status) {
121 switch (status) {
122 case -ENOENT:
123 dev_dbg(&desc->intf->dev,
124 "nonzero urb status received: -ENOENT");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100125 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200126 case -ECONNRESET:
127 dev_dbg(&desc->intf->dev,
128 "nonzero urb status received: -ECONNRESET");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100129 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200130 case -ESHUTDOWN:
131 dev_dbg(&desc->intf->dev,
132 "nonzero urb status received: -ESHUTDOWN");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100133 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200134 case -EPIPE:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700135 dev_err(&desc->intf->dev,
136 "nonzero urb status received: -EPIPE\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200137 break;
138 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700139 dev_err(&desc->intf->dev,
140 "Unexpected error %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200141 break;
142 }
143 }
144
145 desc->rerr = status;
146 desc->reslength = urb->actual_length;
147 memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength);
148 desc->length += desc->reslength;
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100149skip_error:
Oliver Neukumafba9372008-05-13 17:01:25 +0200150 wake_up(&desc->wait);
151
152 set_bit(WDM_READ, &desc->flags);
153 spin_unlock(&desc->iuspin);
154}
155
156static void wdm_int_callback(struct urb *urb)
157{
158 int rv = 0;
159 int status = urb->status;
160 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200161 struct usb_cdc_notification *dr;
162
163 desc = urb->context;
Oliver Neukumafba9372008-05-13 17:01:25 +0200164 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
Oliver Neukumafba9372008-05-13 17:01:25 +0200210 spin_lock(&desc->iuspin);
211 clear_bit(WDM_READ, &desc->flags);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100212 set_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100213 if (!test_bit(WDM_DISCONNECTING, &desc->flags)
214 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200215 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
216 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
217 __func__, rv);
218 }
219 spin_unlock(&desc->iuspin);
220 if (rv < 0) {
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100221 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200222 if (rv == -EPERM)
223 return;
224 if (rv == -ENOMEM) {
225sw:
226 rv = schedule_work(&desc->rxwork);
227 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700228 dev_err(&desc->intf->dev,
229 "Cannot schedule work\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200230 }
231 }
232exit:
233 rv = usb_submit_urb(urb, GFP_ATOMIC);
234 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700235 dev_err(&desc->intf->dev,
236 "%s - usb_submit_urb failed with result %d\n",
237 __func__, rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200238
239}
240
241static void kill_urbs(struct wdm_device *desc)
242{
Oliver Neukum17d80d52008-06-24 15:56:10 +0200243 /* the order here is essential */
Oliver Neukumafba9372008-05-13 17:01:25 +0200244 usb_kill_urb(desc->command);
245 usb_kill_urb(desc->validity);
246 usb_kill_urb(desc->response);
247}
248
249static void free_urbs(struct wdm_device *desc)
250{
251 usb_free_urb(desc->validity);
252 usb_free_urb(desc->response);
253 usb_free_urb(desc->command);
254}
255
256static void cleanup(struct wdm_device *desc)
257{
Bjørn Mork8457d992012-01-16 15:12:00 +0100258 kfree(desc->sbuf);
259 kfree(desc->inbuf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200260 kfree(desc->orq);
261 kfree(desc->irq);
262 kfree(desc->ubuf);
263 free_urbs(desc);
264 kfree(desc);
265}
266
267static ssize_t wdm_write
268(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
269{
270 u8 *buf;
271 int rv = -EMSGSIZE, r, we;
272 struct wdm_device *desc = file->private_data;
273 struct usb_ctrlrequest *req;
274
275 if (count > desc->wMaxCommand)
276 count = desc->wMaxCommand;
277
278 spin_lock_irq(&desc->iuspin);
279 we = desc->werr;
280 desc->werr = 0;
281 spin_unlock_irq(&desc->iuspin);
282 if (we < 0)
283 return -EIO;
284
Oliver Neukum860e41a2010-02-27 20:54:24 +0100285 desc->outbuf = buf = kmalloc(count, GFP_KERNEL);
286 if (!buf) {
287 rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200288 goto outnl;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100289 }
290
291 r = copy_from_user(buf, buffer, count);
292 if (r > 0) {
293 kfree(buf);
294 rv = -EFAULT;
295 goto outnl;
296 }
297
298 /* concurrent writes and disconnect */
299 r = mutex_lock_interruptible(&desc->lock);
300 rv = -ERESTARTSYS;
301 if (r) {
302 kfree(buf);
303 goto outnl;
304 }
305
306 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
307 kfree(buf);
308 rv = -ENODEV;
309 goto outnp;
310 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200311
Oliver Neukum17d80d52008-06-24 15:56:10 +0200312 r = usb_autopm_get_interface(desc->intf);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100313 if (r < 0) {
314 kfree(buf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200315 goto outnp;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100316 }
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200317
David Sterba0cdfb812010-12-27 18:49:58 +0100318 if (!(file->f_flags & O_NONBLOCK))
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200319 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
320 &desc->flags));
321 else
322 if (test_bit(WDM_IN_USE, &desc->flags))
323 r = -EAGAIN;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100324 if (r < 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200325 kfree(buf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200326 goto out;
327 }
328
329 req = desc->orq;
330 usb_fill_control_urb(
331 desc->command,
332 interface_to_usbdev(desc->intf),
333 /* using common endpoint 0 */
334 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
335 (unsigned char *)req,
336 buf,
337 count,
338 wdm_out_callback,
339 desc
340 );
341
342 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
343 USB_RECIP_INTERFACE);
344 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
345 req->wValue = 0;
346 req->wIndex = desc->inum;
347 req->wLength = cpu_to_le16(count);
348 set_bit(WDM_IN_USE, &desc->flags);
349
350 rv = usb_submit_urb(desc->command, GFP_KERNEL);
351 if (rv < 0) {
352 kfree(buf);
353 clear_bit(WDM_IN_USE, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700354 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200355 } else {
356 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
357 req->wIndex);
358 }
359out:
Oliver Neukum17d80d52008-06-24 15:56:10 +0200360 usb_autopm_put_interface(desc->intf);
361outnp:
Oliver Neukum860e41a2010-02-27 20:54:24 +0100362 mutex_unlock(&desc->lock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200363outnl:
364 return rv < 0 ? rv : count;
365}
366
367static ssize_t wdm_read
368(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
369{
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200370 int rv, cntr = 0;
Oliver Neukumafba9372008-05-13 17:01:25 +0200371 int i = 0;
372 struct wdm_device *desc = file->private_data;
373
374
Oliver Neukum860e41a2010-02-27 20:54:24 +0100375 rv = mutex_lock_interruptible(&desc->lock); /*concurrent reads */
Oliver Neukumafba9372008-05-13 17:01:25 +0200376 if (rv < 0)
377 return -ERESTARTSYS;
378
379 if (desc->length == 0) {
380 desc->read = 0;
381retry:
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200382 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
383 rv = -ENODEV;
384 goto err;
385 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200386 i++;
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200387 if (file->f_flags & O_NONBLOCK) {
388 if (!test_bit(WDM_READ, &desc->flags)) {
389 rv = cntr ? cntr : -EAGAIN;
390 goto err;
391 }
392 rv = 0;
393 } else {
394 rv = wait_event_interruptible(desc->wait,
395 test_bit(WDM_READ, &desc->flags));
396 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200397
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200398 /* may have happened while we slept */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200399 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
400 rv = -ENODEV;
401 goto err;
402 }
403 usb_mark_last_busy(interface_to_usbdev(desc->intf));
Oliver Neukumafba9372008-05-13 17:01:25 +0200404 if (rv < 0) {
405 rv = -ERESTARTSYS;
406 goto err;
407 }
408
409 spin_lock_irq(&desc->iuspin);
410
411 if (desc->rerr) { /* read completed, error happened */
Oliver Neukumafba9372008-05-13 17:01:25 +0200412 desc->rerr = 0;
413 spin_unlock_irq(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200414 rv = -EIO;
415 goto err;
416 }
417 /*
418 * recheck whether we've lost the race
419 * against the completion handler
420 */
421 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
422 spin_unlock_irq(&desc->iuspin);
423 goto retry;
424 }
425 if (!desc->reslength) { /* zero length read */
426 spin_unlock_irq(&desc->iuspin);
427 goto retry;
428 }
429 clear_bit(WDM_READ, &desc->flags);
430 spin_unlock_irq(&desc->iuspin);
431 }
432
433 cntr = count > desc->length ? desc->length : count;
434 rv = copy_to_user(buffer, desc->ubuf, cntr);
435 if (rv > 0) {
436 rv = -EFAULT;
437 goto err;
438 }
439
440 for (i = 0; i < desc->length - cntr; i++)
441 desc->ubuf[i] = desc->ubuf[i + cntr];
442
443 desc->length -= cntr;
Oliver Neukum87d65e52008-06-19 14:20:18 +0200444 /* in case we had outstanding data */
445 if (!desc->length)
446 clear_bit(WDM_READ, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200447 rv = cntr;
448
449err:
Oliver Neukum860e41a2010-02-27 20:54:24 +0100450 mutex_unlock(&desc->lock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200451 return rv;
452}
453
454static int wdm_flush(struct file *file, fl_owner_t id)
455{
456 struct wdm_device *desc = file->private_data;
457
458 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
459 if (desc->werr < 0)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700460 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
461 desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200462
463 return desc->werr;
464}
465
466static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
467{
468 struct wdm_device *desc = file->private_data;
469 unsigned long flags;
470 unsigned int mask = 0;
471
472 spin_lock_irqsave(&desc->iuspin, flags);
473 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
474 mask = POLLERR;
475 spin_unlock_irqrestore(&desc->iuspin, flags);
476 goto desc_out;
477 }
478 if (test_bit(WDM_READ, &desc->flags))
479 mask = POLLIN | POLLRDNORM;
480 if (desc->rerr || desc->werr)
481 mask |= POLLERR;
482 if (!test_bit(WDM_IN_USE, &desc->flags))
483 mask |= POLLOUT | POLLWRNORM;
484 spin_unlock_irqrestore(&desc->iuspin, flags);
485
486 poll_wait(file, &desc->wait, wait);
487
488desc_out:
489 return mask;
490}
491
492static int wdm_open(struct inode *inode, struct file *file)
493{
494 int minor = iminor(inode);
495 int rv = -ENODEV;
496 struct usb_interface *intf;
497 struct wdm_device *desc;
498
499 mutex_lock(&wdm_mutex);
500 intf = usb_find_interface(&wdm_driver, minor);
501 if (!intf)
502 goto out;
503
504 desc = usb_get_intfdata(intf);
505 if (test_bit(WDM_DISCONNECTING, &desc->flags))
506 goto out;
Oliver Neukumafba9372008-05-13 17:01:25 +0200507 file->private_data = desc;
508
Oliver Neukum17d80d52008-06-24 15:56:10 +0200509 rv = usb_autopm_get_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200510 if (rv < 0) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700511 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200512 goto out;
513 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200514 intf->needs_remote_wakeup = 1;
Oliver Neukumafba9372008-05-13 17:01:25 +0200515
Oliver Neukum860e41a2010-02-27 20:54:24 +0100516 mutex_lock(&desc->lock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200517 if (!desc->count++) {
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200518 desc->werr = 0;
519 desc->rerr = 0;
Oliver Neukum17d80d52008-06-24 15:56:10 +0200520 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
521 if (rv < 0) {
522 desc->count--;
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700523 dev_err(&desc->intf->dev,
524 "Error submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200525 }
526 } else {
527 rv = 0;
528 }
Oliver Neukum860e41a2010-02-27 20:54:24 +0100529 mutex_unlock(&desc->lock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200530 usb_autopm_put_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200531out:
532 mutex_unlock(&wdm_mutex);
533 return rv;
534}
535
536static int wdm_release(struct inode *inode, struct file *file)
537{
538 struct wdm_device *desc = file->private_data;
539
540 mutex_lock(&wdm_mutex);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100541 mutex_lock(&desc->lock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200542 desc->count--;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100543 mutex_unlock(&desc->lock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200544
Oliver Neukumafba9372008-05-13 17:01:25 +0200545 if (!desc->count) {
546 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
547 kill_urbs(desc);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200548 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
549 desc->intf->needs_remote_wakeup = 0;
Oliver Neukumafba9372008-05-13 17:01:25 +0200550 }
551 mutex_unlock(&wdm_mutex);
552 return 0;
553}
554
555static const struct file_operations wdm_fops = {
556 .owner = THIS_MODULE,
557 .read = wdm_read,
558 .write = wdm_write,
559 .open = wdm_open,
560 .flush = wdm_flush,
561 .release = wdm_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200562 .poll = wdm_poll,
563 .llseek = noop_llseek,
Oliver Neukumafba9372008-05-13 17:01:25 +0200564};
565
566static struct usb_class_driver wdm_class = {
567 .name = "cdc-wdm%d",
568 .fops = &wdm_fops,
569 .minor_base = WDM_MINOR_BASE,
570};
571
572/* --- error handling --- */
573static void wdm_rxwork(struct work_struct *work)
574{
575 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
576 unsigned long flags;
577 int rv;
578
579 spin_lock_irqsave(&desc->iuspin, flags);
580 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
581 spin_unlock_irqrestore(&desc->iuspin, flags);
582 } else {
583 spin_unlock_irqrestore(&desc->iuspin, flags);
584 rv = usb_submit_urb(desc->response, GFP_KERNEL);
585 if (rv < 0 && rv != -EPERM) {
586 spin_lock_irqsave(&desc->iuspin, flags);
587 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
588 schedule_work(&desc->rxwork);
589 spin_unlock_irqrestore(&desc->iuspin, flags);
590 }
591 }
592}
593
594/* --- hotplug --- */
595
596static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
597{
598 int rv = -EINVAL;
Oliver Neukumafba9372008-05-13 17:01:25 +0200599 struct wdm_device *desc;
600 struct usb_host_interface *iface;
601 struct usb_endpoint_descriptor *ep;
602 struct usb_cdc_dmm_desc *dmhd;
603 u8 *buffer = intf->altsetting->extra;
604 int buflen = intf->altsetting->extralen;
605 u16 maxcom = 0;
606
607 if (!buffer)
608 goto out;
609
Oliver Neukum052fbc02009-04-20 17:24:49 +0200610 while (buflen > 2) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200611 if (buffer [1] != USB_DT_CS_INTERFACE) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700612 dev_err(&intf->dev, "skipping garbage\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200613 goto next_desc;
614 }
615
616 switch (buffer [2]) {
617 case USB_CDC_HEADER_TYPE:
618 break;
619 case USB_CDC_DMM_TYPE:
620 dmhd = (struct usb_cdc_dmm_desc *)buffer;
621 maxcom = le16_to_cpu(dmhd->wMaxCommand);
622 dev_dbg(&intf->dev,
623 "Finding maximum buffer length: %d", maxcom);
624 break;
625 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700626 dev_err(&intf->dev,
627 "Ignoring extra header, type %d, length %d\n",
Oliver Neukumafba9372008-05-13 17:01:25 +0200628 buffer[2], buffer[0]);
629 break;
630 }
631next_desc:
632 buflen -= buffer[0];
633 buffer += buffer[0];
634 }
635
636 rv = -ENOMEM;
637 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
638 if (!desc)
639 goto out;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100640 mutex_init(&desc->lock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200641 spin_lock_init(&desc->iuspin);
642 init_waitqueue_head(&desc->wait);
643 desc->wMaxCommand = maxcom;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200644 /* this will be expanded and needed in hardware endianness */
Oliver Neukumafba9372008-05-13 17:01:25 +0200645 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
646 desc->intf = intf;
647 INIT_WORK(&desc->rxwork, wdm_rxwork);
648
Oliver Neukum052fbc02009-04-20 17:24:49 +0200649 rv = -EINVAL;
650 iface = intf->cur_altsetting;
651 if (iface->desc.bNumEndpoints != 1)
Oliver Neukumafba9372008-05-13 17:01:25 +0200652 goto err;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200653 ep = &iface->endpoint[0].desc;
654 if (!ep || !usb_endpoint_is_int_in(ep))
655 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200656
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700657 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
Oliver Neukumafba9372008-05-13 17:01:25 +0200658
659 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
660 if (!desc->orq)
661 goto err;
662 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
663 if (!desc->irq)
664 goto err;
665
666 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
667 if (!desc->validity)
668 goto err;
669
670 desc->response = usb_alloc_urb(0, GFP_KERNEL);
671 if (!desc->response)
672 goto err;
673
674 desc->command = usb_alloc_urb(0, GFP_KERNEL);
675 if (!desc->command)
676 goto err;
677
678 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
679 if (!desc->ubuf)
680 goto err;
681
Bjørn Mork8457d992012-01-16 15:12:00 +0100682 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200683 if (!desc->sbuf)
684 goto err;
685
Bjørn Mork8457d992012-01-16 15:12:00 +0100686 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200687 if (!desc->inbuf)
Bjørn Mork8457d992012-01-16 15:12:00 +0100688 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200689
690 usb_fill_int_urb(
691 desc->validity,
692 interface_to_usbdev(intf),
693 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
694 desc->sbuf,
695 desc->wMaxPacketSize,
696 wdm_int_callback,
697 desc,
698 ep->bInterval
699 );
Oliver Neukumafba9372008-05-13 17:01:25 +0200700
Bjørn Mork19b85b32012-01-16 15:11:58 +0100701 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
702 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
703 desc->irq->wValue = 0;
704 desc->irq->wIndex = desc->inum;
705 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
706
707 usb_fill_control_urb(
708 desc->response,
Bjørn Mork8143a892012-01-16 15:12:01 +0100709 interface_to_usbdev(intf),
Bjørn Mork19b85b32012-01-16 15:11:58 +0100710 /* using common endpoint 0 */
711 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
712 (unsigned char *)desc->irq,
713 desc->inbuf,
714 desc->wMaxCommand,
715 wdm_in_callback,
716 desc
717 );
Bjørn Mork19b85b32012-01-16 15:11:58 +0100718
Oliver Neukumafba9372008-05-13 17:01:25 +0200719 usb_set_intfdata(intf, desc);
720 rv = usb_register_dev(intf, &wdm_class);
Oliver Neukumafba9372008-05-13 17:01:25 +0200721 if (rv < 0)
Bjørn Mork8457d992012-01-16 15:12:00 +0100722 goto err2;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200723 else
724 dev_info(&intf->dev, "cdc-wdm%d: USB WDM device\n",
725 intf->minor - WDM_MINOR_BASE);
Oliver Neukumafba9372008-05-13 17:01:25 +0200726out:
727 return rv;
728err2:
Bjørn Mork8457d992012-01-16 15:12:00 +0100729 usb_set_intfdata(intf, NULL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200730err:
731 free_urbs(desc);
Bjørn Mork8457d992012-01-16 15:12:00 +0100732 kfree(desc->inbuf);
733 kfree(desc->sbuf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200734 kfree(desc->ubuf);
735 kfree(desc->orq);
736 kfree(desc->irq);
737 kfree(desc);
738 return rv;
739}
740
741static void wdm_disconnect(struct usb_interface *intf)
742{
743 struct wdm_device *desc;
744 unsigned long flags;
745
746 usb_deregister_dev(intf, &wdm_class);
747 mutex_lock(&wdm_mutex);
748 desc = usb_get_intfdata(intf);
749
750 /* the spinlock makes sure no new urbs are generated in the callbacks */
751 spin_lock_irqsave(&desc->iuspin, flags);
752 set_bit(WDM_DISCONNECTING, &desc->flags);
753 set_bit(WDM_READ, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200754 /* to terminate pending flushes */
Oliver Neukumafba9372008-05-13 17:01:25 +0200755 clear_bit(WDM_IN_USE, &desc->flags);
756 spin_unlock_irqrestore(&desc->iuspin, flags);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100757 mutex_lock(&desc->lock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200758 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100759 cancel_work_sync(&desc->rxwork);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100760 mutex_unlock(&desc->lock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200761 wake_up_all(&desc->wait);
762 if (!desc->count)
763 cleanup(desc);
764 mutex_unlock(&wdm_mutex);
765}
766
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100767#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200768static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
769{
770 struct wdm_device *desc = usb_get_intfdata(intf);
771 int rv = 0;
772
773 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
774
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100775 /* if this is an autosuspend the caller does the locking */
Alan Stern5b1b0b82011-08-19 23:49:48 +0200776 if (!PMSG_IS_AUTO(message))
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100777 mutex_lock(&desc->lock);
Oliver Neukum62e66852010-02-27 20:56:22 +0100778 spin_lock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100779
Alan Stern5b1b0b82011-08-19 23:49:48 +0200780 if (PMSG_IS_AUTO(message) &&
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100781 (test_bit(WDM_IN_USE, &desc->flags)
782 || test_bit(WDM_RESPONDING, &desc->flags))) {
Oliver Neukum62e66852010-02-27 20:56:22 +0100783 spin_unlock_irq(&desc->iuspin);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200784 rv = -EBUSY;
785 } else {
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100786
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100787 set_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100788 spin_unlock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100789 /* callback submits work - order is essential */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200790 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100791 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200792 }
Alan Stern5b1b0b82011-08-19 23:49:48 +0200793 if (!PMSG_IS_AUTO(message))
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100794 mutex_unlock(&desc->lock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200795
796 return rv;
797}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100798#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200799
800static int recover_from_urb_loss(struct wdm_device *desc)
801{
802 int rv = 0;
803
804 if (desc->count) {
805 rv = usb_submit_urb(desc->validity, GFP_NOIO);
806 if (rv < 0)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700807 dev_err(&desc->intf->dev,
808 "Error resume submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200809 }
810 return rv;
811}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100812
813#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200814static int wdm_resume(struct usb_interface *intf)
815{
816 struct wdm_device *desc = usb_get_intfdata(intf);
817 int rv;
818
819 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
Oliver Neukum338124c2010-02-27 20:57:12 +0100820
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100821 clear_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100822 rv = recover_from_urb_loss(desc);
Oliver Neukum338124c2010-02-27 20:57:12 +0100823
Oliver Neukum17d80d52008-06-24 15:56:10 +0200824 return rv;
825}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100826#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200827
828static int wdm_pre_reset(struct usb_interface *intf)
829{
830 struct wdm_device *desc = usb_get_intfdata(intf);
831
Oliver Neukum860e41a2010-02-27 20:54:24 +0100832 mutex_lock(&desc->lock);
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200833 kill_urbs(desc);
834
835 /*
836 * we notify everybody using poll of
837 * an exceptional situation
838 * must be done before recovery lest a spontaneous
839 * message from the device is lost
840 */
841 spin_lock_irq(&desc->iuspin);
842 desc->rerr = -EINTR;
843 spin_unlock_irq(&desc->iuspin);
844 wake_up_all(&desc->wait);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200845 return 0;
846}
847
848static int wdm_post_reset(struct usb_interface *intf)
849{
850 struct wdm_device *desc = usb_get_intfdata(intf);
851 int rv;
852
853 rv = recover_from_urb_loss(desc);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100854 mutex_unlock(&desc->lock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200855 return 0;
856}
857
Oliver Neukumafba9372008-05-13 17:01:25 +0200858static struct usb_driver wdm_driver = {
859 .name = "cdc_wdm",
860 .probe = wdm_probe,
861 .disconnect = wdm_disconnect,
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100862#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200863 .suspend = wdm_suspend,
864 .resume = wdm_resume,
865 .reset_resume = wdm_resume,
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100866#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200867 .pre_reset = wdm_pre_reset,
868 .post_reset = wdm_post_reset,
Oliver Neukumafba9372008-05-13 17:01:25 +0200869 .id_table = wdm_ids,
Oliver Neukum17d80d52008-06-24 15:56:10 +0200870 .supports_autosuspend = 1,
Oliver Neukumafba9372008-05-13 17:01:25 +0200871};
872
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -0800873module_usb_driver(wdm_driver);
Oliver Neukumafba9372008-05-13 17:01:25 +0200874
875MODULE_AUTHOR(DRIVER_AUTHOR);
Oliver Neukum87d65e52008-06-19 14:20:18 +0200876MODULE_DESCRIPTION(DRIVER_DESC);
Oliver Neukumafba9372008-05-13 17:01:25 +0200877MODULE_LICENSE("GPL");