#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/*XXX character device structure body */struct Xxx_dev{struct CDEVCdev;unsigned char*sth_of_xxx;Reserved};struct Xxx_dev*xxx_dev;/* 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) {struct Xxx_dev *dev = filp->private_data;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 */dev_t Xxx_devno = 0;int Xxx_major = 0;int Xxx_minor = 0;static int __init initialization_xxx_dev (void) {int ret = 0;/* Apply for device number */PRINTK ("before register xxx Major =%d\n", xxx_major);if (xxx_major) {/* Specify the main device number */Xxx_devno = MKDEV (Xxx_major, Xxx_minor);ret = Register_chrdev_region (Xxx_devno, xxx_device_count, "xxx");} else {/* The system automatically assigns the main device */ret = Alloc_chrdev_region (&xxx_devno, Xxx_minor, xxx_device_count, "xxx");Xxx_major = Major (Xxx_devno);Xxx_minor = minor (Xxx_devno);}if (Ret < 0) {PRINTK ("Can ' t get xxx Major%d\n", xxx_major);return ret;}PRINTK ("after register xxx Major =%d\n", xxx_major);/* Apply for device structure in vivo */Xxx_dev = kmalloc (sizeof (struct xxx_dev), gfp_kernel);if (NULL = = Xxx_dev) {PRINTK ("Kmalloc failed.\n");Unregister_chrdev_region (Xxx_devno, Xxx_device_count);Return-enomem;}/* Character Device registration */Cdev_init (&xxx_dev->cdev, &xxx_fops);Xxx_dev->cdev.owner = This_module;Xxx_dev->cdev.ops = &xxx_fops;ret = Cdev_add (&xxx_dev->cdev, Xxx_devno, Xxx_device_count);if (Ret < 0){PRINTK ("Cdev add failed.\n");Kfree (Xxx_dev);Unregister_chrdev_region (Xxx_devno, Xxx_device_count);return ret;}/* Automatically generate device node */Xxx_dev_class = Class_create (This_module, "Xxx_dev");Xxx_dev_class_dev = class_device_create (Xxx_dev_class, NULL, XXX_DEVNO, NULL, "xxx%d", Xxx_minor);"/dev/xxx0"/* Module initialization must return 0 */PRINTK ("Module register ok.\n");return 0;} static void __exit Cleanup_xxx_dev (void) {/* Delete device files */Cdev_del (&xxx_dev->cdev);Kfree (Xxx_dev);Unregister_chrdev_region (Xxx_devno, Xxx_device_count);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.ko xxx_major=xxx xxx_minor=0*/module_param (xxx_major, int, s_irugo); Module_param (xxx_ minor, int, s_irugo);/*The relevant declaration of the module */module_author ("Lhbo"); Module_description ("GPIO Driver for xxx"); Module_license ("GPL");