1.文件结构和在源码中的位置
字符设备的驱动一般位于 package/kernel 目录下面,文件结构如下:
test@test-Inspiron-3670:~/work/openwrt1505_mt7628/package/kernel$ tree mydrv/
mydrv/
├── Makefile
└── src
├── Makefile
└── mydrv.c
2.文件介绍
2.1查看 : cat mydrv/Makefile
mydrv/
├── Makefile
#
#
#
include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/kernel.mk
include $(INCLUDE_DIR)/package.mk
PKG_NAME:=mydrv
PKG_RELEASE:=1
define KernelPackage/mydrv
SUBMENU:=Other modules
TITLE:=mydrv
FILES:=$(PKG_BUILD_DIR)/mydrv.ko
KCONFIG:=
endef
define KernelPackage/mydrv/description
This is a mydrv drivers
endef
MAKE_OPTS:= \
ARCH="$(LINUX_KARCH)" \
CROSS_COMPILE="$(TARGET_CROSS)" \
SUBDIRS="$(PKG_BUILD_DIR)"
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,mydrv))
2.2查看 : cat mydrv/src/Makefile
obj-m += mydrv.o
2.3查看驱动源码cat mydrv/src/mydrv.c
/*****************************
*
* 驱动程序模板
* 版本:V1
* 使用方法(末行模式下):
* :%s/mydrv/"你的驱动名称"/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)
//加密函数用于mydrv_ioctl函数中
//使用举例:ioctl(fd , _IOW('L',0x80,long) , 0x1);
//#define NUMn mydrv , 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 mydrv_cdev;
//auto "mknode /dev/mydrv c dev_num minor_num"
struct class *mydrv_class = NULL;
struct device *mydrv_device = NULL;
/**************** 结构体 file_operations 成员函数 *****************/
//open
static int mydrv_open(struct inode *inode, struct file *file)
{
printk("mydrv drive open...\n");
return 0;
}
//close
static int mydrv_close(struct inode *inode , struct file *file)
{
printk("mydrv drive close...\n");
return 0;
}
//read
static ssize_t mydrv_read(struct file *file, char __user *buffer,
size_t len, loff_t *pos)
{
int ret_v = 0;
printk("mydrv drive read...\n");
return ret_v;
}
//write
static ssize_t mydrv_write( struct file *file , const char __user *buffer,
size_t len , loff_t *offset )
{
int ret_v = 0;
printk("mydrv drive write...\n");
return ret_v;
}
//unlocked_ioctl
static int mydrv_ioctl (struct file *filp , unsigned int cmd , unsigned long arg)
{
int ret_v = 0;
printk("mydrv 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 mydrv_fops = {
.owner = THIS_MODULE,
.open = mydrv_open,
.release = mydrv_close,
.read = mydrv_read,
.write = mydrv_write,
.unlocked_ioctl = mydrv_ioctl,
};
/************* functions: init , exit*******************/
//条件值变量,用于指示资源是否正常使用
unsigned char init_flag = 0;
unsigned char add_code_flag = 0;
//init
static __init int mydrv_init(void)
{
int ret_v = 0;
printk("mydrv drive init...\n");
//函数alloc_chrdev_region主要参数说明:
//参数2: 次设备号
//参数3: 创建多少个设备
if( ( ret_v = alloc_chrdev_region(&dev_num,0,1,"mydrv") ) < 0 )
{
goto dev_reg_error;
}
init_flag = 1; //标示设备创建成功;
printk("The drive info of mydrv:\nmajor: %d\nminor: %d\n",
MAJOR(dev_num),MINOR(dev_num));
cdev_init(&mydrv_cdev,&mydrv_fops);
if( (ret_v = cdev_add(&mydrv_cdev,dev_num,1)) != 0 )
{
goto cdev_add_error;
}
mydrv_class = class_create(THIS_MODULE,"mydrv");
if( IS_ERR(mydrv_class) )
{
goto class_c_error;
}
mydrv_device = device_create(mydrv_class,NULL,dev_num,NULL,"mydrv");
if( IS_ERR(mydrv_device) )
{
goto device_c_error;
}
printk("auto mknod success!\n");
//------------ 请在此添加您的初始化程序 --------------//
//如果需要做错误处理,请:goto mydrv_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(&mydrv_cdev);
unregister_chrdev_region(dev_num, 1);
init_flag = 0;
return PTR_ERR(mydrv_class);
device_c_error:
printk("device_create failed\n");
cdev_del(&mydrv_cdev);
unregister_chrdev_region(dev_num, 1);
class_destroy(mydrv_class);
init_flag = 0;
return PTR_ERR(mydrv_device);
//------------------ 请在此添加您的错误处理内容 ----------------//
mydrv_error:
add_code_flag = 0;
return -1;
//-------------------- END -------------------//
init_success:
printk("mydrv init success!\n");
return 0;
}
//exit
static __exit void mydrv_exit(void)
{
printk("mydrv drive exit...\n");
if(add_code_flag == 1)
{
//---------- 请在这里释放您的程序占有的资源 ---------//
printk("free your resources...\n");
printk("free finish\n");
//---------------------- END -------------------//
}
if(init_flag == 1)
{
//释放初始化使用到的资源;
cdev_del(&mydrv_cdev);
unregister_chrdev_region(dev_num, 1);
device_unregister(mydrv_device);
class_destroy(mydrv_class);
}
}
/**************** module operations**********************/
//module loading
module_init(mydrv_init);
module_exit(mydrv_exit);
//some infomation
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("from Jafy");
MODULE_DESCRIPTION("mydrv drive");
/********************* The End ***************************/
3.配置编译 make menuconfig
make menuconfig —>kernel module -->other modules -->kmod-mydrv
4.编译安装
编译:
make package/kernel/mydrv/compile V=99
发送到开发板
scp bin/ramips/packages/base/kmod-mydrv_3.18.84-1_ramips_24kec.ipk root@192.168.1.110:/mkdir/
安装:进入开发板 mkdir
okkg install kmod-mydrv_3.18.84-1_ramips_24kec.ipk
安装好的在 ls /lib/modules/3.18.84/
lnsmode mydrv.ko
rmmode mydrv.ko
5.设置开机加载
虽然我们通过 opkg insall 得到了.ko 为后缀的驱动模块,但是每次要使用都得进行
insmod,比较麻烦,我们可以让系统开机后自动的加载该驱动模块。
首先进入到特定的目录:
cd /etc/modules.d
然后创建一个文件并且写入一些信息:
vi 61-mydrv 说明:61 表示不使用外接 usb 类的自启动程序,分隔符‘-’后面接
驱动模块名称!
在创建的该文件中写入: mydrv 即驱动的名称。
最后,必须保证 mydrv.ko 文件即我们的驱动模块文件放在: /lib/modules/3.14.25/
好的,重新启动系统即可: reboot
可以通过命令查询当前已经加载的驱动模块:lsmod