summaryrefslogtreecommitdiff
path: root/lib/sysfs_device.c
blob: e37789c8ef33971a17f60fadfdffbb34672cd0ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 * sysfs_device.c
 *
 * Generic device utility functions for libsysfs
 *
 * Copyright (C) IBM Corp. 2003
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */
#include "libsysfs.h"
#include "sysfs.h"

/**
 * sysfs_del_device: routine for dlist integration
 */
static void sysfs_del_device(void *dev)
{
	sysfs_close_device((struct sysfs_device *)dev);
}

/**
 * sysfs_close_dev_tree: routine for dlist integration
 */
static void sysfs_close_dev_tree(void *dev)
{
	sysfs_close_device_tree((struct sysfs_device *)dev);
}

/**
 * sysfs_close_device: closes and cleans up a device
 * @dev = device to clean up
 */
void sysfs_close_device(struct sysfs_device *dev)
{
	if (dev != NULL) {
		if (dev->directory != NULL)
			sysfs_close_directory(dev->directory);
		if (dev->children != NULL && dev->children->count == 0)
			dlist_destroy(dev->children);
		free(dev);
	}
}

/**
 * alloc_device: allocates and initializes device structure
 * returns struct sysfs_device
 */
static struct sysfs_device *alloc_device(void)
{
	return (struct sysfs_device *)calloc(1, sizeof(struct sysfs_device));
}

/**
 * sysfs_get_device_attr: searches dev's attributes by name
 * @dev: device to look through
 * @name: attribute name to get
 * returns sysfs_attribute reference with success or NULL with error.
 */
struct sysfs_attribute *sysfs_get_device_attr(struct sysfs_device *dev,
						const unsigned char *name)
{
	struct sysfs_attribute *cur = NULL;
	unsigned char attrname[SYSFS_NAME_LEN];

	if (dev == NULL || dev->directory == NULL 
	    || dev->directory->attributes == NULL || name == NULL) {
		errno = EINVAL;
		return NULL;
	}
	dlist_for_each_data(dev->directory->attributes, cur,
			struct sysfs_attribute) {
		if (strcmp(cur->name, attrname) != 0)
			continue;

		return cur;
	}

	return NULL;
}

/**
 * sysfs_open_device: opens and populates device structure
 * @path: path to device, this is the /sys/devices/ path
 * returns sysfs_device structure with success or NULL with error
 */
struct sysfs_device *sysfs_open_device(const unsigned char *path)
{
	struct sysfs_device *dev = NULL;
	struct sysfs_directory *sdir = NULL;
	unsigned char *p = NULL;

	if (path == NULL) {
		errno = EINVAL;
		return NULL;
	}
	dev = alloc_device();	
	if (dev == NULL) {
		dprintf("Error allocating device at %s\n", path);
		return NULL;
	}
	sdir = sysfs_open_directory(path);
	if (sdir == NULL) {
		dprintf("Invalid device at %s\n", path);
		errno = EINVAL;
		sysfs_close_device(dev);
		return NULL;
	}
	if ((sysfs_read_directory(sdir)) != 0) {
		dprintf("Error reading device directory at %s\n", path);
		sysfs_close_directory(sdir);
		sysfs_close_device(dev);
		return NULL;
	}
	dev->directory = sdir;
	strcpy(dev->bus_id, sdir->name);

	/* 
	 * 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;
}

/**
 * sysfs_close_device_tree: closes every device in the supplied tree, 
 * 	closing children only.
 * @devroot: device root of tree.
 */
void sysfs_close_device_tree(struct sysfs_device *devroot)
{
	if (devroot != NULL) {
		if (devroot->children != NULL) {
			struct sysfs_device *child = NULL;

			dlist_for_each_data(devroot->children, child,
					struct sysfs_device) {
				sysfs_close_device_tree(child);
			}
		}
		sysfs_close_device(devroot);
	}
}

/**
 * sysfs_open_device_tree: opens root device and all of its children,
 *	creating a tree of devices. Only opens children.
 * @path: sysfs path to devices
 * returns struct sysfs_device and its children with success or NULL with
 *	error.
 */
struct sysfs_device *sysfs_open_device_tree(const unsigned char *path)
{
	struct sysfs_device *rootdev = NULL, *new = NULL;
	struct sysfs_directory *cur = NULL;

	if (path == NULL) {
		errno = EINVAL;
		return NULL;
	}
	rootdev = sysfs_open_device(path);
	if (rootdev == NULL) {
		dprintf("Error opening root device at %s\n", path);
		return NULL;
	}
	if (rootdev->directory->subdirs != NULL) {
		dlist_for_each_data(rootdev->directory->subdirs, cur,
				struct sysfs_directory) {
			new = sysfs_open_device_tree(cur->path);
			if (new == NULL) {
				dprintf("Error opening device tree at %s\n",
					cur->path);
				sysfs_close_device_tree(rootdev);
				return NULL;
			}
			if (rootdev->children == NULL)
				rootdev->children = dlist_new_with_delete
					(sizeof(struct sysfs_device),
					sysfs_del_device);
			dlist_unshift(rootdev->children, new);
		}
	}

	return rootdev;
}

/**
 * sysfs_close_root_device: closes root and all devices
 * @root: root device to close
 */
void sysfs_close_root_device(struct sysfs_root_device *root)
{
	if (root != NULL) {
		if (root->devices != NULL)
			dlist_destroy(root->devices);
		if (root->directory != NULL)
			sysfs_close_directory(root->directory);
		free(root);
	}
}

/**
 * open_root_device_dir: opens up sysfs_directory for specific root dev
 * @name: name of root
 * returns struct sysfs_directory with success and NULL with error
 */
static struct sysfs_directory *open_root_device_dir(const unsigned char *name)
{
	struct sysfs_directory *rdir = NULL;
	unsigned char rootpath[SYSFS_PATH_MAX];

	if (name == NULL) {
		errno = EINVAL;
		return NULL;
	}

	memset(rootpath, 0, SYSFS_PATH_MAX);
	if (sysfs_get_mnt_path(rootpath, SYSFS_PATH_MAX) != 0) {
		dprintf ("Sysfs not supported on this system\n");
		return NULL;
	}

	strcat(rootpath, SYSFS_DEVICES_DIR);
	strcat(rootpath, "/");
	strcat(rootpath, name);
	rdir = sysfs_open_directory(rootpath);
	if (rdir == NULL) {
		errno = EINVAL;
		dprintf ("Root device %s not supported on this system\n",
			name);
		return NULL;
	}
	if (sysfs_read_directory(rdir) != 0) {
		dprintf ("Error reading %s root device at dir %s\n", name,
			rootpath);
		sysfs_close_directory(rdir);
		return NULL;
	}
	
	return rdir;
}

/**
 * get_all_root_devices: opens up all the devices under this root device
 * @root: root device to open devices for
 * returns 0 with success and -1 with error
 */
static int get_all_root_devices(struct sysfs_root_device *root)
{
	struct sysfs_device *dev = NULL;
	struct sysfs_directory *cur = NULL;

	if (root == NULL || root->directory == NULL) {
		errno = EINVAL;
		return -1;
	}
	if (root->directory->subdirs == NULL)
		return 0;

	dlist_for_each_data(root->directory->subdirs, cur,
			struct sysfs_directory) {
		dev = sysfs_open_device_tree(cur->path);
		if (dev == NULL) {
			dprintf ("Error opening device at %s\n", cur->path);
			continue;
		}
		if (root->devices == NULL)
			root->devices = dlist_new_with_delete
				(sizeof(struct sysfs_device), 
				sysfs_close_dev_tree);
		dlist_unshift(root->devices, dev);
	}

	return 0;
}

/**
 * sysfs_open_root_device: opens sysfs devices root and all of its
 *	devices.
 * @name: name of /sys/devices/root to open
 * returns struct sysfs_root_device if success and NULL with error
 */
struct sysfs_root_device *sysfs_open_root_device(const unsigned char *name)
{
	struct sysfs_root_device *root = NULL;
	struct sysfs_directory *rootdir = NULL;

	if (name == NULL) {
		errno = EINVAL;
		return NULL;
	}

	root = (struct sysfs_root_device *)calloc
					(1, sizeof(struct sysfs_root_device));
	if (root == NULL) {
		perror("calloc");
		return NULL;
	}
	rootdir = open_root_device_dir(name);
	if (rootdir == NULL) {
		dprintf ("Invalid root device, %s not supported\n", name);
		sysfs_close_root_device(root);
		return NULL;
	}
	strcpy(root->path, rootdir->path);
	root->directory = rootdir;
	if (get_all_root_devices(root) != 0) {
		dprintf ("Error retrieving devices for root %s\n", name);
		sysfs_close_root_device(root);
		return NULL;
	}

	return root;
}

/**
 * sysfs_get_device_attributes: returns a dlist of attributes corresponding to
 * 	the specific device
 * @device: struct sysfs_device * for which attributes are to be returned
 */
struct dlist *sysfs_get_device_attributes(struct sysfs_device *device)
{
	if (device == NULL || device->directory == NULL) 
		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;
}