diff options
Diffstat (limited to 'drivers/usb/class/cdc-wdm.c')
-rw-r--r-- | drivers/usb/class/cdc-wdm.c | 59 |
1 files changed, 38 insertions, 21 deletions
diff --git a/drivers/usb/class/cdc-wdm.c b/drivers/usb/class/cdc-wdm.c index 1c50baff7725..d2b3cffca3f7 100644 --- a/drivers/usb/class/cdc-wdm.c +++ b/drivers/usb/class/cdc-wdm.c | |||
@@ -57,6 +57,8 @@ MODULE_DEVICE_TABLE (usb, wdm_ids); | |||
57 | 57 | ||
58 | #define WDM_MAX 16 | 58 | #define WDM_MAX 16 |
59 | 59 | ||
60 | /* CDC-WMC r1.1 requires wMaxCommand to be "at least 256 decimal (0x100)" */ | ||
61 | #define WDM_DEFAULT_BUFSIZE 256 | ||
60 | 62 | ||
61 | static DEFINE_MUTEX(wdm_mutex); | 63 | static DEFINE_MUTEX(wdm_mutex); |
62 | 64 | ||
@@ -88,7 +90,8 @@ struct wdm_device { | |||
88 | int count; | 90 | int count; |
89 | dma_addr_t shandle; | 91 | dma_addr_t shandle; |
90 | dma_addr_t ihandle; | 92 | dma_addr_t ihandle; |
91 | struct mutex lock; | 93 | struct mutex wlock; |
94 | struct mutex rlock; | ||
92 | wait_queue_head_t wait; | 95 | wait_queue_head_t wait; |
93 | struct work_struct rxwork; | 96 | struct work_struct rxwork; |
94 | int werr; | 97 | int werr; |
@@ -323,7 +326,7 @@ static ssize_t wdm_write | |||
323 | } | 326 | } |
324 | 327 | ||
325 | /* concurrent writes and disconnect */ | 328 | /* concurrent writes and disconnect */ |
326 | r = mutex_lock_interruptible(&desc->lock); | 329 | r = mutex_lock_interruptible(&desc->wlock); |
327 | rv = -ERESTARTSYS; | 330 | rv = -ERESTARTSYS; |
328 | if (r) { | 331 | if (r) { |
329 | kfree(buf); | 332 | kfree(buf); |
@@ -386,7 +389,7 @@ static ssize_t wdm_write | |||
386 | out: | 389 | out: |
387 | usb_autopm_put_interface(desc->intf); | 390 | usb_autopm_put_interface(desc->intf); |
388 | outnp: | 391 | outnp: |
389 | mutex_unlock(&desc->lock); | 392 | mutex_unlock(&desc->wlock); |
390 | outnl: | 393 | outnl: |
391 | return rv < 0 ? rv : count; | 394 | return rv < 0 ? rv : count; |
392 | } | 395 | } |
@@ -399,7 +402,7 @@ static ssize_t wdm_read | |||
399 | struct wdm_device *desc = file->private_data; | 402 | struct wdm_device *desc = file->private_data; |
400 | 403 | ||
401 | 404 | ||
402 | rv = mutex_lock_interruptible(&desc->lock); /*concurrent reads */ | 405 | rv = mutex_lock_interruptible(&desc->rlock); /*concurrent reads */ |
403 | if (rv < 0) | 406 | if (rv < 0) |
404 | return -ERESTARTSYS; | 407 | return -ERESTARTSYS; |
405 | 408 | ||
@@ -467,14 +470,16 @@ retry: | |||
467 | for (i = 0; i < desc->length - cntr; i++) | 470 | for (i = 0; i < desc->length - cntr; i++) |
468 | desc->ubuf[i] = desc->ubuf[i + cntr]; | 471 | desc->ubuf[i] = desc->ubuf[i + cntr]; |
469 | 472 | ||
473 | spin_lock_irq(&desc->iuspin); | ||
470 | desc->length -= cntr; | 474 | desc->length -= cntr; |
475 | spin_unlock_irq(&desc->iuspin); | ||
471 | /* in case we had outstanding data */ | 476 | /* in case we had outstanding data */ |
472 | if (!desc->length) | 477 | if (!desc->length) |
473 | clear_bit(WDM_READ, &desc->flags); | 478 | clear_bit(WDM_READ, &desc->flags); |
474 | rv = cntr; | 479 | rv = cntr; |
475 | 480 | ||
476 | err: | 481 | err: |
477 | mutex_unlock(&desc->lock); | 482 | mutex_unlock(&desc->rlock); |
478 | return rv; | 483 | return rv; |
479 | } | 484 | } |
480 | 485 | ||
@@ -540,7 +545,8 @@ static int wdm_open(struct inode *inode, struct file *file) | |||
540 | } | 545 | } |
541 | intf->needs_remote_wakeup = 1; | 546 | intf->needs_remote_wakeup = 1; |
542 | 547 | ||
543 | mutex_lock(&desc->lock); | 548 | /* using write lock to protect desc->count */ |
549 | mutex_lock(&desc->wlock); | ||
544 | if (!desc->count++) { | 550 | if (!desc->count++) { |
545 | desc->werr = 0; | 551 | desc->werr = 0; |
546 | desc->rerr = 0; | 552 | desc->rerr = 0; |
@@ -553,7 +559,7 @@ static int wdm_open(struct inode *inode, struct file *file) | |||
553 | } else { | 559 | } else { |
554 | rv = 0; | 560 | rv = 0; |
555 | } | 561 | } |
556 | mutex_unlock(&desc->lock); | 562 | mutex_unlock(&desc->wlock); |
557 | usb_autopm_put_interface(desc->intf); | 563 | usb_autopm_put_interface(desc->intf); |
558 | out: | 564 | out: |
559 | mutex_unlock(&wdm_mutex); | 565 | mutex_unlock(&wdm_mutex); |
@@ -565,9 +571,11 @@ static int wdm_release(struct inode *inode, struct file *file) | |||
565 | struct wdm_device *desc = file->private_data; | 571 | struct wdm_device *desc = file->private_data; |
566 | 572 | ||
567 | mutex_lock(&wdm_mutex); | 573 | mutex_lock(&wdm_mutex); |
568 | mutex_lock(&desc->lock); | 574 | |
575 | /* using write lock to protect desc->count */ | ||
576 | mutex_lock(&desc->wlock); | ||
569 | desc->count--; | 577 | desc->count--; |
570 | mutex_unlock(&desc->lock); | 578 | mutex_unlock(&desc->wlock); |
571 | 579 | ||
572 | if (!desc->count) { | 580 | if (!desc->count) { |
573 | dev_dbg(&desc->intf->dev, "wdm_release: cleanup"); | 581 | dev_dbg(&desc->intf->dev, "wdm_release: cleanup"); |
@@ -630,7 +638,7 @@ static int wdm_probe(struct usb_interface *intf, const struct usb_device_id *id) | |||
630 | struct usb_cdc_dmm_desc *dmhd; | 638 | struct usb_cdc_dmm_desc *dmhd; |
631 | u8 *buffer = intf->altsetting->extra; | 639 | u8 *buffer = intf->altsetting->extra; |
632 | int buflen = intf->altsetting->extralen; | 640 | int buflen = intf->altsetting->extralen; |
633 | u16 maxcom = 0; | 641 | u16 maxcom = WDM_DEFAULT_BUFSIZE; |
634 | 642 | ||
635 | if (!buffer) | 643 | if (!buffer) |
636 | goto out; | 644 | goto out; |
@@ -665,7 +673,8 @@ next_desc: | |||
665 | desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL); | 673 | desc = kzalloc(sizeof(struct wdm_device), GFP_KERNEL); |
666 | if (!desc) | 674 | if (!desc) |
667 | goto out; | 675 | goto out; |
668 | mutex_init(&desc->lock); | 676 | mutex_init(&desc->rlock); |
677 | mutex_init(&desc->wlock); | ||
669 | spin_lock_init(&desc->iuspin); | 678 | spin_lock_init(&desc->iuspin); |
670 | init_waitqueue_head(&desc->wait); | 679 | init_waitqueue_head(&desc->wait); |
671 | desc->wMaxCommand = maxcom; | 680 | desc->wMaxCommand = maxcom; |
@@ -716,7 +725,7 @@ next_desc: | |||
716 | goto err; | 725 | goto err; |
717 | 726 | ||
718 | desc->inbuf = usb_alloc_coherent(interface_to_usbdev(intf), | 727 | desc->inbuf = usb_alloc_coherent(interface_to_usbdev(intf), |
719 | desc->bMaxPacketSize0, | 728 | desc->wMaxCommand, |
720 | GFP_KERNEL, | 729 | GFP_KERNEL, |
721 | &desc->response->transfer_dma); | 730 | &desc->response->transfer_dma); |
722 | if (!desc->inbuf) | 731 | if (!desc->inbuf) |
@@ -779,11 +788,13 @@ static void wdm_disconnect(struct usb_interface *intf) | |||
779 | /* to terminate pending flushes */ | 788 | /* to terminate pending flushes */ |
780 | clear_bit(WDM_IN_USE, &desc->flags); | 789 | clear_bit(WDM_IN_USE, &desc->flags); |
781 | spin_unlock_irqrestore(&desc->iuspin, flags); | 790 | spin_unlock_irqrestore(&desc->iuspin, flags); |
782 | mutex_lock(&desc->lock); | 791 | wake_up_all(&desc->wait); |
792 | mutex_lock(&desc->rlock); | ||
793 | mutex_lock(&desc->wlock); | ||
783 | kill_urbs(desc); | 794 | kill_urbs(desc); |
784 | cancel_work_sync(&desc->rxwork); | 795 | cancel_work_sync(&desc->rxwork); |
785 | mutex_unlock(&desc->lock); | 796 | mutex_unlock(&desc->wlock); |
786 | wake_up_all(&desc->wait); | 797 | mutex_unlock(&desc->rlock); |
787 | if (!desc->count) | 798 | if (!desc->count) |
788 | cleanup(desc); | 799 | cleanup(desc); |
789 | mutex_unlock(&wdm_mutex); | 800 | mutex_unlock(&wdm_mutex); |
@@ -798,8 +809,10 @@ static int wdm_suspend(struct usb_interface *intf, pm_message_t message) | |||
798 | dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor); | 809 | dev_dbg(&desc->intf->dev, "wdm%d_suspend\n", intf->minor); |
799 | 810 | ||
800 | /* if this is an autosuspend the caller does the locking */ | 811 | /* if this is an autosuspend the caller does the locking */ |
801 | if (!PMSG_IS_AUTO(message)) | 812 | if (!PMSG_IS_AUTO(message)) { |
802 | mutex_lock(&desc->lock); | 813 | mutex_lock(&desc->rlock); |
814 | mutex_lock(&desc->wlock); | ||
815 | } | ||
803 | spin_lock_irq(&desc->iuspin); | 816 | spin_lock_irq(&desc->iuspin); |
804 | 817 | ||
805 | if (PMSG_IS_AUTO(message) && | 818 | if (PMSG_IS_AUTO(message) && |
@@ -815,8 +828,10 @@ static int wdm_suspend(struct usb_interface *intf, pm_message_t message) | |||
815 | kill_urbs(desc); | 828 | kill_urbs(desc); |
816 | cancel_work_sync(&desc->rxwork); | 829 | cancel_work_sync(&desc->rxwork); |
817 | } | 830 | } |
818 | if (!PMSG_IS_AUTO(message)) | 831 | if (!PMSG_IS_AUTO(message)) { |
819 | mutex_unlock(&desc->lock); | 832 | mutex_unlock(&desc->wlock); |
833 | mutex_unlock(&desc->rlock); | ||
834 | } | ||
820 | 835 | ||
821 | return rv; | 836 | return rv; |
822 | } | 837 | } |
@@ -854,7 +869,8 @@ static int wdm_pre_reset(struct usb_interface *intf) | |||
854 | { | 869 | { |
855 | struct wdm_device *desc = usb_get_intfdata(intf); | 870 | struct wdm_device *desc = usb_get_intfdata(intf); |
856 | 871 | ||
857 | mutex_lock(&desc->lock); | 872 | mutex_lock(&desc->rlock); |
873 | mutex_lock(&desc->wlock); | ||
858 | kill_urbs(desc); | 874 | kill_urbs(desc); |
859 | 875 | ||
860 | /* | 876 | /* |
@@ -876,7 +892,8 @@ static int wdm_post_reset(struct usb_interface *intf) | |||
876 | int rv; | 892 | int rv; |
877 | 893 | ||
878 | rv = recover_from_urb_loss(desc); | 894 | rv = recover_from_urb_loss(desc); |
879 | mutex_unlock(&desc->lock); | 895 | mutex_unlock(&desc->wlock); |
896 | mutex_unlock(&desc->rlock); | ||
880 | return 0; | 897 | return 0; |
881 | } | 898 | } |
882 | 899 | ||