Linux device driver Development-platform device driver

Source: Internet
Author: User
Tags mutex

A new device driver model-platform (platform) device driver, platform device driver (platform_device) and platform driver (Platform_driver) is introduced into the Linux2.6 kernel, The introduction of platform devices makes Linux device drivers more portable.

First, platform equipment
Platform Device structure:

1 structPlatform_device {2     Const Char* NAME;/*Device Name*/3     intID;4     structDevice Dev;/*Device Structural Body*/5U32 num_resources;/*number of device resources*/6     structResource * resource;/*Equipment Resources*/7 8     Const structPLATFORM_DEVICE_ID *Id_entry;9 Ten     /*Arch Specific Additions*/ One     structPdev_archdata Archdata; A};

Platform equipment is mainly to provide equipment resources and platform data to the platform driver, resource as an array of equipment resources, types have ioresource_io, Ioresource_mem, IORESOURCE_IRQ, IORESOURCE_DMA, Ioresource_dma. The following is a network card chip DM9000 Peripheral resources:

1 Static structResource dm9000_resources[] = {2[0] = {3. Start =s3c64xx_pa_dm9000,4. end = s3c64xx_pa_dm9000 +3,5. Flags =Ioresource_mem,6     },7[1] = {8. Start = s3c64xx_pa_dm9000 +4,9. end = s3c64xx_pa_dm9000 + s3c64xx_sz_dm9000-1,Ten. Flags =Ioresource_mem, One     }, A[2] = { -. Start = Irq_eint (7), -. End = Irq_eint (7), the. Flags = IORESOURCE_IRQ |Irqf_trigger_high, -     }, -};

Dm9000_resources There are three device resources, the first is the Ioresource_mem type, indicating that the first resource memory start address is s3c64xx_pa_dm9000 End address is s3c64xx_pa_dm9000 + 3, the second is also the Ioresource_mem type, indicating that the second resource memory start address is s3c64xx_pa_dm9000 + 4 End address is s3c64xx_pa_dm9000 + s3c64xx_sz_dm9000- 1, the third is a IORESOURCE_IRQ type, indicating that the interrupt number is Irq_eint (7).

1 structDevice {2     structDevice *parent;3 4     structDevice_private *p;5 6     structKobject kobj;7     Const Char*init_name;/*Initial name of the device*/8     structDevice_type *type;9 Ten     structMutex mutex;/*Mutex to synchronize calls to One * its driver. A                      */ -  -     structBus_type *bus;/*type of bus device is on*/ the     structDevice_driver *driver;/*which driver have allocated this - Device*/ -     void*platform_data;/*Platform specific data, device - core doesn ' t touch it*/ +     ... -};

The struct device structure has an important member Platform_data, which is an important member of platform device and platform driver for data transfer.

Platform Device Registration:

1 int platform_device_register (struct platform_device *pdev);

Platform_device_register () invokes the Platform_device_register () function to add it to the subsystem after the platform device has been initialized accordingly.

Platform Device Logoff:

1 void platform_device_unregister (struct platform_device *pdev);

The Platform_device_unregister () function removes the device resource from the subsystem after it has been freed.

Platform Device templates:

1 Static structResource Xxx_resource =2 {3[0] =4     {5. Start = ...,6. end = ...,7. Flags = ...,8     },9[1] =Ten     { One         ... A     } -     ... - }; the  - Static structXxx_plat_data Xxx_data = - { -     ... + }; -  + Static structPlatform_device Xxx_platform_device = A { at. Name =NAME, -. num_resources =array_size (xxx_resource), -. Resource =Xxx_resource, -. Dev = -     { -. Platform_data = &Xxx_data, in     } - }; to  + Static int__init Xxx_device_init (void) - { the     ... *     /*Registering platform Devices*/ $Platform_device_register (&xxx_platform_device);Panax Notoginseng     ... - } the  + Static void__exit Xxx_device_exit (void) A { the     ... +     /*Unregister platform Devices*/ -Platform_device_unregister (&xxx_platform_device); $     ... $}

Second, platform-driven
Platform-driven structure:

1 structPlatform_driver {2     int(*probe) (structPlatform_device *);3     int(*remove) (structPlatform_device *);4     void(*shutdown) (structPlatform_device *);5     int(*suspend) (structPlatform_device *, pm_message_t state);6     int(*resume) (structPlatform_device *);7     structDevice_driver driver;8     Const structPLATFORM_DEVICE_ID *id_table;9};

The name of the platform-driven structure driver member must be identical to the name member inside the platform device structure, and when the system registers a device, it is matched by the name member inside the device structure and the name member in the platform driver driver. When the match succeeds, the platform-driven probe function is called,
It is common to get the resources and private data of the platform device and initialize the device in the probe function.

Get Device resources:

1 struct resource *platform_get_resource (structintint num);

The Platform_get_resource () function is used to obtain resources for platform devices, dev for platform device, type for platform device resource, num for Platform resource number (two for resource number 0, 1 for a resource).

Platform-driven registration:

1 int platform_driver_register (struct platform_driver *drv);

The Platform_driver_register () function completes the platform-driven registration, which is called when the driver module is loaded.

Platform-driven logoff:

1 void platform_driver_unregister (struct platform_driver *drv);

The Platform_driver_unregister () function completes the platform-driven logoff, which is called when the driver module is unloaded.

Platform-driven templates:

1 Static int__devinit Xxx_probe (structPlatform_device *Pdev)2 {3     structXxx_plat_data *pdata = pdev->dev.platform_data;/*Get Private Data*/4Platform_get_resource (PDEV,XXX,X);/*Get device Resources*/5     ...6 }7 8 Static structPlatform_driver Xxx_platform_driver =9 {Ten. Probe =Xxx_probe, One. remove =__devexit_p (xxx_remove), A. Driver = -     { -. Name = Name,/*consistent with platform device name*/ the         ... -     }, -     ... - }; +  - Static int__init Xxx_driver_init (void) + { A     ... at     /*Driver Registration*/ -Platform_driver_register (&xxx_platform_driver); -     ... - } -  - Static void__exit Xxx_driver_exit (void) in { -     ... to     /*Driver Logoff*/ +Platform_driver_unregister (&xxx_platform_driver); -     ... the}

Linux device driver Development-platform device driver

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.