First write driver

Source: Internet
Author: User
For the first time, write the driver-general Linux technology-Linux programming and kernel information. For details, refer to the following. The first time I learned the write driver, I tried to write a driver according to linuxdevicedriver3.

# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Unsigned int test_major = 0;
Static int read_test (struct inode * inode, struct file * file, char * buf, int count ){
Int left;
If (verify_area (VERIFY_WRITE, buf, count) =-EFAULT)
Return-EFAULT;
For (left = count; left> 0; left --){
_ Put_user (1, buf, 1 );
Buf ++;
}
Return count;
}

Static int write_test (struct inode * inode, struct file * file, const char * buf, int count ){
Return count;
}

Static void release_test (struct inode * inode, struct file * file ,){
MOD_DEC_USE_COUNT;
}

Struct file_operations test_fops = {
NULL,
Read_test,
Write_test,
NULL,
NULL,
NULL,
NULL,
Open_test,
Release_test,
NULL,
};

Int init_module (void ){
Int result;
Result = regster_chrdev (0, "test", & test_fops );
If (result <0 ){
Printk (KERN_INFO "test: can't get major number! \ N ");
Return result;
}
If (test_major = 0) test_major = result;
Return 0;
}

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

Can a device driver write such a. c file? (I really don't know. I didn't even mention such a complete driver example ),????
Then execute:
Gcc-O2-DMODULE-d1_kenel _-c test. c

Error:
Linux/module. h: No such file or director
Linux/mm. h: No such file or directory
Linux/error. h: No such file or directory
Linux/config. h: No such file or directory
Asm/segment. h: No such file or directory

Why? Why can't gcc find the lib file? I have installed linux-headers-

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.