rpmsg_client_sample.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Remote processor messaging - sample client driver
  4. *
  5. * Copyright (C) 2011 Texas Instruments, Inc.
  6. * Copyright (C) 2011 Google, Inc.
  7. *
  8. * Ohad Ben-Cohen <ohad@wizery.com>
  9. * Brian Swetland <swetland@google.com>
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/rpmsg.h>
  14. #define MSG "hello world!"
  15. static int count = 100;
  16. module_param(count, int, 0644);
  17. struct instance_data {
  18. int rx_count;
  19. };
  20. static int rpmsg_sample_cb(struct rpmsg_device *rpdev, void *data, int len,
  21. void *priv, u32 src)
  22. {
  23. int ret;
  24. struct instance_data *idata = dev_get_drvdata(&rpdev->dev);
  25. dev_info(&rpdev->dev, "incoming msg %d (src: 0x%x)\n",
  26. ++idata->rx_count, src);
  27. print_hex_dump_debug(__func__, DUMP_PREFIX_NONE, 16, 1, data, len,
  28. true);
  29. /* samples should not live forever */
  30. if (idata->rx_count >= count) {
  31. dev_info(&rpdev->dev, "goodbye!\n");
  32. return 0;
  33. }
  34. /* send a new message now */
  35. ret = rpmsg_send(rpdev->ept, MSG, strlen(MSG));
  36. if (ret)
  37. dev_err(&rpdev->dev, "rpmsg_send failed: %d\n", ret);
  38. return 0;
  39. }
  40. static int rpmsg_sample_probe(struct rpmsg_device *rpdev)
  41. {
  42. int ret;
  43. struct instance_data *idata;
  44. dev_info(&rpdev->dev, "new channel: 0x%x -> 0x%x!\n",
  45. rpdev->src, rpdev->dst);
  46. idata = devm_kzalloc(&rpdev->dev, sizeof(*idata), GFP_KERNEL);
  47. if (!idata)
  48. return -ENOMEM;
  49. dev_set_drvdata(&rpdev->dev, idata);
  50. /* send a message to our remote processor */
  51. ret = rpmsg_send(rpdev->ept, MSG, strlen(MSG));
  52. if (ret) {
  53. dev_err(&rpdev->dev, "rpmsg_send failed: %d\n", ret);
  54. return ret;
  55. }
  56. return 0;
  57. }
  58. static void rpmsg_sample_remove(struct rpmsg_device *rpdev)
  59. {
  60. dev_info(&rpdev->dev, "rpmsg sample client driver is removed\n");
  61. }
  62. static struct rpmsg_device_id rpmsg_driver_sample_id_table[] = {
  63. { .name = "rpmsg-client-sample" },
  64. { },
  65. };
  66. MODULE_DEVICE_TABLE(rpmsg, rpmsg_driver_sample_id_table);
  67. static struct rpmsg_driver rpmsg_sample_client = {
  68. .drv.name = KBUILD_MODNAME,
  69. .id_table = rpmsg_driver_sample_id_table,
  70. .probe = rpmsg_sample_probe,
  71. .callback = rpmsg_sample_cb,
  72. .remove = rpmsg_sample_remove,
  73. };
  74. module_rpmsg_driver(rpmsg_sample_client);
  75. MODULE_DESCRIPTION("Remote processor messaging sample client driver");
  76. MODULE_LICENSE("GPL v2");