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.
33 #include "udev_version.h"
36 #include "libsysfs/libsysfs.h"
39 * Right now the major/minor of a device is stored in a file called
41 * The number is stored as:
45 * The value is in decimal.
47 static int get_major_minor(struct sysfs_class_device *class_dev, struct udevice *udev)
53 dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev");
59 if (sscanf(dev, "%u:%u", &udev->major, &udev->minor) != 2)
62 dbg("found major = %d, minor = %d", udev->major, udev->minor);
70 * we possibly want to add some symlinks here
71 * only numeric owner/group id's are supported
73 static int create_node(struct udevice *dev)
81 strncpy(filename, udev_root, sizeof(filename));
82 strncat(filename, dev->name, sizeof(filename));
85 res = (dev->major << 8) | (dev->minor);
87 res = makedev(dev->major, dev->minor);
102 dbg("unknown node type %c\n", dev->type);
106 /* create subdirectories if requested */
107 if (strchr(dev->name, '/')) {
112 strncpy(path, filename, sizeof(path));
113 pos = strchr(path+1, '/');
115 pos = strchr(pos+1, '/');
119 if (stat(path, &stats)) {
120 retval = mkdir(path, 0755);
122 dbg("mkdir(%s) failed with error '%s'",
123 path, strerror(errno));
126 dbg("created %s", path);
132 dbg("mknod(%s, %#o, %u, %u)", filename, dev->mode, dev->major, dev->minor);
133 retval = mknod(filename, dev->mode, res);
135 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
136 filename, dev->mode, dev->major, dev->minor, strerror(errno));
140 unsigned long id = strtoul(dev->owner, &endptr, 10);
144 dbg("only numeric owner id supported: %s", dev->owner);
149 unsigned long id = strtoul(dev->group, &endptr, 10);
153 dbg("only numeric group id supported: %s", dev->group);
157 dbg("chown(%s, %u, %u)", filename, uid, gid);
158 retval = chown(filename, uid, gid);
160 dbg("chown(%s, %u, %u) failed with error '%s'", filename,
161 uid, gid, strerror(errno));
167 static struct sysfs_class_device *get_class_dev(char *device_name)
169 char dev_path[SYSFS_PATH_MAX];
170 struct sysfs_class_device *class_dev = NULL;
172 strcpy(dev_path, sysfs_path);
173 strcat(dev_path, device_name);
175 dbg("looking at %s", dev_path);
177 /* open up the sysfs class device for this thing... */
178 class_dev = sysfs_open_class_device(dev_path);
179 if (class_dev == NULL) {
180 dbg ("sysfs_open_class_device failed");
183 dbg("class_dev->name = %s", class_dev->name);
189 /* wait for the "dev" file to show up in the directory in sysfs.
190 * If it doesn't happen in about 10 seconds, give up.
192 #define SECONDS_TO_WAIT_FOR_DEV 10
193 static int sleep_for_dev(char *path)
195 char filename[SYSFS_PATH_MAX + 6];
196 int loop = SECONDS_TO_WAIT_FOR_DEV;
199 strcpy(filename, sysfs_path);
200 strcat(filename, path);
201 strcat(filename, "/dev");
206 dbg("looking for %s", filename);
207 retval = stat(filename, &buf);
211 /* sleep for a second or two to give the kernel a chance to
212 * create the dev file */
220 int udev_add_device(char *path, char *subsystem)
222 struct sysfs_class_device *class_dev = NULL;
224 int retval = -EINVAL;
226 /* for now, the block layer is the only place where block devices are */
227 if (strcmp(subsystem, "block") == 0)
232 retval = sleep_for_dev(path);
236 class_dev = get_class_dev(path);
237 if (class_dev == NULL)
240 retval = get_major_minor(class_dev, &dev);
242 dbg("get_major_minor failed");
246 retval = namedev_name_device(class_dev, &dev);
250 retval = udevdb_add_dev(path, &dev);
252 dbg("udevdb_add_dev failed, but we are going to try to create the node anyway. "
253 "But remove might not work properly for this device.");
255 dbg("name = %s", dev.name);
256 retval = create_node(&dev);
260 sysfs_close_class_device(class_dev);