Skip to content

Commit b747a83

Browse files
liveusrVudentz
authored andcommitted
Bluetooth: hci_sync: Refactor add Adv Monitor
Make use of hci_cmd_sync_queue for adding an advertisement monitor. Signed-off-by: Manish Mandlik <[email protected]> Reviewed-by: Miao-chen Chou <[email protected]> Signed-off-by: Luiz Augusto von Dentz <[email protected]>
1 parent 63b1a7d commit b747a83

File tree

4 files changed

+78
-196
lines changed

4 files changed

+78
-196
lines changed

include/net/bluetooth/hci_core.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,10 +1420,8 @@ bool hci_adv_instance_is_scannable(struct hci_dev *hdev, u8 instance);
14201420

14211421
void hci_adv_monitors_clear(struct hci_dev *hdev);
14221422
void hci_free_adv_monitor(struct hci_dev *hdev, struct adv_monitor *monitor);
1423-
int hci_add_adv_patterns_monitor_complete(struct hci_dev *hdev, u8 status);
14241423
int hci_remove_adv_monitor_complete(struct hci_dev *hdev, u8 status);
1425-
bool hci_add_adv_monitor(struct hci_dev *hdev, struct adv_monitor *monitor,
1426-
int *err);
1424+
int hci_add_adv_monitor(struct hci_dev *hdev, struct adv_monitor *monitor);
14271425
bool hci_remove_single_adv_monitor(struct hci_dev *hdev, u16 handle, int *err);
14281426
bool hci_remove_all_adv_monitor(struct hci_dev *hdev, int *err);
14291427
bool hci_is_adv_monitoring(struct hci_dev *hdev);
@@ -1885,7 +1883,6 @@ void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev,
18851883
u8 instance);
18861884
void mgmt_adv_monitor_removed(struct hci_dev *hdev, u16 handle);
18871885
int mgmt_phy_configuration_changed(struct hci_dev *hdev, struct sock *skip);
1888-
int mgmt_add_adv_patterns_monitor_complete(struct hci_dev *hdev, u8 status);
18891886
int mgmt_remove_adv_monitor_complete(struct hci_dev *hdev, u8 status);
18901887
void mgmt_adv_monitor_device_lost(struct hci_dev *hdev, u16 handle,
18911888
bdaddr_t *bdaddr, u8 addr_type);

net/bluetooth/hci_core.c

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,61 +1880,55 @@ void hci_free_adv_monitor(struct hci_dev *hdev, struct adv_monitor *monitor)
18801880
kfree(monitor);
18811881
}
18821882

1883-
int hci_add_adv_patterns_monitor_complete(struct hci_dev *hdev, u8 status)
1884-
{
1885-
return mgmt_add_adv_patterns_monitor_complete(hdev, status);
1886-
}
1887-
18881883
int hci_remove_adv_monitor_complete(struct hci_dev *hdev, u8 status)
18891884
{
18901885
return mgmt_remove_adv_monitor_complete(hdev, status);
18911886
}
18921887

18931888
/* Assigns handle to a monitor, and if offloading is supported and power is on,
18941889
* also attempts to forward the request to the controller.
1895-
* Returns true if request is forwarded (result is pending), false otherwise.
1896-
* This function requires the caller holds hdev->lock.
1890+
* This function requires the caller holds hci_req_sync_lock.
18971891
*/
1898-
bool hci_add_adv_monitor(struct hci_dev *hdev, struct adv_monitor *monitor,
1899-
int *err)
1892+
int hci_add_adv_monitor(struct hci_dev *hdev, struct adv_monitor *monitor)
19001893
{
19011894
int min, max, handle;
1895+
int status = 0;
19021896

1903-
*err = 0;
1897+
if (!monitor)
1898+
return -EINVAL;
19041899

1905-
if (!monitor) {
1906-
*err = -EINVAL;
1907-
return false;
1908-
}
1900+
hci_dev_lock(hdev);
19091901

19101902
min = HCI_MIN_ADV_MONITOR_HANDLE;
19111903
max = HCI_MIN_ADV_MONITOR_HANDLE + HCI_MAX_ADV_MONITOR_NUM_HANDLES;
19121904
handle = idr_alloc(&hdev->adv_monitors_idr, monitor, min, max,
19131905
GFP_KERNEL);
1914-
if (handle < 0) {
1915-
*err = handle;
1916-
return false;
1917-
}
1906+
1907+
hci_dev_unlock(hdev);
1908+
1909+
if (handle < 0)
1910+
return handle;
19181911

19191912
monitor->handle = handle;
19201913

19211914
if (!hdev_is_powered(hdev))
1922-
return false;
1915+
return status;
19231916

19241917
switch (hci_get_adv_monitor_offload_ext(hdev)) {
19251918
case HCI_ADV_MONITOR_EXT_NONE:
1926-
hci_update_passive_scan(hdev);
1927-
bt_dev_dbg(hdev, "%s add monitor status %d", hdev->name, *err);
1919+
bt_dev_dbg(hdev, "%s add monitor %d status %d", hdev->name,
1920+
monitor->handle, status);
19281921
/* Message was not forwarded to controller - not an error */
1929-
return false;
1922+
break;
1923+
19301924
case HCI_ADV_MONITOR_EXT_MSFT:
1931-
*err = msft_add_monitor_pattern(hdev, monitor);
1932-
bt_dev_dbg(hdev, "%s add monitor msft status %d", hdev->name,
1933-
*err);
1925+
status = msft_add_monitor_pattern(hdev, monitor);
1926+
bt_dev_dbg(hdev, "%s add monitor %d msft status %d", hdev->name,
1927+
monitor->handle, status);
19341928
break;
19351929
}
19361930

1937-
return (*err == 0);
1931+
return status;
19381932
}
19391933

19401934
/* Attempts to tell the controller and free the monitor. If somehow the

net/bluetooth/mgmt.c

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4646,23 +4646,15 @@ static int read_adv_mon_features(struct sock *sk, struct hci_dev *hdev,
46464646
return err;
46474647
}
46484648

4649-
int mgmt_add_adv_patterns_monitor_complete(struct hci_dev *hdev, u8 status)
4649+
static void mgmt_add_adv_patterns_monitor_complete(struct hci_dev *hdev,
4650+
void *data, int status)
46504651
{
46514652
struct mgmt_rp_add_adv_patterns_monitor rp;
4652-
struct mgmt_pending_cmd *cmd;
4653-
struct adv_monitor *monitor;
4654-
int err = 0;
4653+
struct mgmt_pending_cmd *cmd = data;
4654+
struct adv_monitor *monitor = cmd->user_data;
46554655

46564656
hci_dev_lock(hdev);
46574657

4658-
cmd = pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, hdev);
4659-
if (!cmd) {
4660-
cmd = pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR, hdev);
4661-
if (!cmd)
4662-
goto done;
4663-
}
4664-
4665-
monitor = cmd->user_data;
46664658
rp.monitor_handle = cpu_to_le16(monitor->handle);
46674659

46684660
if (!status) {
@@ -4673,26 +4665,29 @@ int mgmt_add_adv_patterns_monitor_complete(struct hci_dev *hdev, u8 status)
46734665
hci_update_passive_scan(hdev);
46744666
}
46754667

4676-
err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
4677-
mgmt_status(status), &rp, sizeof(rp));
4668+
mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
4669+
mgmt_status(status), &rp, sizeof(rp));
46784670
mgmt_pending_remove(cmd);
46794671

4680-
done:
46814672
hci_dev_unlock(hdev);
4682-
bt_dev_dbg(hdev, "add monitor %d complete, status %u",
4673+
bt_dev_dbg(hdev, "add monitor %d complete, status %d",
46834674
rp.monitor_handle, status);
4675+
}
46844676

4685-
return err;
4677+
static int mgmt_add_adv_patterns_monitor_sync(struct hci_dev *hdev, void *data)
4678+
{
4679+
struct mgmt_pending_cmd *cmd = data;
4680+
struct adv_monitor *monitor = cmd->user_data;
4681+
4682+
return hci_add_adv_monitor(hdev, monitor);
46864683
}
46874684

46884685
static int __add_adv_patterns_monitor(struct sock *sk, struct hci_dev *hdev,
46894686
struct adv_monitor *m, u8 status,
46904687
void *data, u16 len, u16 op)
46914688
{
4692-
struct mgmt_rp_add_adv_patterns_monitor rp;
46934689
struct mgmt_pending_cmd *cmd;
46944690
int err;
4695-
bool pending;
46964691

46974692
hci_dev_lock(hdev);
46984693

@@ -4714,31 +4709,18 @@ static int __add_adv_patterns_monitor(struct sock *sk, struct hci_dev *hdev,
47144709
}
47154710

47164711
cmd->user_data = m;
4717-
pending = hci_add_adv_monitor(hdev, m, &err);
4712+
err = hci_cmd_sync_queue(hdev, mgmt_add_adv_patterns_monitor_sync, cmd,
4713+
mgmt_add_adv_patterns_monitor_complete);
47184714
if (err) {
4719-
if (err == -ENOSPC || err == -ENOMEM)
4715+
if (err == -ENOMEM)
47204716
status = MGMT_STATUS_NO_RESOURCES;
4721-
else if (err == -EINVAL)
4722-
status = MGMT_STATUS_INVALID_PARAMS;
47234717
else
47244718
status = MGMT_STATUS_FAILED;
47254719

47264720
mgmt_pending_remove(cmd);
47274721
goto unlock;
47284722
}
47294723

4730-
if (!pending) {
4731-
mgmt_pending_remove(cmd);
4732-
rp.monitor_handle = cpu_to_le16(m->handle);
4733-
mgmt_adv_monitor_added(sk, hdev, m->handle);
4734-
m->state = ADV_MONITOR_STATE_REGISTERED;
4735-
hdev->adv_monitors_cnt++;
4736-
4737-
hci_dev_unlock(hdev);
4738-
return mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_SUCCESS,
4739-
&rp, sizeof(rp));
4740-
}
4741-
47424724
hci_dev_unlock(hdev);
47434725

47444726
return 0;

0 commit comments

Comments
 (0)