heartbeat.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Simple heartbeat STM source driver
  4. * Copyright (c) 2016, Intel Corporation.
  5. *
  6. * Heartbeat STM source will send repetitive messages over STM devices to a
  7. * trace host.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/hrtimer.h>
  12. #include <linux/slab.h>
  13. #include <linux/stm.h>
  14. #define STM_HEARTBEAT_MAX 32
  15. static int nr_devs = 4;
  16. static int interval_ms = 10;
  17. module_param(nr_devs, int, 0400);
  18. module_param(interval_ms, int, 0600);
  19. static struct stm_heartbeat {
  20. struct stm_source_data data;
  21. struct hrtimer hrtimer;
  22. unsigned int active;
  23. } stm_heartbeat[STM_HEARTBEAT_MAX];
  24. static const char str[] = "heartbeat stm source driver is here to serve you";
  25. static enum hrtimer_restart stm_heartbeat_hrtimer_handler(struct hrtimer *hr)
  26. {
  27. struct stm_heartbeat *heartbeat = container_of(hr, struct stm_heartbeat,
  28. hrtimer);
  29. stm_source_write(&heartbeat->data, 0, str, sizeof str);
  30. if (heartbeat->active)
  31. hrtimer_forward_now(hr, ms_to_ktime(interval_ms));
  32. return heartbeat->active ? HRTIMER_RESTART : HRTIMER_NORESTART;
  33. }
  34. static int stm_heartbeat_link(struct stm_source_data *data)
  35. {
  36. struct stm_heartbeat *heartbeat =
  37. container_of(data, struct stm_heartbeat, data);
  38. heartbeat->active = 1;
  39. hrtimer_start(&heartbeat->hrtimer, ms_to_ktime(interval_ms),
  40. HRTIMER_MODE_ABS);
  41. return 0;
  42. }
  43. static void stm_heartbeat_unlink(struct stm_source_data *data)
  44. {
  45. struct stm_heartbeat *heartbeat =
  46. container_of(data, struct stm_heartbeat, data);
  47. heartbeat->active = 0;
  48. hrtimer_cancel(&heartbeat->hrtimer);
  49. }
  50. static int stm_heartbeat_init(void)
  51. {
  52. int i, ret = -ENOMEM;
  53. if (nr_devs < 0 || nr_devs > STM_HEARTBEAT_MAX)
  54. return -EINVAL;
  55. for (i = 0; i < nr_devs; i++) {
  56. stm_heartbeat[i].data.name =
  57. kasprintf(GFP_KERNEL, "heartbeat.%d", i);
  58. if (!stm_heartbeat[i].data.name)
  59. goto fail_unregister;
  60. stm_heartbeat[i].data.nr_chans = 1;
  61. stm_heartbeat[i].data.link = stm_heartbeat_link;
  62. stm_heartbeat[i].data.unlink = stm_heartbeat_unlink;
  63. hrtimer_init(&stm_heartbeat[i].hrtimer, CLOCK_MONOTONIC,
  64. HRTIMER_MODE_ABS);
  65. stm_heartbeat[i].hrtimer.function =
  66. stm_heartbeat_hrtimer_handler;
  67. ret = stm_source_register_device(NULL, &stm_heartbeat[i].data);
  68. if (ret)
  69. goto fail_free;
  70. }
  71. return 0;
  72. fail_unregister:
  73. for (i--; i >= 0; i--) {
  74. stm_source_unregister_device(&stm_heartbeat[i].data);
  75. fail_free:
  76. kfree(stm_heartbeat[i].data.name);
  77. }
  78. return ret;
  79. }
  80. static void stm_heartbeat_exit(void)
  81. {
  82. int i;
  83. for (i = 0; i < nr_devs; i++) {
  84. stm_source_unregister_device(&stm_heartbeat[i].data);
  85. kfree(stm_heartbeat[i].data.name);
  86. }
  87. }
  88. module_init(stm_heartbeat_init);
  89. module_exit(stm_heartbeat_exit);
  90. MODULE_LICENSE("GPL v2");
  91. MODULE_DESCRIPTION("stm_heartbeat driver");
  92. MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");