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"
35 #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 decimal.
48 static int get_major_minor(struct sysfs_class_device *class_dev, int *major, int *minor)
54 dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev");
60 if (sscanf(dev, "%u:%u", major, minor) != 2)
63 dbg("found major = %d, minor = %d", *major, *minor);
71 * We also want to add some permissions here, and possibly some symlinks
73 static int create_node(char *name, char type, int major, int minor, mode_t mode)
77 strncpy(filename, UDEV_ROOT, sizeof(filename));
78 strncat(filename, name, sizeof(filename));
91 dbg("unknown node type %c\n", type);
95 dbg("mknod(%s, %#o, %u, %u)", filename, mode, major, minor);
96 retval = mknod(filename, mode, makedev(major, minor));
98 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
99 filename, mode, major, minor, strerror(errno));
103 static struct sysfs_class_device *get_class_dev(char *device_name)
105 char dev_path[SYSFS_PATH_MAX];
106 struct sysfs_class_device *class_dev = NULL;
108 strcpy(dev_path, sysfs_path);
109 strcat(dev_path, device_name);
111 dbg("looking at %s", dev_path);
113 /* open up the sysfs class device for this thing... */
114 class_dev = sysfs_open_class_device(dev_path);
115 if (class_dev == NULL) {
116 dbg ("sysfs_open_class_device failed");
119 dbg("class_dev->name = %s", class_dev->name);
125 /* wait for the "dev" file to show up in the directory in sysfs.
126 * If it doesn't happen in about 10 seconds, give up.
128 #define SECONDS_TO_WAIT_FOR_DEV 10
129 int sleep_for_dev(char *device)
131 char filename[SYSFS_PATH_MAX + 6];
134 int retval = -ENODEV;
136 strcpy(filename, sysfs_path);
137 strcat(filename, device);
138 strcat(filename, "/dev");
140 while (loop < SECONDS_TO_WAIT_FOR_DEV) {
141 dbg("looking for %s", filename);
142 retval = stat(filename, &buf);
148 /* sleep for a second or two to give the kernel a chance to
149 * create the dev file */
157 int udev_add_device(char *device, char *subsystem)
159 struct sysfs_class_device *class_dev;
160 struct device_attr attr;
164 int retval = -EINVAL;
166 /* for now, the block layer is the only place where block devices are */
167 if (strcmp(subsystem, "block") == 0)
172 retval = sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX);
173 dbg("sysfs_path = %s", sysfs_path);
175 dbg("sysfs_get_mnt_path failed");
179 retval = sleep_for_dev(device);
183 class_dev = get_class_dev(device);
184 if (class_dev == NULL)
187 retval = namedev_name_device(class_dev, &attr);
191 retval = get_major_minor(class_dev, &major, &minor);
193 dbg("get_major_minor failed");
197 retval = udevdb_add_device(device, class_dev, attr.name, type, major, minor, attr.mode);
200 dbg("udevdb_add_device failed, but we are going to try to create the node anyway. "
201 "But remove might not work properly for this device.");
203 sysfs_close_class_device(class_dev);
205 return create_node(attr.name, type, major, minor, attr.mode);