Skip to contentSkip to conversation

Bluetooth: Add synchronous HCI command helpers

PATCH Superseded Johan Hedberg 2 participants 9 messages Apr 4, 2013

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Bluetooth: Add synchronous HCI command helpers

Hi,

Here's my two previous patch sets combined into one with the comments received so far taken into account.

Johan

----------------------------------------------------------------
Johan Hedberg (4):
Bluetooth: Track received events in hdev
Bluetooth: Add __hci_cmd_sync() helper function
Bluetooth: Add support for custom event terminated commands
Bluetooth: Add __hci_cmd_sync_ev function

 include/net/bluetooth/bluetooth.h |    1 +
 include/net/bluetooth/hci_core.h  |    8 +++
 net/bluetooth/hci_core.c          |  121 ++++++++++++++++++++++++++++++++++++-
 net/bluetooth/hci_event.c         |   23 ++++++-
 4 files changed, 151 insertions(+), 2 deletions(-)
#1 lore raw

PATCH v2 1/4 Bluetooth: Track received events in hdev

From: Johan Hedberg <[email protected]>

This patch adds tracking of received HCI events to the hci_dev struct. This is necessary so that a subsequent patch can implement a function for sending a single command synchronously and returning the resulting command complete parameters in the function return value.

---
 include/net/bluetooth/hci_core.h |    1 +
 net/bluetooth/hci_core.c         |    3 +++
 net/bluetooth/hci_event.c        |   12 ++++++++++++
 3 files changed, 16 insertions(+)
include/net/bluetooth/hci_core.h +1 -0
index 88dd46e..beae2e8 100644
@@ -244,6 +244,7 @@ struct hci_dev {
 	struct sk_buff_head	raw_q;
 	struct sk_buff_head	cmd_q;
 
+	struct sk_buff		*recv_evt;
 	struct sk_buff		*sent_cmd;
 	struct sk_buff		*reassembly[NUM_REASSEMBLY];
 
net/bluetooth/hci_core.c +3 -0
index 2b488ed..6739cce 100644
@@ -1123,6 +1123,9 @@ static int hci_dev_do_close(struct hci_dev *hdev)
 		hdev->sent_cmd = NULL;
 	}
 
+	kfree_skb(hdev->recv_evt);
+	hdev->recv_evt = NULL;
+
 	/* After this point our queues are empty
 	 * and no tasks are scheduled. */
 	hdev->close(hdev);
net/bluetooth/hci_event.c +12 -0
index 7e7fbca..ed0efb7 100644
@@ -3699,6 +3699,18 @@ void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
 	struct hci_event_hdr *hdr = (void *) skb->data;
 	__u8 event = hdr->evt;
 
+	hci_dev_lock(hdev);
+
+	/* Received events are (currently) only needed when a request is
+	 * ongoing so avoid unnecessary memory allocation.
+	 */
+	if (hdev->req_status == HCI_REQ_PEND) {
+		kfree_skb(hdev->recv_evt);
+		hdev->recv_evt = skb_clone(skb, GFP_KERNEL);
+	}
+
+	hci_dev_unlock(hdev);
+
 	skb_pull(skb, HCI_EVENT_HDR_SIZE);
 
 	switch (event) {
Signature · 1 line
1.7.10.4
maintainer Acked-by on v2 1/4
#2 lore raw

Re: [PATCH v2 1/4] Bluetooth: Track received events in hdev

Hi Johan,

Quoted 11 lines

This patch adds tracking of received HCI events to the hci_dev struct. This is necessary so that a subsequent patch can implement a function for sending a single command synchronously and returning the resulting command complete parameters in the function return value.

---

include/net/bluetooth/hci_core.h |    1 +
net/bluetooth/hci_core.c         |    3 +++
net/bluetooth/hci_event.c        |   12 ++++++++++++
3 files changed, 16 insertions(+)

Regards

Marcel

#3 lore raw

PATCH v2 2/4 Bluetooth: Add __hci_cmd_sync() helper function

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

This patch adds a helper function for sending a single HCI command waiting for its completion and then returning back the parameters in the resulting command complete event (if there was one).

The implementation is very similar to that of hci_req_sync() except that instead of invocing a callback for sending HCI commands the function constructs and sends one itself and after being woken up picks the last received event from hdev->recv_evt (if it matches the right criteria) and returns it.

---
 include/net/bluetooth/hci_core.h |    3 ++
 net/bluetooth/hci_core.c         |   96 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 99 insertions(+)
include/net/bluetooth/hci_core.h +3 -0
index beae2e8..38b653d 100644
@@ -1056,6 +1056,9 @@ int hci_req_run(struct hci_request *req, hci_req_complete_t complete);
 void hci_req_add(struct hci_request *req, u16 opcode, u32 plen, void *param);
 void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status);
 
+struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
+			       void *param, u32 timeout);
+
 int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, void *param);
 void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags);
 void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb);
net/bluetooth/hci_core.c +96 -0

102 lines not on this page · raw message

Signature · 1 line
1.7.10.4
maintainer Acked-by on v2 2/4
#4 lore raw

Re: [PATCH v2 2/4] Bluetooth: Add __hci_cmd_sync() helper function

Hi Johan,

Quoted 46 lines

This patch adds a helper function for sending a single HCI command waiting for its completion and then returning back the parameters in the resulting command complete event (if there was one).

The implementation is very similar to that of hci_req_sync() except that instead of invocing a callback for sending HCI commands the function constructs and sends one itself and after being woken up picks the last received event from hdev->recv_evt (if it matches the right criteria) and returns it.

---
include/net/bluetooth/hci_core.h | 3 ++
net/bluetooth/hci_core.c | 96 ++++++++++++++++++++++++++++++++++++++

2 files changed, 99 insertions(+)
include/net/bluetooth/hci_core.h +3 -0
index beae2e8..38b653d 100644
@@ -1056,6 +1056,9 @@ int hci_req_run(struct hci_request *req, hci_req_complete_t complete);
 void hci_req_add(struct hci_request *req, u16 opcode, u32 plen, void *param);
 void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status);
 
+struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
+			       void *param, u32 timeout);
+
 int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, void *param);
 void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags);
 void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb);
net/bluetooth/hci_core.c +8 -0
index 6739cce..d0c12c0 100644
@@ -79,6 +79,102 @@ static void hci_req_cancel(struct hci_dev *hdev, int err)
 	}
 }
 
+struct sk_buff *hci_get_cmd_complete(struct hci_dev *hdev, u16 opcode)
+{
+	struct hci_ev_cmd_complete *ev;
+	struct hci_event_hdr *hdr;
+	struct sk_buff *skb;
+
+	hci_dev_lock(hdev);
+

net/bluetooth/hci_core.c static void hci_req_cancel(struct hci_dev *hdev, int err) · line 99

Quoted 10 lines
net/bluetooth/hci_core.c +10 -0
+	skb = hdev->recv_evt;
+	hdev->recv_evt = NULL;
+
+	hci_dev_unlock(hdev);
+
+	if (!skb)
+		return ERR_PTR(-ENODATA);
+
+	hdr = (void *) skb->data;
+	skb_pull(skb, HCI_EVENT_HDR_SIZE);

do we check the event header length earlier and can rely on that it is valid? If not, then lets add another sanity check here.

Otherwise this patch looks fine to me.

Regards

Marcel

#5 lore raw

PATCH v2 3/4 Bluetooth: Add support for custom event terminated commands

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

This patch adds support for having commands within HCI requests that do not result in a command complete but some other event. This is at least needed for some vendor specific commands to be issued in the hdev->setup() procecure, but might also be useful for other commands.

The way that the support is implemented is by extending the skb control buffer to have a field to indicate that the command is expected to terminate with a special event. After sending the command each received event can then be compared against this field through hdev->sent_cmd.

---
 include/net/bluetooth/bluetooth.h |    1 +
 include/net/bluetooth/hci_core.h  |    2 ++
 net/bluetooth/hci_core.c          |   10 +++++++++-
 net/bluetooth/hci_event.c         |   11 ++++++++++-
 4 files changed, 22 insertions(+), 2 deletions(-)
include/net/bluetooth/bluetooth.h +1 -0
index ed6e955..591fee7 100644
@@ -266,6 +266,7 @@ typedef void (*hci_req_complete_t)(struct hci_dev *hdev, u8 status);
 
 struct hci_req_ctrl {
 	bool			start;
+	u8			event;
 	hci_req_complete_t	complete;
 };
 
include/net/bluetooth/hci_core.h +2 -0
index 38b653d..b2be51c 100644
@@ -1054,6 +1054,8 @@ struct hci_request {
 void hci_req_init(struct hci_request *req, struct hci_dev *hdev);
 int hci_req_run(struct hci_request *req, hci_req_complete_t complete);
 void hci_req_add(struct hci_request *req, u16 opcode, u32 plen, void *param);
+void hci_req_add_ev(struct hci_request *req, u16 opcode, u32 plen, void *param,
+		    u8 event);
 void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status);
 
 struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
net/bluetooth/hci_core.c +9 -1
index d0c12c0..91ef2cc 100644
@@ -2623,7 +2623,8 @@ int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, void *param)
 }
 
 /* Queue a command to an asynchronous HCI request */
-void hci_req_add(struct hci_request *req, u16 opcode, u32 plen, void *param)
+void hci_req_add_ev(struct hci_request *req, u16 opcode, u32 plen, void *param,
+		    u8 event)
 {
 	struct hci_dev *hdev = req->hdev;
 	struct sk_buff *skb;
@@ -2647,9 +2648,16 @@ void hci_req_add(struct hci_request *req, u16 opcode, u32 plen, void *param)
 	if (skb_queue_empty(&req->cmd_q))
 		bt_cb(skb)->req.start = true;
 
+	bt_cb(skb)->req.event = event;
+
 	skb_queue_tail(&req->cmd_q, skb);
 }
 
+void hci_req_add(struct hci_request *req, u16 opcode, u32 plen, void *param)
+{
+	hci_req_add_ev(req, opcode, plen, param, 0);
+}
+
 /* Get data from the previously sent command */
 void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode)
 {
net/bluetooth/hci_event.c +10 -1
index ed0efb7..0a2b128 100644
@@ -2463,7 +2463,9 @@ static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb)
 	if (opcode != HCI_OP_NOP)
 		del_timer(&hdev->cmd_timer);
 
-	hci_req_cmd_complete(hdev, opcode, ev->status);
+	if (ev->status ||
+	    (hdev->sent_cmd && !bt_cb(hdev->sent_cmd)->req.event))
+		hci_req_cmd_complete(hdev, opcode, ev->status);
 
 	if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags)) {
 		atomic_set(&hdev->cmd_cnt, 1);
@@ -3713,6 +3715,13 @@ void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
 
 	skb_pull(skb, HCI_EVENT_HDR_SIZE);
 
+	if (hdev->sent_cmd && bt_cb(hdev->sent_cmd)->req.event == event) {
+		struct hci_command_hdr *hdr = (void *) hdev->sent_cmd->data;
+		u16 opcode = __le16_to_cpu(hdr->opcode);
+
+		hci_req_cmd_complete(hdev, opcode, 0);
+	}
+
 	switch (event) {
 	case HCI_EV_INQUIRY_COMPLETE:
 		hci_inquiry_complete_evt(hdev, skb);
Signature · 1 line
1.7.10.4
maintainer Acked-by on v2 3/4
#6 lore raw

Re: [PATCH v2 3/4] Bluetooth: Add support for custom event terminated commands

Hi Johan,

Quoted 10 lines

This patch adds support for having commands within HCI requests that do not result in a command complete but some other event. This is at least needed for some vendor specific commands to be issued in the hdev->setup() procecure, but might also be useful for other commands.

The way that the support is implemented is by extending the skb control buffer to have a field to indicate that the command is expected to terminate with a special event. After sending the command each received event can then be compared against this field through hdev->sent_cmd.

Quoted 7 lines

---

include/net/bluetooth/bluetooth.h |    1 +
include/net/bluetooth/hci_core.h  |    2 ++
net/bluetooth/hci_core.c          |   10 +++++++++-
net/bluetooth/hci_event.c         |   11 ++++++++++-
4 files changed, 22 insertions(+), 2 deletions(-)

Regards

Marcel

#7 lore raw

PATCH v2 4/4 Bluetooth: Add __hci_cmd_sync_ev function

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

This patch adds a __hci_cmd_sync_ev function, analogous to __hci_cmd_sync except that it also takes an event parameter to indicate that the command completes with a special event instead of command complete. Internally this new function takes advantage of the hci_req_add_ev function introduced in the previous patch.

The primary expected user of this new function are the setup routines of HCI drivers which may want to send custom commands and return only when they have completed.

---
 include/net/bluetooth/hci_core.h |    2 ++
 net/bluetooth/hci_core.c         |   22 +++++++++++++++++-----
 2 files changed, 19 insertions(+), 5 deletions(-)
include/net/bluetooth/hci_core.h +2 -0
index b2be51c..d4e13bf 100644
@@ -1060,6 +1060,8 @@ void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status);
 
 struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
 			       void *param, u32 timeout);
+struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
+				  void *param, u8 event, u32 timeout);
 
 int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, void *param);
 void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags);
net/bluetooth/hci_core.c +17 -5
index 91ef2cc..117ed69 100644
@@ -79,7 +79,7 @@ static void hci_req_cancel(struct hci_dev *hdev, int err)
 	}
 }
 
-struct sk_buff *hci_get_cmd_complete(struct hci_dev *hdev, u16 opcode)
+struct sk_buff *hci_get_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 event)
 {
 	struct hci_ev_cmd_complete *ev;
 	struct hci_event_hdr *hdr;
@@ -98,6 +98,12 @@ struct sk_buff *hci_get_cmd_complete(struct hci_dev *hdev, u16 opcode)
 	hdr = (void *) skb->data;
 	skb_pull(skb, HCI_EVENT_HDR_SIZE);
 
+	if (event) {
+		if (hdr->evt != event)
+			goto failed;
+		return skb;
+	}
+
 	if (hdr->evt != HCI_EV_CMD_COMPLETE) {
 		BT_DBG("Last event is not cmd complete (0x%2.2x)", hdr->evt);
 		goto failed;
@@ -122,8 +128,8 @@ failed:
 	return ERR_PTR(-ENODATA);
 }
 
-struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
-			       void *param, u32 timeout)
+struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
+				  void *param, u8 event, u32 timeout)
 {
 	DECLARE_WAITQUEUE(wait, current);
 	struct hci_request req;
@@ -133,7 +139,7 @@ struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
 
 	hci_req_init(&req, hdev);
 
-	hci_req_add(&req, opcode, plen, param);
+	hci_req_add_ev(&req, opcode, plen, param, event);
 
 	hdev->req_status = HCI_REQ_PEND;
 
@@ -172,7 +178,13 @@ struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
 	if (err < 0)
 		return ERR_PTR(err);
 
-	return hci_get_cmd_complete(hdev, opcode);
+	return hci_get_cmd_complete(hdev, opcode, event);
+}
+
+struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
+			       void *param, u32 timeout)
+{
+	return __hci_cmd_sync_ev(hdev, opcode, plen, param, 0, timeout);
 }
 
 /* Execute request and wait for completion. */
Signature · 1 line
1.7.10.4
maintainer Acked-by on v2 4/4
#8 lore raw

Re: [PATCH v2 4/4] Bluetooth: Add __hci_cmd_sync_ev function

Hi Johan,

Quoted 10 lines

This patch adds a __hci_cmd_sync_ev function, analogous to __hci_cmd_sync except that it also takes an event parameter to indicate that the command completes with a special event instead of command complete. Internally this new function takes advantage of the hci_req_add_ev function introduced in the previous patch.

The primary expected user of this new function are the setup routines of HCI drivers which may want to send custom commands and return only when they have completed.

Quoted 5 lines

---
include/net/bluetooth/hci_core.h | 2 ++
net/bluetooth/hci_core.c | 22 +++++++++++++++++-----

2 files changed, 19 insertions(+), 5 deletions(-)

Regards

Marcel