First LINUX driver

Source: Internet
Author: User
The first LINUX driver-General Linux technology-Linux programming and kernel information. The following is a detailed description. LINUX: Virtual Machine + DEBIAN
Kernel: LINUX-2.6.32
Note: The parameters mentioned in this document are only applicable to individual usage and may not be universal, especially when the operating system
Different versions are released, such as REDHAT and UBUNTU.
Steps:
Su root # perform the following operations after entering the root mode:
1. apt-get install build-essential # skip this step if the gcc compiler already exists
1. cd/usr/src # you can access any file directory.
2. apt-get install linux-source-2.6.32 # get kernel source code
3. apt-get linux-headers-2.6.32-5-686 # get header file, available uname-r get kernel version information
4. tar-jxf./liunx-source-2.6.32.tar.bz2
5. cp-a./linux-headers-2.6.32-5-686/Module. symvers./linux-source-2.6.32
6. cd./linux-source-2.6.32
7. make oldconfig
8. make prepare
9. make scripts
10. cd/tmp
11. vi hello. c # Edit the hello. c program. for the code here, refer to the LINUX device driver (third edition)
/* ---------------- For the reference program --------------------------*/
# Include
# Include
MODULE_LICENSE ("Dual BSD/GPL ");

Static int hello_init (void)
{
Printk (KERN_ALERT "Hello, world \ n ");
Return 0;
}

Static void hello_exit (void)
{
Printk (KERN_ALERT "Goodbye, cruel world \ n ");
}

Module_init (hello_init );
Module_exit (hello_exit );
/*********************** END *************** *****************/
11. vi Makefile # note that M in Makefile must be capitalized. This Makefile is usable but not standardized.
# Sample Makefile
Obj-m: = hello. o
# End
13. make-C/usr/src/linux-source-2.6.32 M =/tmp modules
# Generate hello. ko after Compilation
14. insmod hello. ko # mount the driver
# At this time, the terminal may not output information because KERN_ALERT has a lower priority.
15. rmmod hello # Uninstall the driver
16. cat/var/log/syslog # capture the output information of the driver. you can see the output information of the hello driver in the last two lines.
# Now the hello. c driver is complete, and the linux device driver development environment is basically complete
# Let's go!

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.