btrsi.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #define RSI_DMA_ALIGN 8
  23. #define RSI_FRAME_DESC_SIZE 16
  24. #define RSI_HEADROOM_FOR_BT_HAL (RSI_FRAME_DESC_SIZE + RSI_DMA_ALIGN)
  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. if (!IS_ALIGNED((unsigned long)skb->data, RSI_DMA_ALIGN)) {
  66. u8 *skb_data = skb->data;
  67. int skb_len = skb->len;
  68. skb_push(skb, RSI_DMA_ALIGN);
  69. skb_pull(skb, PTR_ALIGN(skb->data,
  70. RSI_DMA_ALIGN) - skb->data);
  71. memmove(skb->data, skb_data, skb_len);
  72. skb_trim(skb, skb_len);
  73. }
  74. }
  75. return h_adapter->proto_ops->coex_send_pkt(h_adapter->priv, skb,
  76. RSI_BT_Q);
  77. }
  78. static int rsi_hci_recv_pkt(void *priv, const u8 *pkt)
  79. {
  80. struct rsi_hci_adapter *h_adapter = priv;
  81. struct hci_dev *hdev = h_adapter->hdev;
  82. struct sk_buff *skb;
  83. int pkt_len = get_unaligned_le16(pkt) & 0x0fff;
  84. skb = dev_alloc_skb(pkt_len);
  85. if (!skb)
  86. return -ENOMEM;
  87. memcpy(skb->data, pkt + RSI_FRAME_DESC_SIZE, pkt_len);
  88. skb_put(skb, pkt_len);
  89. h_adapter->hdev->stat.byte_rx += skb->len;
  90. hci_skb_pkt_type(skb) = pkt[14];
  91. return hci_recv_frame(hdev, skb);
  92. }
  93. static int rsi_hci_attach(void *priv, struct rsi_proto_ops *ops)
  94. {
  95. struct rsi_hci_adapter *h_adapter = NULL;
  96. struct hci_dev *hdev;
  97. int err = 0;
  98. h_adapter = kzalloc(sizeof(*h_adapter), GFP_KERNEL);
  99. if (!h_adapter)
  100. return -ENOMEM;
  101. h_adapter->priv = priv;
  102. ops->set_bt_context(priv, h_adapter);
  103. h_adapter->proto_ops = ops;
  104. hdev = hci_alloc_dev();
  105. if (!hdev) {
  106. BT_ERR("Failed to alloc HCI device");
  107. goto err;
  108. }
  109. h_adapter->hdev = hdev;
  110. if (ops->get_host_intf(priv) == RSI_HOST_INTF_SDIO)
  111. hdev->bus = HCI_SDIO;
  112. else
  113. hdev->bus = HCI_USB;
  114. hci_set_drvdata(hdev, h_adapter);
  115. hdev->dev_type = HCI_PRIMARY;
  116. hdev->open = rsi_hci_open;
  117. hdev->close = rsi_hci_close;
  118. hdev->flush = rsi_hci_flush;
  119. hdev->send = rsi_hci_send_pkt;
  120. err = hci_register_dev(hdev);
  121. if (err < 0) {
  122. BT_ERR("HCI registration failed with errcode %d", err);
  123. hci_free_dev(hdev);
  124. goto err;
  125. }
  126. return 0;
  127. err:
  128. h_adapter->hdev = NULL;
  129. kfree(h_adapter);
  130. return -EINVAL;
  131. }
  132. static void rsi_hci_detach(void *priv)
  133. {
  134. struct rsi_hci_adapter *h_adapter = priv;
  135. struct hci_dev *hdev;
  136. if (!h_adapter)
  137. return;
  138. hdev = h_adapter->hdev;
  139. if (hdev) {
  140. hci_unregister_dev(hdev);
  141. hci_free_dev(hdev);
  142. h_adapter->hdev = NULL;
  143. }
  144. kfree(h_adapter);
  145. }
  146. const struct rsi_mod_ops rsi_bt_ops = {
  147. .attach = rsi_hci_attach,
  148. .detach = rsi_hci_detach,
  149. .recv_pkt = rsi_hci_recv_pkt,
  150. };
  151. EXPORT_SYMBOL(rsi_bt_ops);
  152. static int rsi_91x_bt_module_init(void)
  153. {
  154. return 0;
  155. }
  156. static void rsi_91x_bt_module_exit(void)
  157. {
  158. return;
  159. }
  160. module_init(rsi_91x_bt_module_init);
  161. module_exit(rsi_91x_bt_module_exit);
  162. MODULE_AUTHOR("Redpine Signals Inc");
  163. MODULE_DESCRIPTION("RSI BT driver");
  164. MODULE_LICENSE("Dual BSD/GPL");