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