简述:linux 驱动中到处可见DEVICE_ATTR和device_create_file,相关的概念我这里就不说了,网上很多。下面就是我自己学习和测试的实例。

在Android上测试,测试driver代码:

#include <linux/init.h>
#include <linux/module.h>

/*驱动注册的头文件,包含驱动的结构体和注册和卸载的函数*/
#include <linux/platform_device.h>

#define DRIVER_NAME "love_tanghanyue"

MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("TOPEET");

static ssize_t lovelinux_show(struct device *dev, struct device_attribute *attr, char *buf)
{
    printk("lovelinux_show  i love tanghanyue!\n");
}

static ssize_t lovelinux_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
    printk("lovelinux_store\n");
}
static DEVICE_ATTR(vib, 0664, lovelinux_show, lovelinux_store);



static int hello_probe(struct platform_device *pdv){
	int err;
	printk(KERN_EMERG "\tinitialized\n");
	
        err = device_create_file(&pdv->dev, &dev_attr_vib);
        if (err)
             printk(KERN_EMERG "\tdevice_create_file_error\n");
        else
             printk(KERN_EMERG "\t very good device_create_file_success\n");
	return 0;
}

static int hello_remove(struct platform_device *pdv){
	
	return 0;
}

static void hello_shutdown(struct platform_device *pdv){
	
	;
}

static int hello_suspend(struct platform_device *pdv){
	
	return 0;
}

static int hello_resume(struct platform_device *pdv){
	
	return 0;
}

struct platform_driver hello_driver = {
	.probe = hello_probe,
	.remove = hello_remove,
	.shutdown = hello_shutdown,
	.suspend = hello_suspend,
	.resume = hello_resume,
	.driver = {
		.name = DRIVER_NAME,
		.owner = THIS_MODULE,
	}
};


static int hello_init(void)
{
	int DriverState;
	
	printk(KERN_EMERG "HELLO WORLD enter! hanyue\n");
	DriverState = platform_driver_register(&hello_driver);
	
	printk(KERN_EMERG "\tDriverState is %d\n",DriverState);
	return 0;
}

static void hello_exit(void)
{
	printk(KERN_EMERG "HELLO WORLD exit!\n");
	
	platform_driver_unregister(&hello_driver);	
}

module_init(hello_init);
module_exit(hello_exit);


运行结果:

参考文章

创建一个简单的device_create_file文件节点_蓝白天际线的博客-CSDN博客

device_create_file的用法详解_hehui0921的博客-CSDN博客

Logo

更多推荐