Repository navigation
Expand file tree
/
Copy pathbh_threaded.c
More file actions
266 lines (216 loc) · 6.08 KB
/
Copy pathbh_threaded.c
File metadata and controls
266 lines (216 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* bh_threaded.c - Top and bottom half interrupt handling
*
* Based upon the RPi example by Stefan Wendler (devnull@kaltpost.de)
* from:
* https://github.com/wendlers/rpi-kmod-samples
*
* Press one button to turn on a LED and another to turn it off
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/gpio.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/version.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 10, 0)
#define NO_GPIO_REQUEST_ARRAY
#endif
static int button_irqs[] = { -1, -1 };
/* Define GPIOs for LEDs.
* FIXME: Change the numbers for the GPIO on your board.
*/
#ifdef NO_GPIO_REQUEST_ARRAY
static unsigned int led_gpio = 4;
static unsigned int button_gpios[] = { 17, 18 };
#else
static struct gpio leds[] = { { 4, GPIOF_OUT_INIT_LOW, "LED 1" } };
/* Define GPIOs for BUTTONS
* FIXME: Change the numbers for the GPIO on your board.
*/
static struct gpio buttons[] = {
{ 17, GPIOF_IN, "LED 1 ON BUTTON" },
{ 18, GPIOF_IN, "LED 1 OFF BUTTON" },
};
#endif
/* This happens immediately, when the IRQ is triggered */
static irqreturn_t button_top_half(int irq, void *ident)
{
return IRQ_WAKE_THREAD;
}
/* This can happen at leisure, freeing up IRQs for other high priority task */
static irqreturn_t button_bottom_half(int irq, void *ident)
{
pr_info("Bottom half task starts\n");
mdelay(500); /* do something which takes a while */
pr_info("Bottom half task ends\n");
return IRQ_HANDLED;
}
static int __init bottomhalf_init(void)
{
int ret = 0;
pr_info("%s\n", __func__);
/* register LED gpios */
#ifdef NO_GPIO_REQUEST_ARRAY
ret = gpio_request(led_gpio, "LED 1");
#else
ret = gpio_request_array(leds, ARRAY_SIZE(leds));
#endif
if (ret) {
pr_err("Unable to request GPIOs for LEDs: %d\n", ret);
return ret;
}
#ifdef NO_GPIO_REQUEST_ARRAY
ret = gpio_direction_output(led_gpio, 0);
if (ret) {
pr_err("Unable to configure LED GPIO direction: %d\n", ret);
goto fail2;
}
#endif
/* register BUTTON gpios */
#ifdef NO_GPIO_REQUEST_ARRAY
ret = gpio_request(button_gpios[0], "LED 1 ON BUTTON");
if (ret) {
pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
goto fail2;
}
ret = gpio_request(button_gpios[1], "LED 1 OFF BUTTON");
if (ret) {
pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
goto fail3;
}
ret = gpio_direction_input(button_gpios[0]);
if (ret) {
pr_err("Unable to configure BUTTON1 GPIO direction: %d\n", ret);
goto fail4;
}
ret = gpio_direction_input(button_gpios[1]);
if (ret) {
pr_err("Unable to configure BUTTON2 GPIO direction: %d\n", ret);
goto fail4;
}
#else
ret = gpio_request_array(buttons, ARRAY_SIZE(buttons));
if (ret) {
pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
goto fail1;
}
#endif
#ifdef NO_GPIO_REQUEST_ARRAY
pr_info("Current button1 value: %d\n", gpio_get_value(button_gpios[0]));
ret = gpio_to_irq(button_gpios[0]);
#else
pr_info("Current button1 value: %d\n", gpio_get_value(buttons[0].gpio));
ret = gpio_to_irq(buttons[0].gpio);
#endif
if (ret < 0) {
pr_err("Unable to request IRQ: %d\n", ret);
#ifdef NO_GPIO_REQUEST_ARRAY
goto fail4;
#else
goto fail2;
#endif
}
button_irqs[0] = ret;
pr_info("Successfully requested BUTTON1 IRQ # %d\n", button_irqs[0]);
ret = request_threaded_irq(button_irqs[0], button_top_half,
button_bottom_half,
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
#ifdef NO_GPIO_REQUEST_ARRAY
"gpiomod#button1", NULL);
#else
"gpiomod#button1", &buttons[0]);
#endif
if (ret) {
pr_err("Unable to request IRQ: %d\n", ret);
#ifdef NO_GPIO_REQUEST_ARRAY
goto fail4;
#else
goto fail2;
#endif
}
#ifdef NO_GPIO_REQUEST_ARRAY
ret = gpio_to_irq(button_gpios[1]);
#else
ret = gpio_to_irq(buttons[1].gpio);
#endif
if (ret < 0) {
pr_err("Unable to request IRQ: %d\n", ret);
#ifdef NO_GPIO_REQUEST_ARRAY
goto fail5;
#else
goto fail2;
#endif
}
button_irqs[1] = ret;
pr_info("Successfully requested BUTTON2 IRQ # %d\n", button_irqs[1]);
ret = request_threaded_irq(button_irqs[1], button_top_half,
button_bottom_half,
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
#ifdef NO_GPIO_REQUEST_ARRAY
"gpiomod#button2", NULL);
#else
"gpiomod#button2", &buttons[1]);
#endif
if (ret) {
pr_err("Unable to request IRQ: %d\n", ret);
#ifdef NO_GPIO_REQUEST_ARRAY
goto fail5;
#else
goto fail3;
#endif
}
return 0;
/* cleanup what has been setup so far */
#ifdef NO_GPIO_REQUEST_ARRAY
fail5:
free_irq(button_irqs[0], NULL);
fail4:
gpio_free(button_gpios[1]);
fail3:
gpio_free(button_gpios[0]);
fail2:
gpio_free(led_gpio);
#else
fail3:
free_irq(button_irqs[0], &buttons[0]);
fail2:
gpio_free_array(buttons, ARRAY_SIZE(buttons));
fail1:
gpio_free_array(leds, ARRAY_SIZE(leds));
#endif
return ret;
}
static void __exit bottomhalf_exit(void)
{
pr_info("%s\n", __func__);
/* free irqs */
#ifdef NO_GPIO_REQUEST_ARRAY
free_irq(button_irqs[0], NULL);
free_irq(button_irqs[1], NULL);
#else
free_irq(button_irqs[0], &buttons[0]);
free_irq(button_irqs[1], &buttons[1]);
#endif
/* turn all LEDs off */
#ifdef NO_GPIO_REQUEST_ARRAY
gpio_set_value(led_gpio, 0);
#else
int i;
for (i = 0; i < ARRAY_SIZE(leds); i++)
gpio_set_value(leds[i].gpio, 0);
#endif
/* unregister */
#ifdef NO_GPIO_REQUEST_ARRAY
gpio_free(led_gpio);
gpio_free(button_gpios[0]);
gpio_free(button_gpios[1]);
#else
gpio_free_array(leds, ARRAY_SIZE(leds));
gpio_free_array(buttons, ARRAY_SIZE(buttons));
#endif
}
module_init(bottomhalf_init);
module_exit(bottomhalf_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Interrupt with top and bottom half");