ALSA: snd-usb: move calls to usb_set_interface

The rework of the snd-usb endpoint logic moved the calls to
snd_usb_set_interface() into the snd_usb_endpoint implemenation. This
changed the order in which these calls are issued to the device, and
thereby caused regressions for some webcams.

Fix this by moving the calls back to pcm.c for now to make it work again
and use snd_usb_endpoint_activate() to really tear down all remaining
URBs in the flight, consequently fixing another regression caused by USB
packets on the wire after altsetting 0 has been selected.

Signed-off-by: Daniel Mack <zonque@gmail.com>
Reported-and-tested-by: Philipp Dreimann <philipp@dreimann.net>
Reported-by: Joseph Salisbury <joseph.salisbury@canonical.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>

authored by Daniel Mack and committed by Takashi Iwai 68e67f40 9e9b5946

Changed files
+41 -89
sound
+6 -67
sound/usb/endpoint.c
··· 414 414 { 415 415 struct list_head *p; 416 416 struct snd_usb_endpoint *ep; 417 - int ret, is_playback = direction == SNDRV_PCM_STREAM_PLAYBACK; 417 + int is_playback = direction == SNDRV_PCM_STREAM_PLAYBACK; 418 418 419 419 mutex_lock(&chip->mutex); 420 420 ··· 433 433 is_playback ? "playback" : "capture", 434 434 type == SND_USB_ENDPOINT_TYPE_DATA ? "data" : "sync", 435 435 ep_num); 436 - 437 - /* select the alt setting once so the endpoints become valid */ 438 - ret = usb_set_interface(chip->dev, alts->desc.bInterfaceNumber, 439 - alts->desc.bAlternateSetting); 440 - if (ret < 0) { 441 - snd_printk(KERN_ERR "%s(): usb_set_interface() failed, ret = %d\n", 442 - __func__, ret); 443 - ep = NULL; 444 - goto __exit_unlock; 445 - } 446 436 447 437 ep = kzalloc(sizeof(*ep), GFP_KERNEL); 448 438 if (!ep) ··· 821 831 if (++ep->use_count != 1) 822 832 return 0; 823 833 824 - if (snd_BUG_ON(!test_bit(EP_FLAG_ACTIVATED, &ep->flags))) 825 - return -EINVAL; 826 - 827 834 /* just to be sure */ 828 835 deactivate_urbs(ep, 0, 1); 829 836 wait_clear_urbs(ep); ··· 898 911 if (snd_BUG_ON(ep->use_count == 0)) 899 912 return; 900 913 901 - if (snd_BUG_ON(!test_bit(EP_FLAG_ACTIVATED, &ep->flags))) 902 - return; 903 - 904 914 if (--ep->use_count == 0) { 905 915 deactivate_urbs(ep, force, can_sleep); 906 916 ep->data_subs = NULL; ··· 908 924 if (wait) 909 925 wait_clear_urbs(ep); 910 926 } 911 - } 912 - 913 - /** 914 - * snd_usb_endpoint_activate: activate an snd_usb_endpoint 915 - * 916 - * @ep: the endpoint to activate 917 - * 918 - * If the endpoint is not currently in use, this functions will select the 919 - * correct alternate interface setting for the interface of this endpoint. 920 - * 921 - * In case of any active users, this functions does nothing. 922 - * 923 - * Returns an error if usb_set_interface() failed, 0 in all other 924 - * cases. 925 - */ 926 - int snd_usb_endpoint_activate(struct snd_usb_endpoint *ep) 927 - { 928 - if (ep->use_count != 0) 929 - return 0; 930 - 931 - if (!ep->chip->shutdown && 932 - !test_and_set_bit(EP_FLAG_ACTIVATED, &ep->flags)) { 933 - int ret; 934 - 935 - ret = usb_set_interface(ep->chip->dev, ep->iface, ep->alt_idx); 936 - if (ret < 0) { 937 - snd_printk(KERN_ERR "%s() usb_set_interface() failed, ret = %d\n", 938 - __func__, ret); 939 - clear_bit(EP_FLAG_ACTIVATED, &ep->flags); 940 - return ret; 941 - } 942 - 943 - return 0; 944 - } 945 - 946 - return -EBUSY; 947 927 } 948 928 949 929 /** ··· 928 980 if (!ep) 929 981 return -EINVAL; 930 982 983 + deactivate_urbs(ep, 1, 1); 984 + wait_clear_urbs(ep); 985 + 931 986 if (ep->use_count != 0) 932 987 return 0; 933 988 934 - if (!ep->chip->shutdown && 935 - test_and_clear_bit(EP_FLAG_ACTIVATED, &ep->flags)) { 936 - int ret; 989 + clear_bit(EP_FLAG_ACTIVATED, &ep->flags); 937 990 938 - ret = usb_set_interface(ep->chip->dev, ep->iface, 0); 939 - if (ret < 0) { 940 - snd_printk(KERN_ERR "%s(): usb_set_interface() failed, ret = %d\n", 941 - __func__, ret); 942 - return ret; 943 - } 944 - 945 - return 0; 946 - } 947 - 948 - return -EBUSY; 991 + return 0; 949 992 } 950 993 951 994 /**
+35 -22
sound/usb/pcm.c
··· 261 261 force, can_sleep, wait); 262 262 } 263 263 264 - static int activate_endpoints(struct snd_usb_substream *subs) 265 - { 266 - if (subs->sync_endpoint) { 267 - int ret; 268 - 269 - ret = snd_usb_endpoint_activate(subs->sync_endpoint); 270 - if (ret < 0) 271 - return ret; 272 - } 273 - 274 - return snd_usb_endpoint_activate(subs->data_endpoint); 275 - } 276 - 277 264 static int deactivate_endpoints(struct snd_usb_substream *subs) 278 265 { 279 266 int reta, retb; ··· 300 313 301 314 if (fmt == subs->cur_audiofmt) 302 315 return 0; 316 + 317 + /* close the old interface */ 318 + if (subs->interface >= 0 && subs->interface != fmt->iface) { 319 + err = usb_set_interface(subs->dev, subs->interface, 0); 320 + if (err < 0) { 321 + snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed (%d)\n", 322 + dev->devnum, fmt->iface, fmt->altsetting, err); 323 + return -EIO; 324 + } 325 + subs->interface = -1; 326 + subs->altset_idx = 0; 327 + } 328 + 329 + /* set interface */ 330 + if (subs->interface != fmt->iface || 331 + subs->altset_idx != fmt->altset_idx) { 332 + err = usb_set_interface(dev, fmt->iface, fmt->altsetting); 333 + if (err < 0) { 334 + snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed (%d)\n", 335 + dev->devnum, fmt->iface, fmt->altsetting, err); 336 + return -EIO; 337 + } 338 + snd_printdd(KERN_INFO "setting usb interface %d:%d\n", 339 + fmt->iface, fmt->altsetting); 340 + subs->interface = fmt->iface; 341 + subs->altset_idx = fmt->altset_idx; 342 + } 303 343 304 344 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip, 305 345 alts, fmt->endpoint, subs->direction, ··· 474 460 mutex_lock(&subs->stream->chip->shutdown_mutex); 475 461 /* format changed */ 476 462 stop_endpoints(subs, 0, 0, 0); 477 - deactivate_endpoints(subs); 478 - 479 - ret = activate_endpoints(subs); 480 - if (ret < 0) 481 - goto unlock; 482 - 483 463 ret = snd_usb_endpoint_set_params(subs->data_endpoint, hw_params, fmt, 484 464 subs->sync_endpoint); 485 465 if (ret < 0) ··· 508 500 subs->period_bytes = 0; 509 501 mutex_lock(&subs->stream->chip->shutdown_mutex); 510 502 stop_endpoints(subs, 0, 1, 1); 503 + deactivate_endpoints(subs); 511 504 mutex_unlock(&subs->stream->chip->shutdown_mutex); 512 505 return snd_pcm_lib_free_vmalloc_buffer(substream); 513 506 } ··· 947 938 948 939 static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction) 949 940 { 950 - int ret; 951 941 struct snd_usb_stream *as = snd_pcm_substream_chip(substream); 952 942 struct snd_usb_substream *subs = &as->substream[direction]; 953 943 954 944 stop_endpoints(subs, 0, 0, 0); 955 - ret = deactivate_endpoints(subs); 945 + 946 + if (!as->chip->shutdown && subs->interface >= 0) { 947 + usb_set_interface(subs->dev, subs->interface, 0); 948 + subs->interface = -1; 949 + } 950 + 956 951 subs->pcm_substream = NULL; 957 952 snd_usb_autosuspend(subs->stream->chip); 958 953 959 - return ret; 954 + return 0; 960 955 } 961 956 962 957 /* Since a URB can handle only a single linear buffer, we must use double