From b4fc332f0a86fd592e30e7a4df8140caa1d0f19f Mon Sep 17 00:00:00 2001 From: "Geoffrey D. Bennett" Date: Tue, 20 Feb 2024 01:44:31 +1030 Subject: [PATCH] Retrieve and store the device USB PID --- src/alsa.c | 28 ++++++++++++++++++++++++++++ src/alsa.h | 1 + 2 files changed, 29 insertions(+) diff --git a/src/alsa.c b/src/alsa.c index 612eda8..6fc55ee 100644 --- a/src/alsa.c +++ b/src/alsa.c @@ -564,6 +564,33 @@ static void alsa_subscribe(struct alsa_card *card) { snd_ctl_poll_descriptors(card->handle, &card->pfd, 1); } +static void alsa_get_usbid(struct alsa_card *card) { + char path[256]; + snprintf(path, 256, "/proc/asound/card%d/usbid", card->num); + + FILE *f = fopen(path, "r"); + if (!f) { + fprintf(stderr, "can't open %s: %s\n", path, strerror(errno)); + return; + } + + int vid, pid; + int result = fscanf(f, "%04x:%04x", &vid, &pid); + fclose(f); + + if (result != 2) { + fprintf(stderr, "can't read %s\n", path); + return; + } + + if (vid != 0x1235) { + fprintf(stderr, "VID %04x != expected 0x1235 for Focusrite\n", vid); + return; + } + + card->pid = pid; +} + // get the bus and device numbers from /proc/asound/cardxx/usbbus // format is XXX/YYY static int alsa_get_usbbus(struct alsa_card *card, int *bus, int *dev) { @@ -748,6 +775,7 @@ static void alsa_scan_cards(void) { alsa_get_elem_list(card); alsa_subscribe(card); + alsa_get_usbid(card); alsa_get_serial_number(card); if (card->serial) { diff --git a/src/alsa.h b/src/alsa.h index 3aaf92e..30c62a3 100644 --- a/src/alsa.h +++ b/src/alsa.h @@ -152,6 +152,7 @@ struct alsa_elem { struct alsa_card { int num; char *device; + uint32_t pid; char *serial; char *name; snd_ctl_t *handle;