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)


相关推荐

  • Hadoop-Shuffle洗牌过程,与combine和partition的关系「建议收藏」

    Hadoop-Shuffle洗牌过程,与combine和partition的关系「建议收藏」Hadoop-Shuffle洗牌过程,与combine和partition的关系

  • java switch是什么意思_java switch语句详解[通俗易懂]

    java switch是什么意思_java switch语句详解[通俗易懂]switch语句的格式:switch(整型或字符型变量){case变量可能值1:分支一;break;case变量可能值2:分支二;break;case变量可能值3:分支三;break;…default:最后分支;}在switch的语法里,我们要学到4个关键字:switch、case、break、default。在 switch(变量) 这一行里,变量只能是整型…

  • Zookeepers_docker workdir

    Zookeepers_docker workdir文章目录Curator客户端创建会话创建节点获取节点和数据更新数据删除节点事务节点存在事件监听其他工具类开发测试Curator客户端Curator包含了几个包:curator-framework:对zookeeper的底层api的一些封装curator-client:提供一些客户端的操作,例如重试策略等curator-recipes:封装了一些高级特性,如:Cache事件监听、选举…

    2022年10月24日
  • cas 原理分析

    cas 原理分析CAS原理分析1、了解java中锁的类型1.1悲观锁(PessimisticLock)顾名思义,就是很悲观,假定会发生并发冲突,屏蔽一切可能违反数据完整性的操作,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁。1.2乐观锁(OptimisticLock)顾名思义,就是很乐观,假设不会发生并发冲突,只在提交操作时检查是否违反数据完整性。乐观锁不能解决脏读的问题。每次拿数据的时候都认为别人不会修改,所以不会上锁,但是在更新的时候会判断一下在此期间别人有没有去更新这个数据,

    2022年10月16日
  • 说明子网和子网掩码的概念_子网掩码和子网地址

    说明子网和子网掩码的概念_子网掩码和子网地址一个IP(v4)地址如:210.52.207.2,是一个4字节(共32bit)的数字,被分为4段,每段8位,段与段之间用‘.’分隔。每段所能表示的十进制数最大不超过255。IP

发表回复

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

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