From nobody Mon Sep 28 16:21:57 2026 Received: from mail-lf1-f43.google.com (mail-lf1-f43.google.com [209.85.167.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A8FD3806D9 for ; Thu, 20 Aug 2026 15:13:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787238820; cv=none; b=QaxgpwFAoXlrhR/eidSX0DTlt9n4WhUwsqN1Zn8YKG27XwWGo/KFlZUh9AeiG0yJcLmLVHmPtRp8czB7mXgsNmHOR98YJllFpOXEQ4hejp18cO2rOOyiOsq7RjrrwhcoOlNmI4HV3k3ovXgfcN7Fz2m8aTdkKylUACF4pkKCUG8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787238820; c=relaxed/simple; bh=iuOjn88/y2q4/YXmSIdZVxZZMpJ9ywsFkZqHn0JyaEs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RGxjL+vBAn7xjutTGkEEhn5btW5u7bV64KgPIXobElqftDUvckKFTUWaDnSbw69iI/FaXjkF+mhbckhJrpSOn1Qxa5xngVrIkkuU0GaKTCQzc5cXbDqGMSIionmdLJCC9Dy4tdZZLJcwEwYLE0386I4gH9CFlx1Ps6rFlnMpA70= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=D5mJn2jG; arc=none smtp.client-ip=209.85.167.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="D5mJn2jG" Received: by mail-lf1-f43.google.com with SMTP id 2adb3069b0e04-5aec201b582so2839191e87.1 for ; Thu, 20 Aug 2026 08:13:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787238816; x=1787843616; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=NF7wemyY30EzgreaI7YleJoOIw/VWy4wZwty8ifnPUQ=; b=D5mJn2jGYAmM/jO8vDaO45XxBleiBw5+bEQGgKrTQBzX6idyEDpWiZW82sJ8kIoGtB /sJyT3mK7Azc6y39ZUcFVhK0PwTIIc3AOR5Dh0E+ofX4ktAPZDxc4BrDI/dqzic1m/7U Z/RIn2MnTpEwPTFYDK8QBVBveqnAL1pJe9/UlR9T3bUrADUG7iLYZEZWqlkYMsOPR3b5 92FSq+8aDrbHzo/RoFR4o07+DbJmgjoJLpBjFX2z7i+/DyhU2drmjVrF7BkhYtrPjGvV kudB025V/irppouSIMIp7dr/kQQIAH81/QK1tqbAzvkklXUGbM4YEe3GJ/TM+KF+cmBe GAtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787238816; x=1787843616; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=NF7wemyY30EzgreaI7YleJoOIw/VWy4wZwty8ifnPUQ=; b=iyWD98LLwJm3OIgPApb46QJFL8zEWQW79E03+UdGgwC2yaZ9uTgNFLtd+JDl/hyPn0 i2ypYIhuBkf30gACqJGY1W9X3JKZX//IzEhn33Ege3o5g3fGhOVawz5jfY4LVmkTVv8T 1/VN4fhV01eL6jUjPe193sTHMA1feqdb+CDG1SL8sc2UPJ3KlOU0vz036W438rLPxBr5 4eoXSL3soZ9HYCuinb87rNUKNmJvy3CuZrqugSbDDf7JfpuuEH+pDzzZIt2Zx/ysxdnn coqePjABqQMt4ukM3vjKkUgcMCaYYt+PeO4YItr3zJ/GisQWHrU4Wzu75utOpptv5aD5 oxUA== X-Forwarded-Encrypted: i=1; AHgh+RobEFIPYBmPMmmf8rCNPG0tSV9nJNZxgjGQDjbitovPhBryfmGRNPwExKAAcJLZUrQHxDcSrHx0xp99aDU=@vger.kernel.org X-Gm-Message-State: AOJu0YxHbSPlBECr2cdWUTHXamz8qaKl1Fw6JDm2nZjnPwP8ilIntGvw zt6J5fnVDMDij1xMrCzwSzWnIuad4WsJRzqLfmDkPBACxrF1uLLe1uQA X-Gm-Gg: AR+sD11Tn90p/B7jo0Pk+Yd6s8uxU4o9lG/OoQO+fne61lfu3dki2Mm7uWejQvO/ITq HEzwhPMMNNCalfUJ0dxlhXMiq6lyBsaWsQSIWLyHtUnrBWHxX1Bq7z7yyp23524I0qgfzp/qM3v 6Y8uGoVCKxjO1dgkORYrHcGPiRLf3tjP44JcAvY5ZOqpV0pEovtA12UnATQRAADBLkkqK12F5KU n/NP/Pi3HinmVkrKolu+CK2Iy34SK4zo1a8XCGH+Knb4flgKlIlwsoxdqqoRkTUs4o3z2oYw6WA ocO5Qz59mT8DXoW/MbFWErWyhzaxW7md+QsyspTpL/QBnH+xvHUnk4G+3d0psQ9zFGbYxWdRjZg 7AA3obqBqhE4Y4DdP6AtR6UMFm8Nj7QIzoqBzBK6gKAZtAih/ArrTiqY9+CfFyT6D8uon+pBBE3 dhasgTRkAhS/z7wcn6pH2EMu7dogrfHXl8o/KrdeNFKxauaZG7bAERV8a+cGTf8E3dtkQ/EWnBg nUK/U+BX7BZK4wdQoFPJKxqJOrC6wiwAY7kDzVGUzjLRVVbPrHyeFYXtk4N X-Received: by 2002:a05:6512:b94:b0:5b2:a21a:10e7 with SMTP id 2adb3069b0e04-5b478bf4f69mr4973122e87.25.1787238815833; Thu, 20 Aug 2026 08:13:35 -0700 (PDT) Received: from localhost ([188.234.148.119]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5b4788f5a60sm1333617e87.77.2026.08.20.08.13.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Aug 2026 08:13:35 -0700 (PDT) From: Mikhail Gavrilov To: tiwai@suse.com, jikos@kernel.org, bentiss@kernel.org Cc: perex@perex.cz, linux-sound@vger.kernel.org, linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, Mikhail Gavrilov Subject: [RFC 1/2] ALSA: usb-audio: expose the Topping M62's analogue gains as mixer controls Date: Thu, 20 Aug 2026 20:13:28 +0500 Message-ID: <20260820151329.18332-2-mikhail.v.gavrilov@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260820151329.18332-1-mikhail.v.gavrilov@gmail.com> References: <87y0eaxz39.wl-tiwai@suse.de> <20260820151329.18332-1-mikhail.v.gavrilov@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The M62 (152a:875c) keeps its analogue input gains and its output volumes behind a vendor protocol on a HID-class interface, and exposes none of them through UAC. What UAC does offer on the capture side is a digital trim after the converter, which cannot buy signal-to-noise: raising it lifts the converter's own floor along with the signal. A noise-floor ladder against the card shows exactly that, so on Linux today the one knob worth setting is the one that cannot be reached, and a measurement has to begin by asking a human to touch the front panel. The protocol was read off the vendor application's traffic, the way mixer_scarlett2.c describes reading Focusrite's. Frames are fifteen bytes -- start magic, a constant, a target, a property, a signed 32-bit big-endian value, CRC-16/MODBUS over the middle stored big-endian, end magic -- and rebuilding all 2619 captured frames from that description reproduces them byte for byte. The device says nothing until it is subscribed; one write starts the stream, a second makes it announce its whole state, after which every change arrives unsolicited, including a front panel press. So the controls are populated by asking rather than by caching what was written, which matters here because the vendor application on another host pushes its own cached state onto the card on connect. The control pipe cannot carry this: GET_REPORT and SET_REPORT stall with EPIPE for every report type, so the interrupt endpoints on the HID interface are the only route and this driver has to own that interface. hid_ignore_list keeps usbhid away. Nothing is lost by that: the report descriptor the device offers is a fig leaf -- a Generic Desktop application collection, eight unnamed usages, sixteen bytes in and out, no report ID -- so hid-generic can only make a nonexistent mouse of it. The controls are a table: a name, the target and property that carry the knob, the second target that must be written in step with it, the range and the scale. Adding a knob is adding a row. Six rows here -- the two microphone preamps in whole decibels, AUX and Bluetooth on the input side, headphone and OTG on the output side -- and the outputs come in pairs because the device answers on only one of each pair and the other would drift away unheard. The two volume tapers are measured, not guessed: index 0 is mute, index 99 the maximum, the step is 0.5 dB above -10 dB and 1 dB below it, and the family that must cover 97 dB in 98 steps takes 2 dB below -52 dB as well. Both express as DB_RANGE. The microphone preamps are ordinary 1 dB steps from 0 to 88. One thing a mixer quirk cannot do for itself: usb_audio_driver is private to card.c, so claiming an interface the audio class knows nothing about needs a helper there. snd_usb_claim_iface() is that helper, and it is the only change outside the new file and its dispatch. Not included: OTG IN, which has no front panel control and therefore never announced itself in any capture, so its property is unknown. It is one row when it is known. Signed-off-by: Mikhail Gavrilov --- MAINTAINERS | 6 + drivers/hid/hid-ids.h | 3 + drivers/hid/hid-quirks.c | 2 + sound/usb/Makefile | 1 + sound/usb/card.c | 14 ++ sound/usb/mixer_quirks.c | 5 + sound/usb/mixer_topping.c | 495 ++++++++++++++++++++++++++++++++++++++ sound/usb/mixer_topping.h | 7 + sound/usb/usbaudio.h | 3 + 9 files changed, 536 insertions(+) create mode 100644 sound/usb/mixer_topping.c create mode 100644 sound/usb/mixer_topping.h diff --git a/MAINTAINERS b/MAINTAINERS index d2b7ed2..41ec6b7 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -27349,6 +27349,12 @@ S: Maintained W: https://tomoyo.sourceforge.net/ F: security/tomoyo/ =20 +TOPPING M62 MIXER DRIVER +M: Mikhail Gavrilov +L: linux-sound@vger.kernel.org +S: Maintained +F: sound/usb/mixer_topping.* + TOPSTAR LAPTOP EXTRAS DRIVER M: Herton Ronaldo Krzesinski L: platform-driver-x86@vger.kernel.org diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h index 341bf58..092b2a9 100644 --- a/drivers/hid/hid-ids.h +++ b/drivers/hid/hid-ids.h @@ -1470,6 +1470,9 @@ #define USB_DEVICE_ID_TIVO_SLIDE 0x1201 #define USB_DEVICE_ID_TIVO_SLIDE_PRO 0x1203 =20 +#define USB_VENDOR_ID_TOPPING 0x152a +#define USB_DEVICE_ID_TOPPING_M62 0x875c + #define USB_VENDOR_ID_TOPRE 0x0853 #define USB_DEVICE_ID_TOPRE_REALFORCE_R2_108 0x0148 #define USB_DEVICE_ID_TOPRE_REALFORCE_R2_87 0x0146 diff --git a/drivers/hid/hid-quirks.c b/drivers/hid/hid-quirks.c index 8a0b51d..3c156d1 100644 --- a/drivers/hid/hid-quirks.c +++ b/drivers/hid/hid-quirks.c @@ -981,6 +981,8 @@ static const struct hid_device_id hid_ignore_list[] =3D= { { HID_USB_DEVICE(USB_VENDOR_ID_SYNAPTICS, USB_DEVICE_ID_SYNAPTICS_WTP) }, { HID_USB_DEVICE(USB_VENDOR_ID_SYNAPTICS, USB_DEVICE_ID_SYNAPTICS_DPAD) }, #endif + /* the M62's vendor control channel, driven by snd-usb-audio */ + { HID_USB_DEVICE(USB_VENDOR_ID_TOPPING, USB_DEVICE_ID_TOPPING_M62) }, { HID_USB_DEVICE(USB_VENDOR_ID_YEALINK, USB_DEVICE_ID_YEALINK_P1K_P4K_B2K= ) }, { HID_USB_DEVICE(USB_VENDOR_ID_QUANTA, USB_DEVICE_ID_QUANTA_HP_5MP_CAMERA= _5473) }, { } diff --git a/sound/usb/Makefile b/sound/usb/Makefile index e62794a..151b481 100644 --- a/sound/usb/Makefile +++ b/sound/usb/Makefile @@ -14,6 +14,7 @@ snd-usb-audio-y :=3D card.o \ mixer_quirks.o \ mixer_scarlett.o \ mixer_scarlett2.o \ + mixer_topping.o \ mixer_us16x08.o \ mixer_s1810c.o \ pcm.o \ diff --git a/sound/usb/card.c b/sound/usb/card.c index 24112e4..e99b934 100644 --- a/sound/usb/card.c +++ b/sound/usb/card.c @@ -325,6 +325,20 @@ static int snd_usb_create_stream(struct snd_usb_audio = *chip, int ctrlif, int int return 0; } =20 +/* + * Claim an interface of this device for snd-usb-audio. + * + * A mixer quirk may need an interface the audio class knows nothing + * about -- a vendor control channel that happens to wear the HID class, + * for instance -- and cannot claim it itself, because usb_audio_driver + * is private to this file. + */ +int snd_usb_claim_iface(struct snd_usb_audio *chip, struct usb_interface *= iface) +{ + return usb_driver_claim_interface(&usb_audio_driver, iface, + USB_AUDIO_IFACE_UNUSED); +} + /* * parse audio control descriptor and create pcm/midi streams */ diff --git a/sound/usb/mixer_quirks.c b/sound/usb/mixer_quirks.c index a1f5592..10f3302 100644 --- a/sound/usb/mixer_quirks.c +++ b/sound/usb/mixer_quirks.c @@ -36,6 +36,7 @@ #include "mixer_quirks.h" #include "mixer_scarlett.h" #include "mixer_scarlett2.h" +#include "mixer_topping.h" #include "mixer_us16x08.h" #include "mixer_s1810c.h" #include "helper.h" @@ -4531,6 +4532,10 @@ int snd_usb_mixer_apply_create_quirk(struct usb_mixe= r_interface *mixer) err =3D snd_fcp_init(mixer); break; =20 + case USB_ID(0x152a, 0x875c): /* Topping M62 */ + err =3D snd_topping_init(mixer); + break; + case USB_ID(0x041e, 0x323b): /* Creative Sound Blaster E1 */ err =3D snd_soundblaster_e1_switch_create(mixer); break; diff --git a/sound/usb/mixer_topping.c b/sound/usb/mixer_topping.c new file mode 100644 index 0000000..0c89a51 --- /dev/null +++ b/sound/usb/mixer_topping.c @@ -0,0 +1,495 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Mixer controls for Topping interfaces behind a vendor HID channel + * + * Copyright (c) 2026 Mikhail Gavrilov + * + * The M62 (152a:875c) puts its analogue input gains and its output + * volumes behind a vendor protocol on a HID-class interface, and + * exposes nothing of them through UAC. What UAC does expose on the + * capture side is a digital trim AFTER the converter, which cannot buy + * signal-to-noise: raising it lifts the converter's own floor with the + * signal. So the only knob worth automating is unreachable, and a + * measurement application on Linux has to ask a human to set it by + * hand on the front panel. + * + * The protocol was read off the vendor application's traffic. Frames + * are fifteen bytes: + * + * 22 33 | 20 01 01 | TT | PP | s32 value BE | CRC16 BE | 66 77 + * + * with TT a target (an input, an output, or the device itself), PP a + * property of that target, and the checksum CRC-16/MODBUS over bytes + * 2..10 stored most significant byte first. Reports arriving from the + * device are the same frame plus one trailing pad byte; an idle poll + * returns sixteen zeroes. The vendor application sends 00 00 in place + * of the checksum and the device accepts it, so the device evidently + * does not verify what it receives -- this driver signs its writes + * anyway, and validates what it reads. + * + * The device says nothing until it is subscribed: one write of + * 0x11/0x24 starts the notification stream, after which every change, + * including a front panel button, arrives unsolicited. A second + * write, 0x11/0x26, makes the device announce its whole state, which + * is how the controls are populated without caching what we wrote. + * + * Note that the control pipe is not an option here: GET_REPORT and + * SET_REPORT both stall with EPIPE for every report type, so the + * interrupt endpoints on the HID interface are the only route and this + * driver has to own that interface. hid_ignore_list keeps usbhid off + * it; the report descriptor it would bind to describes nothing anyway + * (a Generic Desktop application collection with eight unnamed usages + * and no report ID), so no HID functionality is lost. + */ + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "usbaudio.h" +#include "mixer.h" +#include "mixer_topping.h" + +#define TOPPING_FRAME_LEN 15 /* what we send */ +#define TOPPING_REPORT_LEN 16 /* what arrives, one pad byte more */ +#define TOPPING_EP_BUF 64 /* the endpoints' packet size */ + +/* device-scope properties */ +#define TOPPING_TT_DEVICE 0x11 +#define TOPPING_PP_SUBSCRIBE 0x24 +#define TOPPING_PP_ANNOUNCE 0x26 + +/* + * The two volume tapers, measured against the vendor application's own + * readout: index 0 is always mute, index 99 always the maximum, the + * step is 0.5 dB above -10 dB and 1 dB below it, and the family that + * has to cover 97 dB in 98 steps takes 2 dB below -52 dB as well. + */ +static const DECLARE_TLV_DB_SCALE(topping_tlv_gain, 0, 100, 0); + +static const unsigned int topping_tlv_out_9[] =3D { + TLV_DB_RANGE_HEAD(4), + 0, 0, SNDRV_CTL_TLVD_DB_SCALE_ITEM(SNDRV_CTL_TLVD_DB_GAIN_MUTE, 0, 1), + 1, 19, SNDRV_CTL_TLVD_DB_SCALE_ITEM(-8800, 200, 0), + 20, 61, SNDRV_CTL_TLVD_DB_SCALE_ITEM(-5100, 100, 0), + 62, 99, SNDRV_CTL_TLVD_DB_SCALE_ITEM(-950, 50, 0), +}; + +static const unsigned int topping_tlv_out_0[] =3D { + TLV_DB_RANGE_HEAD(3), + 0, 0, SNDRV_CTL_TLVD_DB_SCALE_ITEM(SNDRV_CTL_TLVD_DB_GAIN_MUTE, 0, 1), + 1, 79, SNDRV_CTL_TLVD_DB_SCALE_ITEM(-8800, 100, 0), + 80, 99, SNDRV_CTL_TLVD_DB_SCALE_ITEM(-950, 50, 0), +}; + +/* + * One row per knob. A row is the whole description of a control: what + * to call it, which target and property carry it, the second target + * that has to be written in step with the first, the range, and the + * scale. Adding a knob is adding a row. + * + * The outputs come in pairs and the device announces only the second + * of each pair, so both are written and the second is the one listened + * for. OTG IN is missing on purpose: it has no front panel control, + * so it never announced itself in any capture, and its property is not + * known. It is one row when it is. + */ +struct topping_ctl_desc { + const char *name; + u8 target; /* the target that reports */ + u8 target_pair; /* written too, or 0 */ + u8 prop; + int min, max; + const unsigned int *tlv; +}; + +static const struct topping_ctl_desc topping_m62_ctls[] =3D { + { "Mic-1 Analog Capture Volume", 0x21, 0, 0x04, 0, 88, + topping_tlv_gain }, + { "Mic-2 Analog Capture Volume", 0x22, 0, 0x04, 0, 88, + topping_tlv_gain }, + { "Aux Capture Volume", 0x23, 0, 0x04, 0, 99, + topping_tlv_out_9 }, + { "Bluetooth Capture Volume", 0x25, 0, 0x04, 0, 99, + topping_tlv_out_0 }, + { "Headphone Playback Volume", 0x64, 0x63, 0x03, 0, 99, + topping_tlv_out_9 }, + { "OTG Playback Volume", 0x62, 0x61, 0x03, 0, 99, + topping_tlv_out_0 }, +}; + +struct topping_mixer { + struct usb_mixer_interface *mixer; + struct usb_interface *iface; + const struct topping_ctl_desc *ctls; + int num_ctls; + struct urb *urb; + u8 *inbuf; + dma_addr_t inbuf_dma; + unsigned int pipe_in, pipe_out; + int interval; + spinlock_t lock; /* guards val[] against the URB */ + int *val; + struct snd_kcontrol **kctl; +}; + +static void topping_build(u8 *f, u8 target, u8 prop, s32 value) +{ + u16 crc; + + f[0] =3D 0x22; + f[1] =3D 0x33; + f[2] =3D 0x20; + f[3] =3D 0x01; + f[4] =3D 0x01; + f[5] =3D target; + f[6] =3D prop; + put_unaligned_be32(value, f + 7); + crc =3D crc16(0xffff, f + 2, 9); + put_unaligned_be16(crc, f + 11); + f[13] =3D 0x66; + f[14] =3D 0x77; +} + +static int topping_send(struct topping_mixer *tm, u8 target, u8 prop, + s32 value) +{ + u8 *buf; + int err, actual; + + buf =3D kzalloc(TOPPING_EP_BUF, GFP_KERNEL); + if (!buf) + return -ENOMEM; + topping_build(buf, target, prop, value); + err =3D usb_interrupt_msg(tm->mixer->chip->dev, tm->pipe_out, + buf, TOPPING_FRAME_LEN, &actual, 1000); + kfree(buf); + if (err < 0) + usb_audio_err(tm->mixer->chip, + "Topping: write %02x/%02x failed: %d\n", + target, prop, err); + return err; +} + +/* -1 when this frame is not one of ours */ +static int topping_index_of(struct topping_mixer *tm, u8 target, u8 prop) +{ + int i; + + for (i =3D 0; i < tm->num_ctls; i++) + if (tm->ctls[i].target =3D=3D target && tm->ctls[i].prop =3D=3D prop) + return i; + return -1; +} + +static void topping_urb_complete(struct urb *urb) +{ + struct topping_mixer *tm =3D urb->context; + const u8 *f =3D urb->transfer_buffer; + unsigned long flags; + int idx, value, err; + + if (urb->status) + return; /* resubmitted below only when running */ + if (urb->actual_length < TOPPING_FRAME_LEN) + goto resubmit; + if (f[0] !=3D 0x22 || f[1] !=3D 0x33 || f[13] !=3D 0x66 || f[14] !=3D 0x7= 7) + goto resubmit; + if (get_unaligned_be16(f + 11) !=3D crc16(0xffff, f + 2, 9)) + goto resubmit; + + idx =3D topping_index_of(tm, f[5], f[6]); + if (idx < 0) + goto resubmit; /* a meter, or something unnamed */ + + value =3D get_unaligned_be32(f + 7); + if (value < tm->ctls[idx].min || value > tm->ctls[idx].max) + goto resubmit; + + spin_lock_irqsave(&tm->lock, flags); + if (tm->val[idx] =3D=3D value) { + spin_unlock_irqrestore(&tm->lock, flags); + goto resubmit; + } + tm->val[idx] =3D value; + spin_unlock_irqrestore(&tm->lock, flags); + + if (tm->kctl[idx]) + snd_ctl_notify(tm->mixer->chip->card, + SNDRV_CTL_EVENT_MASK_VALUE, + &tm->kctl[idx]->id); + +resubmit: + err =3D usb_submit_urb(urb, GFP_ATOMIC); + if (err < 0 && err !=3D -ENODEV && err !=3D -ESHUTDOWN) + usb_audio_err(tm->mixer->chip, + "Topping: cannot resubmit: %d\n", err); +} + +static int topping_ctl_info(struct snd_kcontrol *kctl, + struct snd_ctl_elem_info *uinfo) +{ + struct usb_mixer_elem_info *elem =3D kctl->private_data; + struct topping_mixer *tm =3D elem->head.mixer->private_data; + int idx =3D elem->control; + + uinfo->type =3D SNDRV_CTL_ELEM_TYPE_INTEGER; + uinfo->count =3D 1; + uinfo->value.integer.min =3D tm->ctls[idx].min; + uinfo->value.integer.max =3D tm->ctls[idx].max; + uinfo->value.integer.step =3D 1; + return 0; +} + +static int topping_ctl_get(struct snd_kcontrol *kctl, + struct snd_ctl_elem_value *ucontrol) +{ + struct usb_mixer_elem_info *elem =3D kctl->private_data; + struct topping_mixer *tm =3D elem->head.mixer->private_data; + unsigned long flags; + + spin_lock_irqsave(&tm->lock, flags); + ucontrol->value.integer.value[0] =3D tm->val[elem->control]; + spin_unlock_irqrestore(&tm->lock, flags); + return 0; +} + +static int topping_ctl_put(struct snd_kcontrol *kctl, + struct snd_ctl_elem_value *ucontrol) +{ + struct usb_mixer_elem_info *elem =3D kctl->private_data; + struct usb_mixer_interface *mixer =3D elem->head.mixer; + struct topping_mixer *tm =3D mixer->private_data; + const struct topping_ctl_desc *d =3D &tm->ctls[elem->control]; + unsigned long flags; + int value, err; + + value =3D ucontrol->value.integer.value[0]; + if (value < d->min || value > d->max) + return -EINVAL; + + spin_lock_irqsave(&tm->lock, flags); + if (tm->val[elem->control] =3D=3D value) { + spin_unlock_irqrestore(&tm->lock, flags); + return 0; + } + spin_unlock_irqrestore(&tm->lock, flags); + + err =3D topping_send(tm, d->target, d->prop, value); + if (err < 0) + return err; + if (d->target_pair) { + /* + * The device announces only one of a pair, so the other + * would drift away unheard. + */ + err =3D topping_send(tm, d->target_pair, d->prop, value); + if (err < 0) + return err; + } + + spin_lock_irqsave(&tm->lock, flags); + tm->val[elem->control] =3D value; + spin_unlock_irqrestore(&tm->lock, flags); + return 1; +} + +static const struct snd_kcontrol_new topping_ctl =3D { + .iface =3D SNDRV_CTL_ELEM_IFACE_MIXER, + .access =3D SNDRV_CTL_ELEM_ACCESS_READWRITE | + SNDRV_CTL_ELEM_ACCESS_TLV_READ, + .info =3D topping_ctl_info, + .get =3D topping_ctl_get, + .put =3D topping_ctl_put, +}; + +static int topping_add_ctl(struct topping_mixer *tm, int idx) +{ + struct usb_mixer_elem_info *elem; + struct snd_kcontrol *kctl; + int err; + + elem =3D kzalloc_obj(*elem); + if (!elem) + return -ENOMEM; + + elem->head.mixer =3D tm->mixer; + elem->head.id =3D 0; + elem->control =3D idx; + elem->channels =3D 1; + elem->val_type =3D USB_MIXER_BESPOKEN; + + kctl =3D snd_ctl_new1(&topping_ctl, elem); + if (!kctl) { + kfree(elem); + return -ENOMEM; + } + kctl->private_free =3D snd_usb_mixer_elem_free; + kctl->tlv.p =3D tm->ctls[idx].tlv; + strscpy(kctl->id.name, tm->ctls[idx].name, sizeof(kctl->id.name)); + + err =3D snd_usb_mixer_add_control(&elem->head, kctl); + if (err < 0) + return err; + + tm->kctl[idx] =3D kctl; + return 0; +} + +static void topping_private_free(struct usb_mixer_interface *mixer) +{ + struct topping_mixer *tm =3D mixer->private_data; + + if (!tm) + return; + if (tm->urb) { + usb_kill_urb(tm->urb); + usb_free_coherent(mixer->chip->dev, TOPPING_EP_BUF, + tm->inbuf, tm->inbuf_dma); + usb_free_urb(tm->urb); + } + kfree(tm->val); + kfree(tm->kctl); + kfree(tm); + mixer->private_data =3D NULL; +} + +/* the HID interface, by class rather than by a number in a comment */ +static struct usb_interface *topping_find_iface(struct snd_usb_audio *chip, + int *ep_in, int *ep_out, + int *interval) +{ + struct usb_device *dev =3D chip->dev; + struct usb_host_interface *alts; + struct usb_interface *iface; + int i, e; + + for (i =3D 0; i < 256; i++) { + iface =3D usb_ifnum_to_if(dev, i); + if (!iface) + continue; + alts =3D &iface->altsetting[0]; + if (alts->desc.bInterfaceClass !=3D USB_CLASS_HID) + continue; + *ep_in =3D *ep_out =3D 0; + for (e =3D 0; e < alts->desc.bNumEndpoints; e++) { + struct usb_endpoint_descriptor *ep; + + ep =3D &alts->endpoint[e].desc; + if (!usb_endpoint_xfer_int(ep)) + continue; + if (usb_endpoint_dir_in(ep)) { + *ep_in =3D usb_endpoint_num(ep); + *interval =3D ep->bInterval; + } else { + *ep_out =3D usb_endpoint_num(ep); + } + } + if (*ep_in && *ep_out) + return iface; + } + return NULL; +} + +int snd_topping_init(struct usb_mixer_interface *mixer) +{ + struct snd_usb_audio *chip =3D mixer->chip; + struct usb_interface *iface; + struct topping_mixer *tm; + int ep_in =3D 0, ep_out =3D 0, interval =3D 5; + int i, err; + + iface =3D topping_find_iface(chip, &ep_in, &ep_out, &interval); + if (!iface) { + usb_audio_err(chip, "Topping: no vendor HID interface\n"); + return 0; /* not fatal: the card still plays */ + } + if (usb_interface_claimed(iface)) { + usb_audio_err(chip, + "Topping: the HID interface is already claimed\n"); + return 0; + } + + tm =3D kzalloc_obj(*tm); + if (!tm) + return -ENOMEM; + + tm->mixer =3D mixer; + tm->iface =3D iface; + tm->ctls =3D topping_m62_ctls; + tm->num_ctls =3D ARRAY_SIZE(topping_m62_ctls); + tm->pipe_in =3D usb_rcvintpipe(chip->dev, ep_in); + tm->pipe_out =3D usb_sndintpipe(chip->dev, ep_out); + tm->interval =3D interval; + spin_lock_init(&tm->lock); + + tm->val =3D kcalloc(tm->num_ctls, sizeof(*tm->val), GFP_KERNEL); + tm->kctl =3D kcalloc(tm->num_ctls, sizeof(*tm->kctl), GFP_KERNEL); + if (!tm->val || !tm->kctl) { + err =3D -ENOMEM; + goto fail; + } + + err =3D snd_usb_claim_iface(chip, iface); + if (err < 0) + goto fail; + + tm->urb =3D usb_alloc_urb(0, GFP_KERNEL); + if (!tm->urb) { + err =3D -ENOMEM; + goto fail; + } + tm->inbuf =3D usb_alloc_coherent(chip->dev, TOPPING_EP_BUF, GFP_KERNEL, + &tm->inbuf_dma); + if (!tm->inbuf) { + err =3D -ENOMEM; + goto fail; + } + usb_fill_int_urb(tm->urb, chip->dev, tm->pipe_in, + tm->inbuf, TOPPING_EP_BUF, + topping_urb_complete, tm, tm->interval); + tm->urb->transfer_dma =3D tm->inbuf_dma; + tm->urb->transfer_flags |=3D URB_NO_TRANSFER_DMA_MAP; + + mixer->private_data =3D tm; + mixer->private_free =3D topping_private_free; + + for (i =3D 0; i < tm->num_ctls; i++) { + err =3D topping_add_ctl(tm, i); + if (err < 0) + return err; /* private_free cleans up */ + } + + err =3D usb_submit_urb(tm->urb, GFP_KERNEL); + if (err < 0) { + usb_audio_err(chip, "Topping: cannot listen: %d\n", err); + return err; + } + + /* + * Subscribe, then ask for the state. The device answers in two + * waves -- identification at once, the gains about 3.7 s later, + * which is the same delay a phantom rail takes to settle -- so + * nothing here waits for them: each value lands through the URB + * and notifies its own control. + */ + topping_send(tm, TOPPING_TT_DEVICE, TOPPING_PP_SUBSCRIBE, 1); + topping_send(tm, TOPPING_TT_DEVICE, TOPPING_PP_ANNOUNCE, 1); + return 0; + +fail: + if (tm->inbuf) + usb_free_coherent(chip->dev, TOPPING_EP_BUF, tm->inbuf, + tm->inbuf_dma); + usb_free_urb(tm->urb); + kfree(tm->val); + kfree(tm->kctl); + kfree(tm); + return err; +} diff --git a/sound/usb/mixer_topping.h b/sound/usb/mixer_topping.h new file mode 100644 index 0000000..15e16b5 --- /dev/null +++ b/sound/usb/mixer_topping.h @@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#ifndef __USB_MIXER_TOPPING_H +#define __USB_MIXER_TOPPING_H + +int snd_topping_init(struct usb_mixer_interface *mixer); + +#endif /* __USB_MIXER_TOPPING_H */ diff --git a/sound/usb/usbaudio.h b/sound/usb/usbaudio.h index c49709d..4f9770a 100644 --- a/sound/usb/usbaudio.h +++ b/sound/usb/usbaudio.h @@ -80,6 +80,9 @@ struct snd_usb_audio { =20 #define USB_AUDIO_IFACE_UNUSED ((void *)-1L) =20 +int snd_usb_claim_iface(struct snd_usb_audio *chip, + struct usb_interface *iface); + #define usb_audio_err(chip, fmt, args...) \ dev_err(&(chip)->dev->dev, fmt, ##args) #define usb_audio_err_ratelimited(chip, fmt, args...) \ --=20 2.43.0 From nobody Mon Sep 28 16:21:57 2026 Received: from mail-lf1-f42.google.com (mail-lf1-f42.google.com [209.85.167.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 757BB37F730 for ; Thu, 20 Aug 2026 15:13:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787238821; cv=none; b=oj4hOJdnKTuRejMxASyVWLVG6G+Cy1WjF9aDZ5EkxPDnuN2n7ivSIWpKN2Fwbuhu+OK3qMwbvf3gUrh6bsDFoC1XSba/k8OJSf4/KcyE90axFxNWGpoZjSq4gJKWqXnNtO22QtLgJ2T3L/IUtl7E9iBzY92hDwTi8LbFuQHI92A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787238821; c=relaxed/simple; bh=Sbm/fPSO/OowzxSNV6zi0aDU42cLOUdY3i17kKcBHKw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IK4LGpwSk/gAp14GwpFdCgxjovYDvr6fIZYJefv14/QIdIXzlFtUbLw1WRFwrlLZhEix1sXUuLyvpASda++moovMuPToca7e0cH6ld7Wk2WagugqAik+EyHB+QMtXJ7eDSCg4/OFIAbDjLW0EhNmX6yJFROQ/ioteHdQ23pA1N4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=nTx0Oxsc; arc=none smtp.client-ip=209.85.167.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="nTx0Oxsc" Received: by mail-lf1-f42.google.com with SMTP id 2adb3069b0e04-5b29599b81cso3328341e87.1 for ; Thu, 20 Aug 2026 08:13:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787238817; x=1787843617; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=eWnrIk20T6fTLYmK9j7MbAAAQibqvLV5mGakVK14x7I=; b=nTx0OxscUEUBK/SRrfDZ98IzbjgwTXaYkg3iJaNdlSESwy/sQ0LYcXA9lzSbnM+Erz /7sopCG60OoCyn+eA+9VTx0euG2FypPt2rSNlvVFr6r98SktSo0RtXPMyd3QmRVdGg6f UnSujz9XUVWNGW4dYgxOGcvVcFptS27szZn3OQ4z+juCFYc+RqbL8kFMMW2TwwK0NznA NavIyfvhRM3yryMJA9Uai/sdT86TSaMSf2fCa+l2HV/rm5txyjDAs4/KaR0ag0NdZ+R6 nOX+E6/fbBrXwhPx3t3O062pC2Innan9alhRHIRqiRlSblGBzF5tbyrsUHShA0EwzOlA 7zDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787238817; x=1787843617; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=eWnrIk20T6fTLYmK9j7MbAAAQibqvLV5mGakVK14x7I=; b=XfnCjqXpB4v5p3X9suRqsYKLLRDL/Hm1gpJjyk3WE3vNXignxuzCjxSs4of0uZHvGA 9tyj8W5WMvnmHi8GvfkyfG5wXP/5KfJExo5zmtyyGl5nx0MbMKyWLzOmIXUe5lplYq9z HcDJtQ7cJc9hsExmCHtTiE7S+WZ6E0facrOPDTwOpDcGVlBLknTSM0Pcm9QJeeoPmVyX tqgoFSaPQ2pjCH6eRyUJiLCueOCfqhj2omt6vgm+UXW1SDVwxTf6LVnea/W/bf8dhWap ypu1D4t4EE7qhV8XGnfoJLd3gzjel7tknt7w1eC+s2Kano7YbataQEXXRDCvAAet1I9s BuVA== X-Forwarded-Encrypted: i=1; AHgh+RqEzWwvhvSlD5LnKAMgdu1i4U02F+RkL5ZcOfAzdzjznGEPOK8jCgT+pVrlEVtKptShW0V627iAPq4UcwY=@vger.kernel.org X-Gm-Message-State: AOJu0YxLRPb6PTOFW2mo+gr1tPKBsmCy/AA9i6kX+T1N6/Nmq4KULxMs vNuiK4NF3Ze7O/eSC2LNPW8ecBchF1T9zlKzHOd+C2K9rgOeBHz5/lgX X-Gm-Gg: AR+sD12VhJLEU06rAIy7W0HguRCSaollwfo0tz/kQHbf0qhRyi1mHRmKmaIhTrRnCmx 9xrkqtPucQgFNFXP5rYHVVXepBncXrY/7V7NDhgo/+Zrvv8tml5X7eHOE2zPPP0/Rncuq1sOOXl sQSXvCGr+hxMrCBKilqa3et/VcvhF1MzRxADhIoaoTHnDzyKfjaKhAcXBLOsC9pSOfjxVksJ4Db BHdY3ABKJCYVBv6UYPzvjgqq5iZi53LnYDH5RTRGArJq+ULHjYG0FVEPv/HDQJNQOlzhIhEZNb/ pSfnkLSIcJFpYaVpserpWTwjQWVrq8ifRMcsWaQzVGX5ACa3NhMBiC7EwDHkaIcY5M2IEHUgriW mbRPDuH27oF9jDptBIYKnHkDavws5jJCUfwwAuDZjxjndwQulIPhrP+MItSM/JDx5dPErKnmO5h PeMu2Hi96DBihnzJcsx9TilkhByAeQ+BiZdN84UKdekCwzjynDLB22i/we1obPQNuxxP/0LzSEY sLHUhewApcBvFx13acrcT35kaVYq2DLQ8sSrZcYkEAbTH7bRXlGvkjPRByU X-Received: by 2002:a05:6512:10cf:b0:5ae:b260:cf47 with SMTP id 2adb3069b0e04-5b4789448cdmr4757847e87.10.1787238817106; Thu, 20 Aug 2026 08:13:37 -0700 (PDT) Received: from localhost ([188.234.148.119]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5b4788f5a60sm1333617e87.77.2026.08.20.08.13.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Aug 2026 08:13:36 -0700 (PDT) From: Mikhail Gavrilov To: tiwai@suse.com, jikos@kernel.org, bentiss@kernel.org Cc: perex@perex.cz, linux-sound@vger.kernel.org, linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, Mikhail Gavrilov Subject: [RFC 2/2] HID: topping: driver for the M62's vendor control channel Date: Thu, 20 Aug 2026 20:13:29 +0500 Message-ID: <20260820151329.18332-3-mikhail.v.gavrilov@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260820151329.18332-1-mikhail.v.gavrilov@gmail.com> References: <87y0eaxz39.wl-tiwai@suse.de> <20260820151329.18332-1-mikhail.v.gavrilov@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The alternative to doing this as a snd-usb-audio mixer quirk, written so the two can be compared rather than argued about. The device and the protocol are the same: fifteen-byte frames carrying a target, a property and a signed value, CRC-16/MODBUS over the middle, one write to subscribe and one to make the device announce its state, after which every change arrives unasked, front panel presses included. That part is if anything smaller here than in the quirk -- usbhid owns the endpoints, so hid_hw_output_report replaces a hand-built interrupt URB out and raw_event replaces the one in, and the interface needs no claiming because this driver is what binds to it. The control table is identical, so adding a knob is still adding a row. WHERE THIS ROAD RUNS OUT is the reason to write it. These are mixer controls for an audio device, and the audio device's card belongs to snd-usb-audio. A HID driver cannot put an element there: there is no interface for it, and inventing one means exporting from sound/usb both a lookup from struct usb_device to the card and an add-element call -- and then answering, for a single device, what happens when the two drivers probe in either order, and when either disconnects first, given that the element would live in one module and its private data in another. So this driver does what a HID driver can do alone: it registers a card of its own. That works, and the cost is visible from userspace rather than theoretical -- one physical device appears as two cards, the gains land on a card with no PCM beside them, and anything that looks for a device's mixer next to its streams (alsamixer -c, UCM profiles, PipeWire's device model) does not find them there. hid_hw_start is called with no connect mask on purpose. The report descriptor is a Generic Desktop application collection with eight unnamed usages and no report ID, so hid-generic would build an input device for a mouse that does not exist. Signed-off-by: Mikhail Gavrilov --- MAINTAINERS | 6 + drivers/hid/Kconfig | 13 ++ drivers/hid/Makefile | 1 + drivers/hid/hid-ids.h | 3 + drivers/hid/hid-topping.c | 382 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 405 insertions(+) create mode 100644 drivers/hid/hid-topping.c diff --git a/MAINTAINERS b/MAINTAINERS index d2b7ed2..ec302fa 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -27349,6 +27349,12 @@ S: Maintained W: https://tomoyo.sourceforge.net/ F: security/tomoyo/ =20 +TOPPING M62 HID CONTROL DRIVER +M: Mikhail Gavrilov +L: linux-input@vger.kernel.org +S: Maintained +F: drivers/hid/hid-topping.c + TOPSTAR LAPTOP EXTRAS DRIVER M: Herton Ronaldo Krzesinski L: platform-driver-x86@vger.kernel.org diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig index aa7fa11..712390a 100644 --- a/drivers/hid/Kconfig +++ b/drivers/hid/Kconfig @@ -1283,6 +1283,19 @@ config HID_TIVO help Say Y if you have a TiVo Slide Bluetooth remote control. =20 +config HID_TOPPING + tristate "Topping M62 vendor control channel" + depends on USB_HID + depends on SND + help + Say Y here if you have a Topping M62 audio interface and want + its analogue input gains and output volumes as ALSA mixer + controls. The device keeps them behind a vendor protocol on a + HID interface and exposes none of them through USB audio. + + Note that the controls appear on a card of this driver's own, + not on the card snd-usb-audio creates for the same device. + config HID_TOPSEED tristate "TopSeed Cyberlink, BTC Emprex, Conceptronic remote control supp= ort" help diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile index 48a863b..6e1f8fc 100644 --- a/drivers/hid/Makefile +++ b/drivers/hid/Makefile @@ -141,6 +141,7 @@ obj-$(CONFIG_HID_SUNPLUS) +=3D hid-sunplus.o obj-$(CONFIG_HID_GREENASIA) +=3D hid-gaff.o obj-$(CONFIG_HID_THRUSTMASTER) +=3D hid-tmff.o hid-thrustmaster.o obj-$(CONFIG_HID_TIVO) +=3D hid-tivo.o +obj-$(CONFIG_HID_TOPPING) +=3D hid-topping.o obj-$(CONFIG_HID_TOPSEED) +=3D hid-topseed.o obj-$(CONFIG_HID_TOPRE) +=3D hid-topre.o obj-$(CONFIG_HID_TWINHAN) +=3D hid-twinhan.o diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h index 341bf58..092b2a9 100644 --- a/drivers/hid/hid-ids.h +++ b/drivers/hid/hid-ids.h @@ -1470,6 +1470,9 @@ #define USB_DEVICE_ID_TIVO_SLIDE 0x1201 #define USB_DEVICE_ID_TIVO_SLIDE_PRO 0x1203 =20 +#define USB_VENDOR_ID_TOPPING 0x152a +#define USB_DEVICE_ID_TOPPING_M62 0x875c + #define USB_VENDOR_ID_TOPRE 0x0853 #define USB_DEVICE_ID_TOPRE_REALFORCE_R2_108 0x0148 #define USB_DEVICE_ID_TOPRE_REALFORCE_R2_87 0x0146 diff --git a/drivers/hid/hid-topping.c b/drivers/hid/hid-topping.c new file mode 100644 index 0000000..9d58bce --- /dev/null +++ b/drivers/hid/hid-topping.c @@ -0,0 +1,382 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * HID driver for the Topping M62's vendor control channel + * + * Copyright (c) 2026 Mikhail Gavrilov + * + * The same device and the same protocol as the snd-usb-audio mixer + * quirk this is meant to be compared against; only the road differs. + * Here the vendor channel is taken as what it claims to be -- a HID + * interface -- and the driver rides usbhid instead of claiming the + * interface for the audio driver. + * + * The protocol: fifteen-byte frames, + * + * 22 33 | 20 01 01 | TT | PP | s32 value BE | CRC16 BE | 66 77 + * + * a target TT, a property PP of it, CRC-16/MODBUS over bytes 2..10 + * stored big-endian. Reports arriving are the same plus a pad byte. + * One write of 0x11/0x24 subscribes, one of 0x11/0x26 makes the device + * announce its whole state, and every later change arrives unasked, + * including a front panel press. + * + * WHERE THIS ROAD RUNS OUT, which is the point of writing it: these + * are mixer controls for an audio device, and the audio device's card + * belongs to snd-usb-audio. A HID driver cannot put a control there. + * There is no interface for it, and inventing one means exporting from + * sound/usb both a way to find the card behind a struct usb_device and + * a way to add an element to it -- and then answering, for a single + * device, what happens when the two drivers probe in either order and + * when either disconnects first, since the control's private data + * would live in this module while the element lives in that card. + * + * So this driver does what a HID driver CAN do by itself: it makes a + * card of its own. That works, and it is exactly the wart to weigh -- + * one physical device shows up twice in userspace, the gains land on a + * card that has no PCM, and anything that looks for a device's mixer + * beside its streams (alsamixer -c, UCM, PipeWire) does not find them + * there. + */ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "hid-ids.h" + +#define TOPPING_FRAME_LEN 15 +#define TOPPING_REPORT_LEN 16 + +#define TOPPING_TT_DEVICE 0x11 +#define TOPPING_PP_SUBSCRIBE 0x24 +#define TOPPING_PP_ANNOUNCE 0x26 + +static const DECLARE_TLV_DB_SCALE(topping_tlv_gain, 0, 100, 0); + +static const unsigned int topping_tlv_out_9[] =3D { + TLV_DB_RANGE_HEAD(4), + 0, 0, SNDRV_CTL_TLVD_DB_SCALE_ITEM(SNDRV_CTL_TLVD_DB_GAIN_MUTE, 0, 1), + 1, 19, SNDRV_CTL_TLVD_DB_SCALE_ITEM(-8800, 200, 0), + 20, 61, SNDRV_CTL_TLVD_DB_SCALE_ITEM(-5100, 100, 0), + 62, 99, SNDRV_CTL_TLVD_DB_SCALE_ITEM(-950, 50, 0), +}; + +static const unsigned int topping_tlv_out_0[] =3D { + TLV_DB_RANGE_HEAD(3), + 0, 0, SNDRV_CTL_TLVD_DB_SCALE_ITEM(SNDRV_CTL_TLVD_DB_GAIN_MUTE, 0, 1), + 1, 79, SNDRV_CTL_TLVD_DB_SCALE_ITEM(-8800, 100, 0), + 80, 99, SNDRV_CTL_TLVD_DB_SCALE_ITEM(-950, 50, 0), +}; + +/* identical to the quirk's: a knob is a row */ +struct topping_ctl_desc { + const char *name; + u8 target; + u8 target_pair; + u8 prop; + int min, max; + const unsigned int *tlv; +}; + +static const struct topping_ctl_desc topping_m62_ctls[] =3D { + { "Mic-1 Analog Capture Volume", 0x21, 0, 0x04, 0, 88, + topping_tlv_gain }, + { "Mic-2 Analog Capture Volume", 0x22, 0, 0x04, 0, 88, + topping_tlv_gain }, + { "Aux Capture Volume", 0x23, 0, 0x04, 0, 99, + topping_tlv_out_9 }, + { "Bluetooth Capture Volume", 0x25, 0, 0x04, 0, 99, + topping_tlv_out_0 }, + { "Headphone Playback Volume", 0x64, 0x63, 0x03, 0, 99, + topping_tlv_out_9 }, + { "OTG Playback Volume", 0x62, 0x61, 0x03, 0, 99, + topping_tlv_out_0 }, +}; + +#define TOPPING_NUM_CTLS ARRAY_SIZE(topping_m62_ctls) + +struct topping_hid { + struct hid_device *hdev; + struct snd_card *card; + spinlock_t lock; /* guards val[] against raw_event */ + int val[TOPPING_NUM_CTLS]; + struct snd_kcontrol *kctl[TOPPING_NUM_CTLS]; +}; + +static void topping_build(u8 *f, u8 target, u8 prop, s32 value) +{ + f[0] =3D 0x22; + f[1] =3D 0x33; + f[2] =3D 0x20; + f[3] =3D 0x01; + f[4] =3D 0x01; + f[5] =3D target; + f[6] =3D prop; + put_unaligned_be32(value, f + 7); + put_unaligned_be16(crc16(0xffff, f + 2, 9), f + 11); + f[13] =3D 0x66; + f[14] =3D 0x77; +} + +static int topping_send(struct topping_hid *th, u8 target, u8 prop, + s32 value) +{ + u8 *buf; + int err; + + buf =3D kzalloc(TOPPING_FRAME_LEN, GFP_KERNEL); + if (!buf) + return -ENOMEM; + topping_build(buf, target, prop, value); + err =3D hid_hw_output_report(th->hdev, buf, TOPPING_FRAME_LEN); + kfree(buf); + if (err < 0) + hid_err(th->hdev, "write %02x/%02x failed: %d\n", + target, prop, err); + return err; +} + +static int topping_index_of(u8 target, u8 prop) +{ + int i; + + for (i =3D 0; i < TOPPING_NUM_CTLS; i++) + if (topping_m62_ctls[i].target =3D=3D target && + topping_m62_ctls[i].prop =3D=3D prop) + return i; + return -1; +} + +static int topping_raw_event(struct hid_device *hdev, + struct hid_report *report, u8 *f, int size) +{ + struct topping_hid *th =3D hid_get_drvdata(hdev); + unsigned long flags; + int idx, value; + + if (size < TOPPING_FRAME_LEN) + return 0; + if (f[0] !=3D 0x22 || f[1] !=3D 0x33 || f[13] !=3D 0x66 || f[14] !=3D 0x7= 7) + return 0; + if (get_unaligned_be16(f + 11) !=3D crc16(0xffff, f + 2, 9)) + return 0; + + idx =3D topping_index_of(f[5], f[6]); + if (idx < 0) + return 0; + + value =3D get_unaligned_be32(f + 7); + if (value < topping_m62_ctls[idx].min || + value > topping_m62_ctls[idx].max) + return 0; + + spin_lock_irqsave(&th->lock, flags); + if (th->val[idx] =3D=3D value) { + spin_unlock_irqrestore(&th->lock, flags); + return 0; + } + th->val[idx] =3D value; + spin_unlock_irqrestore(&th->lock, flags); + + if (th->kctl[idx]) + snd_ctl_notify(th->card, SNDRV_CTL_EVENT_MASK_VALUE, + &th->kctl[idx]->id); + return 0; +} + +static int topping_ctl_info(struct snd_kcontrol *kctl, + struct snd_ctl_elem_info *uinfo) +{ + int idx =3D kctl->private_value; + + uinfo->type =3D SNDRV_CTL_ELEM_TYPE_INTEGER; + uinfo->count =3D 1; + uinfo->value.integer.min =3D topping_m62_ctls[idx].min; + uinfo->value.integer.max =3D topping_m62_ctls[idx].max; + uinfo->value.integer.step =3D 1; + return 0; +} + +static int topping_ctl_get(struct snd_kcontrol *kctl, + struct snd_ctl_elem_value *ucontrol) +{ + struct topping_hid *th =3D snd_kcontrol_chip(kctl); + unsigned long flags; + + spin_lock_irqsave(&th->lock, flags); + ucontrol->value.integer.value[0] =3D th->val[kctl->private_value]; + spin_unlock_irqrestore(&th->lock, flags); + return 0; +} + +static int topping_ctl_put(struct snd_kcontrol *kctl, + struct snd_ctl_elem_value *ucontrol) +{ + struct topping_hid *th =3D snd_kcontrol_chip(kctl); + int idx =3D kctl->private_value; + const struct topping_ctl_desc *d =3D &topping_m62_ctls[idx]; + unsigned long flags; + int value, err; + + value =3D ucontrol->value.integer.value[0]; + if (value < d->min || value > d->max) + return -EINVAL; + + spin_lock_irqsave(&th->lock, flags); + if (th->val[idx] =3D=3D value) { + spin_unlock_irqrestore(&th->lock, flags); + return 0; + } + spin_unlock_irqrestore(&th->lock, flags); + + err =3D topping_send(th, d->target, d->prop, value); + if (err < 0) + return err; + if (d->target_pair) { + err =3D topping_send(th, d->target_pair, d->prop, value); + if (err < 0) + return err; + } + + spin_lock_irqsave(&th->lock, flags); + th->val[idx] =3D value; + spin_unlock_irqrestore(&th->lock, flags); + return 1; +} + +static int topping_add_ctls(struct topping_hid *th) +{ + struct snd_kcontrol_new tmpl =3D { + .iface =3D SNDRV_CTL_ELEM_IFACE_MIXER, + .access =3D SNDRV_CTL_ELEM_ACCESS_READWRITE | + SNDRV_CTL_ELEM_ACCESS_TLV_READ, + .info =3D topping_ctl_info, + .get =3D topping_ctl_get, + .put =3D topping_ctl_put, + }; + struct snd_kcontrol *kctl; + int i, err; + + for (i =3D 0; i < TOPPING_NUM_CTLS; i++) { + tmpl.name =3D topping_m62_ctls[i].name; + tmpl.private_value =3D i; + tmpl.tlv.p =3D topping_m62_ctls[i].tlv; + kctl =3D snd_ctl_new1(&tmpl, th); + if (!kctl) + return -ENOMEM; + err =3D snd_ctl_add(th->card, kctl); + if (err < 0) + return err; + th->kctl[i] =3D kctl; + } + return 0; +} + +static int topping_probe(struct hid_device *hdev, + const struct hid_device_id *id) +{ + struct topping_hid *th; + int err; + + th =3D devm_kzalloc(&hdev->dev, sizeof(*th), GFP_KERNEL); + if (!th) + return -ENOMEM; + th->hdev =3D hdev; + spin_lock_init(&th->lock); + hid_set_drvdata(hdev, th); + + err =3D hid_parse(hdev); + if (err) + return err; + + /* + * No connect mask: the report descriptor describes a Generic + * Desktop application collection with eight unnamed usages, so + * letting hid-generic have it would create an input device for a + * mouse that does not exist. + */ + err =3D hid_hw_start(hdev, 0); + if (err) + return err; + + err =3D hid_hw_open(hdev); + if (err) + goto stop; + + /* + * A CARD OF ITS OWN, because the device's real card belongs to + * snd-usb-audio and nothing lets an outside module add an + * element to it. This is the cost of the HID road, and it is + * visible from userspace: two cards for one device. + */ + err =3D snd_card_new(&hdev->dev, SNDRV_DEFAULT_IDX1, "ToppingCtl", + THIS_MODULE, 0, &th->card); + if (err < 0) + goto close; + + strscpy(th->card->driver, "Topping", sizeof(th->card->driver)); + strscpy(th->card->shortname, "Topping M62 control", + sizeof(th->card->shortname)); + strscpy(th->card->longname, "Topping M62 vendor control channel", + sizeof(th->card->longname)); + + err =3D topping_add_ctls(th); + if (err < 0) + goto free_card; + + err =3D snd_card_register(th->card); + if (err < 0) + goto free_card; + + topping_send(th, TOPPING_TT_DEVICE, TOPPING_PP_SUBSCRIBE, 1); + topping_send(th, TOPPING_TT_DEVICE, TOPPING_PP_ANNOUNCE, 1); + return 0; + +free_card: + snd_card_free(th->card); +close: + hid_hw_close(hdev); +stop: + hid_hw_stop(hdev); + return err; +} + +static void topping_remove(struct hid_device *hdev) +{ + struct topping_hid *th =3D hid_get_drvdata(hdev); + + /* + * The card first: its elements call into this module, and the + * hid device under them is about to stop answering. + */ + if (th->card) + snd_card_free(th->card); + hid_hw_close(hdev); + hid_hw_stop(hdev); +} + +static const struct hid_device_id topping_devices[] =3D { + { HID_USB_DEVICE(USB_VENDOR_ID_TOPPING, USB_DEVICE_ID_TOPPING_M62) }, + { } +}; +MODULE_DEVICE_TABLE(hid, topping_devices); + +static struct hid_driver topping_driver =3D { + .name =3D "topping", + .id_table =3D topping_devices, + .probe =3D topping_probe, + .remove =3D topping_remove, + .raw_event =3D topping_raw_event, +}; +module_hid_driver(topping_driver); + +MODULE_DESCRIPTION("HID driver for the Topping M62 vendor control channel"= ); +MODULE_AUTHOR("Mikhail Gavrilov "); +MODULE_LICENSE("GPL"); --=20 2.43.0