btrsi.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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_DMA_ALIGN 8
  24. #define RSI_FRAME_DESC_SIZE 16
  25. #define RSI_HEADROOM_FOR_BT_HAL (RSI_FRAME_DESC_SIZE + RSI_DMA_ALIGN)
  26. struct rsi_hci_adapter {
  27. void *priv;
  28. struct rsi_proto_ops *proto_ops;
  29. struct hci_dev *hdev;
  30. };
  31. static int rsi_hci_open(struct hci_dev *hdev)
  32. {
  33. return 0;
  34. }
  35. static int rsi_hci_close(struct hci_dev *hdev)
  36. {
  37. return 0;
  38. }
  39. static int rsi_hci_flush(struct hci_dev *hdev)
  40. {
  41. return 0;
  42. }
  43. static int rsi_hci_send_pkt(struct hci_dev *hdev, struct sk_buff *skb)
  44. {
  45. struct rsi_hci_adapter *h_adapter = hci_get_drvdata(hdev);
  46. struct sk_buff *new_skb = NULL;
  47. switch (hci_skb_pkt_type(skb)) {
  48. case HCI_COMMAND_PKT:
  49. hdev->stat.cmd_tx++;
  50. break;
  51. case HCI_ACLDATA_PKT:
  52. hdev->stat.acl_tx++;
  53. break;
  54. case HCI_SCODATA_PKT:
  55. hdev->stat.sco_tx++;
  56. break;
  57. }
  58. if (skb_headroom(skb) < RSI_HEADROOM_FOR_BT_HAL) {
  59. /* Insufficient skb headroom - allocate a new skb */
  60. new_skb = skb_realloc_headroom(skb, RSI_HEADROOM_FOR_BT_HAL);
  61. if (unlikely(!new_skb))
  62. return -ENOMEM;
  63. bt_cb(new_skb)->pkt_type = hci_skb_pkt_type(skb);
  64. kfree_skb(skb);
  65. skb = new_skb;
  66. if (!IS_ALIGNED((unsigned long)skb->data, RSI_DMA_ALIGN)) {
  67. u8 *skb_data = skb->data;
  68. int skb_len = skb->len;
  69. skb_push(skb, RSI_DMA_ALIGN);
  70. skb_pull(skb, PTR_ALIGN(skb->data,
  71. RSI_DMA_ALIGN) - skb->data);
  72. memmove(skb->data, skb_data, skb_len);
  73. skb_trim(skb, skb_len);
  74. }
  75. }
  76. return h_adapter->proto_ops->coex_send_pkt(h_adapter->priv, skb,
  77. RSI_BT_Q);
  78. }
  79. static int rsi_hci_recv_pkt(void *priv, const u8 *pkt)
  80. {
  81. struct rsi_hci_adapter *h_adapter = priv;
  82. struct hci_dev *hdev = h_adapter->hdev;
  83. struct sk_buff *skb;
  84. int pkt_len = get_unaligned_le16(pkt) & 0x0fff;
  85. skb = dev_alloc_skb(pkt_len);
  86. if (!skb)
  87. return -ENOMEM;
  88. memcpy(skb->data, pkt + RSI_FRAME_DESC_SIZE, pkt_len);
  89. skb_put(skb, pkt_len);
  90. h_adapter->hdev->stat.byte_rx += skb->len;
  91. hci_skb_pkt_type(skb) = pkt[14];
  92. return hci_recv_frame(hdev, skb);
  93. }
  94. static int rsi_hci_attach(void *priv, struct rsi_proto_ops *ops)
  95. {
  96. struct rsi_hci_adapter *h_adapter = NULL;
  97. struct hci_dev *hdev;
  98. int err = 0;
  99. h_adapter = kzalloc(sizeof(*h_adapter), GFP_KERNEL);
  100. if (!h_adapter)
  101. return -ENOMEM;
  102. h_adapter->priv = priv;
  103. ops->set_bt_context(priv, h_adapter);
  104. h_adapter->proto_ops = ops;
  105. hdev = hci_alloc_dev();
  106. if (!hdev) {
  107. BT_ERR("Failed to alloc HCI device");
  108. goto err;
  109. }
  110. h_adapter->hdev = hdev;
  111. if (ops->get_host_intf(priv) == RSI_HOST_INTF_SDIO)
  112. hdev->bus = HCI_SDIO;
  113. else
  114. hdev->bus = HCI_USB;
  115. hci_set_drvdata(hdev, h_adapter);
  116. hdev->dev_type = HCI_PRIMARY;
  117. hdev->open = rsi_hci_open;
  118. hdev->close = rsi_hci_close;
  119. hdev->flush = rsi_hci_flush;
  120. hdev->send = rsi_hci_send_pkt;
  121. err = hci_register_dev(hdev);
  122. if (err < 0) {
  123. BT_ERR("HCI registration failed with errcode %d", err);
  124. hci_free_dev(hdev);
  125. goto err;
  126. }
  127. return 0;
  128. err:
  129. h_adapter->hdev = NULL;
  130. kfree(h_adapter);
  131. return -EINVAL;
  132. }
  133. static void rsi_hci_detach(void *priv)
  134. {
  135. struct rsi_hci_adapter *h_adapter = priv;
  136. struct hci_dev *hdev;
  137. if (!h_adapter)
  138. return;
  139. hdev = h_adapter->hdev;
  140. if (hdev) {
  141. hci_unregister_dev(hdev);
  142. hci_free_dev(hdev);
  143. h_adapter->hdev = NULL;
  144. }
  145. kfree(h_adapter);
  146. }
  147. const struct rsi_mod_ops rsi_bt_ops = {
  148. .attach = rsi_hci_attach,
  149. .detach = rsi_hci_detach,
  150. .recv_pkt = rsi_hci_recv_pkt,
  151. };
  152. EXPORT_SYMBOL(rsi_bt_ops);
  153. static int rsi_91x_bt_module_init(void)
  154. {
  155. return 0;
  156. }
  157. static void rsi_91x_bt_module_exit(void)
  158. {
  159. return;
  160. }
  161. module_init(rsi_91x_bt_module_init);
  162. module_exit(rsi_91x_bt_module_exit);
  163. MODULE_AUTHOR("Redpine Signals Inc");
  164. MODULE_DESCRIPTION("RSI BT driver");
  165. MODULE_SUPPORTED_DEVICE("RSI-BT");
  166. MODULE_LICENSE("Dual BSD/GPL");