2 * Copyright (C) 2005-2006 Kay Sievers <kay.sievers@vrfy.org>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 char sysfs_path
[PATH_SIZE
];
34 static LIST_HEAD(dev_list
);
36 /* attribute value cache */
37 static LIST_HEAD(attr_list
);
39 struct list_head node
;
41 char *value
; /* points to value_local if value is cached */
42 char value_local
[NAME_SIZE
];
49 env
= getenv("SYSFS_PATH");
51 strlcpy(sysfs_path
, env
, sizeof(sysfs_path
));
52 remove_trailing_chars(sysfs_path
, '/');
54 strlcpy(sysfs_path
, "/sys", sizeof(sysfs_path
));
55 dbg("sysfs_path='%s'", sysfs_path
);
57 INIT_LIST_HEAD(&dev_list
);
58 INIT_LIST_HEAD(&attr_list
);
62 void sysfs_cleanup(void)
64 struct sysfs_attr
*attr_loop
;
65 struct sysfs_attr
*attr_temp
;
66 struct sysfs_device
*dev_loop
;
67 struct sysfs_device
*dev_temp
;
69 list_for_each_entry_safe(attr_loop
, attr_temp
, &attr_list
, node
) {
70 list_del(&attr_loop
->node
);
74 list_for_each_entry_safe(dev_loop
, dev_temp
, &dev_list
, node
) {
75 list_del(&dev_loop
->node
);
80 void sysfs_device_set_values(struct sysfs_device
*dev
, const char *devpath
,
81 const char *subsystem
, const char *driver
)
85 strlcpy(dev
->devpath
, devpath
, sizeof(dev
->devpath
));
86 if (subsystem
!= NULL
)
87 strlcpy(dev
->subsystem
, subsystem
, sizeof(dev
->subsystem
));
89 strlcpy(dev
->driver
, driver
, sizeof(dev
->driver
));
92 pos
= strrchr(dev
->devpath
, '/');
95 strlcpy(dev
->kernel
, &pos
[1], sizeof(dev
->kernel
));
96 dbg("kernel='%s'", dev
->kernel
);
98 /* some devices have '!' in their name, change that to '/' */
100 while (pos
[0] != '\0') {
106 /* get kernel number */
107 pos
= &dev
->kernel
[strlen(dev
->kernel
)];
108 while (isdigit(pos
[-1]))
110 strlcpy(dev
->kernel_number
, pos
, sizeof(dev
->kernel_number
));
111 dbg("kernel_number='%s'", dev
->kernel_number
);
114 int sysfs_resolve_link(char *devpath
, size_t size
)
116 char link_path
[PATH_SIZE
];
117 char link_target
[PATH_SIZE
];
122 strlcpy(link_path
, sysfs_path
, sizeof(link_path
));
123 strlcat(link_path
, devpath
, sizeof(link_path
));
124 len
= readlink(link_path
, link_target
, sizeof(link_target
));
127 link_target
[len
] = '\0';
128 dbg("path link '%s' points to '%s'", devpath
, link_target
);
130 for (back
= 0; strncmp(&link_target
[back
* 3], "../", 3) == 0; back
++)
132 dbg("base '%s', tail '%s', back %i", devpath
, &link_target
[back
* 3], back
);
133 for (i
= 0; i
<= back
; i
++) {
134 char *pos
= strrchr(devpath
, '/');
140 dbg("after moving back '%s'", devpath
);
141 strlcat(devpath
, "/", size
);
142 strlcat(devpath
, &link_target
[back
* 3], size
);
146 struct sysfs_device
*sysfs_device_get(const char *devpath
)
148 char path
[PATH_SIZE
];
149 char devpath_real
[PATH_SIZE
];
150 struct sysfs_device
*dev
;
151 struct sysfs_device
*dev_loop
;
153 char link_path
[PATH_SIZE
];
154 char link_target
[PATH_SIZE
];
158 /* we handle only these devpathes */
159 if (devpath
!= NULL
&&
160 strncmp(devpath
, "/devices/", 9) != 0 &&
161 strncmp(devpath
, "/subsystem/", 11) != 0 &&
162 strncmp(devpath
, "/module/", 8) != 0 &&
163 strncmp(devpath
, "/bus/", 5) != 0 &&
164 strncmp(devpath
, "/class/", 7) != 0 &&
165 strncmp(devpath
, "/block/", 7) != 0)
168 dbg("open '%s'", devpath
);
169 strlcpy(devpath_real
, devpath
, sizeof(devpath_real
));
170 remove_trailing_chars(devpath_real
, '/');
171 if (devpath
[0] == '\0' )
174 /* look for device already in cache (we never put an untranslated path in the cache) */
175 list_for_each_entry(dev_loop
, &dev_list
, node
) {
176 if (strcmp(dev_loop
->devpath
, devpath_real
) == 0) {
177 dbg("found in cache '%s'", dev_loop
->devpath
);
182 /* if we got a link, resolve it to the real device */
183 strlcpy(path
, sysfs_path
, sizeof(path
));
184 strlcat(path
, devpath_real
, sizeof(path
));
185 if (lstat(path
, &statbuf
) != 0) {
186 dbg("stat '%s' failed: %s", path
, strerror(errno
));
189 if (S_ISLNK(statbuf
.st_mode
)) {
190 if (sysfs_resolve_link(devpath_real
, sizeof(devpath_real
)) != 0)
193 /* now look for device in cache after path translation */
194 list_for_each_entry(dev_loop
, &dev_list
, node
) {
195 if (strcmp(dev_loop
->devpath
, devpath_real
) == 0) {
196 dbg("found in cache '%s'", dev_loop
->devpath
);
202 /* it is a new device */
203 dbg("new uncached device '%s'", devpath_real
);
204 dev
= malloc(sizeof(struct sysfs_device
));
207 memset(dev
, 0x00, sizeof(struct sysfs_device
));
209 sysfs_device_set_values(dev
, devpath_real
, NULL
, NULL
);
211 /* get subsystem name */
212 strlcpy(link_path
, sysfs_path
, sizeof(link_path
));
213 strlcat(link_path
, dev
->devpath
, sizeof(link_path
));
214 strlcat(link_path
, "/subsystem", sizeof(link_path
));
215 len
= readlink(link_path
, link_target
, sizeof(link_target
));
217 /* get subsystem from "subsystem" link */
218 link_target
[len
] = '\0';
219 dbg("subsystem link '%s' points to '%s'", link_path
, link_target
);
220 pos
= strrchr(link_target
, '/');
222 strlcpy(dev
->subsystem
, &pos
[1], sizeof(dev
->subsystem
));
223 } else if (strncmp(dev
->devpath
, "/class/", 7) == 0) {
224 /* get subsystem from class dir */
225 strlcpy(dev
->subsystem
, &dev
->devpath
[7], sizeof(dev
->subsystem
));
226 pos
= strchr(dev
->subsystem
, '/');
230 strlcpy(dev
->subsystem
, "subsystem", sizeof(dev
->subsystem
));
231 } else if (strncmp(dev
->devpath
, "/block/", 7) == 0) {
232 strlcpy(dev
->subsystem
, "block", sizeof(dev
->subsystem
));
233 } else if (strncmp(dev
->devpath
, "/devices/", 9) == 0) {
234 /* get subsystem from "bus" link */
235 strlcpy(link_path
, sysfs_path
, sizeof(link_path
));
236 strlcat(link_path
, dev
->devpath
, sizeof(link_path
));
237 strlcat(link_path
, "/bus", sizeof(link_path
));
238 len
= readlink(link_path
, link_target
, sizeof(link_target
));
240 link_target
[len
] = '\0';
241 dbg("bus link '%s' points to '%s'", link_path
, link_target
);
242 pos
= strrchr(link_target
, '/');
244 strlcpy(dev
->subsystem
, &pos
[1], sizeof(dev
->subsystem
));
246 } else if (strstr(dev
->devpath
, "/drivers/") != NULL
) {
247 strlcpy(dev
->subsystem
, "drivers", sizeof(dev
->subsystem
));
248 } else if (strncmp(dev
->devpath
, "/module/", 8) == 0) {
249 strlcpy(dev
->subsystem
, "module", sizeof(dev
->subsystem
));
250 } else if (strncmp(dev
->devpath
, "/subsystem/", 11) == 0) {
251 pos
= strrchr(dev
->devpath
, '/');
252 if (pos
== &dev
->devpath
[10])
253 strlcpy(dev
->subsystem
, "subsystem", sizeof(dev
->subsystem
));
254 } else if (strncmp(dev
->devpath
, "/bus/", 5) == 0) {
255 pos
= strrchr(dev
->devpath
, '/');
256 if (pos
== &dev
->devpath
[4])
257 strlcpy(dev
->subsystem
, "subsystem", sizeof(dev
->subsystem
));
260 /* get driver name */
261 strlcpy(link_path
, sysfs_path
, sizeof(link_path
));
262 strlcat(link_path
, dev
->devpath
, sizeof(link_path
));
263 strlcat(link_path
, "/driver", sizeof(link_path
));
264 len
= readlink(link_path
, link_target
, sizeof(link_target
));
266 link_target
[len
] = '\0';
267 dbg("driver link '%s' points to '%s'", link_path
, link_target
);
268 pos
= strrchr(link_target
, '/');
270 strlcpy(dev
->driver
, &pos
[1], sizeof(dev
->driver
));
273 dbg("add to cache 'devpath=%s', subsystem='%s', driver='%s'", dev
->devpath
, dev
->subsystem
, dev
->driver
);
274 list_add(&dev
->node
, &dev_list
);
279 struct sysfs_device
*sysfs_device_get_parent(struct sysfs_device
*dev
)
281 char parent_devpath
[PATH_SIZE
];
284 dbg("open '%s'", dev
->devpath
);
286 /* look if we already know the parent */
287 if (dev
->parent
!= NULL
)
290 strlcpy(parent_devpath
, dev
->devpath
, sizeof(parent_devpath
));
291 dbg("'%s'", parent_devpath
);
293 /* strip last element */
294 pos
= strrchr(parent_devpath
, '/');
295 if (pos
== NULL
|| pos
== parent_devpath
)
299 if (strncmp(parent_devpath
, "/class", 6) == 0) {
300 pos
= strrchr(parent_devpath
, '/');
301 if (pos
== &parent_devpath
[6] || pos
== parent_devpath
) {
302 dbg("/class top level, look for device link");
306 if (strcmp(parent_devpath
, "/block") == 0) {
307 dbg("/block top level, look for device link");
311 /* are we at the top level? */
312 pos
= strrchr(parent_devpath
, '/');
313 if (pos
== NULL
|| pos
== parent_devpath
)
316 /* get parent and remember it */
317 dev
->parent
= sysfs_device_get(parent_devpath
);
321 strlcpy(parent_devpath
, dev
->devpath
, sizeof(parent_devpath
));
322 strlcat(parent_devpath
, "/device", sizeof(parent_devpath
));
323 if (sysfs_resolve_link(parent_devpath
, sizeof(parent_devpath
)) != 0)
326 /* get parent and remember it */
327 dev
->parent
= sysfs_device_get(parent_devpath
);
331 struct sysfs_device
*sysfs_device_get_parent_with_subsystem(struct sysfs_device
*dev
, const char *subsystem
)
333 struct sysfs_device
*dev_parent
;
335 dev_parent
= sysfs_device_get_parent(dev
);
336 while (dev_parent
!= NULL
) {
337 if (strcmp(dev_parent
->subsystem
, subsystem
) == 0)
339 dev_parent
= sysfs_device_get_parent(dev_parent
);
344 char *sysfs_attr_get_value(const char *devpath
, const char *attr_name
)
346 char path_full
[PATH_SIZE
];
348 char value
[NAME_SIZE
];
349 struct sysfs_attr
*attr_loop
;
350 struct sysfs_attr
*attr
;
356 dbg("open '%s'/'%s'", devpath
, attr_name
);
357 sysfs_len
= strlcpy(path_full
, sysfs_path
, sizeof(path_full
));
358 path
= &path_full
[sysfs_len
];
359 strlcat(path_full
, devpath
, sizeof(path_full
));
360 strlcat(path_full
, "/", sizeof(path_full
));
361 strlcat(path_full
, attr_name
, sizeof(path_full
));
363 /* look for attribute in cache */
364 list_for_each_entry(attr_loop
, &attr_list
, node
) {
365 if (strcmp(attr_loop
->path
, path
) == 0) {
366 dbg("found in cache '%s'", attr_loop
->path
);
367 return attr_loop
->value
;
371 /* store attribute in cache (also negatives are kept in cache) */
372 dbg("new uncached attribute '%s'", path_full
);
373 attr
= malloc(sizeof(struct sysfs_attr
));
376 memset(attr
, 0x00, sizeof(struct sysfs_attr
));
377 strlcpy(attr
->path
, path
, sizeof(attr
->path
));
378 dbg("add to cache '%s'", path_full
);
379 list_add(&attr
->node
, &attr_list
);
381 if (lstat(path_full
, &statbuf
) != 0) {
382 dbg("stat '%s' failed: %s", path_full
, strerror(errno
));
386 if (S_ISLNK(statbuf
.st_mode
)) {
387 /* links return the last element of the target path */
388 char link_target
[PATH_SIZE
];
392 len
= readlink(path_full
, link_target
, sizeof(link_target
));
394 link_target
[len
] = '\0';
395 pos
= strrchr(link_target
, '/');
397 dbg("cache '%s' with link value '%s'", path_full
, value
);
398 strlcpy(attr
->value_local
, &pos
[1], sizeof(attr
->value_local
));
399 attr
->value
= attr
->value_local
;
405 /* skip directories */
406 if (S_ISDIR(statbuf
.st_mode
))
409 /* skip non-readable files */
410 if ((statbuf
.st_mode
& S_IRUSR
) == 0)
413 /* read attribute value */
414 fd
= open(path_full
, O_RDONLY
);
416 dbg("attribute '%s' does not exist", path_full
);
419 size
= read(fd
, value
, sizeof(value
));
423 if (size
== sizeof(value
))
426 /* got a valid value, store and return it */
428 remove_trailing_chars(value
, '\n');
429 dbg("cache '%s' with attribute value '%s'", path_full
, value
);
430 strlcpy(attr
->value_local
, value
, sizeof(attr
->value_local
));
431 attr
->value
= attr
->value_local
;
437 int sysfs_lookup_devpath_by_subsys_id(char *devpath_full
, size_t len
, const char *subsystem
, const char *id
)
440 char path_full
[PATH_SIZE
];
444 sysfs_len
= strlcpy(path_full
, sysfs_path
, sizeof(path_full
));
445 path
= &path_full
[sysfs_len
];
447 if (strcmp(subsystem
, "subsystem") == 0) {
448 strlcpy(path
, "/subsystem/", sizeof(path_full
) - sysfs_len
);
449 strlcat(path
, id
, sizeof(path_full
) - sysfs_len
);
450 if (stat(path_full
, &statbuf
) == 0)
453 strlcpy(path
, "/bus/", sizeof(path_full
) - sysfs_len
);
454 strlcat(path
, id
, sizeof(path_full
) - sysfs_len
);
455 if (stat(path_full
, &statbuf
) == 0)
459 strlcpy(path
, "/class/", sizeof(path_full
) - sysfs_len
);
460 strlcat(path
, id
, sizeof(path_full
) - sysfs_len
);
461 if (stat(path_full
, &statbuf
) == 0)
465 if (strcmp(subsystem
, "module") == 0) {
466 strlcpy(path
, "/module/", sizeof(path_full
) - sysfs_len
);
467 strlcat(path
, id
, sizeof(path_full
) - sysfs_len
);
468 if (stat(path_full
, &statbuf
) == 0)
473 if (strcmp(subsystem
, "drivers") == 0) {
474 char subsys
[NAME_SIZE
];
477 strlcpy(subsys
, id
, sizeof(subsys
));
478 driver
= strchr(subsys
, ':');
479 if (driver
!= NULL
) {
482 strlcpy(path
, "/subsystem/", sizeof(path_full
) - sysfs_len
);
483 strlcat(path
, subsys
, sizeof(path_full
) - sysfs_len
);
484 strlcat(path
, "/drivers/", sizeof(path_full
) - sysfs_len
);
485 strlcat(path
, driver
, sizeof(path_full
) - sysfs_len
);
486 if (stat(path_full
, &statbuf
) == 0)
489 strlcpy(path
, "/bus/", sizeof(path_full
) - sysfs_len
);
490 strlcat(path
, subsys
, sizeof(path_full
) - sysfs_len
);
491 strlcat(path
, "/drivers/", sizeof(path_full
) - sysfs_len
);
492 strlcat(path
, driver
, sizeof(path_full
) - sysfs_len
);
493 if (stat(path_full
, &statbuf
) == 0)
499 strlcpy(path
, "/subsystem/", sizeof(path_full
) - sysfs_len
);
500 strlcat(path
, subsystem
, sizeof(path_full
) - sysfs_len
);
501 strlcat(path
, "/devices/", sizeof(path_full
) - sysfs_len
);
502 strlcat(path
, id
, sizeof(path_full
) - sysfs_len
);
503 if (stat(path_full
, &statbuf
) == 0)
506 strlcpy(path
, "/bus/", sizeof(path_full
) - sysfs_len
);
507 strlcat(path
, subsystem
, sizeof(path_full
) - sysfs_len
);
508 strlcat(path
, "/devices/", sizeof(path_full
) - sysfs_len
);
509 strlcat(path
, id
, sizeof(path_full
) - sysfs_len
);
510 if (stat(path_full
, &statbuf
) == 0)
513 strlcpy(path
, "/class/", sizeof(path_full
) - sysfs_len
);
514 strlcat(path
, subsystem
, sizeof(path_full
) - sysfs_len
);
515 strlcat(path
, "/", sizeof(path_full
) - sysfs_len
);
516 strlcat(path
, id
, sizeof(path_full
) - sysfs_len
);
517 if (stat(path_full
, &statbuf
) == 0)
522 if (S_ISLNK(statbuf
.st_mode
))
523 sysfs_resolve_link(path
, sizeof(path_full
) - sysfs_len
);
524 strlcpy(devpath_full
, path
, len
);