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, int *major, int *minor)
53 dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev");
59 if (sscanf(dev, "%u:%u", major, minor) != 2)
62 dbg("found major = %d, minor = %d", *major, *minor);
70 * We also want to add some permissions here, and possibly some symlinks
72 static int create_node(struct udevice *dev)
78 strncpy(filename, udev_root, sizeof(filename));
79 strncat(filename, dev->name, sizeof(filename));
82 res = (dev->major << 8) | (dev->minor);
84 res = makedev(dev->major, dev->minor);
99 dbg("unknown node type %c\n", dev->type);
103 dbg("mknod(%s, %#o, %u, %u)", filename, dev->mode, dev->major, dev->minor);
104 retval = mknod(filename, dev->mode, res);
106 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
107 filename, dev->mode, dev->major, dev->minor, strerror(errno));
109 // FIXME set the ownership of the node
113 static struct sysfs_class_device *get_class_dev(char *device_name)
115 char dev_path[SYSFS_PATH_MAX];
116 struct sysfs_class_device *class_dev = NULL;
118 strcpy(dev_path, sysfs_path);
119 strcat(dev_path, device_name);
121 dbg("looking at %s", dev_path);
123 /* open up the sysfs class device for this thing... */
124 class_dev = sysfs_open_class_device(dev_path);
125 if (class_dev == NULL) {
126 dbg ("sysfs_open_class_device failed");
129 dbg("class_dev->name = %s", class_dev->name);
135 /* wait for the "dev" file to show up in the directory in sysfs.
136 * If it doesn't happen in about 10 seconds, give up.
138 #define SECONDS_TO_WAIT_FOR_DEV 10
139 static int sleep_for_dev(char *path)
141 char filename[SYSFS_PATH_MAX + 6];
142 int loop = SECONDS_TO_WAIT_FOR_DEV;
145 strcpy(filename, sysfs_path);
146 strcat(filename, path);
147 strcat(filename, "/dev");
152 dbg("looking for %s", filename);
153 retval = stat(filename, &buf);
157 /* sleep for a second or two to give the kernel a chance to
158 * create the dev file */
166 int udev_add_device(char *path, char *subsystem)
168 struct sysfs_class_device *class_dev;
170 int retval = -EINVAL;
172 /* for now, the block layer is the only place where block devices are */
173 if (strcmp(subsystem, "block") == 0)
178 retval = sleep_for_dev(path);
182 class_dev = get_class_dev(path);
183 if (class_dev == NULL)
186 retval = namedev_name_device(class_dev, &dev);
190 retval = get_major_minor(class_dev, &dev.major, &dev.minor);
192 dbg("get_major_minor failed");
196 // strcpy(dev.name, attr.name);
197 // strcpy(dev.owner, attr.owner);
198 // strcpy(dev.group, attr.group);
199 // dev.mode = attr.mode;
201 retval = udevdb_add_dev(path, &dev);
203 dbg("udevdb_add_dev failed, but we are going to try to create the node anyway. "
204 "But remove might not work properly for this device.");
206 sysfs_close_class_device(class_dev);
208 dbg("name = %s", dev.name);
209 retval = create_node(&dev);