xref: /openbmc/linux/net/bluetooth/hci_conn.c (revision 3cde81f8ad3e48615d8e653d700eabb1f963780a)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds    BlueZ - Bluetooth protocol stack for Linux
32d0a0346SRon Shaffer    Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
40fe8c8d0SIulia Tanasescu    Copyright 2023 NXP
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 connection handling. */
271da177e4SLinus Torvalds 
288c520a59SGustavo Padovan #include <linux/export.h>
2923b9ceb7SMarcel Holtmann #include <linux/debugfs.h>
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #include <net/bluetooth/bluetooth.h>
321da177e4SLinus Torvalds #include <net/bluetooth/hci_core.h>
334bc58f51SJohan Hedberg #include <net/bluetooth/l2cap.h>
34eca0ae4aSLuiz Augusto von Dentz #include <net/bluetooth/iso.h>
35eca0ae4aSLuiz Augusto von Dentz #include <net/bluetooth/mgmt.h>
361da177e4SLinus Torvalds 
370857dd3bSJohan Hedberg #include "hci_request.h"
38ac4b7236SMarcel Holtmann #include "smp.h"
39eca0ae4aSLuiz Augusto von Dentz #include "eir.h"
407024728eSMarcel Holtmann 
412dea632fSFrédéric Dalleau struct sco_param {
422dea632fSFrédéric Dalleau 	u16 pkt_type;
432dea632fSFrédéric Dalleau 	u16 max_latency;
44c7da5797SJohan Hedberg 	u8  retrans_effort;
452dea632fSFrédéric Dalleau };
462dea632fSFrédéric Dalleau 
47e07a06b4SBrian Gix struct conn_handle_t {
48e07a06b4SBrian Gix 	struct hci_conn *conn;
49e07a06b4SBrian Gix 	__u16 handle;
50e07a06b4SBrian Gix };
51e07a06b4SBrian Gix 
5248e68ff5SBernhard Thaler static const struct sco_param esco_param_cvsd[] = {
53c7da5797SJohan Hedberg 	{ EDR_ESCO_MASK & ~ESCO_2EV3, 0x000a,	0x01 }, /* S3 */
54c7da5797SJohan Hedberg 	{ EDR_ESCO_MASK & ~ESCO_2EV3, 0x0007,	0x01 }, /* S2 */
55c7da5797SJohan Hedberg 	{ EDR_ESCO_MASK | ESCO_EV3,   0x0007,	0x01 }, /* S1 */
56c7da5797SJohan Hedberg 	{ EDR_ESCO_MASK | ESCO_HV3,   0xffff,	0x01 }, /* D1 */
57c7da5797SJohan Hedberg 	{ EDR_ESCO_MASK | ESCO_HV1,   0xffff,	0x01 }, /* D0 */
582dea632fSFrédéric Dalleau };
592dea632fSFrédéric Dalleau 
6048e68ff5SBernhard Thaler static const struct sco_param sco_param_cvsd[] = {
61c7da5797SJohan Hedberg 	{ EDR_ESCO_MASK | ESCO_HV3,   0xffff,	0xff }, /* D1 */
62c7da5797SJohan Hedberg 	{ EDR_ESCO_MASK | ESCO_HV1,   0xffff,	0xff }, /* D0 */
6348e68ff5SBernhard Thaler };
6448e68ff5SBernhard Thaler 
65565766b0SJohan Hedberg static const struct sco_param esco_param_msbc[] = {
66c7da5797SJohan Hedberg 	{ EDR_ESCO_MASK & ~ESCO_2EV3, 0x000d,	0x02 }, /* T2 */
67c7da5797SJohan Hedberg 	{ EDR_ESCO_MASK | ESCO_EV3,   0x0008,	0x02 }, /* T1 */
682dea632fSFrédéric Dalleau };
692dea632fSFrédéric Dalleau 
70f75113a2SJakub Pawlowski /* This function requires the caller holds hdev->lock */
7119cf60bfSLuiz Augusto von Dentz static void hci_connect_le_scan_cleanup(struct hci_conn *conn, u8 status)
72f75113a2SJakub Pawlowski {
73f75113a2SJakub Pawlowski 	struct hci_conn_params *params;
74b5c2b621SJohan Hedberg 	struct hci_dev *hdev = conn->hdev;
75f75113a2SJakub Pawlowski 	struct smp_irk *irk;
76f75113a2SJakub Pawlowski 	bdaddr_t *bdaddr;
77f75113a2SJakub Pawlowski 	u8 bdaddr_type;
78f75113a2SJakub Pawlowski 
79f75113a2SJakub Pawlowski 	bdaddr = &conn->dst;
80f75113a2SJakub Pawlowski 	bdaddr_type = conn->dst_type;
81f75113a2SJakub Pawlowski 
82f75113a2SJakub Pawlowski 	/* Check if we need to convert to identity address */
83b5c2b621SJohan Hedberg 	irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
84f75113a2SJakub Pawlowski 	if (irk) {
85f75113a2SJakub Pawlowski 		bdaddr = &irk->bdaddr;
86f75113a2SJakub Pawlowski 		bdaddr_type = irk->addr_type;
87f75113a2SJakub Pawlowski 	}
88f75113a2SJakub Pawlowski 
8917bc08f0SJohan Hedberg 	params = hci_pend_le_action_lookup(&hdev->pend_le_conns, bdaddr,
9017bc08f0SJohan Hedberg 					   bdaddr_type);
9119cf60bfSLuiz Augusto von Dentz 	if (!params)
92f75113a2SJakub Pawlowski 		return;
93f75113a2SJakub Pawlowski 
9419cf60bfSLuiz Augusto von Dentz 	if (params->conn) {
9519cf60bfSLuiz Augusto von Dentz 		hci_conn_drop(params->conn);
9619cf60bfSLuiz Augusto von Dentz 		hci_conn_put(params->conn);
9719cf60bfSLuiz Augusto von Dentz 		params->conn = NULL;
9819cf60bfSLuiz Augusto von Dentz 	}
9919cf60bfSLuiz Augusto von Dentz 
10019cf60bfSLuiz Augusto von Dentz 	if (!params->explicit_connect)
10119cf60bfSLuiz Augusto von Dentz 		return;
10219cf60bfSLuiz Augusto von Dentz 
10319cf60bfSLuiz Augusto von Dentz 	/* If the status indicates successful cancellation of
10419cf60bfSLuiz Augusto von Dentz 	 * the attempt (i.e. Unknown Connection Id) there's no point of
10519cf60bfSLuiz Augusto von Dentz 	 * notifying failure since we'll go back to keep trying to
10619cf60bfSLuiz Augusto von Dentz 	 * connect. The only exception is explicit connect requests
10719cf60bfSLuiz Augusto von Dentz 	 * where a timeout + cancel does indicate an actual failure.
10819cf60bfSLuiz Augusto von Dentz 	 */
10919cf60bfSLuiz Augusto von Dentz 	if (status && status != HCI_ERROR_UNKNOWN_CONN_ID)
11019cf60bfSLuiz Augusto von Dentz 		mgmt_connect_failed(hdev, &conn->dst, conn->type,
11119cf60bfSLuiz Augusto von Dentz 				    conn->dst_type, status);
11219cf60bfSLuiz Augusto von Dentz 
113f75113a2SJakub Pawlowski 	/* The connection attempt was doing scan for new RPA, and is
114f75113a2SJakub Pawlowski 	 * in scan phase. If params are not associated with any other
115f75113a2SJakub Pawlowski 	 * autoconnect action, remove them completely. If they are, just unmark
116f75113a2SJakub Pawlowski 	 * them as waiting for connection, by clearing explicit_connect field.
117f75113a2SJakub Pawlowski 	 */
118f75113a2SJakub Pawlowski 	params->explicit_connect = false;
1199ad3e6ffSJohan Hedberg 
120195ef75eSPauli Virtanen 	hci_pend_le_list_del_init(params);
1219ad3e6ffSJohan Hedberg 
1229ad3e6ffSJohan Hedberg 	switch (params->auto_connect) {
1239ad3e6ffSJohan Hedberg 	case HCI_AUTO_CONN_EXPLICIT:
124b5c2b621SJohan Hedberg 		hci_conn_params_del(hdev, bdaddr, bdaddr_type);
1259ad3e6ffSJohan Hedberg 		/* return instead of break to avoid duplicate scan update */
1269ad3e6ffSJohan Hedberg 		return;
1279ad3e6ffSJohan Hedberg 	case HCI_AUTO_CONN_DIRECT:
1289ad3e6ffSJohan Hedberg 	case HCI_AUTO_CONN_ALWAYS:
129195ef75eSPauli Virtanen 		hci_pend_le_list_add(params, &hdev->pend_le_conns);
1309ad3e6ffSJohan Hedberg 		break;
1319ad3e6ffSJohan Hedberg 	case HCI_AUTO_CONN_REPORT:
132195ef75eSPauli Virtanen 		hci_pend_le_list_add(params, &hdev->pend_le_reports);
1339ad3e6ffSJohan Hedberg 		break;
1349ad3e6ffSJohan Hedberg 	default:
1359ad3e6ffSJohan Hedberg 		break;
136168b8a25SJakub Pawlowski 	}
1379ad3e6ffSJohan Hedberg 
1385bee2fd6SLuiz Augusto von Dentz 	hci_update_passive_scan(hdev);
139f75113a2SJakub Pawlowski }
140f75113a2SJakub Pawlowski 
141b958f9a3SJohan Hedberg static void hci_conn_cleanup(struct hci_conn *conn)
142b958f9a3SJohan Hedberg {
143b958f9a3SJohan Hedberg 	struct hci_dev *hdev = conn->hdev;
144b958f9a3SJohan Hedberg 
145b958f9a3SJohan Hedberg 	if (test_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags))
146b958f9a3SJohan Hedberg 		hci_conn_params_del(conn->hdev, &conn->dst, conn->dst_type);
147b958f9a3SJohan Hedberg 
148629f66aaSAlain Michaud 	if (test_and_clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
149629f66aaSAlain Michaud 		hci_remove_link_key(hdev, &conn->dst);
150629f66aaSAlain Michaud 
151b958f9a3SJohan Hedberg 	hci_chan_list_flush(conn);
152b958f9a3SJohan Hedberg 
153b958f9a3SJohan Hedberg 	hci_conn_hash_del(hdev, conn);
154b958f9a3SJohan Hedberg 
15584cb0143SZiyang Xuan 	if (HCI_CONN_HANDLE_UNSET(conn->handle))
15684cb0143SZiyang Xuan 		ida_free(&hdev->unset_handle_ida, conn->handle);
15784cb0143SZiyang Xuan 
15826afbd82SLuiz Augusto von Dentz 	if (conn->cleanup)
15926afbd82SLuiz Augusto von Dentz 		conn->cleanup(conn);
16026afbd82SLuiz Augusto von Dentz 
1611f8330eaSSathish Narsimman 	if (conn->type == SCO_LINK || conn->type == ESCO_LINK) {
1621f8330eaSSathish Narsimman 		switch (conn->setting & SCO_AIRMODE_MASK) {
1631f8330eaSSathish Narsimman 		case SCO_AIRMODE_CVSD:
1641f8330eaSSathish Narsimman 		case SCO_AIRMODE_TRANSP:
1651f8330eaSSathish Narsimman 			if (hdev->notify)
1661f8330eaSSathish Narsimman 				hdev->notify(hdev, HCI_NOTIFY_DISABLE_SCO);
1671f8330eaSSathish Narsimman 			break;
1681f8330eaSSathish Narsimman 		}
1691f8330eaSSathish Narsimman 	} else {
170b958f9a3SJohan Hedberg 		if (hdev->notify)
171b958f9a3SJohan Hedberg 			hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
1721f8330eaSSathish Narsimman 	}
173b958f9a3SJohan Hedberg 
174b958f9a3SJohan Hedberg 	debugfs_remove_recursive(conn->debugfs);
175b958f9a3SJohan Hedberg 
17656a4fddeSZhengHan Wang 	hci_conn_del_sysfs(conn);
177b958f9a3SJohan Hedberg 
17856a4fddeSZhengHan Wang 	hci_dev_put(hdev);
179b958f9a3SJohan Hedberg }
180b958f9a3SJohan Hedberg 
1811aef8669SVinicius Costa Gomes static void hci_acl_create_connection(struct hci_conn *conn)
1821da177e4SLinus Torvalds {
1831da177e4SLinus Torvalds 	struct hci_dev *hdev = conn->hdev;
1841da177e4SLinus Torvalds 	struct inquiry_entry *ie;
1851da177e4SLinus Torvalds 	struct hci_cp_create_conn cp;
1861da177e4SLinus Torvalds 
18742d2d87cSAndrei Emeltchenko 	BT_DBG("hcon %p", conn);
1881da177e4SLinus Torvalds 
18989e65975SSonny Sasaka 	/* Many controllers disallow HCI Create Connection while it is doing
19089e65975SSonny Sasaka 	 * HCI Inquiry. So we cancel the Inquiry first before issuing HCI Create
19189e65975SSonny Sasaka 	 * Connection. This may cause the MGMT discovering state to become false
19289e65975SSonny Sasaka 	 * without user space's request but it is okay since the MGMT Discovery
19389e65975SSonny Sasaka 	 * APIs do not promise that discovery should be done forever. Instead,
19489e65975SSonny Sasaka 	 * the user space monitors the status of MGMT discovering and it may
19589e65975SSonny Sasaka 	 * request for discovery again when this flag becomes false.
19689e65975SSonny Sasaka 	 */
19789e65975SSonny Sasaka 	if (test_bit(HCI_INQUIRY, &hdev->flags)) {
19889e65975SSonny Sasaka 		/* Put this connection to "pending" state so that it will be
19989e65975SSonny Sasaka 		 * executed after the inquiry cancel command complete event.
20089e65975SSonny Sasaka 		 */
20189e65975SSonny Sasaka 		conn->state = BT_CONNECT2;
20289e65975SSonny Sasaka 		hci_send_cmd(hdev, HCI_OP_INQUIRY_CANCEL, 0, NULL);
20389e65975SSonny Sasaka 		return;
20489e65975SSonny Sasaka 	}
20589e65975SSonny Sasaka 
2061da177e4SLinus Torvalds 	conn->state = BT_CONNECT;
207a0c808b3SJohan Hedberg 	conn->out = true;
20840bef302SJohan Hedberg 	conn->role = HCI_ROLE_MASTER;
2091da177e4SLinus Torvalds 
2104c67bc74SMarcel Holtmann 	conn->attempt++;
2114c67bc74SMarcel Holtmann 
212e4e8e37cSMarcel Holtmann 	conn->link_policy = hdev->link_policy;
213e4e8e37cSMarcel Holtmann 
2141da177e4SLinus Torvalds 	memset(&cp, 0, sizeof(cp));
2151da177e4SLinus Torvalds 	bacpy(&cp.bdaddr, &conn->dst);
2161da177e4SLinus Torvalds 	cp.pscan_rep_mode = 0x02;
2171da177e4SLinus Torvalds 
21870f23020SAndrei Emeltchenko 	ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
21970f23020SAndrei Emeltchenko 	if (ie) {
22041a96212SMarcel Holtmann 		if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) {
2211da177e4SLinus Torvalds 			cp.pscan_rep_mode = ie->data.pscan_rep_mode;
2221da177e4SLinus Torvalds 			cp.pscan_mode     = ie->data.pscan_mode;
22341a96212SMarcel Holtmann 			cp.clock_offset   = ie->data.clock_offset |
224dcf4adbfSJoe Perches 					    cpu_to_le16(0x8000);
22541a96212SMarcel Holtmann 		}
22641a96212SMarcel Holtmann 
2271da177e4SLinus Torvalds 		memcpy(conn->dev_class, ie->data.dev_class, 3);
2281da177e4SLinus Torvalds 	}
2291da177e4SLinus Torvalds 
230a8746417SMarcel Holtmann 	cp.pkt_type = cpu_to_le16(conn->pkt_type);
2311da177e4SLinus Torvalds 	if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
2321da177e4SLinus Torvalds 		cp.role_switch = 0x01;
2331da177e4SLinus Torvalds 	else
2341da177e4SLinus Torvalds 		cp.role_switch = 0x00;
2351da177e4SLinus Torvalds 
236a9de9248SMarcel Holtmann 	hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp);
2371da177e4SLinus Torvalds }
2381da177e4SLinus Torvalds 
239e3b679d5SJohan Hedberg int hci_disconnect(struct hci_conn *conn, __u8 reason)
2401da177e4SLinus Torvalds {
24138b3fef1SAndrei Emeltchenko 	BT_DBG("hcon %p", conn);
2421da177e4SLinus Torvalds 
24374be523cSArchie Pusaka 	/* When we are central of an established connection and it enters
244839035a7SJohan Hedberg 	 * the disconnect timeout, then go ahead and try to read the
245839035a7SJohan Hedberg 	 * current clock offset.  Processing of the result is done
246839035a7SJohan Hedberg 	 * within the event handling and hci_clock_offset_evt function.
247839035a7SJohan Hedberg 	 */
24888d07febSJohan Hedberg 	if (conn->type == ACL_LINK && conn->role == HCI_ROLE_MASTER &&
24988d07febSJohan Hedberg 	    (conn->state == BT_CONNECTED || conn->state == BT_CONFIG)) {
250839035a7SJohan Hedberg 		struct hci_dev *hdev = conn->hdev;
2514f639edeSFabian Frederick 		struct hci_cp_read_clock_offset clkoff_cp;
252839035a7SJohan Hedberg 
2534f639edeSFabian Frederick 		clkoff_cp.handle = cpu_to_le16(conn->handle);
2544f639edeSFabian Frederick 		hci_send_cmd(hdev, HCI_OP_READ_CLOCK_OFFSET, sizeof(clkoff_cp),
2554f639edeSFabian Frederick 			     &clkoff_cp);
256839035a7SJohan Hedberg 	}
257839035a7SJohan Hedberg 
25888d07febSJohan Hedberg 	return hci_abort_conn(conn, reason);
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds 
26157f5d0d1SVinicius Costa Gomes static void hci_add_sco(struct hci_conn *conn, __u16 handle)
2621da177e4SLinus Torvalds {
2631da177e4SLinus Torvalds 	struct hci_dev *hdev = conn->hdev;
2641da177e4SLinus Torvalds 	struct hci_cp_add_sco cp;
2651da177e4SLinus Torvalds 
26638b3fef1SAndrei Emeltchenko 	BT_DBG("hcon %p", conn);
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	conn->state = BT_CONNECT;
269a0c808b3SJohan Hedberg 	conn->out = true;
2701da177e4SLinus Torvalds 
271efc7688bSMarcel Holtmann 	conn->attempt++;
272efc7688bSMarcel Holtmann 
273aca3192cSYOSHIFUJI Hideaki 	cp.handle   = cpu_to_le16(handle);
274a8746417SMarcel Holtmann 	cp.pkt_type = cpu_to_le16(conn->pkt_type);
2751da177e4SLinus Torvalds 
276a9de9248SMarcel Holtmann 	hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp);
2771da177e4SLinus Torvalds }
2781da177e4SLinus Torvalds 
2798b1c324cSYu Liu static bool find_next_esco_param(struct hci_conn *conn,
2808b1c324cSYu Liu 				 const struct sco_param *esco_param, int size)
2818b1c324cSYu Liu {
28206149746SLuiz Augusto von Dentz 	if (!conn->parent)
28306149746SLuiz Augusto von Dentz 		return false;
28406149746SLuiz Augusto von Dentz 
2858b1c324cSYu Liu 	for (; conn->attempt <= size; conn->attempt++) {
28606149746SLuiz Augusto von Dentz 		if (lmp_esco_2m_capable(conn->parent) ||
2878b1c324cSYu Liu 		    (esco_param[conn->attempt - 1].pkt_type & ESCO_2EV3))
2888b1c324cSYu Liu 			break;
2898b1c324cSYu Liu 		BT_DBG("hcon %p skipped attempt %d, eSCO 2M not supported",
2908b1c324cSYu Liu 		       conn, conn->attempt);
2918b1c324cSYu Liu 	}
2928b1c324cSYu Liu 
2938b1c324cSYu Liu 	return conn->attempt <= size;
2948b1c324cSYu Liu }
2958b1c324cSYu Liu 
296e07a06b4SBrian Gix static int configure_datapath_sync(struct hci_dev *hdev, struct bt_codec *codec)
297b2af264aSKiran K {
298e07a06b4SBrian Gix 	int err;
299e07a06b4SBrian Gix 	__u8 vnd_len, *vnd_data = NULL;
300e07a06b4SBrian Gix 	struct hci_op_configure_data_path *cmd = NULL;
301e07a06b4SBrian Gix 
302*3cde81f8SZijun Hu 	if (!codec->data_path || !hdev->get_codec_config_data)
303*3cde81f8SZijun Hu 		return 0;
304*3cde81f8SZijun Hu 
305*3cde81f8SZijun Hu 	/* Do not take me as error */
306*3cde81f8SZijun Hu 	if (!hdev->get_codec_config_data)
307*3cde81f8SZijun Hu 		return 0;
308*3cde81f8SZijun Hu 
309e07a06b4SBrian Gix 	err = hdev->get_codec_config_data(hdev, ESCO_LINK, codec, &vnd_len,
310e07a06b4SBrian Gix 					  &vnd_data);
311e07a06b4SBrian Gix 	if (err < 0)
312e07a06b4SBrian Gix 		goto error;
313e07a06b4SBrian Gix 
314e07a06b4SBrian Gix 	cmd = kzalloc(sizeof(*cmd) + vnd_len, GFP_KERNEL);
315e07a06b4SBrian Gix 	if (!cmd) {
316e07a06b4SBrian Gix 		err = -ENOMEM;
317e07a06b4SBrian Gix 		goto error;
318e07a06b4SBrian Gix 	}
319e07a06b4SBrian Gix 
320e07a06b4SBrian Gix 	err = hdev->get_data_path_id(hdev, &cmd->data_path_id);
321e07a06b4SBrian Gix 	if (err < 0)
322e07a06b4SBrian Gix 		goto error;
323e07a06b4SBrian Gix 
324e07a06b4SBrian Gix 	cmd->vnd_len = vnd_len;
325e07a06b4SBrian Gix 	memcpy(cmd->vnd_data, vnd_data, vnd_len);
326e07a06b4SBrian Gix 
327e07a06b4SBrian Gix 	cmd->direction = 0x00;
328e07a06b4SBrian Gix 	__hci_cmd_sync_status(hdev, HCI_CONFIGURE_DATA_PATH,
329e07a06b4SBrian Gix 			      sizeof(*cmd) + vnd_len, cmd, HCI_CMD_TIMEOUT);
330e07a06b4SBrian Gix 
331e07a06b4SBrian Gix 	cmd->direction = 0x01;
332e07a06b4SBrian Gix 	err = __hci_cmd_sync_status(hdev, HCI_CONFIGURE_DATA_PATH,
333e07a06b4SBrian Gix 				    sizeof(*cmd) + vnd_len, cmd,
334e07a06b4SBrian Gix 				    HCI_CMD_TIMEOUT);
335e07a06b4SBrian Gix error:
336e07a06b4SBrian Gix 
337e07a06b4SBrian Gix 	kfree(cmd);
338e07a06b4SBrian Gix 	kfree(vnd_data);
339e07a06b4SBrian Gix 	return err;
340e07a06b4SBrian Gix }
341e07a06b4SBrian Gix 
342e07a06b4SBrian Gix static int hci_enhanced_setup_sync(struct hci_dev *hdev, void *data)
343e07a06b4SBrian Gix {
344e07a06b4SBrian Gix 	struct conn_handle_t *conn_handle = data;
345e07a06b4SBrian Gix 	struct hci_conn *conn = conn_handle->conn;
346e07a06b4SBrian Gix 	__u16 handle = conn_handle->handle;
347b2af264aSKiran K 	struct hci_cp_enhanced_setup_sync_conn cp;
348b2af264aSKiran K 	const struct sco_param *param;
349b2af264aSKiran K 
350e07a06b4SBrian Gix 	kfree(conn_handle);
351e07a06b4SBrian Gix 
352b2af264aSKiran K 	bt_dev_dbg(hdev, "hcon %p", conn);
353b2af264aSKiran K 
354e07a06b4SBrian Gix 	configure_datapath_sync(hdev, &conn->codec);
3559798fbdeSKiran K 
356b2af264aSKiran K 	conn->state = BT_CONNECT;
357b2af264aSKiran K 	conn->out = true;
358b2af264aSKiran K 
359b2af264aSKiran K 	conn->attempt++;
360b2af264aSKiran K 
361b2af264aSKiran K 	memset(&cp, 0x00, sizeof(cp));
362b2af264aSKiran K 
363b2af264aSKiran K 	cp.handle   = cpu_to_le16(handle);
364b2af264aSKiran K 
365b2af264aSKiran K 	cp.tx_bandwidth   = cpu_to_le32(0x00001f40);
366b2af264aSKiran K 	cp.rx_bandwidth   = cpu_to_le32(0x00001f40);
367b2af264aSKiran K 
368b2af264aSKiran K 	switch (conn->codec.id) {
369904c139aSKiran K 	case BT_CODEC_MSBC:
370904c139aSKiran K 		if (!find_next_esco_param(conn, esco_param_msbc,
371904c139aSKiran K 					  ARRAY_SIZE(esco_param_msbc)))
372e07a06b4SBrian Gix 			return -EINVAL;
373904c139aSKiran K 
374904c139aSKiran K 		param = &esco_param_msbc[conn->attempt - 1];
375904c139aSKiran K 		cp.tx_coding_format.id = 0x05;
376904c139aSKiran K 		cp.rx_coding_format.id = 0x05;
377904c139aSKiran K 		cp.tx_codec_frame_size = __cpu_to_le16(60);
378904c139aSKiran K 		cp.rx_codec_frame_size = __cpu_to_le16(60);
379904c139aSKiran K 		cp.in_bandwidth = __cpu_to_le32(32000);
380904c139aSKiran K 		cp.out_bandwidth = __cpu_to_le32(32000);
381904c139aSKiran K 		cp.in_coding_format.id = 0x04;
382904c139aSKiran K 		cp.out_coding_format.id = 0x04;
383904c139aSKiran K 		cp.in_coded_data_size = __cpu_to_le16(16);
384904c139aSKiran K 		cp.out_coded_data_size = __cpu_to_le16(16);
385904c139aSKiran K 		cp.in_pcm_data_format = 2;
386904c139aSKiran K 		cp.out_pcm_data_format = 2;
387904c139aSKiran K 		cp.in_pcm_sample_payload_msb_pos = 0;
388904c139aSKiran K 		cp.out_pcm_sample_payload_msb_pos = 0;
389904c139aSKiran K 		cp.in_data_path = conn->codec.data_path;
390904c139aSKiran K 		cp.out_data_path = conn->codec.data_path;
391904c139aSKiran K 		cp.in_transport_unit_size = 1;
392904c139aSKiran K 		cp.out_transport_unit_size = 1;
393904c139aSKiran K 		break;
394904c139aSKiran K 
395b2af264aSKiran K 	case BT_CODEC_TRANSPARENT:
396b2af264aSKiran K 		if (!find_next_esco_param(conn, esco_param_msbc,
397b2af264aSKiran K 					  ARRAY_SIZE(esco_param_msbc)))
398b2af264aSKiran K 			return false;
399b2af264aSKiran K 		param = &esco_param_msbc[conn->attempt - 1];
400b2af264aSKiran K 		cp.tx_coding_format.id = 0x03;
401b2af264aSKiran K 		cp.rx_coding_format.id = 0x03;
402b2af264aSKiran K 		cp.tx_codec_frame_size = __cpu_to_le16(60);
403b2af264aSKiran K 		cp.rx_codec_frame_size = __cpu_to_le16(60);
404b2af264aSKiran K 		cp.in_bandwidth = __cpu_to_le32(0x1f40);
405b2af264aSKiran K 		cp.out_bandwidth = __cpu_to_le32(0x1f40);
406b2af264aSKiran K 		cp.in_coding_format.id = 0x03;
407b2af264aSKiran K 		cp.out_coding_format.id = 0x03;
408b2af264aSKiran K 		cp.in_coded_data_size = __cpu_to_le16(16);
409b2af264aSKiran K 		cp.out_coded_data_size = __cpu_to_le16(16);
410b2af264aSKiran K 		cp.in_pcm_data_format = 2;
411b2af264aSKiran K 		cp.out_pcm_data_format = 2;
412b2af264aSKiran K 		cp.in_pcm_sample_payload_msb_pos = 0;
413b2af264aSKiran K 		cp.out_pcm_sample_payload_msb_pos = 0;
414b2af264aSKiran K 		cp.in_data_path = conn->codec.data_path;
415b2af264aSKiran K 		cp.out_data_path = conn->codec.data_path;
416b2af264aSKiran K 		cp.in_transport_unit_size = 1;
417b2af264aSKiran K 		cp.out_transport_unit_size = 1;
418b2af264aSKiran K 		break;
419b2af264aSKiran K 
420b2af264aSKiran K 	case BT_CODEC_CVSD:
42106149746SLuiz Augusto von Dentz 		if (conn->parent && lmp_esco_capable(conn->parent)) {
422b2af264aSKiran K 			if (!find_next_esco_param(conn, esco_param_cvsd,
423b2af264aSKiran K 						  ARRAY_SIZE(esco_param_cvsd)))
424e07a06b4SBrian Gix 				return -EINVAL;
425b2af264aSKiran K 			param = &esco_param_cvsd[conn->attempt - 1];
426b2af264aSKiran K 		} else {
427b2af264aSKiran K 			if (conn->attempt > ARRAY_SIZE(sco_param_cvsd))
428e07a06b4SBrian Gix 				return -EINVAL;
429b2af264aSKiran K 			param = &sco_param_cvsd[conn->attempt - 1];
430b2af264aSKiran K 		}
431b2af264aSKiran K 		cp.tx_coding_format.id = 2;
432b2af264aSKiran K 		cp.rx_coding_format.id = 2;
433b2af264aSKiran K 		cp.tx_codec_frame_size = __cpu_to_le16(60);
434b2af264aSKiran K 		cp.rx_codec_frame_size = __cpu_to_le16(60);
435b2af264aSKiran K 		cp.in_bandwidth = __cpu_to_le32(16000);
436b2af264aSKiran K 		cp.out_bandwidth = __cpu_to_le32(16000);
437b2af264aSKiran K 		cp.in_coding_format.id = 4;
438b2af264aSKiran K 		cp.out_coding_format.id = 4;
439b2af264aSKiran K 		cp.in_coded_data_size = __cpu_to_le16(16);
440b2af264aSKiran K 		cp.out_coded_data_size = __cpu_to_le16(16);
441b2af264aSKiran K 		cp.in_pcm_data_format = 2;
442b2af264aSKiran K 		cp.out_pcm_data_format = 2;
443b2af264aSKiran K 		cp.in_pcm_sample_payload_msb_pos = 0;
444b2af264aSKiran K 		cp.out_pcm_sample_payload_msb_pos = 0;
445b2af264aSKiran K 		cp.in_data_path = conn->codec.data_path;
446b2af264aSKiran K 		cp.out_data_path = conn->codec.data_path;
447b2af264aSKiran K 		cp.in_transport_unit_size = 16;
448b2af264aSKiran K 		cp.out_transport_unit_size = 16;
449b2af264aSKiran K 		break;
450b2af264aSKiran K 	default:
451e07a06b4SBrian Gix 		return -EINVAL;
452b2af264aSKiran K 	}
453b2af264aSKiran K 
454b2af264aSKiran K 	cp.retrans_effort = param->retrans_effort;
455b2af264aSKiran K 	cp.pkt_type = __cpu_to_le16(param->pkt_type);
456b2af264aSKiran K 	cp.max_latency = __cpu_to_le16(param->max_latency);
457b2af264aSKiran K 
458b2af264aSKiran K 	if (hci_send_cmd(hdev, HCI_OP_ENHANCED_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0)
459e07a06b4SBrian Gix 		return -EIO;
460b2af264aSKiran K 
461e07a06b4SBrian Gix 	return 0;
462b2af264aSKiran K }
463b2af264aSKiran K 
464b2af264aSKiran K static bool hci_setup_sync_conn(struct hci_conn *conn, __u16 handle)
465b6a0dc82SMarcel Holtmann {
466b6a0dc82SMarcel Holtmann 	struct hci_dev *hdev = conn->hdev;
467b6a0dc82SMarcel Holtmann 	struct hci_cp_setup_sync_conn cp;
4682dea632fSFrédéric Dalleau 	const struct sco_param *param;
469b6a0dc82SMarcel Holtmann 
470b2af264aSKiran K 	bt_dev_dbg(hdev, "hcon %p", conn);
471b6a0dc82SMarcel Holtmann 
472b6a0dc82SMarcel Holtmann 	conn->state = BT_CONNECT;
473a0c808b3SJohan Hedberg 	conn->out = true;
474b6a0dc82SMarcel Holtmann 
475efc7688bSMarcel Holtmann 	conn->attempt++;
476efc7688bSMarcel Holtmann 
477b6a0dc82SMarcel Holtmann 	cp.handle   = cpu_to_le16(handle);
478b6a0dc82SMarcel Holtmann 
479dcf4adbfSJoe Perches 	cp.tx_bandwidth   = cpu_to_le32(0x00001f40);
480dcf4adbfSJoe Perches 	cp.rx_bandwidth   = cpu_to_le32(0x00001f40);
48110c62ddcSFrédéric Dalleau 	cp.voice_setting  = cpu_to_le16(conn->setting);
48210c62ddcSFrédéric Dalleau 
48310c62ddcSFrédéric Dalleau 	switch (conn->setting & SCO_AIRMODE_MASK) {
48410c62ddcSFrédéric Dalleau 	case SCO_AIRMODE_TRANSP:
4858b1c324cSYu Liu 		if (!find_next_esco_param(conn, esco_param_msbc,
4868b1c324cSYu Liu 					  ARRAY_SIZE(esco_param_msbc)))
4872dea632fSFrédéric Dalleau 			return false;
488565766b0SJohan Hedberg 		param = &esco_param_msbc[conn->attempt - 1];
48910c62ddcSFrédéric Dalleau 		break;
49010c62ddcSFrédéric Dalleau 	case SCO_AIRMODE_CVSD:
49106149746SLuiz Augusto von Dentz 		if (conn->parent && lmp_esco_capable(conn->parent)) {
4928b1c324cSYu Liu 			if (!find_next_esco_param(conn, esco_param_cvsd,
4938b1c324cSYu Liu 						  ARRAY_SIZE(esco_param_cvsd)))
4942dea632fSFrédéric Dalleau 				return false;
49548e68ff5SBernhard Thaler 			param = &esco_param_cvsd[conn->attempt - 1];
49648e68ff5SBernhard Thaler 		} else {
49748e68ff5SBernhard Thaler 			if (conn->attempt > ARRAY_SIZE(sco_param_cvsd))
49848e68ff5SBernhard Thaler 				return false;
4992dea632fSFrédéric Dalleau 			param = &sco_param_cvsd[conn->attempt - 1];
50048e68ff5SBernhard Thaler 		}
50110c62ddcSFrédéric Dalleau 		break;
5022dea632fSFrédéric Dalleau 	default:
5032dea632fSFrédéric Dalleau 		return false;
50410c62ddcSFrédéric Dalleau 	}
505b6a0dc82SMarcel Holtmann 
506c7da5797SJohan Hedberg 	cp.retrans_effort = param->retrans_effort;
5072dea632fSFrédéric Dalleau 	cp.pkt_type = __cpu_to_le16(param->pkt_type);
5082dea632fSFrédéric Dalleau 	cp.max_latency = __cpu_to_le16(param->max_latency);
5092dea632fSFrédéric Dalleau 
5102dea632fSFrédéric Dalleau 	if (hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0)
5112dea632fSFrédéric Dalleau 		return false;
5122dea632fSFrédéric Dalleau 
5132dea632fSFrédéric Dalleau 	return true;
514b6a0dc82SMarcel Holtmann }
515b6a0dc82SMarcel Holtmann 
516b2af264aSKiran K bool hci_setup_sync(struct hci_conn *conn, __u16 handle)
517b2af264aSKiran K {
518e07a06b4SBrian Gix 	int result;
519e07a06b4SBrian Gix 	struct conn_handle_t *conn_handle;
520e07a06b4SBrian Gix 
521e07a06b4SBrian Gix 	if (enhanced_sync_conn_capable(conn->hdev)) {
522e07a06b4SBrian Gix 		conn_handle = kzalloc(sizeof(*conn_handle), GFP_KERNEL);
523e07a06b4SBrian Gix 
524e07a06b4SBrian Gix 		if (!conn_handle)
525e07a06b4SBrian Gix 			return false;
526e07a06b4SBrian Gix 
527e07a06b4SBrian Gix 		conn_handle->conn = conn;
528e07a06b4SBrian Gix 		conn_handle->handle = handle;
529e07a06b4SBrian Gix 		result = hci_cmd_sync_queue(conn->hdev, hci_enhanced_setup_sync,
530e07a06b4SBrian Gix 					    conn_handle, NULL);
531e07a06b4SBrian Gix 		if (result < 0)
532e07a06b4SBrian Gix 			kfree(conn_handle);
533e07a06b4SBrian Gix 
534e07a06b4SBrian Gix 		return result == 0;
535e07a06b4SBrian Gix 	}
536b2af264aSKiran K 
537b2af264aSKiran K 	return hci_setup_sync_conn(conn, handle);
538b2af264aSKiran K }
539b2af264aSKiran K 
5407d6ca693SJohan Hedberg u8 hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency,
5417d6ca693SJohan Hedberg 		      u16 to_multiplier)
5422ce603ebSClaudio Takahasi {
5432ce603ebSClaudio Takahasi 	struct hci_dev *hdev = conn->hdev;
544f044eb05SMarcel Holtmann 	struct hci_conn_params *params;
545f044eb05SMarcel Holtmann 	struct hci_cp_le_conn_update cp;
546f044eb05SMarcel Holtmann 
547f044eb05SMarcel Holtmann 	hci_dev_lock(hdev);
548f044eb05SMarcel Holtmann 
549f044eb05SMarcel Holtmann 	params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
550f044eb05SMarcel Holtmann 	if (params) {
551f044eb05SMarcel Holtmann 		params->conn_min_interval = min;
552f044eb05SMarcel Holtmann 		params->conn_max_interval = max;
553f044eb05SMarcel Holtmann 		params->conn_latency = latency;
554f044eb05SMarcel Holtmann 		params->supervision_timeout = to_multiplier;
555f044eb05SMarcel Holtmann 	}
556f044eb05SMarcel Holtmann 
557f044eb05SMarcel Holtmann 	hci_dev_unlock(hdev);
5582ce603ebSClaudio Takahasi 
5592ce603ebSClaudio Takahasi 	memset(&cp, 0, sizeof(cp));
5602ce603ebSClaudio Takahasi 	cp.handle		= cpu_to_le16(conn->handle);
5612ce603ebSClaudio Takahasi 	cp.conn_interval_min	= cpu_to_le16(min);
5622ce603ebSClaudio Takahasi 	cp.conn_interval_max	= cpu_to_le16(max);
5632ce603ebSClaudio Takahasi 	cp.conn_latency		= cpu_to_le16(latency);
5642ce603ebSClaudio Takahasi 	cp.supervision_timeout	= cpu_to_le16(to_multiplier);
565dcf4adbfSJoe Perches 	cp.min_ce_len		= cpu_to_le16(0x0000);
566dcf4adbfSJoe Perches 	cp.max_ce_len		= cpu_to_le16(0x0000);
5672ce603ebSClaudio Takahasi 
5682ce603ebSClaudio Takahasi 	hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp);
5697d6ca693SJohan Hedberg 
5707d6ca693SJohan Hedberg 	if (params)
5717d6ca693SJohan Hedberg 		return 0x01;
5727d6ca693SJohan Hedberg 
5737d6ca693SJohan Hedberg 	return 0x00;
5742ce603ebSClaudio Takahasi }
5752ce603ebSClaudio Takahasi 
576fe39c7b2SMarcel Holtmann void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand,
5778b76ce34SJohan Hedberg 		      __u8 ltk[16], __u8 key_size)
578a7a595f6SVinicius Costa Gomes {
579a7a595f6SVinicius Costa Gomes 	struct hci_dev *hdev = conn->hdev;
580a7a595f6SVinicius Costa Gomes 	struct hci_cp_le_start_enc cp;
581a7a595f6SVinicius Costa Gomes 
58238b3fef1SAndrei Emeltchenko 	BT_DBG("hcon %p", conn);
583a7a595f6SVinicius Costa Gomes 
584a7a595f6SVinicius Costa Gomes 	memset(&cp, 0, sizeof(cp));
585a7a595f6SVinicius Costa Gomes 
586a7a595f6SVinicius Costa Gomes 	cp.handle = cpu_to_le16(conn->handle);
587fe39c7b2SMarcel Holtmann 	cp.rand = rand;
588a7a595f6SVinicius Costa Gomes 	cp.ediv = ediv;
5898b76ce34SJohan Hedberg 	memcpy(cp.ltk, ltk, key_size);
590a7a595f6SVinicius Costa Gomes 
591a7a595f6SVinicius Costa Gomes 	hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp);
592a7a595f6SVinicius Costa Gomes }
593a7a595f6SVinicius Costa Gomes 
594e73439d8SMarcel Holtmann /* Device _must_ be locked */
595e73439d8SMarcel Holtmann void hci_sco_setup(struct hci_conn *conn, __u8 status)
596e73439d8SMarcel Holtmann {
59706149746SLuiz Augusto von Dentz 	struct hci_link *link;
598e73439d8SMarcel Holtmann 
59906149746SLuiz Augusto von Dentz 	link = list_first_entry_or_null(&conn->link_list, struct hci_link, list);
60006149746SLuiz Augusto von Dentz 	if (!link || !link->conn)
601e73439d8SMarcel Holtmann 		return;
602e73439d8SMarcel Holtmann 
60338b3fef1SAndrei Emeltchenko 	BT_DBG("hcon %p", conn);
60438b3fef1SAndrei Emeltchenko 
605e73439d8SMarcel Holtmann 	if (!status) {
606e73439d8SMarcel Holtmann 		if (lmp_esco_capable(conn->hdev))
60706149746SLuiz Augusto von Dentz 			hci_setup_sync(link->conn, conn->handle);
608e73439d8SMarcel Holtmann 		else
60906149746SLuiz Augusto von Dentz 			hci_add_sco(link->conn, conn->handle);
610e73439d8SMarcel Holtmann 	} else {
61106149746SLuiz Augusto von Dentz 		hci_connect_cfm(link->conn, status);
61206149746SLuiz Augusto von Dentz 		hci_conn_del(link->conn);
613e73439d8SMarcel Holtmann 	}
614e73439d8SMarcel Holtmann }
615e73439d8SMarcel Holtmann 
61619c40e3bSGustavo F. Padovan static void hci_conn_timeout(struct work_struct *work)
6171da177e4SLinus Torvalds {
61819c40e3bSGustavo F. Padovan 	struct hci_conn *conn = container_of(work, struct hci_conn,
61919c40e3bSGustavo F. Padovan 					     disc_work.work);
6201d56dc4fSLukasz Rymanowski 	int refcnt = atomic_read(&conn->refcnt);
6211da177e4SLinus Torvalds 
62238b3fef1SAndrei Emeltchenko 	BT_DBG("hcon %p state %s", conn, state_to_string(conn->state));
6231da177e4SLinus Torvalds 
6241d56dc4fSLukasz Rymanowski 	WARN_ON(refcnt < 0);
6251d56dc4fSLukasz Rymanowski 
6261d56dc4fSLukasz Rymanowski 	/* FIXME: It was observed that in pairing failed scenario, refcnt
6271d56dc4fSLukasz Rymanowski 	 * drops below 0. Probably this is because l2cap_conn_del calls
6281d56dc4fSLukasz Rymanowski 	 * l2cap_chan_del for each channel, and inside l2cap_chan_del conn is
6291d56dc4fSLukasz Rymanowski 	 * dropped. After that loop hci_chan_del is called which also drops
6301d56dc4fSLukasz Rymanowski 	 * conn. For now make sure that ACL is alive if refcnt is higher then 0,
6311d56dc4fSLukasz Rymanowski 	 * otherwise drop it.
6321d56dc4fSLukasz Rymanowski 	 */
6331d56dc4fSLukasz Rymanowski 	if (refcnt > 0)
6341da177e4SLinus Torvalds 		return;
6351da177e4SLinus Torvalds 
63689e0ccc8SJohan Hedberg 	hci_abort_conn(conn, hci_proto_disconn_ind(conn));
6371da177e4SLinus Torvalds }
6381da177e4SLinus Torvalds 
639416dc94bSGustavo F. Padovan /* Enter sniff mode */
640a74a84f6SJohan Hedberg static void hci_conn_idle(struct work_struct *work)
641416dc94bSGustavo F. Padovan {
642a74a84f6SJohan Hedberg 	struct hci_conn *conn = container_of(work, struct hci_conn,
643a74a84f6SJohan Hedberg 					     idle_work.work);
644416dc94bSGustavo F. Padovan 	struct hci_dev *hdev = conn->hdev;
645416dc94bSGustavo F. Padovan 
64638b3fef1SAndrei Emeltchenko 	BT_DBG("hcon %p mode %d", conn, conn->mode);
647416dc94bSGustavo F. Padovan 
648416dc94bSGustavo F. Padovan 	if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
649416dc94bSGustavo F. Padovan 		return;
650416dc94bSGustavo F. Padovan 
651416dc94bSGustavo F. Padovan 	if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF))
652416dc94bSGustavo F. Padovan 		return;
653416dc94bSGustavo F. Padovan 
654416dc94bSGustavo F. Padovan 	if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
655416dc94bSGustavo F. Padovan 		struct hci_cp_sniff_subrate cp;
656416dc94bSGustavo F. Padovan 		cp.handle             = cpu_to_le16(conn->handle);
657dcf4adbfSJoe Perches 		cp.max_latency        = cpu_to_le16(0);
658dcf4adbfSJoe Perches 		cp.min_remote_timeout = cpu_to_le16(0);
659dcf4adbfSJoe Perches 		cp.min_local_timeout  = cpu_to_le16(0);
660416dc94bSGustavo F. Padovan 		hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
661416dc94bSGustavo F. Padovan 	}
662416dc94bSGustavo F. Padovan 
66351a8efd7SJohan Hedberg 	if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) {
664416dc94bSGustavo F. Padovan 		struct hci_cp_sniff_mode cp;
665416dc94bSGustavo F. Padovan 		cp.handle       = cpu_to_le16(conn->handle);
666416dc94bSGustavo F. Padovan 		cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
667416dc94bSGustavo F. Padovan 		cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
668dcf4adbfSJoe Perches 		cp.attempt      = cpu_to_le16(4);
669dcf4adbfSJoe Perches 		cp.timeout      = cpu_to_le16(1);
670416dc94bSGustavo F. Padovan 		hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
671416dc94bSGustavo F. Padovan 	}
672416dc94bSGustavo F. Padovan }
673416dc94bSGustavo F. Padovan 
6747bc18d9dSJohan Hedberg static void hci_conn_auto_accept(struct work_struct *work)
6759f61656aSJohan Hedberg {
6767bc18d9dSJohan Hedberg 	struct hci_conn *conn = container_of(work, struct hci_conn,
6777bc18d9dSJohan Hedberg 					     auto_accept_work.work);
6789f61656aSJohan Hedberg 
6797bc18d9dSJohan Hedberg 	hci_send_cmd(conn->hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst),
6809f61656aSJohan Hedberg 		     &conn->dst);
6819f61656aSJohan Hedberg }
6829f61656aSJohan Hedberg 
683c3bed4deSSathish Narsimman static void le_disable_advertising(struct hci_dev *hdev)
684c3bed4deSSathish Narsimman {
685c3bed4deSSathish Narsimman 	if (ext_adv_capable(hdev)) {
686c3bed4deSSathish Narsimman 		struct hci_cp_le_set_ext_adv_enable cp;
687c3bed4deSSathish Narsimman 
688c3bed4deSSathish Narsimman 		cp.enable = 0x00;
689c3bed4deSSathish Narsimman 		cp.num_of_sets = 0x00;
690c3bed4deSSathish Narsimman 
691c3bed4deSSathish Narsimman 		hci_send_cmd(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE, sizeof(cp),
692c3bed4deSSathish Narsimman 			     &cp);
693c3bed4deSSathish Narsimman 	} else {
694c3bed4deSSathish Narsimman 		u8 enable = 0x00;
695c3bed4deSSathish Narsimman 		hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable),
696c3bed4deSSathish Narsimman 			     &enable);
697c3bed4deSSathish Narsimman 	}
698c3bed4deSSathish Narsimman }
699c3bed4deSSathish Narsimman 
7009489eca4SJohan Hedberg static void le_conn_timeout(struct work_struct *work)
7019489eca4SJohan Hedberg {
7029489eca4SJohan Hedberg 	struct hci_conn *conn = container_of(work, struct hci_conn,
7039489eca4SJohan Hedberg 					     le_conn_timeout.work);
7043c857757SJohan Hedberg 	struct hci_dev *hdev = conn->hdev;
7059489eca4SJohan Hedberg 
7069489eca4SJohan Hedberg 	BT_DBG("");
7079489eca4SJohan Hedberg 
7083c857757SJohan Hedberg 	/* We could end up here due to having done directed advertising,
7093c857757SJohan Hedberg 	 * so clean up the state if necessary. This should however only
7103c857757SJohan Hedberg 	 * happen with broken hardware or if low duty cycle was used
7113c857757SJohan Hedberg 	 * (which doesn't have a timeout of its own).
7123c857757SJohan Hedberg 	 */
7130b1db38cSJohan Hedberg 	if (conn->role == HCI_ROLE_SLAVE) {
714c3bed4deSSathish Narsimman 		/* Disable LE Advertising */
715c3bed4deSSathish Narsimman 		le_disable_advertising(hdev);
7169fa6b4cdSNiels Dossche 		hci_dev_lock(hdev);
7179b3628d7SLuiz Augusto von Dentz 		hci_conn_failed(conn, HCI_ERROR_ADVERTISING_TIMEOUT);
7189fa6b4cdSNiels Dossche 		hci_dev_unlock(hdev);
7193c857757SJohan Hedberg 		return;
7203c857757SJohan Hedberg 	}
7213c857757SJohan Hedberg 
72289e0ccc8SJohan Hedberg 	hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM);
7239489eca4SJohan Hedberg }
7249489eca4SJohan Hedberg 
7256b9545dcSPauli Virtanen struct iso_cig_params {
7266b9545dcSPauli Virtanen 	struct hci_cp_le_set_cig_params cp;
7276b9545dcSPauli Virtanen 	struct hci_cis_params cis[0x1f];
7286b9545dcSPauli Virtanen };
7296b9545dcSPauli Virtanen 
730eca0ae4aSLuiz Augusto von Dentz struct iso_list_data {
731eca0ae4aSLuiz Augusto von Dentz 	union {
732eca0ae4aSLuiz Augusto von Dentz 		u8  cig;
733eca0ae4aSLuiz Augusto von Dentz 		u8  big;
734eca0ae4aSLuiz Augusto von Dentz 	};
735eca0ae4aSLuiz Augusto von Dentz 	union {
736eca0ae4aSLuiz Augusto von Dentz 		u8  cis;
737eca0ae4aSLuiz Augusto von Dentz 		u8  bis;
738eca0ae4aSLuiz Augusto von Dentz 		u16 sync_handle;
739eca0ae4aSLuiz Augusto von Dentz 	};
740eca0ae4aSLuiz Augusto von Dentz 	int count;
741a0bfde16SIulia Tanasescu 	bool big_term;
742fbdc4bc4SIulia Tanasescu 	bool pa_sync_term;
743f777d882SIulia Tanasescu 	bool big_sync_term;
744eca0ae4aSLuiz Augusto von Dentz };
745eca0ae4aSLuiz Augusto von Dentz 
746eca0ae4aSLuiz Augusto von Dentz static void bis_list(struct hci_conn *conn, void *data)
747eca0ae4aSLuiz Augusto von Dentz {
748eca0ae4aSLuiz Augusto von Dentz 	struct iso_list_data *d = data;
749eca0ae4aSLuiz Augusto von Dentz 
750eca0ae4aSLuiz Augusto von Dentz 	/* Skip if not broadcast/ANY address */
751eca0ae4aSLuiz Augusto von Dentz 	if (bacmp(&conn->dst, BDADDR_ANY))
752eca0ae4aSLuiz Augusto von Dentz 		return;
753eca0ae4aSLuiz Augusto von Dentz 
7540fe8c8d0SIulia Tanasescu 	if (d->big != conn->iso_qos.bcast.big || d->bis == BT_ISO_QOS_BIS_UNSET ||
7550fe8c8d0SIulia Tanasescu 	    d->bis != conn->iso_qos.bcast.bis)
756eca0ae4aSLuiz Augusto von Dentz 		return;
757eca0ae4aSLuiz Augusto von Dentz 
758eca0ae4aSLuiz Augusto von Dentz 	d->count++;
759eca0ae4aSLuiz Augusto von Dentz }
760eca0ae4aSLuiz Augusto von Dentz 
761eca0ae4aSLuiz Augusto von Dentz static int terminate_big_sync(struct hci_dev *hdev, void *data)
762eca0ae4aSLuiz Augusto von Dentz {
763eca0ae4aSLuiz Augusto von Dentz 	struct iso_list_data *d = data;
764eca0ae4aSLuiz Augusto von Dentz 
765eca0ae4aSLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "big 0x%2.2x bis 0x%2.2x", d->big, d->bis);
766eca0ae4aSLuiz Augusto von Dentz 
767f03d3322SIulia Tanasescu 	hci_disable_per_advertising_sync(hdev, d->bis);
768eca0ae4aSLuiz Augusto von Dentz 	hci_remove_ext_adv_instance_sync(hdev, d->bis, NULL);
769eca0ae4aSLuiz Augusto von Dentz 
770a0bfde16SIulia Tanasescu 	/* Only terminate BIG if it has been created */
771a0bfde16SIulia Tanasescu 	if (!d->big_term)
772eca0ae4aSLuiz Augusto von Dentz 		return 0;
773eca0ae4aSLuiz Augusto von Dentz 
774eca0ae4aSLuiz Augusto von Dentz 	return hci_le_terminate_big_sync(hdev, d->big,
775eca0ae4aSLuiz Augusto von Dentz 					 HCI_ERROR_LOCAL_HOST_TERM);
776eca0ae4aSLuiz Augusto von Dentz }
777eca0ae4aSLuiz Augusto von Dentz 
778eca0ae4aSLuiz Augusto von Dentz static void terminate_big_destroy(struct hci_dev *hdev, void *data, int err)
779eca0ae4aSLuiz Augusto von Dentz {
780eca0ae4aSLuiz Augusto von Dentz 	kfree(data);
781eca0ae4aSLuiz Augusto von Dentz }
782eca0ae4aSLuiz Augusto von Dentz 
783a0bfde16SIulia Tanasescu static int hci_le_terminate_big(struct hci_dev *hdev, struct hci_conn *conn)
784eca0ae4aSLuiz Augusto von Dentz {
785eca0ae4aSLuiz Augusto von Dentz 	struct iso_list_data *d;
7863aa21311SZhengchao Shao 	int ret;
787eca0ae4aSLuiz Augusto von Dentz 
788a0bfde16SIulia Tanasescu 	bt_dev_dbg(hdev, "big 0x%2.2x bis 0x%2.2x", conn->iso_qos.bcast.big,
789a0bfde16SIulia Tanasescu 		   conn->iso_qos.bcast.bis);
790eca0ae4aSLuiz Augusto von Dentz 
7913958e877SKang Minchul 	d = kzalloc(sizeof(*d), GFP_KERNEL);
792eca0ae4aSLuiz Augusto von Dentz 	if (!d)
793eca0ae4aSLuiz Augusto von Dentz 		return -ENOMEM;
794eca0ae4aSLuiz Augusto von Dentz 
795a0bfde16SIulia Tanasescu 	d->big = conn->iso_qos.bcast.big;
796a0bfde16SIulia Tanasescu 	d->bis = conn->iso_qos.bcast.bis;
797a0bfde16SIulia Tanasescu 	d->big_term = test_and_clear_bit(HCI_CONN_BIG_CREATED, &conn->flags);
798eca0ae4aSLuiz Augusto von Dentz 
7993aa21311SZhengchao Shao 	ret = hci_cmd_sync_queue(hdev, terminate_big_sync, d,
800eca0ae4aSLuiz Augusto von Dentz 				 terminate_big_destroy);
8013aa21311SZhengchao Shao 	if (ret)
8023aa21311SZhengchao Shao 		kfree(d);
8033aa21311SZhengchao Shao 
8043aa21311SZhengchao Shao 	return ret;
805eca0ae4aSLuiz Augusto von Dentz }
806eca0ae4aSLuiz Augusto von Dentz 
807eca0ae4aSLuiz Augusto von Dentz static int big_terminate_sync(struct hci_dev *hdev, void *data)
808eca0ae4aSLuiz Augusto von Dentz {
809eca0ae4aSLuiz Augusto von Dentz 	struct iso_list_data *d = data;
810eca0ae4aSLuiz Augusto von Dentz 
811eca0ae4aSLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "big 0x%2.2x sync_handle 0x%4.4x", d->big,
812eca0ae4aSLuiz Augusto von Dentz 		   d->sync_handle);
813eca0ae4aSLuiz Augusto von Dentz 
814f777d882SIulia Tanasescu 	if (d->big_sync_term)
815eca0ae4aSLuiz Augusto von Dentz 		hci_le_big_terminate_sync(hdev, d->big);
816eca0ae4aSLuiz Augusto von Dentz 
817fbdc4bc4SIulia Tanasescu 	if (d->pa_sync_term)
818eca0ae4aSLuiz Augusto von Dentz 		return hci_le_pa_terminate_sync(hdev, d->sync_handle);
819fbdc4bc4SIulia Tanasescu 
820fbdc4bc4SIulia Tanasescu 	return 0;
821eca0ae4aSLuiz Augusto von Dentz }
822eca0ae4aSLuiz Augusto von Dentz 
823f777d882SIulia Tanasescu static int hci_le_big_terminate(struct hci_dev *hdev, u8 big, struct hci_conn *conn)
824eca0ae4aSLuiz Augusto von Dentz {
825eca0ae4aSLuiz Augusto von Dentz 	struct iso_list_data *d;
8263aa21311SZhengchao Shao 	int ret;
827eca0ae4aSLuiz Augusto von Dentz 
828f777d882SIulia Tanasescu 	bt_dev_dbg(hdev, "big 0x%2.2x sync_handle 0x%4.4x", big, conn->sync_handle);
829eca0ae4aSLuiz Augusto von Dentz 
8303958e877SKang Minchul 	d = kzalloc(sizeof(*d), GFP_KERNEL);
831eca0ae4aSLuiz Augusto von Dentz 	if (!d)
832eca0ae4aSLuiz Augusto von Dentz 		return -ENOMEM;
833eca0ae4aSLuiz Augusto von Dentz 
834eca0ae4aSLuiz Augusto von Dentz 	d->big = big;
835f777d882SIulia Tanasescu 	d->sync_handle = conn->sync_handle;
836fbdc4bc4SIulia Tanasescu 	d->pa_sync_term = test_and_clear_bit(HCI_CONN_PA_SYNC, &conn->flags);
837f777d882SIulia Tanasescu 	d->big_sync_term = test_and_clear_bit(HCI_CONN_BIG_SYNC, &conn->flags);
838eca0ae4aSLuiz Augusto von Dentz 
8393aa21311SZhengchao Shao 	ret = hci_cmd_sync_queue(hdev, big_terminate_sync, d,
840eca0ae4aSLuiz Augusto von Dentz 				 terminate_big_destroy);
8413aa21311SZhengchao Shao 	if (ret)
8423aa21311SZhengchao Shao 		kfree(d);
8433aa21311SZhengchao Shao 
8443aa21311SZhengchao Shao 	return ret;
845eca0ae4aSLuiz Augusto von Dentz }
846eca0ae4aSLuiz Augusto von Dentz 
847eca0ae4aSLuiz Augusto von Dentz /* Cleanup BIS connection
848eca0ae4aSLuiz Augusto von Dentz  *
849eca0ae4aSLuiz Augusto von Dentz  * Detects if there any BIS left connected in a BIG
850eca0ae4aSLuiz Augusto von Dentz  * broadcaster: Remove advertising instance and terminate BIG.
851eca0ae4aSLuiz Augusto von Dentz  * broadcaster receiver: Teminate BIG sync and terminate PA sync.
852eca0ae4aSLuiz Augusto von Dentz  */
853eca0ae4aSLuiz Augusto von Dentz static void bis_cleanup(struct hci_conn *conn)
854eca0ae4aSLuiz Augusto von Dentz {
855eca0ae4aSLuiz Augusto von Dentz 	struct hci_dev *hdev = conn->hdev;
856a0bfde16SIulia Tanasescu 	struct hci_conn *bis;
857eca0ae4aSLuiz Augusto von Dentz 
858eca0ae4aSLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "conn %p", conn);
859eca0ae4aSLuiz Augusto von Dentz 
860eca0ae4aSLuiz Augusto von Dentz 	if (conn->role == HCI_ROLE_MASTER) {
861eca0ae4aSLuiz Augusto von Dentz 		if (!test_and_clear_bit(HCI_CONN_PER_ADV, &conn->flags))
862eca0ae4aSLuiz Augusto von Dentz 			return;
863eca0ae4aSLuiz Augusto von Dentz 
864a0bfde16SIulia Tanasescu 		/* Check if ISO connection is a BIS and terminate advertising
865a0bfde16SIulia Tanasescu 		 * set and BIG if there are no other connections using it.
866a0bfde16SIulia Tanasescu 		 */
8676a42e9bfSIulia Tanasescu 		bis = hci_conn_hash_lookup_big(hdev, conn->iso_qos.bcast.big);
868a0bfde16SIulia Tanasescu 		if (bis)
869a0bfde16SIulia Tanasescu 			return;
870a0bfde16SIulia Tanasescu 
871a0bfde16SIulia Tanasescu 		hci_le_terminate_big(hdev, conn);
872eca0ae4aSLuiz Augusto von Dentz 	} else {
873f777d882SIulia Tanasescu 		bis = hci_conn_hash_lookup_big_any_dst(hdev,
874f777d882SIulia Tanasescu 						       conn->iso_qos.bcast.big);
875f777d882SIulia Tanasescu 
876f777d882SIulia Tanasescu 		if (bis)
877f777d882SIulia Tanasescu 			return;
878f777d882SIulia Tanasescu 
8790fe8c8d0SIulia Tanasescu 		hci_le_big_terminate(hdev, conn->iso_qos.bcast.big,
880f777d882SIulia Tanasescu 				     conn);
881eca0ae4aSLuiz Augusto von Dentz 	}
882eca0ae4aSLuiz Augusto von Dentz }
883eca0ae4aSLuiz Augusto von Dentz 
884eca0ae4aSLuiz Augusto von Dentz static int remove_cig_sync(struct hci_dev *hdev, void *data)
885eca0ae4aSLuiz Augusto von Dentz {
886a1f6c3aeSLuiz Augusto von Dentz 	u8 handle = PTR_UINT(data);
887eca0ae4aSLuiz Augusto von Dentz 
888eca0ae4aSLuiz Augusto von Dentz 	return hci_le_remove_cig_sync(hdev, handle);
889eca0ae4aSLuiz Augusto von Dentz }
890eca0ae4aSLuiz Augusto von Dentz 
891eca0ae4aSLuiz Augusto von Dentz static int hci_le_remove_cig(struct hci_dev *hdev, u8 handle)
892eca0ae4aSLuiz Augusto von Dentz {
893eca0ae4aSLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "handle 0x%2.2x", handle);
894eca0ae4aSLuiz Augusto von Dentz 
895a1f6c3aeSLuiz Augusto von Dentz 	return hci_cmd_sync_queue(hdev, remove_cig_sync, UINT_PTR(handle),
896a1f6c3aeSLuiz Augusto von Dentz 				  NULL);
897eca0ae4aSLuiz Augusto von Dentz }
898eca0ae4aSLuiz Augusto von Dentz 
899eca0ae4aSLuiz Augusto von Dentz static void find_cis(struct hci_conn *conn, void *data)
900eca0ae4aSLuiz Augusto von Dentz {
901eca0ae4aSLuiz Augusto von Dentz 	struct iso_list_data *d = data;
902eca0ae4aSLuiz Augusto von Dentz 
90331c5f916SPauli Virtanen 	/* Ignore broadcast or if CIG don't match */
90431c5f916SPauli Virtanen 	if (!bacmp(&conn->dst, BDADDR_ANY) || d->cig != conn->iso_qos.ucast.cig)
905eca0ae4aSLuiz Augusto von Dentz 		return;
906eca0ae4aSLuiz Augusto von Dentz 
907eca0ae4aSLuiz Augusto von Dentz 	d->count++;
908eca0ae4aSLuiz Augusto von Dentz }
909eca0ae4aSLuiz Augusto von Dentz 
910eca0ae4aSLuiz Augusto von Dentz /* Cleanup CIS connection:
911eca0ae4aSLuiz Augusto von Dentz  *
912eca0ae4aSLuiz Augusto von Dentz  * Detects if there any CIS left connected in a CIG and remove it.
913eca0ae4aSLuiz Augusto von Dentz  */
914eca0ae4aSLuiz Augusto von Dentz static void cis_cleanup(struct hci_conn *conn)
915eca0ae4aSLuiz Augusto von Dentz {
916eca0ae4aSLuiz Augusto von Dentz 	struct hci_dev *hdev = conn->hdev;
917eca0ae4aSLuiz Augusto von Dentz 	struct iso_list_data d;
918eca0ae4aSLuiz Augusto von Dentz 
91931c5f916SPauli Virtanen 	if (conn->iso_qos.ucast.cig == BT_ISO_QOS_CIG_UNSET)
92031c5f916SPauli Virtanen 		return;
92131c5f916SPauli Virtanen 
922eca0ae4aSLuiz Augusto von Dentz 	memset(&d, 0, sizeof(d));
9230fe8c8d0SIulia Tanasescu 	d.cig = conn->iso_qos.ucast.cig;
924eca0ae4aSLuiz Augusto von Dentz 
925eca0ae4aSLuiz Augusto von Dentz 	/* Check if ISO connection is a CIS and remove CIG if there are
926eca0ae4aSLuiz Augusto von Dentz 	 * no other connections using it.
927eca0ae4aSLuiz Augusto von Dentz 	 */
9286c242c64SPauli Virtanen 	hci_conn_hash_list_state(hdev, find_cis, ISO_LINK, BT_BOUND, &d);
9296c242c64SPauli Virtanen 	hci_conn_hash_list_state(hdev, find_cis, ISO_LINK, BT_CONNECT, &d);
930eca0ae4aSLuiz Augusto von Dentz 	hci_conn_hash_list_state(hdev, find_cis, ISO_LINK, BT_CONNECTED, &d);
931eca0ae4aSLuiz Augusto von Dentz 	if (d.count)
932eca0ae4aSLuiz Augusto von Dentz 		return;
933eca0ae4aSLuiz Augusto von Dentz 
9340fe8c8d0SIulia Tanasescu 	hci_le_remove_cig(hdev, conn->iso_qos.ucast.cig);
935eca0ae4aSLuiz Augusto von Dentz }
936eca0ae4aSLuiz Augusto von Dentz 
93784cb0143SZiyang Xuan static int hci_conn_hash_alloc_unset(struct hci_dev *hdev)
9389f78191cSLuiz Augusto von Dentz {
93984cb0143SZiyang Xuan 	return ida_alloc_range(&hdev->unset_handle_ida, HCI_CONN_HANDLE_MAX + 1,
94084cb0143SZiyang Xuan 			       U16_MAX, GFP_ATOMIC);
9419f78191cSLuiz Augusto von Dentz }
9429f78191cSLuiz Augusto von Dentz 
9434970e48fSPavel Skripkin static struct hci_conn *__hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst,
94484cb0143SZiyang Xuan 				       u8 role, u16 handle)
9451da177e4SLinus Torvalds {
9461da177e4SLinus Torvalds 	struct hci_conn *conn;
9471da177e4SLinus Torvalds 
948ad3f7986SSungwoo Kim 	switch (type) {
949ad3f7986SSungwoo Kim 	case ACL_LINK:
950ad3f7986SSungwoo Kim 		if (!hdev->acl_mtu)
951ad3f7986SSungwoo Kim 			return ERR_PTR(-ECONNREFUSED);
952ad3f7986SSungwoo Kim 		break;
953ad3f7986SSungwoo Kim 	case ISO_LINK:
954ad3f7986SSungwoo Kim 		if (hdev->iso_mtu)
955ad3f7986SSungwoo Kim 			/* Dedicated ISO Buffer exists */
956ad3f7986SSungwoo Kim 			break;
957ad3f7986SSungwoo Kim 		fallthrough;
958ad3f7986SSungwoo Kim 	case LE_LINK:
959ad3f7986SSungwoo Kim 		if (hdev->le_mtu && hdev->le_mtu < HCI_MIN_LE_MTU)
960ad3f7986SSungwoo Kim 			return ERR_PTR(-ECONNREFUSED);
961ad3f7986SSungwoo Kim 		if (!hdev->le_mtu && hdev->acl_mtu < HCI_MIN_LE_MTU)
962ad3f7986SSungwoo Kim 			return ERR_PTR(-ECONNREFUSED);
963ad3f7986SSungwoo Kim 		break;
964ad3f7986SSungwoo Kim 	case SCO_LINK:
965ad3f7986SSungwoo Kim 	case ESCO_LINK:
966ad3f7986SSungwoo Kim 		if (!hdev->sco_pkts)
967ad3f7986SSungwoo Kim 			/* Controller does not support SCO or eSCO over HCI */
968ad3f7986SSungwoo Kim 			return ERR_PTR(-ECONNREFUSED);
969ad3f7986SSungwoo Kim 		break;
970ad3f7986SSungwoo Kim 	default:
971ad3f7986SSungwoo Kim 		return ERR_PTR(-ECONNREFUSED);
972ad3f7986SSungwoo Kim 	}
973ad3f7986SSungwoo Kim 
97484cb0143SZiyang Xuan 	bt_dev_dbg(hdev, "dst %pMR handle 0x%4.4x", dst, handle);
9751da177e4SLinus Torvalds 
97627f70f3eSJohan Hedberg 	conn = kzalloc(sizeof(*conn), GFP_KERNEL);
97704837f64SMarcel Holtmann 	if (!conn)
978ad3f7986SSungwoo Kim 		return ERR_PTR(-ENOMEM);
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	bacpy(&conn->dst, dst);
981662e8820SMarcel Holtmann 	bacpy(&conn->src, &hdev->bdaddr);
98284cb0143SZiyang Xuan 	conn->handle = handle;
9831da177e4SLinus Torvalds 	conn->hdev  = hdev;
98404837f64SMarcel Holtmann 	conn->type  = type;
985a5c4e309SJohan Hedberg 	conn->role  = role;
98604837f64SMarcel Holtmann 	conn->mode  = HCI_CM_ACTIVE;
9871da177e4SLinus Torvalds 	conn->state = BT_OPEN;
98893f19c9fSAndrei Emeltchenko 	conn->auth_type = HCI_AT_GENERAL_BONDING;
98917fa4b9dSJohan Hedberg 	conn->io_capability = hdev->io_capability;
990a9583556SJohan Hedberg 	conn->remote_auth = 0xff;
99113d39315SWaldemar Rymarkiewicz 	conn->key_type = 0xff;
992ebf86aa3SJohan Hedberg 	conn->rssi = HCI_RSSI_INVALID;
9935a134faeSAndrzej Kaczmarek 	conn->tx_power = HCI_TX_POWER_INVALID;
994d0455ed9SAndrzej Kaczmarek 	conn->max_tx_power = HCI_TX_POWER_INVALID;
99523417475SIulia Tanasescu 	conn->sync_handle = HCI_SYNC_HANDLE_INVALID;
9961da177e4SLinus Torvalds 
99758a681efSJohan Hedberg 	set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
998052b30b0SMarcel Holtmann 	conn->disc_timeout = HCI_DISCONN_TIMEOUT;
99904837f64SMarcel Holtmann 
1000302975cbSSpoorthi Ravishankar Koppad 	/* Set Default Authenticated payload timeout to 30s */
1001302975cbSSpoorthi Ravishankar Koppad 	conn->auth_payload_timeout = DEFAULT_AUTH_PAYLOAD_TIMEOUT;
1002302975cbSSpoorthi Ravishankar Koppad 
1003a5c4e309SJohan Hedberg 	if (conn->role == HCI_ROLE_MASTER)
1004a5c4e309SJohan Hedberg 		conn->out = true;
1005a5c4e309SJohan Hedberg 
1006a8746417SMarcel Holtmann 	switch (type) {
1007a8746417SMarcel Holtmann 	case ACL_LINK:
1008a8746417SMarcel Holtmann 		conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
1009ad3f7986SSungwoo Kim 		conn->mtu = hdev->acl_mtu;
1010a8746417SMarcel Holtmann 		break;
10119c84d1daSJohan Hedberg 	case LE_LINK:
1012eca0ae4aSLuiz Augusto von Dentz 		/* conn->src should reflect the local identity address */
1013eca0ae4aSLuiz Augusto von Dentz 		hci_copy_identity_address(hdev, &conn->src, &conn->src_type);
1014ad3f7986SSungwoo Kim 		conn->mtu = hdev->le_mtu ? hdev->le_mtu : hdev->acl_mtu;
1015eca0ae4aSLuiz Augusto von Dentz 		break;
101626afbd82SLuiz Augusto von Dentz 	case ISO_LINK:
10179c84d1daSJohan Hedberg 		/* conn->src should reflect the local identity address */
10189c84d1daSJohan Hedberg 		hci_copy_identity_address(hdev, &conn->src, &conn->src_type);
1019eca0ae4aSLuiz Augusto von Dentz 
1020eca0ae4aSLuiz Augusto von Dentz 		/* set proper cleanup function */
1021eca0ae4aSLuiz Augusto von Dentz 		if (!bacmp(dst, BDADDR_ANY))
1022eca0ae4aSLuiz Augusto von Dentz 			conn->cleanup = bis_cleanup;
1023eca0ae4aSLuiz Augusto von Dentz 		else if (conn->role == HCI_ROLE_MASTER)
1024eca0ae4aSLuiz Augusto von Dentz 			conn->cleanup = cis_cleanup;
1025eca0ae4aSLuiz Augusto von Dentz 
1026ad3f7986SSungwoo Kim 		conn->mtu = hdev->iso_mtu ? hdev->iso_mtu :
1027ad3f7986SSungwoo Kim 			    hdev->le_mtu ? hdev->le_mtu : hdev->acl_mtu;
10289c84d1daSJohan Hedberg 		break;
1029a8746417SMarcel Holtmann 	case SCO_LINK:
1030a8746417SMarcel Holtmann 		if (lmp_esco_capable(hdev))
1031efc7688bSMarcel Holtmann 			conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
1032efc7688bSMarcel Holtmann 					(hdev->esco_type & EDR_ESCO_MASK);
1033a8746417SMarcel Holtmann 		else
1034a8746417SMarcel Holtmann 			conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK;
1035ad3f7986SSungwoo Kim 
1036ad3f7986SSungwoo Kim 		conn->mtu = hdev->sco_mtu;
1037a8746417SMarcel Holtmann 		break;
1038a8746417SMarcel Holtmann 	case ESCO_LINK:
1039efc7688bSMarcel Holtmann 		conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK;
1040ad3f7986SSungwoo Kim 		conn->mtu = hdev->sco_mtu;
1041a8746417SMarcel Holtmann 		break;
1042a8746417SMarcel Holtmann 	}
1043a8746417SMarcel Holtmann 
10441da177e4SLinus Torvalds 	skb_queue_head_init(&conn->data_q);
104504837f64SMarcel Holtmann 
104670c1f20bSMarcel Holtmann 	INIT_LIST_HEAD(&conn->chan_list);
104706149746SLuiz Augusto von Dentz 	INIT_LIST_HEAD(&conn->link_list);
104873d80debSLuiz Augusto von Dentz 
104919c40e3bSGustavo F. Padovan 	INIT_DELAYED_WORK(&conn->disc_work, hci_conn_timeout);
10507bc18d9dSJohan Hedberg 	INIT_DELAYED_WORK(&conn->auto_accept_work, hci_conn_auto_accept);
1051a74a84f6SJohan Hedberg 	INIT_DELAYED_WORK(&conn->idle_work, hci_conn_idle);
10529489eca4SJohan Hedberg 	INIT_DELAYED_WORK(&conn->le_conn_timeout, le_conn_timeout);
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds 	atomic_set(&conn->refcnt, 0);
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds 	hci_dev_hold(hdev);
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds 	hci_conn_hash_add(hdev, conn);
10591f8330eaSSathish Narsimman 
10601f8330eaSSathish Narsimman 	/* The SCO and eSCO connections will only be notified when their
10611f8330eaSSathish Narsimman 	 * setup has been completed. This is different to ACL links which
10621f8330eaSSathish Narsimman 	 * can be notified right away.
10631f8330eaSSathish Narsimman 	 */
10641f8330eaSSathish Narsimman 	if (conn->type != SCO_LINK && conn->type != ESCO_LINK) {
10653c54711cSGustavo F. Padovan 		if (hdev->notify)
10661da177e4SLinus Torvalds 			hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
10671f8330eaSSathish Narsimman 	}
10681da177e4SLinus Torvalds 
1069a67e899cSMarcel Holtmann 	hci_conn_init_sysfs(conn);
1070a67e899cSMarcel Holtmann 
10711da177e4SLinus Torvalds 	return conn;
10721da177e4SLinus Torvalds }
10731da177e4SLinus Torvalds 
107484cb0143SZiyang Xuan struct hci_conn *hci_conn_add_unset(struct hci_dev *hdev, int type,
107584cb0143SZiyang Xuan 				    bdaddr_t *dst, u8 role)
107684cb0143SZiyang Xuan {
107784cb0143SZiyang Xuan 	int handle;
107884cb0143SZiyang Xuan 
107984cb0143SZiyang Xuan 	bt_dev_dbg(hdev, "dst %pMR", dst);
108084cb0143SZiyang Xuan 
108184cb0143SZiyang Xuan 	handle = hci_conn_hash_alloc_unset(hdev);
108284cb0143SZiyang Xuan 	if (unlikely(handle < 0))
1083ad3f7986SSungwoo Kim 		return ERR_PTR(-ECONNREFUSED);
108484cb0143SZiyang Xuan 
10854970e48fSPavel Skripkin 	return __hci_conn_add(hdev, type, dst, role, handle);
10864970e48fSPavel Skripkin }
10874970e48fSPavel Skripkin 
10884970e48fSPavel Skripkin struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst,
10894970e48fSPavel Skripkin 			      u8 role, u16 handle)
10904970e48fSPavel Skripkin {
10914970e48fSPavel Skripkin 	if (handle > HCI_CONN_HANDLE_MAX)
10924970e48fSPavel Skripkin 		return ERR_PTR(-EINVAL);
10934970e48fSPavel Skripkin 
10944970e48fSPavel Skripkin 	return __hci_conn_add(hdev, type, dst, role, handle);
109584cb0143SZiyang Xuan }
109684cb0143SZiyang Xuan 
10973344d318SPauli Virtanen static void hci_conn_cleanup_child(struct hci_conn *conn, u8 reason)
10983344d318SPauli Virtanen {
10993344d318SPauli Virtanen 	if (!reason)
11003344d318SPauli Virtanen 		reason = HCI_ERROR_REMOTE_USER_TERM;
11013344d318SPauli Virtanen 
11023344d318SPauli Virtanen 	/* Due to race, SCO/ISO conn might be not established yet at this point,
11033344d318SPauli Virtanen 	 * and nothing else will clean it up. In other cases it is done via HCI
11043344d318SPauli Virtanen 	 * events.
11053344d318SPauli Virtanen 	 */
11063344d318SPauli Virtanen 	switch (conn->type) {
11073344d318SPauli Virtanen 	case SCO_LINK:
11083344d318SPauli Virtanen 	case ESCO_LINK:
11093344d318SPauli Virtanen 		if (HCI_CONN_HANDLE_UNSET(conn->handle))
11103344d318SPauli Virtanen 			hci_conn_failed(conn, reason);
11113344d318SPauli Virtanen 		break;
11123344d318SPauli Virtanen 	case ISO_LINK:
11133344d318SPauli Virtanen 		if (conn->state != BT_CONNECTED &&
11143344d318SPauli Virtanen 		    !test_bit(HCI_CONN_CREATE_CIS, &conn->flags))
11153344d318SPauli Virtanen 			hci_conn_failed(conn, reason);
11163344d318SPauli Virtanen 		break;
11173344d318SPauli Virtanen 	}
11183344d318SPauli Virtanen }
11193344d318SPauli Virtanen 
112006149746SLuiz Augusto von Dentz static void hci_conn_unlink(struct hci_conn *conn)
11215dc7d23eSLuiz Augusto von Dentz {
112206149746SLuiz Augusto von Dentz 	struct hci_dev *hdev = conn->hdev;
11235dc7d23eSLuiz Augusto von Dentz 
112406149746SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "hcon %p", conn);
112506149746SLuiz Augusto von Dentz 
112606149746SLuiz Augusto von Dentz 	if (!conn->parent) {
112706149746SLuiz Augusto von Dentz 		struct hci_link *link, *t;
112806149746SLuiz Augusto von Dentz 
1129ca1fd42eSRuihan Li 		list_for_each_entry_safe(link, t, &conn->link_list, list) {
1130ca1fd42eSRuihan Li 			struct hci_conn *child = link->conn;
1131ca1fd42eSRuihan Li 
1132ca1fd42eSRuihan Li 			hci_conn_unlink(child);
1133ca1fd42eSRuihan Li 
1134a2ac591cSRuihan Li 			/* If hdev is down it means
1135a2ac591cSRuihan Li 			 * hci_dev_close_sync/hci_conn_hash_flush is in progress
1136a2ac591cSRuihan Li 			 * and links don't need to be cleanup as all connections
1137a2ac591cSRuihan Li 			 * would be cleanup.
1138a2ac591cSRuihan Li 			 */
1139a2ac591cSRuihan Li 			if (!test_bit(HCI_UP, &hdev->flags))
1140a2ac591cSRuihan Li 				continue;
1141a2ac591cSRuihan Li 
11423344d318SPauli Virtanen 			hci_conn_cleanup_child(child, conn->abort_reason);
1143ca1fd42eSRuihan Li 		}
114406149746SLuiz Augusto von Dentz 
114506149746SLuiz Augusto von Dentz 		return;
114606149746SLuiz Augusto von Dentz 	}
114706149746SLuiz Augusto von Dentz 
114806149746SLuiz Augusto von Dentz 	if (!conn->link)
114906149746SLuiz Augusto von Dentz 		return;
115006149746SLuiz Augusto von Dentz 
115106149746SLuiz Augusto von Dentz 	list_del_rcu(&conn->link->list);
115206149746SLuiz Augusto von Dentz 	synchronize_rcu();
115306149746SLuiz Augusto von Dentz 
1154a2904d28SRuihan Li 	hci_conn_drop(conn->parent);
11552910431aSRuihan Li 	hci_conn_put(conn->parent);
11562910431aSRuihan Li 	conn->parent = NULL;
11572910431aSRuihan Li 
115806149746SLuiz Augusto von Dentz 	kfree(conn->link);
11595dc7d23eSLuiz Augusto von Dentz 	conn->link = NULL;
11605dc7d23eSLuiz Augusto von Dentz }
11615dc7d23eSLuiz Augusto von Dentz 
1162a2ac591cSRuihan Li void hci_conn_del(struct hci_conn *conn)
11631da177e4SLinus Torvalds {
11641da177e4SLinus Torvalds 	struct hci_dev *hdev = conn->hdev;
11651da177e4SLinus Torvalds 
116638b3fef1SAndrei Emeltchenko 	BT_DBG("%s hcon %p handle %d", hdev->name, conn, conn->handle);
11671da177e4SLinus Torvalds 
1168a2904d28SRuihan Li 	hci_conn_unlink(conn);
1169a2904d28SRuihan Li 
117019c40e3bSGustavo F. Padovan 	cancel_delayed_work_sync(&conn->disc_work);
11717bc18d9dSJohan Hedberg 	cancel_delayed_work_sync(&conn->auto_accept_work);
1172a74a84f6SJohan Hedberg 	cancel_delayed_work_sync(&conn->idle_work);
11739f61656aSJohan Hedberg 
11745b7f9909SMarcel Holtmann 	if (conn->type == ACL_LINK) {
11751da177e4SLinus Torvalds 		/* Unacked frames */
11761da177e4SLinus Torvalds 		hdev->acl_cnt += conn->sent;
11776ed58ec5SVille Tervo 	} else if (conn->type == LE_LINK) {
1178980ffc0aSJohan Hedberg 		cancel_delayed_work(&conn->le_conn_timeout);
11799489eca4SJohan Hedberg 
11806ed58ec5SVille Tervo 		if (hdev->le_pkts)
11816ed58ec5SVille Tervo 			hdev->le_cnt += conn->sent;
11826ed58ec5SVille Tervo 		else
11836ed58ec5SVille Tervo 			hdev->acl_cnt += conn->sent;
11845b7f9909SMarcel Holtmann 	} else {
11855638d9eaSLuiz Augusto von Dentz 		/* Unacked ISO frames */
11865638d9eaSLuiz Augusto von Dentz 		if (conn->type == ISO_LINK) {
11875638d9eaSLuiz Augusto von Dentz 			if (hdev->iso_pkts)
11885638d9eaSLuiz Augusto von Dentz 				hdev->iso_cnt += conn->sent;
11895638d9eaSLuiz Augusto von Dentz 			else if (hdev->le_pkts)
11905638d9eaSLuiz Augusto von Dentz 				hdev->le_cnt += conn->sent;
11915638d9eaSLuiz Augusto von Dentz 			else
11925638d9eaSLuiz Augusto von Dentz 				hdev->acl_cnt += conn->sent;
11935638d9eaSLuiz Augusto von Dentz 		}
11941da177e4SLinus Torvalds 	}
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 	skb_queue_purge(&conn->data_q);
11971da177e4SLinus Torvalds 
1198b958f9a3SJohan Hedberg 	/* Remove the connection from the list and cleanup its remaining
1199b958f9a3SJohan Hedberg 	 * state. This is a separate function since for some cases like
1200b958f9a3SJohan Hedberg 	 * BT_CONNECT_SCAN we *only* want the cleanup part without the
1201b958f9a3SJohan Hedberg 	 * rest of hci_conn_del.
1202b958f9a3SJohan Hedberg 	 */
1203b958f9a3SJohan Hedberg 	hci_conn_cleanup(conn);
12041da177e4SLinus Torvalds }
12051da177e4SLinus Torvalds 
120639385cb5SJohan Hedberg struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src, uint8_t src_type)
12071da177e4SLinus Torvalds {
12081da177e4SLinus Torvalds 	int use_src = bacmp(src, BDADDR_ANY);
12098035ded4SLuiz Augusto von Dentz 	struct hci_dev *hdev = NULL, *d;
12101da177e4SLinus Torvalds 
12116ed93dc6SAndrei Emeltchenko 	BT_DBG("%pMR -> %pMR", src, dst);
12121da177e4SLinus Torvalds 
1213f20d09d5SGustavo F. Padovan 	read_lock(&hci_dev_list_lock);
12141da177e4SLinus Torvalds 
12158035ded4SLuiz Augusto von Dentz 	list_for_each_entry(d, &hci_dev_list, list) {
12168fc9ced3SGustavo Padovan 		if (!test_bit(HCI_UP, &d->flags) ||
12175af2e235SLuiz Augusto von Dentz 		    hci_dev_test_flag(d, HCI_USER_CHANNEL))
12181da177e4SLinus Torvalds 			continue;
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds 		/* Simple routing:
12211da177e4SLinus Torvalds 		 *   No source address - find interface with bdaddr != dst
12221da177e4SLinus Torvalds 		 *   Source address    - find interface with bdaddr == src
12231da177e4SLinus Torvalds 		 */
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds 		if (use_src) {
122639385cb5SJohan Hedberg 			bdaddr_t id_addr;
122739385cb5SJohan Hedberg 			u8 id_addr_type;
122839385cb5SJohan Hedberg 
122939385cb5SJohan Hedberg 			if (src_type == BDADDR_BREDR) {
123039385cb5SJohan Hedberg 				if (!lmp_bredr_capable(d))
123139385cb5SJohan Hedberg 					continue;
123239385cb5SJohan Hedberg 				bacpy(&id_addr, &d->bdaddr);
123339385cb5SJohan Hedberg 				id_addr_type = BDADDR_BREDR;
123439385cb5SJohan Hedberg 			} else {
123539385cb5SJohan Hedberg 				if (!lmp_le_capable(d))
123639385cb5SJohan Hedberg 					continue;
123739385cb5SJohan Hedberg 
123839385cb5SJohan Hedberg 				hci_copy_identity_address(d, &id_addr,
123939385cb5SJohan Hedberg 							  &id_addr_type);
124039385cb5SJohan Hedberg 
124139385cb5SJohan Hedberg 				/* Convert from HCI to three-value type */
124239385cb5SJohan Hedberg 				if (id_addr_type == ADDR_LE_DEV_PUBLIC)
124339385cb5SJohan Hedberg 					id_addr_type = BDADDR_LE_PUBLIC;
124439385cb5SJohan Hedberg 				else
124539385cb5SJohan Hedberg 					id_addr_type = BDADDR_LE_RANDOM;
124639385cb5SJohan Hedberg 			}
124739385cb5SJohan Hedberg 
124839385cb5SJohan Hedberg 			if (!bacmp(&id_addr, src) && id_addr_type == src_type) {
12491da177e4SLinus Torvalds 				hdev = d; break;
12501da177e4SLinus Torvalds 			}
12511da177e4SLinus Torvalds 		} else {
12521da177e4SLinus Torvalds 			if (bacmp(&d->bdaddr, dst)) {
12531da177e4SLinus Torvalds 				hdev = d; break;
12541da177e4SLinus Torvalds 			}
12551da177e4SLinus Torvalds 		}
12561da177e4SLinus Torvalds 	}
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 	if (hdev)
12591da177e4SLinus Torvalds 		hdev = hci_dev_hold(hdev);
12601da177e4SLinus Torvalds 
1261f20d09d5SGustavo F. Padovan 	read_unlock(&hci_dev_list_lock);
12621da177e4SLinus Torvalds 	return hdev;
12631da177e4SLinus Torvalds }
12641da177e4SLinus Torvalds EXPORT_SYMBOL(hci_get_route);
12651da177e4SLinus Torvalds 
12669bb3c01fSAndre Guedes /* This function requires the caller holds hdev->lock */
12679b3628d7SLuiz Augusto von Dentz static void hci_le_conn_failed(struct hci_conn *conn, u8 status)
12689bb3c01fSAndre Guedes {
12699bb3c01fSAndre Guedes 	struct hci_dev *hdev = conn->hdev;
1270f161dd41SJohan Hedberg 
127119cf60bfSLuiz Augusto von Dentz 	hci_connect_le_scan_cleanup(conn, status);
12723c857757SJohan Hedberg 
1273abfeea47SLuiz Augusto von Dentz 	/* Enable advertising in case this was a failed connection
12743c857757SJohan Hedberg 	 * attempt as a peripheral.
12753c857757SJohan Hedberg 	 */
1276abfeea47SLuiz Augusto von Dentz 	hci_enable_advertising(hdev);
12779bb3c01fSAndre Guedes }
12789bb3c01fSAndre Guedes 
12799b3628d7SLuiz Augusto von Dentz /* This function requires the caller holds hdev->lock */
12809b3628d7SLuiz Augusto von Dentz void hci_conn_failed(struct hci_conn *conn, u8 status)
12819b3628d7SLuiz Augusto von Dentz {
12829b3628d7SLuiz Augusto von Dentz 	struct hci_dev *hdev = conn->hdev;
12839b3628d7SLuiz Augusto von Dentz 
12849b3628d7SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "status 0x%2.2x", status);
12859b3628d7SLuiz Augusto von Dentz 
12869b3628d7SLuiz Augusto von Dentz 	switch (conn->type) {
12879b3628d7SLuiz Augusto von Dentz 	case LE_LINK:
12889b3628d7SLuiz Augusto von Dentz 		hci_le_conn_failed(conn, status);
12899b3628d7SLuiz Augusto von Dentz 		break;
12909b3628d7SLuiz Augusto von Dentz 	case ACL_LINK:
12919b3628d7SLuiz Augusto von Dentz 		mgmt_connect_failed(hdev, &conn->dst, conn->type,
12929b3628d7SLuiz Augusto von Dentz 				    conn->dst_type, status);
12939b3628d7SLuiz Augusto von Dentz 		break;
12949b3628d7SLuiz Augusto von Dentz 	}
12959b3628d7SLuiz Augusto von Dentz 
1296f03d3322SIulia Tanasescu 	/* In case of BIG/PA sync failed, clear conn flags so that
1297f03d3322SIulia Tanasescu 	 * the conns will be correctly cleaned up by ISO layer
1298f03d3322SIulia Tanasescu 	 */
1299f03d3322SIulia Tanasescu 	test_and_clear_bit(HCI_CONN_BIG_SYNC_FAILED, &conn->flags);
1300f03d3322SIulia Tanasescu 	test_and_clear_bit(HCI_CONN_PA_SYNC_FAILED, &conn->flags);
1301f03d3322SIulia Tanasescu 
13029b3628d7SLuiz Augusto von Dentz 	conn->state = BT_CLOSED;
13039b3628d7SLuiz Augusto von Dentz 	hci_connect_cfm(conn, status);
13049b3628d7SLuiz Augusto von Dentz 	hci_conn_del(conn);
13059b3628d7SLuiz Augusto von Dentz }
13069b3628d7SLuiz Augusto von Dentz 
130716e3b642SLuiz Augusto von Dentz /* This function requires the caller holds hdev->lock */
130816e3b642SLuiz Augusto von Dentz u8 hci_conn_set_handle(struct hci_conn *conn, u16 handle)
130916e3b642SLuiz Augusto von Dentz {
131016e3b642SLuiz Augusto von Dentz 	struct hci_dev *hdev = conn->hdev;
131116e3b642SLuiz Augusto von Dentz 
131216e3b642SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "hcon %p handle 0x%4.4x", conn, handle);
131316e3b642SLuiz Augusto von Dentz 
131416e3b642SLuiz Augusto von Dentz 	if (conn->handle == handle)
131516e3b642SLuiz Augusto von Dentz 		return 0;
131616e3b642SLuiz Augusto von Dentz 
131716e3b642SLuiz Augusto von Dentz 	if (handle > HCI_CONN_HANDLE_MAX) {
131816e3b642SLuiz Augusto von Dentz 		bt_dev_err(hdev, "Invalid handle: 0x%4.4x > 0x%4.4x",
131916e3b642SLuiz Augusto von Dentz 			   handle, HCI_CONN_HANDLE_MAX);
132016e3b642SLuiz Augusto von Dentz 		return HCI_ERROR_INVALID_PARAMETERS;
132116e3b642SLuiz Augusto von Dentz 	}
132216e3b642SLuiz Augusto von Dentz 
132316e3b642SLuiz Augusto von Dentz 	/* If abort_reason has been sent it means the connection is being
132416e3b642SLuiz Augusto von Dentz 	 * aborted and the handle shall not be changed.
132516e3b642SLuiz Augusto von Dentz 	 */
132616e3b642SLuiz Augusto von Dentz 	if (conn->abort_reason)
132716e3b642SLuiz Augusto von Dentz 		return conn->abort_reason;
132816e3b642SLuiz Augusto von Dentz 
132984cb0143SZiyang Xuan 	if (HCI_CONN_HANDLE_UNSET(conn->handle))
133084cb0143SZiyang Xuan 		ida_free(&hdev->unset_handle_ida, conn->handle);
133184cb0143SZiyang Xuan 
133216e3b642SLuiz Augusto von Dentz 	conn->handle = handle;
133316e3b642SLuiz Augusto von Dentz 
133416e3b642SLuiz Augusto von Dentz 	return 0;
133516e3b642SLuiz Augusto von Dentz }
133616e3b642SLuiz Augusto von Dentz 
13378e8b92eeSLuiz Augusto von Dentz static void create_le_conn_complete(struct hci_dev *hdev, void *data, int err)
13381d399ae5SAndre Guedes {
133904a51d61SLuiz Augusto von Dentz 	struct hci_conn *conn;
1340a1f6c3aeSLuiz Augusto von Dentz 	u16 handle = PTR_UINT(data);
134104a51d61SLuiz Augusto von Dentz 
134204a51d61SLuiz Augusto von Dentz 	conn = hci_conn_hash_lookup_handle(hdev, handle);
134304a51d61SLuiz Augusto von Dentz 	if (!conn)
134404a51d61SLuiz Augusto von Dentz 		return;
13451d399ae5SAndre Guedes 
1346b62e7220SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "err %d", err);
1347b62e7220SLuiz Augusto von Dentz 
134828a667c9SJakub Pawlowski 	hci_dev_lock(hdev);
134928a667c9SJakub Pawlowski 
13508e8b92eeSLuiz Augusto von Dentz 	if (!err) {
135119cf60bfSLuiz Augusto von Dentz 		hci_connect_le_scan_cleanup(conn, 0x00);
135228a667c9SJakub Pawlowski 		goto done;
135328a667c9SJakub Pawlowski 	}
13541d399ae5SAndre Guedes 
1355c9f73a21SLuiz Augusto von Dentz 	/* Check if connection is still pending */
1356c9f73a21SLuiz Augusto von Dentz 	if (conn != hci_lookup_le_connect(hdev))
13571d399ae5SAndre Guedes 		goto done;
13581d399ae5SAndre Guedes 
13595cd39700SArchie Pusaka 	/* Flush to make sure we send create conn cancel command if needed */
13605cd39700SArchie Pusaka 	flush_delayed_work(&conn->le_conn_timeout);
1361a86ddbffSLuiz Augusto von Dentz 	hci_conn_failed(conn, bt_status(err));
13621d399ae5SAndre Guedes 
13631d399ae5SAndre Guedes done:
13641d399ae5SAndre Guedes 	hci_dev_unlock(hdev);
13651d399ae5SAndre Guedes }
13661d399ae5SAndre Guedes 
13678e8b92eeSLuiz Augusto von Dentz static int hci_connect_le_sync(struct hci_dev *hdev, void *data)
136882a37adeSJohan Hedberg {
136904a51d61SLuiz Augusto von Dentz 	struct hci_conn *conn;
1370a1f6c3aeSLuiz Augusto von Dentz 	u16 handle = PTR_UINT(data);
137104a51d61SLuiz Augusto von Dentz 
137204a51d61SLuiz Augusto von Dentz 	conn = hci_conn_hash_lookup_handle(hdev, handle);
137304a51d61SLuiz Augusto von Dentz 	if (!conn)
137404a51d61SLuiz Augusto von Dentz 		return 0;
137582a37adeSJohan Hedberg 
13768e8b92eeSLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "conn %p", conn);
137782a37adeSJohan Hedberg 
13783a15324fSLuiz Augusto von Dentz 	clear_bit(HCI_CONN_SCANNING, &conn->flags);
137904a51d61SLuiz Augusto von Dentz 	conn->state = BT_CONNECT;
138004a51d61SLuiz Augusto von Dentz 
13818e8b92eeSLuiz Augusto von Dentz 	return hci_le_create_conn_sync(hdev, conn);
13823c857757SJohan Hedberg }
13833c857757SJohan Hedberg 
138404a6c589SAndre Guedes struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
1385d850bf08SLuiz Augusto von Dentz 				u8 dst_type, bool dst_resolved, u8 sec_level,
13868e8b92eeSLuiz Augusto von Dentz 				u16 conn_timeout, u8 role)
13871da177e4SLinus Torvalds {
1388e2caced4SJohan Hedberg 	struct hci_conn *conn;
13891ebfcc1fSJohan Hedberg 	struct smp_irk *irk;
13901d399ae5SAndre Guedes 	int err;
13911da177e4SLinus Torvalds 
1392152d386eSLukasz Rymanowski 	/* Let's make sure that le is enabled.*/
1393d7a5a11dSMarcel Holtmann 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
1394152d386eSLukasz Rymanowski 		if (lmp_le_capable(hdev))
1395152d386eSLukasz Rymanowski 			return ERR_PTR(-ECONNREFUSED);
1396152d386eSLukasz Rymanowski 
1397152d386eSLukasz Rymanowski 		return ERR_PTR(-EOPNOTSUPP);
1398152d386eSLukasz Rymanowski 	}
1399152d386eSLukasz Rymanowski 
1400658aead9SJohan Hedberg 	/* Since the controller supports only one LE connection attempt at a
1401658aead9SJohan Hedberg 	 * time, we return -EBUSY if there is any connection attempt running.
1402658aead9SJohan Hedberg 	 */
1403658aead9SJohan Hedberg 	if (hci_lookup_le_connect(hdev))
1404658aead9SJohan Hedberg 		return ERR_PTR(-EBUSY);
1405658aead9SJohan Hedberg 
1406e2caced4SJohan Hedberg 	/* If there's already a connection object but it's not in
1407e2caced4SJohan Hedberg 	 * scanning state it means it must already be established, in
1408e2caced4SJohan Hedberg 	 * which case we can't do anything else except report a failure
1409e2caced4SJohan Hedberg 	 * to connect.
1410620ad521SAndre Guedes 	 */
14119d4c1cc1SJohan Hedberg 	conn = hci_conn_hash_lookup_le(hdev, dst, dst_type);
1412e2caced4SJohan Hedberg 	if (conn && !test_bit(HCI_CONN_SCANNING, &conn->flags)) {
1413e2caced4SJohan Hedberg 		return ERR_PTR(-EBUSY);
141428a667c9SJakub Pawlowski 	}
1415620ad521SAndre Guedes 
1416d850bf08SLuiz Augusto von Dentz 	/* Check if the destination address has been resolved by the controller
1417d850bf08SLuiz Augusto von Dentz 	 * since if it did then the identity address shall be used.
1418d850bf08SLuiz Augusto von Dentz 	 */
1419d850bf08SLuiz Augusto von Dentz 	if (!dst_resolved) {
1420edb4b466SMarcel Holtmann 		/* When given an identity address with existing identity
1421edb4b466SMarcel Holtmann 		 * resolving key, the connection needs to be established
1422edb4b466SMarcel Holtmann 		 * to a resolvable random address.
1423edb4b466SMarcel Holtmann 		 *
1424edb4b466SMarcel Holtmann 		 * Storing the resolvable random address is required here
1425edb4b466SMarcel Holtmann 		 * to handle connection failures. The address will later
1426edb4b466SMarcel Holtmann 		 * be resolved back into the original identity address
1427edb4b466SMarcel Holtmann 		 * from the connect request.
1428edb4b466SMarcel Holtmann 		 */
14291ebfcc1fSJohan Hedberg 		irk = hci_find_irk_by_addr(hdev, dst, dst_type);
14301ebfcc1fSJohan Hedberg 		if (irk && bacmp(&irk->rpa, BDADDR_ANY)) {
14311ebfcc1fSJohan Hedberg 			dst = &irk->rpa;
14321ebfcc1fSJohan Hedberg 			dst_type = ADDR_LE_DEV_RANDOM;
14331ebfcc1fSJohan Hedberg 		}
1434d850bf08SLuiz Augusto von Dentz 	}
14351ebfcc1fSJohan Hedberg 
1436e2caced4SJohan Hedberg 	if (conn) {
143728a667c9SJakub Pawlowski 		bacpy(&conn->dst, dst);
143828a667c9SJakub Pawlowski 	} else {
143984cb0143SZiyang Xuan 		conn = hci_conn_add_unset(hdev, LE_LINK, dst, role);
1440ad3f7986SSungwoo Kim 		if (IS_ERR(conn))
1441ad3f7986SSungwoo Kim 			return conn;
1442e2caced4SJohan Hedberg 		hci_conn_hold(conn);
1443e2caced4SJohan Hedberg 		conn->pending_sec_level = sec_level;
1444e2caced4SJohan Hedberg 	}
1445893d6751SAndre Guedes 
14461ebfcc1fSJohan Hedberg 	conn->dst_type = dst_type;
144746a190cbSAndre Guedes 	conn->sec_level = BT_SECURITY_LOW;
144809ae260bSJohan Hedberg 	conn->conn_timeout = conn_timeout;
14494292f1f3SAndre Guedes 
145004a51d61SLuiz Augusto von Dentz 	err = hci_cmd_sync_queue(hdev, hci_connect_le_sync,
1451a1f6c3aeSLuiz Augusto von Dentz 				 UINT_PTR(conn->handle),
14528e8b92eeSLuiz Augusto von Dentz 				 create_le_conn_complete);
14532acf3d90SAndre Guedes 	if (err) {
14542acf3d90SAndre Guedes 		hci_conn_del(conn);
14551d399ae5SAndre Guedes 		return ERR_PTR(err);
14562acf3d90SAndre Guedes 	}
14579f0caeb1SVinicius Costa Gomes 
1458f1e5d547SAndre Guedes 	return conn;
1459fcd89c09SVille Tervo }
1460fcd89c09SVille Tervo 
1461f75113a2SJakub Pawlowski static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
1462f75113a2SJakub Pawlowski {
1463f75113a2SJakub Pawlowski 	struct hci_conn *conn;
1464f75113a2SJakub Pawlowski 
14659d4c1cc1SJohan Hedberg 	conn = hci_conn_hash_lookup_le(hdev, addr, type);
1466f75113a2SJakub Pawlowski 	if (!conn)
1467f75113a2SJakub Pawlowski 		return false;
1468f75113a2SJakub Pawlowski 
1469f75113a2SJakub Pawlowski 	if (conn->state != BT_CONNECTED)
1470f75113a2SJakub Pawlowski 		return false;
1471f75113a2SJakub Pawlowski 
1472f75113a2SJakub Pawlowski 	return true;
1473f75113a2SJakub Pawlowski }
1474f75113a2SJakub Pawlowski 
1475f75113a2SJakub Pawlowski /* This function requires the caller holds hdev->lock */
147684235d22SJohan Hedberg static int hci_explicit_conn_params_set(struct hci_dev *hdev,
1477f75113a2SJakub Pawlowski 					bdaddr_t *addr, u8 addr_type)
1478f75113a2SJakub Pawlowski {
1479f75113a2SJakub Pawlowski 	struct hci_conn_params *params;
1480f75113a2SJakub Pawlowski 
1481f75113a2SJakub Pawlowski 	if (is_connected(hdev, addr, addr_type))
1482f75113a2SJakub Pawlowski 		return -EISCONN;
1483f75113a2SJakub Pawlowski 
14845157b8a5SJakub Pawlowski 	params = hci_conn_params_lookup(hdev, addr, addr_type);
14855157b8a5SJakub Pawlowski 	if (!params) {
1486f75113a2SJakub Pawlowski 		params = hci_conn_params_add(hdev, addr, addr_type);
1487f75113a2SJakub Pawlowski 		if (!params)
14885157b8a5SJakub Pawlowski 			return -ENOMEM;
1489f75113a2SJakub Pawlowski 
14905157b8a5SJakub Pawlowski 		/* If we created new params, mark them to be deleted in
14915157b8a5SJakub Pawlowski 		 * hci_connect_le_scan_cleanup. It's different case than
14925157b8a5SJakub Pawlowski 		 * existing disabled params, those will stay after cleanup.
1493f75113a2SJakub Pawlowski 		 */
14945157b8a5SJakub Pawlowski 		params->auto_connect = HCI_AUTO_CONN_EXPLICIT;
14955157b8a5SJakub Pawlowski 	}
14965157b8a5SJakub Pawlowski 
14975157b8a5SJakub Pawlowski 	/* We're trying to connect, so make sure params are at pend_le_conns */
149849c50922SJohan Hedberg 	if (params->auto_connect == HCI_AUTO_CONN_DISABLED ||
14995157b8a5SJakub Pawlowski 	    params->auto_connect == HCI_AUTO_CONN_REPORT ||
15005157b8a5SJakub Pawlowski 	    params->auto_connect == HCI_AUTO_CONN_EXPLICIT) {
1501195ef75eSPauli Virtanen 		hci_pend_le_list_del_init(params);
1502195ef75eSPauli Virtanen 		hci_pend_le_list_add(params, &hdev->pend_le_conns);
1503f75113a2SJakub Pawlowski 	}
1504f75113a2SJakub Pawlowski 
1505f75113a2SJakub Pawlowski 	params->explicit_connect = true;
1506f75113a2SJakub Pawlowski 
1507f75113a2SJakub Pawlowski 	BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type,
1508f75113a2SJakub Pawlowski 	       params->auto_connect);
1509f75113a2SJakub Pawlowski 
1510f75113a2SJakub Pawlowski 	return 0;
1511f75113a2SJakub Pawlowski }
1512f75113a2SJakub Pawlowski 
1513eca0ae4aSLuiz Augusto von Dentz static int qos_set_big(struct hci_dev *hdev, struct bt_iso_qos *qos)
1514eca0ae4aSLuiz Augusto von Dentz {
15156a42e9bfSIulia Tanasescu 	struct hci_conn *conn;
15166a42e9bfSIulia Tanasescu 	u8  big;
1517eca0ae4aSLuiz Augusto von Dentz 
1518eca0ae4aSLuiz Augusto von Dentz 	/* Allocate a BIG if not set */
15190fe8c8d0SIulia Tanasescu 	if (qos->bcast.big == BT_ISO_QOS_BIG_UNSET) {
15206a42e9bfSIulia Tanasescu 		for (big = 0x00; big < 0xef; big++) {
1521eca0ae4aSLuiz Augusto von Dentz 
15226a42e9bfSIulia Tanasescu 			conn = hci_conn_hash_lookup_big(hdev, big);
15236a42e9bfSIulia Tanasescu 			if (!conn)
1524eca0ae4aSLuiz Augusto von Dentz 				break;
1525eca0ae4aSLuiz Augusto von Dentz 		}
1526eca0ae4aSLuiz Augusto von Dentz 
15276a42e9bfSIulia Tanasescu 		if (big == 0xef)
1528eca0ae4aSLuiz Augusto von Dentz 			return -EADDRNOTAVAIL;
1529eca0ae4aSLuiz Augusto von Dentz 
1530eca0ae4aSLuiz Augusto von Dentz 		/* Update BIG */
15316a42e9bfSIulia Tanasescu 		qos->bcast.big = big;
1532eca0ae4aSLuiz Augusto von Dentz 	}
1533eca0ae4aSLuiz Augusto von Dentz 
1534eca0ae4aSLuiz Augusto von Dentz 	return 0;
1535eca0ae4aSLuiz Augusto von Dentz }
1536eca0ae4aSLuiz Augusto von Dentz 
1537eca0ae4aSLuiz Augusto von Dentz static int qos_set_bis(struct hci_dev *hdev, struct bt_iso_qos *qos)
1538eca0ae4aSLuiz Augusto von Dentz {
15396a42e9bfSIulia Tanasescu 	struct hci_conn *conn;
15406a42e9bfSIulia Tanasescu 	u8  bis;
1541eca0ae4aSLuiz Augusto von Dentz 
1542eca0ae4aSLuiz Augusto von Dentz 	/* Allocate BIS if not set */
15430fe8c8d0SIulia Tanasescu 	if (qos->bcast.bis == BT_ISO_QOS_BIS_UNSET) {
1544eca0ae4aSLuiz Augusto von Dentz 		/* Find an unused adv set to advertise BIS, skip instance 0x00
1545eca0ae4aSLuiz Augusto von Dentz 		 * since it is reserved as general purpose set.
1546eca0ae4aSLuiz Augusto von Dentz 		 */
15476a42e9bfSIulia Tanasescu 		for (bis = 0x01; bis < hdev->le_num_of_adv_sets;
15486a42e9bfSIulia Tanasescu 		     bis++) {
1549eca0ae4aSLuiz Augusto von Dentz 
15506a42e9bfSIulia Tanasescu 			conn = hci_conn_hash_lookup_bis(hdev, BDADDR_ANY, bis);
15516a42e9bfSIulia Tanasescu 			if (!conn)
1552eca0ae4aSLuiz Augusto von Dentz 				break;
1553eca0ae4aSLuiz Augusto von Dentz 		}
1554eca0ae4aSLuiz Augusto von Dentz 
15556a42e9bfSIulia Tanasescu 		if (bis == hdev->le_num_of_adv_sets)
1556eca0ae4aSLuiz Augusto von Dentz 			return -EADDRNOTAVAIL;
1557eca0ae4aSLuiz Augusto von Dentz 
1558eca0ae4aSLuiz Augusto von Dentz 		/* Update BIS */
15596a42e9bfSIulia Tanasescu 		qos->bcast.bis = bis;
1560eca0ae4aSLuiz Augusto von Dentz 	}
1561eca0ae4aSLuiz Augusto von Dentz 
1562eca0ae4aSLuiz Augusto von Dentz 	return 0;
1563eca0ae4aSLuiz Augusto von Dentz }
1564eca0ae4aSLuiz Augusto von Dentz 
1565eca0ae4aSLuiz Augusto von Dentz /* This function requires the caller holds hdev->lock */
1566eca0ae4aSLuiz Augusto von Dentz static struct hci_conn *hci_add_bis(struct hci_dev *hdev, bdaddr_t *dst,
1567a0bfde16SIulia Tanasescu 				    struct bt_iso_qos *qos, __u8 base_len,
1568a0bfde16SIulia Tanasescu 				    __u8 *base)
1569eca0ae4aSLuiz Augusto von Dentz {
1570eca0ae4aSLuiz Augusto von Dentz 	struct hci_conn *conn;
1571eca0ae4aSLuiz Augusto von Dentz 	int err;
1572eca0ae4aSLuiz Augusto von Dentz 
1573eca0ae4aSLuiz Augusto von Dentz 	/* Let's make sure that le is enabled.*/
1574eca0ae4aSLuiz Augusto von Dentz 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
1575eca0ae4aSLuiz Augusto von Dentz 		if (lmp_le_capable(hdev))
1576eca0ae4aSLuiz Augusto von Dentz 			return ERR_PTR(-ECONNREFUSED);
1577eca0ae4aSLuiz Augusto von Dentz 		return ERR_PTR(-EOPNOTSUPP);
1578eca0ae4aSLuiz Augusto von Dentz 	}
1579eca0ae4aSLuiz Augusto von Dentz 
1580eca0ae4aSLuiz Augusto von Dentz 	err = qos_set_big(hdev, qos);
1581eca0ae4aSLuiz Augusto von Dentz 	if (err)
1582eca0ae4aSLuiz Augusto von Dentz 		return ERR_PTR(err);
1583eca0ae4aSLuiz Augusto von Dentz 
1584eca0ae4aSLuiz Augusto von Dentz 	err = qos_set_bis(hdev, qos);
1585eca0ae4aSLuiz Augusto von Dentz 	if (err)
1586eca0ae4aSLuiz Augusto von Dentz 		return ERR_PTR(err);
1587eca0ae4aSLuiz Augusto von Dentz 
1588a0bfde16SIulia Tanasescu 	/* Check if the LE Create BIG command has already been sent */
1589a0bfde16SIulia Tanasescu 	conn = hci_conn_hash_lookup_per_adv_bis(hdev, dst, qos->bcast.big,
1590a0bfde16SIulia Tanasescu 						qos->bcast.big);
1591a0bfde16SIulia Tanasescu 	if (conn)
1592eca0ae4aSLuiz Augusto von Dentz 		return ERR_PTR(-EADDRINUSE);
1593eca0ae4aSLuiz Augusto von Dentz 
1594a0bfde16SIulia Tanasescu 	/* Check BIS settings against other bound BISes, since all
1595a0bfde16SIulia Tanasescu 	 * BISes in a BIG must have the same value for all parameters
1596a0bfde16SIulia Tanasescu 	 */
15976a42e9bfSIulia Tanasescu 	conn = hci_conn_hash_lookup_big(hdev, qos->bcast.big);
1598a0bfde16SIulia Tanasescu 
1599a0bfde16SIulia Tanasescu 	if (conn && (memcmp(qos, &conn->iso_qos, sizeof(*qos)) ||
1600a0bfde16SIulia Tanasescu 		     base_len != conn->le_per_adv_data_len ||
1601a0bfde16SIulia Tanasescu 		     memcmp(conn->le_per_adv_data, base, base_len)))
1602eca0ae4aSLuiz Augusto von Dentz 		return ERR_PTR(-EADDRINUSE);
1603eca0ae4aSLuiz Augusto von Dentz 
160484cb0143SZiyang Xuan 	conn = hci_conn_add_unset(hdev, ISO_LINK, dst, HCI_ROLE_MASTER);
1605ad3f7986SSungwoo Kim 	if (IS_ERR(conn))
1606ad3f7986SSungwoo Kim 		return conn;
1607eca0ae4aSLuiz Augusto von Dentz 
1608eca0ae4aSLuiz Augusto von Dentz 	conn->state = BT_CONNECT;
1609eca0ae4aSLuiz Augusto von Dentz 
1610eca0ae4aSLuiz Augusto von Dentz 	hci_conn_hold(conn);
1611eca0ae4aSLuiz Augusto von Dentz 	return conn;
1612eca0ae4aSLuiz Augusto von Dentz }
1613eca0ae4aSLuiz Augusto von Dentz 
1614f75113a2SJakub Pawlowski /* This function requires the caller holds hdev->lock */
1615f75113a2SJakub Pawlowski struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
1616f75113a2SJakub Pawlowski 				     u8 dst_type, u8 sec_level,
161776b13996SManish Mandlik 				     u16 conn_timeout,
161876b13996SManish Mandlik 				     enum conn_reasons conn_reason)
1619f75113a2SJakub Pawlowski {
1620f75113a2SJakub Pawlowski 	struct hci_conn *conn;
1621f75113a2SJakub Pawlowski 
1622f75113a2SJakub Pawlowski 	/* Let's make sure that le is enabled.*/
1623f75113a2SJakub Pawlowski 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
1624f75113a2SJakub Pawlowski 		if (lmp_le_capable(hdev))
1625f75113a2SJakub Pawlowski 			return ERR_PTR(-ECONNREFUSED);
1626f75113a2SJakub Pawlowski 
1627f75113a2SJakub Pawlowski 		return ERR_PTR(-EOPNOTSUPP);
1628f75113a2SJakub Pawlowski 	}
1629f75113a2SJakub Pawlowski 
1630f75113a2SJakub Pawlowski 	/* Some devices send ATT messages as soon as the physical link is
1631f75113a2SJakub Pawlowski 	 * established. To be able to handle these ATT messages, the user-
1632f75113a2SJakub Pawlowski 	 * space first establishes the connection and then starts the pairing
1633f75113a2SJakub Pawlowski 	 * process.
1634f75113a2SJakub Pawlowski 	 *
1635f75113a2SJakub Pawlowski 	 * So if a hci_conn object already exists for the following connection
1636f75113a2SJakub Pawlowski 	 * attempt, we simply update pending_sec_level and auth_type fields
1637f75113a2SJakub Pawlowski 	 * and return the object found.
1638f75113a2SJakub Pawlowski 	 */
16399d4c1cc1SJohan Hedberg 	conn = hci_conn_hash_lookup_le(hdev, dst, dst_type);
1640f75113a2SJakub Pawlowski 	if (conn) {
1641f75113a2SJakub Pawlowski 		if (conn->pending_sec_level < sec_level)
1642f75113a2SJakub Pawlowski 			conn->pending_sec_level = sec_level;
1643f75113a2SJakub Pawlowski 		goto done;
1644f75113a2SJakub Pawlowski 	}
1645f75113a2SJakub Pawlowski 
1646f75113a2SJakub Pawlowski 	BT_DBG("requesting refresh of dst_addr");
1647f75113a2SJakub Pawlowski 
164884cb0143SZiyang Xuan 	conn = hci_conn_add_unset(hdev, LE_LINK, dst, HCI_ROLE_MASTER);
1649ad3f7986SSungwoo Kim 	if (IS_ERR(conn))
1650ad3f7986SSungwoo Kim 		return conn;
1651f75113a2SJakub Pawlowski 
1652d088337cSNavid Emamdoost 	if (hci_explicit_conn_params_set(hdev, dst, dst_type) < 0) {
1653d088337cSNavid Emamdoost 		hci_conn_del(conn);
1654f75113a2SJakub Pawlowski 		return ERR_PTR(-EBUSY);
1655d088337cSNavid Emamdoost 	}
1656f75113a2SJakub Pawlowski 
1657f75113a2SJakub Pawlowski 	conn->state = BT_CONNECT;
1658f75113a2SJakub Pawlowski 	set_bit(HCI_CONN_SCANNING, &conn->flags);
1659f75113a2SJakub Pawlowski 	conn->dst_type = dst_type;
1660f75113a2SJakub Pawlowski 	conn->sec_level = BT_SECURITY_LOW;
1661f75113a2SJakub Pawlowski 	conn->pending_sec_level = sec_level;
1662f75113a2SJakub Pawlowski 	conn->conn_timeout = conn_timeout;
166376b13996SManish Mandlik 	conn->conn_reason = conn_reason;
1664f75113a2SJakub Pawlowski 
16655bee2fd6SLuiz Augusto von Dentz 	hci_update_passive_scan(hdev);
166684235d22SJohan Hedberg 
1667f75113a2SJakub Pawlowski done:
1668f75113a2SJakub Pawlowski 	hci_conn_hold(conn);
1669f75113a2SJakub Pawlowski 	return conn;
1670f75113a2SJakub Pawlowski }
1671f75113a2SJakub Pawlowski 
167204a6c589SAndre Guedes struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,
167376b13996SManish Mandlik 				 u8 sec_level, u8 auth_type,
167476b13996SManish Mandlik 				 enum conn_reasons conn_reason)
1675d04aef4cSVinicius Costa Gomes {
1676d04aef4cSVinicius Costa Gomes 	struct hci_conn *acl;
1677d04aef4cSVinicius Costa Gomes 
1678d7a5a11dSMarcel Holtmann 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
1679c411110eSLukasz Rymanowski 		if (lmp_bredr_capable(hdev))
1680c411110eSLukasz Rymanowski 			return ERR_PTR(-ECONNREFUSED);
1681c411110eSLukasz Rymanowski 
1682beb19e4cSJohan Hedberg 		return ERR_PTR(-EOPNOTSUPP);
1683c411110eSLukasz Rymanowski 	}
168456f87901SJohan Hedberg 
16851ffc6f8cSLee, Chun-Yi 	/* Reject outgoing connection to device with same BD ADDR against
16861ffc6f8cSLee, Chun-Yi 	 * CVE-2020-26555
16871ffc6f8cSLee, Chun-Yi 	 */
16881ffc6f8cSLee, Chun-Yi 	if (!bacmp(&hdev->bdaddr, dst)) {
16891ffc6f8cSLee, Chun-Yi 		bt_dev_dbg(hdev, "Reject connection with same BD_ADDR %pMR\n",
16901ffc6f8cSLee, Chun-Yi 			   dst);
16911ffc6f8cSLee, Chun-Yi 		return ERR_PTR(-ECONNREFUSED);
16921ffc6f8cSLee, Chun-Yi 	}
16931ffc6f8cSLee, Chun-Yi 
169470f23020SAndrei Emeltchenko 	acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
169570f23020SAndrei Emeltchenko 	if (!acl) {
169684cb0143SZiyang Xuan 		acl = hci_conn_add_unset(hdev, ACL_LINK, dst, HCI_ROLE_MASTER);
1697ad3f7986SSungwoo Kim 		if (IS_ERR(acl))
1698ad3f7986SSungwoo Kim 			return acl;
16991da177e4SLinus Torvalds 	}
17001da177e4SLinus Torvalds 
17011da177e4SLinus Torvalds 	hci_conn_hold(acl);
17021da177e4SLinus Torvalds 
170376b13996SManish Mandlik 	acl->conn_reason = conn_reason;
170409ab6f4cSMarcel Holtmann 	if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
1705765c2a96SJohan Hedberg 		acl->sec_level = BT_SECURITY_LOW;
1706765c2a96SJohan Hedberg 		acl->pending_sec_level = sec_level;
170709ab6f4cSMarcel Holtmann 		acl->auth_type = auth_type;
17081aef8669SVinicius Costa Gomes 		hci_acl_create_connection(acl);
170909ab6f4cSMarcel Holtmann 	}
17101da177e4SLinus Torvalds 
1711db474275SVinicius Costa Gomes 	return acl;
1712db474275SVinicius Costa Gomes }
1713db474275SVinicius Costa Gomes 
171406149746SLuiz Augusto von Dentz static struct hci_link *hci_conn_link(struct hci_conn *parent,
171506149746SLuiz Augusto von Dentz 				      struct hci_conn *conn)
171606149746SLuiz Augusto von Dentz {
171706149746SLuiz Augusto von Dentz 	struct hci_dev *hdev = parent->hdev;
171806149746SLuiz Augusto von Dentz 	struct hci_link *link;
171906149746SLuiz Augusto von Dentz 
172006149746SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "parent %p hcon %p", parent, conn);
172106149746SLuiz Augusto von Dentz 
172206149746SLuiz Augusto von Dentz 	if (conn->link)
172306149746SLuiz Augusto von Dentz 		return conn->link;
172406149746SLuiz Augusto von Dentz 
172506149746SLuiz Augusto von Dentz 	if (conn->parent)
172606149746SLuiz Augusto von Dentz 		return NULL;
172706149746SLuiz Augusto von Dentz 
172806149746SLuiz Augusto von Dentz 	link = kzalloc(sizeof(*link), GFP_KERNEL);
172906149746SLuiz Augusto von Dentz 	if (!link)
173006149746SLuiz Augusto von Dentz 		return NULL;
173106149746SLuiz Augusto von Dentz 
173206149746SLuiz Augusto von Dentz 	link->conn = hci_conn_hold(conn);
173306149746SLuiz Augusto von Dentz 	conn->link = link;
173406149746SLuiz Augusto von Dentz 	conn->parent = hci_conn_get(parent);
173506149746SLuiz Augusto von Dentz 
173606149746SLuiz Augusto von Dentz 	/* Use list_add_tail_rcu append to the list */
173706149746SLuiz Augusto von Dentz 	list_add_tail_rcu(&link->list, &parent->link_list);
173806149746SLuiz Augusto von Dentz 
173906149746SLuiz Augusto von Dentz 	return link;
174006149746SLuiz Augusto von Dentz }
174106149746SLuiz Augusto von Dentz 
174210c62ddcSFrédéric Dalleau struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst,
1743b2af264aSKiran K 				 __u16 setting, struct bt_codec *codec)
1744db474275SVinicius Costa Gomes {
1745db474275SVinicius Costa Gomes 	struct hci_conn *acl;
1746db474275SVinicius Costa Gomes 	struct hci_conn *sco;
174706149746SLuiz Augusto von Dentz 	struct hci_link *link;
1748db474275SVinicius Costa Gomes 
174976b13996SManish Mandlik 	acl = hci_connect_acl(hdev, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING,
175076b13996SManish Mandlik 			      CONN_REASON_SCO_CONNECT);
1751db474275SVinicius Costa Gomes 	if (IS_ERR(acl))
17525b7f9909SMarcel Holtmann 		return acl;
17531da177e4SLinus Torvalds 
175470f23020SAndrei Emeltchenko 	sco = hci_conn_hash_lookup_ba(hdev, type, dst);
175570f23020SAndrei Emeltchenko 	if (!sco) {
175684cb0143SZiyang Xuan 		sco = hci_conn_add_unset(hdev, type, dst, HCI_ROLE_MASTER);
1757ad3f7986SSungwoo Kim 		if (IS_ERR(sco)) {
175876a68ba0SDavid Herrmann 			hci_conn_drop(acl);
1759ad3f7986SSungwoo Kim 			return sco;
17601da177e4SLinus Torvalds 		}
17611da177e4SLinus Torvalds 	}
17625b7f9909SMarcel Holtmann 
176306149746SLuiz Augusto von Dentz 	link = hci_conn_link(acl, sco);
176406149746SLuiz Augusto von Dentz 	if (!link) {
176506149746SLuiz Augusto von Dentz 		hci_conn_drop(acl);
176606149746SLuiz Augusto von Dentz 		hci_conn_drop(sco);
1767b4066eb0SSiddh Raman Pant 		return ERR_PTR(-ENOLINK);
176806149746SLuiz Augusto von Dentz 	}
17691da177e4SLinus Torvalds 
177010c62ddcSFrédéric Dalleau 	sco->setting = setting;
1771b2af264aSKiran K 	sco->codec = *codec;
177210c62ddcSFrédéric Dalleau 
17731da177e4SLinus Torvalds 	if (acl->state == BT_CONNECTED &&
1774b6a0dc82SMarcel Holtmann 	    (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
177558a681efSJohan Hedberg 		set_bit(HCI_CONN_POWER_SAVE, &acl->flags);
177614b12d0bSJaikumar Ganesh 		hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON);
1777c390216bSNick Pelly 
177851a8efd7SJohan Hedberg 		if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->flags)) {
1779e73439d8SMarcel Holtmann 			/* defer SCO setup until mode change completed */
178051a8efd7SJohan Hedberg 			set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->flags);
1781e73439d8SMarcel Holtmann 			return sco;
1782e73439d8SMarcel Holtmann 		}
1783e73439d8SMarcel Holtmann 
1784e73439d8SMarcel Holtmann 		hci_sco_setup(acl, 0x00);
1785b6a0dc82SMarcel Holtmann 	}
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds 	return sco;
17881da177e4SLinus Torvalds }
17891da177e4SLinus Torvalds 
1790eca0ae4aSLuiz Augusto von Dentz static int hci_le_create_big(struct hci_conn *conn, struct bt_iso_qos *qos)
1791eca0ae4aSLuiz Augusto von Dentz {
1792eca0ae4aSLuiz Augusto von Dentz 	struct hci_dev *hdev = conn->hdev;
1793eca0ae4aSLuiz Augusto von Dentz 	struct hci_cp_le_create_big cp;
1794a0bfde16SIulia Tanasescu 	struct iso_list_data data;
1795eca0ae4aSLuiz Augusto von Dentz 
1796eca0ae4aSLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
1797eca0ae4aSLuiz Augusto von Dentz 
1798a0bfde16SIulia Tanasescu 	data.big = qos->bcast.big;
1799a0bfde16SIulia Tanasescu 	data.bis = qos->bcast.bis;
1800a0bfde16SIulia Tanasescu 	data.count = 0;
1801a0bfde16SIulia Tanasescu 
1802a0bfde16SIulia Tanasescu 	/* Create a BIS for each bound connection */
1803a0bfde16SIulia Tanasescu 	hci_conn_hash_list_state(hdev, bis_list, ISO_LINK,
1804a0bfde16SIulia Tanasescu 				 BT_BOUND, &data);
1805a0bfde16SIulia Tanasescu 
18060fe8c8d0SIulia Tanasescu 	cp.handle = qos->bcast.big;
18070fe8c8d0SIulia Tanasescu 	cp.adv_handle = qos->bcast.bis;
1808a0bfde16SIulia Tanasescu 	cp.num_bis  = data.count;
18090fe8c8d0SIulia Tanasescu 	hci_cpu_to_le24(qos->bcast.out.interval, cp.bis.sdu_interval);
18100fe8c8d0SIulia Tanasescu 	cp.bis.sdu = cpu_to_le16(qos->bcast.out.sdu);
18110fe8c8d0SIulia Tanasescu 	cp.bis.latency =  cpu_to_le16(qos->bcast.out.latency);
18120fe8c8d0SIulia Tanasescu 	cp.bis.rtn  = qos->bcast.out.rtn;
18130fe8c8d0SIulia Tanasescu 	cp.bis.phy  = qos->bcast.out.phy;
18140fe8c8d0SIulia Tanasescu 	cp.bis.packing = qos->bcast.packing;
18150fe8c8d0SIulia Tanasescu 	cp.bis.framing = qos->bcast.framing;
18160fe8c8d0SIulia Tanasescu 	cp.bis.encryption = qos->bcast.encryption;
18170fe8c8d0SIulia Tanasescu 	memcpy(cp.bis.bcode, qos->bcast.bcode, sizeof(cp.bis.bcode));
1818eca0ae4aSLuiz Augusto von Dentz 
1819eca0ae4aSLuiz Augusto von Dentz 	return hci_send_cmd(hdev, HCI_OP_LE_CREATE_BIG, sizeof(cp), &cp);
1820eca0ae4aSLuiz Augusto von Dentz }
1821eca0ae4aSLuiz Augusto von Dentz 
18226b9545dcSPauli Virtanen static int set_cig_params_sync(struct hci_dev *hdev, void *data)
18236b9545dcSPauli Virtanen {
1824a1f6c3aeSLuiz Augusto von Dentz 	u8 cig_id = PTR_UINT(data);
1825a0912892SLuiz Augusto von Dentz 	struct hci_conn *conn;
1826a0912892SLuiz Augusto von Dentz 	struct bt_iso_qos *qos;
1827a0912892SLuiz Augusto von Dentz 	struct iso_cig_params pdu;
1828a0912892SLuiz Augusto von Dentz 	u8 cis_id;
18296b9545dcSPauli Virtanen 
1830a0912892SLuiz Augusto von Dentz 	conn = hci_conn_hash_lookup_cig(hdev, cig_id);
1831a0912892SLuiz Augusto von Dentz 	if (!conn)
1832a0912892SLuiz Augusto von Dentz 		return 0;
1833a0912892SLuiz Augusto von Dentz 
1834a0912892SLuiz Augusto von Dentz 	memset(&pdu, 0, sizeof(pdu));
1835a0912892SLuiz Augusto von Dentz 
1836a0912892SLuiz Augusto von Dentz 	qos = &conn->iso_qos;
1837a0912892SLuiz Augusto von Dentz 	pdu.cp.cig_id = cig_id;
1838a0912892SLuiz Augusto von Dentz 	hci_cpu_to_le24(qos->ucast.out.interval, pdu.cp.c_interval);
1839a0912892SLuiz Augusto von Dentz 	hci_cpu_to_le24(qos->ucast.in.interval, pdu.cp.p_interval);
1840a0912892SLuiz Augusto von Dentz 	pdu.cp.sca = qos->ucast.sca;
1841a0912892SLuiz Augusto von Dentz 	pdu.cp.packing = qos->ucast.packing;
1842a0912892SLuiz Augusto von Dentz 	pdu.cp.framing = qos->ucast.framing;
1843a0912892SLuiz Augusto von Dentz 	pdu.cp.c_latency = cpu_to_le16(qos->ucast.out.latency);
1844a0912892SLuiz Augusto von Dentz 	pdu.cp.p_latency = cpu_to_le16(qos->ucast.in.latency);
1845a0912892SLuiz Augusto von Dentz 
1846a0912892SLuiz Augusto von Dentz 	/* Reprogram all CIS(s) with the same CIG, valid range are:
1847a0912892SLuiz Augusto von Dentz 	 * num_cis: 0x00 to 0x1F
1848a0912892SLuiz Augusto von Dentz 	 * cis_id: 0x00 to 0xEF
1849a0912892SLuiz Augusto von Dentz 	 */
1850a0912892SLuiz Augusto von Dentz 	for (cis_id = 0x00; cis_id < 0xf0 &&
1851a0912892SLuiz Augusto von Dentz 	     pdu.cp.num_cis < ARRAY_SIZE(pdu.cis); cis_id++) {
1852a0912892SLuiz Augusto von Dentz 		struct hci_cis_params *cis;
1853a0912892SLuiz Augusto von Dentz 
1854a0912892SLuiz Augusto von Dentz 		conn = hci_conn_hash_lookup_cis(hdev, NULL, 0, cig_id, cis_id);
1855a0912892SLuiz Augusto von Dentz 		if (!conn)
1856a0912892SLuiz Augusto von Dentz 			continue;
1857a0912892SLuiz Augusto von Dentz 
1858a0912892SLuiz Augusto von Dentz 		qos = &conn->iso_qos;
1859a0912892SLuiz Augusto von Dentz 
1860a0912892SLuiz Augusto von Dentz 		cis = &pdu.cis[pdu.cp.num_cis++];
1861a0912892SLuiz Augusto von Dentz 		cis->cis_id = cis_id;
1862a0912892SLuiz Augusto von Dentz 		cis->c_sdu  = cpu_to_le16(conn->iso_qos.ucast.out.sdu);
1863a0912892SLuiz Augusto von Dentz 		cis->p_sdu  = cpu_to_le16(conn->iso_qos.ucast.in.sdu);
1864a0912892SLuiz Augusto von Dentz 		cis->c_phy  = qos->ucast.out.phy ? qos->ucast.out.phy :
1865a0912892SLuiz Augusto von Dentz 			      qos->ucast.in.phy;
1866a0912892SLuiz Augusto von Dentz 		cis->p_phy  = qos->ucast.in.phy ? qos->ucast.in.phy :
1867a0912892SLuiz Augusto von Dentz 			      qos->ucast.out.phy;
1868a0912892SLuiz Augusto von Dentz 		cis->c_rtn  = qos->ucast.out.rtn;
1869a0912892SLuiz Augusto von Dentz 		cis->p_rtn  = qos->ucast.in.rtn;
1870a0912892SLuiz Augusto von Dentz 	}
1871a0912892SLuiz Augusto von Dentz 
1872a0912892SLuiz Augusto von Dentz 	if (!pdu.cp.num_cis)
1873a0912892SLuiz Augusto von Dentz 		return 0;
1874a0912892SLuiz Augusto von Dentz 
1875a0912892SLuiz Augusto von Dentz 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_CIG_PARAMS,
1876a0912892SLuiz Augusto von Dentz 				     sizeof(pdu.cp) +
1877a0912892SLuiz Augusto von Dentz 				     pdu.cp.num_cis * sizeof(pdu.cis[0]), &pdu,
18786b9545dcSPauli Virtanen 				     HCI_CMD_TIMEOUT);
18796b9545dcSPauli Virtanen }
18806b9545dcSPauli Virtanen 
188126afbd82SLuiz Augusto von Dentz static bool hci_le_set_cig_params(struct hci_conn *conn, struct bt_iso_qos *qos)
188226afbd82SLuiz Augusto von Dentz {
188326afbd82SLuiz Augusto von Dentz 	struct hci_dev *hdev = conn->hdev;
188426afbd82SLuiz Augusto von Dentz 	struct iso_list_data data;
188526afbd82SLuiz Augusto von Dentz 
188626afbd82SLuiz Augusto von Dentz 	memset(&data, 0, sizeof(data));
188726afbd82SLuiz Augusto von Dentz 
1888e6a7a46bSPauli Virtanen 	/* Allocate first still reconfigurable CIG if not set */
18890fe8c8d0SIulia Tanasescu 	if (qos->ucast.cig == BT_ISO_QOS_CIG_UNSET) {
1890e6a7a46bSPauli Virtanen 		for (data.cig = 0x00; data.cig < 0xf0; data.cig++) {
189126afbd82SLuiz Augusto von Dentz 			data.count = 0;
189226afbd82SLuiz Augusto von Dentz 
1893e6a7a46bSPauli Virtanen 			hci_conn_hash_list_state(hdev, find_cis, ISO_LINK,
1894e6a7a46bSPauli Virtanen 						 BT_CONNECT, &data);
189526afbd82SLuiz Augusto von Dentz 			if (data.count)
189626afbd82SLuiz Augusto von Dentz 				continue;
189726afbd82SLuiz Augusto von Dentz 
1898e6a7a46bSPauli Virtanen 			hci_conn_hash_list_state(hdev, find_cis, ISO_LINK,
189926afbd82SLuiz Augusto von Dentz 						 BT_CONNECTED, &data);
190026afbd82SLuiz Augusto von Dentz 			if (!data.count)
190126afbd82SLuiz Augusto von Dentz 				break;
190226afbd82SLuiz Augusto von Dentz 		}
190326afbd82SLuiz Augusto von Dentz 
1904e6a7a46bSPauli Virtanen 		if (data.cig == 0xf0)
190526afbd82SLuiz Augusto von Dentz 			return false;
190626afbd82SLuiz Augusto von Dentz 
190726afbd82SLuiz Augusto von Dentz 		/* Update CIG */
19080fe8c8d0SIulia Tanasescu 		qos->ucast.cig = data.cig;
190926afbd82SLuiz Augusto von Dentz 	}
191026afbd82SLuiz Augusto von Dentz 
19110fe8c8d0SIulia Tanasescu 	if (qos->ucast.cis != BT_ISO_QOS_CIS_UNSET) {
1912a0912892SLuiz Augusto von Dentz 		if (hci_conn_hash_lookup_cis(hdev, NULL, 0, qos->ucast.cig,
1913a0912892SLuiz Augusto von Dentz 					     qos->ucast.cis))
191426afbd82SLuiz Augusto von Dentz 			return false;
1915a0912892SLuiz Augusto von Dentz 		goto done;
191626afbd82SLuiz Augusto von Dentz 	}
191726afbd82SLuiz Augusto von Dentz 
1918a0912892SLuiz Augusto von Dentz 	/* Allocate first available CIS if not set */
1919a0912892SLuiz Augusto von Dentz 	for (data.cig = qos->ucast.cig, data.cis = 0x00; data.cis < 0xf0;
1920a0912892SLuiz Augusto von Dentz 	     data.cis++) {
1921a0912892SLuiz Augusto von Dentz 		if (!hci_conn_hash_lookup_cis(hdev, NULL, 0, data.cig,
1922a0912892SLuiz Augusto von Dentz 					      data.cis)) {
192326afbd82SLuiz Augusto von Dentz 			/* Update CIS */
19240fe8c8d0SIulia Tanasescu 			qos->ucast.cis = data.cis;
1925a0912892SLuiz Augusto von Dentz 			break;
192626afbd82SLuiz Augusto von Dentz 		}
192726afbd82SLuiz Augusto von Dentz 	}
192826afbd82SLuiz Augusto von Dentz 
1929a0912892SLuiz Augusto von Dentz 	if (qos->ucast.cis == BT_ISO_QOS_CIS_UNSET)
193026afbd82SLuiz Augusto von Dentz 		return false;
193126afbd82SLuiz Augusto von Dentz 
1932a0912892SLuiz Augusto von Dentz done:
1933a0912892SLuiz Augusto von Dentz 	if (hci_cmd_sync_queue(hdev, set_cig_params_sync,
1934a1f6c3aeSLuiz Augusto von Dentz 			       UINT_PTR(qos->ucast.cig), NULL) < 0)
193526afbd82SLuiz Augusto von Dentz 		return false;
193626afbd82SLuiz Augusto von Dentz 
193726afbd82SLuiz Augusto von Dentz 	return true;
193826afbd82SLuiz Augusto von Dentz }
193926afbd82SLuiz Augusto von Dentz 
194026afbd82SLuiz Augusto von Dentz struct hci_conn *hci_bind_cis(struct hci_dev *hdev, bdaddr_t *dst,
194126afbd82SLuiz Augusto von Dentz 			      __u8 dst_type, struct bt_iso_qos *qos)
194226afbd82SLuiz Augusto von Dentz {
194326afbd82SLuiz Augusto von Dentz 	struct hci_conn *cis;
194426afbd82SLuiz Augusto von Dentz 
1945c14516faSLuiz Augusto von Dentz 	cis = hci_conn_hash_lookup_cis(hdev, dst, dst_type, qos->ucast.cig,
1946c14516faSLuiz Augusto von Dentz 				       qos->ucast.cis);
194726afbd82SLuiz Augusto von Dentz 	if (!cis) {
194884cb0143SZiyang Xuan 		cis = hci_conn_add_unset(hdev, ISO_LINK, dst, HCI_ROLE_MASTER);
1949ad3f7986SSungwoo Kim 		if (IS_ERR(cis))
1950ad3f7986SSungwoo Kim 			return cis;
195126afbd82SLuiz Augusto von Dentz 		cis->cleanup = cis_cleanup;
1952b36a234dSPauli Virtanen 		cis->dst_type = dst_type;
1953b5793de3SPauli Virtanen 		cis->iso_qos.ucast.cig = BT_ISO_QOS_CIG_UNSET;
1954b5793de3SPauli Virtanen 		cis->iso_qos.ucast.cis = BT_ISO_QOS_CIS_UNSET;
195526afbd82SLuiz Augusto von Dentz 	}
195626afbd82SLuiz Augusto von Dentz 
195726afbd82SLuiz Augusto von Dentz 	if (cis->state == BT_CONNECTED)
195826afbd82SLuiz Augusto von Dentz 		return cis;
195926afbd82SLuiz Augusto von Dentz 
196026afbd82SLuiz Augusto von Dentz 	/* Check if CIS has been set and the settings matches */
196126afbd82SLuiz Augusto von Dentz 	if (cis->state == BT_BOUND &&
196226afbd82SLuiz Augusto von Dentz 	    !memcmp(&cis->iso_qos, qos, sizeof(*qos)))
196326afbd82SLuiz Augusto von Dentz 		return cis;
196426afbd82SLuiz Augusto von Dentz 
196526afbd82SLuiz Augusto von Dentz 	/* Update LINK PHYs according to QoS preference */
19660fe8c8d0SIulia Tanasescu 	cis->le_tx_phy = qos->ucast.out.phy;
19670fe8c8d0SIulia Tanasescu 	cis->le_rx_phy = qos->ucast.in.phy;
196826afbd82SLuiz Augusto von Dentz 
196926afbd82SLuiz Augusto von Dentz 	/* If output interval is not set use the input interval as it cannot be
197026afbd82SLuiz Augusto von Dentz 	 * 0x000000.
197126afbd82SLuiz Augusto von Dentz 	 */
19720fe8c8d0SIulia Tanasescu 	if (!qos->ucast.out.interval)
19730fe8c8d0SIulia Tanasescu 		qos->ucast.out.interval = qos->ucast.in.interval;
197426afbd82SLuiz Augusto von Dentz 
197526afbd82SLuiz Augusto von Dentz 	/* If input interval is not set use the output interval as it cannot be
197626afbd82SLuiz Augusto von Dentz 	 * 0x000000.
197726afbd82SLuiz Augusto von Dentz 	 */
19780fe8c8d0SIulia Tanasescu 	if (!qos->ucast.in.interval)
19790fe8c8d0SIulia Tanasescu 		qos->ucast.in.interval = qos->ucast.out.interval;
198026afbd82SLuiz Augusto von Dentz 
198126afbd82SLuiz Augusto von Dentz 	/* If output latency is not set use the input latency as it cannot be
198226afbd82SLuiz Augusto von Dentz 	 * 0x0000.
198326afbd82SLuiz Augusto von Dentz 	 */
19840fe8c8d0SIulia Tanasescu 	if (!qos->ucast.out.latency)
19850fe8c8d0SIulia Tanasescu 		qos->ucast.out.latency = qos->ucast.in.latency;
198626afbd82SLuiz Augusto von Dentz 
198726afbd82SLuiz Augusto von Dentz 	/* If input latency is not set use the output latency as it cannot be
198826afbd82SLuiz Augusto von Dentz 	 * 0x0000.
198926afbd82SLuiz Augusto von Dentz 	 */
19900fe8c8d0SIulia Tanasescu 	if (!qos->ucast.in.latency)
19910fe8c8d0SIulia Tanasescu 		qos->ucast.in.latency = qos->ucast.out.latency;
199226afbd82SLuiz Augusto von Dentz 
199326afbd82SLuiz Augusto von Dentz 	if (!hci_le_set_cig_params(cis, qos)) {
199426afbd82SLuiz Augusto von Dentz 		hci_conn_drop(cis);
199526afbd82SLuiz Augusto von Dentz 		return ERR_PTR(-EINVAL);
199626afbd82SLuiz Augusto von Dentz 	}
199726afbd82SLuiz Augusto von Dentz 
199869997d50SPauli Virtanen 	hci_conn_hold(cis);
199969997d50SPauli Virtanen 
200026afbd82SLuiz Augusto von Dentz 	cis->iso_qos = *qos;
200126afbd82SLuiz Augusto von Dentz 	cis->state = BT_BOUND;
200226afbd82SLuiz Augusto von Dentz 
200326afbd82SLuiz Augusto von Dentz 	return cis;
200426afbd82SLuiz Augusto von Dentz }
200526afbd82SLuiz Augusto von Dentz 
200626afbd82SLuiz Augusto von Dentz bool hci_iso_setup_path(struct hci_conn *conn)
200726afbd82SLuiz Augusto von Dentz {
200826afbd82SLuiz Augusto von Dentz 	struct hci_dev *hdev = conn->hdev;
200926afbd82SLuiz Augusto von Dentz 	struct hci_cp_le_setup_iso_path cmd;
201026afbd82SLuiz Augusto von Dentz 
201126afbd82SLuiz Augusto von Dentz 	memset(&cmd, 0, sizeof(cmd));
201226afbd82SLuiz Augusto von Dentz 
20130fe8c8d0SIulia Tanasescu 	if (conn->iso_qos.ucast.out.sdu) {
201426afbd82SLuiz Augusto von Dentz 		cmd.handle = cpu_to_le16(conn->handle);
201526afbd82SLuiz Augusto von Dentz 		cmd.direction = 0x00; /* Input (Host to Controller) */
201626afbd82SLuiz Augusto von Dentz 		cmd.path = 0x00; /* HCI path if enabled */
201726afbd82SLuiz Augusto von Dentz 		cmd.codec = 0x03; /* Transparent Data */
201826afbd82SLuiz Augusto von Dentz 
201926afbd82SLuiz Augusto von Dentz 		if (hci_send_cmd(hdev, HCI_OP_LE_SETUP_ISO_PATH, sizeof(cmd),
202026afbd82SLuiz Augusto von Dentz 				 &cmd) < 0)
202126afbd82SLuiz Augusto von Dentz 			return false;
202226afbd82SLuiz Augusto von Dentz 	}
202326afbd82SLuiz Augusto von Dentz 
20240fe8c8d0SIulia Tanasescu 	if (conn->iso_qos.ucast.in.sdu) {
202526afbd82SLuiz Augusto von Dentz 		cmd.handle = cpu_to_le16(conn->handle);
202626afbd82SLuiz Augusto von Dentz 		cmd.direction = 0x01; /* Output (Controller to Host) */
202726afbd82SLuiz Augusto von Dentz 		cmd.path = 0x00; /* HCI path if enabled */
202826afbd82SLuiz Augusto von Dentz 		cmd.codec = 0x03; /* Transparent Data */
202926afbd82SLuiz Augusto von Dentz 
203026afbd82SLuiz Augusto von Dentz 		if (hci_send_cmd(hdev, HCI_OP_LE_SETUP_ISO_PATH, sizeof(cmd),
203126afbd82SLuiz Augusto von Dentz 				 &cmd) < 0)
203226afbd82SLuiz Augusto von Dentz 			return false;
203326afbd82SLuiz Augusto von Dentz 	}
203426afbd82SLuiz Augusto von Dentz 
203526afbd82SLuiz Augusto von Dentz 	return true;
203626afbd82SLuiz Augusto von Dentz }
203726afbd82SLuiz Augusto von Dentz 
20387f74563eSPauli Virtanen int hci_conn_check_create_cis(struct hci_conn *conn)
20397f74563eSPauli Virtanen {
20407f74563eSPauli Virtanen 	if (conn->type != ISO_LINK || !bacmp(&conn->dst, BDADDR_ANY))
20417f74563eSPauli Virtanen 		return -EINVAL;
20427f74563eSPauli Virtanen 
20437f74563eSPauli Virtanen 	if (!conn->parent || conn->parent->state != BT_CONNECTED ||
20449f78191cSLuiz Augusto von Dentz 	    conn->state != BT_CONNECT || HCI_CONN_HANDLE_UNSET(conn->handle))
20457f74563eSPauli Virtanen 		return 1;
20467f74563eSPauli Virtanen 
20477f74563eSPauli Virtanen 	return 0;
20487f74563eSPauli Virtanen }
20497f74563eSPauli Virtanen 
205026afbd82SLuiz Augusto von Dentz static int hci_create_cis_sync(struct hci_dev *hdev, void *data)
205126afbd82SLuiz Augusto von Dentz {
20527f74563eSPauli Virtanen 	return hci_le_create_cis_sync(hdev);
205326afbd82SLuiz Augusto von Dentz }
205426afbd82SLuiz Augusto von Dentz 
20557f74563eSPauli Virtanen int hci_le_create_cis_pending(struct hci_dev *hdev)
205626afbd82SLuiz Augusto von Dentz {
20577f74563eSPauli Virtanen 	struct hci_conn *conn;
20587f74563eSPauli Virtanen 	bool pending = false;
205926afbd82SLuiz Augusto von Dentz 
20607f74563eSPauli Virtanen 	rcu_read_lock();
206106149746SLuiz Augusto von Dentz 
20627f74563eSPauli Virtanen 	list_for_each_entry_rcu(conn, &hdev->conn_hash.list, list) {
20637f74563eSPauli Virtanen 		if (test_bit(HCI_CONN_CREATE_CIS, &conn->flags)) {
20647f74563eSPauli Virtanen 			rcu_read_unlock();
20657f74563eSPauli Virtanen 			return -EBUSY;
206606149746SLuiz Augusto von Dentz 		}
206706149746SLuiz Augusto von Dentz 
20687f74563eSPauli Virtanen 		if (!hci_conn_check_create_cis(conn))
20697f74563eSPauli Virtanen 			pending = true;
207026afbd82SLuiz Augusto von Dentz 	}
207126afbd82SLuiz Augusto von Dentz 
20727f74563eSPauli Virtanen 	rcu_read_unlock();
20737f74563eSPauli Virtanen 
20747f74563eSPauli Virtanen 	if (!pending)
207526afbd82SLuiz Augusto von Dentz 		return 0;
207626afbd82SLuiz Augusto von Dentz 
207726afbd82SLuiz Augusto von Dentz 	/* Queue Create CIS */
20787f74563eSPauli Virtanen 	return hci_cmd_sync_queue(hdev, hci_create_cis_sync, NULL, NULL);
207926afbd82SLuiz Augusto von Dentz }
208026afbd82SLuiz Augusto von Dentz 
208126afbd82SLuiz Augusto von Dentz static void hci_iso_qos_setup(struct hci_dev *hdev, struct hci_conn *conn,
208226afbd82SLuiz Augusto von Dentz 			      struct bt_iso_io_qos *qos, __u8 phy)
208326afbd82SLuiz Augusto von Dentz {
208426afbd82SLuiz Augusto von Dentz 	/* Only set MTU if PHY is enabled */
2085ad3f7986SSungwoo Kim 	if (!qos->sdu && qos->phy)
2086ad3f7986SSungwoo Kim 		qos->sdu = conn->mtu;
208726afbd82SLuiz Augusto von Dentz 
208826afbd82SLuiz Augusto von Dentz 	/* Use the same PHY as ACL if set to any */
208926afbd82SLuiz Augusto von Dentz 	if (qos->phy == BT_ISO_PHY_ANY)
209026afbd82SLuiz Augusto von Dentz 		qos->phy = phy;
209126afbd82SLuiz Augusto von Dentz 
209226afbd82SLuiz Augusto von Dentz 	/* Use LE ACL connection interval if not set */
209326afbd82SLuiz Augusto von Dentz 	if (!qos->interval)
209426afbd82SLuiz Augusto von Dentz 		/* ACL interval unit in 1.25 ms to us */
209526afbd82SLuiz Augusto von Dentz 		qos->interval = conn->le_conn_interval * 1250;
209626afbd82SLuiz Augusto von Dentz 
209726afbd82SLuiz Augusto von Dentz 	/* Use LE ACL connection latency if not set */
209826afbd82SLuiz Augusto von Dentz 	if (!qos->latency)
209926afbd82SLuiz Augusto von Dentz 		qos->latency = conn->le_conn_latency;
210026afbd82SLuiz Augusto von Dentz }
210126afbd82SLuiz Augusto von Dentz 
2102eca0ae4aSLuiz Augusto von Dentz static int create_big_sync(struct hci_dev *hdev, void *data)
2103eca0ae4aSLuiz Augusto von Dentz {
2104eca0ae4aSLuiz Augusto von Dentz 	struct hci_conn *conn = data;
2105eca0ae4aSLuiz Augusto von Dentz 	struct bt_iso_qos *qos = &conn->iso_qos;
2106eca0ae4aSLuiz Augusto von Dentz 	u16 interval, sync_interval = 0;
2107eca0ae4aSLuiz Augusto von Dentz 	u32 flags = 0;
2108eca0ae4aSLuiz Augusto von Dentz 	int err;
2109eca0ae4aSLuiz Augusto von Dentz 
21100fe8c8d0SIulia Tanasescu 	if (qos->bcast.out.phy == 0x02)
2111eca0ae4aSLuiz Augusto von Dentz 		flags |= MGMT_ADV_FLAG_SEC_2M;
2112eca0ae4aSLuiz Augusto von Dentz 
2113eca0ae4aSLuiz Augusto von Dentz 	/* Align intervals */
211414f0dcecSLuiz Augusto von Dentz 	interval = (qos->bcast.out.interval / 1250) * qos->bcast.sync_factor;
2115eca0ae4aSLuiz Augusto von Dentz 
21160fe8c8d0SIulia Tanasescu 	if (qos->bcast.bis)
211714f0dcecSLuiz Augusto von Dentz 		sync_interval = interval * 4;
2118eca0ae4aSLuiz Augusto von Dentz 
21190fe8c8d0SIulia Tanasescu 	err = hci_start_per_adv_sync(hdev, qos->bcast.bis, conn->le_per_adv_data_len,
2120eca0ae4aSLuiz Augusto von Dentz 				     conn->le_per_adv_data, flags, interval,
2121eca0ae4aSLuiz Augusto von Dentz 				     interval, sync_interval);
2122eca0ae4aSLuiz Augusto von Dentz 	if (err)
2123eca0ae4aSLuiz Augusto von Dentz 		return err;
2124eca0ae4aSLuiz Augusto von Dentz 
2125eca0ae4aSLuiz Augusto von Dentz 	return hci_le_create_big(conn, &conn->iso_qos);
2126eca0ae4aSLuiz Augusto von Dentz }
2127eca0ae4aSLuiz Augusto von Dentz 
2128eca0ae4aSLuiz Augusto von Dentz static void create_pa_complete(struct hci_dev *hdev, void *data, int err)
2129eca0ae4aSLuiz Augusto von Dentz {
2130eca0ae4aSLuiz Augusto von Dentz 	struct hci_cp_le_pa_create_sync *cp = data;
2131eca0ae4aSLuiz Augusto von Dentz 
2132eca0ae4aSLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "");
2133eca0ae4aSLuiz Augusto von Dentz 
2134eca0ae4aSLuiz Augusto von Dentz 	if (err)
2135eca0ae4aSLuiz Augusto von Dentz 		bt_dev_err(hdev, "Unable to create PA: %d", err);
2136eca0ae4aSLuiz Augusto von Dentz 
2137eca0ae4aSLuiz Augusto von Dentz 	kfree(cp);
2138eca0ae4aSLuiz Augusto von Dentz }
2139eca0ae4aSLuiz Augusto von Dentz 
2140eca0ae4aSLuiz Augusto von Dentz static int create_pa_sync(struct hci_dev *hdev, void *data)
2141eca0ae4aSLuiz Augusto von Dentz {
2142eca0ae4aSLuiz Augusto von Dentz 	struct hci_cp_le_pa_create_sync *cp = data;
2143eca0ae4aSLuiz Augusto von Dentz 	int err;
2144eca0ae4aSLuiz Augusto von Dentz 
2145eca0ae4aSLuiz Augusto von Dentz 	err = __hci_cmd_sync_status(hdev, HCI_OP_LE_PA_CREATE_SYNC,
2146eca0ae4aSLuiz Augusto von Dentz 				    sizeof(*cp), cp, HCI_CMD_TIMEOUT);
2147eca0ae4aSLuiz Augusto von Dentz 	if (err) {
2148eca0ae4aSLuiz Augusto von Dentz 		hci_dev_clear_flag(hdev, HCI_PA_SYNC);
2149eca0ae4aSLuiz Augusto von Dentz 		return err;
2150eca0ae4aSLuiz Augusto von Dentz 	}
2151eca0ae4aSLuiz Augusto von Dentz 
2152eca0ae4aSLuiz Augusto von Dentz 	return hci_update_passive_scan_sync(hdev);
2153eca0ae4aSLuiz Augusto von Dentz }
2154eca0ae4aSLuiz Augusto von Dentz 
2155eca0ae4aSLuiz Augusto von Dentz int hci_pa_create_sync(struct hci_dev *hdev, bdaddr_t *dst, __u8 dst_type,
21560fe8c8d0SIulia Tanasescu 		       __u8 sid, struct bt_iso_qos *qos)
2157eca0ae4aSLuiz Augusto von Dentz {
2158eca0ae4aSLuiz Augusto von Dentz 	struct hci_cp_le_pa_create_sync *cp;
2159eca0ae4aSLuiz Augusto von Dentz 
2160eca0ae4aSLuiz Augusto von Dentz 	if (hci_dev_test_and_set_flag(hdev, HCI_PA_SYNC))
2161eca0ae4aSLuiz Augusto von Dentz 		return -EBUSY;
2162eca0ae4aSLuiz Augusto von Dentz 
216337224a29SJiapeng Chong 	cp = kzalloc(sizeof(*cp), GFP_KERNEL);
2164eca0ae4aSLuiz Augusto von Dentz 	if (!cp) {
2165eca0ae4aSLuiz Augusto von Dentz 		hci_dev_clear_flag(hdev, HCI_PA_SYNC);
2166eca0ae4aSLuiz Augusto von Dentz 		return -ENOMEM;
2167eca0ae4aSLuiz Augusto von Dentz 	}
2168eca0ae4aSLuiz Augusto von Dentz 
21690fe8c8d0SIulia Tanasescu 	cp->options = qos->bcast.options;
2170eca0ae4aSLuiz Augusto von Dentz 	cp->sid = sid;
2171eca0ae4aSLuiz Augusto von Dentz 	cp->addr_type = dst_type;
2172eca0ae4aSLuiz Augusto von Dentz 	bacpy(&cp->addr, dst);
21730fe8c8d0SIulia Tanasescu 	cp->skip = cpu_to_le16(qos->bcast.skip);
21740fe8c8d0SIulia Tanasescu 	cp->sync_timeout = cpu_to_le16(qos->bcast.sync_timeout);
21750fe8c8d0SIulia Tanasescu 	cp->sync_cte_type = qos->bcast.sync_cte_type;
2176eca0ae4aSLuiz Augusto von Dentz 
2177eca0ae4aSLuiz Augusto von Dentz 	/* Queue start pa_create_sync and scan */
2178eca0ae4aSLuiz Augusto von Dentz 	return hci_cmd_sync_queue(hdev, create_pa_sync, cp, create_pa_complete);
2179eca0ae4aSLuiz Augusto von Dentz }
2180eca0ae4aSLuiz Augusto von Dentz 
2181fbdc4bc4SIulia Tanasescu int hci_le_big_create_sync(struct hci_dev *hdev, struct hci_conn *hcon,
2182fbdc4bc4SIulia Tanasescu 			   struct bt_iso_qos *qos,
2183eca0ae4aSLuiz Augusto von Dentz 			   __u16 sync_handle, __u8 num_bis, __u8 bis[])
2184eca0ae4aSLuiz Augusto von Dentz {
2185eca0ae4aSLuiz Augusto von Dentz 	struct _packed {
2186eca0ae4aSLuiz Augusto von Dentz 		struct hci_cp_le_big_create_sync cp;
2187eca0ae4aSLuiz Augusto von Dentz 		__u8  bis[0x11];
2188eca0ae4aSLuiz Augusto von Dentz 	} pdu;
2189eca0ae4aSLuiz Augusto von Dentz 	int err;
2190eca0ae4aSLuiz Augusto von Dentz 
2191eca0ae4aSLuiz Augusto von Dentz 	if (num_bis > sizeof(pdu.bis))
2192eca0ae4aSLuiz Augusto von Dentz 		return -EINVAL;
2193eca0ae4aSLuiz Augusto von Dentz 
2194eca0ae4aSLuiz Augusto von Dentz 	err = qos_set_big(hdev, qos);
2195eca0ae4aSLuiz Augusto von Dentz 	if (err)
2196eca0ae4aSLuiz Augusto von Dentz 		return err;
2197eca0ae4aSLuiz Augusto von Dentz 
2198fbdc4bc4SIulia Tanasescu 	if (hcon)
2199fbdc4bc4SIulia Tanasescu 		hcon->iso_qos.bcast.big = qos->bcast.big;
2200fbdc4bc4SIulia Tanasescu 
2201eca0ae4aSLuiz Augusto von Dentz 	memset(&pdu, 0, sizeof(pdu));
22020fe8c8d0SIulia Tanasescu 	pdu.cp.handle = qos->bcast.big;
2203eca0ae4aSLuiz Augusto von Dentz 	pdu.cp.sync_handle = cpu_to_le16(sync_handle);
22040fe8c8d0SIulia Tanasescu 	pdu.cp.encryption = qos->bcast.encryption;
22050fe8c8d0SIulia Tanasescu 	memcpy(pdu.cp.bcode, qos->bcast.bcode, sizeof(pdu.cp.bcode));
22060fe8c8d0SIulia Tanasescu 	pdu.cp.mse = qos->bcast.mse;
22070fe8c8d0SIulia Tanasescu 	pdu.cp.timeout = cpu_to_le16(qos->bcast.timeout);
2208eca0ae4aSLuiz Augusto von Dentz 	pdu.cp.num_bis = num_bis;
2209eca0ae4aSLuiz Augusto von Dentz 	memcpy(pdu.bis, bis, num_bis);
2210eca0ae4aSLuiz Augusto von Dentz 
2211eca0ae4aSLuiz Augusto von Dentz 	return hci_send_cmd(hdev, HCI_OP_LE_BIG_CREATE_SYNC,
2212eca0ae4aSLuiz Augusto von Dentz 			    sizeof(pdu.cp) + num_bis, &pdu);
2213eca0ae4aSLuiz Augusto von Dentz }
2214eca0ae4aSLuiz Augusto von Dentz 
2215eca0ae4aSLuiz Augusto von Dentz static void create_big_complete(struct hci_dev *hdev, void *data, int err)
2216eca0ae4aSLuiz Augusto von Dentz {
2217eca0ae4aSLuiz Augusto von Dentz 	struct hci_conn *conn = data;
2218eca0ae4aSLuiz Augusto von Dentz 
2219eca0ae4aSLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "conn %p", conn);
2220eca0ae4aSLuiz Augusto von Dentz 
2221eca0ae4aSLuiz Augusto von Dentz 	if (err) {
2222eca0ae4aSLuiz Augusto von Dentz 		bt_dev_err(hdev, "Unable to create BIG: %d", err);
2223eca0ae4aSLuiz Augusto von Dentz 		hci_connect_cfm(conn, err);
2224eca0ae4aSLuiz Augusto von Dentz 		hci_conn_del(conn);
2225eca0ae4aSLuiz Augusto von Dentz 	}
2226eca0ae4aSLuiz Augusto von Dentz }
2227eca0ae4aSLuiz Augusto von Dentz 
2228a0bfde16SIulia Tanasescu struct hci_conn *hci_bind_bis(struct hci_dev *hdev, bdaddr_t *dst,
2229a0bfde16SIulia Tanasescu 			      struct bt_iso_qos *qos,
2230a0bfde16SIulia Tanasescu 			      __u8 base_len, __u8 *base)
2231a0bfde16SIulia Tanasescu {
2232a0bfde16SIulia Tanasescu 	struct hci_conn *conn;
2233a0bfde16SIulia Tanasescu 	__u8 eir[HCI_MAX_PER_AD_LENGTH];
2234a0bfde16SIulia Tanasescu 
2235a0bfde16SIulia Tanasescu 	if (base_len && base)
2236a0bfde16SIulia Tanasescu 		base_len = eir_append_service_data(eir, 0,  0x1851,
2237a0bfde16SIulia Tanasescu 						   base, base_len);
2238a0bfde16SIulia Tanasescu 
2239a0bfde16SIulia Tanasescu 	/* We need hci_conn object using the BDADDR_ANY as dst */
2240a0bfde16SIulia Tanasescu 	conn = hci_add_bis(hdev, dst, qos, base_len, eir);
2241a0bfde16SIulia Tanasescu 	if (IS_ERR(conn))
2242a0bfde16SIulia Tanasescu 		return conn;
2243a0bfde16SIulia Tanasescu 
2244a0bfde16SIulia Tanasescu 	/* Update LINK PHYs according to QoS preference */
2245a0bfde16SIulia Tanasescu 	conn->le_tx_phy = qos->bcast.out.phy;
2246a0bfde16SIulia Tanasescu 	conn->le_tx_phy = qos->bcast.out.phy;
2247a0bfde16SIulia Tanasescu 
2248a0bfde16SIulia Tanasescu 	/* Add Basic Announcement into Peridic Adv Data if BASE is set */
2249a0bfde16SIulia Tanasescu 	if (base_len && base) {
2250a0bfde16SIulia Tanasescu 		memcpy(conn->le_per_adv_data,  eir, sizeof(eir));
2251a0bfde16SIulia Tanasescu 		conn->le_per_adv_data_len = base_len;
2252a0bfde16SIulia Tanasescu 	}
2253a0bfde16SIulia Tanasescu 
2254a0bfde16SIulia Tanasescu 	hci_iso_qos_setup(hdev, conn, &qos->bcast.out,
2255a0bfde16SIulia Tanasescu 			  conn->le_tx_phy ? conn->le_tx_phy :
2256a0bfde16SIulia Tanasescu 			  hdev->le_tx_def_phys);
2257a0bfde16SIulia Tanasescu 
2258a0bfde16SIulia Tanasescu 	conn->iso_qos = *qos;
2259a0bfde16SIulia Tanasescu 	conn->state = BT_BOUND;
2260a0bfde16SIulia Tanasescu 
2261a0bfde16SIulia Tanasescu 	return conn;
2262a0bfde16SIulia Tanasescu }
2263a0bfde16SIulia Tanasescu 
2264a0bfde16SIulia Tanasescu static void bis_mark_per_adv(struct hci_conn *conn, void *data)
2265a0bfde16SIulia Tanasescu {
2266a0bfde16SIulia Tanasescu 	struct iso_list_data *d = data;
2267a0bfde16SIulia Tanasescu 
2268a0bfde16SIulia Tanasescu 	/* Skip if not broadcast/ANY address */
2269a0bfde16SIulia Tanasescu 	if (bacmp(&conn->dst, BDADDR_ANY))
2270a0bfde16SIulia Tanasescu 		return;
2271a0bfde16SIulia Tanasescu 
2272a0bfde16SIulia Tanasescu 	if (d->big != conn->iso_qos.bcast.big ||
2273a0bfde16SIulia Tanasescu 	    d->bis == BT_ISO_QOS_BIS_UNSET ||
2274a0bfde16SIulia Tanasescu 	    d->bis != conn->iso_qos.bcast.bis)
2275a0bfde16SIulia Tanasescu 		return;
2276a0bfde16SIulia Tanasescu 
2277a0bfde16SIulia Tanasescu 	set_bit(HCI_CONN_PER_ADV, &conn->flags);
2278a0bfde16SIulia Tanasescu }
2279a0bfde16SIulia Tanasescu 
2280eca0ae4aSLuiz Augusto von Dentz struct hci_conn *hci_connect_bis(struct hci_dev *hdev, bdaddr_t *dst,
2281eca0ae4aSLuiz Augusto von Dentz 				 __u8 dst_type, struct bt_iso_qos *qos,
2282eca0ae4aSLuiz Augusto von Dentz 				 __u8 base_len, __u8 *base)
2283eca0ae4aSLuiz Augusto von Dentz {
2284eca0ae4aSLuiz Augusto von Dentz 	struct hci_conn *conn;
2285eca0ae4aSLuiz Augusto von Dentz 	int err;
2286a0bfde16SIulia Tanasescu 	struct iso_list_data data;
2287eca0ae4aSLuiz Augusto von Dentz 
2288a0bfde16SIulia Tanasescu 	conn = hci_bind_bis(hdev, dst, qos, base_len, base);
2289eca0ae4aSLuiz Augusto von Dentz 	if (IS_ERR(conn))
2290eca0ae4aSLuiz Augusto von Dentz 		return conn;
2291eca0ae4aSLuiz Augusto von Dentz 
2292a0bfde16SIulia Tanasescu 	data.big = qos->bcast.big;
2293a0bfde16SIulia Tanasescu 	data.bis = qos->bcast.bis;
2294eca0ae4aSLuiz Augusto von Dentz 
2295a0bfde16SIulia Tanasescu 	/* Set HCI_CONN_PER_ADV for all bound connections, to mark that
2296a0bfde16SIulia Tanasescu 	 * the start periodic advertising and create BIG commands have
2297a0bfde16SIulia Tanasescu 	 * been queued
2298a0bfde16SIulia Tanasescu 	 */
2299a0bfde16SIulia Tanasescu 	hci_conn_hash_list_state(hdev, bis_mark_per_adv, ISO_LINK,
2300a0bfde16SIulia Tanasescu 				 BT_BOUND, &data);
2301eca0ae4aSLuiz Augusto von Dentz 
2302eca0ae4aSLuiz Augusto von Dentz 	/* Queue start periodic advertising and create BIG */
2303eca0ae4aSLuiz Augusto von Dentz 	err = hci_cmd_sync_queue(hdev, create_big_sync, conn,
2304eca0ae4aSLuiz Augusto von Dentz 				 create_big_complete);
2305eca0ae4aSLuiz Augusto von Dentz 	if (err < 0) {
2306eca0ae4aSLuiz Augusto von Dentz 		hci_conn_drop(conn);
2307eca0ae4aSLuiz Augusto von Dentz 		return ERR_PTR(err);
2308eca0ae4aSLuiz Augusto von Dentz 	}
2309eca0ae4aSLuiz Augusto von Dentz 
2310eca0ae4aSLuiz Augusto von Dentz 	return conn;
2311eca0ae4aSLuiz Augusto von Dentz }
2312eca0ae4aSLuiz Augusto von Dentz 
231326afbd82SLuiz Augusto von Dentz struct hci_conn *hci_connect_cis(struct hci_dev *hdev, bdaddr_t *dst,
231426afbd82SLuiz Augusto von Dentz 				 __u8 dst_type, struct bt_iso_qos *qos)
231526afbd82SLuiz Augusto von Dentz {
231626afbd82SLuiz Augusto von Dentz 	struct hci_conn *le;
231726afbd82SLuiz Augusto von Dentz 	struct hci_conn *cis;
231806149746SLuiz Augusto von Dentz 	struct hci_link *link;
231926afbd82SLuiz Augusto von Dentz 
232026afbd82SLuiz Augusto von Dentz 	if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
232126afbd82SLuiz Augusto von Dentz 		le = hci_connect_le(hdev, dst, dst_type, false,
232226afbd82SLuiz Augusto von Dentz 				    BT_SECURITY_LOW,
232326afbd82SLuiz Augusto von Dentz 				    HCI_LE_CONN_TIMEOUT,
232426afbd82SLuiz Augusto von Dentz 				    HCI_ROLE_SLAVE);
232526afbd82SLuiz Augusto von Dentz 	else
232626afbd82SLuiz Augusto von Dentz 		le = hci_connect_le_scan(hdev, dst, dst_type,
232726afbd82SLuiz Augusto von Dentz 					 BT_SECURITY_LOW,
232826afbd82SLuiz Augusto von Dentz 					 HCI_LE_CONN_TIMEOUT,
232926afbd82SLuiz Augusto von Dentz 					 CONN_REASON_ISO_CONNECT);
233026afbd82SLuiz Augusto von Dentz 	if (IS_ERR(le))
233126afbd82SLuiz Augusto von Dentz 		return le;
233226afbd82SLuiz Augusto von Dentz 
23330fe8c8d0SIulia Tanasescu 	hci_iso_qos_setup(hdev, le, &qos->ucast.out,
233426afbd82SLuiz Augusto von Dentz 			  le->le_tx_phy ? le->le_tx_phy : hdev->le_tx_def_phys);
23350fe8c8d0SIulia Tanasescu 	hci_iso_qos_setup(hdev, le, &qos->ucast.in,
233626afbd82SLuiz Augusto von Dentz 			  le->le_rx_phy ? le->le_rx_phy : hdev->le_rx_def_phys);
233726afbd82SLuiz Augusto von Dentz 
233826afbd82SLuiz Augusto von Dentz 	cis = hci_bind_cis(hdev, dst, dst_type, qos);
233926afbd82SLuiz Augusto von Dentz 	if (IS_ERR(cis)) {
234026afbd82SLuiz Augusto von Dentz 		hci_conn_drop(le);
234126afbd82SLuiz Augusto von Dentz 		return cis;
234226afbd82SLuiz Augusto von Dentz 	}
234326afbd82SLuiz Augusto von Dentz 
234406149746SLuiz Augusto von Dentz 	link = hci_conn_link(le, cis);
234506149746SLuiz Augusto von Dentz 	if (!link) {
234606149746SLuiz Augusto von Dentz 		hci_conn_drop(le);
234706149746SLuiz Augusto von Dentz 		hci_conn_drop(cis);
2348b4066eb0SSiddh Raman Pant 		return ERR_PTR(-ENOLINK);
234906149746SLuiz Augusto von Dentz 	}
235026afbd82SLuiz Augusto von Dentz 
235169997d50SPauli Virtanen 	/* Link takes the refcount */
235269997d50SPauli Virtanen 	hci_conn_drop(cis);
235369997d50SPauli Virtanen 
23547f74563eSPauli Virtanen 	cis->state = BT_CONNECT;
23557f74563eSPauli Virtanen 
23567f74563eSPauli Virtanen 	hci_le_create_cis_pending(hdev);
235726afbd82SLuiz Augusto von Dentz 
235826afbd82SLuiz Augusto von Dentz 	return cis;
235926afbd82SLuiz Augusto von Dentz }
236026afbd82SLuiz Augusto von Dentz 
2361e7c29cb1SMarcel Holtmann /* Check link security requirement */
2362e7c29cb1SMarcel Holtmann int hci_conn_check_link_mode(struct hci_conn *conn)
2363e7c29cb1SMarcel Holtmann {
236438b3fef1SAndrei Emeltchenko 	BT_DBG("hcon %p", conn);
2365e7c29cb1SMarcel Holtmann 
236640b552aaSMarcel Holtmann 	/* In Secure Connections Only mode, it is required that Secure
236740b552aaSMarcel Holtmann 	 * Connections is used and the link is encrypted with AES-CCM
236840b552aaSMarcel Holtmann 	 * using a P-256 authenticated combination key.
236940b552aaSMarcel Holtmann 	 */
2370d7a5a11dSMarcel Holtmann 	if (hci_dev_test_flag(conn->hdev, HCI_SC_ONLY)) {
237140b552aaSMarcel Holtmann 		if (!hci_conn_sc_enabled(conn) ||
237240b552aaSMarcel Holtmann 		    !test_bit(HCI_CONN_AES_CCM, &conn->flags) ||
237340b552aaSMarcel Holtmann 		    conn->key_type != HCI_LK_AUTH_COMBINATION_P256)
237440b552aaSMarcel Holtmann 			return 0;
237540b552aaSMarcel Holtmann 	}
237640b552aaSMarcel Holtmann 
23778746f135SLuiz Augusto von Dentz 	 /* AES encryption is required for Level 4:
23788746f135SLuiz Augusto von Dentz 	  *
23798746f135SLuiz Augusto von Dentz 	  * BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 3, Part C
23808746f135SLuiz Augusto von Dentz 	  * page 1319:
23818746f135SLuiz Augusto von Dentz 	  *
23828746f135SLuiz Augusto von Dentz 	  * 128-bit equivalent strength for link and encryption keys
23838746f135SLuiz Augusto von Dentz 	  * required using FIPS approved algorithms (E0 not allowed,
23848746f135SLuiz Augusto von Dentz 	  * SAFER+ not allowed, and P-192 not allowed; encryption key
23858746f135SLuiz Augusto von Dentz 	  * not shortened)
23868746f135SLuiz Augusto von Dentz 	  */
23878746f135SLuiz Augusto von Dentz 	if (conn->sec_level == BT_SECURITY_FIPS &&
23888746f135SLuiz Augusto von Dentz 	    !test_bit(HCI_CONN_AES_CCM, &conn->flags)) {
23898746f135SLuiz Augusto von Dentz 		bt_dev_err(conn->hdev,
23908746f135SLuiz Augusto von Dentz 			   "Invalid security: Missing AES-CCM usage");
23918746f135SLuiz Augusto von Dentz 		return 0;
23928746f135SLuiz Augusto von Dentz 	}
23938746f135SLuiz Augusto von Dentz 
23944dae2798SJohan Hedberg 	if (hci_conn_ssp_enabled(conn) &&
23954dae2798SJohan Hedberg 	    !test_bit(HCI_CONN_ENCRYPT, &conn->flags))
2396e7c29cb1SMarcel Holtmann 		return 0;
2397e7c29cb1SMarcel Holtmann 
2398e7c29cb1SMarcel Holtmann 	return 1;
2399e7c29cb1SMarcel Holtmann }
2400e7c29cb1SMarcel Holtmann 
24011da177e4SLinus Torvalds /* Authenticate remote device */
24020684e5f9SMarcel Holtmann static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
24031da177e4SLinus Torvalds {
240438b3fef1SAndrei Emeltchenko 	BT_DBG("hcon %p", conn);
24051da177e4SLinus Torvalds 
2406765c2a96SJohan Hedberg 	if (conn->pending_sec_level > sec_level)
2407765c2a96SJohan Hedberg 		sec_level = conn->pending_sec_level;
2408765c2a96SJohan Hedberg 
240996a31833SMarcel Holtmann 	if (sec_level > conn->sec_level)
2410765c2a96SJohan Hedberg 		conn->pending_sec_level = sec_level;
24114dae2798SJohan Hedberg 	else if (test_bit(HCI_CONN_AUTH, &conn->flags))
24121da177e4SLinus Torvalds 		return 1;
24131da177e4SLinus Torvalds 
241465cf686eSJohan Hedberg 	/* Make sure we preserve an existing MITM requirement*/
241565cf686eSJohan Hedberg 	auth_type |= (conn->auth_type & 0x01);
241665cf686eSJohan Hedberg 
241796a31833SMarcel Holtmann 	conn->auth_type = auth_type;
241896a31833SMarcel Holtmann 
241951a8efd7SJohan Hedberg 	if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
24201da177e4SLinus Torvalds 		struct hci_cp_auth_requested cp;
2421b7d05badSPeter Hurley 
2422aca3192cSYOSHIFUJI Hideaki 		cp.handle = cpu_to_le16(conn->handle);
242340be492fSMarcel Holtmann 		hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
242440be492fSMarcel Holtmann 			     sizeof(cp), &cp);
242509da1f34SJohan Hedberg 
2426f8a5c402SLuiz Augusto von Dentz 		/* Set the ENCRYPT_PEND to trigger encryption after
2427f8a5c402SLuiz Augusto von Dentz 		 * authentication.
242809da1f34SJohan Hedberg 		 */
2429f8a5c402SLuiz Augusto von Dentz 		if (!test_bit(HCI_CONN_ENCRYPT, &conn->flags))
243009da1f34SJohan Hedberg 			set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
24311da177e4SLinus Torvalds 	}
24328c1b2355SMarcel Holtmann 
24331da177e4SLinus Torvalds 	return 0;
24341da177e4SLinus Torvalds }
24351da177e4SLinus Torvalds 
2436bb6d6895SRandy Dunlap /* Encrypt the link */
243713d39315SWaldemar Rymarkiewicz static void hci_conn_encrypt(struct hci_conn *conn)
243813d39315SWaldemar Rymarkiewicz {
243938b3fef1SAndrei Emeltchenko 	BT_DBG("hcon %p", conn);
244013d39315SWaldemar Rymarkiewicz 
244151a8efd7SJohan Hedberg 	if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
244213d39315SWaldemar Rymarkiewicz 		struct hci_cp_set_conn_encrypt cp;
244313d39315SWaldemar Rymarkiewicz 		cp.handle  = cpu_to_le16(conn->handle);
244413d39315SWaldemar Rymarkiewicz 		cp.encrypt = 0x01;
244513d39315SWaldemar Rymarkiewicz 		hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
244613d39315SWaldemar Rymarkiewicz 			     &cp);
244713d39315SWaldemar Rymarkiewicz 	}
244813d39315SWaldemar Rymarkiewicz }
244913d39315SWaldemar Rymarkiewicz 
24508c1b2355SMarcel Holtmann /* Enable security */
2451e7cafc45SJohan Hedberg int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type,
2452e7cafc45SJohan Hedberg 		      bool initiator)
24531da177e4SLinus Torvalds {
245438b3fef1SAndrei Emeltchenko 	BT_DBG("hcon %p", conn);
24551da177e4SLinus Torvalds 
2456d8343f12SVinicius Costa Gomes 	if (conn->type == LE_LINK)
2457d8343f12SVinicius Costa Gomes 		return smp_conn_security(conn, sec_level);
2458d8343f12SVinicius Costa Gomes 
245913d39315SWaldemar Rymarkiewicz 	/* For sdp we don't need the link key. */
24608c1b2355SMarcel Holtmann 	if (sec_level == BT_SECURITY_SDP)
24618c1b2355SMarcel Holtmann 		return 1;
24628c1b2355SMarcel Holtmann 
246313d39315SWaldemar Rymarkiewicz 	/* For non 2.1 devices and low security level we don't need the link
246413d39315SWaldemar Rymarkiewicz 	   key. */
2465aa64a8b5SJohan Hedberg 	if (sec_level == BT_SECURITY_LOW && !hci_conn_ssp_enabled(conn))
24668c1b2355SMarcel Holtmann 		return 1;
24678c1b2355SMarcel Holtmann 
246813d39315SWaldemar Rymarkiewicz 	/* For other security levels we need the link key. */
24694dae2798SJohan Hedberg 	if (!test_bit(HCI_CONN_AUTH, &conn->flags))
247013d39315SWaldemar Rymarkiewicz 		goto auth;
24711da177e4SLinus Torvalds 
24721d8e8014SYing Hsu 	switch (conn->key_type) {
24731d8e8014SYing Hsu 	case HCI_LK_AUTH_COMBINATION_P256:
24741d8e8014SYing Hsu 		/* An authenticated FIPS approved combination key has
24751d8e8014SYing Hsu 		 * sufficient security for security level 4 or lower.
24761d8e8014SYing Hsu 		 */
24771d8e8014SYing Hsu 		if (sec_level <= BT_SECURITY_FIPS)
24787b5a9241SMarcel Holtmann 			goto encrypt;
24791d8e8014SYing Hsu 		break;
24801d8e8014SYing Hsu 	case HCI_LK_AUTH_COMBINATION_P192:
24817b5a9241SMarcel Holtmann 		/* An authenticated combination key has sufficient security for
24821d8e8014SYing Hsu 		 * security level 3 or lower.
24831d8e8014SYing Hsu 		 */
24841d8e8014SYing Hsu 		if (sec_level <= BT_SECURITY_HIGH)
248513d39315SWaldemar Rymarkiewicz 			goto encrypt;
24861d8e8014SYing Hsu 		break;
24871d8e8014SYing Hsu 	case HCI_LK_UNAUTH_COMBINATION_P192:
24881d8e8014SYing Hsu 	case HCI_LK_UNAUTH_COMBINATION_P256:
24891d8e8014SYing Hsu 		/* An unauthenticated combination key has sufficient security
24901d8e8014SYing Hsu 		 * for security level 2 or lower.
24911d8e8014SYing Hsu 		 */
24921d8e8014SYing Hsu 		if (sec_level <= BT_SECURITY_MEDIUM)
249313d39315SWaldemar Rymarkiewicz 			goto encrypt;
24941d8e8014SYing Hsu 		break;
24951d8e8014SYing Hsu 	case HCI_LK_COMBINATION:
24961d8e8014SYing Hsu 		/* A combination key has always sufficient security for the
24971d8e8014SYing Hsu 		 * security levels 2 or lower. High security level requires the
24981d8e8014SYing Hsu 		 * combination key is generated using maximum PIN code length
24991d8e8014SYing Hsu 		 * (16). For pre 2.1 units.
25001d8e8014SYing Hsu 		 */
25011d8e8014SYing Hsu 		if (sec_level <= BT_SECURITY_MEDIUM || conn->pin_length == 16)
250213d39315SWaldemar Rymarkiewicz 			goto encrypt;
25031d8e8014SYing Hsu 		break;
25041d8e8014SYing Hsu 	default:
25051d8e8014SYing Hsu 		break;
25061d8e8014SYing Hsu 	}
250713d39315SWaldemar Rymarkiewicz 
250813d39315SWaldemar Rymarkiewicz auth:
250951a8efd7SJohan Hedberg 	if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
25101da177e4SLinus Torvalds 		return 0;
25111da177e4SLinus Torvalds 
2512977f8fceSJohan Hedberg 	if (initiator)
2513977f8fceSJohan Hedberg 		set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
2514977f8fceSJohan Hedberg 
25156fdf658cSLuiz Augusto von Dentz 	if (!hci_conn_auth(conn, sec_level, auth_type))
251613d39315SWaldemar Rymarkiewicz 		return 0;
25178c1b2355SMarcel Holtmann 
251813d39315SWaldemar Rymarkiewicz encrypt:
2519693cd8ceSMarcel Holtmann 	if (test_bit(HCI_CONN_ENCRYPT, &conn->flags)) {
2520693cd8ceSMarcel Holtmann 		/* Ensure that the encryption key size has been read,
2521693cd8ceSMarcel Holtmann 		 * otherwise stall the upper layer responses.
2522693cd8ceSMarcel Holtmann 		 */
2523693cd8ceSMarcel Holtmann 		if (!conn->enc_key_size)
2524693cd8ceSMarcel Holtmann 			return 0;
2525693cd8ceSMarcel Holtmann 
2526693cd8ceSMarcel Holtmann 		/* Nothing else needed, all requirements are met */
252713d39315SWaldemar Rymarkiewicz 		return 1;
2528693cd8ceSMarcel Holtmann 	}
252913d39315SWaldemar Rymarkiewicz 
253013d39315SWaldemar Rymarkiewicz 	hci_conn_encrypt(conn);
25311da177e4SLinus Torvalds 	return 0;
25321da177e4SLinus Torvalds }
25338c1b2355SMarcel Holtmann EXPORT_SYMBOL(hci_conn_security);
25341da177e4SLinus Torvalds 
2535b3b1b061SWaldemar Rymarkiewicz /* Check secure link requirement */
2536b3b1b061SWaldemar Rymarkiewicz int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level)
2537b3b1b061SWaldemar Rymarkiewicz {
253838b3fef1SAndrei Emeltchenko 	BT_DBG("hcon %p", conn);
2539b3b1b061SWaldemar Rymarkiewicz 
25409cb2e030SMarcel Holtmann 	/* Accept if non-secure or higher security level is required */
25419cb2e030SMarcel Holtmann 	if (sec_level != BT_SECURITY_HIGH && sec_level != BT_SECURITY_FIPS)
2542b3b1b061SWaldemar Rymarkiewicz 		return 1;
2543b3b1b061SWaldemar Rymarkiewicz 
25449cb2e030SMarcel Holtmann 	/* Accept if secure or higher security level is already present */
25459cb2e030SMarcel Holtmann 	if (conn->sec_level == BT_SECURITY_HIGH ||
25469cb2e030SMarcel Holtmann 	    conn->sec_level == BT_SECURITY_FIPS)
25479cb2e030SMarcel Holtmann 		return 1;
25489cb2e030SMarcel Holtmann 
25499cb2e030SMarcel Holtmann 	/* Reject not secure link */
25509cb2e030SMarcel Holtmann 	return 0;
2551b3b1b061SWaldemar Rymarkiewicz }
2552b3b1b061SWaldemar Rymarkiewicz EXPORT_SYMBOL(hci_conn_check_secure);
2553b3b1b061SWaldemar Rymarkiewicz 
25541da177e4SLinus Torvalds /* Switch role */
25558c1b2355SMarcel Holtmann int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
25561da177e4SLinus Torvalds {
255738b3fef1SAndrei Emeltchenko 	BT_DBG("hcon %p", conn);
25581da177e4SLinus Torvalds 
255940bef302SJohan Hedberg 	if (role == conn->role)
25601da177e4SLinus Torvalds 		return 1;
25611da177e4SLinus Torvalds 
256251a8efd7SJohan Hedberg 	if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->flags)) {
25631da177e4SLinus Torvalds 		struct hci_cp_switch_role cp;
25641da177e4SLinus Torvalds 		bacpy(&cp.bdaddr, &conn->dst);
25651da177e4SLinus Torvalds 		cp.role = role;
2566a9de9248SMarcel Holtmann 		hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
25671da177e4SLinus Torvalds 	}
25688c1b2355SMarcel Holtmann 
25691da177e4SLinus Torvalds 	return 0;
25701da177e4SLinus Torvalds }
25711da177e4SLinus Torvalds EXPORT_SYMBOL(hci_conn_switch_role);
25721da177e4SLinus Torvalds 
257304837f64SMarcel Holtmann /* Enter active mode */
257414b12d0bSJaikumar Ganesh void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
257504837f64SMarcel Holtmann {
257604837f64SMarcel Holtmann 	struct hci_dev *hdev = conn->hdev;
257704837f64SMarcel Holtmann 
257838b3fef1SAndrei Emeltchenko 	BT_DBG("hcon %p mode %d", conn, conn->mode);
257904837f64SMarcel Holtmann 
258014b12d0bSJaikumar Ganesh 	if (conn->mode != HCI_CM_SNIFF)
258114b12d0bSJaikumar Ganesh 		goto timer;
258214b12d0bSJaikumar Ganesh 
258358a681efSJohan Hedberg 	if (!test_bit(HCI_CONN_POWER_SAVE, &conn->flags) && !force_active)
258404837f64SMarcel Holtmann 		goto timer;
258504837f64SMarcel Holtmann 
258651a8efd7SJohan Hedberg 	if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) {
258704837f64SMarcel Holtmann 		struct hci_cp_exit_sniff_mode cp;
2588aca3192cSYOSHIFUJI Hideaki 		cp.handle = cpu_to_le16(conn->handle);
2589a9de9248SMarcel Holtmann 		hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
259004837f64SMarcel Holtmann 	}
259104837f64SMarcel Holtmann 
259204837f64SMarcel Holtmann timer:
259304837f64SMarcel Holtmann 	if (hdev->idle_timeout > 0)
2594a74a84f6SJohan Hedberg 		queue_delayed_work(hdev->workqueue, &conn->idle_work,
2595a74a84f6SJohan Hedberg 				   msecs_to_jiffies(hdev->idle_timeout));
259604837f64SMarcel Holtmann }
259704837f64SMarcel Holtmann 
25981da177e4SLinus Torvalds /* Drop all connection on the device */
25991da177e4SLinus Torvalds void hci_conn_hash_flush(struct hci_dev *hdev)
26001da177e4SLinus Torvalds {
2601a2ac591cSRuihan Li 	struct list_head *head = &hdev->conn_hash.list;
2602a2ac591cSRuihan Li 	struct hci_conn *conn;
26031da177e4SLinus Torvalds 
26041da177e4SLinus Torvalds 	BT_DBG("hdev %s", hdev->name);
26051da177e4SLinus Torvalds 
2606a2ac591cSRuihan Li 	/* We should not traverse the list here, because hci_conn_del
2607a2ac591cSRuihan Li 	 * can remove extra links, which may cause the list traversal
2608a2ac591cSRuihan Li 	 * to hit items that have already been released.
2609a2ac591cSRuihan Li 	 */
2610a2ac591cSRuihan Li 	while ((conn = list_first_entry_or_null(head,
2611a2ac591cSRuihan Li 						struct hci_conn,
2612a2ac591cSRuihan Li 						list)) != NULL) {
2613a2ac591cSRuihan Li 		conn->state = BT_CLOSED;
2614a2ac591cSRuihan Li 		hci_disconn_cfm(conn, HCI_ERROR_LOCAL_HOST_TERM);
2615a2ac591cSRuihan Li 		hci_conn_del(conn);
26161da177e4SLinus Torvalds 	}
26171da177e4SLinus Torvalds }
26181da177e4SLinus Torvalds 
2619a9de9248SMarcel Holtmann /* Check pending connect attempts */
2620a9de9248SMarcel Holtmann void hci_conn_check_pending(struct hci_dev *hdev)
2621a9de9248SMarcel Holtmann {
2622a9de9248SMarcel Holtmann 	struct hci_conn *conn;
2623a9de9248SMarcel Holtmann 
2624a9de9248SMarcel Holtmann 	BT_DBG("hdev %s", hdev->name);
2625a9de9248SMarcel Holtmann 
2626a9de9248SMarcel Holtmann 	hci_dev_lock(hdev);
2627a9de9248SMarcel Holtmann 
2628a9de9248SMarcel Holtmann 	conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
2629a9de9248SMarcel Holtmann 	if (conn)
26301aef8669SVinicius Costa Gomes 		hci_acl_create_connection(conn);
2631a9de9248SMarcel Holtmann 
2632a9de9248SMarcel Holtmann 	hci_dev_unlock(hdev);
2633a9de9248SMarcel Holtmann }
2634a9de9248SMarcel Holtmann 
26354dae2798SJohan Hedberg static u32 get_link_mode(struct hci_conn *conn)
26364dae2798SJohan Hedberg {
26374dae2798SJohan Hedberg 	u32 link_mode = 0;
26384dae2798SJohan Hedberg 
263940bef302SJohan Hedberg 	if (conn->role == HCI_ROLE_MASTER)
26404dae2798SJohan Hedberg 		link_mode |= HCI_LM_MASTER;
26414dae2798SJohan Hedberg 
26424dae2798SJohan Hedberg 	if (test_bit(HCI_CONN_ENCRYPT, &conn->flags))
26434dae2798SJohan Hedberg 		link_mode |= HCI_LM_ENCRYPT;
26444dae2798SJohan Hedberg 
26454dae2798SJohan Hedberg 	if (test_bit(HCI_CONN_AUTH, &conn->flags))
26464dae2798SJohan Hedberg 		link_mode |= HCI_LM_AUTH;
26474dae2798SJohan Hedberg 
26484dae2798SJohan Hedberg 	if (test_bit(HCI_CONN_SECURE, &conn->flags))
26494dae2798SJohan Hedberg 		link_mode |= HCI_LM_SECURE;
26504dae2798SJohan Hedberg 
26514dae2798SJohan Hedberg 	if (test_bit(HCI_CONN_FIPS, &conn->flags))
26524dae2798SJohan Hedberg 		link_mode |= HCI_LM_FIPS;
26534dae2798SJohan Hedberg 
26544dae2798SJohan Hedberg 	return link_mode;
26554dae2798SJohan Hedberg }
26564dae2798SJohan Hedberg 
26571da177e4SLinus Torvalds int hci_get_conn_list(void __user *arg)
26581da177e4SLinus Torvalds {
2659fc5fef61SGustavo Padovan 	struct hci_conn *c;
26601da177e4SLinus Torvalds 	struct hci_conn_list_req req, *cl;
26611da177e4SLinus Torvalds 	struct hci_conn_info *ci;
26621da177e4SLinus Torvalds 	struct hci_dev *hdev;
26631da177e4SLinus Torvalds 	int n = 0, size, err;
26641da177e4SLinus Torvalds 
26651da177e4SLinus Torvalds 	if (copy_from_user(&req, arg, sizeof(req)))
26661da177e4SLinus Torvalds 		return -EFAULT;
26671da177e4SLinus Torvalds 
26681da177e4SLinus Torvalds 	if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
26691da177e4SLinus Torvalds 		return -EINVAL;
26701da177e4SLinus Torvalds 
26711da177e4SLinus Torvalds 	size = sizeof(req) + req.conn_num * sizeof(*ci);
26721da177e4SLinus Torvalds 
267370f23020SAndrei Emeltchenko 	cl = kmalloc(size, GFP_KERNEL);
267470f23020SAndrei Emeltchenko 	if (!cl)
26751da177e4SLinus Torvalds 		return -ENOMEM;
26761da177e4SLinus Torvalds 
267770f23020SAndrei Emeltchenko 	hdev = hci_dev_get(req.dev_id);
267870f23020SAndrei Emeltchenko 	if (!hdev) {
26791da177e4SLinus Torvalds 		kfree(cl);
26801da177e4SLinus Torvalds 		return -ENODEV;
26811da177e4SLinus Torvalds 	}
26821da177e4SLinus Torvalds 
26831da177e4SLinus Torvalds 	ci = cl->conn_info;
26841da177e4SLinus Torvalds 
268509fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
26868035ded4SLuiz Augusto von Dentz 	list_for_each_entry(c, &hdev->conn_hash.list, list) {
26871da177e4SLinus Torvalds 		bacpy(&(ci + n)->bdaddr, &c->dst);
26881da177e4SLinus Torvalds 		(ci + n)->handle = c->handle;
26891da177e4SLinus Torvalds 		(ci + n)->type  = c->type;
26901da177e4SLinus Torvalds 		(ci + n)->out   = c->out;
26911da177e4SLinus Torvalds 		(ci + n)->state = c->state;
26924dae2798SJohan Hedberg 		(ci + n)->link_mode = get_link_mode(c);
26931da177e4SLinus Torvalds 		if (++n >= req.conn_num)
26941da177e4SLinus Torvalds 			break;
26951da177e4SLinus Torvalds 	}
269609fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
26971da177e4SLinus Torvalds 
26981da177e4SLinus Torvalds 	cl->dev_id = hdev->id;
26991da177e4SLinus Torvalds 	cl->conn_num = n;
27001da177e4SLinus Torvalds 	size = sizeof(req) + n * sizeof(*ci);
27011da177e4SLinus Torvalds 
27021da177e4SLinus Torvalds 	hci_dev_put(hdev);
27031da177e4SLinus Torvalds 
27041da177e4SLinus Torvalds 	err = copy_to_user(arg, cl, size);
27051da177e4SLinus Torvalds 	kfree(cl);
27061da177e4SLinus Torvalds 
27071da177e4SLinus Torvalds 	return err ? -EFAULT : 0;
27081da177e4SLinus Torvalds }
27091da177e4SLinus Torvalds 
27101da177e4SLinus Torvalds int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
27111da177e4SLinus Torvalds {
27121da177e4SLinus Torvalds 	struct hci_conn_info_req req;
27131da177e4SLinus Torvalds 	struct hci_conn_info ci;
27141da177e4SLinus Torvalds 	struct hci_conn *conn;
27151da177e4SLinus Torvalds 	char __user *ptr = arg + sizeof(req);
27161da177e4SLinus Torvalds 
27171da177e4SLinus Torvalds 	if (copy_from_user(&req, arg, sizeof(req)))
27181da177e4SLinus Torvalds 		return -EFAULT;
27191da177e4SLinus Torvalds 
272009fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
27211da177e4SLinus Torvalds 	conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
27221da177e4SLinus Torvalds 	if (conn) {
27231da177e4SLinus Torvalds 		bacpy(&ci.bdaddr, &conn->dst);
27241da177e4SLinus Torvalds 		ci.handle = conn->handle;
27251da177e4SLinus Torvalds 		ci.type  = conn->type;
27261da177e4SLinus Torvalds 		ci.out   = conn->out;
27271da177e4SLinus Torvalds 		ci.state = conn->state;
27284dae2798SJohan Hedberg 		ci.link_mode = get_link_mode(conn);
27291da177e4SLinus Torvalds 	}
273009fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
27311da177e4SLinus Torvalds 
27321da177e4SLinus Torvalds 	if (!conn)
27331da177e4SLinus Torvalds 		return -ENOENT;
27341da177e4SLinus Torvalds 
27351da177e4SLinus Torvalds 	return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
27361da177e4SLinus Torvalds }
273740be492fSMarcel Holtmann 
273840be492fSMarcel Holtmann int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
273940be492fSMarcel Holtmann {
274040be492fSMarcel Holtmann 	struct hci_auth_info_req req;
274140be492fSMarcel Holtmann 	struct hci_conn *conn;
274240be492fSMarcel Holtmann 
274340be492fSMarcel Holtmann 	if (copy_from_user(&req, arg, sizeof(req)))
274440be492fSMarcel Holtmann 		return -EFAULT;
274540be492fSMarcel Holtmann 
274609fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
274740be492fSMarcel Holtmann 	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
274840be492fSMarcel Holtmann 	if (conn)
274940be492fSMarcel Holtmann 		req.type = conn->auth_type;
275009fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
275140be492fSMarcel Holtmann 
275240be492fSMarcel Holtmann 	if (!conn)
275340be492fSMarcel Holtmann 		return -ENOENT;
275440be492fSMarcel Holtmann 
275540be492fSMarcel Holtmann 	return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
275640be492fSMarcel Holtmann }
275773d80debSLuiz Augusto von Dentz 
275873d80debSLuiz Augusto von Dentz struct hci_chan *hci_chan_create(struct hci_conn *conn)
275973d80debSLuiz Augusto von Dentz {
276073d80debSLuiz Augusto von Dentz 	struct hci_dev *hdev = conn->hdev;
276173d80debSLuiz Augusto von Dentz 	struct hci_chan *chan;
276273d80debSLuiz Augusto von Dentz 
276338b3fef1SAndrei Emeltchenko 	BT_DBG("%s hcon %p", hdev->name, conn);
276473d80debSLuiz Augusto von Dentz 
2765f94b665dSJohan Hedberg 	if (test_bit(HCI_CONN_DROP, &conn->flags)) {
2766f94b665dSJohan Hedberg 		BT_DBG("Refusing to create new hci_chan");
2767f94b665dSJohan Hedberg 		return NULL;
2768f94b665dSJohan Hedberg 	}
2769f94b665dSJohan Hedberg 
277027f70f3eSJohan Hedberg 	chan = kzalloc(sizeof(*chan), GFP_KERNEL);
277173d80debSLuiz Augusto von Dentz 	if (!chan)
277273d80debSLuiz Augusto von Dentz 		return NULL;
277373d80debSLuiz Augusto von Dentz 
27746c388d32SJohan Hedberg 	chan->conn = hci_conn_get(conn);
277573d80debSLuiz Augusto von Dentz 	skb_queue_head_init(&chan->data_q);
2776168df8e5SMat Martineau 	chan->state = BT_CONNECTED;
277773d80debSLuiz Augusto von Dentz 
27788192edefSGustavo F. Padovan 	list_add_rcu(&chan->list, &conn->chan_list);
277973d80debSLuiz Augusto von Dentz 
278073d80debSLuiz Augusto von Dentz 	return chan;
278173d80debSLuiz Augusto von Dentz }
278273d80debSLuiz Augusto von Dentz 
27839472007cSAndrei Emeltchenko void hci_chan_del(struct hci_chan *chan)
278473d80debSLuiz Augusto von Dentz {
278573d80debSLuiz Augusto von Dentz 	struct hci_conn *conn = chan->conn;
278673d80debSLuiz Augusto von Dentz 	struct hci_dev *hdev = conn->hdev;
278773d80debSLuiz Augusto von Dentz 
278838b3fef1SAndrei Emeltchenko 	BT_DBG("%s hcon %p chan %p", hdev->name, conn, chan);
278973d80debSLuiz Augusto von Dentz 
27908192edefSGustavo F. Padovan 	list_del_rcu(&chan->list);
27918192edefSGustavo F. Padovan 
27928192edefSGustavo F. Padovan 	synchronize_rcu();
279373d80debSLuiz Augusto von Dentz 
2794bcbb655aSJohan Hedberg 	/* Prevent new hci_chan's to be created for this hci_conn */
2795f94b665dSJohan Hedberg 	set_bit(HCI_CONN_DROP, &conn->flags);
2796b3ff670aSJohan Hedberg 
27976c388d32SJohan Hedberg 	hci_conn_put(conn);
2798e9b02748SAndrei Emeltchenko 
279973d80debSLuiz Augusto von Dentz 	skb_queue_purge(&chan->data_q);
280073d80debSLuiz Augusto von Dentz 	kfree(chan);
280173d80debSLuiz Augusto von Dentz }
280273d80debSLuiz Augusto von Dentz 
28032c33c06aSGustavo F. Padovan void hci_chan_list_flush(struct hci_conn *conn)
280473d80debSLuiz Augusto von Dentz {
28052a5a5ec6SAndrei Emeltchenko 	struct hci_chan *chan, *n;
280673d80debSLuiz Augusto von Dentz 
280738b3fef1SAndrei Emeltchenko 	BT_DBG("hcon %p", conn);
280873d80debSLuiz Augusto von Dentz 
28092a5a5ec6SAndrei Emeltchenko 	list_for_each_entry_safe(chan, n, &conn->chan_list, list)
281073d80debSLuiz Augusto von Dentz 		hci_chan_del(chan);
281173d80debSLuiz Augusto von Dentz }
281242c4e53eSAndrei Emeltchenko 
281342c4e53eSAndrei Emeltchenko static struct hci_chan *__hci_chan_lookup_handle(struct hci_conn *hcon,
281442c4e53eSAndrei Emeltchenko 						 __u16 handle)
281542c4e53eSAndrei Emeltchenko {
281642c4e53eSAndrei Emeltchenko 	struct hci_chan *hchan;
281742c4e53eSAndrei Emeltchenko 
281842c4e53eSAndrei Emeltchenko 	list_for_each_entry(hchan, &hcon->chan_list, list) {
281942c4e53eSAndrei Emeltchenko 		if (hchan->handle == handle)
282042c4e53eSAndrei Emeltchenko 			return hchan;
282142c4e53eSAndrei Emeltchenko 	}
282242c4e53eSAndrei Emeltchenko 
282342c4e53eSAndrei Emeltchenko 	return NULL;
282442c4e53eSAndrei Emeltchenko }
282542c4e53eSAndrei Emeltchenko 
282642c4e53eSAndrei Emeltchenko struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle)
282742c4e53eSAndrei Emeltchenko {
282842c4e53eSAndrei Emeltchenko 	struct hci_conn_hash *h = &hdev->conn_hash;
282942c4e53eSAndrei Emeltchenko 	struct hci_conn *hcon;
283042c4e53eSAndrei Emeltchenko 	struct hci_chan *hchan = NULL;
283142c4e53eSAndrei Emeltchenko 
283242c4e53eSAndrei Emeltchenko 	rcu_read_lock();
283342c4e53eSAndrei Emeltchenko 
283442c4e53eSAndrei Emeltchenko 	list_for_each_entry_rcu(hcon, &h->list, list) {
283542c4e53eSAndrei Emeltchenko 		hchan = __hci_chan_lookup_handle(hcon, handle);
283642c4e53eSAndrei Emeltchenko 		if (hchan)
283742c4e53eSAndrei Emeltchenko 			break;
283842c4e53eSAndrei Emeltchenko 	}
283942c4e53eSAndrei Emeltchenko 
284042c4e53eSAndrei Emeltchenko 	rcu_read_unlock();
284142c4e53eSAndrei Emeltchenko 
284242c4e53eSAndrei Emeltchenko 	return hchan;
284342c4e53eSAndrei Emeltchenko }
2844eab2404bSLuiz Augusto von Dentz 
2845eab2404bSLuiz Augusto von Dentz u32 hci_conn_get_phy(struct hci_conn *conn)
2846eab2404bSLuiz Augusto von Dentz {
2847eab2404bSLuiz Augusto von Dentz 	u32 phys = 0;
2848eab2404bSLuiz Augusto von Dentz 
2849eab2404bSLuiz Augusto von Dentz 	/* BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 2, Part B page 471:
2850eab2404bSLuiz Augusto von Dentz 	 * Table 6.2: Packets defined for synchronous, asynchronous, and
28516397729bSArchie Pusaka 	 * CPB logical transport types.
2852eab2404bSLuiz Augusto von Dentz 	 */
2853eab2404bSLuiz Augusto von Dentz 	switch (conn->type) {
2854eab2404bSLuiz Augusto von Dentz 	case SCO_LINK:
2855eab2404bSLuiz Augusto von Dentz 		/* SCO logical transport (1 Mb/s):
2856eab2404bSLuiz Augusto von Dentz 		 * HV1, HV2, HV3 and DV.
2857eab2404bSLuiz Augusto von Dentz 		 */
2858eab2404bSLuiz Augusto von Dentz 		phys |= BT_PHY_BR_1M_1SLOT;
2859eab2404bSLuiz Augusto von Dentz 
2860eab2404bSLuiz Augusto von Dentz 		break;
2861eab2404bSLuiz Augusto von Dentz 
2862eab2404bSLuiz Augusto von Dentz 	case ACL_LINK:
2863eab2404bSLuiz Augusto von Dentz 		/* ACL logical transport (1 Mb/s) ptt=0:
2864eab2404bSLuiz Augusto von Dentz 		 * DH1, DM3, DH3, DM5 and DH5.
2865eab2404bSLuiz Augusto von Dentz 		 */
2866eab2404bSLuiz Augusto von Dentz 		phys |= BT_PHY_BR_1M_1SLOT;
2867eab2404bSLuiz Augusto von Dentz 
2868eab2404bSLuiz Augusto von Dentz 		if (conn->pkt_type & (HCI_DM3 | HCI_DH3))
2869eab2404bSLuiz Augusto von Dentz 			phys |= BT_PHY_BR_1M_3SLOT;
2870eab2404bSLuiz Augusto von Dentz 
2871eab2404bSLuiz Augusto von Dentz 		if (conn->pkt_type & (HCI_DM5 | HCI_DH5))
2872eab2404bSLuiz Augusto von Dentz 			phys |= BT_PHY_BR_1M_5SLOT;
2873eab2404bSLuiz Augusto von Dentz 
2874eab2404bSLuiz Augusto von Dentz 		/* ACL logical transport (2 Mb/s) ptt=1:
2875eab2404bSLuiz Augusto von Dentz 		 * 2-DH1, 2-DH3 and 2-DH5.
2876eab2404bSLuiz Augusto von Dentz 		 */
2877eab2404bSLuiz Augusto von Dentz 		if (!(conn->pkt_type & HCI_2DH1))
2878eab2404bSLuiz Augusto von Dentz 			phys |= BT_PHY_EDR_2M_1SLOT;
2879eab2404bSLuiz Augusto von Dentz 
2880eab2404bSLuiz Augusto von Dentz 		if (!(conn->pkt_type & HCI_2DH3))
2881eab2404bSLuiz Augusto von Dentz 			phys |= BT_PHY_EDR_2M_3SLOT;
2882eab2404bSLuiz Augusto von Dentz 
2883eab2404bSLuiz Augusto von Dentz 		if (!(conn->pkt_type & HCI_2DH5))
2884eab2404bSLuiz Augusto von Dentz 			phys |= BT_PHY_EDR_2M_5SLOT;
2885eab2404bSLuiz Augusto von Dentz 
2886eab2404bSLuiz Augusto von Dentz 		/* ACL logical transport (3 Mb/s) ptt=1:
2887eab2404bSLuiz Augusto von Dentz 		 * 3-DH1, 3-DH3 and 3-DH5.
2888eab2404bSLuiz Augusto von Dentz 		 */
2889eab2404bSLuiz Augusto von Dentz 		if (!(conn->pkt_type & HCI_3DH1))
2890eab2404bSLuiz Augusto von Dentz 			phys |= BT_PHY_EDR_3M_1SLOT;
2891eab2404bSLuiz Augusto von Dentz 
2892eab2404bSLuiz Augusto von Dentz 		if (!(conn->pkt_type & HCI_3DH3))
2893eab2404bSLuiz Augusto von Dentz 			phys |= BT_PHY_EDR_3M_3SLOT;
2894eab2404bSLuiz Augusto von Dentz 
2895eab2404bSLuiz Augusto von Dentz 		if (!(conn->pkt_type & HCI_3DH5))
2896eab2404bSLuiz Augusto von Dentz 			phys |= BT_PHY_EDR_3M_5SLOT;
2897eab2404bSLuiz Augusto von Dentz 
2898eab2404bSLuiz Augusto von Dentz 		break;
2899eab2404bSLuiz Augusto von Dentz 
2900eab2404bSLuiz Augusto von Dentz 	case ESCO_LINK:
2901eab2404bSLuiz Augusto von Dentz 		/* eSCO logical transport (1 Mb/s): EV3, EV4 and EV5 */
2902eab2404bSLuiz Augusto von Dentz 		phys |= BT_PHY_BR_1M_1SLOT;
2903eab2404bSLuiz Augusto von Dentz 
2904eab2404bSLuiz Augusto von Dentz 		if (!(conn->pkt_type & (ESCO_EV4 | ESCO_EV5)))
2905eab2404bSLuiz Augusto von Dentz 			phys |= BT_PHY_BR_1M_3SLOT;
2906eab2404bSLuiz Augusto von Dentz 
2907eab2404bSLuiz Augusto von Dentz 		/* eSCO logical transport (2 Mb/s): 2-EV3, 2-EV5 */
2908eab2404bSLuiz Augusto von Dentz 		if (!(conn->pkt_type & ESCO_2EV3))
2909eab2404bSLuiz Augusto von Dentz 			phys |= BT_PHY_EDR_2M_1SLOT;
2910eab2404bSLuiz Augusto von Dentz 
2911eab2404bSLuiz Augusto von Dentz 		if (!(conn->pkt_type & ESCO_2EV5))
2912eab2404bSLuiz Augusto von Dentz 			phys |= BT_PHY_EDR_2M_3SLOT;
2913eab2404bSLuiz Augusto von Dentz 
2914eab2404bSLuiz Augusto von Dentz 		/* eSCO logical transport (3 Mb/s): 3-EV3, 3-EV5 */
2915eab2404bSLuiz Augusto von Dentz 		if (!(conn->pkt_type & ESCO_3EV3))
2916eab2404bSLuiz Augusto von Dentz 			phys |= BT_PHY_EDR_3M_1SLOT;
2917eab2404bSLuiz Augusto von Dentz 
2918eab2404bSLuiz Augusto von Dentz 		if (!(conn->pkt_type & ESCO_3EV5))
2919eab2404bSLuiz Augusto von Dentz 			phys |= BT_PHY_EDR_3M_3SLOT;
2920eab2404bSLuiz Augusto von Dentz 
2921eab2404bSLuiz Augusto von Dentz 		break;
2922eab2404bSLuiz Augusto von Dentz 
2923eab2404bSLuiz Augusto von Dentz 	case LE_LINK:
2924eab2404bSLuiz Augusto von Dentz 		if (conn->le_tx_phy & HCI_LE_SET_PHY_1M)
2925eab2404bSLuiz Augusto von Dentz 			phys |= BT_PHY_LE_1M_TX;
2926eab2404bSLuiz Augusto von Dentz 
2927eab2404bSLuiz Augusto von Dentz 		if (conn->le_rx_phy & HCI_LE_SET_PHY_1M)
2928eab2404bSLuiz Augusto von Dentz 			phys |= BT_PHY_LE_1M_RX;
2929eab2404bSLuiz Augusto von Dentz 
2930eab2404bSLuiz Augusto von Dentz 		if (conn->le_tx_phy & HCI_LE_SET_PHY_2M)
2931eab2404bSLuiz Augusto von Dentz 			phys |= BT_PHY_LE_2M_TX;
2932eab2404bSLuiz Augusto von Dentz 
2933eab2404bSLuiz Augusto von Dentz 		if (conn->le_rx_phy & HCI_LE_SET_PHY_2M)
2934eab2404bSLuiz Augusto von Dentz 			phys |= BT_PHY_LE_2M_RX;
2935eab2404bSLuiz Augusto von Dentz 
2936eab2404bSLuiz Augusto von Dentz 		if (conn->le_tx_phy & HCI_LE_SET_PHY_CODED)
2937eab2404bSLuiz Augusto von Dentz 			phys |= BT_PHY_LE_CODED_TX;
2938eab2404bSLuiz Augusto von Dentz 
2939eab2404bSLuiz Augusto von Dentz 		if (conn->le_rx_phy & HCI_LE_SET_PHY_CODED)
2940eab2404bSLuiz Augusto von Dentz 			phys |= BT_PHY_LE_CODED_RX;
2941eab2404bSLuiz Augusto von Dentz 
2942eab2404bSLuiz Augusto von Dentz 		break;
2943eab2404bSLuiz Augusto von Dentz 	}
2944eab2404bSLuiz Augusto von Dentz 
2945eab2404bSLuiz Augusto von Dentz 	return phys;
2946eab2404bSLuiz Augusto von Dentz }
29471a942de0SBrian Gix 
2948a13f316eSLuiz Augusto von Dentz static int abort_conn_sync(struct hci_dev *hdev, void *data)
29491a942de0SBrian Gix {
2950a13f316eSLuiz Augusto von Dentz 	struct hci_conn *conn;
2951a1f6c3aeSLuiz Augusto von Dentz 	u16 handle = PTR_UINT(data);
29521a942de0SBrian Gix 
2953a13f316eSLuiz Augusto von Dentz 	conn = hci_conn_hash_lookup_handle(hdev, handle);
2954a13f316eSLuiz Augusto von Dentz 	if (!conn)
2955b62e7220SLuiz Augusto von Dentz 		return 0;
2956b62e7220SLuiz Augusto von Dentz 
2957a13f316eSLuiz Augusto von Dentz 	return hci_abort_conn_sync(hdev, conn, conn->abort_reason);
29581a942de0SBrian Gix }
29591a942de0SBrian Gix 
2960a13f316eSLuiz Augusto von Dentz int hci_abort_conn(struct hci_conn *conn, u8 reason)
2961a13f316eSLuiz Augusto von Dentz {
2962a13f316eSLuiz Augusto von Dentz 	struct hci_dev *hdev = conn->hdev;
29631a942de0SBrian Gix 
2964a13f316eSLuiz Augusto von Dentz 	/* If abort_reason has already been set it means the connection is
2965a13f316eSLuiz Augusto von Dentz 	 * already being aborted so don't attempt to overwrite it.
29661a942de0SBrian Gix 	 */
2967a13f316eSLuiz Augusto von Dentz 	if (conn->abort_reason)
2968a13f316eSLuiz Augusto von Dentz 		return 0;
29691a942de0SBrian Gix 
2970a13f316eSLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "handle 0x%2.2x reason 0x%2.2x", conn->handle, reason);
2971a13f316eSLuiz Augusto von Dentz 
2972a13f316eSLuiz Augusto von Dentz 	conn->abort_reason = reason;
2973a13f316eSLuiz Augusto von Dentz 
2974a13f316eSLuiz Augusto von Dentz 	/* If the connection is pending check the command opcode since that
2975a13f316eSLuiz Augusto von Dentz 	 * might be blocking on hci_cmd_sync_work while waiting its respective
2976a13f316eSLuiz Augusto von Dentz 	 * event so we need to hci_cmd_sync_cancel to cancel it.
297704a51d61SLuiz Augusto von Dentz 	 *
297804a51d61SLuiz Augusto von Dentz 	 * hci_connect_le serializes the connection attempts so only one
297904a51d61SLuiz Augusto von Dentz 	 * connection can be in BT_CONNECT at time.
2980a13f316eSLuiz Augusto von Dentz 	 */
2981a13f316eSLuiz Augusto von Dentz 	if (conn->state == BT_CONNECT && hdev->req_status == HCI_REQ_PEND) {
2982a13f316eSLuiz Augusto von Dentz 		switch (hci_skb_event(hdev->sent_cmd)) {
2983a13f316eSLuiz Augusto von Dentz 		case HCI_EV_LE_CONN_COMPLETE:
2984a13f316eSLuiz Augusto von Dentz 		case HCI_EV_LE_ENHANCED_CONN_COMPLETE:
2985a13f316eSLuiz Augusto von Dentz 		case HCI_EVT_LE_CIS_ESTABLISHED:
29862af7aa66SLuiz Augusto von Dentz 			hci_cmd_sync_cancel(hdev, ECANCELED);
29871a942de0SBrian Gix 			break;
29881a942de0SBrian Gix 		}
2989a13f316eSLuiz Augusto von Dentz 	}
29901a942de0SBrian Gix 
2991a1f6c3aeSLuiz Augusto von Dentz 	return hci_cmd_sync_queue(hdev, abort_conn_sync, UINT_PTR(conn->handle),
2992a13f316eSLuiz Augusto von Dentz 				  NULL);
29931a942de0SBrian Gix }
2994