驅動程式 - Linux Device Driver(LDD) - 如何使用sysfs



參考資訊:
https://www.kernel.org/doc/html/latest/filesystems/sysfs.html

範例:

static ssize_t test_show(struct device *dev, struct device_attribute *attr, char *buf)
{
    return sprintf(buf, "%d\n", 0x1234);
}

static ssize_t test_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
    int rc = -1;
    unsigned long v = 0;

    rc = kstrtoul(buf, 0, &v);
    if (rc) {
        return rc;
    }

    printk("set value: %ld\n", v);
    return count;
}
static DEVICE_ATTR_RW(test);

static int xxx_probe(struct platform_device *pdev)
{
    // ...
    device_create_file(&pdev->dev, &dev_attr_test);
}

static int xxx_remove(struct platform_device *pdev)
{
    // ...
    device_remove_file(&pdev->dev, &dev_attr_test);
}

用法:

$ cat /sys/devices/xxx/test
    0x1234

$ echo 0x1234 > /sys/devices/xxx/test