2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated
4 Copyright (C) 2011 ProFUSION Embedded Systems
6 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation;
12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
13 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
15 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
16 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
17 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
22 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
23 SOFTWARE IS DISCLAIMED.
26 /* Bluetooth HCI core. */
28 #include <linux/export.h>
29 #include <linux/idr.h>
30 #include <linux/rfkill.h>
31 #include <linux/debugfs.h>
32 #include <linux/crypto.h>
33 #include <linux/property.h>
34 #include <linux/suspend.h>
35 #include <linux/wait.h>
36 #include <asm/unaligned.h>
38 #include <net/bluetooth/bluetooth.h>
39 #include <net/bluetooth/hci_core.h>
40 #include <net/bluetooth/l2cap.h>
41 #include <net/bluetooth/mgmt.h>
43 #include "hci_request.h"
44 #include "hci_debugfs.h"
48 static void hci_rx_work(struct work_struct
*work
);
49 static void hci_cmd_work(struct work_struct
*work
);
50 static void hci_tx_work(struct work_struct
*work
);
53 LIST_HEAD(hci_dev_list
);
54 DEFINE_RWLOCK(hci_dev_list_lock
);
56 /* HCI callback list */
57 LIST_HEAD(hci_cb_list
);
58 DEFINE_MUTEX(hci_cb_list_lock
);
60 /* HCI ID Numbering */
61 static DEFINE_IDA(hci_index_ida
);
63 /* ---- HCI debugfs entries ---- */
65 static ssize_t
dut_mode_read(struct file
*file
, char __user
*user_buf
,
66 size_t count
, loff_t
*ppos
)
68 struct hci_dev
*hdev
= file
->private_data
;
71 buf
[0] = hci_dev_test_flag(hdev
, HCI_DUT_MODE
) ? 'Y' : 'N';
74 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, 2);
77 static ssize_t
dut_mode_write(struct file
*file
, const char __user
*user_buf
,
78 size_t count
, loff_t
*ppos
)
80 struct hci_dev
*hdev
= file
->private_data
;
85 if (!test_bit(HCI_UP
, &hdev
->flags
))
88 err
= kstrtobool_from_user(user_buf
, count
, &enable
);
92 if (enable
== hci_dev_test_flag(hdev
, HCI_DUT_MODE
))
95 hci_req_sync_lock(hdev
);
97 skb
= __hci_cmd_sync(hdev
, HCI_OP_ENABLE_DUT_MODE
, 0, NULL
,
100 skb
= __hci_cmd_sync(hdev
, HCI_OP_RESET
, 0, NULL
,
102 hci_req_sync_unlock(hdev
);
109 hci_dev_change_flag(hdev
, HCI_DUT_MODE
);
114 static const struct file_operations dut_mode_fops
= {
116 .read
= dut_mode_read
,
117 .write
= dut_mode_write
,
118 .llseek
= default_llseek
,
121 static ssize_t
vendor_diag_read(struct file
*file
, char __user
*user_buf
,
122 size_t count
, loff_t
*ppos
)
124 struct hci_dev
*hdev
= file
->private_data
;
127 buf
[0] = hci_dev_test_flag(hdev
, HCI_VENDOR_DIAG
) ? 'Y' : 'N';
130 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, 2);
133 static ssize_t
vendor_diag_write(struct file
*file
, const char __user
*user_buf
,
134 size_t count
, loff_t
*ppos
)
136 struct hci_dev
*hdev
= file
->private_data
;
140 err
= kstrtobool_from_user(user_buf
, count
, &enable
);
144 /* When the diagnostic flags are not persistent and the transport
145 * is not active or in user channel operation, then there is no need
146 * for the vendor callback. Instead just store the desired value and
147 * the setting will be programmed when the controller gets powered on.
149 if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG
, &hdev
->quirks
) &&
150 (!test_bit(HCI_RUNNING
, &hdev
->flags
) ||
151 hci_dev_test_flag(hdev
, HCI_USER_CHANNEL
)))
154 hci_req_sync_lock(hdev
);
155 err
= hdev
->set_diag(hdev
, enable
);
156 hci_req_sync_unlock(hdev
);
163 hci_dev_set_flag(hdev
, HCI_VENDOR_DIAG
);
165 hci_dev_clear_flag(hdev
, HCI_VENDOR_DIAG
);
170 static const struct file_operations vendor_diag_fops
= {
172 .read
= vendor_diag_read
,
173 .write
= vendor_diag_write
,
174 .llseek
= default_llseek
,
177 static void hci_debugfs_create_basic(struct hci_dev
*hdev
)
179 debugfs_create_file("dut_mode", 0644, hdev
->debugfs
, hdev
,
183 debugfs_create_file("vendor_diag", 0644, hdev
->debugfs
, hdev
,
187 static int hci_reset_req(struct hci_request
*req
, unsigned long opt
)
189 BT_DBG("%s %ld", req
->hdev
->name
, opt
);
192 set_bit(HCI_RESET
, &req
->hdev
->flags
);
193 hci_req_add(req
, HCI_OP_RESET
, 0, NULL
);
197 static void bredr_init(struct hci_request
*req
)
199 req
->hdev
->flow_ctl_mode
= HCI_FLOW_CTL_MODE_PACKET_BASED
;
201 /* Read Local Supported Features */
202 hci_req_add(req
, HCI_OP_READ_LOCAL_FEATURES
, 0, NULL
);
204 /* Read Local Version */
205 hci_req_add(req
, HCI_OP_READ_LOCAL_VERSION
, 0, NULL
);
207 /* Read BD Address */
208 hci_req_add(req
, HCI_OP_READ_BD_ADDR
, 0, NULL
);
211 static void amp_init1(struct hci_request
*req
)
213 req
->hdev
->flow_ctl_mode
= HCI_FLOW_CTL_MODE_BLOCK_BASED
;
215 /* Read Local Version */
216 hci_req_add(req
, HCI_OP_READ_LOCAL_VERSION
, 0, NULL
);
218 /* Read Local Supported Commands */
219 hci_req_add(req
, HCI_OP_READ_LOCAL_COMMANDS
, 0, NULL
);
221 /* Read Local AMP Info */
222 hci_req_add(req
, HCI_OP_READ_LOCAL_AMP_INFO
, 0, NULL
);
224 /* Read Data Blk size */
225 hci_req_add(req
, HCI_OP_READ_DATA_BLOCK_SIZE
, 0, NULL
);
227 /* Read Flow Control Mode */
228 hci_req_add(req
, HCI_OP_READ_FLOW_CONTROL_MODE
, 0, NULL
);
230 /* Read Location Data */
231 hci_req_add(req
, HCI_OP_READ_LOCATION_DATA
, 0, NULL
);
234 static int amp_init2(struct hci_request
*req
)
236 /* Read Local Supported Features. Not all AMP controllers
237 * support this so it's placed conditionally in the second
240 if (req
->hdev
->commands
[14] & 0x20)
241 hci_req_add(req
, HCI_OP_READ_LOCAL_FEATURES
, 0, NULL
);
246 static int hci_init1_req(struct hci_request
*req
, unsigned long opt
)
248 struct hci_dev
*hdev
= req
->hdev
;
250 BT_DBG("%s %ld", hdev
->name
, opt
);
253 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE
, &hdev
->quirks
))
254 hci_reset_req(req
, 0);
256 switch (hdev
->dev_type
) {
264 bt_dev_err(hdev
, "Unknown device type %d", hdev
->dev_type
);
271 static void bredr_setup(struct hci_request
*req
)
276 /* Read Buffer Size (ACL mtu, max pkt, etc.) */
277 hci_req_add(req
, HCI_OP_READ_BUFFER_SIZE
, 0, NULL
);
279 /* Read Class of Device */
280 hci_req_add(req
, HCI_OP_READ_CLASS_OF_DEV
, 0, NULL
);
282 /* Read Local Name */
283 hci_req_add(req
, HCI_OP_READ_LOCAL_NAME
, 0, NULL
);
285 /* Read Voice Setting */
286 hci_req_add(req
, HCI_OP_READ_VOICE_SETTING
, 0, NULL
);
288 /* Read Number of Supported IAC */
289 hci_req_add(req
, HCI_OP_READ_NUM_SUPPORTED_IAC
, 0, NULL
);
291 /* Read Current IAC LAP */
292 hci_req_add(req
, HCI_OP_READ_CURRENT_IAC_LAP
, 0, NULL
);
294 /* Clear Event Filters */
295 flt_type
= HCI_FLT_CLEAR_ALL
;
296 hci_req_add(req
, HCI_OP_SET_EVENT_FLT
, 1, &flt_type
);
298 /* Connection accept timeout ~20 secs */
299 param
= cpu_to_le16(0x7d00);
300 hci_req_add(req
, HCI_OP_WRITE_CA_TIMEOUT
, 2, ¶m
);
303 static void le_setup(struct hci_request
*req
)
305 struct hci_dev
*hdev
= req
->hdev
;
307 /* Read LE Buffer Size */
308 hci_req_add(req
, HCI_OP_LE_READ_BUFFER_SIZE
, 0, NULL
);
310 /* Read LE Local Supported Features */
311 hci_req_add(req
, HCI_OP_LE_READ_LOCAL_FEATURES
, 0, NULL
);
313 /* Read LE Supported States */
314 hci_req_add(req
, HCI_OP_LE_READ_SUPPORTED_STATES
, 0, NULL
);
316 /* LE-only controllers have LE implicitly enabled */
317 if (!lmp_bredr_capable(hdev
))
318 hci_dev_set_flag(hdev
, HCI_LE_ENABLED
);
321 static void hci_setup_event_mask(struct hci_request
*req
)
323 struct hci_dev
*hdev
= req
->hdev
;
325 /* The second byte is 0xff instead of 0x9f (two reserved bits
326 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
329 u8 events
[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
331 /* CSR 1.1 dongles does not accept any bitfield so don't try to set
332 * any event mask for pre 1.2 devices.
334 if (hdev
->hci_ver
< BLUETOOTH_VER_1_2
)
337 if (lmp_bredr_capable(hdev
)) {
338 events
[4] |= 0x01; /* Flow Specification Complete */
340 /* Use a different default for LE-only devices */
341 memset(events
, 0, sizeof(events
));
342 events
[1] |= 0x20; /* Command Complete */
343 events
[1] |= 0x40; /* Command Status */
344 events
[1] |= 0x80; /* Hardware Error */
346 /* If the controller supports the Disconnect command, enable
347 * the corresponding event. In addition enable packet flow
348 * control related events.
350 if (hdev
->commands
[0] & 0x20) {
351 events
[0] |= 0x10; /* Disconnection Complete */
352 events
[2] |= 0x04; /* Number of Completed Packets */
353 events
[3] |= 0x02; /* Data Buffer Overflow */
356 /* If the controller supports the Read Remote Version
357 * Information command, enable the corresponding event.
359 if (hdev
->commands
[2] & 0x80)
360 events
[1] |= 0x08; /* Read Remote Version Information
364 if (hdev
->le_features
[0] & HCI_LE_ENCRYPTION
) {
365 events
[0] |= 0x80; /* Encryption Change */
366 events
[5] |= 0x80; /* Encryption Key Refresh Complete */
370 if (lmp_inq_rssi_capable(hdev
) ||
371 test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE
, &hdev
->quirks
))
372 events
[4] |= 0x02; /* Inquiry Result with RSSI */
374 if (lmp_ext_feat_capable(hdev
))
375 events
[4] |= 0x04; /* Read Remote Extended Features Complete */
377 if (lmp_esco_capable(hdev
)) {
378 events
[5] |= 0x08; /* Synchronous Connection Complete */
379 events
[5] |= 0x10; /* Synchronous Connection Changed */
382 if (lmp_sniffsubr_capable(hdev
))
383 events
[5] |= 0x20; /* Sniff Subrating */
385 if (lmp_pause_enc_capable(hdev
))
386 events
[5] |= 0x80; /* Encryption Key Refresh Complete */
388 if (lmp_ext_inq_capable(hdev
))
389 events
[5] |= 0x40; /* Extended Inquiry Result */
391 if (lmp_no_flush_capable(hdev
))
392 events
[7] |= 0x01; /* Enhanced Flush Complete */
394 if (lmp_lsto_capable(hdev
))
395 events
[6] |= 0x80; /* Link Supervision Timeout Changed */
397 if (lmp_ssp_capable(hdev
)) {
398 events
[6] |= 0x01; /* IO Capability Request */
399 events
[6] |= 0x02; /* IO Capability Response */
400 events
[6] |= 0x04; /* User Confirmation Request */
401 events
[6] |= 0x08; /* User Passkey Request */
402 events
[6] |= 0x10; /* Remote OOB Data Request */
403 events
[6] |= 0x20; /* Simple Pairing Complete */
404 events
[7] |= 0x04; /* User Passkey Notification */
405 events
[7] |= 0x08; /* Keypress Notification */
406 events
[7] |= 0x10; /* Remote Host Supported
407 * Features Notification
411 if (lmp_le_capable(hdev
))
412 events
[7] |= 0x20; /* LE Meta-Event */
414 hci_req_add(req
, HCI_OP_SET_EVENT_MASK
, sizeof(events
), events
);
417 static int hci_init2_req(struct hci_request
*req
, unsigned long opt
)
419 struct hci_dev
*hdev
= req
->hdev
;
421 if (hdev
->dev_type
== HCI_AMP
)
422 return amp_init2(req
);
424 if (lmp_bredr_capable(hdev
))
427 hci_dev_clear_flag(hdev
, HCI_BREDR_ENABLED
);
429 if (lmp_le_capable(hdev
))
432 /* All Bluetooth 1.2 and later controllers should support the
433 * HCI command for reading the local supported commands.
435 * Unfortunately some controllers indicate Bluetooth 1.2 support,
436 * but do not have support for this command. If that is the case,
437 * the driver can quirk the behavior and skip reading the local
438 * supported commands.
440 if (hdev
->hci_ver
> BLUETOOTH_VER_1_1
&&
441 !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS
, &hdev
->quirks
))
442 hci_req_add(req
, HCI_OP_READ_LOCAL_COMMANDS
, 0, NULL
);
444 if (lmp_ssp_capable(hdev
)) {
445 /* When SSP is available, then the host features page
446 * should also be available as well. However some
447 * controllers list the max_page as 0 as long as SSP
448 * has not been enabled. To achieve proper debugging
449 * output, force the minimum max_page to 1 at least.
451 hdev
->max_page
= 0x01;
453 if (hci_dev_test_flag(hdev
, HCI_SSP_ENABLED
)) {
456 hci_req_add(req
, HCI_OP_WRITE_SSP_MODE
,
457 sizeof(mode
), &mode
);
459 struct hci_cp_write_eir cp
;
461 memset(hdev
->eir
, 0, sizeof(hdev
->eir
));
462 memset(&cp
, 0, sizeof(cp
));
464 hci_req_add(req
, HCI_OP_WRITE_EIR
, sizeof(cp
), &cp
);
468 if (lmp_inq_rssi_capable(hdev
) ||
469 test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE
, &hdev
->quirks
)) {
472 /* If Extended Inquiry Result events are supported, then
473 * they are clearly preferred over Inquiry Result with RSSI
476 mode
= lmp_ext_inq_capable(hdev
) ? 0x02 : 0x01;
478 hci_req_add(req
, HCI_OP_WRITE_INQUIRY_MODE
, 1, &mode
);
481 if (lmp_inq_tx_pwr_capable(hdev
))
482 hci_req_add(req
, HCI_OP_READ_INQ_RSP_TX_POWER
, 0, NULL
);
484 if (lmp_ext_feat_capable(hdev
)) {
485 struct hci_cp_read_local_ext_features cp
;
488 hci_req_add(req
, HCI_OP_READ_LOCAL_EXT_FEATURES
,
492 if (hci_dev_test_flag(hdev
, HCI_LINK_SECURITY
)) {
494 hci_req_add(req
, HCI_OP_WRITE_AUTH_ENABLE
, sizeof(enable
),
501 static void hci_setup_link_policy(struct hci_request
*req
)
503 struct hci_dev
*hdev
= req
->hdev
;
504 struct hci_cp_write_def_link_policy cp
;
507 if (lmp_rswitch_capable(hdev
))
508 link_policy
|= HCI_LP_RSWITCH
;
509 if (lmp_hold_capable(hdev
))
510 link_policy
|= HCI_LP_HOLD
;
511 if (lmp_sniff_capable(hdev
))
512 link_policy
|= HCI_LP_SNIFF
;
513 if (lmp_park_capable(hdev
))
514 link_policy
|= HCI_LP_PARK
;
516 cp
.policy
= cpu_to_le16(link_policy
);
517 hci_req_add(req
, HCI_OP_WRITE_DEF_LINK_POLICY
, sizeof(cp
), &cp
);
520 static void hci_set_le_support(struct hci_request
*req
)
522 struct hci_dev
*hdev
= req
->hdev
;
523 struct hci_cp_write_le_host_supported cp
;
525 /* LE-only devices do not support explicit enablement */
526 if (!lmp_bredr_capable(hdev
))
529 memset(&cp
, 0, sizeof(cp
));
531 if (hci_dev_test_flag(hdev
, HCI_LE_ENABLED
)) {
536 if (cp
.le
!= lmp_host_le_capable(hdev
))
537 hci_req_add(req
, HCI_OP_WRITE_LE_HOST_SUPPORTED
, sizeof(cp
),
541 static void hci_set_event_mask_page_2(struct hci_request
*req
)
543 struct hci_dev
*hdev
= req
->hdev
;
544 u8 events
[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
545 bool changed
= false;
547 /* If Connectionless Slave Broadcast master role is supported
548 * enable all necessary events for it.
550 if (lmp_csb_master_capable(hdev
)) {
551 events
[1] |= 0x40; /* Triggered Clock Capture */
552 events
[1] |= 0x80; /* Synchronization Train Complete */
553 events
[2] |= 0x10; /* Slave Page Response Timeout */
554 events
[2] |= 0x20; /* CSB Channel Map Change */
558 /* If Connectionless Slave Broadcast slave role is supported
559 * enable all necessary events for it.
561 if (lmp_csb_slave_capable(hdev
)) {
562 events
[2] |= 0x01; /* Synchronization Train Received */
563 events
[2] |= 0x02; /* CSB Receive */
564 events
[2] |= 0x04; /* CSB Timeout */
565 events
[2] |= 0x08; /* Truncated Page Complete */
569 /* Enable Authenticated Payload Timeout Expired event if supported */
570 if (lmp_ping_capable(hdev
) || hdev
->le_features
[0] & HCI_LE_PING
) {
575 /* Some Broadcom based controllers indicate support for Set Event
576 * Mask Page 2 command, but then actually do not support it. Since
577 * the default value is all bits set to zero, the command is only
578 * required if the event mask has to be changed. In case no change
579 * to the event mask is needed, skip this command.
582 hci_req_add(req
, HCI_OP_SET_EVENT_MASK_PAGE_2
,
583 sizeof(events
), events
);
586 static int hci_init3_req(struct hci_request
*req
, unsigned long opt
)
588 struct hci_dev
*hdev
= req
->hdev
;
591 hci_setup_event_mask(req
);
593 if (hdev
->commands
[6] & 0x20 &&
594 !test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY
, &hdev
->quirks
)) {
595 struct hci_cp_read_stored_link_key cp
;
597 bacpy(&cp
.bdaddr
, BDADDR_ANY
);
599 hci_req_add(req
, HCI_OP_READ_STORED_LINK_KEY
, sizeof(cp
), &cp
);
602 if (hdev
->commands
[5] & 0x10)
603 hci_setup_link_policy(req
);
605 if (hdev
->commands
[8] & 0x01)
606 hci_req_add(req
, HCI_OP_READ_PAGE_SCAN_ACTIVITY
, 0, NULL
);
608 if (hdev
->commands
[18] & 0x04)
609 hci_req_add(req
, HCI_OP_READ_DEF_ERR_DATA_REPORTING
, 0, NULL
);
611 /* Some older Broadcom based Bluetooth 1.2 controllers do not
612 * support the Read Page Scan Type command. Check support for
613 * this command in the bit mask of supported commands.
615 if (hdev
->commands
[13] & 0x01)
616 hci_req_add(req
, HCI_OP_READ_PAGE_SCAN_TYPE
, 0, NULL
);
618 if (lmp_le_capable(hdev
)) {
621 memset(events
, 0, sizeof(events
));
623 if (hdev
->le_features
[0] & HCI_LE_ENCRYPTION
)
624 events
[0] |= 0x10; /* LE Long Term Key Request */
626 /* If controller supports the Connection Parameters Request
627 * Link Layer Procedure, enable the corresponding event.
629 if (hdev
->le_features
[0] & HCI_LE_CONN_PARAM_REQ_PROC
)
630 events
[0] |= 0x20; /* LE Remote Connection
634 /* If the controller supports the Data Length Extension
635 * feature, enable the corresponding event.
637 if (hdev
->le_features
[0] & HCI_LE_DATA_LEN_EXT
)
638 events
[0] |= 0x40; /* LE Data Length Change */
640 /* If the controller supports Extended Scanner Filter
641 * Policies, enable the correspondig event.
643 if (hdev
->le_features
[0] & HCI_LE_EXT_SCAN_POLICY
)
644 events
[1] |= 0x04; /* LE Direct Advertising
648 /* If the controller supports Channel Selection Algorithm #2
649 * feature, enable the corresponding event.
651 if (hdev
->le_features
[1] & HCI_LE_CHAN_SEL_ALG2
)
652 events
[2] |= 0x08; /* LE Channel Selection
656 /* If the controller supports the LE Set Scan Enable command,
657 * enable the corresponding advertising report event.
659 if (hdev
->commands
[26] & 0x08)
660 events
[0] |= 0x02; /* LE Advertising Report */
662 /* If the controller supports the LE Create Connection
663 * command, enable the corresponding event.
665 if (hdev
->commands
[26] & 0x10)
666 events
[0] |= 0x01; /* LE Connection Complete */
668 /* If the controller supports the LE Connection Update
669 * command, enable the corresponding event.
671 if (hdev
->commands
[27] & 0x04)
672 events
[0] |= 0x04; /* LE Connection Update
676 /* If the controller supports the LE Read Remote Used Features
677 * command, enable the corresponding event.
679 if (hdev
->commands
[27] & 0x20)
680 events
[0] |= 0x08; /* LE Read Remote Used
684 /* If the controller supports the LE Read Local P-256
685 * Public Key command, enable the corresponding event.
687 if (hdev
->commands
[34] & 0x02)
688 events
[0] |= 0x80; /* LE Read Local P-256
689 * Public Key Complete
692 /* If the controller supports the LE Generate DHKey
693 * command, enable the corresponding event.
695 if (hdev
->commands
[34] & 0x04)
696 events
[1] |= 0x01; /* LE Generate DHKey Complete */
698 /* If the controller supports the LE Set Default PHY or
699 * LE Set PHY commands, enable the corresponding event.
701 if (hdev
->commands
[35] & (0x20 | 0x40))
702 events
[1] |= 0x08; /* LE PHY Update Complete */
704 /* If the controller supports LE Set Extended Scan Parameters
705 * and LE Set Extended Scan Enable commands, enable the
706 * corresponding event.
708 if (use_ext_scan(hdev
))
709 events
[1] |= 0x10; /* LE Extended Advertising
713 /* If the controller supports the LE Extended Create Connection
714 * command, enable the corresponding event.
716 if (use_ext_conn(hdev
))
717 events
[1] |= 0x02; /* LE Enhanced Connection
721 /* If the controller supports the LE Extended Advertising
722 * command, enable the corresponding event.
724 if (ext_adv_capable(hdev
))
725 events
[2] |= 0x02; /* LE Advertising Set
729 hci_req_add(req
, HCI_OP_LE_SET_EVENT_MASK
, sizeof(events
),
732 /* Read LE Advertising Channel TX Power */
733 if ((hdev
->commands
[25] & 0x40) && !ext_adv_capable(hdev
)) {
734 /* HCI TS spec forbids mixing of legacy and extended
735 * advertising commands wherein READ_ADV_TX_POWER is
736 * also included. So do not call it if extended adv
737 * is supported otherwise controller will return
738 * COMMAND_DISALLOWED for extended commands.
740 hci_req_add(req
, HCI_OP_LE_READ_ADV_TX_POWER
, 0, NULL
);
743 if (hdev
->commands
[26] & 0x40) {
744 /* Read LE White List Size */
745 hci_req_add(req
, HCI_OP_LE_READ_WHITE_LIST_SIZE
,
749 if (hdev
->commands
[26] & 0x80) {
750 /* Clear LE White List */
751 hci_req_add(req
, HCI_OP_LE_CLEAR_WHITE_LIST
, 0, NULL
);
754 if (hdev
->commands
[34] & 0x40) {
755 /* Read LE Resolving List Size */
756 hci_req_add(req
, HCI_OP_LE_READ_RESOLV_LIST_SIZE
,
760 if (hdev
->commands
[34] & 0x20) {
761 /* Clear LE Resolving List */
762 hci_req_add(req
, HCI_OP_LE_CLEAR_RESOLV_LIST
, 0, NULL
);
765 if (hdev
->le_features
[0] & HCI_LE_DATA_LEN_EXT
) {
766 /* Read LE Maximum Data Length */
767 hci_req_add(req
, HCI_OP_LE_READ_MAX_DATA_LEN
, 0, NULL
);
769 /* Read LE Suggested Default Data Length */
770 hci_req_add(req
, HCI_OP_LE_READ_DEF_DATA_LEN
, 0, NULL
);
773 if (ext_adv_capable(hdev
)) {
774 /* Read LE Number of Supported Advertising Sets */
775 hci_req_add(req
, HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS
,
779 hci_set_le_support(req
);
782 /* Read features beyond page 1 if available */
783 for (p
= 2; p
< HCI_MAX_PAGES
&& p
<= hdev
->max_page
; p
++) {
784 struct hci_cp_read_local_ext_features cp
;
787 hci_req_add(req
, HCI_OP_READ_LOCAL_EXT_FEATURES
,
794 static int hci_init4_req(struct hci_request
*req
, unsigned long opt
)
796 struct hci_dev
*hdev
= req
->hdev
;
798 /* Some Broadcom based Bluetooth controllers do not support the
799 * Delete Stored Link Key command. They are clearly indicating its
800 * absence in the bit mask of supported commands.
802 * Check the supported commands and only if the the command is marked
803 * as supported send it. If not supported assume that the controller
804 * does not have actual support for stored link keys which makes this
805 * command redundant anyway.
807 * Some controllers indicate that they support handling deleting
808 * stored link keys, but they don't. The quirk lets a driver
809 * just disable this command.
811 if (hdev
->commands
[6] & 0x80 &&
812 !test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY
, &hdev
->quirks
)) {
813 struct hci_cp_delete_stored_link_key cp
;
815 bacpy(&cp
.bdaddr
, BDADDR_ANY
);
816 cp
.delete_all
= 0x01;
817 hci_req_add(req
, HCI_OP_DELETE_STORED_LINK_KEY
,
821 /* Set event mask page 2 if the HCI command for it is supported */
822 if (hdev
->commands
[22] & 0x04)
823 hci_set_event_mask_page_2(req
);
825 /* Read local codec list if the HCI command is supported */
826 if (hdev
->commands
[29] & 0x20)
827 hci_req_add(req
, HCI_OP_READ_LOCAL_CODECS
, 0, NULL
);
829 /* Get MWS transport configuration if the HCI command is supported */
830 if (hdev
->commands
[30] & 0x08)
831 hci_req_add(req
, HCI_OP_GET_MWS_TRANSPORT_CONFIG
, 0, NULL
);
833 /* Check for Synchronization Train support */
834 if (lmp_sync_train_capable(hdev
))
835 hci_req_add(req
, HCI_OP_READ_SYNC_TRAIN_PARAMS
, 0, NULL
);
837 /* Enable Secure Connections if supported and configured */
838 if (hci_dev_test_flag(hdev
, HCI_SSP_ENABLED
) &&
839 bredr_sc_enabled(hdev
)) {
842 hci_req_add(req
, HCI_OP_WRITE_SC_SUPPORT
,
843 sizeof(support
), &support
);
846 /* Set erroneous data reporting if supported to the wideband speech
849 if (hdev
->commands
[18] & 0x08) {
850 bool enabled
= hci_dev_test_flag(hdev
,
851 HCI_WIDEBAND_SPEECH_ENABLED
);
854 (hdev
->err_data_reporting
== ERR_DATA_REPORTING_ENABLED
)) {
855 struct hci_cp_write_def_err_data_reporting cp
;
857 cp
.err_data_reporting
= enabled
?
858 ERR_DATA_REPORTING_ENABLED
:
859 ERR_DATA_REPORTING_DISABLED
;
861 hci_req_add(req
, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING
,
866 /* Set Suggested Default Data Length to maximum if supported */
867 if (hdev
->le_features
[0] & HCI_LE_DATA_LEN_EXT
) {
868 struct hci_cp_le_write_def_data_len cp
;
870 cp
.tx_len
= cpu_to_le16(hdev
->le_max_tx_len
);
871 cp
.tx_time
= cpu_to_le16(hdev
->le_max_tx_time
);
872 hci_req_add(req
, HCI_OP_LE_WRITE_DEF_DATA_LEN
, sizeof(cp
), &cp
);
875 /* Set Default PHY parameters if command is supported */
876 if (hdev
->commands
[35] & 0x20) {
877 struct hci_cp_le_set_default_phy cp
;
880 cp
.tx_phys
= hdev
->le_tx_def_phys
;
881 cp
.rx_phys
= hdev
->le_rx_def_phys
;
883 hci_req_add(req
, HCI_OP_LE_SET_DEFAULT_PHY
, sizeof(cp
), &cp
);
889 static int __hci_init(struct hci_dev
*hdev
)
893 err
= __hci_req_sync(hdev
, hci_init1_req
, 0, HCI_INIT_TIMEOUT
, NULL
);
897 if (hci_dev_test_flag(hdev
, HCI_SETUP
))
898 hci_debugfs_create_basic(hdev
);
900 err
= __hci_req_sync(hdev
, hci_init2_req
, 0, HCI_INIT_TIMEOUT
, NULL
);
904 /* HCI_PRIMARY covers both single-mode LE, BR/EDR and dual-mode
905 * BR/EDR/LE type controllers. AMP controllers only need the
906 * first two stages of init.
908 if (hdev
->dev_type
!= HCI_PRIMARY
)
911 err
= __hci_req_sync(hdev
, hci_init3_req
, 0, HCI_INIT_TIMEOUT
, NULL
);
915 err
= __hci_req_sync(hdev
, hci_init4_req
, 0, HCI_INIT_TIMEOUT
, NULL
);
919 /* This function is only called when the controller is actually in
920 * configured state. When the controller is marked as unconfigured,
921 * this initialization procedure is not run.
923 * It means that it is possible that a controller runs through its
924 * setup phase and then discovers missing settings. If that is the
925 * case, then this function will not be called. It then will only
926 * be called during the config phase.
928 * So only when in setup phase or config phase, create the debugfs
929 * entries and register the SMP channels.
931 if (!hci_dev_test_flag(hdev
, HCI_SETUP
) &&
932 !hci_dev_test_flag(hdev
, HCI_CONFIG
))
935 hci_debugfs_create_common(hdev
);
937 if (lmp_bredr_capable(hdev
))
938 hci_debugfs_create_bredr(hdev
);
940 if (lmp_le_capable(hdev
))
941 hci_debugfs_create_le(hdev
);
946 static int hci_init0_req(struct hci_request
*req
, unsigned long opt
)
948 struct hci_dev
*hdev
= req
->hdev
;
950 BT_DBG("%s %ld", hdev
->name
, opt
);
953 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE
, &hdev
->quirks
))
954 hci_reset_req(req
, 0);
956 /* Read Local Version */
957 hci_req_add(req
, HCI_OP_READ_LOCAL_VERSION
, 0, NULL
);
959 /* Read BD Address */
960 if (hdev
->set_bdaddr
)
961 hci_req_add(req
, HCI_OP_READ_BD_ADDR
, 0, NULL
);
966 static int __hci_unconf_init(struct hci_dev
*hdev
)
970 if (test_bit(HCI_QUIRK_RAW_DEVICE
, &hdev
->quirks
))
973 err
= __hci_req_sync(hdev
, hci_init0_req
, 0, HCI_INIT_TIMEOUT
, NULL
);
977 if (hci_dev_test_flag(hdev
, HCI_SETUP
))
978 hci_debugfs_create_basic(hdev
);
983 static int hci_scan_req(struct hci_request
*req
, unsigned long opt
)
987 BT_DBG("%s %x", req
->hdev
->name
, scan
);
989 /* Inquiry and Page scans */
990 hci_req_add(req
, HCI_OP_WRITE_SCAN_ENABLE
, 1, &scan
);
994 static int hci_auth_req(struct hci_request
*req
, unsigned long opt
)
998 BT_DBG("%s %x", req
->hdev
->name
, auth
);
1000 /* Authentication */
1001 hci_req_add(req
, HCI_OP_WRITE_AUTH_ENABLE
, 1, &auth
);
1005 static int hci_encrypt_req(struct hci_request
*req
, unsigned long opt
)
1009 BT_DBG("%s %x", req
->hdev
->name
, encrypt
);
1012 hci_req_add(req
, HCI_OP_WRITE_ENCRYPT_MODE
, 1, &encrypt
);
1016 static int hci_linkpol_req(struct hci_request
*req
, unsigned long opt
)
1018 __le16 policy
= cpu_to_le16(opt
);
1020 BT_DBG("%s %x", req
->hdev
->name
, policy
);
1022 /* Default link policy */
1023 hci_req_add(req
, HCI_OP_WRITE_DEF_LINK_POLICY
, 2, &policy
);
1027 /* Get HCI device by index.
1028 * Device is held on return. */
1029 struct hci_dev
*hci_dev_get(int index
)
1031 struct hci_dev
*hdev
= NULL
, *d
;
1033 BT_DBG("%d", index
);
1038 read_lock(&hci_dev_list_lock
);
1039 list_for_each_entry(d
, &hci_dev_list
, list
) {
1040 if (d
->id
== index
) {
1041 hdev
= hci_dev_hold(d
);
1045 read_unlock(&hci_dev_list_lock
);
1049 /* ---- Inquiry support ---- */
1051 bool hci_discovery_active(struct hci_dev
*hdev
)
1053 struct discovery_state
*discov
= &hdev
->discovery
;
1055 switch (discov
->state
) {
1056 case DISCOVERY_FINDING
:
1057 case DISCOVERY_RESOLVING
:
1065 void hci_discovery_set_state(struct hci_dev
*hdev
, int state
)
1067 int old_state
= hdev
->discovery
.state
;
1069 BT_DBG("%s state %u -> %u", hdev
->name
, hdev
->discovery
.state
, state
);
1071 if (old_state
== state
)
1074 hdev
->discovery
.state
= state
;
1077 case DISCOVERY_STOPPED
:
1078 hci_update_background_scan(hdev
);
1080 if (old_state
!= DISCOVERY_STARTING
)
1081 mgmt_discovering(hdev
, 0);
1083 case DISCOVERY_STARTING
:
1085 case DISCOVERY_FINDING
:
1086 mgmt_discovering(hdev
, 1);
1088 case DISCOVERY_RESOLVING
:
1090 case DISCOVERY_STOPPING
:
1095 void hci_inquiry_cache_flush(struct hci_dev
*hdev
)
1097 struct discovery_state
*cache
= &hdev
->discovery
;
1098 struct inquiry_entry
*p
, *n
;
1100 list_for_each_entry_safe(p
, n
, &cache
->all
, all
) {
1105 INIT_LIST_HEAD(&cache
->unknown
);
1106 INIT_LIST_HEAD(&cache
->resolve
);
1109 struct inquiry_entry
*hci_inquiry_cache_lookup(struct hci_dev
*hdev
,
1112 struct discovery_state
*cache
= &hdev
->discovery
;
1113 struct inquiry_entry
*e
;
1115 BT_DBG("cache %p, %pMR", cache
, bdaddr
);
1117 list_for_each_entry(e
, &cache
->all
, all
) {
1118 if (!bacmp(&e
->data
.bdaddr
, bdaddr
))
1125 struct inquiry_entry
*hci_inquiry_cache_lookup_unknown(struct hci_dev
*hdev
,
1128 struct discovery_state
*cache
= &hdev
->discovery
;
1129 struct inquiry_entry
*e
;
1131 BT_DBG("cache %p, %pMR", cache
, bdaddr
);
1133 list_for_each_entry(e
, &cache
->unknown
, list
) {
1134 if (!bacmp(&e
->data
.bdaddr
, bdaddr
))
1141 struct inquiry_entry
*hci_inquiry_cache_lookup_resolve(struct hci_dev
*hdev
,
1145 struct discovery_state
*cache
= &hdev
->discovery
;
1146 struct inquiry_entry
*e
;
1148 BT_DBG("cache %p bdaddr %pMR state %d", cache
, bdaddr
, state
);
1150 list_for_each_entry(e
, &cache
->resolve
, list
) {
1151 if (!bacmp(bdaddr
, BDADDR_ANY
) && e
->name_state
== state
)
1153 if (!bacmp(&e
->data
.bdaddr
, bdaddr
))
1160 void hci_inquiry_cache_update_resolve(struct hci_dev
*hdev
,
1161 struct inquiry_entry
*ie
)
1163 struct discovery_state
*cache
= &hdev
->discovery
;
1164 struct list_head
*pos
= &cache
->resolve
;
1165 struct inquiry_entry
*p
;
1167 list_del(&ie
->list
);
1169 list_for_each_entry(p
, &cache
->resolve
, list
) {
1170 if (p
->name_state
!= NAME_PENDING
&&
1171 abs(p
->data
.rssi
) >= abs(ie
->data
.rssi
))
1176 list_add(&ie
->list
, pos
);
1179 u32
hci_inquiry_cache_update(struct hci_dev
*hdev
, struct inquiry_data
*data
,
1182 struct discovery_state
*cache
= &hdev
->discovery
;
1183 struct inquiry_entry
*ie
;
1186 BT_DBG("cache %p, %pMR", cache
, &data
->bdaddr
);
1188 hci_remove_remote_oob_data(hdev
, &data
->bdaddr
, BDADDR_BREDR
);
1190 if (!data
->ssp_mode
)
1191 flags
|= MGMT_DEV_FOUND_LEGACY_PAIRING
;
1193 ie
= hci_inquiry_cache_lookup(hdev
, &data
->bdaddr
);
1195 if (!ie
->data
.ssp_mode
)
1196 flags
|= MGMT_DEV_FOUND_LEGACY_PAIRING
;
1198 if (ie
->name_state
== NAME_NEEDED
&&
1199 data
->rssi
!= ie
->data
.rssi
) {
1200 ie
->data
.rssi
= data
->rssi
;
1201 hci_inquiry_cache_update_resolve(hdev
, ie
);
1207 /* Entry not in the cache. Add new one. */
1208 ie
= kzalloc(sizeof(*ie
), GFP_KERNEL
);
1210 flags
|= MGMT_DEV_FOUND_CONFIRM_NAME
;
1214 list_add(&ie
->all
, &cache
->all
);
1217 ie
->name_state
= NAME_KNOWN
;
1219 ie
->name_state
= NAME_NOT_KNOWN
;
1220 list_add(&ie
->list
, &cache
->unknown
);
1224 if (name_known
&& ie
->name_state
!= NAME_KNOWN
&&
1225 ie
->name_state
!= NAME_PENDING
) {
1226 ie
->name_state
= NAME_KNOWN
;
1227 list_del(&ie
->list
);
1230 memcpy(&ie
->data
, data
, sizeof(*data
));
1231 ie
->timestamp
= jiffies
;
1232 cache
->timestamp
= jiffies
;
1234 if (ie
->name_state
== NAME_NOT_KNOWN
)
1235 flags
|= MGMT_DEV_FOUND_CONFIRM_NAME
;
1241 static int inquiry_cache_dump(struct hci_dev
*hdev
, int num
, __u8
*buf
)
1243 struct discovery_state
*cache
= &hdev
->discovery
;
1244 struct inquiry_info
*info
= (struct inquiry_info
*) buf
;
1245 struct inquiry_entry
*e
;
1248 list_for_each_entry(e
, &cache
->all
, all
) {
1249 struct inquiry_data
*data
= &e
->data
;
1254 bacpy(&info
->bdaddr
, &data
->bdaddr
);
1255 info
->pscan_rep_mode
= data
->pscan_rep_mode
;
1256 info
->pscan_period_mode
= data
->pscan_period_mode
;
1257 info
->pscan_mode
= data
->pscan_mode
;
1258 memcpy(info
->dev_class
, data
->dev_class
, 3);
1259 info
->clock_offset
= data
->clock_offset
;
1265 BT_DBG("cache %p, copied %d", cache
, copied
);
1269 static int hci_inq_req(struct hci_request
*req
, unsigned long opt
)
1271 struct hci_inquiry_req
*ir
= (struct hci_inquiry_req
*) opt
;
1272 struct hci_dev
*hdev
= req
->hdev
;
1273 struct hci_cp_inquiry cp
;
1275 BT_DBG("%s", hdev
->name
);
1277 if (test_bit(HCI_INQUIRY
, &hdev
->flags
))
1281 memcpy(&cp
.lap
, &ir
->lap
, 3);
1282 cp
.length
= ir
->length
;
1283 cp
.num_rsp
= ir
->num_rsp
;
1284 hci_req_add(req
, HCI_OP_INQUIRY
, sizeof(cp
), &cp
);
1289 int hci_inquiry(void __user
*arg
)
1291 __u8 __user
*ptr
= arg
;
1292 struct hci_inquiry_req ir
;
1293 struct hci_dev
*hdev
;
1294 int err
= 0, do_inquiry
= 0, max_rsp
;
1298 if (copy_from_user(&ir
, ptr
, sizeof(ir
)))
1301 hdev
= hci_dev_get(ir
.dev_id
);
1305 if (hci_dev_test_flag(hdev
, HCI_USER_CHANNEL
)) {
1310 if (hci_dev_test_flag(hdev
, HCI_UNCONFIGURED
)) {
1315 if (hdev
->dev_type
!= HCI_PRIMARY
) {
1320 if (!hci_dev_test_flag(hdev
, HCI_BREDR_ENABLED
)) {
1326 if (inquiry_cache_age(hdev
) > INQUIRY_CACHE_AGE_MAX
||
1327 inquiry_cache_empty(hdev
) || ir
.flags
& IREQ_CACHE_FLUSH
) {
1328 hci_inquiry_cache_flush(hdev
);
1331 hci_dev_unlock(hdev
);
1333 timeo
= ir
.length
* msecs_to_jiffies(2000);
1336 err
= hci_req_sync(hdev
, hci_inq_req
, (unsigned long) &ir
,
1341 /* Wait until Inquiry procedure finishes (HCI_INQUIRY flag is
1342 * cleared). If it is interrupted by a signal, return -EINTR.
1344 if (wait_on_bit(&hdev
->flags
, HCI_INQUIRY
,
1345 TASK_INTERRUPTIBLE
))
1349 /* for unlimited number of responses we will use buffer with
1352 max_rsp
= (ir
.num_rsp
== 0) ? 255 : ir
.num_rsp
;
1354 /* cache_dump can't sleep. Therefore we allocate temp buffer and then
1355 * copy it to the user space.
1357 buf
= kmalloc_array(max_rsp
, sizeof(struct inquiry_info
), GFP_KERNEL
);
1364 ir
.num_rsp
= inquiry_cache_dump(hdev
, max_rsp
, buf
);
1365 hci_dev_unlock(hdev
);
1367 BT_DBG("num_rsp %d", ir
.num_rsp
);
1369 if (!copy_to_user(ptr
, &ir
, sizeof(ir
))) {
1371 if (copy_to_user(ptr
, buf
, sizeof(struct inquiry_info
) *
1385 * hci_dev_get_bd_addr_from_property - Get the Bluetooth Device Address
1386 * (BD_ADDR) for a HCI device from
1387 * a firmware node property.
1388 * @hdev: The HCI device
1390 * Search the firmware node for 'local-bd-address'.
1392 * All-zero BD addresses are rejected, because those could be properties
1393 * that exist in the firmware tables, but were not updated by the firmware. For
1394 * example, the DTS could define 'local-bd-address', with zero BD addresses.
1396 static void hci_dev_get_bd_addr_from_property(struct hci_dev
*hdev
)
1398 struct fwnode_handle
*fwnode
= dev_fwnode(hdev
->dev
.parent
);
1402 ret
= fwnode_property_read_u8_array(fwnode
, "local-bd-address",
1403 (u8
*)&ba
, sizeof(ba
));
1404 if (ret
< 0 || !bacmp(&ba
, BDADDR_ANY
))
1407 bacpy(&hdev
->public_addr
, &ba
);
1410 static int hci_dev_do_open(struct hci_dev
*hdev
)
1414 BT_DBG("%s %p", hdev
->name
, hdev
);
1416 hci_req_sync_lock(hdev
);
1418 if (hci_dev_test_flag(hdev
, HCI_UNREGISTER
)) {
1423 if (!hci_dev_test_flag(hdev
, HCI_SETUP
) &&
1424 !hci_dev_test_flag(hdev
, HCI_CONFIG
)) {
1425 /* Check for rfkill but allow the HCI setup stage to
1426 * proceed (which in itself doesn't cause any RF activity).
1428 if (hci_dev_test_flag(hdev
, HCI_RFKILLED
)) {
1433 /* Check for valid public address or a configured static
1434 * random adddress, but let the HCI setup proceed to
1435 * be able to determine if there is a public address
1438 * In case of user channel usage, it is not important
1439 * if a public address or static random address is
1442 * This check is only valid for BR/EDR controllers
1443 * since AMP controllers do not have an address.
1445 if (!hci_dev_test_flag(hdev
, HCI_USER_CHANNEL
) &&
1446 hdev
->dev_type
== HCI_PRIMARY
&&
1447 !bacmp(&hdev
->bdaddr
, BDADDR_ANY
) &&
1448 !bacmp(&hdev
->static_addr
, BDADDR_ANY
)) {
1449 ret
= -EADDRNOTAVAIL
;
1454 if (test_bit(HCI_UP
, &hdev
->flags
)) {
1459 if (hdev
->open(hdev
)) {
1464 set_bit(HCI_RUNNING
, &hdev
->flags
);
1465 hci_sock_dev_event(hdev
, HCI_DEV_OPEN
);
1467 atomic_set(&hdev
->cmd_cnt
, 1);
1468 set_bit(HCI_INIT
, &hdev
->flags
);
1470 if (hci_dev_test_flag(hdev
, HCI_SETUP
) ||
1471 test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP
, &hdev
->quirks
)) {
1472 bool invalid_bdaddr
;
1474 hci_sock_dev_event(hdev
, HCI_DEV_SETUP
);
1477 ret
= hdev
->setup(hdev
);
1479 /* The transport driver can set the quirk to mark the
1480 * BD_ADDR invalid before creating the HCI device or in
1481 * its setup callback.
1483 invalid_bdaddr
= test_bit(HCI_QUIRK_INVALID_BDADDR
,
1489 if (test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY
, &hdev
->quirks
)) {
1490 if (!bacmp(&hdev
->public_addr
, BDADDR_ANY
))
1491 hci_dev_get_bd_addr_from_property(hdev
);
1493 if (bacmp(&hdev
->public_addr
, BDADDR_ANY
) &&
1495 ret
= hdev
->set_bdaddr(hdev
,
1496 &hdev
->public_addr
);
1498 /* If setting of the BD_ADDR from the device
1499 * property succeeds, then treat the address
1500 * as valid even if the invalid BD_ADDR
1501 * quirk indicates otherwise.
1504 invalid_bdaddr
= false;
1509 /* The transport driver can set these quirks before
1510 * creating the HCI device or in its setup callback.
1512 * For the invalid BD_ADDR quirk it is possible that
1513 * it becomes a valid address if the bootloader does
1514 * provide it (see above).
1516 * In case any of them is set, the controller has to
1517 * start up as unconfigured.
1519 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG
, &hdev
->quirks
) ||
1521 hci_dev_set_flag(hdev
, HCI_UNCONFIGURED
);
1523 /* For an unconfigured controller it is required to
1524 * read at least the version information provided by
1525 * the Read Local Version Information command.
1527 * If the set_bdaddr driver callback is provided, then
1528 * also the original Bluetooth public device address
1529 * will be read using the Read BD Address command.
1531 if (hci_dev_test_flag(hdev
, HCI_UNCONFIGURED
))
1532 ret
= __hci_unconf_init(hdev
);
1535 if (hci_dev_test_flag(hdev
, HCI_CONFIG
)) {
1536 /* If public address change is configured, ensure that
1537 * the address gets programmed. If the driver does not
1538 * support changing the public address, fail the power
1541 if (bacmp(&hdev
->public_addr
, BDADDR_ANY
) &&
1543 ret
= hdev
->set_bdaddr(hdev
, &hdev
->public_addr
);
1545 ret
= -EADDRNOTAVAIL
;
1549 if (!hci_dev_test_flag(hdev
, HCI_UNCONFIGURED
) &&
1550 !hci_dev_test_flag(hdev
, HCI_USER_CHANNEL
)) {
1551 ret
= __hci_init(hdev
);
1552 if (!ret
&& hdev
->post_init
)
1553 ret
= hdev
->post_init(hdev
);
1557 /* If the HCI Reset command is clearing all diagnostic settings,
1558 * then they need to be reprogrammed after the init procedure
1561 if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG
, &hdev
->quirks
) &&
1562 !hci_dev_test_flag(hdev
, HCI_USER_CHANNEL
) &&
1563 hci_dev_test_flag(hdev
, HCI_VENDOR_DIAG
) && hdev
->set_diag
)
1564 ret
= hdev
->set_diag(hdev
, true);
1566 clear_bit(HCI_INIT
, &hdev
->flags
);
1570 hci_dev_set_flag(hdev
, HCI_RPA_EXPIRED
);
1571 hci_adv_instances_set_rpa_expired(hdev
, true);
1572 set_bit(HCI_UP
, &hdev
->flags
);
1573 hci_sock_dev_event(hdev
, HCI_DEV_UP
);
1574 hci_leds_update_powered(hdev
, true);
1575 if (!hci_dev_test_flag(hdev
, HCI_SETUP
) &&
1576 !hci_dev_test_flag(hdev
, HCI_CONFIG
) &&
1577 !hci_dev_test_flag(hdev
, HCI_UNCONFIGURED
) &&
1578 !hci_dev_test_flag(hdev
, HCI_USER_CHANNEL
) &&
1579 hci_dev_test_flag(hdev
, HCI_MGMT
) &&
1580 hdev
->dev_type
== HCI_PRIMARY
) {
1581 ret
= __hci_req_hci_power_on(hdev
);
1582 mgmt_power_on(hdev
, ret
);
1585 /* Init failed, cleanup */
1586 flush_work(&hdev
->tx_work
);
1587 flush_work(&hdev
->cmd_work
);
1588 flush_work(&hdev
->rx_work
);
1590 skb_queue_purge(&hdev
->cmd_q
);
1591 skb_queue_purge(&hdev
->rx_q
);
1596 if (hdev
->sent_cmd
) {
1597 kfree_skb(hdev
->sent_cmd
);
1598 hdev
->sent_cmd
= NULL
;
1601 clear_bit(HCI_RUNNING
, &hdev
->flags
);
1602 hci_sock_dev_event(hdev
, HCI_DEV_CLOSE
);
1605 hdev
->flags
&= BIT(HCI_RAW
);
1609 hci_req_sync_unlock(hdev
);
1613 /* ---- HCI ioctl helpers ---- */
1615 int hci_dev_open(__u16 dev
)
1617 struct hci_dev
*hdev
;
1620 hdev
= hci_dev_get(dev
);
1624 /* Devices that are marked as unconfigured can only be powered
1625 * up as user channel. Trying to bring them up as normal devices
1626 * will result into a failure. Only user channel operation is
1629 * When this function is called for a user channel, the flag
1630 * HCI_USER_CHANNEL will be set first before attempting to
1633 if (hci_dev_test_flag(hdev
, HCI_UNCONFIGURED
) &&
1634 !hci_dev_test_flag(hdev
, HCI_USER_CHANNEL
)) {
1639 /* We need to ensure that no other power on/off work is pending
1640 * before proceeding to call hci_dev_do_open. This is
1641 * particularly important if the setup procedure has not yet
1644 if (hci_dev_test_and_clear_flag(hdev
, HCI_AUTO_OFF
))
1645 cancel_delayed_work(&hdev
->power_off
);
1647 /* After this call it is guaranteed that the setup procedure
1648 * has finished. This means that error conditions like RFKILL
1649 * or no valid public or static random address apply.
1651 flush_workqueue(hdev
->req_workqueue
);
1653 /* For controllers not using the management interface and that
1654 * are brought up using legacy ioctl, set the HCI_BONDABLE bit
1655 * so that pairing works for them. Once the management interface
1656 * is in use this bit will be cleared again and userspace has
1657 * to explicitly enable it.
1659 if (!hci_dev_test_flag(hdev
, HCI_USER_CHANNEL
) &&
1660 !hci_dev_test_flag(hdev
, HCI_MGMT
))
1661 hci_dev_set_flag(hdev
, HCI_BONDABLE
);
1663 err
= hci_dev_do_open(hdev
);
1670 /* This function requires the caller holds hdev->lock */
1671 static void hci_pend_le_actions_clear(struct hci_dev
*hdev
)
1673 struct hci_conn_params
*p
;
1675 list_for_each_entry(p
, &hdev
->le_conn_params
, list
) {
1677 hci_conn_drop(p
->conn
);
1678 hci_conn_put(p
->conn
);
1681 list_del_init(&p
->action
);
1684 BT_DBG("All LE pending actions cleared");
1687 int hci_dev_do_close(struct hci_dev
*hdev
)
1691 BT_DBG("%s %p", hdev
->name
, hdev
);
1693 if (!hci_dev_test_flag(hdev
, HCI_UNREGISTER
) &&
1694 !hci_dev_test_flag(hdev
, HCI_USER_CHANNEL
) &&
1695 test_bit(HCI_UP
, &hdev
->flags
)) {
1696 /* Execute vendor specific shutdown routine */
1698 hdev
->shutdown(hdev
);
1701 cancel_delayed_work(&hdev
->power_off
);
1703 hci_request_cancel_all(hdev
);
1704 hci_req_sync_lock(hdev
);
1706 if (!test_and_clear_bit(HCI_UP
, &hdev
->flags
)) {
1707 cancel_delayed_work_sync(&hdev
->cmd_timer
);
1708 hci_req_sync_unlock(hdev
);
1712 hci_leds_update_powered(hdev
, false);
1714 /* Flush RX and TX works */
1715 flush_work(&hdev
->tx_work
);
1716 flush_work(&hdev
->rx_work
);
1718 if (hdev
->discov_timeout
> 0) {
1719 hdev
->discov_timeout
= 0;
1720 hci_dev_clear_flag(hdev
, HCI_DISCOVERABLE
);
1721 hci_dev_clear_flag(hdev
, HCI_LIMITED_DISCOVERABLE
);
1724 if (hci_dev_test_and_clear_flag(hdev
, HCI_SERVICE_CACHE
))
1725 cancel_delayed_work(&hdev
->service_cache
);
1727 if (hci_dev_test_flag(hdev
, HCI_MGMT
)) {
1728 struct adv_info
*adv_instance
;
1730 cancel_delayed_work_sync(&hdev
->rpa_expired
);
1732 list_for_each_entry(adv_instance
, &hdev
->adv_instances
, list
)
1733 cancel_delayed_work_sync(&adv_instance
->rpa_expired_cb
);
1736 /* Avoid potential lockdep warnings from the *_flush() calls by
1737 * ensuring the workqueue is empty up front.
1739 drain_workqueue(hdev
->workqueue
);
1743 hci_discovery_set_state(hdev
, DISCOVERY_STOPPED
);
1745 auto_off
= hci_dev_test_and_clear_flag(hdev
, HCI_AUTO_OFF
);
1747 if (!auto_off
&& hdev
->dev_type
== HCI_PRIMARY
&&
1748 !hci_dev_test_flag(hdev
, HCI_USER_CHANNEL
) &&
1749 hci_dev_test_flag(hdev
, HCI_MGMT
))
1750 __mgmt_power_off(hdev
);
1752 hci_inquiry_cache_flush(hdev
);
1753 hci_pend_le_actions_clear(hdev
);
1754 hci_conn_hash_flush(hdev
);
1755 hci_dev_unlock(hdev
);
1757 smp_unregister(hdev
);
1759 hci_sock_dev_event(hdev
, HCI_DEV_DOWN
);
1765 skb_queue_purge(&hdev
->cmd_q
);
1766 atomic_set(&hdev
->cmd_cnt
, 1);
1767 if (test_bit(HCI_QUIRK_RESET_ON_CLOSE
, &hdev
->quirks
) &&
1768 !auto_off
&& !hci_dev_test_flag(hdev
, HCI_UNCONFIGURED
)) {
1769 set_bit(HCI_INIT
, &hdev
->flags
);
1770 __hci_req_sync(hdev
, hci_reset_req
, 0, HCI_CMD_TIMEOUT
, NULL
);
1771 clear_bit(HCI_INIT
, &hdev
->flags
);
1774 /* flush cmd work */
1775 flush_work(&hdev
->cmd_work
);
1778 skb_queue_purge(&hdev
->rx_q
);
1779 skb_queue_purge(&hdev
->cmd_q
);
1780 skb_queue_purge(&hdev
->raw_q
);
1782 /* Drop last sent command */
1783 if (hdev
->sent_cmd
) {
1784 cancel_delayed_work_sync(&hdev
->cmd_timer
);
1785 kfree_skb(hdev
->sent_cmd
);
1786 hdev
->sent_cmd
= NULL
;
1789 clear_bit(HCI_RUNNING
, &hdev
->flags
);
1790 hci_sock_dev_event(hdev
, HCI_DEV_CLOSE
);
1792 if (test_and_clear_bit(SUSPEND_POWERING_DOWN
, hdev
->suspend_tasks
))
1793 wake_up(&hdev
->suspend_wait_q
);
1795 /* After this point our queues are empty
1796 * and no tasks are scheduled. */
1800 hdev
->flags
&= BIT(HCI_RAW
);
1801 hci_dev_clear_volatile_flags(hdev
);
1803 /* Controller radio is available but is currently powered down */
1804 hdev
->amp_status
= AMP_STATUS_POWERED_DOWN
;
1806 memset(hdev
->eir
, 0, sizeof(hdev
->eir
));
1807 memset(hdev
->dev_class
, 0, sizeof(hdev
->dev_class
));
1808 bacpy(&hdev
->random_addr
, BDADDR_ANY
);
1810 hci_req_sync_unlock(hdev
);
1816 int hci_dev_close(__u16 dev
)
1818 struct hci_dev
*hdev
;
1821 hdev
= hci_dev_get(dev
);
1825 if (hci_dev_test_flag(hdev
, HCI_USER_CHANNEL
)) {
1830 if (hci_dev_test_and_clear_flag(hdev
, HCI_AUTO_OFF
))
1831 cancel_delayed_work(&hdev
->power_off
);
1833 err
= hci_dev_do_close(hdev
);
1840 static int hci_dev_do_reset(struct hci_dev
*hdev
)
1844 BT_DBG("%s %p", hdev
->name
, hdev
);
1846 hci_req_sync_lock(hdev
);
1849 skb_queue_purge(&hdev
->rx_q
);
1850 skb_queue_purge(&hdev
->cmd_q
);
1852 /* Avoid potential lockdep warnings from the *_flush() calls by
1853 * ensuring the workqueue is empty up front.
1855 drain_workqueue(hdev
->workqueue
);
1858 hci_inquiry_cache_flush(hdev
);
1859 hci_conn_hash_flush(hdev
);
1860 hci_dev_unlock(hdev
);
1865 atomic_set(&hdev
->cmd_cnt
, 1);
1866 hdev
->acl_cnt
= 0; hdev
->sco_cnt
= 0; hdev
->le_cnt
= 0;
1868 ret
= __hci_req_sync(hdev
, hci_reset_req
, 0, HCI_INIT_TIMEOUT
, NULL
);
1870 hci_req_sync_unlock(hdev
);
1874 int hci_dev_reset(__u16 dev
)
1876 struct hci_dev
*hdev
;
1879 hdev
= hci_dev_get(dev
);
1883 if (!test_bit(HCI_UP
, &hdev
->flags
)) {
1888 if (hci_dev_test_flag(hdev
, HCI_USER_CHANNEL
)) {
1893 if (hci_dev_test_flag(hdev
, HCI_UNCONFIGURED
)) {
1898 err
= hci_dev_do_reset(hdev
);
1905 int hci_dev_reset_stat(__u16 dev
)
1907 struct hci_dev
*hdev
;
1910 hdev
= hci_dev_get(dev
);
1914 if (hci_dev_test_flag(hdev
, HCI_USER_CHANNEL
)) {
1919 if (hci_dev_test_flag(hdev
, HCI_UNCONFIGURED
)) {
1924 memset(&hdev
->stat
, 0, sizeof(struct hci_dev_stats
));
1931 static void hci_update_scan_state(struct hci_dev
*hdev
, u8 scan
)
1933 bool conn_changed
, discov_changed
;
1935 BT_DBG("%s scan 0x%02x", hdev
->name
, scan
);
1937 if ((scan
& SCAN_PAGE
))
1938 conn_changed
= !hci_dev_test_and_set_flag(hdev
,
1941 conn_changed
= hci_dev_test_and_clear_flag(hdev
,
1944 if ((scan
& SCAN_INQUIRY
)) {
1945 discov_changed
= !hci_dev_test_and_set_flag(hdev
,
1948 hci_dev_clear_flag(hdev
, HCI_LIMITED_DISCOVERABLE
);
1949 discov_changed
= hci_dev_test_and_clear_flag(hdev
,
1953 if (!hci_dev_test_flag(hdev
, HCI_MGMT
))
1956 if (conn_changed
|| discov_changed
) {
1957 /* In case this was disabled through mgmt */
1958 hci_dev_set_flag(hdev
, HCI_BREDR_ENABLED
);
1960 if (hci_dev_test_flag(hdev
, HCI_LE_ENABLED
))
1961 hci_req_update_adv_data(hdev
, hdev
->cur_adv_instance
);
1963 mgmt_new_settings(hdev
);
1967 int hci_dev_cmd(unsigned int cmd
, void __user
*arg
)
1969 struct hci_dev
*hdev
;
1970 struct hci_dev_req dr
;
1973 if (copy_from_user(&dr
, arg
, sizeof(dr
)))
1976 hdev
= hci_dev_get(dr
.dev_id
);
1980 if (hci_dev_test_flag(hdev
, HCI_USER_CHANNEL
)) {
1985 if (hci_dev_test_flag(hdev
, HCI_UNCONFIGURED
)) {
1990 if (hdev
->dev_type
!= HCI_PRIMARY
) {
1995 if (!hci_dev_test_flag(hdev
, HCI_BREDR_ENABLED
)) {
2002 err
= hci_req_sync(hdev
, hci_auth_req
, dr
.dev_opt
,
2003 HCI_INIT_TIMEOUT
, NULL
);
2007 if (!lmp_encrypt_capable(hdev
)) {
2012 if (!test_bit(HCI_AUTH
, &hdev
->flags
)) {
2013 /* Auth must be enabled first */
2014 err
= hci_req_sync(hdev
, hci_auth_req
, dr
.dev_opt
,
2015 HCI_INIT_TIMEOUT
, NULL
);
2020 err
= hci_req_sync(hdev
, hci_encrypt_req
, dr
.dev_opt
,
2021 HCI_INIT_TIMEOUT
, NULL
);
2025 err
= hci_req_sync(hdev
, hci_scan_req
, dr
.dev_opt
,
2026 HCI_INIT_TIMEOUT
, NULL
);
2028 /* Ensure that the connectable and discoverable states
2029 * get correctly modified as this was a non-mgmt change.
2032 hci_update_scan_state(hdev
, dr
.dev_opt
);
2036 err
= hci_req_sync(hdev
, hci_linkpol_req
, dr
.dev_opt
,
2037 HCI_INIT_TIMEOUT
, NULL
);
2040 case HCISETLINKMODE
:
2041 hdev
->link_mode
= ((__u16
) dr
.dev_opt
) &
2042 (HCI_LM_MASTER
| HCI_LM_ACCEPT
);
2046 if (hdev
->pkt_type
== (__u16
) dr
.dev_opt
)
2049 hdev
->pkt_type
= (__u16
) dr
.dev_opt
;
2050 mgmt_phy_configuration_changed(hdev
, NULL
);
2054 hdev
->acl_mtu
= *((__u16
*) &dr
.dev_opt
+ 1);
2055 hdev
->acl_pkts
= *((__u16
*) &dr
.dev_opt
+ 0);
2059 hdev
->sco_mtu
= *((__u16
*) &dr
.dev_opt
+ 1);
2060 hdev
->sco_pkts
= *((__u16
*) &dr
.dev_opt
+ 0);
2073 int hci_get_dev_list(void __user
*arg
)
2075 struct hci_dev
*hdev
;
2076 struct hci_dev_list_req
*dl
;
2077 struct hci_dev_req
*dr
;
2078 int n
= 0, size
, err
;
2081 if (get_user(dev_num
, (__u16 __user
*) arg
))
2084 if (!dev_num
|| dev_num
> (PAGE_SIZE
* 2) / sizeof(*dr
))
2087 size
= sizeof(*dl
) + dev_num
* sizeof(*dr
);
2089 dl
= kzalloc(size
, GFP_KERNEL
);
2095 read_lock(&hci_dev_list_lock
);
2096 list_for_each_entry(hdev
, &hci_dev_list
, list
) {
2097 unsigned long flags
= hdev
->flags
;
2099 /* When the auto-off is configured it means the transport
2100 * is running, but in that case still indicate that the
2101 * device is actually down.
2103 if (hci_dev_test_flag(hdev
, HCI_AUTO_OFF
))
2104 flags
&= ~BIT(HCI_UP
);
2106 (dr
+ n
)->dev_id
= hdev
->id
;
2107 (dr
+ n
)->dev_opt
= flags
;
2112 read_unlock(&hci_dev_list_lock
);
2115 size
= sizeof(*dl
) + n
* sizeof(*dr
);
2117 err
= copy_to_user(arg
, dl
, size
);
2120 return err
? -EFAULT
: 0;
2123 int hci_get_dev_info(void __user
*arg
)
2125 struct hci_dev
*hdev
;
2126 struct hci_dev_info di
;
2127 unsigned long flags
;
2130 if (copy_from_user(&di
, arg
, sizeof(di
)))
2133 hdev
= hci_dev_get(di
.dev_id
);
2137 /* When the auto-off is configured it means the transport
2138 * is running, but in that case still indicate that the
2139 * device is actually down.
2141 if (hci_dev_test_flag(hdev
, HCI_AUTO_OFF
))
2142 flags
= hdev
->flags
& ~BIT(HCI_UP
);
2144 flags
= hdev
->flags
;
2146 strcpy(di
.name
, hdev
->name
);
2147 di
.bdaddr
= hdev
->bdaddr
;
2148 di
.type
= (hdev
->bus
& 0x0f) | ((hdev
->dev_type
& 0x03) << 4);
2150 di
.pkt_type
= hdev
->pkt_type
;
2151 if (lmp_bredr_capable(hdev
)) {
2152 di
.acl_mtu
= hdev
->acl_mtu
;
2153 di
.acl_pkts
= hdev
->acl_pkts
;
2154 di
.sco_mtu
= hdev
->sco_mtu
;
2155 di
.sco_pkts
= hdev
->sco_pkts
;
2157 di
.acl_mtu
= hdev
->le_mtu
;
2158 di
.acl_pkts
= hdev
->le_pkts
;
2162 di
.link_policy
= hdev
->link_policy
;
2163 di
.link_mode
= hdev
->link_mode
;
2165 memcpy(&di
.stat
, &hdev
->stat
, sizeof(di
.stat
));
2166 memcpy(&di
.features
, &hdev
->features
, sizeof(di
.features
));
2168 if (copy_to_user(arg
, &di
, sizeof(di
)))
2176 /* ---- Interface to HCI drivers ---- */
2178 static int hci_rfkill_set_block(void *data
, bool blocked
)
2180 struct hci_dev
*hdev
= data
;
2182 BT_DBG("%p name %s blocked %d", hdev
, hdev
->name
, blocked
);
2184 if (hci_dev_test_flag(hdev
, HCI_USER_CHANNEL
))
2188 hci_dev_set_flag(hdev
, HCI_RFKILLED
);
2189 if (!hci_dev_test_flag(hdev
, HCI_SETUP
) &&
2190 !hci_dev_test_flag(hdev
, HCI_CONFIG
))
2191 hci_dev_do_close(hdev
);
2193 hci_dev_clear_flag(hdev
, HCI_RFKILLED
);
2199 static const struct rfkill_ops hci_rfkill_ops
= {
2200 .set_block
= hci_rfkill_set_block
,
2203 static void hci_power_on(struct work_struct
*work
)
2205 struct hci_dev
*hdev
= container_of(work
, struct hci_dev
, power_on
);
2208 BT_DBG("%s", hdev
->name
);
2210 if (test_bit(HCI_UP
, &hdev
->flags
) &&
2211 hci_dev_test_flag(hdev
, HCI_MGMT
) &&
2212 hci_dev_test_and_clear_flag(hdev
, HCI_AUTO_OFF
)) {
2213 cancel_delayed_work(&hdev
->power_off
);
2214 hci_req_sync_lock(hdev
);
2215 err
= __hci_req_hci_power_on(hdev
);
2216 hci_req_sync_unlock(hdev
);
2217 mgmt_power_on(hdev
, err
);
2221 err
= hci_dev_do_open(hdev
);
2224 mgmt_set_powered_failed(hdev
, err
);
2225 hci_dev_unlock(hdev
);
2229 /* During the HCI setup phase, a few error conditions are
2230 * ignored and they need to be checked now. If they are still
2231 * valid, it is important to turn the device back off.
2233 if (hci_dev_test_flag(hdev
, HCI_RFKILLED
) ||
2234 hci_dev_test_flag(hdev
, HCI_UNCONFIGURED
) ||
2235 (hdev
->dev_type
== HCI_PRIMARY
&&
2236 !bacmp(&hdev
->bdaddr
, BDADDR_ANY
) &&
2237 !bacmp(&hdev
->static_addr
, BDADDR_ANY
))) {
2238 hci_dev_clear_flag(hdev
, HCI_AUTO_OFF
);
2239 hci_dev_do_close(hdev
);
2240 } else if (hci_dev_test_flag(hdev
, HCI_AUTO_OFF
)) {
2241 queue_delayed_work(hdev
->req_workqueue
, &hdev
->power_off
,
2242 HCI_AUTO_OFF_TIMEOUT
);
2245 if (hci_dev_test_and_clear_flag(hdev
, HCI_SETUP
)) {
2246 /* For unconfigured devices, set the HCI_RAW flag
2247 * so that userspace can easily identify them.
2249 if (hci_dev_test_flag(hdev
, HCI_UNCONFIGURED
))
2250 set_bit(HCI_RAW
, &hdev
->flags
);
2252 /* For fully configured devices, this will send
2253 * the Index Added event. For unconfigured devices,
2254 * it will send Unconfigued Index Added event.
2256 * Devices with HCI_QUIRK_RAW_DEVICE are ignored
2257 * and no event will be send.
2259 mgmt_index_added(hdev
);
2260 } else if (hci_dev_test_and_clear_flag(hdev
, HCI_CONFIG
)) {
2261 /* When the controller is now configured, then it
2262 * is important to clear the HCI_RAW flag.
2264 if (!hci_dev_test_flag(hdev
, HCI_UNCONFIGURED
))
2265 clear_bit(HCI_RAW
, &hdev
->flags
);
2267 /* Powering on the controller with HCI_CONFIG set only
2268 * happens with the transition from unconfigured to
2269 * configured. This will send the Index Added event.
2271 mgmt_index_added(hdev
);
2275 static void hci_power_off(struct work_struct
*work
)
2277 struct hci_dev
*hdev
= container_of(work
, struct hci_dev
,
2280 BT_DBG("%s", hdev
->name
);
2282 hci_dev_do_close(hdev
);
2285 static void hci_error_reset(struct work_struct
*work
)
2287 struct hci_dev
*hdev
= container_of(work
, struct hci_dev
, error_reset
);
2289 BT_DBG("%s", hdev
->name
);
2292 hdev
->hw_error(hdev
, hdev
->hw_error_code
);
2294 bt_dev_err(hdev
, "hardware error 0x%2.2x", hdev
->hw_error_code
);
2296 if (hci_dev_do_close(hdev
))
2299 hci_dev_do_open(hdev
);
2302 void hci_uuids_clear(struct hci_dev
*hdev
)
2304 struct bt_uuid
*uuid
, *tmp
;
2306 list_for_each_entry_safe(uuid
, tmp
, &hdev
->uuids
, list
) {
2307 list_del(&uuid
->list
);
2312 void hci_link_keys_clear(struct hci_dev
*hdev
)
2314 struct link_key
*key
;
2316 list_for_each_entry(key
, &hdev
->link_keys
, list
) {
2317 list_del_rcu(&key
->list
);
2318 kfree_rcu(key
, rcu
);
2322 void hci_smp_ltks_clear(struct hci_dev
*hdev
)
2326 list_for_each_entry(k
, &hdev
->long_term_keys
, list
) {
2327 list_del_rcu(&k
->list
);
2332 void hci_smp_irks_clear(struct hci_dev
*hdev
)
2336 list_for_each_entry(k
, &hdev
->identity_resolving_keys
, list
) {
2337 list_del_rcu(&k
->list
);
2342 void hci_blocked_keys_clear(struct hci_dev
*hdev
)
2344 struct blocked_key
*b
;
2346 list_for_each_entry(b
, &hdev
->blocked_keys
, list
) {
2347 list_del_rcu(&b
->list
);
2352 bool hci_is_blocked_key(struct hci_dev
*hdev
, u8 type
, u8 val
[16])
2354 bool blocked
= false;
2355 struct blocked_key
*b
;
2358 list_for_each_entry_rcu(b
, &hdev
->blocked_keys
, list
) {
2359 if (b
->type
== type
&& !memcmp(b
->val
, val
, sizeof(b
->val
))) {
2369 struct link_key
*hci_find_link_key(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
)
2374 list_for_each_entry_rcu(k
, &hdev
->link_keys
, list
) {
2375 if (bacmp(bdaddr
, &k
->bdaddr
) == 0) {
2378 if (hci_is_blocked_key(hdev
,
2379 HCI_BLOCKED_KEY_TYPE_LINKKEY
,
2381 bt_dev_warn_ratelimited(hdev
,
2382 "Link key blocked for %pMR",
2395 static bool hci_persistent_key(struct hci_dev
*hdev
, struct hci_conn
*conn
,
2396 u8 key_type
, u8 old_key_type
)
2399 if (key_type
< 0x03)
2402 /* Debug keys are insecure so don't store them persistently */
2403 if (key_type
== HCI_LK_DEBUG_COMBINATION
)
2406 /* Changed combination key and there's no previous one */
2407 if (key_type
== HCI_LK_CHANGED_COMBINATION
&& old_key_type
== 0xff)
2410 /* Security mode 3 case */
2414 /* BR/EDR key derived using SC from an LE link */
2415 if (conn
->type
== LE_LINK
)
2418 /* Neither local nor remote side had no-bonding as requirement */
2419 if (conn
->auth_type
> 0x01 && conn
->remote_auth
> 0x01)
2422 /* Local side had dedicated bonding as requirement */
2423 if (conn
->auth_type
== 0x02 || conn
->auth_type
== 0x03)
2426 /* Remote side had dedicated bonding as requirement */
2427 if (conn
->remote_auth
== 0x02 || conn
->remote_auth
== 0x03)
2430 /* If none of the above criteria match, then don't store the key
2435 static u8
ltk_role(u8 type
)
2437 if (type
== SMP_LTK
)
2438 return HCI_ROLE_MASTER
;
2440 return HCI_ROLE_SLAVE
;
2443 struct smp_ltk
*hci_find_ltk(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
2444 u8 addr_type
, u8 role
)
2449 list_for_each_entry_rcu(k
, &hdev
->long_term_keys
, list
) {
2450 if (addr_type
!= k
->bdaddr_type
|| bacmp(bdaddr
, &k
->bdaddr
))
2453 if (smp_ltk_is_sc(k
) || ltk_role(k
->type
) == role
) {
2456 if (hci_is_blocked_key(hdev
, HCI_BLOCKED_KEY_TYPE_LTK
,
2458 bt_dev_warn_ratelimited(hdev
,
2459 "LTK blocked for %pMR",
2472 struct smp_irk
*hci_find_irk_by_rpa(struct hci_dev
*hdev
, bdaddr_t
*rpa
)
2474 struct smp_irk
*irk_to_return
= NULL
;
2475 struct smp_irk
*irk
;
2478 list_for_each_entry_rcu(irk
, &hdev
->identity_resolving_keys
, list
) {
2479 if (!bacmp(&irk
->rpa
, rpa
)) {
2480 irk_to_return
= irk
;
2485 list_for_each_entry_rcu(irk
, &hdev
->identity_resolving_keys
, list
) {
2486 if (smp_irk_matches(hdev
, irk
->val
, rpa
)) {
2487 bacpy(&irk
->rpa
, rpa
);
2488 irk_to_return
= irk
;
2494 if (irk_to_return
&& hci_is_blocked_key(hdev
, HCI_BLOCKED_KEY_TYPE_IRK
,
2495 irk_to_return
->val
)) {
2496 bt_dev_warn_ratelimited(hdev
, "Identity key blocked for %pMR",
2497 &irk_to_return
->bdaddr
);
2498 irk_to_return
= NULL
;
2503 return irk_to_return
;
2506 struct smp_irk
*hci_find_irk_by_addr(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
2509 struct smp_irk
*irk_to_return
= NULL
;
2510 struct smp_irk
*irk
;
2512 /* Identity Address must be public or static random */
2513 if (addr_type
== ADDR_LE_DEV_RANDOM
&& (bdaddr
->b
[5] & 0xc0) != 0xc0)
2517 list_for_each_entry_rcu(irk
, &hdev
->identity_resolving_keys
, list
) {
2518 if (addr_type
== irk
->addr_type
&&
2519 bacmp(bdaddr
, &irk
->bdaddr
) == 0) {
2520 irk_to_return
= irk
;
2527 if (irk_to_return
&& hci_is_blocked_key(hdev
, HCI_BLOCKED_KEY_TYPE_IRK
,
2528 irk_to_return
->val
)) {
2529 bt_dev_warn_ratelimited(hdev
, "Identity key blocked for %pMR",
2530 &irk_to_return
->bdaddr
);
2531 irk_to_return
= NULL
;
2536 return irk_to_return
;
2539 struct link_key
*hci_add_link_key(struct hci_dev
*hdev
, struct hci_conn
*conn
,
2540 bdaddr_t
*bdaddr
, u8
*val
, u8 type
,
2541 u8 pin_len
, bool *persistent
)
2543 struct link_key
*key
, *old_key
;
2546 old_key
= hci_find_link_key(hdev
, bdaddr
);
2548 old_key_type
= old_key
->type
;
2551 old_key_type
= conn
? conn
->key_type
: 0xff;
2552 key
= kzalloc(sizeof(*key
), GFP_KERNEL
);
2555 list_add_rcu(&key
->list
, &hdev
->link_keys
);
2558 BT_DBG("%s key for %pMR type %u", hdev
->name
, bdaddr
, type
);
2560 /* Some buggy controller combinations generate a changed
2561 * combination key for legacy pairing even when there's no
2563 if (type
== HCI_LK_CHANGED_COMBINATION
&&
2564 (!conn
|| conn
->remote_auth
== 0xff) && old_key_type
== 0xff) {
2565 type
= HCI_LK_COMBINATION
;
2567 conn
->key_type
= type
;
2570 bacpy(&key
->bdaddr
, bdaddr
);
2571 memcpy(key
->val
, val
, HCI_LINK_KEY_SIZE
);
2572 key
->pin_len
= pin_len
;
2574 if (type
== HCI_LK_CHANGED_COMBINATION
)
2575 key
->type
= old_key_type
;
2580 *persistent
= hci_persistent_key(hdev
, conn
, type
,
2586 struct smp_ltk
*hci_add_ltk(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
2587 u8 addr_type
, u8 type
, u8 authenticated
,
2588 u8 tk
[16], u8 enc_size
, __le16 ediv
, __le64 rand
)
2590 struct smp_ltk
*key
, *old_key
;
2591 u8 role
= ltk_role(type
);
2593 old_key
= hci_find_ltk(hdev
, bdaddr
, addr_type
, role
);
2597 key
= kzalloc(sizeof(*key
), GFP_KERNEL
);
2600 list_add_rcu(&key
->list
, &hdev
->long_term_keys
);
2603 bacpy(&key
->bdaddr
, bdaddr
);
2604 key
->bdaddr_type
= addr_type
;
2605 memcpy(key
->val
, tk
, sizeof(key
->val
));
2606 key
->authenticated
= authenticated
;
2609 key
->enc_size
= enc_size
;
2615 struct smp_irk
*hci_add_irk(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
2616 u8 addr_type
, u8 val
[16], bdaddr_t
*rpa
)
2618 struct smp_irk
*irk
;
2620 irk
= hci_find_irk_by_addr(hdev
, bdaddr
, addr_type
);
2622 irk
= kzalloc(sizeof(*irk
), GFP_KERNEL
);
2626 bacpy(&irk
->bdaddr
, bdaddr
);
2627 irk
->addr_type
= addr_type
;
2629 list_add_rcu(&irk
->list
, &hdev
->identity_resolving_keys
);
2632 memcpy(irk
->val
, val
, 16);
2633 bacpy(&irk
->rpa
, rpa
);
2638 int hci_remove_link_key(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
)
2640 struct link_key
*key
;
2642 key
= hci_find_link_key(hdev
, bdaddr
);
2646 BT_DBG("%s removing %pMR", hdev
->name
, bdaddr
);
2648 list_del_rcu(&key
->list
);
2649 kfree_rcu(key
, rcu
);
2654 int hci_remove_ltk(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 bdaddr_type
)
2659 list_for_each_entry_rcu(k
, &hdev
->long_term_keys
, list
) {
2660 if (bacmp(bdaddr
, &k
->bdaddr
) || k
->bdaddr_type
!= bdaddr_type
)
2663 BT_DBG("%s removing %pMR", hdev
->name
, bdaddr
);
2665 list_del_rcu(&k
->list
);
2670 return removed
? 0 : -ENOENT
;
2673 void hci_remove_irk(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 addr_type
)
2677 list_for_each_entry_rcu(k
, &hdev
->identity_resolving_keys
, list
) {
2678 if (bacmp(bdaddr
, &k
->bdaddr
) || k
->addr_type
!= addr_type
)
2681 BT_DBG("%s removing %pMR", hdev
->name
, bdaddr
);
2683 list_del_rcu(&k
->list
);
2688 bool hci_bdaddr_is_paired(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 type
)
2691 struct smp_irk
*irk
;
2694 if (type
== BDADDR_BREDR
) {
2695 if (hci_find_link_key(hdev
, bdaddr
))
2700 /* Convert to HCI addr type which struct smp_ltk uses */
2701 if (type
== BDADDR_LE_PUBLIC
)
2702 addr_type
= ADDR_LE_DEV_PUBLIC
;
2704 addr_type
= ADDR_LE_DEV_RANDOM
;
2706 irk
= hci_get_irk(hdev
, bdaddr
, addr_type
);
2708 bdaddr
= &irk
->bdaddr
;
2709 addr_type
= irk
->addr_type
;
2713 list_for_each_entry_rcu(k
, &hdev
->long_term_keys
, list
) {
2714 if (k
->bdaddr_type
== addr_type
&& !bacmp(bdaddr
, &k
->bdaddr
)) {
2724 /* HCI command timer function */
2725 static void hci_cmd_timeout(struct work_struct
*work
)
2727 struct hci_dev
*hdev
= container_of(work
, struct hci_dev
,
2730 if (hdev
->sent_cmd
) {
2731 struct hci_command_hdr
*sent
= (void *) hdev
->sent_cmd
->data
;
2732 u16 opcode
= __le16_to_cpu(sent
->opcode
);
2734 bt_dev_err(hdev
, "command 0x%4.4x tx timeout", opcode
);
2736 bt_dev_err(hdev
, "command tx timeout");
2739 if (hdev
->cmd_timeout
)
2740 hdev
->cmd_timeout(hdev
);
2742 atomic_set(&hdev
->cmd_cnt
, 1);
2743 queue_work(hdev
->workqueue
, &hdev
->cmd_work
);
2746 struct oob_data
*hci_find_remote_oob_data(struct hci_dev
*hdev
,
2747 bdaddr_t
*bdaddr
, u8 bdaddr_type
)
2749 struct oob_data
*data
;
2751 list_for_each_entry(data
, &hdev
->remote_oob_data
, list
) {
2752 if (bacmp(bdaddr
, &data
->bdaddr
) != 0)
2754 if (data
->bdaddr_type
!= bdaddr_type
)
2762 int hci_remove_remote_oob_data(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
2765 struct oob_data
*data
;
2767 data
= hci_find_remote_oob_data(hdev
, bdaddr
, bdaddr_type
);
2771 BT_DBG("%s removing %pMR (%u)", hdev
->name
, bdaddr
, bdaddr_type
);
2773 list_del(&data
->list
);
2779 void hci_remote_oob_data_clear(struct hci_dev
*hdev
)
2781 struct oob_data
*data
, *n
;
2783 list_for_each_entry_safe(data
, n
, &hdev
->remote_oob_data
, list
) {
2784 list_del(&data
->list
);
2789 int hci_add_remote_oob_data(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
2790 u8 bdaddr_type
, u8
*hash192
, u8
*rand192
,
2791 u8
*hash256
, u8
*rand256
)
2793 struct oob_data
*data
;
2795 data
= hci_find_remote_oob_data(hdev
, bdaddr
, bdaddr_type
);
2797 data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
2801 bacpy(&data
->bdaddr
, bdaddr
);
2802 data
->bdaddr_type
= bdaddr_type
;
2803 list_add(&data
->list
, &hdev
->remote_oob_data
);
2806 if (hash192
&& rand192
) {
2807 memcpy(data
->hash192
, hash192
, sizeof(data
->hash192
));
2808 memcpy(data
->rand192
, rand192
, sizeof(data
->rand192
));
2809 if (hash256
&& rand256
)
2810 data
->present
= 0x03;
2812 memset(data
->hash192
, 0, sizeof(data
->hash192
));
2813 memset(data
->rand192
, 0, sizeof(data
->rand192
));
2814 if (hash256
&& rand256
)
2815 data
->present
= 0x02;
2817 data
->present
= 0x00;
2820 if (hash256
&& rand256
) {
2821 memcpy(data
->hash256
, hash256
, sizeof(data
->hash256
));
2822 memcpy(data
->rand256
, rand256
, sizeof(data
->rand256
));
2824 memset(data
->hash256
, 0, sizeof(data
->hash256
));
2825 memset(data
->rand256
, 0, sizeof(data
->rand256
));
2826 if (hash192
&& rand192
)
2827 data
->present
= 0x01;
2830 BT_DBG("%s for %pMR", hdev
->name
, bdaddr
);
2835 /* This function requires the caller holds hdev->lock */
2836 struct adv_info
*hci_find_adv_instance(struct hci_dev
*hdev
, u8 instance
)
2838 struct adv_info
*adv_instance
;
2840 list_for_each_entry(adv_instance
, &hdev
->adv_instances
, list
) {
2841 if (adv_instance
->instance
== instance
)
2842 return adv_instance
;
2848 /* This function requires the caller holds hdev->lock */
2849 struct adv_info
*hci_get_next_instance(struct hci_dev
*hdev
, u8 instance
)
2851 struct adv_info
*cur_instance
;
2853 cur_instance
= hci_find_adv_instance(hdev
, instance
);
2857 if (cur_instance
== list_last_entry(&hdev
->adv_instances
,
2858 struct adv_info
, list
))
2859 return list_first_entry(&hdev
->adv_instances
,
2860 struct adv_info
, list
);
2862 return list_next_entry(cur_instance
, list
);
2865 /* This function requires the caller holds hdev->lock */
2866 int hci_remove_adv_instance(struct hci_dev
*hdev
, u8 instance
)
2868 struct adv_info
*adv_instance
;
2870 adv_instance
= hci_find_adv_instance(hdev
, instance
);
2874 BT_DBG("%s removing %dMR", hdev
->name
, instance
);
2876 if (hdev
->cur_adv_instance
== instance
) {
2877 if (hdev
->adv_instance_timeout
) {
2878 cancel_delayed_work(&hdev
->adv_instance_expire
);
2879 hdev
->adv_instance_timeout
= 0;
2881 hdev
->cur_adv_instance
= 0x00;
2884 cancel_delayed_work_sync(&adv_instance
->rpa_expired_cb
);
2886 list_del(&adv_instance
->list
);
2887 kfree(adv_instance
);
2889 hdev
->adv_instance_cnt
--;
2894 void hci_adv_instances_set_rpa_expired(struct hci_dev
*hdev
, bool rpa_expired
)
2896 struct adv_info
*adv_instance
, *n
;
2898 list_for_each_entry_safe(adv_instance
, n
, &hdev
->adv_instances
, list
)
2899 adv_instance
->rpa_expired
= rpa_expired
;
2902 /* This function requires the caller holds hdev->lock */
2903 void hci_adv_instances_clear(struct hci_dev
*hdev
)
2905 struct adv_info
*adv_instance
, *n
;
2907 if (hdev
->adv_instance_timeout
) {
2908 cancel_delayed_work(&hdev
->adv_instance_expire
);
2909 hdev
->adv_instance_timeout
= 0;
2912 list_for_each_entry_safe(adv_instance
, n
, &hdev
->adv_instances
, list
) {
2913 cancel_delayed_work_sync(&adv_instance
->rpa_expired_cb
);
2914 list_del(&adv_instance
->list
);
2915 kfree(adv_instance
);
2918 hdev
->adv_instance_cnt
= 0;
2919 hdev
->cur_adv_instance
= 0x00;
2922 static void adv_instance_rpa_expired(struct work_struct
*work
)
2924 struct adv_info
*adv_instance
= container_of(work
, struct adv_info
,
2925 rpa_expired_cb
.work
);
2929 adv_instance
->rpa_expired
= true;
2932 /* This function requires the caller holds hdev->lock */
2933 int hci_add_adv_instance(struct hci_dev
*hdev
, u8 instance
, u32 flags
,
2934 u16 adv_data_len
, u8
*adv_data
,
2935 u16 scan_rsp_len
, u8
*scan_rsp_data
,
2936 u16 timeout
, u16 duration
)
2938 struct adv_info
*adv_instance
;
2940 adv_instance
= hci_find_adv_instance(hdev
, instance
);
2942 memset(adv_instance
->adv_data
, 0,
2943 sizeof(adv_instance
->adv_data
));
2944 memset(adv_instance
->scan_rsp_data
, 0,
2945 sizeof(adv_instance
->scan_rsp_data
));
2947 if (hdev
->adv_instance_cnt
>= hdev
->le_num_of_adv_sets
||
2948 instance
< 1 || instance
> HCI_MAX_ADV_INSTANCES
)
2951 adv_instance
= kzalloc(sizeof(*adv_instance
), GFP_KERNEL
);
2955 adv_instance
->pending
= true;
2956 adv_instance
->instance
= instance
;
2957 list_add(&adv_instance
->list
, &hdev
->adv_instances
);
2958 hdev
->adv_instance_cnt
++;
2961 adv_instance
->flags
= flags
;
2962 adv_instance
->adv_data_len
= adv_data_len
;
2963 adv_instance
->scan_rsp_len
= scan_rsp_len
;
2966 memcpy(adv_instance
->adv_data
, adv_data
, adv_data_len
);
2969 memcpy(adv_instance
->scan_rsp_data
,
2970 scan_rsp_data
, scan_rsp_len
);
2972 adv_instance
->timeout
= timeout
;
2973 adv_instance
->remaining_time
= timeout
;
2976 adv_instance
->duration
= HCI_DEFAULT_ADV_DURATION
;
2978 adv_instance
->duration
= duration
;
2980 adv_instance
->tx_power
= HCI_TX_POWER_INVALID
;
2982 INIT_DELAYED_WORK(&adv_instance
->rpa_expired_cb
,
2983 adv_instance_rpa_expired
);
2985 BT_DBG("%s for %dMR", hdev
->name
, instance
);
2990 struct bdaddr_list
*hci_bdaddr_list_lookup(struct list_head
*bdaddr_list
,
2991 bdaddr_t
*bdaddr
, u8 type
)
2993 struct bdaddr_list
*b
;
2995 list_for_each_entry(b
, bdaddr_list
, list
) {
2996 if (!bacmp(&b
->bdaddr
, bdaddr
) && b
->bdaddr_type
== type
)
3003 struct bdaddr_list_with_irk
*hci_bdaddr_list_lookup_with_irk(
3004 struct list_head
*bdaddr_list
, bdaddr_t
*bdaddr
,
3007 struct bdaddr_list_with_irk
*b
;
3009 list_for_each_entry(b
, bdaddr_list
, list
) {
3010 if (!bacmp(&b
->bdaddr
, bdaddr
) && b
->bdaddr_type
== type
)
3017 void hci_bdaddr_list_clear(struct list_head
*bdaddr_list
)
3019 struct bdaddr_list
*b
, *n
;
3021 list_for_each_entry_safe(b
, n
, bdaddr_list
, list
) {
3027 int hci_bdaddr_list_add(struct list_head
*list
, bdaddr_t
*bdaddr
, u8 type
)
3029 struct bdaddr_list
*entry
;
3031 if (!bacmp(bdaddr
, BDADDR_ANY
))
3034 if (hci_bdaddr_list_lookup(list
, bdaddr
, type
))
3037 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
3041 bacpy(&entry
->bdaddr
, bdaddr
);
3042 entry
->bdaddr_type
= type
;
3044 list_add(&entry
->list
, list
);
3049 int hci_bdaddr_list_add_with_irk(struct list_head
*list
, bdaddr_t
*bdaddr
,
3050 u8 type
, u8
*peer_irk
, u8
*local_irk
)
3052 struct bdaddr_list_with_irk
*entry
;
3054 if (!bacmp(bdaddr
, BDADDR_ANY
))
3057 if (hci_bdaddr_list_lookup(list
, bdaddr
, type
))
3060 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
3064 bacpy(&entry
->bdaddr
, bdaddr
);
3065 entry
->bdaddr_type
= type
;
3068 memcpy(entry
->peer_irk
, peer_irk
, 16);
3071 memcpy(entry
->local_irk
, local_irk
, 16);
3073 list_add(&entry
->list
, list
);
3078 int hci_bdaddr_list_del(struct list_head
*list
, bdaddr_t
*bdaddr
, u8 type
)
3080 struct bdaddr_list
*entry
;
3082 if (!bacmp(bdaddr
, BDADDR_ANY
)) {
3083 hci_bdaddr_list_clear(list
);
3087 entry
= hci_bdaddr_list_lookup(list
, bdaddr
, type
);
3091 list_del(&entry
->list
);
3097 int hci_bdaddr_list_del_with_irk(struct list_head
*list
, bdaddr_t
*bdaddr
,
3100 struct bdaddr_list_with_irk
*entry
;
3102 if (!bacmp(bdaddr
, BDADDR_ANY
)) {
3103 hci_bdaddr_list_clear(list
);
3107 entry
= hci_bdaddr_list_lookup_with_irk(list
, bdaddr
, type
);
3111 list_del(&entry
->list
);
3117 /* This function requires the caller holds hdev->lock */
3118 struct hci_conn_params
*hci_conn_params_lookup(struct hci_dev
*hdev
,
3119 bdaddr_t
*addr
, u8 addr_type
)
3121 struct hci_conn_params
*params
;
3123 list_for_each_entry(params
, &hdev
->le_conn_params
, list
) {
3124 if (bacmp(¶ms
->addr
, addr
) == 0 &&
3125 params
->addr_type
== addr_type
) {
3133 /* This function requires the caller holds hdev->lock */
3134 struct hci_conn_params
*hci_pend_le_action_lookup(struct list_head
*list
,
3135 bdaddr_t
*addr
, u8 addr_type
)
3137 struct hci_conn_params
*param
;
3139 list_for_each_entry(param
, list
, action
) {
3140 if (bacmp(¶m
->addr
, addr
) == 0 &&
3141 param
->addr_type
== addr_type
)
3148 /* This function requires the caller holds hdev->lock */
3149 struct hci_conn_params
*hci_conn_params_add(struct hci_dev
*hdev
,
3150 bdaddr_t
*addr
, u8 addr_type
)
3152 struct hci_conn_params
*params
;
3154 params
= hci_conn_params_lookup(hdev
, addr
, addr_type
);
3158 params
= kzalloc(sizeof(*params
), GFP_KERNEL
);
3160 bt_dev_err(hdev
, "out of memory");
3164 bacpy(¶ms
->addr
, addr
);
3165 params
->addr_type
= addr_type
;
3167 list_add(¶ms
->list
, &hdev
->le_conn_params
);
3168 INIT_LIST_HEAD(¶ms
->action
);
3170 params
->conn_min_interval
= hdev
->le_conn_min_interval
;
3171 params
->conn_max_interval
= hdev
->le_conn_max_interval
;
3172 params
->conn_latency
= hdev
->le_conn_latency
;
3173 params
->supervision_timeout
= hdev
->le_supv_timeout
;
3174 params
->auto_connect
= HCI_AUTO_CONN_DISABLED
;
3176 BT_DBG("addr %pMR (type %u)", addr
, addr_type
);
3181 static void hci_conn_params_free(struct hci_conn_params
*params
)
3184 hci_conn_drop(params
->conn
);
3185 hci_conn_put(params
->conn
);
3188 list_del(¶ms
->action
);
3189 list_del(¶ms
->list
);
3193 /* This function requires the caller holds hdev->lock */
3194 void hci_conn_params_del(struct hci_dev
*hdev
, bdaddr_t
*addr
, u8 addr_type
)
3196 struct hci_conn_params
*params
;
3198 params
= hci_conn_params_lookup(hdev
, addr
, addr_type
);
3202 hci_conn_params_free(params
);
3204 hci_update_background_scan(hdev
);
3206 BT_DBG("addr %pMR (type %u)", addr
, addr_type
);
3209 /* This function requires the caller holds hdev->lock */
3210 void hci_conn_params_clear_disabled(struct hci_dev
*hdev
)
3212 struct hci_conn_params
*params
, *tmp
;
3214 list_for_each_entry_safe(params
, tmp
, &hdev
->le_conn_params
, list
) {
3215 if (params
->auto_connect
!= HCI_AUTO_CONN_DISABLED
)
3218 /* If trying to estabilish one time connection to disabled
3219 * device, leave the params, but mark them as just once.
3221 if (params
->explicit_connect
) {
3222 params
->auto_connect
= HCI_AUTO_CONN_EXPLICIT
;
3226 list_del(¶ms
->list
);
3230 BT_DBG("All LE disabled connection parameters were removed");
3233 /* This function requires the caller holds hdev->lock */
3234 static void hci_conn_params_clear_all(struct hci_dev
*hdev
)
3236 struct hci_conn_params
*params
, *tmp
;
3238 list_for_each_entry_safe(params
, tmp
, &hdev
->le_conn_params
, list
)
3239 hci_conn_params_free(params
);
3241 BT_DBG("All LE connection parameters were removed");
3244 /* Copy the Identity Address of the controller.
3246 * If the controller has a public BD_ADDR, then by default use that one.
3247 * If this is a LE only controller without a public address, default to
3248 * the static random address.
3250 * For debugging purposes it is possible to force controllers with a
3251 * public address to use the static random address instead.
3253 * In case BR/EDR has been disabled on a dual-mode controller and
3254 * userspace has configured a static address, then that address
3255 * becomes the identity address instead of the public BR/EDR address.
3257 void hci_copy_identity_address(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
3260 if (hci_dev_test_flag(hdev
, HCI_FORCE_STATIC_ADDR
) ||
3261 !bacmp(&hdev
->bdaddr
, BDADDR_ANY
) ||
3262 (!hci_dev_test_flag(hdev
, HCI_BREDR_ENABLED
) &&
3263 bacmp(&hdev
->static_addr
, BDADDR_ANY
))) {
3264 bacpy(bdaddr
, &hdev
->static_addr
);
3265 *bdaddr_type
= ADDR_LE_DEV_RANDOM
;
3267 bacpy(bdaddr
, &hdev
->bdaddr
);
3268 *bdaddr_type
= ADDR_LE_DEV_PUBLIC
;
3272 static int hci_suspend_wait_event(struct hci_dev
*hdev
)
3275 (find_first_bit(hdev->suspend_tasks, __SUSPEND_NUM_TASKS) == \
3276 __SUSPEND_NUM_TASKS)
3279 int ret
= wait_event_timeout(hdev
->suspend_wait_q
,
3280 WAKE_COND
, SUSPEND_NOTIFIER_TIMEOUT
);
3283 bt_dev_dbg(hdev
, "Timed out waiting for suspend");
3284 for (i
= 0; i
< __SUSPEND_NUM_TASKS
; ++i
) {
3285 if (test_bit(i
, hdev
->suspend_tasks
))
3286 bt_dev_dbg(hdev
, "Bit %d is set", i
);
3287 clear_bit(i
, hdev
->suspend_tasks
);
3298 static void hci_prepare_suspend(struct work_struct
*work
)
3300 struct hci_dev
*hdev
=
3301 container_of(work
, struct hci_dev
, suspend_prepare
);
3304 hci_req_prepare_suspend(hdev
, hdev
->suspend_state_next
);
3305 hci_dev_unlock(hdev
);
3308 static int hci_suspend_notifier(struct notifier_block
*nb
, unsigned long action
,
3311 struct hci_dev
*hdev
=
3312 container_of(nb
, struct hci_dev
, suspend_notifier
);
3315 /* If powering down, wait for completion. */
3316 if (mgmt_powering_down(hdev
)) {
3317 set_bit(SUSPEND_POWERING_DOWN
, hdev
->suspend_tasks
);
3318 ret
= hci_suspend_wait_event(hdev
);
3323 /* Suspend notifier should only act on events when powered. */
3324 if (!hdev_is_powered(hdev
))
3327 if (action
== PM_SUSPEND_PREPARE
) {
3328 /* Suspend consists of two actions:
3329 * - First, disconnect everything and make the controller not
3330 * connectable (disabling scanning)
3331 * - Second, program event filter/whitelist and enable scan
3333 hdev
->suspend_state_next
= BT_SUSPEND_DISCONNECT
;
3334 set_bit(SUSPEND_PREPARE_NOTIFIER
, hdev
->suspend_tasks
);
3335 queue_work(hdev
->req_workqueue
, &hdev
->suspend_prepare
);
3336 ret
= hci_suspend_wait_event(hdev
);
3338 /* If the disconnect portion failed, don't attempt to complete
3339 * by configuring the whitelist. The suspend notifier will
3340 * follow a cancelled suspend with a PM_POST_SUSPEND
3344 hdev
->suspend_state_next
= BT_SUSPEND_COMPLETE
;
3345 set_bit(SUSPEND_PREPARE_NOTIFIER
, hdev
->suspend_tasks
);
3346 queue_work(hdev
->req_workqueue
, &hdev
->suspend_prepare
);
3347 ret
= hci_suspend_wait_event(hdev
);
3349 } else if (action
== PM_POST_SUSPEND
) {
3350 hdev
->suspend_state_next
= BT_RUNNING
;
3351 set_bit(SUSPEND_PREPARE_NOTIFIER
, hdev
->suspend_tasks
);
3352 queue_work(hdev
->req_workqueue
, &hdev
->suspend_prepare
);
3353 ret
= hci_suspend_wait_event(hdev
);
3357 return ret
? notifier_from_errno(-EBUSY
) : NOTIFY_STOP
;
3359 /* Alloc HCI device */
3360 struct hci_dev
*hci_alloc_dev(void)
3362 struct hci_dev
*hdev
;
3364 hdev
= kzalloc(sizeof(*hdev
), GFP_KERNEL
);
3368 hdev
->pkt_type
= (HCI_DM1
| HCI_DH1
| HCI_HV1
);
3369 hdev
->esco_type
= (ESCO_HV1
);
3370 hdev
->link_mode
= (HCI_LM_ACCEPT
);
3371 hdev
->num_iac
= 0x01; /* One IAC support is mandatory */
3372 hdev
->io_capability
= 0x03; /* No Input No Output */
3373 hdev
->manufacturer
= 0xffff; /* Default to internal use */
3374 hdev
->inq_tx_power
= HCI_TX_POWER_INVALID
;
3375 hdev
->adv_tx_power
= HCI_TX_POWER_INVALID
;
3376 hdev
->adv_instance_cnt
= 0;
3377 hdev
->cur_adv_instance
= 0x00;
3378 hdev
->adv_instance_timeout
= 0;
3380 hdev
->sniff_max_interval
= 800;
3381 hdev
->sniff_min_interval
= 80;
3383 hdev
->le_adv_channel_map
= 0x07;
3384 hdev
->le_adv_min_interval
= 0x0800;
3385 hdev
->le_adv_max_interval
= 0x0800;
3386 hdev
->le_scan_interval
= 0x0060;
3387 hdev
->le_scan_window
= 0x0030;
3388 hdev
->le_conn_min_interval
= 0x0018;
3389 hdev
->le_conn_max_interval
= 0x0028;
3390 hdev
->le_conn_latency
= 0x0000;
3391 hdev
->le_supv_timeout
= 0x002a;
3392 hdev
->le_def_tx_len
= 0x001b;
3393 hdev
->le_def_tx_time
= 0x0148;
3394 hdev
->le_max_tx_len
= 0x001b;
3395 hdev
->le_max_tx_time
= 0x0148;
3396 hdev
->le_max_rx_len
= 0x001b;
3397 hdev
->le_max_rx_time
= 0x0148;
3398 hdev
->le_max_key_size
= SMP_MAX_ENC_KEY_SIZE
;
3399 hdev
->le_min_key_size
= SMP_MIN_ENC_KEY_SIZE
;
3400 hdev
->le_tx_def_phys
= HCI_LE_SET_PHY_1M
;
3401 hdev
->le_rx_def_phys
= HCI_LE_SET_PHY_1M
;
3402 hdev
->le_num_of_adv_sets
= HCI_MAX_ADV_INSTANCES
;
3404 hdev
->rpa_timeout
= HCI_DEFAULT_RPA_TIMEOUT
;
3405 hdev
->discov_interleaved_timeout
= DISCOV_INTERLEAVED_TIMEOUT
;
3406 hdev
->conn_info_min_age
= DEFAULT_CONN_INFO_MIN_AGE
;
3407 hdev
->conn_info_max_age
= DEFAULT_CONN_INFO_MAX_AGE
;
3408 hdev
->auth_payload_timeout
= DEFAULT_AUTH_PAYLOAD_TIMEOUT
;
3409 hdev
->min_enc_key_size
= HCI_MIN_ENC_KEY_SIZE
;
3411 mutex_init(&hdev
->lock
);
3412 mutex_init(&hdev
->req_lock
);
3414 INIT_LIST_HEAD(&hdev
->mgmt_pending
);
3415 INIT_LIST_HEAD(&hdev
->blacklist
);
3416 INIT_LIST_HEAD(&hdev
->whitelist
);
3417 INIT_LIST_HEAD(&hdev
->wakeable
);
3418 INIT_LIST_HEAD(&hdev
->uuids
);
3419 INIT_LIST_HEAD(&hdev
->link_keys
);
3420 INIT_LIST_HEAD(&hdev
->long_term_keys
);
3421 INIT_LIST_HEAD(&hdev
->identity_resolving_keys
);
3422 INIT_LIST_HEAD(&hdev
->remote_oob_data
);
3423 INIT_LIST_HEAD(&hdev
->le_white_list
);
3424 INIT_LIST_HEAD(&hdev
->le_resolv_list
);
3425 INIT_LIST_HEAD(&hdev
->le_conn_params
);
3426 INIT_LIST_HEAD(&hdev
->pend_le_conns
);
3427 INIT_LIST_HEAD(&hdev
->pend_le_reports
);
3428 INIT_LIST_HEAD(&hdev
->conn_hash
.list
);
3429 INIT_LIST_HEAD(&hdev
->adv_instances
);
3430 INIT_LIST_HEAD(&hdev
->blocked_keys
);
3432 INIT_WORK(&hdev
->rx_work
, hci_rx_work
);
3433 INIT_WORK(&hdev
->cmd_work
, hci_cmd_work
);
3434 INIT_WORK(&hdev
->tx_work
, hci_tx_work
);
3435 INIT_WORK(&hdev
->power_on
, hci_power_on
);
3436 INIT_WORK(&hdev
->error_reset
, hci_error_reset
);
3437 INIT_WORK(&hdev
->suspend_prepare
, hci_prepare_suspend
);
3439 INIT_DELAYED_WORK(&hdev
->power_off
, hci_power_off
);
3441 skb_queue_head_init(&hdev
->rx_q
);
3442 skb_queue_head_init(&hdev
->cmd_q
);
3443 skb_queue_head_init(&hdev
->raw_q
);
3445 init_waitqueue_head(&hdev
->req_wait_q
);
3446 init_waitqueue_head(&hdev
->suspend_wait_q
);
3448 INIT_DELAYED_WORK(&hdev
->cmd_timer
, hci_cmd_timeout
);
3450 hci_request_setup(hdev
);
3452 hci_init_sysfs(hdev
);
3453 discovery_init(hdev
);
3457 EXPORT_SYMBOL(hci_alloc_dev
);
3459 /* Free HCI device */
3460 void hci_free_dev(struct hci_dev
*hdev
)
3462 /* will free via device release */
3463 put_device(&hdev
->dev
);
3465 EXPORT_SYMBOL(hci_free_dev
);
3467 /* Register HCI device */
3468 int hci_register_dev(struct hci_dev
*hdev
)
3472 if (!hdev
->open
|| !hdev
->close
|| !hdev
->send
)
3475 /* Do not allow HCI_AMP devices to register at index 0,
3476 * so the index can be used as the AMP controller ID.
3478 switch (hdev
->dev_type
) {
3480 id
= ida_simple_get(&hci_index_ida
, 0, 0, GFP_KERNEL
);
3483 id
= ida_simple_get(&hci_index_ida
, 1, 0, GFP_KERNEL
);
3492 sprintf(hdev
->name
, "hci%d", id
);
3495 BT_DBG("%p name %s bus %d", hdev
, hdev
->name
, hdev
->bus
);
3497 hdev
->workqueue
= alloc_ordered_workqueue("%s", WQ_HIGHPRI
, hdev
->name
);
3498 if (!hdev
->workqueue
) {
3503 hdev
->req_workqueue
= alloc_ordered_workqueue("%s", WQ_HIGHPRI
,
3505 if (!hdev
->req_workqueue
) {
3506 destroy_workqueue(hdev
->workqueue
);
3511 if (!IS_ERR_OR_NULL(bt_debugfs
))
3512 hdev
->debugfs
= debugfs_create_dir(hdev
->name
, bt_debugfs
);
3514 dev_set_name(&hdev
->dev
, "%s", hdev
->name
);
3516 error
= device_add(&hdev
->dev
);
3520 hci_leds_init(hdev
);
3522 hdev
->rfkill
= rfkill_alloc(hdev
->name
, &hdev
->dev
,
3523 RFKILL_TYPE_BLUETOOTH
, &hci_rfkill_ops
,
3526 if (rfkill_register(hdev
->rfkill
) < 0) {
3527 rfkill_destroy(hdev
->rfkill
);
3528 hdev
->rfkill
= NULL
;
3532 if (hdev
->rfkill
&& rfkill_blocked(hdev
->rfkill
))
3533 hci_dev_set_flag(hdev
, HCI_RFKILLED
);
3535 hci_dev_set_flag(hdev
, HCI_SETUP
);
3536 hci_dev_set_flag(hdev
, HCI_AUTO_OFF
);
3538 if (hdev
->dev_type
== HCI_PRIMARY
) {
3539 /* Assume BR/EDR support until proven otherwise (such as
3540 * through reading supported features during init.
3542 hci_dev_set_flag(hdev
, HCI_BREDR_ENABLED
);
3545 write_lock(&hci_dev_list_lock
);
3546 list_add(&hdev
->list
, &hci_dev_list
);
3547 write_unlock(&hci_dev_list_lock
);
3549 /* Devices that are marked for raw-only usage are unconfigured
3550 * and should not be included in normal operation.
3552 if (test_bit(HCI_QUIRK_RAW_DEVICE
, &hdev
->quirks
))
3553 hci_dev_set_flag(hdev
, HCI_UNCONFIGURED
);
3555 hci_sock_dev_event(hdev
, HCI_DEV_REG
);
3558 hdev
->suspend_notifier
.notifier_call
= hci_suspend_notifier
;
3559 error
= register_pm_notifier(&hdev
->suspend_notifier
);
3563 queue_work(hdev
->req_workqueue
, &hdev
->power_on
);
3568 destroy_workqueue(hdev
->workqueue
);
3569 destroy_workqueue(hdev
->req_workqueue
);
3571 ida_simple_remove(&hci_index_ida
, hdev
->id
);
3575 EXPORT_SYMBOL(hci_register_dev
);
3577 /* Unregister HCI device */
3578 void hci_unregister_dev(struct hci_dev
*hdev
)
3582 BT_DBG("%p name %s bus %d", hdev
, hdev
->name
, hdev
->bus
);
3584 hci_dev_set_flag(hdev
, HCI_UNREGISTER
);
3588 write_lock(&hci_dev_list_lock
);
3589 list_del(&hdev
->list
);
3590 write_unlock(&hci_dev_list_lock
);
3592 cancel_work_sync(&hdev
->power_on
);
3594 hci_dev_do_close(hdev
);
3596 unregister_pm_notifier(&hdev
->suspend_notifier
);
3598 if (!test_bit(HCI_INIT
, &hdev
->flags
) &&
3599 !hci_dev_test_flag(hdev
, HCI_SETUP
) &&
3600 !hci_dev_test_flag(hdev
, HCI_CONFIG
)) {
3602 mgmt_index_removed(hdev
);
3603 hci_dev_unlock(hdev
);
3606 /* mgmt_index_removed should take care of emptying the
3608 BUG_ON(!list_empty(&hdev
->mgmt_pending
));
3610 hci_sock_dev_event(hdev
, HCI_DEV_UNREG
);
3613 rfkill_unregister(hdev
->rfkill
);
3614 rfkill_destroy(hdev
->rfkill
);
3617 device_del(&hdev
->dev
);
3619 debugfs_remove_recursive(hdev
->debugfs
);
3620 kfree_const(hdev
->hw_info
);
3621 kfree_const(hdev
->fw_info
);
3623 destroy_workqueue(hdev
->workqueue
);
3624 destroy_workqueue(hdev
->req_workqueue
);
3627 hci_bdaddr_list_clear(&hdev
->blacklist
);
3628 hci_bdaddr_list_clear(&hdev
->whitelist
);
3629 hci_uuids_clear(hdev
);
3630 hci_link_keys_clear(hdev
);
3631 hci_smp_ltks_clear(hdev
);
3632 hci_smp_irks_clear(hdev
);
3633 hci_remote_oob_data_clear(hdev
);
3634 hci_adv_instances_clear(hdev
);
3635 hci_bdaddr_list_clear(&hdev
->le_white_list
);
3636 hci_bdaddr_list_clear(&hdev
->le_resolv_list
);
3637 hci_conn_params_clear_all(hdev
);
3638 hci_discovery_filter_clear(hdev
);
3639 hci_blocked_keys_clear(hdev
);
3640 hci_dev_unlock(hdev
);
3644 ida_simple_remove(&hci_index_ida
, id
);
3646 EXPORT_SYMBOL(hci_unregister_dev
);
3648 /* Suspend HCI device */
3649 int hci_suspend_dev(struct hci_dev
*hdev
)
3651 hci_sock_dev_event(hdev
, HCI_DEV_SUSPEND
);
3654 EXPORT_SYMBOL(hci_suspend_dev
);
3656 /* Resume HCI device */
3657 int hci_resume_dev(struct hci_dev
*hdev
)
3659 hci_sock_dev_event(hdev
, HCI_DEV_RESUME
);
3662 EXPORT_SYMBOL(hci_resume_dev
);
3664 /* Reset HCI device */
3665 int hci_reset_dev(struct hci_dev
*hdev
)
3667 static const u8 hw_err
[] = { HCI_EV_HARDWARE_ERROR
, 0x01, 0x00 };
3668 struct sk_buff
*skb
;
3670 skb
= bt_skb_alloc(3, GFP_ATOMIC
);
3674 hci_skb_pkt_type(skb
) = HCI_EVENT_PKT
;
3675 skb_put_data(skb
, hw_err
, 3);
3677 /* Send Hardware Error to upper stack */
3678 return hci_recv_frame(hdev
, skb
);
3680 EXPORT_SYMBOL(hci_reset_dev
);
3682 /* Receive frame from HCI drivers */
3683 int hci_recv_frame(struct hci_dev
*hdev
, struct sk_buff
*skb
)
3685 if (!hdev
|| (!test_bit(HCI_UP
, &hdev
->flags
)
3686 && !test_bit(HCI_INIT
, &hdev
->flags
))) {
3691 if (hci_skb_pkt_type(skb
) != HCI_EVENT_PKT
&&
3692 hci_skb_pkt_type(skb
) != HCI_ACLDATA_PKT
&&
3693 hci_skb_pkt_type(skb
) != HCI_SCODATA_PKT
&&
3694 hci_skb_pkt_type(skb
) != HCI_ISODATA_PKT
) {
3700 bt_cb(skb
)->incoming
= 1;
3703 __net_timestamp(skb
);
3705 skb_queue_tail(&hdev
->rx_q
, skb
);
3706 queue_work(hdev
->workqueue
, &hdev
->rx_work
);
3710 EXPORT_SYMBOL(hci_recv_frame
);
3712 /* Receive diagnostic message from HCI drivers */
3713 int hci_recv_diag(struct hci_dev
*hdev
, struct sk_buff
*skb
)
3715 /* Mark as diagnostic packet */
3716 hci_skb_pkt_type(skb
) = HCI_DIAG_PKT
;
3719 __net_timestamp(skb
);
3721 skb_queue_tail(&hdev
->rx_q
, skb
);
3722 queue_work(hdev
->workqueue
, &hdev
->rx_work
);
3726 EXPORT_SYMBOL(hci_recv_diag
);
3728 void hci_set_hw_info(struct hci_dev
*hdev
, const char *fmt
, ...)
3732 va_start(vargs
, fmt
);
3733 kfree_const(hdev
->hw_info
);
3734 hdev
->hw_info
= kvasprintf_const(GFP_KERNEL
, fmt
, vargs
);
3737 EXPORT_SYMBOL(hci_set_hw_info
);
3739 void hci_set_fw_info(struct hci_dev
*hdev
, const char *fmt
, ...)
3743 va_start(vargs
, fmt
);
3744 kfree_const(hdev
->fw_info
);
3745 hdev
->fw_info
= kvasprintf_const(GFP_KERNEL
, fmt
, vargs
);
3748 EXPORT_SYMBOL(hci_set_fw_info
);
3750 /* ---- Interface to upper protocols ---- */
3752 int hci_register_cb(struct hci_cb
*cb
)
3754 BT_DBG("%p name %s", cb
, cb
->name
);
3756 mutex_lock(&hci_cb_list_lock
);
3757 list_add_tail(&cb
->list
, &hci_cb_list
);
3758 mutex_unlock(&hci_cb_list_lock
);
3762 EXPORT_SYMBOL(hci_register_cb
);
3764 int hci_unregister_cb(struct hci_cb
*cb
)
3766 BT_DBG("%p name %s", cb
, cb
->name
);
3768 mutex_lock(&hci_cb_list_lock
);
3769 list_del(&cb
->list
);
3770 mutex_unlock(&hci_cb_list_lock
);
3774 EXPORT_SYMBOL(hci_unregister_cb
);
3776 static void hci_send_frame(struct hci_dev
*hdev
, struct sk_buff
*skb
)
3780 BT_DBG("%s type %d len %d", hdev
->name
, hci_skb_pkt_type(skb
),
3784 __net_timestamp(skb
);
3786 /* Send copy to monitor */
3787 hci_send_to_monitor(hdev
, skb
);
3789 if (atomic_read(&hdev
->promisc
)) {
3790 /* Send copy to the sockets */
3791 hci_send_to_sock(hdev
, skb
);
3794 /* Get rid of skb owner, prior to sending to the driver. */
3797 if (!test_bit(HCI_RUNNING
, &hdev
->flags
)) {
3802 err
= hdev
->send(hdev
, skb
);
3804 bt_dev_err(hdev
, "sending frame failed (%d)", err
);
3809 /* Send HCI command */
3810 int hci_send_cmd(struct hci_dev
*hdev
, __u16 opcode
, __u32 plen
,
3813 struct sk_buff
*skb
;
3815 BT_DBG("%s opcode 0x%4.4x plen %d", hdev
->name
, opcode
, plen
);
3817 skb
= hci_prepare_cmd(hdev
, opcode
, plen
, param
);
3819 bt_dev_err(hdev
, "no memory for command");
3823 /* Stand-alone HCI commands must be flagged as
3824 * single-command requests.
3826 bt_cb(skb
)->hci
.req_flags
|= HCI_REQ_START
;
3828 skb_queue_tail(&hdev
->cmd_q
, skb
);
3829 queue_work(hdev
->workqueue
, &hdev
->cmd_work
);
3834 int __hci_cmd_send(struct hci_dev
*hdev
, u16 opcode
, u32 plen
,
3837 struct sk_buff
*skb
;
3839 if (hci_opcode_ogf(opcode
) != 0x3f) {
3840 /* A controller receiving a command shall respond with either
3841 * a Command Status Event or a Command Complete Event.
3842 * Therefore, all standard HCI commands must be sent via the
3843 * standard API, using hci_send_cmd or hci_cmd_sync helpers.
3844 * Some vendors do not comply with this rule for vendor-specific
3845 * commands and do not return any event. We want to support
3846 * unresponded commands for such cases only.
3848 bt_dev_err(hdev
, "unresponded command not supported");
3852 skb
= hci_prepare_cmd(hdev
, opcode
, plen
, param
);
3854 bt_dev_err(hdev
, "no memory for command (opcode 0x%4.4x)",
3859 hci_send_frame(hdev
, skb
);
3863 EXPORT_SYMBOL(__hci_cmd_send
);
3865 /* Get data from the previously sent command */
3866 void *hci_sent_cmd_data(struct hci_dev
*hdev
, __u16 opcode
)
3868 struct hci_command_hdr
*hdr
;
3870 if (!hdev
->sent_cmd
)
3873 hdr
= (void *) hdev
->sent_cmd
->data
;
3875 if (hdr
->opcode
!= cpu_to_le16(opcode
))
3878 BT_DBG("%s opcode 0x%4.4x", hdev
->name
, opcode
);
3880 return hdev
->sent_cmd
->data
+ HCI_COMMAND_HDR_SIZE
;
3883 /* Send HCI command and wait for command commplete event */
3884 struct sk_buff
*hci_cmd_sync(struct hci_dev
*hdev
, u16 opcode
, u32 plen
,
3885 const void *param
, u32 timeout
)
3887 struct sk_buff
*skb
;
3889 if (!test_bit(HCI_UP
, &hdev
->flags
))
3890 return ERR_PTR(-ENETDOWN
);
3892 bt_dev_dbg(hdev
, "opcode 0x%4.4x plen %d", opcode
, plen
);
3894 hci_req_sync_lock(hdev
);
3895 skb
= __hci_cmd_sync(hdev
, opcode
, plen
, param
, timeout
);
3896 hci_req_sync_unlock(hdev
);
3900 EXPORT_SYMBOL(hci_cmd_sync
);
3903 static void hci_add_acl_hdr(struct sk_buff
*skb
, __u16 handle
, __u16 flags
)
3905 struct hci_acl_hdr
*hdr
;
3908 skb_push(skb
, HCI_ACL_HDR_SIZE
);
3909 skb_reset_transport_header(skb
);
3910 hdr
= (struct hci_acl_hdr
*)skb_transport_header(skb
);
3911 hdr
->handle
= cpu_to_le16(hci_handle_pack(handle
, flags
));
3912 hdr
->dlen
= cpu_to_le16(len
);
3915 static void hci_queue_acl(struct hci_chan
*chan
, struct sk_buff_head
*queue
,
3916 struct sk_buff
*skb
, __u16 flags
)
3918 struct hci_conn
*conn
= chan
->conn
;
3919 struct hci_dev
*hdev
= conn
->hdev
;
3920 struct sk_buff
*list
;
3922 skb
->len
= skb_headlen(skb
);
3925 hci_skb_pkt_type(skb
) = HCI_ACLDATA_PKT
;
3927 switch (hdev
->dev_type
) {
3929 hci_add_acl_hdr(skb
, conn
->handle
, flags
);
3932 hci_add_acl_hdr(skb
, chan
->handle
, flags
);
3935 bt_dev_err(hdev
, "unknown dev_type %d", hdev
->dev_type
);
3939 list
= skb_shinfo(skb
)->frag_list
;
3941 /* Non fragmented */
3942 BT_DBG("%s nonfrag skb %p len %d", hdev
->name
, skb
, skb
->len
);
3944 skb_queue_tail(queue
, skb
);
3947 BT_DBG("%s frag %p len %d", hdev
->name
, skb
, skb
->len
);
3949 skb_shinfo(skb
)->frag_list
= NULL
;
3951 /* Queue all fragments atomically. We need to use spin_lock_bh
3952 * here because of 6LoWPAN links, as there this function is
3953 * called from softirq and using normal spin lock could cause
3956 spin_lock_bh(&queue
->lock
);
3958 __skb_queue_tail(queue
, skb
);
3960 flags
&= ~ACL_START
;
3963 skb
= list
; list
= list
->next
;
3965 hci_skb_pkt_type(skb
) = HCI_ACLDATA_PKT
;
3966 hci_add_acl_hdr(skb
, conn
->handle
, flags
);
3968 BT_DBG("%s frag %p len %d", hdev
->name
, skb
, skb
->len
);
3970 __skb_queue_tail(queue
, skb
);
3973 spin_unlock_bh(&queue
->lock
);
3977 void hci_send_acl(struct hci_chan
*chan
, struct sk_buff
*skb
, __u16 flags
)
3979 struct hci_dev
*hdev
= chan
->conn
->hdev
;
3981 BT_DBG("%s chan %p flags 0x%4.4x", hdev
->name
, chan
, flags
);
3983 hci_queue_acl(chan
, &chan
->data_q
, skb
, flags
);
3985 queue_work(hdev
->workqueue
, &hdev
->tx_work
);
3989 void hci_send_sco(struct hci_conn
*conn
, struct sk_buff
*skb
)
3991 struct hci_dev
*hdev
= conn
->hdev
;
3992 struct hci_sco_hdr hdr
;
3994 BT_DBG("%s len %d", hdev
->name
, skb
->len
);
3996 hdr
.handle
= cpu_to_le16(conn
->handle
);
3997 hdr
.dlen
= skb
->len
;
3999 skb_push(skb
, HCI_SCO_HDR_SIZE
);
4000 skb_reset_transport_header(skb
);
4001 memcpy(skb_transport_header(skb
), &hdr
, HCI_SCO_HDR_SIZE
);
4003 hci_skb_pkt_type(skb
) = HCI_SCODATA_PKT
;
4005 skb_queue_tail(&conn
->data_q
, skb
);
4006 queue_work(hdev
->workqueue
, &hdev
->tx_work
);
4009 /* ---- HCI TX task (outgoing data) ---- */
4011 /* HCI Connection scheduler */
4012 static struct hci_conn
*hci_low_sent(struct hci_dev
*hdev
, __u8 type
,
4015 struct hci_conn_hash
*h
= &hdev
->conn_hash
;
4016 struct hci_conn
*conn
= NULL
, *c
;
4017 unsigned int num
= 0, min
= ~0;
4019 /* We don't have to lock device here. Connections are always
4020 * added and removed with TX task disabled. */
4024 list_for_each_entry_rcu(c
, &h
->list
, list
) {
4025 if (c
->type
!= type
|| skb_queue_empty(&c
->data_q
))
4028 if (c
->state
!= BT_CONNECTED
&& c
->state
!= BT_CONFIG
)
4033 if (c
->sent
< min
) {
4038 if (hci_conn_num(hdev
, type
) == num
)
4047 switch (conn
->type
) {
4049 cnt
= hdev
->acl_cnt
;
4053 cnt
= hdev
->sco_cnt
;
4056 cnt
= hdev
->le_mtu
? hdev
->le_cnt
: hdev
->acl_cnt
;
4060 bt_dev_err(hdev
, "unknown link type %d", conn
->type
);
4068 BT_DBG("conn %p quote %d", conn
, *quote
);
4072 static void hci_link_tx_to(struct hci_dev
*hdev
, __u8 type
)
4074 struct hci_conn_hash
*h
= &hdev
->conn_hash
;
4077 bt_dev_err(hdev
, "link tx timeout");
4081 /* Kill stalled connections */
4082 list_for_each_entry_rcu(c
, &h
->list
, list
) {
4083 if (c
->type
== type
&& c
->sent
) {
4084 bt_dev_err(hdev
, "killing stalled connection %pMR",
4086 hci_disconnect(c
, HCI_ERROR_REMOTE_USER_TERM
);
4093 static struct hci_chan
*hci_chan_sent(struct hci_dev
*hdev
, __u8 type
,
4096 struct hci_conn_hash
*h
= &hdev
->conn_hash
;
4097 struct hci_chan
*chan
= NULL
;
4098 unsigned int num
= 0, min
= ~0, cur_prio
= 0;
4099 struct hci_conn
*conn
;
4100 int cnt
, q
, conn_num
= 0;
4102 BT_DBG("%s", hdev
->name
);
4106 list_for_each_entry_rcu(conn
, &h
->list
, list
) {
4107 struct hci_chan
*tmp
;
4109 if (conn
->type
!= type
)
4112 if (conn
->state
!= BT_CONNECTED
&& conn
->state
!= BT_CONFIG
)
4117 list_for_each_entry_rcu(tmp
, &conn
->chan_list
, list
) {
4118 struct sk_buff
*skb
;
4120 if (skb_queue_empty(&tmp
->data_q
))
4123 skb
= skb_peek(&tmp
->data_q
);
4124 if (skb
->priority
< cur_prio
)
4127 if (skb
->priority
> cur_prio
) {
4130 cur_prio
= skb
->priority
;
4135 if (conn
->sent
< min
) {
4141 if (hci_conn_num(hdev
, type
) == conn_num
)
4150 switch (chan
->conn
->type
) {
4152 cnt
= hdev
->acl_cnt
;
4155 cnt
= hdev
->block_cnt
;
4159 cnt
= hdev
->sco_cnt
;
4162 cnt
= hdev
->le_mtu
? hdev
->le_cnt
: hdev
->acl_cnt
;
4166 bt_dev_err(hdev
, "unknown link type %d", chan
->conn
->type
);
4171 BT_DBG("chan %p quote %d", chan
, *quote
);
4175 static void hci_prio_recalculate(struct hci_dev
*hdev
, __u8 type
)
4177 struct hci_conn_hash
*h
= &hdev
->conn_hash
;
4178 struct hci_conn
*conn
;
4181 BT_DBG("%s", hdev
->name
);
4185 list_for_each_entry_rcu(conn
, &h
->list
, list
) {
4186 struct hci_chan
*chan
;
4188 if (conn
->type
!= type
)
4191 if (conn
->state
!= BT_CONNECTED
&& conn
->state
!= BT_CONFIG
)
4196 list_for_each_entry_rcu(chan
, &conn
->chan_list
, list
) {
4197 struct sk_buff
*skb
;
4204 if (skb_queue_empty(&chan
->data_q
))
4207 skb
= skb_peek(&chan
->data_q
);
4208 if (skb
->priority
>= HCI_PRIO_MAX
- 1)
4211 skb
->priority
= HCI_PRIO_MAX
- 1;
4213 BT_DBG("chan %p skb %p promoted to %d", chan
, skb
,
4217 if (hci_conn_num(hdev
, type
) == num
)
4225 static inline int __get_blocks(struct hci_dev
*hdev
, struct sk_buff
*skb
)
4227 /* Calculate count of blocks used by this packet */
4228 return DIV_ROUND_UP(skb
->len
- HCI_ACL_HDR_SIZE
, hdev
->block_len
);
4231 static void __check_timeout(struct hci_dev
*hdev
, unsigned int cnt
)
4233 if (!hci_dev_test_flag(hdev
, HCI_UNCONFIGURED
)) {
4234 /* ACL tx timeout must be longer than maximum
4235 * link supervision timeout (40.9 seconds) */
4236 if (!cnt
&& time_after(jiffies
, hdev
->acl_last_tx
+
4237 HCI_ACL_TX_TIMEOUT
))
4238 hci_link_tx_to(hdev
, ACL_LINK
);
4242 static void hci_sched_acl_pkt(struct hci_dev
*hdev
)
4244 unsigned int cnt
= hdev
->acl_cnt
;
4245 struct hci_chan
*chan
;
4246 struct sk_buff
*skb
;
4249 __check_timeout(hdev
, cnt
);
4251 while (hdev
->acl_cnt
&&
4252 (chan
= hci_chan_sent(hdev
, ACL_LINK
, "e
))) {
4253 u32 priority
= (skb_peek(&chan
->data_q
))->priority
;
4254 while (quote
-- && (skb
= skb_peek(&chan
->data_q
))) {
4255 BT_DBG("chan %p skb %p len %d priority %u", chan
, skb
,
4256 skb
->len
, skb
->priority
);
4258 /* Stop if priority has changed */
4259 if (skb
->priority
< priority
)
4262 skb
= skb_dequeue(&chan
->data_q
);
4264 hci_conn_enter_active_mode(chan
->conn
,
4265 bt_cb(skb
)->force_active
);
4267 hci_send_frame(hdev
, skb
);
4268 hdev
->acl_last_tx
= jiffies
;
4276 if (cnt
!= hdev
->acl_cnt
)
4277 hci_prio_recalculate(hdev
, ACL_LINK
);
4280 static void hci_sched_acl_blk(struct hci_dev
*hdev
)
4282 unsigned int cnt
= hdev
->block_cnt
;
4283 struct hci_chan
*chan
;
4284 struct sk_buff
*skb
;
4288 __check_timeout(hdev
, cnt
);
4290 BT_DBG("%s", hdev
->name
);
4292 if (hdev
->dev_type
== HCI_AMP
)
4297 while (hdev
->block_cnt
> 0 &&
4298 (chan
= hci_chan_sent(hdev
, type
, "e
))) {
4299 u32 priority
= (skb_peek(&chan
->data_q
))->priority
;
4300 while (quote
> 0 && (skb
= skb_peek(&chan
->data_q
))) {
4303 BT_DBG("chan %p skb %p len %d priority %u", chan
, skb
,
4304 skb
->len
, skb
->priority
);
4306 /* Stop if priority has changed */
4307 if (skb
->priority
< priority
)
4310 skb
= skb_dequeue(&chan
->data_q
);
4312 blocks
= __get_blocks(hdev
, skb
);
4313 if (blocks
> hdev
->block_cnt
)
4316 hci_conn_enter_active_mode(chan
->conn
,
4317 bt_cb(skb
)->force_active
);
4319 hci_send_frame(hdev
, skb
);
4320 hdev
->acl_last_tx
= jiffies
;
4322 hdev
->block_cnt
-= blocks
;
4325 chan
->sent
+= blocks
;
4326 chan
->conn
->sent
+= blocks
;
4330 if (cnt
!= hdev
->block_cnt
)
4331 hci_prio_recalculate(hdev
, type
);
4334 static void hci_sched_acl(struct hci_dev
*hdev
)
4336 BT_DBG("%s", hdev
->name
);
4338 /* No ACL link over BR/EDR controller */
4339 if (!hci_conn_num(hdev
, ACL_LINK
) && hdev
->dev_type
== HCI_PRIMARY
)
4342 /* No AMP link over AMP controller */
4343 if (!hci_conn_num(hdev
, AMP_LINK
) && hdev
->dev_type
== HCI_AMP
)
4346 switch (hdev
->flow_ctl_mode
) {
4347 case HCI_FLOW_CTL_MODE_PACKET_BASED
:
4348 hci_sched_acl_pkt(hdev
);
4351 case HCI_FLOW_CTL_MODE_BLOCK_BASED
:
4352 hci_sched_acl_blk(hdev
);
4358 static void hci_sched_sco(struct hci_dev
*hdev
)
4360 struct hci_conn
*conn
;
4361 struct sk_buff
*skb
;
4364 BT_DBG("%s", hdev
->name
);
4366 if (!hci_conn_num(hdev
, SCO_LINK
))
4369 while (hdev
->sco_cnt
&& (conn
= hci_low_sent(hdev
, SCO_LINK
, "e
))) {
4370 while (quote
-- && (skb
= skb_dequeue(&conn
->data_q
))) {
4371 BT_DBG("skb %p len %d", skb
, skb
->len
);
4372 hci_send_frame(hdev
, skb
);
4375 if (conn
->sent
== ~0)
4381 static void hci_sched_esco(struct hci_dev
*hdev
)
4383 struct hci_conn
*conn
;
4384 struct sk_buff
*skb
;
4387 BT_DBG("%s", hdev
->name
);
4389 if (!hci_conn_num(hdev
, ESCO_LINK
))
4392 while (hdev
->sco_cnt
&& (conn
= hci_low_sent(hdev
, ESCO_LINK
,
4394 while (quote
-- && (skb
= skb_dequeue(&conn
->data_q
))) {
4395 BT_DBG("skb %p len %d", skb
, skb
->len
);
4396 hci_send_frame(hdev
, skb
);
4399 if (conn
->sent
== ~0)
4405 static void hci_sched_le(struct hci_dev
*hdev
)
4407 struct hci_chan
*chan
;
4408 struct sk_buff
*skb
;
4409 int quote
, cnt
, tmp
;
4411 BT_DBG("%s", hdev
->name
);
4413 if (!hci_conn_num(hdev
, LE_LINK
))
4416 cnt
= hdev
->le_pkts
? hdev
->le_cnt
: hdev
->acl_cnt
;
4418 __check_timeout(hdev
, cnt
);
4421 while (cnt
&& (chan
= hci_chan_sent(hdev
, LE_LINK
, "e
))) {
4422 u32 priority
= (skb_peek(&chan
->data_q
))->priority
;
4423 while (quote
-- && (skb
= skb_peek(&chan
->data_q
))) {
4424 BT_DBG("chan %p skb %p len %d priority %u", chan
, skb
,
4425 skb
->len
, skb
->priority
);
4427 /* Stop if priority has changed */
4428 if (skb
->priority
< priority
)
4431 skb
= skb_dequeue(&chan
->data_q
);
4433 hci_send_frame(hdev
, skb
);
4434 hdev
->le_last_tx
= jiffies
;
4445 hdev
->acl_cnt
= cnt
;
4448 hci_prio_recalculate(hdev
, LE_LINK
);
4451 static void hci_tx_work(struct work_struct
*work
)
4453 struct hci_dev
*hdev
= container_of(work
, struct hci_dev
, tx_work
);
4454 struct sk_buff
*skb
;
4456 BT_DBG("%s acl %d sco %d le %d", hdev
->name
, hdev
->acl_cnt
,
4457 hdev
->sco_cnt
, hdev
->le_cnt
);
4459 if (!hci_dev_test_flag(hdev
, HCI_USER_CHANNEL
)) {
4460 /* Schedule queues and send stuff to HCI driver */
4461 hci_sched_acl(hdev
);
4462 hci_sched_sco(hdev
);
4463 hci_sched_esco(hdev
);
4467 /* Send next queued raw (unknown type) packet */
4468 while ((skb
= skb_dequeue(&hdev
->raw_q
)))
4469 hci_send_frame(hdev
, skb
);
4472 /* ----- HCI RX task (incoming data processing) ----- */
4474 /* ACL data packet */
4475 static void hci_acldata_packet(struct hci_dev
*hdev
, struct sk_buff
*skb
)
4477 struct hci_acl_hdr
*hdr
= (void *) skb
->data
;
4478 struct hci_conn
*conn
;
4479 __u16 handle
, flags
;
4481 skb_pull(skb
, HCI_ACL_HDR_SIZE
);
4483 handle
= __le16_to_cpu(hdr
->handle
);
4484 flags
= hci_flags(handle
);
4485 handle
= hci_handle(handle
);
4487 BT_DBG("%s len %d handle 0x%4.4x flags 0x%4.4x", hdev
->name
, skb
->len
,
4490 hdev
->stat
.acl_rx
++;
4493 conn
= hci_conn_hash_lookup_handle(hdev
, handle
);
4494 hci_dev_unlock(hdev
);
4497 hci_conn_enter_active_mode(conn
, BT_POWER_FORCE_ACTIVE_OFF
);
4499 /* Send to upper protocol */
4500 l2cap_recv_acldata(conn
, skb
, flags
);
4503 bt_dev_err(hdev
, "ACL packet for unknown connection handle %d",
4510 /* SCO data packet */
4511 static void hci_scodata_packet(struct hci_dev
*hdev
, struct sk_buff
*skb
)
4513 struct hci_sco_hdr
*hdr
= (void *) skb
->data
;
4514 struct hci_conn
*conn
;
4515 __u16 handle
, flags
;
4517 skb_pull(skb
, HCI_SCO_HDR_SIZE
);
4519 handle
= __le16_to_cpu(hdr
->handle
);
4520 flags
= hci_flags(handle
);
4521 handle
= hci_handle(handle
);
4523 BT_DBG("%s len %d handle 0x%4.4x flags 0x%4.4x", hdev
->name
, skb
->len
,
4526 hdev
->stat
.sco_rx
++;
4529 conn
= hci_conn_hash_lookup_handle(hdev
, handle
);
4530 hci_dev_unlock(hdev
);
4533 /* Send to upper protocol */
4534 sco_recv_scodata(conn
, skb
);
4537 bt_dev_err(hdev
, "SCO packet for unknown connection handle %d",
4544 static bool hci_req_is_complete(struct hci_dev
*hdev
)
4546 struct sk_buff
*skb
;
4548 skb
= skb_peek(&hdev
->cmd_q
);
4552 return (bt_cb(skb
)->hci
.req_flags
& HCI_REQ_START
);
4555 static void hci_resend_last(struct hci_dev
*hdev
)
4557 struct hci_command_hdr
*sent
;
4558 struct sk_buff
*skb
;
4561 if (!hdev
->sent_cmd
)
4564 sent
= (void *) hdev
->sent_cmd
->data
;
4565 opcode
= __le16_to_cpu(sent
->opcode
);
4566 if (opcode
== HCI_OP_RESET
)
4569 skb
= skb_clone(hdev
->sent_cmd
, GFP_KERNEL
);
4573 skb_queue_head(&hdev
->cmd_q
, skb
);
4574 queue_work(hdev
->workqueue
, &hdev
->cmd_work
);
4577 void hci_req_cmd_complete(struct hci_dev
*hdev
, u16 opcode
, u8 status
,
4578 hci_req_complete_t
*req_complete
,
4579 hci_req_complete_skb_t
*req_complete_skb
)
4581 struct sk_buff
*skb
;
4582 unsigned long flags
;
4584 BT_DBG("opcode 0x%04x status 0x%02x", opcode
, status
);
4586 /* If the completed command doesn't match the last one that was
4587 * sent we need to do special handling of it.
4589 if (!hci_sent_cmd_data(hdev
, opcode
)) {
4590 /* Some CSR based controllers generate a spontaneous
4591 * reset complete event during init and any pending
4592 * command will never be completed. In such a case we
4593 * need to resend whatever was the last sent
4596 if (test_bit(HCI_INIT
, &hdev
->flags
) && opcode
== HCI_OP_RESET
)
4597 hci_resend_last(hdev
);
4602 /* If we reach this point this event matches the last command sent */
4603 hci_dev_clear_flag(hdev
, HCI_CMD_PENDING
);
4605 /* If the command succeeded and there's still more commands in
4606 * this request the request is not yet complete.
4608 if (!status
&& !hci_req_is_complete(hdev
))
4611 /* If this was the last command in a request the complete
4612 * callback would be found in hdev->sent_cmd instead of the
4613 * command queue (hdev->cmd_q).
4615 if (bt_cb(hdev
->sent_cmd
)->hci
.req_flags
& HCI_REQ_SKB
) {
4616 *req_complete_skb
= bt_cb(hdev
->sent_cmd
)->hci
.req_complete_skb
;
4620 if (bt_cb(hdev
->sent_cmd
)->hci
.req_complete
) {
4621 *req_complete
= bt_cb(hdev
->sent_cmd
)->hci
.req_complete
;
4625 /* Remove all pending commands belonging to this request */
4626 spin_lock_irqsave(&hdev
->cmd_q
.lock
, flags
);
4627 while ((skb
= __skb_dequeue(&hdev
->cmd_q
))) {
4628 if (bt_cb(skb
)->hci
.req_flags
& HCI_REQ_START
) {
4629 __skb_queue_head(&hdev
->cmd_q
, skb
);
4633 if (bt_cb(skb
)->hci
.req_flags
& HCI_REQ_SKB
)
4634 *req_complete_skb
= bt_cb(skb
)->hci
.req_complete_skb
;
4636 *req_complete
= bt_cb(skb
)->hci
.req_complete
;
4639 spin_unlock_irqrestore(&hdev
->cmd_q
.lock
, flags
);
4642 static void hci_rx_work(struct work_struct
*work
)
4644 struct hci_dev
*hdev
= container_of(work
, struct hci_dev
, rx_work
);
4645 struct sk_buff
*skb
;
4647 BT_DBG("%s", hdev
->name
);
4649 while ((skb
= skb_dequeue(&hdev
->rx_q
))) {
4650 /* Send copy to monitor */
4651 hci_send_to_monitor(hdev
, skb
);
4653 if (atomic_read(&hdev
->promisc
)) {
4654 /* Send copy to the sockets */
4655 hci_send_to_sock(hdev
, skb
);
4658 /* If the device has been opened in HCI_USER_CHANNEL,
4659 * the userspace has exclusive access to device.
4660 * When device is HCI_INIT, we still need to process
4661 * the data packets to the driver in order
4662 * to complete its setup().
4664 if (hci_dev_test_flag(hdev
, HCI_USER_CHANNEL
) &&
4665 !test_bit(HCI_INIT
, &hdev
->flags
)) {
4670 if (test_bit(HCI_INIT
, &hdev
->flags
)) {
4671 /* Don't process data packets in this states. */
4672 switch (hci_skb_pkt_type(skb
)) {
4673 case HCI_ACLDATA_PKT
:
4674 case HCI_SCODATA_PKT
:
4675 case HCI_ISODATA_PKT
:
4682 switch (hci_skb_pkt_type(skb
)) {
4684 BT_DBG("%s Event packet", hdev
->name
);
4685 hci_event_packet(hdev
, skb
);
4688 case HCI_ACLDATA_PKT
:
4689 BT_DBG("%s ACL data packet", hdev
->name
);
4690 hci_acldata_packet(hdev
, skb
);
4693 case HCI_SCODATA_PKT
:
4694 BT_DBG("%s SCO data packet", hdev
->name
);
4695 hci_scodata_packet(hdev
, skb
);
4705 static void hci_cmd_work(struct work_struct
*work
)
4707 struct hci_dev
*hdev
= container_of(work
, struct hci_dev
, cmd_work
);
4708 struct sk_buff
*skb
;
4710 BT_DBG("%s cmd_cnt %d cmd queued %d", hdev
->name
,
4711 atomic_read(&hdev
->cmd_cnt
), skb_queue_len(&hdev
->cmd_q
));
4713 /* Send queued commands */
4714 if (atomic_read(&hdev
->cmd_cnt
)) {
4715 skb
= skb_dequeue(&hdev
->cmd_q
);
4719 kfree_skb(hdev
->sent_cmd
);
4721 hdev
->sent_cmd
= skb_clone(skb
, GFP_KERNEL
);
4722 if (hdev
->sent_cmd
) {
4723 if (hci_req_status_pend(hdev
))
4724 hci_dev_set_flag(hdev
, HCI_CMD_PENDING
);
4725 atomic_dec(&hdev
->cmd_cnt
);
4726 hci_send_frame(hdev
, skb
);
4727 if (test_bit(HCI_RESET
, &hdev
->flags
))
4728 cancel_delayed_work(&hdev
->cmd_timer
);
4730 schedule_delayed_work(&hdev
->cmd_timer
,
4733 skb_queue_head(&hdev
->cmd_q
, skb
);
4734 queue_work(hdev
->workqueue
, &hdev
->cmd_work
);