Write a device driver for linux2.6

Source: Internet
Author: User

The platform concept has been introduced since version 2.6. When developing the underlying driver, you must first confirm the device resource information, such as the device address,

In the 2.6 kernel, the resources of each device are described using the structure platform_device. The struct is defined in kernel \ 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 this structure is resource, which stores the most important device resource information and is defined in kernel \ include \ Linux \ ioport. h,
Struct resource {
Const char * Name;
Unsigned long start, end;
Unsigned long flags;
Struct resource * parent, * sibling, * child;
};
The following is an example:
Defined in kernel \ arch \ Arm \ Mach-PXA \ PXA27X. c
Tatic struct resource pxa27x_ohci_resources [] = {
[0] = {
. Start = 0x4c000000,
. End = 0x4c00ff6f,
. Flags = ioresource_mem,
},
[1] = {
. Start = irq_usbh1,
. End = irq_usbh1,
. Flags = ioresource_irq,
},
};
Two groups of resources are defined here, which describe the resources of a USB host device, and 1st describe the resources occupied by the USB host device.
BUS address range. ioresource_mem indicates that the 1st group describes the memory-type resource information, and the 2nd group describes the USB host device.
Ioresource_irq indicates the interrupt resource information described in Group 2nd. The device driver obtains the corresponding resource information based on flags.
With the resource information, you can define platform_device:
Static struct platform_device ohci_device = {
. Name = "pxa27x-ohci ",
. ID =-1,
. Dev = {
. Dma_mask = & pxa27x_dmamask,
. Coherent_dma_mask = 0 xffffffff,
},
. Num_resources = array_size (pxa27x_ohci_resources ),
. Resource = pxa27x_ohci_resources,
};
With platform_device, you can call the platform_add_devices function to add the device to the system. The implementation here is
Static int _ init pxa27x_init (void)
{
Return platform_add_devices (devices, array_size (devices ));
}
Pxa27x_init must be called before the device driver is loaded.
Subsys_initcall (pxa27x_init );

The driver needs to implement the struct platform_driver, refer to kernel \ driver \ USB \ host \ ohci-pxa27.c,

Static struct platform_driver ohci_hcd_pxa27x_driver = {
. Probe = ohci_hcd_pxa27x_drv_probe,
. Remove = ohci_hcd_pxa27x_drv_remove,
# Ifdef config_pm
. Suspend = ohci_hcd_pxa27x_drv_suspend,
. Resume = ohci_hcd_pxa27x_drv_resume,
# Endif
. Driver = {
. Name = "pxa27x-ohci ",
},
};

Call the platform_driver_register () function in the driver initialization function to register platform_driver. Note that
In the ohci_device structure, the name element and the driver. name must be the same in the ohci_hcd_pxa27x_driver structure.
When you register platform_driver_register (), the name and current note of all registered platform_devices are
Compare the driver. Name of the platform_driver. Only platfomr_device with the same name can be registered.
Successful. when the registration is successful, the probe function pointer of the platform_driver structure element will be called. Here it is ohci_hcd_pxa27x_drv_probe.

After entering the probe function, you need to obtain the resource information of the device. The functions for obtaining the resource include:
Struct resource * platform_get_resource (struct platform_device * Dev, unsigned int type, unsigned int num );
Obtain the specified Resource Based on the type specified by the parameter, such as ioresource_mem.
Struct int platform_get_irq (struct platform_device * Dev, unsigned int num );
Gets the interrupt number in the resource.
Struct resource * platform_get_resource_byname (struct platform_device * Dev, unsigned int type, char * Name );
Obtains the specified Resource Based on the name specified by the parameter name.
Int platform_get_irq_byname (struct platform_device * Dev, char * Name );
Gets the interrupt number in the resource based on the name specified by the parameter name.

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.