blob: e2cfeea5ee7216f9f7e251ed385acab19cda3b7f [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
3 Copyright (C) 2000-2001 Qualcomm Incorporated
4
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation;
10
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
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>
42#include <asm/uaccess.h>
43#include <asm/unaligned.h>
44
45#include <net/bluetooth/bluetooth.h>
46#include <net/bluetooth/hci_core.h>
47
48#ifndef CONFIG_BT_HCI_CORE_DEBUG
49#undef BT_DBG
50#define BT_DBG(D...)
51#endif
52
53/* Handle HCI Event packets */
54
Marcel Holtmanna9de9242007-10-20 13:33:56 +020055static void hci_cc_inquiry_cancel(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Marcel Holtmanna9de9242007-10-20 13:33:56 +020057 __u8 status = *((__u8 *) skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Marcel Holtmanna9de9242007-10-20 13:33:56 +020059 BT_DBG("%s status 0x%x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Marcel Holtmanna9de9242007-10-20 13:33:56 +020061 if (status)
62 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Marcel Holtmanna9de9242007-10-20 13:33:56 +020064 clear_bit(HCI_INQUIRY, &hdev->flags);
Marcel Holtmann6bd57412006-11-18 22:14:22 +010065
Marcel Holtmanna9de9242007-10-20 13:33:56 +020066 hci_req_complete(hdev, status);
Marcel Holtmann6bd57412006-11-18 22:14:22 +010067
Marcel Holtmanna9de9242007-10-20 13:33:56 +020068 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
Marcel Holtmanna9de9242007-10-20 13:33:56 +020071static void hci_cc_exit_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Marcel Holtmanna9de9242007-10-20 13:33:56 +020073 __u8 status = *((__u8 *) skb->data);
74
75 BT_DBG("%s status 0x%x", hdev->name, status);
76
77 if (status)
78 return;
79
80 clear_bit(HCI_INQUIRY, &hdev->flags);
81
82 hci_conn_check_pending(hdev);
83}
84
85static void hci_cc_remote_name_req_cancel(struct hci_dev *hdev, struct sk_buff *skb)
86{
87 BT_DBG("%s", hdev->name);
88}
89
90static void hci_cc_role_discovery(struct hci_dev *hdev, struct sk_buff *skb)
91{
92 struct hci_rp_role_discovery *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Marcel Holtmanna9de9242007-10-20 13:33:56 +020095 BT_DBG("%s status 0x%x", hdev->name, rp->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Marcel Holtmanna9de9242007-10-20 13:33:56 +020097 if (rp->status)
98 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200100 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200102 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
103 if (conn) {
104 if (rp->role)
105 conn->link_mode &= ~HCI_LM_MASTER;
106 else
107 conn->link_mode |= HCI_LM_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200109
110 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200113static void hci_cc_write_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200115 struct hci_rp_write_link_policy *rp = (void *) skb->data;
116 struct hci_conn *conn;
117 void *sent;
118
119 BT_DBG("%s status 0x%x", hdev->name, rp->status);
120
121 if (rp->status)
122 return;
123
124 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LINK_POLICY);
125 if (!sent)
126 return;
127
128 hci_dev_lock(hdev);
129
130 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
131 if (conn) {
132 __le16 policy = get_unaligned((__le16 *) (sent + 2));
133 conn->link_policy = __le16_to_cpu(policy);
134 }
135
136 hci_dev_unlock(hdev);
137}
138
139static void hci_cc_reset(struct hci_dev *hdev, struct sk_buff *skb)
140{
141 __u8 status = *((__u8 *) skb->data);
142
143 BT_DBG("%s status 0x%x", hdev->name, status);
144
145 hci_req_complete(hdev, status);
146}
147
148static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb)
149{
150 __u8 status = *((__u8 *) skb->data);
151 void *sent;
152
153 BT_DBG("%s status 0x%x", hdev->name, status);
154
155 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LOCAL_NAME);
156 if (!sent)
157 return;
158
159 if (!status)
160 memcpy(hdev->dev_name, sent, 248);
161}
162
163static void hci_cc_read_local_name(struct hci_dev *hdev, struct sk_buff *skb)
164{
165 struct hci_rp_read_local_name *rp = (void *) skb->data;
166
167 BT_DBG("%s status 0x%x", hdev->name, rp->status);
168
169 if (rp->status)
170 return;
171
172 memcpy(hdev->dev_name, rp->name, 248);
173}
174
175static void hci_cc_write_auth_enable(struct hci_dev *hdev, struct sk_buff *skb)
176{
177 __u8 status = *((__u8 *) skb->data);
178 void *sent;
179
180 BT_DBG("%s status 0x%x", hdev->name, status);
181
182 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_ENABLE);
183 if (!sent)
184 return;
185
186 if (!status) {
187 __u8 param = *((__u8 *) sent);
188
189 if (param == AUTH_ENABLED)
190 set_bit(HCI_AUTH, &hdev->flags);
191 else
192 clear_bit(HCI_AUTH, &hdev->flags);
193 }
194
195 hci_req_complete(hdev, status);
196}
197
198static void hci_cc_write_encrypt_mode(struct hci_dev *hdev, struct sk_buff *skb)
199{
200 __u8 status = *((__u8 *) skb->data);
201 void *sent;
202
203 BT_DBG("%s status 0x%x", hdev->name, status);
204
205 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_ENCRYPT_MODE);
206 if (!sent)
207 return;
208
209 if (!status) {
210 __u8 param = *((__u8 *) sent);
211
212 if (param)
213 set_bit(HCI_ENCRYPT, &hdev->flags);
214 else
215 clear_bit(HCI_ENCRYPT, &hdev->flags);
216 }
217
218 hci_req_complete(hdev, status);
219}
220
221static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
222{
223 __u8 status = *((__u8 *) skb->data);
224 void *sent;
225
226 BT_DBG("%s status 0x%x", hdev->name, status);
227
228 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SCAN_ENABLE);
229 if (!sent)
230 return;
231
232 if (!status) {
233 __u8 param = *((__u8 *) sent);
234
235 clear_bit(HCI_PSCAN, &hdev->flags);
236 clear_bit(HCI_ISCAN, &hdev->flags);
237
238 if (param & SCAN_INQUIRY)
239 set_bit(HCI_ISCAN, &hdev->flags);
240
241 if (param & SCAN_PAGE)
242 set_bit(HCI_PSCAN, &hdev->flags);
243 }
244
245 hci_req_complete(hdev, status);
246}
247
248static void hci_cc_read_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
249{
250 struct hci_rp_read_class_of_dev *rp = (void *) skb->data;
251
252 BT_DBG("%s status 0x%x", hdev->name, rp->status);
253
254 if (rp->status)
255 return;
256
257 memcpy(hdev->dev_class, rp->dev_class, 3);
258
259 BT_DBG("%s class 0x%.2x%.2x%.2x", hdev->name,
260 hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
261}
262
263static void hci_cc_write_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
264{
265 __u8 status = *((__u8 *) skb->data);
266 void *sent;
267
268 BT_DBG("%s status 0x%x", hdev->name, status);
269
270 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_CLASS_OF_DEV);
271 if (!sent)
272 return;
273
274 if (!status)
275 memcpy(hdev->dev_class, sent, 3);
276}
277
278static void hci_cc_read_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
279{
280 struct hci_rp_read_voice_setting *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 __u16 setting;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200282
283 BT_DBG("%s status 0x%x", hdev->name, rp->status);
284
285 if (rp->status)
286 return;
287
288 setting = __le16_to_cpu(rp->voice_setting);
289
290 if (hdev->voice_setting == setting )
291 return;
292
293 hdev->voice_setting = setting;
294
295 BT_DBG("%s voice setting 0x%04x", hdev->name, setting);
296
297 if (hdev->notify) {
298 tasklet_disable(&hdev->tx_task);
299 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
300 tasklet_enable(&hdev->tx_task);
301 }
302}
303
304static void hci_cc_write_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
305{
306 __u8 status = *((__u8 *) skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 void *sent;
308
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200309 BT_DBG("%s status 0x%x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200311 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_VOICE_SETTING);
312 if (!sent)
313 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200315 if (!status) {
316 __u16 setting = __le16_to_cpu(get_unaligned((__le16 *) sent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200318 if (hdev->voice_setting != setting) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 hdev->voice_setting = setting;
320
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200321 BT_DBG("%s voice setting 0x%04x", hdev->name, setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 if (hdev->notify) {
324 tasklet_disable(&hdev->tx_task);
325 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
326 tasklet_enable(&hdev->tx_task);
327 }
328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330}
331
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200332static void hci_cc_host_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200334 __u8 status = *((__u8 *) skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200336 BT_DBG("%s status 0x%x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200338 hci_req_complete(hdev, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200341static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
342{
343 struct hci_rp_read_local_version *rp = (void *) skb->data;
344
345 BT_DBG("%s status 0x%x", hdev->name, rp->status);
346
347 if (rp->status)
348 return;
349
350 hdev->hci_ver = rp->hci_ver;
351 hdev->hci_rev = btohs(rp->hci_rev);
352 hdev->manufacturer = btohs(rp->manufacturer);
353
354 BT_DBG("%s manufacturer %d hci ver %d:%d", hdev->name,
355 hdev->manufacturer,
356 hdev->hci_ver, hdev->hci_rev);
357}
358
359static void hci_cc_read_local_commands(struct hci_dev *hdev, struct sk_buff *skb)
360{
361 struct hci_rp_read_local_commands *rp = (void *) skb->data;
362
363 BT_DBG("%s status 0x%x", hdev->name, rp->status);
364
365 if (rp->status)
366 return;
367
368 memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
369}
370
371static void hci_cc_read_local_features(struct hci_dev *hdev, struct sk_buff *skb)
372{
373 struct hci_rp_read_local_features *rp = (void *) skb->data;
374
375 BT_DBG("%s status 0x%x", hdev->name, rp->status);
376
377 if (rp->status)
378 return;
379
380 memcpy(hdev->features, rp->features, 8);
381
382 /* Adjust default settings according to features
383 * supported by device. */
384
385 if (hdev->features[0] & LMP_3SLOT)
386 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
387
388 if (hdev->features[0] & LMP_5SLOT)
389 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
390
391 if (hdev->features[1] & LMP_HV2) {
392 hdev->pkt_type |= (HCI_HV2);
393 hdev->esco_type |= (ESCO_HV2);
394 }
395
396 if (hdev->features[1] & LMP_HV3) {
397 hdev->pkt_type |= (HCI_HV3);
398 hdev->esco_type |= (ESCO_HV3);
399 }
400
401 if (hdev->features[3] & LMP_ESCO)
402 hdev->esco_type |= (ESCO_EV3);
403
404 if (hdev->features[4] & LMP_EV4)
405 hdev->esco_type |= (ESCO_EV4);
406
407 if (hdev->features[4] & LMP_EV5)
408 hdev->esco_type |= (ESCO_EV5);
409
410 BT_DBG("%s features 0x%.2x%.2x%.2x%.2x%.2x%.2x%.2x%.2x", hdev->name,
411 hdev->features[0], hdev->features[1],
412 hdev->features[2], hdev->features[3],
413 hdev->features[4], hdev->features[5],
414 hdev->features[6], hdev->features[7]);
415}
416
417static void hci_cc_read_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
418{
419 struct hci_rp_read_buffer_size *rp = (void *) skb->data;
420
421 BT_DBG("%s status 0x%x", hdev->name, rp->status);
422
423 if (rp->status)
424 return;
425
426 hdev->acl_mtu = __le16_to_cpu(rp->acl_mtu);
427 hdev->sco_mtu = rp->sco_mtu;
428 hdev->acl_pkts = __le16_to_cpu(rp->acl_max_pkt);
429 hdev->sco_pkts = __le16_to_cpu(rp->sco_max_pkt);
430
431 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
432 hdev->sco_mtu = 64;
433 hdev->sco_pkts = 8;
434 }
435
436 hdev->acl_cnt = hdev->acl_pkts;
437 hdev->sco_cnt = hdev->sco_pkts;
438
439 BT_DBG("%s acl mtu %d:%d sco mtu %d:%d", hdev->name,
440 hdev->acl_mtu, hdev->acl_pkts,
441 hdev->sco_mtu, hdev->sco_pkts);
442}
443
444static void hci_cc_read_bd_addr(struct hci_dev *hdev, struct sk_buff *skb)
445{
446 struct hci_rp_read_bd_addr *rp = (void *) skb->data;
447
448 BT_DBG("%s status 0x%x", hdev->name, rp->status);
449
450 if (!rp->status)
451 bacpy(&hdev->bdaddr, &rp->bdaddr);
452
453 hci_req_complete(hdev, rp->status);
454}
455
456static inline void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
457{
458 BT_DBG("%s status 0x%x", hdev->name, status);
459
460 if (status) {
461 hci_req_complete(hdev, status);
462
463 hci_conn_check_pending(hdev);
464 } else
465 set_bit(HCI_INQUIRY, &hdev->flags);
466}
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468static inline void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
469{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200470 struct hci_cp_create_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200473 BT_DBG("%s status 0x%x", hdev->name, status);
474
475 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_CONN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (!cp)
477 return;
478
479 hci_dev_lock(hdev);
480
481 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
482
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200483 BT_DBG("%s bdaddr %s conn %p", hdev->name, batostr(&cp->bdaddr), conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 if (status) {
486 if (conn && conn->state == BT_CONNECT) {
Marcel Holtmann4c67bc72006-10-15 17:30:56 +0200487 if (status != 0x0c || conn->attempt > 2) {
488 conn->state = BT_CLOSED;
489 hci_proto_connect_cfm(conn, status);
490 hci_conn_del(conn);
491 } else
492 conn->state = BT_CONNECT2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494 } else {
495 if (!conn) {
496 conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr);
497 if (conn) {
498 conn->out = 1;
499 conn->link_mode |= HCI_LM_MASTER;
500 } else
501 BT_ERR("No memmory for new connection");
502 }
503 }
504
505 hci_dev_unlock(hdev);
506}
507
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200508static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200510 struct hci_cp_add_sco *cp;
511 struct hci_conn *acl, *sco;
512 __u16 handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200514 if (!status)
515 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200517 BT_DBG("%s status 0x%x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200519 cp = hci_sent_cmd_data(hdev, HCI_OP_ADD_SCO);
520 if (!cp)
521 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200523 handle = __le16_to_cpu(cp->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200525 BT_DBG("%s handle %d", hdev->name, handle);
Marcel Holtmann6bd57412006-11-18 22:14:22 +0100526
527 hci_dev_lock(hdev);
528
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200529 acl = hci_conn_hash_lookup_handle(hdev, handle);
530 if (acl && (sco = acl->link)) {
531 sco->state = BT_CLOSED;
532
533 hci_proto_connect_cfm(sco, status);
534 hci_conn_del(sco);
535 }
Marcel Holtmann6bd57412006-11-18 22:14:22 +0100536
537 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200540static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
541{
542 BT_DBG("%s status 0x%x", hdev->name, status);
543}
544
545static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
546{
547 BT_DBG("%s status 0x%x", hdev->name, status);
548}
549
550static void hci_cs_sniff_mode(struct hci_dev *hdev, __u8 status)
551{
552 struct hci_cp_sniff_mode *cp;
553 struct hci_conn *conn;
554
555 BT_DBG("%s status 0x%x", hdev->name, status);
556
557 if (!status)
558 return;
559
560 cp = hci_sent_cmd_data(hdev, HCI_OP_SNIFF_MODE);
561 if (!cp)
562 return;
563
564 hci_dev_lock(hdev);
565
566 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
567 if (conn)
568 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend);
569
570 hci_dev_unlock(hdev);
571}
572
573static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
574{
575 struct hci_cp_exit_sniff_mode *cp;
576 struct hci_conn *conn;
577
578 BT_DBG("%s status 0x%x", hdev->name, status);
579
580 if (!status)
581 return;
582
583 cp = hci_sent_cmd_data(hdev, HCI_OP_EXIT_SNIFF_MODE);
584 if (!cp)
585 return;
586
587 hci_dev_lock(hdev);
588
589 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
590 if (conn)
591 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend);
592
593 hci_dev_unlock(hdev);
594}
595
596static inline void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
597{
598 __u8 status = *((__u8 *) skb->data);
599
600 BT_DBG("%s status %d", hdev->name, status);
601
602 clear_bit(HCI_INQUIRY, &hdev->flags);
603
604 hci_req_complete(hdev, status);
605
606 hci_conn_check_pending(hdev);
607}
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609static inline void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
610{
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -0700611 struct inquiry_data data;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200612 struct inquiry_info *info = (void *) (skb->data + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 int num_rsp = *((__u8 *) skb->data);
614
615 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
616
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -0700617 if (!num_rsp)
618 return;
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -0700621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 for (; num_rsp; num_rsp--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 bacpy(&data.bdaddr, &info->bdaddr);
624 data.pscan_rep_mode = info->pscan_rep_mode;
625 data.pscan_period_mode = info->pscan_period_mode;
626 data.pscan_mode = info->pscan_mode;
627 memcpy(data.dev_class, info->dev_class, 3);
628 data.clock_offset = info->clock_offset;
629 data.rssi = 0x00;
630 info++;
631 hci_inquiry_cache_update(hdev, &data);
632 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -0700633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 hci_dev_unlock(hdev);
635}
636
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200637static inline void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200639 struct hci_ev_conn_complete *ev = (void *) skb->data;
640 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200642 BT_DBG("%s", hdev->name);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -0700643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -0700645
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200646 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
647 if (!conn)
648 goto unlock;
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -0700649
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200650 if (!ev->status) {
651 conn->handle = __le16_to_cpu(ev->handle);
652 conn->state = BT_CONNECTED;
653
654 if (test_bit(HCI_AUTH, &hdev->flags))
655 conn->link_mode |= HCI_LM_AUTH;
656
657 if (test_bit(HCI_ENCRYPT, &hdev->flags))
658 conn->link_mode |= HCI_LM_ENCRYPT;
659
660 /* Get remote features */
661 if (conn->type == ACL_LINK) {
662 struct hci_cp_read_remote_features cp;
663 cp.handle = ev->handle;
664 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES, sizeof(cp), &cp);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -0700665 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -0700666
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200667 /* Set link policy */
668 if (conn->type == ACL_LINK && hdev->link_policy) {
669 struct hci_cp_write_link_policy cp;
670 cp.handle = ev->handle;
671 cp.policy = cpu_to_le16(hdev->link_policy);
672 hci_send_cmd(hdev, HCI_OP_WRITE_LINK_POLICY, sizeof(cp), &cp);
673 }
674
675 /* Set packet type for incoming connection */
676 if (!conn->out) {
677 struct hci_cp_change_conn_ptype cp;
678 cp.handle = ev->handle;
679 cp.pkt_type = (conn->type == ACL_LINK) ?
680 cpu_to_le16(hdev->pkt_type & ACL_PTYPE_MASK):
681 cpu_to_le16(hdev->pkt_type & SCO_PTYPE_MASK);
682
683 hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE, sizeof(cp), &cp);
684 } else {
685 /* Update disconnect timer */
686 hci_conn_hold(conn);
687 hci_conn_put(conn);
688 }
689 } else
690 conn->state = BT_CLOSED;
691
692 if (conn->type == ACL_LINK) {
693 struct hci_conn *sco = conn->link;
694 if (sco) {
695 if (!ev->status)
696 hci_add_sco(sco, conn->handle);
697 else {
698 hci_proto_connect_cfm(sco, ev->status);
699 hci_conn_del(sco);
700 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -0700701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -0700703
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200704 hci_proto_connect_cfm(conn, ev->status);
705 if (ev->status)
706 hci_conn_del(conn);
707
708unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200710
711 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714static inline void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
715{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200716 struct hci_ev_conn_request *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 int mask = hdev->link_mode;
718
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200719 BT_DBG("%s bdaddr %s type 0x%x", hdev->name,
720 batostr(&ev->bdaddr), ev->link_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type);
723
724 if (mask & HCI_LM_ACCEPT) {
725 /* Connection accepted */
726 struct hci_conn *conn;
727 struct hci_cp_accept_conn_req cp;
728
729 hci_dev_lock(hdev);
730 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
731 if (!conn) {
732 if (!(conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr))) {
733 BT_ERR("No memmory for new connection");
734 hci_dev_unlock(hdev);
735 return;
736 }
737 }
738 memcpy(conn->dev_class, ev->dev_class, 3);
739 conn->state = BT_CONNECT;
740 hci_dev_unlock(hdev);
741
742 bacpy(&cp.bdaddr, &ev->bdaddr);
743
744 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
745 cp.role = 0x00; /* Become master */
746 else
747 cp.role = 0x01; /* Remain slave */
748
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200749 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 } else {
751 /* Connection rejected */
752 struct hci_cp_reject_conn_req cp;
753
754 bacpy(&cp.bdaddr, &ev->bdaddr);
755 cp.reason = 0x0f;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200756 hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758}
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760static inline void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
761{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200762 struct hci_ev_disconn_complete *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200763 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 BT_DBG("%s status %d", hdev->name, ev->status);
766
767 if (ev->status)
768 return;
769
770 hci_dev_lock(hdev);
771
Marcel Holtmann04837f62006-07-03 10:02:33 +0200772 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (conn) {
774 conn->state = BT_CLOSED;
775 hci_proto_disconn_ind(conn, ev->reason);
776 hci_conn_del(conn);
777 }
778
779 hci_dev_unlock(hdev);
780}
781
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200782static inline void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
783{
784 struct hci_ev_auth_complete *ev = (void *) skb->data;
785 struct hci_conn *conn;
786
787 BT_DBG("%s status %d", hdev->name, ev->status);
788
789 hci_dev_lock(hdev);
790
791 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
792 if (conn) {
793 if (!ev->status)
794 conn->link_mode |= HCI_LM_AUTH;
795
796 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
797
798 hci_auth_cfm(conn, ev->status);
799
800 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
801 if (!ev->status) {
802 struct hci_cp_set_conn_encrypt cp;
803 cp.handle = cpu_to_le16(conn->handle);
804 cp.encrypt = 1;
805 hci_send_cmd(conn->hdev,
806 HCI_OP_SET_CONN_ENCRYPT, sizeof(cp), &cp);
807 } else {
808 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
809 hci_encrypt_cfm(conn, ev->status, 0x00);
810 }
811 }
812 }
813
814 hci_dev_unlock(hdev);
815}
816
817static inline void hci_remote_name_evt(struct hci_dev *hdev, struct sk_buff *skb)
818{
819 BT_DBG("%s", hdev->name);
820
821 hci_conn_check_pending(hdev);
822}
823
824static inline void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
825{
826 struct hci_ev_encrypt_change *ev = (void *) skb->data;
827 struct hci_conn *conn;
828
829 BT_DBG("%s status %d", hdev->name, ev->status);
830
831 hci_dev_lock(hdev);
832
833 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
834 if (conn) {
835 if (!ev->status) {
836 if (ev->encrypt)
837 conn->link_mode |= HCI_LM_ENCRYPT;
838 else
839 conn->link_mode &= ~HCI_LM_ENCRYPT;
840 }
841
842 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
843
844 hci_encrypt_cfm(conn, ev->status, ev->encrypt);
845 }
846
847 hci_dev_unlock(hdev);
848}
849
850static inline void hci_change_link_key_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
851{
852 struct hci_ev_change_link_key_complete *ev = (void *) skb->data;
853 struct hci_conn *conn;
854
855 BT_DBG("%s status %d", hdev->name, ev->status);
856
857 hci_dev_lock(hdev);
858
859 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
860 if (conn) {
861 if (!ev->status)
862 conn->link_mode |= HCI_LM_SECURE;
863
864 clear_bit(HCI_CONN_AUTH_PEND, &conn->pend);
865
866 hci_key_change_cfm(conn, ev->status);
867 }
868
869 hci_dev_unlock(hdev);
870}
871
872static inline void hci_remote_features_evt(struct hci_dev *hdev, struct sk_buff *skb)
873{
874 struct hci_ev_remote_features *ev = (void *) skb->data;
875 struct hci_conn *conn;
876
877 BT_DBG("%s status %d", hdev->name, ev->status);
878
879 if (ev->status)
880 return;
881
882 hci_dev_lock(hdev);
883
884 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
885 if (conn)
886 memcpy(conn->features, ev->features, 8);
887
888 hci_dev_unlock(hdev);
889}
890
891static inline void hci_remote_version_evt(struct hci_dev *hdev, struct sk_buff *skb)
892{
893 BT_DBG("%s", hdev->name);
894}
895
896static inline void hci_qos_setup_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
897{
898 BT_DBG("%s", hdev->name);
899}
900
901static inline void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
902{
903 struct hci_ev_cmd_complete *ev = (void *) skb->data;
904 __u16 opcode;
905
906 skb_pull(skb, sizeof(*ev));
907
908 opcode = __le16_to_cpu(ev->opcode);
909
910 switch (opcode) {
911 case HCI_OP_INQUIRY_CANCEL:
912 hci_cc_inquiry_cancel(hdev, skb);
913 break;
914
915 case HCI_OP_EXIT_PERIODIC_INQ:
916 hci_cc_exit_periodic_inq(hdev, skb);
917 break;
918
919 case HCI_OP_REMOTE_NAME_REQ_CANCEL:
920 hci_cc_remote_name_req_cancel(hdev, skb);
921 break;
922
923 case HCI_OP_ROLE_DISCOVERY:
924 hci_cc_role_discovery(hdev, skb);
925 break;
926
927 case HCI_OP_WRITE_LINK_POLICY:
928 hci_cc_write_link_policy(hdev, skb);
929 break;
930
931 case HCI_OP_RESET:
932 hci_cc_reset(hdev, skb);
933 break;
934
935 case HCI_OP_WRITE_LOCAL_NAME:
936 hci_cc_write_local_name(hdev, skb);
937 break;
938
939 case HCI_OP_READ_LOCAL_NAME:
940 hci_cc_read_local_name(hdev, skb);
941 break;
942
943 case HCI_OP_WRITE_AUTH_ENABLE:
944 hci_cc_write_auth_enable(hdev, skb);
945 break;
946
947 case HCI_OP_WRITE_ENCRYPT_MODE:
948 hci_cc_write_encrypt_mode(hdev, skb);
949 break;
950
951 case HCI_OP_WRITE_SCAN_ENABLE:
952 hci_cc_write_scan_enable(hdev, skb);
953 break;
954
955 case HCI_OP_READ_CLASS_OF_DEV:
956 hci_cc_read_class_of_dev(hdev, skb);
957 break;
958
959 case HCI_OP_WRITE_CLASS_OF_DEV:
960 hci_cc_write_class_of_dev(hdev, skb);
961 break;
962
963 case HCI_OP_READ_VOICE_SETTING:
964 hci_cc_read_voice_setting(hdev, skb);
965 break;
966
967 case HCI_OP_WRITE_VOICE_SETTING:
968 hci_cc_write_voice_setting(hdev, skb);
969 break;
970
971 case HCI_OP_HOST_BUFFER_SIZE:
972 hci_cc_host_buffer_size(hdev, skb);
973 break;
974
975 case HCI_OP_READ_LOCAL_VERSION:
976 hci_cc_read_local_version(hdev, skb);
977 break;
978
979 case HCI_OP_READ_LOCAL_COMMANDS:
980 hci_cc_read_local_commands(hdev, skb);
981 break;
982
983 case HCI_OP_READ_LOCAL_FEATURES:
984 hci_cc_read_local_features(hdev, skb);
985 break;
986
987 case HCI_OP_READ_BUFFER_SIZE:
988 hci_cc_read_buffer_size(hdev, skb);
989 break;
990
991 case HCI_OP_READ_BD_ADDR:
992 hci_cc_read_bd_addr(hdev, skb);
993 break;
994
995 default:
996 BT_DBG("%s opcode 0x%x", hdev->name, opcode);
997 break;
998 }
999
1000 if (ev->ncmd) {
1001 atomic_set(&hdev->cmd_cnt, 1);
1002 if (!skb_queue_empty(&hdev->cmd_q))
1003 hci_sched_cmd(hdev);
1004 }
1005}
1006
1007static inline void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb)
1008{
1009 struct hci_ev_cmd_status *ev = (void *) skb->data;
1010 __u16 opcode;
1011
1012 skb_pull(skb, sizeof(*ev));
1013
1014 opcode = __le16_to_cpu(ev->opcode);
1015
1016 switch (opcode) {
1017 case HCI_OP_INQUIRY:
1018 hci_cs_inquiry(hdev, ev->status);
1019 break;
1020
1021 case HCI_OP_CREATE_CONN:
1022 hci_cs_create_conn(hdev, ev->status);
1023 break;
1024
1025 case HCI_OP_ADD_SCO:
1026 hci_cs_add_sco(hdev, ev->status);
1027 break;
1028
1029 case HCI_OP_REMOTE_NAME_REQ:
1030 hci_cs_remote_name_req(hdev, ev->status);
1031 break;
1032
1033 case HCI_OP_SETUP_SYNC_CONN:
1034 hci_cs_setup_sync_conn(hdev, ev->status);
1035 break;
1036
1037 case HCI_OP_SNIFF_MODE:
1038 hci_cs_sniff_mode(hdev, ev->status);
1039 break;
1040
1041 case HCI_OP_EXIT_SNIFF_MODE:
1042 hci_cs_exit_sniff_mode(hdev, ev->status);
1043 break;
1044
1045 default:
1046 BT_DBG("%s opcode 0x%x", hdev->name, opcode);
1047 break;
1048 }
1049
1050 if (ev->ncmd) {
1051 atomic_set(&hdev->cmd_cnt, 1);
1052 if (!skb_queue_empty(&hdev->cmd_q))
1053 hci_sched_cmd(hdev);
1054 }
1055}
1056
1057static inline void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
1058{
1059 struct hci_ev_role_change *ev = (void *) skb->data;
1060 struct hci_conn *conn;
1061
1062 BT_DBG("%s status %d", hdev->name, ev->status);
1063
1064 hci_dev_lock(hdev);
1065
1066 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
1067 if (conn) {
1068 if (!ev->status) {
1069 if (ev->role)
1070 conn->link_mode &= ~HCI_LM_MASTER;
1071 else
1072 conn->link_mode |= HCI_LM_MASTER;
1073 }
1074
1075 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->pend);
1076
1077 hci_role_switch_cfm(conn, ev->status, ev->role);
1078 }
1079
1080 hci_dev_unlock(hdev);
1081}
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083static inline void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
1084{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001085 struct hci_ev_num_comp_pkts *ev = (void *) skb->data;
Marcel Holtmann1ebb9252005-11-08 09:57:21 -08001086 __le16 *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 int i;
1088
1089 skb_pull(skb, sizeof(*ev));
1090
1091 BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
1092
1093 if (skb->len < ev->num_hndl * 4) {
1094 BT_DBG("%s bad parameters", hdev->name);
1095 return;
1096 }
1097
1098 tasklet_disable(&hdev->tx_task);
1099
Marcel Holtmann1ebb9252005-11-08 09:57:21 -08001100 for (i = 0, ptr = (__le16 *) skb->data; i < ev->num_hndl; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 struct hci_conn *conn;
1102 __u16 handle, count;
1103
1104 handle = __le16_to_cpu(get_unaligned(ptr++));
1105 count = __le16_to_cpu(get_unaligned(ptr++));
1106
1107 conn = hci_conn_hash_lookup_handle(hdev, handle);
1108 if (conn) {
1109 conn->sent -= count;
1110
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02001111 if (conn->type == ACL_LINK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if ((hdev->acl_cnt += count) > hdev->acl_pkts)
1113 hdev->acl_cnt = hdev->acl_pkts;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +02001114 } else {
1115 if ((hdev->sco_cnt += count) > hdev->sco_pkts)
1116 hdev->sco_cnt = hdev->sco_pkts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118 }
1119 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 hci_sched_tx(hdev);
1122
1123 tasklet_enable(&hdev->tx_task);
1124}
1125
Marcel Holtmann04837f62006-07-03 10:02:33 +02001126static inline void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001128 struct hci_ev_mode_change *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02001129 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 BT_DBG("%s status %d", hdev->name, ev->status);
1132
1133 hci_dev_lock(hdev);
1134
Marcel Holtmann04837f62006-07-03 10:02:33 +02001135 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1136 if (conn) {
1137 conn->mode = ev->mode;
1138 conn->interval = __le16_to_cpu(ev->interval);
1139
1140 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
1141 if (conn->mode == HCI_CM_ACTIVE)
1142 conn->power_save = 1;
1143 else
1144 conn->power_save = 0;
1145 }
1146 }
1147
1148 hci_dev_unlock(hdev);
1149}
1150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151static inline void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
1152{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001153 BT_DBG("%s", hdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156static inline void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
1157{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001158 BT_DBG("%s", hdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161static inline void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
1162{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001163 BT_DBG("%s", hdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
Marcel Holtmann04837f62006-07-03 10:02:33 +02001166static inline void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
1167{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001168 struct hci_ev_clock_offset *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02001169 struct hci_conn *conn;
1170
1171 BT_DBG("%s status %d", hdev->name, ev->status);
1172
1173 hci_dev_lock(hdev);
1174
1175 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (conn && !ev->status) {
1177 struct inquiry_entry *ie;
1178
1179 if ((ie = hci_inquiry_cache_lookup(hdev, &conn->dst))) {
1180 ie->data.clock_offset = ev->clock_offset;
1181 ie->timestamp = jiffies;
1182 }
1183 }
1184
1185 hci_dev_unlock(hdev);
1186}
1187
Marcel Holtmann85a1e932005-08-09 20:28:02 -07001188static inline void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
1189{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001190 struct hci_ev_pscan_rep_mode *ev = (void *) skb->data;
Marcel Holtmann85a1e932005-08-09 20:28:02 -07001191 struct inquiry_entry *ie;
1192
1193 BT_DBG("%s", hdev->name);
1194
1195 hci_dev_lock(hdev);
1196
1197 if ((ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr))) {
1198 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
1199 ie->timestamp = jiffies;
1200 }
1201
1202 hci_dev_unlock(hdev);
1203}
1204
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001205static inline void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, struct sk_buff *skb)
1206{
1207 struct inquiry_data data;
1208 int num_rsp = *((__u8 *) skb->data);
1209
1210 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
1211
1212 if (!num_rsp)
1213 return;
1214
1215 hci_dev_lock(hdev);
1216
1217 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
1218 struct inquiry_info_with_rssi_and_pscan_mode *info = (void *) (skb->data + 1);
1219
1220 for (; num_rsp; num_rsp--) {
1221 bacpy(&data.bdaddr, &info->bdaddr);
1222 data.pscan_rep_mode = info->pscan_rep_mode;
1223 data.pscan_period_mode = info->pscan_period_mode;
1224 data.pscan_mode = info->pscan_mode;
1225 memcpy(data.dev_class, info->dev_class, 3);
1226 data.clock_offset = info->clock_offset;
1227 data.rssi = info->rssi;
1228 info++;
1229 hci_inquiry_cache_update(hdev, &data);
1230 }
1231 } else {
1232 struct inquiry_info_with_rssi *info = (void *) (skb->data + 1);
1233
1234 for (; num_rsp; num_rsp--) {
1235 bacpy(&data.bdaddr, &info->bdaddr);
1236 data.pscan_rep_mode = info->pscan_rep_mode;
1237 data.pscan_period_mode = info->pscan_period_mode;
1238 data.pscan_mode = 0x00;
1239 memcpy(data.dev_class, info->dev_class, 3);
1240 data.clock_offset = info->clock_offset;
1241 data.rssi = info->rssi;
1242 info++;
1243 hci_inquiry_cache_update(hdev, &data);
1244 }
1245 }
1246
1247 hci_dev_unlock(hdev);
1248}
1249
1250static inline void hci_remote_ext_features_evt(struct hci_dev *hdev, struct sk_buff *skb)
1251{
1252 BT_DBG("%s", hdev->name);
1253}
1254
1255static inline void hci_sync_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1256{
1257 BT_DBG("%s", hdev->name);
1258}
1259
1260static inline void hci_sync_conn_changed_evt(struct hci_dev *hdev, struct sk_buff *skb)
1261{
1262 BT_DBG("%s", hdev->name);
1263}
1264
Marcel Holtmann04837f62006-07-03 10:02:33 +02001265static inline void hci_sniff_subrate_evt(struct hci_dev *hdev, struct sk_buff *skb)
1266{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001267 struct hci_ev_sniff_subrate *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02001268 struct hci_conn *conn;
1269
1270 BT_DBG("%s status %d", hdev->name, ev->status);
1271
1272 hci_dev_lock(hdev);
1273
1274 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
1275 if (conn) {
1276 }
1277
1278 hci_dev_unlock(hdev);
1279}
1280
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001281static inline void hci_extended_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
1282{
1283 struct inquiry_data data;
1284 struct extended_inquiry_info *info = (void *) (skb->data + 1);
1285 int num_rsp = *((__u8 *) skb->data);
1286
1287 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
1288
1289 if (!num_rsp)
1290 return;
1291
1292 hci_dev_lock(hdev);
1293
1294 for (; num_rsp; num_rsp--) {
1295 bacpy(&data.bdaddr, &info->bdaddr);
1296 data.pscan_rep_mode = info->pscan_rep_mode;
1297 data.pscan_period_mode = info->pscan_period_mode;
1298 data.pscan_mode = 0x00;
1299 memcpy(data.dev_class, info->dev_class, 3);
1300 data.clock_offset = info->clock_offset;
1301 data.rssi = info->rssi;
1302 info++;
1303 hci_inquiry_cache_update(hdev, &data);
1304 }
1305
1306 hci_dev_unlock(hdev);
1307}
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
1310{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001311 struct hci_event_hdr *hdr = (void *) skb->data;
1312 __u8 event = hdr->evt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 skb_pull(skb, HCI_EVENT_HDR_SIZE);
1315
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001316 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 case HCI_EV_INQUIRY_COMPLETE:
1318 hci_inquiry_complete_evt(hdev, skb);
1319 break;
1320
1321 case HCI_EV_INQUIRY_RESULT:
1322 hci_inquiry_result_evt(hdev, skb);
1323 break;
1324
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001325 case HCI_EV_CONN_COMPLETE:
1326 hci_conn_complete_evt(hdev, skb);
Marcel Holtmann21d9e302005-09-13 01:32:25 +02001327 break;
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 case HCI_EV_CONN_REQUEST:
1330 hci_conn_request_evt(hdev, skb);
1331 break;
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 case HCI_EV_DISCONN_COMPLETE:
1334 hci_disconn_complete_evt(hdev, skb);
1335 break;
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 case HCI_EV_AUTH_COMPLETE:
1338 hci_auth_complete_evt(hdev, skb);
1339 break;
1340
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001341 case HCI_EV_REMOTE_NAME:
1342 hci_remote_name_evt(hdev, skb);
1343 break;
1344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 case HCI_EV_ENCRYPT_CHANGE:
1346 hci_encrypt_change_evt(hdev, skb);
1347 break;
1348
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001349 case HCI_EV_CHANGE_LINK_KEY_COMPLETE:
1350 hci_change_link_key_complete_evt(hdev, skb);
1351 break;
1352
1353 case HCI_EV_REMOTE_FEATURES:
1354 hci_remote_features_evt(hdev, skb);
1355 break;
1356
1357 case HCI_EV_REMOTE_VERSION:
1358 hci_remote_version_evt(hdev, skb);
1359 break;
1360
1361 case HCI_EV_QOS_SETUP_COMPLETE:
1362 hci_qos_setup_complete_evt(hdev, skb);
1363 break;
1364
1365 case HCI_EV_CMD_COMPLETE:
1366 hci_cmd_complete_evt(hdev, skb);
1367 break;
1368
1369 case HCI_EV_CMD_STATUS:
1370 hci_cmd_status_evt(hdev, skb);
1371 break;
1372
1373 case HCI_EV_ROLE_CHANGE:
1374 hci_role_change_evt(hdev, skb);
1375 break;
1376
1377 case HCI_EV_NUM_COMP_PKTS:
1378 hci_num_comp_pkts_evt(hdev, skb);
1379 break;
1380
1381 case HCI_EV_MODE_CHANGE:
1382 hci_mode_change_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 break;
1384
1385 case HCI_EV_PIN_CODE_REQ:
1386 hci_pin_code_request_evt(hdev, skb);
1387 break;
1388
1389 case HCI_EV_LINK_KEY_REQ:
1390 hci_link_key_request_evt(hdev, skb);
1391 break;
1392
1393 case HCI_EV_LINK_KEY_NOTIFY:
1394 hci_link_key_notify_evt(hdev, skb);
1395 break;
1396
1397 case HCI_EV_CLOCK_OFFSET:
1398 hci_clock_offset_evt(hdev, skb);
1399 break;
1400
Marcel Holtmann85a1e932005-08-09 20:28:02 -07001401 case HCI_EV_PSCAN_REP_MODE:
1402 hci_pscan_rep_mode_evt(hdev, skb);
1403 break;
1404
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001405 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
1406 hci_inquiry_result_with_rssi_evt(hdev, skb);
1407 break;
1408
1409 case HCI_EV_REMOTE_EXT_FEATURES:
1410 hci_remote_ext_features_evt(hdev, skb);
1411 break;
1412
1413 case HCI_EV_SYNC_CONN_COMPLETE:
1414 hci_sync_conn_complete_evt(hdev, skb);
1415 break;
1416
1417 case HCI_EV_SYNC_CONN_CHANGED:
1418 hci_sync_conn_changed_evt(hdev, skb);
1419 break;
1420
Marcel Holtmann04837f62006-07-03 10:02:33 +02001421 case HCI_EV_SNIFF_SUBRATE:
1422 hci_sniff_subrate_evt(hdev, skb);
1423 break;
1424
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001425 case HCI_EV_EXTENDED_INQUIRY_RESULT:
1426 hci_extended_inquiry_result_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 break;
1428
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001429 default:
1430 BT_DBG("%s event 0x%x", hdev->name, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 break;
1432 }
1433
1434 kfree_skb(skb);
1435 hdev->stat.evt_rx++;
1436}
1437
1438/* Generate internal stack event */
1439void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
1440{
1441 struct hci_event_hdr *hdr;
1442 struct hci_ev_stack_internal *ev;
1443 struct sk_buff *skb;
1444
1445 skb = bt_skb_alloc(HCI_EVENT_HDR_SIZE + sizeof(*ev) + dlen, GFP_ATOMIC);
1446 if (!skb)
1447 return;
1448
1449 hdr = (void *) skb_put(skb, HCI_EVENT_HDR_SIZE);
1450 hdr->evt = HCI_EV_STACK_INTERNAL;
1451 hdr->plen = sizeof(*ev) + dlen;
1452
1453 ev = (void *) skb_put(skb, sizeof(*ev) + dlen);
1454 ev->type = type;
1455 memcpy(ev->data, data, dlen);
1456
Marcel Holtmann576c7d82005-08-06 12:36:54 +02001457 bt_cb(skb)->incoming = 1;
Patrick McHardya61bbcf2005-08-14 17:24:31 -07001458 __net_timestamp(skb);
Marcel Holtmann576c7d82005-08-06 12:36:54 +02001459
Marcel Holtmann0d48d932005-08-09 20:30:28 -07001460 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 skb->dev = (void *) hdev;
1462 hci_send_to_sock(hdev, skb);
1463 kfree_skb(skb);
1464}