blob: 451793036d377aaadb4bd424e8a435c05087f5bd [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
Bjørn Mork88044202012-02-10 09:44:08 +010073#define WDM_RESETTING 9
Oliver Neukumafba9372008-05-13 17:01:25 +020074
75#define WDM_MAX 16
76
Bjørn Mork7e3054a2012-01-20 01:49:57 +010077/* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
78#define WDM_DEFAULT_BUFSIZE 256
Oliver Neukumafba9372008-05-13 17:01:25 +020079
80static DEFINE_MUTEX(wdm_mutex);
81
82/* --- method tables --- */
83
84struct wdm_device {
85 u8 *inbuf; /* buffer for response */
86 u8 *outbuf; /* buffer for command */
87 u8 *sbuf; /* buffer for status */
88 u8 *ubuf; /* buffer for copy to user space */
89
90 struct urb *command;
91 struct urb *response;
92 struct urb *validity;
93 struct usb_interface *intf;
94 struct usb_ctrlrequest *orq;
95 struct usb_ctrlrequest *irq;
96 spinlock_t iuspin;
97
98 unsigned long flags;
99 u16 bufsize;
100 u16 wMaxCommand;
101 u16 wMaxPacketSize;
Oliver Neukumafba9372008-05-13 17:01:25 +0200102 __le16 inum;
103 int reslength;
104 int length;
105 int read;
106 int count;
107 dma_addr_t shandle;
108 dma_addr_t ihandle;
Bjørn Morke8537bd2012-01-16 12:41:48 +0100109 struct mutex wlock;
110 struct mutex rlock;
Oliver Neukumafba9372008-05-13 17:01:25 +0200111 wait_queue_head_t wait;
112 struct work_struct rxwork;
113 int werr;
114 int rerr;
115};
116
117static struct usb_driver wdm_driver;
118
119/* --- callbacks --- */
120static void wdm_out_callback(struct urb *urb)
121{
122 struct wdm_device *desc;
123 desc = urb->context;
124 spin_lock(&desc->iuspin);
125 desc->werr = urb->status;
126 spin_unlock(&desc->iuspin);
127 clear_bit(WDM_IN_USE, &desc->flags);
128 kfree(desc->outbuf);
129 wake_up(&desc->wait);
130}
131
132static void wdm_in_callback(struct urb *urb)
133{
134 struct wdm_device *desc = urb->context;
135 int status = urb->status;
136
137 spin_lock(&desc->iuspin);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100138 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200139
140 if (status) {
141 switch (status) {
142 case -ENOENT:
143 dev_dbg(&desc->intf->dev,
144 "nonzero urb status received: -ENOENT");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100145 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200146 case -ECONNRESET:
147 dev_dbg(&desc->intf->dev,
148 "nonzero urb status received: -ECONNRESET");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100149 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200150 case -ESHUTDOWN:
151 dev_dbg(&desc->intf->dev,
152 "nonzero urb status received: -ESHUTDOWN");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100153 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200154 case -EPIPE:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700155 dev_err(&desc->intf->dev,
156 "nonzero urb status received: -EPIPE\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200157 break;
158 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700159 dev_err(&desc->intf->dev,
160 "Unexpected error %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200161 break;
162 }
163 }
164
165 desc->rerr = status;
166 desc->reslength = urb->actual_length;
167 memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength);
168 desc->length += desc->reslength;
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100169skip_error:
Oliver Neukumafba9372008-05-13 17:01:25 +0200170 wake_up(&desc->wait);
171
172 set_bit(WDM_READ, &desc->flags);
173 spin_unlock(&desc->iuspin);
174}
175
176static void wdm_int_callback(struct urb *urb)
177{
178 int rv = 0;
179 int status = urb->status;
180 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200181 struct usb_cdc_notification *dr;
182
183 desc = urb->context;
Oliver Neukumafba9372008-05-13 17:01:25 +0200184 dr = (struct usb_cdc_notification *)desc->sbuf;
185
186 if (status) {
187 switch (status) {
188 case -ESHUTDOWN:
189 case -ENOENT:
190 case -ECONNRESET:
191 return; /* unplug */
192 case -EPIPE:
193 set_bit(WDM_INT_STALL, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700194 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200195 goto sw; /* halt is cleared in work */
196 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700197 dev_err(&desc->intf->dev,
198 "nonzero urb status received: %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200199 break;
200 }
201 }
202
203 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700204 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
205 urb->actual_length);
Oliver Neukumafba9372008-05-13 17:01:25 +0200206 goto exit;
207 }
208
209 switch (dr->bNotificationType) {
210 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
211 dev_dbg(&desc->intf->dev,
212 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
213 dr->wIndex, dr->wLength);
214 break;
215
216 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
217
218 dev_dbg(&desc->intf->dev,
219 "NOTIFY_NETWORK_CONNECTION %s network",
220 dr->wValue ? "connected to" : "disconnected from");
221 goto exit;
222 default:
223 clear_bit(WDM_POLL_RUNNING, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700224 dev_err(&desc->intf->dev,
225 "unknown notification %d received: index %d len %d\n",
Oliver Neukumafba9372008-05-13 17:01:25 +0200226 dr->bNotificationType, dr->wIndex, dr->wLength);
227 goto exit;
228 }
229
Oliver Neukumafba9372008-05-13 17:01:25 +0200230 spin_lock(&desc->iuspin);
231 clear_bit(WDM_READ, &desc->flags);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100232 set_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100233 if (!test_bit(WDM_DISCONNECTING, &desc->flags)
234 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200235 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
236 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
237 __func__, rv);
238 }
239 spin_unlock(&desc->iuspin);
240 if (rv < 0) {
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100241 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200242 if (rv == -EPERM)
243 return;
244 if (rv == -ENOMEM) {
245sw:
246 rv = schedule_work(&desc->rxwork);
247 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700248 dev_err(&desc->intf->dev,
249 "Cannot schedule work\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200250 }
251 }
252exit:
253 rv = usb_submit_urb(urb, GFP_ATOMIC);
254 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700255 dev_err(&desc->intf->dev,
256 "%s - usb_submit_urb failed with result %d\n",
257 __func__, rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200258
259}
260
261static void kill_urbs(struct wdm_device *desc)
262{
Oliver Neukum17d80d52008-06-24 15:56:10 +0200263 /* the order here is essential */
Oliver Neukumafba9372008-05-13 17:01:25 +0200264 usb_kill_urb(desc->command);
265 usb_kill_urb(desc->validity);
266 usb_kill_urb(desc->response);
267}
268
269static void free_urbs(struct wdm_device *desc)
270{
271 usb_free_urb(desc->validity);
272 usb_free_urb(desc->response);
273 usb_free_urb(desc->command);
274}
275
276static void cleanup(struct wdm_device *desc)
277{
Bjørn Mork8457d992012-01-16 15:12:00 +0100278 kfree(desc->sbuf);
279 kfree(desc->inbuf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200280 kfree(desc->orq);
281 kfree(desc->irq);
282 kfree(desc->ubuf);
283 free_urbs(desc);
284 kfree(desc);
285}
286
287static ssize_t wdm_write
288(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
289{
290 u8 *buf;
291 int rv = -EMSGSIZE, r, we;
292 struct wdm_device *desc = file->private_data;
293 struct usb_ctrlrequest *req;
294
295 if (count > desc->wMaxCommand)
296 count = desc->wMaxCommand;
297
298 spin_lock_irq(&desc->iuspin);
299 we = desc->werr;
300 desc->werr = 0;
301 spin_unlock_irq(&desc->iuspin);
302 if (we < 0)
303 return -EIO;
304
Oliver Neukum860e41a2010-02-27 20:54:24 +0100305 desc->outbuf = buf = kmalloc(count, GFP_KERNEL);
306 if (!buf) {
307 rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200308 goto outnl;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100309 }
310
311 r = copy_from_user(buf, buffer, count);
312 if (r > 0) {
313 kfree(buf);
314 rv = -EFAULT;
315 goto outnl;
316 }
317
318 /* concurrent writes and disconnect */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100319 r = mutex_lock_interruptible(&desc->wlock);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100320 rv = -ERESTARTSYS;
321 if (r) {
322 kfree(buf);
323 goto outnl;
324 }
325
326 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
327 kfree(buf);
328 rv = -ENODEV;
329 goto outnp;
330 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200331
Oliver Neukum17d80d52008-06-24 15:56:10 +0200332 r = usb_autopm_get_interface(desc->intf);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100333 if (r < 0) {
334 kfree(buf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200335 goto outnp;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100336 }
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200337
David Sterba0cdfb812010-12-27 18:49:58 +0100338 if (!(file->f_flags & O_NONBLOCK))
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200339 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
340 &desc->flags));
341 else
342 if (test_bit(WDM_IN_USE, &desc->flags))
343 r = -EAGAIN;
Bjørn Mork88044202012-02-10 09:44:08 +0100344
345 if (test_bit(WDM_RESETTING, &desc->flags))
346 r = -EIO;
347
Oliver Neukum860e41a2010-02-27 20:54:24 +0100348 if (r < 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200349 kfree(buf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200350 goto out;
351 }
352
353 req = desc->orq;
354 usb_fill_control_urb(
355 desc->command,
356 interface_to_usbdev(desc->intf),
357 /* using common endpoint 0 */
358 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
359 (unsigned char *)req,
360 buf,
361 count,
362 wdm_out_callback,
363 desc
364 );
365
366 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
367 USB_RECIP_INTERFACE);
368 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
369 req->wValue = 0;
370 req->wIndex = desc->inum;
371 req->wLength = cpu_to_le16(count);
372 set_bit(WDM_IN_USE, &desc->flags);
373
374 rv = usb_submit_urb(desc->command, GFP_KERNEL);
375 if (rv < 0) {
376 kfree(buf);
377 clear_bit(WDM_IN_USE, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700378 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200379 } else {
380 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
381 req->wIndex);
382 }
383out:
Oliver Neukum17d80d52008-06-24 15:56:10 +0200384 usb_autopm_put_interface(desc->intf);
385outnp:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100386 mutex_unlock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200387outnl:
388 return rv < 0 ? rv : count;
389}
390
391static ssize_t wdm_read
392(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
393{
Ben Hutchings711c68b2012-02-12 06:00:41 +0000394 int rv, cntr;
Oliver Neukumafba9372008-05-13 17:01:25 +0200395 int i = 0;
396 struct wdm_device *desc = file->private_data;
397
398
Bjørn Morke8537bd2012-01-16 12:41:48 +0100399 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
Oliver Neukumafba9372008-05-13 17:01:25 +0200400 if (rv < 0)
401 return -ERESTARTSYS;
402
Ben Hutchings711c68b2012-02-12 06:00:41 +0000403 cntr = ACCESS_ONCE(desc->length);
404 if (cntr == 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200405 desc->read = 0;
406retry:
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200407 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
408 rv = -ENODEV;
409 goto err;
410 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200411 i++;
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200412 if (file->f_flags & O_NONBLOCK) {
413 if (!test_bit(WDM_READ, &desc->flags)) {
414 rv = cntr ? cntr : -EAGAIN;
415 goto err;
416 }
417 rv = 0;
418 } else {
419 rv = wait_event_interruptible(desc->wait,
420 test_bit(WDM_READ, &desc->flags));
421 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200422
Oliver Neukum7f1dc313d2009-09-09 10:12:48 +0200423 /* may have happened while we slept */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200424 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
425 rv = -ENODEV;
426 goto err;
427 }
Bjørn Mork88044202012-02-10 09:44:08 +0100428 if (test_bit(WDM_RESETTING, &desc->flags)) {
429 rv = -EIO;
430 goto err;
431 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200432 usb_mark_last_busy(interface_to_usbdev(desc->intf));
Oliver Neukumafba9372008-05-13 17:01:25 +0200433 if (rv < 0) {
434 rv = -ERESTARTSYS;
435 goto err;
436 }
437
438 spin_lock_irq(&desc->iuspin);
439
440 if (desc->rerr) { /* read completed, error happened */
Oliver Neukumafba9372008-05-13 17:01:25 +0200441 desc->rerr = 0;
442 spin_unlock_irq(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200443 rv = -EIO;
444 goto err;
445 }
446 /*
447 * recheck whether we've lost the race
448 * against the completion handler
449 */
450 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
451 spin_unlock_irq(&desc->iuspin);
452 goto retry;
453 }
454 if (!desc->reslength) { /* zero length read */
455 spin_unlock_irq(&desc->iuspin);
456 goto retry;
457 }
Ben Hutchings711c68b2012-02-12 06:00:41 +0000458 cntr = desc->length;
Oliver Neukumafba9372008-05-13 17:01:25 +0200459 spin_unlock_irq(&desc->iuspin);
460 }
461
Ben Hutchings711c68b2012-02-12 06:00:41 +0000462 if (cntr > count)
463 cntr = count;
Oliver Neukumafba9372008-05-13 17:01:25 +0200464 rv = copy_to_user(buffer, desc->ubuf, cntr);
465 if (rv > 0) {
466 rv = -EFAULT;
467 goto err;
468 }
469
Ben Hutchings711c68b2012-02-12 06:00:41 +0000470 spin_lock_irq(&desc->iuspin);
471
Oliver Neukumafba9372008-05-13 17:01:25 +0200472 for (i = 0; i < desc->length - cntr; i++)
473 desc->ubuf[i] = desc->ubuf[i + cntr];
474
475 desc->length -= cntr;
Oliver Neukum87d65e52008-06-19 14:20:18 +0200476 /* in case we had outstanding data */
477 if (!desc->length)
478 clear_bit(WDM_READ, &desc->flags);
Ben Hutchings711c68b2012-02-12 06:00:41 +0000479
480 spin_unlock_irq(&desc->iuspin);
481
Oliver Neukumafba9372008-05-13 17:01:25 +0200482 rv = cntr;
483
484err:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100485 mutex_unlock(&desc->rlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200486 return rv;
487}
488
489static int wdm_flush(struct file *file, fl_owner_t id)
490{
491 struct wdm_device *desc = file->private_data;
492
493 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
494 if (desc->werr < 0)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700495 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
496 desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200497
498 return desc->werr;
499}
500
501static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
502{
503 struct wdm_device *desc = file->private_data;
504 unsigned long flags;
505 unsigned int mask = 0;
506
507 spin_lock_irqsave(&desc->iuspin, flags);
508 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
509 mask = POLLERR;
510 spin_unlock_irqrestore(&desc->iuspin, flags);
511 goto desc_out;
512 }
513 if (test_bit(WDM_READ, &desc->flags))
514 mask = POLLIN | POLLRDNORM;
515 if (desc->rerr || desc->werr)
516 mask |= POLLERR;
517 if (!test_bit(WDM_IN_USE, &desc->flags))
518 mask |= POLLOUT | POLLWRNORM;
519 spin_unlock_irqrestore(&desc->iuspin, flags);
520
521 poll_wait(file, &desc->wait, wait);
522
523desc_out:
524 return mask;
525}
526
527static int wdm_open(struct inode *inode, struct file *file)
528{
529 int minor = iminor(inode);
530 int rv = -ENODEV;
531 struct usb_interface *intf;
532 struct wdm_device *desc;
533
534 mutex_lock(&wdm_mutex);
535 intf = usb_find_interface(&wdm_driver, minor);
536 if (!intf)
537 goto out;
538
539 desc = usb_get_intfdata(intf);
540 if (test_bit(WDM_DISCONNECTING, &desc->flags))
541 goto out;
Oliver Neukumafba9372008-05-13 17:01:25 +0200542 file->private_data = desc;
543
Oliver Neukum17d80d52008-06-24 15:56:10 +0200544 rv = usb_autopm_get_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200545 if (rv < 0) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700546 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200547 goto out;
548 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200549 intf->needs_remote_wakeup = 1;
Oliver Neukumafba9372008-05-13 17:01:25 +0200550
Bjørn Morke8537bd2012-01-16 12:41:48 +0100551 /* using write lock to protect desc->count */
552 mutex_lock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200553 if (!desc->count++) {
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200554 desc->werr = 0;
555 desc->rerr = 0;
Oliver Neukum17d80d52008-06-24 15:56:10 +0200556 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
557 if (rv < 0) {
558 desc->count--;
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700559 dev_err(&desc->intf->dev,
560 "Error submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200561 }
562 } else {
563 rv = 0;
564 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100565 mutex_unlock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200566 usb_autopm_put_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200567out:
568 mutex_unlock(&wdm_mutex);
569 return rv;
570}
571
572static int wdm_release(struct inode *inode, struct file *file)
573{
574 struct wdm_device *desc = file->private_data;
575
576 mutex_lock(&wdm_mutex);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100577
578 /* using write lock to protect desc->count */
579 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200580 desc->count--;
Bjørn Morke8537bd2012-01-16 12:41:48 +0100581 mutex_unlock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200582
Oliver Neukumafba9372008-05-13 17:01:25 +0200583 if (!desc->count) {
584 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
585 kill_urbs(desc);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200586 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
587 desc->intf->needs_remote_wakeup = 0;
Oliver Neukumafba9372008-05-13 17:01:25 +0200588 }
589 mutex_unlock(&wdm_mutex);
590 return 0;
591}
592
593static const struct file_operations wdm_fops = {
594 .owner = THIS_MODULE,
595 .read = wdm_read,
596 .write = wdm_write,
597 .open = wdm_open,
598 .flush = wdm_flush,
599 .release = wdm_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200600 .poll = wdm_poll,
601 .llseek = noop_llseek,
Oliver Neukumafba9372008-05-13 17:01:25 +0200602};
603
604static struct usb_class_driver wdm_class = {
605 .name = "cdc-wdm%d",
606 .fops = &wdm_fops,
607 .minor_base = WDM_MINOR_BASE,
608};
609
610/* --- error handling --- */
611static void wdm_rxwork(struct work_struct *work)
612{
613 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
614 unsigned long flags;
615 int rv;
616
617 spin_lock_irqsave(&desc->iuspin, flags);
618 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
619 spin_unlock_irqrestore(&desc->iuspin, flags);
620 } else {
621 spin_unlock_irqrestore(&desc->iuspin, flags);
622 rv = usb_submit_urb(desc->response, GFP_KERNEL);
623 if (rv < 0 && rv != -EPERM) {
624 spin_lock_irqsave(&desc->iuspin, flags);
625 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
626 schedule_work(&desc->rxwork);
627 spin_unlock_irqrestore(&desc->iuspin, flags);
628 }
629 }
630}
631
632/* --- hotplug --- */
633
Bjørn Mork0dffb482012-03-06 17:29:20 +0100634static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep, u16 bufsize)
Oliver Neukumafba9372008-05-13 17:01:25 +0200635{
Bjørn Mork0dffb482012-03-06 17:29:20 +0100636 int rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200637 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200638
Oliver Neukumafba9372008-05-13 17:01:25 +0200639 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
640 if (!desc)
641 goto out;
Bjørn Morke8537bd2012-01-16 12:41:48 +0100642 mutex_init(&desc->rlock);
643 mutex_init(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200644 spin_lock_init(&desc->iuspin);
645 init_waitqueue_head(&desc->wait);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100646 desc->wMaxCommand = bufsize;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200647 /* this will be expanded and needed in hardware endianness */
Oliver Neukumafba9372008-05-13 17:01:25 +0200648 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
649 desc->intf = intf;
650 INIT_WORK(&desc->rxwork, wdm_rxwork);
651
Oliver Neukum052fbc02009-04-20 17:24:49 +0200652 rv = -EINVAL;
Bjørn Mork0dffb482012-03-06 17:29:20 +0100653 if (!usb_endpoint_is_int_in(ep))
Oliver Neukum052fbc02009-04-20 17:24:49 +0200654 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200655
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700656 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
Oliver Neukumafba9372008-05-13 17:01:25 +0200657
658 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
659 if (!desc->orq)
660 goto err;
661 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
662 if (!desc->irq)
663 goto err;
664
665 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
666 if (!desc->validity)
667 goto err;
668
669 desc->response = usb_alloc_urb(0, GFP_KERNEL);
670 if (!desc->response)
671 goto err;
672
673 desc->command = usb_alloc_urb(0, GFP_KERNEL);
674 if (!desc->command)
675 goto err;
676
677 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
678 if (!desc->ubuf)
679 goto err;
680
Bjørn Mork8457d992012-01-16 15:12:00 +0100681 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200682 if (!desc->sbuf)
683 goto err;
684
Bjørn Mork8457d992012-01-16 15:12:00 +0100685 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200686 if (!desc->inbuf)
Bjørn Mork8457d992012-01-16 15:12:00 +0100687 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200688
689 usb_fill_int_urb(
690 desc->validity,
691 interface_to_usbdev(intf),
692 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
693 desc->sbuf,
694 desc->wMaxPacketSize,
695 wdm_int_callback,
696 desc,
697 ep->bInterval
698 );
Oliver Neukumafba9372008-05-13 17:01:25 +0200699
Bjørn Mork19b85b32012-01-16 15:11:58 +0100700 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
701 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
702 desc->irq->wValue = 0;
703 desc->irq->wIndex = desc->inum;
704 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
705
706 usb_fill_control_urb(
707 desc->response,
Bjørn Mork8143a892012-01-16 15:12:01 +0100708 interface_to_usbdev(intf),
Bjørn Mork19b85b32012-01-16 15:11:58 +0100709 /* using common endpoint 0 */
710 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
711 (unsigned char *)desc->irq,
712 desc->inbuf,
713 desc->wMaxCommand,
714 wdm_in_callback,
715 desc
716 );
Bjørn Mork19b85b32012-01-16 15:11:58 +0100717
Oliver Neukumafba9372008-05-13 17:01:25 +0200718 usb_set_intfdata(intf, desc);
719 rv = usb_register_dev(intf, &wdm_class);
Oliver Neukumafba9372008-05-13 17:01:25 +0200720 if (rv < 0)
Bjørn Mork8457d992012-01-16 15:12:00 +0100721 goto err2;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200722 else
Bjørn Mork820c6292012-01-20 04:17:25 +0100723 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
Oliver Neukumafba9372008-05-13 17:01:25 +0200724out:
725 return rv;
726err2:
Bjørn Mork8457d992012-01-16 15:12:00 +0100727 usb_set_intfdata(intf, NULL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200728err:
Bjørn Mork0dffb482012-03-06 17:29:20 +0100729 cleanup(desc);
730 return rv;
731}
732
733static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
734{
735 int rv = -EINVAL;
736 struct usb_host_interface *iface;
737 struct usb_endpoint_descriptor *ep;
738 struct usb_cdc_dmm_desc *dmhd;
739 u8 *buffer = intf->altsetting->extra;
740 int buflen = intf->altsetting->extralen;
741 u16 maxcom = WDM_DEFAULT_BUFSIZE;
742
743 if (!buffer)
744 goto err;
745 while (buflen > 2) {
746 if (buffer[1] != USB_DT_CS_INTERFACE) {
747 dev_err(&intf->dev, "skipping garbage\n");
748 goto next_desc;
749 }
750
751 switch (buffer[2]) {
752 case USB_CDC_HEADER_TYPE:
753 break;
754 case USB_CDC_DMM_TYPE:
755 dmhd = (struct usb_cdc_dmm_desc *)buffer;
756 maxcom = le16_to_cpu(dmhd->wMaxCommand);
757 dev_dbg(&intf->dev,
758 "Finding maximum buffer length: %d", maxcom);
759 break;
760 default:
761 dev_err(&intf->dev,
762 "Ignoring extra header, type %d, length %d\n",
763 buffer[2], buffer[0]);
764 break;
765 }
766next_desc:
767 buflen -= buffer[0];
768 buffer += buffer[0];
769 }
770
771 iface = intf->cur_altsetting;
772 if (iface->desc.bNumEndpoints != 1)
773 goto err;
774 ep = &iface->endpoint[0].desc;
775
776 rv = wdm_create(intf, ep, maxcom);
777
778err:
Oliver Neukumafba9372008-05-13 17:01:25 +0200779 return rv;
780}
781
782static void wdm_disconnect(struct usb_interface *intf)
783{
784 struct wdm_device *desc;
785 unsigned long flags;
786
787 usb_deregister_dev(intf, &wdm_class);
788 mutex_lock(&wdm_mutex);
789 desc = usb_get_intfdata(intf);
790
791 /* the spinlock makes sure no new urbs are generated in the callbacks */
792 spin_lock_irqsave(&desc->iuspin, flags);
793 set_bit(WDM_DISCONNECTING, &desc->flags);
794 set_bit(WDM_READ, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200795 /* to terminate pending flushes */
Oliver Neukumafba9372008-05-13 17:01:25 +0200796 clear_bit(WDM_IN_USE, &desc->flags);
797 spin_unlock_irqrestore(&desc->iuspin, flags);
Bjørn Mork62aaf242012-01-16 15:11:57 +0100798 wake_up_all(&desc->wait);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100799 mutex_lock(&desc->rlock);
800 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200801 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100802 cancel_work_sync(&desc->rxwork);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100803 mutex_unlock(&desc->wlock);
804 mutex_unlock(&desc->rlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200805 if (!desc->count)
806 cleanup(desc);
807 mutex_unlock(&wdm_mutex);
808}
809
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100810#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200811static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
812{
813 struct wdm_device *desc = usb_get_intfdata(intf);
814 int rv = 0;
815
816 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
817
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100818 /* if this is an autosuspend the caller does the locking */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100819 if (!PMSG_IS_AUTO(message)) {
820 mutex_lock(&desc->rlock);
821 mutex_lock(&desc->wlock);
822 }
Oliver Neukum62e66852010-02-27 20:56:22 +0100823 spin_lock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100824
Alan Stern5b1b0b82011-08-19 23:49:48 +0200825 if (PMSG_IS_AUTO(message) &&
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100826 (test_bit(WDM_IN_USE, &desc->flags)
827 || test_bit(WDM_RESPONDING, &desc->flags))) {
Oliver Neukum62e66852010-02-27 20:56:22 +0100828 spin_unlock_irq(&desc->iuspin);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200829 rv = -EBUSY;
830 } else {
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100831
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100832 set_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100833 spin_unlock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100834 /* callback submits work - order is essential */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200835 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100836 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200837 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100838 if (!PMSG_IS_AUTO(message)) {
839 mutex_unlock(&desc->wlock);
840 mutex_unlock(&desc->rlock);
841 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200842
843 return rv;
844}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100845#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200846
847static int recover_from_urb_loss(struct wdm_device *desc)
848{
849 int rv = 0;
850
851 if (desc->count) {
852 rv = usb_submit_urb(desc->validity, GFP_NOIO);
853 if (rv < 0)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700854 dev_err(&desc->intf->dev,
855 "Error resume submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200856 }
857 return rv;
858}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100859
860#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200861static int wdm_resume(struct usb_interface *intf)
862{
863 struct wdm_device *desc = usb_get_intfdata(intf);
864 int rv;
865
866 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
Oliver Neukum338124c2010-02-27 20:57:12 +0100867
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100868 clear_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100869 rv = recover_from_urb_loss(desc);
Oliver Neukum338124c2010-02-27 20:57:12 +0100870
Oliver Neukum17d80d52008-06-24 15:56:10 +0200871 return rv;
872}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100873#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200874
875static int wdm_pre_reset(struct usb_interface *intf)
876{
877 struct wdm_device *desc = usb_get_intfdata(intf);
878
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200879 /*
880 * we notify everybody using poll of
881 * an exceptional situation
882 * must be done before recovery lest a spontaneous
883 * message from the device is lost
884 */
885 spin_lock_irq(&desc->iuspin);
Bjørn Mork88044202012-02-10 09:44:08 +0100886 set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
887 set_bit(WDM_READ, &desc->flags); /* unblock read */
888 clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200889 desc->rerr = -EINTR;
890 spin_unlock_irq(&desc->iuspin);
891 wake_up_all(&desc->wait);
Bjørn Mork88044202012-02-10 09:44:08 +0100892 mutex_lock(&desc->rlock);
893 mutex_lock(&desc->wlock);
894 kill_urbs(desc);
895 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200896 return 0;
897}
898
899static int wdm_post_reset(struct usb_interface *intf)
900{
901 struct wdm_device *desc = usb_get_intfdata(intf);
902 int rv;
903
Bjørn Mork88044202012-02-10 09:44:08 +0100904 clear_bit(WDM_RESETTING, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200905 rv = recover_from_urb_loss(desc);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100906 mutex_unlock(&desc->wlock);
907 mutex_unlock(&desc->rlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200908 return 0;
909}
910
Oliver Neukumafba9372008-05-13 17:01:25 +0200911static struct usb_driver wdm_driver = {
912 .name = "cdc_wdm",
913 .probe = wdm_probe,
914 .disconnect = wdm_disconnect,
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100915#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200916 .suspend = wdm_suspend,
917 .resume = wdm_resume,
918 .reset_resume = wdm_resume,
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100919#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200920 .pre_reset = wdm_pre_reset,
921 .post_reset = wdm_post_reset,
Oliver Neukumafba9372008-05-13 17:01:25 +0200922 .id_table = wdm_ids,
Oliver Neukum17d80d52008-06-24 15:56:10 +0200923 .supports_autosuspend = 1,
Oliver Neukumafba9372008-05-13 17:01:25 +0200924};
925
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -0800926module_usb_driver(wdm_driver);
Oliver Neukumafba9372008-05-13 17:01:25 +0200927
928MODULE_AUTHOR(DRIVER_AUTHOR);
Oliver Neukum87d65e52008-06-19 14:20:18 +0200929MODULE_DESCRIPTION(DRIVER_DESC);
Oliver Neukumafba9372008-05-13 17:01:25 +0200930MODULE_LICENSE("GPL");