chiark / gitweb /
libsysfs: remove brute-force "bus", "driver" searching for old kernels
[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 dev_t get_devt(struct sysfs_class_device *class_dev)
39 {
40         struct sysfs_attribute *attr = NULL;
41         unsigned int major, minor;
42
43         attr = sysfs_get_classdev_attr(class_dev, "dev");
44         if (attr == NULL)
45                 return 0;
46         dbg("dev='%s'", attr->value);
47
48         if (sscanf(attr->value, "%u:%u", &major, &minor) != 2)
49                 return 0;
50         dbg("found major=%d, minor=%d", major, minor);
51
52         return makedev(major, minor);
53 }
54
55 /* wait for a devices device specific file to show up */
56 int wait_for_devices_device(struct sysfs_device *devices_dev,
57                         const char **error)
58 {
59         static const struct device_file {
60                 const char *bus;
61                 const char *file;
62         } device_files[] = {
63                 { .bus = "scsi",        .file = "vendor" },
64                 { .bus = "usb",         .file = "idVendor" },
65                 { .bus = "usb",         .file = "iInterface" },
66                 { .bus = "usb",         .file = "bNumEndpoints" },
67                 { .bus = "usb-serial",  .file = "bus" },
68                 { .bus = "ide",         .file = "bus" },
69                 { .bus = "pci",         .file = "vendor" },
70                 { .bus = "pci_express", .file = "bus" },
71                 { .bus = "platform",    .file = "bus" },
72                 { .bus = "pcmcia",      .file = "bus" },
73                 { .bus = "i2c",         .file = "bus" },
74                 { .bus = "ieee1394",    .file = "node_count" },
75                 { .bus = "ieee1394",    .file = "nodeid" },
76                 { .bus = "ieee1394",    .file = "address" },
77                 { .bus = "bttv-sub",    .file = NULL },
78                 { .bus = "pnp",         .file = "bus" },
79                 { .bus = "eisa",        .file = "bus" },
80                 { .bus = "serio",       .file = "bus" },
81                 { .bus = "pseudo",      .file = "bus" },
82                 { .bus = "mmc",         .file = "bus" },
83                 { .bus = "macio",       .file = "bus" },
84                 { .bus = "of_platform", .file = "bus" },
85                 { .bus = "vio",         .file = "bus" },
86                 { .bus = "ecard",       .file = "bus" },
87                 { .bus = "sa1111-rab",  .file = "bus" },
88                 { .bus = "amba",        .file = "bus" },
89                 { .bus = "locomo-bus",  .file = "bus" },
90                 { .bus = "logicmodule", .file = "bus" },
91                 { .bus = "parisc",      .file = "bus" },
92                 { .bus = "ocp",         .file = "bus" },
93                 { .bus = "dio",         .file = "bus" },
94                 { .bus = "MCA",         .file = "bus" },
95                 { .bus = "wl",          .file = "bus" },
96                 { .bus = "ccwgroup",    .file = "bus" },
97                 { .bus = "css",         .file = "bus" },
98                 { .bus = "ccw",         .file = "bus" },
99                 { .bus = "iucv",        .file = "bus" },
100                 { NULL, NULL }
101         };
102         const struct device_file *devicefile = NULL;
103         int loop;
104
105         if (getenv("PHYSDEVBUS") == NULL) {
106                 dbg("the kernel says, that there is no bus for '%s'", devices_dev->path);
107                 return 0;
108         }
109
110         /* wait for the bus device link to the devices device */
111         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
112         while (--loop) {
113                 if (sysfs_get_device_bus(devices_dev) == 0)
114                         break;
115
116                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
117         }
118         if (loop == 0) {
119                 dbg("error: getting bus device link");
120                 if (error)
121                         *error = "no bus device link";
122                 return -1;
123         }
124         dbg("bus device link found for bus '%s'", devices_dev->bus);
125
126         /* wait for a bus device specific file to show up */
127         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
128         while (--loop) {
129                 int found_bus_type = 0;
130
131                 for (devicefile = device_files; devicefile->bus != NULL; devicefile++) {
132                         if (strcmp(devices_dev->bus, devicefile->bus) == 0) {
133                                 char filename[PATH_SIZE];
134                                 struct stat stats;
135
136                                 if (devicefile->file == NULL) {
137                                         dbg("bus '%s' has no file to wait for", devices_dev->bus);
138                                         return 0;
139                                 }
140
141                                 found_bus_type = 1;
142                                 snprintf(filename, sizeof(filename), "%s/%s", devices_dev->path, devicefile->file);
143                                 filename[sizeof(filename)-1] = '\0';
144                                 dbg("looking at bus '%s' device for specific file '%s'", devices_dev->bus, filename);
145
146                                 if (stat(filename, &stats) == 0) {
147                                         dbg("bus '%s' device specific file '%s' found", devices_dev->bus, devicefile->file);
148                                         return 0;
149                                 }
150                         }
151                 }
152                 if (found_bus_type == 0) {
153                         if (error)
154                                 *error = "unknown bus";
155                         info("error: unknown bus, please report to "
156                              "<linux-hotplug-devel@lists.sourceforge.net> '%s'", devices_dev->bus);
157                         return -1;
158                 }
159                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
160         }
161
162         dbg("error: getting '%s' device specific file '%s'", devices_dev->bus, devicefile->file);
163         if (error)
164                 *error = "bus device specific file unavailable";
165         return -1;
166 }
167
168
169 struct sysfs_class_device *wait_class_device_open(const char *path)
170 {
171         struct sysfs_class_device *class_dev = NULL;
172         int loop;
173
174         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
175         while (--loop) {
176                 class_dev = sysfs_open_class_device_path(path);
177                 if (class_dev)
178                         break;
179
180                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
181         }
182
183         return class_dev;
184 }
185
186 int wait_for_class_device(struct sysfs_class_device *class_dev,
187                           const char **error)
188 {
189         const struct subsystem_file {
190                 const char *subsystem;
191                 const char *file;
192         } subsystem_files[] = {
193                 { .subsystem = "net",           .file = "ifindex" },
194                 { .subsystem = "scsi_host",     .file = "unique_id" },
195                 { .subsystem = "pcmcia_socket", .file = "card_type" },
196                 { .subsystem = "bluetooth",     .file = "address" },
197                 { .subsystem = "firmware",      .file = "data" },
198                 { .subsystem = "fc_transport",  .file = "port_id" },
199                 { .subsystem = "fc_host",       .file = "port_id" },
200                 { .subsystem = "spi_transport", .file = "width" },
201                 { .subsystem = "spi_host",      .file = "width" },
202                 { NULL, NULL }
203         };
204
205         const struct subsystem_file *subsys_file;
206         struct sysfs_class_device *class_dev_parent;
207         struct sysfs_device *devices_dev = NULL;
208         char filename[PATH_SIZE];
209         int loop;
210
211         /* look if we want to wait for a file  */
212         for (subsys_file = subsystem_files; subsys_file->subsystem != NULL; subsys_file++)
213                 if (strcmp(class_dev->classname, subsys_file->subsystem) == 0)
214                         break;
215
216         if (subsys_file->file == NULL) {
217                 dbg("class '%s' has no file to wait for", class_dev->classname);
218                 return 0;
219         }
220
221         snprintf(filename, sizeof(filename), "%s/%s", class_dev->path, subsys_file->file);
222         filename[sizeof(filename)-1] = '\0';
223         dbg("looking at class '%s' for specific file '%s'", class_dev->classname, filename);
224
225         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
226         while (--loop) {
227                 struct stat stats;
228
229                 if (stat(class_dev->path, &stats) == -1) {
230                         dbg("'%s' now disappeared (probably remove has beaten us)", class_dev->path);
231                         return -ENODEV;
232                 }
233
234                 if (stat(filename, &stats) == 0) {
235                         dbg("class '%s' specific file '%s' found", class_dev->classname, subsys_file->file);
236                         return 0;
237                 }
238
239                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
240         }
241
242         dbg("error: getting class '%s' specific file '%s'", class_dev->classname, subsys_file->file);
243         if (error)
244                 *error = "class specific file unavailable";
245         return -1;
246
247         /* skip devices without devices-link */
248         if (getenv("PHYSDEVPATH") == NULL) {
249                 dbg("the kernel says, that there is no physical device for '%s'", class_dev->path);
250                 return 1;
251         }
252
253         /* the symlink may be on the parent device */
254         class_dev_parent = sysfs_get_classdev_parent(class_dev);
255         if (class_dev_parent)
256                 dbg("looking at parent device for device link '%s'", class_dev_parent->path);
257
258         /* wait for the symlink to the devices device */
259         dbg("waiting for symlink to devices device");
260         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
261         while (--loop) {
262                 if (class_dev_parent)
263                         devices_dev = sysfs_get_classdev_device(class_dev_parent);
264                 else
265                         devices_dev = sysfs_get_classdev_device(class_dev);
266
267                 if (devices_dev)
268                         break;
269
270                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
271         }
272         if (!devices_dev) {
273                 dbg(" error: no devices device symlink found");
274                 if (error)
275                         *error = "no device symlink";
276                 return -ENODEV;
277         }
278         dbg("device symlink found pointing to '%s'", devices_dev->path);
279
280         return wait_for_devices_device(devices_dev, error);
281 }
282
283 struct sysfs_device *wait_devices_device_open(const char *path)
284 {
285         struct sysfs_device *devices_dev = NULL;
286         int loop;
287
288         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
289         while (--loop) {
290                 devices_dev = sysfs_open_device_path(path);
291                 if (devices_dev)
292                         break;
293
294                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
295         }
296
297         return devices_dev;
298 }