Skip to content

idunham/libsysdev

Repository files navigation

libsysdev introduction

The general purpose is to provide a sysfs-based library that can replace
libudev. This does not mean API/ABI-compatible; rather, it should provide
similar functionality without a daemon, and a program should be able to
link with both libsysdev and libudev at the same time (so libsysdev can
be used as a fallback when udevd is not running).
The --enable-sysfs code in mesa provided inspiration.
If someone wants to write a partial udev-compat shim, I'm open to including it.

The API is based on a quick glance through xf86-input-evdev
and mesa to see where they need udev/what they do instead.
By my reading, sysdev_getsyspath() or sysdev_devfd_to_syspath() should be
enough for evdev, while mesa would need sysdev_getproductids() as well.

Thanks to Karl Hammar for inspiration to do this.

Copyright/license:
See LICENSE and the individual files for license information.
The bulk of the library is copyright
 Copyright Isaac Dunham, in the year of our Lord 2015.
 No rights reserved, see LICENSE for details.

TODO:
* Handle multiarch and multilib configurations better than
  expecting the builder to set LIBDIR.
* (WIP) Get evdev to use this.
  A first try, which has *not* been tested beyond compile, is available at
  https://github.com/idunham/xf86-input-evdev
  (in branch sysdev).

API:
libsysdev uses the sysdev_ namespace for public APIs.
Internal functions are prefixed with sdp_ instead.

See util/devinfo.c for an example of how you could use sysdev_getsyspath()
and sysdev_getproductids().

If you have a device fd (devfd) and want to get the PCI IDs, just use this:
  int vendor, device;
  ...
  if (sysdev_devfd_to_pciid(&vendor, &device, devfd)) {
    // handle error condition (PCI ids not read)
  }

Here are the currently available functions:
/* given fd of syspath, set vendor_id and device_id
 * returns 0 for success, nonzero for failure
 */
int sysdev_getproductids(int *vendor_id, int *device_id, int sysfd);

/* given major/minor/type, return the directory in /sys
 * returns a calloc'd string (or NULL on failure)
 */
char * sysdev_getsyspath(unsigned int major, unsigned int minor, int ischar);

/* given fd of device, return the directory in /sys
 * returns a calloc'd string (or NULL on failure)
 * sets errno = EINVAL if devfd is not a device
 */
char *sysdev_devfd_to_syspath(int devfd);

/* given fd of device, return fd of corresponding syspath
 * returns -1 on failure, sets errno = EINVAL if devfd is not a device
 */
int sysdev_devfd_to_sysfd(int devfd);

/* given fd of device, read vendor_id and device_id
 * returns 0 for success, nonzero for failure
 */
int sysdev_devfd_to_pciid(int *vendor_id, int *device_id, int devfd);

About

library to get information about devices from sysfs

Resources

License

Stars

Watchers

Forks

Packages

No packages published