blob: 335c60bad96c24055d2ab0439e1ccc59121cf9fa [file] [log] [blame]
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 BlueZ - Bluetooth protocol stack for Linux
Ron Shaffer2d0a0342010-05-28 11:53:46 -04003 Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation;
10
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090015 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090020 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 SOFTWARE IS DISCLAIMED.
23*/
24
25/* Bluetooth HCI event handling. */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <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
Marcel Holtmanna9de9242007-10-20 13:33:56 +020059 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
75 clear_bit(HCI_INQUIRY, &hdev->flags);
76
77 hci_conn_check_pending(hdev);
78}
79
80static void hci_cc_remote_name_req_cancel(struct hci_dev *hdev, struct sk_buff *skb)
81{
82 BT_DBG("%s", hdev->name);
83}
84
85static void hci_cc_role_discovery(struct hci_dev *hdev, struct sk_buff *skb)
86{
87 struct hci_rp_role_discovery *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Marcel Holtmanna9de9242007-10-20 13:33:56 +020090 BT_DBG("%s status 0x%x", hdev->name, rp->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Marcel Holtmanna9de9242007-10-20 13:33:56 +020092 if (rp->status)
93 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Marcel Holtmanna9de9242007-10-20 13:33:56 +020095 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Marcel Holtmanna9de9242007-10-20 13:33:56 +020097 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
98 if (conn) {
99 if (rp->role)
100 conn->link_mode &= ~HCI_LM_MASTER;
101 else
102 conn->link_mode |= HCI_LM_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200104
105 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200108static void hci_cc_read_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
109{
110 struct hci_rp_read_link_policy *rp = (void *) skb->data;
111 struct hci_conn *conn;
112
113 BT_DBG("%s status 0x%x", hdev->name, rp->status);
114
115 if (rp->status)
116 return;
117
118 hci_dev_lock(hdev);
119
120 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
121 if (conn)
122 conn->link_policy = __le16_to_cpu(rp->policy);
123
124 hci_dev_unlock(hdev);
125}
126
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200127static void hci_cc_write_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200129 struct hci_rp_write_link_policy *rp = (void *) skb->data;
130 struct hci_conn *conn;
131 void *sent;
132
133 BT_DBG("%s status 0x%x", hdev->name, rp->status);
134
135 if (rp->status)
136 return;
137
138 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LINK_POLICY);
139 if (!sent)
140 return;
141
142 hci_dev_lock(hdev);
143
144 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200145 if (conn)
Harvey Harrison83985312008-05-02 16:25:46 -0700146 conn->link_policy = get_unaligned_le16(sent + 2);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200147
148 hci_dev_unlock(hdev);
149}
150
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200151static void hci_cc_read_def_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
152{
153 struct hci_rp_read_def_link_policy *rp = (void *) skb->data;
154
155 BT_DBG("%s status 0x%x", hdev->name, rp->status);
156
157 if (rp->status)
158 return;
159
160 hdev->link_policy = __le16_to_cpu(rp->policy);
161}
162
163static void hci_cc_write_def_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
164{
165 __u8 status = *((__u8 *) skb->data);
166 void *sent;
167
168 BT_DBG("%s status 0x%x", hdev->name, status);
169
170 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_LINK_POLICY);
171 if (!sent)
172 return;
173
174 if (!status)
175 hdev->link_policy = get_unaligned_le16(sent);
176
Johan Hedberg23bb5762010-12-21 23:01:27 +0200177 hci_req_complete(hdev, HCI_OP_WRITE_DEF_LINK_POLICY, status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200178}
179
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200180static void hci_cc_reset(struct hci_dev *hdev, struct sk_buff *skb)
181{
182 __u8 status = *((__u8 *) skb->data);
183
184 BT_DBG("%s status 0x%x", hdev->name, status);
185
Johan Hedberg23bb5762010-12-21 23:01:27 +0200186 hci_req_complete(hdev, HCI_OP_RESET, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200187}
188
189static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb)
190{
191 __u8 status = *((__u8 *) skb->data);
192 void *sent;
193
194 BT_DBG("%s status 0x%x", hdev->name, status);
195
Marcel Holtmannf383f272008-07-14 20:13:47 +0200196 if (status)
197 return;
198
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200199 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LOCAL_NAME);
200 if (!sent)
201 return;
202
Marcel Holtmannf383f272008-07-14 20:13:47 +0200203 memcpy(hdev->dev_name, sent, 248);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200204}
205
206static void hci_cc_read_local_name(struct hci_dev *hdev, struct sk_buff *skb)
207{
208 struct hci_rp_read_local_name *rp = (void *) skb->data;
209
210 BT_DBG("%s status 0x%x", hdev->name, rp->status);
211
212 if (rp->status)
213 return;
214
215 memcpy(hdev->dev_name, rp->name, 248);
216}
217
218static void hci_cc_write_auth_enable(struct hci_dev *hdev, struct sk_buff *skb)
219{
220 __u8 status = *((__u8 *) skb->data);
221 void *sent;
222
223 BT_DBG("%s status 0x%x", hdev->name, status);
224
225 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_ENABLE);
226 if (!sent)
227 return;
228
229 if (!status) {
230 __u8 param = *((__u8 *) sent);
231
232 if (param == AUTH_ENABLED)
233 set_bit(HCI_AUTH, &hdev->flags);
234 else
235 clear_bit(HCI_AUTH, &hdev->flags);
236 }
237
Johan Hedberg23bb5762010-12-21 23:01:27 +0200238 hci_req_complete(hdev, HCI_OP_WRITE_AUTH_ENABLE, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200239}
240
241static void hci_cc_write_encrypt_mode(struct hci_dev *hdev, struct sk_buff *skb)
242{
243 __u8 status = *((__u8 *) skb->data);
244 void *sent;
245
246 BT_DBG("%s status 0x%x", hdev->name, status);
247
248 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_ENCRYPT_MODE);
249 if (!sent)
250 return;
251
252 if (!status) {
253 __u8 param = *((__u8 *) sent);
254
255 if (param)
256 set_bit(HCI_ENCRYPT, &hdev->flags);
257 else
258 clear_bit(HCI_ENCRYPT, &hdev->flags);
259 }
260
Johan Hedberg23bb5762010-12-21 23:01:27 +0200261 hci_req_complete(hdev, HCI_OP_WRITE_ENCRYPT_MODE, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200262}
263
264static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
265{
266 __u8 status = *((__u8 *) skb->data);
267 void *sent;
268
269 BT_DBG("%s status 0x%x", hdev->name, status);
270
271 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SCAN_ENABLE);
272 if (!sent)
273 return;
274
275 if (!status) {
276 __u8 param = *((__u8 *) sent);
Johan Hedberg9fbcbb42010-12-30 00:18:33 +0200277 int old_pscan, old_iscan;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200278
Johan Hedberg9fbcbb42010-12-30 00:18:33 +0200279 old_pscan = test_and_clear_bit(HCI_PSCAN, &hdev->flags);
280 old_iscan = test_and_clear_bit(HCI_ISCAN, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200281
Johan Hedberg73f22f62010-12-29 16:00:25 +0200282 if (param & SCAN_INQUIRY) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200283 set_bit(HCI_ISCAN, &hdev->flags);
Johan Hedberg9fbcbb42010-12-30 00:18:33 +0200284 if (!old_iscan)
285 mgmt_discoverable(hdev->id, 1);
286 } else if (old_iscan)
Johan Hedberg73f22f62010-12-29 16:00:25 +0200287 mgmt_discoverable(hdev->id, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200288
Johan Hedberg9fbcbb42010-12-30 00:18:33 +0200289 if (param & SCAN_PAGE) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200290 set_bit(HCI_PSCAN, &hdev->flags);
Johan Hedberg9fbcbb42010-12-30 00:18:33 +0200291 if (!old_pscan)
292 mgmt_connectable(hdev->id, 1);
293 } else if (old_pscan)
294 mgmt_connectable(hdev->id, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200295 }
296
Johan Hedberg23bb5762010-12-21 23:01:27 +0200297 hci_req_complete(hdev, HCI_OP_WRITE_SCAN_ENABLE, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200298}
299
300static void hci_cc_read_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
301{
302 struct hci_rp_read_class_of_dev *rp = (void *) skb->data;
303
304 BT_DBG("%s status 0x%x", hdev->name, rp->status);
305
306 if (rp->status)
307 return;
308
309 memcpy(hdev->dev_class, rp->dev_class, 3);
310
311 BT_DBG("%s class 0x%.2x%.2x%.2x", hdev->name,
312 hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
313}
314
315static void hci_cc_write_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
316{
317 __u8 status = *((__u8 *) skb->data);
318 void *sent;
319
320 BT_DBG("%s status 0x%x", hdev->name, status);
321
Marcel Holtmannf383f272008-07-14 20:13:47 +0200322 if (status)
323 return;
324
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200325 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_CLASS_OF_DEV);
326 if (!sent)
327 return;
328
Marcel Holtmannf383f272008-07-14 20:13:47 +0200329 memcpy(hdev->dev_class, sent, 3);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200330}
331
332static void hci_cc_read_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
333{
334 struct hci_rp_read_voice_setting *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 __u16 setting;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200336
337 BT_DBG("%s status 0x%x", hdev->name, rp->status);
338
339 if (rp->status)
340 return;
341
342 setting = __le16_to_cpu(rp->voice_setting);
343
Marcel Holtmannf383f272008-07-14 20:13:47 +0200344 if (hdev->voice_setting == setting)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200345 return;
346
347 hdev->voice_setting = setting;
348
349 BT_DBG("%s voice setting 0x%04x", hdev->name, setting);
350
351 if (hdev->notify) {
352 tasklet_disable(&hdev->tx_task);
353 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
354 tasklet_enable(&hdev->tx_task);
355 }
356}
357
358static void hci_cc_write_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
359{
360 __u8 status = *((__u8 *) skb->data);
Marcel Holtmannf383f272008-07-14 20:13:47 +0200361 __u16 setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 void *sent;
363
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200364 BT_DBG("%s status 0x%x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Marcel Holtmannf383f272008-07-14 20:13:47 +0200366 if (status)
367 return;
368
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200369 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_VOICE_SETTING);
370 if (!sent)
371 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Marcel Holtmannf383f272008-07-14 20:13:47 +0200373 setting = get_unaligned_le16(sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Marcel Holtmannf383f272008-07-14 20:13:47 +0200375 if (hdev->voice_setting == setting)
376 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Marcel Holtmannf383f272008-07-14 20:13:47 +0200378 hdev->voice_setting = setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Marcel Holtmannf383f272008-07-14 20:13:47 +0200380 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387}
388
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200389static void hci_cc_host_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200391 __u8 status = *((__u8 *) skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200393 BT_DBG("%s status 0x%x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Johan Hedberg23bb5762010-12-21 23:01:27 +0200395 hci_req_complete(hdev, HCI_OP_HOST_BUFFER_SIZE, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Marcel Holtmann333140b2008-07-14 20:13:48 +0200398static void hci_cc_read_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
399{
400 struct hci_rp_read_ssp_mode *rp = (void *) skb->data;
401
402 BT_DBG("%s status 0x%x", hdev->name, rp->status);
403
404 if (rp->status)
405 return;
406
407 hdev->ssp_mode = rp->mode;
408}
409
410static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
411{
412 __u8 status = *((__u8 *) skb->data);
413 void *sent;
414
415 BT_DBG("%s status 0x%x", hdev->name, status);
416
417 if (status)
418 return;
419
420 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_MODE);
421 if (!sent)
422 return;
423
424 hdev->ssp_mode = *((__u8 *) sent);
425}
426
Johan Hedbergd5859e22011-01-25 01:19:58 +0200427static u8 hci_get_inquiry_mode(struct hci_dev *hdev)
428{
429 if (hdev->features[6] & LMP_EXT_INQ)
430 return 2;
431
432 if (hdev->features[3] & LMP_RSSI_INQ)
433 return 1;
434
435 if (hdev->manufacturer == 11 && hdev->hci_rev == 0x00 &&
436 hdev->lmp_subver == 0x0757)
437 return 1;
438
439 if (hdev->manufacturer == 15) {
440 if (hdev->hci_rev == 0x03 && hdev->lmp_subver == 0x6963)
441 return 1;
442 if (hdev->hci_rev == 0x09 && hdev->lmp_subver == 0x6963)
443 return 1;
444 if (hdev->hci_rev == 0x00 && hdev->lmp_subver == 0x6965)
445 return 1;
446 }
447
448 if (hdev->manufacturer == 31 && hdev->hci_rev == 0x2005 &&
449 hdev->lmp_subver == 0x1805)
450 return 1;
451
452 return 0;
453}
454
455static void hci_setup_inquiry_mode(struct hci_dev *hdev)
456{
457 u8 mode;
458
459 mode = hci_get_inquiry_mode(hdev);
460
461 hci_send_cmd(hdev, HCI_OP_WRITE_INQUIRY_MODE, 1, &mode);
462}
463
464static void hci_setup_event_mask(struct hci_dev *hdev)
465{
466 /* The second byte is 0xff instead of 0x9f (two reserved bits
467 * disabled) since a Broadcom 1.2 dongle doesn't respond to the
468 * command otherwise */
469 u8 events[8] = { 0xff, 0xff, 0xfb, 0xff, 0x00, 0x00, 0x00, 0x00 };
470
471 /* Events for 1.2 and newer controllers */
472 if (hdev->lmp_ver > 1) {
473 events[4] |= 0x01; /* Flow Specification Complete */
474 events[4] |= 0x02; /* Inquiry Result with RSSI */
475 events[4] |= 0x04; /* Read Remote Extended Features Complete */
476 events[5] |= 0x08; /* Synchronous Connection Complete */
477 events[5] |= 0x10; /* Synchronous Connection Changed */
478 }
479
480 if (hdev->features[3] & LMP_RSSI_INQ)
481 events[4] |= 0x04; /* Inquiry Result with RSSI */
482
483 if (hdev->features[5] & LMP_SNIFF_SUBR)
484 events[5] |= 0x20; /* Sniff Subrating */
485
486 if (hdev->features[5] & LMP_PAUSE_ENC)
487 events[5] |= 0x80; /* Encryption Key Refresh Complete */
488
489 if (hdev->features[6] & LMP_EXT_INQ)
490 events[5] |= 0x40; /* Extended Inquiry Result */
491
492 if (hdev->features[6] & LMP_NO_FLUSH)
493 events[7] |= 0x01; /* Enhanced Flush Complete */
494
495 if (hdev->features[7] & LMP_LSTO)
496 events[6] |= 0x80; /* Link Supervision Timeout Changed */
497
498 if (hdev->features[6] & LMP_SIMPLE_PAIR) {
499 events[6] |= 0x01; /* IO Capability Request */
500 events[6] |= 0x02; /* IO Capability Response */
501 events[6] |= 0x04; /* User Confirmation Request */
502 events[6] |= 0x08; /* User Passkey Request */
503 events[6] |= 0x10; /* Remote OOB Data Request */
504 events[6] |= 0x20; /* Simple Pairing Complete */
505 events[7] |= 0x04; /* User Passkey Notification */
506 events[7] |= 0x08; /* Keypress Notification */
507 events[7] |= 0x10; /* Remote Host Supported
508 * Features Notification */
509 }
510
511 if (hdev->features[4] & LMP_LE)
512 events[7] |= 0x20; /* LE Meta-Event */
513
514 hci_send_cmd(hdev, HCI_OP_SET_EVENT_MASK, sizeof(events), events);
515}
516
517static void hci_setup(struct hci_dev *hdev)
518{
519 hci_setup_event_mask(hdev);
520
521 if (hdev->lmp_ver > 1)
522 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL);
523
524 if (hdev->features[6] & LMP_SIMPLE_PAIR) {
525 u8 mode = 0x01;
526 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, sizeof(mode), &mode);
527 }
528
529 if (hdev->features[3] & LMP_RSSI_INQ)
530 hci_setup_inquiry_mode(hdev);
531
532 if (hdev->features[7] & LMP_INQ_TX_PWR)
533 hci_send_cmd(hdev, HCI_OP_READ_INQ_RSP_TX_POWER, 0, NULL);
534}
535
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200536static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
537{
538 struct hci_rp_read_local_version *rp = (void *) skb->data;
539
540 BT_DBG("%s status 0x%x", hdev->name, rp->status);
541
542 if (rp->status)
543 return;
544
545 hdev->hci_ver = rp->hci_ver;
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200546 hdev->hci_rev = __le16_to_cpu(rp->hci_rev);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200547 hdev->lmp_ver = rp->lmp_ver;
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200548 hdev->manufacturer = __le16_to_cpu(rp->manufacturer);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200549 hdev->lmp_subver = __le16_to_cpu(rp->lmp_subver);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200550
551 BT_DBG("%s manufacturer %d hci ver %d:%d", hdev->name,
552 hdev->manufacturer,
553 hdev->hci_ver, hdev->hci_rev);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200554
555 if (test_bit(HCI_INIT, &hdev->flags))
556 hci_setup(hdev);
557}
558
559static void hci_setup_link_policy(struct hci_dev *hdev)
560{
561 u16 link_policy = 0;
562
563 if (hdev->features[0] & LMP_RSWITCH)
564 link_policy |= HCI_LP_RSWITCH;
565 if (hdev->features[0] & LMP_HOLD)
566 link_policy |= HCI_LP_HOLD;
567 if (hdev->features[0] & LMP_SNIFF)
568 link_policy |= HCI_LP_SNIFF;
569 if (hdev->features[1] & LMP_PARK)
570 link_policy |= HCI_LP_PARK;
571
572 link_policy = cpu_to_le16(link_policy);
573 hci_send_cmd(hdev, HCI_OP_WRITE_DEF_LINK_POLICY,
574 sizeof(link_policy), &link_policy);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200575}
576
577static void hci_cc_read_local_commands(struct hci_dev *hdev, struct sk_buff *skb)
578{
579 struct hci_rp_read_local_commands *rp = (void *) skb->data;
580
581 BT_DBG("%s status 0x%x", hdev->name, rp->status);
582
583 if (rp->status)
Johan Hedbergd5859e22011-01-25 01:19:58 +0200584 goto done;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200585
586 memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
Johan Hedbergd5859e22011-01-25 01:19:58 +0200587
588 if (test_bit(HCI_INIT, &hdev->flags) && (hdev->commands[5] & 0x10))
589 hci_setup_link_policy(hdev);
590
591done:
592 hci_req_complete(hdev, HCI_OP_READ_LOCAL_COMMANDS, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200593}
594
595static void hci_cc_read_local_features(struct hci_dev *hdev, struct sk_buff *skb)
596{
597 struct hci_rp_read_local_features *rp = (void *) skb->data;
598
599 BT_DBG("%s status 0x%x", hdev->name, rp->status);
600
601 if (rp->status)
602 return;
603
604 memcpy(hdev->features, rp->features, 8);
605
606 /* Adjust default settings according to features
607 * supported by device. */
608
609 if (hdev->features[0] & LMP_3SLOT)
610 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
611
612 if (hdev->features[0] & LMP_5SLOT)
613 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
614
615 if (hdev->features[1] & LMP_HV2) {
616 hdev->pkt_type |= (HCI_HV2);
617 hdev->esco_type |= (ESCO_HV2);
618 }
619
620 if (hdev->features[1] & LMP_HV3) {
621 hdev->pkt_type |= (HCI_HV3);
622 hdev->esco_type |= (ESCO_HV3);
623 }
624
625 if (hdev->features[3] & LMP_ESCO)
626 hdev->esco_type |= (ESCO_EV3);
627
628 if (hdev->features[4] & LMP_EV4)
629 hdev->esco_type |= (ESCO_EV4);
630
631 if (hdev->features[4] & LMP_EV5)
632 hdev->esco_type |= (ESCO_EV5);
633
Marcel Holtmannefc76882009-02-06 09:13:37 +0100634 if (hdev->features[5] & LMP_EDR_ESCO_2M)
635 hdev->esco_type |= (ESCO_2EV3);
636
637 if (hdev->features[5] & LMP_EDR_ESCO_3M)
638 hdev->esco_type |= (ESCO_3EV3);
639
640 if (hdev->features[5] & LMP_EDR_3S_ESCO)
641 hdev->esco_type |= (ESCO_2EV5 | ESCO_3EV5);
642
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200643 BT_DBG("%s features 0x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x", hdev->name,
644 hdev->features[0], hdev->features[1],
645 hdev->features[2], hdev->features[3],
646 hdev->features[4], hdev->features[5],
647 hdev->features[6], hdev->features[7]);
648}
649
650static void hci_cc_read_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
651{
652 struct hci_rp_read_buffer_size *rp = (void *) skb->data;
653
654 BT_DBG("%s status 0x%x", hdev->name, rp->status);
655
656 if (rp->status)
657 return;
658
659 hdev->acl_mtu = __le16_to_cpu(rp->acl_mtu);
660 hdev->sco_mtu = rp->sco_mtu;
661 hdev->acl_pkts = __le16_to_cpu(rp->acl_max_pkt);
662 hdev->sco_pkts = __le16_to_cpu(rp->sco_max_pkt);
663
664 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
665 hdev->sco_mtu = 64;
666 hdev->sco_pkts = 8;
667 }
668
669 hdev->acl_cnt = hdev->acl_pkts;
670 hdev->sco_cnt = hdev->sco_pkts;
671
672 BT_DBG("%s acl mtu %d:%d sco mtu %d:%d", hdev->name,
673 hdev->acl_mtu, hdev->acl_pkts,
674 hdev->sco_mtu, hdev->sco_pkts);
675}
676
677static void hci_cc_read_bd_addr(struct hci_dev *hdev, struct sk_buff *skb)
678{
679 struct hci_rp_read_bd_addr *rp = (void *) skb->data;
680
681 BT_DBG("%s status 0x%x", hdev->name, rp->status);
682
683 if (!rp->status)
684 bacpy(&hdev->bdaddr, &rp->bdaddr);
685
Johan Hedberg23bb5762010-12-21 23:01:27 +0200686 hci_req_complete(hdev, HCI_OP_READ_BD_ADDR, rp->status);
687}
688
689static void hci_cc_write_ca_timeout(struct hci_dev *hdev, struct sk_buff *skb)
690{
691 __u8 status = *((__u8 *) skb->data);
692
693 BT_DBG("%s status 0x%x", hdev->name, status);
694
695 hci_req_complete(hdev, HCI_OP_WRITE_CA_TIMEOUT, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200696}
697
Johan Hedbergb0916ea2011-01-10 13:44:55 +0200698static void hci_cc_delete_stored_link_key(struct hci_dev *hdev,
699 struct sk_buff *skb)
700{
701 __u8 status = *((__u8 *) skb->data);
702
703 BT_DBG("%s status 0x%x", hdev->name, status);
704
705 hci_req_complete(hdev, HCI_OP_DELETE_STORED_LINK_KEY, status);
706}
707
Johan Hedbergd5859e22011-01-25 01:19:58 +0200708static void hci_cc_set_event_mask(struct hci_dev *hdev, struct sk_buff *skb)
709{
710 __u8 status = *((__u8 *) skb->data);
711
712 BT_DBG("%s status 0x%x", hdev->name, status);
713
714 hci_req_complete(hdev, HCI_OP_SET_EVENT_MASK, status);
715}
716
717static void hci_cc_write_inquiry_mode(struct hci_dev *hdev,
718 struct sk_buff *skb)
719{
720 __u8 status = *((__u8 *) skb->data);
721
722 BT_DBG("%s status 0x%x", hdev->name, status);
723
724 hci_req_complete(hdev, HCI_OP_WRITE_INQUIRY_MODE, status);
725}
726
727static void hci_cc_read_inq_rsp_tx_power(struct hci_dev *hdev,
728 struct sk_buff *skb)
729{
730 __u8 status = *((__u8 *) skb->data);
731
732 BT_DBG("%s status 0x%x", hdev->name, status);
733
734 hci_req_complete(hdev, HCI_OP_READ_INQ_RSP_TX_POWER, status);
735}
736
737static void hci_cc_set_event_flt(struct hci_dev *hdev, struct sk_buff *skb)
738{
739 __u8 status = *((__u8 *) skb->data);
740
741 BT_DBG("%s status 0x%x", hdev->name, status);
742
743 hci_req_complete(hdev, HCI_OP_SET_EVENT_FLT, status);
744}
745
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200746static inline void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
747{
748 BT_DBG("%s status 0x%x", hdev->name, status);
749
750 if (status) {
Johan Hedberg23bb5762010-12-21 23:01:27 +0200751 hci_req_complete(hdev, HCI_OP_INQUIRY, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200752
753 hci_conn_check_pending(hdev);
754 } else
755 set_bit(HCI_INQUIRY, &hdev->flags);
756}
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758static inline void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
759{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200760 struct hci_cp_create_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200763 BT_DBG("%s status 0x%x", hdev->name, status);
764
765 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_CONN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (!cp)
767 return;
768
769 hci_dev_lock(hdev);
770
771 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
772
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200773 BT_DBG("%s bdaddr %s conn %p", hdev->name, batostr(&cp->bdaddr), conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 if (status) {
776 if (conn && conn->state == BT_CONNECT) {
Marcel Holtmann4c67bc72006-10-15 17:30:56 +0200777 if (status != 0x0c || conn->attempt > 2) {
778 conn->state = BT_CLOSED;
779 hci_proto_connect_cfm(conn, status);
780 hci_conn_del(conn);
781 } else
782 conn->state = BT_CONNECT2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784 } else {
785 if (!conn) {
786 conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr);
787 if (conn) {
788 conn->out = 1;
789 conn->link_mode |= HCI_LM_MASTER;
790 } else
Gustavo F. Padovan893ef972010-07-18 15:13:37 -0300791 BT_ERR("No memory for new connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793 }
794
795 hci_dev_unlock(hdev);
796}
797
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200798static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200800 struct hci_cp_add_sco *cp;
801 struct hci_conn *acl, *sco;
802 __u16 handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200804 BT_DBG("%s status 0x%x", hdev->name, status);
805
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200806 if (!status)
807 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200809 cp = hci_sent_cmd_data(hdev, HCI_OP_ADD_SCO);
810 if (!cp)
811 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200813 handle = __le16_to_cpu(cp->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200815 BT_DBG("%s handle %d", hdev->name, handle);
Marcel Holtmann6bd57412006-11-18 22:14:22 +0100816
817 hci_dev_lock(hdev);
818
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200819 acl = hci_conn_hash_lookup_handle(hdev, handle);
820 if (acl && (sco = acl->link)) {
821 sco->state = BT_CLOSED;
822
823 hci_proto_connect_cfm(sco, status);
824 hci_conn_del(sco);
825 }
Marcel Holtmann6bd57412006-11-18 22:14:22 +0100826
827 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
Marcel Holtmannf8558552008-07-14 20:13:49 +0200830static void hci_cs_auth_requested(struct hci_dev *hdev, __u8 status)
831{
832 struct hci_cp_auth_requested *cp;
833 struct hci_conn *conn;
834
835 BT_DBG("%s status 0x%x", hdev->name, status);
836
837 if (!status)
838 return;
839
840 cp = hci_sent_cmd_data(hdev, HCI_OP_AUTH_REQUESTED);
841 if (!cp)
842 return;
843
844 hci_dev_lock(hdev);
845
846 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
847 if (conn) {
848 if (conn->state == BT_CONFIG) {
849 hci_proto_connect_cfm(conn, status);
850 hci_conn_put(conn);
851 }
852 }
853
854 hci_dev_unlock(hdev);
855}
856
857static void hci_cs_set_conn_encrypt(struct hci_dev *hdev, __u8 status)
858{
859 struct hci_cp_set_conn_encrypt *cp;
860 struct hci_conn *conn;
861
862 BT_DBG("%s status 0x%x", hdev->name, status);
863
864 if (!status)
865 return;
866
867 cp = hci_sent_cmd_data(hdev, HCI_OP_SET_CONN_ENCRYPT);
868 if (!cp)
869 return;
870
871 hci_dev_lock(hdev);
872
873 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
874 if (conn) {
875 if (conn->state == BT_CONFIG) {
876 hci_proto_connect_cfm(conn, status);
877 hci_conn_put(conn);
878 }
879 }
880
881 hci_dev_unlock(hdev);
882}
883
Johan Hedberg127178d2010-11-18 22:22:29 +0200884static int hci_outgoing_auth_needed(struct hci_dev *hdev,
Johan Hedberg392599b2010-11-18 22:22:28 +0200885 struct hci_conn *conn)
886{
Johan Hedberg392599b2010-11-18 22:22:28 +0200887 if (conn->state != BT_CONFIG || !conn->out)
888 return 0;
889
Johan Hedberg765c2a92011-01-19 12:06:52 +0530890 if (conn->pending_sec_level == BT_SECURITY_SDP)
Johan Hedberg392599b2010-11-18 22:22:28 +0200891 return 0;
892
893 /* Only request authentication for SSP connections or non-SSP
894 * devices with sec_level HIGH */
895 if (!(hdev->ssp_mode > 0 && conn->ssp_mode > 0) &&
Johan Hedberg765c2a92011-01-19 12:06:52 +0530896 conn->pending_sec_level != BT_SECURITY_HIGH)
Johan Hedberg392599b2010-11-18 22:22:28 +0200897 return 0;
898
Johan Hedberg392599b2010-11-18 22:22:28 +0200899 return 1;
900}
901
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200902static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
903{
Johan Hedberg127178d2010-11-18 22:22:29 +0200904 struct hci_cp_remote_name_req *cp;
905 struct hci_conn *conn;
906
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200907 BT_DBG("%s status 0x%x", hdev->name, status);
Johan Hedberg127178d2010-11-18 22:22:29 +0200908
909 /* If successful wait for the name req complete event before
910 * checking for the need to do authentication */
911 if (!status)
912 return;
913
914 cp = hci_sent_cmd_data(hdev, HCI_OP_REMOTE_NAME_REQ);
915 if (!cp)
916 return;
917
918 hci_dev_lock(hdev);
919
920 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
921 if (conn && hci_outgoing_auth_needed(hdev, conn)) {
922 struct hci_cp_auth_requested cp;
923 cp.handle = __cpu_to_le16(conn->handle);
924 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
925 }
926
927 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200928}
929
Marcel Holtmann769be972008-07-14 20:13:49 +0200930static void hci_cs_read_remote_features(struct hci_dev *hdev, __u8 status)
931{
932 struct hci_cp_read_remote_features *cp;
933 struct hci_conn *conn;
934
935 BT_DBG("%s status 0x%x", hdev->name, status);
936
937 if (!status)
938 return;
939
940 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_FEATURES);
941 if (!cp)
942 return;
943
944 hci_dev_lock(hdev);
945
946 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
947 if (conn) {
948 if (conn->state == BT_CONFIG) {
Marcel Holtmann769be972008-07-14 20:13:49 +0200949 hci_proto_connect_cfm(conn, status);
950 hci_conn_put(conn);
951 }
952 }
953
954 hci_dev_unlock(hdev);
955}
956
957static void hci_cs_read_remote_ext_features(struct hci_dev *hdev, __u8 status)
958{
959 struct hci_cp_read_remote_ext_features *cp;
960 struct hci_conn *conn;
961
962 BT_DBG("%s status 0x%x", hdev->name, status);
963
964 if (!status)
965 return;
966
967 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES);
968 if (!cp)
969 return;
970
971 hci_dev_lock(hdev);
972
973 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
974 if (conn) {
975 if (conn->state == BT_CONFIG) {
Marcel Holtmann769be972008-07-14 20:13:49 +0200976 hci_proto_connect_cfm(conn, status);
977 hci_conn_put(conn);
978 }
979 }
980
981 hci_dev_unlock(hdev);
982}
983
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200984static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
985{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200986 struct hci_cp_setup_sync_conn *cp;
987 struct hci_conn *acl, *sco;
988 __u16 handle;
989
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200990 BT_DBG("%s status 0x%x", hdev->name, status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200991
992 if (!status)
993 return;
994
995 cp = hci_sent_cmd_data(hdev, HCI_OP_SETUP_SYNC_CONN);
996 if (!cp)
997 return;
998
999 handle = __le16_to_cpu(cp->handle);
1000
1001 BT_DBG("%s handle %d", hdev->name, handle);
1002
1003 hci_dev_lock(hdev);
1004
1005 acl = hci_conn_hash_lookup_handle(hdev, handle);
1006 if (acl && (sco = acl->link)) {
1007 sco->state = BT_CLOSED;
1008
1009 hci_proto_connect_cfm(sco, status);
1010 hci_conn_del(sco);
1011 }
1012
1013 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001014}
1015
1016static void hci_cs_sniff_mode(struct hci_dev *hdev, __u8 status)
1017{
1018 struct hci_cp_sniff_mode *cp;
1019 struct hci_conn *conn;
1020
1021 BT_DBG("%s status 0x%x", hdev->name, status);
1022
1023 if (!status)
1024 return;
1025
1026 cp = hci_sent_cmd_data(hdev, HCI_OP_SNIFF_MODE);
1027 if (!cp)
1028 return;
1029
1030 hci_dev_lock(hdev);
1031
1032 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001033 if (conn) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001034 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend);
1035
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001036 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->pend))
1037 hci_sco_setup(conn, status);
1038 }
1039
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001040 hci_dev_unlock(hdev);
1041}
1042
1043static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
1044{
1045 struct hci_cp_exit_sniff_mode *cp;
1046 struct hci_conn *conn;
1047
1048 BT_DBG("%s status 0x%x", hdev->name, status);
1049
1050 if (!status)
1051 return;
1052
1053 cp = hci_sent_cmd_data(hdev, HCI_OP_EXIT_SNIFF_MODE);
1054 if (!cp)
1055 return;
1056
1057 hci_dev_lock(hdev);
1058
1059 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001060 if (conn) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001061 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend);
1062
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001063 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->pend))
1064 hci_sco_setup(conn, status);
1065 }
1066
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001067 hci_dev_unlock(hdev);
1068}
1069
1070static inline void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1071{
1072 __u8 status = *((__u8 *) skb->data);
1073
1074 BT_DBG("%s status %d", hdev->name, status);
1075
1076 clear_bit(HCI_INQUIRY, &hdev->flags);
1077
Johan Hedberg23bb5762010-12-21 23:01:27 +02001078 hci_req_complete(hdev, HCI_OP_INQUIRY, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001079
1080 hci_conn_check_pending(hdev);
1081}
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083static inline void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
1084{
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001085 struct inquiry_data data;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001086 struct inquiry_info *info = (void *) (skb->data + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 int num_rsp = *((__u8 *) skb->data);
1088
1089 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
1090
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001091 if (!num_rsp)
1092 return;
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 for (; num_rsp; num_rsp--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 bacpy(&data.bdaddr, &info->bdaddr);
1098 data.pscan_rep_mode = info->pscan_rep_mode;
1099 data.pscan_period_mode = info->pscan_period_mode;
1100 data.pscan_mode = info->pscan_mode;
1101 memcpy(data.dev_class, info->dev_class, 3);
1102 data.clock_offset = info->clock_offset;
1103 data.rssi = 0x00;
Marcel Holtmann41a96212008-07-14 20:13:48 +02001104 data.ssp_mode = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 info++;
1106 hci_inquiry_cache_update(hdev, &data);
1107 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 hci_dev_unlock(hdev);
1110}
1111
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001112static inline void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001114 struct hci_ev_conn_complete *ev = (void *) skb->data;
1115 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001117 BT_DBG("%s", hdev->name);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001120
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001121 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann94992372009-04-19 19:30:03 +02001122 if (!conn) {
1123 if (ev->link_type != SCO_LINK)
1124 goto unlock;
1125
1126 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
1127 if (!conn)
1128 goto unlock;
1129
1130 conn->type = SCO_LINK;
1131 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001132
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001133 if (!ev->status) {
1134 conn->handle = __le16_to_cpu(ev->handle);
Marcel Holtmann769be972008-07-14 20:13:49 +02001135
1136 if (conn->type == ACL_LINK) {
1137 conn->state = BT_CONFIG;
1138 hci_conn_hold(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02001139 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Johan Hedbergf7520542011-01-20 12:34:39 +02001140 mgmt_connected(hdev->id, &ev->bdaddr);
Marcel Holtmann769be972008-07-14 20:13:49 +02001141 } else
1142 conn->state = BT_CONNECTED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001143
Marcel Holtmann9eba32b2009-08-22 14:19:26 -07001144 hci_conn_hold_device(conn);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02001145 hci_conn_add_sysfs(conn);
1146
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001147 if (test_bit(HCI_AUTH, &hdev->flags))
1148 conn->link_mode |= HCI_LM_AUTH;
1149
1150 if (test_bit(HCI_ENCRYPT, &hdev->flags))
1151 conn->link_mode |= HCI_LM_ENCRYPT;
1152
1153 /* Get remote features */
1154 if (conn->type == ACL_LINK) {
1155 struct hci_cp_read_remote_features cp;
1156 cp.handle = ev->handle;
Marcel Holtmann769be972008-07-14 20:13:49 +02001157 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES,
1158 sizeof(cp), &cp);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001159 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001160
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001161 /* Set packet type for incoming connection */
Marcel Holtmanna8746412008-07-14 20:13:46 +02001162 if (!conn->out && hdev->hci_ver < 3) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001163 struct hci_cp_change_conn_ptype cp;
1164 cp.handle = ev->handle;
Marcel Holtmanna8746412008-07-14 20:13:46 +02001165 cp.pkt_type = cpu_to_le16(conn->pkt_type);
1166 hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE,
1167 sizeof(cp), &cp);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001168 }
1169 } else
1170 conn->state = BT_CLOSED;
1171
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001172 if (conn->type == ACL_LINK)
1173 hci_sco_setup(conn, ev->status);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001174
Marcel Holtmann769be972008-07-14 20:13:49 +02001175 if (ev->status) {
1176 hci_proto_connect_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001177 hci_conn_del(conn);
Marcel Holtmannc89b6e62009-01-15 21:57:03 +01001178 } else if (ev->link_type != ACL_LINK)
1179 hci_proto_connect_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001180
1181unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001183
1184 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187static inline void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
1188{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001189 struct hci_ev_conn_request *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 int mask = hdev->link_mode;
1191
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001192 BT_DBG("%s bdaddr %s type 0x%x", hdev->name,
1193 batostr(&ev->bdaddr), ev->link_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type);
1196
Johan Hedbergf0358562010-05-18 13:20:32 +02001197 if ((mask & HCI_LM_ACCEPT) && !hci_blacklist_lookup(hdev, &ev->bdaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 /* Connection accepted */
Marcel Holtmannc7bdd502008-07-14 20:13:47 +02001199 struct inquiry_entry *ie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 hci_dev_lock(hdev);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001203
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02001204 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
1205 if (ie)
Marcel Holtmannc7bdd502008-07-14 20:13:47 +02001206 memcpy(ie->data.dev_class, ev->dev_class, 3);
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
1209 if (!conn) {
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02001210 conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr);
1211 if (!conn) {
Gustavo F. Padovan893ef972010-07-18 15:13:37 -03001212 BT_ERR("No memory for new connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 hci_dev_unlock(hdev);
1214 return;
1215 }
1216 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 memcpy(conn->dev_class, ev->dev_class, 3);
1219 conn->state = BT_CONNECT;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 hci_dev_unlock(hdev);
1222
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001223 if (ev->link_type == ACL_LINK || !lmp_esco_capable(hdev)) {
1224 struct hci_cp_accept_conn_req cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001226 bacpy(&cp.bdaddr, &ev->bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001228 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
1229 cp.role = 0x00; /* Become master */
1230 else
1231 cp.role = 0x01; /* Remain slave */
1232
1233 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ,
1234 sizeof(cp), &cp);
1235 } else {
1236 struct hci_cp_accept_sync_conn_req cp;
1237
1238 bacpy(&cp.bdaddr, &ev->bdaddr);
Marcel Holtmanna8746412008-07-14 20:13:46 +02001239 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001240
1241 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
1242 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
1243 cp.max_latency = cpu_to_le16(0xffff);
1244 cp.content_format = cpu_to_le16(hdev->voice_setting);
1245 cp.retrans_effort = 0xff;
1246
1247 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ,
1248 sizeof(cp), &cp);
1249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 } else {
1251 /* Connection rejected */
1252 struct hci_cp_reject_conn_req cp;
1253
1254 bacpy(&cp.bdaddr, &ev->bdaddr);
1255 cp.reason = 0x0f;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001256 hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
1258}
1259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260static inline void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1261{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001262 struct hci_ev_disconn_complete *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02001263 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 BT_DBG("%s status %d", hdev->name, ev->status);
1266
Johan Hedberg8962ee72011-01-20 12:40:27 +02001267 if (ev->status) {
1268 mgmt_disconnect_failed(hdev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return;
Johan Hedberg8962ee72011-01-20 12:40:27 +02001270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 hci_dev_lock(hdev);
1273
Marcel Holtmann04837f62006-07-03 10:02:33 +02001274 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergf7520542011-01-20 12:34:39 +02001275 if (!conn)
1276 goto unlock;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02001277
Johan Hedbergf7520542011-01-20 12:34:39 +02001278 conn->state = BT_CLOSED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Johan Hedbergf7520542011-01-20 12:34:39 +02001280 if (conn->type == ACL_LINK)
1281 mgmt_disconnected(hdev->id, &conn->dst);
1282
1283 hci_proto_disconn_cfm(conn, ev->reason);
1284 hci_conn_del(conn);
1285
1286unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 hci_dev_unlock(hdev);
1288}
1289
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001290static inline void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1291{
1292 struct hci_ev_auth_complete *ev = (void *) skb->data;
1293 struct hci_conn *conn;
1294
1295 BT_DBG("%s status %d", hdev->name, ev->status);
1296
1297 hci_dev_lock(hdev);
1298
1299 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1300 if (conn) {
Johan Hedberg765c2a92011-01-19 12:06:52 +05301301 if (!ev->status) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001302 conn->link_mode |= HCI_LM_AUTH;
Johan Hedberg765c2a92011-01-19 12:06:52 +05301303 conn->sec_level = conn->pending_sec_level;
1304 } else
Johan Hedbergda213f42010-06-18 11:08:56 +03001305 conn->sec_level = BT_SECURITY_LOW;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001306
1307 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
1308
Marcel Holtmannf8558552008-07-14 20:13:49 +02001309 if (conn->state == BT_CONFIG) {
1310 if (!ev->status && hdev->ssp_mode > 0 &&
1311 conn->ssp_mode > 0) {
1312 struct hci_cp_set_conn_encrypt cp;
1313 cp.handle = ev->handle;
1314 cp.encrypt = 0x01;
1315 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT,
1316 sizeof(cp), &cp);
1317 } else {
1318 conn->state = BT_CONNECTED;
1319 hci_proto_connect_cfm(conn, ev->status);
1320 hci_conn_put(conn);
1321 }
Marcel Holtmann052b30b2009-04-26 20:01:22 +02001322 } else {
Marcel Holtmannf8558552008-07-14 20:13:49 +02001323 hci_auth_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001324
Marcel Holtmann052b30b2009-04-26 20:01:22 +02001325 hci_conn_hold(conn);
1326 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
1327 hci_conn_put(conn);
1328 }
1329
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001330 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
1331 if (!ev->status) {
1332 struct hci_cp_set_conn_encrypt cp;
Marcel Holtmannf8558552008-07-14 20:13:49 +02001333 cp.handle = ev->handle;
1334 cp.encrypt = 0x01;
1335 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT,
1336 sizeof(cp), &cp);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001337 } else {
1338 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
1339 hci_encrypt_cfm(conn, ev->status, 0x00);
1340 }
1341 }
1342 }
1343
1344 hci_dev_unlock(hdev);
1345}
1346
1347static inline void hci_remote_name_evt(struct hci_dev *hdev, struct sk_buff *skb)
1348{
Johan Hedberg127178d2010-11-18 22:22:29 +02001349 struct hci_ev_remote_name *ev = (void *) skb->data;
1350 struct hci_conn *conn;
1351
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001352 BT_DBG("%s", hdev->name);
1353
1354 hci_conn_check_pending(hdev);
Johan Hedberg127178d2010-11-18 22:22:29 +02001355
1356 hci_dev_lock(hdev);
1357
1358 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
1359 if (conn && hci_outgoing_auth_needed(hdev, conn)) {
1360 struct hci_cp_auth_requested cp;
1361 cp.handle = __cpu_to_le16(conn->handle);
1362 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
1363 }
1364
1365 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001366}
1367
1368static inline void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
1369{
1370 struct hci_ev_encrypt_change *ev = (void *) skb->data;
1371 struct hci_conn *conn;
1372
1373 BT_DBG("%s status %d", hdev->name, ev->status);
1374
1375 hci_dev_lock(hdev);
1376
1377 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1378 if (conn) {
1379 if (!ev->status) {
Marcel Holtmannae293192008-07-14 20:13:45 +02001380 if (ev->encrypt) {
1381 /* Encryption implies authentication */
1382 conn->link_mode |= HCI_LM_AUTH;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001383 conn->link_mode |= HCI_LM_ENCRYPT;
Marcel Holtmannae293192008-07-14 20:13:45 +02001384 } else
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001385 conn->link_mode &= ~HCI_LM_ENCRYPT;
1386 }
1387
1388 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
1389
Marcel Holtmannf8558552008-07-14 20:13:49 +02001390 if (conn->state == BT_CONFIG) {
1391 if (!ev->status)
1392 conn->state = BT_CONNECTED;
1393
1394 hci_proto_connect_cfm(conn, ev->status);
1395 hci_conn_put(conn);
1396 } else
1397 hci_encrypt_cfm(conn, ev->status, ev->encrypt);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001398 }
1399
1400 hci_dev_unlock(hdev);
1401}
1402
1403static inline void hci_change_link_key_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1404{
1405 struct hci_ev_change_link_key_complete *ev = (void *) skb->data;
1406 struct hci_conn *conn;
1407
1408 BT_DBG("%s status %d", hdev->name, ev->status);
1409
1410 hci_dev_lock(hdev);
1411
1412 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1413 if (conn) {
1414 if (!ev->status)
1415 conn->link_mode |= HCI_LM_SECURE;
1416
1417 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
1418
1419 hci_key_change_cfm(conn, ev->status);
1420 }
1421
1422 hci_dev_unlock(hdev);
1423}
1424
1425static inline void hci_remote_features_evt(struct hci_dev *hdev, struct sk_buff *skb)
1426{
1427 struct hci_ev_remote_features *ev = (void *) skb->data;
1428 struct hci_conn *conn;
1429
1430 BT_DBG("%s status %d", hdev->name, ev->status);
1431
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001432 hci_dev_lock(hdev);
1433
1434 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02001435 if (!conn)
1436 goto unlock;
Marcel Holtmann769be972008-07-14 20:13:49 +02001437
Johan Hedbergccd556f2010-11-10 17:11:51 +02001438 if (!ev->status)
1439 memcpy(conn->features, ev->features, 8);
1440
1441 if (conn->state != BT_CONFIG)
1442 goto unlock;
1443
1444 if (!ev->status && lmp_ssp_capable(hdev) && lmp_ssp_capable(conn)) {
1445 struct hci_cp_read_remote_ext_features cp;
1446 cp.handle = ev->handle;
1447 cp.page = 0x01;
1448 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES,
Marcel Holtmann769be972008-07-14 20:13:49 +02001449 sizeof(cp), &cp);
Johan Hedberg392599b2010-11-18 22:22:28 +02001450 goto unlock;
1451 }
1452
Johan Hedberg127178d2010-11-18 22:22:29 +02001453 if (!ev->status) {
1454 struct hci_cp_remote_name_req cp;
1455 memset(&cp, 0, sizeof(cp));
1456 bacpy(&cp.bdaddr, &conn->dst);
1457 cp.pscan_rep_mode = 0x02;
1458 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
1459 }
Johan Hedberg392599b2010-11-18 22:22:28 +02001460
Johan Hedberg127178d2010-11-18 22:22:29 +02001461 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02001462 conn->state = BT_CONNECTED;
1463 hci_proto_connect_cfm(conn, ev->status);
1464 hci_conn_put(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02001465 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001466
Johan Hedbergccd556f2010-11-10 17:11:51 +02001467unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001468 hci_dev_unlock(hdev);
1469}
1470
1471static inline void hci_remote_version_evt(struct hci_dev *hdev, struct sk_buff *skb)
1472{
1473 BT_DBG("%s", hdev->name);
1474}
1475
1476static inline void hci_qos_setup_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1477{
1478 BT_DBG("%s", hdev->name);
1479}
1480
1481static inline void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1482{
1483 struct hci_ev_cmd_complete *ev = (void *) skb->data;
1484 __u16 opcode;
1485
1486 skb_pull(skb, sizeof(*ev));
1487
1488 opcode = __le16_to_cpu(ev->opcode);
1489
1490 switch (opcode) {
1491 case HCI_OP_INQUIRY_CANCEL:
1492 hci_cc_inquiry_cancel(hdev, skb);
1493 break;
1494
1495 case HCI_OP_EXIT_PERIODIC_INQ:
1496 hci_cc_exit_periodic_inq(hdev, skb);
1497 break;
1498
1499 case HCI_OP_REMOTE_NAME_REQ_CANCEL:
1500 hci_cc_remote_name_req_cancel(hdev, skb);
1501 break;
1502
1503 case HCI_OP_ROLE_DISCOVERY:
1504 hci_cc_role_discovery(hdev, skb);
1505 break;
1506
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02001507 case HCI_OP_READ_LINK_POLICY:
1508 hci_cc_read_link_policy(hdev, skb);
1509 break;
1510
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001511 case HCI_OP_WRITE_LINK_POLICY:
1512 hci_cc_write_link_policy(hdev, skb);
1513 break;
1514
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02001515 case HCI_OP_READ_DEF_LINK_POLICY:
1516 hci_cc_read_def_link_policy(hdev, skb);
1517 break;
1518
1519 case HCI_OP_WRITE_DEF_LINK_POLICY:
1520 hci_cc_write_def_link_policy(hdev, skb);
1521 break;
1522
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001523 case HCI_OP_RESET:
1524 hci_cc_reset(hdev, skb);
1525 break;
1526
1527 case HCI_OP_WRITE_LOCAL_NAME:
1528 hci_cc_write_local_name(hdev, skb);
1529 break;
1530
1531 case HCI_OP_READ_LOCAL_NAME:
1532 hci_cc_read_local_name(hdev, skb);
1533 break;
1534
1535 case HCI_OP_WRITE_AUTH_ENABLE:
1536 hci_cc_write_auth_enable(hdev, skb);
1537 break;
1538
1539 case HCI_OP_WRITE_ENCRYPT_MODE:
1540 hci_cc_write_encrypt_mode(hdev, skb);
1541 break;
1542
1543 case HCI_OP_WRITE_SCAN_ENABLE:
1544 hci_cc_write_scan_enable(hdev, skb);
1545 break;
1546
1547 case HCI_OP_READ_CLASS_OF_DEV:
1548 hci_cc_read_class_of_dev(hdev, skb);
1549 break;
1550
1551 case HCI_OP_WRITE_CLASS_OF_DEV:
1552 hci_cc_write_class_of_dev(hdev, skb);
1553 break;
1554
1555 case HCI_OP_READ_VOICE_SETTING:
1556 hci_cc_read_voice_setting(hdev, skb);
1557 break;
1558
1559 case HCI_OP_WRITE_VOICE_SETTING:
1560 hci_cc_write_voice_setting(hdev, skb);
1561 break;
1562
1563 case HCI_OP_HOST_BUFFER_SIZE:
1564 hci_cc_host_buffer_size(hdev, skb);
1565 break;
1566
Marcel Holtmann333140b2008-07-14 20:13:48 +02001567 case HCI_OP_READ_SSP_MODE:
1568 hci_cc_read_ssp_mode(hdev, skb);
1569 break;
1570
1571 case HCI_OP_WRITE_SSP_MODE:
1572 hci_cc_write_ssp_mode(hdev, skb);
1573 break;
1574
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001575 case HCI_OP_READ_LOCAL_VERSION:
1576 hci_cc_read_local_version(hdev, skb);
1577 break;
1578
1579 case HCI_OP_READ_LOCAL_COMMANDS:
1580 hci_cc_read_local_commands(hdev, skb);
1581 break;
1582
1583 case HCI_OP_READ_LOCAL_FEATURES:
1584 hci_cc_read_local_features(hdev, skb);
1585 break;
1586
1587 case HCI_OP_READ_BUFFER_SIZE:
1588 hci_cc_read_buffer_size(hdev, skb);
1589 break;
1590
1591 case HCI_OP_READ_BD_ADDR:
1592 hci_cc_read_bd_addr(hdev, skb);
1593 break;
1594
Johan Hedberg23bb5762010-12-21 23:01:27 +02001595 case HCI_OP_WRITE_CA_TIMEOUT:
1596 hci_cc_write_ca_timeout(hdev, skb);
1597 break;
1598
Johan Hedbergb0916ea2011-01-10 13:44:55 +02001599 case HCI_OP_DELETE_STORED_LINK_KEY:
1600 hci_cc_delete_stored_link_key(hdev, skb);
1601 break;
1602
Johan Hedbergd5859e22011-01-25 01:19:58 +02001603 case HCI_OP_SET_EVENT_MASK:
1604 hci_cc_set_event_mask(hdev, skb);
1605 break;
1606
1607 case HCI_OP_WRITE_INQUIRY_MODE:
1608 hci_cc_write_inquiry_mode(hdev, skb);
1609 break;
1610
1611 case HCI_OP_READ_INQ_RSP_TX_POWER:
1612 hci_cc_read_inq_rsp_tx_power(hdev, skb);
1613 break;
1614
1615 case HCI_OP_SET_EVENT_FLT:
1616 hci_cc_set_event_flt(hdev, skb);
1617 break;
1618
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001619 default:
1620 BT_DBG("%s opcode 0x%x", hdev->name, opcode);
1621 break;
1622 }
1623
1624 if (ev->ncmd) {
1625 atomic_set(&hdev->cmd_cnt, 1);
1626 if (!skb_queue_empty(&hdev->cmd_q))
Marcel Holtmannc78ae282009-11-18 01:02:54 +01001627 tasklet_schedule(&hdev->cmd_task);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001628 }
1629}
1630
1631static inline void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb)
1632{
1633 struct hci_ev_cmd_status *ev = (void *) skb->data;
1634 __u16 opcode;
1635
1636 skb_pull(skb, sizeof(*ev));
1637
1638 opcode = __le16_to_cpu(ev->opcode);
1639
1640 switch (opcode) {
1641 case HCI_OP_INQUIRY:
1642 hci_cs_inquiry(hdev, ev->status);
1643 break;
1644
1645 case HCI_OP_CREATE_CONN:
1646 hci_cs_create_conn(hdev, ev->status);
1647 break;
1648
1649 case HCI_OP_ADD_SCO:
1650 hci_cs_add_sco(hdev, ev->status);
1651 break;
1652
Marcel Holtmannf8558552008-07-14 20:13:49 +02001653 case HCI_OP_AUTH_REQUESTED:
1654 hci_cs_auth_requested(hdev, ev->status);
1655 break;
1656
1657 case HCI_OP_SET_CONN_ENCRYPT:
1658 hci_cs_set_conn_encrypt(hdev, ev->status);
1659 break;
1660
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001661 case HCI_OP_REMOTE_NAME_REQ:
1662 hci_cs_remote_name_req(hdev, ev->status);
1663 break;
1664
Marcel Holtmann769be972008-07-14 20:13:49 +02001665 case HCI_OP_READ_REMOTE_FEATURES:
1666 hci_cs_read_remote_features(hdev, ev->status);
1667 break;
1668
1669 case HCI_OP_READ_REMOTE_EXT_FEATURES:
1670 hci_cs_read_remote_ext_features(hdev, ev->status);
1671 break;
1672
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001673 case HCI_OP_SETUP_SYNC_CONN:
1674 hci_cs_setup_sync_conn(hdev, ev->status);
1675 break;
1676
1677 case HCI_OP_SNIFF_MODE:
1678 hci_cs_sniff_mode(hdev, ev->status);
1679 break;
1680
1681 case HCI_OP_EXIT_SNIFF_MODE:
1682 hci_cs_exit_sniff_mode(hdev, ev->status);
1683 break;
1684
Johan Hedberg8962ee72011-01-20 12:40:27 +02001685 case HCI_OP_DISCONNECT:
1686 if (ev->status != 0)
1687 mgmt_disconnect_failed(hdev->id);
1688 break;
1689
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001690 default:
1691 BT_DBG("%s opcode 0x%x", hdev->name, opcode);
1692 break;
1693 }
1694
1695 if (ev->ncmd) {
1696 atomic_set(&hdev->cmd_cnt, 1);
1697 if (!skb_queue_empty(&hdev->cmd_q))
Marcel Holtmannc78ae282009-11-18 01:02:54 +01001698 tasklet_schedule(&hdev->cmd_task);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001699 }
1700}
1701
1702static inline void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
1703{
1704 struct hci_ev_role_change *ev = (void *) skb->data;
1705 struct hci_conn *conn;
1706
1707 BT_DBG("%s status %d", hdev->name, ev->status);
1708
1709 hci_dev_lock(hdev);
1710
1711 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
1712 if (conn) {
1713 if (!ev->status) {
1714 if (ev->role)
1715 conn->link_mode &= ~HCI_LM_MASTER;
1716 else
1717 conn->link_mode |= HCI_LM_MASTER;
1718 }
1719
1720 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->pend);
1721
1722 hci_role_switch_cfm(conn, ev->status, ev->role);
1723 }
1724
1725 hci_dev_unlock(hdev);
1726}
1727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728static inline void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
1729{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001730 struct hci_ev_num_comp_pkts *ev = (void *) skb->data;
Marcel Holtmann1ebb9252005-11-08 09:57:21 -08001731 __le16 *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 int i;
1733
1734 skb_pull(skb, sizeof(*ev));
1735
1736 BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
1737
1738 if (skb->len < ev->num_hndl * 4) {
1739 BT_DBG("%s bad parameters", hdev->name);
1740 return;
1741 }
1742
1743 tasklet_disable(&hdev->tx_task);
1744
Marcel Holtmann1ebb9252005-11-08 09:57:21 -08001745 for (i = 0, ptr = (__le16 *) skb->data; i < ev->num_hndl; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 struct hci_conn *conn;
1747 __u16 handle, count;
1748
Harvey Harrison83985312008-05-02 16:25:46 -07001749 handle = get_unaligned_le16(ptr++);
1750 count = get_unaligned_le16(ptr++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
1752 conn = hci_conn_hash_lookup_handle(hdev, handle);
1753 if (conn) {
1754 conn->sent -= count;
1755
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02001756 if (conn->type == ACL_LINK) {
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001757 hdev->acl_cnt += count;
1758 if (hdev->acl_cnt > hdev->acl_pkts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 hdev->acl_cnt = hdev->acl_pkts;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02001760 } else {
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001761 hdev->sco_cnt += count;
1762 if (hdev->sco_cnt > hdev->sco_pkts)
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02001763 hdev->sco_cnt = hdev->sco_pkts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
1765 }
1766 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001767
Marcel Holtmannc78ae282009-11-18 01:02:54 +01001768 tasklet_schedule(&hdev->tx_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 tasklet_enable(&hdev->tx_task);
1771}
1772
Marcel Holtmann04837f62006-07-03 10:02:33 +02001773static inline void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001775 struct hci_ev_mode_change *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02001776 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 BT_DBG("%s status %d", hdev->name, ev->status);
1779
1780 hci_dev_lock(hdev);
1781
Marcel Holtmann04837f62006-07-03 10:02:33 +02001782 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1783 if (conn) {
1784 conn->mode = ev->mode;
1785 conn->interval = __le16_to_cpu(ev->interval);
1786
1787 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
1788 if (conn->mode == HCI_CM_ACTIVE)
1789 conn->power_save = 1;
1790 else
1791 conn->power_save = 0;
1792 }
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001793
1794 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->pend))
1795 hci_sco_setup(conn, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02001796 }
1797
1798 hci_dev_unlock(hdev);
1799}
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801static inline void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
1802{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02001803 struct hci_ev_pin_code_req *ev = (void *) skb->data;
1804 struct hci_conn *conn;
1805
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001806 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02001807
1808 hci_dev_lock(hdev);
1809
1810 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Marcel Holtmann3d7a9d12009-05-09 12:09:21 -07001811 if (conn && conn->state == BT_CONNECTED) {
Marcel Holtmann052b30b2009-04-26 20:01:22 +02001812 hci_conn_hold(conn);
1813 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
1814 hci_conn_put(conn);
1815 }
1816
Johan Hedberg03b555e2011-01-04 15:40:05 +02001817 if (!test_bit(HCI_PAIRABLE, &hdev->flags))
1818 hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
1819 sizeof(ev->bdaddr), &ev->bdaddr);
1820
Marcel Holtmann052b30b2009-04-26 20:01:22 +02001821 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
1823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824static inline void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
1825{
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001826 struct hci_ev_link_key_req *ev = (void *) skb->data;
1827 struct hci_cp_link_key_reply cp;
1828 struct hci_conn *conn;
1829 struct link_key *key;
1830
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001831 BT_DBG("%s", hdev->name);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001832
1833 if (!test_bit(HCI_LINK_KEYS, &hdev->flags))
1834 return;
1835
1836 hci_dev_lock(hdev);
1837
1838 key = hci_find_link_key(hdev, &ev->bdaddr);
1839 if (!key) {
1840 BT_DBG("%s link key not found for %s", hdev->name,
1841 batostr(&ev->bdaddr));
1842 goto not_found;
1843 }
1844
1845 BT_DBG("%s found key type %u for %s", hdev->name, key->type,
1846 batostr(&ev->bdaddr));
1847
1848 if (!test_bit(HCI_DEBUG_KEYS, &hdev->flags) && key->type == 0x03) {
1849 BT_DBG("%s ignoring debug key", hdev->name);
1850 goto not_found;
1851 }
1852
1853 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
1854
1855 if (key->type == 0x04 && conn && conn->auth_type != 0xff &&
1856 (conn->auth_type & 0x01)) {
1857 BT_DBG("%s ignoring unauthenticated key", hdev->name);
1858 goto not_found;
1859 }
1860
1861 bacpy(&cp.bdaddr, &ev->bdaddr);
1862 memcpy(cp.link_key, key->val, 16);
1863
1864 hci_send_cmd(hdev, HCI_OP_LINK_KEY_REPLY, sizeof(cp), &cp);
1865
1866 hci_dev_unlock(hdev);
1867
1868 return;
1869
1870not_found:
1871 hci_send_cmd(hdev, HCI_OP_LINK_KEY_NEG_REPLY, 6, &ev->bdaddr);
1872 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873}
1874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875static inline void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
1876{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02001877 struct hci_ev_link_key_notify *ev = (void *) skb->data;
1878 struct hci_conn *conn;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001879 u8 pin_len = 0;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02001880
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001881 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02001882
1883 hci_dev_lock(hdev);
1884
1885 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
1886 if (conn) {
1887 hci_conn_hold(conn);
1888 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
1889 hci_conn_put(conn);
1890 }
1891
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02001892 if (test_bit(HCI_LINK_KEYS, &hdev->flags))
1893 hci_add_link_key(hdev, 1, &ev->bdaddr, ev->link_key,
1894 ev->key_type, pin_len);
1895
Marcel Holtmann052b30b2009-04-26 20:01:22 +02001896 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897}
1898
Marcel Holtmann04837f62006-07-03 10:02:33 +02001899static inline void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
1900{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001901 struct hci_ev_clock_offset *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02001902 struct hci_conn *conn;
1903
1904 BT_DBG("%s status %d", hdev->name, ev->status);
1905
1906 hci_dev_lock(hdev);
1907
1908 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (conn && !ev->status) {
1910 struct inquiry_entry *ie;
1911
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02001912 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
1913 if (ie) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 ie->data.clock_offset = ev->clock_offset;
1915 ie->timestamp = jiffies;
1916 }
1917 }
1918
1919 hci_dev_unlock(hdev);
1920}
1921
Marcel Holtmanna8746412008-07-14 20:13:46 +02001922static inline void hci_pkt_type_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
1923{
1924 struct hci_ev_pkt_type_change *ev = (void *) skb->data;
1925 struct hci_conn *conn;
1926
1927 BT_DBG("%s status %d", hdev->name, ev->status);
1928
1929 hci_dev_lock(hdev);
1930
1931 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1932 if (conn && !ev->status)
1933 conn->pkt_type = __le16_to_cpu(ev->pkt_type);
1934
1935 hci_dev_unlock(hdev);
1936}
1937
Marcel Holtmann85a1e932005-08-09 20:28:02 -07001938static inline void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
1939{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001940 struct hci_ev_pscan_rep_mode *ev = (void *) skb->data;
Marcel Holtmann85a1e932005-08-09 20:28:02 -07001941 struct inquiry_entry *ie;
1942
1943 BT_DBG("%s", hdev->name);
1944
1945 hci_dev_lock(hdev);
1946
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02001947 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
1948 if (ie) {
Marcel Holtmann85a1e932005-08-09 20:28:02 -07001949 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
1950 ie->timestamp = jiffies;
1951 }
1952
1953 hci_dev_unlock(hdev);
1954}
1955
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001956static inline void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, struct sk_buff *skb)
1957{
1958 struct inquiry_data data;
1959 int num_rsp = *((__u8 *) skb->data);
1960
1961 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
1962
1963 if (!num_rsp)
1964 return;
1965
1966 hci_dev_lock(hdev);
1967
1968 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
1969 struct inquiry_info_with_rssi_and_pscan_mode *info = (void *) (skb->data + 1);
1970
1971 for (; num_rsp; num_rsp--) {
1972 bacpy(&data.bdaddr, &info->bdaddr);
1973 data.pscan_rep_mode = info->pscan_rep_mode;
1974 data.pscan_period_mode = info->pscan_period_mode;
1975 data.pscan_mode = info->pscan_mode;
1976 memcpy(data.dev_class, info->dev_class, 3);
1977 data.clock_offset = info->clock_offset;
1978 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02001979 data.ssp_mode = 0x00;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001980 info++;
1981 hci_inquiry_cache_update(hdev, &data);
1982 }
1983 } else {
1984 struct inquiry_info_with_rssi *info = (void *) (skb->data + 1);
1985
1986 for (; num_rsp; num_rsp--) {
1987 bacpy(&data.bdaddr, &info->bdaddr);
1988 data.pscan_rep_mode = info->pscan_rep_mode;
1989 data.pscan_period_mode = info->pscan_period_mode;
1990 data.pscan_mode = 0x00;
1991 memcpy(data.dev_class, info->dev_class, 3);
1992 data.clock_offset = info->clock_offset;
1993 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02001994 data.ssp_mode = 0x00;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001995 info++;
1996 hci_inquiry_cache_update(hdev, &data);
1997 }
1998 }
1999
2000 hci_dev_unlock(hdev);
2001}
2002
2003static inline void hci_remote_ext_features_evt(struct hci_dev *hdev, struct sk_buff *skb)
2004{
Marcel Holtmann41a96212008-07-14 20:13:48 +02002005 struct hci_ev_remote_ext_features *ev = (void *) skb->data;
2006 struct hci_conn *conn;
2007
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002008 BT_DBG("%s", hdev->name);
Marcel Holtmann41a96212008-07-14 20:13:48 +02002009
Marcel Holtmann41a96212008-07-14 20:13:48 +02002010 hci_dev_lock(hdev);
2011
2012 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02002013 if (!conn)
2014 goto unlock;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002015
Johan Hedbergccd556f2010-11-10 17:11:51 +02002016 if (!ev->status && ev->page == 0x01) {
2017 struct inquiry_entry *ie;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002018
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02002019 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
2020 if (ie)
Johan Hedbergccd556f2010-11-10 17:11:51 +02002021 ie->data.ssp_mode = (ev->features[0] & 0x01);
Marcel Holtmann769be972008-07-14 20:13:49 +02002022
Johan Hedbergccd556f2010-11-10 17:11:51 +02002023 conn->ssp_mode = (ev->features[0] & 0x01);
Marcel Holtmann41a96212008-07-14 20:13:48 +02002024 }
2025
Johan Hedbergccd556f2010-11-10 17:11:51 +02002026 if (conn->state != BT_CONFIG)
2027 goto unlock;
2028
Johan Hedberg127178d2010-11-18 22:22:29 +02002029 if (!ev->status) {
2030 struct hci_cp_remote_name_req cp;
2031 memset(&cp, 0, sizeof(cp));
2032 bacpy(&cp.bdaddr, &conn->dst);
2033 cp.pscan_rep_mode = 0x02;
2034 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
2035 }
Johan Hedberg392599b2010-11-18 22:22:28 +02002036
Johan Hedberg127178d2010-11-18 22:22:29 +02002037 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02002038 conn->state = BT_CONNECTED;
2039 hci_proto_connect_cfm(conn, ev->status);
2040 hci_conn_put(conn);
2041 }
2042
2043unlock:
Marcel Holtmann41a96212008-07-14 20:13:48 +02002044 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002045}
2046
2047static inline void hci_sync_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
2048{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002049 struct hci_ev_sync_conn_complete *ev = (void *) skb->data;
2050 struct hci_conn *conn;
2051
2052 BT_DBG("%s status %d", hdev->name, ev->status);
2053
2054 hci_dev_lock(hdev);
2055
2056 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02002057 if (!conn) {
2058 if (ev->link_type == ESCO_LINK)
2059 goto unlock;
2060
2061 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
2062 if (!conn)
2063 goto unlock;
2064
2065 conn->type = SCO_LINK;
2066 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002067
Marcel Holtmann732547f2009-04-19 19:14:14 +02002068 switch (ev->status) {
2069 case 0x00:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002070 conn->handle = __le16_to_cpu(ev->handle);
2071 conn->state = BT_CONNECTED;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002072
Marcel Holtmann9eba32b2009-08-22 14:19:26 -07002073 hci_conn_hold_device(conn);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002074 hci_conn_add_sysfs(conn);
Marcel Holtmann732547f2009-04-19 19:14:14 +02002075 break;
2076
Stephen Coe705e5712010-02-16 11:29:44 -05002077 case 0x11: /* Unsupported Feature or Parameter Value */
Marcel Holtmann732547f2009-04-19 19:14:14 +02002078 case 0x1c: /* SCO interval rejected */
Nick Pelly1038a002010-02-03 11:42:26 -08002079 case 0x1a: /* Unsupported Remote Feature */
Marcel Holtmann732547f2009-04-19 19:14:14 +02002080 case 0x1f: /* Unspecified error */
2081 if (conn->out && conn->attempt < 2) {
2082 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
2083 (hdev->esco_type & EDR_ESCO_MASK);
2084 hci_setup_sync(conn, conn->link->handle);
2085 goto unlock;
2086 }
2087 /* fall through */
2088
2089 default:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002090 conn->state = BT_CLOSED;
Marcel Holtmann732547f2009-04-19 19:14:14 +02002091 break;
2092 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002093
2094 hci_proto_connect_cfm(conn, ev->status);
2095 if (ev->status)
2096 hci_conn_del(conn);
2097
2098unlock:
2099 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002100}
2101
2102static inline void hci_sync_conn_changed_evt(struct hci_dev *hdev, struct sk_buff *skb)
2103{
2104 BT_DBG("%s", hdev->name);
2105}
2106
Marcel Holtmann04837f62006-07-03 10:02:33 +02002107static inline void hci_sniff_subrate_evt(struct hci_dev *hdev, struct sk_buff *skb)
2108{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002109 struct hci_ev_sniff_subrate *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02002110 struct hci_conn *conn;
2111
2112 BT_DBG("%s status %d", hdev->name, ev->status);
2113
2114 hci_dev_lock(hdev);
2115
2116 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
2117 if (conn) {
2118 }
2119
2120 hci_dev_unlock(hdev);
2121}
2122
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002123static inline void hci_extended_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
2124{
2125 struct inquiry_data data;
2126 struct extended_inquiry_info *info = (void *) (skb->data + 1);
2127 int num_rsp = *((__u8 *) skb->data);
2128
2129 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
2130
2131 if (!num_rsp)
2132 return;
2133
2134 hci_dev_lock(hdev);
2135
2136 for (; num_rsp; num_rsp--) {
2137 bacpy(&data.bdaddr, &info->bdaddr);
2138 data.pscan_rep_mode = info->pscan_rep_mode;
2139 data.pscan_period_mode = info->pscan_period_mode;
2140 data.pscan_mode = 0x00;
2141 memcpy(data.dev_class, info->dev_class, 3);
2142 data.clock_offset = info->clock_offset;
2143 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002144 data.ssp_mode = 0x01;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002145 info++;
2146 hci_inquiry_cache_update(hdev, &data);
2147 }
2148
2149 hci_dev_unlock(hdev);
2150}
2151
Marcel Holtmann04936842008-07-14 20:13:48 +02002152static inline void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
2153{
2154 struct hci_ev_io_capa_request *ev = (void *) skb->data;
2155 struct hci_conn *conn;
2156
2157 BT_DBG("%s", hdev->name);
2158
2159 hci_dev_lock(hdev);
2160
2161 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg03b555e2011-01-04 15:40:05 +02002162 if (!conn)
2163 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02002164
Johan Hedberg03b555e2011-01-04 15:40:05 +02002165 hci_conn_hold(conn);
2166
2167 if (!test_bit(HCI_MGMT, &hdev->flags))
2168 goto unlock;
2169
2170 if (test_bit(HCI_PAIRABLE, &hdev->flags) ||
2171 (conn->remote_auth & ~0x01) == HCI_AT_NO_BONDING) {
2172 /* FIXME: Do IO capa response based on information
2173 * provided through the management interface */
2174 } else {
2175 struct hci_cp_io_capability_neg_reply cp;
2176
2177 bacpy(&cp.bdaddr, &ev->bdaddr);
2178 cp.reason = 0x16; /* Pairing not allowed */
2179
2180 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_NEG_REPLY,
2181 sizeof(cp), &cp);
2182 }
2183
2184unlock:
2185 hci_dev_unlock(hdev);
2186}
2187
2188static inline void hci_io_capa_reply_evt(struct hci_dev *hdev, struct sk_buff *skb)
2189{
2190 struct hci_ev_io_capa_reply *ev = (void *) skb->data;
2191 struct hci_conn *conn;
2192
2193 BT_DBG("%s", hdev->name);
2194
2195 hci_dev_lock(hdev);
2196
2197 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
2198 if (!conn)
2199 goto unlock;
2200
2201 hci_conn_hold(conn);
2202
2203 conn->remote_cap = ev->capability;
2204 conn->remote_oob = ev->oob_data;
2205 conn->remote_auth = ev->authentication;
2206
2207unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02002208 hci_dev_unlock(hdev);
2209}
2210
2211static inline void hci_simple_pair_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
2212{
2213 struct hci_ev_simple_pair_complete *ev = (void *) skb->data;
2214 struct hci_conn *conn;
2215
2216 BT_DBG("%s", hdev->name);
2217
2218 hci_dev_lock(hdev);
2219
2220 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
2221 if (conn)
2222 hci_conn_put(conn);
2223
2224 hci_dev_unlock(hdev);
2225}
2226
Marcel Holtmann41a96212008-07-14 20:13:48 +02002227static inline void hci_remote_host_features_evt(struct hci_dev *hdev, struct sk_buff *skb)
2228{
2229 struct hci_ev_remote_host_features *ev = (void *) skb->data;
2230 struct inquiry_entry *ie;
2231
2232 BT_DBG("%s", hdev->name);
2233
2234 hci_dev_lock(hdev);
2235
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02002236 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
2237 if (ie)
Marcel Holtmann41a96212008-07-14 20:13:48 +02002238 ie->data.ssp_mode = (ev->features[0] & 0x01);
2239
2240 hci_dev_unlock(hdev);
2241}
2242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
2244{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002245 struct hci_event_hdr *hdr = (void *) skb->data;
2246 __u8 event = hdr->evt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
2248 skb_pull(skb, HCI_EVENT_HDR_SIZE);
2249
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002250 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 case HCI_EV_INQUIRY_COMPLETE:
2252 hci_inquiry_complete_evt(hdev, skb);
2253 break;
2254
2255 case HCI_EV_INQUIRY_RESULT:
2256 hci_inquiry_result_evt(hdev, skb);
2257 break;
2258
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002259 case HCI_EV_CONN_COMPLETE:
2260 hci_conn_complete_evt(hdev, skb);
Marcel Holtmann21d9e302005-09-13 01:32:25 +02002261 break;
2262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 case HCI_EV_CONN_REQUEST:
2264 hci_conn_request_evt(hdev, skb);
2265 break;
2266
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 case HCI_EV_DISCONN_COMPLETE:
2268 hci_disconn_complete_evt(hdev, skb);
2269 break;
2270
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 case HCI_EV_AUTH_COMPLETE:
2272 hci_auth_complete_evt(hdev, skb);
2273 break;
2274
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002275 case HCI_EV_REMOTE_NAME:
2276 hci_remote_name_evt(hdev, skb);
2277 break;
2278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 case HCI_EV_ENCRYPT_CHANGE:
2280 hci_encrypt_change_evt(hdev, skb);
2281 break;
2282
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002283 case HCI_EV_CHANGE_LINK_KEY_COMPLETE:
2284 hci_change_link_key_complete_evt(hdev, skb);
2285 break;
2286
2287 case HCI_EV_REMOTE_FEATURES:
2288 hci_remote_features_evt(hdev, skb);
2289 break;
2290
2291 case HCI_EV_REMOTE_VERSION:
2292 hci_remote_version_evt(hdev, skb);
2293 break;
2294
2295 case HCI_EV_QOS_SETUP_COMPLETE:
2296 hci_qos_setup_complete_evt(hdev, skb);
2297 break;
2298
2299 case HCI_EV_CMD_COMPLETE:
2300 hci_cmd_complete_evt(hdev, skb);
2301 break;
2302
2303 case HCI_EV_CMD_STATUS:
2304 hci_cmd_status_evt(hdev, skb);
2305 break;
2306
2307 case HCI_EV_ROLE_CHANGE:
2308 hci_role_change_evt(hdev, skb);
2309 break;
2310
2311 case HCI_EV_NUM_COMP_PKTS:
2312 hci_num_comp_pkts_evt(hdev, skb);
2313 break;
2314
2315 case HCI_EV_MODE_CHANGE:
2316 hci_mode_change_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 break;
2318
2319 case HCI_EV_PIN_CODE_REQ:
2320 hci_pin_code_request_evt(hdev, skb);
2321 break;
2322
2323 case HCI_EV_LINK_KEY_REQ:
2324 hci_link_key_request_evt(hdev, skb);
2325 break;
2326
2327 case HCI_EV_LINK_KEY_NOTIFY:
2328 hci_link_key_notify_evt(hdev, skb);
2329 break;
2330
2331 case HCI_EV_CLOCK_OFFSET:
2332 hci_clock_offset_evt(hdev, skb);
2333 break;
2334
Marcel Holtmanna8746412008-07-14 20:13:46 +02002335 case HCI_EV_PKT_TYPE_CHANGE:
2336 hci_pkt_type_change_evt(hdev, skb);
2337 break;
2338
Marcel Holtmann85a1e932005-08-09 20:28:02 -07002339 case HCI_EV_PSCAN_REP_MODE:
2340 hci_pscan_rep_mode_evt(hdev, skb);
2341 break;
2342
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002343 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
2344 hci_inquiry_result_with_rssi_evt(hdev, skb);
2345 break;
2346
2347 case HCI_EV_REMOTE_EXT_FEATURES:
2348 hci_remote_ext_features_evt(hdev, skb);
2349 break;
2350
2351 case HCI_EV_SYNC_CONN_COMPLETE:
2352 hci_sync_conn_complete_evt(hdev, skb);
2353 break;
2354
2355 case HCI_EV_SYNC_CONN_CHANGED:
2356 hci_sync_conn_changed_evt(hdev, skb);
2357 break;
2358
Marcel Holtmann04837f62006-07-03 10:02:33 +02002359 case HCI_EV_SNIFF_SUBRATE:
2360 hci_sniff_subrate_evt(hdev, skb);
2361 break;
2362
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002363 case HCI_EV_EXTENDED_INQUIRY_RESULT:
2364 hci_extended_inquiry_result_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 break;
2366
Marcel Holtmann04936842008-07-14 20:13:48 +02002367 case HCI_EV_IO_CAPA_REQUEST:
2368 hci_io_capa_request_evt(hdev, skb);
2369 break;
2370
Johan Hedberg03b555e2011-01-04 15:40:05 +02002371 case HCI_EV_IO_CAPA_REPLY:
2372 hci_io_capa_reply_evt(hdev, skb);
2373 break;
2374
Marcel Holtmann04936842008-07-14 20:13:48 +02002375 case HCI_EV_SIMPLE_PAIR_COMPLETE:
2376 hci_simple_pair_complete_evt(hdev, skb);
2377 break;
2378
Marcel Holtmann41a96212008-07-14 20:13:48 +02002379 case HCI_EV_REMOTE_HOST_FEATURES:
2380 hci_remote_host_features_evt(hdev, skb);
2381 break;
2382
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002383 default:
2384 BT_DBG("%s event 0x%x", hdev->name, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 break;
2386 }
2387
2388 kfree_skb(skb);
2389 hdev->stat.evt_rx++;
2390}
2391
2392/* Generate internal stack event */
2393void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
2394{
2395 struct hci_event_hdr *hdr;
2396 struct hci_ev_stack_internal *ev;
2397 struct sk_buff *skb;
2398
2399 skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC);
2400 if (!skb)
2401 return;
2402
2403 hdr = (void *) skb_put(skb, HCI_EVENT_HDR_SIZE);
2404 hdr->evt = HCI_EV_STACK_INTERNAL;
2405 hdr->plen = sizeof(*ev) + dlen;
2406
2407 ev = (void *) skb_put(skb, sizeof(*ev) + dlen);
2408 ev->type = type;
2409 memcpy(ev->data, data, dlen);
2410
Marcel Holtmann576c7d82005-08-06 12:36:54 +02002411 bt_cb(skb)->incoming = 1;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07002412 __net_timestamp(skb);
Marcel Holtmann576c7d82005-08-06 12:36:54 +02002413
Marcel Holtmann0d48d932005-08-09 20:30:28 -07002414 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 skb->dev = (void *) hdev;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02002416 hci_send_to_sock(hdev, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 kfree_skb(skb);
2418}