[PATCH] irq-flags: M68KNOMMU: Use the new IRQF_ constants
[linux-2.6/kvm.git] / net / bluetooth / hci_event.c
blob618bacee1b1c3a365f8e67eba9522f91f88b7141
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;
87 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
89 switch (ocf) {
90 case OCF_ROLE_DISCOVERY:
91 rd = (void *) skb->data;
93 if (rd->status)
94 break;
96 hci_dev_lock(hdev);
98 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rd->handle));
99 if (conn) {
100 if (rd->role)
101 conn->link_mode &= ~HCI_LM_MASTER;
102 else
103 conn->link_mode |= HCI_LM_MASTER;
106 hci_dev_unlock(hdev);
107 break;
109 default:
110 BT_DBG("%s: Command complete: ogf LINK_POLICY ocf %x",
111 hdev->name, ocf);
112 break;
116 /* Command Complete OGF HOST_CTL */
117 static void hci_cc_host_ctl(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
119 __u8 status, param;
120 __u16 setting;
121 struct hci_rp_read_voice_setting *vs;
122 void *sent;
124 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
126 switch (ocf) {
127 case OCF_RESET:
128 status = *((__u8 *) skb->data);
129 hci_req_complete(hdev, status);
130 break;
132 case OCF_SET_EVENT_FLT:
133 status = *((__u8 *) skb->data);
134 if (status) {
135 BT_DBG("%s SET_EVENT_FLT failed %d", hdev->name, status);
136 } else {
137 BT_DBG("%s SET_EVENT_FLT succeseful", hdev->name);
139 break;
141 case OCF_WRITE_AUTH_ENABLE:
142 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_AUTH_ENABLE);
143 if (!sent)
144 break;
146 status = *((__u8 *) skb->data);
147 param = *((__u8 *) sent);
149 if (!status) {
150 if (param == AUTH_ENABLED)
151 set_bit(HCI_AUTH, &hdev->flags);
152 else
153 clear_bit(HCI_AUTH, &hdev->flags);
155 hci_req_complete(hdev, status);
156 break;
158 case OCF_WRITE_ENCRYPT_MODE:
159 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_ENCRYPT_MODE);
160 if (!sent)
161 break;
163 status = *((__u8 *) skb->data);
164 param = *((__u8 *) sent);
166 if (!status) {
167 if (param)
168 set_bit(HCI_ENCRYPT, &hdev->flags);
169 else
170 clear_bit(HCI_ENCRYPT, &hdev->flags);
172 hci_req_complete(hdev, status);
173 break;
175 case OCF_WRITE_CA_TIMEOUT:
176 status = *((__u8 *) skb->data);
177 if (status) {
178 BT_DBG("%s OCF_WRITE_CA_TIMEOUT failed %d", hdev->name, status);
179 } else {
180 BT_DBG("%s OCF_WRITE_CA_TIMEOUT succeseful", hdev->name);
182 break;
184 case OCF_WRITE_PG_TIMEOUT:
185 status = *((__u8 *) skb->data);
186 if (status) {
187 BT_DBG("%s OCF_WRITE_PG_TIMEOUT failed %d", hdev->name, status);
188 } else {
189 BT_DBG("%s: OCF_WRITE_PG_TIMEOUT succeseful", hdev->name);
191 break;
193 case OCF_WRITE_SCAN_ENABLE:
194 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_SCAN_ENABLE);
195 if (!sent)
196 break;
198 status = *((__u8 *) skb->data);
199 param = *((__u8 *) sent);
201 BT_DBG("param 0x%x", param);
203 if (!status) {
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);
213 break;
215 case OCF_READ_VOICE_SETTING:
216 vs = (struct hci_rp_read_voice_setting *) skb->data;
218 if (vs->status) {
219 BT_DBG("%s READ_VOICE_SETTING failed %d", hdev->name, vs->status);
220 break;
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);
230 if (hdev->notify) {
231 tasklet_disable(&hdev->tx_task);
232 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
233 tasklet_enable(&hdev->tx_task);
236 break;
238 case OCF_WRITE_VOICE_SETTING:
239 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_VOICE_SETTING);
240 if (!sent)
241 break;
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);
251 if (hdev->notify) {
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);
258 break;
260 case OCF_HOST_BUFFER_SIZE:
261 status = *((__u8 *) skb->data);
262 if (status) {
263 BT_DBG("%s OCF_BUFFER_SIZE failed %d", hdev->name, status);
264 hci_req_complete(hdev, status);
266 break;
268 default:
269 BT_DBG("%s Command complete: ogf HOST_CTL ocf %x", hdev->name, ocf);
270 break;
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);
283 switch (ocf) {
284 case OCF_READ_LOCAL_FEATURES:
285 lf = (struct hci_rp_read_loc_features *) skb->data;
287 if (lf->status) {
288 BT_DBG("%s READ_LOCAL_FEATURES failed %d", hdev->name, lf->status);
289 break;
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]);
310 break;
312 case OCF_READ_BUFFER_SIZE:
313 bs = (struct hci_rp_read_buffer_size *) skb->data;
315 if (bs->status) {
316 BT_DBG("%s READ_BUFFER_SIZE failed %d", hdev->name, bs->status);
317 hci_req_complete(hdev, bs->status);
318 break;
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);
328 break;
330 case OCF_READ_BD_ADDR:
331 ba = (struct hci_rp_read_bd_addr *) skb->data;
333 if (!ba->status) {
334 bacpy(&hdev->bdaddr, &ba->bdaddr);
335 } else {
336 BT_DBG("%s: READ_BD_ADDR failed %d", hdev->name, ba->status);
339 hci_req_complete(hdev, ba->status);
340 break;
342 default:
343 BT_DBG("%s Command complete: ogf INFO_PARAM ocf %x", hdev->name, ocf);
344 break;
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);
354 if (!cp)
355 return;
357 hci_dev_lock(hdev);
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);
364 if (status) {
365 if (conn && conn->state == BT_CONNECT) {
366 conn->state = BT_CLOSED;
367 hci_proto_connect_cfm(conn, status);
368 hci_conn_del(conn);
370 } else {
371 if (!conn) {
372 conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr);
373 if (conn) {
374 conn->out = 1;
375 conn->link_mode |= HCI_LM_MASTER;
376 } else
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);
388 switch (ocf) {
389 case OCF_CREATE_CONN:
390 hci_cs_create_conn(hdev, status);
391 break;
393 case OCF_ADD_SCO:
394 if (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);
397 __u16 handle;
399 if (!cp)
400 break;
402 handle = __le16_to_cpu(cp->handle);
404 BT_DBG("%s Add SCO error: handle %d status 0x%x", hdev->name, handle, status);
406 hci_dev_lock(hdev);
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);
413 hci_conn_del(sco);
416 hci_dev_unlock(hdev);
418 break;
420 case OCF_INQUIRY:
421 if (status) {
422 BT_DBG("%s Inquiry error: status 0x%x", hdev->name, status);
423 hci_req_complete(hdev, status);
424 } else {
425 set_bit(HCI_INQUIRY, &hdev->flags);
427 break;
429 default:
430 BT_DBG("%s Command status: ogf LINK_CTL ocf %x status %d",
431 hdev->name, ocf, status);
432 break;
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);
441 switch (ocf) {
442 default:
443 BT_DBG("%s Command status: ogf HOST_POLICY ocf %x", hdev->name, ocf);
444 break;
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);
453 switch (ocf) {
454 default:
455 BT_DBG("%s Command status: ogf HOST_CTL ocf %x", hdev->name, ocf);
456 break;
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);
465 switch (ocf) {
466 default:
467 BT_DBG("%s Command status: ogf INFO_PARAM ocf %x", hdev->name, ocf);
468 break;
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);
483 /* Inquiry Result */
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);
492 if (!num_rsp)
493 return;
495 hci_dev_lock(hdev);
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;
504 data.rssi = 0x00;
505 info++;
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);
520 if (!num_rsp)
521 return;
523 hci_dev_lock(hdev);
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;
537 info++;
538 hci_inquiry_cache_update(hdev, &data);
540 } else {
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;
552 info++;
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);
569 if (!num_rsp)
570 return;
572 hci_dev_lock(hdev);
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;
582 info++;
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;
605 hci_dev_lock(hdev);
606 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
607 if (!conn) {
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);
611 return;
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 */
622 else
623 cp.role = 0x01; /* Remain slave */
625 hci_send_cmd(hdev, OGF_LINK_CTL, OCF_ACCEPT_CONN_REQ, sizeof(cp), &cp);
626 } else {
627 /* Connection rejected */
628 struct hci_cp_reject_conn_req cp;
630 bacpy(&cp.bdaddr, &ev->bdaddr);
631 cp.reason = 0x0f;
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);
644 hci_dev_lock(hdev);
646 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
647 if (!conn) {
648 hci_dev_unlock(hdev);
649 return;
652 if (!ev->status) {
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 */
671 if (!conn->out) {
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);
680 } else
681 conn->state = BT_CLOSED;
683 if (conn->type == ACL_LINK) {
684 struct hci_conn *sco = conn->link;
685 if (sco) {
686 if (!ev->status)
687 hci_add_sco(sco, conn->handle);
688 else {
689 hci_proto_connect_cfm(sco, ev->status);
690 hci_conn_del(sco);
695 hci_proto_connect_cfm(conn, ev->status);
696 if (ev->status)
697 hci_conn_del(conn);
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);
711 if (ev->status)
712 return;
714 hci_dev_lock(hdev);
716 conn = hci_conn_hash_lookup_handle(hdev, handle);
717 if (conn) {
718 conn->state = BT_CLOSED;
719 hci_proto_disconn_ind(conn, ev->reason);
720 hci_conn_del(conn);
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;
730 __le16 *ptr;
731 int i;
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);
739 return;
742 tasklet_disable(&hdev->tx_task);
744 for (i = 0, ptr = (__le16 *) skb->data; i < ev->num_hndl; i++) {
745 struct hci_conn *conn;
746 __u16 handle, count;
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);
752 if (conn) {
753 conn->sent -= count;
755 if (conn->type == SCO_LINK) {
756 if ((hdev->sco_cnt += count) > hdev->sco_pkts)
757 hdev->sco_cnt = hdev->sco_pkts;
758 } else {
759 if ((hdev->acl_cnt += count) > hdev->acl_pkts)
760 hdev->acl_cnt = hdev->acl_pkts;
764 hci_sched_tx(hdev);
766 tasklet_enable(&hdev->tx_task);
769 /* Role Change */
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);
777 hci_dev_lock(hdev);
779 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
780 if (conn) {
781 if (!ev->status) {
782 if (ev->role)
783 conn->link_mode &= ~HCI_LM_MASTER;
784 else
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);
805 hci_dev_lock(hdev);
807 conn = hci_conn_hash_lookup_handle(hdev, handle);
808 if (conn) {
809 if (!ev->status)
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)) {
817 if (!ev->status) {
818 struct hci_cp_set_conn_encrypt cp;
819 cp.handle = __cpu_to_le16(conn->handle);
820 cp.encrypt = 1;
821 hci_send_cmd(conn->hdev, OGF_LINK_CTL,
822 OCF_SET_CONN_ENCRYPT,
823 sizeof(cp), &cp);
824 } else {
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);
843 hci_dev_lock(hdev);
845 conn = hci_conn_hash_lookup_handle(hdev, handle);
846 if (conn) {
847 if (!ev->status) {
848 if (ev->encrypt)
849 conn->link_mode |= HCI_LM_ENCRYPT;
850 else
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);
871 hci_dev_lock(hdev);
873 conn = hci_conn_hash_lookup_handle(hdev, handle);
874 if (conn) {
875 if (!ev->status)
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)
901 /* Clock Offset */
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);
910 hci_dev_lock(hdev);
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);
933 hci_dev_lock(hdev);
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);
954 switch (hdr->evt) {
955 case HCI_EV_NUM_COMP_PKTS:
956 hci_num_comp_pkts_evt(hdev, skb);
957 break;
959 case HCI_EV_INQUIRY_COMPLETE:
960 hci_inquiry_complete_evt(hdev, skb);
961 break;
963 case HCI_EV_INQUIRY_RESULT:
964 hci_inquiry_result_evt(hdev, skb);
965 break;
967 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
968 hci_inquiry_result_with_rssi_evt(hdev, skb);
969 break;
971 case HCI_EV_EXTENDED_INQUIRY_RESULT:
972 hci_extended_inquiry_result_evt(hdev, skb);
973 break;
975 case HCI_EV_CONN_REQUEST:
976 hci_conn_request_evt(hdev, skb);
977 break;
979 case HCI_EV_CONN_COMPLETE:
980 hci_conn_complete_evt(hdev, skb);
981 break;
983 case HCI_EV_DISCONN_COMPLETE:
984 hci_disconn_complete_evt(hdev, skb);
985 break;
987 case HCI_EV_ROLE_CHANGE:
988 hci_role_change_evt(hdev, skb);
989 break;
991 case HCI_EV_AUTH_COMPLETE:
992 hci_auth_complete_evt(hdev, skb);
993 break;
995 case HCI_EV_ENCRYPT_CHANGE:
996 hci_encrypt_change_evt(hdev, skb);
997 break;
999 case HCI_EV_CHANGE_CONN_LINK_KEY_COMPLETE:
1000 hci_change_conn_link_key_complete_evt(hdev, skb);
1001 break;
1003 case HCI_EV_PIN_CODE_REQ:
1004 hci_pin_code_request_evt(hdev, skb);
1005 break;
1007 case HCI_EV_LINK_KEY_REQ:
1008 hci_link_key_request_evt(hdev, skb);
1009 break;
1011 case HCI_EV_LINK_KEY_NOTIFY:
1012 hci_link_key_notify_evt(hdev, skb);
1013 break;
1015 case HCI_EV_CLOCK_OFFSET:
1016 hci_clock_offset_evt(hdev, skb);
1017 break;
1019 case HCI_EV_PSCAN_REP_MODE:
1020 hci_pscan_rep_mode_evt(hdev, skb);
1021 break;
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);
1031 switch (ogf) {
1032 case OGF_INFO_PARAM:
1033 hci_cs_info_param(hdev, ocf, cs->status);
1034 break;
1036 case OGF_HOST_CTL:
1037 hci_cs_host_ctl(hdev, ocf, cs->status);
1038 break;
1040 case OGF_LINK_CTL:
1041 hci_cs_link_ctl(hdev, ocf, cs->status);
1042 break;
1044 case OGF_LINK_POLICY:
1045 hci_cs_link_policy(hdev, ocf, cs->status);
1046 break;
1048 default:
1049 BT_DBG("%s Command Status OGF %x", hdev->name, ogf);
1050 break;
1053 if (cs->ncmd) {
1054 atomic_set(&hdev->cmd_cnt, 1);
1055 if (!skb_queue_empty(&hdev->cmd_q))
1056 hci_sched_cmd(hdev);
1058 break;
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);
1068 switch (ogf) {
1069 case OGF_INFO_PARAM:
1070 hci_cc_info_param(hdev, ocf, skb);
1071 break;
1073 case OGF_HOST_CTL:
1074 hci_cc_host_ctl(hdev, ocf, skb);
1075 break;
1077 case OGF_LINK_CTL:
1078 hci_cc_link_ctl(hdev, ocf, skb);
1079 break;
1081 case OGF_LINK_POLICY:
1082 hci_cc_link_policy(hdev, ocf, skb);
1083 break;
1085 default:
1086 BT_DBG("%s Command Completed OGF %x", hdev->name, ogf);
1087 break;
1090 if (ec->ncmd) {
1091 atomic_set(&hdev->cmd_cnt, 1);
1092 if (!skb_queue_empty(&hdev->cmd_q))
1093 hci_sched_cmd(hdev);
1095 break;
1098 kfree_skb(skb);
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);
1110 if (!skb)
1111 return;
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);
1118 ev->type = type;
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);
1127 kfree_skb(skb);