xref: /openbmc/linux/net/bluetooth/hci_core.c (revision a3451d27)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds    BlueZ - Bluetooth protocol stack for Linux
31da177e4SLinus Torvalds    Copyright (C) 2000-2001 Qualcomm Incorporated
4590051deSGustavo F. Padovan    Copyright (C) 2011 ProFUSION Embedded Systems
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds    This program is free software; you can redistribute it and/or modify
91da177e4SLinus Torvalds    it under the terms of the GNU General Public License version 2 as
101da177e4SLinus Torvalds    published by the Free Software Foundation;
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
131da177e4SLinus Torvalds    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
141da177e4SLinus Torvalds    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
151da177e4SLinus Torvalds    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
161da177e4SLinus Torvalds    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
171da177e4SLinus Torvalds    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
181da177e4SLinus Torvalds    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
191da177e4SLinus Torvalds    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
221da177e4SLinus Torvalds    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
231da177e4SLinus Torvalds    SOFTWARE IS DISCLAIMED.
241da177e4SLinus Torvalds */
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds /* Bluetooth HCI core. */
271da177e4SLinus Torvalds 
288c520a59SGustavo Padovan #include <linux/export.h>
293df92b31SSasha Levin #include <linux/idr.h>
30611b30f7SMarcel Holtmann #include <linux/rfkill.h>
31baf27f6eSMarcel Holtmann #include <linux/debugfs.h>
3299780a7bSJohan Hedberg #include <linux/crypto.h>
3347219839SMarcel Holtmann #include <asm/unaligned.h>
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #include <net/bluetooth/bluetooth.h>
361da177e4SLinus Torvalds #include <net/bluetooth/hci_core.h>
374bc58f51SJohan Hedberg #include <net/bluetooth/l2cap.h>
38af58925cSMarcel Holtmann #include <net/bluetooth/mgmt.h>
391da177e4SLinus Torvalds 
40970c4e46SJohan Hedberg #include "smp.h"
41970c4e46SJohan Hedberg 
42b78752ccSMarcel Holtmann static void hci_rx_work(struct work_struct *work);
43c347b765SGustavo F. Padovan static void hci_cmd_work(struct work_struct *work);
443eff45eaSGustavo F. Padovan static void hci_tx_work(struct work_struct *work);
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds /* HCI device list */
471da177e4SLinus Torvalds LIST_HEAD(hci_dev_list);
481da177e4SLinus Torvalds DEFINE_RWLOCK(hci_dev_list_lock);
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds /* HCI callback list */
511da177e4SLinus Torvalds LIST_HEAD(hci_cb_list);
521da177e4SLinus Torvalds DEFINE_RWLOCK(hci_cb_list_lock);
531da177e4SLinus Torvalds 
543df92b31SSasha Levin /* HCI ID Numbering */
553df92b31SSasha Levin static DEFINE_IDA(hci_index_ida);
563df92b31SSasha Levin 
571da177e4SLinus Torvalds /* ---- HCI notifications ---- */
581da177e4SLinus Torvalds 
596516455dSMarcel Holtmann static void hci_notify(struct hci_dev *hdev, int event)
601da177e4SLinus Torvalds {
61040030efSMarcel Holtmann 	hci_sock_dev_event(hdev, event);
621da177e4SLinus Torvalds }
631da177e4SLinus Torvalds 
64baf27f6eSMarcel Holtmann /* ---- HCI debugfs entries ---- */
65baf27f6eSMarcel Holtmann 
664b4148e9SMarcel Holtmann static ssize_t dut_mode_read(struct file *file, char __user *user_buf,
674b4148e9SMarcel Holtmann 			     size_t count, loff_t *ppos)
684b4148e9SMarcel Holtmann {
694b4148e9SMarcel Holtmann 	struct hci_dev *hdev = file->private_data;
704b4148e9SMarcel Holtmann 	char buf[3];
714b4148e9SMarcel Holtmann 
72111902f7SMarcel Holtmann 	buf[0] = test_bit(HCI_DUT_MODE, &hdev->dbg_flags) ? 'Y': 'N';
734b4148e9SMarcel Holtmann 	buf[1] = '\n';
744b4148e9SMarcel Holtmann 	buf[2] = '\0';
754b4148e9SMarcel Holtmann 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
764b4148e9SMarcel Holtmann }
774b4148e9SMarcel Holtmann 
784b4148e9SMarcel Holtmann static ssize_t dut_mode_write(struct file *file, const char __user *user_buf,
794b4148e9SMarcel Holtmann 			      size_t count, loff_t *ppos)
804b4148e9SMarcel Holtmann {
814b4148e9SMarcel Holtmann 	struct hci_dev *hdev = file->private_data;
824b4148e9SMarcel Holtmann 	struct sk_buff *skb;
834b4148e9SMarcel Holtmann 	char buf[32];
844b4148e9SMarcel Holtmann 	size_t buf_size = min(count, (sizeof(buf)-1));
854b4148e9SMarcel Holtmann 	bool enable;
864b4148e9SMarcel Holtmann 	int err;
874b4148e9SMarcel Holtmann 
884b4148e9SMarcel Holtmann 	if (!test_bit(HCI_UP, &hdev->flags))
894b4148e9SMarcel Holtmann 		return -ENETDOWN;
904b4148e9SMarcel Holtmann 
914b4148e9SMarcel Holtmann 	if (copy_from_user(buf, user_buf, buf_size))
924b4148e9SMarcel Holtmann 		return -EFAULT;
934b4148e9SMarcel Holtmann 
944b4148e9SMarcel Holtmann 	buf[buf_size] = '\0';
954b4148e9SMarcel Holtmann 	if (strtobool(buf, &enable))
964b4148e9SMarcel Holtmann 		return -EINVAL;
974b4148e9SMarcel Holtmann 
98111902f7SMarcel Holtmann 	if (enable == test_bit(HCI_DUT_MODE, &hdev->dbg_flags))
994b4148e9SMarcel Holtmann 		return -EALREADY;
1004b4148e9SMarcel Holtmann 
1014b4148e9SMarcel Holtmann 	hci_req_lock(hdev);
1024b4148e9SMarcel Holtmann 	if (enable)
1034b4148e9SMarcel Holtmann 		skb = __hci_cmd_sync(hdev, HCI_OP_ENABLE_DUT_MODE, 0, NULL,
1044b4148e9SMarcel Holtmann 				     HCI_CMD_TIMEOUT);
1054b4148e9SMarcel Holtmann 	else
1064b4148e9SMarcel Holtmann 		skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL,
1074b4148e9SMarcel Holtmann 				     HCI_CMD_TIMEOUT);
1084b4148e9SMarcel Holtmann 	hci_req_unlock(hdev);
1094b4148e9SMarcel Holtmann 
1104b4148e9SMarcel Holtmann 	if (IS_ERR(skb))
1114b4148e9SMarcel Holtmann 		return PTR_ERR(skb);
1124b4148e9SMarcel Holtmann 
1134b4148e9SMarcel Holtmann 	err = -bt_to_errno(skb->data[0]);
1144b4148e9SMarcel Holtmann 	kfree_skb(skb);
1154b4148e9SMarcel Holtmann 
1164b4148e9SMarcel Holtmann 	if (err < 0)
1174b4148e9SMarcel Holtmann 		return err;
1184b4148e9SMarcel Holtmann 
119111902f7SMarcel Holtmann 	change_bit(HCI_DUT_MODE, &hdev->dbg_flags);
1204b4148e9SMarcel Holtmann 
1214b4148e9SMarcel Holtmann 	return count;
1224b4148e9SMarcel Holtmann }
1234b4148e9SMarcel Holtmann 
1244b4148e9SMarcel Holtmann static const struct file_operations dut_mode_fops = {
1254b4148e9SMarcel Holtmann 	.open		= simple_open,
1264b4148e9SMarcel Holtmann 	.read		= dut_mode_read,
1274b4148e9SMarcel Holtmann 	.write		= dut_mode_write,
1284b4148e9SMarcel Holtmann 	.llseek		= default_llseek,
1294b4148e9SMarcel Holtmann };
1304b4148e9SMarcel Holtmann 
131dfb826a8SMarcel Holtmann static int features_show(struct seq_file *f, void *ptr)
132dfb826a8SMarcel Holtmann {
133dfb826a8SMarcel Holtmann 	struct hci_dev *hdev = f->private;
134dfb826a8SMarcel Holtmann 	u8 p;
135dfb826a8SMarcel Holtmann 
136dfb826a8SMarcel Holtmann 	hci_dev_lock(hdev);
137dfb826a8SMarcel Holtmann 	for (p = 0; p < HCI_MAX_PAGES && p <= hdev->max_page; p++) {
138cfbb2b5bSMarcel Holtmann 		seq_printf(f, "%2u: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x "
139dfb826a8SMarcel Holtmann 			   "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n", p,
140dfb826a8SMarcel Holtmann 			   hdev->features[p][0], hdev->features[p][1],
141dfb826a8SMarcel Holtmann 			   hdev->features[p][2], hdev->features[p][3],
142dfb826a8SMarcel Holtmann 			   hdev->features[p][4], hdev->features[p][5],
143dfb826a8SMarcel Holtmann 			   hdev->features[p][6], hdev->features[p][7]);
144dfb826a8SMarcel Holtmann 	}
145cfbb2b5bSMarcel Holtmann 	if (lmp_le_capable(hdev))
146cfbb2b5bSMarcel Holtmann 		seq_printf(f, "LE: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x "
147cfbb2b5bSMarcel Holtmann 			   "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n",
148cfbb2b5bSMarcel Holtmann 			   hdev->le_features[0], hdev->le_features[1],
149cfbb2b5bSMarcel Holtmann 			   hdev->le_features[2], hdev->le_features[3],
150cfbb2b5bSMarcel Holtmann 			   hdev->le_features[4], hdev->le_features[5],
151cfbb2b5bSMarcel Holtmann 			   hdev->le_features[6], hdev->le_features[7]);
152dfb826a8SMarcel Holtmann 	hci_dev_unlock(hdev);
153dfb826a8SMarcel Holtmann 
154dfb826a8SMarcel Holtmann 	return 0;
155dfb826a8SMarcel Holtmann }
156dfb826a8SMarcel Holtmann 
157dfb826a8SMarcel Holtmann static int features_open(struct inode *inode, struct file *file)
158dfb826a8SMarcel Holtmann {
159dfb826a8SMarcel Holtmann 	return single_open(file, features_show, inode->i_private);
160dfb826a8SMarcel Holtmann }
161dfb826a8SMarcel Holtmann 
162dfb826a8SMarcel Holtmann static const struct file_operations features_fops = {
163dfb826a8SMarcel Holtmann 	.open		= features_open,
164dfb826a8SMarcel Holtmann 	.read		= seq_read,
165dfb826a8SMarcel Holtmann 	.llseek		= seq_lseek,
166dfb826a8SMarcel Holtmann 	.release	= single_release,
167dfb826a8SMarcel Holtmann };
168dfb826a8SMarcel Holtmann 
16970afe0b8SMarcel Holtmann static int blacklist_show(struct seq_file *f, void *p)
17070afe0b8SMarcel Holtmann {
17170afe0b8SMarcel Holtmann 	struct hci_dev *hdev = f->private;
17270afe0b8SMarcel Holtmann 	struct bdaddr_list *b;
17370afe0b8SMarcel Holtmann 
17470afe0b8SMarcel Holtmann 	hci_dev_lock(hdev);
17570afe0b8SMarcel Holtmann 	list_for_each_entry(b, &hdev->blacklist, list)
176b25f0785SMarcel Holtmann 		seq_printf(f, "%pMR (type %u)\n", &b->bdaddr, b->bdaddr_type);
17770afe0b8SMarcel Holtmann 	hci_dev_unlock(hdev);
17870afe0b8SMarcel Holtmann 
17970afe0b8SMarcel Holtmann 	return 0;
18070afe0b8SMarcel Holtmann }
18170afe0b8SMarcel Holtmann 
18270afe0b8SMarcel Holtmann static int blacklist_open(struct inode *inode, struct file *file)
18370afe0b8SMarcel Holtmann {
18470afe0b8SMarcel Holtmann 	return single_open(file, blacklist_show, inode->i_private);
18570afe0b8SMarcel Holtmann }
18670afe0b8SMarcel Holtmann 
18770afe0b8SMarcel Holtmann static const struct file_operations blacklist_fops = {
18870afe0b8SMarcel Holtmann 	.open		= blacklist_open,
18970afe0b8SMarcel Holtmann 	.read		= seq_read,
19070afe0b8SMarcel Holtmann 	.llseek		= seq_lseek,
19170afe0b8SMarcel Holtmann 	.release	= single_release,
19270afe0b8SMarcel Holtmann };
19370afe0b8SMarcel Holtmann 
19447219839SMarcel Holtmann static int uuids_show(struct seq_file *f, void *p)
19547219839SMarcel Holtmann {
19647219839SMarcel Holtmann 	struct hci_dev *hdev = f->private;
19747219839SMarcel Holtmann 	struct bt_uuid *uuid;
19847219839SMarcel Holtmann 
19947219839SMarcel Holtmann 	hci_dev_lock(hdev);
20047219839SMarcel Holtmann 	list_for_each_entry(uuid, &hdev->uuids, list) {
20158f01aa9SMarcel Holtmann 		u8 i, val[16];
20247219839SMarcel Holtmann 
20358f01aa9SMarcel Holtmann 		/* The Bluetooth UUID values are stored in big endian,
20458f01aa9SMarcel Holtmann 		 * but with reversed byte order. So convert them into
20558f01aa9SMarcel Holtmann 		 * the right order for the %pUb modifier.
20658f01aa9SMarcel Holtmann 		 */
20758f01aa9SMarcel Holtmann 		for (i = 0; i < 16; i++)
20858f01aa9SMarcel Holtmann 			val[i] = uuid->uuid[15 - i];
20947219839SMarcel Holtmann 
21058f01aa9SMarcel Holtmann 		seq_printf(f, "%pUb\n", val);
21147219839SMarcel Holtmann 	}
21247219839SMarcel Holtmann 	hci_dev_unlock(hdev);
21347219839SMarcel Holtmann 
21447219839SMarcel Holtmann 	return 0;
21547219839SMarcel Holtmann }
21647219839SMarcel Holtmann 
21747219839SMarcel Holtmann static int uuids_open(struct inode *inode, struct file *file)
21847219839SMarcel Holtmann {
21947219839SMarcel Holtmann 	return single_open(file, uuids_show, inode->i_private);
22047219839SMarcel Holtmann }
22147219839SMarcel Holtmann 
22247219839SMarcel Holtmann static const struct file_operations uuids_fops = {
22347219839SMarcel Holtmann 	.open		= uuids_open,
22447219839SMarcel Holtmann 	.read		= seq_read,
22547219839SMarcel Holtmann 	.llseek		= seq_lseek,
22647219839SMarcel Holtmann 	.release	= single_release,
22747219839SMarcel Holtmann };
22847219839SMarcel Holtmann 
229baf27f6eSMarcel Holtmann static int inquiry_cache_show(struct seq_file *f, void *p)
230baf27f6eSMarcel Holtmann {
231baf27f6eSMarcel Holtmann 	struct hci_dev *hdev = f->private;
232baf27f6eSMarcel Holtmann 	struct discovery_state *cache = &hdev->discovery;
233baf27f6eSMarcel Holtmann 	struct inquiry_entry *e;
234baf27f6eSMarcel Holtmann 
235baf27f6eSMarcel Holtmann 	hci_dev_lock(hdev);
236baf27f6eSMarcel Holtmann 
237baf27f6eSMarcel Holtmann 	list_for_each_entry(e, &cache->all, all) {
238baf27f6eSMarcel Holtmann 		struct inquiry_data *data = &e->data;
239baf27f6eSMarcel Holtmann 		seq_printf(f, "%pMR %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n",
240baf27f6eSMarcel Holtmann 			   &data->bdaddr,
241baf27f6eSMarcel Holtmann 			   data->pscan_rep_mode, data->pscan_period_mode,
242baf27f6eSMarcel Holtmann 			   data->pscan_mode, data->dev_class[2],
243baf27f6eSMarcel Holtmann 			   data->dev_class[1], data->dev_class[0],
244baf27f6eSMarcel Holtmann 			   __le16_to_cpu(data->clock_offset),
245baf27f6eSMarcel Holtmann 			   data->rssi, data->ssp_mode, e->timestamp);
246baf27f6eSMarcel Holtmann 	}
247baf27f6eSMarcel Holtmann 
248baf27f6eSMarcel Holtmann 	hci_dev_unlock(hdev);
249baf27f6eSMarcel Holtmann 
250baf27f6eSMarcel Holtmann 	return 0;
251baf27f6eSMarcel Holtmann }
252baf27f6eSMarcel Holtmann 
253baf27f6eSMarcel Holtmann static int inquiry_cache_open(struct inode *inode, struct file *file)
254baf27f6eSMarcel Holtmann {
255baf27f6eSMarcel Holtmann 	return single_open(file, inquiry_cache_show, inode->i_private);
256baf27f6eSMarcel Holtmann }
257baf27f6eSMarcel Holtmann 
258baf27f6eSMarcel Holtmann static const struct file_operations inquiry_cache_fops = {
259baf27f6eSMarcel Holtmann 	.open		= inquiry_cache_open,
260baf27f6eSMarcel Holtmann 	.read		= seq_read,
261baf27f6eSMarcel Holtmann 	.llseek		= seq_lseek,
262baf27f6eSMarcel Holtmann 	.release	= single_release,
263baf27f6eSMarcel Holtmann };
264baf27f6eSMarcel Holtmann 
26502d08d15SMarcel Holtmann static int link_keys_show(struct seq_file *f, void *ptr)
26602d08d15SMarcel Holtmann {
26702d08d15SMarcel Holtmann 	struct hci_dev *hdev = f->private;
26802d08d15SMarcel Holtmann 	struct list_head *p, *n;
26902d08d15SMarcel Holtmann 
27002d08d15SMarcel Holtmann 	hci_dev_lock(hdev);
27102d08d15SMarcel Holtmann 	list_for_each_safe(p, n, &hdev->link_keys) {
27202d08d15SMarcel Holtmann 		struct link_key *key = list_entry(p, struct link_key, list);
27302d08d15SMarcel Holtmann 		seq_printf(f, "%pMR %u %*phN %u\n", &key->bdaddr, key->type,
27402d08d15SMarcel Holtmann 			   HCI_LINK_KEY_SIZE, key->val, key->pin_len);
27502d08d15SMarcel Holtmann 	}
27602d08d15SMarcel Holtmann 	hci_dev_unlock(hdev);
27702d08d15SMarcel Holtmann 
27802d08d15SMarcel Holtmann 	return 0;
27902d08d15SMarcel Holtmann }
28002d08d15SMarcel Holtmann 
28102d08d15SMarcel Holtmann static int link_keys_open(struct inode *inode, struct file *file)
28202d08d15SMarcel Holtmann {
28302d08d15SMarcel Holtmann 	return single_open(file, link_keys_show, inode->i_private);
28402d08d15SMarcel Holtmann }
28502d08d15SMarcel Holtmann 
28602d08d15SMarcel Holtmann static const struct file_operations link_keys_fops = {
28702d08d15SMarcel Holtmann 	.open		= link_keys_open,
28802d08d15SMarcel Holtmann 	.read		= seq_read,
28902d08d15SMarcel Holtmann 	.llseek		= seq_lseek,
29002d08d15SMarcel Holtmann 	.release	= single_release,
29102d08d15SMarcel Holtmann };
29202d08d15SMarcel Holtmann 
293babdbb3cSMarcel Holtmann static int dev_class_show(struct seq_file *f, void *ptr)
294babdbb3cSMarcel Holtmann {
295babdbb3cSMarcel Holtmann 	struct hci_dev *hdev = f->private;
296babdbb3cSMarcel Holtmann 
297babdbb3cSMarcel Holtmann 	hci_dev_lock(hdev);
298babdbb3cSMarcel Holtmann 	seq_printf(f, "0x%.2x%.2x%.2x\n", hdev->dev_class[2],
299babdbb3cSMarcel Holtmann 		   hdev->dev_class[1], hdev->dev_class[0]);
300babdbb3cSMarcel Holtmann 	hci_dev_unlock(hdev);
301babdbb3cSMarcel Holtmann 
302babdbb3cSMarcel Holtmann 	return 0;
303babdbb3cSMarcel Holtmann }
304babdbb3cSMarcel Holtmann 
305babdbb3cSMarcel Holtmann static int dev_class_open(struct inode *inode, struct file *file)
306babdbb3cSMarcel Holtmann {
307babdbb3cSMarcel Holtmann 	return single_open(file, dev_class_show, inode->i_private);
308babdbb3cSMarcel Holtmann }
309babdbb3cSMarcel Holtmann 
310babdbb3cSMarcel Holtmann static const struct file_operations dev_class_fops = {
311babdbb3cSMarcel Holtmann 	.open		= dev_class_open,
312babdbb3cSMarcel Holtmann 	.read		= seq_read,
313babdbb3cSMarcel Holtmann 	.llseek		= seq_lseek,
314babdbb3cSMarcel Holtmann 	.release	= single_release,
315babdbb3cSMarcel Holtmann };
316babdbb3cSMarcel Holtmann 
317041000b9SMarcel Holtmann static int voice_setting_get(void *data, u64 *val)
318041000b9SMarcel Holtmann {
319041000b9SMarcel Holtmann 	struct hci_dev *hdev = data;
320041000b9SMarcel Holtmann 
321041000b9SMarcel Holtmann 	hci_dev_lock(hdev);
322041000b9SMarcel Holtmann 	*val = hdev->voice_setting;
323041000b9SMarcel Holtmann 	hci_dev_unlock(hdev);
324041000b9SMarcel Holtmann 
325041000b9SMarcel Holtmann 	return 0;
326041000b9SMarcel Holtmann }
327041000b9SMarcel Holtmann 
328041000b9SMarcel Holtmann DEFINE_SIMPLE_ATTRIBUTE(voice_setting_fops, voice_setting_get,
329041000b9SMarcel Holtmann 			NULL, "0x%4.4llx\n");
330041000b9SMarcel Holtmann 
331ebd1e33bSMarcel Holtmann static int auto_accept_delay_set(void *data, u64 val)
332ebd1e33bSMarcel Holtmann {
333ebd1e33bSMarcel Holtmann 	struct hci_dev *hdev = data;
334ebd1e33bSMarcel Holtmann 
335ebd1e33bSMarcel Holtmann 	hci_dev_lock(hdev);
336ebd1e33bSMarcel Holtmann 	hdev->auto_accept_delay = val;
337ebd1e33bSMarcel Holtmann 	hci_dev_unlock(hdev);
338ebd1e33bSMarcel Holtmann 
339ebd1e33bSMarcel Holtmann 	return 0;
340ebd1e33bSMarcel Holtmann }
341ebd1e33bSMarcel Holtmann 
342ebd1e33bSMarcel Holtmann static int auto_accept_delay_get(void *data, u64 *val)
343ebd1e33bSMarcel Holtmann {
344ebd1e33bSMarcel Holtmann 	struct hci_dev *hdev = data;
345ebd1e33bSMarcel Holtmann 
346ebd1e33bSMarcel Holtmann 	hci_dev_lock(hdev);
347ebd1e33bSMarcel Holtmann 	*val = hdev->auto_accept_delay;
348ebd1e33bSMarcel Holtmann 	hci_dev_unlock(hdev);
349ebd1e33bSMarcel Holtmann 
350ebd1e33bSMarcel Holtmann 	return 0;
351ebd1e33bSMarcel Holtmann }
352ebd1e33bSMarcel Holtmann 
353ebd1e33bSMarcel Holtmann DEFINE_SIMPLE_ATTRIBUTE(auto_accept_delay_fops, auto_accept_delay_get,
354ebd1e33bSMarcel Holtmann 			auto_accept_delay_set, "%llu\n");
355ebd1e33bSMarcel Holtmann 
3565afeac14SMarcel Holtmann static ssize_t force_sc_support_read(struct file *file, char __user *user_buf,
3575afeac14SMarcel Holtmann 				     size_t count, loff_t *ppos)
3585afeac14SMarcel Holtmann {
3595afeac14SMarcel Holtmann 	struct hci_dev *hdev = file->private_data;
3605afeac14SMarcel Holtmann 	char buf[3];
3615afeac14SMarcel Holtmann 
362111902f7SMarcel Holtmann 	buf[0] = test_bit(HCI_FORCE_SC, &hdev->dbg_flags) ? 'Y': 'N';
3635afeac14SMarcel Holtmann 	buf[1] = '\n';
3645afeac14SMarcel Holtmann 	buf[2] = '\0';
3655afeac14SMarcel Holtmann 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
3665afeac14SMarcel Holtmann }
3675afeac14SMarcel Holtmann 
3685afeac14SMarcel Holtmann static ssize_t force_sc_support_write(struct file *file,
3695afeac14SMarcel Holtmann 				      const char __user *user_buf,
3705afeac14SMarcel Holtmann 				      size_t count, loff_t *ppos)
3715afeac14SMarcel Holtmann {
3725afeac14SMarcel Holtmann 	struct hci_dev *hdev = file->private_data;
3735afeac14SMarcel Holtmann 	char buf[32];
3745afeac14SMarcel Holtmann 	size_t buf_size = min(count, (sizeof(buf)-1));
3755afeac14SMarcel Holtmann 	bool enable;
3765afeac14SMarcel Holtmann 
3775afeac14SMarcel Holtmann 	if (test_bit(HCI_UP, &hdev->flags))
3785afeac14SMarcel Holtmann 		return -EBUSY;
3795afeac14SMarcel Holtmann 
3805afeac14SMarcel Holtmann 	if (copy_from_user(buf, user_buf, buf_size))
3815afeac14SMarcel Holtmann 		return -EFAULT;
3825afeac14SMarcel Holtmann 
3835afeac14SMarcel Holtmann 	buf[buf_size] = '\0';
3845afeac14SMarcel Holtmann 	if (strtobool(buf, &enable))
3855afeac14SMarcel Holtmann 		return -EINVAL;
3865afeac14SMarcel Holtmann 
387111902f7SMarcel Holtmann 	if (enable == test_bit(HCI_FORCE_SC, &hdev->dbg_flags))
3885afeac14SMarcel Holtmann 		return -EALREADY;
3895afeac14SMarcel Holtmann 
390111902f7SMarcel Holtmann 	change_bit(HCI_FORCE_SC, &hdev->dbg_flags);
3915afeac14SMarcel Holtmann 
3925afeac14SMarcel Holtmann 	return count;
3935afeac14SMarcel Holtmann }
3945afeac14SMarcel Holtmann 
3955afeac14SMarcel Holtmann static const struct file_operations force_sc_support_fops = {
3965afeac14SMarcel Holtmann 	.open		= simple_open,
3975afeac14SMarcel Holtmann 	.read		= force_sc_support_read,
3985afeac14SMarcel Holtmann 	.write		= force_sc_support_write,
3995afeac14SMarcel Holtmann 	.llseek		= default_llseek,
4005afeac14SMarcel Holtmann };
4015afeac14SMarcel Holtmann 
402134c2a89SMarcel Holtmann static ssize_t sc_only_mode_read(struct file *file, char __user *user_buf,
403134c2a89SMarcel Holtmann 				 size_t count, loff_t *ppos)
404134c2a89SMarcel Holtmann {
405134c2a89SMarcel Holtmann 	struct hci_dev *hdev = file->private_data;
406134c2a89SMarcel Holtmann 	char buf[3];
407134c2a89SMarcel Holtmann 
408134c2a89SMarcel Holtmann 	buf[0] = test_bit(HCI_SC_ONLY, &hdev->dev_flags) ? 'Y': 'N';
409134c2a89SMarcel Holtmann 	buf[1] = '\n';
410134c2a89SMarcel Holtmann 	buf[2] = '\0';
411134c2a89SMarcel Holtmann 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
412134c2a89SMarcel Holtmann }
413134c2a89SMarcel Holtmann 
414134c2a89SMarcel Holtmann static const struct file_operations sc_only_mode_fops = {
415134c2a89SMarcel Holtmann 	.open		= simple_open,
416134c2a89SMarcel Holtmann 	.read		= sc_only_mode_read,
417134c2a89SMarcel Holtmann 	.llseek		= default_llseek,
418134c2a89SMarcel Holtmann };
419134c2a89SMarcel Holtmann 
4202bfa3531SMarcel Holtmann static int idle_timeout_set(void *data, u64 val)
4212bfa3531SMarcel Holtmann {
4222bfa3531SMarcel Holtmann 	struct hci_dev *hdev = data;
4232bfa3531SMarcel Holtmann 
4242bfa3531SMarcel Holtmann 	if (val != 0 && (val < 500 || val > 3600000))
4252bfa3531SMarcel Holtmann 		return -EINVAL;
4262bfa3531SMarcel Holtmann 
4272bfa3531SMarcel Holtmann 	hci_dev_lock(hdev);
4282bfa3531SMarcel Holtmann 	hdev->idle_timeout = val;
4292bfa3531SMarcel Holtmann 	hci_dev_unlock(hdev);
4302bfa3531SMarcel Holtmann 
4312bfa3531SMarcel Holtmann 	return 0;
4322bfa3531SMarcel Holtmann }
4332bfa3531SMarcel Holtmann 
4342bfa3531SMarcel Holtmann static int idle_timeout_get(void *data, u64 *val)
4352bfa3531SMarcel Holtmann {
4362bfa3531SMarcel Holtmann 	struct hci_dev *hdev = data;
4372bfa3531SMarcel Holtmann 
4382bfa3531SMarcel Holtmann 	hci_dev_lock(hdev);
4392bfa3531SMarcel Holtmann 	*val = hdev->idle_timeout;
4402bfa3531SMarcel Holtmann 	hci_dev_unlock(hdev);
4412bfa3531SMarcel Holtmann 
4422bfa3531SMarcel Holtmann 	return 0;
4432bfa3531SMarcel Holtmann }
4442bfa3531SMarcel Holtmann 
4452bfa3531SMarcel Holtmann DEFINE_SIMPLE_ATTRIBUTE(idle_timeout_fops, idle_timeout_get,
4462bfa3531SMarcel Holtmann 			idle_timeout_set, "%llu\n");
4472bfa3531SMarcel Holtmann 
448c982b2eaSJohan Hedberg static int rpa_timeout_set(void *data, u64 val)
449c982b2eaSJohan Hedberg {
450c982b2eaSJohan Hedberg 	struct hci_dev *hdev = data;
451c982b2eaSJohan Hedberg 
452c982b2eaSJohan Hedberg 	/* Require the RPA timeout to be at least 30 seconds and at most
453c982b2eaSJohan Hedberg 	 * 24 hours.
454c982b2eaSJohan Hedberg 	 */
455c982b2eaSJohan Hedberg 	if (val < 30 || val > (60 * 60 * 24))
456c982b2eaSJohan Hedberg 		return -EINVAL;
457c982b2eaSJohan Hedberg 
458c982b2eaSJohan Hedberg 	hci_dev_lock(hdev);
459c982b2eaSJohan Hedberg 	hdev->rpa_timeout = val;
460c982b2eaSJohan Hedberg 	hci_dev_unlock(hdev);
461c982b2eaSJohan Hedberg 
462c982b2eaSJohan Hedberg 	return 0;
463c982b2eaSJohan Hedberg }
464c982b2eaSJohan Hedberg 
465c982b2eaSJohan Hedberg static int rpa_timeout_get(void *data, u64 *val)
466c982b2eaSJohan Hedberg {
467c982b2eaSJohan Hedberg 	struct hci_dev *hdev = data;
468c982b2eaSJohan Hedberg 
469c982b2eaSJohan Hedberg 	hci_dev_lock(hdev);
470c982b2eaSJohan Hedberg 	*val = hdev->rpa_timeout;
471c982b2eaSJohan Hedberg 	hci_dev_unlock(hdev);
472c982b2eaSJohan Hedberg 
473c982b2eaSJohan Hedberg 	return 0;
474c982b2eaSJohan Hedberg }
475c982b2eaSJohan Hedberg 
476c982b2eaSJohan Hedberg DEFINE_SIMPLE_ATTRIBUTE(rpa_timeout_fops, rpa_timeout_get,
477c982b2eaSJohan Hedberg 			rpa_timeout_set, "%llu\n");
478c982b2eaSJohan Hedberg 
4792bfa3531SMarcel Holtmann static int sniff_min_interval_set(void *data, u64 val)
4802bfa3531SMarcel Holtmann {
4812bfa3531SMarcel Holtmann 	struct hci_dev *hdev = data;
4822bfa3531SMarcel Holtmann 
4832bfa3531SMarcel Holtmann 	if (val == 0 || val % 2 || val > hdev->sniff_max_interval)
4842bfa3531SMarcel Holtmann 		return -EINVAL;
4852bfa3531SMarcel Holtmann 
4862bfa3531SMarcel Holtmann 	hci_dev_lock(hdev);
4872bfa3531SMarcel Holtmann 	hdev->sniff_min_interval = val;
4882bfa3531SMarcel Holtmann 	hci_dev_unlock(hdev);
4892bfa3531SMarcel Holtmann 
4902bfa3531SMarcel Holtmann 	return 0;
4912bfa3531SMarcel Holtmann }
4922bfa3531SMarcel Holtmann 
4932bfa3531SMarcel Holtmann static int sniff_min_interval_get(void *data, u64 *val)
4942bfa3531SMarcel Holtmann {
4952bfa3531SMarcel Holtmann 	struct hci_dev *hdev = data;
4962bfa3531SMarcel Holtmann 
4972bfa3531SMarcel Holtmann 	hci_dev_lock(hdev);
4982bfa3531SMarcel Holtmann 	*val = hdev->sniff_min_interval;
4992bfa3531SMarcel Holtmann 	hci_dev_unlock(hdev);
5002bfa3531SMarcel Holtmann 
5012bfa3531SMarcel Holtmann 	return 0;
5022bfa3531SMarcel Holtmann }
5032bfa3531SMarcel Holtmann 
5042bfa3531SMarcel Holtmann DEFINE_SIMPLE_ATTRIBUTE(sniff_min_interval_fops, sniff_min_interval_get,
5052bfa3531SMarcel Holtmann 			sniff_min_interval_set, "%llu\n");
5062bfa3531SMarcel Holtmann 
5072bfa3531SMarcel Holtmann static int sniff_max_interval_set(void *data, u64 val)
5082bfa3531SMarcel Holtmann {
5092bfa3531SMarcel Holtmann 	struct hci_dev *hdev = data;
5102bfa3531SMarcel Holtmann 
5112bfa3531SMarcel Holtmann 	if (val == 0 || val % 2 || val < hdev->sniff_min_interval)
5122bfa3531SMarcel Holtmann 		return -EINVAL;
5132bfa3531SMarcel Holtmann 
5142bfa3531SMarcel Holtmann 	hci_dev_lock(hdev);
5152bfa3531SMarcel Holtmann 	hdev->sniff_max_interval = val;
5162bfa3531SMarcel Holtmann 	hci_dev_unlock(hdev);
5172bfa3531SMarcel Holtmann 
5182bfa3531SMarcel Holtmann 	return 0;
5192bfa3531SMarcel Holtmann }
5202bfa3531SMarcel Holtmann 
5212bfa3531SMarcel Holtmann static int sniff_max_interval_get(void *data, u64 *val)
5222bfa3531SMarcel Holtmann {
5232bfa3531SMarcel Holtmann 	struct hci_dev *hdev = data;
5242bfa3531SMarcel Holtmann 
5252bfa3531SMarcel Holtmann 	hci_dev_lock(hdev);
5262bfa3531SMarcel Holtmann 	*val = hdev->sniff_max_interval;
5272bfa3531SMarcel Holtmann 	hci_dev_unlock(hdev);
5282bfa3531SMarcel Holtmann 
5292bfa3531SMarcel Holtmann 	return 0;
5302bfa3531SMarcel Holtmann }
5312bfa3531SMarcel Holtmann 
5322bfa3531SMarcel Holtmann DEFINE_SIMPLE_ATTRIBUTE(sniff_max_interval_fops, sniff_max_interval_get,
5332bfa3531SMarcel Holtmann 			sniff_max_interval_set, "%llu\n");
5342bfa3531SMarcel Holtmann 
53531ad1691SAndrzej Kaczmarek static int conn_info_min_age_set(void *data, u64 val)
53631ad1691SAndrzej Kaczmarek {
53731ad1691SAndrzej Kaczmarek 	struct hci_dev *hdev = data;
53831ad1691SAndrzej Kaczmarek 
53931ad1691SAndrzej Kaczmarek 	if (val == 0 || val > hdev->conn_info_max_age)
54031ad1691SAndrzej Kaczmarek 		return -EINVAL;
54131ad1691SAndrzej Kaczmarek 
54231ad1691SAndrzej Kaczmarek 	hci_dev_lock(hdev);
54331ad1691SAndrzej Kaczmarek 	hdev->conn_info_min_age = val;
54431ad1691SAndrzej Kaczmarek 	hci_dev_unlock(hdev);
54531ad1691SAndrzej Kaczmarek 
54631ad1691SAndrzej Kaczmarek 	return 0;
54731ad1691SAndrzej Kaczmarek }
54831ad1691SAndrzej Kaczmarek 
54931ad1691SAndrzej Kaczmarek static int conn_info_min_age_get(void *data, u64 *val)
55031ad1691SAndrzej Kaczmarek {
55131ad1691SAndrzej Kaczmarek 	struct hci_dev *hdev = data;
55231ad1691SAndrzej Kaczmarek 
55331ad1691SAndrzej Kaczmarek 	hci_dev_lock(hdev);
55431ad1691SAndrzej Kaczmarek 	*val = hdev->conn_info_min_age;
55531ad1691SAndrzej Kaczmarek 	hci_dev_unlock(hdev);
55631ad1691SAndrzej Kaczmarek 
55731ad1691SAndrzej Kaczmarek 	return 0;
55831ad1691SAndrzej Kaczmarek }
55931ad1691SAndrzej Kaczmarek 
56031ad1691SAndrzej Kaczmarek DEFINE_SIMPLE_ATTRIBUTE(conn_info_min_age_fops, conn_info_min_age_get,
56131ad1691SAndrzej Kaczmarek 			conn_info_min_age_set, "%llu\n");
56231ad1691SAndrzej Kaczmarek 
56331ad1691SAndrzej Kaczmarek static int conn_info_max_age_set(void *data, u64 val)
56431ad1691SAndrzej Kaczmarek {
56531ad1691SAndrzej Kaczmarek 	struct hci_dev *hdev = data;
56631ad1691SAndrzej Kaczmarek 
56731ad1691SAndrzej Kaczmarek 	if (val == 0 || val < hdev->conn_info_min_age)
56831ad1691SAndrzej Kaczmarek 		return -EINVAL;
56931ad1691SAndrzej Kaczmarek 
57031ad1691SAndrzej Kaczmarek 	hci_dev_lock(hdev);
57131ad1691SAndrzej Kaczmarek 	hdev->conn_info_max_age = val;
57231ad1691SAndrzej Kaczmarek 	hci_dev_unlock(hdev);
57331ad1691SAndrzej Kaczmarek 
57431ad1691SAndrzej Kaczmarek 	return 0;
57531ad1691SAndrzej Kaczmarek }
57631ad1691SAndrzej Kaczmarek 
57731ad1691SAndrzej Kaczmarek static int conn_info_max_age_get(void *data, u64 *val)
57831ad1691SAndrzej Kaczmarek {
57931ad1691SAndrzej Kaczmarek 	struct hci_dev *hdev = data;
58031ad1691SAndrzej Kaczmarek 
58131ad1691SAndrzej Kaczmarek 	hci_dev_lock(hdev);
58231ad1691SAndrzej Kaczmarek 	*val = hdev->conn_info_max_age;
58331ad1691SAndrzej Kaczmarek 	hci_dev_unlock(hdev);
58431ad1691SAndrzej Kaczmarek 
58531ad1691SAndrzej Kaczmarek 	return 0;
58631ad1691SAndrzej Kaczmarek }
58731ad1691SAndrzej Kaczmarek 
58831ad1691SAndrzej Kaczmarek DEFINE_SIMPLE_ATTRIBUTE(conn_info_max_age_fops, conn_info_max_age_get,
58931ad1691SAndrzej Kaczmarek 			conn_info_max_age_set, "%llu\n");
59031ad1691SAndrzej Kaczmarek 
591ac345813SMarcel Holtmann static int identity_show(struct seq_file *f, void *p)
592ac345813SMarcel Holtmann {
593ac345813SMarcel Holtmann 	struct hci_dev *hdev = f->private;
594a1f4c318SJohan Hedberg 	bdaddr_t addr;
595ac345813SMarcel Holtmann 	u8 addr_type;
596ac345813SMarcel Holtmann 
597ac345813SMarcel Holtmann 	hci_dev_lock(hdev);
598ac345813SMarcel Holtmann 
599a1f4c318SJohan Hedberg 	hci_copy_identity_address(hdev, &addr, &addr_type);
600ac345813SMarcel Holtmann 
601a1f4c318SJohan Hedberg 	seq_printf(f, "%pMR (type %u) %*phN %pMR\n", &addr, addr_type,
602473deef2SMarcel Holtmann 		   16, hdev->irk, &hdev->rpa);
603ac345813SMarcel Holtmann 
604ac345813SMarcel Holtmann 	hci_dev_unlock(hdev);
605ac345813SMarcel Holtmann 
606ac345813SMarcel Holtmann 	return 0;
607ac345813SMarcel Holtmann }
608ac345813SMarcel Holtmann 
609ac345813SMarcel Holtmann static int identity_open(struct inode *inode, struct file *file)
610ac345813SMarcel Holtmann {
611ac345813SMarcel Holtmann 	return single_open(file, identity_show, inode->i_private);
612ac345813SMarcel Holtmann }
613ac345813SMarcel Holtmann 
614ac345813SMarcel Holtmann static const struct file_operations identity_fops = {
615ac345813SMarcel Holtmann 	.open		= identity_open,
616ac345813SMarcel Holtmann 	.read		= seq_read,
617ac345813SMarcel Holtmann 	.llseek		= seq_lseek,
618ac345813SMarcel Holtmann 	.release	= single_release,
619ac345813SMarcel Holtmann };
620ac345813SMarcel Holtmann 
6217a4cd51dSMarcel Holtmann static int random_address_show(struct seq_file *f, void *p)
6227a4cd51dSMarcel Holtmann {
6237a4cd51dSMarcel Holtmann 	struct hci_dev *hdev = f->private;
6247a4cd51dSMarcel Holtmann 
6257a4cd51dSMarcel Holtmann 	hci_dev_lock(hdev);
6267a4cd51dSMarcel Holtmann 	seq_printf(f, "%pMR\n", &hdev->random_addr);
6277a4cd51dSMarcel Holtmann 	hci_dev_unlock(hdev);
6287a4cd51dSMarcel Holtmann 
6297a4cd51dSMarcel Holtmann 	return 0;
6307a4cd51dSMarcel Holtmann }
6317a4cd51dSMarcel Holtmann 
6327a4cd51dSMarcel Holtmann static int random_address_open(struct inode *inode, struct file *file)
6337a4cd51dSMarcel Holtmann {
6347a4cd51dSMarcel Holtmann 	return single_open(file, random_address_show, inode->i_private);
6357a4cd51dSMarcel Holtmann }
6367a4cd51dSMarcel Holtmann 
6377a4cd51dSMarcel Holtmann static const struct file_operations random_address_fops = {
6387a4cd51dSMarcel Holtmann 	.open		= random_address_open,
6397a4cd51dSMarcel Holtmann 	.read		= seq_read,
6407a4cd51dSMarcel Holtmann 	.llseek		= seq_lseek,
6417a4cd51dSMarcel Holtmann 	.release	= single_release,
6427a4cd51dSMarcel Holtmann };
6437a4cd51dSMarcel Holtmann 
644e7b8fc92SMarcel Holtmann static int static_address_show(struct seq_file *f, void *p)
645e7b8fc92SMarcel Holtmann {
646e7b8fc92SMarcel Holtmann 	struct hci_dev *hdev = f->private;
647e7b8fc92SMarcel Holtmann 
648e7b8fc92SMarcel Holtmann 	hci_dev_lock(hdev);
649e7b8fc92SMarcel Holtmann 	seq_printf(f, "%pMR\n", &hdev->static_addr);
650e7b8fc92SMarcel Holtmann 	hci_dev_unlock(hdev);
651e7b8fc92SMarcel Holtmann 
652e7b8fc92SMarcel Holtmann 	return 0;
653e7b8fc92SMarcel Holtmann }
654e7b8fc92SMarcel Holtmann 
655e7b8fc92SMarcel Holtmann static int static_address_open(struct inode *inode, struct file *file)
656e7b8fc92SMarcel Holtmann {
657e7b8fc92SMarcel Holtmann 	return single_open(file, static_address_show, inode->i_private);
658e7b8fc92SMarcel Holtmann }
659e7b8fc92SMarcel Holtmann 
660e7b8fc92SMarcel Holtmann static const struct file_operations static_address_fops = {
661e7b8fc92SMarcel Holtmann 	.open		= static_address_open,
662e7b8fc92SMarcel Holtmann 	.read		= seq_read,
663e7b8fc92SMarcel Holtmann 	.llseek		= seq_lseek,
664e7b8fc92SMarcel Holtmann 	.release	= single_release,
665e7b8fc92SMarcel Holtmann };
666e7b8fc92SMarcel Holtmann 
667b32bba6cSMarcel Holtmann static ssize_t force_static_address_read(struct file *file,
668b32bba6cSMarcel Holtmann 					 char __user *user_buf,
669b32bba6cSMarcel Holtmann 					 size_t count, loff_t *ppos)
67092202185SMarcel Holtmann {
671b32bba6cSMarcel Holtmann 	struct hci_dev *hdev = file->private_data;
672b32bba6cSMarcel Holtmann 	char buf[3];
67392202185SMarcel Holtmann 
674111902f7SMarcel Holtmann 	buf[0] = test_bit(HCI_FORCE_STATIC_ADDR, &hdev->dbg_flags) ? 'Y': 'N';
675b32bba6cSMarcel Holtmann 	buf[1] = '\n';
676b32bba6cSMarcel Holtmann 	buf[2] = '\0';
677b32bba6cSMarcel Holtmann 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
678b32bba6cSMarcel Holtmann }
679b32bba6cSMarcel Holtmann 
680b32bba6cSMarcel Holtmann static ssize_t force_static_address_write(struct file *file,
681b32bba6cSMarcel Holtmann 					  const char __user *user_buf,
682b32bba6cSMarcel Holtmann 					  size_t count, loff_t *ppos)
683b32bba6cSMarcel Holtmann {
684b32bba6cSMarcel Holtmann 	struct hci_dev *hdev = file->private_data;
685b32bba6cSMarcel Holtmann 	char buf[32];
686b32bba6cSMarcel Holtmann 	size_t buf_size = min(count, (sizeof(buf)-1));
687b32bba6cSMarcel Holtmann 	bool enable;
688b32bba6cSMarcel Holtmann 
689b32bba6cSMarcel Holtmann 	if (test_bit(HCI_UP, &hdev->flags))
690b32bba6cSMarcel Holtmann 		return -EBUSY;
691b32bba6cSMarcel Holtmann 
692b32bba6cSMarcel Holtmann 	if (copy_from_user(buf, user_buf, buf_size))
693b32bba6cSMarcel Holtmann 		return -EFAULT;
694b32bba6cSMarcel Holtmann 
695b32bba6cSMarcel Holtmann 	buf[buf_size] = '\0';
696b32bba6cSMarcel Holtmann 	if (strtobool(buf, &enable))
69792202185SMarcel Holtmann 		return -EINVAL;
69892202185SMarcel Holtmann 
699111902f7SMarcel Holtmann 	if (enable == test_bit(HCI_FORCE_STATIC_ADDR, &hdev->dbg_flags))
700b32bba6cSMarcel Holtmann 		return -EALREADY;
70192202185SMarcel Holtmann 
702111902f7SMarcel Holtmann 	change_bit(HCI_FORCE_STATIC_ADDR, &hdev->dbg_flags);
703b32bba6cSMarcel Holtmann 
704b32bba6cSMarcel Holtmann 	return count;
70592202185SMarcel Holtmann }
70692202185SMarcel Holtmann 
707b32bba6cSMarcel Holtmann static const struct file_operations force_static_address_fops = {
708b32bba6cSMarcel Holtmann 	.open		= simple_open,
709b32bba6cSMarcel Holtmann 	.read		= force_static_address_read,
710b32bba6cSMarcel Holtmann 	.write		= force_static_address_write,
711b32bba6cSMarcel Holtmann 	.llseek		= default_llseek,
712b32bba6cSMarcel Holtmann };
71392202185SMarcel Holtmann 
714d2ab0ac1SMarcel Holtmann static int white_list_show(struct seq_file *f, void *ptr)
715d2ab0ac1SMarcel Holtmann {
716d2ab0ac1SMarcel Holtmann 	struct hci_dev *hdev = f->private;
717d2ab0ac1SMarcel Holtmann 	struct bdaddr_list *b;
718d2ab0ac1SMarcel Holtmann 
719d2ab0ac1SMarcel Holtmann 	hci_dev_lock(hdev);
720d2ab0ac1SMarcel Holtmann 	list_for_each_entry(b, &hdev->le_white_list, list)
721d2ab0ac1SMarcel Holtmann 		seq_printf(f, "%pMR (type %u)\n", &b->bdaddr, b->bdaddr_type);
722d2ab0ac1SMarcel Holtmann 	hci_dev_unlock(hdev);
723d2ab0ac1SMarcel Holtmann 
724d2ab0ac1SMarcel Holtmann 	return 0;
725d2ab0ac1SMarcel Holtmann }
726d2ab0ac1SMarcel Holtmann 
727d2ab0ac1SMarcel Holtmann static int white_list_open(struct inode *inode, struct file *file)
728d2ab0ac1SMarcel Holtmann {
729d2ab0ac1SMarcel Holtmann 	return single_open(file, white_list_show, inode->i_private);
730d2ab0ac1SMarcel Holtmann }
731d2ab0ac1SMarcel Holtmann 
732d2ab0ac1SMarcel Holtmann static const struct file_operations white_list_fops = {
733d2ab0ac1SMarcel Holtmann 	.open		= white_list_open,
734d2ab0ac1SMarcel Holtmann 	.read		= seq_read,
735d2ab0ac1SMarcel Holtmann 	.llseek		= seq_lseek,
736d2ab0ac1SMarcel Holtmann 	.release	= single_release,
737d2ab0ac1SMarcel Holtmann };
738d2ab0ac1SMarcel Holtmann 
7393698d704SMarcel Holtmann static int identity_resolving_keys_show(struct seq_file *f, void *ptr)
7403698d704SMarcel Holtmann {
7413698d704SMarcel Holtmann 	struct hci_dev *hdev = f->private;
7423698d704SMarcel Holtmann 	struct list_head *p, *n;
7433698d704SMarcel Holtmann 
7443698d704SMarcel Holtmann 	hci_dev_lock(hdev);
7453698d704SMarcel Holtmann 	list_for_each_safe(p, n, &hdev->identity_resolving_keys) {
7463698d704SMarcel Holtmann 		struct smp_irk *irk = list_entry(p, struct smp_irk, list);
7473698d704SMarcel Holtmann 		seq_printf(f, "%pMR (type %u) %*phN %pMR\n",
7483698d704SMarcel Holtmann 			   &irk->bdaddr, irk->addr_type,
7493698d704SMarcel Holtmann 			   16, irk->val, &irk->rpa);
7503698d704SMarcel Holtmann 	}
7513698d704SMarcel Holtmann 	hci_dev_unlock(hdev);
7523698d704SMarcel Holtmann 
7533698d704SMarcel Holtmann 	return 0;
7543698d704SMarcel Holtmann }
7553698d704SMarcel Holtmann 
7563698d704SMarcel Holtmann static int identity_resolving_keys_open(struct inode *inode, struct file *file)
7573698d704SMarcel Holtmann {
7583698d704SMarcel Holtmann 	return single_open(file, identity_resolving_keys_show,
7593698d704SMarcel Holtmann 			   inode->i_private);
7603698d704SMarcel Holtmann }
7613698d704SMarcel Holtmann 
7623698d704SMarcel Holtmann static const struct file_operations identity_resolving_keys_fops = {
7633698d704SMarcel Holtmann 	.open		= identity_resolving_keys_open,
7643698d704SMarcel Holtmann 	.read		= seq_read,
7653698d704SMarcel Holtmann 	.llseek		= seq_lseek,
7663698d704SMarcel Holtmann 	.release	= single_release,
7673698d704SMarcel Holtmann };
7683698d704SMarcel Holtmann 
7698f8625cdSMarcel Holtmann static int long_term_keys_show(struct seq_file *f, void *ptr)
7708f8625cdSMarcel Holtmann {
7718f8625cdSMarcel Holtmann 	struct hci_dev *hdev = f->private;
7728f8625cdSMarcel Holtmann 	struct list_head *p, *n;
7738f8625cdSMarcel Holtmann 
7748f8625cdSMarcel Holtmann 	hci_dev_lock(hdev);
775f813f1beSJohan Hedberg 	list_for_each_safe(p, n, &hdev->long_term_keys) {
7768f8625cdSMarcel Holtmann 		struct smp_ltk *ltk = list_entry(p, struct smp_ltk, list);
777fe39c7b2SMarcel Holtmann 		seq_printf(f, "%pMR (type %u) %u 0x%02x %u %.4x %.16llx %*phN\n",
7788f8625cdSMarcel Holtmann 			   &ltk->bdaddr, ltk->bdaddr_type, ltk->authenticated,
7798f8625cdSMarcel Holtmann 			   ltk->type, ltk->enc_size, __le16_to_cpu(ltk->ediv),
780fe39c7b2SMarcel Holtmann 			   __le64_to_cpu(ltk->rand), 16, ltk->val);
7818f8625cdSMarcel Holtmann 	}
7828f8625cdSMarcel Holtmann 	hci_dev_unlock(hdev);
7838f8625cdSMarcel Holtmann 
7848f8625cdSMarcel Holtmann 	return 0;
7858f8625cdSMarcel Holtmann }
7868f8625cdSMarcel Holtmann 
7878f8625cdSMarcel Holtmann static int long_term_keys_open(struct inode *inode, struct file *file)
7888f8625cdSMarcel Holtmann {
7898f8625cdSMarcel Holtmann 	return single_open(file, long_term_keys_show, inode->i_private);
7908f8625cdSMarcel Holtmann }
7918f8625cdSMarcel Holtmann 
7928f8625cdSMarcel Holtmann static const struct file_operations long_term_keys_fops = {
7938f8625cdSMarcel Holtmann 	.open		= long_term_keys_open,
7948f8625cdSMarcel Holtmann 	.read		= seq_read,
7958f8625cdSMarcel Holtmann 	.llseek		= seq_lseek,
7968f8625cdSMarcel Holtmann 	.release	= single_release,
7978f8625cdSMarcel Holtmann };
7988f8625cdSMarcel Holtmann 
7994e70c7e7SMarcel Holtmann static int conn_min_interval_set(void *data, u64 val)
8004e70c7e7SMarcel Holtmann {
8014e70c7e7SMarcel Holtmann 	struct hci_dev *hdev = data;
8024e70c7e7SMarcel Holtmann 
8034e70c7e7SMarcel Holtmann 	if (val < 0x0006 || val > 0x0c80 || val > hdev->le_conn_max_interval)
8044e70c7e7SMarcel Holtmann 		return -EINVAL;
8054e70c7e7SMarcel Holtmann 
8064e70c7e7SMarcel Holtmann 	hci_dev_lock(hdev);
8074e70c7e7SMarcel Holtmann 	hdev->le_conn_min_interval = val;
8084e70c7e7SMarcel Holtmann 	hci_dev_unlock(hdev);
8094e70c7e7SMarcel Holtmann 
8104e70c7e7SMarcel Holtmann 	return 0;
8114e70c7e7SMarcel Holtmann }
8124e70c7e7SMarcel Holtmann 
8134e70c7e7SMarcel Holtmann static int conn_min_interval_get(void *data, u64 *val)
8144e70c7e7SMarcel Holtmann {
8154e70c7e7SMarcel Holtmann 	struct hci_dev *hdev = data;
8164e70c7e7SMarcel Holtmann 
8174e70c7e7SMarcel Holtmann 	hci_dev_lock(hdev);
8184e70c7e7SMarcel Holtmann 	*val = hdev->le_conn_min_interval;
8194e70c7e7SMarcel Holtmann 	hci_dev_unlock(hdev);
8204e70c7e7SMarcel Holtmann 
8214e70c7e7SMarcel Holtmann 	return 0;
8224e70c7e7SMarcel Holtmann }
8234e70c7e7SMarcel Holtmann 
8244e70c7e7SMarcel Holtmann DEFINE_SIMPLE_ATTRIBUTE(conn_min_interval_fops, conn_min_interval_get,
8254e70c7e7SMarcel Holtmann 			conn_min_interval_set, "%llu\n");
8264e70c7e7SMarcel Holtmann 
8274e70c7e7SMarcel Holtmann static int conn_max_interval_set(void *data, u64 val)
8284e70c7e7SMarcel Holtmann {
8294e70c7e7SMarcel Holtmann 	struct hci_dev *hdev = data;
8304e70c7e7SMarcel Holtmann 
8314e70c7e7SMarcel Holtmann 	if (val < 0x0006 || val > 0x0c80 || val < hdev->le_conn_min_interval)
8324e70c7e7SMarcel Holtmann 		return -EINVAL;
8334e70c7e7SMarcel Holtmann 
8344e70c7e7SMarcel Holtmann 	hci_dev_lock(hdev);
8354e70c7e7SMarcel Holtmann 	hdev->le_conn_max_interval = val;
8364e70c7e7SMarcel Holtmann 	hci_dev_unlock(hdev);
8374e70c7e7SMarcel Holtmann 
8384e70c7e7SMarcel Holtmann 	return 0;
8394e70c7e7SMarcel Holtmann }
8404e70c7e7SMarcel Holtmann 
8414e70c7e7SMarcel Holtmann static int conn_max_interval_get(void *data, u64 *val)
8424e70c7e7SMarcel Holtmann {
8434e70c7e7SMarcel Holtmann 	struct hci_dev *hdev = data;
8444e70c7e7SMarcel Holtmann 
8454e70c7e7SMarcel Holtmann 	hci_dev_lock(hdev);
8464e70c7e7SMarcel Holtmann 	*val = hdev->le_conn_max_interval;
8474e70c7e7SMarcel Holtmann 	hci_dev_unlock(hdev);
8484e70c7e7SMarcel Holtmann 
8494e70c7e7SMarcel Holtmann 	return 0;
8504e70c7e7SMarcel Holtmann }
8514e70c7e7SMarcel Holtmann 
8524e70c7e7SMarcel Holtmann DEFINE_SIMPLE_ATTRIBUTE(conn_max_interval_fops, conn_max_interval_get,
8534e70c7e7SMarcel Holtmann 			conn_max_interval_set, "%llu\n");
8544e70c7e7SMarcel Holtmann 
855816a93d1SMarcel Holtmann static int conn_latency_set(void *data, u64 val)
856816a93d1SMarcel Holtmann {
857816a93d1SMarcel Holtmann 	struct hci_dev *hdev = data;
858816a93d1SMarcel Holtmann 
859816a93d1SMarcel Holtmann 	if (val > 0x01f3)
860816a93d1SMarcel Holtmann 		return -EINVAL;
861816a93d1SMarcel Holtmann 
862816a93d1SMarcel Holtmann 	hci_dev_lock(hdev);
863816a93d1SMarcel Holtmann 	hdev->le_conn_latency = val;
864816a93d1SMarcel Holtmann 	hci_dev_unlock(hdev);
865816a93d1SMarcel Holtmann 
866816a93d1SMarcel Holtmann 	return 0;
867816a93d1SMarcel Holtmann }
868816a93d1SMarcel Holtmann 
869816a93d1SMarcel Holtmann static int conn_latency_get(void *data, u64 *val)
870816a93d1SMarcel Holtmann {
871816a93d1SMarcel Holtmann 	struct hci_dev *hdev = data;
872816a93d1SMarcel Holtmann 
873816a93d1SMarcel Holtmann 	hci_dev_lock(hdev);
874816a93d1SMarcel Holtmann 	*val = hdev->le_conn_latency;
875816a93d1SMarcel Holtmann 	hci_dev_unlock(hdev);
876816a93d1SMarcel Holtmann 
877816a93d1SMarcel Holtmann 	return 0;
878816a93d1SMarcel Holtmann }
879816a93d1SMarcel Holtmann 
880816a93d1SMarcel Holtmann DEFINE_SIMPLE_ATTRIBUTE(conn_latency_fops, conn_latency_get,
881816a93d1SMarcel Holtmann 			conn_latency_set, "%llu\n");
882816a93d1SMarcel Holtmann 
883f1649577SMarcel Holtmann static int supervision_timeout_set(void *data, u64 val)
884f1649577SMarcel Holtmann {
885f1649577SMarcel Holtmann 	struct hci_dev *hdev = data;
886f1649577SMarcel Holtmann 
887f1649577SMarcel Holtmann 	if (val < 0x000a || val > 0x0c80)
888f1649577SMarcel Holtmann 		return -EINVAL;
889f1649577SMarcel Holtmann 
890f1649577SMarcel Holtmann 	hci_dev_lock(hdev);
891f1649577SMarcel Holtmann 	hdev->le_supv_timeout = val;
892f1649577SMarcel Holtmann 	hci_dev_unlock(hdev);
893f1649577SMarcel Holtmann 
894f1649577SMarcel Holtmann 	return 0;
895f1649577SMarcel Holtmann }
896f1649577SMarcel Holtmann 
897f1649577SMarcel Holtmann static int supervision_timeout_get(void *data, u64 *val)
898f1649577SMarcel Holtmann {
899f1649577SMarcel Holtmann 	struct hci_dev *hdev = data;
900f1649577SMarcel Holtmann 
901f1649577SMarcel Holtmann 	hci_dev_lock(hdev);
902f1649577SMarcel Holtmann 	*val = hdev->le_supv_timeout;
903f1649577SMarcel Holtmann 	hci_dev_unlock(hdev);
904f1649577SMarcel Holtmann 
905f1649577SMarcel Holtmann 	return 0;
906f1649577SMarcel Holtmann }
907f1649577SMarcel Holtmann 
908f1649577SMarcel Holtmann DEFINE_SIMPLE_ATTRIBUTE(supervision_timeout_fops, supervision_timeout_get,
909f1649577SMarcel Holtmann 			supervision_timeout_set, "%llu\n");
910f1649577SMarcel Holtmann 
9113f959d46SMarcel Holtmann static int adv_channel_map_set(void *data, u64 val)
9123f959d46SMarcel Holtmann {
9133f959d46SMarcel Holtmann 	struct hci_dev *hdev = data;
9143f959d46SMarcel Holtmann 
9153f959d46SMarcel Holtmann 	if (val < 0x01 || val > 0x07)
9163f959d46SMarcel Holtmann 		return -EINVAL;
9173f959d46SMarcel Holtmann 
9183f959d46SMarcel Holtmann 	hci_dev_lock(hdev);
9193f959d46SMarcel Holtmann 	hdev->le_adv_channel_map = val;
9203f959d46SMarcel Holtmann 	hci_dev_unlock(hdev);
9213f959d46SMarcel Holtmann 
9223f959d46SMarcel Holtmann 	return 0;
9233f959d46SMarcel Holtmann }
9243f959d46SMarcel Holtmann 
9253f959d46SMarcel Holtmann static int adv_channel_map_get(void *data, u64 *val)
9263f959d46SMarcel Holtmann {
9273f959d46SMarcel Holtmann 	struct hci_dev *hdev = data;
9283f959d46SMarcel Holtmann 
9293f959d46SMarcel Holtmann 	hci_dev_lock(hdev);
9303f959d46SMarcel Holtmann 	*val = hdev->le_adv_channel_map;
9313f959d46SMarcel Holtmann 	hci_dev_unlock(hdev);
9323f959d46SMarcel Holtmann 
9333f959d46SMarcel Holtmann 	return 0;
9343f959d46SMarcel Holtmann }
9353f959d46SMarcel Holtmann 
9363f959d46SMarcel Holtmann DEFINE_SIMPLE_ATTRIBUTE(adv_channel_map_fops, adv_channel_map_get,
9373f959d46SMarcel Holtmann 			adv_channel_map_set, "%llu\n");
9383f959d46SMarcel Holtmann 
9390b3c7d37SMarcel Holtmann static int device_list_show(struct seq_file *f, void *ptr)
9407d474e06SAndre Guedes {
9410b3c7d37SMarcel Holtmann 	struct hci_dev *hdev = f->private;
9427d474e06SAndre Guedes 	struct hci_conn_params *p;
9437d474e06SAndre Guedes 
9447d474e06SAndre Guedes 	hci_dev_lock(hdev);
9457d474e06SAndre Guedes 	list_for_each_entry(p, &hdev->le_conn_params, list) {
9460b3c7d37SMarcel Holtmann 		seq_printf(f, "%pMR %u %u\n", &p->addr, p->addr_type,
9477d474e06SAndre Guedes 			   p->auto_connect);
9487d474e06SAndre Guedes 	}
9497d474e06SAndre Guedes 	hci_dev_unlock(hdev);
9507d474e06SAndre Guedes 
9517d474e06SAndre Guedes 	return 0;
9527d474e06SAndre Guedes }
9537d474e06SAndre Guedes 
9540b3c7d37SMarcel Holtmann static int device_list_open(struct inode *inode, struct file *file)
9557d474e06SAndre Guedes {
9560b3c7d37SMarcel Holtmann 	return single_open(file, device_list_show, inode->i_private);
9577d474e06SAndre Guedes }
9587d474e06SAndre Guedes 
9590b3c7d37SMarcel Holtmann static const struct file_operations device_list_fops = {
9600b3c7d37SMarcel Holtmann 	.open		= device_list_open,
9617d474e06SAndre Guedes 	.read		= seq_read,
9627d474e06SAndre Guedes 	.llseek		= seq_lseek,
9637d474e06SAndre Guedes 	.release	= single_release,
9647d474e06SAndre Guedes };
9657d474e06SAndre Guedes 
9661da177e4SLinus Torvalds /* ---- HCI requests ---- */
9671da177e4SLinus Torvalds 
96842c6b129SJohan Hedberg static void hci_req_sync_complete(struct hci_dev *hdev, u8 result)
9691da177e4SLinus Torvalds {
97042c6b129SJohan Hedberg 	BT_DBG("%s result 0x%2.2x", hdev->name, result);
97175fb0e32SJohan Hedberg 
9721da177e4SLinus Torvalds 	if (hdev->req_status == HCI_REQ_PEND) {
9731da177e4SLinus Torvalds 		hdev->req_result = result;
9741da177e4SLinus Torvalds 		hdev->req_status = HCI_REQ_DONE;
9751da177e4SLinus Torvalds 		wake_up_interruptible(&hdev->req_wait_q);
9761da177e4SLinus Torvalds 	}
9771da177e4SLinus Torvalds }
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds static void hci_req_cancel(struct hci_dev *hdev, int err)
9801da177e4SLinus Torvalds {
9811da177e4SLinus Torvalds 	BT_DBG("%s err 0x%2.2x", hdev->name, err);
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds 	if (hdev->req_status == HCI_REQ_PEND) {
9841da177e4SLinus Torvalds 		hdev->req_result = err;
9851da177e4SLinus Torvalds 		hdev->req_status = HCI_REQ_CANCELED;
9861da177e4SLinus Torvalds 		wake_up_interruptible(&hdev->req_wait_q);
9871da177e4SLinus Torvalds 	}
9881da177e4SLinus Torvalds }
9891da177e4SLinus Torvalds 
99077a63e0aSFengguang Wu static struct sk_buff *hci_get_cmd_complete(struct hci_dev *hdev, u16 opcode,
99177a63e0aSFengguang Wu 					    u8 event)
99275e84b7cSJohan Hedberg {
99375e84b7cSJohan Hedberg 	struct hci_ev_cmd_complete *ev;
99475e84b7cSJohan Hedberg 	struct hci_event_hdr *hdr;
99575e84b7cSJohan Hedberg 	struct sk_buff *skb;
99675e84b7cSJohan Hedberg 
99775e84b7cSJohan Hedberg 	hci_dev_lock(hdev);
99875e84b7cSJohan Hedberg 
99975e84b7cSJohan Hedberg 	skb = hdev->recv_evt;
100075e84b7cSJohan Hedberg 	hdev->recv_evt = NULL;
100175e84b7cSJohan Hedberg 
100275e84b7cSJohan Hedberg 	hci_dev_unlock(hdev);
100375e84b7cSJohan Hedberg 
100475e84b7cSJohan Hedberg 	if (!skb)
100575e84b7cSJohan Hedberg 		return ERR_PTR(-ENODATA);
100675e84b7cSJohan Hedberg 
100775e84b7cSJohan Hedberg 	if (skb->len < sizeof(*hdr)) {
100875e84b7cSJohan Hedberg 		BT_ERR("Too short HCI event");
100975e84b7cSJohan Hedberg 		goto failed;
101075e84b7cSJohan Hedberg 	}
101175e84b7cSJohan Hedberg 
101275e84b7cSJohan Hedberg 	hdr = (void *) skb->data;
101375e84b7cSJohan Hedberg 	skb_pull(skb, HCI_EVENT_HDR_SIZE);
101475e84b7cSJohan Hedberg 
10157b1abbbeSJohan Hedberg 	if (event) {
10167b1abbbeSJohan Hedberg 		if (hdr->evt != event)
10177b1abbbeSJohan Hedberg 			goto failed;
10187b1abbbeSJohan Hedberg 		return skb;
10197b1abbbeSJohan Hedberg 	}
10207b1abbbeSJohan Hedberg 
102175e84b7cSJohan Hedberg 	if (hdr->evt != HCI_EV_CMD_COMPLETE) {
102275e84b7cSJohan Hedberg 		BT_DBG("Last event is not cmd complete (0x%2.2x)", hdr->evt);
102375e84b7cSJohan Hedberg 		goto failed;
102475e84b7cSJohan Hedberg 	}
102575e84b7cSJohan Hedberg 
102675e84b7cSJohan Hedberg 	if (skb->len < sizeof(*ev)) {
102775e84b7cSJohan Hedberg 		BT_ERR("Too short cmd_complete event");
102875e84b7cSJohan Hedberg 		goto failed;
102975e84b7cSJohan Hedberg 	}
103075e84b7cSJohan Hedberg 
103175e84b7cSJohan Hedberg 	ev = (void *) skb->data;
103275e84b7cSJohan Hedberg 	skb_pull(skb, sizeof(*ev));
103375e84b7cSJohan Hedberg 
103475e84b7cSJohan Hedberg 	if (opcode == __le16_to_cpu(ev->opcode))
103575e84b7cSJohan Hedberg 		return skb;
103675e84b7cSJohan Hedberg 
103775e84b7cSJohan Hedberg 	BT_DBG("opcode doesn't match (0x%2.2x != 0x%2.2x)", opcode,
103875e84b7cSJohan Hedberg 	       __le16_to_cpu(ev->opcode));
103975e84b7cSJohan Hedberg 
104075e84b7cSJohan Hedberg failed:
104175e84b7cSJohan Hedberg 	kfree_skb(skb);
104275e84b7cSJohan Hedberg 	return ERR_PTR(-ENODATA);
104375e84b7cSJohan Hedberg }
104475e84b7cSJohan Hedberg 
10457b1abbbeSJohan Hedberg struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
104607dc93ddSJohan Hedberg 				  const void *param, u8 event, u32 timeout)
104775e84b7cSJohan Hedberg {
104875e84b7cSJohan Hedberg 	DECLARE_WAITQUEUE(wait, current);
104975e84b7cSJohan Hedberg 	struct hci_request req;
105075e84b7cSJohan Hedberg 	int err = 0;
105175e84b7cSJohan Hedberg 
105275e84b7cSJohan Hedberg 	BT_DBG("%s", hdev->name);
105375e84b7cSJohan Hedberg 
105475e84b7cSJohan Hedberg 	hci_req_init(&req, hdev);
105575e84b7cSJohan Hedberg 
10567b1abbbeSJohan Hedberg 	hci_req_add_ev(&req, opcode, plen, param, event);
105775e84b7cSJohan Hedberg 
105875e84b7cSJohan Hedberg 	hdev->req_status = HCI_REQ_PEND;
105975e84b7cSJohan Hedberg 
106075e84b7cSJohan Hedberg 	err = hci_req_run(&req, hci_req_sync_complete);
106175e84b7cSJohan Hedberg 	if (err < 0)
106275e84b7cSJohan Hedberg 		return ERR_PTR(err);
106375e84b7cSJohan Hedberg 
106475e84b7cSJohan Hedberg 	add_wait_queue(&hdev->req_wait_q, &wait);
106575e84b7cSJohan Hedberg 	set_current_state(TASK_INTERRUPTIBLE);
106675e84b7cSJohan Hedberg 
106775e84b7cSJohan Hedberg 	schedule_timeout(timeout);
106875e84b7cSJohan Hedberg 
106975e84b7cSJohan Hedberg 	remove_wait_queue(&hdev->req_wait_q, &wait);
107075e84b7cSJohan Hedberg 
107175e84b7cSJohan Hedberg 	if (signal_pending(current))
107275e84b7cSJohan Hedberg 		return ERR_PTR(-EINTR);
107375e84b7cSJohan Hedberg 
107475e84b7cSJohan Hedberg 	switch (hdev->req_status) {
107575e84b7cSJohan Hedberg 	case HCI_REQ_DONE:
107675e84b7cSJohan Hedberg 		err = -bt_to_errno(hdev->req_result);
107775e84b7cSJohan Hedberg 		break;
107875e84b7cSJohan Hedberg 
107975e84b7cSJohan Hedberg 	case HCI_REQ_CANCELED:
108075e84b7cSJohan Hedberg 		err = -hdev->req_result;
108175e84b7cSJohan Hedberg 		break;
108275e84b7cSJohan Hedberg 
108375e84b7cSJohan Hedberg 	default:
108475e84b7cSJohan Hedberg 		err = -ETIMEDOUT;
108575e84b7cSJohan Hedberg 		break;
108675e84b7cSJohan Hedberg 	}
108775e84b7cSJohan Hedberg 
108875e84b7cSJohan Hedberg 	hdev->req_status = hdev->req_result = 0;
108975e84b7cSJohan Hedberg 
109075e84b7cSJohan Hedberg 	BT_DBG("%s end: err %d", hdev->name, err);
109175e84b7cSJohan Hedberg 
109275e84b7cSJohan Hedberg 	if (err < 0)
109375e84b7cSJohan Hedberg 		return ERR_PTR(err);
109475e84b7cSJohan Hedberg 
10957b1abbbeSJohan Hedberg 	return hci_get_cmd_complete(hdev, opcode, event);
10967b1abbbeSJohan Hedberg }
10977b1abbbeSJohan Hedberg EXPORT_SYMBOL(__hci_cmd_sync_ev);
10987b1abbbeSJohan Hedberg 
10997b1abbbeSJohan Hedberg struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
110007dc93ddSJohan Hedberg 			       const void *param, u32 timeout)
11017b1abbbeSJohan Hedberg {
11027b1abbbeSJohan Hedberg 	return __hci_cmd_sync_ev(hdev, opcode, plen, param, 0, timeout);
110375e84b7cSJohan Hedberg }
110475e84b7cSJohan Hedberg EXPORT_SYMBOL(__hci_cmd_sync);
110575e84b7cSJohan Hedberg 
11061da177e4SLinus Torvalds /* Execute request and wait for completion. */
110701178cd4SJohan Hedberg static int __hci_req_sync(struct hci_dev *hdev,
110842c6b129SJohan Hedberg 			  void (*func)(struct hci_request *req,
110942c6b129SJohan Hedberg 				      unsigned long opt),
11101da177e4SLinus Torvalds 			  unsigned long opt, __u32 timeout)
11111da177e4SLinus Torvalds {
111242c6b129SJohan Hedberg 	struct hci_request req;
11131da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
11141da177e4SLinus Torvalds 	int err = 0;
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds 	BT_DBG("%s start", hdev->name);
11171da177e4SLinus Torvalds 
111842c6b129SJohan Hedberg 	hci_req_init(&req, hdev);
111942c6b129SJohan Hedberg 
11201da177e4SLinus Torvalds 	hdev->req_status = HCI_REQ_PEND;
11211da177e4SLinus Torvalds 
112242c6b129SJohan Hedberg 	func(&req, opt);
112353cce22dSJohan Hedberg 
112442c6b129SJohan Hedberg 	err = hci_req_run(&req, hci_req_sync_complete);
112542c6b129SJohan Hedberg 	if (err < 0) {
112653cce22dSJohan Hedberg 		hdev->req_status = 0;
1127920c8300SAndre Guedes 
1128920c8300SAndre Guedes 		/* ENODATA means the HCI request command queue is empty.
1129920c8300SAndre Guedes 		 * This can happen when a request with conditionals doesn't
1130920c8300SAndre Guedes 		 * trigger any commands to be sent. This is normal behavior
1131920c8300SAndre Guedes 		 * and should not trigger an error return.
113242c6b129SJohan Hedberg 		 */
1133920c8300SAndre Guedes 		if (err == -ENODATA)
113442c6b129SJohan Hedberg 			return 0;
1135920c8300SAndre Guedes 
1136920c8300SAndre Guedes 		return err;
113753cce22dSJohan Hedberg 	}
113853cce22dSJohan Hedberg 
1139bc4445c7SAndre Guedes 	add_wait_queue(&hdev->req_wait_q, &wait);
1140bc4445c7SAndre Guedes 	set_current_state(TASK_INTERRUPTIBLE);
1141bc4445c7SAndre Guedes 
11421da177e4SLinus Torvalds 	schedule_timeout(timeout);
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	remove_wait_queue(&hdev->req_wait_q, &wait);
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 	if (signal_pending(current))
11471da177e4SLinus Torvalds 		return -EINTR;
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 	switch (hdev->req_status) {
11501da177e4SLinus Torvalds 	case HCI_REQ_DONE:
1151e175072fSJoe Perches 		err = -bt_to_errno(hdev->req_result);
11521da177e4SLinus Torvalds 		break;
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds 	case HCI_REQ_CANCELED:
11551da177e4SLinus Torvalds 		err = -hdev->req_result;
11561da177e4SLinus Torvalds 		break;
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 	default:
11591da177e4SLinus Torvalds 		err = -ETIMEDOUT;
11601da177e4SLinus Torvalds 		break;
11613ff50b79SStephen Hemminger 	}
11621da177e4SLinus Torvalds 
1163a5040efaSJohan Hedberg 	hdev->req_status = hdev->req_result = 0;
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 	BT_DBG("%s end: err %d", hdev->name, err);
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 	return err;
11681da177e4SLinus Torvalds }
11691da177e4SLinus Torvalds 
117001178cd4SJohan Hedberg static int hci_req_sync(struct hci_dev *hdev,
117142c6b129SJohan Hedberg 			void (*req)(struct hci_request *req,
117242c6b129SJohan Hedberg 				    unsigned long opt),
11731da177e4SLinus Torvalds 			unsigned long opt, __u32 timeout)
11741da177e4SLinus Torvalds {
11751da177e4SLinus Torvalds 	int ret;
11761da177e4SLinus Torvalds 
11777c6a329eSMarcel Holtmann 	if (!test_bit(HCI_UP, &hdev->flags))
11787c6a329eSMarcel Holtmann 		return -ENETDOWN;
11797c6a329eSMarcel Holtmann 
11801da177e4SLinus Torvalds 	/* Serialize all requests */
11811da177e4SLinus Torvalds 	hci_req_lock(hdev);
118201178cd4SJohan Hedberg 	ret = __hci_req_sync(hdev, req, opt, timeout);
11831da177e4SLinus Torvalds 	hci_req_unlock(hdev);
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 	return ret;
11861da177e4SLinus Torvalds }
11871da177e4SLinus Torvalds 
118842c6b129SJohan Hedberg static void hci_reset_req(struct hci_request *req, unsigned long opt)
11891da177e4SLinus Torvalds {
119042c6b129SJohan Hedberg 	BT_DBG("%s %ld", req->hdev->name, opt);
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	/* Reset device */
119342c6b129SJohan Hedberg 	set_bit(HCI_RESET, &req->hdev->flags);
119442c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_RESET, 0, NULL);
11951da177e4SLinus Torvalds }
11961da177e4SLinus Torvalds 
119742c6b129SJohan Hedberg static void bredr_init(struct hci_request *req)
11981da177e4SLinus Torvalds {
119942c6b129SJohan Hedberg 	req->hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED;
12002455a3eaSAndrei Emeltchenko 
12011da177e4SLinus Torvalds 	/* Read Local Supported Features */
120242c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_READ_LOCAL_FEATURES, 0, NULL);
12031da177e4SLinus Torvalds 
12041143e5a6SMarcel Holtmann 	/* Read Local Version */
120542c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL);
12062177bab5SJohan Hedberg 
12072177bab5SJohan Hedberg 	/* Read BD Address */
120842c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_READ_BD_ADDR, 0, NULL);
12091da177e4SLinus Torvalds }
12101da177e4SLinus Torvalds 
121142c6b129SJohan Hedberg static void amp_init(struct hci_request *req)
1212e61ef499SAndrei Emeltchenko {
121342c6b129SJohan Hedberg 	req->hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED;
12142455a3eaSAndrei Emeltchenko 
1215e61ef499SAndrei Emeltchenko 	/* Read Local Version */
121642c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL);
12176bcbc489SAndrei Emeltchenko 
1218f6996cfeSMarcel Holtmann 	/* Read Local Supported Commands */
1219f6996cfeSMarcel Holtmann 	hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL);
1220f6996cfeSMarcel Holtmann 
1221f6996cfeSMarcel Holtmann 	/* Read Local Supported Features */
1222f6996cfeSMarcel Holtmann 	hci_req_add(req, HCI_OP_READ_LOCAL_FEATURES, 0, NULL);
1223f6996cfeSMarcel Holtmann 
12246bcbc489SAndrei Emeltchenko 	/* Read Local AMP Info */
122542c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL);
1226e71dfabaSAndrei Emeltchenko 
1227e71dfabaSAndrei Emeltchenko 	/* Read Data Blk size */
122842c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_READ_DATA_BLOCK_SIZE, 0, NULL);
12297528ca1cSMarcel Holtmann 
1230f38ba941SMarcel Holtmann 	/* Read Flow Control Mode */
1231f38ba941SMarcel Holtmann 	hci_req_add(req, HCI_OP_READ_FLOW_CONTROL_MODE, 0, NULL);
1232f38ba941SMarcel Holtmann 
12337528ca1cSMarcel Holtmann 	/* Read Location Data */
12347528ca1cSMarcel Holtmann 	hci_req_add(req, HCI_OP_READ_LOCATION_DATA, 0, NULL);
1235e61ef499SAndrei Emeltchenko }
1236e61ef499SAndrei Emeltchenko 
123742c6b129SJohan Hedberg static void hci_init1_req(struct hci_request *req, unsigned long opt)
1238e61ef499SAndrei Emeltchenko {
123942c6b129SJohan Hedberg 	struct hci_dev *hdev = req->hdev;
1240e61ef499SAndrei Emeltchenko 
1241e61ef499SAndrei Emeltchenko 	BT_DBG("%s %ld", hdev->name, opt);
1242e61ef499SAndrei Emeltchenko 
124311778716SAndrei Emeltchenko 	/* Reset */
124411778716SAndrei Emeltchenko 	if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks))
124542c6b129SJohan Hedberg 		hci_reset_req(req, 0);
124611778716SAndrei Emeltchenko 
1247e61ef499SAndrei Emeltchenko 	switch (hdev->dev_type) {
1248e61ef499SAndrei Emeltchenko 	case HCI_BREDR:
124942c6b129SJohan Hedberg 		bredr_init(req);
1250e61ef499SAndrei Emeltchenko 		break;
1251e61ef499SAndrei Emeltchenko 
1252e61ef499SAndrei Emeltchenko 	case HCI_AMP:
125342c6b129SJohan Hedberg 		amp_init(req);
1254e61ef499SAndrei Emeltchenko 		break;
1255e61ef499SAndrei Emeltchenko 
1256e61ef499SAndrei Emeltchenko 	default:
1257e61ef499SAndrei Emeltchenko 		BT_ERR("Unknown device type %d", hdev->dev_type);
1258e61ef499SAndrei Emeltchenko 		break;
1259e61ef499SAndrei Emeltchenko 	}
1260e61ef499SAndrei Emeltchenko }
1261e61ef499SAndrei Emeltchenko 
126242c6b129SJohan Hedberg static void bredr_setup(struct hci_request *req)
12632177bab5SJohan Hedberg {
12644ca048e3SMarcel Holtmann 	struct hci_dev *hdev = req->hdev;
12654ca048e3SMarcel Holtmann 
12662177bab5SJohan Hedberg 	__le16 param;
12672177bab5SJohan Hedberg 	__u8 flt_type;
12682177bab5SJohan Hedberg 
12692177bab5SJohan Hedberg 	/* Read Buffer Size (ACL mtu, max pkt, etc.) */
127042c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_READ_BUFFER_SIZE, 0, NULL);
12712177bab5SJohan Hedberg 
12722177bab5SJohan Hedberg 	/* Read Class of Device */
127342c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_READ_CLASS_OF_DEV, 0, NULL);
12742177bab5SJohan Hedberg 
12752177bab5SJohan Hedberg 	/* Read Local Name */
127642c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_READ_LOCAL_NAME, 0, NULL);
12772177bab5SJohan Hedberg 
12782177bab5SJohan Hedberg 	/* Read Voice Setting */
127942c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_READ_VOICE_SETTING, 0, NULL);
12802177bab5SJohan Hedberg 
1281b4cb9fb2SMarcel Holtmann 	/* Read Number of Supported IAC */
1282b4cb9fb2SMarcel Holtmann 	hci_req_add(req, HCI_OP_READ_NUM_SUPPORTED_IAC, 0, NULL);
1283b4cb9fb2SMarcel Holtmann 
12844b836f39SMarcel Holtmann 	/* Read Current IAC LAP */
12854b836f39SMarcel Holtmann 	hci_req_add(req, HCI_OP_READ_CURRENT_IAC_LAP, 0, NULL);
12864b836f39SMarcel Holtmann 
12872177bab5SJohan Hedberg 	/* Clear Event Filters */
12882177bab5SJohan Hedberg 	flt_type = HCI_FLT_CLEAR_ALL;
128942c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_SET_EVENT_FLT, 1, &flt_type);
12902177bab5SJohan Hedberg 
12912177bab5SJohan Hedberg 	/* Connection accept timeout ~20 secs */
1292dcf4adbfSJoe Perches 	param = cpu_to_le16(0x7d00);
129342c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_WRITE_CA_TIMEOUT, 2, &param);
12942177bab5SJohan Hedberg 
12954ca048e3SMarcel Holtmann 	/* AVM Berlin (31), aka "BlueFRITZ!", reports version 1.2,
12964ca048e3SMarcel Holtmann 	 * but it does not support page scan related HCI commands.
12974ca048e3SMarcel Holtmann 	 */
12984ca048e3SMarcel Holtmann 	if (hdev->manufacturer != 31 && hdev->hci_ver > BLUETOOTH_VER_1_1) {
1299f332ec66SJohan Hedberg 		hci_req_add(req, HCI_OP_READ_PAGE_SCAN_ACTIVITY, 0, NULL);
1300f332ec66SJohan Hedberg 		hci_req_add(req, HCI_OP_READ_PAGE_SCAN_TYPE, 0, NULL);
1301f332ec66SJohan Hedberg 	}
13022177bab5SJohan Hedberg }
13032177bab5SJohan Hedberg 
130442c6b129SJohan Hedberg static void le_setup(struct hci_request *req)
13052177bab5SJohan Hedberg {
1306c73eee91SJohan Hedberg 	struct hci_dev *hdev = req->hdev;
1307c73eee91SJohan Hedberg 
13082177bab5SJohan Hedberg 	/* Read LE Buffer Size */
130942c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_LE_READ_BUFFER_SIZE, 0, NULL);
13102177bab5SJohan Hedberg 
13112177bab5SJohan Hedberg 	/* Read LE Local Supported Features */
131242c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_LE_READ_LOCAL_FEATURES, 0, NULL);
13132177bab5SJohan Hedberg 
1314747d3f03SMarcel Holtmann 	/* Read LE Supported States */
1315747d3f03SMarcel Holtmann 	hci_req_add(req, HCI_OP_LE_READ_SUPPORTED_STATES, 0, NULL);
1316747d3f03SMarcel Holtmann 
13172177bab5SJohan Hedberg 	/* Read LE Advertising Channel TX Power */
131842c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_LE_READ_ADV_TX_POWER, 0, NULL);
13192177bab5SJohan Hedberg 
13202177bab5SJohan Hedberg 	/* Read LE White List Size */
132142c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_LE_READ_WHITE_LIST_SIZE, 0, NULL);
13222177bab5SJohan Hedberg 
1323747d3f03SMarcel Holtmann 	/* Clear LE White List */
1324747d3f03SMarcel Holtmann 	hci_req_add(req, HCI_OP_LE_CLEAR_WHITE_LIST, 0, NULL);
1325c73eee91SJohan Hedberg 
1326c73eee91SJohan Hedberg 	/* LE-only controllers have LE implicitly enabled */
1327c73eee91SJohan Hedberg 	if (!lmp_bredr_capable(hdev))
1328c73eee91SJohan Hedberg 		set_bit(HCI_LE_ENABLED, &hdev->dev_flags);
13292177bab5SJohan Hedberg }
13302177bab5SJohan Hedberg 
13312177bab5SJohan Hedberg static u8 hci_get_inquiry_mode(struct hci_dev *hdev)
13322177bab5SJohan Hedberg {
13332177bab5SJohan Hedberg 	if (lmp_ext_inq_capable(hdev))
13342177bab5SJohan Hedberg 		return 0x02;
13352177bab5SJohan Hedberg 
13362177bab5SJohan Hedberg 	if (lmp_inq_rssi_capable(hdev))
13372177bab5SJohan Hedberg 		return 0x01;
13382177bab5SJohan Hedberg 
13392177bab5SJohan Hedberg 	if (hdev->manufacturer == 11 && hdev->hci_rev == 0x00 &&
13402177bab5SJohan Hedberg 	    hdev->lmp_subver == 0x0757)
13412177bab5SJohan Hedberg 		return 0x01;
13422177bab5SJohan Hedberg 
13432177bab5SJohan Hedberg 	if (hdev->manufacturer == 15) {
13442177bab5SJohan Hedberg 		if (hdev->hci_rev == 0x03 && hdev->lmp_subver == 0x6963)
13452177bab5SJohan Hedberg 			return 0x01;
13462177bab5SJohan Hedberg 		if (hdev->hci_rev == 0x09 && hdev->lmp_subver == 0x6963)
13472177bab5SJohan Hedberg 			return 0x01;
13482177bab5SJohan Hedberg 		if (hdev->hci_rev == 0x00 && hdev->lmp_subver == 0x6965)
13492177bab5SJohan Hedberg 			return 0x01;
13502177bab5SJohan Hedberg 	}
13512177bab5SJohan Hedberg 
13522177bab5SJohan Hedberg 	if (hdev->manufacturer == 31 && hdev->hci_rev == 0x2005 &&
13532177bab5SJohan Hedberg 	    hdev->lmp_subver == 0x1805)
13542177bab5SJohan Hedberg 		return 0x01;
13552177bab5SJohan Hedberg 
13562177bab5SJohan Hedberg 	return 0x00;
13572177bab5SJohan Hedberg }
13582177bab5SJohan Hedberg 
135942c6b129SJohan Hedberg static void hci_setup_inquiry_mode(struct hci_request *req)
13602177bab5SJohan Hedberg {
13612177bab5SJohan Hedberg 	u8 mode;
13622177bab5SJohan Hedberg 
136342c6b129SJohan Hedberg 	mode = hci_get_inquiry_mode(req->hdev);
13642177bab5SJohan Hedberg 
136542c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_WRITE_INQUIRY_MODE, 1, &mode);
13662177bab5SJohan Hedberg }
13672177bab5SJohan Hedberg 
136842c6b129SJohan Hedberg static void hci_setup_event_mask(struct hci_request *req)
13692177bab5SJohan Hedberg {
137042c6b129SJohan Hedberg 	struct hci_dev *hdev = req->hdev;
137142c6b129SJohan Hedberg 
13722177bab5SJohan Hedberg 	/* The second byte is 0xff instead of 0x9f (two reserved bits
13732177bab5SJohan Hedberg 	 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
13742177bab5SJohan Hedberg 	 * command otherwise.
13752177bab5SJohan Hedberg 	 */
13762177bab5SJohan Hedberg 	u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
13772177bab5SJohan Hedberg 
13782177bab5SJohan Hedberg 	/* CSR 1.1 dongles does not accept any bitfield so don't try to set
13792177bab5SJohan Hedberg 	 * any event mask for pre 1.2 devices.
13802177bab5SJohan Hedberg 	 */
13812177bab5SJohan Hedberg 	if (hdev->hci_ver < BLUETOOTH_VER_1_2)
13822177bab5SJohan Hedberg 		return;
13832177bab5SJohan Hedberg 
13842177bab5SJohan Hedberg 	if (lmp_bredr_capable(hdev)) {
13852177bab5SJohan Hedberg 		events[4] |= 0x01; /* Flow Specification Complete */
13862177bab5SJohan Hedberg 		events[4] |= 0x02; /* Inquiry Result with RSSI */
13872177bab5SJohan Hedberg 		events[4] |= 0x04; /* Read Remote Extended Features Complete */
13882177bab5SJohan Hedberg 		events[5] |= 0x08; /* Synchronous Connection Complete */
13892177bab5SJohan Hedberg 		events[5] |= 0x10; /* Synchronous Connection Changed */
1390c7882cbdSMarcel Holtmann 	} else {
1391c7882cbdSMarcel Holtmann 		/* Use a different default for LE-only devices */
1392c7882cbdSMarcel Holtmann 		memset(events, 0, sizeof(events));
1393c7882cbdSMarcel Holtmann 		events[0] |= 0x10; /* Disconnection Complete */
1394c7882cbdSMarcel Holtmann 		events[0] |= 0x80; /* Encryption Change */
1395c7882cbdSMarcel Holtmann 		events[1] |= 0x08; /* Read Remote Version Information Complete */
1396c7882cbdSMarcel Holtmann 		events[1] |= 0x20; /* Command Complete */
1397c7882cbdSMarcel Holtmann 		events[1] |= 0x40; /* Command Status */
1398c7882cbdSMarcel Holtmann 		events[1] |= 0x80; /* Hardware Error */
1399c7882cbdSMarcel Holtmann 		events[2] |= 0x04; /* Number of Completed Packets */
1400c7882cbdSMarcel Holtmann 		events[3] |= 0x02; /* Data Buffer Overflow */
1401c7882cbdSMarcel Holtmann 		events[5] |= 0x80; /* Encryption Key Refresh Complete */
14022177bab5SJohan Hedberg 	}
14032177bab5SJohan Hedberg 
14042177bab5SJohan Hedberg 	if (lmp_inq_rssi_capable(hdev))
14052177bab5SJohan Hedberg 		events[4] |= 0x02; /* Inquiry Result with RSSI */
14062177bab5SJohan Hedberg 
14072177bab5SJohan Hedberg 	if (lmp_sniffsubr_capable(hdev))
14082177bab5SJohan Hedberg 		events[5] |= 0x20; /* Sniff Subrating */
14092177bab5SJohan Hedberg 
14102177bab5SJohan Hedberg 	if (lmp_pause_enc_capable(hdev))
14112177bab5SJohan Hedberg 		events[5] |= 0x80; /* Encryption Key Refresh Complete */
14122177bab5SJohan Hedberg 
14132177bab5SJohan Hedberg 	if (lmp_ext_inq_capable(hdev))
14142177bab5SJohan Hedberg 		events[5] |= 0x40; /* Extended Inquiry Result */
14152177bab5SJohan Hedberg 
14162177bab5SJohan Hedberg 	if (lmp_no_flush_capable(hdev))
14172177bab5SJohan Hedberg 		events[7] |= 0x01; /* Enhanced Flush Complete */
14182177bab5SJohan Hedberg 
14192177bab5SJohan Hedberg 	if (lmp_lsto_capable(hdev))
14202177bab5SJohan Hedberg 		events[6] |= 0x80; /* Link Supervision Timeout Changed */
14212177bab5SJohan Hedberg 
14222177bab5SJohan Hedberg 	if (lmp_ssp_capable(hdev)) {
14232177bab5SJohan Hedberg 		events[6] |= 0x01;	/* IO Capability Request */
14242177bab5SJohan Hedberg 		events[6] |= 0x02;	/* IO Capability Response */
14252177bab5SJohan Hedberg 		events[6] |= 0x04;	/* User Confirmation Request */
14262177bab5SJohan Hedberg 		events[6] |= 0x08;	/* User Passkey Request */
14272177bab5SJohan Hedberg 		events[6] |= 0x10;	/* Remote OOB Data Request */
14282177bab5SJohan Hedberg 		events[6] |= 0x20;	/* Simple Pairing Complete */
14292177bab5SJohan Hedberg 		events[7] |= 0x04;	/* User Passkey Notification */
14302177bab5SJohan Hedberg 		events[7] |= 0x08;	/* Keypress Notification */
14312177bab5SJohan Hedberg 		events[7] |= 0x10;	/* Remote Host Supported
14322177bab5SJohan Hedberg 					 * Features Notification
14332177bab5SJohan Hedberg 					 */
14342177bab5SJohan Hedberg 	}
14352177bab5SJohan Hedberg 
14362177bab5SJohan Hedberg 	if (lmp_le_capable(hdev))
14372177bab5SJohan Hedberg 		events[7] |= 0x20;	/* LE Meta-Event */
14382177bab5SJohan Hedberg 
143942c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_SET_EVENT_MASK, sizeof(events), events);
14402177bab5SJohan Hedberg }
14412177bab5SJohan Hedberg 
144242c6b129SJohan Hedberg static void hci_init2_req(struct hci_request *req, unsigned long opt)
14432177bab5SJohan Hedberg {
144442c6b129SJohan Hedberg 	struct hci_dev *hdev = req->hdev;
144542c6b129SJohan Hedberg 
14462177bab5SJohan Hedberg 	if (lmp_bredr_capable(hdev))
144742c6b129SJohan Hedberg 		bredr_setup(req);
144856f87901SJohan Hedberg 	else
144956f87901SJohan Hedberg 		clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags);
14502177bab5SJohan Hedberg 
14512177bab5SJohan Hedberg 	if (lmp_le_capable(hdev))
145242c6b129SJohan Hedberg 		le_setup(req);
14532177bab5SJohan Hedberg 
145442c6b129SJohan Hedberg 	hci_setup_event_mask(req);
14552177bab5SJohan Hedberg 
14563f8e2d75SJohan Hedberg 	/* AVM Berlin (31), aka "BlueFRITZ!", doesn't support the read
14573f8e2d75SJohan Hedberg 	 * local supported commands HCI command.
14583f8e2d75SJohan Hedberg 	 */
14593f8e2d75SJohan Hedberg 	if (hdev->manufacturer != 31 && hdev->hci_ver > BLUETOOTH_VER_1_1)
146042c6b129SJohan Hedberg 		hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL);
14612177bab5SJohan Hedberg 
14622177bab5SJohan Hedberg 	if (lmp_ssp_capable(hdev)) {
146357af75a8SMarcel Holtmann 		/* When SSP is available, then the host features page
146457af75a8SMarcel Holtmann 		 * should also be available as well. However some
146557af75a8SMarcel Holtmann 		 * controllers list the max_page as 0 as long as SSP
146657af75a8SMarcel Holtmann 		 * has not been enabled. To achieve proper debugging
146757af75a8SMarcel Holtmann 		 * output, force the minimum max_page to 1 at least.
146857af75a8SMarcel Holtmann 		 */
146957af75a8SMarcel Holtmann 		hdev->max_page = 0x01;
147057af75a8SMarcel Holtmann 
14712177bab5SJohan Hedberg 		if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
14722177bab5SJohan Hedberg 			u8 mode = 0x01;
147342c6b129SJohan Hedberg 			hci_req_add(req, HCI_OP_WRITE_SSP_MODE,
14742177bab5SJohan Hedberg 				    sizeof(mode), &mode);
14752177bab5SJohan Hedberg 		} else {
14762177bab5SJohan Hedberg 			struct hci_cp_write_eir cp;
14772177bab5SJohan Hedberg 
14782177bab5SJohan Hedberg 			memset(hdev->eir, 0, sizeof(hdev->eir));
14792177bab5SJohan Hedberg 			memset(&cp, 0, sizeof(cp));
14802177bab5SJohan Hedberg 
148142c6b129SJohan Hedberg 			hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
14822177bab5SJohan Hedberg 		}
14832177bab5SJohan Hedberg 	}
14842177bab5SJohan Hedberg 
14852177bab5SJohan Hedberg 	if (lmp_inq_rssi_capable(hdev))
148642c6b129SJohan Hedberg 		hci_setup_inquiry_mode(req);
14872177bab5SJohan Hedberg 
14882177bab5SJohan Hedberg 	if (lmp_inq_tx_pwr_capable(hdev))
148942c6b129SJohan Hedberg 		hci_req_add(req, HCI_OP_READ_INQ_RSP_TX_POWER, 0, NULL);
14902177bab5SJohan Hedberg 
14912177bab5SJohan Hedberg 	if (lmp_ext_feat_capable(hdev)) {
14922177bab5SJohan Hedberg 		struct hci_cp_read_local_ext_features cp;
14932177bab5SJohan Hedberg 
14942177bab5SJohan Hedberg 		cp.page = 0x01;
149542c6b129SJohan Hedberg 		hci_req_add(req, HCI_OP_READ_LOCAL_EXT_FEATURES,
149642c6b129SJohan Hedberg 			    sizeof(cp), &cp);
14972177bab5SJohan Hedberg 	}
14982177bab5SJohan Hedberg 
14992177bab5SJohan Hedberg 	if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) {
15002177bab5SJohan Hedberg 		u8 enable = 1;
150142c6b129SJohan Hedberg 		hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE, sizeof(enable),
15022177bab5SJohan Hedberg 			    &enable);
15032177bab5SJohan Hedberg 	}
15042177bab5SJohan Hedberg }
15052177bab5SJohan Hedberg 
150642c6b129SJohan Hedberg static void hci_setup_link_policy(struct hci_request *req)
15072177bab5SJohan Hedberg {
150842c6b129SJohan Hedberg 	struct hci_dev *hdev = req->hdev;
15092177bab5SJohan Hedberg 	struct hci_cp_write_def_link_policy cp;
15102177bab5SJohan Hedberg 	u16 link_policy = 0;
15112177bab5SJohan Hedberg 
15122177bab5SJohan Hedberg 	if (lmp_rswitch_capable(hdev))
15132177bab5SJohan Hedberg 		link_policy |= HCI_LP_RSWITCH;
15142177bab5SJohan Hedberg 	if (lmp_hold_capable(hdev))
15152177bab5SJohan Hedberg 		link_policy |= HCI_LP_HOLD;
15162177bab5SJohan Hedberg 	if (lmp_sniff_capable(hdev))
15172177bab5SJohan Hedberg 		link_policy |= HCI_LP_SNIFF;
15182177bab5SJohan Hedberg 	if (lmp_park_capable(hdev))
15192177bab5SJohan Hedberg 		link_policy |= HCI_LP_PARK;
15202177bab5SJohan Hedberg 
15212177bab5SJohan Hedberg 	cp.policy = cpu_to_le16(link_policy);
152242c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_WRITE_DEF_LINK_POLICY, sizeof(cp), &cp);
15232177bab5SJohan Hedberg }
15242177bab5SJohan Hedberg 
152542c6b129SJohan Hedberg static void hci_set_le_support(struct hci_request *req)
15262177bab5SJohan Hedberg {
152742c6b129SJohan Hedberg 	struct hci_dev *hdev = req->hdev;
15282177bab5SJohan Hedberg 	struct hci_cp_write_le_host_supported cp;
15292177bab5SJohan Hedberg 
1530c73eee91SJohan Hedberg 	/* LE-only devices do not support explicit enablement */
1531c73eee91SJohan Hedberg 	if (!lmp_bredr_capable(hdev))
1532c73eee91SJohan Hedberg 		return;
1533c73eee91SJohan Hedberg 
15342177bab5SJohan Hedberg 	memset(&cp, 0, sizeof(cp));
15352177bab5SJohan Hedberg 
15362177bab5SJohan Hedberg 	if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) {
15372177bab5SJohan Hedberg 		cp.le = 0x01;
15382177bab5SJohan Hedberg 		cp.simul = lmp_le_br_capable(hdev);
15392177bab5SJohan Hedberg 	}
15402177bab5SJohan Hedberg 
15412177bab5SJohan Hedberg 	if (cp.le != lmp_host_le_capable(hdev))
154242c6b129SJohan Hedberg 		hci_req_add(req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(cp),
15432177bab5SJohan Hedberg 			    &cp);
15442177bab5SJohan Hedberg }
15452177bab5SJohan Hedberg 
1546d62e6d67SJohan Hedberg static void hci_set_event_mask_page_2(struct hci_request *req)
1547d62e6d67SJohan Hedberg {
1548d62e6d67SJohan Hedberg 	struct hci_dev *hdev = req->hdev;
1549d62e6d67SJohan Hedberg 	u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1550d62e6d67SJohan Hedberg 
1551d62e6d67SJohan Hedberg 	/* If Connectionless Slave Broadcast master role is supported
1552d62e6d67SJohan Hedberg 	 * enable all necessary events for it.
1553d62e6d67SJohan Hedberg 	 */
155453b834d2SMarcel Holtmann 	if (lmp_csb_master_capable(hdev)) {
1555d62e6d67SJohan Hedberg 		events[1] |= 0x40;	/* Triggered Clock Capture */
1556d62e6d67SJohan Hedberg 		events[1] |= 0x80;	/* Synchronization Train Complete */
1557d62e6d67SJohan Hedberg 		events[2] |= 0x10;	/* Slave Page Response Timeout */
1558d62e6d67SJohan Hedberg 		events[2] |= 0x20;	/* CSB Channel Map Change */
1559d62e6d67SJohan Hedberg 	}
1560d62e6d67SJohan Hedberg 
1561d62e6d67SJohan Hedberg 	/* If Connectionless Slave Broadcast slave role is supported
1562d62e6d67SJohan Hedberg 	 * enable all necessary events for it.
1563d62e6d67SJohan Hedberg 	 */
156453b834d2SMarcel Holtmann 	if (lmp_csb_slave_capable(hdev)) {
1565d62e6d67SJohan Hedberg 		events[2] |= 0x01;	/* Synchronization Train Received */
1566d62e6d67SJohan Hedberg 		events[2] |= 0x02;	/* CSB Receive */
1567d62e6d67SJohan Hedberg 		events[2] |= 0x04;	/* CSB Timeout */
1568d62e6d67SJohan Hedberg 		events[2] |= 0x08;	/* Truncated Page Complete */
1569d62e6d67SJohan Hedberg 	}
1570d62e6d67SJohan Hedberg 
157140c59fcbSMarcel Holtmann 	/* Enable Authenticated Payload Timeout Expired event if supported */
157240c59fcbSMarcel Holtmann 	if (lmp_ping_capable(hdev))
157340c59fcbSMarcel Holtmann 		events[2] |= 0x80;
157440c59fcbSMarcel Holtmann 
1575d62e6d67SJohan Hedberg 	hci_req_add(req, HCI_OP_SET_EVENT_MASK_PAGE_2, sizeof(events), events);
1576d62e6d67SJohan Hedberg }
1577d62e6d67SJohan Hedberg 
157842c6b129SJohan Hedberg static void hci_init3_req(struct hci_request *req, unsigned long opt)
15792177bab5SJohan Hedberg {
158042c6b129SJohan Hedberg 	struct hci_dev *hdev = req->hdev;
1581d2c5d77fSJohan Hedberg 	u8 p;
158242c6b129SJohan Hedberg 
1583b8f4e068SGustavo Padovan 	/* Some Broadcom based Bluetooth controllers do not support the
1584b8f4e068SGustavo Padovan 	 * Delete Stored Link Key command. They are clearly indicating its
1585b8f4e068SGustavo Padovan 	 * absence in the bit mask of supported commands.
1586b8f4e068SGustavo Padovan 	 *
1587b8f4e068SGustavo Padovan 	 * Check the supported commands and only if the the command is marked
1588b8f4e068SGustavo Padovan 	 * as supported send it. If not supported assume that the controller
1589b8f4e068SGustavo Padovan 	 * does not have actual support for stored link keys which makes this
1590b8f4e068SGustavo Padovan 	 * command redundant anyway.
1591f9f462faSMarcel Holtmann 	 *
1592f9f462faSMarcel Holtmann 	 * Some controllers indicate that they support handling deleting
1593f9f462faSMarcel Holtmann 	 * stored link keys, but they don't. The quirk lets a driver
1594f9f462faSMarcel Holtmann 	 * just disable this command.
1595b8f4e068SGustavo Padovan 	 */
1596f9f462faSMarcel Holtmann 	if (hdev->commands[6] & 0x80 &&
1597f9f462faSMarcel Holtmann 	    !test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks)) {
159859f45d57SJohan Hedberg 		struct hci_cp_delete_stored_link_key cp;
159959f45d57SJohan Hedberg 
160059f45d57SJohan Hedberg 		bacpy(&cp.bdaddr, BDADDR_ANY);
160159f45d57SJohan Hedberg 		cp.delete_all = 0x01;
160259f45d57SJohan Hedberg 		hci_req_add(req, HCI_OP_DELETE_STORED_LINK_KEY,
160359f45d57SJohan Hedberg 			    sizeof(cp), &cp);
160459f45d57SJohan Hedberg 	}
160559f45d57SJohan Hedberg 
16062177bab5SJohan Hedberg 	if (hdev->commands[5] & 0x10)
160742c6b129SJohan Hedberg 		hci_setup_link_policy(req);
16082177bab5SJohan Hedberg 
16099193c6e8SAndre Guedes 	if (lmp_le_capable(hdev)) {
16109193c6e8SAndre Guedes 		u8 events[8];
16119193c6e8SAndre Guedes 
16129193c6e8SAndre Guedes 		memset(events, 0, sizeof(events));
16139193c6e8SAndre Guedes 		events[0] = 0x1f;
1614662bc2e6SAndre Guedes 
1615662bc2e6SAndre Guedes 		/* If controller supports the Connection Parameters Request
1616662bc2e6SAndre Guedes 		 * Link Layer Procedure, enable the corresponding event.
1617662bc2e6SAndre Guedes 		 */
1618662bc2e6SAndre Guedes 		if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC)
1619662bc2e6SAndre Guedes 			events[0] |= 0x20;	/* LE Remote Connection
1620662bc2e6SAndre Guedes 						 * Parameter Request
1621662bc2e6SAndre Guedes 						 */
1622662bc2e6SAndre Guedes 
16239193c6e8SAndre Guedes 		hci_req_add(req, HCI_OP_LE_SET_EVENT_MASK, sizeof(events),
16249193c6e8SAndre Guedes 			    events);
16259193c6e8SAndre Guedes 
162642c6b129SJohan Hedberg 		hci_set_le_support(req);
16279193c6e8SAndre Guedes 	}
1628d2c5d77fSJohan Hedberg 
1629d2c5d77fSJohan Hedberg 	/* Read features beyond page 1 if available */
1630d2c5d77fSJohan Hedberg 	for (p = 2; p < HCI_MAX_PAGES && p <= hdev->max_page; p++) {
1631d2c5d77fSJohan Hedberg 		struct hci_cp_read_local_ext_features cp;
1632d2c5d77fSJohan Hedberg 
1633d2c5d77fSJohan Hedberg 		cp.page = p;
1634d2c5d77fSJohan Hedberg 		hci_req_add(req, HCI_OP_READ_LOCAL_EXT_FEATURES,
1635d2c5d77fSJohan Hedberg 			    sizeof(cp), &cp);
1636d2c5d77fSJohan Hedberg 	}
16372177bab5SJohan Hedberg }
16382177bab5SJohan Hedberg 
16395d4e7e8dSJohan Hedberg static void hci_init4_req(struct hci_request *req, unsigned long opt)
16405d4e7e8dSJohan Hedberg {
16415d4e7e8dSJohan Hedberg 	struct hci_dev *hdev = req->hdev;
16425d4e7e8dSJohan Hedberg 
1643d62e6d67SJohan Hedberg 	/* Set event mask page 2 if the HCI command for it is supported */
1644d62e6d67SJohan Hedberg 	if (hdev->commands[22] & 0x04)
1645d62e6d67SJohan Hedberg 		hci_set_event_mask_page_2(req);
1646d62e6d67SJohan Hedberg 
16475d4e7e8dSJohan Hedberg 	/* Check for Synchronization Train support */
164853b834d2SMarcel Holtmann 	if (lmp_sync_train_capable(hdev))
16495d4e7e8dSJohan Hedberg 		hci_req_add(req, HCI_OP_READ_SYNC_TRAIN_PARAMS, 0, NULL);
1650a6d0d690SMarcel Holtmann 
1651a6d0d690SMarcel Holtmann 	/* Enable Secure Connections if supported and configured */
16525afeac14SMarcel Holtmann 	if ((lmp_sc_capable(hdev) ||
1653111902f7SMarcel Holtmann 	     test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) &&
1654a6d0d690SMarcel Holtmann 	    test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) {
1655a6d0d690SMarcel Holtmann 		u8 support = 0x01;
1656a6d0d690SMarcel Holtmann 		hci_req_add(req, HCI_OP_WRITE_SC_SUPPORT,
1657a6d0d690SMarcel Holtmann 			    sizeof(support), &support);
1658a6d0d690SMarcel Holtmann 	}
16595d4e7e8dSJohan Hedberg }
16605d4e7e8dSJohan Hedberg 
16612177bab5SJohan Hedberg static int __hci_init(struct hci_dev *hdev)
16622177bab5SJohan Hedberg {
16632177bab5SJohan Hedberg 	int err;
16642177bab5SJohan Hedberg 
16652177bab5SJohan Hedberg 	err = __hci_req_sync(hdev, hci_init1_req, 0, HCI_INIT_TIMEOUT);
16662177bab5SJohan Hedberg 	if (err < 0)
16672177bab5SJohan Hedberg 		return err;
16682177bab5SJohan Hedberg 
16694b4148e9SMarcel Holtmann 	/* The Device Under Test (DUT) mode is special and available for
16704b4148e9SMarcel Holtmann 	 * all controller types. So just create it early on.
16714b4148e9SMarcel Holtmann 	 */
16724b4148e9SMarcel Holtmann 	if (test_bit(HCI_SETUP, &hdev->dev_flags)) {
16734b4148e9SMarcel Holtmann 		debugfs_create_file("dut_mode", 0644, hdev->debugfs, hdev,
16744b4148e9SMarcel Holtmann 				    &dut_mode_fops);
16754b4148e9SMarcel Holtmann 	}
16764b4148e9SMarcel Holtmann 
16772177bab5SJohan Hedberg 	/* HCI_BREDR covers both single-mode LE, BR/EDR and dual-mode
16782177bab5SJohan Hedberg 	 * BR/EDR/LE type controllers. AMP controllers only need the
16792177bab5SJohan Hedberg 	 * first stage init.
16802177bab5SJohan Hedberg 	 */
16812177bab5SJohan Hedberg 	if (hdev->dev_type != HCI_BREDR)
16822177bab5SJohan Hedberg 		return 0;
16832177bab5SJohan Hedberg 
16842177bab5SJohan Hedberg 	err = __hci_req_sync(hdev, hci_init2_req, 0, HCI_INIT_TIMEOUT);
16852177bab5SJohan Hedberg 	if (err < 0)
16862177bab5SJohan Hedberg 		return err;
16872177bab5SJohan Hedberg 
16885d4e7e8dSJohan Hedberg 	err = __hci_req_sync(hdev, hci_init3_req, 0, HCI_INIT_TIMEOUT);
16895d4e7e8dSJohan Hedberg 	if (err < 0)
16905d4e7e8dSJohan Hedberg 		return err;
16915d4e7e8dSJohan Hedberg 
1692baf27f6eSMarcel Holtmann 	err = __hci_req_sync(hdev, hci_init4_req, 0, HCI_INIT_TIMEOUT);
1693baf27f6eSMarcel Holtmann 	if (err < 0)
1694baf27f6eSMarcel Holtmann 		return err;
1695baf27f6eSMarcel Holtmann 
1696baf27f6eSMarcel Holtmann 	/* Only create debugfs entries during the initial setup
1697baf27f6eSMarcel Holtmann 	 * phase and not every time the controller gets powered on.
1698baf27f6eSMarcel Holtmann 	 */
1699baf27f6eSMarcel Holtmann 	if (!test_bit(HCI_SETUP, &hdev->dev_flags))
1700baf27f6eSMarcel Holtmann 		return 0;
1701baf27f6eSMarcel Holtmann 
1702dfb826a8SMarcel Holtmann 	debugfs_create_file("features", 0444, hdev->debugfs, hdev,
1703dfb826a8SMarcel Holtmann 			    &features_fops);
1704ceeb3bc0SMarcel Holtmann 	debugfs_create_u16("manufacturer", 0444, hdev->debugfs,
1705ceeb3bc0SMarcel Holtmann 			   &hdev->manufacturer);
1706ceeb3bc0SMarcel Holtmann 	debugfs_create_u8("hci_version", 0444, hdev->debugfs, &hdev->hci_ver);
1707ceeb3bc0SMarcel Holtmann 	debugfs_create_u16("hci_revision", 0444, hdev->debugfs, &hdev->hci_rev);
170870afe0b8SMarcel Holtmann 	debugfs_create_file("blacklist", 0444, hdev->debugfs, hdev,
170970afe0b8SMarcel Holtmann 			    &blacklist_fops);
171047219839SMarcel Holtmann 	debugfs_create_file("uuids", 0444, hdev->debugfs, hdev, &uuids_fops);
171147219839SMarcel Holtmann 
171231ad1691SAndrzej Kaczmarek 	debugfs_create_file("conn_info_min_age", 0644, hdev->debugfs, hdev,
171331ad1691SAndrzej Kaczmarek 			    &conn_info_min_age_fops);
171431ad1691SAndrzej Kaczmarek 	debugfs_create_file("conn_info_max_age", 0644, hdev->debugfs, hdev,
171531ad1691SAndrzej Kaczmarek 			    &conn_info_max_age_fops);
171631ad1691SAndrzej Kaczmarek 
1717baf27f6eSMarcel Holtmann 	if (lmp_bredr_capable(hdev)) {
1718baf27f6eSMarcel Holtmann 		debugfs_create_file("inquiry_cache", 0444, hdev->debugfs,
1719baf27f6eSMarcel Holtmann 				    hdev, &inquiry_cache_fops);
172002d08d15SMarcel Holtmann 		debugfs_create_file("link_keys", 0400, hdev->debugfs,
172102d08d15SMarcel Holtmann 				    hdev, &link_keys_fops);
1722babdbb3cSMarcel Holtmann 		debugfs_create_file("dev_class", 0444, hdev->debugfs,
1723babdbb3cSMarcel Holtmann 				    hdev, &dev_class_fops);
1724041000b9SMarcel Holtmann 		debugfs_create_file("voice_setting", 0444, hdev->debugfs,
1725041000b9SMarcel Holtmann 				    hdev, &voice_setting_fops);
1726baf27f6eSMarcel Holtmann 	}
1727baf27f6eSMarcel Holtmann 
172806f5b778SMarcel Holtmann 	if (lmp_ssp_capable(hdev)) {
1729ebd1e33bSMarcel Holtmann 		debugfs_create_file("auto_accept_delay", 0644, hdev->debugfs,
1730ebd1e33bSMarcel Holtmann 				    hdev, &auto_accept_delay_fops);
17315afeac14SMarcel Holtmann 		debugfs_create_file("force_sc_support", 0644, hdev->debugfs,
17325afeac14SMarcel Holtmann 				    hdev, &force_sc_support_fops);
1733134c2a89SMarcel Holtmann 		debugfs_create_file("sc_only_mode", 0444, hdev->debugfs,
1734134c2a89SMarcel Holtmann 				    hdev, &sc_only_mode_fops);
173506f5b778SMarcel Holtmann 	}
1736ebd1e33bSMarcel Holtmann 
17372bfa3531SMarcel Holtmann 	if (lmp_sniff_capable(hdev)) {
17382bfa3531SMarcel Holtmann 		debugfs_create_file("idle_timeout", 0644, hdev->debugfs,
17392bfa3531SMarcel Holtmann 				    hdev, &idle_timeout_fops);
17402bfa3531SMarcel Holtmann 		debugfs_create_file("sniff_min_interval", 0644, hdev->debugfs,
17412bfa3531SMarcel Holtmann 				    hdev, &sniff_min_interval_fops);
17422bfa3531SMarcel Holtmann 		debugfs_create_file("sniff_max_interval", 0644, hdev->debugfs,
17432bfa3531SMarcel Holtmann 				    hdev, &sniff_max_interval_fops);
17442bfa3531SMarcel Holtmann 	}
17452bfa3531SMarcel Holtmann 
1746d0f729b8SMarcel Holtmann 	if (lmp_le_capable(hdev)) {
1747ac345813SMarcel Holtmann 		debugfs_create_file("identity", 0400, hdev->debugfs,
1748ac345813SMarcel Holtmann 				    hdev, &identity_fops);
1749ac345813SMarcel Holtmann 		debugfs_create_file("rpa_timeout", 0644, hdev->debugfs,
1750ac345813SMarcel Holtmann 				    hdev, &rpa_timeout_fops);
17517a4cd51dSMarcel Holtmann 		debugfs_create_file("random_address", 0444, hdev->debugfs,
17527a4cd51dSMarcel Holtmann 				    hdev, &random_address_fops);
1753e7b8fc92SMarcel Holtmann 		debugfs_create_file("static_address", 0444, hdev->debugfs,
1754e7b8fc92SMarcel Holtmann 				    hdev, &static_address_fops);
1755b32bba6cSMarcel Holtmann 
1756b32bba6cSMarcel Holtmann 		/* For controllers with a public address, provide a debug
1757b32bba6cSMarcel Holtmann 		 * option to force the usage of the configured static
1758b32bba6cSMarcel Holtmann 		 * address. By default the public address is used.
1759b32bba6cSMarcel Holtmann 		 */
1760b32bba6cSMarcel Holtmann 		if (bacmp(&hdev->bdaddr, BDADDR_ANY))
1761b32bba6cSMarcel Holtmann 			debugfs_create_file("force_static_address", 0644,
1762b32bba6cSMarcel Holtmann 					    hdev->debugfs, hdev,
1763b32bba6cSMarcel Holtmann 					    &force_static_address_fops);
1764b32bba6cSMarcel Holtmann 
1765b32bba6cSMarcel Holtmann 		debugfs_create_u8("white_list_size", 0444, hdev->debugfs,
1766b32bba6cSMarcel Holtmann 				  &hdev->le_white_list_size);
1767d2ab0ac1SMarcel Holtmann 		debugfs_create_file("white_list", 0444, hdev->debugfs, hdev,
1768d2ab0ac1SMarcel Holtmann 				    &white_list_fops);
17693698d704SMarcel Holtmann 		debugfs_create_file("identity_resolving_keys", 0400,
17703698d704SMarcel Holtmann 				    hdev->debugfs, hdev,
17713698d704SMarcel Holtmann 				    &identity_resolving_keys_fops);
17728f8625cdSMarcel Holtmann 		debugfs_create_file("long_term_keys", 0400, hdev->debugfs,
17738f8625cdSMarcel Holtmann 				    hdev, &long_term_keys_fops);
17744e70c7e7SMarcel Holtmann 		debugfs_create_file("conn_min_interval", 0644, hdev->debugfs,
17754e70c7e7SMarcel Holtmann 				    hdev, &conn_min_interval_fops);
17764e70c7e7SMarcel Holtmann 		debugfs_create_file("conn_max_interval", 0644, hdev->debugfs,
17774e70c7e7SMarcel Holtmann 				    hdev, &conn_max_interval_fops);
1778816a93d1SMarcel Holtmann 		debugfs_create_file("conn_latency", 0644, hdev->debugfs,
1779816a93d1SMarcel Holtmann 				    hdev, &conn_latency_fops);
1780f1649577SMarcel Holtmann 		debugfs_create_file("supervision_timeout", 0644, hdev->debugfs,
1781f1649577SMarcel Holtmann 				    hdev, &supervision_timeout_fops);
17823f959d46SMarcel Holtmann 		debugfs_create_file("adv_channel_map", 0644, hdev->debugfs,
17833f959d46SMarcel Holtmann 				    hdev, &adv_channel_map_fops);
17840b3c7d37SMarcel Holtmann 		debugfs_create_file("device_list", 0444, hdev->debugfs, hdev,
17850b3c7d37SMarcel Holtmann 				    &device_list_fops);
1786b9a7a61eSLukasz Rymanowski 		debugfs_create_u16("discov_interleaved_timeout", 0644,
1787b9a7a61eSLukasz Rymanowski 				   hdev->debugfs,
1788b9a7a61eSLukasz Rymanowski 				   &hdev->discov_interleaved_timeout);
1789d0f729b8SMarcel Holtmann 	}
1790e7b8fc92SMarcel Holtmann 
1791baf27f6eSMarcel Holtmann 	return 0;
17922177bab5SJohan Hedberg }
17932177bab5SJohan Hedberg 
179442c6b129SJohan Hedberg static void hci_scan_req(struct hci_request *req, unsigned long opt)
17951da177e4SLinus Torvalds {
17961da177e4SLinus Torvalds 	__u8 scan = opt;
17971da177e4SLinus Torvalds 
179842c6b129SJohan Hedberg 	BT_DBG("%s %x", req->hdev->name, scan);
17991da177e4SLinus Torvalds 
18001da177e4SLinus Torvalds 	/* Inquiry and Page scans */
180142c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
18021da177e4SLinus Torvalds }
18031da177e4SLinus Torvalds 
180442c6b129SJohan Hedberg static void hci_auth_req(struct hci_request *req, unsigned long opt)
18051da177e4SLinus Torvalds {
18061da177e4SLinus Torvalds 	__u8 auth = opt;
18071da177e4SLinus Torvalds 
180842c6b129SJohan Hedberg 	BT_DBG("%s %x", req->hdev->name, auth);
18091da177e4SLinus Torvalds 
18101da177e4SLinus Torvalds 	/* Authentication */
181142c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE, 1, &auth);
18121da177e4SLinus Torvalds }
18131da177e4SLinus Torvalds 
181442c6b129SJohan Hedberg static void hci_encrypt_req(struct hci_request *req, unsigned long opt)
18151da177e4SLinus Torvalds {
18161da177e4SLinus Torvalds 	__u8 encrypt = opt;
18171da177e4SLinus Torvalds 
181842c6b129SJohan Hedberg 	BT_DBG("%s %x", req->hdev->name, encrypt);
18191da177e4SLinus Torvalds 
1820e4e8e37cSMarcel Holtmann 	/* Encryption */
182142c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_WRITE_ENCRYPT_MODE, 1, &encrypt);
18221da177e4SLinus Torvalds }
18231da177e4SLinus Torvalds 
182442c6b129SJohan Hedberg static void hci_linkpol_req(struct hci_request *req, unsigned long opt)
1825e4e8e37cSMarcel Holtmann {
1826e4e8e37cSMarcel Holtmann 	__le16 policy = cpu_to_le16(opt);
1827e4e8e37cSMarcel Holtmann 
182842c6b129SJohan Hedberg 	BT_DBG("%s %x", req->hdev->name, policy);
1829e4e8e37cSMarcel Holtmann 
1830e4e8e37cSMarcel Holtmann 	/* Default link policy */
183142c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_WRITE_DEF_LINK_POLICY, 2, &policy);
1832e4e8e37cSMarcel Holtmann }
1833e4e8e37cSMarcel Holtmann 
18341da177e4SLinus Torvalds /* Get HCI device by index.
18351da177e4SLinus Torvalds  * Device is held on return. */
18361da177e4SLinus Torvalds struct hci_dev *hci_dev_get(int index)
18371da177e4SLinus Torvalds {
18388035ded4SLuiz Augusto von Dentz 	struct hci_dev *hdev = NULL, *d;
18391da177e4SLinus Torvalds 
18401da177e4SLinus Torvalds 	BT_DBG("%d", index);
18411da177e4SLinus Torvalds 
18421da177e4SLinus Torvalds 	if (index < 0)
18431da177e4SLinus Torvalds 		return NULL;
18441da177e4SLinus Torvalds 
18451da177e4SLinus Torvalds 	read_lock(&hci_dev_list_lock);
18468035ded4SLuiz Augusto von Dentz 	list_for_each_entry(d, &hci_dev_list, list) {
18471da177e4SLinus Torvalds 		if (d->id == index) {
18481da177e4SLinus Torvalds 			hdev = hci_dev_hold(d);
18491da177e4SLinus Torvalds 			break;
18501da177e4SLinus Torvalds 		}
18511da177e4SLinus Torvalds 	}
18521da177e4SLinus Torvalds 	read_unlock(&hci_dev_list_lock);
18531da177e4SLinus Torvalds 	return hdev;
18541da177e4SLinus Torvalds }
18551da177e4SLinus Torvalds 
18561da177e4SLinus Torvalds /* ---- Inquiry support ---- */
1857ff9ef578SJohan Hedberg 
185830dc78e1SJohan Hedberg bool hci_discovery_active(struct hci_dev *hdev)
185930dc78e1SJohan Hedberg {
186030dc78e1SJohan Hedberg 	struct discovery_state *discov = &hdev->discovery;
186130dc78e1SJohan Hedberg 
18626fbe195dSAndre Guedes 	switch (discov->state) {
1863343f935bSAndre Guedes 	case DISCOVERY_FINDING:
18646fbe195dSAndre Guedes 	case DISCOVERY_RESOLVING:
186530dc78e1SJohan Hedberg 		return true;
186630dc78e1SJohan Hedberg 
18676fbe195dSAndre Guedes 	default:
186830dc78e1SJohan Hedberg 		return false;
186930dc78e1SJohan Hedberg 	}
18706fbe195dSAndre Guedes }
187130dc78e1SJohan Hedberg 
1872ff9ef578SJohan Hedberg void hci_discovery_set_state(struct hci_dev *hdev, int state)
1873ff9ef578SJohan Hedberg {
1874ff9ef578SJohan Hedberg 	BT_DBG("%s state %u -> %u", hdev->name, hdev->discovery.state, state);
1875ff9ef578SJohan Hedberg 
1876ff9ef578SJohan Hedberg 	if (hdev->discovery.state == state)
1877ff9ef578SJohan Hedberg 		return;
1878ff9ef578SJohan Hedberg 
1879ff9ef578SJohan Hedberg 	switch (state) {
1880ff9ef578SJohan Hedberg 	case DISCOVERY_STOPPED:
1881c54c3860SAndre Guedes 		hci_update_background_scan(hdev);
1882c54c3860SAndre Guedes 
18837b99b659SAndre Guedes 		if (hdev->discovery.state != DISCOVERY_STARTING)
1884ff9ef578SJohan Hedberg 			mgmt_discovering(hdev, 0);
1885ff9ef578SJohan Hedberg 		break;
1886ff9ef578SJohan Hedberg 	case DISCOVERY_STARTING:
1887ff9ef578SJohan Hedberg 		break;
1888343f935bSAndre Guedes 	case DISCOVERY_FINDING:
1889ff9ef578SJohan Hedberg 		mgmt_discovering(hdev, 1);
1890ff9ef578SJohan Hedberg 		break;
189130dc78e1SJohan Hedberg 	case DISCOVERY_RESOLVING:
189230dc78e1SJohan Hedberg 		break;
1893ff9ef578SJohan Hedberg 	case DISCOVERY_STOPPING:
1894ff9ef578SJohan Hedberg 		break;
1895ff9ef578SJohan Hedberg 	}
1896ff9ef578SJohan Hedberg 
1897ff9ef578SJohan Hedberg 	hdev->discovery.state = state;
1898ff9ef578SJohan Hedberg }
1899ff9ef578SJohan Hedberg 
19001f9b9a5dSAndre Guedes void hci_inquiry_cache_flush(struct hci_dev *hdev)
19011da177e4SLinus Torvalds {
190230883512SJohan Hedberg 	struct discovery_state *cache = &hdev->discovery;
1903b57c1a56SJohan Hedberg 	struct inquiry_entry *p, *n;
19041da177e4SLinus Torvalds 
1905561aafbcSJohan Hedberg 	list_for_each_entry_safe(p, n, &cache->all, all) {
1906561aafbcSJohan Hedberg 		list_del(&p->all);
1907b57c1a56SJohan Hedberg 		kfree(p);
19081da177e4SLinus Torvalds 	}
1909561aafbcSJohan Hedberg 
1910561aafbcSJohan Hedberg 	INIT_LIST_HEAD(&cache->unknown);
1911561aafbcSJohan Hedberg 	INIT_LIST_HEAD(&cache->resolve);
19121da177e4SLinus Torvalds }
19131da177e4SLinus Torvalds 
1914a8c5fb1aSGustavo Padovan struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev,
1915a8c5fb1aSGustavo Padovan 					       bdaddr_t *bdaddr)
19161da177e4SLinus Torvalds {
191730883512SJohan Hedberg 	struct discovery_state *cache = &hdev->discovery;
19181da177e4SLinus Torvalds 	struct inquiry_entry *e;
19191da177e4SLinus Torvalds 
19206ed93dc6SAndrei Emeltchenko 	BT_DBG("cache %p, %pMR", cache, bdaddr);
19211da177e4SLinus Torvalds 
1922561aafbcSJohan Hedberg 	list_for_each_entry(e, &cache->all, all) {
19231da177e4SLinus Torvalds 		if (!bacmp(&e->data.bdaddr, bdaddr))
19241da177e4SLinus Torvalds 			return e;
19251da177e4SLinus Torvalds 	}
19261da177e4SLinus Torvalds 
1927b57c1a56SJohan Hedberg 	return NULL;
1928b57c1a56SJohan Hedberg }
1929b57c1a56SJohan Hedberg 
1930561aafbcSJohan Hedberg struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
1931561aafbcSJohan Hedberg 						       bdaddr_t *bdaddr)
1932561aafbcSJohan Hedberg {
193330883512SJohan Hedberg 	struct discovery_state *cache = &hdev->discovery;
1934561aafbcSJohan Hedberg 	struct inquiry_entry *e;
1935561aafbcSJohan Hedberg 
19366ed93dc6SAndrei Emeltchenko 	BT_DBG("cache %p, %pMR", cache, bdaddr);
1937561aafbcSJohan Hedberg 
1938561aafbcSJohan Hedberg 	list_for_each_entry(e, &cache->unknown, list) {
1939561aafbcSJohan Hedberg 		if (!bacmp(&e->data.bdaddr, bdaddr))
1940561aafbcSJohan Hedberg 			return e;
1941561aafbcSJohan Hedberg 	}
1942561aafbcSJohan Hedberg 
1943561aafbcSJohan Hedberg 	return NULL;
1944561aafbcSJohan Hedberg }
1945561aafbcSJohan Hedberg 
194630dc78e1SJohan Hedberg struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev,
194730dc78e1SJohan Hedberg 						       bdaddr_t *bdaddr,
194830dc78e1SJohan Hedberg 						       int state)
194930dc78e1SJohan Hedberg {
195030dc78e1SJohan Hedberg 	struct discovery_state *cache = &hdev->discovery;
195130dc78e1SJohan Hedberg 	struct inquiry_entry *e;
195230dc78e1SJohan Hedberg 
19536ed93dc6SAndrei Emeltchenko 	BT_DBG("cache %p bdaddr %pMR state %d", cache, bdaddr, state);
195430dc78e1SJohan Hedberg 
195530dc78e1SJohan Hedberg 	list_for_each_entry(e, &cache->resolve, list) {
195630dc78e1SJohan Hedberg 		if (!bacmp(bdaddr, BDADDR_ANY) && e->name_state == state)
195730dc78e1SJohan Hedberg 			return e;
195830dc78e1SJohan Hedberg 		if (!bacmp(&e->data.bdaddr, bdaddr))
195930dc78e1SJohan Hedberg 			return e;
196030dc78e1SJohan Hedberg 	}
196130dc78e1SJohan Hedberg 
196230dc78e1SJohan Hedberg 	return NULL;
196330dc78e1SJohan Hedberg }
196430dc78e1SJohan Hedberg 
1965a3d4e20aSJohan Hedberg void hci_inquiry_cache_update_resolve(struct hci_dev *hdev,
1966a3d4e20aSJohan Hedberg 				      struct inquiry_entry *ie)
1967a3d4e20aSJohan Hedberg {
1968a3d4e20aSJohan Hedberg 	struct discovery_state *cache = &hdev->discovery;
1969a3d4e20aSJohan Hedberg 	struct list_head *pos = &cache->resolve;
1970a3d4e20aSJohan Hedberg 	struct inquiry_entry *p;
1971a3d4e20aSJohan Hedberg 
1972a3d4e20aSJohan Hedberg 	list_del(&ie->list);
1973a3d4e20aSJohan Hedberg 
1974a3d4e20aSJohan Hedberg 	list_for_each_entry(p, &cache->resolve, list) {
1975a3d4e20aSJohan Hedberg 		if (p->name_state != NAME_PENDING &&
1976a3d4e20aSJohan Hedberg 		    abs(p->data.rssi) >= abs(ie->data.rssi))
1977a3d4e20aSJohan Hedberg 			break;
1978a3d4e20aSJohan Hedberg 		pos = &p->list;
1979a3d4e20aSJohan Hedberg 	}
1980a3d4e20aSJohan Hedberg 
1981a3d4e20aSJohan Hedberg 	list_add(&ie->list, pos);
1982a3d4e20aSJohan Hedberg }
1983a3d4e20aSJohan Hedberg 
1984af58925cSMarcel Holtmann u32 hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
1985af58925cSMarcel Holtmann 			     bool name_known)
19861da177e4SLinus Torvalds {
198730883512SJohan Hedberg 	struct discovery_state *cache = &hdev->discovery;
198870f23020SAndrei Emeltchenko 	struct inquiry_entry *ie;
1989af58925cSMarcel Holtmann 	u32 flags = 0;
19901da177e4SLinus Torvalds 
19916ed93dc6SAndrei Emeltchenko 	BT_DBG("cache %p, %pMR", cache, &data->bdaddr);
19921da177e4SLinus Torvalds 
19932b2fec4dSSzymon Janc 	hci_remove_remote_oob_data(hdev, &data->bdaddr);
19942b2fec4dSSzymon Janc 
1995af58925cSMarcel Holtmann 	if (!data->ssp_mode)
1996af58925cSMarcel Holtmann 		flags |= MGMT_DEV_FOUND_LEGACY_PAIRING;
1997388fc8faSJohan Hedberg 
199870f23020SAndrei Emeltchenko 	ie = hci_inquiry_cache_lookup(hdev, &data->bdaddr);
1999a3d4e20aSJohan Hedberg 	if (ie) {
2000af58925cSMarcel Holtmann 		if (!ie->data.ssp_mode)
2001af58925cSMarcel Holtmann 			flags |= MGMT_DEV_FOUND_LEGACY_PAIRING;
2002388fc8faSJohan Hedberg 
2003a3d4e20aSJohan Hedberg 		if (ie->name_state == NAME_NEEDED &&
2004a3d4e20aSJohan Hedberg 		    data->rssi != ie->data.rssi) {
2005a3d4e20aSJohan Hedberg 			ie->data.rssi = data->rssi;
2006a3d4e20aSJohan Hedberg 			hci_inquiry_cache_update_resolve(hdev, ie);
2007a3d4e20aSJohan Hedberg 		}
2008a3d4e20aSJohan Hedberg 
2009561aafbcSJohan Hedberg 		goto update;
2010a3d4e20aSJohan Hedberg 	}
2011561aafbcSJohan Hedberg 
20121da177e4SLinus Torvalds 	/* Entry not in the cache. Add new one. */
201370f23020SAndrei Emeltchenko 	ie = kzalloc(sizeof(struct inquiry_entry), GFP_ATOMIC);
2014af58925cSMarcel Holtmann 	if (!ie) {
2015af58925cSMarcel Holtmann 		flags |= MGMT_DEV_FOUND_CONFIRM_NAME;
2016af58925cSMarcel Holtmann 		goto done;
2017af58925cSMarcel Holtmann 	}
201870f23020SAndrei Emeltchenko 
2019561aafbcSJohan Hedberg 	list_add(&ie->all, &cache->all);
2020561aafbcSJohan Hedberg 
2021561aafbcSJohan Hedberg 	if (name_known) {
2022561aafbcSJohan Hedberg 		ie->name_state = NAME_KNOWN;
2023561aafbcSJohan Hedberg 	} else {
2024561aafbcSJohan Hedberg 		ie->name_state = NAME_NOT_KNOWN;
2025561aafbcSJohan Hedberg 		list_add(&ie->list, &cache->unknown);
2026561aafbcSJohan Hedberg 	}
2027561aafbcSJohan Hedberg 
2028561aafbcSJohan Hedberg update:
2029561aafbcSJohan Hedberg 	if (name_known && ie->name_state != NAME_KNOWN &&
2030561aafbcSJohan Hedberg 	    ie->name_state != NAME_PENDING) {
2031561aafbcSJohan Hedberg 		ie->name_state = NAME_KNOWN;
2032561aafbcSJohan Hedberg 		list_del(&ie->list);
20331da177e4SLinus Torvalds 	}
20341da177e4SLinus Torvalds 
203570f23020SAndrei Emeltchenko 	memcpy(&ie->data, data, sizeof(*data));
203670f23020SAndrei Emeltchenko 	ie->timestamp = jiffies;
20371da177e4SLinus Torvalds 	cache->timestamp = jiffies;
20383175405bSJohan Hedberg 
20393175405bSJohan Hedberg 	if (ie->name_state == NAME_NOT_KNOWN)
2040af58925cSMarcel Holtmann 		flags |= MGMT_DEV_FOUND_CONFIRM_NAME;
20413175405bSJohan Hedberg 
2042af58925cSMarcel Holtmann done:
2043af58925cSMarcel Holtmann 	return flags;
20441da177e4SLinus Torvalds }
20451da177e4SLinus Torvalds 
20461da177e4SLinus Torvalds static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf)
20471da177e4SLinus Torvalds {
204830883512SJohan Hedberg 	struct discovery_state *cache = &hdev->discovery;
20491da177e4SLinus Torvalds 	struct inquiry_info *info = (struct inquiry_info *) buf;
20501da177e4SLinus Torvalds 	struct inquiry_entry *e;
20511da177e4SLinus Torvalds 	int copied = 0;
20521da177e4SLinus Torvalds 
2053561aafbcSJohan Hedberg 	list_for_each_entry(e, &cache->all, all) {
20541da177e4SLinus Torvalds 		struct inquiry_data *data = &e->data;
2055b57c1a56SJohan Hedberg 
2056b57c1a56SJohan Hedberg 		if (copied >= num)
2057b57c1a56SJohan Hedberg 			break;
2058b57c1a56SJohan Hedberg 
20591da177e4SLinus Torvalds 		bacpy(&info->bdaddr, &data->bdaddr);
20601da177e4SLinus Torvalds 		info->pscan_rep_mode	= data->pscan_rep_mode;
20611da177e4SLinus Torvalds 		info->pscan_period_mode	= data->pscan_period_mode;
20621da177e4SLinus Torvalds 		info->pscan_mode	= data->pscan_mode;
20631da177e4SLinus Torvalds 		memcpy(info->dev_class, data->dev_class, 3);
20641da177e4SLinus Torvalds 		info->clock_offset	= data->clock_offset;
2065b57c1a56SJohan Hedberg 
20661da177e4SLinus Torvalds 		info++;
2067b57c1a56SJohan Hedberg 		copied++;
20681da177e4SLinus Torvalds 	}
20691da177e4SLinus Torvalds 
20701da177e4SLinus Torvalds 	BT_DBG("cache %p, copied %d", cache, copied);
20711da177e4SLinus Torvalds 	return copied;
20721da177e4SLinus Torvalds }
20731da177e4SLinus Torvalds 
207442c6b129SJohan Hedberg static void hci_inq_req(struct hci_request *req, unsigned long opt)
20751da177e4SLinus Torvalds {
20761da177e4SLinus Torvalds 	struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt;
207742c6b129SJohan Hedberg 	struct hci_dev *hdev = req->hdev;
20781da177e4SLinus Torvalds 	struct hci_cp_inquiry cp;
20791da177e4SLinus Torvalds 
20801da177e4SLinus Torvalds 	BT_DBG("%s", hdev->name);
20811da177e4SLinus Torvalds 
20821da177e4SLinus Torvalds 	if (test_bit(HCI_INQUIRY, &hdev->flags))
20831da177e4SLinus Torvalds 		return;
20841da177e4SLinus Torvalds 
20851da177e4SLinus Torvalds 	/* Start Inquiry */
20861da177e4SLinus Torvalds 	memcpy(&cp.lap, &ir->lap, 3);
20871da177e4SLinus Torvalds 	cp.length  = ir->length;
20881da177e4SLinus Torvalds 	cp.num_rsp = ir->num_rsp;
208942c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_INQUIRY, sizeof(cp), &cp);
20901da177e4SLinus Torvalds }
20911da177e4SLinus Torvalds 
20923e13fa1eSAndre Guedes static int wait_inquiry(void *word)
20933e13fa1eSAndre Guedes {
20943e13fa1eSAndre Guedes 	schedule();
20953e13fa1eSAndre Guedes 	return signal_pending(current);
20963e13fa1eSAndre Guedes }
20973e13fa1eSAndre Guedes 
20981da177e4SLinus Torvalds int hci_inquiry(void __user *arg)
20991da177e4SLinus Torvalds {
21001da177e4SLinus Torvalds 	__u8 __user *ptr = arg;
21011da177e4SLinus Torvalds 	struct hci_inquiry_req ir;
21021da177e4SLinus Torvalds 	struct hci_dev *hdev;
21031da177e4SLinus Torvalds 	int err = 0, do_inquiry = 0, max_rsp;
21041da177e4SLinus Torvalds 	long timeo;
21051da177e4SLinus Torvalds 	__u8 *buf;
21061da177e4SLinus Torvalds 
21071da177e4SLinus Torvalds 	if (copy_from_user(&ir, ptr, sizeof(ir)))
21081da177e4SLinus Torvalds 		return -EFAULT;
21091da177e4SLinus Torvalds 
21105a08ecceSAndrei Emeltchenko 	hdev = hci_dev_get(ir.dev_id);
21115a08ecceSAndrei Emeltchenko 	if (!hdev)
21121da177e4SLinus Torvalds 		return -ENODEV;
21131da177e4SLinus Torvalds 
21140736cfa8SMarcel Holtmann 	if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
21150736cfa8SMarcel Holtmann 		err = -EBUSY;
21160736cfa8SMarcel Holtmann 		goto done;
21170736cfa8SMarcel Holtmann 	}
21180736cfa8SMarcel Holtmann 
2119fee746b0SMarcel Holtmann 	if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) {
2120fee746b0SMarcel Holtmann 		err = -EOPNOTSUPP;
2121fee746b0SMarcel Holtmann 		goto done;
2122fee746b0SMarcel Holtmann 	}
2123fee746b0SMarcel Holtmann 
21245b69bef5SMarcel Holtmann 	if (hdev->dev_type != HCI_BREDR) {
21255b69bef5SMarcel Holtmann 		err = -EOPNOTSUPP;
21265b69bef5SMarcel Holtmann 		goto done;
21275b69bef5SMarcel Holtmann 	}
21285b69bef5SMarcel Holtmann 
212956f87901SJohan Hedberg 	if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
213056f87901SJohan Hedberg 		err = -EOPNOTSUPP;
213156f87901SJohan Hedberg 		goto done;
213256f87901SJohan Hedberg 	}
213356f87901SJohan Hedberg 
213409fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
21351da177e4SLinus Torvalds 	if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX ||
2136a8c5fb1aSGustavo Padovan 	    inquiry_cache_empty(hdev) || ir.flags & IREQ_CACHE_FLUSH) {
21371f9b9a5dSAndre Guedes 		hci_inquiry_cache_flush(hdev);
21381da177e4SLinus Torvalds 		do_inquiry = 1;
21391da177e4SLinus Torvalds 	}
214009fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
21411da177e4SLinus Torvalds 
214204837f64SMarcel Holtmann 	timeo = ir.length * msecs_to_jiffies(2000);
214370f23020SAndrei Emeltchenko 
214470f23020SAndrei Emeltchenko 	if (do_inquiry) {
214501178cd4SJohan Hedberg 		err = hci_req_sync(hdev, hci_inq_req, (unsigned long) &ir,
214601178cd4SJohan Hedberg 				   timeo);
214770f23020SAndrei Emeltchenko 		if (err < 0)
21481da177e4SLinus Torvalds 			goto done;
21493e13fa1eSAndre Guedes 
21503e13fa1eSAndre Guedes 		/* Wait until Inquiry procedure finishes (HCI_INQUIRY flag is
21513e13fa1eSAndre Guedes 		 * cleared). If it is interrupted by a signal, return -EINTR.
21523e13fa1eSAndre Guedes 		 */
21533e13fa1eSAndre Guedes 		if (wait_on_bit(&hdev->flags, HCI_INQUIRY, wait_inquiry,
21543e13fa1eSAndre Guedes 				TASK_INTERRUPTIBLE))
21553e13fa1eSAndre Guedes 			return -EINTR;
215670f23020SAndrei Emeltchenko 	}
21571da177e4SLinus Torvalds 
21588fc9ced3SGustavo Padovan 	/* for unlimited number of responses we will use buffer with
21598fc9ced3SGustavo Padovan 	 * 255 entries
21608fc9ced3SGustavo Padovan 	 */
21611da177e4SLinus Torvalds 	max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp;
21621da177e4SLinus Torvalds 
21631da177e4SLinus Torvalds 	/* cache_dump can't sleep. Therefore we allocate temp buffer and then
21641da177e4SLinus Torvalds 	 * copy it to the user space.
21651da177e4SLinus Torvalds 	 */
216670f23020SAndrei Emeltchenko 	buf = kmalloc(sizeof(struct inquiry_info) * max_rsp, GFP_KERNEL);
216770f23020SAndrei Emeltchenko 	if (!buf) {
21681da177e4SLinus Torvalds 		err = -ENOMEM;
21691da177e4SLinus Torvalds 		goto done;
21701da177e4SLinus Torvalds 	}
21711da177e4SLinus Torvalds 
217209fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
21731da177e4SLinus Torvalds 	ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf);
217409fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
21751da177e4SLinus Torvalds 
21761da177e4SLinus Torvalds 	BT_DBG("num_rsp %d", ir.num_rsp);
21771da177e4SLinus Torvalds 
21781da177e4SLinus Torvalds 	if (!copy_to_user(ptr, &ir, sizeof(ir))) {
21791da177e4SLinus Torvalds 		ptr += sizeof(ir);
21801da177e4SLinus Torvalds 		if (copy_to_user(ptr, buf, sizeof(struct inquiry_info) *
21811da177e4SLinus Torvalds 				 ir.num_rsp))
21821da177e4SLinus Torvalds 			err = -EFAULT;
21831da177e4SLinus Torvalds 	} else
21841da177e4SLinus Torvalds 		err = -EFAULT;
21851da177e4SLinus Torvalds 
21861da177e4SLinus Torvalds 	kfree(buf);
21871da177e4SLinus Torvalds 
21881da177e4SLinus Torvalds done:
21891da177e4SLinus Torvalds 	hci_dev_put(hdev);
21901da177e4SLinus Torvalds 	return err;
21911da177e4SLinus Torvalds }
21921da177e4SLinus Torvalds 
2193cbed0ca1SJohan Hedberg static int hci_dev_do_open(struct hci_dev *hdev)
21941da177e4SLinus Torvalds {
21951da177e4SLinus Torvalds 	int ret = 0;
21961da177e4SLinus Torvalds 
21971da177e4SLinus Torvalds 	BT_DBG("%s %p", hdev->name, hdev);
21981da177e4SLinus Torvalds 
21991da177e4SLinus Torvalds 	hci_req_lock(hdev);
22001da177e4SLinus Torvalds 
220194324962SJohan Hovold 	if (test_bit(HCI_UNREGISTER, &hdev->dev_flags)) {
220294324962SJohan Hovold 		ret = -ENODEV;
220394324962SJohan Hovold 		goto done;
220494324962SJohan Hovold 	}
220594324962SJohan Hovold 
2206a5c8f270SMarcel Holtmann 	if (!test_bit(HCI_SETUP, &hdev->dev_flags)) {
2207a5c8f270SMarcel Holtmann 		/* Check for rfkill but allow the HCI setup stage to
2208a5c8f270SMarcel Holtmann 		 * proceed (which in itself doesn't cause any RF activity).
2209bf543036SJohan Hedberg 		 */
2210a5c8f270SMarcel Holtmann 		if (test_bit(HCI_RFKILLED, &hdev->dev_flags)) {
2211611b30f7SMarcel Holtmann 			ret = -ERFKILL;
2212611b30f7SMarcel Holtmann 			goto done;
2213611b30f7SMarcel Holtmann 		}
2214611b30f7SMarcel Holtmann 
2215a5c8f270SMarcel Holtmann 		/* Check for valid public address or a configured static
2216a5c8f270SMarcel Holtmann 		 * random adddress, but let the HCI setup proceed to
2217a5c8f270SMarcel Holtmann 		 * be able to determine if there is a public address
2218a5c8f270SMarcel Holtmann 		 * or not.
2219a5c8f270SMarcel Holtmann 		 *
2220c6beca0eSMarcel Holtmann 		 * In case of user channel usage, it is not important
2221c6beca0eSMarcel Holtmann 		 * if a public address or static random address is
2222c6beca0eSMarcel Holtmann 		 * available.
2223c6beca0eSMarcel Holtmann 		 *
2224a5c8f270SMarcel Holtmann 		 * This check is only valid for BR/EDR controllers
2225a5c8f270SMarcel Holtmann 		 * since AMP controllers do not have an address.
2226a5c8f270SMarcel Holtmann 		 */
2227c6beca0eSMarcel Holtmann 		if (!test_bit(HCI_USER_CHANNEL, &hdev->dev_flags) &&
2228c6beca0eSMarcel Holtmann 		    hdev->dev_type == HCI_BREDR &&
2229a5c8f270SMarcel Holtmann 		    !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
2230a5c8f270SMarcel Holtmann 		    !bacmp(&hdev->static_addr, BDADDR_ANY)) {
2231a5c8f270SMarcel Holtmann 			ret = -EADDRNOTAVAIL;
2232a5c8f270SMarcel Holtmann 			goto done;
2233a5c8f270SMarcel Holtmann 		}
2234a5c8f270SMarcel Holtmann 	}
2235a5c8f270SMarcel Holtmann 
22361da177e4SLinus Torvalds 	if (test_bit(HCI_UP, &hdev->flags)) {
22371da177e4SLinus Torvalds 		ret = -EALREADY;
22381da177e4SLinus Torvalds 		goto done;
22391da177e4SLinus Torvalds 	}
22401da177e4SLinus Torvalds 
22411da177e4SLinus Torvalds 	if (hdev->open(hdev)) {
22421da177e4SLinus Torvalds 		ret = -EIO;
22431da177e4SLinus Torvalds 		goto done;
22441da177e4SLinus Torvalds 	}
22451da177e4SLinus Torvalds 
22461da177e4SLinus Torvalds 	atomic_set(&hdev->cmd_cnt, 1);
22471da177e4SLinus Torvalds 	set_bit(HCI_INIT, &hdev->flags);
2248f41c70c4SMarcel Holtmann 
2249f41c70c4SMarcel Holtmann 	if (hdev->setup && test_bit(HCI_SETUP, &hdev->dev_flags))
2250f41c70c4SMarcel Holtmann 		ret = hdev->setup(hdev);
2251f41c70c4SMarcel Holtmann 
225224c457e2SMarcel Holtmann 	/* If public address change is configured, ensure that the
225324c457e2SMarcel Holtmann 	 * address gets programmed. If the driver does not support
225424c457e2SMarcel Holtmann 	 * changing the public address, fail the power on procedure.
225524c457e2SMarcel Holtmann 	 */
225624c457e2SMarcel Holtmann 	if (!ret && bacmp(&hdev->public_addr, BDADDR_ANY)) {
225724c457e2SMarcel Holtmann 		if (hdev->set_bdaddr)
225824c457e2SMarcel Holtmann 			ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
225924c457e2SMarcel Holtmann 		else
226024c457e2SMarcel Holtmann 			ret = -EADDRNOTAVAIL;
226124c457e2SMarcel Holtmann 	}
226224c457e2SMarcel Holtmann 
2263f41c70c4SMarcel Holtmann 	if (!ret) {
2264fee746b0SMarcel Holtmann 		if (!test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks) &&
22650736cfa8SMarcel Holtmann 		    !test_bit(HCI_USER_CHANNEL, &hdev->dev_flags))
22662177bab5SJohan Hedberg 			ret = __hci_init(hdev);
22671da177e4SLinus Torvalds 	}
22681da177e4SLinus Torvalds 
2269f41c70c4SMarcel Holtmann 	clear_bit(HCI_INIT, &hdev->flags);
2270f41c70c4SMarcel Holtmann 
22711da177e4SLinus Torvalds 	if (!ret) {
22721da177e4SLinus Torvalds 		hci_dev_hold(hdev);
2273d6bfd59cSJohan Hedberg 		set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);
22741da177e4SLinus Torvalds 		set_bit(HCI_UP, &hdev->flags);
22751da177e4SLinus Torvalds 		hci_notify(hdev, HCI_DEV_UP);
2276bb4b2a9aSAndrei Emeltchenko 		if (!test_bit(HCI_SETUP, &hdev->dev_flags) &&
22770736cfa8SMarcel Holtmann 		    !test_bit(HCI_USER_CHANNEL, &hdev->dev_flags) &&
22781514b892SMarcel Holtmann 		    hdev->dev_type == HCI_BREDR) {
227909fd0de5SGustavo F. Padovan 			hci_dev_lock(hdev);
2280744cf19eSJohan Hedberg 			mgmt_powered(hdev, 1);
228109fd0de5SGustavo F. Padovan 			hci_dev_unlock(hdev);
228256e5cb86SJohan Hedberg 		}
22831da177e4SLinus Torvalds 	} else {
22841da177e4SLinus Torvalds 		/* Init failed, cleanup */
22853eff45eaSGustavo F. Padovan 		flush_work(&hdev->tx_work);
2286c347b765SGustavo F. Padovan 		flush_work(&hdev->cmd_work);
2287b78752ccSMarcel Holtmann 		flush_work(&hdev->rx_work);
22881da177e4SLinus Torvalds 
22891da177e4SLinus Torvalds 		skb_queue_purge(&hdev->cmd_q);
22901da177e4SLinus Torvalds 		skb_queue_purge(&hdev->rx_q);
22911da177e4SLinus Torvalds 
22921da177e4SLinus Torvalds 		if (hdev->flush)
22931da177e4SLinus Torvalds 			hdev->flush(hdev);
22941da177e4SLinus Torvalds 
22951da177e4SLinus Torvalds 		if (hdev->sent_cmd) {
22961da177e4SLinus Torvalds 			kfree_skb(hdev->sent_cmd);
22971da177e4SLinus Torvalds 			hdev->sent_cmd = NULL;
22981da177e4SLinus Torvalds 		}
22991da177e4SLinus Torvalds 
23001da177e4SLinus Torvalds 		hdev->close(hdev);
2301fee746b0SMarcel Holtmann 		hdev->flags &= BIT(HCI_RAW);
23021da177e4SLinus Torvalds 	}
23031da177e4SLinus Torvalds 
23041da177e4SLinus Torvalds done:
23051da177e4SLinus Torvalds 	hci_req_unlock(hdev);
23061da177e4SLinus Torvalds 	return ret;
23071da177e4SLinus Torvalds }
23081da177e4SLinus Torvalds 
2309cbed0ca1SJohan Hedberg /* ---- HCI ioctl helpers ---- */
2310cbed0ca1SJohan Hedberg 
2311cbed0ca1SJohan Hedberg int hci_dev_open(__u16 dev)
2312cbed0ca1SJohan Hedberg {
2313cbed0ca1SJohan Hedberg 	struct hci_dev *hdev;
2314cbed0ca1SJohan Hedberg 	int err;
2315cbed0ca1SJohan Hedberg 
2316cbed0ca1SJohan Hedberg 	hdev = hci_dev_get(dev);
2317cbed0ca1SJohan Hedberg 	if (!hdev)
2318cbed0ca1SJohan Hedberg 		return -ENODEV;
2319cbed0ca1SJohan Hedberg 
2320fee746b0SMarcel Holtmann 	/* Devices that are marked for raw-only usage can only be powered
2321fee746b0SMarcel Holtmann 	 * up as user channel. Trying to bring them up as normal devices
2322fee746b0SMarcel Holtmann 	 * will result into a failure. Only user channel operation is
2323fee746b0SMarcel Holtmann 	 * possible.
2324fee746b0SMarcel Holtmann 	 *
2325fee746b0SMarcel Holtmann 	 * When this function is called for a user channel, the flag
2326fee746b0SMarcel Holtmann 	 * HCI_USER_CHANNEL will be set first before attempting to
2327fee746b0SMarcel Holtmann 	 * open the device.
2328fee746b0SMarcel Holtmann 	 */
2329fee746b0SMarcel Holtmann 	if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks) &&
2330fee746b0SMarcel Holtmann 	    !test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
2331fee746b0SMarcel Holtmann 		err = -EOPNOTSUPP;
2332fee746b0SMarcel Holtmann 		goto done;
2333fee746b0SMarcel Holtmann 	}
2334fee746b0SMarcel Holtmann 
2335e1d08f40SJohan Hedberg 	/* We need to ensure that no other power on/off work is pending
2336e1d08f40SJohan Hedberg 	 * before proceeding to call hci_dev_do_open. This is
2337e1d08f40SJohan Hedberg 	 * particularly important if the setup procedure has not yet
2338e1d08f40SJohan Hedberg 	 * completed.
2339e1d08f40SJohan Hedberg 	 */
2340e1d08f40SJohan Hedberg 	if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
2341e1d08f40SJohan Hedberg 		cancel_delayed_work(&hdev->power_off);
2342e1d08f40SJohan Hedberg 
2343a5c8f270SMarcel Holtmann 	/* After this call it is guaranteed that the setup procedure
2344a5c8f270SMarcel Holtmann 	 * has finished. This means that error conditions like RFKILL
2345a5c8f270SMarcel Holtmann 	 * or no valid public or static random address apply.
2346a5c8f270SMarcel Holtmann 	 */
2347e1d08f40SJohan Hedberg 	flush_workqueue(hdev->req_workqueue);
2348e1d08f40SJohan Hedberg 
2349cbed0ca1SJohan Hedberg 	err = hci_dev_do_open(hdev);
2350cbed0ca1SJohan Hedberg 
2351fee746b0SMarcel Holtmann done:
2352cbed0ca1SJohan Hedberg 	hci_dev_put(hdev);
2353cbed0ca1SJohan Hedberg 	return err;
2354cbed0ca1SJohan Hedberg }
2355cbed0ca1SJohan Hedberg 
23561da177e4SLinus Torvalds static int hci_dev_do_close(struct hci_dev *hdev)
23571da177e4SLinus Torvalds {
23581da177e4SLinus Torvalds 	BT_DBG("%s %p", hdev->name, hdev);
23591da177e4SLinus Torvalds 
236078c04c0bSVinicius Costa Gomes 	cancel_delayed_work(&hdev->power_off);
236178c04c0bSVinicius Costa Gomes 
23621da177e4SLinus Torvalds 	hci_req_cancel(hdev, ENODEV);
23631da177e4SLinus Torvalds 	hci_req_lock(hdev);
23641da177e4SLinus Torvalds 
23651da177e4SLinus Torvalds 	if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
236665cc2b49SMarcel Holtmann 		cancel_delayed_work_sync(&hdev->cmd_timer);
23671da177e4SLinus Torvalds 		hci_req_unlock(hdev);
23681da177e4SLinus Torvalds 		return 0;
23691da177e4SLinus Torvalds 	}
23701da177e4SLinus Torvalds 
23713eff45eaSGustavo F. Padovan 	/* Flush RX and TX works */
23723eff45eaSGustavo F. Padovan 	flush_work(&hdev->tx_work);
2373b78752ccSMarcel Holtmann 	flush_work(&hdev->rx_work);
23741da177e4SLinus Torvalds 
237516ab91abSJohan Hedberg 	if (hdev->discov_timeout > 0) {
2376e0f9309fSJohan Hedberg 		cancel_delayed_work(&hdev->discov_off);
237716ab91abSJohan Hedberg 		hdev->discov_timeout = 0;
23785e5282bbSJohan Hedberg 		clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
2379310a3d48SMarcel Holtmann 		clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
238016ab91abSJohan Hedberg 	}
238116ab91abSJohan Hedberg 
2382a8b2d5c2SJohan Hedberg 	if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
23837d78525dSJohan Hedberg 		cancel_delayed_work(&hdev->service_cache);
23847d78525dSJohan Hedberg 
23857ba8b4beSAndre Guedes 	cancel_delayed_work_sync(&hdev->le_scan_disable);
23864518bb0fSJohan Hedberg 
23874518bb0fSJohan Hedberg 	if (test_bit(HCI_MGMT, &hdev->dev_flags))
2388d6bfd59cSJohan Hedberg 		cancel_delayed_work_sync(&hdev->rpa_expired);
23897ba8b4beSAndre Guedes 
239009fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
23911f9b9a5dSAndre Guedes 	hci_inquiry_cache_flush(hdev);
23921da177e4SLinus Torvalds 	hci_conn_hash_flush(hdev);
23936046dc3eSAndre Guedes 	hci_pend_le_conns_clear(hdev);
239409fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
23951da177e4SLinus Torvalds 
23961da177e4SLinus Torvalds 	hci_notify(hdev, HCI_DEV_DOWN);
23971da177e4SLinus Torvalds 
23981da177e4SLinus Torvalds 	if (hdev->flush)
23991da177e4SLinus Torvalds 		hdev->flush(hdev);
24001da177e4SLinus Torvalds 
24011da177e4SLinus Torvalds 	/* Reset device */
24021da177e4SLinus Torvalds 	skb_queue_purge(&hdev->cmd_q);
24031da177e4SLinus Torvalds 	atomic_set(&hdev->cmd_cnt, 1);
2404fee746b0SMarcel Holtmann 	if (!test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks) &&
24053a6afbd2SMarcel Holtmann 	    !test_bit(HCI_AUTO_OFF, &hdev->dev_flags) &&
2406a6c511c6SSzymon Janc 	    test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
24071da177e4SLinus Torvalds 		set_bit(HCI_INIT, &hdev->flags);
240801178cd4SJohan Hedberg 		__hci_req_sync(hdev, hci_reset_req, 0, HCI_CMD_TIMEOUT);
24091da177e4SLinus Torvalds 		clear_bit(HCI_INIT, &hdev->flags);
24101da177e4SLinus Torvalds 	}
24111da177e4SLinus Torvalds 
2412c347b765SGustavo F. Padovan 	/* flush cmd  work */
2413c347b765SGustavo F. Padovan 	flush_work(&hdev->cmd_work);
24141da177e4SLinus Torvalds 
24151da177e4SLinus Torvalds 	/* Drop queues */
24161da177e4SLinus Torvalds 	skb_queue_purge(&hdev->rx_q);
24171da177e4SLinus Torvalds 	skb_queue_purge(&hdev->cmd_q);
24181da177e4SLinus Torvalds 	skb_queue_purge(&hdev->raw_q);
24191da177e4SLinus Torvalds 
24201da177e4SLinus Torvalds 	/* Drop last sent command */
24211da177e4SLinus Torvalds 	if (hdev->sent_cmd) {
242265cc2b49SMarcel Holtmann 		cancel_delayed_work_sync(&hdev->cmd_timer);
24231da177e4SLinus Torvalds 		kfree_skb(hdev->sent_cmd);
24241da177e4SLinus Torvalds 		hdev->sent_cmd = NULL;
24251da177e4SLinus Torvalds 	}
24261da177e4SLinus Torvalds 
2427b6ddb638SJohan Hedberg 	kfree_skb(hdev->recv_evt);
2428b6ddb638SJohan Hedberg 	hdev->recv_evt = NULL;
2429b6ddb638SJohan Hedberg 
24301da177e4SLinus Torvalds 	/* After this point our queues are empty
24311da177e4SLinus Torvalds 	 * and no tasks are scheduled. */
24321da177e4SLinus Torvalds 	hdev->close(hdev);
24331da177e4SLinus Torvalds 
243435b973c9SJohan Hedberg 	/* Clear flags */
2435fee746b0SMarcel Holtmann 	hdev->flags &= BIT(HCI_RAW);
243635b973c9SJohan Hedberg 	hdev->dev_flags &= ~HCI_PERSISTENT_MASK;
243735b973c9SJohan Hedberg 
243893c311a0SMarcel Holtmann 	if (!test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) {
243993c311a0SMarcel Holtmann 		if (hdev->dev_type == HCI_BREDR) {
244009fd0de5SGustavo F. Padovan 			hci_dev_lock(hdev);
2441744cf19eSJohan Hedberg 			mgmt_powered(hdev, 0);
244209fd0de5SGustavo F. Padovan 			hci_dev_unlock(hdev);
24438ee56540SMarcel Holtmann 		}
244493c311a0SMarcel Holtmann 	}
24455add6af8SJohan Hedberg 
2446ced5c338SAndrei Emeltchenko 	/* Controller radio is available but is currently powered down */
2447536619e8SMarcel Holtmann 	hdev->amp_status = AMP_STATUS_POWERED_DOWN;
2448ced5c338SAndrei Emeltchenko 
2449e59fda8dSJohan Hedberg 	memset(hdev->eir, 0, sizeof(hdev->eir));
245009b3c3fbSJohan Hedberg 	memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
24517a4cd51dSMarcel Holtmann 	bacpy(&hdev->random_addr, BDADDR_ANY);
2452e59fda8dSJohan Hedberg 
24531da177e4SLinus Torvalds 	hci_req_unlock(hdev);
24541da177e4SLinus Torvalds 
24551da177e4SLinus Torvalds 	hci_dev_put(hdev);
24561da177e4SLinus Torvalds 	return 0;
24571da177e4SLinus Torvalds }
24581da177e4SLinus Torvalds 
24591da177e4SLinus Torvalds int hci_dev_close(__u16 dev)
24601da177e4SLinus Torvalds {
24611da177e4SLinus Torvalds 	struct hci_dev *hdev;
24621da177e4SLinus Torvalds 	int err;
24631da177e4SLinus Torvalds 
246470f23020SAndrei Emeltchenko 	hdev = hci_dev_get(dev);
246570f23020SAndrei Emeltchenko 	if (!hdev)
24661da177e4SLinus Torvalds 		return -ENODEV;
24678ee56540SMarcel Holtmann 
24680736cfa8SMarcel Holtmann 	if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
24690736cfa8SMarcel Holtmann 		err = -EBUSY;
24700736cfa8SMarcel Holtmann 		goto done;
24710736cfa8SMarcel Holtmann 	}
24720736cfa8SMarcel Holtmann 
24738ee56540SMarcel Holtmann 	if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
24748ee56540SMarcel Holtmann 		cancel_delayed_work(&hdev->power_off);
24758ee56540SMarcel Holtmann 
24761da177e4SLinus Torvalds 	err = hci_dev_do_close(hdev);
24778ee56540SMarcel Holtmann 
24780736cfa8SMarcel Holtmann done:
24791da177e4SLinus Torvalds 	hci_dev_put(hdev);
24801da177e4SLinus Torvalds 	return err;
24811da177e4SLinus Torvalds }
24821da177e4SLinus Torvalds 
24831da177e4SLinus Torvalds int hci_dev_reset(__u16 dev)
24841da177e4SLinus Torvalds {
24851da177e4SLinus Torvalds 	struct hci_dev *hdev;
24861da177e4SLinus Torvalds 	int ret = 0;
24871da177e4SLinus Torvalds 
248870f23020SAndrei Emeltchenko 	hdev = hci_dev_get(dev);
248970f23020SAndrei Emeltchenko 	if (!hdev)
24901da177e4SLinus Torvalds 		return -ENODEV;
24911da177e4SLinus Torvalds 
24921da177e4SLinus Torvalds 	hci_req_lock(hdev);
24931da177e4SLinus Torvalds 
2494808a049eSMarcel Holtmann 	if (!test_bit(HCI_UP, &hdev->flags)) {
2495808a049eSMarcel Holtmann 		ret = -ENETDOWN;
24961da177e4SLinus Torvalds 		goto done;
2497808a049eSMarcel Holtmann 	}
24981da177e4SLinus Torvalds 
24990736cfa8SMarcel Holtmann 	if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
25000736cfa8SMarcel Holtmann 		ret = -EBUSY;
25010736cfa8SMarcel Holtmann 		goto done;
25020736cfa8SMarcel Holtmann 	}
25030736cfa8SMarcel Holtmann 
2504fee746b0SMarcel Holtmann 	if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) {
2505fee746b0SMarcel Holtmann 		ret = -EOPNOTSUPP;
2506fee746b0SMarcel Holtmann 		goto done;
2507fee746b0SMarcel Holtmann 	}
2508fee746b0SMarcel Holtmann 
25091da177e4SLinus Torvalds 	/* Drop queues */
25101da177e4SLinus Torvalds 	skb_queue_purge(&hdev->rx_q);
25111da177e4SLinus Torvalds 	skb_queue_purge(&hdev->cmd_q);
25121da177e4SLinus Torvalds 
251309fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
25141f9b9a5dSAndre Guedes 	hci_inquiry_cache_flush(hdev);
25151da177e4SLinus Torvalds 	hci_conn_hash_flush(hdev);
251609fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
25171da177e4SLinus Torvalds 
25181da177e4SLinus Torvalds 	if (hdev->flush)
25191da177e4SLinus Torvalds 		hdev->flush(hdev);
25201da177e4SLinus Torvalds 
25211da177e4SLinus Torvalds 	atomic_set(&hdev->cmd_cnt, 1);
25226ed58ec5SVille Tervo 	hdev->acl_cnt = 0; hdev->sco_cnt = 0; hdev->le_cnt = 0;
25231da177e4SLinus Torvalds 
252401178cd4SJohan Hedberg 	ret = __hci_req_sync(hdev, hci_reset_req, 0, HCI_INIT_TIMEOUT);
25251da177e4SLinus Torvalds 
25261da177e4SLinus Torvalds done:
25271da177e4SLinus Torvalds 	hci_req_unlock(hdev);
25281da177e4SLinus Torvalds 	hci_dev_put(hdev);
25291da177e4SLinus Torvalds 	return ret;
25301da177e4SLinus Torvalds }
25311da177e4SLinus Torvalds 
25321da177e4SLinus Torvalds int hci_dev_reset_stat(__u16 dev)
25331da177e4SLinus Torvalds {
25341da177e4SLinus Torvalds 	struct hci_dev *hdev;
25351da177e4SLinus Torvalds 	int ret = 0;
25361da177e4SLinus Torvalds 
253770f23020SAndrei Emeltchenko 	hdev = hci_dev_get(dev);
253870f23020SAndrei Emeltchenko 	if (!hdev)
25391da177e4SLinus Torvalds 		return -ENODEV;
25401da177e4SLinus Torvalds 
25410736cfa8SMarcel Holtmann 	if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
25420736cfa8SMarcel Holtmann 		ret = -EBUSY;
25430736cfa8SMarcel Holtmann 		goto done;
25440736cfa8SMarcel Holtmann 	}
25450736cfa8SMarcel Holtmann 
2546fee746b0SMarcel Holtmann 	if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) {
2547fee746b0SMarcel Holtmann 		ret = -EOPNOTSUPP;
2548fee746b0SMarcel Holtmann 		goto done;
2549fee746b0SMarcel Holtmann 	}
2550fee746b0SMarcel Holtmann 
25511da177e4SLinus Torvalds 	memset(&hdev->stat, 0, sizeof(struct hci_dev_stats));
25521da177e4SLinus Torvalds 
25530736cfa8SMarcel Holtmann done:
25541da177e4SLinus Torvalds 	hci_dev_put(hdev);
25551da177e4SLinus Torvalds 	return ret;
25561da177e4SLinus Torvalds }
25571da177e4SLinus Torvalds 
25581da177e4SLinus Torvalds int hci_dev_cmd(unsigned int cmd, void __user *arg)
25591da177e4SLinus Torvalds {
25601da177e4SLinus Torvalds 	struct hci_dev *hdev;
25611da177e4SLinus Torvalds 	struct hci_dev_req dr;
25621da177e4SLinus Torvalds 	int err = 0;
25631da177e4SLinus Torvalds 
25641da177e4SLinus Torvalds 	if (copy_from_user(&dr, arg, sizeof(dr)))
25651da177e4SLinus Torvalds 		return -EFAULT;
25661da177e4SLinus Torvalds 
256770f23020SAndrei Emeltchenko 	hdev = hci_dev_get(dr.dev_id);
256870f23020SAndrei Emeltchenko 	if (!hdev)
25691da177e4SLinus Torvalds 		return -ENODEV;
25701da177e4SLinus Torvalds 
25710736cfa8SMarcel Holtmann 	if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
25720736cfa8SMarcel Holtmann 		err = -EBUSY;
25730736cfa8SMarcel Holtmann 		goto done;
25740736cfa8SMarcel Holtmann 	}
25750736cfa8SMarcel Holtmann 
2576fee746b0SMarcel Holtmann 	if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) {
2577fee746b0SMarcel Holtmann 		err = -EOPNOTSUPP;
2578fee746b0SMarcel Holtmann 		goto done;
2579fee746b0SMarcel Holtmann 	}
2580fee746b0SMarcel Holtmann 
25815b69bef5SMarcel Holtmann 	if (hdev->dev_type != HCI_BREDR) {
25825b69bef5SMarcel Holtmann 		err = -EOPNOTSUPP;
25835b69bef5SMarcel Holtmann 		goto done;
25845b69bef5SMarcel Holtmann 	}
25855b69bef5SMarcel Holtmann 
258656f87901SJohan Hedberg 	if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
258756f87901SJohan Hedberg 		err = -EOPNOTSUPP;
258856f87901SJohan Hedberg 		goto done;
258956f87901SJohan Hedberg 	}
259056f87901SJohan Hedberg 
25911da177e4SLinus Torvalds 	switch (cmd) {
25921da177e4SLinus Torvalds 	case HCISETAUTH:
259301178cd4SJohan Hedberg 		err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt,
25945f246e89SAndrei Emeltchenko 				   HCI_INIT_TIMEOUT);
25951da177e4SLinus Torvalds 		break;
25961da177e4SLinus Torvalds 
25971da177e4SLinus Torvalds 	case HCISETENCRYPT:
25981da177e4SLinus Torvalds 		if (!lmp_encrypt_capable(hdev)) {
25991da177e4SLinus Torvalds 			err = -EOPNOTSUPP;
26001da177e4SLinus Torvalds 			break;
26011da177e4SLinus Torvalds 		}
26021da177e4SLinus Torvalds 
26031da177e4SLinus Torvalds 		if (!test_bit(HCI_AUTH, &hdev->flags)) {
26041da177e4SLinus Torvalds 			/* Auth must be enabled first */
260501178cd4SJohan Hedberg 			err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt,
26065f246e89SAndrei Emeltchenko 					   HCI_INIT_TIMEOUT);
26071da177e4SLinus Torvalds 			if (err)
26081da177e4SLinus Torvalds 				break;
26091da177e4SLinus Torvalds 		}
26101da177e4SLinus Torvalds 
261101178cd4SJohan Hedberg 		err = hci_req_sync(hdev, hci_encrypt_req, dr.dev_opt,
26125f246e89SAndrei Emeltchenko 				   HCI_INIT_TIMEOUT);
26131da177e4SLinus Torvalds 		break;
26141da177e4SLinus Torvalds 
26151da177e4SLinus Torvalds 	case HCISETSCAN:
261601178cd4SJohan Hedberg 		err = hci_req_sync(hdev, hci_scan_req, dr.dev_opt,
26175f246e89SAndrei Emeltchenko 				   HCI_INIT_TIMEOUT);
26181da177e4SLinus Torvalds 		break;
26191da177e4SLinus Torvalds 
26201da177e4SLinus Torvalds 	case HCISETLINKPOL:
262101178cd4SJohan Hedberg 		err = hci_req_sync(hdev, hci_linkpol_req, dr.dev_opt,
26225f246e89SAndrei Emeltchenko 				   HCI_INIT_TIMEOUT);
26231da177e4SLinus Torvalds 		break;
26241da177e4SLinus Torvalds 
26251da177e4SLinus Torvalds 	case HCISETLINKMODE:
2626e4e8e37cSMarcel Holtmann 		hdev->link_mode = ((__u16) dr.dev_opt) &
2627e4e8e37cSMarcel Holtmann 					(HCI_LM_MASTER | HCI_LM_ACCEPT);
2628e4e8e37cSMarcel Holtmann 		break;
2629e4e8e37cSMarcel Holtmann 
2630e4e8e37cSMarcel Holtmann 	case HCISETPTYPE:
2631e4e8e37cSMarcel Holtmann 		hdev->pkt_type = (__u16) dr.dev_opt;
26321da177e4SLinus Torvalds 		break;
26331da177e4SLinus Torvalds 
26341da177e4SLinus Torvalds 	case HCISETACLMTU:
26351da177e4SLinus Torvalds 		hdev->acl_mtu  = *((__u16 *) &dr.dev_opt + 1);
26361da177e4SLinus Torvalds 		hdev->acl_pkts = *((__u16 *) &dr.dev_opt + 0);
26371da177e4SLinus Torvalds 		break;
26381da177e4SLinus Torvalds 
26391da177e4SLinus Torvalds 	case HCISETSCOMTU:
26401da177e4SLinus Torvalds 		hdev->sco_mtu  = *((__u16 *) &dr.dev_opt + 1);
26411da177e4SLinus Torvalds 		hdev->sco_pkts = *((__u16 *) &dr.dev_opt + 0);
26421da177e4SLinus Torvalds 		break;
26431da177e4SLinus Torvalds 
26441da177e4SLinus Torvalds 	default:
26451da177e4SLinus Torvalds 		err = -EINVAL;
26461da177e4SLinus Torvalds 		break;
26471da177e4SLinus Torvalds 	}
2648e4e8e37cSMarcel Holtmann 
26490736cfa8SMarcel Holtmann done:
26501da177e4SLinus Torvalds 	hci_dev_put(hdev);
26511da177e4SLinus Torvalds 	return err;
26521da177e4SLinus Torvalds }
26531da177e4SLinus Torvalds 
26541da177e4SLinus Torvalds int hci_get_dev_list(void __user *arg)
26551da177e4SLinus Torvalds {
26568035ded4SLuiz Augusto von Dentz 	struct hci_dev *hdev;
26571da177e4SLinus Torvalds 	struct hci_dev_list_req *dl;
26581da177e4SLinus Torvalds 	struct hci_dev_req *dr;
26591da177e4SLinus Torvalds 	int n = 0, size, err;
26601da177e4SLinus Torvalds 	__u16 dev_num;
26611da177e4SLinus Torvalds 
26621da177e4SLinus Torvalds 	if (get_user(dev_num, (__u16 __user *) arg))
26631da177e4SLinus Torvalds 		return -EFAULT;
26641da177e4SLinus Torvalds 
26651da177e4SLinus Torvalds 	if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr))
26661da177e4SLinus Torvalds 		return -EINVAL;
26671da177e4SLinus Torvalds 
26681da177e4SLinus Torvalds 	size = sizeof(*dl) + dev_num * sizeof(*dr);
26691da177e4SLinus Torvalds 
267070f23020SAndrei Emeltchenko 	dl = kzalloc(size, GFP_KERNEL);
267170f23020SAndrei Emeltchenko 	if (!dl)
26721da177e4SLinus Torvalds 		return -ENOMEM;
26731da177e4SLinus Torvalds 
26741da177e4SLinus Torvalds 	dr = dl->dev_req;
26751da177e4SLinus Torvalds 
2676f20d09d5SGustavo F. Padovan 	read_lock(&hci_dev_list_lock);
26778035ded4SLuiz Augusto von Dentz 	list_for_each_entry(hdev, &hci_dev_list, list) {
2678a8b2d5c2SJohan Hedberg 		if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
2679e0f9309fSJohan Hedberg 			cancel_delayed_work(&hdev->power_off);
2680c542a06cSJohan Hedberg 
2681a8b2d5c2SJohan Hedberg 		if (!test_bit(HCI_MGMT, &hdev->dev_flags))
2682a8b2d5c2SJohan Hedberg 			set_bit(HCI_PAIRABLE, &hdev->dev_flags);
2683c542a06cSJohan Hedberg 
26841da177e4SLinus Torvalds 		(dr + n)->dev_id  = hdev->id;
26851da177e4SLinus Torvalds 		(dr + n)->dev_opt = hdev->flags;
2686c542a06cSJohan Hedberg 
26871da177e4SLinus Torvalds 		if (++n >= dev_num)
26881da177e4SLinus Torvalds 			break;
26891da177e4SLinus Torvalds 	}
2690f20d09d5SGustavo F. Padovan 	read_unlock(&hci_dev_list_lock);
26911da177e4SLinus Torvalds 
26921da177e4SLinus Torvalds 	dl->dev_num = n;
26931da177e4SLinus Torvalds 	size = sizeof(*dl) + n * sizeof(*dr);
26941da177e4SLinus Torvalds 
26951da177e4SLinus Torvalds 	err = copy_to_user(arg, dl, size);
26961da177e4SLinus Torvalds 	kfree(dl);
26971da177e4SLinus Torvalds 
26981da177e4SLinus Torvalds 	return err ? -EFAULT : 0;
26991da177e4SLinus Torvalds }
27001da177e4SLinus Torvalds 
27011da177e4SLinus Torvalds int hci_get_dev_info(void __user *arg)
27021da177e4SLinus Torvalds {
27031da177e4SLinus Torvalds 	struct hci_dev *hdev;
27041da177e4SLinus Torvalds 	struct hci_dev_info di;
27051da177e4SLinus Torvalds 	int err = 0;
27061da177e4SLinus Torvalds 
27071da177e4SLinus Torvalds 	if (copy_from_user(&di, arg, sizeof(di)))
27081da177e4SLinus Torvalds 		return -EFAULT;
27091da177e4SLinus Torvalds 
271070f23020SAndrei Emeltchenko 	hdev = hci_dev_get(di.dev_id);
271170f23020SAndrei Emeltchenko 	if (!hdev)
27121da177e4SLinus Torvalds 		return -ENODEV;
27131da177e4SLinus Torvalds 
2714a8b2d5c2SJohan Hedberg 	if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
27153243553fSJohan Hedberg 		cancel_delayed_work_sync(&hdev->power_off);
2716ab81cbf9SJohan Hedberg 
2717a8b2d5c2SJohan Hedberg 	if (!test_bit(HCI_MGMT, &hdev->dev_flags))
2718a8b2d5c2SJohan Hedberg 		set_bit(HCI_PAIRABLE, &hdev->dev_flags);
2719c542a06cSJohan Hedberg 
27201da177e4SLinus Torvalds 	strcpy(di.name, hdev->name);
27211da177e4SLinus Torvalds 	di.bdaddr   = hdev->bdaddr;
272260f2a3edSMarcel Holtmann 	di.type     = (hdev->bus & 0x0f) | ((hdev->dev_type & 0x03) << 4);
27231da177e4SLinus Torvalds 	di.flags    = hdev->flags;
27241da177e4SLinus Torvalds 	di.pkt_type = hdev->pkt_type;
2725572c7f84SJohan Hedberg 	if (lmp_bredr_capable(hdev)) {
27261da177e4SLinus Torvalds 		di.acl_mtu  = hdev->acl_mtu;
27271da177e4SLinus Torvalds 		di.acl_pkts = hdev->acl_pkts;
27281da177e4SLinus Torvalds 		di.sco_mtu  = hdev->sco_mtu;
27291da177e4SLinus Torvalds 		di.sco_pkts = hdev->sco_pkts;
2730572c7f84SJohan Hedberg 	} else {
2731572c7f84SJohan Hedberg 		di.acl_mtu  = hdev->le_mtu;
2732572c7f84SJohan Hedberg 		di.acl_pkts = hdev->le_pkts;
2733572c7f84SJohan Hedberg 		di.sco_mtu  = 0;
2734572c7f84SJohan Hedberg 		di.sco_pkts = 0;
2735572c7f84SJohan Hedberg 	}
27361da177e4SLinus Torvalds 	di.link_policy = hdev->link_policy;
27371da177e4SLinus Torvalds 	di.link_mode   = hdev->link_mode;
27381da177e4SLinus Torvalds 
27391da177e4SLinus Torvalds 	memcpy(&di.stat, &hdev->stat, sizeof(di.stat));
27401da177e4SLinus Torvalds 	memcpy(&di.features, &hdev->features, sizeof(di.features));
27411da177e4SLinus Torvalds 
27421da177e4SLinus Torvalds 	if (copy_to_user(arg, &di, sizeof(di)))
27431da177e4SLinus Torvalds 		err = -EFAULT;
27441da177e4SLinus Torvalds 
27451da177e4SLinus Torvalds 	hci_dev_put(hdev);
27461da177e4SLinus Torvalds 
27471da177e4SLinus Torvalds 	return err;
27481da177e4SLinus Torvalds }
27491da177e4SLinus Torvalds 
27501da177e4SLinus Torvalds /* ---- Interface to HCI drivers ---- */
27511da177e4SLinus Torvalds 
2752611b30f7SMarcel Holtmann static int hci_rfkill_set_block(void *data, bool blocked)
2753611b30f7SMarcel Holtmann {
2754611b30f7SMarcel Holtmann 	struct hci_dev *hdev = data;
2755611b30f7SMarcel Holtmann 
2756611b30f7SMarcel Holtmann 	BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked);
2757611b30f7SMarcel Holtmann 
27580736cfa8SMarcel Holtmann 	if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags))
27590736cfa8SMarcel Holtmann 		return -EBUSY;
27600736cfa8SMarcel Holtmann 
27615e130367SJohan Hedberg 	if (blocked) {
27625e130367SJohan Hedberg 		set_bit(HCI_RFKILLED, &hdev->dev_flags);
2763bf543036SJohan Hedberg 		if (!test_bit(HCI_SETUP, &hdev->dev_flags))
2764611b30f7SMarcel Holtmann 			hci_dev_do_close(hdev);
27655e130367SJohan Hedberg 	} else {
27665e130367SJohan Hedberg 		clear_bit(HCI_RFKILLED, &hdev->dev_flags);
27675e130367SJohan Hedberg 	}
2768611b30f7SMarcel Holtmann 
2769611b30f7SMarcel Holtmann 	return 0;
2770611b30f7SMarcel Holtmann }
2771611b30f7SMarcel Holtmann 
2772611b30f7SMarcel Holtmann static const struct rfkill_ops hci_rfkill_ops = {
2773611b30f7SMarcel Holtmann 	.set_block = hci_rfkill_set_block,
2774611b30f7SMarcel Holtmann };
2775611b30f7SMarcel Holtmann 
2776ab81cbf9SJohan Hedberg static void hci_power_on(struct work_struct *work)
2777ab81cbf9SJohan Hedberg {
2778ab81cbf9SJohan Hedberg 	struct hci_dev *hdev = container_of(work, struct hci_dev, power_on);
277996570ffcSJohan Hedberg 	int err;
2780ab81cbf9SJohan Hedberg 
2781ab81cbf9SJohan Hedberg 	BT_DBG("%s", hdev->name);
2782ab81cbf9SJohan Hedberg 
2783cbed0ca1SJohan Hedberg 	err = hci_dev_do_open(hdev);
278496570ffcSJohan Hedberg 	if (err < 0) {
278596570ffcSJohan Hedberg 		mgmt_set_powered_failed(hdev, err);
2786ab81cbf9SJohan Hedberg 		return;
278796570ffcSJohan Hedberg 	}
2788ab81cbf9SJohan Hedberg 
2789a5c8f270SMarcel Holtmann 	/* During the HCI setup phase, a few error conditions are
2790a5c8f270SMarcel Holtmann 	 * ignored and they need to be checked now. If they are still
2791a5c8f270SMarcel Holtmann 	 * valid, it is important to turn the device back off.
2792a5c8f270SMarcel Holtmann 	 */
2793a5c8f270SMarcel Holtmann 	if (test_bit(HCI_RFKILLED, &hdev->dev_flags) ||
2794a5c8f270SMarcel Holtmann 	    (hdev->dev_type == HCI_BREDR &&
2795a5c8f270SMarcel Holtmann 	     !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
2796a5c8f270SMarcel Holtmann 	     !bacmp(&hdev->static_addr, BDADDR_ANY))) {
2797bf543036SJohan Hedberg 		clear_bit(HCI_AUTO_OFF, &hdev->dev_flags);
2798bf543036SJohan Hedberg 		hci_dev_do_close(hdev);
2799bf543036SJohan Hedberg 	} else if (test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) {
280019202573SJohan Hedberg 		queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
280119202573SJohan Hedberg 				   HCI_AUTO_OFF_TIMEOUT);
2802bf543036SJohan Hedberg 	}
2803ab81cbf9SJohan Hedberg 
2804fee746b0SMarcel Holtmann 	if (test_and_clear_bit(HCI_SETUP, &hdev->dev_flags)) {
2805fee746b0SMarcel Holtmann 		if (!test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
2806744cf19eSJohan Hedberg 			mgmt_index_added(hdev);
2807ab81cbf9SJohan Hedberg 	}
2808fee746b0SMarcel Holtmann }
2809ab81cbf9SJohan Hedberg 
2810ab81cbf9SJohan Hedberg static void hci_power_off(struct work_struct *work)
2811ab81cbf9SJohan Hedberg {
28123243553fSJohan Hedberg 	struct hci_dev *hdev = container_of(work, struct hci_dev,
28133243553fSJohan Hedberg 					    power_off.work);
2814ab81cbf9SJohan Hedberg 
2815ab81cbf9SJohan Hedberg 	BT_DBG("%s", hdev->name);
2816ab81cbf9SJohan Hedberg 
28178ee56540SMarcel Holtmann 	hci_dev_do_close(hdev);
2818ab81cbf9SJohan Hedberg }
2819ab81cbf9SJohan Hedberg 
282016ab91abSJohan Hedberg static void hci_discov_off(struct work_struct *work)
282116ab91abSJohan Hedberg {
282216ab91abSJohan Hedberg 	struct hci_dev *hdev;
282316ab91abSJohan Hedberg 
282416ab91abSJohan Hedberg 	hdev = container_of(work, struct hci_dev, discov_off.work);
282516ab91abSJohan Hedberg 
282616ab91abSJohan Hedberg 	BT_DBG("%s", hdev->name);
282716ab91abSJohan Hedberg 
2828d1967ff8SMarcel Holtmann 	mgmt_discoverable_timeout(hdev);
282916ab91abSJohan Hedberg }
283016ab91abSJohan Hedberg 
283135f7498aSJohan Hedberg void hci_uuids_clear(struct hci_dev *hdev)
28322aeb9a1aSJohan Hedberg {
28334821002cSJohan Hedberg 	struct bt_uuid *uuid, *tmp;
28342aeb9a1aSJohan Hedberg 
28354821002cSJohan Hedberg 	list_for_each_entry_safe(uuid, tmp, &hdev->uuids, list) {
28364821002cSJohan Hedberg 		list_del(&uuid->list);
28372aeb9a1aSJohan Hedberg 		kfree(uuid);
28382aeb9a1aSJohan Hedberg 	}
28392aeb9a1aSJohan Hedberg }
28402aeb9a1aSJohan Hedberg 
284135f7498aSJohan Hedberg void hci_link_keys_clear(struct hci_dev *hdev)
284255ed8ca1SJohan Hedberg {
284355ed8ca1SJohan Hedberg 	struct list_head *p, *n;
284455ed8ca1SJohan Hedberg 
284555ed8ca1SJohan Hedberg 	list_for_each_safe(p, n, &hdev->link_keys) {
284655ed8ca1SJohan Hedberg 		struct link_key *key;
284755ed8ca1SJohan Hedberg 
284855ed8ca1SJohan Hedberg 		key = list_entry(p, struct link_key, list);
284955ed8ca1SJohan Hedberg 
285055ed8ca1SJohan Hedberg 		list_del(p);
285155ed8ca1SJohan Hedberg 		kfree(key);
285255ed8ca1SJohan Hedberg 	}
285355ed8ca1SJohan Hedberg }
285455ed8ca1SJohan Hedberg 
285535f7498aSJohan Hedberg void hci_smp_ltks_clear(struct hci_dev *hdev)
2856b899efafSVinicius Costa Gomes {
2857b899efafSVinicius Costa Gomes 	struct smp_ltk *k, *tmp;
2858b899efafSVinicius Costa Gomes 
2859b899efafSVinicius Costa Gomes 	list_for_each_entry_safe(k, tmp, &hdev->long_term_keys, list) {
2860b899efafSVinicius Costa Gomes 		list_del(&k->list);
2861b899efafSVinicius Costa Gomes 		kfree(k);
2862b899efafSVinicius Costa Gomes 	}
2863b899efafSVinicius Costa Gomes }
2864b899efafSVinicius Costa Gomes 
2865970c4e46SJohan Hedberg void hci_smp_irks_clear(struct hci_dev *hdev)
2866970c4e46SJohan Hedberg {
2867970c4e46SJohan Hedberg 	struct smp_irk *k, *tmp;
2868970c4e46SJohan Hedberg 
2869970c4e46SJohan Hedberg 	list_for_each_entry_safe(k, tmp, &hdev->identity_resolving_keys, list) {
2870970c4e46SJohan Hedberg 		list_del(&k->list);
2871970c4e46SJohan Hedberg 		kfree(k);
2872970c4e46SJohan Hedberg 	}
2873970c4e46SJohan Hedberg }
2874970c4e46SJohan Hedberg 
287555ed8ca1SJohan Hedberg struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr)
287655ed8ca1SJohan Hedberg {
287755ed8ca1SJohan Hedberg 	struct link_key *k;
287855ed8ca1SJohan Hedberg 
28798035ded4SLuiz Augusto von Dentz 	list_for_each_entry(k, &hdev->link_keys, list)
288055ed8ca1SJohan Hedberg 		if (bacmp(bdaddr, &k->bdaddr) == 0)
288155ed8ca1SJohan Hedberg 			return k;
288255ed8ca1SJohan Hedberg 
288355ed8ca1SJohan Hedberg 	return NULL;
288455ed8ca1SJohan Hedberg }
288555ed8ca1SJohan Hedberg 
2886745c0ce3SVishal Agarwal static bool hci_persistent_key(struct hci_dev *hdev, struct hci_conn *conn,
2887d25e28abSJohan Hedberg 			       u8 key_type, u8 old_key_type)
2888d25e28abSJohan Hedberg {
2889d25e28abSJohan Hedberg 	/* Legacy key */
2890d25e28abSJohan Hedberg 	if (key_type < 0x03)
2891745c0ce3SVishal Agarwal 		return true;
2892d25e28abSJohan Hedberg 
2893d25e28abSJohan Hedberg 	/* Debug keys are insecure so don't store them persistently */
2894d25e28abSJohan Hedberg 	if (key_type == HCI_LK_DEBUG_COMBINATION)
2895745c0ce3SVishal Agarwal 		return false;
2896d25e28abSJohan Hedberg 
2897d25e28abSJohan Hedberg 	/* Changed combination key and there's no previous one */
2898d25e28abSJohan Hedberg 	if (key_type == HCI_LK_CHANGED_COMBINATION && old_key_type == 0xff)
2899745c0ce3SVishal Agarwal 		return false;
2900d25e28abSJohan Hedberg 
2901d25e28abSJohan Hedberg 	/* Security mode 3 case */
2902d25e28abSJohan Hedberg 	if (!conn)
2903745c0ce3SVishal Agarwal 		return true;
2904d25e28abSJohan Hedberg 
2905d25e28abSJohan Hedberg 	/* Neither local nor remote side had no-bonding as requirement */
2906d25e28abSJohan Hedberg 	if (conn->auth_type > 0x01 && conn->remote_auth > 0x01)
2907745c0ce3SVishal Agarwal 		return true;
2908d25e28abSJohan Hedberg 
2909d25e28abSJohan Hedberg 	/* Local side had dedicated bonding as requirement */
2910d25e28abSJohan Hedberg 	if (conn->auth_type == 0x02 || conn->auth_type == 0x03)
2911745c0ce3SVishal Agarwal 		return true;
2912d25e28abSJohan Hedberg 
2913d25e28abSJohan Hedberg 	/* Remote side had dedicated bonding as requirement */
2914d25e28abSJohan Hedberg 	if (conn->remote_auth == 0x02 || conn->remote_auth == 0x03)
2915745c0ce3SVishal Agarwal 		return true;
2916d25e28abSJohan Hedberg 
2917d25e28abSJohan Hedberg 	/* If none of the above criteria match, then don't store the key
2918d25e28abSJohan Hedberg 	 * persistently */
2919745c0ce3SVishal Agarwal 	return false;
2920d25e28abSJohan Hedberg }
2921d25e28abSJohan Hedberg 
292298a0b845SJohan Hedberg static bool ltk_type_master(u8 type)
292398a0b845SJohan Hedberg {
2924d97c9fb0SJohan Hedberg 	return (type == SMP_LTK);
292598a0b845SJohan Hedberg }
292698a0b845SJohan Hedberg 
2927fe39c7b2SMarcel Holtmann struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, __le64 rand,
292898a0b845SJohan Hedberg 			     bool master)
292975d262c2SVinicius Costa Gomes {
2930c9839a11SVinicius Costa Gomes 	struct smp_ltk *k;
293175d262c2SVinicius Costa Gomes 
2932c9839a11SVinicius Costa Gomes 	list_for_each_entry(k, &hdev->long_term_keys, list) {
2933fe39c7b2SMarcel Holtmann 		if (k->ediv != ediv || k->rand != rand)
293475d262c2SVinicius Costa Gomes 			continue;
293575d262c2SVinicius Costa Gomes 
293698a0b845SJohan Hedberg 		if (ltk_type_master(k->type) != master)
293798a0b845SJohan Hedberg 			continue;
293898a0b845SJohan Hedberg 
293975d262c2SVinicius Costa Gomes 		return k;
294075d262c2SVinicius Costa Gomes 	}
294175d262c2SVinicius Costa Gomes 
294275d262c2SVinicius Costa Gomes 	return NULL;
294375d262c2SVinicius Costa Gomes }
294475d262c2SVinicius Costa Gomes 
2945c9839a11SVinicius Costa Gomes struct smp_ltk *hci_find_ltk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr,
294698a0b845SJohan Hedberg 				     u8 addr_type, bool master)
294775d262c2SVinicius Costa Gomes {
2948c9839a11SVinicius Costa Gomes 	struct smp_ltk *k;
294975d262c2SVinicius Costa Gomes 
2950c9839a11SVinicius Costa Gomes 	list_for_each_entry(k, &hdev->long_term_keys, list)
2951c9839a11SVinicius Costa Gomes 		if (addr_type == k->bdaddr_type &&
295298a0b845SJohan Hedberg 		    bacmp(bdaddr, &k->bdaddr) == 0 &&
295398a0b845SJohan Hedberg 		    ltk_type_master(k->type) == master)
295475d262c2SVinicius Costa Gomes 			return k;
295575d262c2SVinicius Costa Gomes 
295675d262c2SVinicius Costa Gomes 	return NULL;
295775d262c2SVinicius Costa Gomes }
295875d262c2SVinicius Costa Gomes 
2959970c4e46SJohan Hedberg struct smp_irk *hci_find_irk_by_rpa(struct hci_dev *hdev, bdaddr_t *rpa)
2960970c4e46SJohan Hedberg {
2961970c4e46SJohan Hedberg 	struct smp_irk *irk;
2962970c4e46SJohan Hedberg 
2963970c4e46SJohan Hedberg 	list_for_each_entry(irk, &hdev->identity_resolving_keys, list) {
2964970c4e46SJohan Hedberg 		if (!bacmp(&irk->rpa, rpa))
2965970c4e46SJohan Hedberg 			return irk;
2966970c4e46SJohan Hedberg 	}
2967970c4e46SJohan Hedberg 
2968970c4e46SJohan Hedberg 	list_for_each_entry(irk, &hdev->identity_resolving_keys, list) {
2969970c4e46SJohan Hedberg 		if (smp_irk_matches(hdev->tfm_aes, irk->val, rpa)) {
2970970c4e46SJohan Hedberg 			bacpy(&irk->rpa, rpa);
2971970c4e46SJohan Hedberg 			return irk;
2972970c4e46SJohan Hedberg 		}
2973970c4e46SJohan Hedberg 	}
2974970c4e46SJohan Hedberg 
2975970c4e46SJohan Hedberg 	return NULL;
2976970c4e46SJohan Hedberg }
2977970c4e46SJohan Hedberg 
2978970c4e46SJohan Hedberg struct smp_irk *hci_find_irk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr,
2979970c4e46SJohan Hedberg 				     u8 addr_type)
2980970c4e46SJohan Hedberg {
2981970c4e46SJohan Hedberg 	struct smp_irk *irk;
2982970c4e46SJohan Hedberg 
29836cfc9988SJohan Hedberg 	/* Identity Address must be public or static random */
29846cfc9988SJohan Hedberg 	if (addr_type == ADDR_LE_DEV_RANDOM && (bdaddr->b[5] & 0xc0) != 0xc0)
29856cfc9988SJohan Hedberg 		return NULL;
29866cfc9988SJohan Hedberg 
2987970c4e46SJohan Hedberg 	list_for_each_entry(irk, &hdev->identity_resolving_keys, list) {
2988970c4e46SJohan Hedberg 		if (addr_type == irk->addr_type &&
2989970c4e46SJohan Hedberg 		    bacmp(bdaddr, &irk->bdaddr) == 0)
2990970c4e46SJohan Hedberg 			return irk;
2991970c4e46SJohan Hedberg 	}
2992970c4e46SJohan Hedberg 
2993970c4e46SJohan Hedberg 	return NULL;
2994970c4e46SJohan Hedberg }
2995970c4e46SJohan Hedberg 
2996567fa2aaSJohan Hedberg struct link_key *hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn,
29977652ff6aSJohan Hedberg 				  bdaddr_t *bdaddr, u8 *val, u8 type,
29987652ff6aSJohan Hedberg 				  u8 pin_len, bool *persistent)
299955ed8ca1SJohan Hedberg {
300055ed8ca1SJohan Hedberg 	struct link_key *key, *old_key;
3001745c0ce3SVishal Agarwal 	u8 old_key_type;
300255ed8ca1SJohan Hedberg 
300355ed8ca1SJohan Hedberg 	old_key = hci_find_link_key(hdev, bdaddr);
300455ed8ca1SJohan Hedberg 	if (old_key) {
300555ed8ca1SJohan Hedberg 		old_key_type = old_key->type;
300655ed8ca1SJohan Hedberg 		key = old_key;
300755ed8ca1SJohan Hedberg 	} else {
300812adcf3aSJohan Hedberg 		old_key_type = conn ? conn->key_type : 0xff;
30090a14ab41SJohan Hedberg 		key = kzalloc(sizeof(*key), GFP_KERNEL);
301055ed8ca1SJohan Hedberg 		if (!key)
3011567fa2aaSJohan Hedberg 			return NULL;
301255ed8ca1SJohan Hedberg 		list_add(&key->list, &hdev->link_keys);
301355ed8ca1SJohan Hedberg 	}
301455ed8ca1SJohan Hedberg 
30156ed93dc6SAndrei Emeltchenko 	BT_DBG("%s key for %pMR type %u", hdev->name, bdaddr, type);
301655ed8ca1SJohan Hedberg 
3017d25e28abSJohan Hedberg 	/* Some buggy controller combinations generate a changed
3018d25e28abSJohan Hedberg 	 * combination key for legacy pairing even when there's no
3019d25e28abSJohan Hedberg 	 * previous key */
3020d25e28abSJohan Hedberg 	if (type == HCI_LK_CHANGED_COMBINATION &&
3021a8c5fb1aSGustavo Padovan 	    (!conn || conn->remote_auth == 0xff) && old_key_type == 0xff) {
3022d25e28abSJohan Hedberg 		type = HCI_LK_COMBINATION;
3023655fe6ecSJohan Hedberg 		if (conn)
3024655fe6ecSJohan Hedberg 			conn->key_type = type;
3025655fe6ecSJohan Hedberg 	}
3026d25e28abSJohan Hedberg 
302755ed8ca1SJohan Hedberg 	bacpy(&key->bdaddr, bdaddr);
30289b3b4460SAndrei Emeltchenko 	memcpy(key->val, val, HCI_LINK_KEY_SIZE);
302955ed8ca1SJohan Hedberg 	key->pin_len = pin_len;
303055ed8ca1SJohan Hedberg 
3031b6020ba0SWaldemar Rymarkiewicz 	if (type == HCI_LK_CHANGED_COMBINATION)
303255ed8ca1SJohan Hedberg 		key->type = old_key_type;
30334748fed2SJohan Hedberg 	else
30344748fed2SJohan Hedberg 		key->type = type;
30354748fed2SJohan Hedberg 
30367652ff6aSJohan Hedberg 	if (persistent)
30377652ff6aSJohan Hedberg 		*persistent = hci_persistent_key(hdev, conn, type,
30387652ff6aSJohan Hedberg 						 old_key_type);
303955ed8ca1SJohan Hedberg 
3040567fa2aaSJohan Hedberg 	return key;
304155ed8ca1SJohan Hedberg }
304255ed8ca1SJohan Hedberg 
3043ca9142b8SJohan Hedberg struct smp_ltk *hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr,
304435d70271SJohan Hedberg 			    u8 addr_type, u8 type, u8 authenticated,
3045fe39c7b2SMarcel Holtmann 			    u8 tk[16], u8 enc_size, __le16 ediv, __le64 rand)
304675d262c2SVinicius Costa Gomes {
3047c9839a11SVinicius Costa Gomes 	struct smp_ltk *key, *old_key;
304898a0b845SJohan Hedberg 	bool master = ltk_type_master(type);
304975d262c2SVinicius Costa Gomes 
305098a0b845SJohan Hedberg 	old_key = hci_find_ltk_by_addr(hdev, bdaddr, addr_type, master);
3051c9839a11SVinicius Costa Gomes 	if (old_key)
305275d262c2SVinicius Costa Gomes 		key = old_key;
3053c9839a11SVinicius Costa Gomes 	else {
30540a14ab41SJohan Hedberg 		key = kzalloc(sizeof(*key), GFP_KERNEL);
305575d262c2SVinicius Costa Gomes 		if (!key)
3056ca9142b8SJohan Hedberg 			return NULL;
3057c9839a11SVinicius Costa Gomes 		list_add(&key->list, &hdev->long_term_keys);
305875d262c2SVinicius Costa Gomes 	}
305975d262c2SVinicius Costa Gomes 
306075d262c2SVinicius Costa Gomes 	bacpy(&key->bdaddr, bdaddr);
3061c9839a11SVinicius Costa Gomes 	key->bdaddr_type = addr_type;
3062c9839a11SVinicius Costa Gomes 	memcpy(key->val, tk, sizeof(key->val));
3063c9839a11SVinicius Costa Gomes 	key->authenticated = authenticated;
3064c9839a11SVinicius Costa Gomes 	key->ediv = ediv;
3065fe39c7b2SMarcel Holtmann 	key->rand = rand;
3066c9839a11SVinicius Costa Gomes 	key->enc_size = enc_size;
3067c9839a11SVinicius Costa Gomes 	key->type = type;
306875d262c2SVinicius Costa Gomes 
3069ca9142b8SJohan Hedberg 	return key;
307075d262c2SVinicius Costa Gomes }
307175d262c2SVinicius Costa Gomes 
3072ca9142b8SJohan Hedberg struct smp_irk *hci_add_irk(struct hci_dev *hdev, bdaddr_t *bdaddr,
3073ca9142b8SJohan Hedberg 			    u8 addr_type, u8 val[16], bdaddr_t *rpa)
3074970c4e46SJohan Hedberg {
3075970c4e46SJohan Hedberg 	struct smp_irk *irk;
3076970c4e46SJohan Hedberg 
3077970c4e46SJohan Hedberg 	irk = hci_find_irk_by_addr(hdev, bdaddr, addr_type);
3078970c4e46SJohan Hedberg 	if (!irk) {
3079970c4e46SJohan Hedberg 		irk = kzalloc(sizeof(*irk), GFP_KERNEL);
3080970c4e46SJohan Hedberg 		if (!irk)
3081ca9142b8SJohan Hedberg 			return NULL;
3082970c4e46SJohan Hedberg 
3083970c4e46SJohan Hedberg 		bacpy(&irk->bdaddr, bdaddr);
3084970c4e46SJohan Hedberg 		irk->addr_type = addr_type;
3085970c4e46SJohan Hedberg 
3086970c4e46SJohan Hedberg 		list_add(&irk->list, &hdev->identity_resolving_keys);
3087970c4e46SJohan Hedberg 	}
3088970c4e46SJohan Hedberg 
3089970c4e46SJohan Hedberg 	memcpy(irk->val, val, 16);
3090970c4e46SJohan Hedberg 	bacpy(&irk->rpa, rpa);
3091970c4e46SJohan Hedberg 
3092ca9142b8SJohan Hedberg 	return irk;
3093970c4e46SJohan Hedberg }
3094970c4e46SJohan Hedberg 
309555ed8ca1SJohan Hedberg int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr)
309655ed8ca1SJohan Hedberg {
309755ed8ca1SJohan Hedberg 	struct link_key *key;
309855ed8ca1SJohan Hedberg 
309955ed8ca1SJohan Hedberg 	key = hci_find_link_key(hdev, bdaddr);
310055ed8ca1SJohan Hedberg 	if (!key)
310155ed8ca1SJohan Hedberg 		return -ENOENT;
310255ed8ca1SJohan Hedberg 
31036ed93dc6SAndrei Emeltchenko 	BT_DBG("%s removing %pMR", hdev->name, bdaddr);
310455ed8ca1SJohan Hedberg 
310555ed8ca1SJohan Hedberg 	list_del(&key->list);
310655ed8ca1SJohan Hedberg 	kfree(key);
310755ed8ca1SJohan Hedberg 
310855ed8ca1SJohan Hedberg 	return 0;
310955ed8ca1SJohan Hedberg }
311055ed8ca1SJohan Hedberg 
3111e0b2b27eSJohan Hedberg int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 bdaddr_type)
3112b899efafSVinicius Costa Gomes {
3113b899efafSVinicius Costa Gomes 	struct smp_ltk *k, *tmp;
3114c51ffa0bSJohan Hedberg 	int removed = 0;
3115b899efafSVinicius Costa Gomes 
3116b899efafSVinicius Costa Gomes 	list_for_each_entry_safe(k, tmp, &hdev->long_term_keys, list) {
3117e0b2b27eSJohan Hedberg 		if (bacmp(bdaddr, &k->bdaddr) || k->bdaddr_type != bdaddr_type)
3118b899efafSVinicius Costa Gomes 			continue;
3119b899efafSVinicius Costa Gomes 
31206ed93dc6SAndrei Emeltchenko 		BT_DBG("%s removing %pMR", hdev->name, bdaddr);
3121b899efafSVinicius Costa Gomes 
3122b899efafSVinicius Costa Gomes 		list_del(&k->list);
3123b899efafSVinicius Costa Gomes 		kfree(k);
3124c51ffa0bSJohan Hedberg 		removed++;
3125b899efafSVinicius Costa Gomes 	}
3126b899efafSVinicius Costa Gomes 
3127c51ffa0bSJohan Hedberg 	return removed ? 0 : -ENOENT;
3128b899efafSVinicius Costa Gomes }
3129b899efafSVinicius Costa Gomes 
3130a7ec7338SJohan Hedberg void hci_remove_irk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type)
3131a7ec7338SJohan Hedberg {
3132a7ec7338SJohan Hedberg 	struct smp_irk *k, *tmp;
3133a7ec7338SJohan Hedberg 
3134668b7b19SJohan Hedberg 	list_for_each_entry_safe(k, tmp, &hdev->identity_resolving_keys, list) {
3135a7ec7338SJohan Hedberg 		if (bacmp(bdaddr, &k->bdaddr) || k->addr_type != addr_type)
3136a7ec7338SJohan Hedberg 			continue;
3137a7ec7338SJohan Hedberg 
3138a7ec7338SJohan Hedberg 		BT_DBG("%s removing %pMR", hdev->name, bdaddr);
3139a7ec7338SJohan Hedberg 
3140a7ec7338SJohan Hedberg 		list_del(&k->list);
3141a7ec7338SJohan Hedberg 		kfree(k);
3142a7ec7338SJohan Hedberg 	}
3143a7ec7338SJohan Hedberg }
3144a7ec7338SJohan Hedberg 
31456bd32326SVille Tervo /* HCI command timer function */
314665cc2b49SMarcel Holtmann static void hci_cmd_timeout(struct work_struct *work)
31476bd32326SVille Tervo {
314865cc2b49SMarcel Holtmann 	struct hci_dev *hdev = container_of(work, struct hci_dev,
314965cc2b49SMarcel Holtmann 					    cmd_timer.work);
31506bd32326SVille Tervo 
3151bda4f23aSAndrei Emeltchenko 	if (hdev->sent_cmd) {
3152bda4f23aSAndrei Emeltchenko 		struct hci_command_hdr *sent = (void *) hdev->sent_cmd->data;
3153bda4f23aSAndrei Emeltchenko 		u16 opcode = __le16_to_cpu(sent->opcode);
3154bda4f23aSAndrei Emeltchenko 
3155bda4f23aSAndrei Emeltchenko 		BT_ERR("%s command 0x%4.4x tx timeout", hdev->name, opcode);
3156bda4f23aSAndrei Emeltchenko 	} else {
31576bd32326SVille Tervo 		BT_ERR("%s command tx timeout", hdev->name);
3158bda4f23aSAndrei Emeltchenko 	}
3159bda4f23aSAndrei Emeltchenko 
31606bd32326SVille Tervo 	atomic_set(&hdev->cmd_cnt, 1);
3161c347b765SGustavo F. Padovan 	queue_work(hdev->workqueue, &hdev->cmd_work);
31626bd32326SVille Tervo }
31636bd32326SVille Tervo 
31642763eda6SSzymon Janc struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev,
31652763eda6SSzymon Janc 					  bdaddr_t *bdaddr)
31662763eda6SSzymon Janc {
31672763eda6SSzymon Janc 	struct oob_data *data;
31682763eda6SSzymon Janc 
31692763eda6SSzymon Janc 	list_for_each_entry(data, &hdev->remote_oob_data, list)
31702763eda6SSzymon Janc 		if (bacmp(bdaddr, &data->bdaddr) == 0)
31712763eda6SSzymon Janc 			return data;
31722763eda6SSzymon Janc 
31732763eda6SSzymon Janc 	return NULL;
31742763eda6SSzymon Janc }
31752763eda6SSzymon Janc 
31762763eda6SSzymon Janc int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr)
31772763eda6SSzymon Janc {
31782763eda6SSzymon Janc 	struct oob_data *data;
31792763eda6SSzymon Janc 
31802763eda6SSzymon Janc 	data = hci_find_remote_oob_data(hdev, bdaddr);
31812763eda6SSzymon Janc 	if (!data)
31822763eda6SSzymon Janc 		return -ENOENT;
31832763eda6SSzymon Janc 
31846ed93dc6SAndrei Emeltchenko 	BT_DBG("%s removing %pMR", hdev->name, bdaddr);
31852763eda6SSzymon Janc 
31862763eda6SSzymon Janc 	list_del(&data->list);
31872763eda6SSzymon Janc 	kfree(data);
31882763eda6SSzymon Janc 
31892763eda6SSzymon Janc 	return 0;
31902763eda6SSzymon Janc }
31912763eda6SSzymon Janc 
319235f7498aSJohan Hedberg void hci_remote_oob_data_clear(struct hci_dev *hdev)
31932763eda6SSzymon Janc {
31942763eda6SSzymon Janc 	struct oob_data *data, *n;
31952763eda6SSzymon Janc 
31962763eda6SSzymon Janc 	list_for_each_entry_safe(data, n, &hdev->remote_oob_data, list) {
31972763eda6SSzymon Janc 		list_del(&data->list);
31982763eda6SSzymon Janc 		kfree(data);
31992763eda6SSzymon Janc 	}
32002763eda6SSzymon Janc }
32012763eda6SSzymon Janc 
32020798872eSMarcel Holtmann int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr,
32030798872eSMarcel Holtmann 			    u8 *hash, u8 *randomizer)
32042763eda6SSzymon Janc {
32052763eda6SSzymon Janc 	struct oob_data *data;
32062763eda6SSzymon Janc 
32072763eda6SSzymon Janc 	data = hci_find_remote_oob_data(hdev, bdaddr);
32082763eda6SSzymon Janc 	if (!data) {
32090a14ab41SJohan Hedberg 		data = kmalloc(sizeof(*data), GFP_KERNEL);
32102763eda6SSzymon Janc 		if (!data)
32112763eda6SSzymon Janc 			return -ENOMEM;
32122763eda6SSzymon Janc 
32132763eda6SSzymon Janc 		bacpy(&data->bdaddr, bdaddr);
32142763eda6SSzymon Janc 		list_add(&data->list, &hdev->remote_oob_data);
32152763eda6SSzymon Janc 	}
32162763eda6SSzymon Janc 
3217519ca9d0SMarcel Holtmann 	memcpy(data->hash192, hash, sizeof(data->hash192));
3218519ca9d0SMarcel Holtmann 	memcpy(data->randomizer192, randomizer, sizeof(data->randomizer192));
32192763eda6SSzymon Janc 
32200798872eSMarcel Holtmann 	memset(data->hash256, 0, sizeof(data->hash256));
32210798872eSMarcel Holtmann 	memset(data->randomizer256, 0, sizeof(data->randomizer256));
32220798872eSMarcel Holtmann 
32230798872eSMarcel Holtmann 	BT_DBG("%s for %pMR", hdev->name, bdaddr);
32240798872eSMarcel Holtmann 
32250798872eSMarcel Holtmann 	return 0;
32260798872eSMarcel Holtmann }
32270798872eSMarcel Holtmann 
32280798872eSMarcel Holtmann int hci_add_remote_oob_ext_data(struct hci_dev *hdev, bdaddr_t *bdaddr,
32290798872eSMarcel Holtmann 				u8 *hash192, u8 *randomizer192,
32300798872eSMarcel Holtmann 				u8 *hash256, u8 *randomizer256)
32310798872eSMarcel Holtmann {
32320798872eSMarcel Holtmann 	struct oob_data *data;
32330798872eSMarcel Holtmann 
32340798872eSMarcel Holtmann 	data = hci_find_remote_oob_data(hdev, bdaddr);
32350798872eSMarcel Holtmann 	if (!data) {
32360a14ab41SJohan Hedberg 		data = kmalloc(sizeof(*data), GFP_KERNEL);
32370798872eSMarcel Holtmann 		if (!data)
32380798872eSMarcel Holtmann 			return -ENOMEM;
32390798872eSMarcel Holtmann 
32400798872eSMarcel Holtmann 		bacpy(&data->bdaddr, bdaddr);
32410798872eSMarcel Holtmann 		list_add(&data->list, &hdev->remote_oob_data);
32420798872eSMarcel Holtmann 	}
32430798872eSMarcel Holtmann 
32440798872eSMarcel Holtmann 	memcpy(data->hash192, hash192, sizeof(data->hash192));
32450798872eSMarcel Holtmann 	memcpy(data->randomizer192, randomizer192, sizeof(data->randomizer192));
32460798872eSMarcel Holtmann 
32470798872eSMarcel Holtmann 	memcpy(data->hash256, hash256, sizeof(data->hash256));
32480798872eSMarcel Holtmann 	memcpy(data->randomizer256, randomizer256, sizeof(data->randomizer256));
32490798872eSMarcel Holtmann 
32506ed93dc6SAndrei Emeltchenko 	BT_DBG("%s for %pMR", hdev->name, bdaddr);
32512763eda6SSzymon Janc 
32522763eda6SSzymon Janc 	return 0;
32532763eda6SSzymon Janc }
32542763eda6SSzymon Janc 
3255b9ee0a78SMarcel Holtmann struct bdaddr_list *hci_blacklist_lookup(struct hci_dev *hdev,
3256b9ee0a78SMarcel Holtmann 					 bdaddr_t *bdaddr, u8 type)
3257b2a66aadSAntti Julku {
3258b2a66aadSAntti Julku 	struct bdaddr_list *b;
3259b2a66aadSAntti Julku 
3260b9ee0a78SMarcel Holtmann 	list_for_each_entry(b, &hdev->blacklist, list) {
3261b9ee0a78SMarcel Holtmann 		if (!bacmp(&b->bdaddr, bdaddr) && b->bdaddr_type == type)
3262b2a66aadSAntti Julku 			return b;
3263b9ee0a78SMarcel Holtmann 	}
3264b2a66aadSAntti Julku 
3265b2a66aadSAntti Julku 	return NULL;
3266b2a66aadSAntti Julku }
3267b2a66aadSAntti Julku 
3268c9507490SMarcel Holtmann static void hci_blacklist_clear(struct hci_dev *hdev)
3269b2a66aadSAntti Julku {
3270b2a66aadSAntti Julku 	struct list_head *p, *n;
3271b2a66aadSAntti Julku 
3272b2a66aadSAntti Julku 	list_for_each_safe(p, n, &hdev->blacklist) {
3273b9ee0a78SMarcel Holtmann 		struct bdaddr_list *b = list_entry(p, struct bdaddr_list, list);
3274b2a66aadSAntti Julku 
3275b2a66aadSAntti Julku 		list_del(p);
3276b2a66aadSAntti Julku 		kfree(b);
3277b2a66aadSAntti Julku 	}
3278b2a66aadSAntti Julku }
3279b2a66aadSAntti Julku 
328088c1fe4bSJohan Hedberg int hci_blacklist_add(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
3281b2a66aadSAntti Julku {
3282b2a66aadSAntti Julku 	struct bdaddr_list *entry;
3283b2a66aadSAntti Julku 
3284b9ee0a78SMarcel Holtmann 	if (!bacmp(bdaddr, BDADDR_ANY))
3285b2a66aadSAntti Julku 		return -EBADF;
3286b2a66aadSAntti Julku 
3287b9ee0a78SMarcel Holtmann 	if (hci_blacklist_lookup(hdev, bdaddr, type))
32885e762444SAntti Julku 		return -EEXIST;
3289b2a66aadSAntti Julku 
3290b2a66aadSAntti Julku 	entry = kzalloc(sizeof(struct bdaddr_list), GFP_KERNEL);
32915e762444SAntti Julku 	if (!entry)
32925e762444SAntti Julku 		return -ENOMEM;
3293b2a66aadSAntti Julku 
3294b2a66aadSAntti Julku 	bacpy(&entry->bdaddr, bdaddr);
3295b9ee0a78SMarcel Holtmann 	entry->bdaddr_type = type;
3296b2a66aadSAntti Julku 
3297b2a66aadSAntti Julku 	list_add(&entry->list, &hdev->blacklist);
3298b2a66aadSAntti Julku 
32992a8357f2SJohan Hedberg 	return 0;
3300b2a66aadSAntti Julku }
3301b2a66aadSAntti Julku 
330288c1fe4bSJohan Hedberg int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
3303b2a66aadSAntti Julku {
3304b2a66aadSAntti Julku 	struct bdaddr_list *entry;
3305b2a66aadSAntti Julku 
330635f7498aSJohan Hedberg 	if (!bacmp(bdaddr, BDADDR_ANY)) {
330735f7498aSJohan Hedberg 		hci_blacklist_clear(hdev);
330835f7498aSJohan Hedberg 		return 0;
330935f7498aSJohan Hedberg 	}
3310b2a66aadSAntti Julku 
3311b9ee0a78SMarcel Holtmann 	entry = hci_blacklist_lookup(hdev, bdaddr, type);
33121ec918ceSSzymon Janc 	if (!entry)
33135e762444SAntti Julku 		return -ENOENT;
3314b2a66aadSAntti Julku 
3315b2a66aadSAntti Julku 	list_del(&entry->list);
3316b2a66aadSAntti Julku 	kfree(entry);
3317b2a66aadSAntti Julku 
33182a8357f2SJohan Hedberg 	return 0;
3319b2a66aadSAntti Julku }
3320b2a66aadSAntti Julku 
3321d2ab0ac1SMarcel Holtmann struct bdaddr_list *hci_white_list_lookup(struct hci_dev *hdev,
3322d2ab0ac1SMarcel Holtmann 					  bdaddr_t *bdaddr, u8 type)
3323d2ab0ac1SMarcel Holtmann {
3324d2ab0ac1SMarcel Holtmann 	struct bdaddr_list *b;
3325d2ab0ac1SMarcel Holtmann 
3326d2ab0ac1SMarcel Holtmann 	list_for_each_entry(b, &hdev->le_white_list, list) {
3327d2ab0ac1SMarcel Holtmann 		if (!bacmp(&b->bdaddr, bdaddr) && b->bdaddr_type == type)
3328d2ab0ac1SMarcel Holtmann 			return b;
3329d2ab0ac1SMarcel Holtmann 	}
3330d2ab0ac1SMarcel Holtmann 
3331d2ab0ac1SMarcel Holtmann 	return NULL;
3332d2ab0ac1SMarcel Holtmann }
3333d2ab0ac1SMarcel Holtmann 
3334d2ab0ac1SMarcel Holtmann void hci_white_list_clear(struct hci_dev *hdev)
3335d2ab0ac1SMarcel Holtmann {
3336d2ab0ac1SMarcel Holtmann 	struct list_head *p, *n;
3337d2ab0ac1SMarcel Holtmann 
3338d2ab0ac1SMarcel Holtmann 	list_for_each_safe(p, n, &hdev->le_white_list) {
3339d2ab0ac1SMarcel Holtmann 		struct bdaddr_list *b = list_entry(p, struct bdaddr_list, list);
3340d2ab0ac1SMarcel Holtmann 
3341d2ab0ac1SMarcel Holtmann 		list_del(p);
3342d2ab0ac1SMarcel Holtmann 		kfree(b);
3343d2ab0ac1SMarcel Holtmann 	}
3344d2ab0ac1SMarcel Holtmann }
3345d2ab0ac1SMarcel Holtmann 
3346d2ab0ac1SMarcel Holtmann int hci_white_list_add(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
3347d2ab0ac1SMarcel Holtmann {
3348d2ab0ac1SMarcel Holtmann 	struct bdaddr_list *entry;
3349d2ab0ac1SMarcel Holtmann 
3350d2ab0ac1SMarcel Holtmann 	if (!bacmp(bdaddr, BDADDR_ANY))
3351d2ab0ac1SMarcel Holtmann 		return -EBADF;
3352d2ab0ac1SMarcel Holtmann 
3353d2ab0ac1SMarcel Holtmann 	entry = kzalloc(sizeof(struct bdaddr_list), GFP_KERNEL);
3354d2ab0ac1SMarcel Holtmann 	if (!entry)
3355d2ab0ac1SMarcel Holtmann 		return -ENOMEM;
3356d2ab0ac1SMarcel Holtmann 
3357d2ab0ac1SMarcel Holtmann 	bacpy(&entry->bdaddr, bdaddr);
3358d2ab0ac1SMarcel Holtmann 	entry->bdaddr_type = type;
3359d2ab0ac1SMarcel Holtmann 
3360d2ab0ac1SMarcel Holtmann 	list_add(&entry->list, &hdev->le_white_list);
3361d2ab0ac1SMarcel Holtmann 
3362d2ab0ac1SMarcel Holtmann 	return 0;
3363d2ab0ac1SMarcel Holtmann }
3364d2ab0ac1SMarcel Holtmann 
3365d2ab0ac1SMarcel Holtmann int hci_white_list_del(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
3366d2ab0ac1SMarcel Holtmann {
3367d2ab0ac1SMarcel Holtmann 	struct bdaddr_list *entry;
3368d2ab0ac1SMarcel Holtmann 
3369d2ab0ac1SMarcel Holtmann 	if (!bacmp(bdaddr, BDADDR_ANY))
3370d2ab0ac1SMarcel Holtmann 		return -EBADF;
3371d2ab0ac1SMarcel Holtmann 
3372d2ab0ac1SMarcel Holtmann 	entry = hci_white_list_lookup(hdev, bdaddr, type);
3373d2ab0ac1SMarcel Holtmann 	if (!entry)
3374d2ab0ac1SMarcel Holtmann 		return -ENOENT;
3375d2ab0ac1SMarcel Holtmann 
3376d2ab0ac1SMarcel Holtmann 	list_del(&entry->list);
3377d2ab0ac1SMarcel Holtmann 	kfree(entry);
3378d2ab0ac1SMarcel Holtmann 
3379d2ab0ac1SMarcel Holtmann 	return 0;
3380d2ab0ac1SMarcel Holtmann }
3381d2ab0ac1SMarcel Holtmann 
338215819a70SAndre Guedes /* This function requires the caller holds hdev->lock */
338315819a70SAndre Guedes struct hci_conn_params *hci_conn_params_lookup(struct hci_dev *hdev,
338415819a70SAndre Guedes 					       bdaddr_t *addr, u8 addr_type)
338515819a70SAndre Guedes {
338615819a70SAndre Guedes 	struct hci_conn_params *params;
338715819a70SAndre Guedes 
338815819a70SAndre Guedes 	list_for_each_entry(params, &hdev->le_conn_params, list) {
338915819a70SAndre Guedes 		if (bacmp(&params->addr, addr) == 0 &&
339015819a70SAndre Guedes 		    params->addr_type == addr_type) {
339115819a70SAndre Guedes 			return params;
339215819a70SAndre Guedes 		}
339315819a70SAndre Guedes 	}
339415819a70SAndre Guedes 
339515819a70SAndre Guedes 	return NULL;
339615819a70SAndre Guedes }
339715819a70SAndre Guedes 
3398cef952ceSAndre Guedes static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
3399cef952ceSAndre Guedes {
3400cef952ceSAndre Guedes 	struct hci_conn *conn;
3401cef952ceSAndre Guedes 
3402cef952ceSAndre Guedes 	conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
3403cef952ceSAndre Guedes 	if (!conn)
3404cef952ceSAndre Guedes 		return false;
3405cef952ceSAndre Guedes 
3406cef952ceSAndre Guedes 	if (conn->dst_type != type)
3407cef952ceSAndre Guedes 		return false;
3408cef952ceSAndre Guedes 
3409cef952ceSAndre Guedes 	if (conn->state != BT_CONNECTED)
3410cef952ceSAndre Guedes 		return false;
3411cef952ceSAndre Guedes 
3412cef952ceSAndre Guedes 	return true;
3413cef952ceSAndre Guedes }
3414cef952ceSAndre Guedes 
3415a9b0a04cSAndre Guedes static bool is_identity_address(bdaddr_t *addr, u8 addr_type)
3416a9b0a04cSAndre Guedes {
3417a9b0a04cSAndre Guedes 	if (addr_type == ADDR_LE_DEV_PUBLIC)
3418a9b0a04cSAndre Guedes 		return true;
3419a9b0a04cSAndre Guedes 
3420a9b0a04cSAndre Guedes 	/* Check for Random Static address type */
3421a9b0a04cSAndre Guedes 	if ((addr->b[5] & 0xc0) == 0xc0)
3422a9b0a04cSAndre Guedes 		return true;
3423a9b0a04cSAndre Guedes 
3424a9b0a04cSAndre Guedes 	return false;
3425a9b0a04cSAndre Guedes }
3426a9b0a04cSAndre Guedes 
342715819a70SAndre Guedes /* This function requires the caller holds hdev->lock */
34284b10966fSMarcel Holtmann struct bdaddr_list *hci_pend_le_conn_lookup(struct hci_dev *hdev,
34294b10966fSMarcel Holtmann 					    bdaddr_t *addr, u8 addr_type)
34304b10966fSMarcel Holtmann {
34314b10966fSMarcel Holtmann 	struct bdaddr_list *entry;
34324b10966fSMarcel Holtmann 
34334b10966fSMarcel Holtmann 	list_for_each_entry(entry, &hdev->pend_le_conns, list) {
34344b10966fSMarcel Holtmann 		if (bacmp(&entry->bdaddr, addr) == 0 &&
34354b10966fSMarcel Holtmann 		    entry->bdaddr_type == addr_type)
34364b10966fSMarcel Holtmann 			return entry;
34374b10966fSMarcel Holtmann 	}
34384b10966fSMarcel Holtmann 
34394b10966fSMarcel Holtmann 	return NULL;
34404b10966fSMarcel Holtmann }
34414b10966fSMarcel Holtmann 
34424b10966fSMarcel Holtmann /* This function requires the caller holds hdev->lock */
34434b10966fSMarcel Holtmann void hci_pend_le_conn_add(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type)
34444b10966fSMarcel Holtmann {
34454b10966fSMarcel Holtmann 	struct bdaddr_list *entry;
34464b10966fSMarcel Holtmann 
34474b10966fSMarcel Holtmann 	entry = hci_pend_le_conn_lookup(hdev, addr, addr_type);
34484b10966fSMarcel Holtmann 	if (entry)
34494b10966fSMarcel Holtmann 		goto done;
34504b10966fSMarcel Holtmann 
34514b10966fSMarcel Holtmann 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
34524b10966fSMarcel Holtmann 	if (!entry) {
34534b10966fSMarcel Holtmann 		BT_ERR("Out of memory");
34544b10966fSMarcel Holtmann 		return;
34554b10966fSMarcel Holtmann 	}
34564b10966fSMarcel Holtmann 
34574b10966fSMarcel Holtmann 	bacpy(&entry->bdaddr, addr);
34584b10966fSMarcel Holtmann 	entry->bdaddr_type = addr_type;
34594b10966fSMarcel Holtmann 
34604b10966fSMarcel Holtmann 	list_add(&entry->list, &hdev->pend_le_conns);
34614b10966fSMarcel Holtmann 
34624b10966fSMarcel Holtmann 	BT_DBG("addr %pMR (type %u)", addr, addr_type);
34634b10966fSMarcel Holtmann 
34644b10966fSMarcel Holtmann done:
34654b10966fSMarcel Holtmann 	hci_update_background_scan(hdev);
34664b10966fSMarcel Holtmann }
34674b10966fSMarcel Holtmann 
34684b10966fSMarcel Holtmann /* This function requires the caller holds hdev->lock */
34694b10966fSMarcel Holtmann void hci_pend_le_conn_del(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type)
34704b10966fSMarcel Holtmann {
34714b10966fSMarcel Holtmann 	struct bdaddr_list *entry;
34724b10966fSMarcel Holtmann 
34734b10966fSMarcel Holtmann 	entry = hci_pend_le_conn_lookup(hdev, addr, addr_type);
34744b10966fSMarcel Holtmann 	if (!entry)
34754b10966fSMarcel Holtmann 		goto done;
34764b10966fSMarcel Holtmann 
34774b10966fSMarcel Holtmann 	list_del(&entry->list);
34784b10966fSMarcel Holtmann 	kfree(entry);
34794b10966fSMarcel Holtmann 
34804b10966fSMarcel Holtmann 	BT_DBG("addr %pMR (type %u)", addr, addr_type);
34814b10966fSMarcel Holtmann 
34824b10966fSMarcel Holtmann done:
34834b10966fSMarcel Holtmann 	hci_update_background_scan(hdev);
34844b10966fSMarcel Holtmann }
34854b10966fSMarcel Holtmann 
34864b10966fSMarcel Holtmann /* This function requires the caller holds hdev->lock */
34874b10966fSMarcel Holtmann void hci_pend_le_conns_clear(struct hci_dev *hdev)
34884b10966fSMarcel Holtmann {
34894b10966fSMarcel Holtmann 	struct bdaddr_list *entry, *tmp;
34904b10966fSMarcel Holtmann 
34914b10966fSMarcel Holtmann 	list_for_each_entry_safe(entry, tmp, &hdev->pend_le_conns, list) {
34924b10966fSMarcel Holtmann 		list_del(&entry->list);
34934b10966fSMarcel Holtmann 		kfree(entry);
34944b10966fSMarcel Holtmann 	}
34954b10966fSMarcel Holtmann 
34964b10966fSMarcel Holtmann 	BT_DBG("All LE pending connections cleared");
34971c1697c0SMarcel Holtmann 
34981c1697c0SMarcel Holtmann 	hci_update_background_scan(hdev);
34994b10966fSMarcel Holtmann }
35004b10966fSMarcel Holtmann 
35014b10966fSMarcel Holtmann /* This function requires the caller holds hdev->lock */
350251d167c0SMarcel Holtmann struct hci_conn_params *hci_conn_params_add(struct hci_dev *hdev,
350351d167c0SMarcel Holtmann 					    bdaddr_t *addr, u8 addr_type)
3504bf5b3c8bSMarcel Holtmann {
3505bf5b3c8bSMarcel Holtmann 	struct hci_conn_params *params;
3506bf5b3c8bSMarcel Holtmann 
3507bf5b3c8bSMarcel Holtmann 	if (!is_identity_address(addr, addr_type))
350851d167c0SMarcel Holtmann 		return NULL;
3509bf5b3c8bSMarcel Holtmann 
3510bf5b3c8bSMarcel Holtmann 	params = hci_conn_params_lookup(hdev, addr, addr_type);
3511bf5b3c8bSMarcel Holtmann 	if (params)
351251d167c0SMarcel Holtmann 		return params;
3513bf5b3c8bSMarcel Holtmann 
3514bf5b3c8bSMarcel Holtmann 	params = kzalloc(sizeof(*params), GFP_KERNEL);
3515bf5b3c8bSMarcel Holtmann 	if (!params) {
3516bf5b3c8bSMarcel Holtmann 		BT_ERR("Out of memory");
351751d167c0SMarcel Holtmann 		return NULL;
3518bf5b3c8bSMarcel Holtmann 	}
3519bf5b3c8bSMarcel Holtmann 
3520bf5b3c8bSMarcel Holtmann 	bacpy(&params->addr, addr);
3521bf5b3c8bSMarcel Holtmann 	params->addr_type = addr_type;
3522bf5b3c8bSMarcel Holtmann 
3523bf5b3c8bSMarcel Holtmann 	list_add(&params->list, &hdev->le_conn_params);
3524bf5b3c8bSMarcel Holtmann 
3525bf5b3c8bSMarcel Holtmann 	params->conn_min_interval = hdev->le_conn_min_interval;
3526bf5b3c8bSMarcel Holtmann 	params->conn_max_interval = hdev->le_conn_max_interval;
3527bf5b3c8bSMarcel Holtmann 	params->conn_latency = hdev->le_conn_latency;
3528bf5b3c8bSMarcel Holtmann 	params->supervision_timeout = hdev->le_supv_timeout;
3529bf5b3c8bSMarcel Holtmann 	params->auto_connect = HCI_AUTO_CONN_DISABLED;
3530bf5b3c8bSMarcel Holtmann 
3531bf5b3c8bSMarcel Holtmann 	BT_DBG("addr %pMR (type %u)", addr, addr_type);
3532bf5b3c8bSMarcel Holtmann 
353351d167c0SMarcel Holtmann 	return params;
3534bf5b3c8bSMarcel Holtmann }
3535bf5b3c8bSMarcel Holtmann 
3536bf5b3c8bSMarcel Holtmann /* This function requires the caller holds hdev->lock */
3537bf5b3c8bSMarcel Holtmann int hci_conn_params_set(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type,
3538d06b50ceSMarcel Holtmann 			u8 auto_connect)
353915819a70SAndre Guedes {
354015819a70SAndre Guedes 	struct hci_conn_params *params;
354115819a70SAndre Guedes 
35428c87aae1SMarcel Holtmann 	params = hci_conn_params_add(hdev, addr, addr_type);
35438c87aae1SMarcel Holtmann 	if (!params)
35448c87aae1SMarcel Holtmann 		return -EIO;
3545a9b0a04cSAndre Guedes 
35469fcb18efSAndre Guedes 	params->auto_connect = auto_connect;
354715819a70SAndre Guedes 
3548cef952ceSAndre Guedes 	switch (auto_connect) {
3549cef952ceSAndre Guedes 	case HCI_AUTO_CONN_DISABLED:
3550a3451d27SJohan Hedberg 	case HCI_AUTO_CONN_REPORT:
3551cef952ceSAndre Guedes 	case HCI_AUTO_CONN_LINK_LOSS:
3552cef952ceSAndre Guedes 		hci_pend_le_conn_del(hdev, addr, addr_type);
3553cef952ceSAndre Guedes 		break;
3554cef952ceSAndre Guedes 	case HCI_AUTO_CONN_ALWAYS:
3555cef952ceSAndre Guedes 		if (!is_connected(hdev, addr, addr_type))
3556cef952ceSAndre Guedes 			hci_pend_le_conn_add(hdev, addr, addr_type);
3557cef952ceSAndre Guedes 		break;
3558cef952ceSAndre Guedes 	}
355915819a70SAndre Guedes 
3560d06b50ceSMarcel Holtmann 	BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type,
3561d06b50ceSMarcel Holtmann 	       auto_connect);
3562a9b0a04cSAndre Guedes 
3563a9b0a04cSAndre Guedes 	return 0;
356415819a70SAndre Guedes }
356515819a70SAndre Guedes 
356615819a70SAndre Guedes /* This function requires the caller holds hdev->lock */
356715819a70SAndre Guedes void hci_conn_params_del(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type)
356815819a70SAndre Guedes {
356915819a70SAndre Guedes 	struct hci_conn_params *params;
357015819a70SAndre Guedes 
357115819a70SAndre Guedes 	params = hci_conn_params_lookup(hdev, addr, addr_type);
357215819a70SAndre Guedes 	if (!params)
357315819a70SAndre Guedes 		return;
357415819a70SAndre Guedes 
3575cef952ceSAndre Guedes 	hci_pend_le_conn_del(hdev, addr, addr_type);
3576cef952ceSAndre Guedes 
357715819a70SAndre Guedes 	list_del(&params->list);
357815819a70SAndre Guedes 	kfree(params);
357915819a70SAndre Guedes 
358015819a70SAndre Guedes 	BT_DBG("addr %pMR (type %u)", addr, addr_type);
358115819a70SAndre Guedes }
358215819a70SAndre Guedes 
358315819a70SAndre Guedes /* This function requires the caller holds hdev->lock */
358455af49a8SJohan Hedberg void hci_conn_params_clear_disabled(struct hci_dev *hdev)
358555af49a8SJohan Hedberg {
358655af49a8SJohan Hedberg 	struct hci_conn_params *params, *tmp;
358755af49a8SJohan Hedberg 
358855af49a8SJohan Hedberg 	list_for_each_entry_safe(params, tmp, &hdev->le_conn_params, list) {
358955af49a8SJohan Hedberg 		if (params->auto_connect != HCI_AUTO_CONN_DISABLED)
359055af49a8SJohan Hedberg 			continue;
359155af49a8SJohan Hedberg 		list_del(&params->list);
359255af49a8SJohan Hedberg 		kfree(params);
359355af49a8SJohan Hedberg 	}
359455af49a8SJohan Hedberg 
359555af49a8SJohan Hedberg 	BT_DBG("All LE disabled connection parameters were removed");
359655af49a8SJohan Hedberg }
359755af49a8SJohan Hedberg 
359855af49a8SJohan Hedberg /* This function requires the caller holds hdev->lock */
359955af49a8SJohan Hedberg void hci_conn_params_clear_enabled(struct hci_dev *hdev)
360055af49a8SJohan Hedberg {
360155af49a8SJohan Hedberg 	struct hci_conn_params *params, *tmp;
360255af49a8SJohan Hedberg 
360355af49a8SJohan Hedberg 	list_for_each_entry_safe(params, tmp, &hdev->le_conn_params, list) {
360455af49a8SJohan Hedberg 		if (params->auto_connect == HCI_AUTO_CONN_DISABLED)
360555af49a8SJohan Hedberg 			continue;
360655af49a8SJohan Hedberg 		list_del(&params->list);
360755af49a8SJohan Hedberg 		kfree(params);
360855af49a8SJohan Hedberg 	}
360955af49a8SJohan Hedberg 
361055af49a8SJohan Hedberg 	hci_pend_le_conns_clear(hdev);
361155af49a8SJohan Hedberg 
361255af49a8SJohan Hedberg 	BT_DBG("All enabled LE connection parameters were removed");
361355af49a8SJohan Hedberg }
361455af49a8SJohan Hedberg 
361555af49a8SJohan Hedberg /* This function requires the caller holds hdev->lock */
3616373110c5SJohan Hedberg void hci_conn_params_clear_all(struct hci_dev *hdev)
361715819a70SAndre Guedes {
361815819a70SAndre Guedes 	struct hci_conn_params *params, *tmp;
361915819a70SAndre Guedes 
362015819a70SAndre Guedes 	list_for_each_entry_safe(params, tmp, &hdev->le_conn_params, list) {
362115819a70SAndre Guedes 		list_del(&params->list);
362215819a70SAndre Guedes 		kfree(params);
362315819a70SAndre Guedes 	}
362415819a70SAndre Guedes 
36251089b67dSMarcel Holtmann 	hci_pend_le_conns_clear(hdev);
36261089b67dSMarcel Holtmann 
362715819a70SAndre Guedes 	BT_DBG("All LE connection parameters were removed");
362815819a70SAndre Guedes }
362915819a70SAndre Guedes 
36304c87eaabSAndre Guedes static void inquiry_complete(struct hci_dev *hdev, u8 status)
36317ba8b4beSAndre Guedes {
36324c87eaabSAndre Guedes 	if (status) {
36334c87eaabSAndre Guedes 		BT_ERR("Failed to start inquiry: status %d", status);
36347ba8b4beSAndre Guedes 
36354c87eaabSAndre Guedes 		hci_dev_lock(hdev);
36364c87eaabSAndre Guedes 		hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
36374c87eaabSAndre Guedes 		hci_dev_unlock(hdev);
36384c87eaabSAndre Guedes 		return;
36394c87eaabSAndre Guedes 	}
36407ba8b4beSAndre Guedes }
36417ba8b4beSAndre Guedes 
36424c87eaabSAndre Guedes static void le_scan_disable_work_complete(struct hci_dev *hdev, u8 status)
36437ba8b4beSAndre Guedes {
36444c87eaabSAndre Guedes 	/* General inquiry access code (GIAC) */
36454c87eaabSAndre Guedes 	u8 lap[3] = { 0x33, 0x8b, 0x9e };
36464c87eaabSAndre Guedes 	struct hci_request req;
36474c87eaabSAndre Guedes 	struct hci_cp_inquiry cp;
36487ba8b4beSAndre Guedes 	int err;
36497ba8b4beSAndre Guedes 
36504c87eaabSAndre Guedes 	if (status) {
36514c87eaabSAndre Guedes 		BT_ERR("Failed to disable LE scanning: status %d", status);
36524c87eaabSAndre Guedes 		return;
36537ba8b4beSAndre Guedes 	}
36547ba8b4beSAndre Guedes 
36554c87eaabSAndre Guedes 	switch (hdev->discovery.type) {
36564c87eaabSAndre Guedes 	case DISCOV_TYPE_LE:
36574c87eaabSAndre Guedes 		hci_dev_lock(hdev);
36584c87eaabSAndre Guedes 		hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
36594c87eaabSAndre Guedes 		hci_dev_unlock(hdev);
36604c87eaabSAndre Guedes 		break;
36617dbfac1dSAndre Guedes 
36624c87eaabSAndre Guedes 	case DISCOV_TYPE_INTERLEAVED:
36634c87eaabSAndre Guedes 		hci_req_init(&req, hdev);
36647dbfac1dSAndre Guedes 
36657dbfac1dSAndre Guedes 		memset(&cp, 0, sizeof(cp));
36664c87eaabSAndre Guedes 		memcpy(&cp.lap, lap, sizeof(cp.lap));
36674c87eaabSAndre Guedes 		cp.length = DISCOV_INTERLEAVED_INQUIRY_LEN;
36684c87eaabSAndre Guedes 		hci_req_add(&req, HCI_OP_INQUIRY, sizeof(cp), &cp);
36694c87eaabSAndre Guedes 
36704c87eaabSAndre Guedes 		hci_dev_lock(hdev);
36714c87eaabSAndre Guedes 
36724c87eaabSAndre Guedes 		hci_inquiry_cache_flush(hdev);
36734c87eaabSAndre Guedes 
36744c87eaabSAndre Guedes 		err = hci_req_run(&req, inquiry_complete);
36754c87eaabSAndre Guedes 		if (err) {
36764c87eaabSAndre Guedes 			BT_ERR("Inquiry request failed: err %d", err);
36774c87eaabSAndre Guedes 			hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
36787dbfac1dSAndre Guedes 		}
36797dbfac1dSAndre Guedes 
36804c87eaabSAndre Guedes 		hci_dev_unlock(hdev);
36814c87eaabSAndre Guedes 		break;
36824c87eaabSAndre Guedes 	}
36837dbfac1dSAndre Guedes }
36847dbfac1dSAndre Guedes 
36857ba8b4beSAndre Guedes static void le_scan_disable_work(struct work_struct *work)
36867ba8b4beSAndre Guedes {
36877ba8b4beSAndre Guedes 	struct hci_dev *hdev = container_of(work, struct hci_dev,
36887ba8b4beSAndre Guedes 					    le_scan_disable.work);
36894c87eaabSAndre Guedes 	struct hci_request req;
36904c87eaabSAndre Guedes 	int err;
36917ba8b4beSAndre Guedes 
36927ba8b4beSAndre Guedes 	BT_DBG("%s", hdev->name);
36937ba8b4beSAndre Guedes 
36944c87eaabSAndre Guedes 	hci_req_init(&req, hdev);
36957ba8b4beSAndre Guedes 
3696b1efcc28SAndre Guedes 	hci_req_add_le_scan_disable(&req);
36977ba8b4beSAndre Guedes 
36984c87eaabSAndre Guedes 	err = hci_req_run(&req, le_scan_disable_work_complete);
36994c87eaabSAndre Guedes 	if (err)
37004c87eaabSAndre Guedes 		BT_ERR("Disable LE scanning request failed: err %d", err);
370128b75a89SAndre Guedes }
370228b75a89SAndre Guedes 
37038d97250eSJohan Hedberg static void set_random_addr(struct hci_request *req, bdaddr_t *rpa)
37048d97250eSJohan Hedberg {
37058d97250eSJohan Hedberg 	struct hci_dev *hdev = req->hdev;
37068d97250eSJohan Hedberg 
37078d97250eSJohan Hedberg 	/* If we're advertising or initiating an LE connection we can't
37088d97250eSJohan Hedberg 	 * go ahead and change the random address at this time. This is
37098d97250eSJohan Hedberg 	 * because the eventual initiator address used for the
37108d97250eSJohan Hedberg 	 * subsequently created connection will be undefined (some
37118d97250eSJohan Hedberg 	 * controllers use the new address and others the one we had
37128d97250eSJohan Hedberg 	 * when the operation started).
37138d97250eSJohan Hedberg 	 *
37148d97250eSJohan Hedberg 	 * In this kind of scenario skip the update and let the random
37158d97250eSJohan Hedberg 	 * address be updated at the next cycle.
37168d97250eSJohan Hedberg 	 */
37178d97250eSJohan Hedberg 	if (test_bit(HCI_ADVERTISING, &hdev->dev_flags) ||
37188d97250eSJohan Hedberg 	    hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT)) {
37198d97250eSJohan Hedberg 		BT_DBG("Deferring random address update");
37208d97250eSJohan Hedberg 		return;
37218d97250eSJohan Hedberg 	}
37228d97250eSJohan Hedberg 
37238d97250eSJohan Hedberg 	hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6, rpa);
37248d97250eSJohan Hedberg }
37258d97250eSJohan Hedberg 
372694b1fc92SMarcel Holtmann int hci_update_random_address(struct hci_request *req, bool require_privacy,
372794b1fc92SMarcel Holtmann 			      u8 *own_addr_type)
3728ebd3a747SJohan Hedberg {
3729ebd3a747SJohan Hedberg 	struct hci_dev *hdev = req->hdev;
3730ebd3a747SJohan Hedberg 	int err;
3731ebd3a747SJohan Hedberg 
3732ebd3a747SJohan Hedberg 	/* If privacy is enabled use a resolvable private address. If
37332b5224dcSMarcel Holtmann 	 * current RPA has expired or there is something else than
37342b5224dcSMarcel Holtmann 	 * the current RPA in use, then generate a new one.
3735ebd3a747SJohan Hedberg 	 */
3736ebd3a747SJohan Hedberg 	if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) {
3737ebd3a747SJohan Hedberg 		int to;
3738ebd3a747SJohan Hedberg 
3739ebd3a747SJohan Hedberg 		*own_addr_type = ADDR_LE_DEV_RANDOM;
3740ebd3a747SJohan Hedberg 
3741ebd3a747SJohan Hedberg 		if (!test_and_clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags) &&
37422b5224dcSMarcel Holtmann 		    !bacmp(&hdev->random_addr, &hdev->rpa))
3743ebd3a747SJohan Hedberg 			return 0;
3744ebd3a747SJohan Hedberg 
37452b5224dcSMarcel Holtmann 		err = smp_generate_rpa(hdev->tfm_aes, hdev->irk, &hdev->rpa);
3746ebd3a747SJohan Hedberg 		if (err < 0) {
3747ebd3a747SJohan Hedberg 			BT_ERR("%s failed to generate new RPA", hdev->name);
3748ebd3a747SJohan Hedberg 			return err;
3749ebd3a747SJohan Hedberg 		}
3750ebd3a747SJohan Hedberg 
37518d97250eSJohan Hedberg 		set_random_addr(req, &hdev->rpa);
3752ebd3a747SJohan Hedberg 
3753ebd3a747SJohan Hedberg 		to = msecs_to_jiffies(hdev->rpa_timeout * 1000);
3754ebd3a747SJohan Hedberg 		queue_delayed_work(hdev->workqueue, &hdev->rpa_expired, to);
3755ebd3a747SJohan Hedberg 
3756ebd3a747SJohan Hedberg 		return 0;
3757ebd3a747SJohan Hedberg 	}
3758ebd3a747SJohan Hedberg 
375994b1fc92SMarcel Holtmann 	/* In case of required privacy without resolvable private address,
376094b1fc92SMarcel Holtmann 	 * use an unresolvable private address. This is useful for active
376194b1fc92SMarcel Holtmann 	 * scanning and non-connectable advertising.
376294b1fc92SMarcel Holtmann 	 */
376394b1fc92SMarcel Holtmann 	if (require_privacy) {
376494b1fc92SMarcel Holtmann 		bdaddr_t urpa;
376594b1fc92SMarcel Holtmann 
376694b1fc92SMarcel Holtmann 		get_random_bytes(&urpa, 6);
376794b1fc92SMarcel Holtmann 		urpa.b[5] &= 0x3f;	/* Clear two most significant bits */
376894b1fc92SMarcel Holtmann 
376994b1fc92SMarcel Holtmann 		*own_addr_type = ADDR_LE_DEV_RANDOM;
37708d97250eSJohan Hedberg 		set_random_addr(req, &urpa);
377194b1fc92SMarcel Holtmann 		return 0;
377294b1fc92SMarcel Holtmann 	}
377394b1fc92SMarcel Holtmann 
3774ebd3a747SJohan Hedberg 	/* If forcing static address is in use or there is no public
3775ebd3a747SJohan Hedberg 	 * address use the static address as random address (but skip
3776ebd3a747SJohan Hedberg 	 * the HCI command if the current random address is already the
3777ebd3a747SJohan Hedberg 	 * static one.
3778ebd3a747SJohan Hedberg 	 */
3779111902f7SMarcel Holtmann 	if (test_bit(HCI_FORCE_STATIC_ADDR, &hdev->dbg_flags) ||
3780ebd3a747SJohan Hedberg 	    !bacmp(&hdev->bdaddr, BDADDR_ANY)) {
3781ebd3a747SJohan Hedberg 		*own_addr_type = ADDR_LE_DEV_RANDOM;
3782ebd3a747SJohan Hedberg 		if (bacmp(&hdev->static_addr, &hdev->random_addr))
3783ebd3a747SJohan Hedberg 			hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6,
3784ebd3a747SJohan Hedberg 				    &hdev->static_addr);
3785ebd3a747SJohan Hedberg 		return 0;
3786ebd3a747SJohan Hedberg 	}
3787ebd3a747SJohan Hedberg 
3788ebd3a747SJohan Hedberg 	/* Neither privacy nor static address is being used so use a
3789ebd3a747SJohan Hedberg 	 * public address.
3790ebd3a747SJohan Hedberg 	 */
3791ebd3a747SJohan Hedberg 	*own_addr_type = ADDR_LE_DEV_PUBLIC;
3792ebd3a747SJohan Hedberg 
3793ebd3a747SJohan Hedberg 	return 0;
3794ebd3a747SJohan Hedberg }
3795ebd3a747SJohan Hedberg 
3796a1f4c318SJohan Hedberg /* Copy the Identity Address of the controller.
3797a1f4c318SJohan Hedberg  *
3798a1f4c318SJohan Hedberg  * If the controller has a public BD_ADDR, then by default use that one.
3799a1f4c318SJohan Hedberg  * If this is a LE only controller without a public address, default to
3800a1f4c318SJohan Hedberg  * the static random address.
3801a1f4c318SJohan Hedberg  *
3802a1f4c318SJohan Hedberg  * For debugging purposes it is possible to force controllers with a
3803a1f4c318SJohan Hedberg  * public address to use the static random address instead.
3804a1f4c318SJohan Hedberg  */
3805a1f4c318SJohan Hedberg void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr,
3806a1f4c318SJohan Hedberg 			       u8 *bdaddr_type)
3807a1f4c318SJohan Hedberg {
3808111902f7SMarcel Holtmann 	if (test_bit(HCI_FORCE_STATIC_ADDR, &hdev->dbg_flags) ||
3809a1f4c318SJohan Hedberg 	    !bacmp(&hdev->bdaddr, BDADDR_ANY)) {
3810a1f4c318SJohan Hedberg 		bacpy(bdaddr, &hdev->static_addr);
3811a1f4c318SJohan Hedberg 		*bdaddr_type = ADDR_LE_DEV_RANDOM;
3812a1f4c318SJohan Hedberg 	} else {
3813a1f4c318SJohan Hedberg 		bacpy(bdaddr, &hdev->bdaddr);
3814a1f4c318SJohan Hedberg 		*bdaddr_type = ADDR_LE_DEV_PUBLIC;
3815a1f4c318SJohan Hedberg 	}
3816a1f4c318SJohan Hedberg }
3817a1f4c318SJohan Hedberg 
38189be0dab7SDavid Herrmann /* Alloc HCI device */
38199be0dab7SDavid Herrmann struct hci_dev *hci_alloc_dev(void)
38209be0dab7SDavid Herrmann {
38219be0dab7SDavid Herrmann 	struct hci_dev *hdev;
38229be0dab7SDavid Herrmann 
38239be0dab7SDavid Herrmann 	hdev = kzalloc(sizeof(struct hci_dev), GFP_KERNEL);
38249be0dab7SDavid Herrmann 	if (!hdev)
38259be0dab7SDavid Herrmann 		return NULL;
38269be0dab7SDavid Herrmann 
3827b1b813d4SDavid Herrmann 	hdev->pkt_type  = (HCI_DM1 | HCI_DH1 | HCI_HV1);
3828b1b813d4SDavid Herrmann 	hdev->esco_type = (ESCO_HV1);
3829b1b813d4SDavid Herrmann 	hdev->link_mode = (HCI_LM_ACCEPT);
3830b4cb9fb2SMarcel Holtmann 	hdev->num_iac = 0x01;		/* One IAC support is mandatory */
3831b1b813d4SDavid Herrmann 	hdev->io_capability = 0x03;	/* No Input No Output */
383296c2103aSMarcel Holtmann 	hdev->manufacturer = 0xffff;	/* Default to internal use */
3833bbaf444aSJohan Hedberg 	hdev->inq_tx_power = HCI_TX_POWER_INVALID;
3834bbaf444aSJohan Hedberg 	hdev->adv_tx_power = HCI_TX_POWER_INVALID;
3835b1b813d4SDavid Herrmann 
3836b1b813d4SDavid Herrmann 	hdev->sniff_max_interval = 800;
3837b1b813d4SDavid Herrmann 	hdev->sniff_min_interval = 80;
3838b1b813d4SDavid Herrmann 
38393f959d46SMarcel Holtmann 	hdev->le_adv_channel_map = 0x07;
3840bef64738SMarcel Holtmann 	hdev->le_scan_interval = 0x0060;
3841bef64738SMarcel Holtmann 	hdev->le_scan_window = 0x0030;
38424e70c7e7SMarcel Holtmann 	hdev->le_conn_min_interval = 0x0028;
38434e70c7e7SMarcel Holtmann 	hdev->le_conn_max_interval = 0x0038;
384404fb7d90SMarcel Holtmann 	hdev->le_conn_latency = 0x0000;
384504fb7d90SMarcel Holtmann 	hdev->le_supv_timeout = 0x002a;
3846bef64738SMarcel Holtmann 
3847d6bfd59cSJohan Hedberg 	hdev->rpa_timeout = HCI_DEFAULT_RPA_TIMEOUT;
3848b9a7a61eSLukasz Rymanowski 	hdev->discov_interleaved_timeout = DISCOV_INTERLEAVED_TIMEOUT;
384931ad1691SAndrzej Kaczmarek 	hdev->conn_info_min_age = DEFAULT_CONN_INFO_MIN_AGE;
385031ad1691SAndrzej Kaczmarek 	hdev->conn_info_max_age = DEFAULT_CONN_INFO_MAX_AGE;
3851d6bfd59cSJohan Hedberg 
3852b1b813d4SDavid Herrmann 	mutex_init(&hdev->lock);
3853b1b813d4SDavid Herrmann 	mutex_init(&hdev->req_lock);
3854b1b813d4SDavid Herrmann 
3855b1b813d4SDavid Herrmann 	INIT_LIST_HEAD(&hdev->mgmt_pending);
3856b1b813d4SDavid Herrmann 	INIT_LIST_HEAD(&hdev->blacklist);
3857b1b813d4SDavid Herrmann 	INIT_LIST_HEAD(&hdev->uuids);
3858b1b813d4SDavid Herrmann 	INIT_LIST_HEAD(&hdev->link_keys);
3859b1b813d4SDavid Herrmann 	INIT_LIST_HEAD(&hdev->long_term_keys);
3860970c4e46SJohan Hedberg 	INIT_LIST_HEAD(&hdev->identity_resolving_keys);
3861b1b813d4SDavid Herrmann 	INIT_LIST_HEAD(&hdev->remote_oob_data);
3862d2ab0ac1SMarcel Holtmann 	INIT_LIST_HEAD(&hdev->le_white_list);
386315819a70SAndre Guedes 	INIT_LIST_HEAD(&hdev->le_conn_params);
386477a77a30SAndre Guedes 	INIT_LIST_HEAD(&hdev->pend_le_conns);
38656b536b5eSAndrei Emeltchenko 	INIT_LIST_HEAD(&hdev->conn_hash.list);
3866b1b813d4SDavid Herrmann 
3867b1b813d4SDavid Herrmann 	INIT_WORK(&hdev->rx_work, hci_rx_work);
3868b1b813d4SDavid Herrmann 	INIT_WORK(&hdev->cmd_work, hci_cmd_work);
3869b1b813d4SDavid Herrmann 	INIT_WORK(&hdev->tx_work, hci_tx_work);
3870b1b813d4SDavid Herrmann 	INIT_WORK(&hdev->power_on, hci_power_on);
3871b1b813d4SDavid Herrmann 
3872b1b813d4SDavid Herrmann 	INIT_DELAYED_WORK(&hdev->power_off, hci_power_off);
3873b1b813d4SDavid Herrmann 	INIT_DELAYED_WORK(&hdev->discov_off, hci_discov_off);
3874b1b813d4SDavid Herrmann 	INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable_work);
3875b1b813d4SDavid Herrmann 
3876b1b813d4SDavid Herrmann 	skb_queue_head_init(&hdev->rx_q);
3877b1b813d4SDavid Herrmann 	skb_queue_head_init(&hdev->cmd_q);
3878b1b813d4SDavid Herrmann 	skb_queue_head_init(&hdev->raw_q);
3879b1b813d4SDavid Herrmann 
3880b1b813d4SDavid Herrmann 	init_waitqueue_head(&hdev->req_wait_q);
3881b1b813d4SDavid Herrmann 
388265cc2b49SMarcel Holtmann 	INIT_DELAYED_WORK(&hdev->cmd_timer, hci_cmd_timeout);
3883b1b813d4SDavid Herrmann 
3884b1b813d4SDavid Herrmann 	hci_init_sysfs(hdev);
3885b1b813d4SDavid Herrmann 	discovery_init(hdev);
38869be0dab7SDavid Herrmann 
38879be0dab7SDavid Herrmann 	return hdev;
38889be0dab7SDavid Herrmann }
38899be0dab7SDavid Herrmann EXPORT_SYMBOL(hci_alloc_dev);
38909be0dab7SDavid Herrmann 
38919be0dab7SDavid Herrmann /* Free HCI device */
38929be0dab7SDavid Herrmann void hci_free_dev(struct hci_dev *hdev)
38939be0dab7SDavid Herrmann {
38949be0dab7SDavid Herrmann 	/* will free via device release */
38959be0dab7SDavid Herrmann 	put_device(&hdev->dev);
38969be0dab7SDavid Herrmann }
38979be0dab7SDavid Herrmann EXPORT_SYMBOL(hci_free_dev);
38989be0dab7SDavid Herrmann 
38991da177e4SLinus Torvalds /* Register HCI device */
39001da177e4SLinus Torvalds int hci_register_dev(struct hci_dev *hdev)
39011da177e4SLinus Torvalds {
3902b1b813d4SDavid Herrmann 	int id, error;
39031da177e4SLinus Torvalds 
3904010666a1SDavid Herrmann 	if (!hdev->open || !hdev->close)
39051da177e4SLinus Torvalds 		return -EINVAL;
39061da177e4SLinus Torvalds 
390708add513SMat Martineau 	/* Do not allow HCI_AMP devices to register at index 0,
390808add513SMat Martineau 	 * so the index can be used as the AMP controller ID.
390908add513SMat Martineau 	 */
39103df92b31SSasha Levin 	switch (hdev->dev_type) {
39113df92b31SSasha Levin 	case HCI_BREDR:
39123df92b31SSasha Levin 		id = ida_simple_get(&hci_index_ida, 0, 0, GFP_KERNEL);
39131da177e4SLinus Torvalds 		break;
39143df92b31SSasha Levin 	case HCI_AMP:
39153df92b31SSasha Levin 		id = ida_simple_get(&hci_index_ida, 1, 0, GFP_KERNEL);
39163df92b31SSasha Levin 		break;
39173df92b31SSasha Levin 	default:
39183df92b31SSasha Levin 		return -EINVAL;
39191da177e4SLinus Torvalds 	}
39201da177e4SLinus Torvalds 
39213df92b31SSasha Levin 	if (id < 0)
39223df92b31SSasha Levin 		return id;
39233df92b31SSasha Levin 
39241da177e4SLinus Torvalds 	sprintf(hdev->name, "hci%d", id);
39251da177e4SLinus Torvalds 	hdev->id = id;
39262d8b3a11SAndrei Emeltchenko 
39272d8b3a11SAndrei Emeltchenko 	BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
39282d8b3a11SAndrei Emeltchenko 
3929d8537548SKees Cook 	hdev->workqueue = alloc_workqueue("%s", WQ_HIGHPRI | WQ_UNBOUND |
3930d8537548SKees Cook 					  WQ_MEM_RECLAIM, 1, hdev->name);
393133ca954dSDavid Herrmann 	if (!hdev->workqueue) {
393233ca954dSDavid Herrmann 		error = -ENOMEM;
393333ca954dSDavid Herrmann 		goto err;
393433ca954dSDavid Herrmann 	}
3935f48fd9c8SMarcel Holtmann 
3936d8537548SKees Cook 	hdev->req_workqueue = alloc_workqueue("%s", WQ_HIGHPRI | WQ_UNBOUND |
3937d8537548SKees Cook 					      WQ_MEM_RECLAIM, 1, hdev->name);
39386ead1bbcSJohan Hedberg 	if (!hdev->req_workqueue) {
39396ead1bbcSJohan Hedberg 		destroy_workqueue(hdev->workqueue);
39406ead1bbcSJohan Hedberg 		error = -ENOMEM;
39416ead1bbcSJohan Hedberg 		goto err;
39426ead1bbcSJohan Hedberg 	}
39436ead1bbcSJohan Hedberg 
39440153e2ecSMarcel Holtmann 	if (!IS_ERR_OR_NULL(bt_debugfs))
39450153e2ecSMarcel Holtmann 		hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs);
39460153e2ecSMarcel Holtmann 
3947bdc3e0f1SMarcel Holtmann 	dev_set_name(&hdev->dev, "%s", hdev->name);
3948bdc3e0f1SMarcel Holtmann 
394999780a7bSJohan Hedberg 	hdev->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0,
395099780a7bSJohan Hedberg 					       CRYPTO_ALG_ASYNC);
395199780a7bSJohan Hedberg 	if (IS_ERR(hdev->tfm_aes)) {
395299780a7bSJohan Hedberg 		BT_ERR("Unable to create crypto context");
395399780a7bSJohan Hedberg 		error = PTR_ERR(hdev->tfm_aes);
395499780a7bSJohan Hedberg 		hdev->tfm_aes = NULL;
395599780a7bSJohan Hedberg 		goto err_wqueue;
395699780a7bSJohan Hedberg 	}
395799780a7bSJohan Hedberg 
3958bdc3e0f1SMarcel Holtmann 	error = device_add(&hdev->dev);
395933ca954dSDavid Herrmann 	if (error < 0)
396099780a7bSJohan Hedberg 		goto err_tfm;
39611da177e4SLinus Torvalds 
3962611b30f7SMarcel Holtmann 	hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev,
3963a8c5fb1aSGustavo Padovan 				    RFKILL_TYPE_BLUETOOTH, &hci_rfkill_ops,
3964a8c5fb1aSGustavo Padovan 				    hdev);
3965611b30f7SMarcel Holtmann 	if (hdev->rfkill) {
3966611b30f7SMarcel Holtmann 		if (rfkill_register(hdev->rfkill) < 0) {
3967611b30f7SMarcel Holtmann 			rfkill_destroy(hdev->rfkill);
3968611b30f7SMarcel Holtmann 			hdev->rfkill = NULL;
3969611b30f7SMarcel Holtmann 		}
3970611b30f7SMarcel Holtmann 	}
3971611b30f7SMarcel Holtmann 
39725e130367SJohan Hedberg 	if (hdev->rfkill && rfkill_blocked(hdev->rfkill))
39735e130367SJohan Hedberg 		set_bit(HCI_RFKILLED, &hdev->dev_flags);
39745e130367SJohan Hedberg 
3975a8b2d5c2SJohan Hedberg 	set_bit(HCI_SETUP, &hdev->dev_flags);
3976004b0258SMarcel Holtmann 	set_bit(HCI_AUTO_OFF, &hdev->dev_flags);
3977ce2be9acSAndrei Emeltchenko 
397801cd3404SMarcel Holtmann 	if (hdev->dev_type == HCI_BREDR) {
397956f87901SJohan Hedberg 		/* Assume BR/EDR support until proven otherwise (such as
398056f87901SJohan Hedberg 		 * through reading supported features during init.
398156f87901SJohan Hedberg 		 */
398256f87901SJohan Hedberg 		set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags);
398356f87901SJohan Hedberg 	}
3984ce2be9acSAndrei Emeltchenko 
3985fcee3377SGustavo Padovan 	write_lock(&hci_dev_list_lock);
3986fcee3377SGustavo Padovan 	list_add(&hdev->list, &hci_dev_list);
3987fcee3377SGustavo Padovan 	write_unlock(&hci_dev_list_lock);
3988fcee3377SGustavo Padovan 
3989fee746b0SMarcel Holtmann 	/* Devices that are marked for raw-only usage need to set
3990fee746b0SMarcel Holtmann 	 * the HCI_RAW flag to indicate that only user channel is
3991fee746b0SMarcel Holtmann 	 * supported.
3992fee746b0SMarcel Holtmann 	 */
3993fee746b0SMarcel Holtmann 	if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
3994fee746b0SMarcel Holtmann 		set_bit(HCI_RAW, &hdev->flags);
3995fee746b0SMarcel Holtmann 
39961da177e4SLinus Torvalds 	hci_notify(hdev, HCI_DEV_REG);
3997dc946bd8SDavid Herrmann 	hci_dev_hold(hdev);
39981da177e4SLinus Torvalds 
399919202573SJohan Hedberg 	queue_work(hdev->req_workqueue, &hdev->power_on);
4000fbe96d6fSMarcel Holtmann 
40011da177e4SLinus Torvalds 	return id;
4002f48fd9c8SMarcel Holtmann 
400399780a7bSJohan Hedberg err_tfm:
400499780a7bSJohan Hedberg 	crypto_free_blkcipher(hdev->tfm_aes);
400533ca954dSDavid Herrmann err_wqueue:
400633ca954dSDavid Herrmann 	destroy_workqueue(hdev->workqueue);
40076ead1bbcSJohan Hedberg 	destroy_workqueue(hdev->req_workqueue);
400833ca954dSDavid Herrmann err:
40093df92b31SSasha Levin 	ida_simple_remove(&hci_index_ida, hdev->id);
4010f48fd9c8SMarcel Holtmann 
401133ca954dSDavid Herrmann 	return error;
40121da177e4SLinus Torvalds }
40131da177e4SLinus Torvalds EXPORT_SYMBOL(hci_register_dev);
40141da177e4SLinus Torvalds 
40151da177e4SLinus Torvalds /* Unregister HCI device */
401659735631SDavid Herrmann void hci_unregister_dev(struct hci_dev *hdev)
40171da177e4SLinus Torvalds {
40183df92b31SSasha Levin 	int i, id;
4019ef222013SMarcel Holtmann 
4020c13854ceSMarcel Holtmann 	BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
40211da177e4SLinus Torvalds 
402294324962SJohan Hovold 	set_bit(HCI_UNREGISTER, &hdev->dev_flags);
402394324962SJohan Hovold 
40243df92b31SSasha Levin 	id = hdev->id;
40253df92b31SSasha Levin 
4026f20d09d5SGustavo F. Padovan 	write_lock(&hci_dev_list_lock);
40271da177e4SLinus Torvalds 	list_del(&hdev->list);
4028f20d09d5SGustavo F. Padovan 	write_unlock(&hci_dev_list_lock);
40291da177e4SLinus Torvalds 
40301da177e4SLinus Torvalds 	hci_dev_do_close(hdev);
40311da177e4SLinus Torvalds 
4032cd4c5391SSuraj Sumangala 	for (i = 0; i < NUM_REASSEMBLY; i++)
4033ef222013SMarcel Holtmann 		kfree_skb(hdev->reassembly[i]);
4034ef222013SMarcel Holtmann 
4035b9b5ef18SGustavo Padovan 	cancel_work_sync(&hdev->power_on);
4036b9b5ef18SGustavo Padovan 
4037ab81cbf9SJohan Hedberg 	if (!test_bit(HCI_INIT, &hdev->flags) &&
4038fee746b0SMarcel Holtmann 	    !test_bit(HCI_SETUP, &hdev->dev_flags) &&
4039fee746b0SMarcel Holtmann 	    !test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) {
404009fd0de5SGustavo F. Padovan 		hci_dev_lock(hdev);
4041744cf19eSJohan Hedberg 		mgmt_index_removed(hdev);
404209fd0de5SGustavo F. Padovan 		hci_dev_unlock(hdev);
404356e5cb86SJohan Hedberg 	}
4044ab81cbf9SJohan Hedberg 
40452e58ef3eSJohan Hedberg 	/* mgmt_index_removed should take care of emptying the
40462e58ef3eSJohan Hedberg 	 * pending list */
40472e58ef3eSJohan Hedberg 	BUG_ON(!list_empty(&hdev->mgmt_pending));
40482e58ef3eSJohan Hedberg 
40491da177e4SLinus Torvalds 	hci_notify(hdev, HCI_DEV_UNREG);
40501da177e4SLinus Torvalds 
4051611b30f7SMarcel Holtmann 	if (hdev->rfkill) {
4052611b30f7SMarcel Holtmann 		rfkill_unregister(hdev->rfkill);
4053611b30f7SMarcel Holtmann 		rfkill_destroy(hdev->rfkill);
4054611b30f7SMarcel Holtmann 	}
4055611b30f7SMarcel Holtmann 
405699780a7bSJohan Hedberg 	if (hdev->tfm_aes)
405799780a7bSJohan Hedberg 		crypto_free_blkcipher(hdev->tfm_aes);
405899780a7bSJohan Hedberg 
4059bdc3e0f1SMarcel Holtmann 	device_del(&hdev->dev);
4060147e2d59SDave Young 
40610153e2ecSMarcel Holtmann 	debugfs_remove_recursive(hdev->debugfs);
40620153e2ecSMarcel Holtmann 
4063f48fd9c8SMarcel Holtmann 	destroy_workqueue(hdev->workqueue);
40646ead1bbcSJohan Hedberg 	destroy_workqueue(hdev->req_workqueue);
4065f48fd9c8SMarcel Holtmann 
406609fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
4067e2e0cacbSJohan Hedberg 	hci_blacklist_clear(hdev);
40682aeb9a1aSJohan Hedberg 	hci_uuids_clear(hdev);
406955ed8ca1SJohan Hedberg 	hci_link_keys_clear(hdev);
4070b899efafSVinicius Costa Gomes 	hci_smp_ltks_clear(hdev);
4071970c4e46SJohan Hedberg 	hci_smp_irks_clear(hdev);
40722763eda6SSzymon Janc 	hci_remote_oob_data_clear(hdev);
4073d2ab0ac1SMarcel Holtmann 	hci_white_list_clear(hdev);
4074373110c5SJohan Hedberg 	hci_conn_params_clear_all(hdev);
407509fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
4076e2e0cacbSJohan Hedberg 
4077dc946bd8SDavid Herrmann 	hci_dev_put(hdev);
40783df92b31SSasha Levin 
40793df92b31SSasha Levin 	ida_simple_remove(&hci_index_ida, id);
40801da177e4SLinus Torvalds }
40811da177e4SLinus Torvalds EXPORT_SYMBOL(hci_unregister_dev);
40821da177e4SLinus Torvalds 
40831da177e4SLinus Torvalds /* Suspend HCI device */
40841da177e4SLinus Torvalds int hci_suspend_dev(struct hci_dev *hdev)
40851da177e4SLinus Torvalds {
40861da177e4SLinus Torvalds 	hci_notify(hdev, HCI_DEV_SUSPEND);
40871da177e4SLinus Torvalds 	return 0;
40881da177e4SLinus Torvalds }
40891da177e4SLinus Torvalds EXPORT_SYMBOL(hci_suspend_dev);
40901da177e4SLinus Torvalds 
40911da177e4SLinus Torvalds /* Resume HCI device */
40921da177e4SLinus Torvalds int hci_resume_dev(struct hci_dev *hdev)
40931da177e4SLinus Torvalds {
40941da177e4SLinus Torvalds 	hci_notify(hdev, HCI_DEV_RESUME);
40951da177e4SLinus Torvalds 	return 0;
40961da177e4SLinus Torvalds }
40971da177e4SLinus Torvalds EXPORT_SYMBOL(hci_resume_dev);
40981da177e4SLinus Torvalds 
409976bca880SMarcel Holtmann /* Receive frame from HCI drivers */
4100e1a26170SMarcel Holtmann int hci_recv_frame(struct hci_dev *hdev, struct sk_buff *skb)
410176bca880SMarcel Holtmann {
410276bca880SMarcel Holtmann 	if (!hdev || (!test_bit(HCI_UP, &hdev->flags)
410376bca880SMarcel Holtmann 		      && !test_bit(HCI_INIT, &hdev->flags))) {
410476bca880SMarcel Holtmann 		kfree_skb(skb);
410576bca880SMarcel Holtmann 		return -ENXIO;
410676bca880SMarcel Holtmann 	}
410776bca880SMarcel Holtmann 
4108d82603c6SJorrit Schippers 	/* Incoming skb */
410976bca880SMarcel Holtmann 	bt_cb(skb)->incoming = 1;
411076bca880SMarcel Holtmann 
411176bca880SMarcel Holtmann 	/* Time stamp */
411276bca880SMarcel Holtmann 	__net_timestamp(skb);
411376bca880SMarcel Holtmann 
411476bca880SMarcel Holtmann 	skb_queue_tail(&hdev->rx_q, skb);
4115b78752ccSMarcel Holtmann 	queue_work(hdev->workqueue, &hdev->rx_work);
4116c78ae283SMarcel Holtmann 
411776bca880SMarcel Holtmann 	return 0;
411876bca880SMarcel Holtmann }
411976bca880SMarcel Holtmann EXPORT_SYMBOL(hci_recv_frame);
412076bca880SMarcel Holtmann 
412133e882a5SSuraj Sumangala static int hci_reassembly(struct hci_dev *hdev, int type, void *data,
41221e429f38SGustavo F. Padovan 			  int count, __u8 index)
412333e882a5SSuraj Sumangala {
412433e882a5SSuraj Sumangala 	int len = 0;
412533e882a5SSuraj Sumangala 	int hlen = 0;
412633e882a5SSuraj Sumangala 	int remain = count;
412733e882a5SSuraj Sumangala 	struct sk_buff *skb;
412833e882a5SSuraj Sumangala 	struct bt_skb_cb *scb;
412933e882a5SSuraj Sumangala 
413033e882a5SSuraj Sumangala 	if ((type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) ||
413133e882a5SSuraj Sumangala 	    index >= NUM_REASSEMBLY)
413233e882a5SSuraj Sumangala 		return -EILSEQ;
413333e882a5SSuraj Sumangala 
413433e882a5SSuraj Sumangala 	skb = hdev->reassembly[index];
413533e882a5SSuraj Sumangala 
413633e882a5SSuraj Sumangala 	if (!skb) {
413733e882a5SSuraj Sumangala 		switch (type) {
413833e882a5SSuraj Sumangala 		case HCI_ACLDATA_PKT:
413933e882a5SSuraj Sumangala 			len = HCI_MAX_FRAME_SIZE;
414033e882a5SSuraj Sumangala 			hlen = HCI_ACL_HDR_SIZE;
414133e882a5SSuraj Sumangala 			break;
414233e882a5SSuraj Sumangala 		case HCI_EVENT_PKT:
414333e882a5SSuraj Sumangala 			len = HCI_MAX_EVENT_SIZE;
414433e882a5SSuraj Sumangala 			hlen = HCI_EVENT_HDR_SIZE;
414533e882a5SSuraj Sumangala 			break;
414633e882a5SSuraj Sumangala 		case HCI_SCODATA_PKT:
414733e882a5SSuraj Sumangala 			len = HCI_MAX_SCO_SIZE;
414833e882a5SSuraj Sumangala 			hlen = HCI_SCO_HDR_SIZE;
414933e882a5SSuraj Sumangala 			break;
415033e882a5SSuraj Sumangala 		}
415133e882a5SSuraj Sumangala 
41521e429f38SGustavo F. Padovan 		skb = bt_skb_alloc(len, GFP_ATOMIC);
415333e882a5SSuraj Sumangala 		if (!skb)
415433e882a5SSuraj Sumangala 			return -ENOMEM;
415533e882a5SSuraj Sumangala 
415633e882a5SSuraj Sumangala 		scb = (void *) skb->cb;
415733e882a5SSuraj Sumangala 		scb->expect = hlen;
415833e882a5SSuraj Sumangala 		scb->pkt_type = type;
415933e882a5SSuraj Sumangala 
416033e882a5SSuraj Sumangala 		hdev->reassembly[index] = skb;
416133e882a5SSuraj Sumangala 	}
416233e882a5SSuraj Sumangala 
416333e882a5SSuraj Sumangala 	while (count) {
416433e882a5SSuraj Sumangala 		scb = (void *) skb->cb;
416589bb46d0SDan Carpenter 		len = min_t(uint, scb->expect, count);
416633e882a5SSuraj Sumangala 
416733e882a5SSuraj Sumangala 		memcpy(skb_put(skb, len), data, len);
416833e882a5SSuraj Sumangala 
416933e882a5SSuraj Sumangala 		count -= len;
417033e882a5SSuraj Sumangala 		data += len;
417133e882a5SSuraj Sumangala 		scb->expect -= len;
417233e882a5SSuraj Sumangala 		remain = count;
417333e882a5SSuraj Sumangala 
417433e882a5SSuraj Sumangala 		switch (type) {
417533e882a5SSuraj Sumangala 		case HCI_EVENT_PKT:
417633e882a5SSuraj Sumangala 			if (skb->len == HCI_EVENT_HDR_SIZE) {
417733e882a5SSuraj Sumangala 				struct hci_event_hdr *h = hci_event_hdr(skb);
417833e882a5SSuraj Sumangala 				scb->expect = h->plen;
417933e882a5SSuraj Sumangala 
418033e882a5SSuraj Sumangala 				if (skb_tailroom(skb) < scb->expect) {
418133e882a5SSuraj Sumangala 					kfree_skb(skb);
418233e882a5SSuraj Sumangala 					hdev->reassembly[index] = NULL;
418333e882a5SSuraj Sumangala 					return -ENOMEM;
418433e882a5SSuraj Sumangala 				}
418533e882a5SSuraj Sumangala 			}
418633e882a5SSuraj Sumangala 			break;
418733e882a5SSuraj Sumangala 
418833e882a5SSuraj Sumangala 		case HCI_ACLDATA_PKT:
418933e882a5SSuraj Sumangala 			if (skb->len  == HCI_ACL_HDR_SIZE) {
419033e882a5SSuraj Sumangala 				struct hci_acl_hdr *h = hci_acl_hdr(skb);
419133e882a5SSuraj Sumangala 				scb->expect = __le16_to_cpu(h->dlen);
419233e882a5SSuraj Sumangala 
419333e882a5SSuraj Sumangala 				if (skb_tailroom(skb) < scb->expect) {
419433e882a5SSuraj Sumangala 					kfree_skb(skb);
419533e882a5SSuraj Sumangala 					hdev->reassembly[index] = NULL;
419633e882a5SSuraj Sumangala 					return -ENOMEM;
419733e882a5SSuraj Sumangala 				}
419833e882a5SSuraj Sumangala 			}
419933e882a5SSuraj Sumangala 			break;
420033e882a5SSuraj Sumangala 
420133e882a5SSuraj Sumangala 		case HCI_SCODATA_PKT:
420233e882a5SSuraj Sumangala 			if (skb->len == HCI_SCO_HDR_SIZE) {
420333e882a5SSuraj Sumangala 				struct hci_sco_hdr *h = hci_sco_hdr(skb);
420433e882a5SSuraj Sumangala 				scb->expect = h->dlen;
420533e882a5SSuraj Sumangala 
420633e882a5SSuraj Sumangala 				if (skb_tailroom(skb) < scb->expect) {
420733e882a5SSuraj Sumangala 					kfree_skb(skb);
420833e882a5SSuraj Sumangala 					hdev->reassembly[index] = NULL;
420933e882a5SSuraj Sumangala 					return -ENOMEM;
421033e882a5SSuraj Sumangala 				}
421133e882a5SSuraj Sumangala 			}
421233e882a5SSuraj Sumangala 			break;
421333e882a5SSuraj Sumangala 		}
421433e882a5SSuraj Sumangala 
421533e882a5SSuraj Sumangala 		if (scb->expect == 0) {
421633e882a5SSuraj Sumangala 			/* Complete frame */
421733e882a5SSuraj Sumangala 
421833e882a5SSuraj Sumangala 			bt_cb(skb)->pkt_type = type;
4219e1a26170SMarcel Holtmann 			hci_recv_frame(hdev, skb);
422033e882a5SSuraj Sumangala 
422133e882a5SSuraj Sumangala 			hdev->reassembly[index] = NULL;
422233e882a5SSuraj Sumangala 			return remain;
422333e882a5SSuraj Sumangala 		}
422433e882a5SSuraj Sumangala 	}
422533e882a5SSuraj Sumangala 
422633e882a5SSuraj Sumangala 	return remain;
422733e882a5SSuraj Sumangala }
422833e882a5SSuraj Sumangala 
4229ef222013SMarcel Holtmann int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count)
4230ef222013SMarcel Holtmann {
4231f39a3c06SSuraj Sumangala 	int rem = 0;
4232f39a3c06SSuraj Sumangala 
4233ef222013SMarcel Holtmann 	if (type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT)
4234ef222013SMarcel Holtmann 		return -EILSEQ;
4235ef222013SMarcel Holtmann 
4236da5f6c37SGustavo F. Padovan 	while (count) {
42371e429f38SGustavo F. Padovan 		rem = hci_reassembly(hdev, type, data, count, type - 1);
4238f39a3c06SSuraj Sumangala 		if (rem < 0)
4239f39a3c06SSuraj Sumangala 			return rem;
4240ef222013SMarcel Holtmann 
4241f39a3c06SSuraj Sumangala 		data += (count - rem);
4242f39a3c06SSuraj Sumangala 		count = rem;
4243f81c6224SJoe Perches 	}
4244ef222013SMarcel Holtmann 
4245f39a3c06SSuraj Sumangala 	return rem;
4246ef222013SMarcel Holtmann }
4247ef222013SMarcel Holtmann EXPORT_SYMBOL(hci_recv_fragment);
4248ef222013SMarcel Holtmann 
424999811510SSuraj Sumangala #define STREAM_REASSEMBLY 0
425099811510SSuraj Sumangala 
425199811510SSuraj Sumangala int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count)
425299811510SSuraj Sumangala {
425399811510SSuraj Sumangala 	int type;
425499811510SSuraj Sumangala 	int rem = 0;
425599811510SSuraj Sumangala 
4256da5f6c37SGustavo F. Padovan 	while (count) {
425799811510SSuraj Sumangala 		struct sk_buff *skb = hdev->reassembly[STREAM_REASSEMBLY];
425899811510SSuraj Sumangala 
425999811510SSuraj Sumangala 		if (!skb) {
426099811510SSuraj Sumangala 			struct { char type; } *pkt;
426199811510SSuraj Sumangala 
426299811510SSuraj Sumangala 			/* Start of the frame */
426399811510SSuraj Sumangala 			pkt = data;
426499811510SSuraj Sumangala 			type = pkt->type;
426599811510SSuraj Sumangala 
426699811510SSuraj Sumangala 			data++;
426799811510SSuraj Sumangala 			count--;
426899811510SSuraj Sumangala 		} else
426999811510SSuraj Sumangala 			type = bt_cb(skb)->pkt_type;
427099811510SSuraj Sumangala 
42711e429f38SGustavo F. Padovan 		rem = hci_reassembly(hdev, type, data, count,
42721e429f38SGustavo F. Padovan 				     STREAM_REASSEMBLY);
427399811510SSuraj Sumangala 		if (rem < 0)
427499811510SSuraj Sumangala 			return rem;
427599811510SSuraj Sumangala 
427699811510SSuraj Sumangala 		data += (count - rem);
427799811510SSuraj Sumangala 		count = rem;
4278f81c6224SJoe Perches 	}
427999811510SSuraj Sumangala 
428099811510SSuraj Sumangala 	return rem;
428199811510SSuraj Sumangala }
428299811510SSuraj Sumangala EXPORT_SYMBOL(hci_recv_stream_fragment);
428399811510SSuraj Sumangala 
42841da177e4SLinus Torvalds /* ---- Interface to upper protocols ---- */
42851da177e4SLinus Torvalds 
42861da177e4SLinus Torvalds int hci_register_cb(struct hci_cb *cb)
42871da177e4SLinus Torvalds {
42881da177e4SLinus Torvalds 	BT_DBG("%p name %s", cb, cb->name);
42891da177e4SLinus Torvalds 
4290f20d09d5SGustavo F. Padovan 	write_lock(&hci_cb_list_lock);
42911da177e4SLinus Torvalds 	list_add(&cb->list, &hci_cb_list);
4292f20d09d5SGustavo F. Padovan 	write_unlock(&hci_cb_list_lock);
42931da177e4SLinus Torvalds 
42941da177e4SLinus Torvalds 	return 0;
42951da177e4SLinus Torvalds }
42961da177e4SLinus Torvalds EXPORT_SYMBOL(hci_register_cb);
42971da177e4SLinus Torvalds 
42981da177e4SLinus Torvalds int hci_unregister_cb(struct hci_cb *cb)
42991da177e4SLinus Torvalds {
43001da177e4SLinus Torvalds 	BT_DBG("%p name %s", cb, cb->name);
43011da177e4SLinus Torvalds 
4302f20d09d5SGustavo F. Padovan 	write_lock(&hci_cb_list_lock);
43031da177e4SLinus Torvalds 	list_del(&cb->list);
4304f20d09d5SGustavo F. Padovan 	write_unlock(&hci_cb_list_lock);
43051da177e4SLinus Torvalds 
43061da177e4SLinus Torvalds 	return 0;
43071da177e4SLinus Torvalds }
43081da177e4SLinus Torvalds EXPORT_SYMBOL(hci_unregister_cb);
43091da177e4SLinus Torvalds 
431051086991SMarcel Holtmann static void hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
43111da177e4SLinus Torvalds {
43120d48d939SMarcel Holtmann 	BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
43131da177e4SLinus Torvalds 
43141da177e4SLinus Torvalds 	/* Time stamp */
4315a61bbcf2SPatrick McHardy 	__net_timestamp(skb);
43161da177e4SLinus Torvalds 
4317cd82e61cSMarcel Holtmann 	/* Send copy to monitor */
4318cd82e61cSMarcel Holtmann 	hci_send_to_monitor(hdev, skb);
4319cd82e61cSMarcel Holtmann 
4320cd82e61cSMarcel Holtmann 	if (atomic_read(&hdev->promisc)) {
4321cd82e61cSMarcel Holtmann 		/* Send copy to the sockets */
4322470fe1b5SMarcel Holtmann 		hci_send_to_sock(hdev, skb);
43231da177e4SLinus Torvalds 	}
43241da177e4SLinus Torvalds 
43251da177e4SLinus Torvalds 	/* Get rid of skb owner, prior to sending to the driver. */
43261da177e4SLinus Torvalds 	skb_orphan(skb);
43271da177e4SLinus Torvalds 
43287bd8f09fSMarcel Holtmann 	if (hdev->send(hdev, skb) < 0)
432951086991SMarcel Holtmann 		BT_ERR("%s sending frame failed", hdev->name);
43301da177e4SLinus Torvalds }
43311da177e4SLinus Torvalds 
43323119ae95SJohan Hedberg void hci_req_init(struct hci_request *req, struct hci_dev *hdev)
43333119ae95SJohan Hedberg {
43343119ae95SJohan Hedberg 	skb_queue_head_init(&req->cmd_q);
43353119ae95SJohan Hedberg 	req->hdev = hdev;
43365d73e034SAndre Guedes 	req->err = 0;
43373119ae95SJohan Hedberg }
43383119ae95SJohan Hedberg 
43393119ae95SJohan Hedberg int hci_req_run(struct hci_request *req, hci_req_complete_t complete)
43403119ae95SJohan Hedberg {
43413119ae95SJohan Hedberg 	struct hci_dev *hdev = req->hdev;
43423119ae95SJohan Hedberg 	struct sk_buff *skb;
43433119ae95SJohan Hedberg 	unsigned long flags;
43443119ae95SJohan Hedberg 
43453119ae95SJohan Hedberg 	BT_DBG("length %u", skb_queue_len(&req->cmd_q));
43463119ae95SJohan Hedberg 
43475d73e034SAndre Guedes 	/* If an error occured during request building, remove all HCI
43485d73e034SAndre Guedes 	 * commands queued on the HCI request queue.
43495d73e034SAndre Guedes 	 */
43505d73e034SAndre Guedes 	if (req->err) {
43515d73e034SAndre Guedes 		skb_queue_purge(&req->cmd_q);
43525d73e034SAndre Guedes 		return req->err;
43535d73e034SAndre Guedes 	}
43545d73e034SAndre Guedes 
43553119ae95SJohan Hedberg 	/* Do not allow empty requests */
43563119ae95SJohan Hedberg 	if (skb_queue_empty(&req->cmd_q))
4357382b0c39SAndre Guedes 		return -ENODATA;
43583119ae95SJohan Hedberg 
43593119ae95SJohan Hedberg 	skb = skb_peek_tail(&req->cmd_q);
43603119ae95SJohan Hedberg 	bt_cb(skb)->req.complete = complete;
43613119ae95SJohan Hedberg 
43623119ae95SJohan Hedberg 	spin_lock_irqsave(&hdev->cmd_q.lock, flags);
43633119ae95SJohan Hedberg 	skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
43643119ae95SJohan Hedberg 	spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
43653119ae95SJohan Hedberg 
43663119ae95SJohan Hedberg 	queue_work(hdev->workqueue, &hdev->cmd_work);
43673119ae95SJohan Hedberg 
43683119ae95SJohan Hedberg 	return 0;
43693119ae95SJohan Hedberg }
43703119ae95SJohan Hedberg 
43711ca3a9d0SJohan Hedberg static struct sk_buff *hci_prepare_cmd(struct hci_dev *hdev, u16 opcode,
437207dc93ddSJohan Hedberg 				       u32 plen, const void *param)
43731da177e4SLinus Torvalds {
43741da177e4SLinus Torvalds 	int len = HCI_COMMAND_HDR_SIZE + plen;
43751da177e4SLinus Torvalds 	struct hci_command_hdr *hdr;
43761da177e4SLinus Torvalds 	struct sk_buff *skb;
43771da177e4SLinus Torvalds 
43781da177e4SLinus Torvalds 	skb = bt_skb_alloc(len, GFP_ATOMIC);
43791ca3a9d0SJohan Hedberg 	if (!skb)
43801ca3a9d0SJohan Hedberg 		return NULL;
43811da177e4SLinus Torvalds 
43821da177e4SLinus Torvalds 	hdr = (struct hci_command_hdr *) skb_put(skb, HCI_COMMAND_HDR_SIZE);
4383a9de9248SMarcel Holtmann 	hdr->opcode = cpu_to_le16(opcode);
43841da177e4SLinus Torvalds 	hdr->plen   = plen;
43851da177e4SLinus Torvalds 
43861da177e4SLinus Torvalds 	if (plen)
43871da177e4SLinus Torvalds 		memcpy(skb_put(skb, plen), param, plen);
43881da177e4SLinus Torvalds 
43891da177e4SLinus Torvalds 	BT_DBG("skb len %d", skb->len);
43901da177e4SLinus Torvalds 
43910d48d939SMarcel Holtmann 	bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
4392c78ae283SMarcel Holtmann 
43931ca3a9d0SJohan Hedberg 	return skb;
43941ca3a9d0SJohan Hedberg }
43951ca3a9d0SJohan Hedberg 
43961ca3a9d0SJohan Hedberg /* Send HCI command */
439707dc93ddSJohan Hedberg int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen,
439807dc93ddSJohan Hedberg 		 const void *param)
43991ca3a9d0SJohan Hedberg {
44001ca3a9d0SJohan Hedberg 	struct sk_buff *skb;
44011ca3a9d0SJohan Hedberg 
44021ca3a9d0SJohan Hedberg 	BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen);
44031ca3a9d0SJohan Hedberg 
44041ca3a9d0SJohan Hedberg 	skb = hci_prepare_cmd(hdev, opcode, plen, param);
44051ca3a9d0SJohan Hedberg 	if (!skb) {
44061ca3a9d0SJohan Hedberg 		BT_ERR("%s no memory for command", hdev->name);
44071ca3a9d0SJohan Hedberg 		return -ENOMEM;
44081ca3a9d0SJohan Hedberg 	}
44091ca3a9d0SJohan Hedberg 
441011714b3dSJohan Hedberg 	/* Stand-alone HCI commands must be flaged as
441111714b3dSJohan Hedberg 	 * single-command requests.
441211714b3dSJohan Hedberg 	 */
441311714b3dSJohan Hedberg 	bt_cb(skb)->req.start = true;
441411714b3dSJohan Hedberg 
44151da177e4SLinus Torvalds 	skb_queue_tail(&hdev->cmd_q, skb);
4416c347b765SGustavo F. Padovan 	queue_work(hdev->workqueue, &hdev->cmd_work);
44171da177e4SLinus Torvalds 
44181da177e4SLinus Torvalds 	return 0;
44191da177e4SLinus Torvalds }
44201da177e4SLinus Torvalds 
442171c76a17SJohan Hedberg /* Queue a command to an asynchronous HCI request */
442207dc93ddSJohan Hedberg void hci_req_add_ev(struct hci_request *req, u16 opcode, u32 plen,
442307dc93ddSJohan Hedberg 		    const void *param, u8 event)
442471c76a17SJohan Hedberg {
442571c76a17SJohan Hedberg 	struct hci_dev *hdev = req->hdev;
442671c76a17SJohan Hedberg 	struct sk_buff *skb;
442771c76a17SJohan Hedberg 
442871c76a17SJohan Hedberg 	BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen);
442971c76a17SJohan Hedberg 
443034739c1eSAndre Guedes 	/* If an error occured during request building, there is no point in
443134739c1eSAndre Guedes 	 * queueing the HCI command. We can simply return.
443234739c1eSAndre Guedes 	 */
443334739c1eSAndre Guedes 	if (req->err)
443434739c1eSAndre Guedes 		return;
443534739c1eSAndre Guedes 
443671c76a17SJohan Hedberg 	skb = hci_prepare_cmd(hdev, opcode, plen, param);
443771c76a17SJohan Hedberg 	if (!skb) {
44385d73e034SAndre Guedes 		BT_ERR("%s no memory for command (opcode 0x%4.4x)",
44395d73e034SAndre Guedes 		       hdev->name, opcode);
44405d73e034SAndre Guedes 		req->err = -ENOMEM;
4441e348fe6bSAndre Guedes 		return;
444271c76a17SJohan Hedberg 	}
444371c76a17SJohan Hedberg 
444471c76a17SJohan Hedberg 	if (skb_queue_empty(&req->cmd_q))
444571c76a17SJohan Hedberg 		bt_cb(skb)->req.start = true;
444671c76a17SJohan Hedberg 
444702350a72SJohan Hedberg 	bt_cb(skb)->req.event = event;
444802350a72SJohan Hedberg 
444971c76a17SJohan Hedberg 	skb_queue_tail(&req->cmd_q, skb);
445071c76a17SJohan Hedberg }
445171c76a17SJohan Hedberg 
445207dc93ddSJohan Hedberg void hci_req_add(struct hci_request *req, u16 opcode, u32 plen,
445307dc93ddSJohan Hedberg 		 const void *param)
445402350a72SJohan Hedberg {
445502350a72SJohan Hedberg 	hci_req_add_ev(req, opcode, plen, param, 0);
445602350a72SJohan Hedberg }
445702350a72SJohan Hedberg 
44581da177e4SLinus Torvalds /* Get data from the previously sent command */
4459a9de9248SMarcel Holtmann void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode)
44601da177e4SLinus Torvalds {
44611da177e4SLinus Torvalds 	struct hci_command_hdr *hdr;
44621da177e4SLinus Torvalds 
44631da177e4SLinus Torvalds 	if (!hdev->sent_cmd)
44641da177e4SLinus Torvalds 		return NULL;
44651da177e4SLinus Torvalds 
44661da177e4SLinus Torvalds 	hdr = (void *) hdev->sent_cmd->data;
44671da177e4SLinus Torvalds 
4468a9de9248SMarcel Holtmann 	if (hdr->opcode != cpu_to_le16(opcode))
44691da177e4SLinus Torvalds 		return NULL;
44701da177e4SLinus Torvalds 
4471f0e09510SAndrei Emeltchenko 	BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode);
44721da177e4SLinus Torvalds 
44731da177e4SLinus Torvalds 	return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE;
44741da177e4SLinus Torvalds }
44751da177e4SLinus Torvalds 
44761da177e4SLinus Torvalds /* Send ACL data */
44771da177e4SLinus Torvalds static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags)
44781da177e4SLinus Torvalds {
44791da177e4SLinus Torvalds 	struct hci_acl_hdr *hdr;
44801da177e4SLinus Torvalds 	int len = skb->len;
44811da177e4SLinus Torvalds 
4482badff6d0SArnaldo Carvalho de Melo 	skb_push(skb, HCI_ACL_HDR_SIZE);
4483badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
44849c70220bSArnaldo Carvalho de Melo 	hdr = (struct hci_acl_hdr *)skb_transport_header(skb);
4485aca3192cSYOSHIFUJI Hideaki 	hdr->handle = cpu_to_le16(hci_handle_pack(handle, flags));
4486aca3192cSYOSHIFUJI Hideaki 	hdr->dlen   = cpu_to_le16(len);
44871da177e4SLinus Torvalds }
44881da177e4SLinus Torvalds 
4489ee22be7eSAndrei Emeltchenko static void hci_queue_acl(struct hci_chan *chan, struct sk_buff_head *queue,
449073d80debSLuiz Augusto von Dentz 			  struct sk_buff *skb, __u16 flags)
44911da177e4SLinus Torvalds {
4492ee22be7eSAndrei Emeltchenko 	struct hci_conn *conn = chan->conn;
44931da177e4SLinus Torvalds 	struct hci_dev *hdev = conn->hdev;
44941da177e4SLinus Torvalds 	struct sk_buff *list;
44951da177e4SLinus Torvalds 
4496087bfd99SGustavo Padovan 	skb->len = skb_headlen(skb);
4497087bfd99SGustavo Padovan 	skb->data_len = 0;
4498087bfd99SGustavo Padovan 
4499087bfd99SGustavo Padovan 	bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
4500204a6e54SAndrei Emeltchenko 
4501204a6e54SAndrei Emeltchenko 	switch (hdev->dev_type) {
4502204a6e54SAndrei Emeltchenko 	case HCI_BREDR:
4503087bfd99SGustavo Padovan 		hci_add_acl_hdr(skb, conn->handle, flags);
4504204a6e54SAndrei Emeltchenko 		break;
4505204a6e54SAndrei Emeltchenko 	case HCI_AMP:
4506204a6e54SAndrei Emeltchenko 		hci_add_acl_hdr(skb, chan->handle, flags);
4507204a6e54SAndrei Emeltchenko 		break;
4508204a6e54SAndrei Emeltchenko 	default:
4509204a6e54SAndrei Emeltchenko 		BT_ERR("%s unknown dev_type %d", hdev->name, hdev->dev_type);
4510204a6e54SAndrei Emeltchenko 		return;
4511204a6e54SAndrei Emeltchenko 	}
4512087bfd99SGustavo Padovan 
451370f23020SAndrei Emeltchenko 	list = skb_shinfo(skb)->frag_list;
451470f23020SAndrei Emeltchenko 	if (!list) {
45151da177e4SLinus Torvalds 		/* Non fragmented */
45161da177e4SLinus Torvalds 		BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len);
45171da177e4SLinus Torvalds 
451873d80debSLuiz Augusto von Dentz 		skb_queue_tail(queue, skb);
45191da177e4SLinus Torvalds 	} else {
45201da177e4SLinus Torvalds 		/* Fragmented */
45211da177e4SLinus Torvalds 		BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
45221da177e4SLinus Torvalds 
45231da177e4SLinus Torvalds 		skb_shinfo(skb)->frag_list = NULL;
45241da177e4SLinus Torvalds 
45251da177e4SLinus Torvalds 		/* Queue all fragments atomically */
4526af3e6359SGustavo F. Padovan 		spin_lock(&queue->lock);
45271da177e4SLinus Torvalds 
452873d80debSLuiz Augusto von Dentz 		__skb_queue_tail(queue, skb);
4529e702112fSAndrei Emeltchenko 
4530e702112fSAndrei Emeltchenko 		flags &= ~ACL_START;
4531e702112fSAndrei Emeltchenko 		flags |= ACL_CONT;
45321da177e4SLinus Torvalds 		do {
45331da177e4SLinus Torvalds 			skb = list; list = list->next;
45341da177e4SLinus Torvalds 
45350d48d939SMarcel Holtmann 			bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
4536e702112fSAndrei Emeltchenko 			hci_add_acl_hdr(skb, conn->handle, flags);
45371da177e4SLinus Torvalds 
45381da177e4SLinus Torvalds 			BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
45391da177e4SLinus Torvalds 
454073d80debSLuiz Augusto von Dentz 			__skb_queue_tail(queue, skb);
45411da177e4SLinus Torvalds 		} while (list);
45421da177e4SLinus Torvalds 
4543af3e6359SGustavo F. Padovan 		spin_unlock(&queue->lock);
45441da177e4SLinus Torvalds 	}
454573d80debSLuiz Augusto von Dentz }
454673d80debSLuiz Augusto von Dentz 
454773d80debSLuiz Augusto von Dentz void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags)
454873d80debSLuiz Augusto von Dentz {
4549ee22be7eSAndrei Emeltchenko 	struct hci_dev *hdev = chan->conn->hdev;
455073d80debSLuiz Augusto von Dentz 
4551f0e09510SAndrei Emeltchenko 	BT_DBG("%s chan %p flags 0x%4.4x", hdev->name, chan, flags);
455273d80debSLuiz Augusto von Dentz 
4553ee22be7eSAndrei Emeltchenko 	hci_queue_acl(chan, &chan->data_q, skb, flags);
45541da177e4SLinus Torvalds 
45553eff45eaSGustavo F. Padovan 	queue_work(hdev->workqueue, &hdev->tx_work);
45561da177e4SLinus Torvalds }
45571da177e4SLinus Torvalds 
45581da177e4SLinus Torvalds /* Send SCO data */
45590d861d8bSGustavo F. Padovan void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb)
45601da177e4SLinus Torvalds {
45611da177e4SLinus Torvalds 	struct hci_dev *hdev = conn->hdev;
45621da177e4SLinus Torvalds 	struct hci_sco_hdr hdr;
45631da177e4SLinus Torvalds 
45641da177e4SLinus Torvalds 	BT_DBG("%s len %d", hdev->name, skb->len);
45651da177e4SLinus Torvalds 
4566aca3192cSYOSHIFUJI Hideaki 	hdr.handle = cpu_to_le16(conn->handle);
45671da177e4SLinus Torvalds 	hdr.dlen   = skb->len;
45681da177e4SLinus Torvalds 
4569badff6d0SArnaldo Carvalho de Melo 	skb_push(skb, HCI_SCO_HDR_SIZE);
4570badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
45719c70220bSArnaldo Carvalho de Melo 	memcpy(skb_transport_header(skb), &hdr, HCI_SCO_HDR_SIZE);
45721da177e4SLinus Torvalds 
45730d48d939SMarcel Holtmann 	bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
4574c78ae283SMarcel Holtmann 
45751da177e4SLinus Torvalds 	skb_queue_tail(&conn->data_q, skb);
45763eff45eaSGustavo F. Padovan 	queue_work(hdev->workqueue, &hdev->tx_work);
45771da177e4SLinus Torvalds }
45781da177e4SLinus Torvalds 
45791da177e4SLinus Torvalds /* ---- HCI TX task (outgoing data) ---- */
45801da177e4SLinus Torvalds 
45811da177e4SLinus Torvalds /* HCI Connection scheduler */
45826039aa73SGustavo Padovan static struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type,
4583a8c5fb1aSGustavo Padovan 				     int *quote)
45841da177e4SLinus Torvalds {
45851da177e4SLinus Torvalds 	struct hci_conn_hash *h = &hdev->conn_hash;
45868035ded4SLuiz Augusto von Dentz 	struct hci_conn *conn = NULL, *c;
4587abc5de8fSMikel Astiz 	unsigned int num = 0, min = ~0;
45881da177e4SLinus Torvalds 
45891da177e4SLinus Torvalds 	/* We don't have to lock device here. Connections are always
45901da177e4SLinus Torvalds 	 * added and removed with TX task disabled. */
4591bf4c6325SGustavo F. Padovan 
4592bf4c6325SGustavo F. Padovan 	rcu_read_lock();
4593bf4c6325SGustavo F. Padovan 
4594bf4c6325SGustavo F. Padovan 	list_for_each_entry_rcu(c, &h->list, list) {
4595769be974SMarcel Holtmann 		if (c->type != type || skb_queue_empty(&c->data_q))
45961da177e4SLinus Torvalds 			continue;
4597769be974SMarcel Holtmann 
4598769be974SMarcel Holtmann 		if (c->state != BT_CONNECTED && c->state != BT_CONFIG)
4599769be974SMarcel Holtmann 			continue;
4600769be974SMarcel Holtmann 
46011da177e4SLinus Torvalds 		num++;
46021da177e4SLinus Torvalds 
46031da177e4SLinus Torvalds 		if (c->sent < min) {
46041da177e4SLinus Torvalds 			min  = c->sent;
46051da177e4SLinus Torvalds 			conn = c;
46061da177e4SLinus Torvalds 		}
460752087a79SLuiz Augusto von Dentz 
460852087a79SLuiz Augusto von Dentz 		if (hci_conn_num(hdev, type) == num)
460952087a79SLuiz Augusto von Dentz 			break;
46101da177e4SLinus Torvalds 	}
46111da177e4SLinus Torvalds 
4612bf4c6325SGustavo F. Padovan 	rcu_read_unlock();
4613bf4c6325SGustavo F. Padovan 
46141da177e4SLinus Torvalds 	if (conn) {
46156ed58ec5SVille Tervo 		int cnt, q;
46166ed58ec5SVille Tervo 
46176ed58ec5SVille Tervo 		switch (conn->type) {
46186ed58ec5SVille Tervo 		case ACL_LINK:
46196ed58ec5SVille Tervo 			cnt = hdev->acl_cnt;
46206ed58ec5SVille Tervo 			break;
46216ed58ec5SVille Tervo 		case SCO_LINK:
46226ed58ec5SVille Tervo 		case ESCO_LINK:
46236ed58ec5SVille Tervo 			cnt = hdev->sco_cnt;
46246ed58ec5SVille Tervo 			break;
46256ed58ec5SVille Tervo 		case LE_LINK:
46266ed58ec5SVille Tervo 			cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt;
46276ed58ec5SVille Tervo 			break;
46286ed58ec5SVille Tervo 		default:
46296ed58ec5SVille Tervo 			cnt = 0;
46306ed58ec5SVille Tervo 			BT_ERR("Unknown link type");
46316ed58ec5SVille Tervo 		}
46326ed58ec5SVille Tervo 
46336ed58ec5SVille Tervo 		q = cnt / num;
46341da177e4SLinus Torvalds 		*quote = q ? q : 1;
46351da177e4SLinus Torvalds 	} else
46361da177e4SLinus Torvalds 		*quote = 0;
46371da177e4SLinus Torvalds 
46381da177e4SLinus Torvalds 	BT_DBG("conn %p quote %d", conn, *quote);
46391da177e4SLinus Torvalds 	return conn;
46401da177e4SLinus Torvalds }
46411da177e4SLinus Torvalds 
46426039aa73SGustavo Padovan static void hci_link_tx_to(struct hci_dev *hdev, __u8 type)
46431da177e4SLinus Torvalds {
46441da177e4SLinus Torvalds 	struct hci_conn_hash *h = &hdev->conn_hash;
46451da177e4SLinus Torvalds 	struct hci_conn *c;
46461da177e4SLinus Torvalds 
4647bae1f5d9SVille Tervo 	BT_ERR("%s link tx timeout", hdev->name);
46481da177e4SLinus Torvalds 
4649bf4c6325SGustavo F. Padovan 	rcu_read_lock();
4650bf4c6325SGustavo F. Padovan 
46511da177e4SLinus Torvalds 	/* Kill stalled connections */
4652bf4c6325SGustavo F. Padovan 	list_for_each_entry_rcu(c, &h->list, list) {
4653bae1f5d9SVille Tervo 		if (c->type == type && c->sent) {
46546ed93dc6SAndrei Emeltchenko 			BT_ERR("%s killing stalled connection %pMR",
46556ed93dc6SAndrei Emeltchenko 			       hdev->name, &c->dst);
4656bed71748SAndre Guedes 			hci_disconnect(c, HCI_ERROR_REMOTE_USER_TERM);
46571da177e4SLinus Torvalds 		}
46581da177e4SLinus Torvalds 	}
4659bf4c6325SGustavo F. Padovan 
4660bf4c6325SGustavo F. Padovan 	rcu_read_unlock();
46611da177e4SLinus Torvalds }
46621da177e4SLinus Torvalds 
46636039aa73SGustavo Padovan static struct hci_chan *hci_chan_sent(struct hci_dev *hdev, __u8 type,
466473d80debSLuiz Augusto von Dentz 				      int *quote)
466573d80debSLuiz Augusto von Dentz {
466673d80debSLuiz Augusto von Dentz 	struct hci_conn_hash *h = &hdev->conn_hash;
466773d80debSLuiz Augusto von Dentz 	struct hci_chan *chan = NULL;
4668abc5de8fSMikel Astiz 	unsigned int num = 0, min = ~0, cur_prio = 0;
466973d80debSLuiz Augusto von Dentz 	struct hci_conn *conn;
467073d80debSLuiz Augusto von Dentz 	int cnt, q, conn_num = 0;
467173d80debSLuiz Augusto von Dentz 
467273d80debSLuiz Augusto von Dentz 	BT_DBG("%s", hdev->name);
467373d80debSLuiz Augusto von Dentz 
4674bf4c6325SGustavo F. Padovan 	rcu_read_lock();
4675bf4c6325SGustavo F. Padovan 
4676bf4c6325SGustavo F. Padovan 	list_for_each_entry_rcu(conn, &h->list, list) {
467773d80debSLuiz Augusto von Dentz 		struct hci_chan *tmp;
467873d80debSLuiz Augusto von Dentz 
467973d80debSLuiz Augusto von Dentz 		if (conn->type != type)
468073d80debSLuiz Augusto von Dentz 			continue;
468173d80debSLuiz Augusto von Dentz 
468273d80debSLuiz Augusto von Dentz 		if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
468373d80debSLuiz Augusto von Dentz 			continue;
468473d80debSLuiz Augusto von Dentz 
468573d80debSLuiz Augusto von Dentz 		conn_num++;
468673d80debSLuiz Augusto von Dentz 
46878192edefSGustavo F. Padovan 		list_for_each_entry_rcu(tmp, &conn->chan_list, list) {
468873d80debSLuiz Augusto von Dentz 			struct sk_buff *skb;
468973d80debSLuiz Augusto von Dentz 
469073d80debSLuiz Augusto von Dentz 			if (skb_queue_empty(&tmp->data_q))
469173d80debSLuiz Augusto von Dentz 				continue;
469273d80debSLuiz Augusto von Dentz 
469373d80debSLuiz Augusto von Dentz 			skb = skb_peek(&tmp->data_q);
469473d80debSLuiz Augusto von Dentz 			if (skb->priority < cur_prio)
469573d80debSLuiz Augusto von Dentz 				continue;
469673d80debSLuiz Augusto von Dentz 
469773d80debSLuiz Augusto von Dentz 			if (skb->priority > cur_prio) {
469873d80debSLuiz Augusto von Dentz 				num = 0;
469973d80debSLuiz Augusto von Dentz 				min = ~0;
470073d80debSLuiz Augusto von Dentz 				cur_prio = skb->priority;
470173d80debSLuiz Augusto von Dentz 			}
470273d80debSLuiz Augusto von Dentz 
470373d80debSLuiz Augusto von Dentz 			num++;
470473d80debSLuiz Augusto von Dentz 
470573d80debSLuiz Augusto von Dentz 			if (conn->sent < min) {
470673d80debSLuiz Augusto von Dentz 				min  = conn->sent;
470773d80debSLuiz Augusto von Dentz 				chan = tmp;
470873d80debSLuiz Augusto von Dentz 			}
470973d80debSLuiz Augusto von Dentz 		}
471073d80debSLuiz Augusto von Dentz 
471173d80debSLuiz Augusto von Dentz 		if (hci_conn_num(hdev, type) == conn_num)
471273d80debSLuiz Augusto von Dentz 			break;
471373d80debSLuiz Augusto von Dentz 	}
471473d80debSLuiz Augusto von Dentz 
4715bf4c6325SGustavo F. Padovan 	rcu_read_unlock();
4716bf4c6325SGustavo F. Padovan 
471773d80debSLuiz Augusto von Dentz 	if (!chan)
471873d80debSLuiz Augusto von Dentz 		return NULL;
471973d80debSLuiz Augusto von Dentz 
472073d80debSLuiz Augusto von Dentz 	switch (chan->conn->type) {
472173d80debSLuiz Augusto von Dentz 	case ACL_LINK:
472273d80debSLuiz Augusto von Dentz 		cnt = hdev->acl_cnt;
472373d80debSLuiz Augusto von Dentz 		break;
4724bd1eb66bSAndrei Emeltchenko 	case AMP_LINK:
4725bd1eb66bSAndrei Emeltchenko 		cnt = hdev->block_cnt;
4726bd1eb66bSAndrei Emeltchenko 		break;
472773d80debSLuiz Augusto von Dentz 	case SCO_LINK:
472873d80debSLuiz Augusto von Dentz 	case ESCO_LINK:
472973d80debSLuiz Augusto von Dentz 		cnt = hdev->sco_cnt;
473073d80debSLuiz Augusto von Dentz 		break;
473173d80debSLuiz Augusto von Dentz 	case LE_LINK:
473273d80debSLuiz Augusto von Dentz 		cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt;
473373d80debSLuiz Augusto von Dentz 		break;
473473d80debSLuiz Augusto von Dentz 	default:
473573d80debSLuiz Augusto von Dentz 		cnt = 0;
473673d80debSLuiz Augusto von Dentz 		BT_ERR("Unknown link type");
473773d80debSLuiz Augusto von Dentz 	}
473873d80debSLuiz Augusto von Dentz 
473973d80debSLuiz Augusto von Dentz 	q = cnt / num;
474073d80debSLuiz Augusto von Dentz 	*quote = q ? q : 1;
474173d80debSLuiz Augusto von Dentz 	BT_DBG("chan %p quote %d", chan, *quote);
474273d80debSLuiz Augusto von Dentz 	return chan;
474373d80debSLuiz Augusto von Dentz }
474473d80debSLuiz Augusto von Dentz 
474502b20f0bSLuiz Augusto von Dentz static void hci_prio_recalculate(struct hci_dev *hdev, __u8 type)
474602b20f0bSLuiz Augusto von Dentz {
474702b20f0bSLuiz Augusto von Dentz 	struct hci_conn_hash *h = &hdev->conn_hash;
474802b20f0bSLuiz Augusto von Dentz 	struct hci_conn *conn;
474902b20f0bSLuiz Augusto von Dentz 	int num = 0;
475002b20f0bSLuiz Augusto von Dentz 
475102b20f0bSLuiz Augusto von Dentz 	BT_DBG("%s", hdev->name);
475202b20f0bSLuiz Augusto von Dentz 
4753bf4c6325SGustavo F. Padovan 	rcu_read_lock();
4754bf4c6325SGustavo F. Padovan 
4755bf4c6325SGustavo F. Padovan 	list_for_each_entry_rcu(conn, &h->list, list) {
475602b20f0bSLuiz Augusto von Dentz 		struct hci_chan *chan;
475702b20f0bSLuiz Augusto von Dentz 
475802b20f0bSLuiz Augusto von Dentz 		if (conn->type != type)
475902b20f0bSLuiz Augusto von Dentz 			continue;
476002b20f0bSLuiz Augusto von Dentz 
476102b20f0bSLuiz Augusto von Dentz 		if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
476202b20f0bSLuiz Augusto von Dentz 			continue;
476302b20f0bSLuiz Augusto von Dentz 
476402b20f0bSLuiz Augusto von Dentz 		num++;
476502b20f0bSLuiz Augusto von Dentz 
47668192edefSGustavo F. Padovan 		list_for_each_entry_rcu(chan, &conn->chan_list, list) {
476702b20f0bSLuiz Augusto von Dentz 			struct sk_buff *skb;
476802b20f0bSLuiz Augusto von Dentz 
476902b20f0bSLuiz Augusto von Dentz 			if (chan->sent) {
477002b20f0bSLuiz Augusto von Dentz 				chan->sent = 0;
477102b20f0bSLuiz Augusto von Dentz 				continue;
477202b20f0bSLuiz Augusto von Dentz 			}
477302b20f0bSLuiz Augusto von Dentz 
477402b20f0bSLuiz Augusto von Dentz 			if (skb_queue_empty(&chan->data_q))
477502b20f0bSLuiz Augusto von Dentz 				continue;
477602b20f0bSLuiz Augusto von Dentz 
477702b20f0bSLuiz Augusto von Dentz 			skb = skb_peek(&chan->data_q);
477802b20f0bSLuiz Augusto von Dentz 			if (skb->priority >= HCI_PRIO_MAX - 1)
477902b20f0bSLuiz Augusto von Dentz 				continue;
478002b20f0bSLuiz Augusto von Dentz 
478102b20f0bSLuiz Augusto von Dentz 			skb->priority = HCI_PRIO_MAX - 1;
478202b20f0bSLuiz Augusto von Dentz 
478302b20f0bSLuiz Augusto von Dentz 			BT_DBG("chan %p skb %p promoted to %d", chan, skb,
478402b20f0bSLuiz Augusto von Dentz 			       skb->priority);
478502b20f0bSLuiz Augusto von Dentz 		}
478602b20f0bSLuiz Augusto von Dentz 
478702b20f0bSLuiz Augusto von Dentz 		if (hci_conn_num(hdev, type) == num)
478802b20f0bSLuiz Augusto von Dentz 			break;
478902b20f0bSLuiz Augusto von Dentz 	}
4790bf4c6325SGustavo F. Padovan 
4791bf4c6325SGustavo F. Padovan 	rcu_read_unlock();
4792bf4c6325SGustavo F. Padovan 
479302b20f0bSLuiz Augusto von Dentz }
479402b20f0bSLuiz Augusto von Dentz 
4795b71d385aSAndrei Emeltchenko static inline int __get_blocks(struct hci_dev *hdev, struct sk_buff *skb)
4796b71d385aSAndrei Emeltchenko {
4797b71d385aSAndrei Emeltchenko 	/* Calculate count of blocks used by this packet */
4798b71d385aSAndrei Emeltchenko 	return DIV_ROUND_UP(skb->len - HCI_ACL_HDR_SIZE, hdev->block_len);
4799b71d385aSAndrei Emeltchenko }
4800b71d385aSAndrei Emeltchenko 
48016039aa73SGustavo Padovan static void __check_timeout(struct hci_dev *hdev, unsigned int cnt)
48021da177e4SLinus Torvalds {
4803fee746b0SMarcel Holtmann 	if (!test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) {
48041da177e4SLinus Torvalds 		/* ACL tx timeout must be longer than maximum
48051da177e4SLinus Torvalds 		 * link supervision timeout (40.9 seconds) */
480663d2bc1bSAndrei Emeltchenko 		if (!cnt && time_after(jiffies, hdev->acl_last_tx +
48075f246e89SAndrei Emeltchenko 				       HCI_ACL_TX_TIMEOUT))
4808bae1f5d9SVille Tervo 			hci_link_tx_to(hdev, ACL_LINK);
48091da177e4SLinus Torvalds 	}
481063d2bc1bSAndrei Emeltchenko }
48111da177e4SLinus Torvalds 
48126039aa73SGustavo Padovan static void hci_sched_acl_pkt(struct hci_dev *hdev)
481363d2bc1bSAndrei Emeltchenko {
481463d2bc1bSAndrei Emeltchenko 	unsigned int cnt = hdev->acl_cnt;
481563d2bc1bSAndrei Emeltchenko 	struct hci_chan *chan;
481663d2bc1bSAndrei Emeltchenko 	struct sk_buff *skb;
481763d2bc1bSAndrei Emeltchenko 	int quote;
481863d2bc1bSAndrei Emeltchenko 
481963d2bc1bSAndrei Emeltchenko 	__check_timeout(hdev, cnt);
482004837f64SMarcel Holtmann 
482173d80debSLuiz Augusto von Dentz 	while (hdev->acl_cnt &&
482273d80debSLuiz Augusto von Dentz 	       (chan = hci_chan_sent(hdev, ACL_LINK, &quote))) {
4823ec1cce24SLuiz Augusto von Dentz 		u32 priority = (skb_peek(&chan->data_q))->priority;
4824ec1cce24SLuiz Augusto von Dentz 		while (quote-- && (skb = skb_peek(&chan->data_q))) {
482573d80debSLuiz Augusto von Dentz 			BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
482673d80debSLuiz Augusto von Dentz 			       skb->len, skb->priority);
482773d80debSLuiz Augusto von Dentz 
4828ec1cce24SLuiz Augusto von Dentz 			/* Stop if priority has changed */
4829ec1cce24SLuiz Augusto von Dentz 			if (skb->priority < priority)
4830ec1cce24SLuiz Augusto von Dentz 				break;
4831ec1cce24SLuiz Augusto von Dentz 
4832ec1cce24SLuiz Augusto von Dentz 			skb = skb_dequeue(&chan->data_q);
4833ec1cce24SLuiz Augusto von Dentz 
483473d80debSLuiz Augusto von Dentz 			hci_conn_enter_active_mode(chan->conn,
483573d80debSLuiz Augusto von Dentz 						   bt_cb(skb)->force_active);
483604837f64SMarcel Holtmann 
483757d17d70SMarcel Holtmann 			hci_send_frame(hdev, skb);
48381da177e4SLinus Torvalds 			hdev->acl_last_tx = jiffies;
48391da177e4SLinus Torvalds 
48401da177e4SLinus Torvalds 			hdev->acl_cnt--;
484173d80debSLuiz Augusto von Dentz 			chan->sent++;
484273d80debSLuiz Augusto von Dentz 			chan->conn->sent++;
48431da177e4SLinus Torvalds 		}
48441da177e4SLinus Torvalds 	}
484502b20f0bSLuiz Augusto von Dentz 
484602b20f0bSLuiz Augusto von Dentz 	if (cnt != hdev->acl_cnt)
484702b20f0bSLuiz Augusto von Dentz 		hci_prio_recalculate(hdev, ACL_LINK);
48481da177e4SLinus Torvalds }
48491da177e4SLinus Torvalds 
48506039aa73SGustavo Padovan static void hci_sched_acl_blk(struct hci_dev *hdev)
4851b71d385aSAndrei Emeltchenko {
485263d2bc1bSAndrei Emeltchenko 	unsigned int cnt = hdev->block_cnt;
4853b71d385aSAndrei Emeltchenko 	struct hci_chan *chan;
4854b71d385aSAndrei Emeltchenko 	struct sk_buff *skb;
4855b71d385aSAndrei Emeltchenko 	int quote;
4856bd1eb66bSAndrei Emeltchenko 	u8 type;
4857b71d385aSAndrei Emeltchenko 
485863d2bc1bSAndrei Emeltchenko 	__check_timeout(hdev, cnt);
4859b71d385aSAndrei Emeltchenko 
4860bd1eb66bSAndrei Emeltchenko 	BT_DBG("%s", hdev->name);
4861bd1eb66bSAndrei Emeltchenko 
4862bd1eb66bSAndrei Emeltchenko 	if (hdev->dev_type == HCI_AMP)
4863bd1eb66bSAndrei Emeltchenko 		type = AMP_LINK;
4864bd1eb66bSAndrei Emeltchenko 	else
4865bd1eb66bSAndrei Emeltchenko 		type = ACL_LINK;
4866bd1eb66bSAndrei Emeltchenko 
4867b71d385aSAndrei Emeltchenko 	while (hdev->block_cnt > 0 &&
4868bd1eb66bSAndrei Emeltchenko 	       (chan = hci_chan_sent(hdev, type, &quote))) {
4869b71d385aSAndrei Emeltchenko 		u32 priority = (skb_peek(&chan->data_q))->priority;
4870b71d385aSAndrei Emeltchenko 		while (quote > 0 && (skb = skb_peek(&chan->data_q))) {
4871b71d385aSAndrei Emeltchenko 			int blocks;
4872b71d385aSAndrei Emeltchenko 
4873b71d385aSAndrei Emeltchenko 			BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
4874b71d385aSAndrei Emeltchenko 			       skb->len, skb->priority);
4875b71d385aSAndrei Emeltchenko 
4876b71d385aSAndrei Emeltchenko 			/* Stop if priority has changed */
4877b71d385aSAndrei Emeltchenko 			if (skb->priority < priority)
4878b71d385aSAndrei Emeltchenko 				break;
4879b71d385aSAndrei Emeltchenko 
4880b71d385aSAndrei Emeltchenko 			skb = skb_dequeue(&chan->data_q);
4881b71d385aSAndrei Emeltchenko 
4882b71d385aSAndrei Emeltchenko 			blocks = __get_blocks(hdev, skb);
4883b71d385aSAndrei Emeltchenko 			if (blocks > hdev->block_cnt)
4884b71d385aSAndrei Emeltchenko 				return;
4885b71d385aSAndrei Emeltchenko 
4886b71d385aSAndrei Emeltchenko 			hci_conn_enter_active_mode(chan->conn,
4887b71d385aSAndrei Emeltchenko 						   bt_cb(skb)->force_active);
4888b71d385aSAndrei Emeltchenko 
488957d17d70SMarcel Holtmann 			hci_send_frame(hdev, skb);
4890b71d385aSAndrei Emeltchenko 			hdev->acl_last_tx = jiffies;
4891b71d385aSAndrei Emeltchenko 
4892b71d385aSAndrei Emeltchenko 			hdev->block_cnt -= blocks;
4893b71d385aSAndrei Emeltchenko 			quote -= blocks;
4894b71d385aSAndrei Emeltchenko 
4895b71d385aSAndrei Emeltchenko 			chan->sent += blocks;
4896b71d385aSAndrei Emeltchenko 			chan->conn->sent += blocks;
4897b71d385aSAndrei Emeltchenko 		}
4898b71d385aSAndrei Emeltchenko 	}
4899b71d385aSAndrei Emeltchenko 
4900b71d385aSAndrei Emeltchenko 	if (cnt != hdev->block_cnt)
4901bd1eb66bSAndrei Emeltchenko 		hci_prio_recalculate(hdev, type);
4902b71d385aSAndrei Emeltchenko }
4903b71d385aSAndrei Emeltchenko 
49046039aa73SGustavo Padovan static void hci_sched_acl(struct hci_dev *hdev)
4905b71d385aSAndrei Emeltchenko {
4906b71d385aSAndrei Emeltchenko 	BT_DBG("%s", hdev->name);
4907b71d385aSAndrei Emeltchenko 
4908bd1eb66bSAndrei Emeltchenko 	/* No ACL link over BR/EDR controller */
4909bd1eb66bSAndrei Emeltchenko 	if (!hci_conn_num(hdev, ACL_LINK) && hdev->dev_type == HCI_BREDR)
4910bd1eb66bSAndrei Emeltchenko 		return;
4911bd1eb66bSAndrei Emeltchenko 
4912bd1eb66bSAndrei Emeltchenko 	/* No AMP link over AMP controller */
4913bd1eb66bSAndrei Emeltchenko 	if (!hci_conn_num(hdev, AMP_LINK) && hdev->dev_type == HCI_AMP)
4914b71d385aSAndrei Emeltchenko 		return;
4915b71d385aSAndrei Emeltchenko 
4916b71d385aSAndrei Emeltchenko 	switch (hdev->flow_ctl_mode) {
4917b71d385aSAndrei Emeltchenko 	case HCI_FLOW_CTL_MODE_PACKET_BASED:
4918b71d385aSAndrei Emeltchenko 		hci_sched_acl_pkt(hdev);
4919b71d385aSAndrei Emeltchenko 		break;
4920b71d385aSAndrei Emeltchenko 
4921b71d385aSAndrei Emeltchenko 	case HCI_FLOW_CTL_MODE_BLOCK_BASED:
4922b71d385aSAndrei Emeltchenko 		hci_sched_acl_blk(hdev);
4923b71d385aSAndrei Emeltchenko 		break;
4924b71d385aSAndrei Emeltchenko 	}
4925b71d385aSAndrei Emeltchenko }
4926b71d385aSAndrei Emeltchenko 
49271da177e4SLinus Torvalds /* Schedule SCO */
49286039aa73SGustavo Padovan static void hci_sched_sco(struct hci_dev *hdev)
49291da177e4SLinus Torvalds {
49301da177e4SLinus Torvalds 	struct hci_conn *conn;
49311da177e4SLinus Torvalds 	struct sk_buff *skb;
49321da177e4SLinus Torvalds 	int quote;
49331da177e4SLinus Torvalds 
49341da177e4SLinus Torvalds 	BT_DBG("%s", hdev->name);
49351da177e4SLinus Torvalds 
493652087a79SLuiz Augusto von Dentz 	if (!hci_conn_num(hdev, SCO_LINK))
493752087a79SLuiz Augusto von Dentz 		return;
493852087a79SLuiz Augusto von Dentz 
49391da177e4SLinus Torvalds 	while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, &quote))) {
49401da177e4SLinus Torvalds 		while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
49411da177e4SLinus Torvalds 			BT_DBG("skb %p len %d", skb, skb->len);
494257d17d70SMarcel Holtmann 			hci_send_frame(hdev, skb);
49431da177e4SLinus Torvalds 
49441da177e4SLinus Torvalds 			conn->sent++;
49451da177e4SLinus Torvalds 			if (conn->sent == ~0)
49461da177e4SLinus Torvalds 				conn->sent = 0;
49471da177e4SLinus Torvalds 		}
49481da177e4SLinus Torvalds 	}
49491da177e4SLinus Torvalds }
49501da177e4SLinus Torvalds 
49516039aa73SGustavo Padovan static void hci_sched_esco(struct hci_dev *hdev)
4952b6a0dc82SMarcel Holtmann {
4953b6a0dc82SMarcel Holtmann 	struct hci_conn *conn;
4954b6a0dc82SMarcel Holtmann 	struct sk_buff *skb;
4955b6a0dc82SMarcel Holtmann 	int quote;
4956b6a0dc82SMarcel Holtmann 
4957b6a0dc82SMarcel Holtmann 	BT_DBG("%s", hdev->name);
4958b6a0dc82SMarcel Holtmann 
495952087a79SLuiz Augusto von Dentz 	if (!hci_conn_num(hdev, ESCO_LINK))
496052087a79SLuiz Augusto von Dentz 		return;
496152087a79SLuiz Augusto von Dentz 
49628fc9ced3SGustavo Padovan 	while (hdev->sco_cnt && (conn = hci_low_sent(hdev, ESCO_LINK,
49638fc9ced3SGustavo Padovan 						     &quote))) {
4964b6a0dc82SMarcel Holtmann 		while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
4965b6a0dc82SMarcel Holtmann 			BT_DBG("skb %p len %d", skb, skb->len);
496657d17d70SMarcel Holtmann 			hci_send_frame(hdev, skb);
4967b6a0dc82SMarcel Holtmann 
4968b6a0dc82SMarcel Holtmann 			conn->sent++;
4969b6a0dc82SMarcel Holtmann 			if (conn->sent == ~0)
4970b6a0dc82SMarcel Holtmann 				conn->sent = 0;
4971b6a0dc82SMarcel Holtmann 		}
4972b6a0dc82SMarcel Holtmann 	}
4973b6a0dc82SMarcel Holtmann }
4974b6a0dc82SMarcel Holtmann 
49756039aa73SGustavo Padovan static void hci_sched_le(struct hci_dev *hdev)
49766ed58ec5SVille Tervo {
497773d80debSLuiz Augusto von Dentz 	struct hci_chan *chan;
49786ed58ec5SVille Tervo 	struct sk_buff *skb;
497902b20f0bSLuiz Augusto von Dentz 	int quote, cnt, tmp;
49806ed58ec5SVille Tervo 
49816ed58ec5SVille Tervo 	BT_DBG("%s", hdev->name);
49826ed58ec5SVille Tervo 
498352087a79SLuiz Augusto von Dentz 	if (!hci_conn_num(hdev, LE_LINK))
498452087a79SLuiz Augusto von Dentz 		return;
498552087a79SLuiz Augusto von Dentz 
4986fee746b0SMarcel Holtmann 	if (!test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) {
49876ed58ec5SVille Tervo 		/* LE tx timeout must be longer than maximum
49886ed58ec5SVille Tervo 		 * link supervision timeout (40.9 seconds) */
4989bae1f5d9SVille Tervo 		if (!hdev->le_cnt && hdev->le_pkts &&
49906ed58ec5SVille Tervo 		    time_after(jiffies, hdev->le_last_tx + HZ * 45))
4991bae1f5d9SVille Tervo 			hci_link_tx_to(hdev, LE_LINK);
49926ed58ec5SVille Tervo 	}
49936ed58ec5SVille Tervo 
49946ed58ec5SVille Tervo 	cnt = hdev->le_pkts ? hdev->le_cnt : hdev->acl_cnt;
499502b20f0bSLuiz Augusto von Dentz 	tmp = cnt;
499673d80debSLuiz Augusto von Dentz 	while (cnt && (chan = hci_chan_sent(hdev, LE_LINK, &quote))) {
4997ec1cce24SLuiz Augusto von Dentz 		u32 priority = (skb_peek(&chan->data_q))->priority;
4998ec1cce24SLuiz Augusto von Dentz 		while (quote-- && (skb = skb_peek(&chan->data_q))) {
499973d80debSLuiz Augusto von Dentz 			BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
500073d80debSLuiz Augusto von Dentz 			       skb->len, skb->priority);
50016ed58ec5SVille Tervo 
5002ec1cce24SLuiz Augusto von Dentz 			/* Stop if priority has changed */
5003ec1cce24SLuiz Augusto von Dentz 			if (skb->priority < priority)
5004ec1cce24SLuiz Augusto von Dentz 				break;
5005ec1cce24SLuiz Augusto von Dentz 
5006ec1cce24SLuiz Augusto von Dentz 			skb = skb_dequeue(&chan->data_q);
5007ec1cce24SLuiz Augusto von Dentz 
500857d17d70SMarcel Holtmann 			hci_send_frame(hdev, skb);
50096ed58ec5SVille Tervo 			hdev->le_last_tx = jiffies;
50106ed58ec5SVille Tervo 
50116ed58ec5SVille Tervo 			cnt--;
501273d80debSLuiz Augusto von Dentz 			chan->sent++;
501373d80debSLuiz Augusto von Dentz 			chan->conn->sent++;
50146ed58ec5SVille Tervo 		}
50156ed58ec5SVille Tervo 	}
501673d80debSLuiz Augusto von Dentz 
50176ed58ec5SVille Tervo 	if (hdev->le_pkts)
50186ed58ec5SVille Tervo 		hdev->le_cnt = cnt;
50196ed58ec5SVille Tervo 	else
50206ed58ec5SVille Tervo 		hdev->acl_cnt = cnt;
502102b20f0bSLuiz Augusto von Dentz 
502202b20f0bSLuiz Augusto von Dentz 	if (cnt != tmp)
502302b20f0bSLuiz Augusto von Dentz 		hci_prio_recalculate(hdev, LE_LINK);
50246ed58ec5SVille Tervo }
50256ed58ec5SVille Tervo 
50263eff45eaSGustavo F. Padovan static void hci_tx_work(struct work_struct *work)
50271da177e4SLinus Torvalds {
50283eff45eaSGustavo F. Padovan 	struct hci_dev *hdev = container_of(work, struct hci_dev, tx_work);
50291da177e4SLinus Torvalds 	struct sk_buff *skb;
50301da177e4SLinus Torvalds 
50316ed58ec5SVille Tervo 	BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt,
50326ed58ec5SVille Tervo 	       hdev->sco_cnt, hdev->le_cnt);
50331da177e4SLinus Torvalds 
503452de599eSMarcel Holtmann 	if (!test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
50351da177e4SLinus Torvalds 		/* Schedule queues and send stuff to HCI driver */
50361da177e4SLinus Torvalds 		hci_sched_acl(hdev);
50371da177e4SLinus Torvalds 		hci_sched_sco(hdev);
5038b6a0dc82SMarcel Holtmann 		hci_sched_esco(hdev);
50396ed58ec5SVille Tervo 		hci_sched_le(hdev);
504052de599eSMarcel Holtmann 	}
50416ed58ec5SVille Tervo 
50421da177e4SLinus Torvalds 	/* Send next queued raw (unknown type) packet */
50431da177e4SLinus Torvalds 	while ((skb = skb_dequeue(&hdev->raw_q)))
504457d17d70SMarcel Holtmann 		hci_send_frame(hdev, skb);
50451da177e4SLinus Torvalds }
50461da177e4SLinus Torvalds 
504725985edcSLucas De Marchi /* ----- HCI RX task (incoming data processing) ----- */
50481da177e4SLinus Torvalds 
50491da177e4SLinus Torvalds /* ACL data packet */
50506039aa73SGustavo Padovan static void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb)
50511da177e4SLinus Torvalds {
50521da177e4SLinus Torvalds 	struct hci_acl_hdr *hdr = (void *) skb->data;
50531da177e4SLinus Torvalds 	struct hci_conn *conn;
50541da177e4SLinus Torvalds 	__u16 handle, flags;
50551da177e4SLinus Torvalds 
50561da177e4SLinus Torvalds 	skb_pull(skb, HCI_ACL_HDR_SIZE);
50571da177e4SLinus Torvalds 
50581da177e4SLinus Torvalds 	handle = __le16_to_cpu(hdr->handle);
50591da177e4SLinus Torvalds 	flags  = hci_flags(handle);
50601da177e4SLinus Torvalds 	handle = hci_handle(handle);
50611da177e4SLinus Torvalds 
5062f0e09510SAndrei Emeltchenko 	BT_DBG("%s len %d handle 0x%4.4x flags 0x%4.4x", hdev->name, skb->len,
5063a8c5fb1aSGustavo Padovan 	       handle, flags);
50641da177e4SLinus Torvalds 
50651da177e4SLinus Torvalds 	hdev->stat.acl_rx++;
50661da177e4SLinus Torvalds 
50671da177e4SLinus Torvalds 	hci_dev_lock(hdev);
50681da177e4SLinus Torvalds 	conn = hci_conn_hash_lookup_handle(hdev, handle);
50691da177e4SLinus Torvalds 	hci_dev_unlock(hdev);
50701da177e4SLinus Torvalds 
50711da177e4SLinus Torvalds 	if (conn) {
507265983fc7SMat Martineau 		hci_conn_enter_active_mode(conn, BT_POWER_FORCE_ACTIVE_OFF);
507304837f64SMarcel Holtmann 
50741da177e4SLinus Torvalds 		/* Send to upper protocol */
5075686ebf28SUlisses Furquim 		l2cap_recv_acldata(conn, skb, flags);
50761da177e4SLinus Torvalds 		return;
50771da177e4SLinus Torvalds 	} else {
50781da177e4SLinus Torvalds 		BT_ERR("%s ACL packet for unknown connection handle %d",
50791da177e4SLinus Torvalds 		       hdev->name, handle);
50801da177e4SLinus Torvalds 	}
50811da177e4SLinus Torvalds 
50821da177e4SLinus Torvalds 	kfree_skb(skb);
50831da177e4SLinus Torvalds }
50841da177e4SLinus Torvalds 
50851da177e4SLinus Torvalds /* SCO data packet */
50866039aa73SGustavo Padovan static void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb)
50871da177e4SLinus Torvalds {
50881da177e4SLinus Torvalds 	struct hci_sco_hdr *hdr = (void *) skb->data;
50891da177e4SLinus Torvalds 	struct hci_conn *conn;
50901da177e4SLinus Torvalds 	__u16 handle;
50911da177e4SLinus Torvalds 
50921da177e4SLinus Torvalds 	skb_pull(skb, HCI_SCO_HDR_SIZE);
50931da177e4SLinus Torvalds 
50941da177e4SLinus Torvalds 	handle = __le16_to_cpu(hdr->handle);
50951da177e4SLinus Torvalds 
5096f0e09510SAndrei Emeltchenko 	BT_DBG("%s len %d handle 0x%4.4x", hdev->name, skb->len, handle);
50971da177e4SLinus Torvalds 
50981da177e4SLinus Torvalds 	hdev->stat.sco_rx++;
50991da177e4SLinus Torvalds 
51001da177e4SLinus Torvalds 	hci_dev_lock(hdev);
51011da177e4SLinus Torvalds 	conn = hci_conn_hash_lookup_handle(hdev, handle);
51021da177e4SLinus Torvalds 	hci_dev_unlock(hdev);
51031da177e4SLinus Torvalds 
51041da177e4SLinus Torvalds 	if (conn) {
51051da177e4SLinus Torvalds 		/* Send to upper protocol */
5106686ebf28SUlisses Furquim 		sco_recv_scodata(conn, skb);
51071da177e4SLinus Torvalds 		return;
51081da177e4SLinus Torvalds 	} else {
51091da177e4SLinus Torvalds 		BT_ERR("%s SCO packet for unknown connection handle %d",
51101da177e4SLinus Torvalds 		       hdev->name, handle);
51111da177e4SLinus Torvalds 	}
51121da177e4SLinus Torvalds 
51131da177e4SLinus Torvalds 	kfree_skb(skb);
51141da177e4SLinus Torvalds }
51151da177e4SLinus Torvalds 
51169238f36aSJohan Hedberg static bool hci_req_is_complete(struct hci_dev *hdev)
51179238f36aSJohan Hedberg {
51189238f36aSJohan Hedberg 	struct sk_buff *skb;
51199238f36aSJohan Hedberg 
51209238f36aSJohan Hedberg 	skb = skb_peek(&hdev->cmd_q);
51219238f36aSJohan Hedberg 	if (!skb)
51229238f36aSJohan Hedberg 		return true;
51239238f36aSJohan Hedberg 
51249238f36aSJohan Hedberg 	return bt_cb(skb)->req.start;
51259238f36aSJohan Hedberg }
51269238f36aSJohan Hedberg 
512742c6b129SJohan Hedberg static void hci_resend_last(struct hci_dev *hdev)
512842c6b129SJohan Hedberg {
512942c6b129SJohan Hedberg 	struct hci_command_hdr *sent;
513042c6b129SJohan Hedberg 	struct sk_buff *skb;
513142c6b129SJohan Hedberg 	u16 opcode;
513242c6b129SJohan Hedberg 
513342c6b129SJohan Hedberg 	if (!hdev->sent_cmd)
513442c6b129SJohan Hedberg 		return;
513542c6b129SJohan Hedberg 
513642c6b129SJohan Hedberg 	sent = (void *) hdev->sent_cmd->data;
513742c6b129SJohan Hedberg 	opcode = __le16_to_cpu(sent->opcode);
513842c6b129SJohan Hedberg 	if (opcode == HCI_OP_RESET)
513942c6b129SJohan Hedberg 		return;
514042c6b129SJohan Hedberg 
514142c6b129SJohan Hedberg 	skb = skb_clone(hdev->sent_cmd, GFP_KERNEL);
514242c6b129SJohan Hedberg 	if (!skb)
514342c6b129SJohan Hedberg 		return;
514442c6b129SJohan Hedberg 
514542c6b129SJohan Hedberg 	skb_queue_head(&hdev->cmd_q, skb);
514642c6b129SJohan Hedberg 	queue_work(hdev->workqueue, &hdev->cmd_work);
514742c6b129SJohan Hedberg }
514842c6b129SJohan Hedberg 
51499238f36aSJohan Hedberg void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status)
51509238f36aSJohan Hedberg {
51519238f36aSJohan Hedberg 	hci_req_complete_t req_complete = NULL;
51529238f36aSJohan Hedberg 	struct sk_buff *skb;
51539238f36aSJohan Hedberg 	unsigned long flags;
51549238f36aSJohan Hedberg 
51559238f36aSJohan Hedberg 	BT_DBG("opcode 0x%04x status 0x%02x", opcode, status);
51569238f36aSJohan Hedberg 
515742c6b129SJohan Hedberg 	/* If the completed command doesn't match the last one that was
515842c6b129SJohan Hedberg 	 * sent we need to do special handling of it.
51599238f36aSJohan Hedberg 	 */
516042c6b129SJohan Hedberg 	if (!hci_sent_cmd_data(hdev, opcode)) {
516142c6b129SJohan Hedberg 		/* Some CSR based controllers generate a spontaneous
516242c6b129SJohan Hedberg 		 * reset complete event during init and any pending
516342c6b129SJohan Hedberg 		 * command will never be completed. In such a case we
516442c6b129SJohan Hedberg 		 * need to resend whatever was the last sent
516542c6b129SJohan Hedberg 		 * command.
516642c6b129SJohan Hedberg 		 */
516742c6b129SJohan Hedberg 		if (test_bit(HCI_INIT, &hdev->flags) && opcode == HCI_OP_RESET)
516842c6b129SJohan Hedberg 			hci_resend_last(hdev);
516942c6b129SJohan Hedberg 
51709238f36aSJohan Hedberg 		return;
517142c6b129SJohan Hedberg 	}
51729238f36aSJohan Hedberg 
51739238f36aSJohan Hedberg 	/* If the command succeeded and there's still more commands in
51749238f36aSJohan Hedberg 	 * this request the request is not yet complete.
51759238f36aSJohan Hedberg 	 */
51769238f36aSJohan Hedberg 	if (!status && !hci_req_is_complete(hdev))
51779238f36aSJohan Hedberg 		return;
51789238f36aSJohan Hedberg 
51799238f36aSJohan Hedberg 	/* If this was the last command in a request the complete
51809238f36aSJohan Hedberg 	 * callback would be found in hdev->sent_cmd instead of the
51819238f36aSJohan Hedberg 	 * command queue (hdev->cmd_q).
51829238f36aSJohan Hedberg 	 */
51839238f36aSJohan Hedberg 	if (hdev->sent_cmd) {
51849238f36aSJohan Hedberg 		req_complete = bt_cb(hdev->sent_cmd)->req.complete;
518553e21fbcSJohan Hedberg 
518653e21fbcSJohan Hedberg 		if (req_complete) {
518753e21fbcSJohan Hedberg 			/* We must set the complete callback to NULL to
518853e21fbcSJohan Hedberg 			 * avoid calling the callback more than once if
518953e21fbcSJohan Hedberg 			 * this function gets called again.
519053e21fbcSJohan Hedberg 			 */
519153e21fbcSJohan Hedberg 			bt_cb(hdev->sent_cmd)->req.complete = NULL;
519253e21fbcSJohan Hedberg 
51939238f36aSJohan Hedberg 			goto call_complete;
51949238f36aSJohan Hedberg 		}
519553e21fbcSJohan Hedberg 	}
51969238f36aSJohan Hedberg 
51979238f36aSJohan Hedberg 	/* Remove all pending commands belonging to this request */
51989238f36aSJohan Hedberg 	spin_lock_irqsave(&hdev->cmd_q.lock, flags);
51999238f36aSJohan Hedberg 	while ((skb = __skb_dequeue(&hdev->cmd_q))) {
52009238f36aSJohan Hedberg 		if (bt_cb(skb)->req.start) {
52019238f36aSJohan Hedberg 			__skb_queue_head(&hdev->cmd_q, skb);
52029238f36aSJohan Hedberg 			break;
52039238f36aSJohan Hedberg 		}
52049238f36aSJohan Hedberg 
52059238f36aSJohan Hedberg 		req_complete = bt_cb(skb)->req.complete;
52069238f36aSJohan Hedberg 		kfree_skb(skb);
52079238f36aSJohan Hedberg 	}
52089238f36aSJohan Hedberg 	spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
52099238f36aSJohan Hedberg 
52109238f36aSJohan Hedberg call_complete:
52119238f36aSJohan Hedberg 	if (req_complete)
52129238f36aSJohan Hedberg 		req_complete(hdev, status);
52139238f36aSJohan Hedberg }
52149238f36aSJohan Hedberg 
5215b78752ccSMarcel Holtmann static void hci_rx_work(struct work_struct *work)
52161da177e4SLinus Torvalds {
5217b78752ccSMarcel Holtmann 	struct hci_dev *hdev = container_of(work, struct hci_dev, rx_work);
52181da177e4SLinus Torvalds 	struct sk_buff *skb;
52191da177e4SLinus Torvalds 
52201da177e4SLinus Torvalds 	BT_DBG("%s", hdev->name);
52211da177e4SLinus Torvalds 
52221da177e4SLinus Torvalds 	while ((skb = skb_dequeue(&hdev->rx_q))) {
5223cd82e61cSMarcel Holtmann 		/* Send copy to monitor */
5224cd82e61cSMarcel Holtmann 		hci_send_to_monitor(hdev, skb);
5225cd82e61cSMarcel Holtmann 
52261da177e4SLinus Torvalds 		if (atomic_read(&hdev->promisc)) {
52271da177e4SLinus Torvalds 			/* Send copy to the sockets */
5228470fe1b5SMarcel Holtmann 			hci_send_to_sock(hdev, skb);
52291da177e4SLinus Torvalds 		}
52301da177e4SLinus Torvalds 
5231fee746b0SMarcel Holtmann 		if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
52321da177e4SLinus Torvalds 			kfree_skb(skb);
52331da177e4SLinus Torvalds 			continue;
52341da177e4SLinus Torvalds 		}
52351da177e4SLinus Torvalds 
52361da177e4SLinus Torvalds 		if (test_bit(HCI_INIT, &hdev->flags)) {
52371da177e4SLinus Torvalds 			/* Don't process data packets in this states. */
52380d48d939SMarcel Holtmann 			switch (bt_cb(skb)->pkt_type) {
52391da177e4SLinus Torvalds 			case HCI_ACLDATA_PKT:
52401da177e4SLinus Torvalds 			case HCI_SCODATA_PKT:
52411da177e4SLinus Torvalds 				kfree_skb(skb);
52421da177e4SLinus Torvalds 				continue;
52433ff50b79SStephen Hemminger 			}
52441da177e4SLinus Torvalds 		}
52451da177e4SLinus Torvalds 
52461da177e4SLinus Torvalds 		/* Process frame */
52470d48d939SMarcel Holtmann 		switch (bt_cb(skb)->pkt_type) {
52481da177e4SLinus Torvalds 		case HCI_EVENT_PKT:
5249b78752ccSMarcel Holtmann 			BT_DBG("%s Event packet", hdev->name);
52501da177e4SLinus Torvalds 			hci_event_packet(hdev, skb);
52511da177e4SLinus Torvalds 			break;
52521da177e4SLinus Torvalds 
52531da177e4SLinus Torvalds 		case HCI_ACLDATA_PKT:
52541da177e4SLinus Torvalds 			BT_DBG("%s ACL data packet", hdev->name);
52551da177e4SLinus Torvalds 			hci_acldata_packet(hdev, skb);
52561da177e4SLinus Torvalds 			break;
52571da177e4SLinus Torvalds 
52581da177e4SLinus Torvalds 		case HCI_SCODATA_PKT:
52591da177e4SLinus Torvalds 			BT_DBG("%s SCO data packet", hdev->name);
52601da177e4SLinus Torvalds 			hci_scodata_packet(hdev, skb);
52611da177e4SLinus Torvalds 			break;
52621da177e4SLinus Torvalds 
52631da177e4SLinus Torvalds 		default:
52641da177e4SLinus Torvalds 			kfree_skb(skb);
52651da177e4SLinus Torvalds 			break;
52661da177e4SLinus Torvalds 		}
52671da177e4SLinus Torvalds 	}
52681da177e4SLinus Torvalds }
52691da177e4SLinus Torvalds 
5270c347b765SGustavo F. Padovan static void hci_cmd_work(struct work_struct *work)
52711da177e4SLinus Torvalds {
5272c347b765SGustavo F. Padovan 	struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_work);
52731da177e4SLinus Torvalds 	struct sk_buff *skb;
52741da177e4SLinus Torvalds 
52752104786bSAndrei Emeltchenko 	BT_DBG("%s cmd_cnt %d cmd queued %d", hdev->name,
52762104786bSAndrei Emeltchenko 	       atomic_read(&hdev->cmd_cnt), skb_queue_len(&hdev->cmd_q));
52771da177e4SLinus Torvalds 
52781da177e4SLinus Torvalds 	/* Send queued commands */
52795a08ecceSAndrei Emeltchenko 	if (atomic_read(&hdev->cmd_cnt)) {
52805a08ecceSAndrei Emeltchenko 		skb = skb_dequeue(&hdev->cmd_q);
52815a08ecceSAndrei Emeltchenko 		if (!skb)
52825a08ecceSAndrei Emeltchenko 			return;
52835a08ecceSAndrei Emeltchenko 
52841da177e4SLinus Torvalds 		kfree_skb(hdev->sent_cmd);
52851da177e4SLinus Torvalds 
5286a675d7f1SMarcel Holtmann 		hdev->sent_cmd = skb_clone(skb, GFP_KERNEL);
528770f23020SAndrei Emeltchenko 		if (hdev->sent_cmd) {
52881da177e4SLinus Torvalds 			atomic_dec(&hdev->cmd_cnt);
528957d17d70SMarcel Holtmann 			hci_send_frame(hdev, skb);
52907bdb8a5cSSzymon Janc 			if (test_bit(HCI_RESET, &hdev->flags))
529165cc2b49SMarcel Holtmann 				cancel_delayed_work(&hdev->cmd_timer);
52927bdb8a5cSSzymon Janc 			else
529365cc2b49SMarcel Holtmann 				schedule_delayed_work(&hdev->cmd_timer,
529465cc2b49SMarcel Holtmann 						      HCI_CMD_TIMEOUT);
52951da177e4SLinus Torvalds 		} else {
52961da177e4SLinus Torvalds 			skb_queue_head(&hdev->cmd_q, skb);
5297c347b765SGustavo F. Padovan 			queue_work(hdev->workqueue, &hdev->cmd_work);
52981da177e4SLinus Torvalds 		}
52991da177e4SLinus Torvalds 	}
53001da177e4SLinus Torvalds }
5301b1efcc28SAndre Guedes 
5302b1efcc28SAndre Guedes void hci_req_add_le_scan_disable(struct hci_request *req)
5303b1efcc28SAndre Guedes {
5304b1efcc28SAndre Guedes 	struct hci_cp_le_set_scan_enable cp;
5305b1efcc28SAndre Guedes 
5306b1efcc28SAndre Guedes 	memset(&cp, 0, sizeof(cp));
5307b1efcc28SAndre Guedes 	cp.enable = LE_SCAN_DISABLE;
5308b1efcc28SAndre Guedes 	hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);
5309b1efcc28SAndre Guedes }
5310a4790dbdSAndre Guedes 
53118ef30fd3SAndre Guedes void hci_req_add_le_passive_scan(struct hci_request *req)
53128ef30fd3SAndre Guedes {
53138ef30fd3SAndre Guedes 	struct hci_cp_le_set_scan_param param_cp;
53148ef30fd3SAndre Guedes 	struct hci_cp_le_set_scan_enable enable_cp;
53158ef30fd3SAndre Guedes 	struct hci_dev *hdev = req->hdev;
53168ef30fd3SAndre Guedes 	u8 own_addr_type;
53178ef30fd3SAndre Guedes 
53186ab535a7SMarcel Holtmann 	/* Set require_privacy to false since no SCAN_REQ are send
53196ab535a7SMarcel Holtmann 	 * during passive scanning. Not using an unresolvable address
53206ab535a7SMarcel Holtmann 	 * here is important so that peer devices using direct
53216ab535a7SMarcel Holtmann 	 * advertising with our address will be correctly reported
53226ab535a7SMarcel Holtmann 	 * by the controller.
53238ef30fd3SAndre Guedes 	 */
53246ab535a7SMarcel Holtmann 	if (hci_update_random_address(req, false, &own_addr_type))
53258ef30fd3SAndre Guedes 		return;
53268ef30fd3SAndre Guedes 
53278ef30fd3SAndre Guedes 	memset(&param_cp, 0, sizeof(param_cp));
53288ef30fd3SAndre Guedes 	param_cp.type = LE_SCAN_PASSIVE;
53298ef30fd3SAndre Guedes 	param_cp.interval = cpu_to_le16(hdev->le_scan_interval);
53308ef30fd3SAndre Guedes 	param_cp.window = cpu_to_le16(hdev->le_scan_window);
53318ef30fd3SAndre Guedes 	param_cp.own_address_type = own_addr_type;
53328ef30fd3SAndre Guedes 	hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp),
53338ef30fd3SAndre Guedes 		    &param_cp);
53348ef30fd3SAndre Guedes 
53358ef30fd3SAndre Guedes 	memset(&enable_cp, 0, sizeof(enable_cp));
53368ef30fd3SAndre Guedes 	enable_cp.enable = LE_SCAN_ENABLE;
53374340a124SAndre Guedes 	enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
53388ef30fd3SAndre Guedes 	hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp),
53398ef30fd3SAndre Guedes 		    &enable_cp);
53408ef30fd3SAndre Guedes }
53418ef30fd3SAndre Guedes 
5342a4790dbdSAndre Guedes static void update_background_scan_complete(struct hci_dev *hdev, u8 status)
5343a4790dbdSAndre Guedes {
5344a4790dbdSAndre Guedes 	if (status)
5345a4790dbdSAndre Guedes 		BT_DBG("HCI request failed to update background scanning: "
5346a4790dbdSAndre Guedes 		       "status 0x%2.2x", status);
5347a4790dbdSAndre Guedes }
5348a4790dbdSAndre Guedes 
5349a4790dbdSAndre Guedes /* This function controls the background scanning based on hdev->pend_le_conns
5350a4790dbdSAndre Guedes  * list. If there are pending LE connection we start the background scanning,
5351a4790dbdSAndre Guedes  * otherwise we stop it.
5352a4790dbdSAndre Guedes  *
5353a4790dbdSAndre Guedes  * This function requires the caller holds hdev->lock.
5354a4790dbdSAndre Guedes  */
5355a4790dbdSAndre Guedes void hci_update_background_scan(struct hci_dev *hdev)
5356a4790dbdSAndre Guedes {
5357a4790dbdSAndre Guedes 	struct hci_request req;
5358a4790dbdSAndre Guedes 	struct hci_conn *conn;
5359a4790dbdSAndre Guedes 	int err;
5360a4790dbdSAndre Guedes 
5361c20c02d5SMarcel Holtmann 	if (!test_bit(HCI_UP, &hdev->flags) ||
5362c20c02d5SMarcel Holtmann 	    test_bit(HCI_INIT, &hdev->flags) ||
5363c20c02d5SMarcel Holtmann 	    test_bit(HCI_SETUP, &hdev->dev_flags) ||
5364b8221770SMarcel Holtmann 	    test_bit(HCI_AUTO_OFF, &hdev->dev_flags) ||
5365c20c02d5SMarcel Holtmann 	    test_bit(HCI_UNREGISTER, &hdev->dev_flags))
53661c1697c0SMarcel Holtmann 		return;
53671c1697c0SMarcel Holtmann 
5368a4790dbdSAndre Guedes 	hci_req_init(&req, hdev);
5369a4790dbdSAndre Guedes 
5370a4790dbdSAndre Guedes 	if (list_empty(&hdev->pend_le_conns)) {
5371a4790dbdSAndre Guedes 		/* If there is no pending LE connections, we should stop
5372a4790dbdSAndre Guedes 		 * the background scanning.
5373a4790dbdSAndre Guedes 		 */
5374a4790dbdSAndre Guedes 
5375a4790dbdSAndre Guedes 		/* If controller is not scanning we are done. */
5376a4790dbdSAndre Guedes 		if (!test_bit(HCI_LE_SCAN, &hdev->dev_flags))
5377a4790dbdSAndre Guedes 			return;
5378a4790dbdSAndre Guedes 
5379a4790dbdSAndre Guedes 		hci_req_add_le_scan_disable(&req);
5380a4790dbdSAndre Guedes 
5381a4790dbdSAndre Guedes 		BT_DBG("%s stopping background scanning", hdev->name);
5382a4790dbdSAndre Guedes 	} else {
5383a4790dbdSAndre Guedes 		/* If there is at least one pending LE connection, we should
5384a4790dbdSAndre Guedes 		 * keep the background scan running.
5385a4790dbdSAndre Guedes 		 */
5386a4790dbdSAndre Guedes 
5387a4790dbdSAndre Guedes 		/* If controller is connecting, we should not start scanning
5388a4790dbdSAndre Guedes 		 * since some controllers are not able to scan and connect at
5389a4790dbdSAndre Guedes 		 * the same time.
5390a4790dbdSAndre Guedes 		 */
5391a4790dbdSAndre Guedes 		conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
5392a4790dbdSAndre Guedes 		if (conn)
5393a4790dbdSAndre Guedes 			return;
5394a4790dbdSAndre Guedes 
53954340a124SAndre Guedes 		/* If controller is currently scanning, we stop it to ensure we
53964340a124SAndre Guedes 		 * don't miss any advertising (due to duplicates filter).
53974340a124SAndre Guedes 		 */
53984340a124SAndre Guedes 		if (test_bit(HCI_LE_SCAN, &hdev->dev_flags))
53994340a124SAndre Guedes 			hci_req_add_le_scan_disable(&req);
54004340a124SAndre Guedes 
54018ef30fd3SAndre Guedes 		hci_req_add_le_passive_scan(&req);
5402a4790dbdSAndre Guedes 
5403a4790dbdSAndre Guedes 		BT_DBG("%s starting background scanning", hdev->name);
5404a4790dbdSAndre Guedes 	}
5405a4790dbdSAndre Guedes 
5406a4790dbdSAndre Guedes 	err = hci_req_run(&req, update_background_scan_complete);
5407a4790dbdSAndre Guedes 	if (err)
5408a4790dbdSAndre Guedes 		BT_ERR("Failed to run HCI request: err %d", err);
5409a4790dbdSAndre Guedes }
5410