Linux设备模型(三)platform

来源:互联网 发布:云智能网络 编辑:程序博客网 时间:2024/06/05 21:10

platform设备和驱动与linux设备模型密切相关。platform在linux设备模型中,其实就是一种虚拟总线没有对应的硬件结构。它的主要作用就是管理系统的外设资源。linux在引入了platform机制之后,内核假设所有的这些外设都挂载在platform虚拟总线上,以便进行统一管理。

1.Platform初始化

在系统中platform对应的文件drivers/base/platform.c,它不是作为一个模块注册到内核的,关键的注册总线的函数由系统初始化部分,系统启动时初始化时创建了platform_bus设备和platform_bus_type总线。内核初始化函数kernel_init()中调用了do_basic_setup(),该函数中调用driver_init(),该函数中调用platform_bus_init(),我们看看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);//注册platform_bus_type总线到内核if (error)device_unregister(&platform_bus);return error;}
struct device platform_bus = {.init_name= "platform",};
struct bus_type platform_bus_type = { .name  = "platform", .dev_attrs = platform_dev_attrs,//设备属性 .match  = platform_match,//match函数,这个函数在当属于platform的设备或者驱动注册到内核时就会调用,完成设备与驱动的匹配 工作 .uevent  = platform_uevent,// 热插拔操作函数 .pm  = &platform_dev_pm_ops,};};

改函数把设备名为platform的设备platform_bus注册到系统中,其他的platform的设备都会以它为parent。它在sysfs中目录下. /sys/devices/platform接着bus_register(&platform_bus_type)注册了platform_bus_type总线类型。

来看看platform_match函数:

static int platform_match(struct device *dev, struct device_driver *drv){struct platform_device *pdev = to_platform_device(dev);struct platform_driver *pdrv = to_platform_driver(drv);/* Attempt an OF style match first */if (of_driver_match_device(dev, drv))return 1;/* Then try to match against the id table */if (pdrv->id_table)return platform_match_id(pdrv->id_table, pdev) != NULL;/* fall-back to driver name match */return (strcmp(pdev->name, drv->name) == 0);}
static const struct platform_device_id *platform_match_id(const struct platform_device_id *id,struct platform_device *pdev){while (id->name[0]) {if (strcmp(pdev->name, id->name) == 0) {pdev->id_entry = id;return id;}id++;}return NULL;}

不难看出,如果pdrvid_table数组中包含了pdev->name,或者drv->namepdev->name名字相同,都会认为是匹配成功。id_table数组是为了应对那些对应设备和驱动的drv->namepdev->name名字不同的情况。

再看看platform_uevent()函数:

static int platform_uevent(struct device *dev, struct kobj_uevent_env *env){struct platform_device*pdev = to_platform_device(dev);int rc;/* Some devices have extra OF data and an OF-style MODALIAS */rc = of_device_uevent_modalias(dev,env);if (rc != -ENODEV)return rc;add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,pdev->name);return 0;}

添加了MODALIAS环境变量,我们回顾一下:platform_bus. parent->kobj->kset->uevent_opsdevice_uevent_opsbus_uevent_ops的定义如下:

static struct kset_uevent_ops device_uevent_ops = {

       .filter =    dev_uevent_filter,

       .name =          dev_uevent_name,

       .uevent = dev_uevent,

};

当调用device_add()时会调用kobject_uevent(&dev->kobj, KOBJ_ADD)产生一个事件,这个函数中会调用相应的kset_uevent_opsuevent函数,这里即为dev_uevent(),我们看一下这个函数的代码片段:

static int dev_uevent(struct kset *kset, struct kobject *kobj,

                    struct kobj_uevent_env *env)

{

       .

       .

       .

       /* have the bus specific function add its stuff */

       if (dev->bus && dev->bus->uevent) {

              retval = dev->bus->uevent(dev, env);

              if (retval)

                     pr_debug("device: '%s': %s: bus uevent() returned %d\n",

                             dev_name(dev), __func__, retval);

       }

       .

       .

       .

}

从这里看到如果bus->uevent()函数存在则会调用它。

到这里我们清楚了platform_uevent会在哪里调用了。

2.Platform设备的注册

对于platform设备的初始化,内核源码也提供了platform_device_alloc()函数。

对于platform设备的初注册,内核源码提供了platform_device_add()函数,它是进行一系列的操作后调用device_add()将设备注册到相应的总线上,内核代码中platform设备的其他注册函数都是基于这个函数,如platform_device_register()platform_device_register_simple()platform_device_register_data()等。

我们对这些函数逐个分析,首先看看初始化函数platform_device_alloc()

struct platform_device *platform_device_alloc(const char *name, int id){struct platform_object *pa;pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL);if (pa) {strcpy(pa->name, name);pa->pdev.name = pa->name;pa->pdev.id = id;device_initialize(&pa->pdev.dev);pa->pdev.dev.release = platform_device_release;arch_setup_pdev_archdata(&pa->pdev);}return pa ? &pa->pdev : NULL;}

该函数首先为platform设备分配内存空间,这里的struct platform_object结构是struct platform _device结构的封装,其定义如下:

struct platform_object {

       struct platform_device pdev;

       char name[1];

};

其中第二个字段name的地址用于存放第一个字段pdevname指针上的内容,函数中的代码说明了这点:

              strcpy(pa->name, name);

              pa->pdev.name = pa->name;

接着用输入参数id初始化platform_deviceid字段,这个id是在设置代表它的kobject时会用到的,我们将在后面分析到,如果不用它,则设为-1

接着调用device_initialize()初始化platform_device内嵌的device,并设置其release函数指针。

接着我们看看platform_device_add()函数

/** * platform_device_add - add a platform device to device hierarchy * @pdev: platform device we're adding * * This is part 2 of platform_device_register(), though may be called * separately _iff_ pdev was allocated by platform_device_alloc(). */int platform_device_add(struct platform_device *pdev){ int i, ret = 0;

 if (!pdev)  return -EINVAL;

 if (!pdev->dev.parent)/*都说总线有两个链表,一个是设备链表(通过device 内嵌)一个是驱动链表(通过device_driver内嵌)这里如果pdev->dev.parent为0,说明设备链表还没有设备,因此处理办法是将platform_bus作为设备链表的开始,一直感觉platform_bus和platform_bus_type很难区分,不过在这里清楚了platform_bus是一个设备,platform_bus_type才是真正的总线*/  pdev->dev.parent = &platform_bus;

 pdev->dev.bus = &platform_bus_type;/*device 要挂接在platform_bus_type这个总线上*/    /*设置pdev->dev内嵌的kobj的name字段,它是pdev->name指向的内容加上id,如果id为-1则忽略它*/ 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);/*资源的名称赋值为pdev->dev.bus_id,如果一个platform_device有多个resource 则出现同名现象*/

  p = r->parent;  if (!p) {/*父资源为0,说明不是从一个大的资源里面切割出来的*/    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);/*资源也分配好了,准备工作也做足,终于可以把设备添加到设备链表里面了*/ 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;}

platform_device_add()函数分析完了,我们看下platform_device_register()函数: 

/** * platform_device_register - add a platform-level device * @pdev: platform device we're adding */int platform_device_register(struct platform_device *pdev){device_initialize(&pdev->dev);arch_setup_pdev_archdata(pdev);return platform_device_add(ppdev);}

初始化pdev->dev后调用platform_device_add()把它注册到platform_bus_type上。

另外一个注册函数platform_device_register_simple(),它的作用是根据传入的资源和资源数注册设备。

3.Platform驱动的注册

我们在设备驱动模型的分析中已经知道驱动在注册要调用driver_register()platform driver的注册函数platform_driver_register()同样也是进行其它的一些初始化后调用driver_register()将驱动注册到platform_bus_type总线上,看一下这个函数:

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);}

先看看struct platform_driver结构:

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;};

上面的函数指定了内嵌的driverbus字段为platform_bus_type,即为它将要注册到的总线。

然后设定了platform_driver内嵌的driverproberemoveshutdown函数。

static int platform_drv_probe(struct device *_dev) {        struct platform_driver *drv = to_platform_driver(_dev->driver);        struct platform_device *dev = to_platform_device(_dev);          return drv->probe(dev); } static int platform_drv_remove(struct device *_dev) {        struct platform_driver *drv = to_platform_driver(_dev->driver);        struct platform_device *dev = to_platform_device(_dev);          return drv->remove(dev); }   static void platform_drv_shutdown(struct device *_dev) {        struct platform_driver *drv = to_platform_driver(_dev->driver);        struct platform_device *dev = to_platform_device(_dev);          drv->shutdown(dev); }

从这三个函数的代码可以看到,又找到了相应的platform_driverplatform_device,然后调用platform_driverproberemoveshutdown函数。这是一种高明的做法:在不针对某个驱动具体的proberemoveshutdown指向的函数,而通过上三个过度函数来找到platform_driver,然后调用proberemoveshutdown接口。

到此我们了解了platform的初始化,以及设备和驱动的注册原理和接口函数。

0 0