506f25089207d3c73c6dde264b0943421dbf7217
[pandora-kernel.git] / include / net / bluetooth / hci_core.h
1 /*
2    BlueZ - Bluetooth protocol stack for Linux
3    Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
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
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
18    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
22    SOFTWARE IS DISCLAIMED.
23 */
24
25 #ifndef __HCI_CORE_H
26 #define __HCI_CORE_H
27
28 #include <net/bluetooth/hci.h>
29
30 /* HCI upper protocols */
31 #define HCI_PROTO_L2CAP 0
32 #define HCI_PROTO_SCO   1
33
34 /* HCI Core structures */
35 struct 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
46 struct inquiry_entry {
47         struct inquiry_entry    *next;
48         __u32                   timestamp;
49         struct inquiry_data     data;
50 };
51
52 struct inquiry_cache {
53         spinlock_t              lock;
54         __u32                   timestamp;
55         struct inquiry_entry    *list;
56 };
57
58 struct hci_conn_hash {
59         struct list_head list;
60         spinlock_t       lock;
61         unsigned int     acl_num;
62         unsigned int     sco_num;
63         unsigned int     le_num;
64 };
65
66 struct bdaddr_list {
67         struct list_head list;
68         bdaddr_t bdaddr;
69 };
70
71 struct bt_uuid {
72         struct list_head list;
73         u8 uuid[16];
74         u8 svc_hint;
75 };
76
77 struct link_key {
78         struct list_head list;
79         bdaddr_t bdaddr;
80         u8 type;
81         u8 val[16];
82         u8 pin_len;
83 };
84
85 #define NUM_REASSEMBLY 4
86 struct hci_dev {
87         struct list_head list;
88         spinlock_t      lock;
89         atomic_t        refcnt;
90
91         char            name[8];
92         unsigned long   flags;
93         __u16           id;
94         __u8            bus;
95         __u8            dev_type;
96         bdaddr_t        bdaddr;
97         __u8            dev_name[248];
98         __u8            dev_class[3];
99         __u8            major_class;
100         __u8            minor_class;
101         __u8            features[8];
102         __u8            commands[64];
103         __u8            ssp_mode;
104         __u8            hci_ver;
105         __u16           hci_rev;
106         __u8            lmp_ver;
107         __u16           manufacturer;
108         __le16          lmp_subver;
109         __u16           voice_setting;
110         __u8            io_capability;
111
112         __u16           pkt_type;
113         __u16           esco_type;
114         __u16           link_policy;
115         __u16           link_mode;
116
117         __u32           idle_timeout;
118         __u16           sniff_min_interval;
119         __u16           sniff_max_interval;
120
121         unsigned long   quirks;
122
123         atomic_t        cmd_cnt;
124         unsigned int    acl_cnt;
125         unsigned int    sco_cnt;
126         unsigned int    le_cnt;
127
128         unsigned int    acl_mtu;
129         unsigned int    sco_mtu;
130         unsigned int    le_mtu;
131         unsigned int    acl_pkts;
132         unsigned int    sco_pkts;
133         unsigned int    le_pkts;
134
135         unsigned long   acl_last_tx;
136         unsigned long   sco_last_tx;
137         unsigned long   le_last_tx;
138
139         struct workqueue_struct *workqueue;
140
141         struct work_struct      power_on;
142         struct work_struct      power_off;
143         struct timer_list       off_timer;
144
145         struct timer_list       cmd_timer;
146         struct tasklet_struct   cmd_task;
147         struct tasklet_struct   rx_task;
148         struct tasklet_struct   tx_task;
149
150         struct sk_buff_head     rx_q;
151         struct sk_buff_head     raw_q;
152         struct sk_buff_head     cmd_q;
153
154         struct sk_buff          *sent_cmd;
155         struct sk_buff          *reassembly[NUM_REASSEMBLY];
156
157         struct mutex            req_lock;
158         wait_queue_head_t       req_wait_q;
159         __u32                   req_status;
160         __u32                   req_result;
161
162         __u16                   init_last_cmd;
163
164         struct inquiry_cache    inq_cache;
165         struct hci_conn_hash    conn_hash;
166         struct list_head        blacklist;
167
168         struct list_head        uuids;
169
170         struct list_head        link_keys;
171
172         struct hci_dev_stats    stat;
173
174         struct sk_buff_head     driver_init;
175
176         void                    *driver_data;
177         void                    *core_data;
178
179         atomic_t                promisc;
180
181         struct dentry           *debugfs;
182
183         struct device           *parent;
184         struct device           dev;
185
186         struct rfkill           *rfkill;
187
188         struct module           *owner;
189
190         int (*open)(struct hci_dev *hdev);
191         int (*close)(struct hci_dev *hdev);
192         int (*flush)(struct hci_dev *hdev);
193         int (*send)(struct sk_buff *skb);
194         void (*destruct)(struct hci_dev *hdev);
195         void (*notify)(struct hci_dev *hdev, unsigned int evt);
196         int (*ioctl)(struct hci_dev *hdev, unsigned int cmd, unsigned long arg);
197 };
198
199 struct hci_conn {
200         struct list_head list;
201
202         atomic_t        refcnt;
203         spinlock_t      lock;
204
205         bdaddr_t        dst;
206         __u16           handle;
207         __u16           state;
208         __u8            mode;
209         __u8            type;
210         __u8            out;
211         __u8            attempt;
212         __u8            dev_class[3];
213         __u8            features[8];
214         __u8            ssp_mode;
215         __u16           interval;
216         __u16           pkt_type;
217         __u16           link_policy;
218         __u32           link_mode;
219         __u8            auth_type;
220         __u8            sec_level;
221         __u8            pending_sec_level;
222         __u8            pin_length;
223         __u8            io_capability;
224         __u8            power_save;
225         __u16           disc_timeout;
226         unsigned long   pend;
227
228         __u8            remote_cap;
229         __u8            remote_oob;
230         __u8            remote_auth;
231
232         unsigned int    sent;
233
234         struct sk_buff_head data_q;
235
236         struct timer_list disc_timer;
237         struct timer_list idle_timer;
238
239         struct work_struct work_add;
240         struct work_struct work_del;
241
242         struct device   dev;
243         atomic_t        devref;
244
245         struct hci_dev  *hdev;
246         void            *l2cap_data;
247         void            *sco_data;
248         void            *priv;
249
250         struct hci_conn *link;
251
252         void (*connect_cfm_cb)  (struct hci_conn *conn, u8 status);
253         void (*security_cfm_cb) (struct hci_conn *conn, u8 status);
254         void (*disconn_cfm_cb)  (struct hci_conn *conn, u8 reason);
255 };
256
257 extern struct hci_proto *hci_proto[];
258 extern struct list_head hci_dev_list;
259 extern struct list_head hci_cb_list;
260 extern rwlock_t hci_dev_list_lock;
261 extern rwlock_t hci_cb_list_lock;
262
263 /* ----- Inquiry cache ----- */
264 #define INQUIRY_CACHE_AGE_MAX   (HZ*30)   /* 30 seconds */
265 #define INQUIRY_ENTRY_AGE_MAX   (HZ*60)   /* 60 seconds */
266
267 #define inquiry_cache_lock(c)           spin_lock(&c->lock)
268 #define inquiry_cache_unlock(c)         spin_unlock(&c->lock)
269 #define inquiry_cache_lock_bh(c)        spin_lock_bh(&c->lock)
270 #define inquiry_cache_unlock_bh(c)      spin_unlock_bh(&c->lock)
271
272 static inline void inquiry_cache_init(struct hci_dev *hdev)
273 {
274         struct inquiry_cache *c = &hdev->inq_cache;
275         spin_lock_init(&c->lock);
276         c->list = NULL;
277 }
278
279 static inline int inquiry_cache_empty(struct hci_dev *hdev)
280 {
281         struct inquiry_cache *c = &hdev->inq_cache;
282         return c->list == NULL;
283 }
284
285 static inline long inquiry_cache_age(struct hci_dev *hdev)
286 {
287         struct inquiry_cache *c = &hdev->inq_cache;
288         return jiffies - c->timestamp;
289 }
290
291 static inline long inquiry_entry_age(struct inquiry_entry *e)
292 {
293         return jiffies - e->timestamp;
294 }
295
296 struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr);
297 void hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data);
298
299 /* ----- HCI Connections ----- */
300 enum {
301         HCI_CONN_AUTH_PEND,
302         HCI_CONN_ENCRYPT_PEND,
303         HCI_CONN_RSWITCH_PEND,
304         HCI_CONN_MODE_CHANGE_PEND,
305         HCI_CONN_SCO_SETUP_PEND,
306 };
307
308 static inline void hci_conn_hash_init(struct hci_dev *hdev)
309 {
310         struct hci_conn_hash *h = &hdev->conn_hash;
311         INIT_LIST_HEAD(&h->list);
312         spin_lock_init(&h->lock);
313         h->acl_num = 0;
314         h->sco_num = 0;
315 }
316
317 static inline void hci_conn_hash_add(struct hci_dev *hdev, struct hci_conn *c)
318 {
319         struct hci_conn_hash *h = &hdev->conn_hash;
320         list_add(&c->list, &h->list);
321         switch (c->type) {
322         case ACL_LINK:
323                 h->acl_num++;
324                 break;
325         case LE_LINK:
326                 h->le_num++;
327                 break;
328         case SCO_LINK:
329         case ESCO_LINK:
330                 h->sco_num++;
331                 break;
332         }
333 }
334
335 static inline void hci_conn_hash_del(struct hci_dev *hdev, struct hci_conn *c)
336 {
337         struct hci_conn_hash *h = &hdev->conn_hash;
338         list_del(&c->list);
339         switch (c->type) {
340         case ACL_LINK:
341                 h->acl_num--;
342                 break;
343         case LE_LINK:
344                 h->le_num--;
345                 break;
346         case SCO_LINK:
347         case ESCO_LINK:
348                 h->sco_num--;
349                 break;
350         }
351 }
352
353 static inline struct hci_conn *hci_conn_hash_lookup_handle(struct hci_dev *hdev,
354                                                                 __u16 handle)
355 {
356         struct hci_conn_hash *h = &hdev->conn_hash;
357         struct list_head *p;
358         struct hci_conn  *c;
359
360         list_for_each(p, &h->list) {
361                 c = list_entry(p, struct hci_conn, list);
362                 if (c->handle == handle)
363                         return c;
364         }
365         return NULL;
366 }
367
368 static inline struct hci_conn *hci_conn_hash_lookup_ba(struct hci_dev *hdev,
369                                                         __u8 type, bdaddr_t *ba)
370 {
371         struct hci_conn_hash *h = &hdev->conn_hash;
372         struct list_head *p;
373         struct hci_conn  *c;
374
375         list_for_each(p, &h->list) {
376                 c = list_entry(p, struct hci_conn, list);
377                 if (c->type == type && !bacmp(&c->dst, ba))
378                         return c;
379         }
380         return NULL;
381 }
382
383 static inline struct hci_conn *hci_conn_hash_lookup_state(struct hci_dev *hdev,
384                                                         __u8 type, __u16 state)
385 {
386         struct hci_conn_hash *h = &hdev->conn_hash;
387         struct list_head *p;
388         struct hci_conn  *c;
389
390         list_for_each(p, &h->list) {
391                 c = list_entry(p, struct hci_conn, list);
392                 if (c->type == type && c->state == state)
393                         return c;
394         }
395         return NULL;
396 }
397
398 void hci_acl_connect(struct hci_conn *conn);
399 void hci_acl_disconn(struct hci_conn *conn, __u8 reason);
400 void hci_add_sco(struct hci_conn *conn, __u16 handle);
401 void hci_setup_sync(struct hci_conn *conn, __u16 handle);
402 void hci_sco_setup(struct hci_conn *conn, __u8 status);
403
404 struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst);
405 int hci_conn_del(struct hci_conn *conn);
406 void hci_conn_hash_flush(struct hci_dev *hdev);
407 void hci_conn_check_pending(struct hci_dev *hdev);
408
409 struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst, __u8 sec_level, __u8 auth_type);
410 int hci_conn_check_link_mode(struct hci_conn *conn);
411 int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type);
412 int hci_conn_change_link_key(struct hci_conn *conn);
413 int hci_conn_switch_role(struct hci_conn *conn, __u8 role);
414
415 void hci_conn_enter_active_mode(struct hci_conn *conn);
416 void hci_conn_enter_sniff_mode(struct hci_conn *conn);
417
418 void hci_conn_hold_device(struct hci_conn *conn);
419 void hci_conn_put_device(struct hci_conn *conn);
420
421 static inline void hci_conn_hold(struct hci_conn *conn)
422 {
423         atomic_inc(&conn->refcnt);
424         del_timer(&conn->disc_timer);
425 }
426
427 static inline void hci_conn_put(struct hci_conn *conn)
428 {
429         if (atomic_dec_and_test(&conn->refcnt)) {
430                 unsigned long timeo;
431                 if (conn->type == ACL_LINK) {
432                         del_timer(&conn->idle_timer);
433                         if (conn->state == BT_CONNECTED) {
434                                 timeo = msecs_to_jiffies(conn->disc_timeout);
435                                 if (!conn->out)
436                                         timeo *= 2;
437                         } else
438                                 timeo = msecs_to_jiffies(10);
439                 } else
440                         timeo = msecs_to_jiffies(10);
441                 mod_timer(&conn->disc_timer, jiffies + timeo);
442         }
443 }
444
445 /* ----- HCI Devices ----- */
446 static inline void __hci_dev_put(struct hci_dev *d)
447 {
448         if (atomic_dec_and_test(&d->refcnt))
449                 d->destruct(d);
450 }
451
452 static inline void hci_dev_put(struct hci_dev *d)
453 {
454         __hci_dev_put(d);
455         module_put(d->owner);
456 }
457
458 static inline struct hci_dev *__hci_dev_hold(struct hci_dev *d)
459 {
460         atomic_inc(&d->refcnt);
461         return d;
462 }
463
464 static inline struct hci_dev *hci_dev_hold(struct hci_dev *d)
465 {
466         if (try_module_get(d->owner))
467                 return __hci_dev_hold(d);
468         return NULL;
469 }
470
471 #define hci_dev_lock(d)         spin_lock(&d->lock)
472 #define hci_dev_unlock(d)       spin_unlock(&d->lock)
473 #define hci_dev_lock_bh(d)      spin_lock_bh(&d->lock)
474 #define hci_dev_unlock_bh(d)    spin_unlock_bh(&d->lock)
475
476 struct hci_dev *hci_dev_get(int index);
477 struct hci_dev *hci_get_route(bdaddr_t *src, bdaddr_t *dst);
478
479 struct hci_dev *hci_alloc_dev(void);
480 void hci_free_dev(struct hci_dev *hdev);
481 int hci_register_dev(struct hci_dev *hdev);
482 int hci_unregister_dev(struct hci_dev *hdev);
483 int hci_suspend_dev(struct hci_dev *hdev);
484 int hci_resume_dev(struct hci_dev *hdev);
485 int hci_dev_open(__u16 dev);
486 int hci_dev_close(__u16 dev);
487 int hci_dev_reset(__u16 dev);
488 int hci_dev_reset_stat(__u16 dev);
489 int hci_dev_cmd(unsigned int cmd, void __user *arg);
490 int hci_get_dev_list(void __user *arg);
491 int hci_get_dev_info(void __user *arg);
492 int hci_get_conn_list(void __user *arg);
493 int hci_get_conn_info(struct hci_dev *hdev, void __user *arg);
494 int hci_get_auth_info(struct hci_dev *hdev, void __user *arg);
495 int hci_inquiry(void __user *arg);
496
497 struct bdaddr_list *hci_blacklist_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr);
498 int hci_blacklist_clear(struct hci_dev *hdev);
499
500 int hci_uuids_clear(struct hci_dev *hdev);
501
502 int hci_link_keys_clear(struct hci_dev *hdev);
503 struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
504 int hci_add_link_key(struct hci_dev *hdev, int new_key, bdaddr_t *bdaddr,
505                                                 u8 *key, u8 type, u8 pin_len);
506 int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
507
508 void hci_del_off_timer(struct hci_dev *hdev);
509
510 void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb);
511
512 int hci_recv_frame(struct sk_buff *skb);
513 int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count);
514 int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count);
515
516 int hci_register_sysfs(struct hci_dev *hdev);
517 void hci_unregister_sysfs(struct hci_dev *hdev);
518 void hci_conn_init_sysfs(struct hci_conn *conn);
519 void hci_conn_add_sysfs(struct hci_conn *conn);
520 void hci_conn_del_sysfs(struct hci_conn *conn);
521
522 #define SET_HCIDEV_DEV(hdev, pdev) ((hdev)->parent = (pdev))
523
524 /* ----- LMP capabilities ----- */
525 #define lmp_rswitch_capable(dev)   ((dev)->features[0] & LMP_RSWITCH)
526 #define lmp_encrypt_capable(dev)   ((dev)->features[0] & LMP_ENCRYPT)
527 #define lmp_sniff_capable(dev)     ((dev)->features[0] & LMP_SNIFF)
528 #define lmp_sniffsubr_capable(dev) ((dev)->features[5] & LMP_SNIFF_SUBR)
529 #define lmp_esco_capable(dev)      ((dev)->features[3] & LMP_ESCO)
530 #define lmp_ssp_capable(dev)       ((dev)->features[6] & LMP_SIMPLE_PAIR)
531 #define lmp_no_flush_capable(dev)  ((dev)->features[6] & LMP_NO_FLUSH)
532 #define lmp_le_capable(dev)        ((dev)->features[4] & LMP_LE)
533
534 /* ----- HCI protocols ----- */
535 struct hci_proto {
536         char            *name;
537         unsigned int    id;
538         unsigned long   flags;
539
540         void            *priv;
541
542         int (*connect_ind)      (struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type);
543         int (*connect_cfm)      (struct hci_conn *conn, __u8 status);
544         int (*disconn_ind)      (struct hci_conn *conn);
545         int (*disconn_cfm)      (struct hci_conn *conn, __u8 reason);
546         int (*recv_acldata)     (struct hci_conn *conn, struct sk_buff *skb, __u16 flags);
547         int (*recv_scodata)     (struct hci_conn *conn, struct sk_buff *skb);
548         int (*security_cfm)     (struct hci_conn *conn, __u8 status, __u8 encrypt);
549 };
550
551 static inline int hci_proto_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type)
552 {
553         register struct hci_proto *hp;
554         int mask = 0;
555
556         hp = hci_proto[HCI_PROTO_L2CAP];
557         if (hp && hp->connect_ind)
558                 mask |= hp->connect_ind(hdev, bdaddr, type);
559
560         hp = hci_proto[HCI_PROTO_SCO];
561         if (hp && hp->connect_ind)
562                 mask |= hp->connect_ind(hdev, bdaddr, type);
563
564         return mask;
565 }
566
567 static inline void hci_proto_connect_cfm(struct hci_conn *conn, __u8 status)
568 {
569         register struct hci_proto *hp;
570
571         hp = hci_proto[HCI_PROTO_L2CAP];
572         if (hp && hp->connect_cfm)
573                 hp->connect_cfm(conn, status);
574
575         hp = hci_proto[HCI_PROTO_SCO];
576         if (hp && hp->connect_cfm)
577                 hp->connect_cfm(conn, status);
578
579         if (conn->connect_cfm_cb)
580                 conn->connect_cfm_cb(conn, status);
581 }
582
583 static inline int hci_proto_disconn_ind(struct hci_conn *conn)
584 {
585         register struct hci_proto *hp;
586         int reason = 0x13;
587
588         hp = hci_proto[HCI_PROTO_L2CAP];
589         if (hp && hp->disconn_ind)
590                 reason = hp->disconn_ind(conn);
591
592         hp = hci_proto[HCI_PROTO_SCO];
593         if (hp && hp->disconn_ind)
594                 reason = hp->disconn_ind(conn);
595
596         return reason;
597 }
598
599 static inline void hci_proto_disconn_cfm(struct hci_conn *conn, __u8 reason)
600 {
601         register struct hci_proto *hp;
602
603         hp = hci_proto[HCI_PROTO_L2CAP];
604         if (hp && hp->disconn_cfm)
605                 hp->disconn_cfm(conn, reason);
606
607         hp = hci_proto[HCI_PROTO_SCO];
608         if (hp && hp->disconn_cfm)
609                 hp->disconn_cfm(conn, reason);
610
611         if (conn->disconn_cfm_cb)
612                 conn->disconn_cfm_cb(conn, reason);
613 }
614
615 static inline void hci_proto_auth_cfm(struct hci_conn *conn, __u8 status)
616 {
617         register struct hci_proto *hp;
618         __u8 encrypt;
619
620         if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend))
621                 return;
622
623         encrypt = (conn->link_mode & HCI_LM_ENCRYPT) ? 0x01 : 0x00;
624
625         hp = hci_proto[HCI_PROTO_L2CAP];
626         if (hp && hp->security_cfm)
627                 hp->security_cfm(conn, status, encrypt);
628
629         hp = hci_proto[HCI_PROTO_SCO];
630         if (hp && hp->security_cfm)
631                 hp->security_cfm(conn, status, encrypt);
632
633         if (conn->security_cfm_cb)
634                 conn->security_cfm_cb(conn, status);
635 }
636
637 static inline void hci_proto_encrypt_cfm(struct hci_conn *conn, __u8 status, __u8 encrypt)
638 {
639         register struct hci_proto *hp;
640
641         hp = hci_proto[HCI_PROTO_L2CAP];
642         if (hp && hp->security_cfm)
643                 hp->security_cfm(conn, status, encrypt);
644
645         hp = hci_proto[HCI_PROTO_SCO];
646         if (hp && hp->security_cfm)
647                 hp->security_cfm(conn, status, encrypt);
648
649         if (conn->security_cfm_cb)
650                 conn->security_cfm_cb(conn, status);
651 }
652
653 int hci_register_proto(struct hci_proto *hproto);
654 int hci_unregister_proto(struct hci_proto *hproto);
655
656 /* ----- HCI callbacks ----- */
657 struct hci_cb {
658         struct list_head list;
659
660         char *name;
661
662         void (*security_cfm)    (struct hci_conn *conn, __u8 status, __u8 encrypt);
663         void (*key_change_cfm)  (struct hci_conn *conn, __u8 status);
664         void (*role_switch_cfm) (struct hci_conn *conn, __u8 status, __u8 role);
665 };
666
667 static inline void hci_auth_cfm(struct hci_conn *conn, __u8 status)
668 {
669         struct list_head *p;
670         __u8 encrypt;
671
672         hci_proto_auth_cfm(conn, status);
673
674         if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend))
675                 return;
676
677         encrypt = (conn->link_mode & HCI_LM_ENCRYPT) ? 0x01 : 0x00;
678
679         read_lock_bh(&hci_cb_list_lock);
680         list_for_each(p, &hci_cb_list) {
681                 struct hci_cb *cb = list_entry(p, struct hci_cb, list);
682                 if (cb->security_cfm)
683                         cb->security_cfm(conn, status, encrypt);
684         }
685         read_unlock_bh(&hci_cb_list_lock);
686 }
687
688 static inline void hci_encrypt_cfm(struct hci_conn *conn, __u8 status, __u8 encrypt)
689 {
690         struct list_head *p;
691
692         if (conn->sec_level == BT_SECURITY_SDP)
693                 conn->sec_level = BT_SECURITY_LOW;
694
695         hci_proto_encrypt_cfm(conn, status, encrypt);
696
697         read_lock_bh(&hci_cb_list_lock);
698         list_for_each(p, &hci_cb_list) {
699                 struct hci_cb *cb = list_entry(p, struct hci_cb, list);
700                 if (cb->security_cfm)
701                         cb->security_cfm(conn, status, encrypt);
702         }
703         read_unlock_bh(&hci_cb_list_lock);
704 }
705
706 static inline void hci_key_change_cfm(struct hci_conn *conn, __u8 status)
707 {
708         struct list_head *p;
709
710         read_lock_bh(&hci_cb_list_lock);
711         list_for_each(p, &hci_cb_list) {
712                 struct hci_cb *cb = list_entry(p, struct hci_cb, list);
713                 if (cb->key_change_cfm)
714                         cb->key_change_cfm(conn, status);
715         }
716         read_unlock_bh(&hci_cb_list_lock);
717 }
718
719 static inline void hci_role_switch_cfm(struct hci_conn *conn, __u8 status, __u8 role)
720 {
721         struct list_head *p;
722
723         read_lock_bh(&hci_cb_list_lock);
724         list_for_each(p, &hci_cb_list) {
725                 struct hci_cb *cb = list_entry(p, struct hci_cb, list);
726                 if (cb->role_switch_cfm)
727                         cb->role_switch_cfm(conn, status, role);
728         }
729         read_unlock_bh(&hci_cb_list_lock);
730 }
731
732 int hci_register_cb(struct hci_cb *hcb);
733 int hci_unregister_cb(struct hci_cb *hcb);
734
735 int hci_register_notifier(struct notifier_block *nb);
736 int hci_unregister_notifier(struct notifier_block *nb);
737
738 int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, void *param);
739 void hci_send_acl(struct hci_conn *conn, struct sk_buff *skb, __u16 flags);
740 void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb);
741
742 void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode);
743
744 void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data);
745
746 /* ----- HCI Sockets ----- */
747 void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb,
748                                                         struct sock *skip_sk);
749
750 /* Management interface */
751 int mgmt_control(struct sock *sk, struct msghdr *msg, size_t len);
752 int mgmt_index_added(u16 index);
753 int mgmt_index_removed(u16 index);
754 int mgmt_powered(u16 index, u8 powered);
755 int mgmt_discoverable(u16 index, u8 discoverable);
756 int mgmt_connectable(u16 index, u8 connectable);
757 int mgmt_new_key(u16 index, struct link_key *key, u8 old_key_type);
758 int mgmt_connected(u16 index, bdaddr_t *bdaddr);
759 int mgmt_disconnected(u16 index, bdaddr_t *bdaddr);
760 int mgmt_disconnect_failed(u16 index);
761 int mgmt_connect_failed(u16 index, bdaddr_t *bdaddr, u8 status);
762 int mgmt_pin_code_request(u16 index, bdaddr_t *bdaddr);
763 int mgmt_pin_code_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status);
764 int mgmt_pin_code_neg_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status);
765
766 /* HCI info for socket */
767 #define hci_pi(sk) ((struct hci_pinfo *) sk)
768
769 struct hci_pinfo {
770         struct bt_sock    bt;
771         struct hci_dev    *hdev;
772         struct hci_filter filter;
773         __u32             cmsg_mask;
774         unsigned short   channel;
775 };
776
777 /* HCI security filter */
778 #define HCI_SFLT_MAX_OGF  5
779
780 struct hci_sec_filter {
781         __u32 type_mask;
782         __u32 event_mask[2];
783         __u32 ocf_mask[HCI_SFLT_MAX_OGF + 1][4];
784 };
785
786 /* ----- HCI requests ----- */
787 #define HCI_REQ_DONE      0
788 #define HCI_REQ_PEND      1
789 #define HCI_REQ_CANCELED  2
790
791 #define hci_req_lock(d)         mutex_lock(&d->req_lock)
792 #define hci_req_unlock(d)       mutex_unlock(&d->req_lock)
793
794 void hci_req_complete(struct hci_dev *hdev, __u16 cmd, int result);
795
796 void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max,
797                                         u16 latency, u16 to_multiplier);
798 #endif /* __HCI_CORE_H */