Linux 2.6.19-rc6
[cris-mirror.git] / net / bluetooth / hci_event.c
blob65f094845719126013cfb9f3c1c19e11f4b2b566
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 case OCF_EXIT_PERIODIC_INQ:
66 status = *((__u8 *) skb->data);
68 if (status) {
69 BT_DBG("%s Inquiry cancel error: status 0x%x", hdev->name, status);
70 } else {
71 clear_bit(HCI_INQUIRY, &hdev->flags);
72 hci_req_complete(hdev, status);
74 break;
76 default:
77 BT_DBG("%s Command complete: ogf LINK_CTL ocf %x", hdev->name, ocf);
78 break;
82 /* Command Complete OGF LINK_POLICY */
83 static void hci_cc_link_policy(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
85 struct hci_conn *conn;
86 struct hci_rp_role_discovery *rd;
87 struct hci_rp_write_link_policy *lp;
88 void *sent;
90 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
92 switch (ocf) {
93 case OCF_ROLE_DISCOVERY:
94 rd = (void *) skb->data;
96 if (rd->status)
97 break;
99 hci_dev_lock(hdev);
101 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rd->handle));
102 if (conn) {
103 if (rd->role)
104 conn->link_mode &= ~HCI_LM_MASTER;
105 else
106 conn->link_mode |= HCI_LM_MASTER;
109 hci_dev_unlock(hdev);
110 break;
112 case OCF_WRITE_LINK_POLICY:
113 sent = hci_sent_cmd_data(hdev, OGF_LINK_POLICY, OCF_WRITE_LINK_POLICY);
114 if (!sent)
115 break;
117 lp = (struct hci_rp_write_link_policy *) skb->data;
119 if (lp->status)
120 break;
122 hci_dev_lock(hdev);
124 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(lp->handle));
125 if (conn) {
126 __le16 policy = get_unaligned((__le16 *) (sent + 2));
127 conn->link_policy = __le16_to_cpu(policy);
130 hci_dev_unlock(hdev);
131 break;
133 default:
134 BT_DBG("%s: Command complete: ogf LINK_POLICY ocf %x",
135 hdev->name, ocf);
136 break;
140 /* Command Complete OGF HOST_CTL */
141 static void hci_cc_host_ctl(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
143 __u8 status, param;
144 __u16 setting;
145 struct hci_rp_read_voice_setting *vs;
146 void *sent;
148 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
150 switch (ocf) {
151 case OCF_RESET:
152 status = *((__u8 *) skb->data);
153 hci_req_complete(hdev, status);
154 break;
156 case OCF_SET_EVENT_FLT:
157 status = *((__u8 *) skb->data);
158 if (status) {
159 BT_DBG("%s SET_EVENT_FLT failed %d", hdev->name, status);
160 } else {
161 BT_DBG("%s SET_EVENT_FLT succeseful", hdev->name);
163 break;
165 case OCF_WRITE_AUTH_ENABLE:
166 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_AUTH_ENABLE);
167 if (!sent)
168 break;
170 status = *((__u8 *) skb->data);
171 param = *((__u8 *) sent);
173 if (!status) {
174 if (param == AUTH_ENABLED)
175 set_bit(HCI_AUTH, &hdev->flags);
176 else
177 clear_bit(HCI_AUTH, &hdev->flags);
179 hci_req_complete(hdev, status);
180 break;
182 case OCF_WRITE_ENCRYPT_MODE:
183 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_ENCRYPT_MODE);
184 if (!sent)
185 break;
187 status = *((__u8 *) skb->data);
188 param = *((__u8 *) sent);
190 if (!status) {
191 if (param)
192 set_bit(HCI_ENCRYPT, &hdev->flags);
193 else
194 clear_bit(HCI_ENCRYPT, &hdev->flags);
196 hci_req_complete(hdev, status);
197 break;
199 case OCF_WRITE_CA_TIMEOUT:
200 status = *((__u8 *) skb->data);
201 if (status) {
202 BT_DBG("%s OCF_WRITE_CA_TIMEOUT failed %d", hdev->name, status);
203 } else {
204 BT_DBG("%s OCF_WRITE_CA_TIMEOUT succeseful", hdev->name);
206 break;
208 case OCF_WRITE_PG_TIMEOUT:
209 status = *((__u8 *) skb->data);
210 if (status) {
211 BT_DBG("%s OCF_WRITE_PG_TIMEOUT failed %d", hdev->name, status);
212 } else {
213 BT_DBG("%s: OCF_WRITE_PG_TIMEOUT succeseful", hdev->name);
215 break;
217 case OCF_WRITE_SCAN_ENABLE:
218 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_SCAN_ENABLE);
219 if (!sent)
220 break;
222 status = *((__u8 *) skb->data);
223 param = *((__u8 *) sent);
225 BT_DBG("param 0x%x", param);
227 if (!status) {
228 clear_bit(HCI_PSCAN, &hdev->flags);
229 clear_bit(HCI_ISCAN, &hdev->flags);
230 if (param & SCAN_INQUIRY)
231 set_bit(HCI_ISCAN, &hdev->flags);
233 if (param & SCAN_PAGE)
234 set_bit(HCI_PSCAN, &hdev->flags);
236 hci_req_complete(hdev, status);
237 break;
239 case OCF_READ_VOICE_SETTING:
240 vs = (struct hci_rp_read_voice_setting *) skb->data;
242 if (vs->status) {
243 BT_DBG("%s READ_VOICE_SETTING failed %d", hdev->name, vs->status);
244 break;
247 setting = __le16_to_cpu(vs->voice_setting);
249 if (hdev->voice_setting != setting ) {
250 hdev->voice_setting = setting;
252 BT_DBG("%s: voice setting 0x%04x", hdev->name, setting);
254 if (hdev->notify) {
255 tasklet_disable(&hdev->tx_task);
256 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
257 tasklet_enable(&hdev->tx_task);
260 break;
262 case OCF_WRITE_VOICE_SETTING:
263 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_VOICE_SETTING);
264 if (!sent)
265 break;
267 status = *((__u8 *) skb->data);
268 setting = __le16_to_cpu(get_unaligned((__le16 *) sent));
270 if (!status && hdev->voice_setting != setting) {
271 hdev->voice_setting = setting;
273 BT_DBG("%s: voice setting 0x%04x", hdev->name, setting);
275 if (hdev->notify) {
276 tasklet_disable(&hdev->tx_task);
277 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
278 tasklet_enable(&hdev->tx_task);
281 hci_req_complete(hdev, status);
282 break;
284 case OCF_HOST_BUFFER_SIZE:
285 status = *((__u8 *) skb->data);
286 if (status) {
287 BT_DBG("%s OCF_BUFFER_SIZE failed %d", hdev->name, status);
288 hci_req_complete(hdev, status);
290 break;
292 default:
293 BT_DBG("%s Command complete: ogf HOST_CTL ocf %x", hdev->name, ocf);
294 break;
298 /* Command Complete OGF INFO_PARAM */
299 static void hci_cc_info_param(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
301 struct hci_rp_read_loc_version *lv;
302 struct hci_rp_read_local_features *lf;
303 struct hci_rp_read_buffer_size *bs;
304 struct hci_rp_read_bd_addr *ba;
306 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
308 switch (ocf) {
309 case OCF_READ_LOCAL_VERSION:
310 lv = (struct hci_rp_read_loc_version *) skb->data;
312 if (lv->status) {
313 BT_DBG("%s READ_LOCAL_VERSION failed %d", hdev->name, lf->status);
314 break;
317 hdev->hci_ver = lv->hci_ver;
318 hdev->hci_rev = btohs(lv->hci_rev);
319 hdev->manufacturer = btohs(lv->manufacturer);
321 BT_DBG("%s: manufacturer %d hci_ver %d hci_rev %d", hdev->name,
322 hdev->manufacturer, hdev->hci_ver, hdev->hci_rev);
324 break;
326 case OCF_READ_LOCAL_FEATURES:
327 lf = (struct hci_rp_read_local_features *) skb->data;
329 if (lf->status) {
330 BT_DBG("%s READ_LOCAL_FEATURES failed %d", hdev->name, lf->status);
331 break;
334 memcpy(hdev->features, lf->features, sizeof(hdev->features));
336 /* Adjust default settings according to features
337 * supported by device. */
338 if (hdev->features[0] & LMP_3SLOT)
339 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
341 if (hdev->features[0] & LMP_5SLOT)
342 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
344 if (hdev->features[1] & LMP_HV2)
345 hdev->pkt_type |= (HCI_HV2);
347 if (hdev->features[1] & LMP_HV3)
348 hdev->pkt_type |= (HCI_HV3);
350 BT_DBG("%s: features 0x%x 0x%x 0x%x", hdev->name,
351 lf->features[0], lf->features[1], lf->features[2]);
353 break;
355 case OCF_READ_BUFFER_SIZE:
356 bs = (struct hci_rp_read_buffer_size *) skb->data;
358 if (bs->status) {
359 BT_DBG("%s READ_BUFFER_SIZE failed %d", hdev->name, bs->status);
360 hci_req_complete(hdev, bs->status);
361 break;
364 hdev->acl_mtu = __le16_to_cpu(bs->acl_mtu);
365 hdev->sco_mtu = bs->sco_mtu;
366 hdev->acl_pkts = __le16_to_cpu(bs->acl_max_pkt);
367 hdev->sco_pkts = __le16_to_cpu(bs->sco_max_pkt);
369 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
370 hdev->sco_mtu = 64;
371 hdev->sco_pkts = 8;
374 hdev->acl_cnt = hdev->acl_pkts;
375 hdev->sco_cnt = hdev->sco_pkts;
377 BT_DBG("%s mtu: acl %d, sco %d max_pkt: acl %d, sco %d", hdev->name,
378 hdev->acl_mtu, hdev->sco_mtu, hdev->acl_pkts, hdev->sco_pkts);
379 break;
381 case OCF_READ_BD_ADDR:
382 ba = (struct hci_rp_read_bd_addr *) skb->data;
384 if (!ba->status) {
385 bacpy(&hdev->bdaddr, &ba->bdaddr);
386 } else {
387 BT_DBG("%s: READ_BD_ADDR failed %d", hdev->name, ba->status);
390 hci_req_complete(hdev, ba->status);
391 break;
393 default:
394 BT_DBG("%s Command complete: ogf INFO_PARAM ocf %x", hdev->name, ocf);
395 break;
399 /* Command Status OGF LINK_CTL */
400 static inline void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
402 struct hci_conn *conn;
403 struct hci_cp_create_conn *cp = hci_sent_cmd_data(hdev, OGF_LINK_CTL, OCF_CREATE_CONN);
405 if (!cp)
406 return;
408 hci_dev_lock(hdev);
410 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
412 BT_DBG("%s status 0x%x bdaddr %s conn %p", hdev->name,
413 status, batostr(&cp->bdaddr), conn);
415 if (status) {
416 if (conn && conn->state == BT_CONNECT) {
417 if (status != 0x0c || conn->attempt > 2) {
418 conn->state = BT_CLOSED;
419 hci_proto_connect_cfm(conn, status);
420 hci_conn_del(conn);
421 } else
422 conn->state = BT_CONNECT2;
424 } else {
425 if (!conn) {
426 conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr);
427 if (conn) {
428 conn->out = 1;
429 conn->link_mode |= HCI_LM_MASTER;
430 } else
431 BT_ERR("No memmory for new connection");
435 hci_dev_unlock(hdev);
438 static void hci_cs_link_ctl(struct hci_dev *hdev, __u16 ocf, __u8 status)
440 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
442 switch (ocf) {
443 case OCF_CREATE_CONN:
444 hci_cs_create_conn(hdev, status);
445 break;
447 case OCF_ADD_SCO:
448 if (status) {
449 struct hci_conn *acl, *sco;
450 struct hci_cp_add_sco *cp = hci_sent_cmd_data(hdev, OGF_LINK_CTL, OCF_ADD_SCO);
451 __u16 handle;
453 if (!cp)
454 break;
456 handle = __le16_to_cpu(cp->handle);
458 BT_DBG("%s Add SCO error: handle %d status 0x%x", hdev->name, handle, status);
460 hci_dev_lock(hdev);
462 acl = hci_conn_hash_lookup_handle(hdev, handle);
463 if (acl && (sco = acl->link)) {
464 sco->state = BT_CLOSED;
466 hci_proto_connect_cfm(sco, status);
467 hci_conn_del(sco);
470 hci_dev_unlock(hdev);
472 break;
474 case OCF_INQUIRY:
475 if (status) {
476 BT_DBG("%s Inquiry error: status 0x%x", hdev->name, status);
477 hci_req_complete(hdev, status);
478 } else {
479 set_bit(HCI_INQUIRY, &hdev->flags);
481 break;
483 default:
484 BT_DBG("%s Command status: ogf LINK_CTL ocf %x status %d",
485 hdev->name, ocf, status);
486 break;
490 /* Command Status OGF LINK_POLICY */
491 static void hci_cs_link_policy(struct hci_dev *hdev, __u16 ocf, __u8 status)
493 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
495 switch (ocf) {
496 case OCF_SNIFF_MODE:
497 if (status) {
498 struct hci_conn *conn;
499 struct hci_cp_sniff_mode *cp = hci_sent_cmd_data(hdev, OGF_LINK_POLICY, OCF_SNIFF_MODE);
501 if (!cp)
502 break;
504 hci_dev_lock(hdev);
506 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
507 if (conn) {
508 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend);
511 hci_dev_unlock(hdev);
513 break;
515 case OCF_EXIT_SNIFF_MODE:
516 if (status) {
517 struct hci_conn *conn;
518 struct hci_cp_exit_sniff_mode *cp = hci_sent_cmd_data(hdev, OGF_LINK_POLICY, OCF_EXIT_SNIFF_MODE);
520 if (!cp)
521 break;
523 hci_dev_lock(hdev);
525 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
526 if (conn) {
527 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend);
530 hci_dev_unlock(hdev);
532 break;
534 default:
535 BT_DBG("%s Command status: ogf LINK_POLICY ocf %x", hdev->name, ocf);
536 break;
540 /* Command Status OGF HOST_CTL */
541 static void hci_cs_host_ctl(struct hci_dev *hdev, __u16 ocf, __u8 status)
543 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
545 switch (ocf) {
546 default:
547 BT_DBG("%s Command status: ogf HOST_CTL ocf %x", hdev->name, ocf);
548 break;
552 /* Command Status OGF INFO_PARAM */
553 static void hci_cs_info_param(struct hci_dev *hdev, __u16 ocf, __u8 status)
555 BT_DBG("%s: hci_cs_info_param: ocf 0x%x", hdev->name, ocf);
557 switch (ocf) {
558 default:
559 BT_DBG("%s Command status: ogf INFO_PARAM ocf %x", hdev->name, ocf);
560 break;
564 /* Inquiry Complete */
565 static inline void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
567 __u8 status = *((__u8 *) skb->data);
569 BT_DBG("%s status %d", hdev->name, status);
571 clear_bit(HCI_INQUIRY, &hdev->flags);
572 hci_req_complete(hdev, status);
575 /* Inquiry Result */
576 static inline void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
578 struct inquiry_data data;
579 struct inquiry_info *info = (struct inquiry_info *) (skb->data + 1);
580 int num_rsp = *((__u8 *) skb->data);
582 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
584 if (!num_rsp)
585 return;
587 hci_dev_lock(hdev);
589 for (; num_rsp; num_rsp--) {
590 bacpy(&data.bdaddr, &info->bdaddr);
591 data.pscan_rep_mode = info->pscan_rep_mode;
592 data.pscan_period_mode = info->pscan_period_mode;
593 data.pscan_mode = info->pscan_mode;
594 memcpy(data.dev_class, info->dev_class, 3);
595 data.clock_offset = info->clock_offset;
596 data.rssi = 0x00;
597 info++;
598 hci_inquiry_cache_update(hdev, &data);
601 hci_dev_unlock(hdev);
604 /* Inquiry Result With RSSI */
605 static inline void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, struct sk_buff *skb)
607 struct inquiry_data data;
608 int num_rsp = *((__u8 *) skb->data);
610 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
612 if (!num_rsp)
613 return;
615 hci_dev_lock(hdev);
617 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
618 struct inquiry_info_with_rssi_and_pscan_mode *info =
619 (struct inquiry_info_with_rssi_and_pscan_mode *) (skb->data + 1);
621 for (; num_rsp; num_rsp--) {
622 bacpy(&data.bdaddr, &info->bdaddr);
623 data.pscan_rep_mode = info->pscan_rep_mode;
624 data.pscan_period_mode = info->pscan_period_mode;
625 data.pscan_mode = info->pscan_mode;
626 memcpy(data.dev_class, info->dev_class, 3);
627 data.clock_offset = info->clock_offset;
628 data.rssi = info->rssi;
629 info++;
630 hci_inquiry_cache_update(hdev, &data);
632 } else {
633 struct inquiry_info_with_rssi *info =
634 (struct inquiry_info_with_rssi *) (skb->data + 1);
636 for (; num_rsp; num_rsp--) {
637 bacpy(&data.bdaddr, &info->bdaddr);
638 data.pscan_rep_mode = info->pscan_rep_mode;
639 data.pscan_period_mode = info->pscan_period_mode;
640 data.pscan_mode = 0x00;
641 memcpy(data.dev_class, info->dev_class, 3);
642 data.clock_offset = info->clock_offset;
643 data.rssi = info->rssi;
644 info++;
645 hci_inquiry_cache_update(hdev, &data);
649 hci_dev_unlock(hdev);
652 /* Extended Inquiry Result */
653 static inline void hci_extended_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
655 struct inquiry_data data;
656 struct extended_inquiry_info *info = (struct extended_inquiry_info *) (skb->data + 1);
657 int num_rsp = *((__u8 *) skb->data);
659 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
661 if (!num_rsp)
662 return;
664 hci_dev_lock(hdev);
666 for (; num_rsp; num_rsp--) {
667 bacpy(&data.bdaddr, &info->bdaddr);
668 data.pscan_rep_mode = info->pscan_rep_mode;
669 data.pscan_period_mode = info->pscan_period_mode;
670 data.pscan_mode = 0x00;
671 memcpy(data.dev_class, info->dev_class, 3);
672 data.clock_offset = info->clock_offset;
673 data.rssi = info->rssi;
674 info++;
675 hci_inquiry_cache_update(hdev, &data);
678 hci_dev_unlock(hdev);
681 /* Connect Request */
682 static inline void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
684 struct hci_ev_conn_request *ev = (struct hci_ev_conn_request *) skb->data;
685 int mask = hdev->link_mode;
687 BT_DBG("%s Connection request: %s type 0x%x", hdev->name,
688 batostr(&ev->bdaddr), ev->link_type);
690 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type);
692 if (mask & HCI_LM_ACCEPT) {
693 /* Connection accepted */
694 struct hci_conn *conn;
695 struct hci_cp_accept_conn_req cp;
697 hci_dev_lock(hdev);
698 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
699 if (!conn) {
700 if (!(conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr))) {
701 BT_ERR("No memmory for new connection");
702 hci_dev_unlock(hdev);
703 return;
706 memcpy(conn->dev_class, ev->dev_class, 3);
707 conn->state = BT_CONNECT;
708 hci_dev_unlock(hdev);
710 bacpy(&cp.bdaddr, &ev->bdaddr);
712 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
713 cp.role = 0x00; /* Become master */
714 else
715 cp.role = 0x01; /* Remain slave */
717 hci_send_cmd(hdev, OGF_LINK_CTL,
718 OCF_ACCEPT_CONN_REQ, sizeof(cp), &cp);
719 } else {
720 /* Connection rejected */
721 struct hci_cp_reject_conn_req cp;
723 bacpy(&cp.bdaddr, &ev->bdaddr);
724 cp.reason = 0x0f;
725 hci_send_cmd(hdev, OGF_LINK_CTL,
726 OCF_REJECT_CONN_REQ, sizeof(cp), &cp);
730 /* Connect Complete */
731 static inline void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
733 struct hci_ev_conn_complete *ev = (struct hci_ev_conn_complete *) skb->data;
734 struct hci_conn *conn, *pend;
736 BT_DBG("%s", hdev->name);
738 hci_dev_lock(hdev);
740 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
741 if (!conn) {
742 hci_dev_unlock(hdev);
743 return;
746 if (!ev->status) {
747 conn->handle = __le16_to_cpu(ev->handle);
748 conn->state = BT_CONNECTED;
750 if (test_bit(HCI_AUTH, &hdev->flags))
751 conn->link_mode |= HCI_LM_AUTH;
753 if (test_bit(HCI_ENCRYPT, &hdev->flags))
754 conn->link_mode |= HCI_LM_ENCRYPT;
756 /* Get remote features */
757 if (conn->type == ACL_LINK) {
758 struct hci_cp_read_remote_features cp;
759 cp.handle = ev->handle;
760 hci_send_cmd(hdev, OGF_LINK_CTL,
761 OCF_READ_REMOTE_FEATURES, sizeof(cp), &cp);
764 /* Set link policy */
765 if (conn->type == ACL_LINK && hdev->link_policy) {
766 struct hci_cp_write_link_policy cp;
767 cp.handle = ev->handle;
768 cp.policy = __cpu_to_le16(hdev->link_policy);
769 hci_send_cmd(hdev, OGF_LINK_POLICY,
770 OCF_WRITE_LINK_POLICY, sizeof(cp), &cp);
773 /* Set packet type for incoming connection */
774 if (!conn->out) {
775 struct hci_cp_change_conn_ptype cp;
776 cp.handle = ev->handle;
777 cp.pkt_type = (conn->type == ACL_LINK) ?
778 __cpu_to_le16(hdev->pkt_type & ACL_PTYPE_MASK):
779 __cpu_to_le16(hdev->pkt_type & SCO_PTYPE_MASK);
781 hci_send_cmd(hdev, OGF_LINK_CTL,
782 OCF_CHANGE_CONN_PTYPE, sizeof(cp), &cp);
783 } else {
784 /* Update disconnect timer */
785 hci_conn_hold(conn);
786 hci_conn_put(conn);
788 } else
789 conn->state = BT_CLOSED;
791 if (conn->type == ACL_LINK) {
792 struct hci_conn *sco = conn->link;
793 if (sco) {
794 if (!ev->status)
795 hci_add_sco(sco, conn->handle);
796 else {
797 hci_proto_connect_cfm(sco, ev->status);
798 hci_conn_del(sco);
803 hci_proto_connect_cfm(conn, ev->status);
804 if (ev->status)
805 hci_conn_del(conn);
807 pend = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
808 if (pend)
809 hci_acl_connect(pend);
811 hci_dev_unlock(hdev);
814 /* Disconnect Complete */
815 static inline void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
817 struct hci_ev_disconn_complete *ev = (struct hci_ev_disconn_complete *) skb->data;
818 struct hci_conn *conn;
820 BT_DBG("%s status %d", hdev->name, ev->status);
822 if (ev->status)
823 return;
825 hci_dev_lock(hdev);
827 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
828 if (conn) {
829 conn->state = BT_CLOSED;
830 hci_proto_disconn_ind(conn, ev->reason);
831 hci_conn_del(conn);
834 hci_dev_unlock(hdev);
837 /* Number of completed packets */
838 static inline void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
840 struct hci_ev_num_comp_pkts *ev = (struct hci_ev_num_comp_pkts *) skb->data;
841 __le16 *ptr;
842 int i;
844 skb_pull(skb, sizeof(*ev));
846 BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
848 if (skb->len < ev->num_hndl * 4) {
849 BT_DBG("%s bad parameters", hdev->name);
850 return;
853 tasklet_disable(&hdev->tx_task);
855 for (i = 0, ptr = (__le16 *) skb->data; i < ev->num_hndl; i++) {
856 struct hci_conn *conn;
857 __u16 handle, count;
859 handle = __le16_to_cpu(get_unaligned(ptr++));
860 count = __le16_to_cpu(get_unaligned(ptr++));
862 conn = hci_conn_hash_lookup_handle(hdev, handle);
863 if (conn) {
864 conn->sent -= count;
866 if (conn->type == SCO_LINK) {
867 if ((hdev->sco_cnt += count) > hdev->sco_pkts)
868 hdev->sco_cnt = hdev->sco_pkts;
869 } else {
870 if ((hdev->acl_cnt += count) > hdev->acl_pkts)
871 hdev->acl_cnt = hdev->acl_pkts;
875 hci_sched_tx(hdev);
877 tasklet_enable(&hdev->tx_task);
880 /* Role Change */
881 static inline void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
883 struct hci_ev_role_change *ev = (struct hci_ev_role_change *) skb->data;
884 struct hci_conn *conn;
886 BT_DBG("%s status %d", hdev->name, ev->status);
888 hci_dev_lock(hdev);
890 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
891 if (conn) {
892 if (!ev->status) {
893 if (ev->role)
894 conn->link_mode &= ~HCI_LM_MASTER;
895 else
896 conn->link_mode |= HCI_LM_MASTER;
899 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->pend);
901 hci_role_switch_cfm(conn, ev->status, ev->role);
904 hci_dev_unlock(hdev);
907 /* Mode Change */
908 static inline void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
910 struct hci_ev_mode_change *ev = (struct hci_ev_mode_change *) skb->data;
911 struct hci_conn *conn;
913 BT_DBG("%s status %d", hdev->name, ev->status);
915 hci_dev_lock(hdev);
917 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
918 if (conn) {
919 conn->mode = ev->mode;
920 conn->interval = __le16_to_cpu(ev->interval);
922 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
923 if (conn->mode == HCI_CM_ACTIVE)
924 conn->power_save = 1;
925 else
926 conn->power_save = 0;
930 hci_dev_unlock(hdev);
933 /* Authentication Complete */
934 static inline void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
936 struct hci_ev_auth_complete *ev = (struct hci_ev_auth_complete *) skb->data;
937 struct hci_conn *conn;
939 BT_DBG("%s status %d", hdev->name, ev->status);
941 hci_dev_lock(hdev);
943 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
944 if (conn) {
945 if (!ev->status)
946 conn->link_mode |= HCI_LM_AUTH;
948 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
950 hci_auth_cfm(conn, ev->status);
952 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
953 if (!ev->status) {
954 struct hci_cp_set_conn_encrypt cp;
955 cp.handle = __cpu_to_le16(conn->handle);
956 cp.encrypt = 1;
957 hci_send_cmd(conn->hdev, OGF_LINK_CTL,
958 OCF_SET_CONN_ENCRYPT, sizeof(cp), &cp);
959 } else {
960 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
961 hci_encrypt_cfm(conn, ev->status, 0x00);
966 hci_dev_unlock(hdev);
969 /* Encryption Change */
970 static inline void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
972 struct hci_ev_encrypt_change *ev = (struct hci_ev_encrypt_change *) skb->data;
973 struct hci_conn *conn;
975 BT_DBG("%s status %d", hdev->name, ev->status);
977 hci_dev_lock(hdev);
979 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
980 if (conn) {
981 if (!ev->status) {
982 if (ev->encrypt)
983 conn->link_mode |= HCI_LM_ENCRYPT;
984 else
985 conn->link_mode &= ~HCI_LM_ENCRYPT;
988 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
990 hci_encrypt_cfm(conn, ev->status, ev->encrypt);
993 hci_dev_unlock(hdev);
996 /* Change Connection Link Key Complete */
997 static inline void hci_change_conn_link_key_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
999 struct hci_ev_change_conn_link_key_complete *ev = (struct hci_ev_change_conn_link_key_complete *) skb->data;
1000 struct hci_conn *conn;
1002 BT_DBG("%s status %d", hdev->name, ev->status);
1004 hci_dev_lock(hdev);
1006 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1007 if (conn) {
1008 if (!ev->status)
1009 conn->link_mode |= HCI_LM_SECURE;
1011 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
1013 hci_key_change_cfm(conn, ev->status);
1016 hci_dev_unlock(hdev);
1019 /* Pin Code Request*/
1020 static inline void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
1024 /* Link Key Request */
1025 static inline void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
1029 /* Link Key Notification */
1030 static inline void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
1034 /* Remote Features */
1035 static inline void hci_remote_features_evt(struct hci_dev *hdev, struct sk_buff *skb)
1037 struct hci_ev_remote_features *ev = (struct hci_ev_remote_features *) skb->data;
1038 struct hci_conn *conn;
1040 BT_DBG("%s status %d", hdev->name, ev->status);
1042 hci_dev_lock(hdev);
1044 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1045 if (conn && !ev->status) {
1046 memcpy(conn->features, ev->features, sizeof(conn->features));
1049 hci_dev_unlock(hdev);
1052 /* Clock Offset */
1053 static inline void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
1055 struct hci_ev_clock_offset *ev = (struct hci_ev_clock_offset *) skb->data;
1056 struct hci_conn *conn;
1058 BT_DBG("%s status %d", hdev->name, ev->status);
1060 hci_dev_lock(hdev);
1062 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1063 if (conn && !ev->status) {
1064 struct inquiry_entry *ie;
1066 if ((ie = hci_inquiry_cache_lookup(hdev, &conn->dst))) {
1067 ie->data.clock_offset = ev->clock_offset;
1068 ie->timestamp = jiffies;
1072 hci_dev_unlock(hdev);
1075 /* Page Scan Repetition Mode */
1076 static inline void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
1078 struct hci_ev_pscan_rep_mode *ev = (struct hci_ev_pscan_rep_mode *) skb->data;
1079 struct inquiry_entry *ie;
1081 BT_DBG("%s", hdev->name);
1083 hci_dev_lock(hdev);
1085 if ((ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr))) {
1086 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
1087 ie->timestamp = jiffies;
1090 hci_dev_unlock(hdev);
1093 /* Sniff Subrate */
1094 static inline void hci_sniff_subrate_evt(struct hci_dev *hdev, struct sk_buff *skb)
1096 struct hci_ev_sniff_subrate *ev = (struct hci_ev_sniff_subrate *) skb->data;
1097 struct hci_conn *conn;
1099 BT_DBG("%s status %d", hdev->name, ev->status);
1101 hci_dev_lock(hdev);
1103 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1104 if (conn) {
1107 hci_dev_unlock(hdev);
1110 void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
1112 struct hci_event_hdr *hdr = (struct hci_event_hdr *) skb->data;
1113 struct hci_ev_cmd_complete *ec;
1114 struct hci_ev_cmd_status *cs;
1115 u16 opcode, ocf, ogf;
1117 skb_pull(skb, HCI_EVENT_HDR_SIZE);
1119 BT_DBG("%s evt 0x%x", hdev->name, hdr->evt);
1121 switch (hdr->evt) {
1122 case HCI_EV_NUM_COMP_PKTS:
1123 hci_num_comp_pkts_evt(hdev, skb);
1124 break;
1126 case HCI_EV_INQUIRY_COMPLETE:
1127 hci_inquiry_complete_evt(hdev, skb);
1128 break;
1130 case HCI_EV_INQUIRY_RESULT:
1131 hci_inquiry_result_evt(hdev, skb);
1132 break;
1134 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
1135 hci_inquiry_result_with_rssi_evt(hdev, skb);
1136 break;
1138 case HCI_EV_EXTENDED_INQUIRY_RESULT:
1139 hci_extended_inquiry_result_evt(hdev, skb);
1140 break;
1142 case HCI_EV_CONN_REQUEST:
1143 hci_conn_request_evt(hdev, skb);
1144 break;
1146 case HCI_EV_CONN_COMPLETE:
1147 hci_conn_complete_evt(hdev, skb);
1148 break;
1150 case HCI_EV_DISCONN_COMPLETE:
1151 hci_disconn_complete_evt(hdev, skb);
1152 break;
1154 case HCI_EV_ROLE_CHANGE:
1155 hci_role_change_evt(hdev, skb);
1156 break;
1158 case HCI_EV_MODE_CHANGE:
1159 hci_mode_change_evt(hdev, skb);
1160 break;
1162 case HCI_EV_AUTH_COMPLETE:
1163 hci_auth_complete_evt(hdev, skb);
1164 break;
1166 case HCI_EV_ENCRYPT_CHANGE:
1167 hci_encrypt_change_evt(hdev, skb);
1168 break;
1170 case HCI_EV_CHANGE_CONN_LINK_KEY_COMPLETE:
1171 hci_change_conn_link_key_complete_evt(hdev, skb);
1172 break;
1174 case HCI_EV_PIN_CODE_REQ:
1175 hci_pin_code_request_evt(hdev, skb);
1176 break;
1178 case HCI_EV_LINK_KEY_REQ:
1179 hci_link_key_request_evt(hdev, skb);
1180 break;
1182 case HCI_EV_LINK_KEY_NOTIFY:
1183 hci_link_key_notify_evt(hdev, skb);
1184 break;
1186 case HCI_EV_REMOTE_FEATURES:
1187 hci_remote_features_evt(hdev, skb);
1188 break;
1190 case HCI_EV_CLOCK_OFFSET:
1191 hci_clock_offset_evt(hdev, skb);
1192 break;
1194 case HCI_EV_PSCAN_REP_MODE:
1195 hci_pscan_rep_mode_evt(hdev, skb);
1196 break;
1198 case HCI_EV_SNIFF_SUBRATE:
1199 hci_sniff_subrate_evt(hdev, skb);
1200 break;
1202 case HCI_EV_CMD_STATUS:
1203 cs = (struct hci_ev_cmd_status *) skb->data;
1204 skb_pull(skb, sizeof(cs));
1206 opcode = __le16_to_cpu(cs->opcode);
1207 ogf = hci_opcode_ogf(opcode);
1208 ocf = hci_opcode_ocf(opcode);
1210 switch (ogf) {
1211 case OGF_INFO_PARAM:
1212 hci_cs_info_param(hdev, ocf, cs->status);
1213 break;
1215 case OGF_HOST_CTL:
1216 hci_cs_host_ctl(hdev, ocf, cs->status);
1217 break;
1219 case OGF_LINK_CTL:
1220 hci_cs_link_ctl(hdev, ocf, cs->status);
1221 break;
1223 case OGF_LINK_POLICY:
1224 hci_cs_link_policy(hdev, ocf, cs->status);
1225 break;
1227 default:
1228 BT_DBG("%s Command Status OGF %x", hdev->name, ogf);
1229 break;
1232 if (cs->ncmd) {
1233 atomic_set(&hdev->cmd_cnt, 1);
1234 if (!skb_queue_empty(&hdev->cmd_q))
1235 hci_sched_cmd(hdev);
1237 break;
1239 case HCI_EV_CMD_COMPLETE:
1240 ec = (struct hci_ev_cmd_complete *) skb->data;
1241 skb_pull(skb, sizeof(*ec));
1243 opcode = __le16_to_cpu(ec->opcode);
1244 ogf = hci_opcode_ogf(opcode);
1245 ocf = hci_opcode_ocf(opcode);
1247 switch (ogf) {
1248 case OGF_INFO_PARAM:
1249 hci_cc_info_param(hdev, ocf, skb);
1250 break;
1252 case OGF_HOST_CTL:
1253 hci_cc_host_ctl(hdev, ocf, skb);
1254 break;
1256 case OGF_LINK_CTL:
1257 hci_cc_link_ctl(hdev, ocf, skb);
1258 break;
1260 case OGF_LINK_POLICY:
1261 hci_cc_link_policy(hdev, ocf, skb);
1262 break;
1264 default:
1265 BT_DBG("%s Command Completed OGF %x", hdev->name, ogf);
1266 break;
1269 if (ec->ncmd) {
1270 atomic_set(&hdev->cmd_cnt, 1);
1271 if (!skb_queue_empty(&hdev->cmd_q))
1272 hci_sched_cmd(hdev);
1274 break;
1277 kfree_skb(skb);
1278 hdev->stat.evt_rx++;
1281 /* Generate internal stack event */
1282 void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
1284 struct hci_event_hdr *hdr;
1285 struct hci_ev_stack_internal *ev;
1286 struct sk_buff *skb;
1288 skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC);
1289 if (!skb)
1290 return;
1292 hdr = (void *) skb_put(skb, HCI_EVENT_HDR_SIZE);
1293 hdr->evt = HCI_EV_STACK_INTERNAL;
1294 hdr->plen = sizeof(*ev) + dlen;
1296 ev = (void *) skb_put(skb, sizeof(*ev) + dlen);
1297 ev->type = type;
1298 memcpy(ev->data, data, dlen);
1300 bt_cb(skb)->incoming = 1;
1301 __net_timestamp(skb);
1303 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
1304 skb->dev = (void *) hdev;
1305 hci_send_to_sock(hdev, skb);
1306 kfree_skb(skb);