如何维护从用户空间到内核空间的回调 [英] How Callback is maintained from Userspace to Kernel Space

查看:94
本文介绍了如何维护从用户空间到内核空间的回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习有关驱动程序的信息,并研究了将某些值写入/sys/devices/virtual/wdc_per的看门狗驱动程序代码,现在我猜想这是驱动程序如何从用户空间获取值并在用户空间中公开文件的逻辑

I am learning about the driver and looking into the watchdog driver code where some value is being written to /sys/devices/virtual/wdc_per now I guess this is the logic how driver gets its value from userspace and exposed file in user space is

 "sys/devices/virtual/wdc_per"

但是现在从wdc_per实际获取此值到驱动程序的方式,必须保留一些回调

But now how actually this value from wdc_per is reached to driver, there must some callback maintained

就我而言,其基于GPIO的看门狗驱动程序和gpio_wdt.c可能具有此回调.

In My case its GPIO based Watchdog driver and gpio_wdt.c may be having this callback.

但是我真的不知道它是如何发生的

But I really could not figure out how it actually happens

任何人都可以帮助我找出该用户空间到内核空间的链接.

Anybody can help me find out this userspace to kernel space link.

推荐答案

首先,该驱动程序gpio_wdt.c似乎截至目前为止还没有存在于主线内核中,因此很难对其进行评论.

First of all, this driver, gpio_wdt.c, doesn't seem to exist in the mainline kernel as of this date, so it's hard to comment it.

Sysfs(通常安装在/sys上)实际上非常易于使用. 是一个很好的例子如何创建Sysfs属性.基本上,您可以创建属性(将成为Sysfs文件名),并使用两个定义的操作(回调)注册它们: store show ,它们分别与resp等效. .每次读取Sysfs文件(属性)并写入 store 时,都会调用 show 回调.

Sysfs (usually mounted at /sys) is actually very easy to use. This is a great example of how to create Sysfs attributes. Basically, you create attributes (will become the Sysfs file names) and register them with two defined operations (callbacks): store and show, which are the equivalent of resp. write and read. The show callback is called everytime the Sysfs file (attribute) is read and store when it's written.

编写属于现有类的设备驱动程序(很可能是您的情况)时,您几乎不需要自己做.这是因为标准Linux设备类已经具有一套Sysfs属性,驱动程序将或多或少地间接使用这些Sysfs属性.

When writing a device driver that belongs to an existing class (most likely your situation), you will rarely need to do that yourself. This is because the standard Linux device classes already have a working set of Sysfs attributes that your driver will use more or less indirectly.

例如,在/sys/class/leds类中的leds类(LED设备),每个LED具有一堆Sysfs属性,以便用户可以从用户空间读取/修改它们(亮度,最大亮度,触发器等).现在,如果您在 /drivers/leds 中查看特定于LED的驱动器,您将赢得找不到手动Sysfs属性创建.但是,在探查驱动程序时,您会发现对led_classdev_register的调用,该调用将struct led_classdev*作为参数.此结构具有特定驱动程序需要提供的brightness_set回调成员.当用户写入/sys/class/leds/whatever-led/brightness时,将调用leds类的 store Sysfs回调,该回调又调用特定驱动程序的brightness_set回调.

For example, the leds class (LED devices), of which you will find the devices in /sys/class/leds, has a bunch of Sysfs attributes per LED so that a user may read/modify them from userspace (brightness, maximum brightness, trigger, etc.). Now, if you look at LED specific drivers in /drivers/leds, you won't find manual Sysfs attributes creations. You will find, however, a call to led_classdev_register when the driver is probed, which takes a struct led_classdev* as a parameter. This structure has a brightness_set callback member the specific driver needs to provide. When a user writes to /sys/class/leds/whatever-led/brightness, the leds class' store Sysfs callback gets called which in turn calls the specific driver's brightness_set callback.

我的观点是:在手动添加Sysfs属性之前,请确保您确实了解您的设备类.无论如何,将驱动程序提交给LKML时,您会很快就知道这是否是一个明智的决定.

My point is: make sure you really know your device class before manually adding Sysfs attributes. Anyway, when submitting your driver to the LKML, you will know fast enough if it was a good decision.

这篇关于如何维护从用户空间到内核空间的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆