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"
38 * Right now the major/minor of a device is stored in a file called
40 * The number is stored as:
44 * The value is in decimal.
46 static int get_major_minor(struct sysfs_class_device *class_dev, int *major, int *minor)
52 dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev");
58 if (sscanf(dev, "%u:%u", major, minor) != 2)
61 dbg("found major = %d, minor = %d", *major, *minor);
69 * We also want to add some permissions here, and possibly some symlinks
71 static int create_node(struct udevice *dev)
76 strncpy(filename, udev_root, sizeof(filename));
77 strncat(filename, dev->name, sizeof(filename));
91 dbg("unknown node type %c\n", dev->type);
95 dbg("mknod(%s, %#o, %u, %u)", filename, dev->mode, dev->major, dev->minor);
96 retval = mknod(filename, dev->mode, makedev(dev->major, dev->minor));
98 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
99 filename, dev->mode, dev->major, dev->minor, strerror(errno));
101 // FIXME set the ownership of the node
105 static struct sysfs_class_device *get_class_dev(char *device_name)
107 char dev_path[SYSFS_PATH_MAX];
108 struct sysfs_class_device *class_dev = NULL;
110 strcpy(dev_path, sysfs_path);
111 strcat(dev_path, device_name);
113 dbg("looking at %s", dev_path);
115 /* open up the sysfs class device for this thing... */
116 class_dev = sysfs_open_class_device(dev_path);
117 if (class_dev == NULL) {
118 dbg ("sysfs_open_class_device failed");
121 dbg("class_dev->name = %s", class_dev->name);
127 /* wait for the "dev" file to show up in the directory in sysfs.
128 * If it doesn't happen in about 10 seconds, give up.
130 #define SECONDS_TO_WAIT_FOR_DEV 10
131 int sleep_for_dev(char *path)
133 char filename[SYSFS_PATH_MAX + 6];
136 int retval = -ENODEV;
138 strcpy(filename, sysfs_path);
139 strcat(filename, path);
140 strcat(filename, "/dev");
142 while (loop < SECONDS_TO_WAIT_FOR_DEV) {
143 dbg("looking for %s", filename);
144 retval = stat(filename, &buf);
150 /* sleep for a second or two to give the kernel a chance to
151 * create the dev file */
159 int udev_add_device(char *path, char *subsystem)
161 struct sysfs_class_device *class_dev;
163 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 = sleep_for_dev(path);
176 class_dev = get_class_dev(path);
177 if (class_dev == NULL)
180 retval = namedev_name_device(class_dev, &attr);
184 retval = get_major_minor(class_dev, &dev.major, &dev.minor);
186 dbg("get_major_minor failed");
190 strcpy(dev.name, attr.name);
191 strcpy(dev.owner, attr.owner);
192 strcpy(dev.group, attr.group);
193 dev.mode = attr.mode;
195 retval = udevdb_add_dev(path, &dev);
197 dbg("udevdb_add_dev failed, but we are going to try to create the node anyway. "
198 "But remove might not work properly for this device.");
200 sysfs_close_class_device(class_dev);
202 dbg("name = %s", dev.name);
203 retval = create_node(&dev);