2 BlueZ - Bluetooth protocol stack for Linux
4 Copyright (C) 2014 Intel Corporation
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation;
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
11 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
13 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
14 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
15 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
20 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
21 SOFTWARE IS DISCLAIMED.
24 #include <net/bluetooth/bluetooth.h>
25 #include <net/bluetooth/hci_core.h>
28 #include "hci_request.h"
30 #define HCI_REQ_DONE 0
31 #define HCI_REQ_PEND 1
32 #define HCI_REQ_CANCELED 2
34 void hci_req_init(struct hci_request
*req
, struct hci_dev
*hdev
)
36 skb_queue_head_init(&req
->cmd_q
);
41 static int req_run(struct hci_request
*req
, hci_req_complete_t complete
,
42 hci_req_complete_skb_t complete_skb
)
44 struct hci_dev
*hdev
= req
->hdev
;
48 BT_DBG("length %u", skb_queue_len(&req
->cmd_q
));
50 /* If an error occurred during request building, remove all HCI
51 * commands queued on the HCI request queue.
54 skb_queue_purge(&req
->cmd_q
);
58 /* Do not allow empty requests */
59 if (skb_queue_empty(&req
->cmd_q
))
62 skb
= skb_peek_tail(&req
->cmd_q
);
64 bt_cb(skb
)->hci
.req_complete
= complete
;
65 } else if (complete_skb
) {
66 bt_cb(skb
)->hci
.req_complete_skb
= complete_skb
;
67 bt_cb(skb
)->hci
.req_flags
|= HCI_REQ_SKB
;
70 spin_lock_irqsave(&hdev
->cmd_q
.lock
, flags
);
71 skb_queue_splice_tail(&req
->cmd_q
, &hdev
->cmd_q
);
72 spin_unlock_irqrestore(&hdev
->cmd_q
.lock
, flags
);
74 queue_work(hdev
->workqueue
, &hdev
->cmd_work
);
79 int hci_req_run(struct hci_request
*req
, hci_req_complete_t complete
)
81 return req_run(req
, complete
, NULL
);
84 int hci_req_run_skb(struct hci_request
*req
, hci_req_complete_skb_t complete
)
86 return req_run(req
, NULL
, complete
);
89 static void hci_req_sync_complete(struct hci_dev
*hdev
, u8 result
, u16 opcode
,
92 BT_DBG("%s result 0x%2.2x", hdev
->name
, result
);
94 if (hdev
->req_status
== HCI_REQ_PEND
) {
95 hdev
->req_result
= result
;
96 hdev
->req_status
= HCI_REQ_DONE
;
98 hdev
->req_skb
= skb_get(skb
);
99 wake_up_interruptible(&hdev
->req_wait_q
);
103 void hci_req_sync_cancel(struct hci_dev
*hdev
, int err
)
105 BT_DBG("%s err 0x%2.2x", hdev
->name
, err
);
107 if (hdev
->req_status
== HCI_REQ_PEND
) {
108 hdev
->req_result
= err
;
109 hdev
->req_status
= HCI_REQ_CANCELED
;
110 wake_up_interruptible(&hdev
->req_wait_q
);
114 struct sk_buff
*__hci_cmd_sync_ev(struct hci_dev
*hdev
, u16 opcode
, u32 plen
,
115 const void *param
, u8 event
, u32 timeout
)
117 DECLARE_WAITQUEUE(wait
, current
);
118 struct hci_request req
;
122 BT_DBG("%s", hdev
->name
);
124 hci_req_init(&req
, hdev
);
126 hci_req_add_ev(&req
, opcode
, plen
, param
, event
);
128 hdev
->req_status
= HCI_REQ_PEND
;
130 add_wait_queue(&hdev
->req_wait_q
, &wait
);
131 set_current_state(TASK_INTERRUPTIBLE
);
133 err
= hci_req_run_skb(&req
, hci_req_sync_complete
);
135 remove_wait_queue(&hdev
->req_wait_q
, &wait
);
136 set_current_state(TASK_RUNNING
);
140 schedule_timeout(timeout
);
142 remove_wait_queue(&hdev
->req_wait_q
, &wait
);
144 if (signal_pending(current
))
145 return ERR_PTR(-EINTR
);
147 switch (hdev
->req_status
) {
149 err
= -bt_to_errno(hdev
->req_result
);
152 case HCI_REQ_CANCELED
:
153 err
= -hdev
->req_result
;
161 hdev
->req_status
= hdev
->req_result
= 0;
163 hdev
->req_skb
= NULL
;
165 BT_DBG("%s end: err %d", hdev
->name
, err
);
173 return ERR_PTR(-ENODATA
);
177 EXPORT_SYMBOL(__hci_cmd_sync_ev
);
179 struct sk_buff
*__hci_cmd_sync(struct hci_dev
*hdev
, u16 opcode
, u32 plen
,
180 const void *param
, u32 timeout
)
182 return __hci_cmd_sync_ev(hdev
, opcode
, plen
, param
, 0, timeout
);
184 EXPORT_SYMBOL(__hci_cmd_sync
);
186 /* Execute request and wait for completion. */
187 int __hci_req_sync(struct hci_dev
*hdev
, int (*func
)(struct hci_request
*req
,
189 unsigned long opt
, u32 timeout
, u8
*hci_status
)
191 struct hci_request req
;
192 DECLARE_WAITQUEUE(wait
, current
);
195 BT_DBG("%s start", hdev
->name
);
197 hci_req_init(&req
, hdev
);
199 hdev
->req_status
= HCI_REQ_PEND
;
201 err
= func(&req
, opt
);
204 *hci_status
= HCI_ERROR_UNSPECIFIED
;
208 add_wait_queue(&hdev
->req_wait_q
, &wait
);
209 set_current_state(TASK_INTERRUPTIBLE
);
211 err
= hci_req_run_skb(&req
, hci_req_sync_complete
);
213 hdev
->req_status
= 0;
215 remove_wait_queue(&hdev
->req_wait_q
, &wait
);
216 set_current_state(TASK_RUNNING
);
218 /* ENODATA means the HCI request command queue is empty.
219 * This can happen when a request with conditionals doesn't
220 * trigger any commands to be sent. This is normal behavior
221 * and should not trigger an error return.
229 schedule_timeout(timeout
);
231 remove_wait_queue(&hdev
->req_wait_q
, &wait
);
233 if (signal_pending(current
))
236 switch (hdev
->req_status
) {
238 err
= -bt_to_errno(hdev
->req_result
);
240 *hci_status
= hdev
->req_result
;
243 case HCI_REQ_CANCELED
:
244 err
= -hdev
->req_result
;
246 *hci_status
= HCI_ERROR_UNSPECIFIED
;
252 *hci_status
= HCI_ERROR_UNSPECIFIED
;
256 hdev
->req_status
= hdev
->req_result
= 0;
258 BT_DBG("%s end: err %d", hdev
->name
, err
);
263 int hci_req_sync(struct hci_dev
*hdev
, int (*req
)(struct hci_request
*req
,
265 unsigned long opt
, u32 timeout
, u8
*hci_status
)
269 if (!test_bit(HCI_UP
, &hdev
->flags
))
272 /* Serialize all requests */
273 hci_req_sync_lock(hdev
);
274 ret
= __hci_req_sync(hdev
, req
, opt
, timeout
, hci_status
);
275 hci_req_sync_unlock(hdev
);
280 struct sk_buff
*hci_prepare_cmd(struct hci_dev
*hdev
, u16 opcode
, u32 plen
,
283 int len
= HCI_COMMAND_HDR_SIZE
+ plen
;
284 struct hci_command_hdr
*hdr
;
287 skb
= bt_skb_alloc(len
, GFP_ATOMIC
);
291 hdr
= (struct hci_command_hdr
*) skb_put(skb
, HCI_COMMAND_HDR_SIZE
);
292 hdr
->opcode
= cpu_to_le16(opcode
);
296 memcpy(skb_put(skb
, plen
), param
, plen
);
298 BT_DBG("skb len %d", skb
->len
);
300 hci_skb_pkt_type(skb
) = HCI_COMMAND_PKT
;
301 hci_skb_opcode(skb
) = opcode
;
306 /* Queue a command to an asynchronous HCI request */
307 void hci_req_add_ev(struct hci_request
*req
, u16 opcode
, u32 plen
,
308 const void *param
, u8 event
)
310 struct hci_dev
*hdev
= req
->hdev
;
313 BT_DBG("%s opcode 0x%4.4x plen %d", hdev
->name
, opcode
, plen
);
315 /* If an error occurred during request building, there is no point in
316 * queueing the HCI command. We can simply return.
321 skb
= hci_prepare_cmd(hdev
, opcode
, plen
, param
);
323 BT_ERR("%s no memory for command (opcode 0x%4.4x)",
329 if (skb_queue_empty(&req
->cmd_q
))
330 bt_cb(skb
)->hci
.req_flags
|= HCI_REQ_START
;
332 bt_cb(skb
)->hci
.req_event
= event
;
334 skb_queue_tail(&req
->cmd_q
, skb
);
337 void hci_req_add(struct hci_request
*req
, u16 opcode
, u32 plen
,
340 hci_req_add_ev(req
, opcode
, plen
, param
, 0);
343 void hci_req_add_le_scan_disable(struct hci_request
*req
)
345 struct hci_cp_le_set_scan_enable cp
;
347 memset(&cp
, 0, sizeof(cp
));
348 cp
.enable
= LE_SCAN_DISABLE
;
349 hci_req_add(req
, HCI_OP_LE_SET_SCAN_ENABLE
, sizeof(cp
), &cp
);
352 static void add_to_white_list(struct hci_request
*req
,
353 struct hci_conn_params
*params
)
355 struct hci_cp_le_add_to_white_list cp
;
357 cp
.bdaddr_type
= params
->addr_type
;
358 bacpy(&cp
.bdaddr
, ¶ms
->addr
);
360 hci_req_add(req
, HCI_OP_LE_ADD_TO_WHITE_LIST
, sizeof(cp
), &cp
);
363 static u8
update_white_list(struct hci_request
*req
)
365 struct hci_dev
*hdev
= req
->hdev
;
366 struct hci_conn_params
*params
;
367 struct bdaddr_list
*b
;
368 uint8_t white_list_entries
= 0;
370 /* Go through the current white list programmed into the
371 * controller one by one and check if that address is still
372 * in the list of pending connections or list of devices to
373 * report. If not present in either list, then queue the
374 * command to remove it from the controller.
376 list_for_each_entry(b
, &hdev
->le_white_list
, list
) {
377 struct hci_cp_le_del_from_white_list cp
;
379 if (hci_pend_le_action_lookup(&hdev
->pend_le_conns
,
380 &b
->bdaddr
, b
->bdaddr_type
) ||
381 hci_pend_le_action_lookup(&hdev
->pend_le_reports
,
382 &b
->bdaddr
, b
->bdaddr_type
)) {
383 white_list_entries
++;
387 cp
.bdaddr_type
= b
->bdaddr_type
;
388 bacpy(&cp
.bdaddr
, &b
->bdaddr
);
390 hci_req_add(req
, HCI_OP_LE_DEL_FROM_WHITE_LIST
,
394 /* Since all no longer valid white list entries have been
395 * removed, walk through the list of pending connections
396 * and ensure that any new device gets programmed into
399 * If the list of the devices is larger than the list of
400 * available white list entries in the controller, then
401 * just abort and return filer policy value to not use the
404 list_for_each_entry(params
, &hdev
->pend_le_conns
, action
) {
405 if (hci_bdaddr_list_lookup(&hdev
->le_white_list
,
406 ¶ms
->addr
, params
->addr_type
))
409 if (white_list_entries
>= hdev
->le_white_list_size
) {
410 /* Select filter policy to accept all advertising */
414 if (hci_find_irk_by_addr(hdev
, ¶ms
->addr
,
415 params
->addr_type
)) {
416 /* White list can not be used with RPAs */
420 white_list_entries
++;
421 add_to_white_list(req
, params
);
424 /* After adding all new pending connections, walk through
425 * the list of pending reports and also add these to the
426 * white list if there is still space.
428 list_for_each_entry(params
, &hdev
->pend_le_reports
, action
) {
429 if (hci_bdaddr_list_lookup(&hdev
->le_white_list
,
430 ¶ms
->addr
, params
->addr_type
))
433 if (white_list_entries
>= hdev
->le_white_list_size
) {
434 /* Select filter policy to accept all advertising */
438 if (hci_find_irk_by_addr(hdev
, ¶ms
->addr
,
439 params
->addr_type
)) {
440 /* White list can not be used with RPAs */
444 white_list_entries
++;
445 add_to_white_list(req
, params
);
448 /* Select filter policy to use white list */
452 void hci_req_add_le_passive_scan(struct hci_request
*req
)
454 struct hci_cp_le_set_scan_param param_cp
;
455 struct hci_cp_le_set_scan_enable enable_cp
;
456 struct hci_dev
*hdev
= req
->hdev
;
460 /* Set require_privacy to false since no SCAN_REQ are send
461 * during passive scanning. Not using an non-resolvable address
462 * here is important so that peer devices using direct
463 * advertising with our address will be correctly reported
466 if (hci_update_random_address(req
, false, &own_addr_type
))
469 /* Adding or removing entries from the white list must
470 * happen before enabling scanning. The controller does
471 * not allow white list modification while scanning.
473 filter_policy
= update_white_list(req
);
475 /* When the controller is using random resolvable addresses and
476 * with that having LE privacy enabled, then controllers with
477 * Extended Scanner Filter Policies support can now enable support
478 * for handling directed advertising.
480 * So instead of using filter polices 0x00 (no whitelist)
481 * and 0x01 (whitelist enabled) use the new filter policies
482 * 0x02 (no whitelist) and 0x03 (whitelist enabled).
484 if (hci_dev_test_flag(hdev
, HCI_PRIVACY
) &&
485 (hdev
->le_features
[0] & HCI_LE_EXT_SCAN_POLICY
))
486 filter_policy
|= 0x02;
488 memset(¶m_cp
, 0, sizeof(param_cp
));
489 param_cp
.type
= LE_SCAN_PASSIVE
;
490 param_cp
.interval
= cpu_to_le16(hdev
->le_scan_interval
);
491 param_cp
.window
= cpu_to_le16(hdev
->le_scan_window
);
492 param_cp
.own_address_type
= own_addr_type
;
493 param_cp
.filter_policy
= filter_policy
;
494 hci_req_add(req
, HCI_OP_LE_SET_SCAN_PARAM
, sizeof(param_cp
),
497 memset(&enable_cp
, 0, sizeof(enable_cp
));
498 enable_cp
.enable
= LE_SCAN_ENABLE
;
499 enable_cp
.filter_dup
= LE_SCAN_FILTER_DUP_ENABLE
;
500 hci_req_add(req
, HCI_OP_LE_SET_SCAN_ENABLE
, sizeof(enable_cp
),
504 static void set_random_addr(struct hci_request
*req
, bdaddr_t
*rpa
)
506 struct hci_dev
*hdev
= req
->hdev
;
508 /* If we're advertising or initiating an LE connection we can't
509 * go ahead and change the random address at this time. This is
510 * because the eventual initiator address used for the
511 * subsequently created connection will be undefined (some
512 * controllers use the new address and others the one we had
513 * when the operation started).
515 * In this kind of scenario skip the update and let the random
516 * address be updated at the next cycle.
518 if (hci_dev_test_flag(hdev
, HCI_LE_ADV
) ||
519 hci_lookup_le_connect(hdev
)) {
520 BT_DBG("Deferring random address update");
521 hci_dev_set_flag(hdev
, HCI_RPA_EXPIRED
);
525 hci_req_add(req
, HCI_OP_LE_SET_RANDOM_ADDR
, 6, rpa
);
528 int hci_update_random_address(struct hci_request
*req
, bool require_privacy
,
531 struct hci_dev
*hdev
= req
->hdev
;
534 /* If privacy is enabled use a resolvable private address. If
535 * current RPA has expired or there is something else than
536 * the current RPA in use, then generate a new one.
538 if (hci_dev_test_flag(hdev
, HCI_PRIVACY
)) {
541 *own_addr_type
= ADDR_LE_DEV_RANDOM
;
543 if (!hci_dev_test_and_clear_flag(hdev
, HCI_RPA_EXPIRED
) &&
544 !bacmp(&hdev
->random_addr
, &hdev
->rpa
))
547 err
= smp_generate_rpa(hdev
, hdev
->irk
, &hdev
->rpa
);
549 BT_ERR("%s failed to generate new RPA", hdev
->name
);
553 set_random_addr(req
, &hdev
->rpa
);
555 to
= msecs_to_jiffies(hdev
->rpa_timeout
* 1000);
556 queue_delayed_work(hdev
->workqueue
, &hdev
->rpa_expired
, to
);
561 /* In case of required privacy without resolvable private address,
562 * use an non-resolvable private address. This is useful for active
563 * scanning and non-connectable advertising.
565 if (require_privacy
) {
569 /* The non-resolvable private address is generated
570 * from random six bytes with the two most significant
573 get_random_bytes(&nrpa
, 6);
576 /* The non-resolvable private address shall not be
577 * equal to the public address.
579 if (bacmp(&hdev
->bdaddr
, &nrpa
))
583 *own_addr_type
= ADDR_LE_DEV_RANDOM
;
584 set_random_addr(req
, &nrpa
);
588 /* If forcing static address is in use or there is no public
589 * address use the static address as random address (but skip
590 * the HCI command if the current random address is already the
593 * In case BR/EDR has been disabled on a dual-mode controller
594 * and a static address has been configured, then use that
595 * address instead of the public BR/EDR address.
597 if (hci_dev_test_flag(hdev
, HCI_FORCE_STATIC_ADDR
) ||
598 !bacmp(&hdev
->bdaddr
, BDADDR_ANY
) ||
599 (!hci_dev_test_flag(hdev
, HCI_BREDR_ENABLED
) &&
600 bacmp(&hdev
->static_addr
, BDADDR_ANY
))) {
601 *own_addr_type
= ADDR_LE_DEV_RANDOM
;
602 if (bacmp(&hdev
->static_addr
, &hdev
->random_addr
))
603 hci_req_add(req
, HCI_OP_LE_SET_RANDOM_ADDR
, 6,
608 /* Neither privacy nor static address is being used so use a
611 *own_addr_type
= ADDR_LE_DEV_PUBLIC
;
616 static bool disconnected_whitelist_entries(struct hci_dev
*hdev
)
618 struct bdaddr_list
*b
;
620 list_for_each_entry(b
, &hdev
->whitelist
, list
) {
621 struct hci_conn
*conn
;
623 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &b
->bdaddr
);
627 if (conn
->state
!= BT_CONNECTED
&& conn
->state
!= BT_CONFIG
)
634 void __hci_update_page_scan(struct hci_request
*req
)
636 struct hci_dev
*hdev
= req
->hdev
;
639 if (!hci_dev_test_flag(hdev
, HCI_BREDR_ENABLED
))
642 if (!hdev_is_powered(hdev
))
645 if (mgmt_powering_down(hdev
))
648 if (hci_dev_test_flag(hdev
, HCI_CONNECTABLE
) ||
649 disconnected_whitelist_entries(hdev
))
652 scan
= SCAN_DISABLED
;
654 if (test_bit(HCI_PSCAN
, &hdev
->flags
) == !!(scan
& SCAN_PAGE
))
657 if (hci_dev_test_flag(hdev
, HCI_DISCOVERABLE
))
658 scan
|= SCAN_INQUIRY
;
660 hci_req_add(req
, HCI_OP_WRITE_SCAN_ENABLE
, 1, &scan
);
663 void hci_update_page_scan(struct hci_dev
*hdev
)
665 struct hci_request req
;
667 hci_req_init(&req
, hdev
);
668 __hci_update_page_scan(&req
);
669 hci_req_run(&req
, NULL
);
672 /* This function controls the background scanning based on hdev->pend_le_conns
673 * list. If there are pending LE connection we start the background scanning,
674 * otherwise we stop it.
676 * This function requires the caller holds hdev->lock.
678 static void __hci_update_background_scan(struct hci_request
*req
)
680 struct hci_dev
*hdev
= req
->hdev
;
682 if (!test_bit(HCI_UP
, &hdev
->flags
) ||
683 test_bit(HCI_INIT
, &hdev
->flags
) ||
684 hci_dev_test_flag(hdev
, HCI_SETUP
) ||
685 hci_dev_test_flag(hdev
, HCI_CONFIG
) ||
686 hci_dev_test_flag(hdev
, HCI_AUTO_OFF
) ||
687 hci_dev_test_flag(hdev
, HCI_UNREGISTER
))
690 /* No point in doing scanning if LE support hasn't been enabled */
691 if (!hci_dev_test_flag(hdev
, HCI_LE_ENABLED
))
694 /* If discovery is active don't interfere with it */
695 if (hdev
->discovery
.state
!= DISCOVERY_STOPPED
)
698 /* Reset RSSI and UUID filters when starting background scanning
699 * since these filters are meant for service discovery only.
701 * The Start Discovery and Start Service Discovery operations
702 * ensure to set proper values for RSSI threshold and UUID
703 * filter list. So it is safe to just reset them here.
705 hci_discovery_filter_clear(hdev
);
707 if (list_empty(&hdev
->pend_le_conns
) &&
708 list_empty(&hdev
->pend_le_reports
)) {
709 /* If there is no pending LE connections or devices
710 * to be scanned for, we should stop the background
714 /* If controller is not scanning we are done. */
715 if (!hci_dev_test_flag(hdev
, HCI_LE_SCAN
))
718 hci_req_add_le_scan_disable(req
);
720 BT_DBG("%s stopping background scanning", hdev
->name
);
722 /* If there is at least one pending LE connection, we should
723 * keep the background scan running.
726 /* If controller is connecting, we should not start scanning
727 * since some controllers are not able to scan and connect at
730 if (hci_lookup_le_connect(hdev
))
733 /* If controller is currently scanning, we stop it to ensure we
734 * don't miss any advertising (due to duplicates filter).
736 if (hci_dev_test_flag(hdev
, HCI_LE_SCAN
))
737 hci_req_add_le_scan_disable(req
);
739 hci_req_add_le_passive_scan(req
);
741 BT_DBG("%s starting background scanning", hdev
->name
);
745 void __hci_abort_conn(struct hci_request
*req
, struct hci_conn
*conn
,
748 switch (conn
->state
) {
751 if (conn
->type
== AMP_LINK
) {
752 struct hci_cp_disconn_phy_link cp
;
754 cp
.phy_handle
= HCI_PHY_HANDLE(conn
->handle
);
756 hci_req_add(req
, HCI_OP_DISCONN_PHY_LINK
, sizeof(cp
),
759 struct hci_cp_disconnect dc
;
761 dc
.handle
= cpu_to_le16(conn
->handle
);
763 hci_req_add(req
, HCI_OP_DISCONNECT
, sizeof(dc
), &dc
);
766 conn
->state
= BT_DISCONN
;
770 if (conn
->type
== LE_LINK
) {
771 if (test_bit(HCI_CONN_SCANNING
, &conn
->flags
))
773 hci_req_add(req
, HCI_OP_LE_CREATE_CONN_CANCEL
,
775 } else if (conn
->type
== ACL_LINK
) {
776 if (req
->hdev
->hci_ver
< BLUETOOTH_VER_1_2
)
778 hci_req_add(req
, HCI_OP_CREATE_CONN_CANCEL
,
783 if (conn
->type
== ACL_LINK
) {
784 struct hci_cp_reject_conn_req rej
;
786 bacpy(&rej
.bdaddr
, &conn
->dst
);
789 hci_req_add(req
, HCI_OP_REJECT_CONN_REQ
,
791 } else if (conn
->type
== SCO_LINK
|| conn
->type
== ESCO_LINK
) {
792 struct hci_cp_reject_sync_conn_req rej
;
794 bacpy(&rej
.bdaddr
, &conn
->dst
);
796 /* SCO rejection has its own limited set of
797 * allowed error values (0x0D-0x0F) which isn't
798 * compatible with most values passed to this
799 * function. To be safe hard-code one of the
800 * values that's suitable for SCO.
802 rej
.reason
= HCI_ERROR_REMOTE_LOW_RESOURCES
;
804 hci_req_add(req
, HCI_OP_REJECT_SYNC_CONN_REQ
,
809 conn
->state
= BT_CLOSED
;
814 static void abort_conn_complete(struct hci_dev
*hdev
, u8 status
, u16 opcode
)
817 BT_DBG("Failed to abort connection: status 0x%2.2x", status
);
820 int hci_abort_conn(struct hci_conn
*conn
, u8 reason
)
822 struct hci_request req
;
825 hci_req_init(&req
, conn
->hdev
);
827 __hci_abort_conn(&req
, conn
, reason
);
829 err
= hci_req_run(&req
, abort_conn_complete
);
830 if (err
&& err
!= -ENODATA
) {
831 BT_ERR("Failed to run HCI request: err %d", err
);
838 static int update_bg_scan(struct hci_request
*req
, unsigned long opt
)
840 hci_dev_lock(req
->hdev
);
841 __hci_update_background_scan(req
);
842 hci_dev_unlock(req
->hdev
);
846 static void bg_scan_update(struct work_struct
*work
)
848 struct hci_dev
*hdev
= container_of(work
, struct hci_dev
,
850 struct hci_conn
*conn
;
854 err
= hci_req_sync(hdev
, update_bg_scan
, 0, HCI_CMD_TIMEOUT
, &status
);
860 conn
= hci_conn_hash_lookup_state(hdev
, LE_LINK
, BT_CONNECT
);
862 hci_le_conn_failed(conn
, status
);
864 hci_dev_unlock(hdev
);
867 static int le_scan_disable(struct hci_request
*req
, unsigned long opt
)
869 hci_req_add_le_scan_disable(req
);
873 static int bredr_inquiry(struct hci_request
*req
, unsigned long opt
)
876 /* General inquiry access code (GIAC) */
877 u8 lap
[3] = { 0x33, 0x8b, 0x9e };
878 struct hci_cp_inquiry cp
;
880 BT_DBG("%s", req
->hdev
->name
);
882 hci_dev_lock(req
->hdev
);
883 hci_inquiry_cache_flush(req
->hdev
);
884 hci_dev_unlock(req
->hdev
);
886 memset(&cp
, 0, sizeof(cp
));
887 memcpy(&cp
.lap
, lap
, sizeof(cp
.lap
));
890 hci_req_add(req
, HCI_OP_INQUIRY
, sizeof(cp
), &cp
);
895 static void le_scan_disable_work(struct work_struct
*work
)
897 struct hci_dev
*hdev
= container_of(work
, struct hci_dev
,
898 le_scan_disable
.work
);
901 BT_DBG("%s", hdev
->name
);
903 if (!hci_dev_test_flag(hdev
, HCI_LE_SCAN
))
906 cancel_delayed_work(&hdev
->le_scan_restart
);
908 hci_req_sync(hdev
, le_scan_disable
, 0, HCI_CMD_TIMEOUT
, &status
);
910 BT_ERR("Failed to disable LE scan: status 0x%02x", status
);
914 hdev
->discovery
.scan_start
= 0;
916 /* If we were running LE only scan, change discovery state. If
917 * we were running both LE and BR/EDR inquiry simultaneously,
918 * and BR/EDR inquiry is already finished, stop discovery,
919 * otherwise BR/EDR inquiry will stop discovery when finished.
920 * If we will resolve remote device name, do not change
924 if (hdev
->discovery
.type
== DISCOV_TYPE_LE
)
927 if (hdev
->discovery
.type
!= DISCOV_TYPE_INTERLEAVED
)
930 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY
, &hdev
->quirks
)) {
931 if (!test_bit(HCI_INQUIRY
, &hdev
->flags
) &&
932 hdev
->discovery
.state
!= DISCOVERY_RESOLVING
)
938 hci_req_sync(hdev
, bredr_inquiry
, DISCOV_INTERLEAVED_INQUIRY_LEN
,
939 HCI_CMD_TIMEOUT
, &status
);
941 BT_ERR("Inquiry failed: status 0x%02x", status
);
949 hci_discovery_set_state(hdev
, DISCOVERY_STOPPED
);
950 hci_dev_unlock(hdev
);
953 static int le_scan_restart(struct hci_request
*req
, unsigned long opt
)
955 struct hci_dev
*hdev
= req
->hdev
;
956 struct hci_cp_le_set_scan_enable cp
;
958 /* If controller is not scanning we are done. */
959 if (!hci_dev_test_flag(hdev
, HCI_LE_SCAN
))
962 hci_req_add_le_scan_disable(req
);
964 memset(&cp
, 0, sizeof(cp
));
965 cp
.enable
= LE_SCAN_ENABLE
;
966 cp
.filter_dup
= LE_SCAN_FILTER_DUP_ENABLE
;
967 hci_req_add(req
, HCI_OP_LE_SET_SCAN_ENABLE
, sizeof(cp
), &cp
);
972 static void le_scan_restart_work(struct work_struct
*work
)
974 struct hci_dev
*hdev
= container_of(work
, struct hci_dev
,
975 le_scan_restart
.work
);
976 unsigned long timeout
, duration
, scan_start
, now
;
979 BT_DBG("%s", hdev
->name
);
981 hci_req_sync(hdev
, le_scan_restart
, 0, HCI_CMD_TIMEOUT
, &status
);
983 BT_ERR("Failed to restart LE scan: status %d", status
);
989 if (!test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER
, &hdev
->quirks
) ||
990 !hdev
->discovery
.scan_start
)
993 /* When the scan was started, hdev->le_scan_disable has been queued
994 * after duration from scan_start. During scan restart this job
995 * has been canceled, and we need to queue it again after proper
996 * timeout, to make sure that scan does not run indefinitely.
998 duration
= hdev
->discovery
.scan_duration
;
999 scan_start
= hdev
->discovery
.scan_start
;
1001 if (now
- scan_start
<= duration
) {
1004 if (now
>= scan_start
)
1005 elapsed
= now
- scan_start
;
1007 elapsed
= ULONG_MAX
- scan_start
+ now
;
1009 timeout
= duration
- elapsed
;
1014 queue_delayed_work(hdev
->req_workqueue
,
1015 &hdev
->le_scan_disable
, timeout
);
1018 hci_dev_unlock(hdev
);
1021 static void cancel_adv_timeout(struct hci_dev
*hdev
)
1023 if (hdev
->adv_instance_timeout
) {
1024 hdev
->adv_instance_timeout
= 0;
1025 cancel_delayed_work(&hdev
->adv_instance_expire
);
1029 static void disable_advertising(struct hci_request
*req
)
1033 hci_req_add(req
, HCI_OP_LE_SET_ADV_ENABLE
, sizeof(enable
), &enable
);
1036 static int active_scan(struct hci_request
*req
, unsigned long opt
)
1038 uint16_t interval
= opt
;
1039 struct hci_dev
*hdev
= req
->hdev
;
1040 struct hci_cp_le_set_scan_param param_cp
;
1041 struct hci_cp_le_set_scan_enable enable_cp
;
1045 BT_DBG("%s", hdev
->name
);
1047 if (hci_dev_test_flag(hdev
, HCI_LE_ADV
)) {
1050 /* Don't let discovery abort an outgoing connection attempt
1051 * that's using directed advertising.
1053 if (hci_lookup_le_connect(hdev
)) {
1054 hci_dev_unlock(hdev
);
1058 cancel_adv_timeout(hdev
);
1059 hci_dev_unlock(hdev
);
1061 disable_advertising(req
);
1064 /* If controller is scanning, it means the background scanning is
1065 * running. Thus, we should temporarily stop it in order to set the
1066 * discovery scanning parameters.
1068 if (hci_dev_test_flag(hdev
, HCI_LE_SCAN
))
1069 hci_req_add_le_scan_disable(req
);
1071 /* All active scans will be done with either a resolvable private
1072 * address (when privacy feature has been enabled) or non-resolvable
1075 err
= hci_update_random_address(req
, true, &own_addr_type
);
1077 own_addr_type
= ADDR_LE_DEV_PUBLIC
;
1079 memset(¶m_cp
, 0, sizeof(param_cp
));
1080 param_cp
.type
= LE_SCAN_ACTIVE
;
1081 param_cp
.interval
= cpu_to_le16(interval
);
1082 param_cp
.window
= cpu_to_le16(DISCOV_LE_SCAN_WIN
);
1083 param_cp
.own_address_type
= own_addr_type
;
1085 hci_req_add(req
, HCI_OP_LE_SET_SCAN_PARAM
, sizeof(param_cp
),
1088 memset(&enable_cp
, 0, sizeof(enable_cp
));
1089 enable_cp
.enable
= LE_SCAN_ENABLE
;
1090 enable_cp
.filter_dup
= LE_SCAN_FILTER_DUP_ENABLE
;
1092 hci_req_add(req
, HCI_OP_LE_SET_SCAN_ENABLE
, sizeof(enable_cp
),
1098 static int interleaved_discov(struct hci_request
*req
, unsigned long opt
)
1102 BT_DBG("%s", req
->hdev
->name
);
1104 err
= active_scan(req
, opt
);
1108 return bredr_inquiry(req
, DISCOV_BREDR_INQUIRY_LEN
);
1111 static void start_discovery(struct hci_dev
*hdev
, u8
*status
)
1113 unsigned long timeout
;
1115 BT_DBG("%s type %u", hdev
->name
, hdev
->discovery
.type
);
1117 switch (hdev
->discovery
.type
) {
1118 case DISCOV_TYPE_BREDR
:
1119 if (!hci_dev_test_flag(hdev
, HCI_INQUIRY
))
1120 hci_req_sync(hdev
, bredr_inquiry
,
1121 DISCOV_BREDR_INQUIRY_LEN
, HCI_CMD_TIMEOUT
,
1124 case DISCOV_TYPE_INTERLEAVED
:
1125 /* When running simultaneous discovery, the LE scanning time
1126 * should occupy the whole discovery time sine BR/EDR inquiry
1127 * and LE scanning are scheduled by the controller.
1129 * For interleaving discovery in comparison, BR/EDR inquiry
1130 * and LE scanning are done sequentially with separate
1133 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY
,
1135 timeout
= msecs_to_jiffies(DISCOV_LE_TIMEOUT
);
1136 /* During simultaneous discovery, we double LE scan
1137 * interval. We must leave some time for the controller
1138 * to do BR/EDR inquiry.
1140 hci_req_sync(hdev
, interleaved_discov
,
1141 DISCOV_LE_SCAN_INT
* 2, HCI_CMD_TIMEOUT
,
1146 timeout
= msecs_to_jiffies(hdev
->discov_interleaved_timeout
);
1147 hci_req_sync(hdev
, active_scan
, DISCOV_LE_SCAN_INT
,
1148 HCI_CMD_TIMEOUT
, status
);
1150 case DISCOV_TYPE_LE
:
1151 timeout
= msecs_to_jiffies(DISCOV_LE_TIMEOUT
);
1152 hci_req_sync(hdev
, active_scan
, DISCOV_LE_SCAN_INT
,
1153 HCI_CMD_TIMEOUT
, status
);
1156 *status
= HCI_ERROR_UNSPECIFIED
;
1163 BT_DBG("%s timeout %u ms", hdev
->name
, jiffies_to_msecs(timeout
));
1165 /* When service discovery is used and the controller has a
1166 * strict duplicate filter, it is important to remember the
1167 * start and duration of the scan. This is required for
1168 * restarting scanning during the discovery phase.
1170 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER
, &hdev
->quirks
) &&
1171 hdev
->discovery
.result_filtering
) {
1172 hdev
->discovery
.scan_start
= jiffies
;
1173 hdev
->discovery
.scan_duration
= timeout
;
1176 queue_delayed_work(hdev
->req_workqueue
, &hdev
->le_scan_disable
,
1180 bool hci_req_stop_discovery(struct hci_request
*req
)
1182 struct hci_dev
*hdev
= req
->hdev
;
1183 struct discovery_state
*d
= &hdev
->discovery
;
1184 struct hci_cp_remote_name_req_cancel cp
;
1185 struct inquiry_entry
*e
;
1188 BT_DBG("%s state %u", hdev
->name
, hdev
->discovery
.state
);
1190 if (d
->state
== DISCOVERY_FINDING
|| d
->state
== DISCOVERY_STOPPING
) {
1191 if (test_bit(HCI_INQUIRY
, &hdev
->flags
))
1192 hci_req_add(req
, HCI_OP_INQUIRY_CANCEL
, 0, NULL
);
1194 if (hci_dev_test_flag(hdev
, HCI_LE_SCAN
)) {
1195 cancel_delayed_work(&hdev
->le_scan_disable
);
1196 hci_req_add_le_scan_disable(req
);
1201 /* Passive scanning */
1202 if (hci_dev_test_flag(hdev
, HCI_LE_SCAN
)) {
1203 hci_req_add_le_scan_disable(req
);
1208 /* No further actions needed for LE-only discovery */
1209 if (d
->type
== DISCOV_TYPE_LE
)
1212 if (d
->state
== DISCOVERY_RESOLVING
|| d
->state
== DISCOVERY_STOPPING
) {
1213 e
= hci_inquiry_cache_lookup_resolve(hdev
, BDADDR_ANY
,
1218 bacpy(&cp
.bdaddr
, &e
->data
.bdaddr
);
1219 hci_req_add(req
, HCI_OP_REMOTE_NAME_REQ_CANCEL
, sizeof(cp
),
1227 static int stop_discovery(struct hci_request
*req
, unsigned long opt
)
1229 hci_dev_lock(req
->hdev
);
1230 hci_req_stop_discovery(req
);
1231 hci_dev_unlock(req
->hdev
);
1236 static void discov_update(struct work_struct
*work
)
1238 struct hci_dev
*hdev
= container_of(work
, struct hci_dev
,
1242 switch (hdev
->discovery
.state
) {
1243 case DISCOVERY_STARTING
:
1244 start_discovery(hdev
, &status
);
1245 mgmt_start_discovery_complete(hdev
, status
);
1247 hci_discovery_set_state(hdev
, DISCOVERY_STOPPED
);
1249 hci_discovery_set_state(hdev
, DISCOVERY_FINDING
);
1251 case DISCOVERY_STOPPING
:
1252 hci_req_sync(hdev
, stop_discovery
, 0, HCI_CMD_TIMEOUT
, &status
);
1253 mgmt_stop_discovery_complete(hdev
, status
);
1255 hci_discovery_set_state(hdev
, DISCOVERY_STOPPED
);
1257 case DISCOVERY_STOPPED
:
1263 void hci_request_setup(struct hci_dev
*hdev
)
1265 INIT_WORK(&hdev
->discov_update
, discov_update
);
1266 INIT_WORK(&hdev
->bg_scan_update
, bg_scan_update
);
1267 INIT_DELAYED_WORK(&hdev
->le_scan_disable
, le_scan_disable_work
);
1268 INIT_DELAYED_WORK(&hdev
->le_scan_restart
, le_scan_restart_work
);
1271 void hci_request_cancel_all(struct hci_dev
*hdev
)
1273 hci_req_sync_cancel(hdev
, ENODEV
);
1275 cancel_work_sync(&hdev
->discov_update
);
1276 cancel_work_sync(&hdev
->bg_scan_update
);
1277 cancel_delayed_work_sync(&hdev
->le_scan_disable
);
1278 cancel_delayed_work_sync(&hdev
->le_scan_restart
);