btqcomsmd.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. kfree_skb(skb);
  73. return ret;
  74. }
  75. static int btqcomsmd_open(struct hci_dev *hdev)
  76. {
  77. return 0;
  78. }
  79. static int btqcomsmd_close(struct hci_dev *hdev)
  80. {
  81. return 0;
  82. }
  83. static int btqcomsmd_probe(struct platform_device *pdev)
  84. {
  85. struct btqcomsmd *btq;
  86. struct hci_dev *hdev;
  87. void *wcnss;
  88. int ret;
  89. btq = devm_kzalloc(&pdev->dev, sizeof(*btq), GFP_KERNEL);
  90. if (!btq)
  91. return -ENOMEM;
  92. wcnss = dev_get_drvdata(pdev->dev.parent);
  93. btq->acl_channel = qcom_wcnss_open_channel(wcnss, "APPS_RIVA_BT_ACL",
  94. btqcomsmd_acl_callback);
  95. if (IS_ERR(btq->acl_channel))
  96. return PTR_ERR(btq->acl_channel);
  97. btq->cmd_channel = qcom_wcnss_open_channel(wcnss, "APPS_RIVA_BT_CMD",
  98. btqcomsmd_cmd_callback);
  99. if (IS_ERR(btq->cmd_channel))
  100. return PTR_ERR(btq->cmd_channel);
  101. qcom_smd_set_drvdata(btq->acl_channel, btq);
  102. qcom_smd_set_drvdata(btq->cmd_channel, btq);
  103. hdev = hci_alloc_dev();
  104. if (!hdev)
  105. return -ENOMEM;
  106. hci_set_drvdata(hdev, btq);
  107. btq->hdev = hdev;
  108. SET_HCIDEV_DEV(hdev, &pdev->dev);
  109. hdev->bus = HCI_SMD;
  110. hdev->open = btqcomsmd_open;
  111. hdev->close = btqcomsmd_close;
  112. hdev->send = btqcomsmd_send;
  113. hdev->set_bdaddr = qca_set_bdaddr_rome;
  114. ret = hci_register_dev(hdev);
  115. if (ret < 0) {
  116. hci_free_dev(hdev);
  117. return ret;
  118. }
  119. platform_set_drvdata(pdev, btq);
  120. return 0;
  121. }
  122. static int btqcomsmd_remove(struct platform_device *pdev)
  123. {
  124. struct btqcomsmd *btq = platform_get_drvdata(pdev);
  125. hci_unregister_dev(btq->hdev);
  126. hci_free_dev(btq->hdev);
  127. return 0;
  128. }
  129. static const struct of_device_id btqcomsmd_of_match[] = {
  130. { .compatible = "qcom,wcnss-bt", },
  131. { },
  132. };
  133. static struct platform_driver btqcomsmd_driver = {
  134. .probe = btqcomsmd_probe,
  135. .remove = btqcomsmd_remove,
  136. .driver = {
  137. .name = "btqcomsmd",
  138. .of_match_table = btqcomsmd_of_match,
  139. },
  140. };
  141. module_platform_driver(btqcomsmd_driver);
  142. MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
  143. MODULE_DESCRIPTION("Qualcomm SMD HCI driver");
  144. MODULE_LICENSE("GPL v2");