Bluetooth: Add support for Set External Configuration management command
authorMarcel Holtmann <marcel@holtmann.org>
Fri, 4 Jul 2014 16:11:55 +0000 (18:11 +0200)
committerJohan Hedberg <johan.hedberg@intel.com>
Fri, 4 Jul 2014 18:10:30 +0000 (21:10 +0300)
The Set External Configuration management command allows for switching
between configured and unconfigured start if HCI_QURIK_EXTERNAL_CONFIG
is set by the transport driver.

Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
include/net/bluetooth/hci.h
include/net/bluetooth/mgmt.h
net/bluetooth/mgmt.c

index 4f1c4c7e2f00da23516e41f950276fd9e8b22284..80c5fc947fbc1aa64bc96dab800b7c4ba1ed4cb1 100644 (file)
@@ -173,6 +173,7 @@ enum {
        HCI_UNREGISTER,
        HCI_UNCONFIGURED,
        HCI_USER_CHANNEL,
+       HCI_EXT_CONFIGURED,
 
        HCI_LE_SCAN,
        HCI_SSP_ENABLED,
index 3984678ffab1cd5fa7a21a17401adf22299089fd..c7d537f1bd192e536d0893e01109225db7aa83b4 100644 (file)
@@ -483,6 +483,12 @@ struct mgmt_rp_read_config_info {
        __le32  missing_options;
 } __packed;
 
+#define MGMT_OP_SET_EXTERNAL_CONFIG    0x0038
+struct mgmt_cp_set_external_config {
+       __u8 config;
+} __packed;
+#define MGMT_SET_EXTERNAL_CONFIG_SIZE  1
+
 #define MGMT_EV_CMD_COMPLETE           0x0001
 struct mgmt_ev_cmd_complete {
        __le16  opcode;
index d2d0e051e4f260d46e165ea6c108d1f713b3cf25..91bccef63f30315048d9692735ca7ca72903d417 100644 (file)
@@ -91,6 +91,7 @@ static const u16 mgmt_commands[] = {
        MGMT_OP_LOAD_CONN_PARAM,
        MGMT_OP_READ_UNCONF_INDEX_LIST,
        MGMT_OP_READ_CONFIG_INFO,
+       MGMT_OP_SET_EXTERNAL_CONFIG,
 };
 
 static const u16 mgmt_events[] = {
@@ -441,11 +442,25 @@ static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev,
        return err;
 }
 
+static bool is_configured(struct hci_dev *hdev)
+{
+       if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
+           !test_bit(HCI_EXT_CONFIGURED, &hdev->dev_flags))
+               return false;
+
+       if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) &&
+           !bacmp(&hdev->public_addr, BDADDR_ANY))
+               return false;
+
+       return true;
+}
+
 static __le32 get_missing_options(struct hci_dev *hdev)
 {
        u32 options = 0;
 
-       if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
+       if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
+           !test_bit(HCI_EXT_CONFIGURED, &hdev->dev_flags))
                options |= MGMT_OPTION_EXTERNAL_CONFIG;
 
        if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) &&
@@ -455,6 +470,14 @@ static __le32 get_missing_options(struct hci_dev *hdev)
        return cpu_to_le32(options);
 }
 
+static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
+{
+       __le32 options = get_missing_options(hdev);
+
+       return cmd_complete(sk, hdev->id, opcode, 0, &options,
+                           sizeof(options));
+}
+
 static int read_config_info(struct sock *sk, struct hci_dev *hdev,
                            void *data, u16 data_len)
 {
@@ -5355,6 +5378,54 @@ static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data,
        return cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, NULL, 0);
 }
 
+static int set_external_config(struct sock *sk, struct hci_dev *hdev,
+                              void *data, u16 len)
+{
+       struct mgmt_cp_set_external_config *cp = data;
+       bool changed;
+       int err;
+
+       BT_DBG("%s", hdev->name);
+
+       if (hdev_is_powered(hdev))
+               return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
+                                 MGMT_STATUS_REJECTED);
+
+       if (cp->config != 0x00 && cp->config != 0x01)
+               return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
+                                   MGMT_STATUS_INVALID_PARAMS);
+
+       if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
+               return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
+                                 MGMT_STATUS_NOT_SUPPORTED);
+
+       hci_dev_lock(hdev);
+
+       if (cp->config)
+               changed = !test_and_set_bit(HCI_EXT_CONFIGURED,
+                                           &hdev->dev_flags);
+       else
+               changed = test_and_clear_bit(HCI_EXT_CONFIGURED,
+                                            &hdev->dev_flags);
+
+       err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev);
+       if (err < 0)
+               goto unlock;
+
+       if (!changed)
+               goto unlock;
+
+       if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) == is_configured(hdev)) {
+               mgmt_index_removed(hdev);
+               change_bit(HCI_UNCONFIGURED, &hdev->dev_flags);
+               mgmt_index_added(hdev);
+       }
+
+unlock:
+       hci_dev_unlock(hdev);
+       return err;
+}
+
 static const struct mgmt_handler {
        int (*func) (struct sock *sk, struct hci_dev *hdev, void *data,
                     u16 data_len);
@@ -5417,6 +5488,7 @@ static const struct mgmt_handler {
        { load_conn_param,        true,  MGMT_LOAD_CONN_PARAM_SIZE },
        { read_unconf_index_list, false, MGMT_READ_UNCONF_INDEX_LIST_SIZE },
        { read_config_info,       false, MGMT_READ_CONFIG_INFO_SIZE },
+       { set_external_config,    false, MGMT_SET_EXTERNAL_CONFIG_SIZE },
 };
 
 int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen)
@@ -5469,7 +5541,8 @@ int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen)
                }
 
                if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) &&
-                   opcode != MGMT_OP_READ_CONFIG_INFO) {
+                   opcode != MGMT_OP_READ_CONFIG_INFO &&
+                   opcode != MGMT_OP_SET_EXTERNAL_CONFIG) {
                        err = cmd_status(sk, index, opcode,
                                         MGMT_STATUS_INVALID_INDEX);
                        goto done;