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 <asm/unaligned.h>
35 #include <net/bluetooth/bluetooth.h>
36 #include <net/bluetooth/hci_core.h>
37 #include <net/bluetooth/l2cap.h>
38 #include <net/bluetooth/mgmt.h>
40 #include "hci_request.h"
41 #include "hci_debugfs.h"
45 static void hci_rx_work(struct work_struct *work);
46 static void hci_cmd_work(struct work_struct *work);
47 static void hci_tx_work(struct work_struct *work);
50 LIST_HEAD(hci_dev_list);
51 DEFINE_RWLOCK(hci_dev_list_lock);
53 /* HCI callback list */
54 LIST_HEAD(hci_cb_list);
55 DEFINE_MUTEX(hci_cb_list_lock);
57 /* HCI ID Numbering */
58 static DEFINE_IDA(hci_index_ida);
60 /* ---- HCI debugfs entries ---- */
62 static ssize_t dut_mode_read(struct file *file, char __user *user_buf,
63 size_t count, loff_t *ppos)
65 struct hci_dev *hdev = file->private_data;
68 buf[0] = hci_dev_test_flag(hdev, HCI_DUT_MODE) ? 'Y' : 'N';
71 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
74 static ssize_t dut_mode_write(struct file *file, const char __user *user_buf,
75 size_t count, loff_t *ppos)
77 struct hci_dev *hdev = file->private_data;
82 if (!test_bit(HCI_UP, &hdev->flags))
85 err = kstrtobool_from_user(user_buf, count, &enable);
89 if (enable == hci_dev_test_flag(hdev, HCI_DUT_MODE))
92 hci_req_sync_lock(hdev);
94 skb = __hci_cmd_sync(hdev, HCI_OP_ENABLE_DUT_MODE, 0, NULL,
97 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL,
99 hci_req_sync_unlock(hdev);
106 hci_dev_change_flag(hdev, HCI_DUT_MODE);
111 static const struct file_operations dut_mode_fops = {
113 .read = dut_mode_read,
114 .write = dut_mode_write,
115 .llseek = default_llseek,
118 static ssize_t vendor_diag_read(struct file *file, char __user *user_buf,
119 size_t count, loff_t *ppos)
121 struct hci_dev *hdev = file->private_data;
124 buf[0] = hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) ? 'Y' : 'N';
127 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
130 static ssize_t vendor_diag_write(struct file *file, const char __user *user_buf,
131 size_t count, loff_t *ppos)
133 struct hci_dev *hdev = file->private_data;
137 err = kstrtobool_from_user(user_buf, count, &enable);
141 /* When the diagnostic flags are not persistent and the transport
142 * is not active or in user channel operation, then there is no need
143 * for the vendor callback. Instead just store the desired value and
144 * the setting will be programmed when the controller gets powered on.
146 if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) &&
147 (!test_bit(HCI_RUNNING, &hdev->flags) ||
148 hci_dev_test_flag(hdev, HCI_USER_CHANNEL)))
151 hci_req_sync_lock(hdev);
152 err = hdev->set_diag(hdev, enable);
153 hci_req_sync_unlock(hdev);
160 hci_dev_set_flag(hdev, HCI_VENDOR_DIAG);
162 hci_dev_clear_flag(hdev, HCI_VENDOR_DIAG);
167 static const struct file_operations vendor_diag_fops = {
169 .read = vendor_diag_read,
170 .write = vendor_diag_write,
171 .llseek = default_llseek,
174 static void hci_debugfs_create_basic(struct hci_dev *hdev)
176 debugfs_create_file("dut_mode", 0644, hdev->debugfs, hdev,
180 debugfs_create_file("vendor_diag", 0644, hdev->debugfs, hdev,
184 static int hci_reset_req(struct hci_request *req, unsigned long opt)
186 BT_DBG("%s %ld", req->hdev->name, opt);
189 set_bit(HCI_RESET, &req->hdev->flags);
190 hci_req_add(req, HCI_OP_RESET, 0, NULL);
194 static void bredr_init(struct hci_request *req)
196 req->hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED;
198 /* Read Local Supported Features */
199 hci_req_add(req, HCI_OP_READ_LOCAL_FEATURES, 0, NULL);
201 /* Read Local Version */
202 hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL);
204 /* Read BD Address */
205 hci_req_add(req, HCI_OP_READ_BD_ADDR, 0, NULL);
208 static void amp_init1(struct hci_request *req)
210 req->hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED;
212 /* Read Local Version */
213 hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL);
215 /* Read Local Supported Commands */
216 hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL);
218 /* Read Local AMP Info */
219 hci_req_add(req, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL);
221 /* Read Data Blk size */
222 hci_req_add(req, HCI_OP_READ_DATA_BLOCK_SIZE, 0, NULL);
224 /* Read Flow Control Mode */
225 hci_req_add(req, HCI_OP_READ_FLOW_CONTROL_MODE, 0, NULL);
227 /* Read Location Data */
228 hci_req_add(req, HCI_OP_READ_LOCATION_DATA, 0, NULL);
231 static int amp_init2(struct hci_request *req)
233 /* Read Local Supported Features. Not all AMP controllers
234 * support this so it's placed conditionally in the second
237 if (req->hdev->commands[14] & 0x20)
238 hci_req_add(req, HCI_OP_READ_LOCAL_FEATURES, 0, NULL);
243 static int hci_init1_req(struct hci_request *req, unsigned long opt)
245 struct hci_dev *hdev = req->hdev;
247 BT_DBG("%s %ld", hdev->name, opt);
250 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks))
251 hci_reset_req(req, 0);
253 switch (hdev->dev_type) {
261 bt_dev_err(hdev, "Unknown device type %d", hdev->dev_type);
268 static void bredr_setup(struct hci_request *req)
273 /* Read Buffer Size (ACL mtu, max pkt, etc.) */
274 hci_req_add(req, HCI_OP_READ_BUFFER_SIZE, 0, NULL);
276 /* Read Class of Device */
277 hci_req_add(req, HCI_OP_READ_CLASS_OF_DEV, 0, NULL);
279 /* Read Local Name */
280 hci_req_add(req, HCI_OP_READ_LOCAL_NAME, 0, NULL);
282 /* Read Voice Setting */
283 hci_req_add(req, HCI_OP_READ_VOICE_SETTING, 0, NULL);
285 /* Read Number of Supported IAC */
286 hci_req_add(req, HCI_OP_READ_NUM_SUPPORTED_IAC, 0, NULL);
288 /* Read Current IAC LAP */
289 hci_req_add(req, HCI_OP_READ_CURRENT_IAC_LAP, 0, NULL);
291 /* Clear Event Filters */
292 flt_type = HCI_FLT_CLEAR_ALL;
293 hci_req_add(req, HCI_OP_SET_EVENT_FLT, 1, &flt_type);
295 /* Connection accept timeout ~20 secs */
296 param = cpu_to_le16(0x7d00);
297 hci_req_add(req, HCI_OP_WRITE_CA_TIMEOUT, 2, ¶m);
300 static void le_setup(struct hci_request *req)
302 struct hci_dev *hdev = req->hdev;
304 /* Read LE Buffer Size */
305 hci_req_add(req, HCI_OP_LE_READ_BUFFER_SIZE, 0, NULL);
307 /* Read LE Local Supported Features */
308 hci_req_add(req, HCI_OP_LE_READ_LOCAL_FEATURES, 0, NULL);
310 /* Read LE Supported States */
311 hci_req_add(req, HCI_OP_LE_READ_SUPPORTED_STATES, 0, NULL);
313 /* LE-only controllers have LE implicitly enabled */
314 if (!lmp_bredr_capable(hdev))
315 hci_dev_set_flag(hdev, HCI_LE_ENABLED);
318 static void hci_setup_event_mask(struct hci_request *req)
320 struct hci_dev *hdev = req->hdev;
322 /* The second byte is 0xff instead of 0x9f (two reserved bits
323 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
326 u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
328 /* CSR 1.1 dongles does not accept any bitfield so don't try to set
329 * any event mask for pre 1.2 devices.
331 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
334 if (lmp_bredr_capable(hdev)) {
335 events[4] |= 0x01; /* Flow Specification Complete */
337 /* Use a different default for LE-only devices */
338 memset(events, 0, sizeof(events));
339 events[1] |= 0x20; /* Command Complete */
340 events[1] |= 0x40; /* Command Status */
341 events[1] |= 0x80; /* Hardware Error */
343 /* If the controller supports the Disconnect command, enable
344 * the corresponding event. In addition enable packet flow
345 * control related events.
347 if (hdev->commands[0] & 0x20) {
348 events[0] |= 0x10; /* Disconnection Complete */
349 events[2] |= 0x04; /* Number of Completed Packets */
350 events[3] |= 0x02; /* Data Buffer Overflow */
353 /* If the controller supports the Read Remote Version
354 * Information command, enable the corresponding event.
356 if (hdev->commands[2] & 0x80)
357 events[1] |= 0x08; /* Read Remote Version Information
361 if (hdev->le_features[0] & HCI_LE_ENCRYPTION) {
362 events[0] |= 0x80; /* Encryption Change */
363 events[5] |= 0x80; /* Encryption Key Refresh Complete */
367 if (lmp_inq_rssi_capable(hdev) ||
368 test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks))
369 events[4] |= 0x02; /* Inquiry Result with RSSI */
371 if (lmp_ext_feat_capable(hdev))
372 events[4] |= 0x04; /* Read Remote Extended Features Complete */
374 if (lmp_esco_capable(hdev)) {
375 events[5] |= 0x08; /* Synchronous Connection Complete */
376 events[5] |= 0x10; /* Synchronous Connection Changed */
379 if (lmp_sniffsubr_capable(hdev))
380 events[5] |= 0x20; /* Sniff Subrating */
382 if (lmp_pause_enc_capable(hdev))
383 events[5] |= 0x80; /* Encryption Key Refresh Complete */
385 if (lmp_ext_inq_capable(hdev))
386 events[5] |= 0x40; /* Extended Inquiry Result */
388 if (lmp_no_flush_capable(hdev))
389 events[7] |= 0x01; /* Enhanced Flush Complete */
391 if (lmp_lsto_capable(hdev))
392 events[6] |= 0x80; /* Link Supervision Timeout Changed */
394 if (lmp_ssp_capable(hdev)) {
395 events[6] |= 0x01; /* IO Capability Request */
396 events[6] |= 0x02; /* IO Capability Response */
397 events[6] |= 0x04; /* User Confirmation Request */
398 events[6] |= 0x08; /* User Passkey Request */
399 events[6] |= 0x10; /* Remote OOB Data Request */
400 events[6] |= 0x20; /* Simple Pairing Complete */
401 events[7] |= 0x04; /* User Passkey Notification */
402 events[7] |= 0x08; /* Keypress Notification */
403 events[7] |= 0x10; /* Remote Host Supported
404 * Features Notification
408 if (lmp_le_capable(hdev))
409 events[7] |= 0x20; /* LE Meta-Event */
411 hci_req_add(req, HCI_OP_SET_EVENT_MASK, sizeof(events), events);
414 static int hci_init2_req(struct hci_request *req, unsigned long opt)
416 struct hci_dev *hdev = req->hdev;
418 if (hdev->dev_type == HCI_AMP)
419 return amp_init2(req);
421 if (lmp_bredr_capable(hdev))
424 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
426 if (lmp_le_capable(hdev))
429 /* All Bluetooth 1.2 and later controllers should support the
430 * HCI command for reading the local supported commands.
432 * Unfortunately some controllers indicate Bluetooth 1.2 support,
433 * but do not have support for this command. If that is the case,
434 * the driver can quirk the behavior and skip reading the local
435 * supported commands.
437 if (hdev->hci_ver > BLUETOOTH_VER_1_1 &&
438 !test_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks))
439 hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL);
441 if (lmp_ssp_capable(hdev)) {
442 /* When SSP is available, then the host features page
443 * should also be available as well. However some
444 * controllers list the max_page as 0 as long as SSP
445 * has not been enabled. To achieve proper debugging
446 * output, force the minimum max_page to 1 at least.
448 hdev->max_page = 0x01;
450 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
453 hci_req_add(req, HCI_OP_WRITE_SSP_MODE,
454 sizeof(mode), &mode);
456 struct hci_cp_write_eir cp;
458 memset(hdev->eir, 0, sizeof(hdev->eir));
459 memset(&cp, 0, sizeof(cp));
461 hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
465 if (lmp_inq_rssi_capable(hdev) ||
466 test_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks)) {
469 /* If Extended Inquiry Result events are supported, then
470 * they are clearly preferred over Inquiry Result with RSSI
473 mode = lmp_ext_inq_capable(hdev) ? 0x02 : 0x01;
475 hci_req_add(req, HCI_OP_WRITE_INQUIRY_MODE, 1, &mode);
478 if (lmp_inq_tx_pwr_capable(hdev))
479 hci_req_add(req, HCI_OP_READ_INQ_RSP_TX_POWER, 0, NULL);
481 if (lmp_ext_feat_capable(hdev)) {
482 struct hci_cp_read_local_ext_features cp;
485 hci_req_add(req, HCI_OP_READ_LOCAL_EXT_FEATURES,
489 if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) {
491 hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE, sizeof(enable),
498 static void hci_setup_link_policy(struct hci_request *req)
500 struct hci_dev *hdev = req->hdev;
501 struct hci_cp_write_def_link_policy cp;
504 if (lmp_rswitch_capable(hdev))
505 link_policy |= HCI_LP_RSWITCH;
506 if (lmp_hold_capable(hdev))
507 link_policy |= HCI_LP_HOLD;
508 if (lmp_sniff_capable(hdev))
509 link_policy |= HCI_LP_SNIFF;
510 if (lmp_park_capable(hdev))
511 link_policy |= HCI_LP_PARK;
513 cp.policy = cpu_to_le16(link_policy);
514 hci_req_add(req, HCI_OP_WRITE_DEF_LINK_POLICY, sizeof(cp), &cp);
517 static void hci_set_le_support(struct hci_request *req)
519 struct hci_dev *hdev = req->hdev;
520 struct hci_cp_write_le_host_supported cp;
522 /* LE-only devices do not support explicit enablement */
523 if (!lmp_bredr_capable(hdev))
526 memset(&cp, 0, sizeof(cp));
528 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
533 if (cp.le != lmp_host_le_capable(hdev))
534 hci_req_add(req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(cp),
538 static void hci_set_event_mask_page_2(struct hci_request *req)
540 struct hci_dev *hdev = req->hdev;
541 u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
542 bool changed = false;
544 /* If Connectionless Slave Broadcast master role is supported
545 * enable all necessary events for it.
547 if (lmp_csb_master_capable(hdev)) {
548 events[1] |= 0x40; /* Triggered Clock Capture */
549 events[1] |= 0x80; /* Synchronization Train Complete */
550 events[2] |= 0x10; /* Slave Page Response Timeout */
551 events[2] |= 0x20; /* CSB Channel Map Change */
555 /* If Connectionless Slave Broadcast slave role is supported
556 * enable all necessary events for it.
558 if (lmp_csb_slave_capable(hdev)) {
559 events[2] |= 0x01; /* Synchronization Train Received */
560 events[2] |= 0x02; /* CSB Receive */
561 events[2] |= 0x04; /* CSB Timeout */
562 events[2] |= 0x08; /* Truncated Page Complete */
566 /* Enable Authenticated Payload Timeout Expired event if supported */
567 if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING) {
572 /* Some Broadcom based controllers indicate support for Set Event
573 * Mask Page 2 command, but then actually do not support it. Since
574 * the default value is all bits set to zero, the command is only
575 * required if the event mask has to be changed. In case no change
576 * to the event mask is needed, skip this command.
579 hci_req_add(req, HCI_OP_SET_EVENT_MASK_PAGE_2,
580 sizeof(events), events);
583 static int hci_init3_req(struct hci_request *req, unsigned long opt)
585 struct hci_dev *hdev = req->hdev;
588 hci_setup_event_mask(req);
590 if (hdev->commands[6] & 0x20 &&
591 !test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks)) {
592 struct hci_cp_read_stored_link_key cp;
594 bacpy(&cp.bdaddr, BDADDR_ANY);
596 hci_req_add(req, HCI_OP_READ_STORED_LINK_KEY, sizeof(cp), &cp);
599 if (hdev->commands[5] & 0x10)
600 hci_setup_link_policy(req);
602 if (hdev->commands[8] & 0x01)
603 hci_req_add(req, HCI_OP_READ_PAGE_SCAN_ACTIVITY, 0, NULL);
605 /* Some older Broadcom based Bluetooth 1.2 controllers do not
606 * support the Read Page Scan Type command. Check support for
607 * this command in the bit mask of supported commands.
609 if (hdev->commands[13] & 0x01)
610 hci_req_add(req, HCI_OP_READ_PAGE_SCAN_TYPE, 0, NULL);
612 if (lmp_le_capable(hdev)) {
615 memset(events, 0, sizeof(events));
617 if (hdev->le_features[0] & HCI_LE_ENCRYPTION)
618 events[0] |= 0x10; /* LE Long Term Key Request */
620 /* If controller supports the Connection Parameters Request
621 * Link Layer Procedure, enable the corresponding event.
623 if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC)
624 events[0] |= 0x20; /* LE Remote Connection
628 /* If the controller supports the Data Length Extension
629 * feature, enable the corresponding event.
631 if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)
632 events[0] |= 0x40; /* LE Data Length Change */
634 /* If the controller supports Extended Scanner Filter
635 * Policies, enable the correspondig event.
637 if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)
638 events[1] |= 0x04; /* LE Direct Advertising
642 /* If the controller supports Channel Selection Algorithm #2
643 * feature, enable the corresponding event.
645 if (hdev->le_features[1] & HCI_LE_CHAN_SEL_ALG2)
646 events[2] |= 0x08; /* LE Channel Selection
650 /* If the controller supports the LE Set Scan Enable command,
651 * enable the corresponding advertising report event.
653 if (hdev->commands[26] & 0x08)
654 events[0] |= 0x02; /* LE Advertising Report */
656 /* If the controller supports the LE Create Connection
657 * command, enable the corresponding event.
659 if (hdev->commands[26] & 0x10)
660 events[0] |= 0x01; /* LE Connection Complete */
662 /* If the controller supports the LE Connection Update
663 * command, enable the corresponding event.
665 if (hdev->commands[27] & 0x04)
666 events[0] |= 0x04; /* LE Connection Update
670 /* If the controller supports the LE Read Remote Used Features
671 * command, enable the corresponding event.
673 if (hdev->commands[27] & 0x20)
674 events[0] |= 0x08; /* LE Read Remote Used
678 /* If the controller supports the LE Read Local P-256
679 * Public Key command, enable the corresponding event.
681 if (hdev->commands[34] & 0x02)
682 events[0] |= 0x80; /* LE Read Local P-256
683 * Public Key Complete
686 /* If the controller supports the LE Generate DHKey
687 * command, enable the corresponding event.
689 if (hdev->commands[34] & 0x04)
690 events[1] |= 0x01; /* LE Generate DHKey Complete */
692 /* If the controller supports the LE Set Default PHY or
693 * LE Set PHY commands, enable the corresponding event.
695 if (hdev->commands[35] & (0x20 | 0x40))
696 events[1] |= 0x08; /* LE PHY Update Complete */
698 /* If the controller supports LE Set Extended Scan Parameters
699 * and LE Set Extended Scan Enable commands, enable the
700 * corresponding event.
702 if (use_ext_scan(hdev))
703 events[1] |= 0x10; /* LE Extended Advertising
707 /* If the controller supports the LE Extended Create Connection
708 * command, enable the corresponding event.
710 if (use_ext_conn(hdev))
711 events[1] |= 0x02; /* LE Enhanced Connection
715 /* If the controller supports the LE Extended Advertising
716 * command, enable the corresponding event.
718 if (ext_adv_capable(hdev))
719 events[2] |= 0x02; /* LE Advertising Set
723 hci_req_add(req, HCI_OP_LE_SET_EVENT_MASK, sizeof(events),
726 /* Read LE Advertising Channel TX Power */
727 if ((hdev->commands[25] & 0x40) && !ext_adv_capable(hdev)) {
728 /* HCI TS spec forbids mixing of legacy and extended
729 * advertising commands wherein READ_ADV_TX_POWER is
730 * also included. So do not call it if extended adv
731 * is supported otherwise controller will return
732 * COMMAND_DISALLOWED for extended commands.
734 hci_req_add(req, HCI_OP_LE_READ_ADV_TX_POWER, 0, NULL);
737 if (hdev->commands[26] & 0x40) {
738 /* Read LE White List Size */
739 hci_req_add(req, HCI_OP_LE_READ_WHITE_LIST_SIZE,
743 if (hdev->commands[26] & 0x80) {
744 /* Clear LE White List */
745 hci_req_add(req, HCI_OP_LE_CLEAR_WHITE_LIST, 0, NULL);
748 if (hdev->commands[34] & 0x40) {
749 /* Read LE Resolving List Size */
750 hci_req_add(req, HCI_OP_LE_READ_RESOLV_LIST_SIZE,
754 if (hdev->commands[34] & 0x20) {
755 /* Clear LE Resolving List */
756 hci_req_add(req, HCI_OP_LE_CLEAR_RESOLV_LIST, 0, NULL);
759 if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT) {
760 /* Read LE Maximum Data Length */
761 hci_req_add(req, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL);
763 /* Read LE Suggested Default Data Length */
764 hci_req_add(req, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL);
767 if (ext_adv_capable(hdev)) {
768 /* Read LE Number of Supported Advertising Sets */
769 hci_req_add(req, HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS,
773 hci_set_le_support(req);
776 /* Read features beyond page 1 if available */
777 for (p = 2; p < HCI_MAX_PAGES && p <= hdev->max_page; p++) {
778 struct hci_cp_read_local_ext_features cp;
781 hci_req_add(req, HCI_OP_READ_LOCAL_EXT_FEATURES,
788 static int hci_init4_req(struct hci_request *req, unsigned long opt)
790 struct hci_dev *hdev = req->hdev;
792 /* Some Broadcom based Bluetooth controllers do not support the
793 * Delete Stored Link Key command. They are clearly indicating its
794 * absence in the bit mask of supported commands.
796 * Check the supported commands and only if the the command is marked
797 * as supported send it. If not supported assume that the controller
798 * does not have actual support for stored link keys which makes this
799 * command redundant anyway.
801 * Some controllers indicate that they support handling deleting
802 * stored link keys, but they don't. The quirk lets a driver
803 * just disable this command.
805 if (hdev->commands[6] & 0x80 &&
806 !test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks)) {
807 struct hci_cp_delete_stored_link_key cp;
809 bacpy(&cp.bdaddr, BDADDR_ANY);
810 cp.delete_all = 0x01;
811 hci_req_add(req, HCI_OP_DELETE_STORED_LINK_KEY,
815 /* Set event mask page 2 if the HCI command for it is supported */
816 if (hdev->commands[22] & 0x04)
817 hci_set_event_mask_page_2(req);
819 /* Read local codec list if the HCI command is supported */
820 if (hdev->commands[29] & 0x20)
821 hci_req_add(req, HCI_OP_READ_LOCAL_CODECS, 0, NULL);
823 /* Get MWS transport configuration if the HCI command is supported */
824 if (hdev->commands[30] & 0x08)
825 hci_req_add(req, HCI_OP_GET_MWS_TRANSPORT_CONFIG, 0, NULL);
827 /* Check for Synchronization Train support */
828 if (lmp_sync_train_capable(hdev))
829 hci_req_add(req, HCI_OP_READ_SYNC_TRAIN_PARAMS, 0, NULL);
831 /* Enable Secure Connections if supported and configured */
832 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED) &&
833 bredr_sc_enabled(hdev)) {
836 hci_req_add(req, HCI_OP_WRITE_SC_SUPPORT,
837 sizeof(support), &support);
840 /* Set Suggested Default Data Length to maximum if supported */
841 if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT) {
842 struct hci_cp_le_write_def_data_len cp;
844 cp.tx_len = cpu_to_le16(hdev->le_max_tx_len);
845 cp.tx_time = cpu_to_le16(hdev->le_max_tx_time);
846 hci_req_add(req, HCI_OP_LE_WRITE_DEF_DATA_LEN, sizeof(cp), &cp);
849 /* Set Default PHY parameters if command is supported */
850 if (hdev->commands[35] & 0x20) {
851 struct hci_cp_le_set_default_phy cp;
854 cp.tx_phys = hdev->le_tx_def_phys;
855 cp.rx_phys = hdev->le_rx_def_phys;
857 hci_req_add(req, HCI_OP_LE_SET_DEFAULT_PHY, sizeof(cp), &cp);
863 static int __hci_init(struct hci_dev *hdev)
867 err = __hci_req_sync(hdev, hci_init1_req, 0, HCI_INIT_TIMEOUT, NULL);
871 if (hci_dev_test_flag(hdev, HCI_SETUP))
872 hci_debugfs_create_basic(hdev);
874 err = __hci_req_sync(hdev, hci_init2_req, 0, HCI_INIT_TIMEOUT, NULL);
878 /* HCI_PRIMARY covers both single-mode LE, BR/EDR and dual-mode
879 * BR/EDR/LE type controllers. AMP controllers only need the
880 * first two stages of init.
882 if (hdev->dev_type != HCI_PRIMARY)
885 err = __hci_req_sync(hdev, hci_init3_req, 0, HCI_INIT_TIMEOUT, NULL);
889 err = __hci_req_sync(hdev, hci_init4_req, 0, HCI_INIT_TIMEOUT, NULL);
893 /* This function is only called when the controller is actually in
894 * configured state. When the controller is marked as unconfigured,
895 * this initialization procedure is not run.
897 * It means that it is possible that a controller runs through its
898 * setup phase and then discovers missing settings. If that is the
899 * case, then this function will not be called. It then will only
900 * be called during the config phase.
902 * So only when in setup phase or config phase, create the debugfs
903 * entries and register the SMP channels.
905 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
906 !hci_dev_test_flag(hdev, HCI_CONFIG))
909 hci_debugfs_create_common(hdev);
911 if (lmp_bredr_capable(hdev))
912 hci_debugfs_create_bredr(hdev);
914 if (lmp_le_capable(hdev))
915 hci_debugfs_create_le(hdev);
920 static int hci_init0_req(struct hci_request *req, unsigned long opt)
922 struct hci_dev *hdev = req->hdev;
924 BT_DBG("%s %ld", hdev->name, opt);
927 if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks))
928 hci_reset_req(req, 0);
930 /* Read Local Version */
931 hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL);
933 /* Read BD Address */
934 if (hdev->set_bdaddr)
935 hci_req_add(req, HCI_OP_READ_BD_ADDR, 0, NULL);
940 static int __hci_unconf_init(struct hci_dev *hdev)
944 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
947 err = __hci_req_sync(hdev, hci_init0_req, 0, HCI_INIT_TIMEOUT, NULL);
951 if (hci_dev_test_flag(hdev, HCI_SETUP))
952 hci_debugfs_create_basic(hdev);
957 static int hci_scan_req(struct hci_request *req, unsigned long opt)
961 BT_DBG("%s %x", req->hdev->name, scan);
963 /* Inquiry and Page scans */
964 hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
968 static int hci_auth_req(struct hci_request *req, unsigned long opt)
972 BT_DBG("%s %x", req->hdev->name, auth);
975 hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE, 1, &auth);
979 static int hci_encrypt_req(struct hci_request *req, unsigned long opt)
983 BT_DBG("%s %x", req->hdev->name, encrypt);
986 hci_req_add(req, HCI_OP_WRITE_ENCRYPT_MODE, 1, &encrypt);
990 static int hci_linkpol_req(struct hci_request *req, unsigned long opt)
992 __le16 policy = cpu_to_le16(opt);
994 BT_DBG("%s %x", req->hdev->name, policy);
996 /* Default link policy */
997 hci_req_add(req, HCI_OP_WRITE_DEF_LINK_POLICY, 2, &policy);
1001 /* Get HCI device by index.
1002 * Device is held on return. */
1003 struct hci_dev *hci_dev_get(int index)
1005 struct hci_dev *hdev = NULL, *d;
1007 BT_DBG("%d", index);
1012 read_lock(&hci_dev_list_lock);
1013 list_for_each_entry(d, &hci_dev_list, list) {
1014 if (d->id == index) {
1015 hdev = hci_dev_hold(d);
1019 read_unlock(&hci_dev_list_lock);
1023 /* ---- Inquiry support ---- */
1025 bool hci_discovery_active(struct hci_dev *hdev)
1027 struct discovery_state *discov = &hdev->discovery;
1029 switch (discov->state) {
1030 case DISCOVERY_FINDING:
1031 case DISCOVERY_RESOLVING:
1039 void hci_discovery_set_state(struct hci_dev *hdev, int state)
1041 int old_state = hdev->discovery.state;
1043 BT_DBG("%s state %u -> %u", hdev->name, hdev->discovery.state, state);
1045 if (old_state == state)
1048 hdev->discovery.state = state;
1051 case DISCOVERY_STOPPED:
1052 hci_update_background_scan(hdev);
1054 if (old_state != DISCOVERY_STARTING)
1055 mgmt_discovering(hdev, 0);
1057 case DISCOVERY_STARTING:
1059 case DISCOVERY_FINDING:
1060 mgmt_discovering(hdev, 1);
1062 case DISCOVERY_RESOLVING:
1064 case DISCOVERY_STOPPING:
1070 bool hci_le_discovery_active(struct hci_dev *hdev)
1072 struct discovery_state *discov = &hdev->le_discovery;
1074 switch (discov->state) {
1075 case DISCOVERY_FINDING:
1076 case DISCOVERY_RESOLVING:
1084 void hci_le_discovery_set_state(struct hci_dev *hdev, int state)
1086 BT_DBG("%s state %u -> %u", hdev->name,
1087 hdev->le_discovery.state, state);
1089 if (hdev->le_discovery.state == state)
1093 case DISCOVERY_STOPPED:
1094 hci_update_background_scan(hdev);
1096 if (hdev->le_discovery.state != DISCOVERY_STARTING)
1097 mgmt_le_discovering(hdev, 0);
1099 case DISCOVERY_STARTING:
1101 case DISCOVERY_FINDING:
1102 mgmt_le_discovering(hdev, 1);
1104 case DISCOVERY_RESOLVING:
1106 case DISCOVERY_STOPPING:
1110 hdev->le_discovery.state = state;
1114 void hci_inquiry_cache_flush(struct hci_dev *hdev)
1116 struct discovery_state *cache = &hdev->discovery;
1117 struct inquiry_entry *p, *n;
1119 list_for_each_entry_safe(p, n, &cache->all, all) {
1124 INIT_LIST_HEAD(&cache->unknown);
1125 INIT_LIST_HEAD(&cache->resolve);
1128 struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev,
1131 struct discovery_state *cache = &hdev->discovery;
1132 struct inquiry_entry *e;
1134 BT_DBG("cache %p, %pMR", cache, bdaddr);
1136 list_for_each_entry(e, &cache->all, all) {
1137 if (!bacmp(&e->data.bdaddr, bdaddr))
1144 struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
1147 struct discovery_state *cache = &hdev->discovery;
1148 struct inquiry_entry *e;
1150 BT_DBG("cache %p, %pMR", cache, bdaddr);
1152 list_for_each_entry(e, &cache->unknown, list) {
1153 if (!bacmp(&e->data.bdaddr, bdaddr))
1160 struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev,
1164 struct discovery_state *cache = &hdev->discovery;
1165 struct inquiry_entry *e;
1167 BT_DBG("cache %p bdaddr %pMR state %d", cache, bdaddr, state);
1169 list_for_each_entry(e, &cache->resolve, list) {
1170 if (!bacmp(bdaddr, BDADDR_ANY) && e->name_state == state)
1172 if (!bacmp(&e->data.bdaddr, bdaddr))
1179 void hci_inquiry_cache_update_resolve(struct hci_dev *hdev,
1180 struct inquiry_entry *ie)
1182 struct discovery_state *cache = &hdev->discovery;
1183 struct list_head *pos = &cache->resolve;
1184 struct inquiry_entry *p;
1186 list_del(&ie->list);
1188 list_for_each_entry(p, &cache->resolve, list) {
1189 if (p->name_state != NAME_PENDING &&
1190 abs(p->data.rssi) >= abs(ie->data.rssi))
1195 list_add(&ie->list, pos);
1198 u32 hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
1201 struct discovery_state *cache = &hdev->discovery;
1202 struct inquiry_entry *ie;
1205 BT_DBG("cache %p, %pMR", cache, &data->bdaddr);
1207 hci_remove_remote_oob_data(hdev, &data->bdaddr, BDADDR_BREDR);
1209 if (!data->ssp_mode)
1210 flags |= MGMT_DEV_FOUND_LEGACY_PAIRING;
1212 ie = hci_inquiry_cache_lookup(hdev, &data->bdaddr);
1214 if (!ie->data.ssp_mode)
1215 flags |= MGMT_DEV_FOUND_LEGACY_PAIRING;
1217 if (ie->name_state == NAME_NEEDED &&
1218 data->rssi != ie->data.rssi) {
1219 ie->data.rssi = data->rssi;
1220 hci_inquiry_cache_update_resolve(hdev, ie);
1226 /* Entry not in the cache. Add new one. */
1227 ie = kzalloc(sizeof(*ie), GFP_KERNEL);
1229 flags |= MGMT_DEV_FOUND_CONFIRM_NAME;
1233 list_add(&ie->all, &cache->all);
1236 ie->name_state = NAME_KNOWN;
1238 ie->name_state = NAME_NOT_KNOWN;
1239 list_add(&ie->list, &cache->unknown);
1243 if (name_known && ie->name_state != NAME_KNOWN &&
1244 ie->name_state != NAME_PENDING) {
1245 ie->name_state = NAME_KNOWN;
1246 list_del(&ie->list);
1249 memcpy(&ie->data, data, sizeof(*data));
1250 ie->timestamp = jiffies;
1251 cache->timestamp = jiffies;
1253 if (ie->name_state == NAME_NOT_KNOWN)
1254 flags |= MGMT_DEV_FOUND_CONFIRM_NAME;
1260 static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf)
1262 struct discovery_state *cache = &hdev->discovery;
1263 struct inquiry_info *info = (struct inquiry_info *) buf;
1264 struct inquiry_entry *e;
1267 list_for_each_entry(e, &cache->all, all) {
1268 struct inquiry_data *data = &e->data;
1273 bacpy(&info->bdaddr, &data->bdaddr);
1274 info->pscan_rep_mode = data->pscan_rep_mode;
1275 info->pscan_period_mode = data->pscan_period_mode;
1276 info->pscan_mode = data->pscan_mode;
1277 memcpy(info->dev_class, data->dev_class, 3);
1278 info->clock_offset = data->clock_offset;
1284 BT_DBG("cache %p, copied %d", cache, copied);
1288 static int hci_inq_req(struct hci_request *req, unsigned long opt)
1290 struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt;
1291 struct hci_dev *hdev = req->hdev;
1292 struct hci_cp_inquiry cp;
1294 BT_DBG("%s", hdev->name);
1296 if (test_bit(HCI_INQUIRY, &hdev->flags))
1300 memcpy(&cp.lap, &ir->lap, 3);
1301 cp.length = ir->length;
1302 cp.num_rsp = ir->num_rsp;
1303 hci_req_add(req, HCI_OP_INQUIRY, sizeof(cp), &cp);
1308 int hci_inquiry(void __user *arg)
1310 __u8 __user *ptr = arg;
1311 struct hci_inquiry_req ir;
1312 struct hci_dev *hdev;
1313 int err = 0, do_inquiry = 0, max_rsp;
1317 if (copy_from_user(&ir, ptr, sizeof(ir)))
1320 hdev = hci_dev_get(ir.dev_id);
1324 if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
1329 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
1334 if (hdev->dev_type != HCI_PRIMARY) {
1339 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
1345 if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX ||
1346 inquiry_cache_empty(hdev) || ir.flags & IREQ_CACHE_FLUSH) {
1347 hci_inquiry_cache_flush(hdev);
1350 hci_dev_unlock(hdev);
1352 timeo = ir.length * msecs_to_jiffies(2000);
1355 err = hci_req_sync(hdev, hci_inq_req, (unsigned long) &ir,
1360 /* Wait until Inquiry procedure finishes (HCI_INQUIRY flag is
1361 * cleared). If it is interrupted by a signal, return -EINTR.
1363 if (wait_on_bit(&hdev->flags, HCI_INQUIRY,
1364 TASK_INTERRUPTIBLE))
1368 /* for unlimited number of responses we will use buffer with
1371 max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp;
1373 /* cache_dump can't sleep. Therefore we allocate temp buffer and then
1374 * copy it to the user space.
1376 buf = kmalloc_array(max_rsp, sizeof(struct inquiry_info), GFP_KERNEL);
1383 ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf);
1384 hci_dev_unlock(hdev);
1386 BT_DBG("num_rsp %d", ir.num_rsp);
1388 if (!copy_to_user(ptr, &ir, sizeof(ir))) {
1390 if (copy_to_user(ptr, buf, sizeof(struct inquiry_info) *
1403 static int hci_dev_do_open(struct hci_dev *hdev)
1407 BT_DBG("%s %p", hdev->name, hdev);
1409 hci_req_sync_lock(hdev);
1411 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
1416 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
1417 !hci_dev_test_flag(hdev, HCI_CONFIG)) {
1418 /* Check for rfkill but allow the HCI setup stage to
1419 * proceed (which in itself doesn't cause any RF activity).
1421 if (hci_dev_test_flag(hdev, HCI_RFKILLED)) {
1426 /* Check for valid public address or a configured static
1427 * random adddress, but let the HCI setup proceed to
1428 * be able to determine if there is a public address
1431 * In case of user channel usage, it is not important
1432 * if a public address or static random address is
1435 * This check is only valid for BR/EDR controllers
1436 * since AMP controllers do not have an address.
1438 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
1439 hdev->dev_type == HCI_PRIMARY &&
1440 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
1441 !bacmp(&hdev->static_addr, BDADDR_ANY)) {
1442 ret = -EADDRNOTAVAIL;
1447 if (test_bit(HCI_UP, &hdev->flags)) {
1452 if (hdev->open(hdev)) {
1457 set_bit(HCI_RUNNING, &hdev->flags);
1458 hci_sock_dev_event(hdev, HCI_DEV_OPEN);
1460 atomic_set(&hdev->cmd_cnt, 1);
1461 set_bit(HCI_INIT, &hdev->flags);
1463 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
1464 test_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks)) {
1465 hci_sock_dev_event(hdev, HCI_DEV_SETUP);
1468 ret = hdev->setup(hdev);
1470 /* The transport driver can set these quirks before
1471 * creating the HCI device or in its setup callback.
1473 * In case any of them is set, the controller has to
1474 * start up as unconfigured.
1476 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
1477 test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks))
1478 hci_dev_set_flag(hdev, HCI_UNCONFIGURED);
1480 /* For an unconfigured controller it is required to
1481 * read at least the version information provided by
1482 * the Read Local Version Information command.
1484 * If the set_bdaddr driver callback is provided, then
1485 * also the original Bluetooth public device address
1486 * will be read using the Read BD Address command.
1488 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
1489 ret = __hci_unconf_init(hdev);
1492 if (hci_dev_test_flag(hdev, HCI_CONFIG)) {
1493 /* If public address change is configured, ensure that
1494 * the address gets programmed. If the driver does not
1495 * support changing the public address, fail the power
1498 if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
1500 ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
1502 ret = -EADDRNOTAVAIL;
1506 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
1507 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
1508 ret = __hci_init(hdev);
1509 if (!ret && hdev->post_init)
1510 ret = hdev->post_init(hdev);
1514 /* If the HCI Reset command is clearing all diagnostic settings,
1515 * then they need to be reprogrammed after the init procedure
1518 if (test_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks) &&
1519 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
1520 hci_dev_test_flag(hdev, HCI_VENDOR_DIAG) && hdev->set_diag)
1521 ret = hdev->set_diag(hdev, true);
1523 clear_bit(HCI_INIT, &hdev->flags);
1527 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
1528 hci_adv_instances_set_rpa_expired(hdev, true);
1529 set_bit(HCI_UP, &hdev->flags);
1530 hci_sock_dev_event(hdev, HCI_DEV_UP);
1531 hci_leds_update_powered(hdev, true);
1532 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
1533 !hci_dev_test_flag(hdev, HCI_CONFIG) &&
1534 !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
1535 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
1536 hci_dev_test_flag(hdev, HCI_MGMT) &&
1537 hdev->dev_type == HCI_PRIMARY) {
1538 ret = __hci_req_hci_power_on(hdev);
1539 mgmt_power_on(hdev, ret);
1542 /* Init failed, cleanup */
1543 flush_work(&hdev->tx_work);
1544 flush_work(&hdev->cmd_work);
1545 flush_work(&hdev->rx_work);
1547 skb_queue_purge(&hdev->cmd_q);
1548 skb_queue_purge(&hdev->rx_q);
1553 if (hdev->sent_cmd) {
1554 kfree_skb(hdev->sent_cmd);
1555 hdev->sent_cmd = NULL;
1558 clear_bit(HCI_RUNNING, &hdev->flags);
1559 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
1562 hdev->flags &= BIT(HCI_RAW);
1566 hci_req_sync_unlock(hdev);
1570 /* ---- HCI ioctl helpers ---- */
1572 int hci_dev_open(__u16 dev)
1574 struct hci_dev *hdev;
1577 hdev = hci_dev_get(dev);
1581 /* Devices that are marked as unconfigured can only be powered
1582 * up as user channel. Trying to bring them up as normal devices
1583 * will result into a failure. Only user channel operation is
1586 * When this function is called for a user channel, the flag
1587 * HCI_USER_CHANNEL will be set first before attempting to
1590 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
1591 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
1596 /* We need to ensure that no other power on/off work is pending
1597 * before proceeding to call hci_dev_do_open. This is
1598 * particularly important if the setup procedure has not yet
1601 if (hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF))
1602 cancel_delayed_work(&hdev->power_off);
1604 /* After this call it is guaranteed that the setup procedure
1605 * has finished. This means that error conditions like RFKILL
1606 * or no valid public or static random address apply.
1608 flush_workqueue(hdev->req_workqueue);
1610 /* For controllers not using the management interface and that
1611 * are brought up using legacy ioctl, set the HCI_BONDABLE bit
1612 * so that pairing works for them. Once the management interface
1613 * is in use this bit will be cleared again and userspace has
1614 * to explicitly enable it.
1616 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
1617 !hci_dev_test_flag(hdev, HCI_MGMT))
1618 hci_dev_set_flag(hdev, HCI_BONDABLE);
1620 err = hci_dev_do_open(hdev);
1627 /* This function requires the caller holds hdev->lock */
1628 static void hci_pend_le_actions_clear(struct hci_dev *hdev)
1630 struct hci_conn_params *p;
1632 list_for_each_entry(p, &hdev->le_conn_params, list) {
1634 hci_conn_drop(p->conn);
1635 hci_conn_put(p->conn);
1638 list_del_init(&p->action);
1641 BT_DBG("All LE pending actions cleared");
1644 int hci_dev_do_close(struct hci_dev *hdev)
1648 BT_DBG("%s %p", hdev->name, hdev);
1650 if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) &&
1651 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
1652 test_bit(HCI_UP, &hdev->flags)) {
1653 /* Execute vendor specific shutdown routine */
1655 hdev->shutdown(hdev);
1658 cancel_delayed_work(&hdev->power_off);
1660 hci_request_cancel_all(hdev);
1661 hci_req_sync_lock(hdev);
1663 if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
1664 cancel_delayed_work_sync(&hdev->cmd_timer);
1665 hci_req_sync_unlock(hdev);
1669 hci_leds_update_powered(hdev, false);
1671 /* Flush RX and TX works */
1672 flush_work(&hdev->tx_work);
1673 flush_work(&hdev->rx_work);
1675 if (hdev->discov_timeout > 0) {
1676 hdev->discov_timeout = 0;
1677 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1678 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1681 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
1682 cancel_delayed_work(&hdev->service_cache);
1684 if (hci_dev_test_flag(hdev, HCI_MGMT)) {
1685 struct adv_info *adv_instance;
1687 cancel_delayed_work_sync(&hdev->rpa_expired);
1689 list_for_each_entry(adv_instance, &hdev->adv_instances, list)
1690 cancel_delayed_work_sync(&adv_instance->rpa_expired_cb);
1693 /* Avoid potential lockdep warnings from the *_flush() calls by
1694 * ensuring the workqueue is empty up front.
1696 drain_workqueue(hdev->workqueue);
1700 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1702 auto_off = hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF);
1704 if (!auto_off && hdev->dev_type == HCI_PRIMARY &&
1705 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
1706 hci_dev_test_flag(hdev, HCI_MGMT))
1707 __mgmt_power_off(hdev);
1709 hci_inquiry_cache_flush(hdev);
1710 hci_pend_le_actions_clear(hdev);
1711 hci_conn_hash_flush(hdev);
1712 hci_dev_unlock(hdev);
1714 smp_unregister(hdev);
1716 hci_sock_dev_event(hdev, HCI_DEV_DOWN);
1722 skb_queue_purge(&hdev->cmd_q);
1723 atomic_set(&hdev->cmd_cnt, 1);
1724 if (test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks) &&
1725 !auto_off && !hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
1726 set_bit(HCI_INIT, &hdev->flags);
1727 __hci_req_sync(hdev, hci_reset_req, 0, HCI_CMD_TIMEOUT, NULL);
1728 clear_bit(HCI_INIT, &hdev->flags);
1731 /* flush cmd work */
1732 flush_work(&hdev->cmd_work);
1735 skb_queue_purge(&hdev->rx_q);
1736 skb_queue_purge(&hdev->cmd_q);
1737 skb_queue_purge(&hdev->raw_q);
1739 /* Drop last sent command */
1740 if (hdev->sent_cmd) {
1741 cancel_delayed_work_sync(&hdev->cmd_timer);
1742 kfree_skb(hdev->sent_cmd);
1743 hdev->sent_cmd = NULL;
1746 clear_bit(HCI_RUNNING, &hdev->flags);
1747 hci_sock_dev_event(hdev, HCI_DEV_CLOSE);
1749 /* After this point our queues are empty
1750 * and no tasks are scheduled. */
1754 hdev->flags &= BIT(HCI_RAW);
1755 hci_dev_clear_volatile_flags(hdev);
1757 /* Controller radio is available but is currently powered down */
1758 hdev->amp_status = AMP_STATUS_POWERED_DOWN;
1760 memset(hdev->eir, 0, sizeof(hdev->eir));
1761 memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
1762 bacpy(&hdev->random_addr, BDADDR_ANY);
1764 hci_req_sync_unlock(hdev);
1770 int hci_dev_close(__u16 dev)
1772 struct hci_dev *hdev;
1775 hdev = hci_dev_get(dev);
1779 if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
1784 if (hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF))
1785 cancel_delayed_work(&hdev->power_off);
1787 err = hci_dev_do_close(hdev);
1794 static int hci_dev_do_reset(struct hci_dev *hdev)
1798 BT_DBG("%s %p", hdev->name, hdev);
1800 hci_req_sync_lock(hdev);
1803 skb_queue_purge(&hdev->rx_q);
1804 skb_queue_purge(&hdev->cmd_q);
1806 /* Avoid potential lockdep warnings from the *_flush() calls by
1807 * ensuring the workqueue is empty up front.
1809 drain_workqueue(hdev->workqueue);
1812 hci_inquiry_cache_flush(hdev);
1813 hci_conn_hash_flush(hdev);
1814 hci_dev_unlock(hdev);
1819 atomic_set(&hdev->cmd_cnt, 1);
1820 hdev->acl_cnt = 0; hdev->sco_cnt = 0; hdev->le_cnt = 0;
1822 ret = __hci_req_sync(hdev, hci_reset_req, 0, HCI_INIT_TIMEOUT, NULL);
1824 hci_req_sync_unlock(hdev);
1828 int hci_dev_reset(__u16 dev)
1830 struct hci_dev *hdev;
1833 hdev = hci_dev_get(dev);
1837 if (!test_bit(HCI_UP, &hdev->flags)) {
1842 if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
1847 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
1852 err = hci_dev_do_reset(hdev);
1859 int hci_dev_reset_stat(__u16 dev)
1861 struct hci_dev *hdev;
1864 hdev = hci_dev_get(dev);
1868 if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
1873 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
1878 memset(&hdev->stat, 0, sizeof(struct hci_dev_stats));
1885 static void hci_update_scan_state(struct hci_dev *hdev, u8 scan)
1887 bool conn_changed, discov_changed;
1889 BT_DBG("%s scan 0x%02x", hdev->name, scan);
1891 if ((scan & SCAN_PAGE))
1892 conn_changed = !hci_dev_test_and_set_flag(hdev,
1895 conn_changed = hci_dev_test_and_clear_flag(hdev,
1898 if ((scan & SCAN_INQUIRY)) {
1899 discov_changed = !hci_dev_test_and_set_flag(hdev,
1902 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1903 discov_changed = hci_dev_test_and_clear_flag(hdev,
1907 if (!hci_dev_test_flag(hdev, HCI_MGMT))
1910 if (conn_changed || discov_changed) {
1911 /* In case this was disabled through mgmt */
1912 hci_dev_set_flag(hdev, HCI_BREDR_ENABLED);
1914 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1915 hci_req_update_adv_data(hdev, hdev->cur_adv_instance);
1917 mgmt_new_settings(hdev);
1921 int hci_dev_cmd(unsigned int cmd, void __user *arg)
1923 struct hci_dev *hdev;
1924 struct hci_dev_req dr;
1927 if (copy_from_user(&dr, arg, sizeof(dr)))
1930 hdev = hci_dev_get(dr.dev_id);
1934 if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
1939 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
1944 if (hdev->dev_type != HCI_PRIMARY) {
1949 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
1956 err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt,
1957 HCI_INIT_TIMEOUT, NULL);
1961 if (!lmp_encrypt_capable(hdev)) {
1966 if (!test_bit(HCI_AUTH, &hdev->flags)) {
1967 /* Auth must be enabled first */
1968 err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt,
1969 HCI_INIT_TIMEOUT, NULL);
1974 err = hci_req_sync(hdev, hci_encrypt_req, dr.dev_opt,
1975 HCI_INIT_TIMEOUT, NULL);
1979 err = hci_req_sync(hdev, hci_scan_req, dr.dev_opt,
1980 HCI_INIT_TIMEOUT, NULL);
1982 /* Ensure that the connectable and discoverable states
1983 * get correctly modified as this was a non-mgmt change.
1986 hci_update_scan_state(hdev, dr.dev_opt);
1990 err = hci_req_sync(hdev, hci_linkpol_req, dr.dev_opt,
1991 HCI_INIT_TIMEOUT, NULL);
1994 case HCISETLINKMODE:
1995 hdev->link_mode = ((__u16) dr.dev_opt) &
1996 (HCI_LM_MASTER | HCI_LM_ACCEPT);
2000 if (hdev->pkt_type == (__u16) dr.dev_opt)
2003 hdev->pkt_type = (__u16) dr.dev_opt;
2004 mgmt_phy_configuration_changed(hdev, NULL);
2008 hdev->acl_mtu = *((__u16 *) &dr.dev_opt + 1);
2009 hdev->acl_pkts = *((__u16 *) &dr.dev_opt + 0);
2013 hdev->sco_mtu = *((__u16 *) &dr.dev_opt + 1);
2014 hdev->sco_pkts = *((__u16 *) &dr.dev_opt + 0);
2027 int hci_get_dev_list(void __user *arg)
2029 struct hci_dev *hdev;
2030 struct hci_dev_list_req *dl;
2031 struct hci_dev_req *dr;
2032 int n = 0, size, err;
2035 if (get_user(dev_num, (__u16 __user *) arg))
2038 if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr))
2041 size = sizeof(*dl) + dev_num * sizeof(*dr);
2043 dl = kzalloc(size, GFP_KERNEL);
2049 read_lock(&hci_dev_list_lock);
2050 list_for_each_entry(hdev, &hci_dev_list, list) {
2051 unsigned long flags = hdev->flags;
2053 /* When the auto-off is configured it means the transport
2054 * is running, but in that case still indicate that the
2055 * device is actually down.
2057 if (hci_dev_test_flag(hdev, HCI_AUTO_OFF))
2058 flags &= ~BIT(HCI_UP);
2060 (dr + n)->dev_id = hdev->id;
2061 (dr + n)->dev_opt = flags;
2066 read_unlock(&hci_dev_list_lock);
2069 size = sizeof(*dl) + n * sizeof(*dr);
2071 err = copy_to_user(arg, dl, size);
2074 return err ? -EFAULT : 0;
2077 int hci_get_dev_info(void __user *arg)
2079 struct hci_dev *hdev;
2080 struct hci_dev_info di;
2081 unsigned long flags;
2084 if (copy_from_user(&di, arg, sizeof(di)))
2087 hdev = hci_dev_get(di.dev_id);
2091 /* When the auto-off is configured it means the transport
2092 * is running, but in that case still indicate that the
2093 * device is actually down.
2095 if (hci_dev_test_flag(hdev, HCI_AUTO_OFF))
2096 flags = hdev->flags & ~BIT(HCI_UP);
2098 flags = hdev->flags;
2100 strcpy(di.name, hdev->name);
2101 di.bdaddr = hdev->bdaddr;
2102 di.type = (hdev->bus & 0x0f) | ((hdev->dev_type & 0x03) << 4);
2104 di.pkt_type = hdev->pkt_type;
2105 if (lmp_bredr_capable(hdev)) {
2106 di.acl_mtu = hdev->acl_mtu;
2107 di.acl_pkts = hdev->acl_pkts;
2108 di.sco_mtu = hdev->sco_mtu;
2109 di.sco_pkts = hdev->sco_pkts;
2111 di.acl_mtu = hdev->le_mtu;
2112 di.acl_pkts = hdev->le_pkts;
2116 di.link_policy = hdev->link_policy;
2117 di.link_mode = hdev->link_mode;
2119 memcpy(&di.stat, &hdev->stat, sizeof(di.stat));
2120 memcpy(&di.features, &hdev->features, sizeof(di.features));
2122 if (copy_to_user(arg, &di, sizeof(di)))
2130 /* ---- Interface to HCI drivers ---- */
2132 static int hci_rfkill_set_block(void *data, bool blocked)
2134 struct hci_dev *hdev = data;
2136 BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked);
2138 if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL))
2142 hci_dev_set_flag(hdev, HCI_RFKILLED);
2143 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
2144 !hci_dev_test_flag(hdev, HCI_CONFIG))
2145 hci_dev_do_close(hdev);
2147 hci_dev_clear_flag(hdev, HCI_RFKILLED);
2153 static const struct rfkill_ops hci_rfkill_ops = {
2154 .set_block = hci_rfkill_set_block,
2157 static void hci_power_on(struct work_struct *work)
2159 struct hci_dev *hdev = container_of(work, struct hci_dev, power_on);
2162 BT_DBG("%s", hdev->name);
2164 if (test_bit(HCI_UP, &hdev->flags) &&
2165 hci_dev_test_flag(hdev, HCI_MGMT) &&
2166 hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
2167 cancel_delayed_work(&hdev->power_off);
2168 hci_req_sync_lock(hdev);
2169 err = __hci_req_hci_power_on(hdev);
2170 hci_req_sync_unlock(hdev);
2171 mgmt_power_on(hdev, err);
2175 err = hci_dev_do_open(hdev);
2178 mgmt_set_powered_failed(hdev, err);
2179 hci_dev_unlock(hdev);
2183 /* During the HCI setup phase, a few error conditions are
2184 * ignored and they need to be checked now. If they are still
2185 * valid, it is important to turn the device back off.
2187 if (hci_dev_test_flag(hdev, HCI_RFKILLED) ||
2188 hci_dev_test_flag(hdev, HCI_UNCONFIGURED) ||
2189 (hdev->dev_type == HCI_PRIMARY &&
2190 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
2191 !bacmp(&hdev->static_addr, BDADDR_ANY))) {
2192 hci_dev_clear_flag(hdev, HCI_AUTO_OFF);
2193 hci_dev_do_close(hdev);
2194 } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) {
2195 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
2196 HCI_AUTO_OFF_TIMEOUT);
2199 if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) {
2200 /* For unconfigured devices, set the HCI_RAW flag
2201 * so that userspace can easily identify them.
2203 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
2204 set_bit(HCI_RAW, &hdev->flags);
2206 /* For fully configured devices, this will send
2207 * the Index Added event. For unconfigured devices,
2208 * it will send Unconfigued Index Added event.
2210 * Devices with HCI_QUIRK_RAW_DEVICE are ignored
2211 * and no event will be send.
2213 mgmt_index_added(hdev);
2214 } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) {
2215 /* When the controller is now configured, then it
2216 * is important to clear the HCI_RAW flag.
2218 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
2219 clear_bit(HCI_RAW, &hdev->flags);
2221 /* Powering on the controller with HCI_CONFIG set only
2222 * happens with the transition from unconfigured to
2223 * configured. This will send the Index Added event.
2225 mgmt_index_added(hdev);
2229 static void hci_power_off(struct work_struct *work)
2231 struct hci_dev *hdev = container_of(work, struct hci_dev,
2234 BT_DBG("%s", hdev->name);
2236 hci_dev_do_close(hdev);
2239 static void hci_error_reset(struct work_struct *work)
2241 struct hci_dev *hdev = container_of(work, struct hci_dev, error_reset);
2243 BT_DBG("%s", hdev->name);
2246 hdev->hw_error(hdev, hdev->hw_error_code);
2248 bt_dev_err(hdev, "hardware error 0x%2.2x", hdev->hw_error_code);
2250 if (hci_dev_do_close(hdev))
2253 hci_dev_do_open(hdev);
2256 void hci_uuids_clear(struct hci_dev *hdev)
2258 struct bt_uuid *uuid, *tmp;
2260 list_for_each_entry_safe(uuid, tmp, &hdev->uuids, list) {
2261 list_del(&uuid->list);
2266 void hci_link_keys_clear(struct hci_dev *hdev)
2268 struct link_key *key;
2270 list_for_each_entry_rcu(key, &hdev->link_keys, list) {
2271 list_del_rcu(&key->list);
2272 kfree_rcu(key, rcu);
2276 void hci_smp_ltks_clear(struct hci_dev *hdev)
2280 list_for_each_entry_rcu(k, &hdev->long_term_keys, list) {
2281 list_del_rcu(&k->list);
2286 void hci_smp_irks_clear(struct hci_dev *hdev)
2290 list_for_each_entry_rcu(k, &hdev->identity_resolving_keys, list) {
2291 list_del_rcu(&k->list);
2296 struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr)
2301 list_for_each_entry_rcu(k, &hdev->link_keys, list) {
2302 if (bacmp(bdaddr, &k->bdaddr) == 0) {
2312 static bool hci_persistent_key(struct hci_dev *hdev, struct hci_conn *conn,
2313 u8 key_type, u8 old_key_type)
2316 if (key_type < 0x03)
2319 /* Debug keys are insecure so don't store them persistently */
2320 if (key_type == HCI_LK_DEBUG_COMBINATION)
2323 /* Changed combination key and there's no previous one */
2324 if (key_type == HCI_LK_CHANGED_COMBINATION && old_key_type == 0xff)
2327 /* Security mode 3 case */
2331 /* BR/EDR key derived using SC from an LE link */
2332 if (conn->type == LE_LINK)
2335 /* Neither local nor remote side had no-bonding as requirement */
2336 if (conn->auth_type > 0x01 && conn->remote_auth > 0x01)
2339 /* Local side had dedicated bonding as requirement */
2340 if (conn->auth_type == 0x02 || conn->auth_type == 0x03)
2343 /* Remote side had dedicated bonding as requirement */
2344 if (conn->remote_auth == 0x02 || conn->remote_auth == 0x03)
2347 /* If none of the above criteria match, then don't store the key
2352 static u8 ltk_role(u8 type)
2354 if (type == SMP_LTK)
2355 return HCI_ROLE_MASTER;
2357 return HCI_ROLE_SLAVE;
2360 struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr,
2361 u8 addr_type, u8 role)
2366 list_for_each_entry_rcu(k, &hdev->long_term_keys, list) {
2367 if (addr_type != k->bdaddr_type || bacmp(bdaddr, &k->bdaddr))
2370 if (smp_ltk_is_sc(k) || ltk_role(k->type) == role) {
2380 struct smp_irk *hci_find_irk_by_rpa(struct hci_dev *hdev, bdaddr_t *rpa)
2382 struct smp_irk *irk;
2385 list_for_each_entry_rcu(irk, &hdev->identity_resolving_keys, list) {
2386 if (!bacmp(&irk->rpa, rpa)) {
2392 list_for_each_entry_rcu(irk, &hdev->identity_resolving_keys, list) {
2393 if (smp_irk_matches(hdev, irk->val, rpa)) {
2394 bacpy(&irk->rpa, rpa);
2404 struct smp_irk *hci_find_irk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr,
2407 struct smp_irk *irk;
2409 /* Identity Address must be public or static random */
2410 if (addr_type == ADDR_LE_DEV_RANDOM && (bdaddr->b[5] & 0xc0) != 0xc0)
2414 list_for_each_entry_rcu(irk, &hdev->identity_resolving_keys, list) {
2415 if (addr_type == irk->addr_type &&
2416 bacmp(bdaddr, &irk->bdaddr) == 0) {
2426 struct link_key *hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn,
2427 bdaddr_t *bdaddr, u8 *val, u8 type,
2428 u8 pin_len, bool *persistent)
2430 struct link_key *key, *old_key;
2433 old_key = hci_find_link_key(hdev, bdaddr);
2435 old_key_type = old_key->type;
2438 old_key_type = conn ? conn->key_type : 0xff;
2439 key = kzalloc(sizeof(*key), GFP_KERNEL);
2442 list_add_rcu(&key->list, &hdev->link_keys);
2445 BT_DBG("%s key for %pMR type %u", hdev->name, bdaddr, type);
2447 /* Some buggy controller combinations generate a changed
2448 * combination key for legacy pairing even when there's no
2450 if (type == HCI_LK_CHANGED_COMBINATION &&
2451 (!conn || conn->remote_auth == 0xff) && old_key_type == 0xff) {
2452 type = HCI_LK_COMBINATION;
2454 conn->key_type = type;
2457 bacpy(&key->bdaddr, bdaddr);
2458 memcpy(key->val, val, HCI_LINK_KEY_SIZE);
2459 key->pin_len = pin_len;
2461 if (type == HCI_LK_CHANGED_COMBINATION)
2462 key->type = old_key_type;
2467 *persistent = hci_persistent_key(hdev, conn, type,
2473 struct smp_ltk *hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr,
2474 u8 addr_type, u8 type, u8 authenticated,
2475 u8 tk[16], u8 enc_size, __le16 ediv, __le64 rand)
2477 struct smp_ltk *key, *old_key;
2478 u8 role = ltk_role(type);
2480 old_key = hci_find_ltk(hdev, bdaddr, addr_type, role);
2484 key = kzalloc(sizeof(*key), GFP_KERNEL);
2487 list_add_rcu(&key->list, &hdev->long_term_keys);
2490 bacpy(&key->bdaddr, bdaddr);
2491 key->bdaddr_type = addr_type;
2492 memcpy(key->val, tk, sizeof(key->val));
2493 key->authenticated = authenticated;
2496 key->enc_size = enc_size;
2502 struct smp_irk *hci_add_irk(struct hci_dev *hdev, bdaddr_t *bdaddr,
2503 u8 addr_type, u8 val[16], bdaddr_t *rpa)
2505 struct smp_irk *irk;
2507 irk = hci_find_irk_by_addr(hdev, bdaddr, addr_type);
2509 irk = kzalloc(sizeof(*irk), GFP_KERNEL);
2513 bacpy(&irk->bdaddr, bdaddr);
2514 irk->addr_type = addr_type;
2516 list_add_rcu(&irk->list, &hdev->identity_resolving_keys);
2519 memcpy(irk->val, val, 16);
2520 bacpy(&irk->rpa, rpa);
2525 int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr)
2527 struct link_key *key;
2529 key = hci_find_link_key(hdev, bdaddr);
2533 BT_DBG("%s removing %pMR", hdev->name, bdaddr);
2535 list_del_rcu(&key->list);
2536 kfree_rcu(key, rcu);
2541 int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 bdaddr_type)
2546 list_for_each_entry_rcu(k, &hdev->long_term_keys, list) {
2547 if (bacmp(bdaddr, &k->bdaddr) || k->bdaddr_type != bdaddr_type)
2550 BT_DBG("%s removing %pMR", hdev->name, bdaddr);
2552 list_del_rcu(&k->list);
2557 return removed ? 0 : -ENOENT;
2560 void hci_remove_irk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type)
2564 list_for_each_entry_rcu(k, &hdev->identity_resolving_keys, list) {
2565 if (bacmp(bdaddr, &k->bdaddr) || k->addr_type != addr_type)
2568 BT_DBG("%s removing %pMR", hdev->name, bdaddr);
2570 list_del_rcu(&k->list);
2575 bool hci_bdaddr_is_paired(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
2578 struct smp_irk *irk;
2581 if (type == BDADDR_BREDR) {
2582 if (hci_find_link_key(hdev, bdaddr))
2587 /* Convert to HCI addr type which struct smp_ltk uses */
2588 if (type == BDADDR_LE_PUBLIC)
2589 addr_type = ADDR_LE_DEV_PUBLIC;
2591 addr_type = ADDR_LE_DEV_RANDOM;
2593 irk = hci_get_irk(hdev, bdaddr, addr_type);
2595 bdaddr = &irk->bdaddr;
2596 addr_type = irk->addr_type;
2600 list_for_each_entry_rcu(k, &hdev->long_term_keys, list) {
2601 if (k->bdaddr_type == addr_type && !bacmp(bdaddr, &k->bdaddr)) {
2611 /* HCI command timer function */
2612 static void hci_cmd_timeout(struct work_struct *work)
2614 struct hci_dev *hdev = container_of(work, struct hci_dev,
2617 if (hdev->sent_cmd) {
2618 struct hci_command_hdr *sent = (void *) hdev->sent_cmd->data;
2619 u16 opcode = __le16_to_cpu(sent->opcode);
2621 bt_dev_err(hdev, "command 0x%4.4x tx timeout", opcode);
2623 bt_dev_err(hdev, "command tx timeout");
2626 atomic_set(&hdev->cmd_cnt, 1);
2627 queue_work(hdev->workqueue, &hdev->cmd_work);
2630 struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev,
2631 bdaddr_t *bdaddr, u8 bdaddr_type)
2633 struct oob_data *data;
2635 list_for_each_entry(data, &hdev->remote_oob_data, list) {
2636 if (bacmp(bdaddr, &data->bdaddr) != 0)
2638 if (data->bdaddr_type != bdaddr_type)
2646 int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr,
2649 struct oob_data *data;
2651 data = hci_find_remote_oob_data(hdev, bdaddr, bdaddr_type);
2655 BT_DBG("%s removing %pMR (%u)", hdev->name, bdaddr, bdaddr_type);
2657 list_del(&data->list);
2663 void hci_remote_oob_data_clear(struct hci_dev *hdev)
2665 struct oob_data *data, *n;
2667 list_for_each_entry_safe(data, n, &hdev->remote_oob_data, list) {
2668 list_del(&data->list);
2673 int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr,
2674 u8 bdaddr_type, u8 *hash192, u8 *rand192,
2675 u8 *hash256, u8 *rand256)
2677 struct oob_data *data;
2679 data = hci_find_remote_oob_data(hdev, bdaddr, bdaddr_type);
2681 data = kmalloc(sizeof(*data), GFP_KERNEL);
2685 bacpy(&data->bdaddr, bdaddr);
2686 data->bdaddr_type = bdaddr_type;
2687 list_add(&data->list, &hdev->remote_oob_data);
2690 if (hash192 && rand192) {
2691 memcpy(data->hash192, hash192, sizeof(data->hash192));
2692 memcpy(data->rand192, rand192, sizeof(data->rand192));
2693 if (hash256 && rand256)
2694 data->present = 0x03;
2696 memset(data->hash192, 0, sizeof(data->hash192));
2697 memset(data->rand192, 0, sizeof(data->rand192));
2698 if (hash256 && rand256)
2699 data->present = 0x02;
2701 data->present = 0x00;
2704 if (hash256 && rand256) {
2705 memcpy(data->hash256, hash256, sizeof(data->hash256));
2706 memcpy(data->rand256, rand256, sizeof(data->rand256));
2708 memset(data->hash256, 0, sizeof(data->hash256));
2709 memset(data->rand256, 0, sizeof(data->rand256));
2710 if (hash192 && rand192)
2711 data->present = 0x01;
2714 BT_DBG("%s for %pMR", hdev->name, bdaddr);
2719 /* This function requires the caller holds hdev->lock */
2720 struct adv_info *hci_find_adv_instance(struct hci_dev *hdev, u8 instance)
2722 struct adv_info *adv_instance;
2724 list_for_each_entry(adv_instance, &hdev->adv_instances, list) {
2725 if (adv_instance->instance == instance)
2726 return adv_instance;
2732 /* This function requires the caller holds hdev->lock */
2733 struct adv_info *hci_get_next_instance(struct hci_dev *hdev, u8 instance)
2735 struct adv_info *cur_instance;
2737 cur_instance = hci_find_adv_instance(hdev, instance);
2741 if (cur_instance == list_last_entry(&hdev->adv_instances,
2742 struct adv_info, list))
2743 return list_first_entry(&hdev->adv_instances,
2744 struct adv_info, list);
2746 return list_next_entry(cur_instance, list);
2749 /* This function requires the caller holds hdev->lock */
2750 int hci_remove_adv_instance(struct hci_dev *hdev, u8 instance)
2752 struct adv_info *adv_instance;
2754 adv_instance = hci_find_adv_instance(hdev, instance);
2758 BT_DBG("%s removing %dMR", hdev->name, instance);
2760 if (hdev->cur_adv_instance == instance) {
2761 if (hdev->adv_instance_timeout) {
2762 cancel_delayed_work(&hdev->adv_instance_expire);
2763 hdev->adv_instance_timeout = 0;
2765 hdev->cur_adv_instance = 0x00;
2768 cancel_delayed_work_sync(&adv_instance->rpa_expired_cb);
2770 list_del(&adv_instance->list);
2771 kfree(adv_instance);
2773 hdev->adv_instance_cnt--;
2778 void hci_adv_instances_set_rpa_expired(struct hci_dev *hdev, bool rpa_expired)
2780 struct adv_info *adv_instance, *n;
2782 list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, list)
2783 adv_instance->rpa_expired = rpa_expired;
2786 /* This function requires the caller holds hdev->lock */
2787 void hci_adv_instances_clear(struct hci_dev *hdev)
2789 struct adv_info *adv_instance, *n;
2791 if (hdev->adv_instance_timeout) {
2792 cancel_delayed_work(&hdev->adv_instance_expire);
2793 hdev->adv_instance_timeout = 0;
2796 list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, list) {
2797 cancel_delayed_work_sync(&adv_instance->rpa_expired_cb);
2798 list_del(&adv_instance->list);
2799 kfree(adv_instance);
2802 hdev->adv_instance_cnt = 0;
2803 hdev->cur_adv_instance = 0x00;
2806 static void adv_instance_rpa_expired(struct work_struct *work)
2808 struct adv_info *adv_instance = container_of(work, struct adv_info,
2809 rpa_expired_cb.work);
2813 adv_instance->rpa_expired = true;
2816 /* This function requires the caller holds hdev->lock */
2817 int hci_add_adv_instance(struct hci_dev *hdev, u8 instance, u32 flags,
2818 u16 adv_data_len, u8 *adv_data,
2819 u16 scan_rsp_len, u8 *scan_rsp_data,
2820 u16 timeout, u16 duration)
2822 struct adv_info *adv_instance;
2824 adv_instance = hci_find_adv_instance(hdev, instance);
2826 memset(adv_instance->adv_data, 0,
2827 sizeof(adv_instance->adv_data));
2828 memset(adv_instance->scan_rsp_data, 0,
2829 sizeof(adv_instance->scan_rsp_data));
2831 if (hdev->adv_instance_cnt >= HCI_MAX_ADV_INSTANCES ||
2832 instance < 1 || instance > HCI_MAX_ADV_INSTANCES)
2835 adv_instance = kzalloc(sizeof(*adv_instance), GFP_KERNEL);
2839 adv_instance->pending = true;
2840 adv_instance->instance = instance;
2841 list_add(&adv_instance->list, &hdev->adv_instances);
2842 hdev->adv_instance_cnt++;
2845 adv_instance->flags = flags;
2846 adv_instance->adv_data_len = adv_data_len;
2847 adv_instance->scan_rsp_len = scan_rsp_len;
2850 memcpy(adv_instance->adv_data, adv_data, adv_data_len);
2853 memcpy(adv_instance->scan_rsp_data,
2854 scan_rsp_data, scan_rsp_len);
2856 adv_instance->timeout = timeout;
2857 adv_instance->remaining_time = timeout;
2860 adv_instance->duration = HCI_DEFAULT_ADV_DURATION;
2862 adv_instance->duration = duration;
2864 adv_instance->tx_power = HCI_TX_POWER_INVALID;
2866 INIT_DELAYED_WORK(&adv_instance->rpa_expired_cb,
2867 adv_instance_rpa_expired);
2869 BT_DBG("%s for %dMR", hdev->name, instance);
2874 struct bdaddr_list *hci_bdaddr_list_lookup(struct list_head *bdaddr_list,
2875 bdaddr_t *bdaddr, u8 type)
2877 struct bdaddr_list *b;
2879 list_for_each_entry(b, bdaddr_list, list) {
2880 if (!bacmp(&b->bdaddr, bdaddr) && b->bdaddr_type == type)
2887 void hci_bdaddr_list_clear(struct list_head *bdaddr_list)
2889 struct bdaddr_list *b, *n;
2891 list_for_each_entry_safe(b, n, bdaddr_list, list) {
2897 int hci_bdaddr_list_add(struct list_head *list, bdaddr_t *bdaddr, u8 type)
2899 struct bdaddr_list *entry;
2901 if (!bacmp(bdaddr, BDADDR_ANY))
2904 if (hci_bdaddr_list_lookup(list, bdaddr, type))
2907 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
2911 bacpy(&entry->bdaddr, bdaddr);
2912 entry->bdaddr_type = type;
2914 list_add(&entry->list, list);
2919 int hci_bdaddr_list_del(struct list_head *list, bdaddr_t *bdaddr, u8 type)
2921 struct bdaddr_list *entry;
2923 if (!bacmp(bdaddr, BDADDR_ANY)) {
2924 hci_bdaddr_list_clear(list);
2928 entry = hci_bdaddr_list_lookup(list, bdaddr, type);
2932 list_del(&entry->list);
2938 /* This function requires the caller holds hdev->lock */
2939 struct hci_conn_params *hci_conn_params_lookup(struct hci_dev *hdev,
2940 bdaddr_t *addr, u8 addr_type)
2942 struct hci_conn_params *params;
2944 list_for_each_entry(params, &hdev->le_conn_params, list) {
2945 if (bacmp(¶ms->addr, addr) == 0 &&
2946 params->addr_type == addr_type) {
2954 /* This function requires the caller holds hdev->lock */
2955 struct hci_conn_params *hci_pend_le_action_lookup(struct list_head *list,
2956 bdaddr_t *addr, u8 addr_type)
2958 struct hci_conn_params *param;
2960 list_for_each_entry(param, list, action) {
2961 if (bacmp(¶m->addr, addr) == 0 &&
2962 param->addr_type == addr_type)
2969 /* This function requires the caller holds hdev->lock */
2970 struct hci_conn_params *hci_conn_params_add(struct hci_dev *hdev,
2971 bdaddr_t *addr, u8 addr_type)
2973 struct hci_conn_params *params;
2975 params = hci_conn_params_lookup(hdev, addr, addr_type);
2979 params = kzalloc(sizeof(*params), GFP_KERNEL);
2981 bt_dev_err(hdev, "out of memory");
2985 bacpy(¶ms->addr, addr);
2986 params->addr_type = addr_type;
2988 list_add(¶ms->list, &hdev->le_conn_params);
2989 INIT_LIST_HEAD(¶ms->action);
2991 params->conn_min_interval = hdev->le_conn_min_interval;
2992 params->conn_max_interval = hdev->le_conn_max_interval;
2993 params->conn_latency = hdev->le_conn_latency;
2994 params->supervision_timeout = hdev->le_supv_timeout;
2995 params->auto_connect = HCI_AUTO_CONN_DISABLED;
2997 BT_DBG("addr %pMR (type %u)", addr, addr_type);
3002 static void hci_conn_params_free(struct hci_conn_params *params)
3005 hci_conn_drop(params->conn);
3006 hci_conn_put(params->conn);
3009 list_del(¶ms->action);
3010 list_del(¶ms->list);
3014 /* This function requires the caller holds hdev->lock */
3015 void hci_conn_params_del(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type)
3017 struct hci_conn_params *params;
3019 params = hci_conn_params_lookup(hdev, addr, addr_type);
3023 hci_conn_params_free(params);
3025 hci_update_background_scan(hdev);
3027 BT_DBG("addr %pMR (type %u)", addr, addr_type);
3030 /* This function requires the caller holds hdev->lock */
3031 void hci_conn_params_clear_disabled(struct hci_dev *hdev)
3033 struct hci_conn_params *params, *tmp;
3035 list_for_each_entry_safe(params, tmp, &hdev->le_conn_params, list) {
3036 if (params->auto_connect != HCI_AUTO_CONN_DISABLED)
3039 /* If trying to estabilish one time connection to disabled
3040 * device, leave the params, but mark them as just once.
3042 if (params->explicit_connect) {
3043 params->auto_connect = HCI_AUTO_CONN_EXPLICIT;
3047 list_del(¶ms->list);
3051 BT_DBG("All LE disabled connection parameters were removed");
3054 /* This function requires the caller holds hdev->lock */
3055 static void hci_conn_params_clear_all(struct hci_dev *hdev)
3057 struct hci_conn_params *params, *tmp;
3059 list_for_each_entry_safe(params, tmp, &hdev->le_conn_params, list)
3060 hci_conn_params_free(params);
3062 BT_DBG("All LE connection parameters were removed");
3065 /* Copy the Identity Address of the controller.
3067 * If the controller has a public BD_ADDR, then by default use that one.
3068 * If this is a LE only controller without a public address, default to
3069 * the static random address.
3071 * For debugging purposes it is possible to force controllers with a
3072 * public address to use the static random address instead.
3074 * In case BR/EDR has been disabled on a dual-mode controller and
3075 * userspace has configured a static address, then that address
3076 * becomes the identity address instead of the public BR/EDR address.
3078 void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr,
3081 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
3082 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
3083 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
3084 bacmp(&hdev->static_addr, BDADDR_ANY))) {
3085 bacpy(bdaddr, &hdev->static_addr);
3086 *bdaddr_type = ADDR_LE_DEV_RANDOM;
3088 bacpy(bdaddr, &hdev->bdaddr);
3089 *bdaddr_type = ADDR_LE_DEV_PUBLIC;
3093 /* Alloc HCI device */
3094 struct hci_dev *hci_alloc_dev(void)
3096 struct hci_dev *hdev;
3098 hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
3102 hdev->pkt_type = (HCI_DM1 | HCI_DH1 | HCI_HV1);
3103 hdev->esco_type = (ESCO_HV1);
3104 hdev->link_mode = (HCI_LM_ACCEPT);
3105 hdev->num_iac = 0x01; /* One IAC support is mandatory */
3106 hdev->io_capability = 0x03; /* No Input No Output */
3107 hdev->manufacturer = 0xffff; /* Default to internal use */
3108 hdev->inq_tx_power = HCI_TX_POWER_INVALID;
3109 hdev->adv_tx_power = HCI_TX_POWER_INVALID;
3110 hdev->adv_instance_cnt = 0;
3111 hdev->cur_adv_instance = 0x00;
3112 hdev->adv_instance_timeout = 0;
3114 hdev->sniff_max_interval = 800;
3115 hdev->sniff_min_interval = 80;
3117 hdev->le_adv_channel_map = 0x07;
3118 hdev->le_adv_min_interval = 0x0800;
3119 hdev->le_adv_max_interval = 0x0800;
3121 hdev->adv_filter_policy = 0x00;
3122 hdev->adv_type = 0x00;
3124 hdev->le_scan_interval = 0x0060;
3125 hdev->le_scan_window = 0x0030;
3126 hdev->le_conn_min_interval = 0x0018;
3127 hdev->le_conn_max_interval = 0x0028;
3128 hdev->le_conn_latency = 0x0000;
3129 hdev->le_supv_timeout = 0x002a;
3130 hdev->le_def_tx_len = 0x001b;
3131 hdev->le_def_tx_time = 0x0148;
3132 hdev->le_max_tx_len = 0x001b;
3133 hdev->le_max_tx_time = 0x0148;
3134 hdev->le_max_rx_len = 0x001b;
3135 hdev->le_max_rx_time = 0x0148;
3136 hdev->le_max_key_size = SMP_MAX_ENC_KEY_SIZE;
3137 hdev->le_min_key_size = SMP_MIN_ENC_KEY_SIZE;
3138 hdev->le_tx_def_phys = HCI_LE_SET_PHY_1M;
3139 hdev->le_rx_def_phys = HCI_LE_SET_PHY_1M;
3141 hdev->rpa_timeout = HCI_DEFAULT_RPA_TIMEOUT;
3142 hdev->discov_interleaved_timeout = DISCOV_INTERLEAVED_TIMEOUT;
3143 hdev->conn_info_min_age = DEFAULT_CONN_INFO_MIN_AGE;
3144 hdev->conn_info_max_age = DEFAULT_CONN_INFO_MAX_AGE;
3146 mutex_init(&hdev->lock);
3147 mutex_init(&hdev->req_lock);
3149 INIT_LIST_HEAD(&hdev->mgmt_pending);
3150 INIT_LIST_HEAD(&hdev->blacklist);
3151 INIT_LIST_HEAD(&hdev->whitelist);
3152 INIT_LIST_HEAD(&hdev->uuids);
3153 INIT_LIST_HEAD(&hdev->link_keys);
3154 INIT_LIST_HEAD(&hdev->long_term_keys);
3155 INIT_LIST_HEAD(&hdev->identity_resolving_keys);
3156 INIT_LIST_HEAD(&hdev->remote_oob_data);
3157 INIT_LIST_HEAD(&hdev->le_white_list);
3158 INIT_LIST_HEAD(&hdev->le_resolv_list);
3159 INIT_LIST_HEAD(&hdev->le_conn_params);
3160 INIT_LIST_HEAD(&hdev->pend_le_conns);
3161 INIT_LIST_HEAD(&hdev->pend_le_reports);
3162 INIT_LIST_HEAD(&hdev->conn_hash.list);
3163 INIT_LIST_HEAD(&hdev->adv_instances);
3165 INIT_WORK(&hdev->rx_work, hci_rx_work);
3166 INIT_WORK(&hdev->cmd_work, hci_cmd_work);
3167 INIT_WORK(&hdev->tx_work, hci_tx_work);
3168 INIT_WORK(&hdev->power_on, hci_power_on);
3169 INIT_WORK(&hdev->error_reset, hci_error_reset);
3171 INIT_DELAYED_WORK(&hdev->power_off, hci_power_off);
3173 skb_queue_head_init(&hdev->rx_q);
3174 skb_queue_head_init(&hdev->cmd_q);
3175 skb_queue_head_init(&hdev->raw_q);
3177 init_waitqueue_head(&hdev->req_wait_q);
3179 INIT_DELAYED_WORK(&hdev->cmd_timer, hci_cmd_timeout);
3181 hci_request_setup(hdev);
3183 hci_init_sysfs(hdev);
3184 discovery_init(hdev);
3188 EXPORT_SYMBOL(hci_alloc_dev);
3190 /* Free HCI device */
3191 void hci_free_dev(struct hci_dev *hdev)
3193 /* will free via device release */
3194 put_device(&hdev->dev);
3196 EXPORT_SYMBOL(hci_free_dev);
3198 /* Register HCI device */
3199 int hci_register_dev(struct hci_dev *hdev)
3203 if (!hdev->open || !hdev->close || !hdev->send)
3206 /* Do not allow HCI_AMP devices to register at index 0,
3207 * so the index can be used as the AMP controller ID.
3209 switch (hdev->dev_type) {
3211 id = ida_simple_get(&hci_index_ida, 0, 0, GFP_KERNEL);
3214 id = ida_simple_get(&hci_index_ida, 1, 0, GFP_KERNEL);
3223 sprintf(hdev->name, "hci%d", id);
3226 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
3228 hdev->workqueue = alloc_ordered_workqueue("%s", WQ_HIGHPRI, hdev->name);
3229 if (!hdev->workqueue) {
3234 hdev->req_workqueue = alloc_ordered_workqueue("%s", WQ_HIGHPRI,
3236 if (!hdev->req_workqueue) {
3237 destroy_workqueue(hdev->workqueue);
3242 if (!IS_ERR_OR_NULL(bt_debugfs))
3243 hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs);
3245 dev_set_name(&hdev->dev, "%s", hdev->name);
3247 error = device_add(&hdev->dev);
3251 hci_leds_init(hdev);
3253 hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev,
3254 RFKILL_TYPE_BLUETOOTH, &hci_rfkill_ops,
3257 if (rfkill_register(hdev->rfkill) < 0) {
3258 rfkill_destroy(hdev->rfkill);
3259 hdev->rfkill = NULL;
3263 if (hdev->rfkill && rfkill_blocked(hdev->rfkill))
3264 hci_dev_set_flag(hdev, HCI_RFKILLED);
3266 hci_dev_set_flag(hdev, HCI_SETUP);
3267 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
3269 if (hdev->dev_type == HCI_PRIMARY) {
3270 /* Assume BR/EDR support until proven otherwise (such as
3271 * through reading supported features during init.
3273 hci_dev_set_flag(hdev, HCI_BREDR_ENABLED);
3276 write_lock(&hci_dev_list_lock);
3277 list_add(&hdev->list, &hci_dev_list);
3278 write_unlock(&hci_dev_list_lock);
3280 /* Devices that are marked for raw-only usage are unconfigured
3281 * and should not be included in normal operation.
3283 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
3284 hci_dev_set_flag(hdev, HCI_UNCONFIGURED);
3286 hci_sock_dev_event(hdev, HCI_DEV_REG);
3289 queue_work(hdev->req_workqueue, &hdev->power_on);
3294 destroy_workqueue(hdev->workqueue);
3295 destroy_workqueue(hdev->req_workqueue);
3297 ida_simple_remove(&hci_index_ida, hdev->id);
3301 EXPORT_SYMBOL(hci_register_dev);
3303 /* Unregister HCI device */
3304 void hci_unregister_dev(struct hci_dev *hdev)
3308 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
3310 hci_dev_set_flag(hdev, HCI_UNREGISTER);
3314 write_lock(&hci_dev_list_lock);
3315 list_del(&hdev->list);
3316 write_unlock(&hci_dev_list_lock);
3318 cancel_work_sync(&hdev->power_on);
3320 hci_dev_do_close(hdev);
3322 if (!test_bit(HCI_INIT, &hdev->flags) &&
3323 !hci_dev_test_flag(hdev, HCI_SETUP) &&
3324 !hci_dev_test_flag(hdev, HCI_CONFIG)) {
3326 mgmt_index_removed(hdev);
3327 hci_dev_unlock(hdev);
3330 /* mgmt_index_removed should take care of emptying the
3332 BUG_ON(!list_empty(&hdev->mgmt_pending));
3334 hci_sock_dev_event(hdev, HCI_DEV_UNREG);
3337 rfkill_unregister(hdev->rfkill);
3338 rfkill_destroy(hdev->rfkill);
3341 device_del(&hdev->dev);
3343 debugfs_remove_recursive(hdev->debugfs);
3344 kfree_const(hdev->hw_info);
3345 kfree_const(hdev->fw_info);
3347 destroy_workqueue(hdev->workqueue);
3348 destroy_workqueue(hdev->req_workqueue);
3351 hci_bdaddr_list_clear(&hdev->blacklist);
3352 hci_bdaddr_list_clear(&hdev->whitelist);
3353 hci_uuids_clear(hdev);
3354 hci_link_keys_clear(hdev);
3355 hci_smp_ltks_clear(hdev);
3356 hci_smp_irks_clear(hdev);
3357 hci_remote_oob_data_clear(hdev);
3358 hci_adv_instances_clear(hdev);
3359 hci_bdaddr_list_clear(&hdev->le_white_list);
3360 hci_bdaddr_list_clear(&hdev->le_resolv_list);
3361 hci_conn_params_clear_all(hdev);
3362 hci_discovery_filter_clear(hdev);
3363 hci_dev_unlock(hdev);
3367 ida_simple_remove(&hci_index_ida, id);
3369 EXPORT_SYMBOL(hci_unregister_dev);
3371 /* Suspend HCI device */
3372 int hci_suspend_dev(struct hci_dev *hdev)
3374 hci_sock_dev_event(hdev, HCI_DEV_SUSPEND);
3377 EXPORT_SYMBOL(hci_suspend_dev);
3379 /* Resume HCI device */
3380 int hci_resume_dev(struct hci_dev *hdev)
3382 hci_sock_dev_event(hdev, HCI_DEV_RESUME);
3385 EXPORT_SYMBOL(hci_resume_dev);
3387 /* Reset HCI device */
3388 int hci_reset_dev(struct hci_dev *hdev)
3390 const u8 hw_err[] = { HCI_EV_HARDWARE_ERROR, 0x01, 0x00 };
3391 struct sk_buff *skb;
3393 skb = bt_skb_alloc(3, GFP_ATOMIC);
3397 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
3398 skb_put_data(skb, hw_err, 3);
3400 /* Send Hardware Error to upper stack */
3401 return hci_recv_frame(hdev, skb);
3403 EXPORT_SYMBOL(hci_reset_dev);
3405 /* Receive frame from HCI drivers */
3406 int hci_recv_frame(struct hci_dev *hdev, struct sk_buff *skb)
3408 if (!hdev || (!test_bit(HCI_UP, &hdev->flags)
3409 && !test_bit(HCI_INIT, &hdev->flags))) {
3414 if (hci_skb_pkt_type(skb) != HCI_EVENT_PKT &&
3415 hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
3416 hci_skb_pkt_type(skb) != HCI_SCODATA_PKT) {
3422 bt_cb(skb)->incoming = 1;
3425 __net_timestamp(skb);
3427 skb_queue_tail(&hdev->rx_q, skb);
3428 queue_work(hdev->workqueue, &hdev->rx_work);
3432 EXPORT_SYMBOL(hci_recv_frame);
3434 /* Receive diagnostic message from HCI drivers */
3435 int hci_recv_diag(struct hci_dev *hdev, struct sk_buff *skb)
3437 /* Mark as diagnostic packet */
3438 hci_skb_pkt_type(skb) = HCI_DIAG_PKT;
3441 __net_timestamp(skb);
3443 skb_queue_tail(&hdev->rx_q, skb);
3444 queue_work(hdev->workqueue, &hdev->rx_work);
3448 EXPORT_SYMBOL(hci_recv_diag);
3450 void hci_set_hw_info(struct hci_dev *hdev, const char *fmt, ...)
3454 va_start(vargs, fmt);
3455 kfree_const(hdev->hw_info);
3456 hdev->hw_info = kvasprintf_const(GFP_KERNEL, fmt, vargs);
3459 EXPORT_SYMBOL(hci_set_hw_info);
3461 void hci_set_fw_info(struct hci_dev *hdev, const char *fmt, ...)
3465 va_start(vargs, fmt);
3466 kfree_const(hdev->fw_info);
3467 hdev->fw_info = kvasprintf_const(GFP_KERNEL, fmt, vargs);
3470 EXPORT_SYMBOL(hci_set_fw_info);
3472 /* ---- Interface to upper protocols ---- */
3474 int hci_register_cb(struct hci_cb *cb)
3476 BT_DBG("%p name %s", cb, cb->name);
3478 mutex_lock(&hci_cb_list_lock);
3479 list_add_tail(&cb->list, &hci_cb_list);
3480 mutex_unlock(&hci_cb_list_lock);
3484 EXPORT_SYMBOL(hci_register_cb);
3486 int hci_unregister_cb(struct hci_cb *cb)
3488 BT_DBG("%p name %s", cb, cb->name);
3490 mutex_lock(&hci_cb_list_lock);
3491 list_del(&cb->list);
3492 mutex_unlock(&hci_cb_list_lock);
3496 EXPORT_SYMBOL(hci_unregister_cb);
3498 static void hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
3502 BT_DBG("%s type %d len %d", hdev->name, hci_skb_pkt_type(skb),
3506 __net_timestamp(skb);
3508 /* Send copy to monitor */
3509 hci_send_to_monitor(hdev, skb);
3511 if (atomic_read(&hdev->promisc)) {
3512 /* Send copy to the sockets */
3513 hci_send_to_sock(hdev, skb);
3516 /* Get rid of skb owner, prior to sending to the driver. */
3519 if (!test_bit(HCI_RUNNING, &hdev->flags)) {
3524 err = hdev->send(hdev, skb);
3526 bt_dev_err(hdev, "sending frame failed (%d)", err);
3531 /* Send HCI command */
3532 int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen,
3535 struct sk_buff *skb;
3537 BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen);
3539 skb = hci_prepare_cmd(hdev, opcode, plen, param);
3541 bt_dev_err(hdev, "no memory for command");
3545 /* Stand-alone HCI commands must be flagged as
3546 * single-command requests.
3548 bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
3550 skb_queue_tail(&hdev->cmd_q, skb);
3551 queue_work(hdev->workqueue, &hdev->cmd_work);
3556 int __hci_cmd_send(struct hci_dev *hdev, u16 opcode, u32 plen,
3559 struct sk_buff *skb;
3561 if (hci_opcode_ogf(opcode) != 0x3f) {
3562 /* A controller receiving a command shall respond with either
3563 * a Command Status Event or a Command Complete Event.
3564 * Therefore, all standard HCI commands must be sent via the
3565 * standard API, using hci_send_cmd or hci_cmd_sync helpers.
3566 * Some vendors do not comply with this rule for vendor-specific
3567 * commands and do not return any event. We want to support
3568 * unresponded commands for such cases only.
3570 bt_dev_err(hdev, "unresponded command not supported");
3574 skb = hci_prepare_cmd(hdev, opcode, plen, param);
3576 bt_dev_err(hdev, "no memory for command (opcode 0x%4.4x)",
3581 hci_send_frame(hdev, skb);
3585 EXPORT_SYMBOL(__hci_cmd_send);
3587 /* Get data from the previously sent command */
3588 void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode)
3590 struct hci_command_hdr *hdr;
3592 if (!hdev->sent_cmd)
3595 hdr = (void *) hdev->sent_cmd->data;
3597 if (hdr->opcode != cpu_to_le16(opcode))
3600 BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode);
3602 return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE;
3605 /* Send HCI command and wait for command commplete event */
3606 struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
3607 const void *param, u32 timeout)
3609 struct sk_buff *skb;
3611 if (!test_bit(HCI_UP, &hdev->flags))
3612 return ERR_PTR(-ENETDOWN);
3614 bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
3616 hci_req_sync_lock(hdev);
3617 skb = __hci_cmd_sync(hdev, opcode, plen, param, timeout);
3618 hci_req_sync_unlock(hdev);
3622 EXPORT_SYMBOL(hci_cmd_sync);
3625 static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags)
3627 struct hci_acl_hdr *hdr;
3630 skb_push(skb, HCI_ACL_HDR_SIZE);
3631 skb_reset_transport_header(skb);
3632 hdr = (struct hci_acl_hdr *)skb_transport_header(skb);
3633 hdr->handle = cpu_to_le16(hci_handle_pack(handle, flags));
3634 hdr->dlen = cpu_to_le16(len);
3637 static void hci_queue_acl(struct hci_chan *chan, struct sk_buff_head *queue,
3638 struct sk_buff *skb, __u16 flags)
3640 struct hci_conn *conn = chan->conn;
3641 struct hci_dev *hdev = conn->hdev;
3642 struct sk_buff *list;
3644 skb->len = skb_headlen(skb);
3647 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
3649 switch (hdev->dev_type) {
3651 hci_add_acl_hdr(skb, conn->handle, flags);
3654 hci_add_acl_hdr(skb, chan->handle, flags);
3657 bt_dev_err(hdev, "unknown dev_type %d", hdev->dev_type);
3661 list = skb_shinfo(skb)->frag_list;
3663 /* Non fragmented */
3664 BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len);
3666 skb_queue_tail(queue, skb);
3669 BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
3671 skb_shinfo(skb)->frag_list = NULL;
3673 /* Queue all fragments atomically. We need to use spin_lock_bh
3674 * here because of 6LoWPAN links, as there this function is
3675 * called from softirq and using normal spin lock could cause
3678 spin_lock_bh(&queue->lock);
3680 __skb_queue_tail(queue, skb);
3682 flags &= ~ACL_START;
3685 skb = list; list = list->next;
3687 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
3688 hci_add_acl_hdr(skb, conn->handle, flags);
3690 BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
3692 __skb_queue_tail(queue, skb);
3695 spin_unlock_bh(&queue->lock);
3699 void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags)
3701 struct hci_dev *hdev = chan->conn->hdev;
3703 BT_DBG("%s chan %p flags 0x%4.4x", hdev->name, chan, flags);
3705 hci_queue_acl(chan, &chan->data_q, skb, flags);
3707 queue_work(hdev->workqueue, &hdev->tx_work);
3711 void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb)
3713 struct hci_dev *hdev = conn->hdev;
3714 struct hci_sco_hdr hdr;
3716 BT_DBG("%s len %d", hdev->name, skb->len);
3718 hdr.handle = cpu_to_le16(conn->handle);
3719 hdr.dlen = skb->len;
3721 skb_push(skb, HCI_SCO_HDR_SIZE);
3722 skb_reset_transport_header(skb);
3723 memcpy(skb_transport_header(skb), &hdr, HCI_SCO_HDR_SIZE);
3725 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
3727 skb_queue_tail(&conn->data_q, skb);
3728 queue_work(hdev->workqueue, &hdev->tx_work);
3731 /* ---- HCI TX task (outgoing data) ---- */
3733 /* HCI Connection scheduler */
3734 static struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type,
3737 struct hci_conn_hash *h = &hdev->conn_hash;
3738 struct hci_conn *conn = NULL, *c;
3739 unsigned int num = 0, min = ~0;
3741 /* We don't have to lock device here. Connections are always
3742 * added and removed with TX task disabled. */
3746 list_for_each_entry_rcu(c, &h->list, list) {
3747 if (c->type != type || skb_queue_empty(&c->data_q))
3750 if (c->state != BT_CONNECTED && c->state != BT_CONFIG)
3755 if (c->sent < min) {
3760 if (hci_conn_num(hdev, type) == num)
3769 switch (conn->type) {
3771 cnt = hdev->acl_cnt;
3775 cnt = hdev->sco_cnt;
3778 cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt;
3782 bt_dev_err(hdev, "unknown link type %d", conn->type);
3790 BT_DBG("conn %p quote %d", conn, *quote);
3794 static void hci_link_tx_to(struct hci_dev *hdev, __u8 type)
3796 struct hci_conn_hash *h = &hdev->conn_hash;
3799 bt_dev_err(hdev, "link tx timeout");
3803 /* Kill stalled connections */
3804 list_for_each_entry_rcu(c, &h->list, list) {
3805 if (c->type == type && c->sent) {
3806 bt_dev_err(hdev, "killing stalled connection %pMR",
3808 hci_disconnect(c, HCI_ERROR_REMOTE_USER_TERM);
3815 static struct hci_chan *hci_chan_sent(struct hci_dev *hdev, __u8 type,
3818 struct hci_conn_hash *h = &hdev->conn_hash;
3819 struct hci_chan *chan = NULL;
3820 unsigned int num = 0, min = ~0, cur_prio = 0;
3821 struct hci_conn *conn;
3822 int cnt, q, conn_num = 0;
3824 BT_DBG("%s", hdev->name);
3828 list_for_each_entry_rcu(conn, &h->list, list) {
3829 struct hci_chan *tmp;
3831 if (conn->type != type)
3834 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
3839 list_for_each_entry_rcu(tmp, &conn->chan_list, list) {
3840 struct sk_buff *skb;
3842 if (skb_queue_empty(&tmp->data_q))
3845 skb = skb_peek(&tmp->data_q);
3846 if (skb->priority < cur_prio)
3849 if (skb->priority > cur_prio) {
3852 cur_prio = skb->priority;
3857 if (conn->sent < min) {
3863 if (hci_conn_num(hdev, type) == conn_num)
3872 switch (chan->conn->type) {
3874 cnt = hdev->acl_cnt;
3877 cnt = hdev->block_cnt;
3881 cnt = hdev->sco_cnt;
3884 cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt;
3888 bt_dev_err(hdev, "unknown link type %d", chan->conn->type);
3893 BT_DBG("chan %p quote %d", chan, *quote);
3897 static void hci_prio_recalculate(struct hci_dev *hdev, __u8 type)
3899 struct hci_conn_hash *h = &hdev->conn_hash;
3900 struct hci_conn *conn;
3903 BT_DBG("%s", hdev->name);
3907 list_for_each_entry_rcu(conn, &h->list, list) {
3908 struct hci_chan *chan;
3910 if (conn->type != type)
3913 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
3918 list_for_each_entry_rcu(chan, &conn->chan_list, list) {
3919 struct sk_buff *skb;
3926 if (skb_queue_empty(&chan->data_q))
3929 skb = skb_peek(&chan->data_q);
3930 if (skb->priority >= HCI_PRIO_MAX - 1)
3933 skb->priority = HCI_PRIO_MAX - 1;
3935 BT_DBG("chan %p skb %p promoted to %d", chan, skb,
3939 if (hci_conn_num(hdev, type) == num)
3947 static inline int __get_blocks(struct hci_dev *hdev, struct sk_buff *skb)
3949 /* Calculate count of blocks used by this packet */
3950 return DIV_ROUND_UP(skb->len - HCI_ACL_HDR_SIZE, hdev->block_len);
3953 static void __check_timeout(struct hci_dev *hdev, unsigned int cnt)
3955 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
3956 /* ACL tx timeout must be longer than maximum
3957 * link supervision timeout (40.9 seconds) */
3958 if (!cnt && time_after(jiffies, hdev->acl_last_tx +
3959 HCI_ACL_TX_TIMEOUT))
3960 hci_link_tx_to(hdev, ACL_LINK);
3964 static void hci_sched_acl_pkt(struct hci_dev *hdev)
3966 unsigned int cnt = hdev->acl_cnt;
3967 struct hci_chan *chan;
3968 struct sk_buff *skb;
3971 __check_timeout(hdev, cnt);
3973 while (hdev->acl_cnt &&
3974 (chan = hci_chan_sent(hdev, ACL_LINK, "e))) {
3975 u32 priority = (skb_peek(&chan->data_q))->priority;
3976 while (quote-- && (skb = skb_peek(&chan->data_q))) {
3977 BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
3978 skb->len, skb->priority);
3980 /* Stop if priority has changed */
3981 if (skb->priority < priority)
3984 skb = skb_dequeue(&chan->data_q);
3986 hci_conn_enter_active_mode(chan->conn,
3987 bt_cb(skb)->force_active);
3989 hci_send_frame(hdev, skb);
3990 hdev->acl_last_tx = jiffies;
3998 if (cnt != hdev->acl_cnt)
3999 hci_prio_recalculate(hdev, ACL_LINK);
4002 static void hci_sched_acl_blk(struct hci_dev *hdev)
4004 unsigned int cnt = hdev->block_cnt;
4005 struct hci_chan *chan;
4006 struct sk_buff *skb;
4010 __check_timeout(hdev, cnt);
4012 BT_DBG("%s", hdev->name);
4014 if (hdev->dev_type == HCI_AMP)
4019 while (hdev->block_cnt > 0 &&
4020 (chan = hci_chan_sent(hdev, type, "e))) {
4021 u32 priority = (skb_peek(&chan->data_q))->priority;
4022 while (quote > 0 && (skb = skb_peek(&chan->data_q))) {
4025 BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
4026 skb->len, skb->priority);
4028 /* Stop if priority has changed */
4029 if (skb->priority < priority)
4032 skb = skb_dequeue(&chan->data_q);
4034 blocks = __get_blocks(hdev, skb);
4035 if (blocks > hdev->block_cnt)
4038 hci_conn_enter_active_mode(chan->conn,
4039 bt_cb(skb)->force_active);
4041 hci_send_frame(hdev, skb);
4042 hdev->acl_last_tx = jiffies;
4044 hdev->block_cnt -= blocks;
4047 chan->sent += blocks;
4048 chan->conn->sent += blocks;
4052 if (cnt != hdev->block_cnt)
4053 hci_prio_recalculate(hdev, type);
4056 static void hci_sched_acl(struct hci_dev *hdev)
4058 BT_DBG("%s", hdev->name);
4060 /* No ACL link over BR/EDR controller */
4061 if (!hci_conn_num(hdev, ACL_LINK) && hdev->dev_type == HCI_PRIMARY)
4064 /* No AMP link over AMP controller */
4065 if (!hci_conn_num(hdev, AMP_LINK) && hdev->dev_type == HCI_AMP)
4068 switch (hdev->flow_ctl_mode) {
4069 case HCI_FLOW_CTL_MODE_PACKET_BASED:
4070 hci_sched_acl_pkt(hdev);
4073 case HCI_FLOW_CTL_MODE_BLOCK_BASED:
4074 hci_sched_acl_blk(hdev);
4080 static void hci_sched_sco(struct hci_dev *hdev)
4082 struct hci_conn *conn;
4083 struct sk_buff *skb;
4086 BT_DBG("%s", hdev->name);
4088 if (!hci_conn_num(hdev, SCO_LINK))
4091 while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, "e))) {
4092 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
4093 BT_DBG("skb %p len %d", skb, skb->len);
4094 hci_send_frame(hdev, skb);
4097 if (conn->sent == ~0)
4103 static void hci_sched_esco(struct hci_dev *hdev)
4105 struct hci_conn *conn;
4106 struct sk_buff *skb;
4109 BT_DBG("%s", hdev->name);
4111 if (!hci_conn_num(hdev, ESCO_LINK))
4114 while (hdev->sco_cnt && (conn = hci_low_sent(hdev, ESCO_LINK,
4116 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
4117 BT_DBG("skb %p len %d", skb, skb->len);
4118 hci_send_frame(hdev, skb);
4121 if (conn->sent == ~0)
4127 static void hci_sched_le(struct hci_dev *hdev)
4129 struct hci_chan *chan;
4130 struct sk_buff *skb;
4131 int quote, cnt, tmp;
4133 BT_DBG("%s", hdev->name);
4135 if (!hci_conn_num(hdev, LE_LINK))
4138 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
4139 /* LE tx timeout must be longer than maximum
4140 * link supervision timeout (40.9 seconds) */
4141 if (!hdev->le_cnt && hdev->le_pkts &&
4142 time_after(jiffies, hdev->le_last_tx + HZ * 45))
4143 hci_link_tx_to(hdev, LE_LINK);
4146 cnt = hdev->le_pkts ? hdev->le_cnt : hdev->acl_cnt;
4148 while (cnt && (chan = hci_chan_sent(hdev, LE_LINK, "e))) {
4149 u32 priority = (skb_peek(&chan->data_q))->priority;
4150 while (quote-- && (skb = skb_peek(&chan->data_q))) {
4151 BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
4152 skb->len, skb->priority);
4154 /* Stop if priority has changed */
4155 if (skb->priority < priority)
4158 skb = skb_dequeue(&chan->data_q);
4160 hci_send_frame(hdev, skb);
4161 hdev->le_last_tx = jiffies;
4172 hdev->acl_cnt = cnt;
4175 hci_prio_recalculate(hdev, LE_LINK);
4178 static void hci_tx_work(struct work_struct *work)
4180 struct hci_dev *hdev = container_of(work, struct hci_dev, tx_work);
4181 struct sk_buff *skb;
4183 BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt,
4184 hdev->sco_cnt, hdev->le_cnt);
4186 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4187 /* Schedule queues and send stuff to HCI driver */
4188 hci_sched_acl(hdev);
4189 hci_sched_sco(hdev);
4190 hci_sched_esco(hdev);
4194 /* Send next queued raw (unknown type) packet */
4195 while ((skb = skb_dequeue(&hdev->raw_q)))
4196 hci_send_frame(hdev, skb);
4199 /* ----- HCI RX task (incoming data processing) ----- */
4201 /* ACL data packet */
4202 static void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb)
4204 struct hci_acl_hdr *hdr = (void *) skb->data;
4205 struct hci_conn *conn;
4206 __u16 handle, flags;
4208 skb_pull(skb, HCI_ACL_HDR_SIZE);
4210 handle = __le16_to_cpu(hdr->handle);
4211 flags = hci_flags(handle);
4212 handle = hci_handle(handle);
4214 BT_DBG("%s len %d handle 0x%4.4x flags 0x%4.4x", hdev->name, skb->len,
4217 hdev->stat.acl_rx++;
4220 conn = hci_conn_hash_lookup_handle(hdev, handle);
4221 hci_dev_unlock(hdev);
4224 hci_conn_enter_active_mode(conn, BT_POWER_FORCE_ACTIVE_OFF);
4226 /* Send to upper protocol */
4227 l2cap_recv_acldata(conn, skb, flags);
4230 bt_dev_err(hdev, "ACL packet for unknown connection handle %d",
4237 /* SCO data packet */
4238 static void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb)
4240 struct hci_sco_hdr *hdr = (void *) skb->data;
4241 struct hci_conn *conn;
4244 skb_pull(skb, HCI_SCO_HDR_SIZE);
4246 handle = __le16_to_cpu(hdr->handle);
4248 BT_DBG("%s len %d handle 0x%4.4x", hdev->name, skb->len, handle);
4250 hdev->stat.sco_rx++;
4253 conn = hci_conn_hash_lookup_handle(hdev, handle);
4254 hci_dev_unlock(hdev);
4257 /* Send to upper protocol */
4258 sco_recv_scodata(conn, skb);
4261 bt_dev_err(hdev, "SCO packet for unknown connection handle %d",
4268 static bool hci_req_is_complete(struct hci_dev *hdev)
4270 struct sk_buff *skb;
4272 skb = skb_peek(&hdev->cmd_q);
4276 return (bt_cb(skb)->hci.req_flags & HCI_REQ_START);
4279 static void hci_resend_last(struct hci_dev *hdev)
4281 struct hci_command_hdr *sent;
4282 struct sk_buff *skb;
4285 if (!hdev->sent_cmd)
4288 sent = (void *) hdev->sent_cmd->data;
4289 opcode = __le16_to_cpu(sent->opcode);
4290 if (opcode == HCI_OP_RESET)
4293 skb = skb_clone(hdev->sent_cmd, GFP_KERNEL);
4297 skb_queue_head(&hdev->cmd_q, skb);
4298 queue_work(hdev->workqueue, &hdev->cmd_work);
4301 void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status,
4302 hci_req_complete_t *req_complete,
4303 hci_req_complete_skb_t *req_complete_skb)
4305 struct sk_buff *skb;
4306 unsigned long flags;
4308 BT_DBG("opcode 0x%04x status 0x%02x", opcode, status);
4310 /* If the completed command doesn't match the last one that was
4311 * sent we need to do special handling of it.
4313 if (!hci_sent_cmd_data(hdev, opcode)) {
4314 /* Some CSR based controllers generate a spontaneous
4315 * reset complete event during init and any pending
4316 * command will never be completed. In such a case we
4317 * need to resend whatever was the last sent
4320 if (test_bit(HCI_INIT, &hdev->flags) && opcode == HCI_OP_RESET)
4321 hci_resend_last(hdev);
4326 /* If we reach this point this event matches the last command sent */
4327 hci_dev_clear_flag(hdev, HCI_CMD_PENDING);
4329 /* If the command succeeded and there's still more commands in
4330 * this request the request is not yet complete.
4332 if (!status && !hci_req_is_complete(hdev))
4335 /* If this was the last command in a request the complete
4336 * callback would be found in hdev->sent_cmd instead of the
4337 * command queue (hdev->cmd_q).
4339 if (bt_cb(hdev->sent_cmd)->hci.req_flags & HCI_REQ_SKB) {
4340 *req_complete_skb = bt_cb(hdev->sent_cmd)->hci.req_complete_skb;
4344 if (bt_cb(hdev->sent_cmd)->hci.req_complete) {
4345 *req_complete = bt_cb(hdev->sent_cmd)->hci.req_complete;
4349 /* Remove all pending commands belonging to this request */
4350 spin_lock_irqsave(&hdev->cmd_q.lock, flags);
4351 while ((skb = __skb_dequeue(&hdev->cmd_q))) {
4352 if (bt_cb(skb)->hci.req_flags & HCI_REQ_START) {
4353 __skb_queue_head(&hdev->cmd_q, skb);
4357 if (bt_cb(skb)->hci.req_flags & HCI_REQ_SKB)
4358 *req_complete_skb = bt_cb(skb)->hci.req_complete_skb;
4360 *req_complete = bt_cb(skb)->hci.req_complete;
4363 spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
4366 static void hci_rx_work(struct work_struct *work)
4368 struct hci_dev *hdev = container_of(work, struct hci_dev, rx_work);
4369 struct sk_buff *skb;
4371 BT_DBG("%s", hdev->name);
4373 while ((skb = skb_dequeue(&hdev->rx_q))) {
4374 /* Send copy to monitor */
4375 hci_send_to_monitor(hdev, skb);
4377 if (atomic_read(&hdev->promisc)) {
4378 /* Send copy to the sockets */
4379 hci_send_to_sock(hdev, skb);
4382 /* If the device has been opened in HCI_USER_CHANNEL,
4383 * the userspace has exclusive access to device.
4384 * When device is HCI_INIT, we still need to process
4385 * the data packets to the driver in order
4386 * to complete its setup().
4388 if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
4389 !test_bit(HCI_INIT, &hdev->flags)) {
4394 if (test_bit(HCI_INIT, &hdev->flags)) {
4395 /* Don't process data packets in this states. */
4396 switch (hci_skb_pkt_type(skb)) {
4397 case HCI_ACLDATA_PKT:
4398 case HCI_SCODATA_PKT:
4405 switch (hci_skb_pkt_type(skb)) {
4407 BT_DBG("%s Event packet", hdev->name);
4408 hci_event_packet(hdev, skb);
4411 case HCI_ACLDATA_PKT:
4412 BT_DBG("%s ACL data packet", hdev->name);
4413 hci_acldata_packet(hdev, skb);
4416 case HCI_SCODATA_PKT:
4417 BT_DBG("%s SCO data packet", hdev->name);
4418 hci_scodata_packet(hdev, skb);
4428 static void hci_cmd_work(struct work_struct *work)
4430 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_work);
4431 struct sk_buff *skb;
4433 BT_DBG("%s cmd_cnt %d cmd queued %d", hdev->name,
4434 atomic_read(&hdev->cmd_cnt), skb_queue_len(&hdev->cmd_q));
4436 /* Send queued commands */
4437 if (atomic_read(&hdev->cmd_cnt)) {
4438 skb = skb_dequeue(&hdev->cmd_q);
4442 kfree_skb(hdev->sent_cmd);
4444 hdev->sent_cmd = skb_clone(skb, GFP_KERNEL);
4445 if (hdev->sent_cmd) {
4446 if (hci_req_status_pend(hdev))
4447 hci_dev_set_flag(hdev, HCI_CMD_PENDING);
4448 atomic_dec(&hdev->cmd_cnt);
4449 hci_send_frame(hdev, skb);
4450 if (test_bit(HCI_RESET, &hdev->flags))
4451 cancel_delayed_work(&hdev->cmd_timer);
4453 schedule_delayed_work(&hdev->cmd_timer,
4456 skb_queue_head(&hdev->cmd_q, skb);
4457 queue_work(hdev->workqueue, &hdev->cmd_work);