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(struct udevice *dev)
78 strncpy(filename, UDEV_ROOT, sizeof(filename));
79 strncat(filename, dev->name, sizeof(filename));
93 dbg("unknown node type %c\n", dev->type);
97 dbg("mknod(%s, %#o, %u, %u)", filename, dev->mode, dev->major, dev->minor);
98 retval = mknod(filename, dev->mode, makedev(dev->major, dev->minor));
100 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
101 filename, dev->mode, dev->major, dev->minor, strerror(errno));
103 // FIXME set the ownership of the node
107 static struct sysfs_class_device *get_class_dev(char *device_name)
109 char dev_path[SYSFS_PATH_MAX];
110 struct sysfs_class_device *class_dev = NULL;
112 strcpy(dev_path, sysfs_path);
113 strcat(dev_path, device_name);
115 dbg("looking at %s", dev_path);
117 /* open up the sysfs class device for this thing... */
118 class_dev = sysfs_open_class_device(dev_path);
119 if (class_dev == NULL) {
120 dbg ("sysfs_open_class_device failed");
123 dbg("class_dev->name = %s", class_dev->name);
129 /* wait for the "dev" file to show up in the directory in sysfs.
130 * If it doesn't happen in about 10 seconds, give up.
132 #define SECONDS_TO_WAIT_FOR_DEV 10
133 int sleep_for_dev(char *path)
135 char filename[SYSFS_PATH_MAX + 6];
138 int retval = -ENODEV;
140 strcpy(filename, sysfs_path);
141 strcat(filename, path);
142 strcat(filename, "/dev");
144 while (loop < SECONDS_TO_WAIT_FOR_DEV) {
145 dbg("looking for %s", filename);
146 retval = stat(filename, &buf);
152 /* sleep for a second or two to give the kernel a chance to
153 * create the dev file */
161 int udev_add_device(char *path, char *subsystem)
163 struct sysfs_class_device *class_dev;
165 struct device_attr attr;
166 int retval = -EINVAL;
168 /* for now, the block layer is the only place where block devices are */
169 if (strcmp(subsystem, "block") == 0)
174 retval = sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX);
175 dbg("sysfs_path = %s", sysfs_path);
177 dbg("sysfs_get_mnt_path failed");
181 retval = sleep_for_dev(path);
185 class_dev = get_class_dev(path);
186 if (class_dev == NULL)
189 retval = namedev_name_device(class_dev, &attr);
193 retval = get_major_minor(class_dev, &dev.major, &dev.minor);
195 dbg("get_major_minor failed");
199 strcpy(dev.name, attr.name);
200 strcpy(dev.owner, attr.owner);
201 strcpy(dev.group, attr.group);
202 dev.mode = attr.mode;
204 retval = udevdb_add_dev(path, &dev);
206 dbg("udevdb_add_dev failed, but we are going to try to create the node anyway. "
207 "But remove might not work properly for this device.");
209 sysfs_close_class_device(class_dev);
211 dbg("name = %s", dev.name);
212 retval = create_node(&dev);