1 /*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated
4
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6
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;
10
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.
19
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.
23 */
24
25 /*
26 * HCI Events.
27 *
28 * $Id: hci_event.c,v 1.4 2002/07/27 18:14:38 maxk Exp $
29 */
30
31 #include <linux/config.h>
32 #include <linux/module.h>
33
34 #include <linux/types.h>
35 #include <linux/errno.h>
36 #include <linux/kernel.h>
37 #include <linux/major.h>
38 #include <linux/sched.h>
39 #include <linux/slab.h>
40 #include <linux/poll.h>
41 #include <linux/fcntl.h>
42 #include <linux/init.h>
43 #include <linux/skbuff.h>
44 #include <linux/interrupt.h>
45 #include <linux/notifier.h>
46 #include <net/sock.h>
47
48 #include <asm/system.h>
49 #include <asm/uaccess.h>
50 #include <asm/unaligned.h>
51
52 #include <net/bluetooth/bluetooth.h>
53 #include <net/bluetooth/hci_core.h>
54
55 #ifndef HCI_CORE_DEBUG
56 #undef BT_DBG
57 #define BT_DBG( A... )
58 #endif
59
60 /* Handle HCI Event packets */
61
62 /* Command Complete OGF LINK_CTL */
hci_cc_link_ctl(struct hci_dev * hdev,__u16 ocf,struct sk_buff * skb)63 static void hci_cc_link_ctl(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
64 {
65 __u8 status;
66
67 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
68
69 switch (ocf) {
70 case OCF_INQUIRY_CANCEL:
71 status = *((__u8 *) skb->data);
72
73 if (status) {
74 BT_DBG("%s Inquiry cancel error: status 0x%x", hdev->name, status);
75 } else {
76 clear_bit(HCI_INQUIRY, &hdev->flags);
77 hci_req_complete(hdev, status);
78 }
79 break;
80
81 default:
82 BT_DBG("%s Command complete: ogf LINK_CTL ocf %x", hdev->name, ocf);
83 break;
84 };
85 }
86
87 /* Command Complete OGF LINK_POLICY */
hci_cc_link_policy(struct hci_dev * hdev,__u16 ocf,struct sk_buff * skb)88 static void hci_cc_link_policy(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
89 {
90 struct hci_conn *conn;
91 role_discovery_rp *rd;
92
93 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
94
95 switch (ocf) {
96 case OCF_ROLE_DISCOVERY:
97 rd = (void *) skb->data;
98
99 if (rd->status)
100 break;
101
102 hci_dev_lock(hdev);
103
104 conn = conn_hash_lookup_handle(hdev, __le16_to_cpu(rd->handle));
105 if (conn) {
106 if (rd->role)
107 conn->link_mode &= ~HCI_LM_MASTER;
108 else
109 conn->link_mode |= HCI_LM_MASTER;
110 }
111
112 hci_dev_unlock(hdev);
113 break;
114
115 default:
116 BT_DBG("%s: Command complete: ogf LINK_POLICY ocf %x",
117 hdev->name, ocf);
118 break;
119 };
120 }
121
122 /* Command Complete OGF HOST_CTL */
hci_cc_host_ctl(struct hci_dev * hdev,__u16 ocf,struct sk_buff * skb)123 static void hci_cc_host_ctl(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
124 {
125 __u8 status, param;
126 void *sent;
127
128 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
129
130 switch (ocf) {
131 case OCF_RESET:
132 status = *((__u8 *) skb->data);
133 hci_req_complete(hdev, status);
134 break;
135
136 case OCF_SET_EVENT_FLT:
137 status = *((__u8 *) skb->data);
138 if (status) {
139 BT_DBG("%s SET_EVENT_FLT failed %d", hdev->name, status);
140 } else {
141 BT_DBG("%s SET_EVENT_FLT succeseful", hdev->name);
142 }
143 break;
144
145 case OCF_WRITE_AUTH_ENABLE:
146 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_AUTH_ENABLE);
147 if (!sent)
148 break;
149
150 status = *((__u8 *) skb->data);
151 param = *((__u8 *) sent);
152
153 if (!status) {
154 if (param == AUTH_ENABLED)
155 set_bit(HCI_AUTH, &hdev->flags);
156 else
157 clear_bit(HCI_AUTH, &hdev->flags);
158 }
159 hci_req_complete(hdev, status);
160 break;
161
162 case OCF_WRITE_ENCRYPT_MODE:
163 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_ENCRYPT_MODE);
164 if (!sent)
165 break;
166
167 status = *((__u8 *) skb->data);
168 param = *((__u8 *) sent);
169
170 if (!status) {
171 if (param)
172 set_bit(HCI_ENCRYPT, &hdev->flags);
173 else
174 clear_bit(HCI_ENCRYPT, &hdev->flags);
175 }
176 hci_req_complete(hdev, status);
177 break;
178
179 case OCF_WRITE_CA_TIMEOUT:
180 status = *((__u8 *) skb->data);
181 if (status) {
182 BT_DBG("%s OCF_WRITE_CA_TIMEOUT failed %d", hdev->name, status);
183 } else {
184 BT_DBG("%s OCF_WRITE_CA_TIMEOUT succeseful", hdev->name);
185 }
186 break;
187
188 case OCF_WRITE_PG_TIMEOUT:
189 status = *((__u8 *) skb->data);
190 if (status) {
191 BT_DBG("%s OCF_WRITE_PG_TIMEOUT failed %d", hdev->name, status);
192 } else {
193 BT_DBG("%s: OCF_WRITE_PG_TIMEOUT succeseful", hdev->name);
194 }
195 break;
196
197 case OCF_WRITE_SCAN_ENABLE:
198 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_SCAN_ENABLE);
199 if (!sent)
200 break;
201 status = *((__u8 *) skb->data);
202 param = *((__u8 *) sent);
203
204 BT_DBG("param 0x%x", param);
205
206 if (!status) {
207 clear_bit(HCI_PSCAN, &hdev->flags);
208 clear_bit(HCI_ISCAN, &hdev->flags);
209 if (param & SCAN_INQUIRY)
210 set_bit(HCI_ISCAN, &hdev->flags);
211
212 if (param & SCAN_PAGE)
213 set_bit(HCI_PSCAN, &hdev->flags);
214 }
215 hci_req_complete(hdev, status);
216 break;
217
218 case OCF_HOST_BUFFER_SIZE:
219 status = *((__u8 *) skb->data);
220 if (status) {
221 BT_DBG("%s OCF_BUFFER_SIZE failed %d", hdev->name, status);
222 hci_req_complete(hdev, status);
223 }
224 break;
225
226 default:
227 BT_DBG("%s Command complete: ogf HOST_CTL ocf %x", hdev->name, ocf);
228 break;
229 };
230 }
231
232 /* Command Complete OGF INFO_PARAM */
hci_cc_info_param(struct hci_dev * hdev,__u16 ocf,struct sk_buff * skb)233 static void hci_cc_info_param(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
234 {
235 read_local_features_rp *lf;
236 read_buffer_size_rp *bs;
237 read_bd_addr_rp *ba;
238
239 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
240
241 switch (ocf) {
242 case OCF_READ_LOCAL_FEATURES:
243 lf = (read_local_features_rp *) skb->data;
244
245 if (lf->status) {
246 BT_DBG("%s READ_LOCAL_FEATURES failed %d", hdev->name, lf->status);
247 break;
248 }
249
250 memcpy(hdev->features, lf->features, sizeof(hdev->features));
251
252 /* Adjust default settings according to features
253 * supported by device. */
254 if (hdev->features[0] & LMP_3SLOT)
255 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
256
257 if (hdev->features[0] & LMP_5SLOT)
258 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
259
260 if (hdev->features[1] & LMP_HV2)
261 hdev->pkt_type |= (HCI_HV2);
262
263 if (hdev->features[1] & LMP_HV3)
264 hdev->pkt_type |= (HCI_HV3);
265
266 BT_DBG("%s: features 0x%x 0x%x 0x%x", hdev->name, lf->features[0], lf->features[1], lf->features[2]);
267
268 break;
269
270 case OCF_READ_BUFFER_SIZE:
271 bs = (read_buffer_size_rp *) skb->data;
272
273 if (bs->status) {
274 BT_DBG("%s READ_BUFFER_SIZE failed %d", hdev->name, bs->status);
275 hci_req_complete(hdev, bs->status);
276 break;
277 }
278
279 hdev->acl_mtu = __le16_to_cpu(bs->acl_mtu);
280 hdev->sco_mtu = bs->sco_mtu ? bs->sco_mtu : 64;
281 hdev->acl_pkts = hdev->acl_cnt = __le16_to_cpu(bs->acl_max_pkt);
282 hdev->sco_pkts = hdev->sco_cnt = __le16_to_cpu(bs->sco_max_pkt);
283
284 BT_DBG("%s mtu: acl %d, sco %d max_pkt: acl %d, sco %d", hdev->name,
285 hdev->acl_mtu, hdev->sco_mtu, hdev->acl_pkts, hdev->sco_pkts);
286 break;
287
288 case OCF_READ_BD_ADDR:
289 ba = (read_bd_addr_rp *) skb->data;
290
291 if (!ba->status) {
292 bacpy(&hdev->bdaddr, &ba->bdaddr);
293 } else {
294 BT_DBG("%s: READ_BD_ADDR failed %d", hdev->name, ba->status);
295 }
296
297 hci_req_complete(hdev, ba->status);
298 break;
299
300 default:
301 BT_DBG("%s Command complete: ogf INFO_PARAM ocf %x", hdev->name, ocf);
302 break;
303 };
304 }
305
306 /* Command Status OGF LINK_CTL */
hci_cs_create_conn(struct hci_dev * hdev,__u8 status)307 static inline void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
308 {
309 struct hci_conn *conn;
310 create_conn_cp *cc = hci_sent_cmd_data(hdev, OGF_LINK_CTL, OCF_CREATE_CONN);
311
312 if (!cc)
313 return;
314
315 hci_dev_lock(hdev);
316
317 conn = conn_hash_lookup_ba(hdev, ACL_LINK, &cc->bdaddr);
318
319 BT_DBG("%s status 0x%x bdaddr %s conn %p", hdev->name,
320 status, batostr(&cc->bdaddr), conn);
321
322 if (status) {
323 if (conn && conn->state == BT_CONNECT) {
324 conn->state = BT_CLOSED;
325 hci_proto_connect_cfm(conn, status);
326 hci_conn_del(conn);
327 }
328 } else {
329 if (!conn) {
330 conn = hci_conn_add(hdev, ACL_LINK, &cc->bdaddr);
331 if (conn) {
332 conn->out = 1;
333 conn->link_mode |= HCI_LM_MASTER;
334 } else
335 BT_ERR("No memmory for new connection");
336 }
337 }
338
339 hci_dev_unlock(hdev);
340 }
341
hci_cs_link_ctl(struct hci_dev * hdev,__u16 ocf,__u8 status)342 static void hci_cs_link_ctl(struct hci_dev *hdev, __u16 ocf, __u8 status)
343 {
344 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
345
346 switch (ocf) {
347 case OCF_CREATE_CONN:
348 hci_cs_create_conn(hdev, status);
349 break;
350
351 case OCF_ADD_SCO:
352 if (status) {
353 struct hci_conn *acl, *sco;
354 add_sco_cp *cp = hci_sent_cmd_data(hdev,
355 OGF_LINK_CTL, OCF_ADD_SCO);
356 __u16 handle;
357
358 if (!cp)
359 break;
360
361 handle = __le16_to_cpu(cp->handle);
362
363 BT_DBG("%s Add SCO error: handle %d status 0x%x", hdev->name, handle, status);
364
365 hci_dev_lock(hdev);
366
367 acl = conn_hash_lookup_handle(hdev, handle);
368 if (acl && (sco = acl->link)) {
369 sco->state = BT_CLOSED;
370 hci_proto_connect_cfm(sco, status);
371 hci_conn_del(sco);
372 }
373
374 hci_dev_unlock(hdev);
375 }
376 break;
377
378 case OCF_INQUIRY:
379 if (status) {
380 BT_DBG("%s Inquiry error: status 0x%x", hdev->name, status);
381 hci_req_complete(hdev, status);
382 } else {
383 set_bit(HCI_INQUIRY, &hdev->flags);
384 }
385 break;
386
387 default:
388 BT_DBG("%s Command status: ogf LINK_CTL ocf %x status %d",
389 hdev->name, ocf, status);
390 break;
391 };
392 }
393
394 /* Command Status OGF LINK_POLICY */
hci_cs_link_policy(struct hci_dev * hdev,__u16 ocf,__u8 status)395 static void hci_cs_link_policy(struct hci_dev *hdev, __u16 ocf, __u8 status)
396 {
397 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
398
399 switch (ocf) {
400 default:
401 BT_DBG("%s Command status: ogf HOST_POLICY ocf %x", hdev->name, ocf);
402 break;
403 };
404 }
405
406 /* Command Status OGF HOST_CTL */
hci_cs_host_ctl(struct hci_dev * hdev,__u16 ocf,__u8 status)407 static void hci_cs_host_ctl(struct hci_dev *hdev, __u16 ocf, __u8 status)
408 {
409 BT_DBG("%s ocf 0x%x", hdev->name, ocf);
410
411 switch (ocf) {
412 default:
413 BT_DBG("%s Command status: ogf HOST_CTL ocf %x", hdev->name, ocf);
414 break;
415 };
416 }
417
418 /* Command Status OGF INFO_PARAM */
hci_cs_info_param(struct hci_dev * hdev,__u16 ocf,__u8 status)419 static void hci_cs_info_param(struct hci_dev *hdev, __u16 ocf, __u8 status)
420 {
421 BT_DBG("%s: hci_cs_info_param: ocf 0x%x", hdev->name, ocf);
422
423 switch (ocf) {
424 default:
425 BT_DBG("%s Command status: ogf INFO_PARAM ocf %x", hdev->name, ocf);
426 break;
427 };
428 }
429
430 /* Inquiry Complete */
hci_inquiry_complete_evt(struct hci_dev * hdev,struct sk_buff * skb)431 static inline void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
432 {
433 __u8 status = *((__u8 *) skb->data);
434
435 BT_DBG("%s status %d", hdev->name, status);
436
437 clear_bit(HCI_INQUIRY, &hdev->flags);
438 hci_req_complete(hdev, status);
439 }
440
441 /* Inquiry Result */
hci_inquiry_result_evt(struct hci_dev * hdev,struct sk_buff * skb)442 static inline void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
443 {
444 inquiry_info *info = (inquiry_info *) (skb->data + 1);
445 int num_rsp = *((__u8 *) skb->data);
446
447 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
448
449 hci_dev_lock(hdev);
450 for (; num_rsp; num_rsp--)
451 inquiry_cache_update(hdev, info++);
452 hci_dev_unlock(hdev);
453 }
454
455 /* Inquiry Result With RSSI */
hci_inquiry_result_with_rssi_evt(struct hci_dev * hdev,struct sk_buff * skb)456 static inline void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, struct sk_buff *skb)
457 {
458 inquiry_info_with_rssi *info = (inquiry_info_with_rssi *) (skb->data + 1);
459 int num_rsp = *((__u8 *) skb->data);
460
461 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
462
463 hci_dev_lock(hdev);
464 for (; num_rsp; num_rsp--) {
465 inquiry_info tmp;
466 bacpy(&tmp.bdaddr, &info->bdaddr);
467 tmp.pscan_rep_mode = info->pscan_rep_mode;
468 tmp.pscan_period_mode = info->pscan_period_mode;
469 tmp.pscan_mode = 0x00;
470 memcpy(tmp.dev_class, &info->dev_class, 3);
471 tmp.clock_offset = info->clock_offset;
472 info++;
473 inquiry_cache_update(hdev, &tmp);
474 }
475 hci_dev_unlock(hdev);
476 }
477
478 /* Connect Request */
hci_conn_request_evt(struct hci_dev * hdev,struct sk_buff * skb)479 static inline void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
480 {
481 evt_conn_request *cr = (evt_conn_request *) skb->data;
482 int mask = hdev->link_mode;
483
484 BT_DBG("%s Connection request: %s type 0x%x", hdev->name,
485 batostr(&cr->bdaddr), cr->link_type);
486
487 mask |= hci_proto_connect_ind(hdev, &cr->bdaddr, cr->link_type);
488
489 if (mask & HCI_LM_ACCEPT) {
490 /* Connection accepted */
491 struct hci_conn *conn;
492 accept_conn_req_cp ac;
493
494 hci_dev_lock(hdev);
495 conn = conn_hash_lookup_ba(hdev, cr->link_type, &cr->bdaddr);
496 if (!conn) {
497 if (!(conn = hci_conn_add(hdev, cr->link_type, &cr->bdaddr))) {
498 BT_ERR("No memmory for new connection");
499 hci_dev_unlock(hdev);
500 return;
501 }
502 }
503 conn->state = BT_CONNECT;
504 hci_dev_unlock(hdev);
505
506 bacpy(&ac.bdaddr, &cr->bdaddr);
507
508 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
509 ac.role = 0x00; /* Become master */
510 else
511 ac.role = 0x01; /* Remain slave */
512
513 hci_send_cmd(hdev, OGF_LINK_CTL, OCF_ACCEPT_CONN_REQ,
514 ACCEPT_CONN_REQ_CP_SIZE, &ac);
515 } else {
516 /* Connection rejected */
517 reject_conn_req_cp rc;
518
519 bacpy(&rc.bdaddr, &cr->bdaddr);
520 rc.reason = 0x0f;
521 hci_send_cmd(hdev, OGF_LINK_CTL, OCF_REJECT_CONN_REQ,
522 REJECT_CONN_REQ_CP_SIZE, &rc);
523 }
524 }
525
526 /* Connect Complete */
hci_conn_complete_evt(struct hci_dev * hdev,struct sk_buff * skb)527 static inline void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
528 {
529 evt_conn_complete *cc = (evt_conn_complete *) skb->data;
530 struct hci_conn *conn = NULL;
531
532 BT_DBG("%s", hdev->name);
533
534 hci_dev_lock(hdev);
535
536 conn = conn_hash_lookup_ba(hdev, cc->link_type, &cc->bdaddr);
537 if (!conn) {
538 hci_dev_unlock(hdev);
539 return;
540 }
541
542 if (!cc->status) {
543 conn->handle = __le16_to_cpu(cc->handle);
544 conn->state = BT_CONNECTED;
545
546 if (test_bit(HCI_AUTH, &hdev->flags))
547 conn->link_mode |= HCI_LM_AUTH;
548
549 if (test_bit(HCI_ENCRYPT, &hdev->flags))
550 conn->link_mode |= HCI_LM_ENCRYPT;
551
552
553 /* Set link policy */
554 if (conn->type == ACL_LINK && hdev->link_policy) {
555 write_link_policy_cp lp;
556 lp.handle = cc->handle;
557 lp.policy = __cpu_to_le16(hdev->link_policy);
558 hci_send_cmd(hdev, OGF_LINK_POLICY, OCF_WRITE_LINK_POLICY,
559 WRITE_LINK_POLICY_CP_SIZE, &lp);
560 }
561
562 /* Set packet type for incomming connection */
563 if (!conn->out) {
564 change_conn_ptype_cp cp;
565 cp.handle = cc->handle;
566 cp.pkt_type = (conn->type == ACL_LINK) ?
567 __cpu_to_le16(hdev->pkt_type & ACL_PTYPE_MASK):
568 __cpu_to_le16(hdev->pkt_type & SCO_PTYPE_MASK);
569
570 hci_send_cmd(hdev, OGF_LINK_CTL, OCF_CHANGE_CONN_PTYPE,
571 CHANGE_CONN_PTYPE_CP_SIZE, &cp);
572 }
573 } else
574 conn->state = BT_CLOSED;
575
576 if (conn->type == ACL_LINK) {
577 struct hci_conn *sco = conn->link;
578 if (sco) {
579 if (!cc->status)
580 hci_add_sco(sco, conn->handle);
581 else {
582 hci_proto_connect_cfm(sco, cc->status);
583 hci_conn_del(sco);
584 }
585 }
586 }
587
588 hci_proto_connect_cfm(conn, cc->status);
589 if (cc->status)
590 hci_conn_del(conn);
591
592 hci_dev_unlock(hdev);
593 }
594
595 /* Disconnect Complete */
hci_disconn_complete_evt(struct hci_dev * hdev,struct sk_buff * skb)596 static inline void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
597 {
598 evt_disconn_complete *dc = (evt_disconn_complete *) skb->data;
599 struct hci_conn *conn = NULL;
600 __u16 handle = __le16_to_cpu(dc->handle);
601
602 BT_DBG("%s status %d", hdev->name, dc->status);
603
604 if (dc->status)
605 return;
606
607 hci_dev_lock(hdev);
608
609 conn = conn_hash_lookup_handle(hdev, handle);
610 if (conn) {
611 conn->state = BT_CLOSED;
612 hci_proto_disconn_ind(conn, dc->reason);
613 hci_conn_del(conn);
614 }
615
616 hci_dev_unlock(hdev);
617 }
618
619 /* Number of completed packets */
hci_num_comp_pkts_evt(struct hci_dev * hdev,struct sk_buff * skb)620 static inline void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
621 {
622 evt_num_comp_pkts *nc = (evt_num_comp_pkts *) skb->data;
623 __u16 *ptr;
624 int i;
625
626 skb_pull(skb, EVT_NUM_COMP_PKTS_SIZE);
627
628 BT_DBG("%s num_hndl %d", hdev->name, nc->num_hndl);
629
630 if (skb->len < nc->num_hndl * 4) {
631 BT_DBG("%s bad parameters", hdev->name);
632 return;
633 }
634
635 tasklet_disable(&hdev->tx_task);
636
637 for (i = 0, ptr = (__u16 *) skb->data; i < nc->num_hndl; i++) {
638 struct hci_conn *conn;
639 __u16 handle, count;
640
641 handle = __le16_to_cpu(get_unaligned(ptr++));
642 count = __le16_to_cpu(get_unaligned(ptr++));
643
644 conn = conn_hash_lookup_handle(hdev, handle);
645 if (conn) {
646 conn->sent -= count;
647
648 if (conn->type == SCO_LINK) {
649 if ((hdev->sco_cnt += count) > hdev->sco_pkts)
650 hdev->sco_cnt = hdev->sco_pkts;
651 } else {
652 if ((hdev->acl_cnt += count) > hdev->acl_pkts)
653 hdev->acl_cnt = hdev->acl_pkts;
654 }
655 }
656 }
657 hci_sched_tx(hdev);
658
659 tasklet_enable(&hdev->tx_task);
660 }
661
662 /* Role Change */
hci_role_change_evt(struct hci_dev * hdev,struct sk_buff * skb)663 static inline void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
664 {
665 evt_role_change *rc = (evt_role_change *) skb->data;
666 struct hci_conn *conn = NULL;
667
668 BT_DBG("%s status %d", hdev->name, rc->status);
669
670 if (rc->status)
671 return;
672
673 hci_dev_lock(hdev);
674
675 conn = conn_hash_lookup_ba(hdev, ACL_LINK, &rc->bdaddr);
676 if (conn) {
677 if (rc->role)
678 conn->link_mode &= ~HCI_LM_MASTER;
679 else
680 conn->link_mode |= HCI_LM_MASTER;
681 }
682
683 hci_dev_unlock(hdev);
684 }
685
686 /* Authentication Complete */
hci_auth_complete_evt(struct hci_dev * hdev,struct sk_buff * skb)687 static inline void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
688 {
689 evt_auth_complete *ac = (evt_auth_complete *) skb->data;
690 struct hci_conn *conn = NULL;
691 __u16 handle = __le16_to_cpu(ac->handle);
692
693 BT_DBG("%s status %d", hdev->name, ac->status);
694
695 hci_dev_lock(hdev);
696
697 conn = conn_hash_lookup_handle(hdev, handle);
698 if (conn) {
699 if (!ac->status)
700 conn->link_mode |= HCI_LM_AUTH;
701 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
702
703 hci_proto_auth_cfm(conn, ac->status);
704
705 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
706 if (!ac->status) {
707 set_conn_encrypt_cp ce;
708 ce.handle = __cpu_to_le16(conn->handle);
709 ce.encrypt = 1;
710 hci_send_cmd(conn->hdev, OGF_LINK_CTL,
711 OCF_SET_CONN_ENCRYPT,
712 SET_CONN_ENCRYPT_CP_SIZE, &ce);
713 } else {
714 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
715 hci_proto_encrypt_cfm(conn, ac->status);
716 }
717 }
718 }
719
720 hci_dev_unlock(hdev);
721 }
722
723 /* Encryption Change */
hci_encrypt_change_evt(struct hci_dev * hdev,struct sk_buff * skb)724 static inline void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
725 {
726 evt_encrypt_change *ec = (evt_encrypt_change *) skb->data;
727 struct hci_conn *conn = NULL;
728 __u16 handle = __le16_to_cpu(ec->handle);
729
730 BT_DBG("%s status %d", hdev->name, ec->status);
731
732 hci_dev_lock(hdev);
733
734 conn = conn_hash_lookup_handle(hdev, handle);
735 if (conn) {
736 if (!ec->status) {
737 if (ec->encrypt)
738 conn->link_mode |= HCI_LM_ENCRYPT;
739 else
740 conn->link_mode &= ~HCI_LM_ENCRYPT;
741 }
742 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
743
744 hci_proto_encrypt_cfm(conn, ec->status);
745 }
746
747 hci_dev_unlock(hdev);
748 }
749
hci_event_packet(struct hci_dev * hdev,struct sk_buff * skb)750 void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
751 {
752 hci_event_hdr *he = (hci_event_hdr *) skb->data;
753 evt_cmd_status *cs;
754 evt_cmd_complete *ec;
755 __u16 opcode, ocf, ogf;
756
757 skb_pull(skb, HCI_EVENT_HDR_SIZE);
758
759 BT_DBG("%s evt 0x%x", hdev->name, he->evt);
760
761 switch (he->evt) {
762 case EVT_NUM_COMP_PKTS:
763 hci_num_comp_pkts_evt(hdev, skb);
764 break;
765
766 case EVT_INQUIRY_COMPLETE:
767 hci_inquiry_complete_evt(hdev, skb);
768 break;
769
770 case EVT_INQUIRY_RESULT:
771 hci_inquiry_result_evt(hdev, skb);
772 break;
773
774 case EVT_INQUIRY_RESULT_WITH_RSSI:
775 hci_inquiry_result_with_rssi_evt(hdev, skb);
776 break;
777
778 case EVT_CONN_REQUEST:
779 hci_conn_request_evt(hdev, skb);
780 break;
781
782 case EVT_CONN_COMPLETE:
783 hci_conn_complete_evt(hdev, skb);
784 break;
785
786 case EVT_DISCONN_COMPLETE:
787 hci_disconn_complete_evt(hdev, skb);
788 break;
789
790 case EVT_ROLE_CHANGE:
791 hci_role_change_evt(hdev, skb);
792 break;
793
794 case EVT_AUTH_COMPLETE:
795 hci_auth_complete_evt(hdev, skb);
796 break;
797
798 case EVT_ENCRYPT_CHANGE:
799 hci_encrypt_change_evt(hdev, skb);
800 break;
801
802 case EVT_CMD_STATUS:
803 cs = (evt_cmd_status *) skb->data;
804 skb_pull(skb, EVT_CMD_STATUS_SIZE);
805
806 opcode = __le16_to_cpu(cs->opcode);
807 ogf = cmd_opcode_ogf(opcode);
808 ocf = cmd_opcode_ocf(opcode);
809
810 switch (ogf) {
811 case OGF_INFO_PARAM:
812 hci_cs_info_param(hdev, ocf, cs->status);
813 break;
814
815 case OGF_HOST_CTL:
816 hci_cs_host_ctl(hdev, ocf, cs->status);
817 break;
818
819 case OGF_LINK_CTL:
820 hci_cs_link_ctl(hdev, ocf, cs->status);
821 break;
822
823 case OGF_LINK_POLICY:
824 hci_cs_link_policy(hdev, ocf, cs->status);
825 break;
826
827 default:
828 BT_DBG("%s Command Status OGF %x", hdev->name, ogf);
829 break;
830 };
831
832 if (cs->ncmd) {
833 atomic_set(&hdev->cmd_cnt, 1);
834 if (!skb_queue_empty(&hdev->cmd_q))
835 hci_sched_cmd(hdev);
836 }
837 break;
838
839 case EVT_CMD_COMPLETE:
840 ec = (evt_cmd_complete *) skb->data;
841 skb_pull(skb, EVT_CMD_COMPLETE_SIZE);
842
843 opcode = __le16_to_cpu(ec->opcode);
844 ogf = cmd_opcode_ogf(opcode);
845 ocf = cmd_opcode_ocf(opcode);
846
847 switch (ogf) {
848 case OGF_INFO_PARAM:
849 hci_cc_info_param(hdev, ocf, skb);
850 break;
851
852 case OGF_HOST_CTL:
853 hci_cc_host_ctl(hdev, ocf, skb);
854 break;
855
856 case OGF_LINK_CTL:
857 hci_cc_link_ctl(hdev, ocf, skb);
858 break;
859
860 case OGF_LINK_POLICY:
861 hci_cc_link_policy(hdev, ocf, skb);
862 break;
863
864 default:
865 BT_DBG("%s Command Completed OGF %x", hdev->name, ogf);
866 break;
867 };
868
869 if (ec->ncmd) {
870 atomic_set(&hdev->cmd_cnt, 1);
871 if (!skb_queue_empty(&hdev->cmd_q))
872 hci_sched_cmd(hdev);
873 }
874 break;
875 };
876
877 kfree_skb(skb);
878 hdev->stat.evt_rx++;
879 }
880
881 /* General internal stack event */
hci_si_event(struct hci_dev * hdev,int type,int dlen,void * data)882 void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
883 {
884 hci_event_hdr *eh;
885 evt_stack_internal *si;
886 struct sk_buff *skb;
887 int size;
888 void *ptr;
889
890 size = HCI_EVENT_HDR_SIZE + EVT_STACK_INTERNAL_SIZE + dlen;
891 skb = bluez_skb_alloc(size, GFP_ATOMIC);
892 if (!skb)
893 return;
894
895 ptr = skb_put(skb, size);
896
897 eh = ptr;
898 eh->evt = EVT_STACK_INTERNAL;
899 eh->plen = EVT_STACK_INTERNAL_SIZE + dlen;
900 ptr += HCI_EVENT_HDR_SIZE;
901
902 si = ptr;
903 si->type = type;
904 memcpy(si->data, data, dlen);
905
906 skb->pkt_type = HCI_EVENT_PKT;
907 skb->dev = (void *) hdev;
908 hci_send_to_sock(hdev, skb);
909 kfree_skb(skb);
910 }
911