btqcomsmd.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2016, Linaro Ltd.
  3. * Copyright (c) 2015, Sony Mobile Communications Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 and
  7. * only version 2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/soc/qcom/smd.h>
  17. #include <linux/soc/qcom/wcnss_ctrl.h>
  18. #include <linux/platform_device.h>
  19. #include <net/bluetooth/bluetooth.h>
  20. #include <net/bluetooth/hci_core.h>
  21. #include "btqca.h"
  22. struct btqcomsmd {
  23. struct hci_dev *hdev;
  24. struct qcom_smd_channel *acl_channel;
  25. struct qcom_smd_channel *cmd_channel;
  26. };
  27. static int btqcomsmd_recv(struct hci_dev *hdev, unsigned int type,
  28. const void *data, size_t count)
  29. {
  30. struct sk_buff *skb;
  31. /* Use GFP_ATOMIC as we're in IRQ context */
  32. skb = bt_skb_alloc(count, GFP_ATOMIC);
  33. if (!skb) {
  34. hdev->stat.err_rx++;
  35. return -ENOMEM;
  36. }
  37. hci_skb_pkt_type(skb) = type;
  38. memcpy(skb_put(skb, count), data, count);
  39. return hci_recv_frame(hdev, skb);
  40. }
  41. static int btqcomsmd_acl_callback(struct qcom_smd_channel *channel,
  42. const void *data, size_t count)
  43. {
  44. struct btqcomsmd *btq = qcom_smd_get_drvdata(channel);
  45. btq->hdev->stat.byte_rx += count;
  46. return btqcomsmd_recv(btq->hdev, HCI_ACLDATA_PKT, data, count);
  47. }
  48. static int btqcomsmd_cmd_callback(struct qcom_smd_channel *channel,
  49. const void *data, size_t count)
  50. {
  51. struct btqcomsmd *btq = qcom_smd_get_drvdata(channel);
  52. return btqcomsmd_recv(btq->hdev, HCI_EVENT_PKT, data, count);
  53. }
  54. static int btqcomsmd_send(struct hci_dev *hdev, struct sk_buff *skb)
  55. {
  56. struct btqcomsmd *btq = hci_get_drvdata(hdev);
  57. int ret;
  58. switch (hci_skb_pkt_type(skb)) {
  59. case HCI_ACLDATA_PKT:
  60. ret = qcom_smd_send(btq->acl_channel, skb->data, skb->len);
  61. hdev->stat.acl_tx++;
  62. hdev->stat.byte_tx += skb->len;
  63. break;
  64. case HCI_COMMAND_PKT:
  65. ret = qcom_smd_send(btq->cmd_channel, skb->data, skb->len);
  66. hdev->stat.cmd_tx++;
  67. break;
  68. default:
  69. ret = -EILSEQ;
  70. break;
  71. }
  72. if (!ret)
  73. kfree_skb(skb);
  74. return ret;
  75. }
  76. static int btqcomsmd_open(struct hci_dev *hdev)
  77. {
  78. return 0;
  79. }
  80. static int btqcomsmd_close(struct hci_dev *hdev)
  81. {
  82. return 0;
  83. }
  84. static int btqcomsmd_probe(struct platform_device *pdev)
  85. {
  86. struct btqcomsmd *btq;
  87. struct hci_dev *hdev;
  88. void *wcnss;
  89. int ret;
  90. btq = devm_kzalloc(&pdev->dev, sizeof(*btq), GFP_KERNEL);
  91. if (!btq)
  92. return -ENOMEM;
  93. wcnss = dev_get_drvdata(pdev->dev.parent);
  94. btq->acl_channel = qcom_wcnss_open_channel(wcnss, "APPS_RIVA_BT_ACL",
  95. btqcomsmd_acl_callback);
  96. if (IS_ERR(btq->acl_channel))
  97. return PTR_ERR(btq->acl_channel);
  98. btq->cmd_channel = qcom_wcnss_open_channel(wcnss, "APPS_RIVA_BT_CMD",
  99. btqcomsmd_cmd_callback);
  100. if (IS_ERR(btq->cmd_channel))
  101. return PTR_ERR(btq->cmd_channel);
  102. qcom_smd_set_drvdata(btq->acl_channel, btq);
  103. qcom_smd_set_drvdata(btq->cmd_channel, btq);
  104. hdev = hci_alloc_dev();
  105. if (!hdev)
  106. return -ENOMEM;
  107. hci_set_drvdata(hdev, btq);
  108. btq->hdev = hdev;
  109. SET_HCIDEV_DEV(hdev, &pdev->dev);
  110. hdev->bus = HCI_SMD;
  111. hdev->open = btqcomsmd_open;
  112. hdev->close = btqcomsmd_close;
  113. hdev->send = btqcomsmd_send;
  114. hdev->set_bdaddr = qca_set_bdaddr_rome;
  115. ret = hci_register_dev(hdev);
  116. if (ret < 0) {
  117. hci_free_dev(hdev);
  118. return ret;
  119. }
  120. platform_set_drvdata(pdev, btq);
  121. return 0;
  122. }
  123. static int btqcomsmd_remove(struct platform_device *pdev)
  124. {
  125. struct btqcomsmd *btq = platform_get_drvdata(pdev);
  126. hci_unregister_dev(btq->hdev);
  127. hci_free_dev(btq->hdev);
  128. return 0;
  129. }
  130. static const struct of_device_id btqcomsmd_of_match[] = {
  131. { .compatible = "qcom,wcnss-bt", },
  132. { },
  133. };
  134. static struct platform_driver btqcomsmd_driver = {
  135. .probe = btqcomsmd_probe,
  136. .remove = btqcomsmd_remove,
  137. .driver = {
  138. .name = "btqcomsmd",
  139. .of_match_table = btqcomsmd_of_match,
  140. },
  141. };
  142. module_platform_driver(btqcomsmd_driver);
  143. MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
  144. MODULE_DESCRIPTION("Qualcomm SMD HCI driver");
  145. MODULE_LICENSE("GPL v2");