]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/net/bluetooth/hci_core.h
Bluetooth: Add specific connection parameter clear functions
[mirror_ubuntu-bionic-kernel.git] / include / net / bluetooth / hci_core.h
CommitLineData
04fafe4e 1/*
1da177e4 2 BlueZ - Bluetooth protocol stack for Linux
2d0a0346 3 Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
1da177e4
LT
4
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
04fafe4e
RS
15 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
1da177e4
LT
18 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
04fafe4e
RS
20 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
1da177e4
LT
22 SOFTWARE IS DISCLAIMED.
23*/
24
25#ifndef __HCI_CORE_H
26#define __HCI_CORE_H
27
1da177e4
LT
28#include <net/bluetooth/hci.h>
29
5e59b791
LAD
30/* HCI priority */
31#define HCI_PRIO_MAX 7
32
1da177e4 33/* HCI Core structures */
1da177e4
LT
34struct 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];
1ebb9252 40 __le16 clock_offset;
1da177e4 41 __s8 rssi;
41a96212 42 __u8 ssp_mode;
1da177e4
LT
43};
44
45struct inquiry_entry {
561aafbc
JH
46 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;
1da177e4
LT
54 __u32 timestamp;
55 struct inquiry_data data;
56};
57
30883512 58struct discovery_state {
f64b993f 59 int type;
ff9ef578
JH
60 enum {
61 DISCOVERY_STOPPED,
62 DISCOVERY_STARTING,
343f935b 63 DISCOVERY_FINDING,
30dc78e1 64 DISCOVERY_RESOLVING,
ff9ef578
JH
65 DISCOVERY_STOPPING,
66 } state;
c3c7ea65 67 struct list_head all; /* All devices found during inquiry */
f64b993f
GP
68 struct list_head unknown; /* Name state not known */
69 struct list_head resolve; /* Name needs to be resolved */
70 __u32 timestamp;
b9a6328f
JH
71 bdaddr_t last_adv_addr;
72 u8 last_adv_addr_type;
ff5cd29f 73 s8 last_adv_rssi;
c70a7e4c 74 u32 last_adv_flags;
b9a6328f
JH
75 u8 last_adv_data[HCI_MAX_AD_LENGTH];
76 u8 last_adv_data_len;
1da177e4
LT
77};
78
79struct hci_conn_hash {
80 struct list_head list;
1da177e4 81 unsigned int acl_num;
bd1eb66b 82 unsigned int amp_num;
1da177e4 83 unsigned int sco_num;
fcd89c09 84 unsigned int le_num;
1da177e4
LT
85};
86
f0358568
JH
87struct bdaddr_list {
88 struct list_head list;
89 bdaddr_t bdaddr;
b9ee0a78 90 u8 bdaddr_type;
f0358568 91};
2aeb9a1a
JH
92
93struct bt_uuid {
94 struct list_head list;
95 u8 uuid[16];
83be8eca 96 u8 size;
1aff6f09 97 u8 svc_hint;
2aeb9a1a
JH
98};
99
7ee4ea36
MH
100struct smp_csrk {
101 bdaddr_t bdaddr;
102 u8 bdaddr_type;
103 u8 master;
104 u8 val[16];
105};
106
b899efaf
VCG
107struct smp_ltk {
108 struct list_head list;
109 bdaddr_t bdaddr;
110 u8 bdaddr_type;
111 u8 authenticated;
112 u8 type;
113 u8 enc_size;
114 __le16 ediv;
fe39c7b2 115 __le64 rand;
b899efaf 116 u8 val[16];
03c515d7 117};
b899efaf 118
970c4e46
JH
119struct smp_irk {
120 struct list_head list;
121 bdaddr_t rpa;
122 bdaddr_t bdaddr;
123 u8 addr_type;
124 u8 val[16];
125};
126
55ed8ca1
JH
127struct link_key {
128 struct list_head list;
129 bdaddr_t bdaddr;
130 u8 type;
9b3b4460 131 u8 val[HCI_LINK_KEY_SIZE];
55ed8ca1
JH
132 u8 pin_len;
133};
134
2763eda6
SJ
135struct oob_data {
136 struct list_head list;
137 bdaddr_t bdaddr;
519ca9d0
MH
138 u8 hash192[16];
139 u8 randomizer192[16];
140 u8 hash256[16];
141 u8 randomizer256[16];
2763eda6
SJ
142};
143
490c5bab
JH
144#define HCI_MAX_SHORT_NAME_LENGTH 10
145
d6bfd59c
JH
146/* Default LE RPA expiry time, 15 minutes */
147#define HCI_DEFAULT_RPA_TIMEOUT (15 * 60)
148
31ad1691
AK
149/* Default min/max age of connection information (1s/3s) */
150#define DEFAULT_CONN_INFO_MIN_AGE 1000
151#define DEFAULT_CONN_INFO_MAX_AGE 3000
152
903e4541
AE
153struct amp_assoc {
154 __u16 len;
155 __u16 offset;
93c284ee
AE
156 __u16 rem_len;
157 __u16 len_so_far;
903e4541
AE
158 __u8 data[HCI_MAX_AMP_ASSOC_SIZE];
159};
160
d2c5d77f 161#define HCI_MAX_PAGES 3
cad718ed 162
cd4c5391 163#define NUM_REASSEMBLY 4
1da177e4
LT
164struct hci_dev {
165 struct list_head list;
09fd0de5 166 struct mutex lock;
1da177e4
LT
167
168 char name[8];
169 unsigned long flags;
170 __u16 id;
c13854ce 171 __u8 bus;
943da25d 172 __u8 dev_type;
1da177e4 173 bdaddr_t bdaddr;
24c457e2 174 bdaddr_t public_addr;
7a4cd51d 175 bdaddr_t random_addr;
d13eafce 176 bdaddr_t static_addr;
56ed2cb8 177 __u8 adv_addr_type;
1f6c6378 178 __u8 dev_name[HCI_MAX_NAME_LENGTH];
490c5bab 179 __u8 short_name[HCI_MAX_SHORT_NAME_LENGTH];
80a1e1db 180 __u8 eir[HCI_MAX_EIR_LENGTH];
a9de9248 181 __u8 dev_class[3];
1aff6f09
JH
182 __u8 major_class;
183 __u8 minor_class;
d2c5d77f 184 __u8 max_page;
cad718ed 185 __u8 features[HCI_MAX_PAGES][8];
60e77321 186 __u8 le_features[8];
cf1d081f 187 __u8 le_white_list_size;
9b008c04 188 __u8 le_states[8];
a9de9248 189 __u8 commands[64];
1143e5a6
MH
190 __u8 hci_ver;
191 __u16 hci_rev;
d5859e22 192 __u8 lmp_ver;
1143e5a6 193 __u16 manufacturer;
7d69230c 194 __u16 lmp_subver;
1da177e4 195 __u16 voice_setting;
b4cb9fb2 196 __u8 num_iac;
17fa4b9d 197 __u8 io_capability;
91c4e9b1 198 __s8 inq_tx_power;
f332ec66
JH
199 __u16 page_scan_interval;
200 __u16 page_scan_window;
201 __u8 page_scan_type;
3f959d46 202 __u8 le_adv_channel_map;
533553f8 203 __u8 le_scan_type;
bef64738
MH
204 __u16 le_scan_interval;
205 __u16 le_scan_window;
4e70c7e7
MH
206 __u16 le_conn_min_interval;
207 __u16 le_conn_max_interval;
04fb7d90
MH
208 __u16 le_conn_latency;
209 __u16 le_supv_timeout;
b9a7a61e 210 __u16 discov_interleaved_timeout;
31ad1691
AK
211 __u16 conn_info_min_age;
212 __u16 conn_info_max_age;
06f5b778 213 __u8 ssp_debug_mode;
33f35721 214 __u32 clock;
f332ec66 215
2b9be137
MH
216 __u16 devid_source;
217 __u16 devid_vendor;
218 __u16 devid_product;
219 __u16 devid_version;
1da177e4
LT
220
221 __u16 pkt_type;
5b7f9909 222 __u16 esco_type;
1da177e4
LT
223 __u16 link_policy;
224 __u16 link_mode;
225
04837f64
MH
226 __u32 idle_timeout;
227 __u16 sniff_min_interval;
228 __u16 sniff_max_interval;
229
928abaa7
AE
230 __u8 amp_status;
231 __u32 amp_total_bw;
232 __u32 amp_max_bw;
233 __u32 amp_min_latency;
234 __u32 amp_max_pdu;
235 __u8 amp_type;
236 __u16 amp_pal_cap;
237 __u16 amp_assoc_size;
238 __u32 amp_max_flush_to;
239 __u32 amp_be_flush_to;
240
903e4541
AE
241 struct amp_assoc loc_assoc;
242
1e89cffb
AE
243 __u8 flow_ctl_mode;
244
9f61656a
JH
245 unsigned int auto_accept_delay;
246
1da177e4
LT
247 unsigned long quirks;
248
249 atomic_t cmd_cnt;
250 unsigned int acl_cnt;
251 unsigned int sco_cnt;
6ed58ec5 252 unsigned int le_cnt;
1da177e4
LT
253
254 unsigned int acl_mtu;
255 unsigned int sco_mtu;
6ed58ec5 256 unsigned int le_mtu;
1da177e4
LT
257 unsigned int acl_pkts;
258 unsigned int sco_pkts;
6ed58ec5 259 unsigned int le_pkts;
1da177e4 260
350ee4cf
AE
261 __u16 block_len;
262 __u16 block_mtu;
263 __u16 num_blocks;
264 __u16 block_cnt;
265
1da177e4
LT
266 unsigned long acl_last_tx;
267 unsigned long sco_last_tx;
6ed58ec5 268 unsigned long le_last_tx;
1da177e4 269
f48fd9c8 270 struct workqueue_struct *workqueue;
6ead1bbc 271 struct workqueue_struct *req_workqueue;
f48fd9c8 272
ab81cbf9 273 struct work_struct power_on;
3243553f 274 struct delayed_work power_off;
ab81cbf9 275
16ab91ab
JH
276 __u16 discov_timeout;
277 struct delayed_work discov_off;
278
7d78525d
JH
279 struct delayed_work service_cache;
280
65cc2b49 281 struct delayed_work cmd_timer;
b78752cc
MH
282
283 struct work_struct rx_work;
c347b765 284 struct work_struct cmd_work;
3eff45ea 285 struct work_struct tx_work;
1da177e4
LT
286
287 struct sk_buff_head rx_q;
288 struct sk_buff_head raw_q;
289 struct sk_buff_head cmd_q;
290
b6ddb638 291 struct sk_buff *recv_evt;
1da177e4 292 struct sk_buff *sent_cmd;
cd4c5391 293 struct sk_buff *reassembly[NUM_REASSEMBLY];
1da177e4 294
a6a67efd 295 struct mutex req_lock;
1da177e4
LT
296 wait_queue_head_t req_wait_q;
297 __u32 req_status;
298 __u32 req_result;
a5040efa 299
99780a7b 300 struct crypto_blkcipher *tfm_aes;
2e58ef3e 301
30883512 302 struct discovery_state discovery;
1da177e4
LT
303 struct hci_conn_hash conn_hash;
304
5c136e90
AG
305 struct list_head mgmt_pending;
306 struct list_head blacklist;
2aeb9a1a 307 struct list_head uuids;
55ed8ca1 308 struct list_head link_keys;
b899efaf 309 struct list_head long_term_keys;
970c4e46 310 struct list_head identity_resolving_keys;
2763eda6 311 struct list_head remote_oob_data;
d2ab0ac1 312 struct list_head le_white_list;
15819a70 313 struct list_head le_conn_params;
77a77a30 314 struct list_head pend_le_conns;
2763eda6 315
1da177e4
LT
316 struct hci_dev_stats stat;
317
70f23020 318 atomic_t promisc;
1da177e4 319
ca325f69
MH
320 struct dentry *debugfs;
321
a91f2e39 322 struct device dev;
1da177e4 323
611b30f7
MH
324 struct rfkill *rfkill;
325
111902f7 326 unsigned long dbg_flags;
d23264a8
AG
327 unsigned long dev_flags;
328
7ba8b4be
AG
329 struct delayed_work le_scan_disable;
330
8fa19098 331 __s8 adv_tx_power;
3f0f524b
JH
332 __u8 adv_data[HCI_MAX_AD_LENGTH];
333 __u8 adv_data_len;
f8e808bd
MH
334 __u8 scan_rsp_data[HCI_MAX_AD_LENGTH];
335 __u8 scan_rsp_data_len;
8fa19098 336
863efaf2 337 __u8 irk[16];
d6bfd59c
JH
338 __u32 rpa_timeout;
339 struct delayed_work rpa_expired;
2b5224dc 340 bdaddr_t rpa;
863efaf2 341
1da177e4
LT
342 int (*open)(struct hci_dev *hdev);
343 int (*close)(struct hci_dev *hdev);
344 int (*flush)(struct hci_dev *hdev);
f41c70c4 345 int (*setup)(struct hci_dev *hdev);
7bd8f09f 346 int (*send)(struct hci_dev *hdev, struct sk_buff *skb);
1da177e4 347 void (*notify)(struct hci_dev *hdev, unsigned int evt);
24c457e2 348 int (*set_bdaddr)(struct hci_dev *hdev, const bdaddr_t *bdaddr);
1da177e4
LT
349};
350
53502d69
AE
351#define HCI_PHY_HANDLE(handle) (handle & 0xff)
352
1da177e4
LT
353struct hci_conn {
354 struct list_head list;
355
adc4266d 356 atomic_t refcnt;
adc4266d
SJ
357
358 bdaddr_t dst;
5a9d0a3f 359 __u8 dst_type;
662e8820 360 bdaddr_t src;
e7c4096e 361 __u8 src_type;
cb1d68f7
JH
362 bdaddr_t init_addr;
363 __u8 init_addr_type;
364 bdaddr_t resp_addr;
365 __u8 resp_addr_type;
adc4266d
SJ
366 __u16 handle;
367 __u16 state;
368 __u8 mode;
369 __u8 type;
a0c808b3 370 bool out;
adc4266d
SJ
371 __u8 attempt;
372 __u8 dev_class[3];
cad718ed 373 __u8 features[HCI_MAX_PAGES][8];
adc4266d
SJ
374 __u16 pkt_type;
375 __u16 link_policy;
13d39315 376 __u8 key_type;
adc4266d
SJ
377 __u8 auth_type;
378 __u8 sec_level;
379 __u8 pending_sec_level;
380 __u8 pin_length;
726b4ffc 381 __u8 enc_key_size;
adc4266d 382 __u8 io_capability;
92a25256
JH
383 __u32 passkey_notify;
384 __u8 passkey_entered;
adc4266d 385 __u16 disc_timeout;
10c62ddc 386 __u16 setting;
1e406eef
AG
387 __u16 le_conn_min_interval;
388 __u16 le_conn_max_interval;
e04fde60
MH
389 __u16 le_conn_interval;
390 __u16 le_conn_latency;
391 __u16 le_supv_timeout;
5ae76a94 392 __s8 rssi;
5a134fae 393 __s8 tx_power;
d0455ed9 394 __s8 max_tx_power;
51a8efd7 395 unsigned long flags;
04837f64 396
33f35721
JH
397 __u32 clock;
398 __u16 clock_accuracy;
399
dd983808
AK
400 unsigned long conn_info_timestamp;
401
03b555e1 402 __u8 remote_cap;
03b555e1 403 __u8 remote_auth;
3161ae1c 404 __u8 remote_id;
03b555e1 405
adc4266d 406 unsigned int sent;
04837f64 407
1da177e4 408 struct sk_buff_head data_q;
2c33c06a 409 struct list_head chan_list;
1da177e4 410
19c40e3b 411 struct delayed_work disc_work;
7bc18d9d 412 struct delayed_work auto_accept_work;
a74a84f6 413 struct delayed_work idle_work;
9489eca4 414 struct delayed_work le_conn_timeout;
04837f64 415
b219e3ac
MH
416 struct device dev;
417
1da177e4
LT
418 struct hci_dev *hdev;
419 void *l2cap_data;
420 void *sco_data;
9740e49d 421 struct amp_mgr *amp_mgr;
1da177e4
LT
422
423 struct hci_conn *link;
e9a416b5
JH
424
425 void (*connect_cfm_cb) (struct hci_conn *conn, u8 status);
426 void (*security_cfm_cb) (struct hci_conn *conn, u8 status);
427 void (*disconn_cfm_cb) (struct hci_conn *conn, u8 reason);
1da177e4
LT
428};
429
73d80deb
LAD
430struct hci_chan {
431 struct list_head list;
42c4e53e 432 __u16 handle;
73d80deb
LAD
433 struct hci_conn *conn;
434 struct sk_buff_head data_q;
435 unsigned int sent;
168df8e5 436 __u8 state;
73d80deb
LAD
437};
438
15819a70
AG
439struct hci_conn_params {
440 struct list_head list;
441
442 bdaddr_t addr;
443 u8 addr_type;
444
445 u16 conn_min_interval;
446 u16 conn_max_interval;
f044eb05
MH
447 u16 conn_latency;
448 u16 supervision_timeout;
9fcb18ef
AG
449
450 enum {
451 HCI_AUTO_CONN_DISABLED,
452 HCI_AUTO_CONN_ALWAYS,
453 HCI_AUTO_CONN_LINK_LOSS,
454 } auto_connect;
15819a70
AG
455};
456
1da177e4
LT
457extern struct list_head hci_dev_list;
458extern struct list_head hci_cb_list;
459extern rwlock_t hci_dev_list_lock;
460extern rwlock_t hci_cb_list_lock;
461
686ebf28 462/* ----- HCI interface to upper protocols ----- */
e74e58f8
JP
463int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr);
464void l2cap_connect_cfm(struct hci_conn *hcon, u8 status);
465int l2cap_disconn_ind(struct hci_conn *hcon);
466void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason);
467int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt);
468int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags);
469
470int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags);
471void sco_connect_cfm(struct hci_conn *hcon, __u8 status);
472void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason);
473int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb);
686ebf28 474
1da177e4 475/* ----- Inquiry cache ----- */
70f23020
AE
476#define INQUIRY_CACHE_AGE_MAX (HZ*30) /* 30 seconds */
477#define INQUIRY_ENTRY_AGE_MAX (HZ*60) /* 60 seconds */
1da177e4 478
30883512 479static inline void discovery_init(struct hci_dev *hdev)
1da177e4 480{
ff9ef578 481 hdev->discovery.state = DISCOVERY_STOPPED;
30883512
JH
482 INIT_LIST_HEAD(&hdev->discovery.all);
483 INIT_LIST_HEAD(&hdev->discovery.unknown);
484 INIT_LIST_HEAD(&hdev->discovery.resolve);
1da177e4
LT
485}
486
30dc78e1
JH
487bool hci_discovery_active(struct hci_dev *hdev);
488
ff9ef578
JH
489void hci_discovery_set_state(struct hci_dev *hdev, int state);
490
1da177e4
LT
491static inline int inquiry_cache_empty(struct hci_dev *hdev)
492{
30883512 493 return list_empty(&hdev->discovery.all);
1da177e4
LT
494}
495
496static inline long inquiry_cache_age(struct hci_dev *hdev)
497{
30883512 498 struct discovery_state *c = &hdev->discovery;
1da177e4
LT
499 return jiffies - c->timestamp;
500}
501
502static inline long inquiry_entry_age(struct inquiry_entry *e)
503{
504 return jiffies - e->timestamp;
505}
506
5a9d0a3f 507struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev,
04124681 508 bdaddr_t *bdaddr);
561aafbc 509struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
04124681 510 bdaddr_t *bdaddr);
30dc78e1 511struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev,
04124681
GP
512 bdaddr_t *bdaddr,
513 int state);
a3d4e20a 514void hci_inquiry_cache_update_resolve(struct hci_dev *hdev,
04124681 515 struct inquiry_entry *ie);
af58925c
MH
516u32 hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
517 bool name_known);
1f9b9a5d 518void hci_inquiry_cache_flush(struct hci_dev *hdev);
1da177e4
LT
519
520/* ----- HCI Connections ----- */
521enum {
522 HCI_CONN_AUTH_PEND,
19f8def0 523 HCI_CONN_REAUTH_PEND,
1da177e4 524 HCI_CONN_ENCRYPT_PEND,
04837f64
MH
525 HCI_CONN_RSWITCH_PEND,
526 HCI_CONN_MODE_CHANGE_PEND,
e73439d8 527 HCI_CONN_SCO_SETUP_PEND,
d26a2345 528 HCI_CONN_LE_SMP_PEND,
b644ba33 529 HCI_CONN_MGMT_CONNECTED,
58a681ef 530 HCI_CONN_SSP_ENABLED,
eb9a8f3f 531 HCI_CONN_SC_ENABLED,
abf76bad 532 HCI_CONN_AES_CCM,
58a681ef
JH
533 HCI_CONN_POWER_SAVE,
534 HCI_CONN_REMOTE_OOB,
af6a9c32 535 HCI_CONN_FLUSH_KEY,
4dae2798
JH
536 HCI_CONN_MASTER,
537 HCI_CONN_ENCRYPT,
538 HCI_CONN_AUTH,
539 HCI_CONN_SECURE,
540 HCI_CONN_FIPS,
fe59a05f 541 HCI_CONN_STK_ENCRYPT,
1da177e4
LT
542};
543
aa64a8b5
JH
544static inline bool hci_conn_ssp_enabled(struct hci_conn *conn)
545{
546 struct hci_dev *hdev = conn->hdev;
c3c7ea65
GP
547 return test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) &&
548 test_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
aa64a8b5
JH
549}
550
eb9a8f3f
MH
551static inline bool hci_conn_sc_enabled(struct hci_conn *conn)
552{
553 struct hci_dev *hdev = conn->hdev;
554 return test_bit(HCI_SC_ENABLED, &hdev->dev_flags) &&
555 test_bit(HCI_CONN_SC_ENABLED, &conn->flags);
556}
557
1da177e4
LT
558static inline void hci_conn_hash_add(struct hci_dev *hdev, struct hci_conn *c)
559{
560 struct hci_conn_hash *h = &hdev->conn_hash;
bf4c6325 561 list_add_rcu(&c->list, &h->list);
fcd89c09
VT
562 switch (c->type) {
563 case ACL_LINK:
1da177e4 564 h->acl_num++;
fcd89c09 565 break;
bd1eb66b
AE
566 case AMP_LINK:
567 h->amp_num++;
568 break;
fcd89c09
VT
569 case LE_LINK:
570 h->le_num++;
571 break;
572 case SCO_LINK:
573 case ESCO_LINK:
1da177e4 574 h->sco_num++;
fcd89c09
VT
575 break;
576 }
1da177e4
LT
577}
578
579static inline void hci_conn_hash_del(struct hci_dev *hdev, struct hci_conn *c)
580{
581 struct hci_conn_hash *h = &hdev->conn_hash;
bf4c6325
GP
582
583 list_del_rcu(&c->list);
584 synchronize_rcu();
585
fcd89c09
VT
586 switch (c->type) {
587 case ACL_LINK:
1da177e4 588 h->acl_num--;
fcd89c09 589 break;
bd1eb66b
AE
590 case AMP_LINK:
591 h->amp_num--;
592 break;
fcd89c09
VT
593 case LE_LINK:
594 h->le_num--;
595 break;
596 case SCO_LINK:
597 case ESCO_LINK:
1da177e4 598 h->sco_num--;
fcd89c09
VT
599 break;
600 }
1da177e4
LT
601}
602
52087a79
LAD
603static inline unsigned int hci_conn_num(struct hci_dev *hdev, __u8 type)
604{
605 struct hci_conn_hash *h = &hdev->conn_hash;
606 switch (type) {
607 case ACL_LINK:
608 return h->acl_num;
bd1eb66b
AE
609 case AMP_LINK:
610 return h->amp_num;
52087a79
LAD
611 case LE_LINK:
612 return h->le_num;
613 case SCO_LINK:
614 case ESCO_LINK:
615 return h->sco_num;
616 default:
617 return 0;
618 }
619}
620
f4f07505
JH
621static inline unsigned int hci_conn_count(struct hci_dev *hdev)
622{
623 struct hci_conn_hash *c = &hdev->conn_hash;
624
625 return c->acl_num + c->amp_num + c->sco_num + c->le_num;
626}
627
1da177e4 628static inline struct hci_conn *hci_conn_hash_lookup_handle(struct hci_dev *hdev,
adc4266d 629 __u16 handle)
1da177e4
LT
630{
631 struct hci_conn_hash *h = &hdev->conn_hash;
1da177e4
LT
632 struct hci_conn *c;
633
bf4c6325
GP
634 rcu_read_lock();
635
636 list_for_each_entry_rcu(c, &h->list, list) {
637 if (c->handle == handle) {
638 rcu_read_unlock();
1da177e4 639 return c;
bf4c6325 640 }
1da177e4 641 }
bf4c6325
GP
642 rcu_read_unlock();
643
1da177e4
LT
644 return NULL;
645}
646
647static inline struct hci_conn *hci_conn_hash_lookup_ba(struct hci_dev *hdev,
adc4266d 648 __u8 type, bdaddr_t *ba)
1da177e4
LT
649{
650 struct hci_conn_hash *h = &hdev->conn_hash;
1da177e4
LT
651 struct hci_conn *c;
652
bf4c6325
GP
653 rcu_read_lock();
654
655 list_for_each_entry_rcu(c, &h->list, list) {
656 if (c->type == type && !bacmp(&c->dst, ba)) {
657 rcu_read_unlock();
1da177e4 658 return c;
bf4c6325 659 }
1da177e4 660 }
bf4c6325
GP
661
662 rcu_read_unlock();
663
1da177e4
LT
664 return NULL;
665}
666
4c67bc74 667static inline struct hci_conn *hci_conn_hash_lookup_state(struct hci_dev *hdev,
adc4266d 668 __u8 type, __u16 state)
4c67bc74
MH
669{
670 struct hci_conn_hash *h = &hdev->conn_hash;
4c67bc74
MH
671 struct hci_conn *c;
672
bf4c6325
GP
673 rcu_read_lock();
674
675 list_for_each_entry_rcu(c, &h->list, list) {
676 if (c->type == type && c->state == state) {
677 rcu_read_unlock();
4c67bc74 678 return c;
bf4c6325 679 }
4c67bc74 680 }
73d80deb 681
bf4c6325 682 rcu_read_unlock();
73d80deb 683
4c67bc74 684 return NULL;
73d80deb
LAD
685}
686
bed71748 687void hci_disconnect(struct hci_conn *conn, __u8 reason);
2dea632f 688bool hci_setup_sync(struct hci_conn *conn, __u16 handle);
e73439d8 689void hci_sco_setup(struct hci_conn *conn, __u8 status);
1da177e4
LT
690
691struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst);
a9de9248
MH
692int hci_conn_del(struct hci_conn *conn);
693void hci_conn_hash_flush(struct hci_dev *hdev);
694void hci_conn_check_pending(struct hci_dev *hdev);
1da177e4 695
73d80deb 696struct hci_chan *hci_chan_create(struct hci_conn *conn);
9472007c 697void hci_chan_del(struct hci_chan *chan);
2c33c06a 698void hci_chan_list_flush(struct hci_conn *conn);
42c4e53e 699struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle);
73d80deb 700
04a6c589
AG
701struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
702 u8 dst_type, u8 sec_level, u8 auth_type);
703struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,
704 u8 sec_level, u8 auth_type);
10c62ddc
FD
705struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst,
706 __u16 setting);
e7c29cb1 707int hci_conn_check_link_mode(struct hci_conn *conn);
b3b1b061 708int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level);
0684e5f9 709int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type);
1da177e4 710int hci_conn_change_link_key(struct hci_conn *conn);
8c1b2355 711int hci_conn_switch_role(struct hci_conn *conn, __u8 role);
1da177e4 712
14b12d0b 713void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active);
1da177e4 714
06c053fb
AG
715void hci_le_conn_failed(struct hci_conn *conn, u8 status);
716
8d12356f
DH
717/*
718 * hci_conn_get() and hci_conn_put() are used to control the life-time of an
719 * "hci_conn" object. They do not guarantee that the hci_conn object is running,
720 * working or anything else. They just guarantee that the object is available
721 * and can be dereferenced. So you can use its locks, local variables and any
722 * other constant data.
723 * Before accessing runtime data, you _must_ lock the object and then check that
724 * it is still running. As soon as you release the locks, the connection might
725 * get dropped, though.
726 *
727 * On the other hand, hci_conn_hold() and hci_conn_drop() are used to control
728 * how long the underlying connection is held. So every channel that runs on the
729 * hci_conn object calls this to prevent the connection from disappearing. As
730 * long as you hold a device, you must also guarantee that you have a valid
731 * reference to the device via hci_conn_get() (or the initial reference from
732 * hci_conn_add()).
733 * The hold()/drop() ref-count is known to drop below 0 sometimes, which doesn't
734 * break because nobody cares for that. But this means, we cannot use
735 * _get()/_drop() in it, but require the caller to have a valid ref (FIXME).
736 */
737
738static inline void hci_conn_get(struct hci_conn *conn)
739{
740 get_device(&conn->dev);
741}
742
743static inline void hci_conn_put(struct hci_conn *conn)
744{
745 put_device(&conn->dev);
746}
747
1da177e4
LT
748static inline void hci_conn_hold(struct hci_conn *conn)
749{
71becf0c 750 BT_DBG("hcon %p orig refcnt %d", conn, atomic_read(&conn->refcnt));
38b3fef1 751
1da177e4 752 atomic_inc(&conn->refcnt);
2f304d1e 753 cancel_delayed_work(&conn->disc_work);
1da177e4
LT
754}
755
76a68ba0 756static inline void hci_conn_drop(struct hci_conn *conn)
1da177e4 757{
71becf0c 758 BT_DBG("hcon %p orig refcnt %d", conn, atomic_read(&conn->refcnt));
38b3fef1 759
1da177e4 760 if (atomic_dec_and_test(&conn->refcnt)) {
04837f64 761 unsigned long timeo;
716e4ab5
AE
762
763 switch (conn->type) {
764 case ACL_LINK:
765 case LE_LINK:
a74a84f6 766 cancel_delayed_work(&conn->idle_work);
6ac59344 767 if (conn->state == BT_CONNECTED) {
5f246e89 768 timeo = conn->disc_timeout;
6ac59344 769 if (!conn->out)
052b30b0 770 timeo *= 2;
5a9d0a3f 771 } else {
6ac59344 772 timeo = msecs_to_jiffies(10);
5a9d0a3f 773 }
716e4ab5
AE
774 break;
775
776 case AMP_LINK:
777 timeo = conn->disc_timeout;
778 break;
779
780 default:
04837f64 781 timeo = msecs_to_jiffies(10);
716e4ab5 782 break;
5a9d0a3f 783 }
716e4ab5 784
2f304d1e 785 cancel_delayed_work(&conn->disc_work);
19c40e3b 786 queue_delayed_work(conn->hdev->workqueue,
716e4ab5 787 &conn->disc_work, timeo);
1da177e4
LT
788 }
789}
790
1da177e4 791/* ----- HCI Devices ----- */
dc946bd8 792static inline void hci_dev_put(struct hci_dev *d)
1da177e4 793{
376261ae
AE
794 BT_DBG("%s orig refcnt %d", d->name,
795 atomic_read(&d->dev.kobj.kref.refcount));
796
4c724c71 797 put_device(&d->dev);
1da177e4
LT
798}
799
dc946bd8 800static inline struct hci_dev *hci_dev_hold(struct hci_dev *d)
1da177e4 801{
376261ae
AE
802 BT_DBG("%s orig refcnt %d", d->name,
803 atomic_read(&d->dev.kobj.kref.refcount));
804
4c724c71 805 get_device(&d->dev);
1da177e4
LT
806 return d;
807}
808
09fd0de5
GP
809#define hci_dev_lock(d) mutex_lock(&d->lock)
810#define hci_dev_unlock(d) mutex_unlock(&d->lock)
1da177e4 811
aa2b86d7 812#define to_hci_dev(d) container_of(d, struct hci_dev, dev)
3dc07322 813#define to_hci_conn(c) container_of(c, struct hci_conn, dev)
aa2b86d7 814
155961e8
DH
815static inline void *hci_get_drvdata(struct hci_dev *hdev)
816{
817 return dev_get_drvdata(&hdev->dev);
818}
819
820static inline void hci_set_drvdata(struct hci_dev *hdev, void *data)
821{
822 dev_set_drvdata(&hdev->dev, data);
823}
824
1da177e4 825struct hci_dev *hci_dev_get(int index);
0c0afedf 826struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src);
1da177e4
LT
827
828struct hci_dev *hci_alloc_dev(void);
829void hci_free_dev(struct hci_dev *hdev);
830int hci_register_dev(struct hci_dev *hdev);
59735631 831void hci_unregister_dev(struct hci_dev *hdev);
1da177e4
LT
832int hci_suspend_dev(struct hci_dev *hdev);
833int hci_resume_dev(struct hci_dev *hdev);
834int hci_dev_open(__u16 dev);
835int hci_dev_close(__u16 dev);
836int hci_dev_reset(__u16 dev);
837int hci_dev_reset_stat(__u16 dev);
838int hci_dev_cmd(unsigned int cmd, void __user *arg);
839int hci_get_dev_list(void __user *arg);
840int hci_get_dev_info(void __user *arg);
841int hci_get_conn_list(void __user *arg);
842int hci_get_conn_info(struct hci_dev *hdev, void __user *arg);
40be492f 843int hci_get_auth_info(struct hci_dev *hdev, void __user *arg);
1da177e4
LT
844int hci_inquiry(void __user *arg);
845
c3c7ea65 846struct bdaddr_list *hci_blacklist_lookup(struct hci_dev *hdev,
b9ee0a78 847 bdaddr_t *bdaddr, u8 type);
88c1fe4b
JH
848int hci_blacklist_add(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
849int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
f0358568 850
d2ab0ac1
MH
851struct bdaddr_list *hci_white_list_lookup(struct hci_dev *hdev,
852 bdaddr_t *bdaddr, u8 type);
853void hci_white_list_clear(struct hci_dev *hdev);
854int hci_white_list_add(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
855int hci_white_list_del(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
856
15819a70
AG
857struct hci_conn_params *hci_conn_params_lookup(struct hci_dev *hdev,
858 bdaddr_t *addr, u8 addr_type);
51d167c0
MH
859struct hci_conn_params *hci_conn_params_add(struct hci_dev *hdev,
860 bdaddr_t *addr, u8 addr_type);
bf5b3c8b 861int hci_conn_params_set(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type,
d06b50ce 862 u8 auto_connect);
15819a70 863void hci_conn_params_del(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type);
373110c5 864void hci_conn_params_clear_all(struct hci_dev *hdev);
55af49a8
JH
865void hci_conn_params_clear_disabled(struct hci_dev *hdev);
866void hci_conn_params_clear_enabled(struct hci_dev *hdev);
15819a70 867
77a77a30
AG
868struct bdaddr_list *hci_pend_le_conn_lookup(struct hci_dev *hdev,
869 bdaddr_t *addr, u8 addr_type);
870void hci_pend_le_conn_add(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type);
871void hci_pend_le_conn_del(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type);
872void hci_pend_le_conns_clear(struct hci_dev *hdev);
873
a4790dbd
AG
874void hci_update_background_scan(struct hci_dev *hdev);
875
35f7498a 876void hci_uuids_clear(struct hci_dev *hdev);
2aeb9a1a 877
35f7498a 878void hci_link_keys_clear(struct hci_dev *hdev);
55ed8ca1 879struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
567fa2aa 880struct link_key *hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn,
7652ff6a
JH
881 bdaddr_t *bdaddr, u8 *val, u8 type,
882 u8 pin_len, bool *persistent);
fe39c7b2 883struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, __le64 rand,
98a0b845 884 bool master);
ca9142b8 885struct smp_ltk *hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr,
35d70271 886 u8 addr_type, u8 type, u8 authenticated,
fe39c7b2 887 u8 tk[16], u8 enc_size, __le16 ediv, __le64 rand);
c9839a11 888struct smp_ltk *hci_find_ltk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr,
98a0b845 889 u8 addr_type, bool master);
e0b2b27e 890int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 bdaddr_type);
35f7498a 891void hci_smp_ltks_clear(struct hci_dev *hdev);
55ed8ca1
JH
892int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
893
970c4e46
JH
894struct smp_irk *hci_find_irk_by_rpa(struct hci_dev *hdev, bdaddr_t *rpa);
895struct smp_irk *hci_find_irk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr,
896 u8 addr_type);
ca9142b8
JH
897struct smp_irk *hci_add_irk(struct hci_dev *hdev, bdaddr_t *bdaddr,
898 u8 addr_type, u8 val[16], bdaddr_t *rpa);
a7ec7338 899void hci_remove_irk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type);
970c4e46
JH
900void hci_smp_irks_clear(struct hci_dev *hdev);
901
35f7498a 902void hci_remote_oob_data_clear(struct hci_dev *hdev);
2763eda6 903struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev,
0798872e
MH
904 bdaddr_t *bdaddr);
905int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr,
906 u8 *hash, u8 *randomizer);
907int hci_add_remote_oob_ext_data(struct hci_dev *hdev, bdaddr_t *bdaddr,
908 u8 *hash192, u8 *randomizer192,
909 u8 *hash256, u8 *randomizer256);
2763eda6
SJ
910int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr);
911
1da177e4
LT
912void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb);
913
e1a26170 914int hci_recv_frame(struct hci_dev *hdev, struct sk_buff *skb);
ef222013 915int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count);
99811510 916int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count);
ef222013 917
0ac7e700 918void hci_init_sysfs(struct hci_dev *hdev);
a67e899c 919void hci_conn_init_sysfs(struct hci_conn *conn);
b219e3ac
MH
920void hci_conn_add_sysfs(struct hci_conn *conn);
921void hci_conn_del_sysfs(struct hci_conn *conn);
1da177e4 922
6935e0f5 923#define SET_HCIDEV_DEV(hdev, pdev) ((hdev)->dev.parent = (pdev))
1da177e4
LT
924
925/* ----- LMP capabilities ----- */
cad718ed
JH
926#define lmp_encrypt_capable(dev) ((dev)->features[0][0] & LMP_ENCRYPT)
927#define lmp_rswitch_capable(dev) ((dev)->features[0][0] & LMP_RSWITCH)
928#define lmp_hold_capable(dev) ((dev)->features[0][0] & LMP_HOLD)
929#define lmp_sniff_capable(dev) ((dev)->features[0][0] & LMP_SNIFF)
930#define lmp_park_capable(dev) ((dev)->features[0][1] & LMP_PARK)
931#define lmp_inq_rssi_capable(dev) ((dev)->features[0][3] & LMP_RSSI_INQ)
932#define lmp_esco_capable(dev) ((dev)->features[0][3] & LMP_ESCO)
933#define lmp_bredr_capable(dev) (!((dev)->features[0][4] & LMP_NO_BREDR))
934#define lmp_le_capable(dev) ((dev)->features[0][4] & LMP_LE)
935#define lmp_sniffsubr_capable(dev) ((dev)->features[0][5] & LMP_SNIFF_SUBR)
936#define lmp_pause_enc_capable(dev) ((dev)->features[0][5] & LMP_PAUSE_ENC)
937#define lmp_ext_inq_capable(dev) ((dev)->features[0][6] & LMP_EXT_INQ)
938#define lmp_le_br_capable(dev) (!!((dev)->features[0][6] & LMP_SIMUL_LE_BR))
939#define lmp_ssp_capable(dev) ((dev)->features[0][6] & LMP_SIMPLE_PAIR)
940#define lmp_no_flush_capable(dev) ((dev)->features[0][6] & LMP_NO_FLUSH)
941#define lmp_lsto_capable(dev) ((dev)->features[0][7] & LMP_LSTO)
942#define lmp_inq_tx_pwr_capable(dev) ((dev)->features[0][7] & LMP_INQ_TX_PWR)
943#define lmp_ext_feat_capable(dev) ((dev)->features[0][7] & LMP_EXTFEATURES)
07a5c61e 944#define lmp_transp_capable(dev) ((dev)->features[0][2] & LMP_TRANSPARENT)
1da177e4 945
eead27da 946/* ----- Extended LMP capabilities ----- */
53b834d2
MH
947#define lmp_csb_master_capable(dev) ((dev)->features[2][0] & LMP_CSB_MASTER)
948#define lmp_csb_slave_capable(dev) ((dev)->features[2][0] & LMP_CSB_SLAVE)
949#define lmp_sync_train_capable(dev) ((dev)->features[2][0] & LMP_SYNC_TRAIN)
950#define lmp_sync_scan_capable(dev) ((dev)->features[2][0] & LMP_SYNC_SCAN)
d5991585
MH
951#define lmp_sc_capable(dev) ((dev)->features[2][1] & LMP_SC)
952#define lmp_ping_capable(dev) ((dev)->features[2][1] & LMP_PING)
53b834d2
MH
953
954/* ----- Host capabilities ----- */
cad718ed 955#define lmp_host_ssp_capable(dev) ((dev)->features[1][0] & LMP_HOST_SSP)
d5991585 956#define lmp_host_sc_capable(dev) ((dev)->features[1][0] & LMP_HOST_SC)
cad718ed
JH
957#define lmp_host_le_capable(dev) (!!((dev)->features[1][0] & LMP_HOST_LE))
958#define lmp_host_le_br_capable(dev) (!!((dev)->features[1][0] & LMP_HOST_LE_BREDR))
eead27da 959
1da177e4 960/* ----- HCI protocols ----- */
20714bfe
FD
961#define HCI_PROTO_DEFER 0x01
962
5a9d0a3f 963static inline int hci_proto_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr,
20714bfe 964 __u8 type, __u8 *flags)
1da177e4 965{
686ebf28
UF
966 switch (type) {
967 case ACL_LINK:
968 return l2cap_connect_ind(hdev, bdaddr);
1da177e4 969
686ebf28
UF
970 case SCO_LINK:
971 case ESCO_LINK:
20714bfe 972 return sco_connect_ind(hdev, bdaddr, flags);
1da177e4 973
686ebf28
UF
974 default:
975 BT_ERR("unknown link type %d", type);
976 return -EINVAL;
977 }
1da177e4
LT
978}
979
980static inline void hci_proto_connect_cfm(struct hci_conn *conn, __u8 status)
981{
686ebf28
UF
982 switch (conn->type) {
983 case ACL_LINK:
984 case LE_LINK:
985 l2cap_connect_cfm(conn, status);
986 break;
1da177e4 987
686ebf28
UF
988 case SCO_LINK:
989 case ESCO_LINK:
990 sco_connect_cfm(conn, status);
991 break;
1da177e4 992
686ebf28
UF
993 default:
994 BT_ERR("unknown link type %d", conn->type);
995 break;
996 }
e9a416b5
JH
997
998 if (conn->connect_cfm_cb)
999 conn->connect_cfm_cb(conn, status);
1da177e4
LT
1000}
1001
2950f21a 1002static inline int hci_proto_disconn_ind(struct hci_conn *conn)
1da177e4 1003{
686ebf28
UF
1004 if (conn->type != ACL_LINK && conn->type != LE_LINK)
1005 return HCI_ERROR_REMOTE_USER_TERM;
1da177e4 1006
686ebf28 1007 return l2cap_disconn_ind(conn);
2950f21a
MH
1008}
1009
1010static inline void hci_proto_disconn_cfm(struct hci_conn *conn, __u8 reason)
1011{
686ebf28
UF
1012 switch (conn->type) {
1013 case ACL_LINK:
1014 case LE_LINK:
1015 l2cap_disconn_cfm(conn, reason);
1016 break;
2950f21a 1017
686ebf28
UF
1018 case SCO_LINK:
1019 case ESCO_LINK:
1020 sco_disconn_cfm(conn, reason);
1021 break;
2950f21a 1022
bd1eb66b
AE
1023 /* L2CAP would be handled for BREDR chan */
1024 case AMP_LINK:
1025 break;
1026
686ebf28
UF
1027 default:
1028 BT_ERR("unknown link type %d", conn->type);
1029 break;
1030 }
e9a416b5
JH
1031
1032 if (conn->disconn_cfm_cb)
1033 conn->disconn_cfm_cb(conn, reason);
1da177e4
LT
1034}
1035
1036static inline void hci_proto_auth_cfm(struct hci_conn *conn, __u8 status)
1037{
8c1b2355
MH
1038 __u8 encrypt;
1039
686ebf28
UF
1040 if (conn->type != ACL_LINK && conn->type != LE_LINK)
1041 return;
1042
51a8efd7 1043 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
8c1b2355
MH
1044 return;
1045
4dae2798 1046 encrypt = test_bit(HCI_CONN_ENCRYPT, &conn->flags) ? 0x01 : 0x00;
686ebf28 1047 l2cap_security_cfm(conn, status, encrypt);
e9a416b5
JH
1048
1049 if (conn->security_cfm_cb)
1050 conn->security_cfm_cb(conn, status);
1da177e4
LT
1051}
1052
5a9d0a3f
WR
1053static inline void hci_proto_encrypt_cfm(struct hci_conn *conn, __u8 status,
1054 __u8 encrypt)
1da177e4 1055{
686ebf28
UF
1056 if (conn->type != ACL_LINK && conn->type != LE_LINK)
1057 return;
1da177e4 1058
686ebf28 1059 l2cap_security_cfm(conn, status, encrypt);
e9a416b5
JH
1060
1061 if (conn->security_cfm_cb)
1062 conn->security_cfm_cb(conn, status);
1da177e4
LT
1063}
1064
1da177e4
LT
1065/* ----- HCI callbacks ----- */
1066struct hci_cb {
1067 struct list_head list;
1068
1069 char *name;
1070
5a9d0a3f
WR
1071 void (*security_cfm) (struct hci_conn *conn, __u8 status,
1072 __u8 encrypt);
1da177e4
LT
1073 void (*key_change_cfm) (struct hci_conn *conn, __u8 status);
1074 void (*role_switch_cfm) (struct hci_conn *conn, __u8 status, __u8 role);
1075};
1076
1077static inline void hci_auth_cfm(struct hci_conn *conn, __u8 status)
1078{
711584ea 1079 struct hci_cb *cb;
8c1b2355 1080 __u8 encrypt;
1da177e4
LT
1081
1082 hci_proto_auth_cfm(conn, status);
1083
51a8efd7 1084 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
8c1b2355
MH
1085 return;
1086
4dae2798 1087 encrypt = test_bit(HCI_CONN_ENCRYPT, &conn->flags) ? 0x01 : 0x00;
8c1b2355 1088
f20d09d5 1089 read_lock(&hci_cb_list_lock);
711584ea 1090 list_for_each_entry(cb, &hci_cb_list, list) {
8c1b2355
MH
1091 if (cb->security_cfm)
1092 cb->security_cfm(conn, status, encrypt);
1da177e4 1093 }
f20d09d5 1094 read_unlock(&hci_cb_list_lock);
1da177e4
LT
1095}
1096
5a9d0a3f
WR
1097static inline void hci_encrypt_cfm(struct hci_conn *conn, __u8 status,
1098 __u8 encrypt)
1da177e4 1099{
711584ea 1100 struct hci_cb *cb;
1da177e4 1101
435fef20
MH
1102 if (conn->sec_level == BT_SECURITY_SDP)
1103 conn->sec_level = BT_SECURITY_LOW;
1104
88167aed
VCG
1105 if (conn->pending_sec_level > conn->sec_level)
1106 conn->sec_level = conn->pending_sec_level;
1107
9719f8af 1108 hci_proto_encrypt_cfm(conn, status, encrypt);
1da177e4 1109
f20d09d5 1110 read_lock(&hci_cb_list_lock);
711584ea 1111 list_for_each_entry(cb, &hci_cb_list, list) {
8c1b2355
MH
1112 if (cb->security_cfm)
1113 cb->security_cfm(conn, status, encrypt);
1da177e4 1114 }
f20d09d5 1115 read_unlock(&hci_cb_list_lock);
1da177e4
LT
1116}
1117
1118static inline void hci_key_change_cfm(struct hci_conn *conn, __u8 status)
1119{
711584ea 1120 struct hci_cb *cb;
1da177e4 1121
f20d09d5 1122 read_lock(&hci_cb_list_lock);
711584ea 1123 list_for_each_entry(cb, &hci_cb_list, list) {
1da177e4
LT
1124 if (cb->key_change_cfm)
1125 cb->key_change_cfm(conn, status);
1126 }
f20d09d5 1127 read_unlock(&hci_cb_list_lock);
1da177e4
LT
1128}
1129
5a9d0a3f
WR
1130static inline void hci_role_switch_cfm(struct hci_conn *conn, __u8 status,
1131 __u8 role)
1da177e4 1132{
711584ea 1133 struct hci_cb *cb;
1da177e4 1134
f20d09d5 1135 read_lock(&hci_cb_list_lock);
711584ea 1136 list_for_each_entry(cb, &hci_cb_list, list) {
1da177e4
LT
1137 if (cb->role_switch_cfm)
1138 cb->role_switch_cfm(conn, status, role);
1139 }
f20d09d5 1140 read_unlock(&hci_cb_list_lock);
1da177e4
LT
1141}
1142
6759a675
JH
1143static inline bool eir_has_data_type(u8 *data, size_t data_len, u8 type)
1144{
84d9d071 1145 size_t parsed = 0;
6759a675 1146
6c0c331e
JH
1147 if (data_len < 2)
1148 return false;
1149
84d9d071
JH
1150 while (parsed < data_len - 1) {
1151 u8 field_len = data[0];
6759a675
JH
1152
1153 if (field_len == 0)
1154 break;
1155
1156 parsed += field_len + 1;
1157
1158 if (parsed > data_len)
1159 break;
1160
1161 if (data[1] == type)
1162 return true;
1163
1164 data += field_len + 1;
1165 }
1166
1167 return false;
1168}
1169
301cb2d8
JH
1170static inline bool hci_bdaddr_is_rpa(bdaddr_t *bdaddr, u8 addr_type)
1171{
dbbfa2ab 1172 if (addr_type != ADDR_LE_DEV_RANDOM)
301cb2d8
JH
1173 return false;
1174
1175 if ((bdaddr->b[5] & 0xc0) == 0x40)
1176 return true;
1177
1178 return false;
1179}
1180
2426f3a5
JH
1181static inline struct smp_irk *hci_get_irk(struct hci_dev *hdev,
1182 bdaddr_t *bdaddr, u8 addr_type)
1183{
1184 if (!hci_bdaddr_is_rpa(bdaddr, addr_type))
1185 return NULL;
1186
1187 return hci_find_irk_by_rpa(hdev, bdaddr);
1188}
1189
d4905f24
AG
1190static inline int hci_check_conn_params(u16 min, u16 max, u16 latency,
1191 u16 to_multiplier)
1192{
1193 u16 max_latency;
1194
1195 if (min > max || min < 6 || max > 3200)
1196 return -EINVAL;
1197
1198 if (to_multiplier < 10 || to_multiplier > 3200)
1199 return -EINVAL;
1200
1201 if (max >= to_multiplier * 8)
1202 return -EINVAL;
1203
1204 max_latency = (to_multiplier * 8 / max) - 1;
1205 if (latency > 499 || latency > max_latency)
1206 return -EINVAL;
1207
1208 return 0;
1209}
1210
1da177e4
LT
1211int hci_register_cb(struct hci_cb *hcb);
1212int hci_unregister_cb(struct hci_cb *hcb);
1213
3119ae95
JH
1214struct hci_request {
1215 struct hci_dev *hdev;
1216 struct sk_buff_head cmd_q;
5d73e034
AG
1217
1218 /* If something goes wrong when building the HCI request, the error
1219 * value is stored in this field.
1220 */
1221 int err;
3119ae95
JH
1222};
1223
1224void hci_req_init(struct hci_request *req, struct hci_dev *hdev);
1225int hci_req_run(struct hci_request *req, hci_req_complete_t complete);
07dc93dd
JH
1226void hci_req_add(struct hci_request *req, u16 opcode, u32 plen,
1227 const void *param);
1228void hci_req_add_ev(struct hci_request *req, u16 opcode, u32 plen,
1229 const void *param, u8 event);
9238f36a 1230void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status);
3119ae95 1231
b1efcc28 1232void hci_req_add_le_scan_disable(struct hci_request *req);
8ef30fd3 1233void hci_req_add_le_passive_scan(struct hci_request *req);
b1efcc28 1234
75e84b7c 1235struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
07dc93dd 1236 const void *param, u32 timeout);
7b1abbbe 1237struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
07dc93dd 1238 const void *param, u8 event, u32 timeout);
75e84b7c 1239
07dc93dd
JH
1240int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen,
1241 const void *param);
73d80deb 1242void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags);
0d861d8b 1243void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb);
1da177e4 1244
a9de9248 1245void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode);
1da177e4 1246
1da177e4 1247/* ----- HCI Sockets ----- */
470fe1b5
MH
1248void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb);
1249void hci_send_to_control(struct sk_buff *skb, struct sock *skip_sk);
cd82e61c 1250void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb);
1da177e4 1251
040030ef
MH
1252void hci_sock_dev_event(struct hci_dev *hdev, int event);
1253
0381101f 1254/* Management interface */
591f47f3
AG
1255#define DISCOV_TYPE_BREDR (BIT(BDADDR_BREDR))
1256#define DISCOV_TYPE_LE (BIT(BDADDR_LE_PUBLIC) | \
1257 BIT(BDADDR_LE_RANDOM))
1258#define DISCOV_TYPE_INTERLEAVED (BIT(BDADDR_BREDR) | \
1259 BIT(BDADDR_LE_PUBLIC) | \
1260 BIT(BDADDR_LE_RANDOM))
f39799f5 1261
0d8cc935
AG
1262/* These LE scan and inquiry parameters were chosen according to LE General
1263 * Discovery Procedure specification.
1264 */
1265#define DISCOV_LE_SCAN_WIN 0x12
1266#define DISCOV_LE_SCAN_INT 0x12
3d5a76f0 1267#define DISCOV_LE_TIMEOUT 10240 /* msec */
ae55f598 1268#define DISCOV_INTERLEAVED_TIMEOUT 5120 /* msec */
0d8cc935
AG
1269#define DISCOV_INTERLEAVED_INQUIRY_LEN 0x04
1270#define DISCOV_BREDR_INQUIRY_LEN 0x08
1271
0381101f 1272int mgmt_control(struct sock *sk, struct msghdr *msg, size_t len);
bf6b56db
MH
1273void mgmt_index_added(struct hci_dev *hdev);
1274void mgmt_index_removed(struct hci_dev *hdev);
3eec705e 1275void mgmt_set_powered_failed(struct hci_dev *hdev, int err);
744cf19e 1276int mgmt_powered(struct hci_dev *hdev, u8 powered);
d1967ff8 1277void mgmt_discoverable_timeout(struct hci_dev *hdev);
86a75645 1278void mgmt_discoverable(struct hci_dev *hdev, u8 discoverable);
a330916c 1279void mgmt_connectable(struct hci_dev *hdev, u8 connectable);
778b235a 1280void mgmt_advertising(struct hci_dev *hdev, u8 advertising);
4796e8af 1281void mgmt_write_scan_failed(struct hci_dev *hdev, u8 scan, u8 status);
dc4a5ee2
MH
1282void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
1283 bool persistent);
ecd90ae7
MH
1284void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
1285 u8 addr_type, u32 flags, u8 *name, u8 name_len,
1286 u8 *dev_class);
9b80ec5e 1287void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
12d4a3b2
JH
1288 u8 link_type, u8 addr_type, u8 reason,
1289 bool mgmt_connected);
7892924c
MH
1290void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
1291 u8 link_type, u8 addr_type, u8 status);
445608d0
MH
1292void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
1293 u8 addr_type, u8 status);
ce0e4a0d 1294void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure);
e669cf80
MH
1295void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1296 u8 status);
3eb38528
MH
1297void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1298 u8 status);
744cf19e 1299int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
39adbffe 1300 u8 link_type, u8 addr_type, u32 value,
04124681 1301 u8 confirm_hint);
744cf19e 1302int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
04124681 1303 u8 link_type, u8 addr_type, u8 status);
272d90df 1304int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
04124681 1305 u8 link_type, u8 addr_type, u8 status);
272d90df 1306int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
04124681 1307 u8 link_type, u8 addr_type);
604086b7 1308int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
04124681 1309 u8 link_type, u8 addr_type, u8 status);
272d90df 1310int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
04124681 1311 u8 link_type, u8 addr_type, u8 status);
92a25256
JH
1312int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
1313 u8 link_type, u8 addr_type, u32 passkey,
1314 u8 entered);
e546099c
MH
1315void mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
1316 u8 addr_type, u8 status);
464996ae 1317void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status);
3e248560 1318void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status);
eac83dc6 1319void mgmt_sc_enable_complete(struct hci_dev *hdev, u8 enable, u8 status);
4e1b0245
MH
1320void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
1321 u8 status);
7667da34 1322void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status);
4d2d2796
MH
1323void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192,
1324 u8 *randomizer192, u8 *hash256,
1325 u8 *randomizer256, u8 status);
901801b9 1326void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
af58925c
MH
1327 u8 addr_type, u8 *dev_class, s8 rssi, u32 flags,
1328 u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len);
9cf12aee
MH
1329void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
1330 u8 addr_type, s8 rssi, u8 *name, u8 name_len);
2f1e063b 1331void mgmt_discovering(struct hci_dev *hdev, u8 discovering);
53ac6ab6 1332void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent);
95fbac8a 1333void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk);
53ac6ab6
MH
1334void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
1335 bool persistent);
ffb5a827
AG
1336void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr,
1337 u8 bdaddr_type, u16 min_interval, u16 max_interval,
1338 u16 latency, u16 timeout);
5976e608 1339void mgmt_reenable_advertising(struct hci_dev *hdev);
f4a407be 1340void mgmt_smp_complete(struct hci_conn *conn, bool complete);
346af67b 1341
1da177e4
LT
1342/* HCI info for socket */
1343#define hci_pi(sk) ((struct hci_pinfo *) sk)
1344
1345struct hci_pinfo {
1346 struct bt_sock bt;
1347 struct hci_dev *hdev;
1348 struct hci_filter filter;
1349 __u32 cmsg_mask;
c02178d2 1350 unsigned short channel;
1da177e4
LT
1351};
1352
1353/* HCI security filter */
1354#define HCI_SFLT_MAX_OGF 5
1355
1356struct hci_sec_filter {
1357 __u32 type_mask;
1358 __u32 event_mask[2];
1359 __u32 ocf_mask[HCI_SFLT_MAX_OGF + 1][4];
1360};
1361
1362/* ----- HCI requests ----- */
1363#define HCI_REQ_DONE 0
1364#define HCI_REQ_PEND 1
1365#define HCI_REQ_CANCELED 2
1366
a6a67efd
TG
1367#define hci_req_lock(d) mutex_lock(&d->req_lock)
1368#define hci_req_unlock(d) mutex_unlock(&d->req_lock)
1da177e4 1369
2ce603eb
CT
1370void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max,
1371 u16 latency, u16 to_multiplier);
fe39c7b2 1372void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand,
a7a595f6 1373 __u8 ltk[16]);
2519a1fc 1374
94b1fc92
MH
1375int hci_update_random_address(struct hci_request *req, bool require_privacy,
1376 u8 *own_addr_type);
a1f4c318
JH
1377void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr,
1378 u8 *bdaddr_type);
ebd3a747 1379
5d4d62f6
FD
1380#define SCO_AIRMODE_MASK 0x0003
1381#define SCO_AIRMODE_CVSD 0x0000
1382#define SCO_AIRMODE_TRANSP 0x0003
1383
1da177e4 1384#endif /* __HCI_CORE_H */