Linux driver development experience

Source: Internet
Author: User

Statement: these articles are original articles. If you want to reprint them, you need to obtain your own permission. Otherwise, you will be held accountable. I hope you can understand them! (Small fish in the deep ocean)

Development Platform: PC (XP), virtual machine vwmare6.5, Linux operating system fedora9

Knowledge required:

1. Priority of printk Printing Output

# Define kern_emerg "<0>"/* indicates an emergency message. A message is prompted before the system crashes, indicating that the system is unavailable */
# Define kern_alert "<1>"/* Report messages, indicating that actions must be taken immediately */
# Define kern_crit "<2>"/* critical conditions, usually involving serious hardware or software operation failures */
# Define kern_err "<3>"/* error condition, driver. Commonly used kern_err to report hardware errors */
# Define kern_warning "<4>"/* warning conditions to warn of possible problems */
# Define kern_notice "<5>"/* normal but important conditions are used to remind security-related messages */
# Define kern_info "<6>"/* prompt information, such as hardware information printed when the driver is started */
# Define kern_debug "<7>"/* debug-level messages */

These are the basic messages printed by the kernel, but you must note that not all macro definitions can be displayed on the terminal, but you can be certain that you use these macro definitions during debugging, it must be displayed in some places, as shown in logs and other files.

2. Explanations of several key statements in makefile

Make-C $ (kerneldir) M = $ (PWD) Modules

-C is to change the directory where make runs, $ (kerneldir) is the path address of your local kernel,

M = $ (PWD) modules. This enables makefile to return to the module source code directory before trying to generate a module. The direct point is to give the modules in the directory where the makefile is located to M, execute the make command.

Kerneldir: =/usr/src/kernels/2.6.25-14. fc9.i686

This directory address is the correct address of your VM. Note 2.6.25-14. fc9.i686: This is the name of the folder corresponding to your Linux host: =/usr/src/kernels

OBJ-M: = Hello. O target file depends on hell. o

Now, let's start driving the tour:

Driver. c file

/*hello_module.c*/
#include<linux/init.h>
#include<linux/kernel.h>
#include<linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_EMERG "Hello ,Linux Driver!\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_EMERG "Hello Diver Exit !\n");
}
module_init(hello_init);
module_exit(hello_exit);
 

Makefile

#hello_makefile
obj-m :=hello.o
KERNELDIR :=/usr/src/kernels/2.6.25-14.fc9.i686
PWD :=$(shell pwd)
all:
make -C $(KERNELDIR) M=$(PWD) modules
.PHONY :clean
clean:
rm -rf *.o *ko

 

Note that your. c file and your makefile should be placed in the same directory.

Then execute make

Okay. Load the hello driver.

As you can see, the printed information of your driver is displayed. Of course, you can also check it in your log.

Or use some simple commands.

In this way, you have finished writing the hello driver in Linux! If you have any questions, please leave a message. Hey hey!

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.