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 static const struct subsystem_file {
39 const char *subsystem;
41 } subsystem_files[] = {
42 { .subsystem = "class", .file = NULL },
43 { .subsystem = "net", .file = "ifindex" },
44 { .subsystem = "scsi_host", .file = "unique_id" },
45 { .subsystem = "scsi_device", .file = NULL },
46 { .subsystem = "pcmcia_socket", .file = "card_type" },
47 { .subsystem = "usb_host", .file = NULL },
48 { .subsystem = "bluetooth", .file = "address" },
49 { .subsystem = "firmware", .file = "data" },
50 { .subsystem = "i2c-adapter", .file = NULL },
51 { .subsystem = "pci_bus", .file = NULL },
52 { .subsystem = "ieee1394", .file = NULL },
53 { .subsystem = "ieee1394_host", .file = NULL },
54 { .subsystem = "ieee1394_node", .file = NULL },
55 { .subsystem = "fc_transport", .file = "port_id" },
56 { .subsystem = "fc_host", .file = "port_id" },
60 int subsystem_expect_no_dev(const char *subsystem)
62 const struct subsystem_file *file;
64 for (file = subsystem_files; file->subsystem != NULL; file++)
65 if (strcmp(subsystem, file->subsystem) == 0)
71 /* get subsystem specific files, returns "dev" if no other found */
72 static const char *get_subsystem_specific_file(const char *subsystem)
74 const struct subsystem_file *file;
76 /* look if we want to look for another file instead of "dev" */
77 for (file = subsystem_files; file->subsystem != NULL; file++)
78 if (strcmp(subsystem, file->subsystem) == 0)
84 /* wait for class pecific file to show up */
85 static int wait_for_class_device_attributes(struct sysfs_class_device *class_dev,
89 char filename[SYSFS_PATH_MAX];
92 file = get_subsystem_specific_file(class_dev->classname);
94 dbg("class '%s' has no file to wait for", class_dev->classname);
98 snprintf(filename, SYSFS_PATH_MAX-1, "%s/%s", class_dev->path, file);
99 dbg("looking at class '%s' for specific file '%s'", class_dev->classname, filename);
101 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
105 if (stat(class_dev->path, &stats) == -1) {
106 dbg("'%s' now disappeared (probably remove has beaten us)", class_dev->path);
110 if (stat(filename, &stats) == 0) {
111 dbg("class '%s' specific file '%s' found", class_dev->classname, file);
115 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
118 dbg("error: getting class '%s' specific file '%s'", class_dev->classname, file);
120 *error = "class specific file unavailable";
124 /* check if we need to wait for a physical device */
125 static int class_device_expect_no_device_link(struct sysfs_class_device *class_dev)
127 /* list of devices without a "device" symlink to the physical device
128 * if device is set to NULL, no devices in that subsystem has a link */
129 static const struct class_device {
130 const char *subsystem;
133 { .subsystem = "block", .device = "double" },
134 { .subsystem = "block", .device = "nb" },
135 { .subsystem = "block", .device = "ram" },
136 { .subsystem = "block", .device = "loop" },
137 { .subsystem = "block", .device = "fd" },
138 { .subsystem = "block", .device = "md" },
139 { .subsystem = "block", .device = "dos_cd" },
140 { .subsystem = "block", .device = "rflash" },
141 { .subsystem = "block", .device = "rom" },
142 { .subsystem = "block", .device = "rrom" },
143 { .subsystem = "block", .device = "flash" },
144 { .subsystem = "block", .device = "msd" },
145 { .subsystem = "block", .device = "sbpcd" },
146 { .subsystem = "block", .device = "pcd" },
147 { .subsystem = "block", .device = "pf" },
148 { .subsystem = "block", .device = "scd" },
149 { .subsystem = "block", .device = "ubd" },
150 { .subsystem = "block", .device = "dm-" },
151 { .subsystem = "block", .device = "bcrypt" },
152 { .subsystem = "input", .device = "event" },
153 { .subsystem = "input", .device = "mice" },
154 { .subsystem = "input", .device = "mouse" },
155 { .subsystem = "input", .device = "ts" },
156 { .subsystem = "vc", .device = NULL },
157 { .subsystem = "tty", .device = NULL },
158 { .subsystem = "cpuid", .device = "cpu" },
159 { .subsystem = "graphics", .device = "fb" },
160 { .subsystem = "mem", .device = NULL },
161 { .subsystem = "misc", .device = NULL },
162 { .subsystem = "msr", .device = NULL },
163 { .subsystem = "netlink", .device = NULL },
164 { .subsystem = "net", .device = "sit" },
165 { .subsystem = "net", .device = "lo" },
166 { .subsystem = "net", .device = "tap" },
167 { .subsystem = "net", .device = "ipsec" },
168 { .subsystem = "net", .device = "dummy" },
169 { .subsystem = "net", .device = "irda" },
170 { .subsystem = "net", .device = "ppp" },
171 { .subsystem = "net", .device = "tun" },
172 { .subsystem = "net", .device = "pan" },
173 { .subsystem = "net", .device = "bnep" },
174 { .subsystem = "net", .device = "vmnet" },
175 { .subsystem = "net", .device = "ippp" },
176 { .subsystem = "net", .device = "nlv" },
177 { .subsystem = "net", .device = "atml" },
178 { .subsystem = "ppp", .device = NULL },
179 { .subsystem = "sound", .device = NULL },
180 { .subsystem = "printer", .device = "lp" },
181 { .subsystem = "ppdev", .device = NULL },
182 { .subsystem = "nvidia", .device = NULL },
183 { .subsystem = "video4linux", .device = "vbi" },
184 { .subsystem = "dvb", .device = NULL },
185 { .subsystem = "lirc", .device = NULL },
186 { .subsystem = "firmware", .device = NULL },
187 { .subsystem = "drm", .device = NULL },
188 { .subsystem = "pci_bus", .device = NULL },
189 { .subsystem = "ieee1394", .device = NULL },
190 { .subsystem = "ieee1394_host", .device = NULL },
191 { .subsystem = "ieee1394_node", .device = NULL },
192 { .subsystem = "raw", .device = NULL },
193 { .subsystem = "zaptel", .device = NULL },
194 { .subsystem = "tiglusb", .device = NULL },
195 { .subsystem = "ppdev", .device = NULL },
196 { .subsystem = "ticables", .device = NULL },
197 { .subsystem = "snsc", .device = NULL },
198 { .subsystem = "staliomem", .device = NULL },
199 { .subsystem = "tape", .device = NULL },
200 { .subsystem = "ip2", .device = NULL },
201 { .subsystem = "tpqic02", .device = NULL },
202 { .subsystem = "dsp56k", .device = NULL },
203 { .subsystem = "zft", .device = NULL },
204 { .subsystem = "adb", .device = NULL },
205 { .subsystem = "cosa", .device = NULL },
206 { .subsystem = "pg", .device = NULL },
207 { .subsystem = "pt", .device = NULL },
208 { .subsystem = "capi", .device = NULL },
211 const struct class_device *classdevice;
214 /* the kernel may tell us what to wait for */
215 if (kernel_release_satisfactory(2,6,10) > 0)
216 if (getenv("PHYSDEVPATH") == NULL) {
217 dbg("the kernel says, that there is no physical device for '%s'", class_dev->path);
221 for (classdevice = class_device; classdevice->subsystem != NULL; classdevice++) {
222 if (strcmp(class_dev->classname, classdevice->subsystem) == 0) {
223 /* see if no device in this class is expected to have a device-link */
224 if (classdevice->device == NULL)
227 len = strlen(classdevice->device);
229 /* see if device name matches */
230 if (strncmp(class_dev->name, classdevice->device, len) != 0)
233 /* exact name match */
234 if (strlen(class_dev->name) == len)
237 /* name match with instance number */
238 if (isdigit(class_dev->name[len]))
246 /* skip waiting for the bus of the devices device */
247 static int class_device_expect_no_bus(struct sysfs_class_device *class_dev)
249 static const char *devices_without_bus[] = {
257 for (device = devices_without_bus; *device != NULL; device++) {
258 int len = strlen(*device);
260 if (strncmp(class_dev->classname, *device, len) == 0)
267 /* wait for a devices device specific file to show up */
268 int wait_for_devices_device(struct sysfs_device *devices_dev,
271 static const struct device_file {
275 { .bus = "scsi", .file = "vendor" },
276 { .bus = "usb", .file = "idVendor" },
277 { .bus = "usb", .file = "iInterface" },
278 { .bus = "usb", .file = "bNumEndpoints" },
279 { .bus = "usb-serial", .file = "detach_state" },
280 { .bus = "ide", .file = "detach_state" },
281 { .bus = "pci", .file = "vendor" },
282 { .bus = "platform", .file = "detach_state" },
283 { .bus = "pcmcia", .file = "detach_state" },
284 { .bus = "i2c", .file = "detach_state" },
285 { .bus = "ieee1394", .file = "node_count" },
286 { .bus = "ieee1394", .file = "nodeid" },
287 { .bus = "ieee1394", .file = "address" },
288 { .bus = "bttv-sub", .file = NULL },
289 { .bus = "pnp", .file = "detach_state" },
290 { .bus = "eisa", .file = "detach_state" },
291 { .bus = "pseudo", .file = "detach_state" },
292 { .bus = "mmc", .file = "detach_state" },
293 { .bus = "macio", .file = "detach_state" },
294 { .bus = "of_platform", .file = "detach_state" },
295 { .bus = "vio", .file = "detach_state" },
296 { .bus = "ecard", .file = "detach_state" },
297 { .bus = "sa1111-rab", .file = "detach_state" },
298 { .bus = "amba", .file = "detach_state" },
299 { .bus = "locomo-bus", .file = "detach_state" },
300 { .bus = "logicmodule", .file = "detach_state" },
301 { .bus = "parisc", .file = "detach_state" },
302 { .bus = "ocp", .file = "detach_state" },
303 { .bus = "dio", .file = "detach_state" },
304 { .bus = "MCA", .file = "detach_state" },
305 { .bus = "wl", .file = "detach_state" },
306 { .bus = "ccwgroup", .file = "detach_state" },
307 { .bus = "css", .file = "detach_state" },
308 { .bus = "ccw", .file = "detach_state" },
309 { .bus = "iucv", .file = "detach_state" },
312 const struct device_file *devicefile;
315 /* the kernel may tell us what to wait for */
316 if (kernel_release_satisfactory(2,6,10) > 0)
317 if (getenv("PHYSDEVBUS") == NULL) {
318 dbg("the kernel says, that there is no bus for '%s'", devices_dev->path);
322 /* wait for the bus device link to the devices device */
323 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
325 if (sysfs_get_device_bus(devices_dev) == 0)
328 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
331 dbg("error: getting bus device link");
333 *error = "no bus device link";
336 dbg("bus device link found for bus '%s'", devices_dev->bus);
338 /* wait for a bus device specific file to show up */
339 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
341 int found_bus_type = 0;
343 for (devicefile = device_files; devicefile->bus != NULL; devicefile++) {
344 if (strcmp(devices_dev->bus, devicefile->bus) == 0) {
345 char filename[SYSFS_PATH_MAX];
348 if (devicefile->file == NULL) {
349 dbg("bus '%s' has no file to wait for", devices_dev->bus);
354 snprintf(filename, SYSFS_PATH_MAX-1, "%s/%s", devices_dev->path, devicefile->file);
355 dbg("looking at bus '%s' device for specific file '%s'", devices_dev->bus, filename);
357 if (stat(filename, &stats) == 0) {
358 dbg("bus '%s' device specific file '%s' found", devices_dev->bus, devicefile->file);
363 if (found_bus_type == 0) {
365 *error = "unknown bus";
366 info("error: unknown bus, please report to "
367 "<linux-hotplug-devel@lists.sourceforge.net> '%s'", devices_dev->bus);
370 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
373 dbg("error: getting '%s' device specific file '%s'", devices_dev->bus, devicefile->file);
375 *error = "bus device specific file unavailable";
380 struct sysfs_class_device *wait_class_device_open(const char *path)
382 struct sysfs_class_device *class_dev;
385 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
387 class_dev = sysfs_open_class_device_path(path);
391 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
397 int wait_for_class_device(struct sysfs_class_device *class_dev,
400 struct sysfs_class_device *class_dev_parent;
401 struct sysfs_device *devices_dev = NULL;
404 if (wait_for_class_device_attributes(class_dev, error) != 0)
407 /* skip devices without devices-link */
408 if (class_device_expect_no_device_link(class_dev)) {
409 dbg("no device symlink expected for '%s', ", class_dev->name);
413 /* the symlink may be on the parent device */
414 class_dev_parent = sysfs_get_classdev_parent(class_dev);
415 if (class_dev_parent)
416 dbg("looking at parent device for device link '%s'", class_dev_parent->path);
418 /* wait for the symlink to the devices device */
419 dbg("waiting for symlink to devices device");
420 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
422 if (class_dev_parent)
423 devices_dev = sysfs_get_classdev_device(class_dev_parent);
425 devices_dev = sysfs_get_classdev_device(class_dev);
430 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
433 dbg(" error: no devices device symlink found");
435 *error = "no device symlink";
438 dbg("device symlink found pointing to '%s'", devices_dev->path);
440 /* wait for the devices device */
441 if (class_device_expect_no_bus(class_dev)) {
442 dbg("no bus device expected for '%s', ", class_dev->classname);
446 return wait_for_devices_device(devices_dev, error);
449 struct sysfs_device *wait_devices_device_open(const char *path)
451 struct sysfs_device *devices_dev;
454 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
456 devices_dev = sysfs_open_device_path(path);
460 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);