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