Linux驱动 | 在驱动中创建sysfs接口

简介: Linux驱动 | 在驱动中创建sysfs接口

前言

在一些linux开发板中,经常可以看到通过echo的方式来直接控制硬件或者修改驱动,例如:

//灯灭
echo 0 >/sys/class/leds/firefly:blue:power/brightness 
//灯亮
echo 1 >/sys/class/leds/firefly:blue:power/brightness

这是怎么做到呢?

实际上,这是因为在驱动中提供了sysfs接口给用户使用,使得用户可以通过cat或者echo命令来查看和修改驱动中某些变量的值。

下面介绍驱动中创建sysfs接口的方法。

sysfs接口创建

基本步骤:

1、使用DEVICE_ATTR声明一个sys节点

static DEVICE_ATTR(led_status, 0600, led_status_show, led_status_store);

led_status:在sys接口中显示的节点名字

0600:表示操作这个led_status节点的权限

led_status_show:使用cat命令查看sys接口时调用的函数

led_status_store:使用echo命令往sys接口写入内容时调用的函数

2、完成sys节点的读写函数

static unsigned int led = 0;
/*
*  sys节点的读函数
*  执行 cat /sys/devices/platform/leds/led_status时会调用
*/
static ssize_t led_status_show(struct device *dev, struct device_attribute *attr, char *buf)
{
  //buf是通过cat命令显示到终端的内容,这里显示led变量
 return sprintf(buf, "%s:%d.\n", "led", led);
}
/**
*  sys节点的写函数
*  用echo命令往sys节点写入内容时,会调用该函数
*/
static ssize_t led_status_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
  //写入的内容会存放到buf中,这里将buf内容赋值给led变量
 sscanf(buf, "%d", &led);
 return count;
}

示例中,led_status_show()函数和led_status_store()函数的作用分为打印led变量的值修改led变量的值.

3、定义struct attributestruct attribute_group数组

static struct attribute *led_attributes[]={
  /*上述使用了DEVICE_ATTR声明节点名字为led_status,
  * 则struct attribute名字应为:
  *  dev_attr_ + (节点名) + .attr
  * 所以名字为dev_attr_led_status.attr
  */
  &dev_attr_led_status.attr,
 NULL,
};
static const struct attribute_group led_attrs={
 .attrs = led_attributes,//引用上述struct attribute数组
};

上述使用了DEVICE_ATTR声明节点名字为led_status, 则struct attribute名字应为:dev_attr_ + (节点名) + .attr。所以名字为dev_attr_led_status.attr

4、在probe函数中调用sysfs_create_group()函数注册sysfs接口

完整例子

设备树:

leds:leds{
  compatible = "xx,xx-led";
 };

驱动:

static unsigned int led = 0;
static ssize_t led_status_show(struct device *dev, struct device_attribute *attr, char *buf)
{
 return sprintf(buf, "%s:%d.\n", "led", led);
}
static ssize_t led_status_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
 sscanf(buf, "%d", &led);
 return count;
}
static DEVICE_ATTR(led_status, 0600, led_status_show, led_status_store);
static struct attribute *led_attributes[]={
 &dev_attr_led_status.attr,
 NULL,
};
static const struct attribute_group led_attrs={
 .attrs = led_attributes,
};
static int xx_led_probe(struct platform_device *pdev)
{
 sysfs_create_group(&pdev->dev.kobj, &led_attrs);
 return 0;
}
static int xx_led_remove(struct platform_device *pdev)
{
 sysfs_remove_group(&pdev->dev.kobj, &led_attrs);
 return 0;
}
static const struct of_device_id xx_led_of_match[] = {
 {.compatible = "xx,xx-led"},
};
static struct platform_driver xx_led_driver = {
 .probe = xx_led_probe,
 .remove = xx_led_remove,
 .driver = {
  .name = "xx-led",
  .owner = THIS_MODULE,
  .of_match_table = xx_led_of_match,
 },
};
static int __init xx_led_init(void)
{
 return platform_driver_register(&xx_led_driver );
}
static void __exit xx_led_exit(void)
{
 platform_driver_unregister(&xx_led_driver);
}
module_init(xx_led_init);
module_exit(xx_led_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("xx led driver");
MODULE_AUTHOR("Vincent");
MODULE_VERSION("V1.0.00");

驱动加载后,就可以在linux终端中,使用catecho命令来查看和修改驱动中led变量的值。例如:

//查看led变量的值
cat /sys/devices/platform/leds/led_status
led:0.
//修改led变量的值为9
echo 9 > /sys/devices/platform/leds/led_status
//查看
cat /sys/devices/platform/leds/led_status
led:9.

end

猜你喜欢

Linux reset子系统及驱动实例

Linux clock子系统及驱动实例

Linux内核中常用的C语言技巧

Linux内核死锁检测工具——Lockdep

Linux内核基础篇——常用调试技巧汇总

Linux内核基础篇——动态输出调试

Linux内核基础篇——printk调试

Linux内核基础篇——initcall

实战 | RISC-V Linux入口地址2M预留内存优化

RISC-V Linux启动之页表创建分析

RISC-V Linux汇编启动过程分析

教你在QEMU上运行RISC-V Linux

写给新手的MMU工作原理

相关文章
|
2月前
|
Linux API 数据安全/隐私保护
【Linux 用户管理】Linux用户身份信息获取与管理API 接口
【Linux 用户管理】Linux用户身份信息获取与管理API 接口
29 0
|
2月前
|
Linux API 调度
Linux系统驱动跟裸机驱动的区别
Linux系统驱动跟裸机驱动的区别
33 0
|
2月前
|
域名解析 网络协议 Linux
【Shell 命令集合 网络通讯 】Linux 设置和管理网络接口配置信息 netconfig命令 使用指南
【Shell 命令集合 网络通讯 】Linux 设置和管理网络接口配置信息 netconfig命令 使用指南
64 1
|
2月前
|
存储 缓存 Linux
【Shell 命令集合 磁盘维护 】Linux 设置和查看硬盘驱动器参数 hdparm命令使用教程
【Shell 命令集合 磁盘维护 】Linux 设置和查看硬盘驱动器参数 hdparm命令使用教程
39 0
|
29天前
|
网络协议 Linux
在Linux中,管理和配置网络接口
在Linux中管理网络接口涉及多个命令,如`ifconfig`(在新版本中被`ip`取代)、`ip`(用于网络设备配置)、`nmcli`(NetworkManager的CLI工具)、`nmtui`(文本界面配置)、`route/ip route`(处理路由表)、`netstat/ss`(显示网络状态)和`hostnamectl/systemctl`(主机名和服务管理)。这些命令帮助用户启动接口、设置IP地址、查看连接和路由信息。不同发行版可能有差异,建议参考相应文档。
20 4
|
2月前
|
监控 网络协议 Linux
【Shell 命令集合 网络通讯 】Linux 显示网络 连接、路由表和网络接口信息 netstat命令 使用指南
【Shell 命令集合 网络通讯 】Linux 显示网络 连接、路由表和网络接口信息 netstat命令 使用指南
70 1
|
1天前
|
存储 算法 网络协议
【探索Linux】P.26(网络编程套接字基本概念—— socket编程接口 | socket编程接口相关函数详细介绍 )
【探索Linux】P.26(网络编程套接字基本概念—— socket编程接口 | socket编程接口相关函数详细介绍 )
6 0
|
1天前
|
Linux
linux驱动层输出dev_dbg打印信息
linux驱动层输出dev_dbg打印信息
3 0
|
2天前
|
网络协议 Linux 开发工具
Linux中 /etc/sysconfig/network-scripts/ifcfg-<interface> 网络接口配置 详解 看这一篇够用
Linux中 /etc/sysconfig/network-scripts/ifcfg-<interface> 网络接口配置 详解 看这一篇够用
|
7天前
|
Ubuntu 网络协议 Linux

热门文章

最新文章