-
Notifications
You must be signed in to change notification settings - Fork 534
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
base: master
Are you sure you want to change the base?
Conversation
3a799e4
to
a64cd81
Compare
examples/led.c
Outdated
/* The major device number. We can not rely on dynamic registration | ||
* any more. | ||
*/ | ||
#define MAJOR_NUM 100 |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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
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