一、字符设备驱动

我们之前学习过驱动程序的开发,接下来我们接着深入学习字符设备驱动程序的开发。字符设备驱动是比较简单的一种驱动。主要在于实现init、exit、open、read、write等函数。

二、字符设备驱动实例

1、在package/kernel/文件夹下新建一个chardrv文件夹

2、在chardrv文件夹下新建一个Makefile文件,内容如下:

#
# Copyright (C) 2008 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/kernel.mk

PKG_NAME:=chardrv
PKG_RELEASE:=2

include $(INCLUDE_DIR)/package.mk

define KernelPackage/chardrv
SUBMENU:=Other modules
TITLE:=CharDrv
FILES:=$(PKG_BUILD_DIR)/chardrv.ko
KCONFIG:=
endef

define KernelPackage/chardrv/description
Kernel module for register chardrv.
endef

EXTRA_KCONFIG:= \
CONFIG_CHARDRV=m

EXTRA_CFLAGS:= \
$(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=m,%,$(filter %=m,$(EXTRA_KCONFIG)))) \
$(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=y,%,$(filter %=y,$(EXTRA_KCONFIG)))) \

MAKE_OPTS:= \
ARCH="$(LINUX_KARCH)" \
CROSS_COMPILE="$(TARGET_CROSS)" \
SUBDIRS="$(PKG_BUILD_DIR)" \
EXTRA_CFLAGS="$(EXTRA_CFLAGS)" \
$(EXTRA_KCONFIG)

define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
$(CP) ./src/* $(PKG_BUILD_DIR)/
endef

define Build/Compile
$(MAKE) -C "$(LINUX_DIR)" \
$(MAKE_OPTS) \
modules
endef

$(eval $(call KernelPackage,chardrv))

3、在chardrv文件夹下新建一个src文件夹,在src下新建一个Makefile文件,内容为:

obj-${CONFIG_CHARDRV}  += chardrv.o

4、新建一个chardrv.c的驱动文件,文件内容为:

/***************************** 
*
* 字符设备驱动程序模板
*
*******************************/


#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>


/**************** 基本定义 **********************/


//加密函数参数内容: _IOW(IOW_CHAR , IOW_NUMn , IOW_TYPE)
//加密函数用于chardrv_ioctl函数中
//使用举例:ioctl(fd , _IOW('L',0x80,long) , 0x1);
//#define NUMn chardrv
#define IOW_CHAR 'L'
#define IOW_TYPE long
#define IOW_NUM1 0x80


//初始化函数必要资源定义
//用于初始化函数当中
//device number;
//设备号
dev_t dev_num;
//struct dev
//字符设备
struct cdev chardrv_cdev;
//auto "mknode /dev/chardrv c dev_num minor_num"
//自动创建设备对象
struct class *chardrv_class = NULL;
struct device *chardrv_device = NULL;


/**************** 结构体 file_operations 成员函数 *****************/
//open
static int chardrv_open(struct inode *inode, struct file *file)
{
printk("chardrv drive open...\n");


return 0;
}

//close
static int chardrv_close(struct inode *inode , struct file *file)
{
printk("chardrv drive close...\n");


return 0;
}

//read
static ssize_t chardrv_read(struct file *file, char __user *buffer,
size_t len, loff_t *pos)
{
int ret_v = 0;
printk("chardrv drive read...\n");


return ret_v;
}

//write
static ssize_t chardrv_write( struct file *file , const char __user *buffer,
size_t len , loff_t *offset )
{
int ret_v = 0;
printk("chardrv drive write...\n");


return ret_v;
}

//unlocked_ioctl
static int chardrv_ioctl (struct file *filp , unsigned int cmd , unsigned long arg)
{
int ret_v = 0;
printk("chardrv 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 ,该结构体将驱动中的函数和应用层函数关联(例如当调用应用层调用open函数时就会调用驱动中的open函数)************************/
static const struct file_operations chardrv_fops = {
.owner = THIS_MODULE,
.open = chardrv_open,
.release = chardrv_close,
.read = chardrv_read,
.write = chardrv_write,
.unlocked_ioctl = chardrv_ioctl,
};



//使用insmod挂载驱动时回调
static __init int chardrv_init(void)
{
int ret_v = 0;
printk("mydrv drive init...\n");
/*
函数alloc_chrdev_region主要参数说明:
参数1: 自动分配的设备号
参数2: 次设备号
参数3: 创建多少个设备
*/
if( ( ret_v = alloc_chrdev_region(&dev_num,0,1,"chardrv") ) < 0 ) //为chardrv动态分配设备号
{
goto dev_reg_error;
}

//打印主设备号和次设备号
printk("The drive info of chardrv:\nmajor: %d\nminor: %d\n",
MAJOR(dev_num),MINOR(dev_num));

//关联设备和操作函数
cdev_init(&chardrv_cdev,&chardrv_fops);
//注册设备
if( (ret_v = cdev_add(&chardrv_cdev,dev_num,1)) != 0 )
{
goto cdev_add_error;
}
//创建设备类,用于自动创建设备
chardrv_class = class_create(THIS_MODULE,"chardrv");
if( IS_ERR(chardrv_class) )
{
goto class_c_error;
}
//通过设备类创建设备
chardrv_device = device_create(chardrv_class,NULL,dev_num,NULL,"chardrv");
if( IS_ERR(chardrv_device) )
{
goto device_c_error;
}
printk("auto mknod success!\n");

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




//如果需要做错误处理,请:goto mydrv_error;


//---------------------- 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);
return ret_v;

class_c_error:
printk("class_create failed\n");
cdev_del(&chardrv_cdev);
unregister_chrdev_region(dev_num, 1);
return PTR_ERR(chardrv_class);

device_c_error:
printk("device_create failed\n");
cdev_del(&chardrv_cdev);
unregister_chrdev_region(dev_num, 1);
class_destroy(chardrv_class);
return PTR_ERR(chardrv_device);

//------------------ 请在此添加您的错误处理内容 ----------------//
chardrv_error:




return -1;
//-------------------- END -------------------//

init_success:
printk("chardrv init success!\n");
return 0;
}

//使用rmmod卸载驱动是回调该函数
static __exit void chardrv_exit(void)
{
printk("chardrv drive exit...\n");


//释放初始化使用到的资源;
cdev_del(&chardrv_cdev);
unregister_chrdev_region(dev_num, 1);
device_unregister(chardrv_device);
class_destroy(chardrv_class);

}


/**************** module operations**********************/
//声明加载函数和卸载函数
module_init(chardrv_init);
module_exit(chardrv_exit);

//some infomation
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("YANG");


/********************* The End ***************************/

5、最后就是编译了,和上一节一样进行编译就OK了!