blob: 84e397575d2e099c4245ccfa073e7aa948c9c9b1 [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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003 Copyright (c) 2000-2001, 2010-2011, Code Aurora Forum. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
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>
Andrei Emeltchenko70f230202010-12-01 16:58:25 +020042#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/unaligned.h>
44
45#include <net/bluetooth/bluetooth.h>
46#include <net/bluetooth/hci_core.h>
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/* Handle HCI Event packets */
49
Marcel Holtmanna9de9242007-10-20 13:33:56 +020050static void hci_cc_inquiry_cancel(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Marcel Holtmanna9de9242007-10-20 13:33:56 +020052 __u8 status = *((__u8 *) skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Marcel Holtmanna9de9242007-10-20 13:33:56 +020054 BT_DBG("%s status 0x%x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Marcel Holtmanna9de9242007-10-20 13:33:56 +020056 if (status)
57 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070059 clear_bit(HCI_INQUIRY, &hdev->flags);
Marcel Holtmann6bd57412006-11-18 22:14:22 +010060
Johan Hedberg23bb5762010-12-21 23:01:27 +020061 hci_req_complete(hdev, HCI_OP_INQUIRY_CANCEL, status);
Marcel Holtmann6bd57412006-11-18 22:14:22 +010062
Marcel Holtmanna9de9242007-10-20 13:33:56 +020063 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
Marcel Holtmanna9de9242007-10-20 13:33:56 +020066static void hci_cc_exit_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Marcel Holtmanna9de9242007-10-20 13:33:56 +020068 __u8 status = *((__u8 *) skb->data);
69
70 BT_DBG("%s status 0x%x", hdev->name, status);
71
72 if (status)
73 return;
74
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070075 clear_bit(HCI_INQUIRY, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +020076
77 hci_conn_check_pending(hdev);
78}
79
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080static void hci_cc_link_key_reply(struct hci_dev *hdev, struct sk_buff *skb)
81{
82 struct hci_rp_link_key_reply *rp = (void *) skb->data;
83 struct hci_conn *conn;
84 struct hci_cp_link_key_reply *cp;
85
86 BT_DBG("%s status 0x%x", hdev->name, rp->status);
87 if (rp->status)
88 return;
89
90 cp = hci_sent_cmd_data(hdev, HCI_OP_LINK_KEY_REPLY);
91 if (!cp)
92 return;
93
94 hci_dev_lock(hdev);
95 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
96 if (conn) {
97 hci_conn_hold(conn);
98 memcpy(conn->link_key, cp->link_key, sizeof(conn->link_key));
99 conn->key_type = 5;
100 hci_conn_put(conn);
101 }
102 hci_dev_unlock(hdev);
103}
104
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200105static void hci_cc_remote_name_req_cancel(struct hci_dev *hdev, struct sk_buff *skb)
106{
107 BT_DBG("%s", hdev->name);
108}
109
110static void hci_cc_role_discovery(struct hci_dev *hdev, struct sk_buff *skb)
111{
112 struct hci_rp_role_discovery *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200115 BT_DBG("%s status 0x%x", hdev->name, rp->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200117 if (rp->status)
118 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200120 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200122 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
123 if (conn) {
124 if (rp->role)
125 conn->link_mode &= ~HCI_LM_MASTER;
126 else
127 conn->link_mode |= HCI_LM_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200129
130 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200133static void hci_cc_read_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
134{
135 struct hci_rp_read_link_policy *rp = (void *) skb->data;
136 struct hci_conn *conn;
137
138 BT_DBG("%s status 0x%x", hdev->name, rp->status);
139
140 if (rp->status)
141 return;
142
143 hci_dev_lock(hdev);
144
145 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
146 if (conn)
147 conn->link_policy = __le16_to_cpu(rp->policy);
148
149 hci_dev_unlock(hdev);
150}
151
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200152static void hci_cc_write_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200154 struct hci_rp_write_link_policy *rp = (void *) skb->data;
155 struct hci_conn *conn;
156 void *sent;
157
158 BT_DBG("%s status 0x%x", hdev->name, rp->status);
159
160 if (rp->status)
161 return;
162
163 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LINK_POLICY);
164 if (!sent)
165 return;
166
167 hci_dev_lock(hdev);
168
169 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200170 if (conn)
Harvey Harrison83985312008-05-02 16:25:46 -0700171 conn->link_policy = get_unaligned_le16(sent + 2);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200172
173 hci_dev_unlock(hdev);
174}
175
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200176static void hci_cc_read_def_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
177{
178 struct hci_rp_read_def_link_policy *rp = (void *) skb->data;
179
180 BT_DBG("%s status 0x%x", hdev->name, rp->status);
181
182 if (rp->status)
183 return;
184
185 hdev->link_policy = __le16_to_cpu(rp->policy);
186}
187
188static void hci_cc_write_def_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
189{
190 __u8 status = *((__u8 *) skb->data);
191 void *sent;
192
193 BT_DBG("%s status 0x%x", hdev->name, status);
194
195 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_LINK_POLICY);
196 if (!sent)
197 return;
198
199 if (!status)
200 hdev->link_policy = get_unaligned_le16(sent);
201
Johan Hedberg23bb5762010-12-21 23:01:27 +0200202 hci_req_complete(hdev, HCI_OP_WRITE_DEF_LINK_POLICY, status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200203}
204
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200205static void hci_cc_reset(struct hci_dev *hdev, struct sk_buff *skb)
206{
207 __u8 status = *((__u8 *) skb->data);
208
209 BT_DBG("%s status 0x%x", hdev->name, status);
210
Gustavo F. Padovan10572132011-03-16 15:36:29 -0300211 clear_bit(HCI_RESET, &hdev->flags);
212
Johan Hedberg23bb5762010-12-21 23:01:27 +0200213 hci_req_complete(hdev, HCI_OP_RESET, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200214}
215
216static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb)
217{
218 __u8 status = *((__u8 *) skb->data);
219 void *sent;
220
221 BT_DBG("%s status 0x%x", hdev->name, status);
222
223 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LOCAL_NAME);
224 if (!sent)
225 return;
226
Johan Hedbergb312b1612011-03-16 14:29:37 +0200227 if (test_bit(HCI_MGMT, &hdev->flags))
228 mgmt_set_local_name_complete(hdev->id, sent, status);
229
230 if (status)
231 return;
232
Johan Hedberg1f6c6372011-03-16 14:29:35 +0200233 memcpy(hdev->dev_name, sent, HCI_MAX_NAME_LENGTH);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200234}
235
236static void hci_cc_read_local_name(struct hci_dev *hdev, struct sk_buff *skb)
237{
238 struct hci_rp_read_local_name *rp = (void *) skb->data;
239
240 BT_DBG("%s status 0x%x", hdev->name, rp->status);
241
242 if (rp->status)
243 return;
244
Johan Hedberg1f6c6372011-03-16 14:29:35 +0200245 memcpy(hdev->dev_name, rp->name, HCI_MAX_NAME_LENGTH);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200246}
247
248static void hci_cc_write_auth_enable(struct hci_dev *hdev, struct sk_buff *skb)
249{
250 __u8 status = *((__u8 *) skb->data);
251 void *sent;
252
253 BT_DBG("%s status 0x%x", hdev->name, status);
254
255 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_ENABLE);
256 if (!sent)
257 return;
258
259 if (!status) {
260 __u8 param = *((__u8 *) sent);
261
262 if (param == AUTH_ENABLED)
263 set_bit(HCI_AUTH, &hdev->flags);
264 else
265 clear_bit(HCI_AUTH, &hdev->flags);
266 }
267
Johan Hedberg23bb5762010-12-21 23:01:27 +0200268 hci_req_complete(hdev, HCI_OP_WRITE_AUTH_ENABLE, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200269}
270
271static void hci_cc_write_encrypt_mode(struct hci_dev *hdev, struct sk_buff *skb)
272{
273 __u8 status = *((__u8 *) skb->data);
274 void *sent;
275
276 BT_DBG("%s status 0x%x", hdev->name, status);
277
278 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_ENCRYPT_MODE);
279 if (!sent)
280 return;
281
282 if (!status) {
283 __u8 param = *((__u8 *) sent);
284
285 if (param)
286 set_bit(HCI_ENCRYPT, &hdev->flags);
287 else
288 clear_bit(HCI_ENCRYPT, &hdev->flags);
289 }
290
Johan Hedberg23bb5762010-12-21 23:01:27 +0200291 hci_req_complete(hdev, HCI_OP_WRITE_ENCRYPT_MODE, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200292}
293
294static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
295{
296 __u8 status = *((__u8 *) skb->data);
297 void *sent;
298
299 BT_DBG("%s status 0x%x", hdev->name, status);
300
301 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SCAN_ENABLE);
302 if (!sent)
303 return;
304
305 if (!status) {
306 __u8 param = *((__u8 *) sent);
Johan Hedberg9fbcbb42010-12-30 00:18:33 +0200307 int old_pscan, old_iscan;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200308
Johan Hedberg9fbcbb42010-12-30 00:18:33 +0200309 old_pscan = test_and_clear_bit(HCI_PSCAN, &hdev->flags);
310 old_iscan = test_and_clear_bit(HCI_ISCAN, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200311
Johan Hedberg73f22f62010-12-29 16:00:25 +0200312 if (param & SCAN_INQUIRY) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200313 set_bit(HCI_ISCAN, &hdev->flags);
Johan Hedberg9fbcbb42010-12-30 00:18:33 +0200314 if (!old_iscan)
315 mgmt_discoverable(hdev->id, 1);
316 } else if (old_iscan)
Johan Hedberg73f22f62010-12-29 16:00:25 +0200317 mgmt_discoverable(hdev->id, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200318
Johan Hedberg9fbcbb42010-12-30 00:18:33 +0200319 if (param & SCAN_PAGE) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200320 set_bit(HCI_PSCAN, &hdev->flags);
Johan Hedberg9fbcbb42010-12-30 00:18:33 +0200321 if (!old_pscan)
322 mgmt_connectable(hdev->id, 1);
323 } else if (old_pscan)
324 mgmt_connectable(hdev->id, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200325 }
326
Johan Hedberg23bb5762010-12-21 23:01:27 +0200327 hci_req_complete(hdev, HCI_OP_WRITE_SCAN_ENABLE, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200328}
329
330static void hci_cc_read_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
331{
332 struct hci_rp_read_class_of_dev *rp = (void *) skb->data;
333
334 BT_DBG("%s status 0x%x", hdev->name, rp->status);
335
336 if (rp->status)
337 return;
338
339 memcpy(hdev->dev_class, rp->dev_class, 3);
340
341 BT_DBG("%s class 0x%.2x%.2x%.2x", hdev->name,
342 hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
343}
344
345static void hci_cc_write_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
346{
347 __u8 status = *((__u8 *) skb->data);
348 void *sent;
349
350 BT_DBG("%s status 0x%x", hdev->name, status);
351
Marcel Holtmannf383f272008-07-14 20:13:47 +0200352 if (status)
353 return;
354
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200355 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_CLASS_OF_DEV);
356 if (!sent)
357 return;
358
Marcel Holtmannf383f272008-07-14 20:13:47 +0200359 memcpy(hdev->dev_class, sent, 3);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200360}
361
362static void hci_cc_read_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
363{
364 struct hci_rp_read_voice_setting *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 __u16 setting;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200366
367 BT_DBG("%s status 0x%x", hdev->name, rp->status);
368
369 if (rp->status)
370 return;
371
372 setting = __le16_to_cpu(rp->voice_setting);
373
Marcel Holtmannf383f272008-07-14 20:13:47 +0200374 if (hdev->voice_setting == setting)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200375 return;
376
377 hdev->voice_setting = setting;
378
379 BT_DBG("%s voice setting 0x%04x", hdev->name, setting);
380
381 if (hdev->notify) {
382 tasklet_disable(&hdev->tx_task);
383 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
384 tasklet_enable(&hdev->tx_task);
385 }
386}
387
388static void hci_cc_write_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
389{
390 __u8 status = *((__u8 *) skb->data);
Marcel Holtmannf383f272008-07-14 20:13:47 +0200391 __u16 setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 void *sent;
393
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200394 BT_DBG("%s status 0x%x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Marcel Holtmannf383f272008-07-14 20:13:47 +0200396 if (status)
397 return;
398
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200399 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_VOICE_SETTING);
400 if (!sent)
401 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Marcel Holtmannf383f272008-07-14 20:13:47 +0200403 setting = get_unaligned_le16(sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Marcel Holtmannf383f272008-07-14 20:13:47 +0200405 if (hdev->voice_setting == setting)
406 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Marcel Holtmannf383f272008-07-14 20:13:47 +0200408 hdev->voice_setting = setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Marcel Holtmannf383f272008-07-14 20:13:47 +0200410 BT_DBG("%s voice setting 0x%04x", hdev->name, setting);
411
412 if (hdev->notify) {
413 tasklet_disable(&hdev->tx_task);
414 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
415 tasklet_enable(&hdev->tx_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417}
418
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200419static void hci_cc_host_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200421 __u8 status = *((__u8 *) skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200423 BT_DBG("%s status 0x%x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Johan Hedberg23bb5762010-12-21 23:01:27 +0200425 hci_req_complete(hdev, HCI_OP_HOST_BUFFER_SIZE, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
Marcel Holtmann333140b2008-07-14 20:13:48 +0200428static void hci_cc_read_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
429{
430 struct hci_rp_read_ssp_mode *rp = (void *) skb->data;
431
432 BT_DBG("%s status 0x%x", hdev->name, rp->status);
433
434 if (rp->status)
435 return;
436
437 hdev->ssp_mode = rp->mode;
438}
439
440static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
441{
442 __u8 status = *((__u8 *) skb->data);
443 void *sent;
444
445 BT_DBG("%s status 0x%x", hdev->name, status);
446
447 if (status)
448 return;
449
450 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_MODE);
451 if (!sent)
452 return;
453
454 hdev->ssp_mode = *((__u8 *) sent);
455}
456
Johan Hedbergd5859e22011-01-25 01:19:58 +0200457static u8 hci_get_inquiry_mode(struct hci_dev *hdev)
458{
459 if (hdev->features[6] & LMP_EXT_INQ)
460 return 2;
461
462 if (hdev->features[3] & LMP_RSSI_INQ)
463 return 1;
464
465 if (hdev->manufacturer == 11 && hdev->hci_rev == 0x00 &&
466 hdev->lmp_subver == 0x0757)
467 return 1;
468
469 if (hdev->manufacturer == 15) {
470 if (hdev->hci_rev == 0x03 && hdev->lmp_subver == 0x6963)
471 return 1;
472 if (hdev->hci_rev == 0x09 && hdev->lmp_subver == 0x6963)
473 return 1;
474 if (hdev->hci_rev == 0x00 && hdev->lmp_subver == 0x6965)
475 return 1;
476 }
477
478 if (hdev->manufacturer == 31 && hdev->hci_rev == 0x2005 &&
479 hdev->lmp_subver == 0x1805)
480 return 1;
481
482 return 0;
483}
484
485static void hci_setup_inquiry_mode(struct hci_dev *hdev)
486{
487 u8 mode;
488
489 mode = hci_get_inquiry_mode(hdev);
490
491 hci_send_cmd(hdev, HCI_OP_WRITE_INQUIRY_MODE, 1, &mode);
492}
493
494static void hci_setup_event_mask(struct hci_dev *hdev)
495{
496 /* The second byte is 0xff instead of 0x9f (two reserved bits
497 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
498 * command otherwise */
499 u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
500
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700501 /* Events for 1.2 and newer controllers */
502 if (hdev->lmp_ver > 1) {
503 events[4] |= 0x01; /* Flow Specification Complete */
504 events[4] |= 0x02; /* Inquiry Result with RSSI */
505 events[4] |= 0x04; /* Read Remote Extended Features Complete */
506 events[5] |= 0x08; /* Synchronous Connection Complete */
507 events[5] |= 0x10; /* Synchronous Connection Changed */
508 }
Johan Hedbergd5859e22011-01-25 01:19:58 +0200509
510 if (hdev->features[3] & LMP_RSSI_INQ)
511 events[4] |= 0x04; /* Inquiry Result with RSSI */
512
513 if (hdev->features[5] & LMP_SNIFF_SUBR)
514 events[5] |= 0x20; /* Sniff Subrating */
515
516 if (hdev->features[5] & LMP_PAUSE_ENC)
517 events[5] |= 0x80; /* Encryption Key Refresh Complete */
518
519 if (hdev->features[6] & LMP_EXT_INQ)
520 events[5] |= 0x40; /* Extended Inquiry Result */
521
522 if (hdev->features[6] & LMP_NO_FLUSH)
523 events[7] |= 0x01; /* Enhanced Flush Complete */
524
525 if (hdev->features[7] & LMP_LSTO)
526 events[6] |= 0x80; /* Link Supervision Timeout Changed */
527
528 if (hdev->features[6] & LMP_SIMPLE_PAIR) {
529 events[6] |= 0x01; /* IO Capability Request */
530 events[6] |= 0x02; /* IO Capability Response */
531 events[6] |= 0x04; /* User Confirmation Request */
532 events[6] |= 0x08; /* User Passkey Request */
533 events[6] |= 0x10; /* Remote OOB Data Request */
534 events[6] |= 0x20; /* Simple Pairing Complete */
535 events[7] |= 0x04; /* User Passkey Notification */
536 events[7] |= 0x08; /* Keypress Notification */
537 events[7] |= 0x10; /* Remote Host Supported
538 * Features Notification */
539 }
540
541 if (hdev->features[4] & LMP_LE)
542 events[7] |= 0x20; /* LE Meta-Event */
543
544 hci_send_cmd(hdev, HCI_OP_SET_EVENT_MASK, sizeof(events), events);
545}
546
547static void hci_setup(struct hci_dev *hdev)
548{
549 hci_setup_event_mask(hdev);
550
551 if (hdev->lmp_ver > 1)
552 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL);
553
554 if (hdev->features[6] & LMP_SIMPLE_PAIR) {
555 u8 mode = 0x01;
556 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, sizeof(mode), &mode);
557 }
558
559 if (hdev->features[3] & LMP_RSSI_INQ)
560 hci_setup_inquiry_mode(hdev);
561
562 if (hdev->features[7] & LMP_INQ_TX_PWR)
563 hci_send_cmd(hdev, HCI_OP_READ_INQ_RSP_TX_POWER, 0, NULL);
564}
565
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200566static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
567{
568 struct hci_rp_read_local_version *rp = (void *) skb->data;
569
570 BT_DBG("%s status 0x%x", hdev->name, rp->status);
571
572 if (rp->status)
573 return;
574
575 hdev->hci_ver = rp->hci_ver;
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200576 hdev->hci_rev = __le16_to_cpu(rp->hci_rev);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200577 hdev->lmp_ver = rp->lmp_ver;
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200578 hdev->manufacturer = __le16_to_cpu(rp->manufacturer);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200579 hdev->lmp_subver = __le16_to_cpu(rp->lmp_subver);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200580
581 BT_DBG("%s manufacturer %d hci ver %d:%d", hdev->name,
582 hdev->manufacturer,
583 hdev->hci_ver, hdev->hci_rev);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200584
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700585 if (hdev->dev_type == HCI_BREDR && test_bit(HCI_INIT, &hdev->flags))
Johan Hedbergd5859e22011-01-25 01:19:58 +0200586 hci_setup(hdev);
587}
588
589static void hci_setup_link_policy(struct hci_dev *hdev)
590{
591 u16 link_policy = 0;
592
593 if (hdev->features[0] & LMP_RSWITCH)
594 link_policy |= HCI_LP_RSWITCH;
595 if (hdev->features[0] & LMP_HOLD)
596 link_policy |= HCI_LP_HOLD;
597 if (hdev->features[0] & LMP_SNIFF)
598 link_policy |= HCI_LP_SNIFF;
599 if (hdev->features[1] & LMP_PARK)
600 link_policy |= HCI_LP_PARK;
601
602 link_policy = cpu_to_le16(link_policy);
603 hci_send_cmd(hdev, HCI_OP_WRITE_DEF_LINK_POLICY,
604 sizeof(link_policy), &link_policy);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200605}
606
607static void hci_cc_read_local_commands(struct hci_dev *hdev, struct sk_buff *skb)
608{
609 struct hci_rp_read_local_commands *rp = (void *) skb->data;
610
611 BT_DBG("%s status 0x%x", hdev->name, rp->status);
612
613 if (rp->status)
Johan Hedbergd5859e22011-01-25 01:19:58 +0200614 goto done;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200615
616 memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
Johan Hedbergd5859e22011-01-25 01:19:58 +0200617
618 if (test_bit(HCI_INIT, &hdev->flags) && (hdev->commands[5] & 0x10))
619 hci_setup_link_policy(hdev);
620
621done:
622 hci_req_complete(hdev, HCI_OP_READ_LOCAL_COMMANDS, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200623}
624
625static void hci_cc_read_local_features(struct hci_dev *hdev, struct sk_buff *skb)
626{
627 struct hci_rp_read_local_features *rp = (void *) skb->data;
628
629 BT_DBG("%s status 0x%x", hdev->name, rp->status);
630
631 if (rp->status)
632 return;
633
634 memcpy(hdev->features, rp->features, 8);
635
636 /* Adjust default settings according to features
637 * supported by device. */
638
639 if (hdev->features[0] & LMP_3SLOT)
640 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
641
642 if (hdev->features[0] & LMP_5SLOT)
643 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
644
645 if (hdev->features[1] & LMP_HV2) {
646 hdev->pkt_type |= (HCI_HV2);
647 hdev->esco_type |= (ESCO_HV2);
648 }
649
650 if (hdev->features[1] & LMP_HV3) {
651 hdev->pkt_type |= (HCI_HV3);
652 hdev->esco_type |= (ESCO_HV3);
653 }
654
655 if (hdev->features[3] & LMP_ESCO)
656 hdev->esco_type |= (ESCO_EV3);
657
658 if (hdev->features[4] & LMP_EV4)
659 hdev->esco_type |= (ESCO_EV4);
660
661 if (hdev->features[4] & LMP_EV5)
662 hdev->esco_type |= (ESCO_EV5);
663
Marcel Holtmannefc76882009-02-06 09:13:37 +0100664 if (hdev->features[5] & LMP_EDR_ESCO_2M)
665 hdev->esco_type |= (ESCO_2EV3);
666
667 if (hdev->features[5] & LMP_EDR_ESCO_3M)
668 hdev->esco_type |= (ESCO_3EV3);
669
670 if (hdev->features[5] & LMP_EDR_3S_ESCO)
671 hdev->esco_type |= (ESCO_2EV5 | ESCO_3EV5);
672
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200673 BT_DBG("%s features 0x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x", hdev->name,
674 hdev->features[0], hdev->features[1],
675 hdev->features[2], hdev->features[3],
676 hdev->features[4], hdev->features[5],
677 hdev->features[6], hdev->features[7]);
678}
679
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700680static void hci_cc_read_flow_control_mode(struct hci_dev *hdev,
681 struct sk_buff *skb)
Andre Guedesd5fa5132011-06-30 19:20:52 -0300682{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700683 struct hci_rp_read_flow_control_mode *rp = (void *) skb->data;
Andre Guedesd5fa5132011-06-30 19:20:52 -0300684
685 BT_DBG("%s status 0x%x", hdev->name, rp->status);
686
687 if (rp->status)
688 return;
689
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700690 hdev->flow_ctl_mode = rp->mode;
Andre Guedesd5fa5132011-06-30 19:20:52 -0300691}
692
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200693static void hci_cc_read_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
694{
695 struct hci_rp_read_buffer_size *rp = (void *) skb->data;
696
697 BT_DBG("%s status 0x%x", hdev->name, rp->status);
698
699 if (rp->status)
700 return;
701
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700702 if (hdev->flow_ctl_mode == HCI_PACKET_BASED_FLOW_CTL_MODE) {
703 hdev->acl_mtu = __le16_to_cpu(rp->acl_mtu);
704 hdev->sco_mtu = rp->sco_mtu;
705 hdev->acl_pkts = __le16_to_cpu(rp->acl_max_pkt);
706 hdev->sco_pkts = __le16_to_cpu(rp->sco_max_pkt);
707 hdev->acl_cnt = hdev->acl_pkts;
708 hdev->sco_cnt = hdev->sco_pkts;
709 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200710
711 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
712 hdev->sco_mtu = 64;
713 hdev->sco_pkts = 8;
714 }
715
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200716
717 BT_DBG("%s acl mtu %d:%d sco mtu %d:%d", hdev->name,
718 hdev->acl_mtu, hdev->acl_pkts,
719 hdev->sco_mtu, hdev->sco_pkts);
720}
721
722static void hci_cc_read_bd_addr(struct hci_dev *hdev, struct sk_buff *skb)
723{
724 struct hci_rp_read_bd_addr *rp = (void *) skb->data;
725
726 BT_DBG("%s status 0x%x", hdev->name, rp->status);
727
728 if (!rp->status)
729 bacpy(&hdev->bdaddr, &rp->bdaddr);
730
Johan Hedberg23bb5762010-12-21 23:01:27 +0200731 hci_req_complete(hdev, HCI_OP_READ_BD_ADDR, rp->status);
732}
733
734static void hci_cc_write_ca_timeout(struct hci_dev *hdev, struct sk_buff *skb)
735{
736 __u8 status = *((__u8 *) skb->data);
737
738 BT_DBG("%s status 0x%x", hdev->name, status);
739
740 hci_req_complete(hdev, HCI_OP_WRITE_CA_TIMEOUT, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200741}
742
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700743static void hci_cc_read_data_block_size(struct hci_dev *hdev,
744 struct sk_buff *skb)
745{
746 struct hci_rp_read_data_block_size *rp = (void *) skb->data;
747
748 BT_DBG("%s status 0x%x", hdev->name, rp->status);
749
750 if (rp->status)
751 return;
752
753 if (hdev->flow_ctl_mode == HCI_BLOCK_BASED_FLOW_CTL_MODE) {
754 hdev->acl_mtu = __le16_to_cpu(rp->max_acl_len);
755 hdev->sco_mtu = 0;
756 hdev->data_block_len = __le16_to_cpu(rp->data_block_len);
757 /* acl_pkts indicates the number of blocks */
758 hdev->acl_pkts = __le16_to_cpu(rp->num_blocks);
759 hdev->sco_pkts = 0;
760 hdev->acl_cnt = hdev->acl_pkts;
761 hdev->sco_cnt = 0;
762 }
763
764 BT_DBG("%s acl mtu %d:%d, data block len %d", hdev->name,
765 hdev->acl_mtu, hdev->acl_cnt, hdev->data_block_len);
766}
767
768static void hci_cc_read_local_amp_info(struct hci_dev *hdev,
769 struct sk_buff *skb)
770{
771 struct hci_rp_read_local_amp_info *rp = (void *) skb->data;
772
773 BT_DBG("%s status 0x%x", hdev->name, rp->status);
774
775 if (rp->status)
776 return;
777
778 hdev->amp_status = rp->amp_status;
779 hdev->amp_total_bw = __le32_to_cpu(rp->total_bw);
780 hdev->amp_max_bw = __le32_to_cpu(rp->max_bw);
781 hdev->amp_min_latency = __le32_to_cpu(rp->min_latency);
782 hdev->amp_max_pdu = __le32_to_cpu(rp->max_pdu);
783 hdev->amp_type = rp->amp_type;
784 hdev->amp_pal_cap = __le16_to_cpu(rp->pal_cap);
785 hdev->amp_assoc_size = __le16_to_cpu(rp->max_assoc_size);
786 hdev->amp_be_flush_to = __le32_to_cpu(rp->be_flush_to);
787 hdev->amp_max_flush_to = __le32_to_cpu(rp->max_flush_to);
788
789 hci_req_complete(hdev, HCI_OP_READ_LOCAL_AMP_INFO, rp->status);
790}
791
Johan Hedbergb0916ea2011-01-10 13:44:55 +0200792static void hci_cc_delete_stored_link_key(struct hci_dev *hdev,
793 struct sk_buff *skb)
794{
795 __u8 status = *((__u8 *) skb->data);
796
797 BT_DBG("%s status 0x%x", hdev->name, status);
798
799 hci_req_complete(hdev, HCI_OP_DELETE_STORED_LINK_KEY, status);
800}
801
Johan Hedbergd5859e22011-01-25 01:19:58 +0200802static void hci_cc_set_event_mask(struct hci_dev *hdev, struct sk_buff *skb)
803{
804 __u8 status = *((__u8 *) skb->data);
805
806 BT_DBG("%s status 0x%x", hdev->name, status);
807
808 hci_req_complete(hdev, HCI_OP_SET_EVENT_MASK, status);
809}
810
811static void hci_cc_write_inquiry_mode(struct hci_dev *hdev,
812 struct sk_buff *skb)
813{
814 __u8 status = *((__u8 *) skb->data);
815
816 BT_DBG("%s status 0x%x", hdev->name, status);
817
818 hci_req_complete(hdev, HCI_OP_WRITE_INQUIRY_MODE, status);
819}
820
821static void hci_cc_read_inq_rsp_tx_power(struct hci_dev *hdev,
822 struct sk_buff *skb)
823{
824 __u8 status = *((__u8 *) skb->data);
825
826 BT_DBG("%s status 0x%x", hdev->name, status);
827
828 hci_req_complete(hdev, HCI_OP_READ_INQ_RSP_TX_POWER, status);
829}
830
831static void hci_cc_set_event_flt(struct hci_dev *hdev, struct sk_buff *skb)
832{
833 __u8 status = *((__u8 *) skb->data);
834
835 BT_DBG("%s status 0x%x", hdev->name, status);
836
837 hci_req_complete(hdev, HCI_OP_SET_EVENT_FLT, status);
838}
839
Johan Hedberg980e1a52011-01-22 06:10:07 +0200840static void hci_cc_pin_code_reply(struct hci_dev *hdev, struct sk_buff *skb)
841{
842 struct hci_rp_pin_code_reply *rp = (void *) skb->data;
843 struct hci_cp_pin_code_reply *cp;
844 struct hci_conn *conn;
845
846 BT_DBG("%s status 0x%x", hdev->name, rp->status);
847
848 if (test_bit(HCI_MGMT, &hdev->flags))
849 mgmt_pin_code_reply_complete(hdev->id, &rp->bdaddr, rp->status);
850
851 if (rp->status != 0)
852 return;
853
854 cp = hci_sent_cmd_data(hdev, HCI_OP_PIN_CODE_REPLY);
855 if (!cp)
856 return;
857
858 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
859 if (conn)
860 conn->pin_length = cp->pin_len;
861}
862
863static void hci_cc_pin_code_neg_reply(struct hci_dev *hdev, struct sk_buff *skb)
864{
865 struct hci_rp_pin_code_neg_reply *rp = (void *) skb->data;
866
867 BT_DBG("%s status 0x%x", hdev->name, rp->status);
868
869 if (test_bit(HCI_MGMT, &hdev->flags))
870 mgmt_pin_code_neg_reply_complete(hdev->id, &rp->bdaddr,
871 rp->status);
872}
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300873static void hci_cc_le_read_buffer_size(struct hci_dev *hdev,
874 struct sk_buff *skb)
875{
876 struct hci_rp_le_read_buffer_size *rp = (void *) skb->data;
877
878 BT_DBG("%s status 0x%x", hdev->name, rp->status);
879
880 if (rp->status)
881 return;
882
883 hdev->le_mtu = __le16_to_cpu(rp->le_mtu);
884 hdev->le_pkts = rp->le_max_pkt;
885
886 hdev->le_cnt = hdev->le_pkts;
887
888 BT_DBG("%s le mtu %d:%d", hdev->name, hdev->le_mtu, hdev->le_pkts);
889
890 hci_req_complete(hdev, HCI_OP_LE_READ_BUFFER_SIZE, rp->status);
891}
Johan Hedberg980e1a52011-01-22 06:10:07 +0200892
Johan Hedberga5c29682011-02-19 12:05:57 -0300893static void hci_cc_user_confirm_reply(struct hci_dev *hdev, struct sk_buff *skb)
894{
895 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
896
897 BT_DBG("%s status 0x%x", hdev->name, rp->status);
898
899 if (test_bit(HCI_MGMT, &hdev->flags))
900 mgmt_user_confirm_reply_complete(hdev->id, &rp->bdaddr,
901 rp->status);
902}
903
904static void hci_cc_user_confirm_neg_reply(struct hci_dev *hdev,
905 struct sk_buff *skb)
906{
907 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
908
909 BT_DBG("%s status 0x%x", hdev->name, rp->status);
910
911 if (test_bit(HCI_MGMT, &hdev->flags))
912 mgmt_user_confirm_neg_reply_complete(hdev->id, &rp->bdaddr,
913 rp->status);
914}
915
Szymon Jancc35938b2011-03-22 13:12:21 +0100916static void hci_cc_read_local_oob_data_reply(struct hci_dev *hdev,
917 struct sk_buff *skb)
918{
919 struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
920
921 BT_DBG("%s status 0x%x", hdev->name, rp->status);
922
923 mgmt_read_local_oob_data_reply_complete(hdev->id, rp->hash,
924 rp->randomizer, rp->status);
925}
926
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -0300927static void hci_cc_le_ltk_reply(struct hci_dev *hdev, struct sk_buff *skb)
928{
929 struct hci_rp_le_ltk_reply *rp = (void *) skb->data;
930
931 BT_DBG("%s status 0x%x", hdev->name, rp->status);
932
933 if (rp->status)
934 return;
935
936 hci_req_complete(hdev, HCI_OP_LE_LTK_REPLY, rp->status);
937}
938
939static void hci_cc_le_ltk_neg_reply(struct hci_dev *hdev, struct sk_buff *skb)
940{
941 struct hci_rp_le_ltk_neg_reply *rp = (void *) skb->data;
942
943 BT_DBG("%s status 0x%x", hdev->name, rp->status);
944
945 if (rp->status)
946 return;
947
948 hci_req_complete(hdev, HCI_OP_LE_LTK_NEG_REPLY, rp->status);
949}
950
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700951static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
952 struct sk_buff *skb)
Andre Guedese326af42011-06-30 19:20:53 -0300953{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700954 void *sent;
955 __u8 param_scan_enable;
Andre Guedese326af42011-06-30 19:20:53 -0300956 __u8 status = *((__u8 *) skb->data);
957
Andre Guedese326af42011-06-30 19:20:53 -0300958 if (status)
959 return;
960
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700961 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_ENABLE);
962 if (!sent)
963 return;
964
965 param_scan_enable = *((__u8 *) sent);
966 if (param_scan_enable == 0x01) {
967 del_timer(&hdev->adv_timer);
968 hci_adv_entries_clear(hdev);
969 } else if (param_scan_enable == 0x00) {
970 mod_timer(&hdev->adv_timer, jiffies + ADV_CLEAR_TIMEOUT);
971 }
Andre Guedese326af42011-06-30 19:20:53 -0300972}
973
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200974static inline void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
975{
976 BT_DBG("%s status 0x%x", hdev->name, status);
977
978 if (status) {
Johan Hedberg23bb5762010-12-21 23:01:27 +0200979 hci_req_complete(hdev, HCI_OP_INQUIRY, status);
Johan Hedberg314b2382011-04-27 10:29:57 -0400980
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700981 hci_conn_check_pending(hdev);
982 } else
983 set_bit(HCI_INQUIRY, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200984}
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986static inline void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
987{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200988 struct hci_cp_create_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200991 BT_DBG("%s status 0x%x", hdev->name, status);
992
993 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_CONN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (!cp)
995 return;
996
997 hci_dev_lock(hdev);
998
999 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
1000
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001001 BT_DBG("%s bdaddr %s conn %p", hdev->name, batostr(&cp->bdaddr), conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 if (status) {
1004 if (conn && conn->state == BT_CONNECT) {
Marcel Holtmann4c67bc72006-10-15 17:30:56 +02001005 if (status != 0x0c || conn->attempt > 2) {
1006 conn->state = BT_CLOSED;
1007 hci_proto_connect_cfm(conn, status);
1008 hci_conn_del(conn);
1009 } else
1010 conn->state = BT_CONNECT2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012 } else {
1013 if (!conn) {
Nick Pellybbcda3b2010-02-11 11:54:28 -08001014 conn = hci_conn_add(hdev, ACL_LINK, 0, &cp->bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (conn) {
1016 conn->out = 1;
1017 conn->link_mode |= HCI_LM_MASTER;
1018 } else
Gustavo F. Padovan893ef972010-07-18 15:13:37 -03001019 BT_ERR("No memory for new connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021 }
1022
1023 hci_dev_unlock(hdev);
1024}
1025
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001026static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001028 struct hci_cp_add_sco *cp;
1029 struct hci_conn *acl, *sco;
1030 __u16 handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001032 BT_DBG("%s status 0x%x", hdev->name, status);
1033
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001034 if (!status)
1035 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001037 cp = hci_sent_cmd_data(hdev, HCI_OP_ADD_SCO);
1038 if (!cp)
1039 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001041 handle = __le16_to_cpu(cp->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001043 BT_DBG("%s handle %d", hdev->name, handle);
Marcel Holtmann6bd57412006-11-18 22:14:22 +01001044
1045 hci_dev_lock(hdev);
1046
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001047 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001048 if (acl) {
1049 sco = acl->link;
1050 if (sco) {
1051 sco->state = BT_CLOSED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001052
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001053 hci_proto_connect_cfm(sco, status);
1054 hci_conn_del(sco);
1055 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001056 }
Marcel Holtmann6bd57412006-11-18 22:14:22 +01001057
1058 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
Marcel Holtmannf8558552008-07-14 20:13:49 +02001061static void hci_cs_auth_requested(struct hci_dev *hdev, __u8 status)
1062{
1063 struct hci_cp_auth_requested *cp;
1064 struct hci_conn *conn;
1065
1066 BT_DBG("%s status 0x%x", hdev->name, status);
1067
1068 if (!status)
1069 return;
1070
1071 cp = hci_sent_cmd_data(hdev, HCI_OP_AUTH_REQUESTED);
1072 if (!cp)
1073 return;
1074
1075 hci_dev_lock(hdev);
1076
1077 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1078 if (conn) {
1079 if (conn->state == BT_CONFIG) {
1080 hci_proto_connect_cfm(conn, status);
1081 hci_conn_put(conn);
1082 }
1083 }
1084
1085 hci_dev_unlock(hdev);
1086}
1087
1088static void hci_cs_set_conn_encrypt(struct hci_dev *hdev, __u8 status)
1089{
1090 struct hci_cp_set_conn_encrypt *cp;
1091 struct hci_conn *conn;
1092
1093 BT_DBG("%s status 0x%x", hdev->name, status);
1094
1095 if (!status)
1096 return;
1097
1098 cp = hci_sent_cmd_data(hdev, HCI_OP_SET_CONN_ENCRYPT);
1099 if (!cp)
1100 return;
1101
1102 hci_dev_lock(hdev);
1103
1104 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1105 if (conn) {
1106 if (conn->state == BT_CONFIG) {
1107 hci_proto_connect_cfm(conn, status);
1108 hci_conn_put(conn);
1109 }
1110 }
1111
1112 hci_dev_unlock(hdev);
1113}
1114
Johan Hedberg127178d2010-11-18 22:22:29 +02001115static int hci_outgoing_auth_needed(struct hci_dev *hdev,
Szymon Janc138d22e2011-02-17 16:44:23 +01001116 struct hci_conn *conn)
Johan Hedberg392599b2010-11-18 22:22:28 +02001117{
Johan Hedberg392599b2010-11-18 22:22:28 +02001118 if (conn->state != BT_CONFIG || !conn->out)
1119 return 0;
1120
Johan Hedberg765c2a92011-01-19 12:06:52 +05301121 if (conn->pending_sec_level == BT_SECURITY_SDP)
Johan Hedberg392599b2010-11-18 22:22:28 +02001122 return 0;
1123
1124 /* Only request authentication for SSP connections or non-SSP
1125 * devices with sec_level HIGH */
1126 if (!(hdev->ssp_mode > 0 && conn->ssp_mode > 0) &&
Johan Hedberg765c2a92011-01-19 12:06:52 +05301127 conn->pending_sec_level != BT_SECURITY_HIGH)
Johan Hedberg392599b2010-11-18 22:22:28 +02001128 return 0;
1129
Johan Hedberg392599b2010-11-18 22:22:28 +02001130 return 1;
1131}
1132
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001133static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
1134{
Johan Hedberg127178d2010-11-18 22:22:29 +02001135 struct hci_cp_remote_name_req *cp;
1136 struct hci_conn *conn;
1137
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001138 BT_DBG("%s status 0x%x", hdev->name, status);
Johan Hedberg127178d2010-11-18 22:22:29 +02001139
1140 /* If successful wait for the name req complete event before
1141 * checking for the need to do authentication */
1142 if (!status)
1143 return;
1144
1145 cp = hci_sent_cmd_data(hdev, HCI_OP_REMOTE_NAME_REQ);
1146 if (!cp)
1147 return;
1148
1149 hci_dev_lock(hdev);
1150
1151 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001152 if (conn && hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02001153 struct hci_cp_auth_requested cp;
1154 cp.handle = __cpu_to_le16(conn->handle);
1155 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
1156 }
1157
1158 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001159}
1160
Marcel Holtmann769be972008-07-14 20:13:49 +02001161static void hci_cs_read_remote_features(struct hci_dev *hdev, __u8 status)
1162{
1163 struct hci_cp_read_remote_features *cp;
1164 struct hci_conn *conn;
1165
1166 BT_DBG("%s status 0x%x", hdev->name, status);
1167
1168 if (!status)
1169 return;
1170
1171 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_FEATURES);
1172 if (!cp)
1173 return;
1174
1175 hci_dev_lock(hdev);
1176
1177 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1178 if (conn) {
1179 if (conn->state == BT_CONFIG) {
Marcel Holtmann769be972008-07-14 20:13:49 +02001180 hci_proto_connect_cfm(conn, status);
1181 hci_conn_put(conn);
1182 }
1183 }
1184
1185 hci_dev_unlock(hdev);
1186}
1187
1188static void hci_cs_read_remote_ext_features(struct hci_dev *hdev, __u8 status)
1189{
1190 struct hci_cp_read_remote_ext_features *cp;
1191 struct hci_conn *conn;
1192
1193 BT_DBG("%s status 0x%x", hdev->name, status);
1194
1195 if (!status)
1196 return;
1197
1198 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES);
1199 if (!cp)
1200 return;
1201
1202 hci_dev_lock(hdev);
1203
1204 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1205 if (conn) {
1206 if (conn->state == BT_CONFIG) {
Marcel Holtmann769be972008-07-14 20:13:49 +02001207 hci_proto_connect_cfm(conn, status);
1208 hci_conn_put(conn);
1209 }
1210 }
1211
1212 hci_dev_unlock(hdev);
1213}
1214
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001215static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
1216{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001217 struct hci_cp_setup_sync_conn *cp;
1218 struct hci_conn *acl, *sco;
1219 __u16 handle;
1220
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001221 BT_DBG("%s status 0x%x", hdev->name, status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001222
1223 if (!status)
1224 return;
1225
1226 cp = hci_sent_cmd_data(hdev, HCI_OP_SETUP_SYNC_CONN);
1227 if (!cp)
1228 return;
1229
1230 handle = __le16_to_cpu(cp->handle);
1231
1232 BT_DBG("%s handle %d", hdev->name, handle);
1233
1234 hci_dev_lock(hdev);
1235
1236 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001237 if (acl) {
1238 sco = acl->link;
1239 if (sco) {
1240 sco->state = BT_CLOSED;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001241
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001242 hci_proto_connect_cfm(sco, status);
1243 hci_conn_del(sco);
1244 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001245 }
1246
1247 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001248}
1249
1250static void hci_cs_sniff_mode(struct hci_dev *hdev, __u8 status)
1251{
1252 struct hci_cp_sniff_mode *cp;
1253 struct hci_conn *conn;
1254
1255 BT_DBG("%s status 0x%x", hdev->name, status);
1256
1257 if (!status)
1258 return;
1259
1260 cp = hci_sent_cmd_data(hdev, HCI_OP_SNIFF_MODE);
1261 if (!cp)
1262 return;
1263
1264 hci_dev_lock(hdev);
1265
1266 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001267 if (conn) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001268 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend);
1269
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001270 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->pend))
1271 hci_sco_setup(conn, status);
1272 }
1273
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001274 hci_dev_unlock(hdev);
1275}
1276
1277static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
1278{
1279 struct hci_cp_exit_sniff_mode *cp;
1280 struct hci_conn *conn;
1281
1282 BT_DBG("%s status 0x%x", hdev->name, status);
1283
1284 if (!status)
1285 return;
1286
1287 cp = hci_sent_cmd_data(hdev, HCI_OP_EXIT_SNIFF_MODE);
1288 if (!cp)
1289 return;
1290
1291 hci_dev_lock(hdev);
1292
1293 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001294 if (conn) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001295 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend);
1296
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001297 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->pend))
1298 hci_sco_setup(conn, status);
1299 }
1300
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001301 hci_dev_unlock(hdev);
1302}
1303
Ville Tervofcd89c02011-02-10 22:38:47 -03001304static void hci_cs_le_create_conn(struct hci_dev *hdev, __u8 status)
1305{
1306 struct hci_cp_le_create_conn *cp;
1307 struct hci_conn *conn;
1308
1309 BT_DBG("%s status 0x%x", hdev->name, status);
1310
1311 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_CREATE_CONN);
1312 if (!cp)
1313 return;
1314
1315 hci_dev_lock(hdev);
1316
1317 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->peer_addr);
1318
1319 BT_DBG("%s bdaddr %s conn %p", hdev->name, batostr(&cp->peer_addr),
1320 conn);
1321
1322 if (status) {
1323 if (conn && conn->state == BT_CONNECT) {
1324 conn->state = BT_CLOSED;
1325 hci_proto_connect_cfm(conn, status);
1326 hci_conn_del(conn);
1327 }
1328 } else {
1329 if (!conn) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001330 conn = hci_le_conn_add(hdev, &cp->peer_addr,
1331 cp->peer_addr_type);
1332 if (conn)
Ville Tervofcd89c02011-02-10 22:38:47 -03001333 conn->out = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001334 else
Ville Tervofcd89c02011-02-10 22:38:47 -03001335 BT_ERR("No memory for new connection");
1336 }
1337 }
1338
1339 hci_dev_unlock(hdev);
1340}
1341
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001342static void hci_cs_accept_logical_link(struct hci_dev *hdev, __u8 status)
1343{
1344 struct hci_cp_create_logical_link *ap;
1345 struct hci_chan *chan;
1346
1347 BT_DBG("%s status 0x%x", hdev->name, status);
1348
1349 ap = hci_sent_cmd_data(hdev, HCI_OP_ACCEPT_LOGICAL_LINK);
1350 if (!ap)
1351 return;
1352
1353 hci_dev_lock(hdev);
1354
1355 chan = hci_chan_list_lookup_id(hdev, ap->phy_handle);
1356
1357 BT_DBG("%s chan %p", hdev->name, chan);
1358
1359 if (status) {
1360 if (chan && chan->state == BT_CONNECT) {
1361 chan->state = BT_CLOSED;
1362 hci_proto_create_cfm(chan, status);
1363 hci_chan_del(chan);
1364 }
1365 } else if (chan)
1366 chan->state = BT_CONNECT2;
1367
1368 hci_dev_unlock(hdev);
1369}
1370
1371static void hci_cs_create_logical_link(struct hci_dev *hdev, __u8 status)
1372{
1373 struct hci_cp_create_logical_link *cp;
1374 struct hci_chan *chan;
1375
1376 BT_DBG("%s status 0x%x", hdev->name, status);
1377
1378 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_LOGICAL_LINK);
1379 if (!cp)
1380 return;
1381
1382 hci_dev_lock(hdev);
1383
1384 chan = hci_chan_list_lookup_id(hdev, cp->phy_handle);
1385
1386 BT_DBG("%s chan %p", hdev->name, chan);
1387
1388 if (status) {
1389 if (chan && chan->state == BT_CONNECT) {
1390 chan->state = BT_CLOSED;
1391 hci_proto_create_cfm(chan, status);
1392 hci_chan_del(chan);
1393 }
1394 } else if (chan)
1395 chan->state = BT_CONNECT2;
1396
1397 hci_dev_unlock(hdev);
1398}
1399
1400static void hci_cs_flow_spec_modify(struct hci_dev *hdev, __u8 status)
1401{
1402 struct hci_cp_flow_spec_modify *cp;
1403 struct hci_chan *chan;
1404
1405 BT_DBG("%s status 0x%x", hdev->name, status);
1406
1407 cp = hci_sent_cmd_data(hdev, HCI_OP_FLOW_SPEC_MODIFY);
1408 if (!cp)
1409 return;
1410
1411 hci_dev_lock(hdev);
1412
1413 chan = hci_chan_list_lookup_handle(hdev, cp->log_handle);
1414 if (chan) {
1415 if (status)
1416 hci_proto_modify_cfm(chan, status);
1417 else {
1418 chan->tx_fs = cp->tx_fs;
1419 chan->rx_fs = cp->rx_fs;
1420 }
1421 }
1422
1423 hci_dev_unlock(hdev);
1424}
1425
1426static void hci_cs_disconn_logical_link(struct hci_dev *hdev, __u8 status)
1427{
1428 struct hci_cp_disconn_logical_link *cp;
1429 struct hci_chan *chan;
1430
1431 if (!status)
1432 return;
1433
1434 BT_DBG("%s status 0x%x", hdev->name, status);
1435
1436 cp = hci_sent_cmd_data(hdev, HCI_OP_DISCONN_LOGICAL_LINK);
1437 if (!cp)
1438 return;
1439
1440 hci_dev_lock(hdev);
1441
1442 chan = hci_chan_list_lookup_handle(hdev, cp->log_handle);
1443 if (chan)
1444 hci_chan_del(chan);
1445
1446 hci_dev_unlock(hdev);
1447}
1448
1449static void hci_cs_disconn_physical_link(struct hci_dev *hdev, __u8 status)
1450{
1451 struct hci_cp_disconn_phys_link *cp;
1452 struct hci_conn *conn;
1453
1454 if (!status)
1455 return;
1456
1457 BT_DBG("%s status 0x%x", hdev->name, status);
1458
1459 cp = hci_sent_cmd_data(hdev, HCI_OP_DISCONN_PHYS_LINK);
1460 if (!cp)
1461 return;
1462
1463 hci_dev_lock(hdev);
1464
1465 conn = hci_conn_hash_lookup_handle(hdev, cp->phy_handle);
1466 if (conn) {
1467 conn->state = BT_CLOSED;
1468 hci_conn_del(conn);
1469 }
1470
1471 hci_dev_unlock(hdev);
1472}
1473
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03001474static void hci_cs_le_start_enc(struct hci_dev *hdev, u8 status)
1475{
1476 BT_DBG("%s status 0x%x", hdev->name, status);
1477}
1478
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001479static inline void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1480{
1481 __u8 status = *((__u8 *) skb->data);
1482
1483 BT_DBG("%s status %d", hdev->name, status);
1484
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001485 clear_bit(HCI_INQUIRY, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001486
Johan Hedberg23bb5762010-12-21 23:01:27 +02001487 hci_req_complete(hdev, HCI_OP_INQUIRY, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001488
1489 hci_conn_check_pending(hdev);
1490}
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492static inline void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
1493{
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001494 struct inquiry_data data;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001495 struct inquiry_info *info = (void *) (skb->data + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 int num_rsp = *((__u8 *) skb->data);
1497
1498 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
1499
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001500 if (!num_rsp)
1501 return;
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001504
Johan Hedberge17acd42011-03-30 23:57:16 +03001505 for (; num_rsp; num_rsp--, info++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 bacpy(&data.bdaddr, &info->bdaddr);
1507 data.pscan_rep_mode = info->pscan_rep_mode;
1508 data.pscan_period_mode = info->pscan_period_mode;
1509 data.pscan_mode = info->pscan_mode;
1510 memcpy(data.dev_class, info->dev_class, 3);
1511 data.clock_offset = info->clock_offset;
1512 data.rssi = 0x00;
Marcel Holtmann41a96212008-07-14 20:13:48 +02001513 data.ssp_mode = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 hci_inquiry_cache_update(hdev, &data);
Johan Hedberge17acd42011-03-30 23:57:16 +03001515 mgmt_device_found(hdev->id, &info->bdaddr, info->dev_class, 0,
1516 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 hci_dev_unlock(hdev);
1520}
1521
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001522static inline void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001524 struct hci_ev_conn_complete *ev = (void *) skb->data;
1525 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001527 BT_DBG("%s", hdev->name);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001530
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001531 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann94992372009-04-19 19:30:03 +02001532 if (!conn) {
1533 if (ev->link_type != SCO_LINK)
1534 goto unlock;
1535
1536 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
1537 if (!conn)
1538 goto unlock;
1539
1540 conn->type = SCO_LINK;
1541 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001542
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001543 if (!ev->status) {
1544 conn->handle = __le16_to_cpu(ev->handle);
Marcel Holtmann769be972008-07-14 20:13:49 +02001545
1546 if (conn->type == ACL_LINK) {
1547 conn->state = BT_CONFIG;
1548 hci_conn_hold(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02001549 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Johan Hedbergf7520542011-01-20 12:34:39 +02001550 mgmt_connected(hdev->id, &ev->bdaddr);
Marcel Holtmann769be972008-07-14 20:13:49 +02001551 } else
1552 conn->state = BT_CONNECTED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001553
Marcel Holtmann9eba32b2009-08-22 14:19:26 -07001554 hci_conn_hold_device(conn);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02001555 hci_conn_add_sysfs(conn);
1556
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001557 if (test_bit(HCI_AUTH, &hdev->flags))
1558 conn->link_mode |= HCI_LM_AUTH;
1559
1560 if (test_bit(HCI_ENCRYPT, &hdev->flags))
1561 conn->link_mode |= HCI_LM_ENCRYPT;
1562
1563 /* Get remote features */
1564 if (conn->type == ACL_LINK) {
1565 struct hci_cp_read_remote_features cp;
1566 cp.handle = ev->handle;
Marcel Holtmann769be972008-07-14 20:13:49 +02001567 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES,
1568 sizeof(cp), &cp);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001569 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001570
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001571 /* Set packet type for incoming connection */
Marcel Holtmanna8746412008-07-14 20:13:46 +02001572 if (!conn->out && hdev->hci_ver < 3) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001573 struct hci_cp_change_conn_ptype cp;
1574 cp.handle = ev->handle;
Marcel Holtmanna8746412008-07-14 20:13:46 +02001575 cp.pkt_type = cpu_to_le16(conn->pkt_type);
1576 hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE,
1577 sizeof(cp), &cp);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001578 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02001579 } else {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001580 conn->state = BT_CLOSED;
Johan Hedberg17d5c042011-01-22 06:09:08 +02001581 if (conn->type == ACL_LINK)
1582 mgmt_connect_failed(hdev->id, &ev->bdaddr, ev->status);
1583 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001584
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001585 if (conn->type == ACL_LINK)
1586 hci_sco_setup(conn, ev->status);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001587
Marcel Holtmann769be972008-07-14 20:13:49 +02001588 if (ev->status) {
1589 hci_proto_connect_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001590 hci_conn_del(conn);
Marcel Holtmannc89b6e62009-01-15 21:57:03 +01001591 } else if (ev->link_type != ACL_LINK)
1592 hci_proto_connect_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001593
1594unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001596
1597 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
1599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600static inline void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
1601{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001602 struct hci_ev_conn_request *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 int mask = hdev->link_mode;
1604
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001605 BT_DBG("%s bdaddr %s type 0x%x", hdev->name,
1606 batostr(&ev->bdaddr), ev->link_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
1608 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type);
1609
Szymon Janc138d22e2011-02-17 16:44:23 +01001610 if ((mask & HCI_LM_ACCEPT) &&
1611 !hci_blacklist_lookup(hdev, &ev->bdaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 /* Connection accepted */
Marcel Holtmannc7bdd502008-07-14 20:13:47 +02001613 struct inquiry_entry *ie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 hci_dev_lock(hdev);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001617
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02001618 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
1619 if (ie)
Marcel Holtmannc7bdd502008-07-14 20:13:47 +02001620 memcpy(ie->data.dev_class, ev->dev_class, 3);
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
1623 if (!conn) {
Nick Pellybbcda3b2010-02-11 11:54:28 -08001624 /* pkt_type not yet used for incoming connections */
1625 conn = hci_conn_add(hdev, ev->link_type, 0, &ev->bdaddr);
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02001626 if (!conn) {
Gustavo F. Padovan893ef972010-07-18 15:13:37 -03001627 BT_ERR("No memory for new connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 hci_dev_unlock(hdev);
1629 return;
1630 }
1631 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 memcpy(conn->dev_class, ev->dev_class, 3);
1634 conn->state = BT_CONNECT;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 hci_dev_unlock(hdev);
1637
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001638 if (ev->link_type == ACL_LINK || !lmp_esco_capable(hdev)) {
1639 struct hci_cp_accept_conn_req cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001641 bacpy(&cp.bdaddr, &ev->bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001643 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
1644 cp.role = 0x00; /* Become master */
1645 else
1646 cp.role = 0x01; /* Remain slave */
1647
1648 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ,
1649 sizeof(cp), &cp);
1650 } else {
1651 struct hci_cp_accept_sync_conn_req cp;
1652
1653 bacpy(&cp.bdaddr, &ev->bdaddr);
Marcel Holtmanna8746412008-07-14 20:13:46 +02001654 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001655
1656 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
1657 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001658 cp.max_latency = cpu_to_le16(0x000A);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001659 cp.content_format = cpu_to_le16(hdev->voice_setting);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001660 cp.retrans_effort = 0x01;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001661
1662 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ,
1663 sizeof(cp), &cp);
1664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 } else {
1666 /* Connection rejected */
1667 struct hci_cp_reject_conn_req cp;
1668
1669 bacpy(&cp.bdaddr, &ev->bdaddr);
1670 cp.reason = 0x0f;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001671 hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673}
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675static inline void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1676{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001677 struct hci_ev_disconn_complete *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02001678 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 BT_DBG("%s status %d", hdev->name, ev->status);
1681
Johan Hedberg8962ee72011-01-20 12:40:27 +02001682 if (ev->status) {
1683 mgmt_disconnect_failed(hdev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 return;
Johan Hedberg8962ee72011-01-20 12:40:27 +02001685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 hci_dev_lock(hdev);
1688
Marcel Holtmann04837f62006-07-03 10:02:33 +02001689 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergf7520542011-01-20 12:34:39 +02001690 if (!conn)
1691 goto unlock;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02001692
Johan Hedbergf7520542011-01-20 12:34:39 +02001693 conn->state = BT_CLOSED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001695 if (conn->type == ACL_LINK)
Johan Hedbergf7520542011-01-20 12:34:39 +02001696 mgmt_disconnected(hdev->id, &conn->dst);
1697
1698 hci_proto_disconn_cfm(conn, ev->reason);
1699 hci_conn_del(conn);
1700
1701unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 hci_dev_unlock(hdev);
1703}
1704
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001705static inline void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1706{
1707 struct hci_ev_auth_complete *ev = (void *) skb->data;
1708 struct hci_conn *conn;
1709
1710 BT_DBG("%s status %d", hdev->name, ev->status);
1711
1712 hci_dev_lock(hdev);
1713
1714 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001715 if (conn) {
1716 if (ev->status == 0x06 && hdev->ssp_mode > 0 &&
1717 conn->ssp_mode > 0) {
1718 struct hci_cp_auth_requested cp;
1719 cp.handle = cpu_to_le16(conn->handle);
1720 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
1721 sizeof(cp), &cp);
1722 hci_dev_unlock(hdev);
1723 BT_INFO("Pin or key missing");
1724 return;
1725 }
Waldemar Rymarkiewicz54444292011-05-31 15:49:26 +02001726
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001727 if (!ev->status) {
Waldemar Rymarkiewicz54444292011-05-31 15:49:26 +02001728 conn->link_mode |= HCI_LM_AUTH;
1729 conn->sec_level = conn->pending_sec_level;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02001730 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001731 mgmt_auth_failed(hdev->id, &conn->dst, ev->status);
1732 conn->sec_level = BT_SECURITY_LOW;
1733 }
1734
1735 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
1736
1737 if (conn->state == BT_CONFIG) {
1738 if (!ev->status && hdev->ssp_mode > 0 &&
1739 conn->ssp_mode > 0) {
1740 struct hci_cp_set_conn_encrypt cp;
1741 cp.handle = ev->handle;
1742 cp.encrypt = 0x01;
1743 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT,
1744 sizeof(cp), &cp);
1745 } else {
1746 conn->state = BT_CONNECTED;
1747 hci_proto_connect_cfm(conn, ev->status);
1748 hci_conn_put(conn);
1749 }
1750 } else {
1751 hci_auth_cfm(conn, ev->status);
1752
1753 hci_conn_hold(conn);
1754 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02001755 hci_conn_put(conn);
1756 }
1757
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001758 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
1759 if (!ev->status) {
1760 struct hci_cp_set_conn_encrypt cp;
1761 cp.handle = ev->handle;
1762 cp.encrypt = 0x01;
1763 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT,
1764 sizeof(cp), &cp);
1765 } else {
1766 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
1767 hci_encrypt_cfm(conn, ev->status, 0x00);
1768 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001769 }
1770 }
1771
1772 hci_dev_unlock(hdev);
1773}
1774
1775static inline void hci_remote_name_evt(struct hci_dev *hdev, struct sk_buff *skb)
1776{
Johan Hedberg127178d2010-11-18 22:22:29 +02001777 struct hci_ev_remote_name *ev = (void *) skb->data;
1778 struct hci_conn *conn;
1779
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001780 BT_DBG("%s", hdev->name);
1781
1782 hci_conn_check_pending(hdev);
Johan Hedberg127178d2010-11-18 22:22:29 +02001783
1784 hci_dev_lock(hdev);
1785
Johan Hedberga88a9652011-03-30 13:18:12 +03001786 if (ev->status == 0 && test_bit(HCI_MGMT, &hdev->flags))
1787 mgmt_remote_name(hdev->id, &ev->bdaddr, ev->name);
1788
Johan Hedberg127178d2010-11-18 22:22:29 +02001789 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001790 if (conn && hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02001791 struct hci_cp_auth_requested cp;
1792 cp.handle = __cpu_to_le16(conn->handle);
1793 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
1794 }
1795
1796 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001797}
1798
1799static inline void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
1800{
1801 struct hci_ev_encrypt_change *ev = (void *) skb->data;
1802 struct hci_conn *conn;
1803
1804 BT_DBG("%s status %d", hdev->name, ev->status);
1805
1806 hci_dev_lock(hdev);
1807
1808 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1809 if (conn) {
1810 if (!ev->status) {
Marcel Holtmannae293192008-07-14 20:13:45 +02001811 if (ev->encrypt) {
1812 /* Encryption implies authentication */
1813 conn->link_mode |= HCI_LM_AUTH;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001814 conn->link_mode |= HCI_LM_ENCRYPT;
Vinicius Costa Gomes64532ec2011-06-09 18:50:53 -03001815 conn->sec_level = conn->pending_sec_level;
Marcel Holtmannae293192008-07-14 20:13:45 +02001816 } else
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001817 conn->link_mode &= ~HCI_LM_ENCRYPT;
1818 }
1819
1820 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
1821
Marcel Holtmannf8558552008-07-14 20:13:49 +02001822 if (conn->state == BT_CONFIG) {
1823 if (!ev->status)
1824 conn->state = BT_CONNECTED;
1825
1826 hci_proto_connect_cfm(conn, ev->status);
1827 hci_conn_put(conn);
1828 } else
1829 hci_encrypt_cfm(conn, ev->status, ev->encrypt);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001830 }
1831
1832 hci_dev_unlock(hdev);
1833}
1834
1835static inline void hci_change_link_key_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1836{
1837 struct hci_ev_change_link_key_complete *ev = (void *) skb->data;
1838 struct hci_conn *conn;
1839
1840 BT_DBG("%s status %d", hdev->name, ev->status);
1841
1842 hci_dev_lock(hdev);
1843
1844 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1845 if (conn) {
1846 if (!ev->status)
1847 conn->link_mode |= HCI_LM_SECURE;
1848
1849 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
1850
1851 hci_key_change_cfm(conn, ev->status);
1852 }
1853
1854 hci_dev_unlock(hdev);
1855}
1856
1857static inline void hci_remote_features_evt(struct hci_dev *hdev, struct sk_buff *skb)
1858{
1859 struct hci_ev_remote_features *ev = (void *) skb->data;
1860 struct hci_conn *conn;
1861
1862 BT_DBG("%s status %d", hdev->name, ev->status);
1863
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001864 hci_dev_lock(hdev);
1865
1866 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02001867 if (!conn)
1868 goto unlock;
Marcel Holtmann769be972008-07-14 20:13:49 +02001869
Johan Hedbergccd556f2010-11-10 17:11:51 +02001870 if (!ev->status)
1871 memcpy(conn->features, ev->features, 8);
1872
1873 if (conn->state != BT_CONFIG)
1874 goto unlock;
1875
1876 if (!ev->status && lmp_ssp_capable(hdev) && lmp_ssp_capable(conn)) {
1877 struct hci_cp_read_remote_ext_features cp;
1878 cp.handle = ev->handle;
1879 cp.page = 0x01;
1880 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES,
Marcel Holtmann769be972008-07-14 20:13:49 +02001881 sizeof(cp), &cp);
Johan Hedberg392599b2010-11-18 22:22:28 +02001882 goto unlock;
1883 }
1884
Johan Hedberg127178d2010-11-18 22:22:29 +02001885 if (!ev->status) {
1886 struct hci_cp_remote_name_req cp;
1887 memset(&cp, 0, sizeof(cp));
1888 bacpy(&cp.bdaddr, &conn->dst);
1889 cp.pscan_rep_mode = 0x02;
1890 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
1891 }
Johan Hedberg392599b2010-11-18 22:22:28 +02001892
Johan Hedberg127178d2010-11-18 22:22:29 +02001893 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02001894 conn->state = BT_CONNECTED;
1895 hci_proto_connect_cfm(conn, ev->status);
1896 hci_conn_put(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02001897 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001898
Johan Hedbergccd556f2010-11-10 17:11:51 +02001899unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001900 hci_dev_unlock(hdev);
1901}
1902
1903static inline void hci_remote_version_evt(struct hci_dev *hdev, struct sk_buff *skb)
1904{
1905 BT_DBG("%s", hdev->name);
1906}
1907
1908static inline void hci_qos_setup_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1909{
1910 BT_DBG("%s", hdev->name);
1911}
1912
1913static inline void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1914{
1915 struct hci_ev_cmd_complete *ev = (void *) skb->data;
1916 __u16 opcode;
1917
1918 skb_pull(skb, sizeof(*ev));
1919
1920 opcode = __le16_to_cpu(ev->opcode);
1921
1922 switch (opcode) {
1923 case HCI_OP_INQUIRY_CANCEL:
1924 hci_cc_inquiry_cancel(hdev, skb);
1925 break;
1926
1927 case HCI_OP_EXIT_PERIODIC_INQ:
1928 hci_cc_exit_periodic_inq(hdev, skb);
1929 break;
1930
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001931 case HCI_OP_LINK_KEY_REPLY:
1932 hci_cc_link_key_reply(hdev, skb);
1933 break;
1934
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001935 case HCI_OP_REMOTE_NAME_REQ_CANCEL:
1936 hci_cc_remote_name_req_cancel(hdev, skb);
1937 break;
1938
1939 case HCI_OP_ROLE_DISCOVERY:
1940 hci_cc_role_discovery(hdev, skb);
1941 break;
1942
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02001943 case HCI_OP_READ_LINK_POLICY:
1944 hci_cc_read_link_policy(hdev, skb);
1945 break;
1946
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001947 case HCI_OP_WRITE_LINK_POLICY:
1948 hci_cc_write_link_policy(hdev, skb);
1949 break;
1950
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02001951 case HCI_OP_READ_DEF_LINK_POLICY:
1952 hci_cc_read_def_link_policy(hdev, skb);
1953 break;
1954
1955 case HCI_OP_WRITE_DEF_LINK_POLICY:
1956 hci_cc_write_def_link_policy(hdev, skb);
1957 break;
1958
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001959 case HCI_OP_RESET:
1960 hci_cc_reset(hdev, skb);
1961 break;
1962
1963 case HCI_OP_WRITE_LOCAL_NAME:
1964 hci_cc_write_local_name(hdev, skb);
1965 break;
1966
1967 case HCI_OP_READ_LOCAL_NAME:
1968 hci_cc_read_local_name(hdev, skb);
1969 break;
1970
1971 case HCI_OP_WRITE_AUTH_ENABLE:
1972 hci_cc_write_auth_enable(hdev, skb);
1973 break;
1974
1975 case HCI_OP_WRITE_ENCRYPT_MODE:
1976 hci_cc_write_encrypt_mode(hdev, skb);
1977 break;
1978
1979 case HCI_OP_WRITE_SCAN_ENABLE:
1980 hci_cc_write_scan_enable(hdev, skb);
1981 break;
1982
1983 case HCI_OP_READ_CLASS_OF_DEV:
1984 hci_cc_read_class_of_dev(hdev, skb);
1985 break;
1986
1987 case HCI_OP_WRITE_CLASS_OF_DEV:
1988 hci_cc_write_class_of_dev(hdev, skb);
1989 break;
1990
1991 case HCI_OP_READ_VOICE_SETTING:
1992 hci_cc_read_voice_setting(hdev, skb);
1993 break;
1994
1995 case HCI_OP_WRITE_VOICE_SETTING:
1996 hci_cc_write_voice_setting(hdev, skb);
1997 break;
1998
1999 case HCI_OP_HOST_BUFFER_SIZE:
2000 hci_cc_host_buffer_size(hdev, skb);
2001 break;
2002
Marcel Holtmann333140b2008-07-14 20:13:48 +02002003 case HCI_OP_READ_SSP_MODE:
2004 hci_cc_read_ssp_mode(hdev, skb);
2005 break;
2006
2007 case HCI_OP_WRITE_SSP_MODE:
2008 hci_cc_write_ssp_mode(hdev, skb);
2009 break;
2010
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002011 case HCI_OP_READ_LOCAL_VERSION:
2012 hci_cc_read_local_version(hdev, skb);
2013 break;
2014
2015 case HCI_OP_READ_LOCAL_COMMANDS:
2016 hci_cc_read_local_commands(hdev, skb);
2017 break;
2018
2019 case HCI_OP_READ_LOCAL_FEATURES:
2020 hci_cc_read_local_features(hdev, skb);
2021 break;
2022
2023 case HCI_OP_READ_BUFFER_SIZE:
2024 hci_cc_read_buffer_size(hdev, skb);
2025 break;
2026
2027 case HCI_OP_READ_BD_ADDR:
2028 hci_cc_read_bd_addr(hdev, skb);
2029 break;
2030
Johan Hedberg23bb5762010-12-21 23:01:27 +02002031 case HCI_OP_WRITE_CA_TIMEOUT:
2032 hci_cc_write_ca_timeout(hdev, skb);
2033 break;
2034
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002035 case HCI_OP_READ_FLOW_CONTROL_MODE:
2036 hci_cc_read_flow_control_mode(hdev, skb);
2037 break;
2038
2039 case HCI_OP_READ_DATA_BLOCK_SIZE:
2040 hci_cc_read_data_block_size(hdev, skb);
2041 break;
2042
2043 case HCI_OP_READ_LOCAL_AMP_INFO:
2044 hci_cc_read_local_amp_info(hdev, skb);
2045 break;
2046
2047 case HCI_OP_READ_LOCAL_AMP_ASSOC:
2048 case HCI_OP_WRITE_REMOTE_AMP_ASSOC:
2049 hci_amp_cmd_complete(hdev, opcode, skb);
2050 break;
2051
Johan Hedbergb0916ea2011-01-10 13:44:55 +02002052 case HCI_OP_DELETE_STORED_LINK_KEY:
2053 hci_cc_delete_stored_link_key(hdev, skb);
2054 break;
2055
Johan Hedbergd5859e22011-01-25 01:19:58 +02002056 case HCI_OP_SET_EVENT_MASK:
2057 hci_cc_set_event_mask(hdev, skb);
2058 break;
2059
2060 case HCI_OP_WRITE_INQUIRY_MODE:
2061 hci_cc_write_inquiry_mode(hdev, skb);
2062 break;
2063
2064 case HCI_OP_READ_INQ_RSP_TX_POWER:
2065 hci_cc_read_inq_rsp_tx_power(hdev, skb);
2066 break;
2067
2068 case HCI_OP_SET_EVENT_FLT:
2069 hci_cc_set_event_flt(hdev, skb);
2070 break;
2071
Johan Hedberg980e1a52011-01-22 06:10:07 +02002072 case HCI_OP_PIN_CODE_REPLY:
2073 hci_cc_pin_code_reply(hdev, skb);
2074 break;
2075
2076 case HCI_OP_PIN_CODE_NEG_REPLY:
2077 hci_cc_pin_code_neg_reply(hdev, skb);
2078 break;
2079
Szymon Jancc35938b2011-03-22 13:12:21 +01002080 case HCI_OP_READ_LOCAL_OOB_DATA:
2081 hci_cc_read_local_oob_data_reply(hdev, skb);
2082 break;
2083
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002084 case HCI_OP_LE_READ_BUFFER_SIZE:
2085 hci_cc_le_read_buffer_size(hdev, skb);
2086 break;
2087
Johan Hedberga5c29682011-02-19 12:05:57 -03002088 case HCI_OP_USER_CONFIRM_REPLY:
2089 hci_cc_user_confirm_reply(hdev, skb);
2090 break;
2091
2092 case HCI_OP_USER_CONFIRM_NEG_REPLY:
2093 hci_cc_user_confirm_neg_reply(hdev, skb);
2094 break;
2095
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03002096 case HCI_OP_LE_LTK_REPLY:
2097 hci_cc_le_ltk_reply(hdev, skb);
2098 break;
2099
2100 case HCI_OP_LE_LTK_NEG_REPLY:
2101 hci_cc_le_ltk_neg_reply(hdev, skb);
2102 break;
2103
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002104 case HCI_OP_LE_SET_SCAN_ENABLE:
2105 hci_cc_le_set_scan_enable(hdev, skb);
Andre Guedese326af42011-06-30 19:20:53 -03002106 break;
2107
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002108 default:
2109 BT_DBG("%s opcode 0x%x", hdev->name, opcode);
2110 break;
2111 }
2112
Ville Tervo6bd32322011-02-16 16:32:41 +02002113 if (ev->opcode != HCI_OP_NOP)
2114 del_timer(&hdev->cmd_timer);
2115
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002116 if (ev->ncmd) {
2117 atomic_set(&hdev->cmd_cnt, 1);
2118 if (!skb_queue_empty(&hdev->cmd_q))
Marcel Holtmannc78ae282009-11-18 01:02:54 +01002119 tasklet_schedule(&hdev->cmd_task);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002120 }
2121}
2122
2123static inline void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb)
2124{
2125 struct hci_ev_cmd_status *ev = (void *) skb->data;
2126 __u16 opcode;
2127
2128 skb_pull(skb, sizeof(*ev));
2129
2130 opcode = __le16_to_cpu(ev->opcode);
2131
2132 switch (opcode) {
2133 case HCI_OP_INQUIRY:
2134 hci_cs_inquiry(hdev, ev->status);
2135 break;
2136
2137 case HCI_OP_CREATE_CONN:
2138 hci_cs_create_conn(hdev, ev->status);
2139 break;
2140
2141 case HCI_OP_ADD_SCO:
2142 hci_cs_add_sco(hdev, ev->status);
2143 break;
2144
Marcel Holtmannf8558552008-07-14 20:13:49 +02002145 case HCI_OP_AUTH_REQUESTED:
2146 hci_cs_auth_requested(hdev, ev->status);
2147 break;
2148
2149 case HCI_OP_SET_CONN_ENCRYPT:
2150 hci_cs_set_conn_encrypt(hdev, ev->status);
2151 break;
2152
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002153 case HCI_OP_REMOTE_NAME_REQ:
2154 hci_cs_remote_name_req(hdev, ev->status);
2155 break;
2156
Marcel Holtmann769be972008-07-14 20:13:49 +02002157 case HCI_OP_READ_REMOTE_FEATURES:
2158 hci_cs_read_remote_features(hdev, ev->status);
2159 break;
2160
2161 case HCI_OP_READ_REMOTE_EXT_FEATURES:
2162 hci_cs_read_remote_ext_features(hdev, ev->status);
2163 break;
2164
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002165 case HCI_OP_SETUP_SYNC_CONN:
2166 hci_cs_setup_sync_conn(hdev, ev->status);
2167 break;
2168
2169 case HCI_OP_SNIFF_MODE:
2170 hci_cs_sniff_mode(hdev, ev->status);
2171 break;
2172
2173 case HCI_OP_EXIT_SNIFF_MODE:
2174 hci_cs_exit_sniff_mode(hdev, ev->status);
2175 break;
2176
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002177 case HCI_OP_CREATE_LOGICAL_LINK:
2178 hci_cs_create_logical_link(hdev, ev->status);
2179 break;
2180
2181 case HCI_OP_ACCEPT_LOGICAL_LINK:
2182 hci_cs_accept_logical_link(hdev, ev->status);
2183 break;
2184
2185 case HCI_OP_DISCONN_LOGICAL_LINK:
2186 hci_cs_disconn_logical_link(hdev, ev->status);
2187 break;
2188
2189 case HCI_OP_FLOW_SPEC_MODIFY:
2190 hci_cs_flow_spec_modify(hdev, ev->status);
2191 break;
2192
2193 case HCI_OP_CREATE_PHYS_LINK:
2194 case HCI_OP_ACCEPT_PHYS_LINK:
2195 hci_amp_cmd_status(hdev, opcode, ev->status);
2196 break;
2197
2198 case HCI_OP_DISCONN_PHYS_LINK:
2199 hci_cs_disconn_physical_link(hdev, ev->status);
2200
Johan Hedberg8962ee72011-01-20 12:40:27 +02002201 case HCI_OP_DISCONNECT:
2202 if (ev->status != 0)
2203 mgmt_disconnect_failed(hdev->id);
2204 break;
2205
Ville Tervofcd89c02011-02-10 22:38:47 -03002206 case HCI_OP_LE_CREATE_CONN:
2207 hci_cs_le_create_conn(hdev, ev->status);
2208 break;
2209
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03002210 case HCI_OP_LE_START_ENC:
2211 hci_cs_le_start_enc(hdev, ev->status);
2212 break;
2213
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002214 default:
2215 BT_DBG("%s opcode 0x%x", hdev->name, opcode);
2216 break;
2217 }
2218
Ville Tervo6bd32322011-02-16 16:32:41 +02002219 if (ev->opcode != HCI_OP_NOP)
2220 del_timer(&hdev->cmd_timer);
2221
Gustavo F. Padovan10572132011-03-16 15:36:29 -03002222 if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags)) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002223 atomic_set(&hdev->cmd_cnt, 1);
2224 if (!skb_queue_empty(&hdev->cmd_q))
Marcel Holtmannc78ae282009-11-18 01:02:54 +01002225 tasklet_schedule(&hdev->cmd_task);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002226 }
2227}
2228
2229static inline void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
2230{
2231 struct hci_ev_role_change *ev = (void *) skb->data;
2232 struct hci_conn *conn;
2233
2234 BT_DBG("%s status %d", hdev->name, ev->status);
2235
2236 hci_dev_lock(hdev);
2237
2238 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
2239 if (conn) {
2240 if (!ev->status) {
2241 if (ev->role)
2242 conn->link_mode &= ~HCI_LM_MASTER;
2243 else
2244 conn->link_mode |= HCI_LM_MASTER;
2245 }
2246
2247 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->pend);
2248
2249 hci_role_switch_cfm(conn, ev->status, ev->role);
2250 }
2251
2252 hci_dev_unlock(hdev);
2253}
2254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255static inline void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
2256{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002257 struct hci_ev_num_comp_pkts *ev = (void *) skb->data;
Marcel Holtmann1ebb9252005-11-08 09:57:21 -08002258 __le16 *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 int i;
2260
2261 skb_pull(skb, sizeof(*ev));
2262
2263 BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
2264
2265 if (skb->len < ev->num_hndl * 4) {
2266 BT_DBG("%s bad parameters", hdev->name);
2267 return;
2268 }
2269
2270 tasklet_disable(&hdev->tx_task);
2271
Marcel Holtmann1ebb9252005-11-08 09:57:21 -08002272 for (i = 0, ptr = (__le16 *) skb->data; i < ev->num_hndl; i++) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002273 struct hci_conn *conn = NULL;
2274 struct hci_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 __u16 handle, count;
2276
Harvey Harrison83985312008-05-02 16:25:46 -07002277 handle = get_unaligned_le16(ptr++);
2278 count = get_unaligned_le16(ptr++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002280 if (hdev->dev_type == HCI_BREDR)
2281 conn = hci_conn_hash_lookup_handle(hdev, handle);
2282 else {
2283 chan = hci_chan_list_lookup_handle(hdev, handle);
2284 if (chan)
2285 conn = chan->conn;
2286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 if (conn) {
2288 conn->sent -= count;
2289
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02002290 if (conn->type == ACL_LINK) {
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02002291 hdev->acl_cnt += count;
2292 if (hdev->acl_cnt > hdev->acl_pkts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 hdev->acl_cnt = hdev->acl_pkts;
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002294 } else if (conn->type == LE_LINK) {
2295 if (hdev->le_pkts) {
2296 hdev->le_cnt += count;
2297 if (hdev->le_cnt > hdev->le_pkts)
2298 hdev->le_cnt = hdev->le_pkts;
2299 } else {
2300 hdev->acl_cnt += count;
2301 if (hdev->acl_cnt > hdev->acl_pkts)
2302 hdev->acl_cnt = hdev->acl_pkts;
2303 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02002304 } else {
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02002305 hdev->sco_cnt += count;
2306 if (hdev->sco_cnt > hdev->sco_pkts)
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02002307 hdev->sco_cnt = hdev->sco_pkts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 }
2309 }
2310 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002311
Marcel Holtmannc78ae282009-11-18 01:02:54 +01002312 tasklet_schedule(&hdev->tx_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
2314 tasklet_enable(&hdev->tx_task);
2315}
2316
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002317static inline void hci_num_comp_blocks_evt(struct hci_dev *hdev,
2318 struct sk_buff *skb)
2319{
2320 struct hci_ev_num_comp_blocks *ev = (void *) skb->data;
2321 __le16 *ptr;
2322 int i;
2323
2324 skb_pull(skb, sizeof(*ev));
2325
2326 BT_DBG("%s total_num_blocks %d num_hndl %d",
2327 hdev->name, ev->total_num_blocks, ev->num_hndl);
2328
2329 if (skb->len < ev->num_hndl * 6) {
2330 BT_DBG("%s bad parameters", hdev->name);
2331 return;
2332 }
2333
2334 tasklet_disable(&hdev->tx_task);
2335
2336 for (i = 0, ptr = (__le16 *) skb->data; i < ev->num_hndl; i++) {
2337 struct hci_conn *conn = NULL;
2338 struct hci_chan *chan;
2339 __u16 handle, block_count;
2340
2341 handle = get_unaligned_le16(ptr++);
2342
2343 /* Skip packet count */
2344 ptr++;
2345 block_count = get_unaligned_le16(ptr++);
2346
2347 BT_DBG("%s handle %d count %d", hdev->name, handle,
2348 block_count);
2349
2350 if (hdev->dev_type == HCI_BREDR)
2351 conn = hci_conn_hash_lookup_handle(hdev, handle);
2352 else {
2353 chan = hci_chan_list_lookup_handle(hdev, handle);
2354 if (chan)
2355 conn = chan->conn;
2356 }
2357 if (conn) {
2358 BT_DBG("%s conn %p sent %d", hdev->name,
2359 conn, conn->sent);
2360
2361 conn->sent -= block_count;
2362
2363 if (conn->type == ACL_LINK) {
2364 hdev->acl_cnt += block_count;
2365 if (hdev->acl_cnt > hdev->acl_pkts)
2366 hdev->acl_cnt = hdev->acl_pkts;
2367 } else {
2368 /* We should not find ourselves here */
2369 BT_DBG("Unexpected event for SCO connection");
2370 }
2371 }
2372 }
2373
2374 tasklet_schedule(&hdev->tx_task);
2375
2376 tasklet_enable(&hdev->tx_task);
2377}
2378
Marcel Holtmann04837f62006-07-03 10:02:33 +02002379static inline void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002381 struct hci_ev_mode_change *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02002382 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 BT_DBG("%s status %d", hdev->name, ev->status);
2385
2386 hci_dev_lock(hdev);
2387
Marcel Holtmann04837f62006-07-03 10:02:33 +02002388 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
2389 if (conn) {
2390 conn->mode = ev->mode;
2391 conn->interval = __le16_to_cpu(ev->interval);
2392
2393 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
2394 if (conn->mode == HCI_CM_ACTIVE)
2395 conn->power_save = 1;
2396 else
2397 conn->power_save = 0;
2398 }
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002399
2400 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->pend))
2401 hci_sco_setup(conn, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02002402 }
2403
2404 hci_dev_unlock(hdev);
2405}
2406
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407static inline void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
2408{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002409 struct hci_ev_pin_code_req *ev = (void *) skb->data;
2410 struct hci_conn *conn;
2411
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002412 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002413
2414 hci_dev_lock(hdev);
2415
2416 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Marcel Holtmann3d7a9d12009-05-09 12:09:21 -07002417 if (conn && conn->state == BT_CONNECTED) {
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002418 hci_conn_hold(conn);
2419 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
2420 hci_conn_put(conn);
2421 }
2422
Johan Hedberg03b555e2011-01-04 15:40:05 +02002423 if (!test_bit(HCI_PAIRABLE, &hdev->flags))
2424 hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
2425 sizeof(ev->bdaddr), &ev->bdaddr);
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02002426
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002427 if (test_bit(HCI_MGMT, &hdev->flags))
2428 mgmt_pin_code_request(hdev->id, &ev->bdaddr);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002429
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002430 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431}
2432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433static inline void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
2434{
Johan Hedberg55ed8ca2011-01-17 14:41:05 +02002435 struct hci_ev_link_key_req *ev = (void *) skb->data;
2436 struct hci_cp_link_key_reply cp;
2437 struct hci_conn *conn;
2438 struct link_key *key;
2439
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002440 BT_DBG("%s", hdev->name);
Johan Hedberg55ed8ca2011-01-17 14:41:05 +02002441
2442 if (!test_bit(HCI_LINK_KEYS, &hdev->flags))
2443 return;
2444
2445 hci_dev_lock(hdev);
2446
2447 key = hci_find_link_key(hdev, &ev->bdaddr);
2448 if (!key) {
2449 BT_DBG("%s link key not found for %s", hdev->name,
2450 batostr(&ev->bdaddr));
2451 goto not_found;
2452 }
2453
2454 BT_DBG("%s found key type %u for %s", hdev->name, key->type,
2455 batostr(&ev->bdaddr));
2456
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002457 if (!test_bit(HCI_DEBUG_KEYS, &hdev->flags) && key->type == 0x03) {
Johan Hedberg55ed8ca2011-01-17 14:41:05 +02002458 BT_DBG("%s ignoring debug key", hdev->name);
2459 goto not_found;
2460 }
2461
2462 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
2463
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002464 if (key->type == 0x04 && conn && conn->auth_type != 0xff &&
2465 (conn->auth_type & 0x01)) {
2466 BT_DBG("%s ignoring unauthenticated key", hdev->name);
2467 goto not_found;
Johan Hedberg55ed8ca2011-01-17 14:41:05 +02002468 }
2469
2470 bacpy(&cp.bdaddr, &ev->bdaddr);
2471 memcpy(cp.link_key, key->val, 16);
2472
2473 hci_send_cmd(hdev, HCI_OP_LINK_KEY_REPLY, sizeof(cp), &cp);
2474
2475 hci_dev_unlock(hdev);
2476
2477 return;
2478
2479not_found:
2480 hci_send_cmd(hdev, HCI_OP_LINK_KEY_NEG_REPLY, 6, &ev->bdaddr);
2481 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482}
2483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484static inline void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
2485{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002486 struct hci_ev_link_key_notify *ev = (void *) skb->data;
2487 struct hci_conn *conn;
Johan Hedberg55ed8ca2011-01-17 14:41:05 +02002488 u8 pin_len = 0;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002489
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002490 BT_DBG("%s type %d", hdev->name, ev->key_type);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002491
2492 hci_dev_lock(hdev);
2493
2494 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
2495 if (conn) {
2496 hci_conn_hold(conn);
2497 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002498
2499 memcpy(conn->link_key, ev->link_key, 16);
2500 conn->key_type = ev->key_type;
2501 hci_disconnect_amp(conn, 0x06);
2502
Johan Hedberg980e1a52011-01-22 06:10:07 +02002503 pin_len = conn->pin_length;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002504 hci_conn_put(conn);
2505 }
2506
Johan Hedberg55ed8ca2011-01-17 14:41:05 +02002507 if (test_bit(HCI_LINK_KEYS, &hdev->flags))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002508 hci_add_link_key(hdev, 1, &ev->bdaddr, ev->link_key,
Johan Hedberg55ed8ca2011-01-17 14:41:05 +02002509 ev->key_type, pin_len);
2510
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002511 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512}
2513
Marcel Holtmann04837f62006-07-03 10:02:33 +02002514static inline void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
2515{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002516 struct hci_ev_clock_offset *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02002517 struct hci_conn *conn;
2518
2519 BT_DBG("%s status %d", hdev->name, ev->status);
2520
2521 hci_dev_lock(hdev);
2522
2523 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 if (conn && !ev->status) {
2525 struct inquiry_entry *ie;
2526
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02002527 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
2528 if (ie) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 ie->data.clock_offset = ev->clock_offset;
2530 ie->timestamp = jiffies;
2531 }
2532 }
2533
2534 hci_dev_unlock(hdev);
2535}
2536
Marcel Holtmanna8746412008-07-14 20:13:46 +02002537static inline void hci_pkt_type_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
2538{
2539 struct hci_ev_pkt_type_change *ev = (void *) skb->data;
2540 struct hci_conn *conn;
2541
2542 BT_DBG("%s status %d", hdev->name, ev->status);
2543
2544 hci_dev_lock(hdev);
2545
2546 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
2547 if (conn && !ev->status)
2548 conn->pkt_type = __le16_to_cpu(ev->pkt_type);
2549
2550 hci_dev_unlock(hdev);
2551}
2552
Marcel Holtmann85a1e932005-08-09 20:28:02 -07002553static inline void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
2554{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002555 struct hci_ev_pscan_rep_mode *ev = (void *) skb->data;
Marcel Holtmann85a1e932005-08-09 20:28:02 -07002556 struct inquiry_entry *ie;
2557
2558 BT_DBG("%s", hdev->name);
2559
2560 hci_dev_lock(hdev);
2561
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02002562 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
2563 if (ie) {
Marcel Holtmann85a1e932005-08-09 20:28:02 -07002564 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
2565 ie->timestamp = jiffies;
2566 }
2567
2568 hci_dev_unlock(hdev);
2569}
2570
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002571static inline void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, struct sk_buff *skb)
2572{
2573 struct inquiry_data data;
2574 int num_rsp = *((__u8 *) skb->data);
2575
2576 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
2577
2578 if (!num_rsp)
2579 return;
2580
2581 hci_dev_lock(hdev);
2582
2583 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
Szymon Janc138d22e2011-02-17 16:44:23 +01002584 struct inquiry_info_with_rssi_and_pscan_mode *info;
2585 info = (void *) (skb->data + 1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002586
Johan Hedberge17acd42011-03-30 23:57:16 +03002587 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002588 bacpy(&data.bdaddr, &info->bdaddr);
2589 data.pscan_rep_mode = info->pscan_rep_mode;
2590 data.pscan_period_mode = info->pscan_period_mode;
2591 data.pscan_mode = info->pscan_mode;
2592 memcpy(data.dev_class, info->dev_class, 3);
2593 data.clock_offset = info->clock_offset;
2594 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002595 data.ssp_mode = 0x00;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002596 hci_inquiry_cache_update(hdev, &data);
Johan Hedberge17acd42011-03-30 23:57:16 +03002597 mgmt_device_found(hdev->id, &info->bdaddr,
2598 info->dev_class, info->rssi,
2599 NULL);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002600 }
2601 } else {
2602 struct inquiry_info_with_rssi *info = (void *) (skb->data + 1);
2603
Johan Hedberge17acd42011-03-30 23:57:16 +03002604 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002605 bacpy(&data.bdaddr, &info->bdaddr);
2606 data.pscan_rep_mode = info->pscan_rep_mode;
2607 data.pscan_period_mode = info->pscan_period_mode;
2608 data.pscan_mode = 0x00;
2609 memcpy(data.dev_class, info->dev_class, 3);
2610 data.clock_offset = info->clock_offset;
2611 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002612 data.ssp_mode = 0x00;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002613 hci_inquiry_cache_update(hdev, &data);
Johan Hedberge17acd42011-03-30 23:57:16 +03002614 mgmt_device_found(hdev->id, &info->bdaddr,
2615 info->dev_class, info->rssi,
2616 NULL);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002617 }
2618 }
2619
2620 hci_dev_unlock(hdev);
2621}
2622
2623static inline void hci_remote_ext_features_evt(struct hci_dev *hdev, struct sk_buff *skb)
2624{
Marcel Holtmann41a96212008-07-14 20:13:48 +02002625 struct hci_ev_remote_ext_features *ev = (void *) skb->data;
2626 struct hci_conn *conn;
2627
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002628 BT_DBG("%s", hdev->name);
Marcel Holtmann41a96212008-07-14 20:13:48 +02002629
Marcel Holtmann41a96212008-07-14 20:13:48 +02002630 hci_dev_lock(hdev);
2631
2632 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02002633 if (!conn)
2634 goto unlock;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002635
Johan Hedbergccd556f2010-11-10 17:11:51 +02002636 if (!ev->status && ev->page == 0x01) {
2637 struct inquiry_entry *ie;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002638
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02002639 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
2640 if (ie)
Johan Hedbergccd556f2010-11-10 17:11:51 +02002641 ie->data.ssp_mode = (ev->features[0] & 0x01);
Marcel Holtmann769be972008-07-14 20:13:49 +02002642
Johan Hedbergccd556f2010-11-10 17:11:51 +02002643 conn->ssp_mode = (ev->features[0] & 0x01);
Marcel Holtmann41a96212008-07-14 20:13:48 +02002644 }
2645
Johan Hedbergccd556f2010-11-10 17:11:51 +02002646 if (conn->state != BT_CONFIG)
2647 goto unlock;
2648
Johan Hedberg127178d2010-11-18 22:22:29 +02002649 if (!ev->status) {
2650 struct hci_cp_remote_name_req cp;
2651 memset(&cp, 0, sizeof(cp));
2652 bacpy(&cp.bdaddr, &conn->dst);
2653 cp.pscan_rep_mode = 0x02;
2654 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
2655 }
Johan Hedberg392599b2010-11-18 22:22:28 +02002656
Johan Hedberg127178d2010-11-18 22:22:29 +02002657 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02002658 conn->state = BT_CONNECTED;
2659 hci_proto_connect_cfm(conn, ev->status);
2660 hci_conn_put(conn);
2661 }
2662
2663unlock:
Marcel Holtmann41a96212008-07-14 20:13:48 +02002664 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002665}
2666
2667static inline void hci_sync_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
2668{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002669 struct hci_ev_sync_conn_complete *ev = (void *) skb->data;
2670 struct hci_conn *conn;
2671
2672 BT_DBG("%s status %d", hdev->name, ev->status);
2673
2674 hci_dev_lock(hdev);
2675
2676 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02002677 if (!conn) {
2678 if (ev->link_type == ESCO_LINK)
2679 goto unlock;
2680
2681 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
2682 if (!conn)
2683 goto unlock;
2684
2685 conn->type = SCO_LINK;
2686 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002687
Marcel Holtmann732547f2009-04-19 19:14:14 +02002688 switch (ev->status) {
2689 case 0x00:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002690 conn->handle = __le16_to_cpu(ev->handle);
2691 conn->state = BT_CONNECTED;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002692
Marcel Holtmann9eba32b2009-08-22 14:19:26 -07002693 hci_conn_hold_device(conn);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002694 hci_conn_add_sysfs(conn);
Marcel Holtmann732547f2009-04-19 19:14:14 +02002695 break;
2696
Stephen Coe705e5712010-02-16 11:29:44 -05002697 case 0x11: /* Unsupported Feature or Parameter Value */
Marcel Holtmann732547f2009-04-19 19:14:14 +02002698 case 0x1c: /* SCO interval rejected */
Nick Pelly1038a002010-02-03 11:42:26 -08002699 case 0x1a: /* Unsupported Remote Feature */
Marcel Holtmann732547f2009-04-19 19:14:14 +02002700 case 0x1f: /* Unspecified error */
2701 if (conn->out && conn->attempt < 2) {
2702 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
2703 (hdev->esco_type & EDR_ESCO_MASK);
2704 hci_setup_sync(conn, conn->link->handle);
2705 goto unlock;
2706 }
2707 /* fall through */
2708
2709 default:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002710 conn->state = BT_CLOSED;
Marcel Holtmann732547f2009-04-19 19:14:14 +02002711 break;
2712 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002713
2714 hci_proto_connect_cfm(conn, ev->status);
2715 if (ev->status)
2716 hci_conn_del(conn);
2717
2718unlock:
2719 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002720}
2721
2722static inline void hci_sync_conn_changed_evt(struct hci_dev *hdev, struct sk_buff *skb)
2723{
2724 BT_DBG("%s", hdev->name);
2725}
2726
Marcel Holtmann04837f62006-07-03 10:02:33 +02002727static inline void hci_sniff_subrate_evt(struct hci_dev *hdev, struct sk_buff *skb)
2728{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002729 struct hci_ev_sniff_subrate *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02002730
2731 BT_DBG("%s status %d", hdev->name, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02002732}
2733
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002734static inline void hci_extended_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
2735{
2736 struct inquiry_data data;
2737 struct extended_inquiry_info *info = (void *) (skb->data + 1);
2738 int num_rsp = *((__u8 *) skb->data);
2739
2740 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
2741
2742 if (!num_rsp)
2743 return;
2744
2745 hci_dev_lock(hdev);
2746
Johan Hedberge17acd42011-03-30 23:57:16 +03002747 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002748 bacpy(&data.bdaddr, &info->bdaddr);
Szymon Janc138d22e2011-02-17 16:44:23 +01002749 data.pscan_rep_mode = info->pscan_rep_mode;
2750 data.pscan_period_mode = info->pscan_period_mode;
2751 data.pscan_mode = 0x00;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002752 memcpy(data.dev_class, info->dev_class, 3);
Szymon Janc138d22e2011-02-17 16:44:23 +01002753 data.clock_offset = info->clock_offset;
2754 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002755 data.ssp_mode = 0x01;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002756 hci_inquiry_cache_update(hdev, &data);
Johan Hedberge17acd42011-03-30 23:57:16 +03002757 mgmt_device_found(hdev->id, &info->bdaddr, info->dev_class,
2758 info->rssi, info->data);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002759 }
2760
2761 hci_dev_unlock(hdev);
2762}
2763
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002764static inline u8 hci_get_auth_req(struct hci_conn *conn)
2765{
2766 /* If remote requests dedicated bonding follow that lead */
2767 if (conn->remote_auth == 0x02 || conn->remote_auth == 0x03) {
2768 /* If both remote and local IO capabilities allow MITM
2769 * protection then require it, otherwise don't */
2770 if (conn->remote_cap == 0x03 || conn->io_capability == 0x03)
2771 return 0x02;
2772 else
2773 return 0x03;
2774 }
2775
2776 /* If remote requests no-bonding follow that lead */
2777 if (conn->remote_auth == 0x00 || conn->remote_auth == 0x01)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002778 return 0x00;
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002779
2780 return conn->auth_type;
2781}
2782
Marcel Holtmann04936842008-07-14 20:13:48 +02002783static inline void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
2784{
2785 struct hci_ev_io_capa_request *ev = (void *) skb->data;
2786 struct hci_conn *conn;
2787
2788 BT_DBG("%s", hdev->name);
2789
2790 hci_dev_lock(hdev);
2791
2792 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg03b555e2011-01-04 15:40:05 +02002793 if (!conn)
2794 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02002795
Johan Hedberg03b555e2011-01-04 15:40:05 +02002796 hci_conn_hold(conn);
2797
2798 if (!test_bit(HCI_MGMT, &hdev->flags))
2799 goto unlock;
2800
2801 if (test_bit(HCI_PAIRABLE, &hdev->flags) ||
2802 (conn->remote_auth & ~0x01) == HCI_AT_NO_BONDING) {
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002803 struct hci_cp_io_capability_reply cp;
2804
2805 bacpy(&cp.bdaddr, &ev->bdaddr);
2806 cp.capability = conn->io_capability;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002807 cp.authentication = hci_get_auth_req(conn);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002808
Szymon Jancce85ee12011-03-22 13:12:23 +01002809 if ((conn->out == 0x01 || conn->remote_oob == 0x01) &&
2810 hci_find_remote_oob_data(hdev, &conn->dst))
2811 cp.oob_data = 0x01;
2812 else
2813 cp.oob_data = 0x00;
2814
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002815 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_REPLY,
2816 sizeof(cp), &cp);
Johan Hedberg03b555e2011-01-04 15:40:05 +02002817 } else {
2818 struct hci_cp_io_capability_neg_reply cp;
2819
2820 bacpy(&cp.bdaddr, &ev->bdaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002821 cp.reason = 0x16; /* Pairing not allowed */
Johan Hedberg03b555e2011-01-04 15:40:05 +02002822
2823 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_NEG_REPLY,
2824 sizeof(cp), &cp);
2825 }
2826
2827unlock:
2828 hci_dev_unlock(hdev);
2829}
2830
2831static inline void hci_io_capa_reply_evt(struct hci_dev *hdev, struct sk_buff *skb)
2832{
2833 struct hci_ev_io_capa_reply *ev = (void *) skb->data;
2834 struct hci_conn *conn;
2835
2836 BT_DBG("%s", hdev->name);
2837
2838 hci_dev_lock(hdev);
2839
2840 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
2841 if (!conn)
2842 goto unlock;
2843
Johan Hedberg03b555e2011-01-04 15:40:05 +02002844 conn->remote_cap = ev->capability;
2845 conn->remote_oob = ev->oob_data;
2846 conn->remote_auth = ev->authentication;
2847
2848unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02002849 hci_dev_unlock(hdev);
2850}
2851
Johan Hedberga5c29682011-02-19 12:05:57 -03002852static inline void hci_user_confirm_request_evt(struct hci_dev *hdev,
2853 struct sk_buff *skb)
2854{
2855 struct hci_ev_user_confirm_req *ev = (void *) skb->data;
2856
2857 BT_DBG("%s", hdev->name);
2858
2859 hci_dev_lock(hdev);
2860
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002861 if (test_bit(HCI_MGMT, &hdev->flags))
2862 mgmt_user_confirm_request(hdev->id, &ev->bdaddr, ev->passkey);
Johan Hedberga5c29682011-02-19 12:05:57 -03002863
2864 hci_dev_unlock(hdev);
2865}
2866
Marcel Holtmann04936842008-07-14 20:13:48 +02002867static inline void hci_simple_pair_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
2868{
2869 struct hci_ev_simple_pair_complete *ev = (void *) skb->data;
2870 struct hci_conn *conn;
2871
2872 BT_DBG("%s", hdev->name);
2873
2874 hci_dev_lock(hdev);
2875
2876 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg2a611692011-02-19 12:06:00 -03002877 if (!conn)
2878 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02002879
Johan Hedberg2a611692011-02-19 12:06:00 -03002880 /* To avoid duplicate auth_failed events to user space we check
2881 * the HCI_CONN_AUTH_PEND flag which will be set if we
2882 * initiated the authentication. A traditional auth_complete
2883 * event gets always produced as initiator and is also mapped to
2884 * the mgmt_auth_failed event */
2885 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->pend) && ev->status != 0)
2886 mgmt_auth_failed(hdev->id, &conn->dst, ev->status);
2887
2888 hci_conn_put(conn);
2889
2890unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02002891 hci_dev_unlock(hdev);
2892}
2893
Marcel Holtmann41a96212008-07-14 20:13:48 +02002894static inline void hci_remote_host_features_evt(struct hci_dev *hdev, struct sk_buff *skb)
2895{
2896 struct hci_ev_remote_host_features *ev = (void *) skb->data;
2897 struct inquiry_entry *ie;
2898
2899 BT_DBG("%s", hdev->name);
2900
2901 hci_dev_lock(hdev);
2902
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02002903 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
2904 if (ie)
Marcel Holtmann41a96212008-07-14 20:13:48 +02002905 ie->data.ssp_mode = (ev->features[0] & 0x01);
2906
2907 hci_dev_unlock(hdev);
2908}
2909
Szymon Janc2763eda2011-03-22 13:12:22 +01002910static inline void hci_remote_oob_data_request_evt(struct hci_dev *hdev,
2911 struct sk_buff *skb)
2912{
2913 struct hci_ev_remote_oob_data_request *ev = (void *) skb->data;
2914 struct oob_data *data;
2915
2916 BT_DBG("%s", hdev->name);
2917
2918 hci_dev_lock(hdev);
2919
Szymon Jance1ba1f12011-04-06 13:01:59 +02002920 if (!test_bit(HCI_MGMT, &hdev->flags))
2921 goto unlock;
2922
Szymon Janc2763eda2011-03-22 13:12:22 +01002923 data = hci_find_remote_oob_data(hdev, &ev->bdaddr);
2924 if (data) {
2925 struct hci_cp_remote_oob_data_reply cp;
2926
2927 bacpy(&cp.bdaddr, &ev->bdaddr);
2928 memcpy(cp.hash, data->hash, sizeof(cp.hash));
2929 memcpy(cp.randomizer, data->randomizer, sizeof(cp.randomizer));
2930
2931 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_REPLY, sizeof(cp),
2932 &cp);
2933 } else {
2934 struct hci_cp_remote_oob_data_neg_reply cp;
2935
2936 bacpy(&cp.bdaddr, &ev->bdaddr);
2937 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_NEG_REPLY, sizeof(cp),
2938 &cp);
2939 }
2940
Szymon Jance1ba1f12011-04-06 13:01:59 +02002941unlock:
Szymon Janc2763eda2011-03-22 13:12:22 +01002942 hci_dev_unlock(hdev);
2943}
2944
Ville Tervofcd89c02011-02-10 22:38:47 -03002945static inline void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
2946{
2947 struct hci_ev_le_conn_complete *ev = (void *) skb->data;
2948 struct hci_conn *conn;
2949
2950 BT_DBG("%s status %d", hdev->name, ev->status);
2951
2952 hci_dev_lock(hdev);
2953
2954 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &ev->bdaddr);
Ville Tervob62f3282011-02-10 22:38:50 -03002955 if (!conn) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002956 conn = hci_le_conn_add(hdev, &ev->bdaddr, ev->bdaddr_type);
Ville Tervob62f3282011-02-10 22:38:50 -03002957 if (!conn) {
2958 BT_ERR("No memory for new connection");
2959 hci_dev_unlock(hdev);
2960 return;
2961 }
2962 }
Ville Tervofcd89c02011-02-10 22:38:47 -03002963
2964 if (ev->status) {
2965 hci_proto_connect_cfm(conn, ev->status);
2966 conn->state = BT_CLOSED;
2967 hci_conn_del(conn);
2968 goto unlock;
2969 }
2970
Vinicius Costa Gomes403d2c82011-06-09 18:50:50 -03002971 conn->sec_level = BT_SECURITY_LOW;
Ville Tervofcd89c02011-02-10 22:38:47 -03002972 conn->handle = __le16_to_cpu(ev->handle);
2973 conn->state = BT_CONNECTED;
2974
2975 hci_conn_hold_device(conn);
2976 hci_conn_add_sysfs(conn);
2977
2978 hci_proto_connect_cfm(conn, ev->status);
2979
2980unlock:
2981 hci_dev_unlock(hdev);
2982}
2983
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03002984static inline void hci_le_ltk_request_evt(struct hci_dev *hdev,
2985 struct sk_buff *skb)
2986{
2987 struct hci_ev_le_ltk_req *ev = (void *) skb->data;
2988 struct hci_cp_le_ltk_reply cp;
Vinicius Costa Gomes60e56472011-07-07 18:59:37 -03002989 struct hci_cp_le_ltk_neg_reply neg;
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03002990 struct hci_conn *conn;
Vinicius Costa Gomes60e56472011-07-07 18:59:37 -03002991 struct link_key *ltk;
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03002992
2993 BT_DBG("%s handle %d", hdev->name, cpu_to_le16(ev->handle));
2994
2995 hci_dev_lock(hdev);
2996
2997 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Vinicius Costa Gomes60e56472011-07-07 18:59:37 -03002998 if (conn == NULL)
2999 goto not_found;
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03003000
Vinicius Costa Gomes60e56472011-07-07 18:59:37 -03003001 ltk = hci_find_ltk(hdev, ev->ediv, ev->random);
3002 if (ltk == NULL)
3003 goto not_found;
3004
3005 memcpy(cp.ltk, ltk->val, sizeof(ltk->val));
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03003006 cp.handle = cpu_to_le16(conn->handle);
Vinicius Costa Gomes1fa2de32011-07-08 18:31:45 -03003007 conn->pin_length = ltk->pin_len;
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03003008
3009 hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
3010
3011 hci_dev_unlock(hdev);
Vinicius Costa Gomes60e56472011-07-07 18:59:37 -03003012
3013 return;
3014
3015not_found:
3016 neg.handle = ev->handle;
3017 hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(neg), &neg);
3018 hci_dev_unlock(hdev);
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03003019}
3020
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003021static inline void hci_le_adv_report_evt(struct hci_dev *hdev,
3022 struct sk_buff *skb)
3023{
3024 struct hci_ev_le_advertising_info *ev;
3025 u8 num_reports;
3026
3027 num_reports = skb->data[0];
3028 ev = (void *) &skb->data[1];
3029 hci_add_adv_entry(hdev, ev);
3030
3031 while (--num_reports) {
3032 ev = (void *) (ev->data + ev->length + 1);
3033 hci_add_adv_entry(hdev, ev);
3034 }
3035}
3036
Ville Tervofcd89c02011-02-10 22:38:47 -03003037static inline void hci_le_meta_evt(struct hci_dev *hdev, struct sk_buff *skb)
3038{
3039 struct hci_ev_le_meta *le_ev = (void *) skb->data;
3040
3041 skb_pull(skb, sizeof(*le_ev));
3042
3043 switch (le_ev->subevent) {
3044 case HCI_EV_LE_CONN_COMPLETE:
3045 hci_le_conn_complete_evt(hdev, skb);
3046 break;
3047
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03003048 case HCI_EV_LE_LTK_REQ:
3049 hci_le_ltk_request_evt(hdev, skb);
3050 break;
3051
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003052 case HCI_EV_LE_ADVERTISING_REPORT:
3053 hci_le_adv_report_evt(hdev, skb);
3054 break;
3055
Ville Tervofcd89c02011-02-10 22:38:47 -03003056 default:
3057 break;
3058 }
3059}
3060
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003061static inline void hci_phy_link_complete(struct hci_dev *hdev,
3062 struct sk_buff *skb)
3063{
3064 struct hci_ev_phys_link_complete *ev = (void *) skb->data;
3065 struct hci_conn *conn;
3066
3067 BT_DBG("%s handle %d status %d", hdev->name, ev->phy_handle,
3068 ev->status);
3069
3070 hci_dev_lock(hdev);
3071
3072 if (ev->status == 0) {
3073 conn = hci_conn_add(hdev, ACL_LINK, 0, BDADDR_ANY);
3074 if (conn) {
3075 conn->handle = ev->phy_handle;
3076 conn->state = BT_CONNECTED;
3077
3078 hci_conn_hold(conn);
3079 conn->disc_timeout = HCI_DISCONN_TIMEOUT/2;
3080 hci_conn_put(conn);
3081
3082 hci_conn_hold_device(conn);
3083 hci_conn_add_sysfs(conn);
3084 } else
3085 BT_ERR("No memory for new connection");
3086 }
3087
3088 hci_dev_unlock(hdev);
3089}
3090
3091static inline void hci_log_link_complete(struct hci_dev *hdev,
3092 struct sk_buff *skb)
3093{
3094 struct hci_ev_log_link_complete *ev = (void *) skb->data;
3095 struct hci_chan *chan;
3096
3097 BT_DBG("%s handle %d status %d", hdev->name,
3098 __le16_to_cpu(ev->log_handle), ev->status);
3099
3100 hci_dev_lock(hdev);
3101
3102 chan = hci_chan_list_lookup_id(hdev, ev->phy_handle);
3103
3104 if (ev->status == 0) {
3105 if (chan) {
3106 chan->ll_handle = __le16_to_cpu(ev->log_handle);
3107 chan->state = BT_CONNECTED;
3108 hci_proto_create_cfm(chan, ev->status);
3109 hci_chan_hold(chan);
3110 }
3111 } else {
3112 if (chan) {
3113 chan->state = BT_CLOSED;
3114 hci_proto_create_cfm(chan, ev->status);
3115 hci_chan_del(chan);
3116 }
3117 }
3118
3119 hci_dev_unlock(hdev);
3120}
3121
3122static inline void hci_flow_spec_modify_complete(struct hci_dev *hdev,
3123 struct sk_buff *skb)
3124{
3125 struct hci_ev_flow_spec_modify_complete *ev = (void *) skb->data;
3126 struct hci_chan *chan;
3127
3128 BT_DBG("%s handle %d status %d", hdev->name,
3129 __le16_to_cpu(ev->log_handle), ev->status);
3130
3131 hci_dev_lock(hdev);
3132
3133 chan = hci_chan_list_lookup_handle(hdev, ev->log_handle);
3134 if (chan)
3135 hci_proto_modify_cfm(chan, ev->status);
3136
3137 hci_dev_unlock(hdev);
3138}
3139
3140static inline void hci_disconn_log_link_complete_evt(struct hci_dev *hdev,
3141 struct sk_buff *skb)
3142{
3143 struct hci_ev_disconn_log_link_complete *ev = (void *) skb->data;
3144 struct hci_chan *chan;
3145
3146 BT_DBG("%s handle %d status %d", hdev->name,
3147 __le16_to_cpu(ev->log_handle), ev->status);
3148
3149 if (ev->status)
3150 return;
3151
3152 hci_dev_lock(hdev);
3153
3154 chan = hci_chan_list_lookup_handle(hdev, __le16_to_cpu(ev->log_handle));
3155 if (chan) {
3156 hci_proto_destroy_cfm(chan, ev->reason);
3157 hci_chan_del(chan);
3158 }
3159
3160 hci_dev_unlock(hdev);
3161}
3162
3163static inline void hci_disconn_phy_link_complete_evt(struct hci_dev *hdev,
3164 struct sk_buff *skb)
3165{
3166 struct hci_ev_disconn_phys_link_complete *ev = (void *) skb->data;
3167 struct hci_conn *conn;
3168
3169 BT_DBG("%s status %d", hdev->name, ev->status);
3170
3171 if (ev->status)
3172 return;
3173
3174 hci_dev_lock(hdev);
3175
3176 conn = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
3177 if (conn) {
3178 conn->state = BT_CLOSED;
3179
3180 hci_proto_disconn_cfm(conn, ev->reason);
3181 hci_conn_del(conn);
3182 }
3183
3184 hci_dev_unlock(hdev);
3185}
3186
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
3188{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003189 struct hci_event_hdr *hdr = (void *) skb->data;
3190 __u8 event = hdr->evt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
3192 skb_pull(skb, HCI_EVENT_HDR_SIZE);
3193
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003194 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 case HCI_EV_INQUIRY_COMPLETE:
3196 hci_inquiry_complete_evt(hdev, skb);
3197 break;
3198
3199 case HCI_EV_INQUIRY_RESULT:
3200 hci_inquiry_result_evt(hdev, skb);
3201 break;
3202
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003203 case HCI_EV_CONN_COMPLETE:
3204 hci_conn_complete_evt(hdev, skb);
Marcel Holtmann21d9e302005-09-13 01:32:25 +02003205 break;
3206
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 case HCI_EV_CONN_REQUEST:
3208 hci_conn_request_evt(hdev, skb);
3209 break;
3210
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 case HCI_EV_DISCONN_COMPLETE:
3212 hci_disconn_complete_evt(hdev, skb);
3213 break;
3214
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 case HCI_EV_AUTH_COMPLETE:
3216 hci_auth_complete_evt(hdev, skb);
3217 break;
3218
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003219 case HCI_EV_REMOTE_NAME:
3220 hci_remote_name_evt(hdev, skb);
3221 break;
3222
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 case HCI_EV_ENCRYPT_CHANGE:
3224 hci_encrypt_change_evt(hdev, skb);
3225 break;
3226
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003227 case HCI_EV_CHANGE_LINK_KEY_COMPLETE:
3228 hci_change_link_key_complete_evt(hdev, skb);
3229 break;
3230
3231 case HCI_EV_REMOTE_FEATURES:
3232 hci_remote_features_evt(hdev, skb);
3233 break;
3234
3235 case HCI_EV_REMOTE_VERSION:
3236 hci_remote_version_evt(hdev, skb);
3237 break;
3238
3239 case HCI_EV_QOS_SETUP_COMPLETE:
3240 hci_qos_setup_complete_evt(hdev, skb);
3241 break;
3242
3243 case HCI_EV_CMD_COMPLETE:
3244 hci_cmd_complete_evt(hdev, skb);
3245 break;
3246
3247 case HCI_EV_CMD_STATUS:
3248 hci_cmd_status_evt(hdev, skb);
3249 break;
3250
3251 case HCI_EV_ROLE_CHANGE:
3252 hci_role_change_evt(hdev, skb);
3253 break;
3254
3255 case HCI_EV_NUM_COMP_PKTS:
3256 hci_num_comp_pkts_evt(hdev, skb);
3257 break;
3258
3259 case HCI_EV_MODE_CHANGE:
3260 hci_mode_change_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 break;
3262
3263 case HCI_EV_PIN_CODE_REQ:
3264 hci_pin_code_request_evt(hdev, skb);
3265 break;
3266
3267 case HCI_EV_LINK_KEY_REQ:
3268 hci_link_key_request_evt(hdev, skb);
3269 break;
3270
3271 case HCI_EV_LINK_KEY_NOTIFY:
3272 hci_link_key_notify_evt(hdev, skb);
3273 break;
3274
3275 case HCI_EV_CLOCK_OFFSET:
3276 hci_clock_offset_evt(hdev, skb);
3277 break;
3278
Marcel Holtmanna8746412008-07-14 20:13:46 +02003279 case HCI_EV_PKT_TYPE_CHANGE:
3280 hci_pkt_type_change_evt(hdev, skb);
3281 break;
3282
Marcel Holtmann85a1e932005-08-09 20:28:02 -07003283 case HCI_EV_PSCAN_REP_MODE:
3284 hci_pscan_rep_mode_evt(hdev, skb);
3285 break;
3286
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003287 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
3288 hci_inquiry_result_with_rssi_evt(hdev, skb);
3289 break;
3290
3291 case HCI_EV_REMOTE_EXT_FEATURES:
3292 hci_remote_ext_features_evt(hdev, skb);
3293 break;
3294
3295 case HCI_EV_SYNC_CONN_COMPLETE:
3296 hci_sync_conn_complete_evt(hdev, skb);
3297 break;
3298
3299 case HCI_EV_SYNC_CONN_CHANGED:
3300 hci_sync_conn_changed_evt(hdev, skb);
3301 break;
3302
Marcel Holtmann04837f62006-07-03 10:02:33 +02003303 case HCI_EV_SNIFF_SUBRATE:
3304 hci_sniff_subrate_evt(hdev, skb);
3305 break;
3306
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003307 case HCI_EV_EXTENDED_INQUIRY_RESULT:
3308 hci_extended_inquiry_result_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 break;
3310
Marcel Holtmann04936842008-07-14 20:13:48 +02003311 case HCI_EV_IO_CAPA_REQUEST:
3312 hci_io_capa_request_evt(hdev, skb);
3313 break;
3314
Johan Hedberg03b555e2011-01-04 15:40:05 +02003315 case HCI_EV_IO_CAPA_REPLY:
3316 hci_io_capa_reply_evt(hdev, skb);
3317 break;
3318
Johan Hedberga5c29682011-02-19 12:05:57 -03003319 case HCI_EV_USER_CONFIRM_REQUEST:
3320 hci_user_confirm_request_evt(hdev, skb);
3321 break;
3322
Marcel Holtmann04936842008-07-14 20:13:48 +02003323 case HCI_EV_SIMPLE_PAIR_COMPLETE:
3324 hci_simple_pair_complete_evt(hdev, skb);
3325 break;
3326
Marcel Holtmann41a96212008-07-14 20:13:48 +02003327 case HCI_EV_REMOTE_HOST_FEATURES:
3328 hci_remote_host_features_evt(hdev, skb);
3329 break;
3330
Ville Tervofcd89c02011-02-10 22:38:47 -03003331 case HCI_EV_LE_META:
3332 hci_le_meta_evt(hdev, skb);
3333 break;
3334
Szymon Janc2763eda2011-03-22 13:12:22 +01003335 case HCI_EV_REMOTE_OOB_DATA_REQUEST:
3336 hci_remote_oob_data_request_evt(hdev, skb);
3337 break;
3338
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003339 case HCI_EV_PHYS_LINK_COMPLETE:
3340 hci_phy_link_complete(hdev, skb);
3341 hci_amp_event_packet(hdev, event, skb);
3342 break;
3343
3344 case HCI_EV_LOG_LINK_COMPLETE:
3345 hci_log_link_complete(hdev, skb);
3346 break;
3347
3348 case HCI_EV_FLOW_SPEC_MODIFY_COMPLETE:
3349 hci_flow_spec_modify_complete(hdev, skb);
3350 break;
3351
3352 case HCI_EV_DISCONN_LOG_LINK_COMPLETE:
3353 hci_disconn_log_link_complete_evt(hdev, skb);
3354 break;
3355
3356 case HCI_EV_DISCONN_PHYS_LINK_COMPLETE:
3357 hci_disconn_phy_link_complete_evt(hdev, skb);
3358 hci_amp_event_packet(hdev, event, skb);
3359 break;
3360
3361 case HCI_EV_NUM_COMP_BLOCKS:
3362 hci_num_comp_blocks_evt(hdev, skb);
3363 break;
3364
3365 case HCI_EV_CHANNEL_SELECTED:
3366 hci_amp_event_packet(hdev, event, skb);
3367 break;
3368
3369 case HCI_EV_AMP_STATUS_CHANGE:
3370 hci_amp_event_packet(hdev, event, skb);
3371 break;
3372
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003373 default:
3374 BT_DBG("%s event 0x%x", hdev->name, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 break;
3376 }
3377
3378 kfree_skb(skb);
3379 hdev->stat.evt_rx++;
3380}
3381
3382/* Generate internal stack event */
3383void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
3384{
3385 struct hci_event_hdr *hdr;
3386 struct hci_ev_stack_internal *ev;
3387 struct sk_buff *skb;
3388
3389 skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC);
3390 if (!skb)
3391 return;
3392
3393 hdr = (void *) skb_put(skb, HCI_EVENT_HDR_SIZE);
3394 hdr->evt = HCI_EV_STACK_INTERNAL;
3395 hdr->plen = sizeof(*ev) + dlen;
3396
3397 ev = (void *) skb_put(skb, sizeof(*ev) + dlen);
3398 ev->type = type;
3399 memcpy(ev->data, data, dlen);
3400
Marcel Holtmann576c7d82005-08-06 12:36:54 +02003401 bt_cb(skb)->incoming = 1;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07003402 __net_timestamp(skb);
Marcel Holtmann576c7d82005-08-06 12:36:54 +02003403
Marcel Holtmann0d48d932005-08-09 20:30:28 -07003404 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 skb->dev = (void *) hdev;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02003406 hci_send_to_sock(hdev, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 kfree_skb(skb);
3408}