]>
git.ipfire.org Git - thirdparty/systemd.git/blob - udev_sysfs.c
2 * udev_sysfs.c - sysfs linux kernel specific knowledge
4 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
5 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
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.
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.
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.
32 #include "udev_version.h"
33 #include "udev_sysfs.h"
34 #include "libsysfs/sysfs/libsysfs.h"
36 /* list of subsystem specific files
37 * NULL if there is no file to wait for
39 static struct subsystem_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
},
58 int subsystem_expect_no_dev(const char *subsystem
)
60 struct subsystem_file
*file
;
62 for (file
= subsystem_files
; file
->subsystem
!= NULL
; file
++)
63 if (strcmp(subsystem
, file
->subsystem
) == 0)
69 /* get subsystem specific files, returns "dev" if no other found */
70 static char *get_subsystem_specific_file(const char *subsystem
)
72 struct subsystem_file
*file
;
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)
82 /* wait for class pecific file to show up */
83 static int wait_for_class_device_attributes(struct sysfs_class_device
*class_dev
,
87 char filename
[SYSFS_PATH_MAX
];
90 file
= get_subsystem_specific_file(class_dev
->classname
);
92 dbg("class '%s' has no file to wait for", class_dev
->classname
);
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
);
99 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
103 if (stat(class_dev
->path
, &stats
) == -1) {
104 dbg("'%s' now disappeared (probably remove has beaten us)", class_dev
->path
);
108 if (stat(filename
, &stats
) == 0) {
109 dbg("class '%s' specific file '%s' found", class_dev
->classname
, file
);
113 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
116 dbg("error: getting class '%s' specific file '%s'", class_dev
->classname
, file
);
118 *error
= "class specific file unavailable";
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
)
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
{
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
= "net", .device
= "pan" },
170 { .subsystem
= "net", .device
= "bnep" },
171 { .subsystem
= "net", .device
= "vmnet" },
172 { .subsystem
= "ppp", .device
= NULL
},
173 { .subsystem
= "sound", .device
= NULL
},
174 { .subsystem
= "printer", .device
= "lp" },
175 { .subsystem
= "nvidia", .device
= NULL
},
176 { .subsystem
= "video4linux", .device
= "vbi" },
177 { .subsystem
= "dvb", .device
= NULL
},
178 { .subsystem
= "lirc", .device
= NULL
},
179 { .subsystem
= "firmware", .device
= NULL
},
180 { .subsystem
= "drm", .device
= NULL
},
181 { .subsystem
= "pci_bus", .device
= NULL
},
182 { .subsystem
= "ieee1394", .device
= NULL
},
183 { .subsystem
= "ieee1394_host", .device
= NULL
},
184 { .subsystem
= "ieee1394_node", .device
= NULL
},
185 { .subsystem
= "raw", .device
= NULL
},
186 { .subsystem
= "zaptel", .device
= NULL
},
187 { .subsystem
= "tiglusb", .device
= NULL
},
188 { .subsystem
= "ppdev", .device
= NULL
},
189 { .subsystem
= "ticables", .device
= NULL
},
190 { .subsystem
= "snsc", .device
= NULL
},
191 { .subsystem
= "staliomem", .device
= NULL
},
192 { .subsystem
= "tape", .device
= NULL
},
193 { .subsystem
= "ip2", .device
= NULL
},
194 { .subsystem
= "tpqic02", .device
= NULL
},
195 { .subsystem
= "dsp56k", .device
= NULL
},
196 { .subsystem
= "zft", .device
= NULL
},
197 { .subsystem
= "adb", .device
= NULL
},
198 { .subsystem
= "cosa", .device
= NULL
},
199 { .subsystem
= "pg", .device
= NULL
},
200 { .subsystem
= "pt", .device
= NULL
},
201 { .subsystem
= "capi", .device
= NULL
},
204 struct class_device
*classdevice
;
207 for (classdevice
= class_device
; classdevice
->subsystem
!= NULL
; classdevice
++) {
208 if (strcmp(class_dev
->classname
, classdevice
->subsystem
) == 0) {
209 /* see if no device in this class is expected to have a device-link */
210 if (classdevice
->device
== NULL
)
213 len
= strlen(classdevice
->device
);
215 /* see if device name matches */
216 if (strncmp(class_dev
->name
, classdevice
->device
, len
) != 0)
219 /* exact name match */
220 if (strlen(class_dev
->name
) == len
)
223 /* name match with instance number */
224 if (isdigit(class_dev
->name
[len
]))
232 /* skip waiting for the bus */
233 static int class_device_expect_no_bus(struct sysfs_class_device
*class_dev
)
235 static char *devices_without_bus
[] = {
242 for (device
= devices_without_bus
; *device
!= NULL
; device
++) {
243 int len
= strlen(*device
);
245 if (strncmp(class_dev
->classname
, *device
, len
) == 0)
252 /* wait for the bus and for a bus specific file to show up */
253 int wait_for_bus_device(struct sysfs_device
*devices_dev
,
256 static struct bus_file
{
260 { .bus
= "scsi", .file
= "vendor" },
261 { .bus
= "usb", .file
= "idVendor" },
262 { .bus
= "usb", .file
= "iInterface" },
263 { .bus
= "usb", .file
= "bNumEndpoints" },
264 { .bus
= "usb-serial", .file
= "detach_state" },
265 { .bus
= "ide", .file
= "detach_state" },
266 { .bus
= "pci", .file
= "vendor" },
267 { .bus
= "platform", .file
= "detach_state" },
268 { .bus
= "i2c", .file
= "detach_state" },
269 { .bus
= "ieee1394", .file
= "node_count" },
270 { .bus
= "ieee1394", .file
= "nodeid" },
271 { .bus
= "ieee1394", .file
= "address" },
274 struct bus_file
*busfile
;
277 /* wait for the bus device link to the devices device */
278 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
280 if (sysfs_get_device_bus(devices_dev
) == 0)
283 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
286 dbg("error: getting bus device link");
288 *error
= "no bus device link";
291 dbg("bus device link found for bus '%s'", devices_dev
->bus
);
293 /* wait for a bus specific file to show up */
294 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
296 int found_bus_type
= 0;
298 for (busfile
= bus_files
; busfile
->bus
!= NULL
; busfile
++) {
299 if (strcmp(devices_dev
->bus
, busfile
->bus
) == 0) {
300 char filename
[SYSFS_PATH_MAX
];
304 snprintf(filename
, SYSFS_PATH_MAX
-1, "%s/%s", devices_dev
->path
, busfile
->file
);
305 dbg("looking at bus '%s' for specific file '%s'", devices_dev
->bus
, filename
);
307 if (stat(filename
, &stats
) == 0) {
308 dbg("bus '%s' specific file '%s' found", devices_dev
->bus
, busfile
->file
);
313 if (found_bus_type
== 0) {
315 *error
= "unknown bus";
316 info("error: unknown bus, please report to "
317 "<linux-hotplug-devel@lists.sourceforge.net> '%s'", devices_dev
->bus
);
320 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
323 dbg("error: getting bus '%s' specific file '%s'", devices_dev
->bus
, busfile
->file
);
325 *error
= "bus specific file unavailable";
330 struct sysfs_class_device
*open_class_device_wait(const char *path
)
332 struct sysfs_class_device
*class_dev
;
335 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
337 class_dev
= sysfs_open_class_device_path(path
);
341 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
347 int wait_for_class_device(struct sysfs_class_device
*class_dev
,
350 struct sysfs_class_device
*class_dev_parent
;
351 struct sysfs_device
*devices_dev
= NULL
;
354 if (wait_for_class_device_attributes(class_dev
, error
) != 0)
357 /* skip devices without devices-link */
358 if (class_device_expect_no_device_link(class_dev
)) {
359 dbg("no device symlink expected for '%s', ", class_dev
->name
);
363 /* the symlink may be on the parent device */
364 class_dev_parent
= sysfs_get_classdev_parent(class_dev
);
365 if (class_dev_parent
)
366 dbg("looking at parent device for device link '%s'", class_dev_parent
->path
);
368 /* wait for the symlink to the devices device */
369 dbg("waiting for symlink to devices device");
370 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
372 if (class_dev_parent
)
373 devices_dev
= sysfs_get_classdev_device(class_dev_parent
);
375 devices_dev
= sysfs_get_classdev_device(class_dev
);
380 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
383 dbg(" error: no devices device symlink found");
385 *error
= "no device symlink";
388 dbg("device symlink found pointing to '%s'", devices_dev
->path
);
390 /* wait for the bus value */
391 if (class_device_expect_no_bus(class_dev
)) {
392 dbg("no bus device expected for '%s', ", class_dev
->classname
);
395 return wait_for_bus_device(devices_dev
, error
);
399 struct sysfs_device
*open_devices_device_wait(const char *path
)
401 struct sysfs_device
*devices_dev
;
404 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
406 devices_dev
= sysfs_open_device_path(path
);
410 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);