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"
34 #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 hex.
47 * Yes, this will probably change when we go to a bigger major/minor
48 * range, and will have to be changed at that time.
50 static int get_major_minor(struct sysfs_class_device *class_dev, int *major, int *minor)
56 dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev");
62 if (sscanf(dev, "%u:%u", major, minor) != 2)
65 dbg("found major = %d, minor = %d", *major, *minor);
73 * We also want to add some permissions here, and possibly some symlinks
75 static int create_node(char *name, char type, int major, int minor, int mode)
79 strncpy(filename, UDEV_ROOT, sizeof(filename));
80 strncat(filename, name, sizeof(filename));
83 } else if ((type == 'c') || (type == 'u')){
85 } else if ( type == 'p') {
88 dbg("unknown node type %c\n", type);
92 retval = mknod(filename,mode,makedev(major,minor));
97 struct sysfs_class_device *get_class_dev(char *device_name)
99 char dev_path[SYSFS_PATH_MAX];
100 struct sysfs_class_device *class_dev;
102 strcpy(dev_path, sysfs_path);
103 strcat(dev_path, device_name);
105 dbg("looking at %s", dev_path);
107 /* open up the sysfs class device for this thing... */
108 class_dev = sysfs_open_class_device(dev_path);
109 if (class_dev == NULL) {
110 dbg ("sysfs_open_class_device failed");
113 dbg("class_dev->name = %s", class_dev->name);
118 static int udev_init(void)
122 retval = sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX);
123 dbg("sysfs_path = %s", sysfs_path);
127 int udev_add_device(char *device, char *subsystem)
129 struct sysfs_class_device *class_dev;
130 struct device_attr attr;
136 int retval = -EINVAL;
138 /* sleep for a second or two to give the kernel a chance to
139 * create the dev file
145 /* for now, the block layer is the only place where block devices are */
146 if (strcmp(subsystem, "block") == 0)
151 class_dev = get_class_dev(device);
152 if (class_dev == NULL)
155 retval = namedev_name_device(class_dev, &attr);
159 retval = get_major_minor(class_dev, &major, &minor);
161 dbg ("get_major_minor failed");
165 sysfs_close_class_device(class_dev);
167 return create_node(attr.name, type, major, minor, attr.mode);