[Bluetooth]: Track page scan repetition mode changes
[linux-2.6/linux-loongson.git] / net / bluetooth / hci_event.c
bloba004284c4d98f50c22937aa59a677740c4a94674
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/config.h>
28 #include <linux/module.h>
30 #include <linux/types.h>
31 #include <linux/errno.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/slab.h>
35 #include <linux/poll.h>
36 #include <linux/fcntl.h>
37 #include <linux/init.h>
38 #include <linux/skbuff.h>
39 #include <linux/interrupt.h>
40 #include <linux/notifier.h>
41 #include <net/sock.h>
43 #include <asm/system.h>
44 #include <asm/uaccess.h>
45 #include <asm/unaligned.h>
47 #include <net/bluetooth/bluetooth.h>
48 #include <net/bluetooth/hci_core.h>
50 #ifndef CONFIG_BT_HCI_CORE_DEBUG
51 #undef BT_DBG
52 #define BT_DBG(D...)
53 #endif
55 /* Handle HCI Event packets */
57 /* Command Complete OGF LINK_CTL */
58 static void hci_cc_link_ctl(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
60 __u8 status;
62 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
64 switch (ocf) {
65 case OCF_INQUIRY_CANCEL:
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;
88 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
90 switch (ocf) {
91 case OCF_ROLE_DISCOVERY:
92 rd = (void *) skb->data;
94 if (rd->status)
95 break;
97 hci_dev_lock(hdev);
99 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rd->handle));
100 if (conn) {
101 if (rd->role)
102 conn->link_mode &= ~HCI_LM_MASTER;
103 else
104 conn->link_mode |= HCI_LM_MASTER;
107 hci_dev_unlock(hdev);
108 break;
110 default:
111 BT_DBG("%s: Command complete: ogf LINK_POLICY ocf %x",
112 hdev->name, ocf);
113 break;
117 /* Command Complete OGF HOST_CTL */
118 static void hci_cc_host_ctl(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
120 __u8 status, param;
121 __u16 setting;
122 struct hci_rp_read_voice_setting *vs;
123 void *sent;
125 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
127 switch (ocf) {
128 case OCF_RESET:
129 status = *((__u8 *) skb->data);
130 hci_req_complete(hdev, status);
131 break;
133 case OCF_SET_EVENT_FLT:
134 status = *((__u8 *) skb->data);
135 if (status) {
136 BT_DBG("%s SET_EVENT_FLT failed %d", hdev->name, status);
137 } else {
138 BT_DBG("%s SET_EVENT_FLT succeseful", hdev->name);
140 break;
142 case OCF_WRITE_AUTH_ENABLE:
143 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_AUTH_ENABLE);
144 if (!sent)
145 break;
147 status = *((__u8 *) skb->data);
148 param = *((__u8 *) sent);
150 if (!status) {
151 if (param == AUTH_ENABLED)
152 set_bit(HCI_AUTH, &hdev->flags);
153 else
154 clear_bit(HCI_AUTH, &hdev->flags);
156 hci_req_complete(hdev, status);
157 break;
159 case OCF_WRITE_ENCRYPT_MODE:
160 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_ENCRYPT_MODE);
161 if (!sent)
162 break;
164 status = *((__u8 *) skb->data);
165 param = *((__u8 *) sent);
167 if (!status) {
168 if (param)
169 set_bit(HCI_ENCRYPT, &hdev->flags);
170 else
171 clear_bit(HCI_ENCRYPT, &hdev->flags);
173 hci_req_complete(hdev, status);
174 break;
176 case OCF_WRITE_CA_TIMEOUT:
177 status = *((__u8 *) skb->data);
178 if (status) {
179 BT_DBG("%s OCF_WRITE_CA_TIMEOUT failed %d", hdev->name, status);
180 } else {
181 BT_DBG("%s OCF_WRITE_CA_TIMEOUT succeseful", hdev->name);
183 break;
185 case OCF_WRITE_PG_TIMEOUT:
186 status = *((__u8 *) skb->data);
187 if (status) {
188 BT_DBG("%s OCF_WRITE_PG_TIMEOUT failed %d", hdev->name, status);
189 } else {
190 BT_DBG("%s: OCF_WRITE_PG_TIMEOUT succeseful", hdev->name);
192 break;
194 case OCF_WRITE_SCAN_ENABLE:
195 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_SCAN_ENABLE);
196 if (!sent)
197 break;
199 status = *((__u8 *) skb->data);
200 param = *((__u8 *) sent);
202 BT_DBG("param 0x%x", param);
204 if (!status) {
205 clear_bit(HCI_PSCAN, &hdev->flags);
206 clear_bit(HCI_ISCAN, &hdev->flags);
207 if (param & SCAN_INQUIRY)
208 set_bit(HCI_ISCAN, &hdev->flags);
210 if (param & SCAN_PAGE)
211 set_bit(HCI_PSCAN, &hdev->flags);
213 hci_req_complete(hdev, status);
214 break;
216 case OCF_READ_VOICE_SETTING:
217 vs = (struct hci_rp_read_voice_setting *) skb->data;
219 if (vs->status) {
220 BT_DBG("%s READ_VOICE_SETTING failed %d", hdev->name, vs->status);
221 break;
224 setting = __le16_to_cpu(vs->voice_setting);
226 if (hdev->voice_setting != setting ) {
227 hdev->voice_setting = setting;
229 BT_DBG("%s: voice setting 0x%04x", hdev->name, setting);
231 if (hdev->notify) {
232 tasklet_disable(&hdev->tx_task);
233 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
234 tasklet_enable(&hdev->tx_task);
237 break;
239 case OCF_WRITE_VOICE_SETTING:
240 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_VOICE_SETTING);
241 if (!sent)
242 break;
244 status = *((__u8 *) skb->data);
245 setting = __le16_to_cpu(get_unaligned((__u16 *) sent));
247 if (!status && hdev->voice_setting != setting) {
248 hdev->voice_setting = setting;
250 BT_DBG("%s: voice setting 0x%04x", hdev->name, setting);
252 if (hdev->notify) {
253 tasklet_disable(&hdev->tx_task);
254 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
255 tasklet_enable(&hdev->tx_task);
258 hci_req_complete(hdev, status);
259 break;
261 case OCF_HOST_BUFFER_SIZE:
262 status = *((__u8 *) skb->data);
263 if (status) {
264 BT_DBG("%s OCF_BUFFER_SIZE failed %d", hdev->name, status);
265 hci_req_complete(hdev, status);
267 break;
269 default:
270 BT_DBG("%s Command complete: ogf HOST_CTL ocf %x", hdev->name, ocf);
271 break;
275 /* Command Complete OGF INFO_PARAM */
276 static void hci_cc_info_param(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
278 struct hci_rp_read_loc_features *lf;
279 struct hci_rp_read_buffer_size *bs;
280 struct hci_rp_read_bd_addr *ba;
282 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
284 switch (ocf) {
285 case OCF_READ_LOCAL_FEATURES:
286 lf = (struct hci_rp_read_loc_features *) skb->data;
288 if (lf->status) {
289 BT_DBG("%s READ_LOCAL_FEATURES failed %d", hdev->name, lf->status);
290 break;
293 memcpy(hdev->features, lf->features, sizeof(hdev->features));
295 /* Adjust default settings according to features
296 * supported by device. */
297 if (hdev->features[0] & LMP_3SLOT)
298 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
300 if (hdev->features[0] & LMP_5SLOT)
301 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
303 if (hdev->features[1] & LMP_HV2)
304 hdev->pkt_type |= (HCI_HV2);
306 if (hdev->features[1] & LMP_HV3)
307 hdev->pkt_type |= (HCI_HV3);
309 BT_DBG("%s: features 0x%x 0x%x 0x%x", hdev->name, lf->features[0], lf->features[1], lf->features[2]);
311 break;
313 case OCF_READ_BUFFER_SIZE:
314 bs = (struct hci_rp_read_buffer_size *) skb->data;
316 if (bs->status) {
317 BT_DBG("%s READ_BUFFER_SIZE failed %d", hdev->name, bs->status);
318 hci_req_complete(hdev, bs->status);
319 break;
322 hdev->acl_mtu = __le16_to_cpu(bs->acl_mtu);
323 hdev->sco_mtu = bs->sco_mtu ? bs->sco_mtu : 64;
324 hdev->acl_pkts = hdev->acl_cnt = __le16_to_cpu(bs->acl_max_pkt);
325 hdev->sco_pkts = hdev->sco_cnt = __le16_to_cpu(bs->sco_max_pkt);
327 BT_DBG("%s mtu: acl %d, sco %d max_pkt: acl %d, sco %d", hdev->name,
328 hdev->acl_mtu, hdev->sco_mtu, hdev->acl_pkts, hdev->sco_pkts);
329 break;
331 case OCF_READ_BD_ADDR:
332 ba = (struct hci_rp_read_bd_addr *) skb->data;
334 if (!ba->status) {
335 bacpy(&hdev->bdaddr, &ba->bdaddr);
336 } else {
337 BT_DBG("%s: READ_BD_ADDR failed %d", hdev->name, ba->status);
340 hci_req_complete(hdev, ba->status);
341 break;
343 default:
344 BT_DBG("%s Command complete: ogf INFO_PARAM ocf %x", hdev->name, ocf);
345 break;
349 /* Command Status OGF LINK_CTL */
350 static inline void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
352 struct hci_conn *conn;
353 struct hci_cp_create_conn *cp = hci_sent_cmd_data(hdev, OGF_LINK_CTL, OCF_CREATE_CONN);
355 if (!cp)
356 return;
358 hci_dev_lock(hdev);
360 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
362 BT_DBG("%s status 0x%x bdaddr %s conn %p", hdev->name,
363 status, batostr(&cp->bdaddr), conn);
365 if (status) {
366 if (conn && conn->state == BT_CONNECT) {
367 conn->state = BT_CLOSED;
368 hci_proto_connect_cfm(conn, status);
369 hci_conn_del(conn);
371 } else {
372 if (!conn) {
373 conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr);
374 if (conn) {
375 conn->out = 1;
376 conn->link_mode |= HCI_LM_MASTER;
377 } else
378 BT_ERR("No memmory for new connection");
382 hci_dev_unlock(hdev);
385 static void hci_cs_link_ctl(struct hci_dev *hdev, __u16 ocf, __u8 status)
387 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
389 switch (ocf) {
390 case OCF_CREATE_CONN:
391 hci_cs_create_conn(hdev, status);
392 break;
394 case OCF_ADD_SCO:
395 if (status) {
396 struct hci_conn *acl, *sco;
397 struct hci_cp_add_sco *cp = hci_sent_cmd_data(hdev, OGF_LINK_CTL, OCF_ADD_SCO);
398 __u16 handle;
400 if (!cp)
401 break;
403 handle = __le16_to_cpu(cp->handle);
405 BT_DBG("%s Add SCO error: handle %d status 0x%x", hdev->name, handle, status);
407 hci_dev_lock(hdev);
409 acl = hci_conn_hash_lookup_handle(hdev, handle);
410 if (acl && (sco = acl->link)) {
411 sco->state = BT_CLOSED;
413 hci_proto_connect_cfm(sco, status);
414 hci_conn_del(sco);
417 hci_dev_unlock(hdev);
419 break;
421 case OCF_INQUIRY:
422 if (status) {
423 BT_DBG("%s Inquiry error: status 0x%x", hdev->name, status);
424 hci_req_complete(hdev, status);
425 } else {
426 set_bit(HCI_INQUIRY, &hdev->flags);
428 break;
430 default:
431 BT_DBG("%s Command status: ogf LINK_CTL ocf %x status %d",
432 hdev->name, ocf, status);
433 break;
437 /* Command Status OGF LINK_POLICY */
438 static void hci_cs_link_policy(struct hci_dev *hdev, __u16 ocf, __u8 status)
440 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
442 switch (ocf) {
443 default:
444 BT_DBG("%s Command status: ogf HOST_POLICY ocf %x", hdev->name, ocf);
445 break;
449 /* Command Status OGF HOST_CTL */
450 static void hci_cs_host_ctl(struct hci_dev *hdev, __u16 ocf, __u8 status)
452 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
454 switch (ocf) {
455 default:
456 BT_DBG("%s Command status: ogf HOST_CTL ocf %x", hdev->name, ocf);
457 break;
461 /* Command Status OGF INFO_PARAM */
462 static void hci_cs_info_param(struct hci_dev *hdev, __u16 ocf, __u8 status)
464 BT_DBG("%s: hci_cs_info_param: ocf 0x%x", hdev->name, ocf);
466 switch (ocf) {
467 default:
468 BT_DBG("%s Command status: ogf INFO_PARAM ocf %x", hdev->name, ocf);
469 break;
473 /* Inquiry Complete */
474 static inline void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
476 __u8 status = *((__u8 *) skb->data);
478 BT_DBG("%s status %d", hdev->name, status);
480 clear_bit(HCI_INQUIRY, &hdev->flags);
481 hci_req_complete(hdev, status);
484 /* Inquiry Result */
485 static inline void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
487 struct inquiry_data data;
488 struct inquiry_info *info = (struct inquiry_info *) (skb->data + 1);
489 int num_rsp = *((__u8 *) skb->data);
491 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
493 if (!num_rsp)
494 return;
496 hci_dev_lock(hdev);
498 for (; num_rsp; num_rsp--) {
499 bacpy(&data.bdaddr, &info->bdaddr);
500 data.pscan_rep_mode = info->pscan_rep_mode;
501 data.pscan_period_mode = info->pscan_period_mode;
502 data.pscan_mode = info->pscan_mode;
503 memcpy(data.dev_class, info->dev_class, 3);
504 data.clock_offset = info->clock_offset;
505 data.rssi = 0x00;
506 info++;
507 hci_inquiry_cache_update(hdev, &data);
510 hci_dev_unlock(hdev);
513 /* Inquiry Result With RSSI */
514 static inline void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, struct sk_buff *skb)
516 struct inquiry_data data;
517 int num_rsp = *((__u8 *) skb->data);
519 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
521 if (!num_rsp)
522 return;
524 hci_dev_lock(hdev);
526 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
527 struct inquiry_info_with_rssi_and_pscan_mode *info =
528 (struct inquiry_info_with_rssi_and_pscan_mode *) (skb->data + 1);
530 for (; num_rsp; num_rsp--) {
531 bacpy(&data.bdaddr, &info->bdaddr);
532 data.pscan_rep_mode = info->pscan_rep_mode;
533 data.pscan_period_mode = info->pscan_period_mode;
534 data.pscan_mode = info->pscan_mode;
535 memcpy(data.dev_class, info->dev_class, 3);
536 data.clock_offset = info->clock_offset;
537 data.rssi = info->rssi;
538 info++;
539 hci_inquiry_cache_update(hdev, &data);
541 } else {
542 struct inquiry_info_with_rssi *info =
543 (struct inquiry_info_with_rssi *) (skb->data + 1);
545 for (; num_rsp; num_rsp--) {
546 bacpy(&data.bdaddr, &info->bdaddr);
547 data.pscan_rep_mode = info->pscan_rep_mode;
548 data.pscan_period_mode = info->pscan_period_mode;
549 data.pscan_mode = 0x00;
550 memcpy(data.dev_class, info->dev_class, 3);
551 data.clock_offset = info->clock_offset;
552 data.rssi = info->rssi;
553 info++;
554 hci_inquiry_cache_update(hdev, &data);
558 hci_dev_unlock(hdev);
561 /* Connect Request */
562 static inline void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
564 struct hci_ev_conn_request *ev = (struct hci_ev_conn_request *) skb->data;
565 int mask = hdev->link_mode;
567 BT_DBG("%s Connection request: %s type 0x%x", hdev->name,
568 batostr(&ev->bdaddr), ev->link_type);
570 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type);
572 if (mask & HCI_LM_ACCEPT) {
573 /* Connection accepted */
574 struct hci_conn *conn;
575 struct hci_cp_accept_conn_req cp;
577 hci_dev_lock(hdev);
578 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
579 if (!conn) {
580 if (!(conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr))) {
581 BT_ERR("No memmory for new connection");
582 hci_dev_unlock(hdev);
583 return;
586 memcpy(conn->dev_class, ev->dev_class, 3);
587 conn->state = BT_CONNECT;
588 hci_dev_unlock(hdev);
590 bacpy(&cp.bdaddr, &ev->bdaddr);
592 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
593 cp.role = 0x00; /* Become master */
594 else
595 cp.role = 0x01; /* Remain slave */
597 hci_send_cmd(hdev, OGF_LINK_CTL, OCF_ACCEPT_CONN_REQ, sizeof(cp), &cp);
598 } else {
599 /* Connection rejected */
600 struct hci_cp_reject_conn_req cp;
602 bacpy(&cp.bdaddr, &ev->bdaddr);
603 cp.reason = 0x0f;
604 hci_send_cmd(hdev, OGF_LINK_CTL, OCF_REJECT_CONN_REQ, sizeof(cp), &cp);
608 /* Connect Complete */
609 static inline void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
611 struct hci_ev_conn_complete *ev = (struct hci_ev_conn_complete *) skb->data;
612 struct hci_conn *conn = NULL;
614 BT_DBG("%s", hdev->name);
616 hci_dev_lock(hdev);
618 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
619 if (!conn) {
620 hci_dev_unlock(hdev);
621 return;
624 if (!ev->status) {
625 conn->handle = __le16_to_cpu(ev->handle);
626 conn->state = BT_CONNECTED;
628 if (test_bit(HCI_AUTH, &hdev->flags))
629 conn->link_mode |= HCI_LM_AUTH;
631 if (test_bit(HCI_ENCRYPT, &hdev->flags))
632 conn->link_mode |= HCI_LM_ENCRYPT;
634 /* Set link policy */
635 if (conn->type == ACL_LINK && hdev->link_policy) {
636 struct hci_cp_write_link_policy cp;
637 cp.handle = ev->handle;
638 cp.policy = __cpu_to_le16(hdev->link_policy);
639 hci_send_cmd(hdev, OGF_LINK_POLICY, OCF_WRITE_LINK_POLICY, sizeof(cp), &cp);
642 /* Set packet type for incoming connection */
643 if (!conn->out) {
644 struct hci_cp_change_conn_ptype cp;
645 cp.handle = ev->handle;
646 cp.pkt_type = (conn->type == ACL_LINK) ?
647 __cpu_to_le16(hdev->pkt_type & ACL_PTYPE_MASK):
648 __cpu_to_le16(hdev->pkt_type & SCO_PTYPE_MASK);
650 hci_send_cmd(hdev, OGF_LINK_CTL, OCF_CHANGE_CONN_PTYPE, sizeof(cp), &cp);
652 } else
653 conn->state = BT_CLOSED;
655 if (conn->type == ACL_LINK) {
656 struct hci_conn *sco = conn->link;
657 if (sco) {
658 if (!ev->status)
659 hci_add_sco(sco, conn->handle);
660 else {
661 hci_proto_connect_cfm(sco, ev->status);
662 hci_conn_del(sco);
667 hci_proto_connect_cfm(conn, ev->status);
668 if (ev->status)
669 hci_conn_del(conn);
671 hci_dev_unlock(hdev);
674 /* Disconnect Complete */
675 static inline void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
677 struct hci_ev_disconn_complete *ev = (struct hci_ev_disconn_complete *) skb->data;
678 struct hci_conn *conn = NULL;
679 __u16 handle = __le16_to_cpu(ev->handle);
681 BT_DBG("%s status %d", hdev->name, ev->status);
683 if (ev->status)
684 return;
686 hci_dev_lock(hdev);
688 conn = hci_conn_hash_lookup_handle(hdev, handle);
689 if (conn) {
690 conn->state = BT_CLOSED;
691 hci_proto_disconn_ind(conn, ev->reason);
692 hci_conn_del(conn);
695 hci_dev_unlock(hdev);
698 /* Number of completed packets */
699 static inline void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
701 struct hci_ev_num_comp_pkts *ev = (struct hci_ev_num_comp_pkts *) skb->data;
702 __u16 *ptr;
703 int i;
705 skb_pull(skb, sizeof(*ev));
707 BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
709 if (skb->len < ev->num_hndl * 4) {
710 BT_DBG("%s bad parameters", hdev->name);
711 return;
714 tasklet_disable(&hdev->tx_task);
716 for (i = 0, ptr = (__u16 *) skb->data; i < ev->num_hndl; i++) {
717 struct hci_conn *conn;
718 __u16 handle, count;
720 handle = __le16_to_cpu(get_unaligned(ptr++));
721 count = __le16_to_cpu(get_unaligned(ptr++));
723 conn = hci_conn_hash_lookup_handle(hdev, handle);
724 if (conn) {
725 conn->sent -= count;
727 if (conn->type == SCO_LINK) {
728 if ((hdev->sco_cnt += count) > hdev->sco_pkts)
729 hdev->sco_cnt = hdev->sco_pkts;
730 } else {
731 if ((hdev->acl_cnt += count) > hdev->acl_pkts)
732 hdev->acl_cnt = hdev->acl_pkts;
736 hci_sched_tx(hdev);
738 tasklet_enable(&hdev->tx_task);
741 /* Role Change */
742 static inline void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
744 struct hci_ev_role_change *ev = (struct hci_ev_role_change *) skb->data;
745 struct hci_conn *conn = NULL;
747 BT_DBG("%s status %d", hdev->name, ev->status);
749 hci_dev_lock(hdev);
751 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
752 if (conn) {
753 if (!ev->status) {
754 if (ev->role)
755 conn->link_mode &= ~HCI_LM_MASTER;
756 else
757 conn->link_mode |= HCI_LM_MASTER;
760 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->pend);
762 hci_role_switch_cfm(conn, ev->status, ev->role);
765 hci_dev_unlock(hdev);
768 /* Authentication Complete */
769 static inline void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
771 struct hci_ev_auth_complete *ev = (struct hci_ev_auth_complete *) skb->data;
772 struct hci_conn *conn = NULL;
773 __u16 handle = __le16_to_cpu(ev->handle);
775 BT_DBG("%s status %d", hdev->name, ev->status);
777 hci_dev_lock(hdev);
779 conn = hci_conn_hash_lookup_handle(hdev, handle);
780 if (conn) {
781 if (!ev->status)
782 conn->link_mode |= HCI_LM_AUTH;
784 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
786 hci_auth_cfm(conn, ev->status);
788 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
789 if (!ev->status) {
790 struct hci_cp_set_conn_encrypt cp;
791 cp.handle = __cpu_to_le16(conn->handle);
792 cp.encrypt = 1;
793 hci_send_cmd(conn->hdev, OGF_LINK_CTL,
794 OCF_SET_CONN_ENCRYPT,
795 sizeof(cp), &cp);
796 } else {
797 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
798 hci_encrypt_cfm(conn, ev->status, 0x00);
803 hci_dev_unlock(hdev);
806 /* Encryption Change */
807 static inline void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
809 struct hci_ev_encrypt_change *ev = (struct hci_ev_encrypt_change *) skb->data;
810 struct hci_conn *conn = NULL;
811 __u16 handle = __le16_to_cpu(ev->handle);
813 BT_DBG("%s status %d", hdev->name, ev->status);
815 hci_dev_lock(hdev);
817 conn = hci_conn_hash_lookup_handle(hdev, handle);
818 if (conn) {
819 if (!ev->status) {
820 if (ev->encrypt)
821 conn->link_mode |= HCI_LM_ENCRYPT;
822 else
823 conn->link_mode &= ~HCI_LM_ENCRYPT;
826 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
828 hci_encrypt_cfm(conn, ev->status, ev->encrypt);
831 hci_dev_unlock(hdev);
834 /* Change Connection Link Key Complete */
835 static inline void hci_change_conn_link_key_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
837 struct hci_ev_change_conn_link_key_complete *ev = (struct hci_ev_change_conn_link_key_complete *) 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);
843 hci_dev_lock(hdev);
845 conn = hci_conn_hash_lookup_handle(hdev, handle);
846 if (conn) {
847 if (!ev->status)
848 conn->link_mode |= HCI_LM_SECURE;
850 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
852 hci_key_change_cfm(conn, ev->status);
855 hci_dev_unlock(hdev);
858 /* Pin Code Request*/
859 static inline void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
863 /* Link Key Request */
864 static inline void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
868 /* Link Key Notification */
869 static inline void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
873 /* Clock Offset */
874 static inline void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
876 struct hci_ev_clock_offset *ev = (struct hci_ev_clock_offset *) skb->data;
877 struct hci_conn *conn = NULL;
878 __u16 handle = __le16_to_cpu(ev->handle);
880 BT_DBG("%s status %d", hdev->name, ev->status);
882 hci_dev_lock(hdev);
884 conn = hci_conn_hash_lookup_handle(hdev, handle);
885 if (conn && !ev->status) {
886 struct inquiry_entry *ie;
888 if ((ie = hci_inquiry_cache_lookup(hdev, &conn->dst))) {
889 ie->data.clock_offset = ev->clock_offset;
890 ie->timestamp = jiffies;
894 hci_dev_unlock(hdev);
897 /* Page Scan Repetition Mode */
898 static inline void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
900 struct hci_ev_pscan_rep_mode *ev = (struct hci_ev_pscan_rep_mode *) skb->data;
901 struct inquiry_entry *ie;
903 BT_DBG("%s", hdev->name);
905 hci_dev_lock(hdev);
907 if ((ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr))) {
908 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
909 ie->timestamp = jiffies;
912 hci_dev_unlock(hdev);
915 void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
917 struct hci_event_hdr *hdr = (struct hci_event_hdr *) skb->data;
918 struct hci_ev_cmd_complete *ec;
919 struct hci_ev_cmd_status *cs;
920 u16 opcode, ocf, ogf;
922 skb_pull(skb, HCI_EVENT_HDR_SIZE);
924 BT_DBG("%s evt 0x%x", hdev->name, hdr->evt);
926 switch (hdr->evt) {
927 case HCI_EV_NUM_COMP_PKTS:
928 hci_num_comp_pkts_evt(hdev, skb);
929 break;
931 case HCI_EV_INQUIRY_COMPLETE:
932 hci_inquiry_complete_evt(hdev, skb);
933 break;
935 case HCI_EV_INQUIRY_RESULT:
936 hci_inquiry_result_evt(hdev, skb);
937 break;
939 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
940 hci_inquiry_result_with_rssi_evt(hdev, skb);
941 break;
943 case HCI_EV_CONN_REQUEST:
944 hci_conn_request_evt(hdev, skb);
945 break;
947 case HCI_EV_CONN_COMPLETE:
948 hci_conn_complete_evt(hdev, skb);
949 break;
951 case HCI_EV_DISCONN_COMPLETE:
952 hci_disconn_complete_evt(hdev, skb);
953 break;
955 case HCI_EV_ROLE_CHANGE:
956 hci_role_change_evt(hdev, skb);
957 break;
959 case HCI_EV_AUTH_COMPLETE:
960 hci_auth_complete_evt(hdev, skb);
961 break;
963 case HCI_EV_ENCRYPT_CHANGE:
964 hci_encrypt_change_evt(hdev, skb);
965 break;
967 case HCI_EV_CHANGE_CONN_LINK_KEY_COMPLETE:
968 hci_change_conn_link_key_complete_evt(hdev, skb);
969 break;
971 case HCI_EV_PIN_CODE_REQ:
972 hci_pin_code_request_evt(hdev, skb);
973 break;
975 case HCI_EV_LINK_KEY_REQ:
976 hci_link_key_request_evt(hdev, skb);
977 break;
979 case HCI_EV_LINK_KEY_NOTIFY:
980 hci_link_key_notify_evt(hdev, skb);
981 break;
983 case HCI_EV_CLOCK_OFFSET:
984 hci_clock_offset_evt(hdev, skb);
985 break;
987 case HCI_EV_PSCAN_REP_MODE:
988 hci_pscan_rep_mode_evt(hdev, skb);
989 break;
991 case HCI_EV_CMD_STATUS:
992 cs = (struct hci_ev_cmd_status *) skb->data;
993 skb_pull(skb, sizeof(cs));
995 opcode = __le16_to_cpu(cs->opcode);
996 ogf = hci_opcode_ogf(opcode);
997 ocf = hci_opcode_ocf(opcode);
999 switch (ogf) {
1000 case OGF_INFO_PARAM:
1001 hci_cs_info_param(hdev, ocf, cs->status);
1002 break;
1004 case OGF_HOST_CTL:
1005 hci_cs_host_ctl(hdev, ocf, cs->status);
1006 break;
1008 case OGF_LINK_CTL:
1009 hci_cs_link_ctl(hdev, ocf, cs->status);
1010 break;
1012 case OGF_LINK_POLICY:
1013 hci_cs_link_policy(hdev, ocf, cs->status);
1014 break;
1016 default:
1017 BT_DBG("%s Command Status OGF %x", hdev->name, ogf);
1018 break;
1021 if (cs->ncmd) {
1022 atomic_set(&hdev->cmd_cnt, 1);
1023 if (!skb_queue_empty(&hdev->cmd_q))
1024 hci_sched_cmd(hdev);
1026 break;
1028 case HCI_EV_CMD_COMPLETE:
1029 ec = (struct hci_ev_cmd_complete *) skb->data;
1030 skb_pull(skb, sizeof(*ec));
1032 opcode = __le16_to_cpu(ec->opcode);
1033 ogf = hci_opcode_ogf(opcode);
1034 ocf = hci_opcode_ocf(opcode);
1036 switch (ogf) {
1037 case OGF_INFO_PARAM:
1038 hci_cc_info_param(hdev, ocf, skb);
1039 break;
1041 case OGF_HOST_CTL:
1042 hci_cc_host_ctl(hdev, ocf, skb);
1043 break;
1045 case OGF_LINK_CTL:
1046 hci_cc_link_ctl(hdev, ocf, skb);
1047 break;
1049 case OGF_LINK_POLICY:
1050 hci_cc_link_policy(hdev, ocf, skb);
1051 break;
1053 default:
1054 BT_DBG("%s Command Completed OGF %x", hdev->name, ogf);
1055 break;
1058 if (ec->ncmd) {
1059 atomic_set(&hdev->cmd_cnt, 1);
1060 if (!skb_queue_empty(&hdev->cmd_q))
1061 hci_sched_cmd(hdev);
1063 break;
1066 kfree_skb(skb);
1067 hdev->stat.evt_rx++;
1070 /* Generate internal stack event */
1071 void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
1073 struct hci_event_hdr *hdr;
1074 struct hci_ev_stack_internal *ev;
1075 struct sk_buff *skb;
1077 skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC);
1078 if (!skb)
1079 return;
1081 hdr = (void *) skb_put(skb, HCI_EVENT_HDR_SIZE);
1082 hdr->evt = HCI_EV_STACK_INTERNAL;
1083 hdr->plen = sizeof(*ev) + dlen;
1085 ev = (void *) skb_put(skb, sizeof(*ev) + dlen);
1086 ev->type = type;
1087 memcpy(ev->data, data, dlen);
1089 bt_cb(skb)->incoming = 1;
1090 do_gettimeofday(&skb->stamp);
1092 skb->pkt_type = HCI_EVENT_PKT;
1093 skb->dev = (void *) hdev;
1094 hci_send_to_sock(hdev, skb);
1095 kfree_skb(skb);