blob: 640c54ec1bd29038101a06e6c1e3098e02336a8d [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
Ron Shaffer2d0a0342010-05-28 11:53:46 -04003 Copyright (c) 2000-2001, 2010, 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 <asm/unaligned.h>
28
29#include <net/bluetooth/bluetooth.h>
30#include <net/bluetooth/hci_core.h>
Mikel Astizf0d6a0e2012-08-09 09:52:30 +020031#include <net/bluetooth/mgmt.h>
Marcel Holtmann7ef9fbf2013-10-10 14:54:14 -070032
Marcel Holtmann70247282013-10-10 14:54:15 -070033#include "a2mp.h"
Marcel Holtmann7ef9fbf2013-10-10 14:54:14 -070034#include "amp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/* Handle HCI Event packets */
37
Marcel Holtmanna9de9242007-10-20 13:33:56 +020038static void hci_cc_inquiry_cancel(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Marcel Holtmanna9de9242007-10-20 13:33:56 +020040 __u8 status = *((__u8 *) skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030042 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Andre Guedes82f47852013-04-30 15:29:34 -030044 if (status)
Marcel Holtmanna9de9242007-10-20 13:33:56 +020045 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Andre Guedes89352e72011-11-04 14:16:53 -030047 clear_bit(HCI_INQUIRY, &hdev->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +010048 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
Andre Guedes3e13fa12013-03-27 20:04:56 -030049 wake_up_bit(&hdev->flags, HCI_INQUIRY);
Andre Guedes89352e72011-11-04 14:16:53 -030050
Johan Hedberg50143a42014-06-10 14:05:57 +030051 hci_dev_lock(hdev);
52 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
53 hci_dev_unlock(hdev);
54
Marcel Holtmanna9de9242007-10-20 13:33:56 +020055 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
Andre Guedes4d934832012-03-21 00:03:35 -030058static void hci_cc_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
59{
60 __u8 status = *((__u8 *) skb->data);
61
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030062 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andre Guedesae854a72012-03-21 00:03:36 -030063
64 if (status)
65 return;
66
67 set_bit(HCI_PERIODIC_INQ, &hdev->dev_flags);
Andre Guedes4d934832012-03-21 00:03:35 -030068}
69
Marcel Holtmanna9de9242007-10-20 13:33:56 +020070static void hci_cc_exit_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Marcel Holtmanna9de9242007-10-20 13:33:56 +020072 __u8 status = *((__u8 *) skb->data);
73
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030074 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +020075
76 if (status)
77 return;
78
Andre Guedesae854a72012-03-21 00:03:36 -030079 clear_bit(HCI_PERIODIC_INQ, &hdev->dev_flags);
80
Marcel Holtmanna9de9242007-10-20 13:33:56 +020081 hci_conn_check_pending(hdev);
82}
83
Gustavo Padovan807deac2012-05-17 00:36:24 -030084static void hci_cc_remote_name_req_cancel(struct hci_dev *hdev,
85 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +020086{
87 BT_DBG("%s", hdev->name);
88}
89
90static void hci_cc_role_discovery(struct hci_dev *hdev, struct sk_buff *skb)
91{
92 struct hci_rp_role_discovery *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030095 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Marcel Holtmanna9de9242007-10-20 13:33:56 +020097 if (rp->status)
98 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200100 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200102 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
103 if (conn) {
104 if (rp->role)
105 conn->link_mode &= ~HCI_LM_MASTER;
106 else
107 conn->link_mode |= HCI_LM_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200109
110 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200113static void hci_cc_read_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
114{
115 struct hci_rp_read_link_policy *rp = (void *) skb->data;
116 struct hci_conn *conn;
117
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300118 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200119
120 if (rp->status)
121 return;
122
123 hci_dev_lock(hdev);
124
125 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
126 if (conn)
127 conn->link_policy = __le16_to_cpu(rp->policy);
128
129 hci_dev_unlock(hdev);
130}
131
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200132static void hci_cc_write_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200134 struct hci_rp_write_link_policy *rp = (void *) skb->data;
135 struct hci_conn *conn;
136 void *sent;
137
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300138 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200139
140 if (rp->status)
141 return;
142
143 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LINK_POLICY);
144 if (!sent)
145 return;
146
147 hci_dev_lock(hdev);
148
149 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200150 if (conn)
Harvey Harrison83985312008-05-02 16:25:46 -0700151 conn->link_policy = get_unaligned_le16(sent + 2);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200152
153 hci_dev_unlock(hdev);
154}
155
Gustavo Padovan807deac2012-05-17 00:36:24 -0300156static void hci_cc_read_def_link_policy(struct hci_dev *hdev,
157 struct sk_buff *skb)
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200158{
159 struct hci_rp_read_def_link_policy *rp = (void *) skb->data;
160
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300161 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200162
163 if (rp->status)
164 return;
165
166 hdev->link_policy = __le16_to_cpu(rp->policy);
167}
168
Gustavo Padovan807deac2012-05-17 00:36:24 -0300169static void hci_cc_write_def_link_policy(struct hci_dev *hdev,
170 struct sk_buff *skb)
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200171{
172 __u8 status = *((__u8 *) skb->data);
173 void *sent;
174
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300175 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200176
177 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_LINK_POLICY);
178 if (!sent)
179 return;
180
181 if (!status)
182 hdev->link_policy = get_unaligned_le16(sent);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200183}
184
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200185static void hci_cc_reset(struct hci_dev *hdev, struct sk_buff *skb)
186{
187 __u8 status = *((__u8 *) skb->data);
188
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300189 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200190
Gustavo F. Padovan10572132011-03-16 15:36:29 -0300191 clear_bit(HCI_RESET, &hdev->flags);
192
Johan Hedberga297e972012-02-21 17:55:47 +0200193 /* Reset all non-persistent flags */
Johan Hedberg2cc6fb02013-03-15 17:06:57 -0500194 hdev->dev_flags &= ~HCI_PERSISTENT_MASK;
Andre Guedes69775ff2012-02-23 16:50:05 +0200195
196 hdev->discovery.state = DISCOVERY_STOPPED;
Johan Hedbergbbaf4442012-11-08 01:22:59 +0100197 hdev->inq_tx_power = HCI_TX_POWER_INVALID;
198 hdev->adv_tx_power = HCI_TX_POWER_INVALID;
Johan Hedberg3f0f5242012-11-08 01:23:00 +0100199
200 memset(hdev->adv_data, 0, sizeof(hdev->adv_data));
201 hdev->adv_data_len = 0;
Marcel Holtmannf8e808b2013-10-16 00:16:47 -0700202
203 memset(hdev->scan_rsp_data, 0, sizeof(hdev->scan_rsp_data));
204 hdev->scan_rsp_data_len = 0;
Marcel Holtmann06f5b772013-10-19 07:09:11 -0700205
Marcel Holtmann533553f2014-03-21 12:18:10 -0700206 hdev->le_scan_type = LE_SCAN_PASSIVE;
207
Marcel Holtmann06f5b772013-10-19 07:09:11 -0700208 hdev->ssp_debug_mode = 0;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200209}
210
211static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb)
212{
213 __u8 status = *((__u8 *) skb->data);
214 void *sent;
215
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300216 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200217
218 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LOCAL_NAME);
219 if (!sent)
220 return;
221
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200222 hci_dev_lock(hdev);
223
Johan Hedbergf51d5b22012-02-22 18:17:32 +0200224 if (test_bit(HCI_MGMT, &hdev->dev_flags))
225 mgmt_set_local_name_complete(hdev, sent, status);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +0200226 else if (!status)
227 memcpy(hdev->dev_name, sent, HCI_MAX_NAME_LENGTH);
Johan Hedbergf51d5b22012-02-22 18:17:32 +0200228
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200229 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200230}
231
232static void hci_cc_read_local_name(struct hci_dev *hdev, struct sk_buff *skb)
233{
234 struct hci_rp_read_local_name *rp = (void *) skb->data;
235
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300236 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200237
238 if (rp->status)
239 return;
240
Johan Hedbergdb99b5f2012-02-22 20:14:22 +0200241 if (test_bit(HCI_SETUP, &hdev->dev_flags))
242 memcpy(hdev->dev_name, rp->name, HCI_MAX_NAME_LENGTH);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200243}
244
245static void hci_cc_write_auth_enable(struct hci_dev *hdev, struct sk_buff *skb)
246{
247 __u8 status = *((__u8 *) skb->data);
248 void *sent;
249
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300250 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200251
252 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_ENABLE);
253 if (!sent)
254 return;
255
256 if (!status) {
257 __u8 param = *((__u8 *) sent);
258
259 if (param == AUTH_ENABLED)
260 set_bit(HCI_AUTH, &hdev->flags);
261 else
262 clear_bit(HCI_AUTH, &hdev->flags);
263 }
264
Johan Hedberg33ef95e2012-02-16 23:56:27 +0200265 if (test_bit(HCI_MGMT, &hdev->dev_flags))
266 mgmt_auth_enable_complete(hdev, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200267}
268
269static void hci_cc_write_encrypt_mode(struct hci_dev *hdev, struct sk_buff *skb)
270{
271 __u8 status = *((__u8 *) skb->data);
272 void *sent;
273
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300274 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200275
276 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_ENCRYPT_MODE);
277 if (!sent)
278 return;
279
280 if (!status) {
281 __u8 param = *((__u8 *) sent);
282
283 if (param)
284 set_bit(HCI_ENCRYPT, &hdev->flags);
285 else
286 clear_bit(HCI_ENCRYPT, &hdev->flags);
287 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200288}
289
290static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
291{
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200292 __u8 param, status = *((__u8 *) skb->data);
293 int old_pscan, old_iscan;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200294 void *sent;
295
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300296 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200297
298 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SCAN_ENABLE);
299 if (!sent)
300 return;
301
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200302 param = *((__u8 *) sent);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200303
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200304 hci_dev_lock(hdev);
305
Mikel Astizfa1bd912012-08-09 09:52:29 +0200306 if (status) {
Johan Hedberg744cf192011-11-08 20:40:14 +0200307 mgmt_write_scan_failed(hdev, param, status);
Johan Hedberg2d7cee52011-11-07 22:16:03 +0200308 hdev->discov_timeout = 0;
309 goto done;
310 }
311
Johan Hedberg0663ca22013-10-02 13:43:14 +0300312 /* We need to ensure that we set this back on if someone changed
313 * the scan mode through a raw HCI socket.
314 */
315 set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags);
316
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200317 old_pscan = test_and_clear_bit(HCI_PSCAN, &hdev->flags);
318 old_iscan = test_and_clear_bit(HCI_ISCAN, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200319
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200320 if (param & SCAN_INQUIRY) {
321 set_bit(HCI_ISCAN, &hdev->flags);
322 if (!old_iscan)
Johan Hedberg744cf192011-11-08 20:40:14 +0200323 mgmt_discoverable(hdev, 1);
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200324 } else if (old_iscan)
Johan Hedberg744cf192011-11-08 20:40:14 +0200325 mgmt_discoverable(hdev, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200326
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200327 if (param & SCAN_PAGE) {
328 set_bit(HCI_PSCAN, &hdev->flags);
329 if (!old_pscan)
Johan Hedberg744cf192011-11-08 20:40:14 +0200330 mgmt_connectable(hdev, 1);
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200331 } else if (old_pscan)
Johan Hedberg744cf192011-11-08 20:40:14 +0200332 mgmt_connectable(hdev, 0);
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200333
334done:
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200335 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200336}
337
338static void hci_cc_read_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
339{
340 struct hci_rp_read_class_of_dev *rp = (void *) skb->data;
341
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300342 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200343
344 if (rp->status)
345 return;
346
347 memcpy(hdev->dev_class, rp->dev_class, 3);
348
349 BT_DBG("%s class 0x%.2x%.2x%.2x", hdev->name,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300350 hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200351}
352
353static void hci_cc_write_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
354{
355 __u8 status = *((__u8 *) skb->data);
356 void *sent;
357
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300358 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200359
360 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_CLASS_OF_DEV);
361 if (!sent)
362 return;
363
Marcel Holtmann7f9a9032012-02-22 18:38:01 +0100364 hci_dev_lock(hdev);
365
366 if (status == 0)
367 memcpy(hdev->dev_class, sent, 3);
368
369 if (test_bit(HCI_MGMT, &hdev->dev_flags))
370 mgmt_set_class_of_dev_complete(hdev, sent, status);
371
372 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200373}
374
375static void hci_cc_read_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
376{
377 struct hci_rp_read_voice_setting *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 __u16 setting;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200379
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300380 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200381
382 if (rp->status)
383 return;
384
385 setting = __le16_to_cpu(rp->voice_setting);
386
Marcel Holtmannf383f272008-07-14 20:13:47 +0200387 if (hdev->voice_setting == setting)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200388 return;
389
390 hdev->voice_setting = setting;
391
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300392 BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200393
Gustavo F. Padovan3c547112011-12-14 22:58:44 -0200394 if (hdev->notify)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200395 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200396}
397
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300398static void hci_cc_write_voice_setting(struct hci_dev *hdev,
399 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200400{
401 __u8 status = *((__u8 *) skb->data);
Marcel Holtmannf383f272008-07-14 20:13:47 +0200402 __u16 setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 void *sent;
404
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300405 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Marcel Holtmannf383f272008-07-14 20:13:47 +0200407 if (status)
408 return;
409
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200410 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_VOICE_SETTING);
411 if (!sent)
412 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Marcel Holtmannf383f272008-07-14 20:13:47 +0200414 setting = get_unaligned_le16(sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Marcel Holtmannf383f272008-07-14 20:13:47 +0200416 if (hdev->voice_setting == setting)
417 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Marcel Holtmannf383f272008-07-14 20:13:47 +0200419 hdev->voice_setting = setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300421 BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
Marcel Holtmannf383f272008-07-14 20:13:47 +0200422
Gustavo F. Padovan3c547112011-12-14 22:58:44 -0200423 if (hdev->notify)
Marcel Holtmannf383f272008-07-14 20:13:47 +0200424 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Marcel Holtmannb4cb9fb2013-10-14 13:56:16 -0700427static void hci_cc_read_num_supported_iac(struct hci_dev *hdev,
428 struct sk_buff *skb)
429{
430 struct hci_rp_read_num_supported_iac *rp = (void *) skb->data;
431
432 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
433
434 if (rp->status)
435 return;
436
437 hdev->num_iac = rp->num_iac;
438
439 BT_DBG("%s num iac %d", hdev->name, hdev->num_iac);
440}
441
Marcel Holtmann333140b2008-07-14 20:13:48 +0200442static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
443{
444 __u8 status = *((__u8 *) skb->data);
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300445 struct hci_cp_write_ssp_mode *sent;
Marcel Holtmann333140b2008-07-14 20:13:48 +0200446
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300447 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann333140b2008-07-14 20:13:48 +0200448
Marcel Holtmann333140b2008-07-14 20:13:48 +0200449 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_MODE);
450 if (!sent)
451 return;
452
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300453 if (!status) {
454 if (sent->mode)
Johan Hedbergcad718e2013-04-17 15:00:51 +0300455 hdev->features[1][0] |= LMP_HOST_SSP;
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300456 else
Johan Hedbergcad718e2013-04-17 15:00:51 +0300457 hdev->features[1][0] &= ~LMP_HOST_SSP;
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300458 }
459
Johan Hedberged2c4ee2012-02-17 00:56:28 +0200460 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300461 mgmt_ssp_enable_complete(hdev, sent->mode, status);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +0200462 else if (!status) {
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300463 if (sent->mode)
Johan Hedbergc0ecddc2012-02-22 12:38:31 +0200464 set_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
465 else
466 clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
467 }
Marcel Holtmann333140b2008-07-14 20:13:48 +0200468}
469
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800470static void hci_cc_write_sc_support(struct hci_dev *hdev, struct sk_buff *skb)
471{
472 u8 status = *((u8 *) skb->data);
473 struct hci_cp_write_sc_support *sent;
474
475 BT_DBG("%s status 0x%2.2x", hdev->name, status);
476
477 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SC_SUPPORT);
478 if (!sent)
479 return;
480
481 if (!status) {
482 if (sent->support)
483 hdev->features[1][0] |= LMP_HOST_SC;
484 else
485 hdev->features[1][0] &= ~LMP_HOST_SC;
486 }
487
488 if (test_bit(HCI_MGMT, &hdev->dev_flags))
489 mgmt_sc_enable_complete(hdev, sent->support, status);
490 else if (!status) {
491 if (sent->support)
492 set_bit(HCI_SC_ENABLED, &hdev->dev_flags);
493 else
494 clear_bit(HCI_SC_ENABLED, &hdev->dev_flags);
495 }
496}
497
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200498static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
499{
500 struct hci_rp_read_local_version *rp = (void *) skb->data;
501
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300502 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200503
504 if (rp->status)
Johan Hedberg42c6b122013-03-05 20:37:49 +0200505 return;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200506
Marcel Holtmann0d5551f2013-10-18 12:04:50 -0700507 if (test_bit(HCI_SETUP, &hdev->dev_flags)) {
508 hdev->hci_ver = rp->hci_ver;
509 hdev->hci_rev = __le16_to_cpu(rp->hci_rev);
510 hdev->lmp_ver = rp->lmp_ver;
511 hdev->manufacturer = __le16_to_cpu(rp->manufacturer);
512 hdev->lmp_subver = __le16_to_cpu(rp->lmp_subver);
513 }
Johan Hedbergd5859e22011-01-25 01:19:58 +0200514}
515
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300516static void hci_cc_read_local_commands(struct hci_dev *hdev,
517 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200518{
519 struct hci_rp_read_local_commands *rp = (void *) skb->data;
520
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300521 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200522
Marcel Holtmann6a070e62013-10-31 04:54:33 -0700523 if (rp->status)
524 return;
525
526 if (test_bit(HCI_SETUP, &hdev->dev_flags))
Johan Hedberg2177bab2013-03-05 20:37:43 +0200527 memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200528}
529
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300530static void hci_cc_read_local_features(struct hci_dev *hdev,
531 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200532{
533 struct hci_rp_read_local_features *rp = (void *) skb->data;
534
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300535 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200536
537 if (rp->status)
538 return;
539
540 memcpy(hdev->features, rp->features, 8);
541
542 /* Adjust default settings according to features
543 * supported by device. */
544
Johan Hedbergcad718e2013-04-17 15:00:51 +0300545 if (hdev->features[0][0] & LMP_3SLOT)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200546 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
547
Johan Hedbergcad718e2013-04-17 15:00:51 +0300548 if (hdev->features[0][0] & LMP_5SLOT)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200549 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
550
Johan Hedbergcad718e2013-04-17 15:00:51 +0300551 if (hdev->features[0][1] & LMP_HV2) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200552 hdev->pkt_type |= (HCI_HV2);
553 hdev->esco_type |= (ESCO_HV2);
554 }
555
Johan Hedbergcad718e2013-04-17 15:00:51 +0300556 if (hdev->features[0][1] & LMP_HV3) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200557 hdev->pkt_type |= (HCI_HV3);
558 hdev->esco_type |= (ESCO_HV3);
559 }
560
Andre Guedes45db810f2012-07-24 15:03:49 -0300561 if (lmp_esco_capable(hdev))
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200562 hdev->esco_type |= (ESCO_EV3);
563
Johan Hedbergcad718e2013-04-17 15:00:51 +0300564 if (hdev->features[0][4] & LMP_EV4)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200565 hdev->esco_type |= (ESCO_EV4);
566
Johan Hedbergcad718e2013-04-17 15:00:51 +0300567 if (hdev->features[0][4] & LMP_EV5)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200568 hdev->esco_type |= (ESCO_EV5);
569
Johan Hedbergcad718e2013-04-17 15:00:51 +0300570 if (hdev->features[0][5] & LMP_EDR_ESCO_2M)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100571 hdev->esco_type |= (ESCO_2EV3);
572
Johan Hedbergcad718e2013-04-17 15:00:51 +0300573 if (hdev->features[0][5] & LMP_EDR_ESCO_3M)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100574 hdev->esco_type |= (ESCO_3EV3);
575
Johan Hedbergcad718e2013-04-17 15:00:51 +0300576 if (hdev->features[0][5] & LMP_EDR_3S_ESCO)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100577 hdev->esco_type |= (ESCO_2EV5 | ESCO_3EV5);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200578}
579
Andre Guedes971e3a42011-06-30 19:20:52 -0300580static void hci_cc_read_local_ext_features(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300581 struct sk_buff *skb)
Andre Guedes971e3a42011-06-30 19:20:52 -0300582{
583 struct hci_rp_read_local_ext_features *rp = (void *) skb->data;
584
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300585 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andre Guedes971e3a42011-06-30 19:20:52 -0300586
587 if (rp->status)
Johan Hedberg42c6b122013-03-05 20:37:49 +0200588 return;
Andre Guedes971e3a42011-06-30 19:20:52 -0300589
Marcel Holtmann57af75a2013-10-18 12:04:47 -0700590 if (hdev->max_page < rp->max_page)
591 hdev->max_page = rp->max_page;
Johan Hedbergd2c5d772013-04-17 15:00:52 +0300592
Johan Hedbergcad718e2013-04-17 15:00:51 +0300593 if (rp->page < HCI_MAX_PAGES)
594 memcpy(hdev->features[rp->page], rp->features, 8);
Andre Guedes971e3a42011-06-30 19:20:52 -0300595}
596
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200597static void hci_cc_read_flow_control_mode(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300598 struct sk_buff *skb)
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200599{
600 struct hci_rp_read_flow_control_mode *rp = (void *) skb->data;
601
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300602 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200603
Johan Hedberg42c6b122013-03-05 20:37:49 +0200604 if (!rp->status)
605 hdev->flow_ctl_mode = rp->mode;
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200606}
607
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200608static void hci_cc_read_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
609{
610 struct hci_rp_read_buffer_size *rp = (void *) skb->data;
611
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300612 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200613
614 if (rp->status)
615 return;
616
617 hdev->acl_mtu = __le16_to_cpu(rp->acl_mtu);
618 hdev->sco_mtu = rp->sco_mtu;
619 hdev->acl_pkts = __le16_to_cpu(rp->acl_max_pkt);
620 hdev->sco_pkts = __le16_to_cpu(rp->sco_max_pkt);
621
622 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
623 hdev->sco_mtu = 64;
624 hdev->sco_pkts = 8;
625 }
626
627 hdev->acl_cnt = hdev->acl_pkts;
628 hdev->sco_cnt = hdev->sco_pkts;
629
Gustavo Padovan807deac2012-05-17 00:36:24 -0300630 BT_DBG("%s acl mtu %d:%d sco mtu %d:%d", hdev->name, hdev->acl_mtu,
631 hdev->acl_pkts, hdev->sco_mtu, hdev->sco_pkts);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200632}
633
634static void hci_cc_read_bd_addr(struct hci_dev *hdev, struct sk_buff *skb)
635{
636 struct hci_rp_read_bd_addr *rp = (void *) skb->data;
637
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300638 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200639
640 if (!rp->status)
641 bacpy(&hdev->bdaddr, &rp->bdaddr);
Johan Hedberg23bb5762010-12-21 23:01:27 +0200642}
643
Johan Hedbergf332ec62013-03-15 17:07:11 -0500644static void hci_cc_read_page_scan_activity(struct hci_dev *hdev,
645 struct sk_buff *skb)
646{
647 struct hci_rp_read_page_scan_activity *rp = (void *) skb->data;
648
649 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
650
651 if (test_bit(HCI_INIT, &hdev->flags) && !rp->status) {
652 hdev->page_scan_interval = __le16_to_cpu(rp->interval);
653 hdev->page_scan_window = __le16_to_cpu(rp->window);
654 }
655}
656
Johan Hedberg4a3ee762013-03-15 17:07:12 -0500657static void hci_cc_write_page_scan_activity(struct hci_dev *hdev,
658 struct sk_buff *skb)
659{
660 u8 status = *((u8 *) skb->data);
661 struct hci_cp_write_page_scan_activity *sent;
662
663 BT_DBG("%s status 0x%2.2x", hdev->name, status);
664
665 if (status)
666 return;
667
668 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY);
669 if (!sent)
670 return;
671
672 hdev->page_scan_interval = __le16_to_cpu(sent->interval);
673 hdev->page_scan_window = __le16_to_cpu(sent->window);
674}
675
Johan Hedbergf332ec62013-03-15 17:07:11 -0500676static void hci_cc_read_page_scan_type(struct hci_dev *hdev,
677 struct sk_buff *skb)
678{
679 struct hci_rp_read_page_scan_type *rp = (void *) skb->data;
680
681 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
682
683 if (test_bit(HCI_INIT, &hdev->flags) && !rp->status)
684 hdev->page_scan_type = rp->type;
685}
686
Johan Hedberg4a3ee762013-03-15 17:07:12 -0500687static void hci_cc_write_page_scan_type(struct hci_dev *hdev,
688 struct sk_buff *skb)
689{
690 u8 status = *((u8 *) skb->data);
691 u8 *type;
692
693 BT_DBG("%s status 0x%2.2x", hdev->name, status);
694
695 if (status)
696 return;
697
698 type = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_TYPE);
699 if (type)
700 hdev->page_scan_type = *type;
701}
702
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200703static void hci_cc_read_data_block_size(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300704 struct sk_buff *skb)
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200705{
706 struct hci_rp_read_data_block_size *rp = (void *) skb->data;
707
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300708 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200709
710 if (rp->status)
711 return;
712
713 hdev->block_mtu = __le16_to_cpu(rp->max_acl_len);
714 hdev->block_len = __le16_to_cpu(rp->block_len);
715 hdev->num_blocks = __le16_to_cpu(rp->num_blocks);
716
717 hdev->block_cnt = hdev->num_blocks;
718
719 BT_DBG("%s blk mtu %d cnt %d len %d", hdev->name, hdev->block_mtu,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300720 hdev->block_cnt, hdev->block_len);
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200721}
722
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300723static void hci_cc_read_local_amp_info(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300724 struct sk_buff *skb)
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300725{
726 struct hci_rp_read_local_amp_info *rp = (void *) skb->data;
727
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300728 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300729
730 if (rp->status)
Andrei Emeltchenko8e2a0d92012-09-27 17:26:08 +0300731 goto a2mp_rsp;
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300732
733 hdev->amp_status = rp->amp_status;
734 hdev->amp_total_bw = __le32_to_cpu(rp->total_bw);
735 hdev->amp_max_bw = __le32_to_cpu(rp->max_bw);
736 hdev->amp_min_latency = __le32_to_cpu(rp->min_latency);
737 hdev->amp_max_pdu = __le32_to_cpu(rp->max_pdu);
738 hdev->amp_type = rp->amp_type;
739 hdev->amp_pal_cap = __le16_to_cpu(rp->pal_cap);
740 hdev->amp_assoc_size = __le16_to_cpu(rp->max_assoc_size);
741 hdev->amp_be_flush_to = __le32_to_cpu(rp->be_flush_to);
742 hdev->amp_max_flush_to = __le32_to_cpu(rp->max_flush_to);
743
Andrei Emeltchenko8e2a0d92012-09-27 17:26:08 +0300744a2mp_rsp:
745 a2mp_send_getinfo_rsp(hdev);
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300746}
747
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300748static void hci_cc_read_local_amp_assoc(struct hci_dev *hdev,
749 struct sk_buff *skb)
750{
751 struct hci_rp_read_local_amp_assoc *rp = (void *) skb->data;
752 struct amp_assoc *assoc = &hdev->loc_assoc;
753 size_t rem_len, frag_len;
754
755 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
756
757 if (rp->status)
758 goto a2mp_rsp;
759
760 frag_len = skb->len - sizeof(*rp);
761 rem_len = __le16_to_cpu(rp->rem_len);
762
763 if (rem_len > frag_len) {
Andrei Emeltchenko2e430be32012-09-28 14:44:23 +0300764 BT_DBG("frag_len %zu rem_len %zu", frag_len, rem_len);
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300765
766 memcpy(assoc->data + assoc->offset, rp->frag, frag_len);
767 assoc->offset += frag_len;
768
769 /* Read other fragments */
770 amp_read_loc_assoc_frag(hdev, rp->phy_handle);
771
772 return;
773 }
774
775 memcpy(assoc->data + assoc->offset, rp->frag, rem_len);
776 assoc->len = assoc->offset + rem_len;
777 assoc->offset = 0;
778
779a2mp_rsp:
780 /* Send A2MP Rsp when all fragments are received */
781 a2mp_send_getampassoc_rsp(hdev, rp->status);
Andrei Emeltchenko9495b2e2012-09-27 17:26:22 +0300782 a2mp_send_create_phy_link_req(hdev, rp->status);
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300783}
784
Johan Hedbergd5859e22011-01-25 01:19:58 +0200785static void hci_cc_read_inq_rsp_tx_power(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300786 struct sk_buff *skb)
Johan Hedbergd5859e22011-01-25 01:19:58 +0200787{
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -0700788 struct hci_rp_read_inq_rsp_tx_power *rp = (void *) skb->data;
Johan Hedbergd5859e22011-01-25 01:19:58 +0200789
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300790 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200791
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -0700792 if (!rp->status)
793 hdev->inq_tx_power = rp->tx_power;
Johan Hedbergd5859e22011-01-25 01:19:58 +0200794}
795
Johan Hedberg980e1a52011-01-22 06:10:07 +0200796static void hci_cc_pin_code_reply(struct hci_dev *hdev, struct sk_buff *skb)
797{
798 struct hci_rp_pin_code_reply *rp = (void *) skb->data;
799 struct hci_cp_pin_code_reply *cp;
800 struct hci_conn *conn;
801
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300802 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200803
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200804 hci_dev_lock(hdev);
805
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200806 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg744cf192011-11-08 20:40:14 +0200807 mgmt_pin_code_reply_complete(hdev, &rp->bdaddr, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200808
Mikel Astizfa1bd912012-08-09 09:52:29 +0200809 if (rp->status)
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200810 goto unlock;
Johan Hedberg980e1a52011-01-22 06:10:07 +0200811
812 cp = hci_sent_cmd_data(hdev, HCI_OP_PIN_CODE_REPLY);
813 if (!cp)
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200814 goto unlock;
Johan Hedberg980e1a52011-01-22 06:10:07 +0200815
816 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
817 if (conn)
818 conn->pin_length = cp->pin_len;
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200819
820unlock:
821 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200822}
823
824static void hci_cc_pin_code_neg_reply(struct hci_dev *hdev, struct sk_buff *skb)
825{
826 struct hci_rp_pin_code_neg_reply *rp = (void *) skb->data;
827
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300828 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200829
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200830 hci_dev_lock(hdev);
831
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200832 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg744cf192011-11-08 20:40:14 +0200833 mgmt_pin_code_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300834 rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200835
836 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200837}
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200838
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300839static void hci_cc_le_read_buffer_size(struct hci_dev *hdev,
840 struct sk_buff *skb)
841{
842 struct hci_rp_le_read_buffer_size *rp = (void *) skb->data;
843
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300844 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300845
846 if (rp->status)
847 return;
848
849 hdev->le_mtu = __le16_to_cpu(rp->le_mtu);
850 hdev->le_pkts = rp->le_max_pkt;
851
852 hdev->le_cnt = hdev->le_pkts;
853
854 BT_DBG("%s le mtu %d:%d", hdev->name, hdev->le_mtu, hdev->le_pkts);
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300855}
Johan Hedberg980e1a52011-01-22 06:10:07 +0200856
Johan Hedberg60e77322013-01-22 14:01:59 +0200857static void hci_cc_le_read_local_features(struct hci_dev *hdev,
858 struct sk_buff *skb)
859{
860 struct hci_rp_le_read_local_features *rp = (void *) skb->data;
861
862 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
863
864 if (!rp->status)
865 memcpy(hdev->le_features, rp->features, 8);
Johan Hedberg60e77322013-01-22 14:01:59 +0200866}
867
Johan Hedberg8fa19092012-10-19 20:57:49 +0300868static void hci_cc_le_read_adv_tx_power(struct hci_dev *hdev,
869 struct sk_buff *skb)
870{
871 struct hci_rp_le_read_adv_tx_power *rp = (void *) skb->data;
872
873 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
874
Johan Hedberg04b4edc2013-03-15 17:07:01 -0500875 if (!rp->status)
Johan Hedberg8fa19092012-10-19 20:57:49 +0300876 hdev->adv_tx_power = rp->tx_power;
Johan Hedberg8fa19092012-10-19 20:57:49 +0300877}
878
Johan Hedberga5c29682011-02-19 12:05:57 -0300879static void hci_cc_user_confirm_reply(struct hci_dev *hdev, struct sk_buff *skb)
880{
881 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
882
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300883 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberga5c29682011-02-19 12:05:57 -0300884
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200885 hci_dev_lock(hdev);
886
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200887 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300888 mgmt_user_confirm_reply_complete(hdev, &rp->bdaddr, ACL_LINK, 0,
889 rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200890
891 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -0300892}
893
894static void hci_cc_user_confirm_neg_reply(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300895 struct sk_buff *skb)
Johan Hedberga5c29682011-02-19 12:05:57 -0300896{
897 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
898
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300899 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberga5c29682011-02-19 12:05:57 -0300900
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200901 hci_dev_lock(hdev);
902
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200903 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg744cf192011-11-08 20:40:14 +0200904 mgmt_user_confirm_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300905 ACL_LINK, 0, rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200906
907 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -0300908}
909
Brian Gix1143d452011-11-23 08:28:34 -0800910static void hci_cc_user_passkey_reply(struct hci_dev *hdev, struct sk_buff *skb)
911{
912 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
913
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300914 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -0800915
916 hci_dev_lock(hdev);
917
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200918 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg272d90d2012-02-09 15:26:12 +0200919 mgmt_user_passkey_reply_complete(hdev, &rp->bdaddr, ACL_LINK,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300920 0, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -0800921
922 hci_dev_unlock(hdev);
923}
924
925static void hci_cc_user_passkey_neg_reply(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300926 struct sk_buff *skb)
Brian Gix1143d452011-11-23 08:28:34 -0800927{
928 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
929
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300930 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -0800931
932 hci_dev_lock(hdev);
933
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200934 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Brian Gix1143d452011-11-23 08:28:34 -0800935 mgmt_user_passkey_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300936 ACL_LINK, 0, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -0800937
938 hci_dev_unlock(hdev);
939}
940
Marcel Holtmann4d2d2792014-01-10 02:07:26 -0800941static void hci_cc_read_local_oob_data(struct hci_dev *hdev,
942 struct sk_buff *skb)
Szymon Jancc35938b2011-03-22 13:12:21 +0100943{
944 struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
945
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300946 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Szymon Jancc35938b2011-03-22 13:12:21 +0100947
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200948 hci_dev_lock(hdev);
Marcel Holtmann4d2d2792014-01-10 02:07:26 -0800949 mgmt_read_local_oob_data_complete(hdev, rp->hash, rp->randomizer,
950 NULL, NULL, rp->status);
951 hci_dev_unlock(hdev);
952}
953
954static void hci_cc_read_local_oob_ext_data(struct hci_dev *hdev,
955 struct sk_buff *skb)
956{
957 struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data;
958
959 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
960
961 hci_dev_lock(hdev);
962 mgmt_read_local_oob_data_complete(hdev, rp->hash192, rp->randomizer192,
963 rp->hash256, rp->randomizer256,
964 rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200965 hci_dev_unlock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +0100966}
967
Marcel Holtmann7a4cd512014-02-19 19:52:13 -0800968
969static void hci_cc_le_set_random_addr(struct hci_dev *hdev, struct sk_buff *skb)
970{
971 __u8 status = *((__u8 *) skb->data);
972 bdaddr_t *sent;
973
974 BT_DBG("%s status 0x%2.2x", hdev->name, status);
975
976 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_RANDOM_ADDR);
977 if (!sent)
978 return;
979
980 hci_dev_lock(hdev);
981
982 if (!status)
983 bacpy(&hdev->random_addr, sent);
984
985 hci_dev_unlock(hdev);
986}
987
Johan Hedbergc1d5dc42012-11-08 01:23:01 +0100988static void hci_cc_le_set_adv_enable(struct hci_dev *hdev, struct sk_buff *skb)
989{
990 __u8 *sent, status = *((__u8 *) skb->data);
991
992 BT_DBG("%s status 0x%2.2x", hdev->name, status);
993
994 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_ENABLE);
995 if (!sent)
996 return;
997
Johan Hedberg3c857752014-03-25 10:30:49 +0200998 if (status)
999 return;
1000
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001001 hci_dev_lock(hdev);
1002
Johan Hedberg3c857752014-03-25 10:30:49 +02001003 /* If we're doing connection initation as peripheral. Set a
1004 * timeout in case something goes wrong.
1005 */
1006 if (*sent) {
1007 struct hci_conn *conn;
1008
1009 conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
1010 if (conn)
1011 queue_delayed_work(hdev->workqueue,
1012 &conn->le_conn_timeout,
1013 HCI_LE_CONN_TIMEOUT);
1014 }
1015
1016 mgmt_advertising(hdev, *sent);
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001017
Johan Hedberg04b4edc2013-03-15 17:07:01 -05001018 hci_dev_unlock(hdev);
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001019}
1020
Marcel Holtmann533553f2014-03-21 12:18:10 -07001021static void hci_cc_le_set_scan_param(struct hci_dev *hdev, struct sk_buff *skb)
1022{
1023 struct hci_cp_le_set_scan_param *cp;
1024 __u8 status = *((__u8 *) skb->data);
1025
1026 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1027
1028 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_PARAM);
1029 if (!cp)
1030 return;
1031
1032 hci_dev_lock(hdev);
1033
1034 if (!status)
1035 hdev->le_scan_type = cp->type;
1036
1037 hci_dev_unlock(hdev);
1038}
1039
Johan Hedbergb9a63282014-03-25 10:51:52 +02001040static bool has_pending_adv_report(struct hci_dev *hdev)
1041{
1042 struct discovery_state *d = &hdev->discovery;
1043
1044 return bacmp(&d->last_adv_addr, BDADDR_ANY);
1045}
1046
1047static void clear_pending_adv_report(struct hci_dev *hdev)
1048{
1049 struct discovery_state *d = &hdev->discovery;
1050
1051 bacpy(&d->last_adv_addr, BDADDR_ANY);
1052 d->last_adv_data_len = 0;
1053}
1054
1055static void store_pending_adv_report(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedbergff5cd292014-03-25 14:40:52 +02001056 u8 bdaddr_type, s8 rssi, u8 *data, u8 len)
Johan Hedbergb9a63282014-03-25 10:51:52 +02001057{
1058 struct discovery_state *d = &hdev->discovery;
1059
1060 bacpy(&d->last_adv_addr, bdaddr);
1061 d->last_adv_addr_type = bdaddr_type;
Johan Hedbergff5cd292014-03-25 14:40:52 +02001062 d->last_adv_rssi = rssi;
Johan Hedbergb9a63282014-03-25 10:51:52 +02001063 memcpy(d->last_adv_data, data, len);
1064 d->last_adv_data_len = len;
1065}
1066
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001067static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001068 struct sk_buff *skb)
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001069{
1070 struct hci_cp_le_set_scan_enable *cp;
1071 __u8 status = *((__u8 *) skb->data);
1072
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001073 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001074
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001075 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_ENABLE);
1076 if (!cp)
1077 return;
1078
Andre Guedes3fd319b2013-04-30 15:29:36 -03001079 if (status)
1080 return;
1081
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001082 switch (cp->enable) {
Andre Guedes76a388b2013-04-04 20:21:02 -03001083 case LE_SCAN_ENABLE:
Andre Guedesd23264a2011-11-25 20:53:38 -03001084 set_bit(HCI_LE_SCAN, &hdev->dev_flags);
Johan Hedbergb9a63282014-03-25 10:51:52 +02001085 if (hdev->le_scan_type == LE_SCAN_ACTIVE)
1086 clear_pending_adv_report(hdev);
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001087 break;
1088
Andre Guedes76a388b2013-04-04 20:21:02 -03001089 case LE_SCAN_DISABLE:
Johan Hedbergb9a63282014-03-25 10:51:52 +02001090 /* We do this here instead of when setting DISCOVERY_STOPPED
1091 * since the latter would potentially require waiting for
1092 * inquiry to stop too.
1093 */
1094 if (has_pending_adv_report(hdev)) {
1095 struct discovery_state *d = &hdev->discovery;
1096
1097 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Johan Hedbergab0aa432014-03-26 14:17:12 +02001098 d->last_adv_addr_type, NULL,
1099 d->last_adv_rssi, 0, 1,
1100 d->last_adv_data,
Johan Hedbergb9a63282014-03-25 10:51:52 +02001101 d->last_adv_data_len, NULL, 0);
1102 }
1103
Johan Hedberg317ac8c2014-02-28 20:26:12 +02001104 /* Cancel this timer so that we don't try to disable scanning
1105 * when it's already disabled.
1106 */
1107 cancel_delayed_work(&hdev->le_scan_disable);
1108
Andre Guedesd23264a2011-11-25 20:53:38 -03001109 clear_bit(HCI_LE_SCAN, &hdev->dev_flags);
Johan Hedberg81ad6fd2014-02-28 20:26:13 +02001110 /* The HCI_LE_SCAN_INTERRUPTED flag indicates that we
1111 * interrupted scanning due to a connect request. Mark
1112 * therefore discovery as stopped.
1113 */
1114 if (test_and_clear_bit(HCI_LE_SCAN_INTERRUPTED,
1115 &hdev->dev_flags))
1116 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001117 break;
1118
1119 default:
1120 BT_ERR("Used reserved LE_Scan_Enable param %d", cp->enable);
1121 break;
Andre Guedes35815082011-05-26 16:23:53 -03001122 }
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001123}
1124
Johan Hedbergcf1d0812013-01-22 14:02:00 +02001125static void hci_cc_le_read_white_list_size(struct hci_dev *hdev,
1126 struct sk_buff *skb)
1127{
1128 struct hci_rp_le_read_white_list_size *rp = (void *) skb->data;
1129
1130 BT_DBG("%s status 0x%2.2x size %u", hdev->name, rp->status, rp->size);
1131
1132 if (!rp->status)
1133 hdev->le_white_list_size = rp->size;
Johan Hedbergcf1d0812013-01-22 14:02:00 +02001134}
1135
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001136static void hci_cc_le_clear_white_list(struct hci_dev *hdev,
1137 struct sk_buff *skb)
1138{
1139 __u8 status = *((__u8 *) skb->data);
1140
1141 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1142
1143 if (!status)
1144 hci_white_list_clear(hdev);
1145}
1146
1147static void hci_cc_le_add_to_white_list(struct hci_dev *hdev,
1148 struct sk_buff *skb)
1149{
1150 struct hci_cp_le_add_to_white_list *sent;
1151 __u8 status = *((__u8 *) skb->data);
1152
1153 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1154
1155 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_WHITE_LIST);
1156 if (!sent)
1157 return;
1158
1159 if (!status)
1160 hci_white_list_add(hdev, &sent->bdaddr, sent->bdaddr_type);
1161}
1162
1163static void hci_cc_le_del_from_white_list(struct hci_dev *hdev,
1164 struct sk_buff *skb)
1165{
1166 struct hci_cp_le_del_from_white_list *sent;
1167 __u8 status = *((__u8 *) skb->data);
1168
1169 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1170
1171 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_DEL_FROM_WHITE_LIST);
1172 if (!sent)
1173 return;
1174
1175 if (!status)
1176 hci_white_list_del(hdev, &sent->bdaddr, sent->bdaddr_type);
1177}
1178
Johan Hedberg9b008c02013-01-22 14:02:01 +02001179static void hci_cc_le_read_supported_states(struct hci_dev *hdev,
1180 struct sk_buff *skb)
1181{
1182 struct hci_rp_le_read_supported_states *rp = (void *) skb->data;
1183
1184 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1185
1186 if (!rp->status)
1187 memcpy(hdev->le_states, rp->le_states, 8);
Johan Hedberg9b008c02013-01-22 14:02:01 +02001188}
1189
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001190static void hci_cc_write_le_host_supported(struct hci_dev *hdev,
1191 struct sk_buff *skb)
Andre Guedesf9b49302011-06-30 19:20:53 -03001192{
Johan Hedberg06199cf2012-02-22 16:37:11 +02001193 struct hci_cp_write_le_host_supported *sent;
Andre Guedesf9b49302011-06-30 19:20:53 -03001194 __u8 status = *((__u8 *) skb->data);
1195
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001196 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andre Guedesf9b49302011-06-30 19:20:53 -03001197
Johan Hedberg06199cf2012-02-22 16:37:11 +02001198 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED);
Johan Hedberg8f984df2012-02-28 01:07:22 +02001199 if (!sent)
Andre Guedesf9b49302011-06-30 19:20:53 -03001200 return;
1201
Johan Hedberg8f984df2012-02-28 01:07:22 +02001202 if (!status) {
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001203 if (sent->le) {
Johan Hedbergcad718e2013-04-17 15:00:51 +03001204 hdev->features[1][0] |= LMP_HOST_LE;
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001205 set_bit(HCI_LE_ENABLED, &hdev->dev_flags);
1206 } else {
Johan Hedbergcad718e2013-04-17 15:00:51 +03001207 hdev->features[1][0] &= ~LMP_HOST_LE;
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001208 clear_bit(HCI_LE_ENABLED, &hdev->dev_flags);
Johan Hedbergf3d3444a2013-10-05 12:01:04 +02001209 clear_bit(HCI_ADVERTISING, &hdev->dev_flags);
Johan Hedberg416a4ae2013-09-25 13:26:08 +03001210 }
Johan Hedberg53b2caa2012-10-24 21:11:59 +03001211
1212 if (sent->simul)
Johan Hedbergcad718e2013-04-17 15:00:51 +03001213 hdev->features[1][0] |= LMP_HOST_LE_BREDR;
Johan Hedberg53b2caa2012-10-24 21:11:59 +03001214 else
Johan Hedbergcad718e2013-04-17 15:00:51 +03001215 hdev->features[1][0] &= ~LMP_HOST_LE_BREDR;
Johan Hedberg8f984df2012-02-28 01:07:22 +02001216 }
Andre Guedesf9b49302011-06-30 19:20:53 -03001217}
1218
Johan Hedberg56ed2cb2014-02-27 14:05:40 +02001219static void hci_cc_set_adv_param(struct hci_dev *hdev, struct sk_buff *skb)
1220{
1221 struct hci_cp_le_set_adv_param *cp;
1222 u8 status = *((u8 *) skb->data);
1223
1224 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1225
1226 if (status)
1227 return;
1228
1229 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_PARAM);
1230 if (!cp)
1231 return;
1232
1233 hci_dev_lock(hdev);
1234 hdev->adv_addr_type = cp->own_address_type;
1235 hci_dev_unlock(hdev);
1236}
1237
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +03001238static void hci_cc_write_remote_amp_assoc(struct hci_dev *hdev,
1239 struct sk_buff *skb)
1240{
1241 struct hci_rp_write_remote_amp_assoc *rp = (void *) skb->data;
1242
1243 BT_DBG("%s status 0x%2.2x phy_handle 0x%2.2x",
1244 hdev->name, rp->status, rp->phy_handle);
1245
1246 if (rp->status)
1247 return;
1248
1249 amp_write_rem_assoc_continue(hdev, rp->phy_handle);
1250}
1251
Andrzej Kaczmarek5ae76a92014-05-08 15:32:08 +02001252static void hci_cc_read_rssi(struct hci_dev *hdev, struct sk_buff *skb)
1253{
1254 struct hci_rp_read_rssi *rp = (void *) skb->data;
1255 struct hci_conn *conn;
1256
1257 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1258
1259 if (rp->status)
1260 return;
1261
1262 hci_dev_lock(hdev);
1263
1264 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
1265 if (conn)
1266 conn->rssi = rp->rssi;
1267
1268 hci_dev_unlock(hdev);
1269}
1270
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02001271static void hci_cc_read_tx_power(struct hci_dev *hdev, struct sk_buff *skb)
1272{
1273 struct hci_cp_read_tx_power *sent;
1274 struct hci_rp_read_tx_power *rp = (void *) skb->data;
1275 struct hci_conn *conn;
1276
1277 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1278
1279 if (rp->status)
1280 return;
1281
1282 sent = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
1283 if (!sent)
1284 return;
1285
1286 hci_dev_lock(hdev);
1287
1288 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Andrzej Kaczmarekd0455ed2014-05-14 13:43:05 +02001289 if (!conn)
1290 goto unlock;
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02001291
Andrzej Kaczmarekd0455ed2014-05-14 13:43:05 +02001292 switch (sent->type) {
1293 case 0x00:
1294 conn->tx_power = rp->tx_power;
1295 break;
1296 case 0x01:
1297 conn->max_tx_power = rp->tx_power;
1298 break;
1299 }
1300
1301unlock:
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02001302 hci_dev_unlock(hdev);
1303}
1304
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001305static void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001306{
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001307 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001308
1309 if (status) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001310 hci_conn_check_pending(hdev);
Johan Hedberg314b2382011-04-27 10:29:57 -04001311 return;
1312 }
1313
Andre Guedes89352e72011-11-04 14:16:53 -03001314 set_bit(HCI_INQUIRY, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001315}
1316
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001317static void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001319 struct hci_cp_create_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001322 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001323
1324 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_CONN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 if (!cp)
1326 return;
1327
1328 hci_dev_lock(hdev);
1329
1330 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
1331
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03001332 BT_DBG("%s bdaddr %pMR hcon %p", hdev->name, &cp->bdaddr, conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 if (status) {
1335 if (conn && conn->state == BT_CONNECT) {
Marcel Holtmann4c67bc72006-10-15 17:30:56 +02001336 if (status != 0x0c || conn->attempt > 2) {
1337 conn->state = BT_CLOSED;
1338 hci_proto_connect_cfm(conn, status);
1339 hci_conn_del(conn);
1340 } else
1341 conn->state = BT_CONNECT2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
1343 } else {
1344 if (!conn) {
1345 conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr);
1346 if (conn) {
Johan Hedberga0c808b2012-01-16 09:49:58 +02001347 conn->out = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 conn->link_mode |= HCI_LM_MASTER;
1349 } else
Gustavo F. Padovan893ef972010-07-18 15:13:37 -03001350 BT_ERR("No memory for new connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
1352 }
1353
1354 hci_dev_unlock(hdev);
1355}
1356
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001357static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001359 struct hci_cp_add_sco *cp;
1360 struct hci_conn *acl, *sco;
1361 __u16 handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001363 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001364
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001365 if (!status)
1366 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001368 cp = hci_sent_cmd_data(hdev, HCI_OP_ADD_SCO);
1369 if (!cp)
1370 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001372 handle = __le16_to_cpu(cp->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001374 BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
Marcel Holtmann6bd57412006-11-18 22:14:22 +01001375
1376 hci_dev_lock(hdev);
1377
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001378 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001379 if (acl) {
1380 sco = acl->link;
1381 if (sco) {
1382 sco->state = BT_CLOSED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001383
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001384 hci_proto_connect_cfm(sco, status);
1385 hci_conn_del(sco);
1386 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001387 }
Marcel Holtmann6bd57412006-11-18 22:14:22 +01001388
1389 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
1391
Marcel Holtmannf8558552008-07-14 20:13:49 +02001392static void hci_cs_auth_requested(struct hci_dev *hdev, __u8 status)
1393{
1394 struct hci_cp_auth_requested *cp;
1395 struct hci_conn *conn;
1396
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001397 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001398
1399 if (!status)
1400 return;
1401
1402 cp = hci_sent_cmd_data(hdev, HCI_OP_AUTH_REQUESTED);
1403 if (!cp)
1404 return;
1405
1406 hci_dev_lock(hdev);
1407
1408 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1409 if (conn) {
1410 if (conn->state == BT_CONFIG) {
1411 hci_proto_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02001412 hci_conn_drop(conn);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001413 }
1414 }
1415
1416 hci_dev_unlock(hdev);
1417}
1418
1419static void hci_cs_set_conn_encrypt(struct hci_dev *hdev, __u8 status)
1420{
1421 struct hci_cp_set_conn_encrypt *cp;
1422 struct hci_conn *conn;
1423
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001424 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001425
1426 if (!status)
1427 return;
1428
1429 cp = hci_sent_cmd_data(hdev, HCI_OP_SET_CONN_ENCRYPT);
1430 if (!cp)
1431 return;
1432
1433 hci_dev_lock(hdev);
1434
1435 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1436 if (conn) {
1437 if (conn->state == BT_CONFIG) {
1438 hci_proto_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02001439 hci_conn_drop(conn);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001440 }
1441 }
1442
1443 hci_dev_unlock(hdev);
1444}
1445
Johan Hedberg127178d2010-11-18 22:22:29 +02001446static int hci_outgoing_auth_needed(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001447 struct hci_conn *conn)
Johan Hedberg392599b2010-11-18 22:22:28 +02001448{
Johan Hedberg392599b2010-11-18 22:22:28 +02001449 if (conn->state != BT_CONFIG || !conn->out)
1450 return 0;
1451
Johan Hedberg765c2a92011-01-19 12:06:52 +05301452 if (conn->pending_sec_level == BT_SECURITY_SDP)
Johan Hedberg392599b2010-11-18 22:22:28 +02001453 return 0;
1454
1455 /* Only request authentication for SSP connections or non-SSP
Johan Hedberg264b8b42014-01-08 16:40:39 +02001456 * devices with sec_level MEDIUM or HIGH or if MITM protection
1457 * is requested.
1458 */
Gustavo Padovan807deac2012-05-17 00:36:24 -03001459 if (!hci_conn_ssp_enabled(conn) && !(conn->auth_type & 0x01) &&
Johan Hedberg7e3691e2014-05-30 14:45:19 +03001460 conn->pending_sec_level != BT_SECURITY_FIPS &&
Johan Hedberg264b8b42014-01-08 16:40:39 +02001461 conn->pending_sec_level != BT_SECURITY_HIGH &&
1462 conn->pending_sec_level != BT_SECURITY_MEDIUM)
Johan Hedberg392599b2010-11-18 22:22:28 +02001463 return 0;
1464
Johan Hedberg392599b2010-11-18 22:22:28 +02001465 return 1;
1466}
1467
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001468static int hci_resolve_name(struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001469 struct inquiry_entry *e)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001470{
1471 struct hci_cp_remote_name_req cp;
1472
1473 memset(&cp, 0, sizeof(cp));
1474
1475 bacpy(&cp.bdaddr, &e->data.bdaddr);
1476 cp.pscan_rep_mode = e->data.pscan_rep_mode;
1477 cp.pscan_mode = e->data.pscan_mode;
1478 cp.clock_offset = e->data.clock_offset;
1479
1480 return hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
1481}
1482
Johan Hedbergb644ba32012-01-17 21:48:47 +02001483static bool hci_resolve_next_name(struct hci_dev *hdev)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001484{
1485 struct discovery_state *discov = &hdev->discovery;
1486 struct inquiry_entry *e;
1487
Johan Hedbergb644ba32012-01-17 21:48:47 +02001488 if (list_empty(&discov->resolve))
1489 return false;
1490
1491 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
Ram Malovanyc8100892012-07-19 10:26:09 +03001492 if (!e)
1493 return false;
1494
Johan Hedbergb644ba32012-01-17 21:48:47 +02001495 if (hci_resolve_name(hdev, e) == 0) {
1496 e->name_state = NAME_PENDING;
1497 return true;
1498 }
1499
1500 return false;
1501}
1502
1503static void hci_check_pending_name(struct hci_dev *hdev, struct hci_conn *conn,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001504 bdaddr_t *bdaddr, u8 *name, u8 name_len)
Johan Hedbergb644ba32012-01-17 21:48:47 +02001505{
1506 struct discovery_state *discov = &hdev->discovery;
1507 struct inquiry_entry *e;
1508
1509 if (conn && !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001510 mgmt_device_connected(hdev, bdaddr, ACL_LINK, 0x00, 0, name,
1511 name_len, conn->dev_class);
Johan Hedbergb644ba32012-01-17 21:48:47 +02001512
1513 if (discov->state == DISCOVERY_STOPPED)
1514 return;
1515
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001516 if (discov->state == DISCOVERY_STOPPING)
1517 goto discov_complete;
1518
1519 if (discov->state != DISCOVERY_RESOLVING)
1520 return;
1521
1522 e = hci_inquiry_cache_lookup_resolve(hdev, bdaddr, NAME_PENDING);
Ram Malovany7cc83802012-07-19 10:26:10 +03001523 /* If the device was not found in a list of found devices names of which
1524 * are pending. there is no need to continue resolving a next name as it
1525 * will be done upon receiving another Remote Name Request Complete
1526 * Event */
1527 if (!e)
1528 return;
1529
1530 list_del(&e->list);
1531 if (name) {
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001532 e->name_state = NAME_KNOWN;
Ram Malovany7cc83802012-07-19 10:26:10 +03001533 mgmt_remote_name(hdev, bdaddr, ACL_LINK, 0x00,
1534 e->data.rssi, name, name_len);
Ram Malovanyc3e7c0d2012-07-19 10:26:11 +03001535 } else {
1536 e->name_state = NAME_NOT_KNOWN;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001537 }
1538
Johan Hedbergb644ba32012-01-17 21:48:47 +02001539 if (hci_resolve_next_name(hdev))
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001540 return;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001541
1542discov_complete:
1543 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1544}
1545
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001546static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
1547{
Johan Hedberg127178d2010-11-18 22:22:29 +02001548 struct hci_cp_remote_name_req *cp;
1549 struct hci_conn *conn;
1550
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001551 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Johan Hedberg127178d2010-11-18 22:22:29 +02001552
1553 /* If successful wait for the name req complete event before
1554 * checking for the need to do authentication */
1555 if (!status)
1556 return;
1557
1558 cp = hci_sent_cmd_data(hdev, HCI_OP_REMOTE_NAME_REQ);
1559 if (!cp)
1560 return;
1561
1562 hci_dev_lock(hdev);
1563
1564 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
Johan Hedbergb644ba32012-01-17 21:48:47 +02001565
1566 if (test_bit(HCI_MGMT, &hdev->dev_flags))
1567 hci_check_pending_name(hdev, conn, &cp->bdaddr, NULL, 0);
1568
Johan Hedberg79c6c702011-04-28 11:28:55 -07001569 if (!conn)
1570 goto unlock;
1571
1572 if (!hci_outgoing_auth_needed(hdev, conn))
1573 goto unlock;
1574
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001575 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
Johannes Bergc1f23a22013-10-07 18:19:16 +02001576 struct hci_cp_auth_requested auth_cp;
1577
1578 auth_cp.handle = __cpu_to_le16(conn->handle);
1579 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED,
1580 sizeof(auth_cp), &auth_cp);
Johan Hedberg127178d2010-11-18 22:22:29 +02001581 }
1582
Johan Hedberg79c6c702011-04-28 11:28:55 -07001583unlock:
Johan Hedberg127178d2010-11-18 22:22:29 +02001584 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001585}
1586
Marcel Holtmann769be972008-07-14 20:13:49 +02001587static void hci_cs_read_remote_features(struct hci_dev *hdev, __u8 status)
1588{
1589 struct hci_cp_read_remote_features *cp;
1590 struct hci_conn *conn;
1591
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001592 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann769be972008-07-14 20:13:49 +02001593
1594 if (!status)
1595 return;
1596
1597 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_FEATURES);
1598 if (!cp)
1599 return;
1600
1601 hci_dev_lock(hdev);
1602
1603 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1604 if (conn) {
1605 if (conn->state == BT_CONFIG) {
Marcel Holtmann769be972008-07-14 20:13:49 +02001606 hci_proto_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02001607 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02001608 }
1609 }
1610
1611 hci_dev_unlock(hdev);
1612}
1613
1614static void hci_cs_read_remote_ext_features(struct hci_dev *hdev, __u8 status)
1615{
1616 struct hci_cp_read_remote_ext_features *cp;
1617 struct hci_conn *conn;
1618
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001619 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann769be972008-07-14 20:13:49 +02001620
1621 if (!status)
1622 return;
1623
1624 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES);
1625 if (!cp)
1626 return;
1627
1628 hci_dev_lock(hdev);
1629
1630 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1631 if (conn) {
1632 if (conn->state == BT_CONFIG) {
Marcel Holtmann769be972008-07-14 20:13:49 +02001633 hci_proto_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02001634 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02001635 }
1636 }
1637
1638 hci_dev_unlock(hdev);
1639}
1640
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001641static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
1642{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001643 struct hci_cp_setup_sync_conn *cp;
1644 struct hci_conn *acl, *sco;
1645 __u16 handle;
1646
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001647 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001648
1649 if (!status)
1650 return;
1651
1652 cp = hci_sent_cmd_data(hdev, HCI_OP_SETUP_SYNC_CONN);
1653 if (!cp)
1654 return;
1655
1656 handle = __le16_to_cpu(cp->handle);
1657
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001658 BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001659
1660 hci_dev_lock(hdev);
1661
1662 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001663 if (acl) {
1664 sco = acl->link;
1665 if (sco) {
1666 sco->state = BT_CLOSED;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001667
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001668 hci_proto_connect_cfm(sco, status);
1669 hci_conn_del(sco);
1670 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001671 }
1672
1673 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001674}
1675
1676static void hci_cs_sniff_mode(struct hci_dev *hdev, __u8 status)
1677{
1678 struct hci_cp_sniff_mode *cp;
1679 struct hci_conn *conn;
1680
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001681 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001682
1683 if (!status)
1684 return;
1685
1686 cp = hci_sent_cmd_data(hdev, HCI_OP_SNIFF_MODE);
1687 if (!cp)
1688 return;
1689
1690 hci_dev_lock(hdev);
1691
1692 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001693 if (conn) {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001694 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001695
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001696 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001697 hci_sco_setup(conn, status);
1698 }
1699
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001700 hci_dev_unlock(hdev);
1701}
1702
1703static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
1704{
1705 struct hci_cp_exit_sniff_mode *cp;
1706 struct hci_conn *conn;
1707
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001708 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001709
1710 if (!status)
1711 return;
1712
1713 cp = hci_sent_cmd_data(hdev, HCI_OP_EXIT_SNIFF_MODE);
1714 if (!cp)
1715 return;
1716
1717 hci_dev_lock(hdev);
1718
1719 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001720 if (conn) {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001721 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001722
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001723 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001724 hci_sco_setup(conn, status);
1725 }
1726
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001727 hci_dev_unlock(hdev);
1728}
1729
Johan Hedberg88c3df12012-02-09 14:27:38 +02001730static void hci_cs_disconnect(struct hci_dev *hdev, u8 status)
1731{
1732 struct hci_cp_disconnect *cp;
1733 struct hci_conn *conn;
1734
1735 if (!status)
1736 return;
1737
1738 cp = hci_sent_cmd_data(hdev, HCI_OP_DISCONNECT);
1739 if (!cp)
1740 return;
1741
1742 hci_dev_lock(hdev);
1743
1744 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1745 if (conn)
1746 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001747 conn->dst_type, status);
Johan Hedberg88c3df12012-02-09 14:27:38 +02001748
1749 hci_dev_unlock(hdev);
1750}
1751
Andrei Emeltchenkoa02226d2012-09-27 17:26:19 +03001752static void hci_cs_create_phylink(struct hci_dev *hdev, u8 status)
1753{
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +03001754 struct hci_cp_create_phy_link *cp;
1755
Andrei Emeltchenkoa02226d2012-09-27 17:26:19 +03001756 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +03001757
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +03001758 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_PHY_LINK);
1759 if (!cp)
1760 return;
1761
Andrei Emeltchenkoe58917b2012-10-31 15:46:33 +02001762 hci_dev_lock(hdev);
1763
1764 if (status) {
1765 struct hci_conn *hcon;
1766
1767 hcon = hci_conn_hash_lookup_handle(hdev, cp->phy_handle);
1768 if (hcon)
1769 hci_conn_del(hcon);
1770 } else {
1771 amp_write_remote_assoc(hdev, cp->phy_handle);
1772 }
1773
1774 hci_dev_unlock(hdev);
Andrei Emeltchenkoa02226d2012-09-27 17:26:19 +03001775}
1776
Andrei Emeltchenko0b26ab92012-09-27 17:26:24 +03001777static void hci_cs_accept_phylink(struct hci_dev *hdev, u8 status)
1778{
1779 struct hci_cp_accept_phy_link *cp;
1780
1781 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1782
1783 if (status)
1784 return;
1785
1786 cp = hci_sent_cmd_data(hdev, HCI_OP_ACCEPT_PHY_LINK);
1787 if (!cp)
1788 return;
1789
1790 amp_write_remote_assoc(hdev, cp->phy_handle);
1791}
1792
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02001793static void hci_cs_le_create_conn(struct hci_dev *hdev, u8 status)
1794{
1795 struct hci_cp_le_create_conn *cp;
1796 struct hci_conn *conn;
1797
1798 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1799
1800 /* All connection failure handling is taken care of by the
1801 * hci_le_conn_failed function which is triggered by the HCI
1802 * request completion callbacks used for connecting.
1803 */
1804 if (status)
1805 return;
1806
1807 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_CREATE_CONN);
1808 if (!cp)
1809 return;
1810
1811 hci_dev_lock(hdev);
1812
1813 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->peer_addr);
1814 if (!conn)
1815 goto unlock;
1816
1817 /* Store the initiator and responder address information which
1818 * is needed for SMP. These values will not change during the
1819 * lifetime of the connection.
1820 */
1821 conn->init_addr_type = cp->own_address_type;
1822 if (cp->own_address_type == ADDR_LE_DEV_RANDOM)
1823 bacpy(&conn->init_addr, &hdev->random_addr);
1824 else
1825 bacpy(&conn->init_addr, &hdev->bdaddr);
1826
1827 conn->resp_addr_type = cp->peer_addr_type;
1828 bacpy(&conn->resp_addr, &cp->peer_addr);
1829
Johan Hedberg9489eca2014-02-28 17:45:46 +02001830 /* We don't want the connection attempt to stick around
1831 * indefinitely since LE doesn't have a page timeout concept
1832 * like BR/EDR. Set a timer for any connection that doesn't use
1833 * the white list for connecting.
1834 */
1835 if (cp->filter_policy == HCI_LE_USE_PEER_ADDR)
1836 queue_delayed_work(conn->hdev->workqueue,
1837 &conn->le_conn_timeout,
1838 HCI_LE_CONN_TIMEOUT);
1839
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02001840unlock:
1841 hci_dev_unlock(hdev);
1842}
1843
Johan Hedberg81d0c8a2014-03-24 14:39:04 +02001844static void hci_cs_le_start_enc(struct hci_dev *hdev, u8 status)
1845{
1846 struct hci_cp_le_start_enc *cp;
1847 struct hci_conn *conn;
1848
1849 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1850
1851 if (!status)
1852 return;
1853
1854 hci_dev_lock(hdev);
1855
1856 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_START_ENC);
1857 if (!cp)
1858 goto unlock;
1859
1860 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1861 if (!conn)
1862 goto unlock;
1863
1864 if (conn->state != BT_CONNECTED)
1865 goto unlock;
1866
1867 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
1868 hci_conn_drop(conn);
1869
1870unlock:
1871 hci_dev_unlock(hdev);
1872}
1873
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001874static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001875{
1876 __u8 status = *((__u8 *) skb->data);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001877 struct discovery_state *discov = &hdev->discovery;
1878 struct inquiry_entry *e;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001879
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001880 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001881
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001882 hci_conn_check_pending(hdev);
Andre Guedes89352e72011-11-04 14:16:53 -03001883
1884 if (!test_and_clear_bit(HCI_INQUIRY, &hdev->flags))
1885 return;
1886
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001887 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
Andre Guedes3e13fa12013-03-27 20:04:56 -03001888 wake_up_bit(&hdev->flags, HCI_INQUIRY);
1889
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001890 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001891 return;
1892
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001893 hci_dev_lock(hdev);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001894
Andre Guedes343f9352012-02-17 20:39:37 -03001895 if (discov->state != DISCOVERY_FINDING)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001896 goto unlock;
1897
1898 if (list_empty(&discov->resolve)) {
1899 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1900 goto unlock;
1901 }
1902
1903 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
1904 if (e && hci_resolve_name(hdev, e) == 0) {
1905 e->name_state = NAME_PENDING;
1906 hci_discovery_set_state(hdev, DISCOVERY_RESOLVING);
1907 } else {
1908 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1909 }
1910
1911unlock:
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001912 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001913}
1914
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001915static void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001917 struct inquiry_data data;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001918 struct inquiry_info *info = (void *) (skb->data + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 int num_rsp = *((__u8 *) skb->data);
1920
1921 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
1922
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001923 if (!num_rsp)
1924 return;
1925
Andre Guedes1519cc12012-03-21 00:03:38 -03001926 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags))
1927 return;
1928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001930
Johan Hedberge17acd42011-03-30 23:57:16 +03001931 for (; num_rsp; num_rsp--, info++) {
Johan Hedberg388fc8f2012-02-23 00:38:59 +02001932 bool name_known, ssp;
Johan Hedberg31754052012-01-04 13:39:52 +02001933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 bacpy(&data.bdaddr, &info->bdaddr);
1935 data.pscan_rep_mode = info->pscan_rep_mode;
1936 data.pscan_period_mode = info->pscan_period_mode;
1937 data.pscan_mode = info->pscan_mode;
1938 memcpy(data.dev_class, info->dev_class, 3);
1939 data.clock_offset = info->clock_offset;
1940 data.rssi = 0x00;
Marcel Holtmann41a96212008-07-14 20:13:48 +02001941 data.ssp_mode = 0x00;
Johan Hedberg31754052012-01-04 13:39:52 +02001942
Johan Hedberg388fc8f2012-02-23 00:38:59 +02001943 name_known = hci_inquiry_cache_update(hdev, &data, false, &ssp);
Johan Hedberg48264f02011-11-09 13:58:58 +02001944 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001945 info->dev_class, 0, !name_known, ssp, NULL,
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02001946 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 hci_dev_unlock(hdev);
1950}
1951
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001952static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001954 struct hci_ev_conn_complete *ev = (void *) skb->data;
1955 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001957 BT_DBG("%s", hdev->name);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001960
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001961 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann94992372009-04-19 19:30:03 +02001962 if (!conn) {
1963 if (ev->link_type != SCO_LINK)
1964 goto unlock;
1965
1966 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
1967 if (!conn)
1968 goto unlock;
1969
1970 conn->type = SCO_LINK;
1971 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001972
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001973 if (!ev->status) {
1974 conn->handle = __le16_to_cpu(ev->handle);
Marcel Holtmann769be972008-07-14 20:13:49 +02001975
1976 if (conn->type == ACL_LINK) {
1977 conn->state = BT_CONFIG;
1978 hci_conn_hold(conn);
Szymon Janca9ea3ed2012-07-19 14:46:08 +02001979
1980 if (!conn->out && !hci_conn_ssp_enabled(conn) &&
1981 !hci_find_link_key(hdev, &ev->bdaddr))
1982 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
1983 else
1984 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Marcel Holtmann769be972008-07-14 20:13:49 +02001985 } else
1986 conn->state = BT_CONNECTED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001987
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02001988 hci_conn_add_sysfs(conn);
1989
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001990 if (test_bit(HCI_AUTH, &hdev->flags))
1991 conn->link_mode |= HCI_LM_AUTH;
1992
1993 if (test_bit(HCI_ENCRYPT, &hdev->flags))
1994 conn->link_mode |= HCI_LM_ENCRYPT;
1995
1996 /* Get remote features */
1997 if (conn->type == ACL_LINK) {
1998 struct hci_cp_read_remote_features cp;
1999 cp.handle = ev->handle;
Marcel Holtmann769be972008-07-14 20:13:49 +02002000 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002001 sizeof(cp), &cp);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002002 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002003
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002004 /* Set packet type for incoming connection */
Andrei Emeltchenkod095c1e2011-12-01 14:33:27 +02002005 if (!conn->out && hdev->hci_ver < BLUETOOTH_VER_2_0) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002006 struct hci_cp_change_conn_ptype cp;
2007 cp.handle = ev->handle;
Marcel Holtmanna8746412008-07-14 20:13:46 +02002008 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002009 hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE, sizeof(cp),
2010 &cp);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002011 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02002012 } else {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002013 conn->state = BT_CLOSED;
Johan Hedberg17d5c042011-01-22 06:09:08 +02002014 if (conn->type == ACL_LINK)
Marcel Holtmann64c7b772014-02-18 14:22:20 -08002015 mgmt_connect_failed(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002016 conn->dst_type, ev->status);
Johan Hedberg17d5c042011-01-22 06:09:08 +02002017 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002018
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002019 if (conn->type == ACL_LINK)
2020 hci_sco_setup(conn, ev->status);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002021
Marcel Holtmann769be972008-07-14 20:13:49 +02002022 if (ev->status) {
2023 hci_proto_connect_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002024 hci_conn_del(conn);
Marcel Holtmannc89b6e62009-01-15 21:57:03 +01002025 } else if (ev->link_type != ACL_LINK)
2026 hci_proto_connect_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002027
2028unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002030
2031 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002034static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002036 struct hci_ev_conn_request *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 int mask = hdev->link_mode;
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01002038 __u8 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03002040 BT_DBG("%s bdaddr %pMR type 0x%x", hdev->name, &ev->bdaddr,
Gustavo Padovan807deac2012-05-17 00:36:24 -03002041 ev->link_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01002043 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type,
2044 &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Szymon Janc138d22e2011-02-17 16:44:23 +01002046 if ((mask & HCI_LM_ACCEPT) &&
Marcel Holtmannb9ee0a72013-10-17 17:24:13 -07002047 !hci_blacklist_lookup(hdev, &ev->bdaddr, BDADDR_BREDR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 /* Connection accepted */
Marcel Holtmannc7bdd502008-07-14 20:13:47 +02002049 struct inquiry_entry *ie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
2052 hci_dev_lock(hdev);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002053
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02002054 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
2055 if (ie)
Marcel Holtmannc7bdd502008-07-14 20:13:47 +02002056 memcpy(ie->data.dev_class, ev->dev_class, 3);
2057
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03002058 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type,
2059 &ev->bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 if (!conn) {
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02002061 conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr);
2062 if (!conn) {
Gustavo F. Padovan893ef972010-07-18 15:13:37 -03002063 BT_ERR("No memory for new connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 hci_dev_unlock(hdev);
2065 return;
2066 }
2067 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 memcpy(conn->dev_class, ev->dev_class, 3);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 hci_dev_unlock(hdev);
2072
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01002073 if (ev->link_type == ACL_LINK ||
2074 (!(flags & HCI_PROTO_DEFER) && !lmp_esco_capable(hdev))) {
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002075 struct hci_cp_accept_conn_req cp;
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01002076 conn->state = BT_CONNECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002078 bacpy(&cp.bdaddr, &ev->bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002080 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
2081 cp.role = 0x00; /* Become master */
2082 else
2083 cp.role = 0x01; /* Remain slave */
2084
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002085 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp),
2086 &cp);
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01002087 } else if (!(flags & HCI_PROTO_DEFER)) {
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002088 struct hci_cp_accept_sync_conn_req cp;
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01002089 conn->state = BT_CONNECT;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002090
2091 bacpy(&cp.bdaddr, &ev->bdaddr);
Marcel Holtmanna8746412008-07-14 20:13:46 +02002092 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002093
Joe Perchesdcf4adb2014-03-12 10:52:35 -07002094 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
2095 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
2096 cp.max_latency = cpu_to_le16(0xffff);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002097 cp.content_format = cpu_to_le16(hdev->voice_setting);
2098 cp.retrans_effort = 0xff;
2099
2100 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002101 sizeof(cp), &cp);
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01002102 } else {
2103 conn->state = BT_CONNECT2;
2104 hci_proto_connect_cfm(conn, 0);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 } else {
2107 /* Connection rejected */
2108 struct hci_cp_reject_conn_req cp;
2109
2110 bacpy(&cp.bdaddr, &ev->bdaddr);
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02002111 cp.reason = HCI_ERROR_REJ_BAD_ADDR;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002112 hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
2114}
2115
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02002116static u8 hci_to_mgmt_reason(u8 err)
2117{
2118 switch (err) {
2119 case HCI_ERROR_CONNECTION_TIMEOUT:
2120 return MGMT_DEV_DISCONN_TIMEOUT;
2121 case HCI_ERROR_REMOTE_USER_TERM:
2122 case HCI_ERROR_REMOTE_LOW_RESOURCES:
2123 case HCI_ERROR_REMOTE_POWER_OFF:
2124 return MGMT_DEV_DISCONN_REMOTE;
2125 case HCI_ERROR_LOCAL_HOST_TERM:
2126 return MGMT_DEV_DISCONN_LOCAL_HOST;
2127 default:
2128 return MGMT_DEV_DISCONN_UNKNOWN;
2129 }
2130}
2131
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002132static void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002134 struct hci_ev_disconn_complete *ev = (void *) skb->data;
Andre Guedesabf54a52013-11-07 17:36:09 -03002135 u8 reason = hci_to_mgmt_reason(ev->reason);
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002136 struct hci_conn_params *params;
Marcel Holtmann04837f62006-07-03 10:02:33 +02002137 struct hci_conn *conn;
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02002138 bool mgmt_connected;
Andre Guedes38462202013-11-07 17:36:10 -03002139 u8 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002141 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 hci_dev_lock(hdev);
2144
Marcel Holtmann04837f62006-07-03 10:02:33 +02002145 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergf7520542011-01-20 12:34:39 +02002146 if (!conn)
2147 goto unlock;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002148
Andre Guedesabf54a52013-11-07 17:36:09 -03002149 if (ev->status) {
2150 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
2151 conn->dst_type, ev->status);
2152 goto unlock;
Johan Hedberg37d9ef72011-11-10 15:54:39 +02002153 }
Johan Hedbergf7520542011-01-20 12:34:39 +02002154
Andre Guedes38462202013-11-07 17:36:10 -03002155 conn->state = BT_CLOSED;
2156
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02002157 mgmt_connected = test_and_clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags);
2158 mgmt_device_disconnected(hdev, &conn->dst, conn->type, conn->dst_type,
2159 reason, mgmt_connected);
Andre Guedesabf54a52013-11-07 17:36:09 -03002160
Andre Guedes38462202013-11-07 17:36:10 -03002161 if (conn->type == ACL_LINK && conn->flush_key)
2162 hci_remove_link_key(hdev, &conn->dst);
Johan Hedberg22102462013-10-05 12:01:06 +02002163
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002164 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
2165 if (params) {
2166 switch (params->auto_connect) {
2167 case HCI_AUTO_CONN_LINK_LOSS:
2168 if (ev->reason != HCI_ERROR_CONNECTION_TIMEOUT)
2169 break;
2170 /* Fall through */
2171
2172 case HCI_AUTO_CONN_ALWAYS:
2173 hci_pend_le_conn_add(hdev, &conn->dst, conn->dst_type);
2174 break;
2175
2176 default:
2177 break;
2178 }
2179 }
2180
Andre Guedes38462202013-11-07 17:36:10 -03002181 type = conn->type;
Johan Hedberg22102462013-10-05 12:01:06 +02002182
Andre Guedes38462202013-11-07 17:36:10 -03002183 hci_proto_disconn_cfm(conn, ev->reason);
2184 hci_conn_del(conn);
2185
2186 /* Re-enable advertising if necessary, since it might
2187 * have been disabled by the connection. From the
2188 * HCI_LE_Set_Advertise_Enable command description in
2189 * the core specification (v4.0):
2190 * "The Controller shall continue advertising until the Host
2191 * issues an LE_Set_Advertise_Enable command with
2192 * Advertising_Enable set to 0x00 (Advertising is disabled)
2193 * or until a connection is created or until the Advertising
2194 * is timed out due to Directed Advertising."
2195 */
2196 if (type == LE_LINK)
2197 mgmt_reenable_advertising(hdev);
Johan Hedbergf7520542011-01-20 12:34:39 +02002198
2199unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 hci_dev_unlock(hdev);
2201}
2202
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002203static void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002204{
2205 struct hci_ev_auth_complete *ev = (void *) skb->data;
2206 struct hci_conn *conn;
2207
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002208 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002209
2210 hci_dev_lock(hdev);
2211
2212 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002213 if (!conn)
2214 goto unlock;
2215
2216 if (!ev->status) {
Johan Hedbergaa64a8b2012-01-18 21:33:12 +02002217 if (!hci_conn_ssp_enabled(conn) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03002218 test_bit(HCI_CONN_REAUTH_PEND, &conn->flags)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002219 BT_INFO("re-auth of legacy device is not possible.");
Johan Hedberg2a611692011-02-19 12:06:00 -03002220 } else {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002221 conn->link_mode |= HCI_LM_AUTH;
2222 conn->sec_level = conn->pending_sec_level;
Johan Hedberg2a611692011-02-19 12:06:00 -03002223 }
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002224 } else {
Johan Hedbergbab73cb2012-02-09 16:07:29 +02002225 mgmt_auth_failed(hdev, &conn->dst, conn->type, conn->dst_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002226 ev->status);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002227 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002228
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002229 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
2230 clear_bit(HCI_CONN_REAUTH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002231
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002232 if (conn->state == BT_CONFIG) {
Johan Hedbergaa64a8b2012-01-18 21:33:12 +02002233 if (!ev->status && hci_conn_ssp_enabled(conn)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002234 struct hci_cp_set_conn_encrypt cp;
2235 cp.handle = ev->handle;
2236 cp.encrypt = 0x01;
2237 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
Gustavo Padovan807deac2012-05-17 00:36:24 -03002238 &cp);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002239 } else {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002240 conn->state = BT_CONNECTED;
2241 hci_proto_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02002242 hci_conn_drop(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002243 }
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002244 } else {
2245 hci_auth_cfm(conn, ev->status);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002246
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002247 hci_conn_hold(conn);
2248 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02002249 hci_conn_drop(conn);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002250 }
2251
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002252 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002253 if (!ev->status) {
2254 struct hci_cp_set_conn_encrypt cp;
2255 cp.handle = ev->handle;
2256 cp.encrypt = 0x01;
2257 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
Gustavo Padovan807deac2012-05-17 00:36:24 -03002258 &cp);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002259 } else {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002260 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002261 hci_encrypt_cfm(conn, ev->status, 0x00);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002262 }
2263 }
2264
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002265unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002266 hci_dev_unlock(hdev);
2267}
2268
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002269static void hci_remote_name_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002270{
Johan Hedberg127178d2010-11-18 22:22:29 +02002271 struct hci_ev_remote_name *ev = (void *) skb->data;
2272 struct hci_conn *conn;
2273
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002274 BT_DBG("%s", hdev->name);
2275
2276 hci_conn_check_pending(hdev);
Johan Hedberg127178d2010-11-18 22:22:29 +02002277
2278 hci_dev_lock(hdev);
2279
2280 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedbergb644ba32012-01-17 21:48:47 +02002281
2282 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
2283 goto check_auth;
2284
2285 if (ev->status == 0)
2286 hci_check_pending_name(hdev, conn, &ev->bdaddr, ev->name,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002287 strnlen(ev->name, HCI_MAX_NAME_LENGTH));
Johan Hedbergb644ba32012-01-17 21:48:47 +02002288 else
2289 hci_check_pending_name(hdev, conn, &ev->bdaddr, NULL, 0);
2290
2291check_auth:
Johan Hedberg79c6c702011-04-28 11:28:55 -07002292 if (!conn)
2293 goto unlock;
2294
2295 if (!hci_outgoing_auth_needed(hdev, conn))
2296 goto unlock;
2297
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002298 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02002299 struct hci_cp_auth_requested cp;
2300 cp.handle = __cpu_to_le16(conn->handle);
2301 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
2302 }
2303
Johan Hedberg79c6c702011-04-28 11:28:55 -07002304unlock:
Johan Hedberg127178d2010-11-18 22:22:29 +02002305 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002306}
2307
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002308static void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002309{
2310 struct hci_ev_encrypt_change *ev = (void *) skb->data;
2311 struct hci_conn *conn;
2312
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002313 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002314
2315 hci_dev_lock(hdev);
2316
2317 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08002318 if (!conn)
2319 goto unlock;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002320
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08002321 if (!ev->status) {
2322 if (ev->encrypt) {
2323 /* Encryption implies authentication */
2324 conn->link_mode |= HCI_LM_AUTH;
2325 conn->link_mode |= HCI_LM_ENCRYPT;
2326 conn->sec_level = conn->pending_sec_level;
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08002327
Marcel Holtmann914a6ff2014-02-01 11:52:02 -08002328 /* P-256 authentication key implies FIPS */
2329 if (conn->key_type == HCI_LK_AUTH_COMBINATION_P256)
2330 conn->link_mode |= HCI_LM_FIPS;
2331
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08002332 if ((conn->type == ACL_LINK && ev->encrypt == 0x02) ||
2333 conn->type == LE_LINK)
2334 set_bit(HCI_CONN_AES_CCM, &conn->flags);
2335 } else {
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08002336 conn->link_mode &= ~HCI_LM_ENCRYPT;
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08002337 clear_bit(HCI_CONN_AES_CCM, &conn->flags);
2338 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002339 }
2340
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08002341 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
2342
2343 if (ev->status && conn->state == BT_CONNECTED) {
2344 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
2345 hci_conn_drop(conn);
2346 goto unlock;
2347 }
2348
2349 if (conn->state == BT_CONFIG) {
2350 if (!ev->status)
2351 conn->state = BT_CONNECTED;
2352
Marcel Holtmann40b552a2014-03-19 14:10:25 -07002353 /* In Secure Connections Only mode, do not allow any
2354 * connections that are not encrypted with AES-CCM
2355 * using a P-256 authenticated combination key.
2356 */
2357 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) &&
2358 (!test_bit(HCI_CONN_AES_CCM, &conn->flags) ||
2359 conn->key_type != HCI_LK_AUTH_COMBINATION_P256)) {
2360 hci_proto_connect_cfm(conn, HCI_ERROR_AUTH_FAILURE);
2361 hci_conn_drop(conn);
2362 goto unlock;
2363 }
2364
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08002365 hci_proto_connect_cfm(conn, ev->status);
2366 hci_conn_drop(conn);
2367 } else
2368 hci_encrypt_cfm(conn, ev->status, ev->encrypt);
2369
Gustavo Padovana7d77232012-05-13 03:20:07 -03002370unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002371 hci_dev_unlock(hdev);
2372}
2373
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002374static void hci_change_link_key_complete_evt(struct hci_dev *hdev,
2375 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002376{
2377 struct hci_ev_change_link_key_complete *ev = (void *) skb->data;
2378 struct hci_conn *conn;
2379
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002380 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002381
2382 hci_dev_lock(hdev);
2383
2384 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
2385 if (conn) {
2386 if (!ev->status)
2387 conn->link_mode |= HCI_LM_SECURE;
2388
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002389 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002390
2391 hci_key_change_cfm(conn, ev->status);
2392 }
2393
2394 hci_dev_unlock(hdev);
2395}
2396
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002397static void hci_remote_features_evt(struct hci_dev *hdev,
2398 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002399{
2400 struct hci_ev_remote_features *ev = (void *) skb->data;
2401 struct hci_conn *conn;
2402
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002403 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002404
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002405 hci_dev_lock(hdev);
2406
2407 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02002408 if (!conn)
2409 goto unlock;
Marcel Holtmann769be972008-07-14 20:13:49 +02002410
Johan Hedbergccd556f2010-11-10 17:11:51 +02002411 if (!ev->status)
Johan Hedbergcad718e2013-04-17 15:00:51 +03002412 memcpy(conn->features[0], ev->features, 8);
Johan Hedbergccd556f2010-11-10 17:11:51 +02002413
2414 if (conn->state != BT_CONFIG)
2415 goto unlock;
2416
2417 if (!ev->status && lmp_ssp_capable(hdev) && lmp_ssp_capable(conn)) {
2418 struct hci_cp_read_remote_ext_features cp;
2419 cp.handle = ev->handle;
2420 cp.page = 0x01;
2421 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES,
Gustavo Padovan807deac2012-05-17 00:36:24 -03002422 sizeof(cp), &cp);
Johan Hedberg392599b2010-11-18 22:22:28 +02002423 goto unlock;
2424 }
2425
Johan Hedberg671267b2012-05-12 16:11:50 -03002426 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02002427 struct hci_cp_remote_name_req cp;
2428 memset(&cp, 0, sizeof(cp));
2429 bacpy(&cp.bdaddr, &conn->dst);
2430 cp.pscan_rep_mode = 0x02;
2431 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
Johan Hedbergb644ba32012-01-17 21:48:47 +02002432 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
2433 mgmt_device_connected(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002434 conn->dst_type, 0, NULL, 0,
2435 conn->dev_class);
Johan Hedberg392599b2010-11-18 22:22:28 +02002436
Johan Hedberg127178d2010-11-18 22:22:29 +02002437 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02002438 conn->state = BT_CONNECTED;
2439 hci_proto_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02002440 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02002441 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002442
Johan Hedbergccd556f2010-11-10 17:11:51 +02002443unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002444 hci_dev_unlock(hdev);
2445}
2446
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002447static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002448{
2449 struct hci_ev_cmd_complete *ev = (void *) skb->data;
Johan Hedberg9238f362013-03-05 20:37:48 +02002450 u8 status = skb->data[sizeof(*ev)];
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002451 __u16 opcode;
2452
2453 skb_pull(skb, sizeof(*ev));
2454
2455 opcode = __le16_to_cpu(ev->opcode);
2456
2457 switch (opcode) {
2458 case HCI_OP_INQUIRY_CANCEL:
2459 hci_cc_inquiry_cancel(hdev, skb);
2460 break;
2461
Andre Guedes4d934832012-03-21 00:03:35 -03002462 case HCI_OP_PERIODIC_INQ:
2463 hci_cc_periodic_inq(hdev, skb);
2464 break;
2465
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002466 case HCI_OP_EXIT_PERIODIC_INQ:
2467 hci_cc_exit_periodic_inq(hdev, skb);
2468 break;
2469
2470 case HCI_OP_REMOTE_NAME_REQ_CANCEL:
2471 hci_cc_remote_name_req_cancel(hdev, skb);
2472 break;
2473
2474 case HCI_OP_ROLE_DISCOVERY:
2475 hci_cc_role_discovery(hdev, skb);
2476 break;
2477
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02002478 case HCI_OP_READ_LINK_POLICY:
2479 hci_cc_read_link_policy(hdev, skb);
2480 break;
2481
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002482 case HCI_OP_WRITE_LINK_POLICY:
2483 hci_cc_write_link_policy(hdev, skb);
2484 break;
2485
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02002486 case HCI_OP_READ_DEF_LINK_POLICY:
2487 hci_cc_read_def_link_policy(hdev, skb);
2488 break;
2489
2490 case HCI_OP_WRITE_DEF_LINK_POLICY:
2491 hci_cc_write_def_link_policy(hdev, skb);
2492 break;
2493
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002494 case HCI_OP_RESET:
2495 hci_cc_reset(hdev, skb);
2496 break;
2497
2498 case HCI_OP_WRITE_LOCAL_NAME:
2499 hci_cc_write_local_name(hdev, skb);
2500 break;
2501
2502 case HCI_OP_READ_LOCAL_NAME:
2503 hci_cc_read_local_name(hdev, skb);
2504 break;
2505
2506 case HCI_OP_WRITE_AUTH_ENABLE:
2507 hci_cc_write_auth_enable(hdev, skb);
2508 break;
2509
2510 case HCI_OP_WRITE_ENCRYPT_MODE:
2511 hci_cc_write_encrypt_mode(hdev, skb);
2512 break;
2513
2514 case HCI_OP_WRITE_SCAN_ENABLE:
2515 hci_cc_write_scan_enable(hdev, skb);
2516 break;
2517
2518 case HCI_OP_READ_CLASS_OF_DEV:
2519 hci_cc_read_class_of_dev(hdev, skb);
2520 break;
2521
2522 case HCI_OP_WRITE_CLASS_OF_DEV:
2523 hci_cc_write_class_of_dev(hdev, skb);
2524 break;
2525
2526 case HCI_OP_READ_VOICE_SETTING:
2527 hci_cc_read_voice_setting(hdev, skb);
2528 break;
2529
2530 case HCI_OP_WRITE_VOICE_SETTING:
2531 hci_cc_write_voice_setting(hdev, skb);
2532 break;
2533
Marcel Holtmannb4cb9fb2013-10-14 13:56:16 -07002534 case HCI_OP_READ_NUM_SUPPORTED_IAC:
2535 hci_cc_read_num_supported_iac(hdev, skb);
2536 break;
2537
Marcel Holtmann333140b2008-07-14 20:13:48 +02002538 case HCI_OP_WRITE_SSP_MODE:
2539 hci_cc_write_ssp_mode(hdev, skb);
2540 break;
2541
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08002542 case HCI_OP_WRITE_SC_SUPPORT:
2543 hci_cc_write_sc_support(hdev, skb);
2544 break;
2545
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002546 case HCI_OP_READ_LOCAL_VERSION:
2547 hci_cc_read_local_version(hdev, skb);
2548 break;
2549
2550 case HCI_OP_READ_LOCAL_COMMANDS:
2551 hci_cc_read_local_commands(hdev, skb);
2552 break;
2553
2554 case HCI_OP_READ_LOCAL_FEATURES:
2555 hci_cc_read_local_features(hdev, skb);
2556 break;
2557
Andre Guedes971e3a42011-06-30 19:20:52 -03002558 case HCI_OP_READ_LOCAL_EXT_FEATURES:
2559 hci_cc_read_local_ext_features(hdev, skb);
2560 break;
2561
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002562 case HCI_OP_READ_BUFFER_SIZE:
2563 hci_cc_read_buffer_size(hdev, skb);
2564 break;
2565
2566 case HCI_OP_READ_BD_ADDR:
2567 hci_cc_read_bd_addr(hdev, skb);
2568 break;
2569
Johan Hedbergf332ec62013-03-15 17:07:11 -05002570 case HCI_OP_READ_PAGE_SCAN_ACTIVITY:
2571 hci_cc_read_page_scan_activity(hdev, skb);
2572 break;
2573
Johan Hedberg4a3ee762013-03-15 17:07:12 -05002574 case HCI_OP_WRITE_PAGE_SCAN_ACTIVITY:
2575 hci_cc_write_page_scan_activity(hdev, skb);
2576 break;
2577
Johan Hedbergf332ec62013-03-15 17:07:11 -05002578 case HCI_OP_READ_PAGE_SCAN_TYPE:
2579 hci_cc_read_page_scan_type(hdev, skb);
2580 break;
2581
Johan Hedberg4a3ee762013-03-15 17:07:12 -05002582 case HCI_OP_WRITE_PAGE_SCAN_TYPE:
2583 hci_cc_write_page_scan_type(hdev, skb);
2584 break;
2585
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +02002586 case HCI_OP_READ_DATA_BLOCK_SIZE:
2587 hci_cc_read_data_block_size(hdev, skb);
2588 break;
2589
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +02002590 case HCI_OP_READ_FLOW_CONTROL_MODE:
2591 hci_cc_read_flow_control_mode(hdev, skb);
2592 break;
2593
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +03002594 case HCI_OP_READ_LOCAL_AMP_INFO:
2595 hci_cc_read_local_amp_info(hdev, skb);
2596 break;
2597
Andrei Emeltchenko903e4542012-09-27 17:26:09 +03002598 case HCI_OP_READ_LOCAL_AMP_ASSOC:
2599 hci_cc_read_local_amp_assoc(hdev, skb);
2600 break;
2601
Johan Hedbergd5859e22011-01-25 01:19:58 +02002602 case HCI_OP_READ_INQ_RSP_TX_POWER:
2603 hci_cc_read_inq_rsp_tx_power(hdev, skb);
2604 break;
2605
Johan Hedberg980e1a52011-01-22 06:10:07 +02002606 case HCI_OP_PIN_CODE_REPLY:
2607 hci_cc_pin_code_reply(hdev, skb);
2608 break;
2609
2610 case HCI_OP_PIN_CODE_NEG_REPLY:
2611 hci_cc_pin_code_neg_reply(hdev, skb);
2612 break;
2613
Szymon Jancc35938b2011-03-22 13:12:21 +01002614 case HCI_OP_READ_LOCAL_OOB_DATA:
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08002615 hci_cc_read_local_oob_data(hdev, skb);
2616 break;
2617
2618 case HCI_OP_READ_LOCAL_OOB_EXT_DATA:
2619 hci_cc_read_local_oob_ext_data(hdev, skb);
Szymon Jancc35938b2011-03-22 13:12:21 +01002620 break;
2621
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002622 case HCI_OP_LE_READ_BUFFER_SIZE:
2623 hci_cc_le_read_buffer_size(hdev, skb);
2624 break;
2625
Johan Hedberg60e77322013-01-22 14:01:59 +02002626 case HCI_OP_LE_READ_LOCAL_FEATURES:
2627 hci_cc_le_read_local_features(hdev, skb);
2628 break;
2629
Johan Hedberg8fa19092012-10-19 20:57:49 +03002630 case HCI_OP_LE_READ_ADV_TX_POWER:
2631 hci_cc_le_read_adv_tx_power(hdev, skb);
2632 break;
2633
Johan Hedberga5c29682011-02-19 12:05:57 -03002634 case HCI_OP_USER_CONFIRM_REPLY:
2635 hci_cc_user_confirm_reply(hdev, skb);
2636 break;
2637
2638 case HCI_OP_USER_CONFIRM_NEG_REPLY:
2639 hci_cc_user_confirm_neg_reply(hdev, skb);
2640 break;
2641
Brian Gix1143d452011-11-23 08:28:34 -08002642 case HCI_OP_USER_PASSKEY_REPLY:
2643 hci_cc_user_passkey_reply(hdev, skb);
2644 break;
2645
2646 case HCI_OP_USER_PASSKEY_NEG_REPLY:
2647 hci_cc_user_passkey_neg_reply(hdev, skb);
Szymon Janc16cde992012-04-13 12:32:42 +02002648 break;
Andre Guedes07f7fa52011-12-02 21:13:31 +09002649
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08002650 case HCI_OP_LE_SET_RANDOM_ADDR:
2651 hci_cc_le_set_random_addr(hdev, skb);
2652 break;
2653
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01002654 case HCI_OP_LE_SET_ADV_ENABLE:
2655 hci_cc_le_set_adv_enable(hdev, skb);
2656 break;
2657
Marcel Holtmann533553f2014-03-21 12:18:10 -07002658 case HCI_OP_LE_SET_SCAN_PARAM:
2659 hci_cc_le_set_scan_param(hdev, skb);
2660 break;
2661
Andre Guedeseb9d91f2011-05-26 16:23:52 -03002662 case HCI_OP_LE_SET_SCAN_ENABLE:
2663 hci_cc_le_set_scan_enable(hdev, skb);
2664 break;
2665
Johan Hedbergcf1d0812013-01-22 14:02:00 +02002666 case HCI_OP_LE_READ_WHITE_LIST_SIZE:
2667 hci_cc_le_read_white_list_size(hdev, skb);
2668 break;
2669
Marcel Holtmann0f36b582014-02-27 20:37:31 -08002670 case HCI_OP_LE_CLEAR_WHITE_LIST:
2671 hci_cc_le_clear_white_list(hdev, skb);
2672 break;
2673
2674 case HCI_OP_LE_ADD_TO_WHITE_LIST:
2675 hci_cc_le_add_to_white_list(hdev, skb);
2676 break;
2677
2678 case HCI_OP_LE_DEL_FROM_WHITE_LIST:
2679 hci_cc_le_del_from_white_list(hdev, skb);
2680 break;
2681
Johan Hedberg9b008c02013-01-22 14:02:01 +02002682 case HCI_OP_LE_READ_SUPPORTED_STATES:
2683 hci_cc_le_read_supported_states(hdev, skb);
2684 break;
2685
Andre Guedesf9b49302011-06-30 19:20:53 -03002686 case HCI_OP_WRITE_LE_HOST_SUPPORTED:
2687 hci_cc_write_le_host_supported(hdev, skb);
2688 break;
2689
Johan Hedberg56ed2cb2014-02-27 14:05:40 +02002690 case HCI_OP_LE_SET_ADV_PARAM:
2691 hci_cc_set_adv_param(hdev, skb);
2692 break;
2693
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +03002694 case HCI_OP_WRITE_REMOTE_AMP_ASSOC:
2695 hci_cc_write_remote_amp_assoc(hdev, skb);
2696 break;
2697
Andrzej Kaczmarek5ae76a92014-05-08 15:32:08 +02002698 case HCI_OP_READ_RSSI:
2699 hci_cc_read_rssi(hdev, skb);
2700 break;
2701
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02002702 case HCI_OP_READ_TX_POWER:
2703 hci_cc_read_tx_power(hdev, skb);
2704 break;
2705
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002706 default:
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002707 BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002708 break;
2709 }
2710
Johan Hedbergad82cdd2013-03-09 09:53:50 +02002711 if (opcode != HCI_OP_NOP)
Ville Tervo6bd32322011-02-16 16:32:41 +02002712 del_timer(&hdev->cmd_timer);
2713
Johan Hedbergad82cdd2013-03-09 09:53:50 +02002714 hci_req_cmd_complete(hdev, opcode, status);
Johan Hedberg9238f362013-03-05 20:37:48 +02002715
Szymon Jancdbccd792012-12-11 08:51:19 +01002716 if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags)) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002717 atomic_set(&hdev->cmd_cnt, 1);
2718 if (!skb_queue_empty(&hdev->cmd_q))
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02002719 queue_work(hdev->workqueue, &hdev->cmd_work);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002720 }
2721}
2722
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002723static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002724{
2725 struct hci_ev_cmd_status *ev = (void *) skb->data;
2726 __u16 opcode;
2727
2728 skb_pull(skb, sizeof(*ev));
2729
2730 opcode = __le16_to_cpu(ev->opcode);
2731
2732 switch (opcode) {
2733 case HCI_OP_INQUIRY:
2734 hci_cs_inquiry(hdev, ev->status);
2735 break;
2736
2737 case HCI_OP_CREATE_CONN:
2738 hci_cs_create_conn(hdev, ev->status);
2739 break;
2740
2741 case HCI_OP_ADD_SCO:
2742 hci_cs_add_sco(hdev, ev->status);
2743 break;
2744
Marcel Holtmannf8558552008-07-14 20:13:49 +02002745 case HCI_OP_AUTH_REQUESTED:
2746 hci_cs_auth_requested(hdev, ev->status);
2747 break;
2748
2749 case HCI_OP_SET_CONN_ENCRYPT:
2750 hci_cs_set_conn_encrypt(hdev, ev->status);
2751 break;
2752
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002753 case HCI_OP_REMOTE_NAME_REQ:
2754 hci_cs_remote_name_req(hdev, ev->status);
2755 break;
2756
Marcel Holtmann769be972008-07-14 20:13:49 +02002757 case HCI_OP_READ_REMOTE_FEATURES:
2758 hci_cs_read_remote_features(hdev, ev->status);
2759 break;
2760
2761 case HCI_OP_READ_REMOTE_EXT_FEATURES:
2762 hci_cs_read_remote_ext_features(hdev, ev->status);
2763 break;
2764
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002765 case HCI_OP_SETUP_SYNC_CONN:
2766 hci_cs_setup_sync_conn(hdev, ev->status);
2767 break;
2768
2769 case HCI_OP_SNIFF_MODE:
2770 hci_cs_sniff_mode(hdev, ev->status);
2771 break;
2772
2773 case HCI_OP_EXIT_SNIFF_MODE:
2774 hci_cs_exit_sniff_mode(hdev, ev->status);
2775 break;
2776
Johan Hedberg8962ee72011-01-20 12:40:27 +02002777 case HCI_OP_DISCONNECT:
Johan Hedberg88c3df12012-02-09 14:27:38 +02002778 hci_cs_disconnect(hdev, ev->status);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002779 break;
2780
Andrei Emeltchenkoa02226d2012-09-27 17:26:19 +03002781 case HCI_OP_CREATE_PHY_LINK:
2782 hci_cs_create_phylink(hdev, ev->status);
2783 break;
2784
Andrei Emeltchenko0b26ab92012-09-27 17:26:24 +03002785 case HCI_OP_ACCEPT_PHY_LINK:
2786 hci_cs_accept_phylink(hdev, ev->status);
2787 break;
2788
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02002789 case HCI_OP_LE_CREATE_CONN:
2790 hci_cs_le_create_conn(hdev, ev->status);
2791 break;
2792
Johan Hedberg81d0c8a2014-03-24 14:39:04 +02002793 case HCI_OP_LE_START_ENC:
2794 hci_cs_le_start_enc(hdev, ev->status);
2795 break;
2796
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002797 default:
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002798 BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002799 break;
2800 }
2801
Johan Hedbergad82cdd2013-03-09 09:53:50 +02002802 if (opcode != HCI_OP_NOP)
Ville Tervo6bd32322011-02-16 16:32:41 +02002803 del_timer(&hdev->cmd_timer);
2804
Johan Hedberg02350a72013-04-03 21:50:29 +03002805 if (ev->status ||
2806 (hdev->sent_cmd && !bt_cb(hdev->sent_cmd)->req.event))
2807 hci_req_cmd_complete(hdev, opcode, ev->status);
Johan Hedberg9238f362013-03-05 20:37:48 +02002808
Gustavo F. Padovan10572132011-03-16 15:36:29 -03002809 if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags)) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002810 atomic_set(&hdev->cmd_cnt, 1);
2811 if (!skb_queue_empty(&hdev->cmd_q))
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02002812 queue_work(hdev->workqueue, &hdev->cmd_work);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002813 }
2814}
2815
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002816static void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002817{
2818 struct hci_ev_role_change *ev = (void *) skb->data;
2819 struct hci_conn *conn;
2820
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002821 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002822
2823 hci_dev_lock(hdev);
2824
2825 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
2826 if (conn) {
2827 if (!ev->status) {
2828 if (ev->role)
2829 conn->link_mode &= ~HCI_LM_MASTER;
2830 else
2831 conn->link_mode |= HCI_LM_MASTER;
2832 }
2833
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002834 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002835
2836 hci_role_switch_cfm(conn, ev->status, ev->role);
2837 }
2838
2839 hci_dev_unlock(hdev);
2840}
2841
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002842static void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002844 struct hci_ev_num_comp_pkts *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 int i;
2846
Andrei Emeltchenko32ac5b92011-12-19 16:31:29 +02002847 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_PACKET_BASED) {
2848 BT_ERR("Wrong event for mode %d", hdev->flow_ctl_mode);
2849 return;
2850 }
2851
Andrei Emeltchenkoc5993de2011-12-30 12:07:47 +02002852 if (skb->len < sizeof(*ev) || skb->len < sizeof(*ev) +
Gustavo Padovan807deac2012-05-17 00:36:24 -03002853 ev->num_hndl * sizeof(struct hci_comp_pkts_info)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 BT_DBG("%s bad parameters", hdev->name);
2855 return;
2856 }
2857
Andrei Emeltchenkoc5993de2011-12-30 12:07:47 +02002858 BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
2859
Andrei Emeltchenko613a1c02011-12-19 16:31:30 +02002860 for (i = 0; i < ev->num_hndl; i++) {
2861 struct hci_comp_pkts_info *info = &ev->handles[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 struct hci_conn *conn;
2863 __u16 handle, count;
2864
Andrei Emeltchenko613a1c02011-12-19 16:31:30 +02002865 handle = __le16_to_cpu(info->handle);
2866 count = __le16_to_cpu(info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
2868 conn = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02002869 if (!conn)
2870 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02002872 conn->sent -= count;
2873
2874 switch (conn->type) {
2875 case ACL_LINK:
2876 hdev->acl_cnt += count;
2877 if (hdev->acl_cnt > hdev->acl_pkts)
2878 hdev->acl_cnt = hdev->acl_pkts;
2879 break;
2880
2881 case LE_LINK:
2882 if (hdev->le_pkts) {
2883 hdev->le_cnt += count;
2884 if (hdev->le_cnt > hdev->le_pkts)
2885 hdev->le_cnt = hdev->le_pkts;
2886 } else {
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02002887 hdev->acl_cnt += count;
2888 if (hdev->acl_cnt > hdev->acl_pkts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 hdev->acl_cnt = hdev->acl_pkts;
2890 }
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02002891 break;
2892
2893 case SCO_LINK:
2894 hdev->sco_cnt += count;
2895 if (hdev->sco_cnt > hdev->sco_pkts)
2896 hdev->sco_cnt = hdev->sco_pkts;
2897 break;
2898
2899 default:
2900 BT_ERR("Unknown type %d conn %p", conn->type, conn);
2901 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 }
2903 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002904
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02002905 queue_work(hdev->workqueue, &hdev->tx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906}
2907
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03002908static struct hci_conn *__hci_conn_lookup_handle(struct hci_dev *hdev,
2909 __u16 handle)
2910{
2911 struct hci_chan *chan;
2912
2913 switch (hdev->dev_type) {
2914 case HCI_BREDR:
2915 return hci_conn_hash_lookup_handle(hdev, handle);
2916 case HCI_AMP:
2917 chan = hci_chan_lookup_handle(hdev, handle);
2918 if (chan)
2919 return chan->conn;
2920 break;
2921 default:
2922 BT_ERR("%s unknown dev_type %d", hdev->name, hdev->dev_type);
2923 break;
2924 }
2925
2926 return NULL;
2927}
2928
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002929static void hci_num_comp_blocks_evt(struct hci_dev *hdev, struct sk_buff *skb)
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02002930{
2931 struct hci_ev_num_comp_blocks *ev = (void *) skb->data;
2932 int i;
2933
2934 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_BLOCK_BASED) {
2935 BT_ERR("Wrong event for mode %d", hdev->flow_ctl_mode);
2936 return;
2937 }
2938
2939 if (skb->len < sizeof(*ev) || skb->len < sizeof(*ev) +
Gustavo Padovan807deac2012-05-17 00:36:24 -03002940 ev->num_hndl * sizeof(struct hci_comp_blocks_info)) {
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02002941 BT_DBG("%s bad parameters", hdev->name);
2942 return;
2943 }
2944
2945 BT_DBG("%s num_blocks %d num_hndl %d", hdev->name, ev->num_blocks,
Gustavo Padovan807deac2012-05-17 00:36:24 -03002946 ev->num_hndl);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02002947
2948 for (i = 0; i < ev->num_hndl; i++) {
2949 struct hci_comp_blocks_info *info = &ev->handles[i];
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03002950 struct hci_conn *conn = NULL;
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02002951 __u16 handle, block_count;
2952
2953 handle = __le16_to_cpu(info->handle);
2954 block_count = __le16_to_cpu(info->blocks);
2955
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03002956 conn = __hci_conn_lookup_handle(hdev, handle);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02002957 if (!conn)
2958 continue;
2959
2960 conn->sent -= block_count;
2961
2962 switch (conn->type) {
2963 case ACL_LINK:
Andrei Emeltchenkobd1eb662012-10-10 17:38:30 +03002964 case AMP_LINK:
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02002965 hdev->block_cnt += block_count;
2966 if (hdev->block_cnt > hdev->num_blocks)
2967 hdev->block_cnt = hdev->num_blocks;
2968 break;
2969
2970 default:
2971 BT_ERR("Unknown type %d conn %p", conn->type, conn);
2972 break;
2973 }
2974 }
2975
2976 queue_work(hdev->workqueue, &hdev->tx_work);
2977}
2978
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002979static void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002981 struct hci_ev_mode_change *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02002982 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002984 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
2986 hci_dev_lock(hdev);
2987
Marcel Holtmann04837f62006-07-03 10:02:33 +02002988 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
2989 if (conn) {
2990 conn->mode = ev->mode;
Marcel Holtmann04837f62006-07-03 10:02:33 +02002991
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03002992 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND,
2993 &conn->flags)) {
Marcel Holtmann04837f62006-07-03 10:02:33 +02002994 if (conn->mode == HCI_CM_ACTIVE)
Johan Hedberg58a681e2012-01-16 06:47:28 +02002995 set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +02002996 else
Johan Hedberg58a681e2012-01-16 06:47:28 +02002997 clear_bit(HCI_CONN_POWER_SAVE, &conn->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +02002998 }
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002999
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003000 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04003001 hci_sco_setup(conn, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02003002 }
3003
3004 hci_dev_unlock(hdev);
3005}
3006
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003007static void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003009 struct hci_ev_pin_code_req *ev = (void *) skb->data;
3010 struct hci_conn *conn;
3011
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003012 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003013
3014 hci_dev_lock(hdev);
3015
3016 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Waldemar Rymarkiewiczb6f98042011-09-23 10:01:30 +02003017 if (!conn)
3018 goto unlock;
3019
3020 if (conn->state == BT_CONNECTED) {
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003021 hci_conn_hold(conn);
3022 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02003023 hci_conn_drop(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003024 }
3025
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003026 if (!test_bit(HCI_PAIRABLE, &hdev->dev_flags))
Johan Hedberg03b555e2011-01-04 15:40:05 +02003027 hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003028 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003029 else if (test_bit(HCI_MGMT, &hdev->dev_flags)) {
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02003030 u8 secure;
3031
3032 if (conn->pending_sec_level == BT_SECURITY_HIGH)
3033 secure = 1;
3034 else
3035 secure = 0;
3036
Johan Hedberg744cf192011-11-08 20:40:14 +02003037 mgmt_pin_code_request(hdev, &ev->bdaddr, secure);
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02003038 }
Johan Hedberg980e1a52011-01-22 06:10:07 +02003039
Waldemar Rymarkiewiczb6f98042011-09-23 10:01:30 +02003040unlock:
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003041 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042}
3043
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003044static void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045{
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003046 struct hci_ev_link_key_req *ev = (void *) skb->data;
3047 struct hci_cp_link_key_reply cp;
3048 struct hci_conn *conn;
3049 struct link_key *key;
3050
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003051 BT_DBG("%s", hdev->name);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003052
Andrei Emeltchenko034cbea2013-05-14 11:44:16 +03003053 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003054 return;
3055
3056 hci_dev_lock(hdev);
3057
3058 key = hci_find_link_key(hdev, &ev->bdaddr);
3059 if (!key) {
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03003060 BT_DBG("%s link key not found for %pMR", hdev->name,
3061 &ev->bdaddr);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003062 goto not_found;
3063 }
3064
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03003065 BT_DBG("%s found key type %u for %pMR", hdev->name, key->type,
3066 &ev->bdaddr);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003067
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003068 if (!test_bit(HCI_DEBUG_KEYS, &hdev->dev_flags) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03003069 key->type == HCI_LK_DEBUG_COMBINATION) {
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003070 BT_DBG("%s ignoring debug key", hdev->name);
3071 goto not_found;
3072 }
3073
3074 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02003075 if (conn) {
Marcel Holtmann66138ce2014-01-10 02:07:20 -08003076 if ((key->type == HCI_LK_UNAUTH_COMBINATION_P192 ||
3077 key->type == HCI_LK_UNAUTH_COMBINATION_P256) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03003078 conn->auth_type != 0xff && (conn->auth_type & 0x01)) {
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02003079 BT_DBG("%s ignoring unauthenticated key", hdev->name);
3080 goto not_found;
3081 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003082
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02003083 if (key->type == HCI_LK_COMBINATION && key->pin_len < 16 &&
Johan Hedbergf3fb0b52014-06-02 10:12:44 +03003084 (conn->pending_sec_level == BT_SECURITY_HIGH ||
3085 conn->pending_sec_level == BT_SECURITY_FIPS)) {
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03003086 BT_DBG("%s ignoring key unauthenticated for high security",
3087 hdev->name);
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02003088 goto not_found;
3089 }
3090
3091 conn->key_type = key->type;
3092 conn->pin_length = key->pin_len;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003093 }
3094
3095 bacpy(&cp.bdaddr, &ev->bdaddr);
Andrei Emeltchenko9b3b4462012-05-23 11:31:20 +03003096 memcpy(cp.link_key, key->val, HCI_LINK_KEY_SIZE);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003097
3098 hci_send_cmd(hdev, HCI_OP_LINK_KEY_REPLY, sizeof(cp), &cp);
3099
3100 hci_dev_unlock(hdev);
3101
3102 return;
3103
3104not_found:
3105 hci_send_cmd(hdev, HCI_OP_LINK_KEY_NEG_REPLY, 6, &ev->bdaddr);
3106 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107}
3108
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003109static void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003111 struct hci_ev_link_key_notify *ev = (void *) skb->data;
3112 struct hci_conn *conn;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003113 u8 pin_len = 0;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003114
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003115 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003116
3117 hci_dev_lock(hdev);
3118
3119 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3120 if (conn) {
3121 hci_conn_hold(conn);
3122 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Johan Hedberg980e1a52011-01-22 06:10:07 +02003123 pin_len = conn->pin_length;
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +02003124
3125 if (ev->key_type != HCI_LK_CHANGED_COMBINATION)
3126 conn->key_type = ev->key_type;
3127
David Herrmann76a68ba2013-04-06 20:28:37 +02003128 hci_conn_drop(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003129 }
3130
Andrei Emeltchenko034cbea2013-05-14 11:44:16 +03003131 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedbergd25e28a2011-04-28 11:28:59 -07003132 hci_add_link_key(hdev, conn, 1, &ev->bdaddr, ev->link_key,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003133 ev->key_type, pin_len);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003134
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003135 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136}
3137
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003138static void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann04837f62006-07-03 10:02:33 +02003139{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003140 struct hci_ev_clock_offset *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02003141 struct hci_conn *conn;
3142
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003143 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02003144
3145 hci_dev_lock(hdev);
3146
3147 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 if (conn && !ev->status) {
3149 struct inquiry_entry *ie;
3150
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02003151 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
3152 if (ie) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 ie->data.clock_offset = ev->clock_offset;
3154 ie->timestamp = jiffies;
3155 }
3156 }
3157
3158 hci_dev_unlock(hdev);
3159}
3160
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003161static void hci_pkt_type_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna8746412008-07-14 20:13:46 +02003162{
3163 struct hci_ev_pkt_type_change *ev = (void *) skb->data;
3164 struct hci_conn *conn;
3165
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003166 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna8746412008-07-14 20:13:46 +02003167
3168 hci_dev_lock(hdev);
3169
3170 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3171 if (conn && !ev->status)
3172 conn->pkt_type = __le16_to_cpu(ev->pkt_type);
3173
3174 hci_dev_unlock(hdev);
3175}
3176
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003177static void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann85a1e932005-08-09 20:28:02 -07003178{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003179 struct hci_ev_pscan_rep_mode *ev = (void *) skb->data;
Marcel Holtmann85a1e932005-08-09 20:28:02 -07003180 struct inquiry_entry *ie;
3181
3182 BT_DBG("%s", hdev->name);
3183
3184 hci_dev_lock(hdev);
3185
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02003186 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
3187 if (ie) {
Marcel Holtmann85a1e932005-08-09 20:28:02 -07003188 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
3189 ie->timestamp = jiffies;
3190 }
3191
3192 hci_dev_unlock(hdev);
3193}
3194
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003195static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev,
3196 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003197{
3198 struct inquiry_data data;
3199 int num_rsp = *((__u8 *) skb->data);
Johan Hedberg388fc8f2012-02-23 00:38:59 +02003200 bool name_known, ssp;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003201
3202 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
3203
3204 if (!num_rsp)
3205 return;
3206
Andre Guedes1519cc12012-03-21 00:03:38 -03003207 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags))
3208 return;
3209
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003210 hci_dev_lock(hdev);
3211
3212 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
Szymon Janc138d22e2011-02-17 16:44:23 +01003213 struct inquiry_info_with_rssi_and_pscan_mode *info;
3214 info = (void *) (skb->data + 1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003215
Johan Hedberge17acd42011-03-30 23:57:16 +03003216 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003217 bacpy(&data.bdaddr, &info->bdaddr);
3218 data.pscan_rep_mode = info->pscan_rep_mode;
3219 data.pscan_period_mode = info->pscan_period_mode;
3220 data.pscan_mode = info->pscan_mode;
3221 memcpy(data.dev_class, info->dev_class, 3);
3222 data.clock_offset = info->clock_offset;
3223 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003224 data.ssp_mode = 0x00;
Johan Hedberg31754052012-01-04 13:39:52 +02003225
3226 name_known = hci_inquiry_cache_update(hdev, &data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003227 false, &ssp);
Johan Hedberg48264f02011-11-09 13:58:58 +02003228 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003229 info->dev_class, info->rssi,
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02003230 !name_known, ssp, NULL, 0, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003231 }
3232 } else {
3233 struct inquiry_info_with_rssi *info = (void *) (skb->data + 1);
3234
Johan Hedberge17acd42011-03-30 23:57:16 +03003235 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003236 bacpy(&data.bdaddr, &info->bdaddr);
3237 data.pscan_rep_mode = info->pscan_rep_mode;
3238 data.pscan_period_mode = info->pscan_period_mode;
3239 data.pscan_mode = 0x00;
3240 memcpy(data.dev_class, info->dev_class, 3);
3241 data.clock_offset = info->clock_offset;
3242 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003243 data.ssp_mode = 0x00;
Johan Hedberg31754052012-01-04 13:39:52 +02003244 name_known = hci_inquiry_cache_update(hdev, &data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003245 false, &ssp);
Johan Hedberg48264f02011-11-09 13:58:58 +02003246 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003247 info->dev_class, info->rssi,
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02003248 !name_known, ssp, NULL, 0, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003249 }
3250 }
3251
3252 hci_dev_unlock(hdev);
3253}
3254
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003255static void hci_remote_ext_features_evt(struct hci_dev *hdev,
3256 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003257{
Marcel Holtmann41a96212008-07-14 20:13:48 +02003258 struct hci_ev_remote_ext_features *ev = (void *) skb->data;
3259 struct hci_conn *conn;
3260
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003261 BT_DBG("%s", hdev->name);
Marcel Holtmann41a96212008-07-14 20:13:48 +02003262
Marcel Holtmann41a96212008-07-14 20:13:48 +02003263 hci_dev_lock(hdev);
3264
3265 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02003266 if (!conn)
3267 goto unlock;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003268
Johan Hedbergcad718e2013-04-17 15:00:51 +03003269 if (ev->page < HCI_MAX_PAGES)
3270 memcpy(conn->features[ev->page], ev->features, 8);
3271
Johan Hedbergccd556f2010-11-10 17:11:51 +02003272 if (!ev->status && ev->page == 0x01) {
3273 struct inquiry_entry *ie;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003274
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02003275 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
3276 if (ie)
Johan Hedberg02b7cc62012-02-28 02:28:43 +02003277 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
Marcel Holtmann769be972008-07-14 20:13:49 +02003278
Jaganath Kanakkasserybbb0ead2013-04-16 20:16:30 +05303279 if (ev->features[0] & LMP_HOST_SSP) {
Johan Hedberg58a681e2012-01-16 06:47:28 +02003280 set_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
Jaganath Kanakkasserybbb0ead2013-04-16 20:16:30 +05303281 } else {
3282 /* It is mandatory by the Bluetooth specification that
3283 * Extended Inquiry Results are only used when Secure
3284 * Simple Pairing is enabled, but some devices violate
3285 * this.
3286 *
3287 * To make these devices work, the internal SSP
3288 * enabled flag needs to be cleared if the remote host
3289 * features do not indicate SSP support */
3290 clear_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
3291 }
Marcel Holtmanneb9a8f32014-01-15 22:37:38 -08003292
3293 if (ev->features[0] & LMP_HOST_SC)
3294 set_bit(HCI_CONN_SC_ENABLED, &conn->flags);
Marcel Holtmann41a96212008-07-14 20:13:48 +02003295 }
3296
Johan Hedbergccd556f2010-11-10 17:11:51 +02003297 if (conn->state != BT_CONFIG)
3298 goto unlock;
3299
Johan Hedberg671267b2012-05-12 16:11:50 -03003300 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02003301 struct hci_cp_remote_name_req cp;
3302 memset(&cp, 0, sizeof(cp));
3303 bacpy(&cp.bdaddr, &conn->dst);
3304 cp.pscan_rep_mode = 0x02;
3305 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
Johan Hedbergb644ba32012-01-17 21:48:47 +02003306 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
3307 mgmt_device_connected(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003308 conn->dst_type, 0, NULL, 0,
3309 conn->dev_class);
Johan Hedberg392599b2010-11-18 22:22:28 +02003310
Johan Hedberg127178d2010-11-18 22:22:29 +02003311 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02003312 conn->state = BT_CONNECTED;
3313 hci_proto_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02003314 hci_conn_drop(conn);
Johan Hedbergccd556f2010-11-10 17:11:51 +02003315 }
3316
3317unlock:
Marcel Holtmann41a96212008-07-14 20:13:48 +02003318 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003319}
3320
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003321static void hci_sync_conn_complete_evt(struct hci_dev *hdev,
3322 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003323{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003324 struct hci_ev_sync_conn_complete *ev = (void *) skb->data;
3325 struct hci_conn *conn;
3326
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003327 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003328
3329 hci_dev_lock(hdev);
3330
3331 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02003332 if (!conn) {
3333 if (ev->link_type == ESCO_LINK)
3334 goto unlock;
3335
3336 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
3337 if (!conn)
3338 goto unlock;
3339
3340 conn->type = SCO_LINK;
3341 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003342
Marcel Holtmann732547f2009-04-19 19:14:14 +02003343 switch (ev->status) {
3344 case 0x00:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003345 conn->handle = __le16_to_cpu(ev->handle);
3346 conn->state = BT_CONNECTED;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02003347
3348 hci_conn_add_sysfs(conn);
Marcel Holtmann732547f2009-04-19 19:14:14 +02003349 break;
3350
Frédéric Dalleau1a4c9582013-08-19 14:24:02 +02003351 case 0x0d: /* Connection Rejected due to Limited Resources */
Stephen Coe705e5712010-02-16 11:29:44 -05003352 case 0x11: /* Unsupported Feature or Parameter Value */
Marcel Holtmann732547f2009-04-19 19:14:14 +02003353 case 0x1c: /* SCO interval rejected */
Nick Pelly1038a002010-02-03 11:42:26 -08003354 case 0x1a: /* Unsupported Remote Feature */
Marcel Holtmann732547f2009-04-19 19:14:14 +02003355 case 0x1f: /* Unspecified error */
Andrew Earl27539bc2014-03-10 10:31:04 +00003356 case 0x20: /* Unsupported LMP Parameter value */
Frédéric Dalleau2dea6322013-08-19 14:24:03 +02003357 if (conn->out) {
Marcel Holtmann732547f2009-04-19 19:14:14 +02003358 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
3359 (hdev->esco_type & EDR_ESCO_MASK);
Frédéric Dalleau2dea6322013-08-19 14:24:03 +02003360 if (hci_setup_sync(conn, conn->link->handle))
3361 goto unlock;
Marcel Holtmann732547f2009-04-19 19:14:14 +02003362 }
3363 /* fall through */
3364
3365 default:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003366 conn->state = BT_CLOSED;
Marcel Holtmann732547f2009-04-19 19:14:14 +02003367 break;
3368 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003369
3370 hci_proto_connect_cfm(conn, ev->status);
3371 if (ev->status)
3372 hci_conn_del(conn);
3373
3374unlock:
3375 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003376}
3377
Marcel Holtmannefdcf8e2013-10-15 10:31:12 -07003378static inline size_t eir_get_length(u8 *eir, size_t eir_len)
3379{
3380 size_t parsed = 0;
3381
3382 while (parsed < eir_len) {
3383 u8 field_len = eir[0];
3384
3385 if (field_len == 0)
3386 return parsed;
3387
3388 parsed += field_len + 1;
3389 eir += field_len + 1;
3390 }
3391
3392 return eir_len;
3393}
3394
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003395static void hci_extended_inquiry_result_evt(struct hci_dev *hdev,
3396 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003397{
3398 struct inquiry_data data;
3399 struct extended_inquiry_info *info = (void *) (skb->data + 1);
3400 int num_rsp = *((__u8 *) skb->data);
Vishal Agarwal9d939d92012-04-26 19:19:56 +05303401 size_t eir_len;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003402
3403 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
3404
3405 if (!num_rsp)
3406 return;
3407
Andre Guedes1519cc12012-03-21 00:03:38 -03003408 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags))
3409 return;
3410
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003411 hci_dev_lock(hdev);
3412
Johan Hedberge17acd42011-03-30 23:57:16 +03003413 for (; num_rsp; num_rsp--, info++) {
Johan Hedberg388fc8f2012-02-23 00:38:59 +02003414 bool name_known, ssp;
Johan Hedberg561aafb2012-01-04 13:31:59 +02003415
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003416 bacpy(&data.bdaddr, &info->bdaddr);
Szymon Janc138d22e2011-02-17 16:44:23 +01003417 data.pscan_rep_mode = info->pscan_rep_mode;
3418 data.pscan_period_mode = info->pscan_period_mode;
3419 data.pscan_mode = 0x00;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003420 memcpy(data.dev_class, info->dev_class, 3);
Szymon Janc138d22e2011-02-17 16:44:23 +01003421 data.clock_offset = info->clock_offset;
3422 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003423 data.ssp_mode = 0x01;
Johan Hedberg561aafb2012-01-04 13:31:59 +02003424
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003425 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg4ddb1932012-01-15 20:04:43 +02003426 name_known = eir_has_data_type(info->data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003427 sizeof(info->data),
3428 EIR_NAME_COMPLETE);
Johan Hedberg561aafb2012-01-04 13:31:59 +02003429 else
3430 name_known = true;
3431
Johan Hedberg388fc8f2012-02-23 00:38:59 +02003432 name_known = hci_inquiry_cache_update(hdev, &data, name_known,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003433 &ssp);
Vishal Agarwal9d939d92012-04-26 19:19:56 +05303434 eir_len = eir_get_length(info->data, sizeof(info->data));
Johan Hedberg48264f02011-11-09 13:58:58 +02003435 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003436 info->dev_class, info->rssi, !name_known,
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02003437 ssp, info->data, eir_len, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003438 }
3439
3440 hci_dev_unlock(hdev);
3441}
3442
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003443static void hci_key_refresh_complete_evt(struct hci_dev *hdev,
3444 struct sk_buff *skb)
3445{
3446 struct hci_ev_key_refresh_complete *ev = (void *) skb->data;
3447 struct hci_conn *conn;
3448
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003449 BT_DBG("%s status 0x%2.2x handle 0x%4.4x", hdev->name, ev->status,
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003450 __le16_to_cpu(ev->handle));
3451
3452 hci_dev_lock(hdev);
3453
3454 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3455 if (!conn)
3456 goto unlock;
3457
Johan Hedberg9eb1fbf2014-04-11 12:02:31 -07003458 /* For BR/EDR the necessary steps are taken through the
3459 * auth_complete event.
3460 */
3461 if (conn->type != LE_LINK)
3462 goto unlock;
3463
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003464 if (!ev->status)
3465 conn->sec_level = conn->pending_sec_level;
3466
3467 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
3468
3469 if (ev->status && conn->state == BT_CONNECTED) {
Andre Guedesbed71742013-01-30 11:50:56 -03003470 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
David Herrmann76a68ba2013-04-06 20:28:37 +02003471 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003472 goto unlock;
3473 }
3474
3475 if (conn->state == BT_CONFIG) {
3476 if (!ev->status)
3477 conn->state = BT_CONNECTED;
3478
3479 hci_proto_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02003480 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003481 } else {
3482 hci_auth_cfm(conn, ev->status);
3483
3484 hci_conn_hold(conn);
3485 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02003486 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003487 }
3488
3489unlock:
3490 hci_dev_unlock(hdev);
3491}
3492
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003493static u8 hci_get_auth_req(struct hci_conn *conn)
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003494{
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003495 /* If remote requests no-bonding follow that lead */
Mikel Astizacabae92013-06-28 10:56:28 +02003496 if (conn->remote_auth == HCI_AT_NO_BONDING ||
3497 conn->remote_auth == HCI_AT_NO_BONDING_MITM)
Waldemar Rymarkiewicz58797bf2011-04-28 12:07:58 +02003498 return conn->remote_auth | (conn->auth_type & 0x01);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003499
Mikel Astizb7f94c82014-04-08 14:21:31 +02003500 /* If both remote and local have enough IO capabilities, require
3501 * MITM protection
3502 */
3503 if (conn->remote_cap != HCI_IO_NO_INPUT_OUTPUT &&
3504 conn->io_capability != HCI_IO_NO_INPUT_OUTPUT)
3505 return conn->remote_auth | 0x01;
3506
Timo Mueller7e741702014-04-08 14:21:33 +02003507 /* No MITM protection possible so ignore remote requirement */
3508 return (conn->remote_auth & ~0x01) | (conn->auth_type & 0x01);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003509}
3510
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003511static void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann04936842008-07-14 20:13:48 +02003512{
3513 struct hci_ev_io_capa_request *ev = (void *) skb->data;
3514 struct hci_conn *conn;
3515
3516 BT_DBG("%s", hdev->name);
3517
3518 hci_dev_lock(hdev);
3519
3520 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003521 if (!conn)
3522 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02003523
Johan Hedberg03b555e2011-01-04 15:40:05 +02003524 hci_conn_hold(conn);
3525
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003526 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg03b555e2011-01-04 15:40:05 +02003527 goto unlock;
3528
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003529 if (test_bit(HCI_PAIRABLE, &hdev->dev_flags) ||
Gustavo Padovan807deac2012-05-17 00:36:24 -03003530 (conn->remote_auth & ~0x01) == HCI_AT_NO_BONDING) {
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003531 struct hci_cp_io_capability_reply cp;
3532
3533 bacpy(&cp.bdaddr, &ev->bdaddr);
Hemant Gupta7a7f1e72012-01-16 13:34:29 +05303534 /* Change the IO capability from KeyboardDisplay
3535 * to DisplayYesNo as it is not supported by BT spec. */
3536 cp.capability = (conn->io_capability == 0x04) ?
Mikel Astiza7676312013-06-28 10:56:29 +02003537 HCI_IO_DISPLAY_YESNO : conn->io_capability;
Mikel Astizb7f94c82014-04-08 14:21:31 +02003538
3539 /* If we are initiators, there is no remote information yet */
3540 if (conn->remote_auth == 0xff) {
3541 cp.authentication = conn->auth_type;
Mikel Astiz6fd6b912014-04-08 14:21:32 +02003542
Mikel Astizb16c6602014-04-08 14:21:34 +02003543 /* Request MITM protection if our IO caps allow it
Johan Hedberg4ad51a72014-06-09 14:41:25 +03003544 * except for the no-bonding case.
3545 * conn->auth_type is not updated here since
3546 * that might cause the user confirmation to be
3547 * rejected in case the remote doesn't have the
3548 * IO capabilities for MITM.
Mikel Astizb16c6602014-04-08 14:21:34 +02003549 */
Mikel Astiz6fd6b912014-04-08 14:21:32 +02003550 if (conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
Mikel Astizb16c6602014-04-08 14:21:34 +02003551 cp.authentication != HCI_AT_NO_BONDING)
Mikel Astiz6fd6b912014-04-08 14:21:32 +02003552 cp.authentication |= 0x01;
Mikel Astizb7f94c82014-04-08 14:21:31 +02003553 } else {
3554 conn->auth_type = hci_get_auth_req(conn);
3555 cp.authentication = conn->auth_type;
3556 }
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003557
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03003558 if (hci_find_remote_oob_data(hdev, &conn->dst) &&
3559 (conn->out || test_bit(HCI_CONN_REMOTE_OOB, &conn->flags)))
Szymon Jancce85ee12011-03-22 13:12:23 +01003560 cp.oob_data = 0x01;
3561 else
3562 cp.oob_data = 0x00;
3563
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003564 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003565 sizeof(cp), &cp);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003566 } else {
3567 struct hci_cp_io_capability_neg_reply cp;
3568
3569 bacpy(&cp.bdaddr, &ev->bdaddr);
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02003570 cp.reason = HCI_ERROR_PAIRING_NOT_ALLOWED;
Johan Hedberg03b555e2011-01-04 15:40:05 +02003571
3572 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003573 sizeof(cp), &cp);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003574 }
3575
3576unlock:
3577 hci_dev_unlock(hdev);
3578}
3579
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003580static void hci_io_capa_reply_evt(struct hci_dev *hdev, struct sk_buff *skb)
Johan Hedberg03b555e2011-01-04 15:40:05 +02003581{
3582 struct hci_ev_io_capa_reply *ev = (void *) skb->data;
3583 struct hci_conn *conn;
3584
3585 BT_DBG("%s", hdev->name);
3586
3587 hci_dev_lock(hdev);
3588
3589 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3590 if (!conn)
3591 goto unlock;
3592
Johan Hedberg03b555e2011-01-04 15:40:05 +02003593 conn->remote_cap = ev->capability;
Johan Hedberg03b555e2011-01-04 15:40:05 +02003594 conn->remote_auth = ev->authentication;
Johan Hedberg58a681e2012-01-16 06:47:28 +02003595 if (ev->oob_data)
3596 set_bit(HCI_CONN_REMOTE_OOB, &conn->flags);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003597
3598unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02003599 hci_dev_unlock(hdev);
3600}
3601
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003602static void hci_user_confirm_request_evt(struct hci_dev *hdev,
3603 struct sk_buff *skb)
Johan Hedberga5c29682011-02-19 12:05:57 -03003604{
3605 struct hci_ev_user_confirm_req *ev = (void *) skb->data;
Johan Hedberg55bc1a32011-04-28 11:28:56 -07003606 int loc_mitm, rem_mitm, confirm_hint = 0;
Johan Hedberg7a828902011-04-28 11:28:53 -07003607 struct hci_conn *conn;
Johan Hedberga5c29682011-02-19 12:05:57 -03003608
3609 BT_DBG("%s", hdev->name);
3610
3611 hci_dev_lock(hdev);
3612
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003613 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg7a828902011-04-28 11:28:53 -07003614 goto unlock;
Johan Hedberga5c29682011-02-19 12:05:57 -03003615
Johan Hedberg7a828902011-04-28 11:28:53 -07003616 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3617 if (!conn)
3618 goto unlock;
3619
3620 loc_mitm = (conn->auth_type & 0x01);
3621 rem_mitm = (conn->remote_auth & 0x01);
3622
3623 /* If we require MITM but the remote device can't provide that
Mikel Astiz6fd6b912014-04-08 14:21:32 +02003624 * (it has NoInputNoOutput) then reject the confirmation request
3625 */
3626 if (loc_mitm && conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) {
Johan Hedberg7a828902011-04-28 11:28:53 -07003627 BT_DBG("Rejecting request: remote device can't provide MITM");
3628 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003629 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberg7a828902011-04-28 11:28:53 -07003630 goto unlock;
3631 }
3632
3633 /* If no side requires MITM protection; auto-accept */
Mikel Astiza7676312013-06-28 10:56:29 +02003634 if ((!loc_mitm || conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) &&
3635 (!rem_mitm || conn->io_capability == HCI_IO_NO_INPUT_OUTPUT)) {
Johan Hedberg55bc1a32011-04-28 11:28:56 -07003636
3637 /* If we're not the initiators request authorization to
3638 * proceed from user space (mgmt_user_confirm with
Johan Hedbergba15a582014-06-09 13:58:14 +03003639 * confirm_hint set to 1). The exception is if neither
3640 * side had MITM in which case we do auto-accept.
3641 */
3642 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) &&
3643 (loc_mitm || rem_mitm)) {
Johan Hedberg55bc1a32011-04-28 11:28:56 -07003644 BT_DBG("Confirming auto-accept as acceptor");
3645 confirm_hint = 1;
3646 goto confirm;
3647 }
3648
Johan Hedberg9f616562011-04-28 11:28:54 -07003649 BT_DBG("Auto-accept of user confirmation with %ums delay",
Gustavo Padovan807deac2012-05-17 00:36:24 -03003650 hdev->auto_accept_delay);
Johan Hedberg9f616562011-04-28 11:28:54 -07003651
3652 if (hdev->auto_accept_delay > 0) {
3653 int delay = msecs_to_jiffies(hdev->auto_accept_delay);
Johan Hedberg7bc18d92013-10-16 18:11:39 +03003654 queue_delayed_work(conn->hdev->workqueue,
3655 &conn->auto_accept_work, delay);
Johan Hedberg9f616562011-04-28 11:28:54 -07003656 goto unlock;
3657 }
3658
Johan Hedberg7a828902011-04-28 11:28:53 -07003659 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003660 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberg7a828902011-04-28 11:28:53 -07003661 goto unlock;
3662 }
3663
Johan Hedberg55bc1a32011-04-28 11:28:56 -07003664confirm:
Johan Hedberg39adbff2014-03-20 08:18:14 +02003665 mgmt_user_confirm_request(hdev, &ev->bdaddr, ACL_LINK, 0,
3666 le32_to_cpu(ev->passkey), confirm_hint);
Johan Hedberg7a828902011-04-28 11:28:53 -07003667
3668unlock:
Johan Hedberga5c29682011-02-19 12:05:57 -03003669 hci_dev_unlock(hdev);
3670}
3671
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003672static void hci_user_passkey_request_evt(struct hci_dev *hdev,
3673 struct sk_buff *skb)
Brian Gix1143d452011-11-23 08:28:34 -08003674{
3675 struct hci_ev_user_passkey_req *ev = (void *) skb->data;
3676
3677 BT_DBG("%s", hdev->name);
3678
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003679 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg272d90d2012-02-09 15:26:12 +02003680 mgmt_user_passkey_request(hdev, &ev->bdaddr, ACL_LINK, 0);
Brian Gix1143d452011-11-23 08:28:34 -08003681}
3682
Johan Hedberg92a25252012-09-06 18:39:26 +03003683static void hci_user_passkey_notify_evt(struct hci_dev *hdev,
3684 struct sk_buff *skb)
3685{
3686 struct hci_ev_user_passkey_notify *ev = (void *) skb->data;
3687 struct hci_conn *conn;
3688
3689 BT_DBG("%s", hdev->name);
3690
3691 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3692 if (!conn)
3693 return;
3694
3695 conn->passkey_notify = __le32_to_cpu(ev->passkey);
3696 conn->passkey_entered = 0;
3697
3698 if (test_bit(HCI_MGMT, &hdev->dev_flags))
3699 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
3700 conn->dst_type, conn->passkey_notify,
3701 conn->passkey_entered);
3702}
3703
3704static void hci_keypress_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
3705{
3706 struct hci_ev_keypress_notify *ev = (void *) skb->data;
3707 struct hci_conn *conn;
3708
3709 BT_DBG("%s", hdev->name);
3710
3711 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3712 if (!conn)
3713 return;
3714
3715 switch (ev->type) {
3716 case HCI_KEYPRESS_STARTED:
3717 conn->passkey_entered = 0;
3718 return;
3719
3720 case HCI_KEYPRESS_ENTERED:
3721 conn->passkey_entered++;
3722 break;
3723
3724 case HCI_KEYPRESS_ERASED:
3725 conn->passkey_entered--;
3726 break;
3727
3728 case HCI_KEYPRESS_CLEARED:
3729 conn->passkey_entered = 0;
3730 break;
3731
3732 case HCI_KEYPRESS_COMPLETED:
3733 return;
3734 }
3735
3736 if (test_bit(HCI_MGMT, &hdev->dev_flags))
3737 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
3738 conn->dst_type, conn->passkey_notify,
3739 conn->passkey_entered);
3740}
3741
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003742static void hci_simple_pair_complete_evt(struct hci_dev *hdev,
3743 struct sk_buff *skb)
Marcel Holtmann04936842008-07-14 20:13:48 +02003744{
3745 struct hci_ev_simple_pair_complete *ev = (void *) skb->data;
3746 struct hci_conn *conn;
3747
3748 BT_DBG("%s", hdev->name);
3749
3750 hci_dev_lock(hdev);
3751
3752 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg2a611692011-02-19 12:06:00 -03003753 if (!conn)
3754 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02003755
Johan Hedberg2a611692011-02-19 12:06:00 -03003756 /* To avoid duplicate auth_failed events to user space we check
3757 * the HCI_CONN_AUTH_PEND flag which will be set if we
3758 * initiated the authentication. A traditional auth_complete
3759 * event gets always produced as initiator and is also mapped to
3760 * the mgmt_auth_failed event */
Mikel Astizfa1bd912012-08-09 09:52:29 +02003761 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) && ev->status)
Johan Hedbergbab73cb2012-02-09 16:07:29 +02003762 mgmt_auth_failed(hdev, &conn->dst, conn->type, conn->dst_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003763 ev->status);
Johan Hedberg2a611692011-02-19 12:06:00 -03003764
David Herrmann76a68ba2013-04-06 20:28:37 +02003765 hci_conn_drop(conn);
Johan Hedberg2a611692011-02-19 12:06:00 -03003766
3767unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02003768 hci_dev_unlock(hdev);
3769}
3770
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003771static void hci_remote_host_features_evt(struct hci_dev *hdev,
3772 struct sk_buff *skb)
Marcel Holtmann41a96212008-07-14 20:13:48 +02003773{
3774 struct hci_ev_remote_host_features *ev = (void *) skb->data;
3775 struct inquiry_entry *ie;
Johan Hedbergcad718e2013-04-17 15:00:51 +03003776 struct hci_conn *conn;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003777
3778 BT_DBG("%s", hdev->name);
3779
3780 hci_dev_lock(hdev);
3781
Johan Hedbergcad718e2013-04-17 15:00:51 +03003782 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3783 if (conn)
3784 memcpy(conn->features[1], ev->features, 8);
3785
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02003786 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
3787 if (ie)
Johan Hedberg02b7cc62012-02-28 02:28:43 +02003788 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
Marcel Holtmann41a96212008-07-14 20:13:48 +02003789
3790 hci_dev_unlock(hdev);
3791}
3792
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003793static void hci_remote_oob_data_request_evt(struct hci_dev *hdev,
3794 struct sk_buff *skb)
Szymon Janc2763eda2011-03-22 13:12:22 +01003795{
3796 struct hci_ev_remote_oob_data_request *ev = (void *) skb->data;
3797 struct oob_data *data;
3798
3799 BT_DBG("%s", hdev->name);
3800
3801 hci_dev_lock(hdev);
3802
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003803 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Szymon Jance1ba1f12011-04-06 13:01:59 +02003804 goto unlock;
3805
Szymon Janc2763eda2011-03-22 13:12:22 +01003806 data = hci_find_remote_oob_data(hdev, &ev->bdaddr);
3807 if (data) {
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08003808 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) {
3809 struct hci_cp_remote_oob_ext_data_reply cp;
Szymon Janc2763eda2011-03-22 13:12:22 +01003810
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08003811 bacpy(&cp.bdaddr, &ev->bdaddr);
3812 memcpy(cp.hash192, data->hash192, sizeof(cp.hash192));
3813 memcpy(cp.randomizer192, data->randomizer192,
3814 sizeof(cp.randomizer192));
3815 memcpy(cp.hash256, data->hash256, sizeof(cp.hash256));
3816 memcpy(cp.randomizer256, data->randomizer256,
3817 sizeof(cp.randomizer256));
Szymon Janc2763eda2011-03-22 13:12:22 +01003818
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08003819 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_EXT_DATA_REPLY,
3820 sizeof(cp), &cp);
3821 } else {
3822 struct hci_cp_remote_oob_data_reply cp;
3823
3824 bacpy(&cp.bdaddr, &ev->bdaddr);
3825 memcpy(cp.hash, data->hash192, sizeof(cp.hash));
3826 memcpy(cp.randomizer, data->randomizer192,
3827 sizeof(cp.randomizer));
3828
3829 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_REPLY,
3830 sizeof(cp), &cp);
3831 }
Szymon Janc2763eda2011-03-22 13:12:22 +01003832 } else {
3833 struct hci_cp_remote_oob_data_neg_reply cp;
3834
3835 bacpy(&cp.bdaddr, &ev->bdaddr);
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08003836 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_NEG_REPLY,
3837 sizeof(cp), &cp);
Szymon Janc2763eda2011-03-22 13:12:22 +01003838 }
3839
Szymon Jance1ba1f12011-04-06 13:01:59 +02003840unlock:
Szymon Janc2763eda2011-03-22 13:12:22 +01003841 hci_dev_unlock(hdev);
3842}
3843
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03003844static void hci_phy_link_complete_evt(struct hci_dev *hdev,
3845 struct sk_buff *skb)
3846{
3847 struct hci_ev_phy_link_complete *ev = (void *) skb->data;
3848 struct hci_conn *hcon, *bredr_hcon;
3849
3850 BT_DBG("%s handle 0x%2.2x status 0x%2.2x", hdev->name, ev->phy_handle,
3851 ev->status);
3852
3853 hci_dev_lock(hdev);
3854
3855 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
3856 if (!hcon) {
3857 hci_dev_unlock(hdev);
3858 return;
3859 }
3860
3861 if (ev->status) {
3862 hci_conn_del(hcon);
3863 hci_dev_unlock(hdev);
3864 return;
3865 }
3866
3867 bredr_hcon = hcon->amp_mgr->l2cap_conn->hcon;
3868
3869 hcon->state = BT_CONNECTED;
3870 bacpy(&hcon->dst, &bredr_hcon->dst);
3871
3872 hci_conn_hold(hcon);
3873 hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02003874 hci_conn_drop(hcon);
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03003875
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03003876 hci_conn_add_sysfs(hcon);
3877
Andrei Emeltchenkocf70ff22012-10-31 15:46:36 +02003878 amp_physical_cfm(bredr_hcon, hcon);
3879
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03003880 hci_dev_unlock(hdev);
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03003881}
3882
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03003883static void hci_loglink_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
3884{
3885 struct hci_ev_logical_link_complete *ev = (void *) skb->data;
3886 struct hci_conn *hcon;
3887 struct hci_chan *hchan;
3888 struct amp_mgr *mgr;
3889
3890 BT_DBG("%s log_handle 0x%4.4x phy_handle 0x%2.2x status 0x%2.2x",
3891 hdev->name, le16_to_cpu(ev->handle), ev->phy_handle,
3892 ev->status);
3893
3894 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
3895 if (!hcon)
3896 return;
3897
3898 /* Create AMP hchan */
3899 hchan = hci_chan_create(hcon);
3900 if (!hchan)
3901 return;
3902
3903 hchan->handle = le16_to_cpu(ev->handle);
3904
3905 BT_DBG("hcon %p mgr %p hchan %p", hcon, hcon->amp_mgr, hchan);
3906
3907 mgr = hcon->amp_mgr;
3908 if (mgr && mgr->bredr_chan) {
3909 struct l2cap_chan *bredr_chan = mgr->bredr_chan;
3910
3911 l2cap_chan_lock(bredr_chan);
3912
3913 bredr_chan->conn->mtu = hdev->block_mtu;
3914 l2cap_logical_cfm(bredr_chan, hchan, 0);
3915 hci_conn_hold(hcon);
3916
3917 l2cap_chan_unlock(bredr_chan);
3918 }
3919}
3920
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02003921static void hci_disconn_loglink_complete_evt(struct hci_dev *hdev,
3922 struct sk_buff *skb)
3923{
3924 struct hci_ev_disconn_logical_link_complete *ev = (void *) skb->data;
3925 struct hci_chan *hchan;
3926
3927 BT_DBG("%s log handle 0x%4.4x status 0x%2.2x", hdev->name,
3928 le16_to_cpu(ev->handle), ev->status);
3929
3930 if (ev->status)
3931 return;
3932
3933 hci_dev_lock(hdev);
3934
3935 hchan = hci_chan_lookup_handle(hdev, le16_to_cpu(ev->handle));
3936 if (!hchan)
3937 goto unlock;
3938
3939 amp_destroy_logical_link(hchan, ev->reason);
3940
3941unlock:
3942 hci_dev_unlock(hdev);
3943}
3944
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02003945static void hci_disconn_phylink_complete_evt(struct hci_dev *hdev,
3946 struct sk_buff *skb)
3947{
3948 struct hci_ev_disconn_phy_link_complete *ev = (void *) skb->data;
3949 struct hci_conn *hcon;
3950
3951 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
3952
3953 if (ev->status)
3954 return;
3955
3956 hci_dev_lock(hdev);
3957
3958 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
3959 if (hcon) {
3960 hcon->state = BT_CLOSED;
3961 hci_conn_del(hcon);
3962 }
3963
3964 hci_dev_unlock(hdev);
3965}
3966
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003967static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Ville Tervofcd89c02011-02-10 22:38:47 -03003968{
3969 struct hci_ev_le_conn_complete *ev = (void *) skb->data;
3970 struct hci_conn *conn;
Johan Hedberg68d6f6d2014-02-18 21:41:32 +02003971 struct smp_irk *irk;
Ville Tervofcd89c02011-02-10 22:38:47 -03003972
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003973 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Ville Tervofcd89c02011-02-10 22:38:47 -03003974
3975 hci_dev_lock(hdev);
3976
Andre Guedesb47a09b2012-07-27 15:10:15 -03003977 conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
Ville Tervob62f3282011-02-10 22:38:50 -03003978 if (!conn) {
3979 conn = hci_conn_add(hdev, LE_LINK, &ev->bdaddr);
3980 if (!conn) {
3981 BT_ERR("No memory for new connection");
Andre Guedes230fd162012-07-27 15:10:10 -03003982 goto unlock;
Ville Tervob62f3282011-02-10 22:38:50 -03003983 }
Andre Guedes29b79882011-05-31 14:20:54 -03003984
3985 conn->dst_type = ev->bdaddr_type;
Andre Guedesb9b343d2012-07-27 15:10:11 -03003986
3987 if (ev->role == LE_CONN_ROLE_MASTER) {
3988 conn->out = true;
3989 conn->link_mode |= HCI_LM_MASTER;
3990 }
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02003991
3992 /* If we didn't have a hci_conn object previously
3993 * but we're in master role this must be something
3994 * initiated using a white list. Since white list based
3995 * connections are not "first class citizens" we don't
3996 * have full tracking of them. Therefore, we go ahead
3997 * with a "best effort" approach of determining the
3998 * initiator address based on the HCI_PRIVACY flag.
3999 */
4000 if (conn->out) {
4001 conn->resp_addr_type = ev->bdaddr_type;
4002 bacpy(&conn->resp_addr, &ev->bdaddr);
4003 if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) {
4004 conn->init_addr_type = ADDR_LE_DEV_RANDOM;
4005 bacpy(&conn->init_addr, &hdev->rpa);
4006 } else {
4007 hci_copy_identity_address(hdev,
4008 &conn->init_addr,
4009 &conn->init_addr_type);
4010 }
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02004011 }
Johan Hedberg9489eca2014-02-28 17:45:46 +02004012 } else {
4013 cancel_delayed_work(&conn->le_conn_timeout);
Ville Tervob62f3282011-02-10 22:38:50 -03004014 }
Ville Tervofcd89c02011-02-10 22:38:47 -03004015
Johan Hedberg80c24ab2014-03-24 20:21:51 +02004016 if (!conn->out) {
4017 /* Set the responder (our side) address type based on
4018 * the advertising address type.
4019 */
4020 conn->resp_addr_type = hdev->adv_addr_type;
4021 if (hdev->adv_addr_type == ADDR_LE_DEV_RANDOM)
4022 bacpy(&conn->resp_addr, &hdev->random_addr);
4023 else
4024 bacpy(&conn->resp_addr, &hdev->bdaddr);
4025
4026 conn->init_addr_type = ev->bdaddr_type;
4027 bacpy(&conn->init_addr, &ev->bdaddr);
4028 }
Johan Hedberg7be2edb2014-02-23 19:42:17 +02004029
Marcel Holtmannedb4b462014-02-18 15:13:43 -08004030 /* Lookup the identity address from the stored connection
4031 * address and address type.
4032 *
4033 * When establishing connections to an identity address, the
4034 * connection procedure will store the resolvable random
4035 * address first. Now if it can be converted back into the
4036 * identity address, start using the identity address from
4037 * now on.
4038 */
4039 irk = hci_get_irk(hdev, &conn->dst, conn->dst_type);
Johan Hedberg68d6f6d2014-02-18 21:41:32 +02004040 if (irk) {
4041 bacpy(&conn->dst, &irk->bdaddr);
4042 conn->dst_type = irk->addr_type;
4043 }
4044
Andre Guedescd17dec2012-07-27 15:10:16 -03004045 if (ev->status) {
Andre Guedes06c053f2014-02-26 20:21:41 -03004046 hci_le_conn_failed(conn, ev->status);
Andre Guedescd17dec2012-07-27 15:10:16 -03004047 goto unlock;
4048 }
4049
Johan Hedbergb644ba32012-01-17 21:48:47 +02004050 if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Marcel Holtmann01fdb0f2014-02-18 14:22:19 -08004051 mgmt_device_connected(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004052 conn->dst_type, 0, NULL, 0, NULL);
Vinicius Costa Gomes83bc71b2011-05-06 18:41:43 -03004053
Vinicius Costa Gomes7b5c0d52011-06-09 18:50:50 -03004054 conn->sec_level = BT_SECURITY_LOW;
Ville Tervofcd89c02011-02-10 22:38:47 -03004055 conn->handle = __le16_to_cpu(ev->handle);
4056 conn->state = BT_CONNECTED;
4057
Jukka Rissanen18722c22013-12-11 17:05:37 +02004058 if (test_bit(HCI_6LOWPAN_ENABLED, &hdev->dev_flags))
4059 set_bit(HCI_CONN_6LOWPAN, &conn->flags);
4060
Ville Tervofcd89c02011-02-10 22:38:47 -03004061 hci_conn_add_sysfs(conn);
4062
4063 hci_proto_connect_cfm(conn, ev->status);
4064
Andre Guedesa4790db2014-02-26 20:21:47 -03004065 hci_pend_le_conn_del(hdev, &conn->dst, conn->dst_type);
4066
Ville Tervofcd89c02011-02-10 22:38:47 -03004067unlock:
4068 hci_dev_unlock(hdev);
4069}
4070
Andre Guedesa4790db2014-02-26 20:21:47 -03004071/* This function requires the caller holds hdev->lock */
4072static void check_pending_le_conn(struct hci_dev *hdev, bdaddr_t *addr,
4073 u8 addr_type)
4074{
4075 struct hci_conn *conn;
Andre Guedes5b906a82014-02-26 20:21:53 -03004076 struct smp_irk *irk;
4077
4078 /* If this is a resolvable address, we should resolve it and then
4079 * update address and address type variables.
4080 */
4081 irk = hci_get_irk(hdev, addr, addr_type);
4082 if (irk) {
4083 addr = &irk->bdaddr;
4084 addr_type = irk->addr_type;
4085 }
Andre Guedesa4790db2014-02-26 20:21:47 -03004086
4087 if (!hci_pend_le_conn_lookup(hdev, addr, addr_type))
4088 return;
4089
4090 conn = hci_connect_le(hdev, addr, addr_type, BT_SECURITY_LOW,
4091 HCI_AT_NO_BONDING);
4092 if (!IS_ERR(conn))
4093 return;
4094
4095 switch (PTR_ERR(conn)) {
4096 case -EBUSY:
4097 /* If hci_connect() returns -EBUSY it means there is already
4098 * an LE connection attempt going on. Since controllers don't
4099 * support more than one connection attempt at the time, we
4100 * don't consider this an error case.
4101 */
4102 break;
4103 default:
4104 BT_DBG("Failed to connect: err %ld", PTR_ERR(conn));
4105 }
4106}
4107
Johan Hedberg4af605d2014-03-24 10:48:00 +02004108static void process_adv_report(struct hci_dev *hdev, u8 type, bdaddr_t *bdaddr,
4109 u8 bdaddr_type, s8 rssi, u8 *data, u8 len)
4110{
Johan Hedbergb9a63282014-03-25 10:51:52 +02004111 struct discovery_state *d = &hdev->discovery;
Johan Hedberg474ee062014-03-25 14:34:59 +02004112 bool match;
Johan Hedbergb9a63282014-03-25 10:51:52 +02004113
Johan Hedbergca5c4be2014-03-25 10:30:46 +02004114 /* Passive scanning shouldn't trigger any device found events */
4115 if (hdev->le_scan_type == LE_SCAN_PASSIVE) {
4116 if (type == LE_ADV_IND || type == LE_ADV_DIRECT_IND)
4117 check_pending_le_conn(hdev, bdaddr, bdaddr_type);
4118 return;
4119 }
Johan Hedberg4af605d2014-03-24 10:48:00 +02004120
Johan Hedbergb9a63282014-03-25 10:51:52 +02004121 /* If there's nothing pending either store the data from this
4122 * event or send an immediate device found event if the data
4123 * should not be stored for later.
4124 */
4125 if (!has_pending_adv_report(hdev)) {
4126 /* If the report will trigger a SCAN_REQ store it for
4127 * later merging.
4128 */
4129 if (type == LE_ADV_IND || type == LE_ADV_SCAN_IND) {
4130 store_pending_adv_report(hdev, bdaddr, bdaddr_type,
Johan Hedbergff5cd292014-03-25 14:40:52 +02004131 rssi, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004132 return;
4133 }
4134
4135 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
4136 rssi, 0, 1, data, len, NULL, 0);
4137 return;
4138 }
4139
Johan Hedberg474ee062014-03-25 14:34:59 +02004140 /* Check if the pending report is for the same device as the new one */
4141 match = (!bacmp(bdaddr, &d->last_adv_addr) &&
4142 bdaddr_type == d->last_adv_addr_type);
4143
Johan Hedbergb9a63282014-03-25 10:51:52 +02004144 /* If the pending data doesn't match this report or this isn't a
4145 * scan response (e.g. we got a duplicate ADV_IND) then force
4146 * sending of the pending data.
4147 */
Johan Hedberg474ee062014-03-25 14:34:59 +02004148 if (type != LE_ADV_SCAN_RSP || !match) {
4149 /* Send out whatever is in the cache, but skip duplicates */
4150 if (!match)
4151 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Johan Hedbergff5cd292014-03-25 14:40:52 +02004152 d->last_adv_addr_type, NULL,
4153 d->last_adv_rssi, 0, 1,
4154 d->last_adv_data,
Johan Hedberg474ee062014-03-25 14:34:59 +02004155 d->last_adv_data_len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004156
4157 /* If the new report will trigger a SCAN_REQ store it for
4158 * later merging.
4159 */
4160 if (type == LE_ADV_IND || type == LE_ADV_SCAN_IND) {
4161 store_pending_adv_report(hdev, bdaddr, bdaddr_type,
Johan Hedbergff5cd292014-03-25 14:40:52 +02004162 rssi, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004163 return;
4164 }
4165
4166 /* The advertising reports cannot be merged, so clear
4167 * the pending report and send out a device found event.
4168 */
4169 clear_pending_adv_report(hdev);
Johan Hedberg5c5b93e2014-03-29 08:39:53 +02004170 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
4171 rssi, 0, 1, data, len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004172 return;
4173 }
4174
4175 /* If we get here we've got a pending ADV_IND or ADV_SCAN_IND and
4176 * the new event is a SCAN_RSP. We can therefore proceed with
4177 * sending a merged device found event.
4178 */
4179 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
4180 d->last_adv_addr_type, NULL, rssi, 0, 1, data, len,
4181 d->last_adv_data, d->last_adv_data_len);
4182 clear_pending_adv_report(hdev);
Johan Hedberg4af605d2014-03-24 10:48:00 +02004183}
4184
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004185static void hci_le_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)
Andre Guedes9aa04c92011-05-26 16:23:51 -03004186{
Andre Guedese95beb42011-09-26 20:48:35 -03004187 u8 num_reports = skb->data[0];
4188 void *ptr = &skb->data[1];
Andre Guedes9aa04c92011-05-26 16:23:51 -03004189
Andre Guedesa4790db2014-02-26 20:21:47 -03004190 hci_dev_lock(hdev);
4191
Andre Guedese95beb42011-09-26 20:48:35 -03004192 while (num_reports--) {
4193 struct hci_ev_le_advertising_info *ev = ptr;
Johan Hedberg4af605d2014-03-24 10:48:00 +02004194 s8 rssi;
Andre Guedesa4790db2014-02-26 20:21:47 -03004195
Andre Guedes3c9e9192012-01-10 18:20:50 -03004196 rssi = ev->data[ev->length];
Johan Hedberg4af605d2014-03-24 10:48:00 +02004197 process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
4198 ev->bdaddr_type, rssi, ev->data, ev->length);
Andre Guedes3c9e9192012-01-10 18:20:50 -03004199
Andre Guedese95beb42011-09-26 20:48:35 -03004200 ptr += sizeof(*ev) + ev->length + 1;
Andre Guedes9aa04c92011-05-26 16:23:51 -03004201 }
Andre Guedesa4790db2014-02-26 20:21:47 -03004202
4203 hci_dev_unlock(hdev);
Andre Guedes9aa04c92011-05-26 16:23:51 -03004204}
4205
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004206static void hci_le_ltk_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004207{
4208 struct hci_ev_le_ltk_req *ev = (void *) skb->data;
4209 struct hci_cp_le_ltk_reply cp;
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03004210 struct hci_cp_le_ltk_neg_reply neg;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004211 struct hci_conn *conn;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03004212 struct smp_ltk *ltk;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004213
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004214 BT_DBG("%s handle 0x%4.4x", hdev->name, __le16_to_cpu(ev->handle));
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004215
4216 hci_dev_lock(hdev);
4217
4218 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03004219 if (conn == NULL)
4220 goto not_found;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004221
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08004222 ltk = hci_find_ltk(hdev, ev->ediv, ev->rand, conn->out);
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03004223 if (ltk == NULL)
4224 goto not_found;
4225
4226 memcpy(cp.ltk, ltk->val, sizeof(ltk->val));
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004227 cp.handle = cpu_to_le16(conn->handle);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03004228
4229 if (ltk->authenticated)
Andre Guedesf8776212013-07-31 16:25:28 -03004230 conn->pending_sec_level = BT_SECURITY_HIGH;
4231 else
4232 conn->pending_sec_level = BT_SECURITY_MEDIUM;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004233
Andre Guedes89cbb4d2013-07-31 16:25:29 -03004234 conn->enc_key_size = ltk->enc_size;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004235
4236 hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
4237
Claudio Takahasi5981a882013-07-25 16:34:24 -03004238 /* Ref. Bluetooth Core SPEC pages 1975 and 2004. STK is a
4239 * temporary key used to encrypt a connection following
4240 * pairing. It is used during the Encrypted Session Setup to
4241 * distribute the keys. Later, security can be re-established
4242 * using a distributed LTK.
4243 */
4244 if (ltk->type == HCI_SMP_STK_SLAVE) {
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03004245 list_del(&ltk->list);
4246 kfree(ltk);
4247 }
4248
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004249 hci_dev_unlock(hdev);
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03004250
4251 return;
4252
4253not_found:
4254 neg.handle = ev->handle;
4255 hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(neg), &neg);
4256 hci_dev_unlock(hdev);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004257}
4258
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004259static void hci_le_meta_evt(struct hci_dev *hdev, struct sk_buff *skb)
Ville Tervofcd89c02011-02-10 22:38:47 -03004260{
4261 struct hci_ev_le_meta *le_ev = (void *) skb->data;
4262
4263 skb_pull(skb, sizeof(*le_ev));
4264
4265 switch (le_ev->subevent) {
4266 case HCI_EV_LE_CONN_COMPLETE:
4267 hci_le_conn_complete_evt(hdev, skb);
4268 break;
4269
Andre Guedes9aa04c92011-05-26 16:23:51 -03004270 case HCI_EV_LE_ADVERTISING_REPORT:
4271 hci_le_adv_report_evt(hdev, skb);
4272 break;
4273
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004274 case HCI_EV_LE_LTK_REQ:
4275 hci_le_ltk_request_evt(hdev, skb);
4276 break;
4277
Ville Tervofcd89c02011-02-10 22:38:47 -03004278 default:
4279 break;
4280 }
4281}
4282
Andrei Emeltchenko9495b2e2012-09-27 17:26:22 +03004283static void hci_chan_selected_evt(struct hci_dev *hdev, struct sk_buff *skb)
4284{
4285 struct hci_ev_channel_selected *ev = (void *) skb->data;
4286 struct hci_conn *hcon;
4287
4288 BT_DBG("%s handle 0x%2.2x", hdev->name, ev->phy_handle);
4289
4290 skb_pull(skb, sizeof(*ev));
4291
4292 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
4293 if (!hcon)
4294 return;
4295
4296 amp_read_loc_assoc_final_data(hdev, hcon);
4297}
4298
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
4300{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004301 struct hci_event_hdr *hdr = (void *) skb->data;
4302 __u8 event = hdr->evt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303
Johan Hedbergb6ddb632013-04-02 13:34:31 +03004304 hci_dev_lock(hdev);
4305
4306 /* Received events are (currently) only needed when a request is
4307 * ongoing so avoid unnecessary memory allocation.
4308 */
4309 if (hdev->req_status == HCI_REQ_PEND) {
4310 kfree_skb(hdev->recv_evt);
4311 hdev->recv_evt = skb_clone(skb, GFP_KERNEL);
4312 }
4313
4314 hci_dev_unlock(hdev);
4315
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 skb_pull(skb, HCI_EVENT_HDR_SIZE);
4317
Johan Hedberg02350a72013-04-03 21:50:29 +03004318 if (hdev->sent_cmd && bt_cb(hdev->sent_cmd)->req.event == event) {
Johannes Bergc1f23a22013-10-07 18:19:16 +02004319 struct hci_command_hdr *cmd_hdr = (void *) hdev->sent_cmd->data;
4320 u16 opcode = __le16_to_cpu(cmd_hdr->opcode);
Johan Hedberg02350a72013-04-03 21:50:29 +03004321
4322 hci_req_cmd_complete(hdev, opcode, 0);
4323 }
4324
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004325 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 case HCI_EV_INQUIRY_COMPLETE:
4327 hci_inquiry_complete_evt(hdev, skb);
4328 break;
4329
4330 case HCI_EV_INQUIRY_RESULT:
4331 hci_inquiry_result_evt(hdev, skb);
4332 break;
4333
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004334 case HCI_EV_CONN_COMPLETE:
4335 hci_conn_complete_evt(hdev, skb);
Marcel Holtmann21d9e302005-09-13 01:32:25 +02004336 break;
4337
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 case HCI_EV_CONN_REQUEST:
4339 hci_conn_request_evt(hdev, skb);
4340 break;
4341
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 case HCI_EV_DISCONN_COMPLETE:
4343 hci_disconn_complete_evt(hdev, skb);
4344 break;
4345
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 case HCI_EV_AUTH_COMPLETE:
4347 hci_auth_complete_evt(hdev, skb);
4348 break;
4349
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004350 case HCI_EV_REMOTE_NAME:
4351 hci_remote_name_evt(hdev, skb);
4352 break;
4353
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 case HCI_EV_ENCRYPT_CHANGE:
4355 hci_encrypt_change_evt(hdev, skb);
4356 break;
4357
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004358 case HCI_EV_CHANGE_LINK_KEY_COMPLETE:
4359 hci_change_link_key_complete_evt(hdev, skb);
4360 break;
4361
4362 case HCI_EV_REMOTE_FEATURES:
4363 hci_remote_features_evt(hdev, skb);
4364 break;
4365
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004366 case HCI_EV_CMD_COMPLETE:
4367 hci_cmd_complete_evt(hdev, skb);
4368 break;
4369
4370 case HCI_EV_CMD_STATUS:
4371 hci_cmd_status_evt(hdev, skb);
4372 break;
4373
4374 case HCI_EV_ROLE_CHANGE:
4375 hci_role_change_evt(hdev, skb);
4376 break;
4377
4378 case HCI_EV_NUM_COMP_PKTS:
4379 hci_num_comp_pkts_evt(hdev, skb);
4380 break;
4381
4382 case HCI_EV_MODE_CHANGE:
4383 hci_mode_change_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 break;
4385
4386 case HCI_EV_PIN_CODE_REQ:
4387 hci_pin_code_request_evt(hdev, skb);
4388 break;
4389
4390 case HCI_EV_LINK_KEY_REQ:
4391 hci_link_key_request_evt(hdev, skb);
4392 break;
4393
4394 case HCI_EV_LINK_KEY_NOTIFY:
4395 hci_link_key_notify_evt(hdev, skb);
4396 break;
4397
4398 case HCI_EV_CLOCK_OFFSET:
4399 hci_clock_offset_evt(hdev, skb);
4400 break;
4401
Marcel Holtmanna8746412008-07-14 20:13:46 +02004402 case HCI_EV_PKT_TYPE_CHANGE:
4403 hci_pkt_type_change_evt(hdev, skb);
4404 break;
4405
Marcel Holtmann85a1e932005-08-09 20:28:02 -07004406 case HCI_EV_PSCAN_REP_MODE:
4407 hci_pscan_rep_mode_evt(hdev, skb);
4408 break;
4409
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004410 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
4411 hci_inquiry_result_with_rssi_evt(hdev, skb);
4412 break;
4413
4414 case HCI_EV_REMOTE_EXT_FEATURES:
4415 hci_remote_ext_features_evt(hdev, skb);
4416 break;
4417
4418 case HCI_EV_SYNC_CONN_COMPLETE:
4419 hci_sync_conn_complete_evt(hdev, skb);
4420 break;
4421
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004422 case HCI_EV_EXTENDED_INQUIRY_RESULT:
4423 hci_extended_inquiry_result_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 break;
4425
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004426 case HCI_EV_KEY_REFRESH_COMPLETE:
4427 hci_key_refresh_complete_evt(hdev, skb);
4428 break;
4429
Marcel Holtmann04936842008-07-14 20:13:48 +02004430 case HCI_EV_IO_CAPA_REQUEST:
4431 hci_io_capa_request_evt(hdev, skb);
4432 break;
4433
Johan Hedberg03b555e2011-01-04 15:40:05 +02004434 case HCI_EV_IO_CAPA_REPLY:
4435 hci_io_capa_reply_evt(hdev, skb);
4436 break;
4437
Johan Hedberga5c29682011-02-19 12:05:57 -03004438 case HCI_EV_USER_CONFIRM_REQUEST:
4439 hci_user_confirm_request_evt(hdev, skb);
4440 break;
4441
Brian Gix1143d452011-11-23 08:28:34 -08004442 case HCI_EV_USER_PASSKEY_REQUEST:
4443 hci_user_passkey_request_evt(hdev, skb);
4444 break;
4445
Johan Hedberg92a25252012-09-06 18:39:26 +03004446 case HCI_EV_USER_PASSKEY_NOTIFY:
4447 hci_user_passkey_notify_evt(hdev, skb);
4448 break;
4449
4450 case HCI_EV_KEYPRESS_NOTIFY:
4451 hci_keypress_notify_evt(hdev, skb);
4452 break;
4453
Marcel Holtmann04936842008-07-14 20:13:48 +02004454 case HCI_EV_SIMPLE_PAIR_COMPLETE:
4455 hci_simple_pair_complete_evt(hdev, skb);
4456 break;
4457
Marcel Holtmann41a96212008-07-14 20:13:48 +02004458 case HCI_EV_REMOTE_HOST_FEATURES:
4459 hci_remote_host_features_evt(hdev, skb);
4460 break;
4461
Ville Tervofcd89c02011-02-10 22:38:47 -03004462 case HCI_EV_LE_META:
4463 hci_le_meta_evt(hdev, skb);
4464 break;
4465
Andrei Emeltchenko9495b2e2012-09-27 17:26:22 +03004466 case HCI_EV_CHANNEL_SELECTED:
4467 hci_chan_selected_evt(hdev, skb);
4468 break;
4469
Szymon Janc2763eda2011-03-22 13:12:22 +01004470 case HCI_EV_REMOTE_OOB_DATA_REQUEST:
4471 hci_remote_oob_data_request_evt(hdev, skb);
4472 break;
4473
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03004474 case HCI_EV_PHY_LINK_COMPLETE:
4475 hci_phy_link_complete_evt(hdev, skb);
4476 break;
4477
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03004478 case HCI_EV_LOGICAL_LINK_COMPLETE:
4479 hci_loglink_complete_evt(hdev, skb);
4480 break;
4481
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02004482 case HCI_EV_DISCONN_LOGICAL_LINK_COMPLETE:
4483 hci_disconn_loglink_complete_evt(hdev, skb);
4484 break;
4485
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02004486 case HCI_EV_DISCONN_PHY_LINK_COMPLETE:
4487 hci_disconn_phylink_complete_evt(hdev, skb);
4488 break;
4489
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02004490 case HCI_EV_NUM_COMP_BLOCKS:
4491 hci_num_comp_blocks_evt(hdev, skb);
4492 break;
4493
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004494 default:
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004495 BT_DBG("%s event 0x%2.2x", hdev->name, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 break;
4497 }
4498
4499 kfree_skb(skb);
4500 hdev->stat.evt_rx++;
4501}