heartbeat.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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, 0400);
  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 const char str[] = "heartbeat stm source driver is here to serve you";
  33. static enum hrtimer_restart stm_heartbeat_hrtimer_handler(struct hrtimer *hr)
  34. {
  35. struct stm_heartbeat *heartbeat = container_of(hr, struct stm_heartbeat,
  36. hrtimer);
  37. stm_source_write(&heartbeat->data, 0, str, sizeof str);
  38. if (heartbeat->active)
  39. hrtimer_forward_now(hr, ms_to_ktime(interval_ms));
  40. return heartbeat->active ? HRTIMER_RESTART : HRTIMER_NORESTART;
  41. }
  42. static int stm_heartbeat_link(struct stm_source_data *data)
  43. {
  44. struct stm_heartbeat *heartbeat =
  45. container_of(data, struct stm_heartbeat, data);
  46. heartbeat->active = 1;
  47. hrtimer_start(&heartbeat->hrtimer, ms_to_ktime(interval_ms),
  48. HRTIMER_MODE_ABS);
  49. return 0;
  50. }
  51. static void stm_heartbeat_unlink(struct stm_source_data *data)
  52. {
  53. struct stm_heartbeat *heartbeat =
  54. container_of(data, struct stm_heartbeat, data);
  55. heartbeat->active = 0;
  56. hrtimer_cancel(&heartbeat->hrtimer);
  57. }
  58. static int stm_heartbeat_init(void)
  59. {
  60. int i, ret = -ENOMEM;
  61. if (nr_devs < 0 || nr_devs > STM_HEARTBEAT_MAX)
  62. return -EINVAL;
  63. for (i = 0; i < nr_devs; i++) {
  64. stm_heartbeat[i].data.name =
  65. kasprintf(GFP_KERNEL, "heartbeat.%d", i);
  66. if (!stm_heartbeat[i].data.name)
  67. goto fail_unregister;
  68. stm_heartbeat[i].data.nr_chans = 1;
  69. stm_heartbeat[i].data.link = stm_heartbeat_link;
  70. stm_heartbeat[i].data.unlink = stm_heartbeat_unlink;
  71. hrtimer_init(&stm_heartbeat[i].hrtimer, CLOCK_MONOTONIC,
  72. HRTIMER_MODE_ABS);
  73. stm_heartbeat[i].hrtimer.function =
  74. stm_heartbeat_hrtimer_handler;
  75. ret = stm_source_register_device(NULL, &stm_heartbeat[i].data);
  76. if (ret)
  77. goto fail_free;
  78. }
  79. return 0;
  80. fail_unregister:
  81. for (i--; i >= 0; i--) {
  82. stm_source_unregister_device(&stm_heartbeat[i].data);
  83. fail_free:
  84. kfree(stm_heartbeat[i].data.name);
  85. }
  86. return ret;
  87. }
  88. static void stm_heartbeat_exit(void)
  89. {
  90. int i;
  91. for (i = 0; i < nr_devs; i++) {
  92. stm_source_unregister_device(&stm_heartbeat[i].data);
  93. kfree(stm_heartbeat[i].data.name);
  94. }
  95. }
  96. module_init(stm_heartbeat_init);
  97. module_exit(stm_heartbeat_exit);
  98. MODULE_LICENSE("GPL v2");
  99. MODULE_DESCRIPTION("stm_heartbeat driver");
  100. MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");