Skip to contentSkip to conversation

Bluetooth: Add __hci_cmd_sync() helper

PATCH Superseded Johan Hedberg 3 participants 7 messages Apr 3, 2013 – Apr 4, 2013

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Bluetooth: Add __hci_cmd_sync() helper

Hi,

This function is mainly intended as a helper for the hdev->setup() routine which needs to be able to wait for completion of its commands before returning (i.e. a pre-requisite is Marcel's patch set I sent yesterday).

Johan

----------------------------------------------------------------
Johan Hedberg (2):
Bluetooth: Track received events in hdev
Bluetooth: Add __hci_cmd_sync() helper function

 include/net/bluetooth/hci_core.h |    5 ++
 net/bluetooth/hci_core.c         |   98 ++++++++++++++++++++++++++++++++++++++
 net/bluetooth/hci_event.c        |    8 ++++
 3 files changed, 111 insertions(+)
#1 lore raw

PATCH 1/2 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 |    2 ++
 net/bluetooth/hci_core.c         |    5 +++++
 net/bluetooth/hci_event.c        |    8 ++++++++
 3 files changed, 15 insertions(+)
include/net/bluetooth/hci_core.h +2 -0
index 88dd46e..0a1cb19 100644
@@ -244,6 +244,8 @@ 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 +5 -0
index 2b488ed..7008b4d 100644
@@ -1123,6 +1123,11 @@ static int hci_dev_do_close(struct hci_dev *hdev)
 		hdev->sent_cmd = NULL;
 	}
 
+	if (hdev->recv_evt) {
+		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 +8 -0
index 7e7fbca..24906fb 100644
@@ -3699,6 +3699,14 @@ 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);
+
+	if (hdev->recv_evt)
+		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
#2 lore raw

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

Hi Johan,

Quoted 17 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 |    2 ++
net/bluetooth/hci_core.c         |    5 +++++
net/bluetooth/hci_event.c        |    8 ++++++++
3 files changed, 15 insertions(+)
include/net/bluetooth/hci_core.h +0 -0
index 88dd46e..0a1cb19 100644

include/net/bluetooth/hci_core.h struct hci_dev { · line 248

Quoted 5 lines
include/net/bluetooth/hci_core.h +2 -0
@@ -244,6 +244,8 @@ struct hci_dev {
 	struct sk_buff_head	raw_q;
 	struct sk_buff_head	cmd_q;
 
+	struct sk_buff		*recv_evt;
+

loose the empty line here.

Quoted 8 lines
include/net/bluetooth/hci_core.h +0 -0
 	struct sk_buff		*sent_cmd;
 	struct sk_buff		*reassembly[NUM_REASSEMBLY];
 
net/bluetooth/hci_core.c +0 -0
index 2b488ed..7008b4d 100644

net/bluetooth/hci_core.c static int hci_dev_do_close(struct hci_dev *hdev) · line 1130

Quoted 8 lines
net/bluetooth/hci_core.c +5 -0
@@ -1123,6 +1123,11 @@ static int hci_dev_do_close(struct hci_dev *hdev)
 		hdev->sent_cmd = NULL;
 	}
 
+	if (hdev->recv_evt) {
+		kfree_skb(hdev->recv_evt);
+		hdev->recv_evt = NULL;
+	}
+

Actually kfree_skb does the NULL pointer check. So you do not need that one.

Quoted 8 lines
net/bluetooth/hci_core.c +0 -0
 	/* After this point our queues are empty
 	 * and no tasks are scheduled. */
 	hdev->close(hdev);
net/bluetooth/hci_event.c +0 -0
index 7e7fbca..24906fb 100644

net/bluetooth/hci_event.c void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb) · line 3706

Quoted 8 lines
net/bluetooth/hci_event.c +5 -0
@@ -3699,6 +3699,14 @@ 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);
+
+	if (hdev->recv_evt)
+		kfree_skb(hdev->recv_evt);
+	hdev->recv_evt = skb_clone(skb, GFP_KERNEL);

Same here.

However since this actually means memory allocation for every single event we receive, can we just optimize this to be only done when we actually have a HCI request ongoing.

Regards

Marcel

#3 lore raw

PATCH 2/2 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).

---
 include/net/bluetooth/hci_core.h |    3 ++
 net/bluetooth/hci_core.c         |   93 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 96 insertions(+)
include/net/bluetooth/hci_core.h +3 -0
index 0a1cb19..064b057 100644
@@ -1057,6 +1057,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 +93 -0

99 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#4 lore raw

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

Hi Johan,

Quoted 10 lines · Johan Hedberg
net/bluetooth/hci_core.c +10 -0
+       switch (hdev->req_status) {
+       case HCI_REQ_DONE:
+               err = -bt_to_errno(hdev->req_result);
+               break;
+
+       case HCI_REQ_CANCELED:
+               err = -hdev->req_result;
+               break;
+
+       default:

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

Quoted 10 lines
net/bluetooth/hci_core.c +10 -0
+               err = -ETIMEDOUT;
+               break;
+       }
+
+       hdev->req_status = hdev->req_result = 0;
+
+       BT_DBG("%s end: err %d", hdev->name, err);
+
+       if (err < 0)
+               return ERR_PTR(err);

Isn't this always true (due to the switch() avove) ?

Regards,

Signature · 3 lines
Anderson Lizardo
Instituto Nokia de Tecnologia - INdT
Manaus - Brazil
#5 lore raw

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

Hi Johan,

Quoted 17 lines · Anderson Lizardo

Hi Johan,

Quoted 14 lines · Johan Hedberg
net/bluetooth/hci_core.c +14 -0
+       switch (hdev->req_status) {
+       case HCI_REQ_DONE:
+               err = -bt_to_errno(hdev->req_result);
+               break;
+
+       case HCI_REQ_CANCELED:
+               err = -hdev->req_result;
+               break;
+
+       default:
+               err = -ETIMEDOUT;
+               break;
+       }
+
Quoted 8 lines

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

Quoted 6 lines
net/bluetooth/hci_core.c +6 -0
+       hdev->req_status = hdev->req_result = 0;
+
+       BT_DBG("%s end: err %d", hdev->name, err);
+
+       if (err < 0)
+               return ERR_PTR(err);

Isn't this always true (due to the switch() avove) ?

Ignore that, req_result might be zero...

Regards,

Signature · 3 lines
Anderson Lizardo
Instituto Nokia de Tecnologia - INdT
Manaus - Brazil
maintainer
#6 lore raw

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

In reply to Johan Hedberg

Hi Johan,

Quoted 32 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).

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

2 files changed, 96 insertions(+)
include/net/bluetooth/hci_core.h +3 -0
index 0a1cb19..064b057 100644
@@ -1057,6 +1057,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 +0 -0
index 7008b4d..689c959 100644
@@ -79,6 +79,99 @@ static void hci_req_cancel(struct hci_dev *hdev, int err)
 	}
 }
 

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

Quoted 10 lines
net/bluetooth/hci_core.c +10 -0
+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);
+
+	if (hdev->recv_evt)
+		skb = skb_clone(hdev->recv_evt, GFP_KERNEL);

why are we cloning this again. Can we not just return recv_evt and set it to NULL afterwards.

Quoted 8 lines
net/bluetooth/hci_core.c +8 -0
+	else
+		skb = NULL;
+
+	hci_dev_unlock(hdev);
+
+	if (!skb)
+		return ERR_PTR(-ENODATA);
+

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

Quoted 10 lines
net/bluetooth/hci_core.c +10 -0
+	hdr = (void *) skb->data;
+	skb_pull(skb, HCI_EVENT_HDR_SIZE);
+
+	if (hdr->evt != HCI_EV_CMD_COMPLETE) {
+		BT_DBG("Last event is not cmd complete (0x%2.2x)", hdr->evt);
+		goto failed;
+	}
+
+	ev = (void *) skb->data;
+	skb_pull(skb, sizeof(*ev));

Don't you think we should add some sanity length checks here.

Quoted 58 lines
net/bluetooth/hci_core.c +58 -0
+
+	if (opcode == __le16_to_cpu(ev->opcode))
+		return skb;
+
+	BT_DBG("opcode doesn't match (0x%2.2x != 0x%2.2x)", opcode,
+	       __le16_to_cpu(ev->opcode));
+
+failed:
+	kfree_skb(skb);
+	return ERR_PTR(-ENODATA);
+}
+
+struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
+			       void *param, u32 timeout)
+{
+	DECLARE_WAITQUEUE(wait, current);
+	struct hci_request req;
+	int err = 0;
+
+	BT_DBG("%s", hdev->name);
+
+	hci_req_init(&req, hdev);
+
+	hci_req_add(&req, opcode, plen, param);
+
+	hdev->req_status = HCI_REQ_PEND;
+
+	err = hci_req_run(&req, hci_req_sync_complete);
+	if (err < 0)
+		return ERR_PTR(err);
+
+	add_wait_queue(&hdev->req_wait_q, &wait);
+	set_current_state(TASK_INTERRUPTIBLE);
+
+	schedule_timeout(timeout);
+
+	remove_wait_queue(&hdev->req_wait_q, &wait);
+
+	if (signal_pending(current))
+		return ERR_PTR(-EINTR);
+
+	switch (hdev->req_status) {
+	case HCI_REQ_DONE:
+		err = -bt_to_errno(hdev->req_result);
+		break;
+
+	case HCI_REQ_CANCELED:
+		err = -hdev->req_result;
+		break;
+
+	default:
+		err = -ETIMEDOUT;
+		break;
+	}
+
+	hdev->req_status = hdev->req_result = 0;
+
+	BT_DBG("%s end: err %d", hdev->name, err);

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

Quoted 10 lines
net/bluetooth/hci_core.c +7 -0
+
+	if (err < 0)
+		return ERR_PTR(err);
+
+	return hci_get_cmd_complete(hdev, opcode);
+}
+
 /* Execute request and wait for completion. */
 static int __hci_req_sync(struct hci_dev *hdev,
 			  void (*func)(struct hci_request *req,

Regards

Marcel