Linux字符设备驱动2——杂项设备驱动
杂项设备驱动:linux中无法归类的设备定义为杂项设备。
Note1:杂项设备主设备固定为 10 ;
Note2:杂项设备会自己调用class_create()
和device_create()
创建设备节点。
1.miscdevice结构体
头文件:include/linux/miscdevice.h
struct miscdevice {
int minor;
const char *name;
const struct file_operations *fops;
struct list_head list;
struct device *parent;
struct device *this_device;
const struct attribute_group **groups;
const char *nodename;
umode_t mode;
};
Note3:次设备号minor一般使用宏MISC_DYNAMIC_MINOR
,表示自动分配次设备号。
Note4:杂项设备主要依赖于次设备号来管理。
2.注册与卸载
//注册
int misc_register(struct miscdevice *misc);
//卸载
int misc_deregister(struct miscdevice *misc);
Demo
1.驱动程序
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/io.h>
#include <linux/miscdevice.h>
/* ---------------------------------------------------------------- */
/* 1.字符设备结构体定义 */
struct misc_dev
{
char misc_opr; //LED操作,on/off
};
struct misc_dev miscled;/* 定义设备变量 */
/* registers */
// IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 地址:0x02290000 + 0x14
static volatile unsigned int *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3;
// GPIO5_GDIR 地址:0x020AC004
static volatile unsigned int *GPIO5_GDIR;
//GPIO5_DR 地址:0x020AC000
static volatile unsigned int *GPIO5_DR;
/* 2.file_operartions结构体中对应函数的实现 */
//open函数的实现
static int misc_open(struct inode *inode, struct file *file)
{
file->private_data = &miscled;
/*
enbale gpio5
configure pin as gpio
configure gpio as output
*/
*IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 &= ~0xf;
*IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 |= 0x5;
*GPIO5_GDIR |= (1<<3);
return 0;
}
//read函数的实现
static ssize_t misc_read(struct file * file,char __user *buf,size_t size,loff_t * offset)
{
printk("%s %s line %d\n",__FILE__,__FUNCTION__,__LINE__);
//err = copy_to_user(buf,kernel_buf,MIN(1024,size));
return 0;
}
//write函数的实现
static ssize_t misc_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
int err;
struct misc_dev * misc_operation = (struct misc_dev *)file->private_data;
/*copy from user:ge data from app*/
err = copy_from_user(&misc_operation->misc_opr,buf,1);
if(err != 0)
{
printk("copy_from user error\n");
return -1;
}
/* to set gpio register:out 1/0 */
if(misc_operation->misc_opr)
{
/*set gpio to let led on*/
*GPIO5_DR &= ~(1<<3);
printk("led on\n");
}
else
{
/*set gpio to let led off*/
*GPIO5_DR |= (1<<3);
printk("led off\n");
}
return 1;
}
//close函数的实现
static int misc_close(struct inode * node,struct file * file)
{
printk("%s %s line %d\n",__FILE__,__FUNCTION__,__LINE__);
return 0;
}
/* 3.file_operations结构体的定义 */
struct file_operations misc_drv = {
.owner = THIS_MODULE,
.open = misc_open,
.read = misc_read,
.write = misc_write,
.release = misc_close,
};
struct miscdevice misc_dev = {
.minor = MISC_DYNAMIC_MINOR,
.name = "misc_dev",
.fops = &misc_drv,
};
/* 4.驱动入口函数 */
static int __init misc_init(void)
{
int ret;
ret = misc_register(&misc_dev);
if(ret < 0)
{
printk("misc init\n");
goto err_misc_register;
}
/* 虚拟地址映射 */
IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = ioremap(0x02290000 + 0x14,4);
if(IS_ERR(IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3))
{
ret = PTR_ERR(IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3);
goto err_ioremap;
}
GPIO5_GDIR = ioremap(0x020AC004,4);
if(IS_ERR(GPIO5_GDIR))
{
ret = PTR_ERR(GPIO5_GDIR);
goto err_ioremap;
}
GPIO5_DR = ioremap(0x020AC000,4);
if(IS_ERR(GPIO5_DR) )
{
ret = PTR_ERR(GPIO5_DR);
goto err_ioremap;
}
return 0;
err_ioremap:
iounmap(IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3);
iounmap(GPIO5_GDIR);
iounmap(GPIO5_DR);
err_misc_register:
return ret;
}
/* 5.驱动出口函数 */
static void __exit misc_exit(void)
{
printk("%s %s line %d\n",__FILE__,__FUNCTION__,__LINE__);
iounmap(IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3);
iounmap(GPIO5_GDIR);
iounmap(GPIO5_DR);
misc_deregister(&misc_dev);
}
/* 6.其他完善信息 */
module_init(misc_init); /* 入口函数*/
module_exit(misc_exit); /* 出口函数 */
MODULE_LICENSE("GPL");
MODULE_AUTHOR("To Hui");
MODULE_DESCRIPTION("Driver:misc driver;Hardware operation:LED on/off");
MODULE_VERSION("V1.0.0");
2.应用程序
/*驱动测试程序*/
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
int main(int argc ,char *argv[])
{
int fd;
char status = 0;
if (argc != 3)
{
printf("Usage: %s <dev> <on|off>\n", argv[0]);
printf(" eg: %s /dev/misc_dev on\n", argv[0]);
printf(" eg: %s /dev/misc_dev off\n", argv[0]);
return -1;
}
// open
fd = open(argv[1], O_RDWR);
if (fd < 0)
{
printf("can not open %s\n", argv[0]);
return -1;
}
// write
if (strcmp(argv[2], "on") == 0)
{
status = 1;
}
write(fd, &status, 1);
return 0;
}
3.编译
KERN_DIR = /home/embhui/linux-sdk/Linux-4.9.88
all:
make -C $(KERN_DIR) M=`pwd` modules
$(CROSS_COMPILE)gcc -o application application.c
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
rm -f application
obj-m += misc_driver.o
4.应用
#挂载
insmod misc_device.ko
#查看类
ls /sys/class
#查看主设备号
cat /proc/device
#执行
# 点亮LED
./application /dev/misc_dev on
# 熄灭LED
./application /dev/misc_dev off