hci_bcm.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. *
  3. * Bluetooth HCI UART driver for Broadcom devices
  4. *
  5. * Copyright (C) 2015 Intel Corporation
  6. *
  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 as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/skbuff.h>
  26. #include <net/bluetooth/bluetooth.h>
  27. #include <net/bluetooth/hci_core.h>
  28. #include "btbcm.h"
  29. #include "hci_uart.h"
  30. struct bcm_data {
  31. struct sk_buff *rx_skb;
  32. struct sk_buff_head txq;
  33. };
  34. static int bcm_open(struct hci_uart *hu)
  35. {
  36. struct bcm_data *bcm;
  37. BT_DBG("hu %p", hu);
  38. bcm = kzalloc(sizeof(*bcm), GFP_KERNEL);
  39. if (!bcm)
  40. return -ENOMEM;
  41. skb_queue_head_init(&bcm->txq);
  42. hu->priv = bcm;
  43. return 0;
  44. }
  45. static int bcm_close(struct hci_uart *hu)
  46. {
  47. struct bcm_data *bcm = hu->priv;
  48. BT_DBG("hu %p", hu);
  49. skb_queue_purge(&bcm->txq);
  50. kfree_skb(bcm->rx_skb);
  51. kfree(bcm);
  52. hu->priv = NULL;
  53. return 0;
  54. }
  55. static int bcm_flush(struct hci_uart *hu)
  56. {
  57. struct bcm_data *bcm = hu->priv;
  58. BT_DBG("hu %p", hu);
  59. skb_queue_purge(&bcm->txq);
  60. return 0;
  61. }
  62. static int bcm_setup(struct hci_uart *hu)
  63. {
  64. BT_DBG("hu %p", hu);
  65. hu->hdev->set_bdaddr = btbcm_set_bdaddr;
  66. return btbcm_setup_patchram(hu->hdev);
  67. }
  68. static const struct h4_recv_pkt bcm_recv_pkts[] = {
  69. { H4_RECV_ACL, .recv = hci_recv_frame },
  70. { H4_RECV_SCO, .recv = hci_recv_frame },
  71. { H4_RECV_EVENT, .recv = hci_recv_frame },
  72. };
  73. static int bcm_recv(struct hci_uart *hu, const void *data, int count)
  74. {
  75. struct bcm_data *bcm = hu->priv;
  76. if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
  77. return -EUNATCH;
  78. bcm->rx_skb = h4_recv_buf(hu->hdev, bcm->rx_skb, data, count,
  79. bcm_recv_pkts, ARRAY_SIZE(bcm_recv_pkts));
  80. if (IS_ERR(bcm->rx_skb)) {
  81. int err = PTR_ERR(bcm->rx_skb);
  82. BT_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, err);
  83. return err;
  84. }
  85. return count;
  86. }
  87. static int bcm_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  88. {
  89. struct bcm_data *bcm = hu->priv;
  90. BT_DBG("hu %p skb %p", hu, skb);
  91. /* Prepend skb with frame type */
  92. memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
  93. skb_queue_tail(&bcm->txq, skb);
  94. return 0;
  95. }
  96. static struct sk_buff *bcm_dequeue(struct hci_uart *hu)
  97. {
  98. struct bcm_data *bcm = hu->priv;
  99. return skb_dequeue(&bcm->txq);
  100. }
  101. static const struct hci_uart_proto bcm_proto = {
  102. .id = HCI_UART_BCM,
  103. .name = "BCM",
  104. .open = bcm_open,
  105. .close = bcm_close,
  106. .flush = bcm_flush,
  107. .setup = bcm_setup,
  108. .recv = bcm_recv,
  109. .enqueue = bcm_enqueue,
  110. .dequeue = bcm_dequeue,
  111. };
  112. int __init bcm_init(void)
  113. {
  114. return hci_uart_register_proto(&bcm_proto);
  115. }
  116. int __exit bcm_deinit(void)
  117. {
  118. return hci_uart_unregister_proto(&bcm_proto);
  119. }