


Preface
In some Linux development boards, you can often see the echo method. Directly control the hardware or modify the driver, for example:
//灯灭 echo 0 >/sys/class/leds/firefly:blue:power/brightness //灯亮 echo 1 >/sys/class/leds/firefly:blue:power/brightness
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.
sysfs interface creation
Basic steps:
1. UseDEVICE_ATTR
Declare a sys
node
static DEVICE_ATTR(led_status, 0600, led_status_show, led_status_store);
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节点的读写函数
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 attribute
和struct 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终端中,使用cat
和echo
命令来查看和修改驱动中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.
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!

Maintenance mode is used for system maintenance and repair, allowing administrators to work in a simplified environment. 1. System Repair: Repair corrupt file system and boot loader. 2. Password reset: reset the root user password. 3. Package management: Install, update or delete software packages. By modifying the GRUB configuration or entering maintenance mode with specific keys, you can safely exit after performing maintenance tasks.

Linux network configuration can be completed through the following steps: 1. Configure the network interface, use the ip command to temporarily set or edit the configuration file persistence settings. 2. Set up a static IP, suitable for devices that require a fixed IP. 3. Manage the firewall and use the iptables or firewalld tools to control network traffic.

Maintenance mode plays a key role in Linux system management, helping to repair, upgrade and configuration changes. 1. Enter maintenance mode. You can select it through the GRUB menu or use the command "sudosystemctlisolaterscue.target". 2. In maintenance mode, you can perform file system repair and system update operations. 3. Advanced usage includes tasks such as resetting the root password. 4. Common errors such as not being able to enter maintenance mode or mount the file system, can be fixed by checking the GRUB configuration and using the fsck command.

The timing and reasons for using Linux maintenance mode: 1) When the system starts up, 2) When performing major system updates or upgrades, 3) When performing file system maintenance. Maintenance mode provides a safe and controlled environment, ensuring operational safety and efficiency, reducing impact on users, and enhancing system security.

Indispensable commands in Linux include: 1.ls: list directory contents; 2.cd: change working directory; 3.mkdir: create a new directory; 4.rm: delete file or directory; 5.cp: copy file or directory; 6.mv: move or rename file or directory. These commands help users manage files and systems efficiently by interacting with the kernel.

In Linux, file and directory management uses ls, cd, mkdir, rm, cp, mv commands, and permission management uses chmod, chown, and chgrp commands. 1. File and directory management commands such as ls-l list detailed information, mkdir-p recursively create directories. 2. Permission management commands such as chmod755file set file permissions, chownuserfile changes file owner, and chgrpgroupfile changes file group. These commands are based on file system structure and user and group systems, and operate and control through system calls and metadata.

MaintenanceModeinLinuxisaspecialbootenvironmentforcriticalsystemmaintenancetasks.Itallowsadministratorstoperformtaskslikeresettingpasswords,repairingfilesystems,andrecoveringfrombootfailuresinaminimalenvironment.ToenterMaintenanceMode,interrupttheboo

The core components of Linux include kernel, file system, shell, user and kernel space, device drivers, and performance optimization and best practices. 1) The kernel is the core of the system, managing hardware, memory and processes. 2) The file system organizes data and supports multiple types such as ext4, Btrfs and XFS. 3) Shell is the command center for users to interact with the system and supports scripting. 4) Separate user space from kernel space to ensure system stability. 5) The device driver connects the hardware to the operating system. 6) Performance optimization includes tuning system configuration and following best practices.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
