xref: /openbmc/linux/net/bluetooth/hci_core.c (revision a9f6068e)
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 
400857dd3bSJohan Hedberg #include "hci_request.h"
4160c5f5fbSMarcel Holtmann #include "hci_debugfs.h"
42970c4e46SJohan Hedberg #include "smp.h"
43970c4e46SJohan Hedberg 
44b78752ccSMarcel Holtmann static void hci_rx_work(struct work_struct *work);
45c347b765SGustavo F. Padovan static void hci_cmd_work(struct work_struct *work);
463eff45eaSGustavo F. Padovan static void hci_tx_work(struct work_struct *work);
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds /* HCI device list */
491da177e4SLinus Torvalds LIST_HEAD(hci_dev_list);
501da177e4SLinus Torvalds DEFINE_RWLOCK(hci_dev_list_lock);
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds /* HCI callback list */
531da177e4SLinus Torvalds LIST_HEAD(hci_cb_list);
541da177e4SLinus Torvalds DEFINE_RWLOCK(hci_cb_list_lock);
551da177e4SLinus Torvalds 
563df92b31SSasha Levin /* HCI ID Numbering */
573df92b31SSasha Levin static DEFINE_IDA(hci_index_ida);
583df92b31SSasha Levin 
59899de765SMarcel Holtmann /* ----- HCI requests ----- */
60899de765SMarcel Holtmann 
61899de765SMarcel Holtmann #define HCI_REQ_DONE	  0
62899de765SMarcel Holtmann #define HCI_REQ_PEND	  1
63899de765SMarcel Holtmann #define HCI_REQ_CANCELED  2
64899de765SMarcel Holtmann 
65899de765SMarcel Holtmann #define hci_req_lock(d)		mutex_lock(&d->req_lock)
66899de765SMarcel Holtmann #define hci_req_unlock(d)	mutex_unlock(&d->req_lock)
67899de765SMarcel Holtmann 
681da177e4SLinus Torvalds /* ---- HCI notifications ---- */
691da177e4SLinus Torvalds 
706516455dSMarcel Holtmann static void hci_notify(struct hci_dev *hdev, int event)
711da177e4SLinus Torvalds {
72040030efSMarcel Holtmann 	hci_sock_dev_event(hdev, event);
731da177e4SLinus Torvalds }
741da177e4SLinus Torvalds 
75baf27f6eSMarcel Holtmann /* ---- HCI debugfs entries ---- */
76baf27f6eSMarcel Holtmann 
774b4148e9SMarcel Holtmann static ssize_t dut_mode_read(struct file *file, char __user *user_buf,
784b4148e9SMarcel Holtmann 			     size_t count, loff_t *ppos)
794b4148e9SMarcel Holtmann {
804b4148e9SMarcel Holtmann 	struct hci_dev *hdev = file->private_data;
814b4148e9SMarcel Holtmann 	char buf[3];
824b4148e9SMarcel Holtmann 
83111902f7SMarcel Holtmann 	buf[0] = test_bit(HCI_DUT_MODE, &hdev->dbg_flags) ? 'Y': 'N';
844b4148e9SMarcel Holtmann 	buf[1] = '\n';
854b4148e9SMarcel Holtmann 	buf[2] = '\0';
864b4148e9SMarcel Holtmann 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
874b4148e9SMarcel Holtmann }
884b4148e9SMarcel Holtmann 
894b4148e9SMarcel Holtmann static ssize_t dut_mode_write(struct file *file, const char __user *user_buf,
904b4148e9SMarcel Holtmann 			      size_t count, loff_t *ppos)
914b4148e9SMarcel Holtmann {
924b4148e9SMarcel Holtmann 	struct hci_dev *hdev = file->private_data;
934b4148e9SMarcel Holtmann 	struct sk_buff *skb;
944b4148e9SMarcel Holtmann 	char buf[32];
954b4148e9SMarcel Holtmann 	size_t buf_size = min(count, (sizeof(buf)-1));
964b4148e9SMarcel Holtmann 	bool enable;
974b4148e9SMarcel Holtmann 	int err;
984b4148e9SMarcel Holtmann 
994b4148e9SMarcel Holtmann 	if (!test_bit(HCI_UP, &hdev->flags))
1004b4148e9SMarcel Holtmann 		return -ENETDOWN;
1014b4148e9SMarcel Holtmann 
1024b4148e9SMarcel Holtmann 	if (copy_from_user(buf, user_buf, buf_size))
1034b4148e9SMarcel Holtmann 		return -EFAULT;
1044b4148e9SMarcel Holtmann 
1054b4148e9SMarcel Holtmann 	buf[buf_size] = '\0';
1064b4148e9SMarcel Holtmann 	if (strtobool(buf, &enable))
1074b4148e9SMarcel Holtmann 		return -EINVAL;
1084b4148e9SMarcel Holtmann 
109111902f7SMarcel Holtmann 	if (enable == test_bit(HCI_DUT_MODE, &hdev->dbg_flags))
1104b4148e9SMarcel Holtmann 		return -EALREADY;
1114b4148e9SMarcel Holtmann 
1124b4148e9SMarcel Holtmann 	hci_req_lock(hdev);
1134b4148e9SMarcel Holtmann 	if (enable)
1144b4148e9SMarcel Holtmann 		skb = __hci_cmd_sync(hdev, HCI_OP_ENABLE_DUT_MODE, 0, NULL,
1154b4148e9SMarcel Holtmann 				     HCI_CMD_TIMEOUT);
1164b4148e9SMarcel Holtmann 	else
1174b4148e9SMarcel Holtmann 		skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL,
1184b4148e9SMarcel Holtmann 				     HCI_CMD_TIMEOUT);
1194b4148e9SMarcel Holtmann 	hci_req_unlock(hdev);
1204b4148e9SMarcel Holtmann 
1214b4148e9SMarcel Holtmann 	if (IS_ERR(skb))
1224b4148e9SMarcel Holtmann 		return PTR_ERR(skb);
1234b4148e9SMarcel Holtmann 
1244b4148e9SMarcel Holtmann 	err = -bt_to_errno(skb->data[0]);
1254b4148e9SMarcel Holtmann 	kfree_skb(skb);
1264b4148e9SMarcel Holtmann 
1274b4148e9SMarcel Holtmann 	if (err < 0)
1284b4148e9SMarcel Holtmann 		return err;
1294b4148e9SMarcel Holtmann 
130111902f7SMarcel Holtmann 	change_bit(HCI_DUT_MODE, &hdev->dbg_flags);
1314b4148e9SMarcel Holtmann 
1324b4148e9SMarcel Holtmann 	return count;
1334b4148e9SMarcel Holtmann }
1344b4148e9SMarcel Holtmann 
1354b4148e9SMarcel Holtmann static const struct file_operations dut_mode_fops = {
1364b4148e9SMarcel Holtmann 	.open		= simple_open,
1374b4148e9SMarcel Holtmann 	.read		= dut_mode_read,
1384b4148e9SMarcel Holtmann 	.write		= dut_mode_write,
1394b4148e9SMarcel Holtmann 	.llseek		= default_llseek,
1404b4148e9SMarcel Holtmann };
1414b4148e9SMarcel Holtmann 
1421da177e4SLinus Torvalds /* ---- HCI requests ---- */
1431da177e4SLinus Torvalds 
14442c6b129SJohan Hedberg static void hci_req_sync_complete(struct hci_dev *hdev, u8 result)
1451da177e4SLinus Torvalds {
14642c6b129SJohan Hedberg 	BT_DBG("%s result 0x%2.2x", hdev->name, result);
14775fb0e32SJohan Hedberg 
1481da177e4SLinus Torvalds 	if (hdev->req_status == HCI_REQ_PEND) {
1491da177e4SLinus Torvalds 		hdev->req_result = result;
1501da177e4SLinus Torvalds 		hdev->req_status = HCI_REQ_DONE;
1511da177e4SLinus Torvalds 		wake_up_interruptible(&hdev->req_wait_q);
1521da177e4SLinus Torvalds 	}
1531da177e4SLinus Torvalds }
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds static void hci_req_cancel(struct hci_dev *hdev, int err)
1561da177e4SLinus Torvalds {
1571da177e4SLinus Torvalds 	BT_DBG("%s err 0x%2.2x", hdev->name, err);
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	if (hdev->req_status == HCI_REQ_PEND) {
1601da177e4SLinus Torvalds 		hdev->req_result = err;
1611da177e4SLinus Torvalds 		hdev->req_status = HCI_REQ_CANCELED;
1621da177e4SLinus Torvalds 		wake_up_interruptible(&hdev->req_wait_q);
1631da177e4SLinus Torvalds 	}
1641da177e4SLinus Torvalds }
1651da177e4SLinus Torvalds 
16677a63e0aSFengguang Wu static struct sk_buff *hci_get_cmd_complete(struct hci_dev *hdev, u16 opcode,
16777a63e0aSFengguang Wu 					    u8 event)
16875e84b7cSJohan Hedberg {
16975e84b7cSJohan Hedberg 	struct hci_ev_cmd_complete *ev;
17075e84b7cSJohan Hedberg 	struct hci_event_hdr *hdr;
17175e84b7cSJohan Hedberg 	struct sk_buff *skb;
17275e84b7cSJohan Hedberg 
17375e84b7cSJohan Hedberg 	hci_dev_lock(hdev);
17475e84b7cSJohan Hedberg 
17575e84b7cSJohan Hedberg 	skb = hdev->recv_evt;
17675e84b7cSJohan Hedberg 	hdev->recv_evt = NULL;
17775e84b7cSJohan Hedberg 
17875e84b7cSJohan Hedberg 	hci_dev_unlock(hdev);
17975e84b7cSJohan Hedberg 
18075e84b7cSJohan Hedberg 	if (!skb)
18175e84b7cSJohan Hedberg 		return ERR_PTR(-ENODATA);
18275e84b7cSJohan Hedberg 
18375e84b7cSJohan Hedberg 	if (skb->len < sizeof(*hdr)) {
18475e84b7cSJohan Hedberg 		BT_ERR("Too short HCI event");
18575e84b7cSJohan Hedberg 		goto failed;
18675e84b7cSJohan Hedberg 	}
18775e84b7cSJohan Hedberg 
18875e84b7cSJohan Hedberg 	hdr = (void *) skb->data;
18975e84b7cSJohan Hedberg 	skb_pull(skb, HCI_EVENT_HDR_SIZE);
19075e84b7cSJohan Hedberg 
1917b1abbbeSJohan Hedberg 	if (event) {
1927b1abbbeSJohan Hedberg 		if (hdr->evt != event)
1937b1abbbeSJohan Hedberg 			goto failed;
1947b1abbbeSJohan Hedberg 		return skb;
1957b1abbbeSJohan Hedberg 	}
1967b1abbbeSJohan Hedberg 
19775e84b7cSJohan Hedberg 	if (hdr->evt != HCI_EV_CMD_COMPLETE) {
19875e84b7cSJohan Hedberg 		BT_DBG("Last event is not cmd complete (0x%2.2x)", hdr->evt);
19975e84b7cSJohan Hedberg 		goto failed;
20075e84b7cSJohan Hedberg 	}
20175e84b7cSJohan Hedberg 
20275e84b7cSJohan Hedberg 	if (skb->len < sizeof(*ev)) {
20375e84b7cSJohan Hedberg 		BT_ERR("Too short cmd_complete event");
20475e84b7cSJohan Hedberg 		goto failed;
20575e84b7cSJohan Hedberg 	}
20675e84b7cSJohan Hedberg 
20775e84b7cSJohan Hedberg 	ev = (void *) skb->data;
20875e84b7cSJohan Hedberg 	skb_pull(skb, sizeof(*ev));
20975e84b7cSJohan Hedberg 
21075e84b7cSJohan Hedberg 	if (opcode == __le16_to_cpu(ev->opcode))
21175e84b7cSJohan Hedberg 		return skb;
21275e84b7cSJohan Hedberg 
21375e84b7cSJohan Hedberg 	BT_DBG("opcode doesn't match (0x%2.2x != 0x%2.2x)", opcode,
21475e84b7cSJohan Hedberg 	       __le16_to_cpu(ev->opcode));
21575e84b7cSJohan Hedberg 
21675e84b7cSJohan Hedberg failed:
21775e84b7cSJohan Hedberg 	kfree_skb(skb);
21875e84b7cSJohan Hedberg 	return ERR_PTR(-ENODATA);
21975e84b7cSJohan Hedberg }
22075e84b7cSJohan Hedberg 
2217b1abbbeSJohan Hedberg struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
22207dc93ddSJohan Hedberg 				  const void *param, u8 event, u32 timeout)
22375e84b7cSJohan Hedberg {
22475e84b7cSJohan Hedberg 	DECLARE_WAITQUEUE(wait, current);
22575e84b7cSJohan Hedberg 	struct hci_request req;
22675e84b7cSJohan Hedberg 	int err = 0;
22775e84b7cSJohan Hedberg 
22875e84b7cSJohan Hedberg 	BT_DBG("%s", hdev->name);
22975e84b7cSJohan Hedberg 
23075e84b7cSJohan Hedberg 	hci_req_init(&req, hdev);
23175e84b7cSJohan Hedberg 
2327b1abbbeSJohan Hedberg 	hci_req_add_ev(&req, opcode, plen, param, event);
23375e84b7cSJohan Hedberg 
23475e84b7cSJohan Hedberg 	hdev->req_status = HCI_REQ_PEND;
23575e84b7cSJohan Hedberg 
23675e84b7cSJohan Hedberg 	add_wait_queue(&hdev->req_wait_q, &wait);
23775e84b7cSJohan Hedberg 	set_current_state(TASK_INTERRUPTIBLE);
23875e84b7cSJohan Hedberg 
239039fada5SChan-yeol Park 	err = hci_req_run(&req, hci_req_sync_complete);
240039fada5SChan-yeol Park 	if (err < 0) {
241039fada5SChan-yeol Park 		remove_wait_queue(&hdev->req_wait_q, &wait);
24222a3ceabSJohan Hedberg 		set_current_state(TASK_RUNNING);
243039fada5SChan-yeol Park 		return ERR_PTR(err);
244039fada5SChan-yeol Park 	}
245039fada5SChan-yeol Park 
24675e84b7cSJohan Hedberg 	schedule_timeout(timeout);
24775e84b7cSJohan Hedberg 
24875e84b7cSJohan Hedberg 	remove_wait_queue(&hdev->req_wait_q, &wait);
24975e84b7cSJohan Hedberg 
25075e84b7cSJohan Hedberg 	if (signal_pending(current))
25175e84b7cSJohan Hedberg 		return ERR_PTR(-EINTR);
25275e84b7cSJohan Hedberg 
25375e84b7cSJohan Hedberg 	switch (hdev->req_status) {
25475e84b7cSJohan Hedberg 	case HCI_REQ_DONE:
25575e84b7cSJohan Hedberg 		err = -bt_to_errno(hdev->req_result);
25675e84b7cSJohan Hedberg 		break;
25775e84b7cSJohan Hedberg 
25875e84b7cSJohan Hedberg 	case HCI_REQ_CANCELED:
25975e84b7cSJohan Hedberg 		err = -hdev->req_result;
26075e84b7cSJohan Hedberg 		break;
26175e84b7cSJohan Hedberg 
26275e84b7cSJohan Hedberg 	default:
26375e84b7cSJohan Hedberg 		err = -ETIMEDOUT;
26475e84b7cSJohan Hedberg 		break;
26575e84b7cSJohan Hedberg 	}
26675e84b7cSJohan Hedberg 
26775e84b7cSJohan Hedberg 	hdev->req_status = hdev->req_result = 0;
26875e84b7cSJohan Hedberg 
26975e84b7cSJohan Hedberg 	BT_DBG("%s end: err %d", hdev->name, err);
27075e84b7cSJohan Hedberg 
27175e84b7cSJohan Hedberg 	if (err < 0)
27275e84b7cSJohan Hedberg 		return ERR_PTR(err);
27375e84b7cSJohan Hedberg 
2747b1abbbeSJohan Hedberg 	return hci_get_cmd_complete(hdev, opcode, event);
2757b1abbbeSJohan Hedberg }
2767b1abbbeSJohan Hedberg EXPORT_SYMBOL(__hci_cmd_sync_ev);
2777b1abbbeSJohan Hedberg 
2787b1abbbeSJohan Hedberg struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
27907dc93ddSJohan Hedberg 			       const void *param, u32 timeout)
2807b1abbbeSJohan Hedberg {
2817b1abbbeSJohan Hedberg 	return __hci_cmd_sync_ev(hdev, opcode, plen, param, 0, timeout);
28275e84b7cSJohan Hedberg }
28375e84b7cSJohan Hedberg EXPORT_SYMBOL(__hci_cmd_sync);
28475e84b7cSJohan Hedberg 
2851da177e4SLinus Torvalds /* Execute request and wait for completion. */
28601178cd4SJohan Hedberg static int __hci_req_sync(struct hci_dev *hdev,
28742c6b129SJohan Hedberg 			  void (*func)(struct hci_request *req,
28842c6b129SJohan Hedberg 				      unsigned long opt),
2891da177e4SLinus Torvalds 			  unsigned long opt, __u32 timeout)
2901da177e4SLinus Torvalds {
29142c6b129SJohan Hedberg 	struct hci_request req;
2921da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
2931da177e4SLinus Torvalds 	int err = 0;
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	BT_DBG("%s start", hdev->name);
2961da177e4SLinus Torvalds 
29742c6b129SJohan Hedberg 	hci_req_init(&req, hdev);
29842c6b129SJohan Hedberg 
2991da177e4SLinus Torvalds 	hdev->req_status = HCI_REQ_PEND;
3001da177e4SLinus Torvalds 
30142c6b129SJohan Hedberg 	func(&req, opt);
30253cce22dSJohan Hedberg 
303039fada5SChan-yeol Park 	add_wait_queue(&hdev->req_wait_q, &wait);
304039fada5SChan-yeol Park 	set_current_state(TASK_INTERRUPTIBLE);
305039fada5SChan-yeol Park 
30642c6b129SJohan Hedberg 	err = hci_req_run(&req, hci_req_sync_complete);
30742c6b129SJohan Hedberg 	if (err < 0) {
30853cce22dSJohan Hedberg 		hdev->req_status = 0;
309920c8300SAndre Guedes 
310039fada5SChan-yeol Park 		remove_wait_queue(&hdev->req_wait_q, &wait);
31122a3ceabSJohan Hedberg 		set_current_state(TASK_RUNNING);
312039fada5SChan-yeol Park 
313920c8300SAndre Guedes 		/* ENODATA means the HCI request command queue is empty.
314920c8300SAndre Guedes 		 * This can happen when a request with conditionals doesn't
315920c8300SAndre Guedes 		 * trigger any commands to be sent. This is normal behavior
316920c8300SAndre Guedes 		 * and should not trigger an error return.
31742c6b129SJohan Hedberg 		 */
318920c8300SAndre Guedes 		if (err == -ENODATA)
31942c6b129SJohan Hedberg 			return 0;
320920c8300SAndre Guedes 
321920c8300SAndre Guedes 		return err;
32253cce22dSJohan Hedberg 	}
32353cce22dSJohan Hedberg 
3241da177e4SLinus Torvalds 	schedule_timeout(timeout);
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	remove_wait_queue(&hdev->req_wait_q, &wait);
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	if (signal_pending(current))
3291da177e4SLinus Torvalds 		return -EINTR;
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	switch (hdev->req_status) {
3321da177e4SLinus Torvalds 	case HCI_REQ_DONE:
333e175072fSJoe Perches 		err = -bt_to_errno(hdev->req_result);
3341da177e4SLinus Torvalds 		break;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	case HCI_REQ_CANCELED:
3371da177e4SLinus Torvalds 		err = -hdev->req_result;
3381da177e4SLinus Torvalds 		break;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	default:
3411da177e4SLinus Torvalds 		err = -ETIMEDOUT;
3421da177e4SLinus Torvalds 		break;
3433ff50b79SStephen Hemminger 	}
3441da177e4SLinus Torvalds 
345a5040efaSJohan Hedberg 	hdev->req_status = hdev->req_result = 0;
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 	BT_DBG("%s end: err %d", hdev->name, err);
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	return err;
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
35201178cd4SJohan Hedberg static int hci_req_sync(struct hci_dev *hdev,
35342c6b129SJohan Hedberg 			void (*req)(struct hci_request *req,
35442c6b129SJohan Hedberg 				    unsigned long opt),
3551da177e4SLinus Torvalds 			unsigned long opt, __u32 timeout)
3561da177e4SLinus Torvalds {
3571da177e4SLinus Torvalds 	int ret;
3581da177e4SLinus Torvalds 
3597c6a329eSMarcel Holtmann 	if (!test_bit(HCI_UP, &hdev->flags))
3607c6a329eSMarcel Holtmann 		return -ENETDOWN;
3617c6a329eSMarcel Holtmann 
3621da177e4SLinus Torvalds 	/* Serialize all requests */
3631da177e4SLinus Torvalds 	hci_req_lock(hdev);
36401178cd4SJohan Hedberg 	ret = __hci_req_sync(hdev, req, opt, timeout);
3651da177e4SLinus Torvalds 	hci_req_unlock(hdev);
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	return ret;
3681da177e4SLinus Torvalds }
3691da177e4SLinus Torvalds 
37042c6b129SJohan Hedberg static void hci_reset_req(struct hci_request *req, unsigned long opt)
3711da177e4SLinus Torvalds {
37242c6b129SJohan Hedberg 	BT_DBG("%s %ld", req->hdev->name, opt);
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	/* Reset device */
37542c6b129SJohan Hedberg 	set_bit(HCI_RESET, &req->hdev->flags);
37642c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_RESET, 0, NULL);
3771da177e4SLinus Torvalds }
3781da177e4SLinus Torvalds 
37942c6b129SJohan Hedberg static void bredr_init(struct hci_request *req)
3801da177e4SLinus Torvalds {
38142c6b129SJohan Hedberg 	req->hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED;
3822455a3eaSAndrei Emeltchenko 
3831da177e4SLinus Torvalds 	/* Read Local Supported Features */
38442c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_READ_LOCAL_FEATURES, 0, NULL);
3851da177e4SLinus Torvalds 
3861143e5a6SMarcel Holtmann 	/* Read Local Version */
38742c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL);
3882177bab5SJohan Hedberg 
3892177bab5SJohan Hedberg 	/* Read BD Address */
39042c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_READ_BD_ADDR, 0, NULL);
3911da177e4SLinus Torvalds }
3921da177e4SLinus Torvalds 
39342c6b129SJohan Hedberg static void amp_init(struct hci_request *req)
394e61ef499SAndrei Emeltchenko {
39542c6b129SJohan Hedberg 	req->hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED;
3962455a3eaSAndrei Emeltchenko 
397e61ef499SAndrei Emeltchenko 	/* Read Local Version */
39842c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL);
3996bcbc489SAndrei Emeltchenko 
400f6996cfeSMarcel Holtmann 	/* Read Local Supported Commands */
401f6996cfeSMarcel Holtmann 	hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL);
402f6996cfeSMarcel Holtmann 
403f6996cfeSMarcel Holtmann 	/* Read Local Supported Features */
404f6996cfeSMarcel Holtmann 	hci_req_add(req, HCI_OP_READ_LOCAL_FEATURES, 0, NULL);
405f6996cfeSMarcel Holtmann 
4066bcbc489SAndrei Emeltchenko 	/* Read Local AMP Info */
40742c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL);
408e71dfabaSAndrei Emeltchenko 
409e71dfabaSAndrei Emeltchenko 	/* Read Data Blk size */
41042c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_READ_DATA_BLOCK_SIZE, 0, NULL);
4117528ca1cSMarcel Holtmann 
412f38ba941SMarcel Holtmann 	/* Read Flow Control Mode */
413f38ba941SMarcel Holtmann 	hci_req_add(req, HCI_OP_READ_FLOW_CONTROL_MODE, 0, NULL);
414f38ba941SMarcel Holtmann 
4157528ca1cSMarcel Holtmann 	/* Read Location Data */
4167528ca1cSMarcel Holtmann 	hci_req_add(req, HCI_OP_READ_LOCATION_DATA, 0, NULL);
417e61ef499SAndrei Emeltchenko }
418e61ef499SAndrei Emeltchenko 
41942c6b129SJohan Hedberg static void hci_init1_req(struct hci_request *req, unsigned long opt)
420e61ef499SAndrei Emeltchenko {
42142c6b129SJohan Hedberg 	struct hci_dev *hdev = req->hdev;
422e61ef499SAndrei Emeltchenko 
423e61ef499SAndrei Emeltchenko 	BT_DBG("%s %ld", hdev->name, opt);
424e61ef499SAndrei Emeltchenko 
42511778716SAndrei Emeltchenko 	/* Reset */
42611778716SAndrei Emeltchenko 	if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks))
42742c6b129SJohan Hedberg 		hci_reset_req(req, 0);
42811778716SAndrei Emeltchenko 
429e61ef499SAndrei Emeltchenko 	switch (hdev->dev_type) {
430e61ef499SAndrei Emeltchenko 	case HCI_BREDR:
43142c6b129SJohan Hedberg 		bredr_init(req);
432e61ef499SAndrei Emeltchenko 		break;
433e61ef499SAndrei Emeltchenko 
434e61ef499SAndrei Emeltchenko 	case HCI_AMP:
43542c6b129SJohan Hedberg 		amp_init(req);
436e61ef499SAndrei Emeltchenko 		break;
437e61ef499SAndrei Emeltchenko 
438e61ef499SAndrei Emeltchenko 	default:
439e61ef499SAndrei Emeltchenko 		BT_ERR("Unknown device type %d", hdev->dev_type);
440e61ef499SAndrei Emeltchenko 		break;
441e61ef499SAndrei Emeltchenko 	}
442e61ef499SAndrei Emeltchenko }
443e61ef499SAndrei Emeltchenko 
44442c6b129SJohan Hedberg static void bredr_setup(struct hci_request *req)
4452177bab5SJohan Hedberg {
4462177bab5SJohan Hedberg 	__le16 param;
4472177bab5SJohan Hedberg 	__u8 flt_type;
4482177bab5SJohan Hedberg 
4492177bab5SJohan Hedberg 	/* Read Buffer Size (ACL mtu, max pkt, etc.) */
45042c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_READ_BUFFER_SIZE, 0, NULL);
4512177bab5SJohan Hedberg 
4522177bab5SJohan Hedberg 	/* Read Class of Device */
45342c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_READ_CLASS_OF_DEV, 0, NULL);
4542177bab5SJohan Hedberg 
4552177bab5SJohan Hedberg 	/* Read Local Name */
45642c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_READ_LOCAL_NAME, 0, NULL);
4572177bab5SJohan Hedberg 
4582177bab5SJohan Hedberg 	/* Read Voice Setting */
45942c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_READ_VOICE_SETTING, 0, NULL);
4602177bab5SJohan Hedberg 
461b4cb9fb2SMarcel Holtmann 	/* Read Number of Supported IAC */
462b4cb9fb2SMarcel Holtmann 	hci_req_add(req, HCI_OP_READ_NUM_SUPPORTED_IAC, 0, NULL);
463b4cb9fb2SMarcel Holtmann 
4644b836f39SMarcel Holtmann 	/* Read Current IAC LAP */
4654b836f39SMarcel Holtmann 	hci_req_add(req, HCI_OP_READ_CURRENT_IAC_LAP, 0, NULL);
4664b836f39SMarcel Holtmann 
4672177bab5SJohan Hedberg 	/* Clear Event Filters */
4682177bab5SJohan Hedberg 	flt_type = HCI_FLT_CLEAR_ALL;
46942c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_SET_EVENT_FLT, 1, &flt_type);
4702177bab5SJohan Hedberg 
4712177bab5SJohan Hedberg 	/* Connection accept timeout ~20 secs */
472dcf4adbfSJoe Perches 	param = cpu_to_le16(0x7d00);
47342c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_WRITE_CA_TIMEOUT, 2, &param);
4742177bab5SJohan Hedberg }
4752177bab5SJohan Hedberg 
47642c6b129SJohan Hedberg static void le_setup(struct hci_request *req)
4772177bab5SJohan Hedberg {
478c73eee91SJohan Hedberg 	struct hci_dev *hdev = req->hdev;
479c73eee91SJohan Hedberg 
4802177bab5SJohan Hedberg 	/* Read LE Buffer Size */
48142c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_LE_READ_BUFFER_SIZE, 0, NULL);
4822177bab5SJohan Hedberg 
4832177bab5SJohan Hedberg 	/* Read LE Local Supported Features */
48442c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_LE_READ_LOCAL_FEATURES, 0, NULL);
4852177bab5SJohan Hedberg 
486747d3f03SMarcel Holtmann 	/* Read LE Supported States */
487747d3f03SMarcel Holtmann 	hci_req_add(req, HCI_OP_LE_READ_SUPPORTED_STATES, 0, NULL);
488747d3f03SMarcel Holtmann 
4892177bab5SJohan Hedberg 	/* Read LE White List Size */
49042c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_LE_READ_WHITE_LIST_SIZE, 0, NULL);
4912177bab5SJohan Hedberg 
492747d3f03SMarcel Holtmann 	/* Clear LE White List */
493747d3f03SMarcel Holtmann 	hci_req_add(req, HCI_OP_LE_CLEAR_WHITE_LIST, 0, NULL);
494c73eee91SJohan Hedberg 
495c73eee91SJohan Hedberg 	/* LE-only controllers have LE implicitly enabled */
496c73eee91SJohan Hedberg 	if (!lmp_bredr_capable(hdev))
497c73eee91SJohan Hedberg 		set_bit(HCI_LE_ENABLED, &hdev->dev_flags);
4982177bab5SJohan Hedberg }
4992177bab5SJohan Hedberg 
5002177bab5SJohan Hedberg static u8 hci_get_inquiry_mode(struct hci_dev *hdev)
5012177bab5SJohan Hedberg {
5022177bab5SJohan Hedberg 	if (lmp_ext_inq_capable(hdev))
5032177bab5SJohan Hedberg 		return 0x02;
5042177bab5SJohan Hedberg 
5052177bab5SJohan Hedberg 	if (lmp_inq_rssi_capable(hdev))
5062177bab5SJohan Hedberg 		return 0x01;
5072177bab5SJohan Hedberg 
5082177bab5SJohan Hedberg 	if (hdev->manufacturer == 11 && hdev->hci_rev == 0x00 &&
5092177bab5SJohan Hedberg 	    hdev->lmp_subver == 0x0757)
5102177bab5SJohan Hedberg 		return 0x01;
5112177bab5SJohan Hedberg 
5122177bab5SJohan Hedberg 	if (hdev->manufacturer == 15) {
5132177bab5SJohan Hedberg 		if (hdev->hci_rev == 0x03 && hdev->lmp_subver == 0x6963)
5142177bab5SJohan Hedberg 			return 0x01;
5152177bab5SJohan Hedberg 		if (hdev->hci_rev == 0x09 && hdev->lmp_subver == 0x6963)
5162177bab5SJohan Hedberg 			return 0x01;
5172177bab5SJohan Hedberg 		if (hdev->hci_rev == 0x00 && hdev->lmp_subver == 0x6965)
5182177bab5SJohan Hedberg 			return 0x01;
5192177bab5SJohan Hedberg 	}
5202177bab5SJohan Hedberg 
5212177bab5SJohan Hedberg 	if (hdev->manufacturer == 31 && hdev->hci_rev == 0x2005 &&
5222177bab5SJohan Hedberg 	    hdev->lmp_subver == 0x1805)
5232177bab5SJohan Hedberg 		return 0x01;
5242177bab5SJohan Hedberg 
5252177bab5SJohan Hedberg 	return 0x00;
5262177bab5SJohan Hedberg }
5272177bab5SJohan Hedberg 
52842c6b129SJohan Hedberg static void hci_setup_inquiry_mode(struct hci_request *req)
5292177bab5SJohan Hedberg {
5302177bab5SJohan Hedberg 	u8 mode;
5312177bab5SJohan Hedberg 
53242c6b129SJohan Hedberg 	mode = hci_get_inquiry_mode(req->hdev);
5332177bab5SJohan Hedberg 
53442c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_WRITE_INQUIRY_MODE, 1, &mode);
5352177bab5SJohan Hedberg }
5362177bab5SJohan Hedberg 
53742c6b129SJohan Hedberg static void hci_setup_event_mask(struct hci_request *req)
5382177bab5SJohan Hedberg {
53942c6b129SJohan Hedberg 	struct hci_dev *hdev = req->hdev;
54042c6b129SJohan Hedberg 
5412177bab5SJohan Hedberg 	/* The second byte is 0xff instead of 0x9f (two reserved bits
5422177bab5SJohan Hedberg 	 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
5432177bab5SJohan Hedberg 	 * command otherwise.
5442177bab5SJohan Hedberg 	 */
5452177bab5SJohan Hedberg 	u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
5462177bab5SJohan Hedberg 
5472177bab5SJohan Hedberg 	/* CSR 1.1 dongles does not accept any bitfield so don't try to set
5482177bab5SJohan Hedberg 	 * any event mask for pre 1.2 devices.
5492177bab5SJohan Hedberg 	 */
5502177bab5SJohan Hedberg 	if (hdev->hci_ver < BLUETOOTH_VER_1_2)
5512177bab5SJohan Hedberg 		return;
5522177bab5SJohan Hedberg 
5532177bab5SJohan Hedberg 	if (lmp_bredr_capable(hdev)) {
5542177bab5SJohan Hedberg 		events[4] |= 0x01; /* Flow Specification Complete */
5552177bab5SJohan Hedberg 		events[4] |= 0x02; /* Inquiry Result with RSSI */
5562177bab5SJohan Hedberg 		events[4] |= 0x04; /* Read Remote Extended Features Complete */
5572177bab5SJohan Hedberg 		events[5] |= 0x08; /* Synchronous Connection Complete */
5582177bab5SJohan Hedberg 		events[5] |= 0x10; /* Synchronous Connection Changed */
559c7882cbdSMarcel Holtmann 	} else {
560c7882cbdSMarcel Holtmann 		/* Use a different default for LE-only devices */
561c7882cbdSMarcel Holtmann 		memset(events, 0, sizeof(events));
562c7882cbdSMarcel Holtmann 		events[0] |= 0x10; /* Disconnection Complete */
563c7882cbdSMarcel Holtmann 		events[1] |= 0x08; /* Read Remote Version Information Complete */
564c7882cbdSMarcel Holtmann 		events[1] |= 0x20; /* Command Complete */
565c7882cbdSMarcel Holtmann 		events[1] |= 0x40; /* Command Status */
566c7882cbdSMarcel Holtmann 		events[1] |= 0x80; /* Hardware Error */
567c7882cbdSMarcel Holtmann 		events[2] |= 0x04; /* Number of Completed Packets */
568c7882cbdSMarcel Holtmann 		events[3] |= 0x02; /* Data Buffer Overflow */
5690da71f1bSMarcel Holtmann 
5700da71f1bSMarcel Holtmann 		if (hdev->le_features[0] & HCI_LE_ENCRYPTION) {
5710da71f1bSMarcel Holtmann 			events[0] |= 0x80; /* Encryption Change */
572c7882cbdSMarcel Holtmann 			events[5] |= 0x80; /* Encryption Key Refresh Complete */
5732177bab5SJohan Hedberg 		}
5740da71f1bSMarcel Holtmann 	}
5752177bab5SJohan Hedberg 
5762177bab5SJohan Hedberg 	if (lmp_inq_rssi_capable(hdev))
5772177bab5SJohan Hedberg 		events[4] |= 0x02; /* Inquiry Result with RSSI */
5782177bab5SJohan Hedberg 
5792177bab5SJohan Hedberg 	if (lmp_sniffsubr_capable(hdev))
5802177bab5SJohan Hedberg 		events[5] |= 0x20; /* Sniff Subrating */
5812177bab5SJohan Hedberg 
5822177bab5SJohan Hedberg 	if (lmp_pause_enc_capable(hdev))
5832177bab5SJohan Hedberg 		events[5] |= 0x80; /* Encryption Key Refresh Complete */
5842177bab5SJohan Hedberg 
5852177bab5SJohan Hedberg 	if (lmp_ext_inq_capable(hdev))
5862177bab5SJohan Hedberg 		events[5] |= 0x40; /* Extended Inquiry Result */
5872177bab5SJohan Hedberg 
5882177bab5SJohan Hedberg 	if (lmp_no_flush_capable(hdev))
5892177bab5SJohan Hedberg 		events[7] |= 0x01; /* Enhanced Flush Complete */
5902177bab5SJohan Hedberg 
5912177bab5SJohan Hedberg 	if (lmp_lsto_capable(hdev))
5922177bab5SJohan Hedberg 		events[6] |= 0x80; /* Link Supervision Timeout Changed */
5932177bab5SJohan Hedberg 
5942177bab5SJohan Hedberg 	if (lmp_ssp_capable(hdev)) {
5952177bab5SJohan Hedberg 		events[6] |= 0x01;	/* IO Capability Request */
5962177bab5SJohan Hedberg 		events[6] |= 0x02;	/* IO Capability Response */
5972177bab5SJohan Hedberg 		events[6] |= 0x04;	/* User Confirmation Request */
5982177bab5SJohan Hedberg 		events[6] |= 0x08;	/* User Passkey Request */
5992177bab5SJohan Hedberg 		events[6] |= 0x10;	/* Remote OOB Data Request */
6002177bab5SJohan Hedberg 		events[6] |= 0x20;	/* Simple Pairing Complete */
6012177bab5SJohan Hedberg 		events[7] |= 0x04;	/* User Passkey Notification */
6022177bab5SJohan Hedberg 		events[7] |= 0x08;	/* Keypress Notification */
6032177bab5SJohan Hedberg 		events[7] |= 0x10;	/* Remote Host Supported
6042177bab5SJohan Hedberg 					 * Features Notification
6052177bab5SJohan Hedberg 					 */
6062177bab5SJohan Hedberg 	}
6072177bab5SJohan Hedberg 
6082177bab5SJohan Hedberg 	if (lmp_le_capable(hdev))
6092177bab5SJohan Hedberg 		events[7] |= 0x20;	/* LE Meta-Event */
6102177bab5SJohan Hedberg 
61142c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_SET_EVENT_MASK, sizeof(events), events);
6122177bab5SJohan Hedberg }
6132177bab5SJohan Hedberg 
61442c6b129SJohan Hedberg static void hci_init2_req(struct hci_request *req, unsigned long opt)
6152177bab5SJohan Hedberg {
61642c6b129SJohan Hedberg 	struct hci_dev *hdev = req->hdev;
61742c6b129SJohan Hedberg 
6182177bab5SJohan Hedberg 	if (lmp_bredr_capable(hdev))
61942c6b129SJohan Hedberg 		bredr_setup(req);
62056f87901SJohan Hedberg 	else
62156f87901SJohan Hedberg 		clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags);
6222177bab5SJohan Hedberg 
6232177bab5SJohan Hedberg 	if (lmp_le_capable(hdev))
62442c6b129SJohan Hedberg 		le_setup(req);
6252177bab5SJohan Hedberg 
6263f8e2d75SJohan Hedberg 	/* AVM Berlin (31), aka "BlueFRITZ!", doesn't support the read
6273f8e2d75SJohan Hedberg 	 * local supported commands HCI command.
6283f8e2d75SJohan Hedberg 	 */
6293f8e2d75SJohan Hedberg 	if (hdev->manufacturer != 31 && hdev->hci_ver > BLUETOOTH_VER_1_1)
63042c6b129SJohan Hedberg 		hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL);
6312177bab5SJohan Hedberg 
6322177bab5SJohan Hedberg 	if (lmp_ssp_capable(hdev)) {
63357af75a8SMarcel Holtmann 		/* When SSP is available, then the host features page
63457af75a8SMarcel Holtmann 		 * should also be available as well. However some
63557af75a8SMarcel Holtmann 		 * controllers list the max_page as 0 as long as SSP
63657af75a8SMarcel Holtmann 		 * has not been enabled. To achieve proper debugging
63757af75a8SMarcel Holtmann 		 * output, force the minimum max_page to 1 at least.
63857af75a8SMarcel Holtmann 		 */
63957af75a8SMarcel Holtmann 		hdev->max_page = 0x01;
64057af75a8SMarcel Holtmann 
6412177bab5SJohan Hedberg 		if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
6422177bab5SJohan Hedberg 			u8 mode = 0x01;
64342c6b129SJohan Hedberg 			hci_req_add(req, HCI_OP_WRITE_SSP_MODE,
6442177bab5SJohan Hedberg 				    sizeof(mode), &mode);
6452177bab5SJohan Hedberg 		} else {
6462177bab5SJohan Hedberg 			struct hci_cp_write_eir cp;
6472177bab5SJohan Hedberg 
6482177bab5SJohan Hedberg 			memset(hdev->eir, 0, sizeof(hdev->eir));
6492177bab5SJohan Hedberg 			memset(&cp, 0, sizeof(cp));
6502177bab5SJohan Hedberg 
65142c6b129SJohan Hedberg 			hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
6522177bab5SJohan Hedberg 		}
6532177bab5SJohan Hedberg 	}
6542177bab5SJohan Hedberg 
6552177bab5SJohan Hedberg 	if (lmp_inq_rssi_capable(hdev))
65642c6b129SJohan Hedberg 		hci_setup_inquiry_mode(req);
6572177bab5SJohan Hedberg 
6582177bab5SJohan Hedberg 	if (lmp_inq_tx_pwr_capable(hdev))
65942c6b129SJohan Hedberg 		hci_req_add(req, HCI_OP_READ_INQ_RSP_TX_POWER, 0, NULL);
6602177bab5SJohan Hedberg 
6612177bab5SJohan Hedberg 	if (lmp_ext_feat_capable(hdev)) {
6622177bab5SJohan Hedberg 		struct hci_cp_read_local_ext_features cp;
6632177bab5SJohan Hedberg 
6642177bab5SJohan Hedberg 		cp.page = 0x01;
66542c6b129SJohan Hedberg 		hci_req_add(req, HCI_OP_READ_LOCAL_EXT_FEATURES,
66642c6b129SJohan Hedberg 			    sizeof(cp), &cp);
6672177bab5SJohan Hedberg 	}
6682177bab5SJohan Hedberg 
6692177bab5SJohan Hedberg 	if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) {
6702177bab5SJohan Hedberg 		u8 enable = 1;
67142c6b129SJohan Hedberg 		hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE, sizeof(enable),
6722177bab5SJohan Hedberg 			    &enable);
6732177bab5SJohan Hedberg 	}
6742177bab5SJohan Hedberg }
6752177bab5SJohan Hedberg 
67642c6b129SJohan Hedberg static void hci_setup_link_policy(struct hci_request *req)
6772177bab5SJohan Hedberg {
67842c6b129SJohan Hedberg 	struct hci_dev *hdev = req->hdev;
6792177bab5SJohan Hedberg 	struct hci_cp_write_def_link_policy cp;
6802177bab5SJohan Hedberg 	u16 link_policy = 0;
6812177bab5SJohan Hedberg 
6822177bab5SJohan Hedberg 	if (lmp_rswitch_capable(hdev))
6832177bab5SJohan Hedberg 		link_policy |= HCI_LP_RSWITCH;
6842177bab5SJohan Hedberg 	if (lmp_hold_capable(hdev))
6852177bab5SJohan Hedberg 		link_policy |= HCI_LP_HOLD;
6862177bab5SJohan Hedberg 	if (lmp_sniff_capable(hdev))
6872177bab5SJohan Hedberg 		link_policy |= HCI_LP_SNIFF;
6882177bab5SJohan Hedberg 	if (lmp_park_capable(hdev))
6892177bab5SJohan Hedberg 		link_policy |= HCI_LP_PARK;
6902177bab5SJohan Hedberg 
6912177bab5SJohan Hedberg 	cp.policy = cpu_to_le16(link_policy);
69242c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_WRITE_DEF_LINK_POLICY, sizeof(cp), &cp);
6932177bab5SJohan Hedberg }
6942177bab5SJohan Hedberg 
69542c6b129SJohan Hedberg static void hci_set_le_support(struct hci_request *req)
6962177bab5SJohan Hedberg {
69742c6b129SJohan Hedberg 	struct hci_dev *hdev = req->hdev;
6982177bab5SJohan Hedberg 	struct hci_cp_write_le_host_supported cp;
6992177bab5SJohan Hedberg 
700c73eee91SJohan Hedberg 	/* LE-only devices do not support explicit enablement */
701c73eee91SJohan Hedberg 	if (!lmp_bredr_capable(hdev))
702c73eee91SJohan Hedberg 		return;
703c73eee91SJohan Hedberg 
7042177bab5SJohan Hedberg 	memset(&cp, 0, sizeof(cp));
7052177bab5SJohan Hedberg 
7062177bab5SJohan Hedberg 	if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) {
7072177bab5SJohan Hedberg 		cp.le = 0x01;
70832226e4fSMarcel Holtmann 		cp.simul = 0x00;
7092177bab5SJohan Hedberg 	}
7102177bab5SJohan Hedberg 
7112177bab5SJohan Hedberg 	if (cp.le != lmp_host_le_capable(hdev))
71242c6b129SJohan Hedberg 		hci_req_add(req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(cp),
7132177bab5SJohan Hedberg 			    &cp);
7142177bab5SJohan Hedberg }
7152177bab5SJohan Hedberg 
716d62e6d67SJohan Hedberg static void hci_set_event_mask_page_2(struct hci_request *req)
717d62e6d67SJohan Hedberg {
718d62e6d67SJohan Hedberg 	struct hci_dev *hdev = req->hdev;
719d62e6d67SJohan Hedberg 	u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
720d62e6d67SJohan Hedberg 
721d62e6d67SJohan Hedberg 	/* If Connectionless Slave Broadcast master role is supported
722d62e6d67SJohan Hedberg 	 * enable all necessary events for it.
723d62e6d67SJohan Hedberg 	 */
72453b834d2SMarcel Holtmann 	if (lmp_csb_master_capable(hdev)) {
725d62e6d67SJohan Hedberg 		events[1] |= 0x40;	/* Triggered Clock Capture */
726d62e6d67SJohan Hedberg 		events[1] |= 0x80;	/* Synchronization Train Complete */
727d62e6d67SJohan Hedberg 		events[2] |= 0x10;	/* Slave Page Response Timeout */
728d62e6d67SJohan Hedberg 		events[2] |= 0x20;	/* CSB Channel Map Change */
729d62e6d67SJohan Hedberg 	}
730d62e6d67SJohan Hedberg 
731d62e6d67SJohan Hedberg 	/* If Connectionless Slave Broadcast slave role is supported
732d62e6d67SJohan Hedberg 	 * enable all necessary events for it.
733d62e6d67SJohan Hedberg 	 */
73453b834d2SMarcel Holtmann 	if (lmp_csb_slave_capable(hdev)) {
735d62e6d67SJohan Hedberg 		events[2] |= 0x01;	/* Synchronization Train Received */
736d62e6d67SJohan Hedberg 		events[2] |= 0x02;	/* CSB Receive */
737d62e6d67SJohan Hedberg 		events[2] |= 0x04;	/* CSB Timeout */
738d62e6d67SJohan Hedberg 		events[2] |= 0x08;	/* Truncated Page Complete */
739d62e6d67SJohan Hedberg 	}
740d62e6d67SJohan Hedberg 
74140c59fcbSMarcel Holtmann 	/* Enable Authenticated Payload Timeout Expired event if supported */
742cd7ca0ecSMarcel Holtmann 	if (lmp_ping_capable(hdev) || hdev->le_features[0] & HCI_LE_PING)
74340c59fcbSMarcel Holtmann 		events[2] |= 0x80;
74440c59fcbSMarcel Holtmann 
745d62e6d67SJohan Hedberg 	hci_req_add(req, HCI_OP_SET_EVENT_MASK_PAGE_2, sizeof(events), events);
746d62e6d67SJohan Hedberg }
747d62e6d67SJohan Hedberg 
74842c6b129SJohan Hedberg static void hci_init3_req(struct hci_request *req, unsigned long opt)
7492177bab5SJohan Hedberg {
75042c6b129SJohan Hedberg 	struct hci_dev *hdev = req->hdev;
751d2c5d77fSJohan Hedberg 	u8 p;
75242c6b129SJohan Hedberg 
7530da71f1bSMarcel Holtmann 	hci_setup_event_mask(req);
7540da71f1bSMarcel Holtmann 
755b8f4e068SGustavo Padovan 	/* Some Broadcom based Bluetooth controllers do not support the
756b8f4e068SGustavo Padovan 	 * Delete Stored Link Key command. They are clearly indicating its
757b8f4e068SGustavo Padovan 	 * absence in the bit mask of supported commands.
758b8f4e068SGustavo Padovan 	 *
759b8f4e068SGustavo Padovan 	 * Check the supported commands and only if the the command is marked
760b8f4e068SGustavo Padovan 	 * as supported send it. If not supported assume that the controller
761b8f4e068SGustavo Padovan 	 * does not have actual support for stored link keys which makes this
762b8f4e068SGustavo Padovan 	 * command redundant anyway.
763f9f462faSMarcel Holtmann 	 *
764f9f462faSMarcel Holtmann 	 * Some controllers indicate that they support handling deleting
765f9f462faSMarcel Holtmann 	 * stored link keys, but they don't. The quirk lets a driver
766f9f462faSMarcel Holtmann 	 * just disable this command.
767b8f4e068SGustavo Padovan 	 */
768f9f462faSMarcel Holtmann 	if (hdev->commands[6] & 0x80 &&
769f9f462faSMarcel Holtmann 	    !test_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks)) {
77059f45d57SJohan Hedberg 		struct hci_cp_delete_stored_link_key cp;
77159f45d57SJohan Hedberg 
77259f45d57SJohan Hedberg 		bacpy(&cp.bdaddr, BDADDR_ANY);
77359f45d57SJohan Hedberg 		cp.delete_all = 0x01;
77459f45d57SJohan Hedberg 		hci_req_add(req, HCI_OP_DELETE_STORED_LINK_KEY,
77559f45d57SJohan Hedberg 			    sizeof(cp), &cp);
77659f45d57SJohan Hedberg 	}
77759f45d57SJohan Hedberg 
7782177bab5SJohan Hedberg 	if (hdev->commands[5] & 0x10)
77942c6b129SJohan Hedberg 		hci_setup_link_policy(req);
7802177bab5SJohan Hedberg 
781417287deSMarcel Holtmann 	if (hdev->commands[8] & 0x01)
782417287deSMarcel Holtmann 		hci_req_add(req, HCI_OP_READ_PAGE_SCAN_ACTIVITY, 0, NULL);
783417287deSMarcel Holtmann 
784417287deSMarcel Holtmann 	/* Some older Broadcom based Bluetooth 1.2 controllers do not
785417287deSMarcel Holtmann 	 * support the Read Page Scan Type command. Check support for
786417287deSMarcel Holtmann 	 * this command in the bit mask of supported commands.
787417287deSMarcel Holtmann 	 */
788417287deSMarcel Holtmann 	if (hdev->commands[13] & 0x01)
789417287deSMarcel Holtmann 		hci_req_add(req, HCI_OP_READ_PAGE_SCAN_TYPE, 0, NULL);
790417287deSMarcel Holtmann 
7919193c6e8SAndre Guedes 	if (lmp_le_capable(hdev)) {
7929193c6e8SAndre Guedes 		u8 events[8];
7939193c6e8SAndre Guedes 
7949193c6e8SAndre Guedes 		memset(events, 0, sizeof(events));
7954d6c705bSMarcel Holtmann 		events[0] = 0x0f;
7964d6c705bSMarcel Holtmann 
7974d6c705bSMarcel Holtmann 		if (hdev->le_features[0] & HCI_LE_ENCRYPTION)
7984d6c705bSMarcel Holtmann 			events[0] |= 0x10;	/* LE Long Term Key Request */
799662bc2e6SAndre Guedes 
800662bc2e6SAndre Guedes 		/* If controller supports the Connection Parameters Request
801662bc2e6SAndre Guedes 		 * Link Layer Procedure, enable the corresponding event.
802662bc2e6SAndre Guedes 		 */
803662bc2e6SAndre Guedes 		if (hdev->le_features[0] & HCI_LE_CONN_PARAM_REQ_PROC)
804662bc2e6SAndre Guedes 			events[0] |= 0x20;	/* LE Remote Connection
805662bc2e6SAndre Guedes 						 * Parameter Request
806662bc2e6SAndre Guedes 						 */
807662bc2e6SAndre Guedes 
808a9f6068eSMarcel Holtmann 		/* If the controller supports the Data Length Extension
809a9f6068eSMarcel Holtmann 		 * feature, enable the corresponding event.
810a9f6068eSMarcel Holtmann 		 */
811a9f6068eSMarcel Holtmann 		if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT)
812a9f6068eSMarcel Holtmann 			events[0] |= 0x40;	/* LE Data Length Change */
813a9f6068eSMarcel Holtmann 
8144b71bba4SMarcel Holtmann 		/* If the controller supports Extended Scanner Filter
8154b71bba4SMarcel Holtmann 		 * Policies, enable the correspondig event.
8164b71bba4SMarcel Holtmann 		 */
8174b71bba4SMarcel Holtmann 		if (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)
8184b71bba4SMarcel Holtmann 			events[1] |= 0x04;	/* LE Direct Advertising
8194b71bba4SMarcel Holtmann 						 * Report
8204b71bba4SMarcel Holtmann 						 */
8214b71bba4SMarcel Holtmann 
8225a34bd5fSMarcel Holtmann 		/* If the controller supports the LE Read Local P-256
8235a34bd5fSMarcel Holtmann 		 * Public Key command, enable the corresponding event.
8245a34bd5fSMarcel Holtmann 		 */
8255a34bd5fSMarcel Holtmann 		if (hdev->commands[34] & 0x02)
8265a34bd5fSMarcel Holtmann 			events[0] |= 0x80;	/* LE Read Local P-256
8275a34bd5fSMarcel Holtmann 						 * Public Key Complete
8285a34bd5fSMarcel Holtmann 						 */
8295a34bd5fSMarcel Holtmann 
8305a34bd5fSMarcel Holtmann 		/* If the controller supports the LE Generate DHKey
8315a34bd5fSMarcel Holtmann 		 * command, enable the corresponding event.
8325a34bd5fSMarcel Holtmann 		 */
8335a34bd5fSMarcel Holtmann 		if (hdev->commands[34] & 0x04)
8345a34bd5fSMarcel Holtmann 			events[1] |= 0x01;	/* LE Generate DHKey Complete */
8355a34bd5fSMarcel Holtmann 
8369193c6e8SAndre Guedes 		hci_req_add(req, HCI_OP_LE_SET_EVENT_MASK, sizeof(events),
8379193c6e8SAndre Guedes 			    events);
8389193c6e8SAndre Guedes 
83915a49ccaSMarcel Holtmann 		if (hdev->commands[25] & 0x40) {
84015a49ccaSMarcel Holtmann 			/* Read LE Advertising Channel TX Power */
84115a49ccaSMarcel Holtmann 			hci_req_add(req, HCI_OP_LE_READ_ADV_TX_POWER, 0, NULL);
84215a49ccaSMarcel Holtmann 		}
84315a49ccaSMarcel Holtmann 
844a9f6068eSMarcel Holtmann 		if (hdev->le_features[0] & HCI_LE_DATA_LEN_EXT) {
845a9f6068eSMarcel Holtmann 			/* Read LE Maximum Data Length */
846a9f6068eSMarcel Holtmann 			hci_req_add(req, HCI_OP_LE_READ_MAX_DATA_LEN, 0, NULL);
847a9f6068eSMarcel Holtmann 
848a9f6068eSMarcel Holtmann 			/* Read LE Suggested Default Data Length */
849a9f6068eSMarcel Holtmann 			hci_req_add(req, HCI_OP_LE_READ_DEF_DATA_LEN, 0, NULL);
850a9f6068eSMarcel Holtmann 		}
851a9f6068eSMarcel Holtmann 
85242c6b129SJohan Hedberg 		hci_set_le_support(req);
8539193c6e8SAndre Guedes 	}
854d2c5d77fSJohan Hedberg 
855d2c5d77fSJohan Hedberg 	/* Read features beyond page 1 if available */
856d2c5d77fSJohan Hedberg 	for (p = 2; p < HCI_MAX_PAGES && p <= hdev->max_page; p++) {
857d2c5d77fSJohan Hedberg 		struct hci_cp_read_local_ext_features cp;
858d2c5d77fSJohan Hedberg 
859d2c5d77fSJohan Hedberg 		cp.page = p;
860d2c5d77fSJohan Hedberg 		hci_req_add(req, HCI_OP_READ_LOCAL_EXT_FEATURES,
861d2c5d77fSJohan Hedberg 			    sizeof(cp), &cp);
862d2c5d77fSJohan Hedberg 	}
8632177bab5SJohan Hedberg }
8642177bab5SJohan Hedberg 
8655d4e7e8dSJohan Hedberg static void hci_init4_req(struct hci_request *req, unsigned long opt)
8665d4e7e8dSJohan Hedberg {
8675d4e7e8dSJohan Hedberg 	struct hci_dev *hdev = req->hdev;
8685d4e7e8dSJohan Hedberg 
869d62e6d67SJohan Hedberg 	/* Set event mask page 2 if the HCI command for it is supported */
870d62e6d67SJohan Hedberg 	if (hdev->commands[22] & 0x04)
871d62e6d67SJohan Hedberg 		hci_set_event_mask_page_2(req);
872d62e6d67SJohan Hedberg 
873109e3191SMarcel Holtmann 	/* Read local codec list if the HCI command is supported */
874109e3191SMarcel Holtmann 	if (hdev->commands[29] & 0x20)
875109e3191SMarcel Holtmann 		hci_req_add(req, HCI_OP_READ_LOCAL_CODECS, 0, NULL);
876109e3191SMarcel Holtmann 
877f4fe73edSMarcel Holtmann 	/* Get MWS transport configuration if the HCI command is supported */
878f4fe73edSMarcel Holtmann 	if (hdev->commands[30] & 0x08)
879f4fe73edSMarcel Holtmann 		hci_req_add(req, HCI_OP_GET_MWS_TRANSPORT_CONFIG, 0, NULL);
880f4fe73edSMarcel Holtmann 
8815d4e7e8dSJohan Hedberg 	/* Check for Synchronization Train support */
88253b834d2SMarcel Holtmann 	if (lmp_sync_train_capable(hdev))
8835d4e7e8dSJohan Hedberg 		hci_req_add(req, HCI_OP_READ_SYNC_TRAIN_PARAMS, 0, NULL);
884a6d0d690SMarcel Holtmann 
885a6d0d690SMarcel Holtmann 	/* Enable Secure Connections if supported and configured */
886710f11c0SJohan Hedberg 	if (bredr_sc_enabled(hdev)) {
887a6d0d690SMarcel Holtmann 		u8 support = 0x01;
888a6d0d690SMarcel Holtmann 		hci_req_add(req, HCI_OP_WRITE_SC_SUPPORT,
889a6d0d690SMarcel Holtmann 			    sizeof(support), &support);
890a6d0d690SMarcel Holtmann 	}
8915d4e7e8dSJohan Hedberg }
8925d4e7e8dSJohan Hedberg 
8932177bab5SJohan Hedberg static int __hci_init(struct hci_dev *hdev)
8942177bab5SJohan Hedberg {
8952177bab5SJohan Hedberg 	int err;
8962177bab5SJohan Hedberg 
8972177bab5SJohan Hedberg 	err = __hci_req_sync(hdev, hci_init1_req, 0, HCI_INIT_TIMEOUT);
8982177bab5SJohan Hedberg 	if (err < 0)
8992177bab5SJohan Hedberg 		return err;
9002177bab5SJohan Hedberg 
9014b4148e9SMarcel Holtmann 	/* The Device Under Test (DUT) mode is special and available for
9024b4148e9SMarcel Holtmann 	 * all controller types. So just create it early on.
9034b4148e9SMarcel Holtmann 	 */
9044b4148e9SMarcel Holtmann 	if (test_bit(HCI_SETUP, &hdev->dev_flags)) {
9054b4148e9SMarcel Holtmann 		debugfs_create_file("dut_mode", 0644, hdev->debugfs, hdev,
9064b4148e9SMarcel Holtmann 				    &dut_mode_fops);
9074b4148e9SMarcel Holtmann 	}
9084b4148e9SMarcel Holtmann 
9092177bab5SJohan Hedberg 	/* HCI_BREDR covers both single-mode LE, BR/EDR and dual-mode
9102177bab5SJohan Hedberg 	 * BR/EDR/LE type controllers. AMP controllers only need the
9112177bab5SJohan Hedberg 	 * first stage init.
9122177bab5SJohan Hedberg 	 */
9132177bab5SJohan Hedberg 	if (hdev->dev_type != HCI_BREDR)
9142177bab5SJohan Hedberg 		return 0;
9152177bab5SJohan Hedberg 
9162177bab5SJohan Hedberg 	err = __hci_req_sync(hdev, hci_init2_req, 0, HCI_INIT_TIMEOUT);
9172177bab5SJohan Hedberg 	if (err < 0)
9182177bab5SJohan Hedberg 		return err;
9192177bab5SJohan Hedberg 
9205d4e7e8dSJohan Hedberg 	err = __hci_req_sync(hdev, hci_init3_req, 0, HCI_INIT_TIMEOUT);
9215d4e7e8dSJohan Hedberg 	if (err < 0)
9225d4e7e8dSJohan Hedberg 		return err;
9235d4e7e8dSJohan Hedberg 
924baf27f6eSMarcel Holtmann 	err = __hci_req_sync(hdev, hci_init4_req, 0, HCI_INIT_TIMEOUT);
925baf27f6eSMarcel Holtmann 	if (err < 0)
926baf27f6eSMarcel Holtmann 		return err;
927baf27f6eSMarcel Holtmann 
928baf27f6eSMarcel Holtmann 	/* Only create debugfs entries during the initial setup
929baf27f6eSMarcel Holtmann 	 * phase and not every time the controller gets powered on.
930baf27f6eSMarcel Holtmann 	 */
931baf27f6eSMarcel Holtmann 	if (!test_bit(HCI_SETUP, &hdev->dev_flags))
932baf27f6eSMarcel Holtmann 		return 0;
933baf27f6eSMarcel Holtmann 
93460c5f5fbSMarcel Holtmann 	hci_debugfs_create_common(hdev);
93560c5f5fbSMarcel Holtmann 
93671c3b60eSMarcel Holtmann 	if (lmp_bredr_capable(hdev))
93760c5f5fbSMarcel Holtmann 		hci_debugfs_create_bredr(hdev);
9382bfa3531SMarcel Holtmann 
939d0f729b8SMarcel Holtmann 	if (lmp_le_capable(hdev)) {
94060c5f5fbSMarcel Holtmann 		hci_debugfs_create_le(hdev);
941711eafe3SJohan Hedberg 		smp_register(hdev);
942d0f729b8SMarcel Holtmann 	}
943e7b8fc92SMarcel Holtmann 
944baf27f6eSMarcel Holtmann 	return 0;
9452177bab5SJohan Hedberg }
9462177bab5SJohan Hedberg 
9470ebca7d6SMarcel Holtmann static void hci_init0_req(struct hci_request *req, unsigned long opt)
9480ebca7d6SMarcel Holtmann {
9490ebca7d6SMarcel Holtmann 	struct hci_dev *hdev = req->hdev;
9500ebca7d6SMarcel Holtmann 
9510ebca7d6SMarcel Holtmann 	BT_DBG("%s %ld", hdev->name, opt);
9520ebca7d6SMarcel Holtmann 
9530ebca7d6SMarcel Holtmann 	/* Reset */
9540ebca7d6SMarcel Holtmann 	if (!test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks))
9550ebca7d6SMarcel Holtmann 		hci_reset_req(req, 0);
9560ebca7d6SMarcel Holtmann 
9570ebca7d6SMarcel Holtmann 	/* Read Local Version */
9580ebca7d6SMarcel Holtmann 	hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL);
9590ebca7d6SMarcel Holtmann 
9600ebca7d6SMarcel Holtmann 	/* Read BD Address */
9610ebca7d6SMarcel Holtmann 	if (hdev->set_bdaddr)
9620ebca7d6SMarcel Holtmann 		hci_req_add(req, HCI_OP_READ_BD_ADDR, 0, NULL);
9630ebca7d6SMarcel Holtmann }
9640ebca7d6SMarcel Holtmann 
9650ebca7d6SMarcel Holtmann static int __hci_unconf_init(struct hci_dev *hdev)
9660ebca7d6SMarcel Holtmann {
9670ebca7d6SMarcel Holtmann 	int err;
9680ebca7d6SMarcel Holtmann 
969cc78b44bSMarcel Holtmann 	if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
970cc78b44bSMarcel Holtmann 		return 0;
971cc78b44bSMarcel Holtmann 
9720ebca7d6SMarcel Holtmann 	err = __hci_req_sync(hdev, hci_init0_req, 0, HCI_INIT_TIMEOUT);
9730ebca7d6SMarcel Holtmann 	if (err < 0)
9740ebca7d6SMarcel Holtmann 		return err;
9750ebca7d6SMarcel Holtmann 
9760ebca7d6SMarcel Holtmann 	return 0;
9770ebca7d6SMarcel Holtmann }
9780ebca7d6SMarcel Holtmann 
97942c6b129SJohan Hedberg static void hci_scan_req(struct hci_request *req, unsigned long opt)
9801da177e4SLinus Torvalds {
9811da177e4SLinus Torvalds 	__u8 scan = opt;
9821da177e4SLinus Torvalds 
98342c6b129SJohan Hedberg 	BT_DBG("%s %x", req->hdev->name, scan);
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds 	/* Inquiry and Page scans */
98642c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
9871da177e4SLinus Torvalds }
9881da177e4SLinus Torvalds 
98942c6b129SJohan Hedberg static void hci_auth_req(struct hci_request *req, unsigned long opt)
9901da177e4SLinus Torvalds {
9911da177e4SLinus Torvalds 	__u8 auth = opt;
9921da177e4SLinus Torvalds 
99342c6b129SJohan Hedberg 	BT_DBG("%s %x", req->hdev->name, auth);
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	/* Authentication */
99642c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE, 1, &auth);
9971da177e4SLinus Torvalds }
9981da177e4SLinus Torvalds 
99942c6b129SJohan Hedberg static void hci_encrypt_req(struct hci_request *req, unsigned long opt)
10001da177e4SLinus Torvalds {
10011da177e4SLinus Torvalds 	__u8 encrypt = opt;
10021da177e4SLinus Torvalds 
100342c6b129SJohan Hedberg 	BT_DBG("%s %x", req->hdev->name, encrypt);
10041da177e4SLinus Torvalds 
1005e4e8e37cSMarcel Holtmann 	/* Encryption */
100642c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_WRITE_ENCRYPT_MODE, 1, &encrypt);
10071da177e4SLinus Torvalds }
10081da177e4SLinus Torvalds 
100942c6b129SJohan Hedberg static void hci_linkpol_req(struct hci_request *req, unsigned long opt)
1010e4e8e37cSMarcel Holtmann {
1011e4e8e37cSMarcel Holtmann 	__le16 policy = cpu_to_le16(opt);
1012e4e8e37cSMarcel Holtmann 
101342c6b129SJohan Hedberg 	BT_DBG("%s %x", req->hdev->name, policy);
1014e4e8e37cSMarcel Holtmann 
1015e4e8e37cSMarcel Holtmann 	/* Default link policy */
101642c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_WRITE_DEF_LINK_POLICY, 2, &policy);
1017e4e8e37cSMarcel Holtmann }
1018e4e8e37cSMarcel Holtmann 
10191da177e4SLinus Torvalds /* Get HCI device by index.
10201da177e4SLinus Torvalds  * Device is held on return. */
10211da177e4SLinus Torvalds struct hci_dev *hci_dev_get(int index)
10221da177e4SLinus Torvalds {
10238035ded4SLuiz Augusto von Dentz 	struct hci_dev *hdev = NULL, *d;
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds 	BT_DBG("%d", index);
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 	if (index < 0)
10281da177e4SLinus Torvalds 		return NULL;
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds 	read_lock(&hci_dev_list_lock);
10318035ded4SLuiz Augusto von Dentz 	list_for_each_entry(d, &hci_dev_list, list) {
10321da177e4SLinus Torvalds 		if (d->id == index) {
10331da177e4SLinus Torvalds 			hdev = hci_dev_hold(d);
10341da177e4SLinus Torvalds 			break;
10351da177e4SLinus Torvalds 		}
10361da177e4SLinus Torvalds 	}
10371da177e4SLinus Torvalds 	read_unlock(&hci_dev_list_lock);
10381da177e4SLinus Torvalds 	return hdev;
10391da177e4SLinus Torvalds }
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds /* ---- Inquiry support ---- */
1042ff9ef578SJohan Hedberg 
104330dc78e1SJohan Hedberg bool hci_discovery_active(struct hci_dev *hdev)
104430dc78e1SJohan Hedberg {
104530dc78e1SJohan Hedberg 	struct discovery_state *discov = &hdev->discovery;
104630dc78e1SJohan Hedberg 
10476fbe195dSAndre Guedes 	switch (discov->state) {
1048343f935bSAndre Guedes 	case DISCOVERY_FINDING:
10496fbe195dSAndre Guedes 	case DISCOVERY_RESOLVING:
105030dc78e1SJohan Hedberg 		return true;
105130dc78e1SJohan Hedberg 
10526fbe195dSAndre Guedes 	default:
105330dc78e1SJohan Hedberg 		return false;
105430dc78e1SJohan Hedberg 	}
10556fbe195dSAndre Guedes }
105630dc78e1SJohan Hedberg 
1057ff9ef578SJohan Hedberg void hci_discovery_set_state(struct hci_dev *hdev, int state)
1058ff9ef578SJohan Hedberg {
1059bb3e0a33SJohan Hedberg 	int old_state = hdev->discovery.state;
1060bb3e0a33SJohan Hedberg 
1061ff9ef578SJohan Hedberg 	BT_DBG("%s state %u -> %u", hdev->name, hdev->discovery.state, state);
1062ff9ef578SJohan Hedberg 
1063bb3e0a33SJohan Hedberg 	if (old_state == state)
1064ff9ef578SJohan Hedberg 		return;
1065ff9ef578SJohan Hedberg 
1066bb3e0a33SJohan Hedberg 	hdev->discovery.state = state;
1067bb3e0a33SJohan Hedberg 
1068ff9ef578SJohan Hedberg 	switch (state) {
1069ff9ef578SJohan Hedberg 	case DISCOVERY_STOPPED:
1070c54c3860SAndre Guedes 		hci_update_background_scan(hdev);
1071c54c3860SAndre Guedes 
1072bb3e0a33SJohan Hedberg 		if (old_state != DISCOVERY_STARTING)
1073ff9ef578SJohan Hedberg 			mgmt_discovering(hdev, 0);
1074ff9ef578SJohan Hedberg 		break;
1075ff9ef578SJohan Hedberg 	case DISCOVERY_STARTING:
1076ff9ef578SJohan Hedberg 		break;
1077343f935bSAndre Guedes 	case DISCOVERY_FINDING:
1078ff9ef578SJohan Hedberg 		mgmt_discovering(hdev, 1);
1079ff9ef578SJohan Hedberg 		break;
108030dc78e1SJohan Hedberg 	case DISCOVERY_RESOLVING:
108130dc78e1SJohan Hedberg 		break;
1082ff9ef578SJohan Hedberg 	case DISCOVERY_STOPPING:
1083ff9ef578SJohan Hedberg 		break;
1084ff9ef578SJohan Hedberg 	}
1085ff9ef578SJohan Hedberg }
1086ff9ef578SJohan Hedberg 
10871f9b9a5dSAndre Guedes void hci_inquiry_cache_flush(struct hci_dev *hdev)
10881da177e4SLinus Torvalds {
108930883512SJohan Hedberg 	struct discovery_state *cache = &hdev->discovery;
1090b57c1a56SJohan Hedberg 	struct inquiry_entry *p, *n;
10911da177e4SLinus Torvalds 
1092561aafbcSJohan Hedberg 	list_for_each_entry_safe(p, n, &cache->all, all) {
1093561aafbcSJohan Hedberg 		list_del(&p->all);
1094b57c1a56SJohan Hedberg 		kfree(p);
10951da177e4SLinus Torvalds 	}
1096561aafbcSJohan Hedberg 
1097561aafbcSJohan Hedberg 	INIT_LIST_HEAD(&cache->unknown);
1098561aafbcSJohan Hedberg 	INIT_LIST_HEAD(&cache->resolve);
10991da177e4SLinus Torvalds }
11001da177e4SLinus Torvalds 
1101a8c5fb1aSGustavo Padovan struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev,
1102a8c5fb1aSGustavo Padovan 					       bdaddr_t *bdaddr)
11031da177e4SLinus Torvalds {
110430883512SJohan Hedberg 	struct discovery_state *cache = &hdev->discovery;
11051da177e4SLinus Torvalds 	struct inquiry_entry *e;
11061da177e4SLinus Torvalds 
11076ed93dc6SAndrei Emeltchenko 	BT_DBG("cache %p, %pMR", cache, bdaddr);
11081da177e4SLinus Torvalds 
1109561aafbcSJohan Hedberg 	list_for_each_entry(e, &cache->all, all) {
11101da177e4SLinus Torvalds 		if (!bacmp(&e->data.bdaddr, bdaddr))
11111da177e4SLinus Torvalds 			return e;
11121da177e4SLinus Torvalds 	}
11131da177e4SLinus Torvalds 
1114b57c1a56SJohan Hedberg 	return NULL;
1115b57c1a56SJohan Hedberg }
1116b57c1a56SJohan Hedberg 
1117561aafbcSJohan Hedberg struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
1118561aafbcSJohan Hedberg 						       bdaddr_t *bdaddr)
1119561aafbcSJohan Hedberg {
112030883512SJohan Hedberg 	struct discovery_state *cache = &hdev->discovery;
1121561aafbcSJohan Hedberg 	struct inquiry_entry *e;
1122561aafbcSJohan Hedberg 
11236ed93dc6SAndrei Emeltchenko 	BT_DBG("cache %p, %pMR", cache, bdaddr);
1124561aafbcSJohan Hedberg 
1125561aafbcSJohan Hedberg 	list_for_each_entry(e, &cache->unknown, list) {
1126561aafbcSJohan Hedberg 		if (!bacmp(&e->data.bdaddr, bdaddr))
1127561aafbcSJohan Hedberg 			return e;
1128561aafbcSJohan Hedberg 	}
1129561aafbcSJohan Hedberg 
1130561aafbcSJohan Hedberg 	return NULL;
1131561aafbcSJohan Hedberg }
1132561aafbcSJohan Hedberg 
113330dc78e1SJohan Hedberg struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev,
113430dc78e1SJohan Hedberg 						       bdaddr_t *bdaddr,
113530dc78e1SJohan Hedberg 						       int state)
113630dc78e1SJohan Hedberg {
113730dc78e1SJohan Hedberg 	struct discovery_state *cache = &hdev->discovery;
113830dc78e1SJohan Hedberg 	struct inquiry_entry *e;
113930dc78e1SJohan Hedberg 
11406ed93dc6SAndrei Emeltchenko 	BT_DBG("cache %p bdaddr %pMR state %d", cache, bdaddr, state);
114130dc78e1SJohan Hedberg 
114230dc78e1SJohan Hedberg 	list_for_each_entry(e, &cache->resolve, list) {
114330dc78e1SJohan Hedberg 		if (!bacmp(bdaddr, BDADDR_ANY) && e->name_state == state)
114430dc78e1SJohan Hedberg 			return e;
114530dc78e1SJohan Hedberg 		if (!bacmp(&e->data.bdaddr, bdaddr))
114630dc78e1SJohan Hedberg 			return e;
114730dc78e1SJohan Hedberg 	}
114830dc78e1SJohan Hedberg 
114930dc78e1SJohan Hedberg 	return NULL;
115030dc78e1SJohan Hedberg }
115130dc78e1SJohan Hedberg 
1152a3d4e20aSJohan Hedberg void hci_inquiry_cache_update_resolve(struct hci_dev *hdev,
1153a3d4e20aSJohan Hedberg 				      struct inquiry_entry *ie)
1154a3d4e20aSJohan Hedberg {
1155a3d4e20aSJohan Hedberg 	struct discovery_state *cache = &hdev->discovery;
1156a3d4e20aSJohan Hedberg 	struct list_head *pos = &cache->resolve;
1157a3d4e20aSJohan Hedberg 	struct inquiry_entry *p;
1158a3d4e20aSJohan Hedberg 
1159a3d4e20aSJohan Hedberg 	list_del(&ie->list);
1160a3d4e20aSJohan Hedberg 
1161a3d4e20aSJohan Hedberg 	list_for_each_entry(p, &cache->resolve, list) {
1162a3d4e20aSJohan Hedberg 		if (p->name_state != NAME_PENDING &&
1163a3d4e20aSJohan Hedberg 		    abs(p->data.rssi) >= abs(ie->data.rssi))
1164a3d4e20aSJohan Hedberg 			break;
1165a3d4e20aSJohan Hedberg 		pos = &p->list;
1166a3d4e20aSJohan Hedberg 	}
1167a3d4e20aSJohan Hedberg 
1168a3d4e20aSJohan Hedberg 	list_add(&ie->list, pos);
1169a3d4e20aSJohan Hedberg }
1170a3d4e20aSJohan Hedberg 
1171af58925cSMarcel Holtmann u32 hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
1172af58925cSMarcel Holtmann 			     bool name_known)
11731da177e4SLinus Torvalds {
117430883512SJohan Hedberg 	struct discovery_state *cache = &hdev->discovery;
117570f23020SAndrei Emeltchenko 	struct inquiry_entry *ie;
1176af58925cSMarcel Holtmann 	u32 flags = 0;
11771da177e4SLinus Torvalds 
11786ed93dc6SAndrei Emeltchenko 	BT_DBG("cache %p, %pMR", cache, &data->bdaddr);
11791da177e4SLinus Torvalds 
11806928a924SJohan Hedberg 	hci_remove_remote_oob_data(hdev, &data->bdaddr, BDADDR_BREDR);
11812b2fec4dSSzymon Janc 
1182af58925cSMarcel Holtmann 	if (!data->ssp_mode)
1183af58925cSMarcel Holtmann 		flags |= MGMT_DEV_FOUND_LEGACY_PAIRING;
1184388fc8faSJohan Hedberg 
118570f23020SAndrei Emeltchenko 	ie = hci_inquiry_cache_lookup(hdev, &data->bdaddr);
1186a3d4e20aSJohan Hedberg 	if (ie) {
1187af58925cSMarcel Holtmann 		if (!ie->data.ssp_mode)
1188af58925cSMarcel Holtmann 			flags |= MGMT_DEV_FOUND_LEGACY_PAIRING;
1189388fc8faSJohan Hedberg 
1190a3d4e20aSJohan Hedberg 		if (ie->name_state == NAME_NEEDED &&
1191a3d4e20aSJohan Hedberg 		    data->rssi != ie->data.rssi) {
1192a3d4e20aSJohan Hedberg 			ie->data.rssi = data->rssi;
1193a3d4e20aSJohan Hedberg 			hci_inquiry_cache_update_resolve(hdev, ie);
1194a3d4e20aSJohan Hedberg 		}
1195a3d4e20aSJohan Hedberg 
1196561aafbcSJohan Hedberg 		goto update;
1197a3d4e20aSJohan Hedberg 	}
1198561aafbcSJohan Hedberg 
11991da177e4SLinus Torvalds 	/* Entry not in the cache. Add new one. */
120027f70f3eSJohan Hedberg 	ie = kzalloc(sizeof(*ie), GFP_KERNEL);
1201af58925cSMarcel Holtmann 	if (!ie) {
1202af58925cSMarcel Holtmann 		flags |= MGMT_DEV_FOUND_CONFIRM_NAME;
1203af58925cSMarcel Holtmann 		goto done;
1204af58925cSMarcel Holtmann 	}
120570f23020SAndrei Emeltchenko 
1206561aafbcSJohan Hedberg 	list_add(&ie->all, &cache->all);
1207561aafbcSJohan Hedberg 
1208561aafbcSJohan Hedberg 	if (name_known) {
1209561aafbcSJohan Hedberg 		ie->name_state = NAME_KNOWN;
1210561aafbcSJohan Hedberg 	} else {
1211561aafbcSJohan Hedberg 		ie->name_state = NAME_NOT_KNOWN;
1212561aafbcSJohan Hedberg 		list_add(&ie->list, &cache->unknown);
1213561aafbcSJohan Hedberg 	}
1214561aafbcSJohan Hedberg 
1215561aafbcSJohan Hedberg update:
1216561aafbcSJohan Hedberg 	if (name_known && ie->name_state != NAME_KNOWN &&
1217561aafbcSJohan Hedberg 	    ie->name_state != NAME_PENDING) {
1218561aafbcSJohan Hedberg 		ie->name_state = NAME_KNOWN;
1219561aafbcSJohan Hedberg 		list_del(&ie->list);
12201da177e4SLinus Torvalds 	}
12211da177e4SLinus Torvalds 
122270f23020SAndrei Emeltchenko 	memcpy(&ie->data, data, sizeof(*data));
122370f23020SAndrei Emeltchenko 	ie->timestamp = jiffies;
12241da177e4SLinus Torvalds 	cache->timestamp = jiffies;
12253175405bSJohan Hedberg 
12263175405bSJohan Hedberg 	if (ie->name_state == NAME_NOT_KNOWN)
1227af58925cSMarcel Holtmann 		flags |= MGMT_DEV_FOUND_CONFIRM_NAME;
12283175405bSJohan Hedberg 
1229af58925cSMarcel Holtmann done:
1230af58925cSMarcel Holtmann 	return flags;
12311da177e4SLinus Torvalds }
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf)
12341da177e4SLinus Torvalds {
123530883512SJohan Hedberg 	struct discovery_state *cache = &hdev->discovery;
12361da177e4SLinus Torvalds 	struct inquiry_info *info = (struct inquiry_info *) buf;
12371da177e4SLinus Torvalds 	struct inquiry_entry *e;
12381da177e4SLinus Torvalds 	int copied = 0;
12391da177e4SLinus Torvalds 
1240561aafbcSJohan Hedberg 	list_for_each_entry(e, &cache->all, all) {
12411da177e4SLinus Torvalds 		struct inquiry_data *data = &e->data;
1242b57c1a56SJohan Hedberg 
1243b57c1a56SJohan Hedberg 		if (copied >= num)
1244b57c1a56SJohan Hedberg 			break;
1245b57c1a56SJohan Hedberg 
12461da177e4SLinus Torvalds 		bacpy(&info->bdaddr, &data->bdaddr);
12471da177e4SLinus Torvalds 		info->pscan_rep_mode	= data->pscan_rep_mode;
12481da177e4SLinus Torvalds 		info->pscan_period_mode	= data->pscan_period_mode;
12491da177e4SLinus Torvalds 		info->pscan_mode	= data->pscan_mode;
12501da177e4SLinus Torvalds 		memcpy(info->dev_class, data->dev_class, 3);
12511da177e4SLinus Torvalds 		info->clock_offset	= data->clock_offset;
1252b57c1a56SJohan Hedberg 
12531da177e4SLinus Torvalds 		info++;
1254b57c1a56SJohan Hedberg 		copied++;
12551da177e4SLinus Torvalds 	}
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds 	BT_DBG("cache %p, copied %d", cache, copied);
12581da177e4SLinus Torvalds 	return copied;
12591da177e4SLinus Torvalds }
12601da177e4SLinus Torvalds 
126142c6b129SJohan Hedberg static void hci_inq_req(struct hci_request *req, unsigned long opt)
12621da177e4SLinus Torvalds {
12631da177e4SLinus Torvalds 	struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt;
126442c6b129SJohan Hedberg 	struct hci_dev *hdev = req->hdev;
12651da177e4SLinus Torvalds 	struct hci_cp_inquiry cp;
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 	BT_DBG("%s", hdev->name);
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds 	if (test_bit(HCI_INQUIRY, &hdev->flags))
12701da177e4SLinus Torvalds 		return;
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 	/* Start Inquiry */
12731da177e4SLinus Torvalds 	memcpy(&cp.lap, &ir->lap, 3);
12741da177e4SLinus Torvalds 	cp.length  = ir->length;
12751da177e4SLinus Torvalds 	cp.num_rsp = ir->num_rsp;
127642c6b129SJohan Hedberg 	hci_req_add(req, HCI_OP_INQUIRY, sizeof(cp), &cp);
12771da177e4SLinus Torvalds }
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds int hci_inquiry(void __user *arg)
12801da177e4SLinus Torvalds {
12811da177e4SLinus Torvalds 	__u8 __user *ptr = arg;
12821da177e4SLinus Torvalds 	struct hci_inquiry_req ir;
12831da177e4SLinus Torvalds 	struct hci_dev *hdev;
12841da177e4SLinus Torvalds 	int err = 0, do_inquiry = 0, max_rsp;
12851da177e4SLinus Torvalds 	long timeo;
12861da177e4SLinus Torvalds 	__u8 *buf;
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds 	if (copy_from_user(&ir, ptr, sizeof(ir)))
12891da177e4SLinus Torvalds 		return -EFAULT;
12901da177e4SLinus Torvalds 
12915a08ecceSAndrei Emeltchenko 	hdev = hci_dev_get(ir.dev_id);
12925a08ecceSAndrei Emeltchenko 	if (!hdev)
12931da177e4SLinus Torvalds 		return -ENODEV;
12941da177e4SLinus Torvalds 
12950736cfa8SMarcel Holtmann 	if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
12960736cfa8SMarcel Holtmann 		err = -EBUSY;
12970736cfa8SMarcel Holtmann 		goto done;
12980736cfa8SMarcel Holtmann 	}
12990736cfa8SMarcel Holtmann 
13004a964404SMarcel Holtmann 	if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) {
1301fee746b0SMarcel Holtmann 		err = -EOPNOTSUPP;
1302fee746b0SMarcel Holtmann 		goto done;
1303fee746b0SMarcel Holtmann 	}
1304fee746b0SMarcel Holtmann 
13055b69bef5SMarcel Holtmann 	if (hdev->dev_type != HCI_BREDR) {
13065b69bef5SMarcel Holtmann 		err = -EOPNOTSUPP;
13075b69bef5SMarcel Holtmann 		goto done;
13085b69bef5SMarcel Holtmann 	}
13095b69bef5SMarcel Holtmann 
131056f87901SJohan Hedberg 	if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
131156f87901SJohan Hedberg 		err = -EOPNOTSUPP;
131256f87901SJohan Hedberg 		goto done;
131356f87901SJohan Hedberg 	}
131456f87901SJohan Hedberg 
131509fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
13161da177e4SLinus Torvalds 	if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX ||
1317a8c5fb1aSGustavo Padovan 	    inquiry_cache_empty(hdev) || ir.flags & IREQ_CACHE_FLUSH) {
13181f9b9a5dSAndre Guedes 		hci_inquiry_cache_flush(hdev);
13191da177e4SLinus Torvalds 		do_inquiry = 1;
13201da177e4SLinus Torvalds 	}
132109fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
13221da177e4SLinus Torvalds 
132304837f64SMarcel Holtmann 	timeo = ir.length * msecs_to_jiffies(2000);
132470f23020SAndrei Emeltchenko 
132570f23020SAndrei Emeltchenko 	if (do_inquiry) {
132601178cd4SJohan Hedberg 		err = hci_req_sync(hdev, hci_inq_req, (unsigned long) &ir,
132701178cd4SJohan Hedberg 				   timeo);
132870f23020SAndrei Emeltchenko 		if (err < 0)
13291da177e4SLinus Torvalds 			goto done;
13303e13fa1eSAndre Guedes 
13313e13fa1eSAndre Guedes 		/* Wait until Inquiry procedure finishes (HCI_INQUIRY flag is
13323e13fa1eSAndre Guedes 		 * cleared). If it is interrupted by a signal, return -EINTR.
13333e13fa1eSAndre Guedes 		 */
133474316201SNeilBrown 		if (wait_on_bit(&hdev->flags, HCI_INQUIRY,
13353e13fa1eSAndre Guedes 				TASK_INTERRUPTIBLE))
13363e13fa1eSAndre Guedes 			return -EINTR;
133770f23020SAndrei Emeltchenko 	}
13381da177e4SLinus Torvalds 
13398fc9ced3SGustavo Padovan 	/* for unlimited number of responses we will use buffer with
13408fc9ced3SGustavo Padovan 	 * 255 entries
13418fc9ced3SGustavo Padovan 	 */
13421da177e4SLinus Torvalds 	max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp;
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds 	/* cache_dump can't sleep. Therefore we allocate temp buffer and then
13451da177e4SLinus Torvalds 	 * copy it to the user space.
13461da177e4SLinus Torvalds 	 */
134770f23020SAndrei Emeltchenko 	buf = kmalloc(sizeof(struct inquiry_info) * max_rsp, GFP_KERNEL);
134870f23020SAndrei Emeltchenko 	if (!buf) {
13491da177e4SLinus Torvalds 		err = -ENOMEM;
13501da177e4SLinus Torvalds 		goto done;
13511da177e4SLinus Torvalds 	}
13521da177e4SLinus Torvalds 
135309fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
13541da177e4SLinus Torvalds 	ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf);
135509fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds 	BT_DBG("num_rsp %d", ir.num_rsp);
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds 	if (!copy_to_user(ptr, &ir, sizeof(ir))) {
13601da177e4SLinus Torvalds 		ptr += sizeof(ir);
13611da177e4SLinus Torvalds 		if (copy_to_user(ptr, buf, sizeof(struct inquiry_info) *
13621da177e4SLinus Torvalds 				 ir.num_rsp))
13631da177e4SLinus Torvalds 			err = -EFAULT;
13641da177e4SLinus Torvalds 	} else
13651da177e4SLinus Torvalds 		err = -EFAULT;
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds 	kfree(buf);
13681da177e4SLinus Torvalds 
13691da177e4SLinus Torvalds done:
13701da177e4SLinus Torvalds 	hci_dev_put(hdev);
13711da177e4SLinus Torvalds 	return err;
13721da177e4SLinus Torvalds }
13731da177e4SLinus Torvalds 
1374cbed0ca1SJohan Hedberg static int hci_dev_do_open(struct hci_dev *hdev)
13751da177e4SLinus Torvalds {
13761da177e4SLinus Torvalds 	int ret = 0;
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds 	BT_DBG("%s %p", hdev->name, hdev);
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 	hci_req_lock(hdev);
13811da177e4SLinus Torvalds 
138294324962SJohan Hovold 	if (test_bit(HCI_UNREGISTER, &hdev->dev_flags)) {
138394324962SJohan Hovold 		ret = -ENODEV;
138494324962SJohan Hovold 		goto done;
138594324962SJohan Hovold 	}
138694324962SJohan Hovold 
1387d603b76bSMarcel Holtmann 	if (!test_bit(HCI_SETUP, &hdev->dev_flags) &&
1388d603b76bSMarcel Holtmann 	    !test_bit(HCI_CONFIG, &hdev->dev_flags)) {
1389a5c8f270SMarcel Holtmann 		/* Check for rfkill but allow the HCI setup stage to
1390a5c8f270SMarcel Holtmann 		 * proceed (which in itself doesn't cause any RF activity).
1391bf543036SJohan Hedberg 		 */
1392a5c8f270SMarcel Holtmann 		if (test_bit(HCI_RFKILLED, &hdev->dev_flags)) {
1393611b30f7SMarcel Holtmann 			ret = -ERFKILL;
1394611b30f7SMarcel Holtmann 			goto done;
1395611b30f7SMarcel Holtmann 		}
1396611b30f7SMarcel Holtmann 
1397a5c8f270SMarcel Holtmann 		/* Check for valid public address or a configured static
1398a5c8f270SMarcel Holtmann 		 * random adddress, but let the HCI setup proceed to
1399a5c8f270SMarcel Holtmann 		 * be able to determine if there is a public address
1400a5c8f270SMarcel Holtmann 		 * or not.
1401a5c8f270SMarcel Holtmann 		 *
1402c6beca0eSMarcel Holtmann 		 * In case of user channel usage, it is not important
1403c6beca0eSMarcel Holtmann 		 * if a public address or static random address is
1404c6beca0eSMarcel Holtmann 		 * available.
1405c6beca0eSMarcel Holtmann 		 *
1406a5c8f270SMarcel Holtmann 		 * This check is only valid for BR/EDR controllers
1407a5c8f270SMarcel Holtmann 		 * since AMP controllers do not have an address.
1408a5c8f270SMarcel Holtmann 		 */
1409c6beca0eSMarcel Holtmann 		if (!test_bit(HCI_USER_CHANNEL, &hdev->dev_flags) &&
1410c6beca0eSMarcel Holtmann 		    hdev->dev_type == HCI_BREDR &&
1411a5c8f270SMarcel Holtmann 		    !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
1412a5c8f270SMarcel Holtmann 		    !bacmp(&hdev->static_addr, BDADDR_ANY)) {
1413a5c8f270SMarcel Holtmann 			ret = -EADDRNOTAVAIL;
1414a5c8f270SMarcel Holtmann 			goto done;
1415a5c8f270SMarcel Holtmann 		}
1416a5c8f270SMarcel Holtmann 	}
1417a5c8f270SMarcel Holtmann 
14181da177e4SLinus Torvalds 	if (test_bit(HCI_UP, &hdev->flags)) {
14191da177e4SLinus Torvalds 		ret = -EALREADY;
14201da177e4SLinus Torvalds 		goto done;
14211da177e4SLinus Torvalds 	}
14221da177e4SLinus Torvalds 
14231da177e4SLinus Torvalds 	if (hdev->open(hdev)) {
14241da177e4SLinus Torvalds 		ret = -EIO;
14251da177e4SLinus Torvalds 		goto done;
14261da177e4SLinus Torvalds 	}
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 	atomic_set(&hdev->cmd_cnt, 1);
14291da177e4SLinus Torvalds 	set_bit(HCI_INIT, &hdev->flags);
1430f41c70c4SMarcel Holtmann 
1431af202f84SMarcel Holtmann 	if (test_bit(HCI_SETUP, &hdev->dev_flags)) {
1432af202f84SMarcel Holtmann 		if (hdev->setup)
1433f41c70c4SMarcel Holtmann 			ret = hdev->setup(hdev);
1434f41c70c4SMarcel Holtmann 
1435af202f84SMarcel Holtmann 		/* The transport driver can set these quirks before
1436af202f84SMarcel Holtmann 		 * creating the HCI device or in its setup callback.
1437af202f84SMarcel Holtmann 		 *
1438af202f84SMarcel Holtmann 		 * In case any of them is set, the controller has to
1439af202f84SMarcel Holtmann 		 * start up as unconfigured.
1440af202f84SMarcel Holtmann 		 */
1441eb1904f4SMarcel Holtmann 		if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
1442eb1904f4SMarcel Holtmann 		    test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks))
144389bc22d2SMarcel Holtmann 			set_bit(HCI_UNCONFIGURED, &hdev->dev_flags);
1444f41c70c4SMarcel Holtmann 
14450ebca7d6SMarcel Holtmann 		/* For an unconfigured controller it is required to
14460ebca7d6SMarcel Holtmann 		 * read at least the version information provided by
14470ebca7d6SMarcel Holtmann 		 * the Read Local Version Information command.
14480ebca7d6SMarcel Holtmann 		 *
14490ebca7d6SMarcel Holtmann 		 * If the set_bdaddr driver callback is provided, then
14500ebca7d6SMarcel Holtmann 		 * also the original Bluetooth public device address
14510ebca7d6SMarcel Holtmann 		 * will be read using the Read BD Address command.
14520ebca7d6SMarcel Holtmann 		 */
14530ebca7d6SMarcel Holtmann 		if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags))
14540ebca7d6SMarcel Holtmann 			ret = __hci_unconf_init(hdev);
145589bc22d2SMarcel Holtmann 	}
145689bc22d2SMarcel Holtmann 
14579713c17bSMarcel Holtmann 	if (test_bit(HCI_CONFIG, &hdev->dev_flags)) {
14589713c17bSMarcel Holtmann 		/* If public address change is configured, ensure that
14599713c17bSMarcel Holtmann 		 * the address gets programmed. If the driver does not
14609713c17bSMarcel Holtmann 		 * support changing the public address, fail the power
14619713c17bSMarcel Holtmann 		 * on procedure.
146224c457e2SMarcel Holtmann 		 */
14639713c17bSMarcel Holtmann 		if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
14649713c17bSMarcel Holtmann 		    hdev->set_bdaddr)
146524c457e2SMarcel Holtmann 			ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
146624c457e2SMarcel Holtmann 		else
146724c457e2SMarcel Holtmann 			ret = -EADDRNOTAVAIL;
146824c457e2SMarcel Holtmann 	}
146924c457e2SMarcel Holtmann 
1470f41c70c4SMarcel Holtmann 	if (!ret) {
14714a964404SMarcel Holtmann 		if (!test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) &&
14720736cfa8SMarcel Holtmann 		    !test_bit(HCI_USER_CHANNEL, &hdev->dev_flags))
14732177bab5SJohan Hedberg 			ret = __hci_init(hdev);
14741da177e4SLinus Torvalds 	}
14751da177e4SLinus Torvalds 
1476f41c70c4SMarcel Holtmann 	clear_bit(HCI_INIT, &hdev->flags);
1477f41c70c4SMarcel Holtmann 
14781da177e4SLinus Torvalds 	if (!ret) {
14791da177e4SLinus Torvalds 		hci_dev_hold(hdev);
1480d6bfd59cSJohan Hedberg 		set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);
14811da177e4SLinus Torvalds 		set_bit(HCI_UP, &hdev->flags);
14821da177e4SLinus Torvalds 		hci_notify(hdev, HCI_DEV_UP);
1483bb4b2a9aSAndrei Emeltchenko 		if (!test_bit(HCI_SETUP, &hdev->dev_flags) &&
1484d603b76bSMarcel Holtmann 		    !test_bit(HCI_CONFIG, &hdev->dev_flags) &&
14854a964404SMarcel Holtmann 		    !test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) &&
14860736cfa8SMarcel Holtmann 		    !test_bit(HCI_USER_CHANNEL, &hdev->dev_flags) &&
14871514b892SMarcel Holtmann 		    hdev->dev_type == HCI_BREDR) {
148809fd0de5SGustavo F. Padovan 			hci_dev_lock(hdev);
1489744cf19eSJohan Hedberg 			mgmt_powered(hdev, 1);
149009fd0de5SGustavo F. Padovan 			hci_dev_unlock(hdev);
149156e5cb86SJohan Hedberg 		}
14921da177e4SLinus Torvalds 	} else {
14931da177e4SLinus Torvalds 		/* Init failed, cleanup */
14943eff45eaSGustavo F. Padovan 		flush_work(&hdev->tx_work);
1495c347b765SGustavo F. Padovan 		flush_work(&hdev->cmd_work);
1496b78752ccSMarcel Holtmann 		flush_work(&hdev->rx_work);
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds 		skb_queue_purge(&hdev->cmd_q);
14991da177e4SLinus Torvalds 		skb_queue_purge(&hdev->rx_q);
15001da177e4SLinus Torvalds 
15011da177e4SLinus Torvalds 		if (hdev->flush)
15021da177e4SLinus Torvalds 			hdev->flush(hdev);
15031da177e4SLinus Torvalds 
15041da177e4SLinus Torvalds 		if (hdev->sent_cmd) {
15051da177e4SLinus Torvalds 			kfree_skb(hdev->sent_cmd);
15061da177e4SLinus Torvalds 			hdev->sent_cmd = NULL;
15071da177e4SLinus Torvalds 		}
15081da177e4SLinus Torvalds 
15091da177e4SLinus Torvalds 		hdev->close(hdev);
1510fee746b0SMarcel Holtmann 		hdev->flags &= BIT(HCI_RAW);
15111da177e4SLinus Torvalds 	}
15121da177e4SLinus Torvalds 
15131da177e4SLinus Torvalds done:
15141da177e4SLinus Torvalds 	hci_req_unlock(hdev);
15151da177e4SLinus Torvalds 	return ret;
15161da177e4SLinus Torvalds }
15171da177e4SLinus Torvalds 
1518cbed0ca1SJohan Hedberg /* ---- HCI ioctl helpers ---- */
1519cbed0ca1SJohan Hedberg 
1520cbed0ca1SJohan Hedberg int hci_dev_open(__u16 dev)
1521cbed0ca1SJohan Hedberg {
1522cbed0ca1SJohan Hedberg 	struct hci_dev *hdev;
1523cbed0ca1SJohan Hedberg 	int err;
1524cbed0ca1SJohan Hedberg 
1525cbed0ca1SJohan Hedberg 	hdev = hci_dev_get(dev);
1526cbed0ca1SJohan Hedberg 	if (!hdev)
1527cbed0ca1SJohan Hedberg 		return -ENODEV;
1528cbed0ca1SJohan Hedberg 
15294a964404SMarcel Holtmann 	/* Devices that are marked as unconfigured can only be powered
1530fee746b0SMarcel Holtmann 	 * up as user channel. Trying to bring them up as normal devices
1531fee746b0SMarcel Holtmann 	 * will result into a failure. Only user channel operation is
1532fee746b0SMarcel Holtmann 	 * possible.
1533fee746b0SMarcel Holtmann 	 *
1534fee746b0SMarcel Holtmann 	 * When this function is called for a user channel, the flag
1535fee746b0SMarcel Holtmann 	 * HCI_USER_CHANNEL will be set first before attempting to
1536fee746b0SMarcel Holtmann 	 * open the device.
1537fee746b0SMarcel Holtmann 	 */
15384a964404SMarcel Holtmann 	if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) &&
1539fee746b0SMarcel Holtmann 	    !test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
1540fee746b0SMarcel Holtmann 		err = -EOPNOTSUPP;
1541fee746b0SMarcel Holtmann 		goto done;
1542fee746b0SMarcel Holtmann 	}
1543fee746b0SMarcel Holtmann 
1544e1d08f40SJohan Hedberg 	/* We need to ensure that no other power on/off work is pending
1545e1d08f40SJohan Hedberg 	 * before proceeding to call hci_dev_do_open. This is
1546e1d08f40SJohan Hedberg 	 * particularly important if the setup procedure has not yet
1547e1d08f40SJohan Hedberg 	 * completed.
1548e1d08f40SJohan Hedberg 	 */
1549e1d08f40SJohan Hedberg 	if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
1550e1d08f40SJohan Hedberg 		cancel_delayed_work(&hdev->power_off);
1551e1d08f40SJohan Hedberg 
1552a5c8f270SMarcel Holtmann 	/* After this call it is guaranteed that the setup procedure
1553a5c8f270SMarcel Holtmann 	 * has finished. This means that error conditions like RFKILL
1554a5c8f270SMarcel Holtmann 	 * or no valid public or static random address apply.
1555a5c8f270SMarcel Holtmann 	 */
1556e1d08f40SJohan Hedberg 	flush_workqueue(hdev->req_workqueue);
1557e1d08f40SJohan Hedberg 
155812aa4f0aSMarcel Holtmann 	/* For controllers not using the management interface and that
1559b6ae8457SJohan Hedberg 	 * are brought up using legacy ioctl, set the HCI_BONDABLE bit
156012aa4f0aSMarcel Holtmann 	 * so that pairing works for them. Once the management interface
156112aa4f0aSMarcel Holtmann 	 * is in use this bit will be cleared again and userspace has
156212aa4f0aSMarcel Holtmann 	 * to explicitly enable it.
156312aa4f0aSMarcel Holtmann 	 */
156412aa4f0aSMarcel Holtmann 	if (!test_bit(HCI_USER_CHANNEL, &hdev->dev_flags) &&
156512aa4f0aSMarcel Holtmann 	    !test_bit(HCI_MGMT, &hdev->dev_flags))
1566b6ae8457SJohan Hedberg 		set_bit(HCI_BONDABLE, &hdev->dev_flags);
156712aa4f0aSMarcel Holtmann 
1568cbed0ca1SJohan Hedberg 	err = hci_dev_do_open(hdev);
1569cbed0ca1SJohan Hedberg 
1570fee746b0SMarcel Holtmann done:
1571cbed0ca1SJohan Hedberg 	hci_dev_put(hdev);
1572cbed0ca1SJohan Hedberg 	return err;
1573cbed0ca1SJohan Hedberg }
1574cbed0ca1SJohan Hedberg 
1575d7347f3cSJohan Hedberg /* This function requires the caller holds hdev->lock */
1576d7347f3cSJohan Hedberg static void hci_pend_le_actions_clear(struct hci_dev *hdev)
1577d7347f3cSJohan Hedberg {
1578d7347f3cSJohan Hedberg 	struct hci_conn_params *p;
1579d7347f3cSJohan Hedberg 
1580f161dd41SJohan Hedberg 	list_for_each_entry(p, &hdev->le_conn_params, list) {
1581f161dd41SJohan Hedberg 		if (p->conn) {
1582f161dd41SJohan Hedberg 			hci_conn_drop(p->conn);
1583f8aaf9b6SJohan Hedberg 			hci_conn_put(p->conn);
1584f161dd41SJohan Hedberg 			p->conn = NULL;
1585f161dd41SJohan Hedberg 		}
1586d7347f3cSJohan Hedberg 		list_del_init(&p->action);
1587f161dd41SJohan Hedberg 	}
1588d7347f3cSJohan Hedberg 
1589d7347f3cSJohan Hedberg 	BT_DBG("All LE pending actions cleared");
1590d7347f3cSJohan Hedberg }
1591d7347f3cSJohan Hedberg 
15921da177e4SLinus Torvalds static int hci_dev_do_close(struct hci_dev *hdev)
15931da177e4SLinus Torvalds {
15941da177e4SLinus Torvalds 	BT_DBG("%s %p", hdev->name, hdev);
15951da177e4SLinus Torvalds 
159678c04c0bSVinicius Costa Gomes 	cancel_delayed_work(&hdev->power_off);
159778c04c0bSVinicius Costa Gomes 
15981da177e4SLinus Torvalds 	hci_req_cancel(hdev, ENODEV);
15991da177e4SLinus Torvalds 	hci_req_lock(hdev);
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds 	if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
160265cc2b49SMarcel Holtmann 		cancel_delayed_work_sync(&hdev->cmd_timer);
16031da177e4SLinus Torvalds 		hci_req_unlock(hdev);
16041da177e4SLinus Torvalds 		return 0;
16051da177e4SLinus Torvalds 	}
16061da177e4SLinus Torvalds 
16073eff45eaSGustavo F. Padovan 	/* Flush RX and TX works */
16083eff45eaSGustavo F. Padovan 	flush_work(&hdev->tx_work);
1609b78752ccSMarcel Holtmann 	flush_work(&hdev->rx_work);
16101da177e4SLinus Torvalds 
161116ab91abSJohan Hedberg 	if (hdev->discov_timeout > 0) {
1612e0f9309fSJohan Hedberg 		cancel_delayed_work(&hdev->discov_off);
161316ab91abSJohan Hedberg 		hdev->discov_timeout = 0;
16145e5282bbSJohan Hedberg 		clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
1615310a3d48SMarcel Holtmann 		clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
161616ab91abSJohan Hedberg 	}
161716ab91abSJohan Hedberg 
1618a8b2d5c2SJohan Hedberg 	if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
16197d78525dSJohan Hedberg 		cancel_delayed_work(&hdev->service_cache);
16207d78525dSJohan Hedberg 
16217ba8b4beSAndre Guedes 	cancel_delayed_work_sync(&hdev->le_scan_disable);
16224518bb0fSJohan Hedberg 
16234518bb0fSJohan Hedberg 	if (test_bit(HCI_MGMT, &hdev->dev_flags))
1624d6bfd59cSJohan Hedberg 		cancel_delayed_work_sync(&hdev->rpa_expired);
16257ba8b4beSAndre Guedes 
162676727c02SJohan Hedberg 	/* Avoid potential lockdep warnings from the *_flush() calls by
162776727c02SJohan Hedberg 	 * ensuring the workqueue is empty up front.
162876727c02SJohan Hedberg 	 */
162976727c02SJohan Hedberg 	drain_workqueue(hdev->workqueue);
163076727c02SJohan Hedberg 
163109fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
16321aeb9c65SJohan Hedberg 
16331aeb9c65SJohan Hedberg 	if (!test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) {
16341aeb9c65SJohan Hedberg 		if (hdev->dev_type == HCI_BREDR)
16351aeb9c65SJohan Hedberg 			mgmt_powered(hdev, 0);
16361aeb9c65SJohan Hedberg 	}
16371aeb9c65SJohan Hedberg 
16381f9b9a5dSAndre Guedes 	hci_inquiry_cache_flush(hdev);
1639d7347f3cSJohan Hedberg 	hci_pend_le_actions_clear(hdev);
1640f161dd41SJohan Hedberg 	hci_conn_hash_flush(hdev);
164109fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
16421da177e4SLinus Torvalds 
16431da177e4SLinus Torvalds 	hci_notify(hdev, HCI_DEV_DOWN);
16441da177e4SLinus Torvalds 
16451da177e4SLinus Torvalds 	if (hdev->flush)
16461da177e4SLinus Torvalds 		hdev->flush(hdev);
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds 	/* Reset device */
16491da177e4SLinus Torvalds 	skb_queue_purge(&hdev->cmd_q);
16501da177e4SLinus Torvalds 	atomic_set(&hdev->cmd_cnt, 1);
16514a964404SMarcel Holtmann 	if (!test_bit(HCI_AUTO_OFF, &hdev->dev_flags) &&
16524a964404SMarcel Holtmann 	    !test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) &&
1653a6c511c6SSzymon Janc 	    test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
16541da177e4SLinus Torvalds 		set_bit(HCI_INIT, &hdev->flags);
165501178cd4SJohan Hedberg 		__hci_req_sync(hdev, hci_reset_req, 0, HCI_CMD_TIMEOUT);
16561da177e4SLinus Torvalds 		clear_bit(HCI_INIT, &hdev->flags);
16571da177e4SLinus Torvalds 	}
16581da177e4SLinus Torvalds 
1659c347b765SGustavo F. Padovan 	/* flush cmd  work */
1660c347b765SGustavo F. Padovan 	flush_work(&hdev->cmd_work);
16611da177e4SLinus Torvalds 
16621da177e4SLinus Torvalds 	/* Drop queues */
16631da177e4SLinus Torvalds 	skb_queue_purge(&hdev->rx_q);
16641da177e4SLinus Torvalds 	skb_queue_purge(&hdev->cmd_q);
16651da177e4SLinus Torvalds 	skb_queue_purge(&hdev->raw_q);
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds 	/* Drop last sent command */
16681da177e4SLinus Torvalds 	if (hdev->sent_cmd) {
166965cc2b49SMarcel Holtmann 		cancel_delayed_work_sync(&hdev->cmd_timer);
16701da177e4SLinus Torvalds 		kfree_skb(hdev->sent_cmd);
16711da177e4SLinus Torvalds 		hdev->sent_cmd = NULL;
16721da177e4SLinus Torvalds 	}
16731da177e4SLinus Torvalds 
1674b6ddb638SJohan Hedberg 	kfree_skb(hdev->recv_evt);
1675b6ddb638SJohan Hedberg 	hdev->recv_evt = NULL;
1676b6ddb638SJohan Hedberg 
16771da177e4SLinus Torvalds 	/* After this point our queues are empty
16781da177e4SLinus Torvalds 	 * and no tasks are scheduled. */
16791da177e4SLinus Torvalds 	hdev->close(hdev);
16801da177e4SLinus Torvalds 
168135b973c9SJohan Hedberg 	/* Clear flags */
1682fee746b0SMarcel Holtmann 	hdev->flags &= BIT(HCI_RAW);
168335b973c9SJohan Hedberg 	hdev->dev_flags &= ~HCI_PERSISTENT_MASK;
168435b973c9SJohan Hedberg 
1685ced5c338SAndrei Emeltchenko 	/* Controller radio is available but is currently powered down */
1686536619e8SMarcel Holtmann 	hdev->amp_status = AMP_STATUS_POWERED_DOWN;
1687ced5c338SAndrei Emeltchenko 
1688e59fda8dSJohan Hedberg 	memset(hdev->eir, 0, sizeof(hdev->eir));
168909b3c3fbSJohan Hedberg 	memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
16907a4cd51dSMarcel Holtmann 	bacpy(&hdev->random_addr, BDADDR_ANY);
1691e59fda8dSJohan Hedberg 
16921da177e4SLinus Torvalds 	hci_req_unlock(hdev);
16931da177e4SLinus Torvalds 
16941da177e4SLinus Torvalds 	hci_dev_put(hdev);
16951da177e4SLinus Torvalds 	return 0;
16961da177e4SLinus Torvalds }
16971da177e4SLinus Torvalds 
16981da177e4SLinus Torvalds int hci_dev_close(__u16 dev)
16991da177e4SLinus Torvalds {
17001da177e4SLinus Torvalds 	struct hci_dev *hdev;
17011da177e4SLinus Torvalds 	int err;
17021da177e4SLinus Torvalds 
170370f23020SAndrei Emeltchenko 	hdev = hci_dev_get(dev);
170470f23020SAndrei Emeltchenko 	if (!hdev)
17051da177e4SLinus Torvalds 		return -ENODEV;
17068ee56540SMarcel Holtmann 
17070736cfa8SMarcel Holtmann 	if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
17080736cfa8SMarcel Holtmann 		err = -EBUSY;
17090736cfa8SMarcel Holtmann 		goto done;
17100736cfa8SMarcel Holtmann 	}
17110736cfa8SMarcel Holtmann 
17128ee56540SMarcel Holtmann 	if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
17138ee56540SMarcel Holtmann 		cancel_delayed_work(&hdev->power_off);
17148ee56540SMarcel Holtmann 
17151da177e4SLinus Torvalds 	err = hci_dev_do_close(hdev);
17168ee56540SMarcel Holtmann 
17170736cfa8SMarcel Holtmann done:
17181da177e4SLinus Torvalds 	hci_dev_put(hdev);
17191da177e4SLinus Torvalds 	return err;
17201da177e4SLinus Torvalds }
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds int hci_dev_reset(__u16 dev)
17231da177e4SLinus Torvalds {
17241da177e4SLinus Torvalds 	struct hci_dev *hdev;
17251da177e4SLinus Torvalds 	int ret = 0;
17261da177e4SLinus Torvalds 
172770f23020SAndrei Emeltchenko 	hdev = hci_dev_get(dev);
172870f23020SAndrei Emeltchenko 	if (!hdev)
17291da177e4SLinus Torvalds 		return -ENODEV;
17301da177e4SLinus Torvalds 
17311da177e4SLinus Torvalds 	hci_req_lock(hdev);
17321da177e4SLinus Torvalds 
1733808a049eSMarcel Holtmann 	if (!test_bit(HCI_UP, &hdev->flags)) {
1734808a049eSMarcel Holtmann 		ret = -ENETDOWN;
17351da177e4SLinus Torvalds 		goto done;
1736808a049eSMarcel Holtmann 	}
17371da177e4SLinus Torvalds 
17380736cfa8SMarcel Holtmann 	if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
17390736cfa8SMarcel Holtmann 		ret = -EBUSY;
17400736cfa8SMarcel Holtmann 		goto done;
17410736cfa8SMarcel Holtmann 	}
17420736cfa8SMarcel Holtmann 
17434a964404SMarcel Holtmann 	if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) {
1744fee746b0SMarcel Holtmann 		ret = -EOPNOTSUPP;
1745fee746b0SMarcel Holtmann 		goto done;
1746fee746b0SMarcel Holtmann 	}
1747fee746b0SMarcel Holtmann 
17481da177e4SLinus Torvalds 	/* Drop queues */
17491da177e4SLinus Torvalds 	skb_queue_purge(&hdev->rx_q);
17501da177e4SLinus Torvalds 	skb_queue_purge(&hdev->cmd_q);
17511da177e4SLinus Torvalds 
175276727c02SJohan Hedberg 	/* Avoid potential lockdep warnings from the *_flush() calls by
175376727c02SJohan Hedberg 	 * ensuring the workqueue is empty up front.
175476727c02SJohan Hedberg 	 */
175576727c02SJohan Hedberg 	drain_workqueue(hdev->workqueue);
175676727c02SJohan Hedberg 
175709fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
17581f9b9a5dSAndre Guedes 	hci_inquiry_cache_flush(hdev);
17591da177e4SLinus Torvalds 	hci_conn_hash_flush(hdev);
176009fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
17611da177e4SLinus Torvalds 
17621da177e4SLinus Torvalds 	if (hdev->flush)
17631da177e4SLinus Torvalds 		hdev->flush(hdev);
17641da177e4SLinus Torvalds 
17651da177e4SLinus Torvalds 	atomic_set(&hdev->cmd_cnt, 1);
17666ed58ec5SVille Tervo 	hdev->acl_cnt = 0; hdev->sco_cnt = 0; hdev->le_cnt = 0;
17671da177e4SLinus Torvalds 
176801178cd4SJohan Hedberg 	ret = __hci_req_sync(hdev, hci_reset_req, 0, HCI_INIT_TIMEOUT);
17691da177e4SLinus Torvalds 
17701da177e4SLinus Torvalds done:
17711da177e4SLinus Torvalds 	hci_req_unlock(hdev);
17721da177e4SLinus Torvalds 	hci_dev_put(hdev);
17731da177e4SLinus Torvalds 	return ret;
17741da177e4SLinus Torvalds }
17751da177e4SLinus Torvalds 
17761da177e4SLinus Torvalds int hci_dev_reset_stat(__u16 dev)
17771da177e4SLinus Torvalds {
17781da177e4SLinus Torvalds 	struct hci_dev *hdev;
17791da177e4SLinus Torvalds 	int ret = 0;
17801da177e4SLinus Torvalds 
178170f23020SAndrei Emeltchenko 	hdev = hci_dev_get(dev);
178270f23020SAndrei Emeltchenko 	if (!hdev)
17831da177e4SLinus Torvalds 		return -ENODEV;
17841da177e4SLinus Torvalds 
17850736cfa8SMarcel Holtmann 	if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
17860736cfa8SMarcel Holtmann 		ret = -EBUSY;
17870736cfa8SMarcel Holtmann 		goto done;
17880736cfa8SMarcel Holtmann 	}
17890736cfa8SMarcel Holtmann 
17904a964404SMarcel Holtmann 	if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) {
1791fee746b0SMarcel Holtmann 		ret = -EOPNOTSUPP;
1792fee746b0SMarcel Holtmann 		goto done;
1793fee746b0SMarcel Holtmann 	}
1794fee746b0SMarcel Holtmann 
17951da177e4SLinus Torvalds 	memset(&hdev->stat, 0, sizeof(struct hci_dev_stats));
17961da177e4SLinus Torvalds 
17970736cfa8SMarcel Holtmann done:
17981da177e4SLinus Torvalds 	hci_dev_put(hdev);
17991da177e4SLinus Torvalds 	return ret;
18001da177e4SLinus Torvalds }
18011da177e4SLinus Torvalds 
1802123abc08SJohan Hedberg static void hci_update_scan_state(struct hci_dev *hdev, u8 scan)
1803123abc08SJohan Hedberg {
1804bc6d2d04SJohan Hedberg 	bool conn_changed, discov_changed;
1805123abc08SJohan Hedberg 
1806123abc08SJohan Hedberg 	BT_DBG("%s scan 0x%02x", hdev->name, scan);
1807123abc08SJohan Hedberg 
1808123abc08SJohan Hedberg 	if ((scan & SCAN_PAGE))
1809123abc08SJohan Hedberg 		conn_changed = !test_and_set_bit(HCI_CONNECTABLE,
1810123abc08SJohan Hedberg 						 &hdev->dev_flags);
1811123abc08SJohan Hedberg 	else
1812123abc08SJohan Hedberg 		conn_changed = test_and_clear_bit(HCI_CONNECTABLE,
1813123abc08SJohan Hedberg 						  &hdev->dev_flags);
1814123abc08SJohan Hedberg 
1815bc6d2d04SJohan Hedberg 	if ((scan & SCAN_INQUIRY)) {
1816bc6d2d04SJohan Hedberg 		discov_changed = !test_and_set_bit(HCI_DISCOVERABLE,
1817bc6d2d04SJohan Hedberg 						   &hdev->dev_flags);
1818bc6d2d04SJohan Hedberg 	} else {
1819bc6d2d04SJohan Hedberg 		clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
1820bc6d2d04SJohan Hedberg 		discov_changed = test_and_clear_bit(HCI_DISCOVERABLE,
1821bc6d2d04SJohan Hedberg 						    &hdev->dev_flags);
1822bc6d2d04SJohan Hedberg 	}
1823bc6d2d04SJohan Hedberg 
1824123abc08SJohan Hedberg 	if (!test_bit(HCI_MGMT, &hdev->dev_flags))
1825123abc08SJohan Hedberg 		return;
1826123abc08SJohan Hedberg 
1827bc6d2d04SJohan Hedberg 	if (conn_changed || discov_changed) {
1828bc6d2d04SJohan Hedberg 		/* In case this was disabled through mgmt */
1829bc6d2d04SJohan Hedberg 		set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags);
1830bc6d2d04SJohan Hedberg 
1831bc6d2d04SJohan Hedberg 		if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
1832bc6d2d04SJohan Hedberg 			mgmt_update_adv_data(hdev);
1833bc6d2d04SJohan Hedberg 
1834123abc08SJohan Hedberg 		mgmt_new_settings(hdev);
1835123abc08SJohan Hedberg 	}
1836bc6d2d04SJohan Hedberg }
1837123abc08SJohan Hedberg 
18381da177e4SLinus Torvalds int hci_dev_cmd(unsigned int cmd, void __user *arg)
18391da177e4SLinus Torvalds {
18401da177e4SLinus Torvalds 	struct hci_dev *hdev;
18411da177e4SLinus Torvalds 	struct hci_dev_req dr;
18421da177e4SLinus Torvalds 	int err = 0;
18431da177e4SLinus Torvalds 
18441da177e4SLinus Torvalds 	if (copy_from_user(&dr, arg, sizeof(dr)))
18451da177e4SLinus Torvalds 		return -EFAULT;
18461da177e4SLinus Torvalds 
184770f23020SAndrei Emeltchenko 	hdev = hci_dev_get(dr.dev_id);
184870f23020SAndrei Emeltchenko 	if (!hdev)
18491da177e4SLinus Torvalds 		return -ENODEV;
18501da177e4SLinus Torvalds 
18510736cfa8SMarcel Holtmann 	if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
18520736cfa8SMarcel Holtmann 		err = -EBUSY;
18530736cfa8SMarcel Holtmann 		goto done;
18540736cfa8SMarcel Holtmann 	}
18550736cfa8SMarcel Holtmann 
18564a964404SMarcel Holtmann 	if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) {
1857fee746b0SMarcel Holtmann 		err = -EOPNOTSUPP;
1858fee746b0SMarcel Holtmann 		goto done;
1859fee746b0SMarcel Holtmann 	}
1860fee746b0SMarcel Holtmann 
18615b69bef5SMarcel Holtmann 	if (hdev->dev_type != HCI_BREDR) {
18625b69bef5SMarcel Holtmann 		err = -EOPNOTSUPP;
18635b69bef5SMarcel Holtmann 		goto done;
18645b69bef5SMarcel Holtmann 	}
18655b69bef5SMarcel Holtmann 
186656f87901SJohan Hedberg 	if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
186756f87901SJohan Hedberg 		err = -EOPNOTSUPP;
186856f87901SJohan Hedberg 		goto done;
186956f87901SJohan Hedberg 	}
187056f87901SJohan Hedberg 
18711da177e4SLinus Torvalds 	switch (cmd) {
18721da177e4SLinus Torvalds 	case HCISETAUTH:
187301178cd4SJohan Hedberg 		err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt,
18745f246e89SAndrei Emeltchenko 				   HCI_INIT_TIMEOUT);
18751da177e4SLinus Torvalds 		break;
18761da177e4SLinus Torvalds 
18771da177e4SLinus Torvalds 	case HCISETENCRYPT:
18781da177e4SLinus Torvalds 		if (!lmp_encrypt_capable(hdev)) {
18791da177e4SLinus Torvalds 			err = -EOPNOTSUPP;
18801da177e4SLinus Torvalds 			break;
18811da177e4SLinus Torvalds 		}
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds 		if (!test_bit(HCI_AUTH, &hdev->flags)) {
18841da177e4SLinus Torvalds 			/* Auth must be enabled first */
188501178cd4SJohan Hedberg 			err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt,
18865f246e89SAndrei Emeltchenko 					   HCI_INIT_TIMEOUT);
18871da177e4SLinus Torvalds 			if (err)
18881da177e4SLinus Torvalds 				break;
18891da177e4SLinus Torvalds 		}
18901da177e4SLinus Torvalds 
189101178cd4SJohan Hedberg 		err = hci_req_sync(hdev, hci_encrypt_req, dr.dev_opt,
18925f246e89SAndrei Emeltchenko 				   HCI_INIT_TIMEOUT);
18931da177e4SLinus Torvalds 		break;
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds 	case HCISETSCAN:
189601178cd4SJohan Hedberg 		err = hci_req_sync(hdev, hci_scan_req, dr.dev_opt,
18975f246e89SAndrei Emeltchenko 				   HCI_INIT_TIMEOUT);
189891a668b0SJohan Hedberg 
1899bc6d2d04SJohan Hedberg 		/* Ensure that the connectable and discoverable states
1900bc6d2d04SJohan Hedberg 		 * get correctly modified as this was a non-mgmt change.
190191a668b0SJohan Hedberg 		 */
1902123abc08SJohan Hedberg 		if (!err)
1903123abc08SJohan Hedberg 			hci_update_scan_state(hdev, dr.dev_opt);
19041da177e4SLinus Torvalds 		break;
19051da177e4SLinus Torvalds 
19061da177e4SLinus Torvalds 	case HCISETLINKPOL:
190701178cd4SJohan Hedberg 		err = hci_req_sync(hdev, hci_linkpol_req, dr.dev_opt,
19085f246e89SAndrei Emeltchenko 				   HCI_INIT_TIMEOUT);
19091da177e4SLinus Torvalds 		break;
19101da177e4SLinus Torvalds 
19111da177e4SLinus Torvalds 	case HCISETLINKMODE:
1912e4e8e37cSMarcel Holtmann 		hdev->link_mode = ((__u16) dr.dev_opt) &
1913e4e8e37cSMarcel Holtmann 					(HCI_LM_MASTER | HCI_LM_ACCEPT);
1914e4e8e37cSMarcel Holtmann 		break;
1915e4e8e37cSMarcel Holtmann 
1916e4e8e37cSMarcel Holtmann 	case HCISETPTYPE:
1917e4e8e37cSMarcel Holtmann 		hdev->pkt_type = (__u16) dr.dev_opt;
19181da177e4SLinus Torvalds 		break;
19191da177e4SLinus Torvalds 
19201da177e4SLinus Torvalds 	case HCISETACLMTU:
19211da177e4SLinus Torvalds 		hdev->acl_mtu  = *((__u16 *) &dr.dev_opt + 1);
19221da177e4SLinus Torvalds 		hdev->acl_pkts = *((__u16 *) &dr.dev_opt + 0);
19231da177e4SLinus Torvalds 		break;
19241da177e4SLinus Torvalds 
19251da177e4SLinus Torvalds 	case HCISETSCOMTU:
19261da177e4SLinus Torvalds 		hdev->sco_mtu  = *((__u16 *) &dr.dev_opt + 1);
19271da177e4SLinus Torvalds 		hdev->sco_pkts = *((__u16 *) &dr.dev_opt + 0);
19281da177e4SLinus Torvalds 		break;
19291da177e4SLinus Torvalds 
19301da177e4SLinus Torvalds 	default:
19311da177e4SLinus Torvalds 		err = -EINVAL;
19321da177e4SLinus Torvalds 		break;
19331da177e4SLinus Torvalds 	}
1934e4e8e37cSMarcel Holtmann 
19350736cfa8SMarcel Holtmann done:
19361da177e4SLinus Torvalds 	hci_dev_put(hdev);
19371da177e4SLinus Torvalds 	return err;
19381da177e4SLinus Torvalds }
19391da177e4SLinus Torvalds 
19401da177e4SLinus Torvalds int hci_get_dev_list(void __user *arg)
19411da177e4SLinus Torvalds {
19428035ded4SLuiz Augusto von Dentz 	struct hci_dev *hdev;
19431da177e4SLinus Torvalds 	struct hci_dev_list_req *dl;
19441da177e4SLinus Torvalds 	struct hci_dev_req *dr;
19451da177e4SLinus Torvalds 	int n = 0, size, err;
19461da177e4SLinus Torvalds 	__u16 dev_num;
19471da177e4SLinus Torvalds 
19481da177e4SLinus Torvalds 	if (get_user(dev_num, (__u16 __user *) arg))
19491da177e4SLinus Torvalds 		return -EFAULT;
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds 	if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr))
19521da177e4SLinus Torvalds 		return -EINVAL;
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds 	size = sizeof(*dl) + dev_num * sizeof(*dr);
19551da177e4SLinus Torvalds 
195670f23020SAndrei Emeltchenko 	dl = kzalloc(size, GFP_KERNEL);
195770f23020SAndrei Emeltchenko 	if (!dl)
19581da177e4SLinus Torvalds 		return -ENOMEM;
19591da177e4SLinus Torvalds 
19601da177e4SLinus Torvalds 	dr = dl->dev_req;
19611da177e4SLinus Torvalds 
1962f20d09d5SGustavo F. Padovan 	read_lock(&hci_dev_list_lock);
19638035ded4SLuiz Augusto von Dentz 	list_for_each_entry(hdev, &hci_dev_list, list) {
19642e84d8dbSMarcel Holtmann 		unsigned long flags = hdev->flags;
1965c542a06cSJohan Hedberg 
19662e84d8dbSMarcel Holtmann 		/* When the auto-off is configured it means the transport
19672e84d8dbSMarcel Holtmann 		 * is running, but in that case still indicate that the
19682e84d8dbSMarcel Holtmann 		 * device is actually down.
19692e84d8dbSMarcel Holtmann 		 */
19702e84d8dbSMarcel Holtmann 		if (test_bit(HCI_AUTO_OFF, &hdev->dev_flags))
19712e84d8dbSMarcel Holtmann 			flags &= ~BIT(HCI_UP);
1972c542a06cSJohan Hedberg 
19731da177e4SLinus Torvalds 		(dr + n)->dev_id  = hdev->id;
19742e84d8dbSMarcel Holtmann 		(dr + n)->dev_opt = flags;
1975c542a06cSJohan Hedberg 
19761da177e4SLinus Torvalds 		if (++n >= dev_num)
19771da177e4SLinus Torvalds 			break;
19781da177e4SLinus Torvalds 	}
1979f20d09d5SGustavo F. Padovan 	read_unlock(&hci_dev_list_lock);
19801da177e4SLinus Torvalds 
19811da177e4SLinus Torvalds 	dl->dev_num = n;
19821da177e4SLinus Torvalds 	size = sizeof(*dl) + n * sizeof(*dr);
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds 	err = copy_to_user(arg, dl, size);
19851da177e4SLinus Torvalds 	kfree(dl);
19861da177e4SLinus Torvalds 
19871da177e4SLinus Torvalds 	return err ? -EFAULT : 0;
19881da177e4SLinus Torvalds }
19891da177e4SLinus Torvalds 
19901da177e4SLinus Torvalds int hci_get_dev_info(void __user *arg)
19911da177e4SLinus Torvalds {
19921da177e4SLinus Torvalds 	struct hci_dev *hdev;
19931da177e4SLinus Torvalds 	struct hci_dev_info di;
19942e84d8dbSMarcel Holtmann 	unsigned long flags;
19951da177e4SLinus Torvalds 	int err = 0;
19961da177e4SLinus Torvalds 
19971da177e4SLinus Torvalds 	if (copy_from_user(&di, arg, sizeof(di)))
19981da177e4SLinus Torvalds 		return -EFAULT;
19991da177e4SLinus Torvalds 
200070f23020SAndrei Emeltchenko 	hdev = hci_dev_get(di.dev_id);
200170f23020SAndrei Emeltchenko 	if (!hdev)
20021da177e4SLinus Torvalds 		return -ENODEV;
20031da177e4SLinus Torvalds 
20042e84d8dbSMarcel Holtmann 	/* When the auto-off is configured it means the transport
20052e84d8dbSMarcel Holtmann 	 * is running, but in that case still indicate that the
20062e84d8dbSMarcel Holtmann 	 * device is actually down.
20072e84d8dbSMarcel Holtmann 	 */
20082e84d8dbSMarcel Holtmann 	if (test_bit(HCI_AUTO_OFF, &hdev->dev_flags))
20092e84d8dbSMarcel Holtmann 		flags = hdev->flags & ~BIT(HCI_UP);
20102e84d8dbSMarcel Holtmann 	else
20112e84d8dbSMarcel Holtmann 		flags = hdev->flags;
2012c542a06cSJohan Hedberg 
20131da177e4SLinus Torvalds 	strcpy(di.name, hdev->name);
20141da177e4SLinus Torvalds 	di.bdaddr   = hdev->bdaddr;
201560f2a3edSMarcel Holtmann 	di.type     = (hdev->bus & 0x0f) | ((hdev->dev_type & 0x03) << 4);
20162e84d8dbSMarcel Holtmann 	di.flags    = flags;
20171da177e4SLinus Torvalds 	di.pkt_type = hdev->pkt_type;
2018572c7f84SJohan Hedberg 	if (lmp_bredr_capable(hdev)) {
20191da177e4SLinus Torvalds 		di.acl_mtu  = hdev->acl_mtu;
20201da177e4SLinus Torvalds 		di.acl_pkts = hdev->acl_pkts;
20211da177e4SLinus Torvalds 		di.sco_mtu  = hdev->sco_mtu;
20221da177e4SLinus Torvalds 		di.sco_pkts = hdev->sco_pkts;
2023572c7f84SJohan Hedberg 	} else {
2024572c7f84SJohan Hedberg 		di.acl_mtu  = hdev->le_mtu;
2025572c7f84SJohan Hedberg 		di.acl_pkts = hdev->le_pkts;
2026572c7f84SJohan Hedberg 		di.sco_mtu  = 0;
2027572c7f84SJohan Hedberg 		di.sco_pkts = 0;
2028572c7f84SJohan Hedberg 	}
20291da177e4SLinus Torvalds 	di.link_policy = hdev->link_policy;
20301da177e4SLinus Torvalds 	di.link_mode   = hdev->link_mode;
20311da177e4SLinus Torvalds 
20321da177e4SLinus Torvalds 	memcpy(&di.stat, &hdev->stat, sizeof(di.stat));
20331da177e4SLinus Torvalds 	memcpy(&di.features, &hdev->features, sizeof(di.features));
20341da177e4SLinus Torvalds 
20351da177e4SLinus Torvalds 	if (copy_to_user(arg, &di, sizeof(di)))
20361da177e4SLinus Torvalds 		err = -EFAULT;
20371da177e4SLinus Torvalds 
20381da177e4SLinus Torvalds 	hci_dev_put(hdev);
20391da177e4SLinus Torvalds 
20401da177e4SLinus Torvalds 	return err;
20411da177e4SLinus Torvalds }
20421da177e4SLinus Torvalds 
20431da177e4SLinus Torvalds /* ---- Interface to HCI drivers ---- */
20441da177e4SLinus Torvalds 
2045611b30f7SMarcel Holtmann static int hci_rfkill_set_block(void *data, bool blocked)
2046611b30f7SMarcel Holtmann {
2047611b30f7SMarcel Holtmann 	struct hci_dev *hdev = data;
2048611b30f7SMarcel Holtmann 
2049611b30f7SMarcel Holtmann 	BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked);
2050611b30f7SMarcel Holtmann 
20510736cfa8SMarcel Holtmann 	if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags))
20520736cfa8SMarcel Holtmann 		return -EBUSY;
20530736cfa8SMarcel Holtmann 
20545e130367SJohan Hedberg 	if (blocked) {
20555e130367SJohan Hedberg 		set_bit(HCI_RFKILLED, &hdev->dev_flags);
2056d603b76bSMarcel Holtmann 		if (!test_bit(HCI_SETUP, &hdev->dev_flags) &&
2057d603b76bSMarcel Holtmann 		    !test_bit(HCI_CONFIG, &hdev->dev_flags))
2058611b30f7SMarcel Holtmann 			hci_dev_do_close(hdev);
20595e130367SJohan Hedberg 	} else {
20605e130367SJohan Hedberg 		clear_bit(HCI_RFKILLED, &hdev->dev_flags);
20615e130367SJohan Hedberg 	}
2062611b30f7SMarcel Holtmann 
2063611b30f7SMarcel Holtmann 	return 0;
2064611b30f7SMarcel Holtmann }
2065611b30f7SMarcel Holtmann 
2066611b30f7SMarcel Holtmann static const struct rfkill_ops hci_rfkill_ops = {
2067611b30f7SMarcel Holtmann 	.set_block = hci_rfkill_set_block,
2068611b30f7SMarcel Holtmann };
2069611b30f7SMarcel Holtmann 
2070ab81cbf9SJohan Hedberg static void hci_power_on(struct work_struct *work)
2071ab81cbf9SJohan Hedberg {
2072ab81cbf9SJohan Hedberg 	struct hci_dev *hdev = container_of(work, struct hci_dev, power_on);
207396570ffcSJohan Hedberg 	int err;
2074ab81cbf9SJohan Hedberg 
2075ab81cbf9SJohan Hedberg 	BT_DBG("%s", hdev->name);
2076ab81cbf9SJohan Hedberg 
2077cbed0ca1SJohan Hedberg 	err = hci_dev_do_open(hdev);
207896570ffcSJohan Hedberg 	if (err < 0) {
20793ad67582SJaganath Kanakkassery 		hci_dev_lock(hdev);
208096570ffcSJohan Hedberg 		mgmt_set_powered_failed(hdev, err);
20813ad67582SJaganath Kanakkassery 		hci_dev_unlock(hdev);
2082ab81cbf9SJohan Hedberg 		return;
208396570ffcSJohan Hedberg 	}
2084ab81cbf9SJohan Hedberg 
2085a5c8f270SMarcel Holtmann 	/* During the HCI setup phase, a few error conditions are
2086a5c8f270SMarcel Holtmann 	 * ignored and they need to be checked now. If they are still
2087a5c8f270SMarcel Holtmann 	 * valid, it is important to turn the device back off.
2088a5c8f270SMarcel Holtmann 	 */
2089a5c8f270SMarcel Holtmann 	if (test_bit(HCI_RFKILLED, &hdev->dev_flags) ||
20904a964404SMarcel Holtmann 	    test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) ||
2091a5c8f270SMarcel Holtmann 	    (hdev->dev_type == HCI_BREDR &&
2092a5c8f270SMarcel Holtmann 	     !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
2093a5c8f270SMarcel Holtmann 	     !bacmp(&hdev->static_addr, BDADDR_ANY))) {
2094bf543036SJohan Hedberg 		clear_bit(HCI_AUTO_OFF, &hdev->dev_flags);
2095bf543036SJohan Hedberg 		hci_dev_do_close(hdev);
2096bf543036SJohan Hedberg 	} else if (test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) {
209719202573SJohan Hedberg 		queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
209819202573SJohan Hedberg 				   HCI_AUTO_OFF_TIMEOUT);
2099bf543036SJohan Hedberg 	}
2100ab81cbf9SJohan Hedberg 
2101fee746b0SMarcel Holtmann 	if (test_and_clear_bit(HCI_SETUP, &hdev->dev_flags)) {
21024a964404SMarcel Holtmann 		/* For unconfigured devices, set the HCI_RAW flag
21034a964404SMarcel Holtmann 		 * so that userspace can easily identify them.
21044a964404SMarcel Holtmann 		 */
21054a964404SMarcel Holtmann 		if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags))
21064a964404SMarcel Holtmann 			set_bit(HCI_RAW, &hdev->flags);
21070602a8adSMarcel Holtmann 
21080602a8adSMarcel Holtmann 		/* For fully configured devices, this will send
21090602a8adSMarcel Holtmann 		 * the Index Added event. For unconfigured devices,
21100602a8adSMarcel Holtmann 		 * it will send Unconfigued Index Added event.
21110602a8adSMarcel Holtmann 		 *
21120602a8adSMarcel Holtmann 		 * Devices with HCI_QUIRK_RAW_DEVICE are ignored
21130602a8adSMarcel Holtmann 		 * and no event will be send.
21140602a8adSMarcel Holtmann 		 */
2115744cf19eSJohan Hedberg 		mgmt_index_added(hdev);
2116d603b76bSMarcel Holtmann 	} else if (test_and_clear_bit(HCI_CONFIG, &hdev->dev_flags)) {
21175ea234d3SMarcel Holtmann 		/* When the controller is now configured, then it
21185ea234d3SMarcel Holtmann 		 * is important to clear the HCI_RAW flag.
21195ea234d3SMarcel Holtmann 		 */
21205ea234d3SMarcel Holtmann 		if (!test_bit(HCI_UNCONFIGURED, &hdev->dev_flags))
21215ea234d3SMarcel Holtmann 			clear_bit(HCI_RAW, &hdev->flags);
21225ea234d3SMarcel Holtmann 
2123d603b76bSMarcel Holtmann 		/* Powering on the controller with HCI_CONFIG set only
2124d603b76bSMarcel Holtmann 		 * happens with the transition from unconfigured to
2125d603b76bSMarcel Holtmann 		 * configured. This will send the Index Added event.
2126d603b76bSMarcel Holtmann 		 */
2127d603b76bSMarcel Holtmann 		mgmt_index_added(hdev);
2128ab81cbf9SJohan Hedberg 	}
2129ab81cbf9SJohan Hedberg }
2130ab81cbf9SJohan Hedberg 
2131ab81cbf9SJohan Hedberg static void hci_power_off(struct work_struct *work)
2132ab81cbf9SJohan Hedberg {
21333243553fSJohan Hedberg 	struct hci_dev *hdev = container_of(work, struct hci_dev,
21343243553fSJohan Hedberg 					    power_off.work);
2135ab81cbf9SJohan Hedberg 
2136ab81cbf9SJohan Hedberg 	BT_DBG("%s", hdev->name);
2137ab81cbf9SJohan Hedberg 
21388ee56540SMarcel Holtmann 	hci_dev_do_close(hdev);
2139ab81cbf9SJohan Hedberg }
2140ab81cbf9SJohan Hedberg 
214116ab91abSJohan Hedberg static void hci_discov_off(struct work_struct *work)
214216ab91abSJohan Hedberg {
214316ab91abSJohan Hedberg 	struct hci_dev *hdev;
214416ab91abSJohan Hedberg 
214516ab91abSJohan Hedberg 	hdev = container_of(work, struct hci_dev, discov_off.work);
214616ab91abSJohan Hedberg 
214716ab91abSJohan Hedberg 	BT_DBG("%s", hdev->name);
214816ab91abSJohan Hedberg 
2149d1967ff8SMarcel Holtmann 	mgmt_discoverable_timeout(hdev);
215016ab91abSJohan Hedberg }
215116ab91abSJohan Hedberg 
215235f7498aSJohan Hedberg void hci_uuids_clear(struct hci_dev *hdev)
21532aeb9a1aSJohan Hedberg {
21544821002cSJohan Hedberg 	struct bt_uuid *uuid, *tmp;
21552aeb9a1aSJohan Hedberg 
21564821002cSJohan Hedberg 	list_for_each_entry_safe(uuid, tmp, &hdev->uuids, list) {
21574821002cSJohan Hedberg 		list_del(&uuid->list);
21582aeb9a1aSJohan Hedberg 		kfree(uuid);
21592aeb9a1aSJohan Hedberg 	}
21602aeb9a1aSJohan Hedberg }
21612aeb9a1aSJohan Hedberg 
216235f7498aSJohan Hedberg void hci_link_keys_clear(struct hci_dev *hdev)
216355ed8ca1SJohan Hedberg {
216455ed8ca1SJohan Hedberg 	struct link_key *key;
216555ed8ca1SJohan Hedberg 
21660378b597SJohan Hedberg 	list_for_each_entry_rcu(key, &hdev->link_keys, list) {
21670378b597SJohan Hedberg 		list_del_rcu(&key->list);
21680378b597SJohan Hedberg 		kfree_rcu(key, rcu);
216955ed8ca1SJohan Hedberg 	}
217055ed8ca1SJohan Hedberg }
217155ed8ca1SJohan Hedberg 
217235f7498aSJohan Hedberg void hci_smp_ltks_clear(struct hci_dev *hdev)
2173b899efafSVinicius Costa Gomes {
2174970d0f1bSJohan Hedberg 	struct smp_ltk *k;
2175b899efafSVinicius Costa Gomes 
2176970d0f1bSJohan Hedberg 	list_for_each_entry_rcu(k, &hdev->long_term_keys, list) {
2177970d0f1bSJohan Hedberg 		list_del_rcu(&k->list);
2178970d0f1bSJohan Hedberg 		kfree_rcu(k, rcu);
2179b899efafSVinicius Costa Gomes 	}
2180b899efafSVinicius Costa Gomes }
2181b899efafSVinicius Costa Gomes 
2182970c4e46SJohan Hedberg void hci_smp_irks_clear(struct hci_dev *hdev)
2183970c4e46SJohan Hedberg {
2184adae20cbSJohan Hedberg 	struct smp_irk *k;
2185970c4e46SJohan Hedberg 
2186adae20cbSJohan Hedberg 	list_for_each_entry_rcu(k, &hdev->identity_resolving_keys, list) {
2187adae20cbSJohan Hedberg 		list_del_rcu(&k->list);
2188adae20cbSJohan Hedberg 		kfree_rcu(k, rcu);
2189970c4e46SJohan Hedberg 	}
2190970c4e46SJohan Hedberg }
2191970c4e46SJohan Hedberg 
219255ed8ca1SJohan Hedberg struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr)
219355ed8ca1SJohan Hedberg {
219455ed8ca1SJohan Hedberg 	struct link_key *k;
219555ed8ca1SJohan Hedberg 
21960378b597SJohan Hedberg 	rcu_read_lock();
21970378b597SJohan Hedberg 	list_for_each_entry_rcu(k, &hdev->link_keys, list) {
21980378b597SJohan Hedberg 		if (bacmp(bdaddr, &k->bdaddr) == 0) {
21990378b597SJohan Hedberg 			rcu_read_unlock();
220055ed8ca1SJohan Hedberg 			return k;
22010378b597SJohan Hedberg 		}
22020378b597SJohan Hedberg 	}
22030378b597SJohan Hedberg 	rcu_read_unlock();
220455ed8ca1SJohan Hedberg 
220555ed8ca1SJohan Hedberg 	return NULL;
220655ed8ca1SJohan Hedberg }
220755ed8ca1SJohan Hedberg 
2208745c0ce3SVishal Agarwal static bool hci_persistent_key(struct hci_dev *hdev, struct hci_conn *conn,
2209d25e28abSJohan Hedberg 			       u8 key_type, u8 old_key_type)
2210d25e28abSJohan Hedberg {
2211d25e28abSJohan Hedberg 	/* Legacy key */
2212d25e28abSJohan Hedberg 	if (key_type < 0x03)
2213745c0ce3SVishal Agarwal 		return true;
2214d25e28abSJohan Hedberg 
2215d25e28abSJohan Hedberg 	/* Debug keys are insecure so don't store them persistently */
2216d25e28abSJohan Hedberg 	if (key_type == HCI_LK_DEBUG_COMBINATION)
2217745c0ce3SVishal Agarwal 		return false;
2218d25e28abSJohan Hedberg 
2219d25e28abSJohan Hedberg 	/* Changed combination key and there's no previous one */
2220d25e28abSJohan Hedberg 	if (key_type == HCI_LK_CHANGED_COMBINATION && old_key_type == 0xff)
2221745c0ce3SVishal Agarwal 		return false;
2222d25e28abSJohan Hedberg 
2223d25e28abSJohan Hedberg 	/* Security mode 3 case */
2224d25e28abSJohan Hedberg 	if (!conn)
2225745c0ce3SVishal Agarwal 		return true;
2226d25e28abSJohan Hedberg 
2227e3befab9SJohan Hedberg 	/* BR/EDR key derived using SC from an LE link */
2228e3befab9SJohan Hedberg 	if (conn->type == LE_LINK)
2229e3befab9SJohan Hedberg 		return true;
2230e3befab9SJohan Hedberg 
2231d25e28abSJohan Hedberg 	/* Neither local nor remote side had no-bonding as requirement */
2232d25e28abSJohan Hedberg 	if (conn->auth_type > 0x01 && conn->remote_auth > 0x01)
2233745c0ce3SVishal Agarwal 		return true;
2234d25e28abSJohan Hedberg 
2235d25e28abSJohan Hedberg 	/* Local side had dedicated bonding as requirement */
2236d25e28abSJohan Hedberg 	if (conn->auth_type == 0x02 || conn->auth_type == 0x03)
2237745c0ce3SVishal Agarwal 		return true;
2238d25e28abSJohan Hedberg 
2239d25e28abSJohan Hedberg 	/* Remote side had dedicated bonding as requirement */
2240d25e28abSJohan Hedberg 	if (conn->remote_auth == 0x02 || conn->remote_auth == 0x03)
2241745c0ce3SVishal Agarwal 		return true;
2242d25e28abSJohan Hedberg 
2243d25e28abSJohan Hedberg 	/* If none of the above criteria match, then don't store the key
2244d25e28abSJohan Hedberg 	 * persistently */
2245745c0ce3SVishal Agarwal 	return false;
2246d25e28abSJohan Hedberg }
2247d25e28abSJohan Hedberg 
2248e804d25dSJohan Hedberg static u8 ltk_role(u8 type)
224998a0b845SJohan Hedberg {
2250e804d25dSJohan Hedberg 	if (type == SMP_LTK)
2251e804d25dSJohan Hedberg 		return HCI_ROLE_MASTER;
225298a0b845SJohan Hedberg 
2253e804d25dSJohan Hedberg 	return HCI_ROLE_SLAVE;
225498a0b845SJohan Hedberg }
225598a0b845SJohan Hedberg 
2256f3a73d97SJohan Hedberg struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr,
2257e804d25dSJohan Hedberg 			     u8 addr_type, u8 role)
225875d262c2SVinicius Costa Gomes {
2259c9839a11SVinicius Costa Gomes 	struct smp_ltk *k;
226075d262c2SVinicius Costa Gomes 
2261970d0f1bSJohan Hedberg 	rcu_read_lock();
2262970d0f1bSJohan Hedberg 	list_for_each_entry_rcu(k, &hdev->long_term_keys, list) {
22635378bc56SJohan Hedberg 		if (addr_type != k->bdaddr_type || bacmp(bdaddr, &k->bdaddr))
22645378bc56SJohan Hedberg 			continue;
22655378bc56SJohan Hedberg 
2266923e2414SJohan Hedberg 		if (smp_ltk_is_sc(k) || ltk_role(k->type) == role) {
2267970d0f1bSJohan Hedberg 			rcu_read_unlock();
226875d262c2SVinicius Costa Gomes 			return k;
2269970d0f1bSJohan Hedberg 		}
2270970d0f1bSJohan Hedberg 	}
2271970d0f1bSJohan Hedberg 	rcu_read_unlock();
227275d262c2SVinicius Costa Gomes 
227375d262c2SVinicius Costa Gomes 	return NULL;
227475d262c2SVinicius Costa Gomes }
227575d262c2SVinicius Costa Gomes 
2276970c4e46SJohan Hedberg struct smp_irk *hci_find_irk_by_rpa(struct hci_dev *hdev, bdaddr_t *rpa)
2277970c4e46SJohan Hedberg {
2278970c4e46SJohan Hedberg 	struct smp_irk *irk;
2279970c4e46SJohan Hedberg 
2280adae20cbSJohan Hedberg 	rcu_read_lock();
2281adae20cbSJohan Hedberg 	list_for_each_entry_rcu(irk, &hdev->identity_resolving_keys, list) {
2282adae20cbSJohan Hedberg 		if (!bacmp(&irk->rpa, rpa)) {
2283adae20cbSJohan Hedberg 			rcu_read_unlock();
2284970c4e46SJohan Hedberg 			return irk;
2285970c4e46SJohan Hedberg 		}
2286adae20cbSJohan Hedberg 	}
2287970c4e46SJohan Hedberg 
2288adae20cbSJohan Hedberg 	list_for_each_entry_rcu(irk, &hdev->identity_resolving_keys, list) {
2289defce9e8SJohan Hedberg 		if (smp_irk_matches(hdev, irk->val, rpa)) {
2290970c4e46SJohan Hedberg 			bacpy(&irk->rpa, rpa);
2291adae20cbSJohan Hedberg 			rcu_read_unlock();
2292970c4e46SJohan Hedberg 			return irk;
2293970c4e46SJohan Hedberg 		}
2294970c4e46SJohan Hedberg 	}
2295adae20cbSJohan Hedberg 	rcu_read_unlock();
2296970c4e46SJohan Hedberg 
2297970c4e46SJohan Hedberg 	return NULL;
2298970c4e46SJohan Hedberg }
2299970c4e46SJohan Hedberg 
2300970c4e46SJohan Hedberg struct smp_irk *hci_find_irk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr,
2301970c4e46SJohan Hedberg 				     u8 addr_type)
2302970c4e46SJohan Hedberg {
2303970c4e46SJohan Hedberg 	struct smp_irk *irk;
2304970c4e46SJohan Hedberg 
23056cfc9988SJohan Hedberg 	/* Identity Address must be public or static random */
23066cfc9988SJohan Hedberg 	if (addr_type == ADDR_LE_DEV_RANDOM && (bdaddr->b[5] & 0xc0) != 0xc0)
23076cfc9988SJohan Hedberg 		return NULL;
23086cfc9988SJohan Hedberg 
2309adae20cbSJohan Hedberg 	rcu_read_lock();
2310adae20cbSJohan Hedberg 	list_for_each_entry_rcu(irk, &hdev->identity_resolving_keys, list) {
2311970c4e46SJohan Hedberg 		if (addr_type == irk->addr_type &&
2312adae20cbSJohan Hedberg 		    bacmp(bdaddr, &irk->bdaddr) == 0) {
2313adae20cbSJohan Hedberg 			rcu_read_unlock();
2314970c4e46SJohan Hedberg 			return irk;
2315970c4e46SJohan Hedberg 		}
2316adae20cbSJohan Hedberg 	}
2317adae20cbSJohan Hedberg 	rcu_read_unlock();
2318970c4e46SJohan Hedberg 
2319970c4e46SJohan Hedberg 	return NULL;
2320970c4e46SJohan Hedberg }
2321970c4e46SJohan Hedberg 
2322567fa2aaSJohan Hedberg struct link_key *hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn,
23237652ff6aSJohan Hedberg 				  bdaddr_t *bdaddr, u8 *val, u8 type,
23247652ff6aSJohan Hedberg 				  u8 pin_len, bool *persistent)
232555ed8ca1SJohan Hedberg {
232655ed8ca1SJohan Hedberg 	struct link_key *key, *old_key;
2327745c0ce3SVishal Agarwal 	u8 old_key_type;
232855ed8ca1SJohan Hedberg 
232955ed8ca1SJohan Hedberg 	old_key = hci_find_link_key(hdev, bdaddr);
233055ed8ca1SJohan Hedberg 	if (old_key) {
233155ed8ca1SJohan Hedberg 		old_key_type = old_key->type;
233255ed8ca1SJohan Hedberg 		key = old_key;
233355ed8ca1SJohan Hedberg 	} else {
233412adcf3aSJohan Hedberg 		old_key_type = conn ? conn->key_type : 0xff;
23350a14ab41SJohan Hedberg 		key = kzalloc(sizeof(*key), GFP_KERNEL);
233655ed8ca1SJohan Hedberg 		if (!key)
2337567fa2aaSJohan Hedberg 			return NULL;
23380378b597SJohan Hedberg 		list_add_rcu(&key->list, &hdev->link_keys);
233955ed8ca1SJohan Hedberg 	}
234055ed8ca1SJohan Hedberg 
23416ed93dc6SAndrei Emeltchenko 	BT_DBG("%s key for %pMR type %u", hdev->name, bdaddr, type);
234255ed8ca1SJohan Hedberg 
2343d25e28abSJohan Hedberg 	/* Some buggy controller combinations generate a changed
2344d25e28abSJohan Hedberg 	 * combination key for legacy pairing even when there's no
2345d25e28abSJohan Hedberg 	 * previous key */
2346d25e28abSJohan Hedberg 	if (type == HCI_LK_CHANGED_COMBINATION &&
2347a8c5fb1aSGustavo Padovan 	    (!conn || conn->remote_auth == 0xff) && old_key_type == 0xff) {
2348d25e28abSJohan Hedberg 		type = HCI_LK_COMBINATION;
2349655fe6ecSJohan Hedberg 		if (conn)
2350655fe6ecSJohan Hedberg 			conn->key_type = type;
2351655fe6ecSJohan Hedberg 	}
2352d25e28abSJohan Hedberg 
235355ed8ca1SJohan Hedberg 	bacpy(&key->bdaddr, bdaddr);
23549b3b4460SAndrei Emeltchenko 	memcpy(key->val, val, HCI_LINK_KEY_SIZE);
235555ed8ca1SJohan Hedberg 	key->pin_len = pin_len;
235655ed8ca1SJohan Hedberg 
2357b6020ba0SWaldemar Rymarkiewicz 	if (type == HCI_LK_CHANGED_COMBINATION)
235855ed8ca1SJohan Hedberg 		key->type = old_key_type;
23594748fed2SJohan Hedberg 	else
23604748fed2SJohan Hedberg 		key->type = type;
23614748fed2SJohan Hedberg 
23627652ff6aSJohan Hedberg 	if (persistent)
23637652ff6aSJohan Hedberg 		*persistent = hci_persistent_key(hdev, conn, type,
23647652ff6aSJohan Hedberg 						 old_key_type);
23654df378a1SJohan Hedberg 
2366567fa2aaSJohan Hedberg 	return key;
236755ed8ca1SJohan Hedberg }
236855ed8ca1SJohan Hedberg 
2369ca9142b8SJohan Hedberg struct smp_ltk *hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr,
237035d70271SJohan Hedberg 			    u8 addr_type, u8 type, u8 authenticated,
2371fe39c7b2SMarcel Holtmann 			    u8 tk[16], u8 enc_size, __le16 ediv, __le64 rand)
237275d262c2SVinicius Costa Gomes {
2373c9839a11SVinicius Costa Gomes 	struct smp_ltk *key, *old_key;
2374e804d25dSJohan Hedberg 	u8 role = ltk_role(type);
237575d262c2SVinicius Costa Gomes 
2376f3a73d97SJohan Hedberg 	old_key = hci_find_ltk(hdev, bdaddr, addr_type, role);
2377c9839a11SVinicius Costa Gomes 	if (old_key)
237875d262c2SVinicius Costa Gomes 		key = old_key;
2379c9839a11SVinicius Costa Gomes 	else {
23800a14ab41SJohan Hedberg 		key = kzalloc(sizeof(*key), GFP_KERNEL);
238175d262c2SVinicius Costa Gomes 		if (!key)
2382ca9142b8SJohan Hedberg 			return NULL;
2383970d0f1bSJohan Hedberg 		list_add_rcu(&key->list, &hdev->long_term_keys);
238475d262c2SVinicius Costa Gomes 	}
238575d262c2SVinicius Costa Gomes 
238675d262c2SVinicius Costa Gomes 	bacpy(&key->bdaddr, bdaddr);
2387c9839a11SVinicius Costa Gomes 	key->bdaddr_type = addr_type;
2388c9839a11SVinicius Costa Gomes 	memcpy(key->val, tk, sizeof(key->val));
2389c9839a11SVinicius Costa Gomes 	key->authenticated = authenticated;
2390c9839a11SVinicius Costa Gomes 	key->ediv = ediv;
2391fe39c7b2SMarcel Holtmann 	key->rand = rand;
2392c9839a11SVinicius Costa Gomes 	key->enc_size = enc_size;
2393c9839a11SVinicius Costa Gomes 	key->type = type;
239475d262c2SVinicius Costa Gomes 
2395ca9142b8SJohan Hedberg 	return key;
239675d262c2SVinicius Costa Gomes }
239775d262c2SVinicius Costa Gomes 
2398ca9142b8SJohan Hedberg struct smp_irk *hci_add_irk(struct hci_dev *hdev, bdaddr_t *bdaddr,
2399ca9142b8SJohan Hedberg 			    u8 addr_type, u8 val[16], bdaddr_t *rpa)
2400970c4e46SJohan Hedberg {
2401970c4e46SJohan Hedberg 	struct smp_irk *irk;
2402970c4e46SJohan Hedberg 
2403970c4e46SJohan Hedberg 	irk = hci_find_irk_by_addr(hdev, bdaddr, addr_type);
2404970c4e46SJohan Hedberg 	if (!irk) {
2405970c4e46SJohan Hedberg 		irk = kzalloc(sizeof(*irk), GFP_KERNEL);
2406970c4e46SJohan Hedberg 		if (!irk)
2407ca9142b8SJohan Hedberg 			return NULL;
2408970c4e46SJohan Hedberg 
2409970c4e46SJohan Hedberg 		bacpy(&irk->bdaddr, bdaddr);
2410970c4e46SJohan Hedberg 		irk->addr_type = addr_type;
2411970c4e46SJohan Hedberg 
2412adae20cbSJohan Hedberg 		list_add_rcu(&irk->list, &hdev->identity_resolving_keys);
2413970c4e46SJohan Hedberg 	}
2414970c4e46SJohan Hedberg 
2415970c4e46SJohan Hedberg 	memcpy(irk->val, val, 16);
2416970c4e46SJohan Hedberg 	bacpy(&irk->rpa, rpa);
2417970c4e46SJohan Hedberg 
2418ca9142b8SJohan Hedberg 	return irk;
2419970c4e46SJohan Hedberg }
2420970c4e46SJohan Hedberg 
242155ed8ca1SJohan Hedberg int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr)
242255ed8ca1SJohan Hedberg {
242355ed8ca1SJohan Hedberg 	struct link_key *key;
242455ed8ca1SJohan Hedberg 
242555ed8ca1SJohan Hedberg 	key = hci_find_link_key(hdev, bdaddr);
242655ed8ca1SJohan Hedberg 	if (!key)
242755ed8ca1SJohan Hedberg 		return -ENOENT;
242855ed8ca1SJohan Hedberg 
24296ed93dc6SAndrei Emeltchenko 	BT_DBG("%s removing %pMR", hdev->name, bdaddr);
243055ed8ca1SJohan Hedberg 
24310378b597SJohan Hedberg 	list_del_rcu(&key->list);
24320378b597SJohan Hedberg 	kfree_rcu(key, rcu);
243355ed8ca1SJohan Hedberg 
243455ed8ca1SJohan Hedberg 	return 0;
243555ed8ca1SJohan Hedberg }
243655ed8ca1SJohan Hedberg 
2437e0b2b27eSJohan Hedberg int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 bdaddr_type)
2438b899efafSVinicius Costa Gomes {
2439970d0f1bSJohan Hedberg 	struct smp_ltk *k;
2440c51ffa0bSJohan Hedberg 	int removed = 0;
2441b899efafSVinicius Costa Gomes 
2442970d0f1bSJohan Hedberg 	list_for_each_entry_rcu(k, &hdev->long_term_keys, list) {
2443e0b2b27eSJohan Hedberg 		if (bacmp(bdaddr, &k->bdaddr) || k->bdaddr_type != bdaddr_type)
2444b899efafSVinicius Costa Gomes 			continue;
2445b899efafSVinicius Costa Gomes 
24466ed93dc6SAndrei Emeltchenko 		BT_DBG("%s removing %pMR", hdev->name, bdaddr);
2447b899efafSVinicius Costa Gomes 
2448970d0f1bSJohan Hedberg 		list_del_rcu(&k->list);
2449970d0f1bSJohan Hedberg 		kfree_rcu(k, rcu);
2450c51ffa0bSJohan Hedberg 		removed++;
2451b899efafSVinicius Costa Gomes 	}
2452b899efafSVinicius Costa Gomes 
2453c51ffa0bSJohan Hedberg 	return removed ? 0 : -ENOENT;
2454b899efafSVinicius Costa Gomes }
2455b899efafSVinicius Costa Gomes 
2456a7ec7338SJohan Hedberg void hci_remove_irk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type)
2457a7ec7338SJohan Hedberg {
2458adae20cbSJohan Hedberg 	struct smp_irk *k;
2459a7ec7338SJohan Hedberg 
2460adae20cbSJohan Hedberg 	list_for_each_entry_rcu(k, &hdev->identity_resolving_keys, list) {
2461a7ec7338SJohan Hedberg 		if (bacmp(bdaddr, &k->bdaddr) || k->addr_type != addr_type)
2462a7ec7338SJohan Hedberg 			continue;
2463a7ec7338SJohan Hedberg 
2464a7ec7338SJohan Hedberg 		BT_DBG("%s removing %pMR", hdev->name, bdaddr);
2465a7ec7338SJohan Hedberg 
2466adae20cbSJohan Hedberg 		list_del_rcu(&k->list);
2467adae20cbSJohan Hedberg 		kfree_rcu(k, rcu);
2468a7ec7338SJohan Hedberg 	}
2469a7ec7338SJohan Hedberg }
2470a7ec7338SJohan Hedberg 
24716bd32326SVille Tervo /* HCI command timer function */
247265cc2b49SMarcel Holtmann static void hci_cmd_timeout(struct work_struct *work)
24736bd32326SVille Tervo {
247465cc2b49SMarcel Holtmann 	struct hci_dev *hdev = container_of(work, struct hci_dev,
247565cc2b49SMarcel Holtmann 					    cmd_timer.work);
24766bd32326SVille Tervo 
2477bda4f23aSAndrei Emeltchenko 	if (hdev->sent_cmd) {
2478bda4f23aSAndrei Emeltchenko 		struct hci_command_hdr *sent = (void *) hdev->sent_cmd->data;
2479bda4f23aSAndrei Emeltchenko 		u16 opcode = __le16_to_cpu(sent->opcode);
2480bda4f23aSAndrei Emeltchenko 
2481bda4f23aSAndrei Emeltchenko 		BT_ERR("%s command 0x%4.4x tx timeout", hdev->name, opcode);
2482bda4f23aSAndrei Emeltchenko 	} else {
24836bd32326SVille Tervo 		BT_ERR("%s command tx timeout", hdev->name);
2484bda4f23aSAndrei Emeltchenko 	}
2485bda4f23aSAndrei Emeltchenko 
24866bd32326SVille Tervo 	atomic_set(&hdev->cmd_cnt, 1);
2487c347b765SGustavo F. Padovan 	queue_work(hdev->workqueue, &hdev->cmd_work);
24886bd32326SVille Tervo }
24896bd32326SVille Tervo 
24902763eda6SSzymon Janc struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev,
24916928a924SJohan Hedberg 					  bdaddr_t *bdaddr, u8 bdaddr_type)
24922763eda6SSzymon Janc {
24932763eda6SSzymon Janc 	struct oob_data *data;
24942763eda6SSzymon Janc 
24956928a924SJohan Hedberg 	list_for_each_entry(data, &hdev->remote_oob_data, list) {
24966928a924SJohan Hedberg 		if (bacmp(bdaddr, &data->bdaddr) != 0)
24976928a924SJohan Hedberg 			continue;
24986928a924SJohan Hedberg 		if (data->bdaddr_type != bdaddr_type)
24996928a924SJohan Hedberg 			continue;
25002763eda6SSzymon Janc 		return data;
25016928a924SJohan Hedberg 	}
25022763eda6SSzymon Janc 
25032763eda6SSzymon Janc 	return NULL;
25042763eda6SSzymon Janc }
25052763eda6SSzymon Janc 
25066928a924SJohan Hedberg int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr,
25076928a924SJohan Hedberg 			       u8 bdaddr_type)
25082763eda6SSzymon Janc {
25092763eda6SSzymon Janc 	struct oob_data *data;
25102763eda6SSzymon Janc 
25116928a924SJohan Hedberg 	data = hci_find_remote_oob_data(hdev, bdaddr, bdaddr_type);
25122763eda6SSzymon Janc 	if (!data)
25132763eda6SSzymon Janc 		return -ENOENT;
25142763eda6SSzymon Janc 
25156928a924SJohan Hedberg 	BT_DBG("%s removing %pMR (%u)", hdev->name, bdaddr, bdaddr_type);
25162763eda6SSzymon Janc 
25172763eda6SSzymon Janc 	list_del(&data->list);
25182763eda6SSzymon Janc 	kfree(data);
25192763eda6SSzymon Janc 
25202763eda6SSzymon Janc 	return 0;
25212763eda6SSzymon Janc }
25222763eda6SSzymon Janc 
252335f7498aSJohan Hedberg void hci_remote_oob_data_clear(struct hci_dev *hdev)
25242763eda6SSzymon Janc {
25252763eda6SSzymon Janc 	struct oob_data *data, *n;
25262763eda6SSzymon Janc 
25272763eda6SSzymon Janc 	list_for_each_entry_safe(data, n, &hdev->remote_oob_data, list) {
25282763eda6SSzymon Janc 		list_del(&data->list);
25292763eda6SSzymon Janc 		kfree(data);
25302763eda6SSzymon Janc 	}
25312763eda6SSzymon Janc }
25322763eda6SSzymon Janc 
25330798872eSMarcel Holtmann int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr,
25346928a924SJohan Hedberg 			    u8 bdaddr_type, u8 *hash192, u8 *rand192,
253538da1703SJohan Hedberg 			    u8 *hash256, u8 *rand256)
25360798872eSMarcel Holtmann {
25370798872eSMarcel Holtmann 	struct oob_data *data;
25380798872eSMarcel Holtmann 
25396928a924SJohan Hedberg 	data = hci_find_remote_oob_data(hdev, bdaddr, bdaddr_type);
25400798872eSMarcel Holtmann 	if (!data) {
25410a14ab41SJohan Hedberg 		data = kmalloc(sizeof(*data), GFP_KERNEL);
25420798872eSMarcel Holtmann 		if (!data)
25430798872eSMarcel Holtmann 			return -ENOMEM;
25440798872eSMarcel Holtmann 
25450798872eSMarcel Holtmann 		bacpy(&data->bdaddr, bdaddr);
25466928a924SJohan Hedberg 		data->bdaddr_type = bdaddr_type;
25470798872eSMarcel Holtmann 		list_add(&data->list, &hdev->remote_oob_data);
25480798872eSMarcel Holtmann 	}
25490798872eSMarcel Holtmann 
255081328d5cSJohan Hedberg 	if (hash192 && rand192) {
25510798872eSMarcel Holtmann 		memcpy(data->hash192, hash192, sizeof(data->hash192));
255238da1703SJohan Hedberg 		memcpy(data->rand192, rand192, sizeof(data->rand192));
255381328d5cSJohan Hedberg 	} else {
255481328d5cSJohan Hedberg 		memset(data->hash192, 0, sizeof(data->hash192));
255581328d5cSJohan Hedberg 		memset(data->rand192, 0, sizeof(data->rand192));
255681328d5cSJohan Hedberg 	}
25570798872eSMarcel Holtmann 
255881328d5cSJohan Hedberg 	if (hash256 && rand256) {
25590798872eSMarcel Holtmann 		memcpy(data->hash256, hash256, sizeof(data->hash256));
256038da1703SJohan Hedberg 		memcpy(data->rand256, rand256, sizeof(data->rand256));
256181328d5cSJohan Hedberg 	} else {
256281328d5cSJohan Hedberg 		memset(data->hash256, 0, sizeof(data->hash256));
256381328d5cSJohan Hedberg 		memset(data->rand256, 0, sizeof(data->rand256));
256481328d5cSJohan Hedberg 	}
25650798872eSMarcel Holtmann 
25666ed93dc6SAndrei Emeltchenko 	BT_DBG("%s for %pMR", hdev->name, bdaddr);
25672763eda6SSzymon Janc 
25682763eda6SSzymon Janc 	return 0;
25692763eda6SSzymon Janc }
25702763eda6SSzymon Janc 
2571dcc36c16SJohan Hedberg struct bdaddr_list *hci_bdaddr_list_lookup(struct list_head *bdaddr_list,
2572b9ee0a78SMarcel Holtmann 					 bdaddr_t *bdaddr, u8 type)
2573b2a66aadSAntti Julku {
2574b2a66aadSAntti Julku 	struct bdaddr_list *b;
2575b2a66aadSAntti Julku 
2576dcc36c16SJohan Hedberg 	list_for_each_entry(b, bdaddr_list, list) {
2577b9ee0a78SMarcel Holtmann 		if (!bacmp(&b->bdaddr, bdaddr) && b->bdaddr_type == type)
2578b2a66aadSAntti Julku 			return b;
2579b9ee0a78SMarcel Holtmann 	}
2580b2a66aadSAntti Julku 
2581b2a66aadSAntti Julku 	return NULL;
2582b2a66aadSAntti Julku }
2583b2a66aadSAntti Julku 
2584dcc36c16SJohan Hedberg void hci_bdaddr_list_clear(struct list_head *bdaddr_list)
2585b2a66aadSAntti Julku {
2586b2a66aadSAntti Julku 	struct list_head *p, *n;
2587b2a66aadSAntti Julku 
2588dcc36c16SJohan Hedberg 	list_for_each_safe(p, n, bdaddr_list) {
2589b9ee0a78SMarcel Holtmann 		struct bdaddr_list *b = list_entry(p, struct bdaddr_list, list);
2590b2a66aadSAntti Julku 
2591b2a66aadSAntti Julku 		list_del(p);
2592b2a66aadSAntti Julku 		kfree(b);
2593b2a66aadSAntti Julku 	}
2594b2a66aadSAntti Julku }
2595b2a66aadSAntti Julku 
2596dcc36c16SJohan Hedberg int hci_bdaddr_list_add(struct list_head *list, bdaddr_t *bdaddr, u8 type)
2597b2a66aadSAntti Julku {
2598b2a66aadSAntti Julku 	struct bdaddr_list *entry;
2599b2a66aadSAntti Julku 
2600b9ee0a78SMarcel Holtmann 	if (!bacmp(bdaddr, BDADDR_ANY))
2601b2a66aadSAntti Julku 		return -EBADF;
2602b2a66aadSAntti Julku 
2603dcc36c16SJohan Hedberg 	if (hci_bdaddr_list_lookup(list, bdaddr, type))
26045e762444SAntti Julku 		return -EEXIST;
2605b2a66aadSAntti Julku 
260627f70f3eSJohan Hedberg 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
26075e762444SAntti Julku 	if (!entry)
26085e762444SAntti Julku 		return -ENOMEM;
2609b2a66aadSAntti Julku 
2610b2a66aadSAntti Julku 	bacpy(&entry->bdaddr, bdaddr);
2611b9ee0a78SMarcel Holtmann 	entry->bdaddr_type = type;
2612b2a66aadSAntti Julku 
2613dcc36c16SJohan Hedberg 	list_add(&entry->list, list);
2614b2a66aadSAntti Julku 
26152a8357f2SJohan Hedberg 	return 0;
2616b2a66aadSAntti Julku }
2617b2a66aadSAntti Julku 
2618dcc36c16SJohan Hedberg int hci_bdaddr_list_del(struct list_head *list, bdaddr_t *bdaddr, u8 type)
2619b2a66aadSAntti Julku {
2620b2a66aadSAntti Julku 	struct bdaddr_list *entry;
2621b2a66aadSAntti Julku 
262235f7498aSJohan Hedberg 	if (!bacmp(bdaddr, BDADDR_ANY)) {
2623dcc36c16SJohan Hedberg 		hci_bdaddr_list_clear(list);
262435f7498aSJohan Hedberg 		return 0;
262535f7498aSJohan Hedberg 	}
2626b2a66aadSAntti Julku 
2627dcc36c16SJohan Hedberg 	entry = hci_bdaddr_list_lookup(list, bdaddr, type);
2628d2ab0ac1SMarcel Holtmann 	if (!entry)
2629d2ab0ac1SMarcel Holtmann 		return -ENOENT;
2630d2ab0ac1SMarcel Holtmann 
2631d2ab0ac1SMarcel Holtmann 	list_del(&entry->list);
2632d2ab0ac1SMarcel Holtmann 	kfree(entry);
2633d2ab0ac1SMarcel Holtmann 
2634d2ab0ac1SMarcel Holtmann 	return 0;
2635d2ab0ac1SMarcel Holtmann }
2636d2ab0ac1SMarcel Holtmann 
263715819a70SAndre Guedes /* This function requires the caller holds hdev->lock */
263815819a70SAndre Guedes struct hci_conn_params *hci_conn_params_lookup(struct hci_dev *hdev,
263915819a70SAndre Guedes 					       bdaddr_t *addr, u8 addr_type)
264015819a70SAndre Guedes {
264115819a70SAndre Guedes 	struct hci_conn_params *params;
264215819a70SAndre Guedes 
2643738f6185SJohan Hedberg 	/* The conn params list only contains identity addresses */
2644738f6185SJohan Hedberg 	if (!hci_is_identity_address(addr, addr_type))
2645738f6185SJohan Hedberg 		return NULL;
2646738f6185SJohan Hedberg 
264715819a70SAndre Guedes 	list_for_each_entry(params, &hdev->le_conn_params, list) {
264815819a70SAndre Guedes 		if (bacmp(&params->addr, addr) == 0 &&
264915819a70SAndre Guedes 		    params->addr_type == addr_type) {
265015819a70SAndre Guedes 			return params;
265115819a70SAndre Guedes 		}
265215819a70SAndre Guedes 	}
265315819a70SAndre Guedes 
265415819a70SAndre Guedes 	return NULL;
265515819a70SAndre Guedes }
265615819a70SAndre Guedes 
265715819a70SAndre Guedes /* This function requires the caller holds hdev->lock */
2658501f8827SJohan Hedberg struct hci_conn_params *hci_pend_le_action_lookup(struct list_head *list,
26594b10966fSMarcel Holtmann 						  bdaddr_t *addr, u8 addr_type)
266015819a70SAndre Guedes {
2661912b42efSJohan Hedberg 	struct hci_conn_params *param;
266215819a70SAndre Guedes 
2663738f6185SJohan Hedberg 	/* The list only contains identity addresses */
2664738f6185SJohan Hedberg 	if (!hci_is_identity_address(addr, addr_type))
2665738f6185SJohan Hedberg 		return NULL;
266615819a70SAndre Guedes 
2667501f8827SJohan Hedberg 	list_for_each_entry(param, list, action) {
2668912b42efSJohan Hedberg 		if (bacmp(&param->addr, addr) == 0 &&
2669912b42efSJohan Hedberg 		    param->addr_type == addr_type)
2670912b42efSJohan Hedberg 			return param;
26714b10966fSMarcel Holtmann 	}
26724b10966fSMarcel Holtmann 
26734b10966fSMarcel Holtmann 	return NULL;
267415819a70SAndre Guedes }
267515819a70SAndre Guedes 
267615819a70SAndre Guedes /* This function requires the caller holds hdev->lock */
267751d167c0SMarcel Holtmann struct hci_conn_params *hci_conn_params_add(struct hci_dev *hdev,
267851d167c0SMarcel Holtmann 					    bdaddr_t *addr, u8 addr_type)
267915819a70SAndre Guedes {
268015819a70SAndre Guedes 	struct hci_conn_params *params;
268115819a70SAndre Guedes 
2682c46245b3SJohan Hedberg 	if (!hci_is_identity_address(addr, addr_type))
268351d167c0SMarcel Holtmann 		return NULL;
2684a9b0a04cSAndre Guedes 
268515819a70SAndre Guedes 	params = hci_conn_params_lookup(hdev, addr, addr_type);
2686cef952ceSAndre Guedes 	if (params)
268751d167c0SMarcel Holtmann 		return params;
268815819a70SAndre Guedes 
268915819a70SAndre Guedes 	params = kzalloc(sizeof(*params), GFP_KERNEL);
269015819a70SAndre Guedes 	if (!params) {
269115819a70SAndre Guedes 		BT_ERR("Out of memory");
269251d167c0SMarcel Holtmann 		return NULL;
269315819a70SAndre Guedes 	}
269415819a70SAndre Guedes 
269515819a70SAndre Guedes 	bacpy(&params->addr, addr);
269615819a70SAndre Guedes 	params->addr_type = addr_type;
2697cef952ceSAndre Guedes 
2698cef952ceSAndre Guedes 	list_add(&params->list, &hdev->le_conn_params);
269993450c75SJohan Hedberg 	INIT_LIST_HEAD(&params->action);
2700cef952ceSAndre Guedes 
2701bf5b3c8bSMarcel Holtmann 	params->conn_min_interval = hdev->le_conn_min_interval;
2702bf5b3c8bSMarcel Holtmann 	params->conn_max_interval = hdev->le_conn_max_interval;
2703bf5b3c8bSMarcel Holtmann 	params->conn_latency = hdev->le_conn_latency;
2704bf5b3c8bSMarcel Holtmann 	params->supervision_timeout = hdev->le_supv_timeout;
2705bf5b3c8bSMarcel Holtmann 	params->auto_connect = HCI_AUTO_CONN_DISABLED;
2706bf5b3c8bSMarcel Holtmann 
2707bf5b3c8bSMarcel Holtmann 	BT_DBG("addr %pMR (type %u)", addr, addr_type);
2708bf5b3c8bSMarcel Holtmann 
270951d167c0SMarcel Holtmann 	return params;
2710bf5b3c8bSMarcel Holtmann }
2711bf5b3c8bSMarcel Holtmann 
2712f6c63249SJohan Hedberg static void hci_conn_params_free(struct hci_conn_params *params)
2713f6c63249SJohan Hedberg {
2714f6c63249SJohan Hedberg 	if (params->conn) {
2715f6c63249SJohan Hedberg 		hci_conn_drop(params->conn);
2716f6c63249SJohan Hedberg 		hci_conn_put(params->conn);
2717f6c63249SJohan Hedberg 	}
2718f6c63249SJohan Hedberg 
2719f6c63249SJohan Hedberg 	list_del(&params->action);
2720f6c63249SJohan Hedberg 	list_del(&params->list);
2721f6c63249SJohan Hedberg 	kfree(params);
2722f6c63249SJohan Hedberg }
2723f6c63249SJohan Hedberg 
272415819a70SAndre Guedes /* This function requires the caller holds hdev->lock */
272515819a70SAndre Guedes void hci_conn_params_del(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type)
272615819a70SAndre Guedes {
272715819a70SAndre Guedes 	struct hci_conn_params *params;
272815819a70SAndre Guedes 
272915819a70SAndre Guedes 	params = hci_conn_params_lookup(hdev, addr, addr_type);
273015819a70SAndre Guedes 	if (!params)
273115819a70SAndre Guedes 		return;
273215819a70SAndre Guedes 
2733f6c63249SJohan Hedberg 	hci_conn_params_free(params);
273415819a70SAndre Guedes 
273595305baaSJohan Hedberg 	hci_update_background_scan(hdev);
273695305baaSJohan Hedberg 
273715819a70SAndre Guedes 	BT_DBG("addr %pMR (type %u)", addr, addr_type);
273815819a70SAndre Guedes }
273915819a70SAndre Guedes 
274015819a70SAndre Guedes /* This function requires the caller holds hdev->lock */
274155af49a8SJohan Hedberg void hci_conn_params_clear_disabled(struct hci_dev *hdev)
274215819a70SAndre Guedes {
274315819a70SAndre Guedes 	struct hci_conn_params *params, *tmp;
274415819a70SAndre Guedes 
274515819a70SAndre Guedes 	list_for_each_entry_safe(params, tmp, &hdev->le_conn_params, list) {
274655af49a8SJohan Hedberg 		if (params->auto_connect != HCI_AUTO_CONN_DISABLED)
274755af49a8SJohan Hedberg 			continue;
274815819a70SAndre Guedes 		list_del(&params->list);
274915819a70SAndre Guedes 		kfree(params);
275015819a70SAndre Guedes 	}
275115819a70SAndre Guedes 
275255af49a8SJohan Hedberg 	BT_DBG("All LE disabled connection parameters were removed");
275355af49a8SJohan Hedberg }
275455af49a8SJohan Hedberg 
275555af49a8SJohan Hedberg /* This function requires the caller holds hdev->lock */
2756373110c5SJohan Hedberg void hci_conn_params_clear_all(struct hci_dev *hdev)
275715819a70SAndre Guedes {
275815819a70SAndre Guedes 	struct hci_conn_params *params, *tmp;
275915819a70SAndre Guedes 
2760f6c63249SJohan Hedberg 	list_for_each_entry_safe(params, tmp, &hdev->le_conn_params, list)
2761f6c63249SJohan Hedberg 		hci_conn_params_free(params);
276215819a70SAndre Guedes 
2763a2f41a8fSJohan Hedberg 	hci_update_background_scan(hdev);
27641089b67dSMarcel Holtmann 
276515819a70SAndre Guedes 	BT_DBG("All LE connection parameters were removed");
276615819a70SAndre Guedes }
276715819a70SAndre Guedes 
27684c87eaabSAndre Guedes static void inquiry_complete(struct hci_dev *hdev, u8 status)
27697ba8b4beSAndre Guedes {
27704c87eaabSAndre Guedes 	if (status) {
27714c87eaabSAndre Guedes 		BT_ERR("Failed to start inquiry: status %d", status);
27727ba8b4beSAndre Guedes 
27734c87eaabSAndre Guedes 		hci_dev_lock(hdev);
27744c87eaabSAndre Guedes 		hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
27754c87eaabSAndre Guedes 		hci_dev_unlock(hdev);
27764c87eaabSAndre Guedes 		return;
27774c87eaabSAndre Guedes 	}
27787ba8b4beSAndre Guedes }
27797ba8b4beSAndre Guedes 
27804c87eaabSAndre Guedes static void le_scan_disable_work_complete(struct hci_dev *hdev, u8 status)
27817ba8b4beSAndre Guedes {
27824c87eaabSAndre Guedes 	/* General inquiry access code (GIAC) */
27834c87eaabSAndre Guedes 	u8 lap[3] = { 0x33, 0x8b, 0x9e };
27844c87eaabSAndre Guedes 	struct hci_request req;
27854c87eaabSAndre Guedes 	struct hci_cp_inquiry cp;
27867ba8b4beSAndre Guedes 	int err;
27877ba8b4beSAndre Guedes 
27884c87eaabSAndre Guedes 	if (status) {
27894c87eaabSAndre Guedes 		BT_ERR("Failed to disable LE scanning: status %d", status);
27904c87eaabSAndre Guedes 		return;
27917ba8b4beSAndre Guedes 	}
27927ba8b4beSAndre Guedes 
27934c87eaabSAndre Guedes 	switch (hdev->discovery.type) {
27944c87eaabSAndre Guedes 	case DISCOV_TYPE_LE:
27954c87eaabSAndre Guedes 		hci_dev_lock(hdev);
27964c87eaabSAndre Guedes 		hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
27974c87eaabSAndre Guedes 		hci_dev_unlock(hdev);
27984c87eaabSAndre Guedes 		break;
27997dbfac1dSAndre Guedes 
28004c87eaabSAndre Guedes 	case DISCOV_TYPE_INTERLEAVED:
28014c87eaabSAndre Guedes 		hci_req_init(&req, hdev);
28027dbfac1dSAndre Guedes 
28037dbfac1dSAndre Guedes 		memset(&cp, 0, sizeof(cp));
28044c87eaabSAndre Guedes 		memcpy(&cp.lap, lap, sizeof(cp.lap));
28054c87eaabSAndre Guedes 		cp.length = DISCOV_INTERLEAVED_INQUIRY_LEN;
28064c87eaabSAndre Guedes 		hci_req_add(&req, HCI_OP_INQUIRY, sizeof(cp), &cp);
28074c87eaabSAndre Guedes 
28084c87eaabSAndre Guedes 		hci_dev_lock(hdev);
28094c87eaabSAndre Guedes 
28104c87eaabSAndre Guedes 		hci_inquiry_cache_flush(hdev);
28114c87eaabSAndre Guedes 
28124c87eaabSAndre Guedes 		err = hci_req_run(&req, inquiry_complete);
28134c87eaabSAndre Guedes 		if (err) {
28144c87eaabSAndre Guedes 			BT_ERR("Inquiry request failed: err %d", err);
28154c87eaabSAndre Guedes 			hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
28167dbfac1dSAndre Guedes 		}
28177dbfac1dSAndre Guedes 
28184c87eaabSAndre Guedes 		hci_dev_unlock(hdev);
28194c87eaabSAndre Guedes 		break;
28204c87eaabSAndre Guedes 	}
28217dbfac1dSAndre Guedes }
28227dbfac1dSAndre Guedes 
28237ba8b4beSAndre Guedes static void le_scan_disable_work(struct work_struct *work)
28247ba8b4beSAndre Guedes {
28257ba8b4beSAndre Guedes 	struct hci_dev *hdev = container_of(work, struct hci_dev,
28267ba8b4beSAndre Guedes 					    le_scan_disable.work);
28274c87eaabSAndre Guedes 	struct hci_request req;
28284c87eaabSAndre Guedes 	int err;
28297ba8b4beSAndre Guedes 
28307ba8b4beSAndre Guedes 	BT_DBG("%s", hdev->name);
28317ba8b4beSAndre Guedes 
28324c87eaabSAndre Guedes 	hci_req_init(&req, hdev);
28337ba8b4beSAndre Guedes 
2834b1efcc28SAndre Guedes 	hci_req_add_le_scan_disable(&req);
28357ba8b4beSAndre Guedes 
28364c87eaabSAndre Guedes 	err = hci_req_run(&req, le_scan_disable_work_complete);
28374c87eaabSAndre Guedes 	if (err)
28384c87eaabSAndre Guedes 		BT_ERR("Disable LE scanning request failed: err %d", err);
283928b75a89SAndre Guedes }
284028b75a89SAndre Guedes 
2841a1f4c318SJohan Hedberg /* Copy the Identity Address of the controller.
2842a1f4c318SJohan Hedberg  *
2843a1f4c318SJohan Hedberg  * If the controller has a public BD_ADDR, then by default use that one.
2844a1f4c318SJohan Hedberg  * If this is a LE only controller without a public address, default to
2845a1f4c318SJohan Hedberg  * the static random address.
2846a1f4c318SJohan Hedberg  *
2847a1f4c318SJohan Hedberg  * For debugging purposes it is possible to force controllers with a
2848a1f4c318SJohan Hedberg  * public address to use the static random address instead.
284950b5b952SMarcel Holtmann  *
285050b5b952SMarcel Holtmann  * In case BR/EDR has been disabled on a dual-mode controller and
285150b5b952SMarcel Holtmann  * userspace has configured a static address, then that address
285250b5b952SMarcel Holtmann  * becomes the identity address instead of the public BR/EDR address.
2853a1f4c318SJohan Hedberg  */
2854a1f4c318SJohan Hedberg void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr,
2855a1f4c318SJohan Hedberg 			       u8 *bdaddr_type)
2856a1f4c318SJohan Hedberg {
2857111902f7SMarcel Holtmann 	if (test_bit(HCI_FORCE_STATIC_ADDR, &hdev->dbg_flags) ||
285850b5b952SMarcel Holtmann 	    !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
285950b5b952SMarcel Holtmann 	    (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) &&
286050b5b952SMarcel Holtmann 	     bacmp(&hdev->static_addr, BDADDR_ANY))) {
2861a1f4c318SJohan Hedberg 		bacpy(bdaddr, &hdev->static_addr);
2862a1f4c318SJohan Hedberg 		*bdaddr_type = ADDR_LE_DEV_RANDOM;
2863a1f4c318SJohan Hedberg 	} else {
2864a1f4c318SJohan Hedberg 		bacpy(bdaddr, &hdev->bdaddr);
2865a1f4c318SJohan Hedberg 		*bdaddr_type = ADDR_LE_DEV_PUBLIC;
2866a1f4c318SJohan Hedberg 	}
2867a1f4c318SJohan Hedberg }
2868a1f4c318SJohan Hedberg 
28699be0dab7SDavid Herrmann /* Alloc HCI device */
28709be0dab7SDavid Herrmann struct hci_dev *hci_alloc_dev(void)
28719be0dab7SDavid Herrmann {
28729be0dab7SDavid Herrmann 	struct hci_dev *hdev;
28739be0dab7SDavid Herrmann 
287427f70f3eSJohan Hedberg 	hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
28759be0dab7SDavid Herrmann 	if (!hdev)
28769be0dab7SDavid Herrmann 		return NULL;
28779be0dab7SDavid Herrmann 
2878b1b813d4SDavid Herrmann 	hdev->pkt_type  = (HCI_DM1 | HCI_DH1 | HCI_HV1);
2879b1b813d4SDavid Herrmann 	hdev->esco_type = (ESCO_HV1);
2880b1b813d4SDavid Herrmann 	hdev->link_mode = (HCI_LM_ACCEPT);
2881b4cb9fb2SMarcel Holtmann 	hdev->num_iac = 0x01;		/* One IAC support is mandatory */
2882b1b813d4SDavid Herrmann 	hdev->io_capability = 0x03;	/* No Input No Output */
288396c2103aSMarcel Holtmann 	hdev->manufacturer = 0xffff;	/* Default to internal use */
2884bbaf444aSJohan Hedberg 	hdev->inq_tx_power = HCI_TX_POWER_INVALID;
2885bbaf444aSJohan Hedberg 	hdev->adv_tx_power = HCI_TX_POWER_INVALID;
2886b1b813d4SDavid Herrmann 
2887b1b813d4SDavid Herrmann 	hdev->sniff_max_interval = 800;
2888b1b813d4SDavid Herrmann 	hdev->sniff_min_interval = 80;
2889b1b813d4SDavid Herrmann 
28903f959d46SMarcel Holtmann 	hdev->le_adv_channel_map = 0x07;
2891628531c9SGeorg Lukas 	hdev->le_adv_min_interval = 0x0800;
2892628531c9SGeorg Lukas 	hdev->le_adv_max_interval = 0x0800;
2893bef64738SMarcel Holtmann 	hdev->le_scan_interval = 0x0060;
2894bef64738SMarcel Holtmann 	hdev->le_scan_window = 0x0030;
28954e70c7e7SMarcel Holtmann 	hdev->le_conn_min_interval = 0x0028;
28964e70c7e7SMarcel Holtmann 	hdev->le_conn_max_interval = 0x0038;
289704fb7d90SMarcel Holtmann 	hdev->le_conn_latency = 0x0000;
289804fb7d90SMarcel Holtmann 	hdev->le_supv_timeout = 0x002a;
2899bef64738SMarcel Holtmann 
2900d6bfd59cSJohan Hedberg 	hdev->rpa_timeout = HCI_DEFAULT_RPA_TIMEOUT;
2901b9a7a61eSLukasz Rymanowski 	hdev->discov_interleaved_timeout = DISCOV_INTERLEAVED_TIMEOUT;
290231ad1691SAndrzej Kaczmarek 	hdev->conn_info_min_age = DEFAULT_CONN_INFO_MIN_AGE;
290331ad1691SAndrzej Kaczmarek 	hdev->conn_info_max_age = DEFAULT_CONN_INFO_MAX_AGE;
2904d6bfd59cSJohan Hedberg 
2905b1b813d4SDavid Herrmann 	mutex_init(&hdev->lock);
2906b1b813d4SDavid Herrmann 	mutex_init(&hdev->req_lock);
2907b1b813d4SDavid Herrmann 
2908b1b813d4SDavid Herrmann 	INIT_LIST_HEAD(&hdev->mgmt_pending);
2909b1b813d4SDavid Herrmann 	INIT_LIST_HEAD(&hdev->blacklist);
29106659358eSJohan Hedberg 	INIT_LIST_HEAD(&hdev->whitelist);
2911b1b813d4SDavid Herrmann 	INIT_LIST_HEAD(&hdev->uuids);
2912b1b813d4SDavid Herrmann 	INIT_LIST_HEAD(&hdev->link_keys);
2913b1b813d4SDavid Herrmann 	INIT_LIST_HEAD(&hdev->long_term_keys);
2914970c4e46SJohan Hedberg 	INIT_LIST_HEAD(&hdev->identity_resolving_keys);
2915b1b813d4SDavid Herrmann 	INIT_LIST_HEAD(&hdev->remote_oob_data);
2916d2ab0ac1SMarcel Holtmann 	INIT_LIST_HEAD(&hdev->le_white_list);
291715819a70SAndre Guedes 	INIT_LIST_HEAD(&hdev->le_conn_params);
291877a77a30SAndre Guedes 	INIT_LIST_HEAD(&hdev->pend_le_conns);
291966f8455aSJohan Hedberg 	INIT_LIST_HEAD(&hdev->pend_le_reports);
29206b536b5eSAndrei Emeltchenko 	INIT_LIST_HEAD(&hdev->conn_hash.list);
2921b1b813d4SDavid Herrmann 
2922b1b813d4SDavid Herrmann 	INIT_WORK(&hdev->rx_work, hci_rx_work);
2923b1b813d4SDavid Herrmann 	INIT_WORK(&hdev->cmd_work, hci_cmd_work);
2924b1b813d4SDavid Herrmann 	INIT_WORK(&hdev->tx_work, hci_tx_work);
2925b1b813d4SDavid Herrmann 	INIT_WORK(&hdev->power_on, hci_power_on);
2926b1b813d4SDavid Herrmann 
2927b1b813d4SDavid Herrmann 	INIT_DELAYED_WORK(&hdev->power_off, hci_power_off);
2928b1b813d4SDavid Herrmann 	INIT_DELAYED_WORK(&hdev->discov_off, hci_discov_off);
2929b1b813d4SDavid Herrmann 	INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable_work);
2930b1b813d4SDavid Herrmann 
2931b1b813d4SDavid Herrmann 	skb_queue_head_init(&hdev->rx_q);
2932b1b813d4SDavid Herrmann 	skb_queue_head_init(&hdev->cmd_q);
2933b1b813d4SDavid Herrmann 	skb_queue_head_init(&hdev->raw_q);
2934b1b813d4SDavid Herrmann 
2935b1b813d4SDavid Herrmann 	init_waitqueue_head(&hdev->req_wait_q);
2936b1b813d4SDavid Herrmann 
293765cc2b49SMarcel Holtmann 	INIT_DELAYED_WORK(&hdev->cmd_timer, hci_cmd_timeout);
2938b1b813d4SDavid Herrmann 
2939b1b813d4SDavid Herrmann 	hci_init_sysfs(hdev);
2940b1b813d4SDavid Herrmann 	discovery_init(hdev);
29419be0dab7SDavid Herrmann 
29429be0dab7SDavid Herrmann 	return hdev;
29439be0dab7SDavid Herrmann }
29449be0dab7SDavid Herrmann EXPORT_SYMBOL(hci_alloc_dev);
29459be0dab7SDavid Herrmann 
29469be0dab7SDavid Herrmann /* Free HCI device */
29479be0dab7SDavid Herrmann void hci_free_dev(struct hci_dev *hdev)
29489be0dab7SDavid Herrmann {
29499be0dab7SDavid Herrmann 	/* will free via device release */
29509be0dab7SDavid Herrmann 	put_device(&hdev->dev);
29519be0dab7SDavid Herrmann }
29529be0dab7SDavid Herrmann EXPORT_SYMBOL(hci_free_dev);
29539be0dab7SDavid Herrmann 
29541da177e4SLinus Torvalds /* Register HCI device */
29551da177e4SLinus Torvalds int hci_register_dev(struct hci_dev *hdev)
29561da177e4SLinus Torvalds {
2957b1b813d4SDavid Herrmann 	int id, error;
29581da177e4SLinus Torvalds 
295974292d5aSMarcel Holtmann 	if (!hdev->open || !hdev->close || !hdev->send)
29601da177e4SLinus Torvalds 		return -EINVAL;
29611da177e4SLinus Torvalds 
296208add513SMat Martineau 	/* Do not allow HCI_AMP devices to register at index 0,
296308add513SMat Martineau 	 * so the index can be used as the AMP controller ID.
296408add513SMat Martineau 	 */
29653df92b31SSasha Levin 	switch (hdev->dev_type) {
29663df92b31SSasha Levin 	case HCI_BREDR:
29673df92b31SSasha Levin 		id = ida_simple_get(&hci_index_ida, 0, 0, GFP_KERNEL);
29681da177e4SLinus Torvalds 		break;
29693df92b31SSasha Levin 	case HCI_AMP:
29703df92b31SSasha Levin 		id = ida_simple_get(&hci_index_ida, 1, 0, GFP_KERNEL);
29713df92b31SSasha Levin 		break;
29723df92b31SSasha Levin 	default:
29733df92b31SSasha Levin 		return -EINVAL;
29741da177e4SLinus Torvalds 	}
29751da177e4SLinus Torvalds 
29763df92b31SSasha Levin 	if (id < 0)
29773df92b31SSasha Levin 		return id;
29783df92b31SSasha Levin 
29791da177e4SLinus Torvalds 	sprintf(hdev->name, "hci%d", id);
29801da177e4SLinus Torvalds 	hdev->id = id;
29812d8b3a11SAndrei Emeltchenko 
29822d8b3a11SAndrei Emeltchenko 	BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
29832d8b3a11SAndrei Emeltchenko 
2984d8537548SKees Cook 	hdev->workqueue = alloc_workqueue("%s", WQ_HIGHPRI | WQ_UNBOUND |
2985d8537548SKees Cook 					  WQ_MEM_RECLAIM, 1, hdev->name);
298633ca954dSDavid Herrmann 	if (!hdev->workqueue) {
298733ca954dSDavid Herrmann 		error = -ENOMEM;
298833ca954dSDavid Herrmann 		goto err;
298933ca954dSDavid Herrmann 	}
2990f48fd9c8SMarcel Holtmann 
2991d8537548SKees Cook 	hdev->req_workqueue = alloc_workqueue("%s", WQ_HIGHPRI | WQ_UNBOUND |
2992d8537548SKees Cook 					      WQ_MEM_RECLAIM, 1, hdev->name);
29936ead1bbcSJohan Hedberg 	if (!hdev->req_workqueue) {
29946ead1bbcSJohan Hedberg 		destroy_workqueue(hdev->workqueue);
29956ead1bbcSJohan Hedberg 		error = -ENOMEM;
29966ead1bbcSJohan Hedberg 		goto err;
29976ead1bbcSJohan Hedberg 	}
29986ead1bbcSJohan Hedberg 
29990153e2ecSMarcel Holtmann 	if (!IS_ERR_OR_NULL(bt_debugfs))
30000153e2ecSMarcel Holtmann 		hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs);
30010153e2ecSMarcel Holtmann 
3002bdc3e0f1SMarcel Holtmann 	dev_set_name(&hdev->dev, "%s", hdev->name);
3003bdc3e0f1SMarcel Holtmann 
3004bdc3e0f1SMarcel Holtmann 	error = device_add(&hdev->dev);
300533ca954dSDavid Herrmann 	if (error < 0)
300654506918SJohan Hedberg 		goto err_wqueue;
30071da177e4SLinus Torvalds 
3008611b30f7SMarcel Holtmann 	hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev,
3009a8c5fb1aSGustavo Padovan 				    RFKILL_TYPE_BLUETOOTH, &hci_rfkill_ops,
3010a8c5fb1aSGustavo Padovan 				    hdev);
3011611b30f7SMarcel Holtmann 	if (hdev->rfkill) {
3012611b30f7SMarcel Holtmann 		if (rfkill_register(hdev->rfkill) < 0) {
3013611b30f7SMarcel Holtmann 			rfkill_destroy(hdev->rfkill);
3014611b30f7SMarcel Holtmann 			hdev->rfkill = NULL;
3015611b30f7SMarcel Holtmann 		}
3016611b30f7SMarcel Holtmann 	}
3017611b30f7SMarcel Holtmann 
30185e130367SJohan Hedberg 	if (hdev->rfkill && rfkill_blocked(hdev->rfkill))
30195e130367SJohan Hedberg 		set_bit(HCI_RFKILLED, &hdev->dev_flags);
30205e130367SJohan Hedberg 
3021a8b2d5c2SJohan Hedberg 	set_bit(HCI_SETUP, &hdev->dev_flags);
3022004b0258SMarcel Holtmann 	set_bit(HCI_AUTO_OFF, &hdev->dev_flags);
3023ce2be9acSAndrei Emeltchenko 
302401cd3404SMarcel Holtmann 	if (hdev->dev_type == HCI_BREDR) {
302556f87901SJohan Hedberg 		/* Assume BR/EDR support until proven otherwise (such as
302656f87901SJohan Hedberg 		 * through reading supported features during init.
302756f87901SJohan Hedberg 		 */
302856f87901SJohan Hedberg 		set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags);
302956f87901SJohan Hedberg 	}
3030ce2be9acSAndrei Emeltchenko 
3031fcee3377SGustavo Padovan 	write_lock(&hci_dev_list_lock);
3032fcee3377SGustavo Padovan 	list_add(&hdev->list, &hci_dev_list);
3033fcee3377SGustavo Padovan 	write_unlock(&hci_dev_list_lock);
3034fcee3377SGustavo Padovan 
30354a964404SMarcel Holtmann 	/* Devices that are marked for raw-only usage are unconfigured
30364a964404SMarcel Holtmann 	 * and should not be included in normal operation.
3037fee746b0SMarcel Holtmann 	 */
3038fee746b0SMarcel Holtmann 	if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
30394a964404SMarcel Holtmann 		set_bit(HCI_UNCONFIGURED, &hdev->dev_flags);
3040fee746b0SMarcel Holtmann 
30411da177e4SLinus Torvalds 	hci_notify(hdev, HCI_DEV_REG);
3042dc946bd8SDavid Herrmann 	hci_dev_hold(hdev);
30431da177e4SLinus Torvalds 
304419202573SJohan Hedberg 	queue_work(hdev->req_workqueue, &hdev->power_on);
3045fbe96d6fSMarcel Holtmann 
30461da177e4SLinus Torvalds 	return id;
3047f48fd9c8SMarcel Holtmann 
304833ca954dSDavid Herrmann err_wqueue:
304933ca954dSDavid Herrmann 	destroy_workqueue(hdev->workqueue);
30506ead1bbcSJohan Hedberg 	destroy_workqueue(hdev->req_workqueue);
305133ca954dSDavid Herrmann err:
30523df92b31SSasha Levin 	ida_simple_remove(&hci_index_ida, hdev->id);
3053f48fd9c8SMarcel Holtmann 
305433ca954dSDavid Herrmann 	return error;
30551da177e4SLinus Torvalds }
30561da177e4SLinus Torvalds EXPORT_SYMBOL(hci_register_dev);
30571da177e4SLinus Torvalds 
30581da177e4SLinus Torvalds /* Unregister HCI device */
305959735631SDavid Herrmann void hci_unregister_dev(struct hci_dev *hdev)
30601da177e4SLinus Torvalds {
30613df92b31SSasha Levin 	int i, id;
3062ef222013SMarcel Holtmann 
3063c13854ceSMarcel Holtmann 	BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
30641da177e4SLinus Torvalds 
306594324962SJohan Hovold 	set_bit(HCI_UNREGISTER, &hdev->dev_flags);
306694324962SJohan Hovold 
30673df92b31SSasha Levin 	id = hdev->id;
30683df92b31SSasha Levin 
3069f20d09d5SGustavo F. Padovan 	write_lock(&hci_dev_list_lock);
30701da177e4SLinus Torvalds 	list_del(&hdev->list);
3071f20d09d5SGustavo F. Padovan 	write_unlock(&hci_dev_list_lock);
30721da177e4SLinus Torvalds 
30731da177e4SLinus Torvalds 	hci_dev_do_close(hdev);
30741da177e4SLinus Torvalds 
3075cd4c5391SSuraj Sumangala 	for (i = 0; i < NUM_REASSEMBLY; i++)
3076ef222013SMarcel Holtmann 		kfree_skb(hdev->reassembly[i]);
3077ef222013SMarcel Holtmann 
3078b9b5ef18SGustavo Padovan 	cancel_work_sync(&hdev->power_on);
3079b9b5ef18SGustavo Padovan 
3080ab81cbf9SJohan Hedberg 	if (!test_bit(HCI_INIT, &hdev->flags) &&
3081d603b76bSMarcel Holtmann 	    !test_bit(HCI_SETUP, &hdev->dev_flags) &&
3082d603b76bSMarcel Holtmann 	    !test_bit(HCI_CONFIG, &hdev->dev_flags)) {
308309fd0de5SGustavo F. Padovan 		hci_dev_lock(hdev);
3084744cf19eSJohan Hedberg 		mgmt_index_removed(hdev);
308509fd0de5SGustavo F. Padovan 		hci_dev_unlock(hdev);
308656e5cb86SJohan Hedberg 	}
3087ab81cbf9SJohan Hedberg 
30882e58ef3eSJohan Hedberg 	/* mgmt_index_removed should take care of emptying the
30892e58ef3eSJohan Hedberg 	 * pending list */
30902e58ef3eSJohan Hedberg 	BUG_ON(!list_empty(&hdev->mgmt_pending));
30912e58ef3eSJohan Hedberg 
30921da177e4SLinus Torvalds 	hci_notify(hdev, HCI_DEV_UNREG);
30931da177e4SLinus Torvalds 
3094611b30f7SMarcel Holtmann 	if (hdev->rfkill) {
3095611b30f7SMarcel Holtmann 		rfkill_unregister(hdev->rfkill);
3096611b30f7SMarcel Holtmann 		rfkill_destroy(hdev->rfkill);
3097611b30f7SMarcel Holtmann 	}
3098611b30f7SMarcel Holtmann 
3099711eafe3SJohan Hedberg 	smp_unregister(hdev);
310099780a7bSJohan Hedberg 
3101bdc3e0f1SMarcel Holtmann 	device_del(&hdev->dev);
3102147e2d59SDave Young 
31030153e2ecSMarcel Holtmann 	debugfs_remove_recursive(hdev->debugfs);
31040153e2ecSMarcel Holtmann 
3105f48fd9c8SMarcel Holtmann 	destroy_workqueue(hdev->workqueue);
31066ead1bbcSJohan Hedberg 	destroy_workqueue(hdev->req_workqueue);
3107f48fd9c8SMarcel Holtmann 
310809fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
3109dcc36c16SJohan Hedberg 	hci_bdaddr_list_clear(&hdev->blacklist);
31106659358eSJohan Hedberg 	hci_bdaddr_list_clear(&hdev->whitelist);
31112aeb9a1aSJohan Hedberg 	hci_uuids_clear(hdev);
311255ed8ca1SJohan Hedberg 	hci_link_keys_clear(hdev);
3113b899efafSVinicius Costa Gomes 	hci_smp_ltks_clear(hdev);
3114970c4e46SJohan Hedberg 	hci_smp_irks_clear(hdev);
31152763eda6SSzymon Janc 	hci_remote_oob_data_clear(hdev);
3116dcc36c16SJohan Hedberg 	hci_bdaddr_list_clear(&hdev->le_white_list);
3117373110c5SJohan Hedberg 	hci_conn_params_clear_all(hdev);
311822078800SMarcel Holtmann 	hci_discovery_filter_clear(hdev);
311909fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
3120e2e0cacbSJohan Hedberg 
3121dc946bd8SDavid Herrmann 	hci_dev_put(hdev);
31223df92b31SSasha Levin 
31233df92b31SSasha Levin 	ida_simple_remove(&hci_index_ida, id);
31241da177e4SLinus Torvalds }
31251da177e4SLinus Torvalds EXPORT_SYMBOL(hci_unregister_dev);
31261da177e4SLinus Torvalds 
31271da177e4SLinus Torvalds /* Suspend HCI device */
31281da177e4SLinus Torvalds int hci_suspend_dev(struct hci_dev *hdev)
31291da177e4SLinus Torvalds {
31301da177e4SLinus Torvalds 	hci_notify(hdev, HCI_DEV_SUSPEND);
31311da177e4SLinus Torvalds 	return 0;
31321da177e4SLinus Torvalds }
31331da177e4SLinus Torvalds EXPORT_SYMBOL(hci_suspend_dev);
31341da177e4SLinus Torvalds 
31351da177e4SLinus Torvalds /* Resume HCI device */
31361da177e4SLinus Torvalds int hci_resume_dev(struct hci_dev *hdev)
31371da177e4SLinus Torvalds {
31381da177e4SLinus Torvalds 	hci_notify(hdev, HCI_DEV_RESUME);
31391da177e4SLinus Torvalds 	return 0;
31401da177e4SLinus Torvalds }
31411da177e4SLinus Torvalds EXPORT_SYMBOL(hci_resume_dev);
31421da177e4SLinus Torvalds 
314375e0569fSMarcel Holtmann /* Reset HCI device */
314475e0569fSMarcel Holtmann int hci_reset_dev(struct hci_dev *hdev)
314575e0569fSMarcel Holtmann {
314675e0569fSMarcel Holtmann 	const u8 hw_err[] = { HCI_EV_HARDWARE_ERROR, 0x01, 0x00 };
314775e0569fSMarcel Holtmann 	struct sk_buff *skb;
314875e0569fSMarcel Holtmann 
314975e0569fSMarcel Holtmann 	skb = bt_skb_alloc(3, GFP_ATOMIC);
315075e0569fSMarcel Holtmann 	if (!skb)
315175e0569fSMarcel Holtmann 		return -ENOMEM;
315275e0569fSMarcel Holtmann 
315375e0569fSMarcel Holtmann 	bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
315475e0569fSMarcel Holtmann 	memcpy(skb_put(skb, 3), hw_err, 3);
315575e0569fSMarcel Holtmann 
315675e0569fSMarcel Holtmann 	/* Send Hardware Error to upper stack */
315775e0569fSMarcel Holtmann 	return hci_recv_frame(hdev, skb);
315875e0569fSMarcel Holtmann }
315975e0569fSMarcel Holtmann EXPORT_SYMBOL(hci_reset_dev);
316075e0569fSMarcel Holtmann 
316176bca880SMarcel Holtmann /* Receive frame from HCI drivers */
3162e1a26170SMarcel Holtmann int hci_recv_frame(struct hci_dev *hdev, struct sk_buff *skb)
316376bca880SMarcel Holtmann {
316476bca880SMarcel Holtmann 	if (!hdev || (!test_bit(HCI_UP, &hdev->flags)
316576bca880SMarcel Holtmann 		      && !test_bit(HCI_INIT, &hdev->flags))) {
316676bca880SMarcel Holtmann 		kfree_skb(skb);
316776bca880SMarcel Holtmann 		return -ENXIO;
316876bca880SMarcel Holtmann 	}
316976bca880SMarcel Holtmann 
3170d82603c6SJorrit Schippers 	/* Incoming skb */
317176bca880SMarcel Holtmann 	bt_cb(skb)->incoming = 1;
317276bca880SMarcel Holtmann 
317376bca880SMarcel Holtmann 	/* Time stamp */
317476bca880SMarcel Holtmann 	__net_timestamp(skb);
317576bca880SMarcel Holtmann 
317676bca880SMarcel Holtmann 	skb_queue_tail(&hdev->rx_q, skb);
3177b78752ccSMarcel Holtmann 	queue_work(hdev->workqueue, &hdev->rx_work);
3178c78ae283SMarcel Holtmann 
317976bca880SMarcel Holtmann 	return 0;
318076bca880SMarcel Holtmann }
318176bca880SMarcel Holtmann EXPORT_SYMBOL(hci_recv_frame);
318276bca880SMarcel Holtmann 
318333e882a5SSuraj Sumangala static int hci_reassembly(struct hci_dev *hdev, int type, void *data,
31841e429f38SGustavo F. Padovan 			  int count, __u8 index)
318533e882a5SSuraj Sumangala {
318633e882a5SSuraj Sumangala 	int len = 0;
318733e882a5SSuraj Sumangala 	int hlen = 0;
318833e882a5SSuraj Sumangala 	int remain = count;
318933e882a5SSuraj Sumangala 	struct sk_buff *skb;
319033e882a5SSuraj Sumangala 	struct bt_skb_cb *scb;
319133e882a5SSuraj Sumangala 
319233e882a5SSuraj Sumangala 	if ((type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) ||
319333e882a5SSuraj Sumangala 	    index >= NUM_REASSEMBLY)
319433e882a5SSuraj Sumangala 		return -EILSEQ;
319533e882a5SSuraj Sumangala 
319633e882a5SSuraj Sumangala 	skb = hdev->reassembly[index];
319733e882a5SSuraj Sumangala 
319833e882a5SSuraj Sumangala 	if (!skb) {
319933e882a5SSuraj Sumangala 		switch (type) {
320033e882a5SSuraj Sumangala 		case HCI_ACLDATA_PKT:
320133e882a5SSuraj Sumangala 			len = HCI_MAX_FRAME_SIZE;
320233e882a5SSuraj Sumangala 			hlen = HCI_ACL_HDR_SIZE;
320333e882a5SSuraj Sumangala 			break;
320433e882a5SSuraj Sumangala 		case HCI_EVENT_PKT:
320533e882a5SSuraj Sumangala 			len = HCI_MAX_EVENT_SIZE;
320633e882a5SSuraj Sumangala 			hlen = HCI_EVENT_HDR_SIZE;
320733e882a5SSuraj Sumangala 			break;
320833e882a5SSuraj Sumangala 		case HCI_SCODATA_PKT:
320933e882a5SSuraj Sumangala 			len = HCI_MAX_SCO_SIZE;
321033e882a5SSuraj Sumangala 			hlen = HCI_SCO_HDR_SIZE;
321133e882a5SSuraj Sumangala 			break;
321233e882a5SSuraj Sumangala 		}
321333e882a5SSuraj Sumangala 
32141e429f38SGustavo F. Padovan 		skb = bt_skb_alloc(len, GFP_ATOMIC);
321533e882a5SSuraj Sumangala 		if (!skb)
321633e882a5SSuraj Sumangala 			return -ENOMEM;
321733e882a5SSuraj Sumangala 
321833e882a5SSuraj Sumangala 		scb = (void *) skb->cb;
321933e882a5SSuraj Sumangala 		scb->expect = hlen;
322033e882a5SSuraj Sumangala 		scb->pkt_type = type;
322133e882a5SSuraj Sumangala 
322233e882a5SSuraj Sumangala 		hdev->reassembly[index] = skb;
322333e882a5SSuraj Sumangala 	}
322433e882a5SSuraj Sumangala 
322533e882a5SSuraj Sumangala 	while (count) {
322633e882a5SSuraj Sumangala 		scb = (void *) skb->cb;
322789bb46d0SDan Carpenter 		len = min_t(uint, scb->expect, count);
322833e882a5SSuraj Sumangala 
322933e882a5SSuraj Sumangala 		memcpy(skb_put(skb, len), data, len);
323033e882a5SSuraj Sumangala 
323133e882a5SSuraj Sumangala 		count -= len;
323233e882a5SSuraj Sumangala 		data += len;
323333e882a5SSuraj Sumangala 		scb->expect -= len;
323433e882a5SSuraj Sumangala 		remain = count;
323533e882a5SSuraj Sumangala 
323633e882a5SSuraj Sumangala 		switch (type) {
323733e882a5SSuraj Sumangala 		case HCI_EVENT_PKT:
323833e882a5SSuraj Sumangala 			if (skb->len == HCI_EVENT_HDR_SIZE) {
323933e882a5SSuraj Sumangala 				struct hci_event_hdr *h = hci_event_hdr(skb);
324033e882a5SSuraj Sumangala 				scb->expect = h->plen;
324133e882a5SSuraj Sumangala 
324233e882a5SSuraj Sumangala 				if (skb_tailroom(skb) < scb->expect) {
324333e882a5SSuraj Sumangala 					kfree_skb(skb);
324433e882a5SSuraj Sumangala 					hdev->reassembly[index] = NULL;
324533e882a5SSuraj Sumangala 					return -ENOMEM;
324633e882a5SSuraj Sumangala 				}
324733e882a5SSuraj Sumangala 			}
324833e882a5SSuraj Sumangala 			break;
324933e882a5SSuraj Sumangala 
325033e882a5SSuraj Sumangala 		case HCI_ACLDATA_PKT:
325133e882a5SSuraj Sumangala 			if (skb->len  == HCI_ACL_HDR_SIZE) {
325233e882a5SSuraj Sumangala 				struct hci_acl_hdr *h = hci_acl_hdr(skb);
325333e882a5SSuraj Sumangala 				scb->expect = __le16_to_cpu(h->dlen);
325433e882a5SSuraj Sumangala 
325533e882a5SSuraj Sumangala 				if (skb_tailroom(skb) < scb->expect) {
325633e882a5SSuraj Sumangala 					kfree_skb(skb);
325733e882a5SSuraj Sumangala 					hdev->reassembly[index] = NULL;
325833e882a5SSuraj Sumangala 					return -ENOMEM;
325933e882a5SSuraj Sumangala 				}
326033e882a5SSuraj Sumangala 			}
326133e882a5SSuraj Sumangala 			break;
326233e882a5SSuraj Sumangala 
326333e882a5SSuraj Sumangala 		case HCI_SCODATA_PKT:
326433e882a5SSuraj Sumangala 			if (skb->len == HCI_SCO_HDR_SIZE) {
326533e882a5SSuraj Sumangala 				struct hci_sco_hdr *h = hci_sco_hdr(skb);
326633e882a5SSuraj Sumangala 				scb->expect = h->dlen;
326733e882a5SSuraj Sumangala 
326833e882a5SSuraj Sumangala 				if (skb_tailroom(skb) < scb->expect) {
326933e882a5SSuraj Sumangala 					kfree_skb(skb);
327033e882a5SSuraj Sumangala 					hdev->reassembly[index] = NULL;
327133e882a5SSuraj Sumangala 					return -ENOMEM;
327233e882a5SSuraj Sumangala 				}
327333e882a5SSuraj Sumangala 			}
327433e882a5SSuraj Sumangala 			break;
327533e882a5SSuraj Sumangala 		}
327633e882a5SSuraj Sumangala 
327733e882a5SSuraj Sumangala 		if (scb->expect == 0) {
327833e882a5SSuraj Sumangala 			/* Complete frame */
327933e882a5SSuraj Sumangala 
328033e882a5SSuraj Sumangala 			bt_cb(skb)->pkt_type = type;
3281e1a26170SMarcel Holtmann 			hci_recv_frame(hdev, skb);
328233e882a5SSuraj Sumangala 
328333e882a5SSuraj Sumangala 			hdev->reassembly[index] = NULL;
328433e882a5SSuraj Sumangala 			return remain;
328533e882a5SSuraj Sumangala 		}
328633e882a5SSuraj Sumangala 	}
328733e882a5SSuraj Sumangala 
328833e882a5SSuraj Sumangala 	return remain;
328933e882a5SSuraj Sumangala }
329033e882a5SSuraj Sumangala 
329199811510SSuraj Sumangala #define STREAM_REASSEMBLY 0
329299811510SSuraj Sumangala 
329399811510SSuraj Sumangala int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count)
329499811510SSuraj Sumangala {
329599811510SSuraj Sumangala 	int type;
329699811510SSuraj Sumangala 	int rem = 0;
329799811510SSuraj Sumangala 
3298da5f6c37SGustavo F. Padovan 	while (count) {
329999811510SSuraj Sumangala 		struct sk_buff *skb = hdev->reassembly[STREAM_REASSEMBLY];
330099811510SSuraj Sumangala 
330199811510SSuraj Sumangala 		if (!skb) {
330299811510SSuraj Sumangala 			struct { char type; } *pkt;
330399811510SSuraj Sumangala 
330499811510SSuraj Sumangala 			/* Start of the frame */
330599811510SSuraj Sumangala 			pkt = data;
330699811510SSuraj Sumangala 			type = pkt->type;
330799811510SSuraj Sumangala 
330899811510SSuraj Sumangala 			data++;
330999811510SSuraj Sumangala 			count--;
331099811510SSuraj Sumangala 		} else
331199811510SSuraj Sumangala 			type = bt_cb(skb)->pkt_type;
331299811510SSuraj Sumangala 
33131e429f38SGustavo F. Padovan 		rem = hci_reassembly(hdev, type, data, count,
33141e429f38SGustavo F. Padovan 				     STREAM_REASSEMBLY);
331599811510SSuraj Sumangala 		if (rem < 0)
331699811510SSuraj Sumangala 			return rem;
331799811510SSuraj Sumangala 
331899811510SSuraj Sumangala 		data += (count - rem);
331999811510SSuraj Sumangala 		count = rem;
3320f81c6224SJoe Perches 	}
332199811510SSuraj Sumangala 
332299811510SSuraj Sumangala 	return rem;
332399811510SSuraj Sumangala }
332499811510SSuraj Sumangala EXPORT_SYMBOL(hci_recv_stream_fragment);
332599811510SSuraj Sumangala 
33261da177e4SLinus Torvalds /* ---- Interface to upper protocols ---- */
33271da177e4SLinus Torvalds 
33281da177e4SLinus Torvalds int hci_register_cb(struct hci_cb *cb)
33291da177e4SLinus Torvalds {
33301da177e4SLinus Torvalds 	BT_DBG("%p name %s", cb, cb->name);
33311da177e4SLinus Torvalds 
3332f20d09d5SGustavo F. Padovan 	write_lock(&hci_cb_list_lock);
33331da177e4SLinus Torvalds 	list_add(&cb->list, &hci_cb_list);
3334f20d09d5SGustavo F. Padovan 	write_unlock(&hci_cb_list_lock);
33351da177e4SLinus Torvalds 
33361da177e4SLinus Torvalds 	return 0;
33371da177e4SLinus Torvalds }
33381da177e4SLinus Torvalds EXPORT_SYMBOL(hci_register_cb);
33391da177e4SLinus Torvalds 
33401da177e4SLinus Torvalds int hci_unregister_cb(struct hci_cb *cb)
33411da177e4SLinus Torvalds {
33421da177e4SLinus Torvalds 	BT_DBG("%p name %s", cb, cb->name);
33431da177e4SLinus Torvalds 
3344f20d09d5SGustavo F. Padovan 	write_lock(&hci_cb_list_lock);
33451da177e4SLinus Torvalds 	list_del(&cb->list);
3346f20d09d5SGustavo F. Padovan 	write_unlock(&hci_cb_list_lock);
33471da177e4SLinus Torvalds 
33481da177e4SLinus Torvalds 	return 0;
33491da177e4SLinus Torvalds }
33501da177e4SLinus Torvalds EXPORT_SYMBOL(hci_unregister_cb);
33511da177e4SLinus Torvalds 
335251086991SMarcel Holtmann static void hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
33531da177e4SLinus Torvalds {
3354cdc52faaSMarcel Holtmann 	int err;
3355cdc52faaSMarcel Holtmann 
33560d48d939SMarcel Holtmann 	BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
33571da177e4SLinus Torvalds 
33581da177e4SLinus Torvalds 	/* Time stamp */
3359a61bbcf2SPatrick McHardy 	__net_timestamp(skb);
33601da177e4SLinus Torvalds 
3361cd82e61cSMarcel Holtmann 	/* Send copy to monitor */
3362cd82e61cSMarcel Holtmann 	hci_send_to_monitor(hdev, skb);
3363cd82e61cSMarcel Holtmann 
3364cd82e61cSMarcel Holtmann 	if (atomic_read(&hdev->promisc)) {
3365cd82e61cSMarcel Holtmann 		/* Send copy to the sockets */
3366470fe1b5SMarcel Holtmann 		hci_send_to_sock(hdev, skb);
33671da177e4SLinus Torvalds 	}
33681da177e4SLinus Torvalds 
33691da177e4SLinus Torvalds 	/* Get rid of skb owner, prior to sending to the driver. */
33701da177e4SLinus Torvalds 	skb_orphan(skb);
33711da177e4SLinus Torvalds 
3372cdc52faaSMarcel Holtmann 	err = hdev->send(hdev, skb);
3373cdc52faaSMarcel Holtmann 	if (err < 0) {
3374cdc52faaSMarcel Holtmann 		BT_ERR("%s sending frame failed (%d)", hdev->name, err);
3375cdc52faaSMarcel Holtmann 		kfree_skb(skb);
3376cdc52faaSMarcel Holtmann 	}
33771da177e4SLinus Torvalds }
33781da177e4SLinus Torvalds 
3379899de765SMarcel Holtmann bool hci_req_pending(struct hci_dev *hdev)
3380899de765SMarcel Holtmann {
3381899de765SMarcel Holtmann 	return (hdev->req_status == HCI_REQ_PEND);
3382899de765SMarcel Holtmann }
3383899de765SMarcel Holtmann 
33841ca3a9d0SJohan Hedberg /* Send HCI command */
338507dc93ddSJohan Hedberg int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen,
338607dc93ddSJohan Hedberg 		 const void *param)
33871ca3a9d0SJohan Hedberg {
33881ca3a9d0SJohan Hedberg 	struct sk_buff *skb;
33891ca3a9d0SJohan Hedberg 
33901ca3a9d0SJohan Hedberg 	BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen);
33911ca3a9d0SJohan Hedberg 
33921ca3a9d0SJohan Hedberg 	skb = hci_prepare_cmd(hdev, opcode, plen, param);
33931ca3a9d0SJohan Hedberg 	if (!skb) {
33941ca3a9d0SJohan Hedberg 		BT_ERR("%s no memory for command", hdev->name);
33951ca3a9d0SJohan Hedberg 		return -ENOMEM;
33961ca3a9d0SJohan Hedberg 	}
33971ca3a9d0SJohan Hedberg 
339849c922bbSStephen Hemminger 	/* Stand-alone HCI commands must be flagged as
339911714b3dSJohan Hedberg 	 * single-command requests.
340011714b3dSJohan Hedberg 	 */
340111714b3dSJohan Hedberg 	bt_cb(skb)->req.start = true;
340211714b3dSJohan Hedberg 
34031da177e4SLinus Torvalds 	skb_queue_tail(&hdev->cmd_q, skb);
3404c347b765SGustavo F. Padovan 	queue_work(hdev->workqueue, &hdev->cmd_work);
34051da177e4SLinus Torvalds 
34061da177e4SLinus Torvalds 	return 0;
34071da177e4SLinus Torvalds }
34081da177e4SLinus Torvalds 
34091da177e4SLinus Torvalds /* Get data from the previously sent command */
3410a9de9248SMarcel Holtmann void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode)
34111da177e4SLinus Torvalds {
34121da177e4SLinus Torvalds 	struct hci_command_hdr *hdr;
34131da177e4SLinus Torvalds 
34141da177e4SLinus Torvalds 	if (!hdev->sent_cmd)
34151da177e4SLinus Torvalds 		return NULL;
34161da177e4SLinus Torvalds 
34171da177e4SLinus Torvalds 	hdr = (void *) hdev->sent_cmd->data;
34181da177e4SLinus Torvalds 
3419a9de9248SMarcel Holtmann 	if (hdr->opcode != cpu_to_le16(opcode))
34201da177e4SLinus Torvalds 		return NULL;
34211da177e4SLinus Torvalds 
3422f0e09510SAndrei Emeltchenko 	BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode);
34231da177e4SLinus Torvalds 
34241da177e4SLinus Torvalds 	return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE;
34251da177e4SLinus Torvalds }
34261da177e4SLinus Torvalds 
34271da177e4SLinus Torvalds /* Send ACL data */
34281da177e4SLinus Torvalds static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags)
34291da177e4SLinus Torvalds {
34301da177e4SLinus Torvalds 	struct hci_acl_hdr *hdr;
34311da177e4SLinus Torvalds 	int len = skb->len;
34321da177e4SLinus Torvalds 
3433badff6d0SArnaldo Carvalho de Melo 	skb_push(skb, HCI_ACL_HDR_SIZE);
3434badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
34359c70220bSArnaldo Carvalho de Melo 	hdr = (struct hci_acl_hdr *)skb_transport_header(skb);
3436aca3192cSYOSHIFUJI Hideaki 	hdr->handle = cpu_to_le16(hci_handle_pack(handle, flags));
3437aca3192cSYOSHIFUJI Hideaki 	hdr->dlen   = cpu_to_le16(len);
34381da177e4SLinus Torvalds }
34391da177e4SLinus Torvalds 
3440ee22be7eSAndrei Emeltchenko static void hci_queue_acl(struct hci_chan *chan, struct sk_buff_head *queue,
344173d80debSLuiz Augusto von Dentz 			  struct sk_buff *skb, __u16 flags)
34421da177e4SLinus Torvalds {
3443ee22be7eSAndrei Emeltchenko 	struct hci_conn *conn = chan->conn;
34441da177e4SLinus Torvalds 	struct hci_dev *hdev = conn->hdev;
34451da177e4SLinus Torvalds 	struct sk_buff *list;
34461da177e4SLinus Torvalds 
3447087bfd99SGustavo Padovan 	skb->len = skb_headlen(skb);
3448087bfd99SGustavo Padovan 	skb->data_len = 0;
3449087bfd99SGustavo Padovan 
3450087bfd99SGustavo Padovan 	bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
3451204a6e54SAndrei Emeltchenko 
3452204a6e54SAndrei Emeltchenko 	switch (hdev->dev_type) {
3453204a6e54SAndrei Emeltchenko 	case HCI_BREDR:
3454087bfd99SGustavo Padovan 		hci_add_acl_hdr(skb, conn->handle, flags);
3455204a6e54SAndrei Emeltchenko 		break;
3456204a6e54SAndrei Emeltchenko 	case HCI_AMP:
3457204a6e54SAndrei Emeltchenko 		hci_add_acl_hdr(skb, chan->handle, flags);
3458204a6e54SAndrei Emeltchenko 		break;
3459204a6e54SAndrei Emeltchenko 	default:
3460204a6e54SAndrei Emeltchenko 		BT_ERR("%s unknown dev_type %d", hdev->name, hdev->dev_type);
3461204a6e54SAndrei Emeltchenko 		return;
3462204a6e54SAndrei Emeltchenko 	}
3463087bfd99SGustavo Padovan 
346470f23020SAndrei Emeltchenko 	list = skb_shinfo(skb)->frag_list;
346570f23020SAndrei Emeltchenko 	if (!list) {
34661da177e4SLinus Torvalds 		/* Non fragmented */
34671da177e4SLinus Torvalds 		BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len);
34681da177e4SLinus Torvalds 
346973d80debSLuiz Augusto von Dentz 		skb_queue_tail(queue, skb);
34701da177e4SLinus Torvalds 	} else {
34711da177e4SLinus Torvalds 		/* Fragmented */
34721da177e4SLinus Torvalds 		BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
34731da177e4SLinus Torvalds 
34741da177e4SLinus Torvalds 		skb_shinfo(skb)->frag_list = NULL;
34751da177e4SLinus Torvalds 
34769cfd5a23SJukka Rissanen 		/* Queue all fragments atomically. We need to use spin_lock_bh
34779cfd5a23SJukka Rissanen 		 * here because of 6LoWPAN links, as there this function is
34789cfd5a23SJukka Rissanen 		 * called from softirq and using normal spin lock could cause
34799cfd5a23SJukka Rissanen 		 * deadlocks.
34809cfd5a23SJukka Rissanen 		 */
34819cfd5a23SJukka Rissanen 		spin_lock_bh(&queue->lock);
34821da177e4SLinus Torvalds 
348373d80debSLuiz Augusto von Dentz 		__skb_queue_tail(queue, skb);
3484e702112fSAndrei Emeltchenko 
3485e702112fSAndrei Emeltchenko 		flags &= ~ACL_START;
3486e702112fSAndrei Emeltchenko 		flags |= ACL_CONT;
34871da177e4SLinus Torvalds 		do {
34881da177e4SLinus Torvalds 			skb = list; list = list->next;
34891da177e4SLinus Torvalds 
34900d48d939SMarcel Holtmann 			bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
3491e702112fSAndrei Emeltchenko 			hci_add_acl_hdr(skb, conn->handle, flags);
34921da177e4SLinus Torvalds 
34931da177e4SLinus Torvalds 			BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
34941da177e4SLinus Torvalds 
349573d80debSLuiz Augusto von Dentz 			__skb_queue_tail(queue, skb);
34961da177e4SLinus Torvalds 		} while (list);
34971da177e4SLinus Torvalds 
34989cfd5a23SJukka Rissanen 		spin_unlock_bh(&queue->lock);
34991da177e4SLinus Torvalds 	}
350073d80debSLuiz Augusto von Dentz }
350173d80debSLuiz Augusto von Dentz 
350273d80debSLuiz Augusto von Dentz void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags)
350373d80debSLuiz Augusto von Dentz {
3504ee22be7eSAndrei Emeltchenko 	struct hci_dev *hdev = chan->conn->hdev;
350573d80debSLuiz Augusto von Dentz 
3506f0e09510SAndrei Emeltchenko 	BT_DBG("%s chan %p flags 0x%4.4x", hdev->name, chan, flags);
350773d80debSLuiz Augusto von Dentz 
3508ee22be7eSAndrei Emeltchenko 	hci_queue_acl(chan, &chan->data_q, skb, flags);
35091da177e4SLinus Torvalds 
35103eff45eaSGustavo F. Padovan 	queue_work(hdev->workqueue, &hdev->tx_work);
35111da177e4SLinus Torvalds }
35121da177e4SLinus Torvalds 
35131da177e4SLinus Torvalds /* Send SCO data */
35140d861d8bSGustavo F. Padovan void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb)
35151da177e4SLinus Torvalds {
35161da177e4SLinus Torvalds 	struct hci_dev *hdev = conn->hdev;
35171da177e4SLinus Torvalds 	struct hci_sco_hdr hdr;
35181da177e4SLinus Torvalds 
35191da177e4SLinus Torvalds 	BT_DBG("%s len %d", hdev->name, skb->len);
35201da177e4SLinus Torvalds 
3521aca3192cSYOSHIFUJI Hideaki 	hdr.handle = cpu_to_le16(conn->handle);
35221da177e4SLinus Torvalds 	hdr.dlen   = skb->len;
35231da177e4SLinus Torvalds 
3524badff6d0SArnaldo Carvalho de Melo 	skb_push(skb, HCI_SCO_HDR_SIZE);
3525badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
35269c70220bSArnaldo Carvalho de Melo 	memcpy(skb_transport_header(skb), &hdr, HCI_SCO_HDR_SIZE);
35271da177e4SLinus Torvalds 
35280d48d939SMarcel Holtmann 	bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
3529c78ae283SMarcel Holtmann 
35301da177e4SLinus Torvalds 	skb_queue_tail(&conn->data_q, skb);
35313eff45eaSGustavo F. Padovan 	queue_work(hdev->workqueue, &hdev->tx_work);
35321da177e4SLinus Torvalds }
35331da177e4SLinus Torvalds 
35341da177e4SLinus Torvalds /* ---- HCI TX task (outgoing data) ---- */
35351da177e4SLinus Torvalds 
35361da177e4SLinus Torvalds /* HCI Connection scheduler */
35376039aa73SGustavo Padovan static struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type,
3538a8c5fb1aSGustavo Padovan 				     int *quote)
35391da177e4SLinus Torvalds {
35401da177e4SLinus Torvalds 	struct hci_conn_hash *h = &hdev->conn_hash;
35418035ded4SLuiz Augusto von Dentz 	struct hci_conn *conn = NULL, *c;
3542abc5de8fSMikel Astiz 	unsigned int num = 0, min = ~0;
35431da177e4SLinus Torvalds 
35441da177e4SLinus Torvalds 	/* We don't have to lock device here. Connections are always
35451da177e4SLinus Torvalds 	 * added and removed with TX task disabled. */
3546bf4c6325SGustavo F. Padovan 
3547bf4c6325SGustavo F. Padovan 	rcu_read_lock();
3548bf4c6325SGustavo F. Padovan 
3549bf4c6325SGustavo F. Padovan 	list_for_each_entry_rcu(c, &h->list, list) {
3550769be974SMarcel Holtmann 		if (c->type != type || skb_queue_empty(&c->data_q))
35511da177e4SLinus Torvalds 			continue;
3552769be974SMarcel Holtmann 
3553769be974SMarcel Holtmann 		if (c->state != BT_CONNECTED && c->state != BT_CONFIG)
3554769be974SMarcel Holtmann 			continue;
3555769be974SMarcel Holtmann 
35561da177e4SLinus Torvalds 		num++;
35571da177e4SLinus Torvalds 
35581da177e4SLinus Torvalds 		if (c->sent < min) {
35591da177e4SLinus Torvalds 			min  = c->sent;
35601da177e4SLinus Torvalds 			conn = c;
35611da177e4SLinus Torvalds 		}
356252087a79SLuiz Augusto von Dentz 
356352087a79SLuiz Augusto von Dentz 		if (hci_conn_num(hdev, type) == num)
356452087a79SLuiz Augusto von Dentz 			break;
35651da177e4SLinus Torvalds 	}
35661da177e4SLinus Torvalds 
3567bf4c6325SGustavo F. Padovan 	rcu_read_unlock();
3568bf4c6325SGustavo F. Padovan 
35691da177e4SLinus Torvalds 	if (conn) {
35706ed58ec5SVille Tervo 		int cnt, q;
35716ed58ec5SVille Tervo 
35726ed58ec5SVille Tervo 		switch (conn->type) {
35736ed58ec5SVille Tervo 		case ACL_LINK:
35746ed58ec5SVille Tervo 			cnt = hdev->acl_cnt;
35756ed58ec5SVille Tervo 			break;
35766ed58ec5SVille Tervo 		case SCO_LINK:
35776ed58ec5SVille Tervo 		case ESCO_LINK:
35786ed58ec5SVille Tervo 			cnt = hdev->sco_cnt;
35796ed58ec5SVille Tervo 			break;
35806ed58ec5SVille Tervo 		case LE_LINK:
35816ed58ec5SVille Tervo 			cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt;
35826ed58ec5SVille Tervo 			break;
35836ed58ec5SVille Tervo 		default:
35846ed58ec5SVille Tervo 			cnt = 0;
35856ed58ec5SVille Tervo 			BT_ERR("Unknown link type");
35866ed58ec5SVille Tervo 		}
35876ed58ec5SVille Tervo 
35886ed58ec5SVille Tervo 		q = cnt / num;
35891da177e4SLinus Torvalds 		*quote = q ? q : 1;
35901da177e4SLinus Torvalds 	} else
35911da177e4SLinus Torvalds 		*quote = 0;
35921da177e4SLinus Torvalds 
35931da177e4SLinus Torvalds 	BT_DBG("conn %p quote %d", conn, *quote);
35941da177e4SLinus Torvalds 	return conn;
35951da177e4SLinus Torvalds }
35961da177e4SLinus Torvalds 
35976039aa73SGustavo Padovan static void hci_link_tx_to(struct hci_dev *hdev, __u8 type)
35981da177e4SLinus Torvalds {
35991da177e4SLinus Torvalds 	struct hci_conn_hash *h = &hdev->conn_hash;
36001da177e4SLinus Torvalds 	struct hci_conn *c;
36011da177e4SLinus Torvalds 
3602bae1f5d9SVille Tervo 	BT_ERR("%s link tx timeout", hdev->name);
36031da177e4SLinus Torvalds 
3604bf4c6325SGustavo F. Padovan 	rcu_read_lock();
3605bf4c6325SGustavo F. Padovan 
36061da177e4SLinus Torvalds 	/* Kill stalled connections */
3607bf4c6325SGustavo F. Padovan 	list_for_each_entry_rcu(c, &h->list, list) {
3608bae1f5d9SVille Tervo 		if (c->type == type && c->sent) {
36096ed93dc6SAndrei Emeltchenko 			BT_ERR("%s killing stalled connection %pMR",
36106ed93dc6SAndrei Emeltchenko 			       hdev->name, &c->dst);
3611bed71748SAndre Guedes 			hci_disconnect(c, HCI_ERROR_REMOTE_USER_TERM);
36121da177e4SLinus Torvalds 		}
36131da177e4SLinus Torvalds 	}
3614bf4c6325SGustavo F. Padovan 
3615bf4c6325SGustavo F. Padovan 	rcu_read_unlock();
36161da177e4SLinus Torvalds }
36171da177e4SLinus Torvalds 
36186039aa73SGustavo Padovan static struct hci_chan *hci_chan_sent(struct hci_dev *hdev, __u8 type,
361973d80debSLuiz Augusto von Dentz 				      int *quote)
362073d80debSLuiz Augusto von Dentz {
362173d80debSLuiz Augusto von Dentz 	struct hci_conn_hash *h = &hdev->conn_hash;
362273d80debSLuiz Augusto von Dentz 	struct hci_chan *chan = NULL;
3623abc5de8fSMikel Astiz 	unsigned int num = 0, min = ~0, cur_prio = 0;
362473d80debSLuiz Augusto von Dentz 	struct hci_conn *conn;
362573d80debSLuiz Augusto von Dentz 	int cnt, q, conn_num = 0;
362673d80debSLuiz Augusto von Dentz 
362773d80debSLuiz Augusto von Dentz 	BT_DBG("%s", hdev->name);
362873d80debSLuiz Augusto von Dentz 
3629bf4c6325SGustavo F. Padovan 	rcu_read_lock();
3630bf4c6325SGustavo F. Padovan 
3631bf4c6325SGustavo F. Padovan 	list_for_each_entry_rcu(conn, &h->list, list) {
363273d80debSLuiz Augusto von Dentz 		struct hci_chan *tmp;
363373d80debSLuiz Augusto von Dentz 
363473d80debSLuiz Augusto von Dentz 		if (conn->type != type)
363573d80debSLuiz Augusto von Dentz 			continue;
363673d80debSLuiz Augusto von Dentz 
363773d80debSLuiz Augusto von Dentz 		if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
363873d80debSLuiz Augusto von Dentz 			continue;
363973d80debSLuiz Augusto von Dentz 
364073d80debSLuiz Augusto von Dentz 		conn_num++;
364173d80debSLuiz Augusto von Dentz 
36428192edefSGustavo F. Padovan 		list_for_each_entry_rcu(tmp, &conn->chan_list, list) {
364373d80debSLuiz Augusto von Dentz 			struct sk_buff *skb;
364473d80debSLuiz Augusto von Dentz 
364573d80debSLuiz Augusto von Dentz 			if (skb_queue_empty(&tmp->data_q))
364673d80debSLuiz Augusto von Dentz 				continue;
364773d80debSLuiz Augusto von Dentz 
364873d80debSLuiz Augusto von Dentz 			skb = skb_peek(&tmp->data_q);
364973d80debSLuiz Augusto von Dentz 			if (skb->priority < cur_prio)
365073d80debSLuiz Augusto von Dentz 				continue;
365173d80debSLuiz Augusto von Dentz 
365273d80debSLuiz Augusto von Dentz 			if (skb->priority > cur_prio) {
365373d80debSLuiz Augusto von Dentz 				num = 0;
365473d80debSLuiz Augusto von Dentz 				min = ~0;
365573d80debSLuiz Augusto von Dentz 				cur_prio = skb->priority;
365673d80debSLuiz Augusto von Dentz 			}
365773d80debSLuiz Augusto von Dentz 
365873d80debSLuiz Augusto von Dentz 			num++;
365973d80debSLuiz Augusto von Dentz 
366073d80debSLuiz Augusto von Dentz 			if (conn->sent < min) {
366173d80debSLuiz Augusto von Dentz 				min  = conn->sent;
366273d80debSLuiz Augusto von Dentz 				chan = tmp;
366373d80debSLuiz Augusto von Dentz 			}
366473d80debSLuiz Augusto von Dentz 		}
366573d80debSLuiz Augusto von Dentz 
366673d80debSLuiz Augusto von Dentz 		if (hci_conn_num(hdev, type) == conn_num)
366773d80debSLuiz Augusto von Dentz 			break;
366873d80debSLuiz Augusto von Dentz 	}
366973d80debSLuiz Augusto von Dentz 
3670bf4c6325SGustavo F. Padovan 	rcu_read_unlock();
3671bf4c6325SGustavo F. Padovan 
367273d80debSLuiz Augusto von Dentz 	if (!chan)
367373d80debSLuiz Augusto von Dentz 		return NULL;
367473d80debSLuiz Augusto von Dentz 
367573d80debSLuiz Augusto von Dentz 	switch (chan->conn->type) {
367673d80debSLuiz Augusto von Dentz 	case ACL_LINK:
367773d80debSLuiz Augusto von Dentz 		cnt = hdev->acl_cnt;
367873d80debSLuiz Augusto von Dentz 		break;
3679bd1eb66bSAndrei Emeltchenko 	case AMP_LINK:
3680bd1eb66bSAndrei Emeltchenko 		cnt = hdev->block_cnt;
3681bd1eb66bSAndrei Emeltchenko 		break;
368273d80debSLuiz Augusto von Dentz 	case SCO_LINK:
368373d80debSLuiz Augusto von Dentz 	case ESCO_LINK:
368473d80debSLuiz Augusto von Dentz 		cnt = hdev->sco_cnt;
368573d80debSLuiz Augusto von Dentz 		break;
368673d80debSLuiz Augusto von Dentz 	case LE_LINK:
368773d80debSLuiz Augusto von Dentz 		cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt;
368873d80debSLuiz Augusto von Dentz 		break;
368973d80debSLuiz Augusto von Dentz 	default:
369073d80debSLuiz Augusto von Dentz 		cnt = 0;
369173d80debSLuiz Augusto von Dentz 		BT_ERR("Unknown link type");
369273d80debSLuiz Augusto von Dentz 	}
369373d80debSLuiz Augusto von Dentz 
369473d80debSLuiz Augusto von Dentz 	q = cnt / num;
369573d80debSLuiz Augusto von Dentz 	*quote = q ? q : 1;
369673d80debSLuiz Augusto von Dentz 	BT_DBG("chan %p quote %d", chan, *quote);
369773d80debSLuiz Augusto von Dentz 	return chan;
369873d80debSLuiz Augusto von Dentz }
369973d80debSLuiz Augusto von Dentz 
370002b20f0bSLuiz Augusto von Dentz static void hci_prio_recalculate(struct hci_dev *hdev, __u8 type)
370102b20f0bSLuiz Augusto von Dentz {
370202b20f0bSLuiz Augusto von Dentz 	struct hci_conn_hash *h = &hdev->conn_hash;
370302b20f0bSLuiz Augusto von Dentz 	struct hci_conn *conn;
370402b20f0bSLuiz Augusto von Dentz 	int num = 0;
370502b20f0bSLuiz Augusto von Dentz 
370602b20f0bSLuiz Augusto von Dentz 	BT_DBG("%s", hdev->name);
370702b20f0bSLuiz Augusto von Dentz 
3708bf4c6325SGustavo F. Padovan 	rcu_read_lock();
3709bf4c6325SGustavo F. Padovan 
3710bf4c6325SGustavo F. Padovan 	list_for_each_entry_rcu(conn, &h->list, list) {
371102b20f0bSLuiz Augusto von Dentz 		struct hci_chan *chan;
371202b20f0bSLuiz Augusto von Dentz 
371302b20f0bSLuiz Augusto von Dentz 		if (conn->type != type)
371402b20f0bSLuiz Augusto von Dentz 			continue;
371502b20f0bSLuiz Augusto von Dentz 
371602b20f0bSLuiz Augusto von Dentz 		if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
371702b20f0bSLuiz Augusto von Dentz 			continue;
371802b20f0bSLuiz Augusto von Dentz 
371902b20f0bSLuiz Augusto von Dentz 		num++;
372002b20f0bSLuiz Augusto von Dentz 
37218192edefSGustavo F. Padovan 		list_for_each_entry_rcu(chan, &conn->chan_list, list) {
372202b20f0bSLuiz Augusto von Dentz 			struct sk_buff *skb;
372302b20f0bSLuiz Augusto von Dentz 
372402b20f0bSLuiz Augusto von Dentz 			if (chan->sent) {
372502b20f0bSLuiz Augusto von Dentz 				chan->sent = 0;
372602b20f0bSLuiz Augusto von Dentz 				continue;
372702b20f0bSLuiz Augusto von Dentz 			}
372802b20f0bSLuiz Augusto von Dentz 
372902b20f0bSLuiz Augusto von Dentz 			if (skb_queue_empty(&chan->data_q))
373002b20f0bSLuiz Augusto von Dentz 				continue;
373102b20f0bSLuiz Augusto von Dentz 
373202b20f0bSLuiz Augusto von Dentz 			skb = skb_peek(&chan->data_q);
373302b20f0bSLuiz Augusto von Dentz 			if (skb->priority >= HCI_PRIO_MAX - 1)
373402b20f0bSLuiz Augusto von Dentz 				continue;
373502b20f0bSLuiz Augusto von Dentz 
373602b20f0bSLuiz Augusto von Dentz 			skb->priority = HCI_PRIO_MAX - 1;
373702b20f0bSLuiz Augusto von Dentz 
373802b20f0bSLuiz Augusto von Dentz 			BT_DBG("chan %p skb %p promoted to %d", chan, skb,
373902b20f0bSLuiz Augusto von Dentz 			       skb->priority);
374002b20f0bSLuiz Augusto von Dentz 		}
374102b20f0bSLuiz Augusto von Dentz 
374202b20f0bSLuiz Augusto von Dentz 		if (hci_conn_num(hdev, type) == num)
374302b20f0bSLuiz Augusto von Dentz 			break;
374402b20f0bSLuiz Augusto von Dentz 	}
3745bf4c6325SGustavo F. Padovan 
3746bf4c6325SGustavo F. Padovan 	rcu_read_unlock();
3747bf4c6325SGustavo F. Padovan 
374802b20f0bSLuiz Augusto von Dentz }
374902b20f0bSLuiz Augusto von Dentz 
3750b71d385aSAndrei Emeltchenko static inline int __get_blocks(struct hci_dev *hdev, struct sk_buff *skb)
3751b71d385aSAndrei Emeltchenko {
3752b71d385aSAndrei Emeltchenko 	/* Calculate count of blocks used by this packet */
3753b71d385aSAndrei Emeltchenko 	return DIV_ROUND_UP(skb->len - HCI_ACL_HDR_SIZE, hdev->block_len);
3754b71d385aSAndrei Emeltchenko }
3755b71d385aSAndrei Emeltchenko 
37566039aa73SGustavo Padovan static void __check_timeout(struct hci_dev *hdev, unsigned int cnt)
37571da177e4SLinus Torvalds {
37584a964404SMarcel Holtmann 	if (!test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) {
37591da177e4SLinus Torvalds 		/* ACL tx timeout must be longer than maximum
37601da177e4SLinus Torvalds 		 * link supervision timeout (40.9 seconds) */
376163d2bc1bSAndrei Emeltchenko 		if (!cnt && time_after(jiffies, hdev->acl_last_tx +
37625f246e89SAndrei Emeltchenko 				       HCI_ACL_TX_TIMEOUT))
3763bae1f5d9SVille Tervo 			hci_link_tx_to(hdev, ACL_LINK);
37641da177e4SLinus Torvalds 	}
376563d2bc1bSAndrei Emeltchenko }
37661da177e4SLinus Torvalds 
37676039aa73SGustavo Padovan static void hci_sched_acl_pkt(struct hci_dev *hdev)
376863d2bc1bSAndrei Emeltchenko {
376963d2bc1bSAndrei Emeltchenko 	unsigned int cnt = hdev->acl_cnt;
377063d2bc1bSAndrei Emeltchenko 	struct hci_chan *chan;
377163d2bc1bSAndrei Emeltchenko 	struct sk_buff *skb;
377263d2bc1bSAndrei Emeltchenko 	int quote;
377363d2bc1bSAndrei Emeltchenko 
377463d2bc1bSAndrei Emeltchenko 	__check_timeout(hdev, cnt);
377504837f64SMarcel Holtmann 
377673d80debSLuiz Augusto von Dentz 	while (hdev->acl_cnt &&
377773d80debSLuiz Augusto von Dentz 	       (chan = hci_chan_sent(hdev, ACL_LINK, &quote))) {
3778ec1cce24SLuiz Augusto von Dentz 		u32 priority = (skb_peek(&chan->data_q))->priority;
3779ec1cce24SLuiz Augusto von Dentz 		while (quote-- && (skb = skb_peek(&chan->data_q))) {
378073d80debSLuiz Augusto von Dentz 			BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
378173d80debSLuiz Augusto von Dentz 			       skb->len, skb->priority);
378273d80debSLuiz Augusto von Dentz 
3783ec1cce24SLuiz Augusto von Dentz 			/* Stop if priority has changed */
3784ec1cce24SLuiz Augusto von Dentz 			if (skb->priority < priority)
3785ec1cce24SLuiz Augusto von Dentz 				break;
3786ec1cce24SLuiz Augusto von Dentz 
3787ec1cce24SLuiz Augusto von Dentz 			skb = skb_dequeue(&chan->data_q);
3788ec1cce24SLuiz Augusto von Dentz 
378973d80debSLuiz Augusto von Dentz 			hci_conn_enter_active_mode(chan->conn,
379073d80debSLuiz Augusto von Dentz 						   bt_cb(skb)->force_active);
379104837f64SMarcel Holtmann 
379257d17d70SMarcel Holtmann 			hci_send_frame(hdev, skb);
37931da177e4SLinus Torvalds 			hdev->acl_last_tx = jiffies;
37941da177e4SLinus Torvalds 
37951da177e4SLinus Torvalds 			hdev->acl_cnt--;
379673d80debSLuiz Augusto von Dentz 			chan->sent++;
379773d80debSLuiz Augusto von Dentz 			chan->conn->sent++;
37981da177e4SLinus Torvalds 		}
37991da177e4SLinus Torvalds 	}
380002b20f0bSLuiz Augusto von Dentz 
380102b20f0bSLuiz Augusto von Dentz 	if (cnt != hdev->acl_cnt)
380202b20f0bSLuiz Augusto von Dentz 		hci_prio_recalculate(hdev, ACL_LINK);
38031da177e4SLinus Torvalds }
38041da177e4SLinus Torvalds 
38056039aa73SGustavo Padovan static void hci_sched_acl_blk(struct hci_dev *hdev)
3806b71d385aSAndrei Emeltchenko {
380763d2bc1bSAndrei Emeltchenko 	unsigned int cnt = hdev->block_cnt;
3808b71d385aSAndrei Emeltchenko 	struct hci_chan *chan;
3809b71d385aSAndrei Emeltchenko 	struct sk_buff *skb;
3810b71d385aSAndrei Emeltchenko 	int quote;
3811bd1eb66bSAndrei Emeltchenko 	u8 type;
3812b71d385aSAndrei Emeltchenko 
381363d2bc1bSAndrei Emeltchenko 	__check_timeout(hdev, cnt);
3814b71d385aSAndrei Emeltchenko 
3815bd1eb66bSAndrei Emeltchenko 	BT_DBG("%s", hdev->name);
3816bd1eb66bSAndrei Emeltchenko 
3817bd1eb66bSAndrei Emeltchenko 	if (hdev->dev_type == HCI_AMP)
3818bd1eb66bSAndrei Emeltchenko 		type = AMP_LINK;
3819bd1eb66bSAndrei Emeltchenko 	else
3820bd1eb66bSAndrei Emeltchenko 		type = ACL_LINK;
3821bd1eb66bSAndrei Emeltchenko 
3822b71d385aSAndrei Emeltchenko 	while (hdev->block_cnt > 0 &&
3823bd1eb66bSAndrei Emeltchenko 	       (chan = hci_chan_sent(hdev, type, &quote))) {
3824b71d385aSAndrei Emeltchenko 		u32 priority = (skb_peek(&chan->data_q))->priority;
3825b71d385aSAndrei Emeltchenko 		while (quote > 0 && (skb = skb_peek(&chan->data_q))) {
3826b71d385aSAndrei Emeltchenko 			int blocks;
3827b71d385aSAndrei Emeltchenko 
3828b71d385aSAndrei Emeltchenko 			BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
3829b71d385aSAndrei Emeltchenko 			       skb->len, skb->priority);
3830b71d385aSAndrei Emeltchenko 
3831b71d385aSAndrei Emeltchenko 			/* Stop if priority has changed */
3832b71d385aSAndrei Emeltchenko 			if (skb->priority < priority)
3833b71d385aSAndrei Emeltchenko 				break;
3834b71d385aSAndrei Emeltchenko 
3835b71d385aSAndrei Emeltchenko 			skb = skb_dequeue(&chan->data_q);
3836b71d385aSAndrei Emeltchenko 
3837b71d385aSAndrei Emeltchenko 			blocks = __get_blocks(hdev, skb);
3838b71d385aSAndrei Emeltchenko 			if (blocks > hdev->block_cnt)
3839b71d385aSAndrei Emeltchenko 				return;
3840b71d385aSAndrei Emeltchenko 
3841b71d385aSAndrei Emeltchenko 			hci_conn_enter_active_mode(chan->conn,
3842b71d385aSAndrei Emeltchenko 						   bt_cb(skb)->force_active);
3843b71d385aSAndrei Emeltchenko 
384457d17d70SMarcel Holtmann 			hci_send_frame(hdev, skb);
3845b71d385aSAndrei Emeltchenko 			hdev->acl_last_tx = jiffies;
3846b71d385aSAndrei Emeltchenko 
3847b71d385aSAndrei Emeltchenko 			hdev->block_cnt -= blocks;
3848b71d385aSAndrei Emeltchenko 			quote -= blocks;
3849b71d385aSAndrei Emeltchenko 
3850b71d385aSAndrei Emeltchenko 			chan->sent += blocks;
3851b71d385aSAndrei Emeltchenko 			chan->conn->sent += blocks;
3852b71d385aSAndrei Emeltchenko 		}
3853b71d385aSAndrei Emeltchenko 	}
3854b71d385aSAndrei Emeltchenko 
3855b71d385aSAndrei Emeltchenko 	if (cnt != hdev->block_cnt)
3856bd1eb66bSAndrei Emeltchenko 		hci_prio_recalculate(hdev, type);
3857b71d385aSAndrei Emeltchenko }
3858b71d385aSAndrei Emeltchenko 
38596039aa73SGustavo Padovan static void hci_sched_acl(struct hci_dev *hdev)
3860b71d385aSAndrei Emeltchenko {
3861b71d385aSAndrei Emeltchenko 	BT_DBG("%s", hdev->name);
3862b71d385aSAndrei Emeltchenko 
3863bd1eb66bSAndrei Emeltchenko 	/* No ACL link over BR/EDR controller */
3864bd1eb66bSAndrei Emeltchenko 	if (!hci_conn_num(hdev, ACL_LINK) && hdev->dev_type == HCI_BREDR)
3865bd1eb66bSAndrei Emeltchenko 		return;
3866bd1eb66bSAndrei Emeltchenko 
3867bd1eb66bSAndrei Emeltchenko 	/* No AMP link over AMP controller */
3868bd1eb66bSAndrei Emeltchenko 	if (!hci_conn_num(hdev, AMP_LINK) && hdev->dev_type == HCI_AMP)
3869b71d385aSAndrei Emeltchenko 		return;
3870b71d385aSAndrei Emeltchenko 
3871b71d385aSAndrei Emeltchenko 	switch (hdev->flow_ctl_mode) {
3872b71d385aSAndrei Emeltchenko 	case HCI_FLOW_CTL_MODE_PACKET_BASED:
3873b71d385aSAndrei Emeltchenko 		hci_sched_acl_pkt(hdev);
3874b71d385aSAndrei Emeltchenko 		break;
3875b71d385aSAndrei Emeltchenko 
3876b71d385aSAndrei Emeltchenko 	case HCI_FLOW_CTL_MODE_BLOCK_BASED:
3877b71d385aSAndrei Emeltchenko 		hci_sched_acl_blk(hdev);
3878b71d385aSAndrei Emeltchenko 		break;
3879b71d385aSAndrei Emeltchenko 	}
3880b71d385aSAndrei Emeltchenko }
3881b71d385aSAndrei Emeltchenko 
38821da177e4SLinus Torvalds /* Schedule SCO */
38836039aa73SGustavo Padovan static void hci_sched_sco(struct hci_dev *hdev)
38841da177e4SLinus Torvalds {
38851da177e4SLinus Torvalds 	struct hci_conn *conn;
38861da177e4SLinus Torvalds 	struct sk_buff *skb;
38871da177e4SLinus Torvalds 	int quote;
38881da177e4SLinus Torvalds 
38891da177e4SLinus Torvalds 	BT_DBG("%s", hdev->name);
38901da177e4SLinus Torvalds 
389152087a79SLuiz Augusto von Dentz 	if (!hci_conn_num(hdev, SCO_LINK))
389252087a79SLuiz Augusto von Dentz 		return;
389352087a79SLuiz Augusto von Dentz 
38941da177e4SLinus Torvalds 	while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, &quote))) {
38951da177e4SLinus Torvalds 		while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
38961da177e4SLinus Torvalds 			BT_DBG("skb %p len %d", skb, skb->len);
389757d17d70SMarcel Holtmann 			hci_send_frame(hdev, skb);
38981da177e4SLinus Torvalds 
38991da177e4SLinus Torvalds 			conn->sent++;
39001da177e4SLinus Torvalds 			if (conn->sent == ~0)
39011da177e4SLinus Torvalds 				conn->sent = 0;
39021da177e4SLinus Torvalds 		}
39031da177e4SLinus Torvalds 	}
39041da177e4SLinus Torvalds }
39051da177e4SLinus Torvalds 
39066039aa73SGustavo Padovan static void hci_sched_esco(struct hci_dev *hdev)
3907b6a0dc82SMarcel Holtmann {
3908b6a0dc82SMarcel Holtmann 	struct hci_conn *conn;
3909b6a0dc82SMarcel Holtmann 	struct sk_buff *skb;
3910b6a0dc82SMarcel Holtmann 	int quote;
3911b6a0dc82SMarcel Holtmann 
3912b6a0dc82SMarcel Holtmann 	BT_DBG("%s", hdev->name);
3913b6a0dc82SMarcel Holtmann 
391452087a79SLuiz Augusto von Dentz 	if (!hci_conn_num(hdev, ESCO_LINK))
391552087a79SLuiz Augusto von Dentz 		return;
391652087a79SLuiz Augusto von Dentz 
39178fc9ced3SGustavo Padovan 	while (hdev->sco_cnt && (conn = hci_low_sent(hdev, ESCO_LINK,
39188fc9ced3SGustavo Padovan 						     &quote))) {
3919b6a0dc82SMarcel Holtmann 		while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
3920b6a0dc82SMarcel Holtmann 			BT_DBG("skb %p len %d", skb, skb->len);
392157d17d70SMarcel Holtmann 			hci_send_frame(hdev, skb);
3922b6a0dc82SMarcel Holtmann 
3923b6a0dc82SMarcel Holtmann 			conn->sent++;
3924b6a0dc82SMarcel Holtmann 			if (conn->sent == ~0)
3925b6a0dc82SMarcel Holtmann 				conn->sent = 0;
3926b6a0dc82SMarcel Holtmann 		}
3927b6a0dc82SMarcel Holtmann 	}
3928b6a0dc82SMarcel Holtmann }
3929b6a0dc82SMarcel Holtmann 
39306039aa73SGustavo Padovan static void hci_sched_le(struct hci_dev *hdev)
39316ed58ec5SVille Tervo {
393273d80debSLuiz Augusto von Dentz 	struct hci_chan *chan;
39336ed58ec5SVille Tervo 	struct sk_buff *skb;
393402b20f0bSLuiz Augusto von Dentz 	int quote, cnt, tmp;
39356ed58ec5SVille Tervo 
39366ed58ec5SVille Tervo 	BT_DBG("%s", hdev->name);
39376ed58ec5SVille Tervo 
393852087a79SLuiz Augusto von Dentz 	if (!hci_conn_num(hdev, LE_LINK))
393952087a79SLuiz Augusto von Dentz 		return;
394052087a79SLuiz Augusto von Dentz 
39414a964404SMarcel Holtmann 	if (!test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) {
39426ed58ec5SVille Tervo 		/* LE tx timeout must be longer than maximum
39436ed58ec5SVille Tervo 		 * link supervision timeout (40.9 seconds) */
3944bae1f5d9SVille Tervo 		if (!hdev->le_cnt && hdev->le_pkts &&
39456ed58ec5SVille Tervo 		    time_after(jiffies, hdev->le_last_tx + HZ * 45))
3946bae1f5d9SVille Tervo 			hci_link_tx_to(hdev, LE_LINK);
39476ed58ec5SVille Tervo 	}
39486ed58ec5SVille Tervo 
39496ed58ec5SVille Tervo 	cnt = hdev->le_pkts ? hdev->le_cnt : hdev->acl_cnt;
395002b20f0bSLuiz Augusto von Dentz 	tmp = cnt;
395173d80debSLuiz Augusto von Dentz 	while (cnt && (chan = hci_chan_sent(hdev, LE_LINK, &quote))) {
3952ec1cce24SLuiz Augusto von Dentz 		u32 priority = (skb_peek(&chan->data_q))->priority;
3953ec1cce24SLuiz Augusto von Dentz 		while (quote-- && (skb = skb_peek(&chan->data_q))) {
395473d80debSLuiz Augusto von Dentz 			BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
395573d80debSLuiz Augusto von Dentz 			       skb->len, skb->priority);
39566ed58ec5SVille Tervo 
3957ec1cce24SLuiz Augusto von Dentz 			/* Stop if priority has changed */
3958ec1cce24SLuiz Augusto von Dentz 			if (skb->priority < priority)
3959ec1cce24SLuiz Augusto von Dentz 				break;
3960ec1cce24SLuiz Augusto von Dentz 
3961ec1cce24SLuiz Augusto von Dentz 			skb = skb_dequeue(&chan->data_q);
3962ec1cce24SLuiz Augusto von Dentz 
396357d17d70SMarcel Holtmann 			hci_send_frame(hdev, skb);
39646ed58ec5SVille Tervo 			hdev->le_last_tx = jiffies;
39656ed58ec5SVille Tervo 
39666ed58ec5SVille Tervo 			cnt--;
396773d80debSLuiz Augusto von Dentz 			chan->sent++;
396873d80debSLuiz Augusto von Dentz 			chan->conn->sent++;
39696ed58ec5SVille Tervo 		}
39706ed58ec5SVille Tervo 	}
397173d80debSLuiz Augusto von Dentz 
39726ed58ec5SVille Tervo 	if (hdev->le_pkts)
39736ed58ec5SVille Tervo 		hdev->le_cnt = cnt;
39746ed58ec5SVille Tervo 	else
39756ed58ec5SVille Tervo 		hdev->acl_cnt = cnt;
397602b20f0bSLuiz Augusto von Dentz 
397702b20f0bSLuiz Augusto von Dentz 	if (cnt != tmp)
397802b20f0bSLuiz Augusto von Dentz 		hci_prio_recalculate(hdev, LE_LINK);
39796ed58ec5SVille Tervo }
39806ed58ec5SVille Tervo 
39813eff45eaSGustavo F. Padovan static void hci_tx_work(struct work_struct *work)
39821da177e4SLinus Torvalds {
39833eff45eaSGustavo F. Padovan 	struct hci_dev *hdev = container_of(work, struct hci_dev, tx_work);
39841da177e4SLinus Torvalds 	struct sk_buff *skb;
39851da177e4SLinus Torvalds 
39866ed58ec5SVille Tervo 	BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt,
39876ed58ec5SVille Tervo 	       hdev->sco_cnt, hdev->le_cnt);
39881da177e4SLinus Torvalds 
398952de599eSMarcel Holtmann 	if (!test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
39901da177e4SLinus Torvalds 		/* Schedule queues and send stuff to HCI driver */
39911da177e4SLinus Torvalds 		hci_sched_acl(hdev);
39921da177e4SLinus Torvalds 		hci_sched_sco(hdev);
3993b6a0dc82SMarcel Holtmann 		hci_sched_esco(hdev);
39946ed58ec5SVille Tervo 		hci_sched_le(hdev);
399552de599eSMarcel Holtmann 	}
39966ed58ec5SVille Tervo 
39971da177e4SLinus Torvalds 	/* Send next queued raw (unknown type) packet */
39981da177e4SLinus Torvalds 	while ((skb = skb_dequeue(&hdev->raw_q)))
399957d17d70SMarcel Holtmann 		hci_send_frame(hdev, skb);
40001da177e4SLinus Torvalds }
40011da177e4SLinus Torvalds 
400225985edcSLucas De Marchi /* ----- HCI RX task (incoming data processing) ----- */
40031da177e4SLinus Torvalds 
40041da177e4SLinus Torvalds /* ACL data packet */
40056039aa73SGustavo Padovan static void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb)
40061da177e4SLinus Torvalds {
40071da177e4SLinus Torvalds 	struct hci_acl_hdr *hdr = (void *) skb->data;
40081da177e4SLinus Torvalds 	struct hci_conn *conn;
40091da177e4SLinus Torvalds 	__u16 handle, flags;
40101da177e4SLinus Torvalds 
40111da177e4SLinus Torvalds 	skb_pull(skb, HCI_ACL_HDR_SIZE);
40121da177e4SLinus Torvalds 
40131da177e4SLinus Torvalds 	handle = __le16_to_cpu(hdr->handle);
40141da177e4SLinus Torvalds 	flags  = hci_flags(handle);
40151da177e4SLinus Torvalds 	handle = hci_handle(handle);
40161da177e4SLinus Torvalds 
4017f0e09510SAndrei Emeltchenko 	BT_DBG("%s len %d handle 0x%4.4x flags 0x%4.4x", hdev->name, skb->len,
4018a8c5fb1aSGustavo Padovan 	       handle, flags);
40191da177e4SLinus Torvalds 
40201da177e4SLinus Torvalds 	hdev->stat.acl_rx++;
40211da177e4SLinus Torvalds 
40221da177e4SLinus Torvalds 	hci_dev_lock(hdev);
40231da177e4SLinus Torvalds 	conn = hci_conn_hash_lookup_handle(hdev, handle);
40241da177e4SLinus Torvalds 	hci_dev_unlock(hdev);
40251da177e4SLinus Torvalds 
40261da177e4SLinus Torvalds 	if (conn) {
402765983fc7SMat Martineau 		hci_conn_enter_active_mode(conn, BT_POWER_FORCE_ACTIVE_OFF);
402804837f64SMarcel Holtmann 
40291da177e4SLinus Torvalds 		/* Send to upper protocol */
4030686ebf28SUlisses Furquim 		l2cap_recv_acldata(conn, skb, flags);
40311da177e4SLinus Torvalds 		return;
40321da177e4SLinus Torvalds 	} else {
40331da177e4SLinus Torvalds 		BT_ERR("%s ACL packet for unknown connection handle %d",
40341da177e4SLinus Torvalds 		       hdev->name, handle);
40351da177e4SLinus Torvalds 	}
40361da177e4SLinus Torvalds 
40371da177e4SLinus Torvalds 	kfree_skb(skb);
40381da177e4SLinus Torvalds }
40391da177e4SLinus Torvalds 
40401da177e4SLinus Torvalds /* SCO data packet */
40416039aa73SGustavo Padovan static void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb)
40421da177e4SLinus Torvalds {
40431da177e4SLinus Torvalds 	struct hci_sco_hdr *hdr = (void *) skb->data;
40441da177e4SLinus Torvalds 	struct hci_conn *conn;
40451da177e4SLinus Torvalds 	__u16 handle;
40461da177e4SLinus Torvalds 
40471da177e4SLinus Torvalds 	skb_pull(skb, HCI_SCO_HDR_SIZE);
40481da177e4SLinus Torvalds 
40491da177e4SLinus Torvalds 	handle = __le16_to_cpu(hdr->handle);
40501da177e4SLinus Torvalds 
4051f0e09510SAndrei Emeltchenko 	BT_DBG("%s len %d handle 0x%4.4x", hdev->name, skb->len, handle);
40521da177e4SLinus Torvalds 
40531da177e4SLinus Torvalds 	hdev->stat.sco_rx++;
40541da177e4SLinus Torvalds 
40551da177e4SLinus Torvalds 	hci_dev_lock(hdev);
40561da177e4SLinus Torvalds 	conn = hci_conn_hash_lookup_handle(hdev, handle);
40571da177e4SLinus Torvalds 	hci_dev_unlock(hdev);
40581da177e4SLinus Torvalds 
40591da177e4SLinus Torvalds 	if (conn) {
40601da177e4SLinus Torvalds 		/* Send to upper protocol */
4061686ebf28SUlisses Furquim 		sco_recv_scodata(conn, skb);
40621da177e4SLinus Torvalds 		return;
40631da177e4SLinus Torvalds 	} else {
40641da177e4SLinus Torvalds 		BT_ERR("%s SCO packet for unknown connection handle %d",
40651da177e4SLinus Torvalds 		       hdev->name, handle);
40661da177e4SLinus Torvalds 	}
40671da177e4SLinus Torvalds 
40681da177e4SLinus Torvalds 	kfree_skb(skb);
40691da177e4SLinus Torvalds }
40701da177e4SLinus Torvalds 
40719238f36aSJohan Hedberg static bool hci_req_is_complete(struct hci_dev *hdev)
40729238f36aSJohan Hedberg {
40739238f36aSJohan Hedberg 	struct sk_buff *skb;
40749238f36aSJohan Hedberg 
40759238f36aSJohan Hedberg 	skb = skb_peek(&hdev->cmd_q);
40769238f36aSJohan Hedberg 	if (!skb)
40779238f36aSJohan Hedberg 		return true;
40789238f36aSJohan Hedberg 
40799238f36aSJohan Hedberg 	return bt_cb(skb)->req.start;
40809238f36aSJohan Hedberg }
40819238f36aSJohan Hedberg 
408242c6b129SJohan Hedberg static void hci_resend_last(struct hci_dev *hdev)
408342c6b129SJohan Hedberg {
408442c6b129SJohan Hedberg 	struct hci_command_hdr *sent;
408542c6b129SJohan Hedberg 	struct sk_buff *skb;
408642c6b129SJohan Hedberg 	u16 opcode;
408742c6b129SJohan Hedberg 
408842c6b129SJohan Hedberg 	if (!hdev->sent_cmd)
408942c6b129SJohan Hedberg 		return;
409042c6b129SJohan Hedberg 
409142c6b129SJohan Hedberg 	sent = (void *) hdev->sent_cmd->data;
409242c6b129SJohan Hedberg 	opcode = __le16_to_cpu(sent->opcode);
409342c6b129SJohan Hedberg 	if (opcode == HCI_OP_RESET)
409442c6b129SJohan Hedberg 		return;
409542c6b129SJohan Hedberg 
409642c6b129SJohan Hedberg 	skb = skb_clone(hdev->sent_cmd, GFP_KERNEL);
409742c6b129SJohan Hedberg 	if (!skb)
409842c6b129SJohan Hedberg 		return;
409942c6b129SJohan Hedberg 
410042c6b129SJohan Hedberg 	skb_queue_head(&hdev->cmd_q, skb);
410142c6b129SJohan Hedberg 	queue_work(hdev->workqueue, &hdev->cmd_work);
410242c6b129SJohan Hedberg }
410342c6b129SJohan Hedberg 
41049238f36aSJohan Hedberg void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status)
41059238f36aSJohan Hedberg {
41069238f36aSJohan Hedberg 	hci_req_complete_t req_complete = NULL;
41079238f36aSJohan Hedberg 	struct sk_buff *skb;
41089238f36aSJohan Hedberg 	unsigned long flags;
41099238f36aSJohan Hedberg 
41109238f36aSJohan Hedberg 	BT_DBG("opcode 0x%04x status 0x%02x", opcode, status);
41119238f36aSJohan Hedberg 
411242c6b129SJohan Hedberg 	/* If the completed command doesn't match the last one that was
411342c6b129SJohan Hedberg 	 * sent we need to do special handling of it.
41149238f36aSJohan Hedberg 	 */
411542c6b129SJohan Hedberg 	if (!hci_sent_cmd_data(hdev, opcode)) {
411642c6b129SJohan Hedberg 		/* Some CSR based controllers generate a spontaneous
411742c6b129SJohan Hedberg 		 * reset complete event during init and any pending
411842c6b129SJohan Hedberg 		 * command will never be completed. In such a case we
411942c6b129SJohan Hedberg 		 * need to resend whatever was the last sent
412042c6b129SJohan Hedberg 		 * command.
412142c6b129SJohan Hedberg 		 */
412242c6b129SJohan Hedberg 		if (test_bit(HCI_INIT, &hdev->flags) && opcode == HCI_OP_RESET)
412342c6b129SJohan Hedberg 			hci_resend_last(hdev);
412442c6b129SJohan Hedberg 
41259238f36aSJohan Hedberg 		return;
412642c6b129SJohan Hedberg 	}
41279238f36aSJohan Hedberg 
41289238f36aSJohan Hedberg 	/* If the command succeeded and there's still more commands in
41299238f36aSJohan Hedberg 	 * this request the request is not yet complete.
41309238f36aSJohan Hedberg 	 */
41319238f36aSJohan Hedberg 	if (!status && !hci_req_is_complete(hdev))
41329238f36aSJohan Hedberg 		return;
41339238f36aSJohan Hedberg 
41349238f36aSJohan Hedberg 	/* If this was the last command in a request the complete
41359238f36aSJohan Hedberg 	 * callback would be found in hdev->sent_cmd instead of the
41369238f36aSJohan Hedberg 	 * command queue (hdev->cmd_q).
41379238f36aSJohan Hedberg 	 */
41389238f36aSJohan Hedberg 	if (hdev->sent_cmd) {
41399238f36aSJohan Hedberg 		req_complete = bt_cb(hdev->sent_cmd)->req.complete;
414053e21fbcSJohan Hedberg 
414153e21fbcSJohan Hedberg 		if (req_complete) {
414253e21fbcSJohan Hedberg 			/* We must set the complete callback to NULL to
414353e21fbcSJohan Hedberg 			 * avoid calling the callback more than once if
414453e21fbcSJohan Hedberg 			 * this function gets called again.
414553e21fbcSJohan Hedberg 			 */
414653e21fbcSJohan Hedberg 			bt_cb(hdev->sent_cmd)->req.complete = NULL;
414753e21fbcSJohan Hedberg 
41489238f36aSJohan Hedberg 			goto call_complete;
41499238f36aSJohan Hedberg 		}
415053e21fbcSJohan Hedberg 	}
41519238f36aSJohan Hedberg 
41529238f36aSJohan Hedberg 	/* Remove all pending commands belonging to this request */
41539238f36aSJohan Hedberg 	spin_lock_irqsave(&hdev->cmd_q.lock, flags);
41549238f36aSJohan Hedberg 	while ((skb = __skb_dequeue(&hdev->cmd_q))) {
41559238f36aSJohan Hedberg 		if (bt_cb(skb)->req.start) {
41569238f36aSJohan Hedberg 			__skb_queue_head(&hdev->cmd_q, skb);
41579238f36aSJohan Hedberg 			break;
41589238f36aSJohan Hedberg 		}
41599238f36aSJohan Hedberg 
41609238f36aSJohan Hedberg 		req_complete = bt_cb(skb)->req.complete;
41619238f36aSJohan Hedberg 		kfree_skb(skb);
41629238f36aSJohan Hedberg 	}
41639238f36aSJohan Hedberg 	spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
41649238f36aSJohan Hedberg 
41659238f36aSJohan Hedberg call_complete:
41669238f36aSJohan Hedberg 	if (req_complete)
41679238f36aSJohan Hedberg 		req_complete(hdev, status);
41689238f36aSJohan Hedberg }
41699238f36aSJohan Hedberg 
4170b78752ccSMarcel Holtmann static void hci_rx_work(struct work_struct *work)
41711da177e4SLinus Torvalds {
4172b78752ccSMarcel Holtmann 	struct hci_dev *hdev = container_of(work, struct hci_dev, rx_work);
41731da177e4SLinus Torvalds 	struct sk_buff *skb;
41741da177e4SLinus Torvalds 
41751da177e4SLinus Torvalds 	BT_DBG("%s", hdev->name);
41761da177e4SLinus Torvalds 
41771da177e4SLinus Torvalds 	while ((skb = skb_dequeue(&hdev->rx_q))) {
4178cd82e61cSMarcel Holtmann 		/* Send copy to monitor */
4179cd82e61cSMarcel Holtmann 		hci_send_to_monitor(hdev, skb);
4180cd82e61cSMarcel Holtmann 
41811da177e4SLinus Torvalds 		if (atomic_read(&hdev->promisc)) {
41821da177e4SLinus Torvalds 			/* Send copy to the sockets */
4183470fe1b5SMarcel Holtmann 			hci_send_to_sock(hdev, skb);
41841da177e4SLinus Torvalds 		}
41851da177e4SLinus Torvalds 
4186fee746b0SMarcel Holtmann 		if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
41871da177e4SLinus Torvalds 			kfree_skb(skb);
41881da177e4SLinus Torvalds 			continue;
41891da177e4SLinus Torvalds 		}
41901da177e4SLinus Torvalds 
41911da177e4SLinus Torvalds 		if (test_bit(HCI_INIT, &hdev->flags)) {
41921da177e4SLinus Torvalds 			/* Don't process data packets in this states. */
41930d48d939SMarcel Holtmann 			switch (bt_cb(skb)->pkt_type) {
41941da177e4SLinus Torvalds 			case HCI_ACLDATA_PKT:
41951da177e4SLinus Torvalds 			case HCI_SCODATA_PKT:
41961da177e4SLinus Torvalds 				kfree_skb(skb);
41971da177e4SLinus Torvalds 				continue;
41983ff50b79SStephen Hemminger 			}
41991da177e4SLinus Torvalds 		}
42001da177e4SLinus Torvalds 
42011da177e4SLinus Torvalds 		/* Process frame */
42020d48d939SMarcel Holtmann 		switch (bt_cb(skb)->pkt_type) {
42031da177e4SLinus Torvalds 		case HCI_EVENT_PKT:
4204b78752ccSMarcel Holtmann 			BT_DBG("%s Event packet", hdev->name);
42051da177e4SLinus Torvalds 			hci_event_packet(hdev, skb);
42061da177e4SLinus Torvalds 			break;
42071da177e4SLinus Torvalds 
42081da177e4SLinus Torvalds 		case HCI_ACLDATA_PKT:
42091da177e4SLinus Torvalds 			BT_DBG("%s ACL data packet", hdev->name);
42101da177e4SLinus Torvalds 			hci_acldata_packet(hdev, skb);
42111da177e4SLinus Torvalds 			break;
42121da177e4SLinus Torvalds 
42131da177e4SLinus Torvalds 		case HCI_SCODATA_PKT:
42141da177e4SLinus Torvalds 			BT_DBG("%s SCO data packet", hdev->name);
42151da177e4SLinus Torvalds 			hci_scodata_packet(hdev, skb);
42161da177e4SLinus Torvalds 			break;
42171da177e4SLinus Torvalds 
42181da177e4SLinus Torvalds 		default:
42191da177e4SLinus Torvalds 			kfree_skb(skb);
42201da177e4SLinus Torvalds 			break;
42211da177e4SLinus Torvalds 		}
42221da177e4SLinus Torvalds 	}
42231da177e4SLinus Torvalds }
42241da177e4SLinus Torvalds 
4225c347b765SGustavo F. Padovan static void hci_cmd_work(struct work_struct *work)
42261da177e4SLinus Torvalds {
4227c347b765SGustavo F. Padovan 	struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_work);
42281da177e4SLinus Torvalds 	struct sk_buff *skb;
42291da177e4SLinus Torvalds 
42302104786bSAndrei Emeltchenko 	BT_DBG("%s cmd_cnt %d cmd queued %d", hdev->name,
42312104786bSAndrei Emeltchenko 	       atomic_read(&hdev->cmd_cnt), skb_queue_len(&hdev->cmd_q));
42321da177e4SLinus Torvalds 
42331da177e4SLinus Torvalds 	/* Send queued commands */
42345a08ecceSAndrei Emeltchenko 	if (atomic_read(&hdev->cmd_cnt)) {
42355a08ecceSAndrei Emeltchenko 		skb = skb_dequeue(&hdev->cmd_q);
42365a08ecceSAndrei Emeltchenko 		if (!skb)
42375a08ecceSAndrei Emeltchenko 			return;
42385a08ecceSAndrei Emeltchenko 
42391da177e4SLinus Torvalds 		kfree_skb(hdev->sent_cmd);
42401da177e4SLinus Torvalds 
4241a675d7f1SMarcel Holtmann 		hdev->sent_cmd = skb_clone(skb, GFP_KERNEL);
424270f23020SAndrei Emeltchenko 		if (hdev->sent_cmd) {
42431da177e4SLinus Torvalds 			atomic_dec(&hdev->cmd_cnt);
424457d17d70SMarcel Holtmann 			hci_send_frame(hdev, skb);
42457bdb8a5cSSzymon Janc 			if (test_bit(HCI_RESET, &hdev->flags))
424665cc2b49SMarcel Holtmann 				cancel_delayed_work(&hdev->cmd_timer);
42477bdb8a5cSSzymon Janc 			else
424865cc2b49SMarcel Holtmann 				schedule_delayed_work(&hdev->cmd_timer,
424965cc2b49SMarcel Holtmann 						      HCI_CMD_TIMEOUT);
42501da177e4SLinus Torvalds 		} else {
42511da177e4SLinus Torvalds 			skb_queue_head(&hdev->cmd_q, skb);
4252c347b765SGustavo F. Padovan 			queue_work(hdev->workqueue, &hdev->cmd_work);
42531da177e4SLinus Torvalds 		}
42541da177e4SLinus Torvalds 	}
42551da177e4SLinus Torvalds }
4256