Hello World of Linxu Device Driver!

Source: Internet
Author: User

Next, I want to record how to learn the Linux driver! I have been in touch for a while, but I have no records. I happen to want to take the ARM11 board! Therefore, we are going to record the system from today, mainly to learn the driver architecture, then write the driver program for the peripherals on the board, and test it!

The best driver learning material is the kernel source code, but several books are also recommended.

1. Linux Device Driver

2. Linux device driver development details

Today is the beginning of a series. Keep a good tradition and write a Hello World! .

#include <linux/init.h>#include <linux/module.h>static int hello_init(void) {printk(KERN_INFO "hello world!\n");return 0;}static void hello_exit(void) {printk(KERN_INFO "Goodbye!\n");}module_init(hello_init);module_exit(hello_exit);MODULE_LICENSE("Dual BSD/GPL");

The Makefile file is as follows:

ifneq ($(KERNELRELEASE),)# call from kernel build systemobj-m:= helloworld.oelseKERNELDIR ?= /lib/modules/$(shell uname -r)/buildPWD       := $(shell pwd)default:$(MAKE) -C $(KERNELDIR) M=$(PWD) modulesendif

Enter the make command to compile. You can check the output information compiled on the terminal, which helps us understand the compilation process:


We can see that the entire compilation process is divided into two processes. The first is to enter the kernel compilation system and perform some processing, because our module is compiled for a kernel version, so we need to go to the kernel compilation system for processing, and then compile our module in the second process from the kernel compilation system to the directory of our module source code!

After compilation, many files are generated in the first and second phases, as shown below:


Run the following command: sudo insmod./helloworld. ko to load the module. Run the lsmod command to check whether the load is successful:


But "hello world!" is not output on the terminal! ", But you can view the/var/log/message file:

You can also run the dmesg command to view the result. The output is as follows:


Next, run the rmmod command to uninstall the module. The output is as follows:


It can be seen that the uninstallation was not successful, but after my check, this is not because my kernel is not allowed to uninstall the module!

The above test environment is fedora16 and the kernel version is 3.2.7. Due to the above issue that the module cannot be uninstalled, many people have encountered the same problem on the Internet, but they only said that they can be uninstalled using the previous version. The latest release version does not work, but I didn't give the reason. Next I will use ubuntu_9.0.1 to test the kernel 2.6.28.

The module is loaded directly. Use the lsmod command to view the following information:


Here you can see the difference by comparing the tests above in fedora16!

Next, uninstall the module:


Here the operation is successful. Run the lsmod command to check it:


OK. You can see that the uninstall is successful! But what is the reason for this difference? I haven't found it yet. I hope the person who knows can advise me and leave a message below!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.