您现在的位置是:首页 > 正文

平台设备与平台驱动注册过程platform_driver_register与platform_device_register

2024-01-30 21:57:36阅读 0

对于一个需要添加的设备与驱动而言,例如led平台设备与led平台驱动,需要先注册平台设备,然后再注册平台驱动。原因是注册平台驱动时,需要遍历相应总线上的设备名称来匹配内核中所有已经注册过的设备名。

1、platform_device_regisger

/**
 * 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(pdev);
}
/**
 * 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)
        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);
    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;
}
/**
 * device_add - add device to device hierarchy.
 * @dev: device.
 *
 * This is part 2 of device_register(), though may be called
 * separately _iff_ device_initialize() has been called separately.
 *
 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
 * to the global and sibling lists for the device, then
 * adds it to the other relevant subsystems of the driver model.
 *
 * Do not call this routine or device_register() more than once for
 * any device structure.  The driver model core is not designed to work
 * with devices that get unregistered and then spring back to life.
 * (Among other things, it's very hard to guarantee that all references
 * to the previous incarnation of @dev have been dropped.)  Allocate
 * and register a fresh new struct device instead.
 *
 * NOTE: _Never_ directly free @dev after calling this function, even
 * if it returned an error! Always use put_device() to give up your
 * reference instead.
 */
int device_add(struct device *dev)
{
    struct device *parent = NULL;
    struct kobject *kobj;
    struct class_interface *class_intf;
    in

网站文章

  • 2020-12-28 pip出现Fatal error in launcher错误

    pip出现Fatal error in launcher错误原因:移动了python的安装路径解决方式:1. 新增一个叫做pip-script.py的文件在Python目录下的Scripts里面2. ...

    2024-01-30 21:57:06
  • python---字典元素的遍历

    注意通过这边遍历获得的只是键key,而不是键值对scores={'zs':59,'ls':69,'ww':79}for key in scores: print(key,scores[key])zs 59ls 69ww 79

    2024-01-30 21:57:01
  • 图片上传(VUE ,element)

    一、确定图片上传接口 const UPLOADURL=&#39;http://localhost:7001/admin/upload&#39;; 根据要求写,这种只是展示,不规范,正常情况下只出现一次端口号,其余均是拼接。 二、页面引入

    2024-01-30 21:56:55
  • Netty之DefaultChannelPipeline源码分析

    Netty之DefaultChannelPipeline源码分析

    DefaultChannelPipeline类在初始化时,会创建两个ChannelHandlerContext实例,一个为head,一个为tail。Head为头部,Tail为尾部。其实给Channel...

    2024-01-30 21:56:21
  • hmail 邮箱服务器 从安装到使用

    hmail 邮箱服务器 从安装到使用

    hmail 邮箱服务器 从安装到使用1.下载hmail 下载地址:https://www.hmailserver.com/download2.安装安装期间可能要安装.net环境,选择安装就好设置管理员...

    2024-01-30 21:56:15
  • Codeforces Round 875 (Div. 2)(A~D)

    qwq

    2024-01-30 21:56:08
  • MySQL主从复制 最新发布

    MySQL主从复制 最新发布

    主从复制(也称AB复制)允许将来自一个MySQL数据库服务器(主服务器)的数据复制到一个或多个MySQL数据库服务器(从服务器)。根据配置,自己可以复制数据库中的所有数据库,所选数据库甚至选定的表。

    2024-01-30 21:55:37
  • Unit Test

    Unit Test

    关于单元测试的一篇博文

    2024-01-30 21:55:29
  • Controller响应界面跳转的几种方式 热门推荐

    前面已经了解了Controller的几种配置方式 今天主要写一下响应界面跳转的几种方式 1.在注解的方式中 1.1通过HttpServletResponse的API直接输出(不需要配置渲染器) controller类的主要代码 @Controller public class RequestController{ @RequestMapping(&quot;/resp&quot;) p

    2024-01-30 21:55:20
  • web:[SUCTF 2019]EasySQL

    web:[SUCTF 2019]EasySQL

    将”||”视为字符串的连接操作符而非或运算符,这和Oracle数据库是一样的,也和字符串的拼接函数Concat相类似.SELECT * 和 SELECT 所有列,两者差别几乎可忽略。所以查询所有字段(...

    2024-01-30 21:54:52