Binder驱动的注册流程

Binder驱动采用的是misc机制进行注册的,比较简单,核心代码复制在这里。

device_initcall(binder_init);
static int __init binder_init(void)
{
    int ret;
    char *device_name, *device_names, *device_tmp;
    struct binder_device *device;
    struct hlist_node *tmp;
...
    device_tmp = device_names;
    while ((device_name = strsep(&device_tmp, ","))) {
        ret = init_binder_device(device_name);
        if (ret)
            goto err_init_binder_device_failed;
    }
...
err_init_binder_device_failed:
    hlist_for_each_entry_safe(device, tmp, &binder_devices, hlist) {
        misc_deregister(&device->miscdev);
        hlist_del(&device->hlist);
        kfree(device);
    }
...
}
static int __init init_binder_device(const char *name)
{
    int ret;
    struct binder_device *binder_device;
...
    binder_device->miscdev.fops = &binder_fops;
    binder_device->miscdev.minor = MISC_DYNAMIC_MINOR;
    binder_device->miscdev.name = name;

    binder_device->context.binder_context_mgr_uid = INVALID_UID;
    binder_device->context.name = name;
...
    ret = misc_register(&binder_device->miscdev);
...

    return ret;
}
struct binder_device {
    struct hlist_node hlist;
    struct miscdevice miscdev;
    struct binder_context context;
};
static const struct file_operations binder_fops = {
    .owner = THIS_MODULE,
    .poll = binder_poll,
    .unlocked_ioctl = binder_ioctl,
    .compat_ioctl = binder_ioctl,
    .mmap = binder_mmap,
    .open = binder_open,
    .flush = binder_flush,
    .release = binder_release,
};

Android系统的驱动统一被调用device_initcall进行初始化,device_initcall实际上也调用了module_init函数。

#define device_initcall(fn)     module_init(fn)

device_initcall调用了binder_init函数,binder_init函数又调用了init_binder_device,在init_binder_device中使用misc_register进行了注册,miscdevice存放于binder_device结构体中,miscdevice所关联的file_operations对象为binder_fops。
对驱动感兴趣的可以看下驱动相关的几篇文章。

你可能感兴趣的:(Binder驱动的注册流程)