Step by step, analyze platform device initialization platform_device_register ()

Source: Internet
Author: User

linux2.6.30 Peripherals and other devices, will be at the time of power-up, the overall initialization, and then, the device driver initialization, and then, when the system is running, the device will be hot-swappable to be recognized by the system ....

Then, when the system power on the device initialization, how to do. How to manage.

To find out, start with the platform device registration function to see Platform_device_register ()

Base_platform.c
/** * Platform_device_register-add a platform-level device * @pdev: Platform device we ' re adding */int Platform_device_r Egister (struct Platform_device *pdev) {device_initialize (&pdev->dev); return Platform_device_add (Pdev);}
First, look at how the incoming parameters are defined!

struct Platform_device {const char* name;<span style= "white-space:pre" ></span>//device name, What kind of device is the driver to drive the work, matching find when need this name Compareintid;<span style= "White-space:pre" ></span>//Device ID, The same driver corresponds to multiple devices, such as a serial port driver can drive several serial devices, a truth. struct Devicedev;<span style= "white-space:pre" ></span>//device information device driver address, this variable will be assigned when the device is actually driven u32num_ resources;//number of memory addresses struct resource* resource;//device memory address contains information such as register address and interrupt number struct PLATFORM_DEVICE_ID*ID_ ENTRY;//platform Device ID after the entry driver and device match, this variable} will be used;
The entry parameters are over, now it's easy to see what the device registration functions have done?

Two things: Initialization of device information, adding devices

1), Device information initialization

How to initialize, first look at the code

</pre><pre name= "code" class= "CPP" >/** * device_initialize-init device structure. * @dev: Device. * * This prepares, the device for use, and layers by initializing * it fields.  * It is the first half of Device_register (), if called by * This function, though it can also be called separately, so one * May use @dev's fields. In particular, Get_device ()/put_device () * May is used for reference counting of @dev after calling this * function. * * Note:use Put_device () to give up your reference instead of freeing * @dev directly once the called this function . */void device_initialize (struct device *dev) {Dev->kobj.kset = Devices_kset;<span style= "Font-family:courier New ; Font-size:12px;color: #008000; " ><em>//kset to creat/sys/devices the device directory in the SYS directory to facilitate subsequent setup of equipment information Sysfs</em></span>kobject_init ( &dev->kobj, &device_ktype);//Initialize the device in the SYSFS operation information, operation options, the device in the SYS directory of the contents of the list storage, easy to find and display Init_list_head (& Dev->dma_pools);//Initialize DMA pool Init_mutex (&dEV-&GT;SEM)///Semaphore initialization spin_lock_init (&dev->devres_lock)//dev resource spin lock initialization init_list_head (&dev-> devres_head);//devres list initialization device_init_wakeup (dev, 0);//device_pm_init (dev); Set_dev_node (Dev,-1);}
A second step is to add the device to the bus

/** * Platform_device_add-add A platform device to device hierarchy * @pdev: Platform device we ' re adding * * This is PA  RT 2 of Platform_device_register (), though may be called * separately _iff_ Pdev is allocated by Platform_device_alloc (). */int platform_device_add (struct platform_device *pdev) {int I, ret = 0;if (!pdev) return-einval;if (!pdev->dev.parent ) Pdev->dev.parent = &platform_bus;pdev->dev.bus = &platform_bus_type;<span style= "color: #ff6666;" >if (Pdev->id! =-1) dev_set_name (&pdev->dev, "%s.%d", Pdev->name,  pdev->id); elsedev_set_name (&pdev->dev, pdev->name); <em>//Device ID Some pre-set, not pre-initialized, at this time initialize. For example, the serial port has been initialized, serial port 1 2 3 4 ... </em></span>
<span style= "White-space:pre" ></span><em><span style= "color: #ff0000;" >//name may be set to Uart1 ... </span></em><span style= "White-space:pre" ></span>/*
<span style= "White-space:pre" ></span> the following for code, mainly to classify the device, Io, or IRQ? Or a mem? Or DMA? Wait a minute...
<span style= "White-space:pre" ></span> device Division macro definition, see follow-up. The criterion of judging is mainly the bit8-bit11 of the variable flag.
<span style= "White-space:pre" ></span>*/for (i = 0; i < pdev->num_resources; i++) {struct resource *p, *r  = &pdev->resource[i];if (R->name = = NULL) R->name = Dev_name (&pdev->dev);p = r->parent;if (!p) {if (Resource_type (r) = = Ioresource_mem) p = &iomem_resource;else if (resource_type (r) = = Ioresource_io) p = &ioport_r esource;} if (P && insert_resource (P, r)) {PRINTK (kern_err "%s:failed to claim Resource%d\n", Dev_name (&AMP;PD Ev->dev), i); ret =-ebusy;goto failed;}} Pr_debug ("Registering Platform Device '%s '). Parent at%s\n ", Dev_name (&pdev->dev), Dev_name (pdev->dev.parent)); <span style=" White-space:pre "> </span>//Key key is that this device add function, the specific analysis see the next section bar ret = Device_add (&pdev->dev); if (ret = = 0) return ret; Failed:while (->= 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;} EXPORT_SYMBOL_GPL (Platform_device_add); 

Device Type Division

#define Ioresource_io0x00000100#define Ioresource_mem0x00000200#define Ioresource_irq0x00000400#define IORESOURCE_ dma0x00000800



Step by step, analyze platform device initialization platform_device_register ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.