chiark / gitweb /
[PATCH] expose sysfs functions for sharing it
[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         strcpy(filename, class_dev->path);
97         strcat(filename, "/");
98         strcat(filename, file);
99         dbg("looking at class '%s' for specific file '%s' with full name %s", class_dev->classname, file, filename);
100
101         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
102         while (--loop) {
103                 struct stat stats;
104
105                 if (stat(class_dev->path, &stats) == -1) {
106                         dbg("'%s' now disappeared (probably remove has beaten us)", class_dev->path);
107                         return -ENODEV;
108                 }
109
110                 if (stat(filename, &stats) == 0) {
111                         dbg("class '%s' specific file '%s' found", class_dev->classname, file);
112                         return 0;
113                 }
114
115                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
116         }
117
118         dbg("error: getting class '%s' specific file '%s'", class_dev->classname, file);
119         if (error)
120                 *error = "class specific file unavailable";
121         return -ENOENT;
122 }
123
124 /* check if we need to wait for a physical device */
125 static int class_device_expect_no_device_link(struct sysfs_class_device *class_dev)
126 {
127         /* list of devices without a "device" symlink to the physical device
128          * if device is set to NULL, no devices in that subsystem has a link */
129         static struct class_device {
130                 char *subsystem;
131                 char *device;
132         } class_device[] = {
133                 { .subsystem = "block",         .device = "double" },
134                 { .subsystem = "block",         .device = "nb" },
135                 { .subsystem = "block",         .device = "ram" },
136                 { .subsystem = "block",         .device = "loop" },
137                 { .subsystem = "block",         .device = "fd" },
138                 { .subsystem = "block",         .device = "md" },
139                 { .subsystem = "block",         .device = "dos_cd" },
140                 { .subsystem = "block",         .device = "rflash" },
141                 { .subsystem = "block",         .device = "rom" },
142                 { .subsystem = "block",         .device = "rrom" },
143                 { .subsystem = "block",         .device = "flash" },
144                 { .subsystem = "block",         .device = "msd" },
145                 { .subsystem = "block",         .device = "sbpcd" },
146                 { .subsystem = "block",         .device = "pcd" },
147                 { .subsystem = "block",         .device = "pf" },
148                 { .subsystem = "block",         .device = "scd" },
149                 { .subsystem = "block",         .device = "ubd" },
150                 { .subsystem = "block",         .device = "dm-" },
151                 { .subsystem = "input",         .device = "event" },
152                 { .subsystem = "input",         .device = "mice" },
153                 { .subsystem = "input",         .device = "mouse" },
154                 { .subsystem = "input",         .device = "ts" },
155                 { .subsystem = "vc",            .device = NULL },
156                 { .subsystem = "tty",           .device = NULL },
157                 { .subsystem = "cpuid",         .device = "cpu" },
158                 { .subsystem = "graphics",      .device = "fb" },
159                 { .subsystem = "mem",           .device = NULL },
160                 { .subsystem = "misc",          .device = NULL },
161                 { .subsystem = "msr",           .device = NULL },
162                 { .subsystem = "netlink",       .device = NULL },
163                 { .subsystem = "net",           .device = "sit" },
164                 { .subsystem = "net",           .device = "lo" },
165                 { .subsystem = "net",           .device = "tap" },
166                 { .subsystem = "net",           .device = "ipsec" },
167                 { .subsystem = "net",           .device = "dummy" },
168                 { .subsystem = "net",           .device = "irda" },
169                 { .subsystem = "net",           .device = "ppp" },
170                 { .subsystem = "ppp",           .device = NULL },
171                 { .subsystem = "sound",         .device = NULL },
172                 { .subsystem = "printer",       .device = "lp" },
173                 { .subsystem = "nvidia",        .device = NULL },
174                 { .subsystem = "video4linux",   .device = "vbi" },
175                 { .subsystem = "lirc",          .device = NULL },
176                 { .subsystem = "firmware",      .device = NULL },
177                 { .subsystem = "drm",           .device = NULL },
178                 { .subsystem = "pci_bus",       .device = NULL },
179                 { .subsystem = "ieee1394",      .device = NULL },
180                 { .subsystem = "ieee1394_host", .device = NULL },
181                 { .subsystem = "ieee1394_node", .device = NULL },
182                 { .subsystem = "raw",           .device = NULL },
183                 { NULL, NULL }
184         };
185         struct class_device *classdevice;
186         int len;
187
188         for (classdevice = class_device; classdevice->subsystem != NULL; classdevice++) {
189                 if (strcmp(class_dev->classname, classdevice->subsystem) == 0) {
190                         /* see if no device in this class is expected to have a device-link */
191                         if (classdevice->device == NULL)
192                                 return 1;
193
194                         len = strlen(classdevice->device);
195
196                         /* see if device name matches */
197                         if (strncmp(class_dev->name, classdevice->device, len) != 0)
198                                 continue;
199
200                         /* exact name match */
201                         if (strlen(class_dev->name) == len)
202                                 return 1;
203
204                         /* name match with instance number */
205                         if (isdigit(class_dev->name[len]))
206                                 return 1;
207                 }
208         }
209
210         return 0;
211 }
212
213 /* skip waiting for the bus */
214 static int class_device_expect_no_bus(struct sysfs_class_device *class_dev)
215 {
216         static char *devices_without_bus[] = {
217                 "scsi_host",
218                 "i2c-adapter",
219                 NULL
220         };
221         char **device;
222
223         for (device = devices_without_bus; *device != NULL; device++) {
224                 int len = strlen(*device);
225
226                 if (strncmp(class_dev->classname, *device, len) == 0)
227                         return 1;
228         }
229
230         return 0;
231 }
232
233 /* wait for the bus and for a bus specific file to show up */
234 int wait_for_bus_device(struct sysfs_device *devices_dev,
235                         const char **error)
236 {
237         static struct bus_file {
238                 char *bus;
239                 char *file;
240         } bus_files[] = {
241                 { .bus = "scsi",        .file = "vendor" },
242                 { .bus = "usb",         .file = "idVendor" },
243                 { .bus = "usb",         .file = "iInterface" },
244                 { .bus = "usb",         .file = "bNumEndpoints" },
245                 { .bus = "usb-serial",  .file = "detach_state" },
246                 { .bus = "ide",         .file = "detach_state" },
247                 { .bus = "pci",         .file = "vendor" },
248                 { .bus = "platform",    .file = "detach_state" },
249                 { .bus = "i2c",         .file = "detach_state" },
250                 { NULL }
251         };
252         struct bus_file *busfile;
253         int loop;
254
255         /* wait for the bus device link to the devices device */
256         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
257         while (--loop) {
258                 if (sysfs_get_device_bus(devices_dev) == 0)
259                         break;
260
261                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
262         }
263         if (loop == 0) {
264                 dbg("error: getting bus device link");
265                 if (error)
266                         *error = "no bus device link";
267                 return -1;
268         }
269         dbg("bus device link found for bus '%s'", devices_dev->bus);
270
271         /* wait for a bus specific file to show up */
272         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
273         while (--loop) {
274                 int found = 0;
275
276                 for (busfile = bus_files; busfile->bus != NULL; busfile++) {
277                         if (strcmp(devices_dev->bus, busfile->bus) == 0) {
278                                 found = 1;
279                                 dbg("looking at bus '%s' for specific file '%s'", devices_dev->bus, busfile->file);
280                                 if (sysfs_get_device_attr(devices_dev, busfile->file) != NULL) {
281                                         dbg("bus '%s' specific file '%s' found", devices_dev->bus, busfile->file);
282                                         return 0;
283                                 }
284                         }
285                 }
286                 if (found == 0) {
287                         if (error)
288                                 *error = "unknown bus";
289                         info("error: unknown bus, please report to "
290                              "<linux-hotplug-devel@lists.sourceforge.net> '%s'", devices_dev->bus);
291                         return -1;
292                 }
293                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
294         }
295
296         dbg("error: getting bus '%s' specific file '%s'", devices_dev->bus, busfile->file);
297         if (error)
298                 *error = "bus specific file unavailable";
299         return -1;
300 }
301
302
303 struct sysfs_class_device *open_class_device_wait(const char *path)
304 {
305         struct sysfs_class_device *class_dev;
306         int loop;
307
308         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
309         while (--loop) {
310                 class_dev = sysfs_open_class_device_path(path);
311                 if (class_dev)
312                         break;
313
314                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
315         }
316
317         return (class_dev);
318 }
319
320 int wait_for_class_device(struct sysfs_class_device *class_dev,
321                           const char **error)
322 {
323         struct sysfs_class_device *class_dev_parent;
324         struct sysfs_device *devices_dev = NULL;
325         int loop;
326
327         if (wait_for_class_device_attributes(class_dev, error) != 0)
328                 return -ENOENT;
329
330         /* skip devices without devices-link */
331         if (class_device_expect_no_device_link(class_dev)) {
332                 dbg("no device symlink expected for '%s', ", class_dev->name);
333                 return -ENODEV;
334         }
335
336         /* the symlink may be on the parent device */
337         class_dev_parent = sysfs_get_classdev_parent(class_dev);
338         if (class_dev_parent)
339                 dbg("looking at parent device for device link '%s'", class_dev_parent->path);
340
341         /* wait for the symlink to the devices device */
342         dbg("waiting for symlink to devices device");
343         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
344         while (--loop) {
345                 if (class_dev_parent)
346                         devices_dev = sysfs_get_classdev_device(class_dev_parent);
347                 else
348                         devices_dev = sysfs_get_classdev_device(class_dev);
349
350                 if (devices_dev)
351                         break;
352
353                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
354         }
355         if (!devices_dev) {
356                 dbg(" error: no devices device symlink found");
357                 if (error)
358                         *error = "no device symlink";
359                 return -ENODEV;
360         }
361         dbg("device symlink found pointing to '%s'", devices_dev->path);
362
363         /* wait for the bus value */
364         if (class_device_expect_no_bus(class_dev)) {
365                 dbg("no bus device expected for '%s', ", class_dev->classname);
366                 return 0;
367         } else {
368                 return wait_for_bus_device(devices_dev, error);
369         }
370 }
371
372 struct sysfs_device *open_devices_device_wait(const char *path)
373 {
374         struct sysfs_device *devices_dev;
375         int loop;
376
377         loop = WAIT_MAX_SECONDS * WAIT_LOOP_PER_SECOND;
378         while (--loop) {
379                 devices_dev = sysfs_open_device_path(path);
380                 if (devices_dev)
381                         break;
382
383                 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND);
384         }
385
386         return(devices_dev);
387 }