]>
Commit | Line | Data |
---|---|---|
070f5422 GKH |
1 | /* |
2 | * sysfs_driver.c | |
3 | * | |
4 | * Driver utility functions for libsysfs | |
5 | * | |
fe3fe3b2 | 6 | * Copyright (C) IBM Corp. 2003 |
070f5422 GKH |
7 | * |
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. | |
12 | * | |
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. | |
17 | * | |
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 | |
21 | * | |
22 | */ | |
23 | #include "libsysfs.h" | |
24 | #include "sysfs.h" | |
25 | ||
5d4754f1 | 26 | static void sysfs_close_driver_device(void *device) |
fe3fe3b2 DS |
27 | { |
28 | sysfs_close_device((struct sysfs_device *)device); | |
29 | } | |
30 | ||
fe3fe3b2 | 31 | /** |
5d4754f1 | 32 | * sysfs_close_driver: closes driver and deletes device lists too |
fe3fe3b2 DS |
33 | * @driver: driver to close |
34 | */ | |
5d4754f1 | 35 | void sysfs_close_driver(struct sysfs_driver *driver) |
fe3fe3b2 DS |
36 | { |
37 | if (driver != NULL) { | |
38 | if (driver->devices != NULL) | |
39 | dlist_destroy(driver->devices); | |
40 | if (driver->directory != NULL) | |
41 | sysfs_close_directory(driver->directory); | |
42 | free(driver); | |
43 | } | |
44 | } | |
45 | ||
5d4754f1 DS |
46 | /** |
47 | * open_driver_dir: Open the sysfs_directory for this driver | |
48 | * @driver: Driver whose directory to be opened | |
49 | * Returns 0 on success and 1 on failure | |
50 | */ | |
51 | static int open_driver_dir(struct sysfs_driver *driver) | |
52 | { | |
53 | if (driver == NULL) { | |
54 | errno = EINVAL; | |
55 | return 1; | |
56 | } | |
57 | if (driver->directory == NULL) { | |
58 | driver->directory = sysfs_open_directory(driver->path); | |
59 | if (driver->directory == NULL) { | |
60 | dprintf("Error opening driver directory at %s\n", | |
61 | driver->path); | |
62 | return 1; | |
63 | } | |
64 | } | |
65 | return 0; | |
66 | } | |
67 | ||
070f5422 GKH |
68 | /** |
69 | * alloc_driver: allocates and initializes driver | |
70 | * returns struct sysfs_driver with success and NULL with error. | |
71 | */ | |
72 | static struct sysfs_driver *alloc_driver(void) | |
73 | { | |
74 | return (struct sysfs_driver *)calloc(1, sizeof(struct sysfs_driver)); | |
75 | } | |
76 | ||
77 | /** | |
bcbe2d8e | 78 | * sysfs_open_driver_path: opens and initializes driver structure |
070f5422 GKH |
79 | * @path: path to driver directory |
80 | * returns struct sysfs_driver with success and NULL with error | |
81 | */ | |
bcbe2d8e | 82 | struct sysfs_driver *sysfs_open_driver_path(const unsigned char *path) |
070f5422 GKH |
83 | { |
84 | struct sysfs_driver *driver = NULL; | |
070f5422 GKH |
85 | |
86 | if (path == NULL) { | |
87 | errno = EINVAL; | |
88 | return NULL; | |
89 | } | |
5d4754f1 DS |
90 | if ((sysfs_path_is_dir(path)) != 0) { |
91 | dprintf("Invalid path to driver: %s\n", path); | |
070f5422 GKH |
92 | return NULL; |
93 | } | |
94 | driver = alloc_driver(); | |
95 | if (driver == NULL) { | |
fe3fe3b2 | 96 | dprintf("Error allocating driver at %s\n", path); |
070f5422 GKH |
97 | return NULL; |
98 | } | |
5d4754f1 DS |
99 | if ((sysfs_get_name_from_path(path, driver->name, |
100 | SYSFS_NAME_LEN)) != 0) { | |
101 | dprintf("Error getting driver name from path\n"); | |
102 | free(driver); | |
103 | return NULL; | |
104 | } | |
105 | strcpy(driver->path, path); | |
616a7078 AM |
106 | if ((sysfs_remove_trailing_slash(driver->path)) != 0) { |
107 | dprintf("Invalid path to driver %s\n", driver->path); | |
108 | sysfs_close_driver(driver); | |
109 | return NULL; | |
110 | } | |
070f5422 GKH |
111 | |
112 | return driver; | |
113 | } | |
fe3fe3b2 DS |
114 | |
115 | /** | |
116 | * sysfs_get_driver_attributes: gets list of attributes for the given driver | |
117 | * @driver: sysfs_driver for which attributes are required | |
118 | * returns a dlist of attributes corresponding to the driver if present | |
119 | * NULL otherwise | |
120 | */ | |
121 | struct dlist *sysfs_get_driver_attributes(struct sysfs_driver *driver) | |
122 | { | |
5d4754f1 DS |
123 | if (driver == NULL) { |
124 | errno = EINVAL; | |
fe3fe3b2 | 125 | return NULL; |
5d4754f1 | 126 | } |
fe3fe3b2 | 127 | |
5d4754f1 DS |
128 | if (driver->directory == NULL) { |
129 | if ((open_driver_dir(driver)) == 1) | |
130 | return NULL; | |
131 | } | |
132 | if (driver->directory->attributes == NULL) { | |
616a7078 | 133 | if ((sysfs_read_dir_attributes(driver->directory)) != 0) |
5d4754f1 | 134 | return NULL; |
5d4754f1 | 135 | } |
fe3fe3b2 DS |
136 | return(driver->directory->attributes); |
137 | } | |
138 | ||
616a7078 AM |
139 | /** |
140 | * sysfs_refresh_driver_attributes: refreshes the driver's list of attributes | |
141 | * @driver: sysfs_driver whose attributes to refresh | |
142 | * | |
143 | * NOTE: Upon return, prior references to sysfs_attributes for this driver | |
144 | * _may_ not be valid | |
145 | * | |
146 | * Returns list of attributes on success and NULL on failure | |
147 | */ | |
148 | struct dlist *sysfs_refresh_driver_attributes(struct sysfs_driver *driver) | |
149 | { | |
150 | if (driver == NULL) { | |
151 | errno = EINVAL; | |
152 | return NULL; | |
153 | } | |
154 | if (driver->directory == NULL) | |
155 | return (sysfs_get_driver_attributes(driver)); | |
156 | ||
157 | if ((sysfs_refresh_dir_attributes(driver->directory)) != 0) { | |
158 | dprintf("Error refreshing driver attributes\n"); | |
159 | return NULL; | |
160 | } | |
161 | return (driver->directory->attributes); | |
162 | } | |
163 | ||
fe3fe3b2 DS |
164 | /** |
165 | * sysfs_get_driver_attr: searches driver's attributes by name | |
166 | * @drv: driver to look through | |
167 | * @name: attribute name to get | |
168 | * returns sysfs_attribute reference on success or NULL with error | |
169 | */ | |
170 | struct sysfs_attribute *sysfs_get_driver_attr(struct sysfs_driver *drv, | |
171 | const unsigned char *name) | |
172 | { | |
5d4754f1 | 173 | struct dlist *attrlist = NULL; |
fe3fe3b2 | 174 | |
5d4754f1 | 175 | if (drv == NULL) { |
fe3fe3b2 DS |
176 | errno = EINVAL; |
177 | return NULL; | |
178 | } | |
5d4754f1 DS |
179 | |
180 | attrlist = sysfs_get_driver_attributes(drv); | |
bcbe2d8e | 181 | if (attrlist != NULL) |
616a7078 AM |
182 | return NULL; |
183 | ||
184 | return sysfs_get_directory_attribute(drv->directory, | |
5d4754f1 | 185 | (unsigned char *)name); |
fe3fe3b2 DS |
186 | } |
187 | ||
188 | /** | |
189 | * sysfs_get_driver_links: gets list of links from the given driver | |
190 | * @driver: sysfs_driver for which links list is required | |
191 | * returns a dlist of links corresponding to the driver if present | |
192 | * NULL otherwise | |
193 | */ | |
194 | struct dlist *sysfs_get_driver_links(struct sysfs_driver *driver) | |
195 | { | |
5d4754f1 DS |
196 | if (driver == NULL) { |
197 | errno = EINVAL; | |
fe3fe3b2 | 198 | return NULL; |
5d4754f1 DS |
199 | } |
200 | if (driver->directory == NULL) { | |
201 | if ((open_driver_dir(driver)) == 1) | |
202 | return NULL; | |
bcbe2d8e | 203 | if ((sysfs_read_dir_links(driver->directory)) != 0) |
5d4754f1 DS |
204 | return NULL; |
205 | } | |
fe3fe3b2 DS |
206 | return(driver->directory->links); |
207 | } | |
208 | ||
5d4754f1 DS |
209 | /** |
210 | * sysfs_get_driver_devices: open up the list of devices this driver supports | |
211 | * @driver: sysfs_driver for which devices are needed | |
212 | * Returns dlist of devices on SUCCESS or NULL with ERROR | |
213 | */ | |
214 | struct dlist *sysfs_get_driver_devices(struct sysfs_driver *driver) | |
215 | { | |
216 | struct sysfs_link *curlink = NULL; | |
217 | struct sysfs_device *device = NULL; | |
218 | ||
219 | if (driver == NULL) { | |
220 | errno = EINVAL; | |
221 | return NULL; | |
222 | } | |
223 | ||
224 | if (driver->devices != NULL) | |
225 | return (driver->devices); | |
226 | ||
227 | if (driver->directory == NULL) { | |
228 | if ((open_driver_dir(driver)) == 1) | |
229 | return NULL; | |
bcbe2d8e | 230 | if ((sysfs_read_dir_links(driver->directory)) != 0) |
5d4754f1 DS |
231 | return NULL; |
232 | } | |
233 | if (driver->directory->links != NULL) { | |
234 | dlist_for_each_data(driver->directory->links, curlink, | |
235 | struct sysfs_link) { | |
bcbe2d8e | 236 | device = sysfs_open_device_path(curlink->target); |
5d4754f1 DS |
237 | if (device == NULL) { |
238 | dprintf("Error opening device at %s\n", | |
239 | curlink->target); | |
240 | return NULL; | |
241 | } | |
242 | strcpy(device->driver_name, driver->name); | |
243 | if (driver->devices == NULL) | |
244 | driver->devices = dlist_new_with_delete | |
245 | (sizeof(struct sysfs_device), | |
246 | sysfs_close_driver_device); | |
247 | dlist_unshift(driver->devices, device); | |
248 | } | |
249 | } | |
250 | return (driver->devices); | |
251 | } | |
252 | ||
616a7078 AM |
253 | /** |
254 | * sysfs_refresh_driver_devices: Refreshes drivers list of devices | |
255 | * @driver: sysfs_driver whose devices list needs to be refreshed | |
256 | * | |
257 | * NOTE: Upon return from this function, prior sysfs_device references from | |
258 | * this driver's list of devices _may_ not be valid | |
259 | * | |
260 | * Returns dlist of devices on success and NULL on failure | |
261 | */ | |
262 | struct dlist *sysfs_refresh_driver_devices(struct sysfs_driver *driver) | |
263 | { | |
264 | if (driver == NULL) { | |
265 | errno = EINVAL; | |
266 | return NULL; | |
267 | } | |
268 | ||
269 | if (driver->devices != NULL) { | |
270 | dlist_destroy(driver->devices); | |
271 | driver->devices = NULL; | |
272 | } | |
273 | ||
274 | if (driver->directory == NULL) | |
275 | return (sysfs_get_driver_devices(driver)); | |
276 | ||
277 | if ((sysfs_refresh_dir_links(driver->directory)) != 0) { | |
278 | dprintf("Error refreshing driver links\n"); | |
279 | return NULL; | |
280 | } | |
281 | ||
282 | return (sysfs_get_driver_devices(driver)); | |
283 | } | |
284 | ||
5d4754f1 DS |
285 | /** |
286 | * sysfs_get_driver_device: looks up a device from a list of driver's devices | |
287 | * and returns its sysfs_device corresponding to it | |
288 | * @driver: sysfs_driver on which to search | |
289 | * @name: name of the device to search | |
290 | * Returns a sysfs_device if found, NULL otherwise | |
291 | */ | |
292 | struct sysfs_device *sysfs_get_driver_device(struct sysfs_driver *driver, | |
293 | const unsigned char *name) | |
294 | { | |
295 | struct sysfs_device *device = NULL; | |
296 | struct dlist *devlist = NULL; | |
297 | ||
298 | if (driver == NULL || name == NULL) { | |
299 | errno = EINVAL; | |
300 | return NULL; | |
301 | } | |
302 | ||
303 | if (driver->devices == NULL) { | |
304 | devlist = sysfs_get_driver_devices(driver); | |
305 | if (devlist == NULL) { | |
306 | dprintf("Error getting driver devices\n"); | |
307 | return NULL; | |
308 | } | |
309 | } | |
310 | dlist_for_each_data(driver->devices, device, struct sysfs_device) { | |
311 | if (!(strncmp(device->name, name, SYSFS_NAME_LEN))) | |
312 | return device; | |
313 | } | |
314 | return NULL; | |
315 | } | |
316 | ||
ff44a6b0 DS |
317 | /** |
318 | * get_driver_path: looks up the bus the driver is on and builds path to | |
319 | * the driver. | |
320 | * @bus: bus on which to search | |
321 | * @drv: driver to look for | |
322 | * @path: buffer to return path to driver | |
323 | * @psize: size of "path" | |
324 | * Returns 0 on success and -1 on error | |
325 | */ | |
5d4754f1 DS |
326 | static int get_driver_path(const unsigned char *bus, |
327 | const unsigned char *drv, unsigned char *path, size_t psize) | |
ff44a6b0 DS |
328 | { |
329 | if (bus == NULL || drv == NULL || path == NULL) { | |
330 | errno = EINVAL; | |
331 | return -1; | |
332 | } | |
333 | if (sysfs_get_mnt_path(path, psize) != 0) { | |
334 | dprintf("Error getting sysfs mount path\n"); | |
335 | return -1; | |
336 | } | |
616a7078 | 337 | strcat(path, "/"); |
edcd3364 | 338 | strcat(path, SYSFS_BUS_NAME); |
ff44a6b0 DS |
339 | strcat(path, "/"); |
340 | strcat(path, bus); | |
5d4754f1 DS |
341 | strcat(path, "/"); |
342 | strcat(path, SYSFS_DRIVERS_NAME); | |
ff44a6b0 DS |
343 | strcat(path, "/"); |
344 | strcat(path, drv); | |
345 | return 0; | |
346 | } | |
347 | ||
fe3fe3b2 | 348 | /** |
ff44a6b0 DS |
349 | * sysfs_open_driver_attr: read the user supplied driver attribute |
350 | * @bus: bus on which to look | |
fe3fe3b2 DS |
351 | * @drv: driver whose attribute has to be read |
352 | * @attrib: Attribute to be read | |
ff44a6b0 DS |
353 | * Returns struct sysfs_attribute on success and NULL on failure |
354 | * | |
355 | * NOTE: | |
356 | * A call to sysfs_close_attribute() is required to close the | |
357 | * attribute returned and to free memory | |
fe3fe3b2 | 358 | */ |
ff44a6b0 DS |
359 | struct sysfs_attribute *sysfs_open_driver_attr(const unsigned char *bus, |
360 | const unsigned char *drv, const unsigned char *attrib) | |
fe3fe3b2 DS |
361 | { |
362 | struct sysfs_attribute *attribute = NULL; | |
363 | unsigned char path[SYSFS_PATH_MAX]; | |
364 | ||
ff44a6b0 | 365 | if (bus == NULL || drv == NULL || attrib == NULL) { |
fe3fe3b2 | 366 | errno = EINVAL; |
ff44a6b0 | 367 | return NULL; |
fe3fe3b2 DS |
368 | } |
369 | ||
bcbe2d8e | 370 | memset(path, 0, SYSFS_PATH_MAX); |
ff44a6b0 | 371 | if ((get_driver_path(bus, drv, path, SYSFS_PATH_MAX)) != 0) { |
fe3fe3b2 | 372 | dprintf("Error getting to driver %s\n", drv); |
ff44a6b0 | 373 | return NULL; |
fe3fe3b2 DS |
374 | } |
375 | strcat(path, "/"); | |
376 | strcat(path, attrib); | |
377 | attribute = sysfs_open_attribute(path); | |
378 | if (attribute == NULL) { | |
379 | dprintf("Error opening attribute %s for driver %s\n", | |
380 | attrib, drv); | |
ff44a6b0 | 381 | return NULL; |
fe3fe3b2 DS |
382 | } |
383 | if ((sysfs_read_attribute(attribute)) != 0) { | |
384 | dprintf("Error reading attribute %s for driver %s\n", | |
385 | attrib, drv); | |
386 | sysfs_close_attribute(attribute); | |
ff44a6b0 | 387 | return NULL; |
fe3fe3b2 | 388 | } |
ff44a6b0 | 389 | return attribute; |
fe3fe3b2 DS |
390 | } |
391 | ||
bcbe2d8e DS |
392 | /** |
393 | * sysfs_open_driver: open driver by name, given its bus | |
394 | * @drv_name: Name of the driver | |
395 | * @bus_name: Name of the bus | |
396 | * Returns the sysfs_driver reference on success and NULL on failure | |
397 | */ | |
398 | struct sysfs_driver *sysfs_open_driver(const unsigned char *drv_name, | |
399 | const unsigned char *bus_name) | |
400 | { | |
401 | unsigned char path[SYSFS_PATH_MAX]; | |
402 | struct sysfs_driver *driver = NULL; | |
403 | ||
404 | if (drv_name == NULL || bus_name == NULL) { | |
405 | errno = EINVAL; | |
406 | return NULL; | |
407 | } | |
408 | ||
409 | memset(path, 0, SYSFS_PATH_MAX); | |
410 | if ((get_driver_path(bus_name, drv_name, path, SYSFS_PATH_MAX)) != 0) { | |
411 | dprintf("Error getting to driver %s\n", drv_name); | |
412 | return NULL; | |
413 | } | |
414 | driver = sysfs_open_driver_path(path); | |
415 | if (driver == NULL) { | |
416 | dprintf("Error opening driver at %s\n", path); | |
417 | return NULL; | |
418 | } | |
419 | return driver; | |
420 | } | |
421 |