2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
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;
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.
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.
28 #include <net/bluetooth/hci.h>
30 /* HCI upper protocols */
31 #define HCI_PROTO_L2CAP 0
32 #define HCI_PROTO_SCO 1
34 /* HCI Core structures */
38 __u8 pscan_period_mode;
46 struct inquiry_entry {
47 struct inquiry_entry *next;
49 struct inquiry_data data;
52 struct inquiry_cache {
55 struct inquiry_entry *list;
58 struct hci_conn_hash {
59 struct list_head list;
67 struct list_head list;
72 struct list_head list;
78 struct list_head list;
85 #define NUM_REASSEMBLY 4
87 struct list_head list;
97 __u8 dev_name[HCI_MAX_NAME_LENGTH];
118 __u16 sniff_min_interval;
119 __u16 sniff_max_interval;
121 unsigned long quirks;
124 unsigned int acl_cnt;
125 unsigned int sco_cnt;
128 unsigned int acl_mtu;
129 unsigned int sco_mtu;
131 unsigned int acl_pkts;
132 unsigned int sco_pkts;
133 unsigned int le_pkts;
135 unsigned long acl_last_tx;
136 unsigned long sco_last_tx;
137 unsigned long le_last_tx;
139 struct workqueue_struct *workqueue;
141 struct work_struct power_on;
142 struct work_struct power_off;
143 struct timer_list off_timer;
145 struct timer_list cmd_timer;
146 struct tasklet_struct cmd_task;
147 struct tasklet_struct rx_task;
148 struct tasklet_struct tx_task;
150 struct sk_buff_head rx_q;
151 struct sk_buff_head raw_q;
152 struct sk_buff_head cmd_q;
154 struct sk_buff *sent_cmd;
155 struct sk_buff *reassembly[NUM_REASSEMBLY];
157 struct mutex req_lock;
158 wait_queue_head_t req_wait_q;
164 struct inquiry_cache inq_cache;
165 struct hci_conn_hash conn_hash;
166 struct list_head blacklist;
168 struct list_head uuids;
170 struct list_head link_keys;
172 struct hci_dev_stats stat;
174 struct sk_buff_head driver_init;
181 struct dentry *debugfs;
183 struct device *parent;
186 struct rfkill *rfkill;
188 struct module *owner;
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);
200 struct list_head list;
221 __u8 pending_sec_level;
234 struct sk_buff_head data_q;
236 struct timer_list disc_timer;
237 struct timer_list idle_timer;
239 struct work_struct work_add;
240 struct work_struct work_del;
245 struct hci_dev *hdev;
250 struct hci_conn *link;
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);
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;
263 /* ----- Inquiry cache ----- */
264 #define INQUIRY_CACHE_AGE_MAX (HZ*30) /* 30 seconds */
265 #define INQUIRY_ENTRY_AGE_MAX (HZ*60) /* 60 seconds */
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)
272 static inline void inquiry_cache_init(struct hci_dev *hdev)
274 struct inquiry_cache *c = &hdev->inq_cache;
275 spin_lock_init(&c->lock);
279 static inline int inquiry_cache_empty(struct hci_dev *hdev)
281 struct inquiry_cache *c = &hdev->inq_cache;
282 return c->list == NULL;
285 static inline long inquiry_cache_age(struct hci_dev *hdev)
287 struct inquiry_cache *c = &hdev->inq_cache;
288 return jiffies - c->timestamp;
291 static inline long inquiry_entry_age(struct inquiry_entry *e)
293 return jiffies - e->timestamp;
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);
299 /* ----- HCI Connections ----- */
302 HCI_CONN_ENCRYPT_PEND,
303 HCI_CONN_RSWITCH_PEND,
304 HCI_CONN_MODE_CHANGE_PEND,
305 HCI_CONN_SCO_SETUP_PEND,
308 static inline void hci_conn_hash_init(struct hci_dev *hdev)
310 struct hci_conn_hash *h = &hdev->conn_hash;
311 INIT_LIST_HEAD(&h->list);
312 spin_lock_init(&h->lock);
317 static inline void hci_conn_hash_add(struct hci_dev *hdev, struct hci_conn *c)
319 struct hci_conn_hash *h = &hdev->conn_hash;
320 list_add(&c->list, &h->list);
335 static inline void hci_conn_hash_del(struct hci_dev *hdev, struct hci_conn *c)
337 struct hci_conn_hash *h = &hdev->conn_hash;
353 static inline struct hci_conn *hci_conn_hash_lookup_handle(struct hci_dev *hdev,
356 struct hci_conn_hash *h = &hdev->conn_hash;
360 list_for_each(p, &h->list) {
361 c = list_entry(p, struct hci_conn, list);
362 if (c->handle == handle)
368 static inline struct hci_conn *hci_conn_hash_lookup_ba(struct hci_dev *hdev,
369 __u8 type, bdaddr_t *ba)
371 struct hci_conn_hash *h = &hdev->conn_hash;
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))
383 static inline struct hci_conn *hci_conn_hash_lookup_state(struct hci_dev *hdev,
384 __u8 type, __u16 state)
386 struct hci_conn_hash *h = &hdev->conn_hash;
390 list_for_each(p, &h->list) {
391 c = list_entry(p, struct hci_conn, list);
392 if (c->type == type && c->state == state)
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);
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);
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);
415 void hci_conn_enter_active_mode(struct hci_conn *conn);
416 void hci_conn_enter_sniff_mode(struct hci_conn *conn);
418 void hci_conn_hold_device(struct hci_conn *conn);
419 void hci_conn_put_device(struct hci_conn *conn);
421 static inline void hci_conn_hold(struct hci_conn *conn)
423 atomic_inc(&conn->refcnt);
424 del_timer(&conn->disc_timer);
427 static inline void hci_conn_put(struct hci_conn *conn)
429 if (atomic_dec_and_test(&conn->refcnt)) {
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);
438 timeo = msecs_to_jiffies(10);
440 timeo = msecs_to_jiffies(10);
441 mod_timer(&conn->disc_timer, jiffies + timeo);
445 /* ----- HCI Devices ----- */
446 static inline void __hci_dev_put(struct hci_dev *d)
448 if (atomic_dec_and_test(&d->refcnt))
452 static inline void hci_dev_put(struct hci_dev *d)
455 module_put(d->owner);
458 static inline struct hci_dev *__hci_dev_hold(struct hci_dev *d)
460 atomic_inc(&d->refcnt);
464 static inline struct hci_dev *hci_dev_hold(struct hci_dev *d)
466 if (try_module_get(d->owner))
467 return __hci_dev_hold(d);
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)
476 struct hci_dev *hci_dev_get(int index);
477 struct hci_dev *hci_get_route(bdaddr_t *src, bdaddr_t *dst);
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);
497 struct bdaddr_list *hci_blacklist_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr);
498 int hci_blacklist_clear(struct hci_dev *hdev);
500 int hci_uuids_clear(struct hci_dev *hdev);
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);
508 void hci_del_off_timer(struct hci_dev *hdev);
510 void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb);
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);
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);
522 #define SET_HCIDEV_DEV(hdev, pdev) ((hdev)->parent = (pdev))
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)
534 /* ----- HCI protocols ----- */
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);
551 static inline int hci_proto_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 type)
553 register struct hci_proto *hp;
556 hp = hci_proto[HCI_PROTO_L2CAP];
557 if (hp && hp->connect_ind)
558 mask |= hp->connect_ind(hdev, bdaddr, type);
560 hp = hci_proto[HCI_PROTO_SCO];
561 if (hp && hp->connect_ind)
562 mask |= hp->connect_ind(hdev, bdaddr, type);
567 static inline void hci_proto_connect_cfm(struct hci_conn *conn, __u8 status)
569 register struct hci_proto *hp;
571 hp = hci_proto[HCI_PROTO_L2CAP];
572 if (hp && hp->connect_cfm)
573 hp->connect_cfm(conn, status);
575 hp = hci_proto[HCI_PROTO_SCO];
576 if (hp && hp->connect_cfm)
577 hp->connect_cfm(conn, status);
579 if (conn->connect_cfm_cb)
580 conn->connect_cfm_cb(conn, status);
583 static inline int hci_proto_disconn_ind(struct hci_conn *conn)
585 register struct hci_proto *hp;
588 hp = hci_proto[HCI_PROTO_L2CAP];
589 if (hp && hp->disconn_ind)
590 reason = hp->disconn_ind(conn);
592 hp = hci_proto[HCI_PROTO_SCO];
593 if (hp && hp->disconn_ind)
594 reason = hp->disconn_ind(conn);
599 static inline void hci_proto_disconn_cfm(struct hci_conn *conn, __u8 reason)
601 register struct hci_proto *hp;
603 hp = hci_proto[HCI_PROTO_L2CAP];
604 if (hp && hp->disconn_cfm)
605 hp->disconn_cfm(conn, reason);
607 hp = hci_proto[HCI_PROTO_SCO];
608 if (hp && hp->disconn_cfm)
609 hp->disconn_cfm(conn, reason);
611 if (conn->disconn_cfm_cb)
612 conn->disconn_cfm_cb(conn, reason);
615 static inline void hci_proto_auth_cfm(struct hci_conn *conn, __u8 status)
617 register struct hci_proto *hp;
620 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend))
623 encrypt = (conn->link_mode & HCI_LM_ENCRYPT) ? 0x01 : 0x00;
625 hp = hci_proto[HCI_PROTO_L2CAP];
626 if (hp && hp->security_cfm)
627 hp->security_cfm(conn, status, encrypt);
629 hp = hci_proto[HCI_PROTO_SCO];
630 if (hp && hp->security_cfm)
631 hp->security_cfm(conn, status, encrypt);
633 if (conn->security_cfm_cb)
634 conn->security_cfm_cb(conn, status);
637 static inline void hci_proto_encrypt_cfm(struct hci_conn *conn, __u8 status, __u8 encrypt)
639 register struct hci_proto *hp;
641 hp = hci_proto[HCI_PROTO_L2CAP];
642 if (hp && hp->security_cfm)
643 hp->security_cfm(conn, status, encrypt);
645 hp = hci_proto[HCI_PROTO_SCO];
646 if (hp && hp->security_cfm)
647 hp->security_cfm(conn, status, encrypt);
649 if (conn->security_cfm_cb)
650 conn->security_cfm_cb(conn, status);
653 int hci_register_proto(struct hci_proto *hproto);
654 int hci_unregister_proto(struct hci_proto *hproto);
656 /* ----- HCI callbacks ----- */
658 struct list_head list;
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);
667 static inline void hci_auth_cfm(struct hci_conn *conn, __u8 status)
672 hci_proto_auth_cfm(conn, status);
674 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend))
677 encrypt = (conn->link_mode & HCI_LM_ENCRYPT) ? 0x01 : 0x00;
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);
685 read_unlock_bh(&hci_cb_list_lock);
688 static inline void hci_encrypt_cfm(struct hci_conn *conn, __u8 status, __u8 encrypt)
692 if (conn->sec_level == BT_SECURITY_SDP)
693 conn->sec_level = BT_SECURITY_LOW;
695 hci_proto_encrypt_cfm(conn, status, encrypt);
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);
703 read_unlock_bh(&hci_cb_list_lock);
706 static inline void hci_key_change_cfm(struct hci_conn *conn, __u8 status)
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);
716 read_unlock_bh(&hci_cb_list_lock);
719 static inline void hci_role_switch_cfm(struct hci_conn *conn, __u8 status, __u8 role)
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);
729 read_unlock_bh(&hci_cb_list_lock);
732 int hci_register_cb(struct hci_cb *hcb);
733 int hci_unregister_cb(struct hci_cb *hcb);
735 int hci_register_notifier(struct notifier_block *nb);
736 int hci_unregister_notifier(struct notifier_block *nb);
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);
742 void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode);
744 void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data);
746 /* ----- HCI Sockets ----- */
747 void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb,
748 struct sock *skip_sk);
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 int mgmt_user_confirm_request(u16 index, bdaddr_t *bdaddr, __le32 value);
766 int mgmt_user_confirm_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status);
767 int mgmt_user_confirm_neg_reply_complete(u16 index, bdaddr_t *bdaddr,
769 int mgmt_auth_failed(u16 index, bdaddr_t *bdaddr, u8 status);
771 /* HCI info for socket */
772 #define hci_pi(sk) ((struct hci_pinfo *) sk)
776 struct hci_dev *hdev;
777 struct hci_filter filter;
779 unsigned short channel;
782 /* HCI security filter */
783 #define HCI_SFLT_MAX_OGF 5
785 struct hci_sec_filter {
788 __u32 ocf_mask[HCI_SFLT_MAX_OGF + 1][4];
791 /* ----- HCI requests ----- */
792 #define HCI_REQ_DONE 0
793 #define HCI_REQ_PEND 1
794 #define HCI_REQ_CANCELED 2
796 #define hci_req_lock(d) mutex_lock(&d->req_lock)
797 #define hci_req_unlock(d) mutex_unlock(&d->req_lock)
799 void hci_req_complete(struct hci_dev *hdev, __u16 cmd, int result);
801 void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max,
802 u16 latency, u16 to_multiplier);
803 #endif /* __HCI_CORE_H */