4 * Driver utility functions for libsysfs
6 * Copyright (C) IBM Corp. 2003-2005
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 static void sysfs_close_driver_device(void *device)
28 sysfs_close_device((struct sysfs_device *)device);
32 * sysfs_close_driver: closes driver and deletes device lists too
33 * @driver: driver to close
35 void sysfs_close_driver(struct sysfs_driver *driver)
39 dlist_destroy(driver->devices);
41 dlist_destroy(driver->attrlist);
47 * alloc_driver: allocates and initializes driver
48 * returns struct sysfs_driver with success and NULL with error.
50 static struct sysfs_driver *alloc_driver(void)
52 return (struct sysfs_driver *)calloc(1, sizeof(struct sysfs_driver));
56 * get_driver_bus: gets bus the driver is on
57 * Returns 0 on success and 1 on error
59 static int get_driver_bus(struct sysfs_driver *drv)
61 char drvpath[SYSFS_PATH_MAX], *c = NULL;
68 safestrcpy(drvpath, drv->path);
69 c = strstr(drvpath, SYSFS_DRIVERS_NAME);
73 c = strstr(drvpath, SYSFS_BUS_NAME);
80 safestrcpy(drv->bus, c);
85 * sysfs_get_driver_attr: searches drv's attributes by name
86 * @drv: driver to look through
87 * @name: attribute name to get
88 * returns sysfs_attribute reference with success or NULL with error.
90 struct sysfs_attribute *sysfs_get_driver_attr(struct sysfs_driver *drv,
97 return get_attribute(drv, (char *)name);
101 * sysfs_get_driver_attributes: gets list of driver attributes
102 * @dev: driver whose attributes list is needed
103 * returns dlist of attributes on success or NULL on error
105 struct dlist *sysfs_get_driver_attributes(struct sysfs_driver *drv)
111 return get_attributes_list(drv);
115 * sysfs_open_driver_path: opens and initializes driver structure
116 * @path: path to driver directory
117 * returns struct sysfs_driver with success and NULL with error
119 struct sysfs_driver *sysfs_open_driver_path(const char *path)
121 struct sysfs_driver *driver = NULL;
127 if (sysfs_path_is_dir(path)) {
128 dprintf("Invalid path to driver: %s\n", path);
131 driver = alloc_driver();
133 dprintf("Error allocating driver at %s\n", path);
136 if (sysfs_get_name_from_path(path, driver->name, SYSFS_NAME_LEN)) {
137 dprintf("Error getting driver name from path\n");
141 safestrcpy(driver->path, path);
142 if (sysfs_remove_trailing_slash(driver->path)) {
143 dprintf("Invalid path to driver %s\n", driver->path);
144 sysfs_close_driver(driver);
147 if (get_driver_bus(driver)) {
148 dprintf("Could not get the bus driver is on\n");
149 sysfs_close_driver(driver);
157 * get_driver_path: looks up the bus the driver is on and builds path to
159 * @bus: bus on which to search
160 * @drv: driver to look for
161 * @path: buffer to return path to driver
162 * @psize: size of "path"
163 * Returns 0 on success and -1 on error
165 static int get_driver_path(const char *bus, const char *drv,
166 char *path, size_t psize)
168 if (!bus || !drv || !path || psize == 0) {
172 if (sysfs_get_mnt_path(path, psize)) {
173 dprintf("Error getting sysfs mount path\n");
176 safestrcatmax(path, "/", psize);
177 safestrcatmax(path, SYSFS_BUS_NAME, psize);
178 safestrcatmax(path, "/", psize);
179 safestrcatmax(path, bus, psize);
180 safestrcatmax(path, "/", psize);
181 safestrcatmax(path, SYSFS_DRIVERS_NAME, psize);
182 safestrcatmax(path, "/", psize);
183 safestrcatmax(path, drv, psize);
188 * sysfs_open_driver: open driver by name, given its bus
189 * @bus_name: Name of the bus
190 * @drv_name: Name of the driver
191 * Returns the sysfs_driver reference on success and NULL on failure
193 struct sysfs_driver *sysfs_open_driver(const char *bus_name,
194 const char *drv_name)
196 char path[SYSFS_PATH_MAX];
197 struct sysfs_driver *driver = NULL;
199 if (!drv_name || !bus_name) {
204 memset(path, 0, SYSFS_PATH_MAX);
205 if (get_driver_path(bus_name, drv_name, path, SYSFS_PATH_MAX)) {
206 dprintf("Error getting to driver %s\n", drv_name);
209 driver = sysfs_open_driver_path(path);
211 dprintf("Error opening driver at %s\n", path);
218 * sysfs_get_driver_devices: gets list of devices that use the driver
219 * @drv: sysfs_driver whose device list is needed
220 * Returns dlist of struct sysfs_device on success and NULL on failure
222 struct dlist *sysfs_get_driver_devices(struct sysfs_driver *drv)
225 struct dlist *linklist = NULL;
226 struct sysfs_device *dev = NULL;
233 linklist = read_dir_links(drv->path);
235 dlist_for_each_data(linklist, ln, char) {
237 if (!strncmp(ln, SYSFS_MODULE_NAME, strlen(ln)))
240 dev = sysfs_open_device(drv->bus, ln);
242 dprintf("Error opening driver's device\n");
243 sysfs_close_list(linklist);
247 drv->devices = dlist_new_with_delete
248 (sizeof(struct sysfs_device),
249 sysfs_close_driver_device);
251 dprintf("Error creating device list\n");
252 sysfs_close_list(linklist);
256 dlist_unshift_sorted(drv->devices, dev, sort_list);
258 sysfs_close_list(linklist);