summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormananth <mananth>2003-10-15 12:14:21 +0000
committermananth <mananth>2003-10-15 12:14:21 +0000
commit1d4e840622d426b81616704c7ec2a636d218ca0d (patch)
tree2ca68c9375254d4e5721322b47418b0d978a65eb
parent079fd53e745babf94eda3dd3aa6b79f8a7872aa4 (diff)
downloadsysfsutils-1d4e840622d426b81616704c7ec2a636d218ca0d.tar.gz
Added support to open a device by name
-rw-r--r--ChangeLog6
-rw-r--r--include/libsysfs.h2
-rw-r--r--lib/sysfs_device.c70
3 files changed, 68 insertions, 10 deletions
diff --git a/ChangeLog b/ChangeLog
index 6779aac..146ff7f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,10 @@
+09/15/2003 - Ananth Mavinakayanahalli <ananth@in.ibm.com>
+ * Added sysfs_open_device_by_name() to open a device
+ given the device name
+ * Modified "name" field of sysfs_device to contain same
+ data as the "bus_id" field
+
09/11/2003 - Ananth Mavinakayanahalli <ananth@in.ibm.com>
* Added sysfs_open_driver_by_name() to open a driver and
its device, given the driver name
diff --git a/include/libsysfs.h b/include/libsysfs.h
index 07a8603..827e305 100644
--- a/include/libsysfs.h
+++ b/include/libsysfs.h
@@ -188,6 +188,8 @@ extern struct sysfs_device *sysfs_open_device_tree(const unsigned char *path);
extern struct sysfs_attribute *sysfs_get_device_attr
(struct sysfs_device *dev, const unsigned char *name);
extern struct dlist *sysfs_get_device_attributes(struct sysfs_device *device);
+extern struct sysfs_driver *sysfs_open_driver_by_name
+ (unsigned char *dev_name, unsigned char *bus, size_t bsize);
/* generic sysfs bus access */
extern void sysfs_close_bus(struct sysfs_bus *bus);
diff --git a/lib/sysfs_device.c b/lib/sysfs_device.c
index 593dbf9..e37789c 100644
--- a/lib/sysfs_device.c
+++ b/lib/sysfs_device.c
@@ -127,15 +127,12 @@ struct sysfs_device *sysfs_open_device(const unsigned char *path)
dev->directory = sdir;
strcpy(dev->bus_id, sdir->name);
- /* get device name */
- p = sysfs_get_value_from_attributes(sdir->attributes,
- SYSFS_NAME_ATTRIBUTE);
- if (p != NULL) {
- strncpy(dev->name, p, SYSFS_NAME_LEN);
- p = dev->name + strlen(dev->name) - 1;
- if ((strlen(dev->name) > 0) && *p == '\n')
- *p = '\0';
- }
+ /*
+ * The "name" attribute no longer exists... return the device's
+ * sysfs representation instead, in the "dev->name" field, which
+ * implies that the dev->name and dev->bus_id contain same data.
+ */
+ strncpy(dev->name, sdir->name, SYSFS_NAME_LEN);
return dev;
}
@@ -342,4 +339,57 @@ struct dlist *sysfs_get_device_attributes(struct sysfs_device *device)
return NULL;
return (device->directory->attributes);
-}
+}
+
+/**
+ * sysfs_open_device_by_id: open a device by id (use the "bus" subsystem)
+ * @dev_name: name of the device to open - has to be the name in
+ * /sys/bus/xxx/devices
+ * @bus: bus the device belongs to
+ * @bsize: size of the bus buffer
+ * returns struct sysfs_device if found, NULL otherwise
+ * NOTE: Use sysfs_close_device to close the device
+ */
+struct sysfs_device *sysfs_open_device_by_name(char *dev_name, char *bus,
+ size_t bsize)
+{
+ char sysfs_path[SYSFS_PATH_MAX], device_path[SYSFS_PATH_MAX];
+ struct sysfs_device *device = NULL;
+ struct stat astats;
+
+ if (dev_name == NULL || bus == NULL) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ if ((sysfs_find_device_bus_name(dev_name, bus, bsize)) != 0) {
+ dprintf("Device %s not found\n", dev_name);
+ errno = EINVAL;
+ return NULL;
+ }
+
+ memset(sysfs_path, 0, SYSFS_PATH_MAX);
+ if ((sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX)) != 0) {
+ dprintf("Error getting sysfs mount path\n");
+ return NULL;
+ }
+ strcat(sysfs_path, SYSFS_BUS_DIR);
+ strcat(sysfs_path, "/");
+ strncat(sysfs_path, bus, bsize);
+ strcat(sysfs_path, "/devices/"); /* sysfs_path = "/sys/bus/xxx/devices/" */
+ strcat(sysfs_path, dev_name);
+
+ /* devices under /sys/bus/xxx/devices are links to devices subsystem */
+ if ((sysfs_get_link(sysfs_path, device_path, SYSFS_PATH_MAX)) < 0) {
+ dprintf("Error getting device path\n");
+ return NULL;
+ }
+
+ device = sysfs_open_device(device_path);
+ if (device == NULL) {
+ dprintf("Error opening device %s\n", dev_name);
+ return NULL;
+ }
+
+ return device;
+}