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