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_change_suspend_state(struct hci_dev *hdev,
3309 enum suspended_state next)
3311 hdev->suspend_state_next = next;
3312 set_bit(SUSPEND_PREPARE_NOTIFIER, hdev->suspend_tasks);
3313 queue_work(hdev->req_workqueue, &hdev->suspend_prepare);
3314 return hci_suspend_wait_event(hdev);
3317 static int hci_suspend_notifier(struct notifier_block *nb, unsigned long action,
3320 struct hci_dev *hdev =
3321 container_of(nb, struct hci_dev, suspend_notifier);
3324 /* If powering down, wait for completion. */
3325 if (mgmt_powering_down(hdev)) {
3326 set_bit(SUSPEND_POWERING_DOWN, hdev->suspend_tasks);
3327 ret = hci_suspend_wait_event(hdev);
3332 /* Suspend notifier should only act on events when powered. */
3333 if (!hdev_is_powered(hdev))
3336 if (action == PM_SUSPEND_PREPARE) {
3337 /* Suspend consists of two actions:
3338 * - First, disconnect everything and make the controller not
3339 * connectable (disabling scanning)
3340 * - Second, program event filter/whitelist and enable scan
3342 ret = hci_change_suspend_state(hdev, BT_SUSPEND_DISCONNECT);
3344 /* Only configure whitelist if disconnect succeeded */
3346 ret = hci_change_suspend_state(hdev,
3347 BT_SUSPEND_COMPLETE);
3348 } else if (action == PM_POST_SUSPEND) {
3349 ret = hci_change_suspend_state(hdev, BT_RUNNING);
3352 /* If suspend failed, restore it to running */
3353 if (ret && action == PM_SUSPEND_PREPARE)
3354 hci_change_suspend_state(hdev, BT_RUNNING);
3357 return ret ? notifier_from_errno(-EBUSY) : NOTIFY_STOP;
3360 /* Alloc HCI device */
3361 struct hci_dev *hci_alloc_dev(void)
3363 struct hci_dev *hdev;
3365 hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
3369 hdev->pkt_type = (HCI_DM1 | HCI_DH1 | HCI_HV1);
3370 hdev->esco_type = (ESCO_HV1);
3371 hdev->link_mode = (HCI_LM_ACCEPT);
3372 hdev->num_iac = 0x01; /* One IAC support is mandatory */
3373 hdev->io_capability = 0x03; /* No Input No Output */
3374 hdev->manufacturer = 0xffff; /* Default to internal use */
3375 hdev->inq_tx_power = HCI_TX_POWER_INVALID;
3376 hdev->adv_tx_power = HCI_TX_POWER_INVALID;
3377 hdev->adv_instance_cnt = 0;
3378 hdev->cur_adv_instance = 0x00;
3379 hdev->adv_instance_timeout = 0;
3381 hdev->sniff_max_interval = 800;
3382 hdev->sniff_min_interval = 80;
3384 hdev->le_adv_channel_map = 0x07;
3385 hdev->le_adv_min_interval = 0x0800;
3386 hdev->le_adv_max_interval = 0x0800;
3387 hdev->le_scan_interval = 0x0060;
3388 hdev->le_scan_window = 0x0030;
3389 hdev->le_conn_min_interval = 0x0018;
3390 hdev->le_conn_max_interval = 0x0028;
3391 hdev->le_conn_latency = 0x0000;
3392 hdev->le_supv_timeout = 0x002a;
3393 hdev->le_def_tx_len = 0x001b;
3394 hdev->le_def_tx_time = 0x0148;
3395 hdev->le_max_tx_len = 0x001b;
3396 hdev->le_max_tx_time = 0x0148;
3397 hdev->le_max_rx_len = 0x001b;
3398 hdev->le_max_rx_time = 0x0148;
3399 hdev->le_max_key_size = SMP_MAX_ENC_KEY_SIZE;
3400 hdev->le_min_key_size = SMP_MIN_ENC_KEY_SIZE;
3401 hdev->le_tx_def_phys = HCI_LE_SET_PHY_1M;
3402 hdev->le_rx_def_phys = HCI_LE_SET_PHY_1M;
3403 hdev->le_num_of_adv_sets = HCI_MAX_ADV_INSTANCES;
3405 hdev->rpa_timeout = HCI_DEFAULT_RPA_TIMEOUT;
3406 hdev->discov_interleaved_timeout = DISCOV_INTERLEAVED_TIMEOUT;
3407 hdev->conn_info_min_age = DEFAULT_CONN_INFO_MIN_AGE;
3408 hdev->conn_info_max_age = DEFAULT_CONN_INFO_MAX_AGE;
3409 hdev->auth_payload_timeout = DEFAULT_AUTH_PAYLOAD_TIMEOUT;
3410 hdev->min_enc_key_size = HCI_MIN_ENC_KEY_SIZE;
3412 mutex_init(&hdev->lock);
3413 mutex_init(&hdev->req_lock);
3415 INIT_LIST_HEAD(&hdev->mgmt_pending);
3416 INIT_LIST_HEAD(&hdev->blacklist);
3417 INIT_LIST_HEAD(&hdev->whitelist);
3418 INIT_LIST_HEAD(&hdev->wakeable);
3419 INIT_LIST_HEAD(&hdev->uuids);
3420 INIT_LIST_HEAD(&hdev->link_keys);
3421 INIT_LIST_HEAD(&hdev->long_term_keys);
3422 INIT_LIST_HEAD(&hdev->identity_resolving_keys);
3423 INIT_LIST_HEAD(&hdev->remote_oob_data);
3424 INIT_LIST_HEAD(&hdev->le_white_list);
3425 INIT_LIST_HEAD(&hdev->le_resolv_list);
3426 INIT_LIST_HEAD(&hdev->le_conn_params);
3427 INIT_LIST_HEAD(&hdev->pend_le_conns);
3428 INIT_LIST_HEAD(&hdev->pend_le_reports);
3429 INIT_LIST_HEAD(&hdev->conn_hash.list);
3430 INIT_LIST_HEAD(&hdev->adv_instances);
3431 INIT_LIST_HEAD(&hdev->blocked_keys);
3433 INIT_WORK(&hdev->rx_work, hci_rx_work);
3434 INIT_WORK(&hdev->cmd_work, hci_cmd_work);
3435 INIT_WORK(&hdev->tx_work, hci_tx_work);
3436 INIT_WORK(&hdev->power_on, hci_power_on);
3437 INIT_WORK(&hdev->error_reset, hci_error_reset);
3438 INIT_WORK(&hdev->suspend_prepare, hci_prepare_suspend);
3440 INIT_DELAYED_WORK(&hdev->power_off, hci_power_off);
3442 skb_queue_head_init(&hdev->rx_q);
3443 skb_queue_head_init(&hdev->cmd_q);
3444 skb_queue_head_init(&hdev->raw_q);
3446 init_waitqueue_head(&hdev->req_wait_q);
3447 init_waitqueue_head(&hdev->suspend_wait_q);
3449 INIT_DELAYED_WORK(&hdev->cmd_timer, hci_cmd_timeout);
3451 hci_request_setup(hdev);
3453 hci_init_sysfs(hdev);
3454 discovery_init(hdev);
3458 EXPORT_SYMBOL(hci_alloc_dev);
3460 /* Free HCI device */
3461 void hci_free_dev(struct hci_dev *hdev)
3463 /* will free via device release */
3464 put_device(&hdev->dev);
3466 EXPORT_SYMBOL(hci_free_dev);
3468 /* Register HCI device */
3469 int hci_register_dev(struct hci_dev *hdev)
3473 if (!hdev->open || !hdev->close || !hdev->send)
3476 /* Do not allow HCI_AMP devices to register at index 0,
3477 * so the index can be used as the AMP controller ID.
3479 switch (hdev->dev_type) {
3481 id = ida_simple_get(&hci_index_ida, 0, 0, GFP_KERNEL);
3484 id = ida_simple_get(&hci_index_ida, 1, 0, GFP_KERNEL);
3493 sprintf(hdev->name, "hci%d", id);
3496 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
3498 hdev->workqueue = alloc_ordered_workqueue("%s", WQ_HIGHPRI, hdev->name);
3499 if (!hdev->workqueue) {
3504 hdev->req_workqueue = alloc_ordered_workqueue("%s", WQ_HIGHPRI,
3506 if (!hdev->req_workqueue) {
3507 destroy_workqueue(hdev->workqueue);
3512 if (!IS_ERR_OR_NULL(bt_debugfs))
3513 hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs);
3515 dev_set_name(&hdev->dev, "%s", hdev->name);
3517 error = device_add(&hdev->dev);
3521 hci_leds_init(hdev);
3523 hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev,
3524 RFKILL_TYPE_BLUETOOTH, &hci_rfkill_ops,
3527 if (rfkill_register(hdev->rfkill) < 0) {
3528 rfkill_destroy(hdev->rfkill);
3529 hdev->rfkill = NULL;
3533 if (hdev->rfkill && rfkill_blocked(hdev->rfkill))
3534 hci_dev_set_flag(hdev, HCI_RFKILLED);
3536 hci_dev_set_flag(hdev, HCI_SETUP);
3537 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
3539 if (hdev->dev_type == HCI_PRIMARY) {
3540 /* Assume BR/EDR support until proven otherwise (such as
3541 * through reading supported features during init.
3543 hci_dev_set_flag(hdev, HCI_BREDR_ENABLED);
3546 write_lock(&hci_dev_list_lock);
3547 list_add(&hdev->list, &hci_dev_list);
3548 write_unlock(&hci_dev_list_lock);
3550 /* Devices that are marked for raw-only usage are unconfigured
3551 * and should not be included in normal operation.
3553 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
3554 hci_dev_set_flag(hdev, HCI_UNCONFIGURED);
3556 hci_sock_dev_event(hdev, HCI_DEV_REG);
3559 hdev->suspend_notifier.notifier_call = hci_suspend_notifier;
3560 error = register_pm_notifier(&hdev->suspend_notifier);
3564 queue_work(hdev->req_workqueue, &hdev->power_on);
3569 destroy_workqueue(hdev->workqueue);
3570 destroy_workqueue(hdev->req_workqueue);
3572 ida_simple_remove(&hci_index_ida, hdev->id);
3576 EXPORT_SYMBOL(hci_register_dev);
3578 /* Unregister HCI device */
3579 void hci_unregister_dev(struct hci_dev *hdev)
3583 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
3585 hci_dev_set_flag(hdev, HCI_UNREGISTER);
3589 write_lock(&hci_dev_list_lock);
3590 list_del(&hdev->list);
3591 write_unlock(&hci_dev_list_lock);
3593 cancel_work_sync(&hdev->power_on);
3595 hci_dev_do_close(hdev);
3597 unregister_pm_notifier(&hdev->suspend_notifier);
3599 if (!test_bit(HCI_INIT, &hdev->flags) &&
3600 !hci_dev_test_flag(hdev, HCI_SETUP) &&
3601 !hci_dev_test_flag(hdev, HCI_CONFIG)) {
3603 mgmt_index_removed(hdev);
3604 hci_dev_unlock(hdev);
3607 /* mgmt_index_removed should take care of emptying the
3609 BUG_ON(!list_empty(&hdev->mgmt_pending));
3611 hci_sock_dev_event(hdev, HCI_DEV_UNREG);
3614 rfkill_unregister(hdev->rfkill);
3615 rfkill_destroy(hdev->rfkill);
3618 device_del(&hdev->dev);
3620 debugfs_remove_recursive(hdev->debugfs);
3621 kfree_const(hdev->hw_info);
3622 kfree_const(hdev->fw_info);
3624 destroy_workqueue(hdev->workqueue);
3625 destroy_workqueue(hdev->req_workqueue);
3628 hci_bdaddr_list_clear(&hdev->blacklist);
3629 hci_bdaddr_list_clear(&hdev->whitelist);
3630 hci_uuids_clear(hdev);
3631 hci_link_keys_clear(hdev);
3632 hci_smp_ltks_clear(hdev);
3633 hci_smp_irks_clear(hdev);
3634 hci_remote_oob_data_clear(hdev);
3635 hci_adv_instances_clear(hdev);
3636 hci_bdaddr_list_clear(&hdev->le_white_list);
3637 hci_bdaddr_list_clear(&hdev->le_resolv_list);
3638 hci_conn_params_clear_all(hdev);
3639 hci_discovery_filter_clear(hdev);
3640 hci_blocked_keys_clear(hdev);
3641 hci_dev_unlock(hdev);
3645 ida_simple_remove(&hci_index_ida, id);
3647 EXPORT_SYMBOL(hci_unregister_dev);
3649 /* Suspend HCI device */
3650 int hci_suspend_dev(struct hci_dev *hdev)
3652 hci_sock_dev_event(hdev, HCI_DEV_SUSPEND);
3655 EXPORT_SYMBOL(hci_suspend_dev);
3657 /* Resume HCI device */
3658 int hci_resume_dev(struct hci_dev *hdev)
3660 hci_sock_dev_event(hdev, HCI_DEV_RESUME);
3663 EXPORT_SYMBOL(hci_resume_dev);
3665 /* Reset HCI device */
3666 int hci_reset_dev(struct hci_dev *hdev)
3668 static const u8 hw_err[] = { HCI_EV_HARDWARE_ERROR, 0x01, 0x00 };
3669 struct sk_buff *skb;
3671 skb = bt_skb_alloc(3, GFP_ATOMIC);
3675 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
3676 skb_put_data(skb, hw_err, 3);
3678 /* Send Hardware Error to upper stack */
3679 return hci_recv_frame(hdev, skb);
3681 EXPORT_SYMBOL(hci_reset_dev);
3683 /* Receive frame from HCI drivers */
3684 int hci_recv_frame(struct hci_dev *hdev, struct sk_buff *skb)
3686 if (!hdev || (!test_bit(HCI_UP, &hdev->flags)
3687 && !test_bit(HCI_INIT, &hdev->flags))) {
3692 if (hci_skb_pkt_type(skb) != HCI_EVENT_PKT &&
3693 hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
3694 hci_skb_pkt_type(skb) != HCI_SCODATA_PKT &&
3695 hci_skb_pkt_type(skb) != HCI_ISODATA_PKT) {
3701 bt_cb(skb)->incoming = 1;
3704 __net_timestamp(skb);
3706 skb_queue_tail(&hdev->rx_q, skb);
3707 queue_work(hdev->workqueue, &hdev->rx_work);
3711 EXPORT_SYMBOL(hci_recv_frame);
3713 /* Receive diagnostic message from HCI drivers */
3714 int hci_recv_diag(struct hci_dev *hdev, struct sk_buff *skb)
3716 /* Mark as diagnostic packet */
3717 hci_skb_pkt_type(skb) = HCI_DIAG_PKT;
3720 __net_timestamp(skb);
3722 skb_queue_tail(&hdev->rx_q, skb);
3723 queue_work(hdev->workqueue, &hdev->rx_work);
3727 EXPORT_SYMBOL(hci_recv_diag);
3729 void hci_set_hw_info(struct hci_dev *hdev, const char *fmt, ...)
3733 va_start(vargs, fmt);
3734 kfree_const(hdev->hw_info);
3735 hdev->hw_info = kvasprintf_const(GFP_KERNEL, fmt, vargs);
3738 EXPORT_SYMBOL(hci_set_hw_info);
3740 void hci_set_fw_info(struct hci_dev *hdev, const char *fmt, ...)
3744 va_start(vargs, fmt);
3745 kfree_const(hdev->fw_info);
3746 hdev->fw_info = kvasprintf_const(GFP_KERNEL, fmt, vargs);
3749 EXPORT_SYMBOL(hci_set_fw_info);
3751 /* ---- Interface to upper protocols ---- */
3753 int hci_register_cb(struct hci_cb *cb)
3755 BT_DBG("%p name %s", cb, cb->name);
3757 mutex_lock(&hci_cb_list_lock);
3758 list_add_tail(&cb->list, &hci_cb_list);
3759 mutex_unlock(&hci_cb_list_lock);
3763 EXPORT_SYMBOL(hci_register_cb);
3765 int hci_unregister_cb(struct hci_cb *cb)
3767 BT_DBG("%p name %s", cb, cb->name);
3769 mutex_lock(&hci_cb_list_lock);
3770 list_del(&cb->list);
3771 mutex_unlock(&hci_cb_list_lock);
3775 EXPORT_SYMBOL(hci_unregister_cb);
3777 static void hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
3781 BT_DBG("%s type %d len %d", hdev->name, hci_skb_pkt_type(skb),
3785 __net_timestamp(skb);
3787 /* Send copy to monitor */
3788 hci_send_to_monitor(hdev, skb);
3790 if (atomic_read(&hdev->promisc)) {
3791 /* Send copy to the sockets */
3792 hci_send_to_sock(hdev, skb);
3795 /* Get rid of skb owner, prior to sending to the driver. */
3798 if (!test_bit(HCI_RUNNING, &hdev->flags)) {
3803 err = hdev->send(hdev, skb);
3805 bt_dev_err(hdev, "sending frame failed (%d)", err);
3810 /* Send HCI command */
3811 int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen,
3814 struct sk_buff *skb;
3816 BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen);
3818 skb = hci_prepare_cmd(hdev, opcode, plen, param);
3820 bt_dev_err(hdev, "no memory for command");
3824 /* Stand-alone HCI commands must be flagged as
3825 * single-command requests.
3827 bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
3829 skb_queue_tail(&hdev->cmd_q, skb);
3830 queue_work(hdev->workqueue, &hdev->cmd_work);
3835 int __hci_cmd_send(struct hci_dev *hdev, u16 opcode, u32 plen,
3838 struct sk_buff *skb;
3840 if (hci_opcode_ogf(opcode) != 0x3f) {
3841 /* A controller receiving a command shall respond with either
3842 * a Command Status Event or a Command Complete Event.
3843 * Therefore, all standard HCI commands must be sent via the
3844 * standard API, using hci_send_cmd or hci_cmd_sync helpers.
3845 * Some vendors do not comply with this rule for vendor-specific
3846 * commands and do not return any event. We want to support
3847 * unresponded commands for such cases only.
3849 bt_dev_err(hdev, "unresponded command not supported");
3853 skb = hci_prepare_cmd(hdev, opcode, plen, param);
3855 bt_dev_err(hdev, "no memory for command (opcode 0x%4.4x)",
3860 hci_send_frame(hdev, skb);
3864 EXPORT_SYMBOL(__hci_cmd_send);
3866 /* Get data from the previously sent command */
3867 void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode)
3869 struct hci_command_hdr *hdr;
3871 if (!hdev->sent_cmd)
3874 hdr = (void *) hdev->sent_cmd->data;
3876 if (hdr->opcode != cpu_to_le16(opcode))
3879 BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode);
3881 return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE;
3884 /* Send HCI command and wait for command commplete event */
3885 struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
3886 const void *param, u32 timeout)
3888 struct sk_buff *skb;
3890 if (!test_bit(HCI_UP, &hdev->flags))
3891 return ERR_PTR(-ENETDOWN);
3893 bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
3895 hci_req_sync_lock(hdev);
3896 skb = __hci_cmd_sync(hdev, opcode, plen, param, timeout);
3897 hci_req_sync_unlock(hdev);
3901 EXPORT_SYMBOL(hci_cmd_sync);
3904 static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags)
3906 struct hci_acl_hdr *hdr;
3909 skb_push(skb, HCI_ACL_HDR_SIZE);
3910 skb_reset_transport_header(skb);
3911 hdr = (struct hci_acl_hdr *)skb_transport_header(skb);
3912 hdr->handle = cpu_to_le16(hci_handle_pack(handle, flags));
3913 hdr->dlen = cpu_to_le16(len);
3916 static void hci_queue_acl(struct hci_chan *chan, struct sk_buff_head *queue,
3917 struct sk_buff *skb, __u16 flags)
3919 struct hci_conn *conn = chan->conn;
3920 struct hci_dev *hdev = conn->hdev;
3921 struct sk_buff *list;
3923 skb->len = skb_headlen(skb);
3926 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
3928 switch (hdev->dev_type) {
3930 hci_add_acl_hdr(skb, conn->handle, flags);
3933 hci_add_acl_hdr(skb, chan->handle, flags);
3936 bt_dev_err(hdev, "unknown dev_type %d", hdev->dev_type);
3940 list = skb_shinfo(skb)->frag_list;
3942 /* Non fragmented */
3943 BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len);
3945 skb_queue_tail(queue, skb);
3948 BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
3950 skb_shinfo(skb)->frag_list = NULL;
3952 /* Queue all fragments atomically. We need to use spin_lock_bh
3953 * here because of 6LoWPAN links, as there this function is
3954 * called from softirq and using normal spin lock could cause
3957 spin_lock_bh(&queue->lock);
3959 __skb_queue_tail(queue, skb);
3961 flags &= ~ACL_START;
3964 skb = list; list = list->next;
3966 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
3967 hci_add_acl_hdr(skb, conn->handle, flags);
3969 BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
3971 __skb_queue_tail(queue, skb);
3974 spin_unlock_bh(&queue->lock);
3978 void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags)
3980 struct hci_dev *hdev = chan->conn->hdev;
3982 BT_DBG("%s chan %p flags 0x%4.4x", hdev->name, chan, flags);
3984 hci_queue_acl(chan, &chan->data_q, skb, flags);
3986 queue_work(hdev->workqueue, &hdev->tx_work);
3990 void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb)
3992 struct hci_dev *hdev = conn->hdev;
3993 struct hci_sco_hdr hdr;
3995 BT_DBG("%s len %d", hdev->name, skb->len);
3997 hdr.handle = cpu_to_le16(conn->handle);
3998 hdr.dlen = skb->len;
4000 skb_push(skb, HCI_SCO_HDR_SIZE);
4001 skb_reset_transport_header(skb);
4002 memcpy(skb_transport_header(skb), &hdr, HCI_SCO_HDR_SIZE);
4004 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
4006 skb_queue_tail(&conn->data_q, skb);
4007 queue_work(hdev->workqueue, &hdev->tx_work);
4010 /* ---- HCI TX task (outgoing data) ---- */
4012 /* HCI Connection scheduler */
4013 static struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type,
4016 struct hci_conn_hash *h = &hdev->conn_hash;
4017 struct hci_conn *conn = NULL, *c;
4018 unsigned int num = 0, min = ~0;
4020 /* We don't have to lock device here. Connections are always
4021 * added and removed with TX task disabled. */
4025 list_for_each_entry_rcu(c, &h->list, list) {
4026 if (c->type != type || skb_queue_empty(&c->data_q))
4029 if (c->state != BT_CONNECTED && c->state != BT_CONFIG)
4034 if (c->sent < min) {
4039 if (hci_conn_num(hdev, type) == num)
4048 switch (conn->type) {
4050 cnt = hdev->acl_cnt;
4054 cnt = hdev->sco_cnt;
4057 cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt;
4061 bt_dev_err(hdev, "unknown link type %d", conn->type);
4069 BT_DBG("conn %p quote %d", conn, *quote);
4073 static void hci_link_tx_to(struct hci_dev *hdev, __u8 type)
4075 struct hci_conn_hash *h = &hdev->conn_hash;
4078 bt_dev_err(hdev, "link tx timeout");
4082 /* Kill stalled connections */
4083 list_for_each_entry_rcu(c, &h->list, list) {
4084 if (c->type == type && c->sent) {
4085 bt_dev_err(hdev, "killing stalled connection %pMR",
4087 hci_disconnect(c, HCI_ERROR_REMOTE_USER_TERM);
4094 static struct hci_chan *hci_chan_sent(struct hci_dev *hdev, __u8 type,
4097 struct hci_conn_hash *h = &hdev->conn_hash;
4098 struct hci_chan *chan = NULL;
4099 unsigned int num = 0, min = ~0, cur_prio = 0;
4100 struct hci_conn *conn;
4101 int cnt, q, conn_num = 0;
4103 BT_DBG("%s", hdev->name);
4107 list_for_each_entry_rcu(conn, &h->list, list) {
4108 struct hci_chan *tmp;
4110 if (conn->type != type)
4113 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
4118 list_for_each_entry_rcu(tmp, &conn->chan_list, list) {
4119 struct sk_buff *skb;
4121 if (skb_queue_empty(&tmp->data_q))
4124 skb = skb_peek(&tmp->data_q);
4125 if (skb->priority < cur_prio)
4128 if (skb->priority > cur_prio) {
4131 cur_prio = skb->priority;
4136 if (conn->sent < min) {
4142 if (hci_conn_num(hdev, type) == conn_num)
4151 switch (chan->conn->type) {
4153 cnt = hdev->acl_cnt;
4156 cnt = hdev->block_cnt;
4160 cnt = hdev->sco_cnt;
4163 cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt;
4167 bt_dev_err(hdev, "unknown link type %d", chan->conn->type);
4172 BT_DBG("chan %p quote %d", chan, *quote);
4176 static void hci_prio_recalculate(struct hci_dev *hdev, __u8 type)
4178 struct hci_conn_hash *h = &hdev->conn_hash;
4179 struct hci_conn *conn;
4182 BT_DBG("%s", hdev->name);
4186 list_for_each_entry_rcu(conn, &h->list, list) {
4187 struct hci_chan *chan;
4189 if (conn->type != type)
4192 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
4197 list_for_each_entry_rcu(chan, &conn->chan_list, list) {
4198 struct sk_buff *skb;
4205 if (skb_queue_empty(&chan->data_q))
4208 skb = skb_peek(&chan->data_q);
4209 if (skb->priority >= HCI_PRIO_MAX - 1)
4212 skb->priority = HCI_PRIO_MAX - 1;
4214 BT_DBG("chan %p skb %p promoted to %d", chan, skb,
4218 if (hci_conn_num(hdev, type) == num)
4226 static inline int __get_blocks(struct hci_dev *hdev, struct sk_buff *skb)
4228 /* Calculate count of blocks used by this packet */
4229 return DIV_ROUND_UP(skb->len - HCI_ACL_HDR_SIZE, hdev->block_len);
4232 static void __check_timeout(struct hci_dev *hdev, unsigned int cnt)
4234 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
4235 /* ACL tx timeout must be longer than maximum
4236 * link supervision timeout (40.9 seconds) */
4237 if (!cnt && time_after(jiffies, hdev->acl_last_tx +
4238 HCI_ACL_TX_TIMEOUT))
4239 hci_link_tx_to(hdev, ACL_LINK);
4243 static void hci_sched_acl_pkt(struct hci_dev *hdev)
4245 unsigned int cnt = hdev->acl_cnt;
4246 struct hci_chan *chan;
4247 struct sk_buff *skb;
4250 __check_timeout(hdev, cnt);
4252 while (hdev->acl_cnt &&
4253 (chan = hci_chan_sent(hdev, ACL_LINK, "e))) {
4254 u32 priority = (skb_peek(&chan->data_q))->priority;
4255 while (quote-- && (skb = skb_peek(&chan->data_q))) {
4256 BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
4257 skb->len, skb->priority);
4259 /* Stop if priority has changed */
4260 if (skb->priority < priority)
4263 skb = skb_dequeue(&chan->data_q);
4265 hci_conn_enter_active_mode(chan->conn,
4266 bt_cb(skb)->force_active);
4268 hci_send_frame(hdev, skb);
4269 hdev->acl_last_tx = jiffies;
4277 if (cnt != hdev->acl_cnt)
4278 hci_prio_recalculate(hdev, ACL_LINK);
4281 static void hci_sched_acl_blk(struct hci_dev *hdev)
4283 unsigned int cnt = hdev->block_cnt;
4284 struct hci_chan *chan;
4285 struct sk_buff *skb;
4289 __check_timeout(hdev, cnt);
4291 BT_DBG("%s", hdev->name);
4293 if (hdev->dev_type == HCI_AMP)
4298 while (hdev->block_cnt > 0 &&
4299 (chan = hci_chan_sent(hdev, type, "e))) {
4300 u32 priority = (skb_peek(&chan->data_q))->priority;
4301 while (quote > 0 && (skb = skb_peek(&chan->data_q))) {
4304 BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
4305 skb->len, skb->priority);
4307 /* Stop if priority has changed */
4308 if (skb->priority < priority)
4311 skb = skb_dequeue(&chan->data_q);
4313 blocks = __get_blocks(hdev, skb);
4314 if (blocks > hdev->block_cnt)
4317 hci_conn_enter_active_mode(chan->conn,
4318 bt_cb(skb)->force_active);
4320 hci_send_frame(hdev, skb);
4321 hdev->acl_last_tx = jiffies;
4323 hdev->block_cnt -= blocks;
4326 chan->sent += blocks;
4327 chan->conn->sent += blocks;
4331 if (cnt != hdev->block_cnt)
4332 hci_prio_recalculate(hdev, type);
4335 static void hci_sched_acl(struct hci_dev *hdev)
4337 BT_DBG("%s", hdev->name);
4339 /* No ACL link over BR/EDR controller */
4340 if (!hci_conn_num(hdev, ACL_LINK) && hdev->dev_type == HCI_PRIMARY)
4343 /* No AMP link over AMP controller */
4344 if (!hci_conn_num(hdev, AMP_LINK) && hdev->dev_type == HCI_AMP)
4347 switch (hdev->flow_ctl_mode) {
4348 case HCI_FLOW_CTL_MODE_PACKET_BASED:
4349 hci_sched_acl_pkt(hdev);
4352 case HCI_FLOW_CTL_MODE_BLOCK_BASED:
4353 hci_sched_acl_blk(hdev);
4359 static void hci_sched_sco(struct hci_dev *hdev)
4361 struct hci_conn *conn;
4362 struct sk_buff *skb;
4365 BT_DBG("%s", hdev->name);
4367 if (!hci_conn_num(hdev, SCO_LINK))
4370 while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, "e))) {
4371 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
4372 BT_DBG("skb %p len %d", skb, skb->len);
4373 hci_send_frame(hdev, skb);
4376 if (conn->sent == ~0)
4382 static void hci_sched_esco(struct hci_dev *hdev)
4384 struct hci_conn *conn;
4385 struct sk_buff *skb;
4388 BT_DBG("%s", hdev->name);
4390 if (!hci_conn_num(hdev, ESCO_LINK))
4393 while (hdev->sco_cnt && (conn = hci_low_sent(hdev, ESCO_LINK,
4395 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
4396 BT_DBG("skb %p len %d", skb, skb->len);
4397 hci_send_frame(hdev, skb);
4400 if (conn->sent == ~0)
4406 static void hci_sched_le(struct hci_dev *hdev)
4408 struct hci_chan *chan;
4409 struct sk_buff *skb;
4410 int quote, cnt, tmp;
4412 BT_DBG("%s", hdev->name);
4414 if (!hci_conn_num(hdev, LE_LINK))
4417 cnt = hdev->le_pkts ? hdev->le_cnt : hdev->acl_cnt;
4419 __check_timeout(hdev, cnt);
4422 while (cnt && (chan = hci_chan_sent(hdev, LE_LINK, "e))) {
4423 u32 priority = (skb_peek(&chan->data_q))->priority;
4424 while (quote-- && (skb = skb_peek(&chan->data_q))) {
4425 BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
4426 skb->len, skb->priority);
4428 /* Stop if priority has changed */
4429 if (skb->priority < priority)
4432 skb = skb_dequeue(&chan->data_q);
4434 hci_send_frame(hdev, skb);
4435 hdev->le_last_tx = jiffies;
4446 hdev->acl_cnt = cnt;
4449 hci_prio_recalculate(hdev, LE_LINK);
4452 static void hci_tx_work(struct work_struct *work)
4454 struct hci_dev *hdev = container_of(work, struct hci_dev, tx_work);
4455 struct sk_buff *skb;
4457 BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt,
4458 hdev->sco_cnt, hdev->le_cnt);
4460 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4461 /* Schedule queues and send stuff to HCI driver */
4462 hci_sched_acl(hdev);
4463 hci_sched_sco(hdev);
4464 hci_sched_esco(hdev);
4468 /* Send next queued raw (unknown type) packet */
4469 while ((skb = skb_dequeue(&hdev->raw_q)))
4470 hci_send_frame(hdev, skb);
4473 /* ----- HCI RX task (incoming data processing) ----- */
4475 /* ACL data packet */
4476 static void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb)
4478 struct hci_acl_hdr *hdr = (void *) skb->data;
4479 struct hci_conn *conn;
4480 __u16 handle, flags;
4482 skb_pull(skb, HCI_ACL_HDR_SIZE);
4484 handle = __le16_to_cpu(hdr->handle);
4485 flags = hci_flags(handle);
4486 handle = hci_handle(handle);
4488 BT_DBG("%s len %d handle 0x%4.4x flags 0x%4.4x", hdev->name, skb->len,
4491 hdev->stat.acl_rx++;
4494 conn = hci_conn_hash_lookup_handle(hdev, handle);
4495 hci_dev_unlock(hdev);
4498 hci_conn_enter_active_mode(conn, BT_POWER_FORCE_ACTIVE_OFF);
4500 /* Send to upper protocol */
4501 l2cap_recv_acldata(conn, skb, flags);
4504 bt_dev_err(hdev, "ACL packet for unknown connection handle %d",
4511 /* SCO data packet */
4512 static void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb)
4514 struct hci_sco_hdr *hdr = (void *) skb->data;
4515 struct hci_conn *conn;
4516 __u16 handle, flags;
4518 skb_pull(skb, HCI_SCO_HDR_SIZE);
4520 handle = __le16_to_cpu(hdr->handle);
4521 flags = hci_flags(handle);
4522 handle = hci_handle(handle);
4524 BT_DBG("%s len %d handle 0x%4.4x flags 0x%4.4x", hdev->name, skb->len,
4527 hdev->stat.sco_rx++;
4530 conn = hci_conn_hash_lookup_handle(hdev, handle);
4531 hci_dev_unlock(hdev);
4534 /* Send to upper protocol */
4535 sco_recv_scodata(conn, skb);
4538 bt_dev_err(hdev, "SCO packet for unknown connection handle %d",
4545 static bool hci_req_is_complete(struct hci_dev *hdev)
4547 struct sk_buff *skb;
4549 skb = skb_peek(&hdev->cmd_q);
4553 return (bt_cb(skb)->hci.req_flags & HCI_REQ_START);
4556 static void hci_resend_last(struct hci_dev *hdev)
4558 struct hci_command_hdr *sent;
4559 struct sk_buff *skb;
4562 if (!hdev->sent_cmd)
4565 sent = (void *) hdev->sent_cmd->data;
4566 opcode = __le16_to_cpu(sent->opcode);
4567 if (opcode == HCI_OP_RESET)
4570 skb = skb_clone(hdev->sent_cmd, GFP_KERNEL);
4574 skb_queue_head(&hdev->cmd_q, skb);
4575 queue_work(hdev->workqueue, &hdev->cmd_work);
4578 void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status,
4579 hci_req_complete_t *req_complete,
4580 hci_req_complete_skb_t *req_complete_skb)
4582 struct sk_buff *skb;
4583 unsigned long flags;
4585 BT_DBG("opcode 0x%04x status 0x%02x", opcode, status);
4587 /* If the completed command doesn't match the last one that was
4588 * sent we need to do special handling of it.
4590 if (!hci_sent_cmd_data(hdev, opcode)) {
4591 /* Some CSR based controllers generate a spontaneous
4592 * reset complete event during init and any pending
4593 * command will never be completed. In such a case we
4594 * need to resend whatever was the last sent
4597 if (test_bit(HCI_INIT, &hdev->flags) && opcode == HCI_OP_RESET)
4598 hci_resend_last(hdev);
4603 /* If we reach this point this event matches the last command sent */
4604 hci_dev_clear_flag(hdev, HCI_CMD_PENDING);
4606 /* If the command succeeded and there's still more commands in
4607 * this request the request is not yet complete.
4609 if (!status && !hci_req_is_complete(hdev))
4612 /* If this was the last command in a request the complete
4613 * callback would be found in hdev->sent_cmd instead of the
4614 * command queue (hdev->cmd_q).
4616 if (bt_cb(hdev->sent_cmd)->hci.req_flags & HCI_REQ_SKB) {
4617 *req_complete_skb = bt_cb(hdev->sent_cmd)->hci.req_complete_skb;
4621 if (bt_cb(hdev->sent_cmd)->hci.req_complete) {
4622 *req_complete = bt_cb(hdev->sent_cmd)->hci.req_complete;
4626 /* Remove all pending commands belonging to this request */
4627 spin_lock_irqsave(&hdev->cmd_q.lock, flags);
4628 while ((skb = __skb_dequeue(&hdev->cmd_q))) {
4629 if (bt_cb(skb)->hci.req_flags & HCI_REQ_START) {
4630 __skb_queue_head(&hdev->cmd_q, skb);
4634 if (bt_cb(skb)->hci.req_flags & HCI_REQ_SKB)
4635 *req_complete_skb = bt_cb(skb)->hci.req_complete_skb;
4637 *req_complete = bt_cb(skb)->hci.req_complete;
4640 spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
4643 static void hci_rx_work(struct work_struct *work)
4645 struct hci_dev *hdev = container_of(work, struct hci_dev, rx_work);
4646 struct sk_buff *skb;
4648 BT_DBG("%s", hdev->name);
4650 while ((skb = skb_dequeue(&hdev->rx_q))) {
4651 /* Send copy to monitor */
4652 hci_send_to_monitor(hdev, skb);
4654 if (atomic_read(&hdev->promisc)) {
4655 /* Send copy to the sockets */
4656 hci_send_to_sock(hdev, skb);
4659 /* If the device has been opened in HCI_USER_CHANNEL,
4660 * the userspace has exclusive access to device.
4661 * When device is HCI_INIT, we still need to process
4662 * the data packets to the driver in order
4663 * to complete its setup().
4665 if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4666 !test_bit(HCI_INIT, &hdev->flags)) {
4671 if (test_bit(HCI_INIT, &hdev->flags)) {
4672 /* Don't process data packets in this states. */
4673 switch (hci_skb_pkt_type(skb)) {
4674 case HCI_ACLDATA_PKT:
4675 case HCI_SCODATA_PKT:
4676 case HCI_ISODATA_PKT:
4683 switch (hci_skb_pkt_type(skb)) {
4685 BT_DBG("%s Event packet", hdev->name);
4686 hci_event_packet(hdev, skb);
4689 case HCI_ACLDATA_PKT:
4690 BT_DBG("%s ACL data packet", hdev->name);
4691 hci_acldata_packet(hdev, skb);
4694 case HCI_SCODATA_PKT:
4695 BT_DBG("%s SCO data packet", hdev->name);
4696 hci_scodata_packet(hdev, skb);
4706 static void hci_cmd_work(struct work_struct *work)
4708 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_work);
4709 struct sk_buff *skb;
4711 BT_DBG("%s cmd_cnt %d cmd queued %d", hdev->name,
4712 atomic_read(&hdev->cmd_cnt), skb_queue_len(&hdev->cmd_q));
4714 /* Send queued commands */
4715 if (atomic_read(&hdev->cmd_cnt)) {
4716 skb = skb_dequeue(&hdev->cmd_q);
4720 kfree_skb(hdev->sent_cmd);
4722 hdev->sent_cmd = skb_clone(skb, GFP_KERNEL);
4723 if (hdev->sent_cmd) {
4724 if (hci_req_status_pend(hdev))
4725 hci_dev_set_flag(hdev, HCI_CMD_PENDING);
4726 atomic_dec(&hdev->cmd_cnt);
4727 hci_send_frame(hdev, skb);
4728 if (test_bit(HCI_RESET, &hdev->flags))
4729 cancel_delayed_work(&hdev->cmd_timer);
4731 schedule_delayed_work(&hdev->cmd_timer,
4734 skb_queue_head(&hdev->cmd_q, skb);
4735 queue_work(hdev->workqueue, &hdev->cmd_work);