Linux device driver model - platform bus

1.


On the basis of the driver model, the actual device driver can be built. Here, the platform bus is introduced. Because the platform bus is representative, the platform is not an actual bus, it is virtualized, so any on the device All hardware drivers can be hung on this bus. The most typical one is that the controller modules on the device are all hung on the platform bus; the platform bus driver mode is based on the evolution of the devices_bus_driver driver mode.

2. Platform_bus_type structure declaration

struct bus_type platform_bus_type = {
 .name  = "platform",
 .dev_attrs = platform_dev_attrs,
 .match  = platform_match,
 .uevent  = platform_uevent,
 .pm  = &platform_dev_pm_ops,
};

The platform_bus_rype bus initialization function is platform_bus_init, and the function is initially called under the linux main startup call to see the implementation of platform_bus_init

int __init platform_bus_init(void)
{
 int error;

 early_platform_cleanup();

 error = device_register(&platform_bus);
 if (error)
  return error;
 error =  bus_register(&platform_bus_type);   // 基于总线注册
 if (error)
  device_unregister(&platform_bus);
 return error;
}

 2.  platform_device   结构体
struct platform_device {
 const char * name;
 int  id;
 struct device dev;
 u32  num_resources;
 struct resource * resource;

 const struct platform_device_id *id_entry;

 /* MFD cell pointer */
 struct mfd_cell *mfd_cell;

 /* arch specific additions */
 struct pdev_archdata archdata;
};

 interface

extern int platform_device_register(struct platform_device *);
extern void platform_device_unregister(struct platform_device *);

 Analyze platform_device_register() function implementation

int platform_device_register(struct platform_device *pdev)
{
 device_initialize(&pdev->dev);
 arch_setup_pdev_archdata(pdev);
 return platform_device_add(pdev);
}

int platform_device_add(struct platform_device *pdev)
{
 int i, ret = 0;

 if (!pdev)
  return -EINVAL;

 if (!pdev->dev.parent)
  pdev->dev.parent = &platform_bus;

 pdev->dev.bus = &platform_bus_type;

 if (pdev->id != -1)
  dev_set_name(&pdev->dev, "%s.%d", pdev->name,  pdev->id);
 else
  dev_set_name(&pdev->dev, "%s", pdev->name);

 for (i = 0; i < pdev->num_resources; i++) {
  struct resource *p, *r = &pdev->resource[i];

  if (r->name == NULL)
   r->name = dev_name(&pdev->dev);

  p = r->parent;
  if (!p) {
   if (resource_type(r) == IORESOURCE_MEM)
    p = &iomem_resource;
   else if (resource_type(r) == IORESOURCE_IO)
    p = &ioport_resource;
  }

  if (p && insert_resource(p, r)) {
   printk(KERN_ERR
          "%s: failed to claim resource %d\n",
          dev_name(&pdev->dev), i);
   ret = -EBUSY;
   goto failed;
  }
 }

 pr_debug("Registering platform device '%s'. Parent at %s\n",
   dev_name(&pdev->dev), dev_name(pdev->dev.parent));

 ret = device_add(&pdev->dev); // The function device adds the same process @cyl
 if (ret == 0)
  return ret;

 failed:
 while (--i >= 0) {
  struct resource *r = &pdev->resource[i];
  unsigned long type = resource_type(r);

  if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
   release_resource(r);
 }

 return ret;
}

3. struct platform_driver structure declaration

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 (*resume)(struct platform_device *);
 struct device_driver driver;
 const struct platform_device_id *id_table;
};
接口 int platform_driver_register(struct platform_driver *);
extern void platform_driver_unregister(struct platform_driver *);

分析 platform_driver_register(struct platform_driver *) 注册流程函数
int platform_driver_register(struct platform_driver *drv)
{
 drv->driver.bus = &platform_bus_type;
 if (drv->probe)
  drv->driver.probe = platform_drv_probe;
 if (drv->remove)
  drv->driver.remove = platform_drv_remove;
 if (drv->shutdown)
  drv->driver.shutdown = platform_drv_shutdown;

 return driver_register(&drv->driver); // driver function registration is the same
}







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324731874&siteId=291194637