Linux character driver Template

Source: Internet
Author: User
Linux character driver template-general Linux technology-Linux programming and kernel information. The following is a detailed description. The linux character driver template is applicable to kernels smaller than 2.4.

# Include
# Include
# Include
# Include
# Include
# Include
# Include


# Define MAJOR_NUM 125
# Define DEVICE_NAME "emptychr"

Static ssize_t test_read (struct file * file, char * buf, size_t count, loff_t * f_pos)
{
Return count;
}
Static ssize_t test_write (struct file * file, const char * buf, size_t count, loff_t * f_pos)
{
Return count;
}
Static int test_open (struct inode * inode, struct file * file)
{
MOD_INC_USE_COUNT;
Return 0;
}
Static int test_release (struct inode * inode, struct file * file)
{
MOD_DEC_USE_COUNT;
Return 0;
}
Static int test_ioctl (struct inode * inode, struct file * file, unsigned int cmd, unsigned long arg)
{
Return 0;
}
Struct file_operations test_fops = {
Read: test_read,
Write: test_write,
Open: test_open,
Release: test_release,
Ioctl: test_ioctl
};
Int test_init (void)
{
Int result;
Result = register_chrdev (MAJOR_NUM, DEVICE_NAME, & test_fops );
If (result <0 ){
Printk (KERN_INFO "test: can't get major number \ n ");
Return result;
}

Printk ("init module \ n ");
Return 0;

}
Void test_exit (void)
{
Unregister_chrdev (MAJOR_NUM, DEVICE_NAME );
Printk ("cleanup_module \ n ");
}


Module_init (test_init );
Module_exit (test_exit );

MODULE_LICENSE ("GPL ");
MODULE_AUTHOR ("huangxb ");

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.