Bluetooth: Perform HCI update for power on synchronously
[deliverable/linux.git] / net / bluetooth / hci_request.c
index fe14fd121d367e55f0d4485c489123399efe46ae..7cc24f1448bd3d902aef81fa1f8d9352710ac325 100644 (file)
@@ -21,6 +21,8 @@
    SOFTWARE IS DISCLAIMED.
 */
 
+#include <asm/unaligned.h>
+
 #include <net/bluetooth/bluetooth.h>
 #include <net/bluetooth/hci_core.h>
 #include <net/bluetooth/mgmt.h>
@@ -347,6 +349,41 @@ void hci_req_add(struct hci_request *req, u16 opcode, u32 plen,
        hci_req_add_ev(req, opcode, plen, param, 0);
 }
 
+void __hci_req_write_fast_connectable(struct hci_request *req, bool enable)
+{
+       struct hci_dev *hdev = req->hdev;
+       struct hci_cp_write_page_scan_activity acp;
+       u8 type;
+
+       if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
+               return;
+
+       if (hdev->hci_ver < BLUETOOTH_VER_1_2)
+               return;
+
+       if (enable) {
+               type = PAGE_SCAN_TYPE_INTERLACED;
+
+               /* 160 msec page scan interval */
+               acp.interval = cpu_to_le16(0x0100);
+       } else {
+               type = PAGE_SCAN_TYPE_STANDARD; /* default */
+
+               /* default 1.28 sec page scan */
+               acp.interval = cpu_to_le16(0x0800);
+       }
+
+       acp.window = cpu_to_le16(0x0012);
+
+       if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval ||
+           __cpu_to_le16(hdev->page_scan_window) != acp.window)
+               hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
+                           sizeof(acp), &acp);
+
+       if (hdev->page_scan_type != type)
+               hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type);
+}
+
 /* This function controls the background scanning based on hdev->pend_le_conns
  * list. If there are pending LE connection we start the background scanning,
  * otherwise we stop it.
@@ -420,6 +457,203 @@ static void __hci_update_background_scan(struct hci_request *req)
        }
 }
 
+void __hci_req_update_name(struct hci_request *req)
+{
+       struct hci_dev *hdev = req->hdev;
+       struct hci_cp_write_local_name cp;
+
+       memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
+
+       hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp);
+}
+
+#define PNP_INFO_SVCLASS_ID            0x1200
+
+static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
+{
+       u8 *ptr = data, *uuids_start = NULL;
+       struct bt_uuid *uuid;
+
+       if (len < 4)
+               return ptr;
+
+       list_for_each_entry(uuid, &hdev->uuids, list) {
+               u16 uuid16;
+
+               if (uuid->size != 16)
+                       continue;
+
+               uuid16 = get_unaligned_le16(&uuid->uuid[12]);
+               if (uuid16 < 0x1100)
+                       continue;
+
+               if (uuid16 == PNP_INFO_SVCLASS_ID)
+                       continue;
+
+               if (!uuids_start) {
+                       uuids_start = ptr;
+                       uuids_start[0] = 1;
+                       uuids_start[1] = EIR_UUID16_ALL;
+                       ptr += 2;
+               }
+
+               /* Stop if not enough space to put next UUID */
+               if ((ptr - data) + sizeof(u16) > len) {
+                       uuids_start[1] = EIR_UUID16_SOME;
+                       break;
+               }
+
+               *ptr++ = (uuid16 & 0x00ff);
+               *ptr++ = (uuid16 & 0xff00) >> 8;
+               uuids_start[0] += sizeof(uuid16);
+       }
+
+       return ptr;
+}
+
+static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
+{
+       u8 *ptr = data, *uuids_start = NULL;
+       struct bt_uuid *uuid;
+
+       if (len < 6)
+               return ptr;
+
+       list_for_each_entry(uuid, &hdev->uuids, list) {
+               if (uuid->size != 32)
+                       continue;
+
+               if (!uuids_start) {
+                       uuids_start = ptr;
+                       uuids_start[0] = 1;
+                       uuids_start[1] = EIR_UUID32_ALL;
+                       ptr += 2;
+               }
+
+               /* Stop if not enough space to put next UUID */
+               if ((ptr - data) + sizeof(u32) > len) {
+                       uuids_start[1] = EIR_UUID32_SOME;
+                       break;
+               }
+
+               memcpy(ptr, &uuid->uuid[12], sizeof(u32));
+               ptr += sizeof(u32);
+               uuids_start[0] += sizeof(u32);
+       }
+
+       return ptr;
+}
+
+static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
+{
+       u8 *ptr = data, *uuids_start = NULL;
+       struct bt_uuid *uuid;
+
+       if (len < 18)
+               return ptr;
+
+       list_for_each_entry(uuid, &hdev->uuids, list) {
+               if (uuid->size != 128)
+                       continue;
+
+               if (!uuids_start) {
+                       uuids_start = ptr;
+                       uuids_start[0] = 1;
+                       uuids_start[1] = EIR_UUID128_ALL;
+                       ptr += 2;
+               }
+
+               /* Stop if not enough space to put next UUID */
+               if ((ptr - data) + 16 > len) {
+                       uuids_start[1] = EIR_UUID128_SOME;
+                       break;
+               }
+
+               memcpy(ptr, uuid->uuid, 16);
+               ptr += 16;
+               uuids_start[0] += 16;
+       }
+
+       return ptr;
+}
+
+static void create_eir(struct hci_dev *hdev, u8 *data)
+{
+       u8 *ptr = data;
+       size_t name_len;
+
+       name_len = strlen(hdev->dev_name);
+
+       if (name_len > 0) {
+               /* EIR Data type */
+               if (name_len > 48) {
+                       name_len = 48;
+                       ptr[1] = EIR_NAME_SHORT;
+               } else
+                       ptr[1] = EIR_NAME_COMPLETE;
+
+               /* EIR Data length */
+               ptr[0] = name_len + 1;
+
+               memcpy(ptr + 2, hdev->dev_name, name_len);
+
+               ptr += (name_len + 2);
+       }
+
+       if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) {
+               ptr[0] = 2;
+               ptr[1] = EIR_TX_POWER;
+               ptr[2] = (u8) hdev->inq_tx_power;
+
+               ptr += 3;
+       }
+
+       if (hdev->devid_source > 0) {
+               ptr[0] = 9;
+               ptr[1] = EIR_DEVICE_ID;
+
+               put_unaligned_le16(hdev->devid_source, ptr + 2);
+               put_unaligned_le16(hdev->devid_vendor, ptr + 4);
+               put_unaligned_le16(hdev->devid_product, ptr + 6);
+               put_unaligned_le16(hdev->devid_version, ptr + 8);
+
+               ptr += 10;
+       }
+
+       ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
+       ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
+       ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
+}
+
+void __hci_req_update_eir(struct hci_request *req)
+{
+       struct hci_dev *hdev = req->hdev;
+       struct hci_cp_write_eir cp;
+
+       if (!hdev_is_powered(hdev))
+               return;
+
+       if (!lmp_ext_inq_capable(hdev))
+               return;
+
+       if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
+               return;
+
+       if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
+               return;
+
+       memset(&cp, 0, sizeof(cp));
+
+       create_eir(hdev, cp.data);
+
+       if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
+               return;
+
+       memcpy(hdev->eir, cp.data, sizeof(cp.data));
+
+       hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
+}
+
 void hci_req_add_le_scan_disable(struct hci_request *req)
 {
        struct hci_cp_le_set_scan_enable cp;
@@ -1947,6 +2181,106 @@ static void discov_off(struct work_struct *work)
        mgmt_new_settings(hdev);
 }
 
+static int powered_update_hci(struct hci_request *req, unsigned long opt)
+{
+       struct hci_dev *hdev = req->hdev;
+       struct adv_info *adv_instance;
+       u8 link_sec;
+
+       hci_dev_lock(hdev);
+
+       if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED) &&
+           !lmp_host_ssp_capable(hdev)) {
+               u8 mode = 0x01;
+
+               hci_req_add(req, HCI_OP_WRITE_SSP_MODE, sizeof(mode), &mode);
+
+               if (bredr_sc_enabled(hdev) && !lmp_host_sc_capable(hdev)) {
+                       u8 support = 0x01;
+
+                       hci_req_add(req, HCI_OP_WRITE_SC_SUPPORT,
+                                   sizeof(support), &support);
+               }
+       }
+
+       if (hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
+           lmp_bredr_capable(hdev)) {
+               struct hci_cp_write_le_host_supported cp;
+
+               cp.le = 0x01;
+               cp.simul = 0x00;
+
+               /* Check first if we already have the right
+                * host state (host features set)
+                */
+               if (cp.le != lmp_host_le_capable(hdev) ||
+                   cp.simul != lmp_host_le_br_capable(hdev))
+                       hci_req_add(req, HCI_OP_WRITE_LE_HOST_SUPPORTED,
+                                   sizeof(cp), &cp);
+       }
+
+       if (lmp_le_capable(hdev)) {
+               /* Make sure the controller has a good default for
+                * advertising data. This also applies to the case
+                * where BR/EDR was toggled during the AUTO_OFF phase.
+                */
+               if (hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
+                   (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
+                    !hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))) {
+                       __hci_req_update_adv_data(req, HCI_ADV_CURRENT);
+                       __hci_req_update_scan_rsp_data(req, HCI_ADV_CURRENT);
+               }
+
+               if (hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE) &&
+                   hdev->cur_adv_instance == 0x00 &&
+                   !list_empty(&hdev->adv_instances)) {
+                       adv_instance = list_first_entry(&hdev->adv_instances,
+                                                       struct adv_info, list);
+                       hdev->cur_adv_instance = adv_instance->instance;
+               }
+
+               if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
+                       __hci_req_enable_advertising(req);
+               else if (hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE) &&
+                        hdev->cur_adv_instance)
+                       __hci_req_schedule_adv_instance(req,
+                                                       hdev->cur_adv_instance,
+                                                       true);
+       }
+
+       link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
+       if (link_sec != test_bit(HCI_AUTH, &hdev->flags))
+               hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE,
+                           sizeof(link_sec), &link_sec);
+
+       if (lmp_bredr_capable(hdev)) {
+               if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
+                       __hci_req_write_fast_connectable(req, true);
+               else
+                       __hci_req_write_fast_connectable(req, false);
+               __hci_req_update_scan(req);
+               __hci_req_update_class(req);
+               __hci_req_update_name(req);
+               __hci_req_update_eir(req);
+       }
+
+       hci_dev_unlock(hdev);
+       return 0;
+}
+
+int __hci_req_hci_power_on(struct hci_dev *hdev)
+{
+       /* Register the available SMP channels (BR/EDR and LE) only when
+        * successfully powering on the controller. This late
+        * registration is required so that LE SMP can clearly decide if
+        * the public address or static address is used.
+        */
+       smp_register(hdev);
+
+       return __hci_req_sync(hdev, powered_update_hci, 0, HCI_CMD_TIMEOUT,
+                             NULL);
+}
+
 void hci_request_setup(struct hci_dev *hdev)
 {
        INIT_WORK(&hdev->discov_update, discov_update);
This page took 0.088041 seconds and 5 git commands to generate.