Linux bluetooth development
 help / color / mirror / Atom feed
From: Roland Schober <rolandschober1968@gmail.com>
To: linux-bluetooth@vger.kernel.org
Cc: luiz.dentz@gmail.com, marcel@holtmann.org,
	Roland Schober <rolandschober1968@gmail.com>
Subject: [RFC PATCH 5/5] Bluetooth: MGMT: Add PAwR subevent data and response reports
Date: Fri, 27 Nov 2026 09:54:00 +0100	[thread overview]
Message-ID: <20261127085401.263615-6-rolandschober1968@gmail.com> (raw)
In-Reply-To: <20261127085401.263615-1-rolandschober1968@gmail.com>

Complete the advertiser side of Periodic Advertising with Responses
(PAwR) over MGMT:

  PA Subevent Data Request event    the controller asks for the data
                                    of a range of subevents
  Set PA Subevent Data command      user space answers with that data
  PA Response Report event          the responses received in the
                                    response slots of a subevent

The LE events behind the two MGMT events are enabled if the controller
supports the PAwR advertiser feature, and are forwarded for instances
with subevents only.

The controller wants the subevent data within its lead time, a few tens
of milliseconds before the subevent. Set PA Subevent Data is therefore
sent with hci_send_cmd() and answered from the Command Complete handler,
not queued with hci_cmd_sync_queue(), where it would wait behind
sequences which block for an event, such as creating a connection.

Assisted-by: Claude:claude-fable-5-2
Signed-off-by: Roland Schober <rolandschober1968@gmail.com>
---
 include/net/bluetooth/hci_core.h |   6 ++
 include/net/bluetooth/mgmt.h     |  49 ++++++++++
 net/bluetooth/hci_event.c        |  90 +++++++++++++++++
 net/bluetooth/hci_sync.c         |   5 +
 net/bluetooth/mgmt.c             | 162 +++++++++++++++++++++++++++++++
 5 files changed, 312 insertions(+)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 117408645b9b..37ac912fbb85 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -2661,6 +2661,12 @@ void mgmt_adv_monitor_device_lost(struct hci_dev *hdev, u16 handle,
 				  bdaddr_t *bdaddr, u8 addr_type);
 void mgmt_conn_subrate_notify(struct hci_dev *hdev, struct hci_conn *conn,
 			      u8 status);
+void mgmt_pa_subevent_data_request(struct hci_dev *hdev, u8 instance,
+				   u8 subevent_start, u8 subevent_data_count);
+void mgmt_set_pa_subevent_data_complete(struct hci_dev *hdev, u8 status);
+void mgmt_pa_response_report(struct hci_dev *hdev, u8 instance, u8 subevent,
+			     u8 tx_status, u8 num_responses,
+			     const void *responses, u16 len);
 
 int hci_abort_conn(struct hci_conn *conn, u8 reason);
 void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency,
diff --git a/include/net/bluetooth/mgmt.h b/include/net/bluetooth/mgmt.h
index a46e87a9152c..b85ff94e518c 100644
--- a/include/net/bluetooth/mgmt.h
+++ b/include/net/bluetooth/mgmt.h
@@ -952,6 +952,28 @@ struct mgmt_rp_add_periodic_adv_data {
 	__u8	instance;
 } __packed;
 
+struct mgmt_pa_subevent_data {
+	__u8	subevent;
+	__u8	response_slot_start;
+	__u8	response_slot_count;
+	__u8	data_len;
+	__u8	data[] __counted_by(data_len);
+} __packed;
+
+/* The answer to MGMT_EV_PA_SUBEVENT_DATA_REQUEST: num_subevents entries of
+ * struct mgmt_pa_subevent_data.
+ */
+#define MGMT_OP_SET_PA_SUBEVENT_DATA	0x005F
+struct mgmt_cp_set_pa_subevent_data {
+	__u8	instance;
+	__u8	num_subevents;
+	__u8	subevents[];
+} __packed;
+#define MGMT_SET_PA_SUBEVENT_DATA_SIZE	2
+struct mgmt_rp_set_pa_subevent_data {
+	__u8	instance;
+} __packed;
+
 #define MGMT_EV_CMD_COMPLETE		0x0001
 struct mgmt_ev_cmd_complete {
 	__le16	opcode;
@@ -1276,3 +1298,30 @@ struct mgmt_ev_security_level_changed {
 	__u8	tlv_length;
 	__u8	tlv_data[] __counted_by(tlv_length);
 } __packed;
+
+#define MGMT_EV_PA_SUBEVENT_DATA_REQUEST	0x0035
+struct mgmt_ev_pa_subevent_data_request {
+	__u8	instance;
+	__u8	subevent_start;
+	__u8	subevent_data_count;
+} __packed;
+
+struct mgmt_pa_response {
+	__s8	tx_power;
+	__s8	rssi;
+	__u8	cte_type;
+	__u8	response_slot;
+	__u8	data_status;
+	__u8	data_len;
+	__u8	data[] __counted_by(data_len);
+} __packed;
+
+/* num_responses entries of struct mgmt_pa_response */
+#define MGMT_EV_PA_RESPONSE_REPORT		0x0036
+struct mgmt_ev_pa_response_report {
+	__u8	instance;
+	__u8	subevent;
+	__u8	tx_status;
+	__u8	num_responses;
+	__u8	responses[];
+} __packed;
diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
index a9b6a6ccaa03..b88dad2eadf3 100644
--- a/net/bluetooth/hci_event.c
+++ b/net/bluetooth/hci_event.c
@@ -4598,6 +4598,20 @@ static u8 hci_cc_le_set_per_adv_enable(struct hci_dev *hdev, void *data,
 	return rp->status;
 }
 
+static u8 hci_cc_le_set_per_adv_subevent_data(struct hci_dev *hdev, void *data,
+					      struct sk_buff *skb)
+{
+	struct hci_ev_status *rp = data;
+
+	bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
+
+	hci_dev_lock(hdev);
+	mgmt_set_pa_subevent_data_complete(hdev, rp->status);
+	hci_dev_unlock(hdev);
+
+	return rp->status;
+}
+
 #define HCI_CC_VL(_op, _func, _min, _max) \
 { \
 	.op = _op, \
@@ -4774,6 +4788,10 @@ static const struct hci_cc {
 	HCI_CC_STATUS(HCI_OP_LE_SET_PER_ADV_PARAMS, hci_cc_set_per_adv_param),
 	HCI_CC_STATUS(HCI_OP_LE_SET_PER_ADV_ENABLE,
 		      hci_cc_le_set_per_adv_enable),
+	/* Status and advertising handle */
+	HCI_CC_VL(HCI_OP_LE_SET_PER_ADV_SUBEVENT_DATA,
+		  hci_cc_le_set_per_adv_subevent_data,
+		  sizeof(struct hci_ev_status), HCI_MAX_CC_PLEN),
 	HCI_CC(HCI_OP_LE_READ_TRANSMIT_POWER, hci_cc_le_read_transmit_power,
 	       sizeof(struct hci_rp_le_read_transmit_power)),
 	HCI_CC_STATUS(HCI_OP_LE_SET_PRIVACY_MODE, hci_cc_le_set_privacy_mode),
@@ -8128,6 +8146,69 @@ static void hci_le_big_info_adv_report_evt(struct hci_dev *hdev, void *data,
 	hci_dev_unlock(hdev);
 }
 
+static void hci_le_per_adv_subevent_data_req_evt(struct hci_dev *hdev,
+						 void *data,
+						 struct sk_buff *skb)
+{
+	struct hci_ev_le_per_adv_subevent_data_req *ev = data;
+	struct adv_info *adv;
+
+	bt_dev_dbg(hdev, "handle 0x%2.2x start %u count %u", ev->handle,
+		   ev->subevent_start, ev->subevent_data_count);
+
+	hci_dev_lock(hdev);
+
+	adv = hci_le_ext_adv_owner(hdev, ev->handle);
+	if (adv && adv->num_subevents)
+		mgmt_pa_subevent_data_request(hdev, adv->instance,
+					      ev->subevent_start,
+					      ev->subevent_data_count);
+
+	hci_dev_unlock(hdev);
+}
+
+static void hci_le_per_adv_response_report_evt(struct hci_dev *hdev,
+					       void *data, struct sk_buff *skb)
+{
+	struct hci_ev_le_per_adv_response_report *ev = data;
+	struct hci_le_per_adv_response *rsp;
+	struct adv_info *adv;
+	void *responses = skb->data;
+	u16 len = skb->len;
+	u8 i;
+
+	bt_dev_dbg(hdev, "handle 0x%2.2x subevent %u num %u", ev->handle,
+		   ev->subevent, ev->num_responses);
+
+	/* The responses go to user space as they are: check that they are
+	 * all there.
+	 */
+	for (i = 0; i < ev->num_responses; i++) {
+		rsp = hci_le_ev_skb_pull(hdev, skb,
+					 HCI_EV_LE_PER_ADV_RESPONSE_REPORT,
+					 sizeof(*rsp));
+		if (!rsp)
+			return;
+
+		if (!hci_le_ev_skb_pull(hdev, skb,
+					HCI_EV_LE_PER_ADV_RESPONSE_REPORT,
+					rsp->data_len))
+			return;
+	}
+
+	len -= skb->len;
+
+	hci_dev_lock(hdev);
+
+	adv = hci_le_ext_adv_owner(hdev, ev->handle);
+	if (adv && adv->num_subevents)
+		mgmt_pa_response_report(hdev, adv->instance, ev->subevent,
+					ev->tx_status, ev->num_responses,
+					responses, len);
+
+	hci_dev_unlock(hdev);
+}
+
 static void hci_le_read_all_remote_features_evt(struct hci_dev *hdev,
 						void *data, struct sk_buff *skb)
 {
@@ -8437,6 +8518,15 @@ static const struct hci_le_ev {
 		     hci_le_big_info_adv_report_evt,
 		     sizeof(struct hci_evt_le_big_info_adv_report),
 		     HCI_MAX_LE_SUBEVT_PLEN),
+	/* [0x27 = HCI_EV_LE_PER_ADV_SUBEVENT_DATA_REQ] */
+	HCI_LE_EV(HCI_EV_LE_PER_ADV_SUBEVENT_DATA_REQ,
+		  hci_le_per_adv_subevent_data_req_evt,
+		  sizeof(struct hci_ev_le_per_adv_subevent_data_req)),
+	/* [0x28 = HCI_EV_LE_PER_ADV_RESPONSE_REPORT] */
+	HCI_LE_EV_VL(HCI_EV_LE_PER_ADV_RESPONSE_REPORT,
+		     hci_le_per_adv_response_report_evt,
+		     sizeof(struct hci_ev_le_per_adv_response_report),
+		     HCI_MAX_LE_SUBEVT_PLEN),
 	/* [0x2b = HCI_EVT_LE_ALL_REMOTE_FEATURES_COMPLETE] */
 	HCI_LE_EV_VL(HCI_EVT_LE_ALL_REMOTE_FEATURES_COMPLETE,
 		     hci_le_read_all_remote_features_evt,
diff --git a/net/bluetooth/hci_sync.c b/net/bluetooth/hci_sync.c
index 05151cf6f413..71005596f7df 100644
--- a/net/bluetooth/hci_sync.c
+++ b/net/bluetooth/hci_sync.c
@@ -5099,6 +5099,11 @@ static int hci_le_set_event_mask_sync(struct hci_dev *hdev)
 	if (ll_ext_feature_capable(hdev))
 		events[5] |= BIT(2);
 
+	if (pawr_adv_capable(hdev)) {
+		events[4] |= 0x40;	/* LE PA Subevent Data Request */
+		events[4] |= 0x80;	/* LE PA Response Report */
+	}
+
 	if (le_cs_capable(hdev)) {
 		/* Channel Sounding events */
 		events[5] |= 0x08;	/* LE CS Read Remote Supported Cap Complete event */
diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c
index 0f8a58ad1e3c..037cb8513a11 100644
--- a/net/bluetooth/mgmt.c
+++ b/net/bluetooth/mgmt.c
@@ -132,6 +132,7 @@ static const u16 mgmt_commands[] = {
 	MGMT_OP_HCI_CMD_SYNC,
 	MGMT_OP_ADD_PERIODIC_ADV_PARAMS,
 	MGMT_OP_ADD_PERIODIC_ADV_DATA,
+	MGMT_OP_SET_PA_SUBEVENT_DATA,
 };
 
 static const u16 mgmt_events[] = {
@@ -180,6 +181,8 @@ static const u16 mgmt_events[] = {
 	MGMT_EV_ADV_MONITOR_DEVICE_FOUND,
 	MGMT_EV_ADV_MONITOR_DEVICE_LOST,
 	MGMT_EV_SECURITY_LEVEL_CHANGED,
+	MGMT_EV_PA_SUBEVENT_DATA_REQUEST,
+	MGMT_EV_PA_RESPONSE_REPORT,
 };
 
 static const u16 mgmt_untrusted_commands[] = {
@@ -10014,6 +10017,122 @@ static int add_periodic_adv_data(struct sock *sk, struct hci_dev *hdev,
 	return err;
 }
 
+/* The controller's answer to LE Set Periodic Advertising Subevent Data. The
+ * commands are answered in the order they were sent.
+ */
+void mgmt_set_pa_subevent_data_complete(struct hci_dev *hdev, u8 status)
+{
+	struct mgmt_cp_set_pa_subevent_data *cp;
+	struct mgmt_rp_set_pa_subevent_data rp;
+	struct mgmt_pending_cmd *cmd;
+
+	cmd = pending_find(MGMT_OP_SET_PA_SUBEVENT_DATA, hdev);
+	if (!cmd)
+		return;
+
+	cp = cmd->param;
+
+	if (status) {
+		mgmt_cmd_status(cmd->sk, hdev->id, cmd->opcode,
+				mgmt_status(status));
+	} else {
+		rp.instance = cp->instance;
+		mgmt_cmd_complete(cmd->sk, hdev->id, cmd->opcode,
+				  MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
+	}
+
+	mgmt_pending_remove(cmd);
+}
+
+static int set_pa_subevent_data(struct sock *sk, struct hci_dev *hdev,
+				void *data, u16 data_len)
+{
+	DEFINE_RAW_FLEX(struct hci_cp_le_set_per_adv_subevent_data, hci_cp,
+			subevents, HCI_MAX_PER_ADV_SUBEVENT_DATA_LEN);
+	struct mgmt_cp_set_pa_subevent_data *cp = data;
+	struct mgmt_pa_subevent_data *subevent;
+	struct mgmt_pending_cmd *cmd;
+	struct adv_info *adv;
+	u16 len = data_len - sizeof(*cp);
+	u16 left = len;
+	u8 *ptr = cp->subevents;
+	int err;
+	u8 i;
+
+	bt_dev_dbg(hdev, "sock %p", sk);
+
+	BUILD_BUG_ON(sizeof(struct mgmt_pa_subevent_data) !=
+		     sizeof(struct hci_le_per_adv_subevent_data));
+
+	/* One HCI command takes it all, or nothing is sent */
+	if (left > HCI_MAX_PER_ADV_SUBEVENT_DATA_LEN)
+		return mgmt_cmd_status(sk, hdev->id,
+				       MGMT_OP_SET_PA_SUBEVENT_DATA,
+				       MGMT_STATUS_INVALID_PARAMS);
+
+	for (i = 0; i < cp->num_subevents; i++) {
+		subevent = (void *)ptr;
+
+		if (left < sizeof(*subevent) ||
+		    left < sizeof(*subevent) + subevent->data_len)
+			return mgmt_cmd_status(sk, hdev->id,
+					       MGMT_OP_SET_PA_SUBEVENT_DATA,
+					       MGMT_STATUS_INVALID_PARAMS);
+
+		ptr += sizeof(*subevent) + subevent->data_len;
+		left -= sizeof(*subevent) + subevent->data_len;
+	}
+
+	if (!cp->num_subevents || left)
+		return mgmt_cmd_status(sk, hdev->id,
+				       MGMT_OP_SET_PA_SUBEVENT_DATA,
+				       MGMT_STATUS_INVALID_PARAMS);
+
+	hci_dev_lock(hdev);
+
+	if (!hdev_is_powered(hdev)) {
+		err = mgmt_cmd_status(sk, hdev->id,
+				      MGMT_OP_SET_PA_SUBEVENT_DATA,
+				      MGMT_STATUS_REJECTED);
+		goto unlock;
+	}
+
+	adv = hci_find_adv_instance(hdev, cp->instance);
+	if (!adv || !adv->periodic || !adv->num_subevents) {
+		err = mgmt_cmd_status(sk, hdev->id,
+				      MGMT_OP_SET_PA_SUBEVENT_DATA,
+				      MGMT_STATUS_INVALID_PARAMS);
+		goto unlock;
+	}
+
+	cmd = mgmt_pending_add(sk, MGMT_OP_SET_PA_SUBEVENT_DATA, hdev, data,
+			       data_len);
+	if (!cmd) {
+		err = -ENOMEM;
+		goto unlock;
+	}
+
+	/* struct mgmt_pa_subevent_data has the layout of the HCI one */
+	hci_cp->handle = adv->handle;
+	hci_cp->num_subevents = cp->num_subevents;
+	memcpy(hci_cp->subevents, cp->subevents, len);
+
+	/* The controller wants the data within its lead time, a few tens of
+	 * milliseconds. The command is therefore not queued with
+	 * hci_cmd_sync_queue(): it would wait behind sequences which block
+	 * for an event, such as creating a connection.
+	 */
+	err = hci_send_cmd(hdev, HCI_OP_LE_SET_PER_ADV_SUBEVENT_DATA,
+			   struct_size(hci_cp, subevents, len), hci_cp);
+	if (err < 0)
+		mgmt_pending_remove(cmd);
+
+unlock:
+	hci_dev_unlock(hdev);
+
+	return err;
+}
+
 static void remove_advertising_complete(struct hci_dev *hdev, void *data,
 					int err)
 {
@@ -10278,6 +10397,8 @@ static const struct hci_mgmt_handler mgmt_handlers[] = {
 	{ add_periodic_adv_params, MGMT_ADD_PERIODIC_ADV_PARAMS_SIZE },
 	{ add_periodic_adv_data,   MGMT_ADD_PERIODIC_ADV_DATA_SIZE,
 						HCI_MGMT_VAR_LEN },
+	{ set_pa_subevent_data,    MGMT_SET_PA_SUBEVENT_DATA_SIZE,
+						HCI_MGMT_VAR_LEN },
 };
 
 void mgmt_index_added(struct hci_dev *hdev)
@@ -11131,6 +11252,47 @@ void mgmt_adv_monitor_device_lost(struct hci_dev *hdev, u16 handle,
 		   NULL);
 }
 
+void mgmt_pa_subevent_data_request(struct hci_dev *hdev, u8 instance,
+				   u8 subevent_start, u8 subevent_data_count)
+{
+	struct mgmt_ev_pa_subevent_data_request ev;
+
+	ev.instance = instance;
+	ev.subevent_start = subevent_start;
+	ev.subevent_data_count = subevent_data_count;
+
+	mgmt_event(MGMT_EV_PA_SUBEVENT_DATA_REQUEST, hdev, &ev, sizeof(ev),
+		   NULL);
+}
+
+/* responses: num_responses entries of struct hci_le_per_adv_response, which
+ * has the layout of struct mgmt_pa_response, len octets in total.
+ */
+void mgmt_pa_response_report(struct hci_dev *hdev, u8 instance, u8 subevent,
+			     u8 tx_status, u8 num_responses,
+			     const void *responses, u16 len)
+{
+	struct mgmt_ev_pa_response_report *ev;
+	struct sk_buff *skb;
+
+	BUILD_BUG_ON(sizeof(struct mgmt_pa_response) !=
+		     sizeof(struct hci_le_per_adv_response));
+
+	skb = mgmt_alloc_skb(hdev, MGMT_EV_PA_RESPONSE_REPORT,
+			     sizeof(*ev) + len);
+	if (!skb)
+		return;
+
+	ev = skb_put(skb, sizeof(*ev));
+	ev->instance = instance;
+	ev->subevent = subevent;
+	ev->tx_status = tx_status;
+	ev->num_responses = num_responses;
+	skb_put_data(skb, responses, len);
+
+	mgmt_event_skb(skb, NULL);
+}
+
 static void mgmt_send_adv_monitor_device_found(struct hci_dev *hdev,
 					       struct sk_buff *skb,
 					       struct sock *skip_sk,
-- 
2.47.3


      parent reply	other threads:[~2026-11-27  8:54 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  8:53 [RFC PATCH 0/5] Bluetooth: Periodic advertising and PAwR over MGMT Roland Schober
2026-11-27  8:53 ` [RFC PATCH 1/5] Bluetooth: hci: Add Periodic Advertising with Responses definitions Roland Schober
2026-11-27  8:53 ` [RFC PATCH 2/5] Bluetooth: hci_sync: Disable periodic advertising before removing a set Roland Schober
2026-11-27  8:53 ` [RFC PATCH 3/5] Bluetooth: MGMT: Add Add Periodic Advertising Parameters command Roland Schober
2026-11-27  8:53 ` [RFC PATCH 4/5] Bluetooth: MGMT: Add Add Periodic Advertising Data command Roland Schober
2026-11-27  8:54 ` Roland Schober [this message]

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=20261127085401.263615-6-rolandschober1968@gmail.com \
    --to=rolandschober1968@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=luiz.dentz@gmail.com \
    --cc=marcel@holtmann.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox