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