#include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/platform_device.h> #include <linux/slab.h> #include <linux/rockchip/common.h> #include <linux/kdev_t.h> #include <linux/device.h> /** struct hello_test_data { struct device *classdev; int blue=456; } static struct hello_test_data *hello_data; **/ static struct class *hello_test_class; //定义设备类 static struct device *classdev; static char mybuf[100]="123"; //执行cat会调用到此函数 show对应的是read static ssize_t hello_test_show(struct device *dev, struct device_attribute *attr, char *buf) { return sprintf(buf, "%s ", mybuf); } //执行echo 1 > hello_test会执行到此函数 store 对应的是write static ssize_t hello_test_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { sprintf(mybuf, "%s", buf); return count; } // _name _mode(权限) (显示函数) (写入) static DEVICE_ATTR(hello_test, 0777, hello_test_show, hello_test_store); //设备属性文件 名 // probe初始化函数 //cdev 分配设备对象空间 static int hello_test_probe(struct platform_device *pdev) { printk("Gatsby probe hello_test "); // 创建对应的节点file //device_create_file(&pdev->dev, &dev_attr_hello_test); classdev=device_create(hello_test_class, &pdev->dev, MKDEV(0, 0), NULL,"hello_test_device",0); sysfs_create_file(&pdev->dev.kobj, &dev_attr_hello_test.attr); return 0; } static const struct of_device_id hello_of_match[] = { { .compatible = "rockchip,hello_test"}, { }, }; // platform_driver 的结构 static struct platform_driver hello_test_driver = { .probe = hello_test_probe, .driver = { .name = "hello_test_class", .owner = THIS_MODULE, .of_match_table = hello_of_match, //需要再dts中做匹配 } }; static int __init hello_init(void) { printk("Gatsby hello_init "); /** struct device *classdev; classdev = device_create(hello_test, 0, MKDEV(0,0),NULL,"mytest_device"); //创建mytest_device设备 **/ hello_test_class = class_create(THIS_MODULE, "hello_test_class");// 在/sys/class目录下生成 hello_test目录 platform_driver_register(&hello_test_driver); return 0; } static void __exit hello_exit(void) { printk("Gatsby hello_exit "); platform_driver_unregister(&hello_test_driver); } module_init(hello_init); module_exit(hello_exit); MODULE_AUTHOR("hello_test"); MODULE_DESCRIPTION("Gatsby"); MODULE_LICENSE("GPL");
makefile
obj-$(CONFIG_HELLO_TEST) += hello_test.o
kconfig
config HELLO_TEST tristate "Hello world for Gatsby" help Hello for Gatsby
上一级目录 makefile
obj-y += gatsby/
kconfig
source "drivers/gatsby/Kconfig"