当前位置: 首页 > news >正文

android平台驱动开发(四)--系统属性节点控制GPIO

驱动开发

如何创建系统属性节点
直接在sys/目录下创建


文章目录

  • 驱动开发
  • 前言
  • 一、代码添加
  • 二、编译
  • 三、 验证
  • 总结


前言

最简单的系统属性节点控制GPIO


一、代码添加

在AU_LINUX_ANDROID_LA.VENDOR.1.0\kernel_platform\msm-kernel\drivers\misc\目录下新建test.c

#include <linux/module.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/ctype.h>
#include <linux/platform_device.h>
#include <linux/reboot.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <asm/io.h>
#include <linux/ioport.h>static ssize_t eth_en_store(struct kobject *dev,struct kobj_attribute  *attr,const char *buf, size_t len)
{unsigned long new;if (kstrtoul(buf, 0, &new)) {pr_err("%s kstrtoul err\n", __func__);return -EINVAL;}if (!!new == 1) {pr_err("%s set 1, g_rtase_resume\n", __func__);} else {pr_err("%s set 0, g_rtase_suspend\n", __func__);}return len;
}static ssize_t eth_en_show(struct kobject *dev,struct kobj_attribute  *attr, char *buf)
{return sprintf(buf, "%s\n", "sucess");
}static struct kobj_attribute eth_en_attribute_attr =__ATTR(eth_en, S_IWUSR|S_IRUGO, eth_en_show, eth_en_store);static struct attribute *sysfs_eth_en_attributes[] = {&eth_en_attribute_attr.attr,NULL
};static const struct attribute_group sysfs_eth_en_attr_group = {.attrs = sysfs_eth_en_attributes,
};static const struct of_device_id of_test_demo_match[] = {{ .compatible = "test-demo", },{},
};static int test_mode_probe(struct platform_device *pdev)
{printk(KERN_INFO "%s start...\n",__func__);return 0;
}static int test_mode_remove(struct platform_device *pdev)
{return 0;
}static struct platform_driver test_mode_driver = {.probe      = test_mode_probe,.remove     = test_mode_remove,.driver     = {.name   = "test",.owner  = THIS_MODULE,.of_match_table = of_match_ptr(of_test_demo_match),},
};static int test_driver_init(void)
{if ((k_obj = kobject_create_and_add("test_demo", NULL)) == NULL ) {pr_err("sysfs_demo sys node create error \n");}if(sysfs_create_group(k_obj, &sysfs_eth_en_attr_group) ) {pr_err("sysfs_create_group failed\n");}return platform_driver_register(&test_demo_driver);
}static void test_driver_exit(void)
{printk(KERN_INFO "%s,%d: Enter\n",__func__,__LINE__);if (k_obj) {sysfs_remove_group(k_obj, &sysfs_eth_en_attr_group);kobject_put(k_obj);}platform_driver_unregister(&test_demo_driver);return;
}module_init(test_driver_init);
module_exit(test_driver_exit);MODULE_AUTHOR("zh@testsmart.com");
MODULE_DESCRIPTION("testSmart Hardware Verion driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:test-mode-driver");

修改misc目录下的Makefile
AU_LINUX_ANDROID_LA.VENDOR.1.0\kernel_platform\msm-kernel\drivers\misc\Makefile

+obj-m	+= test_reboot.o

注:obj-m最好是以宏控的方式控制,当然也可以以obj-y的方式编译进内核,后续详细介绍

二、编译

以骁龙8 gen1 plus平台为例
在AU_LINUX_ANDROID_LA.VENDOR.1.0目录下执行:

xxxx@u99:~/AU_LINUX_ANDROID_LA.VENDOR.1.0$bash kernel_platform/qcom/proprietary/prebuilt_HY11/vendorsetup.shxxxx@u99:~/AU_LINUX_ANDROID_LA.VENDOR.1.0$cd kernel_platform/xxxx@u99:~/AU_LINUX_ANDROID_LA.VENDOR.1.0/kernel_platform$BUILD_CONFIG=./common/build.config.msm.waipio ./build/all-variants.sh "./build/build.sh"

如上编译完成后AU_LINUX_ANDROID_LA.VENDOR.1.0\kernel_platform\out\msm-waipio-waipio-consolidate\dist\目录下会生成hello_world.ko;push到设备中验证即可。

三、 验证

adb push AU_LINUX_ANDROID_LA.VENDOR.1.0\kernel_platform\out\msm-waipio-waipio-consolidate\dist\test_reboot.ko /vendor_dlkm/lib/modules/

adb push AU_LINUX_ANDROID_LA.VENDOR.1.0\kernel_platform\out\msm-waipio-waipio-consolidate\dist\test_reboot.ko /vendor/lib/modules/

taro:/ # insmod vendor/lib/modules/test_reboot.ko
taro:/ # taro:/ # find sys/ |grep force_reboot
sys/class/test_class/force_reboottaro:/ # cat sys/class/test_class/force_reboot
0
taro:/ # echo 1 > sys/class/test_class/force_reboot//echo 1 即可重启

总结

关于kobject相关的函数后续继续更新,这个是linux的核心函数

http://www.xdnf.cn/news/673885.html

相关文章:

  • 字节跳动BAGEL-7B-MoT模型开源:多模态AI技术的新范式与行业涟漪
  • Python爬虫实战:研究Playwright框架相关技术
  • SD07_NVM的安装及相关操作
  • 解码STREAM:GEO技术方法论全解析
  • 多空短线决策副图指标,通达信炒股软件指标操盘图文教程
  • 安卓学习笔记-声明式UI
  • 2025年5月蓝桥杯stema省赛真题——象棋移动
  • ViewModel
  • MySQL问题:主要索引类型(聚簇、辅助、覆盖、前缀)
  • WPF中的图标闪烁功能
  • ubuntu 制作 ssl 证书
  • WEB安全威胁与SSL
  • 互联网大厂Java面试:从Spring到微服务的挑战
  • Linux下使用socat将TCP服务转为虚拟串口设备
  • Docker 在 CI/CD 中的应用:自动化构建、测试与部署流程设计
  • 超声体模的保修制度和维护保养
  • uniapp vue3 鸿蒙支持的 HTML5+接口
  • 服务器数据迁移
  • Windows逆向工程提升之IMAGE_TLS_DIRECTORY
  • 嵌入式学习 D29:系统编程--线程
  • AbMole| MGCD0103(M1790,Mocetinostat)
  • 深入解析Google多线程环境下的空间配置器——TCMalloc
  • 哈希算法及其在文件唯一性判定中的应用
  • [Vue]浅浅了解vue3响应式的基本原理
  • 【c++】exe找不到dll里的符号:error LNK2019: unresolved external symbol
  • (LeetCode 每日一题)2894. 分类求和并作差(数组、数学)
  • 优秘AI短视频数字人6月功能更新预告:新增多个AIGC热门功能,智能体和知识库再升级
  • 11.13 LangGraph记忆机制解析:构建生产级AI Agent的关键技术
  • MyBatis-Plus一站式增强组件MyBatis-Plus-kit(更新2.0版本):零Controller也能生成API?
  • 数据链路层