Skip to contentSkip to conversation

Bluetooth: Fix mgmt handling of power on failures

PATCH Superseded Johan Hedberg 4 participants 8 messages May 28, 2013 – May 30, 2013

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

PATCH 1/1 Bluetooth: Fix mgmt handling of power on failures

From: Johan Hedberg <[email protected]>

If hci_dev_open fails we need to ensure that the corresponding mgmt_set_powered command gets an appropriate response. This patch fixes the missing response by adding a new mgmt_set_powered_failed function that's used to indicate a power on failure to mgmt.

---
 include/net/bluetooth/hci_core.h |    1 +
 net/bluetooth/hci_core.c         |    6 +++++-
 net/bluetooth/mgmt.c             |   16 ++++++++++++++++
 3 files changed, 22 insertions(+), 1 deletion(-)
include/net/bluetooth/hci_core.h +1 -0
index 606615c..f77885e 100644
@@ -1118,6 +1118,7 @@ void hci_sock_dev_event(struct hci_dev *hdev, int event);
 int mgmt_control(struct sock *sk, struct msghdr *msg, size_t len);
 int mgmt_index_added(struct hci_dev *hdev);
 int mgmt_index_removed(struct hci_dev *hdev);
+int mgmt_set_powered_failed(struct hci_dev *hdev, int err);
 int mgmt_powered(struct hci_dev *hdev, u8 powered);
 int mgmt_discoverable(struct hci_dev *hdev, u8 discoverable);
 int mgmt_connectable(struct hci_dev *hdev, u8 connectable);
net/bluetooth/hci_core.c +5 -1
index 21ad85d..d7c2dbd 100644
@@ -1598,11 +1598,15 @@ static const struct rfkill_ops hci_rfkill_ops = {
 static void hci_power_on(struct work_struct *work)
 {
 	struct hci_dev *hdev = container_of(work, struct hci_dev, power_on);
+	int err;
 
 	BT_DBG("%s", hdev->name);
 
-	if (hci_dev_open(hdev->id) < 0)
+	err = hci_dev_open(hdev->id);
+	if (err < 0) {
+		mgmt_set_powered_failed(hdev, err);
 		return;
+	}
 
 	if (test_bit(HCI_AUTO_OFF, &hdev->dev_flags))
 		queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
net/bluetooth/mgmt.c +16 -0
index 5cd3aee..00b006b 100644
@@ -3533,6 +3533,22 @@ new_settings:
 	return err;
 }
 
+int mgmt_set_powered_failed(struct hci_dev *hdev, int err)
+{
+	struct pending_cmd *cmd;
+
+	cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev);
+	if (!cmd)
+		return -ENOENT;
+
+	err = cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED,
+			 MGMT_STATUS_FAILED);
+
+	mgmt_pending_remove(cmd);
+
+	return err;
+}
+
 int mgmt_discoverable(struct hci_dev *hdev, u8 discoverable)
 {
 	struct cmd_lookup match = { NULL, hdev };
Signature · 1 line
1.7.10.4
#1 lore raw

Hi Johan,

Quoted 6 lines · Johan Hedberg
net/bluetooth/mgmt.c +6 -0
+int mgmt_set_powered_failed(struct hci_dev *hdev, int err)
+{
+       struct pending_cmd *cmd;
+
+       cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev);
+       if (!cmd)

net/bluetooth/mgmt.c new_settings: · line 3551

Quoted 10 lines
net/bluetooth/mgmt.c +10 -0
+               return -ENOENT;
+
+       err = cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED,
+                        MGMT_STATUS_FAILED);
+
+       mgmt_pending_remove(cmd);
+
+       return err;
+}
+

Was the "err" parameter value supposed to be used on this function?

Best Regards,

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

Hi Lizardo,

Quoted 11 lines · Anderson Lizardo
Quoted 10 lines · Johan Hedberg
net/bluetooth/mgmt.c +10 -0
+int mgmt_set_powered_failed(struct hci_dev *hdev, int err)
+{
+       struct pending_cmd *cmd;
+
+       cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev);
+       if (!cmd)
+               return -ENOENT;
+
+       err = cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED,
+                        MGMT_STATUS_FAILED);
Quoted 8 lines

net/bluetooth/mgmt.c new_settings: · line 3551

Quoted 6 lines
net/bluetooth/mgmt.c +6 -0
+
+       mgmt_pending_remove(cmd);
+
+       return err;
+}
+

Was the "err" parameter value supposed to be used on this function?

As I mentioned in my reply to Marcel it's there so this can be easily extended to have a special mapping to the exact mgmt error returned (and now it seems that this mapping will be implemented before this goes upstream).

Johan

#3 lore raw
In reply to Johan Hedberg

Hi Johan,

Quoted 7 lines · Johan Hedberg

From: Johan Hedberg <[email protected]>

If hci_dev_open fails we need to ensure that the corresponding mgmt_set_powered command gets an appropriate response. This patch fixes the missing response by adding a new mgmt_set_powered_failed function that's used to indicate a power on failure to mgmt.

Quoted 6 lines
---
 include/net/bluetooth/hci_core.h |    1 +
 net/bluetooth/hci_core.c         |    6 +++++-
 net/bluetooth/mgmt.c             |   16 ++++++++++++++++
 3 files changed, 22 insertions(+), 1 deletion(-)

Patch has been applied to bluetooth.git. Thanks.

Gustavo

maintainer
#4 lore raw

Hi Gustavo,

Quoted 5 lines
Quoted 5 lines

If hci_dev_open fails we need to ensure that the corresponding mgmt_set_powered command gets an appropriate response. This patch fixes the missing response by adding a new mgmt_set_powered_failed function that's used to indicate a power on failure to mgmt.

Quoted 8 lines
Quoted 6 lines

---

include/net/bluetooth/hci_core.h |    1 +
net/bluetooth/hci_core.c         |    6 +++++-
net/bluetooth/mgmt.c             |   16 ++++++++++++++++
3 files changed, 22 insertions(+), 1 deletion(-)

Patch has been applied to bluetooth.git. Thanks.

I rather have this fixed properly with a new error for specifically ERFKILL. Having one stable kernel return rejected and another rfkill is a bad idea.

Regards

Marcel

#5 lore raw

PATCH v2 1/1 Bluetooth: Fix mgmt handling of power on failures

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

If hci_dev_open fails we need to ensure that the corresponding mgmt_set_powered command gets an appropriate response. This patch fixes the missing response by adding a new mgmt_set_powered_failed function that's used to indicate a power on failure to mgmt. Since a sutuation with the device being rfkilled may require special handling in user space the patch uses a new dedicated mgmt status code for this.

---
v2: Added dedicated mgmt error for the ERFKILL case

 include/net/bluetooth/hci_core.h |    1 +
 include/net/bluetooth/mgmt.h     |    1 +
 net/bluetooth/hci_core.c         |    6 +++++-
 net/bluetooth/mgmt.c             |   21 +++++++++++++++++++++
 4 files changed, 28 insertions(+), 1 deletion(-)
include/net/bluetooth/hci_core.h +1 -0
index 606615c..f77885e 100644
@@ -1118,6 +1118,7 @@ void hci_sock_dev_event(struct hci_dev *hdev, int event);
 int mgmt_control(struct sock *sk, struct msghdr *msg, size_t len);
 int mgmt_index_added(struct hci_dev *hdev);
 int mgmt_index_removed(struct hci_dev *hdev);
+int mgmt_set_powered_failed(struct hci_dev *hdev, int err);
 int mgmt_powered(struct hci_dev *hdev, u8 powered);
 int mgmt_discoverable(struct hci_dev *hdev, u8 discoverable);
 int mgmt_connectable(struct hci_dev *hdev, u8 connectable);
include/net/bluetooth/mgmt.h +1 -0
index 22980a7..9944c3e 100644
@@ -42,6 +42,7 @@
 #define MGMT_STATUS_NOT_POWERED		0x0f
 #define MGMT_STATUS_CANCELLED		0x10
 #define MGMT_STATUS_INVALID_INDEX	0x11
+#define MGMT_STATUS_RFKILLED		0x12
 
 struct mgmt_hdr {
 	__le16	opcode;
net/bluetooth/hci_core.c +5 -1
index 21ad85d..d7c2dbd 100644
@@ -1598,11 +1598,15 @@ static const struct rfkill_ops hci_rfkill_ops = {
 static void hci_power_on(struct work_struct *work)
 {
 	struct hci_dev *hdev = container_of(work, struct hci_dev, power_on);
+	int err;
 
 	BT_DBG("%s", hdev->name);
 
-	if (hci_dev_open(hdev->id) < 0)
+	err = hci_dev_open(hdev->id);
+	if (err < 0) {
+		mgmt_set_powered_failed(hdev, err);
 		return;
+	}
 
 	if (test_bit(HCI_AUTO_OFF, &hdev->dev_flags))
 		queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
net/bluetooth/mgmt.c +21 -0
index 5cd3aee..fedc539 100644
@@ -3533,6 +3533,27 @@ new_settings:
 	return err;
 }
 
+int mgmt_set_powered_failed(struct hci_dev *hdev, int err)
+{
+	struct pending_cmd *cmd;
+	u8 status;
+
+	cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev);
+	if (!cmd)
+		return -ENOENT;
+
+	if (err == -ERFKILL)
+		status = MGMT_STATUS_RFKILLED;
+	else
+		status = MGMT_STATUS_FAILED;
+
+	err = cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status);
+
+	mgmt_pending_remove(cmd);
+
+	return err;
+}
+
 int mgmt_discoverable(struct hci_dev *hdev, u8 discoverable)
 {
 	struct cmd_lookup match = { NULL, hdev };
Signature · 1 line
1.7.10.4
maintainer Acked-by on v2 1/1
#6 lore raw

Hi Johan,

Quoted 4 lines

If hci_dev_open fails we need to ensure that the corresponding mgmt_set_powered command gets an appropriate response. This patch fixes the missing response by adding a new mgmt_set_powered_failed function that's used to indicate a power on failure to mgmt. Since a situation

might want to fix this typo.

Quoted 81 lines

with the device being rfkilled may require special handling in user space the patch uses a new dedicated mgmt status code for this.

---
v2: Added dedicated mgmt error for the ERFKILL case

include/net/bluetooth/hci_core.h |    1 +
include/net/bluetooth/mgmt.h     |    1 +
net/bluetooth/hci_core.c         |    6 +++++-
net/bluetooth/mgmt.c             |   21 +++++++++++++++++++++
4 files changed, 28 insertions(+), 1 deletion(-)
include/net/bluetooth/hci_core.h +1 -0
index 606615c..f77885e 100644
@@ -1118,6 +1118,7 @@ void hci_sock_dev_event(struct hci_dev *hdev, int event);
 int mgmt_control(struct sock *sk, struct msghdr *msg, size_t len);
 int mgmt_index_added(struct hci_dev *hdev);
 int mgmt_index_removed(struct hci_dev *hdev);
+int mgmt_set_powered_failed(struct hci_dev *hdev, int err);
 int mgmt_powered(struct hci_dev *hdev, u8 powered);
 int mgmt_discoverable(struct hci_dev *hdev, u8 discoverable);
 int mgmt_connectable(struct hci_dev *hdev, u8 connectable);
include/net/bluetooth/mgmt.h +1 -0
index 22980a7..9944c3e 100644
@@ -42,6 +42,7 @@
 #define MGMT_STATUS_NOT_POWERED		0x0f
 #define MGMT_STATUS_CANCELLED		0x10
 #define MGMT_STATUS_INVALID_INDEX	0x11
+#define MGMT_STATUS_RFKILLED		0x12
 
 struct mgmt_hdr {
 	__le16	opcode;
net/bluetooth/hci_core.c +5 -1
index 21ad85d..d7c2dbd 100644
@@ -1598,11 +1598,15 @@ static const struct rfkill_ops hci_rfkill_ops = {
 static void hci_power_on(struct work_struct *work)
 {
 	struct hci_dev *hdev = container_of(work, struct hci_dev, power_on);
+	int err;
 
 	BT_DBG("%s", hdev->name);
 
-	if (hci_dev_open(hdev->id) < 0)
+	err = hci_dev_open(hdev->id);
+	if (err < 0) {
+		mgmt_set_powered_failed(hdev, err);
 		return;
+	}
 
 	if (test_bit(HCI_AUTO_OFF, &hdev->dev_flags))
 		queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
net/bluetooth/mgmt.c +14 -0
index 5cd3aee..fedc539 100644
@@ -3533,6 +3533,27 @@ new_settings:
 	return err;
 }
 
+int mgmt_set_powered_failed(struct hci_dev *hdev, int err)
+{
+	struct pending_cmd *cmd;
+	u8 status;
+
+	cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev);
+	if (!cmd)
+		return -ENOENT;
+
+	if (err == -ERFKILL)
+		status = MGMT_STATUS_RFKILLED;
+	else
+		status = MGMT_STATUS_FAILED;
+

net/bluetooth/mgmt.c new_settings: · line 3559

Quoted 10 lines
net/bluetooth/mgmt.c +7 -0
+	err = cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status);
+
+	mgmt_pending_remove(cmd);
+
+	return err;
+}
+
 int mgmt_discoverable(struct hci_dev *hdev, u8 discoverable)
 {
 	struct cmd_lookup match = { NULL, hdev };

We do not need to do this in this patch, but I think we need to get rid of all the return values for the status notification functions. Since we never use them, it is pointless to keep them around. Or are we doing anything useful with them.

Anyhow, this patch is fine with me.

Regards

Marcel

#7 lore raw
In reply to Johan Hedberg

Hi Johan,

Quoted 11 lines · Johan Hedberg

From: Johan Hedberg <[email protected]>

If hci_dev_open fails we need to ensure that the corresponding mgmt_set_powered command gets an appropriate response. This patch fixes the missing response by adding a new mgmt_set_powered_failed function that's used to indicate a power on failure to mgmt. Since a sutuation with the device being rfkilled may require special handling in user space the patch uses a new dedicated mgmt status code for this.

Quoted 8 lines

---
v2: Added dedicated mgmt error for the ERFKILL case

 include/net/bluetooth/hci_core.h |    1 +
 include/net/bluetooth/mgmt.h     |    1 +
 net/bluetooth/hci_core.c         |    6 +++++-
 net/bluetooth/mgmt.c             |   21 +++++++++++++++++++++
 4 files changed, 28 insertions(+), 1 deletion(-)

Patch has been applied, I fixed the typo too. Thanks.

Gustavo