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