1 /* 2 * cdc-wdm.c 3 * 4 * This driver supports USB CDC WCM Device Management. 5 * 6 * Copyright (c) 2007-2009 Oliver Neukum 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/ioctl.h> 17 #include <linux/slab.h> 18 #include <linux/module.h> 19 #include <linux/mutex.h> 20 #include <linux/uaccess.h> 21 #include <linux/bitops.h> 22 #include <linux/poll.h> 23 #include <linux/usb.h> 24 #include <linux/usb/cdc.h> 25 #include <asm/byteorder.h> 26 #include <asm/unaligned.h> 27 #include <linux/usb/cdc-wdm.h> 28 29 #define DRIVER_AUTHOR "Oliver Neukum" 30 #define DRIVER_DESC "USB Abstract Control Model driver for USB WCM Device Management" 31 32 static const struct usb_device_id wdm_ids[] = { 33 { 34 .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS | 35 USB_DEVICE_ID_MATCH_INT_SUBCLASS, 36 .bInterfaceClass = USB_CLASS_COMM, 37 .bInterfaceSubClass = USB_CDC_SUBCLASS_DMM 38 }, 39 { } 40 }; 41 42 MODULE_DEVICE_TABLE (usb, wdm_ids); 43 44 #define WDM_MINOR_BASE 176 45 46 47 #define WDM_IN_USE 1 48 #define WDM_DISCONNECTING 2 49 #define WDM_RESULT 3 50 #define WDM_READ 4 51 #define WDM_INT_STALL 5 52 #define WDM_POLL_RUNNING 6 53 #define WDM_RESPONDING 7 54 #define WDM_SUSPENDING 8 55 #define WDM_RESETTING 9 56 #define WDM_OVERFLOW 10 57 58 #define WDM_MAX 16 59 60 /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */ 61 #define WDM_DEFAULT_BUFSIZE 256 62 63 static DEFINE_MUTEX(wdm_mutex); 64 static DEFINE_SPINLOCK(wdm_device_list_lock); 65 static LIST_HEAD(wdm_device_list); 66 67 /* --- method tables --- */ 68 69 struct wdm_device { 70 u8 *inbuf; /* buffer for response */ 71 u8 *outbuf; /* buffer for command */ 72 u8 *sbuf; /* buffer for status */ 73 u8 *ubuf; /* buffer for copy to user space */ 74 75 struct urb *command; 76 struct urb *response; 77 struct urb *validity; 78 struct usb_interface *intf; 79 struct usb_ctrlrequest *orq; 80 struct usb_ctrlrequest *irq; 81 spinlock_t iuspin; 82 83 unsigned long flags; 84 u16 bufsize; 85 u16 wMaxCommand; 86 u16 wMaxPacketSize; 87 __le16 inum; 88 int reslength; 89 int length; 90 int read; 91 int count; 92 dma_addr_t shandle; 93 dma_addr_t ihandle; 94 struct mutex wlock; 95 struct mutex rlock; 96 wait_queue_head_t wait; 97 struct work_struct rxwork; 98 int werr; 99 int rerr; 100 int resp_count; 101 102 struct list_head device_list; 103 int (*manage_power)(struct usb_interface *, int); 104 }; 105 106 static struct usb_driver wdm_driver; 107 108 /* return intfdata if we own the interface, else look up intf in the list */ 109 static struct wdm_device *wdm_find_device(struct usb_interface *intf) 110 { 111 struct wdm_device *desc; 112 113 spin_lock(&wdm_device_list_lock); 114 list_for_each_entry(desc, &wdm_device_list, device_list) 115 if (desc->intf == intf) 116 goto found; 117 desc = NULL; 118 found: 119 spin_unlock(&wdm_device_list_lock); 120 121 return desc; 122 } 123 124 static struct wdm_device *wdm_find_device_by_minor(int minor) 125 { 126 struct wdm_device *desc; 127 128 spin_lock(&wdm_device_list_lock); 129 list_for_each_entry(desc, &wdm_device_list, device_list) 130 if (desc->intf->minor == minor) 131 goto found; 132 desc = NULL; 133 found: 134 spin_unlock(&wdm_device_list_lock); 135 136 return desc; 137 } 138 139 /* --- callbacks --- */ 140 static void wdm_out_callback(struct urb *urb) 141 { 142 struct wdm_device *desc; 143 desc = urb->context; 144 spin_lock(&desc->iuspin); 145 desc->werr = urb->status; 146 spin_unlock(&desc->iuspin); 147 kfree(desc->outbuf); 148 desc->outbuf = NULL; 149 clear_bit(WDM_IN_USE, &desc->flags); 150 wake_up(&desc->wait); 151 } 152 153 /* forward declaration */ 154 static int service_outstanding_interrupt(struct wdm_device *desc); 155 156 static void wdm_in_callback(struct urb *urb) 157 { 158 struct wdm_device *desc = urb->context; 159 int status = urb->status; 160 int length = urb->actual_length; 161 162 spin_lock(&desc->iuspin); 163 clear_bit(WDM_RESPONDING, &desc->flags); 164 165 if (status) { 166 switch (status) { 167 case -ENOENT: 168 dev_dbg(&desc->intf->dev, 169 "nonzero urb status received: -ENOENT\n"); 170 goto skip_error; 171 case -ECONNRESET: 172 dev_dbg(&desc->intf->dev, 173 "nonzero urb status received: -ECONNRESET\n"); 174 goto skip_error; 175 case -ESHUTDOWN: 176 dev_dbg(&desc->intf->dev, 177 "nonzero urb status received: -ESHUTDOWN\n"); 178 goto skip_error; 179 case -EPIPE: 180 dev_err(&desc->intf->dev, 181 "nonzero urb status received: -EPIPE\n"); 182 break; 183 default: 184 dev_err(&desc->intf->dev, 185 "Unexpected error %d\n", status); 186 break; 187 } 188 } 189 190 /* 191 * only set a new error if there is no previous error. 192 * Errors are only cleared during read/open 193 */ 194 if (desc->rerr == 0) 195 desc->rerr = status; 196 197 if (length + desc->length > desc->wMaxCommand) { 198 /* The buffer would overflow */ 199 set_bit(WDM_OVERFLOW, &desc->flags); 200 } else { 201 /* we may already be in overflow */ 202 if (!test_bit(WDM_OVERFLOW, &desc->flags)) { 203 memmove(desc->ubuf + desc->length, desc->inbuf, length); 204 desc->length += length; 205 desc->reslength = length; 206 } 207 } 208 skip_error: 209 set_bit(WDM_READ, &desc->flags); 210 wake_up(&desc->wait); 211 212 if (desc->rerr) { 213 /* 214 * Since there was an error, userspace may decide to not read 215 * any data after poll'ing. 216 * We should respond to further attempts from the device to send 217 * data, so that we can get unstuck. 218 */ 219 service_outstanding_interrupt(desc); 220 } 221 222 spin_unlock(&desc->iuspin); 223 } 224 225 static void wdm_int_callback(struct urb *urb) 226 { 227 int rv = 0; 228 int responding; 229 int status = urb->status; 230 struct wdm_device *desc; 231 struct usb_cdc_notification *dr; 232 233 desc = urb->context; 234 dr = (struct usb_cdc_notification *)desc->sbuf; 235 236 if (status) { 237 switch (status) { 238 case -ESHUTDOWN: 239 case -ENOENT: 240 case -ECONNRESET: 241 return; /* unplug */ 242 case -EPIPE: 243 set_bit(WDM_INT_STALL, &desc->flags); 244 dev_err(&desc->intf->dev, "Stall on int endpoint\n"); 245 goto sw; /* halt is cleared in work */ 246 default: 247 dev_err(&desc->intf->dev, 248 "nonzero urb status received: %d\n", status); 249 break; 250 } 251 } 252 253 if (urb->actual_length < sizeof(struct usb_cdc_notification)) { 254 dev_err(&desc->intf->dev, "wdm_int_callback - %d bytes\n", 255 urb->actual_length); 256 goto exit; 257 } 258 259 switch (dr->bNotificationType) { 260 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE: 261 dev_dbg(&desc->intf->dev, 262 "NOTIFY_RESPONSE_AVAILABLE received: index %d len %d\n", 263 le16_to_cpu(dr->wIndex), le16_to_cpu(dr->wLength)); 264 break; 265 266 case USB_CDC_NOTIFY_NETWORK_CONNECTION: 267 268 dev_dbg(&desc->intf->dev, 269 "NOTIFY_NETWORK_CONNECTION %s network\n", 270 dr->wValue ? "connected to" : "disconnected from"); 271 goto exit; 272 case USB_CDC_NOTIFY_SPEED_CHANGE: 273 dev_dbg(&desc->intf->dev, "SPEED_CHANGE received (len %u)\n", 274 urb->actual_length); 275 goto exit; 276 default: 277 clear_bit(WDM_POLL_RUNNING, &desc->flags); 278 dev_err(&desc->intf->dev, 279 "unknown notification %d received: index %d len %d\n", 280 dr->bNotificationType, 281 le16_to_cpu(dr->wIndex), 282 le16_to_cpu(dr->wLength)); 283 goto exit; 284 } 285 286 spin_lock(&desc->iuspin); 287 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags); 288 if (!desc->resp_count++ && !responding 289 && !test_bit(WDM_DISCONNECTING, &desc->flags) 290 && !test_bit(WDM_SUSPENDING, &desc->flags)) { 291 rv = usb_submit_urb(desc->response, GFP_ATOMIC); 292 dev_dbg(&desc->intf->dev, "submit response URB %d\n", rv); 293 } 294 spin_unlock(&desc->iuspin); 295 if (rv < 0) { 296 clear_bit(WDM_RESPONDING, &desc->flags); 297 if (rv == -EPERM) 298 return; 299 if (rv == -ENOMEM) { 300 sw: 301 rv = schedule_work(&desc->rxwork); 302 if (rv) 303 dev_err(&desc->intf->dev, 304 "Cannot schedule work\n"); 305 } 306 } 307 exit: 308 rv = usb_submit_urb(urb, GFP_ATOMIC); 309 if (rv) 310 dev_err(&desc->intf->dev, 311 "%s - usb_submit_urb failed with result %d\n", 312 __func__, rv); 313 314 } 315 316 static void kill_urbs(struct wdm_device *desc) 317 { 318 /* the order here is essential */ 319 usb_kill_urb(desc->command); 320 usb_kill_urb(desc->validity); 321 usb_kill_urb(desc->response); 322 } 323 324 static void free_urbs(struct wdm_device *desc) 325 { 326 usb_free_urb(desc->validity); 327 usb_free_urb(desc->response); 328 usb_free_urb(desc->command); 329 } 330 331 static void cleanup(struct wdm_device *desc) 332 { 333 kfree(desc->sbuf); 334 kfree(desc->inbuf); 335 kfree(desc->orq); 336 kfree(desc->irq); 337 kfree(desc->ubuf); 338 free_urbs(desc); 339 kfree(desc); 340 } 341 342 static ssize_t wdm_write 343 (struct file *file, const char __user *buffer, size_t count, loff_t *ppos) 344 { 345 u8 *buf; 346 int rv = -EMSGSIZE, r, we; 347 struct wdm_device *desc = file->private_data; 348 struct usb_ctrlrequest *req; 349 350 if (count > desc->wMaxCommand) 351 count = desc->wMaxCommand; 352 353 spin_lock_irq(&desc->iuspin); 354 we = desc->werr; 355 desc->werr = 0; 356 spin_unlock_irq(&desc->iuspin); 357 if (we < 0) 358 return usb_translate_errors(we); 359 360 buf = memdup_user(buffer, count); 361 if (IS_ERR(buf)) 362 return PTR_ERR(buf); 363 364 /* concurrent writes and disconnect */ 365 r = mutex_lock_interruptible(&desc->wlock); 366 rv = -ERESTARTSYS; 367 if (r) 368 goto out_free_mem; 369 370 if (test_bit(WDM_DISCONNECTING, &desc->flags)) { 371 rv = -ENODEV; 372 goto out_free_mem_lock; 373 } 374 375 r = usb_autopm_get_interface(desc->intf); 376 if (r < 0) { 377 rv = usb_translate_errors(r); 378 goto out_free_mem_lock; 379 } 380 381 if (!(file->f_flags & O_NONBLOCK)) 382 r = wait_event_interruptible(desc->wait, !test_bit(WDM_IN_USE, 383 &desc->flags)); 384 else 385 if (test_bit(WDM_IN_USE, &desc->flags)) 386 r = -EAGAIN; 387 388 if (test_bit(WDM_RESETTING, &desc->flags)) 389 r = -EIO; 390 391 if (r < 0) { 392 rv = r; 393 goto out_free_mem_pm; 394 } 395 396 req = desc->orq; 397 usb_fill_control_urb( 398 desc->command, 399 interface_to_usbdev(desc->intf), 400 /* using common endpoint 0 */ 401 usb_sndctrlpipe(interface_to_usbdev(desc->intf), 0), 402 (unsigned char *)req, 403 buf, 404 count, 405 wdm_out_callback, 406 desc 407 ); 408 409 req->bRequestType = (USB_DIR_OUT | USB_TYPE_CLASS | 410 USB_RECIP_INTERFACE); 411 req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND; 412 req->wValue = 0; 413 req->wIndex = desc->inum; /* already converted */ 414 req->wLength = cpu_to_le16(count); 415 set_bit(WDM_IN_USE, &desc->flags); 416 desc->outbuf = buf; 417 418 rv = usb_submit_urb(desc->command, GFP_KERNEL); 419 if (rv < 0) { 420 desc->outbuf = NULL; 421 clear_bit(WDM_IN_USE, &desc->flags); 422 dev_err(&desc->intf->dev, "Tx URB error: %d\n", rv); 423 rv = usb_translate_errors(rv); 424 goto out_free_mem_pm; 425 } else { 426 dev_dbg(&desc->intf->dev, "Tx URB has been submitted index=%d\n", 427 le16_to_cpu(req->wIndex)); 428 } 429 430 usb_autopm_put_interface(desc->intf); 431 mutex_unlock(&desc->wlock); 432 return count; 433 434 out_free_mem_pm: 435 usb_autopm_put_interface(desc->intf); 436 out_free_mem_lock: 437 mutex_unlock(&desc->wlock); 438 out_free_mem: 439 kfree(buf); 440 return rv; 441 } 442 443 /* 444 * Submit the read urb if resp_count is non-zero. 445 * 446 * Called with desc->iuspin locked 447 */ 448 static int service_outstanding_interrupt(struct wdm_device *desc) 449 { 450 int rv = 0; 451 452 /* submit read urb only if the device is waiting for it */ 453 if (!desc->resp_count || !--desc->resp_count) 454 goto out; 455 456 set_bit(WDM_RESPONDING, &desc->flags); 457 spin_unlock_irq(&desc->iuspin); 458 rv = usb_submit_urb(desc->response, GFP_KERNEL); 459 spin_lock_irq(&desc->iuspin); 460 if (rv) { 461 dev_err(&desc->intf->dev, 462 "usb_submit_urb failed with result %d\n", rv); 463 464 /* make sure the next notification trigger a submit */ 465 clear_bit(WDM_RESPONDING, &desc->flags); 466 desc->resp_count = 0; 467 } 468 out: 469 return rv; 470 } 471 472 static ssize_t wdm_read 473 (struct file *file, char __user *buffer, size_t count, loff_t *ppos) 474 { 475 int rv, cntr; 476 int i = 0; 477 struct wdm_device *desc = file->private_data; 478 479 480 rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */ 481 if (rv < 0) 482 return -ERESTARTSYS; 483 484 cntr = ACCESS_ONCE(desc->length); 485 if (cntr == 0) { 486 desc->read = 0; 487 retry: 488 if (test_bit(WDM_DISCONNECTING, &desc->flags)) { 489 rv = -ENODEV; 490 goto err; 491 } 492 if (test_bit(WDM_OVERFLOW, &desc->flags)) { 493 clear_bit(WDM_OVERFLOW, &desc->flags); 494 rv = -ENOBUFS; 495 goto err; 496 } 497 i++; 498 if (file->f_flags & O_NONBLOCK) { 499 if (!test_bit(WDM_READ, &desc->flags)) { 500 rv = -EAGAIN; 501 goto err; 502 } 503 rv = 0; 504 } else { 505 rv = wait_event_interruptible(desc->wait, 506 test_bit(WDM_READ, &desc->flags)); 507 } 508 509 /* may have happened while we slept */ 510 if (test_bit(WDM_DISCONNECTING, &desc->flags)) { 511 rv = -ENODEV; 512 goto err; 513 } 514 if (test_bit(WDM_RESETTING, &desc->flags)) { 515 rv = -EIO; 516 goto err; 517 } 518 usb_mark_last_busy(interface_to_usbdev(desc->intf)); 519 if (rv < 0) { 520 rv = -ERESTARTSYS; 521 goto err; 522 } 523 524 spin_lock_irq(&desc->iuspin); 525 526 if (desc->rerr) { /* read completed, error happened */ 527 rv = usb_translate_errors(desc->rerr); 528 desc->rerr = 0; 529 spin_unlock_irq(&desc->iuspin); 530 goto err; 531 } 532 /* 533 * recheck whether we've lost the race 534 * against the completion handler 535 */ 536 if (!test_bit(WDM_READ, &desc->flags)) { /* lost race */ 537 spin_unlock_irq(&desc->iuspin); 538 goto retry; 539 } 540 541 if (!desc->reslength) { /* zero length read */ 542 dev_dbg(&desc->intf->dev, "zero length - clearing WDM_READ\n"); 543 clear_bit(WDM_READ, &desc->flags); 544 rv = service_outstanding_interrupt(desc); 545 spin_unlock_irq(&desc->iuspin); 546 if (rv < 0) 547 goto err; 548 goto retry; 549 } 550 cntr = desc->length; 551 spin_unlock_irq(&desc->iuspin); 552 } 553 554 if (cntr > count) 555 cntr = count; 556 rv = copy_to_user(buffer, desc->ubuf, cntr); 557 if (rv > 0) { 558 rv = -EFAULT; 559 goto err; 560 } 561 562 spin_lock_irq(&desc->iuspin); 563 564 for (i = 0; i < desc->length - cntr; i++) 565 desc->ubuf[i] = desc->ubuf[i + cntr]; 566 567 desc->length -= cntr; 568 /* in case we had outstanding data */ 569 if (!desc->length) { 570 clear_bit(WDM_READ, &desc->flags); 571 service_outstanding_interrupt(desc); 572 } 573 spin_unlock_irq(&desc->iuspin); 574 rv = cntr; 575 576 err: 577 mutex_unlock(&desc->rlock); 578 return rv; 579 } 580 581 static int wdm_flush(struct file *file, fl_owner_t id) 582 { 583 struct wdm_device *desc = file->private_data; 584 585 wait_event(desc->wait, !test_bit(WDM_IN_USE, &desc->flags)); 586 587 /* cannot dereference desc->intf if WDM_DISCONNECTING */ 588 if (desc->werr < 0 && !test_bit(WDM_DISCONNECTING, &desc->flags)) 589 dev_err(&desc->intf->dev, "Error in flush path: %d\n", 590 desc->werr); 591 592 return usb_translate_errors(desc->werr); 593 } 594 595 static unsigned int wdm_poll(struct file *file, struct poll_table_struct *wait) 596 { 597 struct wdm_device *desc = file->private_data; 598 unsigned long flags; 599 unsigned int mask = 0; 600 601 spin_lock_irqsave(&desc->iuspin, flags); 602 if (test_bit(WDM_DISCONNECTING, &desc->flags)) { 603 mask = POLLHUP | POLLERR; 604 spin_unlock_irqrestore(&desc->iuspin, flags); 605 goto desc_out; 606 } 607 if (test_bit(WDM_READ, &desc->flags)) 608 mask = POLLIN | POLLRDNORM; 609 if (desc->rerr || desc->werr) 610 mask |= POLLERR; 611 if (!test_bit(WDM_IN_USE, &desc->flags)) 612 mask |= POLLOUT | POLLWRNORM; 613 spin_unlock_irqrestore(&desc->iuspin, flags); 614 615 poll_wait(file, &desc->wait, wait); 616 617 desc_out: 618 return mask; 619 } 620 621 static int wdm_open(struct inode *inode, struct file *file) 622 { 623 int minor = iminor(inode); 624 int rv = -ENODEV; 625 struct usb_interface *intf; 626 struct wdm_device *desc; 627 628 mutex_lock(&wdm_mutex); 629 desc = wdm_find_device_by_minor(minor); 630 if (!desc) 631 goto out; 632 633 intf = desc->intf; 634 if (test_bit(WDM_DISCONNECTING, &desc->flags)) 635 goto out; 636 file->private_data = desc; 637 638 rv = usb_autopm_get_interface(desc->intf); 639 if (rv < 0) { 640 dev_err(&desc->intf->dev, "Error autopm - %d\n", rv); 641 goto out; 642 } 643 644 /* using write lock to protect desc->count */ 645 mutex_lock(&desc->wlock); 646 if (!desc->count++) { 647 desc->werr = 0; 648 desc->rerr = 0; 649 rv = usb_submit_urb(desc->validity, GFP_KERNEL); 650 if (rv < 0) { 651 desc->count--; 652 dev_err(&desc->intf->dev, 653 "Error submitting int urb - %d\n", rv); 654 rv = usb_translate_errors(rv); 655 } 656 } else { 657 rv = 0; 658 } 659 mutex_unlock(&desc->wlock); 660 if (desc->count == 1) 661 desc->manage_power(intf, 1); 662 usb_autopm_put_interface(desc->intf); 663 out: 664 mutex_unlock(&wdm_mutex); 665 return rv; 666 } 667 668 static int wdm_release(struct inode *inode, struct file *file) 669 { 670 struct wdm_device *desc = file->private_data; 671 672 mutex_lock(&wdm_mutex); 673 674 /* using write lock to protect desc->count */ 675 mutex_lock(&desc->wlock); 676 desc->count--; 677 mutex_unlock(&desc->wlock); 678 679 if (!desc->count) { 680 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) { 681 dev_dbg(&desc->intf->dev, "wdm_release: cleanup\n"); 682 kill_urbs(desc); 683 spin_lock_irq(&desc->iuspin); 684 desc->resp_count = 0; 685 spin_unlock_irq(&desc->iuspin); 686 desc->manage_power(desc->intf, 0); 687 } else { 688 /* must avoid dev_printk here as desc->intf is invalid */ 689 pr_debug(KBUILD_MODNAME " %s: device gone - cleaning up\n", __func__); 690 cleanup(desc); 691 } 692 } 693 mutex_unlock(&wdm_mutex); 694 return 0; 695 } 696 697 static long wdm_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 698 { 699 struct wdm_device *desc = file->private_data; 700 int rv = 0; 701 702 switch (cmd) { 703 case IOCTL_WDM_MAX_COMMAND: 704 if (copy_to_user((void __user *)arg, &desc->wMaxCommand, sizeof(desc->wMaxCommand))) 705 rv = -EFAULT; 706 break; 707 default: 708 rv = -ENOTTY; 709 } 710 return rv; 711 } 712 713 static const struct file_operations wdm_fops = { 714 .owner = THIS_MODULE, 715 .read = wdm_read, 716 .write = wdm_write, 717 .open = wdm_open, 718 .flush = wdm_flush, 719 .release = wdm_release, 720 .poll = wdm_poll, 721 .unlocked_ioctl = wdm_ioctl, 722 .compat_ioctl = wdm_ioctl, 723 .llseek = noop_llseek, 724 }; 725 726 static struct usb_class_driver wdm_class = { 727 .name = "cdc-wdm%d", 728 .fops = &wdm_fops, 729 .minor_base = WDM_MINOR_BASE, 730 }; 731 732 /* --- error handling --- */ 733 static void wdm_rxwork(struct work_struct *work) 734 { 735 struct wdm_device *desc = container_of(work, struct wdm_device, rxwork); 736 unsigned long flags; 737 int rv = 0; 738 int responding; 739 740 spin_lock_irqsave(&desc->iuspin, flags); 741 if (test_bit(WDM_DISCONNECTING, &desc->flags)) { 742 spin_unlock_irqrestore(&desc->iuspin, flags); 743 } else { 744 responding = test_and_set_bit(WDM_RESPONDING, &desc->flags); 745 spin_unlock_irqrestore(&desc->iuspin, flags); 746 if (!responding) 747 rv = usb_submit_urb(desc->response, GFP_KERNEL); 748 if (rv < 0 && rv != -EPERM) { 749 spin_lock_irqsave(&desc->iuspin, flags); 750 clear_bit(WDM_RESPONDING, &desc->flags); 751 if (!test_bit(WDM_DISCONNECTING, &desc->flags)) 752 schedule_work(&desc->rxwork); 753 spin_unlock_irqrestore(&desc->iuspin, flags); 754 } 755 } 756 } 757 758 /* --- hotplug --- */ 759 760 static int wdm_create(struct usb_interface *intf, struct usb_endpoint_descriptor *ep, 761 u16 bufsize, int (*manage_power)(struct usb_interface *, int)) 762 { 763 int rv = -ENOMEM; 764 struct wdm_device *desc; 765 766 desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL); 767 if (!desc) 768 goto out; 769 INIT_LIST_HEAD(&desc->device_list); 770 mutex_init(&desc->rlock); 771 mutex_init(&desc->wlock); 772 spin_lock_init(&desc->iuspin); 773 init_waitqueue_head(&desc->wait); 774 desc->wMaxCommand = bufsize; 775 /* this will be expanded and needed in hardware endianness */ 776 desc->inum = cpu_to_le16((u16)intf->cur_altsetting->desc.bInterfaceNumber); 777 desc->intf = intf; 778 INIT_WORK(&desc->rxwork, wdm_rxwork); 779 780 rv = -EINVAL; 781 if (!usb_endpoint_is_int_in(ep)) 782 goto err; 783 784 desc->wMaxPacketSize = usb_endpoint_maxp(ep); 785 786 desc->orq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); 787 if (!desc->orq) 788 goto err; 789 desc->irq = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); 790 if (!desc->irq) 791 goto err; 792 793 desc->validity = usb_alloc_urb(0, GFP_KERNEL); 794 if (!desc->validity) 795 goto err; 796 797 desc->response = usb_alloc_urb(0, GFP_KERNEL); 798 if (!desc->response) 799 goto err; 800 801 desc->command = usb_alloc_urb(0, GFP_KERNEL); 802 if (!desc->command) 803 goto err; 804 805 desc->ubuf = kmalloc(desc->wMaxCommand, GFP_KERNEL); 806 if (!desc->ubuf) 807 goto err; 808 809 desc->sbuf = kmalloc(desc->wMaxPacketSize, GFP_KERNEL); 810 if (!desc->sbuf) 811 goto err; 812 813 desc->inbuf = kmalloc(desc->wMaxCommand, GFP_KERNEL); 814 if (!desc->inbuf) 815 goto err; 816 817 usb_fill_int_urb( 818 desc->validity, 819 interface_to_usbdev(intf), 820 usb_rcvintpipe(interface_to_usbdev(intf), ep->bEndpointAddress), 821 desc->sbuf, 822 desc->wMaxPacketSize, 823 wdm_int_callback, 824 desc, 825 ep->bInterval 826 ); 827 828 desc->irq->bRequestType = (USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE); 829 desc->irq->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE; 830 desc->irq->wValue = 0; 831 desc->irq->wIndex = desc->inum; /* already converted */ 832 desc->irq->wLength = cpu_to_le16(desc->wMaxCommand); 833 834 usb_fill_control_urb( 835 desc->response, 836 interface_to_usbdev(intf), 837 /* using common endpoint 0 */ 838 usb_rcvctrlpipe(interface_to_usbdev(desc->intf), 0), 839 (unsigned char *)desc->irq, 840 desc->inbuf, 841 desc->wMaxCommand, 842 wdm_in_callback, 843 desc 844 ); 845 846 desc->manage_power = manage_power; 847 848 spin_lock(&wdm_device_list_lock); 849 list_add(&desc->device_list, &wdm_device_list); 850 spin_unlock(&wdm_device_list_lock); 851 852 rv = usb_register_dev(intf, &wdm_class); 853 if (rv < 0) 854 goto err; 855 else 856 dev_info(&intf->dev, "%s: USB WDM device\n", dev_name(intf->usb_dev)); 857 out: 858 return rv; 859 err: 860 spin_lock(&wdm_device_list_lock); 861 list_del(&desc->device_list); 862 spin_unlock(&wdm_device_list_lock); 863 cleanup(desc); 864 return rv; 865 } 866 867 static int wdm_manage_power(struct usb_interface *intf, int on) 868 { 869 /* need autopm_get/put here to ensure the usbcore sees the new value */ 870 int rv = usb_autopm_get_interface(intf); 871 872 intf->needs_remote_wakeup = on; 873 if (!rv) 874 usb_autopm_put_interface(intf); 875 return 0; 876 } 877 878 static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id) 879 { 880 int rv = -EINVAL; 881 struct usb_host_interface *iface; 882 struct usb_endpoint_descriptor *ep; 883 struct usb_cdc_parsed_header hdr; 884 u8 *buffer = intf->altsetting->extra; 885 int buflen = intf->altsetting->extralen; 886 u16 maxcom = WDM_DEFAULT_BUFSIZE; 887 888 if (!buffer) 889 goto err; 890 891 cdc_parse_cdc_header(&hdr, intf, buffer, buflen); 892 893 if (hdr.usb_cdc_dmm_desc) 894 maxcom = le16_to_cpu(hdr.usb_cdc_dmm_desc->wMaxCommand); 895 896 iface = intf->cur_altsetting; 897 if (iface->desc.bNumEndpoints != 1) 898 goto err; 899 ep = &iface->endpoint[0].desc; 900 901 rv = wdm_create(intf, ep, maxcom, &wdm_manage_power); 902 903 err: 904 return rv; 905 } 906 907 /** 908 * usb_cdc_wdm_register - register a WDM subdriver 909 * @intf: usb interface the subdriver will associate with 910 * @ep: interrupt endpoint to monitor for notifications 911 * @bufsize: maximum message size to support for read/write 912 * 913 * Create WDM usb class character device and associate it with intf 914 * without binding, allowing another driver to manage the interface. 915 * 916 * The subdriver will manage the given interrupt endpoint exclusively 917 * and will issue control requests referring to the given intf. It 918 * will otherwise avoid interferring, and in particular not do 919 * usb_set_intfdata/usb_get_intfdata on intf. 920 * 921 * The return value is a pointer to the subdriver's struct usb_driver. 922 * The registering driver is responsible for calling this subdriver's 923 * disconnect, suspend, resume, pre_reset and post_reset methods from 924 * its own. 925 */ 926 struct usb_driver *usb_cdc_wdm_register(struct usb_interface *intf, 927 struct usb_endpoint_descriptor *ep, 928 int bufsize, 929 int (*manage_power)(struct usb_interface *, int)) 930 { 931 int rv = -EINVAL; 932 933 rv = wdm_create(intf, ep, bufsize, manage_power); 934 if (rv < 0) 935 goto err; 936 937 return &wdm_driver; 938 err: 939 return ERR_PTR(rv); 940 } 941 EXPORT_SYMBOL(usb_cdc_wdm_register); 942 943 static void wdm_disconnect(struct usb_interface *intf) 944 { 945 struct wdm_device *desc; 946 unsigned long flags; 947 948 usb_deregister_dev(intf, &wdm_class); 949 desc = wdm_find_device(intf); 950 mutex_lock(&wdm_mutex); 951 952 /* the spinlock makes sure no new urbs are generated in the callbacks */ 953 spin_lock_irqsave(&desc->iuspin, flags); 954 set_bit(WDM_DISCONNECTING, &desc->flags); 955 set_bit(WDM_READ, &desc->flags); 956 /* to terminate pending flushes */ 957 clear_bit(WDM_IN_USE, &desc->flags); 958 spin_unlock_irqrestore(&desc->iuspin, flags); 959 wake_up_all(&desc->wait); 960 mutex_lock(&desc->rlock); 961 mutex_lock(&desc->wlock); 962 kill_urbs(desc); 963 cancel_work_sync(&desc->rxwork); 964 mutex_unlock(&desc->wlock); 965 mutex_unlock(&desc->rlock); 966 967 /* the desc->intf pointer used as list key is now invalid */ 968 spin_lock(&wdm_device_list_lock); 969 list_del(&desc->device_list); 970 spin_unlock(&wdm_device_list_lock); 971 972 if (!desc->count) 973 cleanup(desc); 974 else 975 dev_dbg(&intf->dev, "%d open files - postponing cleanup\n", desc->count); 976 mutex_unlock(&wdm_mutex); 977 } 978 979 #ifdef CONFIG_PM 980 static int wdm_suspend(struct usb_interface *intf, pm_message_t message) 981 { 982 struct wdm_device *desc = wdm_find_device(intf); 983 int rv = 0; 984 985 dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor); 986 987 /* if this is an autosuspend the caller does the locking */ 988 if (!PMSG_IS_AUTO(message)) { 989 mutex_lock(&desc->rlock); 990 mutex_lock(&desc->wlock); 991 } 992 spin_lock_irq(&desc->iuspin); 993 994 if (PMSG_IS_AUTO(message) && 995 (test_bit(WDM_IN_USE, &desc->flags) 996 || test_bit(WDM_RESPONDING, &desc->flags))) { 997 spin_unlock_irq(&desc->iuspin); 998 rv = -EBUSY; 999 } else { 1000 1001 set_bit(WDM_SUSPENDING, &desc->flags); 1002 spin_unlock_irq(&desc->iuspin); 1003 /* callback submits work - order is essential */ 1004 kill_urbs(desc); 1005 cancel_work_sync(&desc->rxwork); 1006 } 1007 if (!PMSG_IS_AUTO(message)) { 1008 mutex_unlock(&desc->wlock); 1009 mutex_unlock(&desc->rlock); 1010 } 1011 1012 return rv; 1013 } 1014 #endif 1015 1016 static int recover_from_urb_loss(struct wdm_device *desc) 1017 { 1018 int rv = 0; 1019 1020 if (desc->count) { 1021 rv = usb_submit_urb(desc->validity, GFP_NOIO); 1022 if (rv < 0) 1023 dev_err(&desc->intf->dev, 1024 "Error resume submitting int urb - %d\n", rv); 1025 } 1026 return rv; 1027 } 1028 1029 #ifdef CONFIG_PM 1030 static int wdm_resume(struct usb_interface *intf) 1031 { 1032 struct wdm_device *desc = wdm_find_device(intf); 1033 int rv; 1034 1035 dev_dbg(&desc->intf->dev, "wdm%d_resume\n", intf->minor); 1036 1037 clear_bit(WDM_SUSPENDING, &desc->flags); 1038 rv = recover_from_urb_loss(desc); 1039 1040 return rv; 1041 } 1042 #endif 1043 1044 static int wdm_pre_reset(struct usb_interface *intf) 1045 { 1046 struct wdm_device *desc = wdm_find_device(intf); 1047 1048 /* 1049 * we notify everybody using poll of 1050 * an exceptional situation 1051 * must be done before recovery lest a spontaneous 1052 * message from the device is lost 1053 */ 1054 spin_lock_irq(&desc->iuspin); 1055 set_bit(WDM_RESETTING, &desc->flags); /* inform read/write */ 1056 set_bit(WDM_READ, &desc->flags); /* unblock read */ 1057 clear_bit(WDM_IN_USE, &desc->flags); /* unblock write */ 1058 desc->rerr = -EINTR; 1059 spin_unlock_irq(&desc->iuspin); 1060 wake_up_all(&desc->wait); 1061 mutex_lock(&desc->rlock); 1062 mutex_lock(&desc->wlock); 1063 kill_urbs(desc); 1064 cancel_work_sync(&desc->rxwork); 1065 return 0; 1066 } 1067 1068 static int wdm_post_reset(struct usb_interface *intf) 1069 { 1070 struct wdm_device *desc = wdm_find_device(intf); 1071 int rv; 1072 1073 clear_bit(WDM_OVERFLOW, &desc->flags); 1074 clear_bit(WDM_RESETTING, &desc->flags); 1075 rv = recover_from_urb_loss(desc); 1076 mutex_unlock(&desc->wlock); 1077 mutex_unlock(&desc->rlock); 1078 return 0; 1079 } 1080 1081 static struct usb_driver wdm_driver = { 1082 .name = "cdc_wdm", 1083 .probe = wdm_probe, 1084 .disconnect = wdm_disconnect, 1085 #ifdef CONFIG_PM 1086 .suspend = wdm_suspend, 1087 .resume = wdm_resume, 1088 .reset_resume = wdm_resume, 1089 #endif 1090 .pre_reset = wdm_pre_reset, 1091 .post_reset = wdm_post_reset, 1092 .id_table = wdm_ids, 1093 .supports_autosuspend = 1, 1094 .disable_hub_initiated_lpm = 1, 1095 }; 1096 1097 module_usb_driver(wdm_driver); 1098 1099 MODULE_AUTHOR(DRIVER_AUTHOR); 1100 MODULE_DESCRIPTION(DRIVER_DESC); 1101 MODULE_LICENSE("GPL"); 1102