]>
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.
31 #include "libsysfs/sysfs/libsysfs.h"
32 #include "udev_version.h"
33 #include "udev_sysfs.h"
37 /* list of subsystem specific files, NULL if there is no file to wait for */
38 static struct subsystem_file
{
41 } subsystem_files
[] = {
42 { .subsystem
= "net", .file
= "ifindex" },
43 { .subsystem
= "scsi_host", .file
= "unique_id" },
44 { .subsystem
= "scsi_device", .file
= NULL
},
45 { .subsystem
= "pcmcia_socket", .file
= "card_type" },
46 { .subsystem
= "usb_host", .file
= NULL
},
47 { .subsystem
= "bluetooth", .file
= "address" },
48 { .subsystem
= "firmware", .file
= "data" },
49 { .subsystem
= "i2c-adapter", .file
= NULL
},
50 { .subsystem
= "pci_bus", .file
= NULL
},
51 { .subsystem
= "ieee1394", .file
= NULL
},
52 { .subsystem
= "ieee1394_host", .file
= NULL
},
53 { .subsystem
= "ieee1394_node", .file
= NULL
},
57 int subsystem_expect_no_dev(const char *subsystem
)
59 struct subsystem_file
*file
;
61 for (file
= subsystem_files
; file
->subsystem
!= NULL
; file
++)
62 if (strcmp(subsystem
, file
->subsystem
) == 0)
68 /* get subsystem specific files, returns "dev" if no other found */
69 static char *get_subsystem_specific_file(const char *subsystem
)
71 struct subsystem_file
*file
;
73 /* look if we want to look for another file instead of "dev" */
74 for (file
= subsystem_files
; file
->subsystem
!= NULL
; file
++)
75 if (strcmp(subsystem
, file
->subsystem
) == 0)
81 /* wait for class pecific file to show up */
82 static int wait_for_class_device_attributes(struct sysfs_class_device
*class_dev
,
86 char filename
[SYSFS_PATH_MAX
];
89 file
= get_subsystem_specific_file(class_dev
->classname
);
91 dbg("class '%s' has no file to wait for", class_dev
->classname
);
95 snprintf(filename
, SYSFS_PATH_MAX
-1, "%s/%s", class_dev
->path
, file
);
96 dbg("looking at class '%s' for specific file '%s'", class_dev
->classname
, filename
);
98 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
102 if (stat(class_dev
->path
, &stats
) == -1) {
103 dbg("'%s' now disappeared (probably remove has beaten us)", class_dev
->path
);
107 if (stat(filename
, &stats
) == 0) {
108 dbg("class '%s' specific file '%s' found", class_dev
->classname
, file
);
112 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
115 dbg("error: getting class '%s' specific file '%s'", class_dev
->classname
, file
);
117 *error
= "class specific file unavailable";
121 /* check if we need to wait for a physical device */
122 static int class_device_expect_no_device_link(struct sysfs_class_device
*class_dev
)
124 /* list of devices without a "device" symlink to the physical device
125 * if device is set to NULL, no devices in that subsystem has a link */
126 static struct class_device
{
130 { .subsystem
= "block", .device
= "double" },
131 { .subsystem
= "block", .device
= "nb" },
132 { .subsystem
= "block", .device
= "ram" },
133 { .subsystem
= "block", .device
= "loop" },
134 { .subsystem
= "block", .device
= "fd" },
135 { .subsystem
= "block", .device
= "md" },
136 { .subsystem
= "block", .device
= "dos_cd" },
137 { .subsystem
= "block", .device
= "rflash" },
138 { .subsystem
= "block", .device
= "rom" },
139 { .subsystem
= "block", .device
= "rrom" },
140 { .subsystem
= "block", .device
= "flash" },
141 { .subsystem
= "block", .device
= "msd" },
142 { .subsystem
= "block", .device
= "sbpcd" },
143 { .subsystem
= "block", .device
= "pcd" },
144 { .subsystem
= "block", .device
= "pf" },
145 { .subsystem
= "block", .device
= "scd" },
146 { .subsystem
= "block", .device
= "ubd" },
147 { .subsystem
= "block", .device
= "dm-" },
148 { .subsystem
= "input", .device
= "event" },
149 { .subsystem
= "input", .device
= "mice" },
150 { .subsystem
= "input", .device
= "mouse" },
151 { .subsystem
= "input", .device
= "ts" },
152 { .subsystem
= "vc", .device
= NULL
},
153 { .subsystem
= "tty", .device
= NULL
},
154 { .subsystem
= "cpuid", .device
= "cpu" },
155 { .subsystem
= "graphics", .device
= "fb" },
156 { .subsystem
= "mem", .device
= NULL
},
157 { .subsystem
= "misc", .device
= NULL
},
158 { .subsystem
= "msr", .device
= NULL
},
159 { .subsystem
= "netlink", .device
= NULL
},
160 { .subsystem
= "net", .device
= "sit" },
161 { .subsystem
= "net", .device
= "lo" },
162 { .subsystem
= "net", .device
= "tap" },
163 { .subsystem
= "net", .device
= "ipsec" },
164 { .subsystem
= "net", .device
= "dummy" },
165 { .subsystem
= "net", .device
= "irda" },
166 { .subsystem
= "net", .device
= "ppp" },
167 { .subsystem
= "net", .device
= "tun" },
168 { .subsystem
= "net", .device
= "pan" },
169 { .subsystem
= "net", .device
= "bnep" },
170 { .subsystem
= "net", .device
= "vmnet" },
171 { .subsystem
= "ppp", .device
= NULL
},
172 { .subsystem
= "sound", .device
= NULL
},
173 { .subsystem
= "printer", .device
= "lp" },
174 { .subsystem
= "nvidia", .device
= NULL
},
175 { .subsystem
= "video4linux", .device
= "vbi" },
176 { .subsystem
= "dvb", .device
= NULL
},
177 { .subsystem
= "lirc", .device
= NULL
},
178 { .subsystem
= "firmware", .device
= NULL
},
179 { .subsystem
= "drm", .device
= NULL
},
180 { .subsystem
= "pci_bus", .device
= NULL
},
181 { .subsystem
= "ieee1394", .device
= NULL
},
182 { .subsystem
= "ieee1394_host", .device
= NULL
},
183 { .subsystem
= "ieee1394_node", .device
= NULL
},
184 { .subsystem
= "raw", .device
= NULL
},
185 { .subsystem
= "zaptel", .device
= NULL
},
186 { .subsystem
= "tiglusb", .device
= NULL
},
187 { .subsystem
= "ppdev", .device
= NULL
},
188 { .subsystem
= "ticables", .device
= NULL
},
189 { .subsystem
= "snsc", .device
= NULL
},
190 { .subsystem
= "staliomem", .device
= NULL
},
191 { .subsystem
= "tape", .device
= NULL
},
192 { .subsystem
= "ip2", .device
= NULL
},
193 { .subsystem
= "tpqic02", .device
= NULL
},
194 { .subsystem
= "dsp56k", .device
= NULL
},
195 { .subsystem
= "zft", .device
= NULL
},
196 { .subsystem
= "adb", .device
= NULL
},
197 { .subsystem
= "cosa", .device
= NULL
},
198 { .subsystem
= "pg", .device
= NULL
},
199 { .subsystem
= "pt", .device
= NULL
},
200 { .subsystem
= "capi", .device
= NULL
},
203 struct class_device
*classdevice
;
206 /* the kernel may tell us what to wait for */
207 if (kernel_release_satisfactory(2,6,10) > 0)
208 if (getenv("PHYSDEVPATH") == NULL
) {
209 dbg("the kernel says, that there is no physical device for '%s'", class_dev
->path
);
213 for (classdevice
= class_device
; classdevice
->subsystem
!= NULL
; classdevice
++) {
214 if (strcmp(class_dev
->classname
, classdevice
->subsystem
) == 0) {
215 /* see if no device in this class is expected to have a device-link */
216 if (classdevice
->device
== NULL
)
219 len
= strlen(classdevice
->device
);
221 /* see if device name matches */
222 if (strncmp(class_dev
->name
, classdevice
->device
, len
) != 0)
225 /* exact name match */
226 if (strlen(class_dev
->name
) == len
)
229 /* name match with instance number */
230 if (isdigit(class_dev
->name
[len
]))
238 /* skip waiting for the bus of the devices device */
239 static int class_device_expect_no_bus(struct sysfs_class_device
*class_dev
)
241 static char *devices_without_bus
[] = {
249 for (device
= devices_without_bus
; *device
!= NULL
; device
++) {
250 int len
= strlen(*device
);
252 if (strncmp(class_dev
->classname
, *device
, len
) == 0)
259 /* wait for a devices device specific file to show up */
260 int wait_for_devices_device(struct sysfs_device
*devices_dev
,
263 static struct device_file
{
267 { .bus
= "scsi", .file
= "vendor" },
268 { .bus
= "usb", .file
= "idVendor" },
269 { .bus
= "usb", .file
= "iInterface" },
270 { .bus
= "usb", .file
= "bNumEndpoints" },
271 { .bus
= "usb-serial", .file
= "detach_state" },
272 { .bus
= "ide", .file
= "detach_state" },
273 { .bus
= "pci", .file
= "vendor" },
274 { .bus
= "platform", .file
= "detach_state" },
275 { .bus
= "i2c", .file
= "detach_state" },
276 { .bus
= "ieee1394", .file
= "node_count" },
277 { .bus
= "ieee1394", .file
= "nodeid" },
278 { .bus
= "ieee1394", .file
= "address" },
281 struct device_file
*devicefile
;
284 /* the kernel may tell us what to wait for */
285 if (kernel_release_satisfactory(2,6,10) > 0)
286 if (getenv("PHYSDEVBUS") == NULL
) {
287 dbg("the kernel says, that there is no bus for '%s'", devices_dev
->path
);
291 /* wait for the bus device link to the devices device */
292 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
294 if (sysfs_get_device_bus(devices_dev
) == 0)
297 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
300 dbg("error: getting bus device link");
302 *error
= "no bus device link";
305 dbg("bus device link found for bus '%s'", devices_dev
->bus
);
307 /* wait for a bus device specific file to show up */
308 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
310 int found_bus_type
= 0;
312 for (devicefile
= device_files
; devicefile
->bus
!= NULL
; devicefile
++) {
313 if (strcmp(devices_dev
->bus
, devicefile
->bus
) == 0) {
314 char filename
[SYSFS_PATH_MAX
];
318 snprintf(filename
, SYSFS_PATH_MAX
-1, "%s/%s", devices_dev
->path
, devicefile
->file
);
319 dbg("looking at bus '%s' device for specific file '%s'", devices_dev
->bus
, filename
);
321 if (stat(filename
, &stats
) == 0) {
322 dbg("bus '%s' device specific file '%s' found", devices_dev
->bus
, devicefile
->file
);
327 if (found_bus_type
== 0) {
329 *error
= "unknown bus";
330 info("error: unknown bus, please report to "
331 "<linux-hotplug-devel@lists.sourceforge.net> '%s'", devices_dev
->bus
);
334 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
337 dbg("error: getting '%s' device specific file '%s'", devices_dev
->bus
, devicefile
->file
);
339 *error
= "bus device specific file unavailable";
344 struct sysfs_class_device
*wait_class_device_open(const char *path
)
346 struct sysfs_class_device
*class_dev
;
349 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
351 class_dev
= sysfs_open_class_device_path(path
);
355 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
361 int wait_for_class_device(struct sysfs_class_device
*class_dev
,
364 struct sysfs_class_device
*class_dev_parent
;
365 struct sysfs_device
*devices_dev
= NULL
;
368 if (wait_for_class_device_attributes(class_dev
, error
) != 0)
371 /* skip devices without devices-link */
372 if (class_device_expect_no_device_link(class_dev
)) {
373 dbg("no device symlink expected for '%s', ", class_dev
->name
);
377 /* the symlink may be on the parent device */
378 class_dev_parent
= sysfs_get_classdev_parent(class_dev
);
379 if (class_dev_parent
)
380 dbg("looking at parent device for device link '%s'", class_dev_parent
->path
);
382 /* wait for the symlink to the devices device */
383 dbg("waiting for symlink to devices device");
384 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
386 if (class_dev_parent
)
387 devices_dev
= sysfs_get_classdev_device(class_dev_parent
);
389 devices_dev
= sysfs_get_classdev_device(class_dev
);
394 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);
397 dbg(" error: no devices device symlink found");
399 *error
= "no device symlink";
402 dbg("device symlink found pointing to '%s'", devices_dev
->path
);
404 /* wait for the devices device */
405 if (class_device_expect_no_bus(class_dev
)) {
406 dbg("no bus device expected for '%s', ", class_dev
->classname
);
410 return wait_for_devices_device(devices_dev
, error
);
413 struct sysfs_device
*wait_devices_device_open(const char *path
)
415 struct sysfs_device
*devices_dev
;
418 loop
= WAIT_MAX_SECONDS
* WAIT_LOOP_PER_SECOND
;
420 devices_dev
= sysfs_open_device_path(path
);
424 usleep(1000 * 1000 / WAIT_LOOP_PER_SECOND
);