Linux bluetooth development
 help / color / mirror / Atom feed
From: Johan Hedberg <johan.hedberg@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH v3 07/16] Bluetooth: Add transaction cmd_complete and cmd_status functions
Date: Wed, 27 Feb 2013 09:57:15 +0200	[thread overview]
Message-ID: <1361951844-13719-8-git-send-email-johan.hedberg@gmail.com> (raw)
In-Reply-To: <1361951844-13719-1-git-send-email-johan.hedberg@gmail.com>

From: Johan Hedberg <johan.hedberg@intel.com>

This patch introduces functions to process the transaction state when
receiving HCI Command Status or Command Complete events. Some HCI
commands, like Inquiry do not result in a Command complete event so
special handling is needed for them. Inquiry is a particularly important
one since it is the only forseeable "non-cmd_complete" command that will
make good use of the transaction framework, and its completion is either
indicated by an Inquiry Complete event of a successful Command Complete
for HCI_Inquiry_Cancel.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
---
 include/net/bluetooth/hci_core.h |    2 ++
 net/bluetooth/hci_core.c         |   68 ++++++++++++++++++++++++++++++++++++++
 net/bluetooth/hci_event.c        |    7 ++++
 3 files changed, 77 insertions(+)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 8c2553f..038f8c7a 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -1056,6 +1056,8 @@ int hci_transaction_run(struct hci_transaction *transaction);
 int hci_transaction_cmd(struct hci_transaction *transaction, u16 opcode,
 			u32 plen, void *param);
 void hci_transaction_from_skb(struct hci_dev *hdev, struct sk_buff *skb);
+void hci_transaction_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status);
+void hci_transaction_cmd_status(struct hci_dev *hdev, u16 opcode, u8 status);
 
 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);
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 315728c..15ec531 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -3216,6 +3216,74 @@ static void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb)
 	kfree_skb(skb);
 }
 
+static bool hci_transaction_is_complete(struct hci_dev *hdev)
+{
+	struct sk_buff *skb;
+
+	skb = skb_peek(&hdev->cmd_q);
+	if (!skb)
+		return true;
+
+	return bt_cb(skb)->transaction.start;
+}
+
+void hci_transaction_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status)
+{
+	struct sk_buff *skb;
+
+	BT_DBG("opcode 0x%04x status 0x%02x", opcode, status);
+
+	/* Check that the completed command really matches the last one
+	 * that was sent.
+	 */
+	if (!hci_sent_cmd_data(hdev, opcode))
+		return;
+
+	/* If the command succeeded and there's still more commands in
+	 * this transaction the transaction is not yet complete.
+	 */
+	if (!status && !hci_transaction_is_complete(hdev))
+		return;
+
+	/* Remove all pending commands belonging to this transaction */
+	spin_lock(&hdev->cmd_q.lock);
+	while ((skb = __skb_dequeue(&hdev->cmd_q))) {
+		if (bt_cb(skb)->transaction.start) {
+			__skb_queue_head(&hdev->cmd_q, skb);
+			break;
+		}
+
+		kfree_skb(skb);
+	}
+	spin_unlock(&hdev->cmd_q.lock);
+
+	if (hdev->transaction_complete) {
+		hdev->transaction_complete(hdev, opcode, status);
+		hdev->transaction_complete = NULL;
+	}
+}
+
+void hci_transaction_cmd_status(struct hci_dev *hdev, u16 opcode, u8 status)
+{
+	BT_DBG("opcode 0x%04x status 0x%02x", opcode, status);
+
+	if (status) {
+		hci_transaction_cmd_complete(hdev, opcode, status);
+		return;
+	}
+
+	/* No need to handle success status if there are more commands */
+	if (!hci_transaction_is_complete(hdev))
+		return;
+
+	/* If the transaction doesn't have a complete callback or there
+	 * are other commands/transactions in the hdev queue we consider
+	 * this transaction as completed.
+	 */
+	if (!hdev->transaction_complete || !skb_queue_empty(&hdev->cmd_q))
+		hci_transaction_cmd_complete(hdev, opcode, status);
+}
+
 static void hci_rx_work(struct work_struct *work)
 {
 	struct hci_dev *hdev = container_of(work, struct hci_dev, rx_work);
diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
index 14e872a..6dd5fd4 100644
--- a/net/bluetooth/hci_event.c
+++ b/net/bluetooth/hci_event.c
@@ -53,6 +53,7 @@ static void hci_cc_inquiry_cancel(struct hci_dev *hdev, struct sk_buff *skb)
 	hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
 	hci_dev_unlock(hdev);
 
+	hci_transaction_cmd_complete(hdev, HCI_OP_INQUIRY, status);
 	hci_req_complete(hdev, HCI_OP_INQUIRY_CANCEL, status);
 
 	hci_conn_check_pending(hdev);
@@ -1692,6 +1693,7 @@ static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
 
 	BT_DBG("%s status 0x%2.2x", hdev->name, status);
 
+	hci_transaction_cmd_complete(hdev, HCI_OP_INQUIRY, status);
 	hci_req_complete(hdev, HCI_OP_INQUIRY, status);
 
 	hci_conn_check_pending(hdev);
@@ -2254,6 +2256,7 @@ static void hci_qos_setup_complete_evt(struct hci_dev *hdev,
 static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
 {
 	struct hci_ev_cmd_complete *ev = (void *) skb->data;
+	u8 status = skb->data[sizeof(*ev)];
 	__u16 opcode;
 
 	skb_pull(skb, sizeof(*ev));
@@ -2497,6 +2500,8 @@ static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
 	if (ev->opcode != HCI_OP_NOP)
 		del_timer(&hdev->cmd_timer);
 
+	hci_transaction_cmd_complete(hdev, ev->opcode, status);
+
 	if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags)) {
 		atomic_set(&hdev->cmd_cnt, 1);
 		if (!skb_queue_empty(&hdev->cmd_q))
@@ -2590,6 +2595,8 @@ static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb)
 	if (ev->opcode != HCI_OP_NOP)
 		del_timer(&hdev->cmd_timer);
 
+	hci_transaction_cmd_status(hdev, ev->opcode, ev->status);
+
 	if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags)) {
 		atomic_set(&hdev->cmd_cnt, 1);
 		if (!skb_queue_empty(&hdev->cmd_q))
-- 
1.7.10.4


  parent reply	other threads:[~2013-02-27  7:57 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-02-27  7:57 [PATCH v3 00/16] Bluetooth: Add HCI transaction framework Johan Hedberg
2013-02-27  7:57 ` [PATCH v3 01/16] Bluetooth: Fix __hci_request() handling of empty requests Johan Hedberg
2013-02-27  7:57 ` [PATCH v3 02/16] Bluetooth: Split HCI init sequence into three stages Johan Hedberg
2013-02-28 19:54   ` Marcel Holtmann
2013-03-01  6:55     ` Johan Hedberg
2013-02-27  7:57 ` [PATCH v3 03/16] Bluetooth: Add initial skeleton for HCI transaction framework Johan Hedberg
2013-02-28 19:52   ` Marcel Holtmann
2013-03-01  7:04     ` Johan Hedberg
2013-03-01  7:30       ` Marcel Holtmann
2013-03-01 10:03         ` Johan Hedberg
2013-03-01 10:10           ` Johan Hedberg
2013-03-01 16:07             ` Marcel Holtmann
2013-03-01 16:13           ` Marcel Holtmann
2013-02-27  7:57 ` [PATCH v3 04/16] Bluetooth: Refactor HCI command skb creation Johan Hedberg
2013-02-27  7:57 ` [PATCH v3 05/16] Bluetooth: Introduce new hci_transaction_cmd function Johan Hedberg
2013-02-27  7:57 ` [PATCH v3 06/16] Bluetooth: Introduce a hci_transaction_from_skb function Johan Hedberg
2013-02-27  7:57 ` Johan Hedberg [this message]
2013-02-27  7:57 ` [PATCH v3 08/16] Bluetooth: Convert hci_request to use HCI transaction framework Johan Hedberg
2013-02-27  7:57 ` [PATCH v3 09/16] Bluetooth: Remove unused hdev->init_last_cmd Johan Hedberg
2013-02-27  7:57 ` [PATCH v3 10/16] Bluetooth: Move power on HCI command updates to their own function Johan Hedberg
2013-02-27  7:57 ` [PATCH v3 11/16] Bluetooth: Update mgmt powered HCI commands to use transactions Johan Hedberg
2013-02-27  7:57 ` [PATCH v3 12/16] Bluetooth: Wait for HCI command completion with mgmt_set_powered Johan Hedberg
2013-02-27  7:57 ` [PATCH v3 13/16] Bluetooth: Fix busy condition testing for EIR and class updates Johan Hedberg
2013-02-28 20:01   ` Marcel Holtmann
2013-03-01  7:32     ` Johan Hedberg
2013-03-01  8:00       ` Marcel Holtmann
2013-03-01  8:39         ` Johan Hedberg
2013-03-01 16:02           ` Marcel Holtmann
2013-02-27  7:57 ` [PATCH v3 14/16] Bluetooth: Fix UUID/class mgmt command response synchronization Johan Hedberg
2013-02-27  7:57 ` [PATCH v3 15/16] Bluetooth: Remove useless HCI_PENDING_CLASS flag Johan Hedberg
2013-02-27  7:57 ` [PATCH v3 16/16] Bluetooth: Remove empty HCI event handlers Johan Hedberg
2013-02-28 23:05 ` [PATCH v3 00/16] Bluetooth: Add HCI transaction framework Vinicius Costa Gomes
2013-03-01  8:46   ` Johan Hedberg

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=1361951844-13719-8-git-send-email-johan.hedberg@gmail.com \
    --to=johan.hedberg@gmail.com \
    --cc=linux-bluetooth@vger.kernel.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