6 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation version 2 of the License.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 675 Mass Ave, Cambridge, MA 02139, USA.
32 #include "udev_version.h"
34 #include "libsysfs/libsysfs.h"
37 static char sysfs_path[SYSFS_PATH_MAX];
40 * Right now the major/minor of a device is stored in a file called
42 * The number is stored as:
46 * The value is in hex.
47 * Yes, this will probably change when we go to a bigger major/minor
48 * range, and will have to be changed at that time.
50 static int get_major_minor(struct sysfs_class_device *class_dev, int *major, int *minor)
56 dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev");
62 if (sscanf(dev, "%u:%u", major, minor) != 2)
65 dbg("found major = %d, minor = %d", *major, *minor);
73 * We also want to add some permissions here, and possibly some symlinks
75 static int create_node(char *name, char type, int major, int minor, int mode)
79 strncpy(filename, UDEV_ROOT, sizeof(filename));
80 strncat(filename, name, sizeof(filename));
93 dbg("unknown node type %c\n", type);
97 retval = mknod(filename,mode,makedev(major,minor));
99 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
100 filename, mode, major, minor, strerror(errno));
104 struct sysfs_class_device *get_class_dev(char *device_name)
106 char dev_path[SYSFS_PATH_MAX];
107 struct sysfs_class_device *class_dev;
109 strcpy(dev_path, sysfs_path);
110 strcat(dev_path, device_name);
112 dbg("looking at %s", dev_path);
114 /* open up the sysfs class device for this thing... */
115 class_dev = sysfs_open_class_device(dev_path);
116 if (class_dev == NULL) {
117 dbg ("sysfs_open_class_device failed");
120 dbg("class_dev->name = %s", class_dev->name);
125 static int udev_init(void)
129 retval = sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX);
130 dbg("sysfs_path = %s", sysfs_path);
134 int udev_add_device(char *device, char *subsystem)
136 struct sysfs_class_device *class_dev;
137 struct device_attr attr;
143 int retval = -EINVAL;
145 /* sleep for a second or two to give the kernel a chance to
146 * create the dev file
152 /* for now, the block layer is the only place where block devices are */
153 if (strcmp(subsystem, "block") == 0)
158 class_dev = get_class_dev(device);
159 if (class_dev == NULL)
162 retval = namedev_name_device(class_dev, &attr);
166 retval = get_major_minor(class_dev, &major, &minor);
168 dbg ("get_major_minor failed");
172 sysfs_close_class_device(class_dev);
174 return create_node(attr.name, type, major, minor, attr.mode);