From: Cezary Rojewski <cezary.rojewski@intel.com>
To: broonie@kernel.org, tiwai@suse.com, perex@perex.cz
Cc: amadeuszx.slawinski@linux.intel.com, linux-sound@vger.kernel.org,
gregkh@linuxfoundation.org, quic_wcheng@quicinc.com,
mathias.nyman@linux.intel.com,
Cezary Rojewski <cezary.rojewski@intel.com>
Subject: [RFC 07/15] ALSA: usb: Switch to the two-stage chip probing
Date: Wed, 9 Apr 2025 13:07:22 +0200 [thread overview]
Message-ID: <20250409110731.3752332-8-cezary.rojewski@intel.com> (raw)
In-Reply-To: <20250409110731.3752332-1-cezary.rojewski@intel.com>
Utilize newly added functions and update existing probing procedure of
USB Audio-Class (AC) device from one-stage into two-stage process. In
consequence, drop snd_usb_audio_create() and rewrite usb_audio_probe()
to first do snd_usb_probe_unlocked() which is intended to be shared by
both ALSA and ASoC driver before continuing with creating all the
streams.
While at it, do not forget about snd_audio_disconnect(). Use
snd_usb_chip_release() to keep both probe() and disconnect()
synchronized.
Signed-off-by: Cezary Rojewski <cezary.rojewski@intel.com>
---
sound/usb/card.c | 281 ++++++++---------------------------------------
1 file changed, 45 insertions(+), 236 deletions(-)
diff --git a/sound/usb/card.c b/sound/usb/card.c
index a9e37fe0d29e..4af69a69b5ce 100644
--- a/sound/usb/card.c
+++ b/sound/usb/card.c
@@ -21,6 +21,7 @@
#include <linux/bitops.h>
+#include <linux/cleanup.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/slab.h>
@@ -687,86 +688,6 @@ static void usb_audio_make_longname(struct usb_device *dev,
}
}
-/*
- * create a chip instance and set its names.
- */
-static int snd_usb_audio_create(struct usb_interface *intf,
- struct usb_device *dev, int idx,
- const struct snd_usb_audio_quirk *quirk,
- unsigned int usb_id,
- struct snd_usb_audio **rchip)
-{
- struct snd_card *card;
- struct snd_usb_audio *chip;
- int err;
- char component[14];
-
- *rchip = NULL;
-
- switch (snd_usb_get_speed(dev)) {
- case USB_SPEED_LOW:
- case USB_SPEED_FULL:
- case USB_SPEED_HIGH:
- case USB_SPEED_SUPER:
- case USB_SPEED_SUPER_PLUS:
- break;
- default:
- dev_err(&dev->dev, "unknown device speed %d\n", snd_usb_get_speed(dev));
- return -ENXIO;
- }
-
- chip = kzalloc(sizeof(*chip), GFP_KERNEL);
- if (!chip)
- return -ENOMEM;
-
- err = snd_card_new(&intf->dev, index[idx], id[idx], THIS_MODULE, 0, &card);
- if (err < 0) {
- dev_err(&dev->dev, "cannot create card instance %d\n", idx);
- kfree(chip);
- return err;
- }
-
- mutex_init(&chip->mutex);
- init_waitqueue_head(&chip->shutdown_wait);
- chip->index = idx;
- chip->dev = dev;
- chip->card = card;
- chip->setup = device_setup[idx];
- chip->generic_implicit_fb = implicit_fb[idx];
- chip->autoclock = autoclock;
- chip->lowlatency = lowlatency;
- atomic_set(&chip->active, 1); /* avoid autopm during probing */
- atomic_set(&chip->usage_count, 0);
- atomic_set(&chip->shutdown, 0);
-
- chip->usb_id = usb_id;
- INIT_LIST_HEAD(&chip->pcm_list);
- INIT_LIST_HEAD(&chip->ep_list);
- INIT_LIST_HEAD(&chip->iface_ref_list);
- INIT_LIST_HEAD(&chip->clock_ref_list);
- INIT_LIST_HEAD(&chip->midi_list);
- INIT_LIST_HEAD(&chip->midi_v2_list);
- INIT_LIST_HEAD(&chip->mixer_list);
-
- if (quirk_flags[idx])
- chip->quirk_flags = quirk_flags[idx];
- else
- snd_usb_init_quirk_flags(chip);
-
- strcpy(card->driver, "USB-Audio");
- sprintf(component, "USB%04x:%04x",
- USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
- snd_component_add(card, component);
-
- usb_audio_make_shortname(dev, chip, quirk);
- usb_audio_make_longname(dev, chip, quirk);
-
- snd_usb_audio_create_proc(chip);
-
- *rchip = chip;
- return 0;
-}
-
/* look for a matching quirk alias id */
static bool get_alias_id(struct usb_device *dev, unsigned int *id)
{
@@ -870,7 +791,6 @@ static int snd_usb_parse_pcms(struct snd_usb_audio *chip, struct usb_interface *
return 0;
}
-__maybe_unused
static int snd_usb_create_nonpcms(struct snd_usb_audio *chip, struct usb_interface *iface,
struct usb_driver *driver)
{
@@ -1118,7 +1038,6 @@ static int snd_usb_sanity_check(struct usb_interface *iface,
return 0;
}
-__maybe_unused
static int snd_usb_probe_unlocked(struct usb_interface *iface, const struct usb_device_id *usb_id,
struct usb_driver *driver)
{
@@ -1179,7 +1098,6 @@ int snd_usb_bind_card(struct snd_usb_audio *chip, struct snd_card *card,
}
EXPORT_SYMBOL_GPL(snd_usb_bind_card);
-__maybe_unused
static int snd_usb_create_card(struct snd_usb_audio *chip, struct usb_interface *iface)
{
struct usb_device *udev = chip->dev;
@@ -1227,170 +1145,68 @@ static int try_to_register_card(struct snd_usb_audio *chip, int ifnum)
* only at the first time. the successive calls of this function will
* append the pcm interface to the corresponding card.
*/
-static int usb_audio_probe(struct usb_interface *intf,
- const struct usb_device_id *usb_id)
+static int usb_audio_probe(struct usb_interface *iface, const struct usb_device_id *usb_id)
{
- struct usb_device *dev = interface_to_usbdev(intf);
- const struct snd_usb_audio_quirk *quirk =
- (const struct snd_usb_audio_quirk *)usb_id->driver_info;
- struct snd_usb_audio *chip;
- int i, err;
struct usb_host_interface *alts;
- int ifnum;
- u32 id;
+ struct snd_usb_audio *chip;
+ struct usb_device *udev;
+ int ifnum, ret;
- alts = &intf->altsetting[0];
- ifnum = get_iface_desc(alts)->bInterfaceNumber;
- id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
- le16_to_cpu(dev->descriptor.idProduct));
- if (get_alias_id(dev, &id))
- quirk = get_alias_quirk(dev, id);
- if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
- return -ENXIO;
- if (quirk && quirk->ifnum == QUIRK_NODEV_INTERFACE)
+ alts = &iface->altsetting[0];
+ ifnum = alts->desc.bInterfaceNumber;
+ udev = interface_to_usbdev(iface);
+
+ udev = interface_to_usbdev(iface);
+ if (udev->audsb_capable)
return -ENODEV;
- err = snd_usb_apply_boot_quirk(dev, intf, quirk, id);
- if (err < 0)
- return err;
-
- /*
- * found a config. now register to ALSA
- */
-
- /* check whether it's already registered */
- chip = NULL;
- mutex_lock(®ister_mutex);
- for (i = 0; i < SNDRV_CARDS; i++) {
- if (usb_chip[i] && usb_chip[i]->dev == dev) {
- if (atomic_read(&usb_chip[i]->shutdown)) {
- dev_err(&dev->dev, "USB device is in the shutdown state, cannot create a card instance\n");
- err = -EIO;
- goto __error;
- }
- chip = usb_chip[i];
- atomic_inc(&chip->active); /* avoid autopm */
- break;
- }
- }
- if (! chip) {
- err = snd_usb_apply_boot_quirk_once(dev, intf, quirk, id);
- if (err < 0)
- goto __error;
-
- /* it's a fresh one.
- * now look for an empty slot and create a new card instance
- */
- for (i = 0; i < SNDRV_CARDS; i++)
- if (!usb_chip[i] &&
- (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
- (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
- if (enable[i]) {
- err = snd_usb_audio_create(intf, dev, i, quirk,
- id, &chip);
- if (err < 0)
- goto __error;
- break;
- } else if (vid[i] != -1 || pid[i] != -1) {
- dev_info(&dev->dev,
- "device (%04x:%04x) is disabled\n",
- USB_ID_VENDOR(id),
- USB_ID_PRODUCT(id));
- err = -ENOENT;
- goto __error;
- }
- }
- if (!chip) {
- dev_err(&dev->dev, "no available usb audio device\n");
- err = -ENODEV;
- goto __error;
- }
- find_last_interface(chip);
- }
-
- if (chip->num_interfaces >= MAX_CARD_INTERFACES) {
- dev_info(&dev->dev, "Too many interfaces assigned to the single USB-audio card\n");
- err = -EINVAL;
- goto __error;
+ guard(mutex)(®ister_mutex);
+
+ ret = snd_usb_probe_unlocked(iface, usb_id, &usb_audio_driver);
+ chip = dev_get_drvdata(&udev->dev);
+ /* If chip isn't there, probe failed at the first interface. */
+ if (!chip)
+ return ret;
+ if (ret)
+ goto err_try_register;
+ if (!chip->card) {
+ ret = snd_usb_create_card(chip, iface);
+ if (ret)
+ goto err_no_register;
}
- dev_set_drvdata(&dev->dev, chip);
+ ret = snd_usb_add_pcms(chip);
+ if (ret)
+ goto err_try_register;
- if (ignore_ctl_error)
- chip->quirk_flags |= QUIRK_FLAG_IGNORE_CTL_ERROR;
-
- if (chip->quirk_flags & QUIRK_FLAG_DISABLE_AUTOSUSPEND)
- usb_disable_autosuspend(interface_to_usbdev(intf));
-
- /*
- * For devices with more than one control interface, we assume the
- * first contains the audio controls. We might need a more specific
- * check here in the future.
- */
- if (!chip->ctrl_intf)
- chip->ctrl_intf = alts;
-
- err = 1; /* continue */
- if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
- /* need some special handlings */
- err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk);
- if (err < 0)
- goto __error;
- }
-
- if (err > 0) {
- /* create normal USB audio interfaces */
- err = snd_usb_create_streams(chip, ifnum);
- if (err < 0)
- goto __error;
- err = snd_usb_create_mixer(chip, ifnum);
- if (err < 0)
- goto __error;
- }
+ ret = snd_usb_create_nonpcms(chip, iface, &usb_audio_driver);
+ if (ret)
+ goto err_try_register;
if (chip->need_delayed_register) {
- dev_info(&dev->dev,
- "Found post-registration device assignment: %08x:%02x\n",
+ dev_info(&udev->dev, "Found post-registration device assignment: %08x:%02x\n",
chip->usb_id, ifnum);
chip->need_delayed_register = false; /* clear again */
}
- err = try_to_register_card(chip, ifnum);
- if (err < 0)
- goto __error_no_register;
+ ret = try_to_register_card(chip, ifnum);
+ if (ret < 0)
+ goto err_no_register;
- /* don't want to fail when snd_media_device_create() fails */
- snd_media_device_create(chip, intf);
+ /* Media device is optional, ignore error checks. */
+ snd_media_device_create(chip, iface);
- if (quirk)
- chip->quirk_type = quirk->type;
-
- usb_chip[chip->index] = chip;
- chip->intf[chip->num_interfaces] = intf;
- chip->num_interfaces++;
- usb_set_intfdata(intf, chip);
atomic_dec(&chip->active);
- mutex_unlock(®ister_mutex);
return 0;
- __error:
- /* in the case of error in secondary interface, still try to register */
- if (chip)
- try_to_register_card(chip, ifnum);
+err_try_register:
+ /* In the case of error in secondary interface, still try to register. */
+ try_to_register_card(chip, ifnum);
- __error_no_register:
- if (chip) {
- /* chip->active is inside the chip->card object,
- * decrement before memory is possibly returned.
- */
- atomic_dec(&chip->active);
- if (!chip->num_interfaces) {
- snd_card_free(chip->card);
- snd_usb_chip_free(chip);
- }
- }
- mutex_unlock(®ister_mutex);
- return err;
+err_no_register:
+ atomic_dec(&chip->active);
+ snd_usb_chip_release(chip, iface);
+ return ret;
}
/*
@@ -1445,15 +1261,8 @@ static void usb_audio_disconnect(struct usb_interface *intf)
if (chip->quirk_flags & QUIRK_FLAG_DISABLE_AUTOSUSPEND)
usb_enable_autosuspend(interface_to_usbdev(intf));
- chip->num_interfaces--;
- if (chip->num_interfaces <= 0) {
- usb_chip[chip->index] = NULL;
- mutex_unlock(®ister_mutex);
- snd_card_free(card);
- snd_usb_chip_free(chip);
- } else {
- mutex_unlock(®ister_mutex);
- }
+ snd_usb_chip_release(chip, intf);
+ mutex_unlock(®ister_mutex);
}
/* lock the shutdown (disconnect) task and autoresume */
--
2.25.1
next prev parent reply other threads:[~2025-04-09 10:51 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-09 11:07 [RFC 00/15] ALSA/ASoC: USB Audio Offload Cezary Rojewski
2025-04-09 11:07 ` [RFC 01/15] ALSA: usb: Move media-filters to the media code Cezary Rojewski
2025-04-09 11:07 ` [RFC 02/15] ALSA: usb: Drop private_free() usage for card and pcms Cezary Rojewski
2025-04-09 11:07 ` [RFC 03/15] ALSA: usb: Relocate the usbaudio header file Cezary Rojewski
2025-04-09 11:07 ` [RFC 04/15] ALSA: usb: Implement two-stage quirk applying mechanism Cezary Rojewski
2025-04-09 11:07 ` [RFC 05/15] ALSA: usb: Implement two-stage stream creation mechanism Cezary Rojewski
2025-04-09 11:07 ` [RFC 06/15] ALSA: usb: Implement two-stage chip probing mechanism Cezary Rojewski
2025-04-09 11:07 ` Cezary Rojewski [this message]
2025-04-09 11:07 ` [RFC 08/15] ALSA: usb: Switch to the two-stage stream creation Cezary Rojewski
2025-04-09 11:07 ` [RFC 09/15] ALSA: usb: Switch to the two-stage quirk applying Cezary Rojewski
2025-04-09 11:07 ` [RFC 10/15] ALSA: usb: Export PCM operations Cezary Rojewski
2025-04-09 11:07 ` [RFC 11/15] ALSA: usb: Export usb_interface driver operations Cezary Rojewski
2025-04-09 11:07 ` [RFC 12/15] ALSA: usb: Export card-naming procedure Cezary Rojewski
2025-04-09 11:07 ` [RFC 13/15] ALSA: usb: Add getters to obtain endpoint information Cezary Rojewski
2025-04-09 11:07 ` [RFC 14/15] ASoC: codecs: Add USB-Audio driver Cezary Rojewski
2025-04-09 11:07 ` [RFC 15/15] ASoC: Intel: avs: Add USB machine board Cezary Rojewski
2025-04-09 12:10 ` [RFC 00/15] ALSA/ASoC: USB Audio Offload Greg KH
2025-04-09 13:06 ` Cezary Rojewski
2025-04-10 10:10 ` Takashi Iwai
2025-04-10 10:24 ` Takashi Iwai
2025-04-11 9:39 ` Cezary Rojewski
2025-04-15 16:15 ` Takashi Iwai
2025-04-17 10:15 ` Cezary Rojewski
2025-04-22 11:28 ` Pierre-Louis Bossart
2025-04-22 14:15 ` Cezary Rojewski
2025-04-25 16:53 ` Pierre-Louis Bossart
2025-04-11 14:04 ` Greg KH
2025-04-11 16:51 ` Cezary Rojewski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250409110731.3752332-8-cezary.rojewski@intel.com \
--to=cezary.rojewski@intel.com \
--cc=amadeuszx.slawinski@linux.intel.com \
--cc=broonie@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=linux-sound@vger.kernel.org \
--cc=mathias.nyman@linux.intel.com \
--cc=perex@perex.cz \
--cc=quic_wcheng@quicinc.com \
--cc=tiwai@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox