Linux driver-Linux general technology-Linux programming and kernel information. For more information, see the following section. Kernel version: 2.4.22
Objective: To learn how to compile a Linux Device Driver.
To read this article, read the following two files: hello. c, asdf. c.
This article assumes that the reader:
You can use C to write Linux applications,
Understanding "character device files, block device files, master device numbers, secondary device numbers ",
Write simple Shell scripts and Makefile.
1. "hello. c"
--------------------------------
/*
* This is our first source file,
* It is a kernel module that can be loaded,
* "Hello, World!" is displayed during loading! ",
* "Bye!" is displayed during uninstallation! ".
* It should be noted that the system call or function library used to write the kernel or kernel module cannot be used to write the application,
* Because we write code that provides system calls for applications.
* The kernel has a dedicated function library, such , , ,
* There is no need to know much about it now,
* The printk function used here is similar to printf.
* "/Usr/src/linux" is a symbolic link to your actual kernel source code directory,
* Create one if it is not present, because it will be used later and later.
* Compile it with "gcc-c-I/usr/src/linux/include hello. c ",
* If the file hello. o is generated normally,
* Load it with "insmod hello. o ",
* The output can be viewed only in the text terminal.
* Uninstall it and use "rmmod hello"
*/
/*
* Tips: add a line to. bashrc in the user directory:
* Alias mkmod = 'gcc-c-I/usr/src/linux/include'
* Then log on to the Shell again,
* Later, you can use the "mkmod hello. c" method to compile the kernel module.
*/
2. "asdf. c"
------------------------------------
/*
* This file is a kernel module.
* Kernel module compilation, loading, and uninstallation have been described earlier.
* The function of this module is to create a character device.
* This device has a 4096-byte shared memory.
* The primary device number allocated by the kernel is displayed when the module is loaded.
*/
# Include/* copy_to_user (), copy_from_user */
# Include /* Struct file_operations, register_chrdev (),...*/
# Include /* Printk () in this file */
# Include /* Related to task scheduling */
# Include /* U8, 2010, u32 ...*/
/*
* For the kernel function library, you can search for details online,
*/
/* The callback function when the file is operated */
Static int asdf_open (struct inode * inode, struct file * filp );
Static int asdf_release (struct inode * inode, struct file * filp );
Static ssize_t asdf_read (struct file * filp, char * buf, size_t count, loff_t * f_pos );
Static ssize_t asdf_write (struct file * filp, const char * buf, size_t count, loff_t * f_pos );
Static loff_t asdf_lseek (struct file * file, loff_t offset, int orig );
/* The structure used to apply for the master device number, which is defined in linux/fs. h */
Struct file_operations asdf_fops = {
Open: asdf_open,
Release: asdf_release,
Read: asdf_read,
Write: asdf_write,
Llseek: asdf_lseek,
};
Static int asdf_major;/* used to save the applied master device Number */
Static u8 asdf_body [4096] = "asdf_body \ n";/* Device */
Static int
Init_module
(){
Printk ("Hi, This 'a Simple Device File! \ N ");
Asdf_major = register_chrdev (0, "A Simple Device File", & asdf_fops);/* apply for the master Device Number of the character Device */
If (asdf_major <0) return asdf_major;/* if the application fails, the error number is returned */
Printk ("The major is: % d \ n", asdf_major);/* display The applied master device Number */
Return 0;/* initialize the module normally */
}
Static void
Cleanup_module
(){
Unregister_chrdev (asdf_major, "A Simple Device File");/* The Device does not exist after cancellation */
Printk ("A Simple Device has been removed, Bye! \ N ");
}
/*
* Compile this module and load it,
* If it is normal, the primary device number of your device is displayed.
* Now your device has been set up. Let's test it.
* Assume that the master device number applied for by your module is 254,
* Run "mknod abc c 254 0" to create the device file abc.
* You can test the function as a 4096-byte memory block,
* For example, "cat abc", "cp abc image", "cp image abc ",
* You can use it to write several applications for communication.
* Introduce two things that need attention,
* 1. The display of printk () can only be seen in non-graphic terminals,
* Second, it is best to unmount the loaded modules after they are not used.
* If you are unfamiliar with Linux system calls, we recommend that you read APUE first.
*/
Static int
Asdf_open/* open callback */
(
Struct inode * inode,
Struct file * filp
){
Printk ("cmd_^: open % s \ n ",\
Current-> comm );
/*
* The running environment of the application is provided by the kernel, and the running environment of the kernel is provided by the hardware.
* Current is a pointer to the current process,
* There is no need to understand the details of current.
* The current process is on this device,
* If 0 is returned, it indicates that the file is successfully opened and the kernel will give it a file descriptor.
* Comm is the command string of the current process in Shell.
*/
Return 0;
}
Static int
Asdf_release/* close callback */
(
Struct inode * inode,
Struct file * filp
){
Printk ("pai_^: close \ n ");
Return 0;
}
Static ssize_t
Asdf_read/* read callback */
(
Struct file * filp,
Char * buf,
Size_t count,
Loff_t * f_pos
){
Loff_t pos;
Pos = * f_pos;/* file read/write location */
If (pos = 4096) | (count> 4096) return 0;/* determine whether the device tail is reached or the write length exceeds the device size */
Pos + = count;
If (pos> 4096 ){
Count-= (pos-4096 );
Pos = 4096;
}
If (copy_to_user (buf, asdf_body + * f_pos, count) return-EFAULT;/* write data to the Application Space */
* F_pos = pos;/* change the file's read/write location */
Return count;/* return the number of bytes read */
}
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