blob: f83c1086136854013a4be06e70c7f88eb6159846 [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
Srinivas Krovvidi32ba9352012-01-26 10:48:08 +05303 Copyright (c) 2000-2001, 2010-2012, 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;
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800226 hci_dev_lock(hdev);
Royston Rodriguesc58c0c22011-10-24 12:54:13 +0530227 if (!status)
228 memcpy(hdev->dev_name, sent, HCI_MAX_NAME_LENGTH);
229
Johan Hedbergb312b1612011-03-16 14:29:37 +0200230 if (test_bit(HCI_MGMT, &hdev->flags))
231 mgmt_set_local_name_complete(hdev->id, sent, status);
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800232 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200233}
234
235static void hci_cc_read_local_name(struct hci_dev *hdev, struct sk_buff *skb)
236{
237 struct hci_rp_read_local_name *rp = (void *) skb->data;
238
239 BT_DBG("%s status 0x%x", hdev->name, rp->status);
240
241 if (rp->status)
242 return;
243
Johan Hedberg1f6c6372011-03-16 14:29:35 +0200244 memcpy(hdev->dev_name, rp->name, HCI_MAX_NAME_LENGTH);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200245}
246
247static void hci_cc_write_auth_enable(struct hci_dev *hdev, struct sk_buff *skb)
248{
249 __u8 status = *((__u8 *) skb->data);
250 void *sent;
251
252 BT_DBG("%s status 0x%x", hdev->name, status);
253
254 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_ENABLE);
255 if (!sent)
256 return;
257
258 if (!status) {
259 __u8 param = *((__u8 *) sent);
260
261 if (param == AUTH_ENABLED)
262 set_bit(HCI_AUTH, &hdev->flags);
263 else
264 clear_bit(HCI_AUTH, &hdev->flags);
265 }
266
Johan Hedberg23bb5762010-12-21 23:01:27 +0200267 hci_req_complete(hdev, HCI_OP_WRITE_AUTH_ENABLE, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200268}
269
270static void hci_cc_write_encrypt_mode(struct hci_dev *hdev, struct sk_buff *skb)
271{
272 __u8 status = *((__u8 *) skb->data);
273 void *sent;
274
275 BT_DBG("%s status 0x%x", hdev->name, status);
276
277 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_ENCRYPT_MODE);
278 if (!sent)
279 return;
280
281 if (!status) {
282 __u8 param = *((__u8 *) sent);
283
284 if (param)
285 set_bit(HCI_ENCRYPT, &hdev->flags);
286 else
287 clear_bit(HCI_ENCRYPT, &hdev->flags);
288 }
289
Johan Hedberg23bb5762010-12-21 23:01:27 +0200290 hci_req_complete(hdev, HCI_OP_WRITE_ENCRYPT_MODE, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200291}
292
293static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
294{
295 __u8 status = *((__u8 *) skb->data);
296 void *sent;
297
298 BT_DBG("%s status 0x%x", hdev->name, status);
299
300 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SCAN_ENABLE);
301 if (!sent)
302 return;
303
304 if (!status) {
305 __u8 param = *((__u8 *) sent);
Johan Hedberg9fbcbb42010-12-30 00:18:33 +0200306 int old_pscan, old_iscan;
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800307 hci_dev_lock(hdev);
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);
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800325 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200326 }
327
Johan Hedberg23bb5762010-12-21 23:01:27 +0200328 hci_req_complete(hdev, HCI_OP_WRITE_SCAN_ENABLE, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200329}
330
331static void hci_cc_read_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
332{
333 struct hci_rp_read_class_of_dev *rp = (void *) skb->data;
334
335 BT_DBG("%s status 0x%x", hdev->name, rp->status);
336
337 if (rp->status)
338 return;
339
340 memcpy(hdev->dev_class, rp->dev_class, 3);
341
342 BT_DBG("%s class 0x%.2x%.2x%.2x", hdev->name,
343 hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
344}
345
346static void hci_cc_write_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
347{
348 __u8 status = *((__u8 *) skb->data);
349 void *sent;
350
351 BT_DBG("%s status 0x%x", hdev->name, status);
352
Marcel Holtmannf383f272008-07-14 20:13:47 +0200353 if (status)
354 return;
355
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200356 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_CLASS_OF_DEV);
357 if (!sent)
358 return;
359
Marcel Holtmannf383f272008-07-14 20:13:47 +0200360 memcpy(hdev->dev_class, sent, 3);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200361}
362
363static void hci_cc_read_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
364{
365 struct hci_rp_read_voice_setting *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 __u16 setting;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200367
368 BT_DBG("%s status 0x%x", hdev->name, rp->status);
369
370 if (rp->status)
371 return;
372
373 setting = __le16_to_cpu(rp->voice_setting);
374
Marcel Holtmannf383f272008-07-14 20:13:47 +0200375 if (hdev->voice_setting == setting)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200376 return;
377
378 hdev->voice_setting = setting;
379
380 BT_DBG("%s voice setting 0x%04x", hdev->name, setting);
381
382 if (hdev->notify) {
383 tasklet_disable(&hdev->tx_task);
384 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
385 tasklet_enable(&hdev->tx_task);
386 }
387}
388
389static void hci_cc_write_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
390{
391 __u8 status = *((__u8 *) skb->data);
Marcel Holtmannf383f272008-07-14 20:13:47 +0200392 __u16 setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 void *sent;
394
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200395 BT_DBG("%s status 0x%x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Marcel Holtmannf383f272008-07-14 20:13:47 +0200397 if (status)
398 return;
399
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200400 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_VOICE_SETTING);
401 if (!sent)
402 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Marcel Holtmannf383f272008-07-14 20:13:47 +0200404 setting = get_unaligned_le16(sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Marcel Holtmannf383f272008-07-14 20:13:47 +0200406 if (hdev->voice_setting == setting)
407 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Marcel Holtmannf383f272008-07-14 20:13:47 +0200409 hdev->voice_setting = setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Marcel Holtmannf383f272008-07-14 20:13:47 +0200411 BT_DBG("%s voice setting 0x%04x", hdev->name, setting);
412
413 if (hdev->notify) {
414 tasklet_disable(&hdev->tx_task);
415 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
416 tasklet_enable(&hdev->tx_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418}
419
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200420static void hci_cc_host_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200422 __u8 status = *((__u8 *) skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200424 BT_DBG("%s status 0x%x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Johan Hedberg23bb5762010-12-21 23:01:27 +0200426 hci_req_complete(hdev, HCI_OP_HOST_BUFFER_SIZE, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
Marcel Holtmann333140b2008-07-14 20:13:48 +0200429static void hci_cc_read_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
430{
431 struct hci_rp_read_ssp_mode *rp = (void *) skb->data;
432
433 BT_DBG("%s status 0x%x", hdev->name, rp->status);
434
435 if (rp->status)
436 return;
437
438 hdev->ssp_mode = rp->mode;
439}
440
441static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
442{
443 __u8 status = *((__u8 *) skb->data);
444 void *sent;
445
446 BT_DBG("%s status 0x%x", hdev->name, status);
447
448 if (status)
449 return;
450
451 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_MODE);
452 if (!sent)
453 return;
454
455 hdev->ssp_mode = *((__u8 *) sent);
456}
457
Johan Hedbergd5859e22011-01-25 01:19:58 +0200458static u8 hci_get_inquiry_mode(struct hci_dev *hdev)
459{
460 if (hdev->features[6] & LMP_EXT_INQ)
461 return 2;
462
463 if (hdev->features[3] & LMP_RSSI_INQ)
464 return 1;
465
466 if (hdev->manufacturer == 11 && hdev->hci_rev == 0x00 &&
467 hdev->lmp_subver == 0x0757)
468 return 1;
469
470 if (hdev->manufacturer == 15) {
471 if (hdev->hci_rev == 0x03 && hdev->lmp_subver == 0x6963)
472 return 1;
473 if (hdev->hci_rev == 0x09 && hdev->lmp_subver == 0x6963)
474 return 1;
475 if (hdev->hci_rev == 0x00 && hdev->lmp_subver == 0x6965)
476 return 1;
477 }
478
479 if (hdev->manufacturer == 31 && hdev->hci_rev == 0x2005 &&
480 hdev->lmp_subver == 0x1805)
481 return 1;
482
483 return 0;
484}
485
486static void hci_setup_inquiry_mode(struct hci_dev *hdev)
487{
488 u8 mode;
489
490 mode = hci_get_inquiry_mode(hdev);
491
492 hci_send_cmd(hdev, HCI_OP_WRITE_INQUIRY_MODE, 1, &mode);
493}
494
495static void hci_setup_event_mask(struct hci_dev *hdev)
496{
497 /* The second byte is 0xff instead of 0x9f (two reserved bits
498 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
499 * command otherwise */
500 u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
501
Brian Gixa68668b2011-08-11 15:49:36 -0700502 BT_DBG("");
503
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700504 /* Events for 1.2 and newer controllers */
505 if (hdev->lmp_ver > 1) {
506 events[4] |= 0x01; /* Flow Specification Complete */
507 events[4] |= 0x02; /* Inquiry Result with RSSI */
508 events[4] |= 0x04; /* Read Remote Extended Features Complete */
509 events[5] |= 0x08; /* Synchronous Connection Complete */
510 events[5] |= 0x10; /* Synchronous Connection Changed */
511 }
Johan Hedbergd5859e22011-01-25 01:19:58 +0200512
513 if (hdev->features[3] & LMP_RSSI_INQ)
514 events[4] |= 0x04; /* Inquiry Result with RSSI */
515
516 if (hdev->features[5] & LMP_SNIFF_SUBR)
517 events[5] |= 0x20; /* Sniff Subrating */
518
519 if (hdev->features[5] & LMP_PAUSE_ENC)
520 events[5] |= 0x80; /* Encryption Key Refresh Complete */
521
522 if (hdev->features[6] & LMP_EXT_INQ)
523 events[5] |= 0x40; /* Extended Inquiry Result */
524
525 if (hdev->features[6] & LMP_NO_FLUSH)
526 events[7] |= 0x01; /* Enhanced Flush Complete */
527
528 if (hdev->features[7] & LMP_LSTO)
529 events[6] |= 0x80; /* Link Supervision Timeout Changed */
530
531 if (hdev->features[6] & LMP_SIMPLE_PAIR) {
532 events[6] |= 0x01; /* IO Capability Request */
533 events[6] |= 0x02; /* IO Capability Response */
534 events[6] |= 0x04; /* User Confirmation Request */
535 events[6] |= 0x08; /* User Passkey Request */
536 events[6] |= 0x10; /* Remote OOB Data Request */
537 events[6] |= 0x20; /* Simple Pairing Complete */
538 events[7] |= 0x04; /* User Passkey Notification */
539 events[7] |= 0x08; /* Keypress Notification */
540 events[7] |= 0x10; /* Remote Host Supported
541 * Features Notification */
542 }
543
544 if (hdev->features[4] & LMP_LE)
545 events[7] |= 0x20; /* LE Meta-Event */
546
547 hci_send_cmd(hdev, HCI_OP_SET_EVENT_MASK, sizeof(events), events);
548}
549
550static void hci_setup(struct hci_dev *hdev)
551{
Johan Hedbergd5859e22011-01-25 01:19:58 +0200552 if (hdev->lmp_ver > 1)
553 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL);
554
555 if (hdev->features[6] & LMP_SIMPLE_PAIR) {
556 u8 mode = 0x01;
557 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, sizeof(mode), &mode);
558 }
559
560 if (hdev->features[3] & LMP_RSSI_INQ)
561 hci_setup_inquiry_mode(hdev);
562
563 if (hdev->features[7] & LMP_INQ_TX_PWR)
564 hci_send_cmd(hdev, HCI_OP_READ_INQ_RSP_TX_POWER, 0, NULL);
565}
566
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200567static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
568{
569 struct hci_rp_read_local_version *rp = (void *) skb->data;
570
571 BT_DBG("%s status 0x%x", hdev->name, rp->status);
572
573 if (rp->status)
574 return;
575
576 hdev->hci_ver = rp->hci_ver;
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200577 hdev->hci_rev = __le16_to_cpu(rp->hci_rev);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200578 hdev->lmp_ver = rp->lmp_ver;
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200579 hdev->manufacturer = __le16_to_cpu(rp->manufacturer);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200580 hdev->lmp_subver = __le16_to_cpu(rp->lmp_subver);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200581
582 BT_DBG("%s manufacturer %d hci ver %d:%d", hdev->name,
583 hdev->manufacturer,
584 hdev->hci_ver, hdev->hci_rev);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200585
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700586 if (hdev->dev_type == HCI_BREDR && test_bit(HCI_INIT, &hdev->flags))
Johan Hedbergd5859e22011-01-25 01:19:58 +0200587 hci_setup(hdev);
588}
589
590static void hci_setup_link_policy(struct hci_dev *hdev)
591{
592 u16 link_policy = 0;
593
594 if (hdev->features[0] & LMP_RSWITCH)
595 link_policy |= HCI_LP_RSWITCH;
596 if (hdev->features[0] & LMP_HOLD)
597 link_policy |= HCI_LP_HOLD;
598 if (hdev->features[0] & LMP_SNIFF)
599 link_policy |= HCI_LP_SNIFF;
600 if (hdev->features[1] & LMP_PARK)
601 link_policy |= HCI_LP_PARK;
602
603 link_policy = cpu_to_le16(link_policy);
604 hci_send_cmd(hdev, HCI_OP_WRITE_DEF_LINK_POLICY,
605 sizeof(link_policy), &link_policy);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200606}
607
608static void hci_cc_read_local_commands(struct hci_dev *hdev, struct sk_buff *skb)
609{
610 struct hci_rp_read_local_commands *rp = (void *) skb->data;
611
612 BT_DBG("%s status 0x%x", hdev->name, rp->status);
613
614 if (rp->status)
Johan Hedbergd5859e22011-01-25 01:19:58 +0200615 goto done;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200616
617 memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
Johan Hedbergd5859e22011-01-25 01:19:58 +0200618
619 if (test_bit(HCI_INIT, &hdev->flags) && (hdev->commands[5] & 0x10))
620 hci_setup_link_policy(hdev);
621
622done:
623 hci_req_complete(hdev, HCI_OP_READ_LOCAL_COMMANDS, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200624}
625
626static void hci_cc_read_local_features(struct hci_dev *hdev, struct sk_buff *skb)
627{
628 struct hci_rp_read_local_features *rp = (void *) skb->data;
629
630 BT_DBG("%s status 0x%x", hdev->name, rp->status);
631
632 if (rp->status)
633 return;
634
635 memcpy(hdev->features, rp->features, 8);
636
Brian Gixa68668b2011-08-11 15:49:36 -0700637 if (hdev->dev_type == HCI_BREDR && test_bit(HCI_INIT, &hdev->flags)) {
638 if (hdev->features[6] & LMP_SIMPLE_PAIR) {
639 u8 mode = 0x01;
640 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE,
641 sizeof(mode), &mode);
642 }
643
644 if (hdev->features[3] & LMP_RSSI_INQ)
645 hci_setup_inquiry_mode(hdev);
646
647 if (hdev->features[7] & LMP_INQ_TX_PWR)
648 hci_send_cmd(hdev, HCI_OP_READ_INQ_RSP_TX_POWER,
649 0, NULL);
650
651 hci_setup_event_mask(hdev);
652 }
653
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200654 /* Adjust default settings according to features
655 * supported by device. */
656
657 if (hdev->features[0] & LMP_3SLOT)
658 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
659
660 if (hdev->features[0] & LMP_5SLOT)
661 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
662
663 if (hdev->features[1] & LMP_HV2) {
664 hdev->pkt_type |= (HCI_HV2);
665 hdev->esco_type |= (ESCO_HV2);
666 }
667
668 if (hdev->features[1] & LMP_HV3) {
669 hdev->pkt_type |= (HCI_HV3);
670 hdev->esco_type |= (ESCO_HV3);
671 }
672
673 if (hdev->features[3] & LMP_ESCO)
674 hdev->esco_type |= (ESCO_EV3);
675
676 if (hdev->features[4] & LMP_EV4)
677 hdev->esco_type |= (ESCO_EV4);
678
679 if (hdev->features[4] & LMP_EV5)
680 hdev->esco_type |= (ESCO_EV5);
681
Marcel Holtmannefc76882009-02-06 09:13:37 +0100682 if (hdev->features[5] & LMP_EDR_ESCO_2M)
683 hdev->esco_type |= (ESCO_2EV3);
684
685 if (hdev->features[5] & LMP_EDR_ESCO_3M)
686 hdev->esco_type |= (ESCO_3EV3);
687
688 if (hdev->features[5] & LMP_EDR_3S_ESCO)
689 hdev->esco_type |= (ESCO_2EV5 | ESCO_3EV5);
690
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200691 BT_DBG("%s features 0x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x", hdev->name,
692 hdev->features[0], hdev->features[1],
693 hdev->features[2], hdev->features[3],
694 hdev->features[4], hdev->features[5],
695 hdev->features[6], hdev->features[7]);
696}
697
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700698static void hci_cc_read_flow_control_mode(struct hci_dev *hdev,
699 struct sk_buff *skb)
Andre Guedesd5fa5132011-06-30 19:20:52 -0300700{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700701 struct hci_rp_read_flow_control_mode *rp = (void *) skb->data;
Andre Guedesd5fa5132011-06-30 19:20:52 -0300702
703 BT_DBG("%s status 0x%x", hdev->name, rp->status);
704
705 if (rp->status)
706 return;
707
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700708 hdev->flow_ctl_mode = rp->mode;
Andre Guedesd5fa5132011-06-30 19:20:52 -0300709}
710
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200711static void hci_cc_read_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
712{
713 struct hci_rp_read_buffer_size *rp = (void *) skb->data;
714
715 BT_DBG("%s status 0x%x", hdev->name, rp->status);
716
717 if (rp->status)
718 return;
719
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700720 if (hdev->flow_ctl_mode == HCI_PACKET_BASED_FLOW_CTL_MODE) {
721 hdev->acl_mtu = __le16_to_cpu(rp->acl_mtu);
722 hdev->sco_mtu = rp->sco_mtu;
723 hdev->acl_pkts = __le16_to_cpu(rp->acl_max_pkt);
724 hdev->sco_pkts = __le16_to_cpu(rp->sco_max_pkt);
725 hdev->acl_cnt = hdev->acl_pkts;
726 hdev->sco_cnt = hdev->sco_pkts;
727 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200728
729 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
730 hdev->sco_mtu = 64;
731 hdev->sco_pkts = 8;
732 }
733
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200734
735 BT_DBG("%s acl mtu %d:%d sco mtu %d:%d", hdev->name,
736 hdev->acl_mtu, hdev->acl_pkts,
737 hdev->sco_mtu, hdev->sco_pkts);
738}
739
740static void hci_cc_read_bd_addr(struct hci_dev *hdev, struct sk_buff *skb)
741{
742 struct hci_rp_read_bd_addr *rp = (void *) skb->data;
743
744 BT_DBG("%s status 0x%x", hdev->name, rp->status);
745
746 if (!rp->status)
747 bacpy(&hdev->bdaddr, &rp->bdaddr);
748
Johan Hedberg23bb5762010-12-21 23:01:27 +0200749 hci_req_complete(hdev, HCI_OP_READ_BD_ADDR, rp->status);
750}
751
752static void hci_cc_write_ca_timeout(struct hci_dev *hdev, struct sk_buff *skb)
753{
754 __u8 status = *((__u8 *) skb->data);
755
756 BT_DBG("%s status 0x%x", hdev->name, status);
757
758 hci_req_complete(hdev, HCI_OP_WRITE_CA_TIMEOUT, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200759}
760
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700761static void hci_cc_read_data_block_size(struct hci_dev *hdev,
762 struct sk_buff *skb)
763{
764 struct hci_rp_read_data_block_size *rp = (void *) skb->data;
765
766 BT_DBG("%s status 0x%x", hdev->name, rp->status);
767
768 if (rp->status)
769 return;
770
771 if (hdev->flow_ctl_mode == HCI_BLOCK_BASED_FLOW_CTL_MODE) {
772 hdev->acl_mtu = __le16_to_cpu(rp->max_acl_len);
773 hdev->sco_mtu = 0;
774 hdev->data_block_len = __le16_to_cpu(rp->data_block_len);
775 /* acl_pkts indicates the number of blocks */
776 hdev->acl_pkts = __le16_to_cpu(rp->num_blocks);
777 hdev->sco_pkts = 0;
778 hdev->acl_cnt = hdev->acl_pkts;
779 hdev->sco_cnt = 0;
780 }
781
782 BT_DBG("%s acl mtu %d:%d, data block len %d", hdev->name,
783 hdev->acl_mtu, hdev->acl_cnt, hdev->data_block_len);
784}
785
786static void hci_cc_read_local_amp_info(struct hci_dev *hdev,
787 struct sk_buff *skb)
788{
789 struct hci_rp_read_local_amp_info *rp = (void *) skb->data;
790
791 BT_DBG("%s status 0x%x", hdev->name, rp->status);
792
793 if (rp->status)
794 return;
795
796 hdev->amp_status = rp->amp_status;
797 hdev->amp_total_bw = __le32_to_cpu(rp->total_bw);
798 hdev->amp_max_bw = __le32_to_cpu(rp->max_bw);
799 hdev->amp_min_latency = __le32_to_cpu(rp->min_latency);
800 hdev->amp_max_pdu = __le32_to_cpu(rp->max_pdu);
801 hdev->amp_type = rp->amp_type;
802 hdev->amp_pal_cap = __le16_to_cpu(rp->pal_cap);
803 hdev->amp_assoc_size = __le16_to_cpu(rp->max_assoc_size);
804 hdev->amp_be_flush_to = __le32_to_cpu(rp->be_flush_to);
805 hdev->amp_max_flush_to = __le32_to_cpu(rp->max_flush_to);
806
807 hci_req_complete(hdev, HCI_OP_READ_LOCAL_AMP_INFO, rp->status);
808}
809
Johan Hedbergb0916ea2011-01-10 13:44:55 +0200810static void hci_cc_delete_stored_link_key(struct hci_dev *hdev,
811 struct sk_buff *skb)
812{
813 __u8 status = *((__u8 *) skb->data);
814
815 BT_DBG("%s status 0x%x", hdev->name, status);
816
817 hci_req_complete(hdev, HCI_OP_DELETE_STORED_LINK_KEY, status);
818}
819
Johan Hedbergd5859e22011-01-25 01:19:58 +0200820static void hci_cc_set_event_mask(struct hci_dev *hdev, struct sk_buff *skb)
821{
822 __u8 status = *((__u8 *) skb->data);
823
824 BT_DBG("%s status 0x%x", hdev->name, status);
825
826 hci_req_complete(hdev, HCI_OP_SET_EVENT_MASK, status);
827}
828
829static void hci_cc_write_inquiry_mode(struct hci_dev *hdev,
830 struct sk_buff *skb)
831{
832 __u8 status = *((__u8 *) skb->data);
833
834 BT_DBG("%s status 0x%x", hdev->name, status);
835
836 hci_req_complete(hdev, HCI_OP_WRITE_INQUIRY_MODE, status);
837}
838
839static void hci_cc_read_inq_rsp_tx_power(struct hci_dev *hdev,
840 struct sk_buff *skb)
841{
842 __u8 status = *((__u8 *) skb->data);
843
844 BT_DBG("%s status 0x%x", hdev->name, status);
845
846 hci_req_complete(hdev, HCI_OP_READ_INQ_RSP_TX_POWER, status);
847}
848
849static void hci_cc_set_event_flt(struct hci_dev *hdev, struct sk_buff *skb)
850{
851 __u8 status = *((__u8 *) skb->data);
852
853 BT_DBG("%s status 0x%x", hdev->name, status);
854
855 hci_req_complete(hdev, HCI_OP_SET_EVENT_FLT, status);
856}
857
Johan Hedberg980e1a52011-01-22 06:10:07 +0200858static void hci_cc_pin_code_reply(struct hci_dev *hdev, struct sk_buff *skb)
859{
860 struct hci_rp_pin_code_reply *rp = (void *) skb->data;
861 struct hci_cp_pin_code_reply *cp;
862 struct hci_conn *conn;
863
864 BT_DBG("%s status 0x%x", hdev->name, rp->status);
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800865 hci_dev_lock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200866
867 if (test_bit(HCI_MGMT, &hdev->flags))
868 mgmt_pin_code_reply_complete(hdev->id, &rp->bdaddr, rp->status);
869
870 if (rp->status != 0)
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800871 goto unlock;
Johan Hedberg980e1a52011-01-22 06:10:07 +0200872
873 cp = hci_sent_cmd_data(hdev, HCI_OP_PIN_CODE_REPLY);
874 if (!cp)
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800875 goto unlock;
Johan Hedberg980e1a52011-01-22 06:10:07 +0200876
877 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
878 if (conn)
879 conn->pin_length = cp->pin_len;
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800880unlock:
881 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200882}
883
884static void hci_cc_pin_code_neg_reply(struct hci_dev *hdev, struct sk_buff *skb)
885{
886 struct hci_rp_pin_code_neg_reply *rp = (void *) skb->data;
887
888 BT_DBG("%s status 0x%x", hdev->name, rp->status);
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800889 hci_dev_lock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200890
891 if (test_bit(HCI_MGMT, &hdev->flags))
892 mgmt_pin_code_neg_reply_complete(hdev->id, &rp->bdaddr,
893 rp->status);
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800894 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200895}
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300896static void hci_cc_le_read_buffer_size(struct hci_dev *hdev,
897 struct sk_buff *skb)
898{
899 struct hci_rp_le_read_buffer_size *rp = (void *) skb->data;
900
901 BT_DBG("%s status 0x%x", hdev->name, rp->status);
902
903 if (rp->status)
904 return;
905
906 hdev->le_mtu = __le16_to_cpu(rp->le_mtu);
907 hdev->le_pkts = rp->le_max_pkt;
908
909 hdev->le_cnt = hdev->le_pkts;
910
911 BT_DBG("%s le mtu %d:%d", hdev->name, hdev->le_mtu, hdev->le_pkts);
912
913 hci_req_complete(hdev, HCI_OP_LE_READ_BUFFER_SIZE, rp->status);
914}
Johan Hedberg980e1a52011-01-22 06:10:07 +0200915
Johan Hedberga5c29682011-02-19 12:05:57 -0300916static void hci_cc_user_confirm_reply(struct hci_dev *hdev, struct sk_buff *skb)
917{
918 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
919
920 BT_DBG("%s status 0x%x", hdev->name, rp->status);
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800921 hci_dev_lock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -0300922
923 if (test_bit(HCI_MGMT, &hdev->flags))
924 mgmt_user_confirm_reply_complete(hdev->id, &rp->bdaddr,
925 rp->status);
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800926 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -0300927}
928
929static void hci_cc_user_confirm_neg_reply(struct hci_dev *hdev,
930 struct sk_buff *skb)
931{
932 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
933
934 BT_DBG("%s status 0x%x", hdev->name, rp->status);
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800935 hci_dev_lock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -0300936
937 if (test_bit(HCI_MGMT, &hdev->flags))
938 mgmt_user_confirm_neg_reply_complete(hdev->id, &rp->bdaddr,
939 rp->status);
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800940 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -0300941}
942
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800943static void hci_cc_read_rssi(struct hci_dev *hdev, struct sk_buff *skb)
944{
945 struct hci_conn *conn;
946 struct hci_rp_read_rssi *rp = (void *) skb->data;
947
948 BT_DBG("%s status 0x%x", hdev->name, rp->status);
949
950 BT_DBG("%s rssi : %d handle : %d", hdev->name, rp->rssi, rp->handle);
951
952 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
953 if (conn)
954 mgmt_read_rssi_complete(hdev->id, rp->rssi, &conn->dst,
955 __le16_to_cpu(rp->handle), rp->status);
956}
957
Szymon Jancc35938b2011-03-22 13:12:21 +0100958static void hci_cc_read_local_oob_data_reply(struct hci_dev *hdev,
959 struct sk_buff *skb)
960{
961 struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
962
963 BT_DBG("%s status 0x%x", hdev->name, rp->status);
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800964 hci_dev_lock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +0100965
966 mgmt_read_local_oob_data_reply_complete(hdev->id, rp->hash,
967 rp->randomizer, rp->status);
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800968 hci_dev_unlock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +0100969}
970
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -0300971static void hci_cc_le_ltk_reply(struct hci_dev *hdev, struct sk_buff *skb)
972{
973 struct hci_rp_le_ltk_reply *rp = (void *) skb->data;
974
975 BT_DBG("%s status 0x%x", hdev->name, rp->status);
976
977 if (rp->status)
978 return;
979
980 hci_req_complete(hdev, HCI_OP_LE_LTK_REPLY, rp->status);
981}
982
983static void hci_cc_le_ltk_neg_reply(struct hci_dev *hdev, struct sk_buff *skb)
984{
985 struct hci_rp_le_ltk_neg_reply *rp = (void *) skb->data;
986
987 BT_DBG("%s status 0x%x", hdev->name, rp->status);
988
989 if (rp->status)
990 return;
991
992 hci_req_complete(hdev, HCI_OP_LE_LTK_NEG_REPLY, rp->status);
993}
994
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700995static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
996 struct sk_buff *skb)
Andre Guedese326af42011-06-30 19:20:53 -0300997{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700998 void *sent;
999 __u8 param_scan_enable;
Andre Guedese326af42011-06-30 19:20:53 -03001000 __u8 status = *((__u8 *) skb->data);
1001
Andre Guedese326af42011-06-30 19:20:53 -03001002 if (status)
1003 return;
1004
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001005 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_ENABLE);
1006 if (!sent)
1007 return;
1008
1009 param_scan_enable = *((__u8 *) sent);
1010 if (param_scan_enable == 0x01) {
1011 del_timer(&hdev->adv_timer);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001012 } else if (param_scan_enable == 0x00) {
1013 mod_timer(&hdev->adv_timer, jiffies + ADV_CLEAR_TIMEOUT);
1014 }
Andre Guedese326af42011-06-30 19:20:53 -03001015}
1016
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001017static inline void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
1018{
1019 BT_DBG("%s status 0x%x", hdev->name, status);
1020
1021 if (status) {
Johan Hedberg23bb5762010-12-21 23:01:27 +02001022 hci_req_complete(hdev, HCI_OP_INQUIRY, status);
Johan Hedberg314b2382011-04-27 10:29:57 -04001023
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001024 hci_conn_check_pending(hdev);
Brian Gixa68668b2011-08-11 15:49:36 -07001025 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001026 set_bit(HCI_INQUIRY, &hdev->flags);
Subramanian Srinivasana727a492011-11-30 13:06:07 -08001027 hci_dev_lock(hdev);
Brian Gixa68668b2011-08-11 15:49:36 -07001028 if (test_bit(HCI_MGMT, &hdev->flags))
1029 mgmt_inquiry_started(hdev->id);
Subramanian Srinivasana727a492011-11-30 13:06:07 -08001030 hci_dev_unlock(hdev);
Brian Gixa68668b2011-08-11 15:49:36 -07001031 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001032}
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034static inline void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
1035{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001036 struct hci_cp_create_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001039 BT_DBG("%s status 0x%x", hdev->name, status);
1040
1041 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_CONN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (!cp)
1043 return;
1044
1045 hci_dev_lock(hdev);
1046
1047 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
1048
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001049 BT_DBG("%s bdaddr %s conn %p", hdev->name, batostr(&cp->bdaddr), conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 if (status) {
1052 if (conn && conn->state == BT_CONNECT) {
Marcel Holtmann4c67bc72006-10-15 17:30:56 +02001053 if (status != 0x0c || conn->attempt > 2) {
1054 conn->state = BT_CLOSED;
1055 hci_proto_connect_cfm(conn, status);
1056 hci_conn_del(conn);
1057 } else
1058 conn->state = BT_CONNECT2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060 } else {
1061 if (!conn) {
Nick Pellybbcda3b2010-02-11 11:54:28 -08001062 conn = hci_conn_add(hdev, ACL_LINK, 0, &cp->bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (conn) {
1064 conn->out = 1;
1065 conn->link_mode |= HCI_LM_MASTER;
1066 } else
Gustavo F. Padovan893ef972010-07-18 15:13:37 -03001067 BT_ERR("No memory for new connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069 }
1070
1071 hci_dev_unlock(hdev);
1072}
1073
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001074static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001076 struct hci_cp_add_sco *cp;
1077 struct hci_conn *acl, *sco;
1078 __u16 handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001080 BT_DBG("%s status 0x%x", hdev->name, status);
1081
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001082 if (!status)
1083 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001085 cp = hci_sent_cmd_data(hdev, HCI_OP_ADD_SCO);
1086 if (!cp)
1087 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001089 handle = __le16_to_cpu(cp->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001091 BT_DBG("%s handle %d", hdev->name, handle);
Marcel Holtmann6bd57412006-11-18 22:14:22 +01001092
1093 hci_dev_lock(hdev);
1094
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001095 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001096 if (acl) {
1097 sco = acl->link;
1098 if (sco) {
1099 sco->state = BT_CLOSED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001100
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001101 hci_proto_connect_cfm(sco, status);
1102 hci_conn_del(sco);
1103 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001104 }
Marcel Holtmann6bd57412006-11-18 22:14:22 +01001105
1106 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
1108
Marcel Holtmannf8558552008-07-14 20:13:49 +02001109static void hci_cs_auth_requested(struct hci_dev *hdev, __u8 status)
1110{
1111 struct hci_cp_auth_requested *cp;
1112 struct hci_conn *conn;
1113
1114 BT_DBG("%s status 0x%x", hdev->name, status);
1115
Marcel Holtmannf8558552008-07-14 20:13:49 +02001116 cp = hci_sent_cmd_data(hdev, HCI_OP_AUTH_REQUESTED);
1117 if (!cp)
1118 return;
1119
1120 hci_dev_lock(hdev);
1121
1122 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1123 if (conn) {
Prabhakaran Mc7953ca72012-03-06 20:43:46 +05301124 if (status) {
1125 mgmt_auth_failed(hdev->id, &conn->dst, status);
1126 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
1127
1128 if (conn->state == BT_CONFIG) {
1129 conn->state = BT_CONNECTED;
1130 hci_proto_connect_cfm(conn, status);
1131 hci_conn_put(conn);
1132 } else {
1133 hci_auth_cfm(conn, status);
1134 hci_conn_hold(conn);
1135 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
1136 hci_conn_put(conn);
1137 }
1138
1139 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
1140 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
1141 hci_encrypt_cfm(conn, status, 0x00);
1142 }
Marcel Holtmannf8558552008-07-14 20:13:49 +02001143 }
Srinivas Krovvidi9ff51452011-09-27 19:25:02 +05301144 conn->auth_initiator = 1;
Marcel Holtmannf8558552008-07-14 20:13:49 +02001145 }
1146
1147 hci_dev_unlock(hdev);
1148}
1149
1150static void hci_cs_set_conn_encrypt(struct hci_dev *hdev, __u8 status)
1151{
1152 struct hci_cp_set_conn_encrypt *cp;
1153 struct hci_conn *conn;
1154
1155 BT_DBG("%s status 0x%x", hdev->name, status);
1156
1157 if (!status)
1158 return;
1159
1160 cp = hci_sent_cmd_data(hdev, HCI_OP_SET_CONN_ENCRYPT);
1161 if (!cp)
1162 return;
1163
1164 hci_dev_lock(hdev);
1165
1166 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1167 if (conn) {
1168 if (conn->state == BT_CONFIG) {
1169 hci_proto_connect_cfm(conn, status);
1170 hci_conn_put(conn);
1171 }
1172 }
1173
1174 hci_dev_unlock(hdev);
1175}
1176
Johan Hedberg127178d2010-11-18 22:22:29 +02001177static int hci_outgoing_auth_needed(struct hci_dev *hdev,
Szymon Janc138d22e2011-02-17 16:44:23 +01001178 struct hci_conn *conn)
Johan Hedberg392599b2010-11-18 22:22:28 +02001179{
Johan Hedberg392599b2010-11-18 22:22:28 +02001180 if (conn->state != BT_CONFIG || !conn->out)
1181 return 0;
1182
Johan Hedberg765c2a92011-01-19 12:06:52 +05301183 if (conn->pending_sec_level == BT_SECURITY_SDP)
Johan Hedberg392599b2010-11-18 22:22:28 +02001184 return 0;
1185
1186 /* Only request authentication for SSP connections or non-SSP
Prabhakaran Mc6001a712011-09-06 11:56:25 +05301187 * devices with sec_level >= BT_SECURITY_MEDIUM*/
1188 BT_DBG("Pending sec level is %d", conn->pending_sec_level);
Johan Hedberg392599b2010-11-18 22:22:28 +02001189 if (!(hdev->ssp_mode > 0 && conn->ssp_mode > 0) &&
Prabhakaran Mc6001a712011-09-06 11:56:25 +05301190 conn->pending_sec_level < BT_SECURITY_MEDIUM)
Johan Hedberg392599b2010-11-18 22:22:28 +02001191 return 0;
1192
Johan Hedberg392599b2010-11-18 22:22:28 +02001193 return 1;
1194}
1195
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001196static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
1197{
Johan Hedberg127178d2010-11-18 22:22:29 +02001198 struct hci_cp_remote_name_req *cp;
1199 struct hci_conn *conn;
1200
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001201 BT_DBG("%s status 0x%x", hdev->name, status);
Johan Hedberg127178d2010-11-18 22:22:29 +02001202
1203 /* If successful wait for the name req complete event before
1204 * checking for the need to do authentication */
1205 if (!status)
1206 return;
1207
1208 cp = hci_sent_cmd_data(hdev, HCI_OP_REMOTE_NAME_REQ);
1209 if (!cp)
1210 return;
1211
1212 hci_dev_lock(hdev);
1213
1214 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001215 if (conn && hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02001216 struct hci_cp_auth_requested cp;
1217 cp.handle = __cpu_to_le16(conn->handle);
1218 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
1219 }
1220
1221 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001222}
1223
Marcel Holtmann769be972008-07-14 20:13:49 +02001224static void hci_cs_read_remote_features(struct hci_dev *hdev, __u8 status)
1225{
1226 struct hci_cp_read_remote_features *cp;
1227 struct hci_conn *conn;
1228
1229 BT_DBG("%s status 0x%x", hdev->name, status);
1230
1231 if (!status)
1232 return;
1233
1234 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_FEATURES);
1235 if (!cp)
1236 return;
1237
1238 hci_dev_lock(hdev);
1239
1240 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1241 if (conn) {
1242 if (conn->state == BT_CONFIG) {
Marcel Holtmann769be972008-07-14 20:13:49 +02001243 hci_proto_connect_cfm(conn, status);
1244 hci_conn_put(conn);
1245 }
1246 }
1247
1248 hci_dev_unlock(hdev);
1249}
1250
1251static void hci_cs_read_remote_ext_features(struct hci_dev *hdev, __u8 status)
1252{
1253 struct hci_cp_read_remote_ext_features *cp;
1254 struct hci_conn *conn;
1255
1256 BT_DBG("%s status 0x%x", hdev->name, status);
1257
1258 if (!status)
1259 return;
1260
1261 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES);
1262 if (!cp)
1263 return;
1264
1265 hci_dev_lock(hdev);
1266
1267 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1268 if (conn) {
1269 if (conn->state == BT_CONFIG) {
Marcel Holtmann769be972008-07-14 20:13:49 +02001270 hci_proto_connect_cfm(conn, status);
1271 hci_conn_put(conn);
1272 }
1273 }
1274
1275 hci_dev_unlock(hdev);
1276}
1277
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001278static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
1279{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001280 struct hci_cp_setup_sync_conn *cp;
1281 struct hci_conn *acl, *sco;
1282 __u16 handle;
1283
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001284 BT_DBG("%s status 0x%x", hdev->name, status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001285
1286 if (!status)
1287 return;
1288
1289 cp = hci_sent_cmd_data(hdev, HCI_OP_SETUP_SYNC_CONN);
1290 if (!cp)
1291 return;
1292
1293 handle = __le16_to_cpu(cp->handle);
1294
1295 BT_DBG("%s handle %d", hdev->name, handle);
1296
1297 hci_dev_lock(hdev);
1298
1299 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001300 if (acl) {
1301 sco = acl->link;
1302 if (sco) {
1303 sco->state = BT_CLOSED;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001304
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001305 hci_proto_connect_cfm(sco, status);
1306 hci_conn_del(sco);
1307 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001308 }
1309
1310 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001311}
1312
1313static void hci_cs_sniff_mode(struct hci_dev *hdev, __u8 status)
1314{
1315 struct hci_cp_sniff_mode *cp;
1316 struct hci_conn *conn;
1317
1318 BT_DBG("%s status 0x%x", hdev->name, status);
1319
1320 if (!status)
1321 return;
1322
1323 cp = hci_sent_cmd_data(hdev, HCI_OP_SNIFF_MODE);
1324 if (!cp)
1325 return;
1326
1327 hci_dev_lock(hdev);
1328
1329 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001330 if (conn) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001331 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend);
1332
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001333 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->pend))
1334 hci_sco_setup(conn, status);
1335 }
1336
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001337 hci_dev_unlock(hdev);
1338}
1339
1340static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
1341{
1342 struct hci_cp_exit_sniff_mode *cp;
1343 struct hci_conn *conn;
1344
1345 BT_DBG("%s status 0x%x", hdev->name, status);
1346
1347 if (!status)
1348 return;
1349
1350 cp = hci_sent_cmd_data(hdev, HCI_OP_EXIT_SNIFF_MODE);
1351 if (!cp)
1352 return;
1353
1354 hci_dev_lock(hdev);
1355
1356 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001357 if (conn) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001358 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend);
1359
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001360 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->pend))
1361 hci_sco_setup(conn, status);
1362 }
1363
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001364 hci_dev_unlock(hdev);
1365}
1366
Ville Tervofcd89c02011-02-10 22:38:47 -03001367static void hci_cs_le_create_conn(struct hci_dev *hdev, __u8 status)
1368{
1369 struct hci_cp_le_create_conn *cp;
1370 struct hci_conn *conn;
Brian Gixa94b6122012-02-23 16:07:10 -08001371 unsigned long exp = msecs_to_jiffies(5000);
Ville Tervofcd89c02011-02-10 22:38:47 -03001372
1373 BT_DBG("%s status 0x%x", hdev->name, status);
1374
1375 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_CREATE_CONN);
1376 if (!cp)
1377 return;
1378
1379 hci_dev_lock(hdev);
1380
1381 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->peer_addr);
1382
1383 BT_DBG("%s bdaddr %s conn %p", hdev->name, batostr(&cp->peer_addr),
1384 conn);
1385
1386 if (status) {
1387 if (conn && conn->state == BT_CONNECT) {
1388 conn->state = BT_CLOSED;
1389 hci_proto_connect_cfm(conn, status);
1390 hci_conn_del(conn);
1391 }
1392 } else {
1393 if (!conn) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001394 conn = hci_le_conn_add(hdev, &cp->peer_addr,
1395 cp->peer_addr_type);
1396 if (conn)
Ville Tervofcd89c02011-02-10 22:38:47 -03001397 conn->out = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001398 else
Ville Tervofcd89c02011-02-10 22:38:47 -03001399 BT_ERR("No memory for new connection");
Brian Gixa94b6122012-02-23 16:07:10 -08001400 } else
1401 exp = msecs_to_jiffies(conn->conn_timeout * 1000);
Brian Gix114f3a62011-09-27 14:02:20 -07001402
Brian Gixa94b6122012-02-23 16:07:10 -08001403 if (conn && exp)
1404 mod_timer(&conn->disc_timer, jiffies + exp);
Ville Tervofcd89c02011-02-10 22:38:47 -03001405 }
1406
1407 hci_dev_unlock(hdev);
1408}
1409
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001410static void hci_cs_accept_logical_link(struct hci_dev *hdev, __u8 status)
1411{
1412 struct hci_cp_create_logical_link *ap;
1413 struct hci_chan *chan;
1414
1415 BT_DBG("%s status 0x%x", hdev->name, status);
1416
1417 ap = hci_sent_cmd_data(hdev, HCI_OP_ACCEPT_LOGICAL_LINK);
1418 if (!ap)
1419 return;
1420
1421 hci_dev_lock(hdev);
1422
1423 chan = hci_chan_list_lookup_id(hdev, ap->phy_handle);
1424
1425 BT_DBG("%s chan %p", hdev->name, chan);
1426
1427 if (status) {
1428 if (chan && chan->state == BT_CONNECT) {
1429 chan->state = BT_CLOSED;
1430 hci_proto_create_cfm(chan, status);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001431 }
Mat Martineau9f8d4672011-12-14 12:10:46 -08001432 } else if (chan) {
1433 chan->state = BT_CONNECT2;
1434 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001435
1436 hci_dev_unlock(hdev);
1437}
1438
1439static void hci_cs_create_logical_link(struct hci_dev *hdev, __u8 status)
1440{
1441 struct hci_cp_create_logical_link *cp;
1442 struct hci_chan *chan;
1443
1444 BT_DBG("%s status 0x%x", hdev->name, status);
1445
1446 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_LOGICAL_LINK);
1447 if (!cp)
1448 return;
1449
1450 hci_dev_lock(hdev);
1451
1452 chan = hci_chan_list_lookup_id(hdev, cp->phy_handle);
1453
1454 BT_DBG("%s chan %p", hdev->name, chan);
1455
1456 if (status) {
1457 if (chan && chan->state == BT_CONNECT) {
1458 chan->state = BT_CLOSED;
1459 hci_proto_create_cfm(chan, status);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001460 }
1461 } else if (chan)
1462 chan->state = BT_CONNECT2;
1463
1464 hci_dev_unlock(hdev);
1465}
1466
1467static void hci_cs_flow_spec_modify(struct hci_dev *hdev, __u8 status)
1468{
1469 struct hci_cp_flow_spec_modify *cp;
1470 struct hci_chan *chan;
1471
1472 BT_DBG("%s status 0x%x", hdev->name, status);
1473
1474 cp = hci_sent_cmd_data(hdev, HCI_OP_FLOW_SPEC_MODIFY);
1475 if (!cp)
1476 return;
1477
1478 hci_dev_lock(hdev);
1479
1480 chan = hci_chan_list_lookup_handle(hdev, cp->log_handle);
1481 if (chan) {
1482 if (status)
1483 hci_proto_modify_cfm(chan, status);
1484 else {
1485 chan->tx_fs = cp->tx_fs;
1486 chan->rx_fs = cp->rx_fs;
1487 }
1488 }
1489
1490 hci_dev_unlock(hdev);
1491}
1492
1493static void hci_cs_disconn_logical_link(struct hci_dev *hdev, __u8 status)
1494{
1495 struct hci_cp_disconn_logical_link *cp;
1496 struct hci_chan *chan;
1497
1498 if (!status)
1499 return;
1500
1501 BT_DBG("%s status 0x%x", hdev->name, status);
1502
1503 cp = hci_sent_cmd_data(hdev, HCI_OP_DISCONN_LOGICAL_LINK);
1504 if (!cp)
1505 return;
1506
1507 hci_dev_lock(hdev);
1508
1509 chan = hci_chan_list_lookup_handle(hdev, cp->log_handle);
1510 if (chan)
1511 hci_chan_del(chan);
1512
1513 hci_dev_unlock(hdev);
1514}
1515
1516static void hci_cs_disconn_physical_link(struct hci_dev *hdev, __u8 status)
1517{
1518 struct hci_cp_disconn_phys_link *cp;
1519 struct hci_conn *conn;
1520
1521 if (!status)
1522 return;
1523
1524 BT_DBG("%s status 0x%x", hdev->name, status);
1525
1526 cp = hci_sent_cmd_data(hdev, HCI_OP_DISCONN_PHYS_LINK);
1527 if (!cp)
1528 return;
1529
1530 hci_dev_lock(hdev);
1531
1532 conn = hci_conn_hash_lookup_handle(hdev, cp->phy_handle);
1533 if (conn) {
1534 conn->state = BT_CLOSED;
1535 hci_conn_del(conn);
1536 }
1537
1538 hci_dev_unlock(hdev);
1539}
1540
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03001541static void hci_cs_le_start_enc(struct hci_dev *hdev, u8 status)
1542{
1543 BT_DBG("%s status 0x%x", hdev->name, status);
1544}
1545
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001546static inline void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1547{
1548 __u8 status = *((__u8 *) skb->data);
1549
1550 BT_DBG("%s status %d", hdev->name, status);
1551
Royston Rodrigues1670f382012-02-28 21:39:10 +05301552 if (!hdev->disco_state)
Brian Gixa68668b2011-08-11 15:49:36 -07001553 clear_bit(HCI_INQUIRY, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001554
Johan Hedberg23bb5762010-12-21 23:01:27 +02001555 hci_req_complete(hdev, HCI_OP_INQUIRY, status);
Subramanian Srinivasana727a492011-11-30 13:06:07 -08001556 hci_dev_lock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001557
Brian Gixa68668b2011-08-11 15:49:36 -07001558 if (test_bit(HCI_MGMT, &hdev->flags))
1559 mgmt_inquiry_complete_evt(hdev->id, status);
Subramanian Srinivasana727a492011-11-30 13:06:07 -08001560 hci_dev_unlock(hdev);
Brian Gixa68668b2011-08-11 15:49:36 -07001561
1562 if (!lmp_le_capable(hdev))
1563 hci_conn_check_pending(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001564}
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566static inline void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
1567{
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001568 struct inquiry_data data;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001569 struct inquiry_info *info = (void *) (skb->data + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 int num_rsp = *((__u8 *) skb->data);
1571
1572 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
1573
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001574 if (!num_rsp)
1575 return;
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001578
Johan Hedberge17acd42011-03-30 23:57:16 +03001579 for (; num_rsp; num_rsp--, info++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 bacpy(&data.bdaddr, &info->bdaddr);
1581 data.pscan_rep_mode = info->pscan_rep_mode;
1582 data.pscan_period_mode = info->pscan_period_mode;
1583 data.pscan_mode = info->pscan_mode;
1584 memcpy(data.dev_class, info->dev_class, 3);
1585 data.clock_offset = info->clock_offset;
1586 data.rssi = 0x00;
Marcel Holtmann41a96212008-07-14 20:13:48 +02001587 data.ssp_mode = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 hci_inquiry_cache_update(hdev, &data);
Brian Gixa68668b2011-08-11 15:49:36 -07001589 mgmt_device_found(hdev->id, &info->bdaddr, 0, 0,
1590 info->dev_class, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 hci_dev_unlock(hdev);
1594}
1595
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001596static inline void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001598 struct hci_ev_conn_complete *ev = (void *) skb->data;
1599 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001601 BT_DBG("%s", hdev->name);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001604
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001605 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann94992372009-04-19 19:30:03 +02001606 if (!conn) {
1607 if (ev->link_type != SCO_LINK)
1608 goto unlock;
1609
1610 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
1611 if (!conn)
1612 goto unlock;
1613
1614 conn->type = SCO_LINK;
1615 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001616
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001617 if (!ev->status) {
1618 conn->handle = __le16_to_cpu(ev->handle);
Marcel Holtmann769be972008-07-14 20:13:49 +02001619
1620 if (conn->type == ACL_LINK) {
1621 conn->state = BT_CONFIG;
1622 hci_conn_hold(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02001623 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Brian Gix2e2f50d2011-09-13 12:36:04 -07001624 mgmt_connected(hdev->id, &ev->bdaddr, 0);
Brian Gixa68668b2011-08-11 15:49:36 -07001625 } else if (conn->type == LE_LINK) {
1626 conn->state = BT_CONNECTED;
1627 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Brian Gix2e2f50d2011-09-13 12:36:04 -07001628 mgmt_connected(hdev->id, &ev->bdaddr, 1);
Marcel Holtmann769be972008-07-14 20:13:49 +02001629 } else
1630 conn->state = BT_CONNECTED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001631
Marcel Holtmann9eba32b2009-08-22 14:19:26 -07001632 hci_conn_hold_device(conn);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02001633 hci_conn_add_sysfs(conn);
1634
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001635 if (test_bit(HCI_AUTH, &hdev->flags))
1636 conn->link_mode |= HCI_LM_AUTH;
1637
1638 if (test_bit(HCI_ENCRYPT, &hdev->flags))
1639 conn->link_mode |= HCI_LM_ENCRYPT;
1640
Srinivas Krovvidid352b262012-01-12 19:46:26 +05301641 /* Get remote version */
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001642 if (conn->type == ACL_LINK) {
Srinivas Krovvidid352b262012-01-12 19:46:26 +05301643 struct hci_cp_read_remote_version cp;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001644 cp.handle = ev->handle;
Srinivas Krovvidid352b262012-01-12 19:46:26 +05301645 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_VERSION,
1646 sizeof(cp), &cp);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001647 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001648
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001649 /* Set packet type for incoming connection */
Marcel Holtmanna8746412008-07-14 20:13:46 +02001650 if (!conn->out && hdev->hci_ver < 3) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001651 struct hci_cp_change_conn_ptype cp;
1652 cp.handle = ev->handle;
Marcel Holtmanna8746412008-07-14 20:13:46 +02001653 cp.pkt_type = cpu_to_le16(conn->pkt_type);
1654 hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE,
1655 sizeof(cp), &cp);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001656 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02001657 } else {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001658 conn->state = BT_CLOSED;
Brian Gixa68668b2011-08-11 15:49:36 -07001659 if (conn->type == ACL_LINK || conn->type == LE_LINK)
Johan Hedberg17d5c042011-01-22 06:09:08 +02001660 mgmt_connect_failed(hdev->id, &ev->bdaddr, ev->status);
1661 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001662
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001663 if (conn->type == ACL_LINK)
1664 hci_sco_setup(conn, ev->status);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001665
Marcel Holtmann769be972008-07-14 20:13:49 +02001666 if (ev->status) {
1667 hci_proto_connect_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001668 hci_conn_del(conn);
Marcel Holtmannc89b6e62009-01-15 21:57:03 +01001669 } else if (ev->link_type != ACL_LINK)
1670 hci_proto_connect_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001671
1672unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001674
1675 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676}
1677
hyungseoung.yoodbb18fb2011-11-18 13:57:01 +09001678static inline bool is_sco_active(struct hci_dev *hdev)
1679{
1680 if (hci_conn_hash_lookup_state(hdev, SCO_LINK, BT_CONNECTED) ||
1681 (hci_conn_hash_lookup_state(hdev, ESCO_LINK,
1682 BT_CONNECTED)))
1683 return true;
1684 return false;
1685}
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687static inline void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
1688{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001689 struct hci_ev_conn_request *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 int mask = hdev->link_mode;
1691
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001692 BT_DBG("%s bdaddr %s type 0x%x", hdev->name,
1693 batostr(&ev->bdaddr), ev->link_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type);
1696
Szymon Janc138d22e2011-02-17 16:44:23 +01001697 if ((mask & HCI_LM_ACCEPT) &&
1698 !hci_blacklist_lookup(hdev, &ev->bdaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 /* Connection accepted */
Marcel Holtmannc7bdd502008-07-14 20:13:47 +02001700 struct inquiry_entry *ie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703 hci_dev_lock(hdev);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001704
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02001705 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
1706 if (ie)
Marcel Holtmannc7bdd502008-07-14 20:13:47 +02001707 memcpy(ie->data.dev_class, ev->dev_class, 3);
1708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
1710 if (!conn) {
Nick Pellybbcda3b2010-02-11 11:54:28 -08001711 /* pkt_type not yet used for incoming connections */
1712 conn = hci_conn_add(hdev, ev->link_type, 0, &ev->bdaddr);
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02001713 if (!conn) {
Gustavo F. Padovan893ef972010-07-18 15:13:37 -03001714 BT_ERR("No memory for new connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 hci_dev_unlock(hdev);
1716 return;
1717 }
1718 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 memcpy(conn->dev_class, ev->dev_class, 3);
Srinivas Krovvidi0916aed2011-12-20 12:06:34 +05301721 /* For incoming connection update remote class to userspace */
1722 mgmt_remote_class(hdev->id, &ev->bdaddr, ev->dev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 conn->state = BT_CONNECT;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 hci_dev_unlock(hdev);
1726
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001727 if (ev->link_type == ACL_LINK || !lmp_esco_capable(hdev)) {
1728 struct hci_cp_accept_conn_req cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001730 bacpy(&cp.bdaddr, &ev->bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
hyungseoung.yoodbb18fb2011-11-18 13:57:01 +09001732 if (lmp_rswitch_capable(hdev) && ((mask & HCI_LM_MASTER)
1733 || is_sco_active(hdev)))
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001734 cp.role = 0x00; /* Become master */
1735 else
1736 cp.role = 0x01; /* Remain slave */
1737
1738 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ,
1739 sizeof(cp), &cp);
1740 } else {
1741 struct hci_cp_accept_sync_conn_req cp;
1742
1743 bacpy(&cp.bdaddr, &ev->bdaddr);
Marcel Holtmanna8746412008-07-14 20:13:46 +02001744 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001745
1746 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
1747 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001748 cp.max_latency = cpu_to_le16(0x000A);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001749 cp.content_format = cpu_to_le16(hdev->voice_setting);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001750 cp.retrans_effort = 0x01;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001751
1752 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ,
1753 sizeof(cp), &cp);
1754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 } else {
1756 /* Connection rejected */
1757 struct hci_cp_reject_conn_req cp;
1758
1759 bacpy(&cp.bdaddr, &ev->bdaddr);
1760 cp.reason = 0x0f;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001761 hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
1763}
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765static inline void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1766{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001767 struct hci_ev_disconn_complete *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02001768 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 BT_DBG("%s status %d", hdev->name, ev->status);
1771
Johan Hedberg8962ee72011-01-20 12:40:27 +02001772 if (ev->status) {
Subramanian Srinivasana727a492011-11-30 13:06:07 -08001773 hci_dev_lock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02001774 mgmt_disconnect_failed(hdev->id);
Subramanian Srinivasana727a492011-11-30 13:06:07 -08001775 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return;
Johan Hedberg8962ee72011-01-20 12:40:27 +02001777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
1779 hci_dev_lock(hdev);
1780
Marcel Holtmann04837f62006-07-03 10:02:33 +02001781 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergf7520542011-01-20 12:34:39 +02001782 if (!conn)
1783 goto unlock;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02001784
Johan Hedbergf7520542011-01-20 12:34:39 +02001785 conn->state = BT_CLOSED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Brian Gixa68668b2011-08-11 15:49:36 -07001787 if (conn->type == ACL_LINK || conn->type == LE_LINK)
Johan Hedbergf7520542011-01-20 12:34:39 +02001788 mgmt_disconnected(hdev->id, &conn->dst);
1789
Brian Gixe9ceb522011-09-22 10:46:35 -07001790 if (conn->type == LE_LINK)
1791 del_timer(&conn->smp_timer);
1792
Mat Martineau3b9239a2012-02-16 11:54:30 -08001793 hci_proto_disconn_cfm(conn, ev->reason, 0);
Johan Hedbergf7520542011-01-20 12:34:39 +02001794 hci_conn_del(conn);
1795
1796unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 hci_dev_unlock(hdev);
1798}
1799
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001800static inline void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1801{
1802 struct hci_ev_auth_complete *ev = (void *) skb->data;
1803 struct hci_conn *conn;
1804
1805 BT_DBG("%s status %d", hdev->name, ev->status);
1806
1807 hci_dev_lock(hdev);
1808
1809 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001810 if (conn) {
1811 if (ev->status == 0x06 && hdev->ssp_mode > 0 &&
1812 conn->ssp_mode > 0) {
1813 struct hci_cp_auth_requested cp;
Prabhakaran Mcb04401d2011-09-20 17:37:55 +05301814 hci_remove_link_key(hdev, &conn->dst);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001815 cp.handle = cpu_to_le16(conn->handle);
Royston Rodrigues45df0f92012-05-30 10:26:42 +05301816 /*Initiates dedicated bonding as pin or key is missing
1817 on remote device*/
1818 /*In case if remote device is ssp supported,
1819 reduce the security level to MEDIUM if it is HIGH*/
1820 if (conn->ssp_mode && conn->auth_initiator &&
1821 conn->io_capability != 0x03) {
1822 conn->pending_sec_level = BT_SECURITY_HIGH;
1823 conn->auth_type = HCI_AT_DEDICATED_BONDING_MITM;
1824 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001825 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
1826 sizeof(cp), &cp);
1827 hci_dev_unlock(hdev);
1828 BT_INFO("Pin or key missing");
1829 return;
1830 }
Waldemar Rymarkiewicz54444292011-05-31 15:49:26 +02001831
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001832 if (!ev->status) {
Waldemar Rymarkiewicz54444292011-05-31 15:49:26 +02001833 conn->link_mode |= HCI_LM_AUTH;
1834 conn->sec_level = conn->pending_sec_level;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02001835 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001836 mgmt_auth_failed(hdev->id, &conn->dst, ev->status);
1837 conn->sec_level = BT_SECURITY_LOW;
1838 }
1839
1840 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
1841
1842 if (conn->state == BT_CONFIG) {
1843 if (!ev->status && hdev->ssp_mode > 0 &&
1844 conn->ssp_mode > 0) {
1845 struct hci_cp_set_conn_encrypt cp;
1846 cp.handle = ev->handle;
1847 cp.encrypt = 0x01;
1848 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT,
1849 sizeof(cp), &cp);
1850 } else {
1851 conn->state = BT_CONNECTED;
1852 hci_proto_connect_cfm(conn, ev->status);
1853 hci_conn_put(conn);
1854 }
1855 } else {
1856 hci_auth_cfm(conn, ev->status);
1857
1858 hci_conn_hold(conn);
1859 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02001860 hci_conn_put(conn);
1861 }
1862
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001863 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
1864 if (!ev->status) {
Prabhakaran Mc48584712012-04-20 15:47:58 +05301865 if (conn->link_mode & HCI_LM_ENCRYPT) {
1866 /* Encryption implies authentication */
1867 conn->link_mode |= HCI_LM_AUTH;
1868 conn->link_mode |= HCI_LM_ENCRYPT;
1869 conn->sec_level =
1870 conn->pending_sec_level;
1871 clear_bit(HCI_CONN_ENCRYPT_PEND,
1872 &conn->pend);
1873 hci_encrypt_cfm(conn, ev->status, 1);
1874
1875 if (test_bit(HCI_MGMT, &hdev->flags))
1876 mgmt_encrypt_change(hdev->id,
1877 &conn->dst,
1878 ev->status);
1879
1880 } else {
1881 struct hci_cp_set_conn_encrypt cp;
1882 cp.handle = ev->handle;
1883 cp.encrypt = 0x01;
1884 hci_send_cmd(hdev,
1885 HCI_OP_SET_CONN_ENCRYPT,
1886 sizeof(cp), &cp);
1887 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001888 } else {
1889 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
1890 hci_encrypt_cfm(conn, ev->status, 0x00);
1891 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001892 }
1893 }
1894
1895 hci_dev_unlock(hdev);
1896}
1897
1898static inline void hci_remote_name_evt(struct hci_dev *hdev, struct sk_buff *skb)
1899{
Johan Hedberg127178d2010-11-18 22:22:29 +02001900 struct hci_ev_remote_name *ev = (void *) skb->data;
1901 struct hci_conn *conn;
1902
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001903 BT_DBG("%s", hdev->name);
1904
1905 hci_conn_check_pending(hdev);
Johan Hedberg127178d2010-11-18 22:22:29 +02001906
1907 hci_dev_lock(hdev);
1908
Brian Gixa68668b2011-08-11 15:49:36 -07001909 if (test_bit(HCI_MGMT, &hdev->flags))
1910 mgmt_remote_name(hdev->id, &ev->bdaddr, ev->status, ev->name);
Johan Hedberga88a9652011-03-30 13:18:12 +03001911
Johan Hedberg127178d2010-11-18 22:22:29 +02001912 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001913 if (conn && hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02001914 struct hci_cp_auth_requested cp;
1915 cp.handle = __cpu_to_le16(conn->handle);
1916 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
1917 }
1918
1919 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001920}
1921
1922static inline void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
1923{
1924 struct hci_ev_encrypt_change *ev = (void *) skb->data;
1925 struct hci_conn *conn;
1926
1927 BT_DBG("%s status %d", hdev->name, ev->status);
1928
1929 hci_dev_lock(hdev);
1930
1931 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1932 if (conn) {
1933 if (!ev->status) {
Marcel Holtmannae293192008-07-14 20:13:45 +02001934 if (ev->encrypt) {
1935 /* Encryption implies authentication */
1936 conn->link_mode |= HCI_LM_AUTH;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001937 conn->link_mode |= HCI_LM_ENCRYPT;
Vinicius Costa Gomes64532ec2011-06-09 18:50:53 -03001938 conn->sec_level = conn->pending_sec_level;
Marcel Holtmannae293192008-07-14 20:13:45 +02001939 } else
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001940 conn->link_mode &= ~HCI_LM_ENCRYPT;
1941 }
1942
1943 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
1944
Marcel Holtmannf8558552008-07-14 20:13:49 +02001945 if (conn->state == BT_CONFIG) {
1946 if (!ev->status)
1947 conn->state = BT_CONNECTED;
1948
1949 hci_proto_connect_cfm(conn, ev->status);
1950 hci_conn_put(conn);
Prabhakaran Mcafface82012-04-10 11:38:35 +05301951 } else {
1952 /*
1953 * If the remote device does not support
1954 * Pause Encryption, usually during the
1955 * roleSwitch we see Encryption disable
1956 * for short duration. Allow remote device
1957 * to disable encryption
1958 * for short duration in this case.
1959 */
1960 if ((ev->encrypt == 0) && (ev->status == 0) &&
1961 ((conn->features[5] & LMP_PAUSE_ENC) == 0)) {
1962 mod_timer(&conn->encrypt_pause_timer,
1963 jiffies + msecs_to_jiffies(500));
1964 BT_INFO("enc pause timer, enc_pend_flag set");
1965 } else {
1966 del_timer(&conn->encrypt_pause_timer);
1967 hci_encrypt_cfm(conn, ev->status, ev->encrypt);
1968 }
1969 }
Prabhakaran Mc46230fa2011-11-30 18:11:21 +05301970
1971 if (test_bit(HCI_MGMT, &hdev->flags))
1972 mgmt_encrypt_change(hdev->id, &conn->dst, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001973 }
1974
1975 hci_dev_unlock(hdev);
1976}
1977
1978static inline void hci_change_link_key_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1979{
1980 struct hci_ev_change_link_key_complete *ev = (void *) skb->data;
1981 struct hci_conn *conn;
1982
1983 BT_DBG("%s status %d", hdev->name, ev->status);
1984
1985 hci_dev_lock(hdev);
1986
1987 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1988 if (conn) {
1989 if (!ev->status)
1990 conn->link_mode |= HCI_LM_SECURE;
1991
1992 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
1993
1994 hci_key_change_cfm(conn, ev->status);
1995 }
1996
1997 hci_dev_unlock(hdev);
1998}
1999
2000static inline void hci_remote_features_evt(struct hci_dev *hdev, struct sk_buff *skb)
2001{
2002 struct hci_ev_remote_features *ev = (void *) skb->data;
2003 struct hci_conn *conn;
2004
2005 BT_DBG("%s status %d", hdev->name, ev->status);
2006
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002007 hci_dev_lock(hdev);
2008
2009 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02002010 if (!conn)
2011 goto unlock;
Marcel Holtmann769be972008-07-14 20:13:49 +02002012
Sunny Kapdif3caf882012-02-25 19:27:09 -08002013 if (!ev->status) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02002014 memcpy(conn->features, ev->features, 8);
Sunny Kapdif3caf882012-02-25 19:27:09 -08002015 mgmt_remote_features(hdev->id, &conn->dst, ev->features);
2016 }
Johan Hedbergccd556f2010-11-10 17:11:51 +02002017
2018 if (conn->state != BT_CONFIG)
2019 goto unlock;
2020
2021 if (!ev->status && lmp_ssp_capable(hdev) && lmp_ssp_capable(conn)) {
2022 struct hci_cp_read_remote_ext_features cp;
2023 cp.handle = ev->handle;
2024 cp.page = 0x01;
2025 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES,
Marcel Holtmann769be972008-07-14 20:13:49 +02002026 sizeof(cp), &cp);
Johan Hedberg392599b2010-11-18 22:22:28 +02002027 goto unlock;
Prabhakaran Mc9f08bfd2012-03-26 18:18:17 +05302028 } else if (!(lmp_ssp_capable(conn)) && conn->auth_initiator &&
2029 (conn->pending_sec_level == BT_SECURITY_HIGH)) {
2030 conn->pending_sec_level = BT_SECURITY_MEDIUM;
Johan Hedberg392599b2010-11-18 22:22:28 +02002031 }
2032
Johan Hedberg127178d2010-11-18 22:22:29 +02002033 if (!ev->status) {
2034 struct hci_cp_remote_name_req cp;
2035 memset(&cp, 0, sizeof(cp));
2036 bacpy(&cp.bdaddr, &conn->dst);
2037 cp.pscan_rep_mode = 0x02;
2038 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
2039 }
Johan Hedberg392599b2010-11-18 22:22:28 +02002040
Johan Hedberg127178d2010-11-18 22:22:29 +02002041 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02002042 conn->state = BT_CONNECTED;
2043 hci_proto_connect_cfm(conn, ev->status);
2044 hci_conn_put(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02002045 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002046
Johan Hedbergccd556f2010-11-10 17:11:51 +02002047unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002048 hci_dev_unlock(hdev);
2049}
2050
2051static inline void hci_remote_version_evt(struct hci_dev *hdev, struct sk_buff *skb)
2052{
Srinivas Krovvidid352b262012-01-12 19:46:26 +05302053 struct hci_ev_remote_version *ev = (void *) skb->data;
2054 struct hci_cp_read_remote_features cp;
2055 struct hci_conn *conn;
2056 BT_DBG("%s status %d", hdev->name, ev->status);
2057
2058 hci_dev_lock(hdev);
2059 cp.handle = ev->handle;
2060 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES,
2061 sizeof(cp), &cp);
2062
2063 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
2064 if (!conn)
2065 goto unlock;
2066 if (!ev->status)
2067 mgmt_remote_version(hdev->id, &conn->dst, ev->lmp_ver,
2068 ev->manufacturer, ev->lmp_subver);
2069unlock:
2070 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002071}
2072
2073static inline void hci_qos_setup_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
2074{
2075 BT_DBG("%s", hdev->name);
2076}
2077
2078static inline void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
2079{
2080 struct hci_ev_cmd_complete *ev = (void *) skb->data;
2081 __u16 opcode;
2082
2083 skb_pull(skb, sizeof(*ev));
2084
2085 opcode = __le16_to_cpu(ev->opcode);
2086
Bhasker Neti6f4ceb12012-06-26 16:48:18 +05302087 if (test_bit(HCI_RESET, &hdev->flags) && (opcode != HCI_OP_RESET))
2088 return;
2089
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002090 switch (opcode) {
2091 case HCI_OP_INQUIRY_CANCEL:
2092 hci_cc_inquiry_cancel(hdev, skb);
2093 break;
2094
2095 case HCI_OP_EXIT_PERIODIC_INQ:
2096 hci_cc_exit_periodic_inq(hdev, skb);
2097 break;
2098
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002099 case HCI_OP_LINK_KEY_REPLY:
2100 hci_cc_link_key_reply(hdev, skb);
2101 break;
2102
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002103 case HCI_OP_REMOTE_NAME_REQ_CANCEL:
2104 hci_cc_remote_name_req_cancel(hdev, skb);
2105 break;
2106
2107 case HCI_OP_ROLE_DISCOVERY:
2108 hci_cc_role_discovery(hdev, skb);
2109 break;
2110
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02002111 case HCI_OP_READ_LINK_POLICY:
2112 hci_cc_read_link_policy(hdev, skb);
2113 break;
2114
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002115 case HCI_OP_WRITE_LINK_POLICY:
2116 hci_cc_write_link_policy(hdev, skb);
2117 break;
2118
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02002119 case HCI_OP_READ_DEF_LINK_POLICY:
2120 hci_cc_read_def_link_policy(hdev, skb);
2121 break;
2122
2123 case HCI_OP_WRITE_DEF_LINK_POLICY:
2124 hci_cc_write_def_link_policy(hdev, skb);
2125 break;
2126
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002127 case HCI_OP_RESET:
2128 hci_cc_reset(hdev, skb);
2129 break;
2130
2131 case HCI_OP_WRITE_LOCAL_NAME:
2132 hci_cc_write_local_name(hdev, skb);
2133 break;
2134
2135 case HCI_OP_READ_LOCAL_NAME:
2136 hci_cc_read_local_name(hdev, skb);
2137 break;
2138
2139 case HCI_OP_WRITE_AUTH_ENABLE:
2140 hci_cc_write_auth_enable(hdev, skb);
2141 break;
2142
2143 case HCI_OP_WRITE_ENCRYPT_MODE:
2144 hci_cc_write_encrypt_mode(hdev, skb);
2145 break;
2146
2147 case HCI_OP_WRITE_SCAN_ENABLE:
2148 hci_cc_write_scan_enable(hdev, skb);
2149 break;
2150
2151 case HCI_OP_READ_CLASS_OF_DEV:
2152 hci_cc_read_class_of_dev(hdev, skb);
2153 break;
2154
2155 case HCI_OP_WRITE_CLASS_OF_DEV:
2156 hci_cc_write_class_of_dev(hdev, skb);
2157 break;
2158
2159 case HCI_OP_READ_VOICE_SETTING:
2160 hci_cc_read_voice_setting(hdev, skb);
2161 break;
2162
2163 case HCI_OP_WRITE_VOICE_SETTING:
2164 hci_cc_write_voice_setting(hdev, skb);
2165 break;
2166
2167 case HCI_OP_HOST_BUFFER_SIZE:
2168 hci_cc_host_buffer_size(hdev, skb);
2169 break;
2170
Marcel Holtmann333140b2008-07-14 20:13:48 +02002171 case HCI_OP_READ_SSP_MODE:
2172 hci_cc_read_ssp_mode(hdev, skb);
2173 break;
2174
2175 case HCI_OP_WRITE_SSP_MODE:
2176 hci_cc_write_ssp_mode(hdev, skb);
2177 break;
2178
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002179 case HCI_OP_READ_LOCAL_VERSION:
2180 hci_cc_read_local_version(hdev, skb);
2181 break;
2182
2183 case HCI_OP_READ_LOCAL_COMMANDS:
2184 hci_cc_read_local_commands(hdev, skb);
2185 break;
2186
2187 case HCI_OP_READ_LOCAL_FEATURES:
2188 hci_cc_read_local_features(hdev, skb);
2189 break;
2190
2191 case HCI_OP_READ_BUFFER_SIZE:
2192 hci_cc_read_buffer_size(hdev, skb);
2193 break;
2194
2195 case HCI_OP_READ_BD_ADDR:
2196 hci_cc_read_bd_addr(hdev, skb);
2197 break;
2198
Johan Hedberg23bb5762010-12-21 23:01:27 +02002199 case HCI_OP_WRITE_CA_TIMEOUT:
2200 hci_cc_write_ca_timeout(hdev, skb);
2201 break;
2202
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002203 case HCI_OP_READ_FLOW_CONTROL_MODE:
2204 hci_cc_read_flow_control_mode(hdev, skb);
2205 break;
2206
2207 case HCI_OP_READ_DATA_BLOCK_SIZE:
2208 hci_cc_read_data_block_size(hdev, skb);
2209 break;
2210
2211 case HCI_OP_READ_LOCAL_AMP_INFO:
2212 hci_cc_read_local_amp_info(hdev, skb);
2213 break;
2214
2215 case HCI_OP_READ_LOCAL_AMP_ASSOC:
2216 case HCI_OP_WRITE_REMOTE_AMP_ASSOC:
2217 hci_amp_cmd_complete(hdev, opcode, skb);
2218 break;
2219
Johan Hedbergb0916ea2011-01-10 13:44:55 +02002220 case HCI_OP_DELETE_STORED_LINK_KEY:
2221 hci_cc_delete_stored_link_key(hdev, skb);
2222 break;
2223
Johan Hedbergd5859e22011-01-25 01:19:58 +02002224 case HCI_OP_SET_EVENT_MASK:
2225 hci_cc_set_event_mask(hdev, skb);
2226 break;
2227
2228 case HCI_OP_WRITE_INQUIRY_MODE:
2229 hci_cc_write_inquiry_mode(hdev, skb);
2230 break;
2231
2232 case HCI_OP_READ_INQ_RSP_TX_POWER:
2233 hci_cc_read_inq_rsp_tx_power(hdev, skb);
2234 break;
2235
2236 case HCI_OP_SET_EVENT_FLT:
2237 hci_cc_set_event_flt(hdev, skb);
2238 break;
2239
Johan Hedberg980e1a52011-01-22 06:10:07 +02002240 case HCI_OP_PIN_CODE_REPLY:
2241 hci_cc_pin_code_reply(hdev, skb);
2242 break;
2243
2244 case HCI_OP_PIN_CODE_NEG_REPLY:
2245 hci_cc_pin_code_neg_reply(hdev, skb);
2246 break;
2247
Szymon Jancc35938b2011-03-22 13:12:21 +01002248 case HCI_OP_READ_LOCAL_OOB_DATA:
2249 hci_cc_read_local_oob_data_reply(hdev, skb);
2250 break;
2251
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002252 case HCI_OP_LE_READ_BUFFER_SIZE:
2253 hci_cc_le_read_buffer_size(hdev, skb);
2254 break;
2255
Archana Ramachandran26a752b2011-12-20 11:27:40 -08002256 case HCI_OP_READ_RSSI:
2257 hci_cc_read_rssi(hdev, skb);
2258 break;
2259
Johan Hedberga5c29682011-02-19 12:05:57 -03002260 case HCI_OP_USER_CONFIRM_REPLY:
2261 hci_cc_user_confirm_reply(hdev, skb);
2262 break;
2263
2264 case HCI_OP_USER_CONFIRM_NEG_REPLY:
2265 hci_cc_user_confirm_neg_reply(hdev, skb);
2266 break;
2267
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03002268 case HCI_OP_LE_LTK_REPLY:
2269 hci_cc_le_ltk_reply(hdev, skb);
2270 break;
2271
2272 case HCI_OP_LE_LTK_NEG_REPLY:
2273 hci_cc_le_ltk_neg_reply(hdev, skb);
2274 break;
2275
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002276 case HCI_OP_LE_SET_SCAN_ENABLE:
2277 hci_cc_le_set_scan_enable(hdev, skb);
Andre Guedese326af42011-06-30 19:20:53 -03002278 break;
2279
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002280 default:
2281 BT_DBG("%s opcode 0x%x", hdev->name, opcode);
2282 break;
2283 }
2284
Ville Tervo6bd32322011-02-16 16:32:41 +02002285 if (ev->opcode != HCI_OP_NOP)
2286 del_timer(&hdev->cmd_timer);
2287
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002288 if (ev->ncmd) {
2289 atomic_set(&hdev->cmd_cnt, 1);
2290 if (!skb_queue_empty(&hdev->cmd_q))
Marcel Holtmannc78ae282009-11-18 01:02:54 +01002291 tasklet_schedule(&hdev->cmd_task);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002292 }
2293}
2294
2295static inline void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb)
2296{
2297 struct hci_ev_cmd_status *ev = (void *) skb->data;
2298 __u16 opcode;
2299
2300 skb_pull(skb, sizeof(*ev));
2301
2302 opcode = __le16_to_cpu(ev->opcode);
2303
2304 switch (opcode) {
2305 case HCI_OP_INQUIRY:
2306 hci_cs_inquiry(hdev, ev->status);
2307 break;
2308
2309 case HCI_OP_CREATE_CONN:
2310 hci_cs_create_conn(hdev, ev->status);
2311 break;
2312
2313 case HCI_OP_ADD_SCO:
2314 hci_cs_add_sco(hdev, ev->status);
2315 break;
2316
Marcel Holtmannf8558552008-07-14 20:13:49 +02002317 case HCI_OP_AUTH_REQUESTED:
2318 hci_cs_auth_requested(hdev, ev->status);
2319 break;
2320
2321 case HCI_OP_SET_CONN_ENCRYPT:
2322 hci_cs_set_conn_encrypt(hdev, ev->status);
2323 break;
2324
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002325 case HCI_OP_REMOTE_NAME_REQ:
2326 hci_cs_remote_name_req(hdev, ev->status);
2327 break;
2328
Marcel Holtmann769be972008-07-14 20:13:49 +02002329 case HCI_OP_READ_REMOTE_FEATURES:
2330 hci_cs_read_remote_features(hdev, ev->status);
2331 break;
2332
2333 case HCI_OP_READ_REMOTE_EXT_FEATURES:
2334 hci_cs_read_remote_ext_features(hdev, ev->status);
2335 break;
2336
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002337 case HCI_OP_SETUP_SYNC_CONN:
2338 hci_cs_setup_sync_conn(hdev, ev->status);
2339 break;
2340
2341 case HCI_OP_SNIFF_MODE:
2342 hci_cs_sniff_mode(hdev, ev->status);
2343 break;
2344
2345 case HCI_OP_EXIT_SNIFF_MODE:
2346 hci_cs_exit_sniff_mode(hdev, ev->status);
2347 break;
2348
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002349 case HCI_OP_CREATE_LOGICAL_LINK:
2350 hci_cs_create_logical_link(hdev, ev->status);
2351 break;
2352
2353 case HCI_OP_ACCEPT_LOGICAL_LINK:
2354 hci_cs_accept_logical_link(hdev, ev->status);
2355 break;
2356
2357 case HCI_OP_DISCONN_LOGICAL_LINK:
2358 hci_cs_disconn_logical_link(hdev, ev->status);
2359 break;
2360
2361 case HCI_OP_FLOW_SPEC_MODIFY:
2362 hci_cs_flow_spec_modify(hdev, ev->status);
2363 break;
2364
2365 case HCI_OP_CREATE_PHYS_LINK:
2366 case HCI_OP_ACCEPT_PHYS_LINK:
2367 hci_amp_cmd_status(hdev, opcode, ev->status);
2368 break;
2369
2370 case HCI_OP_DISCONN_PHYS_LINK:
2371 hci_cs_disconn_physical_link(hdev, ev->status);
2372
Johan Hedberg8962ee72011-01-20 12:40:27 +02002373 case HCI_OP_DISCONNECT:
2374 if (ev->status != 0)
2375 mgmt_disconnect_failed(hdev->id);
2376 break;
2377
Ville Tervofcd89c02011-02-10 22:38:47 -03002378 case HCI_OP_LE_CREATE_CONN:
2379 hci_cs_le_create_conn(hdev, ev->status);
2380 break;
2381
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03002382 case HCI_OP_LE_START_ENC:
2383 hci_cs_le_start_enc(hdev, ev->status);
2384 break;
2385
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002386 default:
2387 BT_DBG("%s opcode 0x%x", hdev->name, opcode);
2388 break;
2389 }
2390
Ville Tervo6bd32322011-02-16 16:32:41 +02002391 if (ev->opcode != HCI_OP_NOP)
2392 del_timer(&hdev->cmd_timer);
2393
Gustavo F. Padovan10572132011-03-16 15:36:29 -03002394 if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags)) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002395 atomic_set(&hdev->cmd_cnt, 1);
2396 if (!skb_queue_empty(&hdev->cmd_q))
Marcel Holtmannc78ae282009-11-18 01:02:54 +01002397 tasklet_schedule(&hdev->cmd_task);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002398 }
2399}
2400
2401static inline void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
2402{
2403 struct hci_ev_role_change *ev = (void *) skb->data;
2404 struct hci_conn *conn;
2405
2406 BT_DBG("%s status %d", hdev->name, ev->status);
2407
2408 hci_dev_lock(hdev);
2409
2410 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
2411 if (conn) {
2412 if (!ev->status) {
2413 if (ev->role)
2414 conn->link_mode &= ~HCI_LM_MASTER;
2415 else
2416 conn->link_mode |= HCI_LM_MASTER;
2417 }
2418
2419 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->pend);
2420
2421 hci_role_switch_cfm(conn, ev->status, ev->role);
2422 }
2423
2424 hci_dev_unlock(hdev);
2425}
2426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427static inline void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
2428{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002429 struct hci_ev_num_comp_pkts *ev = (void *) skb->data;
Marcel Holtmann1ebb9252005-11-08 09:57:21 -08002430 __le16 *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 int i;
2432
2433 skb_pull(skb, sizeof(*ev));
2434
2435 BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
2436
2437 if (skb->len < ev->num_hndl * 4) {
2438 BT_DBG("%s bad parameters", hdev->name);
2439 return;
2440 }
2441
2442 tasklet_disable(&hdev->tx_task);
2443
Marcel Holtmann1ebb9252005-11-08 09:57:21 -08002444 for (i = 0, ptr = (__le16 *) skb->data; i < ev->num_hndl; i++) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002445 struct hci_conn *conn = NULL;
2446 struct hci_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 __u16 handle, count;
2448
Harvey Harrison83985312008-05-02 16:25:46 -07002449 handle = get_unaligned_le16(ptr++);
2450 count = get_unaligned_le16(ptr++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002452 if (hdev->dev_type == HCI_BREDR)
2453 conn = hci_conn_hash_lookup_handle(hdev, handle);
2454 else {
2455 chan = hci_chan_list_lookup_handle(hdev, handle);
2456 if (chan)
2457 conn = chan->conn;
2458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 if (conn) {
2460 conn->sent -= count;
2461
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02002462 if (conn->type == ACL_LINK) {
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02002463 hdev->acl_cnt += count;
2464 if (hdev->acl_cnt > hdev->acl_pkts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 hdev->acl_cnt = hdev->acl_pkts;
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002466 } else if (conn->type == LE_LINK) {
2467 if (hdev->le_pkts) {
2468 hdev->le_cnt += count;
2469 if (hdev->le_cnt > hdev->le_pkts)
2470 hdev->le_cnt = hdev->le_pkts;
2471 } else {
2472 hdev->acl_cnt += count;
2473 if (hdev->acl_cnt > hdev->acl_pkts)
2474 hdev->acl_cnt = hdev->acl_pkts;
2475 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02002476 } else {
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02002477 hdev->sco_cnt += count;
2478 if (hdev->sco_cnt > hdev->sco_pkts)
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02002479 hdev->sco_cnt = hdev->sco_pkts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 }
2481 }
2482 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002483
Marcel Holtmannc78ae282009-11-18 01:02:54 +01002484 tasklet_schedule(&hdev->tx_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
2486 tasklet_enable(&hdev->tx_task);
2487}
2488
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002489static inline void hci_num_comp_blocks_evt(struct hci_dev *hdev,
2490 struct sk_buff *skb)
2491{
2492 struct hci_ev_num_comp_blocks *ev = (void *) skb->data;
2493 __le16 *ptr;
2494 int i;
2495
2496 skb_pull(skb, sizeof(*ev));
2497
2498 BT_DBG("%s total_num_blocks %d num_hndl %d",
2499 hdev->name, ev->total_num_blocks, ev->num_hndl);
2500
2501 if (skb->len < ev->num_hndl * 6) {
2502 BT_DBG("%s bad parameters", hdev->name);
2503 return;
2504 }
2505
2506 tasklet_disable(&hdev->tx_task);
2507
2508 for (i = 0, ptr = (__le16 *) skb->data; i < ev->num_hndl; i++) {
2509 struct hci_conn *conn = NULL;
2510 struct hci_chan *chan;
2511 __u16 handle, block_count;
2512
2513 handle = get_unaligned_le16(ptr++);
2514
2515 /* Skip packet count */
2516 ptr++;
2517 block_count = get_unaligned_le16(ptr++);
2518
2519 BT_DBG("%s handle %d count %d", hdev->name, handle,
2520 block_count);
2521
2522 if (hdev->dev_type == HCI_BREDR)
2523 conn = hci_conn_hash_lookup_handle(hdev, handle);
2524 else {
2525 chan = hci_chan_list_lookup_handle(hdev, handle);
2526 if (chan)
2527 conn = chan->conn;
2528 }
2529 if (conn) {
2530 BT_DBG("%s conn %p sent %d", hdev->name,
2531 conn, conn->sent);
2532
2533 conn->sent -= block_count;
2534
2535 if (conn->type == ACL_LINK) {
2536 hdev->acl_cnt += block_count;
2537 if (hdev->acl_cnt > hdev->acl_pkts)
2538 hdev->acl_cnt = hdev->acl_pkts;
2539 } else {
2540 /* We should not find ourselves here */
2541 BT_DBG("Unexpected event for SCO connection");
2542 }
2543 }
2544 }
2545
2546 tasklet_schedule(&hdev->tx_task);
2547
2548 tasklet_enable(&hdev->tx_task);
2549}
2550
Marcel Holtmann04837f62006-07-03 10:02:33 +02002551static inline void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002553 struct hci_ev_mode_change *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02002554 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
2556 BT_DBG("%s status %d", hdev->name, ev->status);
2557
2558 hci_dev_lock(hdev);
2559
Marcel Holtmann04837f62006-07-03 10:02:33 +02002560 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
2561 if (conn) {
2562 conn->mode = ev->mode;
2563 conn->interval = __le16_to_cpu(ev->interval);
2564
2565 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
2566 if (conn->mode == HCI_CM_ACTIVE)
2567 conn->power_save = 1;
2568 else
2569 conn->power_save = 0;
2570 }
Rahul Kashyapb44f9e22012-05-03 16:45:17 +05302571 if (conn->mode == HCI_CM_SNIFF)
2572 if (wake_lock_active(&conn->idle_lock))
2573 wake_unlock(&conn->idle_lock);
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002574
2575 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->pend))
2576 hci_sco_setup(conn, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02002577 }
2578
2579 hci_dev_unlock(hdev);
2580}
2581
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582static inline void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
2583{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002584 struct hci_ev_pin_code_req *ev = (void *) skb->data;
2585 struct hci_conn *conn;
2586
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002587 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002588
2589 hci_dev_lock(hdev);
2590
2591 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Marcel Holtmann3d7a9d12009-05-09 12:09:21 -07002592 if (conn && conn->state == BT_CONNECTED) {
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002593 hci_conn_hold(conn);
2594 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
2595 hci_conn_put(conn);
Srinivas Krovvidi32ba9352012-01-26 10:48:08 +05302596 hci_conn_enter_active_mode(conn, 0);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002597 }
2598
Johan Hedberg03b555e2011-01-04 15:40:05 +02002599 if (!test_bit(HCI_PAIRABLE, &hdev->flags))
2600 hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
2601 sizeof(ev->bdaddr), &ev->bdaddr);
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02002602
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002603 if (test_bit(HCI_MGMT, &hdev->flags))
2604 mgmt_pin_code_request(hdev->id, &ev->bdaddr);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002605
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002606 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607}
2608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609static inline void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
2610{
Johan Hedberg55ed8ca2011-01-17 14:41:05 +02002611 struct hci_ev_link_key_req *ev = (void *) skb->data;
2612 struct hci_cp_link_key_reply cp;
2613 struct hci_conn *conn;
2614 struct link_key *key;
2615
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002616 BT_DBG("%s", hdev->name);
Johan Hedberg55ed8ca2011-01-17 14:41:05 +02002617
2618 if (!test_bit(HCI_LINK_KEYS, &hdev->flags))
2619 return;
2620
2621 hci_dev_lock(hdev);
2622
2623 key = hci_find_link_key(hdev, &ev->bdaddr);
2624 if (!key) {
2625 BT_DBG("%s link key not found for %s", hdev->name,
2626 batostr(&ev->bdaddr));
2627 goto not_found;
2628 }
2629
Brian Gixcf956772011-10-20 15:18:51 -07002630 BT_DBG("%s found key type %u for %s", hdev->name, key->key_type,
Johan Hedberg55ed8ca2011-01-17 14:41:05 +02002631 batostr(&ev->bdaddr));
2632
Brian Gixcf956772011-10-20 15:18:51 -07002633 if (!test_bit(HCI_DEBUG_KEYS, &hdev->flags) && key->key_type == 0x03) {
Johan Hedberg55ed8ca2011-01-17 14:41:05 +02002634 BT_DBG("%s ignoring debug key", hdev->name);
2635 goto not_found;
2636 }
2637
2638 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
2639
Prabhakaran Mc6001a712011-09-06 11:56:25 +05302640 if (conn) {
2641 BT_DBG("Conn pending sec level is %d, ssp is %d, key len is %d",
2642 conn->pending_sec_level, conn->ssp_mode, key->pin_len);
2643 }
2644 if (conn && (conn->ssp_mode == 0) &&
2645 (conn->pending_sec_level == BT_SECURITY_HIGH) &&
2646 (key->pin_len != 16)) {
2647 BT_DBG("Security is high ignoring this key");
2648 goto not_found;
2649 }
2650
Brian Gixcf956772011-10-20 15:18:51 -07002651 if (key->key_type == 0x04 && conn && conn->auth_type != 0xff &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002652 (conn->auth_type & 0x01)) {
2653 BT_DBG("%s ignoring unauthenticated key", hdev->name);
2654 goto not_found;
Johan Hedberg55ed8ca2011-01-17 14:41:05 +02002655 }
2656
2657 bacpy(&cp.bdaddr, &ev->bdaddr);
2658 memcpy(cp.link_key, key->val, 16);
2659
2660 hci_send_cmd(hdev, HCI_OP_LINK_KEY_REPLY, sizeof(cp), &cp);
2661
2662 hci_dev_unlock(hdev);
2663
2664 return;
2665
2666not_found:
2667 hci_send_cmd(hdev, HCI_OP_LINK_KEY_NEG_REPLY, 6, &ev->bdaddr);
2668 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669}
2670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671static inline void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
2672{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002673 struct hci_ev_link_key_notify *ev = (void *) skb->data;
2674 struct hci_conn *conn;
Johan Hedberg55ed8ca2011-01-17 14:41:05 +02002675 u8 pin_len = 0;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002676
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002677 BT_DBG("%s type %d", hdev->name, ev->key_type);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002678
2679 hci_dev_lock(hdev);
2680
2681 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
2682 if (conn) {
2683 hci_conn_hold(conn);
2684 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002685
2686 memcpy(conn->link_key, ev->link_key, 16);
2687 conn->key_type = ev->key_type;
2688 hci_disconnect_amp(conn, 0x06);
2689
Prabhakaran Mc48584712012-04-20 15:47:58 +05302690 conn->link_mode &= ~HCI_LM_ENCRYPT;
Johan Hedberg980e1a52011-01-22 06:10:07 +02002691 pin_len = conn->pin_length;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002692 hci_conn_put(conn);
Srinivas Krovvidi32ba9352012-01-26 10:48:08 +05302693 hci_conn_enter_active_mode(conn, 0);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002694 }
2695
Johan Hedberg55ed8ca2011-01-17 14:41:05 +02002696 if (test_bit(HCI_LINK_KEYS, &hdev->flags))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002697 hci_add_link_key(hdev, 1, &ev->bdaddr, ev->link_key,
Johan Hedberg55ed8ca2011-01-17 14:41:05 +02002698 ev->key_type, pin_len);
2699
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002700 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701}
2702
Marcel Holtmann04837f62006-07-03 10:02:33 +02002703static inline void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
2704{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002705 struct hci_ev_clock_offset *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02002706 struct hci_conn *conn;
2707
2708 BT_DBG("%s status %d", hdev->name, ev->status);
2709
2710 hci_dev_lock(hdev);
2711
2712 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 if (conn && !ev->status) {
2714 struct inquiry_entry *ie;
2715
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02002716 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
2717 if (ie) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 ie->data.clock_offset = ev->clock_offset;
2719 ie->timestamp = jiffies;
2720 }
2721 }
2722
2723 hci_dev_unlock(hdev);
2724}
2725
Marcel Holtmanna8746412008-07-14 20:13:46 +02002726static inline void hci_pkt_type_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
2727{
2728 struct hci_ev_pkt_type_change *ev = (void *) skb->data;
2729 struct hci_conn *conn;
2730
2731 BT_DBG("%s status %d", hdev->name, ev->status);
2732
2733 hci_dev_lock(hdev);
2734
2735 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
2736 if (conn && !ev->status)
2737 conn->pkt_type = __le16_to_cpu(ev->pkt_type);
2738
2739 hci_dev_unlock(hdev);
2740}
2741
Marcel Holtmann85a1e932005-08-09 20:28:02 -07002742static inline void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
2743{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002744 struct hci_ev_pscan_rep_mode *ev = (void *) skb->data;
Marcel Holtmann85a1e932005-08-09 20:28:02 -07002745 struct inquiry_entry *ie;
2746
2747 BT_DBG("%s", hdev->name);
2748
2749 hci_dev_lock(hdev);
2750
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02002751 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
2752 if (ie) {
Marcel Holtmann85a1e932005-08-09 20:28:02 -07002753 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
2754 ie->timestamp = jiffies;
2755 }
2756
2757 hci_dev_unlock(hdev);
2758}
2759
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002760static inline void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, struct sk_buff *skb)
2761{
2762 struct inquiry_data data;
2763 int num_rsp = *((__u8 *) skb->data);
2764
2765 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
2766
2767 if (!num_rsp)
2768 return;
2769
2770 hci_dev_lock(hdev);
2771
2772 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
Szymon Janc138d22e2011-02-17 16:44:23 +01002773 struct inquiry_info_with_rssi_and_pscan_mode *info;
2774 info = (void *) (skb->data + 1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002775
Johan Hedberge17acd42011-03-30 23:57:16 +03002776 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002777 bacpy(&data.bdaddr, &info->bdaddr);
2778 data.pscan_rep_mode = info->pscan_rep_mode;
2779 data.pscan_period_mode = info->pscan_period_mode;
2780 data.pscan_mode = info->pscan_mode;
2781 memcpy(data.dev_class, info->dev_class, 3);
2782 data.clock_offset = info->clock_offset;
2783 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002784 data.ssp_mode = 0x00;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002785 hci_inquiry_cache_update(hdev, &data);
Brian Gixa68668b2011-08-11 15:49:36 -07002786 mgmt_device_found(hdev->id, &info->bdaddr, 0, 0,
Johan Hedberge17acd42011-03-30 23:57:16 +03002787 info->dev_class, info->rssi,
Brian Gixa68668b2011-08-11 15:49:36 -07002788 0, NULL);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002789 }
2790 } else {
2791 struct inquiry_info_with_rssi *info = (void *) (skb->data + 1);
2792
Johan Hedberge17acd42011-03-30 23:57:16 +03002793 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002794 bacpy(&data.bdaddr, &info->bdaddr);
2795 data.pscan_rep_mode = info->pscan_rep_mode;
2796 data.pscan_period_mode = info->pscan_period_mode;
2797 data.pscan_mode = 0x00;
2798 memcpy(data.dev_class, info->dev_class, 3);
2799 data.clock_offset = info->clock_offset;
2800 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002801 data.ssp_mode = 0x00;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002802 hci_inquiry_cache_update(hdev, &data);
Brian Gixa68668b2011-08-11 15:49:36 -07002803 mgmt_device_found(hdev->id, &info->bdaddr, 0, 0,
Johan Hedberge17acd42011-03-30 23:57:16 +03002804 info->dev_class, info->rssi,
Brian Gixa68668b2011-08-11 15:49:36 -07002805 0, NULL);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002806 }
2807 }
2808
2809 hci_dev_unlock(hdev);
2810}
2811
2812static inline void hci_remote_ext_features_evt(struct hci_dev *hdev, struct sk_buff *skb)
2813{
Marcel Holtmann41a96212008-07-14 20:13:48 +02002814 struct hci_ev_remote_ext_features *ev = (void *) skb->data;
2815 struct hci_conn *conn;
2816
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002817 BT_DBG("%s", hdev->name);
Marcel Holtmann41a96212008-07-14 20:13:48 +02002818
Marcel Holtmann41a96212008-07-14 20:13:48 +02002819 hci_dev_lock(hdev);
2820
2821 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02002822 if (!conn)
2823 goto unlock;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002824
Johan Hedbergccd556f2010-11-10 17:11:51 +02002825 if (!ev->status && ev->page == 0x01) {
2826 struct inquiry_entry *ie;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002827
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02002828 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
2829 if (ie)
Johan Hedbergccd556f2010-11-10 17:11:51 +02002830 ie->data.ssp_mode = (ev->features[0] & 0x01);
Marcel Holtmann769be972008-07-14 20:13:49 +02002831
Johan Hedbergccd556f2010-11-10 17:11:51 +02002832 conn->ssp_mode = (ev->features[0] & 0x01);
Bhakthavatsala Raghavendra6edb4f12011-11-04 16:07:41 +05302833 /*In case if remote device ssp supported/2.0 device
2834 reduce the security level to MEDIUM if it is HIGH*/
Prabhakaran Mc453651c2012-03-02 11:55:59 +05302835 if (!conn->ssp_mode && conn->auth_initiator &&
Bhakthavatsala Raghavendra6edb4f12011-11-04 16:07:41 +05302836 (conn->pending_sec_level == BT_SECURITY_HIGH))
2837 conn->pending_sec_level = BT_SECURITY_MEDIUM;
Prabhakaran Mc76a83552012-04-09 14:43:18 +05302838
2839 if (conn->ssp_mode && conn->auth_initiator &&
2840 conn->io_capability != 0x03) {
2841 conn->pending_sec_level = BT_SECURITY_HIGH;
2842 conn->auth_type = HCI_AT_DEDICATED_BONDING_MITM;
2843 }
Marcel Holtmann41a96212008-07-14 20:13:48 +02002844 }
2845
Johan Hedbergccd556f2010-11-10 17:11:51 +02002846 if (conn->state != BT_CONFIG)
2847 goto unlock;
2848
Johan Hedberg127178d2010-11-18 22:22:29 +02002849 if (!ev->status) {
2850 struct hci_cp_remote_name_req cp;
2851 memset(&cp, 0, sizeof(cp));
2852 bacpy(&cp.bdaddr, &conn->dst);
2853 cp.pscan_rep_mode = 0x02;
2854 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
2855 }
Johan Hedberg392599b2010-11-18 22:22:28 +02002856
Johan Hedberg127178d2010-11-18 22:22:29 +02002857 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02002858 conn->state = BT_CONNECTED;
2859 hci_proto_connect_cfm(conn, ev->status);
2860 hci_conn_put(conn);
2861 }
2862
2863unlock:
Marcel Holtmann41a96212008-07-14 20:13:48 +02002864 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002865}
2866
2867static inline void hci_sync_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
2868{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002869 struct hci_ev_sync_conn_complete *ev = (void *) skb->data;
2870 struct hci_conn *conn;
2871
2872 BT_DBG("%s status %d", hdev->name, ev->status);
2873
2874 hci_dev_lock(hdev);
2875
2876 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02002877 if (!conn) {
2878 if (ev->link_type == ESCO_LINK)
2879 goto unlock;
2880
2881 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
2882 if (!conn)
2883 goto unlock;
2884
2885 conn->type = SCO_LINK;
2886 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002887
Marcel Holtmann732547f2009-04-19 19:14:14 +02002888 switch (ev->status) {
2889 case 0x00:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002890 conn->handle = __le16_to_cpu(ev->handle);
2891 conn->state = BT_CONNECTED;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002892
Marcel Holtmann9eba32b2009-08-22 14:19:26 -07002893 hci_conn_hold_device(conn);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002894 hci_conn_add_sysfs(conn);
Marcel Holtmann732547f2009-04-19 19:14:14 +02002895 break;
2896
Stephen Coe705e5712010-02-16 11:29:44 -05002897 case 0x11: /* Unsupported Feature or Parameter Value */
Marcel Holtmann732547f2009-04-19 19:14:14 +02002898 case 0x1c: /* SCO interval rejected */
Nick Pelly1038a002010-02-03 11:42:26 -08002899 case 0x1a: /* Unsupported Remote Feature */
Marcel Holtmann732547f2009-04-19 19:14:14 +02002900 case 0x1f: /* Unspecified error */
2901 if (conn->out && conn->attempt < 2) {
Kun Han Kim15b911f2011-07-08 09:30:28 -07002902 if (!conn->hdev->is_wbs)
2903 conn->pkt_type =
2904 (hdev->esco_type & SCO_ESCO_MASK) |
Marcel Holtmann732547f2009-04-19 19:14:14 +02002905 (hdev->esco_type & EDR_ESCO_MASK);
2906 hci_setup_sync(conn, conn->link->handle);
2907 goto unlock;
2908 }
2909 /* fall through */
2910
2911 default:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002912 conn->state = BT_CLOSED;
Marcel Holtmann732547f2009-04-19 19:14:14 +02002913 break;
2914 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002915
2916 hci_proto_connect_cfm(conn, ev->status);
2917 if (ev->status)
2918 hci_conn_del(conn);
2919
2920unlock:
2921 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002922}
2923
2924static inline void hci_sync_conn_changed_evt(struct hci_dev *hdev, struct sk_buff *skb)
2925{
2926 BT_DBG("%s", hdev->name);
2927}
2928
Marcel Holtmann04837f62006-07-03 10:02:33 +02002929static inline void hci_sniff_subrate_evt(struct hci_dev *hdev, struct sk_buff *skb)
2930{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002931 struct hci_ev_sniff_subrate *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02002932
2933 BT_DBG("%s status %d", hdev->name, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02002934}
2935
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002936static inline void hci_extended_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
2937{
2938 struct inquiry_data data;
2939 struct extended_inquiry_info *info = (void *) (skb->data + 1);
2940 int num_rsp = *((__u8 *) skb->data);
2941
2942 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
2943
2944 if (!num_rsp)
2945 return;
2946
2947 hci_dev_lock(hdev);
2948
Johan Hedberge17acd42011-03-30 23:57:16 +03002949 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002950 bacpy(&data.bdaddr, &info->bdaddr);
Szymon Janc138d22e2011-02-17 16:44:23 +01002951 data.pscan_rep_mode = info->pscan_rep_mode;
2952 data.pscan_period_mode = info->pscan_period_mode;
2953 data.pscan_mode = 0x00;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002954 memcpy(data.dev_class, info->dev_class, 3);
Szymon Janc138d22e2011-02-17 16:44:23 +01002955 data.clock_offset = info->clock_offset;
2956 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002957 data.ssp_mode = 0x01;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002958 hci_inquiry_cache_update(hdev, &data);
Brian Gixa68668b2011-08-11 15:49:36 -07002959 mgmt_device_found(hdev->id, &info->bdaddr, 0, 0,
2960 info->dev_class, info->rssi,
2961 HCI_MAX_EIR_LENGTH, info->data);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002962 }
2963
2964 hci_dev_unlock(hdev);
2965}
2966
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002967static inline u8 hci_get_auth_req(struct hci_conn *conn)
2968{
Brian Gixa68668b2011-08-11 15:49:36 -07002969 BT_DBG("%p", conn);
2970
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002971 /* If remote requests dedicated bonding follow that lead */
2972 if (conn->remote_auth == 0x02 || conn->remote_auth == 0x03) {
2973 /* If both remote and local IO capabilities allow MITM
2974 * protection then require it, otherwise don't */
Brian Gixa68668b2011-08-11 15:49:36 -07002975 if (conn->remote_cap == 0x03 || conn->io_capability == 0x03) {
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002976 return 0x02;
Brian Gixa68668b2011-08-11 15:49:36 -07002977 } else {
2978 conn->auth_type |= 0x01;
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002979 return 0x03;
Brian Gixa68668b2011-08-11 15:49:36 -07002980 }
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002981 }
2982
2983 /* If remote requests no-bonding follow that lead */
Brian Gixa68668b2011-08-11 15:49:36 -07002984 if (conn->remote_auth <= 0x01)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002985 return 0x00;
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002986
2987 return conn->auth_type;
2988}
2989
Marcel Holtmann04936842008-07-14 20:13:48 +02002990static inline void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
2991{
2992 struct hci_ev_io_capa_request *ev = (void *) skb->data;
2993 struct hci_conn *conn;
2994
2995 BT_DBG("%s", hdev->name);
2996
2997 hci_dev_lock(hdev);
2998
2999 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003000 if (!conn)
3001 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02003002
Johan Hedberg03b555e2011-01-04 15:40:05 +02003003 hci_conn_hold(conn);
3004
3005 if (!test_bit(HCI_MGMT, &hdev->flags))
3006 goto unlock;
3007
3008 if (test_bit(HCI_PAIRABLE, &hdev->flags) ||
3009 (conn->remote_auth & ~0x01) == HCI_AT_NO_BONDING) {
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003010 struct hci_cp_io_capability_reply cp;
Brian Gixa68668b2011-08-11 15:49:36 -07003011 u8 io_cap = conn->io_capability;
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003012
Brian Gixa68668b2011-08-11 15:49:36 -07003013 /* ACL-SSP does not support IO CAP 0x04 */
3014 cp.capability = (io_cap == 0x04) ? 0x01 : io_cap;
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003015 bacpy(&cp.bdaddr, &ev->bdaddr);
Brian Gixa68668b2011-08-11 15:49:36 -07003016 if (conn->auth_initiator)
3017 cp.authentication = conn->auth_type;
3018 else
3019 cp.authentication = hci_get_auth_req(conn);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003020
Szymon Jancce85ee12011-03-22 13:12:23 +01003021 if ((conn->out == 0x01 || conn->remote_oob == 0x01) &&
3022 hci_find_remote_oob_data(hdev, &conn->dst))
3023 cp.oob_data = 0x01;
3024 else
3025 cp.oob_data = 0x00;
3026
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003027 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_REPLY,
3028 sizeof(cp), &cp);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003029 } else {
3030 struct hci_cp_io_capability_neg_reply cp;
3031
3032 bacpy(&cp.bdaddr, &ev->bdaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003033 cp.reason = 0x16; /* Pairing not allowed */
Johan Hedberg03b555e2011-01-04 15:40:05 +02003034
3035 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_NEG_REPLY,
3036 sizeof(cp), &cp);
3037 }
3038
3039unlock:
3040 hci_dev_unlock(hdev);
3041}
3042
3043static inline void hci_io_capa_reply_evt(struct hci_dev *hdev, struct sk_buff *skb)
3044{
3045 struct hci_ev_io_capa_reply *ev = (void *) skb->data;
3046 struct hci_conn *conn;
3047
3048 BT_DBG("%s", hdev->name);
3049
3050 hci_dev_lock(hdev);
3051
3052 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3053 if (!conn)
3054 goto unlock;
3055
Johan Hedberg03b555e2011-01-04 15:40:05 +02003056 conn->remote_cap = ev->capability;
3057 conn->remote_oob = ev->oob_data;
3058 conn->remote_auth = ev->authentication;
3059
3060unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02003061 hci_dev_unlock(hdev);
3062}
3063
Brian Gixa68668b2011-08-11 15:49:36 -07003064static inline void hci_user_ssp_confirmation_evt(struct hci_dev *hdev,
3065 u8 event, struct sk_buff *skb)
Johan Hedberga5c29682011-02-19 12:05:57 -03003066{
3067 struct hci_ev_user_confirm_req *ev = (void *) skb->data;
3068
3069 BT_DBG("%s", hdev->name);
3070
3071 hci_dev_lock(hdev);
3072
Brian Gixa68668b2011-08-11 15:49:36 -07003073 if (test_bit(HCI_MGMT, &hdev->flags)) {
3074 if (event == HCI_EV_USER_PASSKEY_REQUEST)
3075 mgmt_user_confirm_request(hdev->id, event,
3076 &ev->bdaddr, 0);
3077 else
3078 mgmt_user_confirm_request(hdev->id, event,
3079 &ev->bdaddr, ev->passkey);
3080 }
Johan Hedberga5c29682011-02-19 12:05:57 -03003081
3082 hci_dev_unlock(hdev);
3083}
3084
Marcel Holtmann04936842008-07-14 20:13:48 +02003085static inline void hci_simple_pair_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
3086{
3087 struct hci_ev_simple_pair_complete *ev = (void *) skb->data;
3088 struct hci_conn *conn;
3089
3090 BT_DBG("%s", hdev->name);
3091
3092 hci_dev_lock(hdev);
3093
3094 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg2a611692011-02-19 12:06:00 -03003095 if (!conn)
3096 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02003097
Johan Hedberg2a611692011-02-19 12:06:00 -03003098 /* To avoid duplicate auth_failed events to user space we check
3099 * the HCI_CONN_AUTH_PEND flag which will be set if we
3100 * initiated the authentication. A traditional auth_complete
3101 * event gets always produced as initiator and is also mapped to
3102 * the mgmt_auth_failed event */
3103 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->pend) && ev->status != 0)
3104 mgmt_auth_failed(hdev->id, &conn->dst, ev->status);
3105
3106 hci_conn_put(conn);
3107
3108unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02003109 hci_dev_unlock(hdev);
3110}
3111
Marcel Holtmann41a96212008-07-14 20:13:48 +02003112static inline void hci_remote_host_features_evt(struct hci_dev *hdev, struct sk_buff *skb)
3113{
3114 struct hci_ev_remote_host_features *ev = (void *) skb->data;
3115 struct inquiry_entry *ie;
3116
3117 BT_DBG("%s", hdev->name);
3118
3119 hci_dev_lock(hdev);
3120
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02003121 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
3122 if (ie)
Marcel Holtmann41a96212008-07-14 20:13:48 +02003123 ie->data.ssp_mode = (ev->features[0] & 0x01);
3124
3125 hci_dev_unlock(hdev);
3126}
3127
Szymon Janc2763eda2011-03-22 13:12:22 +01003128static inline void hci_remote_oob_data_request_evt(struct hci_dev *hdev,
3129 struct sk_buff *skb)
3130{
3131 struct hci_ev_remote_oob_data_request *ev = (void *) skb->data;
3132 struct oob_data *data;
3133
3134 BT_DBG("%s", hdev->name);
3135
3136 hci_dev_lock(hdev);
3137
Szymon Jance1ba1f12011-04-06 13:01:59 +02003138 if (!test_bit(HCI_MGMT, &hdev->flags))
3139 goto unlock;
3140
Szymon Janc2763eda2011-03-22 13:12:22 +01003141 data = hci_find_remote_oob_data(hdev, &ev->bdaddr);
3142 if (data) {
3143 struct hci_cp_remote_oob_data_reply cp;
3144
3145 bacpy(&cp.bdaddr, &ev->bdaddr);
3146 memcpy(cp.hash, data->hash, sizeof(cp.hash));
3147 memcpy(cp.randomizer, data->randomizer, sizeof(cp.randomizer));
3148
3149 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_REPLY, sizeof(cp),
3150 &cp);
3151 } else {
3152 struct hci_cp_remote_oob_data_neg_reply cp;
3153
3154 bacpy(&cp.bdaddr, &ev->bdaddr);
3155 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_NEG_REPLY, sizeof(cp),
3156 &cp);
3157 }
3158
Szymon Jance1ba1f12011-04-06 13:01:59 +02003159unlock:
Szymon Janc2763eda2011-03-22 13:12:22 +01003160 hci_dev_unlock(hdev);
3161}
3162
Ville Tervofcd89c02011-02-10 22:38:47 -03003163static inline void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
3164{
3165 struct hci_ev_le_conn_complete *ev = (void *) skb->data;
3166 struct hci_conn *conn;
3167
3168 BT_DBG("%s status %d", hdev->name, ev->status);
3169
3170 hci_dev_lock(hdev);
3171
3172 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &ev->bdaddr);
Ville Tervob62f3282011-02-10 22:38:50 -03003173 if (!conn) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003174 conn = hci_le_conn_add(hdev, &ev->bdaddr, ev->bdaddr_type);
Ville Tervob62f3282011-02-10 22:38:50 -03003175 if (!conn) {
3176 BT_ERR("No memory for new connection");
3177 hci_dev_unlock(hdev);
3178 return;
3179 }
3180 }
Ville Tervofcd89c02011-02-10 22:38:47 -03003181
3182 if (ev->status) {
3183 hci_proto_connect_cfm(conn, ev->status);
3184 conn->state = BT_CLOSED;
3185 hci_conn_del(conn);
3186 goto unlock;
3187 }
3188
Vinicius Costa Gomes403d2c82011-06-09 18:50:50 -03003189 conn->sec_level = BT_SECURITY_LOW;
Ville Tervofcd89c02011-02-10 22:38:47 -03003190 conn->handle = __le16_to_cpu(ev->handle);
3191 conn->state = BT_CONNECTED;
Brian Gixa68668b2011-08-11 15:49:36 -07003192 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Brian Gix2e2f50d2011-09-13 12:36:04 -07003193 mgmt_connected(hdev->id, &ev->bdaddr, 1);
Sunny Kapdia42b5022012-07-05 22:48:31 -07003194 mgmt_le_conn_params(hdev->id, &ev->bdaddr,
3195 __le16_to_cpu(ev->interval),
3196 __le16_to_cpu(ev->latency),
3197 __le16_to_cpu(ev->supervision_timeout));
Ville Tervofcd89c02011-02-10 22:38:47 -03003198
Brian Gix6d5fb8a2011-09-09 14:53:04 -07003199 hci_conn_hold(conn);
Ville Tervofcd89c02011-02-10 22:38:47 -03003200 hci_conn_hold_device(conn);
3201 hci_conn_add_sysfs(conn);
3202
3203 hci_proto_connect_cfm(conn, ev->status);
3204
3205unlock:
3206 hci_dev_unlock(hdev);
3207}
3208
Sunny Kapdia42b5022012-07-05 22:48:31 -07003209static inline void hci_le_conn_update_complete_evt(struct hci_dev *hdev,
3210 struct sk_buff *skb)
3211{
3212 struct hci_ev_le_conn_update_complete *ev = (void *) skb->data;
3213 struct hci_conn *conn;
3214
3215 BT_DBG("%s status %d", hdev->name, ev->status);
3216
3217 hci_dev_lock(hdev);
3218
3219 conn = hci_conn_hash_lookup_handle(hdev,
3220 __le16_to_cpu(ev->handle));
3221 if (conn == NULL) {
3222 BT_ERR("Unknown connection update");
3223 goto unlock;
3224 }
3225
3226 if (ev->status) {
3227 BT_ERR("Connection update unsuccessful");
3228 goto unlock;
3229 }
3230
3231 mgmt_le_conn_params(hdev->id, &conn->dst,
3232 __le16_to_cpu(ev->interval),
3233 __le16_to_cpu(ev->latency),
3234 __le16_to_cpu(ev->supervision_timeout));
3235
3236unlock:
3237 hci_dev_unlock(hdev);
3238}
3239
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03003240static inline void hci_le_ltk_request_evt(struct hci_dev *hdev,
3241 struct sk_buff *skb)
3242{
3243 struct hci_ev_le_ltk_req *ev = (void *) skb->data;
3244 struct hci_cp_le_ltk_reply cp;
Vinicius Costa Gomes60e56472011-07-07 18:59:37 -03003245 struct hci_cp_le_ltk_neg_reply neg;
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03003246 struct hci_conn *conn;
Vinicius Costa Gomes60e56472011-07-07 18:59:37 -03003247 struct link_key *ltk;
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03003248
3249 BT_DBG("%s handle %d", hdev->name, cpu_to_le16(ev->handle));
3250
3251 hci_dev_lock(hdev);
3252
3253 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Vinicius Costa Gomes60e56472011-07-07 18:59:37 -03003254 if (conn == NULL)
3255 goto not_found;
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03003256
Vinicius Costa Gomes60e56472011-07-07 18:59:37 -03003257 ltk = hci_find_ltk(hdev, ev->ediv, ev->random);
3258 if (ltk == NULL)
3259 goto not_found;
3260
3261 memcpy(cp.ltk, ltk->val, sizeof(ltk->val));
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03003262 cp.handle = cpu_to_le16(conn->handle);
Vinicius Costa Gomes1fa2de32011-07-08 18:31:45 -03003263 conn->pin_length = ltk->pin_len;
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03003264
3265 hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
3266
3267 hci_dev_unlock(hdev);
Vinicius Costa Gomes60e56472011-07-07 18:59:37 -03003268
3269 return;
3270
3271not_found:
3272 neg.handle = ev->handle;
3273 hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(neg), &neg);
3274 hci_dev_unlock(hdev);
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03003275}
3276
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003277static inline void hci_le_adv_report_evt(struct hci_dev *hdev,
3278 struct sk_buff *skb)
3279{
3280 struct hci_ev_le_advertising_info *ev;
3281 u8 num_reports;
3282
3283 num_reports = skb->data[0];
3284 ev = (void *) &skb->data[1];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003285
Brian Gix3dd70172011-09-16 21:38:54 -07003286 hci_dev_lock(hdev);
3287
Brian Gixa68668b2011-08-11 15:49:36 -07003288 while (num_reports--) {
3289 mgmt_device_found(hdev->id, &ev->bdaddr, ev->bdaddr_type,
3290 1, NULL, 0, ev->length, ev->data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003291 hci_add_adv_entry(hdev, ev);
Brian Gixa68668b2011-08-11 15:49:36 -07003292 ev = (void *) (ev->data + ev->length + 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003293 }
Brian Gix3dd70172011-09-16 21:38:54 -07003294
3295 hci_dev_unlock(hdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003296}
3297
Ville Tervofcd89c02011-02-10 22:38:47 -03003298static inline void hci_le_meta_evt(struct hci_dev *hdev, struct sk_buff *skb)
3299{
3300 struct hci_ev_le_meta *le_ev = (void *) skb->data;
3301
3302 skb_pull(skb, sizeof(*le_ev));
3303
3304 switch (le_ev->subevent) {
3305 case HCI_EV_LE_CONN_COMPLETE:
3306 hci_le_conn_complete_evt(hdev, skb);
3307 break;
3308
Sunny Kapdia42b5022012-07-05 22:48:31 -07003309 case HCI_EV_LE_CONN_UPDATE_COMPLETE:
3310 hci_le_conn_update_complete_evt(hdev, skb);
3311 break;
3312
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03003313 case HCI_EV_LE_LTK_REQ:
3314 hci_le_ltk_request_evt(hdev, skb);
3315 break;
3316
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003317 case HCI_EV_LE_ADVERTISING_REPORT:
3318 hci_le_adv_report_evt(hdev, skb);
3319 break;
3320
Ville Tervofcd89c02011-02-10 22:38:47 -03003321 default:
3322 break;
3323 }
3324}
3325
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003326static inline void hci_phy_link_complete(struct hci_dev *hdev,
3327 struct sk_buff *skb)
3328{
3329 struct hci_ev_phys_link_complete *ev = (void *) skb->data;
3330 struct hci_conn *conn;
3331
3332 BT_DBG("%s handle %d status %d", hdev->name, ev->phy_handle,
3333 ev->status);
3334
3335 hci_dev_lock(hdev);
3336
3337 if (ev->status == 0) {
3338 conn = hci_conn_add(hdev, ACL_LINK, 0, BDADDR_ANY);
3339 if (conn) {
3340 conn->handle = ev->phy_handle;
3341 conn->state = BT_CONNECTED;
3342
3343 hci_conn_hold(conn);
3344 conn->disc_timeout = HCI_DISCONN_TIMEOUT/2;
3345 hci_conn_put(conn);
3346
3347 hci_conn_hold_device(conn);
3348 hci_conn_add_sysfs(conn);
3349 } else
3350 BT_ERR("No memory for new connection");
3351 }
3352
3353 hci_dev_unlock(hdev);
3354}
3355
3356static inline void hci_log_link_complete(struct hci_dev *hdev,
3357 struct sk_buff *skb)
3358{
3359 struct hci_ev_log_link_complete *ev = (void *) skb->data;
3360 struct hci_chan *chan;
3361
3362 BT_DBG("%s handle %d status %d", hdev->name,
3363 __le16_to_cpu(ev->log_handle), ev->status);
3364
3365 hci_dev_lock(hdev);
3366
3367 chan = hci_chan_list_lookup_id(hdev, ev->phy_handle);
3368
Mat Martineau9f8d4672011-12-14 12:10:46 -08003369 if (chan) {
3370 if (ev->status == 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003371 chan->ll_handle = __le16_to_cpu(ev->log_handle);
3372 chan->state = BT_CONNECTED;
Mat Martineau9f8d4672011-12-14 12:10:46 -08003373 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003374 chan->state = BT_CLOSED;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003375 }
Mat Martineau9f8d4672011-12-14 12:10:46 -08003376
3377 hci_proto_create_cfm(chan, ev->status);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003378 }
3379
3380 hci_dev_unlock(hdev);
3381}
3382
3383static inline void hci_flow_spec_modify_complete(struct hci_dev *hdev,
3384 struct sk_buff *skb)
3385{
3386 struct hci_ev_flow_spec_modify_complete *ev = (void *) skb->data;
3387 struct hci_chan *chan;
3388
3389 BT_DBG("%s handle %d status %d", hdev->name,
3390 __le16_to_cpu(ev->log_handle), ev->status);
3391
3392 hci_dev_lock(hdev);
3393
3394 chan = hci_chan_list_lookup_handle(hdev, ev->log_handle);
3395 if (chan)
3396 hci_proto_modify_cfm(chan, ev->status);
3397
3398 hci_dev_unlock(hdev);
3399}
3400
3401static inline void hci_disconn_log_link_complete_evt(struct hci_dev *hdev,
3402 struct sk_buff *skb)
3403{
3404 struct hci_ev_disconn_log_link_complete *ev = (void *) skb->data;
3405 struct hci_chan *chan;
3406
3407 BT_DBG("%s handle %d status %d", hdev->name,
3408 __le16_to_cpu(ev->log_handle), ev->status);
3409
3410 if (ev->status)
3411 return;
3412
3413 hci_dev_lock(hdev);
3414
3415 chan = hci_chan_list_lookup_handle(hdev, __le16_to_cpu(ev->log_handle));
Mat Martineau9f8d4672011-12-14 12:10:46 -08003416 if (chan)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003417 hci_proto_destroy_cfm(chan, ev->reason);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003418
3419 hci_dev_unlock(hdev);
3420}
3421
3422static inline void hci_disconn_phy_link_complete_evt(struct hci_dev *hdev,
3423 struct sk_buff *skb)
3424{
3425 struct hci_ev_disconn_phys_link_complete *ev = (void *) skb->data;
3426 struct hci_conn *conn;
3427
3428 BT_DBG("%s status %d", hdev->name, ev->status);
3429
3430 if (ev->status)
3431 return;
3432
3433 hci_dev_lock(hdev);
3434
3435 conn = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
3436 if (conn) {
3437 conn->state = BT_CLOSED;
3438
Mat Martineau3b9239a2012-02-16 11:54:30 -08003439 hci_proto_disconn_cfm(conn, ev->reason, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003440 hci_conn_del(conn);
3441 }
3442
3443 hci_dev_unlock(hdev);
3444}
3445
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
3447{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003448 struct hci_event_hdr *hdr = (void *) skb->data;
3449 __u8 event = hdr->evt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
Brian Gixdfdd9362011-08-18 09:58:02 -07003451 BT_DBG("");
3452
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 skb_pull(skb, HCI_EVENT_HDR_SIZE);
3454
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003455 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 case HCI_EV_INQUIRY_COMPLETE:
3457 hci_inquiry_complete_evt(hdev, skb);
3458 break;
3459
3460 case HCI_EV_INQUIRY_RESULT:
3461 hci_inquiry_result_evt(hdev, skb);
3462 break;
3463
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003464 case HCI_EV_CONN_COMPLETE:
3465 hci_conn_complete_evt(hdev, skb);
Marcel Holtmann21d9e302005-09-13 01:32:25 +02003466 break;
3467
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 case HCI_EV_CONN_REQUEST:
3469 hci_conn_request_evt(hdev, skb);
3470 break;
3471
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 case HCI_EV_DISCONN_COMPLETE:
3473 hci_disconn_complete_evt(hdev, skb);
3474 break;
3475
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 case HCI_EV_AUTH_COMPLETE:
3477 hci_auth_complete_evt(hdev, skb);
3478 break;
3479
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003480 case HCI_EV_REMOTE_NAME:
3481 hci_remote_name_evt(hdev, skb);
3482 break;
3483
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 case HCI_EV_ENCRYPT_CHANGE:
3485 hci_encrypt_change_evt(hdev, skb);
3486 break;
3487
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003488 case HCI_EV_CHANGE_LINK_KEY_COMPLETE:
3489 hci_change_link_key_complete_evt(hdev, skb);
3490 break;
3491
3492 case HCI_EV_REMOTE_FEATURES:
3493 hci_remote_features_evt(hdev, skb);
3494 break;
3495
3496 case HCI_EV_REMOTE_VERSION:
3497 hci_remote_version_evt(hdev, skb);
3498 break;
3499
3500 case HCI_EV_QOS_SETUP_COMPLETE:
3501 hci_qos_setup_complete_evt(hdev, skb);
3502 break;
3503
3504 case HCI_EV_CMD_COMPLETE:
3505 hci_cmd_complete_evt(hdev, skb);
3506 break;
3507
3508 case HCI_EV_CMD_STATUS:
3509 hci_cmd_status_evt(hdev, skb);
3510 break;
3511
3512 case HCI_EV_ROLE_CHANGE:
3513 hci_role_change_evt(hdev, skb);
3514 break;
3515
3516 case HCI_EV_NUM_COMP_PKTS:
3517 hci_num_comp_pkts_evt(hdev, skb);
3518 break;
3519
3520 case HCI_EV_MODE_CHANGE:
3521 hci_mode_change_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 break;
3523
3524 case HCI_EV_PIN_CODE_REQ:
3525 hci_pin_code_request_evt(hdev, skb);
3526 break;
3527
3528 case HCI_EV_LINK_KEY_REQ:
3529 hci_link_key_request_evt(hdev, skb);
3530 break;
3531
3532 case HCI_EV_LINK_KEY_NOTIFY:
3533 hci_link_key_notify_evt(hdev, skb);
3534 break;
3535
3536 case HCI_EV_CLOCK_OFFSET:
3537 hci_clock_offset_evt(hdev, skb);
3538 break;
3539
Marcel Holtmanna8746412008-07-14 20:13:46 +02003540 case HCI_EV_PKT_TYPE_CHANGE:
3541 hci_pkt_type_change_evt(hdev, skb);
3542 break;
3543
Marcel Holtmann85a1e932005-08-09 20:28:02 -07003544 case HCI_EV_PSCAN_REP_MODE:
3545 hci_pscan_rep_mode_evt(hdev, skb);
3546 break;
3547
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003548 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
3549 hci_inquiry_result_with_rssi_evt(hdev, skb);
3550 break;
3551
3552 case HCI_EV_REMOTE_EXT_FEATURES:
3553 hci_remote_ext_features_evt(hdev, skb);
3554 break;
3555
3556 case HCI_EV_SYNC_CONN_COMPLETE:
3557 hci_sync_conn_complete_evt(hdev, skb);
3558 break;
3559
3560 case HCI_EV_SYNC_CONN_CHANGED:
3561 hci_sync_conn_changed_evt(hdev, skb);
3562 break;
3563
Marcel Holtmann04837f62006-07-03 10:02:33 +02003564 case HCI_EV_SNIFF_SUBRATE:
3565 hci_sniff_subrate_evt(hdev, skb);
3566 break;
3567
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003568 case HCI_EV_EXTENDED_INQUIRY_RESULT:
3569 hci_extended_inquiry_result_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 break;
3571
Marcel Holtmann04936842008-07-14 20:13:48 +02003572 case HCI_EV_IO_CAPA_REQUEST:
3573 hci_io_capa_request_evt(hdev, skb);
3574 break;
3575
Johan Hedberg03b555e2011-01-04 15:40:05 +02003576 case HCI_EV_IO_CAPA_REPLY:
3577 hci_io_capa_reply_evt(hdev, skb);
3578 break;
3579
Brian Gixa68668b2011-08-11 15:49:36 -07003580 case HCI_EV_USER_PASSKEY_REQUEST:
3581 case HCI_EV_USER_PASSKEY_NOTIFICATION:
Johan Hedberga5c29682011-02-19 12:05:57 -03003582 case HCI_EV_USER_CONFIRM_REQUEST:
Brian Gixa68668b2011-08-11 15:49:36 -07003583 hci_user_ssp_confirmation_evt(hdev, event, skb);
Johan Hedberga5c29682011-02-19 12:05:57 -03003584 break;
3585
Marcel Holtmann04936842008-07-14 20:13:48 +02003586 case HCI_EV_SIMPLE_PAIR_COMPLETE:
3587 hci_simple_pair_complete_evt(hdev, skb);
3588 break;
3589
Marcel Holtmann41a96212008-07-14 20:13:48 +02003590 case HCI_EV_REMOTE_HOST_FEATURES:
3591 hci_remote_host_features_evt(hdev, skb);
3592 break;
3593
Ville Tervofcd89c02011-02-10 22:38:47 -03003594 case HCI_EV_LE_META:
3595 hci_le_meta_evt(hdev, skb);
3596 break;
3597
Szymon Janc2763eda2011-03-22 13:12:22 +01003598 case HCI_EV_REMOTE_OOB_DATA_REQUEST:
3599 hci_remote_oob_data_request_evt(hdev, skb);
3600 break;
3601
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003602 case HCI_EV_PHYS_LINK_COMPLETE:
3603 hci_phy_link_complete(hdev, skb);
3604 hci_amp_event_packet(hdev, event, skb);
3605 break;
3606
3607 case HCI_EV_LOG_LINK_COMPLETE:
3608 hci_log_link_complete(hdev, skb);
3609 break;
3610
3611 case HCI_EV_FLOW_SPEC_MODIFY_COMPLETE:
3612 hci_flow_spec_modify_complete(hdev, skb);
3613 break;
3614
3615 case HCI_EV_DISCONN_LOG_LINK_COMPLETE:
3616 hci_disconn_log_link_complete_evt(hdev, skb);
3617 break;
3618
3619 case HCI_EV_DISCONN_PHYS_LINK_COMPLETE:
3620 hci_disconn_phy_link_complete_evt(hdev, skb);
3621 hci_amp_event_packet(hdev, event, skb);
3622 break;
3623
3624 case HCI_EV_NUM_COMP_BLOCKS:
3625 hci_num_comp_blocks_evt(hdev, skb);
3626 break;
3627
3628 case HCI_EV_CHANNEL_SELECTED:
3629 hci_amp_event_packet(hdev, event, skb);
3630 break;
3631
3632 case HCI_EV_AMP_STATUS_CHANGE:
3633 hci_amp_event_packet(hdev, event, skb);
3634 break;
3635
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003636 default:
3637 BT_DBG("%s event 0x%x", hdev->name, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 break;
3639 }
3640
3641 kfree_skb(skb);
3642 hdev->stat.evt_rx++;
3643}
3644
3645/* Generate internal stack event */
3646void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
3647{
3648 struct hci_event_hdr *hdr;
3649 struct hci_ev_stack_internal *ev;
3650 struct sk_buff *skb;
3651
3652 skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC);
3653 if (!skb)
3654 return;
3655
3656 hdr = (void *) skb_put(skb, HCI_EVENT_HDR_SIZE);
3657 hdr->evt = HCI_EV_STACK_INTERNAL;
3658 hdr->plen = sizeof(*ev) + dlen;
3659
3660 ev = (void *) skb_put(skb, sizeof(*ev) + dlen);
3661 ev->type = type;
3662 memcpy(ev->data, data, dlen);
3663
Marcel Holtmann576c7d82005-08-06 12:36:54 +02003664 bt_cb(skb)->incoming = 1;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07003665 __net_timestamp(skb);
Marcel Holtmann576c7d82005-08-06 12:36:54 +02003666
Marcel Holtmann0d48d932005-08-09 20:30:28 -07003667 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 skb->dev = (void *) hdev;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02003669 hci_send_to_sock(hdev, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 kfree_skb(skb);
3671}