Linux has been done for three months, some of the structure of the code has not been very clear, for example, Platform_device and Platform_driver have been unclear relationship. Search on the Internet, do a summary. The order of the two is to define the Platform_device, Platform_device->, and then define the platform_driver-> register Platform_driver.
(1) The registration process for the Platform_device device must be called before the corresponding device driver is loaded, because the device name that was registered in the kernel is required to be matched to the driver. Platform_device is a register in the S3c_arch_init () function in init.c during system startup. This function is declared as Arch_initcall (S3c_arch_init); will be called during the system initialization phase. The arch_initcall has a priority higher than module_init, so it is called before the platform-driven register. Today, the kernel is not using the Arch_initcall (s3c_arch_init) Platform_device structure but through the. Init_machine member to save it in Arch_initcall (customize_ Machine) waiting to be called (Machine_start to Machine_end defined in mach-smdk6410.c); in fact, the same quality is placed on the. Initcall3.init waiting to be called. The struct struct platform_driver is then defined, and the function Platform_driver_register () Platform_driver is called in the driver initialization function. The concrete process descriptive narration is for example the following:
Linux from the 2.6 version of the introduction of the concept of platform, in the development of the underlying driver, the first thing to be sure is the device resource information, in the 2.6 kernel will be the resources of each device in the structure Platform_device to describe the narrative, the structure is defined in the kernel In/include/linux/platform_device.h,
struct platform_device{ const char * name; U32 ID; struct device dev; U32 num_resources; struct resource * resource;};
An important element of the structure is resource, which is the most important device resource information, defined in Kernel/include/linux/ioport.h,
Example:
struct Resource { const char *name; Unsigned long start, end; unsigned long flags; struct resource *parent, *sibling, *child;};
In fact, for example:
static struct resource s3c_usb_resource[] = {[0] = { . Start = S3c_pa_usbhost, . End = s3c_pa_usbhost + s3c_sz_ USBHOST-1, . Flags = Ioresource_mem, }, [1] = { . Start = Irq_uhost, . End = Irq_uhost, . Flags = IO RESOURCE_IRQ, }};
The above is the resource information assigned by USB host 6410. The 1th group describes the bus address range occupied by this USB host device, the starting address and size are determined by the hardware, ioresource_mem means that the 1th group describes the memory type of resource information; The 2nd group describes the interrupt number of the USB host device, which is also set by the hardware. IORESOURCE_IRQ that the 2nd group describes the interruption of resource information. The device driver obtains the corresponding resource information according to flags.
With the resource information, you can define the Platform_device:
struct Platform_device S3c_device_usb = { . Name = "S3C2410-OHCI", //s3c6410-usb . ID =-1, . Num_resources = array_size (S3c_usb_resource), . Resource = S3c_usb_resource, . Dev = { . Dma_mask = &s3c_device_usb_dmamask, . Coherent_dma_mask = 0xffffffffUL };
With Platform_device, you can call the function platform_add_devices to add the device to the system. Device resources in the system can be enumerated in such a way, and then into an array of pointers, such as:
static struct Platform_device *smdk6410_devices[] __initdata = {
......
&s3c_device_usbgadget,
&S3C_DEVICE_USB,//jeff Add.
......
}
Then run in the 6410 initialization function smdk6410_machine_init ():
Platform_add_devices (Smdk6410_devices, Array_size (smdk6410_devices)); Add all the device to the system. The advantage of Platform_add_devices is that it runs multiple platform_device_register at once.
(2) As to the driver needs to implement struct struct platform_driver, also defined in the kernel/include/linux/platform_device.h:
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 (*suspend_late) (struct Platform_device *, pm_message_t State); Int (*resume_early) (struct platform_device *); Int (*resume) (struct platform_device *); struct Pm_ext_ops *pm; struct device_driver driver;};
The USB host implementation at that point is:
static struct Platform_driver Ohci_hcd_s3c2410_driver = { . Probe = Ohci_hcd_s3c2410_drv_probe, . Remove = Ohci_hcd_s3c2410_drv_remove, . Shutdown = usb_hcd_platform_shutdown, /*.suspend = ohci_hcd_s3c2410_ Drv_suspend, */ /*.resume = ohci_hcd_s3c2410_drv_resume, */ . Driver = { . Owner = This_module, . Name = "S3C2410-OHCI", },};
Call the function Platform_driver_register () in the driver initialization (OHCI-HCD.C 1124 line) function to register the Platform_driver, it is important to note that the name in the S3C_DEVICE_USB structure elements and Ohci_hcd_s3c2410_driver structures in driver. Name must be the same so that the Platform_driver_register () register will have the name of the element in all the Platform_device and the current register PLATFORM_ Driver Driver.name to compare, only found with the same name Platform_device exist, platform_driver Talent register success. When the register succeeds, it calls the PLATFORM_DRIVER structure element probe function pointer, and here is ohci_hcd_s3c2410_drv_probe to start probing. The functions in the platform driver are entered with the platform device as the number of references.
(3) Why does the name of two name have to match the ability to implement device and driver bindings? (1) Kernel_init ()->do_basic_setup ()->driver_init ()->platform_bus_init () Initialization of Platform_bus (virtual bus) at kernel initialization ; (2) When the Equipment Register Platform_device_register ()->platform_device_add () (Pdev->dev.bus = &platform_bus_type) Hang the device under the virtual platform bus; (3) when driving the Register Platform_driver_register ()->driver_register ()->bus_add_driver () Driver_attach ()->bus_for_each_dev (), for each device hanging on the virtual platform bus as __driver_attach ()->driver_probe_device (), Infer if Drv->bus->match () is present and successful, run Platform_match by pointer, strncmp (Pdev->name, Drv->name, Bus_id_size) , assuming that the match is called Really_probe (actually is the corresponding device running Platform_driver->probe (platform_device), note platform_drv_probe _ The dev reference is obtained by Bus_for_each_dev's Next_device ) and starts a real probe load, assuming probe succeeds binding the device to the driver.
When entering the probe function, it is necessary to obtain the resource information of the device, depending on the type specified by the parameter, such as IORESOURCE_MEM, to obtain the specified resource separately.
struct resource * Platform_get_resource (struct platform_device *dev, unsigned int type, unsigned int num), and of course, the ability to fix resource types such as Take the interrupt number in the resource: struct int platform_get_irq (struct platform_device *dev, unsigned int num);
The probe function generally completes the hardware device enable, the acquisition of the struct resource, and the dynamic mapping of the virtual address and the registration of the detailed type device (since the platform device is only a virtual device type); Remove function completes the shutdown of the hardware device, the struct Resource and the release of dynamic mappings for virtual addresses and verbose type device logoff. It is possible to implement a addresses and IRQs with a relatively independent resource (in other words, a device that does not have the minimum system required to perform the kernel), rather than the kernel itself. such as: Lcd,usb,uart, and so on, can be written with platfrom_driver, and timer,irq such as the smallest system within the device is best not platfrom_driver mechanism, in fact, the core implementation is this.
References: http://blog.chinaunix.net/u1/49507/showart_494193.html
References: http://blog.csdn.net/yd4330152763132/archive/2010/02/01/5275776.aspx
Platform_device and Platform_driver