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_PROTO_L2CAP 0
32#define HCI_PROTO_SCO 1
33
34
35struct inquiry_data {
36 bdaddr_t bdaddr;
37 __u8 pscan_rep_mode;
38 __u8 pscan_period_mode;
39 __u8 pscan_mode;
40 __u8 dev_class[3];
41 __le16 clock_offset;
42 __s8 rssi;
43 __u8 ssp_mode;
44};
45
46struct inquiry_entry {
47 struct inquiry_entry *next;
48 __u32 timestamp;
49 struct inquiry_data data;
50};
51
52struct inquiry_cache {
53 spinlock_t lock;
54 __u32 timestamp;
55 struct inquiry_entry *list;
56};
57
58struct hci_conn_hash {
59 struct list_head list;
60 spinlock_t lock;
61 unsigned int acl_num;
62 unsigned int sco_num;
63};
64
65struct hci_dev {
66 struct list_head list;
67 spinlock_t lock;
68 atomic_t refcnt;
69
70 char name[8];
71 unsigned long flags;
72 __u16 id;
73 __u8 type;
74 bdaddr_t bdaddr;
75 __u8 dev_name[248];
76 __u8 dev_class[3];
77 __u8 features[8];
78 __u8 commands[64];
79 __u8 ssp_mode;
80 __u8 hci_ver;
81 __u16 hci_rev;
82 __u16 manufacturer;
83 __u16 voice_setting;
84
85 __u16 pkt_type;
86 __u16 esco_type;
87 __u16 link_policy;
88 __u16 link_mode;
89
90 __u32 idle_timeout;
91 __u16 sniff_min_interval;
92 __u16 sniff_max_interval;
93
94 unsigned long quirks;
95
96 atomic_t cmd_cnt;
97 unsigned int acl_cnt;
98 unsigned int sco_cnt;
99
100 unsigned int acl_mtu;
101 unsigned int sco_mtu;
102 unsigned int acl_pkts;
103 unsigned int sco_pkts;
104
105 unsigned long cmd_last_tx;
106 unsigned long acl_last_tx;
107 unsigned long sco_last_tx;
108
109 struct tasklet_struct cmd_task;
110 struct tasklet_struct rx_task;
111 struct tasklet_struct tx_task;
112
113 struct sk_buff_head rx_q;
114 struct sk_buff_head raw_q;
115 struct sk_buff_head cmd_q;
116
117 struct sk_buff *sent_cmd;
118 struct sk_buff *reassembly[3];
119
120 struct semaphore req_lock;
121 wait_queue_head_t req_wait_q;
122 __u32 req_status;
123 __u32 req_result;
124
125 struct inquiry_cache inq_cache;
126 struct hci_conn_hash conn_hash;
127
128 struct hci_dev_stats stat;
129
130 struct sk_buff_head driver_init;
131
132 void *driver_data;
133 void *core_data;
134
135 atomic_t promisc;
136
137 struct device *parent;
138 struct device dev;
139
140 struct rfkill *rfkill;
141
142 struct module *owner;
143
144 int (*open)(struct hci_dev *hdev);
145 int (*close)(struct hci_dev *hdev);
146 int (*flush)(struct hci_dev *hdev);
147 int (*send)(struct sk_buff *skb);
148 void (*destruct)(struct hci_dev *hdev);
149 void (*notify)(struct hci_dev *hdev, unsigned int evt);
150 int (*ioctl)(struct hci_dev *hdev, unsigned int cmd, unsigned long arg);
151};
152
153struct hci_conn {
154 struct list_head list;
155
156 atomic_t refcnt;
157 spinlock_t lock;
158
159 bdaddr_t dst;
160 __u16 handle;
161 __u16 state;
162 __u8 mode;
163 __u8 type;
164 __u8 out;
165 __u8 attempt;
166 __u8 dev_class[3];
167 __u8 features[8];
168 __u8 ssp_mode;
169 __u16 interval;
170 __u16 pkt_type;
171 __u16 link_policy;
172 __u32 link_mode;
173 __u8 auth_type;
174 __u8 sec_level;
175 __u8 power_save;
176 __u16 disc_timeout;
177 unsigned long pend;
178
179 unsigned int sent;
180
181 struct sk_buff_head data_q;
182
183 struct timer_list disc_timer;
184 struct timer_list idle_timer;
185
186 struct work_struct work_add;
187 struct work_struct work_del;
188
189 struct device dev;
190
191 struct hci_dev *hdev;
192 void *l2cap_data;
193 void *sco_data;
194 void *priv;
195
196 struct hci_conn *link;
197};
198
199extern struct hci_proto *hci_proto[];
200extern struct list_head hci_dev_list;
201extern struct list_head hci_cb_list;
202extern rwlock_t hci_dev_list_lock;
203extern rwlock_t hci_cb_list_lock;
204
205
206#define INQUIRY_CACHE_AGE_MAX (HZ*30)
207#define INQUIRY_ENTRY_AGE_MAX (HZ*60)
208
209#define inquiry_cache_lock(c) spin_lock(&c->lock)
210#define inquiry_cache_unlock(c) spin_unlock(&c->lock)
211#define inquiry_cache_lock_bh(c) spin_lock_bh(&c->lock)
212#define inquiry_cache_unlock_bh(c) spin_unlock_bh(&c->lock)
213
214static inline void inquiry_cache_init(struct hci_dev *hdev)
215{
216 struct inquiry_cache *c = &hdev->inq_cache;
217 spin_lock_init(&c->lock);
218 c->list = NULL;
219}
220
221static inline int inquiry_cache_empty(struct hci_dev *hdev)
222{
223 struct inquiry_cache *c = &hdev->inq_cache;
224 return (c->list == NULL);
225}
226
227static inline long inquiry_cache_age(struct hci_dev *hdev)
228{
229 struct inquiry_cache *c = &hdev->inq_cache;
230 return jiffies - c->timestamp;
231}
232
233static inline long inquiry_entry_age(struct inquiry_entry *e)
234{
235 return jiffies - e->timestamp;
236}
237
238struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr);
239void hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data);
240
241
242enum {
243 HCI_CONN_AUTH_PEND,
244 HCI_CONN_ENCRYPT_PEND,
245 HCI_CONN_RSWITCH_PEND,
246 HCI_CONN_MODE_CHANGE_PEND,
247};
248
249static inline void hci_conn_hash_init(struct hci_dev *hdev)
250{
251 struct hci_conn_hash *h = &hdev->conn_hash;
252 INIT_LIST_HEAD(&h->list);
253 spin_lock_init(&h->lock);
254 h->acl_num = 0;
255 h->sco_num = 0;
256}
257
258static inline void hci_conn_hash_add(struct hci_dev *hdev, struct hci_conn *c)
259{
260 struct hci_conn_hash *h = &hdev->conn_hash;
261 list_add(&c->list, &h->list);
262 if (c->type == ACL_LINK)
263 h->acl_num++;
264 else
265 h->sco_num++;
266}
267
268static inline void hci_conn_hash_del(struct hci_dev *hdev, struct hci_conn *c)
269{
270 struct hci_conn_hash *h = &hdev->conn_hash;
271 list_del(&c->list);
272 if (c->type == ACL_LINK)
273 h->acl_num--;
274 else
275 h->sco_num--;
276}
277
278static inline struct hci_conn *hci_conn_hash_lookup_handle(struct hci_dev *hdev,
279 __u16 handle)
280{
281 struct hci_conn_hash *h = &hdev->conn_hash;
282 struct list_head *p;
283 struct hci_conn *c;
284
285 list_for_each(p, &h->list) {
286 c = list_entry(p, struct hci_conn, list);
287 if (c->handle == handle)
288 return c;
289 }
290 return NULL;
291}
292
293static inline struct hci_conn *hci_conn_hash_lookup_ba(struct hci_dev *hdev,
294 __u8 type, bdaddr_t *ba)
295{
296 struct hci_conn_hash *h = &hdev->conn_hash;
297 struct list_head *p;
298 struct hci_conn *c;
299
300 list_for_each(p, &h->list) {
301 c = list_entry(p, struct hci_conn, list);
302 if (c->type == type && !bacmp(&c->dst, ba))
303 return c;
304 }
305 return NULL;
306}
307
308static inline struct hci_conn *hci_conn_hash_lookup_state(struct hci_dev *hdev,
309 __u8 type, __u16 state)
310{
311 struct hci_conn_hash *h = &hdev->conn_hash;
312 struct list_head *p;
313 struct hci_conn *c;
314
315 list_for_each(p, &h->list) {
316 c = list_entry(p, struct hci_conn, list);
317 if (c->type == type && c->state == state)
318 return c;
319 }
320 return NULL;
321}
322
323void hci_acl_connect(struct hci_conn *conn);
324void hci_acl_disconn(struct hci_conn *conn, __u8 reason);
325void hci_add_sco(struct hci_conn *conn, __u16 handle);
326void hci_setup_sync(struct hci_conn *conn, __u16 handle);
327
328struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst);
329int hci_conn_del(struct hci_conn *conn);
330void hci_conn_hash_flush(struct hci_dev *hdev);
331void hci_conn_check_pending(struct hci_dev *hdev);
332
333struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst, __u8 sec_level, __u8 auth_type);
334int hci_conn_check_link_mode(struct hci_conn *conn);
335int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type);
336int hci_conn_change_link_key(struct hci_conn *conn);
337int hci_conn_switch_role(struct hci_conn *conn, __u8 role);
338
339void hci_conn_enter_active_mode(struct hci_conn *conn);
340void hci_conn_enter_sniff_mode(struct hci_conn *conn);
341
342static inline void hci_conn_hold(struct hci_conn *conn)
343{
344 atomic_inc(&conn->refcnt);
345 del_timer(&conn->disc_timer);
346}
347
348static inline void hci_conn_put(struct hci_conn *conn)
349{
350 if (atomic_dec_and_test(&conn->refcnt)) {
351 unsigned long timeo;
352 if (conn->type == ACL_LINK) {
353 del_timer(&conn->idle_timer);
354 if (conn->state == BT_CONNECTED) {
355 timeo = msecs_to_jiffies(conn->disc_timeout);
356 if (!conn->out)
357 timeo *= 2;
358 } else
359 timeo = msecs_to_jiffies(10);
360 } else
361 timeo = msecs_to_jiffies(10);
362 mod_timer(&conn->disc_timer, jiffies + timeo);
363 }
364}
365
366
367static inline void hci_sched_cmd(struct hci_dev *hdev)
368{
369 tasklet_schedule(&hdev->cmd_task);
370}
371
372static inline void hci_sched_rx(struct hci_dev *hdev)
373{
374 tasklet_schedule(&hdev->rx_task);
375}
376
377static inline void hci_sched_tx(struct hci_dev *hdev)
378{
379 tasklet_schedule(&hdev->tx_task);
380}
381
382
383static inline void __hci_dev_put(struct hci_dev *d)
384{
385 if (atomic_dec_and_test(&d->refcnt))
386 d->destruct(d);
387}
388
389static inline void hci_dev_put(struct hci_dev *d)
390{
391 __hci_dev_put(d);
392 module_put(d->owner);
393}
394
395static inline struct hci_dev *__hci_dev_hold(struct hci_dev *d)
396{
397 atomic_inc(&d->refcnt);
398 return d;
399}
400
401static inline struct hci_dev *hci_dev_hold(struct hci_dev *d)
402{
403 if (try_module_get(d->owner))
404 return __hci_dev_hold(d);
405 return NULL;
406}
407
408#define hci_dev_lock(d) spin_lock(&d->lock)
409#define hci_dev_unlock(d) spin_unlock(&d->lock)
410#define hci_dev_lock_bh(d) spin_lock_bh(&d->lock)
411#define hci_dev_unlock_bh(d) spin_unlock_bh(&d->lock)
412
413struct hci_dev *hci_dev_get(int index);
414struct hci_dev *hci_get_route(bdaddr_t *src, bdaddr_t *dst);
415
416struct hci_dev *hci_alloc_dev(void);
417void hci_free_dev(struct hci_dev *hdev);
418int hci_register_dev(struct hci_dev *hdev);
419int hci_unregister_dev(struct hci_dev *hdev);
420int hci_suspend_dev(struct hci_dev *hdev);
421int hci_resume_dev(struct hci_dev *hdev);
422int hci_dev_open(__u16 dev);
423int hci_dev_close(__u16 dev);
424int hci_dev_reset(__u16 dev);
425int hci_dev_reset_stat(__u16 dev);
426int hci_dev_cmd(unsigned int cmd, void __user *arg);
427int hci_get_dev_list(void __user *arg);
428int hci_get_dev_info(void __user *arg);
429int hci_get_conn_list(void __user *arg);
430int hci_get_conn_info(struct hci_dev *hdev, void __user *arg);
431int hci_get_auth_info(struct hci_dev *hdev, void __user *arg);
432int hci_inquiry(void __user *arg);
433
434void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb);
435
436
437static inline int hci_recv_frame(struct sk_buff *skb)
438{
439 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
440 if (!hdev || (!test_bit(HCI_UP, &hdev->flags)
441 && !test_bit(HCI_INIT, &hdev->flags))) {
442 kfree_skb(skb);
443 return -ENXIO;
444 }
445
446
447 bt_cb(skb)->incoming = 1;
448
449
450 __net_timestamp(skb);
451
452
453 skb_queue_tail(&hdev->rx_q, skb);
454 hci_sched_rx(hdev);
455 return 0;
456}
457
458int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count);
459
460int hci_register_sysfs(struct hci_dev *hdev);
461void hci_unregister_sysfs(struct hci_dev *hdev);
462void hci_conn_init_sysfs(struct hci_conn *conn);
463void hci_conn_add_sysfs(struct hci_conn *conn);
464void hci_conn_del_sysfs(struct hci_conn *conn);
465
466#define SET_HCIDEV_DEV(hdev, pdev) ((hdev)->parent = (pdev))
467
468
469#define lmp_rswitch_capable(dev) ((dev)->features[0] & LMP_RSWITCH)
470#define lmp_encrypt_capable(dev) ((dev)->features[0] & LMP_ENCRYPT)
471#define lmp_sniff_capable(dev) ((dev)->features[0] & LMP_SNIFF)
472#define lmp_sniffsubr_capable(dev) ((dev)->features[5] & LMP_SNIFF_SUBR)
473#define lmp_esco_capable(dev) ((dev)->features[3] & LMP_ESCO)
474#define lmp_ssp_capable(dev) ((dev)->features[6] & LMP_SIMPLE_PAIR)
475
476
477struct hci_proto {
478 char *name;
479 unsigned int id;
480 unsigned long flags;
481
482 void *priv;
483
484 int (*connect_ind) (struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type);
485 int (*connect_cfm) (struct hci_conn *conn, __u8 status);
486 int (*disconn_ind) (struct hci_conn *conn);
487 int (*disconn_cfm) (struct hci_conn *conn, __u8 reason);
488 int (*recv_acldata) (struct hci_conn *conn, struct sk_buff *skb, __u16 flags);
489 int (*recv_scodata) (struct hci_conn *conn, struct sk_buff *skb);
490 int (*security_cfm) (struct hci_conn *conn, __u8 status, __u8 encrypt);
491};
492
493static inline int hci_proto_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type)
494{
495 register struct hci_proto *hp;
496 int mask = 0;
497
498 hp = hci_proto[HCI_PROTO_L2CAP];
499 if (hp && hp->connect_ind)
500 mask |= hp->connect_ind(hdev, bdaddr, type);
501
502 hp = hci_proto[HCI_PROTO_SCO];
503 if (hp && hp->connect_ind)
504 mask |= hp->connect_ind(hdev, bdaddr, type);
505
506 return mask;
507}
508
509static inline void hci_proto_connect_cfm(struct hci_conn *conn, __u8 status)
510{
511 register struct hci_proto *hp;
512
513 hp = hci_proto[HCI_PROTO_L2CAP];
514 if (hp && hp->connect_cfm)
515 hp->connect_cfm(conn, status);
516
517 hp = hci_proto[HCI_PROTO_SCO];
518 if (hp && hp->connect_cfm)
519 hp->connect_cfm(conn, status);
520}
521
522static inline int hci_proto_disconn_ind(struct hci_conn *conn)
523{
524 register struct hci_proto *hp;
525 int reason = 0x13;
526
527 hp = hci_proto[HCI_PROTO_L2CAP];
528 if (hp && hp->disconn_ind)
529 reason = hp->disconn_ind(conn);
530
531 hp = hci_proto[HCI_PROTO_SCO];
532 if (hp && hp->disconn_ind)
533 reason = hp->disconn_ind(conn);
534
535 return reason;
536}
537
538static inline void hci_proto_disconn_cfm(struct hci_conn *conn, __u8 reason)
539{
540 register struct hci_proto *hp;
541
542 hp = hci_proto[HCI_PROTO_L2CAP];
543 if (hp && hp->disconn_cfm)
544 hp->disconn_cfm(conn, reason);
545
546 hp = hci_proto[HCI_PROTO_SCO];
547 if (hp && hp->disconn_cfm)
548 hp->disconn_cfm(conn, reason);
549}
550
551static inline void hci_proto_auth_cfm(struct hci_conn *conn, __u8 status)
552{
553 register struct hci_proto *hp;
554 __u8 encrypt;
555
556 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend))
557 return;
558
559 encrypt = (conn->link_mode & HCI_LM_ENCRYPT) ? 0x01 : 0x00;
560
561 hp = hci_proto[HCI_PROTO_L2CAP];
562 if (hp && hp->security_cfm)
563 hp->security_cfm(conn, status, encrypt);
564
565 hp = hci_proto[HCI_PROTO_SCO];
566 if (hp && hp->security_cfm)
567 hp->security_cfm(conn, status, encrypt);
568}
569
570static inline void hci_proto_encrypt_cfm(struct hci_conn *conn, __u8 status, __u8 encrypt)
571{
572 register struct hci_proto *hp;
573
574 hp = hci_proto[HCI_PROTO_L2CAP];
575 if (hp && hp->security_cfm)
576 hp->security_cfm(conn, status, encrypt);
577
578 hp = hci_proto[HCI_PROTO_SCO];
579 if (hp && hp->security_cfm)
580 hp->security_cfm(conn, status, encrypt);
581}
582
583int hci_register_proto(struct hci_proto *hproto);
584int hci_unregister_proto(struct hci_proto *hproto);
585
586
587struct hci_cb {
588 struct list_head list;
589
590 char *name;
591
592 void (*security_cfm) (struct hci_conn *conn, __u8 status, __u8 encrypt);
593 void (*key_change_cfm) (struct hci_conn *conn, __u8 status);
594 void (*role_switch_cfm) (struct hci_conn *conn, __u8 status, __u8 role);
595};
596
597static inline void hci_auth_cfm(struct hci_conn *conn, __u8 status)
598{
599 struct list_head *p;
600 __u8 encrypt;
601
602 hci_proto_auth_cfm(conn, status);
603
604 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend))
605 return;
606
607 encrypt = (conn->link_mode & HCI_LM_ENCRYPT) ? 0x01 : 0x00;
608
609 read_lock_bh(&hci_cb_list_lock);
610 list_for_each(p, &hci_cb_list) {
611 struct hci_cb *cb = list_entry(p, struct hci_cb, list);
612 if (cb->security_cfm)
613 cb->security_cfm(conn, status, encrypt);
614 }
615 read_unlock_bh(&hci_cb_list_lock);
616}
617
618static inline void hci_encrypt_cfm(struct hci_conn *conn, __u8 status, __u8 encrypt)
619{
620 struct list_head *p;
621
622 if (conn->sec_level == BT_SECURITY_SDP)
623 conn->sec_level = BT_SECURITY_LOW;
624
625 hci_proto_encrypt_cfm(conn, status, encrypt);
626
627 read_lock_bh(&hci_cb_list_lock);
628 list_for_each(p, &hci_cb_list) {
629 struct hci_cb *cb = list_entry(p, struct hci_cb, list);
630 if (cb->security_cfm)
631 cb->security_cfm(conn, status, encrypt);
632 }
633 read_unlock_bh(&hci_cb_list_lock);
634}
635
636static inline void hci_key_change_cfm(struct hci_conn *conn, __u8 status)
637{
638 struct list_head *p;
639
640 read_lock_bh(&hci_cb_list_lock);
641 list_for_each(p, &hci_cb_list) {
642 struct hci_cb *cb = list_entry(p, struct hci_cb, list);
643 if (cb->key_change_cfm)
644 cb->key_change_cfm(conn, status);
645 }
646 read_unlock_bh(&hci_cb_list_lock);
647}
648
649static inline void hci_role_switch_cfm(struct hci_conn *conn, __u8 status, __u8 role)
650{
651 struct list_head *p;
652
653 read_lock_bh(&hci_cb_list_lock);
654 list_for_each(p, &hci_cb_list) {
655 struct hci_cb *cb = list_entry(p, struct hci_cb, list);
656 if (cb->role_switch_cfm)
657 cb->role_switch_cfm(conn, status, role);
658 }
659 read_unlock_bh(&hci_cb_list_lock);
660}
661
662int hci_register_cb(struct hci_cb *hcb);
663int hci_unregister_cb(struct hci_cb *hcb);
664
665int hci_register_notifier(struct notifier_block *nb);
666int hci_unregister_notifier(struct notifier_block *nb);
667
668int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, void *param);
669int hci_send_acl(struct hci_conn *conn, struct sk_buff *skb, __u16 flags);
670int hci_send_sco(struct hci_conn *conn, struct sk_buff *skb);
671
672void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode);
673
674void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data);
675
676
677void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb);
678
679
680#define hci_pi(sk) ((struct hci_pinfo *) sk)
681
682struct hci_pinfo {
683 struct bt_sock bt;
684 struct hci_dev *hdev;
685 struct hci_filter filter;
686 __u32 cmsg_mask;
687};
688
689
690#define HCI_SFLT_MAX_OGF 5
691
692struct hci_sec_filter {
693 __u32 type_mask;
694 __u32 event_mask[2];
695 __u32 ocf_mask[HCI_SFLT_MAX_OGF + 1][4];
696};
697
698
699#define HCI_REQ_DONE 0
700#define HCI_REQ_PEND 1
701#define HCI_REQ_CANCELED 2
702
703#define hci_req_lock(d) down(&d->req_lock)
704#define hci_req_unlock(d) up(&d->req_lock)
705
706void hci_req_complete(struct hci_dev *hdev, int result);
707
708#endif
709