c語言,結構體裡面的函數

來源:互聯網
上載者:User

標籤:

以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 driver *        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;};

...

 

c語言,結構體裡面的函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.