In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "what is the Linux platform bus driver device model". In the daily operation, I believe many people have doubts about what the Linux platform bus driver device model is. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what is the Linux platform bus driver device model?" Next, please follow the editor to study!
The platform bus is a virtual bus, the corresponding device is platform_device, and the driver is platform_driver. In the device driver model of Linux 2.6, I2C, RTC, LCD and so on are all summarized as platform_device.
The bus binds the device to the driver, and each time the system registers a device, it looks for a matching driver; on the contrary, each time the system registers a driver, it looks for a matching device, and the matching is done by the bus.
An instance of bus_type platform_bus_type is defined in Linux2.6 system.
Struct bus_type platform_bus_type = {.name = "platform", .dev _ attrs = platform_dev_attrs, .match = platform_match, / / Devices and drivers use the match function to determine whether they match .uevent = platform_uevent, .pm = PLATFORM_PM_OPS_PTR,} / * platform_match function is used to match drivers and devices in the bus * / static int platform_match (struct device * dev, struct device_driver * drv) {struct platform_device * pdev = to_platform_device (dev); struct platform_driver * pdrv = to_platform_driver (drv) / * match against the id table first * / if (pdrv- > id_table) return platform_match_id (pdrv- > id_table, pdev)! = NULL; / * fall-back to driver name match * / return (strcmp (pdev- > name, drv- > name) = 0);}
The platform_match function first determines whether it is by id_table, and if so, uses id_table to match. Otherwise, it determines the name in platform_device and platform_driver members, and matches if their name fields are the same. If there is a match, call the probe function of platform_driver.
Definition of platform_device structure
Struct platform_device {const char * name; / * name * / int id; struct device dev; U32 num_resources; / * Total Resources * / struct resource * resource; / * Resources * / struct platform_device_id * id_entry;}
One of the important members is resource, which is the resource information of the device, such as IO address, interrupt number and so on.
Struct resource {start value of resource_size_t start; / / Resource resource_size_t end; / / end value of Resource const char * name; unsigned long flags; / / Type of resource, such as IORESOURCE_IO,IORESOURCE_MEM,IORESOURCE_IRQ,IORESOURCE_DMA struct resource * parent, * sibling, * child;}
Some devices may have multiple resources, which are usually obtained by using the platform_get_resource function.
/ * * platform_get_resource-get a resource for a device * @ dev: platform device * @ type: resource type * @ num: resource index * / struct resource * platform_get_resource (struct platform_device * dev, unsigned int type, unsigned int num) {int i; for (I = 0; I
< dev->Num_resources; iTunes +) {struct resource * r = & dev- > resource [I]; if (type = = resource_type (r) & & num-- = = 0) return r;} return NULL;}
Registration of platform devices, using the platform_device_register function
Int platform_device_register (struct platform_device * pdev) {device_initialize (& pdev- > dev); return platform_device_add (pdev);}
The platform_device_register function initializes the device member of the platform_device through the device_initialize function, and then calls platform_device_add to add a platform device to the kernel.
Int platform_device_add (struct platform_device * pdev) {int I, ret = 0; if (! pdev) / * return EINVAL * / return-EINVAL; / * if pdev is empty, set pdev- > dev.parent to platform_bus * / if (! pdev- > dev.parent) pdev- > dev.parent = & platform_bus if pdev- > dev.parent is empty Pdev- > dev.bus = & platform_bus_type; / * set bus type * / if (pdev- > id! =-1) / * if id =-1 means automatic allocation of name * / dev_set_name (& pdev- > dev, "% s% d", pdev- > name, pdev- > id); else dev_set_name (& pdev- > dev, pdev- > name) For (I = 0; I)
< pdev->Num_resources; iTunes +) {struct resource * p, * r = & pdev- > resource [I]; / * get resources * / if (r-> name = = NULL) r-> name = dev_name (& pdev- > dev); p = r-> parent If (! p) {if (resource_type (r) = = IORESOURCE_MEM) / * set resource type * / p = & iomem_resource; else if (resource_type (r) = = IORESOURCE_IO) p = & ioport_resource } if (p & & insert_resource (p, r)) {printk (KERN_ERR "% s: failed to claim resource% d\ n", dev_name (& pdev- > dev), I); ret =-EBUSY; goto failed }} pr_debug ("Registering platform device'% slots." Parent at% s\ n ", dev_name (& pdev- > dev), dev_name (pdev- > dev.parent); / * add a device * / ret = device_add (& pdev- > dev) to the kernel; if (ret = = 0) return ret; failed: while (--I > = 0) {struct resource * r = & pdev- > resource [I] Unsigned long type = resource_type (r); if (type = = IORESOURCE_MEM | | type = = IORESOURCE_IO) release_resource (r);} return ret;}
Platform_device_add finally calls device_add to complete the registration of the platform device.
Conversely, if you want to log out of the platform device, use the platform_device_unregister function
Void platform_device_unregister (struct platform_device * pdev) {platform_device_del (pdev); platform_device_put (pdev);}
The platform_device_unregister function calls the platform_device_del function to log off the platform device.
Void platform_device_del (struct platform_device * pdev) {int i; if (pdev) {device_del (& pdev- > dev); for (I = 0; I
< pdev->Num_resources; iTunes +) {struct resource * r = & pdev- > resource [I]; unsigned long type = resource_type (r); if (type = = IORESOURCE_MEM | | type = = IORESOURCE_IO) release_resource (r);}
The platform_device_del function calls the device_del function to delete the platform device, and accordingly, to release the resource, the release_resource function should be called, provided that the resource type must be IORESOURCE_MEM or IORESOURCE_IO.
Definition of platform_driver:
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;}
Definition of device_driver:
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; const struct acpi_device_id * acpi_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;}
The platform_driver structure has device_driver members whose respective fields are shown above. Device_driver also has functions such as probe, remove, shutdown, and so on, which are initialized when the platform driver is registered.
As mentioned earlier, when there is a match between the match function of the platform device and the platform driver through the bus in the system, the probe function of platform_driver will be called with the parameter platform_device, and sometimes the matching will be determined by id_table.
Struct platform_device_id {char name [platform _ NAME_SIZE]; kernel_ulong_t driver_data _ _ attribute__ ((aligned (sizeof (kernel_ulong_t);}
Platform-driven registration uses the platform_driver_register function
Int platform_driver_register (struct platform_driver * drv) {drv- > driver.bus = & platform_bus_type; if (drv- > probe) drv- > driver.probe = platform_drv_probe; if (drv- > remove) drv- > driver.remove = platform_drv_remove; if (drv- > shutdown) drv- > driver.shutdown = platform_drv_shutdown If (drv- > suspend) drv- > driver.suspend = platform_drv_suspend; if (drv- > resume) drv- > driver.resume = platform_drv_resume; return driver_register (& drv- > driver);}
First initialize the driver in platform_driver, the type of driver is device_driver, set the bus of driver to platform_bus_type;, set the probe of driver to platform_drv_probe;, set remove of driver to platform_drv_remove;, set shutdown of driver to platform_drv_shutdown;, set suspend of driver to platform_drv_suspend;, set resume of driver to platform_drv_resume,***, call the driver_register function to register the platform driver.
Instead, to log out of the platform driver, use the platform_driver_unregister function
Void platform_driver_unregister (struct platform_driver * drv) {driver_unregister (& drv- > driver);} at this point, the study of "what is the bus driver device model of the Linux platform" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.