rpmsg_client_sample.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Remote processor messaging - sample client driver
  3. *
  4. * Copyright (C) 2011 Texas Instruments, Inc.
  5. * Copyright (C) 2011 Google, Inc.
  6. *
  7. * Ohad Ben-Cohen <[email protected]>
  8. * Brian Swetland <[email protected]>
  9. *
  10. * This software is licensed under the terms of the GNU General Public
  11. * License version 2, as published by the Free Software Foundation, and
  12. * may be copied, distributed, and modified under those terms.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/rpmsg.h>
  22. #define MSG "hello world!"
  23. #define MSG_LIMIT 100
  24. struct instance_data {
  25. int rx_count;
  26. };
  27. static int rpmsg_sample_cb(struct rpmsg_device *rpdev, void *data, int len,
  28. void *priv, u32 src)
  29. {
  30. int ret;
  31. struct instance_data *idata = dev_get_drvdata(&rpdev->dev);
  32. dev_info(&rpdev->dev, "incoming msg %d (src: 0x%x)\n",
  33. ++idata->rx_count, src);
  34. print_hex_dump(KERN_DEBUG, __func__, DUMP_PREFIX_NONE, 16, 1,
  35. data, len, true);
  36. /* samples should not live forever */
  37. if (idata->rx_count >= MSG_LIMIT) {
  38. dev_info(&rpdev->dev, "goodbye!\n");
  39. return 0;
  40. }
  41. /* send a new message now */
  42. ret = rpmsg_send(rpdev->ept, MSG, strlen(MSG));
  43. if (ret)
  44. dev_err(&rpdev->dev, "rpmsg_send failed: %d\n", ret);
  45. return 0;
  46. }
  47. static int rpmsg_sample_probe(struct rpmsg_device *rpdev)
  48. {
  49. int ret;
  50. struct instance_data *idata;
  51. dev_info(&rpdev->dev, "new channel: 0x%x -> 0x%x!\n",
  52. rpdev->src, rpdev->dst);
  53. idata = devm_kzalloc(&rpdev->dev, sizeof(*idata), GFP_KERNEL);
  54. if (!idata)
  55. return -ENOMEM;
  56. dev_set_drvdata(&rpdev->dev, idata);
  57. /* send a message to our remote processor */
  58. ret = rpmsg_send(rpdev->ept, MSG, strlen(MSG));
  59. if (ret) {
  60. dev_err(&rpdev->dev, "rpmsg_send failed: %d\n", ret);
  61. return ret;
  62. }
  63. return 0;
  64. }
  65. static void rpmsg_sample_remove(struct rpmsg_device *rpdev)
  66. {
  67. dev_info(&rpdev->dev, "rpmsg sample client driver is removed\n");
  68. }
  69. static struct rpmsg_device_id rpmsg_driver_sample_id_table[] = {
  70. { .name = "rpmsg-client-sample" },
  71. { },
  72. };
  73. MODULE_DEVICE_TABLE(rpmsg, rpmsg_driver_sample_id_table);
  74. static struct rpmsg_driver rpmsg_sample_client = {
  75. .drv.name = KBUILD_MODNAME,
  76. .id_table = rpmsg_driver_sample_id_table,
  77. .probe = rpmsg_sample_probe,
  78. .callback = rpmsg_sample_cb,
  79. .remove = rpmsg_sample_remove,
  80. };
  81. module_rpmsg_driver(rpmsg_sample_client);
  82. MODULE_DESCRIPTION("Remote processor messaging sample client driver");
  83. MODULE_LICENSE("GPL v2");