blob: a3055e90a5bbfa2b1dd89212fa07ba6c7f9085c6 [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
Johan Hedberg0857dd32014-12-19 13:40:20 +020033#include "hci_request.h"
Marcel Holtmann70247282013-10-10 14:54:15 -070034#include "a2mp.h"
Marcel Holtmann7ef9fbf2013-10-10 14:54:14 -070035#include "amp.h"
Johan Hedberg2ceba532014-06-16 19:25:16 +030036#include "smp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/* Handle HCI Event packets */
39
Marcel Holtmanna9de9242007-10-20 13:33:56 +020040static void hci_cc_inquiry_cancel(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Marcel Holtmanna9de9242007-10-20 13:33:56 +020042 __u8 status = *((__u8 *) skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030044 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Andre Guedes82f47852013-04-30 15:29:34 -030046 if (status)
Marcel Holtmanna9de9242007-10-20 13:33:56 +020047 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Andre Guedes89352e72011-11-04 14:16:53 -030049 clear_bit(HCI_INQUIRY, &hdev->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +010050 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
Andre Guedes3e13fa12013-03-27 20:04:56 -030051 wake_up_bit(&hdev->flags, HCI_INQUIRY);
Andre Guedes89352e72011-11-04 14:16:53 -030052
Johan Hedberg50143a42014-06-10 14:05:57 +030053 hci_dev_lock(hdev);
54 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
55 hci_dev_unlock(hdev);
56
Marcel Holtmanna9de9242007-10-20 13:33:56 +020057 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
Andre Guedes4d934832012-03-21 00:03:35 -030060static void hci_cc_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
61{
62 __u8 status = *((__u8 *) skb->data);
63
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030064 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andre Guedesae854a72012-03-21 00:03:36 -030065
66 if (status)
67 return;
68
69 set_bit(HCI_PERIODIC_INQ, &hdev->dev_flags);
Andre Guedes4d934832012-03-21 00:03:35 -030070}
71
Marcel Holtmanna9de9242007-10-20 13:33:56 +020072static void hci_cc_exit_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Marcel Holtmanna9de9242007-10-20 13:33:56 +020074 __u8 status = *((__u8 *) skb->data);
75
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030076 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +020077
78 if (status)
79 return;
80
Andre Guedesae854a72012-03-21 00:03:36 -030081 clear_bit(HCI_PERIODIC_INQ, &hdev->dev_flags);
82
Marcel Holtmanna9de9242007-10-20 13:33:56 +020083 hci_conn_check_pending(hdev);
84}
85
Gustavo Padovan807deac2012-05-17 00:36:24 -030086static void hci_cc_remote_name_req_cancel(struct hci_dev *hdev,
87 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +020088{
89 BT_DBG("%s", hdev->name);
90}
91
92static void hci_cc_role_discovery(struct hci_dev *hdev, struct sk_buff *skb)
93{
94 struct hci_rp_role_discovery *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030097 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Marcel Holtmanna9de9242007-10-20 13:33:56 +020099 if (rp->status)
100 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200102 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200104 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Johan Hedberg40bef302014-07-16 11:42:27 +0300105 if (conn)
106 conn->role = rp->role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200107
108 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200111static void hci_cc_read_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
112{
113 struct hci_rp_read_link_policy *rp = (void *) skb->data;
114 struct hci_conn *conn;
115
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300116 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200117
118 if (rp->status)
119 return;
120
121 hci_dev_lock(hdev);
122
123 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
124 if (conn)
125 conn->link_policy = __le16_to_cpu(rp->policy);
126
127 hci_dev_unlock(hdev);
128}
129
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200130static void hci_cc_write_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200132 struct hci_rp_write_link_policy *rp = (void *) skb->data;
133 struct hci_conn *conn;
134 void *sent;
135
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300136 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200137
138 if (rp->status)
139 return;
140
141 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LINK_POLICY);
142 if (!sent)
143 return;
144
145 hci_dev_lock(hdev);
146
147 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200148 if (conn)
Harvey Harrison83985312008-05-02 16:25:46 -0700149 conn->link_policy = get_unaligned_le16(sent + 2);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200150
151 hci_dev_unlock(hdev);
152}
153
Gustavo Padovan807deac2012-05-17 00:36:24 -0300154static void hci_cc_read_def_link_policy(struct hci_dev *hdev,
155 struct sk_buff *skb)
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200156{
157 struct hci_rp_read_def_link_policy *rp = (void *) skb->data;
158
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300159 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200160
161 if (rp->status)
162 return;
163
164 hdev->link_policy = __le16_to_cpu(rp->policy);
165}
166
Gustavo Padovan807deac2012-05-17 00:36:24 -0300167static void hci_cc_write_def_link_policy(struct hci_dev *hdev,
168 struct sk_buff *skb)
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200169{
170 __u8 status = *((__u8 *) skb->data);
171 void *sent;
172
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300173 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200174
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200175 if (status)
176 return;
177
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200178 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_LINK_POLICY);
179 if (!sent)
180 return;
181
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200182 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
Marcel Holtmann8761f9d2014-11-02 02:45:58 +0100193 if (status)
194 return;
195
Johan Hedberga297e972012-02-21 17:55:47 +0200196 /* Reset all non-persistent flags */
Johan Hedberg2cc6fb02013-03-15 17:06:57 -0500197 hdev->dev_flags &= ~HCI_PERSISTENT_MASK;
Andre Guedes69775ff2012-02-23 16:50:05 +0200198
199 hdev->discovery.state = DISCOVERY_STOPPED;
Johan Hedbergbbaf4442012-11-08 01:22:59 +0100200 hdev->inq_tx_power = HCI_TX_POWER_INVALID;
201 hdev->adv_tx_power = HCI_TX_POWER_INVALID;
Johan Hedberg3f0f5242012-11-08 01:23:00 +0100202
203 memset(hdev->adv_data, 0, sizeof(hdev->adv_data));
204 hdev->adv_data_len = 0;
Marcel Holtmannf8e808b2013-10-16 00:16:47 -0700205
206 memset(hdev->scan_rsp_data, 0, sizeof(hdev->scan_rsp_data));
207 hdev->scan_rsp_data_len = 0;
Marcel Holtmann06f5b772013-10-19 07:09:11 -0700208
Marcel Holtmann533553f2014-03-21 12:18:10 -0700209 hdev->le_scan_type = LE_SCAN_PASSIVE;
210
Marcel Holtmann06f5b772013-10-19 07:09:11 -0700211 hdev->ssp_debug_mode = 0;
Marcel Holtmanna4d55042014-10-29 23:37:53 +0100212
213 hci_bdaddr_list_clear(&hdev->le_white_list);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200214}
215
216static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb)
217{
218 __u8 status = *((__u8 *) skb->data);
219 void *sent;
220
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300221 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200222
223 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LOCAL_NAME);
224 if (!sent)
225 return;
226
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200227 hci_dev_lock(hdev);
228
Johan Hedbergf51d5b22012-02-22 18:17:32 +0200229 if (test_bit(HCI_MGMT, &hdev->dev_flags))
230 mgmt_set_local_name_complete(hdev, sent, status);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +0200231 else if (!status)
232 memcpy(hdev->dev_name, sent, HCI_MAX_NAME_LENGTH);
Johan Hedbergf51d5b22012-02-22 18:17:32 +0200233
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200234 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200235}
236
237static void hci_cc_read_local_name(struct hci_dev *hdev, struct sk_buff *skb)
238{
239 struct hci_rp_read_local_name *rp = (void *) skb->data;
240
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300241 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200242
243 if (rp->status)
244 return;
245
Johan Hedbergdb99b5f2012-02-22 20:14:22 +0200246 if (test_bit(HCI_SETUP, &hdev->dev_flags))
247 memcpy(hdev->dev_name, rp->name, HCI_MAX_NAME_LENGTH);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200248}
249
250static void hci_cc_write_auth_enable(struct hci_dev *hdev, struct sk_buff *skb)
251{
252 __u8 status = *((__u8 *) skb->data);
253 void *sent;
254
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300255 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200256
257 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_ENABLE);
258 if (!sent)
259 return;
260
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530261 hci_dev_lock(hdev);
262
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200263 if (!status) {
264 __u8 param = *((__u8 *) sent);
265
266 if (param == AUTH_ENABLED)
267 set_bit(HCI_AUTH, &hdev->flags);
268 else
269 clear_bit(HCI_AUTH, &hdev->flags);
270 }
271
Johan Hedberg33ef95e2012-02-16 23:56:27 +0200272 if (test_bit(HCI_MGMT, &hdev->dev_flags))
273 mgmt_auth_enable_complete(hdev, status);
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530274
275 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200276}
277
278static void hci_cc_write_encrypt_mode(struct hci_dev *hdev, struct sk_buff *skb)
279{
280 __u8 status = *((__u8 *) skb->data);
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200281 __u8 param;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200282 void *sent;
283
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300284 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200285
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200286 if (status)
287 return;
288
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200289 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_ENCRYPT_MODE);
290 if (!sent)
291 return;
292
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200293 param = *((__u8 *) sent);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200294
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200295 if (param)
296 set_bit(HCI_ENCRYPT, &hdev->flags);
297 else
298 clear_bit(HCI_ENCRYPT, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200299}
300
301static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
302{
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200303 __u8 status = *((__u8 *) skb->data);
304 __u8 param;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200305 void *sent;
306
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300307 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200308
309 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SCAN_ENABLE);
310 if (!sent)
311 return;
312
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200313 param = *((__u8 *) sent);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200314
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200315 hci_dev_lock(hdev);
316
Mikel Astizfa1bd912012-08-09 09:52:29 +0200317 if (status) {
Johan Hedberg2d7cee52011-11-07 22:16:03 +0200318 hdev->discov_timeout = 0;
319 goto done;
320 }
321
Johan Hedbergbc6d2d02014-07-10 12:09:08 +0300322 if (param & SCAN_INQUIRY)
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200323 set_bit(HCI_ISCAN, &hdev->flags);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +0300324 else
325 clear_bit(HCI_ISCAN, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200326
Johan Hedberg031547d2014-07-10 12:09:06 +0300327 if (param & SCAN_PAGE)
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200328 set_bit(HCI_PSCAN, &hdev->flags);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +0300329 else
Johan Hedberg204e3992014-07-28 15:45:31 +0300330 clear_bit(HCI_PSCAN, &hdev->flags);
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200331
332done:
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200333 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200334}
335
336static void hci_cc_read_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
337{
338 struct hci_rp_read_class_of_dev *rp = (void *) skb->data;
339
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300340 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200341
342 if (rp->status)
343 return;
344
345 memcpy(hdev->dev_class, rp->dev_class, 3);
346
347 BT_DBG("%s class 0x%.2x%.2x%.2x", hdev->name,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300348 hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200349}
350
351static void hci_cc_write_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
352{
353 __u8 status = *((__u8 *) skb->data);
354 void *sent;
355
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300356 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200357
358 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_CLASS_OF_DEV);
359 if (!sent)
360 return;
361
Marcel Holtmann7f9a9032012-02-22 18:38:01 +0100362 hci_dev_lock(hdev);
363
364 if (status == 0)
365 memcpy(hdev->dev_class, sent, 3);
366
367 if (test_bit(HCI_MGMT, &hdev->dev_flags))
368 mgmt_set_class_of_dev_complete(hdev, sent, status);
369
370 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200371}
372
373static void hci_cc_read_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
374{
375 struct hci_rp_read_voice_setting *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 __u16 setting;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200377
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300378 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200379
380 if (rp->status)
381 return;
382
383 setting = __le16_to_cpu(rp->voice_setting);
384
Marcel Holtmannf383f272008-07-14 20:13:47 +0200385 if (hdev->voice_setting == setting)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200386 return;
387
388 hdev->voice_setting = setting;
389
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300390 BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200391
Gustavo F. Padovan3c547112011-12-14 22:58:44 -0200392 if (hdev->notify)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200393 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200394}
395
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300396static void hci_cc_write_voice_setting(struct hci_dev *hdev,
397 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200398{
399 __u8 status = *((__u8 *) skb->data);
Marcel Holtmannf383f272008-07-14 20:13:47 +0200400 __u16 setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 void *sent;
402
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300403 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Marcel Holtmannf383f272008-07-14 20:13:47 +0200405 if (status)
406 return;
407
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200408 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_VOICE_SETTING);
409 if (!sent)
410 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Marcel Holtmannf383f272008-07-14 20:13:47 +0200412 setting = get_unaligned_le16(sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Marcel Holtmannf383f272008-07-14 20:13:47 +0200414 if (hdev->voice_setting == setting)
415 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Marcel Holtmannf383f272008-07-14 20:13:47 +0200417 hdev->voice_setting = setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300419 BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
Marcel Holtmannf383f272008-07-14 20:13:47 +0200420
Gustavo F. Padovan3c547112011-12-14 22:58:44 -0200421 if (hdev->notify)
Marcel Holtmannf383f272008-07-14 20:13:47 +0200422 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Marcel Holtmannb4cb9fb2013-10-14 13:56:16 -0700425static void hci_cc_read_num_supported_iac(struct hci_dev *hdev,
426 struct sk_buff *skb)
427{
428 struct hci_rp_read_num_supported_iac *rp = (void *) skb->data;
429
430 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
431
432 if (rp->status)
433 return;
434
435 hdev->num_iac = rp->num_iac;
436
437 BT_DBG("%s num iac %d", hdev->name, hdev->num_iac);
438}
439
Marcel Holtmann333140b2008-07-14 20:13:48 +0200440static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
441{
442 __u8 status = *((__u8 *) skb->data);
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300443 struct hci_cp_write_ssp_mode *sent;
Marcel Holtmann333140b2008-07-14 20:13:48 +0200444
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300445 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann333140b2008-07-14 20:13:48 +0200446
Marcel Holtmann333140b2008-07-14 20:13:48 +0200447 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_MODE);
448 if (!sent)
449 return;
450
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530451 hci_dev_lock(hdev);
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 }
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530468
469 hci_dev_unlock(hdev);
Marcel Holtmann333140b2008-07-14 20:13:48 +0200470}
471
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800472static void hci_cc_write_sc_support(struct hci_dev *hdev, struct sk_buff *skb)
473{
474 u8 status = *((u8 *) skb->data);
475 struct hci_cp_write_sc_support *sent;
476
477 BT_DBG("%s status 0x%2.2x", hdev->name, status);
478
479 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SC_SUPPORT);
480 if (!sent)
481 return;
482
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530483 hci_dev_lock(hdev);
484
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800485 if (!status) {
486 if (sent->support)
487 hdev->features[1][0] |= LMP_HOST_SC;
488 else
489 hdev->features[1][0] &= ~LMP_HOST_SC;
490 }
491
492 if (test_bit(HCI_MGMT, &hdev->dev_flags))
493 mgmt_sc_enable_complete(hdev, sent->support, status);
494 else if (!status) {
495 if (sent->support)
496 set_bit(HCI_SC_ENABLED, &hdev->dev_flags);
497 else
498 clear_bit(HCI_SC_ENABLED, &hdev->dev_flags);
499 }
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530500
501 hci_dev_unlock(hdev);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800502}
503
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200504static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
505{
506 struct hci_rp_read_local_version *rp = (void *) skb->data;
507
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300508 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200509
510 if (rp->status)
Johan Hedberg42c6b122013-03-05 20:37:49 +0200511 return;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200512
Marcel Holtmann0d5551f2013-10-18 12:04:50 -0700513 if (test_bit(HCI_SETUP, &hdev->dev_flags)) {
514 hdev->hci_ver = rp->hci_ver;
515 hdev->hci_rev = __le16_to_cpu(rp->hci_rev);
516 hdev->lmp_ver = rp->lmp_ver;
517 hdev->manufacturer = __le16_to_cpu(rp->manufacturer);
518 hdev->lmp_subver = __le16_to_cpu(rp->lmp_subver);
519 }
Johan Hedbergd5859e22011-01-25 01:19:58 +0200520}
521
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300522static void hci_cc_read_local_commands(struct hci_dev *hdev,
523 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200524{
525 struct hci_rp_read_local_commands *rp = (void *) skb->data;
526
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300527 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200528
Marcel Holtmann6a070e62013-10-31 04:54:33 -0700529 if (rp->status)
530 return;
531
532 if (test_bit(HCI_SETUP, &hdev->dev_flags))
Johan Hedberg2177bab2013-03-05 20:37:43 +0200533 memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200534}
535
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300536static void hci_cc_read_local_features(struct hci_dev *hdev,
537 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200538{
539 struct hci_rp_read_local_features *rp = (void *) skb->data;
540
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300541 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200542
543 if (rp->status)
544 return;
545
546 memcpy(hdev->features, rp->features, 8);
547
548 /* Adjust default settings according to features
549 * supported by device. */
550
Johan Hedbergcad718e2013-04-17 15:00:51 +0300551 if (hdev->features[0][0] & LMP_3SLOT)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200552 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
553
Johan Hedbergcad718e2013-04-17 15:00:51 +0300554 if (hdev->features[0][0] & LMP_5SLOT)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200555 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
556
Johan Hedbergcad718e2013-04-17 15:00:51 +0300557 if (hdev->features[0][1] & LMP_HV2) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200558 hdev->pkt_type |= (HCI_HV2);
559 hdev->esco_type |= (ESCO_HV2);
560 }
561
Johan Hedbergcad718e2013-04-17 15:00:51 +0300562 if (hdev->features[0][1] & LMP_HV3) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200563 hdev->pkt_type |= (HCI_HV3);
564 hdev->esco_type |= (ESCO_HV3);
565 }
566
Andre Guedes45db810f2012-07-24 15:03:49 -0300567 if (lmp_esco_capable(hdev))
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200568 hdev->esco_type |= (ESCO_EV3);
569
Johan Hedbergcad718e2013-04-17 15:00:51 +0300570 if (hdev->features[0][4] & LMP_EV4)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200571 hdev->esco_type |= (ESCO_EV4);
572
Johan Hedbergcad718e2013-04-17 15:00:51 +0300573 if (hdev->features[0][4] & LMP_EV5)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200574 hdev->esco_type |= (ESCO_EV5);
575
Johan Hedbergcad718e2013-04-17 15:00:51 +0300576 if (hdev->features[0][5] & LMP_EDR_ESCO_2M)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100577 hdev->esco_type |= (ESCO_2EV3);
578
Johan Hedbergcad718e2013-04-17 15:00:51 +0300579 if (hdev->features[0][5] & LMP_EDR_ESCO_3M)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100580 hdev->esco_type |= (ESCO_3EV3);
581
Johan Hedbergcad718e2013-04-17 15:00:51 +0300582 if (hdev->features[0][5] & LMP_EDR_3S_ESCO)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100583 hdev->esco_type |= (ESCO_2EV5 | ESCO_3EV5);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200584}
585
Andre Guedes971e3a42011-06-30 19:20:52 -0300586static void hci_cc_read_local_ext_features(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300587 struct sk_buff *skb)
Andre Guedes971e3a42011-06-30 19:20:52 -0300588{
589 struct hci_rp_read_local_ext_features *rp = (void *) skb->data;
590
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300591 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andre Guedes971e3a42011-06-30 19:20:52 -0300592
593 if (rp->status)
Johan Hedberg42c6b122013-03-05 20:37:49 +0200594 return;
Andre Guedes971e3a42011-06-30 19:20:52 -0300595
Marcel Holtmann57af75a2013-10-18 12:04:47 -0700596 if (hdev->max_page < rp->max_page)
597 hdev->max_page = rp->max_page;
Johan Hedbergd2c5d772013-04-17 15:00:52 +0300598
Johan Hedbergcad718e2013-04-17 15:00:51 +0300599 if (rp->page < HCI_MAX_PAGES)
600 memcpy(hdev->features[rp->page], rp->features, 8);
Andre Guedes971e3a42011-06-30 19:20:52 -0300601}
602
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200603static void hci_cc_read_flow_control_mode(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300604 struct sk_buff *skb)
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200605{
606 struct hci_rp_read_flow_control_mode *rp = (void *) skb->data;
607
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300608 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200609
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200610 if (rp->status)
611 return;
612
613 hdev->flow_ctl_mode = rp->mode;
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200614}
615
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200616static void hci_cc_read_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
617{
618 struct hci_rp_read_buffer_size *rp = (void *) skb->data;
619
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300620 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200621
622 if (rp->status)
623 return;
624
625 hdev->acl_mtu = __le16_to_cpu(rp->acl_mtu);
626 hdev->sco_mtu = rp->sco_mtu;
627 hdev->acl_pkts = __le16_to_cpu(rp->acl_max_pkt);
628 hdev->sco_pkts = __le16_to_cpu(rp->sco_max_pkt);
629
630 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
631 hdev->sco_mtu = 64;
632 hdev->sco_pkts = 8;
633 }
634
635 hdev->acl_cnt = hdev->acl_pkts;
636 hdev->sco_cnt = hdev->sco_pkts;
637
Gustavo Padovan807deac2012-05-17 00:36:24 -0300638 BT_DBG("%s acl mtu %d:%d sco mtu %d:%d", hdev->name, hdev->acl_mtu,
639 hdev->acl_pkts, hdev->sco_mtu, hdev->sco_pkts);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200640}
641
642static void hci_cc_read_bd_addr(struct hci_dev *hdev, struct sk_buff *skb)
643{
644 struct hci_rp_read_bd_addr *rp = (void *) skb->data;
645
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300646 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200647
Marcel Holtmanne30d3f52014-07-05 10:48:03 +0200648 if (rp->status)
649 return;
650
651 if (test_bit(HCI_INIT, &hdev->flags))
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200652 bacpy(&hdev->bdaddr, &rp->bdaddr);
Marcel Holtmanne30d3f52014-07-05 10:48:03 +0200653
654 if (test_bit(HCI_SETUP, &hdev->dev_flags))
655 bacpy(&hdev->setup_addr, &rp->bdaddr);
Johan Hedberg23bb5762010-12-21 23:01:27 +0200656}
657
Johan Hedbergf332ec62013-03-15 17:07:11 -0500658static void hci_cc_read_page_scan_activity(struct hci_dev *hdev,
659 struct sk_buff *skb)
660{
661 struct hci_rp_read_page_scan_activity *rp = (void *) skb->data;
662
663 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
664
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200665 if (rp->status)
666 return;
667
668 if (test_bit(HCI_INIT, &hdev->flags)) {
Johan Hedbergf332ec62013-03-15 17:07:11 -0500669 hdev->page_scan_interval = __le16_to_cpu(rp->interval);
670 hdev->page_scan_window = __le16_to_cpu(rp->window);
671 }
672}
673
Johan Hedberg4a3ee762013-03-15 17:07:12 -0500674static void hci_cc_write_page_scan_activity(struct hci_dev *hdev,
675 struct sk_buff *skb)
676{
677 u8 status = *((u8 *) skb->data);
678 struct hci_cp_write_page_scan_activity *sent;
679
680 BT_DBG("%s status 0x%2.2x", hdev->name, status);
681
682 if (status)
683 return;
684
685 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY);
686 if (!sent)
687 return;
688
689 hdev->page_scan_interval = __le16_to_cpu(sent->interval);
690 hdev->page_scan_window = __le16_to_cpu(sent->window);
691}
692
Johan Hedbergf332ec62013-03-15 17:07:11 -0500693static void hci_cc_read_page_scan_type(struct hci_dev *hdev,
694 struct sk_buff *skb)
695{
696 struct hci_rp_read_page_scan_type *rp = (void *) skb->data;
697
698 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
699
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200700 if (rp->status)
701 return;
702
703 if (test_bit(HCI_INIT, &hdev->flags))
Johan Hedbergf332ec62013-03-15 17:07:11 -0500704 hdev->page_scan_type = rp->type;
705}
706
Johan Hedberg4a3ee762013-03-15 17:07:12 -0500707static void hci_cc_write_page_scan_type(struct hci_dev *hdev,
708 struct sk_buff *skb)
709{
710 u8 status = *((u8 *) skb->data);
711 u8 *type;
712
713 BT_DBG("%s status 0x%2.2x", hdev->name, status);
714
715 if (status)
716 return;
717
718 type = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_TYPE);
719 if (type)
720 hdev->page_scan_type = *type;
721}
722
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200723static void hci_cc_read_data_block_size(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300724 struct sk_buff *skb)
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200725{
726 struct hci_rp_read_data_block_size *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 Emeltchenko350ee4c2011-12-07 15:56:51 +0200729
730 if (rp->status)
731 return;
732
733 hdev->block_mtu = __le16_to_cpu(rp->max_acl_len);
734 hdev->block_len = __le16_to_cpu(rp->block_len);
735 hdev->num_blocks = __le16_to_cpu(rp->num_blocks);
736
737 hdev->block_cnt = hdev->num_blocks;
738
739 BT_DBG("%s blk mtu %d cnt %d len %d", hdev->name, hdev->block_mtu,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300740 hdev->block_cnt, hdev->block_len);
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200741}
742
Johan Hedberg33f35722014-06-28 17:54:06 +0300743static void hci_cc_read_clock(struct hci_dev *hdev, struct sk_buff *skb)
744{
745 struct hci_rp_read_clock *rp = (void *) skb->data;
746 struct hci_cp_read_clock *cp;
747 struct hci_conn *conn;
748
749 BT_DBG("%s", hdev->name);
750
751 if (skb->len < sizeof(*rp))
752 return;
753
754 if (rp->status)
755 return;
756
757 hci_dev_lock(hdev);
758
759 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
760 if (!cp)
761 goto unlock;
762
763 if (cp->which == 0x00) {
764 hdev->clock = le32_to_cpu(rp->clock);
765 goto unlock;
766 }
767
768 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
769 if (conn) {
770 conn->clock = le32_to_cpu(rp->clock);
771 conn->clock_accuracy = le16_to_cpu(rp->accuracy);
772 }
773
774unlock:
775 hci_dev_unlock(hdev);
776}
777
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300778static void hci_cc_read_local_amp_info(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300779 struct sk_buff *skb)
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300780{
781 struct hci_rp_read_local_amp_info *rp = (void *) skb->data;
782
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300783 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300784
785 if (rp->status)
Andrei Emeltchenko8e2a0d92012-09-27 17:26:08 +0300786 goto a2mp_rsp;
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300787
788 hdev->amp_status = rp->amp_status;
789 hdev->amp_total_bw = __le32_to_cpu(rp->total_bw);
790 hdev->amp_max_bw = __le32_to_cpu(rp->max_bw);
791 hdev->amp_min_latency = __le32_to_cpu(rp->min_latency);
792 hdev->amp_max_pdu = __le32_to_cpu(rp->max_pdu);
793 hdev->amp_type = rp->amp_type;
794 hdev->amp_pal_cap = __le16_to_cpu(rp->pal_cap);
795 hdev->amp_assoc_size = __le16_to_cpu(rp->max_assoc_size);
796 hdev->amp_be_flush_to = __le32_to_cpu(rp->be_flush_to);
797 hdev->amp_max_flush_to = __le32_to_cpu(rp->max_flush_to);
798
Andrei Emeltchenko8e2a0d92012-09-27 17:26:08 +0300799a2mp_rsp:
800 a2mp_send_getinfo_rsp(hdev);
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300801}
802
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300803static void hci_cc_read_local_amp_assoc(struct hci_dev *hdev,
804 struct sk_buff *skb)
805{
806 struct hci_rp_read_local_amp_assoc *rp = (void *) skb->data;
807 struct amp_assoc *assoc = &hdev->loc_assoc;
808 size_t rem_len, frag_len;
809
810 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
811
812 if (rp->status)
813 goto a2mp_rsp;
814
815 frag_len = skb->len - sizeof(*rp);
816 rem_len = __le16_to_cpu(rp->rem_len);
817
818 if (rem_len > frag_len) {
Andrei Emeltchenko2e430be32012-09-28 14:44:23 +0300819 BT_DBG("frag_len %zu rem_len %zu", frag_len, rem_len);
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300820
821 memcpy(assoc->data + assoc->offset, rp->frag, frag_len);
822 assoc->offset += frag_len;
823
824 /* Read other fragments */
825 amp_read_loc_assoc_frag(hdev, rp->phy_handle);
826
827 return;
828 }
829
830 memcpy(assoc->data + assoc->offset, rp->frag, rem_len);
831 assoc->len = assoc->offset + rem_len;
832 assoc->offset = 0;
833
834a2mp_rsp:
835 /* Send A2MP Rsp when all fragments are received */
836 a2mp_send_getampassoc_rsp(hdev, rp->status);
Andrei Emeltchenko9495b2e2012-09-27 17:26:22 +0300837 a2mp_send_create_phy_link_req(hdev, rp->status);
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300838}
839
Johan Hedbergd5859e22011-01-25 01:19:58 +0200840static void hci_cc_read_inq_rsp_tx_power(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300841 struct sk_buff *skb)
Johan Hedbergd5859e22011-01-25 01:19:58 +0200842{
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -0700843 struct hci_rp_read_inq_rsp_tx_power *rp = (void *) skb->data;
Johan Hedbergd5859e22011-01-25 01:19:58 +0200844
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300845 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200846
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200847 if (rp->status)
848 return;
849
850 hdev->inq_tx_power = rp->tx_power;
Johan Hedbergd5859e22011-01-25 01:19:58 +0200851}
852
Johan Hedberg980e1a52011-01-22 06:10:07 +0200853static void hci_cc_pin_code_reply(struct hci_dev *hdev, struct sk_buff *skb)
854{
855 struct hci_rp_pin_code_reply *rp = (void *) skb->data;
856 struct hci_cp_pin_code_reply *cp;
857 struct hci_conn *conn;
858
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300859 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200860
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200861 hci_dev_lock(hdev);
862
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200863 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg744cf192011-11-08 20:40:14 +0200864 mgmt_pin_code_reply_complete(hdev, &rp->bdaddr, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200865
Mikel Astizfa1bd912012-08-09 09:52:29 +0200866 if (rp->status)
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200867 goto unlock;
Johan Hedberg980e1a52011-01-22 06:10:07 +0200868
869 cp = hci_sent_cmd_data(hdev, HCI_OP_PIN_CODE_REPLY);
870 if (!cp)
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200871 goto unlock;
Johan Hedberg980e1a52011-01-22 06:10:07 +0200872
873 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
874 if (conn)
875 conn->pin_length = cp->pin_len;
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200876
877unlock:
878 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200879}
880
881static void hci_cc_pin_code_neg_reply(struct hci_dev *hdev, struct sk_buff *skb)
882{
883 struct hci_rp_pin_code_neg_reply *rp = (void *) skb->data;
884
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300885 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200886
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200887 hci_dev_lock(hdev);
888
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200889 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg744cf192011-11-08 20:40:14 +0200890 mgmt_pin_code_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300891 rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200892
893 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200894}
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200895
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300896static void hci_cc_le_read_buffer_size(struct hci_dev *hdev,
897 struct sk_buff *skb)
898{
899 struct hci_rp_le_read_buffer_size *rp = (void *) skb->data;
900
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300901 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300902
903 if (rp->status)
904 return;
905
906 hdev->le_mtu = __le16_to_cpu(rp->le_mtu);
907 hdev->le_pkts = rp->le_max_pkt;
908
909 hdev->le_cnt = hdev->le_pkts;
910
911 BT_DBG("%s le mtu %d:%d", hdev->name, hdev->le_mtu, hdev->le_pkts);
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300912}
Johan Hedberg980e1a52011-01-22 06:10:07 +0200913
Johan Hedberg60e77322013-01-22 14:01:59 +0200914static void hci_cc_le_read_local_features(struct hci_dev *hdev,
915 struct sk_buff *skb)
916{
917 struct hci_rp_le_read_local_features *rp = (void *) skb->data;
918
919 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
920
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200921 if (rp->status)
922 return;
923
924 memcpy(hdev->le_features, rp->features, 8);
Johan Hedberg60e77322013-01-22 14:01:59 +0200925}
926
Johan Hedberg8fa19092012-10-19 20:57:49 +0300927static void hci_cc_le_read_adv_tx_power(struct hci_dev *hdev,
928 struct sk_buff *skb)
929{
930 struct hci_rp_le_read_adv_tx_power *rp = (void *) skb->data;
931
932 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
933
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200934 if (rp->status)
935 return;
936
937 hdev->adv_tx_power = rp->tx_power;
Johan Hedberg8fa19092012-10-19 20:57:49 +0300938}
939
Johan Hedberga5c29682011-02-19 12:05:57 -0300940static void hci_cc_user_confirm_reply(struct hci_dev *hdev, struct sk_buff *skb)
941{
942 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
943
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300944 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberga5c29682011-02-19 12:05:57 -0300945
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200946 hci_dev_lock(hdev);
947
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200948 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300949 mgmt_user_confirm_reply_complete(hdev, &rp->bdaddr, ACL_LINK, 0,
950 rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200951
952 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -0300953}
954
955static void hci_cc_user_confirm_neg_reply(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300956 struct sk_buff *skb)
Johan Hedberga5c29682011-02-19 12:05:57 -0300957{
958 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
959
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300960 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberga5c29682011-02-19 12:05:57 -0300961
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200962 hci_dev_lock(hdev);
963
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200964 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg744cf192011-11-08 20:40:14 +0200965 mgmt_user_confirm_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300966 ACL_LINK, 0, rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200967
968 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -0300969}
970
Brian Gix1143d452011-11-23 08:28:34 -0800971static void hci_cc_user_passkey_reply(struct hci_dev *hdev, struct sk_buff *skb)
972{
973 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
974
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300975 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -0800976
977 hci_dev_lock(hdev);
978
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200979 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg272d90d2012-02-09 15:26:12 +0200980 mgmt_user_passkey_reply_complete(hdev, &rp->bdaddr, ACL_LINK,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300981 0, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -0800982
983 hci_dev_unlock(hdev);
984}
985
986static void hci_cc_user_passkey_neg_reply(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300987 struct sk_buff *skb)
Brian Gix1143d452011-11-23 08:28:34 -0800988{
989 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
990
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300991 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -0800992
993 hci_dev_lock(hdev);
994
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200995 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Brian Gix1143d452011-11-23 08:28:34 -0800996 mgmt_user_passkey_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300997 ACL_LINK, 0, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -0800998
999 hci_dev_unlock(hdev);
1000}
1001
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08001002static void hci_cc_read_local_oob_data(struct hci_dev *hdev,
1003 struct sk_buff *skb)
Szymon Jancc35938b2011-03-22 13:12:21 +01001004{
1005 struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
1006
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001007 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Szymon Jancc35938b2011-03-22 13:12:21 +01001008
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001009 hci_dev_lock(hdev);
Johan Hedberg38da1702014-11-17 20:52:20 +02001010 mgmt_read_local_oob_data_complete(hdev, rp->hash, rp->rand, NULL, NULL,
1011 rp->status);
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08001012 hci_dev_unlock(hdev);
1013}
1014
1015static void hci_cc_read_local_oob_ext_data(struct hci_dev *hdev,
1016 struct sk_buff *skb)
1017{
1018 struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data;
1019
1020 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1021
1022 hci_dev_lock(hdev);
Johan Hedberg38da1702014-11-17 20:52:20 +02001023 mgmt_read_local_oob_data_complete(hdev, rp->hash192, rp->rand192,
1024 rp->hash256, rp->rand256,
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08001025 rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001026 hci_dev_unlock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01001027}
1028
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001029
1030static void hci_cc_le_set_random_addr(struct hci_dev *hdev, struct sk_buff *skb)
1031{
1032 __u8 status = *((__u8 *) skb->data);
1033 bdaddr_t *sent;
1034
1035 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1036
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001037 if (status)
1038 return;
1039
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001040 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_RANDOM_ADDR);
1041 if (!sent)
1042 return;
1043
1044 hci_dev_lock(hdev);
1045
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001046 bacpy(&hdev->random_addr, sent);
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001047
1048 hci_dev_unlock(hdev);
1049}
1050
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001051static void hci_cc_le_set_adv_enable(struct hci_dev *hdev, struct sk_buff *skb)
1052{
1053 __u8 *sent, status = *((__u8 *) skb->data);
1054
1055 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1056
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001057 if (status)
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001058 return;
1059
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001060 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_ENABLE);
1061 if (!sent)
Johan Hedberg3c857752014-03-25 10:30:49 +02001062 return;
1063
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001064 hci_dev_lock(hdev);
1065
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001066 /* If we're doing connection initiation as peripheral. Set a
Johan Hedberg3c857752014-03-25 10:30:49 +02001067 * timeout in case something goes wrong.
1068 */
1069 if (*sent) {
1070 struct hci_conn *conn;
1071
Johan Hedberg66c417c2014-07-08 15:07:47 +03001072 set_bit(HCI_LE_ADV, &hdev->dev_flags);
1073
Johan Hedberg3c857752014-03-25 10:30:49 +02001074 conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
1075 if (conn)
1076 queue_delayed_work(hdev->workqueue,
1077 &conn->le_conn_timeout,
Johan Hedberg09ae2602014-07-06 13:41:15 +03001078 conn->conn_timeout);
Johan Hedberg66c417c2014-07-08 15:07:47 +03001079 } else {
1080 clear_bit(HCI_LE_ADV, &hdev->dev_flags);
Johan Hedberg3c857752014-03-25 10:30:49 +02001081 }
1082
Johan Hedberg04b4edc2013-03-15 17:07:01 -05001083 hci_dev_unlock(hdev);
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001084}
1085
Marcel Holtmann533553f2014-03-21 12:18:10 -07001086static void hci_cc_le_set_scan_param(struct hci_dev *hdev, struct sk_buff *skb)
1087{
1088 struct hci_cp_le_set_scan_param *cp;
1089 __u8 status = *((__u8 *) skb->data);
1090
1091 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1092
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001093 if (status)
1094 return;
1095
Marcel Holtmann533553f2014-03-21 12:18:10 -07001096 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_PARAM);
1097 if (!cp)
1098 return;
1099
1100 hci_dev_lock(hdev);
1101
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001102 hdev->le_scan_type = cp->type;
Marcel Holtmann533553f2014-03-21 12:18:10 -07001103
1104 hci_dev_unlock(hdev);
1105}
1106
Johan Hedbergb9a63282014-03-25 10:51:52 +02001107static bool has_pending_adv_report(struct hci_dev *hdev)
1108{
1109 struct discovery_state *d = &hdev->discovery;
1110
1111 return bacmp(&d->last_adv_addr, BDADDR_ANY);
1112}
1113
1114static void clear_pending_adv_report(struct hci_dev *hdev)
1115{
1116 struct discovery_state *d = &hdev->discovery;
1117
1118 bacpy(&d->last_adv_addr, BDADDR_ANY);
1119 d->last_adv_data_len = 0;
1120}
1121
1122static void store_pending_adv_report(struct hci_dev *hdev, bdaddr_t *bdaddr,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02001123 u8 bdaddr_type, s8 rssi, u32 flags,
1124 u8 *data, u8 len)
Johan Hedbergb9a63282014-03-25 10:51:52 +02001125{
1126 struct discovery_state *d = &hdev->discovery;
1127
1128 bacpy(&d->last_adv_addr, bdaddr);
1129 d->last_adv_addr_type = bdaddr_type;
Johan Hedbergff5cd292014-03-25 14:40:52 +02001130 d->last_adv_rssi = rssi;
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02001131 d->last_adv_flags = flags;
Johan Hedbergb9a63282014-03-25 10:51:52 +02001132 memcpy(d->last_adv_data, data, len);
1133 d->last_adv_data_len = len;
1134}
1135
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001136static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001137 struct sk_buff *skb)
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001138{
1139 struct hci_cp_le_set_scan_enable *cp;
1140 __u8 status = *((__u8 *) skb->data);
1141
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001142 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001143
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001144 if (status)
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001145 return;
1146
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001147 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_ENABLE);
1148 if (!cp)
Andre Guedes3fd319b2013-04-30 15:29:36 -03001149 return;
1150
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +05301151 hci_dev_lock(hdev);
1152
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001153 switch (cp->enable) {
Andre Guedes76a388be2013-04-04 20:21:02 -03001154 case LE_SCAN_ENABLE:
Andre Guedesd23264a2011-11-25 20:53:38 -03001155 set_bit(HCI_LE_SCAN, &hdev->dev_flags);
Johan Hedbergb9a63282014-03-25 10:51:52 +02001156 if (hdev->le_scan_type == LE_SCAN_ACTIVE)
1157 clear_pending_adv_report(hdev);
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001158 break;
1159
Andre Guedes76a388be2013-04-04 20:21:02 -03001160 case LE_SCAN_DISABLE:
Johan Hedbergb9a63282014-03-25 10:51:52 +02001161 /* We do this here instead of when setting DISCOVERY_STOPPED
1162 * since the latter would potentially require waiting for
1163 * inquiry to stop too.
1164 */
1165 if (has_pending_adv_report(hdev)) {
1166 struct discovery_state *d = &hdev->discovery;
1167
1168 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Johan Hedbergab0aa432014-03-26 14:17:12 +02001169 d->last_adv_addr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02001170 d->last_adv_rssi, d->last_adv_flags,
Johan Hedbergab0aa432014-03-26 14:17:12 +02001171 d->last_adv_data,
Johan Hedbergb9a63282014-03-25 10:51:52 +02001172 d->last_adv_data_len, NULL, 0);
1173 }
1174
Johan Hedberg317ac8c2014-02-28 20:26:12 +02001175 /* Cancel this timer so that we don't try to disable scanning
1176 * when it's already disabled.
1177 */
1178 cancel_delayed_work(&hdev->le_scan_disable);
1179
Andre Guedesd23264a2011-11-25 20:53:38 -03001180 clear_bit(HCI_LE_SCAN, &hdev->dev_flags);
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001181
Johan Hedberg81ad6fd2014-02-28 20:26:13 +02001182 /* The HCI_LE_SCAN_INTERRUPTED flag indicates that we
1183 * interrupted scanning due to a connect request. Mark
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001184 * therefore discovery as stopped. If this was not
1185 * because of a connect request advertising might have
1186 * been disabled because of active scanning, so
1187 * re-enable it again if necessary.
Johan Hedberg81ad6fd2014-02-28 20:26:13 +02001188 */
1189 if (test_and_clear_bit(HCI_LE_SCAN_INTERRUPTED,
1190 &hdev->dev_flags))
1191 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001192 else if (!test_bit(HCI_LE_ADV, &hdev->dev_flags) &&
Johan Hedberg34722272014-07-08 16:05:05 +03001193 hdev->discovery.state == DISCOVERY_FINDING)
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001194 mgmt_reenable_advertising(hdev);
1195
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001196 break;
1197
1198 default:
1199 BT_ERR("Used reserved LE_Scan_Enable param %d", cp->enable);
1200 break;
Andre Guedes35815082011-05-26 16:23:53 -03001201 }
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +05301202
1203 hci_dev_unlock(hdev);
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001204}
1205
Johan Hedbergcf1d0812013-01-22 14:02:00 +02001206static void hci_cc_le_read_white_list_size(struct hci_dev *hdev,
1207 struct sk_buff *skb)
1208{
1209 struct hci_rp_le_read_white_list_size *rp = (void *) skb->data;
1210
1211 BT_DBG("%s status 0x%2.2x size %u", hdev->name, rp->status, rp->size);
1212
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001213 if (rp->status)
1214 return;
1215
1216 hdev->le_white_list_size = rp->size;
Johan Hedbergcf1d0812013-01-22 14:02:00 +02001217}
1218
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001219static void hci_cc_le_clear_white_list(struct hci_dev *hdev,
1220 struct sk_buff *skb)
1221{
1222 __u8 status = *((__u8 *) skb->data);
1223
1224 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1225
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001226 if (status)
1227 return;
1228
Johan Hedbergdcc36c12014-07-09 12:59:13 +03001229 hci_bdaddr_list_clear(&hdev->le_white_list);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001230}
1231
1232static void hci_cc_le_add_to_white_list(struct hci_dev *hdev,
1233 struct sk_buff *skb)
1234{
1235 struct hci_cp_le_add_to_white_list *sent;
1236 __u8 status = *((__u8 *) skb->data);
1237
1238 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1239
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001240 if (status)
1241 return;
1242
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001243 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_WHITE_LIST);
1244 if (!sent)
1245 return;
1246
Johan Hedbergdcc36c12014-07-09 12:59:13 +03001247 hci_bdaddr_list_add(&hdev->le_white_list, &sent->bdaddr,
1248 sent->bdaddr_type);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001249}
1250
1251static void hci_cc_le_del_from_white_list(struct hci_dev *hdev,
1252 struct sk_buff *skb)
1253{
1254 struct hci_cp_le_del_from_white_list *sent;
1255 __u8 status = *((__u8 *) skb->data);
1256
1257 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1258
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001259 if (status)
1260 return;
1261
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001262 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_DEL_FROM_WHITE_LIST);
1263 if (!sent)
1264 return;
1265
Johan Hedbergdcc36c12014-07-09 12:59:13 +03001266 hci_bdaddr_list_del(&hdev->le_white_list, &sent->bdaddr,
1267 sent->bdaddr_type);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001268}
1269
Johan Hedberg9b008c02013-01-22 14:02:01 +02001270static void hci_cc_le_read_supported_states(struct hci_dev *hdev,
1271 struct sk_buff *skb)
1272{
1273 struct hci_rp_le_read_supported_states *rp = (void *) skb->data;
1274
1275 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1276
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001277 if (rp->status)
1278 return;
1279
1280 memcpy(hdev->le_states, rp->le_states, 8);
Johan Hedberg9b008c02013-01-22 14:02:01 +02001281}
1282
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01001283static void hci_cc_le_read_def_data_len(struct hci_dev *hdev,
1284 struct sk_buff *skb)
1285{
1286 struct hci_rp_le_read_def_data_len *rp = (void *) skb->data;
1287
1288 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1289
1290 if (rp->status)
1291 return;
1292
1293 hdev->le_def_tx_len = le16_to_cpu(rp->tx_len);
1294 hdev->le_def_tx_time = le16_to_cpu(rp->tx_time);
1295}
1296
1297static void hci_cc_le_write_def_data_len(struct hci_dev *hdev,
1298 struct sk_buff *skb)
1299{
1300 struct hci_cp_le_write_def_data_len *sent;
1301 __u8 status = *((__u8 *) skb->data);
1302
1303 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1304
1305 if (status)
1306 return;
1307
1308 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN);
1309 if (!sent)
1310 return;
1311
1312 hdev->le_def_tx_len = le16_to_cpu(sent->tx_len);
1313 hdev->le_def_tx_time = le16_to_cpu(sent->tx_time);
1314}
1315
1316static void hci_cc_le_read_max_data_len(struct hci_dev *hdev,
1317 struct sk_buff *skb)
1318{
1319 struct hci_rp_le_read_max_data_len *rp = (void *) skb->data;
1320
1321 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1322
1323 if (rp->status)
1324 return;
1325
1326 hdev->le_max_tx_len = le16_to_cpu(rp->tx_len);
1327 hdev->le_max_tx_time = le16_to_cpu(rp->tx_time);
1328 hdev->le_max_rx_len = le16_to_cpu(rp->rx_len);
1329 hdev->le_max_rx_time = le16_to_cpu(rp->rx_time);
1330}
1331
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001332static void hci_cc_write_le_host_supported(struct hci_dev *hdev,
1333 struct sk_buff *skb)
Andre Guedesf9b49302011-06-30 19:20:53 -03001334{
Johan Hedberg06199cf2012-02-22 16:37:11 +02001335 struct hci_cp_write_le_host_supported *sent;
Andre Guedesf9b49302011-06-30 19:20:53 -03001336 __u8 status = *((__u8 *) skb->data);
1337
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001338 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andre Guedesf9b49302011-06-30 19:20:53 -03001339
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001340 if (status)
1341 return;
1342
Johan Hedberg06199cf2012-02-22 16:37:11 +02001343 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED);
Johan Hedberg8f984df2012-02-28 01:07:22 +02001344 if (!sent)
Andre Guedesf9b49302011-06-30 19:20:53 -03001345 return;
1346
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +05301347 hci_dev_lock(hdev);
1348
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001349 if (sent->le) {
1350 hdev->features[1][0] |= LMP_HOST_LE;
1351 set_bit(HCI_LE_ENABLED, &hdev->dev_flags);
1352 } else {
1353 hdev->features[1][0] &= ~LMP_HOST_LE;
1354 clear_bit(HCI_LE_ENABLED, &hdev->dev_flags);
1355 clear_bit(HCI_ADVERTISING, &hdev->dev_flags);
Johan Hedberg8f984df2012-02-28 01:07:22 +02001356 }
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001357
1358 if (sent->simul)
1359 hdev->features[1][0] |= LMP_HOST_LE_BREDR;
1360 else
1361 hdev->features[1][0] &= ~LMP_HOST_LE_BREDR;
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +05301362
1363 hci_dev_unlock(hdev);
Andre Guedesf9b49302011-06-30 19:20:53 -03001364}
1365
Johan Hedberg56ed2cb2014-02-27 14:05:40 +02001366static void hci_cc_set_adv_param(struct hci_dev *hdev, struct sk_buff *skb)
1367{
1368 struct hci_cp_le_set_adv_param *cp;
1369 u8 status = *((u8 *) skb->data);
1370
1371 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1372
1373 if (status)
1374 return;
1375
1376 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_PARAM);
1377 if (!cp)
1378 return;
1379
1380 hci_dev_lock(hdev);
1381 hdev->adv_addr_type = cp->own_address_type;
1382 hci_dev_unlock(hdev);
1383}
1384
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +03001385static void hci_cc_write_remote_amp_assoc(struct hci_dev *hdev,
1386 struct sk_buff *skb)
1387{
1388 struct hci_rp_write_remote_amp_assoc *rp = (void *) skb->data;
1389
1390 BT_DBG("%s status 0x%2.2x phy_handle 0x%2.2x",
1391 hdev->name, rp->status, rp->phy_handle);
1392
1393 if (rp->status)
1394 return;
1395
1396 amp_write_rem_assoc_continue(hdev, rp->phy_handle);
1397}
1398
Andrzej Kaczmarek5ae76a92014-05-08 15:32:08 +02001399static void hci_cc_read_rssi(struct hci_dev *hdev, struct sk_buff *skb)
1400{
1401 struct hci_rp_read_rssi *rp = (void *) skb->data;
1402 struct hci_conn *conn;
1403
1404 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1405
1406 if (rp->status)
1407 return;
1408
1409 hci_dev_lock(hdev);
1410
1411 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
1412 if (conn)
1413 conn->rssi = rp->rssi;
1414
1415 hci_dev_unlock(hdev);
1416}
1417
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02001418static void hci_cc_read_tx_power(struct hci_dev *hdev, struct sk_buff *skb)
1419{
1420 struct hci_cp_read_tx_power *sent;
1421 struct hci_rp_read_tx_power *rp = (void *) skb->data;
1422 struct hci_conn *conn;
1423
1424 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1425
1426 if (rp->status)
1427 return;
1428
1429 sent = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
1430 if (!sent)
1431 return;
1432
1433 hci_dev_lock(hdev);
1434
1435 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Andrzej Kaczmarekd0455ed2014-05-14 13:43:05 +02001436 if (!conn)
1437 goto unlock;
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02001438
Andrzej Kaczmarekd0455ed2014-05-14 13:43:05 +02001439 switch (sent->type) {
1440 case 0x00:
1441 conn->tx_power = rp->tx_power;
1442 break;
1443 case 0x01:
1444 conn->max_tx_power = rp->tx_power;
1445 break;
1446 }
1447
1448unlock:
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02001449 hci_dev_unlock(hdev);
1450}
1451
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001452static void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001453{
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001454 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001455
1456 if (status) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001457 hci_conn_check_pending(hdev);
Johan Hedberg314b2382011-04-27 10:29:57 -04001458 return;
1459 }
1460
Andre Guedes89352e72011-11-04 14:16:53 -03001461 set_bit(HCI_INQUIRY, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001462}
1463
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001464static void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001466 struct hci_cp_create_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001469 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001470
1471 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_CONN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (!cp)
1473 return;
1474
1475 hci_dev_lock(hdev);
1476
1477 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
1478
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03001479 BT_DBG("%s bdaddr %pMR hcon %p", hdev->name, &cp->bdaddr, conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 if (status) {
1482 if (conn && conn->state == BT_CONNECT) {
Marcel Holtmann4c67bc72006-10-15 17:30:56 +02001483 if (status != 0x0c || conn->attempt > 2) {
1484 conn->state = BT_CLOSED;
1485 hci_proto_connect_cfm(conn, status);
1486 hci_conn_del(conn);
1487 } else
1488 conn->state = BT_CONNECT2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
1490 } else {
1491 if (!conn) {
Johan Hedberga5c4e302014-07-16 11:56:07 +03001492 conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr,
1493 HCI_ROLE_MASTER);
1494 if (!conn)
Gustavo F. Padovan893ef972010-07-18 15:13:37 -03001495 BT_ERR("No memory for new connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 }
1497 }
1498
1499 hci_dev_unlock(hdev);
1500}
1501
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001502static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001504 struct hci_cp_add_sco *cp;
1505 struct hci_conn *acl, *sco;
1506 __u16 handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001508 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001509
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001510 if (!status)
1511 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001513 cp = hci_sent_cmd_data(hdev, HCI_OP_ADD_SCO);
1514 if (!cp)
1515 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001517 handle = __le16_to_cpu(cp->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001519 BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
Marcel Holtmann6bd57412006-11-18 22:14:22 +01001520
1521 hci_dev_lock(hdev);
1522
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001523 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001524 if (acl) {
1525 sco = acl->link;
1526 if (sco) {
1527 sco->state = BT_CLOSED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001528
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001529 hci_proto_connect_cfm(sco, status);
1530 hci_conn_del(sco);
1531 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001532 }
Marcel Holtmann6bd57412006-11-18 22:14:22 +01001533
1534 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
Marcel Holtmannf8558552008-07-14 20:13:49 +02001537static void hci_cs_auth_requested(struct hci_dev *hdev, __u8 status)
1538{
1539 struct hci_cp_auth_requested *cp;
1540 struct hci_conn *conn;
1541
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001542 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001543
1544 if (!status)
1545 return;
1546
1547 cp = hci_sent_cmd_data(hdev, HCI_OP_AUTH_REQUESTED);
1548 if (!cp)
1549 return;
1550
1551 hci_dev_lock(hdev);
1552
1553 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1554 if (conn) {
1555 if (conn->state == BT_CONFIG) {
1556 hci_proto_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02001557 hci_conn_drop(conn);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001558 }
1559 }
1560
1561 hci_dev_unlock(hdev);
1562}
1563
1564static void hci_cs_set_conn_encrypt(struct hci_dev *hdev, __u8 status)
1565{
1566 struct hci_cp_set_conn_encrypt *cp;
1567 struct hci_conn *conn;
1568
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001569 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001570
1571 if (!status)
1572 return;
1573
1574 cp = hci_sent_cmd_data(hdev, HCI_OP_SET_CONN_ENCRYPT);
1575 if (!cp)
1576 return;
1577
1578 hci_dev_lock(hdev);
1579
1580 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1581 if (conn) {
1582 if (conn->state == BT_CONFIG) {
1583 hci_proto_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02001584 hci_conn_drop(conn);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001585 }
1586 }
1587
1588 hci_dev_unlock(hdev);
1589}
1590
Johan Hedberg127178d2010-11-18 22:22:29 +02001591static int hci_outgoing_auth_needed(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001592 struct hci_conn *conn)
Johan Hedberg392599b2010-11-18 22:22:28 +02001593{
Johan Hedberg392599b2010-11-18 22:22:28 +02001594 if (conn->state != BT_CONFIG || !conn->out)
1595 return 0;
1596
Johan Hedberg765c2a92011-01-19 12:06:52 +05301597 if (conn->pending_sec_level == BT_SECURITY_SDP)
Johan Hedberg392599b2010-11-18 22:22:28 +02001598 return 0;
1599
1600 /* Only request authentication for SSP connections or non-SSP
Johan Hedberg264b8b42014-01-08 16:40:39 +02001601 * devices with sec_level MEDIUM or HIGH or if MITM protection
1602 * is requested.
1603 */
Gustavo Padovan807deac2012-05-17 00:36:24 -03001604 if (!hci_conn_ssp_enabled(conn) && !(conn->auth_type & 0x01) &&
Johan Hedberg7e3691e2014-05-30 14:45:19 +03001605 conn->pending_sec_level != BT_SECURITY_FIPS &&
Johan Hedberg264b8b42014-01-08 16:40:39 +02001606 conn->pending_sec_level != BT_SECURITY_HIGH &&
1607 conn->pending_sec_level != BT_SECURITY_MEDIUM)
Johan Hedberg392599b2010-11-18 22:22:28 +02001608 return 0;
1609
Johan Hedberg392599b2010-11-18 22:22:28 +02001610 return 1;
1611}
1612
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001613static int hci_resolve_name(struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001614 struct inquiry_entry *e)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001615{
1616 struct hci_cp_remote_name_req cp;
1617
1618 memset(&cp, 0, sizeof(cp));
1619
1620 bacpy(&cp.bdaddr, &e->data.bdaddr);
1621 cp.pscan_rep_mode = e->data.pscan_rep_mode;
1622 cp.pscan_mode = e->data.pscan_mode;
1623 cp.clock_offset = e->data.clock_offset;
1624
1625 return hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
1626}
1627
Johan Hedbergb644ba32012-01-17 21:48:47 +02001628static bool hci_resolve_next_name(struct hci_dev *hdev)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001629{
1630 struct discovery_state *discov = &hdev->discovery;
1631 struct inquiry_entry *e;
1632
Johan Hedbergb644ba32012-01-17 21:48:47 +02001633 if (list_empty(&discov->resolve))
1634 return false;
1635
1636 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
Ram Malovanyc8100892012-07-19 10:26:09 +03001637 if (!e)
1638 return false;
1639
Johan Hedbergb644ba32012-01-17 21:48:47 +02001640 if (hci_resolve_name(hdev, e) == 0) {
1641 e->name_state = NAME_PENDING;
1642 return true;
1643 }
1644
1645 return false;
1646}
1647
1648static void hci_check_pending_name(struct hci_dev *hdev, struct hci_conn *conn,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001649 bdaddr_t *bdaddr, u8 *name, u8 name_len)
Johan Hedbergb644ba32012-01-17 21:48:47 +02001650{
1651 struct discovery_state *discov = &hdev->discovery;
1652 struct inquiry_entry *e;
1653
Johan Hedberg60cb49d2014-11-11 11:33:24 +02001654 /* Update the mgmt connected state if necessary. Be careful with
1655 * conn objects that exist but are not (yet) connected however.
1656 * Only those in BT_CONFIG or BT_CONNECTED states can be
1657 * considered connected.
1658 */
1659 if (conn &&
1660 (conn->state == BT_CONFIG || conn->state == BT_CONNECTED) &&
Jaganath Kanakkasserycb77c3e2014-11-07 16:39:09 +05301661 !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00001662 mgmt_device_connected(hdev, conn, 0, name, name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02001663
1664 if (discov->state == DISCOVERY_STOPPED)
1665 return;
1666
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001667 if (discov->state == DISCOVERY_STOPPING)
1668 goto discov_complete;
1669
1670 if (discov->state != DISCOVERY_RESOLVING)
1671 return;
1672
1673 e = hci_inquiry_cache_lookup_resolve(hdev, bdaddr, NAME_PENDING);
Ram Malovany7cc83802012-07-19 10:26:10 +03001674 /* If the device was not found in a list of found devices names of which
1675 * are pending. there is no need to continue resolving a next name as it
1676 * will be done upon receiving another Remote Name Request Complete
1677 * Event */
1678 if (!e)
1679 return;
1680
1681 list_del(&e->list);
1682 if (name) {
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001683 e->name_state = NAME_KNOWN;
Ram Malovany7cc83802012-07-19 10:26:10 +03001684 mgmt_remote_name(hdev, bdaddr, ACL_LINK, 0x00,
1685 e->data.rssi, name, name_len);
Ram Malovanyc3e7c0d2012-07-19 10:26:11 +03001686 } else {
1687 e->name_state = NAME_NOT_KNOWN;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001688 }
1689
Johan Hedbergb644ba32012-01-17 21:48:47 +02001690 if (hci_resolve_next_name(hdev))
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001691 return;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001692
1693discov_complete:
1694 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1695}
1696
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001697static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
1698{
Johan Hedberg127178d2010-11-18 22:22:29 +02001699 struct hci_cp_remote_name_req *cp;
1700 struct hci_conn *conn;
1701
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001702 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Johan Hedberg127178d2010-11-18 22:22:29 +02001703
1704 /* If successful wait for the name req complete event before
1705 * checking for the need to do authentication */
1706 if (!status)
1707 return;
1708
1709 cp = hci_sent_cmd_data(hdev, HCI_OP_REMOTE_NAME_REQ);
1710 if (!cp)
1711 return;
1712
1713 hci_dev_lock(hdev);
1714
1715 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
Johan Hedbergb644ba32012-01-17 21:48:47 +02001716
1717 if (test_bit(HCI_MGMT, &hdev->dev_flags))
1718 hci_check_pending_name(hdev, conn, &cp->bdaddr, NULL, 0);
1719
Johan Hedberg79c6c702011-04-28 11:28:55 -07001720 if (!conn)
1721 goto unlock;
1722
1723 if (!hci_outgoing_auth_needed(hdev, conn))
1724 goto unlock;
1725
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001726 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
Johannes Bergc1f23a22013-10-07 18:19:16 +02001727 struct hci_cp_auth_requested auth_cp;
1728
Johan Hedberg977f8fc2014-07-17 15:35:39 +03001729 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
1730
Johannes Bergc1f23a22013-10-07 18:19:16 +02001731 auth_cp.handle = __cpu_to_le16(conn->handle);
1732 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED,
1733 sizeof(auth_cp), &auth_cp);
Johan Hedberg127178d2010-11-18 22:22:29 +02001734 }
1735
Johan Hedberg79c6c702011-04-28 11:28:55 -07001736unlock:
Johan Hedberg127178d2010-11-18 22:22:29 +02001737 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001738}
1739
Marcel Holtmann769be972008-07-14 20:13:49 +02001740static void hci_cs_read_remote_features(struct hci_dev *hdev, __u8 status)
1741{
1742 struct hci_cp_read_remote_features *cp;
1743 struct hci_conn *conn;
1744
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001745 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann769be972008-07-14 20:13:49 +02001746
1747 if (!status)
1748 return;
1749
1750 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_FEATURES);
1751 if (!cp)
1752 return;
1753
1754 hci_dev_lock(hdev);
1755
1756 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1757 if (conn) {
1758 if (conn->state == BT_CONFIG) {
Marcel Holtmann769be972008-07-14 20:13:49 +02001759 hci_proto_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02001760 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02001761 }
1762 }
1763
1764 hci_dev_unlock(hdev);
1765}
1766
1767static void hci_cs_read_remote_ext_features(struct hci_dev *hdev, __u8 status)
1768{
1769 struct hci_cp_read_remote_ext_features *cp;
1770 struct hci_conn *conn;
1771
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001772 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann769be972008-07-14 20:13:49 +02001773
1774 if (!status)
1775 return;
1776
1777 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES);
1778 if (!cp)
1779 return;
1780
1781 hci_dev_lock(hdev);
1782
1783 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1784 if (conn) {
1785 if (conn->state == BT_CONFIG) {
Marcel Holtmann769be972008-07-14 20:13:49 +02001786 hci_proto_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02001787 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02001788 }
1789 }
1790
1791 hci_dev_unlock(hdev);
1792}
1793
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001794static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
1795{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001796 struct hci_cp_setup_sync_conn *cp;
1797 struct hci_conn *acl, *sco;
1798 __u16 handle;
1799
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001800 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001801
1802 if (!status)
1803 return;
1804
1805 cp = hci_sent_cmd_data(hdev, HCI_OP_SETUP_SYNC_CONN);
1806 if (!cp)
1807 return;
1808
1809 handle = __le16_to_cpu(cp->handle);
1810
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001811 BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001812
1813 hci_dev_lock(hdev);
1814
1815 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001816 if (acl) {
1817 sco = acl->link;
1818 if (sco) {
1819 sco->state = BT_CLOSED;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001820
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001821 hci_proto_connect_cfm(sco, status);
1822 hci_conn_del(sco);
1823 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001824 }
1825
1826 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001827}
1828
1829static void hci_cs_sniff_mode(struct hci_dev *hdev, __u8 status)
1830{
1831 struct hci_cp_sniff_mode *cp;
1832 struct hci_conn *conn;
1833
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001834 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001835
1836 if (!status)
1837 return;
1838
1839 cp = hci_sent_cmd_data(hdev, HCI_OP_SNIFF_MODE);
1840 if (!cp)
1841 return;
1842
1843 hci_dev_lock(hdev);
1844
1845 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001846 if (conn) {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001847 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001848
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001849 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001850 hci_sco_setup(conn, status);
1851 }
1852
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001853 hci_dev_unlock(hdev);
1854}
1855
1856static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
1857{
1858 struct hci_cp_exit_sniff_mode *cp;
1859 struct hci_conn *conn;
1860
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001861 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001862
1863 if (!status)
1864 return;
1865
1866 cp = hci_sent_cmd_data(hdev, HCI_OP_EXIT_SNIFF_MODE);
1867 if (!cp)
1868 return;
1869
1870 hci_dev_lock(hdev);
1871
1872 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001873 if (conn) {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001874 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001875
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001876 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001877 hci_sco_setup(conn, status);
1878 }
1879
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001880 hci_dev_unlock(hdev);
1881}
1882
Johan Hedberg88c3df12012-02-09 14:27:38 +02001883static void hci_cs_disconnect(struct hci_dev *hdev, u8 status)
1884{
1885 struct hci_cp_disconnect *cp;
1886 struct hci_conn *conn;
1887
1888 if (!status)
1889 return;
1890
1891 cp = hci_sent_cmd_data(hdev, HCI_OP_DISCONNECT);
1892 if (!cp)
1893 return;
1894
1895 hci_dev_lock(hdev);
1896
1897 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1898 if (conn)
1899 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001900 conn->dst_type, status);
Johan Hedberg88c3df12012-02-09 14:27:38 +02001901
1902 hci_dev_unlock(hdev);
1903}
1904
Andrei Emeltchenkoa02226d2012-09-27 17:26:19 +03001905static void hci_cs_create_phylink(struct hci_dev *hdev, u8 status)
1906{
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +03001907 struct hci_cp_create_phy_link *cp;
1908
Andrei Emeltchenkoa02226d2012-09-27 17:26:19 +03001909 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +03001910
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +03001911 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_PHY_LINK);
1912 if (!cp)
1913 return;
1914
Andrei Emeltchenkoe58917b2012-10-31 15:46:33 +02001915 hci_dev_lock(hdev);
1916
1917 if (status) {
1918 struct hci_conn *hcon;
1919
1920 hcon = hci_conn_hash_lookup_handle(hdev, cp->phy_handle);
1921 if (hcon)
1922 hci_conn_del(hcon);
1923 } else {
1924 amp_write_remote_assoc(hdev, cp->phy_handle);
1925 }
1926
1927 hci_dev_unlock(hdev);
Andrei Emeltchenkoa02226d2012-09-27 17:26:19 +03001928}
1929
Andrei Emeltchenko0b26ab92012-09-27 17:26:24 +03001930static void hci_cs_accept_phylink(struct hci_dev *hdev, u8 status)
1931{
1932 struct hci_cp_accept_phy_link *cp;
1933
1934 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1935
1936 if (status)
1937 return;
1938
1939 cp = hci_sent_cmd_data(hdev, HCI_OP_ACCEPT_PHY_LINK);
1940 if (!cp)
1941 return;
1942
1943 amp_write_remote_assoc(hdev, cp->phy_handle);
1944}
1945
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02001946static void hci_cs_le_create_conn(struct hci_dev *hdev, u8 status)
1947{
1948 struct hci_cp_le_create_conn *cp;
1949 struct hci_conn *conn;
1950
1951 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1952
1953 /* All connection failure handling is taken care of by the
1954 * hci_le_conn_failed function which is triggered by the HCI
1955 * request completion callbacks used for connecting.
1956 */
1957 if (status)
1958 return;
1959
1960 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_CREATE_CONN);
1961 if (!cp)
1962 return;
1963
1964 hci_dev_lock(hdev);
1965
1966 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->peer_addr);
1967 if (!conn)
1968 goto unlock;
1969
1970 /* Store the initiator and responder address information which
1971 * is needed for SMP. These values will not change during the
1972 * lifetime of the connection.
1973 */
1974 conn->init_addr_type = cp->own_address_type;
1975 if (cp->own_address_type == ADDR_LE_DEV_RANDOM)
1976 bacpy(&conn->init_addr, &hdev->random_addr);
1977 else
1978 bacpy(&conn->init_addr, &hdev->bdaddr);
1979
1980 conn->resp_addr_type = cp->peer_addr_type;
1981 bacpy(&conn->resp_addr, &cp->peer_addr);
1982
Johan Hedberg9489eca2014-02-28 17:45:46 +02001983 /* We don't want the connection attempt to stick around
1984 * indefinitely since LE doesn't have a page timeout concept
1985 * like BR/EDR. Set a timer for any connection that doesn't use
1986 * the white list for connecting.
1987 */
1988 if (cp->filter_policy == HCI_LE_USE_PEER_ADDR)
1989 queue_delayed_work(conn->hdev->workqueue,
1990 &conn->le_conn_timeout,
Johan Hedberg09ae2602014-07-06 13:41:15 +03001991 conn->conn_timeout);
Johan Hedberg9489eca2014-02-28 17:45:46 +02001992
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02001993unlock:
1994 hci_dev_unlock(hdev);
1995}
1996
Johan Hedberg81d0c8a2014-03-24 14:39:04 +02001997static void hci_cs_le_start_enc(struct hci_dev *hdev, u8 status)
1998{
1999 struct hci_cp_le_start_enc *cp;
2000 struct hci_conn *conn;
2001
2002 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2003
2004 if (!status)
2005 return;
2006
2007 hci_dev_lock(hdev);
2008
2009 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_START_ENC);
2010 if (!cp)
2011 goto unlock;
2012
2013 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2014 if (!conn)
2015 goto unlock;
2016
2017 if (conn->state != BT_CONNECTED)
2018 goto unlock;
2019
2020 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
2021 hci_conn_drop(conn);
2022
2023unlock:
2024 hci_dev_unlock(hdev);
2025}
2026
Kuba Pawlak50fc85f2014-11-06 19:36:52 +01002027static void hci_cs_switch_role(struct hci_dev *hdev, u8 status)
2028{
2029 struct hci_cp_switch_role *cp;
2030 struct hci_conn *conn;
2031
2032 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2033
2034 if (!status)
2035 return;
2036
2037 cp = hci_sent_cmd_data(hdev, HCI_OP_SWITCH_ROLE);
2038 if (!cp)
2039 return;
2040
2041 hci_dev_lock(hdev);
2042
2043 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
2044 if (conn)
2045 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
2046
2047 hci_dev_unlock(hdev);
2048}
2049
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002050static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002051{
2052 __u8 status = *((__u8 *) skb->data);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002053 struct discovery_state *discov = &hdev->discovery;
2054 struct inquiry_entry *e;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002055
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002056 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002057
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002058 hci_conn_check_pending(hdev);
Andre Guedes89352e72011-11-04 14:16:53 -03002059
2060 if (!test_and_clear_bit(HCI_INQUIRY, &hdev->flags))
2061 return;
2062
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002063 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
Andre Guedes3e13fa12013-03-27 20:04:56 -03002064 wake_up_bit(&hdev->flags, HCI_INQUIRY);
2065
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02002066 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002067 return;
2068
Johan Hedberg56e5cb82011-11-08 20:40:16 +02002069 hci_dev_lock(hdev);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002070
Andre Guedes343f9352012-02-17 20:39:37 -03002071 if (discov->state != DISCOVERY_FINDING)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002072 goto unlock;
2073
2074 if (list_empty(&discov->resolve)) {
2075 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
2076 goto unlock;
2077 }
2078
2079 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
2080 if (e && hci_resolve_name(hdev, e) == 0) {
2081 e->name_state = NAME_PENDING;
2082 hci_discovery_set_state(hdev, DISCOVERY_RESOLVING);
2083 } else {
2084 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
2085 }
2086
2087unlock:
Johan Hedberg56e5cb82011-11-08 20:40:16 +02002088 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002089}
2090
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002091static void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002093 struct inquiry_data data;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002094 struct inquiry_info *info = (void *) (skb->data + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 int num_rsp = *((__u8 *) skb->data);
2096
2097 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
2098
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002099 if (!num_rsp)
2100 return;
2101
Andre Guedes1519cc12012-03-21 00:03:38 -03002102 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags))
2103 return;
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002106
Johan Hedberge17acd42011-03-30 23:57:16 +03002107 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02002108 u32 flags;
Johan Hedberg31754052012-01-04 13:39:52 +02002109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 bacpy(&data.bdaddr, &info->bdaddr);
2111 data.pscan_rep_mode = info->pscan_rep_mode;
2112 data.pscan_period_mode = info->pscan_period_mode;
2113 data.pscan_mode = info->pscan_mode;
2114 memcpy(data.dev_class, info->dev_class, 3);
2115 data.clock_offset = info->clock_offset;
Marcel Holtmannefb25132014-12-05 13:03:34 +01002116 data.rssi = HCI_RSSI_INVALID;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002117 data.ssp_mode = 0x00;
Johan Hedberg31754052012-01-04 13:39:52 +02002118
Marcel Holtmannaf589252014-07-01 14:11:20 +02002119 flags = hci_inquiry_cache_update(hdev, &data, false);
2120
Johan Hedberg48264f02011-11-09 13:58:58 +02002121 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Marcel Holtmannefb25132014-12-05 13:03:34 +01002122 info->dev_class, HCI_RSSI_INVALID,
2123 flags, NULL, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 hci_dev_unlock(hdev);
2127}
2128
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002129static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002131 struct hci_ev_conn_complete *ev = (void *) skb->data;
2132 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002134 BT_DBG("%s", hdev->name);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002137
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002138 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann94992372009-04-19 19:30:03 +02002139 if (!conn) {
2140 if (ev->link_type != SCO_LINK)
2141 goto unlock;
2142
2143 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
2144 if (!conn)
2145 goto unlock;
2146
2147 conn->type = SCO_LINK;
2148 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002149
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002150 if (!ev->status) {
2151 conn->handle = __le16_to_cpu(ev->handle);
Marcel Holtmann769be972008-07-14 20:13:49 +02002152
2153 if (conn->type == ACL_LINK) {
2154 conn->state = BT_CONFIG;
2155 hci_conn_hold(conn);
Szymon Janca9ea3ed2012-07-19 14:46:08 +02002156
2157 if (!conn->out && !hci_conn_ssp_enabled(conn) &&
2158 !hci_find_link_key(hdev, &ev->bdaddr))
2159 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
2160 else
2161 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Marcel Holtmann769be972008-07-14 20:13:49 +02002162 } else
2163 conn->state = BT_CONNECTED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002164
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002165 hci_conn_add_sysfs(conn);
2166
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002167 if (test_bit(HCI_AUTH, &hdev->flags))
Johan Hedberg4dae2792014-06-24 17:03:50 +03002168 set_bit(HCI_CONN_AUTH, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002169
2170 if (test_bit(HCI_ENCRYPT, &hdev->flags))
Johan Hedberg4dae2792014-06-24 17:03:50 +03002171 set_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002172
2173 /* Get remote features */
2174 if (conn->type == ACL_LINK) {
2175 struct hci_cp_read_remote_features cp;
2176 cp.handle = ev->handle;
Marcel Holtmann769be972008-07-14 20:13:49 +02002177 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002178 sizeof(cp), &cp);
Johan Hedberg22f433d2014-08-01 11:13:32 +03002179
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02002180 hci_update_page_scan(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002181 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002182
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002183 /* Set packet type for incoming connection */
Andrei Emeltchenkod095c1e2011-12-01 14:33:27 +02002184 if (!conn->out && hdev->hci_ver < BLUETOOTH_VER_2_0) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002185 struct hci_cp_change_conn_ptype cp;
2186 cp.handle = ev->handle;
Marcel Holtmanna8746412008-07-14 20:13:46 +02002187 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002188 hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE, sizeof(cp),
2189 &cp);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002190 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02002191 } else {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002192 conn->state = BT_CLOSED;
Johan Hedberg17d5c042011-01-22 06:09:08 +02002193 if (conn->type == ACL_LINK)
Marcel Holtmann64c7b772014-02-18 14:22:20 -08002194 mgmt_connect_failed(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002195 conn->dst_type, ev->status);
Johan Hedberg17d5c042011-01-22 06:09:08 +02002196 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002197
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002198 if (conn->type == ACL_LINK)
2199 hci_sco_setup(conn, ev->status);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002200
Marcel Holtmann769be972008-07-14 20:13:49 +02002201 if (ev->status) {
2202 hci_proto_connect_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002203 hci_conn_del(conn);
Marcel Holtmannc89b6e62009-01-15 21:57:03 +01002204 } else if (ev->link_type != ACL_LINK)
2205 hci_proto_connect_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002206
2207unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002209
2210 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211}
2212
Johan Hedberg70c46422014-07-09 12:59:17 +03002213static void hci_reject_conn(struct hci_dev *hdev, bdaddr_t *bdaddr)
2214{
2215 struct hci_cp_reject_conn_req cp;
2216
2217 bacpy(&cp.bdaddr, bdaddr);
2218 cp.reason = HCI_ERROR_REJ_BAD_ADDR;
2219 hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
2220}
2221
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002222static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002224 struct hci_ev_conn_request *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 int mask = hdev->link_mode;
Johan Hedberg70c46422014-07-09 12:59:17 +03002226 struct inquiry_entry *ie;
2227 struct hci_conn *conn;
Frédéric Dalleau20714bfe2012-11-21 10:51:12 +01002228 __u8 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03002230 BT_DBG("%s bdaddr %pMR type 0x%x", hdev->name, &ev->bdaddr,
Gustavo Padovan807deac2012-05-17 00:36:24 -03002231 ev->link_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Frédéric Dalleau20714bfe2012-11-21 10:51:12 +01002233 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type,
2234 &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Johan Hedberg70c46422014-07-09 12:59:17 +03002236 if (!(mask & HCI_LM_ACCEPT)) {
2237 hci_reject_conn(hdev, &ev->bdaddr);
2238 return;
2239 }
2240
Johan Hedberg46c4c942014-07-16 16:19:21 +03002241 if (hci_bdaddr_list_lookup(&hdev->blacklist, &ev->bdaddr,
2242 BDADDR_BREDR)) {
2243 hci_reject_conn(hdev, &ev->bdaddr);
2244 return;
2245 }
2246
2247 if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags) &&
2248 !hci_bdaddr_list_lookup(&hdev->whitelist, &ev->bdaddr,
2249 BDADDR_BREDR)) {
2250 hci_reject_conn(hdev, &ev->bdaddr);
2251 return;
Johan Hedberg70c46422014-07-09 12:59:17 +03002252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Johan Hedberg70c46422014-07-09 12:59:17 +03002254 /* Connection accepted */
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002255
Johan Hedberg70c46422014-07-09 12:59:17 +03002256 hci_dev_lock(hdev);
Marcel Holtmannc7bdd502008-07-14 20:13:47 +02002257
Johan Hedberg70c46422014-07-09 12:59:17 +03002258 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
2259 if (ie)
2260 memcpy(ie->data.dev_class, ev->dev_class, 3);
2261
2262 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type,
2263 &ev->bdaddr);
2264 if (!conn) {
Johan Hedberga5c4e302014-07-16 11:56:07 +03002265 conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr,
2266 HCI_ROLE_SLAVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 if (!conn) {
Johan Hedberg70c46422014-07-09 12:59:17 +03002268 BT_ERR("No memory for new connection");
2269 hci_dev_unlock(hdev);
2270 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
Johan Hedberg70c46422014-07-09 12:59:17 +03002272 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002273
Johan Hedberg70c46422014-07-09 12:59:17 +03002274 memcpy(conn->dev_class, ev->dev_class, 3);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002275
Johan Hedberg70c46422014-07-09 12:59:17 +03002276 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Johan Hedberg70c46422014-07-09 12:59:17 +03002278 if (ev->link_type == ACL_LINK ||
2279 (!(flags & HCI_PROTO_DEFER) && !lmp_esco_capable(hdev))) {
2280 struct hci_cp_accept_conn_req cp;
2281 conn->state = BT_CONNECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
2283 bacpy(&cp.bdaddr, &ev->bdaddr);
Johan Hedberg70c46422014-07-09 12:59:17 +03002284
2285 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
2286 cp.role = 0x00; /* Become master */
2287 else
2288 cp.role = 0x01; /* Remain slave */
2289
2290 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
2291 } else if (!(flags & HCI_PROTO_DEFER)) {
2292 struct hci_cp_accept_sync_conn_req cp;
2293 conn->state = BT_CONNECT;
2294
2295 bacpy(&cp.bdaddr, &ev->bdaddr);
2296 cp.pkt_type = cpu_to_le16(conn->pkt_type);
2297
2298 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
2299 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
2300 cp.max_latency = cpu_to_le16(0xffff);
2301 cp.content_format = cpu_to_le16(hdev->voice_setting);
2302 cp.retrans_effort = 0xff;
2303
2304 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ, sizeof(cp),
2305 &cp);
2306 } else {
2307 conn->state = BT_CONNECT2;
2308 hci_proto_connect_cfm(conn, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 }
2310}
2311
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02002312static u8 hci_to_mgmt_reason(u8 err)
2313{
2314 switch (err) {
2315 case HCI_ERROR_CONNECTION_TIMEOUT:
2316 return MGMT_DEV_DISCONN_TIMEOUT;
2317 case HCI_ERROR_REMOTE_USER_TERM:
2318 case HCI_ERROR_REMOTE_LOW_RESOURCES:
2319 case HCI_ERROR_REMOTE_POWER_OFF:
2320 return MGMT_DEV_DISCONN_REMOTE;
2321 case HCI_ERROR_LOCAL_HOST_TERM:
2322 return MGMT_DEV_DISCONN_LOCAL_HOST;
2323 default:
2324 return MGMT_DEV_DISCONN_UNKNOWN;
2325 }
2326}
2327
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002328static void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002330 struct hci_ev_disconn_complete *ev = (void *) skb->data;
Andre Guedesabf54a52013-11-07 17:36:09 -03002331 u8 reason = hci_to_mgmt_reason(ev->reason);
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002332 struct hci_conn_params *params;
Marcel Holtmann04837f62006-07-03 10:02:33 +02002333 struct hci_conn *conn;
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02002334 bool mgmt_connected;
Andre Guedes38462202013-11-07 17:36:10 -03002335 u8 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002337 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 hci_dev_lock(hdev);
2340
Marcel Holtmann04837f62006-07-03 10:02:33 +02002341 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergf7520542011-01-20 12:34:39 +02002342 if (!conn)
2343 goto unlock;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002344
Andre Guedesabf54a52013-11-07 17:36:09 -03002345 if (ev->status) {
2346 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
2347 conn->dst_type, ev->status);
2348 goto unlock;
Johan Hedberg37d9ef72011-11-10 15:54:39 +02002349 }
Johan Hedbergf7520542011-01-20 12:34:39 +02002350
Andre Guedes38462202013-11-07 17:36:10 -03002351 conn->state = BT_CLOSED;
2352
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02002353 mgmt_connected = test_and_clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags);
2354 mgmt_device_disconnected(hdev, &conn->dst, conn->type, conn->dst_type,
2355 reason, mgmt_connected);
Andre Guedesabf54a52013-11-07 17:36:09 -03002356
Johan Hedberg22f433d2014-08-01 11:13:32 +03002357 if (conn->type == ACL_LINK) {
2358 if (test_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
2359 hci_remove_link_key(hdev, &conn->dst);
2360
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02002361 hci_update_page_scan(hdev);
Johan Hedberg22f433d2014-08-01 11:13:32 +03002362 }
Johan Hedberg22102462013-10-05 12:01:06 +02002363
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002364 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
2365 if (params) {
2366 switch (params->auto_connect) {
2367 case HCI_AUTO_CONN_LINK_LOSS:
2368 if (ev->reason != HCI_ERROR_CONNECTION_TIMEOUT)
2369 break;
2370 /* Fall through */
2371
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02002372 case HCI_AUTO_CONN_DIRECT:
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002373 case HCI_AUTO_CONN_ALWAYS:
Johan Hedberg418025d2014-07-04 12:37:24 +03002374 list_del_init(&params->action);
2375 list_add(&params->action, &hdev->pend_le_conns);
2376 hci_update_background_scan(hdev);
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002377 break;
2378
2379 default:
2380 break;
2381 }
2382 }
2383
Andre Guedes38462202013-11-07 17:36:10 -03002384 type = conn->type;
Johan Hedberg22102462013-10-05 12:01:06 +02002385
Andre Guedes38462202013-11-07 17:36:10 -03002386 hci_proto_disconn_cfm(conn, ev->reason);
2387 hci_conn_del(conn);
2388
2389 /* Re-enable advertising if necessary, since it might
2390 * have been disabled by the connection. From the
2391 * HCI_LE_Set_Advertise_Enable command description in
2392 * the core specification (v4.0):
2393 * "The Controller shall continue advertising until the Host
2394 * issues an LE_Set_Advertise_Enable command with
2395 * Advertising_Enable set to 0x00 (Advertising is disabled)
2396 * or until a connection is created or until the Advertising
2397 * is timed out due to Directed Advertising."
2398 */
2399 if (type == LE_LINK)
2400 mgmt_reenable_advertising(hdev);
Johan Hedbergf7520542011-01-20 12:34:39 +02002401
2402unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 hci_dev_unlock(hdev);
2404}
2405
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002406static void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002407{
2408 struct hci_ev_auth_complete *ev = (void *) skb->data;
2409 struct hci_conn *conn;
2410
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002411 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002412
2413 hci_dev_lock(hdev);
2414
2415 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002416 if (!conn)
2417 goto unlock;
2418
2419 if (!ev->status) {
Johan Hedbergaa64a8b2012-01-18 21:33:12 +02002420 if (!hci_conn_ssp_enabled(conn) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03002421 test_bit(HCI_CONN_REAUTH_PEND, &conn->flags)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002422 BT_INFO("re-auth of legacy device is not possible.");
Johan Hedberg2a611692011-02-19 12:06:00 -03002423 } else {
Johan Hedberg4dae2792014-06-24 17:03:50 +03002424 set_bit(HCI_CONN_AUTH, &conn->flags);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002425 conn->sec_level = conn->pending_sec_level;
Johan Hedberg2a611692011-02-19 12:06:00 -03002426 }
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002427 } else {
Johan Hedberge1e930f2014-09-08 17:09:49 -07002428 mgmt_auth_failed(conn, ev->status);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002429 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002430
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002431 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
2432 clear_bit(HCI_CONN_REAUTH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002433
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002434 if (conn->state == BT_CONFIG) {
Johan Hedbergaa64a8b2012-01-18 21:33:12 +02002435 if (!ev->status && hci_conn_ssp_enabled(conn)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002436 struct hci_cp_set_conn_encrypt cp;
2437 cp.handle = ev->handle;
2438 cp.encrypt = 0x01;
2439 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
Gustavo Padovan807deac2012-05-17 00:36:24 -03002440 &cp);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002441 } else {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002442 conn->state = BT_CONNECTED;
2443 hci_proto_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02002444 hci_conn_drop(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002445 }
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002446 } else {
2447 hci_auth_cfm(conn, ev->status);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002448
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002449 hci_conn_hold(conn);
2450 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02002451 hci_conn_drop(conn);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002452 }
2453
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002454 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002455 if (!ev->status) {
2456 struct hci_cp_set_conn_encrypt cp;
2457 cp.handle = ev->handle;
2458 cp.encrypt = 0x01;
2459 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
Gustavo Padovan807deac2012-05-17 00:36:24 -03002460 &cp);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002461 } else {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002462 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002463 hci_encrypt_cfm(conn, ev->status, 0x00);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002464 }
2465 }
2466
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002467unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002468 hci_dev_unlock(hdev);
2469}
2470
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002471static void hci_remote_name_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002472{
Johan Hedberg127178d2010-11-18 22:22:29 +02002473 struct hci_ev_remote_name *ev = (void *) skb->data;
2474 struct hci_conn *conn;
2475
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002476 BT_DBG("%s", hdev->name);
2477
2478 hci_conn_check_pending(hdev);
Johan Hedberg127178d2010-11-18 22:22:29 +02002479
2480 hci_dev_lock(hdev);
2481
2482 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedbergb644ba32012-01-17 21:48:47 +02002483
2484 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
2485 goto check_auth;
2486
2487 if (ev->status == 0)
2488 hci_check_pending_name(hdev, conn, &ev->bdaddr, ev->name,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002489 strnlen(ev->name, HCI_MAX_NAME_LENGTH));
Johan Hedbergb644ba32012-01-17 21:48:47 +02002490 else
2491 hci_check_pending_name(hdev, conn, &ev->bdaddr, NULL, 0);
2492
2493check_auth:
Johan Hedberg79c6c702011-04-28 11:28:55 -07002494 if (!conn)
2495 goto unlock;
2496
2497 if (!hci_outgoing_auth_needed(hdev, conn))
2498 goto unlock;
2499
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002500 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02002501 struct hci_cp_auth_requested cp;
Johan Hedberg977f8fc2014-07-17 15:35:39 +03002502
2503 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
2504
Johan Hedberg127178d2010-11-18 22:22:29 +02002505 cp.handle = __cpu_to_le16(conn->handle);
2506 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
2507 }
2508
Johan Hedberg79c6c702011-04-28 11:28:55 -07002509unlock:
Johan Hedberg127178d2010-11-18 22:22:29 +02002510 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002511}
2512
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002513static void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002514{
2515 struct hci_ev_encrypt_change *ev = (void *) skb->data;
2516 struct hci_conn *conn;
2517
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002518 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002519
2520 hci_dev_lock(hdev);
2521
2522 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08002523 if (!conn)
2524 goto unlock;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002525
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08002526 if (!ev->status) {
2527 if (ev->encrypt) {
2528 /* Encryption implies authentication */
Johan Hedberg4dae2792014-06-24 17:03:50 +03002529 set_bit(HCI_CONN_AUTH, &conn->flags);
2530 set_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08002531 conn->sec_level = conn->pending_sec_level;
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08002532
Marcel Holtmann914a6ff2014-02-01 11:52:02 -08002533 /* P-256 authentication key implies FIPS */
2534 if (conn->key_type == HCI_LK_AUTH_COMBINATION_P256)
Johan Hedberg4dae2792014-06-24 17:03:50 +03002535 set_bit(HCI_CONN_FIPS, &conn->flags);
Marcel Holtmann914a6ff2014-02-01 11:52:02 -08002536
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08002537 if ((conn->type == ACL_LINK && ev->encrypt == 0x02) ||
2538 conn->type == LE_LINK)
2539 set_bit(HCI_CONN_AES_CCM, &conn->flags);
2540 } else {
Johan Hedberg4dae2792014-06-24 17:03:50 +03002541 clear_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08002542 clear_bit(HCI_CONN_AES_CCM, &conn->flags);
2543 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002544 }
2545
Johan Hedberg7ed3fa22014-09-10 22:16:35 -07002546 /* We should disregard the current RPA and generate a new one
2547 * whenever the encryption procedure fails.
2548 */
2549 if (ev->status && conn->type == LE_LINK)
2550 set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);
2551
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08002552 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
2553
2554 if (ev->status && conn->state == BT_CONNECTED) {
2555 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
2556 hci_conn_drop(conn);
2557 goto unlock;
2558 }
2559
2560 if (conn->state == BT_CONFIG) {
2561 if (!ev->status)
2562 conn->state = BT_CONNECTED;
2563
Marcel Holtmann40b552a2014-03-19 14:10:25 -07002564 /* In Secure Connections Only mode, do not allow any
2565 * connections that are not encrypted with AES-CCM
2566 * using a P-256 authenticated combination key.
2567 */
2568 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) &&
2569 (!test_bit(HCI_CONN_AES_CCM, &conn->flags) ||
2570 conn->key_type != HCI_LK_AUTH_COMBINATION_P256)) {
2571 hci_proto_connect_cfm(conn, HCI_ERROR_AUTH_FAILURE);
2572 hci_conn_drop(conn);
2573 goto unlock;
2574 }
2575
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08002576 hci_proto_connect_cfm(conn, ev->status);
2577 hci_conn_drop(conn);
2578 } else
2579 hci_encrypt_cfm(conn, ev->status, ev->encrypt);
2580
Gustavo Padovana7d77232012-05-13 03:20:07 -03002581unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002582 hci_dev_unlock(hdev);
2583}
2584
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002585static void hci_change_link_key_complete_evt(struct hci_dev *hdev,
2586 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002587{
2588 struct hci_ev_change_link_key_complete *ev = (void *) skb->data;
2589 struct hci_conn *conn;
2590
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002591 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002592
2593 hci_dev_lock(hdev);
2594
2595 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
2596 if (conn) {
2597 if (!ev->status)
Johan Hedberg4dae2792014-06-24 17:03:50 +03002598 set_bit(HCI_CONN_SECURE, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002599
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002600 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002601
2602 hci_key_change_cfm(conn, ev->status);
2603 }
2604
2605 hci_dev_unlock(hdev);
2606}
2607
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002608static void hci_remote_features_evt(struct hci_dev *hdev,
2609 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002610{
2611 struct hci_ev_remote_features *ev = (void *) skb->data;
2612 struct hci_conn *conn;
2613
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002614 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002615
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002616 hci_dev_lock(hdev);
2617
2618 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02002619 if (!conn)
2620 goto unlock;
Marcel Holtmann769be972008-07-14 20:13:49 +02002621
Johan Hedbergccd556f2010-11-10 17:11:51 +02002622 if (!ev->status)
Johan Hedbergcad718e2013-04-17 15:00:51 +03002623 memcpy(conn->features[0], ev->features, 8);
Johan Hedbergccd556f2010-11-10 17:11:51 +02002624
2625 if (conn->state != BT_CONFIG)
2626 goto unlock;
2627
2628 if (!ev->status && lmp_ssp_capable(hdev) && lmp_ssp_capable(conn)) {
2629 struct hci_cp_read_remote_ext_features cp;
2630 cp.handle = ev->handle;
2631 cp.page = 0x01;
2632 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES,
Gustavo Padovan807deac2012-05-17 00:36:24 -03002633 sizeof(cp), &cp);
Johan Hedberg392599b2010-11-18 22:22:28 +02002634 goto unlock;
2635 }
2636
Johan Hedberg671267b2012-05-12 16:11:50 -03002637 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02002638 struct hci_cp_remote_name_req cp;
2639 memset(&cp, 0, sizeof(cp));
2640 bacpy(&cp.bdaddr, &conn->dst);
2641 cp.pscan_rep_mode = 0x02;
2642 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
Johan Hedbergb644ba32012-01-17 21:48:47 +02002643 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00002644 mgmt_device_connected(hdev, conn, 0, NULL, 0);
Johan Hedberg392599b2010-11-18 22:22:28 +02002645
Johan Hedberg127178d2010-11-18 22:22:29 +02002646 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02002647 conn->state = BT_CONNECTED;
2648 hci_proto_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02002649 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02002650 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002651
Johan Hedbergccd556f2010-11-10 17:11:51 +02002652unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002653 hci_dev_unlock(hdev);
2654}
2655
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002656static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002657{
2658 struct hci_ev_cmd_complete *ev = (void *) skb->data;
Johan Hedberg9238f362013-03-05 20:37:48 +02002659 u8 status = skb->data[sizeof(*ev)];
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002660 __u16 opcode;
2661
2662 skb_pull(skb, sizeof(*ev));
2663
2664 opcode = __le16_to_cpu(ev->opcode);
2665
2666 switch (opcode) {
2667 case HCI_OP_INQUIRY_CANCEL:
2668 hci_cc_inquiry_cancel(hdev, skb);
2669 break;
2670
Andre Guedes4d934832012-03-21 00:03:35 -03002671 case HCI_OP_PERIODIC_INQ:
2672 hci_cc_periodic_inq(hdev, skb);
2673 break;
2674
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002675 case HCI_OP_EXIT_PERIODIC_INQ:
2676 hci_cc_exit_periodic_inq(hdev, skb);
2677 break;
2678
2679 case HCI_OP_REMOTE_NAME_REQ_CANCEL:
2680 hci_cc_remote_name_req_cancel(hdev, skb);
2681 break;
2682
2683 case HCI_OP_ROLE_DISCOVERY:
2684 hci_cc_role_discovery(hdev, skb);
2685 break;
2686
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02002687 case HCI_OP_READ_LINK_POLICY:
2688 hci_cc_read_link_policy(hdev, skb);
2689 break;
2690
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002691 case HCI_OP_WRITE_LINK_POLICY:
2692 hci_cc_write_link_policy(hdev, skb);
2693 break;
2694
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02002695 case HCI_OP_READ_DEF_LINK_POLICY:
2696 hci_cc_read_def_link_policy(hdev, skb);
2697 break;
2698
2699 case HCI_OP_WRITE_DEF_LINK_POLICY:
2700 hci_cc_write_def_link_policy(hdev, skb);
2701 break;
2702
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002703 case HCI_OP_RESET:
2704 hci_cc_reset(hdev, skb);
2705 break;
2706
2707 case HCI_OP_WRITE_LOCAL_NAME:
2708 hci_cc_write_local_name(hdev, skb);
2709 break;
2710
2711 case HCI_OP_READ_LOCAL_NAME:
2712 hci_cc_read_local_name(hdev, skb);
2713 break;
2714
2715 case HCI_OP_WRITE_AUTH_ENABLE:
2716 hci_cc_write_auth_enable(hdev, skb);
2717 break;
2718
2719 case HCI_OP_WRITE_ENCRYPT_MODE:
2720 hci_cc_write_encrypt_mode(hdev, skb);
2721 break;
2722
2723 case HCI_OP_WRITE_SCAN_ENABLE:
2724 hci_cc_write_scan_enable(hdev, skb);
2725 break;
2726
2727 case HCI_OP_READ_CLASS_OF_DEV:
2728 hci_cc_read_class_of_dev(hdev, skb);
2729 break;
2730
2731 case HCI_OP_WRITE_CLASS_OF_DEV:
2732 hci_cc_write_class_of_dev(hdev, skb);
2733 break;
2734
2735 case HCI_OP_READ_VOICE_SETTING:
2736 hci_cc_read_voice_setting(hdev, skb);
2737 break;
2738
2739 case HCI_OP_WRITE_VOICE_SETTING:
2740 hci_cc_write_voice_setting(hdev, skb);
2741 break;
2742
Marcel Holtmannb4cb9fb2013-10-14 13:56:16 -07002743 case HCI_OP_READ_NUM_SUPPORTED_IAC:
2744 hci_cc_read_num_supported_iac(hdev, skb);
2745 break;
2746
Marcel Holtmann333140b2008-07-14 20:13:48 +02002747 case HCI_OP_WRITE_SSP_MODE:
2748 hci_cc_write_ssp_mode(hdev, skb);
2749 break;
2750
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08002751 case HCI_OP_WRITE_SC_SUPPORT:
2752 hci_cc_write_sc_support(hdev, skb);
2753 break;
2754
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002755 case HCI_OP_READ_LOCAL_VERSION:
2756 hci_cc_read_local_version(hdev, skb);
2757 break;
2758
2759 case HCI_OP_READ_LOCAL_COMMANDS:
2760 hci_cc_read_local_commands(hdev, skb);
2761 break;
2762
2763 case HCI_OP_READ_LOCAL_FEATURES:
2764 hci_cc_read_local_features(hdev, skb);
2765 break;
2766
Andre Guedes971e3a42011-06-30 19:20:52 -03002767 case HCI_OP_READ_LOCAL_EXT_FEATURES:
2768 hci_cc_read_local_ext_features(hdev, skb);
2769 break;
2770
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002771 case HCI_OP_READ_BUFFER_SIZE:
2772 hci_cc_read_buffer_size(hdev, skb);
2773 break;
2774
2775 case HCI_OP_READ_BD_ADDR:
2776 hci_cc_read_bd_addr(hdev, skb);
2777 break;
2778
Johan Hedbergf332ec62013-03-15 17:07:11 -05002779 case HCI_OP_READ_PAGE_SCAN_ACTIVITY:
2780 hci_cc_read_page_scan_activity(hdev, skb);
2781 break;
2782
Johan Hedberg4a3ee762013-03-15 17:07:12 -05002783 case HCI_OP_WRITE_PAGE_SCAN_ACTIVITY:
2784 hci_cc_write_page_scan_activity(hdev, skb);
2785 break;
2786
Johan Hedbergf332ec62013-03-15 17:07:11 -05002787 case HCI_OP_READ_PAGE_SCAN_TYPE:
2788 hci_cc_read_page_scan_type(hdev, skb);
2789 break;
2790
Johan Hedberg4a3ee762013-03-15 17:07:12 -05002791 case HCI_OP_WRITE_PAGE_SCAN_TYPE:
2792 hci_cc_write_page_scan_type(hdev, skb);
2793 break;
2794
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +02002795 case HCI_OP_READ_DATA_BLOCK_SIZE:
2796 hci_cc_read_data_block_size(hdev, skb);
2797 break;
2798
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +02002799 case HCI_OP_READ_FLOW_CONTROL_MODE:
2800 hci_cc_read_flow_control_mode(hdev, skb);
2801 break;
2802
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +03002803 case HCI_OP_READ_LOCAL_AMP_INFO:
2804 hci_cc_read_local_amp_info(hdev, skb);
2805 break;
2806
Johan Hedberg33f35722014-06-28 17:54:06 +03002807 case HCI_OP_READ_CLOCK:
2808 hci_cc_read_clock(hdev, skb);
2809 break;
2810
Andrei Emeltchenko903e4542012-09-27 17:26:09 +03002811 case HCI_OP_READ_LOCAL_AMP_ASSOC:
2812 hci_cc_read_local_amp_assoc(hdev, skb);
2813 break;
2814
Johan Hedbergd5859e22011-01-25 01:19:58 +02002815 case HCI_OP_READ_INQ_RSP_TX_POWER:
2816 hci_cc_read_inq_rsp_tx_power(hdev, skb);
2817 break;
2818
Johan Hedberg980e1a52011-01-22 06:10:07 +02002819 case HCI_OP_PIN_CODE_REPLY:
2820 hci_cc_pin_code_reply(hdev, skb);
2821 break;
2822
2823 case HCI_OP_PIN_CODE_NEG_REPLY:
2824 hci_cc_pin_code_neg_reply(hdev, skb);
2825 break;
2826
Szymon Jancc35938b2011-03-22 13:12:21 +01002827 case HCI_OP_READ_LOCAL_OOB_DATA:
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08002828 hci_cc_read_local_oob_data(hdev, skb);
2829 break;
2830
2831 case HCI_OP_READ_LOCAL_OOB_EXT_DATA:
2832 hci_cc_read_local_oob_ext_data(hdev, skb);
Szymon Jancc35938b2011-03-22 13:12:21 +01002833 break;
2834
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002835 case HCI_OP_LE_READ_BUFFER_SIZE:
2836 hci_cc_le_read_buffer_size(hdev, skb);
2837 break;
2838
Johan Hedberg60e77322013-01-22 14:01:59 +02002839 case HCI_OP_LE_READ_LOCAL_FEATURES:
2840 hci_cc_le_read_local_features(hdev, skb);
2841 break;
2842
Johan Hedberg8fa19092012-10-19 20:57:49 +03002843 case HCI_OP_LE_READ_ADV_TX_POWER:
2844 hci_cc_le_read_adv_tx_power(hdev, skb);
2845 break;
2846
Johan Hedberga5c29682011-02-19 12:05:57 -03002847 case HCI_OP_USER_CONFIRM_REPLY:
2848 hci_cc_user_confirm_reply(hdev, skb);
2849 break;
2850
2851 case HCI_OP_USER_CONFIRM_NEG_REPLY:
2852 hci_cc_user_confirm_neg_reply(hdev, skb);
2853 break;
2854
Brian Gix1143d452011-11-23 08:28:34 -08002855 case HCI_OP_USER_PASSKEY_REPLY:
2856 hci_cc_user_passkey_reply(hdev, skb);
2857 break;
2858
2859 case HCI_OP_USER_PASSKEY_NEG_REPLY:
2860 hci_cc_user_passkey_neg_reply(hdev, skb);
Szymon Janc16cde992012-04-13 12:32:42 +02002861 break;
Andre Guedes07f7fa52011-12-02 21:13:31 +09002862
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08002863 case HCI_OP_LE_SET_RANDOM_ADDR:
2864 hci_cc_le_set_random_addr(hdev, skb);
2865 break;
2866
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01002867 case HCI_OP_LE_SET_ADV_ENABLE:
2868 hci_cc_le_set_adv_enable(hdev, skb);
2869 break;
2870
Marcel Holtmann533553f2014-03-21 12:18:10 -07002871 case HCI_OP_LE_SET_SCAN_PARAM:
2872 hci_cc_le_set_scan_param(hdev, skb);
2873 break;
2874
Andre Guedeseb9d91f2011-05-26 16:23:52 -03002875 case HCI_OP_LE_SET_SCAN_ENABLE:
2876 hci_cc_le_set_scan_enable(hdev, skb);
2877 break;
2878
Johan Hedbergcf1d0812013-01-22 14:02:00 +02002879 case HCI_OP_LE_READ_WHITE_LIST_SIZE:
2880 hci_cc_le_read_white_list_size(hdev, skb);
2881 break;
2882
Marcel Holtmann0f36b582014-02-27 20:37:31 -08002883 case HCI_OP_LE_CLEAR_WHITE_LIST:
2884 hci_cc_le_clear_white_list(hdev, skb);
2885 break;
2886
2887 case HCI_OP_LE_ADD_TO_WHITE_LIST:
2888 hci_cc_le_add_to_white_list(hdev, skb);
2889 break;
2890
2891 case HCI_OP_LE_DEL_FROM_WHITE_LIST:
2892 hci_cc_le_del_from_white_list(hdev, skb);
2893 break;
2894
Johan Hedberg9b008c02013-01-22 14:02:01 +02002895 case HCI_OP_LE_READ_SUPPORTED_STATES:
2896 hci_cc_le_read_supported_states(hdev, skb);
2897 break;
2898
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01002899 case HCI_OP_LE_READ_DEF_DATA_LEN:
2900 hci_cc_le_read_def_data_len(hdev, skb);
2901 break;
2902
2903 case HCI_OP_LE_WRITE_DEF_DATA_LEN:
2904 hci_cc_le_write_def_data_len(hdev, skb);
2905 break;
2906
2907 case HCI_OP_LE_READ_MAX_DATA_LEN:
2908 hci_cc_le_read_max_data_len(hdev, skb);
2909 break;
2910
Andre Guedesf9b49302011-06-30 19:20:53 -03002911 case HCI_OP_WRITE_LE_HOST_SUPPORTED:
2912 hci_cc_write_le_host_supported(hdev, skb);
2913 break;
2914
Johan Hedberg56ed2cb2014-02-27 14:05:40 +02002915 case HCI_OP_LE_SET_ADV_PARAM:
2916 hci_cc_set_adv_param(hdev, skb);
2917 break;
2918
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +03002919 case HCI_OP_WRITE_REMOTE_AMP_ASSOC:
2920 hci_cc_write_remote_amp_assoc(hdev, skb);
2921 break;
2922
Andrzej Kaczmarek5ae76a92014-05-08 15:32:08 +02002923 case HCI_OP_READ_RSSI:
2924 hci_cc_read_rssi(hdev, skb);
2925 break;
2926
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02002927 case HCI_OP_READ_TX_POWER:
2928 hci_cc_read_tx_power(hdev, skb);
2929 break;
2930
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002931 default:
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002932 BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002933 break;
2934 }
2935
Johan Hedbergad82cdd2013-03-09 09:53:50 +02002936 if (opcode != HCI_OP_NOP)
Marcel Holtmann65cc2b42014-06-16 12:30:56 +02002937 cancel_delayed_work(&hdev->cmd_timer);
Ville Tervo6bd32322011-02-16 16:32:41 +02002938
Johan Hedbergad82cdd2013-03-09 09:53:50 +02002939 hci_req_cmd_complete(hdev, opcode, status);
Johan Hedberg9238f362013-03-05 20:37:48 +02002940
Szymon Jancdbccd792012-12-11 08:51:19 +01002941 if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags)) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002942 atomic_set(&hdev->cmd_cnt, 1);
2943 if (!skb_queue_empty(&hdev->cmd_q))
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02002944 queue_work(hdev->workqueue, &hdev->cmd_work);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002945 }
2946}
2947
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002948static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002949{
2950 struct hci_ev_cmd_status *ev = (void *) skb->data;
2951 __u16 opcode;
2952
2953 skb_pull(skb, sizeof(*ev));
2954
2955 opcode = __le16_to_cpu(ev->opcode);
2956
2957 switch (opcode) {
2958 case HCI_OP_INQUIRY:
2959 hci_cs_inquiry(hdev, ev->status);
2960 break;
2961
2962 case HCI_OP_CREATE_CONN:
2963 hci_cs_create_conn(hdev, ev->status);
2964 break;
2965
Kuba Pawlak9645c762014-11-06 19:36:53 +01002966 case HCI_OP_DISCONNECT:
2967 hci_cs_disconnect(hdev, ev->status);
2968 break;
2969
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002970 case HCI_OP_ADD_SCO:
2971 hci_cs_add_sco(hdev, ev->status);
2972 break;
2973
Marcel Holtmannf8558552008-07-14 20:13:49 +02002974 case HCI_OP_AUTH_REQUESTED:
2975 hci_cs_auth_requested(hdev, ev->status);
2976 break;
2977
2978 case HCI_OP_SET_CONN_ENCRYPT:
2979 hci_cs_set_conn_encrypt(hdev, ev->status);
2980 break;
2981
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002982 case HCI_OP_REMOTE_NAME_REQ:
2983 hci_cs_remote_name_req(hdev, ev->status);
2984 break;
2985
Marcel Holtmann769be972008-07-14 20:13:49 +02002986 case HCI_OP_READ_REMOTE_FEATURES:
2987 hci_cs_read_remote_features(hdev, ev->status);
2988 break;
2989
2990 case HCI_OP_READ_REMOTE_EXT_FEATURES:
2991 hci_cs_read_remote_ext_features(hdev, ev->status);
2992 break;
2993
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002994 case HCI_OP_SETUP_SYNC_CONN:
2995 hci_cs_setup_sync_conn(hdev, ev->status);
2996 break;
2997
Kuba Pawlak9645c762014-11-06 19:36:53 +01002998 case HCI_OP_CREATE_PHY_LINK:
2999 hci_cs_create_phylink(hdev, ev->status);
3000 break;
3001
3002 case HCI_OP_ACCEPT_PHY_LINK:
3003 hci_cs_accept_phylink(hdev, ev->status);
3004 break;
3005
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003006 case HCI_OP_SNIFF_MODE:
3007 hci_cs_sniff_mode(hdev, ev->status);
3008 break;
3009
3010 case HCI_OP_EXIT_SNIFF_MODE:
3011 hci_cs_exit_sniff_mode(hdev, ev->status);
3012 break;
3013
Kuba Pawlak50fc85f2014-11-06 19:36:52 +01003014 case HCI_OP_SWITCH_ROLE:
3015 hci_cs_switch_role(hdev, ev->status);
3016 break;
3017
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02003018 case HCI_OP_LE_CREATE_CONN:
3019 hci_cs_le_create_conn(hdev, ev->status);
3020 break;
3021
Johan Hedberg81d0c8a2014-03-24 14:39:04 +02003022 case HCI_OP_LE_START_ENC:
3023 hci_cs_le_start_enc(hdev, ev->status);
3024 break;
3025
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003026 default:
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003027 BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003028 break;
3029 }
3030
Johan Hedbergad82cdd2013-03-09 09:53:50 +02003031 if (opcode != HCI_OP_NOP)
Marcel Holtmann65cc2b42014-06-16 12:30:56 +02003032 cancel_delayed_work(&hdev->cmd_timer);
Ville Tervo6bd32322011-02-16 16:32:41 +02003033
Johan Hedberg02350a72013-04-03 21:50:29 +03003034 if (ev->status ||
3035 (hdev->sent_cmd && !bt_cb(hdev->sent_cmd)->req.event))
3036 hci_req_cmd_complete(hdev, opcode, ev->status);
Johan Hedberg9238f362013-03-05 20:37:48 +02003037
Gustavo F. Padovan10572132011-03-16 15:36:29 -03003038 if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags)) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003039 atomic_set(&hdev->cmd_cnt, 1);
3040 if (!skb_queue_empty(&hdev->cmd_q))
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02003041 queue_work(hdev->workqueue, &hdev->cmd_work);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003042 }
3043}
3044
Marcel Holtmann24dfa342014-11-02 02:56:41 +01003045static void hci_hardware_error_evt(struct hci_dev *hdev, struct sk_buff *skb)
3046{
3047 struct hci_ev_hardware_error *ev = (void *) skb->data;
3048
3049 BT_ERR("%s hardware error 0x%2.2x", hdev->name, ev->code);
3050}
3051
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003052static void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003053{
3054 struct hci_ev_role_change *ev = (void *) skb->data;
3055 struct hci_conn *conn;
3056
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003057 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003058
3059 hci_dev_lock(hdev);
3060
3061 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3062 if (conn) {
Johan Hedberg40bef302014-07-16 11:42:27 +03003063 if (!ev->status)
3064 conn->role = ev->role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003065
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003066 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003067
3068 hci_role_switch_cfm(conn, ev->status, ev->role);
3069 }
3070
3071 hci_dev_unlock(hdev);
3072}
3073
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003074static void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003076 struct hci_ev_num_comp_pkts *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 int i;
3078
Andrei Emeltchenko32ac5b92011-12-19 16:31:29 +02003079 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_PACKET_BASED) {
3080 BT_ERR("Wrong event for mode %d", hdev->flow_ctl_mode);
3081 return;
3082 }
3083
Andrei Emeltchenkoc5993de2011-12-30 12:07:47 +02003084 if (skb->len < sizeof(*ev) || skb->len < sizeof(*ev) +
Gustavo Padovan807deac2012-05-17 00:36:24 -03003085 ev->num_hndl * sizeof(struct hci_comp_pkts_info)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 BT_DBG("%s bad parameters", hdev->name);
3087 return;
3088 }
3089
Andrei Emeltchenkoc5993de2011-12-30 12:07:47 +02003090 BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
3091
Andrei Emeltchenko613a1c02011-12-19 16:31:30 +02003092 for (i = 0; i < ev->num_hndl; i++) {
3093 struct hci_comp_pkts_info *info = &ev->handles[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 struct hci_conn *conn;
3095 __u16 handle, count;
3096
Andrei Emeltchenko613a1c02011-12-19 16:31:30 +02003097 handle = __le16_to_cpu(info->handle);
3098 count = __le16_to_cpu(info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
3100 conn = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02003101 if (!conn)
3102 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02003104 conn->sent -= count;
3105
3106 switch (conn->type) {
3107 case ACL_LINK:
3108 hdev->acl_cnt += count;
3109 if (hdev->acl_cnt > hdev->acl_pkts)
3110 hdev->acl_cnt = hdev->acl_pkts;
3111 break;
3112
3113 case LE_LINK:
3114 if (hdev->le_pkts) {
3115 hdev->le_cnt += count;
3116 if (hdev->le_cnt > hdev->le_pkts)
3117 hdev->le_cnt = hdev->le_pkts;
3118 } else {
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02003119 hdev->acl_cnt += count;
3120 if (hdev->acl_cnt > hdev->acl_pkts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 hdev->acl_cnt = hdev->acl_pkts;
3122 }
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02003123 break;
3124
3125 case SCO_LINK:
3126 hdev->sco_cnt += count;
3127 if (hdev->sco_cnt > hdev->sco_pkts)
3128 hdev->sco_cnt = hdev->sco_pkts;
3129 break;
3130
3131 default:
3132 BT_ERR("Unknown type %d conn %p", conn->type, conn);
3133 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 }
3135 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003136
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02003137 queue_work(hdev->workqueue, &hdev->tx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138}
3139
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003140static struct hci_conn *__hci_conn_lookup_handle(struct hci_dev *hdev,
3141 __u16 handle)
3142{
3143 struct hci_chan *chan;
3144
3145 switch (hdev->dev_type) {
3146 case HCI_BREDR:
3147 return hci_conn_hash_lookup_handle(hdev, handle);
3148 case HCI_AMP:
3149 chan = hci_chan_lookup_handle(hdev, handle);
3150 if (chan)
3151 return chan->conn;
3152 break;
3153 default:
3154 BT_ERR("%s unknown dev_type %d", hdev->name, hdev->dev_type);
3155 break;
3156 }
3157
3158 return NULL;
3159}
3160
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003161static void hci_num_comp_blocks_evt(struct hci_dev *hdev, struct sk_buff *skb)
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003162{
3163 struct hci_ev_num_comp_blocks *ev = (void *) skb->data;
3164 int i;
3165
3166 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_BLOCK_BASED) {
3167 BT_ERR("Wrong event for mode %d", hdev->flow_ctl_mode);
3168 return;
3169 }
3170
3171 if (skb->len < sizeof(*ev) || skb->len < sizeof(*ev) +
Gustavo Padovan807deac2012-05-17 00:36:24 -03003172 ev->num_hndl * sizeof(struct hci_comp_blocks_info)) {
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003173 BT_DBG("%s bad parameters", hdev->name);
3174 return;
3175 }
3176
3177 BT_DBG("%s num_blocks %d num_hndl %d", hdev->name, ev->num_blocks,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003178 ev->num_hndl);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003179
3180 for (i = 0; i < ev->num_hndl; i++) {
3181 struct hci_comp_blocks_info *info = &ev->handles[i];
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003182 struct hci_conn *conn = NULL;
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003183 __u16 handle, block_count;
3184
3185 handle = __le16_to_cpu(info->handle);
3186 block_count = __le16_to_cpu(info->blocks);
3187
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003188 conn = __hci_conn_lookup_handle(hdev, handle);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003189 if (!conn)
3190 continue;
3191
3192 conn->sent -= block_count;
3193
3194 switch (conn->type) {
3195 case ACL_LINK:
Andrei Emeltchenkobd1eb662012-10-10 17:38:30 +03003196 case AMP_LINK:
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003197 hdev->block_cnt += block_count;
3198 if (hdev->block_cnt > hdev->num_blocks)
3199 hdev->block_cnt = hdev->num_blocks;
3200 break;
3201
3202 default:
3203 BT_ERR("Unknown type %d conn %p", conn->type, conn);
3204 break;
3205 }
3206 }
3207
3208 queue_work(hdev->workqueue, &hdev->tx_work);
3209}
3210
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003211static void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003213 struct hci_ev_mode_change *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02003214 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003216 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
3218 hci_dev_lock(hdev);
3219
Marcel Holtmann04837f62006-07-03 10:02:33 +02003220 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3221 if (conn) {
3222 conn->mode = ev->mode;
Marcel Holtmann04837f62006-07-03 10:02:33 +02003223
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03003224 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND,
3225 &conn->flags)) {
Marcel Holtmann04837f62006-07-03 10:02:33 +02003226 if (conn->mode == HCI_CM_ACTIVE)
Johan Hedberg58a681e2012-01-16 06:47:28 +02003227 set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +02003228 else
Johan Hedberg58a681e2012-01-16 06:47:28 +02003229 clear_bit(HCI_CONN_POWER_SAVE, &conn->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +02003230 }
Marcel Holtmanne73439d2010-07-26 10:06:00 -04003231
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003232 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04003233 hci_sco_setup(conn, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02003234 }
3235
3236 hci_dev_unlock(hdev);
3237}
3238
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003239static void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003241 struct hci_ev_pin_code_req *ev = (void *) skb->data;
3242 struct hci_conn *conn;
3243
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003244 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003245
3246 hci_dev_lock(hdev);
3247
3248 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Waldemar Rymarkiewiczb6f98042011-09-23 10:01:30 +02003249 if (!conn)
3250 goto unlock;
3251
3252 if (conn->state == BT_CONNECTED) {
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003253 hci_conn_hold(conn);
3254 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02003255 hci_conn_drop(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003256 }
3257
Johan Hedbergb6ae8452014-07-30 09:22:22 +03003258 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
Johan Hedberg2f407f02014-07-17 15:35:40 +03003259 !test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags)) {
Johan Hedberg03b555e2011-01-04 15:40:05 +02003260 hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003261 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberg2f407f02014-07-17 15:35:40 +03003262 } else if (test_bit(HCI_MGMT, &hdev->dev_flags)) {
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02003263 u8 secure;
3264
3265 if (conn->pending_sec_level == BT_SECURITY_HIGH)
3266 secure = 1;
3267 else
3268 secure = 0;
3269
Johan Hedberg744cf192011-11-08 20:40:14 +02003270 mgmt_pin_code_request(hdev, &ev->bdaddr, secure);
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02003271 }
Johan Hedberg980e1a52011-01-22 06:10:07 +02003272
Waldemar Rymarkiewiczb6f98042011-09-23 10:01:30 +02003273unlock:
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003274 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275}
3276
Johan Hedbergcb6f3f72014-11-19 14:53:04 +02003277static void conn_set_key(struct hci_conn *conn, u8 key_type, u8 pin_len)
3278{
3279 if (key_type == HCI_LK_CHANGED_COMBINATION)
3280 return;
3281
3282 conn->pin_length = pin_len;
3283 conn->key_type = key_type;
3284
3285 switch (key_type) {
3286 case HCI_LK_LOCAL_UNIT:
3287 case HCI_LK_REMOTE_UNIT:
3288 case HCI_LK_DEBUG_COMBINATION:
3289 return;
3290 case HCI_LK_COMBINATION:
3291 if (pin_len == 16)
3292 conn->pending_sec_level = BT_SECURITY_HIGH;
3293 else
3294 conn->pending_sec_level = BT_SECURITY_MEDIUM;
3295 break;
3296 case HCI_LK_UNAUTH_COMBINATION_P192:
3297 case HCI_LK_UNAUTH_COMBINATION_P256:
3298 conn->pending_sec_level = BT_SECURITY_MEDIUM;
3299 break;
3300 case HCI_LK_AUTH_COMBINATION_P192:
3301 conn->pending_sec_level = BT_SECURITY_HIGH;
3302 break;
3303 case HCI_LK_AUTH_COMBINATION_P256:
3304 conn->pending_sec_level = BT_SECURITY_FIPS;
3305 break;
3306 }
3307}
3308
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003309static void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310{
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003311 struct hci_ev_link_key_req *ev = (void *) skb->data;
3312 struct hci_cp_link_key_reply cp;
3313 struct hci_conn *conn;
3314 struct link_key *key;
3315
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003316 BT_DBG("%s", hdev->name);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003317
Andrei Emeltchenko034cbea2013-05-14 11:44:16 +03003318 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003319 return;
3320
3321 hci_dev_lock(hdev);
3322
3323 key = hci_find_link_key(hdev, &ev->bdaddr);
3324 if (!key) {
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03003325 BT_DBG("%s link key not found for %pMR", hdev->name,
3326 &ev->bdaddr);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003327 goto not_found;
3328 }
3329
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03003330 BT_DBG("%s found key type %u for %pMR", hdev->name, key->type,
3331 &ev->bdaddr);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003332
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003333 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02003334 if (conn) {
Johan Hedbergfe8bc5a2014-08-14 12:33:17 +03003335 clear_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
3336
Marcel Holtmann66138ce2014-01-10 02:07:20 -08003337 if ((key->type == HCI_LK_UNAUTH_COMBINATION_P192 ||
3338 key->type == HCI_LK_UNAUTH_COMBINATION_P256) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03003339 conn->auth_type != 0xff && (conn->auth_type & 0x01)) {
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02003340 BT_DBG("%s ignoring unauthenticated key", hdev->name);
3341 goto not_found;
3342 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003343
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02003344 if (key->type == HCI_LK_COMBINATION && key->pin_len < 16 &&
Johan Hedbergf3fb0b52014-06-02 10:12:44 +03003345 (conn->pending_sec_level == BT_SECURITY_HIGH ||
3346 conn->pending_sec_level == BT_SECURITY_FIPS)) {
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03003347 BT_DBG("%s ignoring key unauthenticated for high security",
3348 hdev->name);
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02003349 goto not_found;
3350 }
3351
Johan Hedbergcb6f3f72014-11-19 14:53:04 +02003352 conn_set_key(conn, key->type, key->pin_len);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003353 }
3354
3355 bacpy(&cp.bdaddr, &ev->bdaddr);
Andrei Emeltchenko9b3b4462012-05-23 11:31:20 +03003356 memcpy(cp.link_key, key->val, HCI_LINK_KEY_SIZE);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003357
3358 hci_send_cmd(hdev, HCI_OP_LINK_KEY_REPLY, sizeof(cp), &cp);
3359
3360 hci_dev_unlock(hdev);
3361
3362 return;
3363
3364not_found:
3365 hci_send_cmd(hdev, HCI_OP_LINK_KEY_NEG_REPLY, 6, &ev->bdaddr);
3366 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367}
3368
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003369static void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003371 struct hci_ev_link_key_notify *ev = (void *) skb->data;
3372 struct hci_conn *conn;
Johan Hedberg7652ff62014-06-24 13:15:49 +03003373 struct link_key *key;
3374 bool persistent;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003375 u8 pin_len = 0;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003376
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003377 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003378
3379 hci_dev_lock(hdev);
3380
3381 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg82c13d42014-12-03 11:03:06 +02003382 if (!conn)
3383 goto unlock;
3384
3385 hci_conn_hold(conn);
3386 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
3387 hci_conn_drop(conn);
3388
Johan Hedbergfe8bc5a2014-08-14 12:33:17 +03003389 set_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
Johan Hedberg82c13d42014-12-03 11:03:06 +02003390 conn_set_key(conn, ev->key_type, conn->pin_length);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003391
Johan Hedberg7652ff62014-06-24 13:15:49 +03003392 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
3393 goto unlock;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003394
Johan Hedberg7652ff62014-06-24 13:15:49 +03003395 key = hci_add_link_key(hdev, conn, &ev->bdaddr, ev->link_key,
3396 ev->key_type, pin_len, &persistent);
3397 if (!key)
3398 goto unlock;
3399
Johan Hedbergcb6f3f72014-11-19 14:53:04 +02003400 /* Update connection information since adding the key will have
3401 * fixed up the type in the case of changed combination keys.
3402 */
3403 if (ev->key_type == HCI_LK_CHANGED_COMBINATION)
3404 conn_set_key(conn, key->type, key->pin_len);
3405
Johan Hedberg7652ff62014-06-24 13:15:49 +03003406 mgmt_new_link_key(hdev, key, persistent);
3407
Johan Hedberg6d5650c2014-06-24 13:15:51 +03003408 /* Keep debug keys around only if the HCI_KEEP_DEBUG_KEYS flag
3409 * is set. If it's not set simply remove the key from the kernel
3410 * list (we've still notified user space about it but with
3411 * store_hint being 0).
3412 */
3413 if (key->type == HCI_LK_DEBUG_COMBINATION &&
3414 !test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags)) {
Johan Hedberg0378b592014-11-19 15:22:22 +02003415 list_del_rcu(&key->list);
3416 kfree_rcu(key, rcu);
Johan Hedberg82c13d42014-12-03 11:03:06 +02003417 goto unlock;
Johan Hedberg6d5650c2014-06-24 13:15:51 +03003418 }
Johan Hedberg7652ff62014-06-24 13:15:49 +03003419
Johan Hedberg82c13d42014-12-03 11:03:06 +02003420 if (persistent)
3421 clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
3422 else
3423 set_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
3424
Johan Hedberg7652ff62014-06-24 13:15:49 +03003425unlock:
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003426 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427}
3428
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003429static void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann04837f62006-07-03 10:02:33 +02003430{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003431 struct hci_ev_clock_offset *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02003432 struct hci_conn *conn;
3433
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003434 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02003435
3436 hci_dev_lock(hdev);
3437
3438 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 if (conn && !ev->status) {
3440 struct inquiry_entry *ie;
3441
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02003442 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
3443 if (ie) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 ie->data.clock_offset = ev->clock_offset;
3445 ie->timestamp = jiffies;
3446 }
3447 }
3448
3449 hci_dev_unlock(hdev);
3450}
3451
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003452static void hci_pkt_type_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna8746412008-07-14 20:13:46 +02003453{
3454 struct hci_ev_pkt_type_change *ev = (void *) skb->data;
3455 struct hci_conn *conn;
3456
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003457 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna8746412008-07-14 20:13:46 +02003458
3459 hci_dev_lock(hdev);
3460
3461 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3462 if (conn && !ev->status)
3463 conn->pkt_type = __le16_to_cpu(ev->pkt_type);
3464
3465 hci_dev_unlock(hdev);
3466}
3467
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003468static void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann85a1e932005-08-09 20:28:02 -07003469{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003470 struct hci_ev_pscan_rep_mode *ev = (void *) skb->data;
Marcel Holtmann85a1e932005-08-09 20:28:02 -07003471 struct inquiry_entry *ie;
3472
3473 BT_DBG("%s", hdev->name);
3474
3475 hci_dev_lock(hdev);
3476
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02003477 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
3478 if (ie) {
Marcel Holtmann85a1e932005-08-09 20:28:02 -07003479 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
3480 ie->timestamp = jiffies;
3481 }
3482
3483 hci_dev_unlock(hdev);
3484}
3485
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003486static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev,
3487 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003488{
3489 struct inquiry_data data;
3490 int num_rsp = *((__u8 *) skb->data);
3491
3492 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
3493
3494 if (!num_rsp)
3495 return;
3496
Andre Guedes1519cc12012-03-21 00:03:38 -03003497 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags))
3498 return;
3499
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003500 hci_dev_lock(hdev);
3501
3502 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
Szymon Janc138d22e2011-02-17 16:44:23 +01003503 struct inquiry_info_with_rssi_and_pscan_mode *info;
3504 info = (void *) (skb->data + 1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003505
Johan Hedberge17acd42011-03-30 23:57:16 +03003506 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02003507 u32 flags;
3508
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003509 bacpy(&data.bdaddr, &info->bdaddr);
3510 data.pscan_rep_mode = info->pscan_rep_mode;
3511 data.pscan_period_mode = info->pscan_period_mode;
3512 data.pscan_mode = info->pscan_mode;
3513 memcpy(data.dev_class, info->dev_class, 3);
3514 data.clock_offset = info->clock_offset;
3515 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003516 data.ssp_mode = 0x00;
Johan Hedberg31754052012-01-04 13:39:52 +02003517
Marcel Holtmannaf589252014-07-01 14:11:20 +02003518 flags = hci_inquiry_cache_update(hdev, &data, false);
3519
Johan Hedberg48264f02011-11-09 13:58:58 +02003520 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003521 info->dev_class, info->rssi,
Marcel Holtmannaf589252014-07-01 14:11:20 +02003522 flags, NULL, 0, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003523 }
3524 } else {
3525 struct inquiry_info_with_rssi *info = (void *) (skb->data + 1);
3526
Johan Hedberge17acd42011-03-30 23:57:16 +03003527 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02003528 u32 flags;
3529
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003530 bacpy(&data.bdaddr, &info->bdaddr);
3531 data.pscan_rep_mode = info->pscan_rep_mode;
3532 data.pscan_period_mode = info->pscan_period_mode;
3533 data.pscan_mode = 0x00;
3534 memcpy(data.dev_class, info->dev_class, 3);
3535 data.clock_offset = info->clock_offset;
3536 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003537 data.ssp_mode = 0x00;
Marcel Holtmannaf589252014-07-01 14:11:20 +02003538
3539 flags = hci_inquiry_cache_update(hdev, &data, false);
3540
Johan Hedberg48264f02011-11-09 13:58:58 +02003541 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003542 info->dev_class, info->rssi,
Marcel Holtmannaf589252014-07-01 14:11:20 +02003543 flags, NULL, 0, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003544 }
3545 }
3546
3547 hci_dev_unlock(hdev);
3548}
3549
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003550static void hci_remote_ext_features_evt(struct hci_dev *hdev,
3551 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003552{
Marcel Holtmann41a96212008-07-14 20:13:48 +02003553 struct hci_ev_remote_ext_features *ev = (void *) skb->data;
3554 struct hci_conn *conn;
3555
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003556 BT_DBG("%s", hdev->name);
Marcel Holtmann41a96212008-07-14 20:13:48 +02003557
Marcel Holtmann41a96212008-07-14 20:13:48 +02003558 hci_dev_lock(hdev);
3559
3560 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02003561 if (!conn)
3562 goto unlock;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003563
Johan Hedbergcad718e2013-04-17 15:00:51 +03003564 if (ev->page < HCI_MAX_PAGES)
3565 memcpy(conn->features[ev->page], ev->features, 8);
3566
Johan Hedbergccd556f2010-11-10 17:11:51 +02003567 if (!ev->status && ev->page == 0x01) {
3568 struct inquiry_entry *ie;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003569
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02003570 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
3571 if (ie)
Johan Hedberg02b7cc62012-02-28 02:28:43 +02003572 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
Marcel Holtmann769be972008-07-14 20:13:49 +02003573
Jaganath Kanakkasserybbb0ead2013-04-16 20:16:30 +05303574 if (ev->features[0] & LMP_HOST_SSP) {
Johan Hedberg58a681e2012-01-16 06:47:28 +02003575 set_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
Jaganath Kanakkasserybbb0ead2013-04-16 20:16:30 +05303576 } else {
3577 /* It is mandatory by the Bluetooth specification that
3578 * Extended Inquiry Results are only used when Secure
3579 * Simple Pairing is enabled, but some devices violate
3580 * this.
3581 *
3582 * To make these devices work, the internal SSP
3583 * enabled flag needs to be cleared if the remote host
3584 * features do not indicate SSP support */
3585 clear_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
3586 }
Marcel Holtmanneb9a8f32014-01-15 22:37:38 -08003587
3588 if (ev->features[0] & LMP_HOST_SC)
3589 set_bit(HCI_CONN_SC_ENABLED, &conn->flags);
Marcel Holtmann41a96212008-07-14 20:13:48 +02003590 }
3591
Johan Hedbergccd556f2010-11-10 17:11:51 +02003592 if (conn->state != BT_CONFIG)
3593 goto unlock;
3594
Johan Hedberg671267b2012-05-12 16:11:50 -03003595 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02003596 struct hci_cp_remote_name_req cp;
3597 memset(&cp, 0, sizeof(cp));
3598 bacpy(&cp.bdaddr, &conn->dst);
3599 cp.pscan_rep_mode = 0x02;
3600 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
Johan Hedbergb644ba32012-01-17 21:48:47 +02003601 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00003602 mgmt_device_connected(hdev, conn, 0, NULL, 0);
Johan Hedberg392599b2010-11-18 22:22:28 +02003603
Johan Hedberg127178d2010-11-18 22:22:29 +02003604 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02003605 conn->state = BT_CONNECTED;
3606 hci_proto_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02003607 hci_conn_drop(conn);
Johan Hedbergccd556f2010-11-10 17:11:51 +02003608 }
3609
3610unlock:
Marcel Holtmann41a96212008-07-14 20:13:48 +02003611 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003612}
3613
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003614static void hci_sync_conn_complete_evt(struct hci_dev *hdev,
3615 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003616{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003617 struct hci_ev_sync_conn_complete *ev = (void *) skb->data;
3618 struct hci_conn *conn;
3619
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003620 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003621
3622 hci_dev_lock(hdev);
3623
3624 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02003625 if (!conn) {
3626 if (ev->link_type == ESCO_LINK)
3627 goto unlock;
3628
3629 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
3630 if (!conn)
3631 goto unlock;
3632
3633 conn->type = SCO_LINK;
3634 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003635
Marcel Holtmann732547f2009-04-19 19:14:14 +02003636 switch (ev->status) {
3637 case 0x00:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003638 conn->handle = __le16_to_cpu(ev->handle);
3639 conn->state = BT_CONNECTED;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02003640
3641 hci_conn_add_sysfs(conn);
Marcel Holtmann732547f2009-04-19 19:14:14 +02003642 break;
3643
Nick Pelly81218d22014-06-30 11:25:01 +05303644 case 0x10: /* Connection Accept Timeout */
Frédéric Dalleau1a4c9582013-08-19 14:24:02 +02003645 case 0x0d: /* Connection Rejected due to Limited Resources */
Stephen Coe705e5712010-02-16 11:29:44 -05003646 case 0x11: /* Unsupported Feature or Parameter Value */
Marcel Holtmann732547f2009-04-19 19:14:14 +02003647 case 0x1c: /* SCO interval rejected */
Nick Pelly1038a002010-02-03 11:42:26 -08003648 case 0x1a: /* Unsupported Remote Feature */
Marcel Holtmann732547f2009-04-19 19:14:14 +02003649 case 0x1f: /* Unspecified error */
Andrew Earl27539bc2014-03-10 10:31:04 +00003650 case 0x20: /* Unsupported LMP Parameter value */
Frédéric Dalleau2dea6322013-08-19 14:24:03 +02003651 if (conn->out) {
Marcel Holtmann732547f2009-04-19 19:14:14 +02003652 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
3653 (hdev->esco_type & EDR_ESCO_MASK);
Frédéric Dalleau2dea6322013-08-19 14:24:03 +02003654 if (hci_setup_sync(conn, conn->link->handle))
3655 goto unlock;
Marcel Holtmann732547f2009-04-19 19:14:14 +02003656 }
3657 /* fall through */
3658
3659 default:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003660 conn->state = BT_CLOSED;
Marcel Holtmann732547f2009-04-19 19:14:14 +02003661 break;
3662 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003663
3664 hci_proto_connect_cfm(conn, ev->status);
3665 if (ev->status)
3666 hci_conn_del(conn);
3667
3668unlock:
3669 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003670}
3671
Marcel Holtmannefdcf8e2013-10-15 10:31:12 -07003672static inline size_t eir_get_length(u8 *eir, size_t eir_len)
3673{
3674 size_t parsed = 0;
3675
3676 while (parsed < eir_len) {
3677 u8 field_len = eir[0];
3678
3679 if (field_len == 0)
3680 return parsed;
3681
3682 parsed += field_len + 1;
3683 eir += field_len + 1;
3684 }
3685
3686 return eir_len;
3687}
3688
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003689static void hci_extended_inquiry_result_evt(struct hci_dev *hdev,
3690 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003691{
3692 struct inquiry_data data;
3693 struct extended_inquiry_info *info = (void *) (skb->data + 1);
3694 int num_rsp = *((__u8 *) skb->data);
Vishal Agarwal9d939d92012-04-26 19:19:56 +05303695 size_t eir_len;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003696
3697 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
3698
3699 if (!num_rsp)
3700 return;
3701
Andre Guedes1519cc12012-03-21 00:03:38 -03003702 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags))
3703 return;
3704
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003705 hci_dev_lock(hdev);
3706
Johan Hedberge17acd42011-03-30 23:57:16 +03003707 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02003708 u32 flags;
3709 bool name_known;
Johan Hedberg561aafb2012-01-04 13:31:59 +02003710
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003711 bacpy(&data.bdaddr, &info->bdaddr);
Szymon Janc138d22e2011-02-17 16:44:23 +01003712 data.pscan_rep_mode = info->pscan_rep_mode;
3713 data.pscan_period_mode = info->pscan_period_mode;
3714 data.pscan_mode = 0x00;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003715 memcpy(data.dev_class, info->dev_class, 3);
Szymon Janc138d22e2011-02-17 16:44:23 +01003716 data.clock_offset = info->clock_offset;
3717 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003718 data.ssp_mode = 0x01;
Johan Hedberg561aafb2012-01-04 13:31:59 +02003719
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003720 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg4ddb1932012-01-15 20:04:43 +02003721 name_known = eir_has_data_type(info->data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003722 sizeof(info->data),
3723 EIR_NAME_COMPLETE);
Johan Hedberg561aafb2012-01-04 13:31:59 +02003724 else
3725 name_known = true;
3726
Marcel Holtmannaf589252014-07-01 14:11:20 +02003727 flags = hci_inquiry_cache_update(hdev, &data, name_known);
3728
Vishal Agarwal9d939d92012-04-26 19:19:56 +05303729 eir_len = eir_get_length(info->data, sizeof(info->data));
Marcel Holtmannaf589252014-07-01 14:11:20 +02003730
Johan Hedberg48264f02011-11-09 13:58:58 +02003731 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Marcel Holtmannaf589252014-07-01 14:11:20 +02003732 info->dev_class, info->rssi,
3733 flags, info->data, eir_len, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003734 }
3735
3736 hci_dev_unlock(hdev);
3737}
3738
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003739static void hci_key_refresh_complete_evt(struct hci_dev *hdev,
3740 struct sk_buff *skb)
3741{
3742 struct hci_ev_key_refresh_complete *ev = (void *) skb->data;
3743 struct hci_conn *conn;
3744
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003745 BT_DBG("%s status 0x%2.2x handle 0x%4.4x", hdev->name, ev->status,
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003746 __le16_to_cpu(ev->handle));
3747
3748 hci_dev_lock(hdev);
3749
3750 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3751 if (!conn)
3752 goto unlock;
3753
Johan Hedberg9eb1fbf2014-04-11 12:02:31 -07003754 /* For BR/EDR the necessary steps are taken through the
3755 * auth_complete event.
3756 */
3757 if (conn->type != LE_LINK)
3758 goto unlock;
3759
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003760 if (!ev->status)
3761 conn->sec_level = conn->pending_sec_level;
3762
3763 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
3764
3765 if (ev->status && conn->state == BT_CONNECTED) {
Andre Guedesbed71742013-01-30 11:50:56 -03003766 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
David Herrmann76a68ba2013-04-06 20:28:37 +02003767 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003768 goto unlock;
3769 }
3770
3771 if (conn->state == BT_CONFIG) {
3772 if (!ev->status)
3773 conn->state = BT_CONNECTED;
3774
3775 hci_proto_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02003776 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003777 } else {
3778 hci_auth_cfm(conn, ev->status);
3779
3780 hci_conn_hold(conn);
3781 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02003782 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003783 }
3784
3785unlock:
3786 hci_dev_unlock(hdev);
3787}
3788
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003789static u8 hci_get_auth_req(struct hci_conn *conn)
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003790{
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003791 /* If remote requests no-bonding follow that lead */
Mikel Astizacabae92013-06-28 10:56:28 +02003792 if (conn->remote_auth == HCI_AT_NO_BONDING ||
3793 conn->remote_auth == HCI_AT_NO_BONDING_MITM)
Waldemar Rymarkiewicz58797bf2011-04-28 12:07:58 +02003794 return conn->remote_auth | (conn->auth_type & 0x01);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003795
Mikel Astizb7f94c82014-04-08 14:21:31 +02003796 /* If both remote and local have enough IO capabilities, require
3797 * MITM protection
3798 */
3799 if (conn->remote_cap != HCI_IO_NO_INPUT_OUTPUT &&
3800 conn->io_capability != HCI_IO_NO_INPUT_OUTPUT)
3801 return conn->remote_auth | 0x01;
3802
Timo Mueller7e741702014-04-08 14:21:33 +02003803 /* No MITM protection possible so ignore remote requirement */
3804 return (conn->remote_auth & ~0x01) | (conn->auth_type & 0x01);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003805}
3806
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003807static void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann04936842008-07-14 20:13:48 +02003808{
3809 struct hci_ev_io_capa_request *ev = (void *) skb->data;
3810 struct hci_conn *conn;
3811
3812 BT_DBG("%s", hdev->name);
3813
3814 hci_dev_lock(hdev);
3815
3816 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003817 if (!conn)
3818 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02003819
Johan Hedberg03b555e2011-01-04 15:40:05 +02003820 hci_conn_hold(conn);
3821
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003822 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg03b555e2011-01-04 15:40:05 +02003823 goto unlock;
3824
Johan Hedberg2f407f02014-07-17 15:35:40 +03003825 /* Allow pairing if we're pairable, the initiators of the
3826 * pairing or if the remote is not requesting bonding.
3827 */
Johan Hedbergb6ae8452014-07-30 09:22:22 +03003828 if (test_bit(HCI_BONDABLE, &hdev->dev_flags) ||
Johan Hedberg2f407f02014-07-17 15:35:40 +03003829 test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags) ||
Gustavo Padovan807deac2012-05-17 00:36:24 -03003830 (conn->remote_auth & ~0x01) == HCI_AT_NO_BONDING) {
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003831 struct hci_cp_io_capability_reply cp;
3832
3833 bacpy(&cp.bdaddr, &ev->bdaddr);
Hemant Gupta7a7f1e72012-01-16 13:34:29 +05303834 /* Change the IO capability from KeyboardDisplay
3835 * to DisplayYesNo as it is not supported by BT spec. */
3836 cp.capability = (conn->io_capability == 0x04) ?
Mikel Astiza7676312013-06-28 10:56:29 +02003837 HCI_IO_DISPLAY_YESNO : conn->io_capability;
Mikel Astizb7f94c82014-04-08 14:21:31 +02003838
3839 /* If we are initiators, there is no remote information yet */
3840 if (conn->remote_auth == 0xff) {
Mikel Astizb16c6602014-04-08 14:21:34 +02003841 /* Request MITM protection if our IO caps allow it
Johan Hedberg4ad51a72014-06-09 14:41:25 +03003842 * except for the no-bonding case.
Mikel Astizb16c6602014-04-08 14:21:34 +02003843 */
Mikel Astiz6fd6b912014-04-08 14:21:32 +02003844 if (conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
Johan Hedberg9f743d72014-07-17 11:56:33 +03003845 conn->auth_type != HCI_AT_NO_BONDING)
Johan Hedberg6c538232014-07-11 15:32:23 +03003846 conn->auth_type |= 0x01;
Mikel Astizb7f94c82014-04-08 14:21:31 +02003847 } else {
3848 conn->auth_type = hci_get_auth_req(conn);
Mikel Astizb7f94c82014-04-08 14:21:31 +02003849 }
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003850
Johan Hedberg82c295b2014-07-30 09:22:24 +03003851 /* If we're not bondable, force one of the non-bondable
3852 * authentication requirement values.
3853 */
3854 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags))
3855 conn->auth_type &= HCI_AT_NO_BONDING_MITM;
3856
3857 cp.authentication = conn->auth_type;
3858
Johan Hedberg6928a922014-10-26 20:46:09 +01003859 if (hci_find_remote_oob_data(hdev, &conn->dst, BDADDR_BREDR) &&
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03003860 (conn->out || test_bit(HCI_CONN_REMOTE_OOB, &conn->flags)))
Szymon Jancce85ee12011-03-22 13:12:23 +01003861 cp.oob_data = 0x01;
3862 else
3863 cp.oob_data = 0x00;
3864
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003865 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003866 sizeof(cp), &cp);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003867 } else {
3868 struct hci_cp_io_capability_neg_reply cp;
3869
3870 bacpy(&cp.bdaddr, &ev->bdaddr);
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02003871 cp.reason = HCI_ERROR_PAIRING_NOT_ALLOWED;
Johan Hedberg03b555e2011-01-04 15:40:05 +02003872
3873 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003874 sizeof(cp), &cp);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003875 }
3876
3877unlock:
3878 hci_dev_unlock(hdev);
3879}
3880
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003881static void hci_io_capa_reply_evt(struct hci_dev *hdev, struct sk_buff *skb)
Johan Hedberg03b555e2011-01-04 15:40:05 +02003882{
3883 struct hci_ev_io_capa_reply *ev = (void *) skb->data;
3884 struct hci_conn *conn;
3885
3886 BT_DBG("%s", hdev->name);
3887
3888 hci_dev_lock(hdev);
3889
3890 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3891 if (!conn)
3892 goto unlock;
3893
Johan Hedberg03b555e2011-01-04 15:40:05 +02003894 conn->remote_cap = ev->capability;
Johan Hedberg03b555e2011-01-04 15:40:05 +02003895 conn->remote_auth = ev->authentication;
Johan Hedberg58a681e2012-01-16 06:47:28 +02003896 if (ev->oob_data)
3897 set_bit(HCI_CONN_REMOTE_OOB, &conn->flags);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003898
3899unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02003900 hci_dev_unlock(hdev);
3901}
3902
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003903static void hci_user_confirm_request_evt(struct hci_dev *hdev,
3904 struct sk_buff *skb)
Johan Hedberga5c29682011-02-19 12:05:57 -03003905{
3906 struct hci_ev_user_confirm_req *ev = (void *) skb->data;
Johan Hedberg55bc1a32011-04-28 11:28:56 -07003907 int loc_mitm, rem_mitm, confirm_hint = 0;
Johan Hedberg7a828902011-04-28 11:28:53 -07003908 struct hci_conn *conn;
Johan Hedberga5c29682011-02-19 12:05:57 -03003909
3910 BT_DBG("%s", hdev->name);
3911
3912 hci_dev_lock(hdev);
3913
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003914 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg7a828902011-04-28 11:28:53 -07003915 goto unlock;
Johan Hedberga5c29682011-02-19 12:05:57 -03003916
Johan Hedberg7a828902011-04-28 11:28:53 -07003917 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3918 if (!conn)
3919 goto unlock;
3920
3921 loc_mitm = (conn->auth_type & 0x01);
3922 rem_mitm = (conn->remote_auth & 0x01);
3923
3924 /* If we require MITM but the remote device can't provide that
Johan Hedberg6c538232014-07-11 15:32:23 +03003925 * (it has NoInputNoOutput) then reject the confirmation
3926 * request. We check the security level here since it doesn't
3927 * necessarily match conn->auth_type.
Mikel Astiz6fd6b912014-04-08 14:21:32 +02003928 */
Johan Hedberg6c538232014-07-11 15:32:23 +03003929 if (conn->pending_sec_level > BT_SECURITY_MEDIUM &&
3930 conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) {
Johan Hedberg7a828902011-04-28 11:28:53 -07003931 BT_DBG("Rejecting request: remote device can't provide MITM");
3932 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003933 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberg7a828902011-04-28 11:28:53 -07003934 goto unlock;
3935 }
3936
3937 /* If no side requires MITM protection; auto-accept */
Mikel Astiza7676312013-06-28 10:56:29 +02003938 if ((!loc_mitm || conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) &&
3939 (!rem_mitm || conn->io_capability == HCI_IO_NO_INPUT_OUTPUT)) {
Johan Hedberg55bc1a32011-04-28 11:28:56 -07003940
3941 /* If we're not the initiators request authorization to
3942 * proceed from user space (mgmt_user_confirm with
Johan Hedbergba15a582014-06-09 13:58:14 +03003943 * confirm_hint set to 1). The exception is if neither
Johan Hedberg02f3e252014-07-16 15:09:13 +03003944 * side had MITM or if the local IO capability is
3945 * NoInputNoOutput, in which case we do auto-accept
Johan Hedbergba15a582014-06-09 13:58:14 +03003946 */
3947 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) &&
Johan Hedberg02f3e252014-07-16 15:09:13 +03003948 conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
Johan Hedbergba15a582014-06-09 13:58:14 +03003949 (loc_mitm || rem_mitm)) {
Johan Hedberg55bc1a32011-04-28 11:28:56 -07003950 BT_DBG("Confirming auto-accept as acceptor");
3951 confirm_hint = 1;
3952 goto confirm;
3953 }
3954
Johan Hedberg9f616562011-04-28 11:28:54 -07003955 BT_DBG("Auto-accept of user confirmation with %ums delay",
Gustavo Padovan807deac2012-05-17 00:36:24 -03003956 hdev->auto_accept_delay);
Johan Hedberg9f616562011-04-28 11:28:54 -07003957
3958 if (hdev->auto_accept_delay > 0) {
3959 int delay = msecs_to_jiffies(hdev->auto_accept_delay);
Johan Hedberg7bc18d92013-10-16 18:11:39 +03003960 queue_delayed_work(conn->hdev->workqueue,
3961 &conn->auto_accept_work, delay);
Johan Hedberg9f616562011-04-28 11:28:54 -07003962 goto unlock;
3963 }
3964
Johan Hedberg7a828902011-04-28 11:28:53 -07003965 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003966 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberg7a828902011-04-28 11:28:53 -07003967 goto unlock;
3968 }
3969
Johan Hedberg55bc1a32011-04-28 11:28:56 -07003970confirm:
Johan Hedberg39adbff2014-03-20 08:18:14 +02003971 mgmt_user_confirm_request(hdev, &ev->bdaddr, ACL_LINK, 0,
3972 le32_to_cpu(ev->passkey), confirm_hint);
Johan Hedberg7a828902011-04-28 11:28:53 -07003973
3974unlock:
Johan Hedberga5c29682011-02-19 12:05:57 -03003975 hci_dev_unlock(hdev);
3976}
3977
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003978static void hci_user_passkey_request_evt(struct hci_dev *hdev,
3979 struct sk_buff *skb)
Brian Gix1143d452011-11-23 08:28:34 -08003980{
3981 struct hci_ev_user_passkey_req *ev = (void *) skb->data;
3982
3983 BT_DBG("%s", hdev->name);
3984
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003985 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg272d90d2012-02-09 15:26:12 +02003986 mgmt_user_passkey_request(hdev, &ev->bdaddr, ACL_LINK, 0);
Brian Gix1143d452011-11-23 08:28:34 -08003987}
3988
Johan Hedberg92a25252012-09-06 18:39:26 +03003989static void hci_user_passkey_notify_evt(struct hci_dev *hdev,
3990 struct sk_buff *skb)
3991{
3992 struct hci_ev_user_passkey_notify *ev = (void *) skb->data;
3993 struct hci_conn *conn;
3994
3995 BT_DBG("%s", hdev->name);
3996
3997 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3998 if (!conn)
3999 return;
4000
4001 conn->passkey_notify = __le32_to_cpu(ev->passkey);
4002 conn->passkey_entered = 0;
4003
4004 if (test_bit(HCI_MGMT, &hdev->dev_flags))
4005 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
4006 conn->dst_type, conn->passkey_notify,
4007 conn->passkey_entered);
4008}
4009
4010static void hci_keypress_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
4011{
4012 struct hci_ev_keypress_notify *ev = (void *) skb->data;
4013 struct hci_conn *conn;
4014
4015 BT_DBG("%s", hdev->name);
4016
4017 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4018 if (!conn)
4019 return;
4020
4021 switch (ev->type) {
4022 case HCI_KEYPRESS_STARTED:
4023 conn->passkey_entered = 0;
4024 return;
4025
4026 case HCI_KEYPRESS_ENTERED:
4027 conn->passkey_entered++;
4028 break;
4029
4030 case HCI_KEYPRESS_ERASED:
4031 conn->passkey_entered--;
4032 break;
4033
4034 case HCI_KEYPRESS_CLEARED:
4035 conn->passkey_entered = 0;
4036 break;
4037
4038 case HCI_KEYPRESS_COMPLETED:
4039 return;
4040 }
4041
4042 if (test_bit(HCI_MGMT, &hdev->dev_flags))
4043 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
4044 conn->dst_type, conn->passkey_notify,
4045 conn->passkey_entered);
4046}
4047
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004048static void hci_simple_pair_complete_evt(struct hci_dev *hdev,
4049 struct sk_buff *skb)
Marcel Holtmann04936842008-07-14 20:13:48 +02004050{
4051 struct hci_ev_simple_pair_complete *ev = (void *) skb->data;
4052 struct hci_conn *conn;
4053
4054 BT_DBG("%s", hdev->name);
4055
4056 hci_dev_lock(hdev);
4057
4058 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg2a611692011-02-19 12:06:00 -03004059 if (!conn)
4060 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02004061
Johan Hedbergc1d4fa72014-07-17 15:14:50 +03004062 /* Reset the authentication requirement to unknown */
4063 conn->remote_auth = 0xff;
4064
Johan Hedberg2a611692011-02-19 12:06:00 -03004065 /* To avoid duplicate auth_failed events to user space we check
4066 * the HCI_CONN_AUTH_PEND flag which will be set if we
4067 * initiated the authentication. A traditional auth_complete
4068 * event gets always produced as initiator and is also mapped to
4069 * the mgmt_auth_failed event */
Mikel Astizfa1bd912012-08-09 09:52:29 +02004070 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) && ev->status)
Johan Hedberge1e930f2014-09-08 17:09:49 -07004071 mgmt_auth_failed(conn, ev->status);
Johan Hedberg2a611692011-02-19 12:06:00 -03004072
David Herrmann76a68ba2013-04-06 20:28:37 +02004073 hci_conn_drop(conn);
Johan Hedberg2a611692011-02-19 12:06:00 -03004074
4075unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02004076 hci_dev_unlock(hdev);
4077}
4078
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004079static void hci_remote_host_features_evt(struct hci_dev *hdev,
4080 struct sk_buff *skb)
Marcel Holtmann41a96212008-07-14 20:13:48 +02004081{
4082 struct hci_ev_remote_host_features *ev = (void *) skb->data;
4083 struct inquiry_entry *ie;
Johan Hedbergcad718e2013-04-17 15:00:51 +03004084 struct hci_conn *conn;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004085
4086 BT_DBG("%s", hdev->name);
4087
4088 hci_dev_lock(hdev);
4089
Johan Hedbergcad718e2013-04-17 15:00:51 +03004090 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4091 if (conn)
4092 memcpy(conn->features[1], ev->features, 8);
4093
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02004094 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
4095 if (ie)
Johan Hedberg02b7cc62012-02-28 02:28:43 +02004096 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
Marcel Holtmann41a96212008-07-14 20:13:48 +02004097
4098 hci_dev_unlock(hdev);
4099}
4100
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004101static void hci_remote_oob_data_request_evt(struct hci_dev *hdev,
4102 struct sk_buff *skb)
Szymon Janc2763eda2011-03-22 13:12:22 +01004103{
4104 struct hci_ev_remote_oob_data_request *ev = (void *) skb->data;
4105 struct oob_data *data;
4106
4107 BT_DBG("%s", hdev->name);
4108
4109 hci_dev_lock(hdev);
4110
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02004111 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Szymon Jance1ba1f12011-04-06 13:01:59 +02004112 goto unlock;
4113
Johan Hedberg6928a922014-10-26 20:46:09 +01004114 data = hci_find_remote_oob_data(hdev, &ev->bdaddr, BDADDR_BREDR);
Szymon Janc2763eda2011-03-22 13:12:22 +01004115 if (data) {
Johan Hedberg710f11c2014-05-26 11:21:22 +03004116 if (bredr_sc_enabled(hdev)) {
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08004117 struct hci_cp_remote_oob_ext_data_reply cp;
Szymon Janc2763eda2011-03-22 13:12:22 +01004118
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08004119 bacpy(&cp.bdaddr, &ev->bdaddr);
4120 memcpy(cp.hash192, data->hash192, sizeof(cp.hash192));
Johan Hedberg38da1702014-11-17 20:52:20 +02004121 memcpy(cp.rand192, data->rand192, sizeof(cp.rand192));
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08004122 memcpy(cp.hash256, data->hash256, sizeof(cp.hash256));
Johan Hedberg38da1702014-11-17 20:52:20 +02004123 memcpy(cp.rand256, data->rand256, sizeof(cp.rand256));
Szymon Janc2763eda2011-03-22 13:12:22 +01004124
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08004125 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_EXT_DATA_REPLY,
4126 sizeof(cp), &cp);
4127 } else {
4128 struct hci_cp_remote_oob_data_reply cp;
4129
4130 bacpy(&cp.bdaddr, &ev->bdaddr);
4131 memcpy(cp.hash, data->hash192, sizeof(cp.hash));
Johan Hedberg38da1702014-11-17 20:52:20 +02004132 memcpy(cp.rand, data->rand192, sizeof(cp.rand));
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08004133
4134 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_REPLY,
4135 sizeof(cp), &cp);
4136 }
Szymon Janc2763eda2011-03-22 13:12:22 +01004137 } else {
4138 struct hci_cp_remote_oob_data_neg_reply cp;
4139
4140 bacpy(&cp.bdaddr, &ev->bdaddr);
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08004141 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_NEG_REPLY,
4142 sizeof(cp), &cp);
Szymon Janc2763eda2011-03-22 13:12:22 +01004143 }
4144
Szymon Jance1ba1f12011-04-06 13:01:59 +02004145unlock:
Szymon Janc2763eda2011-03-22 13:12:22 +01004146 hci_dev_unlock(hdev);
4147}
4148
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03004149static void hci_phy_link_complete_evt(struct hci_dev *hdev,
4150 struct sk_buff *skb)
4151{
4152 struct hci_ev_phy_link_complete *ev = (void *) skb->data;
4153 struct hci_conn *hcon, *bredr_hcon;
4154
4155 BT_DBG("%s handle 0x%2.2x status 0x%2.2x", hdev->name, ev->phy_handle,
4156 ev->status);
4157
4158 hci_dev_lock(hdev);
4159
4160 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
4161 if (!hcon) {
4162 hci_dev_unlock(hdev);
4163 return;
4164 }
4165
4166 if (ev->status) {
4167 hci_conn_del(hcon);
4168 hci_dev_unlock(hdev);
4169 return;
4170 }
4171
4172 bredr_hcon = hcon->amp_mgr->l2cap_conn->hcon;
4173
4174 hcon->state = BT_CONNECTED;
4175 bacpy(&hcon->dst, &bredr_hcon->dst);
4176
4177 hci_conn_hold(hcon);
4178 hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02004179 hci_conn_drop(hcon);
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03004180
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03004181 hci_conn_add_sysfs(hcon);
4182
Andrei Emeltchenkocf70ff22012-10-31 15:46:36 +02004183 amp_physical_cfm(bredr_hcon, hcon);
4184
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03004185 hci_dev_unlock(hdev);
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03004186}
4187
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03004188static void hci_loglink_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
4189{
4190 struct hci_ev_logical_link_complete *ev = (void *) skb->data;
4191 struct hci_conn *hcon;
4192 struct hci_chan *hchan;
4193 struct amp_mgr *mgr;
4194
4195 BT_DBG("%s log_handle 0x%4.4x phy_handle 0x%2.2x status 0x%2.2x",
4196 hdev->name, le16_to_cpu(ev->handle), ev->phy_handle,
4197 ev->status);
4198
4199 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
4200 if (!hcon)
4201 return;
4202
4203 /* Create AMP hchan */
4204 hchan = hci_chan_create(hcon);
4205 if (!hchan)
4206 return;
4207
4208 hchan->handle = le16_to_cpu(ev->handle);
4209
4210 BT_DBG("hcon %p mgr %p hchan %p", hcon, hcon->amp_mgr, hchan);
4211
4212 mgr = hcon->amp_mgr;
4213 if (mgr && mgr->bredr_chan) {
4214 struct l2cap_chan *bredr_chan = mgr->bredr_chan;
4215
4216 l2cap_chan_lock(bredr_chan);
4217
4218 bredr_chan->conn->mtu = hdev->block_mtu;
4219 l2cap_logical_cfm(bredr_chan, hchan, 0);
4220 hci_conn_hold(hcon);
4221
4222 l2cap_chan_unlock(bredr_chan);
4223 }
4224}
4225
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02004226static void hci_disconn_loglink_complete_evt(struct hci_dev *hdev,
4227 struct sk_buff *skb)
4228{
4229 struct hci_ev_disconn_logical_link_complete *ev = (void *) skb->data;
4230 struct hci_chan *hchan;
4231
4232 BT_DBG("%s log handle 0x%4.4x status 0x%2.2x", hdev->name,
4233 le16_to_cpu(ev->handle), ev->status);
4234
4235 if (ev->status)
4236 return;
4237
4238 hci_dev_lock(hdev);
4239
4240 hchan = hci_chan_lookup_handle(hdev, le16_to_cpu(ev->handle));
4241 if (!hchan)
4242 goto unlock;
4243
4244 amp_destroy_logical_link(hchan, ev->reason);
4245
4246unlock:
4247 hci_dev_unlock(hdev);
4248}
4249
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02004250static void hci_disconn_phylink_complete_evt(struct hci_dev *hdev,
4251 struct sk_buff *skb)
4252{
4253 struct hci_ev_disconn_phy_link_complete *ev = (void *) skb->data;
4254 struct hci_conn *hcon;
4255
4256 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
4257
4258 if (ev->status)
4259 return;
4260
4261 hci_dev_lock(hdev);
4262
4263 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
4264 if (hcon) {
4265 hcon->state = BT_CLOSED;
4266 hci_conn_del(hcon);
4267 }
4268
4269 hci_dev_unlock(hdev);
4270}
4271
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004272static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Ville Tervofcd89c02011-02-10 22:38:47 -03004273{
4274 struct hci_ev_le_conn_complete *ev = (void *) skb->data;
Johan Hedberg912b42ef2014-07-03 19:33:49 +03004275 struct hci_conn_params *params;
Ville Tervofcd89c02011-02-10 22:38:47 -03004276 struct hci_conn *conn;
Johan Hedberg68d6f6d2014-02-18 21:41:32 +02004277 struct smp_irk *irk;
Johan Hedberg837d5022014-07-02 09:36:22 +03004278 u8 addr_type;
Ville Tervofcd89c02011-02-10 22:38:47 -03004279
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004280 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Ville Tervofcd89c02011-02-10 22:38:47 -03004281
4282 hci_dev_lock(hdev);
4283
Johan Hedbergfbd96c12014-07-08 17:21:51 +03004284 /* All controllers implicitly stop advertising in the event of a
4285 * connection, so ensure that the state bit is cleared.
4286 */
4287 clear_bit(HCI_LE_ADV, &hdev->dev_flags);
4288
Andre Guedesb47a09b2012-07-27 15:10:15 -03004289 conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
Ville Tervob62f3282011-02-10 22:38:50 -03004290 if (!conn) {
Johan Hedberga5c4e302014-07-16 11:56:07 +03004291 conn = hci_conn_add(hdev, LE_LINK, &ev->bdaddr, ev->role);
Ville Tervob62f3282011-02-10 22:38:50 -03004292 if (!conn) {
4293 BT_ERR("No memory for new connection");
Andre Guedes230fd162012-07-27 15:10:10 -03004294 goto unlock;
Ville Tervob62f3282011-02-10 22:38:50 -03004295 }
Andre Guedes29b79882011-05-31 14:20:54 -03004296
4297 conn->dst_type = ev->bdaddr_type;
Andre Guedesb9b343d2012-07-27 15:10:11 -03004298
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02004299 /* If we didn't have a hci_conn object previously
4300 * but we're in master role this must be something
4301 * initiated using a white list. Since white list based
4302 * connections are not "first class citizens" we don't
4303 * have full tracking of them. Therefore, we go ahead
4304 * with a "best effort" approach of determining the
4305 * initiator address based on the HCI_PRIVACY flag.
4306 */
4307 if (conn->out) {
4308 conn->resp_addr_type = ev->bdaddr_type;
4309 bacpy(&conn->resp_addr, &ev->bdaddr);
4310 if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) {
4311 conn->init_addr_type = ADDR_LE_DEV_RANDOM;
4312 bacpy(&conn->init_addr, &hdev->rpa);
4313 } else {
4314 hci_copy_identity_address(hdev,
4315 &conn->init_addr,
4316 &conn->init_addr_type);
4317 }
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02004318 }
Johan Hedberg9489eca2014-02-28 17:45:46 +02004319 } else {
4320 cancel_delayed_work(&conn->le_conn_timeout);
Ville Tervob62f3282011-02-10 22:38:50 -03004321 }
Ville Tervofcd89c02011-02-10 22:38:47 -03004322
Johan Hedberg80c24ab2014-03-24 20:21:51 +02004323 if (!conn->out) {
4324 /* Set the responder (our side) address type based on
4325 * the advertising address type.
4326 */
4327 conn->resp_addr_type = hdev->adv_addr_type;
4328 if (hdev->adv_addr_type == ADDR_LE_DEV_RANDOM)
4329 bacpy(&conn->resp_addr, &hdev->random_addr);
4330 else
4331 bacpy(&conn->resp_addr, &hdev->bdaddr);
4332
4333 conn->init_addr_type = ev->bdaddr_type;
4334 bacpy(&conn->init_addr, &ev->bdaddr);
Marcel Holtmanna720d732014-06-23 12:14:51 +02004335
4336 /* For incoming connections, set the default minimum
4337 * and maximum connection interval. They will be used
4338 * to check if the parameters are in range and if not
4339 * trigger the connection update procedure.
4340 */
4341 conn->le_conn_min_interval = hdev->le_conn_min_interval;
4342 conn->le_conn_max_interval = hdev->le_conn_max_interval;
Johan Hedberg80c24ab2014-03-24 20:21:51 +02004343 }
Johan Hedberg7be2edb2014-02-23 19:42:17 +02004344
Marcel Holtmannedb4b462014-02-18 15:13:43 -08004345 /* Lookup the identity address from the stored connection
4346 * address and address type.
4347 *
4348 * When establishing connections to an identity address, the
4349 * connection procedure will store the resolvable random
4350 * address first. Now if it can be converted back into the
4351 * identity address, start using the identity address from
4352 * now on.
4353 */
4354 irk = hci_get_irk(hdev, &conn->dst, conn->dst_type);
Johan Hedberg68d6f6d2014-02-18 21:41:32 +02004355 if (irk) {
4356 bacpy(&conn->dst, &irk->bdaddr);
4357 conn->dst_type = irk->addr_type;
4358 }
4359
Johan Hedberg2d3c2262014-07-15 11:51:28 +03004360 if (ev->status) {
4361 hci_le_conn_failed(conn, ev->status);
Johan Hedberg837d5022014-07-02 09:36:22 +03004362 goto unlock;
4363 }
4364
Johan Hedberg08853f12014-08-15 21:06:55 +03004365 if (conn->dst_type == ADDR_LE_DEV_PUBLIC)
4366 addr_type = BDADDR_LE_PUBLIC;
4367 else
4368 addr_type = BDADDR_LE_RANDOM;
4369
Johan Hedberg2d3c2262014-07-15 11:51:28 +03004370 /* Drop the connection if the device is blocked */
4371 if (hci_bdaddr_list_lookup(&hdev->blacklist, &conn->dst, addr_type)) {
4372 hci_conn_drop(conn);
Andre Guedescd17dec2012-07-27 15:10:16 -03004373 goto unlock;
4374 }
4375
Johan Hedbergb644ba32012-01-17 21:48:47 +02004376 if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00004377 mgmt_device_connected(hdev, conn, 0, NULL, 0);
Vinicius Costa Gomes83bc71b2011-05-06 18:41:43 -03004378
Vinicius Costa Gomes7b5c0d52011-06-09 18:50:50 -03004379 conn->sec_level = BT_SECURITY_LOW;
Ville Tervofcd89c02011-02-10 22:38:47 -03004380 conn->handle = __le16_to_cpu(ev->handle);
4381 conn->state = BT_CONNECTED;
4382
Marcel Holtmanne04fde62014-06-23 11:40:04 +02004383 conn->le_conn_interval = le16_to_cpu(ev->interval);
4384 conn->le_conn_latency = le16_to_cpu(ev->latency);
4385 conn->le_supv_timeout = le16_to_cpu(ev->supervision_timeout);
4386
Ville Tervofcd89c02011-02-10 22:38:47 -03004387 hci_conn_add_sysfs(conn);
4388
4389 hci_proto_connect_cfm(conn, ev->status);
4390
Johan Hedberg54776102014-08-15 21:06:56 +03004391 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, &conn->dst,
4392 conn->dst_type);
Johan Hedbergf161dd42014-08-15 21:06:54 +03004393 if (params) {
Johan Hedberg95305ba2014-07-04 12:37:21 +03004394 list_del_init(&params->action);
Johan Hedbergf161dd42014-08-15 21:06:54 +03004395 if (params->conn) {
4396 hci_conn_drop(params->conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03004397 hci_conn_put(params->conn);
Johan Hedbergf161dd42014-08-15 21:06:54 +03004398 params->conn = NULL;
4399 }
4400 }
Andre Guedesa4790db2014-02-26 20:21:47 -03004401
Ville Tervofcd89c02011-02-10 22:38:47 -03004402unlock:
Johan Hedberg223683a52014-07-06 15:44:23 +03004403 hci_update_background_scan(hdev);
Ville Tervofcd89c02011-02-10 22:38:47 -03004404 hci_dev_unlock(hdev);
4405}
4406
Marcel Holtmann1855d922014-06-23 11:40:05 +02004407static void hci_le_conn_update_complete_evt(struct hci_dev *hdev,
4408 struct sk_buff *skb)
4409{
4410 struct hci_ev_le_conn_update_complete *ev = (void *) skb->data;
4411 struct hci_conn *conn;
4412
4413 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
4414
4415 if (ev->status)
4416 return;
4417
4418 hci_dev_lock(hdev);
4419
4420 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4421 if (conn) {
4422 conn->le_conn_interval = le16_to_cpu(ev->interval);
4423 conn->le_conn_latency = le16_to_cpu(ev->latency);
4424 conn->le_supv_timeout = le16_to_cpu(ev->supervision_timeout);
4425 }
4426
4427 hci_dev_unlock(hdev);
4428}
4429
Andre Guedesa4790db2014-02-26 20:21:47 -03004430/* This function requires the caller holds hdev->lock */
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004431static struct hci_conn *check_pending_le_conn(struct hci_dev *hdev,
4432 bdaddr_t *addr,
4433 u8 addr_type, u8 adv_type)
Andre Guedesa4790db2014-02-26 20:21:47 -03004434{
4435 struct hci_conn *conn;
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02004436 struct hci_conn_params *params;
Andre Guedesa4790db2014-02-26 20:21:47 -03004437
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004438 /* If the event is not connectable don't proceed further */
4439 if (adv_type != LE_ADV_IND && adv_type != LE_ADV_DIRECT_IND)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004440 return NULL;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004441
4442 /* Ignore if the device is blocked */
Johan Hedbergdcc36c12014-07-09 12:59:13 +03004443 if (hci_bdaddr_list_lookup(&hdev->blacklist, addr, addr_type))
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004444 return NULL;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004445
Johan Hedbergf99353c2014-07-16 11:56:09 +03004446 /* Most controller will fail if we try to create new connections
4447 * while we have an existing one in slave role.
4448 */
4449 if (hdev->conn_hash.le_num_slave > 0)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004450 return NULL;
Johan Hedbergf99353c2014-07-16 11:56:09 +03004451
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004452 /* If we're not connectable only connect devices that we have in
4453 * our pend_le_conns list.
4454 */
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02004455 params = hci_pend_le_action_lookup(&hdev->pend_le_conns,
4456 addr, addr_type);
4457 if (!params)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004458 return NULL;
Andre Guedesa4790db2014-02-26 20:21:47 -03004459
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02004460 switch (params->auto_connect) {
4461 case HCI_AUTO_CONN_DIRECT:
4462 /* Only devices advertising with ADV_DIRECT_IND are
4463 * triggering a connection attempt. This is allowing
4464 * incoming connections from slave devices.
4465 */
4466 if (adv_type != LE_ADV_DIRECT_IND)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004467 return NULL;
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02004468 break;
4469 case HCI_AUTO_CONN_ALWAYS:
4470 /* Devices advertising with ADV_IND or ADV_DIRECT_IND
4471 * are triggering a connection attempt. This means
4472 * that incoming connectioms from slave device are
4473 * accepted and also outgoing connections to slave
4474 * devices are established when found.
4475 */
4476 break;
4477 default:
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004478 return NULL;
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02004479 }
4480
Andre Guedesa4790db2014-02-26 20:21:47 -03004481 conn = hci_connect_le(hdev, addr, addr_type, BT_SECURITY_LOW,
Johan Hedberge804d252014-07-16 11:42:28 +03004482 HCI_LE_AUTOCONN_TIMEOUT, HCI_ROLE_MASTER);
Johan Hedbergf161dd42014-08-15 21:06:54 +03004483 if (!IS_ERR(conn)) {
4484 /* Store the pointer since we don't really have any
4485 * other owner of the object besides the params that
4486 * triggered it. This way we can abort the connection if
4487 * the parameters get removed and keep the reference
4488 * count consistent once the connection is established.
4489 */
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03004490 params->conn = hci_conn_get(conn);
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004491 return conn;
Johan Hedbergf161dd42014-08-15 21:06:54 +03004492 }
Andre Guedesa4790db2014-02-26 20:21:47 -03004493
4494 switch (PTR_ERR(conn)) {
4495 case -EBUSY:
4496 /* If hci_connect() returns -EBUSY it means there is already
4497 * an LE connection attempt going on. Since controllers don't
4498 * support more than one connection attempt at the time, we
4499 * don't consider this an error case.
4500 */
4501 break;
4502 default:
4503 BT_DBG("Failed to connect: err %ld", PTR_ERR(conn));
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004504 return NULL;
Andre Guedesa4790db2014-02-26 20:21:47 -03004505 }
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004506
4507 return NULL;
Andre Guedesa4790db2014-02-26 20:21:47 -03004508}
4509
Johan Hedberg4af605d2014-03-24 10:48:00 +02004510static void process_adv_report(struct hci_dev *hdev, u8 type, bdaddr_t *bdaddr,
Marcel Holtmann2f010b52014-12-05 16:20:13 +01004511 u8 bdaddr_type, bdaddr_t *direct_addr,
4512 u8 direct_addr_type, s8 rssi, u8 *data, u8 len)
Johan Hedberg4af605d2014-03-24 10:48:00 +02004513{
Johan Hedbergb9a63282014-03-25 10:51:52 +02004514 struct discovery_state *d = &hdev->discovery;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004515 struct smp_irk *irk;
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004516 struct hci_conn *conn;
Johan Hedberg474ee062014-03-25 14:34:59 +02004517 bool match;
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004518 u32 flags;
Johan Hedbergb9a63282014-03-25 10:51:52 +02004519
Marcel Holtmann2f010b52014-12-05 16:20:13 +01004520 /* If the direct address is present, then this report is from
4521 * a LE Direct Advertising Report event. In that case it is
4522 * important to see if the address is matching the local
4523 * controller address.
4524 */
4525 if (direct_addr) {
4526 /* Only resolvable random addresses are valid for these
4527 * kind of reports and others can be ignored.
4528 */
4529 if (!hci_bdaddr_is_rpa(direct_addr, direct_addr_type))
4530 return;
4531
4532 /* If the controller is not using resolvable random
4533 * addresses, then this report can be ignored.
4534 */
4535 if (!test_bit(HCI_PRIVACY, &hdev->dev_flags))
4536 return;
4537
4538 /* If the local IRK of the controller does not match
4539 * with the resolvable random address provided, then
4540 * this report can be ignored.
4541 */
4542 if (!smp_irk_matches(hdev, hdev->irk, direct_addr))
4543 return;
4544 }
4545
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004546 /* Check if we need to convert to identity address */
4547 irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
4548 if (irk) {
4549 bdaddr = &irk->bdaddr;
4550 bdaddr_type = irk->addr_type;
4551 }
4552
4553 /* Check if we have been requested to connect to this device */
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004554 conn = check_pending_le_conn(hdev, bdaddr, bdaddr_type, type);
4555 if (conn && type == LE_ADV_IND) {
4556 /* Store report for later inclusion by
4557 * mgmt_device_connected
4558 */
4559 memcpy(conn->le_adv_data, data, len);
4560 conn->le_adv_data_len = len;
4561 }
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004562
Johan Hedberg0d2bf132014-07-02 22:42:02 +03004563 /* Passive scanning shouldn't trigger any device found events,
4564 * except for devices marked as CONN_REPORT for which we do send
4565 * device found events.
4566 */
Johan Hedbergca5c4be2014-03-25 10:30:46 +02004567 if (hdev->le_scan_type == LE_SCAN_PASSIVE) {
Johan Hedberg0d2bf132014-07-02 22:42:02 +03004568 if (type == LE_ADV_DIRECT_IND)
4569 return;
4570
Johan Hedberg3a19b6f2014-07-15 08:07:59 +03004571 if (!hci_pend_le_action_lookup(&hdev->pend_le_reports,
4572 bdaddr, bdaddr_type))
Johan Hedberg0d2bf132014-07-02 22:42:02 +03004573 return;
4574
4575 if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND)
4576 flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
4577 else
4578 flags = 0;
4579 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
4580 rssi, flags, data, len, NULL, 0);
Johan Hedberg97bf2e92014-07-04 12:37:16 +03004581 return;
Johan Hedbergca5c4be2014-03-25 10:30:46 +02004582 }
Johan Hedberg4af605d2014-03-24 10:48:00 +02004583
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004584 /* When receiving non-connectable or scannable undirected
4585 * advertising reports, this means that the remote device is
4586 * not connectable and then clearly indicate this in the
4587 * device found event.
4588 *
4589 * When receiving a scan response, then there is no way to
4590 * know if the remote device is connectable or not. However
4591 * since scan responses are merged with a previously seen
4592 * advertising report, the flags field from that report
4593 * will be used.
4594 *
4595 * In the really unlikely case that a controller get confused
4596 * and just sends a scan response event, then it is marked as
4597 * not connectable as well.
4598 */
4599 if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND ||
4600 type == LE_ADV_SCAN_RSP)
4601 flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
4602 else
4603 flags = 0;
4604
Johan Hedbergb9a63282014-03-25 10:51:52 +02004605 /* If there's nothing pending either store the data from this
4606 * event or send an immediate device found event if the data
4607 * should not be stored for later.
4608 */
4609 if (!has_pending_adv_report(hdev)) {
4610 /* If the report will trigger a SCAN_REQ store it for
4611 * later merging.
4612 */
4613 if (type == LE_ADV_IND || type == LE_ADV_SCAN_IND) {
4614 store_pending_adv_report(hdev, bdaddr, bdaddr_type,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004615 rssi, flags, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004616 return;
4617 }
4618
4619 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004620 rssi, flags, data, len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004621 return;
4622 }
4623
Johan Hedberg474ee062014-03-25 14:34:59 +02004624 /* Check if the pending report is for the same device as the new one */
4625 match = (!bacmp(bdaddr, &d->last_adv_addr) &&
4626 bdaddr_type == d->last_adv_addr_type);
4627
Johan Hedbergb9a63282014-03-25 10:51:52 +02004628 /* If the pending data doesn't match this report or this isn't a
4629 * scan response (e.g. we got a duplicate ADV_IND) then force
4630 * sending of the pending data.
4631 */
Johan Hedberg474ee062014-03-25 14:34:59 +02004632 if (type != LE_ADV_SCAN_RSP || !match) {
4633 /* Send out whatever is in the cache, but skip duplicates */
4634 if (!match)
4635 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Johan Hedbergff5cd292014-03-25 14:40:52 +02004636 d->last_adv_addr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004637 d->last_adv_rssi, d->last_adv_flags,
Johan Hedbergff5cd292014-03-25 14:40:52 +02004638 d->last_adv_data,
Johan Hedberg474ee062014-03-25 14:34:59 +02004639 d->last_adv_data_len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004640
4641 /* If the new report will trigger a SCAN_REQ store it for
4642 * later merging.
4643 */
4644 if (type == LE_ADV_IND || type == LE_ADV_SCAN_IND) {
4645 store_pending_adv_report(hdev, bdaddr, bdaddr_type,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004646 rssi, flags, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004647 return;
4648 }
4649
4650 /* The advertising reports cannot be merged, so clear
4651 * the pending report and send out a device found event.
4652 */
4653 clear_pending_adv_report(hdev);
Johan Hedberg5c5b93e2014-03-29 08:39:53 +02004654 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004655 rssi, flags, data, len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004656 return;
4657 }
4658
4659 /* If we get here we've got a pending ADV_IND or ADV_SCAN_IND and
4660 * the new event is a SCAN_RSP. We can therefore proceed with
4661 * sending a merged device found event.
4662 */
4663 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004664 d->last_adv_addr_type, NULL, rssi, d->last_adv_flags,
Marcel Holtmann42bd6a52014-07-01 14:11:19 +02004665 d->last_adv_data, d->last_adv_data_len, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004666 clear_pending_adv_report(hdev);
Johan Hedberg4af605d2014-03-24 10:48:00 +02004667}
4668
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004669static void hci_le_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)
Andre Guedes9aa04c92011-05-26 16:23:51 -03004670{
Andre Guedese95beb42011-09-26 20:48:35 -03004671 u8 num_reports = skb->data[0];
4672 void *ptr = &skb->data[1];
Andre Guedes9aa04c92011-05-26 16:23:51 -03004673
Andre Guedesa4790db2014-02-26 20:21:47 -03004674 hci_dev_lock(hdev);
4675
Andre Guedese95beb42011-09-26 20:48:35 -03004676 while (num_reports--) {
4677 struct hci_ev_le_advertising_info *ev = ptr;
Johan Hedberg4af605d2014-03-24 10:48:00 +02004678 s8 rssi;
Andre Guedesa4790db2014-02-26 20:21:47 -03004679
Andre Guedes3c9e9192012-01-10 18:20:50 -03004680 rssi = ev->data[ev->length];
Johan Hedberg4af605d2014-03-24 10:48:00 +02004681 process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
Marcel Holtmann2f010b52014-12-05 16:20:13 +01004682 ev->bdaddr_type, NULL, 0, rssi,
4683 ev->data, ev->length);
Andre Guedes3c9e9192012-01-10 18:20:50 -03004684
Andre Guedese95beb42011-09-26 20:48:35 -03004685 ptr += sizeof(*ev) + ev->length + 1;
Andre Guedes9aa04c92011-05-26 16:23:51 -03004686 }
Andre Guedesa4790db2014-02-26 20:21:47 -03004687
4688 hci_dev_unlock(hdev);
Andre Guedes9aa04c92011-05-26 16:23:51 -03004689}
4690
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004691static void hci_le_ltk_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004692{
4693 struct hci_ev_le_ltk_req *ev = (void *) skb->data;
4694 struct hci_cp_le_ltk_reply cp;
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03004695 struct hci_cp_le_ltk_neg_reply neg;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004696 struct hci_conn *conn;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03004697 struct smp_ltk *ltk;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004698
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004699 BT_DBG("%s handle 0x%4.4x", hdev->name, __le16_to_cpu(ev->handle));
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004700
4701 hci_dev_lock(hdev);
4702
4703 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03004704 if (conn == NULL)
4705 goto not_found;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004706
Johan Hedbergf3a73d92014-05-29 15:02:59 +03004707 ltk = hci_find_ltk(hdev, &conn->dst, conn->dst_type, conn->role);
Johan Hedberg5378bc52014-05-29 14:00:39 +03004708 if (!ltk)
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03004709 goto not_found;
4710
Johan Hedberg5378bc52014-05-29 14:00:39 +03004711 if (smp_ltk_is_sc(ltk)) {
4712 /* With SC both EDiv and Rand are set to zero */
4713 if (ev->ediv || ev->rand)
4714 goto not_found;
4715 } else {
4716 /* For non-SC keys check that EDiv and Rand match */
4717 if (ev->ediv != ltk->ediv || ev->rand != ltk->rand)
4718 goto not_found;
4719 }
4720
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03004721 memcpy(cp.ltk, ltk->val, sizeof(ltk->val));
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004722 cp.handle = cpu_to_le16(conn->handle);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03004723
Johan Hedberga6f78332014-09-10 17:37:45 -07004724 conn->pending_sec_level = smp_ltk_sec_level(ltk);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004725
Andre Guedes89cbb4d2013-07-31 16:25:29 -03004726 conn->enc_key_size = ltk->enc_size;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004727
4728 hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
4729
Claudio Takahasi5981a882013-07-25 16:34:24 -03004730 /* Ref. Bluetooth Core SPEC pages 1975 and 2004. STK is a
4731 * temporary key used to encrypt a connection following
4732 * pairing. It is used during the Encrypted Session Setup to
4733 * distribute the keys. Later, security can be re-established
4734 * using a distributed LTK.
4735 */
Johan Hedberg2ceba532014-06-16 19:25:16 +03004736 if (ltk->type == SMP_STK) {
Johan Hedbergfe59a052014-07-01 19:14:12 +03004737 set_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
Johan Hedberg970d0f12014-11-13 14:37:47 +02004738 list_del_rcu(&ltk->list);
4739 kfree_rcu(ltk, rcu);
Johan Hedbergfe59a052014-07-01 19:14:12 +03004740 } else {
4741 clear_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03004742 }
4743
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004744 hci_dev_unlock(hdev);
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03004745
4746 return;
4747
4748not_found:
4749 neg.handle = ev->handle;
4750 hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(neg), &neg);
4751 hci_dev_unlock(hdev);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004752}
4753
Andre Guedes8e75b462014-07-01 18:10:08 -03004754static void send_conn_param_neg_reply(struct hci_dev *hdev, u16 handle,
4755 u8 reason)
4756{
4757 struct hci_cp_le_conn_param_req_neg_reply cp;
4758
4759 cp.handle = cpu_to_le16(handle);
4760 cp.reason = reason;
4761
4762 hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_NEG_REPLY, sizeof(cp),
4763 &cp);
4764}
4765
4766static void hci_le_remote_conn_param_req_evt(struct hci_dev *hdev,
4767 struct sk_buff *skb)
4768{
4769 struct hci_ev_le_remote_conn_param_req *ev = (void *) skb->data;
4770 struct hci_cp_le_conn_param_req_reply cp;
4771 struct hci_conn *hcon;
4772 u16 handle, min, max, latency, timeout;
4773
4774 handle = le16_to_cpu(ev->handle);
4775 min = le16_to_cpu(ev->interval_min);
4776 max = le16_to_cpu(ev->interval_max);
4777 latency = le16_to_cpu(ev->latency);
4778 timeout = le16_to_cpu(ev->timeout);
4779
4780 hcon = hci_conn_hash_lookup_handle(hdev, handle);
4781 if (!hcon || hcon->state != BT_CONNECTED)
4782 return send_conn_param_neg_reply(hdev, handle,
4783 HCI_ERROR_UNKNOWN_CONN_ID);
4784
4785 if (hci_check_conn_params(min, max, latency, timeout))
4786 return send_conn_param_neg_reply(hdev, handle,
4787 HCI_ERROR_INVALID_LL_PARAMS);
4788
Johan Hedberg40bef302014-07-16 11:42:27 +03004789 if (hcon->role == HCI_ROLE_MASTER) {
Johan Hedberg348d50b2014-07-02 17:37:30 +03004790 struct hci_conn_params *params;
Johan Hedbergf4869e22014-07-02 17:37:32 +03004791 u8 store_hint;
Johan Hedberg348d50b2014-07-02 17:37:30 +03004792
4793 hci_dev_lock(hdev);
4794
4795 params = hci_conn_params_lookup(hdev, &hcon->dst,
4796 hcon->dst_type);
4797 if (params) {
4798 params->conn_min_interval = min;
4799 params->conn_max_interval = max;
4800 params->conn_latency = latency;
4801 params->supervision_timeout = timeout;
Johan Hedbergf4869e22014-07-02 17:37:32 +03004802 store_hint = 0x01;
4803 } else{
4804 store_hint = 0x00;
Johan Hedberg348d50b2014-07-02 17:37:30 +03004805 }
4806
4807 hci_dev_unlock(hdev);
4808
Johan Hedbergf4869e22014-07-02 17:37:32 +03004809 mgmt_new_conn_param(hdev, &hcon->dst, hcon->dst_type,
4810 store_hint, min, max, latency, timeout);
Johan Hedberg348d50b2014-07-02 17:37:30 +03004811 }
Andre Guedesffb5a8272014-07-01 18:10:11 -03004812
Andre Guedes8e75b462014-07-01 18:10:08 -03004813 cp.handle = ev->handle;
4814 cp.interval_min = ev->interval_min;
4815 cp.interval_max = ev->interval_max;
4816 cp.latency = ev->latency;
4817 cp.timeout = ev->timeout;
4818 cp.min_ce_len = 0;
4819 cp.max_ce_len = 0;
4820
4821 hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_REPLY, sizeof(cp), &cp);
4822}
4823
Marcel Holtmann2f010b52014-12-05 16:20:13 +01004824static void hci_le_direct_adv_report_evt(struct hci_dev *hdev,
4825 struct sk_buff *skb)
4826{
4827 u8 num_reports = skb->data[0];
4828 void *ptr = &skb->data[1];
4829
4830 hci_dev_lock(hdev);
4831
4832 while (num_reports--) {
4833 struct hci_ev_le_direct_adv_info *ev = ptr;
4834
4835 process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
4836 ev->bdaddr_type, &ev->direct_addr,
4837 ev->direct_addr_type, ev->rssi, NULL, 0);
4838
4839 ptr += sizeof(*ev);
4840 }
4841
4842 hci_dev_unlock(hdev);
4843}
4844
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004845static void hci_le_meta_evt(struct hci_dev *hdev, struct sk_buff *skb)
Ville Tervofcd89c02011-02-10 22:38:47 -03004846{
4847 struct hci_ev_le_meta *le_ev = (void *) skb->data;
4848
4849 skb_pull(skb, sizeof(*le_ev));
4850
4851 switch (le_ev->subevent) {
4852 case HCI_EV_LE_CONN_COMPLETE:
4853 hci_le_conn_complete_evt(hdev, skb);
4854 break;
4855
Marcel Holtmann1855d922014-06-23 11:40:05 +02004856 case HCI_EV_LE_CONN_UPDATE_COMPLETE:
4857 hci_le_conn_update_complete_evt(hdev, skb);
4858 break;
4859
Andre Guedes9aa04c92011-05-26 16:23:51 -03004860 case HCI_EV_LE_ADVERTISING_REPORT:
4861 hci_le_adv_report_evt(hdev, skb);
4862 break;
4863
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004864 case HCI_EV_LE_LTK_REQ:
4865 hci_le_ltk_request_evt(hdev, skb);
4866 break;
4867
Andre Guedes8e75b462014-07-01 18:10:08 -03004868 case HCI_EV_LE_REMOTE_CONN_PARAM_REQ:
4869 hci_le_remote_conn_param_req_evt(hdev, skb);
4870 break;
4871
Marcel Holtmann2f010b52014-12-05 16:20:13 +01004872 case HCI_EV_LE_DIRECT_ADV_REPORT:
4873 hci_le_direct_adv_report_evt(hdev, skb);
4874 break;
4875
Ville Tervofcd89c02011-02-10 22:38:47 -03004876 default:
4877 break;
4878 }
4879}
4880
Andrei Emeltchenko9495b2e2012-09-27 17:26:22 +03004881static void hci_chan_selected_evt(struct hci_dev *hdev, struct sk_buff *skb)
4882{
4883 struct hci_ev_channel_selected *ev = (void *) skb->data;
4884 struct hci_conn *hcon;
4885
4886 BT_DBG("%s handle 0x%2.2x", hdev->name, ev->phy_handle);
4887
4888 skb_pull(skb, sizeof(*ev));
4889
4890 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
4891 if (!hcon)
4892 return;
4893
4894 amp_read_loc_assoc_final_data(hdev, hcon);
4895}
4896
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
4898{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004899 struct hci_event_hdr *hdr = (void *) skb->data;
4900 __u8 event = hdr->evt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901
Johan Hedbergb6ddb632013-04-02 13:34:31 +03004902 hci_dev_lock(hdev);
4903
4904 /* Received events are (currently) only needed when a request is
4905 * ongoing so avoid unnecessary memory allocation.
4906 */
Marcel Holtmann899de762014-07-11 05:51:58 +02004907 if (hci_req_pending(hdev)) {
Johan Hedbergb6ddb632013-04-02 13:34:31 +03004908 kfree_skb(hdev->recv_evt);
4909 hdev->recv_evt = skb_clone(skb, GFP_KERNEL);
4910 }
4911
4912 hci_dev_unlock(hdev);
4913
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 skb_pull(skb, HCI_EVENT_HDR_SIZE);
4915
Johan Hedberg02350a72013-04-03 21:50:29 +03004916 if (hdev->sent_cmd && bt_cb(hdev->sent_cmd)->req.event == event) {
Johannes Bergc1f23a22013-10-07 18:19:16 +02004917 struct hci_command_hdr *cmd_hdr = (void *) hdev->sent_cmd->data;
4918 u16 opcode = __le16_to_cpu(cmd_hdr->opcode);
Johan Hedberg02350a72013-04-03 21:50:29 +03004919
4920 hci_req_cmd_complete(hdev, opcode, 0);
4921 }
4922
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004923 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 case HCI_EV_INQUIRY_COMPLETE:
4925 hci_inquiry_complete_evt(hdev, skb);
4926 break;
4927
4928 case HCI_EV_INQUIRY_RESULT:
4929 hci_inquiry_result_evt(hdev, skb);
4930 break;
4931
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004932 case HCI_EV_CONN_COMPLETE:
4933 hci_conn_complete_evt(hdev, skb);
Marcel Holtmann21d9e302005-09-13 01:32:25 +02004934 break;
4935
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 case HCI_EV_CONN_REQUEST:
4937 hci_conn_request_evt(hdev, skb);
4938 break;
4939
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 case HCI_EV_DISCONN_COMPLETE:
4941 hci_disconn_complete_evt(hdev, skb);
4942 break;
4943
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 case HCI_EV_AUTH_COMPLETE:
4945 hci_auth_complete_evt(hdev, skb);
4946 break;
4947
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004948 case HCI_EV_REMOTE_NAME:
4949 hci_remote_name_evt(hdev, skb);
4950 break;
4951
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 case HCI_EV_ENCRYPT_CHANGE:
4953 hci_encrypt_change_evt(hdev, skb);
4954 break;
4955
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004956 case HCI_EV_CHANGE_LINK_KEY_COMPLETE:
4957 hci_change_link_key_complete_evt(hdev, skb);
4958 break;
4959
4960 case HCI_EV_REMOTE_FEATURES:
4961 hci_remote_features_evt(hdev, skb);
4962 break;
4963
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004964 case HCI_EV_CMD_COMPLETE:
4965 hci_cmd_complete_evt(hdev, skb);
4966 break;
4967
4968 case HCI_EV_CMD_STATUS:
4969 hci_cmd_status_evt(hdev, skb);
4970 break;
4971
Marcel Holtmann24dfa342014-11-02 02:56:41 +01004972 case HCI_EV_HARDWARE_ERROR:
4973 hci_hardware_error_evt(hdev, skb);
4974 break;
4975
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004976 case HCI_EV_ROLE_CHANGE:
4977 hci_role_change_evt(hdev, skb);
4978 break;
4979
4980 case HCI_EV_NUM_COMP_PKTS:
4981 hci_num_comp_pkts_evt(hdev, skb);
4982 break;
4983
4984 case HCI_EV_MODE_CHANGE:
4985 hci_mode_change_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 break;
4987
4988 case HCI_EV_PIN_CODE_REQ:
4989 hci_pin_code_request_evt(hdev, skb);
4990 break;
4991
4992 case HCI_EV_LINK_KEY_REQ:
4993 hci_link_key_request_evt(hdev, skb);
4994 break;
4995
4996 case HCI_EV_LINK_KEY_NOTIFY:
4997 hci_link_key_notify_evt(hdev, skb);
4998 break;
4999
5000 case HCI_EV_CLOCK_OFFSET:
5001 hci_clock_offset_evt(hdev, skb);
5002 break;
5003
Marcel Holtmanna8746412008-07-14 20:13:46 +02005004 case HCI_EV_PKT_TYPE_CHANGE:
5005 hci_pkt_type_change_evt(hdev, skb);
5006 break;
5007
Marcel Holtmann85a1e932005-08-09 20:28:02 -07005008 case HCI_EV_PSCAN_REP_MODE:
5009 hci_pscan_rep_mode_evt(hdev, skb);
5010 break;
5011
Marcel Holtmanna9de9242007-10-20 13:33:56 +02005012 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
5013 hci_inquiry_result_with_rssi_evt(hdev, skb);
5014 break;
5015
5016 case HCI_EV_REMOTE_EXT_FEATURES:
5017 hci_remote_ext_features_evt(hdev, skb);
5018 break;
5019
5020 case HCI_EV_SYNC_CONN_COMPLETE:
5021 hci_sync_conn_complete_evt(hdev, skb);
5022 break;
5023
Marcel Holtmanna9de9242007-10-20 13:33:56 +02005024 case HCI_EV_EXTENDED_INQUIRY_RESULT:
5025 hci_extended_inquiry_result_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 break;
5027
Johan Hedberg1c2e0042012-06-08 23:31:13 +08005028 case HCI_EV_KEY_REFRESH_COMPLETE:
5029 hci_key_refresh_complete_evt(hdev, skb);
5030 break;
5031
Marcel Holtmann04936842008-07-14 20:13:48 +02005032 case HCI_EV_IO_CAPA_REQUEST:
5033 hci_io_capa_request_evt(hdev, skb);
5034 break;
5035
Johan Hedberg03b555e2011-01-04 15:40:05 +02005036 case HCI_EV_IO_CAPA_REPLY:
5037 hci_io_capa_reply_evt(hdev, skb);
5038 break;
5039
Johan Hedberga5c29682011-02-19 12:05:57 -03005040 case HCI_EV_USER_CONFIRM_REQUEST:
5041 hci_user_confirm_request_evt(hdev, skb);
5042 break;
5043
Brian Gix1143d452011-11-23 08:28:34 -08005044 case HCI_EV_USER_PASSKEY_REQUEST:
5045 hci_user_passkey_request_evt(hdev, skb);
5046 break;
5047
Johan Hedberg92a25252012-09-06 18:39:26 +03005048 case HCI_EV_USER_PASSKEY_NOTIFY:
5049 hci_user_passkey_notify_evt(hdev, skb);
5050 break;
5051
5052 case HCI_EV_KEYPRESS_NOTIFY:
5053 hci_keypress_notify_evt(hdev, skb);
5054 break;
5055
Marcel Holtmann04936842008-07-14 20:13:48 +02005056 case HCI_EV_SIMPLE_PAIR_COMPLETE:
5057 hci_simple_pair_complete_evt(hdev, skb);
5058 break;
5059
Marcel Holtmann41a96212008-07-14 20:13:48 +02005060 case HCI_EV_REMOTE_HOST_FEATURES:
5061 hci_remote_host_features_evt(hdev, skb);
5062 break;
5063
Ville Tervofcd89c02011-02-10 22:38:47 -03005064 case HCI_EV_LE_META:
5065 hci_le_meta_evt(hdev, skb);
5066 break;
5067
Andrei Emeltchenko9495b2e2012-09-27 17:26:22 +03005068 case HCI_EV_CHANNEL_SELECTED:
5069 hci_chan_selected_evt(hdev, skb);
5070 break;
5071
Szymon Janc2763eda2011-03-22 13:12:22 +01005072 case HCI_EV_REMOTE_OOB_DATA_REQUEST:
5073 hci_remote_oob_data_request_evt(hdev, skb);
5074 break;
5075
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005076 case HCI_EV_PHY_LINK_COMPLETE:
5077 hci_phy_link_complete_evt(hdev, skb);
5078 break;
5079
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03005080 case HCI_EV_LOGICAL_LINK_COMPLETE:
5081 hci_loglink_complete_evt(hdev, skb);
5082 break;
5083
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02005084 case HCI_EV_DISCONN_LOGICAL_LINK_COMPLETE:
5085 hci_disconn_loglink_complete_evt(hdev, skb);
5086 break;
5087
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02005088 case HCI_EV_DISCONN_PHY_LINK_COMPLETE:
5089 hci_disconn_phylink_complete_evt(hdev, skb);
5090 break;
5091
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02005092 case HCI_EV_NUM_COMP_BLOCKS:
5093 hci_num_comp_blocks_evt(hdev, skb);
5094 break;
5095
Marcel Holtmanna9de9242007-10-20 13:33:56 +02005096 default:
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03005097 BT_DBG("%s event 0x%2.2x", hdev->name, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 break;
5099 }
5100
5101 kfree_skb(skb);
5102 hdev->stat.evt_rx++;
5103}