Products > Programming

Topic donated. No longer about learning to write a driver for fun and with ease

(1/3) > >>

RoGeorge:
- How easy?
- This easy!

The 'hello_drv.c' file:

--- Code: ---#include <linux/module.h>
#include <linux/init.h>

int __init my_init(void)
{
printk("hello_drv - Hello from the kernel!\n");
return 0;
}

void __exit my_exit(void)
{
printk("hello_drv - Bye, bye, driver unloaded.\n");
}

MODULE_LICENSE("GPL");
MODULE_AUTHOR("RoGeorge");
MODULE_DESCRIPTION("The Hello World of a Loadable Kernel Module");

module_init(my_init);
module_exit(my_exit);

--- End code ---

The 'Makefile':

--- Code: ---obj-m += hello_drv.o

all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

--- End code ---



- How?
- From a terminal, install the build tools, make the .ko module then load/unload your driver into the Linux kernel!  8)
('hello_drv.c', 'Makefile' and a compiled 'hello_drv.ko' are in the zip archive attached at the end of this post)

--- Code: ---/*
 * # as seen in the [url]https://www.youtube.com/watch?v=2-vKcbfa0bc[/url]
 *
 * Linux Driver Workshop at Kieler Linuxtage (english)
 * Johannes 4GNU_Linux
 * by Johannes Roith, 2023-09-15
 *
 * sudo apt install build-essential
 * sudo apt install linux-headers-$(uname -r)
 *
 * make
 *
 * sudo dmesg
 * sudo insmod ./hello_drv.ko # loads the driver module into the Linux kernel
 * # sudo modprobe ./hello_drv.ko # loads the driver including its dependencies
 * sudo dmesg # observe the message in the kernel log '[timestamp] hello_drv - Hello from the kernel!'
 *
 * lsmod | grep hello_drv
 *
 * sudo rmmod ./hello_drv.ko
 * sudo dmesg # observe the message '[timestamp] hello_drv - Bye, bye, driver unloaded.
 *
 * modinfo ./hello_drv.ko
 *
 * make clean
 */

--- End code ---

Interested for more than just the Hello World?  See the full workshop (1.5 hours):
Linux Driver Workshop at Kieler Linuxtage (english)
Johannes 4GNU_Linux
https://youtu.be/2-vKcbfa0bc

or the playlist 'Let's code a Linux Driver' from the same YT channel:
https://www.youtube.com/playlist?list=PLCGpd0Do5-I3b5TtyqeF1UdyD4C-S-dMa

janoc:
That may sound a bit pedantic but what you have there is not a driver of any kind, only a Linux kernel module (for the uninitiated - sort of a kernel plugin). Most drivers are modules these days but not every module is a driver and not every driver is/can be a module.

Try to write a driver e.g. for a new block device or some USB device, then the "It's easy" would have more relevance  ;)



magic:
The use of "code" as a verb is an instant giveaway that you are dealing with a layman or a material dumbed down for laymen.

Literally nobody in the industry uses it that way.

RoGeorge:
A software jedi master Yoda is not.  Yrsssss!  >:(



--- Quote from: janoc on September 16, 2023, 10:00:28 pm ---Try to write a driver e.g. for a new block device or some USB device, then the "It's easy" would have more relevance  ;)

--- End quote ---

Master Yoda said  "No, try not.  Do, or do not."  :)


Starting a drivers-101 with a block device might be a block.  Even the pros want them gone, like in FreeBSD:

--- Quote ---9.4. Block Devices (Are Gone)

Other UNIX® systems may support a second type of disk device known as block devices. Block devices are disk devices for which the kernel provides caching. This caching makes block-devices almost unusable, or at least dangerously unreliable. The caching will reorder the sequence of write operations, depriving the application of the ability to know the exact disk contents at any one instant in time.

This makes predictable and reliable crash recovery of on-disk data structures (filesystems, databases, etc.) impossible. Since writes may be delayed, there is no way the kernel can report to the application which particular write operation encountered a write error, this further compounds the consistency problem.

For this reason, no serious applications rely on block devices, and in fact, almost all applications which access disks directly take great pains to specify that character (or "raw") devices should always be used. As the implementation of the aliasing of each disk (partition) to two devices with different semantics significantly complicated the relevant kernel code, FreeBSD dropped support for cached disk devices as part of the modernization of the disk I/O infrastructure.
--- End quote ---
Source:  https://docs.freebsd.org/en/books/arch-handbook/driverbasics/#driverbasics-block

Then, the USB is as its name tells:  universal, with so many possibilities it will confuse anyone at some point.

DiTBho:

--- Quote from: janoc on September 16, 2023, 10:00:28 pm ---Most drivers are modules these days but not every module is a driver and not every driver is/can be a module.

--- End quote ---

I still have a serious problem with the kernel module of a fiber optic card. After thousands of hours of debugging I still haven't understood why if compiled statically (kernel built-in) the probe succeeds, while if compiled as a module it fails randomly.

It cannot be compiled as kernel module due to severe restrictions in the final embedded diskless environment, so it's ok as kernel built-in, but ... what the bloody hello is wrong with the Probe? It's just out of curiosity and I don't have time to investigate further as you have a layer of things to look at, starting with PCI, and then going up to all the layers above.

The next person who tells me "uh, oh, it's simple", I swear I'll throw the card at their head!
And it doesn't weigh little, it's 450g, coiunting a massive copper heatsink and thick connectors!

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod