Skip to content

Commit 30f68d0

Browse files
Rongronggg9tiwai
authored andcommitted
ALSA: usb-audio: Support string-descriptor-based quirk table entry
Some quirky devices do not have a unique VID/PID. Matching them using DEVICE_FLG() or VENDOR_FLG() may result in conflicts. Add two new macros DEVICE_STRING_FLG() and VENDOR_STRING_FLG() to match USB string descriptors (manufacturer and/or product) in addition to VID and/or PID, so that we can deconflict these devices safely. No functional change intended. Signed-off-by: Rong Zhang <i@rong.moe> Signed-off-by: Takashi Iwai <tiwai@suse.de> Link: https://patch.msgid.link/20260303194805.266158-5-i@rong.moe
1 parent 52dc4b1 commit 30f68d0

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

sound/usb/quirks.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
/*
33
*/
44

5+
#include <linux/cleanup.h>
6+
#include <linux/err.h>
57
#include <linux/init.h>
68
#include <linux/slab.h>
9+
#include <linux/string.h>
710
#include <linux/usb.h>
811
#include <linux/usb/audio.h>
912
#include <linux/usb/midi.h>
@@ -2135,16 +2138,39 @@ void snd_usb_audioformat_attributes_quirk(struct snd_usb_audio *chip,
21352138
/*
21362139
* driver behavior quirk flags
21372140
*/
2141+
struct usb_string_match {
2142+
const char *manufacturer;
2143+
const char *product;
2144+
};
2145+
21382146
struct usb_audio_quirk_flags_table {
21392147
u32 id;
21402148
u32 flags;
2149+
const struct usb_string_match *usb_string_match;
21412150
};
21422151

21432152
#define DEVICE_FLG(vid, pid, _flags) \
21442153
{ .id = USB_ID(vid, pid), .flags = (_flags) }
21452154
#define VENDOR_FLG(vid, _flags) DEVICE_FLG(vid, 0, _flags)
21462155

2156+
/* Use as a last resort if using DEVICE_FLG() is prone to VID/PID conflicts. */
2157+
#define DEVICE_STRING_FLG(vid, pid, _manufacturer, _product, _flags) \
2158+
{ \
2159+
.id = USB_ID(vid, pid), \
2160+
.usb_string_match = &(const struct usb_string_match) { \
2161+
.manufacturer = _manufacturer, \
2162+
.product = _product, \
2163+
}, \
2164+
.flags = (_flags), \
2165+
}
2166+
2167+
/* Use as a last resort if using VENDOR_FLG() is prone to VID conflicts. */
2168+
#define VENDOR_STRING_FLG(vid, _manufacturer, _flags) \
2169+
DEVICE_STRING_FLG(vid, 0, _manufacturer, NULL, _flags)
2170+
21472171
static const struct usb_audio_quirk_flags_table quirk_flags_table[] = {
2172+
/* Device and string descriptor matches */
2173+
21482174
/* Device matches */
21492175
DEVICE_FLG(0x001f, 0x0b21, /* AB13X USB Audio */
21502176
QUIRK_FLAG_FORCE_IFACE_RESET | QUIRK_FLAG_IFACE_DELAY),
@@ -2416,6 +2442,8 @@ static const struct usb_audio_quirk_flags_table quirk_flags_table[] = {
24162442
DEVICE_FLG(0x534d, 0x2109, /* MacroSilicon MS2109 */
24172443
QUIRK_FLAG_ALIGN_TRANSFER),
24182444

2445+
/* Vendor and string descriptor matches */
2446+
24192447
/* Vendor matches */
24202448
VENDOR_FLG(0x045e, /* MS Lifecam */
24212449
QUIRK_FLAG_GET_SAMPLE_RATE),
@@ -2560,14 +2588,64 @@ void snd_usb_apply_flag_dbg(const char *reason,
25602588
}
25612589
}
25622590

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

25672625
for (p = quirk_flags_table; p->id; p++) {
25682626
if (chip->usb_id == p->id ||
25692627
(!USB_ID_PRODUCT(p->id) &&
25702628
USB_ID_VENDOR(chip->usb_id) == USB_ID_VENDOR(p->id))) {
2629+
/* Handle DEVICE_STRING_FLG/VENDOR_STRING_FLG. */
2630+
if (p->usb_string_match && p->usb_string_match->manufacturer) {
2631+
if (!manufacturer) {
2632+
manufacturer = snd_usb_get_string(chip,
2633+
chip->dev->descriptor.iManufacturer);
2634+
}
2635+
if (IS_ERR_OR_NULL(manufacturer) ||
2636+
strcmp(p->usb_string_match->manufacturer, manufacturer))
2637+
continue;
2638+
}
2639+
if (p->usb_string_match && p->usb_string_match->product) {
2640+
if (!product) {
2641+
product = snd_usb_get_string(chip,
2642+
chip->dev->descriptor.iProduct);
2643+
}
2644+
if (IS_ERR_OR_NULL(product) ||
2645+
strcmp(p->usb_string_match->product, product))
2646+
continue;
2647+
}
2648+
25712649
snd_usb_apply_flag_dbg("builtin table", chip, p->flags);
25722650
chip->quirk_flags |= p->flags;
25732651
return;

0 commit comments

Comments
 (0)