Binder Driver 2: 初始化

2018-06-27  本文已影响0人  ColdWave
struct binder_context {  // 描述 ServiceManager 的 Server
    struct binder_node *binder_context_mgr_node;
    struct mutex context_mgr_node_lock;

    kuid_t binder_context_mgr_uid;
    const char *name;
};

struct binder_device {
    struct hlist_node hlist;
    struct miscdevice miscdev;
    struct binder_context context;
};

static char *binder_devices_param = CONFIG_ANDROID_BINDER_DEVICES;
module_param_named(devices, binder_devices_param, charp, S_IRUGO);

static int __init binder_init(void)
{
    int ret;
    char *device_name, *device_names;
    struct binder_device *device;

    // 由于 Android O 创建了 3 个不同的 Binder 设备,分别是 /dev/binder, /dev/vndbinder, /dev/hwbinder ,分别负责不同的功能。
    device_names = kzalloc(strlen(binder_devices_param) + 1, GFP_KERNEL);
    strcpy(device_names, binder_devices_param);

    while ((device_name = strsep(&device_names, ","))) {
        ret = init_binder_device(device_name);
    }

    return ret;
}
static int __init init_binder_device(const char *name)
{
    int ret;
    struct binder_device *binder_device;

    binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);

    binder_device->miscdev.fops = &binder_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;
    mutex_init(&binder_device->context.context_mgr_node_lock);

    ret = misc_register(&binder_device->miscdev);  // Binder 设备注册为 混杂 设备

    hlist_add_head(&binder_device->hlist, &binder_devices);

    return ret;
}
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,
};
上一篇下一篇

猜你喜欢

热点阅读