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