Skip to contentSkip to conversation

Bluetooth: Add synchronous HCI command helpers

PATCH New Johan Hedberg 1 participant 5 messages Apr 4, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover Bluetooth: Add synchronous HCI command helpers

Hi,

Same as v2 except that the necessary EXPORT_SYMBOL declarations have been added to __hci_cmd_sync and __hci_cmd_sync_ev.

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          |  123 ++++++++++++++++++++++++++++++++++++-
 net/bluetooth/hci_event.c         |   23 ++++++-
 4 files changed, 153 insertions(+), 2 deletions(-)
#1 lore raw

PATCH v3 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 9e07f2f..40249f6 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
#2 lore raw

PATCH v3 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         |   97 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 100 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 +97 -0

103 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#3 lore raw

PATCH v3 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 f3e9768..a02c4b2 100644
@@ -2625,7 +2625,8 @@ int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, void *param)
 EXPORT_SYMBOL(hci_send_cmd);
 
 /* 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;
@@ -2649,9 +2650,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
#4 lore raw

PATCH v3 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         |   23 ++++++++++++++++++-----
 2 files changed, 20 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 +18 -5
index a02c4b2..719510a 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,14 @@ 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);
+}
+EXPORT_SYMBOL(__hci_cmd_sync_ev);
+
+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);
 }
 EXPORT_SYMBOL(__hci_cmd_sync);
 
Signature · 1 line
1.7.10.4