2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated
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.
25 /* Bluetooth HCI event handling. */
27 #include <linux/module.h>
29 #include <linux/types.h>
30 #include <linux/errno.h>
31 #include <linux/kernel.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
34 #include <linux/poll.h>
35 #include <linux/fcntl.h>
36 #include <linux/init.h>
37 #include <linux/skbuff.h>
38 #include <linux/interrupt.h>
39 #include <linux/notifier.h>
42 #include <asm/system.h>
43 #include <asm/uaccess.h>
44 #include <asm/unaligned.h>
46 #include <net/bluetooth/bluetooth.h>
47 #include <net/bluetooth/hci_core.h>
49 #ifndef CONFIG_BT_HCI_CORE_DEBUG
54 /* Handle HCI Event packets */
56 /* Command Complete OGF LINK_CTL */
57 static void hci_cc_link_ctl(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
61 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
64 case OCF_INQUIRY_CANCEL:
65 status = *((__u8 *) skb->data);
68 BT_DBG("%s Inquiry cancel error: status 0x%x", hdev->name, status);
70 clear_bit(HCI_INQUIRY, &hdev->flags);
71 hci_req_complete(hdev, status);
76 BT_DBG("%s Command complete: ogf LINK_CTL ocf %x", hdev->name, ocf);
81 /* Command Complete OGF LINK_POLICY */
82 static void hci_cc_link_policy(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
84 struct hci_conn *conn;
85 struct hci_rp_role_discovery *rd;
87 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
90 case OCF_ROLE_DISCOVERY:
91 rd = (void *) skb->data;
98 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rd->handle));
101 conn->link_mode &= ~HCI_LM_MASTER;
103 conn->link_mode |= HCI_LM_MASTER;
106 hci_dev_unlock(hdev);
110 BT_DBG("%s: Command complete: ogf LINK_POLICY ocf %x",
116 /* Command Complete OGF HOST_CTL */
117 static void hci_cc_host_ctl(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
121 struct hci_rp_read_voice_setting *vs;
124 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
128 status = *((__u8 *) skb->data);
129 hci_req_complete(hdev, status);
132 case OCF_SET_EVENT_FLT:
133 status = *((__u8 *) skb->data);
135 BT_DBG("%s SET_EVENT_FLT failed %d", hdev->name, status);
137 BT_DBG("%s SET_EVENT_FLT succeseful", hdev->name);
141 case OCF_WRITE_AUTH_ENABLE:
142 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_AUTH_ENABLE);
146 status = *((__u8 *) skb->data);
147 param = *((__u8 *) sent);
150 if (param == AUTH_ENABLED)
151 set_bit(HCI_AUTH, &hdev->flags);
153 clear_bit(HCI_AUTH, &hdev->flags);
155 hci_req_complete(hdev, status);
158 case OCF_WRITE_ENCRYPT_MODE:
159 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_ENCRYPT_MODE);
163 status = *((__u8 *) skb->data);
164 param = *((__u8 *) sent);
168 set_bit(HCI_ENCRYPT, &hdev->flags);
170 clear_bit(HCI_ENCRYPT, &hdev->flags);
172 hci_req_complete(hdev, status);
175 case OCF_WRITE_CA_TIMEOUT:
176 status = *((__u8 *) skb->data);
178 BT_DBG("%s OCF_WRITE_CA_TIMEOUT failed %d", hdev->name, status);
180 BT_DBG("%s OCF_WRITE_CA_TIMEOUT succeseful", hdev->name);
184 case OCF_WRITE_PG_TIMEOUT:
185 status = *((__u8 *) skb->data);
187 BT_DBG("%s OCF_WRITE_PG_TIMEOUT failed %d", hdev->name, status);
189 BT_DBG("%s: OCF_WRITE_PG_TIMEOUT succeseful", hdev->name);
193 case OCF_WRITE_SCAN_ENABLE:
194 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_SCAN_ENABLE);
198 status = *((__u8 *) skb->data);
199 param = *((__u8 *) sent);
201 BT_DBG("param 0x%x", param);
204 clear_bit(HCI_PSCAN, &hdev->flags);
205 clear_bit(HCI_ISCAN, &hdev->flags);
206 if (param & SCAN_INQUIRY)
207 set_bit(HCI_ISCAN, &hdev->flags);
209 if (param & SCAN_PAGE)
210 set_bit(HCI_PSCAN, &hdev->flags);
212 hci_req_complete(hdev, status);
215 case OCF_READ_VOICE_SETTING:
216 vs = (struct hci_rp_read_voice_setting *) skb->data;
219 BT_DBG("%s READ_VOICE_SETTING failed %d", hdev->name, vs->status);
223 setting = __le16_to_cpu(vs->voice_setting);
225 if (hdev->voice_setting != setting ) {
226 hdev->voice_setting = setting;
228 BT_DBG("%s: voice setting 0x%04x", hdev->name, setting);
231 tasklet_disable(&hdev->tx_task);
232 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
233 tasklet_enable(&hdev->tx_task);
238 case OCF_WRITE_VOICE_SETTING:
239 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_VOICE_SETTING);
243 status = *((__u8 *) skb->data);
244 setting = __le16_to_cpu(get_unaligned((__le16 *) sent));
246 if (!status && hdev->voice_setting != setting) {
247 hdev->voice_setting = setting;
249 BT_DBG("%s: voice setting 0x%04x", hdev->name, setting);
252 tasklet_disable(&hdev->tx_task);
253 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
254 tasklet_enable(&hdev->tx_task);
257 hci_req_complete(hdev, status);
260 case OCF_HOST_BUFFER_SIZE:
261 status = *((__u8 *) skb->data);
263 BT_DBG("%s OCF_BUFFER_SIZE failed %d", hdev->name, status);
264 hci_req_complete(hdev, status);
269 BT_DBG("%s Command complete: ogf HOST_CTL ocf %x", hdev->name, ocf);
274 /* Command Complete OGF INFO_PARAM */
275 static void hci_cc_info_param(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
277 struct hci_rp_read_loc_features *lf;
278 struct hci_rp_read_buffer_size *bs;
279 struct hci_rp_read_bd_addr *ba;
281 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
284 case OCF_READ_LOCAL_FEATURES:
285 lf = (struct hci_rp_read_loc_features *) skb->data;
288 BT_DBG("%s READ_LOCAL_FEATURES failed %d", hdev->name, lf->status);
292 memcpy(hdev->features, lf->features, sizeof(hdev->features));
294 /* Adjust default settings according to features
295 * supported by device. */
296 if (hdev->features[0] & LMP_3SLOT)
297 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
299 if (hdev->features[0] & LMP_5SLOT)
300 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
302 if (hdev->features[1] & LMP_HV2)
303 hdev->pkt_type |= (HCI_HV2);
305 if (hdev->features[1] & LMP_HV3)
306 hdev->pkt_type |= (HCI_HV3);
308 BT_DBG("%s: features 0x%x 0x%x 0x%x", hdev->name, lf->features[0], lf->features[1], lf->features[2]);
312 case OCF_READ_BUFFER_SIZE:
313 bs = (struct hci_rp_read_buffer_size *) skb->data;
316 BT_DBG("%s READ_BUFFER_SIZE failed %d", hdev->name, bs->status);
317 hci_req_complete(hdev, bs->status);
321 hdev->acl_mtu = __le16_to_cpu(bs->acl_mtu);
322 hdev->sco_mtu = bs->sco_mtu ? bs->sco_mtu : 64;
323 hdev->acl_pkts = hdev->acl_cnt = __le16_to_cpu(bs->acl_max_pkt);
324 hdev->sco_pkts = hdev->sco_cnt = __le16_to_cpu(bs->sco_max_pkt);
326 BT_DBG("%s mtu: acl %d, sco %d max_pkt: acl %d, sco %d", hdev->name,
327 hdev->acl_mtu, hdev->sco_mtu, hdev->acl_pkts, hdev->sco_pkts);
330 case OCF_READ_BD_ADDR:
331 ba = (struct hci_rp_read_bd_addr *) skb->data;
334 bacpy(&hdev->bdaddr, &ba->bdaddr);
336 BT_DBG("%s: READ_BD_ADDR failed %d", hdev->name, ba->status);
339 hci_req_complete(hdev, ba->status);
343 BT_DBG("%s Command complete: ogf INFO_PARAM ocf %x", hdev->name, ocf);
348 /* Command Status OGF LINK_CTL */
349 static inline void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
351 struct hci_conn *conn;
352 struct hci_cp_create_conn *cp = hci_sent_cmd_data(hdev, OGF_LINK_CTL, OCF_CREATE_CONN);
359 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
361 BT_DBG("%s status 0x%x bdaddr %s conn %p", hdev->name,
362 status, batostr(&cp->bdaddr), conn);
365 if (conn && conn->state == BT_CONNECT) {
366 conn->state = BT_CLOSED;
367 hci_proto_connect_cfm(conn, status);
372 conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr);
375 conn->link_mode |= HCI_LM_MASTER;
377 BT_ERR("No memmory for new connection");
381 hci_dev_unlock(hdev);
384 static void hci_cs_link_ctl(struct hci_dev *hdev, __u16 ocf, __u8 status)
386 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
389 case OCF_CREATE_CONN:
390 hci_cs_create_conn(hdev, status);
395 struct hci_conn *acl, *sco;
396 struct hci_cp_add_sco *cp = hci_sent_cmd_data(hdev, OGF_LINK_CTL, OCF_ADD_SCO);
402 handle = __le16_to_cpu(cp->handle);
404 BT_DBG("%s Add SCO error: handle %d status 0x%x", hdev->name, handle, status);
408 acl = hci_conn_hash_lookup_handle(hdev, handle);
409 if (acl && (sco = acl->link)) {
410 sco->state = BT_CLOSED;
412 hci_proto_connect_cfm(sco, status);
416 hci_dev_unlock(hdev);
422 BT_DBG("%s Inquiry error: status 0x%x", hdev->name, status);
423 hci_req_complete(hdev, status);
425 set_bit(HCI_INQUIRY, &hdev->flags);
430 BT_DBG("%s Command status: ogf LINK_CTL ocf %x status %d",
431 hdev->name, ocf, status);
436 /* Command Status OGF LINK_POLICY */
437 static void hci_cs_link_policy(struct hci_dev *hdev, __u16 ocf, __u8 status)
439 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
443 BT_DBG("%s Command status: ogf HOST_POLICY ocf %x", hdev->name, ocf);
448 /* Command Status OGF HOST_CTL */
449 static void hci_cs_host_ctl(struct hci_dev *hdev, __u16 ocf, __u8 status)
451 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
455 BT_DBG("%s Command status: ogf HOST_CTL ocf %x", hdev->name, ocf);
460 /* Command Status OGF INFO_PARAM */
461 static void hci_cs_info_param(struct hci_dev *hdev, __u16 ocf, __u8 status)
463 BT_DBG("%s: hci_cs_info_param: ocf 0x%x", hdev->name, ocf);
467 BT_DBG("%s Command status: ogf INFO_PARAM ocf %x", hdev->name, ocf);
472 /* Inquiry Complete */
473 static inline void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
475 __u8 status = *((__u8 *) skb->data);
477 BT_DBG("%s status %d", hdev->name, status);
479 clear_bit(HCI_INQUIRY, &hdev->flags);
480 hci_req_complete(hdev, status);
484 static inline void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
486 struct inquiry_data data;
487 struct inquiry_info *info = (struct inquiry_info *) (skb->data + 1);
488 int num_rsp = *((__u8 *) skb->data);
490 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
497 for (; num_rsp; num_rsp--) {
498 bacpy(&data.bdaddr, &info->bdaddr);
499 data.pscan_rep_mode = info->pscan_rep_mode;
500 data.pscan_period_mode = info->pscan_period_mode;
501 data.pscan_mode = info->pscan_mode;
502 memcpy(data.dev_class, info->dev_class, 3);
503 data.clock_offset = info->clock_offset;
506 hci_inquiry_cache_update(hdev, &data);
509 hci_dev_unlock(hdev);
512 /* Inquiry Result With RSSI */
513 static inline void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, struct sk_buff *skb)
515 struct inquiry_data data;
516 int num_rsp = *((__u8 *) skb->data);
518 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
525 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
526 struct inquiry_info_with_rssi_and_pscan_mode *info =
527 (struct inquiry_info_with_rssi_and_pscan_mode *) (skb->data + 1);
529 for (; num_rsp; num_rsp--) {
530 bacpy(&data.bdaddr, &info->bdaddr);
531 data.pscan_rep_mode = info->pscan_rep_mode;
532 data.pscan_period_mode = info->pscan_period_mode;
533 data.pscan_mode = info->pscan_mode;
534 memcpy(data.dev_class, info->dev_class, 3);
535 data.clock_offset = info->clock_offset;
536 data.rssi = info->rssi;
538 hci_inquiry_cache_update(hdev, &data);
541 struct inquiry_info_with_rssi *info =
542 (struct inquiry_info_with_rssi *) (skb->data + 1);
544 for (; num_rsp; num_rsp--) {
545 bacpy(&data.bdaddr, &info->bdaddr);
546 data.pscan_rep_mode = info->pscan_rep_mode;
547 data.pscan_period_mode = info->pscan_period_mode;
548 data.pscan_mode = 0x00;
549 memcpy(data.dev_class, info->dev_class, 3);
550 data.clock_offset = info->clock_offset;
551 data.rssi = info->rssi;
553 hci_inquiry_cache_update(hdev, &data);
557 hci_dev_unlock(hdev);
560 /* Extended Inquiry Result */
561 static inline void hci_extended_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
563 struct inquiry_data data;
564 struct extended_inquiry_info *info = (struct extended_inquiry_info *) (skb->data + 1);
565 int num_rsp = *((__u8 *) skb->data);
567 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
574 for (; num_rsp; num_rsp--) {
575 bacpy(&data.bdaddr, &info->bdaddr);
576 data.pscan_rep_mode = info->pscan_rep_mode;
577 data.pscan_period_mode = info->pscan_period_mode;
578 data.pscan_mode = 0x00;
579 memcpy(data.dev_class, info->dev_class, 3);
580 data.clock_offset = info->clock_offset;
581 data.rssi = info->rssi;
583 hci_inquiry_cache_update(hdev, &data);
586 hci_dev_unlock(hdev);
589 /* Connect Request */
590 static inline void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
592 struct hci_ev_conn_request *ev = (struct hci_ev_conn_request *) skb->data;
593 int mask = hdev->link_mode;
595 BT_DBG("%s Connection request: %s type 0x%x", hdev->name,
596 batostr(&ev->bdaddr), ev->link_type);
598 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type);
600 if (mask & HCI_LM_ACCEPT) {
601 /* Connection accepted */
602 struct hci_conn *conn;
603 struct hci_cp_accept_conn_req cp;
606 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
608 if (!(conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr))) {
609 BT_ERR("No memmory for new connection");
610 hci_dev_unlock(hdev);
614 memcpy(conn->dev_class, ev->dev_class, 3);
615 conn->state = BT_CONNECT;
616 hci_dev_unlock(hdev);
618 bacpy(&cp.bdaddr, &ev->bdaddr);
620 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
621 cp.role = 0x00; /* Become master */
623 cp.role = 0x01; /* Remain slave */
625 hci_send_cmd(hdev, OGF_LINK_CTL, OCF_ACCEPT_CONN_REQ, sizeof(cp), &cp);
627 /* Connection rejected */
628 struct hci_cp_reject_conn_req cp;
630 bacpy(&cp.bdaddr, &ev->bdaddr);
632 hci_send_cmd(hdev, OGF_LINK_CTL, OCF_REJECT_CONN_REQ, sizeof(cp), &cp);
636 /* Connect Complete */
637 static inline void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
639 struct hci_ev_conn_complete *ev = (struct hci_ev_conn_complete *) skb->data;
640 struct hci_conn *conn = NULL;
642 BT_DBG("%s", hdev->name);
646 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
648 hci_dev_unlock(hdev);
653 conn->handle = __le16_to_cpu(ev->handle);
654 conn->state = BT_CONNECTED;
656 if (test_bit(HCI_AUTH, &hdev->flags))
657 conn->link_mode |= HCI_LM_AUTH;
659 if (test_bit(HCI_ENCRYPT, &hdev->flags))
660 conn->link_mode |= HCI_LM_ENCRYPT;
662 /* Set link policy */
663 if (conn->type == ACL_LINK && hdev->link_policy) {
664 struct hci_cp_write_link_policy cp;
665 cp.handle = ev->handle;
666 cp.policy = __cpu_to_le16(hdev->link_policy);
667 hci_send_cmd(hdev, OGF_LINK_POLICY, OCF_WRITE_LINK_POLICY, sizeof(cp), &cp);
670 /* Set packet type for incoming connection */
672 struct hci_cp_change_conn_ptype cp;
673 cp.handle = ev->handle;
674 cp.pkt_type = (conn->type == ACL_LINK) ?
675 __cpu_to_le16(hdev->pkt_type & ACL_PTYPE_MASK):
676 __cpu_to_le16(hdev->pkt_type & SCO_PTYPE_MASK);
678 hci_send_cmd(hdev, OGF_LINK_CTL, OCF_CHANGE_CONN_PTYPE, sizeof(cp), &cp);
681 conn->state = BT_CLOSED;
683 if (conn->type == ACL_LINK) {
684 struct hci_conn *sco = conn->link;
687 hci_add_sco(sco, conn->handle);
689 hci_proto_connect_cfm(sco, ev->status);
695 hci_proto_connect_cfm(conn, ev->status);
699 hci_dev_unlock(hdev);
702 /* Disconnect Complete */
703 static inline void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
705 struct hci_ev_disconn_complete *ev = (struct hci_ev_disconn_complete *) skb->data;
706 struct hci_conn *conn = NULL;
707 __u16 handle = __le16_to_cpu(ev->handle);
709 BT_DBG("%s status %d", hdev->name, ev->status);
716 conn = hci_conn_hash_lookup_handle(hdev, handle);
718 conn->state = BT_CLOSED;
719 hci_proto_disconn_ind(conn, ev->reason);
723 hci_dev_unlock(hdev);
726 /* Number of completed packets */
727 static inline void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
729 struct hci_ev_num_comp_pkts *ev = (struct hci_ev_num_comp_pkts *) skb->data;
733 skb_pull(skb, sizeof(*ev));
735 BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
737 if (skb->len < ev->num_hndl * 4) {
738 BT_DBG("%s bad parameters", hdev->name);
742 tasklet_disable(&hdev->tx_task);
744 for (i = 0, ptr = (__le16 *) skb->data; i < ev->num_hndl; i++) {
745 struct hci_conn *conn;
748 handle = __le16_to_cpu(get_unaligned(ptr++));
749 count = __le16_to_cpu(get_unaligned(ptr++));
751 conn = hci_conn_hash_lookup_handle(hdev, handle);
755 if (conn->type == SCO_LINK) {
756 if ((hdev->sco_cnt += count) > hdev->sco_pkts)
757 hdev->sco_cnt = hdev->sco_pkts;
759 if ((hdev->acl_cnt += count) > hdev->acl_pkts)
760 hdev->acl_cnt = hdev->acl_pkts;
766 tasklet_enable(&hdev->tx_task);
770 static inline void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
772 struct hci_ev_role_change *ev = (struct hci_ev_role_change *) skb->data;
773 struct hci_conn *conn = NULL;
775 BT_DBG("%s status %d", hdev->name, ev->status);
779 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
783 conn->link_mode &= ~HCI_LM_MASTER;
785 conn->link_mode |= HCI_LM_MASTER;
788 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->pend);
790 hci_role_switch_cfm(conn, ev->status, ev->role);
793 hci_dev_unlock(hdev);
796 /* Authentication Complete */
797 static inline void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
799 struct hci_ev_auth_complete *ev = (struct hci_ev_auth_complete *) skb->data;
800 struct hci_conn *conn = NULL;
801 __u16 handle = __le16_to_cpu(ev->handle);
803 BT_DBG("%s status %d", hdev->name, ev->status);
807 conn = hci_conn_hash_lookup_handle(hdev, handle);
810 conn->link_mode |= HCI_LM_AUTH;
812 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
814 hci_auth_cfm(conn, ev->status);
816 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
818 struct hci_cp_set_conn_encrypt cp;
819 cp.handle = __cpu_to_le16(conn->handle);
821 hci_send_cmd(conn->hdev, OGF_LINK_CTL,
822 OCF_SET_CONN_ENCRYPT,
825 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
826 hci_encrypt_cfm(conn, ev->status, 0x00);
831 hci_dev_unlock(hdev);
834 /* Encryption Change */
835 static inline void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
837 struct hci_ev_encrypt_change *ev = (struct hci_ev_encrypt_change *) skb->data;
838 struct hci_conn *conn = NULL;
839 __u16 handle = __le16_to_cpu(ev->handle);
841 BT_DBG("%s status %d", hdev->name, ev->status);
845 conn = hci_conn_hash_lookup_handle(hdev, handle);
849 conn->link_mode |= HCI_LM_ENCRYPT;
851 conn->link_mode &= ~HCI_LM_ENCRYPT;
854 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
856 hci_encrypt_cfm(conn, ev->status, ev->encrypt);
859 hci_dev_unlock(hdev);
862 /* Change Connection Link Key Complete */
863 static inline void hci_change_conn_link_key_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
865 struct hci_ev_change_conn_link_key_complete *ev = (struct hci_ev_change_conn_link_key_complete *) skb->data;
866 struct hci_conn *conn = NULL;
867 __u16 handle = __le16_to_cpu(ev->handle);
869 BT_DBG("%s status %d", hdev->name, ev->status);
873 conn = hci_conn_hash_lookup_handle(hdev, handle);
876 conn->link_mode |= HCI_LM_SECURE;
878 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
880 hci_key_change_cfm(conn, ev->status);
883 hci_dev_unlock(hdev);
886 /* Pin Code Request*/
887 static inline void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
891 /* Link Key Request */
892 static inline void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
896 /* Link Key Notification */
897 static inline void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
902 static inline void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
904 struct hci_ev_clock_offset *ev = (struct hci_ev_clock_offset *) skb->data;
905 struct hci_conn *conn = NULL;
906 __u16 handle = __le16_to_cpu(ev->handle);
908 BT_DBG("%s status %d", hdev->name, ev->status);
912 conn = hci_conn_hash_lookup_handle(hdev, handle);
913 if (conn && !ev->status) {
914 struct inquiry_entry *ie;
916 if ((ie = hci_inquiry_cache_lookup(hdev, &conn->dst))) {
917 ie->data.clock_offset = ev->clock_offset;
918 ie->timestamp = jiffies;
922 hci_dev_unlock(hdev);
925 /* Page Scan Repetition Mode */
926 static inline void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
928 struct hci_ev_pscan_rep_mode *ev = (struct hci_ev_pscan_rep_mode *) skb->data;
929 struct inquiry_entry *ie;
931 BT_DBG("%s", hdev->name);
935 if ((ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr))) {
936 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
937 ie->timestamp = jiffies;
940 hci_dev_unlock(hdev);
943 void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
945 struct hci_event_hdr *hdr = (struct hci_event_hdr *) skb->data;
946 struct hci_ev_cmd_complete *ec;
947 struct hci_ev_cmd_status *cs;
948 u16 opcode, ocf, ogf;
950 skb_pull(skb, HCI_EVENT_HDR_SIZE);
952 BT_DBG("%s evt 0x%x", hdev->name, hdr->evt);
955 case HCI_EV_NUM_COMP_PKTS:
956 hci_num_comp_pkts_evt(hdev, skb);
959 case HCI_EV_INQUIRY_COMPLETE:
960 hci_inquiry_complete_evt(hdev, skb);
963 case HCI_EV_INQUIRY_RESULT:
964 hci_inquiry_result_evt(hdev, skb);
967 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
968 hci_inquiry_result_with_rssi_evt(hdev, skb);
971 case HCI_EV_EXTENDED_INQUIRY_RESULT:
972 hci_extended_inquiry_result_evt(hdev, skb);
975 case HCI_EV_CONN_REQUEST:
976 hci_conn_request_evt(hdev, skb);
979 case HCI_EV_CONN_COMPLETE:
980 hci_conn_complete_evt(hdev, skb);
983 case HCI_EV_DISCONN_COMPLETE:
984 hci_disconn_complete_evt(hdev, skb);
987 case HCI_EV_ROLE_CHANGE:
988 hci_role_change_evt(hdev, skb);
991 case HCI_EV_AUTH_COMPLETE:
992 hci_auth_complete_evt(hdev, skb);
995 case HCI_EV_ENCRYPT_CHANGE:
996 hci_encrypt_change_evt(hdev, skb);
999 case HCI_EV_CHANGE_CONN_LINK_KEY_COMPLETE:
1000 hci_change_conn_link_key_complete_evt(hdev, skb);
1003 case HCI_EV_PIN_CODE_REQ:
1004 hci_pin_code_request_evt(hdev, skb);
1007 case HCI_EV_LINK_KEY_REQ:
1008 hci_link_key_request_evt(hdev, skb);
1011 case HCI_EV_LINK_KEY_NOTIFY:
1012 hci_link_key_notify_evt(hdev, skb);
1015 case HCI_EV_CLOCK_OFFSET:
1016 hci_clock_offset_evt(hdev, skb);
1019 case HCI_EV_PSCAN_REP_MODE:
1020 hci_pscan_rep_mode_evt(hdev, skb);
1023 case HCI_EV_CMD_STATUS:
1024 cs = (struct hci_ev_cmd_status *) skb->data;
1025 skb_pull(skb, sizeof(cs));
1027 opcode = __le16_to_cpu(cs->opcode);
1028 ogf = hci_opcode_ogf(opcode);
1029 ocf = hci_opcode_ocf(opcode);
1032 case OGF_INFO_PARAM:
1033 hci_cs_info_param(hdev, ocf, cs->status);
1037 hci_cs_host_ctl(hdev, ocf, cs->status);
1041 hci_cs_link_ctl(hdev, ocf, cs->status);
1044 case OGF_LINK_POLICY:
1045 hci_cs_link_policy(hdev, ocf, cs->status);
1049 BT_DBG("%s Command Status OGF %x", hdev->name, ogf);
1054 atomic_set(&hdev->cmd_cnt, 1);
1055 if (!skb_queue_empty(&hdev->cmd_q))
1056 hci_sched_cmd(hdev);
1060 case HCI_EV_CMD_COMPLETE:
1061 ec = (struct hci_ev_cmd_complete *) skb->data;
1062 skb_pull(skb, sizeof(*ec));
1064 opcode = __le16_to_cpu(ec->opcode);
1065 ogf = hci_opcode_ogf(opcode);
1066 ocf = hci_opcode_ocf(opcode);
1069 case OGF_INFO_PARAM:
1070 hci_cc_info_param(hdev, ocf, skb);
1074 hci_cc_host_ctl(hdev, ocf, skb);
1078 hci_cc_link_ctl(hdev, ocf, skb);
1081 case OGF_LINK_POLICY:
1082 hci_cc_link_policy(hdev, ocf, skb);
1086 BT_DBG("%s Command Completed OGF %x", hdev->name, ogf);
1091 atomic_set(&hdev->cmd_cnt, 1);
1092 if (!skb_queue_empty(&hdev->cmd_q))
1093 hci_sched_cmd(hdev);
1099 hdev->stat.evt_rx++;
1102 /* Generate internal stack event */
1103 void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
1105 struct hci_event_hdr *hdr;
1106 struct hci_ev_stack_internal *ev;
1107 struct sk_buff *skb;
1109 skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC);
1113 hdr = (void *) skb_put(skb, HCI_EVENT_HDR_SIZE);
1114 hdr->evt = HCI_EV_STACK_INTERNAL;
1115 hdr->plen = sizeof(*ev) + dlen;
1117 ev = (void *) skb_put(skb, sizeof(*ev) + dlen);
1119 memcpy(ev->data, data, dlen);
1121 bt_cb(skb)->incoming = 1;
1122 __net_timestamp(skb);
1124 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
1125 skb->dev = (void *) hdev;
1126 hci_send_to_sock(hdev, skb);