# linux driver examples **Repository Path**: unix1970/linux-driver-examples ## Basic Information - **Project Name**: linux driver examples - **Description**: 一个简单的 linux 驱动程序,展示了如何接入 linux 模块,以及模块的安装和卸载。 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-07-10 - **Last Updated**: 2024-07-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README **简介** 一个简单的 linux 驱动程序,展示了如何接入 linux 模块,以及模块的安装和卸载。 **目录结构** ```c hello_world.c Makefile ``` **hello_world.c** ```c #include #include static int __init hello_world_init(void) { printk("Hello world!\n"); return 0; } static void __exit hello_world_exit(void) { printk("Goodbye world!\n"); } module_init(hello_world_init); module_exit(hello_world_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("A simple hello world module"); MODULE_VERSION("1.0"); ``` **Makefile** ```c obj-m := hello_world.o KDIR := /lib/modules/$(shell uname -r)/build all: $(MAKE) -C $(KDIR) M=$(PWD) modules clean: $(MAKE) -C $(KDIR) M=$(PWD) clean ``` **编译** ```c make ``` **安装模块** ```c sudo insmod hello_world.ko ``` 执行 `sudo dmesg | tail`,可以看见日志有打印 `Hello world!`。 **卸载模块** ```c sudo rmmod hello_world ``` 执行 `sudo dmesg | tail`,可以看见日志有打印 `Goodbye world!`。 **清理项目目录** ```c make clean ``` **参考** * [An Introduction to Linux Device Driver Programming | by viral patel | Medium](https://medium.com/@vir29032001/an-introduction-to-linux-device-driver-programming-635a59a64de3) * [valerie-henson-device-drivers-hello.pdf (pitt.edu)](https://people.cs.pitt.edu/~jmisurda/teaching/cs449/valerie-henson-device-drivers-hello.pdf)