[PATCH] selinux: eliminate selinux_task_ctxid
[pv_ops_mirror.git] / net / bluetooth / hci_event.c
blob3896dabab11da70898640581f9c3e5b2195f7678
1 /*
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>
40 #include <net/sock.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
50 #undef BT_DBG
51 #define BT_DBG(D...)
52 #endif
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)
59 __u8 status;
61 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
63 switch (ocf) {
64 case OCF_INQUIRY_CANCEL:
65 status = *((__u8 *) skb->data);
67 if (status) {
68 BT_DBG("%s Inquiry cancel error: status 0x%x", hdev->name, status);
69 } else {
70 clear_bit(HCI_INQUIRY, &hdev->flags);
71 hci_req_complete(hdev, status);
73 break;
75 default:
76 BT_DBG("%s Command complete: ogf LINK_CTL ocf %x", hdev->name, ocf);
77 break;
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;
86 struct hci_rp_write_link_policy *lp;
87 void *sent;
89 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
91 switch (ocf) {
92 case OCF_ROLE_DISCOVERY:
93 rd = (void *) skb->data;
95 if (rd->status)
96 break;
98 hci_dev_lock(hdev);
100 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rd->handle));
101 if (conn) {
102 if (rd->role)
103 conn->link_mode &= ~HCI_LM_MASTER;
104 else
105 conn->link_mode |= HCI_LM_MASTER;
108 hci_dev_unlock(hdev);
109 break;
111 case OCF_WRITE_LINK_POLICY:
112 sent = hci_sent_cmd_data(hdev, OGF_LINK_POLICY, OCF_WRITE_LINK_POLICY);
113 if (!sent)
114 break;
116 lp = (struct hci_rp_write_link_policy *) skb->data;
118 if (lp->status)
119 break;
121 hci_dev_lock(hdev);
123 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(lp->handle));
124 if (conn) {
125 __le16 policy = get_unaligned((__le16 *) (sent + 2));
126 conn->link_policy = __le16_to_cpu(policy);
129 hci_dev_unlock(hdev);
130 break;
132 default:
133 BT_DBG("%s: Command complete: ogf LINK_POLICY ocf %x",
134 hdev->name, ocf);
135 break;
139 /* Command Complete OGF HOST_CTL */
140 static void hci_cc_host_ctl(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
142 __u8 status, param;
143 __u16 setting;
144 struct hci_rp_read_voice_setting *vs;
145 void *sent;
147 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
149 switch (ocf) {
150 case OCF_RESET:
151 status = *((__u8 *) skb->data);
152 hci_req_complete(hdev, status);
153 break;
155 case OCF_SET_EVENT_FLT:
156 status = *((__u8 *) skb->data);
157 if (status) {
158 BT_DBG("%s SET_EVENT_FLT failed %d", hdev->name, status);
159 } else {
160 BT_DBG("%s SET_EVENT_FLT succeseful", hdev->name);
162 break;
164 case OCF_WRITE_AUTH_ENABLE:
165 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_AUTH_ENABLE);
166 if (!sent)
167 break;
169 status = *((__u8 *) skb->data);
170 param = *((__u8 *) sent);
172 if (!status) {
173 if (param == AUTH_ENABLED)
174 set_bit(HCI_AUTH, &hdev->flags);
175 else
176 clear_bit(HCI_AUTH, &hdev->flags);
178 hci_req_complete(hdev, status);
179 break;
181 case OCF_WRITE_ENCRYPT_MODE:
182 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_ENCRYPT_MODE);
183 if (!sent)
184 break;
186 status = *((__u8 *) skb->data);
187 param = *((__u8 *) sent);
189 if (!status) {
190 if (param)
191 set_bit(HCI_ENCRYPT, &hdev->flags);
192 else
193 clear_bit(HCI_ENCRYPT, &hdev->flags);
195 hci_req_complete(hdev, status);
196 break;
198 case OCF_WRITE_CA_TIMEOUT:
199 status = *((__u8 *) skb->data);
200 if (status) {
201 BT_DBG("%s OCF_WRITE_CA_TIMEOUT failed %d", hdev->name, status);
202 } else {
203 BT_DBG("%s OCF_WRITE_CA_TIMEOUT succeseful", hdev->name);
205 break;
207 case OCF_WRITE_PG_TIMEOUT:
208 status = *((__u8 *) skb->data);
209 if (status) {
210 BT_DBG("%s OCF_WRITE_PG_TIMEOUT failed %d", hdev->name, status);
211 } else {
212 BT_DBG("%s: OCF_WRITE_PG_TIMEOUT succeseful", hdev->name);
214 break;
216 case OCF_WRITE_SCAN_ENABLE:
217 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_SCAN_ENABLE);
218 if (!sent)
219 break;
221 status = *((__u8 *) skb->data);
222 param = *((__u8 *) sent);
224 BT_DBG("param 0x%x", param);
226 if (!status) {
227 clear_bit(HCI_PSCAN, &hdev->flags);
228 clear_bit(HCI_ISCAN, &hdev->flags);
229 if (param & SCAN_INQUIRY)
230 set_bit(HCI_ISCAN, &hdev->flags);
232 if (param & SCAN_PAGE)
233 set_bit(HCI_PSCAN, &hdev->flags);
235 hci_req_complete(hdev, status);
236 break;
238 case OCF_READ_VOICE_SETTING:
239 vs = (struct hci_rp_read_voice_setting *) skb->data;
241 if (vs->status) {
242 BT_DBG("%s READ_VOICE_SETTING failed %d", hdev->name, vs->status);
243 break;
246 setting = __le16_to_cpu(vs->voice_setting);
248 if (hdev->voice_setting != setting ) {
249 hdev->voice_setting = setting;
251 BT_DBG("%s: voice setting 0x%04x", hdev->name, setting);
253 if (hdev->notify) {
254 tasklet_disable(&hdev->tx_task);
255 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
256 tasklet_enable(&hdev->tx_task);
259 break;
261 case OCF_WRITE_VOICE_SETTING:
262 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_VOICE_SETTING);
263 if (!sent)
264 break;
266 status = *((__u8 *) skb->data);
267 setting = __le16_to_cpu(get_unaligned((__le16 *) sent));
269 if (!status && hdev->voice_setting != setting) {
270 hdev->voice_setting = setting;
272 BT_DBG("%s: voice setting 0x%04x", hdev->name, setting);
274 if (hdev->notify) {
275 tasklet_disable(&hdev->tx_task);
276 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
277 tasklet_enable(&hdev->tx_task);
280 hci_req_complete(hdev, status);
281 break;
283 case OCF_HOST_BUFFER_SIZE:
284 status = *((__u8 *) skb->data);
285 if (status) {
286 BT_DBG("%s OCF_BUFFER_SIZE failed %d", hdev->name, status);
287 hci_req_complete(hdev, status);
289 break;
291 default:
292 BT_DBG("%s Command complete: ogf HOST_CTL ocf %x", hdev->name, ocf);
293 break;
297 /* Command Complete OGF INFO_PARAM */
298 static void hci_cc_info_param(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
300 struct hci_rp_read_local_features *lf;
301 struct hci_rp_read_buffer_size *bs;
302 struct hci_rp_read_bd_addr *ba;
304 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
306 switch (ocf) {
307 case OCF_READ_LOCAL_FEATURES:
308 lf = (struct hci_rp_read_local_features *) skb->data;
310 if (lf->status) {
311 BT_DBG("%s READ_LOCAL_FEATURES failed %d", hdev->name, lf->status);
312 break;
315 memcpy(hdev->features, lf->features, sizeof(hdev->features));
317 /* Adjust default settings according to features
318 * supported by device. */
319 if (hdev->features[0] & LMP_3SLOT)
320 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
322 if (hdev->features[0] & LMP_5SLOT)
323 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
325 if (hdev->features[1] & LMP_HV2)
326 hdev->pkt_type |= (HCI_HV2);
328 if (hdev->features[1] & LMP_HV3)
329 hdev->pkt_type |= (HCI_HV3);
331 BT_DBG("%s: features 0x%x 0x%x 0x%x", hdev->name, lf->features[0], lf->features[1], lf->features[2]);
333 break;
335 case OCF_READ_BUFFER_SIZE:
336 bs = (struct hci_rp_read_buffer_size *) skb->data;
338 if (bs->status) {
339 BT_DBG("%s READ_BUFFER_SIZE failed %d", hdev->name, bs->status);
340 hci_req_complete(hdev, bs->status);
341 break;
344 hdev->acl_mtu = __le16_to_cpu(bs->acl_mtu);
345 hdev->sco_mtu = bs->sco_mtu;
346 hdev->acl_pkts = __le16_to_cpu(bs->acl_max_pkt);
347 hdev->sco_pkts = __le16_to_cpu(bs->sco_max_pkt);
349 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
350 hdev->sco_mtu = 64;
351 hdev->sco_pkts = 8;
354 hdev->acl_cnt = hdev->acl_pkts;
355 hdev->sco_cnt = hdev->sco_pkts;
357 BT_DBG("%s mtu: acl %d, sco %d max_pkt: acl %d, sco %d", hdev->name,
358 hdev->acl_mtu, hdev->sco_mtu, hdev->acl_pkts, hdev->sco_pkts);
359 break;
361 case OCF_READ_BD_ADDR:
362 ba = (struct hci_rp_read_bd_addr *) skb->data;
364 if (!ba->status) {
365 bacpy(&hdev->bdaddr, &ba->bdaddr);
366 } else {
367 BT_DBG("%s: READ_BD_ADDR failed %d", hdev->name, ba->status);
370 hci_req_complete(hdev, ba->status);
371 break;
373 default:
374 BT_DBG("%s Command complete: ogf INFO_PARAM ocf %x", hdev->name, ocf);
375 break;
379 /* Command Status OGF LINK_CTL */
380 static inline void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
382 struct hci_conn *conn;
383 struct hci_cp_create_conn *cp = hci_sent_cmd_data(hdev, OGF_LINK_CTL, OCF_CREATE_CONN);
385 if (!cp)
386 return;
388 hci_dev_lock(hdev);
390 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
392 BT_DBG("%s status 0x%x bdaddr %s conn %p", hdev->name,
393 status, batostr(&cp->bdaddr), conn);
395 if (status) {
396 if (conn && conn->state == BT_CONNECT) {
397 conn->state = BT_CLOSED;
398 hci_proto_connect_cfm(conn, status);
399 hci_conn_del(conn);
401 } else {
402 if (!conn) {
403 conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr);
404 if (conn) {
405 conn->out = 1;
406 conn->link_mode |= HCI_LM_MASTER;
407 } else
408 BT_ERR("No memmory for new connection");
412 hci_dev_unlock(hdev);
415 static void hci_cs_link_ctl(struct hci_dev *hdev, __u16 ocf, __u8 status)
417 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
419 switch (ocf) {
420 case OCF_CREATE_CONN:
421 hci_cs_create_conn(hdev, status);
422 break;
424 case OCF_ADD_SCO:
425 if (status) {
426 struct hci_conn *acl, *sco;
427 struct hci_cp_add_sco *cp = hci_sent_cmd_data(hdev, OGF_LINK_CTL, OCF_ADD_SCO);
428 __u16 handle;
430 if (!cp)
431 break;
433 handle = __le16_to_cpu(cp->handle);
435 BT_DBG("%s Add SCO error: handle %d status 0x%x", hdev->name, handle, status);
437 hci_dev_lock(hdev);
439 acl = hci_conn_hash_lookup_handle(hdev, handle);
440 if (acl && (sco = acl->link)) {
441 sco->state = BT_CLOSED;
443 hci_proto_connect_cfm(sco, status);
444 hci_conn_del(sco);
447 hci_dev_unlock(hdev);
449 break;
451 case OCF_INQUIRY:
452 if (status) {
453 BT_DBG("%s Inquiry error: status 0x%x", hdev->name, status);
454 hci_req_complete(hdev, status);
455 } else {
456 set_bit(HCI_INQUIRY, &hdev->flags);
458 break;
460 default:
461 BT_DBG("%s Command status: ogf LINK_CTL ocf %x status %d",
462 hdev->name, ocf, status);
463 break;
467 /* Command Status OGF LINK_POLICY */
468 static void hci_cs_link_policy(struct hci_dev *hdev, __u16 ocf, __u8 status)
470 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
472 switch (ocf) {
473 case OCF_SNIFF_MODE:
474 if (status) {
475 struct hci_conn *conn;
476 struct hci_cp_sniff_mode *cp = hci_sent_cmd_data(hdev, OGF_LINK_POLICY, OCF_SNIFF_MODE);
478 if (!cp)
479 break;
481 hci_dev_lock(hdev);
483 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
484 if (conn) {
485 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend);
488 hci_dev_unlock(hdev);
490 break;
492 case OCF_EXIT_SNIFF_MODE:
493 if (status) {
494 struct hci_conn *conn;
495 struct hci_cp_exit_sniff_mode *cp = hci_sent_cmd_data(hdev, OGF_LINK_POLICY, OCF_EXIT_SNIFF_MODE);
497 if (!cp)
498 break;
500 hci_dev_lock(hdev);
502 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
503 if (conn) {
504 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend);
507 hci_dev_unlock(hdev);
509 break;
511 default:
512 BT_DBG("%s Command status: ogf LINK_POLICY ocf %x", hdev->name, ocf);
513 break;
517 /* Command Status OGF HOST_CTL */
518 static void hci_cs_host_ctl(struct hci_dev *hdev, __u16 ocf, __u8 status)
520 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
522 switch (ocf) {
523 default:
524 BT_DBG("%s Command status: ogf HOST_CTL ocf %x", hdev->name, ocf);
525 break;
529 /* Command Status OGF INFO_PARAM */
530 static void hci_cs_info_param(struct hci_dev *hdev, __u16 ocf, __u8 status)
532 BT_DBG("%s: hci_cs_info_param: ocf 0x%x", hdev->name, ocf);
534 switch (ocf) {
535 default:
536 BT_DBG("%s Command status: ogf INFO_PARAM ocf %x", hdev->name, ocf);
537 break;
541 /* Inquiry Complete */
542 static inline void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
544 __u8 status = *((__u8 *) skb->data);
546 BT_DBG("%s status %d", hdev->name, status);
548 clear_bit(HCI_INQUIRY, &hdev->flags);
549 hci_req_complete(hdev, status);
552 /* Inquiry Result */
553 static inline void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
555 struct inquiry_data data;
556 struct inquiry_info *info = (struct inquiry_info *) (skb->data + 1);
557 int num_rsp = *((__u8 *) skb->data);
559 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
561 if (!num_rsp)
562 return;
564 hci_dev_lock(hdev);
566 for (; num_rsp; num_rsp--) {
567 bacpy(&data.bdaddr, &info->bdaddr);
568 data.pscan_rep_mode = info->pscan_rep_mode;
569 data.pscan_period_mode = info->pscan_period_mode;
570 data.pscan_mode = info->pscan_mode;
571 memcpy(data.dev_class, info->dev_class, 3);
572 data.clock_offset = info->clock_offset;
573 data.rssi = 0x00;
574 info++;
575 hci_inquiry_cache_update(hdev, &data);
578 hci_dev_unlock(hdev);
581 /* Inquiry Result With RSSI */
582 static inline void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, struct sk_buff *skb)
584 struct inquiry_data data;
585 int num_rsp = *((__u8 *) skb->data);
587 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
589 if (!num_rsp)
590 return;
592 hci_dev_lock(hdev);
594 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
595 struct inquiry_info_with_rssi_and_pscan_mode *info =
596 (struct inquiry_info_with_rssi_and_pscan_mode *) (skb->data + 1);
598 for (; num_rsp; num_rsp--) {
599 bacpy(&data.bdaddr, &info->bdaddr);
600 data.pscan_rep_mode = info->pscan_rep_mode;
601 data.pscan_period_mode = info->pscan_period_mode;
602 data.pscan_mode = info->pscan_mode;
603 memcpy(data.dev_class, info->dev_class, 3);
604 data.clock_offset = info->clock_offset;
605 data.rssi = info->rssi;
606 info++;
607 hci_inquiry_cache_update(hdev, &data);
609 } else {
610 struct inquiry_info_with_rssi *info =
611 (struct inquiry_info_with_rssi *) (skb->data + 1);
613 for (; num_rsp; num_rsp--) {
614 bacpy(&data.bdaddr, &info->bdaddr);
615 data.pscan_rep_mode = info->pscan_rep_mode;
616 data.pscan_period_mode = info->pscan_period_mode;
617 data.pscan_mode = 0x00;
618 memcpy(data.dev_class, info->dev_class, 3);
619 data.clock_offset = info->clock_offset;
620 data.rssi = info->rssi;
621 info++;
622 hci_inquiry_cache_update(hdev, &data);
626 hci_dev_unlock(hdev);
629 /* Extended Inquiry Result */
630 static inline void hci_extended_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
632 struct inquiry_data data;
633 struct extended_inquiry_info *info = (struct extended_inquiry_info *) (skb->data + 1);
634 int num_rsp = *((__u8 *) skb->data);
636 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
638 if (!num_rsp)
639 return;
641 hci_dev_lock(hdev);
643 for (; num_rsp; num_rsp--) {
644 bacpy(&data.bdaddr, &info->bdaddr);
645 data.pscan_rep_mode = info->pscan_rep_mode;
646 data.pscan_period_mode = info->pscan_period_mode;
647 data.pscan_mode = 0x00;
648 memcpy(data.dev_class, info->dev_class, 3);
649 data.clock_offset = info->clock_offset;
650 data.rssi = info->rssi;
651 info++;
652 hci_inquiry_cache_update(hdev, &data);
655 hci_dev_unlock(hdev);
658 /* Connect Request */
659 static inline void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
661 struct hci_ev_conn_request *ev = (struct hci_ev_conn_request *) skb->data;
662 int mask = hdev->link_mode;
664 BT_DBG("%s Connection request: %s type 0x%x", hdev->name,
665 batostr(&ev->bdaddr), ev->link_type);
667 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type);
669 if (mask & HCI_LM_ACCEPT) {
670 /* Connection accepted */
671 struct hci_conn *conn;
672 struct hci_cp_accept_conn_req cp;
674 hci_dev_lock(hdev);
675 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
676 if (!conn) {
677 if (!(conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr))) {
678 BT_ERR("No memmory for new connection");
679 hci_dev_unlock(hdev);
680 return;
683 memcpy(conn->dev_class, ev->dev_class, 3);
684 conn->state = BT_CONNECT;
685 hci_dev_unlock(hdev);
687 bacpy(&cp.bdaddr, &ev->bdaddr);
689 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
690 cp.role = 0x00; /* Become master */
691 else
692 cp.role = 0x01; /* Remain slave */
694 hci_send_cmd(hdev, OGF_LINK_CTL,
695 OCF_ACCEPT_CONN_REQ, sizeof(cp), &cp);
696 } else {
697 /* Connection rejected */
698 struct hci_cp_reject_conn_req cp;
700 bacpy(&cp.bdaddr, &ev->bdaddr);
701 cp.reason = 0x0f;
702 hci_send_cmd(hdev, OGF_LINK_CTL,
703 OCF_REJECT_CONN_REQ, sizeof(cp), &cp);
707 /* Connect Complete */
708 static inline void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
710 struct hci_ev_conn_complete *ev = (struct hci_ev_conn_complete *) skb->data;
711 struct hci_conn *conn;
713 BT_DBG("%s", hdev->name);
715 hci_dev_lock(hdev);
717 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
718 if (!conn) {
719 hci_dev_unlock(hdev);
720 return;
723 if (!ev->status) {
724 conn->handle = __le16_to_cpu(ev->handle);
725 conn->state = BT_CONNECTED;
727 if (test_bit(HCI_AUTH, &hdev->flags))
728 conn->link_mode |= HCI_LM_AUTH;
730 if (test_bit(HCI_ENCRYPT, &hdev->flags))
731 conn->link_mode |= HCI_LM_ENCRYPT;
733 /* Get remote features */
734 if (conn->type == ACL_LINK) {
735 struct hci_cp_read_remote_features cp;
736 cp.handle = ev->handle;
737 hci_send_cmd(hdev, OGF_LINK_CTL,
738 OCF_READ_REMOTE_FEATURES, sizeof(cp), &cp);
741 /* Set link policy */
742 if (conn->type == ACL_LINK && hdev->link_policy) {
743 struct hci_cp_write_link_policy cp;
744 cp.handle = ev->handle;
745 cp.policy = __cpu_to_le16(hdev->link_policy);
746 hci_send_cmd(hdev, OGF_LINK_POLICY,
747 OCF_WRITE_LINK_POLICY, sizeof(cp), &cp);
750 /* Set packet type for incoming connection */
751 if (!conn->out) {
752 struct hci_cp_change_conn_ptype cp;
753 cp.handle = ev->handle;
754 cp.pkt_type = (conn->type == ACL_LINK) ?
755 __cpu_to_le16(hdev->pkt_type & ACL_PTYPE_MASK):
756 __cpu_to_le16(hdev->pkt_type & SCO_PTYPE_MASK);
758 hci_send_cmd(hdev, OGF_LINK_CTL,
759 OCF_CHANGE_CONN_PTYPE, sizeof(cp), &cp);
761 } else
762 conn->state = BT_CLOSED;
764 if (conn->type == ACL_LINK) {
765 struct hci_conn *sco = conn->link;
766 if (sco) {
767 if (!ev->status)
768 hci_add_sco(sco, conn->handle);
769 else {
770 hci_proto_connect_cfm(sco, ev->status);
771 hci_conn_del(sco);
776 hci_proto_connect_cfm(conn, ev->status);
777 if (ev->status)
778 hci_conn_del(conn);
780 hci_dev_unlock(hdev);
783 /* Disconnect Complete */
784 static inline void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
786 struct hci_ev_disconn_complete *ev = (struct hci_ev_disconn_complete *) skb->data;
787 struct hci_conn *conn;
789 BT_DBG("%s status %d", hdev->name, ev->status);
791 if (ev->status)
792 return;
794 hci_dev_lock(hdev);
796 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
797 if (conn) {
798 conn->state = BT_CLOSED;
799 hci_proto_disconn_ind(conn, ev->reason);
800 hci_conn_del(conn);
803 hci_dev_unlock(hdev);
806 /* Number of completed packets */
807 static inline void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
809 struct hci_ev_num_comp_pkts *ev = (struct hci_ev_num_comp_pkts *) skb->data;
810 __le16 *ptr;
811 int i;
813 skb_pull(skb, sizeof(*ev));
815 BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
817 if (skb->len < ev->num_hndl * 4) {
818 BT_DBG("%s bad parameters", hdev->name);
819 return;
822 tasklet_disable(&hdev->tx_task);
824 for (i = 0, ptr = (__le16 *) skb->data; i < ev->num_hndl; i++) {
825 struct hci_conn *conn;
826 __u16 handle, count;
828 handle = __le16_to_cpu(get_unaligned(ptr++));
829 count = __le16_to_cpu(get_unaligned(ptr++));
831 conn = hci_conn_hash_lookup_handle(hdev, handle);
832 if (conn) {
833 conn->sent -= count;
835 if (conn->type == SCO_LINK) {
836 if ((hdev->sco_cnt += count) > hdev->sco_pkts)
837 hdev->sco_cnt = hdev->sco_pkts;
838 } else {
839 if ((hdev->acl_cnt += count) > hdev->acl_pkts)
840 hdev->acl_cnt = hdev->acl_pkts;
844 hci_sched_tx(hdev);
846 tasklet_enable(&hdev->tx_task);
849 /* Role Change */
850 static inline void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
852 struct hci_ev_role_change *ev = (struct hci_ev_role_change *) skb->data;
853 struct hci_conn *conn;
855 BT_DBG("%s status %d", hdev->name, ev->status);
857 hci_dev_lock(hdev);
859 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
860 if (conn) {
861 if (!ev->status) {
862 if (ev->role)
863 conn->link_mode &= ~HCI_LM_MASTER;
864 else
865 conn->link_mode |= HCI_LM_MASTER;
868 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->pend);
870 hci_role_switch_cfm(conn, ev->status, ev->role);
873 hci_dev_unlock(hdev);
876 /* Mode Change */
877 static inline void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
879 struct hci_ev_mode_change *ev = (struct hci_ev_mode_change *) skb->data;
880 struct hci_conn *conn;
882 BT_DBG("%s status %d", hdev->name, ev->status);
884 hci_dev_lock(hdev);
886 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
887 if (conn) {
888 conn->mode = ev->mode;
889 conn->interval = __le16_to_cpu(ev->interval);
891 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
892 if (conn->mode == HCI_CM_ACTIVE)
893 conn->power_save = 1;
894 else
895 conn->power_save = 0;
899 hci_dev_unlock(hdev);
902 /* Authentication Complete */
903 static inline void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
905 struct hci_ev_auth_complete *ev = (struct hci_ev_auth_complete *) skb->data;
906 struct hci_conn *conn;
908 BT_DBG("%s status %d", hdev->name, ev->status);
910 hci_dev_lock(hdev);
912 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
913 if (conn) {
914 if (!ev->status)
915 conn->link_mode |= HCI_LM_AUTH;
917 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
919 hci_auth_cfm(conn, ev->status);
921 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
922 if (!ev->status) {
923 struct hci_cp_set_conn_encrypt cp;
924 cp.handle = __cpu_to_le16(conn->handle);
925 cp.encrypt = 1;
926 hci_send_cmd(conn->hdev, OGF_LINK_CTL,
927 OCF_SET_CONN_ENCRYPT, sizeof(cp), &cp);
928 } else {
929 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
930 hci_encrypt_cfm(conn, ev->status, 0x00);
935 hci_dev_unlock(hdev);
938 /* Encryption Change */
939 static inline void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
941 struct hci_ev_encrypt_change *ev = (struct hci_ev_encrypt_change *) skb->data;
942 struct hci_conn *conn;
944 BT_DBG("%s status %d", hdev->name, ev->status);
946 hci_dev_lock(hdev);
948 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
949 if (conn) {
950 if (!ev->status) {
951 if (ev->encrypt)
952 conn->link_mode |= HCI_LM_ENCRYPT;
953 else
954 conn->link_mode &= ~HCI_LM_ENCRYPT;
957 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
959 hci_encrypt_cfm(conn, ev->status, ev->encrypt);
962 hci_dev_unlock(hdev);
965 /* Change Connection Link Key Complete */
966 static inline void hci_change_conn_link_key_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
968 struct hci_ev_change_conn_link_key_complete *ev = (struct hci_ev_change_conn_link_key_complete *) skb->data;
969 struct hci_conn *conn;
971 BT_DBG("%s status %d", hdev->name, ev->status);
973 hci_dev_lock(hdev);
975 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
976 if (conn) {
977 if (!ev->status)
978 conn->link_mode |= HCI_LM_SECURE;
980 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
982 hci_key_change_cfm(conn, ev->status);
985 hci_dev_unlock(hdev);
988 /* Pin Code Request*/
989 static inline void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
993 /* Link Key Request */
994 static inline void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
998 /* Link Key Notification */
999 static inline void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
1003 /* Remote Features */
1004 static inline void hci_remote_features_evt(struct hci_dev *hdev, struct sk_buff *skb)
1006 struct hci_ev_remote_features *ev = (struct hci_ev_remote_features *) skb->data;
1007 struct hci_conn *conn;
1009 BT_DBG("%s status %d", hdev->name, ev->status);
1011 hci_dev_lock(hdev);
1013 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1014 if (conn && !ev->status) {
1015 memcpy(conn->features, ev->features, sizeof(conn->features));
1018 hci_dev_unlock(hdev);
1021 /* Clock Offset */
1022 static inline void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
1024 struct hci_ev_clock_offset *ev = (struct hci_ev_clock_offset *) skb->data;
1025 struct hci_conn *conn;
1027 BT_DBG("%s status %d", hdev->name, ev->status);
1029 hci_dev_lock(hdev);
1031 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1032 if (conn && !ev->status) {
1033 struct inquiry_entry *ie;
1035 if ((ie = hci_inquiry_cache_lookup(hdev, &conn->dst))) {
1036 ie->data.clock_offset = ev->clock_offset;
1037 ie->timestamp = jiffies;
1041 hci_dev_unlock(hdev);
1044 /* Page Scan Repetition Mode */
1045 static inline void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
1047 struct hci_ev_pscan_rep_mode *ev = (struct hci_ev_pscan_rep_mode *) skb->data;
1048 struct inquiry_entry *ie;
1050 BT_DBG("%s", hdev->name);
1052 hci_dev_lock(hdev);
1054 if ((ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr))) {
1055 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
1056 ie->timestamp = jiffies;
1059 hci_dev_unlock(hdev);
1062 /* Sniff Subrate */
1063 static inline void hci_sniff_subrate_evt(struct hci_dev *hdev, struct sk_buff *skb)
1065 struct hci_ev_sniff_subrate *ev = (struct hci_ev_sniff_subrate *) skb->data;
1066 struct hci_conn *conn;
1068 BT_DBG("%s status %d", hdev->name, ev->status);
1070 hci_dev_lock(hdev);
1072 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1073 if (conn) {
1076 hci_dev_unlock(hdev);
1079 void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
1081 struct hci_event_hdr *hdr = (struct hci_event_hdr *) skb->data;
1082 struct hci_ev_cmd_complete *ec;
1083 struct hci_ev_cmd_status *cs;
1084 u16 opcode, ocf, ogf;
1086 skb_pull(skb, HCI_EVENT_HDR_SIZE);
1088 BT_DBG("%s evt 0x%x", hdev->name, hdr->evt);
1090 switch (hdr->evt) {
1091 case HCI_EV_NUM_COMP_PKTS:
1092 hci_num_comp_pkts_evt(hdev, skb);
1093 break;
1095 case HCI_EV_INQUIRY_COMPLETE:
1096 hci_inquiry_complete_evt(hdev, skb);
1097 break;
1099 case HCI_EV_INQUIRY_RESULT:
1100 hci_inquiry_result_evt(hdev, skb);
1101 break;
1103 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
1104 hci_inquiry_result_with_rssi_evt(hdev, skb);
1105 break;
1107 case HCI_EV_EXTENDED_INQUIRY_RESULT:
1108 hci_extended_inquiry_result_evt(hdev, skb);
1109 break;
1111 case HCI_EV_CONN_REQUEST:
1112 hci_conn_request_evt(hdev, skb);
1113 break;
1115 case HCI_EV_CONN_COMPLETE:
1116 hci_conn_complete_evt(hdev, skb);
1117 break;
1119 case HCI_EV_DISCONN_COMPLETE:
1120 hci_disconn_complete_evt(hdev, skb);
1121 break;
1123 case HCI_EV_ROLE_CHANGE:
1124 hci_role_change_evt(hdev, skb);
1125 break;
1127 case HCI_EV_MODE_CHANGE:
1128 hci_mode_change_evt(hdev, skb);
1129 break;
1131 case HCI_EV_AUTH_COMPLETE:
1132 hci_auth_complete_evt(hdev, skb);
1133 break;
1135 case HCI_EV_ENCRYPT_CHANGE:
1136 hci_encrypt_change_evt(hdev, skb);
1137 break;
1139 case HCI_EV_CHANGE_CONN_LINK_KEY_COMPLETE:
1140 hci_change_conn_link_key_complete_evt(hdev, skb);
1141 break;
1143 case HCI_EV_PIN_CODE_REQ:
1144 hci_pin_code_request_evt(hdev, skb);
1145 break;
1147 case HCI_EV_LINK_KEY_REQ:
1148 hci_link_key_request_evt(hdev, skb);
1149 break;
1151 case HCI_EV_LINK_KEY_NOTIFY:
1152 hci_link_key_notify_evt(hdev, skb);
1153 break;
1155 case HCI_EV_REMOTE_FEATURES:
1156 hci_remote_features_evt(hdev, skb);
1157 break;
1159 case HCI_EV_CLOCK_OFFSET:
1160 hci_clock_offset_evt(hdev, skb);
1161 break;
1163 case HCI_EV_PSCAN_REP_MODE:
1164 hci_pscan_rep_mode_evt(hdev, skb);
1165 break;
1167 case HCI_EV_SNIFF_SUBRATE:
1168 hci_sniff_subrate_evt(hdev, skb);
1169 break;
1171 case HCI_EV_CMD_STATUS:
1172 cs = (struct hci_ev_cmd_status *) skb->data;
1173 skb_pull(skb, sizeof(cs));
1175 opcode = __le16_to_cpu(cs->opcode);
1176 ogf = hci_opcode_ogf(opcode);
1177 ocf = hci_opcode_ocf(opcode);
1179 switch (ogf) {
1180 case OGF_INFO_PARAM:
1181 hci_cs_info_param(hdev, ocf, cs->status);
1182 break;
1184 case OGF_HOST_CTL:
1185 hci_cs_host_ctl(hdev, ocf, cs->status);
1186 break;
1188 case OGF_LINK_CTL:
1189 hci_cs_link_ctl(hdev, ocf, cs->status);
1190 break;
1192 case OGF_LINK_POLICY:
1193 hci_cs_link_policy(hdev, ocf, cs->status);
1194 break;
1196 default:
1197 BT_DBG("%s Command Status OGF %x", hdev->name, ogf);
1198 break;
1201 if (cs->ncmd) {
1202 atomic_set(&hdev->cmd_cnt, 1);
1203 if (!skb_queue_empty(&hdev->cmd_q))
1204 hci_sched_cmd(hdev);
1206 break;
1208 case HCI_EV_CMD_COMPLETE:
1209 ec = (struct hci_ev_cmd_complete *) skb->data;
1210 skb_pull(skb, sizeof(*ec));
1212 opcode = __le16_to_cpu(ec->opcode);
1213 ogf = hci_opcode_ogf(opcode);
1214 ocf = hci_opcode_ocf(opcode);
1216 switch (ogf) {
1217 case OGF_INFO_PARAM:
1218 hci_cc_info_param(hdev, ocf, skb);
1219 break;
1221 case OGF_HOST_CTL:
1222 hci_cc_host_ctl(hdev, ocf, skb);
1223 break;
1225 case OGF_LINK_CTL:
1226 hci_cc_link_ctl(hdev, ocf, skb);
1227 break;
1229 case OGF_LINK_POLICY:
1230 hci_cc_link_policy(hdev, ocf, skb);
1231 break;
1233 default:
1234 BT_DBG("%s Command Completed OGF %x", hdev->name, ogf);
1235 break;
1238 if (ec->ncmd) {
1239 atomic_set(&hdev->cmd_cnt, 1);
1240 if (!skb_queue_empty(&hdev->cmd_q))
1241 hci_sched_cmd(hdev);
1243 break;
1246 kfree_skb(skb);
1247 hdev->stat.evt_rx++;
1250 /* Generate internal stack event */
1251 void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
1253 struct hci_event_hdr *hdr;
1254 struct hci_ev_stack_internal *ev;
1255 struct sk_buff *skb;
1257 skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC);
1258 if (!skb)
1259 return;
1261 hdr = (void *) skb_put(skb, HCI_EVENT_HDR_SIZE);
1262 hdr->evt = HCI_EV_STACK_INTERNAL;
1263 hdr->plen = sizeof(*ev) + dlen;
1265 ev = (void *) skb_put(skb, sizeof(*ev) + dlen);
1266 ev->type = type;
1267 memcpy(ev->data, data, dlen);
1269 bt_cb(skb)->incoming = 1;
1270 __net_timestamp(skb);
1272 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
1273 skb->dev = (void *) hdev;
1274 hci_send_to_sock(hdev, skb);
1275 kfree_skb(skb);