Skip to content

Commit

Permalink
Fix GPIO example to run on Kernel 6.10+
Browse files Browse the repository at this point in the history
gpio_request_array() and gpio_free_array()
were removed in Kernel v6.10-rc1. Use
gpio_request() and gpio_free() instead,
and ensure the LKM can run successfully
on Kernel v5.15, v6.1, and v6.6+.
  • Loading branch information
jeremy90307 committed Dec 4, 2024
1 parent d891311 commit 9883e5a
Show file tree
Hide file tree
Showing 3 changed files with 350 additions and 6 deletions.
119 changes: 117 additions & 2 deletions examples/bh_threaded.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ static int __init bottomhalf_init(void)

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

<<<<<<< HEAD
<<<<<<< HEAD
/* register LED gpios */
#ifdef NO_ARRAY
Expand All @@ -73,12 +74,30 @@ static int __init bottomhalf_init(void)
ret = gpio_request_array(leds, ARRAY_SIZE(leds));
#endif
>>>>>>> 076079b (Fix GPIO example to run on Kernel 6.10+)
=======
/* register LED gpios */
#ifdef NO_ARRAY
ret = gpio_request(leds[0].gpio, leds[0].label);
#else
ret = gpio_request_array(leds, ARRAY_SIZE(leds));
#endif
== == == =
/* register LED gpios */
#ifdef NO_ARRAY
ret = gpio_request(leds[0].gpio, leds[0].label);
#else
ret = gpio_request_array(leds, ARRAY_SIZE(leds));
#endif
>>>>>>> 076079b(Fix GPIO example to run on Kernel 6.10 +)
>>>>>>> 5ce6390 (Fix GPIO example to run on Kernel 6.10+)

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

<<<<<<< HEAD
<<<<<<< HEAD
/* register BUTTON gpios */
#ifdef NO_ARRAY
Expand Down Expand Up @@ -115,14 +134,56 @@ static int __init bottomhalf_init(void)
if (ret) {
pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
goto BottonError;
=======
/* register BUTTON gpios */
#ifdef NO_ARRAY
== == == =
/* register BUTTON gpios */
#ifdef NO_ARRAY
>>>>>>> 076079b(Fix GPIO example to run on Kernel 6.10 +) ret =
gpio_request(buttons[0].gpio, buttons[0].label);

if (ret) {
pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
goto Button0Error;
>>>>>>> 5ce6390 (Fix GPIO example to run on Kernel 6.10+)
}
<<<<<<< HEAD
#endif
=======
#endif
>>>>>>> 076079b (Fix GPIO example to run on Kernel 6.10+)

pr_info("Current button1 value: %d\n", gpio_get_value(buttons[0].gpio));
<<<<<<< HEAD
ret = gpio_request(buttons[1].gpio, buttons[1].label);

if (ret) {
pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
goto Botton1Error;
}
#else
== == == = ret = gpio_request(buttons[1].gpio, buttons[1].label);

if (ret) {
pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
goto Botton1Error;
}
#else
>>>>>>> 076079b(Fix GPIO example to run on Kernel 6.10 +) ret =
gpio_request_array(buttons, ARRAY_SIZE(buttons));

if (ret) {
pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
goto BottonError;
}
<<<<<<< HEAD
#endif
== == == =
#endif
>>>>>>> 076079b(Fix GPIO example to run on Kernel 6.10 +)

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

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

Expand Down Expand Up @@ -174,6 +235,7 @@ static int __init bottomhalf_init(void)

#ifdef NO_ARRAY
<<<<<<< HEAD
<<<<<<< HEAD
IRQ0Error:
gpio_free(buttons[1].gpio);

Expand All @@ -196,6 +258,28 @@ static int __init bottomhalf_init(void)
IRQ0Error:
gpio_free_array(buttons, ARRAY_SIZE(leds));

=======
< < < < < < < HEAD IRQ0Error : gpio_free(buttons[1].gpio);

Botton1Error:
gpio_free(buttons[0].gpio);

Button0Error:
gpio_free(leds[0].gpio);
=======
IRQ0Error:
gpio_free(buttons[1].gpio);

Botton1Error:
gpio_free(buttons[0].gpio);

Button0Error:
gpio_free(leds[0].gpio);
>>>>>>> 076079b (Fix GPIO example to run on Kernel 6.10+)
#else
IRQ0Error : gpio_free_array(buttons, ARRAY_SIZE(leds));

>>>>>>> 5ce6390 (Fix GPIO example to run on Kernel 6.10+)
BottonError:
gpio_free_array(leds, ARRAY_SIZE(leds));
#endif
Expand All @@ -211,6 +295,7 @@ static void __exit bottomhalf_exit(void)
free_irq(button_irqs[0], NULL);
free_irq(button_irqs[1], NULL);

<<<<<<< HEAD
<<<<<<< HEAD
/* turn all LEDs off */
#ifdef NO_ARRAY
Expand All @@ -236,6 +321,32 @@ static void __exit bottomhalf_exit(void)
gpio_set_value(leds[0].gpio, 0);
#else
int i;
=======
/* turn all LEDs off */
#ifdef NO_ARRAY
gpio_set_value(leds[0].gpio, 0);
#else
int i;
for (i = 0; i < ARRAY_SIZE(leds); i++)
gpio_set_value(leds[i].gpio, 0);
#endif

/* unregister */
#ifdef NO_ARRAY
gpio_free(leds[0].gpio);
gpio_free(buttons[0].gpio);
gpio_free(buttons[1].gpio);
#else
gpio_free_array(leds, ARRAY_SIZE(leds));
gpio_free_array(buttons, ARRAY_SIZE(buttons));
#endif
== == == =
/* turn all LEDs off */
#ifdef NO_ARRAY
gpio_set_value(leds[0].gpio, 0);
#else
int i;
>>>>>>> 5ce6390 (Fix GPIO example to run on Kernel 6.10+)
for (i = 0; i < ARRAY_SIZE(leds); i++)
gpio_set_value(leds[i].gpio, 0);
#endif
Expand All @@ -249,7 +360,11 @@ static void __exit bottomhalf_exit(void)
gpio_free_array(leds, ARRAY_SIZE(leds));
gpio_free_array(buttons, ARRAY_SIZE(buttons));
#endif
<<<<<<< HEAD
>>>>>>> 076079b (Fix GPIO example to run on Kernel 6.10+)
=======
>>>>>>> 076079b(Fix GPIO example to run on Kernel 6.10 +)
>>>>>>> 5ce6390 (Fix GPIO example to run on Kernel 6.10+)
}

module_init(bottomhalf_init);
Expand Down
122 changes: 120 additions & 2 deletions examples/bottomhalf.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ static int __init bottomhalf_init(void)

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

<<<<<<< HEAD
<<<<<<< HEAD
/* register LED gpios */
#ifdef NO_ARRAY
Expand All @@ -92,12 +93,30 @@ static int __init bottomhalf_init(void)
ret = gpio_request_array(leds, ARRAY_SIZE(leds));
#endif
>>>>>>> 076079b (Fix GPIO example to run on Kernel 6.10+)
=======
/* register LED gpios */
#ifdef NO_ARRAY
ret = gpio_request(leds[0].gpio, leds[0].label);
#else
ret = gpio_request_array(leds, ARRAY_SIZE(leds));
#endif
== == == =
/* register LED gpios */
#ifdef NO_ARRAY
ret = gpio_request(leds[0].gpio, leds[0].label);
#else
ret = gpio_request_array(leds, ARRAY_SIZE(leds));
#endif
>>>>>>> 076079b(Fix GPIO example to run on Kernel 6.10 +)
>>>>>>> 5ce6390 (Fix GPIO example to run on Kernel 6.10+)

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

<<<<<<< HEAD
<<<<<<< HEAD
/* register BUTTON gpios */
#ifdef NO_ARRAY
Expand Down Expand Up @@ -125,6 +144,19 @@ static int __init bottomhalf_init(void)
pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
<<<<<<< HEAD
goto BottonError;
=======
/* register BUTTON gpios */
#ifdef NO_ARRAY
== == == =
/* register BUTTON gpios */
#ifdef NO_ARRAY
>>>>>>> 076079b(Fix GPIO example to run on Kernel 6.10 +) ret =
gpio_request(buttons[0].gpio, buttons[0].label);

if (ret) {
pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
goto Button0Error;
>>>>>>> 5ce6390 (Fix GPIO example to run on Kernel 6.10+)
}
#endif
=======
Expand All @@ -140,7 +172,35 @@ static int __init bottomhalf_init(void)
#endif
>>>>>>> 076079b (Fix GPIO example to run on Kernel 6.10+)

pr_info("Current button1 value: %d\n", gpio_get_value(buttons[0].gpio));
ret = gpio_request(buttons[1].gpio, buttons[1].label);

if (ret) {
pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
goto Botton1Error;
}
#else
ret = gpio_request_array(buttons, ARRAY_SIZE(buttons));

if (ret) {
pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
<<<<<<< HEAD
goto BottonError;
}
#endif
== == == = goto Botton1Error;
}
#else
ret = gpio_request_array(buttons, ARRAY_SIZE(buttons));

if (ret) {
pr_err("Unable to request GPIOs for BUTTONs: %d\n", ret);
goto BottonError;
}
#endif
>>>>>>> 076079b(Fix GPIO example to run on Kernel 6.10 +)

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

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

Expand Down Expand Up @@ -190,6 +250,7 @@ static int __init bottomhalf_init(void)

#ifdef NO_ARRAY
<<<<<<< HEAD
<<<<<<< HEAD
IRQ0Error:
gpio_free(buttons[1].gpio);

Expand All @@ -198,6 +259,29 @@ static int __init bottomhalf_init(void)

Button0Error:
gpio_free(leds[0].gpio);
=======
< < < < < < < HEAD IRQ0Error : gpio_free(buttons[1].gpio);

Botton1Error:
gpio_free(buttons[0].gpio);

Button0Error:
gpio_free(leds[0].gpio);

#else
IRQ0Error : gpio_free_array(buttons, ARRAY_SIZE(leds));

BottonError : gpio_free_array(leds, ARRAY_SIZE(leds));
#endif
== == == = IRQ0Error : gpio_free(buttons[1].gpio);

Botton1Error:
gpio_free(buttons[0].gpio);

Button0Error:
gpio_free(leds[0].gpio);
>>>>>>> 076079b (Fix GPIO example to run on Kernel 6.10+)
>>>>>>> 5ce6390 (Fix GPIO example to run on Kernel 6.10+)

#else
IRQ0Error:
Expand All @@ -206,6 +290,7 @@ static int __init bottomhalf_init(void)
BottonError:
gpio_free_array(leds, ARRAY_SIZE(leds));
#endif
<<<<<<< HEAD
=======
IRQ0Error:
gpio_free(buttons[1].gpio);
Expand All @@ -224,6 +309,8 @@ static int __init bottomhalf_init(void)
BottonError:
gpio_free_array(leds, ARRAY_SIZE(leds));
#endif
=======
>>>>>>> 5ce6390 (Fix GPIO example to run on Kernel 6.10+)

return ret;
}
Expand All @@ -232,6 +319,7 @@ static void __exit bottomhalf_exit(void)
{
pr_info("%s\n", __func__);

<<<<<<< HEAD
<<<<<<< HEAD
/* free irqs */
#ifdef NO_ARRAY
Expand All @@ -257,6 +345,32 @@ static void __exit bottomhalf_exit(void)
gpio_set_value(leds[0].gpio, 0);
#else
int i;
=======
/* free irqs */
#ifdef NO_ARRAY
gpio_set_value(leds[0].gpio, 0);
#else
int i;
for (i = 0; i < ARRAY_SIZE(leds); i++)
gpio_set_value(leds[i].gpio, 0);
#endif

/* unregister */
#ifdef NO_ARRAY
gpio_free(leds[0].gpio);
gpio_free(buttons[0].gpio);
gpio_free(buttons[1].gpio);
#else
gpio_free_array(leds, ARRAY_SIZE(leds));
gpio_free_array(buttons, ARRAY_SIZE(buttons));
#endif
== == == =
/* free irqs */
#ifdef NO_ARRAY
gpio_set_value(leds[0].gpio, 0);
#else
int i;
>>>>>>> 5ce6390 (Fix GPIO example to run on Kernel 6.10+)
for (i = 0; i < ARRAY_SIZE(leds); i++)
gpio_set_value(leds[i].gpio, 0);
#endif
Expand All @@ -270,7 +384,11 @@ static void __exit bottomhalf_exit(void)
gpio_free_array(leds, ARRAY_SIZE(leds));
gpio_free_array(buttons, ARRAY_SIZE(buttons));
#endif
<<<<<<< HEAD
>>>>>>> 076079b (Fix GPIO example to run on Kernel 6.10+)
=======
>>>>>>> 076079b(Fix GPIO example to run on Kernel 6.10 +)
>>>>>>> 5ce6390 (Fix GPIO example to run on Kernel 6.10+)
}

module_init(bottomhalf_init);
Expand Down
Loading

0 comments on commit 9883e5a

Please sign in to comment.