环境: qemu linux4.15 vexpress_defconfig aarch32 buildroot2018
char device 字符设备
platform device 平台设备--虚拟出的一类cpu不用总线可以直接访问的设备,platform总线
class device 类设备,class 的概念没查到讲的比较清楚的
echo /sbin/mdev > /pros/sys/kernel/hotplug ,mdev负责自动创建设备节点
修改韦东山char device例子实现基于平台设备和dts的多个char设备驱动,源代码如下
motherboard {
testchar1@3,03000000 {
compatible = "test,testchar";
charidx = <1>;
};
testchar2@3,03000000 {
compatible = "test,testchar";
charidx = <2>;
};
}
"arch/arm/boot/dts/vexpress-v2m.dtsi" [readonly] 451 lines --9%--
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#define MAX_DEV_NUM (4)
/* 1. 确定主设备号 */
static int major = 0;
static unsigned int chrcnt = 0;
static char kernel_buf1[1024];
static char kernel_buf2[1024];
static struct class *hello_class;
struct hello_data{
struct cdev hello_cdev;
int charidx;
int inited;
char name[10];
char *kernel_buf;
};
struct hello_data mydata[MAX_DEV_NUM];
#define MIN(a, b) (a < b ? a : b)
/* 3. 实现对应的open/read/write等函数,填入file_operations结构体 */
static ssize_t hello_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
int err;
struct hello_data *fdata = (struct hello_data *)file->private_data;
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
err = copy_to_user(buf, fdata->kernel_buf, MIN(1024, size));
return MIN(1024, size);
}
static ssize_t hello_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
int err;
struct hello_data *fdata = (struct hello_data *)file->private_data;
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
err = copy_from_user(fdata->kernel_buf, buf, MIN(1024, size));
return MIN(1024, size);
}
static int hello_drv_open (struct inode *node, struct file *file)
{
int devidx = iminor(node);
file->private_data = (void*)&mydata[devidx];
printk("%s %s line %d open %s%d\n",
__FILE__, __FUNCTION__, __LINE__,
mydata[devidx].name, devidx);
return 0;
}
static int hello_drv_close (struct inode *node, struct file *file)
{
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
return 0;
}
/* 2. 定义自己的file_operations结构体 */
static struct file_operations hello_drv = {
.owner = THIS_MODULE,
.open = hello_drv_open,
.read = hello_drv_read,
.write = hello_drv_write,
.release = hello_drv_close,
};
static int hello_probe(struct platform_device *pdev){
int val;
struct device_node *np = pdev->dev.of_node;
dev_t devid;
struct cdev *mychr;
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
if(!of_property_read_u32(np, "charidx", &val))
{
printk("read id %d\n", val);
}
else
{
printk("get char id failed!!\n");
val =1;
}
if(val < 1)
{
printk("get char id failed!!\n");
val =1;
}
mychr = &mydata[val-1].hello_cdev;
platform_set_drvdata(pdev, (void*)&mydata[val-1]);
devid = MKDEV(major, val);
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
cdev_init(mychr, &hello_drv);
cdev_add(mychr, devid, 1);
device_create(hello_class, NULL, devid, NULL, "hello%d", val); /* /dev/hello */
mydata[val-1].charidx = val;
mydata[val-1].inited = 1;
if(!val){
memcpy(mydata[val-1].name, "maindev", 8);
mydata[val-1].kernel_buf = kernel_buf1;
}else{
memcpy(mydata[val].name, "subdev", 7);
mydata[val-1].kernel_buf = kernel_buf2;
}
chrcnt++;
return 0;
}
static int hello_remove(struct platform_device *pdev){
struct hello_data *s_mydata = platform_get_drvdata(pdev);
//dump_stack();
printk("%s %s line %d remove hello%d\n",
__FILE__, __FUNCTION__, __LINE__, s_mydata->charidx);
cdev_del(&s_mydata->hello_cdev);
device_destroy(hello_class, MKDEV(major, s_mydata->charidx));
s_mydata->inited = 0;
chrcnt--;
if(!chrcnt)
{
printk("%s %s line %d remove chrcnt%d\n",
__FILE__, __FUNCTION__, __LINE__, chrcnt);
class_unregister(hello_class);
class_destroy(hello_class);
unregister_chrdev_region(major, MAX_DEV_NUM);
}
return 0;
}
static const struct of_device_id hello_of_table[] = {
{ .compatible = "test,testchar" },
{}
};
static struct platform_driver hello_platform_driver = {
.probe = hello_probe,
.remove = hello_remove,
.driver = {
.name = "hello",
.of_match_table = hello_of_table,
},
};
static int hello_uevent(struct device *dev, struct kobj_uevent_env *env)
{
dump_stack();
add_uevent_var(env, "DEVMODE=%#o", 0666);
return 0;
}
/* 4. 把file_operations结构体告诉内核:注册驱动程序 */
/* 5. 谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数 */
static int __init hello_init(void)
{
int err;
int rc;
dev_t devid;
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
//major = register_chrdev(0, "hello", &hello_drv); /* /dev/hello */
rc = alloc_chrdev_region(&devid, 0, MAX_DEV_NUM, "hello");
major = MAJOR(devid);
hello_class = class_create(THIS_MODULE, "hello_class");
err = PTR_ERR(hello_class);
if (IS_ERR(hello_class)) {
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
return -1;
}
//hello_class->dev_uevent = hello_uevent;
return platform_driver_register(&hello_platform_driver);;
}
/* 6. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数 */
static void __exit hello_exit(void)
{
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
//dump_stack();
return platform_driver_unregister(&hello_platform_driver);;
}
/* 7. 其他完善:提供设备信息,自动创建设备节点 */
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
echo /sbin/mdev > /pros/sys/kernel/hotplug
着重了解下mdev的原理,mdev的是userspace自动创建/dev/下的设备节点
如果没有上面的echo操作,insmod之后也可以手动执行下mdev -s,/dev/hello1/2节点也会自动创建。busybox中mdev的部分源码如下,默认mdev的配置文件在/etc/mdev.conf,mdev -s会在/sys/class目录和/sys/block目录下遍历并执行fileAction和dirAction
busybox-1.23.2/util-linux/mdev.c
int mdev_main(int argc UNUSED_PARAM, char **argv)
{
#if ENABLE_FEATURE_MDEV_CONF
G.filename = "/etc/mdev.conf";
#endif
/* We can be called as hotplug helper */
/* Kernel cannot provide suitable stdio fds for us, do it ourself */
xchdir("/dev");
if (argv[1] && strcmp(argv[1], "-s") == 0) {
/*
* Scan: mdev -s
*/
struct stat st;
#if ENABLE_FEATURE_MDEV_CONF
/* Same as xrealloc_vector(NULL, 4, 0): */
G.rule_vec = xzalloc((1 << 4) * sizeof(*G.rule_vec));
#endif
xstat("/", &st);
G.root_major = major(st.st_dev);
G.root_minor = minor(st.st_dev);
putenv((char*)"ACTION=add");
if (access("/sys/class/block", F_OK) != 0) {
recursive_action("/sys/block",
ACTION_RECURSE | ACTION_FOLLOWLINKS | ACTION_QUIET,
fileAction, dirAction, temp, 0);
}
recursive_action("/sys/class",
ACTION_RECURSE | ACTION_FOLLOWLINKS,
fileAction, dirAction, temp, 0);
}
}
fileAction中会执行make_device, make_device会根据/sys/class/hello_class/hello1/dev里面找到主从设备号 249:1 。再从/sys/class/hello_class/hello1/uevent中找到设备名DEVNAME=hello1,然后调用mknode系统调用船舰设备节点
/* File callback for /sys/ traversal */
static int FAST_FUNC fileAction(const char *fileName,
struct stat *statbuf UNUSED_PARAM,
void *userData,
int depth UNUSED_PARAM)
{
size_t len = strlen(fileName) - 4; /* can't underflow */
char *scratch = userData;
/* len check is for paranoid reasons */
if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX)
return FALSE;
strcpy(scratch, fileName);
scratch[len] = '\0';
make_device(/*DEVNAME:*/ NULL, scratch, OP_add);
return TRUE;
}
dev power subsystem uevent
# cat dev
249:1
# cat uevent
MAJOR=249
MINOR=1
DEVNAME=hello1
# pwd
/sys/class/hello_class/hello1