c语言,结构体里面的函数

c语⾔,结构体⾥⾯的函数
以linux-3.2内核代码为例,结构体⾥⾯的函数的⽤法:金属槽筒
例,在某驱动⽂件中,定义了⼀个平台设备驱动:
static struct platform_driver s3c24xx_led_driver = {
.probe        = s3c24xx_led_probe,
.remove        = s3c24xx_led_remove,
.driver        = {
.name        = "s3c24xx_led",
.owner        = THIS_MODULE,
},
};
对struct platform_driver⽽⾔,probe平台设备注册时需要的函数,remove是平台设备移除时时需要的函数。
对于不同的硬件,其注册和移除的时候,有各⾃不同的部分操作,⽐如硬件上拉下拉、特殊寄存器cfg配置等。
这些不同的操作就体现在例⼦中的s3c24xx_led_probe, s3c24xx_led_remove中。
static int s3c24xx_led_remove(struct platform_device *dev)
{
struct s3c24xx_gpio_led *led = pdev_to_gpio(dev);
led_classdev_unregister(&led->cdev);
kfree(led);
return 0;
}
static int s3c24xx_led_probe(struct platform_device *dev)
{
struct s3c24xx_led_platdata *pdata = dev->dev.platform_data;
struct s3c24xx_gpio_led *led;水处理控制器
int ret;
led = kzalloc(sizeof(struct s3c24xx_gpio_led), GFP_KERNEL);
if (led == NULL) {
泄洪道dev_err(&dev->dev, "No memory for device\n");
return -ENOMEM;
}
platform_set_drvdata(dev, led);
led->cdev.brightness_set = s3c24xx_led_set;
led->cdev.default_trigger = pdata->def_trigger;
led->cdev.name = pdata->name;
led->cdev.flags |= LED_CORE_SUSPENDRESUME;
led->pdata = pdata;
/* no point in having a pull-up if we are always driving */
if (pdata->flags & S3C24XX_LEDF_TRISTATE) {
s3c2410_gpio_setpin(pdata->gpio, 0);
s3c2410_gpio_cfgpin(pdata->gpio, S3C2410_GPIO_INPUT);
} else {
s3c2410_gpio_pullup(pdata->gpio, 0);
s3c2410_gpio_setpin(pdata->gpio, 0);
s3c2410_gpio_cfgpin(pdata->gpio, S3C2410_GPIO_OUTPUT);
}
虚拟房间
/* register our new led device */
ret = led_classdev_register(&dev->dev, &led->cdev);
if (ret < 0) {
dev_err(&dev->dev, "led_classdev_register failed\n");
kfree(led);九阴真经斩虎刀鞘
return ret;
}
return 0;
}
利⽤struct,我们实现了⼀种⾯向对象的思想,实例化的结构体对象中,有描述这个对象的⾏为⽅式(函数),有描述对象特征值或者对象组成的变量(变量,结构体变量等)。下⾯是设备结构体定义供参考:
struct platform_driver {
int (*probe)(struct platform_device *);
int (*remove)(struct platform_device *);
void (*shutdown)(struct platform_device *);
int (*suspend)(struct platform_device *, pm_message_t state);
int (*resume)(struct platform_device *);
struct device_driver driver;
const struct platform_device_id *id_table;
};
/**
* struct device_driver - The basic device driver structure
* @name:    Name of the device driver.
* @bus:    The bus which the device of this driver belongs to.
* @owner:    The module owner.
* @mod_name:    Used for built-in modules.
* @suppress_bind_attrs: Disables bind/unbind via sysfs.
* @of_match_table: The open firmware table.
* @probe:    Called to query the existence of a specific device,
*        whether this driver can work with it, and bind the driver
*        to a specific device.
* @remove:    Called when the device is removed from the system to
*        unbind a device from this driver.
* @shutdown:    Called at shut-down time to quiesce the device.
* @suspend:    Called to put the device to sleep mode. Usually to a
*        low power state.
* @resume:    Called to bring a device from sleep mode.
* @groups:    Default attributes that get created by the driver core
*        automatically.
* @pm:        Power management operations of the device which matched
*        this driver.
* @p:        Driver core's private data, no one other than the driverled发光模块
*        core can touch this.
*
* The device driver-model tracks all of the drivers known to the system.
* The main reason for this tracking is to enable the driver core to match
* up drivers with new devices. Once drivers are known objects within the
* system, however, a number of other things become possible. Device drivers  * can export information and configuration variables that are independent
* of any specific device.
*/
struct device_driver {
const char        *name;
struct bus_type        *bus;
struct module        *owner;
const char        *mod_name;    /* used for built-in modules */
bool suppress_bind_attrs;    /* disables bind/unbind via sysfs */
const struct of_device_id    *of_match_table;
int (*probe) (struct device *dev);
int (*remove) (struct device *dev);
void (*shutdown) (struct device *dev);
int (*suspend) (struct device *dev, pm_message_t state);
int (*resume) (struct device *dev);
const struct attribute_group **groups;
const struct dev_pm_ops *pm;
struct driver_private *p;
};
...

本文发布于:2024-09-21 20:36:25,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/4/276553.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:对象   设备   平台   变量   结构   需要   硬件
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议