Skip to content

Commit e442916

Browse files
authored
Merge pull request #287 from jeremy90307/master
Allow GPIO example built with Linux v6.10+
2 parents 0b455a2 + bde77a1 commit e442916

File tree

3 files changed

+203
-11
lines changed

3 files changed

+203
-11
lines changed

examples/bh_threaded.c

+71-7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
#include <linux/gpio.h>
1414
#include <linux/delay.h>
1515
#include <linux/interrupt.h>
16+
#include <linux/version.h>
17+
18+
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 10, 0)
19+
#define NO_GPIO_REQUEST_ARRAY
20+
#endif
1621

1722
static int button_irqs[] = { -1, -1 };
1823

@@ -50,29 +55,53 @@ static int __init bottomhalf_init(void)
5055

5156
pr_info("%s\n", __func__);
5257

53-
/* register LED gpios */
58+
/* register LED gpios */
59+
#ifdef NO_GPIO_REQUEST_ARRAY
60+
ret = gpio_request(leds[0].gpio, leds[0].label);
61+
#else
5462
ret = gpio_request_array(leds, ARRAY_SIZE(leds));
63+
#endif
5564

5665
if (ret) {
5766
pr_err("Unable to request GPIOs for LEDs: %d\n", ret);
5867
return ret;
5968
}
6069

61-
/* register BUTTON gpios */
70+
/* register BUTTON gpios */
71+
#ifdef NO_GPIO_REQUEST_ARRAY
72+
ret = gpio_request(buttons[0].gpio, buttons[0].label);
73+
74+
if (ret) {
75+
pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
76+
goto fail1;
77+
}
78+
79+
ret = gpio_request(buttons[1].gpio, buttons[1].label);
80+
81+
if (ret) {
82+
pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
83+
goto fail2;
84+
}
85+
#else
6286
ret = gpio_request_array(buttons, ARRAY_SIZE(buttons));
6387

6488
if (ret) {
6589
pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
6690
goto fail1;
6791
}
92+
#endif
6893

6994
pr_info("Current button1 value: %d\n", gpio_get_value(buttons[0].gpio));
7095

7196
ret = gpio_to_irq(buttons[0].gpio);
7297

7398
if (ret < 0) {
7499
pr_err("Unable to request IRQ: %d\n", ret);
100+
#ifdef NO_GPIO_REQUEST_ARRAY
101+
goto fail3;
102+
#else
75103
goto fail2;
104+
#endif
76105
}
77106

78107
button_irqs[0] = ret;
@@ -86,14 +115,22 @@ static int __init bottomhalf_init(void)
86115

87116
if (ret) {
88117
pr_err("Unable to request IRQ: %d\n", ret);
118+
#ifdef NO_GPIO_REQUEST_ARRAY
119+
goto fail3;
120+
#else
89121
goto fail2;
122+
#endif
90123
}
91124

92125
ret = gpio_to_irq(buttons[1].gpio);
93126

94127
if (ret < 0) {
95128
pr_err("Unable to request IRQ: %d\n", ret);
129+
#ifdef NO_GPIO_REQUEST_ARRAY
130+
goto fail3;
131+
#else
96132
goto fail2;
133+
#endif
97134
}
98135

99136
button_irqs[1] = ret;
@@ -107,12 +144,29 @@ static int __init bottomhalf_init(void)
107144

108145
if (ret) {
109146
pr_err("Unable to request IRQ: %d\n", ret);
147+
#ifdef NO_GPIO_REQUEST_ARRAY
148+
goto fail4;
149+
#else
110150
goto fail3;
151+
#endif
111152
}
112153

113154
return 0;
114155

115156
/* cleanup what has been setup so far */
157+
#ifdef NO_GPIO_REQUEST_ARRAY
158+
fail4:
159+
free_irq(button_irqs[0], NULL);
160+
161+
fail3:
162+
gpio_free(buttons[1].gpio);
163+
164+
fail2:
165+
gpio_free(buttons[0].gpio);
166+
167+
fail1:
168+
gpio_free(leds[0].gpio);
169+
#else
116170
fail3:
117171
free_irq(button_irqs[0], NULL);
118172

@@ -121,27 +175,37 @@ static int __init bottomhalf_init(void)
121175

122176
fail1:
123177
gpio_free_array(leds, ARRAY_SIZE(leds));
178+
#endif
124179

125180
return ret;
126181
}
127182

128183
static void __exit bottomhalf_exit(void)
129184
{
130-
int i;
131-
132185
pr_info("%s\n", __func__);
133186

134187
/* free irqs */
135188
free_irq(button_irqs[0], NULL);
136189
free_irq(button_irqs[1], NULL);
137190

138-
/* turn all LEDs off */
191+
/* turn all LEDs off */
192+
#ifdef NO_GPIO_REQUEST_ARRAY
193+
gpio_set_value(leds[0].gpio, 0);
194+
#else
195+
int i;
139196
for (i = 0; i < ARRAY_SIZE(leds); i++)
140197
gpio_set_value(leds[i].gpio, 0);
141-
142-
/* unregister */
198+
#endif
199+
200+
/* unregister */
201+
#ifdef NO_GPIO_REQUEST_ARRAY
202+
gpio_free(leds[0].gpio);
203+
gpio_free(buttons[0].gpio);
204+
gpio_free(buttons[1].gpio);
205+
#else
143206
gpio_free_array(leds, ARRAY_SIZE(leds));
144207
gpio_free_array(buttons, ARRAY_SIZE(buttons));
208+
#endif
145209
}
146210

147211
module_init(bottomhalf_init);

examples/bottomhalf.c

+66-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
#include <linux/module.h>
1515
#include <linux/printk.h>
1616
#include <linux/init.h>
17+
#include <linux/version.h>
18+
19+
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 10, 0)
20+
#define NO_GPIO_REQUEST_ARRAY
21+
#endif
1722

1823
/* Macro DECLARE_TASKLET_OLD exists for compatibility.
1924
* See https://lwn.net/Articles/830964/
@@ -70,28 +75,52 @@ static int __init bottomhalf_init(void)
7075
pr_info("%s\n", __func__);
7176

7277
/* register LED gpios */
78+
#ifdef NO_GPIO_REQUEST_ARRAY
79+
ret = gpio_request(leds[0].gpio, leds[0].label);
80+
#else
7381
ret = gpio_request_array(leds, ARRAY_SIZE(leds));
82+
#endif
7483

7584
if (ret) {
7685
pr_err("Unable to request GPIOs for LEDs: %d\n", ret);
7786
return ret;
7887
}
7988

8089
/* register BUTTON gpios */
90+
#ifdef NO_GPIO_REQUEST_ARRAY
91+
ret = gpio_request(buttons[0].gpio, buttons[0].label);
92+
93+
if (ret) {
94+
pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
95+
goto fail1;
96+
}
97+
98+
ret = gpio_request(buttons[1].gpio, buttons[1].label);
99+
100+
if (ret) {
101+
pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
102+
goto fail2;
103+
}
104+
#else
81105
ret = gpio_request_array(buttons, ARRAY_SIZE(buttons));
82106

83107
if (ret) {
84108
pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
85109
goto fail1;
86110
}
111+
#endif
87112

88113
pr_info("Current button1 value: %d\n", gpio_get_value(buttons[0].gpio));
89114

90115
ret = gpio_to_irq(buttons[0].gpio);
91116

92117
if (ret < 0) {
93118
pr_err("Unable to request IRQ: %d\n", ret);
119+
#ifdef NO_GPIO_REQUEST_ARRAY
120+
goto fail3;
121+
#else
94122
goto fail2;
123+
#endif
95124
}
96125

97126
button_irqs[0] = ret;
@@ -104,14 +133,22 @@ static int __init bottomhalf_init(void)
104133

105134
if (ret) {
106135
pr_err("Unable to request IRQ: %d\n", ret);
136+
#ifdef NO_GPIO_REQUEST_ARRAY
137+
goto fail3;
138+
#else
107139
goto fail2;
140+
#endif
108141
}
109142

110143
ret = gpio_to_irq(buttons[1].gpio);
111144

112145
if (ret < 0) {
113146
pr_err("Unable to request IRQ: %d\n", ret);
147+
#ifdef NO_GPIO_REQUEST_ARRAY
148+
goto fail3;
149+
#else
114150
goto fail2;
151+
#endif
115152
}
116153

117154
button_irqs[1] = ret;
@@ -124,12 +161,29 @@ static int __init bottomhalf_init(void)
124161

125162
if (ret) {
126163
pr_err("Unable to request IRQ: %d\n", ret);
164+
#ifdef NO_GPIO_REQUEST_ARRAY
165+
goto fail4;
166+
#else
127167
goto fail3;
168+
#endif
128169
}
129170

130171
return 0;
131172

132173
/* cleanup what has been setup so far */
174+
#ifdef NO_GPIO_REQUEST_ARRAY
175+
fail4:
176+
free_irq(button_irqs[0], NULL);
177+
178+
fail3:
179+
gpio_free(buttons[1].gpio);
180+
181+
fail2:
182+
gpio_free(buttons[0].gpio);
183+
184+
fail1:
185+
gpio_free(leds[0].gpio);
186+
#else
133187
fail3:
134188
free_irq(button_irqs[0], NULL);
135189

@@ -138,27 +192,37 @@ static int __init bottomhalf_init(void)
138192

139193
fail1:
140194
gpio_free_array(leds, ARRAY_SIZE(leds));
195+
#endif
141196

142197
return ret;
143198
}
144199

145200
static void __exit bottomhalf_exit(void)
146201
{
147-
int i;
148-
149202
pr_info("%s\n", __func__);
150203

151204
/* free irqs */
152205
free_irq(button_irqs[0], NULL);
153206
free_irq(button_irqs[1], NULL);
154207

155208
/* turn all LEDs off */
209+
#ifdef NO_GPIO_REQUEST_ARRAY
210+
gpio_set_value(leds[0].gpio, 0);
211+
#else
212+
int i;
156213
for (i = 0; i < ARRAY_SIZE(leds); i++)
157214
gpio_set_value(leds[i].gpio, 0);
215+
#endif
158216

159217
/* unregister */
218+
#ifdef NO_GPIO_REQUEST_ARRAY
219+
gpio_free(leds[0].gpio);
220+
gpio_free(buttons[0].gpio);
221+
gpio_free(buttons[1].gpio);
222+
#else
160223
gpio_free_array(leds, ARRAY_SIZE(leds));
161224
gpio_free_array(buttons, ARRAY_SIZE(buttons));
225+
#endif
162226
}
163227

164228
module_init(bottomhalf_init);

0 commit comments

Comments
 (0)