The following describes how to register a driver:
1. Define a platform_driver Structure
2. initialize this structure, specify functions such as probe and remove, and initialize the driver variable.
3. Implement functions such as probe and remove.
View the structure of platform_driver, which is defined in the include/Linux/platform_device.h file:
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 device_driver driver;
};
It can be seen that it contains several functional functions for device operations. It also contains a device_driver structure. The variable needs to be initialized in the driver. The definition of this variable is located in include/Linux/device. h:
Struct device_driver {
Const char * Name;
Struct bus_type * bus;
Struct kobject kobj;
Struct klist klist_devices;
Struct klist_node knode_bus;
Struct module * owner;
Const char * mod_name;/* used for built-in modules */
Struct module_kobject * mkobj;
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 );
};
Note the two variables: Name and owner. The role is to associate with the relevant platform_device. The role of the owner is to indicate the owner of the module, and the driver is generally initialized as this_module.
The following is an initialization instance of platform_driver:
Static struct platform_driver s3c2410iis_driver = {
. Probe = s3c2410iis_probe,
. Remove = s3c2410iis_remove,
. Driver = {
. Name = "s3c2410-iis ",
. Owner = this_module,
},
};
The above Initialization is an audio driver instance. Note that the driver struct only initializes the number of its name and owner. Next, let's take a look at another structure related to the driver, which is defined as follows:
Struct platform_device {
Const char * Name;
Int ID;
Struct device dev;
U32 num_resources;
Struct resource * resource;
};
This structure also has a name variable. Platform_driver is a device driver. Who is the device driver? Of course it is a device. Platform_device describes the device object. The following is an example:
Struct platform_device initi_device_iis = {
. Name = "s3c2410-iis ",
. ID =-1,
. Num_resources = array_size (cloud_iis_resource ),
. Resource = maid,
. Dev = {
. Dma_mask = & cloud_device_iis_dmamask,
. Coherent_dma_mask = 0 xfffffffful
}
};
Its name variable is consistent with the name variable of platform_driver. The kernel uses this consistency to find resources for the driver, that is, resources in platform_device. The structure is defined as follows in include/Linux/ioport. h:
Struct resource {
Resource_size_t start;
Resource_size_t end;
Const char * Name;
Unsigned long flags;
Struct resource * parent, * sibling, * child;
};
The following is an example:
Static struct resource initi_iis_resource [] = {
[0] = {
. Start = s3c24xx_pa_iis,
. End = s3c24xx_pa_iis + s3c24xx_sz_iis-1,
. Flags = ioresource_mem,
}
};
The role of this structure is to tell the start address and end address of the driver device and the port type of the device. The address here refers to the physical address.
In addition, pay attention to the device structure in platform_device, which describes the situation of the device in detail. The definition is as follows:
Struct device {
Struct klist klist_children;
Struct klist_node knode_parent;/* node in sibling list */
Struct klist_node knode_driver;
Struct klist_node knode_bus;
Struct device * parent;
Struct kobject kobj;
Char bus_id [bus_id_size];/* position on parent bus */
Struct device_type * type;
Unsigned is_registered: 1;
Unsigned uevent_suppress: 1;
Struct semaphore SEM;/* semaphore to synchronize callto
* Its driver.
*/
Struct bus_type * bus;/* type of bus device is on */
Struct device_driver * driver;/* Which driver has allocated this
Device */
Void * driver_data;/* data private to the driver */
Void * platform_data;/* platform specific data, Device
Core doesn' t touch it */
Struct dev_pm_info power;
# Ifdef config_numa
Int numa_node;/* NUMA node this device is close */
# Endif
U64 * dma_mask;/* DMA mask (if DMA 'able device )*/
U64 coherent_dma_mask;/* Like dma_mask, but
Alloc_coherent mappings
Not all hardware supports
64-bit addresses for consistent
Allocations such descriptors .*/
Struct list_head dma_pools;/* DMA pools (if DMA 'ble )*/
Struct dma_coherent_mem * dma_mem;/* internal for coherent mem
Override */
/* Arch specific additions */
Struct dev_archdata archdata;
Spinlock_t devres_lock;
Struct list_head devres_head;
/* Class_device migration path */
Struct list_head node;
Struct class * class;
Dev_t devt;/* dev_t, creates the sysfs "Dev "*/
Struct attribute_group ** groups;/* optional groups */
Void (* release) (struct device * Dev );
};
The main structures involved in the driver are described above. The following describes how to process the structure in the driver to enable the driver to run.
I believe everyone knows the module_init () Macro. The macro is called when the driver module is loaded. It receives a function as a parameter, and the function as its parameter will process the aforementioned platform_driver. Let's take a look at an example: if the module_init parameter to be received is the function number of s3c2410_uda1341_init, the following is the definition of this function:
Static int _ init s3c2410_uda1341_init (void ){
Memzero (& input_stream, sizeof (audio_stream_t ));
Memzero (& output_stream, sizeof (audio_stream_t ));
Return platform_driver_register (& s3c2410iis_driver );
}
Note that the last row of the function body calls the platform_driver_register function. This function is defined in Driver/base/platform. C. The prototype is as follows:
Int platform_driver_register (struct platform_driver * DRV)
Its function is to specify function functions for the variables probe and remove in the driver structure of plarform_driver mentioned above.
So far, the kernel has known such a driver module. When the kernel is started, the probe function related to the driver is called. Let's take a look at the functions implemented by the probe function.
The prototype of the probe function is
Int xxx_probe (struct platform_device * pdev)
That is, it returns an int type and receives a pointer of the platform_device type as a parameter. The return type is the error code we are familiar with. What about the received parameter? As we have mentioned above, if the driver serves the device, you need to know the device information. This parameter contains device-related information.
After the probe function receives the plarform_device parameter, it needs to extract the required information. It generally obtains relevant information by calling the kernel functions such as platform_get_resource and platform_get_irq. For example, after obtaining the start address of a device through platform_get_resource, you can perform operations such as request_mem_region and ioremap for the application to operate on the device. After obtaining the device interrupt number through platform_get_irq, you can call the request_irq function to request an interruption from the system. These operations are generally completed in the device driver.
After completing the above work and some other necessary initialization operations, you can register the device files that we can see in the/dev directory with the system. For example, in the audio chip driver, you can call register_sound_dsp to register a DSP device file. In the LCD driver, you can call register_framebuffer to register the FB device file. After this is done, the system will have the device files we need. Operations related to device files are performed through a file_operations
. When calling functions such as register_sound_dsp, you need to pass a file_operations pointer. This pointer provides write, read, and other functions that can be called by the user space. The file_operations structure is defined in include/Linux/fs. h and is listed as follows:
Struct file_operations {
Struct module * owner;
Loff_t (* llseek) (struct file *, loff_t, INT );
Ssize_t (* read) (struct file *, char _ User *, size_t, loff_t *);
Ssize_t (* write) (struct file *, const char _ User *, size_t, loff_t *);
Ssize_t (* aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t );
Ssize_t (* aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t );
INT (* readdir) (struct file *, void *, filldir_t );
Unsigned int (* poll) (struct file *, struct poll_table_struct *);
INT (* IOCTL) (struct inode *, struct file *, unsigned int, unsigned long );
Long (* unlocked_ioctl) (struct file *, unsigned int, unsigned long );
Long (* compat_ioctl) (struct file *, unsigned int, unsigned long );
INT (* MMAP) (struct file *, struct vm_area_struct *);
INT (* open) (struct inode *, struct file *);
INT (* flush) (struct file *, fl_owner_t ID );
INT (* release) (struct inode *, struct file *);
INT (* fsync) (struct file *, struct dentry *, int datasync );
INT (* aio_fsync) (struct kiocb *, int datasync );
INT (* fasync) (INT, struct file *, INT );
INT (* Lock) (struct file *, Int, struct file_lock *);
Ssize_t (* sendpage) (struct file *, struct page *, Int, size_t, loff_t *, INT );
Unsigned long (* get_unmapped_area) (struct file *, unsigned long, unsigned long );
INT (* check_flags) (INT );
INT (* dir_notify) (struct file * filp, unsigned long Arg );
INT (* flock) (struct file *, Int, struct file_lock *);
Ssize_t (* splice_write) (struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned INT );
Ssize_t (* splice_read) (struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned INT );
INT (* setlease) (struct file *, long, struct file_lock **);
};
So far, the probe function has been completed.
When you open a device and call its functions such as read and write, you can use file_operations to find related functions. Therefore, the user driver also needs to implement these functions. The specific implementation is closely related to the relevant devices, so we will not introduce them here.
Do_basic_setup ()-> driver_init ()-> platform_bus_init ()->... initialize platform bus (Virtual Bus)
When the device registers with the kernel, platform_device_register ()-> platform_device_add ()->... the kernel hangs the device under the virtual platform bus.
Platform_driver_register ()-> driver_register ()-> bus_add_driver ()-> driver_attach ()-> bus_for_each_dev () perform _ driver_attach ()-> driver_probe_device ()-> DRV-> bus-> match () = platform_match ()-& gt; compare strncmp (pdev-> name, DRV-> name, bus_id_size), call platform_drv_probe ()-> driver-> probe () if they match
Binding the device to the driver.