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>
---
Changes in v2:
- Alloc string buffers with kmalloc() instead of on stack (thanks
Takashi Iwai)
- Get string descriptors in a neater way (ditto)
- Tiny differences compared to Takashi's sugeestion:
- Use `IS_ERR_OR_NULL() || strcmp()' instead of `!IS_ERR_OR_NULL() &&
strcmp()', so failure in getting the string descriptor won't
resulting in quirk flags being applied to irrelevant devices
- Use trivial goto cleanup patterns instead of `__free(kfree)' as the
latter can't handle ERR_PTR()
- Tiny differences compared to my previous reply:
- Use usb_string() as Takashi suggested instead of usb_cache_string(),
so that we can retrieve the errno and print it out on failure
---
sound/usb/quirks.c | 94 ++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 91 insertions(+), 3 deletions(-)
diff --git a/sound/usb/quirks.c b/sound/usb/quirks.c
index c6a78efbcaa30..8deeb9d247500 100644
--- a/sound/usb/quirks.c
+++ b/sound/usb/quirks.c
@@ -2,8 +2,10 @@
/*
*/
+#include <linux/err.h>
#include <linux/init.h>
#include <linux/slab.h>
+#include <linux/string.h>
#include <linux/usb.h>
#include <linux/usb/audio.h>
#include <linux/usb/midi.h>
@@ -2135,16 +2137,39 @@ void snd_usb_audioformat_attributes_quirk(struct snd_usb_audio *chip,
/*
* driver behavior quirk flags
*/
+struct usb_string_match {
+ const char *manufacturer;
+ const char *product;
+};
+
struct usb_audio_quirk_flags_table {
u32 id;
u32 flags;
+ const struct usb_string_match *usb_string_match;
};
#define DEVICE_FLG(vid, pid, _flags) \
{ .id = USB_ID(vid, pid), .flags = (_flags) }
#define VENDOR_FLG(vid, _flags) DEVICE_FLG(vid, 0, _flags)
+/* Use as a last resort if using DEVICE_FLG() is prone to VID/PID conflicts. */
+#define DEVICE_STRING_FLG(vid, pid, _manufacturer, _product, _flags) \
+{ \
+ .id = USB_ID(vid, pid), \
+ .usb_string_match = &(const struct usb_string_match) { \
+ .manufacturer = _manufacturer, \
+ .product = _product, \
+ }, \
+ .flags = (_flags), \
+}
+
+/* Use as a last resort if using VENDOR_FLG() is prone to VID conflicts. */
+#define VENDOR_STRING_FLG(vid, _manufacturer, _flags) \
+ DEVICE_STRING_FLG(vid, 0, _manufacturer, NULL, _flags)
+
static const struct usb_audio_quirk_flags_table quirk_flags_table[] = {
+ /* Device and string descriptor matches */
+
/* Device matches */
DEVICE_FLG(0x001f, 0x0b21, /* AB13X USB Audio */
QUIRK_FLAG_FORCE_IFACE_RESET | QUIRK_FLAG_IFACE_DELAY),
@@ -2414,6 +2439,8 @@ static const struct usb_audio_quirk_flags_table quirk_flags_table[] = {
DEVICE_FLG(0x534d, 0x2109, /* MacroSilicon MS2109 */
QUIRK_FLAG_ALIGN_TRANSFER),
+ /* Vendor and string descriptor matches */
+
/* Vendor matches */
VENDOR_FLG(0x045e, /* MS Lifecam */
QUIRK_FLAG_GET_SAMPLE_RATE),
@@ -2558,19 +2585,80 @@ void snd_usb_apply_flag_dbg(const char *reason,
}
}
+#define USB_STRING_SIZE 128
+
+static char *snd_usb_get_string(struct snd_usb_audio *chip, int id)
+{
+ char *buf;
+ int ret;
+
+ /*
+ * Devices without the corresponding string descriptor.
+ * This is non-fatal as *_STRING_FLG have nothing to do in this case.
+ */
+ if (id == 0)
+ return ERR_PTR(-ENODATA);
+
+ buf = kmalloc(USB_STRING_SIZE, GFP_KERNEL);
+ if (buf == NULL)
+ return ERR_PTR(-ENOMEM);
+
+ ret = usb_string(chip->dev, id, buf, USB_STRING_SIZE);
+ if (ret < 0) {
+ usb_audio_warn(chip, "failed to get string for id%d: %d\n", id, ret);
+ kfree(buf);
+ return ERR_PTR(ret);
+ }
+
+ return buf;
+}
+
void snd_usb_init_quirk_flags_table(struct snd_usb_audio *chip)
{
const struct usb_audio_quirk_flags_table *p;
+ char *manufacturer = NULL;
+ char *product = NULL;
for (p = quirk_flags_table; p->id; p++) {
if (chip->usb_id == p->id ||
(!USB_ID_PRODUCT(p->id) &&
USB_ID_VENDOR(chip->usb_id) == USB_ID_VENDOR(p->id))) {
- snd_usb_apply_flag_dbg("builtin table", chip, p->flags);
- chip->quirk_flags |= p->flags;
- return;
+ if (!p->usb_string_match)
+ goto apply; /* DEVICE_FLG or VENDOR_FLG */
+
+ /* DEVICE_STRING_FLG or VENDOR_STRING_FLG */
+ if (p->usb_string_match->manufacturer) {
+ if (!manufacturer) {
+ manufacturer = snd_usb_get_string(chip,
+ chip->dev->descriptor.iManufacturer);
+ }
+ if (IS_ERR_OR_NULL(manufacturer) ||
+ strcmp(p->usb_string_match->manufacturer, manufacturer))
+ continue;
+ }
+ if (p->usb_string_match->product) {
+ if (!product) {
+ product = snd_usb_get_string(chip,
+ chip->dev->descriptor.iProduct);
+ }
+ if (IS_ERR_OR_NULL(product) ||
+ strcmp(p->usb_string_match->product, product))
+ continue;
+ }
+ goto apply;
}
}
+ goto clean;
+
+apply:
+ snd_usb_apply_flag_dbg("builtin table", chip, p->flags);
+ chip->quirk_flags |= p->flags;
+
+clean:
+ if (!IS_ERR_OR_NULL(manufacturer))
+ kfree(manufacturer);
+ if (!IS_ERR_OR_NULL(product))
+ kfree(product);
}
void snd_usb_init_quirk_flags_parse_string(struct snd_usb_audio *chip,
--
2.51.0