In some Linux development boards, you can often see the echo method. Directly control the hardware or modify the driver, for example:
1 2 3 4 |
|
How to do this?
Actually, this is because the sysfs
interface is provided in the driver for users to use, so that users can use the cat
or echo
command to View and modify the values of certain variables in the driver.
The following describes how to create a sysfs interface in the driver.
Basic steps:
1. UseDEVICE_ATTR
Declare a sys
node
1 |
|
led_status
: The node name displayed in the sys interface
0600
:表示操作这个led_status节点的权限
led_status_show
:使用cat
命令查看sys接口时调用的函数
led_status_store
:使用echo
命令往sys接口写入内容时调用的函数
2、完成sys节点的读写函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
示例中,led_status_show()
函数和led_status_store()
函数的作用分为打印led变量的值和修改led变量的值.
3、定义struct attribute
和struct attribute_group
数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
上述使用了DEVICE_ATTR
声明节点名字为led_status
,
则struct attribute
名字应为:dev_attr_ + (节点名) + .attr
。所以名字为dev_attr_led_status.attr
。
4、在probe函数中调用sysfs_create_group()
函数注册sysfs
接口
设备树:
1 2 3 |
|
驱动:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
|
驱动加载后,就可以在linux终端中,使用cat
和echo
命令来查看和修改驱动中led
变量的值。例如:
1 2 3 4 5 6 7 8 9 |
|
The above is the detailed content of Linux driver | Create sysfs interface in driver. For more information, please follow other related articles on the PHP Chinese website!