1.函数说明
1.1、注册杂项设备
int misc_register(struct miscdevice * misc)
头文件:#include <linux/miscdevice.h>
功能:注册一个杂项设备
参数:misc- 杂项设备的核心结构指针,至少已经实现minor,name,fops三个成员。
返回值:0,表示注册成功;负数,注册失败。
说明:注册成功之后,会在/dev目录下生成一个设备节点文件。
2.2、注销函数
int misc_deregister(struct miscdevice *misc)
头文件:#include <linux/miscdevice.h>
功能:注销一个已经存在杂项设备
参数:misc,杂项设备的核心结构指针,已经注册的struct miscdevice结构。
返回值:0,表示注销成功;负数,注销失败
杂项设备的核心数据结构struct miscdevice:(头文件中已经定义好)
struct miscdevice {
int minor;
const char *name;
const struct file_operations *fops;
struct list_head list;
struct device *parent;
struct device *this_device;
const char *nodename;
umode_t mode;
};
结构体struct miscdevice结构中的函数结构液体struct file_operations(头文件中已经定义好)
struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
int (*readdir) (struct file *, void *, filldir_t);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, loff_t, loff_t, int datasync);
int (*aio_fsync) (struct kiocb *, int datasync);
int (*fasync) (int, struct file *, int);
int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
int (*check_flags)(int);
int (*flock) (struct file *, int, struct file_lock *);
ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
int (*setlease)(struct file *, long, struct file_lock **);
long (*fallocate)(struct file *file, int mode, loff_t offset, loff_t len);
};
代码例子:
驱动代码
#include <linux/kernel.h>
#include <linux/module.h>
//包含必须的头文件
#include <linux/fs.h>
#include <linux/miscdevice.h>
//以下是文件操作方法的具体实现代码
static int xxx_open (struct inode *pinode, struct file *pfile)
{
printk("line:%d,%s is call\n",__LINE__,__FUNCTION__);
return 0;
}
static ssize_t xxx_read (struct file *pfile, char __user *buf, size_t count, loff_t *poff)
{
printk("line:%d,%s is call\n",__LINE__,__FUNCTION__);
return count;
}
static ssize_t xxx_write (struct file *pfile, const char __user *buf, size_t count, loff_t *poff)
{
printk("line:%d,%s is call\n",__LINE__,__FUNCTION__);
return count;
}
static int xxx_release (struct inode *pinode, struct file *pfile)
{
printk("line:%d,%s is call\n",__LINE__,__FUNCTION__);
return 0;
}
//文件操作方法集合指针
//文件操作方法:根据具体设备实现需要的功能
static const struct file_operations char_dev_fops={
.open = xxx_open,
.write = xxx_write,
.read = xxx_read,
.release = xxx_release,
};
//定义核心数据结构
#define DEV_NAME "mymisc"
static struct miscdevice misc_dev={
.minor = 255,
.name = DEV_NAME,
.fops = &char_dev_fops,
};
static int __init chrdev_test_init(void)
{
int ret;
//注册核心结构
ret = misc_register(&misc_dev);
if(ret < 0)
{
printk("misc_register erron!\n");
return ret;
}
printk("misc_register OK!\n");
return 0;
}
static void __exit chrdev_test_exit(void)
{
int ret;
//注销核心结构
ret = misc_deregister(&misc_dev);
if(ret < 0)
{
printk("misc_deregister erron!\n");
return ;
}
printk("misc_deregister OK!\n");
}
module_init(chrdev_test_init);
module_exit(chrdev_test_exit);
MODULE_LICENSE("GPL");
应用代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define DEV_NAME "/dev/mymisc"
int main(void)
{
int ret;
char buf[] = {1, 0, 0, 0};//1,亮,0表示灭
int fd;
//打开文件设备O_RDWR, O_RDONLY, O_WRONLY,
fd = open(DEV_NAME, O_RDWR);
if(fd < 0)
{
printf("open :%s failt!\r\n", DEV_NAME);
return -1;
}
read(fd, buf, 4); //从内核空间中读取数据
write(fd, buf, 4);//写数据到内核空间
close(fd);//关闭设备 ,可以不调用,程序关闭时系统自动调用
return 0;
}
编译代码:
# Makefile 2.6
#交叉编译器前缀
cc := arm-linux-
#应用程序名
AN := app
#应用程序源文件列表
ANSRC := app.c
#模块列表
obj-m := mise_device_moudle.o
#内核源码路径
#KDIR :=/lib/modules/$(shell uname -r)/build
KDIR := /home/jian/aaa/linux/linux-3.5/
#模块最终存放
rootfs_dir := /home/jian/aaa/rootfs/home
all:
@make -C $(KDIR) M=$(PWD) modules
@${cc}gcc ${ANSRC} -o ${AN}
cp ${AN} *.ko ${rootfs_dir}
@rm -rf .tmp_versions *.o *.mod.o *.mod.c *.bak *.symvers *.markers *.unsigned *.order *~ .*.*.cmd .*.*.*.cmd
clean:
@rm -f ${AN} *.ko *.o *.mod.o *.mod.c *.bak *.symvers *.markers *.unsigned *.order *~