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