Commit e42a09bc authored by Takashi Iwai's avatar Takashi Iwai

ALSA: usb-audio: Add snd_usb_get_host_interface() helper

Add a helper function to retrieve the usb_host_interface object from
the given interface and altsetting number pair, which is a commonly
used procedure in the driver code.

No functional changes, just minor code refactoring.
Tested-by: default avatarKeith Milner <kamilner@superlative.org>
Tested-by: default avatarDylan Robinson <dylan_robinson@motu.com>
Link: https://lore.kernel.org/r/20201123085347.19667-17-tiwai@suse.deSigned-off-by: default avatarTakashi Iwai <tiwai@suse.de>
parent 98215056
...@@ -121,3 +121,13 @@ unsigned char snd_usb_parse_datainterval(struct snd_usb_audio *chip, ...@@ -121,3 +121,13 @@ unsigned char snd_usb_parse_datainterval(struct snd_usb_audio *chip,
return 0; return 0;
} }
struct usb_host_interface *
snd_usb_get_host_interface(struct snd_usb_audio *chip, int ifnum, int altsetting)
{
struct usb_interface *iface;
iface = usb_ifnum_to_if(chip->dev, ifnum);
if (!iface)
return NULL;
return usb_altnum_to_altsetting(iface, altsetting);
}
...@@ -14,6 +14,9 @@ int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, ...@@ -14,6 +14,9 @@ int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe,
unsigned char snd_usb_parse_datainterval(struct snd_usb_audio *chip, unsigned char snd_usb_parse_datainterval(struct snd_usb_audio *chip,
struct usb_host_interface *alts); struct usb_host_interface *alts);
struct usb_host_interface *
snd_usb_get_host_interface(struct snd_usb_audio *chip, int ifnum, int altsetting);
/* /*
* retrieve usb_interface descriptor from the host interface * retrieve usb_interface descriptor from the host interface
* (conditional for compatibility with the older API) * (conditional for compatibility with the older API)
......
...@@ -289,20 +289,16 @@ static int snd_usb_pcm_sync_stop(struct snd_pcm_substream *substream) ...@@ -289,20 +289,16 @@ static int snd_usb_pcm_sync_stop(struct snd_pcm_substream *substream)
} }
/* Check whether the given iface:altsetting points to an implicit fb source */ /* Check whether the given iface:altsetting points to an implicit fb source */
static bool search_generic_implicit_fb(struct usb_device *dev, int ifnum, static bool search_generic_implicit_fb(struct snd_usb_audio *chip, int ifnum,
unsigned int altsetting, unsigned int altsetting,
struct usb_host_interface **altsp, struct usb_host_interface **altsp,
unsigned int *ep) unsigned int *ep)
{ {
struct usb_interface *iface;
struct usb_host_interface *alts; struct usb_host_interface *alts;
struct usb_interface_descriptor *altsd; struct usb_interface_descriptor *altsd;
struct usb_endpoint_descriptor *epd; struct usb_endpoint_descriptor *epd;
iface = usb_ifnum_to_if(dev, ifnum); alts = snd_usb_get_host_interface(chip, ifnum, altsetting);
if (!iface)
return false;
alts = usb_altnum_to_altsetting(iface, altsetting);
if (!alts) if (!alts)
return false; return false;
altsd = get_iface_desc(alts); altsd = get_iface_desc(alts);
...@@ -322,20 +318,16 @@ static bool search_generic_implicit_fb(struct usb_device *dev, int ifnum, ...@@ -322,20 +318,16 @@ static bool search_generic_implicit_fb(struct usb_device *dev, int ifnum,
} }
/* Like the function above, but specific to Roland with vendor class and hack */ /* Like the function above, but specific to Roland with vendor class and hack */
static bool search_roland_implicit_fb(struct usb_device *dev, int ifnum, static bool search_roland_implicit_fb(struct snd_usb_audio *chip, int ifnum,
unsigned int altsetting, unsigned int altsetting,
struct usb_host_interface **altsp, struct usb_host_interface **altsp,
unsigned int *ep) unsigned int *ep)
{ {
struct usb_interface *iface;
struct usb_host_interface *alts; struct usb_host_interface *alts;
struct usb_interface_descriptor *altsd; struct usb_interface_descriptor *altsd;
struct usb_endpoint_descriptor *epd; struct usb_endpoint_descriptor *epd;
iface = usb_ifnum_to_if(dev, ifnum); alts = snd_usb_get_host_interface(chip, ifnum, altsetting);
if (!iface)
return false;
alts = usb_altnum_to_altsetting(iface, altsetting);
if (!alts) if (!alts)
return false; return false;
altsd = get_iface_desc(alts); altsd = get_iface_desc(alts);
...@@ -359,11 +351,11 @@ static bool search_roland_implicit_fb(struct usb_device *dev, int ifnum, ...@@ -359,11 +351,11 @@ static bool search_roland_implicit_fb(struct usb_device *dev, int ifnum,
*/ */
static int audioformat_implicit_fb_quirk(struct snd_usb_audio *chip, static int audioformat_implicit_fb_quirk(struct snd_usb_audio *chip,
struct audioformat *fmt, struct audioformat *fmt,
struct usb_interface *iface,
struct usb_host_interface *alts) struct usb_host_interface *alts)
{ {
struct usb_device *dev = chip->dev; struct usb_device *dev = chip->dev;
struct usb_interface_descriptor *altsd = get_iface_desc(alts); struct usb_interface_descriptor *altsd = get_iface_desc(alts);
struct usb_interface *iface;
unsigned int attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE; unsigned int attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
unsigned int ep; unsigned int ep;
unsigned int ifnum; unsigned int ifnum;
...@@ -431,7 +423,7 @@ static int audioformat_implicit_fb_quirk(struct snd_usb_audio *chip, ...@@ -431,7 +423,7 @@ static int audioformat_implicit_fb_quirk(struct snd_usb_audio *chip,
altsd->bInterfaceProtocol == UAC_VERSION_2 && altsd->bInterfaceProtocol == UAC_VERSION_2 &&
altsd->bNumEndpoints == 1) { altsd->bNumEndpoints == 1) {
ifnum = altsd->bInterfaceNumber + 1; ifnum = altsd->bInterfaceNumber + 1;
if (search_generic_implicit_fb(dev, ifnum, if (search_generic_implicit_fb(chip, ifnum,
altsd->bAlternateSetting, altsd->bAlternateSetting,
&alts, &ep)) &alts, &ep))
goto add_sync_ep; goto add_sync_ep;
...@@ -444,7 +436,7 @@ static int audioformat_implicit_fb_quirk(struct snd_usb_audio *chip, ...@@ -444,7 +436,7 @@ static int audioformat_implicit_fb_quirk(struct snd_usb_audio *chip,
altsd->bNumEndpoints == 1 && altsd->bNumEndpoints == 1 &&
USB_ID_VENDOR(chip->usb_id) == 0x0582 /* Roland */) { USB_ID_VENDOR(chip->usb_id) == 0x0582 /* Roland */) {
ifnum = altsd->bInterfaceNumber + 1; ifnum = altsd->bInterfaceNumber + 1;
if (search_roland_implicit_fb(dev, ifnum, if (search_roland_implicit_fb(chip, ifnum,
altsd->bAlternateSetting, altsd->bAlternateSetting,
&alts, &ep)) &alts, &ep))
goto add_sync_ep; goto add_sync_ep;
...@@ -477,24 +469,20 @@ int snd_usb_audioformat_set_sync_ep(struct snd_usb_audio *chip, ...@@ -477,24 +469,20 @@ int snd_usb_audioformat_set_sync_ep(struct snd_usb_audio *chip,
struct audioformat *fmt) struct audioformat *fmt)
{ {
struct usb_device *dev = chip->dev; struct usb_device *dev = chip->dev;
struct usb_interface *iface;
struct usb_host_interface *alts; struct usb_host_interface *alts;
struct usb_interface_descriptor *altsd; struct usb_interface_descriptor *altsd;
unsigned int ep, attr, sync_attr; unsigned int ep, attr, sync_attr;
bool is_playback; bool is_playback;
int err; int err;
iface = usb_ifnum_to_if(dev, fmt->iface); alts = snd_usb_get_host_interface(chip, fmt->iface, fmt->altsetting);
if (!iface)
return 0;
alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
if (!alts) if (!alts)
return 0; return 0;
altsd = get_iface_desc(alts); altsd = get_iface_desc(alts);
is_playback = !(get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN); is_playback = !(get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN);
if (is_playback) { if (is_playback) {
err = audioformat_implicit_fb_quirk(chip, fmt, iface, alts); err = audioformat_implicit_fb_quirk(chip, fmt, alts);
if (err > 0) if (err > 0)
return 0; return 0;
} }
...@@ -564,7 +552,6 @@ static int set_sync_endpoint(struct snd_usb_substream *subs, ...@@ -564,7 +552,6 @@ static int set_sync_endpoint(struct snd_usb_substream *subs,
struct audioformat *fmt) struct audioformat *fmt)
{ {
struct usb_device *dev = subs->dev; struct usb_device *dev = subs->dev;
struct usb_interface *iface;
struct usb_host_interface *alts; struct usb_host_interface *alts;
int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK; int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
unsigned int ep; unsigned int ep;
...@@ -577,11 +564,8 @@ static int set_sync_endpoint(struct snd_usb_substream *subs, ...@@ -577,11 +564,8 @@ static int set_sync_endpoint(struct snd_usb_substream *subs,
if (!ep) if (!ep)
return 0; return 0;
iface = usb_ifnum_to_if(dev, fmt->sync_iface); alts = snd_usb_get_host_interface(subs->stream->chip, fmt->sync_iface,
if (!iface) fmt->altsetting);
return 0;
alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
if (!alts) if (!alts)
return 0; return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment