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"
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 = "net", .file = "ifindex" },
43 { .subsystem = "scsi_host", .file = "unique_id" },
44 { .subsystem = "scsi_device", .file = NULL },
45 { .subsystem = "pcmcia_socket", .file = "card_type" },
46 { .subsystem = "usb_host", .file = NULL },
47 { .subsystem = "bluetooth", .file = "address" },
48 { .subsystem = "firmware", .file = "data" },
49 { .subsystem = "i2c-adapter", .file = NULL },
50 { .subsystem = "pci_bus", .file = NULL },
51 { .subsystem = "ieee1394", .file = NULL },
52 { .subsystem = "ieee1394_host", .file = NULL },
53 { .subsystem = "ieee1394_node", .file = NULL },
57 int subsystem_expect_no_dev(const char *subsystem)
59 const struct subsystem_file *file;
61 for (file = subsystem_files; file->subsystem != NULL; file++)
62 if (strcmp(subsystem, file->subsystem) == 0)
68 /* get subsystem specific files, returns "dev" if no other found */
69 static const char *get_subsystem_specific_file(const char *subsystem)
71 const struct subsystem_file *file;
73 /* look if we want to look for another file instead of "dev" */
74 for (file = subsystem_files; file->subsystem != NULL; file++)
75 if (strcmp(subsystem, file->subsystem) == 0)
81 /* wait for class pecific file to show up */
82 static int wait_for_class_device_attributes(struct sysfs_class_device *class_dev,
86 char filename[SYSFS_PATH_MAX];
89 file = get_subsystem_specific_file(class_dev->classname);
91 dbg("class '%s' has no file to wait for", class_dev->classname);
95 snprintf(filename, SYSFS_PATH_MAX-1, "%s/%s", class_dev->path, file);
96 dbg("looking at class '%s' for specific file '%s'", class_dev->classname, filename);
98 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
102 if (stat(class_dev->path, &stats) == -1) {
103 dbg("'%s' now disappeared (probably remove has beaten us)", class_dev->path);
107 if (stat(filename, &stats) == 0) {
108 dbg("class '%s' specific file '%s' found", class_dev->classname, file);
112 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
115 dbg("error: getting class '%s' specific file '%s'", class_dev->classname, file);
117 *error = "class specific file unavailable";
121 /* check if we need to wait for a physical device */
122 static int class_device_expect_no_device_link(struct sysfs_class_device *class_dev)
124 /* list of devices without a "device" symlink to the physical device
125 * if device is set to NULL, no devices in that subsystem has a link */
126 static const struct class_device {
127 const char *subsystem;
130 { .subsystem = "block", .device = "double" },
131 { .subsystem = "block", .device = "nb" },
132 { .subsystem = "block", .device = "ram" },
133 { .subsystem = "block", .device = "loop" },
134 { .subsystem = "block", .device = "fd" },
135 { .subsystem = "block", .device = "md" },
136 { .subsystem = "block", .device = "dos_cd" },
137 { .subsystem = "block", .device = "rflash" },
138 { .subsystem = "block", .device = "rom" },
139 { .subsystem = "block", .device = "rrom" },
140 { .subsystem = "block", .device = "flash" },
141 { .subsystem = "block", .device = "msd" },
142 { .subsystem = "block", .device = "sbpcd" },
143 { .subsystem = "block", .device = "pcd" },
144 { .subsystem = "block", .device = "pf" },
145 { .subsystem = "block", .device = "scd" },
146 { .subsystem = "block", .device = "ubd" },
147 { .subsystem = "block", .device = "dm-" },
148 { .subsystem = "block", .device = "bcrypt" },
149 { .subsystem = "input", .device = "event" },
150 { .subsystem = "input", .device = "mice" },
151 { .subsystem = "input", .device = "mouse" },
152 { .subsystem = "input", .device = "ts" },
153 { .subsystem = "vc", .device = NULL },
154 { .subsystem = "tty", .device = NULL },
155 { .subsystem = "cpuid", .device = "cpu" },
156 { .subsystem = "graphics", .device = "fb" },
157 { .subsystem = "mem", .device = NULL },
158 { .subsystem = "misc", .device = NULL },
159 { .subsystem = "msr", .device = NULL },
160 { .subsystem = "netlink", .device = NULL },
161 { .subsystem = "net", .device = "sit" },
162 { .subsystem = "net", .device = "lo" },
163 { .subsystem = "net", .device = "tap" },
164 { .subsystem = "net", .device = "ipsec" },
165 { .subsystem = "net", .device = "dummy" },
166 { .subsystem = "net", .device = "irda" },
167 { .subsystem = "net", .device = "ppp" },
168 { .subsystem = "net", .device = "tun" },
169 { .subsystem = "net", .device = "pan" },
170 { .subsystem = "net", .device = "bnep" },
171 { .subsystem = "net", .device = "vmnet" },
172 { .subsystem = "net", .device = "ippp" },
173 { .subsystem = "ppp", .device = NULL },
174 { .subsystem = "sound", .device = NULL },
175 { .subsystem = "printer", .device = "lp" },
176 { .subsystem = "nvidia", .device = NULL },
177 { .subsystem = "video4linux", .device = "vbi" },
178 { .subsystem = "dvb", .device = NULL },
179 { .subsystem = "lirc", .device = NULL },
180 { .subsystem = "firmware", .device = NULL },
181 { .subsystem = "drm", .device = NULL },
182 { .subsystem = "pci_bus", .device = NULL },
183 { .subsystem = "ieee1394", .device = NULL },
184 { .subsystem = "ieee1394_host", .device = NULL },
185 { .subsystem = "ieee1394_node", .device = NULL },
186 { .subsystem = "raw", .device = NULL },
187 { .subsystem = "zaptel", .device = NULL },
188 { .subsystem = "tiglusb", .device = NULL },
189 { .subsystem = "ppdev", .device = NULL },
190 { .subsystem = "ticables", .device = NULL },
191 { .subsystem = "snsc", .device = NULL },
192 { .subsystem = "staliomem", .device = NULL },
193 { .subsystem = "tape", .device = NULL },
194 { .subsystem = "ip2", .device = NULL },
195 { .subsystem = "tpqic02", .device = NULL },
196 { .subsystem = "dsp56k", .device = NULL },
197 { .subsystem = "zft", .device = NULL },
198 { .subsystem = "adb", .device = NULL },
199 { .subsystem = "cosa", .device = NULL },
200 { .subsystem = "pg", .device = NULL },
201 { .subsystem = "pt", .device = NULL },
202 { .subsystem = "capi", .device = NULL },
205 const struct class_device *classdevice;
208 /* the kernel may tell us what to wait for */
209 if (kernel_release_satisfactory(2,6,10) > 0)
210 if (getenv("PHYSDEVPATH") == NULL) {
211 dbg("the kernel says, that there is no physical device for '%s'", class_dev->path);
215 for (classdevice = class_device; classdevice->subsystem != NULL; classdevice++) {
216 if (strcmp(class_dev->classname, classdevice->subsystem) == 0) {
217 /* see if no device in this class is expected to have a device-link */
218 if (classdevice->device == NULL)
221 len = strlen(classdevice->device);
223 /* see if device name matches */
224 if (strncmp(class_dev->name, classdevice->device, len) != 0)
227 /* exact name match */
228 if (strlen(class_dev->name) == len)
231 /* name match with instance number */
232 if (isdigit(class_dev->name[len]))
240 /* skip waiting for the bus of the devices device */
241 static int class_device_expect_no_bus(struct sysfs_class_device *class_dev)
243 static const char *devices_without_bus[] = {
251 for (device = devices_without_bus; *device != NULL; device++) {
252 int len = strlen(*device);
254 if (strncmp(class_dev->classname, *device, len) == 0)
261 /* wait for a devices device specific file to show up */
262 int wait_for_devices_device(struct sysfs_device *devices_dev,
265 static const struct device_file {
269 { .bus = "scsi", .file = "vendor" },
270 { .bus = "usb", .file = "idVendor" },
271 { .bus = "usb", .file = "iInterface" },
272 { .bus = "usb", .file = "bNumEndpoints" },
273 { .bus = "usb-serial", .file = "detach_state" },
274 { .bus = "ide", .file = "detach_state" },
275 { .bus = "pci", .file = "vendor" },
276 { .bus = "platform", .file = "detach_state" },
277 { .bus = "i2c", .file = "detach_state" },
278 { .bus = "ieee1394", .file = "node_count" },
279 { .bus = "ieee1394", .file = "nodeid" },
280 { .bus = "ieee1394", .file = "address" },
281 { .bus = "bttv-sub", .file = NULL },
282 { .bus = "pnp", .file = "detach_state" },
283 { .bus = "eisa", .file = "detach_state" },
284 { .bus = "pseudo", .file = "detach_state" },
285 { .bus = "mmc", .file = "detach_state" },
286 { .bus = "macio", .file = "detach_state" },
287 { .bus = "of_platform", .file = "detach_state" },
288 { .bus = "vio", .file = "detach_state" },
289 { .bus = "ecard", .file = "detach_state" },
290 { .bus = "sa1111-rab", .file = "detach_state" },
291 { .bus = "amba", .file = "detach_state" },
292 { .bus = "locomo-bus", .file = "detach_state" },
293 { .bus = "logicmodule", .file = "detach_state" },
294 { .bus = "parisc", .file = "detach_state" },
295 { .bus = "ocp", .file = "detach_state" },
296 { .bus = "dio", .file = "detach_state" },
297 { .bus = "MCA", .file = "detach_state" },
298 { .bus = "wl", .file = "detach_state" },
299 { .bus = "ccwgroup", .file = "detach_state" },
300 { .bus = "css", .file = "detach_state" },
301 { .bus = "ccw", .file = "detach_state" },
302 { .bus = "iucv", .file = "detach_state" },
305 const struct device_file *devicefile;
308 /* the kernel may tell us what to wait for */
309 if (kernel_release_satisfactory(2,6,10) > 0)
310 if (getenv("PHYSDEVBUS") == NULL) {
311 dbg("the kernel says, that there is no bus for '%s'", devices_dev->path);
315 /* wait for the bus device link to the devices device */
316 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
318 if (sysfs_get_device_bus(devices_dev) == 0)
321 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
324 dbg("error: getting bus device link");
326 *error = "no bus device link";
329 dbg("bus device link found for bus '%s'", devices_dev->bus);
331 /* wait for a bus device specific file to show up */
332 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
334 int found_bus_type = 0;
336 for (devicefile = device_files; devicefile->bus != NULL; devicefile++) {
337 if (strcmp(devices_dev->bus, devicefile->bus) == 0) {
338 char filename[SYSFS_PATH_MAX];
341 if (devicefile->file == NULL) {
342 dbg("bus '%s' has no file to wait for", devices_dev->bus);
347 snprintf(filename, SYSFS_PATH_MAX-1, "%s/%s", devices_dev->path, devicefile->file);
348 dbg("looking at bus '%s' device for specific file '%s'", devices_dev->bus, filename);
350 if (stat(filename, &stats) == 0) {
351 dbg("bus '%s' device specific file '%s' found", devices_dev->bus, devicefile->file);
356 if (found_bus_type == 0) {
358 *error = "unknown bus";
359 info("error: unknown bus, please report to "
360 "<linux-hotplug-devel@lists.sourceforge.net> '%s'", devices_dev->bus);
363 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
366 dbg("error: getting '%s' device specific file '%s'", devices_dev->bus, devicefile->file);
368 *error = "bus device specific file unavailable";
373 struct sysfs_class_device *wait_class_device_open(const char *path)
375 struct sysfs_class_device *class_dev;
378 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
380 class_dev = sysfs_open_class_device_path(path);
384 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
390 int wait_for_class_device(struct sysfs_class_device *class_dev,
393 struct sysfs_class_device *class_dev_parent;
394 struct sysfs_device *devices_dev = NULL;
397 if (wait_for_class_device_attributes(class_dev, error) != 0)
400 /* skip devices without devices-link */
401 if (class_device_expect_no_device_link(class_dev)) {
402 dbg("no device symlink expected for '%s', ", class_dev->name);
406 /* the symlink may be on the parent device */
407 class_dev_parent = sysfs_get_classdev_parent(class_dev);
408 if (class_dev_parent)
409 dbg("looking at parent device for device link '%s'", class_dev_parent->path);
411 /* wait for the symlink to the devices device */
412 dbg("waiting for symlink to devices device");
413 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
415 if (class_dev_parent)
416 devices_dev = sysfs_get_classdev_device(class_dev_parent);
418 devices_dev = sysfs_get_classdev_device(class_dev);
423 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
426 dbg(" error: no devices device symlink found");
428 *error = "no device symlink";
431 dbg("device symlink found pointing to '%s'", devices_dev->path);
433 /* wait for the devices device */
434 if (class_device_expect_no_bus(class_dev)) {
435 dbg("no bus device expected for '%s', ", class_dev->classname);
439 return wait_for_devices_device(devices_dev, error);
442 struct sysfs_device *wait_devices_device_open(const char *path)
444 struct sysfs_device *devices_dev;
447 loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
449 devices_dev = sysfs_open_device_path(path);
453 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);