Device 与 platform device的不同(一)[通俗易懂]

Device 与 platform device的不同(一)[通俗易懂]Device与platformdevice在注册方法上有所不同。Device注册有两步,platformdevice注册也有两步,它们第一步相同,都是initialize设备,但第二步有所不同,Device是直接调用device_add()函数来add设备,而platformdevice则调用platform_device_add()函数。实际上,platfo

大家好,又见面了,我是你们的朋友全栈君。

Device 与 platform device 在注册方法上有所不同。

Device 注册有两步,platform device注册也有两步,它们第一步相同,都是initialize设备,

但第二步有所不同,Device是直接调用device_add()函数来add设备,而platform device则调用platform_device_add()函数。

实际上,platform_device_add()里面也嵌套了device_add()函数(红色部分),不过在device_add()被调用之前,platform device要先注册它的resources。

就是下面蓝色的部分。

 

 >>>>>>>>>>>>>>>>>>>> device的注册 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

 void device_register(struct device *dev)

{

device_initialize(dev);

return device_add(dev);

}

>>>>>>>>>>>>>>>>>>>>>>device_register的第一步, 初始化device>>>>>>>>>>>>>>>>>>>>

void device_initialize(struct device *dev)

{

     dev->kobj.kset = device_kset;

     kboject_init(&dev->kobj, &device_ktype);

     INIT_LIST_HEAD(&dev->dma_pools);

     mutex_init(&dev->mutex);

     lockdep_set_novalidate_class(&dev->mutex);

     spin_lock_init(&dev->devres_lock);

     INIT_LIST_HEAD(&dev->devres_head);

     device_pm_init(dev);

     set_dev_node(dev, -1);

}

>>>>>>>>备注: struct device_private *p;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

struct device_private {             //hold the private to the driver core portions of the device structure

      struct klist klist_children;             //containing all children of this device

      struct klist_node knode_parent;   //node in sibling list

      struct klist_node knode_driver;   //node in driver list

      struct klist_node knode_bus;    //node in bus list

      void *driver_data;       //private pointer for driver specific info.

      struct device *device;        //pointer back to the struct class that this structure is associated with

};  //  Nothing outside of the driver core should ever touch these fields.

 

>>>>>>>>>>>>>>>>> device_register的第二步, 添加device>>>>>>>>>>>>>>>>>>>>>>>>> 

int device_add(struct device *dev)

{

    struct device *parent =NULL;

    struct class_interface *class_intf;

    int error = -EINVAL;

     dev =get_device(dev);

     if(!dev)

             goto done;

    if(!dev->p){

            error = device_private_init(dev);

            if(error)

                    goto done;

    }

    /* for statically allocated devices, which should all be converted some day, we need to initialize the name. We prevent reading back the name, and force the use of     dev_name() */

   if(dev->init_name){

         dev_set_name(dev, “%s”, dev->init_name);

         dev->init_name =NULL;

   }

    if(!dev_name(dev)){

         error = – EINVAL;

         goto name_error;

     }

     pr_debug(“device: ‘%s’ : %s\n”, dev_name(dev), __func__);

     parent = getdevce(dev->parent);

     setup_parent(dev, parent);

     /*use paent numa_node*/

     if(parent)

               set_dev_node(dev, dev_to_node(parent));

     //first, register with generic layer, we require the name to be set before, and pass NULL

     error = kboject_add(&dev->kobj, dev->kobj.parent, NULL);

     if(error)

               goto Error;

     //notify platform of device entry

     if(platform_notify)

                 platform_notify(dev);

     erro = device_create_file(dev, &uevent_attr);

     if(error)

               goto attrError;

      if(MAJOR(dev->devt)){

              error = device_create_file(dev, &devt_attr);

              if(error)

                         goto ueventattrError;

              error = device_create_sys_dev_entry(dev);

              if(error)

                        goto devtattrError;

             devtmpfs_create_node(dev);

     }

     error = device_add_class_symlinks(dev);

     if(error)

             goto SymlinkError;

     error = device_add_attrs(dev);

     if(error)

            goto AttrsError;

     error = bus_add_device(dev);

     if(error)

            goto BusError;

     error = dpm_sysfs_add(dev);

      if(error)

            goto DPMError;

      device_pm_add(dev);

     / /Notify clients of device addtion. This call must come after dpm_sysf_add() and before kobject_uevent().

    if(dev->bus)

           blocking_notifier_call_chain(&dev->bus->p->bus_notifier, BUS_NOTIFY_ADD_DEVICE, dev);

    kobject_uevent(&dev->kobj, KOBJ_ADD);

    bus_probe_device(dev);

    if(parent)

              klist_add_tail(&dev->p->knode_parent, &parent->p->klist_children);

    if(dev->class){

              mutex_lock(&dev->class->p->class_mutex);

              //tie the class to the device

             klist_add_tail(&dev->knode_class, &dev->class->p->class_devices);

             list_for_each_entry(class_intf, &dev->class->p->class_interfaces, node);

             if(class_intf->add_dev)

                     class_intf->add_dev(dev, class_intf);

            mutex_unlock(&dev->class->class_mutex);

     }

 

done:

          put_device(dev);

          return error;

DPMError:

          bus_remove_device(dev);

BusError:

          device_remove_attrs(dev);

AttrsError:

          device_remove_class_syslinks(dev);

SymlinkError:

          if(MAJOR(dev->devt))

                        devtmpfs_delete_node(dev);

          if(MAJOR(dev->devt))

                        device_remover_file(dev, &devt_attr);

ueventattrError:

          device_remove_file(dev, &uevent_attr);

attrError:

          kobject_uevent(&dev->kobj, KOBJ_REMOVE);

          kobject_del(&dev->kobj);

Error:

          cleanup_device_parent(dev);

          if(parent)

                 put_device(parent);

name_error:

           kfree(dev->p);

           dev-> = NULL;

           goto done;

}

>>>>>>>>>>>>>>> platform_device 的注册>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

// 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);          //第一步与device完全相同

             return platform_device_add(pdev);

}

 

>>>>>>>>>>>>>>>>>>>platform_device_add()>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

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 = -; i < pdev->num_resources; i++){

                 struct resource *p, *r = &pdev->resource[i];

                 if( r->name == NULL )

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

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

       if(ret == 0)

                  return ret;

failed:

       while( –i>=0){

                 struct resource *r = &pdev->resource[i];

                 unsigned long type = resouce_type(r);

                 if(type==IORESOURCE_MEM)||type==IORESOURCE_IO)

                              release_resource(r);

        }

        return ret;

}

}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/163792.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • Feign原理_feign基于什么协议

    Feign原理_feign基于什么协议一、feign测试代码1.服务接口上标注@FeignClient@FeignClient(“order”)publicinterfaceOrderServiceFeign{@GetMapping(“/getOrder”)StringgetOrder();}2.调用方可以直接注入然后直接调用访问@RestController@RequestMapping(“/feign”)publicclassFeignTestController{@Autow

  • sstream相关用法

    sstream相关用法 ①将数字转换为字符串       ②字符串转换为数字输入1234,输出1234输入123456分别输出12,34,56③把一行字符串放入流中,单词以空格隔开。之后把一个个单词从流中依次读取到字符串…

  • 使用Xib封装一个view的步骤

    使用Xib封装一个view的步骤

  • mysql autoconf_autoconf手册(一)

    mysql autoconf_autoconf手册(一)AutoconfCreatingAutomaticConfigurationScriptsEdition2.13,forAutoconfversion2.13December1998byDavidMacKenzieandBenElliston—————————————————————–…

  • 51单片机的毕业设计题目_51单片机新颖毕业设计题目

    51单片机的毕业设计题目_51单片机新颖毕业设计题目以下是学长亲手整理的C51单片机相关的毕业设计选题,都是经过学长精心审核的题目,适合作为毕设,难度不高,工作量达标,对毕设有任何疑问都可以问学长哦!相对容易工作量达标题目新颖,含创新点httpshttpshttpshttpshttpshttps。…

  • MYSQLServer2008R2详细的图文安装教程

    MYSQLServer2008R2详细的图文安装教程本文主要总结了在win7系统上安装Mysql_Server2008(64位)的详细图文教程,具体的安装步骤如下所示:MysqlServer2008R2企业版下载地址:(直接打开链接,默认用的是迅雷下载,里面自带序列号)ed2k://|file|cn_sql_server_2008_r2_enterprise_x86_x64_ia64_dvd_522233.iso|4662884352|1D…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号