Bluetooth: Use the updated key structures for handling LTKs
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / bluetooth / hci_core.c
1 /*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated
4 Copyright (C) 2011 ProFUSION Embedded Systems
5
6 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation;
11
12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
13 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
15 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
16 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
17 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20
21 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
22 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
23 SOFTWARE IS DISCLAIMED.
24 */
25
26 /* Bluetooth HCI core. */
27
28 #include <linux/jiffies.h>
29 #include <linux/module.h>
30 #include <linux/kmod.h>
31
32 #include <linux/types.h>
33 #include <linux/errno.h>
34 #include <linux/kernel.h>
35 #include <linux/sched.h>
36 #include <linux/slab.h>
37 #include <linux/poll.h>
38 #include <linux/fcntl.h>
39 #include <linux/init.h>
40 #include <linux/skbuff.h>
41 #include <linux/workqueue.h>
42 #include <linux/interrupt.h>
43 #include <linux/notifier.h>
44 #include <linux/rfkill.h>
45 #include <linux/timer.h>
46 #include <linux/crypto.h>
47 #include <net/sock.h>
48
49 #include <asm/system.h>
50 #include <linux/uaccess.h>
51 #include <asm/unaligned.h>
52
53 #include <net/bluetooth/bluetooth.h>
54 #include <net/bluetooth/hci_core.h>
55
56 #define AUTO_OFF_TIMEOUT 2000
57
58 bool enable_hs;
59
60 static void hci_rx_work(struct work_struct *work);
61 static void hci_cmd_work(struct work_struct *work);
62 static void hci_tx_work(struct work_struct *work);
63
64 /* HCI device list */
65 LIST_HEAD(hci_dev_list);
66 DEFINE_RWLOCK(hci_dev_list_lock);
67
68 /* HCI callback list */
69 LIST_HEAD(hci_cb_list);
70 DEFINE_RWLOCK(hci_cb_list_lock);
71
72 /* HCI notifiers list */
73 static ATOMIC_NOTIFIER_HEAD(hci_notifier);
74
75 /* ---- HCI notifications ---- */
76
77 int hci_register_notifier(struct notifier_block *nb)
78 {
79 return atomic_notifier_chain_register(&hci_notifier, nb);
80 }
81
82 int hci_unregister_notifier(struct notifier_block *nb)
83 {
84 return atomic_notifier_chain_unregister(&hci_notifier, nb);
85 }
86
87 static void hci_notify(struct hci_dev *hdev, int event)
88 {
89 atomic_notifier_call_chain(&hci_notifier, event, hdev);
90 }
91
92 /* ---- HCI requests ---- */
93
94 void hci_req_complete(struct hci_dev *hdev, __u16 cmd, int result)
95 {
96 BT_DBG("%s command 0x%04x result 0x%2.2x", hdev->name, cmd, result);
97
98 /* If this is the init phase check if the completed command matches
99 * the last init command, and if not just return.
100 */
101 if (test_bit(HCI_INIT, &hdev->flags) && hdev->init_last_cmd != cmd)
102 return;
103
104 if (hdev->req_status == HCI_REQ_PEND) {
105 hdev->req_result = result;
106 hdev->req_status = HCI_REQ_DONE;
107 wake_up_interruptible(&hdev->req_wait_q);
108 }
109 }
110
111 static void hci_req_cancel(struct hci_dev *hdev, int err)
112 {
113 BT_DBG("%s err 0x%2.2x", hdev->name, err);
114
115 if (hdev->req_status == HCI_REQ_PEND) {
116 hdev->req_result = err;
117 hdev->req_status = HCI_REQ_CANCELED;
118 wake_up_interruptible(&hdev->req_wait_q);
119 }
120 }
121
122 /* Execute request and wait for completion. */
123 static int __hci_request(struct hci_dev *hdev, void (*req)(struct hci_dev *hdev, unsigned long opt),
124 unsigned long opt, __u32 timeout)
125 {
126 DECLARE_WAITQUEUE(wait, current);
127 int err = 0;
128
129 BT_DBG("%s start", hdev->name);
130
131 hdev->req_status = HCI_REQ_PEND;
132
133 add_wait_queue(&hdev->req_wait_q, &wait);
134 set_current_state(TASK_INTERRUPTIBLE);
135
136 req(hdev, opt);
137 schedule_timeout(timeout);
138
139 remove_wait_queue(&hdev->req_wait_q, &wait);
140
141 if (signal_pending(current))
142 return -EINTR;
143
144 switch (hdev->req_status) {
145 case HCI_REQ_DONE:
146 err = -bt_to_errno(hdev->req_result);
147 break;
148
149 case HCI_REQ_CANCELED:
150 err = -hdev->req_result;
151 break;
152
153 default:
154 err = -ETIMEDOUT;
155 break;
156 }
157
158 hdev->req_status = hdev->req_result = 0;
159
160 BT_DBG("%s end: err %d", hdev->name, err);
161
162 return err;
163 }
164
165 static inline int hci_request(struct hci_dev *hdev, void (*req)(struct hci_dev *hdev, unsigned long opt),
166 unsigned long opt, __u32 timeout)
167 {
168 int ret;
169
170 if (!test_bit(HCI_UP, &hdev->flags))
171 return -ENETDOWN;
172
173 /* Serialize all requests */
174 hci_req_lock(hdev);
175 ret = __hci_request(hdev, req, opt, timeout);
176 hci_req_unlock(hdev);
177
178 return ret;
179 }
180
181 static void hci_reset_req(struct hci_dev *hdev, unsigned long opt)
182 {
183 BT_DBG("%s %ld", hdev->name, opt);
184
185 /* Reset device */
186 set_bit(HCI_RESET, &hdev->flags);
187 hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL);
188 }
189
190 static void bredr_init(struct hci_dev *hdev)
191 {
192 struct hci_cp_delete_stored_link_key cp;
193 __le16 param;
194 __u8 flt_type;
195
196 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED;
197
198 /* Mandatory initialization */
199
200 /* Reset */
201 if (!test_bit(HCI_QUIRK_NO_RESET, &hdev->quirks)) {
202 set_bit(HCI_RESET, &hdev->flags);
203 hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL);
204 }
205
206 /* Read Local Supported Features */
207 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_FEATURES, 0, NULL);
208
209 /* Read Local Version */
210 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL);
211
212 /* Read Buffer Size (ACL mtu, max pkt, etc.) */
213 hci_send_cmd(hdev, HCI_OP_READ_BUFFER_SIZE, 0, NULL);
214
215 /* Read BD Address */
216 hci_send_cmd(hdev, HCI_OP_READ_BD_ADDR, 0, NULL);
217
218 /* Read Class of Device */
219 hci_send_cmd(hdev, HCI_OP_READ_CLASS_OF_DEV, 0, NULL);
220
221 /* Read Local Name */
222 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_NAME, 0, NULL);
223
224 /* Read Voice Setting */
225 hci_send_cmd(hdev, HCI_OP_READ_VOICE_SETTING, 0, NULL);
226
227 /* Optional initialization */
228
229 /* Clear Event Filters */
230 flt_type = HCI_FLT_CLEAR_ALL;
231 hci_send_cmd(hdev, HCI_OP_SET_EVENT_FLT, 1, &flt_type);
232
233 /* Connection accept timeout ~20 secs */
234 param = cpu_to_le16(0x7d00);
235 hci_send_cmd(hdev, HCI_OP_WRITE_CA_TIMEOUT, 2, &param);
236
237 bacpy(&cp.bdaddr, BDADDR_ANY);
238 cp.delete_all = 1;
239 hci_send_cmd(hdev, HCI_OP_DELETE_STORED_LINK_KEY, sizeof(cp), &cp);
240 }
241
242 static void amp_init(struct hci_dev *hdev)
243 {
244 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED;
245
246 /* Reset */
247 hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL);
248
249 /* Read Local Version */
250 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL);
251 }
252
253 static void hci_init_req(struct hci_dev *hdev, unsigned long opt)
254 {
255 struct sk_buff *skb;
256
257 BT_DBG("%s %ld", hdev->name, opt);
258
259 /* Driver initialization */
260
261 /* Special commands */
262 while ((skb = skb_dequeue(&hdev->driver_init))) {
263 bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
264 skb->dev = (void *) hdev;
265
266 skb_queue_tail(&hdev->cmd_q, skb);
267 queue_work(hdev->workqueue, &hdev->cmd_work);
268 }
269 skb_queue_purge(&hdev->driver_init);
270
271 switch (hdev->dev_type) {
272 case HCI_BREDR:
273 bredr_init(hdev);
274 break;
275
276 case HCI_AMP:
277 amp_init(hdev);
278 break;
279
280 default:
281 BT_ERR("Unknown device type %d", hdev->dev_type);
282 break;
283 }
284
285 }
286
287 static void hci_le_init_req(struct hci_dev *hdev, unsigned long opt)
288 {
289 BT_DBG("%s", hdev->name);
290
291 /* Read LE buffer size */
292 hci_send_cmd(hdev, HCI_OP_LE_READ_BUFFER_SIZE, 0, NULL);
293 }
294
295 static void hci_scan_req(struct hci_dev *hdev, unsigned long opt)
296 {
297 __u8 scan = opt;
298
299 BT_DBG("%s %x", hdev->name, scan);
300
301 /* Inquiry and Page scans */
302 hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
303 }
304
305 static void hci_auth_req(struct hci_dev *hdev, unsigned long opt)
306 {
307 __u8 auth = opt;
308
309 BT_DBG("%s %x", hdev->name, auth);
310
311 /* Authentication */
312 hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, 1, &auth);
313 }
314
315 static void hci_encrypt_req(struct hci_dev *hdev, unsigned long opt)
316 {
317 __u8 encrypt = opt;
318
319 BT_DBG("%s %x", hdev->name, encrypt);
320
321 /* Encryption */
322 hci_send_cmd(hdev, HCI_OP_WRITE_ENCRYPT_MODE, 1, &encrypt);
323 }
324
325 static void hci_linkpol_req(struct hci_dev *hdev, unsigned long opt)
326 {
327 __le16 policy = cpu_to_le16(opt);
328
329 BT_DBG("%s %x", hdev->name, policy);
330
331 /* Default link policy */
332 hci_send_cmd(hdev, HCI_OP_WRITE_DEF_LINK_POLICY, 2, &policy);
333 }
334
335 /* Get HCI device by index.
336 * Device is held on return. */
337 struct hci_dev *hci_dev_get(int index)
338 {
339 struct hci_dev *hdev = NULL, *d;
340
341 BT_DBG("%d", index);
342
343 if (index < 0)
344 return NULL;
345
346 read_lock(&hci_dev_list_lock);
347 list_for_each_entry(d, &hci_dev_list, list) {
348 if (d->id == index) {
349 hdev = hci_dev_hold(d);
350 break;
351 }
352 }
353 read_unlock(&hci_dev_list_lock);
354 return hdev;
355 }
356
357 /* ---- Inquiry support ---- */
358
359 bool hci_discovery_active(struct hci_dev *hdev)
360 {
361 struct discovery_state *discov = &hdev->discovery;
362
363 if (discov->state == DISCOVERY_INQUIRY ||
364 discov->state == DISCOVERY_RESOLVING)
365 return true;
366
367 return false;
368 }
369
370 void hci_discovery_set_state(struct hci_dev *hdev, int state)
371 {
372 BT_DBG("%s state %u -> %u", hdev->name, hdev->discovery.state, state);
373
374 if (hdev->discovery.state == state)
375 return;
376
377 switch (state) {
378 case DISCOVERY_STOPPED:
379 mgmt_discovering(hdev, 0);
380 break;
381 case DISCOVERY_STARTING:
382 break;
383 case DISCOVERY_INQUIRY:
384 mgmt_discovering(hdev, 1);
385 break;
386 case DISCOVERY_RESOLVING:
387 break;
388 case DISCOVERY_STOPPING:
389 break;
390 }
391
392 hdev->discovery.state = state;
393 }
394
395 static void inquiry_cache_flush(struct hci_dev *hdev)
396 {
397 struct discovery_state *cache = &hdev->discovery;
398 struct inquiry_entry *p, *n;
399
400 list_for_each_entry_safe(p, n, &cache->all, all) {
401 list_del(&p->all);
402 kfree(p);
403 }
404
405 INIT_LIST_HEAD(&cache->unknown);
406 INIT_LIST_HEAD(&cache->resolve);
407 cache->state = DISCOVERY_STOPPED;
408 }
409
410 struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr)
411 {
412 struct discovery_state *cache = &hdev->discovery;
413 struct inquiry_entry *e;
414
415 BT_DBG("cache %p, %s", cache, batostr(bdaddr));
416
417 list_for_each_entry(e, &cache->all, all) {
418 if (!bacmp(&e->data.bdaddr, bdaddr))
419 return e;
420 }
421
422 return NULL;
423 }
424
425 struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
426 bdaddr_t *bdaddr)
427 {
428 struct discovery_state *cache = &hdev->discovery;
429 struct inquiry_entry *e;
430
431 BT_DBG("cache %p, %s", cache, batostr(bdaddr));
432
433 list_for_each_entry(e, &cache->unknown, list) {
434 if (!bacmp(&e->data.bdaddr, bdaddr))
435 return e;
436 }
437
438 return NULL;
439 }
440
441 struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev,
442 bdaddr_t *bdaddr,
443 int state)
444 {
445 struct discovery_state *cache = &hdev->discovery;
446 struct inquiry_entry *e;
447
448 BT_DBG("cache %p bdaddr %s state %d", cache, batostr(bdaddr), state);
449
450 list_for_each_entry(e, &cache->resolve, list) {
451 if (!bacmp(bdaddr, BDADDR_ANY) && e->name_state == state)
452 return e;
453 if (!bacmp(&e->data.bdaddr, bdaddr))
454 return e;
455 }
456
457 return NULL;
458 }
459
460 void hci_inquiry_cache_update_resolve(struct hci_dev *hdev,
461 struct inquiry_entry *ie)
462 {
463 struct discovery_state *cache = &hdev->discovery;
464 struct list_head *pos = &cache->resolve;
465 struct inquiry_entry *p;
466
467 list_del(&ie->list);
468
469 list_for_each_entry(p, &cache->resolve, list) {
470 if (p->name_state != NAME_PENDING &&
471 abs(p->data.rssi) >= abs(ie->data.rssi))
472 break;
473 pos = &p->list;
474 }
475
476 list_add(&ie->list, pos);
477 }
478
479 bool hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
480 bool name_known)
481 {
482 struct discovery_state *cache = &hdev->discovery;
483 struct inquiry_entry *ie;
484
485 BT_DBG("cache %p, %s", cache, batostr(&data->bdaddr));
486
487 ie = hci_inquiry_cache_lookup(hdev, &data->bdaddr);
488 if (ie) {
489 if (ie->name_state == NAME_NEEDED &&
490 data->rssi != ie->data.rssi) {
491 ie->data.rssi = data->rssi;
492 hci_inquiry_cache_update_resolve(hdev, ie);
493 }
494
495 goto update;
496 }
497
498 /* Entry not in the cache. Add new one. */
499 ie = kzalloc(sizeof(struct inquiry_entry), GFP_ATOMIC);
500 if (!ie)
501 return false;
502
503 list_add(&ie->all, &cache->all);
504
505 if (name_known) {
506 ie->name_state = NAME_KNOWN;
507 } else {
508 ie->name_state = NAME_NOT_KNOWN;
509 list_add(&ie->list, &cache->unknown);
510 }
511
512 update:
513 if (name_known && ie->name_state != NAME_KNOWN &&
514 ie->name_state != NAME_PENDING) {
515 ie->name_state = NAME_KNOWN;
516 list_del(&ie->list);
517 }
518
519 memcpy(&ie->data, data, sizeof(*data));
520 ie->timestamp = jiffies;
521 cache->timestamp = jiffies;
522
523 if (ie->name_state == NAME_NOT_KNOWN)
524 return false;
525
526 return true;
527 }
528
529 static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf)
530 {
531 struct discovery_state *cache = &hdev->discovery;
532 struct inquiry_info *info = (struct inquiry_info *) buf;
533 struct inquiry_entry *e;
534 int copied = 0;
535
536 list_for_each_entry(e, &cache->all, all) {
537 struct inquiry_data *data = &e->data;
538
539 if (copied >= num)
540 break;
541
542 bacpy(&info->bdaddr, &data->bdaddr);
543 info->pscan_rep_mode = data->pscan_rep_mode;
544 info->pscan_period_mode = data->pscan_period_mode;
545 info->pscan_mode = data->pscan_mode;
546 memcpy(info->dev_class, data->dev_class, 3);
547 info->clock_offset = data->clock_offset;
548
549 info++;
550 copied++;
551 }
552
553 BT_DBG("cache %p, copied %d", cache, copied);
554 return copied;
555 }
556
557 static void hci_inq_req(struct hci_dev *hdev, unsigned long opt)
558 {
559 struct hci_inquiry_req *ir = (struct hci_inquiry_req *) opt;
560 struct hci_cp_inquiry cp;
561
562 BT_DBG("%s", hdev->name);
563
564 if (test_bit(HCI_INQUIRY, &hdev->flags))
565 return;
566
567 /* Start Inquiry */
568 memcpy(&cp.lap, &ir->lap, 3);
569 cp.length = ir->length;
570 cp.num_rsp = ir->num_rsp;
571 hci_send_cmd(hdev, HCI_OP_INQUIRY, sizeof(cp), &cp);
572 }
573
574 int hci_inquiry(void __user *arg)
575 {
576 __u8 __user *ptr = arg;
577 struct hci_inquiry_req ir;
578 struct hci_dev *hdev;
579 int err = 0, do_inquiry = 0, max_rsp;
580 long timeo;
581 __u8 *buf;
582
583 if (copy_from_user(&ir, ptr, sizeof(ir)))
584 return -EFAULT;
585
586 hdev = hci_dev_get(ir.dev_id);
587 if (!hdev)
588 return -ENODEV;
589
590 hci_dev_lock(hdev);
591 if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX ||
592 inquiry_cache_empty(hdev) ||
593 ir.flags & IREQ_CACHE_FLUSH) {
594 inquiry_cache_flush(hdev);
595 do_inquiry = 1;
596 }
597 hci_dev_unlock(hdev);
598
599 timeo = ir.length * msecs_to_jiffies(2000);
600
601 if (do_inquiry) {
602 err = hci_request(hdev, hci_inq_req, (unsigned long)&ir, timeo);
603 if (err < 0)
604 goto done;
605 }
606
607 /* for unlimited number of responses we will use buffer with 255 entries */
608 max_rsp = (ir.num_rsp == 0) ? 255 : ir.num_rsp;
609
610 /* cache_dump can't sleep. Therefore we allocate temp buffer and then
611 * copy it to the user space.
612 */
613 buf = kmalloc(sizeof(struct inquiry_info) * max_rsp, GFP_KERNEL);
614 if (!buf) {
615 err = -ENOMEM;
616 goto done;
617 }
618
619 hci_dev_lock(hdev);
620 ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf);
621 hci_dev_unlock(hdev);
622
623 BT_DBG("num_rsp %d", ir.num_rsp);
624
625 if (!copy_to_user(ptr, &ir, sizeof(ir))) {
626 ptr += sizeof(ir);
627 if (copy_to_user(ptr, buf, sizeof(struct inquiry_info) *
628 ir.num_rsp))
629 err = -EFAULT;
630 } else
631 err = -EFAULT;
632
633 kfree(buf);
634
635 done:
636 hci_dev_put(hdev);
637 return err;
638 }
639
640 /* ---- HCI ioctl helpers ---- */
641
642 int hci_dev_open(__u16 dev)
643 {
644 struct hci_dev *hdev;
645 int ret = 0;
646
647 hdev = hci_dev_get(dev);
648 if (!hdev)
649 return -ENODEV;
650
651 BT_DBG("%s %p", hdev->name, hdev);
652
653 hci_req_lock(hdev);
654
655 if (hdev->rfkill && rfkill_blocked(hdev->rfkill)) {
656 ret = -ERFKILL;
657 goto done;
658 }
659
660 if (test_bit(HCI_UP, &hdev->flags)) {
661 ret = -EALREADY;
662 goto done;
663 }
664
665 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
666 set_bit(HCI_RAW, &hdev->flags);
667
668 /* Treat all non BR/EDR controllers as raw devices if
669 enable_hs is not set */
670 if (hdev->dev_type != HCI_BREDR && !enable_hs)
671 set_bit(HCI_RAW, &hdev->flags);
672
673 if (hdev->open(hdev)) {
674 ret = -EIO;
675 goto done;
676 }
677
678 if (!test_bit(HCI_RAW, &hdev->flags)) {
679 atomic_set(&hdev->cmd_cnt, 1);
680 set_bit(HCI_INIT, &hdev->flags);
681 hdev->init_last_cmd = 0;
682
683 ret = __hci_request(hdev, hci_init_req, 0,
684 msecs_to_jiffies(HCI_INIT_TIMEOUT));
685
686 if (lmp_host_le_capable(hdev))
687 ret = __hci_request(hdev, hci_le_init_req, 0,
688 msecs_to_jiffies(HCI_INIT_TIMEOUT));
689
690 clear_bit(HCI_INIT, &hdev->flags);
691 }
692
693 if (!ret) {
694 hci_dev_hold(hdev);
695 set_bit(HCI_UP, &hdev->flags);
696 hci_notify(hdev, HCI_DEV_UP);
697 if (!test_bit(HCI_SETUP, &hdev->dev_flags)) {
698 hci_dev_lock(hdev);
699 mgmt_powered(hdev, 1);
700 hci_dev_unlock(hdev);
701 }
702 } else {
703 /* Init failed, cleanup */
704 flush_work(&hdev->tx_work);
705 flush_work(&hdev->cmd_work);
706 flush_work(&hdev->rx_work);
707
708 skb_queue_purge(&hdev->cmd_q);
709 skb_queue_purge(&hdev->rx_q);
710
711 if (hdev->flush)
712 hdev->flush(hdev);
713
714 if (hdev->sent_cmd) {
715 kfree_skb(hdev->sent_cmd);
716 hdev->sent_cmd = NULL;
717 }
718
719 hdev->close(hdev);
720 hdev->flags = 0;
721 }
722
723 done:
724 hci_req_unlock(hdev);
725 hci_dev_put(hdev);
726 return ret;
727 }
728
729 static int hci_dev_do_close(struct hci_dev *hdev)
730 {
731 BT_DBG("%s %p", hdev->name, hdev);
732
733 hci_req_cancel(hdev, ENODEV);
734 hci_req_lock(hdev);
735
736 if (!test_and_clear_bit(HCI_UP, &hdev->flags)) {
737 del_timer_sync(&hdev->cmd_timer);
738 hci_req_unlock(hdev);
739 return 0;
740 }
741
742 /* Flush RX and TX works */
743 flush_work(&hdev->tx_work);
744 flush_work(&hdev->rx_work);
745
746 if (hdev->discov_timeout > 0) {
747 cancel_delayed_work(&hdev->discov_off);
748 hdev->discov_timeout = 0;
749 }
750
751 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
752 cancel_delayed_work(&hdev->power_off);
753
754 if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
755 cancel_delayed_work(&hdev->service_cache);
756
757 hci_dev_lock(hdev);
758 inquiry_cache_flush(hdev);
759 hci_conn_hash_flush(hdev);
760 hci_dev_unlock(hdev);
761
762 hci_notify(hdev, HCI_DEV_DOWN);
763
764 if (hdev->flush)
765 hdev->flush(hdev);
766
767 /* Reset device */
768 skb_queue_purge(&hdev->cmd_q);
769 atomic_set(&hdev->cmd_cnt, 1);
770 if (!test_bit(HCI_RAW, &hdev->flags)) {
771 set_bit(HCI_INIT, &hdev->flags);
772 __hci_request(hdev, hci_reset_req, 0,
773 msecs_to_jiffies(250));
774 clear_bit(HCI_INIT, &hdev->flags);
775 }
776
777 /* flush cmd work */
778 flush_work(&hdev->cmd_work);
779
780 /* Drop queues */
781 skb_queue_purge(&hdev->rx_q);
782 skb_queue_purge(&hdev->cmd_q);
783 skb_queue_purge(&hdev->raw_q);
784
785 /* Drop last sent command */
786 if (hdev->sent_cmd) {
787 del_timer_sync(&hdev->cmd_timer);
788 kfree_skb(hdev->sent_cmd);
789 hdev->sent_cmd = NULL;
790 }
791
792 /* After this point our queues are empty
793 * and no tasks are scheduled. */
794 hdev->close(hdev);
795
796 hci_dev_lock(hdev);
797 mgmt_powered(hdev, 0);
798 hci_dev_unlock(hdev);
799
800 /* Clear flags */
801 hdev->flags = 0;
802
803 hci_req_unlock(hdev);
804
805 hci_dev_put(hdev);
806 return 0;
807 }
808
809 int hci_dev_close(__u16 dev)
810 {
811 struct hci_dev *hdev;
812 int err;
813
814 hdev = hci_dev_get(dev);
815 if (!hdev)
816 return -ENODEV;
817 err = hci_dev_do_close(hdev);
818 hci_dev_put(hdev);
819 return err;
820 }
821
822 int hci_dev_reset(__u16 dev)
823 {
824 struct hci_dev *hdev;
825 int ret = 0;
826
827 hdev = hci_dev_get(dev);
828 if (!hdev)
829 return -ENODEV;
830
831 hci_req_lock(hdev);
832
833 if (!test_bit(HCI_UP, &hdev->flags))
834 goto done;
835
836 /* Drop queues */
837 skb_queue_purge(&hdev->rx_q);
838 skb_queue_purge(&hdev->cmd_q);
839
840 hci_dev_lock(hdev);
841 inquiry_cache_flush(hdev);
842 hci_conn_hash_flush(hdev);
843 hci_dev_unlock(hdev);
844
845 if (hdev->flush)
846 hdev->flush(hdev);
847
848 atomic_set(&hdev->cmd_cnt, 1);
849 hdev->acl_cnt = 0; hdev->sco_cnt = 0; hdev->le_cnt = 0;
850
851 if (!test_bit(HCI_RAW, &hdev->flags))
852 ret = __hci_request(hdev, hci_reset_req, 0,
853 msecs_to_jiffies(HCI_INIT_TIMEOUT));
854
855 done:
856 hci_req_unlock(hdev);
857 hci_dev_put(hdev);
858 return ret;
859 }
860
861 int hci_dev_reset_stat(__u16 dev)
862 {
863 struct hci_dev *hdev;
864 int ret = 0;
865
866 hdev = hci_dev_get(dev);
867 if (!hdev)
868 return -ENODEV;
869
870 memset(&hdev->stat, 0, sizeof(struct hci_dev_stats));
871
872 hci_dev_put(hdev);
873
874 return ret;
875 }
876
877 int hci_dev_cmd(unsigned int cmd, void __user *arg)
878 {
879 struct hci_dev *hdev;
880 struct hci_dev_req dr;
881 int err = 0;
882
883 if (copy_from_user(&dr, arg, sizeof(dr)))
884 return -EFAULT;
885
886 hdev = hci_dev_get(dr.dev_id);
887 if (!hdev)
888 return -ENODEV;
889
890 switch (cmd) {
891 case HCISETAUTH:
892 err = hci_request(hdev, hci_auth_req, dr.dev_opt,
893 msecs_to_jiffies(HCI_INIT_TIMEOUT));
894 break;
895
896 case HCISETENCRYPT:
897 if (!lmp_encrypt_capable(hdev)) {
898 err = -EOPNOTSUPP;
899 break;
900 }
901
902 if (!test_bit(HCI_AUTH, &hdev->flags)) {
903 /* Auth must be enabled first */
904 err = hci_request(hdev, hci_auth_req, dr.dev_opt,
905 msecs_to_jiffies(HCI_INIT_TIMEOUT));
906 if (err)
907 break;
908 }
909
910 err = hci_request(hdev, hci_encrypt_req, dr.dev_opt,
911 msecs_to_jiffies(HCI_INIT_TIMEOUT));
912 break;
913
914 case HCISETSCAN:
915 err = hci_request(hdev, hci_scan_req, dr.dev_opt,
916 msecs_to_jiffies(HCI_INIT_TIMEOUT));
917 break;
918
919 case HCISETLINKPOL:
920 err = hci_request(hdev, hci_linkpol_req, dr.dev_opt,
921 msecs_to_jiffies(HCI_INIT_TIMEOUT));
922 break;
923
924 case HCISETLINKMODE:
925 hdev->link_mode = ((__u16) dr.dev_opt) &
926 (HCI_LM_MASTER | HCI_LM_ACCEPT);
927 break;
928
929 case HCISETPTYPE:
930 hdev->pkt_type = (__u16) dr.dev_opt;
931 break;
932
933 case HCISETACLMTU:
934 hdev->acl_mtu = *((__u16 *) &dr.dev_opt + 1);
935 hdev->acl_pkts = *((__u16 *) &dr.dev_opt + 0);
936 break;
937
938 case HCISETSCOMTU:
939 hdev->sco_mtu = *((__u16 *) &dr.dev_opt + 1);
940 hdev->sco_pkts = *((__u16 *) &dr.dev_opt + 0);
941 break;
942
943 default:
944 err = -EINVAL;
945 break;
946 }
947
948 hci_dev_put(hdev);
949 return err;
950 }
951
952 int hci_get_dev_list(void __user *arg)
953 {
954 struct hci_dev *hdev;
955 struct hci_dev_list_req *dl;
956 struct hci_dev_req *dr;
957 int n = 0, size, err;
958 __u16 dev_num;
959
960 if (get_user(dev_num, (__u16 __user *) arg))
961 return -EFAULT;
962
963 if (!dev_num || dev_num > (PAGE_SIZE * 2) / sizeof(*dr))
964 return -EINVAL;
965
966 size = sizeof(*dl) + dev_num * sizeof(*dr);
967
968 dl = kzalloc(size, GFP_KERNEL);
969 if (!dl)
970 return -ENOMEM;
971
972 dr = dl->dev_req;
973
974 read_lock(&hci_dev_list_lock);
975 list_for_each_entry(hdev, &hci_dev_list, list) {
976 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
977 cancel_delayed_work(&hdev->power_off);
978
979 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
980 set_bit(HCI_PAIRABLE, &hdev->dev_flags);
981
982 (dr + n)->dev_id = hdev->id;
983 (dr + n)->dev_opt = hdev->flags;
984
985 if (++n >= dev_num)
986 break;
987 }
988 read_unlock(&hci_dev_list_lock);
989
990 dl->dev_num = n;
991 size = sizeof(*dl) + n * sizeof(*dr);
992
993 err = copy_to_user(arg, dl, size);
994 kfree(dl);
995
996 return err ? -EFAULT : 0;
997 }
998
999 int hci_get_dev_info(void __user *arg)
1000 {
1001 struct hci_dev *hdev;
1002 struct hci_dev_info di;
1003 int err = 0;
1004
1005 if (copy_from_user(&di, arg, sizeof(di)))
1006 return -EFAULT;
1007
1008 hdev = hci_dev_get(di.dev_id);
1009 if (!hdev)
1010 return -ENODEV;
1011
1012 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
1013 cancel_delayed_work_sync(&hdev->power_off);
1014
1015 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
1016 set_bit(HCI_PAIRABLE, &hdev->dev_flags);
1017
1018 strcpy(di.name, hdev->name);
1019 di.bdaddr = hdev->bdaddr;
1020 di.type = (hdev->bus & 0x0f) | (hdev->dev_type << 4);
1021 di.flags = hdev->flags;
1022 di.pkt_type = hdev->pkt_type;
1023 di.acl_mtu = hdev->acl_mtu;
1024 di.acl_pkts = hdev->acl_pkts;
1025 di.sco_mtu = hdev->sco_mtu;
1026 di.sco_pkts = hdev->sco_pkts;
1027 di.link_policy = hdev->link_policy;
1028 di.link_mode = hdev->link_mode;
1029
1030 memcpy(&di.stat, &hdev->stat, sizeof(di.stat));
1031 memcpy(&di.features, &hdev->features, sizeof(di.features));
1032
1033 if (copy_to_user(arg, &di, sizeof(di)))
1034 err = -EFAULT;
1035
1036 hci_dev_put(hdev);
1037
1038 return err;
1039 }
1040
1041 /* ---- Interface to HCI drivers ---- */
1042
1043 static int hci_rfkill_set_block(void *data, bool blocked)
1044 {
1045 struct hci_dev *hdev = data;
1046
1047 BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked);
1048
1049 if (!blocked)
1050 return 0;
1051
1052 hci_dev_do_close(hdev);
1053
1054 return 0;
1055 }
1056
1057 static const struct rfkill_ops hci_rfkill_ops = {
1058 .set_block = hci_rfkill_set_block,
1059 };
1060
1061 /* Alloc HCI device */
1062 struct hci_dev *hci_alloc_dev(void)
1063 {
1064 struct hci_dev *hdev;
1065
1066 hdev = kzalloc(sizeof(struct hci_dev), GFP_KERNEL);
1067 if (!hdev)
1068 return NULL;
1069
1070 hci_init_sysfs(hdev);
1071 skb_queue_head_init(&hdev->driver_init);
1072
1073 return hdev;
1074 }
1075 EXPORT_SYMBOL(hci_alloc_dev);
1076
1077 /* Free HCI device */
1078 void hci_free_dev(struct hci_dev *hdev)
1079 {
1080 skb_queue_purge(&hdev->driver_init);
1081
1082 /* will free via device release */
1083 put_device(&hdev->dev);
1084 }
1085 EXPORT_SYMBOL(hci_free_dev);
1086
1087 static void hci_power_on(struct work_struct *work)
1088 {
1089 struct hci_dev *hdev = container_of(work, struct hci_dev, power_on);
1090
1091 BT_DBG("%s", hdev->name);
1092
1093 if (hci_dev_open(hdev->id) < 0)
1094 return;
1095
1096 if (test_bit(HCI_AUTO_OFF, &hdev->dev_flags))
1097 schedule_delayed_work(&hdev->power_off,
1098 msecs_to_jiffies(AUTO_OFF_TIMEOUT));
1099
1100 if (test_and_clear_bit(HCI_SETUP, &hdev->dev_flags))
1101 mgmt_index_added(hdev);
1102 }
1103
1104 static void hci_power_off(struct work_struct *work)
1105 {
1106 struct hci_dev *hdev = container_of(work, struct hci_dev,
1107 power_off.work);
1108
1109 BT_DBG("%s", hdev->name);
1110
1111 clear_bit(HCI_AUTO_OFF, &hdev->dev_flags);
1112
1113 hci_dev_close(hdev->id);
1114 }
1115
1116 static void hci_discov_off(struct work_struct *work)
1117 {
1118 struct hci_dev *hdev;
1119 u8 scan = SCAN_PAGE;
1120
1121 hdev = container_of(work, struct hci_dev, discov_off.work);
1122
1123 BT_DBG("%s", hdev->name);
1124
1125 hci_dev_lock(hdev);
1126
1127 hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan);
1128
1129 hdev->discov_timeout = 0;
1130
1131 hci_dev_unlock(hdev);
1132 }
1133
1134 int hci_uuids_clear(struct hci_dev *hdev)
1135 {
1136 struct list_head *p, *n;
1137
1138 list_for_each_safe(p, n, &hdev->uuids) {
1139 struct bt_uuid *uuid;
1140
1141 uuid = list_entry(p, struct bt_uuid, list);
1142
1143 list_del(p);
1144 kfree(uuid);
1145 }
1146
1147 return 0;
1148 }
1149
1150 int hci_link_keys_clear(struct hci_dev *hdev)
1151 {
1152 struct list_head *p, *n;
1153
1154 list_for_each_safe(p, n, &hdev->link_keys) {
1155 struct link_key *key;
1156
1157 key = list_entry(p, struct link_key, list);
1158
1159 list_del(p);
1160 kfree(key);
1161 }
1162
1163 return 0;
1164 }
1165
1166 int hci_smp_ltks_clear(struct hci_dev *hdev)
1167 {
1168 struct smp_ltk *k, *tmp;
1169
1170 list_for_each_entry_safe(k, tmp, &hdev->long_term_keys, list) {
1171 list_del(&k->list);
1172 kfree(k);
1173 }
1174
1175 return 0;
1176 }
1177
1178 struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr)
1179 {
1180 struct link_key *k;
1181
1182 list_for_each_entry(k, &hdev->link_keys, list)
1183 if (bacmp(bdaddr, &k->bdaddr) == 0)
1184 return k;
1185
1186 return NULL;
1187 }
1188
1189 static int hci_persistent_key(struct hci_dev *hdev, struct hci_conn *conn,
1190 u8 key_type, u8 old_key_type)
1191 {
1192 /* Legacy key */
1193 if (key_type < 0x03)
1194 return 1;
1195
1196 /* Debug keys are insecure so don't store them persistently */
1197 if (key_type == HCI_LK_DEBUG_COMBINATION)
1198 return 0;
1199
1200 /* Changed combination key and there's no previous one */
1201 if (key_type == HCI_LK_CHANGED_COMBINATION && old_key_type == 0xff)
1202 return 0;
1203
1204 /* Security mode 3 case */
1205 if (!conn)
1206 return 1;
1207
1208 /* Neither local nor remote side had no-bonding as requirement */
1209 if (conn->auth_type > 0x01 && conn->remote_auth > 0x01)
1210 return 1;
1211
1212 /* Local side had dedicated bonding as requirement */
1213 if (conn->auth_type == 0x02 || conn->auth_type == 0x03)
1214 return 1;
1215
1216 /* Remote side had dedicated bonding as requirement */
1217 if (conn->remote_auth == 0x02 || conn->remote_auth == 0x03)
1218 return 1;
1219
1220 /* If none of the above criteria match, then don't store the key
1221 * persistently */
1222 return 0;
1223 }
1224
1225 struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, u8 rand[8])
1226 {
1227 struct smp_ltk *k;
1228
1229 list_for_each_entry(k, &hdev->long_term_keys, list) {
1230 if (k->ediv != ediv ||
1231 memcmp(rand, k->rand, sizeof(k->rand)))
1232 continue;
1233
1234 return k;
1235 }
1236
1237 return NULL;
1238 }
1239 EXPORT_SYMBOL(hci_find_ltk);
1240
1241 struct smp_ltk *hci_find_ltk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr,
1242 u8 addr_type)
1243 {
1244 struct smp_ltk *k;
1245
1246 list_for_each_entry(k, &hdev->long_term_keys, list)
1247 if (addr_type == k->bdaddr_type &&
1248 bacmp(bdaddr, &k->bdaddr) == 0)
1249 return k;
1250
1251 return NULL;
1252 }
1253 EXPORT_SYMBOL(hci_find_ltk_by_addr);
1254
1255 int hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, int new_key,
1256 bdaddr_t *bdaddr, u8 *val, u8 type, u8 pin_len)
1257 {
1258 struct link_key *key, *old_key;
1259 u8 old_key_type, persistent;
1260
1261 old_key = hci_find_link_key(hdev, bdaddr);
1262 if (old_key) {
1263 old_key_type = old_key->type;
1264 key = old_key;
1265 } else {
1266 old_key_type = conn ? conn->key_type : 0xff;
1267 key = kzalloc(sizeof(*key), GFP_ATOMIC);
1268 if (!key)
1269 return -ENOMEM;
1270 list_add(&key->list, &hdev->link_keys);
1271 }
1272
1273 BT_DBG("%s key for %s type %u", hdev->name, batostr(bdaddr), type);
1274
1275 /* Some buggy controller combinations generate a changed
1276 * combination key for legacy pairing even when there's no
1277 * previous key */
1278 if (type == HCI_LK_CHANGED_COMBINATION &&
1279 (!conn || conn->remote_auth == 0xff) &&
1280 old_key_type == 0xff) {
1281 type = HCI_LK_COMBINATION;
1282 if (conn)
1283 conn->key_type = type;
1284 }
1285
1286 bacpy(&key->bdaddr, bdaddr);
1287 memcpy(key->val, val, 16);
1288 key->pin_len = pin_len;
1289
1290 if (type == HCI_LK_CHANGED_COMBINATION)
1291 key->type = old_key_type;
1292 else
1293 key->type = type;
1294
1295 if (!new_key)
1296 return 0;
1297
1298 persistent = hci_persistent_key(hdev, conn, type, old_key_type);
1299
1300 mgmt_new_link_key(hdev, key, persistent);
1301
1302 if (!persistent) {
1303 list_del(&key->list);
1304 kfree(key);
1305 }
1306
1307 return 0;
1308 }
1309
1310 int hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type, u8 type,
1311 int new_key, u8 authenticated, u8 tk[16],
1312 u8 enc_size, u16 ediv, u8 rand[8])
1313 {
1314 struct smp_ltk *key, *old_key;
1315
1316 if (!(type & HCI_SMP_STK) && !(type & HCI_SMP_LTK))
1317 return 0;
1318
1319 old_key = hci_find_ltk_by_addr(hdev, bdaddr, addr_type);
1320 if (old_key)
1321 key = old_key;
1322 else {
1323 key = kzalloc(sizeof(*key), GFP_ATOMIC);
1324 if (!key)
1325 return -ENOMEM;
1326 list_add(&key->list, &hdev->long_term_keys);
1327 }
1328
1329 bacpy(&key->bdaddr, bdaddr);
1330 key->bdaddr_type = addr_type;
1331 memcpy(key->val, tk, sizeof(key->val));
1332 key->authenticated = authenticated;
1333 key->ediv = ediv;
1334 key->enc_size = enc_size;
1335 key->type = type;
1336 memcpy(key->rand, rand, sizeof(key->rand));
1337
1338 if (!new_key)
1339 return 0;
1340
1341 return 0;
1342 }
1343
1344 int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr)
1345 {
1346 struct link_key *key;
1347
1348 key = hci_find_link_key(hdev, bdaddr);
1349 if (!key)
1350 return -ENOENT;
1351
1352 BT_DBG("%s removing %s", hdev->name, batostr(bdaddr));
1353
1354 list_del(&key->list);
1355 kfree(key);
1356
1357 return 0;
1358 }
1359
1360 int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr)
1361 {
1362 struct smp_ltk *k, *tmp;
1363
1364 list_for_each_entry_safe(k, tmp, &hdev->long_term_keys, list) {
1365 if (bacmp(bdaddr, &k->bdaddr))
1366 continue;
1367
1368 BT_DBG("%s removing %s", hdev->name, batostr(bdaddr));
1369
1370 list_del(&k->list);
1371 kfree(k);
1372 }
1373
1374 return 0;
1375 }
1376
1377 /* HCI command timer function */
1378 static void hci_cmd_timer(unsigned long arg)
1379 {
1380 struct hci_dev *hdev = (void *) arg;
1381
1382 BT_ERR("%s command tx timeout", hdev->name);
1383 atomic_set(&hdev->cmd_cnt, 1);
1384 queue_work(hdev->workqueue, &hdev->cmd_work);
1385 }
1386
1387 struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev,
1388 bdaddr_t *bdaddr)
1389 {
1390 struct oob_data *data;
1391
1392 list_for_each_entry(data, &hdev->remote_oob_data, list)
1393 if (bacmp(bdaddr, &data->bdaddr) == 0)
1394 return data;
1395
1396 return NULL;
1397 }
1398
1399 int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr)
1400 {
1401 struct oob_data *data;
1402
1403 data = hci_find_remote_oob_data(hdev, bdaddr);
1404 if (!data)
1405 return -ENOENT;
1406
1407 BT_DBG("%s removing %s", hdev->name, batostr(bdaddr));
1408
1409 list_del(&data->list);
1410 kfree(data);
1411
1412 return 0;
1413 }
1414
1415 int hci_remote_oob_data_clear(struct hci_dev *hdev)
1416 {
1417 struct oob_data *data, *n;
1418
1419 list_for_each_entry_safe(data, n, &hdev->remote_oob_data, list) {
1420 list_del(&data->list);
1421 kfree(data);
1422 }
1423
1424 return 0;
1425 }
1426
1427 int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 *hash,
1428 u8 *randomizer)
1429 {
1430 struct oob_data *data;
1431
1432 data = hci_find_remote_oob_data(hdev, bdaddr);
1433
1434 if (!data) {
1435 data = kmalloc(sizeof(*data), GFP_ATOMIC);
1436 if (!data)
1437 return -ENOMEM;
1438
1439 bacpy(&data->bdaddr, bdaddr);
1440 list_add(&data->list, &hdev->remote_oob_data);
1441 }
1442
1443 memcpy(data->hash, hash, sizeof(data->hash));
1444 memcpy(data->randomizer, randomizer, sizeof(data->randomizer));
1445
1446 BT_DBG("%s for %s", hdev->name, batostr(bdaddr));
1447
1448 return 0;
1449 }
1450
1451 struct bdaddr_list *hci_blacklist_lookup(struct hci_dev *hdev,
1452 bdaddr_t *bdaddr)
1453 {
1454 struct bdaddr_list *b;
1455
1456 list_for_each_entry(b, &hdev->blacklist, list)
1457 if (bacmp(bdaddr, &b->bdaddr) == 0)
1458 return b;
1459
1460 return NULL;
1461 }
1462
1463 int hci_blacklist_clear(struct hci_dev *hdev)
1464 {
1465 struct list_head *p, *n;
1466
1467 list_for_each_safe(p, n, &hdev->blacklist) {
1468 struct bdaddr_list *b;
1469
1470 b = list_entry(p, struct bdaddr_list, list);
1471
1472 list_del(p);
1473 kfree(b);
1474 }
1475
1476 return 0;
1477 }
1478
1479 int hci_blacklist_add(struct hci_dev *hdev, bdaddr_t *bdaddr)
1480 {
1481 struct bdaddr_list *entry;
1482
1483 if (bacmp(bdaddr, BDADDR_ANY) == 0)
1484 return -EBADF;
1485
1486 if (hci_blacklist_lookup(hdev, bdaddr))
1487 return -EEXIST;
1488
1489 entry = kzalloc(sizeof(struct bdaddr_list), GFP_KERNEL);
1490 if (!entry)
1491 return -ENOMEM;
1492
1493 bacpy(&entry->bdaddr, bdaddr);
1494
1495 list_add(&entry->list, &hdev->blacklist);
1496
1497 return mgmt_device_blocked(hdev, bdaddr);
1498 }
1499
1500 int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr)
1501 {
1502 struct bdaddr_list *entry;
1503
1504 if (bacmp(bdaddr, BDADDR_ANY) == 0)
1505 return hci_blacklist_clear(hdev);
1506
1507 entry = hci_blacklist_lookup(hdev, bdaddr);
1508 if (!entry)
1509 return -ENOENT;
1510
1511 list_del(&entry->list);
1512 kfree(entry);
1513
1514 return mgmt_device_unblocked(hdev, bdaddr);
1515 }
1516
1517 static void hci_clear_adv_cache(struct work_struct *work)
1518 {
1519 struct hci_dev *hdev = container_of(work, struct hci_dev,
1520 adv_work.work);
1521
1522 hci_dev_lock(hdev);
1523
1524 hci_adv_entries_clear(hdev);
1525
1526 hci_dev_unlock(hdev);
1527 }
1528
1529 int hci_adv_entries_clear(struct hci_dev *hdev)
1530 {
1531 struct adv_entry *entry, *tmp;
1532
1533 list_for_each_entry_safe(entry, tmp, &hdev->adv_entries, list) {
1534 list_del(&entry->list);
1535 kfree(entry);
1536 }
1537
1538 BT_DBG("%s adv cache cleared", hdev->name);
1539
1540 return 0;
1541 }
1542
1543 struct adv_entry *hci_find_adv_entry(struct hci_dev *hdev, bdaddr_t *bdaddr)
1544 {
1545 struct adv_entry *entry;
1546
1547 list_for_each_entry(entry, &hdev->adv_entries, list)
1548 if (bacmp(bdaddr, &entry->bdaddr) == 0)
1549 return entry;
1550
1551 return NULL;
1552 }
1553
1554 static inline int is_connectable_adv(u8 evt_type)
1555 {
1556 if (evt_type == ADV_IND || evt_type == ADV_DIRECT_IND)
1557 return 1;
1558
1559 return 0;
1560 }
1561
1562 int hci_add_adv_entry(struct hci_dev *hdev,
1563 struct hci_ev_le_advertising_info *ev)
1564 {
1565 struct adv_entry *entry;
1566
1567 if (!is_connectable_adv(ev->evt_type))
1568 return -EINVAL;
1569
1570 /* Only new entries should be added to adv_entries. So, if
1571 * bdaddr was found, don't add it. */
1572 if (hci_find_adv_entry(hdev, &ev->bdaddr))
1573 return 0;
1574
1575 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1576 if (!entry)
1577 return -ENOMEM;
1578
1579 bacpy(&entry->bdaddr, &ev->bdaddr);
1580 entry->bdaddr_type = ev->bdaddr_type;
1581
1582 list_add(&entry->list, &hdev->adv_entries);
1583
1584 BT_DBG("%s adv entry added: address %s type %u", hdev->name,
1585 batostr(&entry->bdaddr), entry->bdaddr_type);
1586
1587 return 0;
1588 }
1589
1590 /* Register HCI device */
1591 int hci_register_dev(struct hci_dev *hdev)
1592 {
1593 struct list_head *head = &hci_dev_list, *p;
1594 int i, id, error;
1595
1596 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
1597
1598 if (!hdev->open || !hdev->close)
1599 return -EINVAL;
1600
1601 /* Do not allow HCI_AMP devices to register at index 0,
1602 * so the index can be used as the AMP controller ID.
1603 */
1604 id = (hdev->dev_type == HCI_BREDR) ? 0 : 1;
1605
1606 write_lock(&hci_dev_list_lock);
1607
1608 /* Find first available device id */
1609 list_for_each(p, &hci_dev_list) {
1610 if (list_entry(p, struct hci_dev, list)->id != id)
1611 break;
1612 head = p; id++;
1613 }
1614
1615 sprintf(hdev->name, "hci%d", id);
1616 hdev->id = id;
1617 list_add_tail(&hdev->list, head);
1618
1619 mutex_init(&hdev->lock);
1620
1621 hdev->flags = 0;
1622 hdev->dev_flags = 0;
1623 hdev->pkt_type = (HCI_DM1 | HCI_DH1 | HCI_HV1);
1624 hdev->esco_type = (ESCO_HV1);
1625 hdev->link_mode = (HCI_LM_ACCEPT);
1626 hdev->io_capability = 0x03; /* No Input No Output */
1627
1628 hdev->idle_timeout = 0;
1629 hdev->sniff_max_interval = 800;
1630 hdev->sniff_min_interval = 80;
1631
1632 INIT_WORK(&hdev->rx_work, hci_rx_work);
1633 INIT_WORK(&hdev->cmd_work, hci_cmd_work);
1634 INIT_WORK(&hdev->tx_work, hci_tx_work);
1635
1636
1637 skb_queue_head_init(&hdev->rx_q);
1638 skb_queue_head_init(&hdev->cmd_q);
1639 skb_queue_head_init(&hdev->raw_q);
1640
1641 setup_timer(&hdev->cmd_timer, hci_cmd_timer, (unsigned long) hdev);
1642
1643 for (i = 0; i < NUM_REASSEMBLY; i++)
1644 hdev->reassembly[i] = NULL;
1645
1646 init_waitqueue_head(&hdev->req_wait_q);
1647 mutex_init(&hdev->req_lock);
1648
1649 discovery_init(hdev);
1650
1651 hci_conn_hash_init(hdev);
1652
1653 INIT_LIST_HEAD(&hdev->mgmt_pending);
1654
1655 INIT_LIST_HEAD(&hdev->blacklist);
1656
1657 INIT_LIST_HEAD(&hdev->uuids);
1658
1659 INIT_LIST_HEAD(&hdev->link_keys);
1660 INIT_LIST_HEAD(&hdev->long_term_keys);
1661
1662 INIT_LIST_HEAD(&hdev->remote_oob_data);
1663
1664 INIT_LIST_HEAD(&hdev->adv_entries);
1665
1666 INIT_DELAYED_WORK(&hdev->adv_work, hci_clear_adv_cache);
1667 INIT_WORK(&hdev->power_on, hci_power_on);
1668 INIT_DELAYED_WORK(&hdev->power_off, hci_power_off);
1669
1670 INIT_DELAYED_WORK(&hdev->discov_off, hci_discov_off);
1671
1672 memset(&hdev->stat, 0, sizeof(struct hci_dev_stats));
1673
1674 atomic_set(&hdev->promisc, 0);
1675
1676 write_unlock(&hci_dev_list_lock);
1677
1678 hdev->workqueue = alloc_workqueue(hdev->name, WQ_HIGHPRI | WQ_UNBOUND |
1679 WQ_MEM_RECLAIM, 1);
1680 if (!hdev->workqueue) {
1681 error = -ENOMEM;
1682 goto err;
1683 }
1684
1685 error = hci_add_sysfs(hdev);
1686 if (error < 0)
1687 goto err_wqueue;
1688
1689 hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev,
1690 RFKILL_TYPE_BLUETOOTH, &hci_rfkill_ops, hdev);
1691 if (hdev->rfkill) {
1692 if (rfkill_register(hdev->rfkill) < 0) {
1693 rfkill_destroy(hdev->rfkill);
1694 hdev->rfkill = NULL;
1695 }
1696 }
1697
1698 set_bit(HCI_AUTO_OFF, &hdev->dev_flags);
1699 set_bit(HCI_SETUP, &hdev->dev_flags);
1700 schedule_work(&hdev->power_on);
1701
1702 hci_notify(hdev, HCI_DEV_REG);
1703 hci_dev_hold(hdev);
1704
1705 return id;
1706
1707 err_wqueue:
1708 destroy_workqueue(hdev->workqueue);
1709 err:
1710 write_lock(&hci_dev_list_lock);
1711 list_del(&hdev->list);
1712 write_unlock(&hci_dev_list_lock);
1713
1714 return error;
1715 }
1716 EXPORT_SYMBOL(hci_register_dev);
1717
1718 /* Unregister HCI device */
1719 void hci_unregister_dev(struct hci_dev *hdev)
1720 {
1721 int i;
1722
1723 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
1724
1725 write_lock(&hci_dev_list_lock);
1726 list_del(&hdev->list);
1727 write_unlock(&hci_dev_list_lock);
1728
1729 hci_dev_do_close(hdev);
1730
1731 for (i = 0; i < NUM_REASSEMBLY; i++)
1732 kfree_skb(hdev->reassembly[i]);
1733
1734 if (!test_bit(HCI_INIT, &hdev->flags) &&
1735 !test_bit(HCI_SETUP, &hdev->dev_flags)) {
1736 hci_dev_lock(hdev);
1737 mgmt_index_removed(hdev);
1738 hci_dev_unlock(hdev);
1739 }
1740
1741 /* mgmt_index_removed should take care of emptying the
1742 * pending list */
1743 BUG_ON(!list_empty(&hdev->mgmt_pending));
1744
1745 hci_notify(hdev, HCI_DEV_UNREG);
1746
1747 if (hdev->rfkill) {
1748 rfkill_unregister(hdev->rfkill);
1749 rfkill_destroy(hdev->rfkill);
1750 }
1751
1752 hci_del_sysfs(hdev);
1753
1754 cancel_delayed_work_sync(&hdev->adv_work);
1755
1756 destroy_workqueue(hdev->workqueue);
1757
1758 hci_dev_lock(hdev);
1759 hci_blacklist_clear(hdev);
1760 hci_uuids_clear(hdev);
1761 hci_link_keys_clear(hdev);
1762 hci_smp_ltks_clear(hdev);
1763 hci_remote_oob_data_clear(hdev);
1764 hci_adv_entries_clear(hdev);
1765 hci_dev_unlock(hdev);
1766
1767 hci_dev_put(hdev);
1768 }
1769 EXPORT_SYMBOL(hci_unregister_dev);
1770
1771 /* Suspend HCI device */
1772 int hci_suspend_dev(struct hci_dev *hdev)
1773 {
1774 hci_notify(hdev, HCI_DEV_SUSPEND);
1775 return 0;
1776 }
1777 EXPORT_SYMBOL(hci_suspend_dev);
1778
1779 /* Resume HCI device */
1780 int hci_resume_dev(struct hci_dev *hdev)
1781 {
1782 hci_notify(hdev, HCI_DEV_RESUME);
1783 return 0;
1784 }
1785 EXPORT_SYMBOL(hci_resume_dev);
1786
1787 /* Receive frame from HCI drivers */
1788 int hci_recv_frame(struct sk_buff *skb)
1789 {
1790 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
1791 if (!hdev || (!test_bit(HCI_UP, &hdev->flags)
1792 && !test_bit(HCI_INIT, &hdev->flags))) {
1793 kfree_skb(skb);
1794 return -ENXIO;
1795 }
1796
1797 /* Incomming skb */
1798 bt_cb(skb)->incoming = 1;
1799
1800 /* Time stamp */
1801 __net_timestamp(skb);
1802
1803 skb_queue_tail(&hdev->rx_q, skb);
1804 queue_work(hdev->workqueue, &hdev->rx_work);
1805
1806 return 0;
1807 }
1808 EXPORT_SYMBOL(hci_recv_frame);
1809
1810 static int hci_reassembly(struct hci_dev *hdev, int type, void *data,
1811 int count, __u8 index)
1812 {
1813 int len = 0;
1814 int hlen = 0;
1815 int remain = count;
1816 struct sk_buff *skb;
1817 struct bt_skb_cb *scb;
1818
1819 if ((type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) ||
1820 index >= NUM_REASSEMBLY)
1821 return -EILSEQ;
1822
1823 skb = hdev->reassembly[index];
1824
1825 if (!skb) {
1826 switch (type) {
1827 case HCI_ACLDATA_PKT:
1828 len = HCI_MAX_FRAME_SIZE;
1829 hlen = HCI_ACL_HDR_SIZE;
1830 break;
1831 case HCI_EVENT_PKT:
1832 len = HCI_MAX_EVENT_SIZE;
1833 hlen = HCI_EVENT_HDR_SIZE;
1834 break;
1835 case HCI_SCODATA_PKT:
1836 len = HCI_MAX_SCO_SIZE;
1837 hlen = HCI_SCO_HDR_SIZE;
1838 break;
1839 }
1840
1841 skb = bt_skb_alloc(len, GFP_ATOMIC);
1842 if (!skb)
1843 return -ENOMEM;
1844
1845 scb = (void *) skb->cb;
1846 scb->expect = hlen;
1847 scb->pkt_type = type;
1848
1849 skb->dev = (void *) hdev;
1850 hdev->reassembly[index] = skb;
1851 }
1852
1853 while (count) {
1854 scb = (void *) skb->cb;
1855 len = min(scb->expect, (__u16)count);
1856
1857 memcpy(skb_put(skb, len), data, len);
1858
1859 count -= len;
1860 data += len;
1861 scb->expect -= len;
1862 remain = count;
1863
1864 switch (type) {
1865 case HCI_EVENT_PKT:
1866 if (skb->len == HCI_EVENT_HDR_SIZE) {
1867 struct hci_event_hdr *h = hci_event_hdr(skb);
1868 scb->expect = h->plen;
1869
1870 if (skb_tailroom(skb) < scb->expect) {
1871 kfree_skb(skb);
1872 hdev->reassembly[index] = NULL;
1873 return -ENOMEM;
1874 }
1875 }
1876 break;
1877
1878 case HCI_ACLDATA_PKT:
1879 if (skb->len == HCI_ACL_HDR_SIZE) {
1880 struct hci_acl_hdr *h = hci_acl_hdr(skb);
1881 scb->expect = __le16_to_cpu(h->dlen);
1882
1883 if (skb_tailroom(skb) < scb->expect) {
1884 kfree_skb(skb);
1885 hdev->reassembly[index] = NULL;
1886 return -ENOMEM;
1887 }
1888 }
1889 break;
1890
1891 case HCI_SCODATA_PKT:
1892 if (skb->len == HCI_SCO_HDR_SIZE) {
1893 struct hci_sco_hdr *h = hci_sco_hdr(skb);
1894 scb->expect = h->dlen;
1895
1896 if (skb_tailroom(skb) < scb->expect) {
1897 kfree_skb(skb);
1898 hdev->reassembly[index] = NULL;
1899 return -ENOMEM;
1900 }
1901 }
1902 break;
1903 }
1904
1905 if (scb->expect == 0) {
1906 /* Complete frame */
1907
1908 bt_cb(skb)->pkt_type = type;
1909 hci_recv_frame(skb);
1910
1911 hdev->reassembly[index] = NULL;
1912 return remain;
1913 }
1914 }
1915
1916 return remain;
1917 }
1918
1919 int hci_recv_fragment(struct hci_dev *hdev, int type, void *data, int count)
1920 {
1921 int rem = 0;
1922
1923 if (type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT)
1924 return -EILSEQ;
1925
1926 while (count) {
1927 rem = hci_reassembly(hdev, type, data, count, type - 1);
1928 if (rem < 0)
1929 return rem;
1930
1931 data += (count - rem);
1932 count = rem;
1933 }
1934
1935 return rem;
1936 }
1937 EXPORT_SYMBOL(hci_recv_fragment);
1938
1939 #define STREAM_REASSEMBLY 0
1940
1941 int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count)
1942 {
1943 int type;
1944 int rem = 0;
1945
1946 while (count) {
1947 struct sk_buff *skb = hdev->reassembly[STREAM_REASSEMBLY];
1948
1949 if (!skb) {
1950 struct { char type; } *pkt;
1951
1952 /* Start of the frame */
1953 pkt = data;
1954 type = pkt->type;
1955
1956 data++;
1957 count--;
1958 } else
1959 type = bt_cb(skb)->pkt_type;
1960
1961 rem = hci_reassembly(hdev, type, data, count,
1962 STREAM_REASSEMBLY);
1963 if (rem < 0)
1964 return rem;
1965
1966 data += (count - rem);
1967 count = rem;
1968 }
1969
1970 return rem;
1971 }
1972 EXPORT_SYMBOL(hci_recv_stream_fragment);
1973
1974 /* ---- Interface to upper protocols ---- */
1975
1976 int hci_register_cb(struct hci_cb *cb)
1977 {
1978 BT_DBG("%p name %s", cb, cb->name);
1979
1980 write_lock(&hci_cb_list_lock);
1981 list_add(&cb->list, &hci_cb_list);
1982 write_unlock(&hci_cb_list_lock);
1983
1984 return 0;
1985 }
1986 EXPORT_SYMBOL(hci_register_cb);
1987
1988 int hci_unregister_cb(struct hci_cb *cb)
1989 {
1990 BT_DBG("%p name %s", cb, cb->name);
1991
1992 write_lock(&hci_cb_list_lock);
1993 list_del(&cb->list);
1994 write_unlock(&hci_cb_list_lock);
1995
1996 return 0;
1997 }
1998 EXPORT_SYMBOL(hci_unregister_cb);
1999
2000 static int hci_send_frame(struct sk_buff *skb)
2001 {
2002 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
2003
2004 if (!hdev) {
2005 kfree_skb(skb);
2006 return -ENODEV;
2007 }
2008
2009 BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
2010
2011 if (atomic_read(&hdev->promisc)) {
2012 /* Time stamp */
2013 __net_timestamp(skb);
2014
2015 hci_send_to_sock(hdev, skb, NULL);
2016 }
2017
2018 /* Get rid of skb owner, prior to sending to the driver. */
2019 skb_orphan(skb);
2020
2021 return hdev->send(skb);
2022 }
2023
2024 /* Send HCI command */
2025 int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, void *param)
2026 {
2027 int len = HCI_COMMAND_HDR_SIZE + plen;
2028 struct hci_command_hdr *hdr;
2029 struct sk_buff *skb;
2030
2031 BT_DBG("%s opcode 0x%x plen %d", hdev->name, opcode, plen);
2032
2033 skb = bt_skb_alloc(len, GFP_ATOMIC);
2034 if (!skb) {
2035 BT_ERR("%s no memory for command", hdev->name);
2036 return -ENOMEM;
2037 }
2038
2039 hdr = (struct hci_command_hdr *) skb_put(skb, HCI_COMMAND_HDR_SIZE);
2040 hdr->opcode = cpu_to_le16(opcode);
2041 hdr->plen = plen;
2042
2043 if (plen)
2044 memcpy(skb_put(skb, plen), param, plen);
2045
2046 BT_DBG("skb len %d", skb->len);
2047
2048 bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
2049 skb->dev = (void *) hdev;
2050
2051 if (test_bit(HCI_INIT, &hdev->flags))
2052 hdev->init_last_cmd = opcode;
2053
2054 skb_queue_tail(&hdev->cmd_q, skb);
2055 queue_work(hdev->workqueue, &hdev->cmd_work);
2056
2057 return 0;
2058 }
2059
2060 /* Get data from the previously sent command */
2061 void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode)
2062 {
2063 struct hci_command_hdr *hdr;
2064
2065 if (!hdev->sent_cmd)
2066 return NULL;
2067
2068 hdr = (void *) hdev->sent_cmd->data;
2069
2070 if (hdr->opcode != cpu_to_le16(opcode))
2071 return NULL;
2072
2073 BT_DBG("%s opcode 0x%x", hdev->name, opcode);
2074
2075 return hdev->sent_cmd->data + HCI_COMMAND_HDR_SIZE;
2076 }
2077
2078 /* Send ACL data */
2079 static void hci_add_acl_hdr(struct sk_buff *skb, __u16 handle, __u16 flags)
2080 {
2081 struct hci_acl_hdr *hdr;
2082 int len = skb->len;
2083
2084 skb_push(skb, HCI_ACL_HDR_SIZE);
2085 skb_reset_transport_header(skb);
2086 hdr = (struct hci_acl_hdr *)skb_transport_header(skb);
2087 hdr->handle = cpu_to_le16(hci_handle_pack(handle, flags));
2088 hdr->dlen = cpu_to_le16(len);
2089 }
2090
2091 static void hci_queue_acl(struct hci_conn *conn, struct sk_buff_head *queue,
2092 struct sk_buff *skb, __u16 flags)
2093 {
2094 struct hci_dev *hdev = conn->hdev;
2095 struct sk_buff *list;
2096
2097 list = skb_shinfo(skb)->frag_list;
2098 if (!list) {
2099 /* Non fragmented */
2100 BT_DBG("%s nonfrag skb %p len %d", hdev->name, skb, skb->len);
2101
2102 skb_queue_tail(queue, skb);
2103 } else {
2104 /* Fragmented */
2105 BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
2106
2107 skb_shinfo(skb)->frag_list = NULL;
2108
2109 /* Queue all fragments atomically */
2110 spin_lock(&queue->lock);
2111
2112 __skb_queue_tail(queue, skb);
2113
2114 flags &= ~ACL_START;
2115 flags |= ACL_CONT;
2116 do {
2117 skb = list; list = list->next;
2118
2119 skb->dev = (void *) hdev;
2120 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
2121 hci_add_acl_hdr(skb, conn->handle, flags);
2122
2123 BT_DBG("%s frag %p len %d", hdev->name, skb, skb->len);
2124
2125 __skb_queue_tail(queue, skb);
2126 } while (list);
2127
2128 spin_unlock(&queue->lock);
2129 }
2130 }
2131
2132 void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags)
2133 {
2134 struct hci_conn *conn = chan->conn;
2135 struct hci_dev *hdev = conn->hdev;
2136
2137 BT_DBG("%s chan %p flags 0x%x", hdev->name, chan, flags);
2138
2139 skb->dev = (void *) hdev;
2140 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
2141 hci_add_acl_hdr(skb, conn->handle, flags);
2142
2143 hci_queue_acl(conn, &chan->data_q, skb, flags);
2144
2145 queue_work(hdev->workqueue, &hdev->tx_work);
2146 }
2147 EXPORT_SYMBOL(hci_send_acl);
2148
2149 /* Send SCO data */
2150 void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb)
2151 {
2152 struct hci_dev *hdev = conn->hdev;
2153 struct hci_sco_hdr hdr;
2154
2155 BT_DBG("%s len %d", hdev->name, skb->len);
2156
2157 hdr.handle = cpu_to_le16(conn->handle);
2158 hdr.dlen = skb->len;
2159
2160 skb_push(skb, HCI_SCO_HDR_SIZE);
2161 skb_reset_transport_header(skb);
2162 memcpy(skb_transport_header(skb), &hdr, HCI_SCO_HDR_SIZE);
2163
2164 skb->dev = (void *) hdev;
2165 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
2166
2167 skb_queue_tail(&conn->data_q, skb);
2168 queue_work(hdev->workqueue, &hdev->tx_work);
2169 }
2170 EXPORT_SYMBOL(hci_send_sco);
2171
2172 /* ---- HCI TX task (outgoing data) ---- */
2173
2174 /* HCI Connection scheduler */
2175 static inline struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type, int *quote)
2176 {
2177 struct hci_conn_hash *h = &hdev->conn_hash;
2178 struct hci_conn *conn = NULL, *c;
2179 int num = 0, min = ~0;
2180
2181 /* We don't have to lock device here. Connections are always
2182 * added and removed with TX task disabled. */
2183
2184 rcu_read_lock();
2185
2186 list_for_each_entry_rcu(c, &h->list, list) {
2187 if (c->type != type || skb_queue_empty(&c->data_q))
2188 continue;
2189
2190 if (c->state != BT_CONNECTED && c->state != BT_CONFIG)
2191 continue;
2192
2193 num++;
2194
2195 if (c->sent < min) {
2196 min = c->sent;
2197 conn = c;
2198 }
2199
2200 if (hci_conn_num(hdev, type) == num)
2201 break;
2202 }
2203
2204 rcu_read_unlock();
2205
2206 if (conn) {
2207 int cnt, q;
2208
2209 switch (conn->type) {
2210 case ACL_LINK:
2211 cnt = hdev->acl_cnt;
2212 break;
2213 case SCO_LINK:
2214 case ESCO_LINK:
2215 cnt = hdev->sco_cnt;
2216 break;
2217 case LE_LINK:
2218 cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt;
2219 break;
2220 default:
2221 cnt = 0;
2222 BT_ERR("Unknown link type");
2223 }
2224
2225 q = cnt / num;
2226 *quote = q ? q : 1;
2227 } else
2228 *quote = 0;
2229
2230 BT_DBG("conn %p quote %d", conn, *quote);
2231 return conn;
2232 }
2233
2234 static inline void hci_link_tx_to(struct hci_dev *hdev, __u8 type)
2235 {
2236 struct hci_conn_hash *h = &hdev->conn_hash;
2237 struct hci_conn *c;
2238
2239 BT_ERR("%s link tx timeout", hdev->name);
2240
2241 rcu_read_lock();
2242
2243 /* Kill stalled connections */
2244 list_for_each_entry_rcu(c, &h->list, list) {
2245 if (c->type == type && c->sent) {
2246 BT_ERR("%s killing stalled connection %s",
2247 hdev->name, batostr(&c->dst));
2248 hci_acl_disconn(c, 0x13);
2249 }
2250 }
2251
2252 rcu_read_unlock();
2253 }
2254
2255 static inline struct hci_chan *hci_chan_sent(struct hci_dev *hdev, __u8 type,
2256 int *quote)
2257 {
2258 struct hci_conn_hash *h = &hdev->conn_hash;
2259 struct hci_chan *chan = NULL;
2260 int num = 0, min = ~0, cur_prio = 0;
2261 struct hci_conn *conn;
2262 int cnt, q, conn_num = 0;
2263
2264 BT_DBG("%s", hdev->name);
2265
2266 rcu_read_lock();
2267
2268 list_for_each_entry_rcu(conn, &h->list, list) {
2269 struct hci_chan *tmp;
2270
2271 if (conn->type != type)
2272 continue;
2273
2274 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
2275 continue;
2276
2277 conn_num++;
2278
2279 list_for_each_entry_rcu(tmp, &conn->chan_list, list) {
2280 struct sk_buff *skb;
2281
2282 if (skb_queue_empty(&tmp->data_q))
2283 continue;
2284
2285 skb = skb_peek(&tmp->data_q);
2286 if (skb->priority < cur_prio)
2287 continue;
2288
2289 if (skb->priority > cur_prio) {
2290 num = 0;
2291 min = ~0;
2292 cur_prio = skb->priority;
2293 }
2294
2295 num++;
2296
2297 if (conn->sent < min) {
2298 min = conn->sent;
2299 chan = tmp;
2300 }
2301 }
2302
2303 if (hci_conn_num(hdev, type) == conn_num)
2304 break;
2305 }
2306
2307 rcu_read_unlock();
2308
2309 if (!chan)
2310 return NULL;
2311
2312 switch (chan->conn->type) {
2313 case ACL_LINK:
2314 cnt = hdev->acl_cnt;
2315 break;
2316 case SCO_LINK:
2317 case ESCO_LINK:
2318 cnt = hdev->sco_cnt;
2319 break;
2320 case LE_LINK:
2321 cnt = hdev->le_mtu ? hdev->le_cnt : hdev->acl_cnt;
2322 break;
2323 default:
2324 cnt = 0;
2325 BT_ERR("Unknown link type");
2326 }
2327
2328 q = cnt / num;
2329 *quote = q ? q : 1;
2330 BT_DBG("chan %p quote %d", chan, *quote);
2331 return chan;
2332 }
2333
2334 static void hci_prio_recalculate(struct hci_dev *hdev, __u8 type)
2335 {
2336 struct hci_conn_hash *h = &hdev->conn_hash;
2337 struct hci_conn *conn;
2338 int num = 0;
2339
2340 BT_DBG("%s", hdev->name);
2341
2342 rcu_read_lock();
2343
2344 list_for_each_entry_rcu(conn, &h->list, list) {
2345 struct hci_chan *chan;
2346
2347 if (conn->type != type)
2348 continue;
2349
2350 if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
2351 continue;
2352
2353 num++;
2354
2355 list_for_each_entry_rcu(chan, &conn->chan_list, list) {
2356 struct sk_buff *skb;
2357
2358 if (chan->sent) {
2359 chan->sent = 0;
2360 continue;
2361 }
2362
2363 if (skb_queue_empty(&chan->data_q))
2364 continue;
2365
2366 skb = skb_peek(&chan->data_q);
2367 if (skb->priority >= HCI_PRIO_MAX - 1)
2368 continue;
2369
2370 skb->priority = HCI_PRIO_MAX - 1;
2371
2372 BT_DBG("chan %p skb %p promoted to %d", chan, skb,
2373 skb->priority);
2374 }
2375
2376 if (hci_conn_num(hdev, type) == num)
2377 break;
2378 }
2379
2380 rcu_read_unlock();
2381
2382 }
2383
2384 static inline void hci_sched_acl(struct hci_dev *hdev)
2385 {
2386 struct hci_chan *chan;
2387 struct sk_buff *skb;
2388 int quote;
2389 unsigned int cnt;
2390
2391 BT_DBG("%s", hdev->name);
2392
2393 if (!hci_conn_num(hdev, ACL_LINK))
2394 return;
2395
2396 if (!test_bit(HCI_RAW, &hdev->flags)) {
2397 /* ACL tx timeout must be longer than maximum
2398 * link supervision timeout (40.9 seconds) */
2399 if (!hdev->acl_cnt && time_after(jiffies, hdev->acl_last_tx +
2400 msecs_to_jiffies(HCI_ACL_TX_TIMEOUT)))
2401 hci_link_tx_to(hdev, ACL_LINK);
2402 }
2403
2404 cnt = hdev->acl_cnt;
2405
2406 while (hdev->acl_cnt &&
2407 (chan = hci_chan_sent(hdev, ACL_LINK, &quote))) {
2408 u32 priority = (skb_peek(&chan->data_q))->priority;
2409 while (quote-- && (skb = skb_peek(&chan->data_q))) {
2410 BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
2411 skb->len, skb->priority);
2412
2413 /* Stop if priority has changed */
2414 if (skb->priority < priority)
2415 break;
2416
2417 skb = skb_dequeue(&chan->data_q);
2418
2419 hci_conn_enter_active_mode(chan->conn,
2420 bt_cb(skb)->force_active);
2421
2422 hci_send_frame(skb);
2423 hdev->acl_last_tx = jiffies;
2424
2425 hdev->acl_cnt--;
2426 chan->sent++;
2427 chan->conn->sent++;
2428 }
2429 }
2430
2431 if (cnt != hdev->acl_cnt)
2432 hci_prio_recalculate(hdev, ACL_LINK);
2433 }
2434
2435 /* Schedule SCO */
2436 static inline void hci_sched_sco(struct hci_dev *hdev)
2437 {
2438 struct hci_conn *conn;
2439 struct sk_buff *skb;
2440 int quote;
2441
2442 BT_DBG("%s", hdev->name);
2443
2444 if (!hci_conn_num(hdev, SCO_LINK))
2445 return;
2446
2447 while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, &quote))) {
2448 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
2449 BT_DBG("skb %p len %d", skb, skb->len);
2450 hci_send_frame(skb);
2451
2452 conn->sent++;
2453 if (conn->sent == ~0)
2454 conn->sent = 0;
2455 }
2456 }
2457 }
2458
2459 static inline void hci_sched_esco(struct hci_dev *hdev)
2460 {
2461 struct hci_conn *conn;
2462 struct sk_buff *skb;
2463 int quote;
2464
2465 BT_DBG("%s", hdev->name);
2466
2467 if (!hci_conn_num(hdev, ESCO_LINK))
2468 return;
2469
2470 while (hdev->sco_cnt && (conn = hci_low_sent(hdev, ESCO_LINK, &quote))) {
2471 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
2472 BT_DBG("skb %p len %d", skb, skb->len);
2473 hci_send_frame(skb);
2474
2475 conn->sent++;
2476 if (conn->sent == ~0)
2477 conn->sent = 0;
2478 }
2479 }
2480 }
2481
2482 static inline void hci_sched_le(struct hci_dev *hdev)
2483 {
2484 struct hci_chan *chan;
2485 struct sk_buff *skb;
2486 int quote, cnt, tmp;
2487
2488 BT_DBG("%s", hdev->name);
2489
2490 if (!hci_conn_num(hdev, LE_LINK))
2491 return;
2492
2493 if (!test_bit(HCI_RAW, &hdev->flags)) {
2494 /* LE tx timeout must be longer than maximum
2495 * link supervision timeout (40.9 seconds) */
2496 if (!hdev->le_cnt && hdev->le_pkts &&
2497 time_after(jiffies, hdev->le_last_tx + HZ * 45))
2498 hci_link_tx_to(hdev, LE_LINK);
2499 }
2500
2501 cnt = hdev->le_pkts ? hdev->le_cnt : hdev->acl_cnt;
2502 tmp = cnt;
2503 while (cnt && (chan = hci_chan_sent(hdev, LE_LINK, &quote))) {
2504 u32 priority = (skb_peek(&chan->data_q))->priority;
2505 while (quote-- && (skb = skb_peek(&chan->data_q))) {
2506 BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
2507 skb->len, skb->priority);
2508
2509 /* Stop if priority has changed */
2510 if (skb->priority < priority)
2511 break;
2512
2513 skb = skb_dequeue(&chan->data_q);
2514
2515 hci_send_frame(skb);
2516 hdev->le_last_tx = jiffies;
2517
2518 cnt--;
2519 chan->sent++;
2520 chan->conn->sent++;
2521 }
2522 }
2523
2524 if (hdev->le_pkts)
2525 hdev->le_cnt = cnt;
2526 else
2527 hdev->acl_cnt = cnt;
2528
2529 if (cnt != tmp)
2530 hci_prio_recalculate(hdev, LE_LINK);
2531 }
2532
2533 static void hci_tx_work(struct work_struct *work)
2534 {
2535 struct hci_dev *hdev = container_of(work, struct hci_dev, tx_work);
2536 struct sk_buff *skb;
2537
2538 BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt,
2539 hdev->sco_cnt, hdev->le_cnt);
2540
2541 /* Schedule queues and send stuff to HCI driver */
2542
2543 hci_sched_acl(hdev);
2544
2545 hci_sched_sco(hdev);
2546
2547 hci_sched_esco(hdev);
2548
2549 hci_sched_le(hdev);
2550
2551 /* Send next queued raw (unknown type) packet */
2552 while ((skb = skb_dequeue(&hdev->raw_q)))
2553 hci_send_frame(skb);
2554 }
2555
2556 /* ----- HCI RX task (incoming data processing) ----- */
2557
2558 /* ACL data packet */
2559 static inline void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb)
2560 {
2561 struct hci_acl_hdr *hdr = (void *) skb->data;
2562 struct hci_conn *conn;
2563 __u16 handle, flags;
2564
2565 skb_pull(skb, HCI_ACL_HDR_SIZE);
2566
2567 handle = __le16_to_cpu(hdr->handle);
2568 flags = hci_flags(handle);
2569 handle = hci_handle(handle);
2570
2571 BT_DBG("%s len %d handle 0x%x flags 0x%x", hdev->name, skb->len, handle, flags);
2572
2573 hdev->stat.acl_rx++;
2574
2575 hci_dev_lock(hdev);
2576 conn = hci_conn_hash_lookup_handle(hdev, handle);
2577 hci_dev_unlock(hdev);
2578
2579 if (conn) {
2580 hci_conn_enter_active_mode(conn, BT_POWER_FORCE_ACTIVE_OFF);
2581
2582 /* Send to upper protocol */
2583 l2cap_recv_acldata(conn, skb, flags);
2584 return;
2585 } else {
2586 BT_ERR("%s ACL packet for unknown connection handle %d",
2587 hdev->name, handle);
2588 }
2589
2590 kfree_skb(skb);
2591 }
2592
2593 /* SCO data packet */
2594 static inline void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb)
2595 {
2596 struct hci_sco_hdr *hdr = (void *) skb->data;
2597 struct hci_conn *conn;
2598 __u16 handle;
2599
2600 skb_pull(skb, HCI_SCO_HDR_SIZE);
2601
2602 handle = __le16_to_cpu(hdr->handle);
2603
2604 BT_DBG("%s len %d handle 0x%x", hdev->name, skb->len, handle);
2605
2606 hdev->stat.sco_rx++;
2607
2608 hci_dev_lock(hdev);
2609 conn = hci_conn_hash_lookup_handle(hdev, handle);
2610 hci_dev_unlock(hdev);
2611
2612 if (conn) {
2613 /* Send to upper protocol */
2614 sco_recv_scodata(conn, skb);
2615 return;
2616 } else {
2617 BT_ERR("%s SCO packet for unknown connection handle %d",
2618 hdev->name, handle);
2619 }
2620
2621 kfree_skb(skb);
2622 }
2623
2624 static void hci_rx_work(struct work_struct *work)
2625 {
2626 struct hci_dev *hdev = container_of(work, struct hci_dev, rx_work);
2627 struct sk_buff *skb;
2628
2629 BT_DBG("%s", hdev->name);
2630
2631 while ((skb = skb_dequeue(&hdev->rx_q))) {
2632 if (atomic_read(&hdev->promisc)) {
2633 /* Send copy to the sockets */
2634 hci_send_to_sock(hdev, skb, NULL);
2635 }
2636
2637 if (test_bit(HCI_RAW, &hdev->flags)) {
2638 kfree_skb(skb);
2639 continue;
2640 }
2641
2642 if (test_bit(HCI_INIT, &hdev->flags)) {
2643 /* Don't process data packets in this states. */
2644 switch (bt_cb(skb)->pkt_type) {
2645 case HCI_ACLDATA_PKT:
2646 case HCI_SCODATA_PKT:
2647 kfree_skb(skb);
2648 continue;
2649 }
2650 }
2651
2652 /* Process frame */
2653 switch (bt_cb(skb)->pkt_type) {
2654 case HCI_EVENT_PKT:
2655 BT_DBG("%s Event packet", hdev->name);
2656 hci_event_packet(hdev, skb);
2657 break;
2658
2659 case HCI_ACLDATA_PKT:
2660 BT_DBG("%s ACL data packet", hdev->name);
2661 hci_acldata_packet(hdev, skb);
2662 break;
2663
2664 case HCI_SCODATA_PKT:
2665 BT_DBG("%s SCO data packet", hdev->name);
2666 hci_scodata_packet(hdev, skb);
2667 break;
2668
2669 default:
2670 kfree_skb(skb);
2671 break;
2672 }
2673 }
2674 }
2675
2676 static void hci_cmd_work(struct work_struct *work)
2677 {
2678 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_work);
2679 struct sk_buff *skb;
2680
2681 BT_DBG("%s cmd %d", hdev->name, atomic_read(&hdev->cmd_cnt));
2682
2683 /* Send queued commands */
2684 if (atomic_read(&hdev->cmd_cnt)) {
2685 skb = skb_dequeue(&hdev->cmd_q);
2686 if (!skb)
2687 return;
2688
2689 kfree_skb(hdev->sent_cmd);
2690
2691 hdev->sent_cmd = skb_clone(skb, GFP_ATOMIC);
2692 if (hdev->sent_cmd) {
2693 atomic_dec(&hdev->cmd_cnt);
2694 hci_send_frame(skb);
2695 if (test_bit(HCI_RESET, &hdev->flags))
2696 del_timer(&hdev->cmd_timer);
2697 else
2698 mod_timer(&hdev->cmd_timer,
2699 jiffies + msecs_to_jiffies(HCI_CMD_TIMEOUT));
2700 } else {
2701 skb_queue_head(&hdev->cmd_q, skb);
2702 queue_work(hdev->workqueue, &hdev->cmd_work);
2703 }
2704 }
2705 }
2706
2707 int hci_do_inquiry(struct hci_dev *hdev, u8 length)
2708 {
2709 /* General inquiry access code (GIAC) */
2710 u8 lap[3] = { 0x33, 0x8b, 0x9e };
2711 struct hci_cp_inquiry cp;
2712
2713 BT_DBG("%s", hdev->name);
2714
2715 if (test_bit(HCI_INQUIRY, &hdev->flags))
2716 return -EINPROGRESS;
2717
2718 inquiry_cache_flush(hdev);
2719
2720 memset(&cp, 0, sizeof(cp));
2721 memcpy(&cp.lap, lap, sizeof(cp.lap));
2722 cp.length = length;
2723
2724 return hci_send_cmd(hdev, HCI_OP_INQUIRY, sizeof(cp), &cp);
2725 }
2726
2727 int hci_cancel_inquiry(struct hci_dev *hdev)
2728 {
2729 BT_DBG("%s", hdev->name);
2730
2731 if (!test_bit(HCI_INQUIRY, &hdev->flags))
2732 return -EPERM;
2733
2734 return hci_send_cmd(hdev, HCI_OP_INQUIRY_CANCEL, 0, NULL);
2735 }
2736
2737 module_param(enable_hs, bool, 0644);
2738 MODULE_PARM_DESC(enable_hs, "Enable High Speed");