]>
Commit | Line | Data |
---|---|---|
f0713480 KS |
1 | /* |
2 | * udev_sysfs.c - sysfs linux kernel specific knowledge | |
3 | * | |
4 | * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org> | |
5 | * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com> | |
6 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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. | |
19 | * | |
20 | */ | |
21 | ||
22 | #include <stdio.h> | |
23 | #include <stddef.h> | |
24 | #include <stdlib.h> | |
25 | #include <unistd.h> | |
26 | #include <string.h> | |
27 | #include <ctype.h> | |
28 | #include <errno.h> | |
29 | #include <sys/stat.h> | |
30 | ||
d402af7d | 31 | #include "libsysfs/sysfs/libsysfs.h" |
f0713480 KS |
32 | #include "udev_version.h" |
33 | #include "udev_sysfs.h" | |
9af5bb2f | 34 | #include "udev_utils.h" |
d402af7d | 35 | #include "logging.h" |
f0713480 | 36 | |
d402af7d | 37 | /* list of subsystem specific files, NULL if there is no file to wait for */ |
a4f0cc79 KS |
38 | static const struct subsystem_file { |
39 | const char *subsystem; | |
40 | const char *file; | |
f0713480 | 41 | } subsystem_files[] = { |
5ce120d3 | 42 | { .subsystem = "class", .file = NULL }, |
f0713480 KS |
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 }, | |
d2fe701e | 55 | { .subsystem = "fc_transport", .file = "port_id" }, |
0dfbe945 | 56 | { .subsystem = "fc_host", .file = "port_id" }, |
4be25ce1 KS |
57 | { .subsystem = "spi_transport", .file = "width" }, |
58 | { .subsystem = "spi_host", .file = "width" }, | |
f0713480 KS |
59 | { NULL, NULL } |
60 | }; | |
61 | ||
62 | int subsystem_expect_no_dev(const char *subsystem) | |
63 | { | |
a4f0cc79 | 64 | const struct subsystem_file *file; |
f0713480 KS |
65 | |
66 | for (file = subsystem_files; file->subsystem != NULL; file++) | |
67 | if (strcmp(subsystem, file->subsystem) == 0) | |
68 | return 1; | |
69 | ||
70 | return 0; | |
71 | } | |
72 | ||
73 | /* get subsystem specific files, returns "dev" if no other found */ | |
a4f0cc79 | 74 | static const char *get_subsystem_specific_file(const char *subsystem) |
f0713480 | 75 | { |
a4f0cc79 | 76 | const struct subsystem_file *file; |
f0713480 KS |
77 | |
78 | /* look if we want to look for another file instead of "dev" */ | |
79 | for (file = subsystem_files; file->subsystem != NULL; file++) | |
80 | if (strcmp(subsystem, file->subsystem) == 0) | |
81 | return file->file; | |
82 | ||
83 | return "dev"; | |
84 | } | |
85 | ||
86 | /* wait for class pecific file to show up */ | |
87 | static int wait_for_class_device_attributes(struct sysfs_class_device *class_dev, | |
88 | const char **error) | |
89 | { | |
90 | const char *file; | |
91 | char filename[SYSFS_PATH_MAX]; | |
92 | int loop; | |
93 | ||
94 | file = get_subsystem_specific_file(class_dev->classname); | |
95 | if (file == NULL) { | |
96 | dbg("class '%s' has no file to wait for", class_dev->classname); | |
97 | return 0; | |
98 | } | |
99 | ||
1ad9a8c1 KS |
100 | snprintf(filename, SYSFS_PATH_MAX-1, "%s/%s", class_dev->path, file); |
101 | dbg("looking at class '%s' for specific file '%s'", class_dev->classname, filename); | |
f0713480 KS |
102 | |
103 | loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND; | |
104 | while (--loop) { | |
105 | struct stat stats; | |
106 | ||
107 | if (stat(class_dev->path, &stats) == -1) { | |
108 | dbg("'%s' now disappeared (probably remove has beaten us)", class_dev->path); | |
109 | return -ENODEV; | |
110 | } | |
111 | ||
112 | if (stat(filename, &stats) == 0) { | |
113 | dbg("class '%s' specific file '%s' found", class_dev->classname, file); | |
114 | return 0; | |
115 | } | |
116 | ||
117 | usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND); | |
118 | } | |
119 | ||
120 | dbg("error: getting class '%s' specific file '%s'", class_dev->classname, file); | |
121 | if (error) | |
122 | *error = "class specific file unavailable"; | |
123 | return -ENOENT; | |
124 | } | |
125 | ||
126 | /* check if we need to wait for a physical device */ | |
127 | static int class_device_expect_no_device_link(struct sysfs_class_device *class_dev) | |
128 | { | |
129 | /* list of devices without a "device" symlink to the physical device | |
130 | * if device is set to NULL, no devices in that subsystem has a link */ | |
a4f0cc79 KS |
131 | static const struct class_device { |
132 | const char *subsystem; | |
133 | const char *device; | |
f0713480 KS |
134 | } class_device[] = { |
135 | { .subsystem = "block", .device = "double" }, | |
136 | { .subsystem = "block", .device = "nb" }, | |
137 | { .subsystem = "block", .device = "ram" }, | |
138 | { .subsystem = "block", .device = "loop" }, | |
139 | { .subsystem = "block", .device = "fd" }, | |
140 | { .subsystem = "block", .device = "md" }, | |
141 | { .subsystem = "block", .device = "dos_cd" }, | |
142 | { .subsystem = "block", .device = "rflash" }, | |
143 | { .subsystem = "block", .device = "rom" }, | |
144 | { .subsystem = "block", .device = "rrom" }, | |
145 | { .subsystem = "block", .device = "flash" }, | |
146 | { .subsystem = "block", .device = "msd" }, | |
147 | { .subsystem = "block", .device = "sbpcd" }, | |
148 | { .subsystem = "block", .device = "pcd" }, | |
149 | { .subsystem = "block", .device = "pf" }, | |
150 | { .subsystem = "block", .device = "scd" }, | |
151 | { .subsystem = "block", .device = "ubd" }, | |
152 | { .subsystem = "block", .device = "dm-" }, | |
4bee9994 | 153 | { .subsystem = "block", .device = "bcrypt" }, |
f0713480 KS |
154 | { .subsystem = "input", .device = "event" }, |
155 | { .subsystem = "input", .device = "mice" }, | |
156 | { .subsystem = "input", .device = "mouse" }, | |
157 | { .subsystem = "input", .device = "ts" }, | |
158 | { .subsystem = "vc", .device = NULL }, | |
159 | { .subsystem = "tty", .device = NULL }, | |
160 | { .subsystem = "cpuid", .device = "cpu" }, | |
161 | { .subsystem = "graphics", .device = "fb" }, | |
162 | { .subsystem = "mem", .device = NULL }, | |
163 | { .subsystem = "misc", .device = NULL }, | |
164 | { .subsystem = "msr", .device = NULL }, | |
165 | { .subsystem = "netlink", .device = NULL }, | |
166 | { .subsystem = "net", .device = "sit" }, | |
167 | { .subsystem = "net", .device = "lo" }, | |
168 | { .subsystem = "net", .device = "tap" }, | |
169 | { .subsystem = "net", .device = "ipsec" }, | |
170 | { .subsystem = "net", .device = "dummy" }, | |
171 | { .subsystem = "net", .device = "irda" }, | |
172 | { .subsystem = "net", .device = "ppp" }, | |
1ad9a8c1 | 173 | { .subsystem = "net", .device = "tun" }, |
21d2888a KS |
174 | { .subsystem = "net", .device = "pan" }, |
175 | { .subsystem = "net", .device = "bnep" }, | |
6628a2ea | 176 | { .subsystem = "net", .device = "vmnet" }, |
4bee9994 | 177 | { .subsystem = "net", .device = "ippp" }, |
f2503c97 | 178 | { .subsystem = "net", .device = "nlv" }, |
e1b7e62a | 179 | { .subsystem = "net", .device = "atml" }, |
f0713480 KS |
180 | { .subsystem = "ppp", .device = NULL }, |
181 | { .subsystem = "sound", .device = NULL }, | |
182 | { .subsystem = "printer", .device = "lp" }, | |
e1b7e62a | 183 | { .subsystem = "ppdev", .device = NULL }, |
f0713480 KS |
184 | { .subsystem = "nvidia", .device = NULL }, |
185 | { .subsystem = "video4linux", .device = "vbi" }, | |
56b979e0 | 186 | { .subsystem = "dvb", .device = NULL }, |
f0713480 KS |
187 | { .subsystem = "lirc", .device = NULL }, |
188 | { .subsystem = "firmware", .device = NULL }, | |
189 | { .subsystem = "drm", .device = NULL }, | |
190 | { .subsystem = "pci_bus", .device = NULL }, | |
191 | { .subsystem = "ieee1394", .device = NULL }, | |
192 | { .subsystem = "ieee1394_host", .device = NULL }, | |
193 | { .subsystem = "ieee1394_node", .device = NULL }, | |
194 | { .subsystem = "raw", .device = NULL }, | |
6628a2ea | 195 | { .subsystem = "zaptel", .device = NULL }, |
56b979e0 KS |
196 | { .subsystem = "tiglusb", .device = NULL }, |
197 | { .subsystem = "ppdev", .device = NULL }, | |
198 | { .subsystem = "ticables", .device = NULL }, | |
199 | { .subsystem = "snsc", .device = NULL }, | |
200 | { .subsystem = "staliomem", .device = NULL }, | |
201 | { .subsystem = "tape", .device = NULL }, | |
202 | { .subsystem = "ip2", .device = NULL }, | |
203 | { .subsystem = "tpqic02", .device = NULL }, | |
204 | { .subsystem = "dsp56k", .device = NULL }, | |
205 | { .subsystem = "zft", .device = NULL }, | |
206 | { .subsystem = "adb", .device = NULL }, | |
207 | { .subsystem = "cosa", .device = NULL }, | |
208 | { .subsystem = "pg", .device = NULL }, | |
209 | { .subsystem = "pt", .device = NULL }, | |
210 | { .subsystem = "capi", .device = NULL }, | |
f0713480 KS |
211 | { NULL, NULL } |
212 | }; | |
a4f0cc79 | 213 | const struct class_device *classdevice; |
f0713480 KS |
214 | int len; |
215 | ||
d402af7d KS |
216 | /* the kernel may tell us what to wait for */ |
217 | if (kernel_release_satisfactory(2,6,10) > 0) | |
218 | if (getenv("PHYSDEVPATH") == NULL) { | |
219 | dbg("the kernel says, that there is no physical device for '%s'", class_dev->path); | |
220 | return 1; | |
221 | } | |
222 | ||
f0713480 KS |
223 | for (classdevice = class_device; classdevice->subsystem != NULL; classdevice++) { |
224 | if (strcmp(class_dev->classname, classdevice->subsystem) == 0) { | |
225 | /* see if no device in this class is expected to have a device-link */ | |
226 | if (classdevice->device == NULL) | |
227 | return 1; | |
228 | ||
229 | len = strlen(classdevice->device); | |
230 | ||
231 | /* see if device name matches */ | |
232 | if (strncmp(class_dev->name, classdevice->device, len) != 0) | |
233 | continue; | |
234 | ||
235 | /* exact name match */ | |
236 | if (strlen(class_dev->name) == len) | |
237 | return 1; | |
238 | ||
239 | /* name match with instance number */ | |
240 | if (isdigit(class_dev->name[len])) | |
241 | return 1; | |
242 | } | |
243 | } | |
244 | ||
245 | return 0; | |
246 | } | |
247 | ||
d402af7d | 248 | /* skip waiting for the bus of the devices device */ |
f0713480 KS |
249 | static int class_device_expect_no_bus(struct sysfs_class_device *class_dev) |
250 | { | |
a4f0cc79 | 251 | static const char *devices_without_bus[] = { |
f0713480 KS |
252 | "scsi_host", |
253 | "i2c-adapter", | |
a258d159 | 254 | "i2c-dev", |
f0713480 KS |
255 | NULL |
256 | }; | |
a4f0cc79 | 257 | const char **device; |
f0713480 KS |
258 | |
259 | for (device = devices_without_bus; *device != NULL; device++) { | |
260 | int len = strlen(*device); | |
261 | ||
262 | if (strncmp(class_dev->classname, *device, len) == 0) | |
263 | return 1; | |
264 | } | |
265 | ||
266 | return 0; | |
267 | } | |
268 | ||
d402af7d KS |
269 | /* wait for a devices device specific file to show up */ |
270 | int wait_for_devices_device(struct sysfs_device *devices_dev, | |
f0713480 KS |
271 | const char **error) |
272 | { | |
a4f0cc79 KS |
273 | static const struct device_file { |
274 | const char *bus; | |
275 | const char *file; | |
d402af7d | 276 | } device_files[] = { |
f0713480 KS |
277 | { .bus = "scsi", .file = "vendor" }, |
278 | { .bus = "usb", .file = "idVendor" }, | |
279 | { .bus = "usb", .file = "iInterface" }, | |
280 | { .bus = "usb", .file = "bNumEndpoints" }, | |
281 | { .bus = "usb-serial", .file = "detach_state" }, | |
282 | { .bus = "ide", .file = "detach_state" }, | |
283 | { .bus = "pci", .file = "vendor" }, | |
284 | { .bus = "platform", .file = "detach_state" }, | |
d2fe701e | 285 | { .bus = "pcmcia", .file = "detach_state" }, |
f0713480 | 286 | { .bus = "i2c", .file = "detach_state" }, |
1ad9a8c1 KS |
287 | { .bus = "ieee1394", .file = "node_count" }, |
288 | { .bus = "ieee1394", .file = "nodeid" }, | |
289 | { .bus = "ieee1394", .file = "address" }, | |
7a0643a9 | 290 | { .bus = "bttv-sub", .file = NULL }, |
8474ff50 KS |
291 | { .bus = "pnp", .file = "detach_state" }, |
292 | { .bus = "eisa", .file = "detach_state" }, | |
293 | { .bus = "pseudo", .file = "detach_state" }, | |
294 | { .bus = "mmc", .file = "detach_state" }, | |
295 | { .bus = "macio", .file = "detach_state" }, | |
296 | { .bus = "of_platform", .file = "detach_state" }, | |
297 | { .bus = "vio", .file = "detach_state" }, | |
298 | { .bus = "ecard", .file = "detach_state" }, | |
299 | { .bus = "sa1111-rab", .file = "detach_state" }, | |
300 | { .bus = "amba", .file = "detach_state" }, | |
301 | { .bus = "locomo-bus", .file = "detach_state" }, | |
302 | { .bus = "logicmodule", .file = "detach_state" }, | |
303 | { .bus = "parisc", .file = "detach_state" }, | |
304 | { .bus = "ocp", .file = "detach_state" }, | |
305 | { .bus = "dio", .file = "detach_state" }, | |
306 | { .bus = "MCA", .file = "detach_state" }, | |
307 | { .bus = "wl", .file = "detach_state" }, | |
308 | { .bus = "ccwgroup", .file = "detach_state" }, | |
309 | { .bus = "css", .file = "detach_state" }, | |
310 | { .bus = "ccw", .file = "detach_state" }, | |
311 | { .bus = "iucv", .file = "detach_state" }, | |
1ad9a8c1 | 312 | { NULL, NULL } |
f0713480 | 313 | }; |
a4f0cc79 | 314 | const struct device_file *devicefile; |
f0713480 KS |
315 | int loop; |
316 | ||
d402af7d KS |
317 | /* the kernel may tell us what to wait for */ |
318 | if (kernel_release_satisfactory(2,6,10) > 0) | |
319 | if (getenv("PHYSDEVBUS") == NULL) { | |
320 | dbg("the kernel says, that there is no bus for '%s'", devices_dev->path); | |
321 | return 0; | |
322 | } | |
323 | ||
f0713480 KS |
324 | /* wait for the bus device link to the devices device */ |
325 | loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND; | |
326 | while (--loop) { | |
327 | if (sysfs_get_device_bus(devices_dev) == 0) | |
328 | break; | |
329 | ||
330 | usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND); | |
331 | } | |
332 | if (loop == 0) { | |
333 | dbg("error: getting bus device link"); | |
334 | if (error) | |
335 | *error = "no bus device link"; | |
336 | return -1; | |
337 | } | |
338 | dbg("bus device link found for bus '%s'", devices_dev->bus); | |
339 | ||
d402af7d | 340 | /* wait for a bus device specific file to show up */ |
f0713480 KS |
341 | loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND; |
342 | while (--loop) { | |
1ad9a8c1 | 343 | int found_bus_type = 0; |
f0713480 | 344 | |
d402af7d KS |
345 | for (devicefile = device_files; devicefile->bus != NULL; devicefile++) { |
346 | if (strcmp(devices_dev->bus, devicefile->bus) == 0) { | |
1ad9a8c1 KS |
347 | char filename[SYSFS_PATH_MAX]; |
348 | struct stat stats; | |
349 | ||
7a0643a9 KS |
350 | if (devicefile->file == NULL) { |
351 | dbg("bus '%s' has no file to wait for", devices_dev->bus); | |
352 | return 0; | |
353 | } | |
354 | ||
1ad9a8c1 | 355 | found_bus_type = 1; |
d402af7d KS |
356 | snprintf(filename, SYSFS_PATH_MAX-1, "%s/%s", devices_dev->path, devicefile->file); |
357 | dbg("looking at bus '%s' device for specific file '%s'", devices_dev->bus, filename); | |
1ad9a8c1 KS |
358 | |
359 | if (stat(filename, &stats) == 0) { | |
d402af7d | 360 | dbg("bus '%s' device specific file '%s' found", devices_dev->bus, devicefile->file); |
f0713480 KS |
361 | return 0; |
362 | } | |
363 | } | |
364 | } | |
1ad9a8c1 | 365 | if (found_bus_type == 0) { |
f0713480 KS |
366 | if (error) |
367 | *error = "unknown bus"; | |
368 | info("error: unknown bus, please report to " | |
369 | "<linux-hotplug-devel@lists.sourceforge.net> '%s'", devices_dev->bus); | |
370 | return -1; | |
371 | } | |
372 | usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND); | |
373 | } | |
374 | ||
d402af7d | 375 | dbg("error: getting '%s' device specific file '%s'", devices_dev->bus, devicefile->file); |
f0713480 | 376 | if (error) |
d402af7d | 377 | *error = "bus device specific file unavailable"; |
f0713480 KS |
378 | return -1; |
379 | } | |
380 | ||
381 | ||
d402af7d | 382 | struct sysfs_class_device *wait_class_device_open(const char *path) |
f0713480 KS |
383 | { |
384 | struct sysfs_class_device *class_dev; | |
385 | int loop; | |
386 | ||
387 | loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND; | |
388 | while (--loop) { | |
389 | class_dev = sysfs_open_class_device_path(path); | |
390 | if (class_dev) | |
391 | break; | |
392 | ||
393 | usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND); | |
394 | } | |
395 | ||
396 | return (class_dev); | |
397 | } | |
398 | ||
399 | int wait_for_class_device(struct sysfs_class_device *class_dev, | |
400 | const char **error) | |
401 | { | |
402 | struct sysfs_class_device *class_dev_parent; | |
403 | struct sysfs_device *devices_dev = NULL; | |
404 | int loop; | |
405 | ||
406 | if (wait_for_class_device_attributes(class_dev, error) != 0) | |
407 | return -ENOENT; | |
408 | ||
409 | /* skip devices without devices-link */ | |
410 | if (class_device_expect_no_device_link(class_dev)) { | |
411 | dbg("no device symlink expected for '%s', ", class_dev->name); | |
d402af7d | 412 | return 0; |
f0713480 KS |
413 | } |
414 | ||
415 | /* the symlink may be on the parent device */ | |
416 | class_dev_parent = sysfs_get_classdev_parent(class_dev); | |
417 | if (class_dev_parent) | |
418 | dbg("looking at parent device for device link '%s'", class_dev_parent->path); | |
419 | ||
420 | /* wait for the symlink to the devices device */ | |
421 | dbg("waiting for symlink to devices device"); | |
422 | loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND; | |
423 | while (--loop) { | |
424 | if (class_dev_parent) | |
425 | devices_dev = sysfs_get_classdev_device(class_dev_parent); | |
426 | else | |
427 | devices_dev = sysfs_get_classdev_device(class_dev); | |
428 | ||
429 | if (devices_dev) | |
430 | break; | |
431 | ||
432 | usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND); | |
433 | } | |
434 | if (!devices_dev) { | |
435 | dbg(" error: no devices device symlink found"); | |
436 | if (error) | |
437 | *error = "no device symlink"; | |
438 | return -ENODEV; | |
439 | } | |
440 | dbg("device symlink found pointing to '%s'", devices_dev->path); | |
441 | ||
d402af7d | 442 | /* wait for the devices device */ |
f0713480 KS |
443 | if (class_device_expect_no_bus(class_dev)) { |
444 | dbg("no bus device expected for '%s', ", class_dev->classname); | |
445 | return 0; | |
f0713480 | 446 | } |
d402af7d KS |
447 | |
448 | return wait_for_devices_device(devices_dev, error); | |
f0713480 KS |
449 | } |
450 | ||
d402af7d | 451 | struct sysfs_device *wait_devices_device_open(const char *path) |
f0713480 KS |
452 | { |
453 | struct sysfs_device *devices_dev; | |
454 | int loop; | |
455 | ||
456 | loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND; | |
457 | while (--loop) { | |
458 | devices_dev = sysfs_open_device_path(path); | |
459 | if (devices_dev) | |
460 | break; | |
461 | ||
462 | usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND); | |
463 | } | |
464 | ||
465 | return(devices_dev); | |
466 | } |