Bluetooth: Move LE scan changes behind req_workqueue
Subsystems (2)
v2 cover Bluetooth: Move LE scan changes behind req_workqueue
Hi,
v2: updated version with feedback for 01/12 and 02/12 taken into account.
Quoted 1 line
From the original cover letter:
The current bluetooth-next tree is not able to reliably and in a race-free manner handle updates to the LE scan state. This results in the kernel ending up out of sync with the actual LE scan state and starts sending incorrect scan state HCI commands, often resulting in "not allowed" HCI responses.
This is an initial set of patches to try solve the issue. I've tried to retain as much as possible of the original logic so that it's easier to see that nothing should get broken. Because of this, there are still many simplifications and cleanups that can be done. However, since I've already got 12 patches lined up I wanted to get them sent, reviewed and merged. After that I can concentrate on the cleanups and I'll also be looking into moving the adverising state changes to hci_request.c as well.
The general solution that this set takes to solve the synchronization issues with the LE scan state, is to move all of it behind the hdev->req_workqueue and perform the actions synchronously. The main tool I've used for this is the hci_req_sync() API which allows taking existing asynchronous HCI request helpers and using them to perform the same commands synchronously (by passing the helper as the callback to hci_req_sync).
Since only one work callback is active at a time we get an easy way to avoid races between the changes. All of the new synchronous code can be found in hci_request.c which seemed like an appropriate place for it.
I've done a fair amount of testing with the patches, including mgmt-tester, l2cap-tester and various bluetoothd tests (both with controllers supporting simultaneous Inquiry+LE scan as well as those not supporting it). However, because of the size of the changes it's perfectly possible that I've missed or broken something, so it'd be good if others could give the set a spin before merging it.
Johan
----------------------------------------------------------------
Johan Hedberg (12):
Bluetooth: Add stubs for synchronous HCI request functionality
Bluetooth: Run all background scan updates through req_workqueue
Bluetooth: Don't wait for HCI in Add/Remove Device
Bluetooth: Add HCI status return parameter to hci_req_sync()
Bluetooth: Use req_workqueue for explicit connect requests
Bluetooth: Use req_workqueue for background scanning when powering on
Bluetooth: Make __hci_update_background_scan private to hci_request.c
Bluetooth: Move LE scan disable/restart behind req_workqueue
Bluetooth: Add discovery type validity helper
Bluetooth: Add error return value to hci_req_sync callback
Bluetooth: Move Start Discovery to req_workqueue
Bluetooth: Move Stop Discovery to req_workqueue
include/net/bluetooth/hci.h | 3 +- include/net/bluetooth/hci_core.h | 10 +- net/bluetooth/hci_conn.c | 39 +-- net/bluetooth/hci_core.c | 243 ++++------------- net/bluetooth/hci_request.c | 537 +++++++++++++++++++++++++++++++++++--- net/bluetooth/hci_request.h | 26 +- net/bluetooth/mgmt.c | 503 +++++++---------------------------- 7 files changed, 684 insertions(+), 677 deletions(-)
PATCH v2 1/12 Bluetooth: Add stubs for synchronous HCI request functionality
From: Johan Hedberg <[email protected]>
Prepare hci_request.c to have code for doing synchronous HCI requests, such as LE scanning or advertising changes. The necessary work callbacks will be set up in hci_request_setup() and cleaned up in hci_request_cancel_all(). The former is used when an HCI device get registered, and the latter each time it is powered off (or unregistered).
--- net/bluetooth/hci_core.c | 4 ++++ net/bluetooth/hci_request.c | 8 ++++++++ net/bluetooth/hci_request.h | 3 +++ 3 files changed, 15 insertions(+)
net/bluetooth/hci_core.c +4 -0
@@ -1604,6 +1604,8 @@ int hci_dev_do_close(struct hci_dev *hdev)hci_req_sync_unlock (hdev); +hci_request_cancel_all (hdev); +hci_dev_put (hdev);return 0 ; }
@@ -3156,6 +3158,8 @@ struct hci_dev *hci_alloc_dev(void)INIT_DELAYED_WORK (&hdev->cmd_timer, hci_cmd_timeout); +hci_request_setup (hdev); +hci_init_sysfs (hdev);discovery_init (hdev);
net/bluetooth/hci_request.c +8 -0
@@ -845,3 +845,11 @@ int hci_abort_conn(struct hci_conn *conn, u8 reason)return 0 ; } + +void hci_request_setup (struct hci_dev *hdev) +{ +} + +void hci_request_cancel_all (struct hci_dev *hdev) +{ +}
net/bluetooth/hci_request.h +3 -0
@@ -70,3 +70,6 @@ void __hci_update_background_scan(struct hci_request *req);int hci_abort_conn (struct hci_conn *conn,u8 reason);void __hci_abort_conn (struct hci_request *req,struct hci_conn *conn,u8 reason); + +void hci_request_setup (struct hci_dev *hdev); +void hci_request_cancel_all (struct hci_dev *hdev);
Signature · 1 line
2.5.0
PATCH v2 2/12 Bluetooth: Run all background scan updates through req_workqueue
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
Instead of firing off a simple async request queue all background scan updates through req_workqueue and use hci_req_sync() there to ensure that no two updates overlap with each other.
--- include/net/bluetooth/hci_core.h | 2 ++ net/bluetooth/hci_request.c | 39 +++++++++++++++++---------------------- net/bluetooth/hci_request.h | 6 +++++- net/bluetooth/mgmt.c | 2 +- 4 files changed, 25 insertions(+), 24 deletions(-)
include/net/bluetooth/hci_core.h +2 -0
@@ -327,6 +327,8 @@ struct hci_dev {struct work_struct cmd_work;struct work_struct tx_work; +struct work_struct bg_scan_update; +struct sk_buff_head rx_q;struct sk_buff_head raw_q;struct sk_buff_head cmd_q;
net/bluetooth/hci_request.c +17 -22
@@ -731,28 +731,6 @@ void __hci_update_background_scan(struct hci_request *req) } } -static void update_background_scan_complete (struct hci_dev *hdev,u8 status, -u16 opcode) -{ -if (status) -BT_DBG ("HCI request failed to update background scanning: " -"status 0x%2.2x" , status); -} - -void hci_update_background_scan (struct hci_dev *hdev) -{ -int err; -struct hci_request req; - -hci_req_init (&req, hdev); - -__hci_update_background_scan (&req); - - err =hci_req_run (&req, update_background_scan_complete); -if (err && err != -ENODATA ) -BT_ERR ("Failed to run HCI request: err %d" , err); -} -void __hci_abort_conn (struct hci_request *req,struct hci_conn *conn,u8 reason) {
@@ -846,10 +824,27 @@ int hci_abort_conn(struct hci_conn *conn, u8 reason)return 0 ; } +static void update_bg_scan (struct hci_request *req,unsigned long opt) +{ +hci_dev_lock (req->hdev); +__hci_update_background_scan (req); +hci_dev_unlock (req->hdev); +} + +static void bg_scan_update (struct work_struct *work) +{ + struct hci_dev *hdev = container_of(work, struct hci_dev, + bg_scan_update); + +hci_req_sync (hdev, update_bg_scan,0 ,HCI_CMD_TIMEOUT ); +} +void hci_request_setup (struct hci_dev *hdev) { +INIT_WORK (&hdev->bg_scan_update, bg_scan_update); }void hci_request_cancel_all (struct hci_dev *hdev) { +cancel_work_sync (&hdev->bg_scan_update); }
net/bluetooth/hci_request.h +5 -1
@@ -64,12 +64,16 @@ void __hci_update_page_scan(struct hci_request *req);int hci_update_random_address (struct hci_request *req,bool require_privacy,u8 *own_addr_type); -void hci_update_background_scan (struct hci_dev *hdev);void __hci_update_background_scan (struct hci_request *req);int hci_abort_conn (struct hci_conn *conn,u8 reason);void __hci_abort_conn (struct hci_request *req,struct hci_conn *conn,u8 reason); +static inline void hci_update_background_scan (struct hci_dev *hdev) +{ +queue_work (hdev->req_workqueue, &hdev->bg_scan_update); +} +void hci_request_setup (struct hci_dev *hdev);void hci_request_cancel_all (struct hci_dev *hdev);
net/bluetooth/mgmt.c +1 -1
@@ -2510,8 +2510,8 @@ static void le_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)hci_req_init (&req, hdev);update_adv_data (&req);update_scan_rsp_data (&req); -__hci_update_background_scan (&req);hci_req_run (&req,NULL ); +hci_update_background_scan (hdev); } unlock:
Signature · 1 line
2.5.0
PATCH v2 3/12 Bluetooth: Don't wait for HCI in Add/Remove Device
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
There's no point in waiting for HCI activity in Add/Remove Device since the effects of these calls are long-lasting and we can anyway not report up to the application all HCI failures.
--- net/bluetooth/mgmt.c | 161 ++++++++++++++++----------------------------------- 1 file changed, 50 insertions(+), 111 deletions(-)
net/bluetooth/mgmt.c +50 -111
@@ -6076,10 +6076,9 @@ static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type) }/* This function requires the caller holds hdev->lock */ -static int hci_conn_params_set (struct hci_request *req,bdaddr_t *addr, +static int hci_conn_params_set (struct hci_dev *hdev,bdaddr_t *addr,u8 addr_type,u8 auto_connect) { -struct hci_dev *hdev = req->hdev;struct hci_conn_params *params; params =hci_conn_params_add (hdev, addr, addr_type);
@@ -6099,26 +6098,17 @@ static int hci_conn_params_set(struct hci_request *req, bdaddr_t *addr,*/ if (params->explicit_connect)list_add (¶ms->action, &hdev->pend_le_conns); - -__hci_update_background_scan (req);break ;case HCI_AUTO_CONN_REPORT :if (params->explicit_connect)list_add (¶ms->action, &hdev->pend_le_conns);else list_add (¶ms->action, &hdev->pend_le_reports); -__hci_update_background_scan (req);break ;case HCI_AUTO_CONN_DIRECT :case HCI_AUTO_CONN_ALWAYS : -if (!is_connected (hdev, addr, addr_type)) { +if (!is_connected (hdev, addr, addr_type))list_add (¶ms->action, &hdev->pend_le_conns); -/* If we are in scan phase of connecting, we were -* already added to pend_le_conns and scanning. -*/ -if (params->auto_connect !=HCI_AUTO_CONN_EXPLICIT ) -__hci_update_background_scan (req); - }break ; }
@@ -6142,25 +6132,6 @@ static void device_added(struct sock *sk, struct hci_dev *hdev,mgmt_event (MGMT_EV_DEVICE_ADDED , hdev, &ev,sizeof (ev), sk); } -static void add_device_complete (struct hci_dev *hdev,u8 status,u16 opcode) -{ -struct mgmt_pending_cmd *cmd; - -BT_DBG ("status 0x%02x" , status); - -hci_dev_lock (hdev); - - cmd =pending_find (MGMT_OP_ADD_DEVICE , hdev); -if (!cmd) - goto unlock; - - cmd->cmd_complete (cmd,mgmt_status (status)); -mgmt_pending_remove (cmd); - -unlock: -hci_dev_unlock (hdev); -} -static int add_device (struct sock *sk,struct hci_dev *hdev,void *data,u16 len) {
@@ -6198,9 +6169,10 @@ static int add_device(struct sock *sk, struct hci_dev *hdev,if (cp->addr.type ==BDADDR_BREDR ) {/* Only incoming connections action is supported for now */ if (cp->action !=0x01 ) { - err = cmd->cmd_complete (cmd, -MGMT_STATUS_INVALID_PARAMS ); -mgmt_pending_remove (cmd); + err =mgmt_cmd_complete (sk, hdev->id, +MGMT_OP_ADD_DEVICE , +MGMT_STATUS_INVALID_PARAMS , + &cp->addr,sizeof (cp->addr)); goto unlock; }
@@ -6229,33 +6201,31 @@ static int add_device(struct sock *sk, struct hci_dev *hdev,* hci_conn_params_lookup. */ if (!hci_is_identity_address (&cp->addr.bdaddr, addr_type)) { - err = cmd->cmd_complete (cmd,MGMT_STATUS_INVALID_PARAMS ); -mgmt_pending_remove (cmd); + err =mgmt_cmd_complete (sk, hdev->id,MGMT_OP_ADD_DEVICE , +MGMT_STATUS_INVALID_PARAMS , + &cp->addr,sizeof (cp->addr)); goto unlock; }/* If the connection parameters don't exist for this device, * they will be created and configured with defaults. */ -if (hci_conn_params_set (&req, &cp->addr.bdaddr, addr_type, +if (hci_conn_params_set (hdev, &cp->addr.bdaddr, addr_type, auto_conn) <0 ) { - err = cmd->cmd_complete (cmd,MGMT_STATUS_FAILED ); -mgmt_pending_remove (cmd); + err =mgmt_cmd_complete (sk, hdev->id,MGMT_OP_ADD_DEVICE , +MGMT_STATUS_FAILED , &cp->addr, +sizeof (cp->addr)); goto unlock; } +hci_update_background_scan (hdev); + added:device_added (sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action); - err =hci_req_run (&req, add_device_complete); -if (err <0 ) { -/* ENODATA means no HCI commands were needed (e.g. if -* the adapter is powered off). -*/ -if (err == -ENODATA ) - err = cmd->cmd_complete (cmd,MGMT_STATUS_SUCCESS ); -mgmt_pending_remove (cmd); - } + err =mgmt_cmd_complete (sk, hdev->id,MGMT_OP_ADD_DEVICE , +MGMT_STATUS_SUCCESS , &cp->addr, +sizeof (cp->addr)); unlock:hci_dev_unlock (hdev);
@@ -6273,55 +6243,25 @@ static void device_removed(struct sock *sk, struct hci_dev *hdev,mgmt_event (MGMT_EV_DEVICE_REMOVED , hdev, &ev,sizeof (ev), sk); } -static void remove_device_complete (struct hci_dev *hdev,u8 status,u16 opcode) -{ -struct mgmt_pending_cmd *cmd; - -BT_DBG ("status 0x%02x" , status); - -hci_dev_lock (hdev); - - cmd =pending_find (MGMT_OP_REMOVE_DEVICE , hdev); -if (!cmd) - goto unlock; - - cmd->cmd_complete (cmd,mgmt_status (status)); -mgmt_pending_remove (cmd); - -unlock: -hci_dev_unlock (hdev); -} -static int remove_device (struct sock *sk,struct hci_dev *hdev,void *data,u16 len) {struct mgmt_cp_remove_device *cp = data; -struct mgmt_pending_cmd *cmd; -struct hci_request req;int err;BT_DBG ("%s" , hdev->name); -hci_req_init (&req, hdev); -hci_dev_lock (hdev); - cmd =mgmt_pending_add (sk,MGMT_OP_REMOVE_DEVICE , hdev, data, len); -if (!cmd) { - err = -ENOMEM ; - goto unlock; - } - - cmd->cmd_complete = addr_cmd_complete; -if (bacmp (&cp->addr.bdaddr,BDADDR_ANY )) {struct hci_conn_params *params;u8 addr_type;if (!bdaddr_type_is_valid (cp->addr.type)) { - err = cmd->cmd_complete (cmd, -MGMT_STATUS_INVALID_PARAMS ); -mgmt_pending_remove (cmd); + err =mgmt_cmd_complete (sk, hdev->id, +MGMT_OP_REMOVE_DEVICE , +MGMT_STATUS_INVALID_PARAMS , + &cp->addr,sizeof (cp->addr)); goto unlock; }
@@ -6330,13 +6270,15 @@ static int remove_device(struct sock *sk, struct hci_dev *hdev, &cp->addr.bdaddr, cp->addr.type);if (err) { - err = cmd->cmd_complete (cmd, -MGMT_STATUS_INVALID_PARAMS ); -mgmt_pending_remove (cmd); + err =mgmt_cmd_complete (sk, hdev->id, +MGMT_OP_REMOVE_DEVICE , +MGMT_STATUS_INVALID_PARAMS , + &cp->addr, +sizeof (cp->addr)); goto unlock; } -__hci_update_page_scan (&req); +hci_update_page_scan (hdev);device_removed (sk, hdev, &cp->addr.bdaddr, cp->addr.type);
@@ -6351,33 +6293,36 @@ static int remove_device(struct sock *sk, struct hci_dev *hdev,* hci_conn_params_lookup. */ if (!hci_is_identity_address (&cp->addr.bdaddr, addr_type)) { - err = cmd->cmd_complete (cmd, -MGMT_STATUS_INVALID_PARAMS ); -mgmt_pending_remove (cmd); + err =mgmt_cmd_complete (sk, hdev->id, +MGMT_OP_REMOVE_DEVICE , +MGMT_STATUS_INVALID_PARAMS , + &cp->addr,sizeof (cp->addr)); goto unlock; } params =hci_conn_params_lookup (hdev, &cp->addr.bdaddr, addr_type);if (!params) { - err = cmd->cmd_complete (cmd, -MGMT_STATUS_INVALID_PARAMS ); -mgmt_pending_remove (cmd); + err =mgmt_cmd_complete (sk, hdev->id, +MGMT_OP_REMOVE_DEVICE , +MGMT_STATUS_INVALID_PARAMS , + &cp->addr,sizeof (cp->addr)); goto unlock; }if (params->auto_connect ==HCI_AUTO_CONN_DISABLED || params->auto_connect ==HCI_AUTO_CONN_EXPLICIT ) { - err = cmd->cmd_complete (cmd, -MGMT_STATUS_INVALID_PARAMS ); -mgmt_pending_remove (cmd); + err =mgmt_cmd_complete (sk, hdev->id, +MGMT_OP_REMOVE_DEVICE , +MGMT_STATUS_INVALID_PARAMS , + &cp->addr,sizeof (cp->addr)); goto unlock; }list_del (¶ms->action);list_del (¶ms->list);kfree (params); -__hci_update_background_scan (&req); +hci_update_background_scan (hdev);device_removed (sk, hdev, &cp->addr.bdaddr, cp->addr.type); }else {
@@ -6385,9 +6330,10 @@ static int remove_device(struct sock *sk, struct hci_dev *hdev,struct bdaddr_list *b, *btmp;if (cp->addr.type) { - err = cmd->cmd_complete (cmd, -MGMT_STATUS_INVALID_PARAMS ); -mgmt_pending_remove (cmd); + err =mgmt_cmd_complete (sk, hdev->id, +MGMT_OP_REMOVE_DEVICE , +MGMT_STATUS_INVALID_PARAMS , + &cp->addr,sizeof (cp->addr)); goto unlock; }
@@ -6397,7 +6343,7 @@ static int remove_device(struct sock *sk, struct hci_dev *hdev,kfree (b); } -__hci_update_page_scan (&req); +hci_update_page_scan (hdev);list_for_each_entry_safe (p, tmp, &hdev->le_conn_params, list) {if (p->auto_connect ==HCI_AUTO_CONN_DISABLED )
@@ -6414,20 +6360,13 @@ static int remove_device(struct sock *sk, struct hci_dev *hdev,BT_DBG ("All LE connection parameters were removed" ); -__hci_update_background_scan (&req); +hci_update_background_scan (hdev); } complete: - err =hci_req_run (&req, remove_device_complete); -if (err <0 ) { -/* ENODATA means no HCI commands were needed (e.g. if -* the adapter is powered off). -*/ -if (err == -ENODATA ) - err = cmd->cmd_complete (cmd,MGMT_STATUS_SUCCESS ); -mgmt_pending_remove (cmd); - } - + err =mgmt_cmd_complete (sk, hdev->id,MGMT_OP_REMOVE_DEVICE , +MGMT_STATUS_SUCCESS , &cp->addr, +sizeof (cp->addr)); unlock:hci_dev_unlock (hdev);return err;
Signature · 1 line
2.5.0
PATCH v2 4/12 Bluetooth: Add HCI status return parameter to hci_req_sync()
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
In some cases it may be important to get the exact HCI status rather than the converted HCI-to-errno value. Add an optional return parameter to the hci_req_sync() API to allow for this. Since there are no good HCI translation candidates for cancelation and timeout, use the "unknown" status code for those cases.
--- include/net/bluetooth/hci.h | 3 ++- net/bluetooth/hci_core.c | 26 +++++++++++++------------- net/bluetooth/hci_request.c | 12 +++++++++--- net/bluetooth/hci_request.h | 4 ++-- 4 files changed, 26 insertions(+), 19 deletions(-)
include/net/bluetooth/hci.h +2 -1
@@ -452,7 +452,8 @@ enum {#define HCI_ERROR_REMOTE_POWER_OFF 0x15#define HCI_ERROR_LOCAL_HOST_TERM 0x16#define HCI_ERROR_PAIRING_NOT_ALLOWED 0x18 -#define HCI_ERROR_INVALID_LL_PARAMS 0x1E +#define HCI_ERROR_INVALID_LL_PARAMS 0x1e +#define HCI_ERROR_UNSPECIFIED 0x1f#define HCI_ERROR_ADVERTISING_TIMEOUT 0x3c/* Flow control modes */
net/bluetooth/hci_core.c +13 -13
@@ -763,14 +763,14 @@ static int __hci_init(struct hci_dev *hdev) {int err; - err =__hci_req_sync (hdev, hci_init1_req,0 ,HCI_INIT_TIMEOUT ); + err =__hci_req_sync (hdev, hci_init1_req,0 ,HCI_INIT_TIMEOUT ,NULL );if (err <0 )return err;if (hci_dev_test_flag (hdev,HCI_SETUP ))hci_debugfs_create_basic (hdev); - err =__hci_req_sync (hdev, hci_init2_req,0 ,HCI_INIT_TIMEOUT ); + err =__hci_req_sync (hdev, hci_init2_req,0 ,HCI_INIT_TIMEOUT ,NULL );if (err <0 )return err;
@@ -781,11 +781,11 @@ static int __hci_init(struct hci_dev *hdev)if (hdev->dev_type !=HCI_BREDR )return 0 ; - err =__hci_req_sync (hdev, hci_init3_req,0 ,HCI_INIT_TIMEOUT ); + err =__hci_req_sync (hdev, hci_init3_req,0 ,HCI_INIT_TIMEOUT ,NULL );if (err <0 )return err; - err =__hci_req_sync (hdev, hci_init4_req,0 ,HCI_INIT_TIMEOUT ); + err =__hci_req_sync (hdev, hci_init4_req,0 ,HCI_INIT_TIMEOUT ,NULL );if (err <0 )return err;
@@ -841,7 +841,7 @@ static int __hci_unconf_init(struct hci_dev *hdev)if (test_bit (HCI_QUIRK_RAW_DEVICE , &hdev->quirks))return 0 ; - err =__hci_req_sync (hdev, hci_init0_req,0 ,HCI_INIT_TIMEOUT ); + err =__hci_req_sync (hdev, hci_init0_req,0 ,HCI_INIT_TIMEOUT ,NULL );if (err <0 )return err;
@@ -1199,7 +1199,7 @@ int hci_inquiry(void __user *arg)if (do_inquiry) { err =hci_req_sync (hdev, hci_inq_req, (unsigned long ) &ir, - timeo); + timeo,NULL );if (err <0 ) goto done;
@@ -1565,7 +1565,7 @@ int hci_dev_do_close(struct hci_dev *hdev)if (test_bit (HCI_QUIRK_RESET_ON_CLOSE , &hdev->quirks) && !auto_off && !hci_dev_test_flag (hdev,HCI_UNCONFIGURED )) {set_bit (HCI_INIT , &hdev->flags); -__hci_req_sync (hdev, hci_reset_req,0 ,HCI_CMD_TIMEOUT ); +__hci_req_sync (hdev, hci_reset_req,0 ,HCI_CMD_TIMEOUT ,NULL );clear_bit (HCI_INIT , &hdev->flags); }
@@ -1662,7 +1662,7 @@ static int hci_dev_do_reset(struct hci_dev *hdev)atomic_set (&hdev->cmd_cnt,1 ); hdev->acl_cnt =0 ; hdev->sco_cnt =0 ; hdev->le_cnt =0 ; - ret =__hci_req_sync (hdev, hci_reset_req,0 ,HCI_INIT_TIMEOUT ); + ret =__hci_req_sync (hdev, hci_reset_req,0 ,HCI_INIT_TIMEOUT ,NULL );hci_req_sync_unlock (hdev);return ret;
@@ -1797,7 +1797,7 @@ int hci_dev_cmd(unsigned int cmd, void __user *arg)switch (cmd) {case HCISETAUTH : err =hci_req_sync (hdev, hci_auth_req, dr.dev_opt, -HCI_INIT_TIMEOUT ); +HCI_INIT_TIMEOUT ,NULL );break ;case HCISETENCRYPT :
@@ -1809,18 +1809,18 @@ int hci_dev_cmd(unsigned int cmd, void __user *arg)if (!test_bit (HCI_AUTH , &hdev->flags)) {/* Auth must be enabled first */ err =hci_req_sync (hdev, hci_auth_req, dr.dev_opt, -HCI_INIT_TIMEOUT ); +HCI_INIT_TIMEOUT ,NULL );if (err)break ; } err =hci_req_sync (hdev, hci_encrypt_req, dr.dev_opt, -HCI_INIT_TIMEOUT ); +HCI_INIT_TIMEOUT ,NULL );break ;case HCISETSCAN : err =hci_req_sync (hdev, hci_scan_req, dr.dev_opt, -HCI_INIT_TIMEOUT ); +HCI_INIT_TIMEOUT ,NULL );/* Ensure that the connectable and discoverable states * get correctly modified as this was a non-mgmt change.
@@ -1831,7 +1831,7 @@ int hci_dev_cmd(unsigned int cmd, void __user *arg)case HCISETLINKPOL : err =hci_req_sync (hdev, hci_linkpol_req, dr.dev_opt, -HCI_INIT_TIMEOUT ); +HCI_INIT_TIMEOUT ,NULL );break ;case HCISETLINKMODE :
net/bluetooth/hci_request.c +9 -3
@@ -186,7 +186,7 @@ EXPORT_SYMBOL(__hci_cmd_sync);/* Execute request and wait for completion. */ int __hci_req_sync (struct hci_dev *hdev,void (*func)(struct hci_request *req,unsigned long opt), -unsigned long opt,__u32 timeout) +unsigned long opt,u32 timeout,u8 *hci_status) {struct hci_request req;DECLARE_WAITQUEUE (wait, current);
@@ -231,14 +231,20 @@ int __hci_req_sync(struct hci_dev *hdev, void (*func)(struct hci_request *req,switch (hdev->req_status) {case HCI_REQ_DONE : err = -bt_to_errno (hdev->req_result); +if (hci_status) + *hci_status = hdev->req_result;break ;case HCI_REQ_CANCELED : err = -hdev->req_result; +if (hci_status) + *hci_status =HCI_ERROR_UNSPECIFIED ;break ;default : err = -ETIMEDOUT ; +if (hci_status) + *hci_status =HCI_ERROR_UNSPECIFIED ;break ; }
@@ -251,7 +257,7 @@ int __hci_req_sync(struct hci_dev *hdev, void (*func)(struct hci_request *req,int hci_req_sync (struct hci_dev *hdev,void (*req)(struct hci_request *req,unsigned long opt), -unsigned long opt,__u32 timeout) +unsigned long opt,u32 timeout,u8 *hci_status) {int ret;
@@ -260,7 +266,7 @@ int hci_req_sync(struct hci_dev *hdev, void (*req)(struct hci_request *req,/* Serialize all requests */ hci_req_sync_lock (hdev); - ret =__hci_req_sync (hdev, req, opt, timeout); + ret =__hci_req_sync (hdev, req, opt, timeout, hci_status);hci_req_sync_unlock (hdev);return ret;
net/bluetooth/hci_request.h +2 -2
@@ -46,10 +46,10 @@ void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status,int hci_req_sync (struct hci_dev *hdev,void (*req)(struct hci_request *req,unsigned long opt), -unsigned long opt,__u32 timeout); +unsigned long opt,u32 timeout,u8 *hci_status);int __hci_req_sync (struct hci_dev *hdev,void (*func)(struct hci_request *req,unsigned long opt), -unsigned long opt,__u32 timeout); +unsigned long opt,u32 timeout,u8 *hci_status);void hci_req_sync_cancel (struct hci_dev *hdev,int err); struct sk_buff *hci_prepare_cmd(struct hci_dev *hdev, u16 opcode, u32 plen,
Signature · 1 line
2.5.0
PATCH v2 5/12 Bluetooth: Use req_workqueue for explicit connect requests
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
Since explicit connect requests are also a sub-category of passive scan updates, run them through the same workqueue as the other passive scan changes.
--- net/bluetooth/hci_conn.c | 39 ++++----------------------------------- net/bluetooth/hci_request.c | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 36 deletions(-)
net/bluetooth/hci_conn.c +4 -35
@@ -933,26 +933,6 @@ done:return conn; } -static void hci_connect_le_scan_complete (struct hci_dev *hdev,u8 status, -u16 opcode) -{ -struct hci_conn *conn; - -if (!status) -return ; - -BT_ERR ("Failed to add device to auto conn whitelist: status 0x%2.2x" , - status); - -hci_dev_lock (hdev); - - conn =hci_conn_hash_lookup_state (hdev,LE_LINK ,BT_CONNECT ); -if (conn) -hci_le_conn_failed (conn, status); - -hci_dev_unlock (hdev); -} -static bool is_connected (struct hci_dev *hdev,bdaddr_t *addr,u8 type) {struct hci_conn *conn;
@@ -968,10 +948,9 @@ static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type) }/* This function requires the caller holds hdev->lock */ -static int hci_explicit_conn_params_set (struct hci_request *req, +static int hci_explicit_conn_params_set (struct hci_dev *hdev,bdaddr_t *addr,u8 addr_type) { -struct hci_dev *hdev = req->hdev;struct hci_conn_params *params;if (is_connected (hdev, addr, addr_type))
@@ -999,7 +978,6 @@ static int hci_explicit_conn_params_set(struct hci_request *req, } params->explicit_connect = true; -__hci_update_background_scan (req);BT_DBG ("addr %pMR (type %u) auto_connect %u" , addr, addr_type, params->auto_connect);
@@ -1013,8 +991,6 @@ struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst, u16 conn_timeout, u8 role) {struct hci_conn *conn; -struct hci_request req; -int err;/* Let's make sure that le is enabled.*/ if (!hci_dev_test_flag (hdev,HCI_LE_ENABLED )) {
@@ -1046,25 +1022,18 @@ struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,if (!conn)return ERR_PTR (-ENOMEM ); -hci_req_init (&req, hdev); - -if (hci_explicit_conn_params_set (&req, dst, dst_type) <0 ) +if (hci_explicit_conn_params_set (hdev, dst, dst_type) <0 )return ERR_PTR (-EBUSY ); conn->state =BT_CONNECT ;set_bit (HCI_CONN_SCANNING , &conn->flags); - - err =hci_req_run (&req, hci_connect_le_scan_complete); -if (err && err != -ENODATA ) { -hci_conn_del (conn); -return ERR_PTR (err); - } - conn->dst_type = dst_type; conn->sec_level =BT_SECURITY_LOW ; conn->pending_sec_level = sec_level; conn->conn_timeout = conn_timeout; +hci_update_background_scan (hdev); + done:hci_conn_hold (conn);return conn;
net/bluetooth/hci_request.c +14 -1
@@ -841,8 +841,21 @@ static void bg_scan_update(struct work_struct *work) { struct hci_dev *hdev = container_of(work, struct hci_dev, bg_scan_update); +struct hci_conn *conn; +u8 status; +int err; + + err =hci_req_sync (hdev, update_bg_scan,0 ,HCI_CMD_TIMEOUT , &status); +if (!err) +return ; + +hci_dev_lock (hdev); + + conn =hci_conn_hash_lookup_state (hdev,LE_LINK ,BT_CONNECT ); +if (conn) +hci_le_conn_failed (conn, status); -hci_req_sync (hdev, update_bg_scan,0 ,HCI_CMD_TIMEOUT ); +hci_dev_unlock (hdev); }void hci_request_setup (struct hci_dev *hdev)
Signature · 1 line
2.5.0
PATCH v2 6/12 Bluetooth: Use req_workqueue for background scanning when powering on
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
We can easily use the new req_workqueue based background scan update for the power on case. This also removes the last external user of __hci_update_background_scan().
--- net/bluetooth/mgmt.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
net/bluetooth/mgmt.c +4 -6
@@ -7465,9 +7465,8 @@ void mgmt_index_removed(struct hci_dev *hdev) }/* This function requires the caller holds hdev->lock */ -static void restart_le_actions (struct hci_request *req) +static void restart_le_actions (struct hci_dev *hdev) { -struct hci_dev *hdev = req->hdev;struct hci_conn_params *p;list_for_each_entry (p, &hdev->le_conn_params, list) {
@@ -7488,8 +7487,6 @@ static void restart_le_actions(struct hci_request *req)break ; } } - -__hci_update_background_scan (req); }static void powered_complete (struct hci_dev *hdev,u8 status,u16 opcode)
@@ -7505,6 +7502,9 @@ static void powered_complete(struct hci_dev *hdev, u8 status, u16 opcode)* decide if the public address or static address is used. */ smp_register (hdev); + +restart_le_actions (hdev); +hci_update_background_scan (hdev); }hci_dev_lock (hdev);
@@ -7583,8 +7583,6 @@ static int powered_update_hci(struct hci_dev *hdev) hdev->cur_adv_instance)schedule_adv_instance (&req, hdev->cur_adv_instance, true); - -restart_le_actions (&req); } link_sec =hci_dev_test_flag (hdev,HCI_LINK_SECURITY );
Signature · 1 line
2.5.0
PATCH v2 7/12 Bluetooth: Make __hci_update_background_scan private to hci_request.c
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
There are no more external users so this API can be made private.
--- net/bluetooth/hci_request.c | 2 +- net/bluetooth/hci_request.h | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-)
net/bluetooth/hci_request.c +1 -1
@@ -670,7 +670,7 @@ void hci_update_page_scan(struct hci_dev *hdev)* * This function requires the caller holds hdev->lock. */ -void __hci_update_background_scan (struct hci_request *req) +static void __hci_update_background_scan (struct hci_request *req) {struct hci_dev *hdev = req->hdev;
net/bluetooth/hci_request.h +0 -2
@@ -64,8 +64,6 @@ void __hci_update_page_scan(struct hci_request *req);int hci_update_random_address (struct hci_request *req,bool require_privacy,u8 *own_addr_type); -void __hci_update_background_scan (struct hci_request *req); -int hci_abort_conn (struct hci_conn *conn,u8 reason);void __hci_abort_conn (struct hci_request *req,struct hci_conn *conn,u8 reason);
Signature · 1 line
2.5.0
PATCH v2 8/12 Bluetooth: Move LE scan disable/restart behind req_workqueue
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
To avoid any risks of races, place also these LE scan modification work callbacks behind the same work queue as the other LE scan changes.
--- include/net/bluetooth/hci_core.h | 5 +- net/bluetooth/hci_core.c | 168 ------------------------------------ net/bluetooth/hci_request.c | 179 +++++++++++++++++++++++++++++++++++++++ net/bluetooth/mgmt.c | 4 +- 4 files changed, 183 insertions(+), 173 deletions(-)
include/net/bluetooth/hci_core.h +2 -3
@@ -328,6 +328,8 @@ struct hci_dev {struct work_struct tx_work;struct work_struct bg_scan_update; +struct delayed_work le_scan_disable; +struct delayed_work le_scan_restart;struct sk_buff_head rx_q;struct sk_buff_head raw_q;
@@ -372,9 +374,6 @@ struct hci_dev {DECLARE_BITMAP (dev_flags, __HCI_NUM_FLAGS); -struct delayed_work le_scan_disable; -struct delayed_work le_scan_restart; -__s8 adv_tx_power;__u8 adv_data[HCI_MAX_AD_LENGTH ];__u8 adv_data_len;
net/bluetooth/hci_core.c +0 -168
186 lines not on this page · raw message
net/bluetooth/hci_request.c +179 -0
191 lines not on this page · raw message
net/bluetooth/mgmt.c +2 -2
@@ -4367,7 +4367,7 @@ static void start_discovery_complete(struct hci_dev *hdev, u8 status, hdev->discovery.scan_duration = timeout; } -queue_delayed_work (hdev->workqueue, +queue_delayed_work (hdev->req_workqueue, &hdev->le_scan_disable, timeout); }
@@ -8389,7 +8389,7 @@ static void restart_le_scan(struct hci_dev *hdev) hdev->discovery.scan_duration))return ; -queue_delayed_work (hdev->workqueue, &hdev->le_scan_restart, +queue_delayed_work (hdev->req_workqueue, &hdev->le_scan_restart,DISCOV_LE_RESTART_DELAY ); }
Signature · 1 line
2.5.0
PATCH v2 9/12 Bluetooth: Add discovery type validity helper
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
As preparation for moving the discovery HCI commands behind req_workqueue, add a helper and do the validity checks of the given discovery type before proceeding further. This way we don't need to do them again in hci_request.c.
--- net/bluetooth/mgmt.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+)
net/bluetooth/mgmt.c +40 -0
@@ -4375,6 +4375,33 @@ unlock:hci_dev_unlock (hdev); } +static bool discovery_type_is_valid (struct hci_dev *hdev,uint8_t type, +uint8_t *mgmt_status) +{ +switch (type) { +case DISCOV_TYPE_LE : + *mgmt_status =mgmt_le_support (hdev); +if (*mgmt_status) +return false; +break ; +case DISCOV_TYPE_INTERLEAVED : + *mgmt_status =mgmt_le_support (hdev); +if (*mgmt_status) +return false; +/* Intentional fall-through */ +case DISCOV_TYPE_BREDR : + *mgmt_status =mgmt_bredr_support (hdev); +if (*mgmt_status) +return false; +break ; +default : + *mgmt_status =MGMT_STATUS_INVALID_PARAMS ; +return false; + } + +return true; +} +static int start_discovery (struct sock *sk,struct hci_dev *hdev,void *data,u16 len) {
@@ -4403,6 +4430,12 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev, goto failed; } +if (!discovery_type_is_valid (hdev, cp->type, &status)) { + err =mgmt_cmd_complete (sk, hdev->id,MGMT_OP_START_DISCOVERY , + status, &cp->type,sizeof (cp->type)); + goto failed; + } + cmd =mgmt_pending_add (sk,MGMT_OP_START_DISCOVERY , hdev, data, len);if (!cmd) { err = -ENOMEM ;
@@ -4502,6 +4535,13 @@ static int start_service_discovery(struct sock *sk, struct hci_dev *hdev, goto failed; } +if (!discovery_type_is_valid (hdev, cp->type, &status)) { + err =mgmt_cmd_complete (sk, hdev->id, +MGMT_OP_START_SERVICE_DISCOVERY , + status, &cp->type,sizeof (cp->type)); + goto failed; + } + cmd =mgmt_pending_add (sk,MGMT_OP_START_SERVICE_DISCOVERY , hdev, data, len);if (!cmd) {
Signature · 1 line
2.5.0
PATCH v2 10/12 Bluetooth: Add error return value to hci_req_sync callback
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
In some circumstances it may be useful to abort the request through checks done in the request callback. To make the feature possible this patch changes the return value of the request callback from void to int and aborts the request if a non-zero value is returned.
--- net/bluetooth/hci_core.c | 45 ++++++++++++++++++++++++++++++++------------- net/bluetooth/hci_request.c | 27 ++++++++++++++++++--------- net/bluetooth/hci_request.h | 8 ++++---- 3 files changed, 54 insertions(+), 26 deletions(-)
net/bluetooth/hci_core.c +32 -13
@@ -189,13 +189,14 @@ static void hci_debugfs_create_basic(struct hci_dev *hdev) &vendor_diag_fops); } -static void hci_reset_req (struct hci_request *req,unsigned long opt) +static int hci_reset_req (struct hci_request *req,unsigned long opt) {BT_DBG ("%s %ld" , req->hdev->name, opt);/* Reset device */ set_bit (HCI_RESET , &req->hdev->flags);hci_req_add (req,HCI_OP_RESET ,0 ,NULL ); +return 0 ; }static void bredr_init (struct hci_request *req)
@@ -235,7 +236,7 @@ static void amp_init1(struct hci_request *req)hci_req_add (req,HCI_OP_READ_LOCATION_DATA ,0 ,NULL ); } -static void amp_init2 (struct hci_request *req) +static int amp_init2 (struct hci_request *req) {/* Read Local Supported Features. Not all AMP controllers * support this so it's placed conditionally in the second
@@ -243,9 +244,11 @@ static void amp_init2(struct hci_request *req)*/ if (req->hdev->commands[14 ] &0x20 )hci_req_add (req,HCI_OP_READ_LOCAL_FEATURES ,0 ,NULL ); + +return 0 ; } -static void hci_init1_req (struct hci_request *req,unsigned long opt) +static int hci_init1_req (struct hci_request *req,unsigned long opt) {struct hci_dev *hdev = req->hdev;
@@ -268,6 +271,8 @@ static void hci_init1_req(struct hci_request *req, unsigned long opt)BT_ERR ("Unknown device type %d" , hdev->dev_type);break ; } + +return 0 ; }static void bredr_setup (struct hci_request *req)
@@ -422,7 +427,7 @@ static void hci_setup_event_mask(struct hci_request *req)hci_req_add (req,HCI_OP_SET_EVENT_MASK ,sizeof (events), events); } -static void hci_init2_req (struct hci_request *req,unsigned long opt) +static int hci_init2_req (struct hci_request *req,unsigned long opt) {struct hci_dev *hdev = req->hdev;
@@ -502,6 +507,8 @@ static void hci_init2_req(struct hci_request *req, unsigned long opt)hci_req_add (req,HCI_OP_WRITE_AUTH_ENABLE ,sizeof (enable), &enable); } + +return 0 ; }static void hci_setup_link_policy (struct hci_request *req)
@@ -576,7 +583,7 @@ static void hci_set_event_mask_page_2(struct hci_request *req)hci_req_add (req,HCI_OP_SET_EVENT_MASK_PAGE_2 ,sizeof (events), events); } -static void hci_init3_req (struct hci_request *req,unsigned long opt) +static int hci_init3_req (struct hci_request *req,unsigned long opt) {struct hci_dev *hdev = req->hdev;u8 p;
@@ -704,9 +711,11 @@ static void hci_init3_req(struct hci_request *req, unsigned long opt)hci_req_add (req,HCI_OP_READ_LOCAL_EXT_FEATURES ,sizeof (cp), &cp); } + +return 0 ; } -static void hci_init4_req (struct hci_request *req,unsigned long opt) +static int hci_init4_req (struct hci_request *req,unsigned long opt) {struct hci_dev *hdev = req->hdev;
@@ -757,6 +766,8 @@ static void hci_init4_req(struct hci_request *req, unsigned long opt)hci_req_add (req,HCI_OP_WRITE_SC_SUPPORT ,sizeof (support), &support); } + +return 0 ; }static int __hci_init (struct hci_dev *hdev)
@@ -816,7 +827,7 @@ static int __hci_init(struct hci_dev *hdev)return 0 ; } -static void hci_init0_req (struct hci_request *req,unsigned long opt) +static int hci_init0_req (struct hci_request *req,unsigned long opt) {struct hci_dev *hdev = req->hdev;
@@ -832,6 +843,8 @@ static void hci_init0_req(struct hci_request *req, unsigned long opt)/* Read BD Address */ if (hdev->set_bdaddr)hci_req_add (req,HCI_OP_READ_BD_ADDR ,0 ,NULL ); + +return 0 ; }static int __hci_unconf_init (struct hci_dev *hdev)
@@ -851,7 +864,7 @@ static int __hci_unconf_init(struct hci_dev *hdev)return 0 ; } -static void hci_scan_req (struct hci_request *req,unsigned long opt) +static int hci_scan_req (struct hci_request *req,unsigned long opt) {__u8 scan = opt;
@@ -859,9 +872,10 @@ static void hci_scan_req(struct hci_request *req, unsigned long opt)/* Inquiry and Page scans */ hci_req_add (req,HCI_OP_WRITE_SCAN_ENABLE ,1 , &scan); +return 0 ; } -static void hci_auth_req (struct hci_request *req,unsigned long opt) +static int hci_auth_req (struct hci_request *req,unsigned long opt) {__u8 auth = opt;
@@ -869,9 +883,10 @@ static void hci_auth_req(struct hci_request *req, unsigned long opt)/* Authentication */ hci_req_add (req,HCI_OP_WRITE_AUTH_ENABLE ,1 , &auth); +return 0 ; } -static void hci_encrypt_req (struct hci_request *req,unsigned long opt) +static int hci_encrypt_req (struct hci_request *req,unsigned long opt) {__u8 encrypt = opt;
@@ -879,9 +894,10 @@ static void hci_encrypt_req(struct hci_request *req, unsigned long opt)/* Encryption */ hci_req_add (req,HCI_OP_WRITE_ENCRYPT_MODE ,1 , &encrypt); +return 0 ; } -static void hci_linkpol_req (struct hci_request *req,unsigned long opt) +static int hci_linkpol_req (struct hci_request *req,unsigned long opt) {__le16 policy =cpu_to_le16 (opt);
@@ -889,6 +905,7 @@ static void hci_linkpol_req(struct hci_request *req, unsigned long opt)/* Default link policy */ hci_req_add (req,HCI_OP_WRITE_DEF_LINK_POLICY ,2 , &policy); +return 0 ; }/* Get HCI device by index.
@@ -1133,7 +1150,7 @@ static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf)return copied; } -static void hci_inq_req (struct hci_request *req,unsigned long opt) +static int hci_inq_req (struct hci_request *req,unsigned long opt) {struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt;struct hci_dev *hdev = req->hdev;
@@ -1142,13 +1159,15 @@ static void hci_inq_req(struct hci_request *req, unsigned long opt)BT_DBG ("%s" , hdev->name);if (test_bit (HCI_INQUIRY , &hdev->flags)) -return ; +return 0 ;/* Start Inquiry */ memcpy (&cp.lap, &ir->lap,3 ); cp.length = ir->length; cp.num_rsp = ir->num_rsp;hci_req_add (req,HCI_OP_INQUIRY ,sizeof (cp), &cp); + +return 0 ; }int hci_inquiry (void __user *arg)
net/bluetooth/hci_request.c +18 -9
@@ -184,8 +184,8 @@ struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,EXPORT_SYMBOL (__hci_cmd_sync);/* Execute request and wait for completion. */ -int __hci_req_sync (struct hci_dev *hdev,void (*func)(struct hci_request *req, -unsigned long opt), +int __hci_req_sync (struct hci_dev *hdev,int (*func)(struct hci_request *req, +unsigned long opt),unsigned long opt,u32 timeout,u8 *hci_status) {struct hci_request req;
@@ -198,7 +198,12 @@ int __hci_req_sync(struct hci_dev *hdev, void (*func)(struct hci_request *req, hdev->req_status =HCI_REQ_PEND ; -func (&req, opt); + err =func (&req, opt); +if (err) { +if (hci_status) + *hci_status =HCI_ERROR_UNSPECIFIED ; +return err; + }add_wait_queue (&hdev->req_wait_q, &wait);set_current_state (TASK_INTERRUPTIBLE );
@@ -255,8 +260,8 @@ int __hci_req_sync(struct hci_dev *hdev, void (*func)(struct hci_request *req,return err; } -int hci_req_sync (struct hci_dev *hdev,void (*req)(struct hci_request *req, -unsigned long opt), +int hci_req_sync (struct hci_dev *hdev,int (*req)(struct hci_request *req, +unsigned long opt),unsigned long opt,u32 timeout,u8 *hci_status) {int ret;
@@ -830,11 +835,12 @@ int hci_abort_conn(struct hci_conn *conn, u8 reason)return 0 ; } -static void update_bg_scan (struct hci_request *req,unsigned long opt) +static int update_bg_scan (struct hci_request *req,unsigned long opt) {hci_dev_lock (req->hdev);__hci_update_background_scan (req);hci_dev_unlock (req->hdev); +return 0 ; }static void bg_scan_update (struct work_struct *work)
@@ -932,9 +938,10 @@ static void le_scan_disable_work_complete(struct hci_dev *hdev, u8 status) } } -static void le_scan_disable (struct hci_request *req,unsigned long opt) +static int le_scan_disable (struct hci_request *req,unsigned long opt) {hci_req_add_le_scan_disable (req); +return 0 ; }static void le_scan_disable_work (struct work_struct *work)
@@ -1000,14 +1007,14 @@ unlock:hci_dev_unlock (hdev); } -static void le_scan_restart (struct hci_request *req,unsigned long opt) +static int le_scan_restart (struct hci_request *req,unsigned long opt) {struct hci_dev *hdev = req->hdev;struct hci_cp_le_set_scan_enable cp;/* If controller is not scanning we are done. */ if (!hci_dev_test_flag (hdev,HCI_LE_SCAN )) -return ; +return 0 ;hci_req_add_le_scan_disable (req);
@@ -1015,6 +1022,8 @@ static void le_scan_restart(struct hci_request *req, unsigned long opt) cp.enable =LE_SCAN_ENABLE ; cp.filter_dup =LE_SCAN_FILTER_DUP_ENABLE ;hci_req_add (req,HCI_OP_LE_SET_SCAN_ENABLE ,sizeof (cp), &cp); + +return 0 ; }static void le_scan_restart_work (struct work_struct *work)
net/bluetooth/hci_request.h +4 -4
@@ -44,11 +44,11 @@ void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status, hci_req_complete_t *req_complete, hci_req_complete_skb_t *req_complete_skb); -int hci_req_sync (struct hci_dev *hdev,void (*req)(struct hci_request *req, -unsigned long opt), +int hci_req_sync (struct hci_dev *hdev,int (*req)(struct hci_request *req, +unsigned long opt),unsigned long opt,u32 timeout,u8 *hci_status); -int __hci_req_sync (struct hci_dev *hdev,void (*func)(struct hci_request *req, -unsigned long opt), +int __hci_req_sync (struct hci_dev *hdev,int (*func)(struct hci_request *req, +unsigned long opt),unsigned long opt,u32 timeout,u8 *hci_status);void hci_req_sync_cancel (struct hci_dev *hdev,int err);
Signature · 1 line
2.5.0
PATCH v2 12/12 Bluetooth: Move Stop Discovery to req_workqueue
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
Since discovery also deals with LE scanning it makes sense to move it behind the same req_workqueue as other LE scanning changes. This also simplifies the logic since we do many of the actions in a synchronous manner.
Part of this refactoring is moving hci_req_stop_discovery() to hci_request.c. At the same time the function receives support for properly handling the STOPPING state since that's the state we'll be in when stopping through the req_workqueue.
--- include/net/bluetooth/hci_core.h | 1 + net/bluetooth/hci_request.c | 62 ++++++++++++++++++++++++++++++++++ net/bluetooth/hci_request.h | 3 ++ net/bluetooth/mgmt.c | 72 +++------------------------------------- 4 files changed, 71 insertions(+), 67 deletions(-)
include/net/bluetooth/hci_core.h +1 -0
@@ -1475,6 +1475,7 @@ void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, u8 status);void mgmt_set_local_name_complete (struct hci_dev *hdev,u8 *name,u8 status);void mgmt_start_discovery_complete (struct hci_dev *hdev,u8 status); +void mgmt_stop_discovery_complete (struct hci_dev *hdev,u8 status);void mgmt_device_found (struct hci_dev *hdev,bdaddr_t *bdaddr,u8 link_type,u8 addr_type,u8 *dev_class,s8 rssi,u32 flags,u8 *eir,u16 eir_len,u8 *scan_rsp,u8 scan_rsp_len);
net/bluetooth/hci_request.c +62 -0
74 lines not on this page · raw message
net/bluetooth/hci_request.h +3 -0
@@ -58,6 +58,9 @@ struct sk_buff *hci_prepare_cmd(struct hci_dev *hdev, u16 opcode, u32 plen,void hci_req_add_le_scan_disable (struct hci_request *req);void hci_req_add_le_passive_scan (struct hci_request *req); +/* Returns true if HCI commands were queued */ +bool hci_req_stop_discovery (struct hci_request *req); +void hci_update_page_scan (struct hci_dev *hdev);void __hci_update_page_scan (struct hci_request *req);
net/bluetooth/mgmt.c +5 -67
108 lines not on this page · raw message
Signature · 1 line
2.5.0
Marcel Holtmann
maintainerHi Johan,
Quoted 56 lines
v2: updated version with feedback for 01/12 and 02/12 taken into account.
From the original cover letter:
The current bluetooth-next tree is not able to reliably and in a race-free manner handle updates to the LE scan state. This results in the kernel ending up out of sync with the actual LE scan state and starts sending incorrect scan state HCI commands, often resulting in "not allowed" HCI responses.
This is an initial set of patches to try solve the issue. I've tried to retain as much as possible of the original logic so that it's easier to see that nothing should get broken. Because of this, there are still many simplifications and cleanups that can be done. However, since I've already got 12 patches lined up I wanted to get them sent, reviewed and merged. After that I can concentrate on the cleanups and I'll also be looking into moving the adverising state changes to
hci_request.cas well.The general solution that this set takes to solve the synchronization issues with the LE scan state, is to move all of it behind the
hdev->req_workqueueand perform the actions synchronously. The main tool I've used for this is thehci_req_sync()API which allows taking existing asynchronous HCI request helpers and using them to perform the same commands synchronously (by passing the helper as the callback tohci_req_sync).Since only one work callback is active at a time we get an easy way to avoid races between the changes. All of the new synchronous code can be found in
hci_request.cwhich seemed like an appropriate place for it.I've done a fair amount of testing with the patches, including mgmt-tester, l2cap-tester and various bluetoothd tests (both with controllers supporting simultaneous Inquiry+LE scan as well as those not supporting it). However, because of the size of the changes it's perfectly possible that I've missed or broken something, so it'd be good if others could give the set a spin before merging it.
Johan
----------------------------------------------------------------
Johan Hedberg (12):
Bluetooth: Add stubs for synchronous HCI request functionality
Bluetooth: Run all background scan updates throughreq_workqueue
Bluetooth: Don't wait for HCI in Add/Remove Device
Bluetooth: Add HCI status return parameter tohci_req_sync()
Bluetooth: Usereq_workqueuefor explicit connect requests
Bluetooth: Usereq_workqueuefor background scanning when powering on
Bluetooth: Make__hci_update_background_scanprivate tohci_request.c
Bluetooth: Move LE scan disable/restart behindreq_workqueue
Bluetooth: Add discovery type validity helper
Bluetooth: Add error return value tohci_req_synccallback
Bluetooth: Move Start Discovery toreq_workqueue
Bluetooth: Move Stop Discovery toreq_workqueue
Quoted 8 lines
include/net/bluetooth/hci.h | 3 +- include/net/bluetooth/hci_core.h | 10 +- net/bluetooth/hci_conn.c | 39 +-- net/bluetooth/hci_core.c | 243 ++++------------- net/bluetooth/hci_request.c | 537 +++++++++++++++++++++++++++++++++++--- net/bluetooth/hci_request.h | 26 +- net/bluetooth/mgmt.c | 503 +++++++---------------------------- 7 files changed, 684 insertions(+), 677 deletions(-)
patches 1-10 have been applied to bluetooth-next tree.
Regards
Marcel