chiark / gitweb /
d849a8eba7199f68b9dd85bde516ecc0ae84010e
[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 "logging.h"
32 #include "udev_version.h"
33 #include "udev_sysfs.h"
34 #include "libsysfs/sysfs/libsysfs.h"
35
36 /* list of subsystem specific files
37  * NULL if there is no file to wait for
38  */
39 static struct subsystem_file {
40         char *subsystem;
41         char *file;
42 } subsystem_files[] = {
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 },
55         { NULL, NULL }
56 };
57
58 int subsystem_expect_no_dev(const char *subsystem)
59 {
60         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 char *get_subsystem_specific_file(const char *subsystem)
71 {
72         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 struct class_device {
128                 char *subsystem;
129                 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 = "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 = "ppp",           .device = NULL },
170                 { .subsystem = "sound",         .device = NULL },
171                 { .subsystem = "printer",       .device = "lp" },
172                 { .subsystem = "nvidia",        .device = NULL },
173                 { .subsystem = "video4linux",   .device = "vbi" },
174                 { .subsystem = "lirc",          .device = NULL },
175                 { .subsystem = "firmware",      .device = NULL },
176                 { .subsystem = "drm",           .device = NULL },
177                 { .subsystem = "pci_bus",       .device = NULL },
178                 { .subsystem = "ieee1394",      .device = NULL },
179                 { .subsystem = "ieee1394_host", .device = NULL },
180                 { .subsystem = "ieee1394_node", .device = NULL },
181                 { .subsystem = "raw",           .device = NULL },
182                 { NULL, NULL }
183         };
184         struct class_device *classdevice;
185         int len;
186
187         for (classdevice = class_device; classdevice->subsystem != NULL; classdevice++) {
188                 if (strcmp(class_dev->classname, classdevice->subsystem) == 0) {
189                         /* see if no device in this class is expected to have a device-link */
190                         if (classdevice->device == NULL)
191                                 return 1;
192
193                         len = strlen(classdevice->device);
194
195                         /* see if device name matches */
196                         if (strncmp(class_dev->name, classdevice->device, len) != 0)
197                                 continue;
198
199                         /* exact name match */
200                         if (strlen(class_dev->name) == len)
201                                 return 1;
202
203                         /* name match with instance number */
204                         if (isdigit(class_dev->name[len]))
205                                 return 1;
206                 }
207         }
208
209         return 0;
210 }
211
212 /* skip waiting for the bus */
213 static int class_device_expect_no_bus(struct sysfs_class_device *class_dev)
214 {
215         static char *devices_without_bus[] = {
216                 "scsi_host",
217                 "i2c-adapter",
218                 NULL
219         };
220         char **device;
221
222         for (device = devices_without_bus; *device != NULL; device++) {
223                 int len = strlen(*device);
224
225                 if (strncmp(class_dev->classname, *device, len) == 0)
226                         return 1;
227         }
228
229         return 0;
230 }
231
232 /* wait for the bus and for a bus specific file to show up */
233 int wait_for_bus_device(struct sysfs_device *devices_dev,
234                         const char **error)
235 {
236         static struct bus_file {
237                 char *bus;
238                 char *file;
239         } bus_files[] = {
240                 { .bus = "scsi",        .file = "vendor" },
241                 { .bus = "usb",         .file = "idVendor" },
242                 { .bus = "usb",         .file = "iInterface" },
243                 { .bus = "usb",         .file = "bNumEndpoints" },
244                 { .bus = "usb-serial",  .file = "detach_state" },
245                 { .bus = "ide",         .file = "detach_state" },
246                 { .bus = "pci",         .file = "vendor" },
247                 { .bus = "platform",    .file = "detach_state" },
248                 { .bus = "i2c",         .file = "detach_state" },
249                 { .bus = "ieee1394",    .file = "node_count" },
250                 { .bus = "ieee1394",    .file = "nodeid" },
251                 { .bus = "ieee1394",    .file = "address" },
252                 { NULL, NULL }
253         };
254         struct bus_file *busfile;
255         int loop;
256
257         /* wait for the bus device link to the devices device */
258         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
259         while (--loop) {
260                 if (sysfs_get_device_bus(devices_dev) == 0)
261                         break;
262
263                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
264         }
265         if (loop == 0) {
266                 dbg("error: getting bus device link");
267                 if (error)
268                         *error = "no bus device link";
269                 return -1;
270         }
271         dbg("bus device link found for bus '%s'", devices_dev->bus);
272
273         /* wait for a bus specific file to show up */
274         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
275         while (--loop) {
276                 int found_bus_type = 0;
277
278                 for (busfile = bus_files; busfile->bus != NULL; busfile++) {
279                         if (strcmp(devices_dev->bus, busfile->bus) == 0) {
280                                 char filename[SYSFS_PATH_MAX];
281                                 struct stat stats;
282
283                                 found_bus_type = 1;
284                                 snprintf(filename, SYSFS_PATH_MAX-1, "%s/%s", devices_dev->path, busfile->file);
285                                 dbg("looking at bus '%s' for specific file '%s'", devices_dev->bus, filename);
286
287                                 if (stat(filename, &stats) == 0) {
288                                         dbg("bus '%s' specific file '%s' found", devices_dev->bus, busfile->file);
289                                         return 0;
290                                 }
291                         }
292                 }
293                 if (found_bus_type == 0) {
294                         if (error)
295                                 *error = "unknown bus";
296                         info("error: unknown bus, please report to "
297                              "<linux-hotplug-devel@lists.sourceforge.net> '%s'", devices_dev->bus);
298                         return -1;
299                 }
300                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
301         }
302
303         dbg("error: getting bus '%s' specific file '%s'", devices_dev->bus, busfile->file);
304         if (error)
305                 *error = "bus specific file unavailable";
306         return -1;
307 }
308
309
310 struct sysfs_class_device *open_class_device_wait(const char *path)
311 {
312         struct sysfs_class_device *class_dev;
313         int loop;
314
315         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
316         while (--loop) {
317                 class_dev = sysfs_open_class_device_path(path);
318                 if (class_dev)
319                         break;
320
321                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
322         }
323
324         return (class_dev);
325 }
326
327 int wait_for_class_device(struct sysfs_class_device *class_dev,
328                           const char **error)
329 {
330         struct sysfs_class_device *class_dev_parent;
331         struct sysfs_device *devices_dev = NULL;
332         int loop;
333
334         if (wait_for_class_device_attributes(class_dev, error) != 0)
335                 return -ENOENT;
336
337         /* skip devices without devices-link */
338         if (class_device_expect_no_device_link(class_dev)) {
339                 dbg("no device symlink expected for '%s', ", class_dev->name);
340                 return -ENODEV;
341         }
342
343         /* the symlink may be on the parent device */
344         class_dev_parent = sysfs_get_classdev_parent(class_dev);
345         if (class_dev_parent)
346                 dbg("looking at parent device for device link '%s'", class_dev_parent->path);
347
348         /* wait for the symlink to the devices device */
349         dbg("waiting for symlink to devices device");
350         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
351         while (--loop) {
352                 if (class_dev_parent)
353                         devices_dev = sysfs_get_classdev_device(class_dev_parent);
354                 else
355                         devices_dev = sysfs_get_classdev_device(class_dev);
356
357                 if (devices_dev)
358                         break;
359
360                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
361         }
362         if (!devices_dev) {
363                 dbg(" error: no devices device symlink found");
364                 if (error)
365                         *error = "no device symlink";
366                 return -ENODEV;
367         }
368         dbg("device symlink found pointing to '%s'", devices_dev->path);
369
370         /* wait for the bus value */
371         if (class_device_expect_no_bus(class_dev)) {
372                 dbg("no bus device expected for '%s', ", class_dev->classname);
373                 return 0;
374         } else {
375                 return wait_for_bus_device(devices_dev, error);
376         }
377 }
378
379 struct sysfs_device *open_devices_device_wait(const char *path)
380 {
381         struct sysfs_device *devices_dev;
382         int loop;
383
384         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
385         while (--loop) {
386                 devices_dev = sysfs_open_device_path(path);
387                 if (devices_dev)
388                         break;
389
390                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
391         }
392
393         return(devices_dev);
394 }