1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#ifndef __HCI_CORE_H
26#define __HCI_CORE_H
27
28#include <net/bluetooth/hci.h>
29
30
31#define HCI_PRIO_MAX 7
32
33
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];
40 __le16 clock_offset;
41 __s8 rssi;
42 __u8 ssp_mode;
43};
44
45struct inquiry_entry {
46 struct list_head all;
47 struct list_head list;
48 enum {
49 NAME_NOT_KNOWN,
50 NAME_NEEDED,
51 NAME_PENDING,
52 NAME_KNOWN,
53 } name_state;
54 __u32 timestamp;
55 struct inquiry_data data;
56};
57
58struct discovery_state {
59 int type;
60 enum {
61 DISCOVERY_STOPPED,
62 DISCOVERY_STARTING,
63 DISCOVERY_FINDING,
64 DISCOVERY_RESOLVING,
65 DISCOVERY_STOPPING,
66 } state;
67 struct list_head all;
68 struct list_head unknown;
69 struct list_head resolve;
70 __u32 timestamp;
71};
72
73struct hci_conn_hash {
74 struct list_head list;
75 unsigned int acl_num;
76 unsigned int amp_num;
77 unsigned int sco_num;
78 unsigned int le_num;
79};
80
81struct bdaddr_list {
82 struct list_head list;
83 bdaddr_t bdaddr;
84};
85
86struct bt_uuid {
87 struct list_head list;
88 u8 uuid[16];
89 u8 size;
90 u8 svc_hint;
91};
92
93struct smp_ltk {
94 struct list_head list;
95 bdaddr_t bdaddr;
96 u8 bdaddr_type;
97 u8 authenticated;
98 u8 type;
99 u8 enc_size;
100 __le16 ediv;
101 u8 rand[8];
102 u8 val[16];
103} __packed;
104
105struct link_key {
106 struct list_head list;
107 bdaddr_t bdaddr;
108 u8 type;
109 u8 val[HCI_LINK_KEY_SIZE];
110 u8 pin_len;
111};
112
113struct oob_data {
114 struct list_head list;
115 bdaddr_t bdaddr;
116 u8 hash[16];
117 u8 randomizer[16];
118};
119
120#define HCI_MAX_SHORT_NAME_LENGTH 10
121
122struct amp_assoc {
123 __u16 len;
124 __u16 offset;
125 __u16 rem_len;
126 __u16 len_so_far;
127 __u8 data[HCI_MAX_AMP_ASSOC_SIZE];
128};
129
130#define HCI_MAX_PAGES 3
131
132#define NUM_REASSEMBLY 4
133struct hci_dev {
134 struct list_head list;
135 struct mutex lock;
136
137 char name[8];
138 unsigned long flags;
139 __u16 id;
140 __u8 bus;
141 __u8 dev_type;
142 bdaddr_t bdaddr;
143 __u8 dev_name[HCI_MAX_NAME_LENGTH];
144 __u8 short_name[HCI_MAX_SHORT_NAME_LENGTH];
145 __u8 eir[HCI_MAX_EIR_LENGTH];
146 __u8 dev_class[3];
147 __u8 major_class;
148 __u8 minor_class;
149 __u8 max_page;
150 __u8 features[HCI_MAX_PAGES][8];
151 __u8 le_features[8];
152 __u8 le_white_list_size;
153 __u8 le_states[8];
154 __u8 commands[64];
155 __u8 hci_ver;
156 __u16 hci_rev;
157 __u8 lmp_ver;
158 __u16 manufacturer;
159 __u16 lmp_subver;
160 __u16 voice_setting;
161 __u8 io_capability;
162 __s8 inq_tx_power;
163 __u16 page_scan_interval;
164 __u16 page_scan_window;
165 __u8 page_scan_type;
166
167 __u16 devid_source;
168 __u16 devid_vendor;
169 __u16 devid_product;
170 __u16 devid_version;
171
172 __u16 pkt_type;
173 __u16 esco_type;
174 __u16 link_policy;
175 __u16 link_mode;
176
177 __u32 idle_timeout;
178 __u16 sniff_min_interval;
179 __u16 sniff_max_interval;
180
181 __u8 amp_status;
182 __u32 amp_total_bw;
183 __u32 amp_max_bw;
184 __u32 amp_min_latency;
185 __u32 amp_max_pdu;
186 __u8 amp_type;
187 __u16 amp_pal_cap;
188 __u16 amp_assoc_size;
189 __u32 amp_max_flush_to;
190 __u32 amp_be_flush_to;
191
192 struct amp_assoc loc_assoc;
193
194 __u8 flow_ctl_mode;
195
196 unsigned int auto_accept_delay;
197
198 unsigned long quirks;
199
200 atomic_t cmd_cnt;
201 unsigned int acl_cnt;
202 unsigned int sco_cnt;
203 unsigned int le_cnt;
204
205 unsigned int acl_mtu;
206 unsigned int sco_mtu;
207 unsigned int le_mtu;
208 unsigned int acl_pkts;
209 unsigned int sco_pkts;
210 unsigned int le_pkts;
211
212 __u16 block_len;
213 __u16 block_mtu;
214 __u16 num_blocks;
215 __u16 block_cnt;
216
217 unsigned long acl_last_tx;
218 unsigned long sco_last_tx;
219 unsigned long le_last_tx;
220
221 struct workqueue_struct *workqueue;
222 struct workqueue_struct *req_workqueue;
223
224 struct work_struct power_on;
225 struct delayed_work power_off;
226
227 __u16 discov_timeout;
228 struct delayed_work discov_off;
229
230 struct delayed_work service_cache;
231
232 struct timer_list cmd_timer;
233
234 struct work_struct rx_work;
235 struct work_struct cmd_work;
236 struct work_struct tx_work;
237
238 struct sk_buff_head rx_q;
239 struct sk_buff_head raw_q;
240 struct sk_buff_head cmd_q;
241
242 struct sk_buff *recv_evt;
243 struct sk_buff *sent_cmd;
244 struct sk_buff *reassembly[NUM_REASSEMBLY];
245
246 struct mutex req_lock;
247 wait_queue_head_t req_wait_q;
248 __u32 req_status;
249 __u32 req_result;
250
251 struct list_head mgmt_pending;
252
253 struct discovery_state discovery;
254 struct hci_conn_hash conn_hash;
255 struct list_head blacklist;
256
257 struct list_head uuids;
258
259 struct list_head link_keys;
260
261 struct list_head long_term_keys;
262
263 struct list_head remote_oob_data;
264
265 struct hci_dev_stats stat;
266
267 atomic_t promisc;
268
269 struct dentry *debugfs;
270
271 struct device dev;
272
273 struct rfkill *rfkill;
274
275 unsigned long dev_flags;
276
277 struct delayed_work le_scan_disable;
278
279 __s8 adv_tx_power;
280 __u8 adv_data[HCI_MAX_AD_LENGTH];
281 __u8 adv_data_len;
282
283 int (*open)(struct hci_dev *hdev);
284 int (*close)(struct hci_dev *hdev);
285 int (*flush)(struct hci_dev *hdev);
286 int (*setup)(struct hci_dev *hdev);
287 int (*send)(struct sk_buff *skb);
288 void (*notify)(struct hci_dev *hdev, unsigned int evt);
289 int (*ioctl)(struct hci_dev *hdev, unsigned int cmd, unsigned long arg);
290};
291
292#define HCI_PHY_HANDLE(handle) (handle & 0xff)
293
294struct hci_conn {
295 struct list_head list;
296
297 atomic_t refcnt;
298
299 bdaddr_t dst;
300 __u8 dst_type;
301 __u16 handle;
302 __u16 state;
303 __u8 mode;
304 __u8 type;
305 bool out;
306 __u8 attempt;
307 __u8 dev_class[3];
308 __u8 features[HCI_MAX_PAGES][8];
309 __u16 interval;
310 __u16 pkt_type;
311 __u16 link_policy;
312 __u32 link_mode;
313 __u8 key_type;
314 __u8 auth_type;
315 __u8 sec_level;
316 __u8 pending_sec_level;
317 __u8 pin_length;
318 __u8 enc_key_size;
319 __u8 io_capability;
320 __u32 passkey_notify;
321 __u8 passkey_entered;
322 __u16 disc_timeout;
323 __u16 setting;
324 unsigned long flags;
325
326 __u8 remote_cap;
327 __u8 remote_auth;
328 __u8 remote_id;
329 bool flush_key;
330
331 unsigned int sent;
332
333 struct sk_buff_head data_q;
334 struct list_head chan_list;
335
336 struct delayed_work disc_work;
337 struct timer_list idle_timer;
338 struct timer_list auto_accept_timer;
339
340 struct device dev;
341
342 struct hci_dev *hdev;
343 void *l2cap_data;
344 void *sco_data;
345 void *smp_conn;
346 struct amp_mgr *amp_mgr;
347
348 struct hci_conn *link;
349
350 void (*connect_cfm_cb) (struct hci_conn *conn, u8 status);
351 void (*security_cfm_cb) (struct hci_conn *conn, u8 status);
352 void (*disconn_cfm_cb) (struct hci_conn *conn, u8 reason);
353};
354
355struct hci_chan {
356 struct list_head list;
357 __u16 handle;
358 struct hci_conn *conn;
359 struct sk_buff_head data_q;
360 unsigned int sent;
361 __u8 state;
362};
363
364extern struct list_head hci_dev_list;
365extern struct list_head hci_cb_list;
366extern rwlock_t hci_dev_list_lock;
367extern rwlock_t hci_cb_list_lock;
368
369
370extern int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr);
371extern void l2cap_connect_cfm(struct hci_conn *hcon, u8 status);
372extern int l2cap_disconn_ind(struct hci_conn *hcon);
373extern void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason);
374extern int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt);
375extern int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb,
376 u16 flags);
377
378extern int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags);
379extern void sco_connect_cfm(struct hci_conn *hcon, __u8 status);
380extern void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason);
381extern int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb);
382
383
384#define INQUIRY_CACHE_AGE_MAX (HZ*30)
385#define INQUIRY_ENTRY_AGE_MAX (HZ*60)
386
387static inline void discovery_init(struct hci_dev *hdev)
388{
389 hdev->discovery.state = DISCOVERY_STOPPED;
390 INIT_LIST_HEAD(&hdev->discovery.all);
391 INIT_LIST_HEAD(&hdev->discovery.unknown);
392 INIT_LIST_HEAD(&hdev->discovery.resolve);
393}
394
395bool hci_discovery_active(struct hci_dev *hdev);
396
397void hci_discovery_set_state(struct hci_dev *hdev, int state);
398
399static inline int inquiry_cache_empty(struct hci_dev *hdev)
400{
401 return list_empty(&hdev->discovery.all);
402}
403
404static inline long inquiry_cache_age(struct hci_dev *hdev)
405{
406 struct discovery_state *c = &hdev->discovery;
407 return jiffies - c->timestamp;
408}
409
410static inline long inquiry_entry_age(struct inquiry_entry *e)
411{
412 return jiffies - e->timestamp;
413}
414
415struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev,
416 bdaddr_t *bdaddr);
417struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
418 bdaddr_t *bdaddr);
419struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev,
420 bdaddr_t *bdaddr,
421 int state);
422void hci_inquiry_cache_update_resolve(struct hci_dev *hdev,
423 struct inquiry_entry *ie);
424bool hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
425 bool name_known, bool *ssp);
426void hci_inquiry_cache_flush(struct hci_dev *hdev);
427
428
429enum {
430 HCI_CONN_AUTH_PEND,
431 HCI_CONN_REAUTH_PEND,
432 HCI_CONN_ENCRYPT_PEND,
433 HCI_CONN_RSWITCH_PEND,
434 HCI_CONN_MODE_CHANGE_PEND,
435 HCI_CONN_SCO_SETUP_PEND,
436 HCI_CONN_LE_SMP_PEND,
437 HCI_CONN_MGMT_CONNECTED,
438 HCI_CONN_SSP_ENABLED,
439 HCI_CONN_POWER_SAVE,
440 HCI_CONN_REMOTE_OOB,
441};
442
443static inline bool hci_conn_ssp_enabled(struct hci_conn *conn)
444{
445 struct hci_dev *hdev = conn->hdev;
446 return test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) &&
447 test_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
448}
449
450static inline void hci_conn_hash_add(struct hci_dev *hdev, struct hci_conn *c)
451{
452 struct hci_conn_hash *h = &hdev->conn_hash;
453 list_add_rcu(&c->list, &h->list);
454 switch (c->type) {
455 case ACL_LINK:
456 h->acl_num++;
457 break;
458 case AMP_LINK:
459 h->amp_num++;
460 break;
461 case LE_LINK:
462 h->le_num++;
463 break;
464 case SCO_LINK:
465 case ESCO_LINK:
466 h->sco_num++;
467 break;
468 }
469}
470
471static inline void hci_conn_hash_del(struct hci_dev *hdev, struct hci_conn *c)
472{
473 struct hci_conn_hash *h = &hdev->conn_hash;
474
475 list_del_rcu(&c->list);
476 synchronize_rcu();
477
478 switch (c->type) {
479 case ACL_LINK:
480 h->acl_num--;
481 break;
482 case AMP_LINK:
483 h->amp_num--;
484 break;
485 case LE_LINK:
486 h->le_num--;
487 break;
488 case SCO_LINK:
489 case ESCO_LINK:
490 h->sco_num--;
491 break;
492 }
493}
494
495static inline unsigned int hci_conn_num(struct hci_dev *hdev, __u8 type)
496{
497 struct hci_conn_hash *h = &hdev->conn_hash;
498 switch (type) {
499 case ACL_LINK:
500 return h->acl_num;
501 case AMP_LINK:
502 return h->amp_num;
503 case LE_LINK:
504 return h->le_num;
505 case SCO_LINK:
506 case ESCO_LINK:
507 return h->sco_num;
508 default:
509 return 0;
510 }
511}
512
513static inline struct hci_conn *hci_conn_hash_lookup_handle(struct hci_dev *hdev,
514 __u16 handle)
515{
516 struct hci_conn_hash *h = &hdev->conn_hash;
517 struct hci_conn *c;
518
519 rcu_read_lock();
520
521 list_for_each_entry_rcu(c, &h->list, list) {
522 if (c->handle == handle) {
523 rcu_read_unlock();
524 return c;
525 }
526 }
527 rcu_read_unlock();
528
529 return NULL;
530}
531
532static inline struct hci_conn *hci_conn_hash_lookup_ba(struct hci_dev *hdev,
533 __u8 type, bdaddr_t *ba)
534{
535 struct hci_conn_hash *h = &hdev->conn_hash;
536 struct hci_conn *c;
537
538 rcu_read_lock();
539
540 list_for_each_entry_rcu(c, &h->list, list) {
541 if (c->type == type && !bacmp(&c->dst, ba)) {
542 rcu_read_unlock();
543 return c;
544 }
545 }
546
547 rcu_read_unlock();
548
549 return NULL;
550}
551
552static inline struct hci_conn *hci_conn_hash_lookup_state(struct hci_dev *hdev,
553 __u8 type, __u16 state)
554{
555 struct hci_conn_hash *h = &hdev->conn_hash;
556 struct hci_conn *c;
557
558 rcu_read_lock();
559
560 list_for_each_entry_rcu(c, &h->list, list) {
561 if (c->type == type && c->state == state) {
562 rcu_read_unlock();
563 return c;
564 }
565 }
566
567 rcu_read_unlock();
568
569 return NULL;
570}
571
572void hci_disconnect(struct hci_conn *conn, __u8 reason);
573bool hci_setup_sync(struct hci_conn *conn, __u16 handle);
574void hci_sco_setup(struct hci_conn *conn, __u8 status);
575
576struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst);
577int hci_conn_del(struct hci_conn *conn);
578void hci_conn_hash_flush(struct hci_dev *hdev);
579void hci_conn_check_pending(struct hci_dev *hdev);
580
581struct hci_chan *hci_chan_create(struct hci_conn *conn);
582void hci_chan_del(struct hci_chan *chan);
583void hci_chan_list_flush(struct hci_conn *conn);
584struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle);
585
586struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst,
587 __u8 dst_type, __u8 sec_level, __u8 auth_type);
588struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst,
589 __u16 setting);
590int hci_conn_check_link_mode(struct hci_conn *conn);
591int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level);
592int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type);
593int hci_conn_change_link_key(struct hci_conn *conn);
594int hci_conn_switch_role(struct hci_conn *conn, __u8 role);
595
596void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active);
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619static inline void hci_conn_get(struct hci_conn *conn)
620{
621 get_device(&conn->dev);
622}
623
624static inline void hci_conn_put(struct hci_conn *conn)
625{
626 put_device(&conn->dev);
627}
628
629static inline void hci_conn_hold(struct hci_conn *conn)
630{
631 BT_DBG("hcon %p orig refcnt %d", conn, atomic_read(&conn->refcnt));
632
633 atomic_inc(&conn->refcnt);
634 cancel_delayed_work(&conn->disc_work);
635}
636
637static inline void hci_conn_drop(struct hci_conn *conn)
638{
639 BT_DBG("hcon %p orig refcnt %d", conn, atomic_read(&conn->refcnt));
640
641 if (atomic_dec_and_test(&conn->refcnt)) {
642 unsigned long timeo;
643
644 switch (conn->type) {
645 case ACL_LINK:
646 case LE_LINK:
647 del_timer(&conn->idle_timer);
648 if (conn->state == BT_CONNECTED) {
649 timeo = conn->disc_timeout;
650 if (!conn->out)
651 timeo *= 2;
652 } else {
653 timeo = msecs_to_jiffies(10);
654 }
655 break;
656
657 case AMP_LINK:
658 timeo = conn->disc_timeout;
659 break;
660
661 default:
662 timeo = msecs_to_jiffies(10);
663 break;
664 }
665
666 cancel_delayed_work(&conn->disc_work);
667 queue_delayed_work(conn->hdev->workqueue,
668 &conn->disc_work, timeo);
669 }
670}
671
672
673static inline void hci_dev_put(struct hci_dev *d)
674{
675 BT_DBG("%s orig refcnt %d", d->name,
676 atomic_read(&d->dev.kobj.kref.refcount));
677
678 put_device(&d->dev);
679}
680
681static inline struct hci_dev *hci_dev_hold(struct hci_dev *d)
682{
683 BT_DBG("%s orig refcnt %d", d->name,
684 atomic_read(&d->dev.kobj.kref.refcount));
685
686 get_device(&d->dev);
687 return d;
688}
689
690#define hci_dev_lock(d) mutex_lock(&d->lock)
691#define hci_dev_unlock(d) mutex_unlock(&d->lock)
692
693#define to_hci_dev(d) container_of(d, struct hci_dev, dev)
694#define to_hci_conn(c) container_of(c, struct hci_conn, dev)
695
696static inline void *hci_get_drvdata(struct hci_dev *hdev)
697{
698 return dev_get_drvdata(&hdev->dev);
699}
700
701static inline void hci_set_drvdata(struct hci_dev *hdev, void *data)
702{
703 dev_set_drvdata(&hdev->dev, data);
704}
705
706
707static inline uint8_t __hci_num_ctrl(void)
708{
709 uint8_t count = 0;
710 struct list_head *p;
711
712 list_for_each(p, &hci_dev_list) {
713 count++;
714 }
715
716 return count;
717}
718
719struct hci_dev *hci_dev_get(int index);
720struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src);
721
722struct hci_dev *hci_alloc_dev(void);
723void hci_free_dev(struct hci_dev *hdev);
724int hci_register_dev(struct hci_dev *hdev);
725void hci_unregister_dev(struct hci_dev *hdev);
726int hci_suspend_dev(struct hci_dev *hdev);
727int hci_resume_dev(struct hci_dev *hdev);
728int hci_dev_open(__u16 dev);
729int hci_dev_close(__u16 dev);
730int hci_dev_reset(__u16 dev);
731int hci_dev_reset_stat(__u16 dev);
732int hci_dev_cmd(unsigned int cmd, void __user *arg);
733int hci_get_dev_list(void __user *arg);
734int hci_get_dev_info(void __user *arg);
735int hci_get_conn_list(void __user *arg);
736int hci_get_conn_info(struct hci_dev *hdev, void __user *arg);
737int hci_get_auth_info(struct hci_dev *hdev, void __user *arg);
738int hci_inquiry(void __user *arg);
739
740struct bdaddr_list *hci_blacklist_lookup(struct hci_dev *hdev,
741 bdaddr_t *bdaddr);
742int hci_blacklist_clear(struct hci_dev *hdev);
743int hci_blacklist_add(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
744int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
745
746int hci_uuids_clear(struct hci_dev *hdev);
747
748int hci_link_keys_clear(struct hci_dev *hdev);
749struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
750int hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, int new_key,
751 bdaddr_t *bdaddr, u8 *val, u8 type, u8 pin_len);
752struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, u8 rand[8]);
753int hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type, u8 type,
754 int new_key, u8 authenticated, u8 tk[16], u8 enc_size,
755 __le16 ediv, u8 rand[8]);
756struct smp_ltk *hci_find_ltk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr,
757 u8 addr_type);
758int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr);
759int hci_smp_ltks_clear(struct hci_dev *hdev);
760int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
761
762int hci_remote_oob_data_clear(struct hci_dev *hdev);
763struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev,
764 bdaddr_t *bdaddr);
765int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 *hash,
766 u8 *randomizer);
767int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr);
768
769void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb);
770
771int hci_recv_frame(struct sk_buff *skb);
772int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count);
773int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count);
774
775void hci_init_sysfs(struct hci_dev *hdev);
776int hci_add_sysfs(struct hci_dev *hdev);
777void hci_del_sysfs(struct hci_dev *hdev);
778void hci_conn_init_sysfs(struct hci_conn *conn);
779void hci_conn_add_sysfs(struct hci_conn *conn);
780void hci_conn_del_sysfs(struct hci_conn *conn);
781
782#define SET_HCIDEV_DEV(hdev, pdev) ((hdev)->dev.parent = (pdev))
783
784
785#define lmp_encrypt_capable(dev) ((dev)->features[0][0] & LMP_ENCRYPT)
786#define lmp_rswitch_capable(dev) ((dev)->features[0][0] & LMP_RSWITCH)
787#define lmp_hold_capable(dev) ((dev)->features[0][0] & LMP_HOLD)
788#define lmp_sniff_capable(dev) ((dev)->features[0][0] & LMP_SNIFF)
789#define lmp_park_capable(dev) ((dev)->features[0][1] & LMP_PARK)
790#define lmp_inq_rssi_capable(dev) ((dev)->features[0][3] & LMP_RSSI_INQ)
791#define lmp_esco_capable(dev) ((dev)->features[0][3] & LMP_ESCO)
792#define lmp_bredr_capable(dev) (!((dev)->features[0][4] & LMP_NO_BREDR))
793#define lmp_le_capable(dev) ((dev)->features[0][4] & LMP_LE)
794#define lmp_sniffsubr_capable(dev) ((dev)->features[0][5] & LMP_SNIFF_SUBR)
795#define lmp_pause_enc_capable(dev) ((dev)->features[0][5] & LMP_PAUSE_ENC)
796#define lmp_ext_inq_capable(dev) ((dev)->features[0][6] & LMP_EXT_INQ)
797#define lmp_le_br_capable(dev) (!!((dev)->features[0][6] & LMP_SIMUL_LE_BR))
798#define lmp_ssp_capable(dev) ((dev)->features[0][6] & LMP_SIMPLE_PAIR)
799#define lmp_no_flush_capable(dev) ((dev)->features[0][6] & LMP_NO_FLUSH)
800#define lmp_lsto_capable(dev) ((dev)->features[0][7] & LMP_LSTO)
801#define lmp_inq_tx_pwr_capable(dev) ((dev)->features[0][7] & LMP_INQ_TX_PWR)
802#define lmp_ext_feat_capable(dev) ((dev)->features[0][7] & LMP_EXTFEATURES)
803#define lmp_transp_capable(dev) ((dev)->features[0][2] & LMP_TRANSPARENT)
804
805
806#define lmp_host_ssp_capable(dev) ((dev)->features[1][0] & LMP_HOST_SSP)
807#define lmp_host_le_capable(dev) (!!((dev)->features[1][0] & LMP_HOST_LE))
808#define lmp_host_le_br_capable(dev) (!!((dev)->features[1][0] & LMP_HOST_LE_BREDR))
809
810
811static inline bool hci_amp_capable(void)
812{
813 struct hci_dev *hdev;
814 bool ret = false;
815
816 read_lock(&hci_dev_list_lock);
817 list_for_each_entry(hdev, &hci_dev_list, list)
818 if (hdev->amp_type == HCI_AMP &&
819 test_bit(HCI_UP, &hdev->flags))
820 ret = true;
821 read_unlock(&hci_dev_list_lock);
822
823 return ret;
824}
825
826
827#define HCI_PROTO_DEFER 0x01
828
829static inline int hci_proto_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr,
830 __u8 type, __u8 *flags)
831{
832 switch (type) {
833 case ACL_LINK:
834 return l2cap_connect_ind(hdev, bdaddr);
835
836 case SCO_LINK:
837 case ESCO_LINK:
838 return sco_connect_ind(hdev, bdaddr, flags);
839
840 default:
841 BT_ERR("unknown link type %d", type);
842 return -EINVAL;
843 }
844}
845
846static inline void hci_proto_connect_cfm(struct hci_conn *conn, __u8 status)
847{
848 switch (conn->type) {
849 case ACL_LINK:
850 case LE_LINK:
851 l2cap_connect_cfm(conn, status);
852 break;
853
854 case SCO_LINK:
855 case ESCO_LINK:
856 sco_connect_cfm(conn, status);
857 break;
858
859 default:
860 BT_ERR("unknown link type %d", conn->type);
861 break;
862 }
863
864 if (conn->connect_cfm_cb)
865 conn->connect_cfm_cb(conn, status);
866}
867
868static inline int hci_proto_disconn_ind(struct hci_conn *conn)
869{
870 if (conn->type != ACL_LINK && conn->type != LE_LINK)
871 return HCI_ERROR_REMOTE_USER_TERM;
872
873 return l2cap_disconn_ind(conn);
874}
875
876static inline void hci_proto_disconn_cfm(struct hci_conn *conn, __u8 reason)
877{
878 switch (conn->type) {
879 case ACL_LINK:
880 case LE_LINK:
881 l2cap_disconn_cfm(conn, reason);
882 break;
883
884 case SCO_LINK:
885 case ESCO_LINK:
886 sco_disconn_cfm(conn, reason);
887 break;
888
889
890 case AMP_LINK:
891 break;
892
893 default:
894 BT_ERR("unknown link type %d", conn->type);
895 break;
896 }
897
898 if (conn->disconn_cfm_cb)
899 conn->disconn_cfm_cb(conn, reason);
900}
901
902static inline void hci_proto_auth_cfm(struct hci_conn *conn, __u8 status)
903{
904 __u8 encrypt;
905
906 if (conn->type != ACL_LINK && conn->type != LE_LINK)
907 return;
908
909 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
910 return;
911
912 encrypt = (conn->link_mode & HCI_LM_ENCRYPT) ? 0x01 : 0x00;
913 l2cap_security_cfm(conn, status, encrypt);
914
915 if (conn->security_cfm_cb)
916 conn->security_cfm_cb(conn, status);
917}
918
919static inline void hci_proto_encrypt_cfm(struct hci_conn *conn, __u8 status,
920 __u8 encrypt)
921{
922 if (conn->type != ACL_LINK && conn->type != LE_LINK)
923 return;
924
925 l2cap_security_cfm(conn, status, encrypt);
926
927 if (conn->security_cfm_cb)
928 conn->security_cfm_cb(conn, status);
929}
930
931
932struct hci_cb {
933 struct list_head list;
934
935 char *name;
936
937 void (*security_cfm) (struct hci_conn *conn, __u8 status,
938 __u8 encrypt);
939 void (*key_change_cfm) (struct hci_conn *conn, __u8 status);
940 void (*role_switch_cfm) (struct hci_conn *conn, __u8 status, __u8 role);
941};
942
943static inline void hci_auth_cfm(struct hci_conn *conn, __u8 status)
944{
945 struct hci_cb *cb;
946 __u8 encrypt;
947
948 hci_proto_auth_cfm(conn, status);
949
950 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
951 return;
952
953 encrypt = (conn->link_mode & HCI_LM_ENCRYPT) ? 0x01 : 0x00;
954
955 read_lock(&hci_cb_list_lock);
956 list_for_each_entry(cb, &hci_cb_list, list) {
957 if (cb->security_cfm)
958 cb->security_cfm(conn, status, encrypt);
959 }
960 read_unlock(&hci_cb_list_lock);
961}
962
963static inline void hci_encrypt_cfm(struct hci_conn *conn, __u8 status,
964 __u8 encrypt)
965{
966 struct hci_cb *cb;
967
968 if (conn->sec_level == BT_SECURITY_SDP)
969 conn->sec_level = BT_SECURITY_LOW;
970
971 if (conn->pending_sec_level > conn->sec_level)
972 conn->sec_level = conn->pending_sec_level;
973
974 hci_proto_encrypt_cfm(conn, status, encrypt);
975
976 read_lock(&hci_cb_list_lock);
977 list_for_each_entry(cb, &hci_cb_list, list) {
978 if (cb->security_cfm)
979 cb->security_cfm(conn, status, encrypt);
980 }
981 read_unlock(&hci_cb_list_lock);
982}
983
984static inline void hci_key_change_cfm(struct hci_conn *conn, __u8 status)
985{
986 struct hci_cb *cb;
987
988 read_lock(&hci_cb_list_lock);
989 list_for_each_entry(cb, &hci_cb_list, list) {
990 if (cb->key_change_cfm)
991 cb->key_change_cfm(conn, status);
992 }
993 read_unlock(&hci_cb_list_lock);
994}
995
996static inline void hci_role_switch_cfm(struct hci_conn *conn, __u8 status,
997 __u8 role)
998{
999 struct hci_cb *cb;
1000
1001 read_lock(&hci_cb_list_lock);
1002 list_for_each_entry(cb, &hci_cb_list, list) {
1003 if (cb->role_switch_cfm)
1004 cb->role_switch_cfm(conn, status, role);
1005 }
1006 read_unlock(&hci_cb_list_lock);
1007}
1008
1009static inline bool eir_has_data_type(u8 *data, size_t data_len, u8 type)
1010{
1011 size_t parsed = 0;
1012
1013 if (data_len < 2)
1014 return false;
1015
1016 while (parsed < data_len - 1) {
1017 u8 field_len = data[0];
1018
1019 if (field_len == 0)
1020 break;
1021
1022 parsed += field_len + 1;
1023
1024 if (parsed > data_len)
1025 break;
1026
1027 if (data[1] == type)
1028 return true;
1029
1030 data += field_len + 1;
1031 }
1032
1033 return false;
1034}
1035
1036static inline size_t eir_get_length(u8 *eir, size_t eir_len)
1037{
1038 size_t parsed = 0;
1039
1040 while (parsed < eir_len) {
1041 u8 field_len = eir[0];
1042
1043 if (field_len == 0)
1044 return parsed;
1045
1046 parsed += field_len + 1;
1047 eir += field_len + 1;
1048 }
1049
1050 return eir_len;
1051}
1052
1053static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data,
1054 u8 data_len)
1055{
1056 eir[eir_len++] = sizeof(type) + data_len;
1057 eir[eir_len++] = type;
1058 memcpy(&eir[eir_len], data, data_len);
1059 eir_len += data_len;
1060
1061 return eir_len;
1062}
1063
1064int hci_register_cb(struct hci_cb *hcb);
1065int hci_unregister_cb(struct hci_cb *hcb);
1066
1067struct hci_request {
1068 struct hci_dev *hdev;
1069 struct sk_buff_head cmd_q;
1070
1071
1072
1073
1074 int err;
1075};
1076
1077void hci_req_init(struct hci_request *req, struct hci_dev *hdev);
1078int hci_req_run(struct hci_request *req, hci_req_complete_t complete);
1079void hci_req_add(struct hci_request *req, u16 opcode, u32 plen,
1080 const void *param);
1081void hci_req_add_ev(struct hci_request *req, u16 opcode, u32 plen,
1082 const void *param, u8 event);
1083void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status);
1084
1085struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
1086 const void *param, u32 timeout);
1087struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
1088 const void *param, u8 event, u32 timeout);
1089
1090int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen,
1091 const void *param);
1092void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags);
1093void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb);
1094
1095void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode);
1096
1097
1098void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb);
1099void hci_send_to_control(struct sk_buff *skb, struct sock *skip_sk);
1100void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb);
1101
1102void hci_sock_dev_event(struct hci_dev *hdev, int event);
1103
1104
1105#define DISCOV_TYPE_BREDR (BIT(BDADDR_BREDR))
1106#define DISCOV_TYPE_LE (BIT(BDADDR_LE_PUBLIC) | \
1107 BIT(BDADDR_LE_RANDOM))
1108#define DISCOV_TYPE_INTERLEAVED (BIT(BDADDR_BREDR) | \
1109 BIT(BDADDR_LE_PUBLIC) | \
1110 BIT(BDADDR_LE_RANDOM))
1111
1112
1113
1114
1115#define DISCOV_LE_SCAN_WIN 0x12
1116#define DISCOV_LE_SCAN_INT 0x12
1117#define DISCOV_LE_TIMEOUT msecs_to_jiffies(10240)
1118#define DISCOV_INTERLEAVED_TIMEOUT msecs_to_jiffies(5120)
1119#define DISCOV_INTERLEAVED_INQUIRY_LEN 0x04
1120#define DISCOV_BREDR_INQUIRY_LEN 0x08
1121
1122int mgmt_control(struct sock *sk, struct msghdr *msg, size_t len);
1123int mgmt_index_added(struct hci_dev *hdev);
1124int mgmt_index_removed(struct hci_dev *hdev);
1125int mgmt_set_powered_failed(struct hci_dev *hdev, int err);
1126int mgmt_powered(struct hci_dev *hdev, u8 powered);
1127int mgmt_discoverable(struct hci_dev *hdev, u8 discoverable);
1128int mgmt_connectable(struct hci_dev *hdev, u8 connectable);
1129int mgmt_write_scan_failed(struct hci_dev *hdev, u8 scan, u8 status);
1130int mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
1131 bool persistent);
1132int mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
1133 u8 addr_type, u32 flags, u8 *name, u8 name_len,
1134 u8 *dev_class);
1135int mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
1136 u8 link_type, u8 addr_type, u8 reason);
1137int mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
1138 u8 link_type, u8 addr_type, u8 status);
1139int mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
1140 u8 addr_type, u8 status);
1141int mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure);
1142int mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1143 u8 status);
1144int mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1145 u8 status);
1146int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
1147 u8 link_type, u8 addr_type, __le32 value,
1148 u8 confirm_hint);
1149int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1150 u8 link_type, u8 addr_type, u8 status);
1151int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1152 u8 link_type, u8 addr_type, u8 status);
1153int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
1154 u8 link_type, u8 addr_type);
1155int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1156 u8 link_type, u8 addr_type, u8 status);
1157int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
1158 u8 link_type, u8 addr_type, u8 status);
1159int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
1160 u8 link_type, u8 addr_type, u32 passkey,
1161 u8 entered);
1162int mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
1163 u8 addr_type, u8 status);
1164int mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status);
1165int mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status);
1166int mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
1167 u8 status);
1168int mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status);
1169int mgmt_read_local_oob_data_reply_complete(struct hci_dev *hdev, u8 *hash,
1170 u8 *randomizer, u8 status);
1171int mgmt_le_enable_complete(struct hci_dev *hdev, u8 enable, u8 status);
1172int mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
1173 u8 addr_type, u8 *dev_class, s8 rssi, u8 cfm_name,
1174 u8 ssp, u8 *eir, u16 eir_len);
1175int mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
1176 u8 addr_type, s8 rssi, u8 *name, u8 name_len);
1177int mgmt_discovering(struct hci_dev *hdev, u8 discovering);
1178int mgmt_device_blocked(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
1179int mgmt_device_unblocked(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
1180bool mgmt_valid_hdev(struct hci_dev *hdev);
1181int mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, u8 persistent);
1182
1183
1184#define hci_pi(sk) ((struct hci_pinfo *) sk)
1185
1186struct hci_pinfo {
1187 struct bt_sock bt;
1188 struct hci_dev *hdev;
1189 struct hci_filter filter;
1190 __u32 cmsg_mask;
1191 unsigned short channel;
1192};
1193
1194
1195#define HCI_SFLT_MAX_OGF 5
1196
1197struct hci_sec_filter {
1198 __u32 type_mask;
1199 __u32 event_mask[2];
1200 __u32 ocf_mask[HCI_SFLT_MAX_OGF + 1][4];
1201};
1202
1203
1204#define HCI_REQ_DONE 0
1205#define HCI_REQ_PEND 1
1206#define HCI_REQ_CANCELED 2
1207
1208#define hci_req_lock(d) mutex_lock(&d->req_lock)
1209#define hci_req_unlock(d) mutex_unlock(&d->req_lock)
1210
1211void hci_update_ad(struct hci_request *req);
1212
1213void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max,
1214 u16 latency, u16 to_multiplier);
1215void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8],
1216 __u8 ltk[16]);
1217
1218u8 bdaddr_to_le(u8 bdaddr_type);
1219
1220#define SCO_AIRMODE_MASK 0x0003
1221#define SCO_AIRMODE_CVSD 0x0000
1222#define SCO_AIRMODE_TRANSP 0x0003
1223
1224#endif
1225