Bluetooth: Make hci_le_scan use HCI request

This patch changes hci_le_scan helper so it uses the HCI request
framework to enable LE scanning. The LE scanning disable timeout
was removed from the helper and it is now handled by mgmt start_
discovery code.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Merge LE-only and interleaved cases

This patch merges the LE-only and interleaved discovery type cases.
Since hci_le_scan helper uses the hci request framework, those cases
are pretty much the same now.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Remove LE scan work

This patch removes the LE scan work and all code related to it. We
now use the HCI request framework to enable LE scanning. The LE scan
work is no longer needed.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Remove timeout handling from hci_cancel_le_scan

Since LE scanning timeout is not handled by LE scanning helpers
anymore, we should handle it in stop_discovery instead of
hci_cancel_le_scan.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Change LE scanning timeout macros

Define LE scanning timeout macros in jiffies just like others timeout
macros do.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Add LE scan type macros

This patch adds macros for active and passive LE scan type values. It
also removes the LE_SCAN_TYPE macro since it is not used anymore.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Handle start discovery failure in le_scan_complete

This patch moves start discovery failure handling to le_scan_
complete callback. It also removes the LE Set Scan Parameter
HCI event handler since it becomes empty.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Update hci_cancel_le_scan to use HCI request

This patch changes hci_cancel_le_scan so it uses HCI request
framework. The main reason for this change is we'll need this
helper supporting HCI request to properly implement the LE
connection state machine.

As a side effect, we can also register a complete callback and handle
stop discovery failures in mgmt layer as we already do with start
discovery failures.
Bluetooth: Make hci_le_create_connection return error

This patch changes hci_le_create_connection helper so it return error
if hci_send_cmd fails. It also does the proper error handling in
hci_connect_le.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Initialize hci_conn fields in hci_connect_le

This patch moves some hci_conn fields initialization from hci_le_
create_connection. It makes more sense to initialize these fields
within the function that creates the hci_conn object.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Refactor LE Connection Complete HCI event handler

This patch does some code refactorig in LE Connection Complete HCI
event handler. It basically adds a switch case to separate new master
connection code from new slave connection code.

Besides improving code readability, it will also make multiple LE
connection support a bit cleaner.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Locking refactor in hci_le_conn_complete_evt

This patch moves hci_dev_lock and hci_dev_unlock call to where they
are really required, reducing the critical region in hci_le_conn_
complete_evt function. hdev->lock is required only in hci_conn_del
and hci_conn_add call to protect concurrent add and remove operations
in hci_conn_hash list.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: LE Create Connection support in request framework

No Command Complete event is sent by the controller to indicate that
HCI LE Create Connection command has been completed. Instead, the LE
Connection Complete events indicates this command has been completed.
For that reason, HCI request framework requires a special handling
for this command.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Add LE create connection helpers

This patch adds two static helpers with HCI request support. They
will be used by the LE connection state machine.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: LE connection state machine

This patch implements a state machine for carrying out the general
connection establishment procedure described in Core spec.

The state machine should be used as follows: when the kernel
receives a new LE connection attempt, it should go to HCI_CONN_LE_
SCAN state, starting the passive LE scanning. Once the target remote
device is in-range, it should go to HCI_CONN_LE_INITIATE state,
stopping the ongoing LE scanning and create the LE connection.

This state machine will be used by the LE connection routine in
order to establish LE connections.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Remove unneeded command status handler

This patch removes the hci_cs_le_create_conn handler from hci_event.c.
Since LE connection failures are now handled in le_state_complete
callback, there is no need to have the hci_cs_le_create_conn handler.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
WIP: Bluetooth: Handle hci_conn timeout in BT_CONNECT

In order to properly cancel an ongoing LE connection attempts (hci_conn
in BT_CONNECT state), we should set HCI_CONN_LE_IDLE state. We also
need to do the proper hci_conn deletion when the scanning is disabled
as we already to in case the connection attempt fails in le_conn_complete.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>

WIP: Bluetooth: Add LE Create Connection Cancel handler

This patch adds a handler for HCI LE Create Connection Cancel command
complete event. This way, we can do all the properly handling of this
command in this function instead of fixing this logic within LE conn
complete handler.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Remove unneeded handling

This patch removes codes to handle LE Connection Complete events with
error status. All cases of failure are already handled in le_state_
complete callback or in LE Create Connection Cancel Command Complete
event handler.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Change LE connection routine

In order to better support LE connection requirements, such as multiple
connections and re-connections, we should use the general connection
establishment procedure described in Core spec. Today, we use the direct
connection establishment procedure which has some limitations and,
therefore, requires extra connection management at user-space in order
to support LE connection requirements.

According to the spec, the general procedure is described as follows:
The host starts scanning for LE devices, once the device we want to
connect to is in-range, the host stops scanning and initiates a
connection. The procedure is terminated when the connection is
established or when the host terminates the procedure.

This patch changes the LE connection routine so we perform the general
procedure instead of the direct procedure.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>
Bluetooth: Count pending LE connection attempts

To avoid traversing the hci_conn list every time we get an advertising
report, we keep a counter of pending LE connection attempts (connections
in HCI_CONN_LE_SCAN state). This way, we only traverse the list if the
counter is greater from zero.

Signed-off-by: Andre Guedes <andre.guedes@openbossa.org>