在驱动中创建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终端中,使用catecho命令来查看和修改驱动中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.
打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分