| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 1 | /* | 
|  | 2 | * cdc-wdm.c | 
|  | 3 | * | 
|  | 4 | * This driver supports USB CDC WCM Device Management. | 
|  | 5 | * | 
| Oliver Neukum | 052fbc0 | 2009-04-20 17:24:49 +0200 | [diff] [blame] | 6 | * Copyright (c) 2007-2009 Oliver Neukum | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 7 | * | 
|  | 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 Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 18 | #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 Neukum | 87d65e5 | 2008-06-19 14:20:18 +0200 | [diff] [blame] | 30 | #define DRIVER_VERSION "v0.03" | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 31 | #define DRIVER_AUTHOR "Oliver Neukum" | 
| Oliver Neukum | 87d65e5 | 2008-06-19 14:20:18 +0200 | [diff] [blame] | 32 | #define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management" | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 33 |  | 
|  | 34 | static struct usb_device_id wdm_ids[] = { | 
|  | 35 | { | 
|  | 36 | .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS | | 
|  | 37 | USB_DEVICE_ID_MATCH_INT_SUBCLASS, | 
|  | 38 | .bInterfaceClass = USB_CLASS_COMM, | 
|  | 39 | .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM | 
|  | 40 | }, | 
|  | 41 | { } | 
|  | 42 | }; | 
|  | 43 |  | 
| Oliver Neukum | aa5380b | 2008-10-13 14:05:20 +0200 | [diff] [blame] | 44 | MODULE_DEVICE_TABLE (usb, wdm_ids); | 
|  | 45 |  | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 46 | #define WDM_MINOR_BASE	176 | 
|  | 47 |  | 
|  | 48 |  | 
|  | 49 | #define WDM_IN_USE		1 | 
|  | 50 | #define WDM_DISCONNECTING	2 | 
|  | 51 | #define WDM_RESULT		3 | 
|  | 52 | #define WDM_READ		4 | 
|  | 53 | #define WDM_INT_STALL		5 | 
|  | 54 | #define WDM_POLL_RUNNING	6 | 
|  | 55 |  | 
|  | 56 |  | 
|  | 57 | #define WDM_MAX			16 | 
|  | 58 |  | 
|  | 59 |  | 
|  | 60 | static DEFINE_MUTEX(wdm_mutex); | 
|  | 61 |  | 
|  | 62 | /* --- method tables --- */ | 
|  | 63 |  | 
|  | 64 | struct wdm_device { | 
|  | 65 | u8			*inbuf; /* buffer for response */ | 
|  | 66 | u8			*outbuf; /* buffer for command */ | 
|  | 67 | u8			*sbuf; /* buffer for status */ | 
|  | 68 | u8			*ubuf; /* buffer for copy to user space */ | 
|  | 69 |  | 
|  | 70 | struct urb		*command; | 
|  | 71 | struct urb		*response; | 
|  | 72 | struct urb		*validity; | 
|  | 73 | struct usb_interface	*intf; | 
|  | 74 | struct usb_ctrlrequest	*orq; | 
|  | 75 | struct usb_ctrlrequest	*irq; | 
|  | 76 | spinlock_t		iuspin; | 
|  | 77 |  | 
|  | 78 | unsigned long		flags; | 
|  | 79 | u16			bufsize; | 
|  | 80 | u16			wMaxCommand; | 
|  | 81 | u16			wMaxPacketSize; | 
|  | 82 | u16			bMaxPacketSize0; | 
|  | 83 | __le16			inum; | 
|  | 84 | int			reslength; | 
|  | 85 | int			length; | 
|  | 86 | int			read; | 
|  | 87 | int			count; | 
|  | 88 | dma_addr_t		shandle; | 
|  | 89 | dma_addr_t		ihandle; | 
|  | 90 | struct mutex		wlock; | 
|  | 91 | struct mutex		rlock; | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 92 | struct mutex		plock; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 93 | wait_queue_head_t	wait; | 
|  | 94 | struct work_struct	rxwork; | 
|  | 95 | int			werr; | 
|  | 96 | int			rerr; | 
|  | 97 | }; | 
|  | 98 |  | 
|  | 99 | static struct usb_driver wdm_driver; | 
|  | 100 |  | 
|  | 101 | /* --- callbacks --- */ | 
|  | 102 | static void wdm_out_callback(struct urb *urb) | 
|  | 103 | { | 
|  | 104 | struct wdm_device *desc; | 
|  | 105 | desc = urb->context; | 
|  | 106 | spin_lock(&desc->iuspin); | 
|  | 107 | desc->werr = urb->status; | 
|  | 108 | spin_unlock(&desc->iuspin); | 
|  | 109 | clear_bit(WDM_IN_USE, &desc->flags); | 
|  | 110 | kfree(desc->outbuf); | 
|  | 111 | wake_up(&desc->wait); | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | static void wdm_in_callback(struct urb *urb) | 
|  | 115 | { | 
|  | 116 | struct wdm_device *desc = urb->context; | 
|  | 117 | int status = urb->status; | 
|  | 118 |  | 
|  | 119 | spin_lock(&desc->iuspin); | 
|  | 120 |  | 
|  | 121 | if (status) { | 
|  | 122 | switch (status) { | 
|  | 123 | case -ENOENT: | 
|  | 124 | dev_dbg(&desc->intf->dev, | 
|  | 125 | "nonzero urb status received: -ENOENT"); | 
|  | 126 | break; | 
|  | 127 | case -ECONNRESET: | 
|  | 128 | dev_dbg(&desc->intf->dev, | 
|  | 129 | "nonzero urb status received: -ECONNRESET"); | 
|  | 130 | break; | 
|  | 131 | case -ESHUTDOWN: | 
|  | 132 | dev_dbg(&desc->intf->dev, | 
|  | 133 | "nonzero urb status received: -ESHUTDOWN"); | 
|  | 134 | break; | 
|  | 135 | case -EPIPE: | 
| Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 136 | dev_err(&desc->intf->dev, | 
|  | 137 | "nonzero urb status received: -EPIPE\n"); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 138 | break; | 
|  | 139 | default: | 
| Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 140 | dev_err(&desc->intf->dev, | 
|  | 141 | "Unexpected error %d\n", status); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 142 | break; | 
|  | 143 | } | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | desc->rerr = status; | 
|  | 147 | desc->reslength = urb->actual_length; | 
|  | 148 | memmove(desc->ubuf + desc->length, desc->inbuf, desc->reslength); | 
|  | 149 | desc->length += desc->reslength; | 
|  | 150 | wake_up(&desc->wait); | 
|  | 151 |  | 
|  | 152 | set_bit(WDM_READ, &desc->flags); | 
|  | 153 | spin_unlock(&desc->iuspin); | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | static void wdm_int_callback(struct urb *urb) | 
|  | 157 | { | 
|  | 158 | int rv = 0; | 
|  | 159 | int status = urb->status; | 
|  | 160 | struct wdm_device *desc; | 
|  | 161 | struct usb_ctrlrequest *req; | 
|  | 162 | struct usb_cdc_notification *dr; | 
|  | 163 |  | 
|  | 164 | desc = urb->context; | 
|  | 165 | req = desc->irq; | 
|  | 166 | dr = (struct usb_cdc_notification *)desc->sbuf; | 
|  | 167 |  | 
|  | 168 | if (status) { | 
|  | 169 | switch (status) { | 
|  | 170 | case -ESHUTDOWN: | 
|  | 171 | case -ENOENT: | 
|  | 172 | case -ECONNRESET: | 
|  | 173 | return; /* unplug */ | 
|  | 174 | case -EPIPE: | 
|  | 175 | set_bit(WDM_INT_STALL, &desc->flags); | 
| Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 176 | dev_err(&desc->intf->dev, "Stall on int endpoint\n"); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 177 | goto sw; /* halt is cleared in work */ | 
|  | 178 | default: | 
| Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 179 | dev_err(&desc->intf->dev, | 
|  | 180 | "nonzero urb status received: %d\n", status); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 181 | break; | 
|  | 182 | } | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | if (urb->actual_length < sizeof(struct usb_cdc_notification)) { | 
| Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 186 | dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n", | 
|  | 187 | urb->actual_length); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 188 | goto exit; | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | switch (dr->bNotificationType) { | 
|  | 192 | case USB_CDC_NOTIFY_RESPONSE_AVAILABLE: | 
|  | 193 | dev_dbg(&desc->intf->dev, | 
|  | 194 | "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d", | 
|  | 195 | dr->wIndex, dr->wLength); | 
|  | 196 | break; | 
|  | 197 |  | 
|  | 198 | case USB_CDC_NOTIFY_NETWORK_CONNECTION: | 
|  | 199 |  | 
|  | 200 | dev_dbg(&desc->intf->dev, | 
|  | 201 | "NOTIFY_NETWORK_CONNECTION %s network", | 
|  | 202 | dr->wValue ? "connected to" : "disconnected from"); | 
|  | 203 | goto exit; | 
|  | 204 | default: | 
|  | 205 | clear_bit(WDM_POLL_RUNNING, &desc->flags); | 
| Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 206 | dev_err(&desc->intf->dev, | 
|  | 207 | "unknown notification %d received: index %d len %d\n", | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 208 | dr->bNotificationType, dr->wIndex, dr->wLength); | 
|  | 209 | goto exit; | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | req->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE); | 
|  | 213 | req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE; | 
|  | 214 | req->wValue = 0; | 
|  | 215 | req->wIndex = desc->inum; | 
| Oliver Neukum | 87d65e5 | 2008-06-19 14:20:18 +0200 | [diff] [blame] | 216 | req->wLength = cpu_to_le16(desc->wMaxCommand); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 217 |  | 
|  | 218 | usb_fill_control_urb( | 
|  | 219 | desc->response, | 
|  | 220 | interface_to_usbdev(desc->intf), | 
|  | 221 | /* using common endpoint 0 */ | 
|  | 222 | usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0), | 
|  | 223 | (unsigned char *)req, | 
|  | 224 | desc->inbuf, | 
| Oliver Neukum | 87d65e5 | 2008-06-19 14:20:18 +0200 | [diff] [blame] | 225 | desc->wMaxCommand, | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 226 | wdm_in_callback, | 
|  | 227 | desc | 
|  | 228 | ); | 
|  | 229 | desc->response->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | 
|  | 230 | spin_lock(&desc->iuspin); | 
|  | 231 | clear_bit(WDM_READ, &desc->flags); | 
|  | 232 | if (!test_bit(WDM_DISCONNECTING, &desc->flags)) { | 
|  | 233 | rv = usb_submit_urb(desc->response, GFP_ATOMIC); | 
|  | 234 | dev_dbg(&desc->intf->dev, "%s: usb_submit_urb %d", | 
|  | 235 | __func__, rv); | 
|  | 236 | } | 
|  | 237 | spin_unlock(&desc->iuspin); | 
|  | 238 | if (rv < 0) { | 
|  | 239 | if (rv == -EPERM) | 
|  | 240 | return; | 
|  | 241 | if (rv == -ENOMEM) { | 
|  | 242 | sw: | 
|  | 243 | rv = schedule_work(&desc->rxwork); | 
|  | 244 | if (rv) | 
| Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 245 | dev_err(&desc->intf->dev, | 
|  | 246 | "Cannot schedule work\n"); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 247 | } | 
|  | 248 | } | 
|  | 249 | exit: | 
|  | 250 | rv = usb_submit_urb(urb, GFP_ATOMIC); | 
|  | 251 | if (rv) | 
| Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 252 | dev_err(&desc->intf->dev, | 
|  | 253 | "%s - usb_submit_urb failed with result %d\n", | 
|  | 254 | __func__, rv); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 255 |  | 
|  | 256 | } | 
|  | 257 |  | 
|  | 258 | static void kill_urbs(struct wdm_device *desc) | 
|  | 259 | { | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 260 | /* the order here is essential */ | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 261 | usb_kill_urb(desc->command); | 
|  | 262 | usb_kill_urb(desc->validity); | 
|  | 263 | usb_kill_urb(desc->response); | 
|  | 264 | } | 
|  | 265 |  | 
|  | 266 | static void free_urbs(struct wdm_device *desc) | 
|  | 267 | { | 
|  | 268 | usb_free_urb(desc->validity); | 
|  | 269 | usb_free_urb(desc->response); | 
|  | 270 | usb_free_urb(desc->command); | 
|  | 271 | } | 
|  | 272 |  | 
|  | 273 | static void cleanup(struct wdm_device *desc) | 
|  | 274 | { | 
|  | 275 | usb_buffer_free(interface_to_usbdev(desc->intf), | 
|  | 276 | desc->wMaxPacketSize, | 
|  | 277 | desc->sbuf, | 
|  | 278 | desc->validity->transfer_dma); | 
|  | 279 | usb_buffer_free(interface_to_usbdev(desc->intf), | 
| Oliver Neukum | 87d65e5 | 2008-06-19 14:20:18 +0200 | [diff] [blame] | 280 | desc->wMaxCommand, | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 281 | desc->inbuf, | 
|  | 282 | desc->response->transfer_dma); | 
|  | 283 | kfree(desc->orq); | 
|  | 284 | kfree(desc->irq); | 
|  | 285 | kfree(desc->ubuf); | 
|  | 286 | free_urbs(desc); | 
|  | 287 | kfree(desc); | 
|  | 288 | } | 
|  | 289 |  | 
|  | 290 | static ssize_t wdm_write | 
|  | 291 | (struct file *file, const char __user *buffer, size_t count, loff_t *ppos) | 
|  | 292 | { | 
|  | 293 | u8 *buf; | 
|  | 294 | int rv = -EMSGSIZE, r, we; | 
|  | 295 | struct wdm_device *desc = file->private_data; | 
|  | 296 | struct usb_ctrlrequest *req; | 
|  | 297 |  | 
|  | 298 | if (count > desc->wMaxCommand) | 
|  | 299 | count = desc->wMaxCommand; | 
|  | 300 |  | 
|  | 301 | spin_lock_irq(&desc->iuspin); | 
|  | 302 | we = desc->werr; | 
|  | 303 | desc->werr = 0; | 
|  | 304 | spin_unlock_irq(&desc->iuspin); | 
|  | 305 | if (we < 0) | 
|  | 306 | return -EIO; | 
|  | 307 |  | 
|  | 308 | r = mutex_lock_interruptible(&desc->wlock); /* concurrent writes */ | 
|  | 309 | rv = -ERESTARTSYS; | 
|  | 310 | if (r) | 
|  | 311 | goto outnl; | 
|  | 312 |  | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 313 | r = usb_autopm_get_interface(desc->intf); | 
|  | 314 | if (r < 0) | 
|  | 315 | goto outnp; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 316 | r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE, | 
|  | 317 | &desc->flags)); | 
|  | 318 | if (r < 0) | 
|  | 319 | goto out; | 
|  | 320 |  | 
|  | 321 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { | 
|  | 322 | rv = -ENODEV; | 
|  | 323 | goto out; | 
|  | 324 | } | 
|  | 325 |  | 
|  | 326 | desc->outbuf = buf = kmalloc(count, GFP_KERNEL); | 
|  | 327 | if (!buf) { | 
|  | 328 | rv = -ENOMEM; | 
|  | 329 | goto out; | 
|  | 330 | } | 
|  | 331 |  | 
|  | 332 | r = copy_from_user(buf, buffer, count); | 
|  | 333 | if (r > 0) { | 
|  | 334 | kfree(buf); | 
|  | 335 | rv = -EFAULT; | 
|  | 336 | goto out; | 
|  | 337 | } | 
|  | 338 |  | 
|  | 339 | req = desc->orq; | 
|  | 340 | usb_fill_control_urb( | 
|  | 341 | desc->command, | 
|  | 342 | interface_to_usbdev(desc->intf), | 
|  | 343 | /* using common endpoint 0 */ | 
|  | 344 | usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0), | 
|  | 345 | (unsigned char *)req, | 
|  | 346 | buf, | 
|  | 347 | count, | 
|  | 348 | wdm_out_callback, | 
|  | 349 | desc | 
|  | 350 | ); | 
|  | 351 |  | 
|  | 352 | req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS | | 
|  | 353 | USB_RECIP_INTERFACE); | 
|  | 354 | req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND; | 
|  | 355 | req->wValue = 0; | 
|  | 356 | req->wIndex = desc->inum; | 
|  | 357 | req->wLength = cpu_to_le16(count); | 
|  | 358 | set_bit(WDM_IN_USE, &desc->flags); | 
|  | 359 |  | 
|  | 360 | rv = usb_submit_urb(desc->command, GFP_KERNEL); | 
|  | 361 | if (rv < 0) { | 
|  | 362 | kfree(buf); | 
|  | 363 | clear_bit(WDM_IN_USE, &desc->flags); | 
| Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 364 | dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 365 | } else { | 
|  | 366 | dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d", | 
|  | 367 | req->wIndex); | 
|  | 368 | } | 
|  | 369 | out: | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 370 | usb_autopm_put_interface(desc->intf); | 
|  | 371 | outnp: | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 372 | mutex_unlock(&desc->wlock); | 
|  | 373 | outnl: | 
|  | 374 | return rv < 0 ? rv : count; | 
|  | 375 | } | 
|  | 376 |  | 
|  | 377 | static ssize_t wdm_read | 
|  | 378 | (struct file *file, char __user *buffer, size_t count, loff_t *ppos) | 
|  | 379 | { | 
|  | 380 | int rv, cntr; | 
|  | 381 | int i = 0; | 
|  | 382 | struct wdm_device *desc = file->private_data; | 
|  | 383 |  | 
|  | 384 |  | 
|  | 385 | rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */ | 
|  | 386 | if (rv < 0) | 
|  | 387 | return -ERESTARTSYS; | 
|  | 388 |  | 
|  | 389 | if (desc->length == 0) { | 
|  | 390 | desc->read = 0; | 
|  | 391 | retry: | 
|  | 392 | i++; | 
|  | 393 | rv = wait_event_interruptible(desc->wait, | 
|  | 394 | test_bit(WDM_READ, &desc->flags)); | 
|  | 395 |  | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 396 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { | 
|  | 397 | rv = -ENODEV; | 
|  | 398 | goto err; | 
|  | 399 | } | 
|  | 400 | usb_mark_last_busy(interface_to_usbdev(desc->intf)); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 401 | if (rv < 0) { | 
|  | 402 | rv = -ERESTARTSYS; | 
|  | 403 | goto err; | 
|  | 404 | } | 
|  | 405 |  | 
|  | 406 | spin_lock_irq(&desc->iuspin); | 
|  | 407 |  | 
|  | 408 | if (desc->rerr) { /* read completed, error happened */ | 
|  | 409 | int t = desc->rerr; | 
|  | 410 | desc->rerr = 0; | 
|  | 411 | spin_unlock_irq(&desc->iuspin); | 
| Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 412 | dev_err(&desc->intf->dev, | 
|  | 413 | "reading had resulted in %d\n", t); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 414 | rv = -EIO; | 
|  | 415 | goto err; | 
|  | 416 | } | 
|  | 417 | /* | 
|  | 418 | * recheck whether we've lost the race | 
|  | 419 | * against the completion handler | 
|  | 420 | */ | 
|  | 421 | if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */ | 
|  | 422 | spin_unlock_irq(&desc->iuspin); | 
|  | 423 | goto retry; | 
|  | 424 | } | 
|  | 425 | if (!desc->reslength) { /* zero length read */ | 
|  | 426 | spin_unlock_irq(&desc->iuspin); | 
|  | 427 | goto retry; | 
|  | 428 | } | 
|  | 429 | clear_bit(WDM_READ, &desc->flags); | 
|  | 430 | spin_unlock_irq(&desc->iuspin); | 
|  | 431 | } | 
|  | 432 |  | 
|  | 433 | cntr = count > desc->length ? desc->length : count; | 
|  | 434 | rv = copy_to_user(buffer, desc->ubuf, cntr); | 
|  | 435 | if (rv > 0) { | 
|  | 436 | rv = -EFAULT; | 
|  | 437 | goto err; | 
|  | 438 | } | 
|  | 439 |  | 
|  | 440 | for (i = 0; i < desc->length - cntr; i++) | 
|  | 441 | desc->ubuf[i] = desc->ubuf[i + cntr]; | 
|  | 442 |  | 
|  | 443 | desc->length -= cntr; | 
| Oliver Neukum | 87d65e5 | 2008-06-19 14:20:18 +0200 | [diff] [blame] | 444 | /* in case we had outstanding data */ | 
|  | 445 | if (!desc->length) | 
|  | 446 | clear_bit(WDM_READ, &desc->flags); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 447 | rv = cntr; | 
|  | 448 |  | 
|  | 449 | err: | 
|  | 450 | mutex_unlock(&desc->rlock); | 
|  | 451 | if (rv < 0) | 
| Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 452 | dev_err(&desc->intf->dev, "wdm_read: exit error\n"); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 453 | return rv; | 
|  | 454 | } | 
|  | 455 |  | 
|  | 456 | static int wdm_flush(struct file *file, fl_owner_t id) | 
|  | 457 | { | 
|  | 458 | struct wdm_device *desc = file->private_data; | 
|  | 459 |  | 
|  | 460 | wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags)); | 
|  | 461 | if (desc->werr < 0) | 
| Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 462 | dev_err(&desc->intf->dev, "Error in flush path: %d\n", | 
|  | 463 | desc->werr); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 464 |  | 
|  | 465 | return desc->werr; | 
|  | 466 | } | 
|  | 467 |  | 
|  | 468 | static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait) | 
|  | 469 | { | 
|  | 470 | struct wdm_device *desc = file->private_data; | 
|  | 471 | unsigned long flags; | 
|  | 472 | unsigned int mask = 0; | 
|  | 473 |  | 
|  | 474 | spin_lock_irqsave(&desc->iuspin, flags); | 
|  | 475 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { | 
|  | 476 | mask = POLLERR; | 
|  | 477 | spin_unlock_irqrestore(&desc->iuspin, flags); | 
|  | 478 | goto desc_out; | 
|  | 479 | } | 
|  | 480 | if (test_bit(WDM_READ, &desc->flags)) | 
|  | 481 | mask = POLLIN | POLLRDNORM; | 
|  | 482 | if (desc->rerr || desc->werr) | 
|  | 483 | mask |= POLLERR; | 
|  | 484 | if (!test_bit(WDM_IN_USE, &desc->flags)) | 
|  | 485 | mask |= POLLOUT | POLLWRNORM; | 
|  | 486 | spin_unlock_irqrestore(&desc->iuspin, flags); | 
|  | 487 |  | 
|  | 488 | poll_wait(file, &desc->wait, wait); | 
|  | 489 |  | 
|  | 490 | desc_out: | 
|  | 491 | return mask; | 
|  | 492 | } | 
|  | 493 |  | 
|  | 494 | static int wdm_open(struct inode *inode, struct file *file) | 
|  | 495 | { | 
|  | 496 | int minor = iminor(inode); | 
|  | 497 | int rv = -ENODEV; | 
|  | 498 | struct usb_interface *intf; | 
|  | 499 | struct wdm_device *desc; | 
|  | 500 |  | 
|  | 501 | mutex_lock(&wdm_mutex); | 
|  | 502 | intf = usb_find_interface(&wdm_driver, minor); | 
|  | 503 | if (!intf) | 
|  | 504 | goto out; | 
|  | 505 |  | 
|  | 506 | desc = usb_get_intfdata(intf); | 
|  | 507 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) | 
|  | 508 | goto out; | 
|  | 509 |  | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 510 | ; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 511 | file->private_data = desc; | 
|  | 512 |  | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 513 | rv = usb_autopm_get_interface(desc->intf); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 514 | if (rv < 0) { | 
| Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 515 | dev_err(&desc->intf->dev, "Error autopm - %d\n", rv); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 516 | goto out; | 
|  | 517 | } | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 518 | intf->needs_remote_wakeup = 1; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 519 |  | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 520 | mutex_lock(&desc->plock); | 
|  | 521 | if (!desc->count++) { | 
|  | 522 | rv = usb_submit_urb(desc->validity, GFP_KERNEL); | 
|  | 523 | if (rv < 0) { | 
|  | 524 | desc->count--; | 
| Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 525 | dev_err(&desc->intf->dev, | 
|  | 526 | "Error submitting int urb - %d\n", rv); | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 527 | } | 
|  | 528 | } else { | 
|  | 529 | rv = 0; | 
|  | 530 | } | 
|  | 531 | mutex_unlock(&desc->plock); | 
|  | 532 | usb_autopm_put_interface(desc->intf); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 533 | out: | 
|  | 534 | mutex_unlock(&wdm_mutex); | 
|  | 535 | return rv; | 
|  | 536 | } | 
|  | 537 |  | 
|  | 538 | static int wdm_release(struct inode *inode, struct file *file) | 
|  | 539 | { | 
|  | 540 | struct wdm_device *desc = file->private_data; | 
|  | 541 |  | 
|  | 542 | mutex_lock(&wdm_mutex); | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 543 | mutex_lock(&desc->plock); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 544 | desc->count--; | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 545 | mutex_unlock(&desc->plock); | 
|  | 546 |  | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 547 | if (!desc->count) { | 
|  | 548 | dev_dbg(&desc->intf->dev, "wdm_release: cleanup"); | 
|  | 549 | kill_urbs(desc); | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 550 | if (!test_bit(WDM_DISCONNECTING, &desc->flags)) | 
|  | 551 | desc->intf->needs_remote_wakeup = 0; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 552 | } | 
|  | 553 | mutex_unlock(&wdm_mutex); | 
|  | 554 | return 0; | 
|  | 555 | } | 
|  | 556 |  | 
|  | 557 | static const struct file_operations wdm_fops = { | 
|  | 558 | .owner =	THIS_MODULE, | 
|  | 559 | .read =		wdm_read, | 
|  | 560 | .write =	wdm_write, | 
|  | 561 | .open =		wdm_open, | 
|  | 562 | .flush =	wdm_flush, | 
|  | 563 | .release =	wdm_release, | 
|  | 564 | .poll =		wdm_poll | 
|  | 565 | }; | 
|  | 566 |  | 
|  | 567 | static struct usb_class_driver wdm_class = { | 
|  | 568 | .name =		"cdc-wdm%d", | 
|  | 569 | .fops =		&wdm_fops, | 
|  | 570 | .minor_base =	WDM_MINOR_BASE, | 
|  | 571 | }; | 
|  | 572 |  | 
|  | 573 | /* --- error handling --- */ | 
|  | 574 | static void wdm_rxwork(struct work_struct *work) | 
|  | 575 | { | 
|  | 576 | struct wdm_device *desc = container_of(work, struct wdm_device, rxwork); | 
|  | 577 | unsigned long flags; | 
|  | 578 | int rv; | 
|  | 579 |  | 
|  | 580 | spin_lock_irqsave(&desc->iuspin, flags); | 
|  | 581 | if (test_bit(WDM_DISCONNECTING, &desc->flags)) { | 
|  | 582 | spin_unlock_irqrestore(&desc->iuspin, flags); | 
|  | 583 | } else { | 
|  | 584 | spin_unlock_irqrestore(&desc->iuspin, flags); | 
|  | 585 | rv = usb_submit_urb(desc->response, GFP_KERNEL); | 
|  | 586 | if (rv < 0 && rv != -EPERM) { | 
|  | 587 | spin_lock_irqsave(&desc->iuspin, flags); | 
|  | 588 | if (!test_bit(WDM_DISCONNECTING, &desc->flags)) | 
|  | 589 | schedule_work(&desc->rxwork); | 
|  | 590 | spin_unlock_irqrestore(&desc->iuspin, flags); | 
|  | 591 | } | 
|  | 592 | } | 
|  | 593 | } | 
|  | 594 |  | 
|  | 595 | /* --- hotplug --- */ | 
|  | 596 |  | 
|  | 597 | static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id) | 
|  | 598 | { | 
|  | 599 | int rv = -EINVAL; | 
|  | 600 | struct usb_device *udev = interface_to_usbdev(intf); | 
|  | 601 | struct wdm_device *desc; | 
|  | 602 | struct usb_host_interface *iface; | 
|  | 603 | struct usb_endpoint_descriptor *ep; | 
|  | 604 | struct usb_cdc_dmm_desc *dmhd; | 
|  | 605 | u8 *buffer = intf->altsetting->extra; | 
|  | 606 | int buflen = intf->altsetting->extralen; | 
|  | 607 | u16 maxcom = 0; | 
|  | 608 |  | 
|  | 609 | if (!buffer) | 
|  | 610 | goto out; | 
|  | 611 |  | 
| Oliver Neukum | 052fbc0 | 2009-04-20 17:24:49 +0200 | [diff] [blame] | 612 | while (buflen > 2) { | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 613 | if (buffer [1] != USB_DT_CS_INTERFACE) { | 
| Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 614 | dev_err(&intf->dev, "skipping garbage\n"); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 615 | goto next_desc; | 
|  | 616 | } | 
|  | 617 |  | 
|  | 618 | switch (buffer [2]) { | 
|  | 619 | case USB_CDC_HEADER_TYPE: | 
|  | 620 | break; | 
|  | 621 | case USB_CDC_DMM_TYPE: | 
|  | 622 | dmhd = (struct usb_cdc_dmm_desc *)buffer; | 
|  | 623 | maxcom = le16_to_cpu(dmhd->wMaxCommand); | 
|  | 624 | dev_dbg(&intf->dev, | 
|  | 625 | "Finding maximum buffer length: %d", maxcom); | 
|  | 626 | break; | 
|  | 627 | default: | 
| Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 628 | dev_err(&intf->dev, | 
|  | 629 | "Ignoring extra header, type %d, length %d\n", | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 630 | buffer[2], buffer[0]); | 
|  | 631 | break; | 
|  | 632 | } | 
|  | 633 | next_desc: | 
|  | 634 | buflen -= buffer[0]; | 
|  | 635 | buffer += buffer[0]; | 
|  | 636 | } | 
|  | 637 |  | 
|  | 638 | rv = -ENOMEM; | 
|  | 639 | desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL); | 
|  | 640 | if (!desc) | 
|  | 641 | goto out; | 
|  | 642 | mutex_init(&desc->wlock); | 
|  | 643 | mutex_init(&desc->rlock); | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 644 | mutex_init(&desc->plock); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 645 | spin_lock_init(&desc->iuspin); | 
|  | 646 | init_waitqueue_head(&desc->wait); | 
|  | 647 | desc->wMaxCommand = maxcom; | 
| Oliver Neukum | 052fbc0 | 2009-04-20 17:24:49 +0200 | [diff] [blame] | 648 | /* this will be expanded and needed in hardware endianness */ | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 649 | desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber); | 
|  | 650 | desc->intf = intf; | 
|  | 651 | INIT_WORK(&desc->rxwork, wdm_rxwork); | 
|  | 652 |  | 
| Oliver Neukum | 052fbc0 | 2009-04-20 17:24:49 +0200 | [diff] [blame] | 653 | rv = -EINVAL; | 
|  | 654 | iface = intf->cur_altsetting; | 
|  | 655 | if (iface->desc.bNumEndpoints != 1) | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 656 | goto err; | 
| Oliver Neukum | 052fbc0 | 2009-04-20 17:24:49 +0200 | [diff] [blame] | 657 | ep = &iface->endpoint[0].desc; | 
|  | 658 | if (!ep || !usb_endpoint_is_int_in(ep)) | 
|  | 659 | goto err; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 660 |  | 
| Al Viro | fa4144b | 2008-06-02 10:59:02 +0100 | [diff] [blame] | 661 | desc->wMaxPacketSize = le16_to_cpu(ep->wMaxPacketSize); | 
|  | 662 | desc->bMaxPacketSize0 = udev->descriptor.bMaxPacketSize0; | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 663 |  | 
|  | 664 | desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); | 
|  | 665 | if (!desc->orq) | 
|  | 666 | goto err; | 
|  | 667 | desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); | 
|  | 668 | if (!desc->irq) | 
|  | 669 | goto err; | 
|  | 670 |  | 
|  | 671 | desc->validity = usb_alloc_urb(0, GFP_KERNEL); | 
|  | 672 | if (!desc->validity) | 
|  | 673 | goto err; | 
|  | 674 |  | 
|  | 675 | desc->response = usb_alloc_urb(0, GFP_KERNEL); | 
|  | 676 | if (!desc->response) | 
|  | 677 | goto err; | 
|  | 678 |  | 
|  | 679 | desc->command = usb_alloc_urb(0, GFP_KERNEL); | 
|  | 680 | if (!desc->command) | 
|  | 681 | goto err; | 
|  | 682 |  | 
|  | 683 | desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL); | 
|  | 684 | if (!desc->ubuf) | 
|  | 685 | goto err; | 
|  | 686 |  | 
|  | 687 | desc->sbuf = usb_buffer_alloc(interface_to_usbdev(intf), | 
|  | 688 | desc->wMaxPacketSize, | 
|  | 689 | GFP_KERNEL, | 
|  | 690 | &desc->validity->transfer_dma); | 
|  | 691 | if (!desc->sbuf) | 
|  | 692 | goto err; | 
|  | 693 |  | 
|  | 694 | desc->inbuf = usb_buffer_alloc(interface_to_usbdev(intf), | 
|  | 695 | desc->bMaxPacketSize0, | 
|  | 696 | GFP_KERNEL, | 
|  | 697 | &desc->response->transfer_dma); | 
|  | 698 | if (!desc->inbuf) | 
|  | 699 | goto err2; | 
|  | 700 |  | 
|  | 701 | usb_fill_int_urb( | 
|  | 702 | desc->validity, | 
|  | 703 | interface_to_usbdev(intf), | 
|  | 704 | usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress), | 
|  | 705 | desc->sbuf, | 
|  | 706 | desc->wMaxPacketSize, | 
|  | 707 | wdm_int_callback, | 
|  | 708 | desc, | 
|  | 709 | ep->bInterval | 
|  | 710 | ); | 
|  | 711 | desc->validity->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | 
|  | 712 |  | 
|  | 713 | usb_set_intfdata(intf, desc); | 
|  | 714 | rv = usb_register_dev(intf, &wdm_class); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 715 | if (rv < 0) | 
| Oliver Neukum | 052fbc0 | 2009-04-20 17:24:49 +0200 | [diff] [blame] | 716 | goto err3; | 
|  | 717 | else | 
|  | 718 | dev_info(&intf->dev, "cdc-wdm%d: USB WDM device\n", | 
|  | 719 | intf->minor - WDM_MINOR_BASE); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 720 | out: | 
|  | 721 | return rv; | 
| Oliver Neukum | 052fbc0 | 2009-04-20 17:24:49 +0200 | [diff] [blame] | 722 | err3: | 
|  | 723 | usb_set_intfdata(intf, NULL); | 
|  | 724 | usb_buffer_free(interface_to_usbdev(desc->intf), | 
|  | 725 | desc->bMaxPacketSize0, | 
|  | 726 | desc->inbuf, | 
|  | 727 | desc->response->transfer_dma); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 728 | err2: | 
|  | 729 | usb_buffer_free(interface_to_usbdev(desc->intf), | 
|  | 730 | desc->wMaxPacketSize, | 
|  | 731 | desc->sbuf, | 
|  | 732 | desc->validity->transfer_dma); | 
|  | 733 | err: | 
|  | 734 | free_urbs(desc); | 
|  | 735 | kfree(desc->ubuf); | 
|  | 736 | kfree(desc->orq); | 
|  | 737 | kfree(desc->irq); | 
|  | 738 | kfree(desc); | 
|  | 739 | return rv; | 
|  | 740 | } | 
|  | 741 |  | 
|  | 742 | static void wdm_disconnect(struct usb_interface *intf) | 
|  | 743 | { | 
|  | 744 | struct wdm_device *desc; | 
|  | 745 | unsigned long flags; | 
|  | 746 |  | 
|  | 747 | usb_deregister_dev(intf, &wdm_class); | 
|  | 748 | mutex_lock(&wdm_mutex); | 
|  | 749 | desc = usb_get_intfdata(intf); | 
|  | 750 |  | 
|  | 751 | /* the spinlock makes sure no new urbs are generated in the callbacks */ | 
|  | 752 | spin_lock_irqsave(&desc->iuspin, flags); | 
|  | 753 | set_bit(WDM_DISCONNECTING, &desc->flags); | 
|  | 754 | set_bit(WDM_READ, &desc->flags); | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 755 | /* to terminate pending flushes */ | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 756 | clear_bit(WDM_IN_USE, &desc->flags); | 
|  | 757 | spin_unlock_irqrestore(&desc->iuspin, flags); | 
|  | 758 | cancel_work_sync(&desc->rxwork); | 
|  | 759 | kill_urbs(desc); | 
|  | 760 | wake_up_all(&desc->wait); | 
|  | 761 | if (!desc->count) | 
|  | 762 | cleanup(desc); | 
|  | 763 | mutex_unlock(&wdm_mutex); | 
|  | 764 | } | 
|  | 765 |  | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 766 | static int wdm_suspend(struct usb_interface *intf, pm_message_t message) | 
|  | 767 | { | 
|  | 768 | struct wdm_device *desc = usb_get_intfdata(intf); | 
|  | 769 | int rv = 0; | 
|  | 770 |  | 
|  | 771 | dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor); | 
|  | 772 |  | 
|  | 773 | mutex_lock(&desc->plock); | 
| Oliver Neukum | 3575858 | 2008-07-01 19:10:08 +0200 | [diff] [blame] | 774 | #ifdef CONFIG_PM | 
| Alan Stern | 65bfd29 | 2008-11-25 16:39:18 -0500 | [diff] [blame] | 775 | if ((message.event & PM_EVENT_AUTO) && | 
|  | 776 | test_bit(WDM_IN_USE, &desc->flags)) { | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 777 | rv = -EBUSY; | 
|  | 778 | } else { | 
| Oliver Neukum | 3575858 | 2008-07-01 19:10:08 +0200 | [diff] [blame] | 779 | #endif | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 780 | cancel_work_sync(&desc->rxwork); | 
|  | 781 | kill_urbs(desc); | 
| Oliver Neukum | 3575858 | 2008-07-01 19:10:08 +0200 | [diff] [blame] | 782 | #ifdef CONFIG_PM | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 783 | } | 
| Oliver Neukum | 3575858 | 2008-07-01 19:10:08 +0200 | [diff] [blame] | 784 | #endif | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 785 | mutex_unlock(&desc->plock); | 
|  | 786 |  | 
|  | 787 | return rv; | 
|  | 788 | } | 
|  | 789 |  | 
|  | 790 | static int recover_from_urb_loss(struct wdm_device *desc) | 
|  | 791 | { | 
|  | 792 | int rv = 0; | 
|  | 793 |  | 
|  | 794 | if (desc->count) { | 
|  | 795 | rv = usb_submit_urb(desc->validity, GFP_NOIO); | 
|  | 796 | if (rv < 0) | 
| Greg Kroah-Hartman | 9908a32 | 2008-08-14 09:37:34 -0700 | [diff] [blame] | 797 | dev_err(&desc->intf->dev, | 
|  | 798 | "Error resume submitting int urb - %d\n", rv); | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 799 | } | 
|  | 800 | return rv; | 
|  | 801 | } | 
|  | 802 | static int wdm_resume(struct usb_interface *intf) | 
|  | 803 | { | 
|  | 804 | struct wdm_device *desc = usb_get_intfdata(intf); | 
|  | 805 | int rv; | 
|  | 806 |  | 
|  | 807 | dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor); | 
|  | 808 | mutex_lock(&desc->plock); | 
|  | 809 | rv = recover_from_urb_loss(desc); | 
|  | 810 | mutex_unlock(&desc->plock); | 
|  | 811 | return rv; | 
|  | 812 | } | 
|  | 813 |  | 
|  | 814 | static int wdm_pre_reset(struct usb_interface *intf) | 
|  | 815 | { | 
|  | 816 | struct wdm_device *desc = usb_get_intfdata(intf); | 
|  | 817 |  | 
|  | 818 | mutex_lock(&desc->plock); | 
|  | 819 | return 0; | 
|  | 820 | } | 
|  | 821 |  | 
|  | 822 | static int wdm_post_reset(struct usb_interface *intf) | 
|  | 823 | { | 
|  | 824 | struct wdm_device *desc = usb_get_intfdata(intf); | 
|  | 825 | int rv; | 
|  | 826 |  | 
|  | 827 | rv = recover_from_urb_loss(desc); | 
|  | 828 | mutex_unlock(&desc->plock); | 
|  | 829 | return 0; | 
|  | 830 | } | 
|  | 831 |  | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 832 | static struct usb_driver wdm_driver = { | 
|  | 833 | .name =		"cdc_wdm", | 
|  | 834 | .probe =	wdm_probe, | 
|  | 835 | .disconnect =	wdm_disconnect, | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 836 | .suspend =	wdm_suspend, | 
|  | 837 | .resume =	wdm_resume, | 
|  | 838 | .reset_resume =	wdm_resume, | 
|  | 839 | .pre_reset =	wdm_pre_reset, | 
|  | 840 | .post_reset =	wdm_post_reset, | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 841 | .id_table =	wdm_ids, | 
| Oliver Neukum | 17d80d5 | 2008-06-24 15:56:10 +0200 | [diff] [blame] | 842 | .supports_autosuspend = 1, | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 843 | }; | 
|  | 844 |  | 
|  | 845 | /* --- low level module stuff --- */ | 
|  | 846 |  | 
|  | 847 | static int __init wdm_init(void) | 
|  | 848 | { | 
|  | 849 | int rv; | 
|  | 850 |  | 
|  | 851 | rv = usb_register(&wdm_driver); | 
|  | 852 |  | 
|  | 853 | return rv; | 
|  | 854 | } | 
|  | 855 |  | 
|  | 856 | static void __exit wdm_exit(void) | 
|  | 857 | { | 
|  | 858 | usb_deregister(&wdm_driver); | 
|  | 859 | } | 
|  | 860 |  | 
|  | 861 | module_init(wdm_init); | 
|  | 862 | module_exit(wdm_exit); | 
|  | 863 |  | 
|  | 864 | MODULE_AUTHOR(DRIVER_AUTHOR); | 
| Oliver Neukum | 87d65e5 | 2008-06-19 14:20:18 +0200 | [diff] [blame] | 865 | MODULE_DESCRIPTION(DRIVER_DESC); | 
| Oliver Neukum | afba937 | 2008-05-13 17:01:25 +0200 | [diff] [blame] | 866 | MODULE_LICENSE("GPL"); |