Device Driver Development 1

Source: Internet
Author: User

Character Device DriverProgramFramework:

1. Define the struct static struct file_operations, and define the open, close, read, write, and control functions of the device;

2. Implement the functions defined in the struct in vitro;

3. register the driver module with the kernel.

Write the following program test. C in Linux

# Include <Linux/types. h>/* basic type definition */
# Include <Linux/fs. h>/* related header files used by the file system */
# Include <Linux/mm. h>
# Include <Linux/errno. h>
# Include <ASM/segment. h>
# Include <ASM/uaccess. h>
# Include <Linux/module. h>
Unsigned int test_major = 0;

// This is the READ function.
Static int read_test (struct inode * node, struct file * file, char * Buf, int count ){
Int left;
If (access_ OK (verify_write, Buf, count ))
For (Left = count; left> 0; left --){
_ Put_user ('A', Buf );
Buf ++;
}
Return count;
}

// This Is The Write function.
Static int write_test (struct inode * inode, struct file * file, const char * Buf, int count) {// 33
Return count;
}

// This is an open function.
Static int open_test (struct inode * inode, struct file * file) {// 36
// Mod_inc_use_count;/* increase the module count by 1, indicating that a device in the current kernel is loaded into the kernel */
Try_module_get (this_module );
Return 0;
}

// This is the release function.
Static void release_test (struct inode * inode, struct file * file) {// 41
// Mod_dec_use_count;
Module_put (this_module );
}

 

// This is the key struct.
Struct file_operations test_fops =
{
. Owner = this_module,
. Read = read_test,
. Write = write_test,
. Open = open_test,
. Release = release_test,
};

// This is the initialization function.
Int init_module (void ){
Int result;
Result = register_chrdev (0, "test", & test_fops);/* Register. The entire interface for device operations */
If (result <0 ){
Printk (kern_info "test: Can't Get Major number \ n ");
Return result;
}
If (test_major = 0)
Test_major = result;/* dynamic */
Return 0;
}

Void cleanup_module (void ){
Unregister_chrdev (test_major, "test ");
}

Write another MAKEFILE file:

OBJ-M: = test. o
Kerneldir: =/usr/src/linux-headers-2.6.38-8-generic (kernel location)

PWD: = $ (shell PWD)
Default:
$ (Make)-C $ (kerneldir) M = $ (PWD) Modules

 

1. Execute make to generate the following files:

EXE. C is a test applet for testing the driver, including read/write operations. A. Out is compiled by exe. C.

2. Install the driver in the system: insmod-F test. Ko

3. lsmod will be able to see a device named test.

4. Find test in CAT/proc/devices and check the device number registered in the kernel. For example, if the device Number of test is 250, perform this step first, if the test program runs after the next step, the device cannot be opened:

5. Create a device file: mknod/dev/test c 250 0

By now, the compilation of the device driver framework has been completed. for the specific device driver, modify the test_open, test_read, and test_write functions in the program.

 

 

 

 

 

 

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.