android中usb设备驱动不能自动创建设备节点

调试一个usb驱动,发现在android下无法自动创建设备节点,手动创建设备节点可以正常访问硬件。后来发现是在init进程里面对一些usb设备进行了过滤。

system/core/init/devices.c 中的下面函数中。

static void handle_generic_device_event(struct uevent *uevent)
{
    char *base;
    const char *name;
    char devpath[96] = {0};
    char **links = NULL;

    name = parse_device_name(uevent, 64);
    if (!name)
        return;

    if (!strncmp(uevent->subsystem, "usb", 3)) {
         if (!strcmp(uevent->subsystem, "usb")) {
             /* This imitates the file system that would be created
              * if we were using devfs instead.
              * Minors are broken up into groups of 128, starting at "001"
              */
             int bus_id = uevent->minor / 128 + 1;
             int device_id = uevent->minor % 128 + 1;
             /* build directories */
             mkdir("/dev/bus", 0755);
             mkdir("/dev/bus/usb", 0755);
             snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
             mkdir(devpath, 0755);
             snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
         } else {
             /* ignore other USB events */
             return;
         }
     } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
         base = "/dev/graphics/";
         mkdir(base, 0755);
     } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
         base = "/dev/oncrpc/";
         mkdir(base, 0755);
     } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
         base = "/dev/adsp/";
         mkdir(base, 0755);
     } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
         base = "/dev/msm_camera/";
         mkdir(base, 0755);
     } else if(!strncmp(uevent->subsystem, "input", 5)) {
         base = "/dev/input/";
         mkdir(base, 0755);
     } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
         base = "/dev/mtd/";
         mkdir(base, 0755);
     } else if(!strncmp(uevent->subsystem, "sound", 5)) {
         base = "/dev/snd/";
         mkdir(base, 0755);
     } else if(!strncmp(uevent->subsystem, "misc", 4) &&
                 !strncmp(name, "log_", 4)) {
         base = "/dev/log/";
         mkdir(base, 0755);
         name += 4;
     } else
         base = "/dev/";
     links = get_character_device_symlinks(uevent);

     if (!devpath[0])
         snprintf(devpath, sizeof(devpath), "%s%s", base, name);

     handle_device(uevent->action, devpath, uevent->path, 0,
             uevent->major, uevent->minor, links);
}
若要自动生成,修改修改相应代码即可。

比如我要在/dev下生成名为testdev的设备节点,那么我需要添加下面的代码:

if(!strncmp(name, "testdev", 7))
    base = "/dev/";


你可能感兴趣的:(android中usb设备驱动不能自动创建设备节点)