Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an LED driver example using GPIO #290

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jeremy90307
Copy link
Contributor

Add an LED driver example using GPIO and add related knowledge.

Test detail:

  • Tested on Raspberry Pi 5B with Raspberry Pi OS (Debian 12, Linux version 6.12.1-v8-16k+)

  • Verify that LED sample compiles and loads successfully

  • Verify that LED turns on and off sucessfully

@jeremy90307 jeremy90307 force-pushed the master branch 3 times, most recently from 3a799e4 to a64cd81 Compare December 18, 2024 12:11
lkmpg.tex Outdated Show resolved Hide resolved
examples/led.c Outdated Show resolved Hide resolved
examples/led.c Outdated
Comment on lines 22 to 25
/* The major device number. We can not rely on dynamic registration
* any more.
*/
#define MAJOR_NUM 100
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explain why.

examples/led.c Outdated
{
pr_info("device_open(%p)\n", file);

try_module_get(THIS_MODULE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See if try_module_get is required or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have noticed this issue, such as issue #52, and there are still several examples using it. Let me fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to remove try_module_get, we also need to stop using register_chrdev and use cdev_init and cdev_add instead.

Because with register_chrdev, we need to manually increase or decrease the module's reference count.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The try_module_get() function should generally be avoided. The only legitimate use case is when you need to reference another module before interacting with it, such as in file operations code. While try_module_get(THIS_MODULE) appears multiple times in the kernel source code, these instances are typically bugs that need to be fixed rather than examples to follow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my modified code, register_chrdev is not included. Should I use the preprocessor to make the changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I saw that cdev already appeared in Linux v2.6.

examples/led.c Outdated Show resolved Hide resolved
lkmpg.tex Outdated Show resolved Hide resolved
lkmpg.tex Outdated Show resolved Hide resolved
Add an LED driver example using GPIO and add related knowledge.

Test detail:

- Tested on Raspberry Pi 5B with Raspberry Pi OS (Debian 12, Linux
  version 6.12.1-v8-16k+)

- Verify that LED example compiles and loads successfully

- Verify that LED turns on and off sucessfully
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants