Bluetooth: Add/Remove Device completion through hci_request
Subsystems (2)
cover Bluetooth: Add/Remove Device completion through hci_request
Hi,
Here's a second attempt at this set, now rebased against the changes with hci_request.{c,h}.
Johan
----------------------------------------------------------------
Johan Hedberg (4):
Bluetooth: Add hci_request support for hci_update_background_scan
Bluetooth: Fix Remove Device to wait for HCI before sending cmd_complete
Bluetooth: Fix Add Device to wait for HCI before sending cmd_complete
Bluetooth: Add return parameter to cmd_complete callbacks
include/net/bluetooth/hci_core.h | 4 - net/bluetooth/hci_core.c | 147 ---------------------- net/bluetooth/hci_request.c | 96 ++++++++++++++ net/bluetooth/hci_request.h | 3 + net/bluetooth/mgmt.c | 254 +++++++++++++++++++++++++++++--------- 5 files changed, 298 insertions(+), 206 deletions(-)
PATCH 1/4 Bluetooth: Add hci_request support for hci_update_background_scan
From: Johan Hedberg <[email protected]>
Many places using hci_update_background_scan() try to synchronize whatever they're doing with the help of hci_request callbacks. However, since the hci_update_background_scan() function hasn't so far accepted a hci_request pointer any commands triggered by it have been left out by the synchronization. This patch modifies the API in a similar way as was done for hci_update_page_scan, i.e. there's a variant that takes a hci_request and another one that takes a hci_dev.
--- include/net/bluetooth/hci_core.h | 2 - net/bluetooth/hci_core.c | 89 ------------------------------------- net/bluetooth/hci_request.c | 96 ++++++++++++++++++++++++++++++++++++++++ net/bluetooth/hci_request.h | 3 ++ net/bluetooth/mgmt.c | 12 ++--- 5 files changed, 105 insertions(+), 97 deletions(-)
include/net/bluetooth/hci_core.h +0 -2
@@ -930,8 +930,6 @@ struct hci_conn_params *hci_pend_le_action_lookup(struct list_head *list,bdaddr_t *addr, u8 addr_type); -void hci_update_background_scan (struct hci_dev *hdev); -void hci_uuids_clear (struct hci_dev *hdev);void hci_link_keys_clear (struct hci_dev *hdev);
net/bluetooth/hci_core.c +0 -89
95 lines not on this page · raw message
net/bluetooth/hci_request.c +96 -0
99 lines not on this page · raw message
net/bluetooth/hci_request.h +3 -0
@@ -49,3 +49,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_dev *hdev); +void __hci_update_background_scan (struct hci_request *req);
net/bluetooth/mgmt.c +6 -6
@@ -2228,9 +2228,8 @@ static void le_enable_complete(struct hci_dev *hdev, u8 status)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:
@@ -6038,8 +6037,9 @@ void mgmt_index_removed(struct hci_dev *hdev) }/* This function requires the caller holds hdev->lock */ -static void restart_le_actions (struct hci_dev *hdev) +static void restart_le_actions (struct hci_request *req) { +struct hci_dev *hdev = req->hdev;struct hci_conn_params *p;list_for_each_entry (p, &hdev->le_conn_params, list) {
@@ -6061,7 +6061,7 @@ static void restart_le_actions(struct hci_dev *hdev) } } -hci_update_background_scan (hdev); +__hci_update_background_scan (req); }static void powered_complete (struct hci_dev *hdev,u8 status)
@@ -6072,8 +6072,6 @@ static void powered_complete(struct hci_dev *hdev, u8 status)hci_dev_lock (hdev); -restart_le_actions (hdev); -mgmt_pending_foreach (MGMT_OP_SET_POWERED , hdev, settings_rsp, &match);new_settings (hdev, match.sk);
@@ -6131,6 +6129,8 @@ static int powered_update_hci(struct hci_dev *hdev)if (test_bit (HCI_ADVERTISING , &hdev->dev_flags))enable_advertising (&req); + +restart_le_actions (&req); } link_sec =test_bit (HCI_LINK_SECURITY , &hdev->dev_flags);
Signature · 1 line
2.1.0
PATCH 2/4 Bluetooth: Fix Remove Device to wait for HCI before sending cmd_complete
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
This patch updates the Remove Device mgmt command handler to use a hci_request to wait for HCI command completion before notifying user space of the mgmt command completion. This way we ensure that once the mgmt command returns all HCI commands triggered by it have also completed.
--- net/bluetooth/mgmt.c | 84 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 62 insertions(+), 22 deletions(-)
net/bluetooth/mgmt.c +62 -22
@@ -5523,24 +5523,55 @@ 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) +{ +struct pending_cmd *cmd; + +BT_DBG ("status 0x%02x" , status); + +hci_dev_lock (hdev); + + cmd =mgmt_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 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_complete (sk, hdev->id,MGMT_OP_REMOVE_DEVICE , -MGMT_STATUS_INVALID_PARAMS , - &cp->addr,sizeof (cp->addr)); + err =0 ; + cmd->cmd_complete (cmd,MGMT_STATUS_INVALID_PARAMS ); +mgmt_pending_remove (cmd); goto unlock; }
@@ -5549,14 +5580,14 @@ static int remove_device(struct sock *sk, struct hci_dev *hdev, &cp->addr.bdaddr, cp->addr.type);if (err) { - err =cmd_complete (sk, hdev->id, -MGMT_OP_REMOVE_DEVICE , -MGMT_STATUS_INVALID_PARAMS , - &cp->addr,sizeof (cp->addr)); + err =0 ; + cmd->cmd_complete (cmd, +MGMT_STATUS_INVALID_PARAMS ); +mgmt_pending_remove (cmd); goto unlock; } -hci_update_page_scan (hdev); +__hci_update_page_scan (&req);device_removed (sk, hdev, &cp->addr.bdaddr, cp->addr.type);
@@ -5571,23 +5602,23 @@ static int remove_device(struct sock *sk, struct hci_dev *hdev, params =hci_conn_params_lookup (hdev, &cp->addr.bdaddr, addr_type);if (!params) { - err =cmd_complete (sk, hdev->id,MGMT_OP_REMOVE_DEVICE , -MGMT_STATUS_INVALID_PARAMS , - &cp->addr,sizeof (cp->addr)); + err =0 ; + cmd->cmd_complete (cmd,MGMT_STATUS_INVALID_PARAMS ); +mgmt_pending_remove (cmd); goto unlock; }if (params->auto_connect ==HCI_AUTO_CONN_DISABLED ) { - err =cmd_complete (sk, hdev->id,MGMT_OP_REMOVE_DEVICE , -MGMT_STATUS_INVALID_PARAMS , - &cp->addr,sizeof (cp->addr)); + err =0 ; + cmd->cmd_complete (cmd,MGMT_STATUS_INVALID_PARAMS ); +mgmt_pending_remove (cmd); goto unlock; }list_del (¶ms->action);list_del (¶ms->list);kfree (params); -hci_update_background_scan (hdev); +__hci_update_background_scan (&req);device_removed (sk, hdev, &cp->addr.bdaddr, cp->addr.type); }else {
@@ -5595,9 +5626,9 @@ static int remove_device(struct sock *sk, struct hci_dev *hdev,struct bdaddr_list *b, *btmp;if (cp->addr.type) { - err =cmd_complete (sk, hdev->id,MGMT_OP_REMOVE_DEVICE , -MGMT_STATUS_INVALID_PARAMS , - &cp->addr,sizeof (cp->addr)); + err =0 ; + cmd->cmd_complete (cmd,MGMT_STATUS_INVALID_PARAMS ); +mgmt_pending_remove (cmd); goto unlock; }
@@ -5607,7 +5638,7 @@ static int remove_device(struct sock *sk, struct hci_dev *hdev,kfree (b); } -hci_update_page_scan (hdev); +__hci_update_page_scan (&req);list_for_each_entry_safe (p, tmp, &hdev->le_conn_params, list) {if (p->auto_connect ==HCI_AUTO_CONN_DISABLED )
@@ -5620,12 +5651,21 @@ static int remove_device(struct sock *sk, struct hci_dev *hdev,BT_DBG ("All LE connection parameters were removed" ); -hci_update_background_scan (hdev); +__hci_update_background_scan (&req); } complete: - err =cmd_complete (sk, hdev->id,MGMT_OP_REMOVE_DEVICE , -MGMT_STATUS_SUCCESS , &cp->addr,sizeof (cp->addr)); + 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 ) { + cmd->cmd_complete (cmd,MGMT_STATUS_SUCCESS ); + err =0 ; + } +mgmt_pending_remove (cmd); + } unlock:hci_dev_unlock (hdev);
Signature · 1 line
2.1.0
PATCH 3/4 Bluetooth: Fix Add Device to wait for HCI before sending cmd_complete
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
This patch updates the Add Device mgmt command handler to use a hci_request to wait for HCI command completion before notifying user space of the mgmt command completion. To do this we need to add an extra hci_request parameter to the hci_conn_params_set function. Since this function has no other users besides mgmt.c it's moved there as a static function.
--- include/net/bluetooth/hci_core.h | 2 - net/bluetooth/hci_core.c | 58 ------------------- net/bluetooth/mgmt.c | 119 +++++++++++++++++++++++++++++++++++---- 3 files changed, 109 insertions(+), 70 deletions(-)
include/net/bluetooth/hci_core.h +0 -2
@@ -920,8 +920,6 @@ struct hci_conn_params *hci_conn_params_lookup(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type);struct hci_conn_params *hci_conn_params_add (struct hci_dev *hdev,bdaddr_t *addr,u8 addr_type); -int hci_conn_params_set (struct hci_dev *hdev,bdaddr_t *addr,u8 addr_type, -u8 auto_connect);void hci_conn_params_del (struct hci_dev *hdev,bdaddr_t *addr,u8 addr_type);void hci_conn_params_clear_all (struct hci_dev *hdev);void hci_conn_params_clear_disabled (struct hci_dev *hdev);
net/bluetooth/hci_core.c +0 -58
70 lines not on this page · raw message
net/bluetooth/mgmt.c +109 -10
165 lines not on this page · raw message
Signature · 1 line
2.1.0
PATCH 4/4 Bluetooth: Add return parameter to cmd_complete callbacks
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
The cmd_complete callbacks for pending mgmt commands may fail e.g. in the case of memory allocation. Previously this error would be caught and returned to user space in the form of a failed write on the mgmt socket (when the error happened in the mgmt command handler) but with the introduction of the generic cmd_complete callback this information was lost. This patch returns the feature by making cmd_complete callbacks return int instead of void.
--- net/bluetooth/mgmt.c | 85 +++++++++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 40 deletions(-)
net/bluetooth/mgmt.c +45 -40
@@ -139,7 +139,7 @@ struct pending_cmd {size_t param_len;struct sock *sk;void *user_data; -void (*cmd_complete)(struct pending_cmd *cmd,u8 status); +int (*cmd_complete)(struct pending_cmd *cmd,u8 status); };/* HCI to MGMT error code conversion table */
@@ -1487,16 +1487,16 @@ static void cmd_complete_rsp(struct pending_cmd *cmd, void *data)cmd_status_rsp (cmd, data); } -static void generic_cmd_complete (struct pending_cmd *cmd,u8 status) +static int generic_cmd_complete (struct pending_cmd *cmd,u8 status) { -cmd_complete (cmd->sk, cmd->index, cmd->opcode, status, cmd->param, - cmd->param_len); +return cmd_complete (cmd->sk, cmd->index, cmd->opcode, status, + cmd->param, cmd->param_len); } -static void addr_cmd_complete (struct pending_cmd *cmd,u8 status) +static int addr_cmd_complete (struct pending_cmd *cmd,u8 status) { -cmd_complete (cmd->sk, cmd->index, cmd->opcode, status, cmd->param, -sizeof (struct mgmt_addr_info )); +return cmd_complete (cmd->sk, cmd->index, cmd->opcode, status, cmd->param, +sizeof (struct mgmt_addr_info )); }static u8 mgmt_bredr_support (struct hci_dev *hdev)
@@ -3098,16 +3098,17 @@ static struct pending_cmd *find_pairing(struct hci_conn *conn)return NULL ; } -static void pairing_complete (struct pending_cmd *cmd,u8 status) +static int pairing_complete (struct pending_cmd *cmd,u8 status) {struct mgmt_rp_pair_device rp;struct hci_conn *conn = cmd->user_data; +int err;bacpy (&rp.addr.bdaddr, &conn->dst); rp.addr.type =link_to_bdaddr (conn->type, conn->dst_type); -cmd_complete (cmd->sk, cmd->index,MGMT_OP_PAIR_DEVICE , status, - &rp,sizeof (rp)); + err =cmd_complete (cmd->sk, cmd->index,MGMT_OP_PAIR_DEVICE , status, + &rp,sizeof (rp));/* So we don't get further callbacks for this connection */ conn->connect_cfm_cb =NULL ;
@@ -3122,6 +3123,8 @@ static void pairing_complete(struct pending_cmd *cmd, u8 status)clear_bit (HCI_CONN_PARAM_REMOVAL_PEND , &conn->flags);hci_conn_put (conn); + +return err; }void mgmt_smp_complete (struct hci_conn *conn,bool complete)
@@ -3947,9 +3950,10 @@ failed:return err; } -static void service_discovery_cmd_complete (struct pending_cmd *cmd,u8 status) +static int service_discovery_cmd_complete (struct pending_cmd *cmd,u8 status) { -cmd_complete (cmd->sk, cmd->index, cmd->opcode, status, cmd->param,1 ); +return cmd_complete (cmd->sk, cmd->index, cmd->opcode, status, + cmd->param,1 ); } static int start_service_discovery(struct sock *sk, struct hci_dev *hdev,
@@ -5091,10 +5095,11 @@ static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,return err; } -static void conn_info_cmd_complete (struct pending_cmd *cmd,u8 status) +static int conn_info_cmd_complete (struct pending_cmd *cmd,u8 status) {struct hci_conn *conn = cmd->user_data;struct mgmt_rp_get_conn_info rp; +int err;memcpy (&rp.addr, cmd->param,sizeof (rp.addr));
@@ -5108,11 +5113,13 @@ static void conn_info_cmd_complete(struct pending_cmd *cmd, u8 status) rp.max_tx_power =HCI_TX_POWER_INVALID ; } -cmd_complete (cmd->sk, cmd->index,MGMT_OP_GET_CONN_INFO , status, - &rp,sizeof (rp)); + err =cmd_complete (cmd->sk, cmd->index,MGMT_OP_GET_CONN_INFO , status, + &rp,sizeof (rp));hci_conn_drop (conn);hci_conn_put (conn); + +return err; }static void conn_info_refresh_complete (struct hci_dev *hdev,u8 hci_status)
@@ -5286,11 +5293,12 @@ unlock:return err; } -static void clock_info_cmd_complete (struct pending_cmd *cmd,u8 status) +static int clock_info_cmd_complete (struct pending_cmd *cmd,u8 status) {struct hci_conn *conn = cmd->user_data;struct mgmt_rp_get_clock_info rp;struct hci_dev *hdev; +int err;memset (&rp,0 ,sizeof (rp));memcpy (&rp.addr, &cmd->param,sizeof (rp.addr));
@@ -5310,12 +5318,15 @@ static void clock_info_cmd_complete(struct pending_cmd *cmd, u8 status) } complete: -cmd_complete (cmd->sk, cmd->index, cmd->opcode, status, &rp,sizeof (rp)); + err =cmd_complete (cmd->sk, cmd->index, cmd->opcode, status, &rp, +sizeof (rp));if (conn) {hci_conn_drop (conn);hci_conn_put (conn); } + +return err; }static void get_clock_info_complete (struct hci_dev *hdev,u8 status)
@@ -5552,8 +5563,8 @@ 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 =0 ; - cmd->cmd_complete (cmd,MGMT_STATUS_INVALID_PARAMS ); + err = cmd->cmd_complete (cmd, +MGMT_STATUS_INVALID_PARAMS );mgmt_pending_remove (cmd); goto unlock; }
@@ -5585,8 +5596,7 @@ static int add_device(struct sock *sk, struct hci_dev *hdev,*/ if (hci_conn_params_set (&req, &cp->addr.bdaddr, addr_type, auto_conn) <0 ) { - err =0 ; - cmd->cmd_complete (cmd,MGMT_STATUS_FAILED ); + err = cmd->cmd_complete (cmd,MGMT_STATUS_FAILED );mgmt_pending_remove (cmd); goto unlock; }
@@ -5599,10 +5609,8 @@ added:/* ENODATA means no HCI commands were needed (e.g. if * the adapter is powered off). */ -if (err == -ENODATA ) { - cmd->cmd_complete (cmd,MGMT_STATUS_SUCCESS ); - err =0 ; - } +if (err == -ENODATA ) + err = cmd->cmd_complete (cmd,MGMT_STATUS_SUCCESS );mgmt_pending_remove (cmd); }
@@ -5668,8 +5676,8 @@ static int remove_device(struct sock *sk, struct hci_dev *hdev,u8 addr_type;if (!bdaddr_type_is_valid (cp->addr.type)) { - err =0 ; - cmd->cmd_complete (cmd,MGMT_STATUS_INVALID_PARAMS ); + err = cmd->cmd_complete (cmd, +MGMT_STATUS_INVALID_PARAMS );mgmt_pending_remove (cmd); goto unlock; }
@@ -5679,9 +5687,8 @@ static int remove_device(struct sock *sk, struct hci_dev *hdev, &cp->addr.bdaddr, cp->addr.type);if (err) { - err =0 ; - cmd->cmd_complete (cmd, -MGMT_STATUS_INVALID_PARAMS ); + err = cmd->cmd_complete (cmd, +MGMT_STATUS_INVALID_PARAMS );mgmt_pending_remove (cmd); goto unlock; }
@@ -5701,15 +5708,15 @@ static int remove_device(struct sock *sk, struct hci_dev *hdev, params =hci_conn_params_lookup (hdev, &cp->addr.bdaddr, addr_type);if (!params) { - err =0 ; - cmd->cmd_complete (cmd,MGMT_STATUS_INVALID_PARAMS ); + err = cmd->cmd_complete (cmd, +MGMT_STATUS_INVALID_PARAMS );mgmt_pending_remove (cmd); goto unlock; }if (params->auto_connect ==HCI_AUTO_CONN_DISABLED ) { - err =0 ; - cmd->cmd_complete (cmd,MGMT_STATUS_INVALID_PARAMS ); + err = cmd->cmd_complete (cmd, +MGMT_STATUS_INVALID_PARAMS );mgmt_pending_remove (cmd); goto unlock; }
@@ -5725,8 +5732,8 @@ static int remove_device(struct sock *sk, struct hci_dev *hdev,struct bdaddr_list *b, *btmp;if (cp->addr.type) { - err =0 ; - cmd->cmd_complete (cmd,MGMT_STATUS_INVALID_PARAMS ); + err = cmd->cmd_complete (cmd, +MGMT_STATUS_INVALID_PARAMS );mgmt_pending_remove (cmd); goto unlock; }
@@ -5759,10 +5766,8 @@ complete:/* ENODATA means no HCI commands were needed (e.g. if * the adapter is powered off). */ -if (err == -ENODATA ) { - cmd->cmd_complete (cmd,MGMT_STATUS_SUCCESS ); - err =0 ; - } +if (err == -ENODATA ) + err = cmd->cmd_complete (cmd,MGMT_STATUS_SUCCESS );mgmt_pending_remove (cmd); }
Signature · 1 line
2.1.0
Marcel Holtmann
maintainerHi Johan,
Quoted 12 lines
Here's a second attempt at this set, now rebased against the changes with hci_request.{c,h}.
Johan
----------------------------------------------------------------
Johan Hedberg (4):
Bluetooth: Addhci_requestsupport forhci_update_background_scan
Bluetooth: Fix Remove Device to wait for HCI before sendingcmd_complete
Bluetooth: Fix Add Device to wait for HCI before sendingcmd_complete
Bluetooth: Add return parameter tocmd_completecallbacks
Quoted 6 lines
include/net/bluetooth/hci_core.h | 4 - net/bluetooth/hci_core.c | 147 ---------------------- net/bluetooth/hci_request.c | 96 ++++++++++++++ net/bluetooth/hci_request.h | 3 + net/bluetooth/mgmt.c | 254 +++++++++++++++++++++++++++++--------- 5 files changed, 298 insertions(+), 206 deletions(-)
all 4 patches have been applied to bluetooth-next tree.
Regards
Marcel