Skip to content

Commit 9558a2c

Browse files
Rongronggg9tiwai
authored andcommitted
ALSA: usb-audio: Refine string-descriptor-based quirk matching
Remove snd_usb_get_string() and use the manufacturer and product strings stored in struct usb_device directly to match quirk table entries. Their NULLity can be checked to determine if the device has no these strings. This simplifies the code a lot. Meanwhile, allow quirk table entries to match "no string" explicitly, and add appropriate comments to show the expected usages of DEVICE_STRING_FLG() and VENDOR_STRING_FLG(). These changes are tiny and doesn't form another separate patch, so that back-and-forth changes can be avoided. Suggested-by: Terry Junge <linuxsound@cosmicgizmosystems.com> Link: https://lore.kernel.org/r/b59da54a-9c80-4212-a337-c5ea98da52d1@cosmicgizmosystems.com Signed-off-by: Rong Zhang <i@rong.moe> Link: https://patch.msgid.link/20260305174711.1106324-1-i@rong.moe Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent b364a0d commit 9558a2c

1 file changed

Lines changed: 40 additions & 50 deletions

File tree

sound/usb/quirks.c

Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,7 +2153,28 @@ struct usb_audio_quirk_flags_table {
21532153
{ .id = USB_ID(vid, pid), .flags = (_flags) }
21542154
#define VENDOR_FLG(vid, _flags) DEVICE_FLG(vid, 0, _flags)
21552155

2156-
/* Use as a last resort if using DEVICE_FLG() is prone to VID/PID conflicts. */
2156+
/*
2157+
* Use as a last resort if using DEVICE_FLG() is prone to VID/PID conflicts.
2158+
*
2159+
* Usage:
2160+
* // match vid, pid, "manufacturer", and "product"
2161+
* DEVICE_STRING_FLG(vid, pid, "manufacturer", "product", flags)
2162+
*
2163+
* // match vid, pid, "manufacturer", and any product string
2164+
* DEVICE_STRING_FLG(vid, pid, "manufacturer", NULL, flags)
2165+
*
2166+
* // match vid, pid, "manufacturer", and device must have no product string
2167+
* DEVICE_STRING_FLG(vid, pid, "manufacturer", "", flags)
2168+
*
2169+
* // match vid, pid, any manufacturer string, and "product"
2170+
* DEVICE_STRING_FLG(vid, pid, NULL, "product", flags)
2171+
*
2172+
* // match vid, pid, no manufacturer string, and "product"
2173+
* DEVICE_STRING_FLG(vid, pid, "", "product", flags)
2174+
*
2175+
* // match vid, pid, no manufacturer string, and no product string
2176+
* DEVICE_STRING_FLG(vid, pid, "", "", flags)
2177+
*/
21572178
#define DEVICE_STRING_FLG(vid, pid, _manufacturer, _product, _flags) \
21582179
{ \
21592180
.id = USB_ID(vid, pid), \
@@ -2164,7 +2185,16 @@ struct usb_audio_quirk_flags_table {
21642185
.flags = (_flags), \
21652186
}
21662187

2167-
/* Use as a last resort if using VENDOR_FLG() is prone to VID conflicts. */
2188+
/*
2189+
* Use as a last resort if using VENDOR_FLG() is prone to VID conflicts.
2190+
*
2191+
* Usage:
2192+
* // match vid, and "manufacturer"
2193+
* VENDOR_STRING_FLG(vid, "manufacturer", flags)
2194+
*
2195+
* // match vid, and device must have no manufacturer string
2196+
* VENDOR_STRING_FLG(vid, "", flags)
2197+
*/
21682198
#define VENDOR_STRING_FLG(vid, _manufacturer, _flags) \
21692199
DEVICE_STRING_FLG(vid, 0, _manufacturer, NULL, _flags)
21702200

@@ -2595,63 +2625,23 @@ void snd_usb_apply_flag_dbg(const char *reason,
25952625
}
25962626
}
25972627

2598-
#define USB_STRING_SIZE 128
2599-
2600-
static char *snd_usb_get_string(struct snd_usb_audio *chip, int id)
2601-
{
2602-
char *buf;
2603-
int ret;
2604-
2605-
/*
2606-
* Devices without the corresponding string descriptor.
2607-
* This is non-fatal as *_STRING_FLG have nothing to do in this case.
2608-
*/
2609-
if (id == 0)
2610-
return ERR_PTR(-ENODATA);
2611-
2612-
buf = kmalloc(USB_STRING_SIZE, GFP_KERNEL);
2613-
if (buf == NULL)
2614-
return ERR_PTR(-ENOMEM);
2615-
2616-
ret = usb_string(chip->dev, id, buf, USB_STRING_SIZE);
2617-
if (ret < 0) {
2618-
usb_audio_warn(chip, "failed to get string for id%d: %d\n", id, ret);
2619-
kfree(buf);
2620-
return ERR_PTR(ret);
2621-
}
2622-
2623-
return buf;
2624-
}
2625-
26262628
void snd_usb_init_quirk_flags_table(struct snd_usb_audio *chip)
26272629
{
26282630
const struct usb_audio_quirk_flags_table *p;
2629-
char *manufacturer __free(kfree) = NULL;
2630-
char *product __free(kfree) = NULL;
26312631

26322632
for (p = quirk_flags_table; p->id; p++) {
26332633
if (chip->usb_id == p->id ||
26342634
(!USB_ID_PRODUCT(p->id) &&
26352635
USB_ID_VENDOR(chip->usb_id) == USB_ID_VENDOR(p->id))) {
26362636
/* Handle DEVICE_STRING_FLG/VENDOR_STRING_FLG. */
2637-
if (p->usb_string_match && p->usb_string_match->manufacturer) {
2638-
if (!manufacturer) {
2639-
manufacturer = snd_usb_get_string(chip,
2640-
chip->dev->descriptor.iManufacturer);
2641-
}
2642-
if (IS_ERR_OR_NULL(manufacturer) ||
2643-
strcmp(p->usb_string_match->manufacturer, manufacturer))
2644-
continue;
2645-
}
2646-
if (p->usb_string_match && p->usb_string_match->product) {
2647-
if (!product) {
2648-
product = snd_usb_get_string(chip,
2649-
chip->dev->descriptor.iProduct);
2650-
}
2651-
if (IS_ERR_OR_NULL(product) ||
2652-
strcmp(p->usb_string_match->product, product))
2653-
continue;
2654-
}
2637+
if (p->usb_string_match && p->usb_string_match->manufacturer &&
2638+
strcmp(p->usb_string_match->manufacturer,
2639+
chip->dev->manufacturer ? chip->dev->manufacturer : ""))
2640+
continue;
2641+
if (p->usb_string_match && p->usb_string_match->product &&
2642+
strcmp(p->usb_string_match->product,
2643+
chip->dev->product ? chip->dev->product : ""))
2644+
continue;
26552645

26562646
snd_usb_apply_flag_dbg("builtin table", chip, p->flags);
26572647
chip->quirk_flags |= p->flags;

0 commit comments

Comments
 (0)