GNU Linux-libre 4.9.309-gnu1
[releases.git] / drivers / hwtracing / stm / heartbeat.c
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
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/hrtimer.h>
21 #include <linux/slab.h>
22 #include <linux/stm.h>
23
24 #define STM_HEARTBEAT_MAX       32
25
26 static int nr_devs = 4;
27 static int interval_ms = 10;
28
29 module_param(nr_devs, int, 0400);
30 module_param(interval_ms, int, 0600);
31
32 static struct stm_heartbeat {
33         struct stm_source_data  data;
34         struct hrtimer          hrtimer;
35         unsigned int            active;
36 } stm_heartbeat[STM_HEARTBEAT_MAX];
37
38 static const char str[] = "heartbeat stm source driver is here to serve you";
39
40 static enum hrtimer_restart stm_heartbeat_hrtimer_handler(struct hrtimer *hr)
41 {
42         struct stm_heartbeat *heartbeat = container_of(hr, struct stm_heartbeat,
43                                                        hrtimer);
44
45         stm_source_write(&heartbeat->data, 0, str, sizeof str);
46         if (heartbeat->active)
47                 hrtimer_forward_now(hr, ms_to_ktime(interval_ms));
48
49         return heartbeat->active ? HRTIMER_RESTART : HRTIMER_NORESTART;
50 }
51
52 static int stm_heartbeat_link(struct stm_source_data *data)
53 {
54         struct stm_heartbeat *heartbeat =
55                 container_of(data, struct stm_heartbeat, data);
56
57         heartbeat->active = 1;
58         hrtimer_start(&heartbeat->hrtimer, ms_to_ktime(interval_ms),
59                       HRTIMER_MODE_ABS);
60
61         return 0;
62 }
63
64 static void stm_heartbeat_unlink(struct stm_source_data *data)
65 {
66         struct stm_heartbeat *heartbeat =
67                 container_of(data, struct stm_heartbeat, data);
68
69         heartbeat->active = 0;
70         hrtimer_cancel(&heartbeat->hrtimer);
71 }
72
73 static int stm_heartbeat_init(void)
74 {
75         int i, ret;
76
77         if (nr_devs < 0 || nr_devs > STM_HEARTBEAT_MAX)
78                 return -EINVAL;
79
80         for (i = 0; i < nr_devs; i++) {
81                 stm_heartbeat[i].data.name =
82                         kasprintf(GFP_KERNEL, "heartbeat.%d", i);
83                 if (!stm_heartbeat[i].data.name) {
84                         ret = -ENOMEM;
85                         goto fail_unregister;
86                 }
87
88                 stm_heartbeat[i].data.nr_chans  = 1;
89                 stm_heartbeat[i].data.link              = stm_heartbeat_link;
90                 stm_heartbeat[i].data.unlink    = stm_heartbeat_unlink;
91                 hrtimer_init(&stm_heartbeat[i].hrtimer, CLOCK_MONOTONIC,
92                              HRTIMER_MODE_ABS);
93                 stm_heartbeat[i].hrtimer.function =
94                         stm_heartbeat_hrtimer_handler;
95
96                 ret = stm_source_register_device(NULL, &stm_heartbeat[i].data);
97                 if (ret)
98                         goto fail_free;
99         }
100
101         return 0;
102
103 fail_unregister:
104         for (i--; i >= 0; i--) {
105                 stm_source_unregister_device(&stm_heartbeat[i].data);
106 fail_free:
107                 kfree(stm_heartbeat[i].data.name);
108         }
109
110         return ret;
111 }
112
113 static void stm_heartbeat_exit(void)
114 {
115         int i;
116
117         for (i = 0; i < nr_devs; i++) {
118                 stm_source_unregister_device(&stm_heartbeat[i].data);
119                 kfree(stm_heartbeat[i].data.name);
120         }
121 }
122
123 module_init(stm_heartbeat_init);
124 module_exit(stm_heartbeat_exit);
125
126 MODULE_LICENSE("GPL v2");
127 MODULE_DESCRIPTION("stm_heartbeat driver");
128 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");