4 * Driver utility functions for libsysfs
6 * Copyright (C) 2003 International Business Machines, Inc.
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
27 * sysfs_close_driver: closes and cleans up driver structure
28 * @driver: driver to close
30 void sysfs_close_driver(struct sysfs_driver *driver)
33 if (driver->directory != NULL)
34 sysfs_close_directory(driver->directory);
40 * alloc_driver: allocates and initializes driver
41 * returns struct sysfs_driver with success and NULL with error.
43 static struct sysfs_driver *alloc_driver(void)
45 return (struct sysfs_driver *)calloc(1, sizeof(struct sysfs_driver));
49 * sysfs_open_driver: opens and initializes driver structure
50 * @path: path to driver directory
51 * returns struct sysfs_driver with success and NULL with error
53 struct sysfs_driver *sysfs_open_driver(const char *path)
55 struct sysfs_driver *driver = NULL;
56 struct sysfs_directory *sdir = NULL;
57 char devname[SYSFS_NAME_LEN];
63 sdir = sysfs_open_directory(path);
65 dprintf (stderr, "Error opening directory %s\n", path);
68 if ((sysfs_read_directory(sdir)) != 0) {
69 dprintf (stderr, "Error reading directory %s\n", path);
70 sysfs_close_directory(sdir);
73 driver = alloc_driver();
75 dprintf(stderr, "Error allocating driver at %s\n", path);
76 sysfs_close_directory(sdir);
79 if ((sysfs_get_name_from_path(path, devname, SYSFS_NAME_LEN)) != 0) {
80 dprintf (stderr, "Error reading directory %s\n", path);
81 sysfs_close_directory(sdir);
85 strncpy(driver->name, devname, sizeof(driver->name));
86 driver->directory = sdir;