btrsi.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * Copyright (c) 2017 Redpine Signals Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <net/bluetooth/bluetooth.h>
  19. #include <net/bluetooth/hci_core.h>
  20. #include <asm/unaligned.h>
  21. #include <net/rsi_91x.h>
  22. #include <net/genetlink.h>
  23. #define RSI_HEADROOM_FOR_BT_HAL 16
  24. #define RSI_FRAME_DESC_SIZE 16
  25. struct rsi_hci_adapter {
  26. void *priv;
  27. struct rsi_proto_ops *proto_ops;
  28. struct hci_dev *hdev;
  29. };
  30. static int rsi_hci_open(struct hci_dev *hdev)
  31. {
  32. return 0;
  33. }
  34. static int rsi_hci_close(struct hci_dev *hdev)
  35. {
  36. return 0;
  37. }
  38. static int rsi_hci_flush(struct hci_dev *hdev)
  39. {
  40. return 0;
  41. }
  42. static int rsi_hci_send_pkt(struct hci_dev *hdev, struct sk_buff *skb)
  43. {
  44. struct rsi_hci_adapter *h_adapter = hci_get_drvdata(hdev);
  45. struct sk_buff *new_skb = NULL;
  46. switch (hci_skb_pkt_type(skb)) {
  47. case HCI_COMMAND_PKT:
  48. hdev->stat.cmd_tx++;
  49. break;
  50. case HCI_ACLDATA_PKT:
  51. hdev->stat.acl_tx++;
  52. break;
  53. case HCI_SCODATA_PKT:
  54. hdev->stat.sco_tx++;
  55. break;
  56. }
  57. if (skb_headroom(skb) < RSI_HEADROOM_FOR_BT_HAL) {
  58. /* Insufficient skb headroom - allocate a new skb */
  59. new_skb = skb_realloc_headroom(skb, RSI_HEADROOM_FOR_BT_HAL);
  60. if (unlikely(!new_skb))
  61. return -ENOMEM;
  62. bt_cb(new_skb)->pkt_type = hci_skb_pkt_type(skb);
  63. kfree_skb(skb);
  64. skb = new_skb;
  65. }
  66. return h_adapter->proto_ops->coex_send_pkt(h_adapter->priv, skb,
  67. RSI_BT_Q);
  68. }
  69. static int rsi_hci_recv_pkt(void *priv, const u8 *pkt)
  70. {
  71. struct rsi_hci_adapter *h_adapter = priv;
  72. struct hci_dev *hdev = h_adapter->hdev;
  73. struct sk_buff *skb;
  74. int pkt_len = get_unaligned_le16(pkt) & 0x0fff;
  75. skb = dev_alloc_skb(pkt_len);
  76. if (!skb)
  77. return -ENOMEM;
  78. memcpy(skb->data, pkt + RSI_FRAME_DESC_SIZE, pkt_len);
  79. skb_put(skb, pkt_len);
  80. h_adapter->hdev->stat.byte_rx += skb->len;
  81. hci_skb_pkt_type(skb) = pkt[14];
  82. return hci_recv_frame(hdev, skb);
  83. }
  84. static int rsi_hci_attach(void *priv, struct rsi_proto_ops *ops)
  85. {
  86. struct rsi_hci_adapter *h_adapter = NULL;
  87. struct hci_dev *hdev;
  88. int err = 0;
  89. h_adapter = kzalloc(sizeof(*h_adapter), GFP_KERNEL);
  90. if (!h_adapter)
  91. return -ENOMEM;
  92. h_adapter->priv = priv;
  93. ops->set_bt_context(priv, h_adapter);
  94. h_adapter->proto_ops = ops;
  95. hdev = hci_alloc_dev();
  96. if (!hdev) {
  97. BT_ERR("Failed to alloc HCI device");
  98. goto err;
  99. }
  100. h_adapter->hdev = hdev;
  101. if (ops->get_host_intf(priv) == RSI_HOST_INTF_SDIO)
  102. hdev->bus = HCI_SDIO;
  103. else
  104. hdev->bus = HCI_USB;
  105. hci_set_drvdata(hdev, h_adapter);
  106. hdev->dev_type = HCI_PRIMARY;
  107. hdev->open = rsi_hci_open;
  108. hdev->close = rsi_hci_close;
  109. hdev->flush = rsi_hci_flush;
  110. hdev->send = rsi_hci_send_pkt;
  111. err = hci_register_dev(hdev);
  112. if (err < 0) {
  113. BT_ERR("HCI registration failed with errcode %d", err);
  114. hci_free_dev(hdev);
  115. goto err;
  116. }
  117. return 0;
  118. err:
  119. h_adapter->hdev = NULL;
  120. kfree(h_adapter);
  121. return -EINVAL;
  122. }
  123. static void rsi_hci_detach(void *priv)
  124. {
  125. struct rsi_hci_adapter *h_adapter = priv;
  126. struct hci_dev *hdev;
  127. if (!h_adapter)
  128. return;
  129. hdev = h_adapter->hdev;
  130. if (hdev) {
  131. hci_unregister_dev(hdev);
  132. hci_free_dev(hdev);
  133. h_adapter->hdev = NULL;
  134. }
  135. kfree(h_adapter);
  136. }
  137. const struct rsi_mod_ops rsi_bt_ops = {
  138. .attach = rsi_hci_attach,
  139. .detach = rsi_hci_detach,
  140. .recv_pkt = rsi_hci_recv_pkt,
  141. };
  142. EXPORT_SYMBOL(rsi_bt_ops);
  143. static int rsi_91x_bt_module_init(void)
  144. {
  145. return 0;
  146. }
  147. static void rsi_91x_bt_module_exit(void)
  148. {
  149. return;
  150. }
  151. module_init(rsi_91x_bt_module_init);
  152. module_exit(rsi_91x_bt_module_exit);
  153. MODULE_AUTHOR("Redpine Signals Inc");
  154. MODULE_DESCRIPTION("RSI BT driver");
  155. MODULE_SUPPORTED_DEVICE("RSI-BT");
  156. MODULE_LICENSE("Dual BSD/GPL");