linux 起步走 platform_driver_register

linux 起步走 platform_driver_registerstaticstructplatform_drivermxc_v4l2_driver={.driver={.name=“mxc_v4l2_capture”,.owner=THIS_MODULE,.of_match_table=mxc_v4l2_dt_ids,}

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

static struct platform_driver mxc_v4l2_driver = {
.driver = {
.name = “mxc_v4l2_capture”,
.owner = THIS_MODULE,
.of_match_table = mxc_v4l2_dt_ids,
},
.id_table = imx_v4l2_devtype,
.probe = mxc_v4l2_probe,
.remove = mxc_v4l2_remove,
.suspend = mxc_v4l2_suspend,
.resume = mxc_v4l2_resume,
.shutdown = NULL,
};

int platform_driver_register(struct platform_driver *drv)
{
    drv->driver.bus = &platform_bus_type; //这个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);
}

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

    if (ACPI_HANDLE(_dev))
        acpi_dev_pm_attach(_dev, true);

    ret = drv->probe(dev);
    if (ret && ACPI_HANDLE(_dev))
        acpi_dev_pm_detach(_dev, true);

    return ret;
}
#define to_platform_driver(drv) (container_of((drv), struct platform_driver, driver))
//这里platform_drv_probe调用时就会调用drv->probe(dev)也就是mxc_v4l2_probe,其它几个类似
struct bus_type platform_bus_type = {
    .name       = "platform",
    .dev_attrs  = platform_dev_attrs,
    .match      = platform_match,
    .uevent     = platform_uevent,
    .pm     = &platform_dev_pm_ops,
};
//继续往下看return driver_register(&drv->driver);
int driver_register(struct device_driver *drv)
{
    int ret;
    struct device_driver *other;

    BUG_ON(!drv->bus->p);  //这里的bus就是platform_bus_type,这个也有初始化

    if ((drv->bus->probe && drv->probe) ||
        (drv->bus->remove && drv->remove) ||
        (drv->bus->shutdown && drv->shutdown))
        printk(KERN_WARNING "Driver '%s' needs updating - please use "
            "bus_type methods\n", drv->name);

    other = driver_find(drv->name, drv->bus); //这里肯定是一个逆过程,跳过往下看怎么注册的
    if (other) {
        printk(KERN_ERR "Error: Driver '%s' is already registered, "
            "aborting...\n", drv->name);
        return -EBUSY;
    }

    ret = bus_add_driver(drv);
    if (ret)
        return ret;
    ret = driver_add_groups(drv, drv->groups);
    if (ret) {
        bus_remove_driver(drv);
        return ret;
    }
    kobject_uevent(&drv->p->kobj, KOBJ_ADD);

    return ret;
}
int bus_register(struct bus_type *bus)// platform_bus_init会调用这个初始化platform_bus_type
{
    int retval;
    struct subsys_private *priv;
    struct lock_class_key *key = &bus->lock_key;

    priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
    if (!priv)
        return -ENOMEM;

    priv->bus = bus;
    bus->p = priv;

    BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);

    retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);//name = "platform"
    if (retval)
        goto out;

    priv->subsys.kobj.kset = bus_kset;
    priv->subsys.kobj.ktype = &bus_ktype;
    priv->drivers_autoprobe = 1;

    retval = kset_register(&priv->subsys);
    if (retval)
        goto out;

    retval = bus_create_file(bus, &bus_attr_uevent);
    if (retval)
        goto bus_uevent_fail;

    priv->devices_kset = kset_create_and_add("devices", NULL,
                         &priv->subsys.kobj);
    if (!priv->devices_kset) {
        retval = -ENOMEM;
        goto bus_devices_fail;
    }

    priv->drivers_kset = kset_create_and_add("drivers", NULL,
                         &priv->subsys.kobj);
    if (!priv->drivers_kset) {
        retval = -ENOMEM;
        goto bus_drivers_fail;
    }

    INIT_LIST_HEAD(&priv->interfaces);
    __mutex_init(&priv->mutex, "subsys mutex", key);
    klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
    klist_init(&priv->klist_drivers, NULL, NULL);

    retval = add_probe_files(bus);
    if (retval)
        goto bus_probe_files_fail;

    retval = bus_add_attrs(bus);
    if (retval)
        goto bus_attrs_fail;

    pr_debug("bus: '%s': registered\n", bus->name);
    return 0;

bus_attrs_fail:
    remove_probe_files(bus);
bus_probe_files_fail:
    kset_unregister(bus->p->drivers_kset);
bus_drivers_fail:
    kset_unregister(bus->p->devices_kset);
bus_devices_fail:
    bus_remove_file(bus, &bus_attr_uevent);
bus_uevent_fail:
    kset_unregister(&bus->p->subsys);
out:
    kfree(bus->p);
    bus->p = NULL;
    return retval;
}
int bus_add_driver(struct device_driver *drv)
{
    struct bus_type *bus;
    struct driver_private *priv;
    int error = 0;

    bus = bus_get(drv->bus);
    if (!bus)
        return -EINVAL;

    pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);

    priv = kzalloc(sizeof(*priv), GFP_KERNEL);
    if (!priv) {
        error = -ENOMEM;
        goto out_put_bus;
    }
    klist_init(&priv->klist_devices, NULL, NULL);
    priv->driver = drv;
    drv->p = priv;
    priv->kobj.kset = bus->p->drivers_kset;
    error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
                     "%s", drv->name);
    if (error)
        goto out_unregister;

    klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
    if (drv->bus->p->drivers_autoprobe) {
        error = driver_attach(drv);
        if (error)
            goto out_unregister;
    }
    module_add_driver(drv->owner, drv);

    error = driver_create_file(drv, &driver_attr_uevent);
    if (error) {
        printk(KERN_ERR "%s: uevent attr (%s) failed\n",
            __func__, drv->name);
    }
    error = driver_add_attrs(bus, drv);
    if (error) {
        /* How the hell do we get out of this pickle? Give up */
        printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
            __func__, drv->name);
    }

    if (!drv->suppress_bind_attrs) {
        error = add_bind_files(drv);
        if (error) {
            /* Ditto */
            printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
                __func__, drv->name);
        }
    }

    return 0;

out_unregister:
    kobject_put(&priv->kobj);
    kfree(drv->p);
    drv->p = NULL;
out_put_bus:
    bus_put(bus);
    return error;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

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

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

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

(0)


相关推荐

  • js数组去重的10种方法

    js数组去重的10种方法Methods1:思路:定义一个新数组,并存放原数组的第一个元素,然后将元素组一一和新数组的元素对比,若不同则存放在新数组中。functionunique(arr){letnewArr=[arr[0]];for(leti=1;i<arr.length;i++){…

  • busybox 安装mysql_busybox怎么安装

    busybox 安装mysql_busybox怎么安装把”busybox-armv6l”重命名为”busybox”;将busybox传入手机的SD卡,打开terminal(Linux,Mac)或cmd(Windows)adbpush~/Desktop/busybox/mnt/sdcard其中的~/Desktop请根据自己的情况替换成正确的路径输入以下命令,为了在/system目录写入文件adbshellsumount-oremount,r…

  • export_symbol的使用

    export_symbol的使用       问题是这样的,前几天师妹需要在内核模块中使用tasklist_lock来操作保护内核进程链表,由于tasklist_lock如果要在模块中使用就必须把它填充到内核符号表里面,就要使用export_symbol,她在sched.h里面就是tasklist_lock声明的地方,添加export_symbol(tasklist_lock),在编译的时候总是出现undefinedsymb

  • 40 道基础Dubbo 面试题及答案

    40 道基础Dubbo 面试题及答案转载自史上最全40道Dubbo面试题及答案,看完碾压面试官想往高处走,怎么能不懂Dubbo?Dubbo是国内最出名的分布式服务框架,也是Java程序员必备的必会的框架之一。Dubbo更是中高级面试过程中经常会问的技术,无论你是否用过,你都必须熟悉。下面我为大家准备了一些Dubbo常见的的面试题,一些是我经常问别人的,一些是我过去面试遇到的一些问题,总结给大家,希…

  • 分布式事务saga开源实现_spring分布式事务解决方案

    分布式事务saga开源实现_spring分布式事务解决方案Saga模式是一种分布式异步事务,一种最终一致性事务,是一种柔性事务。Saga事务模型又叫做长时间运行的事务(Long-running-transaction),它是由普林斯顿大学的H.Garcia-Molina等人提出,它描述的是另外一种在没有两阶段提交的的情况下解决分布式系统中复杂的业务事务问题。Saga的组成每个Saga由一系列sub-transactionTi组成每个T…

  • python爱心代码制作_python如何编出一个爱心文字

    python爱心代码制作_python如何编出一个爱心文字[M][fts=2][ftc=F7941D]╭╮╭╮[/ft][ftc=F7941D]╭╮╭╮[/ft]\[ftc=FFF100]∨[/ft]/\[ftc=FFF100]∨[/ft]/([ftc=00BFF3]≥[/ft]﹏[ftc=00BFF3]≤[/ft])([ftc=EF6EA8]≥[/ft]﹏[ftc=EF6EA8]≤[/ft])/[ftc=…

    2022年10月18日

发表回复

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

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