smd.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2015, Sony Mobile Communications Inc.
  3. * Copyright (c) 2013, The Linux Foundation. All rights reserved.
  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/skbuff.h>
  16. #include <linux/rpmsg.h>
  17. #include "qrtr.h"
  18. struct qrtr_smd_dev {
  19. struct qrtr_endpoint ep;
  20. struct rpmsg_endpoint *channel;
  21. struct device *dev;
  22. };
  23. /* from smd to qrtr */
  24. static int qcom_smd_qrtr_callback(struct rpmsg_device *rpdev,
  25. void *data, int len, void *priv, u32 addr)
  26. {
  27. struct qrtr_smd_dev *qdev = dev_get_drvdata(&rpdev->dev);
  28. int rc;
  29. if (!qdev)
  30. return -EAGAIN;
  31. rc = qrtr_endpoint_post(&qdev->ep, data, len);
  32. if (rc == -EINVAL) {
  33. dev_err(qdev->dev, "invalid ipcrouter packet\n");
  34. /* return 0 to let smd drop the packet */
  35. rc = 0;
  36. }
  37. return rc;
  38. }
  39. /* from qrtr to smd */
  40. static int qcom_smd_qrtr_send(struct qrtr_endpoint *ep, struct sk_buff *skb)
  41. {
  42. struct qrtr_smd_dev *qdev = container_of(ep, struct qrtr_smd_dev, ep);
  43. int rc;
  44. rc = skb_linearize(skb);
  45. if (rc)
  46. goto out;
  47. rc = rpmsg_send(qdev->channel, skb->data, skb->len);
  48. out:
  49. if (rc)
  50. kfree_skb(skb);
  51. else
  52. consume_skb(skb);
  53. return rc;
  54. }
  55. static int qcom_smd_qrtr_probe(struct rpmsg_device *rpdev)
  56. {
  57. struct qrtr_smd_dev *qdev;
  58. int rc;
  59. qdev = devm_kzalloc(&rpdev->dev, sizeof(*qdev), GFP_KERNEL);
  60. if (!qdev)
  61. return -ENOMEM;
  62. qdev->channel = rpdev->ept;
  63. qdev->dev = &rpdev->dev;
  64. qdev->ep.xmit = qcom_smd_qrtr_send;
  65. rc = qrtr_endpoint_register(&qdev->ep, QRTR_EP_NID_AUTO);
  66. if (rc)
  67. return rc;
  68. dev_set_drvdata(&rpdev->dev, qdev);
  69. dev_dbg(&rpdev->dev, "Qualcomm SMD QRTR driver probed\n");
  70. return 0;
  71. }
  72. static void qcom_smd_qrtr_remove(struct rpmsg_device *rpdev)
  73. {
  74. struct qrtr_smd_dev *qdev = dev_get_drvdata(&rpdev->dev);
  75. qrtr_endpoint_unregister(&qdev->ep);
  76. dev_set_drvdata(&rpdev->dev, NULL);
  77. }
  78. static const struct rpmsg_device_id qcom_smd_qrtr_smd_match[] = {
  79. { "IPCRTR" },
  80. {}
  81. };
  82. static struct rpmsg_driver qcom_smd_qrtr_driver = {
  83. .probe = qcom_smd_qrtr_probe,
  84. .remove = qcom_smd_qrtr_remove,
  85. .callback = qcom_smd_qrtr_callback,
  86. .id_table = qcom_smd_qrtr_smd_match,
  87. .drv = {
  88. .name = "qcom_smd_qrtr",
  89. },
  90. };
  91. module_rpmsg_driver(qcom_smd_qrtr_driver);
  92. MODULE_ALIAS("rpmsg:IPCRTR");
  93. MODULE_DESCRIPTION("Qualcomm IPC-Router SMD interface driver");
  94. MODULE_LICENSE("GPL v2");