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