blob: 2022b43c7353ee98d7546d6c9e0ef67c43811d3f [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
Gustavo Padovan8c520a52012-05-23 04:04:22 -030027#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/unaligned.h>
29
30#include <net/bluetooth/bluetooth.h>
31#include <net/bluetooth/hci_core.h>
Mikel Astizf0d6a0e2012-08-09 09:52:30 +020032#include <net/bluetooth/mgmt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/* Handle HCI Event packets */
35
Marcel Holtmanna9de9242007-10-20 13:33:56 +020036static void hci_cc_inquiry_cancel(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Marcel Holtmanna9de9242007-10-20 13:33:56 +020038 __u8 status = *((__u8 *) skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030040 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Andre Guedese6d465c2011-11-09 17:14:26 -030042 if (status) {
43 hci_dev_lock(hdev);
44 mgmt_stop_discovery_failed(hdev, status);
45 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +020046 return;
Andre Guedese6d465c2011-11-09 17:14:26 -030047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Andre Guedes89352e72011-11-04 14:16:53 -030049 clear_bit(HCI_INQUIRY, &hdev->flags);
50
Johan Hedberg56e5cb82011-11-08 20:40:16 +020051 hci_dev_lock(hdev);
Johan Hedbergff9ef572012-01-04 14:23:45 +020052 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Johan Hedberg56e5cb82011-11-08 20:40:16 +020053 hci_dev_unlock(hdev);
Marcel Holtmann6bd57412006-11-18 22:14:22 +010054
Johan Hedberg23bb5762010-12-21 23:01:27 +020055 hci_req_complete(hdev, HCI_OP_INQUIRY_CANCEL, status);
Marcel Holtmann6bd57412006-11-18 22:14:22 +010056
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));
105 if (conn) {
106 if (rp->role)
107 conn->link_mode &= ~HCI_LM_MASTER;
108 else
109 conn->link_mode |= HCI_LM_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200111
112 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200115static void hci_cc_read_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
116{
117 struct hci_rp_read_link_policy *rp = (void *) skb->data;
118 struct hci_conn *conn;
119
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300120 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200121
122 if (rp->status)
123 return;
124
125 hci_dev_lock(hdev);
126
127 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
128 if (conn)
129 conn->link_policy = __le16_to_cpu(rp->policy);
130
131 hci_dev_unlock(hdev);
132}
133
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200134static void hci_cc_write_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200136 struct hci_rp_write_link_policy *rp = (void *) skb->data;
137 struct hci_conn *conn;
138 void *sent;
139
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300140 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200141
142 if (rp->status)
143 return;
144
145 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LINK_POLICY);
146 if (!sent)
147 return;
148
149 hci_dev_lock(hdev);
150
151 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200152 if (conn)
Harvey Harrison83985312008-05-02 16:25:46 -0700153 conn->link_policy = get_unaligned_le16(sent + 2);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200154
155 hci_dev_unlock(hdev);
156}
157
Gustavo Padovan807deac2012-05-17 00:36:24 -0300158static void hci_cc_read_def_link_policy(struct hci_dev *hdev,
159 struct sk_buff *skb)
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200160{
161 struct hci_rp_read_def_link_policy *rp = (void *) skb->data;
162
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300163 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200164
165 if (rp->status)
166 return;
167
168 hdev->link_policy = __le16_to_cpu(rp->policy);
169}
170
Gustavo Padovan807deac2012-05-17 00:36:24 -0300171static void hci_cc_write_def_link_policy(struct hci_dev *hdev,
172 struct sk_buff *skb)
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200173{
174 __u8 status = *((__u8 *) skb->data);
175 void *sent;
176
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300177 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200178
179 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_LINK_POLICY);
180 if (!sent)
181 return;
182
183 if (!status)
184 hdev->link_policy = get_unaligned_le16(sent);
185
Johan Hedberg23bb5762010-12-21 23:01:27 +0200186 hci_req_complete(hdev, HCI_OP_WRITE_DEF_LINK_POLICY, status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200187}
188
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200189static void hci_cc_reset(struct hci_dev *hdev, struct sk_buff *skb)
190{
191 __u8 status = *((__u8 *) skb->data);
192
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300193 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200194
Gustavo F. Padovan10572132011-03-16 15:36:29 -0300195 clear_bit(HCI_RESET, &hdev->flags);
196
Johan Hedberg23bb5762010-12-21 23:01:27 +0200197 hci_req_complete(hdev, HCI_OP_RESET, status);
Andre Guedesd23264a2011-11-25 20:53:38 -0300198
Johan Hedberga297e972012-02-21 17:55:47 +0200199 /* Reset all non-persistent flags */
Andre Guedesae854a72012-03-21 00:03:36 -0300200 hdev->dev_flags &= ~(BIT(HCI_LE_SCAN) | BIT(HCI_PENDING_CLASS) |
201 BIT(HCI_PERIODIC_INQ));
Andre Guedes69775ff2012-02-23 16:50:05 +0200202
203 hdev->discovery.state = DISCOVERY_STOPPED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200204}
205
206static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb)
207{
208 __u8 status = *((__u8 *) skb->data);
209 void *sent;
210
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300211 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200212
213 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LOCAL_NAME);
214 if (!sent)
215 return;
216
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200217 hci_dev_lock(hdev);
218
Johan Hedbergf51d5b22012-02-22 18:17:32 +0200219 if (test_bit(HCI_MGMT, &hdev->dev_flags))
220 mgmt_set_local_name_complete(hdev, sent, status);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +0200221 else if (!status)
222 memcpy(hdev->dev_name, sent, HCI_MAX_NAME_LENGTH);
Johan Hedbergf51d5b22012-02-22 18:17:32 +0200223
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200224 hci_dev_unlock(hdev);
Johan Hedberg3159d382012-02-24 13:47:56 +0200225
226 hci_req_complete(hdev, HCI_OP_WRITE_LOCAL_NAME, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200227}
228
229static void hci_cc_read_local_name(struct hci_dev *hdev, struct sk_buff *skb)
230{
231 struct hci_rp_read_local_name *rp = (void *) skb->data;
232
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300233 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200234
235 if (rp->status)
236 return;
237
Johan Hedbergdb99b5f2012-02-22 20:14:22 +0200238 if (test_bit(HCI_SETUP, &hdev->dev_flags))
239 memcpy(hdev->dev_name, rp->name, HCI_MAX_NAME_LENGTH);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200240}
241
242static void hci_cc_write_auth_enable(struct hci_dev *hdev, struct sk_buff *skb)
243{
244 __u8 status = *((__u8 *) skb->data);
245 void *sent;
246
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300247 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200248
249 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_ENABLE);
250 if (!sent)
251 return;
252
253 if (!status) {
254 __u8 param = *((__u8 *) sent);
255
256 if (param == AUTH_ENABLED)
257 set_bit(HCI_AUTH, &hdev->flags);
258 else
259 clear_bit(HCI_AUTH, &hdev->flags);
260 }
261
Johan Hedberg33ef95e2012-02-16 23:56:27 +0200262 if (test_bit(HCI_MGMT, &hdev->dev_flags))
263 mgmt_auth_enable_complete(hdev, status);
264
Johan Hedberg23bb5762010-12-21 23:01:27 +0200265 hci_req_complete(hdev, HCI_OP_WRITE_AUTH_ENABLE, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200266}
267
268static void hci_cc_write_encrypt_mode(struct hci_dev *hdev, struct sk_buff *skb)
269{
270 __u8 status = *((__u8 *) skb->data);
271 void *sent;
272
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300273 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200274
275 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_ENCRYPT_MODE);
276 if (!sent)
277 return;
278
279 if (!status) {
280 __u8 param = *((__u8 *) sent);
281
282 if (param)
283 set_bit(HCI_ENCRYPT, &hdev->flags);
284 else
285 clear_bit(HCI_ENCRYPT, &hdev->flags);
286 }
287
Johan Hedberg23bb5762010-12-21 23:01:27 +0200288 hci_req_complete(hdev, HCI_OP_WRITE_ENCRYPT_MODE, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200289}
290
291static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
292{
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200293 __u8 param, status = *((__u8 *) skb->data);
294 int old_pscan, old_iscan;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200295 void *sent;
296
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300297 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200298
299 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SCAN_ENABLE);
300 if (!sent)
301 return;
302
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200303 param = *((__u8 *) sent);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200304
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200305 hci_dev_lock(hdev);
306
Mikel Astizfa1bd912012-08-09 09:52:29 +0200307 if (status) {
Johan Hedberg744cf192011-11-08 20:40:14 +0200308 mgmt_write_scan_failed(hdev, param, status);
Johan Hedberg2d7cee52011-11-07 22:16:03 +0200309 hdev->discov_timeout = 0;
310 goto done;
311 }
312
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200313 old_pscan = test_and_clear_bit(HCI_PSCAN, &hdev->flags);
314 old_iscan = test_and_clear_bit(HCI_ISCAN, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200315
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200316 if (param & SCAN_INQUIRY) {
317 set_bit(HCI_ISCAN, &hdev->flags);
318 if (!old_iscan)
Johan Hedberg744cf192011-11-08 20:40:14 +0200319 mgmt_discoverable(hdev, 1);
Johan Hedberg16ab91a2011-11-07 22:16:02 +0200320 if (hdev->discov_timeout > 0) {
321 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
322 queue_delayed_work(hdev->workqueue, &hdev->discov_off,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300323 to);
Johan Hedberg16ab91a2011-11-07 22:16:02 +0200324 }
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200325 } else if (old_iscan)
Johan Hedberg744cf192011-11-08 20:40:14 +0200326 mgmt_discoverable(hdev, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200327
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200328 if (param & SCAN_PAGE) {
329 set_bit(HCI_PSCAN, &hdev->flags);
330 if (!old_pscan)
Johan Hedberg744cf192011-11-08 20:40:14 +0200331 mgmt_connectable(hdev, 1);
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200332 } else if (old_pscan)
Johan Hedberg744cf192011-11-08 20:40:14 +0200333 mgmt_connectable(hdev, 0);
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200334
335done:
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200336 hci_dev_unlock(hdev);
Johan Hedberg23bb5762010-12-21 23:01:27 +0200337 hci_req_complete(hdev, HCI_OP_WRITE_SCAN_ENABLE, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200338}
339
340static void hci_cc_read_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
341{
342 struct hci_rp_read_class_of_dev *rp = (void *) skb->data;
343
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300344 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200345
346 if (rp->status)
347 return;
348
349 memcpy(hdev->dev_class, rp->dev_class, 3);
350
351 BT_DBG("%s class 0x%.2x%.2x%.2x", hdev->name,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300352 hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200353}
354
355static void hci_cc_write_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
356{
357 __u8 status = *((__u8 *) skb->data);
358 void *sent;
359
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300360 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200361
362 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_CLASS_OF_DEV);
363 if (!sent)
364 return;
365
Marcel Holtmann7f9a9032012-02-22 18:38:01 +0100366 hci_dev_lock(hdev);
367
368 if (status == 0)
369 memcpy(hdev->dev_class, sent, 3);
370
371 if (test_bit(HCI_MGMT, &hdev->dev_flags))
372 mgmt_set_class_of_dev_complete(hdev, sent, status);
373
374 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200375}
376
377static void hci_cc_read_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
378{
379 struct hci_rp_read_voice_setting *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 __u16 setting;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200381
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300382 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200383
384 if (rp->status)
385 return;
386
387 setting = __le16_to_cpu(rp->voice_setting);
388
Marcel Holtmannf383f272008-07-14 20:13:47 +0200389 if (hdev->voice_setting == setting)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200390 return;
391
392 hdev->voice_setting = setting;
393
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300394 BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200395
Gustavo F. Padovan3c547112011-12-14 22:58:44 -0200396 if (hdev->notify)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200397 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200398}
399
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300400static void hci_cc_write_voice_setting(struct hci_dev *hdev,
401 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200402{
403 __u8 status = *((__u8 *) skb->data);
Marcel Holtmannf383f272008-07-14 20:13:47 +0200404 __u16 setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 void *sent;
406
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300407 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Marcel Holtmannf383f272008-07-14 20:13:47 +0200409 if (status)
410 return;
411
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200412 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_VOICE_SETTING);
413 if (!sent)
414 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Marcel Holtmannf383f272008-07-14 20:13:47 +0200416 setting = get_unaligned_le16(sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Marcel Holtmannf383f272008-07-14 20:13:47 +0200418 if (hdev->voice_setting == setting)
419 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Marcel Holtmannf383f272008-07-14 20:13:47 +0200421 hdev->voice_setting = setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300423 BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
Marcel Holtmannf383f272008-07-14 20:13:47 +0200424
Gustavo F. Padovan3c547112011-12-14 22:58:44 -0200425 if (hdev->notify)
Marcel Holtmannf383f272008-07-14 20:13:47 +0200426 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200429static void hci_cc_host_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200431 __u8 status = *((__u8 *) skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300433 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Johan Hedberg23bb5762010-12-21 23:01:27 +0200435 hci_req_complete(hdev, HCI_OP_HOST_BUFFER_SIZE, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
Marcel Holtmann333140b2008-07-14 20:13:48 +0200438static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
439{
440 __u8 status = *((__u8 *) skb->data);
441 void *sent;
442
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300443 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann333140b2008-07-14 20:13:48 +0200444
Marcel Holtmann333140b2008-07-14 20:13:48 +0200445 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_MODE);
446 if (!sent)
447 return;
448
Johan Hedberged2c4ee2012-02-17 00:56:28 +0200449 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedbergc0ecddc2012-02-22 12:38:31 +0200450 mgmt_ssp_enable_complete(hdev, *((u8 *) sent), status);
451 else if (!status) {
452 if (*((u8 *) sent))
453 set_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
454 else
455 clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
456 }
Marcel Holtmann333140b2008-07-14 20:13:48 +0200457}
458
Johan Hedbergd5859e22011-01-25 01:19:58 +0200459static u8 hci_get_inquiry_mode(struct hci_dev *hdev)
460{
461 if (hdev->features[6] & LMP_EXT_INQ)
462 return 2;
463
464 if (hdev->features[3] & LMP_RSSI_INQ)
465 return 1;
466
467 if (hdev->manufacturer == 11 && hdev->hci_rev == 0x00 &&
Gustavo Padovan807deac2012-05-17 00:36:24 -0300468 hdev->lmp_subver == 0x0757)
Johan Hedbergd5859e22011-01-25 01:19:58 +0200469 return 1;
470
471 if (hdev->manufacturer == 15) {
472 if (hdev->hci_rev == 0x03 && hdev->lmp_subver == 0x6963)
473 return 1;
474 if (hdev->hci_rev == 0x09 && hdev->lmp_subver == 0x6963)
475 return 1;
476 if (hdev->hci_rev == 0x00 && hdev->lmp_subver == 0x6965)
477 return 1;
478 }
479
480 if (hdev->manufacturer == 31 && hdev->hci_rev == 0x2005 &&
Gustavo Padovan807deac2012-05-17 00:36:24 -0300481 hdev->lmp_subver == 0x1805)
Johan Hedbergd5859e22011-01-25 01:19:58 +0200482 return 1;
483
484 return 0;
485}
486
487static void hci_setup_inquiry_mode(struct hci_dev *hdev)
488{
489 u8 mode;
490
491 mode = hci_get_inquiry_mode(hdev);
492
493 hci_send_cmd(hdev, HCI_OP_WRITE_INQUIRY_MODE, 1, &mode);
494}
495
496static void hci_setup_event_mask(struct hci_dev *hdev)
497{
498 /* The second byte is 0xff instead of 0x9f (two reserved bits
499 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
500 * command otherwise */
501 u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
502
Ville Tervo6de6c182011-05-27 11:16:21 +0300503 /* CSR 1.1 dongles does not accept any bitfield so don't try to set
504 * any event mask for pre 1.2 devices */
Andrei Emeltchenko5a13b092011-12-01 14:33:28 +0200505 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
Ville Tervo6de6c182011-05-27 11:16:21 +0300506 return;
507
508 events[4] |= 0x01; /* Flow Specification Complete */
509 events[4] |= 0x02; /* Inquiry Result with RSSI */
510 events[4] |= 0x04; /* Read Remote Extended Features Complete */
511 events[5] |= 0x08; /* Synchronous Connection Complete */
512 events[5] |= 0x10; /* Synchronous Connection Changed */
Johan Hedbergd5859e22011-01-25 01:19:58 +0200513
514 if (hdev->features[3] & LMP_RSSI_INQ)
Johan Hedberga24299e2012-04-26 09:47:46 +0300515 events[4] |= 0x02; /* Inquiry Result with RSSI */
Johan Hedbergd5859e22011-01-25 01:19:58 +0200516
Andre Guedes999dcd12012-07-24 15:03:52 -0300517 if (lmp_sniffsubr_capable(hdev))
Johan Hedbergd5859e22011-01-25 01:19:58 +0200518 events[5] |= 0x20; /* Sniff Subrating */
519
520 if (hdev->features[5] & LMP_PAUSE_ENC)
521 events[5] |= 0x80; /* Encryption Key Refresh Complete */
522
523 if (hdev->features[6] & LMP_EXT_INQ)
524 events[5] |= 0x40; /* Extended Inquiry Result */
525
Andre Guedesc58e8102012-07-24 15:03:53 -0300526 if (lmp_no_flush_capable(hdev))
Johan Hedbergd5859e22011-01-25 01:19:58 +0200527 events[7] |= 0x01; /* Enhanced Flush Complete */
528
529 if (hdev->features[7] & LMP_LSTO)
530 events[6] |= 0x80; /* Link Supervision Timeout Changed */
531
Andre Guedes9a1a1992012-07-24 15:03:48 -0300532 if (lmp_ssp_capable(hdev)) {
Johan Hedbergd5859e22011-01-25 01:19:58 +0200533 events[6] |= 0x01; /* IO Capability Request */
534 events[6] |= 0x02; /* IO Capability Response */
535 events[6] |= 0x04; /* User Confirmation Request */
536 events[6] |= 0x08; /* User Passkey Request */
537 events[6] |= 0x10; /* Remote OOB Data Request */
538 events[6] |= 0x20; /* Simple Pairing Complete */
539 events[7] |= 0x04; /* User Passkey Notification */
540 events[7] |= 0x08; /* Keypress Notification */
541 events[7] |= 0x10; /* Remote Host Supported
542 * Features Notification */
543 }
544
Andre Guedesc383ddc2012-07-24 15:03:47 -0300545 if (lmp_le_capable(hdev))
Johan Hedbergd5859e22011-01-25 01:19:58 +0200546 events[7] |= 0x20; /* LE Meta-Event */
547
548 hci_send_cmd(hdev, HCI_OP_SET_EVENT_MASK, sizeof(events), events);
549}
550
551static void hci_setup(struct hci_dev *hdev)
552{
Andrei Emeltchenkoe61ef4992011-12-19 16:31:27 +0200553 if (hdev->dev_type != HCI_BREDR)
554 return;
555
Johan Hedbergd5859e22011-01-25 01:19:58 +0200556 hci_setup_event_mask(hdev);
557
Andrei Emeltchenkod095c1e2011-12-01 14:33:27 +0200558 if (hdev->hci_ver > BLUETOOTH_VER_1_1)
Johan Hedbergd5859e22011-01-25 01:19:58 +0200559 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL);
560
Gustavo Padovan6d3c7302012-05-24 03:36:37 -0300561 if (lmp_ssp_capable(hdev)) {
Johan Hedberg54d04db2012-02-22 15:47:48 +0200562 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
563 u8 mode = 0x01;
564 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300565 sizeof(mode), &mode);
Johan Hedberg54d04db2012-02-22 15:47:48 +0200566 } else {
567 struct hci_cp_write_eir cp;
568
569 memset(hdev->eir, 0, sizeof(hdev->eir));
570 memset(&cp, 0, sizeof(cp));
571
572 hci_send_cmd(hdev, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
573 }
Johan Hedbergd5859e22011-01-25 01:19:58 +0200574 }
575
576 if (hdev->features[3] & LMP_RSSI_INQ)
577 hci_setup_inquiry_mode(hdev);
578
579 if (hdev->features[7] & LMP_INQ_TX_PWR)
580 hci_send_cmd(hdev, HCI_OP_READ_INQ_RSP_TX_POWER, 0, NULL);
Andre Guedes971e3a42011-06-30 19:20:52 -0300581
582 if (hdev->features[7] & LMP_EXTFEATURES) {
583 struct hci_cp_read_local_ext_features cp;
584
585 cp.page = 0x01;
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300586 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES, sizeof(cp),
587 &cp);
Andre Guedes971e3a42011-06-30 19:20:52 -0300588 }
Andre Guedese6100a22011-06-30 19:20:54 -0300589
Johan Hedberg47990ea2012-02-22 11:58:37 +0200590 if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) {
591 u8 enable = 1;
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300592 hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(enable),
593 &enable);
Johan Hedberg47990ea2012-02-22 11:58:37 +0200594 }
Johan Hedbergd5859e22011-01-25 01:19:58 +0200595}
596
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200597static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
598{
599 struct hci_rp_read_local_version *rp = (void *) skb->data;
600
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300601 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200602
603 if (rp->status)
Andrei Emeltchenko28b8df72012-02-24 12:45:44 +0200604 goto done;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200605
606 hdev->hci_ver = rp->hci_ver;
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200607 hdev->hci_rev = __le16_to_cpu(rp->hci_rev);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200608 hdev->lmp_ver = rp->lmp_ver;
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200609 hdev->manufacturer = __le16_to_cpu(rp->manufacturer);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200610 hdev->lmp_subver = __le16_to_cpu(rp->lmp_subver);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200611
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300612 BT_DBG("%s manufacturer 0x%4.4x hci ver %d:%d", hdev->name,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300613 hdev->manufacturer, hdev->hci_ver, hdev->hci_rev);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200614
615 if (test_bit(HCI_INIT, &hdev->flags))
616 hci_setup(hdev);
Andrei Emeltchenko28b8df72012-02-24 12:45:44 +0200617
618done:
619 hci_req_complete(hdev, HCI_OP_READ_LOCAL_VERSION, rp->status);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200620}
621
622static void hci_setup_link_policy(struct hci_dev *hdev)
623{
Andrei Emeltchenko035100c2012-03-12 15:59:32 +0200624 struct hci_cp_write_def_link_policy cp;
Johan Hedbergd5859e22011-01-25 01:19:58 +0200625 u16 link_policy = 0;
626
Andre Guedes9f92ebf2012-07-24 15:03:50 -0300627 if (lmp_rswitch_capable(hdev))
Johan Hedbergd5859e22011-01-25 01:19:58 +0200628 link_policy |= HCI_LP_RSWITCH;
629 if (hdev->features[0] & LMP_HOLD)
630 link_policy |= HCI_LP_HOLD;
Andre Guedes6eded102012-07-24 15:03:51 -0300631 if (lmp_sniff_capable(hdev))
Johan Hedbergd5859e22011-01-25 01:19:58 +0200632 link_policy |= HCI_LP_SNIFF;
633 if (hdev->features[1] & LMP_PARK)
634 link_policy |= HCI_LP_PARK;
635
Andrei Emeltchenko035100c2012-03-12 15:59:32 +0200636 cp.policy = cpu_to_le16(link_policy);
637 hci_send_cmd(hdev, HCI_OP_WRITE_DEF_LINK_POLICY, sizeof(cp), &cp);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200638}
639
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300640static void hci_cc_read_local_commands(struct hci_dev *hdev,
641 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200642{
643 struct hci_rp_read_local_commands *rp = (void *) skb->data;
644
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300645 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200646
647 if (rp->status)
Johan Hedbergd5859e22011-01-25 01:19:58 +0200648 goto done;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200649
650 memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
Johan Hedbergd5859e22011-01-25 01:19:58 +0200651
652 if (test_bit(HCI_INIT, &hdev->flags) && (hdev->commands[5] & 0x10))
653 hci_setup_link_policy(hdev);
654
655done:
656 hci_req_complete(hdev, HCI_OP_READ_LOCAL_COMMANDS, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200657}
658
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300659static void hci_cc_read_local_features(struct hci_dev *hdev,
660 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200661{
662 struct hci_rp_read_local_features *rp = (void *) skb->data;
663
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300664 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200665
666 if (rp->status)
667 return;
668
669 memcpy(hdev->features, rp->features, 8);
670
671 /* Adjust default settings according to features
672 * supported by device. */
673
674 if (hdev->features[0] & LMP_3SLOT)
675 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
676
677 if (hdev->features[0] & LMP_5SLOT)
678 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
679
680 if (hdev->features[1] & LMP_HV2) {
681 hdev->pkt_type |= (HCI_HV2);
682 hdev->esco_type |= (ESCO_HV2);
683 }
684
685 if (hdev->features[1] & LMP_HV3) {
686 hdev->pkt_type |= (HCI_HV3);
687 hdev->esco_type |= (ESCO_HV3);
688 }
689
Andre Guedes45db810f2012-07-24 15:03:49 -0300690 if (lmp_esco_capable(hdev))
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200691 hdev->esco_type |= (ESCO_EV3);
692
693 if (hdev->features[4] & LMP_EV4)
694 hdev->esco_type |= (ESCO_EV4);
695
696 if (hdev->features[4] & LMP_EV5)
697 hdev->esco_type |= (ESCO_EV5);
698
Marcel Holtmannefc76882009-02-06 09:13:37 +0100699 if (hdev->features[5] & LMP_EDR_ESCO_2M)
700 hdev->esco_type |= (ESCO_2EV3);
701
702 if (hdev->features[5] & LMP_EDR_ESCO_3M)
703 hdev->esco_type |= (ESCO_3EV3);
704
705 if (hdev->features[5] & LMP_EDR_3S_ESCO)
706 hdev->esco_type |= (ESCO_2EV5 | ESCO_3EV5);
707
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200708 BT_DBG("%s features 0x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x", hdev->name,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300709 hdev->features[0], hdev->features[1],
710 hdev->features[2], hdev->features[3],
711 hdev->features[4], hdev->features[5],
712 hdev->features[6], hdev->features[7]);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200713}
714
Johan Hedberg8f984df2012-02-28 01:07:22 +0200715static void hci_set_le_support(struct hci_dev *hdev)
716{
717 struct hci_cp_write_le_host_supported cp;
718
719 memset(&cp, 0, sizeof(cp));
720
Marcel Holtmann9d428202012-05-03 07:12:31 +0200721 if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) {
Johan Hedberg8f984df2012-02-28 01:07:22 +0200722 cp.le = 1;
723 cp.simul = !!(hdev->features[6] & LMP_SIMUL_LE_BR);
724 }
725
726 if (cp.le != !!(hdev->host_features[0] & LMP_HOST_LE))
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300727 hci_send_cmd(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(cp),
728 &cp);
Johan Hedberg8f984df2012-02-28 01:07:22 +0200729}
730
Andre Guedes971e3a42011-06-30 19:20:52 -0300731static void hci_cc_read_local_ext_features(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300732 struct sk_buff *skb)
Andre Guedes971e3a42011-06-30 19:20:52 -0300733{
734 struct hci_rp_read_local_ext_features *rp = (void *) skb->data;
735
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300736 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andre Guedes971e3a42011-06-30 19:20:52 -0300737
738 if (rp->status)
Johan Hedberg8f984df2012-02-28 01:07:22 +0200739 goto done;
Andre Guedes971e3a42011-06-30 19:20:52 -0300740
Andre Guedesb5b32b62011-12-30 10:34:04 -0300741 switch (rp->page) {
742 case 0:
743 memcpy(hdev->features, rp->features, 8);
744 break;
745 case 1:
746 memcpy(hdev->host_features, rp->features, 8);
747 break;
748 }
Andre Guedes971e3a42011-06-30 19:20:52 -0300749
Andre Guedesc383ddc2012-07-24 15:03:47 -0300750 if (test_bit(HCI_INIT, &hdev->flags) && lmp_le_capable(hdev))
Johan Hedberg8f984df2012-02-28 01:07:22 +0200751 hci_set_le_support(hdev);
752
753done:
Andre Guedes971e3a42011-06-30 19:20:52 -0300754 hci_req_complete(hdev, HCI_OP_READ_LOCAL_EXT_FEATURES, rp->status);
755}
756
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200757static void hci_cc_read_flow_control_mode(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300758 struct sk_buff *skb)
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200759{
760 struct hci_rp_read_flow_control_mode *rp = (void *) skb->data;
761
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300762 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200763
764 if (rp->status)
765 return;
766
767 hdev->flow_ctl_mode = rp->mode;
768
769 hci_req_complete(hdev, HCI_OP_READ_FLOW_CONTROL_MODE, rp->status);
770}
771
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200772static void hci_cc_read_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
773{
774 struct hci_rp_read_buffer_size *rp = (void *) skb->data;
775
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300776 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200777
778 if (rp->status)
779 return;
780
781 hdev->acl_mtu = __le16_to_cpu(rp->acl_mtu);
782 hdev->sco_mtu = rp->sco_mtu;
783 hdev->acl_pkts = __le16_to_cpu(rp->acl_max_pkt);
784 hdev->sco_pkts = __le16_to_cpu(rp->sco_max_pkt);
785
786 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
787 hdev->sco_mtu = 64;
788 hdev->sco_pkts = 8;
789 }
790
791 hdev->acl_cnt = hdev->acl_pkts;
792 hdev->sco_cnt = hdev->sco_pkts;
793
Gustavo Padovan807deac2012-05-17 00:36:24 -0300794 BT_DBG("%s acl mtu %d:%d sco mtu %d:%d", hdev->name, hdev->acl_mtu,
795 hdev->acl_pkts, hdev->sco_mtu, hdev->sco_pkts);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200796}
797
798static void hci_cc_read_bd_addr(struct hci_dev *hdev, struct sk_buff *skb)
799{
800 struct hci_rp_read_bd_addr *rp = (void *) skb->data;
801
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300802 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200803
804 if (!rp->status)
805 bacpy(&hdev->bdaddr, &rp->bdaddr);
806
Johan Hedberg23bb5762010-12-21 23:01:27 +0200807 hci_req_complete(hdev, HCI_OP_READ_BD_ADDR, rp->status);
808}
809
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200810static void hci_cc_read_data_block_size(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300811 struct sk_buff *skb)
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200812{
813 struct hci_rp_read_data_block_size *rp = (void *) skb->data;
814
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300815 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200816
817 if (rp->status)
818 return;
819
820 hdev->block_mtu = __le16_to_cpu(rp->max_acl_len);
821 hdev->block_len = __le16_to_cpu(rp->block_len);
822 hdev->num_blocks = __le16_to_cpu(rp->num_blocks);
823
824 hdev->block_cnt = hdev->num_blocks;
825
826 BT_DBG("%s blk mtu %d cnt %d len %d", hdev->name, hdev->block_mtu,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300827 hdev->block_cnt, hdev->block_len);
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200828
829 hci_req_complete(hdev, HCI_OP_READ_DATA_BLOCK_SIZE, rp->status);
830}
831
Johan Hedberg23bb5762010-12-21 23:01:27 +0200832static void hci_cc_write_ca_timeout(struct hci_dev *hdev, struct sk_buff *skb)
833{
834 __u8 status = *((__u8 *) skb->data);
835
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300836 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Johan Hedberg23bb5762010-12-21 23:01:27 +0200837
838 hci_req_complete(hdev, HCI_OP_WRITE_CA_TIMEOUT, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200839}
840
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300841static void hci_cc_read_local_amp_info(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300842 struct sk_buff *skb)
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300843{
844 struct hci_rp_read_local_amp_info *rp = (void *) skb->data;
845
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300846 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300847
848 if (rp->status)
849 return;
850
851 hdev->amp_status = rp->amp_status;
852 hdev->amp_total_bw = __le32_to_cpu(rp->total_bw);
853 hdev->amp_max_bw = __le32_to_cpu(rp->max_bw);
854 hdev->amp_min_latency = __le32_to_cpu(rp->min_latency);
855 hdev->amp_max_pdu = __le32_to_cpu(rp->max_pdu);
856 hdev->amp_type = rp->amp_type;
857 hdev->amp_pal_cap = __le16_to_cpu(rp->pal_cap);
858 hdev->amp_assoc_size = __le16_to_cpu(rp->max_assoc_size);
859 hdev->amp_be_flush_to = __le32_to_cpu(rp->be_flush_to);
860 hdev->amp_max_flush_to = __le32_to_cpu(rp->max_flush_to);
861
862 hci_req_complete(hdev, HCI_OP_READ_LOCAL_AMP_INFO, rp->status);
863}
864
Johan Hedbergb0916ea2011-01-10 13:44:55 +0200865static void hci_cc_delete_stored_link_key(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300866 struct sk_buff *skb)
Johan Hedbergb0916ea2011-01-10 13:44:55 +0200867{
868 __u8 status = *((__u8 *) skb->data);
869
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300870 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Johan Hedbergb0916ea2011-01-10 13:44:55 +0200871
872 hci_req_complete(hdev, HCI_OP_DELETE_STORED_LINK_KEY, status);
873}
874
Johan Hedbergd5859e22011-01-25 01:19:58 +0200875static void hci_cc_set_event_mask(struct hci_dev *hdev, struct sk_buff *skb)
876{
877 __u8 status = *((__u8 *) skb->data);
878
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300879 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200880
881 hci_req_complete(hdev, HCI_OP_SET_EVENT_MASK, status);
882}
883
884static void hci_cc_write_inquiry_mode(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300885 struct sk_buff *skb)
Johan Hedbergd5859e22011-01-25 01:19:58 +0200886{
887 __u8 status = *((__u8 *) skb->data);
888
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300889 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200890
891 hci_req_complete(hdev, HCI_OP_WRITE_INQUIRY_MODE, status);
892}
893
894static void hci_cc_read_inq_rsp_tx_power(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300895 struct sk_buff *skb)
Johan Hedbergd5859e22011-01-25 01:19:58 +0200896{
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -0700897 struct hci_rp_read_inq_rsp_tx_power *rp = (void *) skb->data;
Johan Hedbergd5859e22011-01-25 01:19:58 +0200898
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300899 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200900
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -0700901 if (!rp->status)
902 hdev->inq_tx_power = rp->tx_power;
903
904 hci_req_complete(hdev, HCI_OP_READ_INQ_RSP_TX_POWER, rp->status);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200905}
906
907static void hci_cc_set_event_flt(struct hci_dev *hdev, struct sk_buff *skb)
908{
909 __u8 status = *((__u8 *) skb->data);
910
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300911 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200912
913 hci_req_complete(hdev, HCI_OP_SET_EVENT_FLT, status);
914}
915
Johan Hedberg980e1a52011-01-22 06:10:07 +0200916static void hci_cc_pin_code_reply(struct hci_dev *hdev, struct sk_buff *skb)
917{
918 struct hci_rp_pin_code_reply *rp = (void *) skb->data;
919 struct hci_cp_pin_code_reply *cp;
920 struct hci_conn *conn;
921
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300922 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200923
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200924 hci_dev_lock(hdev);
925
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200926 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg744cf192011-11-08 20:40:14 +0200927 mgmt_pin_code_reply_complete(hdev, &rp->bdaddr, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200928
Mikel Astizfa1bd912012-08-09 09:52:29 +0200929 if (rp->status)
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200930 goto unlock;
Johan Hedberg980e1a52011-01-22 06:10:07 +0200931
932 cp = hci_sent_cmd_data(hdev, HCI_OP_PIN_CODE_REPLY);
933 if (!cp)
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200934 goto unlock;
Johan Hedberg980e1a52011-01-22 06:10:07 +0200935
936 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
937 if (conn)
938 conn->pin_length = cp->pin_len;
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200939
940unlock:
941 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200942}
943
944static void hci_cc_pin_code_neg_reply(struct hci_dev *hdev, struct sk_buff *skb)
945{
946 struct hci_rp_pin_code_neg_reply *rp = (void *) skb->data;
947
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300948 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200949
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200950 hci_dev_lock(hdev);
951
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200952 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg744cf192011-11-08 20:40:14 +0200953 mgmt_pin_code_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300954 rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200955
956 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200957}
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200958
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300959static void hci_cc_le_read_buffer_size(struct hci_dev *hdev,
960 struct sk_buff *skb)
961{
962 struct hci_rp_le_read_buffer_size *rp = (void *) skb->data;
963
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300964 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300965
966 if (rp->status)
967 return;
968
969 hdev->le_mtu = __le16_to_cpu(rp->le_mtu);
970 hdev->le_pkts = rp->le_max_pkt;
971
972 hdev->le_cnt = hdev->le_pkts;
973
974 BT_DBG("%s le mtu %d:%d", hdev->name, hdev->le_mtu, hdev->le_pkts);
975
976 hci_req_complete(hdev, HCI_OP_LE_READ_BUFFER_SIZE, rp->status);
977}
Johan Hedberg980e1a52011-01-22 06:10:07 +0200978
Johan Hedberga5c29682011-02-19 12:05:57 -0300979static void hci_cc_user_confirm_reply(struct hci_dev *hdev, struct sk_buff *skb)
980{
981 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
982
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300983 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberga5c29682011-02-19 12:05:57 -0300984
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200985 hci_dev_lock(hdev);
986
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200987 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300988 mgmt_user_confirm_reply_complete(hdev, &rp->bdaddr, ACL_LINK, 0,
989 rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200990
991 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -0300992}
993
994static void hci_cc_user_confirm_neg_reply(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300995 struct sk_buff *skb)
Johan Hedberga5c29682011-02-19 12:05:57 -0300996{
997 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
998
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300999 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberga5c29682011-02-19 12:05:57 -03001000
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001001 hci_dev_lock(hdev);
1002
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001003 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg744cf192011-11-08 20:40:14 +02001004 mgmt_user_confirm_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001005 ACL_LINK, 0, rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001006
1007 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03001008}
1009
Brian Gix1143d452011-11-23 08:28:34 -08001010static void hci_cc_user_passkey_reply(struct hci_dev *hdev, struct sk_buff *skb)
1011{
1012 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
1013
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001014 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -08001015
1016 hci_dev_lock(hdev);
1017
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001018 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg272d90d2012-02-09 15:26:12 +02001019 mgmt_user_passkey_reply_complete(hdev, &rp->bdaddr, ACL_LINK,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001020 0, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -08001021
1022 hci_dev_unlock(hdev);
1023}
1024
1025static void hci_cc_user_passkey_neg_reply(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001026 struct sk_buff *skb)
Brian Gix1143d452011-11-23 08:28:34 -08001027{
1028 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
1029
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001030 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -08001031
1032 hci_dev_lock(hdev);
1033
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001034 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Brian Gix1143d452011-11-23 08:28:34 -08001035 mgmt_user_passkey_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001036 ACL_LINK, 0, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -08001037
1038 hci_dev_unlock(hdev);
1039}
1040
Szymon Jancc35938b2011-03-22 13:12:21 +01001041static void hci_cc_read_local_oob_data_reply(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001042 struct sk_buff *skb)
Szymon Jancc35938b2011-03-22 13:12:21 +01001043{
1044 struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
1045
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001046 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Szymon Jancc35938b2011-03-22 13:12:21 +01001047
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001048 hci_dev_lock(hdev);
Johan Hedberg744cf192011-11-08 20:40:14 +02001049 mgmt_read_local_oob_data_reply_complete(hdev, rp->hash,
Szymon Jancc35938b2011-03-22 13:12:21 +01001050 rp->randomizer, rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001051 hci_dev_unlock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01001052}
1053
Andre Guedes07f7fa52011-12-02 21:13:31 +09001054static void hci_cc_le_set_scan_param(struct hci_dev *hdev, struct sk_buff *skb)
1055{
1056 __u8 status = *((__u8 *) skb->data);
1057
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001058 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andre Guedes7ba8b4b2012-02-03 17:47:59 -03001059
1060 hci_req_complete(hdev, HCI_OP_LE_SET_SCAN_PARAM, status);
Andre Guedes3fd24152012-02-03 17:48:01 -03001061
1062 if (status) {
1063 hci_dev_lock(hdev);
1064 mgmt_start_discovery_failed(hdev, status);
1065 hci_dev_unlock(hdev);
1066 return;
1067 }
Andre Guedes07f7fa52011-12-02 21:13:31 +09001068}
1069
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001070static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001071 struct sk_buff *skb)
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001072{
1073 struct hci_cp_le_set_scan_enable *cp;
1074 __u8 status = *((__u8 *) skb->data);
1075
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001076 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001077
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001078 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_ENABLE);
1079 if (!cp)
1080 return;
1081
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001082 switch (cp->enable) {
1083 case LE_SCANNING_ENABLED:
Andre Guedes7ba8b4b2012-02-03 17:47:59 -03001084 hci_req_complete(hdev, HCI_OP_LE_SET_SCAN_ENABLE, status);
1085
Andre Guedes3fd24152012-02-03 17:48:01 -03001086 if (status) {
1087 hci_dev_lock(hdev);
1088 mgmt_start_discovery_failed(hdev, status);
1089 hci_dev_unlock(hdev);
Andre Guedes7ba8b4b2012-02-03 17:47:59 -03001090 return;
Andre Guedes3fd24152012-02-03 17:48:01 -03001091 }
Andre Guedes7ba8b4b2012-02-03 17:47:59 -03001092
Andre Guedesd23264a2011-11-25 20:53:38 -03001093 set_bit(HCI_LE_SCAN, &hdev->dev_flags);
1094
Andre Guedesa8f13c82011-09-09 18:56:24 -03001095 hci_dev_lock(hdev);
Andre Guedes343f9352012-02-17 20:39:37 -03001096 hci_discovery_set_state(hdev, DISCOVERY_FINDING);
Andre Guedesa8f13c82011-09-09 18:56:24 -03001097 hci_dev_unlock(hdev);
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001098 break;
1099
1100 case LE_SCANNING_DISABLED:
Andre Guedesc9ecc482012-03-15 16:52:08 -03001101 if (status) {
1102 hci_dev_lock(hdev);
1103 mgmt_stop_discovery_failed(hdev, status);
1104 hci_dev_unlock(hdev);
Andre Guedes7ba8b4b2012-02-03 17:47:59 -03001105 return;
Andre Guedesc9ecc482012-03-15 16:52:08 -03001106 }
Andre Guedes7ba8b4b2012-02-03 17:47:59 -03001107
Andre Guedesd23264a2011-11-25 20:53:38 -03001108 clear_bit(HCI_LE_SCAN, &hdev->dev_flags);
1109
Andre Guedesbc3dd332012-03-06 19:37:06 -03001110 if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED &&
1111 hdev->discovery.state == DISCOVERY_FINDING) {
Andre Guedes5e0452c2012-02-17 20:39:38 -03001112 mgmt_interleaved_discovery(hdev);
1113 } else {
1114 hci_dev_lock(hdev);
1115 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1116 hci_dev_unlock(hdev);
1117 }
1118
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001119 break;
1120
1121 default:
1122 BT_ERR("Used reserved LE_Scan_Enable param %d", cp->enable);
1123 break;
Andre Guedes35815082011-05-26 16:23:53 -03001124 }
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001125}
1126
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03001127static void hci_cc_le_ltk_reply(struct hci_dev *hdev, struct sk_buff *skb)
1128{
1129 struct hci_rp_le_ltk_reply *rp = (void *) skb->data;
1130
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001131 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03001132
1133 if (rp->status)
1134 return;
1135
1136 hci_req_complete(hdev, HCI_OP_LE_LTK_REPLY, rp->status);
1137}
1138
1139static void hci_cc_le_ltk_neg_reply(struct hci_dev *hdev, struct sk_buff *skb)
1140{
1141 struct hci_rp_le_ltk_neg_reply *rp = (void *) skb->data;
1142
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001143 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03001144
1145 if (rp->status)
1146 return;
1147
1148 hci_req_complete(hdev, HCI_OP_LE_LTK_NEG_REPLY, rp->status);
1149}
1150
Gustavo Padovan6039aa732012-05-23 04:04:18 -03001151static void hci_cc_write_le_host_supported(struct hci_dev *hdev,
1152 struct sk_buff *skb)
Andre Guedesf9b49302011-06-30 19:20:53 -03001153{
Johan Hedberg06199cf2012-02-22 16:37:11 +02001154 struct hci_cp_write_le_host_supported *sent;
Andre Guedesf9b49302011-06-30 19:20:53 -03001155 __u8 status = *((__u8 *) skb->data);
1156
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001157 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andre Guedesf9b49302011-06-30 19:20:53 -03001158
Johan Hedberg06199cf2012-02-22 16:37:11 +02001159 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED);
Johan Hedberg8f984df2012-02-28 01:07:22 +02001160 if (!sent)
Andre Guedesf9b49302011-06-30 19:20:53 -03001161 return;
1162
Johan Hedberg8f984df2012-02-28 01:07:22 +02001163 if (!status) {
1164 if (sent->le)
1165 hdev->host_features[0] |= LMP_HOST_LE;
1166 else
1167 hdev->host_features[0] &= ~LMP_HOST_LE;
1168 }
1169
1170 if (test_bit(HCI_MGMT, &hdev->dev_flags) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03001171 !test_bit(HCI_INIT, &hdev->flags))
Johan Hedberg8f984df2012-02-28 01:07:22 +02001172 mgmt_le_enable_complete(hdev, sent->le, status);
1173
1174 hci_req_complete(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED, status);
Andre Guedesf9b49302011-06-30 19:20:53 -03001175}
1176
Gustavo Padovan6039aa732012-05-23 04:04:18 -03001177static void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001178{
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001179 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001180
1181 if (status) {
Johan Hedberg23bb5762010-12-21 23:01:27 +02001182 hci_req_complete(hdev, HCI_OP_INQUIRY, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001183 hci_conn_check_pending(hdev);
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001184 hci_dev_lock(hdev);
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001185 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Andre Guedes7a135102011-11-09 17:14:25 -03001186 mgmt_start_discovery_failed(hdev, status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001187 hci_dev_unlock(hdev);
Johan Hedberg314b2382011-04-27 10:29:57 -04001188 return;
1189 }
1190
Andre Guedes89352e72011-11-04 14:16:53 -03001191 set_bit(HCI_INQUIRY, &hdev->flags);
1192
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001193 hci_dev_lock(hdev);
Andre Guedes343f9352012-02-17 20:39:37 -03001194 hci_discovery_set_state(hdev, DISCOVERY_FINDING);
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001195 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001196}
1197
Gustavo Padovan6039aa732012-05-23 04:04:18 -03001198static void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001200 struct hci_cp_create_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001203 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001204
1205 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_CONN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 if (!cp)
1207 return;
1208
1209 hci_dev_lock(hdev);
1210
1211 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
1212
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001213 BT_DBG("%s bdaddr %s hcon %p", hdev->name, batostr(&cp->bdaddr), conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 if (status) {
1216 if (conn && conn->state == BT_CONNECT) {
Marcel Holtmann4c67bc72006-10-15 17:30:56 +02001217 if (status != 0x0c || conn->attempt > 2) {
1218 conn->state = BT_CLOSED;
1219 hci_proto_connect_cfm(conn, status);
1220 hci_conn_del(conn);
1221 } else
1222 conn->state = BT_CONNECT2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224 } else {
1225 if (!conn) {
1226 conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr);
1227 if (conn) {
Johan Hedberga0c808b2012-01-16 09:49:58 +02001228 conn->out = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 conn->link_mode |= HCI_LM_MASTER;
1230 } else
Gustavo F. Padovan893ef972010-07-18 15:13:37 -03001231 BT_ERR("No memory for new connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
1233 }
1234
1235 hci_dev_unlock(hdev);
1236}
1237
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001238static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001240 struct hci_cp_add_sco *cp;
1241 struct hci_conn *acl, *sco;
1242 __u16 handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001244 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001245
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001246 if (!status)
1247 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001249 cp = hci_sent_cmd_data(hdev, HCI_OP_ADD_SCO);
1250 if (!cp)
1251 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001253 handle = __le16_to_cpu(cp->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001255 BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
Marcel Holtmann6bd57412006-11-18 22:14:22 +01001256
1257 hci_dev_lock(hdev);
1258
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001259 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001260 if (acl) {
1261 sco = acl->link;
1262 if (sco) {
1263 sco->state = BT_CLOSED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001264
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001265 hci_proto_connect_cfm(sco, status);
1266 hci_conn_del(sco);
1267 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001268 }
Marcel Holtmann6bd57412006-11-18 22:14:22 +01001269
1270 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
Marcel Holtmannf8558552008-07-14 20:13:49 +02001273static void hci_cs_auth_requested(struct hci_dev *hdev, __u8 status)
1274{
1275 struct hci_cp_auth_requested *cp;
1276 struct hci_conn *conn;
1277
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001278 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001279
1280 if (!status)
1281 return;
1282
1283 cp = hci_sent_cmd_data(hdev, HCI_OP_AUTH_REQUESTED);
1284 if (!cp)
1285 return;
1286
1287 hci_dev_lock(hdev);
1288
1289 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1290 if (conn) {
1291 if (conn->state == BT_CONFIG) {
1292 hci_proto_connect_cfm(conn, status);
1293 hci_conn_put(conn);
1294 }
1295 }
1296
1297 hci_dev_unlock(hdev);
1298}
1299
1300static void hci_cs_set_conn_encrypt(struct hci_dev *hdev, __u8 status)
1301{
1302 struct hci_cp_set_conn_encrypt *cp;
1303 struct hci_conn *conn;
1304
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001305 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001306
1307 if (!status)
1308 return;
1309
1310 cp = hci_sent_cmd_data(hdev, HCI_OP_SET_CONN_ENCRYPT);
1311 if (!cp)
1312 return;
1313
1314 hci_dev_lock(hdev);
1315
1316 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1317 if (conn) {
1318 if (conn->state == BT_CONFIG) {
1319 hci_proto_connect_cfm(conn, status);
1320 hci_conn_put(conn);
1321 }
1322 }
1323
1324 hci_dev_unlock(hdev);
1325}
1326
Johan Hedberg127178d2010-11-18 22:22:29 +02001327static int hci_outgoing_auth_needed(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001328 struct hci_conn *conn)
Johan Hedberg392599b2010-11-18 22:22:28 +02001329{
Johan Hedberg392599b2010-11-18 22:22:28 +02001330 if (conn->state != BT_CONFIG || !conn->out)
1331 return 0;
1332
Johan Hedberg765c2a92011-01-19 12:06:52 +05301333 if (conn->pending_sec_level == BT_SECURITY_SDP)
Johan Hedberg392599b2010-11-18 22:22:28 +02001334 return 0;
1335
1336 /* Only request authentication for SSP connections or non-SSP
Vinicius Costa Gomese9bf2bf2011-09-02 14:51:20 -03001337 * devices with sec_level HIGH or if MITM protection is requested */
Gustavo Padovan807deac2012-05-17 00:36:24 -03001338 if (!hci_conn_ssp_enabled(conn) && !(conn->auth_type & 0x01) &&
1339 conn->pending_sec_level != BT_SECURITY_HIGH)
Johan Hedberg392599b2010-11-18 22:22:28 +02001340 return 0;
1341
Johan Hedberg392599b2010-11-18 22:22:28 +02001342 return 1;
1343}
1344
Gustavo Padovan6039aa732012-05-23 04:04:18 -03001345static int hci_resolve_name(struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001346 struct inquiry_entry *e)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001347{
1348 struct hci_cp_remote_name_req cp;
1349
1350 memset(&cp, 0, sizeof(cp));
1351
1352 bacpy(&cp.bdaddr, &e->data.bdaddr);
1353 cp.pscan_rep_mode = e->data.pscan_rep_mode;
1354 cp.pscan_mode = e->data.pscan_mode;
1355 cp.clock_offset = e->data.clock_offset;
1356
1357 return hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
1358}
1359
Johan Hedbergb644ba32012-01-17 21:48:47 +02001360static bool hci_resolve_next_name(struct hci_dev *hdev)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001361{
1362 struct discovery_state *discov = &hdev->discovery;
1363 struct inquiry_entry *e;
1364
Johan Hedbergb644ba32012-01-17 21:48:47 +02001365 if (list_empty(&discov->resolve))
1366 return false;
1367
1368 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
Ram Malovanyc8100892012-07-19 10:26:09 +03001369 if (!e)
1370 return false;
1371
Johan Hedbergb644ba32012-01-17 21:48:47 +02001372 if (hci_resolve_name(hdev, e) == 0) {
1373 e->name_state = NAME_PENDING;
1374 return true;
1375 }
1376
1377 return false;
1378}
1379
1380static void hci_check_pending_name(struct hci_dev *hdev, struct hci_conn *conn,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001381 bdaddr_t *bdaddr, u8 *name, u8 name_len)
Johan Hedbergb644ba32012-01-17 21:48:47 +02001382{
1383 struct discovery_state *discov = &hdev->discovery;
1384 struct inquiry_entry *e;
1385
1386 if (conn && !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001387 mgmt_device_connected(hdev, bdaddr, ACL_LINK, 0x00, 0, name,
1388 name_len, conn->dev_class);
Johan Hedbergb644ba32012-01-17 21:48:47 +02001389
1390 if (discov->state == DISCOVERY_STOPPED)
1391 return;
1392
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001393 if (discov->state == DISCOVERY_STOPPING)
1394 goto discov_complete;
1395
1396 if (discov->state != DISCOVERY_RESOLVING)
1397 return;
1398
1399 e = hci_inquiry_cache_lookup_resolve(hdev, bdaddr, NAME_PENDING);
Ram Malovany7cc83802012-07-19 10:26:10 +03001400 /* If the device was not found in a list of found devices names of which
1401 * are pending. there is no need to continue resolving a next name as it
1402 * will be done upon receiving another Remote Name Request Complete
1403 * Event */
1404 if (!e)
1405 return;
1406
1407 list_del(&e->list);
1408 if (name) {
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001409 e->name_state = NAME_KNOWN;
Ram Malovany7cc83802012-07-19 10:26:10 +03001410 mgmt_remote_name(hdev, bdaddr, ACL_LINK, 0x00,
1411 e->data.rssi, name, name_len);
Ram Malovanyc3e7c0d2012-07-19 10:26:11 +03001412 } else {
1413 e->name_state = NAME_NOT_KNOWN;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001414 }
1415
Johan Hedbergb644ba32012-01-17 21:48:47 +02001416 if (hci_resolve_next_name(hdev))
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001417 return;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001418
1419discov_complete:
1420 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1421}
1422
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001423static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
1424{
Johan Hedberg127178d2010-11-18 22:22:29 +02001425 struct hci_cp_remote_name_req *cp;
1426 struct hci_conn *conn;
1427
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001428 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Johan Hedberg127178d2010-11-18 22:22:29 +02001429
1430 /* If successful wait for the name req complete event before
1431 * checking for the need to do authentication */
1432 if (!status)
1433 return;
1434
1435 cp = hci_sent_cmd_data(hdev, HCI_OP_REMOTE_NAME_REQ);
1436 if (!cp)
1437 return;
1438
1439 hci_dev_lock(hdev);
1440
1441 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
Johan Hedbergb644ba32012-01-17 21:48:47 +02001442
1443 if (test_bit(HCI_MGMT, &hdev->dev_flags))
1444 hci_check_pending_name(hdev, conn, &cp->bdaddr, NULL, 0);
1445
Johan Hedberg79c6c702011-04-28 11:28:55 -07001446 if (!conn)
1447 goto unlock;
1448
1449 if (!hci_outgoing_auth_needed(hdev, conn))
1450 goto unlock;
1451
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001452 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02001453 struct hci_cp_auth_requested cp;
1454 cp.handle = __cpu_to_le16(conn->handle);
1455 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
1456 }
1457
Johan Hedberg79c6c702011-04-28 11:28:55 -07001458unlock:
Johan Hedberg127178d2010-11-18 22:22:29 +02001459 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001460}
1461
Marcel Holtmann769be972008-07-14 20:13:49 +02001462static void hci_cs_read_remote_features(struct hci_dev *hdev, __u8 status)
1463{
1464 struct hci_cp_read_remote_features *cp;
1465 struct hci_conn *conn;
1466
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001467 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann769be972008-07-14 20:13:49 +02001468
1469 if (!status)
1470 return;
1471
1472 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_FEATURES);
1473 if (!cp)
1474 return;
1475
1476 hci_dev_lock(hdev);
1477
1478 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1479 if (conn) {
1480 if (conn->state == BT_CONFIG) {
Marcel Holtmann769be972008-07-14 20:13:49 +02001481 hci_proto_connect_cfm(conn, status);
1482 hci_conn_put(conn);
1483 }
1484 }
1485
1486 hci_dev_unlock(hdev);
1487}
1488
1489static void hci_cs_read_remote_ext_features(struct hci_dev *hdev, __u8 status)
1490{
1491 struct hci_cp_read_remote_ext_features *cp;
1492 struct hci_conn *conn;
1493
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001494 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann769be972008-07-14 20:13:49 +02001495
1496 if (!status)
1497 return;
1498
1499 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES);
1500 if (!cp)
1501 return;
1502
1503 hci_dev_lock(hdev);
1504
1505 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1506 if (conn) {
1507 if (conn->state == BT_CONFIG) {
Marcel Holtmann769be972008-07-14 20:13:49 +02001508 hci_proto_connect_cfm(conn, status);
1509 hci_conn_put(conn);
1510 }
1511 }
1512
1513 hci_dev_unlock(hdev);
1514}
1515
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001516static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
1517{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001518 struct hci_cp_setup_sync_conn *cp;
1519 struct hci_conn *acl, *sco;
1520 __u16 handle;
1521
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001522 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001523
1524 if (!status)
1525 return;
1526
1527 cp = hci_sent_cmd_data(hdev, HCI_OP_SETUP_SYNC_CONN);
1528 if (!cp)
1529 return;
1530
1531 handle = __le16_to_cpu(cp->handle);
1532
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001533 BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001534
1535 hci_dev_lock(hdev);
1536
1537 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001538 if (acl) {
1539 sco = acl->link;
1540 if (sco) {
1541 sco->state = BT_CLOSED;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001542
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001543 hci_proto_connect_cfm(sco, status);
1544 hci_conn_del(sco);
1545 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001546 }
1547
1548 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001549}
1550
1551static void hci_cs_sniff_mode(struct hci_dev *hdev, __u8 status)
1552{
1553 struct hci_cp_sniff_mode *cp;
1554 struct hci_conn *conn;
1555
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001556 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001557
1558 if (!status)
1559 return;
1560
1561 cp = hci_sent_cmd_data(hdev, HCI_OP_SNIFF_MODE);
1562 if (!cp)
1563 return;
1564
1565 hci_dev_lock(hdev);
1566
1567 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001568 if (conn) {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001569 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001570
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001571 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001572 hci_sco_setup(conn, status);
1573 }
1574
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001575 hci_dev_unlock(hdev);
1576}
1577
1578static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
1579{
1580 struct hci_cp_exit_sniff_mode *cp;
1581 struct hci_conn *conn;
1582
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001583 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001584
1585 if (!status)
1586 return;
1587
1588 cp = hci_sent_cmd_data(hdev, HCI_OP_EXIT_SNIFF_MODE);
1589 if (!cp)
1590 return;
1591
1592 hci_dev_lock(hdev);
1593
1594 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001595 if (conn) {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001596 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001597
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001598 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001599 hci_sco_setup(conn, status);
1600 }
1601
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001602 hci_dev_unlock(hdev);
1603}
1604
Johan Hedberg88c3df12012-02-09 14:27:38 +02001605static void hci_cs_disconnect(struct hci_dev *hdev, u8 status)
1606{
1607 struct hci_cp_disconnect *cp;
1608 struct hci_conn *conn;
1609
1610 if (!status)
1611 return;
1612
1613 cp = hci_sent_cmd_data(hdev, HCI_OP_DISCONNECT);
1614 if (!cp)
1615 return;
1616
1617 hci_dev_lock(hdev);
1618
1619 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1620 if (conn)
1621 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001622 conn->dst_type, status);
Johan Hedberg88c3df12012-02-09 14:27:38 +02001623
1624 hci_dev_unlock(hdev);
1625}
1626
Ville Tervofcd89c02011-02-10 22:38:47 -03001627static void hci_cs_le_create_conn(struct hci_dev *hdev, __u8 status)
1628{
Ville Tervofcd89c02011-02-10 22:38:47 -03001629 struct hci_conn *conn;
1630
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001631 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Ville Tervofcd89c02011-02-10 22:38:47 -03001632
Ville Tervofcd89c02011-02-10 22:38:47 -03001633 if (status) {
Andre Guedesf00a06a2012-07-27 15:10:13 -03001634 hci_dev_lock(hdev);
Ville Tervofcd89c02011-02-10 22:38:47 -03001635
Andre Guedes0c95ab72012-07-27 15:10:14 -03001636 conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
Andre Guedesf00a06a2012-07-27 15:10:13 -03001637 if (!conn) {
1638 hci_dev_unlock(hdev);
1639 return;
1640 }
1641
Andre Guedes0c95ab72012-07-27 15:10:14 -03001642 BT_DBG("%s bdaddr %s conn %p", hdev->name, batostr(&conn->dst),
Andre Guedesf00a06a2012-07-27 15:10:13 -03001643 conn);
1644
1645 conn->state = BT_CLOSED;
Andre Guedes0c95ab72012-07-27 15:10:14 -03001646 mgmt_connect_failed(hdev, &conn->dst, conn->type,
Andre Guedesf00a06a2012-07-27 15:10:13 -03001647 conn->dst_type, status);
1648 hci_proto_connect_cfm(conn, status);
1649 hci_conn_del(conn);
1650
1651 hci_dev_unlock(hdev);
1652 }
Ville Tervofcd89c02011-02-10 22:38:47 -03001653}
1654
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03001655static void hci_cs_le_start_enc(struct hci_dev *hdev, u8 status)
1656{
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001657 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03001658}
1659
Gustavo Padovan6039aa732012-05-23 04:04:18 -03001660static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001661{
1662 __u8 status = *((__u8 *) skb->data);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001663 struct discovery_state *discov = &hdev->discovery;
1664 struct inquiry_entry *e;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001665
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001666 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001667
Johan Hedberg23bb5762010-12-21 23:01:27 +02001668 hci_req_complete(hdev, HCI_OP_INQUIRY, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001669
1670 hci_conn_check_pending(hdev);
Andre Guedes89352e72011-11-04 14:16:53 -03001671
1672 if (!test_and_clear_bit(HCI_INQUIRY, &hdev->flags))
1673 return;
1674
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001675 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001676 return;
1677
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001678 hci_dev_lock(hdev);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001679
Andre Guedes343f9352012-02-17 20:39:37 -03001680 if (discov->state != DISCOVERY_FINDING)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001681 goto unlock;
1682
1683 if (list_empty(&discov->resolve)) {
1684 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1685 goto unlock;
1686 }
1687
1688 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
1689 if (e && hci_resolve_name(hdev, e) == 0) {
1690 e->name_state = NAME_PENDING;
1691 hci_discovery_set_state(hdev, DISCOVERY_RESOLVING);
1692 } else {
1693 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1694 }
1695
1696unlock:
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001697 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001698}
1699
Gustavo Padovan6039aa732012-05-23 04:04:18 -03001700static void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001702 struct inquiry_data data;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001703 struct inquiry_info *info = (void *) (skb->data + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 int num_rsp = *((__u8 *) skb->data);
1705
1706 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
1707
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001708 if (!num_rsp)
1709 return;
1710
Andre Guedes1519cc12012-03-21 00:03:38 -03001711 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags))
1712 return;
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001715
Johan Hedberge17acd42011-03-30 23:57:16 +03001716 for (; num_rsp; num_rsp--, info++) {
Johan Hedberg388fc8f2012-02-23 00:38:59 +02001717 bool name_known, ssp;
Johan Hedberg31754052012-01-04 13:39:52 +02001718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 bacpy(&data.bdaddr, &info->bdaddr);
1720 data.pscan_rep_mode = info->pscan_rep_mode;
1721 data.pscan_period_mode = info->pscan_period_mode;
1722 data.pscan_mode = info->pscan_mode;
1723 memcpy(data.dev_class, info->dev_class, 3);
1724 data.clock_offset = info->clock_offset;
1725 data.rssi = 0x00;
Marcel Holtmann41a96212008-07-14 20:13:48 +02001726 data.ssp_mode = 0x00;
Johan Hedberg31754052012-01-04 13:39:52 +02001727
Johan Hedberg388fc8f2012-02-23 00:38:59 +02001728 name_known = hci_inquiry_cache_update(hdev, &data, false, &ssp);
Johan Hedberg48264f02011-11-09 13:58:58 +02001729 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001730 info->dev_class, 0, !name_known, ssp, NULL,
1731 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 hci_dev_unlock(hdev);
1735}
1736
Gustavo Padovan6039aa732012-05-23 04:04:18 -03001737static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001739 struct hci_ev_conn_complete *ev = (void *) skb->data;
1740 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001742 BT_DBG("%s", hdev->name);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001745
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001746 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann94992372009-04-19 19:30:03 +02001747 if (!conn) {
1748 if (ev->link_type != SCO_LINK)
1749 goto unlock;
1750
1751 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
1752 if (!conn)
1753 goto unlock;
1754
1755 conn->type = SCO_LINK;
1756 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001757
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001758 if (!ev->status) {
1759 conn->handle = __le16_to_cpu(ev->handle);
Marcel Holtmann769be972008-07-14 20:13:49 +02001760
1761 if (conn->type == ACL_LINK) {
1762 conn->state = BT_CONFIG;
1763 hci_conn_hold(conn);
Szymon Janca9ea3ed2012-07-19 14:46:08 +02001764
1765 if (!conn->out && !hci_conn_ssp_enabled(conn) &&
1766 !hci_find_link_key(hdev, &ev->bdaddr))
1767 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
1768 else
1769 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Marcel Holtmann769be972008-07-14 20:13:49 +02001770 } else
1771 conn->state = BT_CONNECTED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001772
Marcel Holtmann9eba32b2009-08-22 14:19:26 -07001773 hci_conn_hold_device(conn);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02001774 hci_conn_add_sysfs(conn);
1775
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001776 if (test_bit(HCI_AUTH, &hdev->flags))
1777 conn->link_mode |= HCI_LM_AUTH;
1778
1779 if (test_bit(HCI_ENCRYPT, &hdev->flags))
1780 conn->link_mode |= HCI_LM_ENCRYPT;
1781
1782 /* Get remote features */
1783 if (conn->type == ACL_LINK) {
1784 struct hci_cp_read_remote_features cp;
1785 cp.handle = ev->handle;
Marcel Holtmann769be972008-07-14 20:13:49 +02001786 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001787 sizeof(cp), &cp);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001788 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001789
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001790 /* Set packet type for incoming connection */
Andrei Emeltchenkod095c1e2011-12-01 14:33:27 +02001791 if (!conn->out && hdev->hci_ver < BLUETOOTH_VER_2_0) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001792 struct hci_cp_change_conn_ptype cp;
1793 cp.handle = ev->handle;
Marcel Holtmanna8746412008-07-14 20:13:46 +02001794 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001795 hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE, sizeof(cp),
1796 &cp);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001797 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02001798 } else {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001799 conn->state = BT_CLOSED;
Johan Hedberg17d5c042011-01-22 06:09:08 +02001800 if (conn->type == ACL_LINK)
Johan Hedberg744cf192011-11-08 20:40:14 +02001801 mgmt_connect_failed(hdev, &ev->bdaddr, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001802 conn->dst_type, ev->status);
Johan Hedberg17d5c042011-01-22 06:09:08 +02001803 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001804
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001805 if (conn->type == ACL_LINK)
1806 hci_sco_setup(conn, ev->status);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001807
Marcel Holtmann769be972008-07-14 20:13:49 +02001808 if (ev->status) {
1809 hci_proto_connect_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001810 hci_conn_del(conn);
Marcel Holtmannc89b6e62009-01-15 21:57:03 +01001811 } else if (ev->link_type != ACL_LINK)
1812 hci_proto_connect_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001813
1814unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001816
1817 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
1819
Gustavo Padovan6039aa732012-05-23 04:04:18 -03001820static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001822 struct hci_ev_conn_request *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 int mask = hdev->link_mode;
1824
Gustavo Padovan807deac2012-05-17 00:36:24 -03001825 BT_DBG("%s bdaddr %s type 0x%x", hdev->name, batostr(&ev->bdaddr),
1826 ev->link_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
1828 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type);
1829
Szymon Janc138d22e2011-02-17 16:44:23 +01001830 if ((mask & HCI_LM_ACCEPT) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03001831 !hci_blacklist_lookup(hdev, &ev->bdaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 /* Connection accepted */
Marcel Holtmannc7bdd502008-07-14 20:13:47 +02001833 struct inquiry_entry *ie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836 hci_dev_lock(hdev);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001837
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02001838 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
1839 if (ie)
Marcel Holtmannc7bdd502008-07-14 20:13:47 +02001840 memcpy(ie->data.dev_class, ev->dev_class, 3);
1841
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03001842 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type,
1843 &ev->bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (!conn) {
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02001845 conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr);
1846 if (!conn) {
Gustavo F. Padovan893ef972010-07-18 15:13:37 -03001847 BT_ERR("No memory for new connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 hci_dev_unlock(hdev);
1849 return;
1850 }
1851 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 memcpy(conn->dev_class, ev->dev_class, 3);
1854 conn->state = BT_CONNECT;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 hci_dev_unlock(hdev);
1857
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001858 if (ev->link_type == ACL_LINK || !lmp_esco_capable(hdev)) {
1859 struct hci_cp_accept_conn_req cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001861 bacpy(&cp.bdaddr, &ev->bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001863 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
1864 cp.role = 0x00; /* Become master */
1865 else
1866 cp.role = 0x01; /* Remain slave */
1867
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001868 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp),
1869 &cp);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001870 } else {
1871 struct hci_cp_accept_sync_conn_req cp;
1872
1873 bacpy(&cp.bdaddr, &ev->bdaddr);
Marcel Holtmanna8746412008-07-14 20:13:46 +02001874 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001875
Andrei Emeltchenko82781e62012-05-25 11:38:27 +03001876 cp.tx_bandwidth = __constant_cpu_to_le32(0x00001f40);
1877 cp.rx_bandwidth = __constant_cpu_to_le32(0x00001f40);
1878 cp.max_latency = __constant_cpu_to_le16(0xffff);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001879 cp.content_format = cpu_to_le16(hdev->voice_setting);
1880 cp.retrans_effort = 0xff;
1881
1882 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001883 sizeof(cp), &cp);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 } else {
1886 /* Connection rejected */
1887 struct hci_cp_reject_conn_req cp;
1888
1889 bacpy(&cp.bdaddr, &ev->bdaddr);
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02001890 cp.reason = HCI_ERROR_REJ_BAD_ADDR;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001891 hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 }
1893}
1894
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02001895static u8 hci_to_mgmt_reason(u8 err)
1896{
1897 switch (err) {
1898 case HCI_ERROR_CONNECTION_TIMEOUT:
1899 return MGMT_DEV_DISCONN_TIMEOUT;
1900 case HCI_ERROR_REMOTE_USER_TERM:
1901 case HCI_ERROR_REMOTE_LOW_RESOURCES:
1902 case HCI_ERROR_REMOTE_POWER_OFF:
1903 return MGMT_DEV_DISCONN_REMOTE;
1904 case HCI_ERROR_LOCAL_HOST_TERM:
1905 return MGMT_DEV_DISCONN_LOCAL_HOST;
1906 default:
1907 return MGMT_DEV_DISCONN_UNKNOWN;
1908 }
1909}
1910
Gustavo Padovan6039aa732012-05-23 04:04:18 -03001911static void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001913 struct hci_ev_disconn_complete *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02001914 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001916 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 hci_dev_lock(hdev);
1919
Marcel Holtmann04837f62006-07-03 10:02:33 +02001920 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergf7520542011-01-20 12:34:39 +02001921 if (!conn)
1922 goto unlock;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02001923
Johan Hedberg37d9ef72011-11-10 15:54:39 +02001924 if (ev->status == 0)
1925 conn->state = BT_CLOSED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Johan Hedbergb644ba32012-01-17 21:48:47 +02001927 if (test_and_clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03001928 (conn->type == ACL_LINK || conn->type == LE_LINK)) {
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02001929 if (ev->status) {
Johan Hedberg88c3df12012-02-09 14:27:38 +02001930 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001931 conn->dst_type, ev->status);
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02001932 } else {
1933 u8 reason = hci_to_mgmt_reason(ev->reason);
1934
Johan Hedbergafc747a2012-01-15 18:11:07 +02001935 mgmt_device_disconnected(hdev, &conn->dst, conn->type,
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02001936 conn->dst_type, reason);
1937 }
Johan Hedberg37d9ef72011-11-10 15:54:39 +02001938 }
Johan Hedbergf7520542011-01-20 12:34:39 +02001939
Johan Hedberg37d9ef72011-11-10 15:54:39 +02001940 if (ev->status == 0) {
Vishal Agarwal6ec5bca2012-04-16 14:44:44 +05301941 if (conn->type == ACL_LINK && conn->flush_key)
1942 hci_remove_link_key(hdev, &conn->dst);
Johan Hedberg37d9ef72011-11-10 15:54:39 +02001943 hci_proto_disconn_cfm(conn, ev->reason);
1944 hci_conn_del(conn);
1945 }
Johan Hedbergf7520542011-01-20 12:34:39 +02001946
1947unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 hci_dev_unlock(hdev);
1949}
1950
Gustavo Padovan6039aa732012-05-23 04:04:18 -03001951static void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001952{
1953 struct hci_ev_auth_complete *ev = (void *) skb->data;
1954 struct hci_conn *conn;
1955
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001956 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001957
1958 hci_dev_lock(hdev);
1959
1960 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02001961 if (!conn)
1962 goto unlock;
1963
1964 if (!ev->status) {
Johan Hedbergaa64a8b2012-01-18 21:33:12 +02001965 if (!hci_conn_ssp_enabled(conn) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03001966 test_bit(HCI_CONN_REAUTH_PEND, &conn->flags)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02001967 BT_INFO("re-auth of legacy device is not possible.");
Johan Hedberg2a611692011-02-19 12:06:00 -03001968 } else {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02001969 conn->link_mode |= HCI_LM_AUTH;
1970 conn->sec_level = conn->pending_sec_level;
Johan Hedberg2a611692011-02-19 12:06:00 -03001971 }
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02001972 } else {
Johan Hedbergbab73cb2012-02-09 16:07:29 +02001973 mgmt_auth_failed(hdev, &conn->dst, conn->type, conn->dst_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001974 ev->status);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02001975 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001976
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001977 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
1978 clear_bit(HCI_CONN_REAUTH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001979
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02001980 if (conn->state == BT_CONFIG) {
Johan Hedbergaa64a8b2012-01-18 21:33:12 +02001981 if (!ev->status && hci_conn_ssp_enabled(conn)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02001982 struct hci_cp_set_conn_encrypt cp;
1983 cp.handle = ev->handle;
1984 cp.encrypt = 0x01;
1985 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
Gustavo Padovan807deac2012-05-17 00:36:24 -03001986 &cp);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02001987 } else {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02001988 conn->state = BT_CONNECTED;
1989 hci_proto_connect_cfm(conn, ev->status);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02001990 hci_conn_put(conn);
1991 }
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02001992 } else {
1993 hci_auth_cfm(conn, ev->status);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02001994
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02001995 hci_conn_hold(conn);
1996 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
1997 hci_conn_put(conn);
1998 }
1999
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002000 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002001 if (!ev->status) {
2002 struct hci_cp_set_conn_encrypt cp;
2003 cp.handle = ev->handle;
2004 cp.encrypt = 0x01;
2005 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
Gustavo Padovan807deac2012-05-17 00:36:24 -03002006 &cp);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002007 } else {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002008 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002009 hci_encrypt_cfm(conn, ev->status, 0x00);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002010 }
2011 }
2012
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002013unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002014 hci_dev_unlock(hdev);
2015}
2016
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002017static void hci_remote_name_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002018{
Johan Hedberg127178d2010-11-18 22:22:29 +02002019 struct hci_ev_remote_name *ev = (void *) skb->data;
2020 struct hci_conn *conn;
2021
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002022 BT_DBG("%s", hdev->name);
2023
2024 hci_conn_check_pending(hdev);
Johan Hedberg127178d2010-11-18 22:22:29 +02002025
2026 hci_dev_lock(hdev);
2027
2028 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedbergb644ba32012-01-17 21:48:47 +02002029
2030 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
2031 goto check_auth;
2032
2033 if (ev->status == 0)
2034 hci_check_pending_name(hdev, conn, &ev->bdaddr, ev->name,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002035 strnlen(ev->name, HCI_MAX_NAME_LENGTH));
Johan Hedbergb644ba32012-01-17 21:48:47 +02002036 else
2037 hci_check_pending_name(hdev, conn, &ev->bdaddr, NULL, 0);
2038
2039check_auth:
Johan Hedberg79c6c702011-04-28 11:28:55 -07002040 if (!conn)
2041 goto unlock;
2042
2043 if (!hci_outgoing_auth_needed(hdev, conn))
2044 goto unlock;
2045
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002046 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02002047 struct hci_cp_auth_requested cp;
2048 cp.handle = __cpu_to_le16(conn->handle);
2049 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
2050 }
2051
Johan Hedberg79c6c702011-04-28 11:28:55 -07002052unlock:
Johan Hedberg127178d2010-11-18 22:22:29 +02002053 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002054}
2055
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002056static void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002057{
2058 struct hci_ev_encrypt_change *ev = (void *) skb->data;
2059 struct hci_conn *conn;
2060
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002061 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002062
2063 hci_dev_lock(hdev);
2064
2065 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
2066 if (conn) {
2067 if (!ev->status) {
Marcel Holtmannae293192008-07-14 20:13:45 +02002068 if (ev->encrypt) {
2069 /* Encryption implies authentication */
2070 conn->link_mode |= HCI_LM_AUTH;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002071 conn->link_mode |= HCI_LM_ENCRYPT;
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002072 conn->sec_level = conn->pending_sec_level;
Marcel Holtmannae293192008-07-14 20:13:45 +02002073 } else
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002074 conn->link_mode &= ~HCI_LM_ENCRYPT;
2075 }
2076
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002077 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002078
Gustavo Padovana7d77232012-05-13 03:20:07 -03002079 if (ev->status && conn->state == BT_CONNECTED) {
Gustavo Padovand839c812012-05-16 12:17:12 -03002080 hci_acl_disconn(conn, HCI_ERROR_AUTH_FAILURE);
Gustavo Padovana7d77232012-05-13 03:20:07 -03002081 hci_conn_put(conn);
2082 goto unlock;
2083 }
2084
Marcel Holtmannf8558552008-07-14 20:13:49 +02002085 if (conn->state == BT_CONFIG) {
2086 if (!ev->status)
2087 conn->state = BT_CONNECTED;
2088
2089 hci_proto_connect_cfm(conn, ev->status);
2090 hci_conn_put(conn);
2091 } else
2092 hci_encrypt_cfm(conn, ev->status, ev->encrypt);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002093 }
2094
Gustavo Padovana7d77232012-05-13 03:20:07 -03002095unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002096 hci_dev_unlock(hdev);
2097}
2098
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002099static void hci_change_link_key_complete_evt(struct hci_dev *hdev,
2100 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002101{
2102 struct hci_ev_change_link_key_complete *ev = (void *) skb->data;
2103 struct hci_conn *conn;
2104
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002105 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002106
2107 hci_dev_lock(hdev);
2108
2109 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
2110 if (conn) {
2111 if (!ev->status)
2112 conn->link_mode |= HCI_LM_SECURE;
2113
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002114 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002115
2116 hci_key_change_cfm(conn, ev->status);
2117 }
2118
2119 hci_dev_unlock(hdev);
2120}
2121
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002122static void hci_remote_features_evt(struct hci_dev *hdev,
2123 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002124{
2125 struct hci_ev_remote_features *ev = (void *) skb->data;
2126 struct hci_conn *conn;
2127
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002128 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002129
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002130 hci_dev_lock(hdev);
2131
2132 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02002133 if (!conn)
2134 goto unlock;
Marcel Holtmann769be972008-07-14 20:13:49 +02002135
Johan Hedbergccd556f2010-11-10 17:11:51 +02002136 if (!ev->status)
2137 memcpy(conn->features, ev->features, 8);
2138
2139 if (conn->state != BT_CONFIG)
2140 goto unlock;
2141
2142 if (!ev->status && lmp_ssp_capable(hdev) && lmp_ssp_capable(conn)) {
2143 struct hci_cp_read_remote_ext_features cp;
2144 cp.handle = ev->handle;
2145 cp.page = 0x01;
2146 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES,
Gustavo Padovan807deac2012-05-17 00:36:24 -03002147 sizeof(cp), &cp);
Johan Hedberg392599b2010-11-18 22:22:28 +02002148 goto unlock;
2149 }
2150
Johan Hedberg671267b2012-05-12 16:11:50 -03002151 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02002152 struct hci_cp_remote_name_req cp;
2153 memset(&cp, 0, sizeof(cp));
2154 bacpy(&cp.bdaddr, &conn->dst);
2155 cp.pscan_rep_mode = 0x02;
2156 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
Johan Hedbergb644ba32012-01-17 21:48:47 +02002157 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
2158 mgmt_device_connected(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002159 conn->dst_type, 0, NULL, 0,
2160 conn->dev_class);
Johan Hedberg392599b2010-11-18 22:22:28 +02002161
Johan Hedberg127178d2010-11-18 22:22:29 +02002162 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02002163 conn->state = BT_CONNECTED;
2164 hci_proto_connect_cfm(conn, ev->status);
2165 hci_conn_put(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02002166 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002167
Johan Hedbergccd556f2010-11-10 17:11:51 +02002168unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002169 hci_dev_unlock(hdev);
2170}
2171
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002172static void hci_remote_version_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002173{
2174 BT_DBG("%s", hdev->name);
2175}
2176
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002177static void hci_qos_setup_complete_evt(struct hci_dev *hdev,
2178 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002179{
2180 BT_DBG("%s", hdev->name);
2181}
2182
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002183static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002184{
2185 struct hci_ev_cmd_complete *ev = (void *) skb->data;
2186 __u16 opcode;
2187
2188 skb_pull(skb, sizeof(*ev));
2189
2190 opcode = __le16_to_cpu(ev->opcode);
2191
2192 switch (opcode) {
2193 case HCI_OP_INQUIRY_CANCEL:
2194 hci_cc_inquiry_cancel(hdev, skb);
2195 break;
2196
Andre Guedes4d934832012-03-21 00:03:35 -03002197 case HCI_OP_PERIODIC_INQ:
2198 hci_cc_periodic_inq(hdev, skb);
2199 break;
2200
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002201 case HCI_OP_EXIT_PERIODIC_INQ:
2202 hci_cc_exit_periodic_inq(hdev, skb);
2203 break;
2204
2205 case HCI_OP_REMOTE_NAME_REQ_CANCEL:
2206 hci_cc_remote_name_req_cancel(hdev, skb);
2207 break;
2208
2209 case HCI_OP_ROLE_DISCOVERY:
2210 hci_cc_role_discovery(hdev, skb);
2211 break;
2212
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02002213 case HCI_OP_READ_LINK_POLICY:
2214 hci_cc_read_link_policy(hdev, skb);
2215 break;
2216
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002217 case HCI_OP_WRITE_LINK_POLICY:
2218 hci_cc_write_link_policy(hdev, skb);
2219 break;
2220
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02002221 case HCI_OP_READ_DEF_LINK_POLICY:
2222 hci_cc_read_def_link_policy(hdev, skb);
2223 break;
2224
2225 case HCI_OP_WRITE_DEF_LINK_POLICY:
2226 hci_cc_write_def_link_policy(hdev, skb);
2227 break;
2228
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002229 case HCI_OP_RESET:
2230 hci_cc_reset(hdev, skb);
2231 break;
2232
2233 case HCI_OP_WRITE_LOCAL_NAME:
2234 hci_cc_write_local_name(hdev, skb);
2235 break;
2236
2237 case HCI_OP_READ_LOCAL_NAME:
2238 hci_cc_read_local_name(hdev, skb);
2239 break;
2240
2241 case HCI_OP_WRITE_AUTH_ENABLE:
2242 hci_cc_write_auth_enable(hdev, skb);
2243 break;
2244
2245 case HCI_OP_WRITE_ENCRYPT_MODE:
2246 hci_cc_write_encrypt_mode(hdev, skb);
2247 break;
2248
2249 case HCI_OP_WRITE_SCAN_ENABLE:
2250 hci_cc_write_scan_enable(hdev, skb);
2251 break;
2252
2253 case HCI_OP_READ_CLASS_OF_DEV:
2254 hci_cc_read_class_of_dev(hdev, skb);
2255 break;
2256
2257 case HCI_OP_WRITE_CLASS_OF_DEV:
2258 hci_cc_write_class_of_dev(hdev, skb);
2259 break;
2260
2261 case HCI_OP_READ_VOICE_SETTING:
2262 hci_cc_read_voice_setting(hdev, skb);
2263 break;
2264
2265 case HCI_OP_WRITE_VOICE_SETTING:
2266 hci_cc_write_voice_setting(hdev, skb);
2267 break;
2268
2269 case HCI_OP_HOST_BUFFER_SIZE:
2270 hci_cc_host_buffer_size(hdev, skb);
2271 break;
2272
Marcel Holtmann333140b2008-07-14 20:13:48 +02002273 case HCI_OP_WRITE_SSP_MODE:
2274 hci_cc_write_ssp_mode(hdev, skb);
2275 break;
2276
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002277 case HCI_OP_READ_LOCAL_VERSION:
2278 hci_cc_read_local_version(hdev, skb);
2279 break;
2280
2281 case HCI_OP_READ_LOCAL_COMMANDS:
2282 hci_cc_read_local_commands(hdev, skb);
2283 break;
2284
2285 case HCI_OP_READ_LOCAL_FEATURES:
2286 hci_cc_read_local_features(hdev, skb);
2287 break;
2288
Andre Guedes971e3a42011-06-30 19:20:52 -03002289 case HCI_OP_READ_LOCAL_EXT_FEATURES:
2290 hci_cc_read_local_ext_features(hdev, skb);
2291 break;
2292
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002293 case HCI_OP_READ_BUFFER_SIZE:
2294 hci_cc_read_buffer_size(hdev, skb);
2295 break;
2296
2297 case HCI_OP_READ_BD_ADDR:
2298 hci_cc_read_bd_addr(hdev, skb);
2299 break;
2300
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +02002301 case HCI_OP_READ_DATA_BLOCK_SIZE:
2302 hci_cc_read_data_block_size(hdev, skb);
2303 break;
2304
Johan Hedberg23bb5762010-12-21 23:01:27 +02002305 case HCI_OP_WRITE_CA_TIMEOUT:
2306 hci_cc_write_ca_timeout(hdev, skb);
2307 break;
2308
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +02002309 case HCI_OP_READ_FLOW_CONTROL_MODE:
2310 hci_cc_read_flow_control_mode(hdev, skb);
2311 break;
2312
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +03002313 case HCI_OP_READ_LOCAL_AMP_INFO:
2314 hci_cc_read_local_amp_info(hdev, skb);
2315 break;
2316
Johan Hedbergb0916ea2011-01-10 13:44:55 +02002317 case HCI_OP_DELETE_STORED_LINK_KEY:
2318 hci_cc_delete_stored_link_key(hdev, skb);
2319 break;
2320
Johan Hedbergd5859e22011-01-25 01:19:58 +02002321 case HCI_OP_SET_EVENT_MASK:
2322 hci_cc_set_event_mask(hdev, skb);
2323 break;
2324
2325 case HCI_OP_WRITE_INQUIRY_MODE:
2326 hci_cc_write_inquiry_mode(hdev, skb);
2327 break;
2328
2329 case HCI_OP_READ_INQ_RSP_TX_POWER:
2330 hci_cc_read_inq_rsp_tx_power(hdev, skb);
2331 break;
2332
2333 case HCI_OP_SET_EVENT_FLT:
2334 hci_cc_set_event_flt(hdev, skb);
2335 break;
2336
Johan Hedberg980e1a52011-01-22 06:10:07 +02002337 case HCI_OP_PIN_CODE_REPLY:
2338 hci_cc_pin_code_reply(hdev, skb);
2339 break;
2340
2341 case HCI_OP_PIN_CODE_NEG_REPLY:
2342 hci_cc_pin_code_neg_reply(hdev, skb);
2343 break;
2344
Szymon Jancc35938b2011-03-22 13:12:21 +01002345 case HCI_OP_READ_LOCAL_OOB_DATA:
2346 hci_cc_read_local_oob_data_reply(hdev, skb);
2347 break;
2348
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002349 case HCI_OP_LE_READ_BUFFER_SIZE:
2350 hci_cc_le_read_buffer_size(hdev, skb);
2351 break;
2352
Johan Hedberga5c29682011-02-19 12:05:57 -03002353 case HCI_OP_USER_CONFIRM_REPLY:
2354 hci_cc_user_confirm_reply(hdev, skb);
2355 break;
2356
2357 case HCI_OP_USER_CONFIRM_NEG_REPLY:
2358 hci_cc_user_confirm_neg_reply(hdev, skb);
2359 break;
2360
Brian Gix1143d452011-11-23 08:28:34 -08002361 case HCI_OP_USER_PASSKEY_REPLY:
2362 hci_cc_user_passkey_reply(hdev, skb);
2363 break;
2364
2365 case HCI_OP_USER_PASSKEY_NEG_REPLY:
2366 hci_cc_user_passkey_neg_reply(hdev, skb);
Szymon Janc16cde992012-04-13 12:32:42 +02002367 break;
Andre Guedes07f7fa52011-12-02 21:13:31 +09002368
2369 case HCI_OP_LE_SET_SCAN_PARAM:
2370 hci_cc_le_set_scan_param(hdev, skb);
Brian Gix1143d452011-11-23 08:28:34 -08002371 break;
2372
Andre Guedeseb9d91f2011-05-26 16:23:52 -03002373 case HCI_OP_LE_SET_SCAN_ENABLE:
2374 hci_cc_le_set_scan_enable(hdev, skb);
2375 break;
2376
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03002377 case HCI_OP_LE_LTK_REPLY:
2378 hci_cc_le_ltk_reply(hdev, skb);
2379 break;
2380
2381 case HCI_OP_LE_LTK_NEG_REPLY:
2382 hci_cc_le_ltk_neg_reply(hdev, skb);
2383 break;
2384
Andre Guedesf9b49302011-06-30 19:20:53 -03002385 case HCI_OP_WRITE_LE_HOST_SUPPORTED:
2386 hci_cc_write_le_host_supported(hdev, skb);
2387 break;
2388
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002389 default:
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002390 BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002391 break;
2392 }
2393
Ville Tervo6bd32322011-02-16 16:32:41 +02002394 if (ev->opcode != HCI_OP_NOP)
2395 del_timer(&hdev->cmd_timer);
2396
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002397 if (ev->ncmd) {
2398 atomic_set(&hdev->cmd_cnt, 1);
2399 if (!skb_queue_empty(&hdev->cmd_q))
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02002400 queue_work(hdev->workqueue, &hdev->cmd_work);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002401 }
2402}
2403
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002404static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002405{
2406 struct hci_ev_cmd_status *ev = (void *) skb->data;
2407 __u16 opcode;
2408
2409 skb_pull(skb, sizeof(*ev));
2410
2411 opcode = __le16_to_cpu(ev->opcode);
2412
2413 switch (opcode) {
2414 case HCI_OP_INQUIRY:
2415 hci_cs_inquiry(hdev, ev->status);
2416 break;
2417
2418 case HCI_OP_CREATE_CONN:
2419 hci_cs_create_conn(hdev, ev->status);
2420 break;
2421
2422 case HCI_OP_ADD_SCO:
2423 hci_cs_add_sco(hdev, ev->status);
2424 break;
2425
Marcel Holtmannf8558552008-07-14 20:13:49 +02002426 case HCI_OP_AUTH_REQUESTED:
2427 hci_cs_auth_requested(hdev, ev->status);
2428 break;
2429
2430 case HCI_OP_SET_CONN_ENCRYPT:
2431 hci_cs_set_conn_encrypt(hdev, ev->status);
2432 break;
2433
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002434 case HCI_OP_REMOTE_NAME_REQ:
2435 hci_cs_remote_name_req(hdev, ev->status);
2436 break;
2437
Marcel Holtmann769be972008-07-14 20:13:49 +02002438 case HCI_OP_READ_REMOTE_FEATURES:
2439 hci_cs_read_remote_features(hdev, ev->status);
2440 break;
2441
2442 case HCI_OP_READ_REMOTE_EXT_FEATURES:
2443 hci_cs_read_remote_ext_features(hdev, ev->status);
2444 break;
2445
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002446 case HCI_OP_SETUP_SYNC_CONN:
2447 hci_cs_setup_sync_conn(hdev, ev->status);
2448 break;
2449
2450 case HCI_OP_SNIFF_MODE:
2451 hci_cs_sniff_mode(hdev, ev->status);
2452 break;
2453
2454 case HCI_OP_EXIT_SNIFF_MODE:
2455 hci_cs_exit_sniff_mode(hdev, ev->status);
2456 break;
2457
Johan Hedberg8962ee72011-01-20 12:40:27 +02002458 case HCI_OP_DISCONNECT:
Johan Hedberg88c3df12012-02-09 14:27:38 +02002459 hci_cs_disconnect(hdev, ev->status);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002460 break;
2461
Ville Tervofcd89c02011-02-10 22:38:47 -03002462 case HCI_OP_LE_CREATE_CONN:
2463 hci_cs_le_create_conn(hdev, ev->status);
2464 break;
2465
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03002466 case HCI_OP_LE_START_ENC:
2467 hci_cs_le_start_enc(hdev, ev->status);
2468 break;
2469
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002470 default:
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002471 BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002472 break;
2473 }
2474
Ville Tervo6bd32322011-02-16 16:32:41 +02002475 if (ev->opcode != HCI_OP_NOP)
2476 del_timer(&hdev->cmd_timer);
2477
Gustavo F. Padovan10572132011-03-16 15:36:29 -03002478 if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags)) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002479 atomic_set(&hdev->cmd_cnt, 1);
2480 if (!skb_queue_empty(&hdev->cmd_q))
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02002481 queue_work(hdev->workqueue, &hdev->cmd_work);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002482 }
2483}
2484
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002485static void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002486{
2487 struct hci_ev_role_change *ev = (void *) skb->data;
2488 struct hci_conn *conn;
2489
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002490 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002491
2492 hci_dev_lock(hdev);
2493
2494 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
2495 if (conn) {
2496 if (!ev->status) {
2497 if (ev->role)
2498 conn->link_mode &= ~HCI_LM_MASTER;
2499 else
2500 conn->link_mode |= HCI_LM_MASTER;
2501 }
2502
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002503 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002504
2505 hci_role_switch_cfm(conn, ev->status, ev->role);
2506 }
2507
2508 hci_dev_unlock(hdev);
2509}
2510
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002511static void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002513 struct hci_ev_num_comp_pkts *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 int i;
2515
Andrei Emeltchenko32ac5b92011-12-19 16:31:29 +02002516 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_PACKET_BASED) {
2517 BT_ERR("Wrong event for mode %d", hdev->flow_ctl_mode);
2518 return;
2519 }
2520
Andrei Emeltchenkoc5993de2011-12-30 12:07:47 +02002521 if (skb->len < sizeof(*ev) || skb->len < sizeof(*ev) +
Gustavo Padovan807deac2012-05-17 00:36:24 -03002522 ev->num_hndl * sizeof(struct hci_comp_pkts_info)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 BT_DBG("%s bad parameters", hdev->name);
2524 return;
2525 }
2526
Andrei Emeltchenkoc5993de2011-12-30 12:07:47 +02002527 BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
2528
Andrei Emeltchenko613a1c02011-12-19 16:31:30 +02002529 for (i = 0; i < ev->num_hndl; i++) {
2530 struct hci_comp_pkts_info *info = &ev->handles[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 struct hci_conn *conn;
2532 __u16 handle, count;
2533
Andrei Emeltchenko613a1c02011-12-19 16:31:30 +02002534 handle = __le16_to_cpu(info->handle);
2535 count = __le16_to_cpu(info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
2537 conn = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02002538 if (!conn)
2539 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02002541 conn->sent -= count;
2542
2543 switch (conn->type) {
2544 case ACL_LINK:
2545 hdev->acl_cnt += count;
2546 if (hdev->acl_cnt > hdev->acl_pkts)
2547 hdev->acl_cnt = hdev->acl_pkts;
2548 break;
2549
2550 case LE_LINK:
2551 if (hdev->le_pkts) {
2552 hdev->le_cnt += count;
2553 if (hdev->le_cnt > hdev->le_pkts)
2554 hdev->le_cnt = hdev->le_pkts;
2555 } else {
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02002556 hdev->acl_cnt += count;
2557 if (hdev->acl_cnt > hdev->acl_pkts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 hdev->acl_cnt = hdev->acl_pkts;
2559 }
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02002560 break;
2561
2562 case SCO_LINK:
2563 hdev->sco_cnt += count;
2564 if (hdev->sco_cnt > hdev->sco_pkts)
2565 hdev->sco_cnt = hdev->sco_pkts;
2566 break;
2567
2568 default:
2569 BT_ERR("Unknown type %d conn %p", conn->type, conn);
2570 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 }
2572 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002573
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02002574 queue_work(hdev->workqueue, &hdev->tx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575}
2576
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002577static void hci_num_comp_blocks_evt(struct hci_dev *hdev, struct sk_buff *skb)
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02002578{
2579 struct hci_ev_num_comp_blocks *ev = (void *) skb->data;
2580 int i;
2581
2582 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_BLOCK_BASED) {
2583 BT_ERR("Wrong event for mode %d", hdev->flow_ctl_mode);
2584 return;
2585 }
2586
2587 if (skb->len < sizeof(*ev) || skb->len < sizeof(*ev) +
Gustavo Padovan807deac2012-05-17 00:36:24 -03002588 ev->num_hndl * sizeof(struct hci_comp_blocks_info)) {
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02002589 BT_DBG("%s bad parameters", hdev->name);
2590 return;
2591 }
2592
2593 BT_DBG("%s num_blocks %d num_hndl %d", hdev->name, ev->num_blocks,
Gustavo Padovan807deac2012-05-17 00:36:24 -03002594 ev->num_hndl);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02002595
2596 for (i = 0; i < ev->num_hndl; i++) {
2597 struct hci_comp_blocks_info *info = &ev->handles[i];
2598 struct hci_conn *conn;
2599 __u16 handle, block_count;
2600
2601 handle = __le16_to_cpu(info->handle);
2602 block_count = __le16_to_cpu(info->blocks);
2603
2604 conn = hci_conn_hash_lookup_handle(hdev, handle);
2605 if (!conn)
2606 continue;
2607
2608 conn->sent -= block_count;
2609
2610 switch (conn->type) {
2611 case ACL_LINK:
2612 hdev->block_cnt += block_count;
2613 if (hdev->block_cnt > hdev->num_blocks)
2614 hdev->block_cnt = hdev->num_blocks;
2615 break;
2616
2617 default:
2618 BT_ERR("Unknown type %d conn %p", conn->type, conn);
2619 break;
2620 }
2621 }
2622
2623 queue_work(hdev->workqueue, &hdev->tx_work);
2624}
2625
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002626static void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002628 struct hci_ev_mode_change *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02002629 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002631 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
2633 hci_dev_lock(hdev);
2634
Marcel Holtmann04837f62006-07-03 10:02:33 +02002635 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
2636 if (conn) {
2637 conn->mode = ev->mode;
2638 conn->interval = __le16_to_cpu(ev->interval);
2639
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03002640 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND,
2641 &conn->flags)) {
Marcel Holtmann04837f62006-07-03 10:02:33 +02002642 if (conn->mode == HCI_CM_ACTIVE)
Johan Hedberg58a681e2012-01-16 06:47:28 +02002643 set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +02002644 else
Johan Hedberg58a681e2012-01-16 06:47:28 +02002645 clear_bit(HCI_CONN_POWER_SAVE, &conn->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +02002646 }
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002647
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002648 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002649 hci_sco_setup(conn, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02002650 }
2651
2652 hci_dev_unlock(hdev);
2653}
2654
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002655static void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002657 struct hci_ev_pin_code_req *ev = (void *) skb->data;
2658 struct hci_conn *conn;
2659
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002660 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002661
2662 hci_dev_lock(hdev);
2663
2664 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Waldemar Rymarkiewiczb6f98042011-09-23 10:01:30 +02002665 if (!conn)
2666 goto unlock;
2667
2668 if (conn->state == BT_CONNECTED) {
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002669 hci_conn_hold(conn);
2670 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
2671 hci_conn_put(conn);
2672 }
2673
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02002674 if (!test_bit(HCI_PAIRABLE, &hdev->dev_flags))
Johan Hedberg03b555e2011-01-04 15:40:05 +02002675 hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03002676 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02002677 else if (test_bit(HCI_MGMT, &hdev->dev_flags)) {
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02002678 u8 secure;
2679
2680 if (conn->pending_sec_level == BT_SECURITY_HIGH)
2681 secure = 1;
2682 else
2683 secure = 0;
2684
Johan Hedberg744cf192011-11-08 20:40:14 +02002685 mgmt_pin_code_request(hdev, &ev->bdaddr, secure);
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02002686 }
Johan Hedberg980e1a52011-01-22 06:10:07 +02002687
Waldemar Rymarkiewiczb6f98042011-09-23 10:01:30 +02002688unlock:
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002689 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690}
2691
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002692static void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693{
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002694 struct hci_ev_link_key_req *ev = (void *) skb->data;
2695 struct hci_cp_link_key_reply cp;
2696 struct hci_conn *conn;
2697 struct link_key *key;
2698
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002699 BT_DBG("%s", hdev->name);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002700
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02002701 if (!test_bit(HCI_LINK_KEYS, &hdev->dev_flags))
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002702 return;
2703
2704 hci_dev_lock(hdev);
2705
2706 key = hci_find_link_key(hdev, &ev->bdaddr);
2707 if (!key) {
2708 BT_DBG("%s link key not found for %s", hdev->name,
Gustavo Padovan807deac2012-05-17 00:36:24 -03002709 batostr(&ev->bdaddr));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002710 goto not_found;
2711 }
2712
2713 BT_DBG("%s found key type %u for %s", hdev->name, key->type,
Gustavo Padovan807deac2012-05-17 00:36:24 -03002714 batostr(&ev->bdaddr));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002715
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02002716 if (!test_bit(HCI_DEBUG_KEYS, &hdev->dev_flags) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03002717 key->type == HCI_LK_DEBUG_COMBINATION) {
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002718 BT_DBG("%s ignoring debug key", hdev->name);
2719 goto not_found;
2720 }
2721
2722 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02002723 if (conn) {
2724 if (key->type == HCI_LK_UNAUTH_COMBINATION &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03002725 conn->auth_type != 0xff && (conn->auth_type & 0x01)) {
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02002726 BT_DBG("%s ignoring unauthenticated key", hdev->name);
2727 goto not_found;
2728 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002729
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02002730 if (key->type == HCI_LK_COMBINATION && key->pin_len < 16 &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03002731 conn->pending_sec_level == BT_SECURITY_HIGH) {
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03002732 BT_DBG("%s ignoring key unauthenticated for high security",
2733 hdev->name);
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02002734 goto not_found;
2735 }
2736
2737 conn->key_type = key->type;
2738 conn->pin_length = key->pin_len;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002739 }
2740
2741 bacpy(&cp.bdaddr, &ev->bdaddr);
Andrei Emeltchenko9b3b4462012-05-23 11:31:20 +03002742 memcpy(cp.link_key, key->val, HCI_LINK_KEY_SIZE);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002743
2744 hci_send_cmd(hdev, HCI_OP_LINK_KEY_REPLY, sizeof(cp), &cp);
2745
2746 hci_dev_unlock(hdev);
2747
2748 return;
2749
2750not_found:
2751 hci_send_cmd(hdev, HCI_OP_LINK_KEY_NEG_REPLY, 6, &ev->bdaddr);
2752 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753}
2754
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002755static void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002757 struct hci_ev_link_key_notify *ev = (void *) skb->data;
2758 struct hci_conn *conn;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002759 u8 pin_len = 0;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002760
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002761 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002762
2763 hci_dev_lock(hdev);
2764
2765 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
2766 if (conn) {
2767 hci_conn_hold(conn);
2768 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Johan Hedberg980e1a52011-01-22 06:10:07 +02002769 pin_len = conn->pin_length;
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +02002770
2771 if (ev->key_type != HCI_LK_CHANGED_COMBINATION)
2772 conn->key_type = ev->key_type;
2773
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002774 hci_conn_put(conn);
2775 }
2776
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02002777 if (test_bit(HCI_LINK_KEYS, &hdev->dev_flags))
Johan Hedbergd25e28a2011-04-28 11:28:59 -07002778 hci_add_link_key(hdev, conn, 1, &ev->bdaddr, ev->link_key,
Gustavo Padovan807deac2012-05-17 00:36:24 -03002779 ev->key_type, pin_len);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002780
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002781 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782}
2783
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002784static void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann04837f62006-07-03 10:02:33 +02002785{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002786 struct hci_ev_clock_offset *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02002787 struct hci_conn *conn;
2788
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002789 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02002790
2791 hci_dev_lock(hdev);
2792
2793 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 if (conn && !ev->status) {
2795 struct inquiry_entry *ie;
2796
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02002797 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
2798 if (ie) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 ie->data.clock_offset = ev->clock_offset;
2800 ie->timestamp = jiffies;
2801 }
2802 }
2803
2804 hci_dev_unlock(hdev);
2805}
2806
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002807static void hci_pkt_type_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna8746412008-07-14 20:13:46 +02002808{
2809 struct hci_ev_pkt_type_change *ev = (void *) skb->data;
2810 struct hci_conn *conn;
2811
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002812 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna8746412008-07-14 20:13:46 +02002813
2814 hci_dev_lock(hdev);
2815
2816 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
2817 if (conn && !ev->status)
2818 conn->pkt_type = __le16_to_cpu(ev->pkt_type);
2819
2820 hci_dev_unlock(hdev);
2821}
2822
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002823static void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann85a1e932005-08-09 20:28:02 -07002824{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002825 struct hci_ev_pscan_rep_mode *ev = (void *) skb->data;
Marcel Holtmann85a1e932005-08-09 20:28:02 -07002826 struct inquiry_entry *ie;
2827
2828 BT_DBG("%s", hdev->name);
2829
2830 hci_dev_lock(hdev);
2831
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02002832 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
2833 if (ie) {
Marcel Holtmann85a1e932005-08-09 20:28:02 -07002834 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
2835 ie->timestamp = jiffies;
2836 }
2837
2838 hci_dev_unlock(hdev);
2839}
2840
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002841static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev,
2842 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002843{
2844 struct inquiry_data data;
2845 int num_rsp = *((__u8 *) skb->data);
Johan Hedberg388fc8f2012-02-23 00:38:59 +02002846 bool name_known, ssp;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002847
2848 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
2849
2850 if (!num_rsp)
2851 return;
2852
Andre Guedes1519cc12012-03-21 00:03:38 -03002853 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags))
2854 return;
2855
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002856 hci_dev_lock(hdev);
2857
2858 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
Szymon Janc138d22e2011-02-17 16:44:23 +01002859 struct inquiry_info_with_rssi_and_pscan_mode *info;
2860 info = (void *) (skb->data + 1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002861
Johan Hedberge17acd42011-03-30 23:57:16 +03002862 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002863 bacpy(&data.bdaddr, &info->bdaddr);
2864 data.pscan_rep_mode = info->pscan_rep_mode;
2865 data.pscan_period_mode = info->pscan_period_mode;
2866 data.pscan_mode = info->pscan_mode;
2867 memcpy(data.dev_class, info->dev_class, 3);
2868 data.clock_offset = info->clock_offset;
2869 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002870 data.ssp_mode = 0x00;
Johan Hedberg31754052012-01-04 13:39:52 +02002871
2872 name_known = hci_inquiry_cache_update(hdev, &data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002873 false, &ssp);
Johan Hedberg48264f02011-11-09 13:58:58 +02002874 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002875 info->dev_class, info->rssi,
2876 !name_known, ssp, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002877 }
2878 } else {
2879 struct inquiry_info_with_rssi *info = (void *) (skb->data + 1);
2880
Johan Hedberge17acd42011-03-30 23:57:16 +03002881 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002882 bacpy(&data.bdaddr, &info->bdaddr);
2883 data.pscan_rep_mode = info->pscan_rep_mode;
2884 data.pscan_period_mode = info->pscan_period_mode;
2885 data.pscan_mode = 0x00;
2886 memcpy(data.dev_class, info->dev_class, 3);
2887 data.clock_offset = info->clock_offset;
2888 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002889 data.ssp_mode = 0x00;
Johan Hedberg31754052012-01-04 13:39:52 +02002890 name_known = hci_inquiry_cache_update(hdev, &data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002891 false, &ssp);
Johan Hedberg48264f02011-11-09 13:58:58 +02002892 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002893 info->dev_class, info->rssi,
2894 !name_known, ssp, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002895 }
2896 }
2897
2898 hci_dev_unlock(hdev);
2899}
2900
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002901static void hci_remote_ext_features_evt(struct hci_dev *hdev,
2902 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002903{
Marcel Holtmann41a96212008-07-14 20:13:48 +02002904 struct hci_ev_remote_ext_features *ev = (void *) skb->data;
2905 struct hci_conn *conn;
2906
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002907 BT_DBG("%s", hdev->name);
Marcel Holtmann41a96212008-07-14 20:13:48 +02002908
Marcel Holtmann41a96212008-07-14 20:13:48 +02002909 hci_dev_lock(hdev);
2910
2911 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02002912 if (!conn)
2913 goto unlock;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002914
Johan Hedbergccd556f2010-11-10 17:11:51 +02002915 if (!ev->status && ev->page == 0x01) {
2916 struct inquiry_entry *ie;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002917
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02002918 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
2919 if (ie)
Johan Hedberg02b7cc62012-02-28 02:28:43 +02002920 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
Marcel Holtmann769be972008-07-14 20:13:49 +02002921
Johan Hedberg02b7cc62012-02-28 02:28:43 +02002922 if (ev->features[0] & LMP_HOST_SSP)
Johan Hedberg58a681e2012-01-16 06:47:28 +02002923 set_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
Marcel Holtmann41a96212008-07-14 20:13:48 +02002924 }
2925
Johan Hedbergccd556f2010-11-10 17:11:51 +02002926 if (conn->state != BT_CONFIG)
2927 goto unlock;
2928
Johan Hedberg671267b2012-05-12 16:11:50 -03002929 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02002930 struct hci_cp_remote_name_req cp;
2931 memset(&cp, 0, sizeof(cp));
2932 bacpy(&cp.bdaddr, &conn->dst);
2933 cp.pscan_rep_mode = 0x02;
2934 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
Johan Hedbergb644ba32012-01-17 21:48:47 +02002935 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
2936 mgmt_device_connected(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002937 conn->dst_type, 0, NULL, 0,
2938 conn->dev_class);
Johan Hedberg392599b2010-11-18 22:22:28 +02002939
Johan Hedberg127178d2010-11-18 22:22:29 +02002940 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02002941 conn->state = BT_CONNECTED;
2942 hci_proto_connect_cfm(conn, ev->status);
2943 hci_conn_put(conn);
2944 }
2945
2946unlock:
Marcel Holtmann41a96212008-07-14 20:13:48 +02002947 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002948}
2949
Gustavo Padovan6039aa732012-05-23 04:04:18 -03002950static void hci_sync_conn_complete_evt(struct hci_dev *hdev,
2951 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002952{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002953 struct hci_ev_sync_conn_complete *ev = (void *) skb->data;
2954 struct hci_conn *conn;
2955
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002956 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002957
2958 hci_dev_lock(hdev);
2959
2960 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02002961 if (!conn) {
2962 if (ev->link_type == ESCO_LINK)
2963 goto unlock;
2964
2965 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
2966 if (!conn)
2967 goto unlock;
2968
2969 conn->type = SCO_LINK;
2970 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002971
Marcel Holtmann732547f2009-04-19 19:14:14 +02002972 switch (ev->status) {
2973 case 0x00:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002974 conn->handle = __le16_to_cpu(ev->handle);
2975 conn->state = BT_CONNECTED;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002976
Marcel Holtmann9eba32b2009-08-22 14:19:26 -07002977 hci_conn_hold_device(conn);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002978 hci_conn_add_sysfs(conn);
Marcel Holtmann732547f2009-04-19 19:14:14 +02002979 break;
2980
Stephen Coe705e5712010-02-16 11:29:44 -05002981 case 0x11: /* Unsupported Feature or Parameter Value */
Marcel Holtmann732547f2009-04-19 19:14:14 +02002982 case 0x1c: /* SCO interval rejected */
Nick Pelly1038a002010-02-03 11:42:26 -08002983 case 0x1a: /* Unsupported Remote Feature */
Marcel Holtmann732547f2009-04-19 19:14:14 +02002984 case 0x1f: /* Unspecified error */
2985 if (conn->out && conn->attempt < 2) {
2986 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
2987 (hdev->esco_type & EDR_ESCO_MASK);
2988 hci_setup_sync(conn, conn->link->handle);
2989 goto unlock;
2990 }
2991 /* fall through */
2992
2993 default:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002994 conn->state = BT_CLOSED;
Marcel Holtmann732547f2009-04-19 19:14:14 +02002995 break;
2996 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002997
2998 hci_proto_connect_cfm(conn, ev->status);
2999 if (ev->status)
3000 hci_conn_del(conn);
3001
3002unlock:
3003 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003004}
3005
Gustavo Padovan6039aa732012-05-23 04:04:18 -03003006static void hci_sync_conn_changed_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003007{
3008 BT_DBG("%s", hdev->name);
3009}
3010
Gustavo Padovan6039aa732012-05-23 04:04:18 -03003011static void hci_sniff_subrate_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann04837f62006-07-03 10:02:33 +02003012{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003013 struct hci_ev_sniff_subrate *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02003014
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003015 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02003016}
3017
Gustavo Padovan6039aa732012-05-23 04:04:18 -03003018static void hci_extended_inquiry_result_evt(struct hci_dev *hdev,
3019 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003020{
3021 struct inquiry_data data;
3022 struct extended_inquiry_info *info = (void *) (skb->data + 1);
3023 int num_rsp = *((__u8 *) skb->data);
Vishal Agarwal9d939d92012-04-26 19:19:56 +05303024 size_t eir_len;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003025
3026 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
3027
3028 if (!num_rsp)
3029 return;
3030
Andre Guedes1519cc12012-03-21 00:03:38 -03003031 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags))
3032 return;
3033
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003034 hci_dev_lock(hdev);
3035
Johan Hedberge17acd42011-03-30 23:57:16 +03003036 for (; num_rsp; num_rsp--, info++) {
Johan Hedberg388fc8f2012-02-23 00:38:59 +02003037 bool name_known, ssp;
Johan Hedberg561aafb2012-01-04 13:31:59 +02003038
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003039 bacpy(&data.bdaddr, &info->bdaddr);
Szymon Janc138d22e2011-02-17 16:44:23 +01003040 data.pscan_rep_mode = info->pscan_rep_mode;
3041 data.pscan_period_mode = info->pscan_period_mode;
3042 data.pscan_mode = 0x00;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003043 memcpy(data.dev_class, info->dev_class, 3);
Szymon Janc138d22e2011-02-17 16:44:23 +01003044 data.clock_offset = info->clock_offset;
3045 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003046 data.ssp_mode = 0x01;
Johan Hedberg561aafb2012-01-04 13:31:59 +02003047
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003048 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg4ddb1932012-01-15 20:04:43 +02003049 name_known = eir_has_data_type(info->data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003050 sizeof(info->data),
3051 EIR_NAME_COMPLETE);
Johan Hedberg561aafb2012-01-04 13:31:59 +02003052 else
3053 name_known = true;
3054
Johan Hedberg388fc8f2012-02-23 00:38:59 +02003055 name_known = hci_inquiry_cache_update(hdev, &data, name_known,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003056 &ssp);
Vishal Agarwal9d939d92012-04-26 19:19:56 +05303057 eir_len = eir_get_length(info->data, sizeof(info->data));
Johan Hedberg48264f02011-11-09 13:58:58 +02003058 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003059 info->dev_class, info->rssi, !name_known,
Vishal Agarwal9d939d92012-04-26 19:19:56 +05303060 ssp, info->data, eir_len);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003061 }
3062
3063 hci_dev_unlock(hdev);
3064}
3065
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003066static void hci_key_refresh_complete_evt(struct hci_dev *hdev,
3067 struct sk_buff *skb)
3068{
3069 struct hci_ev_key_refresh_complete *ev = (void *) skb->data;
3070 struct hci_conn *conn;
3071
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003072 BT_DBG("%s status 0x%2.2x handle 0x%4.4x", hdev->name, ev->status,
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003073 __le16_to_cpu(ev->handle));
3074
3075 hci_dev_lock(hdev);
3076
3077 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3078 if (!conn)
3079 goto unlock;
3080
3081 if (!ev->status)
3082 conn->sec_level = conn->pending_sec_level;
3083
3084 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
3085
3086 if (ev->status && conn->state == BT_CONNECTED) {
3087 hci_acl_disconn(conn, HCI_ERROR_AUTH_FAILURE);
3088 hci_conn_put(conn);
3089 goto unlock;
3090 }
3091
3092 if (conn->state == BT_CONFIG) {
3093 if (!ev->status)
3094 conn->state = BT_CONNECTED;
3095
3096 hci_proto_connect_cfm(conn, ev->status);
3097 hci_conn_put(conn);
3098 } else {
3099 hci_auth_cfm(conn, ev->status);
3100
3101 hci_conn_hold(conn);
3102 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
3103 hci_conn_put(conn);
3104 }
3105
3106unlock:
3107 hci_dev_unlock(hdev);
3108}
3109
Gustavo Padovan6039aa732012-05-23 04:04:18 -03003110static u8 hci_get_auth_req(struct hci_conn *conn)
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003111{
3112 /* If remote requests dedicated bonding follow that lead */
3113 if (conn->remote_auth == 0x02 || conn->remote_auth == 0x03) {
3114 /* If both remote and local IO capabilities allow MITM
3115 * protection then require it, otherwise don't */
3116 if (conn->remote_cap == 0x03 || conn->io_capability == 0x03)
3117 return 0x02;
3118 else
3119 return 0x03;
3120 }
3121
3122 /* If remote requests no-bonding follow that lead */
3123 if (conn->remote_auth == 0x00 || conn->remote_auth == 0x01)
Waldemar Rymarkiewicz58797bf2011-04-28 12:07:58 +02003124 return conn->remote_auth | (conn->auth_type & 0x01);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003125
3126 return conn->auth_type;
3127}
3128
Gustavo Padovan6039aa732012-05-23 04:04:18 -03003129static void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann04936842008-07-14 20:13:48 +02003130{
3131 struct hci_ev_io_capa_request *ev = (void *) skb->data;
3132 struct hci_conn *conn;
3133
3134 BT_DBG("%s", hdev->name);
3135
3136 hci_dev_lock(hdev);
3137
3138 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003139 if (!conn)
3140 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02003141
Johan Hedberg03b555e2011-01-04 15:40:05 +02003142 hci_conn_hold(conn);
3143
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003144 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg03b555e2011-01-04 15:40:05 +02003145 goto unlock;
3146
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003147 if (test_bit(HCI_PAIRABLE, &hdev->dev_flags) ||
Gustavo Padovan807deac2012-05-17 00:36:24 -03003148 (conn->remote_auth & ~0x01) == HCI_AT_NO_BONDING) {
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003149 struct hci_cp_io_capability_reply cp;
3150
3151 bacpy(&cp.bdaddr, &ev->bdaddr);
Hemant Gupta7a7f1e72012-01-16 13:34:29 +05303152 /* Change the IO capability from KeyboardDisplay
3153 * to DisplayYesNo as it is not supported by BT spec. */
3154 cp.capability = (conn->io_capability == 0x04) ?
3155 0x01 : conn->io_capability;
Johan Hedberg7cbc9bd2011-04-28 11:29:04 -07003156 conn->auth_type = hci_get_auth_req(conn);
3157 cp.authentication = conn->auth_type;
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003158
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03003159 if (hci_find_remote_oob_data(hdev, &conn->dst) &&
3160 (conn->out || test_bit(HCI_CONN_REMOTE_OOB, &conn->flags)))
Szymon Jancce85ee12011-03-22 13:12:23 +01003161 cp.oob_data = 0x01;
3162 else
3163 cp.oob_data = 0x00;
3164
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003165 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003166 sizeof(cp), &cp);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003167 } else {
3168 struct hci_cp_io_capability_neg_reply cp;
3169
3170 bacpy(&cp.bdaddr, &ev->bdaddr);
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02003171 cp.reason = HCI_ERROR_PAIRING_NOT_ALLOWED;
Johan Hedberg03b555e2011-01-04 15:40:05 +02003172
3173 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003174 sizeof(cp), &cp);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003175 }
3176
3177unlock:
3178 hci_dev_unlock(hdev);
3179}
3180
Gustavo Padovan6039aa732012-05-23 04:04:18 -03003181static void hci_io_capa_reply_evt(struct hci_dev *hdev, struct sk_buff *skb)
Johan Hedberg03b555e2011-01-04 15:40:05 +02003182{
3183 struct hci_ev_io_capa_reply *ev = (void *) skb->data;
3184 struct hci_conn *conn;
3185
3186 BT_DBG("%s", hdev->name);
3187
3188 hci_dev_lock(hdev);
3189
3190 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3191 if (!conn)
3192 goto unlock;
3193
Johan Hedberg03b555e2011-01-04 15:40:05 +02003194 conn->remote_cap = ev->capability;
Johan Hedberg03b555e2011-01-04 15:40:05 +02003195 conn->remote_auth = ev->authentication;
Johan Hedberg58a681e2012-01-16 06:47:28 +02003196 if (ev->oob_data)
3197 set_bit(HCI_CONN_REMOTE_OOB, &conn->flags);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003198
3199unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02003200 hci_dev_unlock(hdev);
3201}
3202
Gustavo Padovan6039aa732012-05-23 04:04:18 -03003203static void hci_user_confirm_request_evt(struct hci_dev *hdev,
3204 struct sk_buff *skb)
Johan Hedberga5c29682011-02-19 12:05:57 -03003205{
3206 struct hci_ev_user_confirm_req *ev = (void *) skb->data;
Johan Hedberg55bc1a32011-04-28 11:28:56 -07003207 int loc_mitm, rem_mitm, confirm_hint = 0;
Johan Hedberg7a828902011-04-28 11:28:53 -07003208 struct hci_conn *conn;
Johan Hedberga5c29682011-02-19 12:05:57 -03003209
3210 BT_DBG("%s", hdev->name);
3211
3212 hci_dev_lock(hdev);
3213
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003214 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg7a828902011-04-28 11:28:53 -07003215 goto unlock;
Johan Hedberga5c29682011-02-19 12:05:57 -03003216
Johan Hedberg7a828902011-04-28 11:28:53 -07003217 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3218 if (!conn)
3219 goto unlock;
3220
3221 loc_mitm = (conn->auth_type & 0x01);
3222 rem_mitm = (conn->remote_auth & 0x01);
3223
3224 /* If we require MITM but the remote device can't provide that
3225 * (it has NoInputNoOutput) then reject the confirmation
3226 * request. The only exception is when we're dedicated bonding
3227 * initiators (connect_cfm_cb set) since then we always have the MITM
3228 * bit set. */
3229 if (!conn->connect_cfm_cb && loc_mitm && conn->remote_cap == 0x03) {
3230 BT_DBG("Rejecting request: remote device can't provide MITM");
3231 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003232 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberg7a828902011-04-28 11:28:53 -07003233 goto unlock;
3234 }
3235
3236 /* If no side requires MITM protection; auto-accept */
3237 if ((!loc_mitm || conn->remote_cap == 0x03) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03003238 (!rem_mitm || conn->io_capability == 0x03)) {
Johan Hedberg55bc1a32011-04-28 11:28:56 -07003239
3240 /* If we're not the initiators request authorization to
3241 * proceed from user space (mgmt_user_confirm with
3242 * confirm_hint set to 1). */
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003243 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
Johan Hedberg55bc1a32011-04-28 11:28:56 -07003244 BT_DBG("Confirming auto-accept as acceptor");
3245 confirm_hint = 1;
3246 goto confirm;
3247 }
3248
Johan Hedberg9f616562011-04-28 11:28:54 -07003249 BT_DBG("Auto-accept of user confirmation with %ums delay",
Gustavo Padovan807deac2012-05-17 00:36:24 -03003250 hdev->auto_accept_delay);
Johan Hedberg9f616562011-04-28 11:28:54 -07003251
3252 if (hdev->auto_accept_delay > 0) {
3253 int delay = msecs_to_jiffies(hdev->auto_accept_delay);
3254 mod_timer(&conn->auto_accept_timer, jiffies + delay);
3255 goto unlock;
3256 }
3257
Johan Hedberg7a828902011-04-28 11:28:53 -07003258 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003259 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberg7a828902011-04-28 11:28:53 -07003260 goto unlock;
3261 }
3262
Johan Hedberg55bc1a32011-04-28 11:28:56 -07003263confirm:
Johan Hedberg272d90d2012-02-09 15:26:12 +02003264 mgmt_user_confirm_request(hdev, &ev->bdaddr, ACL_LINK, 0, ev->passkey,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003265 confirm_hint);
Johan Hedberg7a828902011-04-28 11:28:53 -07003266
3267unlock:
Johan Hedberga5c29682011-02-19 12:05:57 -03003268 hci_dev_unlock(hdev);
3269}
3270
Gustavo Padovan6039aa732012-05-23 04:04:18 -03003271static void hci_user_passkey_request_evt(struct hci_dev *hdev,
3272 struct sk_buff *skb)
Brian Gix1143d452011-11-23 08:28:34 -08003273{
3274 struct hci_ev_user_passkey_req *ev = (void *) skb->data;
3275
3276 BT_DBG("%s", hdev->name);
3277
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003278 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg272d90d2012-02-09 15:26:12 +02003279 mgmt_user_passkey_request(hdev, &ev->bdaddr, ACL_LINK, 0);
Brian Gix1143d452011-11-23 08:28:34 -08003280}
3281
Johan Hedberg92a25252012-09-06 18:39:26 +03003282static void hci_user_passkey_notify_evt(struct hci_dev *hdev,
3283 struct sk_buff *skb)
3284{
3285 struct hci_ev_user_passkey_notify *ev = (void *) skb->data;
3286 struct hci_conn *conn;
3287
3288 BT_DBG("%s", hdev->name);
3289
3290 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3291 if (!conn)
3292 return;
3293
3294 conn->passkey_notify = __le32_to_cpu(ev->passkey);
3295 conn->passkey_entered = 0;
3296
3297 if (test_bit(HCI_MGMT, &hdev->dev_flags))
3298 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
3299 conn->dst_type, conn->passkey_notify,
3300 conn->passkey_entered);
3301}
3302
3303static void hci_keypress_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
3304{
3305 struct hci_ev_keypress_notify *ev = (void *) skb->data;
3306 struct hci_conn *conn;
3307
3308 BT_DBG("%s", hdev->name);
3309
3310 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3311 if (!conn)
3312 return;
3313
3314 switch (ev->type) {
3315 case HCI_KEYPRESS_STARTED:
3316 conn->passkey_entered = 0;
3317 return;
3318
3319 case HCI_KEYPRESS_ENTERED:
3320 conn->passkey_entered++;
3321 break;
3322
3323 case HCI_KEYPRESS_ERASED:
3324 conn->passkey_entered--;
3325 break;
3326
3327 case HCI_KEYPRESS_CLEARED:
3328 conn->passkey_entered = 0;
3329 break;
3330
3331 case HCI_KEYPRESS_COMPLETED:
3332 return;
3333 }
3334
3335 if (test_bit(HCI_MGMT, &hdev->dev_flags))
3336 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
3337 conn->dst_type, conn->passkey_notify,
3338 conn->passkey_entered);
3339}
3340
Gustavo Padovan6039aa732012-05-23 04:04:18 -03003341static void hci_simple_pair_complete_evt(struct hci_dev *hdev,
3342 struct sk_buff *skb)
Marcel Holtmann04936842008-07-14 20:13:48 +02003343{
3344 struct hci_ev_simple_pair_complete *ev = (void *) skb->data;
3345 struct hci_conn *conn;
3346
3347 BT_DBG("%s", hdev->name);
3348
3349 hci_dev_lock(hdev);
3350
3351 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg2a611692011-02-19 12:06:00 -03003352 if (!conn)
3353 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02003354
Johan Hedberg2a611692011-02-19 12:06:00 -03003355 /* To avoid duplicate auth_failed events to user space we check
3356 * the HCI_CONN_AUTH_PEND flag which will be set if we
3357 * initiated the authentication. A traditional auth_complete
3358 * event gets always produced as initiator and is also mapped to
3359 * the mgmt_auth_failed event */
Mikel Astizfa1bd912012-08-09 09:52:29 +02003360 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) && ev->status)
Johan Hedbergbab73cb2012-02-09 16:07:29 +02003361 mgmt_auth_failed(hdev, &conn->dst, conn->type, conn->dst_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003362 ev->status);
Johan Hedberg2a611692011-02-19 12:06:00 -03003363
3364 hci_conn_put(conn);
3365
3366unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02003367 hci_dev_unlock(hdev);
3368}
3369
Gustavo Padovan6039aa732012-05-23 04:04:18 -03003370static void hci_remote_host_features_evt(struct hci_dev *hdev,
3371 struct sk_buff *skb)
Marcel Holtmann41a96212008-07-14 20:13:48 +02003372{
3373 struct hci_ev_remote_host_features *ev = (void *) skb->data;
3374 struct inquiry_entry *ie;
3375
3376 BT_DBG("%s", hdev->name);
3377
3378 hci_dev_lock(hdev);
3379
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02003380 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
3381 if (ie)
Johan Hedberg02b7cc62012-02-28 02:28:43 +02003382 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
Marcel Holtmann41a96212008-07-14 20:13:48 +02003383
3384 hci_dev_unlock(hdev);
3385}
3386
Gustavo Padovan6039aa732012-05-23 04:04:18 -03003387static void hci_remote_oob_data_request_evt(struct hci_dev *hdev,
3388 struct sk_buff *skb)
Szymon Janc2763eda2011-03-22 13:12:22 +01003389{
3390 struct hci_ev_remote_oob_data_request *ev = (void *) skb->data;
3391 struct oob_data *data;
3392
3393 BT_DBG("%s", hdev->name);
3394
3395 hci_dev_lock(hdev);
3396
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003397 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Szymon Jance1ba1f12011-04-06 13:01:59 +02003398 goto unlock;
3399
Szymon Janc2763eda2011-03-22 13:12:22 +01003400 data = hci_find_remote_oob_data(hdev, &ev->bdaddr);
3401 if (data) {
3402 struct hci_cp_remote_oob_data_reply cp;
3403
3404 bacpy(&cp.bdaddr, &ev->bdaddr);
3405 memcpy(cp.hash, data->hash, sizeof(cp.hash));
3406 memcpy(cp.randomizer, data->randomizer, sizeof(cp.randomizer));
3407
3408 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_REPLY, sizeof(cp),
Gustavo Padovan807deac2012-05-17 00:36:24 -03003409 &cp);
Szymon Janc2763eda2011-03-22 13:12:22 +01003410 } else {
3411 struct hci_cp_remote_oob_data_neg_reply cp;
3412
3413 bacpy(&cp.bdaddr, &ev->bdaddr);
3414 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_NEG_REPLY, sizeof(cp),
Gustavo Padovan807deac2012-05-17 00:36:24 -03003415 &cp);
Szymon Janc2763eda2011-03-22 13:12:22 +01003416 }
3417
Szymon Jance1ba1f12011-04-06 13:01:59 +02003418unlock:
Szymon Janc2763eda2011-03-22 13:12:22 +01003419 hci_dev_unlock(hdev);
3420}
3421
Gustavo Padovan6039aa732012-05-23 04:04:18 -03003422static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Ville Tervofcd89c02011-02-10 22:38:47 -03003423{
3424 struct hci_ev_le_conn_complete *ev = (void *) skb->data;
3425 struct hci_conn *conn;
3426
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003427 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Ville Tervofcd89c02011-02-10 22:38:47 -03003428
3429 hci_dev_lock(hdev);
3430
Andre Guedesb47a09b2012-07-27 15:10:15 -03003431 conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
Ville Tervob62f3282011-02-10 22:38:50 -03003432 if (!conn) {
3433 conn = hci_conn_add(hdev, LE_LINK, &ev->bdaddr);
3434 if (!conn) {
3435 BT_ERR("No memory for new connection");
Andre Guedes230fd162012-07-27 15:10:10 -03003436 goto unlock;
Ville Tervob62f3282011-02-10 22:38:50 -03003437 }
Andre Guedes29b79882011-05-31 14:20:54 -03003438
3439 conn->dst_type = ev->bdaddr_type;
Andre Guedesb9b343d2012-07-27 15:10:11 -03003440
3441 if (ev->role == LE_CONN_ROLE_MASTER) {
3442 conn->out = true;
3443 conn->link_mode |= HCI_LM_MASTER;
3444 }
Ville Tervob62f3282011-02-10 22:38:50 -03003445 }
Ville Tervofcd89c02011-02-10 22:38:47 -03003446
Andre Guedescd17dec2012-07-27 15:10:16 -03003447 if (ev->status) {
3448 mgmt_connect_failed(hdev, &conn->dst, conn->type,
3449 conn->dst_type, ev->status);
3450 hci_proto_connect_cfm(conn, ev->status);
3451 conn->state = BT_CLOSED;
3452 hci_conn_del(conn);
3453 goto unlock;
3454 }
3455
Johan Hedbergb644ba32012-01-17 21:48:47 +02003456 if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
3457 mgmt_device_connected(hdev, &ev->bdaddr, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003458 conn->dst_type, 0, NULL, 0, NULL);
Vinicius Costa Gomes83bc71b2011-05-06 18:41:43 -03003459
Vinicius Costa Gomes7b5c0d52011-06-09 18:50:50 -03003460 conn->sec_level = BT_SECURITY_LOW;
Ville Tervofcd89c02011-02-10 22:38:47 -03003461 conn->handle = __le16_to_cpu(ev->handle);
3462 conn->state = BT_CONNECTED;
3463
3464 hci_conn_hold_device(conn);
3465 hci_conn_add_sysfs(conn);
3466
3467 hci_proto_connect_cfm(conn, ev->status);
3468
3469unlock:
3470 hci_dev_unlock(hdev);
3471}
3472
Gustavo Padovan6039aa732012-05-23 04:04:18 -03003473static void hci_le_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)
Andre Guedes9aa04c92011-05-26 16:23:51 -03003474{
Andre Guedese95beb42011-09-26 20:48:35 -03003475 u8 num_reports = skb->data[0];
3476 void *ptr = &skb->data[1];
Andre Guedes3c9e9192012-01-10 18:20:50 -03003477 s8 rssi;
Andre Guedes9aa04c92011-05-26 16:23:51 -03003478
3479 hci_dev_lock(hdev);
3480
Andre Guedese95beb42011-09-26 20:48:35 -03003481 while (num_reports--) {
3482 struct hci_ev_le_advertising_info *ev = ptr;
Andre Guedes9aa04c92011-05-26 16:23:51 -03003483
Andre Guedes3c9e9192012-01-10 18:20:50 -03003484 rssi = ev->data[ev->length];
3485 mgmt_device_found(hdev, &ev->bdaddr, LE_LINK, ev->bdaddr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003486 NULL, rssi, 0, 1, ev->data, ev->length);
Andre Guedes3c9e9192012-01-10 18:20:50 -03003487
Andre Guedese95beb42011-09-26 20:48:35 -03003488 ptr += sizeof(*ev) + ev->length + 1;
Andre Guedes9aa04c92011-05-26 16:23:51 -03003489 }
3490
3491 hci_dev_unlock(hdev);
3492}
3493
Gustavo Padovan6039aa732012-05-23 04:04:18 -03003494static void hci_le_ltk_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03003495{
3496 struct hci_ev_le_ltk_req *ev = (void *) skb->data;
3497 struct hci_cp_le_ltk_reply cp;
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03003498 struct hci_cp_le_ltk_neg_reply neg;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03003499 struct hci_conn *conn;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03003500 struct smp_ltk *ltk;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03003501
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003502 BT_DBG("%s handle 0x%4.4x", hdev->name, __le16_to_cpu(ev->handle));
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03003503
3504 hci_dev_lock(hdev);
3505
3506 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03003507 if (conn == NULL)
3508 goto not_found;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03003509
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03003510 ltk = hci_find_ltk(hdev, ev->ediv, ev->random);
3511 if (ltk == NULL)
3512 goto not_found;
3513
3514 memcpy(cp.ltk, ltk->val, sizeof(ltk->val));
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03003515 cp.handle = cpu_to_le16(conn->handle);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03003516
3517 if (ltk->authenticated)
3518 conn->sec_level = BT_SECURITY_HIGH;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03003519
3520 hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
3521
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03003522 if (ltk->type & HCI_SMP_STK) {
3523 list_del(&ltk->list);
3524 kfree(ltk);
3525 }
3526
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03003527 hci_dev_unlock(hdev);
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03003528
3529 return;
3530
3531not_found:
3532 neg.handle = ev->handle;
3533 hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(neg), &neg);
3534 hci_dev_unlock(hdev);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03003535}
3536
Gustavo Padovan6039aa732012-05-23 04:04:18 -03003537static void hci_le_meta_evt(struct hci_dev *hdev, struct sk_buff *skb)
Ville Tervofcd89c02011-02-10 22:38:47 -03003538{
3539 struct hci_ev_le_meta *le_ev = (void *) skb->data;
3540
3541 skb_pull(skb, sizeof(*le_ev));
3542
3543 switch (le_ev->subevent) {
3544 case HCI_EV_LE_CONN_COMPLETE:
3545 hci_le_conn_complete_evt(hdev, skb);
3546 break;
3547
Andre Guedes9aa04c92011-05-26 16:23:51 -03003548 case HCI_EV_LE_ADVERTISING_REPORT:
3549 hci_le_adv_report_evt(hdev, skb);
3550 break;
3551
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03003552 case HCI_EV_LE_LTK_REQ:
3553 hci_le_ltk_request_evt(hdev, skb);
3554 break;
3555
Ville Tervofcd89c02011-02-10 22:38:47 -03003556 default:
3557 break;
3558 }
3559}
3560
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
3562{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003563 struct hci_event_hdr *hdr = (void *) skb->data;
3564 __u8 event = hdr->evt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
3566 skb_pull(skb, HCI_EVENT_HDR_SIZE);
3567
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003568 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 case HCI_EV_INQUIRY_COMPLETE:
3570 hci_inquiry_complete_evt(hdev, skb);
3571 break;
3572
3573 case HCI_EV_INQUIRY_RESULT:
3574 hci_inquiry_result_evt(hdev, skb);
3575 break;
3576
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003577 case HCI_EV_CONN_COMPLETE:
3578 hci_conn_complete_evt(hdev, skb);
Marcel Holtmann21d9e302005-09-13 01:32:25 +02003579 break;
3580
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 case HCI_EV_CONN_REQUEST:
3582 hci_conn_request_evt(hdev, skb);
3583 break;
3584
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 case HCI_EV_DISCONN_COMPLETE:
3586 hci_disconn_complete_evt(hdev, skb);
3587 break;
3588
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 case HCI_EV_AUTH_COMPLETE:
3590 hci_auth_complete_evt(hdev, skb);
3591 break;
3592
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003593 case HCI_EV_REMOTE_NAME:
3594 hci_remote_name_evt(hdev, skb);
3595 break;
3596
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 case HCI_EV_ENCRYPT_CHANGE:
3598 hci_encrypt_change_evt(hdev, skb);
3599 break;
3600
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003601 case HCI_EV_CHANGE_LINK_KEY_COMPLETE:
3602 hci_change_link_key_complete_evt(hdev, skb);
3603 break;
3604
3605 case HCI_EV_REMOTE_FEATURES:
3606 hci_remote_features_evt(hdev, skb);
3607 break;
3608
3609 case HCI_EV_REMOTE_VERSION:
3610 hci_remote_version_evt(hdev, skb);
3611 break;
3612
3613 case HCI_EV_QOS_SETUP_COMPLETE:
3614 hci_qos_setup_complete_evt(hdev, skb);
3615 break;
3616
3617 case HCI_EV_CMD_COMPLETE:
3618 hci_cmd_complete_evt(hdev, skb);
3619 break;
3620
3621 case HCI_EV_CMD_STATUS:
3622 hci_cmd_status_evt(hdev, skb);
3623 break;
3624
3625 case HCI_EV_ROLE_CHANGE:
3626 hci_role_change_evt(hdev, skb);
3627 break;
3628
3629 case HCI_EV_NUM_COMP_PKTS:
3630 hci_num_comp_pkts_evt(hdev, skb);
3631 break;
3632
3633 case HCI_EV_MODE_CHANGE:
3634 hci_mode_change_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 break;
3636
3637 case HCI_EV_PIN_CODE_REQ:
3638 hci_pin_code_request_evt(hdev, skb);
3639 break;
3640
3641 case HCI_EV_LINK_KEY_REQ:
3642 hci_link_key_request_evt(hdev, skb);
3643 break;
3644
3645 case HCI_EV_LINK_KEY_NOTIFY:
3646 hci_link_key_notify_evt(hdev, skb);
3647 break;
3648
3649 case HCI_EV_CLOCK_OFFSET:
3650 hci_clock_offset_evt(hdev, skb);
3651 break;
3652
Marcel Holtmanna8746412008-07-14 20:13:46 +02003653 case HCI_EV_PKT_TYPE_CHANGE:
3654 hci_pkt_type_change_evt(hdev, skb);
3655 break;
3656
Marcel Holtmann85a1e932005-08-09 20:28:02 -07003657 case HCI_EV_PSCAN_REP_MODE:
3658 hci_pscan_rep_mode_evt(hdev, skb);
3659 break;
3660
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003661 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
3662 hci_inquiry_result_with_rssi_evt(hdev, skb);
3663 break;
3664
3665 case HCI_EV_REMOTE_EXT_FEATURES:
3666 hci_remote_ext_features_evt(hdev, skb);
3667 break;
3668
3669 case HCI_EV_SYNC_CONN_COMPLETE:
3670 hci_sync_conn_complete_evt(hdev, skb);
3671 break;
3672
3673 case HCI_EV_SYNC_CONN_CHANGED:
3674 hci_sync_conn_changed_evt(hdev, skb);
3675 break;
3676
Marcel Holtmann04837f62006-07-03 10:02:33 +02003677 case HCI_EV_SNIFF_SUBRATE:
3678 hci_sniff_subrate_evt(hdev, skb);
3679 break;
3680
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003681 case HCI_EV_EXTENDED_INQUIRY_RESULT:
3682 hci_extended_inquiry_result_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 break;
3684
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003685 case HCI_EV_KEY_REFRESH_COMPLETE:
3686 hci_key_refresh_complete_evt(hdev, skb);
3687 break;
3688
Marcel Holtmann04936842008-07-14 20:13:48 +02003689 case HCI_EV_IO_CAPA_REQUEST:
3690 hci_io_capa_request_evt(hdev, skb);
3691 break;
3692
Johan Hedberg03b555e2011-01-04 15:40:05 +02003693 case HCI_EV_IO_CAPA_REPLY:
3694 hci_io_capa_reply_evt(hdev, skb);
3695 break;
3696
Johan Hedberga5c29682011-02-19 12:05:57 -03003697 case HCI_EV_USER_CONFIRM_REQUEST:
3698 hci_user_confirm_request_evt(hdev, skb);
3699 break;
3700
Brian Gix1143d452011-11-23 08:28:34 -08003701 case HCI_EV_USER_PASSKEY_REQUEST:
3702 hci_user_passkey_request_evt(hdev, skb);
3703 break;
3704
Johan Hedberg92a25252012-09-06 18:39:26 +03003705 case HCI_EV_USER_PASSKEY_NOTIFY:
3706 hci_user_passkey_notify_evt(hdev, skb);
3707 break;
3708
3709 case HCI_EV_KEYPRESS_NOTIFY:
3710 hci_keypress_notify_evt(hdev, skb);
3711 break;
3712
Marcel Holtmann04936842008-07-14 20:13:48 +02003713 case HCI_EV_SIMPLE_PAIR_COMPLETE:
3714 hci_simple_pair_complete_evt(hdev, skb);
3715 break;
3716
Marcel Holtmann41a96212008-07-14 20:13:48 +02003717 case HCI_EV_REMOTE_HOST_FEATURES:
3718 hci_remote_host_features_evt(hdev, skb);
3719 break;
3720
Ville Tervofcd89c02011-02-10 22:38:47 -03003721 case HCI_EV_LE_META:
3722 hci_le_meta_evt(hdev, skb);
3723 break;
3724
Szymon Janc2763eda2011-03-22 13:12:22 +01003725 case HCI_EV_REMOTE_OOB_DATA_REQUEST:
3726 hci_remote_oob_data_request_evt(hdev, skb);
3727 break;
3728
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003729 case HCI_EV_NUM_COMP_BLOCKS:
3730 hci_num_comp_blocks_evt(hdev, skb);
3731 break;
3732
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003733 default:
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003734 BT_DBG("%s event 0x%2.2x", hdev->name, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 break;
3736 }
3737
3738 kfree_skb(skb);
3739 hdev->stat.evt_rx++;
3740}