RK3568平台 sys虚拟文件系统添加节点_ch423 driver(1)

root@ubuntu:/sys/block# ls
loop0  loop3  loop6  ram1   ram12  ram15  ram4  ram7  sda
loop1  loop4  loop7  ram10  ram13  ram2   ram5  ram8  sr0
loop2  loop5  ram0   ram11  ram14  ram3   ram6  ram9  sr1
root@ubuntu:/sys/block# cd sr0
root@ubuntu:/sys/block/sr0# ls
alignment_offset  discard_alignment  holders   removable  subsystem
bdi               events             inflight  ro         trace
capability        events_async       power     size       uevent
dev               events_poll_msecs  queue     slaves
device            ext_range          range     stat
root@ubuntu:/sys/block/sr0# cat size 
2097151
root@ubuntu:/sys/block/sr0# cd ..
root@ubuntu:/sys/block# ls -l sda
lrwxrwxrwx 1 root root 0 Jul  4 20:50 sda -> ../devices/pci0000:00/0000:00:10.0/host32/target32:0:0/32:0:0:0/block/sda
root@ubuntu:/sys/block# ls -l sr0
lrwxrwxrwx 1 root root 0 Aug  2 00:03 sr0 -> ../devices/pci0000:00/0000:00:11.0/0000:02:05.0/ata3/host2/target2:0:0/2:0:0:0/block/sr0
root@ubuntu:/sys/block# ls -l sr1
lrwxrwxrwx 1 root root 0 Aug  2 00:03 sr1 -> ../devices/pci0000:00/0000:00:11.0/0000:02:05.0/ata4/host3/target3:0:0/3:0:0:0/block/sr1
root@ubuntu:/sys/block# ls -l ram1
lrwxrwxrwx 1 root root 0 Aug  2 00:03 ram1 -> ../devices/virtual/block/ram1
root@ubuntu:/sys/block# ls -l loop1
lrwxrwxrwx 1 root root 0 Aug  2 00:03 loop1 -> ../devices/virtual/block/loop1
root@ubuntu:/sys/block#

3、bus目录

在内核注册的每条总线,在该目录下对应一个子目录,比如i2c、spi、pci、scsi、usb等等。

root@ubuntu:/sys/bus# ls
ac97         cpu           hid           mdio_bus     platform  sdio   virtio
acpi         eisa          i2c           mmc          pnp       serio  workqueue
clockevents  event_source  isa           pci          rapidio   spi    xen
clocksource  gameport      machinecheck  pci_express  scsi      usb    xen-backend

4、devices目录

包含系统的所有设备。

root@ubuntu:/sys/devices# ls
breakpoint  isa          pci0000:00  pnp0  rapidio   system      virtual
cpu         LNXSYSTM:00  platform    pnp1  software  tracepoint

5、fs目录

描述系统中的文件系统。

6、filewire目录

描述系统中的固件

7、power目录

描述系统中的电源选项。

8、module目录

描述系统中的模块信息。

9、kernel目录

内核中的配置参数。

三.sys文件系统搭建

sys文件系统的搭建流程,左边是初始化流程,右边是sys的文件目录:

四.添加一个sys节点

以ch423为例子,在sys/misc下面添加一个ch423的节点。

#include <linux/miscdevice.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/notifier.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/compat.h>
#include <linux/printk.h>
#include <linux/kobject.h>
#include <linux/version.h>
#include <linux/kthread.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>

struct ch423_dev {
    struct device *dev;
    struct device sys_dev;
    struct gpio_desc *ch423_clk;
    struct gpio_desc *ch423_dat;
};

struct ch423_dev *g_ch423;
struct ch423_dev *ch423;

static ssize_t ch423_gpio_oc_l_read(struct device *dev,
                      struct device_attribute *attr, char *buf)
{

    return sprintf(buf, "%lx\n",CH423_OC_L_STATUS);
}

static ssize_t ch423_gpio_oc_l_write(struct device *dev,
                       struct device_attribute *attr,
                       const char *buf, size_t count)
{
    return count;
}

static DEVICE_ATTR(ch423_gpio_oc_l, 0644,
        ch423_gpio_oc_l_read, ch423_gpio_oc_l_write);


static const struct file_operations ch423_fops = {
    .owner = THIS_MODULE,
    .read = ch423_read,
    .write = ch423_write,
    .unlocked_ioctl = ch423_ioctl,
};

struct miscdevice ch423_miscdev = {
    .minor = MISC_DYNAMIC_MINOR,
    .name = "ch423_dev",
    .fops = &ch423_fops,
};

static int ch423_probe(struct platform_device *pdev)
{
    struct ch423_dev *ch423;
    int ret = 0;

    ch423 = devm_kzalloc(&pdev->dev, sizeof(*ch423), GFP_KERNEL);
    if (!ch423)
        return -ENOMEM;

    ch423->dev = &pdev->dev;

    ch423->ch423_clk = devm_gpiod_get_optional(ch423->dev,
                             "ch423-clk", GPIOD_OUT_HIGH);
    if (IS_ERR(ch423->ch423_clk)) {
        dev_warn(ch423->dev, "Could not get ch423-clk!\n");
        ch423->ch423_clk = NULL;
    }

    ch423->ch423_dat = devm_gpiod_get_optional(ch423->dev,
                             "ch423-dat", GPIOD_OUT_HIGH);
    if (IS_ERR(ch423->ch423_dat)) {
        dev_warn(ch423->dev, "Could not get ch423-clk!\n");
        ch423->ch423_dat = NULL;
    }
    g_ch423 = ch423;

    ret = misc_register(&ch423_miscdev);
    if (ret) {
        ERR("ch423_miscdev ERROR: could not register ch423_miscdev device\n");
        return ret;
    }

    ret = device_create_file(ch423_miscdev.this_device,
                &dev_attr_ch423_gpio_oc_l);
    if (ret) {
        dev_err(ch423->dev, "failed to create attr ch423_gpio_oc_l!\n");
        return ret;
    }
    return 0;
## 最后

**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**

**深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。**

**因此收集整理了一份《2024年嵌入式&物联网开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**

![img](https://img-blog.csdnimg.cn/img_convert/1b24074fb908ab4df6302de313d6b9e4.png)

![img](https://img-blog.csdnimg.cn/img_convert/875112b224b477630b429cba3ad99a6d.jpeg)

![img](https://img-blog.csdnimg.cn/img_convert/093a3677c2bda7091025d1f6cc1ef2e7.png)

 ![img](https://img-blog.csdnimg.cn/img_convert/3b1cde53e70fe2ef7600144bf08b4103.png)

![img](https://img-blog.csdnimg.cn/img_convert/14c92310a29e8fd043c8b603d70a1922.png)

![img](https://img-blog.csdnimg.cn/img_convert/20e05bae65375ddc054ab705cc751d04.png)

![](https://img-blog.csdnimg.cn/img_convert/fe3d3f11ad03f973646aecd584552177.png)

 

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上嵌入式&物联网开发知识点,真正体系化!**

[**如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!**](https://bbs.csdn.net/topics/618654289)

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**!!


**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上嵌入式&物联网开发知识点,真正体系化!**

[**如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!**](https://bbs.csdn.net/topics/618654289)

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**!!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值