blob: 4c8321ea4c522f133b4a8b6c445c9c15544f18ee [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>
Bjørn Mork3cc36152012-03-06 17:29:22 +010026#include <linux/usb/cdc-wdm.h>
Oliver Neukumafba9372008-05-13 17:01:25 +020027
28/*
29 * Version Information
30 */
Oliver Neukum87d65e52008-06-19 14:20:18 +020031#define DRIVER_VERSION "v0.03"
Oliver Neukumafba9372008-05-13 17:01:25 +020032#define DRIVER_AUTHOR "Oliver Neukum"
Oliver Neukum87d65e52008-06-19 14:20:18 +020033#define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management"
Oliver Neukumafba9372008-05-13 17:01:25 +020034
Bjørn Morkfec67b42012-01-25 13:03:29 +010035#define HUAWEI_VENDOR_ID 0x12D1
36
Németh Márton6ef48522010-01-10 15:33:45 +010037static const struct usb_device_id wdm_ids[] = {
Oliver Neukumafba9372008-05-13 17:01:25 +020038 {
39 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS |
40 USB_DEVICE_ID_MATCH_INT_SUBCLASS,
41 .bInterfaceClass = USB_CLASS_COMM,
42 .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM
43 },
Bjørn Morkfec67b42012-01-25 13:03:29 +010044 {
45 /*
46 * Huawei E392, E398 and possibly other Qualcomm based modems
47 * embed the Qualcomm QMI protocol inside CDC on CDC ECM like
48 * control interfaces. Userspace access to this is required
49 * to configure the accompanying data interface
50 */
51 .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
52 USB_DEVICE_ID_MATCH_INT_INFO,
53 .idVendor = HUAWEI_VENDOR_ID,
54 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
55 .bInterfaceSubClass = 1,
56 .bInterfaceProtocol = 9, /* NOTE: CDC ECM control interface! */
57 },
Oliver Neukumafba9372008-05-13 17:01:25 +020058 { }
59};
60
Oliver Neukumaa5380b2008-10-13 14:05:20 +020061MODULE_DEVICE_TABLE (usb, wdm_ids);
62
Oliver Neukumafba9372008-05-13 17:01:25 +020063#define WDM_MINOR_BASE 176
64
65
66#define WDM_IN_USE 1
67#define WDM_DISCONNECTING 2
68#define WDM_RESULT 3
69#define WDM_READ 4
70#define WDM_INT_STALL 5
71#define WDM_POLL_RUNNING 6
Oliver Neukum922a5ea2010-02-27 20:54:59 +010072#define WDM_RESPONDING 7
Oliver Neukumbeb1d352010-02-27 20:55:52 +010073#define WDM_SUSPENDING 8
Bjørn Mork88044202012-02-10 09:44:08 +010074#define WDM_RESETTING 9
Oliver Neukumafba9372008-05-13 17:01:25 +020075
76#define WDM_MAX 16
77
Bjørn Mork7e3054a2012-01-20 01:49:57 +010078/* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */
79#define WDM_DEFAULT_BUFSIZE 256
Oliver Neukumafba9372008-05-13 17:01:25 +020080
81static DEFINE_MUTEX(wdm_mutex);
Bjørn Morkb0c13862012-03-06 17:29:21 +010082static DEFINE_SPINLOCK(wdm_device_list_lock);
83static LIST_HEAD(wdm_device_list);
Oliver Neukumafba9372008-05-13 17:01:25 +020084
85/* --- method tables --- */
86
87struct wdm_device {
88 u8 *inbuf; /* buffer for response */
89 u8 *outbuf; /* buffer for command */
90 u8 *sbuf; /* buffer for status */
91 u8 *ubuf; /* buffer for copy to user space */
92
93 struct urb *command;
94 struct urb *response;
95 struct urb *validity;
96 struct usb_interface *intf;
97 struct usb_ctrlrequest *orq;
98 struct usb_ctrlrequest *irq;
99 spinlock_t iuspin;
100
101 unsigned long flags;
102 u16 bufsize;
103 u16 wMaxCommand;
104 u16 wMaxPacketSize;
Oliver Neukumafba9372008-05-13 17:01:25 +0200105 __le16 inum;
106 int reslength;
107 int length;
108 int read;
109 int count;
110 dma_addr_t shandle;
111 dma_addr_t ihandle;
Bjørn Morke8537bd2012-01-16 12:41:48 +0100112 struct mutex wlock;
113 struct mutex rlock;
Oliver Neukumafba9372008-05-13 17:01:25 +0200114 wait_queue_head_t wait;
115 struct work_struct rxwork;
116 int werr;
117 int rerr;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100118
119 struct list_head device_list;
Bjørn Mork3cc36152012-03-06 17:29:22 +0100120 int (*manage_power)(struct usb_interface *, int);
Oliver Neukumafba9372008-05-13 17:01:25 +0200121};
122
123static struct usb_driver wdm_driver;
124
Bjørn Morkb0c13862012-03-06 17:29:21 +0100125/* return intfdata if we own the interface, else look up intf in the list */
126static struct wdm_device *wdm_find_device(struct usb_interface *intf)
127{
128 struct wdm_device *desc = NULL;
129
130 spin_lock(&wdm_device_list_lock);
131 list_for_each_entry(desc, &wdm_device_list, device_list)
132 if (desc->intf == intf)
133 break;
134 spin_unlock(&wdm_device_list_lock);
135
136 return desc;
137}
138
139static struct wdm_device *wdm_find_device_by_minor(int minor)
140{
141 struct wdm_device *desc = NULL;
142
143 spin_lock(&wdm_device_list_lock);
144 list_for_each_entry(desc, &wdm_device_list, device_list)
145 if (desc->intf->minor == minor)
146 break;
147 spin_unlock(&wdm_device_list_lock);
148
149 return desc;
150}
151
Oliver Neukumafba9372008-05-13 17:01:25 +0200152/* --- callbacks --- */
153static void wdm_out_callback(struct urb *urb)
154{
155 struct wdm_device *desc;
156 desc = urb->context;
157 spin_lock(&desc->iuspin);
158 desc->werr = urb->status;
159 spin_unlock(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200160 kfree(desc->outbuf);
Oliver Neukum5c228372012-04-26 21:59:10 +0200161 desc->outbuf = NULL;
162 clear_bit(WDM_IN_USE, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200163 wake_up(&desc->wait);
164}
165
166static void wdm_in_callback(struct urb *urb)
167{
168 struct wdm_device *desc = urb->context;
169 int status = urb->status;
170
171 spin_lock(&desc->iuspin);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100172 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200173
174 if (status) {
175 switch (status) {
176 case -ENOENT:
177 dev_dbg(&desc->intf->dev,
178 "nonzero urb status received: -ENOENT");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100179 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200180 case -ECONNRESET:
181 dev_dbg(&desc->intf->dev,
182 "nonzero urb status received: -ECONNRESET");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100183 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200184 case -ESHUTDOWN:
185 dev_dbg(&desc->intf->dev,
186 "nonzero urb status received: -ESHUTDOWN");
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100187 goto skip_error;
Oliver Neukumafba9372008-05-13 17:01:25 +0200188 case -EPIPE:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700189 dev_err(&desc->intf->dev,
190 "nonzero urb status received: -EPIPE\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200191 break;
192 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700193 dev_err(&desc->intf->dev,
194 "Unexpected error %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200195 break;
196 }
197 }
198
199 desc->rerr = status;
200 desc->reslength = urb->actual_length;
201 memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength);
202 desc->length += desc->reslength;
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100203skip_error:
Oliver Neukumafba9372008-05-13 17:01:25 +0200204 wake_up(&desc->wait);
205
206 set_bit(WDM_READ, &desc->flags);
207 spin_unlock(&desc->iuspin);
208}
209
210static void wdm_int_callback(struct urb *urb)
211{
212 int rv = 0;
213 int status = urb->status;
214 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200215 struct usb_cdc_notification *dr;
216
217 desc = urb->context;
Oliver Neukumafba9372008-05-13 17:01:25 +0200218 dr = (struct usb_cdc_notification *)desc->sbuf;
219
220 if (status) {
221 switch (status) {
222 case -ESHUTDOWN:
223 case -ENOENT:
224 case -ECONNRESET:
225 return; /* unplug */
226 case -EPIPE:
227 set_bit(WDM_INT_STALL, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700228 dev_err(&desc->intf->dev, "Stall on int endpoint\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200229 goto sw; /* halt is cleared in work */
230 default:
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700231 dev_err(&desc->intf->dev,
232 "nonzero urb status received: %d\n", status);
Oliver Neukumafba9372008-05-13 17:01:25 +0200233 break;
234 }
235 }
236
237 if (urb->actual_length < sizeof(struct usb_cdc_notification)) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700238 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n",
239 urb->actual_length);
Oliver Neukumafba9372008-05-13 17:01:25 +0200240 goto exit;
241 }
242
243 switch (dr->bNotificationType) {
244 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
245 dev_dbg(&desc->intf->dev,
246 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d",
247 dr->wIndex, dr->wLength);
248 break;
249
250 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
251
252 dev_dbg(&desc->intf->dev,
253 "NOTIFY_NETWORK_CONNECTION %s network",
254 dr->wValue ? "connected to" : "disconnected from");
255 goto exit;
256 default:
257 clear_bit(WDM_POLL_RUNNING, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700258 dev_err(&desc->intf->dev,
259 "unknown notification %d received: index %d len %d\n",
Oliver Neukumafba9372008-05-13 17:01:25 +0200260 dr->bNotificationType, dr->wIndex, dr->wLength);
261 goto exit;
262 }
263
Oliver Neukumafba9372008-05-13 17:01:25 +0200264 spin_lock(&desc->iuspin);
265 clear_bit(WDM_READ, &desc->flags);
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100266 set_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100267 if (!test_bit(WDM_DISCONNECTING, &desc->flags)
268 && !test_bit(WDM_SUSPENDING, &desc->flags)) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200269 rv = usb_submit_urb(desc->response, GFP_ATOMIC);
270 dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d",
271 __func__, rv);
272 }
273 spin_unlock(&desc->iuspin);
274 if (rv < 0) {
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100275 clear_bit(WDM_RESPONDING, &desc->flags);
Oliver Neukumafba9372008-05-13 17:01:25 +0200276 if (rv == -EPERM)
277 return;
278 if (rv == -ENOMEM) {
279sw:
280 rv = schedule_work(&desc->rxwork);
281 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700282 dev_err(&desc->intf->dev,
283 "Cannot schedule work\n");
Oliver Neukumafba9372008-05-13 17:01:25 +0200284 }
285 }
286exit:
287 rv = usb_submit_urb(urb, GFP_ATOMIC);
288 if (rv)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700289 dev_err(&desc->intf->dev,
290 "%s - usb_submit_urb failed with result %d\n",
291 __func__, rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200292
293}
294
295static void kill_urbs(struct wdm_device *desc)
296{
Oliver Neukum17d80d52008-06-24 15:56:10 +0200297 /* the order here is essential */
Oliver Neukumafba9372008-05-13 17:01:25 +0200298 usb_kill_urb(desc->command);
299 usb_kill_urb(desc->validity);
300 usb_kill_urb(desc->response);
301}
302
303static void free_urbs(struct wdm_device *desc)
304{
305 usb_free_urb(desc->validity);
306 usb_free_urb(desc->response);
307 usb_free_urb(desc->command);
308}
309
310static void cleanup(struct wdm_device *desc)
311{
Bjørn Mork8457d992012-01-16 15:12:00 +0100312 kfree(desc->sbuf);
313 kfree(desc->inbuf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200314 kfree(desc->orq);
315 kfree(desc->irq);
316 kfree(desc->ubuf);
317 free_urbs(desc);
318 kfree(desc);
319}
320
321static ssize_t wdm_write
322(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
323{
324 u8 *buf;
325 int rv = -EMSGSIZE, r, we;
326 struct wdm_device *desc = file->private_data;
327 struct usb_ctrlrequest *req;
328
329 if (count > desc->wMaxCommand)
330 count = desc->wMaxCommand;
331
332 spin_lock_irq(&desc->iuspin);
333 we = desc->werr;
334 desc->werr = 0;
335 spin_unlock_irq(&desc->iuspin);
336 if (we < 0)
337 return -EIO;
338
Oliver Neukum5c228372012-04-26 21:59:10 +0200339 buf = kmalloc(count, GFP_KERNEL);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100340 if (!buf) {
341 rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200342 goto outnl;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100343 }
344
345 r = copy_from_user(buf, buffer, count);
346 if (r > 0) {
347 kfree(buf);
348 rv = -EFAULT;
349 goto outnl;
350 }
351
352 /* concurrent writes and disconnect */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100353 r = mutex_lock_interruptible(&desc->wlock);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100354 rv = -ERESTARTSYS;
355 if (r) {
356 kfree(buf);
357 goto outnl;
358 }
359
360 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
361 kfree(buf);
362 rv = -ENODEV;
363 goto outnp;
364 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200365
Oliver Neukum17d80d52008-06-24 15:56:10 +0200366 r = usb_autopm_get_interface(desc->intf);
Oliver Neukum860e41a2010-02-27 20:54:24 +0100367 if (r < 0) {
368 kfree(buf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200369 goto outnp;
Oliver Neukum860e41a2010-02-27 20:54:24 +0100370 }
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200371
David Sterba0cdfb812010-12-27 18:49:58 +0100372 if (!(file->f_flags & O_NONBLOCK))
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200373 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE,
374 &desc->flags));
375 else
376 if (test_bit(WDM_IN_USE, &desc->flags))
377 r = -EAGAIN;
Bjørn Mork88044202012-02-10 09:44:08 +0100378
379 if (test_bit(WDM_RESETTING, &desc->flags))
380 r = -EIO;
381
Oliver Neukum860e41a2010-02-27 20:54:24 +0100382 if (r < 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200383 kfree(buf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200384 goto out;
385 }
386
387 req = desc->orq;
388 usb_fill_control_urb(
389 desc->command,
390 interface_to_usbdev(desc->intf),
391 /* using common endpoint 0 */
392 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0),
393 (unsigned char *)req,
394 buf,
395 count,
396 wdm_out_callback,
397 desc
398 );
399
400 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS |
401 USB_RECIP_INTERFACE);
402 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
403 req->wValue = 0;
404 req->wIndex = desc->inum;
405 req->wLength = cpu_to_le16(count);
406 set_bit(WDM_IN_USE, &desc->flags);
Oliver Neukum5c228372012-04-26 21:59:10 +0200407 desc->outbuf = buf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200408
409 rv = usb_submit_urb(desc->command, GFP_KERNEL);
410 if (rv < 0) {
411 kfree(buf);
Oliver Neukum5c228372012-04-26 21:59:10 +0200412 desc->outbuf = NULL;
Oliver Neukumafba9372008-05-13 17:01:25 +0200413 clear_bit(WDM_IN_USE, &desc->flags);
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700414 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200415 } else {
416 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d",
417 req->wIndex);
418 }
419out:
Oliver Neukum17d80d52008-06-24 15:56:10 +0200420 usb_autopm_put_interface(desc->intf);
421outnp:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100422 mutex_unlock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200423outnl:
424 return rv < 0 ? rv : count;
425}
426
427static ssize_t wdm_read
428(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
429{
Ben Hutchings711c68b2012-02-12 06:00:41 +0000430 int rv, cntr;
Oliver Neukumafba9372008-05-13 17:01:25 +0200431 int i = 0;
432 struct wdm_device *desc = file->private_data;
433
434
Bjørn Morke8537bd2012-01-16 12:41:48 +0100435 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */
Oliver Neukumafba9372008-05-13 17:01:25 +0200436 if (rv < 0)
437 return -ERESTARTSYS;
438
Ben Hutchings711c68b2012-02-12 06:00:41 +0000439 cntr = ACCESS_ONCE(desc->length);
440 if (cntr == 0) {
Oliver Neukumafba9372008-05-13 17:01:25 +0200441 desc->read = 0;
442retry:
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200443 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
444 rv = -ENODEV;
445 goto err;
446 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200447 i++;
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200448 if (file->f_flags & O_NONBLOCK) {
449 if (!test_bit(WDM_READ, &desc->flags)) {
450 rv = cntr ? cntr : -EAGAIN;
451 goto err;
452 }
453 rv = 0;
454 } else {
455 rv = wait_event_interruptible(desc->wait,
456 test_bit(WDM_READ, &desc->flags));
457 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200458
Oliver Neukum7f1dc312009-09-09 10:12:48 +0200459 /* may have happened while we slept */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200460 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
461 rv = -ENODEV;
462 goto err;
463 }
Bjørn Mork88044202012-02-10 09:44:08 +0100464 if (test_bit(WDM_RESETTING, &desc->flags)) {
465 rv = -EIO;
466 goto err;
467 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200468 usb_mark_last_busy(interface_to_usbdev(desc->intf));
Oliver Neukumafba9372008-05-13 17:01:25 +0200469 if (rv < 0) {
470 rv = -ERESTARTSYS;
471 goto err;
472 }
473
474 spin_lock_irq(&desc->iuspin);
475
476 if (desc->rerr) { /* read completed, error happened */
Oliver Neukumafba9372008-05-13 17:01:25 +0200477 desc->rerr = 0;
478 spin_unlock_irq(&desc->iuspin);
Oliver Neukumafba9372008-05-13 17:01:25 +0200479 rv = -EIO;
480 goto err;
481 }
482 /*
483 * recheck whether we've lost the race
484 * against the completion handler
485 */
486 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */
487 spin_unlock_irq(&desc->iuspin);
488 goto retry;
489 }
490 if (!desc->reslength) { /* zero length read */
491 spin_unlock_irq(&desc->iuspin);
492 goto retry;
493 }
Ben Hutchings711c68b2012-02-12 06:00:41 +0000494 cntr = desc->length;
Oliver Neukumafba9372008-05-13 17:01:25 +0200495 spin_unlock_irq(&desc->iuspin);
496 }
497
Ben Hutchings711c68b2012-02-12 06:00:41 +0000498 if (cntr > count)
499 cntr = count;
Oliver Neukumafba9372008-05-13 17:01:25 +0200500 rv = copy_to_user(buffer, desc->ubuf, cntr);
501 if (rv > 0) {
502 rv = -EFAULT;
503 goto err;
504 }
505
Ben Hutchings711c68b2012-02-12 06:00:41 +0000506 spin_lock_irq(&desc->iuspin);
507
Oliver Neukumafba9372008-05-13 17:01:25 +0200508 for (i = 0; i < desc->length - cntr; i++)
509 desc->ubuf[i] = desc->ubuf[i + cntr];
510
511 desc->length -= cntr;
Oliver Neukum87d65e52008-06-19 14:20:18 +0200512 /* in case we had outstanding data */
513 if (!desc->length)
514 clear_bit(WDM_READ, &desc->flags);
Ben Hutchings711c68b2012-02-12 06:00:41 +0000515
516 spin_unlock_irq(&desc->iuspin);
517
Oliver Neukumafba9372008-05-13 17:01:25 +0200518 rv = cntr;
519
520err:
Bjørn Morke8537bd2012-01-16 12:41:48 +0100521 mutex_unlock(&desc->rlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200522 return rv;
523}
524
525static int wdm_flush(struct file *file, fl_owner_t id)
526{
527 struct wdm_device *desc = file->private_data;
528
529 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags));
Bjørn Mork577bd3e2012-05-09 13:53:22 +0200530
531 /* cannot dereference desc->intf if WDM_DISCONNECTING */
532 if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags))
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700533 dev_err(&desc->intf->dev, "Error in flush path: %d\n",
534 desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200535
Oliver Neukumeae68612012-04-27 14:23:54 +0200536 return usb_translate_errors(desc->werr);
Oliver Neukumafba9372008-05-13 17:01:25 +0200537}
538
539static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait)
540{
541 struct wdm_device *desc = file->private_data;
542 unsigned long flags;
543 unsigned int mask = 0;
544
545 spin_lock_irqsave(&desc->iuspin, flags);
546 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
Bjørn Morkd0765ea2012-05-09 13:53:21 +0200547 mask = POLLHUP | POLLERR;
Oliver Neukumafba9372008-05-13 17:01:25 +0200548 spin_unlock_irqrestore(&desc->iuspin, flags);
549 goto desc_out;
550 }
551 if (test_bit(WDM_READ, &desc->flags))
552 mask = POLLIN | POLLRDNORM;
553 if (desc->rerr || desc->werr)
554 mask |= POLLERR;
555 if (!test_bit(WDM_IN_USE, &desc->flags))
556 mask |= POLLOUT | POLLWRNORM;
557 spin_unlock_irqrestore(&desc->iuspin, flags);
558
559 poll_wait(file, &desc->wait, wait);
560
561desc_out:
562 return mask;
563}
564
565static int wdm_open(struct inode *inode, struct file *file)
566{
567 int minor = iminor(inode);
568 int rv = -ENODEV;
569 struct usb_interface *intf;
570 struct wdm_device *desc;
571
572 mutex_lock(&wdm_mutex);
Bjørn Morkb0c13862012-03-06 17:29:21 +0100573 desc = wdm_find_device_by_minor(minor);
574 if (!desc)
Oliver Neukumafba9372008-05-13 17:01:25 +0200575 goto out;
576
Bjørn Morkb0c13862012-03-06 17:29:21 +0100577 intf = desc->intf;
Oliver Neukumafba9372008-05-13 17:01:25 +0200578 if (test_bit(WDM_DISCONNECTING, &desc->flags))
579 goto out;
Oliver Neukumafba9372008-05-13 17:01:25 +0200580 file->private_data = desc;
581
Oliver Neukum17d80d52008-06-24 15:56:10 +0200582 rv = usb_autopm_get_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200583 if (rv < 0) {
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700584 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv);
Oliver Neukumafba9372008-05-13 17:01:25 +0200585 goto out;
586 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200587
Bjørn Morke8537bd2012-01-16 12:41:48 +0100588 /* using write lock to protect desc->count */
589 mutex_lock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200590 if (!desc->count++) {
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200591 desc->werr = 0;
592 desc->rerr = 0;
Oliver Neukum17d80d52008-06-24 15:56:10 +0200593 rv = usb_submit_urb(desc->validity, GFP_KERNEL);
594 if (rv < 0) {
595 desc->count--;
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700596 dev_err(&desc->intf->dev,
597 "Error submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200598 }
599 } else {
600 rv = 0;
601 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100602 mutex_unlock(&desc->wlock);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100603 if (desc->count == 1)
604 desc->manage_power(intf, 1);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200605 usb_autopm_put_interface(desc->intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200606out:
607 mutex_unlock(&wdm_mutex);
608 return rv;
609}
610
611static int wdm_release(struct inode *inode, struct file *file)
612{
613 struct wdm_device *desc = file->private_data;
614
615 mutex_lock(&wdm_mutex);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100616
617 /* using write lock to protect desc->count */
618 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200619 desc->count--;
Bjørn Morke8537bd2012-01-16 12:41:48 +0100620 mutex_unlock(&desc->wlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200621
Oliver Neukumafba9372008-05-13 17:01:25 +0200622 if (!desc->count) {
Bjørn Mork577bd3e2012-05-09 13:53:22 +0200623 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) {
624 dev_dbg(&desc->intf->dev, "wdm_release: cleanup");
625 kill_urbs(desc);
Bjørn Mork3cc36152012-03-06 17:29:22 +0100626 desc->manage_power(desc->intf, 0);
Bjørn Mork577bd3e2012-05-09 13:53:22 +0200627 } else {
628 /* must avoid dev_printk here as desc->intf is invalid */
629 pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__);
Oliver Neukumfccfda62012-04-27 14:36:37 +0200630 cleanup(desc);
Bjørn Mork577bd3e2012-05-09 13:53:22 +0200631 }
Oliver Neukumafba9372008-05-13 17:01:25 +0200632 }
633 mutex_unlock(&wdm_mutex);
634 return 0;
635}
636
637static const struct file_operations wdm_fops = {
638 .owner = THIS_MODULE,
639 .read = wdm_read,
640 .write = wdm_write,
641 .open = wdm_open,
642 .flush = wdm_flush,
643 .release = wdm_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200644 .poll = wdm_poll,
645 .llseek = noop_llseek,
Oliver Neukumafba9372008-05-13 17:01:25 +0200646};
647
648static struct usb_class_driver wdm_class = {
649 .name = "cdc-wdm%d",
650 .fops = &wdm_fops,
651 .minor_base = WDM_MINOR_BASE,
652};
653
654/* --- error handling --- */
655static void wdm_rxwork(struct work_struct *work)
656{
657 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork);
658 unsigned long flags;
659 int rv;
660
661 spin_lock_irqsave(&desc->iuspin, flags);
662 if (test_bit(WDM_DISCONNECTING, &desc->flags)) {
663 spin_unlock_irqrestore(&desc->iuspin, flags);
664 } else {
665 spin_unlock_irqrestore(&desc->iuspin, flags);
666 rv = usb_submit_urb(desc->response, GFP_KERNEL);
667 if (rv < 0 && rv != -EPERM) {
668 spin_lock_irqsave(&desc->iuspin, flags);
669 if (!test_bit(WDM_DISCONNECTING, &desc->flags))
670 schedule_work(&desc->rxwork);
671 spin_unlock_irqrestore(&desc->iuspin, flags);
672 }
673 }
674}
675
676/* --- hotplug --- */
677
Bjørn Mork3cc36152012-03-06 17:29:22 +0100678static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep,
679 u16 bufsize, int (*manage_power)(struct usb_interface *, int))
Oliver Neukumafba9372008-05-13 17:01:25 +0200680{
Bjørn Mork0dffb482012-03-06 17:29:20 +0100681 int rv = -ENOMEM;
Oliver Neukumafba9372008-05-13 17:01:25 +0200682 struct wdm_device *desc;
Oliver Neukumafba9372008-05-13 17:01:25 +0200683
Oliver Neukumafba9372008-05-13 17:01:25 +0200684 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL);
685 if (!desc)
686 goto out;
Bjørn Morkb0c13862012-03-06 17:29:21 +0100687 INIT_LIST_HEAD(&desc->device_list);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100688 mutex_init(&desc->rlock);
689 mutex_init(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200690 spin_lock_init(&desc->iuspin);
691 init_waitqueue_head(&desc->wait);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100692 desc->wMaxCommand = bufsize;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200693 /* this will be expanded and needed in hardware endianness */
Oliver Neukumafba9372008-05-13 17:01:25 +0200694 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber);
695 desc->intf = intf;
696 INIT_WORK(&desc->rxwork, wdm_rxwork);
697
Oliver Neukum052fbc02009-04-20 17:24:49 +0200698 rv = -EINVAL;
Bjørn Mork0dffb482012-03-06 17:29:20 +0100699 if (!usb_endpoint_is_int_in(ep))
Oliver Neukum052fbc02009-04-20 17:24:49 +0200700 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200701
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700702 desc->wMaxPacketSize = usb_endpoint_maxp(ep);
Oliver Neukumafba9372008-05-13 17:01:25 +0200703
704 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
705 if (!desc->orq)
706 goto err;
707 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
708 if (!desc->irq)
709 goto err;
710
711 desc->validity = usb_alloc_urb(0, GFP_KERNEL);
712 if (!desc->validity)
713 goto err;
714
715 desc->response = usb_alloc_urb(0, GFP_KERNEL);
716 if (!desc->response)
717 goto err;
718
719 desc->command = usb_alloc_urb(0, GFP_KERNEL);
720 if (!desc->command)
721 goto err;
722
723 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
724 if (!desc->ubuf)
725 goto err;
726
Bjørn Mork8457d992012-01-16 15:12:00 +0100727 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200728 if (!desc->sbuf)
729 goto err;
730
Bjørn Mork8457d992012-01-16 15:12:00 +0100731 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL);
Oliver Neukumafba9372008-05-13 17:01:25 +0200732 if (!desc->inbuf)
Bjørn Mork8457d992012-01-16 15:12:00 +0100733 goto err;
Oliver Neukumafba9372008-05-13 17:01:25 +0200734
735 usb_fill_int_urb(
736 desc->validity,
737 interface_to_usbdev(intf),
738 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress),
739 desc->sbuf,
740 desc->wMaxPacketSize,
741 wdm_int_callback,
742 desc,
743 ep->bInterval
744 );
Oliver Neukumafba9372008-05-13 17:01:25 +0200745
Bjørn Mork19b85b32012-01-16 15:11:58 +0100746 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
747 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
748 desc->irq->wValue = 0;
749 desc->irq->wIndex = desc->inum;
750 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand);
751
752 usb_fill_control_urb(
753 desc->response,
Bjørn Mork8143a892012-01-16 15:12:01 +0100754 interface_to_usbdev(intf),
Bjørn Mork19b85b32012-01-16 15:11:58 +0100755 /* using common endpoint 0 */
756 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0),
757 (unsigned char *)desc->irq,
758 desc->inbuf,
759 desc->wMaxCommand,
760 wdm_in_callback,
761 desc
762 );
Bjørn Mork19b85b32012-01-16 15:11:58 +0100763
Bjørn Mork3cc36152012-03-06 17:29:22 +0100764 desc->manage_power = manage_power;
765
Bjørn Morkb0c13862012-03-06 17:29:21 +0100766 spin_lock(&wdm_device_list_lock);
767 list_add(&desc->device_list, &wdm_device_list);
768 spin_unlock(&wdm_device_list_lock);
769
Oliver Neukumafba9372008-05-13 17:01:25 +0200770 rv = usb_register_dev(intf, &wdm_class);
Oliver Neukumafba9372008-05-13 17:01:25 +0200771 if (rv < 0)
Bjørn Morkb0c13862012-03-06 17:29:21 +0100772 goto err;
Oliver Neukum052fbc02009-04-20 17:24:49 +0200773 else
Bjørn Mork820c6292012-01-20 04:17:25 +0100774 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev));
Oliver Neukumafba9372008-05-13 17:01:25 +0200775out:
776 return rv;
Oliver Neukumafba9372008-05-13 17:01:25 +0200777err:
Bjørn Mork9ed2cb72012-05-09 13:53:23 +0200778 spin_lock(&wdm_device_list_lock);
779 list_del(&desc->device_list);
780 spin_unlock(&wdm_device_list_lock);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100781 cleanup(desc);
782 return rv;
783}
784
Bjørn Mork3cc36152012-03-06 17:29:22 +0100785static int wdm_manage_power(struct usb_interface *intf, int on)
786{
787 /* need autopm_get/put here to ensure the usbcore sees the new value */
788 int rv = usb_autopm_get_interface(intf);
789 if (rv < 0)
790 goto err;
791
792 intf->needs_remote_wakeup = on;
793 usb_autopm_put_interface(intf);
794err:
795 return rv;
796}
797
Bjørn Mork0dffb482012-03-06 17:29:20 +0100798static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id)
799{
800 int rv = -EINVAL;
801 struct usb_host_interface *iface;
802 struct usb_endpoint_descriptor *ep;
803 struct usb_cdc_dmm_desc *dmhd;
804 u8 *buffer = intf->altsetting->extra;
805 int buflen = intf->altsetting->extralen;
806 u16 maxcom = WDM_DEFAULT_BUFSIZE;
807
808 if (!buffer)
809 goto err;
810 while (buflen > 2) {
811 if (buffer[1] != USB_DT_CS_INTERFACE) {
812 dev_err(&intf->dev, "skipping garbage\n");
813 goto next_desc;
814 }
815
816 switch (buffer[2]) {
817 case USB_CDC_HEADER_TYPE:
818 break;
819 case USB_CDC_DMM_TYPE:
820 dmhd = (struct usb_cdc_dmm_desc *)buffer;
821 maxcom = le16_to_cpu(dmhd->wMaxCommand);
822 dev_dbg(&intf->dev,
823 "Finding maximum buffer length: %d", maxcom);
824 break;
825 default:
826 dev_err(&intf->dev,
827 "Ignoring extra header, type %d, length %d\n",
828 buffer[2], buffer[0]);
829 break;
830 }
831next_desc:
832 buflen -= buffer[0];
833 buffer += buffer[0];
834 }
835
836 iface = intf->cur_altsetting;
837 if (iface->desc.bNumEndpoints != 1)
838 goto err;
839 ep = &iface->endpoint[0].desc;
840
Bjørn Mork3cc36152012-03-06 17:29:22 +0100841 rv = wdm_create(intf, ep, maxcom, &wdm_manage_power);
Bjørn Mork0dffb482012-03-06 17:29:20 +0100842
843err:
Oliver Neukumafba9372008-05-13 17:01:25 +0200844 return rv;
845}
846
Bjørn Mork3cc36152012-03-06 17:29:22 +0100847/**
848 * usb_cdc_wdm_register - register a WDM subdriver
849 * @intf: usb interface the subdriver will associate with
850 * @ep: interrupt endpoint to monitor for notifications
851 * @bufsize: maximum message size to support for read/write
852 *
853 * Create WDM usb class character device and associate it with intf
854 * without binding, allowing another driver to manage the interface.
855 *
856 * The subdriver will manage the given interrupt endpoint exclusively
857 * and will issue control requests referring to the given intf. It
858 * will otherwise avoid interferring, and in particular not do
859 * usb_set_intfdata/usb_get_intfdata on intf.
860 *
861 * The return value is a pointer to the subdriver's struct usb_driver.
862 * The registering driver is responsible for calling this subdriver's
863 * disconnect, suspend, resume, pre_reset and post_reset methods from
864 * its own.
865 */
866struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf,
867 struct usb_endpoint_descriptor *ep,
868 int bufsize,
869 int (*manage_power)(struct usb_interface *, int))
870{
871 int rv = -EINVAL;
872
873 rv = wdm_create(intf, ep, bufsize, manage_power);
874 if (rv < 0)
875 goto err;
876
877 return &wdm_driver;
878err:
879 return ERR_PTR(rv);
880}
881EXPORT_SYMBOL(usb_cdc_wdm_register);
882
Oliver Neukumafba9372008-05-13 17:01:25 +0200883static void wdm_disconnect(struct usb_interface *intf)
884{
885 struct wdm_device *desc;
886 unsigned long flags;
887
888 usb_deregister_dev(intf, &wdm_class);
Bjørn Morkb0c13862012-03-06 17:29:21 +0100889 desc = wdm_find_device(intf);
Oliver Neukumafba9372008-05-13 17:01:25 +0200890 mutex_lock(&wdm_mutex);
Oliver Neukumafba9372008-05-13 17:01:25 +0200891
892 /* the spinlock makes sure no new urbs are generated in the callbacks */
893 spin_lock_irqsave(&desc->iuspin, flags);
894 set_bit(WDM_DISCONNECTING, &desc->flags);
895 set_bit(WDM_READ, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200896 /* to terminate pending flushes */
Oliver Neukumafba9372008-05-13 17:01:25 +0200897 clear_bit(WDM_IN_USE, &desc->flags);
898 spin_unlock_irqrestore(&desc->iuspin, flags);
Bjørn Mork62aaf242012-01-16 15:11:57 +0100899 wake_up_all(&desc->wait);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100900 mutex_lock(&desc->rlock);
901 mutex_lock(&desc->wlock);
Oliver Neukumafba9372008-05-13 17:01:25 +0200902 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100903 cancel_work_sync(&desc->rxwork);
Bjørn Morke8537bd2012-01-16 12:41:48 +0100904 mutex_unlock(&desc->wlock);
905 mutex_unlock(&desc->rlock);
Bjørn Mork9ed2cb72012-05-09 13:53:23 +0200906
907 /* the desc->intf pointer used as list key is now invalid */
908 spin_lock(&wdm_device_list_lock);
909 list_del(&desc->device_list);
910 spin_unlock(&wdm_device_list_lock);
911
Oliver Neukumafba9372008-05-13 17:01:25 +0200912 if (!desc->count)
913 cleanup(desc);
914 mutex_unlock(&wdm_mutex);
915}
916
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100917#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200918static int wdm_suspend(struct usb_interface *intf, pm_message_t message)
919{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100920 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200921 int rv = 0;
922
923 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor);
924
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100925 /* if this is an autosuspend the caller does the locking */
Bjørn Morke8537bd2012-01-16 12:41:48 +0100926 if (!PMSG_IS_AUTO(message)) {
927 mutex_lock(&desc->rlock);
928 mutex_lock(&desc->wlock);
929 }
Oliver Neukum62e66852010-02-27 20:56:22 +0100930 spin_lock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100931
Alan Stern5b1b0b82011-08-19 23:49:48 +0200932 if (PMSG_IS_AUTO(message) &&
Oliver Neukum922a5ea2010-02-27 20:54:59 +0100933 (test_bit(WDM_IN_USE, &desc->flags)
934 || test_bit(WDM_RESPONDING, &desc->flags))) {
Oliver Neukum62e66852010-02-27 20:56:22 +0100935 spin_unlock_irq(&desc->iuspin);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200936 rv = -EBUSY;
937 } else {
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100938
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100939 set_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100940 spin_unlock_irq(&desc->iuspin);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100941 /* callback submits work - order is essential */
Oliver Neukum17d80d52008-06-24 15:56:10 +0200942 kill_urbs(desc);
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100943 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200944 }
Bjørn Morke8537bd2012-01-16 12:41:48 +0100945 if (!PMSG_IS_AUTO(message)) {
946 mutex_unlock(&desc->wlock);
947 mutex_unlock(&desc->rlock);
948 }
Oliver Neukum17d80d52008-06-24 15:56:10 +0200949
950 return rv;
951}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100952#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200953
954static int recover_from_urb_loss(struct wdm_device *desc)
955{
956 int rv = 0;
957
958 if (desc->count) {
959 rv = usb_submit_urb(desc->validity, GFP_NOIO);
960 if (rv < 0)
Greg Kroah-Hartman9908a322008-08-14 09:37:34 -0700961 dev_err(&desc->intf->dev,
962 "Error resume submitting int urb - %d\n", rv);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200963 }
964 return rv;
965}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100966
967#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +0200968static int wdm_resume(struct usb_interface *intf)
969{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100970 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200971 int rv;
972
973 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor);
Oliver Neukum338124c2010-02-27 20:57:12 +0100974
Oliver Neukumbeb1d352010-02-27 20:55:52 +0100975 clear_bit(WDM_SUSPENDING, &desc->flags);
Oliver Neukum62e66852010-02-27 20:56:22 +0100976 rv = recover_from_urb_loss(desc);
Oliver Neukum338124c2010-02-27 20:57:12 +0100977
Oliver Neukum17d80d52008-06-24 15:56:10 +0200978 return rv;
979}
Oliver Neukumd93d16e2010-02-27 20:56:47 +0100980#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +0200981
982static int wdm_pre_reset(struct usb_interface *intf)
983{
Bjørn Morkb0c13862012-03-06 17:29:21 +0100984 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +0200985
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200986 /*
987 * we notify everybody using poll of
988 * an exceptional situation
989 * must be done before recovery lest a spontaneous
990 * message from the device is lost
991 */
992 spin_lock_irq(&desc->iuspin);
Bjørn Mork88044202012-02-10 09:44:08 +0100993 set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */
994 set_bit(WDM_READ, &desc->flags); /* unblock read */
995 clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */
Oliver Neukumd771d8a2011-04-29 14:12:21 +0200996 desc->rerr = -EINTR;
997 spin_unlock_irq(&desc->iuspin);
998 wake_up_all(&desc->wait);
Bjørn Mork88044202012-02-10 09:44:08 +0100999 mutex_lock(&desc->rlock);
1000 mutex_lock(&desc->wlock);
1001 kill_urbs(desc);
1002 cancel_work_sync(&desc->rxwork);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001003 return 0;
1004}
1005
1006static int wdm_post_reset(struct usb_interface *intf)
1007{
Bjørn Morkb0c13862012-03-06 17:29:21 +01001008 struct wdm_device *desc = wdm_find_device(intf);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001009 int rv;
1010
Bjørn Mork88044202012-02-10 09:44:08 +01001011 clear_bit(WDM_RESETTING, &desc->flags);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001012 rv = recover_from_urb_loss(desc);
Bjørn Morke8537bd2012-01-16 12:41:48 +01001013 mutex_unlock(&desc->wlock);
1014 mutex_unlock(&desc->rlock);
Oliver Neukum17d80d52008-06-24 15:56:10 +02001015 return 0;
1016}
1017
Oliver Neukumafba9372008-05-13 17:01:25 +02001018static struct usb_driver wdm_driver = {
1019 .name = "cdc_wdm",
1020 .probe = wdm_probe,
1021 .disconnect = wdm_disconnect,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001022#ifdef CONFIG_PM
Oliver Neukum17d80d52008-06-24 15:56:10 +02001023 .suspend = wdm_suspend,
1024 .resume = wdm_resume,
1025 .reset_resume = wdm_resume,
Oliver Neukumd93d16e2010-02-27 20:56:47 +01001026#endif
Oliver Neukum17d80d52008-06-24 15:56:10 +02001027 .pre_reset = wdm_pre_reset,
1028 .post_reset = wdm_post_reset,
Oliver Neukumafba9372008-05-13 17:01:25 +02001029 .id_table = wdm_ids,
Oliver Neukum17d80d52008-06-24 15:56:10 +02001030 .supports_autosuspend = 1,
Oliver Neukumafba9372008-05-13 17:01:25 +02001031};
1032
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001033module_usb_driver(wdm_driver);
Oliver Neukumafba9372008-05-13 17:01:25 +02001034
1035MODULE_AUTHOR(DRIVER_AUTHOR);
Oliver Neukum87d65e52008-06-19 14:20:18 +02001036MODULE_DESCRIPTION(DRIVER_DESC);
Oliver Neukumafba9372008-05-13 17:01:25 +02001037MODULE_LICENSE("GPL");