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