chiark / gitweb /
[PATCH] link archive insted of objects
[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         { NULL, NULL }
55 };
56
57 int subsystem_expect_no_dev(const char *subsystem)
58 {
59         const struct subsystem_file *file;
60
61         for (file = subsystem_files; file->subsystem != NULL; file++)
62                 if (strcmp(subsystem, file->subsystem) == 0)
63                         return 1;
64
65         return 0;
66 }
67
68 /* get subsystem specific files, returns "dev" if no other found */
69 static const char *get_subsystem_specific_file(const char *subsystem)
70 {
71         const struct subsystem_file *file;
72
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)
76                         return file->file;
77
78         return "dev";
79 }
80
81 /* wait for class pecific file to show up */
82 static int wait_for_class_device_attributes(struct sysfs_class_device *class_dev,
83                                             const char **error)
84 {
85         const char *file;
86         char filename[SYSFS_PATH_MAX];
87         int loop;
88
89         file = get_subsystem_specific_file(class_dev->classname);
90         if (file == NULL) {
91                 dbg("class '%s' has no file to wait for", class_dev->classname);
92                 return 0;
93         }
94
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);
97
98         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
99         while (--loop) {
100                 struct stat stats;
101
102                 if (stat(class_dev->path, &stats) == -1) {
103                         dbg("'%s' now disappeared (probably remove has beaten us)", class_dev->path);
104                         return -ENODEV;
105                 }
106
107                 if (stat(filename, &stats) == 0) {
108                         dbg("class '%s' specific file '%s' found", class_dev->classname, file);
109                         return 0;
110                 }
111
112                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
113         }
114
115         dbg("error: getting class '%s' specific file '%s'", class_dev->classname, file);
116         if (error)
117                 *error = "class specific file unavailable";
118         return -ENOENT;
119 }
120
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)
123 {
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;
128                 const char *device;
129         } class_device[] = {
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 },
203                 { NULL, NULL }
204         };
205         const struct class_device *classdevice;
206         int len;
207
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);
212                         return 1;
213                 }
214
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)
219                                 return 1;
220
221                         len = strlen(classdevice->device);
222
223                         /* see if device name matches */
224                         if (strncmp(class_dev->name, classdevice->device, len) != 0)
225                                 continue;
226
227                         /* exact name match */
228                         if (strlen(class_dev->name) == len)
229                                 return 1;
230
231                         /* name match with instance number */
232                         if (isdigit(class_dev->name[len]))
233                                 return 1;
234                 }
235         }
236
237         return 0;
238 }
239
240 /* skip waiting for the bus of the devices device */
241 static int class_device_expect_no_bus(struct sysfs_class_device *class_dev)
242 {
243         static const char *devices_without_bus[] = {
244                 "scsi_host",
245                 "i2c-adapter",
246                 "i2c-dev",
247                 NULL
248         };
249         const char **device;
250
251         for (device = devices_without_bus; *device != NULL; device++) {
252                 int len = strlen(*device);
253
254                 if (strncmp(class_dev->classname, *device, len) == 0)
255                         return 1;
256         }
257
258         return 0;
259 }
260
261 /* wait for a devices device specific file to show up */
262 int wait_for_devices_device(struct sysfs_device *devices_dev,
263                         const char **error)
264 {
265         static const struct device_file {
266                 const char *bus;
267                 const char *file;
268         } device_files[] = {
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" },
303                 { NULL, NULL }
304         };
305         const struct device_file *devicefile;
306         int loop;
307
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);
312                         return 0;
313                 }
314
315         /* wait for the bus device link to the devices device */
316         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
317         while (--loop) {
318                 if (sysfs_get_device_bus(devices_dev) == 0)
319                         break;
320
321                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
322         }
323         if (loop == 0) {
324                 dbg("error: getting bus device link");
325                 if (error)
326                         *error = "no bus device link";
327                 return -1;
328         }
329         dbg("bus device link found for bus '%s'", devices_dev->bus);
330
331         /* wait for a bus device specific file to show up */
332         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
333         while (--loop) {
334                 int found_bus_type = 0;
335
336                 for (devicefile = device_files; devicefile->bus != NULL; devicefile++) {
337                         if (strcmp(devices_dev->bus, devicefile->bus) == 0) {
338                                 char filename[SYSFS_PATH_MAX];
339                                 struct stat stats;
340
341                                 if (devicefile->file == NULL) {
342                                         dbg("bus '%s' has no file to wait for", devices_dev->bus);
343                                         return 0;
344                                 }
345
346                                 found_bus_type = 1;
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);
349
350                                 if (stat(filename, &stats) == 0) {
351                                         dbg("bus '%s' device specific file '%s' found", devices_dev->bus, devicefile->file);
352                                         return 0;
353                                 }
354                         }
355                 }
356                 if (found_bus_type == 0) {
357                         if (error)
358                                 *error = "unknown bus";
359                         info("error: unknown bus, please report to "
360                              "<linux-hotplug-devel@lists.sourceforge.net> '%s'", devices_dev->bus);
361                         return -1;
362                 }
363                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
364         }
365
366         dbg("error: getting '%s' device specific file '%s'", devices_dev->bus, devicefile->file);
367         if (error)
368                 *error = "bus device specific file unavailable";
369         return -1;
370 }
371
372
373 struct sysfs_class_device *wait_class_device_open(const char *path)
374 {
375         struct sysfs_class_device *class_dev;
376         int loop;
377
378         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
379         while (--loop) {
380                 class_dev = sysfs_open_class_device_path(path);
381                 if (class_dev)
382                         break;
383
384                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
385         }
386
387         return (class_dev);
388 }
389
390 int wait_for_class_device(struct sysfs_class_device *class_dev,
391                           const char **error)
392 {
393         struct sysfs_class_device *class_dev_parent;
394         struct sysfs_device *devices_dev = NULL;
395         int loop;
396
397         if (wait_for_class_device_attributes(class_dev, error) != 0)
398                 return -ENOENT;
399
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);
403                 return 0;
404         }
405
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);
410
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;
414         while (--loop) {
415                 if (class_dev_parent)
416                         devices_dev = sysfs_get_classdev_device(class_dev_parent);
417                 else
418                         devices_dev = sysfs_get_classdev_device(class_dev);
419
420                 if (devices_dev)
421                         break;
422
423                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
424         }
425         if (!devices_dev) {
426                 dbg(" error: no devices device symlink found");
427                 if (error)
428                         *error = "no device symlink";
429                 return -ENODEV;
430         }
431         dbg("device symlink found pointing to '%s'", devices_dev->path);
432
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);
436                 return 0;
437         }
438
439         return wait_for_devices_device(devices_dev, error);
440 }
441
442 struct sysfs_device *wait_devices_device_open(const char *path)
443 {
444         struct sysfs_device *devices_dev;
445         int loop;
446
447         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
448         while (--loop) {
449                 devices_dev = sysfs_open_device_path(path);
450                 if (devices_dev)
451                         break;
452
453                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
454         }
455
456         return(devices_dev);
457 }