All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sebastian <seb.witt@googlemail.com>
To: linux-bluetooth@vger.kernel.org
Cc: Sebastian <seb.witt@googlemail.com>,
	"Claude Opus 5.5" <noreply@anthropic.com>
Subject: [PATCH 1/2] shared/hog: Read the PnP ID before creating the uHID device
Date: Mon, 30 Nov 2026 20:25:20 +0100	[thread overview]
Message-ID: <20261130192521.246374-2-seb.witt@googlemail.com> (raw)
In-Reply-To: <20261130192521.246374-1-seb.witt@googlemail.com>

Since commit 96f1201f5ac2 ("input/hog: Use shared/hog") the vendor,
product and version of the uHID device are the ones of the btd_device
when the connection is accepted. On the first connection after pairing
they are not known yet, as the deviceinfo plugin reads the PnP ID
concurrently, so the uHID device is created with 0000:0000 and the
kernel drivers and quirks matching the device don't apply until it
reconnects:

  hid-multitouch 0005:0000:0000.001C: input,hidraw1: BLUETOOTH HID v0.00

hog-lib used to read the PnP ID itself before creating the uHID device,
so read it again, if the Device Information Service has one, along with
the other characteristics the uHID device is created after.

Fixes: 96f1201f5ac2 ("input/hog: Use shared/hog")
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
 src/shared/hog.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 76 insertions(+)

diff --git a/src/shared/hog.c b/src/shared/hog.c
index e62423fa4..ed89b3960 100644
--- a/src/shared/hog.c
+++ b/src/shared/hog.c
@@ -61,6 +61,10 @@
 
 #define HID_INFO_SIZE			4
 
+#define DIS_UUID16		0x180A
+#define DIS_PNP_ID_UUID		0x2A50
+#define PNP_ID_SIZE		7
+
 struct bt_hog {
 	int			ref_count;
 	char			*name;
@@ -78,6 +82,7 @@ struct bt_hog {
 	uint16_t		bcdhid;
 	uint8_t			bcountrycode;
 	uint16_t		info_handle;
+	uint16_t		pnp_id_handle;
 	uint16_t		proto_mode_handle;
 	uint16_t		cp_handle;
 	uint8_t			flags;
@@ -1335,6 +1340,74 @@ void bt_hog_set_ids(struct bt_hog *hog, uint16_t vendor, uint16_t product,
 		bt_hog_set_ids(entry->data, vendor, product, version);
 }
 
+static void pnp_id_read_cb(bool success, uint8_t att_ecode,
+					const uint8_t *value, uint16_t length,
+					void *user_data)
+{
+	struct bt_hog *hog = user_data;
+	struct iovec iov = { (void *)value, length };
+	uint8_t source;
+
+	if (!success) {
+		DBG(hog, "PnP ID read failed: 0x%02x", att_ecode);
+		return;
+	}
+
+	if (length < PNP_ID_SIZE) {
+		DBG(hog, "Malformed PnP ID: length %u", length);
+		return;
+	}
+
+	cache_value(hog, hog->pnp_id_handle, value, length);
+
+	if (!util_iov_pull_u8(&iov, &source) ||
+			!util_iov_pull_le16(&iov, &hog->vendor) ||
+			!util_iov_pull_le16(&iov, &hog->product) ||
+			!util_iov_pull_le16(&iov, &hog->version))
+		return;
+
+	DBG(hog, "source: 0x%02X vendor: 0x%04X product: 0x%04X "
+			"version: 0x%04X", source, hog->vendor, hog->product,
+			hog->version);
+}
+
+static void foreach_dis_chrc(struct gatt_db_attribute *attr, void *user_data)
+{
+	struct bt_hog *hog = user_data;
+	bt_uuid_t uuid, pnp_id_uuid;
+	uint16_t value_handle;
+
+	gatt_db_attribute_get_char_data(attr, NULL, &value_handle, NULL,
+					NULL, &uuid);
+
+	bt_uuid16_create(&pnp_id_uuid, DIS_PNP_ID_UUID);
+	if (!bt_uuid_cmp(&pnp_id_uuid, &uuid))
+		hog->pnp_id_handle = value_handle;
+}
+
+static void foreach_dis_service(struct gatt_db_attribute *attr,
+							void *user_data)
+{
+	gatt_db_service_foreach_char(attr, foreach_dis_chrc, user_data);
+}
+
+/* Read the PnP ID of the Device Information Service, if any, so the uHID
+ * device is created with the vendor, product and version of the device
+ * even on the first connection, when they are not known yet.
+ */
+static void read_pnp_id(struct bt_hog *hog)
+{
+	bt_uuid_t uuid;
+
+	hog->pnp_id_handle = 0;
+
+	bt_uuid16_create(&uuid, DIS_UUID16);
+	gatt_db_foreach_service(hog->db, &uuid, foreach_dis_service, hog);
+
+	if (hog->pnp_id_handle)
+		read_char(hog, hog->pnp_id_handle, true, pnp_id_read_cb, hog);
+}
+
 static void discovery_idle(void *user_data)
 {
 	struct bt_hog *hog = user_data;
@@ -1376,6 +1449,9 @@ bool bt_hog_attach(struct bt_hog *hog, struct bt_gatt_client *client)
 	DBG(hog, "HoG discovering characteristics");
 	gatt_db_service_foreach_char(hog->attr, foreach_hog_chrc, hog);
 
+	if (!created)
+		read_pnp_id(hog);
+
 	/* Create the uHID device once every request is complete, including
 	 * the ones made once others complete, e.g. the CCC once the Report
 	 * Reference is read, even if some failed, e.g. reading an optional
-- 
2.56.1


  reply	other threads:[~2026-11-30 19:25 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 19:25 [PATCH 0/2] Read the PnP ID before creating the uHID device Sebastian
2026-11-30 19:25 ` Sebastian [this message]
2026-11-30 19:39   ` [PATCH 1/2] shared/hog: " Luiz Augusto von Dentz
2026-11-30 23:39   ` bluez.test.bot
2026-11-30 19:25 ` [PATCH 2/2] unit/test-hog: Add test for the PnP ID Sebastian

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130192521.246374-2-seb.witt@googlemail.com \
    --to=seb.witt@googlemail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=noreply@anthropic.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.