blob: f63601a2054c17cda748559f80ba9378f6ec07b0 [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
Bjørn Morkfec67b42012-01-25 13:03:29 +010034#define HUAWEI_VENDOR_ID 0x12D1
35
Németh Márton6ef48522010-01-10 15:33:45 +010036static const struct usb_device_id wdm_ids[] = {
Oliver Neukumafba9372008-05-13 17:01:25 +020037 {
38 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
39 USB_DEVICE_ID_MATCH_INT_SUBCLASS,
40 .bInterfaceClass = USB_CLASS_COMM,
41 .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM
42 },
Bjørn Morkfec67b42012-01-25 13:03:29 +010043 {
44 /*
45 * Huawei E392, E398 and possibly other Qualcomm based modems
46 * embed the Qualcomm QMI protocol inside CDC on CDC ECM like
47 * control interfaces. Userspace access to this is required
48 * to configure the accompanying data interface
49 */
50 .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
51 USB_DEVICE_ID_MATCH_INT_INFO,
52 .idVendor = HUAWEI_VENDOR_ID,
53 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
54 .bInterfaceSubClass = 1,
55 .bInterfaceProtocol = 9, /* NOTE: CDC ECM control interface! */
56 },
Oliver Neukumafba9372008-05-13 17:01:25 +020057 { }
58};
59
Oliver Neukumaa5380b2008-10-13 14:05:20 +020060MODULE_DEVICE_TABLE (usb, wdm_ids);
61
Oliver Neukumafba9372008-05-13 17:01:25 +020062#define WDM_MINOR_BASE 176
63
64
65#define WDM_IN_USE 1
66#define WDM_DISCONNECTING 2
67#define WDM_RESULT 3
68#define WDM_READ 4
69#define WDM_INT_STALL 5
70#define WDM_POLL_RUNNING 6
Oliver Neukum922a5ea2010-02-27 20:54:59 +010071#define WDM_RESPONDING 7
Oliver Neukumbeb1d352010-02-27 20:55:52 +010072#define WDM_SUSPENDING 8
Oliver Neukumafba9372008-05-13 17:01:25 +020073
74#define WDM_MAX 16
75
Bjørn Mork7e3054a2012-01-20 01:49:57 +010076/* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
77#define WDM_DEFAULT_BUFSIZE 256
Oliver Neukumafba9372008-05-13 17:01:25 +020078
79static DEFINE_MUTEX(wdm_mutex);
80
81/* --- method tables --- */
82
83struct wdm_device {
84 u8 *inbuf; /* buffer for response */
85 u8 *outbuf; /* buffer for command */
86 u8 *sbuf; /* buffer for status */
87 u8 *ubuf; /* buffer for copy to user space */
88
89 struct urb *command;
90 struct urb *response;
91 struct urb *validity;
92 struct usb_interface *intf;
93 struct usb_ctrlrequest *orq;
94 struct usb_ctrlrequest *irq;
95 spinlock_t iuspin;
96
97 unsigned long flags;
98 u16 bufsize;
99 u16 wMaxCommand;
100 u16 wMaxPacketSize;
Oliver Neukumafba9372008-05-13 17:01:25 +0200101 __le16 inum;
102 int reslength;
103 int length;
104 int read;
105 int count;
106 dma_addr_t shandle;
107 dma_addr_t ihandle;
Bjørn Morke8537bd2012-01-16 12:41:48 +0100108 struct mutex wlock;
109 struct mutex rlock;
Oliver Neukumafba9372008-05-13 17:01:25 +0200110 wait_queue_head_t wait;
111 struct work_struct rxwork;
112 int werr;
113 int rerr;
114};
115
116static struct usb_driver wdm_driver;
117
118/* --- callbacks --- */
119static void wdm_out_callback(struct urb *urb)
120{
121 struct wdm_device *desc;
122 desc = urb->context;
123 spin_lock(&desc->iuspin);
124 desc->werr = urb->status;
125 spin_unlock(&desc->iuspin);
126 clear_bit(WDM_IN_USE, &desc->flags);
127 kfree(desc->outbuf);
128 wake_up(&desc->wait);
129}
130
131static void wdm_in_callback(struct urb *urb)
132{
133 struct wdm_device *desc = urb->context;
134 int status = urb->status;
135
136 spin_lock(&desc->iuspin);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100137 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200138
139 if (status) {
140 switch (status) {
141 case -ENOENT:
142 dev_dbg(&desc->intf->dev,
143 "nonzero urb status received: -ENOENT");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100144 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200145 case -ECONNRESET:
146 dev_dbg(&desc->intf->dev,
147 "nonzero urb status received: -ECONNRESET");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100148 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200149 case -ESHUTDOWN:
150 dev_dbg(&desc->intf->dev,
151 "nonzero urb status received: -ESHUTDOWN");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100152 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200153 case -EPIPE:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700154 dev_err(&desc->intf->dev,
155 "nonzero urb status received: -EPIPE\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200156 break;
157 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700158 dev_err(&desc->intf->dev,
159 "Unexpected error %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200160 break;
161 }
162 }
163
164 desc->rerr = status;
165 desc->reslength = urb->actual_length;
166 memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength);
167 desc->length += desc->reslength;
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100168skip_error:
Oliver Neukumafba9372008-05-13 17:01:25 +0200169 wake_up(&desc->wait);
170
171 set_bit(WDM_READ, &desc->flags);
172 spin_unlock(&desc->iuspin);
173}
174
175static void wdm_int_callback(struct urb *urb)
176{
177 int rv = 0;
178 int status = urb->status;
179 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200180 struct usb_cdc_notification *dr;
181
182 desc = urb->context;
Oliver Neukumafba9372008-05-13 17:01:25 +0200183 dr = (struct usb_cdc_notification *)desc->sbuf;
184
185 if (status) {
186 switch (status) {
187 case -ESHUTDOWN:
188 case -ENOENT:
189 case -ECONNRESET:
190 return; /* unplug */
191 case -EPIPE:
192 set_bit(WDM_INT_STALL, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700193 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200194 goto sw; /* halt is cleared in work */
195 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700196 dev_err(&desc->intf->dev,
197 "nonzero urb status received: %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200198 break;
199 }
200 }
201
202 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700203 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
204 urb->actual_length);
Oliver Neukumafba9372008-05-13 17:01:25 +0200205 goto exit;
206 }
207
208 switch (dr->bNotificationType) {
209 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
210 dev_dbg(&desc->intf->dev,
211 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
212 dr->wIndex, dr->wLength);
213 break;
214
215 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
216
217 dev_dbg(&desc->intf->dev,
218 "NOTIFY_NETWORK_CONNECTION %s network",
219 dr->wValue ? "connected to" : "disconnected from");
220 goto exit;
221 default:
222 clear_bit(WDM_POLL_RUNNING, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700223 dev_err(&desc->intf->dev,
224 "unknown notification %d received: index %d len %d\n",
Oliver Neukumafba9372008-05-13 17:01:25 +0200225 dr->bNotificationType, dr->wIndex, dr->wLength);
226 goto exit;
227 }
228
Oliver Neukumafba9372008-05-13 17:01:25 +0200229 spin_lock(&desc->iuspin);
230 clear_bit(WDM_READ, &desc->flags);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100231 set_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100232 if (!test_bit(WDM_DISCONNECTING, &desc->flags)
233 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200234 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
235 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
236 __func__, rv);
237 }
238 spin_unlock(&desc->iuspin);
239 if (rv < 0) {
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100240 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200241 if (rv == -EPERM)
242 return;
243 if (rv == -ENOMEM) {
244sw:
245 rv = schedule_work(&desc->rxwork);
246 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700247 dev_err(&desc->intf->dev,
248 "Cannot schedule work\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200249 }
250 }
251exit:
252 rv = usb_submit_urb(urb, GFP_ATOMIC);
253 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700254 dev_err(&desc->intf->dev,
255 "%s - usb_submit_urb failed with result %d\n",
256 __func__, rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200257
258}
259
260static void kill_urbs(struct wdm_device *desc)
261{
Oliver Neukum17d80d52008-06-24 15:56:10 +0200262 /* the order here is essential */
Oliver Neukumafba9372008-05-13 17:01:25 +0200263 usb_kill_urb(desc->command);
264 usb_kill_urb(desc->validity);
265 usb_kill_urb(desc->response);
266}
267
268static void free_urbs(struct wdm_device *desc)
269{
270 usb_free_urb(desc->validity);
271 usb_free_urb(desc->response);
272 usb_free_urb(desc->command);
273}
274
275static void cleanup(struct wdm_device *desc)
276{
Bjørn Mork8457d992012-01-16 15:12:00 +0100277 kfree(desc->sbuf);
278 kfree(desc->inbuf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200279 kfree(desc->orq);
280 kfree(desc->irq);
281 kfree(desc->ubuf);
282 free_urbs(desc);
283 kfree(desc);
284}
285
286static ssize_t wdm_write
287(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
288{
289 u8 *buf;
290 int rv = -EMSGSIZE, r, we;
291 struct wdm_device *desc = file->private_data;
292 struct usb_ctrlrequest *req;
293
294 if (count > desc->wMaxCommand)
295 count = desc->wMaxCommand;
296
297 spin_lock_irq(&desc->iuspin);
298 we = desc->werr;
299 desc->werr = 0;
300 spin_unlock_irq(&desc->iuspin);
301 if (we < 0)
302 return -EIO;
303
Oliver Neukum860e41a2010-02-27 20:54:24 +0100304 desc->outbuf = buf = kmalloc(count, GFP_KERNEL);
305 if (!buf) {
306 rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200307 goto outnl;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100308 }
309
310 r = copy_from_user(buf, buffer, count);
311 if (r > 0) {
312 kfree(buf);
313 rv = -EFAULT;
314 goto outnl;
315 }
316
317 /* concurrent writes and disconnect */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100318 r = mutex_lock_interruptible(&desc->wlock);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100319 rv = -ERESTARTSYS;
320 if (r) {
321 kfree(buf);
322 goto outnl;
323 }
324
325 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
326 kfree(buf);
327 rv = -ENODEV;
328 goto outnp;
329 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200330
Oliver Neukum17d80d52008-06-24 15:56:10 +0200331 r = usb_autopm_get_interface(desc->intf);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100332 if (r < 0) {
333 kfree(buf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200334 goto outnp;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100335 }
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200336
David Sterba0cdfb812010-12-27 18:49:58 +0100337 if (!(file->f_flags & O_NONBLOCK))
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200338 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
339 &desc->flags));
340 else
341 if (test_bit(WDM_IN_USE, &desc->flags))
342 r = -EAGAIN;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100343 if (r < 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200344 kfree(buf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200345 goto out;
346 }
347
348 req = desc->orq;
349 usb_fill_control_urb(
350 desc->command,
351 interface_to_usbdev(desc->intf),
352 /* using common endpoint 0 */
353 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
354 (unsigned char *)req,
355 buf,
356 count,
357 wdm_out_callback,
358 desc
359 );
360
361 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
362 USB_RECIP_INTERFACE);
363 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
364 req->wValue = 0;
365 req->wIndex = desc->inum;
366 req->wLength = cpu_to_le16(count);
367 set_bit(WDM_IN_USE, &desc->flags);
368
369 rv = usb_submit_urb(desc->command, GFP_KERNEL);
370 if (rv < 0) {
371 kfree(buf);
372 clear_bit(WDM_IN_USE, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700373 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200374 } else {
375 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
376 req->wIndex);
377 }
378out:
Oliver Neukum17d80d52008-06-24 15:56:10 +0200379 usb_autopm_put_interface(desc->intf);
380outnp:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100381 mutex_unlock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200382outnl:
383 return rv < 0 ? rv : count;
384}
385
386static ssize_t wdm_read
387(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
388{
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200389 int rv, cntr = 0;
Oliver Neukumafba9372008-05-13 17:01:25 +0200390 int i = 0;
391 struct wdm_device *desc = file->private_data;
392
393
Bjørn Morke8537bd2012-01-16 12:41:48 +0100394 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
Oliver Neukumafba9372008-05-13 17:01:25 +0200395 if (rv < 0)
396 return -ERESTARTSYS;
397
398 if (desc->length == 0) {
399 desc->read = 0;
400retry:
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200401 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
402 rv = -ENODEV;
403 goto err;
404 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200405 i++;
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200406 if (file->f_flags & O_NONBLOCK) {
407 if (!test_bit(WDM_READ, &desc->flags)) {
408 rv = cntr ? cntr : -EAGAIN;
409 goto err;
410 }
411 rv = 0;
412 } else {
413 rv = wait_event_interruptible(desc->wait,
414 test_bit(WDM_READ, &desc->flags));
415 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200416
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200417 /* may have happened while we slept */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200418 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
419 rv = -ENODEV;
420 goto err;
421 }
422 usb_mark_last_busy(interface_to_usbdev(desc->intf));
Oliver Neukumafba9372008-05-13 17:01:25 +0200423 if (rv < 0) {
424 rv = -ERESTARTSYS;
425 goto err;
426 }
427
428 spin_lock_irq(&desc->iuspin);
429
430 if (desc->rerr) { /* read completed, error happened */
Oliver Neukumafba9372008-05-13 17:01:25 +0200431 desc->rerr = 0;
432 spin_unlock_irq(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200433 rv = -EIO;
434 goto err;
435 }
436 /*
437 * recheck whether we've lost the race
438 * against the completion handler
439 */
440 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
441 spin_unlock_irq(&desc->iuspin);
442 goto retry;
443 }
444 if (!desc->reslength) { /* zero length read */
445 spin_unlock_irq(&desc->iuspin);
446 goto retry;
447 }
448 clear_bit(WDM_READ, &desc->flags);
449 spin_unlock_irq(&desc->iuspin);
450 }
451
452 cntr = count > desc->length ? desc->length : count;
453 rv = copy_to_user(buffer, desc->ubuf, cntr);
454 if (rv > 0) {
455 rv = -EFAULT;
456 goto err;
457 }
458
459 for (i = 0; i < desc->length - cntr; i++)
460 desc->ubuf[i] = desc->ubuf[i + cntr];
461
Bjørn Morkc428b702012-01-16 12:41:47 +0100462 spin_lock_irq(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200463 desc->length -= cntr;
Bjørn Morkc428b702012-01-16 12:41:47 +0100464 spin_unlock_irq(&desc->iuspin);
Oliver Neukum87d65e52008-06-19 14:20:18 +0200465 /* in case we had outstanding data */
466 if (!desc->length)
467 clear_bit(WDM_READ, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200468 rv = cntr;
469
470err:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100471 mutex_unlock(&desc->rlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200472 return rv;
473}
474
475static int wdm_flush(struct file *file, fl_owner_t id)
476{
477 struct wdm_device *desc = file->private_data;
478
479 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
480 if (desc->werr < 0)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700481 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
482 desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200483
484 return desc->werr;
485}
486
487static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
488{
489 struct wdm_device *desc = file->private_data;
490 unsigned long flags;
491 unsigned int mask = 0;
492
493 spin_lock_irqsave(&desc->iuspin, flags);
494 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
495 mask = POLLERR;
496 spin_unlock_irqrestore(&desc->iuspin, flags);
497 goto desc_out;
498 }
499 if (test_bit(WDM_READ, &desc->flags))
500 mask = POLLIN | POLLRDNORM;
501 if (desc->rerr || desc->werr)
502 mask |= POLLERR;
503 if (!test_bit(WDM_IN_USE, &desc->flags))
504 mask |= POLLOUT | POLLWRNORM;
505 spin_unlock_irqrestore(&desc->iuspin, flags);
506
507 poll_wait(file, &desc->wait, wait);
508
509desc_out:
510 return mask;
511}
512
513static int wdm_open(struct inode *inode, struct file *file)
514{
515 int minor = iminor(inode);
516 int rv = -ENODEV;
517 struct usb_interface *intf;
518 struct wdm_device *desc;
519
520 mutex_lock(&wdm_mutex);
521 intf = usb_find_interface(&wdm_driver, minor);
522 if (!intf)
523 goto out;
524
525 desc = usb_get_intfdata(intf);
526 if (test_bit(WDM_DISCONNECTING, &desc->flags))
527 goto out;
Oliver Neukumafba9372008-05-13 17:01:25 +0200528 file->private_data = desc;
529
Oliver Neukum17d80d52008-06-24 15:56:10 +0200530 rv = usb_autopm_get_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200531 if (rv < 0) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700532 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200533 goto out;
534 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200535 intf->needs_remote_wakeup = 1;
Oliver Neukumafba9372008-05-13 17:01:25 +0200536
Bjørn Morke8537bd2012-01-16 12:41:48 +0100537 /* using write lock to protect desc->count */
538 mutex_lock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200539 if (!desc->count++) {
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200540 desc->werr = 0;
541 desc->rerr = 0;
Oliver Neukum17d80d52008-06-24 15:56:10 +0200542 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
543 if (rv < 0) {
544 desc->count--;
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700545 dev_err(&desc->intf->dev,
546 "Error submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200547 }
548 } else {
549 rv = 0;
550 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100551 mutex_unlock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200552 usb_autopm_put_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200553out:
554 mutex_unlock(&wdm_mutex);
555 return rv;
556}
557
558static int wdm_release(struct inode *inode, struct file *file)
559{
560 struct wdm_device *desc = file->private_data;
561
562 mutex_lock(&wdm_mutex);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100563
564 /* using write lock to protect desc->count */
565 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200566 desc->count--;
Bjørn Morke8537bd2012-01-16 12:41:48 +0100567 mutex_unlock(&desc->wlock);
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,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200586 .poll = wdm_poll,
587 .llseek = noop_llseek,
Oliver Neukumafba9372008-05-13 17:01:25 +0200588};
589
590static struct usb_class_driver wdm_class = {
591 .name = "cdc-wdm%d",
592 .fops = &wdm_fops,
593 .minor_base = WDM_MINOR_BASE,
594};
595
596/* --- error handling --- */
597static void wdm_rxwork(struct work_struct *work)
598{
599 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
600 unsigned long flags;
601 int rv;
602
603 spin_lock_irqsave(&desc->iuspin, flags);
604 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
605 spin_unlock_irqrestore(&desc->iuspin, flags);
606 } else {
607 spin_unlock_irqrestore(&desc->iuspin, flags);
608 rv = usb_submit_urb(desc->response, GFP_KERNEL);
609 if (rv < 0 && rv != -EPERM) {
610 spin_lock_irqsave(&desc->iuspin, flags);
611 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
612 schedule_work(&desc->rxwork);
613 spin_unlock_irqrestore(&desc->iuspin, flags);
614 }
615 }
616}
617
618/* --- hotplug --- */
619
620static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
621{
622 int rv = -EINVAL;
Oliver Neukumafba9372008-05-13 17:01:25 +0200623 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;
Bjørn Mork7e3054a2012-01-20 01:49:57 +0100629 u16 maxcom = WDM_DEFAULT_BUFSIZE;
Oliver Neukumafba9372008-05-13 17:01:25 +0200630
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;
Bjørn Morke8537bd2012-01-16 12:41:48 +0100664 mutex_init(&desc->rlock);
665 mutex_init(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200666 spin_lock_init(&desc->iuspin);
667 init_waitqueue_head(&desc->wait);
668 desc->wMaxCommand = maxcom;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200669 /* this will be expanded and needed in hardware endianness */
Oliver Neukumafba9372008-05-13 17:01:25 +0200670 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
671 desc->intf = intf;
672 INIT_WORK(&desc->rxwork, wdm_rxwork);
673
Oliver Neukum052fbc02009-04-20 17:24:49 +0200674 rv = -EINVAL;
675 iface = intf->cur_altsetting;
676 if (iface->desc.bNumEndpoints != 1)
Oliver Neukumafba9372008-05-13 17:01:25 +0200677 goto err;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200678 ep = &iface->endpoint[0].desc;
679 if (!ep || !usb_endpoint_is_int_in(ep))
680 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200681
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700682 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
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
Bjørn Mork8457d992012-01-16 15:12:00 +0100707 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200708 if (!desc->sbuf)
709 goto err;
710
Bjørn Mork8457d992012-01-16 15:12:00 +0100711 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200712 if (!desc->inbuf)
Bjørn Mork8457d992012-01-16 15:12:00 +0100713 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200714
715 usb_fill_int_urb(
716 desc->validity,
717 interface_to_usbdev(intf),
718 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
719 desc->sbuf,
720 desc->wMaxPacketSize,
721 wdm_int_callback,
722 desc,
723 ep->bInterval
724 );
Oliver Neukumafba9372008-05-13 17:01:25 +0200725
Bjørn Mork19b85b32012-01-16 15:11:58 +0100726 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
727 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
728 desc->irq->wValue = 0;
729 desc->irq->wIndex = desc->inum;
730 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
731
732 usb_fill_control_urb(
733 desc->response,
Bjørn Mork8143a892012-01-16 15:12:01 +0100734 interface_to_usbdev(intf),
Bjørn Mork19b85b32012-01-16 15:11:58 +0100735 /* using common endpoint 0 */
736 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
737 (unsigned char *)desc->irq,
738 desc->inbuf,
739 desc->wMaxCommand,
740 wdm_in_callback,
741 desc
742 );
Bjørn Mork19b85b32012-01-16 15:11:58 +0100743
Oliver Neukumafba9372008-05-13 17:01:25 +0200744 usb_set_intfdata(intf, desc);
745 rv = usb_register_dev(intf, &wdm_class);
Oliver Neukumafba9372008-05-13 17:01:25 +0200746 if (rv < 0)
Bjørn Mork8457d992012-01-16 15:12:00 +0100747 goto err2;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200748 else
Bjørn Mork820c6292012-01-20 04:17:25 +0100749 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
Oliver Neukumafba9372008-05-13 17:01:25 +0200750out:
751 return rv;
752err2:
Bjørn Mork8457d992012-01-16 15:12:00 +0100753 usb_set_intfdata(intf, NULL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200754err:
755 free_urbs(desc);
Bjørn Mork8457d992012-01-16 15:12:00 +0100756 kfree(desc->inbuf);
757 kfree(desc->sbuf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200758 kfree(desc->ubuf);
759 kfree(desc->orq);
760 kfree(desc->irq);
761 kfree(desc);
762 return rv;
763}
764
765static void wdm_disconnect(struct usb_interface *intf)
766{
767 struct wdm_device *desc;
768 unsigned long flags;
769
770 usb_deregister_dev(intf, &wdm_class);
771 mutex_lock(&wdm_mutex);
772 desc = usb_get_intfdata(intf);
773
774 /* the spinlock makes sure no new urbs are generated in the callbacks */
775 spin_lock_irqsave(&desc->iuspin, flags);
776 set_bit(WDM_DISCONNECTING, &desc->flags);
777 set_bit(WDM_READ, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200778 /* to terminate pending flushes */
Oliver Neukumafba9372008-05-13 17:01:25 +0200779 clear_bit(WDM_IN_USE, &desc->flags);
780 spin_unlock_irqrestore(&desc->iuspin, flags);
Bjørn Mork62aaf242012-01-16 15:11:57 +0100781 wake_up_all(&desc->wait);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100782 mutex_lock(&desc->rlock);
783 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200784 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100785 cancel_work_sync(&desc->rxwork);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100786 mutex_unlock(&desc->wlock);
787 mutex_unlock(&desc->rlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200788 if (!desc->count)
789 cleanup(desc);
790 mutex_unlock(&wdm_mutex);
791}
792
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100793#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200794static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
795{
796 struct wdm_device *desc = usb_get_intfdata(intf);
797 int rv = 0;
798
799 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
800
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100801 /* if this is an autosuspend the caller does the locking */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100802 if (!PMSG_IS_AUTO(message)) {
803 mutex_lock(&desc->rlock);
804 mutex_lock(&desc->wlock);
805 }
Oliver Neukum62e66852010-02-27 20:56:22 +0100806 spin_lock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100807
Alan Stern5b1b0b82011-08-19 23:49:48 +0200808 if (PMSG_IS_AUTO(message) &&
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100809 (test_bit(WDM_IN_USE, &desc->flags)
810 || test_bit(WDM_RESPONDING, &desc->flags))) {
Oliver Neukum62e66852010-02-27 20:56:22 +0100811 spin_unlock_irq(&desc->iuspin);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200812 rv = -EBUSY;
813 } else {
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100814
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100815 set_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100816 spin_unlock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100817 /* callback submits work - order is essential */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200818 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100819 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200820 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100821 if (!PMSG_IS_AUTO(message)) {
822 mutex_unlock(&desc->wlock);
823 mutex_unlock(&desc->rlock);
824 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200825
826 return rv;
827}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100828#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200829
830static int recover_from_urb_loss(struct wdm_device *desc)
831{
832 int rv = 0;
833
834 if (desc->count) {
835 rv = usb_submit_urb(desc->validity, GFP_NOIO);
836 if (rv < 0)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700837 dev_err(&desc->intf->dev,
838 "Error resume submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200839 }
840 return rv;
841}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100842
843#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200844static int wdm_resume(struct usb_interface *intf)
845{
846 struct wdm_device *desc = usb_get_intfdata(intf);
847 int rv;
848
849 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
Oliver Neukum338124c2010-02-27 20:57:12 +0100850
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100851 clear_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100852 rv = recover_from_urb_loss(desc);
Oliver Neukum338124c2010-02-27 20:57:12 +0100853
Oliver Neukum17d80d52008-06-24 15:56:10 +0200854 return rv;
855}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100856#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200857
858static int wdm_pre_reset(struct usb_interface *intf)
859{
860 struct wdm_device *desc = usb_get_intfdata(intf);
861
Bjørn Morke8537bd2012-01-16 12:41:48 +0100862 mutex_lock(&desc->rlock);
863 mutex_lock(&desc->wlock);
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200864 kill_urbs(desc);
865
866 /*
867 * we notify everybody using poll of
868 * an exceptional situation
869 * must be done before recovery lest a spontaneous
870 * message from the device is lost
871 */
872 spin_lock_irq(&desc->iuspin);
873 desc->rerr = -EINTR;
874 spin_unlock_irq(&desc->iuspin);
875 wake_up_all(&desc->wait);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200876 return 0;
877}
878
879static int wdm_post_reset(struct usb_interface *intf)
880{
881 struct wdm_device *desc = usb_get_intfdata(intf);
882 int rv;
883
884 rv = recover_from_urb_loss(desc);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100885 mutex_unlock(&desc->wlock);
886 mutex_unlock(&desc->rlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200887 return 0;
888}
889
Oliver Neukumafba9372008-05-13 17:01:25 +0200890static struct usb_driver wdm_driver = {
891 .name = "cdc_wdm",
892 .probe = wdm_probe,
893 .disconnect = wdm_disconnect,
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100894#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200895 .suspend = wdm_suspend,
896 .resume = wdm_resume,
897 .reset_resume = wdm_resume,
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100898#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200899 .pre_reset = wdm_pre_reset,
900 .post_reset = wdm_post_reset,
Oliver Neukumafba9372008-05-13 17:01:25 +0200901 .id_table = wdm_ids,
Oliver Neukum17d80d52008-06-24 15:56:10 +0200902 .supports_autosuspend = 1,
Oliver Neukumafba9372008-05-13 17:01:25 +0200903};
904
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -0800905module_usb_driver(wdm_driver);
Oliver Neukumafba9372008-05-13 17:01:25 +0200906
907MODULE_AUTHOR(DRIVER_AUTHOR);
Oliver Neukum87d65e52008-06-19 14:20:18 +0200908MODULE_DESCRIPTION(DRIVER_DESC);
Oliver Neukumafba9372008-05-13 17:01:25 +0200909MODULE_LICENSE("GPL");