blob: 3be2905010cd9dc3efbb5ed6f9177d095429d8d5 [file] [log] [blame]
Ron Shaffer04fafe42010-05-28 11:53:45 -04001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 BlueZ - Bluetooth protocol stack for Linux
Ron Shaffer2d0a0342010-05-28 11:53:46 -04003 Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation;
10
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
Ron Shaffer04fafe42010-05-28 11:53:45 -040015 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
Ron Shaffer04fafe42010-05-28 11:53:45 -040020 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 SOFTWARE IS DISCLAIMED.
23*/
24
25#ifndef __HCI_CORE_H
26#define __HCI_CORE_H
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <net/bluetooth/hci.h>
29
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +020030/* HCI priority */
31#define HCI_PRIO_MAX 7
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/* HCI Core structures */
Linus Torvalds1da177e2005-04-16 15:20:36 -070034struct inquiry_data {
35 bdaddr_t bdaddr;
36 __u8 pscan_rep_mode;
37 __u8 pscan_period_mode;
38 __u8 pscan_mode;
39 __u8 dev_class[3];
Marcel Holtmann1ebb9252005-11-08 09:57:21 -080040 __le16 clock_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 __s8 rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +020042 __u8 ssp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043};
44
45struct inquiry_entry {
Johan Hedberg561aafb2012-01-04 13:31:59 +020046 struct list_head all; /* inq_cache.all */
47 struct list_head list; /* unknown or resolve */
48 enum {
49 NAME_NOT_KNOWN,
50 NAME_NEEDED,
51 NAME_PENDING,
52 NAME_KNOWN,
53 } name_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 __u32 timestamp;
55 struct inquiry_data data;
56};
57
Johan Hedberg30883512012-01-04 14:16:21 +020058struct discovery_state {
Gustavo F. Padovanf64b9932012-03-06 23:48:33 -030059 int type;
Johan Hedbergff9ef572012-01-04 14:23:45 +020060 enum {
61 DISCOVERY_STOPPED,
62 DISCOVERY_STARTING,
Andre Guedes343f9352012-02-17 20:39:37 -030063 DISCOVERY_FINDING,
Johan Hedberg30dc78e2012-01-04 15:44:20 +020064 DISCOVERY_RESOLVING,
Johan Hedbergff9ef572012-01-04 14:23:45 +020065 DISCOVERY_STOPPING,
66 } state;
Gustavo Padovanc3c7ea62012-05-23 04:04:20 -030067 struct list_head all; /* All devices found during inquiry */
Gustavo F. Padovanf64b9932012-03-06 23:48:33 -030068 struct list_head unknown; /* Name state not known */
69 struct list_head resolve; /* Name needs to be resolved */
70 __u32 timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071};
72
73struct hci_conn_hash {
74 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 unsigned int acl_num;
Andrei Emeltchenkobd1eb662012-10-10 17:38:30 +030076 unsigned int amp_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 unsigned int sco_num;
Ville Tervofcd89c02011-02-10 22:38:47 -030078 unsigned int le_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
Johan Hedbergf0358562010-05-18 13:20:32 +020081struct bdaddr_list {
82 struct list_head list;
83 bdaddr_t bdaddr;
Marcel Holtmannb9ee0a72013-10-17 17:24:13 -070084 u8 bdaddr_type;
Johan Hedbergf0358562010-05-18 13:20:32 +020085};
Johan Hedberg2aeb9a12011-01-04 12:08:51 +020086
87struct bt_uuid {
88 struct list_head list;
89 u8 uuid[16];
Johan Hedberg83be8ec2013-01-27 00:31:29 +020090 u8 size;
Johan Hedberg1aff6f02011-01-13 21:56:52 +020091 u8 svc_hint;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +020092};
93
Vinicius Costa Gomesb899efa2012-02-02 21:08:00 -030094struct smp_ltk {
95 struct list_head list;
96 bdaddr_t bdaddr;
97 u8 bdaddr_type;
98 u8 authenticated;
99 u8 type;
100 u8 enc_size;
101 __le16 ediv;
102 u8 rand[8];
103 u8 val[16];
Marcel Holtmann03c515d2014-01-31 18:42:18 -0800104};
Vinicius Costa Gomesb899efa2012-02-02 21:08:00 -0300105
Johan Hedberg970c4e42014-02-18 10:19:33 +0200106struct smp_irk {
107 struct list_head list;
108 bdaddr_t rpa;
109 bdaddr_t bdaddr;
110 u8 addr_type;
111 u8 val[16];
112};
113
Johan Hedberg55ed8ca12011-01-17 14:41:05 +0200114struct link_key {
115 struct list_head list;
116 bdaddr_t bdaddr;
117 u8 type;
Andrei Emeltchenko9b3b4462012-05-23 11:31:20 +0300118 u8 val[HCI_LINK_KEY_SIZE];
Johan Hedberg55ed8ca12011-01-17 14:41:05 +0200119 u8 pin_len;
120};
121
Szymon Janc2763eda2011-03-22 13:12:22 +0100122struct oob_data {
123 struct list_head list;
124 bdaddr_t bdaddr;
Marcel Holtmann519ca9d2014-01-10 02:07:28 -0800125 u8 hash192[16];
126 u8 randomizer192[16];
127 u8 hash256[16];
128 u8 randomizer256[16];
Szymon Janc2763eda2011-03-22 13:12:22 +0100129};
130
Johan Hedberg490c5ba2012-02-22 19:19:09 +0200131#define HCI_MAX_SHORT_NAME_LENGTH 10
132
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300133struct amp_assoc {
134 __u16 len;
135 __u16 offset;
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +0300136 __u16 rem_len;
137 __u16 len_so_far;
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300138 __u8 data[HCI_MAX_AMP_ASSOC_SIZE];
139};
140
Johan Hedbergd2c5d772013-04-17 15:00:52 +0300141#define HCI_MAX_PAGES 3
Johan Hedbergcad718e2013-04-17 15:00:51 +0300142
Suraj Sumangalacd4c5392010-07-14 13:02:16 +0530143#define NUM_REASSEMBLY 4
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144struct hci_dev {
145 struct list_head list;
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300146 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 char name[8];
149 unsigned long flags;
150 __u16 id;
Marcel Holtmannc13854c2010-02-08 15:27:07 +0100151 __u8 bus;
Marcel Holtmann943da252010-02-13 02:28:41 +0100152 __u8 dev_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 bdaddr_t bdaddr;
Marcel Holtmannd13eafc2013-10-02 04:41:30 -0700154 bdaddr_t static_addr;
Marcel Holtmann79830f62013-10-18 16:38:09 -0700155 __u8 own_addr_type;
Johan Hedberg1f6c6372011-03-16 14:29:35 +0200156 __u8 dev_name[HCI_MAX_NAME_LENGTH];
Johan Hedberg490c5ba2012-02-22 19:19:09 +0200157 __u8 short_name[HCI_MAX_SHORT_NAME_LENGTH];
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300158 __u8 eir[HCI_MAX_EIR_LENGTH];
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200159 __u8 dev_class[3];
Johan Hedberg1aff6f02011-01-13 21:56:52 +0200160 __u8 major_class;
161 __u8 minor_class;
Johan Hedbergd2c5d772013-04-17 15:00:52 +0300162 __u8 max_page;
Johan Hedbergcad718e2013-04-17 15:00:51 +0300163 __u8 features[HCI_MAX_PAGES][8];
Johan Hedberg60e77322013-01-22 14:01:59 +0200164 __u8 le_features[8];
Johan Hedbergcf1d0812013-01-22 14:02:00 +0200165 __u8 le_white_list_size;
Johan Hedberg9b008c02013-01-22 14:02:01 +0200166 __u8 le_states[8];
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200167 __u8 commands[64];
Marcel Holtmann1143e5a2006-09-23 09:57:20 +0200168 __u8 hci_ver;
169 __u16 hci_rev;
Johan Hedbergd5859e22011-01-25 01:19:58 +0200170 __u8 lmp_ver;
Marcel Holtmann1143e5a2006-09-23 09:57:20 +0200171 __u16 manufacturer;
Andrei Emeltchenko7d692302012-03-09 11:46:49 +0200172 __u16 lmp_subver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 __u16 voice_setting;
Marcel Holtmannb4cb9fb2013-10-14 13:56:16 -0700174 __u8 num_iac;
Johan Hedberg17fa4b92011-01-25 13:28:33 +0200175 __u8 io_capability;
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -0700176 __s8 inq_tx_power;
Johan Hedbergf332ec62013-03-15 17:07:11 -0500177 __u16 page_scan_interval;
178 __u16 page_scan_window;
179 __u8 page_scan_type;
Marcel Holtmannbef64732013-10-11 08:23:19 -0700180 __u16 le_scan_interval;
181 __u16 le_scan_window;
Marcel Holtmann4e70c7e2013-10-19 07:09:13 -0700182 __u16 le_conn_min_interval;
183 __u16 le_conn_max_interval;
Marcel Holtmann06f5b772013-10-19 07:09:11 -0700184 __u8 ssp_debug_mode;
Johan Hedbergf332ec62013-03-15 17:07:11 -0500185
Marcel Holtmann2b9be132012-03-11 19:32:12 -0700186 __u16 devid_source;
187 __u16 devid_vendor;
188 __u16 devid_product;
189 __u16 devid_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 __u16 pkt_type;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200192 __u16 esco_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 __u16 link_policy;
194 __u16 link_mode;
195
Marcel Holtmann04837f62006-07-03 10:02:33 +0200196 __u32 idle_timeout;
197 __u16 sniff_min_interval;
198 __u16 sniff_max_interval;
199
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300200 __u8 amp_status;
201 __u32 amp_total_bw;
202 __u32 amp_max_bw;
203 __u32 amp_min_latency;
204 __u32 amp_max_pdu;
205 __u8 amp_type;
206 __u16 amp_pal_cap;
207 __u16 amp_assoc_size;
208 __u32 amp_max_flush_to;
209 __u32 amp_be_flush_to;
210
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300211 struct amp_assoc loc_assoc;
212
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200213 __u8 flow_ctl_mode;
214
Johan Hedberg9f616562011-04-28 11:28:54 -0700215 unsigned int auto_accept_delay;
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 unsigned long quirks;
218
219 atomic_t cmd_cnt;
220 unsigned int acl_cnt;
221 unsigned int sco_cnt;
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300222 unsigned int le_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 unsigned int acl_mtu;
225 unsigned int sco_mtu;
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300226 unsigned int le_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 unsigned int acl_pkts;
228 unsigned int sco_pkts;
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300229 unsigned int le_pkts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200231 __u16 block_len;
232 __u16 block_mtu;
233 __u16 num_blocks;
234 __u16 block_cnt;
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 unsigned long acl_last_tx;
237 unsigned long sco_last_tx;
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300238 unsigned long le_last_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +0100240 struct workqueue_struct *workqueue;
Johan Hedberg6ead1bb2013-01-14 22:33:50 +0200241 struct workqueue_struct *req_workqueue;
Marcel Holtmannf48fd9c2010-03-20 15:20:04 +0100242
Johan Hedbergab81cbf2010-12-15 13:53:18 +0200243 struct work_struct power_on;
Johan Hedberg32435532011-11-07 22:16:04 +0200244 struct delayed_work power_off;
Johan Hedbergab81cbf2010-12-15 13:53:18 +0200245
Johan Hedberg16ab91a2011-11-07 22:16:02 +0200246 __u16 discov_timeout;
247 struct delayed_work discov_off;
248
Johan Hedberg7d785252011-12-15 00:47:39 +0200249 struct delayed_work service_cache;
250
Ville Tervo6bd32322011-02-16 16:32:41 +0200251 struct timer_list cmd_timer;
Marcel Holtmannb78752c2010-08-08 23:06:53 -0400252
253 struct work_struct rx_work;
Gustavo F. Padovanc347b762011-12-14 23:53:47 -0200254 struct work_struct cmd_work;
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -0200255 struct work_struct tx_work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 struct sk_buff_head rx_q;
258 struct sk_buff_head raw_q;
259 struct sk_buff_head cmd_q;
260
Johan Hedbergb6ddb632013-04-02 13:34:31 +0300261 struct sk_buff *recv_evt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 struct sk_buff *sent_cmd;
Suraj Sumangalacd4c5392010-07-14 13:02:16 +0530263 struct sk_buff *reassembly[NUM_REASSEMBLY];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Thomas Gleixnera6a67ef2009-07-26 08:18:19 +0000265 struct mutex req_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 wait_queue_head_t req_wait_q;
267 __u32 req_status;
268 __u32 req_result;
Johan Hedberga5040ef2011-01-10 13:28:59 +0200269
Johan Hedberg99780a72014-02-18 10:40:07 +0200270 struct crypto_blkcipher *tfm_aes;
Johan Hedberg2e58ef32011-11-08 20:40:15 +0200271
Johan Hedberg30883512012-01-04 14:16:21 +0200272 struct discovery_state discovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 struct hci_conn_hash conn_hash;
Andre Guedes5c136e92014-01-30 18:22:05 -0300274
275 struct list_head mgmt_pending;
David Millerea4bd8b2010-07-30 21:54:49 -0700276 struct list_head blacklist;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +0200277 struct list_head uuids;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +0200278 struct list_head link_keys;
Vinicius Costa Gomesb899efa2012-02-02 21:08:00 -0300279 struct list_head long_term_keys;
Johan Hedberg970c4e42014-02-18 10:19:33 +0200280 struct list_head identity_resolving_keys;
Szymon Janc2763eda2011-03-22 13:12:22 +0100281 struct list_head remote_oob_data;
Andre Guedes15819a72014-02-03 13:56:18 -0300282 struct list_head le_conn_params;
Szymon Janc2763eda2011-03-22 13:12:22 +0100283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 struct hci_dev_stats stat;
285
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200286 atomic_t promisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Marcel Holtmannca325f62010-02-08 16:22:31 +0100288 struct dentry *debugfs;
289
Marcel Holtmanna91f2e32006-07-03 10:02:41 +0200290 struct device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Marcel Holtmann611b30f2009-06-08 14:41:38 +0200292 struct rfkill *rfkill;
293
Andre Guedesd23264a2011-11-25 20:53:38 -0300294 unsigned long dev_flags;
295
Andre Guedes7ba8b4b2012-02-03 17:47:59 -0300296 struct delayed_work le_scan_disable;
297
Johan Hedberg8fa19092012-10-19 20:57:49 +0300298 __s8 adv_tx_power;
Johan Hedberg3f0f5242012-11-08 01:23:00 +0100299 __u8 adv_data[HCI_MAX_AD_LENGTH];
300 __u8 adv_data_len;
Marcel Holtmannf8e808b2013-10-16 00:16:47 -0700301 __u8 scan_rsp_data[HCI_MAX_AD_LENGTH];
302 __u8 scan_rsp_data_len;
Johan Hedberg8fa19092012-10-19 20:57:49 +0300303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 int (*open)(struct hci_dev *hdev);
305 int (*close)(struct hci_dev *hdev);
306 int (*flush)(struct hci_dev *hdev);
Marcel Holtmannf41c70c2012-11-12 14:02:14 +0900307 int (*setup)(struct hci_dev *hdev);
Marcel Holtmann7bd8f092013-10-11 06:19:18 -0700308 int (*send)(struct hci_dev *hdev, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 void (*notify)(struct hci_dev *hdev, unsigned int evt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310};
311
Andrei Emeltchenko53502d62012-10-10 17:38:27 +0300312#define HCI_PHY_HANDLE(handle) (handle & 0xff)
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314struct hci_conn {
315 struct list_head list;
316
Szymon Jancadc42662011-02-17 16:42:00 +0100317 atomic_t refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Szymon Jancadc42662011-02-17 16:42:00 +0100319 bdaddr_t dst;
Waldemar Rymarkiewicz5a9d0a32011-06-07 11:18:06 +0200320 __u8 dst_type;
Marcel Holtmann662e8822013-10-13 05:23:59 -0700321 bdaddr_t src;
Marcel Holtmanne7c40962013-10-13 03:57:39 -0700322 __u8 src_type;
Szymon Jancadc42662011-02-17 16:42:00 +0100323 __u16 handle;
324 __u16 state;
325 __u8 mode;
326 __u8 type;
Johan Hedberga0c808b2012-01-16 09:49:58 +0200327 bool out;
Szymon Jancadc42662011-02-17 16:42:00 +0100328 __u8 attempt;
329 __u8 dev_class[3];
Johan Hedbergcad718e2013-04-17 15:00:51 +0300330 __u8 features[HCI_MAX_PAGES][8];
Szymon Jancadc42662011-02-17 16:42:00 +0100331 __u16 pkt_type;
332 __u16 link_policy;
333 __u32 link_mode;
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200334 __u8 key_type;
Szymon Jancadc42662011-02-17 16:42:00 +0100335 __u8 auth_type;
336 __u8 sec_level;
337 __u8 pending_sec_level;
338 __u8 pin_length;
Vinicius Costa Gomes726b4ff2011-07-08 18:31:45 -0300339 __u8 enc_key_size;
Szymon Jancadc42662011-02-17 16:42:00 +0100340 __u8 io_capability;
Johan Hedberg92a25252012-09-06 18:39:26 +0300341 __u32 passkey_notify;
342 __u8 passkey_entered;
Szymon Jancadc42662011-02-17 16:42:00 +0100343 __u16 disc_timeout;
Frédéric Dalleau10c62dd2013-08-19 14:23:59 +0200344 __u16 setting;
Andre Guedes1e406ee2014-01-30 18:22:04 -0300345 __u16 le_conn_min_interval;
346 __u16 le_conn_max_interval;
Johan Hedberg51a8efd2012-01-16 06:10:31 +0200347 unsigned long flags;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200348
Johan Hedberg03b555e2011-01-04 15:40:05 +0200349 __u8 remote_cap;
Johan Hedberg03b555e2011-01-04 15:40:05 +0200350 __u8 remote_auth;
Andrei Emeltchenko3161ae12012-09-27 17:26:11 +0300351 __u8 remote_id;
Vishal Agarwal6ec5bca2012-04-16 14:44:44 +0530352 bool flush_key;
Johan Hedberg03b555e2011-01-04 15:40:05 +0200353
Szymon Jancadc42662011-02-17 16:42:00 +0100354 unsigned int sent;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 struct sk_buff_head data_q;
Gustavo F. Padovan2c33c062011-12-14 13:02:51 -0200357 struct list_head chan_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Gustavo F. Padovan19c40e32011-06-17 13:03:21 -0300359 struct delayed_work disc_work;
Johan Hedberg7bc18d92013-10-16 18:11:39 +0300360 struct delayed_work auto_accept_work;
Johan Hedberga74a84f2013-10-16 18:11:40 +0300361 struct delayed_work idle_work;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200362
Marcel Holtmannb219e3a2006-07-06 12:38:46 +0200363 struct device dev;
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 struct hci_dev *hdev;
366 void *l2cap_data;
367 void *sco_data;
Brian Gix2b64d152011-12-21 16:12:12 -0800368 void *smp_conn;
Andrei Emeltchenko9740e492012-05-29 13:59:02 +0300369 struct amp_mgr *amp_mgr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 struct hci_conn *link;
Johan Hedberge9a416b2011-02-19 12:05:56 -0300372
373 void (*connect_cfm_cb) (struct hci_conn *conn, u8 status);
374 void (*security_cfm_cb) (struct hci_conn *conn, u8 status);
375 void (*disconn_cfm_cb) (struct hci_conn *conn, u8 reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376};
377
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200378struct hci_chan {
379 struct list_head list;
Andrei Emeltchenko42c4e532012-10-10 17:38:28 +0300380 __u16 handle;
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200381 struct hci_conn *conn;
382 struct sk_buff_head data_q;
383 unsigned int sent;
Mat Martineau168df8e2012-10-23 15:24:13 -0700384 __u8 state;
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200385};
386
Andre Guedes15819a72014-02-03 13:56:18 -0300387struct hci_conn_params {
388 struct list_head list;
389
390 bdaddr_t addr;
391 u8 addr_type;
392
393 u16 conn_min_interval;
394 u16 conn_max_interval;
395};
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397extern struct list_head hci_dev_list;
398extern struct list_head hci_cb_list;
399extern rwlock_t hci_dev_list_lock;
400extern rwlock_t hci_cb_list_lock;
401
Ulisses Furquim686ebf22011-12-21 10:11:33 -0200402/* ----- HCI interface to upper protocols ----- */
Joe Perchese74e58f2013-09-23 11:37:36 -0700403int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr);
404void l2cap_connect_cfm(struct hci_conn *hcon, u8 status);
405int l2cap_disconn_ind(struct hci_conn *hcon);
406void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason);
407int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt);
408int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags);
Ulisses Furquim686ebf22011-12-21 10:11:33 -0200409
Joe Perchese74e58f2013-09-23 11:37:36 -0700410int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags);
411void sco_connect_cfm(struct hci_conn *hcon, __u8 status);
412void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason);
413int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb);
Ulisses Furquim686ebf22011-12-21 10:11:33 -0200414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415/* ----- Inquiry cache ----- */
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200416#define INQUIRY_CACHE_AGE_MAX (HZ*30) /* 30 seconds */
417#define INQUIRY_ENTRY_AGE_MAX (HZ*60) /* 60 seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Johan Hedberg30883512012-01-04 14:16:21 +0200419static inline void discovery_init(struct hci_dev *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Johan Hedbergff9ef572012-01-04 14:23:45 +0200421 hdev->discovery.state = DISCOVERY_STOPPED;
Johan Hedberg30883512012-01-04 14:16:21 +0200422 INIT_LIST_HEAD(&hdev->discovery.all);
423 INIT_LIST_HEAD(&hdev->discovery.unknown);
424 INIT_LIST_HEAD(&hdev->discovery.resolve);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Johan Hedberg30dc78e2012-01-04 15:44:20 +0200427bool hci_discovery_active(struct hci_dev *hdev);
428
Johan Hedbergff9ef572012-01-04 14:23:45 +0200429void hci_discovery_set_state(struct hci_dev *hdev, int state);
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431static inline int inquiry_cache_empty(struct hci_dev *hdev)
432{
Johan Hedberg30883512012-01-04 14:16:21 +0200433 return list_empty(&hdev->discovery.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436static inline long inquiry_cache_age(struct hci_dev *hdev)
437{
Johan Hedberg30883512012-01-04 14:16:21 +0200438 struct discovery_state *c = &hdev->discovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return jiffies - c->timestamp;
440}
441
442static inline long inquiry_entry_age(struct inquiry_entry *e)
443{
444 return jiffies - e->timestamp;
445}
446
Waldemar Rymarkiewicz5a9d0a32011-06-07 11:18:06 +0200447struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300448 bdaddr_t *bdaddr);
Johan Hedberg561aafb2012-01-04 13:31:59 +0200449struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300450 bdaddr_t *bdaddr);
Johan Hedberg30dc78e2012-01-04 15:44:20 +0200451struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300452 bdaddr_t *bdaddr,
453 int state);
Johan Hedberga3d4e202012-01-09 00:53:02 +0200454void hci_inquiry_cache_update_resolve(struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300455 struct inquiry_entry *ie);
Johan Hedberg31754052012-01-04 13:39:52 +0200456bool hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300457 bool name_known, bool *ssp);
Andre Guedes1f9b9a52013-04-30 15:29:27 -0300458void hci_inquiry_cache_flush(struct hci_dev *hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460/* ----- HCI Connections ----- */
461enum {
462 HCI_CONN_AUTH_PEND,
Waldemar Rymarkiewicz19f8def2011-05-31 15:49:25 +0200463 HCI_CONN_REAUTH_PEND,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 HCI_CONN_ENCRYPT_PEND,
Marcel Holtmann04837f62006-07-03 10:02:33 +0200465 HCI_CONN_RSWITCH_PEND,
466 HCI_CONN_MODE_CHANGE_PEND,
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400467 HCI_CONN_SCO_SETUP_PEND,
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -0300468 HCI_CONN_LE_SMP_PEND,
Johan Hedbergb644ba32012-01-17 21:48:47 +0200469 HCI_CONN_MGMT_CONNECTED,
Johan Hedberg58a681e2012-01-16 06:47:28 +0200470 HCI_CONN_SSP_ENABLED,
Marcel Holtmanneb9a8f32014-01-15 22:37:38 -0800471 HCI_CONN_SC_ENABLED,
Marcel Holtmannabf76ba2014-01-31 16:24:28 -0800472 HCI_CONN_AES_CCM,
Johan Hedberg58a681e2012-01-16 06:47:28 +0200473 HCI_CONN_POWER_SAVE,
474 HCI_CONN_REMOTE_OOB,
Jukka Rissanen18722c22013-12-11 17:05:37 +0200475 HCI_CONN_6LOWPAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476};
477
Johan Hedbergaa64a8b2012-01-18 21:33:12 +0200478static inline bool hci_conn_ssp_enabled(struct hci_conn *conn)
479{
480 struct hci_dev *hdev = conn->hdev;
Gustavo Padovanc3c7ea62012-05-23 04:04:20 -0300481 return test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) &&
482 test_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
Johan Hedbergaa64a8b2012-01-18 21:33:12 +0200483}
484
Marcel Holtmanneb9a8f32014-01-15 22:37:38 -0800485static inline bool hci_conn_sc_enabled(struct hci_conn *conn)
486{
487 struct hci_dev *hdev = conn->hdev;
488 return test_bit(HCI_SC_ENABLED, &hdev->dev_flags) &&
489 test_bit(HCI_CONN_SC_ENABLED, &conn->flags);
490}
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492static inline void hci_conn_hash_add(struct hci_dev *hdev, struct hci_conn *c)
493{
494 struct hci_conn_hash *h = &hdev->conn_hash;
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -0200495 list_add_rcu(&c->list, &h->list);
Ville Tervofcd89c02011-02-10 22:38:47 -0300496 switch (c->type) {
497 case ACL_LINK:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 h->acl_num++;
Ville Tervofcd89c02011-02-10 22:38:47 -0300499 break;
Andrei Emeltchenkobd1eb662012-10-10 17:38:30 +0300500 case AMP_LINK:
501 h->amp_num++;
502 break;
Ville Tervofcd89c02011-02-10 22:38:47 -0300503 case LE_LINK:
504 h->le_num++;
505 break;
506 case SCO_LINK:
507 case ESCO_LINK:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 h->sco_num++;
Ville Tervofcd89c02011-02-10 22:38:47 -0300509 break;
510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
513static inline void hci_conn_hash_del(struct hci_dev *hdev, struct hci_conn *c)
514{
515 struct hci_conn_hash *h = &hdev->conn_hash;
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -0200516
517 list_del_rcu(&c->list);
518 synchronize_rcu();
519
Ville Tervofcd89c02011-02-10 22:38:47 -0300520 switch (c->type) {
521 case ACL_LINK:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 h->acl_num--;
Ville Tervofcd89c02011-02-10 22:38:47 -0300523 break;
Andrei Emeltchenkobd1eb662012-10-10 17:38:30 +0300524 case AMP_LINK:
525 h->amp_num--;
526 break;
Ville Tervofcd89c02011-02-10 22:38:47 -0300527 case LE_LINK:
528 h->le_num--;
529 break;
530 case SCO_LINK:
531 case ESCO_LINK:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 h->sco_num--;
Ville Tervofcd89c02011-02-10 22:38:47 -0300533 break;
534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Luiz Augusto von Dentz52087a72011-08-17 16:23:00 +0300537static inline unsigned int hci_conn_num(struct hci_dev *hdev, __u8 type)
538{
539 struct hci_conn_hash *h = &hdev->conn_hash;
540 switch (type) {
541 case ACL_LINK:
542 return h->acl_num;
Andrei Emeltchenkobd1eb662012-10-10 17:38:30 +0300543 case AMP_LINK:
544 return h->amp_num;
Luiz Augusto von Dentz52087a72011-08-17 16:23:00 +0300545 case LE_LINK:
546 return h->le_num;
547 case SCO_LINK:
548 case ESCO_LINK:
549 return h->sco_num;
550 default:
551 return 0;
552 }
553}
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555static inline struct hci_conn *hci_conn_hash_lookup_handle(struct hci_dev *hdev,
Szymon Jancadc42662011-02-17 16:42:00 +0100556 __u16 handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 struct hci_conn_hash *h = &hdev->conn_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 struct hci_conn *c;
560
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -0200561 rcu_read_lock();
562
563 list_for_each_entry_rcu(c, &h->list, list) {
564 if (c->handle == handle) {
565 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return c;
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -0200567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -0200569 rcu_read_unlock();
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return NULL;
572}
573
574static inline struct hci_conn *hci_conn_hash_lookup_ba(struct hci_dev *hdev,
Szymon Jancadc42662011-02-17 16:42:00 +0100575 __u8 type, bdaddr_t *ba)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 struct hci_conn_hash *h = &hdev->conn_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 struct hci_conn *c;
579
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -0200580 rcu_read_lock();
581
582 list_for_each_entry_rcu(c, &h->list, list) {
583 if (c->type == type && !bacmp(&c->dst, ba)) {
584 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return c;
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -0200586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -0200588
589 rcu_read_unlock();
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return NULL;
592}
593
Marcel Holtmann4c67bc72006-10-15 17:30:56 +0200594static inline struct hci_conn *hci_conn_hash_lookup_state(struct hci_dev *hdev,
Szymon Jancadc42662011-02-17 16:42:00 +0100595 __u8 type, __u16 state)
Marcel Holtmann4c67bc72006-10-15 17:30:56 +0200596{
597 struct hci_conn_hash *h = &hdev->conn_hash;
Marcel Holtmann4c67bc72006-10-15 17:30:56 +0200598 struct hci_conn *c;
599
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -0200600 rcu_read_lock();
601
602 list_for_each_entry_rcu(c, &h->list, list) {
603 if (c->type == type && c->state == state) {
604 rcu_read_unlock();
Marcel Holtmann4c67bc72006-10-15 17:30:56 +0200605 return c;
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -0200606 }
Marcel Holtmann4c67bc72006-10-15 17:30:56 +0200607 }
Gustavo F. Padovanbf4c6322011-12-14 22:54:12 -0200608
609 rcu_read_unlock();
610
Marcel Holtmann4c67bc72006-10-15 17:30:56 +0200611 return NULL;
612}
613
Andre Guedesbed71742013-01-30 11:50:56 -0300614void hci_disconnect(struct hci_conn *conn, __u8 reason);
Frédéric Dalleau2dea6322013-08-19 14:24:03 +0200615bool hci_setup_sync(struct hci_conn *conn, __u16 handle);
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400616void hci_sco_setup(struct hci_conn *conn, __u8 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200619int hci_conn_del(struct hci_conn *conn);
620void hci_conn_hash_flush(struct hci_dev *hdev);
621void hci_conn_check_pending(struct hci_dev *hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200623struct hci_chan *hci_chan_create(struct hci_conn *conn);
Andrei Emeltchenko94720072012-09-06 15:05:43 +0300624void hci_chan_del(struct hci_chan *chan);
Gustavo F. Padovan2c33c062011-12-14 13:02:51 -0200625void hci_chan_list_flush(struct hci_conn *conn);
Andrei Emeltchenko42c4e532012-10-10 17:38:28 +0300626struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle);
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200627
Waldemar Rymarkiewicz5a9d0a32011-06-07 11:18:06 +0200628struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst,
Andre Guedesb12f62c2012-04-24 21:02:54 -0300629 __u8 dst_type, __u8 sec_level, __u8 auth_type);
Frédéric Dalleau10c62dd2013-08-19 14:23:59 +0200630struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst,
631 __u16 setting);
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +0200632int hci_conn_check_link_mode(struct hci_conn *conn);
Waldemar Rymarkiewiczb3b1b062011-05-06 09:42:31 +0200633int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level);
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100634int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635int hci_conn_change_link_key(struct hci_conn *conn);
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100636int hci_conn_switch_role(struct hci_conn *conn, __u8 role);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700638void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
David Herrmann8d123562013-04-06 20:28:39 +0200640/*
641 * hci_conn_get() and hci_conn_put() are used to control the life-time of an
642 * "hci_conn" object. They do not guarantee that the hci_conn object is running,
643 * working or anything else. They just guarantee that the object is available
644 * and can be dereferenced. So you can use its locks, local variables and any
645 * other constant data.
646 * Before accessing runtime data, you _must_ lock the object and then check that
647 * it is still running. As soon as you release the locks, the connection might
648 * get dropped, though.
649 *
650 * On the other hand, hci_conn_hold() and hci_conn_drop() are used to control
651 * how long the underlying connection is held. So every channel that runs on the
652 * hci_conn object calls this to prevent the connection from disappearing. As
653 * long as you hold a device, you must also guarantee that you have a valid
654 * reference to the device via hci_conn_get() (or the initial reference from
655 * hci_conn_add()).
656 * The hold()/drop() ref-count is known to drop below 0 sometimes, which doesn't
657 * break because nobody cares for that. But this means, we cannot use
658 * _get()/_drop() in it, but require the caller to have a valid ref (FIXME).
659 */
660
661static inline void hci_conn_get(struct hci_conn *conn)
662{
663 get_device(&conn->dev);
664}
665
666static inline void hci_conn_put(struct hci_conn *conn)
667{
668 put_device(&conn->dev);
669}
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671static inline void hci_conn_hold(struct hci_conn *conn)
672{
Andrei Emeltchenko71becf02012-07-19 17:03:42 +0300673 BT_DBG("hcon %p orig refcnt %d", conn, atomic_read(&conn->refcnt));
Andrei Emeltchenko38b3fef2012-06-15 11:50:28 +0300674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 atomic_inc(&conn->refcnt);
Andre Guedes2f304d12012-01-27 19:42:02 -0300676 cancel_delayed_work(&conn->disc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
David Herrmann76a68ba2013-04-06 20:28:37 +0200679static inline void hci_conn_drop(struct hci_conn *conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Andrei Emeltchenko71becf02012-07-19 17:03:42 +0300681 BT_DBG("hcon %p orig refcnt %d", conn, atomic_read(&conn->refcnt));
Andrei Emeltchenko38b3fef2012-06-15 11:50:28 +0300682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (atomic_dec_and_test(&conn->refcnt)) {
Marcel Holtmann04837f62006-07-03 10:02:33 +0200684 unsigned long timeo;
Andrei Emeltchenko716e4ab2012-10-10 17:38:31 +0300685
686 switch (conn->type) {
687 case ACL_LINK:
688 case LE_LINK:
Johan Hedberga74a84f2013-10-16 18:11:40 +0300689 cancel_delayed_work(&conn->idle_work);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200690 if (conn->state == BT_CONNECTED) {
Andrei Emeltchenko5f246e82012-06-11 11:13:07 +0300691 timeo = conn->disc_timeout;
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200692 if (!conn->out)
Marcel Holtmann052b30b2009-04-26 20:01:22 +0200693 timeo *= 2;
Waldemar Rymarkiewicz5a9d0a32011-06-07 11:18:06 +0200694 } else {
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200695 timeo = msecs_to_jiffies(10);
Waldemar Rymarkiewicz5a9d0a32011-06-07 11:18:06 +0200696 }
Andrei Emeltchenko716e4ab2012-10-10 17:38:31 +0300697 break;
698
699 case AMP_LINK:
700 timeo = conn->disc_timeout;
701 break;
702
703 default:
Marcel Holtmann04837f62006-07-03 10:02:33 +0200704 timeo = msecs_to_jiffies(10);
Andrei Emeltchenko716e4ab2012-10-10 17:38:31 +0300705 break;
Waldemar Rymarkiewicz5a9d0a32011-06-07 11:18:06 +0200706 }
Andrei Emeltchenko716e4ab2012-10-10 17:38:31 +0300707
Andre Guedes2f304d12012-01-27 19:42:02 -0300708 cancel_delayed_work(&conn->disc_work);
Gustavo F. Padovan19c40e32011-06-17 13:03:21 -0300709 queue_delayed_work(conn->hdev->workqueue,
Andrei Emeltchenko716e4ab2012-10-10 17:38:31 +0300710 &conn->disc_work, timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712}
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714/* ----- HCI Devices ----- */
David Herrmanndc946bd2012-01-07 15:47:24 +0100715static inline void hci_dev_put(struct hci_dev *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Andrei Emeltchenko376261a2012-09-06 15:05:45 +0300717 BT_DBG("%s orig refcnt %d", d->name,
718 atomic_read(&d->dev.kobj.kref.refcount));
719
David Herrmann4c724c72012-01-07 15:47:23 +0100720 put_device(&d->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
David Herrmanndc946bd2012-01-07 15:47:24 +0100723static inline struct hci_dev *hci_dev_hold(struct hci_dev *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Andrei Emeltchenko376261a2012-09-06 15:05:45 +0300725 BT_DBG("%s orig refcnt %d", d->name,
726 atomic_read(&d->dev.kobj.kref.refcount));
727
David Herrmann4c724c72012-01-07 15:47:23 +0100728 get_device(&d->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return d;
730}
731
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -0300732#define hci_dev_lock(d) mutex_lock(&d->lock)
733#define hci_dev_unlock(d) mutex_unlock(&d->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
David Herrmannaa2b86d2012-02-09 21:58:30 +0100735#define to_hci_dev(d) container_of(d, struct hci_dev, dev)
David Herrmann3dc07322012-02-09 21:58:33 +0100736#define to_hci_conn(c) container_of(c, struct hci_conn, dev)
David Herrmannaa2b86d2012-02-09 21:58:30 +0100737
David Herrmann155961e2012-02-09 21:58:32 +0100738static inline void *hci_get_drvdata(struct hci_dev *hdev)
739{
740 return dev_get_drvdata(&hdev->dev);
741}
742
743static inline void hci_set_drvdata(struct hci_dev *hdev, void *data)
744{
745 dev_set_drvdata(&hdev->dev, data);
746}
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748struct hci_dev *hci_dev_get(int index);
Johan Hedberg0c0afed2012-11-01 13:27:26 +0200749struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751struct hci_dev *hci_alloc_dev(void);
752void hci_free_dev(struct hci_dev *hdev);
753int hci_register_dev(struct hci_dev *hdev);
David Herrmann59735632011-10-26 10:43:19 +0200754void hci_unregister_dev(struct hci_dev *hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755int hci_suspend_dev(struct hci_dev *hdev);
756int hci_resume_dev(struct hci_dev *hdev);
757int hci_dev_open(__u16 dev);
758int hci_dev_close(__u16 dev);
759int hci_dev_reset(__u16 dev);
760int hci_dev_reset_stat(__u16 dev);
761int hci_dev_cmd(unsigned int cmd, void __user *arg);
762int hci_get_dev_list(void __user *arg);
763int hci_get_dev_info(void __user *arg);
764int hci_get_conn_list(void __user *arg);
765int hci_get_conn_info(struct hci_dev *hdev, void __user *arg);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200766int hci_get_auth_info(struct hci_dev *hdev, void __user *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767int hci_inquiry(void __user *arg);
768
Gustavo Padovanc3c7ea62012-05-23 04:04:20 -0300769struct bdaddr_list *hci_blacklist_lookup(struct hci_dev *hdev,
Marcel Holtmannb9ee0a72013-10-17 17:24:13 -0700770 bdaddr_t *bdaddr, u8 type);
Johan Hedberg35f74982014-02-18 17:14:32 +0200771void hci_blacklist_clear(struct hci_dev *hdev);
Johan Hedberg88c1fe42012-02-09 15:56:11 +0200772int hci_blacklist_add(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
773int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
Johan Hedbergf0358562010-05-18 13:20:32 +0200774
Andre Guedes15819a72014-02-03 13:56:18 -0300775struct hci_conn_params *hci_conn_params_lookup(struct hci_dev *hdev,
776 bdaddr_t *addr, u8 addr_type);
777void hci_conn_params_add(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type,
778 u16 conn_min_interval, u16 conn_max_interval);
779void hci_conn_params_del(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type);
780void hci_conn_params_clear(struct hci_dev *hdev);
781
Johan Hedberg35f74982014-02-18 17:14:32 +0200782void hci_uuids_clear(struct hci_dev *hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +0200783
Johan Hedberg35f74982014-02-18 17:14:32 +0200784void hci_link_keys_clear(struct hci_dev *hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +0200785struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
Johan Hedbergd25e28a2011-04-28 11:28:59 -0700786int hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, int new_key,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300787 bdaddr_t *bdaddr, u8 *val, u8 type, u8 pin_len);
Johan Hedberg98a0b842014-01-30 19:40:00 -0800788struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, u8 rand[8],
789 bool master);
Johan Hedbergca9142b2014-02-19 14:57:44 +0200790struct smp_ltk *hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg35d70272014-02-19 14:57:47 +0200791 u8 addr_type, u8 type, u8 authenticated,
792 u8 tk[16], u8 enc_size, __le16 ediv, u8 rand[8]);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -0300793struct smp_ltk *hci_find_ltk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg98a0b842014-01-30 19:40:00 -0800794 u8 addr_type, bool master);
Johan Hedberge0b2b272014-02-18 17:14:31 +0200795int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 bdaddr_type);
Johan Hedberg35f74982014-02-18 17:14:32 +0200796void hci_smp_ltks_clear(struct hci_dev *hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +0200797int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
798
Johan Hedberg970c4e42014-02-18 10:19:33 +0200799struct smp_irk *hci_find_irk_by_rpa(struct hci_dev *hdev, bdaddr_t *rpa);
800struct smp_irk *hci_find_irk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr,
801 u8 addr_type);
Johan Hedbergca9142b2014-02-19 14:57:44 +0200802struct smp_irk *hci_add_irk(struct hci_dev *hdev, bdaddr_t *bdaddr,
803 u8 addr_type, u8 val[16], bdaddr_t *rpa);
Johan Hedberga7ec7332014-02-18 17:14:35 +0200804void hci_remove_irk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type);
Johan Hedberg970c4e42014-02-18 10:19:33 +0200805void hci_smp_irks_clear(struct hci_dev *hdev);
806
Johan Hedberg35f74982014-02-18 17:14:32 +0200807void hci_remote_oob_data_clear(struct hci_dev *hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +0100808struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev,
Marcel Holtmann07988722014-01-10 02:07:29 -0800809 bdaddr_t *bdaddr);
810int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr,
811 u8 *hash, u8 *randomizer);
812int hci_add_remote_oob_ext_data(struct hci_dev *hdev, bdaddr_t *bdaddr,
813 u8 *hash192, u8 *randomizer192,
814 u8 *hash256, u8 *randomizer256);
Szymon Janc2763eda2011-03-22 13:12:22 +0100815int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr);
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb);
818
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700819int hci_recv_frame(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmannef222012007-07-11 06:42:04 +0200820int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count);
Suraj Sumangala99811512010-07-14 13:02:19 +0530821int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count);
Marcel Holtmannef222012007-07-11 06:42:04 +0200822
David Herrmann0ac7e702011-10-08 14:58:47 +0200823void hci_init_sysfs(struct hci_dev *hdev);
Marcel Holtmanna67e8992009-05-02 18:24:06 -0700824void hci_conn_init_sysfs(struct hci_conn *conn);
Marcel Holtmannb219e3a2006-07-06 12:38:46 +0200825void hci_conn_add_sysfs(struct hci_conn *conn);
826void hci_conn_del_sysfs(struct hci_conn *conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
David Herrmann6935e0f2012-03-09 15:53:42 +0100828#define SET_HCIDEV_DEV(hdev, pdev) ((hdev)->dev.parent = (pdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830/* ----- LMP capabilities ----- */
Johan Hedbergcad718e2013-04-17 15:00:51 +0300831#define lmp_encrypt_capable(dev) ((dev)->features[0][0] & LMP_ENCRYPT)
832#define lmp_rswitch_capable(dev) ((dev)->features[0][0] & LMP_RSWITCH)
833#define lmp_hold_capable(dev) ((dev)->features[0][0] & LMP_HOLD)
834#define lmp_sniff_capable(dev) ((dev)->features[0][0] & LMP_SNIFF)
835#define lmp_park_capable(dev) ((dev)->features[0][1] & LMP_PARK)
836#define lmp_inq_rssi_capable(dev) ((dev)->features[0][3] & LMP_RSSI_INQ)
837#define lmp_esco_capable(dev) ((dev)->features[0][3] & LMP_ESCO)
838#define lmp_bredr_capable(dev) (!((dev)->features[0][4] & LMP_NO_BREDR))
839#define lmp_le_capable(dev) ((dev)->features[0][4] & LMP_LE)
840#define lmp_sniffsubr_capable(dev) ((dev)->features[0][5] & LMP_SNIFF_SUBR)
841#define lmp_pause_enc_capable(dev) ((dev)->features[0][5] & LMP_PAUSE_ENC)
842#define lmp_ext_inq_capable(dev) ((dev)->features[0][6] & LMP_EXT_INQ)
843#define lmp_le_br_capable(dev) (!!((dev)->features[0][6] & LMP_SIMUL_LE_BR))
844#define lmp_ssp_capable(dev) ((dev)->features[0][6] & LMP_SIMPLE_PAIR)
845#define lmp_no_flush_capable(dev) ((dev)->features[0][6] & LMP_NO_FLUSH)
846#define lmp_lsto_capable(dev) ((dev)->features[0][7] & LMP_LSTO)
847#define lmp_inq_tx_pwr_capable(dev) ((dev)->features[0][7] & LMP_INQ_TX_PWR)
848#define lmp_ext_feat_capable(dev) ((dev)->features[0][7] & LMP_EXTFEATURES)
Frédéric Dalleau07a5c612013-08-19 14:24:00 +0200849#define lmp_transp_capable(dev) ((dev)->features[0][2] & LMP_TRANSPARENT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Andre Guedeseead27d2011-06-30 19:20:55 -0300851/* ----- Extended LMP capabilities ----- */
Marcel Holtmann53b834d22013-12-08 11:55:33 -0800852#define lmp_csb_master_capable(dev) ((dev)->features[2][0] & LMP_CSB_MASTER)
853#define lmp_csb_slave_capable(dev) ((dev)->features[2][0] & LMP_CSB_SLAVE)
854#define lmp_sync_train_capable(dev) ((dev)->features[2][0] & LMP_SYNC_TRAIN)
855#define lmp_sync_scan_capable(dev) ((dev)->features[2][0] & LMP_SYNC_SCAN)
Marcel Holtmannd5991582014-01-10 02:07:16 -0800856#define lmp_sc_capable(dev) ((dev)->features[2][1] & LMP_SC)
857#define lmp_ping_capable(dev) ((dev)->features[2][1] & LMP_PING)
Marcel Holtmann53b834d22013-12-08 11:55:33 -0800858
859/* ----- Host capabilities ----- */
Johan Hedbergcad718e2013-04-17 15:00:51 +0300860#define lmp_host_ssp_capable(dev) ((dev)->features[1][0] & LMP_HOST_SSP)
Marcel Holtmannd5991582014-01-10 02:07:16 -0800861#define lmp_host_sc_capable(dev) ((dev)->features[1][0] & LMP_HOST_SC)
Johan Hedbergcad718e2013-04-17 15:00:51 +0300862#define lmp_host_le_capable(dev) (!!((dev)->features[1][0] & LMP_HOST_LE))
863#define lmp_host_le_br_capable(dev) (!!((dev)->features[1][0] & LMP_HOST_LE_BREDR))
Andre Guedeseead27d2011-06-30 19:20:55 -0300864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865/* ----- HCI protocols ----- */
Frédéric Dalleau20714bf2012-11-21 10:51:12 +0100866#define HCI_PROTO_DEFER 0x01
867
Waldemar Rymarkiewicz5a9d0a32011-06-07 11:18:06 +0200868static inline int hci_proto_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr,
Frédéric Dalleau20714bf2012-11-21 10:51:12 +0100869 __u8 type, __u8 *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Ulisses Furquim686ebf22011-12-21 10:11:33 -0200871 switch (type) {
872 case ACL_LINK:
873 return l2cap_connect_ind(hdev, bdaddr);
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100874
Ulisses Furquim686ebf22011-12-21 10:11:33 -0200875 case SCO_LINK:
876 case ESCO_LINK:
Frédéric Dalleau20714bf2012-11-21 10:51:12 +0100877 return sco_connect_ind(hdev, bdaddr, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Ulisses Furquim686ebf22011-12-21 10:11:33 -0200879 default:
880 BT_ERR("unknown link type %d", type);
881 return -EINVAL;
882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
885static inline void hci_proto_connect_cfm(struct hci_conn *conn, __u8 status)
886{
Ulisses Furquim686ebf22011-12-21 10:11:33 -0200887 switch (conn->type) {
888 case ACL_LINK:
889 case LE_LINK:
890 l2cap_connect_cfm(conn, status);
891 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Ulisses Furquim686ebf22011-12-21 10:11:33 -0200893 case SCO_LINK:
894 case ESCO_LINK:
895 sco_connect_cfm(conn, status);
896 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Ulisses Furquim686ebf22011-12-21 10:11:33 -0200898 default:
899 BT_ERR("unknown link type %d", conn->type);
900 break;
901 }
Johan Hedberge9a416b2011-02-19 12:05:56 -0300902
903 if (conn->connect_cfm_cb)
904 conn->connect_cfm_cb(conn, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
Marcel Holtmann2950f212009-02-12 14:02:50 +0100907static inline int hci_proto_disconn_ind(struct hci_conn *conn)
908{
Ulisses Furquim686ebf22011-12-21 10:11:33 -0200909 if (conn->type != ACL_LINK && conn->type != LE_LINK)
910 return HCI_ERROR_REMOTE_USER_TERM;
Marcel Holtmann2950f212009-02-12 14:02:50 +0100911
Ulisses Furquim686ebf22011-12-21 10:11:33 -0200912 return l2cap_disconn_ind(conn);
Marcel Holtmann2950f212009-02-12 14:02:50 +0100913}
914
915static inline void hci_proto_disconn_cfm(struct hci_conn *conn, __u8 reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Ulisses Furquim686ebf22011-12-21 10:11:33 -0200917 switch (conn->type) {
918 case ACL_LINK:
919 case LE_LINK:
920 l2cap_disconn_cfm(conn, reason);
921 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Ulisses Furquim686ebf22011-12-21 10:11:33 -0200923 case SCO_LINK:
924 case ESCO_LINK:
925 sco_disconn_cfm(conn, reason);
926 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Andrei Emeltchenkobd1eb662012-10-10 17:38:30 +0300928 /* L2CAP would be handled for BREDR chan */
929 case AMP_LINK:
930 break;
931
Ulisses Furquim686ebf22011-12-21 10:11:33 -0200932 default:
933 BT_ERR("unknown link type %d", conn->type);
934 break;
935 }
Johan Hedberge9a416b2011-02-19 12:05:56 -0300936
937 if (conn->disconn_cfm_cb)
938 conn->disconn_cfm_cb(conn, reason);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
941static inline void hci_proto_auth_cfm(struct hci_conn *conn, __u8 status)
942{
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100943 __u8 encrypt;
944
Ulisses Furquim686ebf22011-12-21 10:11:33 -0200945 if (conn->type != ACL_LINK && conn->type != LE_LINK)
946 return;
947
Johan Hedberg51a8efd2012-01-16 06:10:31 +0200948 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100949 return;
950
951 encrypt = (conn->link_mode & HCI_LM_ENCRYPT) ? 0x01 : 0x00;
Ulisses Furquim686ebf22011-12-21 10:11:33 -0200952 l2cap_security_cfm(conn, status, encrypt);
Johan Hedberge9a416b2011-02-19 12:05:56 -0300953
954 if (conn->security_cfm_cb)
955 conn->security_cfm_cb(conn, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
Waldemar Rymarkiewicz5a9d0a32011-06-07 11:18:06 +0200958static inline void hci_proto_encrypt_cfm(struct hci_conn *conn, __u8 status,
959 __u8 encrypt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Ulisses Furquim686ebf22011-12-21 10:11:33 -0200961 if (conn->type != ACL_LINK && conn->type != LE_LINK)
962 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Ulisses Furquim686ebf22011-12-21 10:11:33 -0200964 l2cap_security_cfm(conn, status, encrypt);
Johan Hedberge9a416b2011-02-19 12:05:56 -0300965
966 if (conn->security_cfm_cb)
967 conn->security_cfm_cb(conn, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970/* ----- HCI callbacks ----- */
971struct hci_cb {
972 struct list_head list;
973
974 char *name;
975
Waldemar Rymarkiewicz5a9d0a32011-06-07 11:18:06 +0200976 void (*security_cfm) (struct hci_conn *conn, __u8 status,
977 __u8 encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 void (*key_change_cfm) (struct hci_conn *conn, __u8 status);
979 void (*role_switch_cfm) (struct hci_conn *conn, __u8 status, __u8 role);
980};
981
982static inline void hci_auth_cfm(struct hci_conn *conn, __u8 status)
983{
Denis Kirjanov711584e2012-10-22 17:22:01 +0400984 struct hci_cb *cb;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100985 __u8 encrypt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 hci_proto_auth_cfm(conn, status);
988
Johan Hedberg51a8efd2012-01-16 06:10:31 +0200989 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100990 return;
991
992 encrypt = (conn->link_mode & HCI_LM_ENCRYPT) ? 0x01 : 0x00;
993
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -0200994 read_lock(&hci_cb_list_lock);
Denis Kirjanov711584e2012-10-22 17:22:01 +0400995 list_for_each_entry(cb, &hci_cb_list, list) {
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100996 if (cb->security_cfm)
997 cb->security_cfm(conn, status, encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -0200999 read_unlock(&hci_cb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
Waldemar Rymarkiewicz5a9d0a32011-06-07 11:18:06 +02001002static inline void hci_encrypt_cfm(struct hci_conn *conn, __u8 status,
1003 __u8 encrypt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Denis Kirjanov711584e2012-10-22 17:22:01 +04001005 struct hci_cb *cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Marcel Holtmann435fef22009-02-09 03:55:28 +01001007 if (conn->sec_level == BT_SECURITY_SDP)
1008 conn->sec_level = BT_SECURITY_LOW;
1009
Vinicius Costa Gomes88167ae2011-06-09 18:50:51 -03001010 if (conn->pending_sec_level > conn->sec_level)
1011 conn->sec_level = conn->pending_sec_level;
1012
Marcel Holtmann9719f8a2008-07-14 20:13:45 +02001013 hci_proto_encrypt_cfm(conn, status, encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02001015 read_lock(&hci_cb_list_lock);
Denis Kirjanov711584e2012-10-22 17:22:01 +04001016 list_for_each_entry(cb, &hci_cb_list, list) {
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01001017 if (cb->security_cfm)
1018 cb->security_cfm(conn, status, encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02001020 read_unlock(&hci_cb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
1023static inline void hci_key_change_cfm(struct hci_conn *conn, __u8 status)
1024{
Denis Kirjanov711584e2012-10-22 17:22:01 +04001025 struct hci_cb *cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02001027 read_lock(&hci_cb_list_lock);
Denis Kirjanov711584e2012-10-22 17:22:01 +04001028 list_for_each_entry(cb, &hci_cb_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (cb->key_change_cfm)
1030 cb->key_change_cfm(conn, status);
1031 }
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02001032 read_unlock(&hci_cb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
Waldemar Rymarkiewicz5a9d0a32011-06-07 11:18:06 +02001035static inline void hci_role_switch_cfm(struct hci_conn *conn, __u8 status,
1036 __u8 role)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Denis Kirjanov711584e2012-10-22 17:22:01 +04001038 struct hci_cb *cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02001040 read_lock(&hci_cb_list_lock);
Denis Kirjanov711584e2012-10-22 17:22:01 +04001041 list_for_each_entry(cb, &hci_cb_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (cb->role_switch_cfm)
1043 cb->role_switch_cfm(conn, status, role);
1044 }
Gustavo F. Padovanf20d09d2011-12-22 16:30:27 -02001045 read_unlock(&hci_cb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047
Johan Hedberg6759a672012-01-15 20:51:14 +02001048static inline bool eir_has_data_type(u8 *data, size_t data_len, u8 type)
1049{
Johan Hedberg84d9d072012-03-26 14:21:41 +03001050 size_t parsed = 0;
Johan Hedberg6759a672012-01-15 20:51:14 +02001051
Johan Hedberg6c0c3312012-03-26 14:21:42 +03001052 if (data_len < 2)
1053 return false;
1054
Johan Hedberg84d9d072012-03-26 14:21:41 +03001055 while (parsed < data_len - 1) {
1056 u8 field_len = data[0];
Johan Hedberg6759a672012-01-15 20:51:14 +02001057
1058 if (field_len == 0)
1059 break;
1060
1061 parsed += field_len + 1;
1062
1063 if (parsed > data_len)
1064 break;
1065
1066 if (data[1] == type)
1067 return true;
1068
1069 data += field_len + 1;
1070 }
1071
1072 return false;
1073}
1074
Johan Hedberg301cb2d2014-02-18 10:19:34 +02001075static inline bool hci_bdaddr_is_rpa(bdaddr_t *bdaddr, u8 addr_type)
1076{
1077 if (addr_type != 0x01)
1078 return false;
1079
1080 if ((bdaddr->b[5] & 0xc0) == 0x40)
1081 return true;
1082
1083 return false;
1084}
1085
Johan Hedberg2426f3a2014-02-18 17:14:36 +02001086static inline struct smp_irk *hci_get_irk(struct hci_dev *hdev,
1087 bdaddr_t *bdaddr, u8 addr_type)
1088{
1089 if (!hci_bdaddr_is_rpa(bdaddr, addr_type))
1090 return NULL;
1091
1092 return hci_find_irk_by_rpa(hdev, bdaddr);
1093}
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095int hci_register_cb(struct hci_cb *hcb);
1096int hci_unregister_cb(struct hci_cb *hcb);
1097
Johan Hedberg3119ae92013-03-05 20:37:44 +02001098struct hci_request {
1099 struct hci_dev *hdev;
1100 struct sk_buff_head cmd_q;
Andre Guedes5d73e032013-03-08 11:20:16 -03001101
1102 /* If something goes wrong when building the HCI request, the error
1103 * value is stored in this field.
1104 */
1105 int err;
Johan Hedberg3119ae92013-03-05 20:37:44 +02001106};
1107
1108void hci_req_init(struct hci_request *req, struct hci_dev *hdev);
1109int hci_req_run(struct hci_request *req, hci_req_complete_t complete);
Johan Hedberg07dc93d2013-04-19 10:14:51 +03001110void hci_req_add(struct hci_request *req, u16 opcode, u32 plen,
1111 const void *param);
1112void hci_req_add_ev(struct hci_request *req, u16 opcode, u32 plen,
1113 const void *param, u8 event);
Johan Hedberg9238f362013-03-05 20:37:48 +02001114void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status);
Johan Hedberg3119ae92013-03-05 20:37:44 +02001115
Johan Hedberg75e84b72013-04-02 13:35:04 +03001116struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
Johan Hedberg07dc93d2013-04-19 10:14:51 +03001117 const void *param, u32 timeout);
Johan Hedberg7b1abbb2013-04-03 21:54:47 +03001118struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
Johan Hedberg07dc93d2013-04-19 10:14:51 +03001119 const void *param, u8 event, u32 timeout);
Johan Hedberg75e84b72013-04-02 13:35:04 +03001120
Johan Hedberg07dc93d2013-04-19 10:14:51 +03001121int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen,
1122 const void *param);
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02001123void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags);
Gustavo F. Padovan0d861d82010-05-01 16:15:35 -03001124void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001126void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128/* ----- HCI Sockets ----- */
Marcel Holtmann470fe1b2012-02-20 14:50:30 +01001129void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb);
1130void hci_send_to_control(struct sk_buff *skb, struct sock *skip_sk);
Marcel Holtmanncd82e612012-02-20 20:34:38 +01001131void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Marcel Holtmann040030e2012-02-20 14:50:37 +01001133void hci_sock_dev_event(struct hci_dev *hdev, int event);
1134
Johan Hedberg03811012010-12-08 00:21:06 +02001135/* Management interface */
Andre Guedes591f47f2012-04-24 21:02:49 -03001136#define DISCOV_TYPE_BREDR (BIT(BDADDR_BREDR))
1137#define DISCOV_TYPE_LE (BIT(BDADDR_LE_PUBLIC) | \
1138 BIT(BDADDR_LE_RANDOM))
1139#define DISCOV_TYPE_INTERLEAVED (BIT(BDADDR_BREDR) | \
1140 BIT(BDADDR_LE_PUBLIC) | \
1141 BIT(BDADDR_LE_RANDOM))
Andre Guedesf39799f2012-02-17 20:39:35 -03001142
Andre Guedes0d8cc932013-04-30 15:29:31 -03001143/* These LE scan and inquiry parameters were chosen according to LE General
1144 * Discovery Procedure specification.
1145 */
1146#define DISCOV_LE_SCAN_WIN 0x12
1147#define DISCOV_LE_SCAN_INT 0x12
1148#define DISCOV_LE_TIMEOUT msecs_to_jiffies(10240)
1149#define DISCOV_INTERLEAVED_TIMEOUT msecs_to_jiffies(5120)
1150#define DISCOV_INTERLEAVED_INQUIRY_LEN 0x04
1151#define DISCOV_BREDR_INQUIRY_LEN 0x08
1152
Johan Hedberg03811012010-12-08 00:21:06 +02001153int mgmt_control(struct sock *sk, struct msghdr *msg, size_t len);
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07001154void mgmt_index_added(struct hci_dev *hdev);
1155void mgmt_index_removed(struct hci_dev *hdev);
Marcel Holtmann3eec7052013-10-06 23:55:46 -07001156void mgmt_set_powered_failed(struct hci_dev *hdev, int err);
Johan Hedberg744cf192011-11-08 20:40:14 +02001157int mgmt_powered(struct hci_dev *hdev, u8 powered);
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07001158void mgmt_discoverable_timeout(struct hci_dev *hdev);
Marcel Holtmann86a75642013-10-15 06:33:54 -07001159void mgmt_discoverable(struct hci_dev *hdev, u8 discoverable);
Marcel Holtmanna3309162013-10-15 06:33:55 -07001160void mgmt_connectable(struct hci_dev *hdev, u8 connectable);
Marcel Holtmann4796e8a2013-10-15 06:33:56 -07001161void mgmt_write_scan_failed(struct hci_dev *hdev, u8 scan, u8 status);
Marcel Holtmanndc4a5ee2013-10-15 10:15:57 -07001162void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
1163 bool persistent);
Marcel Holtmannecd90ae2013-10-06 23:55:49 -07001164void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
1165 u8 addr_type, u32 flags, u8 *name, u8 name_len,
1166 u8 *dev_class);
Marcel Holtmann9b80ec52013-10-06 23:55:50 -07001167void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
1168 u8 link_type, u8 addr_type, u8 reason);
Marcel Holtmann78929242013-10-06 23:55:47 -07001169void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
1170 u8 link_type, u8 addr_type, u8 status);
Marcel Holtmann445608d2013-10-06 23:55:48 -07001171void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
1172 u8 addr_type, u8 status);
Marcel Holtmannce0e4a02013-10-15 14:26:20 -07001173void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure);
Marcel Holtmanne669cf82013-10-15 14:26:21 -07001174void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1175 u8 status);
Marcel Holtmann3eb38522013-10-15 14:26:22 -07001176void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1177 u8 status);
Johan Hedberg744cf192011-11-08 20:40:14 +02001178int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001179 u8 link_type, u8 addr_type, __le32 value,
1180 u8 confirm_hint);
Johan Hedberg744cf192011-11-08 20:40:14 +02001181int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001182 u8 link_type, u8 addr_type, u8 status);
Johan Hedberg272d90d2012-02-09 15:26:12 +02001183int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001184 u8 link_type, u8 addr_type, u8 status);
Johan Hedberg272d90d2012-02-09 15:26:12 +02001185int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001186 u8 link_type, u8 addr_type);
Brian Gix604086b2011-11-23 08:28:33 -08001187int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001188 u8 link_type, u8 addr_type, u8 status);
Johan Hedberg272d90d2012-02-09 15:26:12 +02001189int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001190 u8 link_type, u8 addr_type, u8 status);
Johan Hedberg92a25252012-09-06 18:39:26 +03001191int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
1192 u8 link_type, u8 addr_type, u32 passkey,
1193 u8 entered);
Marcel Holtmanne5460992013-10-15 14:26:23 -07001194void mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
1195 u8 addr_type, u8 status);
Marcel Holtmann464996a2013-10-15 14:26:24 -07001196void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status);
Marcel Holtmann3e248562013-10-15 14:26:25 -07001197void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08001198void mgmt_sc_enable_complete(struct hci_dev *hdev, u8 enable, u8 status);
Marcel Holtmann4e1b0242013-10-15 14:26:26 -07001199void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
1200 u8 status);
Marcel Holtmann7667da32013-10-15 14:26:27 -07001201void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status);
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08001202void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192,
1203 u8 *randomizer192, u8 *hash256,
1204 u8 *randomizer256, u8 status);
Marcel Holtmann901801b2013-10-06 23:55:51 -07001205void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
1206 u8 addr_type, u8 *dev_class, s8 rssi, u8 cfm_name,
1207 u8 ssp, u8 *eir, u16 eir_len);
Marcel Holtmann9cf12ae2013-10-06 23:55:52 -07001208void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
1209 u8 addr_type, s8 rssi, u8 *name, u8 name_len);
Marcel Holtmann2f1e0632013-10-06 23:55:53 -07001210void mgmt_discovering(struct hci_dev *hdev, u8 discovering);
Johan Hedberg88c1fe42012-02-09 15:56:11 +02001211int mgmt_device_blocked(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
1212int mgmt_device_unblocked(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
Johan Hedbergba74b662014-02-19 14:57:45 +02001213void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key);
Johan Hedberg95fbac82014-02-19 15:18:31 +02001214void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk);
Marcel Holtmann5976e602013-10-06 04:08:14 -07001215void mgmt_reenable_advertising(struct hci_dev *hdev);
Johan Hedbergf4a407b2014-02-18 21:41:34 +02001216void mgmt_smp_complete(struct hci_conn *conn, bool complete);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03001217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218/* HCI info for socket */
1219#define hci_pi(sk) ((struct hci_pinfo *) sk)
1220
1221struct hci_pinfo {
1222 struct bt_sock bt;
1223 struct hci_dev *hdev;
1224 struct hci_filter filter;
1225 __u32 cmsg_mask;
Johan Hedbergc02178d2010-12-08 00:21:05 +02001226 unsigned short channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227};
1228
1229/* HCI security filter */
1230#define HCI_SFLT_MAX_OGF 5
1231
1232struct hci_sec_filter {
1233 __u32 type_mask;
1234 __u32 event_mask[2];
1235 __u32 ocf_mask[HCI_SFLT_MAX_OGF + 1][4];
1236};
1237
1238/* ----- HCI requests ----- */
1239#define HCI_REQ_DONE 0
1240#define HCI_REQ_PEND 1
1241#define HCI_REQ_CANCELED 2
1242
Thomas Gleixnera6a67ef2009-07-26 08:18:19 +00001243#define hci_req_lock(d) mutex_lock(&d->req_lock)
1244#define hci_req_unlock(d) mutex_unlock(&d->req_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02001246void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max,
1247 u16 latency, u16 to_multiplier);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03001248void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8],
1249 __u8 ltk[16]);
Andre Guedes2519a1f2011-11-07 11:45:24 -03001250
Frédéric Dalleau5d4d62f2013-08-19 14:23:57 +02001251#define SCO_AIRMODE_MASK 0x0003
1252#define SCO_AIRMODE_CVSD 0x0000
1253#define SCO_AIRMODE_TRANSP 0x0003
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255#endif /* __HCI_CORE_H */