Linux character device driver framework (old method)

Source: Internet
Author: User

#include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/fs.h > #include <linux/delay.h> #include <asm/uaccess.h> #include <asm/irq.h> #include <asm/io.h > #include <asm/arch/regs-gpio.h> #include <asm/hardware.h> #include <linux/cdev.h> #include < Linux/types.h> #defineXxx_device_count1/* Automatically create device node class */static struct class *xxx_dev_class;static struct Class_device *xxx_dev_class_dev;/*XXX Device related operating functions: Open, read, write, close, IOCTL, etc. */static int xxx_dev_open (struct inode *inode, struct file *filp) {PRINTK ("Open xxx device ok.\n");return 0;} static int xxx_dev_close (struct inode *inode, struct file *filp) {PRINTK ("Close xxx device ok.\n");return 0;} static int xxx_dev_write (struct file *file, const char __user *buf, size_t count, loff_t *ppos) {PRINTK ("Write xxx device ok.\n");return 0;} static int xxx_dev_read (struct file *file, const char __user *buf, size_t count, loff_t PPOs) {PRINTK ("Read xxx device ok.\n");return 0;} static int xxx_dev_ioctl (struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg) {PRINTK ("Driver:get cmd%d.\n", cmd);return 0;} /*XXX device operation function structure body */struct file_operations xxx_fops = {. Owner = This_module,. open = Xxx_dev_open,. Release = Xxx_dev_close,. Read = Xxx_dev_read,. write = Xxx_dev_write,. IOCTL = xxx_dev_ioctl,};/*XXX Device driver module registration and uninstallation */int xxx_major = 0;static int __init initialization_xxx_dev (void) {/* Register the device number */PRINTK ("before register xxx Major =%d\n", xxx_major);if (xxx_major) {Register_chrdev (xxx_major, "xxx", &xxx_fops);} else {Xxx_major = Register_chrdev (0, "xxx", &xxx_fops);}PRINTK ("after register xxx Major =%d\n", xxx_major);/* Automatically generate device node */Xxx_dev_class = Class_create (This_module, "Xxx_dev");Xxx_dev_class_dev = class_device_create (Xxx_dev_class, NULL, MKDEV (xxx_major, 0), NULL, "XXX");/* Module initialization must return 0 */PRINTK ("Module register ok.\n");return 0;} static void __exit Cleanup_xxx_dev (void) {/* Delete device files */Unregister_chrdev (xxx_major, "xxx");Class_device_unregister (Xxx_dev_class_dev);Class_destroy (Xxx_dev_class);PRINTK ("Module unregister ok.\n");} /*Module registration and uninstallation */module_init (Initialization_xxx_dev); Module_exit (Cleanup_xxx_dev);/*Module reference: Insmod Char_driver_frame_old.ko xxx_major=xxx*/module_param (xxx_major, int, s_irugo);/*The relevant declaration of the module */module_author ("Lhbo"); Module_description ("GPIO Driver for xxx"); Module_license ("GPL");

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.