/*****************************
*
* Driver Templates
* Version: V1
* Usage method (in the last line mode):
*:%s/xxx/"Your driver name"/g
*
*******************************/
#include <linux/mm.h>
#include <linux/miscdevice.h>
#include <linux/slab.h>
# Include <linux/vmalloc.h>
#include <linux/mman.h>
#include <linux/random.h>
#include <linux/init.h>
#include <linux/raw.h>
#include <linux/tty.h>
#include <linux/ Capability.h>
#include <linux/ptrace.h>
#include <linux/device.h>
#include <linux/ Highmem.h>
#include <linux/crash_dump.h>
#include <linux/backing-dev.h>
#include < Linux/bootmem.h>
#include <linux/splice.h>
#include <linux/pfn.h>
#include <linux/ Export.h>
#include <linux/io.h>
#include <linux/aio.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <asm/uaccess.h>
#include <linux/ioctl.h>
/**************** Basic Definition **********************/
Kernel space Buffer definition
#if 0
#define KB_MAX_SIZE 20
#define KBUF[KB_MAX_SIZE];
#endif
Cryptographic function Parameter contents: _iow (Iow_char, Iow_numn, Iow_type)
The cryptographic function is used in the XXX_IOCTL function
Examples of use: IOCTL (FD, _iow (' L ', 0x80,long), 0x1);
#define Numn xxx, if you need!
#define Iow_char ' L '
#define Iow_type Long
#define IOW_NUM1 0x80
Initialization function necessary Resource definition
Used in initialization functions
Device number;
dev_t Dev_num;
struct DEV
struct Cdev Xxx_cdev;
Auto "Mknode/dev/xxx c dev_num minor_num"
struct class *xxx_class = NULL;
struct device *xxx_device = NULL;
/**************** struct File_operations member function *****************/
Open
static int Xxx_open (struct inode *inode, struct file *file)
{
PRINTK ("xxx drive open...\n");
return 0;
}
Close
static int xxx_close (struct inode *inode, struct file *file)
{
PRINTK ("xxx drive close...\n");
return 0;
}
Read
Static ssize_t xxx_read (struct file *file, char __user *buffer,
size_t Len, loff_t *pos)
{
int ret_v = 0;
PRINTK ("xxx drive read...\n");
return ret_v;
}
Write
Static ssize_t xxx_write (struct file *file, const char __user *buffer,
size_t Len, loff_t *offset)
{
int ret_v = 0;
PRINTK ("xxx drive write...\n");
return ret_v;
}
Unlocked_ioctl
static int xxx_ioctl (struct file *filp, unsigned int cmd, unsigned long arg)
{
int ret_v = 0;
PRINTK ("xxx drive ioctl...\n");
Switch (CMD)
{
Conventional:
The CMD value modifies itself
Case 0x1:
{
if (arg = = 0x1)//second condition;
{
}
}
Break
With password protection:
Please make the necessary definitions in the "basic definition"
Case _iow (Iow_char,iow_num1,iow_type):
{
if (arg = = 0x1)//second condition
{
}
}
Break
Default
Break
}
return ret_v;
}
/***************** structural body: file_operations ************************/
struct
static const struct File_operations Xxx_fops = {
. Owner = This_module,
. open = Xxx_open,
. Release = Xxx_close,
. Read = Xxx_read,
. write = Xxx_write,
. unlocked_ioctl= Xxx_ioctl,
};
/************* Functions:init, exit*******************/
A condition value variable that indicates whether the resource is working properly
unsigned char init_flag = 0;
unsigned char add_code_flag = 0;
Init
static __init int xxx_init (void)
{
int ret_v = 0;
PRINTK ("xxx drive init...\n");
function alloc_chrdev_region Main parameter description:
Parameter 2: Secondary device number
Parameter 3: Number of devices created
if ((Ret_v = Alloc_chrdev_region (&dev_num,0,1, "xxx")) < 0)
{
Goto Dev_reg_error;
}
Init_flag = 1; Marking device creation success;
PRINTK ("The drive info of xxx:\nmajor:%d\nminor:%d\n",
MAJOR (Dev_num), MINOR (Dev_num));
Cdev_init (&xxx_cdev,&xxx_fops);
if ((Ret_v = Cdev_add (&xxx_cdev,dev_num,1))! = 0)
{
Goto Cdev_add_error;
}
Xxx_class = Class_create (this_module, "xxx");
if (Is_err (Xxx_class))
{
Goto Class_c_error;
}
Xxx_device = Device_create (xxx_class,null,dev_num,null, "xxx");
if (Is_err (Xxx_device))
{
Goto Device_c_error;
}
PRINTK ("Auto Mknod success!\n");
------------Please add your initialization program here--------------//
If you need to do error handling, please: Goto Xxx_error;
Add_code_flag = 1;
----------------------END---------------------------//
Goto init_success;
Dev_reg_error:
PRINTK ("Alloc_chrdev_region failed\n");
return ret_v;
Cdev_add_error:
PRINTK ("Cdev_add failed\n");
Unregister_chrdev_region (Dev_num, 1);
Init_flag = 0;
return ret_v;
Class_c_error:
PRINTK ("Class_create failed\n");
Cdev_del (&xxx_cdev);
Unregister_chrdev_region (Dev_num, 1);
Init_flag = 0;
Return Ptr_err (Xxx_class);
Device_c_error:
PRINTK ("Device_create failed\n");
Cdev_del (&xxx_cdev);
Unregister_chrdev_region (Dev_num, 1);
Class_destroy (Xxx_class);
Init_flag = 0;
Return Ptr_err (Xxx_device);
------------------please add your error handling content here----------------//
Xxx_error:
Add_code_flag = 0;
return-1;
--------------------END-------------------//
Init_success:
PRINTK ("XXX init success!\n");
return 0;
}
Exit
static __exit void Xxx_exit (void)
{
PRINTK ("xxx drive exit...\n");
if (Add_code_flag = = 1)
{
----------Please release the resources your program occupies---------//
PRINTK ("Free Your resources...\n");
PRINTK ("Free finish\n");
----------------------END-------------------//
}
if (Init_flag = = 1)
{
Releases the resources used to initialize the initialization;
Cdev_del (&xxx_cdev);
Unregister_chrdev_region (Dev_num, 1);
Device_unregister (Xxx_device);
Class_destroy (Xxx_class);
}
}
/**************** Module operations**********************/
Module loading
Module_init (Xxx_init);
Module_exit (Xxx_exit);
Some infomation
Module_license ("GPL v2");
Module_author ("from Jafy");
Module_description ("xxx drive");
/********************* the End ***************************/
The character device template for Linux driver development