/*****************************
*
* 驱动程序模板
* 版本:V1
* 使用方法(末行模式下):
* :%s/xxx/"你的驱动名称"/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>

/**************** 基本定义 **********************/
//内核空间缓冲区定义
#if 0
#define KB_MAX_SIZE 20
#define kbuf[KB_MAX_SIZE];
#endif

//加密函数参数内容: _IOW(IOW_CHAR , IOW_NUMn , IOW_TYPE)
//加密函数用于xxx_ioctl函数中
//使用举例: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

//初始化函数必要资源定义
//用于初始化函数当中
//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;

/**************** 结构体 file_operations 成员函数 *****************/
//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)
{
//常规:
//cmd值自行进行修改
case 0x1:
{
if(arg == 0x1) //第二条件;
{

}
}
break;

//带密码保护:
//请在"基本定义"进行必要的定义
case _IOW(IOW_CHAR,IOW_NUM1,IOW_TYPE):
{
if(arg == 0x1) //第二条件
{

}

}
break;

default:
break;
}

return ret_v;
}

/***************** 结构体: 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*******************/
//条件值变量,用于指示资源是否正常使用
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");

//函数alloc_chrdev_region主要参数说明:
//参数2: 次设备号
//参数3: 创建多少个设备
if( ( ret_v = alloc_chrdev_region(&dev_num,0,1,"xxx") ) < 0 )
{
goto dev_reg_error;
}
init_flag = 1; //标示设备创建成功;

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");

//------------ 请在此添加您的初始化程序 --------------//

//如果需要做错误处理,请: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);

//------------------ 请在此添加您的错误处理内容 ----------------//
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)
{
//---------- 请在这里释放您的程序占有的资源 ---------//
printk("free your resources...\n");

printk("free finish\n");
//---------------------- END -------------------//
}

if(init_flag == 1)
{
//释放初始化使用到的资源;
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 ***************************/

Linux驱动开发之字符设备模板的更多相关文章

  1. Linux驱动开发2——字符设备驱动

    1.申请设备号 #include <linux/fs.h> int register_chrdev_region(dev_t first, unsigned int count, char ...

  2. Linux驱动开发之字符设备驱动模型之file_operations

    90%的驱动模型都是按照下图开发的 下面来说下设备描述结构是什么东西 打开Linux-2.6.32.2的Source Insight 工程,搜索cdev 比如一个应用程序需要调用read和write这 ...

  3. linux驱动开发之块设备学习笔记

    我的博客主要用来存放我的学习笔记,如有侵权,请与我练习,我会立刻删除.学习参考:http://www.cnblogs.com/yuanfang/archive/2010/12/24/1916231.h ...

  4. 驱动开发--【字符设备、块设备简介】【sky原创】

    驱动开发   字符设备,块设备,网络设备   字符设备 以字节流的方式访问, 不能随机访问 有例外,显卡.EEPROM可以随机访问   EEPROM可以擦写1亿次,是一种字符设备,可以随机访问 读写是 ...

  5. 初入android驱动开发之字符设备(一)

    大学毕业,初入公司,招进去的是android驱动开发工程师的岗位,那时候刚进去,首先学到的就是如何搭建kernel.android的编译环境,然后就是了解如何刷设备以及一些最基本的工具.如adb.fa ...

  6. Linux 驱动框架---cdev字符设备驱动和misc杂项设备驱动

    字符设备 Linux中设备常见分类是字符设备,块设备.网络设备,其中字符设备也是Linux驱动中最常用的设备类型.因此开发Linux设备驱动肯定是要先学习一下字符设备的抽象的.在内核中使用struct ...

  7. 初入android驱动开发之字符设备(四-中断)

    上一篇讲到android驱动开发中,应用是怎样去操作底层硬件的整个流程,实现了按键控制led的亮灭.当然,这是一个非常easy的实例,只是略微演变一下,就能够得到广泛的应用. 如开发扫描头,应用透过监 ...

  8. 【Linux 驱动】简单字符设备驱动架构(LED驱动)

    本文基于icool210开发板,内核版本:linux2.6.35: 驱动代码: (1)头文件:led.h #ifndef __LED_H__ #define __LED_H__ #define LED ...

  9. 【linux驱动笔记】字符设备驱动相关数据结构与算法

    欢迎转载,转载时需保留作者信息,谢谢. 邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http:// ...

随机推荐

  1. YUV主要采样格式理解

    主要的采样格式有YCbCr 4:2:0.YCbCr 4:2:2.YCbCr 4:1:1和 YCbCr 4:4:4.其中YCbCr 4:1:1 比较常用,其含义为:每个点保存一个 8bit 的亮度值(也 ...

  2. HDU 2209 翻纸牌游戏 状态BFS

    翻纸牌游戏 Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem De ...

  3. 初探appium之appium的使用

    上一篇中已经讲了python+appium的环境搭建.这里简单的讲一下appium的使用. 我也是第一次使用appium,看了教程问了人.知道appium可以通过模拟也可以连接上手机使用.本篇中,先使 ...

  4. C/C++程序员面试易错题

    c部分::::::::::::::::::::::::::::::::::: . 关键字volatile有什么含意? 并给出三个不同的例 子. [参考答案]一个定义为volatile的变量是说这变量可 ...

  5. MVC+EF更新数据库

    要使用代码先行提供的迁移功能来保证模型和数据库自动匹配,在库程序包管理器里依次执行以下命令:1.启用迁移功能:Enable-Migrations -ContextTypeName MvcMovie.M ...

  6. MFC六大核心机制之二:运行时类型识别(RTTI)

    上一节讲的是MFC六大核心机制之一:MFC程序的初始化,本节继续讲解MFC六大核心机制之二:运行时类型识别(RTTI). typeid运算子 运行时类型识别(RTTI)即是程序执行过程中知道某个对象属 ...

  7. oracle权限

    Oracle 权限 权限允许用户访问属于其它用户的对象或执行程序,ORACLE系统提供三种权限:Object 对象级.System 系统级.Role 角色级.这些权限可以授予给用户.特殊用户publi ...

  8. postgresql安装配置

    一,什么是postgresql PostgreSQL是以加州大学伯克利分校计算机系开发的 POSTGRES 版本 4.2 为基础的对象关系型数据库管理系统(ORDBMS),简称pgsql,它支持大部分 ...

  9. Linux下多线程编程

    一.为什么要引入线程? 使用多线程的理由之一是和进程相比,它是一种非常"节俭"的多任务操作方式.在Linux系统下,启动一个新的进程必须分配给它独立的地址空间,建立众多的数据表来维 ...

  10. 移动端的头部标签和meta

    <!DOCTYPE html><!--HTML5 doctype--> <html> <head> <title>xxx</title ...