blob: 6bc5a0506c6c0dbbb032113cff193da07942a032 [file] [log] [blame]
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 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
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090015 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090020 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 SOFTWARE IS DISCLAIMED.
23*/
24
25/* Bluetooth HCI event handling. */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
28
29#include <linux/types.h>
30#include <linux/errno.h>
31#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/slab.h>
33#include <linux/poll.h>
34#include <linux/fcntl.h>
35#include <linux/init.h>
36#include <linux/skbuff.h>
37#include <linux/interrupt.h>
38#include <linux/notifier.h>
39#include <net/sock.h>
40
41#include <asm/system.h>
42#include <asm/uaccess.h>
43#include <asm/unaligned.h>
44
45#include <net/bluetooth/bluetooth.h>
46#include <net/bluetooth/hci_core.h>
47
48#ifndef CONFIG_BT_HCI_CORE_DEBUG
49#undef BT_DBG
50#define BT_DBG(D...)
51#endif
52
53/* Handle HCI Event packets */
54
Marcel Holtmanna9de9242007-10-20 13:33:56 +020055static void hci_cc_inquiry_cancel(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Marcel Holtmanna9de9242007-10-20 13:33:56 +020057 __u8 status = *((__u8 *) skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Marcel Holtmanna9de9242007-10-20 13:33:56 +020059 BT_DBG("%s status 0x%x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Marcel Holtmanna9de9242007-10-20 13:33:56 +020061 if (status)
62 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Marcel Holtmanna9de9242007-10-20 13:33:56 +020064 clear_bit(HCI_INQUIRY, &hdev->flags);
Marcel Holtmann6bd57412006-11-18 22:14:22 +010065
Marcel Holtmanna9de9242007-10-20 13:33:56 +020066 hci_req_complete(hdev, status);
Marcel Holtmann6bd57412006-11-18 22:14:22 +010067
Marcel Holtmanna9de9242007-10-20 13:33:56 +020068 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
Marcel Holtmanna9de9242007-10-20 13:33:56 +020071static void hci_cc_exit_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Marcel Holtmanna9de9242007-10-20 13:33:56 +020073 __u8 status = *((__u8 *) skb->data);
74
75 BT_DBG("%s status 0x%x", hdev->name, status);
76
77 if (status)
78 return;
79
80 clear_bit(HCI_INQUIRY, &hdev->flags);
81
82 hci_conn_check_pending(hdev);
83}
84
85static void hci_cc_remote_name_req_cancel(struct hci_dev *hdev, struct sk_buff *skb)
86{
87 BT_DBG("%s", hdev->name);
88}
89
90static void hci_cc_role_discovery(struct hci_dev *hdev, struct sk_buff *skb)
91{
92 struct hci_rp_role_discovery *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Marcel Holtmanna9de9242007-10-20 13:33:56 +020095 BT_DBG("%s status 0x%x", hdev->name, rp->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Marcel Holtmanna9de9242007-10-20 13:33:56 +020097 if (rp->status)
98 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200100 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200102 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
103 if (conn) {
104 if (rp->role)
105 conn->link_mode &= ~HCI_LM_MASTER;
106 else
107 conn->link_mode |= HCI_LM_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200109
110 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200113static void hci_cc_write_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200115 struct hci_rp_write_link_policy *rp = (void *) skb->data;
116 struct hci_conn *conn;
117 void *sent;
118
119 BT_DBG("%s status 0x%x", hdev->name, rp->status);
120
121 if (rp->status)
122 return;
123
124 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LINK_POLICY);
125 if (!sent)
126 return;
127
128 hci_dev_lock(hdev);
129
130 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
131 if (conn) {
Harvey Harrison83985312008-05-02 16:25:46 -0700132 conn->link_policy = get_unaligned_le16(sent + 2);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200133 }
134
135 hci_dev_unlock(hdev);
136}
137
138static void hci_cc_reset(struct hci_dev *hdev, struct sk_buff *skb)
139{
140 __u8 status = *((__u8 *) skb->data);
141
142 BT_DBG("%s status 0x%x", hdev->name, status);
143
144 hci_req_complete(hdev, status);
145}
146
147static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb)
148{
149 __u8 status = *((__u8 *) skb->data);
150 void *sent;
151
152 BT_DBG("%s status 0x%x", hdev->name, status);
153
154 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LOCAL_NAME);
155 if (!sent)
156 return;
157
158 if (!status)
159 memcpy(hdev->dev_name, sent, 248);
160}
161
162static void hci_cc_read_local_name(struct hci_dev *hdev, struct sk_buff *skb)
163{
164 struct hci_rp_read_local_name *rp = (void *) skb->data;
165
166 BT_DBG("%s status 0x%x", hdev->name, rp->status);
167
168 if (rp->status)
169 return;
170
171 memcpy(hdev->dev_name, rp->name, 248);
172}
173
174static void hci_cc_write_auth_enable(struct hci_dev *hdev, struct sk_buff *skb)
175{
176 __u8 status = *((__u8 *) skb->data);
177 void *sent;
178
179 BT_DBG("%s status 0x%x", hdev->name, status);
180
181 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_ENABLE);
182 if (!sent)
183 return;
184
185 if (!status) {
186 __u8 param = *((__u8 *) sent);
187
188 if (param == AUTH_ENABLED)
189 set_bit(HCI_AUTH, &hdev->flags);
190 else
191 clear_bit(HCI_AUTH, &hdev->flags);
192 }
193
194 hci_req_complete(hdev, status);
195}
196
197static void hci_cc_write_encrypt_mode(struct hci_dev *hdev, struct sk_buff *skb)
198{
199 __u8 status = *((__u8 *) skb->data);
200 void *sent;
201
202 BT_DBG("%s status 0x%x", hdev->name, status);
203
204 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_ENCRYPT_MODE);
205 if (!sent)
206 return;
207
208 if (!status) {
209 __u8 param = *((__u8 *) sent);
210
211 if (param)
212 set_bit(HCI_ENCRYPT, &hdev->flags);
213 else
214 clear_bit(HCI_ENCRYPT, &hdev->flags);
215 }
216
217 hci_req_complete(hdev, status);
218}
219
220static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
221{
222 __u8 status = *((__u8 *) skb->data);
223 void *sent;
224
225 BT_DBG("%s status 0x%x", hdev->name, status);
226
227 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SCAN_ENABLE);
228 if (!sent)
229 return;
230
231 if (!status) {
232 __u8 param = *((__u8 *) sent);
233
234 clear_bit(HCI_PSCAN, &hdev->flags);
235 clear_bit(HCI_ISCAN, &hdev->flags);
236
237 if (param & SCAN_INQUIRY)
238 set_bit(HCI_ISCAN, &hdev->flags);
239
240 if (param & SCAN_PAGE)
241 set_bit(HCI_PSCAN, &hdev->flags);
242 }
243
244 hci_req_complete(hdev, status);
245}
246
247static void hci_cc_read_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
248{
249 struct hci_rp_read_class_of_dev *rp = (void *) skb->data;
250
251 BT_DBG("%s status 0x%x", hdev->name, rp->status);
252
253 if (rp->status)
254 return;
255
256 memcpy(hdev->dev_class, rp->dev_class, 3);
257
258 BT_DBG("%s class 0x%.2x%.2x%.2x", hdev->name,
259 hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
260}
261
262static void hci_cc_write_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
263{
264 __u8 status = *((__u8 *) skb->data);
265 void *sent;
266
267 BT_DBG("%s status 0x%x", hdev->name, status);
268
269 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_CLASS_OF_DEV);
270 if (!sent)
271 return;
272
273 if (!status)
274 memcpy(hdev->dev_class, sent, 3);
275}
276
277static void hci_cc_read_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
278{
279 struct hci_rp_read_voice_setting *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 __u16 setting;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200281
282 BT_DBG("%s status 0x%x", hdev->name, rp->status);
283
284 if (rp->status)
285 return;
286
287 setting = __le16_to_cpu(rp->voice_setting);
288
289 if (hdev->voice_setting == setting )
290 return;
291
292 hdev->voice_setting = setting;
293
294 BT_DBG("%s voice setting 0x%04x", hdev->name, setting);
295
296 if (hdev->notify) {
297 tasklet_disable(&hdev->tx_task);
298 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
299 tasklet_enable(&hdev->tx_task);
300 }
301}
302
303static void hci_cc_write_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
304{
305 __u8 status = *((__u8 *) skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 void *sent;
307
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200308 BT_DBG("%s status 0x%x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200310 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_VOICE_SETTING);
311 if (!sent)
312 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200314 if (!status) {
Harvey Harrison83985312008-05-02 16:25:46 -0700315 __u16 setting = get_unaligned_le16(sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200317 if (hdev->voice_setting != setting) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 hdev->voice_setting = setting;
319
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200320 BT_DBG("%s voice setting 0x%04x", hdev->name, setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 if (hdev->notify) {
323 tasklet_disable(&hdev->tx_task);
324 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
325 tasklet_enable(&hdev->tx_task);
326 }
327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329}
330
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200331static void hci_cc_host_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200333 __u8 status = *((__u8 *) skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200335 BT_DBG("%s status 0x%x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200337 hci_req_complete(hdev, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200340static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
341{
342 struct hci_rp_read_local_version *rp = (void *) skb->data;
343
344 BT_DBG("%s status 0x%x", hdev->name, rp->status);
345
346 if (rp->status)
347 return;
348
349 hdev->hci_ver = rp->hci_ver;
350 hdev->hci_rev = btohs(rp->hci_rev);
351 hdev->manufacturer = btohs(rp->manufacturer);
352
353 BT_DBG("%s manufacturer %d hci ver %d:%d", hdev->name,
354 hdev->manufacturer,
355 hdev->hci_ver, hdev->hci_rev);
356}
357
358static void hci_cc_read_local_commands(struct hci_dev *hdev, struct sk_buff *skb)
359{
360 struct hci_rp_read_local_commands *rp = (void *) skb->data;
361
362 BT_DBG("%s status 0x%x", hdev->name, rp->status);
363
364 if (rp->status)
365 return;
366
367 memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
368}
369
370static void hci_cc_read_local_features(struct hci_dev *hdev, struct sk_buff *skb)
371{
372 struct hci_rp_read_local_features *rp = (void *) skb->data;
373
374 BT_DBG("%s status 0x%x", hdev->name, rp->status);
375
376 if (rp->status)
377 return;
378
379 memcpy(hdev->features, rp->features, 8);
380
381 /* Adjust default settings according to features
382 * supported by device. */
383
384 if (hdev->features[0] & LMP_3SLOT)
385 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
386
387 if (hdev->features[0] & LMP_5SLOT)
388 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
389
390 if (hdev->features[1] & LMP_HV2) {
391 hdev->pkt_type |= (HCI_HV2);
392 hdev->esco_type |= (ESCO_HV2);
393 }
394
395 if (hdev->features[1] & LMP_HV3) {
396 hdev->pkt_type |= (HCI_HV3);
397 hdev->esco_type |= (ESCO_HV3);
398 }
399
400 if (hdev->features[3] & LMP_ESCO)
401 hdev->esco_type |= (ESCO_EV3);
402
403 if (hdev->features[4] & LMP_EV4)
404 hdev->esco_type |= (ESCO_EV4);
405
406 if (hdev->features[4] & LMP_EV5)
407 hdev->esco_type |= (ESCO_EV5);
408
409 BT_DBG("%s features 0x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x", hdev->name,
410 hdev->features[0], hdev->features[1],
411 hdev->features[2], hdev->features[3],
412 hdev->features[4], hdev->features[5],
413 hdev->features[6], hdev->features[7]);
414}
415
416static void hci_cc_read_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
417{
418 struct hci_rp_read_buffer_size *rp = (void *) skb->data;
419
420 BT_DBG("%s status 0x%x", hdev->name, rp->status);
421
422 if (rp->status)
423 return;
424
425 hdev->acl_mtu = __le16_to_cpu(rp->acl_mtu);
426 hdev->sco_mtu = rp->sco_mtu;
427 hdev->acl_pkts = __le16_to_cpu(rp->acl_max_pkt);
428 hdev->sco_pkts = __le16_to_cpu(rp->sco_max_pkt);
429
430 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
431 hdev->sco_mtu = 64;
432 hdev->sco_pkts = 8;
433 }
434
435 hdev->acl_cnt = hdev->acl_pkts;
436 hdev->sco_cnt = hdev->sco_pkts;
437
438 BT_DBG("%s acl mtu %d:%d sco mtu %d:%d", hdev->name,
439 hdev->acl_mtu, hdev->acl_pkts,
440 hdev->sco_mtu, hdev->sco_pkts);
441}
442
443static void hci_cc_read_bd_addr(struct hci_dev *hdev, struct sk_buff *skb)
444{
445 struct hci_rp_read_bd_addr *rp = (void *) skb->data;
446
447 BT_DBG("%s status 0x%x", hdev->name, rp->status);
448
449 if (!rp->status)
450 bacpy(&hdev->bdaddr, &rp->bdaddr);
451
452 hci_req_complete(hdev, rp->status);
453}
454
455static inline void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
456{
457 BT_DBG("%s status 0x%x", hdev->name, status);
458
459 if (status) {
460 hci_req_complete(hdev, status);
461
462 hci_conn_check_pending(hdev);
463 } else
464 set_bit(HCI_INQUIRY, &hdev->flags);
465}
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467static inline void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
468{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200469 struct hci_cp_create_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200472 BT_DBG("%s status 0x%x", hdev->name, status);
473
474 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_CONN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (!cp)
476 return;
477
478 hci_dev_lock(hdev);
479
480 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
481
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200482 BT_DBG("%s bdaddr %s conn %p", hdev->name, batostr(&cp->bdaddr), conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 if (status) {
485 if (conn && conn->state == BT_CONNECT) {
Marcel Holtmann4c67bc72006-10-15 17:30:56 +0200486 if (status != 0x0c || conn->attempt > 2) {
487 conn->state = BT_CLOSED;
488 hci_proto_connect_cfm(conn, status);
489 hci_conn_del(conn);
490 } else
491 conn->state = BT_CONNECT2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493 } else {
494 if (!conn) {
495 conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr);
496 if (conn) {
497 conn->out = 1;
498 conn->link_mode |= HCI_LM_MASTER;
499 } else
500 BT_ERR("No memmory for new connection");
501 }
502 }
503
504 hci_dev_unlock(hdev);
505}
506
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200507static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200509 struct hci_cp_add_sco *cp;
510 struct hci_conn *acl, *sco;
511 __u16 handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200513 BT_DBG("%s status 0x%x", hdev->name, status);
514
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200515 if (!status)
516 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200518 cp = hci_sent_cmd_data(hdev, HCI_OP_ADD_SCO);
519 if (!cp)
520 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200522 handle = __le16_to_cpu(cp->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200524 BT_DBG("%s handle %d", hdev->name, handle);
Marcel Holtmann6bd57412006-11-18 22:14:22 +0100525
526 hci_dev_lock(hdev);
527
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200528 acl = hci_conn_hash_lookup_handle(hdev, handle);
529 if (acl && (sco = acl->link)) {
530 sco->state = BT_CLOSED;
531
532 hci_proto_connect_cfm(sco, status);
533 hci_conn_del(sco);
534 }
Marcel Holtmann6bd57412006-11-18 22:14:22 +0100535
536 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200539static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
540{
541 BT_DBG("%s status 0x%x", hdev->name, status);
542}
543
544static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
545{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200546 struct hci_cp_setup_sync_conn *cp;
547 struct hci_conn *acl, *sco;
548 __u16 handle;
549
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200550 BT_DBG("%s status 0x%x", hdev->name, status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200551
552 if (!status)
553 return;
554
555 cp = hci_sent_cmd_data(hdev, HCI_OP_SETUP_SYNC_CONN);
556 if (!cp)
557 return;
558
559 handle = __le16_to_cpu(cp->handle);
560
561 BT_DBG("%s handle %d", hdev->name, handle);
562
563 hci_dev_lock(hdev);
564
565 acl = hci_conn_hash_lookup_handle(hdev, handle);
566 if (acl && (sco = acl->link)) {
567 sco->state = BT_CLOSED;
568
569 hci_proto_connect_cfm(sco, status);
570 hci_conn_del(sco);
571 }
572
573 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200574}
575
576static void hci_cs_sniff_mode(struct hci_dev *hdev, __u8 status)
577{
578 struct hci_cp_sniff_mode *cp;
579 struct hci_conn *conn;
580
581 BT_DBG("%s status 0x%x", hdev->name, status);
582
583 if (!status)
584 return;
585
586 cp = hci_sent_cmd_data(hdev, HCI_OP_SNIFF_MODE);
587 if (!cp)
588 return;
589
590 hci_dev_lock(hdev);
591
592 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
593 if (conn)
594 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend);
595
596 hci_dev_unlock(hdev);
597}
598
599static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
600{
601 struct hci_cp_exit_sniff_mode *cp;
602 struct hci_conn *conn;
603
604 BT_DBG("%s status 0x%x", hdev->name, status);
605
606 if (!status)
607 return;
608
609 cp = hci_sent_cmd_data(hdev, HCI_OP_EXIT_SNIFF_MODE);
610 if (!cp)
611 return;
612
613 hci_dev_lock(hdev);
614
615 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
616 if (conn)
617 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend);
618
619 hci_dev_unlock(hdev);
620}
621
622static inline void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
623{
624 __u8 status = *((__u8 *) skb->data);
625
626 BT_DBG("%s status %d", hdev->name, status);
627
628 clear_bit(HCI_INQUIRY, &hdev->flags);
629
630 hci_req_complete(hdev, status);
631
632 hci_conn_check_pending(hdev);
633}
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635static inline void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
636{
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -0700637 struct inquiry_data data;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200638 struct inquiry_info *info = (void *) (skb->data + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 int num_rsp = *((__u8 *) skb->data);
640
641 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
642
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -0700643 if (!num_rsp)
644 return;
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -0700647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 for (; num_rsp; num_rsp--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 bacpy(&data.bdaddr, &info->bdaddr);
650 data.pscan_rep_mode = info->pscan_rep_mode;
651 data.pscan_period_mode = info->pscan_period_mode;
652 data.pscan_mode = info->pscan_mode;
653 memcpy(data.dev_class, info->dev_class, 3);
654 data.clock_offset = info->clock_offset;
655 data.rssi = 0x00;
656 info++;
657 hci_inquiry_cache_update(hdev, &data);
658 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -0700659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 hci_dev_unlock(hdev);
661}
662
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200663static inline void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200665 struct hci_ev_conn_complete *ev = (void *) skb->data;
666 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200668 BT_DBG("%s", hdev->name);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -0700669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -0700671
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200672 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
673 if (!conn)
674 goto unlock;
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -0700675
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200676 if (!ev->status) {
677 conn->handle = __le16_to_cpu(ev->handle);
678 conn->state = BT_CONNECTED;
679
680 if (test_bit(HCI_AUTH, &hdev->flags))
681 conn->link_mode |= HCI_LM_AUTH;
682
683 if (test_bit(HCI_ENCRYPT, &hdev->flags))
684 conn->link_mode |= HCI_LM_ENCRYPT;
685
686 /* Get remote features */
687 if (conn->type == ACL_LINK) {
688 struct hci_cp_read_remote_features cp;
689 cp.handle = ev->handle;
690 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES, sizeof(cp), &cp);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -0700691 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -0700692
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200693 /* Set link policy */
694 if (conn->type == ACL_LINK && hdev->link_policy) {
695 struct hci_cp_write_link_policy cp;
696 cp.handle = ev->handle;
697 cp.policy = cpu_to_le16(hdev->link_policy);
698 hci_send_cmd(hdev, HCI_OP_WRITE_LINK_POLICY, sizeof(cp), &cp);
699 }
700
701 /* Set packet type for incoming connection */
702 if (!conn->out) {
703 struct hci_cp_change_conn_ptype cp;
704 cp.handle = ev->handle;
705 cp.pkt_type = (conn->type == ACL_LINK) ?
706 cpu_to_le16(hdev->pkt_type & ACL_PTYPE_MASK):
707 cpu_to_le16(hdev->pkt_type & SCO_PTYPE_MASK);
708
709 hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE, sizeof(cp), &cp);
710 } else {
711 /* Update disconnect timer */
712 hci_conn_hold(conn);
713 hci_conn_put(conn);
714 }
715 } else
716 conn->state = BT_CLOSED;
717
718 if (conn->type == ACL_LINK) {
719 struct hci_conn *sco = conn->link;
720 if (sco) {
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200721 if (!ev->status) {
722 if (lmp_esco_capable(hdev))
723 hci_setup_sync(sco, conn->handle);
724 else
725 hci_add_sco(sco, conn->handle);
726 } else {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200727 hci_proto_connect_cfm(sco, ev->status);
728 hci_conn_del(sco);
729 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -0700730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -0700732
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200733 hci_proto_connect_cfm(conn, ev->status);
734 if (ev->status)
735 hci_conn_del(conn);
736
737unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200739
740 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743static inline void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
744{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200745 struct hci_ev_conn_request *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 int mask = hdev->link_mode;
747
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200748 BT_DBG("%s bdaddr %s type 0x%x", hdev->name,
749 batostr(&ev->bdaddr), ev->link_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type);
752
753 if (mask & HCI_LM_ACCEPT) {
754 /* Connection accepted */
755 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 hci_dev_lock(hdev);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
760 if (!conn) {
761 if (!(conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr))) {
762 BT_ERR("No memmory for new connection");
763 hci_dev_unlock(hdev);
764 return;
765 }
766 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 memcpy(conn->dev_class, ev->dev_class, 3);
769 conn->state = BT_CONNECT;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 hci_dev_unlock(hdev);
772
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200773 if (ev->link_type == ACL_LINK || !lmp_esco_capable(hdev)) {
774 struct hci_cp_accept_conn_req cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200776 bacpy(&cp.bdaddr, &ev->bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200778 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
779 cp.role = 0x00; /* Become master */
780 else
781 cp.role = 0x01; /* Remain slave */
782
783 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ,
784 sizeof(cp), &cp);
785 } else {
786 struct hci_cp_accept_sync_conn_req cp;
787
788 bacpy(&cp.bdaddr, &ev->bdaddr);
789 cp.pkt_type = cpu_to_le16(hdev->esco_type);
790
791 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
792 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
793 cp.max_latency = cpu_to_le16(0xffff);
794 cp.content_format = cpu_to_le16(hdev->voice_setting);
795 cp.retrans_effort = 0xff;
796
797 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ,
798 sizeof(cp), &cp);
799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 } else {
801 /* Connection rejected */
802 struct hci_cp_reject_conn_req cp;
803
804 bacpy(&cp.bdaddr, &ev->bdaddr);
805 cp.reason = 0x0f;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200806 hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808}
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810static inline void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
811{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200812 struct hci_ev_disconn_complete *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200813 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 BT_DBG("%s status %d", hdev->name, ev->status);
816
817 if (ev->status)
818 return;
819
820 hci_dev_lock(hdev);
821
Marcel Holtmann04837f62006-07-03 10:02:33 +0200822 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (conn) {
824 conn->state = BT_CLOSED;
825 hci_proto_disconn_ind(conn, ev->reason);
826 hci_conn_del(conn);
827 }
828
829 hci_dev_unlock(hdev);
830}
831
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200832static inline void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
833{
834 struct hci_ev_auth_complete *ev = (void *) skb->data;
835 struct hci_conn *conn;
836
837 BT_DBG("%s status %d", hdev->name, ev->status);
838
839 hci_dev_lock(hdev);
840
841 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
842 if (conn) {
843 if (!ev->status)
844 conn->link_mode |= HCI_LM_AUTH;
845
846 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
847
848 hci_auth_cfm(conn, ev->status);
849
850 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
851 if (!ev->status) {
852 struct hci_cp_set_conn_encrypt cp;
853 cp.handle = cpu_to_le16(conn->handle);
854 cp.encrypt = 1;
855 hci_send_cmd(conn->hdev,
856 HCI_OP_SET_CONN_ENCRYPT, sizeof(cp), &cp);
857 } else {
858 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
859 hci_encrypt_cfm(conn, ev->status, 0x00);
860 }
861 }
862 }
863
864 hci_dev_unlock(hdev);
865}
866
867static inline void hci_remote_name_evt(struct hci_dev *hdev, struct sk_buff *skb)
868{
869 BT_DBG("%s", hdev->name);
870
871 hci_conn_check_pending(hdev);
872}
873
874static inline void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
875{
876 struct hci_ev_encrypt_change *ev = (void *) skb->data;
877 struct hci_conn *conn;
878
879 BT_DBG("%s status %d", hdev->name, ev->status);
880
881 hci_dev_lock(hdev);
882
883 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
884 if (conn) {
885 if (!ev->status) {
Marcel Holtmannae293192008-07-14 20:13:45 +0200886 if (ev->encrypt) {
887 /* Encryption implies authentication */
888 conn->link_mode |= HCI_LM_AUTH;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200889 conn->link_mode |= HCI_LM_ENCRYPT;
Marcel Holtmannae293192008-07-14 20:13:45 +0200890 } else
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200891 conn->link_mode &= ~HCI_LM_ENCRYPT;
892 }
893
894 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
895
896 hci_encrypt_cfm(conn, ev->status, ev->encrypt);
897 }
898
899 hci_dev_unlock(hdev);
900}
901
902static inline void hci_change_link_key_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
903{
904 struct hci_ev_change_link_key_complete *ev = (void *) skb->data;
905 struct hci_conn *conn;
906
907 BT_DBG("%s status %d", hdev->name, ev->status);
908
909 hci_dev_lock(hdev);
910
911 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
912 if (conn) {
913 if (!ev->status)
914 conn->link_mode |= HCI_LM_SECURE;
915
916 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
917
918 hci_key_change_cfm(conn, ev->status);
919 }
920
921 hci_dev_unlock(hdev);
922}
923
924static inline void hci_remote_features_evt(struct hci_dev *hdev, struct sk_buff *skb)
925{
926 struct hci_ev_remote_features *ev = (void *) skb->data;
927 struct hci_conn *conn;
928
929 BT_DBG("%s status %d", hdev->name, ev->status);
930
931 if (ev->status)
932 return;
933
934 hci_dev_lock(hdev);
935
936 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
937 if (conn)
938 memcpy(conn->features, ev->features, 8);
939
940 hci_dev_unlock(hdev);
941}
942
943static inline void hci_remote_version_evt(struct hci_dev *hdev, struct sk_buff *skb)
944{
945 BT_DBG("%s", hdev->name);
946}
947
948static inline void hci_qos_setup_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
949{
950 BT_DBG("%s", hdev->name);
951}
952
953static inline void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
954{
955 struct hci_ev_cmd_complete *ev = (void *) skb->data;
956 __u16 opcode;
957
958 skb_pull(skb, sizeof(*ev));
959
960 opcode = __le16_to_cpu(ev->opcode);
961
962 switch (opcode) {
963 case HCI_OP_INQUIRY_CANCEL:
964 hci_cc_inquiry_cancel(hdev, skb);
965 break;
966
967 case HCI_OP_EXIT_PERIODIC_INQ:
968 hci_cc_exit_periodic_inq(hdev, skb);
969 break;
970
971 case HCI_OP_REMOTE_NAME_REQ_CANCEL:
972 hci_cc_remote_name_req_cancel(hdev, skb);
973 break;
974
975 case HCI_OP_ROLE_DISCOVERY:
976 hci_cc_role_discovery(hdev, skb);
977 break;
978
979 case HCI_OP_WRITE_LINK_POLICY:
980 hci_cc_write_link_policy(hdev, skb);
981 break;
982
983 case HCI_OP_RESET:
984 hci_cc_reset(hdev, skb);
985 break;
986
987 case HCI_OP_WRITE_LOCAL_NAME:
988 hci_cc_write_local_name(hdev, skb);
989 break;
990
991 case HCI_OP_READ_LOCAL_NAME:
992 hci_cc_read_local_name(hdev, skb);
993 break;
994
995 case HCI_OP_WRITE_AUTH_ENABLE:
996 hci_cc_write_auth_enable(hdev, skb);
997 break;
998
999 case HCI_OP_WRITE_ENCRYPT_MODE:
1000 hci_cc_write_encrypt_mode(hdev, skb);
1001 break;
1002
1003 case HCI_OP_WRITE_SCAN_ENABLE:
1004 hci_cc_write_scan_enable(hdev, skb);
1005 break;
1006
1007 case HCI_OP_READ_CLASS_OF_DEV:
1008 hci_cc_read_class_of_dev(hdev, skb);
1009 break;
1010
1011 case HCI_OP_WRITE_CLASS_OF_DEV:
1012 hci_cc_write_class_of_dev(hdev, skb);
1013 break;
1014
1015 case HCI_OP_READ_VOICE_SETTING:
1016 hci_cc_read_voice_setting(hdev, skb);
1017 break;
1018
1019 case HCI_OP_WRITE_VOICE_SETTING:
1020 hci_cc_write_voice_setting(hdev, skb);
1021 break;
1022
1023 case HCI_OP_HOST_BUFFER_SIZE:
1024 hci_cc_host_buffer_size(hdev, skb);
1025 break;
1026
1027 case HCI_OP_READ_LOCAL_VERSION:
1028 hci_cc_read_local_version(hdev, skb);
1029 break;
1030
1031 case HCI_OP_READ_LOCAL_COMMANDS:
1032 hci_cc_read_local_commands(hdev, skb);
1033 break;
1034
1035 case HCI_OP_READ_LOCAL_FEATURES:
1036 hci_cc_read_local_features(hdev, skb);
1037 break;
1038
1039 case HCI_OP_READ_BUFFER_SIZE:
1040 hci_cc_read_buffer_size(hdev, skb);
1041 break;
1042
1043 case HCI_OP_READ_BD_ADDR:
1044 hci_cc_read_bd_addr(hdev, skb);
1045 break;
1046
1047 default:
1048 BT_DBG("%s opcode 0x%x", hdev->name, opcode);
1049 break;
1050 }
1051
1052 if (ev->ncmd) {
1053 atomic_set(&hdev->cmd_cnt, 1);
1054 if (!skb_queue_empty(&hdev->cmd_q))
1055 hci_sched_cmd(hdev);
1056 }
1057}
1058
1059static inline void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb)
1060{
1061 struct hci_ev_cmd_status *ev = (void *) skb->data;
1062 __u16 opcode;
1063
1064 skb_pull(skb, sizeof(*ev));
1065
1066 opcode = __le16_to_cpu(ev->opcode);
1067
1068 switch (opcode) {
1069 case HCI_OP_INQUIRY:
1070 hci_cs_inquiry(hdev, ev->status);
1071 break;
1072
1073 case HCI_OP_CREATE_CONN:
1074 hci_cs_create_conn(hdev, ev->status);
1075 break;
1076
1077 case HCI_OP_ADD_SCO:
1078 hci_cs_add_sco(hdev, ev->status);
1079 break;
1080
1081 case HCI_OP_REMOTE_NAME_REQ:
1082 hci_cs_remote_name_req(hdev, ev->status);
1083 break;
1084
1085 case HCI_OP_SETUP_SYNC_CONN:
1086 hci_cs_setup_sync_conn(hdev, ev->status);
1087 break;
1088
1089 case HCI_OP_SNIFF_MODE:
1090 hci_cs_sniff_mode(hdev, ev->status);
1091 break;
1092
1093 case HCI_OP_EXIT_SNIFF_MODE:
1094 hci_cs_exit_sniff_mode(hdev, ev->status);
1095 break;
1096
1097 default:
1098 BT_DBG("%s opcode 0x%x", hdev->name, opcode);
1099 break;
1100 }
1101
1102 if (ev->ncmd) {
1103 atomic_set(&hdev->cmd_cnt, 1);
1104 if (!skb_queue_empty(&hdev->cmd_q))
1105 hci_sched_cmd(hdev);
1106 }
1107}
1108
1109static inline void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
1110{
1111 struct hci_ev_role_change *ev = (void *) skb->data;
1112 struct hci_conn *conn;
1113
1114 BT_DBG("%s status %d", hdev->name, ev->status);
1115
1116 hci_dev_lock(hdev);
1117
1118 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
1119 if (conn) {
1120 if (!ev->status) {
1121 if (ev->role)
1122 conn->link_mode &= ~HCI_LM_MASTER;
1123 else
1124 conn->link_mode |= HCI_LM_MASTER;
1125 }
1126
1127 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->pend);
1128
1129 hci_role_switch_cfm(conn, ev->status, ev->role);
1130 }
1131
1132 hci_dev_unlock(hdev);
1133}
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135static inline void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
1136{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001137 struct hci_ev_num_comp_pkts *ev = (void *) skb->data;
Marcel Holtmann1ebb9252005-11-08 09:57:21 -08001138 __le16 *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 int i;
1140
1141 skb_pull(skb, sizeof(*ev));
1142
1143 BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
1144
1145 if (skb->len < ev->num_hndl * 4) {
1146 BT_DBG("%s bad parameters", hdev->name);
1147 return;
1148 }
1149
1150 tasklet_disable(&hdev->tx_task);
1151
Marcel Holtmann1ebb9252005-11-08 09:57:21 -08001152 for (i = 0, ptr = (__le16 *) skb->data; i < ev->num_hndl; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 struct hci_conn *conn;
1154 __u16 handle, count;
1155
Harvey Harrison83985312008-05-02 16:25:46 -07001156 handle = get_unaligned_le16(ptr++);
1157 count = get_unaligned_le16(ptr++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 conn = hci_conn_hash_lookup_handle(hdev, handle);
1160 if (conn) {
1161 conn->sent -= count;
1162
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02001163 if (conn->type == ACL_LINK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if ((hdev->acl_cnt += count) > hdev->acl_pkts)
1165 hdev->acl_cnt = hdev->acl_pkts;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02001166 } else {
1167 if ((hdev->sco_cnt += count) > hdev->sco_pkts)
1168 hdev->sco_cnt = hdev->sco_pkts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
1170 }
1171 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 hci_sched_tx(hdev);
1174
1175 tasklet_enable(&hdev->tx_task);
1176}
1177
Marcel Holtmann04837f62006-07-03 10:02:33 +02001178static inline void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001180 struct hci_ev_mode_change *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02001181 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 BT_DBG("%s status %d", hdev->name, ev->status);
1184
1185 hci_dev_lock(hdev);
1186
Marcel Holtmann04837f62006-07-03 10:02:33 +02001187 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1188 if (conn) {
1189 conn->mode = ev->mode;
1190 conn->interval = __le16_to_cpu(ev->interval);
1191
1192 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
1193 if (conn->mode == HCI_CM_ACTIVE)
1194 conn->power_save = 1;
1195 else
1196 conn->power_save = 0;
1197 }
1198 }
1199
1200 hci_dev_unlock(hdev);
1201}
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203static inline void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
1204{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001205 BT_DBG("%s", hdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208static inline void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
1209{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001210 BT_DBG("%s", hdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213static inline void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
1214{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001215 BT_DBG("%s", hdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
Marcel Holtmann04837f62006-07-03 10:02:33 +02001218static inline void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
1219{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001220 struct hci_ev_clock_offset *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02001221 struct hci_conn *conn;
1222
1223 BT_DBG("%s status %d", hdev->name, ev->status);
1224
1225 hci_dev_lock(hdev);
1226
1227 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (conn && !ev->status) {
1229 struct inquiry_entry *ie;
1230
1231 if ((ie = hci_inquiry_cache_lookup(hdev, &conn->dst))) {
1232 ie->data.clock_offset = ev->clock_offset;
1233 ie->timestamp = jiffies;
1234 }
1235 }
1236
1237 hci_dev_unlock(hdev);
1238}
1239
Marcel Holtmann85a1e932005-08-09 20:28:02 -07001240static inline void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
1241{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001242 struct hci_ev_pscan_rep_mode *ev = (void *) skb->data;
Marcel Holtmann85a1e932005-08-09 20:28:02 -07001243 struct inquiry_entry *ie;
1244
1245 BT_DBG("%s", hdev->name);
1246
1247 hci_dev_lock(hdev);
1248
1249 if ((ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr))) {
1250 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
1251 ie->timestamp = jiffies;
1252 }
1253
1254 hci_dev_unlock(hdev);
1255}
1256
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001257static inline void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, struct sk_buff *skb)
1258{
1259 struct inquiry_data data;
1260 int num_rsp = *((__u8 *) skb->data);
1261
1262 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
1263
1264 if (!num_rsp)
1265 return;
1266
1267 hci_dev_lock(hdev);
1268
1269 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
1270 struct inquiry_info_with_rssi_and_pscan_mode *info = (void *) (skb->data + 1);
1271
1272 for (; num_rsp; num_rsp--) {
1273 bacpy(&data.bdaddr, &info->bdaddr);
1274 data.pscan_rep_mode = info->pscan_rep_mode;
1275 data.pscan_period_mode = info->pscan_period_mode;
1276 data.pscan_mode = info->pscan_mode;
1277 memcpy(data.dev_class, info->dev_class, 3);
1278 data.clock_offset = info->clock_offset;
1279 data.rssi = info->rssi;
1280 info++;
1281 hci_inquiry_cache_update(hdev, &data);
1282 }
1283 } else {
1284 struct inquiry_info_with_rssi *info = (void *) (skb->data + 1);
1285
1286 for (; num_rsp; num_rsp--) {
1287 bacpy(&data.bdaddr, &info->bdaddr);
1288 data.pscan_rep_mode = info->pscan_rep_mode;
1289 data.pscan_period_mode = info->pscan_period_mode;
1290 data.pscan_mode = 0x00;
1291 memcpy(data.dev_class, info->dev_class, 3);
1292 data.clock_offset = info->clock_offset;
1293 data.rssi = info->rssi;
1294 info++;
1295 hci_inquiry_cache_update(hdev, &data);
1296 }
1297 }
1298
1299 hci_dev_unlock(hdev);
1300}
1301
1302static inline void hci_remote_ext_features_evt(struct hci_dev *hdev, struct sk_buff *skb)
1303{
1304 BT_DBG("%s", hdev->name);
1305}
1306
1307static inline void hci_sync_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1308{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001309 struct hci_ev_sync_conn_complete *ev = (void *) skb->data;
1310 struct hci_conn *conn;
1311
1312 BT_DBG("%s status %d", hdev->name, ev->status);
1313
1314 hci_dev_lock(hdev);
1315
1316 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02001317 if (!conn) {
1318 if (ev->link_type == ESCO_LINK)
1319 goto unlock;
1320
1321 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
1322 if (!conn)
1323 goto unlock;
1324
1325 conn->type = SCO_LINK;
1326 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001327
1328 if (!ev->status) {
1329 conn->handle = __le16_to_cpu(ev->handle);
1330 conn->state = BT_CONNECTED;
1331 } else
1332 conn->state = BT_CLOSED;
1333
1334 hci_proto_connect_cfm(conn, ev->status);
1335 if (ev->status)
1336 hci_conn_del(conn);
1337
1338unlock:
1339 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001340}
1341
1342static inline void hci_sync_conn_changed_evt(struct hci_dev *hdev, struct sk_buff *skb)
1343{
1344 BT_DBG("%s", hdev->name);
1345}
1346
Marcel Holtmann04837f62006-07-03 10:02:33 +02001347static inline void hci_sniff_subrate_evt(struct hci_dev *hdev, struct sk_buff *skb)
1348{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001349 struct hci_ev_sniff_subrate *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02001350 struct hci_conn *conn;
1351
1352 BT_DBG("%s status %d", hdev->name, ev->status);
1353
1354 hci_dev_lock(hdev);
1355
1356 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1357 if (conn) {
1358 }
1359
1360 hci_dev_unlock(hdev);
1361}
1362
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001363static inline void hci_extended_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
1364{
1365 struct inquiry_data data;
1366 struct extended_inquiry_info *info = (void *) (skb->data + 1);
1367 int num_rsp = *((__u8 *) skb->data);
1368
1369 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
1370
1371 if (!num_rsp)
1372 return;
1373
1374 hci_dev_lock(hdev);
1375
1376 for (; num_rsp; num_rsp--) {
1377 bacpy(&data.bdaddr, &info->bdaddr);
1378 data.pscan_rep_mode = info->pscan_rep_mode;
1379 data.pscan_period_mode = info->pscan_period_mode;
1380 data.pscan_mode = 0x00;
1381 memcpy(data.dev_class, info->dev_class, 3);
1382 data.clock_offset = info->clock_offset;
1383 data.rssi = info->rssi;
1384 info++;
1385 hci_inquiry_cache_update(hdev, &data);
1386 }
1387
1388 hci_dev_unlock(hdev);
1389}
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
1392{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001393 struct hci_event_hdr *hdr = (void *) skb->data;
1394 __u8 event = hdr->evt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 skb_pull(skb, HCI_EVENT_HDR_SIZE);
1397
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001398 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 case HCI_EV_INQUIRY_COMPLETE:
1400 hci_inquiry_complete_evt(hdev, skb);
1401 break;
1402
1403 case HCI_EV_INQUIRY_RESULT:
1404 hci_inquiry_result_evt(hdev, skb);
1405 break;
1406
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001407 case HCI_EV_CONN_COMPLETE:
1408 hci_conn_complete_evt(hdev, skb);
Marcel Holtmann21d9e302005-09-13 01:32:25 +02001409 break;
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 case HCI_EV_CONN_REQUEST:
1412 hci_conn_request_evt(hdev, skb);
1413 break;
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 case HCI_EV_DISCONN_COMPLETE:
1416 hci_disconn_complete_evt(hdev, skb);
1417 break;
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 case HCI_EV_AUTH_COMPLETE:
1420 hci_auth_complete_evt(hdev, skb);
1421 break;
1422
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001423 case HCI_EV_REMOTE_NAME:
1424 hci_remote_name_evt(hdev, skb);
1425 break;
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 case HCI_EV_ENCRYPT_CHANGE:
1428 hci_encrypt_change_evt(hdev, skb);
1429 break;
1430
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001431 case HCI_EV_CHANGE_LINK_KEY_COMPLETE:
1432 hci_change_link_key_complete_evt(hdev, skb);
1433 break;
1434
1435 case HCI_EV_REMOTE_FEATURES:
1436 hci_remote_features_evt(hdev, skb);
1437 break;
1438
1439 case HCI_EV_REMOTE_VERSION:
1440 hci_remote_version_evt(hdev, skb);
1441 break;
1442
1443 case HCI_EV_QOS_SETUP_COMPLETE:
1444 hci_qos_setup_complete_evt(hdev, skb);
1445 break;
1446
1447 case HCI_EV_CMD_COMPLETE:
1448 hci_cmd_complete_evt(hdev, skb);
1449 break;
1450
1451 case HCI_EV_CMD_STATUS:
1452 hci_cmd_status_evt(hdev, skb);
1453 break;
1454
1455 case HCI_EV_ROLE_CHANGE:
1456 hci_role_change_evt(hdev, skb);
1457 break;
1458
1459 case HCI_EV_NUM_COMP_PKTS:
1460 hci_num_comp_pkts_evt(hdev, skb);
1461 break;
1462
1463 case HCI_EV_MODE_CHANGE:
1464 hci_mode_change_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 break;
1466
1467 case HCI_EV_PIN_CODE_REQ:
1468 hci_pin_code_request_evt(hdev, skb);
1469 break;
1470
1471 case HCI_EV_LINK_KEY_REQ:
1472 hci_link_key_request_evt(hdev, skb);
1473 break;
1474
1475 case HCI_EV_LINK_KEY_NOTIFY:
1476 hci_link_key_notify_evt(hdev, skb);
1477 break;
1478
1479 case HCI_EV_CLOCK_OFFSET:
1480 hci_clock_offset_evt(hdev, skb);
1481 break;
1482
Marcel Holtmann85a1e932005-08-09 20:28:02 -07001483 case HCI_EV_PSCAN_REP_MODE:
1484 hci_pscan_rep_mode_evt(hdev, skb);
1485 break;
1486
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001487 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
1488 hci_inquiry_result_with_rssi_evt(hdev, skb);
1489 break;
1490
1491 case HCI_EV_REMOTE_EXT_FEATURES:
1492 hci_remote_ext_features_evt(hdev, skb);
1493 break;
1494
1495 case HCI_EV_SYNC_CONN_COMPLETE:
1496 hci_sync_conn_complete_evt(hdev, skb);
1497 break;
1498
1499 case HCI_EV_SYNC_CONN_CHANGED:
1500 hci_sync_conn_changed_evt(hdev, skb);
1501 break;
1502
Marcel Holtmann04837f62006-07-03 10:02:33 +02001503 case HCI_EV_SNIFF_SUBRATE:
1504 hci_sniff_subrate_evt(hdev, skb);
1505 break;
1506
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001507 case HCI_EV_EXTENDED_INQUIRY_RESULT:
1508 hci_extended_inquiry_result_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 break;
1510
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001511 default:
1512 BT_DBG("%s event 0x%x", hdev->name, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 break;
1514 }
1515
1516 kfree_skb(skb);
1517 hdev->stat.evt_rx++;
1518}
1519
1520/* Generate internal stack event */
1521void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
1522{
1523 struct hci_event_hdr *hdr;
1524 struct hci_ev_stack_internal *ev;
1525 struct sk_buff *skb;
1526
1527 skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC);
1528 if (!skb)
1529 return;
1530
1531 hdr = (void *) skb_put(skb, HCI_EVENT_HDR_SIZE);
1532 hdr->evt = HCI_EV_STACK_INTERNAL;
1533 hdr->plen = sizeof(*ev) + dlen;
1534
1535 ev = (void *) skb_put(skb, sizeof(*ev) + dlen);
1536 ev->type = type;
1537 memcpy(ev->data, data, dlen);
1538
Marcel Holtmann576c7d82005-08-06 12:36:54 +02001539 bt_cb(skb)->incoming = 1;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001540 __net_timestamp(skb);
Marcel Holtmann576c7d82005-08-06 12:36:54 +02001541
Marcel Holtmann0d48d932005-08-09 20:30:28 -07001542 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 skb->dev = (void *) hdev;
1544 hci_send_to_sock(hdev, skb);
1545 kfree_skb(skb);
1546}