heartbeat.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Simple heartbeat STM source driver
  3. * Copyright (c) 2016, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * Heartbeat STM source will send repetitive messages over STM devices to a
  15. * trace host.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/hrtimer.h>
  20. #include <linux/slab.h>
  21. #include <linux/stm.h>
  22. #define STM_HEARTBEAT_MAX 32
  23. static int nr_devs = 4;
  24. static int interval_ms = 10;
  25. module_param(nr_devs, int, 0600);
  26. module_param(interval_ms, int, 0600);
  27. static struct stm_heartbeat {
  28. struct stm_source_data data;
  29. struct hrtimer hrtimer;
  30. unsigned int active;
  31. } stm_heartbeat[STM_HEARTBEAT_MAX];
  32. static unsigned int nr_instances;
  33. static const char str[] = "heartbeat stm source driver is here to serve you";
  34. static enum hrtimer_restart stm_heartbeat_hrtimer_handler(struct hrtimer *hr)
  35. {
  36. struct stm_heartbeat *heartbeat = container_of(hr, struct stm_heartbeat,
  37. hrtimer);
  38. stm_source_write(&heartbeat->data, 0, str, sizeof str);
  39. if (heartbeat->active)
  40. hrtimer_forward_now(hr, ms_to_ktime(interval_ms));
  41. return heartbeat->active ? HRTIMER_RESTART : HRTIMER_NORESTART;
  42. }
  43. static int stm_heartbeat_link(struct stm_source_data *data)
  44. {
  45. struct stm_heartbeat *heartbeat =
  46. container_of(data, struct stm_heartbeat, data);
  47. heartbeat->active = 1;
  48. hrtimer_start(&heartbeat->hrtimer, ms_to_ktime(interval_ms),
  49. HRTIMER_MODE_ABS);
  50. return 0;
  51. }
  52. static void stm_heartbeat_unlink(struct stm_source_data *data)
  53. {
  54. struct stm_heartbeat *heartbeat =
  55. container_of(data, struct stm_heartbeat, data);
  56. heartbeat->active = 0;
  57. hrtimer_cancel(&heartbeat->hrtimer);
  58. }
  59. static int stm_heartbeat_init(void)
  60. {
  61. int i, ret = -ENOMEM, __nr_instances = ACCESS_ONCE(nr_devs);
  62. if (__nr_instances < 0 || __nr_instances > STM_HEARTBEAT_MAX)
  63. return -EINVAL;
  64. for (i = 0; i < __nr_instances; i++) {
  65. stm_heartbeat[i].data.name =
  66. kasprintf(GFP_KERNEL, "heartbeat.%d", i);
  67. if (!stm_heartbeat[i].data.name)
  68. goto fail_unregister;
  69. stm_heartbeat[i].data.nr_chans = 1;
  70. stm_heartbeat[i].data.link = stm_heartbeat_link;
  71. stm_heartbeat[i].data.unlink = stm_heartbeat_unlink;
  72. hrtimer_init(&stm_heartbeat[i].hrtimer, CLOCK_MONOTONIC,
  73. HRTIMER_MODE_ABS);
  74. stm_heartbeat[i].hrtimer.function =
  75. stm_heartbeat_hrtimer_handler;
  76. ret = stm_source_register_device(NULL, &stm_heartbeat[i].data);
  77. if (ret)
  78. goto fail_free;
  79. }
  80. nr_instances = __nr_instances;
  81. return 0;
  82. fail_unregister:
  83. for (i--; i >= 0; i--) {
  84. stm_source_unregister_device(&stm_heartbeat[i].data);
  85. fail_free:
  86. kfree(stm_heartbeat[i].data.name);
  87. }
  88. return ret;
  89. }
  90. static void stm_heartbeat_exit(void)
  91. {
  92. int i;
  93. for (i = 0; i < nr_instances; i++) {
  94. stm_source_unregister_device(&stm_heartbeat[i].data);
  95. kfree(stm_heartbeat[i].data.name);
  96. }
  97. }
  98. module_init(stm_heartbeat_init);
  99. module_exit(stm_heartbeat_exit);
  100. MODULE_LICENSE("GPL v2");
  101. MODULE_DESCRIPTION("stm_heartbeat driver");
  102. MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");