Skip to content

Commit 7cf5c29

Browse files
liveusrVudentz
authored andcommitted
Bluetooth: hci_sync: Refactor remove Adv Monitor
Make use of hci_cmd_sync_queue for removing 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 b747a83 commit 7cf5c29

File tree

5 files changed

+75
-178
lines changed

5 files changed

+75
-178
lines changed

include/net/bluetooth/hci_core.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,10 +1420,9 @@ 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_remove_adv_monitor_complete(struct hci_dev *hdev, u8 status);
14241423
int hci_add_adv_monitor(struct hci_dev *hdev, struct adv_monitor *monitor);
1425-
bool hci_remove_single_adv_monitor(struct hci_dev *hdev, u16 handle, int *err);
1426-
bool hci_remove_all_adv_monitor(struct hci_dev *hdev, int *err);
1424+
int hci_remove_single_adv_monitor(struct hci_dev *hdev, u16 handle);
1425+
int hci_remove_all_adv_monitor(struct hci_dev *hdev);
14271426
bool hci_is_adv_monitoring(struct hci_dev *hdev);
14281427
int hci_get_adv_monitor_offload_ext(struct hci_dev *hdev);
14291428

@@ -1883,7 +1882,6 @@ void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev,
18831882
u8 instance);
18841883
void mgmt_adv_monitor_removed(struct hci_dev *hdev, u16 handle);
18851884
int mgmt_phy_configuration_changed(struct hci_dev *hdev, struct sock *skip);
1886-
int mgmt_remove_adv_monitor_complete(struct hci_dev *hdev, u8 status);
18871885
void mgmt_adv_monitor_device_lost(struct hci_dev *hdev, u16 handle,
18881886
bdaddr_t *bdaddr, u8 addr_type);
18891887

net/bluetooth/hci_core.c

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

1883-
int hci_remove_adv_monitor_complete(struct hci_dev *hdev, u8 status)
1884-
{
1885-
return mgmt_remove_adv_monitor_complete(hdev, status);
1886-
}
1887-
18881883
/* Assigns handle to a monitor, and if offloading is supported and power is on,
18891884
* also attempts to forward the request to the controller.
18901885
* This function requires the caller holds hci_req_sync_lock.
@@ -1933,92 +1928,72 @@ int hci_add_adv_monitor(struct hci_dev *hdev, struct adv_monitor *monitor)
19331928

19341929
/* Attempts to tell the controller and free the monitor. If somehow the
19351930
* controller doesn't have a corresponding handle, remove anyway.
1936-
* Returns true if request is forwarded (result is pending), false otherwise.
1937-
* This function requires the caller holds hdev->lock.
1931+
* This function requires the caller holds hci_req_sync_lock.
19381932
*/
1939-
static bool hci_remove_adv_monitor(struct hci_dev *hdev,
1940-
struct adv_monitor *monitor,
1941-
u16 handle, int *err)
1933+
static int hci_remove_adv_monitor(struct hci_dev *hdev,
1934+
struct adv_monitor *monitor)
19421935
{
1943-
*err = 0;
1936+
int status = 0;
19441937

19451938
switch (hci_get_adv_monitor_offload_ext(hdev)) {
19461939
case HCI_ADV_MONITOR_EXT_NONE: /* also goes here when powered off */
1940+
bt_dev_dbg(hdev, "%s remove monitor %d status %d", hdev->name,
1941+
monitor->handle, status);
19471942
goto free_monitor;
1943+
19481944
case HCI_ADV_MONITOR_EXT_MSFT:
1949-
*err = msft_remove_monitor(hdev, monitor, handle);
1945+
status = msft_remove_monitor(hdev, monitor);
1946+
bt_dev_dbg(hdev, "%s remove monitor %d msft status %d",
1947+
hdev->name, monitor->handle, status);
19501948
break;
19511949
}
19521950

19531951
/* In case no matching handle registered, just free the monitor */
1954-
if (*err == -ENOENT)
1952+
if (status == -ENOENT)
19551953
goto free_monitor;
19561954

1957-
return (*err == 0);
1955+
return status;
19581956

19591957
free_monitor:
1960-
if (*err == -ENOENT)
1958+
if (status == -ENOENT)
19611959
bt_dev_warn(hdev, "Removing monitor with no matching handle %d",
19621960
monitor->handle);
19631961
hci_free_adv_monitor(hdev, monitor);
19641962

1965-
*err = 0;
1966-
return false;
1963+
return status;
19671964
}
19681965

1969-
/* Returns true if request is forwarded (result is pending), false otherwise.
1970-
* This function requires the caller holds hdev->lock.
1971-
*/
1972-
bool hci_remove_single_adv_monitor(struct hci_dev *hdev, u16 handle, int *err)
1966+
/* This function requires the caller holds hci_req_sync_lock */
1967+
int hci_remove_single_adv_monitor(struct hci_dev *hdev, u16 handle)
19731968
{
19741969
struct adv_monitor *monitor = idr_find(&hdev->adv_monitors_idr, handle);
1975-
bool pending;
1976-
1977-
if (!monitor) {
1978-
*err = -EINVAL;
1979-
return false;
1980-
}
1981-
1982-
pending = hci_remove_adv_monitor(hdev, monitor, handle, err);
1983-
if (!*err && !pending)
1984-
hci_update_passive_scan(hdev);
19851970

1986-
bt_dev_dbg(hdev, "%s remove monitor handle %d, status %d, %spending",
1987-
hdev->name, handle, *err, pending ? "" : "not ");
1971+
if (!monitor)
1972+
return -EINVAL;
19881973

1989-
return pending;
1974+
return hci_remove_adv_monitor(hdev, monitor);
19901975
}
19911976

1992-
/* Returns true if request is forwarded (result is pending), false otherwise.
1993-
* This function requires the caller holds hdev->lock.
1994-
*/
1995-
bool hci_remove_all_adv_monitor(struct hci_dev *hdev, int *err)
1977+
/* This function requires the caller holds hci_req_sync_lock */
1978+
int hci_remove_all_adv_monitor(struct hci_dev *hdev)
19961979
{
19971980
struct adv_monitor *monitor;
19981981
int idr_next_id = 0;
1999-
bool pending = false;
2000-
bool update = false;
2001-
2002-
*err = 0;
1982+
int status = 0;
20031983

2004-
while (!*err && !pending) {
1984+
while (1) {
20051985
monitor = idr_get_next(&hdev->adv_monitors_idr, &idr_next_id);
20061986
if (!monitor)
20071987
break;
20081988

2009-
pending = hci_remove_adv_monitor(hdev, monitor, 0, err);
1989+
status = hci_remove_adv_monitor(hdev, monitor);
1990+
if (status)
1991+
return status;
20101992

2011-
if (!*err && !pending)
2012-
update = true;
1993+
idr_next_id++;
20131994
}
20141995

2015-
if (update)
2016-
hci_update_passive_scan(hdev);
2017-
2018-
bt_dev_dbg(hdev, "%s remove all monitors status %d, %spending",
2019-
hdev->name, *err, pending ? "" : "not ");
2020-
2021-
return pending;
1996+
return status;
20221997
}
20231998

20241999
/* This function requires the caller holds hdev->lock */

net/bluetooth/mgmt.c

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4861,49 +4861,46 @@ static int add_adv_patterns_monitor_rssi(struct sock *sk, struct hci_dev *hdev,
48614861
MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI);
48624862
}
48634863

4864-
int mgmt_remove_adv_monitor_complete(struct hci_dev *hdev, u8 status)
4864+
static void mgmt_remove_adv_monitor_complete(struct hci_dev *hdev,
4865+
void *data, int status)
48654866
{
48664867
struct mgmt_rp_remove_adv_monitor rp;
4867-
struct mgmt_cp_remove_adv_monitor *cp;
4868-
struct mgmt_pending_cmd *cmd;
4869-
int err = 0;
4868+
struct mgmt_pending_cmd *cmd = data;
4869+
struct mgmt_cp_remove_adv_monitor *cp = cmd->param;
48704870

48714871
hci_dev_lock(hdev);
48724872

4873-
cmd = pending_find(MGMT_OP_REMOVE_ADV_MONITOR, hdev);
4874-
if (!cmd)
4875-
goto done;
4876-
4877-
cp = cmd->param;
48784873
rp.monitor_handle = cp->monitor_handle;
48794874

48804875
if (!status)
48814876
hci_update_passive_scan(hdev);
48824877

4883-
err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
4884-
mgmt_status(status), &rp, sizeof(rp));
4878+
mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
4879+
mgmt_status(status), &rp, sizeof(rp));
48854880
mgmt_pending_remove(cmd);
48864881

4887-
done:
48884882
hci_dev_unlock(hdev);
4889-
bt_dev_dbg(hdev, "remove monitor %d complete, status %u",
4883+
bt_dev_dbg(hdev, "remove monitor %d complete, status %d",
48904884
rp.monitor_handle, status);
4885+
}
48914886

4892-
return err;
4887+
static int mgmt_remove_adv_monitor_sync(struct hci_dev *hdev, void *data)
4888+
{
4889+
struct mgmt_pending_cmd *cmd = data;
4890+
struct mgmt_cp_remove_adv_monitor *cp = cmd->param;
4891+
u16 handle = __le16_to_cpu(cp->monitor_handle);
4892+
4893+
if (!handle)
4894+
return hci_remove_all_adv_monitor(hdev);
4895+
4896+
return hci_remove_single_adv_monitor(hdev, handle);
48934897
}
48944898

48954899
static int remove_adv_monitor(struct sock *sk, struct hci_dev *hdev,
48964900
void *data, u16 len)
48974901
{
4898-
struct mgmt_cp_remove_adv_monitor *cp = data;
4899-
struct mgmt_rp_remove_adv_monitor rp;
49004902
struct mgmt_pending_cmd *cmd;
4901-
u16 handle = __le16_to_cpu(cp->monitor_handle);
49024903
int err, status;
4903-
bool pending;
4904-
4905-
BT_DBG("request for %s", hdev->name);
4906-
rp.monitor_handle = cp->monitor_handle;
49074904

49084905
hci_dev_lock(hdev);
49094906

@@ -4921,34 +4918,23 @@ static int remove_adv_monitor(struct sock *sk, struct hci_dev *hdev,
49214918
goto unlock;
49224919
}
49234920

4924-
if (handle)
4925-
pending = hci_remove_single_adv_monitor(hdev, handle, &err);
4926-
else
4927-
pending = hci_remove_all_adv_monitor(hdev, &err);
4921+
err = hci_cmd_sync_queue(hdev, mgmt_remove_adv_monitor_sync, cmd,
4922+
mgmt_remove_adv_monitor_complete);
49284923

49294924
if (err) {
49304925
mgmt_pending_remove(cmd);
49314926

4932-
if (err == -ENOENT)
4933-
status = MGMT_STATUS_INVALID_INDEX;
4927+
if (err == -ENOMEM)
4928+
status = MGMT_STATUS_NO_RESOURCES;
49344929
else
49354930
status = MGMT_STATUS_FAILED;
49364931

4937-
goto unlock;
4938-
}
4939-
4940-
/* monitor can be removed without forwarding request to controller */
4941-
if (!pending) {
49424932
mgmt_pending_remove(cmd);
4943-
hci_dev_unlock(hdev);
4944-
4945-
return mgmt_cmd_complete(sk, hdev->id,
4946-
MGMT_OP_REMOVE_ADV_MONITOR,
4947-
MGMT_STATUS_SUCCESS,
4948-
&rp, sizeof(rp));
4933+
goto unlock;
49494934
}
49504935

49514936
hci_dev_unlock(hdev);
4937+
49524938
return 0;
49534939

49544940
unlock:

0 commit comments

Comments
 (0)