Bluetooth: HCI command synchronisation fixes
Subsystems (2)
v3 cover Bluetooth: HCI command synchronisation fixes
Hi,
This v3 is a rebase of the patch set against latest bluetooth-next.git, taking into account to subtle changes to the request API.
Quoted 1 line
From the original cover letter:
This set of patches fixes HCI command synchronization for powered, UUID and Class of Device related mgmt commands. It fixes these issues by using asynchronous requests to wait for the completion of the associated HCI commands and only then responds to user space with a mgmt command complete event.
Johan
----------------------------------------------------------------
Johan Hedberg (6):
Bluetooth: Move power on HCI command updates to their own function
Bluetooth: Update mgmt powered HCI commands to use async requests
Bluetooth: Wait for HCI command completion with mgmt_set_powered
Bluetooth: Fix busy condition testing for EIR and class updates
Bluetooth: Fix UUID/class mgmt command response synchronization
Bluetooth: Remove useless HCI_PENDING_CLASS flag
include/net/bluetooth/hci.h | 1 - net/bluetooth/hci_event.c | 3 +- net/bluetooth/mgmt.c | 367 +++++++++++++++++++++++++++++-------------- 3 files changed, 248 insertions(+), 123 deletions(-)
PATCH v3 1/6 Bluetooth: Move power on HCI command updates to their own function
From: Johan Hedberg <[email protected]>
These commands will in a subsequent patch be performed in their own asynchronous request, so it's more readable (not just from a resulting code perspective but also the way the patches look like) to have them performed in their own function.
--- net/bluetooth/mgmt.c | 78 +++++++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 36 deletions(-)
net/bluetooth/mgmt.c +42 -36
@@ -3058,53 +3058,59 @@ static int set_bredr_scan(struct hci_dev *hdev)return hci_send_cmd (hdev,HCI_OP_WRITE_SCAN_ENABLE ,1 , &scan); } -int mgmt_powered (struct hci_dev *hdev,u8 powered) +static int powered_update_hci (struct hci_dev *hdev) { -struct cmd_lookup match = {NULL , hdev }; -int err; +u8 link_sec; -if (!test_bit (HCI_MGMT , &hdev->dev_flags)) -return 0 ; +if (test_bit (HCI_SSP_ENABLED , &hdev->dev_flags) && + !lmp_host_ssp_capable (hdev)) { +u8 ssp =1 ; -mgmt_pending_foreach (MGMT_OP_SET_POWERED , hdev, settings_rsp, &match); +hci_send_cmd (hdev,HCI_OP_WRITE_SSP_MODE ,1 , &ssp); + } -if (powered) { -u8 link_sec; +if (test_bit (HCI_LE_ENABLED , &hdev->dev_flags)) { +struct hci_cp_write_le_host_supported cp; -if (test_bit (HCI_SSP_ENABLED , &hdev->dev_flags) && - !lmp_host_ssp_capable (hdev)) { -u8 ssp =1 ; + cp.le =1 ; + cp.simul =lmp_le_br_capable (hdev); -hci_send_cmd (hdev,HCI_OP_WRITE_SSP_MODE ,1 , &ssp); - } +/* 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_send_cmd (hdev,HCI_OP_WRITE_LE_HOST_SUPPORTED , +sizeof (cp), &cp); + } -if (test_bit (HCI_LE_ENABLED , &hdev->dev_flags)) { -struct hci_cp_write_le_host_supported cp; + link_sec =test_bit (HCI_LINK_SECURITY , &hdev->dev_flags); +if (link_sec !=test_bit (HCI_AUTH , &hdev->flags)) +hci_send_cmd (hdev,HCI_OP_WRITE_AUTH_ENABLE , +sizeof (link_sec), &link_sec); - cp.le =1 ; - cp.simul =lmp_le_br_capable (hdev); +if (lmp_bredr_capable (hdev)) { +set_bredr_scan (hdev); +update_class (hdev); +update_name (hdev, hdev->dev_name); +update_eir (hdev); + } -/* 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_send_cmd (hdev, -HCI_OP_WRITE_LE_HOST_SUPPORTED , -sizeof (cp), &cp); - } +return 0 ; +} - link_sec =test_bit (HCI_LINK_SECURITY , &hdev->dev_flags); -if (link_sec !=test_bit (HCI_AUTH , &hdev->flags)) -hci_send_cmd (hdev,HCI_OP_WRITE_AUTH_ENABLE , -sizeof (link_sec), &link_sec); +int mgmt_powered (struct hci_dev *hdev,u8 powered) +{ +struct cmd_lookup match = {NULL , hdev }; +int err; -if (lmp_bredr_capable (hdev)) { -set_bredr_scan (hdev); -update_class (hdev); -update_name (hdev, hdev->dev_name); -update_eir (hdev); - } +if (!test_bit (HCI_MGMT , &hdev->dev_flags)) +return 0 ; + +mgmt_pending_foreach (MGMT_OP_SET_POWERED , hdev, settings_rsp, &match); + +if (powered) { +powered_update_hci (hdev); }else {u8 status =MGMT_STATUS_NOT_POWERED ;u8 zero_cod[] = {0 ,0 ,0 };
Signature · 1 line
1.7.10.4
PATCH v3 2/6 Bluetooth: Update mgmt powered HCI commands to use async requests
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
This patch updates sending of HCI commands related to mgmt_set_powered (e.g. class, name and EIR data) to be sent using asynchronous requests. This is necessary since it's the only (well, at least the cleanest) way to keep the power on procedure synchronized and let user space know it has completed only when all HCI commands are completed (this actual fix is coming in a subsequent patch).
--- net/bluetooth/mgmt.c | 163 +++++++++++++++++++++++++++++++------------------- 1 file changed, 100 insertions(+), 63 deletions(-)
net/bluetooth/mgmt.c +100 -63
@@ -591,32 +591,33 @@ static void create_eir(struct hci_dev *hdev, u8 *data) ptr =create_uuid128_list (hdev, ptr,HCI_MAX_EIR_LENGTH - (ptr - data)); } -static int update_eir (struct hci_dev *hdev) +static void update_eir (struct hci_request *req) { +struct hci_dev *hdev = req->hdev;struct hci_cp_write_eir cp;if (!hdev_is_powered (hdev)) -return 0 ; +return ;if (!lmp_ext_inq_capable (hdev)) -return 0 ; +return ;if (!test_bit (HCI_SSP_ENABLED , &hdev->dev_flags)) -return 0 ; +return ;if (test_bit (HCI_SERVICE_CACHE , &hdev->dev_flags)) -return 0 ; +return ;memset (&cp,0 ,sizeof (cp));create_eir (hdev, cp.data);if (memcmp (cp.data, hdev->eir,sizeof (cp.data)) ==0 ) -return 0 ; +return ;memcpy (hdev->eir, cp.data,sizeof (cp.data)); -return hci_send_cmd (hdev,HCI_OP_WRITE_EIR ,sizeof (cp), &cp); +hci_req_add (req,HCI_OP_WRITE_EIR ,sizeof (cp), &cp); }static u8 get_service_classes (struct hci_dev *hdev)
@@ -630,47 +631,50 @@ static u8 get_service_classes(struct hci_dev *hdev)return val; } -static int update_class (struct hci_dev *hdev) +static void update_class (struct hci_request *req) { +struct hci_dev *hdev = req->hdev;u8 cod[3 ]; -int err;BT_DBG ("%s" , hdev->name);if (!hdev_is_powered (hdev)) -return 0 ; +return ;if (test_bit (HCI_SERVICE_CACHE , &hdev->dev_flags)) -return 0 ; +return ; cod[0 ] = hdev->minor_class; cod[1 ] = hdev->major_class; cod[2 ] =get_service_classes (hdev);if (memcmp (cod, hdev->dev_class,3 ) ==0 ) -return 0 ; +return ; - err =hci_send_cmd (hdev,HCI_OP_WRITE_CLASS_OF_DEV ,sizeof (cod), cod); -if (err ==0 ) -set_bit (HCI_PENDING_CLASS , &hdev->dev_flags); +hci_req_add (req,HCI_OP_WRITE_CLASS_OF_DEV ,sizeof (cod), cod); -return err; +set_bit (HCI_PENDING_CLASS , &hdev->dev_flags); }static void service_cache_off (struct work_struct *work) { struct hci_dev *hdev = container_of(work, struct hci_dev, service_cache.work); +struct hci_request req;if (!test_and_clear_bit (HCI_SERVICE_CACHE , &hdev->dev_flags))return ; +hci_req_init (&req, hdev); +hci_dev_lock (hdev); -update_eir (hdev); -update_class (hdev); +update_eir (&req); +update_class (&req);hci_dev_unlock (hdev); + +hci_req_run (&req,NULL ); }static void mgmt_init_hdev (struct sock *sk,struct hci_dev *hdev)
@@ -1355,6 +1359,7 @@ static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) {struct mgmt_cp_add_uuid *cp = data;struct pending_cmd *cmd; +struct hci_request req;struct bt_uuid *uuid;int err;
@@ -1380,13 +1385,12 @@ static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)list_add_tail (&uuid->list, &hdev->uuids); - err =update_class (hdev); -if (err <0 ) - goto failed; +hci_req_init (&req, hdev); - err =update_eir (hdev); -if (err <0 ) - goto failed; +update_class (&req); +update_eir (&req); + +hci_req_run (&req,NULL );if (!test_bit (HCI_PENDING_CLASS , &hdev->dev_flags)) { err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0,
@@ -1395,8 +1399,12 @@ static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) } cmd =mgmt_pending_add (sk,MGMT_OP_ADD_UUID , hdev, data, len); -if (!cmd) +if (!cmd) { err = -ENOMEM ; + goto failed; + } + + err =0 ; failed:hci_dev_unlock (hdev);
@@ -1424,6 +1432,7 @@ static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,struct pending_cmd *cmd;struct bt_uuid *match, *tmp;u8 bt_uuid_any[] = {0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 }; +struct hci_request req;int err, found;BT_DBG ("request for %s" , hdev->name);
@@ -1466,13 +1475,12 @@ static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data, } update_class: - err =update_class (hdev); -if (err <0 ) - goto unlock; +hci_req_init (&req, hdev); - err =update_eir (hdev); -if (err <0 ) - goto unlock; +update_class (&req); +update_eir (&req); + +hci_req_run (&req,NULL );if (!test_bit (HCI_PENDING_CLASS , &hdev->dev_flags)) { err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0,
@@ -1481,8 +1489,12 @@ update_class: } cmd =mgmt_pending_add (sk,MGMT_OP_REMOVE_UUID , hdev, data, len); -if (!cmd) +if (!cmd) { err = -ENOMEM ; + goto unlock; + } + + err =0 ; unlock:hci_dev_unlock (hdev);
@@ -1494,6 +1506,7 @@ static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data, {struct mgmt_cp_set_dev_class *cp = data;struct pending_cmd *cmd; +struct hci_request req;int err;BT_DBG ("request for %s" , hdev->name);
@@ -1521,16 +1534,18 @@ static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data, goto unlock; } +hci_req_init (&req, hdev); +if (test_and_clear_bit (HCI_SERVICE_CACHE , &hdev->dev_flags)) {hci_dev_unlock (hdev);cancel_delayed_work_sync (&hdev->service_cache);hci_dev_lock (hdev); -update_eir (hdev); +update_eir (&req); } - err =update_class (hdev); -if (err <0 ) - goto unlock; +update_class (&req); + +hci_req_run (&req,NULL );if (!test_bit (HCI_PENDING_CLASS , &hdev->dev_flags)) { err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
@@ -1539,8 +1554,12 @@ static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data, } cmd =mgmt_pending_add (sk,MGMT_OP_SET_DEV_CLASS , hdev, data, len); -if (!cmd) +if (!cmd) { err = -ENOMEM ; + goto unlock; + } + + err =0 ; unlock:hci_dev_unlock (hdev);
@@ -2268,13 +2287,13 @@ static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev, HCI_OP_USER_PASSKEY_NEG_REPLY, 0); } -static int update_name (struct hci_dev *hdev,const char *name) +static void update_name (struct hci_request *req,const char *name) {struct hci_cp_write_local_name cp;memcpy (cp.name, name,sizeof (cp.name)); -return hci_send_cmd (hdev,HCI_OP_WRITE_LOCAL_NAME ,sizeof (cp), &cp); +hci_req_add (req,HCI_OP_WRITE_LOCAL_NAME ,sizeof (cp), &cp); } static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
@@ -2282,6 +2301,7 @@ static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data, {struct mgmt_cp_set_local_name *cp = data;struct pending_cmd *cmd; +struct hci_request req;int err;BT_DBG ("" );
@@ -2310,7 +2330,9 @@ static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data, goto failed; } - err =update_name (hdev, cp->name); +hci_req_init (&req, hdev); +update_name (&req, cp->name); + err =hci_req_run (&req,NULL );if (err <0 )mgmt_pending_remove (cmd);
@@ -2698,6 +2720,7 @@ static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) {struct mgmt_cp_set_device_id *cp = data; +struct hci_request req;int err;__u16 source;
@@ -2718,7 +2741,9 @@ static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, err =cmd_complete (sk, hdev->id,MGMT_OP_SET_DEVICE_ID ,0 ,NULL ,0 ); -update_eir (hdev); +hci_req_init (&req, hdev); +update_eir (&req); +hci_req_run (&req,NULL );hci_dev_unlock (hdev);
@@ -3043,8 +3068,9 @@ static void settings_rsp(struct pending_cmd *cmd, void *data)mgmt_pending_free (cmd); } -static int set_bredr_scan (struct hci_dev *hdev) +static void set_bredr_scan (struct hci_request *req) { +struct hci_dev *hdev = req->hdev;u8 scan =0 ;if (test_bit (HCI_CONNECTABLE , &hdev->dev_flags))
@@ -3052,21 +3078,22 @@ static int set_bredr_scan(struct hci_dev *hdev)if (test_bit (HCI_DISCOVERABLE , &hdev->dev_flags)) scan |=SCAN_INQUIRY ; -if (!scan) -return 0 ; - -return hci_send_cmd (hdev,HCI_OP_WRITE_SCAN_ENABLE ,1 , &scan); +if (scan) +hci_req_add (req,HCI_OP_WRITE_SCAN_ENABLE ,1 , &scan); }static int powered_update_hci (struct hci_dev *hdev) { +struct hci_request req;u8 link_sec; +hci_req_init (&req, hdev); +if (test_bit (HCI_SSP_ENABLED , &hdev->dev_flags) && !lmp_host_ssp_capable (hdev)) {u8 ssp =1 ; -hci_send_cmd (hdev,HCI_OP_WRITE_SSP_MODE ,1 , &ssp); +hci_req_add (&req,HCI_OP_WRITE_SSP_MODE ,1 , &ssp); }if (test_bit (HCI_LE_ENABLED , &hdev->dev_flags)) {
@@ -3080,23 +3107,23 @@ static int powered_update_hci(struct hci_dev *hdev)*/ if (cp.le !=lmp_host_le_capable (hdev) || cp.simul !=lmp_host_le_br_capable (hdev)) -hci_send_cmd (hdev,HCI_OP_WRITE_LE_HOST_SUPPORTED , -sizeof (cp), &cp); +hci_req_add (&req,HCI_OP_WRITE_LE_HOST_SUPPORTED , +sizeof (cp), &cp); } link_sec =test_bit (HCI_LINK_SECURITY , &hdev->dev_flags);if (link_sec !=test_bit (HCI_AUTH , &hdev->flags)) -hci_send_cmd (hdev,HCI_OP_WRITE_AUTH_ENABLE , -sizeof (link_sec), &link_sec); +hci_req_add (&req,HCI_OP_WRITE_AUTH_ENABLE , +sizeof (link_sec), &link_sec);if (lmp_bredr_capable (hdev)) { -set_bredr_scan (hdev); -update_class (hdev); -update_name (hdev, hdev->dev_name); -update_eir (hdev); +set_bredr_scan (&req); +update_class (&req); +update_name (&req, hdev->dev_name); +update_eir (&req); } -return 0 ; +return hci_req_run (&req,NULL ); }int mgmt_powered (struct hci_dev *hdev,u8 powered)
@@ -3561,23 +3588,25 @@ int mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status)return err; } -static int clear_eir (struct hci_dev *hdev) +static void clear_eir (struct hci_request *req) { +struct hci_dev *hdev = req->hdev;struct hci_cp_write_eir cp;if (!lmp_ext_inq_capable (hdev)) -return 0 ; +return ;memset (hdev->eir,0 ,sizeof (hdev->eir));memset (&cp,0 ,sizeof (cp)); -return hci_send_cmd (hdev,HCI_OP_WRITE_EIR ,sizeof (cp), &cp); +hci_req_add (req,HCI_OP_WRITE_EIR ,sizeof (cp), &cp); }int mgmt_ssp_enable_complete (struct hci_dev *hdev,u8 enable,u8 status) {struct cmd_lookup match = {NULL , hdev }; +struct hci_request req;bool changed = false;int err =0 ;
@@ -3610,10 +3639,14 @@ int mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)if (match.sk)sock_put (match.sk); +hci_req_init (&req, hdev); +if (test_bit (HCI_SSP_ENABLED , &hdev->dev_flags)) -update_eir (hdev); +update_eir (&req);else -clear_eir (hdev); +clear_eir (&req); + +hci_req_run (&req,NULL );return err; }
@@ -3701,8 +3734,12 @@ send_event:* adapter so only update them here if this is a name change * unrelated to power on. */ -if (!test_bit (HCI_INIT , &hdev->flags)) -update_eir (hdev); +if (!test_bit (HCI_INIT , &hdev->flags)) { +struct hci_request req; +hci_req_init (&req, hdev); +update_eir (&req); +hci_req_run (&req,NULL ); + } failed:if (cmd)
Signature · 1 line
1.7.10.4
PATCH v3 3/6 Bluetooth: Wait for HCI command completion with mgmt_set_powered
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
We should only notify user space that the adapter has been powered on after all HCI commands related to the action have completed. This patch fixes the issue by instating an async request complete callback for these HCI commands and only notifies user space in the callback.
--- net/bluetooth/mgmt.c | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-)
net/bluetooth/mgmt.c +34 -12
@@ -3082,6 +3082,24 @@ static void set_bredr_scan(struct hci_request *req)hci_req_add (req,HCI_OP_WRITE_SCAN_ENABLE ,1 , &scan); } +static void powered_complete (struct hci_dev *hdev,u8 status) +{ +struct cmd_lookup match = {NULL , hdev }; + +BT_DBG ("status 0x%02x" , status); + +hci_dev_lock (hdev); + +mgmt_pending_foreach (MGMT_OP_SET_POWERED , hdev, settings_rsp, &match); + +new_settings (hdev, match.sk); + +hci_dev_unlock (hdev); + +if (match.sk) +sock_put (match.sk); +} +static int powered_update_hci (struct hci_dev *hdev) {struct hci_request req;
@@ -3123,32 +3141,36 @@ static int powered_update_hci(struct hci_dev *hdev)update_eir (&req); } -return hci_req_run (&req,NULL ); +return hci_req_run (&req, powered_complete); }int mgmt_powered (struct hci_dev *hdev,u8 powered) {struct cmd_lookup match = {NULL , hdev }; +u8 status_not_powered =MGMT_STATUS_NOT_POWERED ; +u8 zero_cod[] = {0 ,0 ,0 };int err;if (!test_bit (HCI_MGMT , &hdev->dev_flags))return 0 ; -mgmt_pending_foreach (MGMT_OP_SET_POWERED , hdev, settings_rsp, &match); -if (powered) { -powered_update_hci (hdev); - }else { -u8 status =MGMT_STATUS_NOT_POWERED ; -u8 zero_cod[] = {0 ,0 ,0 }; - -mgmt_pending_foreach (0 , hdev, cmd_status_rsp, &status); +if (powered_update_hci (hdev) ==0 ) +return 0 ; -if (memcmp (hdev->dev_class, zero_cod,sizeof (zero_cod)) !=0 ) -mgmt_event (MGMT_EV_CLASS_OF_DEV_CHANGED , hdev, - zero_cod,sizeof (zero_cod),NULL ); +mgmt_pending_foreach (MGMT_OP_SET_POWERED , hdev, settings_rsp, + &match); + goto new_settings; } +mgmt_pending_foreach (MGMT_OP_SET_POWERED , hdev, settings_rsp, &match); +mgmt_pending_foreach (0 , hdev, cmd_status_rsp, &status_not_powered); + +if (memcmp (hdev->dev_class, zero_cod,sizeof (zero_cod)) !=0 ) +mgmt_event (MGMT_EV_CLASS_OF_DEV_CHANGED , hdev, + zero_cod,sizeof (zero_cod),NULL ); + +new_settings: err =new_settings (hdev, match.sk);if (match.sk)
Signature · 1 line
1.7.10.4
PATCH v3 4/6 Bluetooth: Fix busy condition testing for EIR and class updates
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
The add/remove_uuid and set_dev_class mgmt commands can trigger both EIR and class HCI commands, so testing just for a pending class command is enough. The simplest way to monitor conflicts that should trigger "busy" error returns is to check for any pending mgmt command that can trigger these HCI commands. This patch adds a helper function for this (pending_eir_or_class) and uses it instead of the old HCI_PENDING_CLASS flag to test for busy conditions.
--- net/bluetooth/mgmt.c | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-)
net/bluetooth/mgmt.c +36 -9
@@ -1336,6 +1336,29 @@ unlock:return err; } +/* This is a helper function to test for pending mgmt commands that can +* cause CoD or EIR HCI commands. We can only allow one such pending +* mgmt command at a time since otherwise we cannot easily track what +* the current values are, will be, and based on that calculate if a new +* HCI command needs to be sent and if yes with what value. +*/ +static bool pending_eir_or_class (struct hci_dev *hdev) +{ +struct pending_cmd *cmd; + +list_for_each_entry (cmd, &hdev->mgmt_pending, list) { +switch (cmd->opcode) { +case MGMT_OP_ADD_UUID : +case MGMT_OP_REMOVE_UUID : +case MGMT_OP_SET_DEV_CLASS : +case MGMT_OP_SET_POWERED : +return true; + } + } + +return false; +} +static const u8 bluetooth_base_uuid[] = {0xfb ,0x34 ,0x9b ,0x5f ,0x80 ,0x00 ,0x00 ,0x80 ,0x00 ,0x10 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,
@@ -1367,7 +1390,7 @@ static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)hci_dev_lock (hdev); -if (test_bit (HCI_PENDING_CLASS , &hdev->dev_flags)) { +if (pending_eir_or_class (hdev)) { err =cmd_status (sk, hdev->id,MGMT_OP_ADD_UUID ,MGMT_STATUS_BUSY ); goto failed;
@@ -1439,7 +1462,7 @@ static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,hci_dev_lock (hdev); -if (test_bit (HCI_PENDING_CLASS , &hdev->dev_flags)) { +if (pending_eir_or_class (hdev)) { err =cmd_status (sk, hdev->id,MGMT_OP_REMOVE_UUID ,MGMT_STATUS_BUSY ); goto unlock;
@@ -1515,15 +1538,19 @@ static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,return cmd_status (sk, hdev->id,MGMT_OP_SET_DEV_CLASS ,MGMT_STATUS_NOT_SUPPORTED ); -if (test_bit (HCI_PENDING_CLASS , &hdev->dev_flags)) -return cmd_status (sk, hdev->id,MGMT_OP_SET_DEV_CLASS , -MGMT_STATUS_BUSY ); +hci_dev_lock (hdev); -if ((cp->minor &0x03 ) !=0 || (cp->major &0xe0 ) !=0 ) -return cmd_status (sk, hdev->id,MGMT_OP_SET_DEV_CLASS , -MGMT_STATUS_INVALID_PARAMS ); +if (pending_eir_or_class (hdev)) { + err =cmd_status (sk, hdev->id,MGMT_OP_SET_DEV_CLASS , +MGMT_STATUS_BUSY ); + goto unlock; + } -hci_dev_lock (hdev); +if ((cp->minor &0x03 ) !=0 || (cp->major &0xe0 ) !=0 ) { + err =cmd_status (sk, hdev->id,MGMT_OP_SET_DEV_CLASS , +MGMT_STATUS_INVALID_PARAMS ); + goto unlock; + } hdev->major_class = cp->major; hdev->minor_class = cp->minor;
Signature · 1 line
1.7.10.4
PATCH v3 5/6 Bluetooth: Fix UUID/class mgmt command response synchronization
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
We should only return a mgmt command complete once all HCI commands to a mgmt_set_dev_class or mgmt_add/remove_uuid command have completed. This patch fixes the issue by having a proper async request complete callback for these actions and responding to user space in the callback.
--- net/bluetooth/mgmt.c | 73 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 56 insertions(+), 17 deletions(-)
net/bluetooth/mgmt.c +56 -17
@@ -1378,6 +1378,32 @@ static u8 get_uuid_size(const u8 *uuid)return 16 ; } +static void mgmt_class_complete (struct hci_dev *hdev,u16 mgmt_op,u8 status) +{ +struct pending_cmd *cmd; + +hci_dev_lock (hdev); + + cmd =mgmt_pending_find (mgmt_op, hdev); +if (!cmd) + goto unlock; + +cmd_complete (cmd->sk, cmd->index, cmd->opcode,mgmt_status (status), + hdev->dev_class,3 ); + +mgmt_pending_remove (cmd); + +unlock: +hci_dev_unlock (hdev); +} + +static void add_uuid_complete (struct hci_dev *hdev,u8 status) +{ +BT_DBG ("status 0x%02x" , status); + +mgmt_class_complete (hdev,MGMT_OP_ADD_UUID , status); +} +static int add_uuid (struct sock *sk,struct hci_dev *hdev,void *data,u16 len) {struct mgmt_cp_add_uuid *cp = data;
@@ -1413,9 +1439,11 @@ static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)update_class (&req);update_eir (&req); -hci_req_run (&req,NULL ); + err =hci_req_run (&req, add_uuid_complete); +if (err <0 ) { +if (err != -ENODATA ) + goto failed; -if (!test_bit (HCI_PENDING_CLASS , &hdev->dev_flags)) { err =cmd_complete (sk, hdev->id,MGMT_OP_ADD_UUID ,0 , hdev->dev_class,3 ); goto failed;
@@ -1448,6 +1476,13 @@ static bool enable_service_cache(struct hci_dev *hdev)return false; } +static void remove_uuid_complete (struct hci_dev *hdev,u8 status) +{ +BT_DBG ("status 0x%02x" , status); + +mgmt_class_complete (hdev,MGMT_OP_REMOVE_UUID , status); +} +static int remove_uuid (struct sock *sk,struct hci_dev *hdev,void *data,u16 len) {
@@ -1503,9 +1538,11 @@ update_class:update_class (&req);update_eir (&req); -hci_req_run (&req,NULL ); + err =hci_req_run (&req, remove_uuid_complete); +if (err <0 ) { +if (err != -ENODATA ) + goto unlock; -if (!test_bit (HCI_PENDING_CLASS , &hdev->dev_flags)) { err =cmd_complete (sk, hdev->id,MGMT_OP_REMOVE_UUID ,0 , hdev->dev_class,3 ); goto unlock;
@@ -1524,6 +1561,13 @@ unlock:return err; } +static void set_class_complete (struct hci_dev *hdev,u8 status) +{ +BT_DBG ("status 0x%02x" , status); + +mgmt_class_complete (hdev,MGMT_OP_SET_DEV_CLASS , status); +} +static int set_dev_class (struct sock *sk,struct hci_dev *hdev,void *data,u16 len) {
@@ -1572,9 +1616,11 @@ static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,update_class (&req); -hci_req_run (&req,NULL ); + err =hci_req_run (&req, set_class_complete); +if (err <0 ) { +if (err != -ENODATA ) + goto unlock; -if (!test_bit (HCI_PENDING_CLASS , &hdev->dev_flags)) { err =cmd_complete (sk, hdev->id,MGMT_OP_SET_DEV_CLASS ,0 , hdev->dev_class,3 ); goto unlock;
@@ -3700,21 +3746,14 @@ int mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)return err; } -static void class_rsp (struct pending_cmd *cmd,void *data) +static void sk_lookup (struct pending_cmd *cmd,void *data) {struct cmd_lookup *match = data; -cmd_complete (cmd->sk, cmd->index, cmd->opcode, match->mgmt_status, - match->hdev->dev_class,3 ); - -list_del (&cmd->list); -if (match->sk ==NULL ) { match->sk = cmd->sk;sock_hold (match->sk); } - -mgmt_pending_free (cmd); } int mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
@@ -3725,9 +3764,9 @@ int mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,clear_bit (HCI_PENDING_CLASS , &hdev->dev_flags); -mgmt_pending_foreach (MGMT_OP_SET_DEV_CLASS , hdev, class_rsp, &match); -mgmt_pending_foreach (MGMT_OP_ADD_UUID , hdev, class_rsp, &match); -mgmt_pending_foreach (MGMT_OP_REMOVE_UUID , hdev, class_rsp, &match); +mgmt_pending_foreach (MGMT_OP_SET_DEV_CLASS , hdev, sk_lookup, &match); +mgmt_pending_foreach (MGMT_OP_ADD_UUID , hdev, sk_lookup, &match); +mgmt_pending_foreach (MGMT_OP_REMOVE_UUID , hdev, sk_lookup, &match);if (!status) err = mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class,
Signature · 1 line
1.7.10.4
PATCH v3 6/6 Bluetooth: Remove useless HCI_PENDING_CLASS flag
In reply to Johan HedbergFrom: Johan Hedberg <[email protected]>
Now that class related operations are tracked through asynchronous HCI requests this flag is no longer needed.
--- include/net/bluetooth/hci.h | 1 - net/bluetooth/hci_event.c | 3 +-- net/bluetooth/mgmt.c | 4 ---- 3 files changed, 1 insertion(+), 7 deletions(-)
include/net/bluetooth/hci.h +0 -1
@@ -119,7 +119,6 @@ enum {HCI_CONNECTABLE ,HCI_DISCOVERABLE ,HCI_LINK_SECURITY , -HCI_PENDING_CLASS , HCI_PERIODIC_INQ, };
net/bluetooth/hci_event.c +1 -2
@@ -194,8 +194,7 @@ static void hci_cc_reset(struct hci_dev *hdev, struct sk_buff *skb)clear_bit (HCI_RESET , &hdev->flags);/* Reset all non-persistent flags */ - hdev->dev_flags &= ~(BIT (HCI_LE_SCAN ) |BIT (HCI_PENDING_CLASS ) | -BIT (HCI_PERIODIC_INQ )); + hdev->dev_flags &= ~(BIT (HCI_LE_SCAN ) |BIT (HCI_PERIODIC_INQ )); hdev->discovery.state =DISCOVERY_STOPPED ; hdev->inq_tx_power =HCI_TX_POWER_INVALID ;
net/bluetooth/mgmt.c +0 -4
@@ -652,8 +652,6 @@ static void update_class(struct hci_request *req)return ;hci_req_add (req,HCI_OP_WRITE_CLASS_OF_DEV ,sizeof (cod), cod); - -set_bit (HCI_PENDING_CLASS , &hdev->dev_flags); }static void service_cache_off (struct work_struct *work)
@@ -3762,8 +3760,6 @@ int mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,struct cmd_lookup match = {NULL , hdev,mgmt_status (status) };int err =0 ; -clear_bit (HCI_PENDING_CLASS , &hdev->dev_flags); -mgmt_pending_foreach (MGMT_OP_SET_DEV_CLASS , hdev, sk_lookup, &match);mgmt_pending_foreach (MGMT_OP_ADD_UUID , hdev, sk_lookup, &match);mgmt_pending_foreach (MGMT_OP_REMOVE_UUID , hdev, sk_lookup, &match);
Signature · 1 line
1.7.10.4