2 * udev_sysfs.c - sysfs linux kernel specific knowledge
4 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
5 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation version 2 of the License.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include "libsysfs/sysfs/libsysfs.h"
32 #include "udev_version.h"
33 #include "udev_sysfs.h"
34 #include "udev_utils.h"
37 /* list of subsystem specific files, NULL if there is no file to wait for */
38 dev_t get_devt(struct sysfs_class_device *class_dev)
40 struct sysfs_attribute *attr = NULL;
41 unsigned int major, minor;
43 attr = sysfs_get_classdev_attr(class_dev, "dev");
46 dbg("dev='%s'", attr->value);
48 if (sscanf(attr->value, "%u:%u", &major, &minor) != 2)
50 dbg("found major=%d, minor=%d", major, minor);
52 return makedev(major, minor);
55 /* wait for a devices device specific file to show up */
56 int wait_for_devices_device(struct sysfs_device *devices_dev,
59 static const struct device_file {
63 { .bus = "scsi", .file = "vendor" },
64 { .bus = "usb", .file = "idVendor" },
65 { .bus = "usb", .file = "iInterface" },
66 { .bus = "usb", .file = "bNumEndpoints" },
67 { .bus = "usb-serial", .file = "bus" },
68 { .bus = "ide", .file = "bus" },
69 { .bus = "pci", .file = "vendor" },
70 { .bus = "pci_express", .file = "bus" },
71 { .bus = "platform", .file = "bus" },
72 { .bus = "pcmcia", .file = "bus" },
73 { .bus = "i2c", .file = "bus" },
74 { .bus = "ieee1394", .file = "node_count" },
75 { .bus = "ieee1394", .file = "nodeid" },
76 { .bus = "ieee1394", .file = "address" },
77 { .bus = "bttv-sub", .file = NULL },
78 { .bus = "pnp", .file = "bus" },
79 { .bus = "eisa", .file = "bus" },
80 { .bus = "serio", .file = "bus" },
81 { .bus = "pseudo", .file = "bus" },
82 { .bus = "mmc", .file = "bus" },
83 { .bus = "macio", .file = "bus" },
84 { .bus = "of_platform", .file = "bus" },
85 { .bus = "vio", .file = "bus" },
86 { .bus = "ecard", .file = "bus" },
87 { .bus = "sa1111-rab", .file = "bus" },
88 { .bus = "amba", .file = "bus" },
89 { .bus = "locomo-bus", .file = "bus" },
90 { .bus = "logicmodule", .file = "bus" },
91 { .bus = "parisc", .file = "bus" },
92 { .bus = "ocp", .file = "bus" },
93 { .bus = "dio", .file = "bus" },
94 { .bus = "MCA", .file = "bus" },
95 { .bus = "wl", .file = "bus" },
96 { .bus = "ccwgroup", .file = "bus" },
97 { .bus = "css", .file = "bus" },
98 { .bus = "ccw", .file = "bus" },
99 { .bus = "iucv", .file = "bus" },
102 const struct device_file *devicefile = NULL;
105 if (getenv("PHYSDEVBUS") == NULL) {
106 dbg("the kernel says, that there is no bus for '%s'", devices_dev->path);
110 /* wait for the bus device link to the devices device */
111 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
113 if (sysfs_get_device_bus(devices_dev) == 0)
116 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
119 dbg("error: getting bus device link");
121 *error = "no bus device link";
124 dbg("bus device link found for bus '%s'", devices_dev->bus);
126 /* wait for a bus device specific file to show up */
127 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
129 int found_bus_type = 0;
131 for (devicefile = device_files; devicefile->bus != NULL; devicefile++) {
132 if (strcmp(devices_dev->bus, devicefile->bus) == 0) {
133 char filename[PATH_SIZE];
136 if (devicefile->file == NULL) {
137 dbg("bus '%s' has no file to wait for", devices_dev->bus);
142 snprintf(filename, sizeof(filename), "%s/%s", devices_dev->path, devicefile->file);
143 filename[sizeof(filename)-1] = '\0';
144 dbg("looking at bus '%s' device for specific file '%s'", devices_dev->bus, filename);
146 if (stat(filename, &stats) == 0) {
147 dbg("bus '%s' device specific file '%s' found", devices_dev->bus, devicefile->file);
152 if (found_bus_type == 0) {
154 *error = "unknown bus";
155 info("error: unknown bus, please report to "
156 "<linux-hotplug-devel@lists.sourceforge.net> '%s'", devices_dev->bus);
159 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
162 dbg("error: getting '%s' device specific file '%s'", devices_dev->bus, devicefile->file);
164 *error = "bus device specific file unavailable";
169 struct sysfs_class_device *wait_class_device_open(const char *path)
171 struct sysfs_class_device *class_dev = NULL;
174 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
176 class_dev = sysfs_open_class_device_path(path);
180 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
186 int wait_for_class_device(struct sysfs_class_device *class_dev,
189 const struct subsystem_file {
190 const char *subsystem;
192 } subsystem_files[] = {
193 { .subsystem = "net", .file = "ifindex" },
194 { .subsystem = "scsi_host", .file = "unique_id" },
195 { .subsystem = "pcmcia_socket", .file = "card_type" },
196 { .subsystem = "bluetooth", .file = "address" },
197 { .subsystem = "firmware", .file = "data" },
198 { .subsystem = "fc_transport", .file = "port_id" },
199 { .subsystem = "fc_host", .file = "port_id" },
200 { .subsystem = "spi_transport", .file = "width" },
201 { .subsystem = "spi_host", .file = "width" },
205 const struct subsystem_file *subsys_file;
206 struct sysfs_class_device *class_dev_parent;
207 struct sysfs_device *devices_dev = NULL;
208 char filename[PATH_SIZE];
211 /* look if we want to wait for a file */
212 for (subsys_file = subsystem_files; subsys_file->subsystem != NULL; subsys_file++)
213 if (strcmp(class_dev->classname, subsys_file->subsystem) == 0)
216 if (subsys_file->file == NULL) {
217 dbg("class '%s' has no file to wait for", class_dev->classname);
221 snprintf(filename, sizeof(filename), "%s/%s", class_dev->path, subsys_file->file);
222 filename[sizeof(filename)-1] = '\0';
223 dbg("looking at class '%s' for specific file '%s'", class_dev->classname, filename);
225 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
229 if (stat(class_dev->path, &stats) == -1) {
230 dbg("'%s' now disappeared (probably remove has beaten us)", class_dev->path);
234 if (stat(filename, &stats) == 0) {
235 dbg("class '%s' specific file '%s' found", class_dev->classname, subsys_file->file);
239 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
242 dbg("error: getting class '%s' specific file '%s'", class_dev->classname, subsys_file->file);
244 *error = "class specific file unavailable";
247 /* skip devices without devices-link */
248 if (getenv("PHYSDEVPATH") == NULL) {
249 dbg("the kernel says, that there is no physical device for '%s'", class_dev->path);
253 /* the symlink may be on the parent device */
254 class_dev_parent = sysfs_get_classdev_parent(class_dev);
255 if (class_dev_parent)
256 dbg("looking at parent device for device link '%s'", class_dev_parent->path);
258 /* wait for the symlink to the devices device */
259 dbg("waiting for symlink to devices device");
260 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
262 if (class_dev_parent)
263 devices_dev = sysfs_get_classdev_device(class_dev_parent);
265 devices_dev = sysfs_get_classdev_device(class_dev);
270 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
273 dbg(" error: no devices device symlink found");
275 *error = "no device symlink";
278 dbg("device symlink found pointing to '%s'", devices_dev->path);
280 return wait_for_devices_device(devices_dev, error);
283 struct sysfs_device *wait_devices_device_open(const char *path)
285 struct sysfs_device *devices_dev = NULL;
288 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
290 devices_dev = sysfs_open_device_path(path);
294 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);