Bluetooth: Update stop_discovery to use HCI request
[deliverable/linux.git] / net / bluetooth / hci_event.c
CommitLineData
8e87d142 1/*
1da177e4 2 BlueZ - Bluetooth protocol stack for Linux
2d0a0346 3 Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
1da177e4
LT
4
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
8e87d142
YH
15 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
1da177e4
LT
18 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
8e87d142
YH
20 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
1da177e4
LT
22 SOFTWARE IS DISCLAIMED.
23*/
24
25/* Bluetooth HCI event handling. */
26
1da177e4
LT
27#include <asm/unaligned.h>
28
29#include <net/bluetooth/bluetooth.h>
30#include <net/bluetooth/hci_core.h>
f0d6a0ea 31#include <net/bluetooth/mgmt.h>
8e2a0d92 32#include <net/bluetooth/a2mp.h>
903e4541 33#include <net/bluetooth/amp.h>
1da177e4 34
1da177e4
LT
35/* Handle HCI Event packets */
36
a9de9248 37static void hci_cc_inquiry_cancel(struct hci_dev *hdev, struct sk_buff *skb)
1da177e4 38{
a9de9248 39 __u8 status = *((__u8 *) skb->data);
1da177e4 40
9f1db00c 41 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1da177e4 42
e6d465cb
AG
43 if (status) {
44 hci_dev_lock(hdev);
45 mgmt_stop_discovery_failed(hdev, status);
46 hci_dev_unlock(hdev);
a9de9248 47 return;
e6d465cb 48 }
1da177e4 49
89352e7d 50 clear_bit(HCI_INQUIRY, &hdev->flags);
3e13fa1e
AG
51 smp_mb__after_clear_bit(); /* wake_up_bit advises about this barrier */
52 wake_up_bit(&hdev->flags, HCI_INQUIRY);
89352e7d 53
56e5cb86 54 hci_dev_lock(hdev);
ff9ef578 55 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
56e5cb86 56 hci_dev_unlock(hdev);
6bd57416 57
a9de9248
MH
58 hci_conn_check_pending(hdev);
59}
6bd57416 60
4d93483b
AG
61static void hci_cc_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
62{
63 __u8 status = *((__u8 *) skb->data);
64
9f1db00c 65 BT_DBG("%s status 0x%2.2x", hdev->name, status);
ae854a70
AG
66
67 if (status)
68 return;
69
70 set_bit(HCI_PERIODIC_INQ, &hdev->dev_flags);
4d93483b
AG
71}
72
a9de9248
MH
73static void hci_cc_exit_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
74{
75 __u8 status = *((__u8 *) skb->data);
6bd57416 76
9f1db00c 77 BT_DBG("%s status 0x%2.2x", hdev->name, status);
6bd57416 78
a9de9248
MH
79 if (status)
80 return;
1da177e4 81
ae854a70
AG
82 clear_bit(HCI_PERIODIC_INQ, &hdev->dev_flags);
83
a9de9248
MH
84 hci_conn_check_pending(hdev);
85}
86
807deac2
GP
87static void hci_cc_remote_name_req_cancel(struct hci_dev *hdev,
88 struct sk_buff *skb)
a9de9248
MH
89{
90 BT_DBG("%s", hdev->name);
91}
92
93static void hci_cc_role_discovery(struct hci_dev *hdev, struct sk_buff *skb)
94{
95 struct hci_rp_role_discovery *rp = (void *) skb->data;
96 struct hci_conn *conn;
97
9f1db00c 98 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
a9de9248
MH
99
100 if (rp->status)
101 return;
102
103 hci_dev_lock(hdev);
104
105 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
106 if (conn) {
107 if (rp->role)
108 conn->link_mode &= ~HCI_LM_MASTER;
109 else
110 conn->link_mode |= HCI_LM_MASTER;
1da177e4 111 }
a9de9248
MH
112
113 hci_dev_unlock(hdev);
1da177e4
LT
114}
115
e4e8e37c
MH
116static void hci_cc_read_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
117{
118 struct hci_rp_read_link_policy *rp = (void *) skb->data;
119 struct hci_conn *conn;
120
9f1db00c 121 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
e4e8e37c
MH
122
123 if (rp->status)
124 return;
125
126 hci_dev_lock(hdev);
127
128 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
129 if (conn)
130 conn->link_policy = __le16_to_cpu(rp->policy);
131
132 hci_dev_unlock(hdev);
133}
134
a9de9248 135static void hci_cc_write_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
1da177e4 136{
a9de9248 137 struct hci_rp_write_link_policy *rp = (void *) skb->data;
1da177e4 138 struct hci_conn *conn;
04837f64 139 void *sent;
1da177e4 140
9f1db00c 141 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1da177e4 142
a9de9248
MH
143 if (rp->status)
144 return;
1da177e4 145
a9de9248
MH
146 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LINK_POLICY);
147 if (!sent)
148 return;
1da177e4 149
a9de9248 150 hci_dev_lock(hdev);
1da177e4 151
a9de9248 152 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
e4e8e37c 153 if (conn)
83985319 154 conn->link_policy = get_unaligned_le16(sent + 2);
1da177e4 155
a9de9248
MH
156 hci_dev_unlock(hdev);
157}
1da177e4 158
807deac2
GP
159static void hci_cc_read_def_link_policy(struct hci_dev *hdev,
160 struct sk_buff *skb)
e4e8e37c
MH
161{
162 struct hci_rp_read_def_link_policy *rp = (void *) skb->data;
163
9f1db00c 164 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
e4e8e37c
MH
165
166 if (rp->status)
167 return;
168
169 hdev->link_policy = __le16_to_cpu(rp->policy);
170}
171
807deac2
GP
172static void hci_cc_write_def_link_policy(struct hci_dev *hdev,
173 struct sk_buff *skb)
e4e8e37c
MH
174{
175 __u8 status = *((__u8 *) skb->data);
176 void *sent;
177
9f1db00c 178 BT_DBG("%s status 0x%2.2x", hdev->name, status);
e4e8e37c
MH
179
180 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_LINK_POLICY);
181 if (!sent)
182 return;
183
184 if (!status)
185 hdev->link_policy = get_unaligned_le16(sent);
e4e8e37c
MH
186}
187
a9de9248
MH
188static void hci_cc_reset(struct hci_dev *hdev, struct sk_buff *skb)
189{
190 __u8 status = *((__u8 *) skb->data);
04837f64 191
9f1db00c 192 BT_DBG("%s status 0x%2.2x", hdev->name, status);
04837f64 193
10572132
GP
194 clear_bit(HCI_RESET, &hdev->flags);
195
a297e97c 196 /* Reset all non-persistent flags */
2cc6fb00 197 hdev->dev_flags &= ~HCI_PERSISTENT_MASK;
69775ff6
AG
198
199 hdev->discovery.state = DISCOVERY_STOPPED;
bbaf444a
JH
200 hdev->inq_tx_power = HCI_TX_POWER_INVALID;
201 hdev->adv_tx_power = HCI_TX_POWER_INVALID;
3f0f524b
JH
202
203 memset(hdev->adv_data, 0, sizeof(hdev->adv_data));
204 hdev->adv_data_len = 0;
a9de9248 205}
04837f64 206
a9de9248
MH
207static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb)
208{
209 __u8 status = *((__u8 *) skb->data);
210 void *sent;
04837f64 211
9f1db00c 212 BT_DBG("%s status 0x%2.2x", hdev->name, status);
04837f64 213
a9de9248
MH
214 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LOCAL_NAME);
215 if (!sent)
216 return;
04837f64 217
56e5cb86
JH
218 hci_dev_lock(hdev);
219
f51d5b24
JH
220 if (test_bit(HCI_MGMT, &hdev->dev_flags))
221 mgmt_set_local_name_complete(hdev, sent, status);
28cc7bde
JH
222 else if (!status)
223 memcpy(hdev->dev_name, sent, HCI_MAX_NAME_LENGTH);
f51d5b24 224
56e5cb86 225 hci_dev_unlock(hdev);
a9de9248
MH
226}
227
228static void hci_cc_read_local_name(struct hci_dev *hdev, struct sk_buff *skb)
229{
230 struct hci_rp_read_local_name *rp = (void *) skb->data;
231
9f1db00c 232 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
a9de9248
MH
233
234 if (rp->status)
235 return;
236
db99b5fc
JH
237 if (test_bit(HCI_SETUP, &hdev->dev_flags))
238 memcpy(hdev->dev_name, rp->name, HCI_MAX_NAME_LENGTH);
a9de9248
MH
239}
240
241static void hci_cc_write_auth_enable(struct hci_dev *hdev, struct sk_buff *skb)
242{
243 __u8 status = *((__u8 *) skb->data);
244 void *sent;
245
9f1db00c 246 BT_DBG("%s status 0x%2.2x", hdev->name, status);
a9de9248
MH
247
248 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_ENABLE);
249 if (!sent)
250 return;
251
252 if (!status) {
253 __u8 param = *((__u8 *) sent);
254
255 if (param == AUTH_ENABLED)
256 set_bit(HCI_AUTH, &hdev->flags);
257 else
258 clear_bit(HCI_AUTH, &hdev->flags);
1da177e4 259 }
a9de9248 260
33ef95ed
JH
261 if (test_bit(HCI_MGMT, &hdev->dev_flags))
262 mgmt_auth_enable_complete(hdev, status);
1da177e4
LT
263}
264
a9de9248 265static void hci_cc_write_encrypt_mode(struct hci_dev *hdev, struct sk_buff *skb)
1da177e4 266{
a9de9248 267 __u8 status = *((__u8 *) skb->data);
1da177e4
LT
268 void *sent;
269
9f1db00c 270 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1da177e4 271
a9de9248
MH
272 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_ENCRYPT_MODE);
273 if (!sent)
274 return;
1da177e4 275
a9de9248
MH
276 if (!status) {
277 __u8 param = *((__u8 *) sent);
278
279 if (param)
280 set_bit(HCI_ENCRYPT, &hdev->flags);
281 else
282 clear_bit(HCI_ENCRYPT, &hdev->flags);
283 }
a9de9248 284}
1da177e4 285
a9de9248
MH
286static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
287{
36f7fc7e
JH
288 __u8 param, status = *((__u8 *) skb->data);
289 int old_pscan, old_iscan;
a9de9248 290 void *sent;
1da177e4 291
9f1db00c 292 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1da177e4 293
a9de9248
MH
294 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SCAN_ENABLE);
295 if (!sent)
296 return;
1da177e4 297
36f7fc7e
JH
298 param = *((__u8 *) sent);
299
56e5cb86
JH
300 hci_dev_lock(hdev);
301
fa1bd918 302 if (status) {
744cf19e 303 mgmt_write_scan_failed(hdev, param, status);
2d7cee58
JH
304 hdev->discov_timeout = 0;
305 goto done;
306 }
307
36f7fc7e
JH
308 old_pscan = test_and_clear_bit(HCI_PSCAN, &hdev->flags);
309 old_iscan = test_and_clear_bit(HCI_ISCAN, &hdev->flags);
310
311 if (param & SCAN_INQUIRY) {
312 set_bit(HCI_ISCAN, &hdev->flags);
313 if (!old_iscan)
744cf19e 314 mgmt_discoverable(hdev, 1);
16ab91ab
JH
315 if (hdev->discov_timeout > 0) {
316 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
317 queue_delayed_work(hdev->workqueue, &hdev->discov_off,
807deac2 318 to);
16ab91ab 319 }
36f7fc7e 320 } else if (old_iscan)
744cf19e 321 mgmt_discoverable(hdev, 0);
36f7fc7e
JH
322
323 if (param & SCAN_PAGE) {
324 set_bit(HCI_PSCAN, &hdev->flags);
325 if (!old_pscan)
744cf19e 326 mgmt_connectable(hdev, 1);
36f7fc7e 327 } else if (old_pscan)
744cf19e 328 mgmt_connectable(hdev, 0);
1da177e4 329
36f7fc7e 330done:
56e5cb86 331 hci_dev_unlock(hdev);
a9de9248 332}
1da177e4 333
a9de9248
MH
334static void hci_cc_read_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
335{
336 struct hci_rp_read_class_of_dev *rp = (void *) skb->data;
1da177e4 337
9f1db00c 338 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1da177e4 339
a9de9248
MH
340 if (rp->status)
341 return;
1da177e4 342
a9de9248 343 memcpy(hdev->dev_class, rp->dev_class, 3);
1da177e4 344
a9de9248 345 BT_DBG("%s class 0x%.2x%.2x%.2x", hdev->name,
807deac2 346 hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
a9de9248 347}
1da177e4 348
a9de9248
MH
349static void hci_cc_write_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
350{
351 __u8 status = *((__u8 *) skb->data);
352 void *sent;
1da177e4 353
9f1db00c 354 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1da177e4 355
a9de9248
MH
356 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_CLASS_OF_DEV);
357 if (!sent)
358 return;
1da177e4 359
7f9a903c
MH
360 hci_dev_lock(hdev);
361
362 if (status == 0)
363 memcpy(hdev->dev_class, sent, 3);
364
365 if (test_bit(HCI_MGMT, &hdev->dev_flags))
366 mgmt_set_class_of_dev_complete(hdev, sent, status);
367
368 hci_dev_unlock(hdev);
a9de9248 369}
1da177e4 370
a9de9248
MH
371static void hci_cc_read_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
372{
373 struct hci_rp_read_voice_setting *rp = (void *) skb->data;
374 __u16 setting;
375
9f1db00c 376 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
a9de9248
MH
377
378 if (rp->status)
379 return;
380
381 setting = __le16_to_cpu(rp->voice_setting);
382
f383f275 383 if (hdev->voice_setting == setting)
a9de9248
MH
384 return;
385
386 hdev->voice_setting = setting;
387
9f1db00c 388 BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
a9de9248 389
3c54711c 390 if (hdev->notify)
a9de9248 391 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
a9de9248
MH
392}
393
8fc9ced3
GP
394static void hci_cc_write_voice_setting(struct hci_dev *hdev,
395 struct sk_buff *skb)
a9de9248
MH
396{
397 __u8 status = *((__u8 *) skb->data);
f383f275 398 __u16 setting;
a9de9248
MH
399 void *sent;
400
9f1db00c 401 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1da177e4 402
f383f275
MH
403 if (status)
404 return;
405
a9de9248
MH
406 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_VOICE_SETTING);
407 if (!sent)
408 return;
1da177e4 409
f383f275 410 setting = get_unaligned_le16(sent);
1da177e4 411
f383f275
MH
412 if (hdev->voice_setting == setting)
413 return;
414
415 hdev->voice_setting = setting;
1da177e4 416
9f1db00c 417 BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
1da177e4 418
3c54711c 419 if (hdev->notify)
f383f275 420 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
1da177e4
LT
421}
422
333140b5
MH
423static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
424{
425 __u8 status = *((__u8 *) skb->data);
5ed8eb2f 426 struct hci_cp_write_ssp_mode *sent;
333140b5 427
9f1db00c 428 BT_DBG("%s status 0x%2.2x", hdev->name, status);
333140b5 429
333140b5
MH
430 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_MODE);
431 if (!sent)
432 return;
433
5ed8eb2f
JH
434 if (!status) {
435 if (sent->mode)
cad718ed 436 hdev->features[1][0] |= LMP_HOST_SSP;
5ed8eb2f 437 else
cad718ed 438 hdev->features[1][0] &= ~LMP_HOST_SSP;
5ed8eb2f
JH
439 }
440
ed2c4ee3 441 if (test_bit(HCI_MGMT, &hdev->dev_flags))
5ed8eb2f 442 mgmt_ssp_enable_complete(hdev, sent->mode, status);
c0ecddc2 443 else if (!status) {
5ed8eb2f 444 if (sent->mode)
c0ecddc2
JH
445 set_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
446 else
447 clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
448 }
333140b5
MH
449}
450
a9de9248
MH
451static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
452{
453 struct hci_rp_read_local_version *rp = (void *) skb->data;
1143e5a6 454
9f1db00c 455 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1143e5a6 456
a9de9248 457 if (rp->status)
42c6b129 458 return;
1143e5a6 459
a9de9248 460 hdev->hci_ver = rp->hci_ver;
e4e8e37c 461 hdev->hci_rev = __le16_to_cpu(rp->hci_rev);
d5859e22 462 hdev->lmp_ver = rp->lmp_ver;
e4e8e37c 463 hdev->manufacturer = __le16_to_cpu(rp->manufacturer);
d5859e22 464 hdev->lmp_subver = __le16_to_cpu(rp->lmp_subver);
1143e5a6 465
9f1db00c 466 BT_DBG("%s manufacturer 0x%4.4x hci ver %d:%d", hdev->name,
807deac2 467 hdev->manufacturer, hdev->hci_ver, hdev->hci_rev);
d5859e22
JH
468}
469
8fc9ced3
GP
470static void hci_cc_read_local_commands(struct hci_dev *hdev,
471 struct sk_buff *skb)
a9de9248
MH
472{
473 struct hci_rp_read_local_commands *rp = (void *) skb->data;
1da177e4 474
9f1db00c 475 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1da177e4 476
2177bab5
JH
477 if (!rp->status)
478 memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
a9de9248 479}
1da177e4 480
8fc9ced3
GP
481static void hci_cc_read_local_features(struct hci_dev *hdev,
482 struct sk_buff *skb)
a9de9248
MH
483{
484 struct hci_rp_read_local_features *rp = (void *) skb->data;
5b7f9909 485
9f1db00c 486 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1da177e4 487
a9de9248
MH
488 if (rp->status)
489 return;
5b7f9909 490
a9de9248 491 memcpy(hdev->features, rp->features, 8);
5b7f9909 492
a9de9248
MH
493 /* Adjust default settings according to features
494 * supported by device. */
1da177e4 495
cad718ed 496 if (hdev->features[0][0] & LMP_3SLOT)
a9de9248 497 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
1da177e4 498
cad718ed 499 if (hdev->features[0][0] & LMP_5SLOT)
a9de9248 500 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
1da177e4 501
cad718ed 502 if (hdev->features[0][1] & LMP_HV2) {
a9de9248
MH
503 hdev->pkt_type |= (HCI_HV2);
504 hdev->esco_type |= (ESCO_HV2);
505 }
1da177e4 506
cad718ed 507 if (hdev->features[0][1] & LMP_HV3) {
a9de9248
MH
508 hdev->pkt_type |= (HCI_HV3);
509 hdev->esco_type |= (ESCO_HV3);
510 }
1da177e4 511
45db810f 512 if (lmp_esco_capable(hdev))
a9de9248 513 hdev->esco_type |= (ESCO_EV3);
da1f5198 514
cad718ed 515 if (hdev->features[0][4] & LMP_EV4)
a9de9248 516 hdev->esco_type |= (ESCO_EV4);
da1f5198 517
cad718ed 518 if (hdev->features[0][4] & LMP_EV5)
a9de9248 519 hdev->esco_type |= (ESCO_EV5);
1da177e4 520
cad718ed 521 if (hdev->features[0][5] & LMP_EDR_ESCO_2M)
efc7688b
MH
522 hdev->esco_type |= (ESCO_2EV3);
523
cad718ed 524 if (hdev->features[0][5] & LMP_EDR_ESCO_3M)
efc7688b
MH
525 hdev->esco_type |= (ESCO_3EV3);
526
cad718ed 527 if (hdev->features[0][5] & LMP_EDR_3S_ESCO)
efc7688b
MH
528 hdev->esco_type |= (ESCO_2EV5 | ESCO_3EV5);
529
a9de9248 530 BT_DBG("%s features 0x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x", hdev->name,
cad718ed
JH
531 hdev->features[0][0], hdev->features[0][1],
532 hdev->features[0][2], hdev->features[0][3],
533 hdev->features[0][4], hdev->features[0][5],
534 hdev->features[0][6], hdev->features[0][7]);
a9de9248 535}
1da177e4 536
971e3a4b 537static void hci_cc_read_local_ext_features(struct hci_dev *hdev,
807deac2 538 struct sk_buff *skb)
971e3a4b
AG
539{
540 struct hci_rp_read_local_ext_features *rp = (void *) skb->data;
541
9f1db00c 542 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
971e3a4b
AG
543
544 if (rp->status)
42c6b129 545 return;
971e3a4b 546
d2c5d77f
JH
547 hdev->max_page = rp->max_page;
548
cad718ed
JH
549 if (rp->page < HCI_MAX_PAGES)
550 memcpy(hdev->features[rp->page], rp->features, 8);
971e3a4b
AG
551}
552
1e89cffb 553static void hci_cc_read_flow_control_mode(struct hci_dev *hdev,
807deac2 554 struct sk_buff *skb)
1e89cffb
AE
555{
556 struct hci_rp_read_flow_control_mode *rp = (void *) skb->data;
557
9f1db00c 558 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1e89cffb 559
42c6b129
JH
560 if (!rp->status)
561 hdev->flow_ctl_mode = rp->mode;
1e89cffb
AE
562}
563
a9de9248
MH
564static void hci_cc_read_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
565{
566 struct hci_rp_read_buffer_size *rp = (void *) skb->data;
1da177e4 567
9f1db00c 568 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1da177e4 569
a9de9248
MH
570 if (rp->status)
571 return;
1da177e4 572
a9de9248
MH
573 hdev->acl_mtu = __le16_to_cpu(rp->acl_mtu);
574 hdev->sco_mtu = rp->sco_mtu;
575 hdev->acl_pkts = __le16_to_cpu(rp->acl_max_pkt);
576 hdev->sco_pkts = __le16_to_cpu(rp->sco_max_pkt);
577
578 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
579 hdev->sco_mtu = 64;
580 hdev->sco_pkts = 8;
1da177e4 581 }
a9de9248
MH
582
583 hdev->acl_cnt = hdev->acl_pkts;
584 hdev->sco_cnt = hdev->sco_pkts;
585
807deac2
GP
586 BT_DBG("%s acl mtu %d:%d sco mtu %d:%d", hdev->name, hdev->acl_mtu,
587 hdev->acl_pkts, hdev->sco_mtu, hdev->sco_pkts);
a9de9248
MH
588}
589
590static void hci_cc_read_bd_addr(struct hci_dev *hdev, struct sk_buff *skb)
591{
592 struct hci_rp_read_bd_addr *rp = (void *) skb->data;
593
9f1db00c 594 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
a9de9248
MH
595
596 if (!rp->status)
597 bacpy(&hdev->bdaddr, &rp->bdaddr);
23bb5763
JH
598}
599
f332ec66
JH
600static void hci_cc_read_page_scan_activity(struct hci_dev *hdev,
601 struct sk_buff *skb)
602{
603 struct hci_rp_read_page_scan_activity *rp = (void *) skb->data;
604
605 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
606
607 if (test_bit(HCI_INIT, &hdev->flags) && !rp->status) {
608 hdev->page_scan_interval = __le16_to_cpu(rp->interval);
609 hdev->page_scan_window = __le16_to_cpu(rp->window);
610 }
611}
612
4a3ee763
JH
613static void hci_cc_write_page_scan_activity(struct hci_dev *hdev,
614 struct sk_buff *skb)
615{
616 u8 status = *((u8 *) skb->data);
617 struct hci_cp_write_page_scan_activity *sent;
618
619 BT_DBG("%s status 0x%2.2x", hdev->name, status);
620
621 if (status)
622 return;
623
624 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY);
625 if (!sent)
626 return;
627
628 hdev->page_scan_interval = __le16_to_cpu(sent->interval);
629 hdev->page_scan_window = __le16_to_cpu(sent->window);
630}
631
f332ec66
JH
632static void hci_cc_read_page_scan_type(struct hci_dev *hdev,
633 struct sk_buff *skb)
634{
635 struct hci_rp_read_page_scan_type *rp = (void *) skb->data;
636
637 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
638
639 if (test_bit(HCI_INIT, &hdev->flags) && !rp->status)
640 hdev->page_scan_type = rp->type;
641}
642
4a3ee763
JH
643static void hci_cc_write_page_scan_type(struct hci_dev *hdev,
644 struct sk_buff *skb)
645{
646 u8 status = *((u8 *) skb->data);
647 u8 *type;
648
649 BT_DBG("%s status 0x%2.2x", hdev->name, status);
650
651 if (status)
652 return;
653
654 type = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_TYPE);
655 if (type)
656 hdev->page_scan_type = *type;
657}
658
350ee4cf 659static void hci_cc_read_data_block_size(struct hci_dev *hdev,
807deac2 660 struct sk_buff *skb)
350ee4cf
AE
661{
662 struct hci_rp_read_data_block_size *rp = (void *) skb->data;
663
9f1db00c 664 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
350ee4cf
AE
665
666 if (rp->status)
667 return;
668
669 hdev->block_mtu = __le16_to_cpu(rp->max_acl_len);
670 hdev->block_len = __le16_to_cpu(rp->block_len);
671 hdev->num_blocks = __le16_to_cpu(rp->num_blocks);
672
673 hdev->block_cnt = hdev->num_blocks;
674
675 BT_DBG("%s blk mtu %d cnt %d len %d", hdev->name, hdev->block_mtu,
807deac2 676 hdev->block_cnt, hdev->block_len);
350ee4cf
AE
677}
678
928abaa7 679static void hci_cc_read_local_amp_info(struct hci_dev *hdev,
807deac2 680 struct sk_buff *skb)
928abaa7
AE
681{
682 struct hci_rp_read_local_amp_info *rp = (void *) skb->data;
683
9f1db00c 684 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
928abaa7
AE
685
686 if (rp->status)
8e2a0d92 687 goto a2mp_rsp;
928abaa7
AE
688
689 hdev->amp_status = rp->amp_status;
690 hdev->amp_total_bw = __le32_to_cpu(rp->total_bw);
691 hdev->amp_max_bw = __le32_to_cpu(rp->max_bw);
692 hdev->amp_min_latency = __le32_to_cpu(rp->min_latency);
693 hdev->amp_max_pdu = __le32_to_cpu(rp->max_pdu);
694 hdev->amp_type = rp->amp_type;
695 hdev->amp_pal_cap = __le16_to_cpu(rp->pal_cap);
696 hdev->amp_assoc_size = __le16_to_cpu(rp->max_assoc_size);
697 hdev->amp_be_flush_to = __le32_to_cpu(rp->be_flush_to);
698 hdev->amp_max_flush_to = __le32_to_cpu(rp->max_flush_to);
699
8e2a0d92
AE
700a2mp_rsp:
701 a2mp_send_getinfo_rsp(hdev);
928abaa7
AE
702}
703
903e4541
AE
704static void hci_cc_read_local_amp_assoc(struct hci_dev *hdev,
705 struct sk_buff *skb)
706{
707 struct hci_rp_read_local_amp_assoc *rp = (void *) skb->data;
708 struct amp_assoc *assoc = &hdev->loc_assoc;
709 size_t rem_len, frag_len;
710
711 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
712
713 if (rp->status)
714 goto a2mp_rsp;
715
716 frag_len = skb->len - sizeof(*rp);
717 rem_len = __le16_to_cpu(rp->rem_len);
718
719 if (rem_len > frag_len) {
2e430be3 720 BT_DBG("frag_len %zu rem_len %zu", frag_len, rem_len);
903e4541
AE
721
722 memcpy(assoc->data + assoc->offset, rp->frag, frag_len);
723 assoc->offset += frag_len;
724
725 /* Read other fragments */
726 amp_read_loc_assoc_frag(hdev, rp->phy_handle);
727
728 return;
729 }
730
731 memcpy(assoc->data + assoc->offset, rp->frag, rem_len);
732 assoc->len = assoc->offset + rem_len;
733 assoc->offset = 0;
734
735a2mp_rsp:
736 /* Send A2MP Rsp when all fragments are received */
737 a2mp_send_getampassoc_rsp(hdev, rp->status);
9495b2ee 738 a2mp_send_create_phy_link_req(hdev, rp->status);
903e4541
AE
739}
740
d5859e22 741static void hci_cc_read_inq_rsp_tx_power(struct hci_dev *hdev,
807deac2 742 struct sk_buff *skb)
d5859e22 743{
91c4e9b1 744 struct hci_rp_read_inq_rsp_tx_power *rp = (void *) skb->data;
d5859e22 745
9f1db00c 746 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
91c4e9b1
MH
747
748 if (!rp->status)
749 hdev->inq_tx_power = rp->tx_power;
d5859e22
JH
750}
751
980e1a53
JH
752static void hci_cc_pin_code_reply(struct hci_dev *hdev, struct sk_buff *skb)
753{
754 struct hci_rp_pin_code_reply *rp = (void *) skb->data;
755 struct hci_cp_pin_code_reply *cp;
756 struct hci_conn *conn;
757
9f1db00c 758 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
980e1a53 759
56e5cb86
JH
760 hci_dev_lock(hdev);
761
a8b2d5c2 762 if (test_bit(HCI_MGMT, &hdev->dev_flags))
744cf19e 763 mgmt_pin_code_reply_complete(hdev, &rp->bdaddr, rp->status);
980e1a53 764
fa1bd918 765 if (rp->status)
56e5cb86 766 goto unlock;
980e1a53
JH
767
768 cp = hci_sent_cmd_data(hdev, HCI_OP_PIN_CODE_REPLY);
769 if (!cp)
56e5cb86 770 goto unlock;
980e1a53
JH
771
772 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
773 if (conn)
774 conn->pin_length = cp->pin_len;
56e5cb86
JH
775
776unlock:
777 hci_dev_unlock(hdev);
980e1a53
JH
778}
779
780static void hci_cc_pin_code_neg_reply(struct hci_dev *hdev, struct sk_buff *skb)
781{
782 struct hci_rp_pin_code_neg_reply *rp = (void *) skb->data;
783
9f1db00c 784 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
980e1a53 785
56e5cb86
JH
786 hci_dev_lock(hdev);
787
a8b2d5c2 788 if (test_bit(HCI_MGMT, &hdev->dev_flags))
744cf19e 789 mgmt_pin_code_neg_reply_complete(hdev, &rp->bdaddr,
807deac2 790 rp->status);
56e5cb86
JH
791
792 hci_dev_unlock(hdev);
980e1a53 793}
56e5cb86 794
6ed58ec5
VT
795static void hci_cc_le_read_buffer_size(struct hci_dev *hdev,
796 struct sk_buff *skb)
797{
798 struct hci_rp_le_read_buffer_size *rp = (void *) skb->data;
799
9f1db00c 800 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
6ed58ec5
VT
801
802 if (rp->status)
803 return;
804
805 hdev->le_mtu = __le16_to_cpu(rp->le_mtu);
806 hdev->le_pkts = rp->le_max_pkt;
807
808 hdev->le_cnt = hdev->le_pkts;
809
810 BT_DBG("%s le mtu %d:%d", hdev->name, hdev->le_mtu, hdev->le_pkts);
6ed58ec5 811}
980e1a53 812
60e77321
JH
813static void hci_cc_le_read_local_features(struct hci_dev *hdev,
814 struct sk_buff *skb)
815{
816 struct hci_rp_le_read_local_features *rp = (void *) skb->data;
817
818 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
819
820 if (!rp->status)
821 memcpy(hdev->le_features, rp->features, 8);
60e77321
JH
822}
823
8fa19098
JH
824static void hci_cc_le_read_adv_tx_power(struct hci_dev *hdev,
825 struct sk_buff *skb)
826{
827 struct hci_rp_le_read_adv_tx_power *rp = (void *) skb->data;
828
829 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
830
04b4edcb 831 if (!rp->status)
8fa19098 832 hdev->adv_tx_power = rp->tx_power;
8fa19098
JH
833}
834
a5c29683
JH
835static void hci_cc_user_confirm_reply(struct hci_dev *hdev, struct sk_buff *skb)
836{
837 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
838
9f1db00c 839 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
a5c29683 840
56e5cb86
JH
841 hci_dev_lock(hdev);
842
a8b2d5c2 843 if (test_bit(HCI_MGMT, &hdev->dev_flags))
04124681
GP
844 mgmt_user_confirm_reply_complete(hdev, &rp->bdaddr, ACL_LINK, 0,
845 rp->status);
56e5cb86
JH
846
847 hci_dev_unlock(hdev);
a5c29683
JH
848}
849
850static void hci_cc_user_confirm_neg_reply(struct hci_dev *hdev,
807deac2 851 struct sk_buff *skb)
a5c29683
JH
852{
853 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
854
9f1db00c 855 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
a5c29683 856
56e5cb86
JH
857 hci_dev_lock(hdev);
858
a8b2d5c2 859 if (test_bit(HCI_MGMT, &hdev->dev_flags))
744cf19e 860 mgmt_user_confirm_neg_reply_complete(hdev, &rp->bdaddr,
04124681 861 ACL_LINK, 0, rp->status);
56e5cb86
JH
862
863 hci_dev_unlock(hdev);
a5c29683
JH
864}
865
1143d458
BG
866static void hci_cc_user_passkey_reply(struct hci_dev *hdev, struct sk_buff *skb)
867{
868 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
869
9f1db00c 870 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1143d458
BG
871
872 hci_dev_lock(hdev);
873
a8b2d5c2 874 if (test_bit(HCI_MGMT, &hdev->dev_flags))
272d90df 875 mgmt_user_passkey_reply_complete(hdev, &rp->bdaddr, ACL_LINK,
04124681 876 0, rp->status);
1143d458
BG
877
878 hci_dev_unlock(hdev);
879}
880
881static void hci_cc_user_passkey_neg_reply(struct hci_dev *hdev,
807deac2 882 struct sk_buff *skb)
1143d458
BG
883{
884 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
885
9f1db00c 886 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1143d458
BG
887
888 hci_dev_lock(hdev);
889
a8b2d5c2 890 if (test_bit(HCI_MGMT, &hdev->dev_flags))
1143d458 891 mgmt_user_passkey_neg_reply_complete(hdev, &rp->bdaddr,
04124681 892 ACL_LINK, 0, rp->status);
1143d458
BG
893
894 hci_dev_unlock(hdev);
895}
896
c35938b2 897static void hci_cc_read_local_oob_data_reply(struct hci_dev *hdev,
807deac2 898 struct sk_buff *skb)
c35938b2
SJ
899{
900 struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
901
9f1db00c 902 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
c35938b2 903
56e5cb86 904 hci_dev_lock(hdev);
744cf19e 905 mgmt_read_local_oob_data_reply_complete(hdev, rp->hash,
c35938b2 906 rp->randomizer, rp->status);
56e5cb86 907 hci_dev_unlock(hdev);
c35938b2
SJ
908}
909
c1d5dc4a
JH
910static void hci_cc_le_set_adv_enable(struct hci_dev *hdev, struct sk_buff *skb)
911{
912 __u8 *sent, status = *((__u8 *) skb->data);
913
914 BT_DBG("%s status 0x%2.2x", hdev->name, status);
915
916 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_ENABLE);
917 if (!sent)
918 return;
919
920 hci_dev_lock(hdev);
921
922 if (!status) {
923 if (*sent)
924 set_bit(HCI_LE_PERIPHERAL, &hdev->dev_flags);
925 else
926 clear_bit(HCI_LE_PERIPHERAL, &hdev->dev_flags);
927 }
928
04b4edcb
JH
929 if (!test_bit(HCI_INIT, &hdev->flags)) {
930 struct hci_request req;
c1d5dc4a 931
04b4edcb
JH
932 hci_req_init(&req, hdev);
933 hci_update_ad(&req);
934 hci_req_run(&req, NULL);
935 }
936
937 hci_dev_unlock(hdev);
c1d5dc4a
JH
938}
939
07f7fa5d
AG
940static void hci_cc_le_set_scan_param(struct hci_dev *hdev, struct sk_buff *skb)
941{
942 __u8 status = *((__u8 *) skb->data);
943
9f1db00c 944 BT_DBG("%s status 0x%2.2x", hdev->name, status);
7ba8b4be 945
07f7fa5d
AG
946}
947
eb9d91f5 948static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
807deac2 949 struct sk_buff *skb)
eb9d91f5
AG
950{
951 struct hci_cp_le_set_scan_enable *cp;
952 __u8 status = *((__u8 *) skb->data);
953
9f1db00c 954 BT_DBG("%s status 0x%2.2x", hdev->name, status);
eb9d91f5 955
eb9d91f5
AG
956 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_ENABLE);
957 if (!cp)
958 return;
959
68a8aea4 960 switch (cp->enable) {
76a388be 961 case LE_SCAN_ENABLE:
fef5234a 962 if (status)
7ba8b4be
AG
963 return;
964
d23264a8 965 set_bit(HCI_LE_SCAN, &hdev->dev_flags);
68a8aea4
AE
966 break;
967
76a388be 968 case LE_SCAN_DISABLE:
c9ecc48e
AG
969 if (status) {
970 hci_dev_lock(hdev);
971 mgmt_stop_discovery_failed(hdev, status);
972 hci_dev_unlock(hdev);
7ba8b4be 973 return;
c9ecc48e 974 }
7ba8b4be 975
d23264a8 976 clear_bit(HCI_LE_SCAN, &hdev->dev_flags);
68a8aea4
AE
977 break;
978
979 default:
980 BT_ERR("Used reserved LE_Scan_Enable param %d", cp->enable);
981 break;
35815085 982 }
eb9d91f5
AG
983}
984
cf1d081f
JH
985static void hci_cc_le_read_white_list_size(struct hci_dev *hdev,
986 struct sk_buff *skb)
987{
988 struct hci_rp_le_read_white_list_size *rp = (void *) skb->data;
989
990 BT_DBG("%s status 0x%2.2x size %u", hdev->name, rp->status, rp->size);
991
992 if (!rp->status)
993 hdev->le_white_list_size = rp->size;
cf1d081f
JH
994}
995
9b008c04
JH
996static void hci_cc_le_read_supported_states(struct hci_dev *hdev,
997 struct sk_buff *skb)
998{
999 struct hci_rp_le_read_supported_states *rp = (void *) skb->data;
1000
1001 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1002
1003 if (!rp->status)
1004 memcpy(hdev->le_states, rp->le_states, 8);
9b008c04
JH
1005}
1006
6039aa73
GP
1007static void hci_cc_write_le_host_supported(struct hci_dev *hdev,
1008 struct sk_buff *skb)
f9b49306 1009{
06199cf8 1010 struct hci_cp_write_le_host_supported *sent;
f9b49306
AG
1011 __u8 status = *((__u8 *) skb->data);
1012
9f1db00c 1013 BT_DBG("%s status 0x%2.2x", hdev->name, status);
f9b49306 1014
06199cf8 1015 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED);
8f984dfa 1016 if (!sent)
f9b49306
AG
1017 return;
1018
8f984dfa
JH
1019 if (!status) {
1020 if (sent->le)
cad718ed 1021 hdev->features[1][0] |= LMP_HOST_LE;
8f984dfa 1022 else
cad718ed 1023 hdev->features[1][0] &= ~LMP_HOST_LE;
53b2caab
JH
1024
1025 if (sent->simul)
cad718ed 1026 hdev->features[1][0] |= LMP_HOST_LE_BREDR;
53b2caab 1027 else
cad718ed 1028 hdev->features[1][0] &= ~LMP_HOST_LE_BREDR;
8f984dfa
JH
1029 }
1030
1031 if (test_bit(HCI_MGMT, &hdev->dev_flags) &&
807deac2 1032 !test_bit(HCI_INIT, &hdev->flags))
8f984dfa 1033 mgmt_le_enable_complete(hdev, sent->le, status);
f9b49306
AG
1034}
1035
93c284ee
AE
1036static void hci_cc_write_remote_amp_assoc(struct hci_dev *hdev,
1037 struct sk_buff *skb)
1038{
1039 struct hci_rp_write_remote_amp_assoc *rp = (void *) skb->data;
1040
1041 BT_DBG("%s status 0x%2.2x phy_handle 0x%2.2x",
1042 hdev->name, rp->status, rp->phy_handle);
1043
1044 if (rp->status)
1045 return;
1046
1047 amp_write_rem_assoc_continue(hdev, rp->phy_handle);
1048}
1049
6039aa73 1050static void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
a9de9248 1051{
9f1db00c 1052 BT_DBG("%s status 0x%2.2x", hdev->name, status);
a9de9248
MH
1053
1054 if (status) {
a9de9248 1055 hci_conn_check_pending(hdev);
314b2381
JH
1056 return;
1057 }
1058
89352e7d 1059 set_bit(HCI_INQUIRY, &hdev->flags);
1da177e4
LT
1060}
1061
6039aa73 1062static void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
1da177e4 1063{
a9de9248 1064 struct hci_cp_create_conn *cp;
1da177e4 1065 struct hci_conn *conn;
1da177e4 1066
9f1db00c 1067 BT_DBG("%s status 0x%2.2x", hdev->name, status);
a9de9248
MH
1068
1069 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_CONN);
1da177e4
LT
1070 if (!cp)
1071 return;
1072
1073 hci_dev_lock(hdev);
1074
1075 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
1076
6ed93dc6 1077 BT_DBG("%s bdaddr %pMR hcon %p", hdev->name, &cp->bdaddr, conn);
1da177e4
LT
1078
1079 if (status) {
1080 if (conn && conn->state == BT_CONNECT) {
4c67bc74
MH
1081 if (status != 0x0c || conn->attempt > 2) {
1082 conn->state = BT_CLOSED;
1083 hci_proto_connect_cfm(conn, status);
1084 hci_conn_del(conn);
1085 } else
1086 conn->state = BT_CONNECT2;
1da177e4
LT
1087 }
1088 } else {
1089 if (!conn) {
1090 conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr);
1091 if (conn) {
a0c808b3 1092 conn->out = true;
1da177e4
LT
1093 conn->link_mode |= HCI_LM_MASTER;
1094 } else
893ef971 1095 BT_ERR("No memory for new connection");
1da177e4
LT
1096 }
1097 }
1098
1099 hci_dev_unlock(hdev);
1100}
1101
a9de9248 1102static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
1da177e4 1103{
a9de9248
MH
1104 struct hci_cp_add_sco *cp;
1105 struct hci_conn *acl, *sco;
1106 __u16 handle;
1da177e4 1107
9f1db00c 1108 BT_DBG("%s status 0x%2.2x", hdev->name, status);
b6a0dc82 1109
a9de9248
MH
1110 if (!status)
1111 return;
1da177e4 1112
a9de9248
MH
1113 cp = hci_sent_cmd_data(hdev, HCI_OP_ADD_SCO);
1114 if (!cp)
1115 return;
1da177e4 1116
a9de9248 1117 handle = __le16_to_cpu(cp->handle);
1da177e4 1118
9f1db00c 1119 BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
1da177e4 1120
a9de9248 1121 hci_dev_lock(hdev);
1da177e4 1122
a9de9248 1123 acl = hci_conn_hash_lookup_handle(hdev, handle);
5a08ecce
AE
1124 if (acl) {
1125 sco = acl->link;
1126 if (sco) {
1127 sco->state = BT_CLOSED;
1da177e4 1128
5a08ecce
AE
1129 hci_proto_connect_cfm(sco, status);
1130 hci_conn_del(sco);
1131 }
a9de9248 1132 }
1da177e4 1133
a9de9248
MH
1134 hci_dev_unlock(hdev);
1135}
1da177e4 1136
f8558555
MH
1137static void hci_cs_auth_requested(struct hci_dev *hdev, __u8 status)
1138{
1139 struct hci_cp_auth_requested *cp;
1140 struct hci_conn *conn;
1141
9f1db00c 1142 BT_DBG("%s status 0x%2.2x", hdev->name, status);
f8558555
MH
1143
1144 if (!status)
1145 return;
1146
1147 cp = hci_sent_cmd_data(hdev, HCI_OP_AUTH_REQUESTED);
1148 if (!cp)
1149 return;
1150
1151 hci_dev_lock(hdev);
1152
1153 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1154 if (conn) {
1155 if (conn->state == BT_CONFIG) {
1156 hci_proto_connect_cfm(conn, status);
76a68ba0 1157 hci_conn_drop(conn);
f8558555
MH
1158 }
1159 }
1160
1161 hci_dev_unlock(hdev);
1162}
1163
1164static void hci_cs_set_conn_encrypt(struct hci_dev *hdev, __u8 status)
1165{
1166 struct hci_cp_set_conn_encrypt *cp;
1167 struct hci_conn *conn;
1168
9f1db00c 1169 BT_DBG("%s status 0x%2.2x", hdev->name, status);
f8558555
MH
1170
1171 if (!status)
1172 return;
1173
1174 cp = hci_sent_cmd_data(hdev, HCI_OP_SET_CONN_ENCRYPT);
1175 if (!cp)
1176 return;
1177
1178 hci_dev_lock(hdev);
1179
1180 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1181 if (conn) {
1182 if (conn->state == BT_CONFIG) {
1183 hci_proto_connect_cfm(conn, status);
76a68ba0 1184 hci_conn_drop(conn);
f8558555
MH
1185 }
1186 }
1187
1188 hci_dev_unlock(hdev);
1189}
1190
127178d2 1191static int hci_outgoing_auth_needed(struct hci_dev *hdev,
807deac2 1192 struct hci_conn *conn)
392599b9 1193{
392599b9
JH
1194 if (conn->state != BT_CONFIG || !conn->out)
1195 return 0;
1196
765c2a96 1197 if (conn->pending_sec_level == BT_SECURITY_SDP)
392599b9
JH
1198 return 0;
1199
1200 /* Only request authentication for SSP connections or non-SSP
e9bf2bf0 1201 * devices with sec_level HIGH or if MITM protection is requested */
807deac2
GP
1202 if (!hci_conn_ssp_enabled(conn) && !(conn->auth_type & 0x01) &&
1203 conn->pending_sec_level != BT_SECURITY_HIGH)
392599b9
JH
1204 return 0;
1205
392599b9
JH
1206 return 1;
1207}
1208
6039aa73 1209static int hci_resolve_name(struct hci_dev *hdev,
04124681 1210 struct inquiry_entry *e)
30dc78e1
JH
1211{
1212 struct hci_cp_remote_name_req cp;
1213
1214 memset(&cp, 0, sizeof(cp));
1215
1216 bacpy(&cp.bdaddr, &e->data.bdaddr);
1217 cp.pscan_rep_mode = e->data.pscan_rep_mode;
1218 cp.pscan_mode = e->data.pscan_mode;
1219 cp.clock_offset = e->data.clock_offset;
1220
1221 return hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
1222}
1223
b644ba33 1224static bool hci_resolve_next_name(struct hci_dev *hdev)
30dc78e1
JH
1225{
1226 struct discovery_state *discov = &hdev->discovery;
1227 struct inquiry_entry *e;
1228
b644ba33
JH
1229 if (list_empty(&discov->resolve))
1230 return false;
1231
1232 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
c810089c
RM
1233 if (!e)
1234 return false;
1235
b644ba33
JH
1236 if (hci_resolve_name(hdev, e) == 0) {
1237 e->name_state = NAME_PENDING;
1238 return true;
1239 }
1240
1241 return false;
1242}
1243
1244static void hci_check_pending_name(struct hci_dev *hdev, struct hci_conn *conn,
04124681 1245 bdaddr_t *bdaddr, u8 *name, u8 name_len)
b644ba33
JH
1246{
1247 struct discovery_state *discov = &hdev->discovery;
1248 struct inquiry_entry *e;
1249
1250 if (conn && !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
04124681
GP
1251 mgmt_device_connected(hdev, bdaddr, ACL_LINK, 0x00, 0, name,
1252 name_len, conn->dev_class);
b644ba33
JH
1253
1254 if (discov->state == DISCOVERY_STOPPED)
1255 return;
1256
30dc78e1
JH
1257 if (discov->state == DISCOVERY_STOPPING)
1258 goto discov_complete;
1259
1260 if (discov->state != DISCOVERY_RESOLVING)
1261 return;
1262
1263 e = hci_inquiry_cache_lookup_resolve(hdev, bdaddr, NAME_PENDING);
7cc8380e
RM
1264 /* If the device was not found in a list of found devices names of which
1265 * are pending. there is no need to continue resolving a next name as it
1266 * will be done upon receiving another Remote Name Request Complete
1267 * Event */
1268 if (!e)
1269 return;
1270
1271 list_del(&e->list);
1272 if (name) {
30dc78e1 1273 e->name_state = NAME_KNOWN;
7cc8380e
RM
1274 mgmt_remote_name(hdev, bdaddr, ACL_LINK, 0x00,
1275 e->data.rssi, name, name_len);
c3e7c0d9
RM
1276 } else {
1277 e->name_state = NAME_NOT_KNOWN;
30dc78e1
JH
1278 }
1279
b644ba33 1280 if (hci_resolve_next_name(hdev))
30dc78e1 1281 return;
30dc78e1
JH
1282
1283discov_complete:
1284 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1285}
1286
a9de9248
MH
1287static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
1288{
127178d2
JH
1289 struct hci_cp_remote_name_req *cp;
1290 struct hci_conn *conn;
1291
9f1db00c 1292 BT_DBG("%s status 0x%2.2x", hdev->name, status);
127178d2
JH
1293
1294 /* If successful wait for the name req complete event before
1295 * checking for the need to do authentication */
1296 if (!status)
1297 return;
1298
1299 cp = hci_sent_cmd_data(hdev, HCI_OP_REMOTE_NAME_REQ);
1300 if (!cp)
1301 return;
1302
1303 hci_dev_lock(hdev);
1304
b644ba33
JH
1305 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
1306
a8b2d5c2 1307 if (test_bit(HCI_MGMT, &hdev->dev_flags))
b644ba33 1308 hci_check_pending_name(hdev, conn, &cp->bdaddr, NULL, 0);
30dc78e1 1309
79c6c70c
JH
1310 if (!conn)
1311 goto unlock;
1312
1313 if (!hci_outgoing_auth_needed(hdev, conn))
1314 goto unlock;
1315
51a8efd7 1316 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
127178d2
JH
1317 struct hci_cp_auth_requested cp;
1318 cp.handle = __cpu_to_le16(conn->handle);
1319 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
1320 }
1321
79c6c70c 1322unlock:
127178d2 1323 hci_dev_unlock(hdev);
a9de9248 1324}
1da177e4 1325
769be974
MH
1326static void hci_cs_read_remote_features(struct hci_dev *hdev, __u8 status)
1327{
1328 struct hci_cp_read_remote_features *cp;
1329 struct hci_conn *conn;
1330
9f1db00c 1331 BT_DBG("%s status 0x%2.2x", hdev->name, status);
769be974
MH
1332
1333 if (!status)
1334 return;
1335
1336 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_FEATURES);
1337 if (!cp)
1338 return;
1339
1340 hci_dev_lock(hdev);
1341
1342 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1343 if (conn) {
1344 if (conn->state == BT_CONFIG) {
769be974 1345 hci_proto_connect_cfm(conn, status);
76a68ba0 1346 hci_conn_drop(conn);
769be974
MH
1347 }
1348 }
1349
1350 hci_dev_unlock(hdev);
1351}
1352
1353static void hci_cs_read_remote_ext_features(struct hci_dev *hdev, __u8 status)
1354{
1355 struct hci_cp_read_remote_ext_features *cp;
1356 struct hci_conn *conn;
1357
9f1db00c 1358 BT_DBG("%s status 0x%2.2x", hdev->name, status);
769be974
MH
1359
1360 if (!status)
1361 return;
1362
1363 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES);
1364 if (!cp)
1365 return;
1366
1367 hci_dev_lock(hdev);
1368
1369 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1370 if (conn) {
1371 if (conn->state == BT_CONFIG) {
769be974 1372 hci_proto_connect_cfm(conn, status);
76a68ba0 1373 hci_conn_drop(conn);
769be974
MH
1374 }
1375 }
1376
1377 hci_dev_unlock(hdev);
1378}
1379
a9de9248
MH
1380static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
1381{
b6a0dc82
MH
1382 struct hci_cp_setup_sync_conn *cp;
1383 struct hci_conn *acl, *sco;
1384 __u16 handle;
1385
9f1db00c 1386 BT_DBG("%s status 0x%2.2x", hdev->name, status);
b6a0dc82
MH
1387
1388 if (!status)
1389 return;
1390
1391 cp = hci_sent_cmd_data(hdev, HCI_OP_SETUP_SYNC_CONN);
1392 if (!cp)
1393 return;
1394
1395 handle = __le16_to_cpu(cp->handle);
1396
9f1db00c 1397 BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
b6a0dc82
MH
1398
1399 hci_dev_lock(hdev);
1400
1401 acl = hci_conn_hash_lookup_handle(hdev, handle);
5a08ecce
AE
1402 if (acl) {
1403 sco = acl->link;
1404 if (sco) {
1405 sco->state = BT_CLOSED;
b6a0dc82 1406
5a08ecce
AE
1407 hci_proto_connect_cfm(sco, status);
1408 hci_conn_del(sco);
1409 }
b6a0dc82
MH
1410 }
1411
1412 hci_dev_unlock(hdev);
1da177e4
LT
1413}
1414
a9de9248 1415static void hci_cs_sniff_mode(struct hci_dev *hdev, __u8 status)
1da177e4 1416{
a9de9248
MH
1417 struct hci_cp_sniff_mode *cp;
1418 struct hci_conn *conn;
1da177e4 1419
9f1db00c 1420 BT_DBG("%s status 0x%2.2x", hdev->name, status);
04837f64 1421
a9de9248
MH
1422 if (!status)
1423 return;
04837f64 1424
a9de9248
MH
1425 cp = hci_sent_cmd_data(hdev, HCI_OP_SNIFF_MODE);
1426 if (!cp)
1427 return;
04837f64 1428
a9de9248 1429 hci_dev_lock(hdev);
04837f64 1430
a9de9248 1431 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
e73439d8 1432 if (conn) {
51a8efd7 1433 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
04837f64 1434
51a8efd7 1435 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
e73439d8
MH
1436 hci_sco_setup(conn, status);
1437 }
1438
a9de9248
MH
1439 hci_dev_unlock(hdev);
1440}
04837f64 1441
a9de9248
MH
1442static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
1443{
1444 struct hci_cp_exit_sniff_mode *cp;
1445 struct hci_conn *conn;
04837f64 1446
9f1db00c 1447 BT_DBG("%s status 0x%2.2x", hdev->name, status);
04837f64 1448
a9de9248
MH
1449 if (!status)
1450 return;
04837f64 1451
a9de9248
MH
1452 cp = hci_sent_cmd_data(hdev, HCI_OP_EXIT_SNIFF_MODE);
1453 if (!cp)
1454 return;
04837f64 1455
a9de9248 1456 hci_dev_lock(hdev);
1da177e4 1457
a9de9248 1458 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
e73439d8 1459 if (conn) {
51a8efd7 1460 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
1da177e4 1461
51a8efd7 1462 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
e73439d8
MH
1463 hci_sco_setup(conn, status);
1464 }
1465
a9de9248 1466 hci_dev_unlock(hdev);
1da177e4
LT
1467}
1468
88c3df13
JH
1469static void hci_cs_disconnect(struct hci_dev *hdev, u8 status)
1470{
1471 struct hci_cp_disconnect *cp;
1472 struct hci_conn *conn;
1473
1474 if (!status)
1475 return;
1476
1477 cp = hci_sent_cmd_data(hdev, HCI_OP_DISCONNECT);
1478 if (!cp)
1479 return;
1480
1481 hci_dev_lock(hdev);
1482
1483 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1484 if (conn)
1485 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
04124681 1486 conn->dst_type, status);
88c3df13
JH
1487
1488 hci_dev_unlock(hdev);
1489}
1490
fcd89c09
VT
1491static void hci_cs_le_create_conn(struct hci_dev *hdev, __u8 status)
1492{
fcd89c09
VT
1493 struct hci_conn *conn;
1494
9f1db00c 1495 BT_DBG("%s status 0x%2.2x", hdev->name, status);
fcd89c09 1496
f00a06ac
AG
1497 if (status) {
1498 hci_dev_lock(hdev);
fcd89c09 1499
0c95ab78 1500 conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
f00a06ac
AG
1501 if (!conn) {
1502 hci_dev_unlock(hdev);
1503 return;
1504 }
fcd89c09 1505
6ed93dc6 1506 BT_DBG("%s bdaddr %pMR conn %p", hdev->name, &conn->dst, conn);
fcd89c09 1507
f00a06ac 1508 conn->state = BT_CLOSED;
0c95ab78 1509 mgmt_connect_failed(hdev, &conn->dst, conn->type,
f00a06ac
AG
1510 conn->dst_type, status);
1511 hci_proto_connect_cfm(conn, status);
1512 hci_conn_del(conn);
fcd89c09 1513
f00a06ac
AG
1514 hci_dev_unlock(hdev);
1515 }
fcd89c09
VT
1516}
1517
a02226d6
AE
1518static void hci_cs_create_phylink(struct hci_dev *hdev, u8 status)
1519{
93c284ee
AE
1520 struct hci_cp_create_phy_link *cp;
1521
a02226d6 1522 BT_DBG("%s status 0x%2.2x", hdev->name, status);
93c284ee 1523
93c284ee
AE
1524 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_PHY_LINK);
1525 if (!cp)
1526 return;
1527
e58917b9
AE
1528 hci_dev_lock(hdev);
1529
1530 if (status) {
1531 struct hci_conn *hcon;
1532
1533 hcon = hci_conn_hash_lookup_handle(hdev, cp->phy_handle);
1534 if (hcon)
1535 hci_conn_del(hcon);
1536 } else {
1537 amp_write_remote_assoc(hdev, cp->phy_handle);
1538 }
1539
1540 hci_dev_unlock(hdev);
a02226d6
AE
1541}
1542
0b26ab9d
AE
1543static void hci_cs_accept_phylink(struct hci_dev *hdev, u8 status)
1544{
1545 struct hci_cp_accept_phy_link *cp;
1546
1547 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1548
1549 if (status)
1550 return;
1551
1552 cp = hci_sent_cmd_data(hdev, HCI_OP_ACCEPT_PHY_LINK);
1553 if (!cp)
1554 return;
1555
1556 amp_write_remote_assoc(hdev, cp->phy_handle);
1557}
1558
6039aa73 1559static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1da177e4
LT
1560{
1561 __u8 status = *((__u8 *) skb->data);
30dc78e1
JH
1562 struct discovery_state *discov = &hdev->discovery;
1563 struct inquiry_entry *e;
1da177e4 1564
9f1db00c 1565 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1da177e4 1566
a9de9248 1567 hci_conn_check_pending(hdev);
89352e7d
AG
1568
1569 if (!test_and_clear_bit(HCI_INQUIRY, &hdev->flags))
1570 return;
1571
3e13fa1e
AG
1572 smp_mb__after_clear_bit(); /* wake_up_bit advises about this barrier */
1573 wake_up_bit(&hdev->flags, HCI_INQUIRY);
1574
a8b2d5c2 1575 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
30dc78e1
JH
1576 return;
1577
56e5cb86 1578 hci_dev_lock(hdev);
30dc78e1 1579
343f935b 1580 if (discov->state != DISCOVERY_FINDING)
30dc78e1
JH
1581 goto unlock;
1582
1583 if (list_empty(&discov->resolve)) {
1584 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1585 goto unlock;
1586 }
1587
1588 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
1589 if (e && hci_resolve_name(hdev, e) == 0) {
1590 e->name_state = NAME_PENDING;
1591 hci_discovery_set_state(hdev, DISCOVERY_RESOLVING);
1592 } else {
1593 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1594 }
1595
1596unlock:
56e5cb86 1597 hci_dev_unlock(hdev);
1da177e4
LT
1598}
1599
6039aa73 1600static void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
1da177e4 1601{
45bb4bf0 1602 struct inquiry_data data;
a9de9248 1603 struct inquiry_info *info = (void *) (skb->data + 1);
1da177e4
LT
1604 int num_rsp = *((__u8 *) skb->data);
1605
1606 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
1607
45bb4bf0
MH
1608 if (!num_rsp)
1609 return;
1610
1519cc17
AG
1611 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags))
1612 return;
1613
1da177e4 1614 hci_dev_lock(hdev);
45bb4bf0 1615
e17acd40 1616 for (; num_rsp; num_rsp--, info++) {
388fc8fa 1617 bool name_known, ssp;
3175405b 1618
1da177e4
LT
1619 bacpy(&data.bdaddr, &info->bdaddr);
1620 data.pscan_rep_mode = info->pscan_rep_mode;
1621 data.pscan_period_mode = info->pscan_period_mode;
1622 data.pscan_mode = info->pscan_mode;
1623 memcpy(data.dev_class, info->dev_class, 3);
1624 data.clock_offset = info->clock_offset;
1625 data.rssi = 0x00;
41a96212 1626 data.ssp_mode = 0x00;
3175405b 1627
388fc8fa 1628 name_known = hci_inquiry_cache_update(hdev, &data, false, &ssp);
48264f06 1629 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
04124681
GP
1630 info->dev_class, 0, !name_known, ssp, NULL,
1631 0);
1da177e4 1632 }
45bb4bf0 1633
1da177e4
LT
1634 hci_dev_unlock(hdev);
1635}
1636
6039aa73 1637static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1da177e4 1638{
a9de9248
MH
1639 struct hci_ev_conn_complete *ev = (void *) skb->data;
1640 struct hci_conn *conn;
1da177e4
LT
1641
1642 BT_DBG("%s", hdev->name);
1643
1644 hci_dev_lock(hdev);
1645
1646 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
9499237a
MH
1647 if (!conn) {
1648 if (ev->link_type != SCO_LINK)
1649 goto unlock;
1650
1651 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
1652 if (!conn)
1653 goto unlock;
1654
1655 conn->type = SCO_LINK;
1656 }
1da177e4
LT
1657
1658 if (!ev->status) {
1659 conn->handle = __le16_to_cpu(ev->handle);
769be974
MH
1660
1661 if (conn->type == ACL_LINK) {
1662 conn->state = BT_CONFIG;
1663 hci_conn_hold(conn);
a9ea3ed9
SJ
1664
1665 if (!conn->out && !hci_conn_ssp_enabled(conn) &&
1666 !hci_find_link_key(hdev, &ev->bdaddr))
1667 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
1668 else
1669 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
769be974
MH
1670 } else
1671 conn->state = BT_CONNECTED;
1da177e4 1672
7d0db0a3
MH
1673 hci_conn_add_sysfs(conn);
1674
1da177e4
LT
1675 if (test_bit(HCI_AUTH, &hdev->flags))
1676 conn->link_mode |= HCI_LM_AUTH;
1677
1678 if (test_bit(HCI_ENCRYPT, &hdev->flags))
1679 conn->link_mode |= HCI_LM_ENCRYPT;
1680
04837f64
MH
1681 /* Get remote features */
1682 if (conn->type == ACL_LINK) {
1683 struct hci_cp_read_remote_features cp;
1684 cp.handle = ev->handle;
769be974 1685 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES,
04124681 1686 sizeof(cp), &cp);
04837f64
MH
1687 }
1688
1da177e4 1689 /* Set packet type for incoming connection */
d095c1eb 1690 if (!conn->out && hdev->hci_ver < BLUETOOTH_VER_2_0) {
1da177e4
LT
1691 struct hci_cp_change_conn_ptype cp;
1692 cp.handle = ev->handle;
a8746417 1693 cp.pkt_type = cpu_to_le16(conn->pkt_type);
04124681
GP
1694 hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE, sizeof(cp),
1695 &cp);
1da177e4 1696 }
17d5c04c 1697 } else {
1da177e4 1698 conn->state = BT_CLOSED;
17d5c04c 1699 if (conn->type == ACL_LINK)
744cf19e 1700 mgmt_connect_failed(hdev, &ev->bdaddr, conn->type,
04124681 1701 conn->dst_type, ev->status);
17d5c04c 1702 }
1da177e4 1703
e73439d8
MH
1704 if (conn->type == ACL_LINK)
1705 hci_sco_setup(conn, ev->status);
1da177e4 1706
769be974
MH
1707 if (ev->status) {
1708 hci_proto_connect_cfm(conn, ev->status);
1da177e4 1709 hci_conn_del(conn);
c89b6e6b
MH
1710 } else if (ev->link_type != ACL_LINK)
1711 hci_proto_connect_cfm(conn, ev->status);
1da177e4 1712
a9de9248 1713unlock:
1da177e4 1714 hci_dev_unlock(hdev);
1da177e4 1715
a9de9248 1716 hci_conn_check_pending(hdev);
1da177e4
LT
1717}
1718
6039aa73 1719static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
1da177e4 1720{
a9de9248
MH
1721 struct hci_ev_conn_request *ev = (void *) skb->data;
1722 int mask = hdev->link_mode;
20714bfe 1723 __u8 flags = 0;
1da177e4 1724
6ed93dc6 1725 BT_DBG("%s bdaddr %pMR type 0x%x", hdev->name, &ev->bdaddr,
807deac2 1726 ev->link_type);
1da177e4 1727
20714bfe
FD
1728 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type,
1729 &flags);
1da177e4 1730
138d22ef 1731 if ((mask & HCI_LM_ACCEPT) &&
807deac2 1732 !hci_blacklist_lookup(hdev, &ev->bdaddr)) {
a9de9248 1733 /* Connection accepted */
c7bdd502 1734 struct inquiry_entry *ie;
1da177e4 1735 struct hci_conn *conn;
1da177e4 1736
a9de9248 1737 hci_dev_lock(hdev);
b6a0dc82 1738
cc11b9c1
AE
1739 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
1740 if (ie)
c7bdd502
MH
1741 memcpy(ie->data.dev_class, ev->dev_class, 3);
1742
8fc9ced3
GP
1743 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type,
1744 &ev->bdaddr);
a9de9248 1745 if (!conn) {
cc11b9c1
AE
1746 conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr);
1747 if (!conn) {
893ef971 1748 BT_ERR("No memory for new connection");
a9de9248
MH
1749 hci_dev_unlock(hdev);
1750 return;
1da177e4
LT
1751 }
1752 }
b6a0dc82 1753
a9de9248 1754 memcpy(conn->dev_class, ev->dev_class, 3);
b6a0dc82 1755
a9de9248 1756 hci_dev_unlock(hdev);
1da177e4 1757
20714bfe
FD
1758 if (ev->link_type == ACL_LINK ||
1759 (!(flags & HCI_PROTO_DEFER) && !lmp_esco_capable(hdev))) {
b6a0dc82 1760 struct hci_cp_accept_conn_req cp;
20714bfe 1761 conn->state = BT_CONNECT;
1da177e4 1762
b6a0dc82
MH
1763 bacpy(&cp.bdaddr, &ev->bdaddr);
1764
1765 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
1766 cp.role = 0x00; /* Become master */
1767 else
1768 cp.role = 0x01; /* Remain slave */
1769
04124681
GP
1770 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp),
1771 &cp);
20714bfe 1772 } else if (!(flags & HCI_PROTO_DEFER)) {
b6a0dc82 1773 struct hci_cp_accept_sync_conn_req cp;
20714bfe 1774 conn->state = BT_CONNECT;
b6a0dc82
MH
1775
1776 bacpy(&cp.bdaddr, &ev->bdaddr);
a8746417 1777 cp.pkt_type = cpu_to_le16(conn->pkt_type);
b6a0dc82 1778
82781e63
AE
1779 cp.tx_bandwidth = __constant_cpu_to_le32(0x00001f40);
1780 cp.rx_bandwidth = __constant_cpu_to_le32(0x00001f40);
1781 cp.max_latency = __constant_cpu_to_le16(0xffff);
b6a0dc82
MH
1782 cp.content_format = cpu_to_le16(hdev->voice_setting);
1783 cp.retrans_effort = 0xff;
1da177e4 1784
b6a0dc82 1785 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ,
04124681 1786 sizeof(cp), &cp);
20714bfe
FD
1787 } else {
1788 conn->state = BT_CONNECT2;
1789 hci_proto_connect_cfm(conn, 0);
b6a0dc82 1790 }
a9de9248
MH
1791 } else {
1792 /* Connection rejected */
1793 struct hci_cp_reject_conn_req cp;
1da177e4 1794
a9de9248 1795 bacpy(&cp.bdaddr, &ev->bdaddr);
9f5a0d7b 1796 cp.reason = HCI_ERROR_REJ_BAD_ADDR;
a9de9248 1797 hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
1da177e4 1798 }
1da177e4
LT
1799}
1800
f0d6a0ea
MA
1801static u8 hci_to_mgmt_reason(u8 err)
1802{
1803 switch (err) {
1804 case HCI_ERROR_CONNECTION_TIMEOUT:
1805 return MGMT_DEV_DISCONN_TIMEOUT;
1806 case HCI_ERROR_REMOTE_USER_TERM:
1807 case HCI_ERROR_REMOTE_LOW_RESOURCES:
1808 case HCI_ERROR_REMOTE_POWER_OFF:
1809 return MGMT_DEV_DISCONN_REMOTE;
1810 case HCI_ERROR_LOCAL_HOST_TERM:
1811 return MGMT_DEV_DISCONN_LOCAL_HOST;
1812 default:
1813 return MGMT_DEV_DISCONN_UNKNOWN;
1814 }
1815}
1816
6039aa73 1817static void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
04837f64 1818{
a9de9248 1819 struct hci_ev_disconn_complete *ev = (void *) skb->data;
04837f64
MH
1820 struct hci_conn *conn;
1821
9f1db00c 1822 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
04837f64
MH
1823
1824 hci_dev_lock(hdev);
1825
1826 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
f7520543
JH
1827 if (!conn)
1828 goto unlock;
7d0db0a3 1829
37d9ef76
JH
1830 if (ev->status == 0)
1831 conn->state = BT_CLOSED;
04837f64 1832
b644ba33 1833 if (test_and_clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags) &&
807deac2 1834 (conn->type == ACL_LINK || conn->type == LE_LINK)) {
f0d6a0ea 1835 if (ev->status) {
88c3df13 1836 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
807deac2 1837 conn->dst_type, ev->status);
f0d6a0ea
MA
1838 } else {
1839 u8 reason = hci_to_mgmt_reason(ev->reason);
1840
afc747a6 1841 mgmt_device_disconnected(hdev, &conn->dst, conn->type,
f0d6a0ea
MA
1842 conn->dst_type, reason);
1843 }
37d9ef76 1844 }
f7520543 1845
37d9ef76 1846 if (ev->status == 0) {
6ec5bcad
VA
1847 if (conn->type == ACL_LINK && conn->flush_key)
1848 hci_remove_link_key(hdev, &conn->dst);
37d9ef76
JH
1849 hci_proto_disconn_cfm(conn, ev->reason);
1850 hci_conn_del(conn);
1851 }
f7520543
JH
1852
1853unlock:
04837f64
MH
1854 hci_dev_unlock(hdev);
1855}
1856
6039aa73 1857static void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1da177e4 1858{
a9de9248 1859 struct hci_ev_auth_complete *ev = (void *) skb->data;
04837f64 1860 struct hci_conn *conn;
1da177e4 1861
9f1db00c 1862 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
1da177e4
LT
1863
1864 hci_dev_lock(hdev);
1865
04837f64 1866 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
d7556e20
WR
1867 if (!conn)
1868 goto unlock;
1869
1870 if (!ev->status) {
aa64a8b5 1871 if (!hci_conn_ssp_enabled(conn) &&
807deac2 1872 test_bit(HCI_CONN_REAUTH_PEND, &conn->flags)) {
d7556e20 1873 BT_INFO("re-auth of legacy device is not possible.");
2a611692 1874 } else {
d7556e20
WR
1875 conn->link_mode |= HCI_LM_AUTH;
1876 conn->sec_level = conn->pending_sec_level;
2a611692 1877 }
d7556e20 1878 } else {
bab73cb6 1879 mgmt_auth_failed(hdev, &conn->dst, conn->type, conn->dst_type,
04124681 1880 ev->status);
d7556e20 1881 }
1da177e4 1882
51a8efd7
JH
1883 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
1884 clear_bit(HCI_CONN_REAUTH_PEND, &conn->flags);
1da177e4 1885
d7556e20 1886 if (conn->state == BT_CONFIG) {
aa64a8b5 1887 if (!ev->status && hci_conn_ssp_enabled(conn)) {
d7556e20
WR
1888 struct hci_cp_set_conn_encrypt cp;
1889 cp.handle = ev->handle;
1890 cp.encrypt = 0x01;
1891 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
807deac2 1892 &cp);
052b30b0 1893 } else {
d7556e20
WR
1894 conn->state = BT_CONNECTED;
1895 hci_proto_connect_cfm(conn, ev->status);
76a68ba0 1896 hci_conn_drop(conn);
052b30b0 1897 }
d7556e20
WR
1898 } else {
1899 hci_auth_cfm(conn, ev->status);
052b30b0 1900
d7556e20
WR
1901 hci_conn_hold(conn);
1902 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
76a68ba0 1903 hci_conn_drop(conn);
d7556e20
WR
1904 }
1905
51a8efd7 1906 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
d7556e20
WR
1907 if (!ev->status) {
1908 struct hci_cp_set_conn_encrypt cp;
1909 cp.handle = ev->handle;
1910 cp.encrypt = 0x01;
1911 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
807deac2 1912 &cp);
d7556e20 1913 } else {
51a8efd7 1914 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
d7556e20 1915 hci_encrypt_cfm(conn, ev->status, 0x00);
1da177e4
LT
1916 }
1917 }
1918
d7556e20 1919unlock:
1da177e4
LT
1920 hci_dev_unlock(hdev);
1921}
1922
6039aa73 1923static void hci_remote_name_evt(struct hci_dev *hdev, struct sk_buff *skb)
1da177e4 1924{
127178d2
JH
1925 struct hci_ev_remote_name *ev = (void *) skb->data;
1926 struct hci_conn *conn;
1927
a9de9248 1928 BT_DBG("%s", hdev->name);
1da177e4 1929
a9de9248 1930 hci_conn_check_pending(hdev);
127178d2
JH
1931
1932 hci_dev_lock(hdev);
1933
b644ba33 1934 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
30dc78e1 1935
b644ba33
JH
1936 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
1937 goto check_auth;
a88a9652 1938
b644ba33
JH
1939 if (ev->status == 0)
1940 hci_check_pending_name(hdev, conn, &ev->bdaddr, ev->name,
04124681 1941 strnlen(ev->name, HCI_MAX_NAME_LENGTH));
b644ba33
JH
1942 else
1943 hci_check_pending_name(hdev, conn, &ev->bdaddr, NULL, 0);
1944
1945check_auth:
79c6c70c
JH
1946 if (!conn)
1947 goto unlock;
1948
1949 if (!hci_outgoing_auth_needed(hdev, conn))
1950 goto unlock;
1951
51a8efd7 1952 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
127178d2
JH
1953 struct hci_cp_auth_requested cp;
1954 cp.handle = __cpu_to_le16(conn->handle);
1955 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
1956 }
1957
79c6c70c 1958unlock:
127178d2 1959 hci_dev_unlock(hdev);
a9de9248
MH
1960}
1961
6039aa73 1962static void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
a9de9248
MH
1963{
1964 struct hci_ev_encrypt_change *ev = (void *) skb->data;
1965 struct hci_conn *conn;
1966
9f1db00c 1967 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
1da177e4
LT
1968
1969 hci_dev_lock(hdev);
1970
04837f64 1971 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1da177e4
LT
1972 if (conn) {
1973 if (!ev->status) {
ae293196
MH
1974 if (ev->encrypt) {
1975 /* Encryption implies authentication */
1976 conn->link_mode |= HCI_LM_AUTH;
1da177e4 1977 conn->link_mode |= HCI_LM_ENCRYPT;
da85e5e5 1978 conn->sec_level = conn->pending_sec_level;
ae293196 1979 } else
1da177e4
LT
1980 conn->link_mode &= ~HCI_LM_ENCRYPT;
1981 }
1982
51a8efd7 1983 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
1da177e4 1984
a7d7723a 1985 if (ev->status && conn->state == BT_CONNECTED) {
bed71748 1986 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
76a68ba0 1987 hci_conn_drop(conn);
a7d7723a
GP
1988 goto unlock;
1989 }
1990
f8558555
MH
1991 if (conn->state == BT_CONFIG) {
1992 if (!ev->status)
1993 conn->state = BT_CONNECTED;
1994
1995 hci_proto_connect_cfm(conn, ev->status);
76a68ba0 1996 hci_conn_drop(conn);
f8558555
MH
1997 } else
1998 hci_encrypt_cfm(conn, ev->status, ev->encrypt);
1da177e4
LT
1999 }
2000
a7d7723a 2001unlock:
1da177e4
LT
2002 hci_dev_unlock(hdev);
2003}
2004
6039aa73
GP
2005static void hci_change_link_key_complete_evt(struct hci_dev *hdev,
2006 struct sk_buff *skb)
1da177e4 2007{
a9de9248 2008 struct hci_ev_change_link_key_complete *ev = (void *) skb->data;
04837f64 2009 struct hci_conn *conn;
1da177e4 2010
9f1db00c 2011 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
1da177e4
LT
2012
2013 hci_dev_lock(hdev);
2014
04837f64 2015 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1da177e4
LT
2016 if (conn) {
2017 if (!ev->status)
2018 conn->link_mode |= HCI_LM_SECURE;
2019
51a8efd7 2020 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
1da177e4
LT
2021
2022 hci_key_change_cfm(conn, ev->status);
2023 }
2024
2025 hci_dev_unlock(hdev);
2026}
2027
6039aa73
GP
2028static void hci_remote_features_evt(struct hci_dev *hdev,
2029 struct sk_buff *skb)
1da177e4 2030{
a9de9248
MH
2031 struct hci_ev_remote_features *ev = (void *) skb->data;
2032 struct hci_conn *conn;
2033
9f1db00c 2034 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
a9de9248 2035
a9de9248
MH
2036 hci_dev_lock(hdev);
2037
2038 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
ccd556fe
JH
2039 if (!conn)
2040 goto unlock;
769be974 2041
ccd556fe 2042 if (!ev->status)
cad718ed 2043 memcpy(conn->features[0], ev->features, 8);
ccd556fe
JH
2044
2045 if (conn->state != BT_CONFIG)
2046 goto unlock;
2047
2048 if (!ev->status && lmp_ssp_capable(hdev) && lmp_ssp_capable(conn)) {
2049 struct hci_cp_read_remote_ext_features cp;
2050 cp.handle = ev->handle;
2051 cp.page = 0x01;
2052 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES,
807deac2 2053 sizeof(cp), &cp);
392599b9
JH
2054 goto unlock;
2055 }
2056
671267bf 2057 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
127178d2
JH
2058 struct hci_cp_remote_name_req cp;
2059 memset(&cp, 0, sizeof(cp));
2060 bacpy(&cp.bdaddr, &conn->dst);
2061 cp.pscan_rep_mode = 0x02;
2062 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
b644ba33
JH
2063 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
2064 mgmt_device_connected(hdev, &conn->dst, conn->type,
04124681
GP
2065 conn->dst_type, 0, NULL, 0,
2066 conn->dev_class);
392599b9 2067
127178d2 2068 if (!hci_outgoing_auth_needed(hdev, conn)) {
ccd556fe
JH
2069 conn->state = BT_CONNECTED;
2070 hci_proto_connect_cfm(conn, ev->status);
76a68ba0 2071 hci_conn_drop(conn);
769be974 2072 }
a9de9248 2073
ccd556fe 2074unlock:
a9de9248 2075 hci_dev_unlock(hdev);
1da177e4
LT
2076}
2077
6039aa73 2078static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
a9de9248
MH
2079{
2080 struct hci_ev_cmd_complete *ev = (void *) skb->data;
9238f36a 2081 u8 status = skb->data[sizeof(*ev)];
a9de9248
MH
2082 __u16 opcode;
2083
2084 skb_pull(skb, sizeof(*ev));
2085
2086 opcode = __le16_to_cpu(ev->opcode);
2087
2088 switch (opcode) {
2089 case HCI_OP_INQUIRY_CANCEL:
2090 hci_cc_inquiry_cancel(hdev, skb);
2091 break;
2092
4d93483b
AG
2093 case HCI_OP_PERIODIC_INQ:
2094 hci_cc_periodic_inq(hdev, skb);
2095 break;
2096
a9de9248
MH
2097 case HCI_OP_EXIT_PERIODIC_INQ:
2098 hci_cc_exit_periodic_inq(hdev, skb);
2099 break;
2100
2101 case HCI_OP_REMOTE_NAME_REQ_CANCEL:
2102 hci_cc_remote_name_req_cancel(hdev, skb);
2103 break;
2104
2105 case HCI_OP_ROLE_DISCOVERY:
2106 hci_cc_role_discovery(hdev, skb);
2107 break;
2108
e4e8e37c
MH
2109 case HCI_OP_READ_LINK_POLICY:
2110 hci_cc_read_link_policy(hdev, skb);
2111 break;
2112
a9de9248
MH
2113 case HCI_OP_WRITE_LINK_POLICY:
2114 hci_cc_write_link_policy(hdev, skb);
2115 break;
2116
e4e8e37c
MH
2117 case HCI_OP_READ_DEF_LINK_POLICY:
2118 hci_cc_read_def_link_policy(hdev, skb);
2119 break;
2120
2121 case HCI_OP_WRITE_DEF_LINK_POLICY:
2122 hci_cc_write_def_link_policy(hdev, skb);
2123 break;
2124
a9de9248
MH
2125 case HCI_OP_RESET:
2126 hci_cc_reset(hdev, skb);
2127 break;
2128
2129 case HCI_OP_WRITE_LOCAL_NAME:
2130 hci_cc_write_local_name(hdev, skb);
2131 break;
2132
2133 case HCI_OP_READ_LOCAL_NAME:
2134 hci_cc_read_local_name(hdev, skb);
2135 break;
2136
2137 case HCI_OP_WRITE_AUTH_ENABLE:
2138 hci_cc_write_auth_enable(hdev, skb);
2139 break;
2140
2141 case HCI_OP_WRITE_ENCRYPT_MODE:
2142 hci_cc_write_encrypt_mode(hdev, skb);
2143 break;
2144
2145 case HCI_OP_WRITE_SCAN_ENABLE:
2146 hci_cc_write_scan_enable(hdev, skb);
2147 break;
2148
2149 case HCI_OP_READ_CLASS_OF_DEV:
2150 hci_cc_read_class_of_dev(hdev, skb);
2151 break;
2152
2153 case HCI_OP_WRITE_CLASS_OF_DEV:
2154 hci_cc_write_class_of_dev(hdev, skb);
2155 break;
2156
2157 case HCI_OP_READ_VOICE_SETTING:
2158 hci_cc_read_voice_setting(hdev, skb);
2159 break;
2160
2161 case HCI_OP_WRITE_VOICE_SETTING:
2162 hci_cc_write_voice_setting(hdev, skb);
2163 break;
2164
333140b5
MH
2165 case HCI_OP_WRITE_SSP_MODE:
2166 hci_cc_write_ssp_mode(hdev, skb);
2167 break;
2168
a9de9248
MH
2169 case HCI_OP_READ_LOCAL_VERSION:
2170 hci_cc_read_local_version(hdev, skb);
2171 break;
2172
2173 case HCI_OP_READ_LOCAL_COMMANDS:
2174 hci_cc_read_local_commands(hdev, skb);
2175 break;
2176
2177 case HCI_OP_READ_LOCAL_FEATURES:
2178 hci_cc_read_local_features(hdev, skb);
2179 break;
2180
971e3a4b
AG
2181 case HCI_OP_READ_LOCAL_EXT_FEATURES:
2182 hci_cc_read_local_ext_features(hdev, skb);
2183 break;
2184
a9de9248
MH
2185 case HCI_OP_READ_BUFFER_SIZE:
2186 hci_cc_read_buffer_size(hdev, skb);
2187 break;
2188
2189 case HCI_OP_READ_BD_ADDR:
2190 hci_cc_read_bd_addr(hdev, skb);
2191 break;
2192
f332ec66
JH
2193 case HCI_OP_READ_PAGE_SCAN_ACTIVITY:
2194 hci_cc_read_page_scan_activity(hdev, skb);
2195 break;
2196
4a3ee763
JH
2197 case HCI_OP_WRITE_PAGE_SCAN_ACTIVITY:
2198 hci_cc_write_page_scan_activity(hdev, skb);
2199 break;
2200
f332ec66
JH
2201 case HCI_OP_READ_PAGE_SCAN_TYPE:
2202 hci_cc_read_page_scan_type(hdev, skb);
2203 break;
2204
4a3ee763
JH
2205 case HCI_OP_WRITE_PAGE_SCAN_TYPE:
2206 hci_cc_write_page_scan_type(hdev, skb);
2207 break;
2208
350ee4cf
AE
2209 case HCI_OP_READ_DATA_BLOCK_SIZE:
2210 hci_cc_read_data_block_size(hdev, skb);
2211 break;
2212
1e89cffb
AE
2213 case HCI_OP_READ_FLOW_CONTROL_MODE:
2214 hci_cc_read_flow_control_mode(hdev, skb);
2215 break;
2216
928abaa7
AE
2217 case HCI_OP_READ_LOCAL_AMP_INFO:
2218 hci_cc_read_local_amp_info(hdev, skb);
2219 break;
2220
903e4541
AE
2221 case HCI_OP_READ_LOCAL_AMP_ASSOC:
2222 hci_cc_read_local_amp_assoc(hdev, skb);
2223 break;
2224
d5859e22
JH
2225 case HCI_OP_READ_INQ_RSP_TX_POWER:
2226 hci_cc_read_inq_rsp_tx_power(hdev, skb);
2227 break;
2228
980e1a53
JH
2229 case HCI_OP_PIN_CODE_REPLY:
2230 hci_cc_pin_code_reply(hdev, skb);
2231 break;
2232
2233 case HCI_OP_PIN_CODE_NEG_REPLY:
2234 hci_cc_pin_code_neg_reply(hdev, skb);
2235 break;
2236
c35938b2
SJ
2237 case HCI_OP_READ_LOCAL_OOB_DATA:
2238 hci_cc_read_local_oob_data_reply(hdev, skb);
2239 break;
2240
6ed58ec5
VT
2241 case HCI_OP_LE_READ_BUFFER_SIZE:
2242 hci_cc_le_read_buffer_size(hdev, skb);
2243 break;
2244
60e77321
JH
2245 case HCI_OP_LE_READ_LOCAL_FEATURES:
2246 hci_cc_le_read_local_features(hdev, skb);
2247 break;
2248
8fa19098
JH
2249 case HCI_OP_LE_READ_ADV_TX_POWER:
2250 hci_cc_le_read_adv_tx_power(hdev, skb);
2251 break;
2252
a5c29683
JH
2253 case HCI_OP_USER_CONFIRM_REPLY:
2254 hci_cc_user_confirm_reply(hdev, skb);
2255 break;
2256
2257 case HCI_OP_USER_CONFIRM_NEG_REPLY:
2258 hci_cc_user_confirm_neg_reply(hdev, skb);
2259 break;
2260
1143d458
BG
2261 case HCI_OP_USER_PASSKEY_REPLY:
2262 hci_cc_user_passkey_reply(hdev, skb);
2263 break;
2264
2265 case HCI_OP_USER_PASSKEY_NEG_REPLY:
2266 hci_cc_user_passkey_neg_reply(hdev, skb);
16cde993 2267 break;
07f7fa5d
AG
2268
2269 case HCI_OP_LE_SET_SCAN_PARAM:
2270 hci_cc_le_set_scan_param(hdev, skb);
1143d458
BG
2271 break;
2272
c1d5dc4a
JH
2273 case HCI_OP_LE_SET_ADV_ENABLE:
2274 hci_cc_le_set_adv_enable(hdev, skb);
2275 break;
2276
eb9d91f5
AG
2277 case HCI_OP_LE_SET_SCAN_ENABLE:
2278 hci_cc_le_set_scan_enable(hdev, skb);
2279 break;
2280
cf1d081f
JH
2281 case HCI_OP_LE_READ_WHITE_LIST_SIZE:
2282 hci_cc_le_read_white_list_size(hdev, skb);
2283 break;
2284
9b008c04
JH
2285 case HCI_OP_LE_READ_SUPPORTED_STATES:
2286 hci_cc_le_read_supported_states(hdev, skb);
2287 break;
2288
f9b49306
AG
2289 case HCI_OP_WRITE_LE_HOST_SUPPORTED:
2290 hci_cc_write_le_host_supported(hdev, skb);
2291 break;
2292
93c284ee
AE
2293 case HCI_OP_WRITE_REMOTE_AMP_ASSOC:
2294 hci_cc_write_remote_amp_assoc(hdev, skb);
2295 break;
2296
a9de9248 2297 default:
9f1db00c 2298 BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode);
a9de9248
MH
2299 break;
2300 }
2301
ad82cdd1 2302 if (opcode != HCI_OP_NOP)
6bd32326
VT
2303 del_timer(&hdev->cmd_timer);
2304
ad82cdd1 2305 hci_req_cmd_complete(hdev, opcode, status);
9238f36a 2306
dbccd791 2307 if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags)) {
a9de9248
MH
2308 atomic_set(&hdev->cmd_cnt, 1);
2309 if (!skb_queue_empty(&hdev->cmd_q))
c347b765 2310 queue_work(hdev->workqueue, &hdev->cmd_work);
a9de9248
MH
2311 }
2312}
2313
6039aa73 2314static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb)
a9de9248
MH
2315{
2316 struct hci_ev_cmd_status *ev = (void *) skb->data;
2317 __u16 opcode;
2318
2319 skb_pull(skb, sizeof(*ev));
2320
2321 opcode = __le16_to_cpu(ev->opcode);
2322
2323 switch (opcode) {
2324 case HCI_OP_INQUIRY:
2325 hci_cs_inquiry(hdev, ev->status);
2326 break;
2327
2328 case HCI_OP_CREATE_CONN:
2329 hci_cs_create_conn(hdev, ev->status);
2330 break;
2331
2332 case HCI_OP_ADD_SCO:
2333 hci_cs_add_sco(hdev, ev->status);
2334 break;
2335
f8558555
MH
2336 case HCI_OP_AUTH_REQUESTED:
2337 hci_cs_auth_requested(hdev, ev->status);
2338 break;
2339
2340 case HCI_OP_SET_CONN_ENCRYPT:
2341 hci_cs_set_conn_encrypt(hdev, ev->status);
2342 break;
2343
a9de9248
MH
2344 case HCI_OP_REMOTE_NAME_REQ:
2345 hci_cs_remote_name_req(hdev, ev->status);
2346 break;
2347
769be974
MH
2348 case HCI_OP_READ_REMOTE_FEATURES:
2349 hci_cs_read_remote_features(hdev, ev->status);
2350 break;
2351
2352 case HCI_OP_READ_REMOTE_EXT_FEATURES:
2353 hci_cs_read_remote_ext_features(hdev, ev->status);
2354 break;
2355
a9de9248
MH
2356 case HCI_OP_SETUP_SYNC_CONN:
2357 hci_cs_setup_sync_conn(hdev, ev->status);
2358 break;
2359
2360 case HCI_OP_SNIFF_MODE:
2361 hci_cs_sniff_mode(hdev, ev->status);
2362 break;
2363
2364 case HCI_OP_EXIT_SNIFF_MODE:
2365 hci_cs_exit_sniff_mode(hdev, ev->status);
2366 break;
2367
8962ee74 2368 case HCI_OP_DISCONNECT:
88c3df13 2369 hci_cs_disconnect(hdev, ev->status);
8962ee74
JH
2370 break;
2371
fcd89c09
VT
2372 case HCI_OP_LE_CREATE_CONN:
2373 hci_cs_le_create_conn(hdev, ev->status);
2374 break;
2375
a02226d6
AE
2376 case HCI_OP_CREATE_PHY_LINK:
2377 hci_cs_create_phylink(hdev, ev->status);
2378 break;
2379
0b26ab9d
AE
2380 case HCI_OP_ACCEPT_PHY_LINK:
2381 hci_cs_accept_phylink(hdev, ev->status);
2382 break;
2383
a9de9248 2384 default:
9f1db00c 2385 BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode);
a9de9248
MH
2386 break;
2387 }
2388
ad82cdd1 2389 if (opcode != HCI_OP_NOP)
6bd32326
VT
2390 del_timer(&hdev->cmd_timer);
2391
02350a72
JH
2392 if (ev->status ||
2393 (hdev->sent_cmd && !bt_cb(hdev->sent_cmd)->req.event))
2394 hci_req_cmd_complete(hdev, opcode, ev->status);
9238f36a 2395
10572132 2396 if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags)) {
a9de9248
MH
2397 atomic_set(&hdev->cmd_cnt, 1);
2398 if (!skb_queue_empty(&hdev->cmd_q))
c347b765 2399 queue_work(hdev->workqueue, &hdev->cmd_work);
a9de9248
MH
2400 }
2401}
2402
6039aa73 2403static void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
a9de9248
MH
2404{
2405 struct hci_ev_role_change *ev = (void *) skb->data;
2406 struct hci_conn *conn;
2407
9f1db00c 2408 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
a9de9248
MH
2409
2410 hci_dev_lock(hdev);
2411
2412 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
2413 if (conn) {
2414 if (!ev->status) {
2415 if (ev->role)
2416 conn->link_mode &= ~HCI_LM_MASTER;
2417 else
2418 conn->link_mode |= HCI_LM_MASTER;
2419 }
2420
51a8efd7 2421 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
a9de9248
MH
2422
2423 hci_role_switch_cfm(conn, ev->status, ev->role);
2424 }
2425
2426 hci_dev_unlock(hdev);
2427}
2428
6039aa73 2429static void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
a9de9248
MH
2430{
2431 struct hci_ev_num_comp_pkts *ev = (void *) skb->data;
a9de9248
MH
2432 int i;
2433
32ac5b9b
AE
2434 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_PACKET_BASED) {
2435 BT_ERR("Wrong event for mode %d", hdev->flow_ctl_mode);
2436 return;
2437 }
2438
c5993de8 2439 if (skb->len < sizeof(*ev) || skb->len < sizeof(*ev) +
807deac2 2440 ev->num_hndl * sizeof(struct hci_comp_pkts_info)) {
a9de9248
MH
2441 BT_DBG("%s bad parameters", hdev->name);
2442 return;
2443 }
2444
c5993de8
AE
2445 BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
2446
613a1c0c
AE
2447 for (i = 0; i < ev->num_hndl; i++) {
2448 struct hci_comp_pkts_info *info = &ev->handles[i];
a9de9248
MH
2449 struct hci_conn *conn;
2450 __u16 handle, count;
2451
613a1c0c
AE
2452 handle = __le16_to_cpu(info->handle);
2453 count = __le16_to_cpu(info->count);
a9de9248
MH
2454
2455 conn = hci_conn_hash_lookup_handle(hdev, handle);
f4280918
AE
2456 if (!conn)
2457 continue;
2458
2459 conn->sent -= count;
2460
2461 switch (conn->type) {
2462 case ACL_LINK:
2463 hdev->acl_cnt += count;
2464 if (hdev->acl_cnt > hdev->acl_pkts)
2465 hdev->acl_cnt = hdev->acl_pkts;
2466 break;
2467
2468 case LE_LINK:
2469 if (hdev->le_pkts) {
2470 hdev->le_cnt += count;
2471 if (hdev->le_cnt > hdev->le_pkts)
2472 hdev->le_cnt = hdev->le_pkts;
2473 } else {
70f23020
AE
2474 hdev->acl_cnt += count;
2475 if (hdev->acl_cnt > hdev->acl_pkts)
a9de9248 2476 hdev->acl_cnt = hdev->acl_pkts;
a9de9248 2477 }
f4280918
AE
2478 break;
2479
2480 case SCO_LINK:
2481 hdev->sco_cnt += count;
2482 if (hdev->sco_cnt > hdev->sco_pkts)
2483 hdev->sco_cnt = hdev->sco_pkts;
2484 break;
2485
2486 default:
2487 BT_ERR("Unknown type %d conn %p", conn->type, conn);
2488 break;
a9de9248
MH
2489 }
2490 }
2491
3eff45ea 2492 queue_work(hdev->workqueue, &hdev->tx_work);
a9de9248
MH
2493}
2494
76ef7cf7
AE
2495static struct hci_conn *__hci_conn_lookup_handle(struct hci_dev *hdev,
2496 __u16 handle)
2497{
2498 struct hci_chan *chan;
2499
2500 switch (hdev->dev_type) {
2501 case HCI_BREDR:
2502 return hci_conn_hash_lookup_handle(hdev, handle);
2503 case HCI_AMP:
2504 chan = hci_chan_lookup_handle(hdev, handle);
2505 if (chan)
2506 return chan->conn;
2507 break;
2508 default:
2509 BT_ERR("%s unknown dev_type %d", hdev->name, hdev->dev_type);
2510 break;
2511 }
2512
2513 return NULL;
2514}
2515
6039aa73 2516static void hci_num_comp_blocks_evt(struct hci_dev *hdev, struct sk_buff *skb)
25e89e99
AE
2517{
2518 struct hci_ev_num_comp_blocks *ev = (void *) skb->data;
2519 int i;
2520
2521 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_BLOCK_BASED) {
2522 BT_ERR("Wrong event for mode %d", hdev->flow_ctl_mode);
2523 return;
2524 }
2525
2526 if (skb->len < sizeof(*ev) || skb->len < sizeof(*ev) +
807deac2 2527 ev->num_hndl * sizeof(struct hci_comp_blocks_info)) {
25e89e99
AE
2528 BT_DBG("%s bad parameters", hdev->name);
2529 return;
2530 }
2531
2532 BT_DBG("%s num_blocks %d num_hndl %d", hdev->name, ev->num_blocks,
807deac2 2533 ev->num_hndl);
25e89e99
AE
2534
2535 for (i = 0; i < ev->num_hndl; i++) {
2536 struct hci_comp_blocks_info *info = &ev->handles[i];
76ef7cf7 2537 struct hci_conn *conn = NULL;
25e89e99
AE
2538 __u16 handle, block_count;
2539
2540 handle = __le16_to_cpu(info->handle);
2541 block_count = __le16_to_cpu(info->blocks);
2542
76ef7cf7 2543 conn = __hci_conn_lookup_handle(hdev, handle);
25e89e99
AE
2544 if (!conn)
2545 continue;
2546
2547 conn->sent -= block_count;
2548
2549 switch (conn->type) {
2550 case ACL_LINK:
bd1eb66b 2551 case AMP_LINK:
25e89e99
AE
2552 hdev->block_cnt += block_count;
2553 if (hdev->block_cnt > hdev->num_blocks)
2554 hdev->block_cnt = hdev->num_blocks;
2555 break;
2556
2557 default:
2558 BT_ERR("Unknown type %d conn %p", conn->type, conn);
2559 break;
2560 }
2561 }
2562
2563 queue_work(hdev->workqueue, &hdev->tx_work);
2564}
2565
6039aa73 2566static void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
04837f64 2567{
a9de9248 2568 struct hci_ev_mode_change *ev = (void *) skb->data;
04837f64
MH
2569 struct hci_conn *conn;
2570
9f1db00c 2571 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
04837f64
MH
2572
2573 hci_dev_lock(hdev);
2574
2575 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
a9de9248
MH
2576 if (conn) {
2577 conn->mode = ev->mode;
2578 conn->interval = __le16_to_cpu(ev->interval);
2579
8fc9ced3
GP
2580 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND,
2581 &conn->flags)) {
a9de9248 2582 if (conn->mode == HCI_CM_ACTIVE)
58a681ef 2583 set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
a9de9248 2584 else
58a681ef 2585 clear_bit(HCI_CONN_POWER_SAVE, &conn->flags);
a9de9248 2586 }
e73439d8 2587
51a8efd7 2588 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
e73439d8 2589 hci_sco_setup(conn, ev->status);
04837f64
MH
2590 }
2591
2592 hci_dev_unlock(hdev);
2593}
2594
6039aa73 2595static void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
a9de9248 2596{
052b30b0
MH
2597 struct hci_ev_pin_code_req *ev = (void *) skb->data;
2598 struct hci_conn *conn;
2599
a9de9248 2600 BT_DBG("%s", hdev->name);
052b30b0
MH
2601
2602 hci_dev_lock(hdev);
2603
2604 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
b6f98044
WR
2605 if (!conn)
2606 goto unlock;
2607
2608 if (conn->state == BT_CONNECTED) {
052b30b0
MH
2609 hci_conn_hold(conn);
2610 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
76a68ba0 2611 hci_conn_drop(conn);
052b30b0
MH
2612 }
2613
a8b2d5c2 2614 if (!test_bit(HCI_PAIRABLE, &hdev->dev_flags))
03b555e1 2615 hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
807deac2 2616 sizeof(ev->bdaddr), &ev->bdaddr);
a8b2d5c2 2617 else if (test_bit(HCI_MGMT, &hdev->dev_flags)) {
a770bb5a
WR
2618 u8 secure;
2619
2620 if (conn->pending_sec_level == BT_SECURITY_HIGH)
2621 secure = 1;
2622 else
2623 secure = 0;
2624
744cf19e 2625 mgmt_pin_code_request(hdev, &ev->bdaddr, secure);
a770bb5a 2626 }
980e1a53 2627
b6f98044 2628unlock:
052b30b0 2629 hci_dev_unlock(hdev);
a9de9248
MH
2630}
2631
6039aa73 2632static void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
a9de9248 2633{
55ed8ca1
JH
2634 struct hci_ev_link_key_req *ev = (void *) skb->data;
2635 struct hci_cp_link_key_reply cp;
2636 struct hci_conn *conn;
2637 struct link_key *key;
2638
a9de9248 2639 BT_DBG("%s", hdev->name);
55ed8ca1 2640
a8b2d5c2 2641 if (!test_bit(HCI_LINK_KEYS, &hdev->dev_flags))
55ed8ca1
JH
2642 return;
2643
2644 hci_dev_lock(hdev);
2645
2646 key = hci_find_link_key(hdev, &ev->bdaddr);
2647 if (!key) {
6ed93dc6
AE
2648 BT_DBG("%s link key not found for %pMR", hdev->name,
2649 &ev->bdaddr);
55ed8ca1
JH
2650 goto not_found;
2651 }
2652
6ed93dc6
AE
2653 BT_DBG("%s found key type %u for %pMR", hdev->name, key->type,
2654 &ev->bdaddr);
55ed8ca1 2655
a8b2d5c2 2656 if (!test_bit(HCI_DEBUG_KEYS, &hdev->dev_flags) &&
807deac2 2657 key->type == HCI_LK_DEBUG_COMBINATION) {
55ed8ca1
JH
2658 BT_DBG("%s ignoring debug key", hdev->name);
2659 goto not_found;
2660 }
2661
2662 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
60b83f57
WR
2663 if (conn) {
2664 if (key->type == HCI_LK_UNAUTH_COMBINATION &&
807deac2 2665 conn->auth_type != 0xff && (conn->auth_type & 0x01)) {
60b83f57
WR
2666 BT_DBG("%s ignoring unauthenticated key", hdev->name);
2667 goto not_found;
2668 }
55ed8ca1 2669
60b83f57 2670 if (key->type == HCI_LK_COMBINATION && key->pin_len < 16 &&
807deac2 2671 conn->pending_sec_level == BT_SECURITY_HIGH) {
8fc9ced3
GP
2672 BT_DBG("%s ignoring key unauthenticated for high security",
2673 hdev->name);
60b83f57
WR
2674 goto not_found;
2675 }
2676
2677 conn->key_type = key->type;
2678 conn->pin_length = key->pin_len;
55ed8ca1
JH
2679 }
2680
2681 bacpy(&cp.bdaddr, &ev->bdaddr);
9b3b4460 2682 memcpy(cp.link_key, key->val, HCI_LINK_KEY_SIZE);
55ed8ca1
JH
2683
2684 hci_send_cmd(hdev, HCI_OP_LINK_KEY_REPLY, sizeof(cp), &cp);
2685
2686 hci_dev_unlock(hdev);
2687
2688 return;
2689
2690not_found:
2691 hci_send_cmd(hdev, HCI_OP_LINK_KEY_NEG_REPLY, 6, &ev->bdaddr);
2692 hci_dev_unlock(hdev);
a9de9248
MH
2693}
2694
6039aa73 2695static void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
a9de9248 2696{
052b30b0
MH
2697 struct hci_ev_link_key_notify *ev = (void *) skb->data;
2698 struct hci_conn *conn;
55ed8ca1 2699 u8 pin_len = 0;
052b30b0 2700
a9de9248 2701 BT_DBG("%s", hdev->name);
052b30b0
MH
2702
2703 hci_dev_lock(hdev);
2704
2705 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
2706 if (conn) {
2707 hci_conn_hold(conn);
2708 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
980e1a53 2709 pin_len = conn->pin_length;
13d39315
WR
2710
2711 if (ev->key_type != HCI_LK_CHANGED_COMBINATION)
2712 conn->key_type = ev->key_type;
2713
76a68ba0 2714 hci_conn_drop(conn);
052b30b0
MH
2715 }
2716
a8b2d5c2 2717 if (test_bit(HCI_LINK_KEYS, &hdev->dev_flags))
d25e28ab 2718 hci_add_link_key(hdev, conn, 1, &ev->bdaddr, ev->link_key,
807deac2 2719 ev->key_type, pin_len);
55ed8ca1 2720
052b30b0 2721 hci_dev_unlock(hdev);
a9de9248
MH
2722}
2723
6039aa73 2724static void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
1da177e4 2725{
a9de9248 2726 struct hci_ev_clock_offset *ev = (void *) skb->data;
04837f64 2727 struct hci_conn *conn;
1da177e4 2728
9f1db00c 2729 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
1da177e4
LT
2730
2731 hci_dev_lock(hdev);
2732
04837f64 2733 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1da177e4
LT
2734 if (conn && !ev->status) {
2735 struct inquiry_entry *ie;
2736
cc11b9c1
AE
2737 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
2738 if (ie) {
1da177e4
LT
2739 ie->data.clock_offset = ev->clock_offset;
2740 ie->timestamp = jiffies;
2741 }
2742 }
2743
2744 hci_dev_unlock(hdev);
2745}
2746
6039aa73 2747static void hci_pkt_type_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
a8746417
MH
2748{
2749 struct hci_ev_pkt_type_change *ev = (void *) skb->data;
2750 struct hci_conn *conn;
2751
9f1db00c 2752 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
a8746417
MH
2753
2754 hci_dev_lock(hdev);
2755
2756 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
2757 if (conn && !ev->status)
2758 conn->pkt_type = __le16_to_cpu(ev->pkt_type);
2759
2760 hci_dev_unlock(hdev);
2761}
2762
6039aa73 2763static void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
85a1e930 2764{
a9de9248 2765 struct hci_ev_pscan_rep_mode *ev = (void *) skb->data;
85a1e930
MH
2766 struct inquiry_entry *ie;
2767
2768 BT_DBG("%s", hdev->name);
2769
2770 hci_dev_lock(hdev);
2771
cc11b9c1
AE
2772 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
2773 if (ie) {
85a1e930
MH
2774 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
2775 ie->timestamp = jiffies;
2776 }
2777
2778 hci_dev_unlock(hdev);
2779}
2780
6039aa73
GP
2781static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev,
2782 struct sk_buff *skb)
a9de9248
MH
2783{
2784 struct inquiry_data data;
2785 int num_rsp = *((__u8 *) skb->data);
388fc8fa 2786 bool name_known, ssp;
a9de9248
MH
2787
2788 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
2789
2790 if (!num_rsp)
2791 return;
2792
1519cc17
AG
2793 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags))
2794 return;
2795
a9de9248
MH
2796 hci_dev_lock(hdev);
2797
2798 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
138d22ef
SJ
2799 struct inquiry_info_with_rssi_and_pscan_mode *info;
2800 info = (void *) (skb->data + 1);
a9de9248 2801
e17acd40 2802 for (; num_rsp; num_rsp--, info++) {
a9de9248
MH
2803 bacpy(&data.bdaddr, &info->bdaddr);
2804 data.pscan_rep_mode = info->pscan_rep_mode;
2805 data.pscan_period_mode = info->pscan_period_mode;
2806 data.pscan_mode = info->pscan_mode;
2807 memcpy(data.dev_class, info->dev_class, 3);
2808 data.clock_offset = info->clock_offset;
2809 data.rssi = info->rssi;
41a96212 2810 data.ssp_mode = 0x00;
3175405b
JH
2811
2812 name_known = hci_inquiry_cache_update(hdev, &data,
04124681 2813 false, &ssp);
48264f06 2814 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
04124681
GP
2815 info->dev_class, info->rssi,
2816 !name_known, ssp, NULL, 0);
a9de9248
MH
2817 }
2818 } else {
2819 struct inquiry_info_with_rssi *info = (void *) (skb->data + 1);
2820
e17acd40 2821 for (; num_rsp; num_rsp--, info++) {
a9de9248
MH
2822 bacpy(&data.bdaddr, &info->bdaddr);
2823 data.pscan_rep_mode = info->pscan_rep_mode;
2824 data.pscan_period_mode = info->pscan_period_mode;
2825 data.pscan_mode = 0x00;
2826 memcpy(data.dev_class, info->dev_class, 3);
2827 data.clock_offset = info->clock_offset;
2828 data.rssi = info->rssi;
41a96212 2829 data.ssp_mode = 0x00;
3175405b 2830 name_known = hci_inquiry_cache_update(hdev, &data,
04124681 2831 false, &ssp);
48264f06 2832 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
04124681
GP
2833 info->dev_class, info->rssi,
2834 !name_known, ssp, NULL, 0);
a9de9248
MH
2835 }
2836 }
2837
2838 hci_dev_unlock(hdev);
2839}
2840
6039aa73
GP
2841static void hci_remote_ext_features_evt(struct hci_dev *hdev,
2842 struct sk_buff *skb)
a9de9248 2843{
41a96212
MH
2844 struct hci_ev_remote_ext_features *ev = (void *) skb->data;
2845 struct hci_conn *conn;
2846
a9de9248 2847 BT_DBG("%s", hdev->name);
41a96212 2848
41a96212
MH
2849 hci_dev_lock(hdev);
2850
2851 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
ccd556fe
JH
2852 if (!conn)
2853 goto unlock;
41a96212 2854
cad718ed
JH
2855 if (ev->page < HCI_MAX_PAGES)
2856 memcpy(conn->features[ev->page], ev->features, 8);
2857
ccd556fe
JH
2858 if (!ev->status && ev->page == 0x01) {
2859 struct inquiry_entry *ie;
41a96212 2860
cc11b9c1
AE
2861 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
2862 if (ie)
02b7cc62 2863 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
769be974 2864
bbb0eada 2865 if (ev->features[0] & LMP_HOST_SSP) {
58a681ef 2866 set_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
bbb0eada
JK
2867 } else {
2868 /* It is mandatory by the Bluetooth specification that
2869 * Extended Inquiry Results are only used when Secure
2870 * Simple Pairing is enabled, but some devices violate
2871 * this.
2872 *
2873 * To make these devices work, the internal SSP
2874 * enabled flag needs to be cleared if the remote host
2875 * features do not indicate SSP support */
2876 clear_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
2877 }
ccd556fe
JH
2878 }
2879
2880 if (conn->state != BT_CONFIG)
2881 goto unlock;
2882
671267bf 2883 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
127178d2
JH
2884 struct hci_cp_remote_name_req cp;
2885 memset(&cp, 0, sizeof(cp));
2886 bacpy(&cp.bdaddr, &conn->dst);
2887 cp.pscan_rep_mode = 0x02;
2888 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
b644ba33
JH
2889 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
2890 mgmt_device_connected(hdev, &conn->dst, conn->type,
04124681
GP
2891 conn->dst_type, 0, NULL, 0,
2892 conn->dev_class);
392599b9 2893
127178d2 2894 if (!hci_outgoing_auth_needed(hdev, conn)) {
ccd556fe
JH
2895 conn->state = BT_CONNECTED;
2896 hci_proto_connect_cfm(conn, ev->status);
76a68ba0 2897 hci_conn_drop(conn);
41a96212
MH
2898 }
2899
ccd556fe 2900unlock:
41a96212 2901 hci_dev_unlock(hdev);
a9de9248
MH
2902}
2903
6039aa73
GP
2904static void hci_sync_conn_complete_evt(struct hci_dev *hdev,
2905 struct sk_buff *skb)
a9de9248 2906{
b6a0dc82
MH
2907 struct hci_ev_sync_conn_complete *ev = (void *) skb->data;
2908 struct hci_conn *conn;
2909
9f1db00c 2910 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
b6a0dc82
MH
2911
2912 hci_dev_lock(hdev);
2913
2914 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
9dc0a3af
MH
2915 if (!conn) {
2916 if (ev->link_type == ESCO_LINK)
2917 goto unlock;
2918
2919 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
2920 if (!conn)
2921 goto unlock;
2922
2923 conn->type = SCO_LINK;
2924 }
b6a0dc82 2925
732547f9
MH
2926 switch (ev->status) {
2927 case 0x00:
b6a0dc82
MH
2928 conn->handle = __le16_to_cpu(ev->handle);
2929 conn->state = BT_CONNECTED;
7d0db0a3
MH
2930
2931 hci_conn_add_sysfs(conn);
732547f9
MH
2932 break;
2933
705e5711 2934 case 0x11: /* Unsupported Feature or Parameter Value */
732547f9 2935 case 0x1c: /* SCO interval rejected */
1038a00b 2936 case 0x1a: /* Unsupported Remote Feature */
732547f9
MH
2937 case 0x1f: /* Unspecified error */
2938 if (conn->out && conn->attempt < 2) {
2939 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
2940 (hdev->esco_type & EDR_ESCO_MASK);
2941 hci_setup_sync(conn, conn->link->handle);
2942 goto unlock;
2943 }
2944 /* fall through */
2945
2946 default:
b6a0dc82 2947 conn->state = BT_CLOSED;
732547f9
MH
2948 break;
2949 }
b6a0dc82
MH
2950
2951 hci_proto_connect_cfm(conn, ev->status);
2952 if (ev->status)
2953 hci_conn_del(conn);
2954
2955unlock:
2956 hci_dev_unlock(hdev);
a9de9248
MH
2957}
2958
6039aa73
GP
2959static void hci_extended_inquiry_result_evt(struct hci_dev *hdev,
2960 struct sk_buff *skb)
1da177e4 2961{
a9de9248
MH
2962 struct inquiry_data data;
2963 struct extended_inquiry_info *info = (void *) (skb->data + 1);
2964 int num_rsp = *((__u8 *) skb->data);
9d939d94 2965 size_t eir_len;
1da177e4 2966
a9de9248 2967 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
1da177e4 2968
a9de9248
MH
2969 if (!num_rsp)
2970 return;
1da177e4 2971
1519cc17
AG
2972 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags))
2973 return;
2974
a9de9248
MH
2975 hci_dev_lock(hdev);
2976
e17acd40 2977 for (; num_rsp; num_rsp--, info++) {
388fc8fa 2978 bool name_known, ssp;
561aafbc 2979
a9de9248 2980 bacpy(&data.bdaddr, &info->bdaddr);
138d22ef
SJ
2981 data.pscan_rep_mode = info->pscan_rep_mode;
2982 data.pscan_period_mode = info->pscan_period_mode;
2983 data.pscan_mode = 0x00;
a9de9248 2984 memcpy(data.dev_class, info->dev_class, 3);
138d22ef
SJ
2985 data.clock_offset = info->clock_offset;
2986 data.rssi = info->rssi;
41a96212 2987 data.ssp_mode = 0x01;
561aafbc 2988
a8b2d5c2 2989 if (test_bit(HCI_MGMT, &hdev->dev_flags))
4ddb1930 2990 name_known = eir_has_data_type(info->data,
04124681
GP
2991 sizeof(info->data),
2992 EIR_NAME_COMPLETE);
561aafbc
JH
2993 else
2994 name_known = true;
2995
388fc8fa 2996 name_known = hci_inquiry_cache_update(hdev, &data, name_known,
04124681 2997 &ssp);
9d939d94 2998 eir_len = eir_get_length(info->data, sizeof(info->data));
48264f06 2999 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
04124681 3000 info->dev_class, info->rssi, !name_known,
9d939d94 3001 ssp, info->data, eir_len);
a9de9248
MH
3002 }
3003
3004 hci_dev_unlock(hdev);
3005}
1da177e4 3006
1c2e0041
JH
3007static void hci_key_refresh_complete_evt(struct hci_dev *hdev,
3008 struct sk_buff *skb)
3009{
3010 struct hci_ev_key_refresh_complete *ev = (void *) skb->data;
3011 struct hci_conn *conn;
3012
9f1db00c 3013 BT_DBG("%s status 0x%2.2x handle 0x%4.4x", hdev->name, ev->status,
1c2e0041
JH
3014 __le16_to_cpu(ev->handle));
3015
3016 hci_dev_lock(hdev);
3017
3018 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3019 if (!conn)
3020 goto unlock;
3021
3022 if (!ev->status)
3023 conn->sec_level = conn->pending_sec_level;
3024
3025 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
3026
3027 if (ev->status && conn->state == BT_CONNECTED) {
bed71748 3028 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
76a68ba0 3029 hci_conn_drop(conn);
1c2e0041
JH
3030 goto unlock;
3031 }
3032
3033 if (conn->state == BT_CONFIG) {
3034 if (!ev->status)
3035 conn->state = BT_CONNECTED;
3036
3037 hci_proto_connect_cfm(conn, ev->status);
76a68ba0 3038 hci_conn_drop(conn);
1c2e0041
JH
3039 } else {
3040 hci_auth_cfm(conn, ev->status);
3041
3042 hci_conn_hold(conn);
3043 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
76a68ba0 3044 hci_conn_drop(conn);
1c2e0041
JH
3045 }
3046
3047unlock:
3048 hci_dev_unlock(hdev);
3049}
3050
6039aa73 3051static u8 hci_get_auth_req(struct hci_conn *conn)
17fa4b9d
JH
3052{
3053 /* If remote requests dedicated bonding follow that lead */
3054 if (conn->remote_auth == 0x02 || conn->remote_auth == 0x03) {
3055 /* If both remote and local IO capabilities allow MITM
3056 * protection then require it, otherwise don't */
3057 if (conn->remote_cap == 0x03 || conn->io_capability == 0x03)
3058 return 0x02;
3059 else
3060 return 0x03;
3061 }
3062
3063 /* If remote requests no-bonding follow that lead */
3064 if (conn->remote_auth == 0x00 || conn->remote_auth == 0x01)
58797bf7 3065 return conn->remote_auth | (conn->auth_type & 0x01);
17fa4b9d
JH
3066
3067 return conn->auth_type;
3068}
3069
6039aa73 3070static void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
0493684e
MH
3071{
3072 struct hci_ev_io_capa_request *ev = (void *) skb->data;
3073 struct hci_conn *conn;
3074
3075 BT_DBG("%s", hdev->name);
3076
3077 hci_dev_lock(hdev);
3078
3079 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
03b555e1
JH
3080 if (!conn)
3081 goto unlock;
3082
3083 hci_conn_hold(conn);
3084
a8b2d5c2 3085 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
03b555e1
JH
3086 goto unlock;
3087
a8b2d5c2 3088 if (test_bit(HCI_PAIRABLE, &hdev->dev_flags) ||
807deac2 3089 (conn->remote_auth & ~0x01) == HCI_AT_NO_BONDING) {
17fa4b9d
JH
3090 struct hci_cp_io_capability_reply cp;
3091
3092 bacpy(&cp.bdaddr, &ev->bdaddr);
7a7f1e7c
HG
3093 /* Change the IO capability from KeyboardDisplay
3094 * to DisplayYesNo as it is not supported by BT spec. */
3095 cp.capability = (conn->io_capability == 0x04) ?
3096 0x01 : conn->io_capability;
7cbc9bd9
JH
3097 conn->auth_type = hci_get_auth_req(conn);
3098 cp.authentication = conn->auth_type;
17fa4b9d 3099
8fc9ced3
GP
3100 if (hci_find_remote_oob_data(hdev, &conn->dst) &&
3101 (conn->out || test_bit(HCI_CONN_REMOTE_OOB, &conn->flags)))
ce85ee13
SJ
3102 cp.oob_data = 0x01;
3103 else
3104 cp.oob_data = 0x00;
3105
17fa4b9d 3106 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_REPLY,
807deac2 3107 sizeof(cp), &cp);
03b555e1
JH
3108 } else {
3109 struct hci_cp_io_capability_neg_reply cp;
3110
3111 bacpy(&cp.bdaddr, &ev->bdaddr);
9f5a0d7b 3112 cp.reason = HCI_ERROR_PAIRING_NOT_ALLOWED;
0493684e 3113
03b555e1 3114 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_NEG_REPLY,
807deac2 3115 sizeof(cp), &cp);
03b555e1
JH
3116 }
3117
3118unlock:
3119 hci_dev_unlock(hdev);
3120}
3121
6039aa73 3122static void hci_io_capa_reply_evt(struct hci_dev *hdev, struct sk_buff *skb)
03b555e1
JH
3123{
3124 struct hci_ev_io_capa_reply *ev = (void *) skb->data;
3125 struct hci_conn *conn;
3126
3127 BT_DBG("%s", hdev->name);
3128
3129 hci_dev_lock(hdev);
3130
3131 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3132 if (!conn)
3133 goto unlock;
3134
03b555e1 3135 conn->remote_cap = ev->capability;
03b555e1 3136 conn->remote_auth = ev->authentication;
58a681ef
JH
3137 if (ev->oob_data)
3138 set_bit(HCI_CONN_REMOTE_OOB, &conn->flags);
03b555e1
JH
3139
3140unlock:
0493684e
MH
3141 hci_dev_unlock(hdev);
3142}
3143
6039aa73
GP
3144static void hci_user_confirm_request_evt(struct hci_dev *hdev,
3145 struct sk_buff *skb)
a5c29683
JH
3146{
3147 struct hci_ev_user_confirm_req *ev = (void *) skb->data;
55bc1a37 3148 int loc_mitm, rem_mitm, confirm_hint = 0;
7a828908 3149 struct hci_conn *conn;
a5c29683
JH
3150
3151 BT_DBG("%s", hdev->name);
3152
3153 hci_dev_lock(hdev);
3154
a8b2d5c2 3155 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
7a828908 3156 goto unlock;
a5c29683 3157
7a828908
JH
3158 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3159 if (!conn)
3160 goto unlock;
3161
3162 loc_mitm = (conn->auth_type & 0x01);
3163 rem_mitm = (conn->remote_auth & 0x01);
3164
3165 /* If we require MITM but the remote device can't provide that
3166 * (it has NoInputNoOutput) then reject the confirmation
3167 * request. The only exception is when we're dedicated bonding
3168 * initiators (connect_cfm_cb set) since then we always have the MITM
3169 * bit set. */
3170 if (!conn->connect_cfm_cb && loc_mitm && conn->remote_cap == 0x03) {
3171 BT_DBG("Rejecting request: remote device can't provide MITM");
3172 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_NEG_REPLY,
807deac2 3173 sizeof(ev->bdaddr), &ev->bdaddr);
7a828908
JH
3174 goto unlock;
3175 }
3176
3177 /* If no side requires MITM protection; auto-accept */
3178 if ((!loc_mitm || conn->remote_cap == 0x03) &&
807deac2 3179 (!rem_mitm || conn->io_capability == 0x03)) {
55bc1a37
JH
3180
3181 /* If we're not the initiators request authorization to
3182 * proceed from user space (mgmt_user_confirm with
3183 * confirm_hint set to 1). */
51a8efd7 3184 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
55bc1a37
JH
3185 BT_DBG("Confirming auto-accept as acceptor");
3186 confirm_hint = 1;
3187 goto confirm;
3188 }
3189
9f61656a 3190 BT_DBG("Auto-accept of user confirmation with %ums delay",
807deac2 3191 hdev->auto_accept_delay);
9f61656a
JH
3192
3193 if (hdev->auto_accept_delay > 0) {
3194 int delay = msecs_to_jiffies(hdev->auto_accept_delay);
3195 mod_timer(&conn->auto_accept_timer, jiffies + delay);
3196 goto unlock;
3197 }
3198
7a828908 3199 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY,
807deac2 3200 sizeof(ev->bdaddr), &ev->bdaddr);
7a828908
JH
3201 goto unlock;
3202 }
3203
55bc1a37 3204confirm:
272d90df 3205 mgmt_user_confirm_request(hdev, &ev->bdaddr, ACL_LINK, 0, ev->passkey,
04124681 3206 confirm_hint);
7a828908
JH
3207
3208unlock:
a5c29683
JH
3209 hci_dev_unlock(hdev);
3210}
3211
6039aa73
GP
3212static void hci_user_passkey_request_evt(struct hci_dev *hdev,
3213 struct sk_buff *skb)
1143d458
BG
3214{
3215 struct hci_ev_user_passkey_req *ev = (void *) skb->data;
3216
3217 BT_DBG("%s", hdev->name);
3218
a8b2d5c2 3219 if (test_bit(HCI_MGMT, &hdev->dev_flags))
272d90df 3220 mgmt_user_passkey_request(hdev, &ev->bdaddr, ACL_LINK, 0);
1143d458
BG
3221}
3222
92a25256
JH
3223static void hci_user_passkey_notify_evt(struct hci_dev *hdev,
3224 struct sk_buff *skb)
3225{
3226 struct hci_ev_user_passkey_notify *ev = (void *) skb->data;
3227 struct hci_conn *conn;
3228
3229 BT_DBG("%s", hdev->name);
3230
3231 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3232 if (!conn)
3233 return;
3234
3235 conn->passkey_notify = __le32_to_cpu(ev->passkey);
3236 conn->passkey_entered = 0;
3237
3238 if (test_bit(HCI_MGMT, &hdev->dev_flags))
3239 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
3240 conn->dst_type, conn->passkey_notify,
3241 conn->passkey_entered);
3242}
3243
3244static void hci_keypress_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
3245{
3246 struct hci_ev_keypress_notify *ev = (void *) skb->data;
3247 struct hci_conn *conn;
3248
3249 BT_DBG("%s", hdev->name);
3250
3251 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3252 if (!conn)
3253 return;
3254
3255 switch (ev->type) {
3256 case HCI_KEYPRESS_STARTED:
3257 conn->passkey_entered = 0;
3258 return;
3259
3260 case HCI_KEYPRESS_ENTERED:
3261 conn->passkey_entered++;
3262 break;
3263
3264 case HCI_KEYPRESS_ERASED:
3265 conn->passkey_entered--;
3266 break;
3267
3268 case HCI_KEYPRESS_CLEARED:
3269 conn->passkey_entered = 0;
3270 break;
3271
3272 case HCI_KEYPRESS_COMPLETED:
3273 return;
3274 }
3275
3276 if (test_bit(HCI_MGMT, &hdev->dev_flags))
3277 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
3278 conn->dst_type, conn->passkey_notify,
3279 conn->passkey_entered);
3280}
3281
6039aa73
GP
3282static void hci_simple_pair_complete_evt(struct hci_dev *hdev,
3283 struct sk_buff *skb)
0493684e
MH
3284{
3285 struct hci_ev_simple_pair_complete *ev = (void *) skb->data;
3286 struct hci_conn *conn;
3287
3288 BT_DBG("%s", hdev->name);
3289
3290 hci_dev_lock(hdev);
3291
3292 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
2a611692
JH
3293 if (!conn)
3294 goto unlock;
3295
3296 /* To avoid duplicate auth_failed events to user space we check
3297 * the HCI_CONN_AUTH_PEND flag which will be set if we
3298 * initiated the authentication. A traditional auth_complete
3299 * event gets always produced as initiator and is also mapped to
3300 * the mgmt_auth_failed event */
fa1bd918 3301 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) && ev->status)
bab73cb6 3302 mgmt_auth_failed(hdev, &conn->dst, conn->type, conn->dst_type,
04124681 3303 ev->status);
0493684e 3304
76a68ba0 3305 hci_conn_drop(conn);
2a611692
JH
3306
3307unlock:
0493684e
MH
3308 hci_dev_unlock(hdev);
3309}
3310
6039aa73
GP
3311static void hci_remote_host_features_evt(struct hci_dev *hdev,
3312 struct sk_buff *skb)
41a96212
MH
3313{
3314 struct hci_ev_remote_host_features *ev = (void *) skb->data;
3315 struct inquiry_entry *ie;
cad718ed 3316 struct hci_conn *conn;
41a96212
MH
3317
3318 BT_DBG("%s", hdev->name);
3319
3320 hci_dev_lock(hdev);
3321
cad718ed
JH
3322 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3323 if (conn)
3324 memcpy(conn->features[1], ev->features, 8);
3325
cc11b9c1
AE
3326 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
3327 if (ie)
02b7cc62 3328 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
41a96212
MH
3329
3330 hci_dev_unlock(hdev);
3331}
3332
6039aa73
GP
3333static void hci_remote_oob_data_request_evt(struct hci_dev *hdev,
3334 struct sk_buff *skb)
2763eda6
SJ
3335{
3336 struct hci_ev_remote_oob_data_request *ev = (void *) skb->data;
3337 struct oob_data *data;
3338
3339 BT_DBG("%s", hdev->name);
3340
3341 hci_dev_lock(hdev);
3342
a8b2d5c2 3343 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
e1ba1f15
SJ
3344 goto unlock;
3345
2763eda6
SJ
3346 data = hci_find_remote_oob_data(hdev, &ev->bdaddr);
3347 if (data) {
3348 struct hci_cp_remote_oob_data_reply cp;
3349
3350 bacpy(&cp.bdaddr, &ev->bdaddr);
3351 memcpy(cp.hash, data->hash, sizeof(cp.hash));
3352 memcpy(cp.randomizer, data->randomizer, sizeof(cp.randomizer));
3353
3354 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_REPLY, sizeof(cp),
807deac2 3355 &cp);
2763eda6
SJ
3356 } else {
3357 struct hci_cp_remote_oob_data_neg_reply cp;
3358
3359 bacpy(&cp.bdaddr, &ev->bdaddr);
3360 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_NEG_REPLY, sizeof(cp),
807deac2 3361 &cp);
2763eda6
SJ
3362 }
3363
e1ba1f15 3364unlock:
2763eda6
SJ
3365 hci_dev_unlock(hdev);
3366}
3367
d5e91192
AE
3368static void hci_phy_link_complete_evt(struct hci_dev *hdev,
3369 struct sk_buff *skb)
3370{
3371 struct hci_ev_phy_link_complete *ev = (void *) skb->data;
3372 struct hci_conn *hcon, *bredr_hcon;
3373
3374 BT_DBG("%s handle 0x%2.2x status 0x%2.2x", hdev->name, ev->phy_handle,
3375 ev->status);
3376
3377 hci_dev_lock(hdev);
3378
3379 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
3380 if (!hcon) {
3381 hci_dev_unlock(hdev);
3382 return;
3383 }
3384
3385 if (ev->status) {
3386 hci_conn_del(hcon);
3387 hci_dev_unlock(hdev);
3388 return;
3389 }
3390
3391 bredr_hcon = hcon->amp_mgr->l2cap_conn->hcon;
3392
3393 hcon->state = BT_CONNECTED;
3394 bacpy(&hcon->dst, &bredr_hcon->dst);
3395
3396 hci_conn_hold(hcon);
3397 hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
76a68ba0 3398 hci_conn_drop(hcon);
d5e91192 3399
d5e91192
AE
3400 hci_conn_add_sysfs(hcon);
3401
cf70ff22 3402 amp_physical_cfm(bredr_hcon, hcon);
d5e91192 3403
cf70ff22 3404 hci_dev_unlock(hdev);
d5e91192
AE
3405}
3406
27695fb4
AE
3407static void hci_loglink_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
3408{
3409 struct hci_ev_logical_link_complete *ev = (void *) skb->data;
3410 struct hci_conn *hcon;
3411 struct hci_chan *hchan;
3412 struct amp_mgr *mgr;
3413
3414 BT_DBG("%s log_handle 0x%4.4x phy_handle 0x%2.2x status 0x%2.2x",
3415 hdev->name, le16_to_cpu(ev->handle), ev->phy_handle,
3416 ev->status);
3417
3418 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
3419 if (!hcon)
3420 return;
3421
3422 /* Create AMP hchan */
3423 hchan = hci_chan_create(hcon);
3424 if (!hchan)
3425 return;
3426
3427 hchan->handle = le16_to_cpu(ev->handle);
3428
3429 BT_DBG("hcon %p mgr %p hchan %p", hcon, hcon->amp_mgr, hchan);
3430
3431 mgr = hcon->amp_mgr;
3432 if (mgr && mgr->bredr_chan) {
3433 struct l2cap_chan *bredr_chan = mgr->bredr_chan;
3434
3435 l2cap_chan_lock(bredr_chan);
3436
3437 bredr_chan->conn->mtu = hdev->block_mtu;
3438 l2cap_logical_cfm(bredr_chan, hchan, 0);
3439 hci_conn_hold(hcon);
3440
3441 l2cap_chan_unlock(bredr_chan);
3442 }
3443}
3444
606e2a10
AE
3445static void hci_disconn_loglink_complete_evt(struct hci_dev *hdev,
3446 struct sk_buff *skb)
3447{
3448 struct hci_ev_disconn_logical_link_complete *ev = (void *) skb->data;
3449 struct hci_chan *hchan;
3450
3451 BT_DBG("%s log handle 0x%4.4x status 0x%2.2x", hdev->name,
3452 le16_to_cpu(ev->handle), ev->status);
3453
3454 if (ev->status)
3455 return;
3456
3457 hci_dev_lock(hdev);
3458
3459 hchan = hci_chan_lookup_handle(hdev, le16_to_cpu(ev->handle));
3460 if (!hchan)
3461 goto unlock;
3462
3463 amp_destroy_logical_link(hchan, ev->reason);
3464
3465unlock:
3466 hci_dev_unlock(hdev);
3467}
3468
9eef6b3a
AE
3469static void hci_disconn_phylink_complete_evt(struct hci_dev *hdev,
3470 struct sk_buff *skb)
3471{
3472 struct hci_ev_disconn_phy_link_complete *ev = (void *) skb->data;
3473 struct hci_conn *hcon;
3474
3475 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
3476
3477 if (ev->status)
3478 return;
3479
3480 hci_dev_lock(hdev);
3481
3482 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
3483 if (hcon) {
3484 hcon->state = BT_CLOSED;
3485 hci_conn_del(hcon);
3486 }
3487
3488 hci_dev_unlock(hdev);
3489}
3490
6039aa73 3491static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
fcd89c09
VT
3492{
3493 struct hci_ev_le_conn_complete *ev = (void *) skb->data;
3494 struct hci_conn *conn;
3495
9f1db00c 3496 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
fcd89c09
VT
3497
3498 hci_dev_lock(hdev);
3499
b47a09b3 3500 conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
b62f328b
VT
3501 if (!conn) {
3502 conn = hci_conn_add(hdev, LE_LINK, &ev->bdaddr);
3503 if (!conn) {
3504 BT_ERR("No memory for new connection");
230fd16a 3505 goto unlock;
b62f328b 3506 }
29b7988a
AG
3507
3508 conn->dst_type = ev->bdaddr_type;
b9b343d2
AG
3509
3510 if (ev->role == LE_CONN_ROLE_MASTER) {
3511 conn->out = true;
3512 conn->link_mode |= HCI_LM_MASTER;
3513 }
b62f328b 3514 }
fcd89c09 3515
cd17decb
AG
3516 if (ev->status) {
3517 mgmt_connect_failed(hdev, &conn->dst, conn->type,
3518 conn->dst_type, ev->status);
3519 hci_proto_connect_cfm(conn, ev->status);
3520 conn->state = BT_CLOSED;
3521 hci_conn_del(conn);
3522 goto unlock;
3523 }
3524
b644ba33
JH
3525 if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
3526 mgmt_device_connected(hdev, &ev->bdaddr, conn->type,
04124681 3527 conn->dst_type, 0, NULL, 0, NULL);
83bc71b4 3528
7b5c0d52 3529 conn->sec_level = BT_SECURITY_LOW;
fcd89c09
VT
3530 conn->handle = __le16_to_cpu(ev->handle);
3531 conn->state = BT_CONNECTED;
3532
fcd89c09
VT
3533 hci_conn_add_sysfs(conn);
3534
3535 hci_proto_connect_cfm(conn, ev->status);
3536
3537unlock:
3538 hci_dev_unlock(hdev);
3539}
3540
6039aa73 3541static void hci_le_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)
9aa04c91 3542{
e95beb41
AG
3543 u8 num_reports = skb->data[0];
3544 void *ptr = &skb->data[1];
3c9e9195 3545 s8 rssi;
9aa04c91 3546
e95beb41
AG
3547 while (num_reports--) {
3548 struct hci_ev_le_advertising_info *ev = ptr;
9aa04c91 3549
3c9e9195
AG
3550 rssi = ev->data[ev->length];
3551 mgmt_device_found(hdev, &ev->bdaddr, LE_LINK, ev->bdaddr_type,
04124681 3552 NULL, rssi, 0, 1, ev->data, ev->length);
3c9e9195 3553
e95beb41 3554 ptr += sizeof(*ev) + ev->length + 1;
9aa04c91 3555 }
9aa04c91
AG
3556}
3557
6039aa73 3558static void hci_le_ltk_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
a7a595f6
VCG
3559{
3560 struct hci_ev_le_ltk_req *ev = (void *) skb->data;
3561 struct hci_cp_le_ltk_reply cp;
bea710fe 3562 struct hci_cp_le_ltk_neg_reply neg;
a7a595f6 3563 struct hci_conn *conn;
c9839a11 3564 struct smp_ltk *ltk;
a7a595f6 3565
9f1db00c 3566 BT_DBG("%s handle 0x%4.4x", hdev->name, __le16_to_cpu(ev->handle));
a7a595f6
VCG
3567
3568 hci_dev_lock(hdev);
3569
3570 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
bea710fe
VCG
3571 if (conn == NULL)
3572 goto not_found;
a7a595f6 3573
bea710fe
VCG
3574 ltk = hci_find_ltk(hdev, ev->ediv, ev->random);
3575 if (ltk == NULL)
3576 goto not_found;
3577
3578 memcpy(cp.ltk, ltk->val, sizeof(ltk->val));
a7a595f6 3579 cp.handle = cpu_to_le16(conn->handle);
c9839a11
VCG
3580
3581 if (ltk->authenticated)
3582 conn->sec_level = BT_SECURITY_HIGH;
a7a595f6
VCG
3583
3584 hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
3585
c9839a11
VCG
3586 if (ltk->type & HCI_SMP_STK) {
3587 list_del(&ltk->list);
3588 kfree(ltk);
3589 }
3590
a7a595f6 3591 hci_dev_unlock(hdev);
bea710fe
VCG
3592
3593 return;
3594
3595not_found:
3596 neg.handle = ev->handle;
3597 hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(neg), &neg);
3598 hci_dev_unlock(hdev);
a7a595f6
VCG
3599}
3600
6039aa73 3601static void hci_le_meta_evt(struct hci_dev *hdev, struct sk_buff *skb)
fcd89c09
VT
3602{
3603 struct hci_ev_le_meta *le_ev = (void *) skb->data;
3604
3605 skb_pull(skb, sizeof(*le_ev));
3606
3607 switch (le_ev->subevent) {
3608 case HCI_EV_LE_CONN_COMPLETE:
3609 hci_le_conn_complete_evt(hdev, skb);
3610 break;
3611
9aa04c91
AG
3612 case HCI_EV_LE_ADVERTISING_REPORT:
3613 hci_le_adv_report_evt(hdev, skb);
3614 break;
3615
a7a595f6
VCG
3616 case HCI_EV_LE_LTK_REQ:
3617 hci_le_ltk_request_evt(hdev, skb);
3618 break;
3619
fcd89c09
VT
3620 default:
3621 break;
3622 }
3623}
3624
9495b2ee
AE
3625static void hci_chan_selected_evt(struct hci_dev *hdev, struct sk_buff *skb)
3626{
3627 struct hci_ev_channel_selected *ev = (void *) skb->data;
3628 struct hci_conn *hcon;
3629
3630 BT_DBG("%s handle 0x%2.2x", hdev->name, ev->phy_handle);
3631
3632 skb_pull(skb, sizeof(*ev));
3633
3634 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
3635 if (!hcon)
3636 return;
3637
3638 amp_read_loc_assoc_final_data(hdev, hcon);
3639}
3640
a9de9248
MH
3641void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
3642{
3643 struct hci_event_hdr *hdr = (void *) skb->data;
3644 __u8 event = hdr->evt;
3645
b6ddb638
JH
3646 hci_dev_lock(hdev);
3647
3648 /* Received events are (currently) only needed when a request is
3649 * ongoing so avoid unnecessary memory allocation.
3650 */
3651 if (hdev->req_status == HCI_REQ_PEND) {
3652 kfree_skb(hdev->recv_evt);
3653 hdev->recv_evt = skb_clone(skb, GFP_KERNEL);
3654 }
3655
3656 hci_dev_unlock(hdev);
3657
a9de9248
MH
3658 skb_pull(skb, HCI_EVENT_HDR_SIZE);
3659
02350a72
JH
3660 if (hdev->sent_cmd && bt_cb(hdev->sent_cmd)->req.event == event) {
3661 struct hci_command_hdr *hdr = (void *) hdev->sent_cmd->data;
3662 u16 opcode = __le16_to_cpu(hdr->opcode);
3663
3664 hci_req_cmd_complete(hdev, opcode, 0);
3665 }
3666
a9de9248 3667 switch (event) {
1da177e4
LT
3668 case HCI_EV_INQUIRY_COMPLETE:
3669 hci_inquiry_complete_evt(hdev, skb);
3670 break;
3671
3672 case HCI_EV_INQUIRY_RESULT:
3673 hci_inquiry_result_evt(hdev, skb);
3674 break;
3675
a9de9248
MH
3676 case HCI_EV_CONN_COMPLETE:
3677 hci_conn_complete_evt(hdev, skb);
21d9e30e
MH
3678 break;
3679
1da177e4
LT
3680 case HCI_EV_CONN_REQUEST:
3681 hci_conn_request_evt(hdev, skb);
3682 break;
3683
1da177e4
LT
3684 case HCI_EV_DISCONN_COMPLETE:
3685 hci_disconn_complete_evt(hdev, skb);
3686 break;
3687
1da177e4
LT
3688 case HCI_EV_AUTH_COMPLETE:
3689 hci_auth_complete_evt(hdev, skb);
3690 break;
3691
a9de9248
MH
3692 case HCI_EV_REMOTE_NAME:
3693 hci_remote_name_evt(hdev, skb);
3694 break;
3695
1da177e4
LT
3696 case HCI_EV_ENCRYPT_CHANGE:
3697 hci_encrypt_change_evt(hdev, skb);
3698 break;
3699
a9de9248
MH
3700 case HCI_EV_CHANGE_LINK_KEY_COMPLETE:
3701 hci_change_link_key_complete_evt(hdev, skb);
3702 break;
3703
3704 case HCI_EV_REMOTE_FEATURES:
3705 hci_remote_features_evt(hdev, skb);
3706 break;
3707
a9de9248
MH
3708 case HCI_EV_CMD_COMPLETE:
3709 hci_cmd_complete_evt(hdev, skb);
3710 break;
3711
3712 case HCI_EV_CMD_STATUS:
3713 hci_cmd_status_evt(hdev, skb);
3714 break;
3715
3716 case HCI_EV_ROLE_CHANGE:
3717 hci_role_change_evt(hdev, skb);
3718 break;
3719
3720 case HCI_EV_NUM_COMP_PKTS:
3721 hci_num_comp_pkts_evt(hdev, skb);
3722 break;
3723
3724 case HCI_EV_MODE_CHANGE:
3725 hci_mode_change_evt(hdev, skb);
1da177e4
LT
3726 break;
3727
3728 case HCI_EV_PIN_CODE_REQ:
3729 hci_pin_code_request_evt(hdev, skb);
3730 break;
3731
3732 case HCI_EV_LINK_KEY_REQ:
3733 hci_link_key_request_evt(hdev, skb);
3734 break;
3735
3736 case HCI_EV_LINK_KEY_NOTIFY:
3737 hci_link_key_notify_evt(hdev, skb);
3738 break;
3739
3740 case HCI_EV_CLOCK_OFFSET:
3741 hci_clock_offset_evt(hdev, skb);
3742 break;
3743
a8746417
MH
3744 case HCI_EV_PKT_TYPE_CHANGE:
3745 hci_pkt_type_change_evt(hdev, skb);
3746 break;
3747
85a1e930
MH
3748 case HCI_EV_PSCAN_REP_MODE:
3749 hci_pscan_rep_mode_evt(hdev, skb);
3750 break;
3751
a9de9248
MH
3752 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
3753 hci_inquiry_result_with_rssi_evt(hdev, skb);
04837f64
MH
3754 break;
3755
a9de9248
MH
3756 case HCI_EV_REMOTE_EXT_FEATURES:
3757 hci_remote_ext_features_evt(hdev, skb);
1da177e4
LT
3758 break;
3759
a9de9248
MH
3760 case HCI_EV_SYNC_CONN_COMPLETE:
3761 hci_sync_conn_complete_evt(hdev, skb);
3762 break;
1da177e4 3763
a9de9248
MH
3764 case HCI_EV_EXTENDED_INQUIRY_RESULT:
3765 hci_extended_inquiry_result_evt(hdev, skb);
3766 break;
1da177e4 3767
1c2e0041
JH
3768 case HCI_EV_KEY_REFRESH_COMPLETE:
3769 hci_key_refresh_complete_evt(hdev, skb);
3770 break;
3771
0493684e
MH
3772 case HCI_EV_IO_CAPA_REQUEST:
3773 hci_io_capa_request_evt(hdev, skb);
3774 break;
3775
03b555e1
JH
3776 case HCI_EV_IO_CAPA_REPLY:
3777 hci_io_capa_reply_evt(hdev, skb);
3778 break;
3779
a5c29683
JH
3780 case HCI_EV_USER_CONFIRM_REQUEST:
3781 hci_user_confirm_request_evt(hdev, skb);
3782 break;
3783
1143d458
BG
3784 case HCI_EV_USER_PASSKEY_REQUEST:
3785 hci_user_passkey_request_evt(hdev, skb);
3786 break;
3787
92a25256
JH
3788 case HCI_EV_USER_PASSKEY_NOTIFY:
3789 hci_user_passkey_notify_evt(hdev, skb);
3790 break;
3791
3792 case HCI_EV_KEYPRESS_NOTIFY:
3793 hci_keypress_notify_evt(hdev, skb);
3794 break;
3795
0493684e
MH
3796 case HCI_EV_SIMPLE_PAIR_COMPLETE:
3797 hci_simple_pair_complete_evt(hdev, skb);
3798 break;
3799
41a96212
MH
3800 case HCI_EV_REMOTE_HOST_FEATURES:
3801 hci_remote_host_features_evt(hdev, skb);
3802 break;
3803
fcd89c09
VT
3804 case HCI_EV_LE_META:
3805 hci_le_meta_evt(hdev, skb);
3806 break;
3807
9495b2ee
AE
3808 case HCI_EV_CHANNEL_SELECTED:
3809 hci_chan_selected_evt(hdev, skb);
3810 break;
3811
2763eda6
SJ
3812 case HCI_EV_REMOTE_OOB_DATA_REQUEST:
3813 hci_remote_oob_data_request_evt(hdev, skb);
3814 break;
3815
d5e91192
AE
3816 case HCI_EV_PHY_LINK_COMPLETE:
3817 hci_phy_link_complete_evt(hdev, skb);
3818 break;
3819
27695fb4
AE
3820 case HCI_EV_LOGICAL_LINK_COMPLETE:
3821 hci_loglink_complete_evt(hdev, skb);
3822 break;
3823
606e2a10
AE
3824 case HCI_EV_DISCONN_LOGICAL_LINK_COMPLETE:
3825 hci_disconn_loglink_complete_evt(hdev, skb);
3826 break;
3827
9eef6b3a
AE
3828 case HCI_EV_DISCONN_PHY_LINK_COMPLETE:
3829 hci_disconn_phylink_complete_evt(hdev, skb);
3830 break;
3831
25e89e99
AE
3832 case HCI_EV_NUM_COMP_BLOCKS:
3833 hci_num_comp_blocks_evt(hdev, skb);
3834 break;
3835
a9de9248 3836 default:
9f1db00c 3837 BT_DBG("%s event 0x%2.2x", hdev->name, event);
1da177e4
LT
3838 break;
3839 }
3840
3841 kfree_skb(skb);
3842 hdev->stat.evt_rx++;
3843}
This page took 1.175676 seconds and 5 git commands to generate.