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