ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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.3 2002/04/17 17:37:16 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 CONFIG_BT_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         __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  */
88 static void hci_cc_link_policy(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
89 {
90         struct hci_conn *conn;
91         struct hci_rp_role_discovery *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 = hci_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  */
123 static void hci_cc_host_ctl(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
124 {
125         __u8 status, param;
126         __u16 setting;
127         struct hci_rp_read_voice_setting *vs;
128         void *sent;
129
130         BT_DBG("%s ocf 0x%x", hdev->name, ocf);
131
132         switch (ocf) {
133         case OCF_RESET:
134                 status = *((__u8 *) skb->data);
135                 hci_req_complete(hdev, status);
136                 break;
137
138         case OCF_SET_EVENT_FLT:
139                 status = *((__u8 *) skb->data);
140                 if (status) {
141                         BT_DBG("%s SET_EVENT_FLT failed %d", hdev->name, status);
142                 } else {
143                         BT_DBG("%s SET_EVENT_FLT succeseful", hdev->name);
144                 }
145                 break;
146
147         case OCF_WRITE_AUTH_ENABLE:
148                 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_AUTH_ENABLE);
149                 if (!sent)
150                         break;
151
152                 status = *((__u8 *) skb->data);
153                 param  = *((__u8 *) sent);
154
155                 if (!status) {
156                         if (param == AUTH_ENABLED)
157                                 set_bit(HCI_AUTH, &hdev->flags);
158                         else
159                                 clear_bit(HCI_AUTH, &hdev->flags);
160                 }
161                 hci_req_complete(hdev, status);
162                 break;
163
164         case OCF_WRITE_ENCRYPT_MODE:
165                 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_ENCRYPT_MODE);
166                 if (!sent)
167                         break;
168
169                 status = *((__u8 *) skb->data);
170                 param  = *((__u8 *) sent);
171
172                 if (!status) {
173                         if (param)
174                                 set_bit(HCI_ENCRYPT, &hdev->flags);
175                         else
176                                 clear_bit(HCI_ENCRYPT, &hdev->flags);
177                 }
178                 hci_req_complete(hdev, status);
179                 break;
180
181         case OCF_WRITE_CA_TIMEOUT:
182                 status = *((__u8 *) skb->data);
183                 if (status) {
184                         BT_DBG("%s OCF_WRITE_CA_TIMEOUT failed %d", hdev->name, status);
185                 } else {
186                         BT_DBG("%s OCF_WRITE_CA_TIMEOUT succeseful", hdev->name);
187                 }
188                 break;
189
190         case OCF_WRITE_PG_TIMEOUT:
191                 status = *((__u8 *) skb->data);
192                 if (status) {
193                         BT_DBG("%s OCF_WRITE_PG_TIMEOUT failed %d", hdev->name, status);
194                 } else {
195                         BT_DBG("%s: OCF_WRITE_PG_TIMEOUT succeseful", hdev->name);
196                 }
197                 break;
198
199         case OCF_WRITE_SCAN_ENABLE:
200                 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_SCAN_ENABLE);
201                 if (!sent)
202                         break;
203
204                 status = *((__u8 *) skb->data);
205                 param  = *((__u8 *) sent);
206
207                 BT_DBG("param 0x%x", param);
208
209                 if (!status) {
210                         clear_bit(HCI_PSCAN, &hdev->flags);
211                         clear_bit(HCI_ISCAN, &hdev->flags);
212                         if (param & SCAN_INQUIRY) 
213                                 set_bit(HCI_ISCAN, &hdev->flags);
214
215                         if (param & SCAN_PAGE) 
216                                 set_bit(HCI_PSCAN, &hdev->flags);
217                 }
218                 hci_req_complete(hdev, status);
219                 break;
220
221         case OCF_READ_VOICE_SETTING:
222                 vs = (struct hci_rp_read_voice_setting *) skb->data;
223
224                 if (vs->status) {
225                         BT_DBG("%s READ_VOICE_SETTING failed %d", hdev->name, vs->status);
226                         break;
227                 }
228
229                 setting = __le16_to_cpu(vs->voice_setting);
230
231                 if (hdev->voice_setting != setting ) {
232                         hdev->voice_setting = setting;
233
234                         BT_DBG("%s: voice setting 0x%04x", hdev->name, setting);
235
236                         if (hdev->notify)
237                                 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
238                 }
239                 break;
240
241         case OCF_WRITE_VOICE_SETTING:
242                 sent = hci_sent_cmd_data(hdev, OGF_HOST_CTL, OCF_WRITE_VOICE_SETTING);
243                 if (!sent)
244                         break;
245
246                 status = *((__u8 *) skb->data);
247                 setting = __le16_to_cpu(get_unaligned((__u16 *) sent));
248
249                 if (!status && hdev->voice_setting != setting) {
250                         hdev->voice_setting = setting;
251
252                         BT_DBG("%s: voice setting 0x%04x", hdev->name, setting);
253
254                         if (hdev->notify)
255                                 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
256                 }
257                 hci_req_complete(hdev, status);
258                 break;
259
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);
265                 }
266                 break;
267
268         default:
269                 BT_DBG("%s Command complete: ogf HOST_CTL ocf %x", hdev->name, ocf);
270                 break;
271         }
272 }
273
274 /* Command Complete OGF INFO_PARAM  */
275 static void hci_cc_info_param(struct hci_dev *hdev, __u16 ocf, struct sk_buff *skb)
276 {
277         struct hci_rp_read_loc_features *lf;
278         struct hci_rp_read_buffer_size *bs;
279         struct hci_rp_read_bd_addr *ba;
280
281         BT_DBG("%s ocf 0x%x", hdev->name, ocf);
282
283         switch (ocf) {
284         case OCF_READ_LOCAL_FEATURES:
285                 lf = (struct hci_rp_read_loc_features *) skb->data;
286
287                 if (lf->status) {
288                         BT_DBG("%s READ_LOCAL_FEATURES failed %d", hdev->name, lf->status);
289                         break;
290                 }
291
292                 memcpy(hdev->features, lf->features, sizeof(hdev->features));
293
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);
298
299                 if (hdev->features[0] & LMP_5SLOT)
300                         hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
301
302                 if (hdev->features[1] & LMP_HV2)
303                         hdev->pkt_type |= (HCI_HV2);
304
305                 if (hdev->features[1] & LMP_HV3)
306                         hdev->pkt_type |= (HCI_HV3);
307
308                 BT_DBG("%s: features 0x%x 0x%x 0x%x", hdev->name, lf->features[0], lf->features[1], lf->features[2]);
309
310                 break;
311
312         case OCF_READ_BUFFER_SIZE:
313                 bs = (struct hci_rp_read_buffer_size *) skb->data;
314
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;
319                 }
320
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);
325
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;
329
330         case OCF_READ_BD_ADDR:
331                 ba = (struct hci_rp_read_bd_addr *) skb->data;
332
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);
337                 }
338
339                 hci_req_complete(hdev, ba->status);
340                 break;
341
342         default:
343                 BT_DBG("%s Command complete: ogf INFO_PARAM ocf %x", hdev->name, ocf);
344                 break;
345         }
346 }
347
348 /* Command Status OGF LINK_CTL  */
349 static inline void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
350 {
351         struct hci_conn *conn;
352         struct hci_cp_create_conn *cp = hci_sent_cmd_data(hdev, OGF_LINK_CTL, OCF_CREATE_CONN);
353
354         if (!cp)
355                 return;
356
357         hci_dev_lock(hdev);
358         
359         conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
360
361         BT_DBG("%s status 0x%x bdaddr %s conn %p", hdev->name,
362                         status, batostr(&cp->bdaddr), conn);
363
364         if (status) {
365                 if (conn) {
366                         conn->state = BT_CLOSED;
367                         hci_proto_connect_cfm(conn, status);
368                         hci_conn_del(conn);
369                 }
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");
378                 }
379         }
380
381         hci_dev_unlock(hdev);
382 }
383
384 static void hci_cs_link_ctl(struct hci_dev *hdev, __u16 ocf, __u8 status)
385 {
386         BT_DBG("%s ocf 0x%x", hdev->name, ocf);
387
388         switch (ocf) {
389         case OCF_CREATE_CONN:
390                 hci_cs_create_conn(hdev, status);
391                 break;
392
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;
398
399                         if (!cp)
400                                 break;
401
402                         handle = __le16_to_cpu(cp->handle);
403
404                         BT_DBG("%s Add SCO error: handle %d status 0x%x", hdev->name, handle, status);
405
406                         hci_dev_lock(hdev);
407         
408                         acl = hci_conn_hash_lookup_handle(hdev, handle);
409                         if (acl && (sco = acl->link)) {
410                                 sco->state = BT_CLOSED;
411
412                                 hci_proto_connect_cfm(sco, status);
413                                 hci_conn_del(sco);
414                         }
415
416                         hci_dev_unlock(hdev);
417                 }
418                 break;
419
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);
426                 }
427                 break;
428
429         default:
430                 BT_DBG("%s Command status: ogf LINK_CTL ocf %x status %d", 
431                         hdev->name, ocf, status);
432                 break;
433         }
434 }
435
436 /* Command Status OGF LINK_POLICY */
437 static void hci_cs_link_policy(struct hci_dev *hdev, __u16 ocf, __u8 status)
438 {
439         BT_DBG("%s ocf 0x%x", hdev->name, ocf);
440
441         switch (ocf) {
442         default:
443                 BT_DBG("%s Command status: ogf HOST_POLICY ocf %x", hdev->name, ocf);
444                 break;
445         }
446 }
447
448 /* Command Status OGF HOST_CTL */
449 static void hci_cs_host_ctl(struct hci_dev *hdev, __u16 ocf, __u8 status)
450 {
451         BT_DBG("%s ocf 0x%x", hdev->name, ocf);
452
453         switch (ocf) {
454         default:
455                 BT_DBG("%s Command status: ogf HOST_CTL ocf %x", hdev->name, ocf);
456                 break;
457         }
458 }
459
460 /* Command Status OGF INFO_PARAM  */
461 static void hci_cs_info_param(struct hci_dev *hdev, __u16 ocf, __u8 status)
462 {
463         BT_DBG("%s: hci_cs_info_param: ocf 0x%x", hdev->name, ocf);
464
465         switch (ocf) {
466         default:
467                 BT_DBG("%s Command status: ogf INFO_PARAM ocf %x", hdev->name, ocf);
468                 break;
469         }
470 }
471
472 /* Inquiry Complete */
473 static inline void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
474 {
475         __u8 status = *((__u8 *) skb->data);
476
477         BT_DBG("%s status %d", hdev->name, status);
478
479         clear_bit(HCI_INQUIRY, &hdev->flags);
480         hci_req_complete(hdev, status);
481 }
482
483 /* Inquiry Result */
484 static inline void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
485 {
486         struct inquiry_info *info = (struct inquiry_info *) (skb->data + 1);
487         int num_rsp = *((__u8 *) skb->data);
488
489         BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
490
491         hci_dev_lock(hdev);
492         for (; num_rsp; num_rsp--)
493                 inquiry_cache_update(hdev, info++);
494         hci_dev_unlock(hdev);
495 }
496
497 /* Inquiry Result With RSSI */
498 static inline void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, struct sk_buff *skb)
499 {
500         struct inquiry_info_with_rssi *info = (struct inquiry_info_with_rssi *) (skb->data + 1);
501         int num_rsp = *((__u8 *) skb->data);
502
503         BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
504
505         hci_dev_lock(hdev);
506         for (; num_rsp; num_rsp--) {
507                 struct inquiry_info tmp;
508                 bacpy(&tmp.bdaddr, &info->bdaddr);
509                 tmp.pscan_rep_mode    = info->pscan_rep_mode;
510                 tmp.pscan_period_mode = info->pscan_period_mode;
511                 tmp.pscan_mode        = 0x00;
512                 memcpy(tmp.dev_class, &info->dev_class, 3);
513                 tmp.clock_offset      = info->clock_offset;
514                 info++;
515                 inquiry_cache_update(hdev, &tmp);
516         }
517         hci_dev_unlock(hdev);
518 }
519
520 /* Connect Request */
521 static inline void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
522 {
523         struct hci_ev_conn_request *ev = (struct hci_ev_conn_request *) skb->data;
524         int mask = hdev->link_mode;
525
526         BT_DBG("%s Connection request: %s type 0x%x", hdev->name,
527                         batostr(&ev->bdaddr), ev->link_type);
528
529         mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type);
530
531         if (mask & HCI_LM_ACCEPT) {
532                 /* Connection accepted */
533                 struct hci_conn *conn;
534                 struct hci_cp_accept_conn_req cp;
535
536                 hci_dev_lock(hdev);
537                 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
538                 if (!conn) {
539                         if (!(conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr))) {
540                                 BT_ERR("No memmory for new connection");
541                                 hci_dev_unlock(hdev);
542                                 return;
543                         }
544                 }
545                 conn->state = BT_CONNECT;
546                 hci_dev_unlock(hdev);
547
548                 bacpy(&cp.bdaddr, &ev->bdaddr);
549         
550                 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
551                         cp.role = 0x00; /* Become master */
552                 else
553                         cp.role = 0x01; /* Remain slave */
554
555                 hci_send_cmd(hdev, OGF_LINK_CTL, OCF_ACCEPT_CONN_REQ, sizeof(cp), &cp);
556         } else {
557                 /* Connection rejected */
558                 struct hci_cp_reject_conn_req cp;
559
560                 bacpy(&cp.bdaddr, &ev->bdaddr);
561                 cp.reason = 0x0f;
562                 hci_send_cmd(hdev, OGF_LINK_CTL, OCF_REJECT_CONN_REQ, sizeof(cp), &cp);
563         }
564 }
565
566 /* Connect Complete */
567 static inline void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
568 {
569         struct hci_ev_conn_complete *ev = (struct hci_ev_conn_complete *) skb->data;
570         struct hci_conn *conn = NULL;
571
572         BT_DBG("%s", hdev->name);
573
574         hci_dev_lock(hdev);
575         
576         conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
577         if (!conn) {
578                 hci_dev_unlock(hdev);
579                 return;
580         }
581
582         if (!ev->status) {
583                 conn->handle = __le16_to_cpu(ev->handle);
584                 conn->state  = BT_CONNECTED;
585
586                 if (test_bit(HCI_AUTH, &hdev->flags))
587                         conn->link_mode |= HCI_LM_AUTH;
588                 
589                 if (test_bit(HCI_ENCRYPT, &hdev->flags))
590                         conn->link_mode |= HCI_LM_ENCRYPT;
591
592
593                 /* Set link policy */
594                 if (conn->type == ACL_LINK && hdev->link_policy) {
595                         struct hci_cp_write_link_policy cp;
596                         cp.handle = ev->handle;
597                         cp.policy = __cpu_to_le16(hdev->link_policy);
598                         hci_send_cmd(hdev, OGF_LINK_POLICY, OCF_WRITE_LINK_POLICY, sizeof(cp), &cp);
599                 }
600
601                 /* Set packet type for incoming connection */
602                 if (!conn->out) {
603                         struct hci_cp_change_conn_ptype cp;
604                         cp.handle = ev->handle;
605                         cp.pkt_type = (conn->type == ACL_LINK) ? 
606                                 __cpu_to_le16(hdev->pkt_type & ACL_PTYPE_MASK):
607                                 __cpu_to_le16(hdev->pkt_type & SCO_PTYPE_MASK);
608
609                         hci_send_cmd(hdev, OGF_LINK_CTL, OCF_CHANGE_CONN_PTYPE, sizeof(cp), &cp);
610                 }
611         } else
612                 conn->state = BT_CLOSED;
613
614         if (conn->type == ACL_LINK) {
615                 struct hci_conn *sco = conn->link;
616                 if (sco) {
617                         if (!ev->status)
618                                 hci_add_sco(sco, conn->handle);
619                         else {
620                                 hci_proto_connect_cfm(sco, ev->status);
621                                 hci_conn_del(sco);
622                         }
623                 }
624         }
625
626         hci_proto_connect_cfm(conn, ev->status);
627         if (ev->status)
628                 hci_conn_del(conn);
629
630         hci_dev_unlock(hdev);
631 }
632
633 /* Disconnect Complete */
634 static inline void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
635 {
636         struct hci_ev_disconn_complete *ev = (struct hci_ev_disconn_complete *) skb->data;
637         struct hci_conn *conn = NULL;
638         __u16 handle = __le16_to_cpu(ev->handle);
639
640         BT_DBG("%s status %d", hdev->name, ev->status);
641
642         if (ev->status)
643                 return;
644
645         hci_dev_lock(hdev);
646         
647         conn = hci_conn_hash_lookup_handle(hdev, handle);
648         if (conn) {
649                 conn->state = BT_CLOSED;
650                 hci_proto_disconn_ind(conn, ev->reason);
651                 hci_conn_del(conn);
652         }
653
654         hci_dev_unlock(hdev);
655 }
656
657 /* Number of completed packets */
658 static inline void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
659 {
660         struct hci_ev_num_comp_pkts *ev = (struct hci_ev_num_comp_pkts *) skb->data;
661         __u16 *ptr;
662         int i;
663
664         skb_pull(skb, sizeof(*ev));
665
666         BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
667
668         if (skb->len < ev->num_hndl * 4) {
669                 BT_DBG("%s bad parameters", hdev->name);
670                 return;
671         }
672
673         tasklet_disable(&hdev->tx_task);
674
675         for (i = 0, ptr = (__u16 *) skb->data; i < ev->num_hndl; i++) {
676                 struct hci_conn *conn;
677                 __u16  handle, count;
678
679                 handle = __le16_to_cpu(get_unaligned(ptr++));
680                 count  = __le16_to_cpu(get_unaligned(ptr++));
681
682                 conn = hci_conn_hash_lookup_handle(hdev, handle);
683                 if (conn) {
684                         conn->sent -= count;
685
686                         if (conn->type == SCO_LINK) {
687                                 if ((hdev->sco_cnt += count) > hdev->sco_pkts)
688                                         hdev->sco_cnt = hdev->sco_pkts;
689                         } else {
690                                 if ((hdev->acl_cnt += count) > hdev->acl_pkts)
691                                         hdev->acl_cnt = hdev->acl_pkts;
692                         }
693                 }
694         }
695         hci_sched_tx(hdev);
696
697         tasklet_enable(&hdev->tx_task);
698 }
699
700 /* Role Change */
701 static inline void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
702 {
703         struct hci_ev_role_change *ev = (struct hci_ev_role_change *) skb->data;
704         struct hci_conn *conn = NULL;
705
706         BT_DBG("%s status %d", hdev->name, ev->status);
707
708         if (ev->status)
709                 return;
710
711         hci_dev_lock(hdev);
712         
713         conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
714         if (conn) {
715                 if (ev->role)
716                         conn->link_mode &= ~HCI_LM_MASTER;
717                 else 
718                         conn->link_mode |= HCI_LM_MASTER;
719         }
720
721         hci_dev_unlock(hdev);
722 }
723
724 /* Authentication Complete */
725 static inline void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
726 {
727         struct hci_ev_auth_complete *ev = (struct hci_ev_auth_complete *) skb->data;
728         struct hci_conn *conn = NULL;
729         __u16 handle = __le16_to_cpu(ev->handle);
730
731         BT_DBG("%s status %d", hdev->name, ev->status);
732
733         hci_dev_lock(hdev);
734         
735         conn = hci_conn_hash_lookup_handle(hdev, handle);
736         if (conn) {
737                 if (!ev->status)
738                         conn->link_mode |= HCI_LM_AUTH;
739                 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
740
741                 hci_proto_auth_cfm(conn, ev->status);
742                 
743                 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
744                         if (!ev->status) {
745                                 struct hci_cp_set_conn_encrypt cp;
746                                 cp.handle  = __cpu_to_le16(conn->handle);
747                                 cp.encrypt = 1;
748                                 hci_send_cmd(conn->hdev, OGF_LINK_CTL,
749                                                 OCF_SET_CONN_ENCRYPT,
750                                                 sizeof(cp), &cp);
751                         } else {
752                                 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
753                                 hci_proto_encrypt_cfm(conn, ev->status);
754                         }
755                 }
756         }
757
758         hci_dev_unlock(hdev);
759 }
760
761 /* Encryption Change */
762 static inline void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
763 {
764         struct hci_ev_encrypt_change *ev = (struct hci_ev_encrypt_change *) skb->data;
765         struct hci_conn *conn = NULL;
766         __u16 handle = __le16_to_cpu(ev->handle);
767
768         BT_DBG("%s status %d", hdev->name, ev->status);
769
770         hci_dev_lock(hdev);
771         
772         conn = hci_conn_hash_lookup_handle(hdev, handle);
773         if (conn) {
774                 if (!ev->status) {
775                         if (ev->encrypt)
776                                 conn->link_mode |= HCI_LM_ENCRYPT;
777                         else
778                                 conn->link_mode &= ~HCI_LM_ENCRYPT;
779                 }
780                 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
781                 
782                 hci_proto_encrypt_cfm(conn, ev->status);
783         }
784
785         hci_dev_unlock(hdev);
786 }
787
788 void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
789 {
790         struct hci_event_hdr *hdr = (struct hci_event_hdr *) skb->data;
791         struct hci_ev_cmd_complete *ec;
792         struct hci_ev_cmd_status *cs;
793         u16 opcode, ocf, ogf;
794
795         skb_pull(skb, HCI_EVENT_HDR_SIZE);
796
797         BT_DBG("%s evt 0x%x", hdev->name, hdr->evt);
798
799         switch (hdr->evt) {
800         case HCI_EV_NUM_COMP_PKTS:
801                 hci_num_comp_pkts_evt(hdev, skb);
802                 break;
803
804         case HCI_EV_INQUIRY_COMPLETE:
805                 hci_inquiry_complete_evt(hdev, skb);
806                 break;
807
808         case HCI_EV_INQUIRY_RESULT:
809                 hci_inquiry_result_evt(hdev, skb);
810                 break;
811
812         case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
813                 hci_inquiry_result_with_rssi_evt(hdev, skb);
814                 break;
815
816         case HCI_EV_CONN_REQUEST:
817                 hci_conn_request_evt(hdev, skb);
818                 break;
819
820         case HCI_EV_CONN_COMPLETE:
821                 hci_conn_complete_evt(hdev, skb);
822                 break;
823
824         case HCI_EV_DISCONN_COMPLETE:
825                 hci_disconn_complete_evt(hdev, skb);
826                 break;
827
828         case HCI_EV_ROLE_CHANGE:
829                 hci_role_change_evt(hdev, skb);
830                 break;
831
832         case HCI_EV_AUTH_COMPLETE:
833                 hci_auth_complete_evt(hdev, skb);
834                 break;
835
836         case HCI_EV_ENCRYPT_CHANGE:
837                 hci_encrypt_change_evt(hdev, skb);
838                 break;
839
840         case HCI_EV_CMD_STATUS:
841                 cs = (struct hci_ev_cmd_status *) skb->data;
842                 skb_pull(skb, sizeof(cs));
843                                 
844                 opcode = __le16_to_cpu(cs->opcode);
845                 ogf = hci_opcode_ogf(opcode);
846                 ocf = hci_opcode_ocf(opcode);
847
848                 switch (ogf) {
849                 case OGF_INFO_PARAM:
850                         hci_cs_info_param(hdev, ocf, cs->status);
851                         break;
852
853                 case OGF_HOST_CTL:
854                         hci_cs_host_ctl(hdev, ocf, cs->status);
855                         break;
856
857                 case OGF_LINK_CTL:
858                         hci_cs_link_ctl(hdev, ocf, cs->status);
859                         break;
860
861                 case OGF_LINK_POLICY:
862                         hci_cs_link_policy(hdev, ocf, cs->status);
863                         break;
864
865                 default:
866                         BT_DBG("%s Command Status OGF %x", hdev->name, ogf);
867                         break;
868                 }
869
870                 if (cs->ncmd) {
871                         atomic_set(&hdev->cmd_cnt, 1);
872                         if (!skb_queue_empty(&hdev->cmd_q))
873                                 hci_sched_cmd(hdev);
874                 }
875                 break;
876
877         case HCI_EV_CMD_COMPLETE:
878                 ec = (struct hci_ev_cmd_complete *) skb->data;
879                 skb_pull(skb, sizeof(*ec));
880
881                 opcode = __le16_to_cpu(ec->opcode);
882                 ogf = hci_opcode_ogf(opcode);
883                 ocf = hci_opcode_ocf(opcode);
884
885                 switch (ogf) {
886                 case OGF_INFO_PARAM:
887                         hci_cc_info_param(hdev, ocf, skb);
888                         break;
889
890                 case OGF_HOST_CTL:
891                         hci_cc_host_ctl(hdev, ocf, skb);
892                         break;
893
894                 case OGF_LINK_CTL:
895                         hci_cc_link_ctl(hdev, ocf, skb);
896                         break;
897
898                 case OGF_LINK_POLICY:
899                         hci_cc_link_policy(hdev, ocf, skb);
900                         break;
901
902                 default:
903                         BT_DBG("%s Command Completed OGF %x", hdev->name, ogf);
904                         break;
905                 }
906
907                 if (ec->ncmd) {
908                         atomic_set(&hdev->cmd_cnt, 1);
909                         if (!skb_queue_empty(&hdev->cmd_q))
910                                 hci_sched_cmd(hdev);
911                 }
912                 break;
913         }
914
915         kfree_skb(skb);
916         hdev->stat.evt_rx++;
917 }
918
919 /* Generate internal stack event */
920 void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
921 {
922         struct hci_event_hdr *hdr;
923         struct hci_ev_stack_internal *ev;
924         struct sk_buff *skb;
925
926         skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC);
927         if (!skb)
928                 return;
929
930         hdr = (void *) skb_put(skb, HCI_EVENT_HDR_SIZE);
931         hdr->evt  = HCI_EV_STACK_INTERNAL;
932         hdr->plen = sizeof(*ev) + dlen;
933
934         ev  = (void *) skb_put(skb, sizeof(*ev) + dlen);
935         ev->type = type;
936         memcpy(ev->data, data, dlen);
937         
938         skb->pkt_type = HCI_EVENT_PKT;
939         skb->dev = (void *) hdev;
940         hci_send_to_sock(hdev, skb);
941         kfree_skb(skb);
942 }