This article, through the platform bus device model as an example, explains the bus device driver Model:
Platform_device_ Register role: 1. Put the device into the bus's device chain List 2. Remove each driver from the bus's driver list, and use the bus's match function to determine if driver can support this device3. driver function PL called probe if supported Atform_driver_ Register role: 1. Put driver into bus driver link List 2. Remove each device from the bus's device list and use the bus's match function to determine if the driver can support this device3. Driver function to invoke if supported One, device driver 1. Constructs and initializes the platform device resource structure body resource
struct resource led_resource[] = { [0] = { 0x56000000, 0x5600000081, = ioresource_mem, }, [ 1] = { 5, 5, = Ioresouce_ IRQ, },};
2. Construct and initialize the platform device structure body Platform_device
struct platform_device led_dev = {. Name
= myled " =-1 ; . Num_resource = Array_size (Led_resource); . Resource = Led_resource,. Dev = = Led_release,},}; /* The release function can do nothing, but cannot be null */ static void led_release (struct device * dev) {}
3. Register platform Device
Platform_device_register (&led_dev);
Second, driver driver 1. Constructs and initializes the platform-driven structure body platform_driver
struct platform_driver led_drv = { = led_probe, = Led_remove, = { " myled " , },};
2. Construct and initialize the file_operations structure
Static structFile_operations Led_fops ={. Owner= This_module,/*This is a macro that is automatically created when you push the module to compile the __this_module variable*/. Open=Led_open,. Write=Led_write,};Static intLed_open (structInode *inode,structFile *file) { //PRINTK ("first_drv_open\n"); /*configured as Output*/*gpio_con &= ~ (0x3<< (pin*2)); *gpio_con |= (0x1<< (pin*2)); return 0; }Staticssize_t Led_write (structFile *file,Const Char__user *buf, size_t count, loff_t *PPOs) { intVal; //PRINTK ("first_drv_write\n");Copy_from_user (&val, buf, Count);//Copy_to_user (); if(val = =1) { //Lighting*gpio_dat &= ~ (1<<pin); } Else { //extinguishing lamp*gpio_dat |= (1<<pin); } return 0;}3. Implementing the probe function of the platform_driver structure
Static intLed_probe (structPlatform_device *Pdev) { structResource *Res; *Platform_get_resource*@dev:p Latformdevice*@type: ResourceType*@num: Resourceindex res= Platform_get_resource (Pdev, Ioresource_mem,0); Gpio_con= Ioremap (res->start,res->end-res->start+1); Gpio_dat= Gpio_con +1; Res= Platform_get_resource (Pdev, IORESOURCE_IRQ,0) Pin= res->start; Major= Register_chrdev (0,"myled", &led_fops); CLS= Class_create (This_module,"myled"); Device_create (CLS, NULL, MKDEV (Major,0), NULL,"led");}4. Register platform Driver Driver
Platform_driver_register (&LED_DRV);