blob: a1a5a72bad897018c9454680d76bb9ffe5d58c2e [file] [log] [blame]
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 BlueZ - Bluetooth protocol stack for Linux
Srinivas Krovvidi32ba9352012-01-26 10:48:08 +05303 Copyright (c) 2000-2001, 2010-2012, Code Aurora Forum. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation;
10
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090015 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090020 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 SOFTWARE IS DISCLAIMED.
23*/
24
25/* Bluetooth HCI event handling. */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
28
29#include <linux/types.h>
30#include <linux/errno.h>
31#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/slab.h>
33#include <linux/poll.h>
34#include <linux/fcntl.h>
35#include <linux/init.h>
36#include <linux/skbuff.h>
37#include <linux/interrupt.h>
38#include <linux/notifier.h>
39#include <net/sock.h>
40
41#include <asm/system.h>
Andrei Emeltchenko70f230202010-12-01 16:58:25 +020042#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/unaligned.h>
44
45#include <net/bluetooth/bluetooth.h>
46#include <net/bluetooth/hci_core.h>
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/* Handle HCI Event packets */
49
Marcel Holtmanna9de9242007-10-20 13:33:56 +020050static void hci_cc_inquiry_cancel(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Marcel Holtmanna9de9242007-10-20 13:33:56 +020052 __u8 status = *((__u8 *) skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Marcel Holtmanna9de9242007-10-20 13:33:56 +020054 BT_DBG("%s status 0x%x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Marcel Holtmanna9de9242007-10-20 13:33:56 +020056 if (status)
57 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070059 clear_bit(HCI_INQUIRY, &hdev->flags);
Marcel Holtmann6bd57412006-11-18 22:14:22 +010060
Johan Hedberg23bb5762010-12-21 23:01:27 +020061 hci_req_complete(hdev, HCI_OP_INQUIRY_CANCEL, status);
Marcel Holtmann6bd57412006-11-18 22:14:22 +010062
Marcel Holtmanna9de9242007-10-20 13:33:56 +020063 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
Marcel Holtmanna9de9242007-10-20 13:33:56 +020066static void hci_cc_exit_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Marcel Holtmanna9de9242007-10-20 13:33:56 +020068 __u8 status = *((__u8 *) skb->data);
69
70 BT_DBG("%s status 0x%x", hdev->name, status);
71
72 if (status)
73 return;
74
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070075 clear_bit(HCI_INQUIRY, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +020076
77 hci_conn_check_pending(hdev);
78}
79
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080static void hci_cc_link_key_reply(struct hci_dev *hdev, struct sk_buff *skb)
81{
82 struct hci_rp_link_key_reply *rp = (void *) skb->data;
83 struct hci_conn *conn;
84 struct hci_cp_link_key_reply *cp;
85
86 BT_DBG("%s status 0x%x", hdev->name, rp->status);
87 if (rp->status)
88 return;
89
90 cp = hci_sent_cmd_data(hdev, HCI_OP_LINK_KEY_REPLY);
91 if (!cp)
92 return;
93
94 hci_dev_lock(hdev);
95 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
96 if (conn) {
97 hci_conn_hold(conn);
98 memcpy(conn->link_key, cp->link_key, sizeof(conn->link_key));
99 conn->key_type = 5;
100 hci_conn_put(conn);
101 }
102 hci_dev_unlock(hdev);
103}
104
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200105static void hci_cc_remote_name_req_cancel(struct hci_dev *hdev, struct sk_buff *skb)
106{
107 BT_DBG("%s", hdev->name);
108}
109
110static void hci_cc_role_discovery(struct hci_dev *hdev, struct sk_buff *skb)
111{
112 struct hci_rp_role_discovery *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200115 BT_DBG("%s status 0x%x", hdev->name, rp->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200117 if (rp->status)
118 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200120 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200122 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
123 if (conn) {
124 if (rp->role)
125 conn->link_mode &= ~HCI_LM_MASTER;
126 else
127 conn->link_mode |= HCI_LM_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200129
130 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200133static void hci_cc_read_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
134{
135 struct hci_rp_read_link_policy *rp = (void *) skb->data;
136 struct hci_conn *conn;
137
138 BT_DBG("%s status 0x%x", hdev->name, rp->status);
139
140 if (rp->status)
141 return;
142
143 hci_dev_lock(hdev);
144
145 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
146 if (conn)
147 conn->link_policy = __le16_to_cpu(rp->policy);
148
149 hci_dev_unlock(hdev);
150}
151
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200152static void hci_cc_write_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200154 struct hci_rp_write_link_policy *rp = (void *) skb->data;
155 struct hci_conn *conn;
156 void *sent;
157
158 BT_DBG("%s status 0x%x", hdev->name, rp->status);
159
160 if (rp->status)
161 return;
162
163 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LINK_POLICY);
164 if (!sent)
165 return;
166
167 hci_dev_lock(hdev);
168
169 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200170 if (conn)
Harvey Harrison83985312008-05-02 16:25:46 -0700171 conn->link_policy = get_unaligned_le16(sent + 2);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200172
173 hci_dev_unlock(hdev);
174}
175
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200176static void hci_cc_read_def_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
177{
178 struct hci_rp_read_def_link_policy *rp = (void *) skb->data;
179
180 BT_DBG("%s status 0x%x", hdev->name, rp->status);
181
182 if (rp->status)
183 return;
184
185 hdev->link_policy = __le16_to_cpu(rp->policy);
186}
187
188static void hci_cc_write_def_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
189{
190 __u8 status = *((__u8 *) skb->data);
191 void *sent;
192
193 BT_DBG("%s status 0x%x", hdev->name, status);
194
195 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_LINK_POLICY);
196 if (!sent)
197 return;
198
199 if (!status)
200 hdev->link_policy = get_unaligned_le16(sent);
201
Johan Hedberg23bb5762010-12-21 23:01:27 +0200202 hci_req_complete(hdev, HCI_OP_WRITE_DEF_LINK_POLICY, status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200203}
204
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200205static void hci_cc_reset(struct hci_dev *hdev, struct sk_buff *skb)
206{
207 __u8 status = *((__u8 *) skb->data);
208
209 BT_DBG("%s status 0x%x", hdev->name, status);
210
Gustavo F. Padovan10572132011-03-16 15:36:29 -0300211 clear_bit(HCI_RESET, &hdev->flags);
212
Johan Hedberg23bb5762010-12-21 23:01:27 +0200213 hci_req_complete(hdev, HCI_OP_RESET, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200214}
215
216static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb)
217{
218 __u8 status = *((__u8 *) skb->data);
219 void *sent;
220
221 BT_DBG("%s status 0x%x", hdev->name, status);
222
223 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LOCAL_NAME);
224 if (!sent)
225 return;
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800226 hci_dev_lock(hdev);
Royston Rodriguesc58c0c22011-10-24 12:54:13 +0530227 if (!status)
228 memcpy(hdev->dev_name, sent, HCI_MAX_NAME_LENGTH);
229
Johan Hedbergb312b1612011-03-16 14:29:37 +0200230 if (test_bit(HCI_MGMT, &hdev->flags))
231 mgmt_set_local_name_complete(hdev->id, sent, status);
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800232 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200233}
234
235static void hci_cc_read_local_name(struct hci_dev *hdev, struct sk_buff *skb)
236{
237 struct hci_rp_read_local_name *rp = (void *) skb->data;
238
239 BT_DBG("%s status 0x%x", hdev->name, rp->status);
240
241 if (rp->status)
242 return;
243
Johan Hedberg1f6c6372011-03-16 14:29:35 +0200244 memcpy(hdev->dev_name, rp->name, HCI_MAX_NAME_LENGTH);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200245}
246
247static void hci_cc_write_auth_enable(struct hci_dev *hdev, struct sk_buff *skb)
248{
249 __u8 status = *((__u8 *) skb->data);
250 void *sent;
251
252 BT_DBG("%s status 0x%x", hdev->name, status);
253
254 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_ENABLE);
255 if (!sent)
256 return;
257
258 if (!status) {
259 __u8 param = *((__u8 *) sent);
260
261 if (param == AUTH_ENABLED)
262 set_bit(HCI_AUTH, &hdev->flags);
263 else
264 clear_bit(HCI_AUTH, &hdev->flags);
265 }
266
Johan Hedberg23bb5762010-12-21 23:01:27 +0200267 hci_req_complete(hdev, HCI_OP_WRITE_AUTH_ENABLE, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200268}
269
270static void hci_cc_write_encrypt_mode(struct hci_dev *hdev, struct sk_buff *skb)
271{
272 __u8 status = *((__u8 *) skb->data);
273 void *sent;
274
275 BT_DBG("%s status 0x%x", hdev->name, status);
276
277 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_ENCRYPT_MODE);
278 if (!sent)
279 return;
280
281 if (!status) {
282 __u8 param = *((__u8 *) sent);
283
284 if (param)
285 set_bit(HCI_ENCRYPT, &hdev->flags);
286 else
287 clear_bit(HCI_ENCRYPT, &hdev->flags);
288 }
289
Johan Hedberg23bb5762010-12-21 23:01:27 +0200290 hci_req_complete(hdev, HCI_OP_WRITE_ENCRYPT_MODE, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200291}
292
293static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
294{
295 __u8 status = *((__u8 *) skb->data);
296 void *sent;
297
298 BT_DBG("%s status 0x%x", hdev->name, status);
299
300 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SCAN_ENABLE);
301 if (!sent)
302 return;
303
304 if (!status) {
305 __u8 param = *((__u8 *) sent);
Johan Hedberg9fbcbb42010-12-30 00:18:33 +0200306 int old_pscan, old_iscan;
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800307 hci_dev_lock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200308
Johan Hedberg9fbcbb42010-12-30 00:18:33 +0200309 old_pscan = test_and_clear_bit(HCI_PSCAN, &hdev->flags);
310 old_iscan = test_and_clear_bit(HCI_ISCAN, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200311
Johan Hedberg73f22f62010-12-29 16:00:25 +0200312 if (param & SCAN_INQUIRY) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200313 set_bit(HCI_ISCAN, &hdev->flags);
Johan Hedberg9fbcbb42010-12-30 00:18:33 +0200314 if (!old_iscan)
315 mgmt_discoverable(hdev->id, 1);
316 } else if (old_iscan)
Johan Hedberg73f22f62010-12-29 16:00:25 +0200317 mgmt_discoverable(hdev->id, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200318
Johan Hedberg9fbcbb42010-12-30 00:18:33 +0200319 if (param & SCAN_PAGE) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200320 set_bit(HCI_PSCAN, &hdev->flags);
Johan Hedberg9fbcbb42010-12-30 00:18:33 +0200321 if (!old_pscan)
322 mgmt_connectable(hdev->id, 1);
323 } else if (old_pscan)
324 mgmt_connectable(hdev->id, 0);
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800325 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200326 }
327
Johan Hedberg23bb5762010-12-21 23:01:27 +0200328 hci_req_complete(hdev, HCI_OP_WRITE_SCAN_ENABLE, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200329}
330
331static void hci_cc_read_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
332{
333 struct hci_rp_read_class_of_dev *rp = (void *) skb->data;
334
335 BT_DBG("%s status 0x%x", hdev->name, rp->status);
336
337 if (rp->status)
338 return;
339
340 memcpy(hdev->dev_class, rp->dev_class, 3);
341
342 BT_DBG("%s class 0x%.2x%.2x%.2x", hdev->name,
343 hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
344}
345
346static void hci_cc_write_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
347{
348 __u8 status = *((__u8 *) skb->data);
349 void *sent;
350
351 BT_DBG("%s status 0x%x", hdev->name, status);
352
Marcel Holtmannf383f272008-07-14 20:13:47 +0200353 if (status)
354 return;
355
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200356 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_CLASS_OF_DEV);
357 if (!sent)
358 return;
359
Marcel Holtmannf383f272008-07-14 20:13:47 +0200360 memcpy(hdev->dev_class, sent, 3);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200361}
362
363static void hci_cc_read_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
364{
365 struct hci_rp_read_voice_setting *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 __u16 setting;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200367
368 BT_DBG("%s status 0x%x", hdev->name, rp->status);
369
370 if (rp->status)
371 return;
372
373 setting = __le16_to_cpu(rp->voice_setting);
374
Marcel Holtmannf383f272008-07-14 20:13:47 +0200375 if (hdev->voice_setting == setting)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200376 return;
377
378 hdev->voice_setting = setting;
379
380 BT_DBG("%s voice setting 0x%04x", hdev->name, setting);
381
382 if (hdev->notify) {
383 tasklet_disable(&hdev->tx_task);
384 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
385 tasklet_enable(&hdev->tx_task);
386 }
387}
388
389static void hci_cc_write_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
390{
391 __u8 status = *((__u8 *) skb->data);
Marcel Holtmannf383f272008-07-14 20:13:47 +0200392 __u16 setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 void *sent;
394
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200395 BT_DBG("%s status 0x%x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Marcel Holtmannf383f272008-07-14 20:13:47 +0200397 if (status)
398 return;
399
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200400 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_VOICE_SETTING);
401 if (!sent)
402 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Marcel Holtmannf383f272008-07-14 20:13:47 +0200404 setting = get_unaligned_le16(sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Marcel Holtmannf383f272008-07-14 20:13:47 +0200406 if (hdev->voice_setting == setting)
407 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Marcel Holtmannf383f272008-07-14 20:13:47 +0200409 hdev->voice_setting = setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Marcel Holtmannf383f272008-07-14 20:13:47 +0200411 BT_DBG("%s voice setting 0x%04x", hdev->name, setting);
412
413 if (hdev->notify) {
414 tasklet_disable(&hdev->tx_task);
415 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
416 tasklet_enable(&hdev->tx_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418}
419
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200420static void hci_cc_host_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200422 __u8 status = *((__u8 *) skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200424 BT_DBG("%s status 0x%x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Johan Hedberg23bb5762010-12-21 23:01:27 +0200426 hci_req_complete(hdev, HCI_OP_HOST_BUFFER_SIZE, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
Marcel Holtmann333140b2008-07-14 20:13:48 +0200429static void hci_cc_read_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
430{
431 struct hci_rp_read_ssp_mode *rp = (void *) skb->data;
432
433 BT_DBG("%s status 0x%x", hdev->name, rp->status);
434
435 if (rp->status)
436 return;
437
438 hdev->ssp_mode = rp->mode;
439}
440
441static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
442{
443 __u8 status = *((__u8 *) skb->data);
444 void *sent;
445
446 BT_DBG("%s status 0x%x", hdev->name, status);
447
448 if (status)
449 return;
450
451 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_MODE);
452 if (!sent)
453 return;
454
455 hdev->ssp_mode = *((__u8 *) sent);
456}
457
Johan Hedbergd5859e22011-01-25 01:19:58 +0200458static u8 hci_get_inquiry_mode(struct hci_dev *hdev)
459{
460 if (hdev->features[6] & LMP_EXT_INQ)
461 return 2;
462
463 if (hdev->features[3] & LMP_RSSI_INQ)
464 return 1;
465
466 if (hdev->manufacturer == 11 && hdev->hci_rev == 0x00 &&
467 hdev->lmp_subver == 0x0757)
468 return 1;
469
470 if (hdev->manufacturer == 15) {
471 if (hdev->hci_rev == 0x03 && hdev->lmp_subver == 0x6963)
472 return 1;
473 if (hdev->hci_rev == 0x09 && hdev->lmp_subver == 0x6963)
474 return 1;
475 if (hdev->hci_rev == 0x00 && hdev->lmp_subver == 0x6965)
476 return 1;
477 }
478
479 if (hdev->manufacturer == 31 && hdev->hci_rev == 0x2005 &&
480 hdev->lmp_subver == 0x1805)
481 return 1;
482
483 return 0;
484}
485
486static void hci_setup_inquiry_mode(struct hci_dev *hdev)
487{
488 u8 mode;
489
490 mode = hci_get_inquiry_mode(hdev);
491
492 hci_send_cmd(hdev, HCI_OP_WRITE_INQUIRY_MODE, 1, &mode);
493}
494
495static void hci_setup_event_mask(struct hci_dev *hdev)
496{
497 /* The second byte is 0xff instead of 0x9f (two reserved bits
498 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
499 * command otherwise */
500 u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
501
Brian Gixa68668b2011-08-11 15:49:36 -0700502 BT_DBG("");
503
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700504 /* Events for 1.2 and newer controllers */
505 if (hdev->lmp_ver > 1) {
506 events[4] |= 0x01; /* Flow Specification Complete */
507 events[4] |= 0x02; /* Inquiry Result with RSSI */
508 events[4] |= 0x04; /* Read Remote Extended Features Complete */
509 events[5] |= 0x08; /* Synchronous Connection Complete */
510 events[5] |= 0x10; /* Synchronous Connection Changed */
511 }
Johan Hedbergd5859e22011-01-25 01:19:58 +0200512
513 if (hdev->features[3] & LMP_RSSI_INQ)
514 events[4] |= 0x04; /* Inquiry Result with RSSI */
515
516 if (hdev->features[5] & LMP_SNIFF_SUBR)
517 events[5] |= 0x20; /* Sniff Subrating */
518
519 if (hdev->features[5] & LMP_PAUSE_ENC)
520 events[5] |= 0x80; /* Encryption Key Refresh Complete */
521
522 if (hdev->features[6] & LMP_EXT_INQ)
523 events[5] |= 0x40; /* Extended Inquiry Result */
524
525 if (hdev->features[6] & LMP_NO_FLUSH)
526 events[7] |= 0x01; /* Enhanced Flush Complete */
527
528 if (hdev->features[7] & LMP_LSTO)
529 events[6] |= 0x80; /* Link Supervision Timeout Changed */
530
531 if (hdev->features[6] & LMP_SIMPLE_PAIR) {
532 events[6] |= 0x01; /* IO Capability Request */
533 events[6] |= 0x02; /* IO Capability Response */
534 events[6] |= 0x04; /* User Confirmation Request */
535 events[6] |= 0x08; /* User Passkey Request */
536 events[6] |= 0x10; /* Remote OOB Data Request */
537 events[6] |= 0x20; /* Simple Pairing Complete */
538 events[7] |= 0x04; /* User Passkey Notification */
539 events[7] |= 0x08; /* Keypress Notification */
540 events[7] |= 0x10; /* Remote Host Supported
541 * Features Notification */
542 }
543
544 if (hdev->features[4] & LMP_LE)
545 events[7] |= 0x20; /* LE Meta-Event */
546
547 hci_send_cmd(hdev, HCI_OP_SET_EVENT_MASK, sizeof(events), events);
548}
549
550static void hci_setup(struct hci_dev *hdev)
551{
Johan Hedbergd5859e22011-01-25 01:19:58 +0200552 if (hdev->lmp_ver > 1)
553 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL);
554
555 if (hdev->features[6] & LMP_SIMPLE_PAIR) {
556 u8 mode = 0x01;
557 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, sizeof(mode), &mode);
558 }
559
560 if (hdev->features[3] & LMP_RSSI_INQ)
561 hci_setup_inquiry_mode(hdev);
562
563 if (hdev->features[7] & LMP_INQ_TX_PWR)
564 hci_send_cmd(hdev, HCI_OP_READ_INQ_RSP_TX_POWER, 0, NULL);
565}
566
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200567static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
568{
569 struct hci_rp_read_local_version *rp = (void *) skb->data;
570
571 BT_DBG("%s status 0x%x", hdev->name, rp->status);
572
573 if (rp->status)
574 return;
575
576 hdev->hci_ver = rp->hci_ver;
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200577 hdev->hci_rev = __le16_to_cpu(rp->hci_rev);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200578 hdev->lmp_ver = rp->lmp_ver;
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200579 hdev->manufacturer = __le16_to_cpu(rp->manufacturer);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200580 hdev->lmp_subver = __le16_to_cpu(rp->lmp_subver);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200581
582 BT_DBG("%s manufacturer %d hci ver %d:%d", hdev->name,
583 hdev->manufacturer,
584 hdev->hci_ver, hdev->hci_rev);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200585
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700586 if (hdev->dev_type == HCI_BREDR && test_bit(HCI_INIT, &hdev->flags))
Johan Hedbergd5859e22011-01-25 01:19:58 +0200587 hci_setup(hdev);
588}
589
590static void hci_setup_link_policy(struct hci_dev *hdev)
591{
592 u16 link_policy = 0;
593
594 if (hdev->features[0] & LMP_RSWITCH)
595 link_policy |= HCI_LP_RSWITCH;
596 if (hdev->features[0] & LMP_HOLD)
597 link_policy |= HCI_LP_HOLD;
598 if (hdev->features[0] & LMP_SNIFF)
599 link_policy |= HCI_LP_SNIFF;
600 if (hdev->features[1] & LMP_PARK)
601 link_policy |= HCI_LP_PARK;
602
603 link_policy = cpu_to_le16(link_policy);
604 hci_send_cmd(hdev, HCI_OP_WRITE_DEF_LINK_POLICY,
605 sizeof(link_policy), &link_policy);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200606}
607
608static void hci_cc_read_local_commands(struct hci_dev *hdev, struct sk_buff *skb)
609{
610 struct hci_rp_read_local_commands *rp = (void *) skb->data;
611
612 BT_DBG("%s status 0x%x", hdev->name, rp->status);
613
614 if (rp->status)
Johan Hedbergd5859e22011-01-25 01:19:58 +0200615 goto done;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200616
617 memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
Johan Hedbergd5859e22011-01-25 01:19:58 +0200618
619 if (test_bit(HCI_INIT, &hdev->flags) && (hdev->commands[5] & 0x10))
620 hci_setup_link_policy(hdev);
621
622done:
623 hci_req_complete(hdev, HCI_OP_READ_LOCAL_COMMANDS, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200624}
625
626static void hci_cc_read_local_features(struct hci_dev *hdev, struct sk_buff *skb)
627{
628 struct hci_rp_read_local_features *rp = (void *) skb->data;
629
630 BT_DBG("%s status 0x%x", hdev->name, rp->status);
631
632 if (rp->status)
633 return;
634
635 memcpy(hdev->features, rp->features, 8);
636
Brian Gixa68668b2011-08-11 15:49:36 -0700637 if (hdev->dev_type == HCI_BREDR && test_bit(HCI_INIT, &hdev->flags)) {
638 if (hdev->features[6] & LMP_SIMPLE_PAIR) {
639 u8 mode = 0x01;
640 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE,
641 sizeof(mode), &mode);
642 }
643
644 if (hdev->features[3] & LMP_RSSI_INQ)
645 hci_setup_inquiry_mode(hdev);
646
647 if (hdev->features[7] & LMP_INQ_TX_PWR)
648 hci_send_cmd(hdev, HCI_OP_READ_INQ_RSP_TX_POWER,
649 0, NULL);
650
651 hci_setup_event_mask(hdev);
652 }
653
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200654 /* Adjust default settings according to features
655 * supported by device. */
656
657 if (hdev->features[0] & LMP_3SLOT)
658 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
659
660 if (hdev->features[0] & LMP_5SLOT)
661 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
662
663 if (hdev->features[1] & LMP_HV2) {
664 hdev->pkt_type |= (HCI_HV2);
665 hdev->esco_type |= (ESCO_HV2);
666 }
667
668 if (hdev->features[1] & LMP_HV3) {
669 hdev->pkt_type |= (HCI_HV3);
670 hdev->esco_type |= (ESCO_HV3);
671 }
672
673 if (hdev->features[3] & LMP_ESCO)
674 hdev->esco_type |= (ESCO_EV3);
675
676 if (hdev->features[4] & LMP_EV4)
677 hdev->esco_type |= (ESCO_EV4);
678
679 if (hdev->features[4] & LMP_EV5)
680 hdev->esco_type |= (ESCO_EV5);
681
Marcel Holtmannefc76882009-02-06 09:13:37 +0100682 if (hdev->features[5] & LMP_EDR_ESCO_2M)
683 hdev->esco_type |= (ESCO_2EV3);
684
685 if (hdev->features[5] & LMP_EDR_ESCO_3M)
686 hdev->esco_type |= (ESCO_3EV3);
687
688 if (hdev->features[5] & LMP_EDR_3S_ESCO)
689 hdev->esco_type |= (ESCO_2EV5 | ESCO_3EV5);
690
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200691 BT_DBG("%s features 0x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x", hdev->name,
692 hdev->features[0], hdev->features[1],
693 hdev->features[2], hdev->features[3],
694 hdev->features[4], hdev->features[5],
695 hdev->features[6], hdev->features[7]);
696}
697
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700698static void hci_cc_read_flow_control_mode(struct hci_dev *hdev,
699 struct sk_buff *skb)
Andre Guedesd5fa5132011-06-30 19:20:52 -0300700{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700701 struct hci_rp_read_flow_control_mode *rp = (void *) skb->data;
Andre Guedesd5fa5132011-06-30 19:20:52 -0300702
703 BT_DBG("%s status 0x%x", hdev->name, rp->status);
704
705 if (rp->status)
706 return;
707
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700708 hdev->flow_ctl_mode = rp->mode;
Andre Guedesd5fa5132011-06-30 19:20:52 -0300709}
710
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200711static void hci_cc_read_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
712{
713 struct hci_rp_read_buffer_size *rp = (void *) skb->data;
714
715 BT_DBG("%s status 0x%x", hdev->name, rp->status);
716
717 if (rp->status)
718 return;
719
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700720 if (hdev->flow_ctl_mode == HCI_PACKET_BASED_FLOW_CTL_MODE) {
721 hdev->acl_mtu = __le16_to_cpu(rp->acl_mtu);
722 hdev->sco_mtu = rp->sco_mtu;
723 hdev->acl_pkts = __le16_to_cpu(rp->acl_max_pkt);
724 hdev->sco_pkts = __le16_to_cpu(rp->sco_max_pkt);
725 hdev->acl_cnt = hdev->acl_pkts;
726 hdev->sco_cnt = hdev->sco_pkts;
727 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200728
729 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
730 hdev->sco_mtu = 64;
731 hdev->sco_pkts = 8;
732 }
733
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200734
735 BT_DBG("%s acl mtu %d:%d sco mtu %d:%d", hdev->name,
736 hdev->acl_mtu, hdev->acl_pkts,
737 hdev->sco_mtu, hdev->sco_pkts);
738}
739
740static void hci_cc_read_bd_addr(struct hci_dev *hdev, struct sk_buff *skb)
741{
742 struct hci_rp_read_bd_addr *rp = (void *) skb->data;
743
744 BT_DBG("%s status 0x%x", hdev->name, rp->status);
745
746 if (!rp->status)
747 bacpy(&hdev->bdaddr, &rp->bdaddr);
748
Johan Hedberg23bb5762010-12-21 23:01:27 +0200749 hci_req_complete(hdev, HCI_OP_READ_BD_ADDR, rp->status);
750}
751
752static void hci_cc_write_ca_timeout(struct hci_dev *hdev, struct sk_buff *skb)
753{
754 __u8 status = *((__u8 *) skb->data);
755
756 BT_DBG("%s status 0x%x", hdev->name, status);
757
758 hci_req_complete(hdev, HCI_OP_WRITE_CA_TIMEOUT, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200759}
760
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700761static void hci_cc_read_data_block_size(struct hci_dev *hdev,
762 struct sk_buff *skb)
763{
764 struct hci_rp_read_data_block_size *rp = (void *) skb->data;
765
766 BT_DBG("%s status 0x%x", hdev->name, rp->status);
767
768 if (rp->status)
769 return;
770
771 if (hdev->flow_ctl_mode == HCI_BLOCK_BASED_FLOW_CTL_MODE) {
772 hdev->acl_mtu = __le16_to_cpu(rp->max_acl_len);
773 hdev->sco_mtu = 0;
774 hdev->data_block_len = __le16_to_cpu(rp->data_block_len);
775 /* acl_pkts indicates the number of blocks */
776 hdev->acl_pkts = __le16_to_cpu(rp->num_blocks);
777 hdev->sco_pkts = 0;
778 hdev->acl_cnt = hdev->acl_pkts;
779 hdev->sco_cnt = 0;
780 }
781
782 BT_DBG("%s acl mtu %d:%d, data block len %d", hdev->name,
783 hdev->acl_mtu, hdev->acl_cnt, hdev->data_block_len);
784}
785
786static void hci_cc_read_local_amp_info(struct hci_dev *hdev,
787 struct sk_buff *skb)
788{
789 struct hci_rp_read_local_amp_info *rp = (void *) skb->data;
790
791 BT_DBG("%s status 0x%x", hdev->name, rp->status);
792
793 if (rp->status)
794 return;
795
796 hdev->amp_status = rp->amp_status;
797 hdev->amp_total_bw = __le32_to_cpu(rp->total_bw);
798 hdev->amp_max_bw = __le32_to_cpu(rp->max_bw);
799 hdev->amp_min_latency = __le32_to_cpu(rp->min_latency);
800 hdev->amp_max_pdu = __le32_to_cpu(rp->max_pdu);
801 hdev->amp_type = rp->amp_type;
802 hdev->amp_pal_cap = __le16_to_cpu(rp->pal_cap);
803 hdev->amp_assoc_size = __le16_to_cpu(rp->max_assoc_size);
804 hdev->amp_be_flush_to = __le32_to_cpu(rp->be_flush_to);
805 hdev->amp_max_flush_to = __le32_to_cpu(rp->max_flush_to);
806
807 hci_req_complete(hdev, HCI_OP_READ_LOCAL_AMP_INFO, rp->status);
808}
809
Johan Hedbergb0916ea2011-01-10 13:44:55 +0200810static void hci_cc_delete_stored_link_key(struct hci_dev *hdev,
811 struct sk_buff *skb)
812{
813 __u8 status = *((__u8 *) skb->data);
814
815 BT_DBG("%s status 0x%x", hdev->name, status);
816
817 hci_req_complete(hdev, HCI_OP_DELETE_STORED_LINK_KEY, status);
818}
819
Johan Hedbergd5859e22011-01-25 01:19:58 +0200820static void hci_cc_set_event_mask(struct hci_dev *hdev, struct sk_buff *skb)
821{
822 __u8 status = *((__u8 *) skb->data);
823
824 BT_DBG("%s status 0x%x", hdev->name, status);
825
826 hci_req_complete(hdev, HCI_OP_SET_EVENT_MASK, status);
827}
828
829static void hci_cc_write_inquiry_mode(struct hci_dev *hdev,
830 struct sk_buff *skb)
831{
832 __u8 status = *((__u8 *) skb->data);
833
834 BT_DBG("%s status 0x%x", hdev->name, status);
835
836 hci_req_complete(hdev, HCI_OP_WRITE_INQUIRY_MODE, status);
837}
838
839static void hci_cc_read_inq_rsp_tx_power(struct hci_dev *hdev,
840 struct sk_buff *skb)
841{
842 __u8 status = *((__u8 *) skb->data);
843
844 BT_DBG("%s status 0x%x", hdev->name, status);
845
846 hci_req_complete(hdev, HCI_OP_READ_INQ_RSP_TX_POWER, status);
847}
848
849static void hci_cc_set_event_flt(struct hci_dev *hdev, struct sk_buff *skb)
850{
851 __u8 status = *((__u8 *) skb->data);
852
853 BT_DBG("%s status 0x%x", hdev->name, status);
854
855 hci_req_complete(hdev, HCI_OP_SET_EVENT_FLT, status);
856}
857
Johan Hedberg980e1a52011-01-22 06:10:07 +0200858static void hci_cc_pin_code_reply(struct hci_dev *hdev, struct sk_buff *skb)
859{
860 struct hci_rp_pin_code_reply *rp = (void *) skb->data;
861 struct hci_cp_pin_code_reply *cp;
862 struct hci_conn *conn;
863
864 BT_DBG("%s status 0x%x", hdev->name, rp->status);
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800865 hci_dev_lock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200866
867 if (test_bit(HCI_MGMT, &hdev->flags))
868 mgmt_pin_code_reply_complete(hdev->id, &rp->bdaddr, rp->status);
869
870 if (rp->status != 0)
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800871 goto unlock;
Johan Hedberg980e1a52011-01-22 06:10:07 +0200872
873 cp = hci_sent_cmd_data(hdev, HCI_OP_PIN_CODE_REPLY);
874 if (!cp)
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800875 goto unlock;
Johan Hedberg980e1a52011-01-22 06:10:07 +0200876
877 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
878 if (conn)
879 conn->pin_length = cp->pin_len;
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800880unlock:
881 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200882}
883
884static void hci_cc_pin_code_neg_reply(struct hci_dev *hdev, struct sk_buff *skb)
885{
886 struct hci_rp_pin_code_neg_reply *rp = (void *) skb->data;
887
888 BT_DBG("%s status 0x%x", hdev->name, rp->status);
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800889 hci_dev_lock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200890
891 if (test_bit(HCI_MGMT, &hdev->flags))
892 mgmt_pin_code_neg_reply_complete(hdev->id, &rp->bdaddr,
893 rp->status);
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800894 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200895}
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300896static void hci_cc_le_read_buffer_size(struct hci_dev *hdev,
897 struct sk_buff *skb)
898{
899 struct hci_rp_le_read_buffer_size *rp = (void *) skb->data;
900
901 BT_DBG("%s status 0x%x", hdev->name, rp->status);
902
903 if (rp->status)
904 return;
905
906 hdev->le_mtu = __le16_to_cpu(rp->le_mtu);
907 hdev->le_pkts = rp->le_max_pkt;
908
909 hdev->le_cnt = hdev->le_pkts;
910
911 BT_DBG("%s le mtu %d:%d", hdev->name, hdev->le_mtu, hdev->le_pkts);
912
913 hci_req_complete(hdev, HCI_OP_LE_READ_BUFFER_SIZE, rp->status);
914}
Johan Hedberg980e1a52011-01-22 06:10:07 +0200915
Johan Hedberga5c29682011-02-19 12:05:57 -0300916static void hci_cc_user_confirm_reply(struct hci_dev *hdev, struct sk_buff *skb)
917{
918 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
919
920 BT_DBG("%s status 0x%x", hdev->name, rp->status);
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800921 hci_dev_lock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -0300922
923 if (test_bit(HCI_MGMT, &hdev->flags))
924 mgmt_user_confirm_reply_complete(hdev->id, &rp->bdaddr,
925 rp->status);
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800926 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -0300927}
928
929static void hci_cc_user_confirm_neg_reply(struct hci_dev *hdev,
930 struct sk_buff *skb)
931{
932 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
933
934 BT_DBG("%s status 0x%x", hdev->name, rp->status);
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800935 hci_dev_lock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -0300936
937 if (test_bit(HCI_MGMT, &hdev->flags))
938 mgmt_user_confirm_neg_reply_complete(hdev->id, &rp->bdaddr,
939 rp->status);
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800940 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -0300941}
942
Szymon Jancc35938b2011-03-22 13:12:21 +0100943static void hci_cc_read_local_oob_data_reply(struct hci_dev *hdev,
944 struct sk_buff *skb)
945{
946 struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
947
948 BT_DBG("%s status 0x%x", hdev->name, rp->status);
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800949 hci_dev_lock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +0100950
951 mgmt_read_local_oob_data_reply_complete(hdev->id, rp->hash,
952 rp->randomizer, rp->status);
Subramanian Srinivasana727a492011-11-30 13:06:07 -0800953 hci_dev_unlock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +0100954}
955
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -0300956static void hci_cc_le_ltk_reply(struct hci_dev *hdev, struct sk_buff *skb)
957{
958 struct hci_rp_le_ltk_reply *rp = (void *) skb->data;
959
960 BT_DBG("%s status 0x%x", hdev->name, rp->status);
961
962 if (rp->status)
963 return;
964
965 hci_req_complete(hdev, HCI_OP_LE_LTK_REPLY, rp->status);
966}
967
968static void hci_cc_le_ltk_neg_reply(struct hci_dev *hdev, struct sk_buff *skb)
969{
970 struct hci_rp_le_ltk_neg_reply *rp = (void *) skb->data;
971
972 BT_DBG("%s status 0x%x", hdev->name, rp->status);
973
974 if (rp->status)
975 return;
976
977 hci_req_complete(hdev, HCI_OP_LE_LTK_NEG_REPLY, rp->status);
978}
979
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700980static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
981 struct sk_buff *skb)
Andre Guedese326af42011-06-30 19:20:53 -0300982{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700983 void *sent;
984 __u8 param_scan_enable;
Andre Guedese326af42011-06-30 19:20:53 -0300985 __u8 status = *((__u8 *) skb->data);
986
Andre Guedese326af42011-06-30 19:20:53 -0300987 if (status)
988 return;
989
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700990 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_ENABLE);
991 if (!sent)
992 return;
993
994 param_scan_enable = *((__u8 *) sent);
995 if (param_scan_enable == 0x01) {
996 del_timer(&hdev->adv_timer);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700997 } else if (param_scan_enable == 0x00) {
998 mod_timer(&hdev->adv_timer, jiffies + ADV_CLEAR_TIMEOUT);
999 }
Andre Guedese326af42011-06-30 19:20:53 -03001000}
1001
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001002static inline void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
1003{
1004 BT_DBG("%s status 0x%x", hdev->name, status);
1005
1006 if (status) {
Johan Hedberg23bb5762010-12-21 23:01:27 +02001007 hci_req_complete(hdev, HCI_OP_INQUIRY, status);
Johan Hedberg314b2382011-04-27 10:29:57 -04001008
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001009 hci_conn_check_pending(hdev);
Brian Gixa68668b2011-08-11 15:49:36 -07001010 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001011 set_bit(HCI_INQUIRY, &hdev->flags);
Subramanian Srinivasana727a492011-11-30 13:06:07 -08001012 hci_dev_lock(hdev);
Brian Gixa68668b2011-08-11 15:49:36 -07001013 if (test_bit(HCI_MGMT, &hdev->flags))
1014 mgmt_inquiry_started(hdev->id);
Subramanian Srinivasana727a492011-11-30 13:06:07 -08001015 hci_dev_unlock(hdev);
Brian Gixa68668b2011-08-11 15:49:36 -07001016 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001017}
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019static inline void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
1020{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001021 struct hci_cp_create_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001024 BT_DBG("%s status 0x%x", hdev->name, status);
1025
1026 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_CONN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (!cp)
1028 return;
1029
1030 hci_dev_lock(hdev);
1031
1032 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
1033
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001034 BT_DBG("%s bdaddr %s conn %p", hdev->name, batostr(&cp->bdaddr), conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 if (status) {
1037 if (conn && conn->state == BT_CONNECT) {
Marcel Holtmann4c67bc72006-10-15 17:30:56 +02001038 if (status != 0x0c || conn->attempt > 2) {
1039 conn->state = BT_CLOSED;
1040 hci_proto_connect_cfm(conn, status);
1041 hci_conn_del(conn);
1042 } else
1043 conn->state = BT_CONNECT2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045 } else {
1046 if (!conn) {
Nick Pellybbcda3b2010-02-11 11:54:28 -08001047 conn = hci_conn_add(hdev, ACL_LINK, 0, &cp->bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (conn) {
1049 conn->out = 1;
1050 conn->link_mode |= HCI_LM_MASTER;
1051 } else
Gustavo F. Padovan893ef972010-07-18 15:13:37 -03001052 BT_ERR("No memory for new connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054 }
1055
1056 hci_dev_unlock(hdev);
1057}
1058
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001059static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001061 struct hci_cp_add_sco *cp;
1062 struct hci_conn *acl, *sco;
1063 __u16 handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001065 BT_DBG("%s status 0x%x", hdev->name, status);
1066
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001067 if (!status)
1068 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001070 cp = hci_sent_cmd_data(hdev, HCI_OP_ADD_SCO);
1071 if (!cp)
1072 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001074 handle = __le16_to_cpu(cp->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001076 BT_DBG("%s handle %d", hdev->name, handle);
Marcel Holtmann6bd57412006-11-18 22:14:22 +01001077
1078 hci_dev_lock(hdev);
1079
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001080 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001081 if (acl) {
1082 sco = acl->link;
1083 if (sco) {
1084 sco->state = BT_CLOSED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001085
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001086 hci_proto_connect_cfm(sco, status);
1087 hci_conn_del(sco);
1088 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001089 }
Marcel Holtmann6bd57412006-11-18 22:14:22 +01001090
1091 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
1093
Marcel Holtmannf8558552008-07-14 20:13:49 +02001094static void hci_cs_auth_requested(struct hci_dev *hdev, __u8 status)
1095{
1096 struct hci_cp_auth_requested *cp;
1097 struct hci_conn *conn;
1098
1099 BT_DBG("%s status 0x%x", hdev->name, status);
1100
Marcel Holtmannf8558552008-07-14 20:13:49 +02001101 cp = hci_sent_cmd_data(hdev, HCI_OP_AUTH_REQUESTED);
1102 if (!cp)
1103 return;
1104
1105 hci_dev_lock(hdev);
1106
1107 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1108 if (conn) {
Prabhakaran Mc7953ca72012-03-06 20:43:46 +05301109 if (status) {
1110 mgmt_auth_failed(hdev->id, &conn->dst, status);
1111 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
1112
1113 if (conn->state == BT_CONFIG) {
1114 conn->state = BT_CONNECTED;
1115 hci_proto_connect_cfm(conn, status);
1116 hci_conn_put(conn);
1117 } else {
1118 hci_auth_cfm(conn, status);
1119 hci_conn_hold(conn);
1120 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
1121 hci_conn_put(conn);
1122 }
1123
1124 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
1125 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
1126 hci_encrypt_cfm(conn, status, 0x00);
1127 }
Marcel Holtmannf8558552008-07-14 20:13:49 +02001128 }
Srinivas Krovvidi9ff51452011-09-27 19:25:02 +05301129 conn->auth_initiator = 1;
Marcel Holtmannf8558552008-07-14 20:13:49 +02001130 }
1131
1132 hci_dev_unlock(hdev);
1133}
1134
1135static void hci_cs_set_conn_encrypt(struct hci_dev *hdev, __u8 status)
1136{
1137 struct hci_cp_set_conn_encrypt *cp;
1138 struct hci_conn *conn;
1139
1140 BT_DBG("%s status 0x%x", hdev->name, status);
1141
1142 if (!status)
1143 return;
1144
1145 cp = hci_sent_cmd_data(hdev, HCI_OP_SET_CONN_ENCRYPT);
1146 if (!cp)
1147 return;
1148
1149 hci_dev_lock(hdev);
1150
1151 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1152 if (conn) {
1153 if (conn->state == BT_CONFIG) {
1154 hci_proto_connect_cfm(conn, status);
1155 hci_conn_put(conn);
1156 }
1157 }
1158
1159 hci_dev_unlock(hdev);
1160}
1161
Johan Hedberg127178d2010-11-18 22:22:29 +02001162static int hci_outgoing_auth_needed(struct hci_dev *hdev,
Szymon Janc138d22e2011-02-17 16:44:23 +01001163 struct hci_conn *conn)
Johan Hedberg392599b2010-11-18 22:22:28 +02001164{
Johan Hedberg392599b2010-11-18 22:22:28 +02001165 if (conn->state != BT_CONFIG || !conn->out)
1166 return 0;
1167
Johan Hedberg765c2a92011-01-19 12:06:52 +05301168 if (conn->pending_sec_level == BT_SECURITY_SDP)
Johan Hedberg392599b2010-11-18 22:22:28 +02001169 return 0;
1170
1171 /* Only request authentication for SSP connections or non-SSP
Prabhakaran Mc6001a712011-09-06 11:56:25 +05301172 * devices with sec_level >= BT_SECURITY_MEDIUM*/
1173 BT_DBG("Pending sec level is %d", conn->pending_sec_level);
Johan Hedberg392599b2010-11-18 22:22:28 +02001174 if (!(hdev->ssp_mode > 0 && conn->ssp_mode > 0) &&
Prabhakaran Mc6001a712011-09-06 11:56:25 +05301175 conn->pending_sec_level < BT_SECURITY_MEDIUM)
Johan Hedberg392599b2010-11-18 22:22:28 +02001176 return 0;
1177
Johan Hedberg392599b2010-11-18 22:22:28 +02001178 return 1;
1179}
1180
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001181static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
1182{
Johan Hedberg127178d2010-11-18 22:22:29 +02001183 struct hci_cp_remote_name_req *cp;
1184 struct hci_conn *conn;
1185
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001186 BT_DBG("%s status 0x%x", hdev->name, status);
Johan Hedberg127178d2010-11-18 22:22:29 +02001187
1188 /* If successful wait for the name req complete event before
1189 * checking for the need to do authentication */
1190 if (!status)
1191 return;
1192
1193 cp = hci_sent_cmd_data(hdev, HCI_OP_REMOTE_NAME_REQ);
1194 if (!cp)
1195 return;
1196
1197 hci_dev_lock(hdev);
1198
1199 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001200 if (conn && hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02001201 struct hci_cp_auth_requested cp;
1202 cp.handle = __cpu_to_le16(conn->handle);
1203 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
1204 }
1205
1206 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001207}
1208
Marcel Holtmann769be972008-07-14 20:13:49 +02001209static void hci_cs_read_remote_features(struct hci_dev *hdev, __u8 status)
1210{
1211 struct hci_cp_read_remote_features *cp;
1212 struct hci_conn *conn;
1213
1214 BT_DBG("%s status 0x%x", hdev->name, status);
1215
1216 if (!status)
1217 return;
1218
1219 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_FEATURES);
1220 if (!cp)
1221 return;
1222
1223 hci_dev_lock(hdev);
1224
1225 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1226 if (conn) {
1227 if (conn->state == BT_CONFIG) {
Marcel Holtmann769be972008-07-14 20:13:49 +02001228 hci_proto_connect_cfm(conn, status);
1229 hci_conn_put(conn);
1230 }
1231 }
1232
1233 hci_dev_unlock(hdev);
1234}
1235
1236static void hci_cs_read_remote_ext_features(struct hci_dev *hdev, __u8 status)
1237{
1238 struct hci_cp_read_remote_ext_features *cp;
1239 struct hci_conn *conn;
1240
1241 BT_DBG("%s status 0x%x", hdev->name, status);
1242
1243 if (!status)
1244 return;
1245
1246 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES);
1247 if (!cp)
1248 return;
1249
1250 hci_dev_lock(hdev);
1251
1252 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1253 if (conn) {
1254 if (conn->state == BT_CONFIG) {
Marcel Holtmann769be972008-07-14 20:13:49 +02001255 hci_proto_connect_cfm(conn, status);
1256 hci_conn_put(conn);
1257 }
1258 }
1259
1260 hci_dev_unlock(hdev);
1261}
1262
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001263static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
1264{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001265 struct hci_cp_setup_sync_conn *cp;
1266 struct hci_conn *acl, *sco;
1267 __u16 handle;
1268
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001269 BT_DBG("%s status 0x%x", hdev->name, status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001270
1271 if (!status)
1272 return;
1273
1274 cp = hci_sent_cmd_data(hdev, HCI_OP_SETUP_SYNC_CONN);
1275 if (!cp)
1276 return;
1277
1278 handle = __le16_to_cpu(cp->handle);
1279
1280 BT_DBG("%s handle %d", hdev->name, handle);
1281
1282 hci_dev_lock(hdev);
1283
1284 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001285 if (acl) {
1286 sco = acl->link;
1287 if (sco) {
1288 sco->state = BT_CLOSED;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001289
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001290 hci_proto_connect_cfm(sco, status);
1291 hci_conn_del(sco);
1292 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001293 }
1294
1295 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001296}
1297
1298static void hci_cs_sniff_mode(struct hci_dev *hdev, __u8 status)
1299{
1300 struct hci_cp_sniff_mode *cp;
1301 struct hci_conn *conn;
1302
1303 BT_DBG("%s status 0x%x", hdev->name, status);
1304
1305 if (!status)
1306 return;
1307
1308 cp = hci_sent_cmd_data(hdev, HCI_OP_SNIFF_MODE);
1309 if (!cp)
1310 return;
1311
1312 hci_dev_lock(hdev);
1313
1314 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001315 if (conn) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001316 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend);
1317
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001318 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->pend))
1319 hci_sco_setup(conn, status);
1320 }
1321
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001322 hci_dev_unlock(hdev);
1323}
1324
1325static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
1326{
1327 struct hci_cp_exit_sniff_mode *cp;
1328 struct hci_conn *conn;
1329
1330 BT_DBG("%s status 0x%x", hdev->name, status);
1331
1332 if (!status)
1333 return;
1334
1335 cp = hci_sent_cmd_data(hdev, HCI_OP_EXIT_SNIFF_MODE);
1336 if (!cp)
1337 return;
1338
1339 hci_dev_lock(hdev);
1340
1341 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001342 if (conn) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001343 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend);
1344
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001345 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->pend))
1346 hci_sco_setup(conn, status);
1347 }
1348
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001349 hci_dev_unlock(hdev);
1350}
1351
Ville Tervofcd89c02011-02-10 22:38:47 -03001352static void hci_cs_le_create_conn(struct hci_dev *hdev, __u8 status)
1353{
1354 struct hci_cp_le_create_conn *cp;
1355 struct hci_conn *conn;
1356
1357 BT_DBG("%s status 0x%x", hdev->name, status);
1358
1359 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_CREATE_CONN);
1360 if (!cp)
1361 return;
1362
1363 hci_dev_lock(hdev);
1364
1365 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->peer_addr);
1366
1367 BT_DBG("%s bdaddr %s conn %p", hdev->name, batostr(&cp->peer_addr),
1368 conn);
1369
1370 if (status) {
1371 if (conn && conn->state == BT_CONNECT) {
1372 conn->state = BT_CLOSED;
1373 hci_proto_connect_cfm(conn, status);
1374 hci_conn_del(conn);
1375 }
1376 } else {
1377 if (!conn) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001378 conn = hci_le_conn_add(hdev, &cp->peer_addr,
1379 cp->peer_addr_type);
1380 if (conn)
Ville Tervofcd89c02011-02-10 22:38:47 -03001381 conn->out = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001382 else
Ville Tervofcd89c02011-02-10 22:38:47 -03001383 BT_ERR("No memory for new connection");
1384 }
Brian Gix114f3a62011-09-27 14:02:20 -07001385
1386 if (conn)
1387 mod_timer(&conn->disc_timer,
1388 jiffies + msecs_to_jiffies(5000));
Ville Tervofcd89c02011-02-10 22:38:47 -03001389 }
1390
1391 hci_dev_unlock(hdev);
1392}
1393
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001394static void hci_cs_accept_logical_link(struct hci_dev *hdev, __u8 status)
1395{
1396 struct hci_cp_create_logical_link *ap;
1397 struct hci_chan *chan;
1398
1399 BT_DBG("%s status 0x%x", hdev->name, status);
1400
1401 ap = hci_sent_cmd_data(hdev, HCI_OP_ACCEPT_LOGICAL_LINK);
1402 if (!ap)
1403 return;
1404
1405 hci_dev_lock(hdev);
1406
1407 chan = hci_chan_list_lookup_id(hdev, ap->phy_handle);
1408
1409 BT_DBG("%s chan %p", hdev->name, chan);
1410
1411 if (status) {
1412 if (chan && chan->state == BT_CONNECT) {
1413 chan->state = BT_CLOSED;
1414 hci_proto_create_cfm(chan, status);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001415 }
Mat Martineau9f8d4672011-12-14 12:10:46 -08001416 } else if (chan) {
1417 chan->state = BT_CONNECT2;
1418 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001419
1420 hci_dev_unlock(hdev);
1421}
1422
1423static void hci_cs_create_logical_link(struct hci_dev *hdev, __u8 status)
1424{
1425 struct hci_cp_create_logical_link *cp;
1426 struct hci_chan *chan;
1427
1428 BT_DBG("%s status 0x%x", hdev->name, status);
1429
1430 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_LOGICAL_LINK);
1431 if (!cp)
1432 return;
1433
1434 hci_dev_lock(hdev);
1435
1436 chan = hci_chan_list_lookup_id(hdev, cp->phy_handle);
1437
1438 BT_DBG("%s chan %p", hdev->name, chan);
1439
1440 if (status) {
1441 if (chan && chan->state == BT_CONNECT) {
1442 chan->state = BT_CLOSED;
1443 hci_proto_create_cfm(chan, status);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001444 }
1445 } else if (chan)
1446 chan->state = BT_CONNECT2;
1447
1448 hci_dev_unlock(hdev);
1449}
1450
1451static void hci_cs_flow_spec_modify(struct hci_dev *hdev, __u8 status)
1452{
1453 struct hci_cp_flow_spec_modify *cp;
1454 struct hci_chan *chan;
1455
1456 BT_DBG("%s status 0x%x", hdev->name, status);
1457
1458 cp = hci_sent_cmd_data(hdev, HCI_OP_FLOW_SPEC_MODIFY);
1459 if (!cp)
1460 return;
1461
1462 hci_dev_lock(hdev);
1463
1464 chan = hci_chan_list_lookup_handle(hdev, cp->log_handle);
1465 if (chan) {
1466 if (status)
1467 hci_proto_modify_cfm(chan, status);
1468 else {
1469 chan->tx_fs = cp->tx_fs;
1470 chan->rx_fs = cp->rx_fs;
1471 }
1472 }
1473
1474 hci_dev_unlock(hdev);
1475}
1476
1477static void hci_cs_disconn_logical_link(struct hci_dev *hdev, __u8 status)
1478{
1479 struct hci_cp_disconn_logical_link *cp;
1480 struct hci_chan *chan;
1481
1482 if (!status)
1483 return;
1484
1485 BT_DBG("%s status 0x%x", hdev->name, status);
1486
1487 cp = hci_sent_cmd_data(hdev, HCI_OP_DISCONN_LOGICAL_LINK);
1488 if (!cp)
1489 return;
1490
1491 hci_dev_lock(hdev);
1492
1493 chan = hci_chan_list_lookup_handle(hdev, cp->log_handle);
1494 if (chan)
1495 hci_chan_del(chan);
1496
1497 hci_dev_unlock(hdev);
1498}
1499
1500static void hci_cs_disconn_physical_link(struct hci_dev *hdev, __u8 status)
1501{
1502 struct hci_cp_disconn_phys_link *cp;
1503 struct hci_conn *conn;
1504
1505 if (!status)
1506 return;
1507
1508 BT_DBG("%s status 0x%x", hdev->name, status);
1509
1510 cp = hci_sent_cmd_data(hdev, HCI_OP_DISCONN_PHYS_LINK);
1511 if (!cp)
1512 return;
1513
1514 hci_dev_lock(hdev);
1515
1516 conn = hci_conn_hash_lookup_handle(hdev, cp->phy_handle);
1517 if (conn) {
1518 conn->state = BT_CLOSED;
1519 hci_conn_del(conn);
1520 }
1521
1522 hci_dev_unlock(hdev);
1523}
1524
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03001525static void hci_cs_le_start_enc(struct hci_dev *hdev, u8 status)
1526{
1527 BT_DBG("%s status 0x%x", hdev->name, status);
1528}
1529
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001530static inline void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1531{
1532 __u8 status = *((__u8 *) skb->data);
1533
1534 BT_DBG("%s status %d", hdev->name, status);
1535
Brian Gixa68668b2011-08-11 15:49:36 -07001536 if (!lmp_le_capable(hdev))
1537 clear_bit(HCI_INQUIRY, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001538
Johan Hedberg23bb5762010-12-21 23:01:27 +02001539 hci_req_complete(hdev, HCI_OP_INQUIRY, status);
Subramanian Srinivasana727a492011-11-30 13:06:07 -08001540 hci_dev_lock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001541
Brian Gixa68668b2011-08-11 15:49:36 -07001542 if (test_bit(HCI_MGMT, &hdev->flags))
1543 mgmt_inquiry_complete_evt(hdev->id, status);
Subramanian Srinivasana727a492011-11-30 13:06:07 -08001544 hci_dev_unlock(hdev);
Brian Gixa68668b2011-08-11 15:49:36 -07001545
1546 if (!lmp_le_capable(hdev))
1547 hci_conn_check_pending(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001548}
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550static inline void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
1551{
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001552 struct inquiry_data data;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001553 struct inquiry_info *info = (void *) (skb->data + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 int num_rsp = *((__u8 *) skb->data);
1555
1556 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
1557
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001558 if (!num_rsp)
1559 return;
1560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001562
Johan Hedberge17acd42011-03-30 23:57:16 +03001563 for (; num_rsp; num_rsp--, info++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 bacpy(&data.bdaddr, &info->bdaddr);
1565 data.pscan_rep_mode = info->pscan_rep_mode;
1566 data.pscan_period_mode = info->pscan_period_mode;
1567 data.pscan_mode = info->pscan_mode;
1568 memcpy(data.dev_class, info->dev_class, 3);
1569 data.clock_offset = info->clock_offset;
1570 data.rssi = 0x00;
Marcel Holtmann41a96212008-07-14 20:13:48 +02001571 data.ssp_mode = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 hci_inquiry_cache_update(hdev, &data);
Brian Gixa68668b2011-08-11 15:49:36 -07001573 mgmt_device_found(hdev->id, &info->bdaddr, 0, 0,
1574 info->dev_class, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 hci_dev_unlock(hdev);
1578}
1579
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001580static inline void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001582 struct hci_ev_conn_complete *ev = (void *) skb->data;
1583 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001585 BT_DBG("%s", hdev->name);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001588
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001589 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann94992372009-04-19 19:30:03 +02001590 if (!conn) {
1591 if (ev->link_type != SCO_LINK)
1592 goto unlock;
1593
1594 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
1595 if (!conn)
1596 goto unlock;
1597
1598 conn->type = SCO_LINK;
1599 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001600
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001601 if (!ev->status) {
1602 conn->handle = __le16_to_cpu(ev->handle);
Marcel Holtmann769be972008-07-14 20:13:49 +02001603
1604 if (conn->type == ACL_LINK) {
1605 conn->state = BT_CONFIG;
1606 hci_conn_hold(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02001607 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Brian Gix2e2f50d2011-09-13 12:36:04 -07001608 mgmt_connected(hdev->id, &ev->bdaddr, 0);
Brian Gixa68668b2011-08-11 15:49:36 -07001609 } else if (conn->type == LE_LINK) {
1610 conn->state = BT_CONNECTED;
1611 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Brian Gix2e2f50d2011-09-13 12:36:04 -07001612 mgmt_connected(hdev->id, &ev->bdaddr, 1);
Marcel Holtmann769be972008-07-14 20:13:49 +02001613 } else
1614 conn->state = BT_CONNECTED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001615
Marcel Holtmann9eba32b2009-08-22 14:19:26 -07001616 hci_conn_hold_device(conn);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02001617 hci_conn_add_sysfs(conn);
1618
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001619 if (test_bit(HCI_AUTH, &hdev->flags))
1620 conn->link_mode |= HCI_LM_AUTH;
1621
1622 if (test_bit(HCI_ENCRYPT, &hdev->flags))
1623 conn->link_mode |= HCI_LM_ENCRYPT;
1624
Srinivas Krovvidid352b262012-01-12 19:46:26 +05301625 /* Get remote version */
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001626 if (conn->type == ACL_LINK) {
Srinivas Krovvidid352b262012-01-12 19:46:26 +05301627 struct hci_cp_read_remote_version cp;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001628 cp.handle = ev->handle;
Srinivas Krovvidid352b262012-01-12 19:46:26 +05301629 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_VERSION,
1630 sizeof(cp), &cp);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001631 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001632
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001633 /* Set packet type for incoming connection */
Marcel Holtmanna8746412008-07-14 20:13:46 +02001634 if (!conn->out && hdev->hci_ver < 3) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001635 struct hci_cp_change_conn_ptype cp;
1636 cp.handle = ev->handle;
Marcel Holtmanna8746412008-07-14 20:13:46 +02001637 cp.pkt_type = cpu_to_le16(conn->pkt_type);
1638 hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE,
1639 sizeof(cp), &cp);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001640 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02001641 } else {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001642 conn->state = BT_CLOSED;
Brian Gixa68668b2011-08-11 15:49:36 -07001643 if (conn->type == ACL_LINK || conn->type == LE_LINK)
Johan Hedberg17d5c042011-01-22 06:09:08 +02001644 mgmt_connect_failed(hdev->id, &ev->bdaddr, ev->status);
1645 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001646
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001647 if (conn->type == ACL_LINK)
1648 hci_sco_setup(conn, ev->status);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001649
Marcel Holtmann769be972008-07-14 20:13:49 +02001650 if (ev->status) {
1651 hci_proto_connect_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001652 hci_conn_del(conn);
Marcel Holtmannc89b6e62009-01-15 21:57:03 +01001653 } else if (ev->link_type != ACL_LINK)
1654 hci_proto_connect_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001655
1656unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001658
1659 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660}
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662static inline void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
1663{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001664 struct hci_ev_conn_request *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 int mask = hdev->link_mode;
1666
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001667 BT_DBG("%s bdaddr %s type 0x%x", hdev->name,
1668 batostr(&ev->bdaddr), ev->link_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type);
1671
Szymon Janc138d22e2011-02-17 16:44:23 +01001672 if ((mask & HCI_LM_ACCEPT) &&
1673 !hci_blacklist_lookup(hdev, &ev->bdaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 /* Connection accepted */
Marcel Holtmannc7bdd502008-07-14 20:13:47 +02001675 struct inquiry_entry *ie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
1678 hci_dev_lock(hdev);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001679
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02001680 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
1681 if (ie)
Marcel Holtmannc7bdd502008-07-14 20:13:47 +02001682 memcpy(ie->data.dev_class, ev->dev_class, 3);
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
1685 if (!conn) {
Nick Pellybbcda3b2010-02-11 11:54:28 -08001686 /* pkt_type not yet used for incoming connections */
1687 conn = hci_conn_add(hdev, ev->link_type, 0, &ev->bdaddr);
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02001688 if (!conn) {
Gustavo F. Padovan893ef972010-07-18 15:13:37 -03001689 BT_ERR("No memory for new connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 hci_dev_unlock(hdev);
1691 return;
1692 }
1693 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 memcpy(conn->dev_class, ev->dev_class, 3);
Srinivas Krovvidi0916aed2011-12-20 12:06:34 +05301696 /* For incoming connection update remote class to userspace */
1697 mgmt_remote_class(hdev->id, &ev->bdaddr, ev->dev_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 conn->state = BT_CONNECT;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 hci_dev_unlock(hdev);
1701
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001702 if (ev->link_type == ACL_LINK || !lmp_esco_capable(hdev)) {
1703 struct hci_cp_accept_conn_req cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001705 bacpy(&cp.bdaddr, &ev->bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001707 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
1708 cp.role = 0x00; /* Become master */
1709 else
1710 cp.role = 0x01; /* Remain slave */
1711
1712 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ,
1713 sizeof(cp), &cp);
1714 } else {
1715 struct hci_cp_accept_sync_conn_req cp;
1716
1717 bacpy(&cp.bdaddr, &ev->bdaddr);
Marcel Holtmanna8746412008-07-14 20:13:46 +02001718 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001719
1720 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
1721 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001722 cp.max_latency = cpu_to_le16(0x000A);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001723 cp.content_format = cpu_to_le16(hdev->voice_setting);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001724 cp.retrans_effort = 0x01;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001725
1726 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ,
1727 sizeof(cp), &cp);
1728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 } else {
1730 /* Connection rejected */
1731 struct hci_cp_reject_conn_req cp;
1732
1733 bacpy(&cp.bdaddr, &ev->bdaddr);
1734 cp.reason = 0x0f;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001735 hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 }
1737}
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739static inline void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1740{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001741 struct hci_ev_disconn_complete *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02001742 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 BT_DBG("%s status %d", hdev->name, ev->status);
1745
Johan Hedberg8962ee72011-01-20 12:40:27 +02001746 if (ev->status) {
Subramanian Srinivasana727a492011-11-30 13:06:07 -08001747 hci_dev_lock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02001748 mgmt_disconnect_failed(hdev->id);
Subramanian Srinivasana727a492011-11-30 13:06:07 -08001749 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return;
Johan Hedberg8962ee72011-01-20 12:40:27 +02001751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 hci_dev_lock(hdev);
1754
Marcel Holtmann04837f62006-07-03 10:02:33 +02001755 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergf7520542011-01-20 12:34:39 +02001756 if (!conn)
1757 goto unlock;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02001758
Johan Hedbergf7520542011-01-20 12:34:39 +02001759 conn->state = BT_CLOSED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Brian Gixa68668b2011-08-11 15:49:36 -07001761 if (conn->type == ACL_LINK || conn->type == LE_LINK)
Johan Hedbergf7520542011-01-20 12:34:39 +02001762 mgmt_disconnected(hdev->id, &conn->dst);
1763
Brian Gixe9ceb522011-09-22 10:46:35 -07001764 if (conn->type == LE_LINK)
1765 del_timer(&conn->smp_timer);
1766
Mat Martineau3b9239a2012-02-16 11:54:30 -08001767 hci_proto_disconn_cfm(conn, ev->reason, 0);
Johan Hedbergf7520542011-01-20 12:34:39 +02001768 hci_conn_del(conn);
1769
1770unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 hci_dev_unlock(hdev);
1772}
1773
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001774static inline void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1775{
1776 struct hci_ev_auth_complete *ev = (void *) skb->data;
1777 struct hci_conn *conn;
1778
1779 BT_DBG("%s status %d", hdev->name, ev->status);
1780
1781 hci_dev_lock(hdev);
1782
1783 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001784 if (conn) {
1785 if (ev->status == 0x06 && hdev->ssp_mode > 0 &&
1786 conn->ssp_mode > 0) {
1787 struct hci_cp_auth_requested cp;
Prabhakaran Mcb04401d2011-09-20 17:37:55 +05301788 hci_remove_link_key(hdev, &conn->dst);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001789 cp.handle = cpu_to_le16(conn->handle);
1790 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
1791 sizeof(cp), &cp);
1792 hci_dev_unlock(hdev);
1793 BT_INFO("Pin or key missing");
1794 return;
1795 }
Waldemar Rymarkiewicz54444292011-05-31 15:49:26 +02001796
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001797 if (!ev->status) {
Waldemar Rymarkiewicz54444292011-05-31 15:49:26 +02001798 conn->link_mode |= HCI_LM_AUTH;
1799 conn->sec_level = conn->pending_sec_level;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02001800 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001801 mgmt_auth_failed(hdev->id, &conn->dst, ev->status);
1802 conn->sec_level = BT_SECURITY_LOW;
1803 }
1804
1805 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
1806
1807 if (conn->state == BT_CONFIG) {
1808 if (!ev->status && hdev->ssp_mode > 0 &&
1809 conn->ssp_mode > 0) {
1810 struct hci_cp_set_conn_encrypt cp;
1811 cp.handle = ev->handle;
1812 cp.encrypt = 0x01;
1813 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT,
1814 sizeof(cp), &cp);
1815 } else {
1816 conn->state = BT_CONNECTED;
1817 hci_proto_connect_cfm(conn, ev->status);
1818 hci_conn_put(conn);
1819 }
1820 } else {
1821 hci_auth_cfm(conn, ev->status);
1822
1823 hci_conn_hold(conn);
1824 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02001825 hci_conn_put(conn);
1826 }
1827
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001828 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
1829 if (!ev->status) {
1830 struct hci_cp_set_conn_encrypt cp;
1831 cp.handle = ev->handle;
1832 cp.encrypt = 0x01;
1833 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT,
1834 sizeof(cp), &cp);
1835 } else {
1836 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
1837 hci_encrypt_cfm(conn, ev->status, 0x00);
1838 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001839 }
1840 }
1841
1842 hci_dev_unlock(hdev);
1843}
1844
1845static inline void hci_remote_name_evt(struct hci_dev *hdev, struct sk_buff *skb)
1846{
Johan Hedberg127178d2010-11-18 22:22:29 +02001847 struct hci_ev_remote_name *ev = (void *) skb->data;
1848 struct hci_conn *conn;
1849
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001850 BT_DBG("%s", hdev->name);
1851
1852 hci_conn_check_pending(hdev);
Johan Hedberg127178d2010-11-18 22:22:29 +02001853
1854 hci_dev_lock(hdev);
1855
Brian Gixa68668b2011-08-11 15:49:36 -07001856 if (test_bit(HCI_MGMT, &hdev->flags))
1857 mgmt_remote_name(hdev->id, &ev->bdaddr, ev->status, ev->name);
Johan Hedberga88a9652011-03-30 13:18:12 +03001858
Johan Hedberg127178d2010-11-18 22:22:29 +02001859 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001860 if (conn && hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02001861 struct hci_cp_auth_requested cp;
1862 cp.handle = __cpu_to_le16(conn->handle);
1863 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
1864 }
1865
1866 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001867}
1868
1869static inline void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
1870{
1871 struct hci_ev_encrypt_change *ev = (void *) skb->data;
1872 struct hci_conn *conn;
1873
1874 BT_DBG("%s status %d", hdev->name, ev->status);
1875
1876 hci_dev_lock(hdev);
1877
1878 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1879 if (conn) {
1880 if (!ev->status) {
Marcel Holtmannae293192008-07-14 20:13:45 +02001881 if (ev->encrypt) {
1882 /* Encryption implies authentication */
1883 conn->link_mode |= HCI_LM_AUTH;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001884 conn->link_mode |= HCI_LM_ENCRYPT;
Vinicius Costa Gomes64532ec2011-06-09 18:50:53 -03001885 conn->sec_level = conn->pending_sec_level;
Marcel Holtmannae293192008-07-14 20:13:45 +02001886 } else
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001887 conn->link_mode &= ~HCI_LM_ENCRYPT;
1888 }
1889
1890 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
1891
Marcel Holtmannf8558552008-07-14 20:13:49 +02001892 if (conn->state == BT_CONFIG) {
1893 if (!ev->status)
1894 conn->state = BT_CONNECTED;
1895
1896 hci_proto_connect_cfm(conn, ev->status);
1897 hci_conn_put(conn);
1898 } else
1899 hci_encrypt_cfm(conn, ev->status, ev->encrypt);
Prabhakaran Mc46230fa2011-11-30 18:11:21 +05301900
1901 if (test_bit(HCI_MGMT, &hdev->flags))
1902 mgmt_encrypt_change(hdev->id, &conn->dst, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001903 }
1904
1905 hci_dev_unlock(hdev);
1906}
1907
1908static inline void hci_change_link_key_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1909{
1910 struct hci_ev_change_link_key_complete *ev = (void *) skb->data;
1911 struct hci_conn *conn;
1912
1913 BT_DBG("%s status %d", hdev->name, ev->status);
1914
1915 hci_dev_lock(hdev);
1916
1917 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1918 if (conn) {
1919 if (!ev->status)
1920 conn->link_mode |= HCI_LM_SECURE;
1921
1922 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
1923
1924 hci_key_change_cfm(conn, ev->status);
1925 }
1926
1927 hci_dev_unlock(hdev);
1928}
1929
1930static inline void hci_remote_features_evt(struct hci_dev *hdev, struct sk_buff *skb)
1931{
1932 struct hci_ev_remote_features *ev = (void *) skb->data;
1933 struct hci_conn *conn;
1934
1935 BT_DBG("%s status %d", hdev->name, ev->status);
1936
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001937 hci_dev_lock(hdev);
1938
1939 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02001940 if (!conn)
1941 goto unlock;
Marcel Holtmann769be972008-07-14 20:13:49 +02001942
Johan Hedbergccd556f2010-11-10 17:11:51 +02001943 if (!ev->status)
1944 memcpy(conn->features, ev->features, 8);
1945
1946 if (conn->state != BT_CONFIG)
1947 goto unlock;
1948
1949 if (!ev->status && lmp_ssp_capable(hdev) && lmp_ssp_capable(conn)) {
1950 struct hci_cp_read_remote_ext_features cp;
1951 cp.handle = ev->handle;
1952 cp.page = 0x01;
1953 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES,
Marcel Holtmann769be972008-07-14 20:13:49 +02001954 sizeof(cp), &cp);
Johan Hedberg392599b2010-11-18 22:22:28 +02001955 goto unlock;
1956 }
1957
Johan Hedberg127178d2010-11-18 22:22:29 +02001958 if (!ev->status) {
1959 struct hci_cp_remote_name_req cp;
1960 memset(&cp, 0, sizeof(cp));
1961 bacpy(&cp.bdaddr, &conn->dst);
1962 cp.pscan_rep_mode = 0x02;
1963 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
1964 }
Johan Hedberg392599b2010-11-18 22:22:28 +02001965
Johan Hedberg127178d2010-11-18 22:22:29 +02001966 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02001967 conn->state = BT_CONNECTED;
1968 hci_proto_connect_cfm(conn, ev->status);
1969 hci_conn_put(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02001970 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001971
Johan Hedbergccd556f2010-11-10 17:11:51 +02001972unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001973 hci_dev_unlock(hdev);
1974}
1975
1976static inline void hci_remote_version_evt(struct hci_dev *hdev, struct sk_buff *skb)
1977{
Srinivas Krovvidid352b262012-01-12 19:46:26 +05301978 struct hci_ev_remote_version *ev = (void *) skb->data;
1979 struct hci_cp_read_remote_features cp;
1980 struct hci_conn *conn;
1981 BT_DBG("%s status %d", hdev->name, ev->status);
1982
1983 hci_dev_lock(hdev);
1984 cp.handle = ev->handle;
1985 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES,
1986 sizeof(cp), &cp);
1987
1988 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1989 if (!conn)
1990 goto unlock;
1991 if (!ev->status)
1992 mgmt_remote_version(hdev->id, &conn->dst, ev->lmp_ver,
1993 ev->manufacturer, ev->lmp_subver);
1994unlock:
1995 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001996}
1997
1998static inline void hci_qos_setup_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1999{
2000 BT_DBG("%s", hdev->name);
2001}
2002
2003static inline void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
2004{
2005 struct hci_ev_cmd_complete *ev = (void *) skb->data;
2006 __u16 opcode;
2007
2008 skb_pull(skb, sizeof(*ev));
2009
2010 opcode = __le16_to_cpu(ev->opcode);
2011
2012 switch (opcode) {
2013 case HCI_OP_INQUIRY_CANCEL:
2014 hci_cc_inquiry_cancel(hdev, skb);
2015 break;
2016
2017 case HCI_OP_EXIT_PERIODIC_INQ:
2018 hci_cc_exit_periodic_inq(hdev, skb);
2019 break;
2020
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002021 case HCI_OP_LINK_KEY_REPLY:
2022 hci_cc_link_key_reply(hdev, skb);
2023 break;
2024
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002025 case HCI_OP_REMOTE_NAME_REQ_CANCEL:
2026 hci_cc_remote_name_req_cancel(hdev, skb);
2027 break;
2028
2029 case HCI_OP_ROLE_DISCOVERY:
2030 hci_cc_role_discovery(hdev, skb);
2031 break;
2032
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02002033 case HCI_OP_READ_LINK_POLICY:
2034 hci_cc_read_link_policy(hdev, skb);
2035 break;
2036
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002037 case HCI_OP_WRITE_LINK_POLICY:
2038 hci_cc_write_link_policy(hdev, skb);
2039 break;
2040
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02002041 case HCI_OP_READ_DEF_LINK_POLICY:
2042 hci_cc_read_def_link_policy(hdev, skb);
2043 break;
2044
2045 case HCI_OP_WRITE_DEF_LINK_POLICY:
2046 hci_cc_write_def_link_policy(hdev, skb);
2047 break;
2048
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002049 case HCI_OP_RESET:
2050 hci_cc_reset(hdev, skb);
2051 break;
2052
2053 case HCI_OP_WRITE_LOCAL_NAME:
2054 hci_cc_write_local_name(hdev, skb);
2055 break;
2056
2057 case HCI_OP_READ_LOCAL_NAME:
2058 hci_cc_read_local_name(hdev, skb);
2059 break;
2060
2061 case HCI_OP_WRITE_AUTH_ENABLE:
2062 hci_cc_write_auth_enable(hdev, skb);
2063 break;
2064
2065 case HCI_OP_WRITE_ENCRYPT_MODE:
2066 hci_cc_write_encrypt_mode(hdev, skb);
2067 break;
2068
2069 case HCI_OP_WRITE_SCAN_ENABLE:
2070 hci_cc_write_scan_enable(hdev, skb);
2071 break;
2072
2073 case HCI_OP_READ_CLASS_OF_DEV:
2074 hci_cc_read_class_of_dev(hdev, skb);
2075 break;
2076
2077 case HCI_OP_WRITE_CLASS_OF_DEV:
2078 hci_cc_write_class_of_dev(hdev, skb);
2079 break;
2080
2081 case HCI_OP_READ_VOICE_SETTING:
2082 hci_cc_read_voice_setting(hdev, skb);
2083 break;
2084
2085 case HCI_OP_WRITE_VOICE_SETTING:
2086 hci_cc_write_voice_setting(hdev, skb);
2087 break;
2088
2089 case HCI_OP_HOST_BUFFER_SIZE:
2090 hci_cc_host_buffer_size(hdev, skb);
2091 break;
2092
Marcel Holtmann333140b2008-07-14 20:13:48 +02002093 case HCI_OP_READ_SSP_MODE:
2094 hci_cc_read_ssp_mode(hdev, skb);
2095 break;
2096
2097 case HCI_OP_WRITE_SSP_MODE:
2098 hci_cc_write_ssp_mode(hdev, skb);
2099 break;
2100
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002101 case HCI_OP_READ_LOCAL_VERSION:
2102 hci_cc_read_local_version(hdev, skb);
2103 break;
2104
2105 case HCI_OP_READ_LOCAL_COMMANDS:
2106 hci_cc_read_local_commands(hdev, skb);
2107 break;
2108
2109 case HCI_OP_READ_LOCAL_FEATURES:
2110 hci_cc_read_local_features(hdev, skb);
2111 break;
2112
2113 case HCI_OP_READ_BUFFER_SIZE:
2114 hci_cc_read_buffer_size(hdev, skb);
2115 break;
2116
2117 case HCI_OP_READ_BD_ADDR:
2118 hci_cc_read_bd_addr(hdev, skb);
2119 break;
2120
Johan Hedberg23bb5762010-12-21 23:01:27 +02002121 case HCI_OP_WRITE_CA_TIMEOUT:
2122 hci_cc_write_ca_timeout(hdev, skb);
2123 break;
2124
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002125 case HCI_OP_READ_FLOW_CONTROL_MODE:
2126 hci_cc_read_flow_control_mode(hdev, skb);
2127 break;
2128
2129 case HCI_OP_READ_DATA_BLOCK_SIZE:
2130 hci_cc_read_data_block_size(hdev, skb);
2131 break;
2132
2133 case HCI_OP_READ_LOCAL_AMP_INFO:
2134 hci_cc_read_local_amp_info(hdev, skb);
2135 break;
2136
2137 case HCI_OP_READ_LOCAL_AMP_ASSOC:
2138 case HCI_OP_WRITE_REMOTE_AMP_ASSOC:
2139 hci_amp_cmd_complete(hdev, opcode, skb);
2140 break;
2141
Johan Hedbergb0916ea2011-01-10 13:44:55 +02002142 case HCI_OP_DELETE_STORED_LINK_KEY:
2143 hci_cc_delete_stored_link_key(hdev, skb);
2144 break;
2145
Johan Hedbergd5859e22011-01-25 01:19:58 +02002146 case HCI_OP_SET_EVENT_MASK:
2147 hci_cc_set_event_mask(hdev, skb);
2148 break;
2149
2150 case HCI_OP_WRITE_INQUIRY_MODE:
2151 hci_cc_write_inquiry_mode(hdev, skb);
2152 break;
2153
2154 case HCI_OP_READ_INQ_RSP_TX_POWER:
2155 hci_cc_read_inq_rsp_tx_power(hdev, skb);
2156 break;
2157
2158 case HCI_OP_SET_EVENT_FLT:
2159 hci_cc_set_event_flt(hdev, skb);
2160 break;
2161
Johan Hedberg980e1a52011-01-22 06:10:07 +02002162 case HCI_OP_PIN_CODE_REPLY:
2163 hci_cc_pin_code_reply(hdev, skb);
2164 break;
2165
2166 case HCI_OP_PIN_CODE_NEG_REPLY:
2167 hci_cc_pin_code_neg_reply(hdev, skb);
2168 break;
2169
Szymon Jancc35938b2011-03-22 13:12:21 +01002170 case HCI_OP_READ_LOCAL_OOB_DATA:
2171 hci_cc_read_local_oob_data_reply(hdev, skb);
2172 break;
2173
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002174 case HCI_OP_LE_READ_BUFFER_SIZE:
2175 hci_cc_le_read_buffer_size(hdev, skb);
2176 break;
2177
Johan Hedberga5c29682011-02-19 12:05:57 -03002178 case HCI_OP_USER_CONFIRM_REPLY:
2179 hci_cc_user_confirm_reply(hdev, skb);
2180 break;
2181
2182 case HCI_OP_USER_CONFIRM_NEG_REPLY:
2183 hci_cc_user_confirm_neg_reply(hdev, skb);
2184 break;
2185
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03002186 case HCI_OP_LE_LTK_REPLY:
2187 hci_cc_le_ltk_reply(hdev, skb);
2188 break;
2189
2190 case HCI_OP_LE_LTK_NEG_REPLY:
2191 hci_cc_le_ltk_neg_reply(hdev, skb);
2192 break;
2193
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002194 case HCI_OP_LE_SET_SCAN_ENABLE:
2195 hci_cc_le_set_scan_enable(hdev, skb);
Andre Guedese326af42011-06-30 19:20:53 -03002196 break;
2197
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002198 default:
2199 BT_DBG("%s opcode 0x%x", hdev->name, opcode);
2200 break;
2201 }
2202
Ville Tervo6bd32322011-02-16 16:32:41 +02002203 if (ev->opcode != HCI_OP_NOP)
2204 del_timer(&hdev->cmd_timer);
2205
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002206 if (ev->ncmd) {
2207 atomic_set(&hdev->cmd_cnt, 1);
2208 if (!skb_queue_empty(&hdev->cmd_q))
Marcel Holtmannc78ae282009-11-18 01:02:54 +01002209 tasklet_schedule(&hdev->cmd_task);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002210 }
2211}
2212
2213static inline void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb)
2214{
2215 struct hci_ev_cmd_status *ev = (void *) skb->data;
2216 __u16 opcode;
2217
2218 skb_pull(skb, sizeof(*ev));
2219
2220 opcode = __le16_to_cpu(ev->opcode);
2221
2222 switch (opcode) {
2223 case HCI_OP_INQUIRY:
2224 hci_cs_inquiry(hdev, ev->status);
2225 break;
2226
2227 case HCI_OP_CREATE_CONN:
2228 hci_cs_create_conn(hdev, ev->status);
2229 break;
2230
2231 case HCI_OP_ADD_SCO:
2232 hci_cs_add_sco(hdev, ev->status);
2233 break;
2234
Marcel Holtmannf8558552008-07-14 20:13:49 +02002235 case HCI_OP_AUTH_REQUESTED:
2236 hci_cs_auth_requested(hdev, ev->status);
2237 break;
2238
2239 case HCI_OP_SET_CONN_ENCRYPT:
2240 hci_cs_set_conn_encrypt(hdev, ev->status);
2241 break;
2242
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002243 case HCI_OP_REMOTE_NAME_REQ:
2244 hci_cs_remote_name_req(hdev, ev->status);
2245 break;
2246
Marcel Holtmann769be972008-07-14 20:13:49 +02002247 case HCI_OP_READ_REMOTE_FEATURES:
2248 hci_cs_read_remote_features(hdev, ev->status);
2249 break;
2250
2251 case HCI_OP_READ_REMOTE_EXT_FEATURES:
2252 hci_cs_read_remote_ext_features(hdev, ev->status);
2253 break;
2254
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002255 case HCI_OP_SETUP_SYNC_CONN:
2256 hci_cs_setup_sync_conn(hdev, ev->status);
2257 break;
2258
2259 case HCI_OP_SNIFF_MODE:
2260 hci_cs_sniff_mode(hdev, ev->status);
2261 break;
2262
2263 case HCI_OP_EXIT_SNIFF_MODE:
2264 hci_cs_exit_sniff_mode(hdev, ev->status);
2265 break;
2266
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002267 case HCI_OP_CREATE_LOGICAL_LINK:
2268 hci_cs_create_logical_link(hdev, ev->status);
2269 break;
2270
2271 case HCI_OP_ACCEPT_LOGICAL_LINK:
2272 hci_cs_accept_logical_link(hdev, ev->status);
2273 break;
2274
2275 case HCI_OP_DISCONN_LOGICAL_LINK:
2276 hci_cs_disconn_logical_link(hdev, ev->status);
2277 break;
2278
2279 case HCI_OP_FLOW_SPEC_MODIFY:
2280 hci_cs_flow_spec_modify(hdev, ev->status);
2281 break;
2282
2283 case HCI_OP_CREATE_PHYS_LINK:
2284 case HCI_OP_ACCEPT_PHYS_LINK:
2285 hci_amp_cmd_status(hdev, opcode, ev->status);
2286 break;
2287
2288 case HCI_OP_DISCONN_PHYS_LINK:
2289 hci_cs_disconn_physical_link(hdev, ev->status);
2290
Johan Hedberg8962ee72011-01-20 12:40:27 +02002291 case HCI_OP_DISCONNECT:
2292 if (ev->status != 0)
2293 mgmt_disconnect_failed(hdev->id);
2294 break;
2295
Ville Tervofcd89c02011-02-10 22:38:47 -03002296 case HCI_OP_LE_CREATE_CONN:
2297 hci_cs_le_create_conn(hdev, ev->status);
2298 break;
2299
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03002300 case HCI_OP_LE_START_ENC:
2301 hci_cs_le_start_enc(hdev, ev->status);
2302 break;
2303
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002304 default:
2305 BT_DBG("%s opcode 0x%x", hdev->name, opcode);
2306 break;
2307 }
2308
Ville Tervo6bd32322011-02-16 16:32:41 +02002309 if (ev->opcode != HCI_OP_NOP)
2310 del_timer(&hdev->cmd_timer);
2311
Gustavo F. Padovan10572132011-03-16 15:36:29 -03002312 if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags)) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002313 atomic_set(&hdev->cmd_cnt, 1);
2314 if (!skb_queue_empty(&hdev->cmd_q))
Marcel Holtmannc78ae282009-11-18 01:02:54 +01002315 tasklet_schedule(&hdev->cmd_task);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002316 }
2317}
2318
2319static inline void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
2320{
2321 struct hci_ev_role_change *ev = (void *) skb->data;
2322 struct hci_conn *conn;
2323
2324 BT_DBG("%s status %d", hdev->name, ev->status);
2325
2326 hci_dev_lock(hdev);
2327
2328 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
2329 if (conn) {
2330 if (!ev->status) {
2331 if (ev->role)
2332 conn->link_mode &= ~HCI_LM_MASTER;
2333 else
2334 conn->link_mode |= HCI_LM_MASTER;
2335 }
2336
2337 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->pend);
2338
2339 hci_role_switch_cfm(conn, ev->status, ev->role);
2340 }
2341
2342 hci_dev_unlock(hdev);
2343}
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345static inline void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
2346{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002347 struct hci_ev_num_comp_pkts *ev = (void *) skb->data;
Marcel Holtmann1ebb9252005-11-08 09:57:21 -08002348 __le16 *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 int i;
2350
2351 skb_pull(skb, sizeof(*ev));
2352
2353 BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
2354
2355 if (skb->len < ev->num_hndl * 4) {
2356 BT_DBG("%s bad parameters", hdev->name);
2357 return;
2358 }
2359
2360 tasklet_disable(&hdev->tx_task);
2361
Marcel Holtmann1ebb9252005-11-08 09:57:21 -08002362 for (i = 0, ptr = (__le16 *) skb->data; i < ev->num_hndl; i++) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002363 struct hci_conn *conn = NULL;
2364 struct hci_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 __u16 handle, count;
2366
Harvey Harrison83985312008-05-02 16:25:46 -07002367 handle = get_unaligned_le16(ptr++);
2368 count = get_unaligned_le16(ptr++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002370 if (hdev->dev_type == HCI_BREDR)
2371 conn = hci_conn_hash_lookup_handle(hdev, handle);
2372 else {
2373 chan = hci_chan_list_lookup_handle(hdev, handle);
2374 if (chan)
2375 conn = chan->conn;
2376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 if (conn) {
2378 conn->sent -= count;
2379
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02002380 if (conn->type == ACL_LINK) {
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02002381 hdev->acl_cnt += count;
2382 if (hdev->acl_cnt > hdev->acl_pkts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 hdev->acl_cnt = hdev->acl_pkts;
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002384 } else if (conn->type == LE_LINK) {
2385 if (hdev->le_pkts) {
2386 hdev->le_cnt += count;
2387 if (hdev->le_cnt > hdev->le_pkts)
2388 hdev->le_cnt = hdev->le_pkts;
2389 } else {
2390 hdev->acl_cnt += count;
2391 if (hdev->acl_cnt > hdev->acl_pkts)
2392 hdev->acl_cnt = hdev->acl_pkts;
2393 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02002394 } else {
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02002395 hdev->sco_cnt += count;
2396 if (hdev->sco_cnt > hdev->sco_pkts)
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02002397 hdev->sco_cnt = hdev->sco_pkts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
2399 }
2400 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002401
Marcel Holtmannc78ae282009-11-18 01:02:54 +01002402 tasklet_schedule(&hdev->tx_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404 tasklet_enable(&hdev->tx_task);
2405}
2406
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002407static inline void hci_num_comp_blocks_evt(struct hci_dev *hdev,
2408 struct sk_buff *skb)
2409{
2410 struct hci_ev_num_comp_blocks *ev = (void *) skb->data;
2411 __le16 *ptr;
2412 int i;
2413
2414 skb_pull(skb, sizeof(*ev));
2415
2416 BT_DBG("%s total_num_blocks %d num_hndl %d",
2417 hdev->name, ev->total_num_blocks, ev->num_hndl);
2418
2419 if (skb->len < ev->num_hndl * 6) {
2420 BT_DBG("%s bad parameters", hdev->name);
2421 return;
2422 }
2423
2424 tasklet_disable(&hdev->tx_task);
2425
2426 for (i = 0, ptr = (__le16 *) skb->data; i < ev->num_hndl; i++) {
2427 struct hci_conn *conn = NULL;
2428 struct hci_chan *chan;
2429 __u16 handle, block_count;
2430
2431 handle = get_unaligned_le16(ptr++);
2432
2433 /* Skip packet count */
2434 ptr++;
2435 block_count = get_unaligned_le16(ptr++);
2436
2437 BT_DBG("%s handle %d count %d", hdev->name, handle,
2438 block_count);
2439
2440 if (hdev->dev_type == HCI_BREDR)
2441 conn = hci_conn_hash_lookup_handle(hdev, handle);
2442 else {
2443 chan = hci_chan_list_lookup_handle(hdev, handle);
2444 if (chan)
2445 conn = chan->conn;
2446 }
2447 if (conn) {
2448 BT_DBG("%s conn %p sent %d", hdev->name,
2449 conn, conn->sent);
2450
2451 conn->sent -= block_count;
2452
2453 if (conn->type == ACL_LINK) {
2454 hdev->acl_cnt += block_count;
2455 if (hdev->acl_cnt > hdev->acl_pkts)
2456 hdev->acl_cnt = hdev->acl_pkts;
2457 } else {
2458 /* We should not find ourselves here */
2459 BT_DBG("Unexpected event for SCO connection");
2460 }
2461 }
2462 }
2463
2464 tasklet_schedule(&hdev->tx_task);
2465
2466 tasklet_enable(&hdev->tx_task);
2467}
2468
Marcel Holtmann04837f62006-07-03 10:02:33 +02002469static inline void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002471 struct hci_ev_mode_change *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02002472 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
2474 BT_DBG("%s status %d", hdev->name, ev->status);
2475
2476 hci_dev_lock(hdev);
2477
Marcel Holtmann04837f62006-07-03 10:02:33 +02002478 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
2479 if (conn) {
2480 conn->mode = ev->mode;
2481 conn->interval = __le16_to_cpu(ev->interval);
2482
2483 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
2484 if (conn->mode == HCI_CM_ACTIVE)
2485 conn->power_save = 1;
2486 else
2487 conn->power_save = 0;
2488 }
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002489
2490 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->pend))
2491 hci_sco_setup(conn, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02002492 }
2493
2494 hci_dev_unlock(hdev);
2495}
2496
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497static inline void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
2498{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002499 struct hci_ev_pin_code_req *ev = (void *) skb->data;
2500 struct hci_conn *conn;
2501
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002502 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002503
2504 hci_dev_lock(hdev);
2505
2506 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Marcel Holtmann3d7a9d12009-05-09 12:09:21 -07002507 if (conn && conn->state == BT_CONNECTED) {
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002508 hci_conn_hold(conn);
2509 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
2510 hci_conn_put(conn);
Srinivas Krovvidi32ba9352012-01-26 10:48:08 +05302511 hci_conn_enter_active_mode(conn, 0);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002512 }
2513
Johan Hedberg03b555e2011-01-04 15:40:05 +02002514 if (!test_bit(HCI_PAIRABLE, &hdev->flags))
2515 hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
2516 sizeof(ev->bdaddr), &ev->bdaddr);
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02002517
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002518 if (test_bit(HCI_MGMT, &hdev->flags))
2519 mgmt_pin_code_request(hdev->id, &ev->bdaddr);
Johan Hedberg980e1a52011-01-22 06:10:07 +02002520
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002521 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522}
2523
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524static inline void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
2525{
Johan Hedberg55ed8ca2011-01-17 14:41:05 +02002526 struct hci_ev_link_key_req *ev = (void *) skb->data;
2527 struct hci_cp_link_key_reply cp;
2528 struct hci_conn *conn;
2529 struct link_key *key;
2530
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002531 BT_DBG("%s", hdev->name);
Johan Hedberg55ed8ca2011-01-17 14:41:05 +02002532
2533 if (!test_bit(HCI_LINK_KEYS, &hdev->flags))
2534 return;
2535
2536 hci_dev_lock(hdev);
2537
2538 key = hci_find_link_key(hdev, &ev->bdaddr);
2539 if (!key) {
2540 BT_DBG("%s link key not found for %s", hdev->name,
2541 batostr(&ev->bdaddr));
2542 goto not_found;
2543 }
2544
Brian Gixcf956772011-10-20 15:18:51 -07002545 BT_DBG("%s found key type %u for %s", hdev->name, key->key_type,
Johan Hedberg55ed8ca2011-01-17 14:41:05 +02002546 batostr(&ev->bdaddr));
2547
Brian Gixcf956772011-10-20 15:18:51 -07002548 if (!test_bit(HCI_DEBUG_KEYS, &hdev->flags) && key->key_type == 0x03) {
Johan Hedberg55ed8ca2011-01-17 14:41:05 +02002549 BT_DBG("%s ignoring debug key", hdev->name);
2550 goto not_found;
2551 }
2552
2553 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
2554
Prabhakaran Mc6001a712011-09-06 11:56:25 +05302555 if (conn) {
2556 BT_DBG("Conn pending sec level is %d, ssp is %d, key len is %d",
2557 conn->pending_sec_level, conn->ssp_mode, key->pin_len);
2558 }
2559 if (conn && (conn->ssp_mode == 0) &&
2560 (conn->pending_sec_level == BT_SECURITY_HIGH) &&
2561 (key->pin_len != 16)) {
2562 BT_DBG("Security is high ignoring this key");
2563 goto not_found;
2564 }
2565
Brian Gixcf956772011-10-20 15:18:51 -07002566 if (key->key_type == 0x04 && conn && conn->auth_type != 0xff &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002567 (conn->auth_type & 0x01)) {
2568 BT_DBG("%s ignoring unauthenticated key", hdev->name);
2569 goto not_found;
Johan Hedberg55ed8ca2011-01-17 14:41:05 +02002570 }
2571
2572 bacpy(&cp.bdaddr, &ev->bdaddr);
2573 memcpy(cp.link_key, key->val, 16);
2574
2575 hci_send_cmd(hdev, HCI_OP_LINK_KEY_REPLY, sizeof(cp), &cp);
2576
2577 hci_dev_unlock(hdev);
2578
2579 return;
2580
2581not_found:
2582 hci_send_cmd(hdev, HCI_OP_LINK_KEY_NEG_REPLY, 6, &ev->bdaddr);
2583 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
2585
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586static inline void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
2587{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002588 struct hci_ev_link_key_notify *ev = (void *) skb->data;
2589 struct hci_conn *conn;
Johan Hedberg55ed8ca2011-01-17 14:41:05 +02002590 u8 pin_len = 0;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002591
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002592 BT_DBG("%s type %d", hdev->name, ev->key_type);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002593
2594 hci_dev_lock(hdev);
2595
2596 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
2597 if (conn) {
2598 hci_conn_hold(conn);
2599 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002600
2601 memcpy(conn->link_key, ev->link_key, 16);
2602 conn->key_type = ev->key_type;
2603 hci_disconnect_amp(conn, 0x06);
2604
Johan Hedberg980e1a52011-01-22 06:10:07 +02002605 pin_len = conn->pin_length;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002606 hci_conn_put(conn);
Srinivas Krovvidi32ba9352012-01-26 10:48:08 +05302607 hci_conn_enter_active_mode(conn, 0);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002608 }
2609
Johan Hedberg55ed8ca2011-01-17 14:41:05 +02002610 if (test_bit(HCI_LINK_KEYS, &hdev->flags))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002611 hci_add_link_key(hdev, 1, &ev->bdaddr, ev->link_key,
Johan Hedberg55ed8ca2011-01-17 14:41:05 +02002612 ev->key_type, pin_len);
2613
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002614 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615}
2616
Marcel Holtmann04837f62006-07-03 10:02:33 +02002617static inline void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
2618{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002619 struct hci_ev_clock_offset *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02002620 struct hci_conn *conn;
2621
2622 BT_DBG("%s status %d", hdev->name, ev->status);
2623
2624 hci_dev_lock(hdev);
2625
2626 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 if (conn && !ev->status) {
2628 struct inquiry_entry *ie;
2629
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02002630 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
2631 if (ie) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 ie->data.clock_offset = ev->clock_offset;
2633 ie->timestamp = jiffies;
2634 }
2635 }
2636
2637 hci_dev_unlock(hdev);
2638}
2639
Marcel Holtmanna8746412008-07-14 20:13:46 +02002640static inline void hci_pkt_type_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
2641{
2642 struct hci_ev_pkt_type_change *ev = (void *) skb->data;
2643 struct hci_conn *conn;
2644
2645 BT_DBG("%s status %d", hdev->name, ev->status);
2646
2647 hci_dev_lock(hdev);
2648
2649 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
2650 if (conn && !ev->status)
2651 conn->pkt_type = __le16_to_cpu(ev->pkt_type);
2652
2653 hci_dev_unlock(hdev);
2654}
2655
Marcel Holtmann85a1e932005-08-09 20:28:02 -07002656static inline void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
2657{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002658 struct hci_ev_pscan_rep_mode *ev = (void *) skb->data;
Marcel Holtmann85a1e932005-08-09 20:28:02 -07002659 struct inquiry_entry *ie;
2660
2661 BT_DBG("%s", hdev->name);
2662
2663 hci_dev_lock(hdev);
2664
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02002665 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
2666 if (ie) {
Marcel Holtmann85a1e932005-08-09 20:28:02 -07002667 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
2668 ie->timestamp = jiffies;
2669 }
2670
2671 hci_dev_unlock(hdev);
2672}
2673
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002674static inline void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, struct sk_buff *skb)
2675{
2676 struct inquiry_data data;
2677 int num_rsp = *((__u8 *) skb->data);
2678
2679 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
2680
2681 if (!num_rsp)
2682 return;
2683
2684 hci_dev_lock(hdev);
2685
2686 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
Szymon Janc138d22e2011-02-17 16:44:23 +01002687 struct inquiry_info_with_rssi_and_pscan_mode *info;
2688 info = (void *) (skb->data + 1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002689
Johan Hedberge17acd42011-03-30 23:57:16 +03002690 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002691 bacpy(&data.bdaddr, &info->bdaddr);
2692 data.pscan_rep_mode = info->pscan_rep_mode;
2693 data.pscan_period_mode = info->pscan_period_mode;
2694 data.pscan_mode = info->pscan_mode;
2695 memcpy(data.dev_class, info->dev_class, 3);
2696 data.clock_offset = info->clock_offset;
2697 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002698 data.ssp_mode = 0x00;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002699 hci_inquiry_cache_update(hdev, &data);
Brian Gixa68668b2011-08-11 15:49:36 -07002700 mgmt_device_found(hdev->id, &info->bdaddr, 0, 0,
Johan Hedberge17acd42011-03-30 23:57:16 +03002701 info->dev_class, info->rssi,
Brian Gixa68668b2011-08-11 15:49:36 -07002702 0, NULL);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002703 }
2704 } else {
2705 struct inquiry_info_with_rssi *info = (void *) (skb->data + 1);
2706
Johan Hedberge17acd42011-03-30 23:57:16 +03002707 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002708 bacpy(&data.bdaddr, &info->bdaddr);
2709 data.pscan_rep_mode = info->pscan_rep_mode;
2710 data.pscan_period_mode = info->pscan_period_mode;
2711 data.pscan_mode = 0x00;
2712 memcpy(data.dev_class, info->dev_class, 3);
2713 data.clock_offset = info->clock_offset;
2714 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002715 data.ssp_mode = 0x00;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002716 hci_inquiry_cache_update(hdev, &data);
Brian Gixa68668b2011-08-11 15:49:36 -07002717 mgmt_device_found(hdev->id, &info->bdaddr, 0, 0,
Johan Hedberge17acd42011-03-30 23:57:16 +03002718 info->dev_class, info->rssi,
Brian Gixa68668b2011-08-11 15:49:36 -07002719 0, NULL);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002720 }
2721 }
2722
2723 hci_dev_unlock(hdev);
2724}
2725
2726static inline void hci_remote_ext_features_evt(struct hci_dev *hdev, struct sk_buff *skb)
2727{
Marcel Holtmann41a96212008-07-14 20:13:48 +02002728 struct hci_ev_remote_ext_features *ev = (void *) skb->data;
2729 struct hci_conn *conn;
2730
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002731 BT_DBG("%s", hdev->name);
Marcel Holtmann41a96212008-07-14 20:13:48 +02002732
Marcel Holtmann41a96212008-07-14 20:13:48 +02002733 hci_dev_lock(hdev);
2734
2735 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02002736 if (!conn)
2737 goto unlock;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002738
Johan Hedbergccd556f2010-11-10 17:11:51 +02002739 if (!ev->status && ev->page == 0x01) {
2740 struct inquiry_entry *ie;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002741
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02002742 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
2743 if (ie)
Johan Hedbergccd556f2010-11-10 17:11:51 +02002744 ie->data.ssp_mode = (ev->features[0] & 0x01);
Marcel Holtmann769be972008-07-14 20:13:49 +02002745
Johan Hedbergccd556f2010-11-10 17:11:51 +02002746 conn->ssp_mode = (ev->features[0] & 0x01);
Bhakthavatsala Raghavendra6edb4f12011-11-04 16:07:41 +05302747 /*In case if remote device ssp supported/2.0 device
2748 reduce the security level to MEDIUM if it is HIGH*/
2749 if (!conn->ssp_mode &&
2750 (conn->pending_sec_level == BT_SECURITY_HIGH))
2751 conn->pending_sec_level = BT_SECURITY_MEDIUM;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002752 }
2753
Johan Hedbergccd556f2010-11-10 17:11:51 +02002754 if (conn->state != BT_CONFIG)
2755 goto unlock;
2756
Johan Hedberg127178d2010-11-18 22:22:29 +02002757 if (!ev->status) {
2758 struct hci_cp_remote_name_req cp;
2759 memset(&cp, 0, sizeof(cp));
2760 bacpy(&cp.bdaddr, &conn->dst);
2761 cp.pscan_rep_mode = 0x02;
2762 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
2763 }
Johan Hedberg392599b2010-11-18 22:22:28 +02002764
Johan Hedberg127178d2010-11-18 22:22:29 +02002765 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02002766 conn->state = BT_CONNECTED;
2767 hci_proto_connect_cfm(conn, ev->status);
2768 hci_conn_put(conn);
2769 }
2770
2771unlock:
Marcel Holtmann41a96212008-07-14 20:13:48 +02002772 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002773}
2774
2775static inline void hci_sync_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
2776{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002777 struct hci_ev_sync_conn_complete *ev = (void *) skb->data;
2778 struct hci_conn *conn;
2779
2780 BT_DBG("%s status %d", hdev->name, ev->status);
2781
2782 hci_dev_lock(hdev);
2783
2784 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02002785 if (!conn) {
2786 if (ev->link_type == ESCO_LINK)
2787 goto unlock;
2788
2789 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
2790 if (!conn)
2791 goto unlock;
2792
2793 conn->type = SCO_LINK;
2794 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002795
Marcel Holtmann732547f2009-04-19 19:14:14 +02002796 switch (ev->status) {
2797 case 0x00:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002798 conn->handle = __le16_to_cpu(ev->handle);
2799 conn->state = BT_CONNECTED;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002800
Marcel Holtmann9eba32b2009-08-22 14:19:26 -07002801 hci_conn_hold_device(conn);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002802 hci_conn_add_sysfs(conn);
Marcel Holtmann732547f2009-04-19 19:14:14 +02002803 break;
2804
Stephen Coe705e5712010-02-16 11:29:44 -05002805 case 0x11: /* Unsupported Feature or Parameter Value */
Marcel Holtmann732547f2009-04-19 19:14:14 +02002806 case 0x1c: /* SCO interval rejected */
Nick Pelly1038a002010-02-03 11:42:26 -08002807 case 0x1a: /* Unsupported Remote Feature */
Marcel Holtmann732547f2009-04-19 19:14:14 +02002808 case 0x1f: /* Unspecified error */
2809 if (conn->out && conn->attempt < 2) {
Kun Han Kim15b911f2011-07-08 09:30:28 -07002810 if (!conn->hdev->is_wbs)
2811 conn->pkt_type =
2812 (hdev->esco_type & SCO_ESCO_MASK) |
Marcel Holtmann732547f2009-04-19 19:14:14 +02002813 (hdev->esco_type & EDR_ESCO_MASK);
2814 hci_setup_sync(conn, conn->link->handle);
2815 goto unlock;
2816 }
2817 /* fall through */
2818
2819 default:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002820 conn->state = BT_CLOSED;
Marcel Holtmann732547f2009-04-19 19:14:14 +02002821 break;
2822 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002823
2824 hci_proto_connect_cfm(conn, ev->status);
2825 if (ev->status)
2826 hci_conn_del(conn);
2827
2828unlock:
2829 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002830}
2831
2832static inline void hci_sync_conn_changed_evt(struct hci_dev *hdev, struct sk_buff *skb)
2833{
2834 BT_DBG("%s", hdev->name);
2835}
2836
Marcel Holtmann04837f62006-07-03 10:02:33 +02002837static inline void hci_sniff_subrate_evt(struct hci_dev *hdev, struct sk_buff *skb)
2838{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002839 struct hci_ev_sniff_subrate *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02002840
2841 BT_DBG("%s status %d", hdev->name, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02002842}
2843
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002844static inline void hci_extended_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
2845{
2846 struct inquiry_data data;
2847 struct extended_inquiry_info *info = (void *) (skb->data + 1);
2848 int num_rsp = *((__u8 *) skb->data);
2849
2850 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
2851
2852 if (!num_rsp)
2853 return;
2854
2855 hci_dev_lock(hdev);
2856
Johan Hedberge17acd42011-03-30 23:57:16 +03002857 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002858 bacpy(&data.bdaddr, &info->bdaddr);
Szymon Janc138d22e2011-02-17 16:44:23 +01002859 data.pscan_rep_mode = info->pscan_rep_mode;
2860 data.pscan_period_mode = info->pscan_period_mode;
2861 data.pscan_mode = 0x00;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002862 memcpy(data.dev_class, info->dev_class, 3);
Szymon Janc138d22e2011-02-17 16:44:23 +01002863 data.clock_offset = info->clock_offset;
2864 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002865 data.ssp_mode = 0x01;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002866 hci_inquiry_cache_update(hdev, &data);
Brian Gixa68668b2011-08-11 15:49:36 -07002867 mgmt_device_found(hdev->id, &info->bdaddr, 0, 0,
2868 info->dev_class, info->rssi,
2869 HCI_MAX_EIR_LENGTH, info->data);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002870 }
2871
2872 hci_dev_unlock(hdev);
2873}
2874
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002875static inline u8 hci_get_auth_req(struct hci_conn *conn)
2876{
Brian Gixa68668b2011-08-11 15:49:36 -07002877 BT_DBG("%p", conn);
2878
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002879 /* If remote requests dedicated bonding follow that lead */
2880 if (conn->remote_auth == 0x02 || conn->remote_auth == 0x03) {
2881 /* If both remote and local IO capabilities allow MITM
2882 * protection then require it, otherwise don't */
Brian Gixa68668b2011-08-11 15:49:36 -07002883 if (conn->remote_cap == 0x03 || conn->io_capability == 0x03) {
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002884 return 0x02;
Brian Gixa68668b2011-08-11 15:49:36 -07002885 } else {
2886 conn->auth_type |= 0x01;
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002887 return 0x03;
Brian Gixa68668b2011-08-11 15:49:36 -07002888 }
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002889 }
2890
2891 /* If remote requests no-bonding follow that lead */
Brian Gixa68668b2011-08-11 15:49:36 -07002892 if (conn->remote_auth <= 0x01)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002893 return 0x00;
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002894
2895 return conn->auth_type;
2896}
2897
Marcel Holtmann04936842008-07-14 20:13:48 +02002898static inline void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
2899{
2900 struct hci_ev_io_capa_request *ev = (void *) skb->data;
2901 struct hci_conn *conn;
2902
2903 BT_DBG("%s", hdev->name);
2904
2905 hci_dev_lock(hdev);
2906
2907 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg03b555e2011-01-04 15:40:05 +02002908 if (!conn)
2909 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02002910
Johan Hedberg03b555e2011-01-04 15:40:05 +02002911 hci_conn_hold(conn);
2912
2913 if (!test_bit(HCI_MGMT, &hdev->flags))
2914 goto unlock;
2915
2916 if (test_bit(HCI_PAIRABLE, &hdev->flags) ||
2917 (conn->remote_auth & ~0x01) == HCI_AT_NO_BONDING) {
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002918 struct hci_cp_io_capability_reply cp;
Brian Gixa68668b2011-08-11 15:49:36 -07002919 u8 io_cap = conn->io_capability;
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002920
Brian Gixa68668b2011-08-11 15:49:36 -07002921 /* ACL-SSP does not support IO CAP 0x04 */
2922 cp.capability = (io_cap == 0x04) ? 0x01 : io_cap;
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002923 bacpy(&cp.bdaddr, &ev->bdaddr);
Brian Gixa68668b2011-08-11 15:49:36 -07002924 if (conn->auth_initiator)
2925 cp.authentication = conn->auth_type;
2926 else
2927 cp.authentication = hci_get_auth_req(conn);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002928
Szymon Jancce85ee12011-03-22 13:12:23 +01002929 if ((conn->out == 0x01 || conn->remote_oob == 0x01) &&
2930 hci_find_remote_oob_data(hdev, &conn->dst))
2931 cp.oob_data = 0x01;
2932 else
2933 cp.oob_data = 0x00;
2934
Johan Hedberg17fa4b92011-01-25 13:28:33 +02002935 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_REPLY,
2936 sizeof(cp), &cp);
Johan Hedberg03b555e2011-01-04 15:40:05 +02002937 } else {
2938 struct hci_cp_io_capability_neg_reply cp;
2939
2940 bacpy(&cp.bdaddr, &ev->bdaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002941 cp.reason = 0x16; /* Pairing not allowed */
Johan Hedberg03b555e2011-01-04 15:40:05 +02002942
2943 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_NEG_REPLY,
2944 sizeof(cp), &cp);
2945 }
2946
2947unlock:
2948 hci_dev_unlock(hdev);
2949}
2950
2951static inline void hci_io_capa_reply_evt(struct hci_dev *hdev, struct sk_buff *skb)
2952{
2953 struct hci_ev_io_capa_reply *ev = (void *) skb->data;
2954 struct hci_conn *conn;
2955
2956 BT_DBG("%s", hdev->name);
2957
2958 hci_dev_lock(hdev);
2959
2960 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
2961 if (!conn)
2962 goto unlock;
2963
Johan Hedberg03b555e2011-01-04 15:40:05 +02002964 conn->remote_cap = ev->capability;
2965 conn->remote_oob = ev->oob_data;
2966 conn->remote_auth = ev->authentication;
2967
2968unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02002969 hci_dev_unlock(hdev);
2970}
2971
Brian Gixa68668b2011-08-11 15:49:36 -07002972static inline void hci_user_ssp_confirmation_evt(struct hci_dev *hdev,
2973 u8 event, struct sk_buff *skb)
Johan Hedberga5c29682011-02-19 12:05:57 -03002974{
2975 struct hci_ev_user_confirm_req *ev = (void *) skb->data;
2976
2977 BT_DBG("%s", hdev->name);
2978
2979 hci_dev_lock(hdev);
2980
Brian Gixa68668b2011-08-11 15:49:36 -07002981 if (test_bit(HCI_MGMT, &hdev->flags)) {
2982 if (event == HCI_EV_USER_PASSKEY_REQUEST)
2983 mgmt_user_confirm_request(hdev->id, event,
2984 &ev->bdaddr, 0);
2985 else
2986 mgmt_user_confirm_request(hdev->id, event,
2987 &ev->bdaddr, ev->passkey);
2988 }
Johan Hedberga5c29682011-02-19 12:05:57 -03002989
2990 hci_dev_unlock(hdev);
2991}
2992
Marcel Holtmann04936842008-07-14 20:13:48 +02002993static inline void hci_simple_pair_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
2994{
2995 struct hci_ev_simple_pair_complete *ev = (void *) skb->data;
2996 struct hci_conn *conn;
2997
2998 BT_DBG("%s", hdev->name);
2999
3000 hci_dev_lock(hdev);
3001
3002 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg2a611692011-02-19 12:06:00 -03003003 if (!conn)
3004 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02003005
Johan Hedberg2a611692011-02-19 12:06:00 -03003006 /* To avoid duplicate auth_failed events to user space we check
3007 * the HCI_CONN_AUTH_PEND flag which will be set if we
3008 * initiated the authentication. A traditional auth_complete
3009 * event gets always produced as initiator and is also mapped to
3010 * the mgmt_auth_failed event */
3011 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->pend) && ev->status != 0)
3012 mgmt_auth_failed(hdev->id, &conn->dst, ev->status);
3013
3014 hci_conn_put(conn);
3015
3016unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02003017 hci_dev_unlock(hdev);
3018}
3019
Marcel Holtmann41a96212008-07-14 20:13:48 +02003020static inline void hci_remote_host_features_evt(struct hci_dev *hdev, struct sk_buff *skb)
3021{
3022 struct hci_ev_remote_host_features *ev = (void *) skb->data;
3023 struct inquiry_entry *ie;
3024
3025 BT_DBG("%s", hdev->name);
3026
3027 hci_dev_lock(hdev);
3028
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02003029 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
3030 if (ie)
Marcel Holtmann41a96212008-07-14 20:13:48 +02003031 ie->data.ssp_mode = (ev->features[0] & 0x01);
3032
3033 hci_dev_unlock(hdev);
3034}
3035
Szymon Janc2763eda2011-03-22 13:12:22 +01003036static inline void hci_remote_oob_data_request_evt(struct hci_dev *hdev,
3037 struct sk_buff *skb)
3038{
3039 struct hci_ev_remote_oob_data_request *ev = (void *) skb->data;
3040 struct oob_data *data;
3041
3042 BT_DBG("%s", hdev->name);
3043
3044 hci_dev_lock(hdev);
3045
Szymon Jance1ba1f12011-04-06 13:01:59 +02003046 if (!test_bit(HCI_MGMT, &hdev->flags))
3047 goto unlock;
3048
Szymon Janc2763eda2011-03-22 13:12:22 +01003049 data = hci_find_remote_oob_data(hdev, &ev->bdaddr);
3050 if (data) {
3051 struct hci_cp_remote_oob_data_reply cp;
3052
3053 bacpy(&cp.bdaddr, &ev->bdaddr);
3054 memcpy(cp.hash, data->hash, sizeof(cp.hash));
3055 memcpy(cp.randomizer, data->randomizer, sizeof(cp.randomizer));
3056
3057 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_REPLY, sizeof(cp),
3058 &cp);
3059 } else {
3060 struct hci_cp_remote_oob_data_neg_reply cp;
3061
3062 bacpy(&cp.bdaddr, &ev->bdaddr);
3063 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_NEG_REPLY, sizeof(cp),
3064 &cp);
3065 }
3066
Szymon Jance1ba1f12011-04-06 13:01:59 +02003067unlock:
Szymon Janc2763eda2011-03-22 13:12:22 +01003068 hci_dev_unlock(hdev);
3069}
3070
Ville Tervofcd89c02011-02-10 22:38:47 -03003071static inline void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
3072{
3073 struct hci_ev_le_conn_complete *ev = (void *) skb->data;
3074 struct hci_conn *conn;
3075
3076 BT_DBG("%s status %d", hdev->name, ev->status);
3077
3078 hci_dev_lock(hdev);
3079
3080 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &ev->bdaddr);
Ville Tervob62f3282011-02-10 22:38:50 -03003081 if (!conn) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003082 conn = hci_le_conn_add(hdev, &ev->bdaddr, ev->bdaddr_type);
Ville Tervob62f3282011-02-10 22:38:50 -03003083 if (!conn) {
3084 BT_ERR("No memory for new connection");
3085 hci_dev_unlock(hdev);
3086 return;
3087 }
3088 }
Ville Tervofcd89c02011-02-10 22:38:47 -03003089
3090 if (ev->status) {
3091 hci_proto_connect_cfm(conn, ev->status);
3092 conn->state = BT_CLOSED;
3093 hci_conn_del(conn);
3094 goto unlock;
3095 }
3096
Vinicius Costa Gomes403d2c82011-06-09 18:50:50 -03003097 conn->sec_level = BT_SECURITY_LOW;
Ville Tervofcd89c02011-02-10 22:38:47 -03003098 conn->handle = __le16_to_cpu(ev->handle);
3099 conn->state = BT_CONNECTED;
Brian Gixa68668b2011-08-11 15:49:36 -07003100 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Brian Gix2e2f50d2011-09-13 12:36:04 -07003101 mgmt_connected(hdev->id, &ev->bdaddr, 1);
Ville Tervofcd89c02011-02-10 22:38:47 -03003102
Brian Gix6d5fb8a2011-09-09 14:53:04 -07003103 hci_conn_hold(conn);
Ville Tervofcd89c02011-02-10 22:38:47 -03003104 hci_conn_hold_device(conn);
3105 hci_conn_add_sysfs(conn);
3106
3107 hci_proto_connect_cfm(conn, ev->status);
3108
3109unlock:
3110 hci_dev_unlock(hdev);
3111}
3112
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03003113static inline void hci_le_ltk_request_evt(struct hci_dev *hdev,
3114 struct sk_buff *skb)
3115{
3116 struct hci_ev_le_ltk_req *ev = (void *) skb->data;
3117 struct hci_cp_le_ltk_reply cp;
Vinicius Costa Gomes60e56472011-07-07 18:59:37 -03003118 struct hci_cp_le_ltk_neg_reply neg;
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03003119 struct hci_conn *conn;
Vinicius Costa Gomes60e56472011-07-07 18:59:37 -03003120 struct link_key *ltk;
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03003121
3122 BT_DBG("%s handle %d", hdev->name, cpu_to_le16(ev->handle));
3123
3124 hci_dev_lock(hdev);
3125
3126 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Vinicius Costa Gomes60e56472011-07-07 18:59:37 -03003127 if (conn == NULL)
3128 goto not_found;
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03003129
Vinicius Costa Gomes60e56472011-07-07 18:59:37 -03003130 ltk = hci_find_ltk(hdev, ev->ediv, ev->random);
3131 if (ltk == NULL)
3132 goto not_found;
3133
3134 memcpy(cp.ltk, ltk->val, sizeof(ltk->val));
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03003135 cp.handle = cpu_to_le16(conn->handle);
Vinicius Costa Gomes1fa2de32011-07-08 18:31:45 -03003136 conn->pin_length = ltk->pin_len;
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03003137
3138 hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
3139
3140 hci_dev_unlock(hdev);
Vinicius Costa Gomes60e56472011-07-07 18:59:37 -03003141
3142 return;
3143
3144not_found:
3145 neg.handle = ev->handle;
3146 hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(neg), &neg);
3147 hci_dev_unlock(hdev);
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03003148}
3149
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003150static inline void hci_le_adv_report_evt(struct hci_dev *hdev,
3151 struct sk_buff *skb)
3152{
3153 struct hci_ev_le_advertising_info *ev;
3154 u8 num_reports;
3155
3156 num_reports = skb->data[0];
3157 ev = (void *) &skb->data[1];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003158
Brian Gix3dd70172011-09-16 21:38:54 -07003159 hci_dev_lock(hdev);
3160
Brian Gixa68668b2011-08-11 15:49:36 -07003161 while (num_reports--) {
3162 mgmt_device_found(hdev->id, &ev->bdaddr, ev->bdaddr_type,
3163 1, NULL, 0, ev->length, ev->data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003164 hci_add_adv_entry(hdev, ev);
Brian Gixa68668b2011-08-11 15:49:36 -07003165 ev = (void *) (ev->data + ev->length + 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003166 }
Brian Gix3dd70172011-09-16 21:38:54 -07003167
3168 hci_dev_unlock(hdev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003169}
3170
Ville Tervofcd89c02011-02-10 22:38:47 -03003171static inline void hci_le_meta_evt(struct hci_dev *hdev, struct sk_buff *skb)
3172{
3173 struct hci_ev_le_meta *le_ev = (void *) skb->data;
3174
3175 skb_pull(skb, sizeof(*le_ev));
3176
3177 switch (le_ev->subevent) {
3178 case HCI_EV_LE_CONN_COMPLETE:
3179 hci_le_conn_complete_evt(hdev, skb);
3180 break;
3181
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -03003182 case HCI_EV_LE_LTK_REQ:
3183 hci_le_ltk_request_evt(hdev, skb);
3184 break;
3185
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003186 case HCI_EV_LE_ADVERTISING_REPORT:
3187 hci_le_adv_report_evt(hdev, skb);
3188 break;
3189
Ville Tervofcd89c02011-02-10 22:38:47 -03003190 default:
3191 break;
3192 }
3193}
3194
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003195static inline void hci_phy_link_complete(struct hci_dev *hdev,
3196 struct sk_buff *skb)
3197{
3198 struct hci_ev_phys_link_complete *ev = (void *) skb->data;
3199 struct hci_conn *conn;
3200
3201 BT_DBG("%s handle %d status %d", hdev->name, ev->phy_handle,
3202 ev->status);
3203
3204 hci_dev_lock(hdev);
3205
3206 if (ev->status == 0) {
3207 conn = hci_conn_add(hdev, ACL_LINK, 0, BDADDR_ANY);
3208 if (conn) {
3209 conn->handle = ev->phy_handle;
3210 conn->state = BT_CONNECTED;
3211
3212 hci_conn_hold(conn);
3213 conn->disc_timeout = HCI_DISCONN_TIMEOUT/2;
3214 hci_conn_put(conn);
3215
3216 hci_conn_hold_device(conn);
3217 hci_conn_add_sysfs(conn);
3218 } else
3219 BT_ERR("No memory for new connection");
3220 }
3221
3222 hci_dev_unlock(hdev);
3223}
3224
3225static inline void hci_log_link_complete(struct hci_dev *hdev,
3226 struct sk_buff *skb)
3227{
3228 struct hci_ev_log_link_complete *ev = (void *) skb->data;
3229 struct hci_chan *chan;
3230
3231 BT_DBG("%s handle %d status %d", hdev->name,
3232 __le16_to_cpu(ev->log_handle), ev->status);
3233
3234 hci_dev_lock(hdev);
3235
3236 chan = hci_chan_list_lookup_id(hdev, ev->phy_handle);
3237
Mat Martineau9f8d4672011-12-14 12:10:46 -08003238 if (chan) {
3239 if (ev->status == 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003240 chan->ll_handle = __le16_to_cpu(ev->log_handle);
3241 chan->state = BT_CONNECTED;
Mat Martineau9f8d4672011-12-14 12:10:46 -08003242 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003243 chan->state = BT_CLOSED;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003244 }
Mat Martineau9f8d4672011-12-14 12:10:46 -08003245
3246 hci_proto_create_cfm(chan, ev->status);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003247 }
3248
3249 hci_dev_unlock(hdev);
3250}
3251
3252static inline void hci_flow_spec_modify_complete(struct hci_dev *hdev,
3253 struct sk_buff *skb)
3254{
3255 struct hci_ev_flow_spec_modify_complete *ev = (void *) skb->data;
3256 struct hci_chan *chan;
3257
3258 BT_DBG("%s handle %d status %d", hdev->name,
3259 __le16_to_cpu(ev->log_handle), ev->status);
3260
3261 hci_dev_lock(hdev);
3262
3263 chan = hci_chan_list_lookup_handle(hdev, ev->log_handle);
3264 if (chan)
3265 hci_proto_modify_cfm(chan, ev->status);
3266
3267 hci_dev_unlock(hdev);
3268}
3269
3270static inline void hci_disconn_log_link_complete_evt(struct hci_dev *hdev,
3271 struct sk_buff *skb)
3272{
3273 struct hci_ev_disconn_log_link_complete *ev = (void *) skb->data;
3274 struct hci_chan *chan;
3275
3276 BT_DBG("%s handle %d status %d", hdev->name,
3277 __le16_to_cpu(ev->log_handle), ev->status);
3278
3279 if (ev->status)
3280 return;
3281
3282 hci_dev_lock(hdev);
3283
3284 chan = hci_chan_list_lookup_handle(hdev, __le16_to_cpu(ev->log_handle));
Mat Martineau9f8d4672011-12-14 12:10:46 -08003285 if (chan)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003286 hci_proto_destroy_cfm(chan, ev->reason);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003287
3288 hci_dev_unlock(hdev);
3289}
3290
3291static inline void hci_disconn_phy_link_complete_evt(struct hci_dev *hdev,
3292 struct sk_buff *skb)
3293{
3294 struct hci_ev_disconn_phys_link_complete *ev = (void *) skb->data;
3295 struct hci_conn *conn;
3296
3297 BT_DBG("%s status %d", hdev->name, ev->status);
3298
3299 if (ev->status)
3300 return;
3301
3302 hci_dev_lock(hdev);
3303
3304 conn = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
3305 if (conn) {
3306 conn->state = BT_CLOSED;
3307
Mat Martineau3b9239a2012-02-16 11:54:30 -08003308 hci_proto_disconn_cfm(conn, ev->reason, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003309 hci_conn_del(conn);
3310 }
3311
3312 hci_dev_unlock(hdev);
3313}
3314
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
3316{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003317 struct hci_event_hdr *hdr = (void *) skb->data;
3318 __u8 event = hdr->evt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319
Brian Gixdfdd9362011-08-18 09:58:02 -07003320 BT_DBG("");
3321
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 skb_pull(skb, HCI_EVENT_HDR_SIZE);
3323
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003324 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 case HCI_EV_INQUIRY_COMPLETE:
3326 hci_inquiry_complete_evt(hdev, skb);
3327 break;
3328
3329 case HCI_EV_INQUIRY_RESULT:
3330 hci_inquiry_result_evt(hdev, skb);
3331 break;
3332
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003333 case HCI_EV_CONN_COMPLETE:
3334 hci_conn_complete_evt(hdev, skb);
Marcel Holtmann21d9e302005-09-13 01:32:25 +02003335 break;
3336
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 case HCI_EV_CONN_REQUEST:
3338 hci_conn_request_evt(hdev, skb);
3339 break;
3340
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 case HCI_EV_DISCONN_COMPLETE:
3342 hci_disconn_complete_evt(hdev, skb);
3343 break;
3344
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 case HCI_EV_AUTH_COMPLETE:
3346 hci_auth_complete_evt(hdev, skb);
3347 break;
3348
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003349 case HCI_EV_REMOTE_NAME:
3350 hci_remote_name_evt(hdev, skb);
3351 break;
3352
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 case HCI_EV_ENCRYPT_CHANGE:
3354 hci_encrypt_change_evt(hdev, skb);
3355 break;
3356
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003357 case HCI_EV_CHANGE_LINK_KEY_COMPLETE:
3358 hci_change_link_key_complete_evt(hdev, skb);
3359 break;
3360
3361 case HCI_EV_REMOTE_FEATURES:
3362 hci_remote_features_evt(hdev, skb);
3363 break;
3364
3365 case HCI_EV_REMOTE_VERSION:
3366 hci_remote_version_evt(hdev, skb);
3367 break;
3368
3369 case HCI_EV_QOS_SETUP_COMPLETE:
3370 hci_qos_setup_complete_evt(hdev, skb);
3371 break;
3372
3373 case HCI_EV_CMD_COMPLETE:
3374 hci_cmd_complete_evt(hdev, skb);
3375 break;
3376
3377 case HCI_EV_CMD_STATUS:
3378 hci_cmd_status_evt(hdev, skb);
3379 break;
3380
3381 case HCI_EV_ROLE_CHANGE:
3382 hci_role_change_evt(hdev, skb);
3383 break;
3384
3385 case HCI_EV_NUM_COMP_PKTS:
3386 hci_num_comp_pkts_evt(hdev, skb);
3387 break;
3388
3389 case HCI_EV_MODE_CHANGE:
3390 hci_mode_change_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 break;
3392
3393 case HCI_EV_PIN_CODE_REQ:
3394 hci_pin_code_request_evt(hdev, skb);
3395 break;
3396
3397 case HCI_EV_LINK_KEY_REQ:
3398 hci_link_key_request_evt(hdev, skb);
3399 break;
3400
3401 case HCI_EV_LINK_KEY_NOTIFY:
3402 hci_link_key_notify_evt(hdev, skb);
3403 break;
3404
3405 case HCI_EV_CLOCK_OFFSET:
3406 hci_clock_offset_evt(hdev, skb);
3407 break;
3408
Marcel Holtmanna8746412008-07-14 20:13:46 +02003409 case HCI_EV_PKT_TYPE_CHANGE:
3410 hci_pkt_type_change_evt(hdev, skb);
3411 break;
3412
Marcel Holtmann85a1e932005-08-09 20:28:02 -07003413 case HCI_EV_PSCAN_REP_MODE:
3414 hci_pscan_rep_mode_evt(hdev, skb);
3415 break;
3416
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003417 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
3418 hci_inquiry_result_with_rssi_evt(hdev, skb);
3419 break;
3420
3421 case HCI_EV_REMOTE_EXT_FEATURES:
3422 hci_remote_ext_features_evt(hdev, skb);
3423 break;
3424
3425 case HCI_EV_SYNC_CONN_COMPLETE:
3426 hci_sync_conn_complete_evt(hdev, skb);
3427 break;
3428
3429 case HCI_EV_SYNC_CONN_CHANGED:
3430 hci_sync_conn_changed_evt(hdev, skb);
3431 break;
3432
Marcel Holtmann04837f62006-07-03 10:02:33 +02003433 case HCI_EV_SNIFF_SUBRATE:
3434 hci_sniff_subrate_evt(hdev, skb);
3435 break;
3436
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003437 case HCI_EV_EXTENDED_INQUIRY_RESULT:
3438 hci_extended_inquiry_result_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 break;
3440
Marcel Holtmann04936842008-07-14 20:13:48 +02003441 case HCI_EV_IO_CAPA_REQUEST:
3442 hci_io_capa_request_evt(hdev, skb);
3443 break;
3444
Johan Hedberg03b555e2011-01-04 15:40:05 +02003445 case HCI_EV_IO_CAPA_REPLY:
3446 hci_io_capa_reply_evt(hdev, skb);
3447 break;
3448
Brian Gixa68668b2011-08-11 15:49:36 -07003449 case HCI_EV_USER_PASSKEY_REQUEST:
3450 case HCI_EV_USER_PASSKEY_NOTIFICATION:
Johan Hedberga5c29682011-02-19 12:05:57 -03003451 case HCI_EV_USER_CONFIRM_REQUEST:
Brian Gixa68668b2011-08-11 15:49:36 -07003452 hci_user_ssp_confirmation_evt(hdev, event, skb);
Johan Hedberga5c29682011-02-19 12:05:57 -03003453 break;
3454
Marcel Holtmann04936842008-07-14 20:13:48 +02003455 case HCI_EV_SIMPLE_PAIR_COMPLETE:
3456 hci_simple_pair_complete_evt(hdev, skb);
3457 break;
3458
Marcel Holtmann41a96212008-07-14 20:13:48 +02003459 case HCI_EV_REMOTE_HOST_FEATURES:
3460 hci_remote_host_features_evt(hdev, skb);
3461 break;
3462
Ville Tervofcd89c02011-02-10 22:38:47 -03003463 case HCI_EV_LE_META:
3464 hci_le_meta_evt(hdev, skb);
3465 break;
3466
Szymon Janc2763eda2011-03-22 13:12:22 +01003467 case HCI_EV_REMOTE_OOB_DATA_REQUEST:
3468 hci_remote_oob_data_request_evt(hdev, skb);
3469 break;
3470
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003471 case HCI_EV_PHYS_LINK_COMPLETE:
3472 hci_phy_link_complete(hdev, skb);
3473 hci_amp_event_packet(hdev, event, skb);
3474 break;
3475
3476 case HCI_EV_LOG_LINK_COMPLETE:
3477 hci_log_link_complete(hdev, skb);
3478 break;
3479
3480 case HCI_EV_FLOW_SPEC_MODIFY_COMPLETE:
3481 hci_flow_spec_modify_complete(hdev, skb);
3482 break;
3483
3484 case HCI_EV_DISCONN_LOG_LINK_COMPLETE:
3485 hci_disconn_log_link_complete_evt(hdev, skb);
3486 break;
3487
3488 case HCI_EV_DISCONN_PHYS_LINK_COMPLETE:
3489 hci_disconn_phy_link_complete_evt(hdev, skb);
3490 hci_amp_event_packet(hdev, event, skb);
3491 break;
3492
3493 case HCI_EV_NUM_COMP_BLOCKS:
3494 hci_num_comp_blocks_evt(hdev, skb);
3495 break;
3496
3497 case HCI_EV_CHANNEL_SELECTED:
3498 hci_amp_event_packet(hdev, event, skb);
3499 break;
3500
3501 case HCI_EV_AMP_STATUS_CHANGE:
3502 hci_amp_event_packet(hdev, event, skb);
3503 break;
3504
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003505 default:
3506 BT_DBG("%s event 0x%x", hdev->name, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 break;
3508 }
3509
3510 kfree_skb(skb);
3511 hdev->stat.evt_rx++;
3512}
3513
3514/* Generate internal stack event */
3515void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
3516{
3517 struct hci_event_hdr *hdr;
3518 struct hci_ev_stack_internal *ev;
3519 struct sk_buff *skb;
3520
3521 skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC);
3522 if (!skb)
3523 return;
3524
3525 hdr = (void *) skb_put(skb, HCI_EVENT_HDR_SIZE);
3526 hdr->evt = HCI_EV_STACK_INTERNAL;
3527 hdr->plen = sizeof(*ev) + dlen;
3528
3529 ev = (void *) skb_put(skb, sizeof(*ev) + dlen);
3530 ev->type = type;
3531 memcpy(ev->data, data, dlen);
3532
Marcel Holtmann576c7d82005-08-06 12:36:54 +02003533 bt_cb(skb)->incoming = 1;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07003534 __net_timestamp(skb);
Marcel Holtmann576c7d82005-08-06 12:36:54 +02003535
Marcel Holtmann0d48d932005-08-09 20:30:28 -07003536 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 skb->dev = (void *) hdev;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02003538 hci_send_to_sock(hdev, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 kfree_skb(skb);
3540}