aboutsummaryrefslogtreecommitdiffstats
path: root/net/bluetooth/hci_core.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/bluetooth/hci_core.c')
-rw-r--r--net/bluetooth/hci_core.c325
1 files changed, 152 insertions, 173 deletions
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index ce3727ecc0c..6d38d80195c 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -1,6 +1,7 @@
1/* 1/*
2 BlueZ - Bluetooth protocol stack for Linux 2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated 3 Copyright (C) 2000-2001 Qualcomm Incorporated
4 Copyright (C) 2011 ProFUSION Embedded Systems
4 5
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> 6 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6 7
@@ -56,11 +57,9 @@
56 57
57int enable_hs; 58int enable_hs;
58 59
59static void hci_cmd_task(unsigned long arg); 60static void hci_rx_work(struct work_struct *work);
60static void hci_rx_task(unsigned long arg); 61static void hci_cmd_work(struct work_struct *work);
61static void hci_tx_task(unsigned long arg); 62static void hci_tx_work(struct work_struct *work);
62
63static DEFINE_RWLOCK(hci_task_lock);
64 63
65/* HCI device list */ 64/* HCI device list */
66LIST_HEAD(hci_dev_list); 65LIST_HEAD(hci_dev_list);
@@ -70,10 +69,6 @@ DEFINE_RWLOCK(hci_dev_list_lock);
70LIST_HEAD(hci_cb_list); 69LIST_HEAD(hci_cb_list);
71DEFINE_RWLOCK(hci_cb_list_lock); 70DEFINE_RWLOCK(hci_cb_list_lock);
72 71
73/* HCI protocols */
74#define HCI_MAX_PROTO 2
75struct hci_proto *hci_proto[HCI_MAX_PROTO];
76
77/* HCI notifiers list */ 72/* HCI notifiers list */
78static ATOMIC_NOTIFIER_HEAD(hci_notifier); 73static ATOMIC_NOTIFIER_HEAD(hci_notifier);
79 74
@@ -192,33 +187,20 @@ static void hci_reset_req(struct hci_dev *hdev, unsigned long opt)
192 hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL); 187 hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL);
193} 188}
194 189
195static void hci_init_req(struct hci_dev *hdev, unsigned long opt) 190static void bredr_init(struct hci_dev *hdev)
196{ 191{
197 struct hci_cp_delete_stored_link_key cp; 192 struct hci_cp_delete_stored_link_key cp;
198 struct sk_buff *skb;
199 __le16 param; 193 __le16 param;
200 __u8 flt_type; 194 __u8 flt_type;
201 195
202 BT_DBG("%s %ld", hdev->name, opt); 196 hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_PACKET_BASED;
203
204 /* Driver initialization */
205
206 /* Special commands */
207 while ((skb = skb_dequeue(&hdev->driver_init))) {
208 bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
209 skb->dev = (void *) hdev;
210
211 skb_queue_tail(&hdev->cmd_q, skb);
212 tasklet_schedule(&hdev->cmd_task);
213 }
214 skb_queue_purge(&hdev->driver_init);
215 197
216 /* Mandatory initialization */ 198 /* Mandatory initialization */
217 199
218 /* Reset */ 200 /* Reset */
219 if (!test_bit(HCI_QUIRK_NO_RESET, &hdev->quirks)) { 201 if (!test_bit(HCI_QUIRK_NO_RESET, &hdev->quirks)) {
220 set_bit(HCI_RESET, &hdev->flags); 202 set_bit(HCI_RESET, &hdev->flags);
221 hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL); 203 hci_send_cmd(hdev, HCI_OP_RESET, 0, NULL);
222 } 204 }
223 205
224 /* Read Local Supported Features */ 206 /* Read Local Supported Features */
@@ -257,6 +239,51 @@ static void hci_init_req(struct hci_dev *hdev, unsigned long opt)
257 hci_send_cmd(hdev, HCI_OP_DELETE_STORED_LINK_KEY, sizeof(cp), &cp); 239 hci_send_cmd(hdev, HCI_OP_DELETE_STORED_LINK_KEY, sizeof(cp), &cp);
258} 240}
259 241
242static 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
253static 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
260static void hci_le_init_req(struct hci_dev *hdev, unsigned long opt) 287static void hci_le_init_req(struct hci_dev *hdev, unsigned long opt)
261{ 288{
262 BT_DBG("%s", hdev->name); 289 BT_DBG("%s", hdev->name);
@@ -433,14 +460,14 @@ int hci_inquiry(void __user *arg)
433 if (!hdev) 460 if (!hdev)
434 return -ENODEV; 461 return -ENODEV;
435 462
436 hci_dev_lock_bh(hdev); 463 hci_dev_lock(hdev);
437 if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX || 464 if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX ||
438 inquiry_cache_empty(hdev) || 465 inquiry_cache_empty(hdev) ||
439 ir.flags & IREQ_CACHE_FLUSH) { 466 ir.flags & IREQ_CACHE_FLUSH) {
440 inquiry_cache_flush(hdev); 467 inquiry_cache_flush(hdev);
441 do_inquiry = 1; 468 do_inquiry = 1;
442 } 469 }
443 hci_dev_unlock_bh(hdev); 470 hci_dev_unlock(hdev);
444 471
445 timeo = ir.length * msecs_to_jiffies(2000); 472 timeo = ir.length * msecs_to_jiffies(2000);
446 473
@@ -462,9 +489,9 @@ int hci_inquiry(void __user *arg)
462 goto done; 489 goto done;
463 } 490 }
464 491
465 hci_dev_lock_bh(hdev); 492 hci_dev_lock(hdev);
466 ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf); 493 ir.num_rsp = inquiry_cache_dump(hdev, max_rsp, buf);
467 hci_dev_unlock_bh(hdev); 494 hci_dev_unlock(hdev);
468 495
469 BT_DBG("num_rsp %d", ir.num_rsp); 496 BT_DBG("num_rsp %d", ir.num_rsp);
470 497
@@ -541,15 +568,15 @@ int hci_dev_open(__u16 dev)
541 set_bit(HCI_UP, &hdev->flags); 568 set_bit(HCI_UP, &hdev->flags);
542 hci_notify(hdev, HCI_DEV_UP); 569 hci_notify(hdev, HCI_DEV_UP);
543 if (!test_bit(HCI_SETUP, &hdev->flags)) { 570 if (!test_bit(HCI_SETUP, &hdev->flags)) {
544 hci_dev_lock_bh(hdev); 571 hci_dev_lock(hdev);
545 mgmt_powered(hdev, 1); 572 mgmt_powered(hdev, 1);
546 hci_dev_unlock_bh(hdev); 573 hci_dev_unlock(hdev);
547 } 574 }
548 } else { 575 } else {
549 /* Init failed, cleanup */ 576 /* Init failed, cleanup */
550 tasklet_kill(&hdev->rx_task); 577 flush_work(&hdev->tx_work);
551 tasklet_kill(&hdev->tx_task); 578 flush_work(&hdev->cmd_work);
552 tasklet_kill(&hdev->cmd_task); 579 flush_work(&hdev->rx_work);
553 580
554 skb_queue_purge(&hdev->cmd_q); 581 skb_queue_purge(&hdev->cmd_q);
555 skb_queue_purge(&hdev->rx_q); 582 skb_queue_purge(&hdev->rx_q);
@@ -585,9 +612,9 @@ static int hci_dev_do_close(struct hci_dev *hdev)
585 return 0; 612 return 0;
586 } 613 }
587 614
588 /* Kill RX and TX tasks */ 615 /* Flush RX and TX works */
589 tasklet_kill(&hdev->rx_task); 616 flush_work(&hdev->tx_work);
590 tasklet_kill(&hdev->tx_task); 617 flush_work(&hdev->rx_work);
591 618
592 if (hdev->discov_timeout > 0) { 619 if (hdev->discov_timeout > 0) {
593 cancel_delayed_work(&hdev->discov_off); 620 cancel_delayed_work(&hdev->discov_off);
@@ -597,10 +624,13 @@ static int hci_dev_do_close(struct hci_dev *hdev)
597 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->flags)) 624 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->flags))
598 cancel_delayed_work(&hdev->power_off); 625 cancel_delayed_work(&hdev->power_off);
599 626
600 hci_dev_lock_bh(hdev); 627 if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->flags))
628 cancel_delayed_work(&hdev->service_cache);
629
630 hci_dev_lock(hdev);
601 inquiry_cache_flush(hdev); 631 inquiry_cache_flush(hdev);
602 hci_conn_hash_flush(hdev); 632 hci_conn_hash_flush(hdev);
603 hci_dev_unlock_bh(hdev); 633 hci_dev_unlock(hdev);
604 634
605 hci_notify(hdev, HCI_DEV_DOWN); 635 hci_notify(hdev, HCI_DEV_DOWN);
606 636
@@ -617,8 +647,8 @@ static int hci_dev_do_close(struct hci_dev *hdev)
617 clear_bit(HCI_INIT, &hdev->flags); 647 clear_bit(HCI_INIT, &hdev->flags);
618 } 648 }
619 649
620 /* Kill cmd task */ 650 /* flush cmd work */
621 tasklet_kill(&hdev->cmd_task); 651 flush_work(&hdev->cmd_work);
622 652
623 /* Drop queues */ 653 /* Drop queues */
624 skb_queue_purge(&hdev->rx_q); 654 skb_queue_purge(&hdev->rx_q);
@@ -636,9 +666,9 @@ static int hci_dev_do_close(struct hci_dev *hdev)
636 * and no tasks are scheduled. */ 666 * and no tasks are scheduled. */
637 hdev->close(hdev); 667 hdev->close(hdev);
638 668
639 hci_dev_lock_bh(hdev); 669 hci_dev_lock(hdev);
640 mgmt_powered(hdev, 0); 670 mgmt_powered(hdev, 0);
641 hci_dev_unlock_bh(hdev); 671 hci_dev_unlock(hdev);
642 672
643 /* Clear flags */ 673 /* Clear flags */
644 hdev->flags = 0; 674 hdev->flags = 0;
@@ -672,7 +702,6 @@ int hci_dev_reset(__u16 dev)
672 return -ENODEV; 702 return -ENODEV;
673 703
674 hci_req_lock(hdev); 704 hci_req_lock(hdev);
675 tasklet_disable(&hdev->tx_task);
676 705
677 if (!test_bit(HCI_UP, &hdev->flags)) 706 if (!test_bit(HCI_UP, &hdev->flags))
678 goto done; 707 goto done;
@@ -681,10 +710,10 @@ int hci_dev_reset(__u16 dev)
681 skb_queue_purge(&hdev->rx_q); 710 skb_queue_purge(&hdev->rx_q);
682 skb_queue_purge(&hdev->cmd_q); 711 skb_queue_purge(&hdev->cmd_q);
683 712
684 hci_dev_lock_bh(hdev); 713 hci_dev_lock(hdev);
685 inquiry_cache_flush(hdev); 714 inquiry_cache_flush(hdev);
686 hci_conn_hash_flush(hdev); 715 hci_conn_hash_flush(hdev);
687 hci_dev_unlock_bh(hdev); 716 hci_dev_unlock(hdev);
688 717
689 if (hdev->flush) 718 if (hdev->flush)
690 hdev->flush(hdev); 719 hdev->flush(hdev);
@@ -697,7 +726,6 @@ int hci_dev_reset(__u16 dev)
697 msecs_to_jiffies(HCI_INIT_TIMEOUT)); 726 msecs_to_jiffies(HCI_INIT_TIMEOUT));
698 727
699done: 728done:
700 tasklet_enable(&hdev->tx_task);
701 hci_req_unlock(hdev); 729 hci_req_unlock(hdev);
702 hci_dev_put(hdev); 730 hci_dev_put(hdev);
703 return ret; 731 return ret;
@@ -816,7 +844,7 @@ int hci_get_dev_list(void __user *arg)
816 844
817 dr = dl->dev_req; 845 dr = dl->dev_req;
818 846
819 read_lock_bh(&hci_dev_list_lock); 847 read_lock(&hci_dev_list_lock);
820 list_for_each_entry(hdev, &hci_dev_list, list) { 848 list_for_each_entry(hdev, &hci_dev_list, list) {
821 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->flags)) 849 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->flags))
822 cancel_delayed_work(&hdev->power_off); 850 cancel_delayed_work(&hdev->power_off);
@@ -830,7 +858,7 @@ int hci_get_dev_list(void __user *arg)
830 if (++n >= dev_num) 858 if (++n >= dev_num)
831 break; 859 break;
832 } 860 }
833 read_unlock_bh(&hci_dev_list_lock); 861 read_unlock(&hci_dev_list_lock);
834 862
835 dl->dev_num = n; 863 dl->dev_num = n;
836 size = sizeof(*dl) + n * sizeof(*dr); 864 size = sizeof(*dl) + n * sizeof(*dr);
@@ -939,7 +967,7 @@ static void hci_power_on(struct work_struct *work)
939 return; 967 return;
940 968
941 if (test_bit(HCI_AUTO_OFF, &hdev->flags)) 969 if (test_bit(HCI_AUTO_OFF, &hdev->flags))
942 queue_delayed_work(hdev->workqueue, &hdev->power_off, 970 schedule_delayed_work(&hdev->power_off,
943 msecs_to_jiffies(AUTO_OFF_TIMEOUT)); 971 msecs_to_jiffies(AUTO_OFF_TIMEOUT));
944 972
945 if (test_and_clear_bit(HCI_SETUP, &hdev->flags)) 973 if (test_and_clear_bit(HCI_SETUP, &hdev->flags))
@@ -967,13 +995,13 @@ static void hci_discov_off(struct work_struct *work)
967 995
968 BT_DBG("%s", hdev->name); 996 BT_DBG("%s", hdev->name);
969 997
970 hci_dev_lock_bh(hdev); 998 hci_dev_lock(hdev);
971 999
972 hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); 1000 hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan);
973 1001
974 hdev->discov_timeout = 0; 1002 hdev->discov_timeout = 0;
975 1003
976 hci_dev_unlock_bh(hdev); 1004 hci_dev_unlock(hdev);
977} 1005}
978 1006
979int hci_uuids_clear(struct hci_dev *hdev) 1007int hci_uuids_clear(struct hci_dev *hdev)
@@ -1207,7 +1235,7 @@ static void hci_cmd_timer(unsigned long arg)
1207 1235
1208 BT_ERR("%s command tx timeout", hdev->name); 1236 BT_ERR("%s command tx timeout", hdev->name);
1209 atomic_set(&hdev->cmd_cnt, 1); 1237 atomic_set(&hdev->cmd_cnt, 1);
1210 tasklet_schedule(&hdev->cmd_task); 1238 queue_work(hdev->workqueue, &hdev->cmd_work);
1211} 1239}
1212 1240
1213struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev, 1241struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev,
@@ -1340,9 +1368,10 @@ int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr)
1340 return mgmt_device_unblocked(hdev, bdaddr); 1368 return mgmt_device_unblocked(hdev, bdaddr);
1341} 1369}
1342 1370
1343static void hci_clear_adv_cache(unsigned long arg) 1371static void hci_clear_adv_cache(struct work_struct *work)
1344{ 1372{
1345 struct hci_dev *hdev = (void *) arg; 1373 struct hci_dev *hdev = container_of(work, struct hci_dev,
1374 adv_work.work);
1346 1375
1347 hci_dev_lock(hdev); 1376 hci_dev_lock(hdev);
1348 1377
@@ -1429,7 +1458,7 @@ int hci_register_dev(struct hci_dev *hdev)
1429 */ 1458 */
1430 id = (hdev->dev_type == HCI_BREDR) ? 0 : 1; 1459 id = (hdev->dev_type == HCI_BREDR) ? 0 : 1;
1431 1460
1432 write_lock_bh(&hci_dev_list_lock); 1461 write_lock(&hci_dev_list_lock);
1433 1462
1434 /* Find first available device id */ 1463 /* Find first available device id */
1435 list_for_each(p, &hci_dev_list) { 1464 list_for_each(p, &hci_dev_list) {
@@ -1443,7 +1472,7 @@ int hci_register_dev(struct hci_dev *hdev)
1443 list_add_tail(&hdev->list, head); 1472 list_add_tail(&hdev->list, head);
1444 1473
1445 atomic_set(&hdev->refcnt, 1); 1474 atomic_set(&hdev->refcnt, 1);
1446 spin_lock_init(&hdev->lock); 1475 mutex_init(&hdev->lock);
1447 1476
1448 hdev->flags = 0; 1477 hdev->flags = 0;
1449 hdev->dev_flags = 0; 1478 hdev->dev_flags = 0;
@@ -1456,9 +1485,10 @@ int hci_register_dev(struct hci_dev *hdev)
1456 hdev->sniff_max_interval = 800; 1485 hdev->sniff_max_interval = 800;
1457 hdev->sniff_min_interval = 80; 1486 hdev->sniff_min_interval = 80;
1458 1487
1459 tasklet_init(&hdev->cmd_task, hci_cmd_task, (unsigned long) hdev); 1488 INIT_WORK(&hdev->rx_work, hci_rx_work);
1460 tasklet_init(&hdev->rx_task, hci_rx_task, (unsigned long) hdev); 1489 INIT_WORK(&hdev->cmd_work, hci_cmd_work);
1461 tasklet_init(&hdev->tx_task, hci_tx_task, (unsigned long) hdev); 1490 INIT_WORK(&hdev->tx_work, hci_tx_work);
1491
1462 1492
1463 skb_queue_head_init(&hdev->rx_q); 1493 skb_queue_head_init(&hdev->rx_q);
1464 skb_queue_head_init(&hdev->cmd_q); 1494 skb_queue_head_init(&hdev->cmd_q);
@@ -1487,9 +1517,8 @@ int hci_register_dev(struct hci_dev *hdev)
1487 INIT_LIST_HEAD(&hdev->remote_oob_data); 1517 INIT_LIST_HEAD(&hdev->remote_oob_data);
1488 1518
1489 INIT_LIST_HEAD(&hdev->adv_entries); 1519 INIT_LIST_HEAD(&hdev->adv_entries);
1490 setup_timer(&hdev->adv_timer, hci_clear_adv_cache,
1491 (unsigned long) hdev);
1492 1520
1521 INIT_DELAYED_WORK(&hdev->adv_work, hci_clear_adv_cache);
1493 INIT_WORK(&hdev->power_on, hci_power_on); 1522 INIT_WORK(&hdev->power_on, hci_power_on);
1494 INIT_DELAYED_WORK(&hdev->power_off, hci_power_off); 1523 INIT_DELAYED_WORK(&hdev->power_off, hci_power_off);
1495 1524
@@ -1499,9 +1528,10 @@ int hci_register_dev(struct hci_dev *hdev)
1499 1528
1500 atomic_set(&hdev->promisc, 0); 1529 atomic_set(&hdev->promisc, 0);
1501 1530
1502 write_unlock_bh(&hci_dev_list_lock); 1531 write_unlock(&hci_dev_list_lock);
1503 1532
1504 hdev->workqueue = create_singlethread_workqueue(hdev->name); 1533 hdev->workqueue = alloc_workqueue(hdev->name, WQ_HIGHPRI | WQ_UNBOUND |
1534 WQ_MEM_RECLAIM, 1);
1505 if (!hdev->workqueue) { 1535 if (!hdev->workqueue) {
1506 error = -ENOMEM; 1536 error = -ENOMEM;
1507 goto err; 1537 goto err;
@@ -1522,7 +1552,7 @@ int hci_register_dev(struct hci_dev *hdev)
1522 1552
1523 set_bit(HCI_AUTO_OFF, &hdev->flags); 1553 set_bit(HCI_AUTO_OFF, &hdev->flags);
1524 set_bit(HCI_SETUP, &hdev->flags); 1554 set_bit(HCI_SETUP, &hdev->flags);
1525 queue_work(hdev->workqueue, &hdev->power_on); 1555 schedule_work(&hdev->power_on);
1526 1556
1527 hci_notify(hdev, HCI_DEV_REG); 1557 hci_notify(hdev, HCI_DEV_REG);
1528 1558
@@ -1531,9 +1561,9 @@ int hci_register_dev(struct hci_dev *hdev)
1531err_wqueue: 1561err_wqueue:
1532 destroy_workqueue(hdev->workqueue); 1562 destroy_workqueue(hdev->workqueue);
1533err: 1563err:
1534 write_lock_bh(&hci_dev_list_lock); 1564 write_lock(&hci_dev_list_lock);
1535 list_del(&hdev->list); 1565 list_del(&hdev->list);
1536 write_unlock_bh(&hci_dev_list_lock); 1566 write_unlock(&hci_dev_list_lock);
1537 1567
1538 return error; 1568 return error;
1539} 1569}
@@ -1546,9 +1576,9 @@ void hci_unregister_dev(struct hci_dev *hdev)
1546 1576
1547 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus); 1577 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
1548 1578
1549 write_lock_bh(&hci_dev_list_lock); 1579 write_lock(&hci_dev_list_lock);
1550 list_del(&hdev->list); 1580 list_del(&hdev->list);
1551 write_unlock_bh(&hci_dev_list_lock); 1581 write_unlock(&hci_dev_list_lock);
1552 1582
1553 hci_dev_do_close(hdev); 1583 hci_dev_do_close(hdev);
1554 1584
@@ -1557,9 +1587,9 @@ void hci_unregister_dev(struct hci_dev *hdev)
1557 1587
1558 if (!test_bit(HCI_INIT, &hdev->flags) && 1588 if (!test_bit(HCI_INIT, &hdev->flags) &&
1559 !test_bit(HCI_SETUP, &hdev->flags)) { 1589 !test_bit(HCI_SETUP, &hdev->flags)) {
1560 hci_dev_lock_bh(hdev); 1590 hci_dev_lock(hdev);
1561 mgmt_index_removed(hdev); 1591 mgmt_index_removed(hdev);
1562 hci_dev_unlock_bh(hdev); 1592 hci_dev_unlock(hdev);
1563 } 1593 }
1564 1594
1565 /* mgmt_index_removed should take care of emptying the 1595 /* mgmt_index_removed should take care of emptying the
@@ -1575,17 +1605,17 @@ void hci_unregister_dev(struct hci_dev *hdev)
1575 1605
1576 hci_del_sysfs(hdev); 1606 hci_del_sysfs(hdev);
1577 1607
1578 del_timer(&hdev->adv_timer); 1608 cancel_delayed_work_sync(&hdev->adv_work);
1579 1609
1580 destroy_workqueue(hdev->workqueue); 1610 destroy_workqueue(hdev->workqueue);
1581 1611
1582 hci_dev_lock_bh(hdev); 1612 hci_dev_lock(hdev);
1583 hci_blacklist_clear(hdev); 1613 hci_blacklist_clear(hdev);
1584 hci_uuids_clear(hdev); 1614 hci_uuids_clear(hdev);
1585 hci_link_keys_clear(hdev); 1615 hci_link_keys_clear(hdev);
1586 hci_remote_oob_data_clear(hdev); 1616 hci_remote_oob_data_clear(hdev);
1587 hci_adv_entries_clear(hdev); 1617 hci_adv_entries_clear(hdev);
1588 hci_dev_unlock_bh(hdev); 1618 hci_dev_unlock(hdev);
1589 1619
1590 __hci_dev_put(hdev); 1620 __hci_dev_put(hdev);
1591} 1621}
@@ -1623,9 +1653,8 @@ int hci_recv_frame(struct sk_buff *skb)
1623 /* Time stamp */ 1653 /* Time stamp */
1624 __net_timestamp(skb); 1654 __net_timestamp(skb);
1625 1655
1626 /* Queue frame for rx task */
1627 skb_queue_tail(&hdev->rx_q, skb); 1656 skb_queue_tail(&hdev->rx_q, skb);
1628 tasklet_schedule(&hdev->rx_task); 1657 queue_work(hdev->workqueue, &hdev->rx_work);
1629 1658
1630 return 0; 1659 return 0;
1631} 1660}
@@ -1797,59 +1826,13 @@ EXPORT_SYMBOL(hci_recv_stream_fragment);
1797 1826
1798/* ---- Interface to upper protocols ---- */ 1827/* ---- Interface to upper protocols ---- */
1799 1828
1800/* Register/Unregister protocols.
1801 * hci_task_lock is used to ensure that no tasks are running. */
1802int hci_register_proto(struct hci_proto *hp)
1803{
1804 int err = 0;
1805
1806 BT_DBG("%p name %s id %d", hp, hp->name, hp->id);
1807
1808 if (hp->id >= HCI_MAX_PROTO)
1809 return -EINVAL;
1810
1811 write_lock_bh(&hci_task_lock);
1812
1813 if (!hci_proto[hp->id])
1814 hci_proto[hp->id] = hp;
1815 else
1816 err = -EEXIST;
1817
1818 write_unlock_bh(&hci_task_lock);
1819
1820 return err;
1821}
1822EXPORT_SYMBOL(hci_register_proto);
1823
1824int hci_unregister_proto(struct hci_proto *hp)
1825{
1826 int err = 0;
1827
1828 BT_DBG("%p name %s id %d", hp, hp->name, hp->id);
1829
1830 if (hp->id >= HCI_MAX_PROTO)
1831 return -EINVAL;
1832
1833 write_lock_bh(&hci_task_lock);
1834
1835 if (hci_proto[hp->id])
1836 hci_proto[hp->id] = NULL;
1837 else
1838 err = -ENOENT;
1839
1840 write_unlock_bh(&hci_task_lock);
1841
1842 return err;
1843}
1844EXPORT_SYMBOL(hci_unregister_proto);
1845
1846int hci_register_cb(struct hci_cb *cb) 1829int hci_register_cb(struct hci_cb *cb)
1847{ 1830{
1848 BT_DBG("%p name %s", cb, cb->name); 1831 BT_DBG("%p name %s", cb, cb->name);
1849 1832
1850 write_lock_bh(&hci_cb_list_lock); 1833 write_lock(&hci_cb_list_lock);
1851 list_add(&cb->list, &hci_cb_list); 1834 list_add(&cb->list, &hci_cb_list);
1852 write_unlock_bh(&hci_cb_list_lock); 1835 write_unlock(&hci_cb_list_lock);
1853 1836
1854 return 0; 1837 return 0;
1855} 1838}
@@ -1859,9 +1842,9 @@ int hci_unregister_cb(struct hci_cb *cb)
1859{ 1842{
1860 BT_DBG("%p name %s", cb, cb->name); 1843 BT_DBG("%p name %s", cb, cb->name);
1861 1844
1862 write_lock_bh(&hci_cb_list_lock); 1845 write_lock(&hci_cb_list_lock);
1863 list_del(&cb->list); 1846 list_del(&cb->list);
1864 write_unlock_bh(&hci_cb_list_lock); 1847 write_unlock(&hci_cb_list_lock);
1865 1848
1866 return 0; 1849 return 0;
1867} 1850}
@@ -1922,7 +1905,7 @@ int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, void *param)
1922 hdev->init_last_cmd = opcode; 1905 hdev->init_last_cmd = opcode;
1923 1906
1924 skb_queue_tail(&hdev->cmd_q, skb); 1907 skb_queue_tail(&hdev->cmd_q, skb);
1925 tasklet_schedule(&hdev->cmd_task); 1908 queue_work(hdev->workqueue, &hdev->cmd_work);
1926 1909
1927 return 0; 1910 return 0;
1928} 1911}
@@ -1977,7 +1960,7 @@ static void hci_queue_acl(struct hci_conn *conn, struct sk_buff_head *queue,
1977 skb_shinfo(skb)->frag_list = NULL; 1960 skb_shinfo(skb)->frag_list = NULL;
1978 1961
1979 /* Queue all fragments atomically */ 1962 /* Queue all fragments atomically */
1980 spin_lock_bh(&queue->lock); 1963 spin_lock(&queue->lock);
1981 1964
1982 __skb_queue_tail(queue, skb); 1965 __skb_queue_tail(queue, skb);
1983 1966
@@ -1995,7 +1978,7 @@ static void hci_queue_acl(struct hci_conn *conn, struct sk_buff_head *queue,
1995 __skb_queue_tail(queue, skb); 1978 __skb_queue_tail(queue, skb);
1996 } while (list); 1979 } while (list);
1997 1980
1998 spin_unlock_bh(&queue->lock); 1981 spin_unlock(&queue->lock);
1999 } 1982 }
2000} 1983}
2001 1984
@@ -2012,7 +1995,7 @@ void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags)
2012 1995
2013 hci_queue_acl(conn, &chan->data_q, skb, flags); 1996 hci_queue_acl(conn, &chan->data_q, skb, flags);
2014 1997
2015 tasklet_schedule(&hdev->tx_task); 1998 queue_work(hdev->workqueue, &hdev->tx_work);
2016} 1999}
2017EXPORT_SYMBOL(hci_send_acl); 2000EXPORT_SYMBOL(hci_send_acl);
2018 2001
@@ -2035,7 +2018,7 @@ void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb)
2035 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT; 2018 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
2036 2019
2037 skb_queue_tail(&conn->data_q, skb); 2020 skb_queue_tail(&conn->data_q, skb);
2038 tasklet_schedule(&hdev->tx_task); 2021 queue_work(hdev->workqueue, &hdev->tx_work);
2039} 2022}
2040EXPORT_SYMBOL(hci_send_sco); 2023EXPORT_SYMBOL(hci_send_sco);
2041 2024
@@ -2050,7 +2033,10 @@ static inline struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type, int
2050 2033
2051 /* We don't have to lock device here. Connections are always 2034 /* We don't have to lock device here. Connections are always
2052 * added and removed with TX task disabled. */ 2035 * added and removed with TX task disabled. */
2053 list_for_each_entry(c, &h->list, list) { 2036
2037 rcu_read_lock();
2038
2039 list_for_each_entry_rcu(c, &h->list, list) {
2054 if (c->type != type || skb_queue_empty(&c->data_q)) 2040 if (c->type != type || skb_queue_empty(&c->data_q))
2055 continue; 2041 continue;
2056 2042
@@ -2068,6 +2054,8 @@ static inline struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type, int
2068 break; 2054 break;
2069 } 2055 }
2070 2056
2057 rcu_read_unlock();
2058
2071 if (conn) { 2059 if (conn) {
2072 int cnt, q; 2060 int cnt, q;
2073 2061
@@ -2103,14 +2091,18 @@ static inline void hci_link_tx_to(struct hci_dev *hdev, __u8 type)
2103 2091
2104 BT_ERR("%s link tx timeout", hdev->name); 2092 BT_ERR("%s link tx timeout", hdev->name);
2105 2093
2094 rcu_read_lock();
2095
2106 /* Kill stalled connections */ 2096 /* Kill stalled connections */
2107 list_for_each_entry(c, &h->list, list) { 2097 list_for_each_entry_rcu(c, &h->list, list) {
2108 if (c->type == type && c->sent) { 2098 if (c->type == type && c->sent) {
2109 BT_ERR("%s killing stalled connection %s", 2099 BT_ERR("%s killing stalled connection %s",
2110 hdev->name, batostr(&c->dst)); 2100 hdev->name, batostr(&c->dst));
2111 hci_acl_disconn(c, 0x13); 2101 hci_acl_disconn(c, 0x13);
2112 } 2102 }
2113 } 2103 }
2104
2105 rcu_read_unlock();
2114} 2106}
2115 2107
2116static inline struct hci_chan *hci_chan_sent(struct hci_dev *hdev, __u8 type, 2108static inline struct hci_chan *hci_chan_sent(struct hci_dev *hdev, __u8 type,
@@ -2124,8 +2116,9 @@ static inline struct hci_chan *hci_chan_sent(struct hci_dev *hdev, __u8 type,
2124 2116
2125 BT_DBG("%s", hdev->name); 2117 BT_DBG("%s", hdev->name);
2126 2118
2127 list_for_each_entry(conn, &h->list, list) { 2119 rcu_read_lock();
2128 struct hci_chan_hash *ch; 2120
2121 list_for_each_entry_rcu(conn, &h->list, list) {
2129 struct hci_chan *tmp; 2122 struct hci_chan *tmp;
2130 2123
2131 if (conn->type != type) 2124 if (conn->type != type)
@@ -2136,9 +2129,7 @@ static inline struct hci_chan *hci_chan_sent(struct hci_dev *hdev, __u8 type,
2136 2129
2137 conn_num++; 2130 conn_num++;
2138 2131
2139 ch = &conn->chan_hash; 2132 list_for_each_entry_rcu(tmp, &conn->chan_list, list) {
2140
2141 list_for_each_entry(tmp, &ch->list, list) {
2142 struct sk_buff *skb; 2133 struct sk_buff *skb;
2143 2134
2144 if (skb_queue_empty(&tmp->data_q)) 2135 if (skb_queue_empty(&tmp->data_q))
@@ -2166,6 +2157,8 @@ static inline struct hci_chan *hci_chan_sent(struct hci_dev *hdev, __u8 type,
2166 break; 2157 break;
2167 } 2158 }
2168 2159
2160 rcu_read_unlock();
2161
2169 if (!chan) 2162 if (!chan)
2170 return NULL; 2163 return NULL;
2171 2164
@@ -2199,8 +2192,9 @@ static void hci_prio_recalculate(struct hci_dev *hdev, __u8 type)
2199 2192
2200 BT_DBG("%s", hdev->name); 2193 BT_DBG("%s", hdev->name);
2201 2194
2202 list_for_each_entry(conn, &h->list, list) { 2195 rcu_read_lock();
2203 struct hci_chan_hash *ch; 2196
2197 list_for_each_entry_rcu(conn, &h->list, list) {
2204 struct hci_chan *chan; 2198 struct hci_chan *chan;
2205 2199
2206 if (conn->type != type) 2200 if (conn->type != type)
@@ -2211,8 +2205,7 @@ static void hci_prio_recalculate(struct hci_dev *hdev, __u8 type)
2211 2205
2212 num++; 2206 num++;
2213 2207
2214 ch = &conn->chan_hash; 2208 list_for_each_entry_rcu(chan, &conn->chan_list, list) {
2215 list_for_each_entry(chan, &ch->list, list) {
2216 struct sk_buff *skb; 2209 struct sk_buff *skb;
2217 2210
2218 if (chan->sent) { 2211 if (chan->sent) {
@@ -2236,6 +2229,9 @@ static void hci_prio_recalculate(struct hci_dev *hdev, __u8 type)
2236 if (hci_conn_num(hdev, type) == num) 2229 if (hci_conn_num(hdev, type) == num)
2237 break; 2230 break;
2238 } 2231 }
2232
2233 rcu_read_unlock();
2234
2239} 2235}
2240 2236
2241static inline void hci_sched_acl(struct hci_dev *hdev) 2237static inline void hci_sched_acl(struct hci_dev *hdev)
@@ -2386,13 +2382,11 @@ static inline void hci_sched_le(struct hci_dev *hdev)
2386 hci_prio_recalculate(hdev, LE_LINK); 2382 hci_prio_recalculate(hdev, LE_LINK);
2387} 2383}
2388 2384
2389static void hci_tx_task(unsigned long arg) 2385static void hci_tx_work(struct work_struct *work)
2390{ 2386{
2391 struct hci_dev *hdev = (struct hci_dev *) arg; 2387 struct hci_dev *hdev = container_of(work, struct hci_dev, tx_work);
2392 struct sk_buff *skb; 2388 struct sk_buff *skb;
2393 2389
2394 read_lock(&hci_task_lock);
2395
2396 BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt, 2390 BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt,
2397 hdev->sco_cnt, hdev->le_cnt); 2391 hdev->sco_cnt, hdev->le_cnt);
2398 2392
@@ -2409,8 +2403,6 @@ static void hci_tx_task(unsigned long arg)
2409 /* Send next queued raw (unknown type) packet */ 2403 /* Send next queued raw (unknown type) packet */
2410 while ((skb = skb_dequeue(&hdev->raw_q))) 2404 while ((skb = skb_dequeue(&hdev->raw_q)))
2411 hci_send_frame(skb); 2405 hci_send_frame(skb);
2412
2413 read_unlock(&hci_task_lock);
2414} 2406}
2415 2407
2416/* ----- HCI RX task (incoming data processing) ----- */ 2408/* ----- HCI RX task (incoming data processing) ----- */
@@ -2437,16 +2429,11 @@ static inline void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb)
2437 hci_dev_unlock(hdev); 2429 hci_dev_unlock(hdev);
2438 2430
2439 if (conn) { 2431 if (conn) {
2440 register struct hci_proto *hp; 2432 hci_conn_enter_active_mode(conn, BT_POWER_FORCE_ACTIVE_OFF);
2441
2442 hci_conn_enter_active_mode(conn, bt_cb(skb)->force_active);
2443 2433
2444 /* Send to upper protocol */ 2434 /* Send to upper protocol */
2445 hp = hci_proto[HCI_PROTO_L2CAP]; 2435 l2cap_recv_acldata(conn, skb, flags);
2446 if (hp && hp->recv_acldata) { 2436 return;
2447 hp->recv_acldata(conn, skb, flags);
2448 return;
2449 }
2450 } else { 2437 } else {
2451 BT_ERR("%s ACL packet for unknown connection handle %d", 2438 BT_ERR("%s ACL packet for unknown connection handle %d",
2452 hdev->name, handle); 2439 hdev->name, handle);
@@ -2475,14 +2462,9 @@ static inline void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb)
2475 hci_dev_unlock(hdev); 2462 hci_dev_unlock(hdev);
2476 2463
2477 if (conn) { 2464 if (conn) {
2478 register struct hci_proto *hp;
2479
2480 /* Send to upper protocol */ 2465 /* Send to upper protocol */
2481 hp = hci_proto[HCI_PROTO_SCO]; 2466 sco_recv_scodata(conn, skb);
2482 if (hp && hp->recv_scodata) { 2467 return;
2483 hp->recv_scodata(conn, skb);
2484 return;
2485 }
2486 } else { 2468 } else {
2487 BT_ERR("%s SCO packet for unknown connection handle %d", 2469 BT_ERR("%s SCO packet for unknown connection handle %d",
2488 hdev->name, handle); 2470 hdev->name, handle);
@@ -2491,15 +2473,13 @@ static inline void hci_scodata_packet(struct hci_dev *hdev, struct sk_buff *skb)
2491 kfree_skb(skb); 2473 kfree_skb(skb);
2492} 2474}
2493 2475
2494static void hci_rx_task(unsigned long arg) 2476static void hci_rx_work(struct work_struct *work)
2495{ 2477{
2496 struct hci_dev *hdev = (struct hci_dev *) arg; 2478 struct hci_dev *hdev = container_of(work, struct hci_dev, rx_work);
2497 struct sk_buff *skb; 2479 struct sk_buff *skb;
2498 2480
2499 BT_DBG("%s", hdev->name); 2481 BT_DBG("%s", hdev->name);
2500 2482
2501 read_lock(&hci_task_lock);
2502
2503 while ((skb = skb_dequeue(&hdev->rx_q))) { 2483 while ((skb = skb_dequeue(&hdev->rx_q))) {
2504 if (atomic_read(&hdev->promisc)) { 2484 if (atomic_read(&hdev->promisc)) {
2505 /* Send copy to the sockets */ 2485 /* Send copy to the sockets */
@@ -2524,6 +2504,7 @@ static void hci_rx_task(unsigned long arg)
2524 /* Process frame */ 2504 /* Process frame */
2525 switch (bt_cb(skb)->pkt_type) { 2505 switch (bt_cb(skb)->pkt_type) {
2526 case HCI_EVENT_PKT: 2506 case HCI_EVENT_PKT:
2507 BT_DBG("%s Event packet", hdev->name);
2527 hci_event_packet(hdev, skb); 2508 hci_event_packet(hdev, skb);
2528 break; 2509 break;
2529 2510
@@ -2542,13 +2523,11 @@ static void hci_rx_task(unsigned long arg)
2542 break; 2523 break;
2543 } 2524 }
2544 } 2525 }
2545
2546 read_unlock(&hci_task_lock);
2547} 2526}
2548 2527
2549static void hci_cmd_task(unsigned long arg) 2528static void hci_cmd_work(struct work_struct *work)
2550{ 2529{
2551 struct hci_dev *hdev = (struct hci_dev *) arg; 2530 struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_work);
2552 struct sk_buff *skb; 2531 struct sk_buff *skb;
2553 2532
2554 BT_DBG("%s cmd %d", hdev->name, atomic_read(&hdev->cmd_cnt)); 2533 BT_DBG("%s cmd %d", hdev->name, atomic_read(&hdev->cmd_cnt));
@@ -2572,7 +2551,7 @@ static void hci_cmd_task(unsigned long arg)
2572 jiffies + msecs_to_jiffies(HCI_CMD_TIMEOUT)); 2551 jiffies + msecs_to_jiffies(HCI_CMD_TIMEOUT));
2573 } else { 2552 } else {
2574 skb_queue_head(&hdev->cmd_q, skb); 2553 skb_queue_head(&hdev->cmd_q, skb);
2575 tasklet_schedule(&hdev->cmd_task); 2554 queue_work(hdev->workqueue, &hdev->cmd_work);
2576 } 2555 }
2577 } 2556 }
2578} 2557}