summaryrefslogtreecommitdiff
path: root/cmd/lsbus.c
blob: 2cbe334c020c399a85310c13808fee36c9d68a54 (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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
 * lsbus.c
 *
 * Utility to list bus devices
 *
 * Copyright (C) IBM Corp. 2003
 *
 *	This program is free software; you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the
 *	Free Software Foundation version 2 of the License.
 * 
 *	This program 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
 *	General Public License for more details.
 * 
 *	You should have received a copy of the GNU General Public License along
 *	with this program; if not, write to the Free Software Foundation, Inc.,
 *	675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <mntent.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>

#include "libsysfs.h"
#include "names.h"

/* Command Options */
static int show_options = 0;		/* bitmask of show options */
static unsigned char *attr_to_show = NULL; /* print value for this attribute*/
static unsigned char *bus_device = NULL; /* print only this bus device */
struct pci_access *pacc = NULL;
unsigned char *bus_to_print = NULL;	

#define SHOW_ATTRIBUTES		0x01	/* show attributes command option */
#define SHOW_ATTRIBUTE_VALUE	0x02	/* show an attribute value option */
#define SHOW_DEVICES		0x04	/* show only devices option */
#define SHOW_DRIVERS		0x08	/* show only drivers option */
#define SHOW_ALL_ATTRIB_VALUES	0x10	/* show all attributes with values */
                                                                                
#define SHOW_ALL		0xff

static unsigned char cmd_options[] = "aA:dDhv";

/*
 * binary_files - defines existing sysfs binary files that should be printed
 * in hex.
 */
static unsigned char *binary_files[] = {
	"config",
	"data"
};

static int binfiles = 2;

static unsigned int get_pciconfig_word(int offset, unsigned char *buf)
{
        unsigned short val = (unsigned char)buf[offset] |
		                ((unsigned char)buf[offset+1] << 8);
        return val;
}

static unsigned char get_pciconfig_byte(int offset, unsigned char *buf)
{
        return((unsigned char)buf[offset]);
}


/**
 * usage: prints utility usage.
 */
void usage(void)
{
	fprintf(stdout, "Usage: lsbus [<options> bus [device]]\n");
	fprintf(stdout, "\t-a\t\t\tShow attributes\n");
	fprintf(stdout, "\t-d\t\t\tShow only devices\n");
	fprintf(stdout, "\t-h\t\t\tShow usage\n");
	fprintf(stdout, "\t-v\t\t\tShow all attributes with values\n");
	fprintf(stdout, "\t-A <attribute_name>\tShow attribute value\n");
	fprintf(stdout, "\t-D\t\t\tShow only drivers\n");
}

/**
 * remove_end_newline: removes newline on the end of an attribute value
 * @value: string to remove newline from
 */
void remove_end_newline(unsigned char *value)
{
	unsigned char *p = value + (strlen(value) - 1);

	if (p != NULL && *p == '\n')
		*p = '\0';
}

/**
 * isbinaryvalue: checks to see if attribute is binary or not.
 * @attr: attribute to check.
 * returns 1 if binary or 0 if not.
 */
int isbinaryvalue(struct sysfs_attribute *attr)
{
	int i;

	if (attr == NULL || attr->value == NULL)
		return 0;

	for (i = 0; i < binfiles; i++) 
		if ((strcmp(attr->name, binary_files[i])) == 0)
			return 1;

	return 0;
}

/**
 * print_attribute_value: prints out single attribute value.
 * @attr: attricute to print.
 */
void print_attribute_value(struct sysfs_attribute *attr)
{
	if (attr == NULL)
		return;
                                                                                
	if (attr->method & SYSFS_METHOD_SHOW) {
		if (isbinaryvalue(attr) != 0) {
			int i;
			fprintf(stdout, "\t: ");
			for (i = 0; i < attr->len; i++) {
				if (!(i %16)) {
					fprintf(stdout, "\n\t\t\t");
				} else if (!(i %8))
					fprintf(stdout, " ");
				fprintf(stdout, "%02x ",
					(unsigned char)attr->value[i]);
			}
			fprintf(stdout, "\n");
		} else if (attr->value != NULL && strlen(attr->value) > 0) {
			remove_end_newline(attr->value);
			fprintf(stdout, "\t: %s\n", attr->value);
		} else
			fprintf(stdout, "\n");
	} else {
		fprintf(stdout, "\t: store method only\n");
	}
}

/**
 * print_attribute: prints out a single attribute
 * @attr: attribute to print.
 */
void print_attribute(struct sysfs_attribute *attr)
{
	if (attr == NULL) 
		return;

	if (show_options & SHOW_ALL_ATTRIB_VALUES) {
		fprintf(stdout, "\t\t%s", attr->name);
		print_attribute_value(attr);

	} else if ((show_options & SHOW_ATTRIBUTES) || ((show_options 
	    & SHOW_ATTRIBUTE_VALUE) && (strcmp(attr->name, attr_to_show) 
	    == 0))) {
		fprintf (stdout, "\t\t%s", attr->name);
		if (show_options & SHOW_ATTRIBUTE_VALUE && attr->value != NULL
		    && (strcmp(attr->name, attr_to_show)) == 0) {
			print_attribute_value(attr);
		} else {
			fprintf(stdout, "\n");
		}
	}
}

/**
 * print_device_attributes: prints out device attributes.
 * @sdir: print this device's attributes/files.
 */
void print_device_attributes(struct dlist *attributes)
{
	struct sysfs_attribute *cur = NULL;;

	fprintf (stdout, "\tAttributes:\n");
	dlist_for_each_data(attributes, cur,
				struct sysfs_attribute) {
		print_attribute(cur);
	}
}

/**
 * print_device: prints out device information.
 * @device: device to print.
 */
void print_device(struct sysfs_device *device)
{
	struct dlist *attributes = NULL;
	unsigned int vendor_id, device_id;
	unsigned char buf[128], path[SYSFS_PATH_MAX], value[256];
	
	if (device != NULL) {
		if (!(strcmp(bus_to_print, "pci"))) {
			fprintf(stdout, "  %s: ", device->bus_id);
			memset(path, 0, SYSFS_PATH_MAX);
			memset(value, 0, SYSFS_PATH_MAX);
			strcpy(path, device->path);
			strcat(path, "/config");
			if ((sysfs_read_attribute_value(path,
						value, 256)) == 0) {
				vendor_id = get_pciconfig_word
						(PCI_VENDOR_ID, value);
				device_id = get_pciconfig_word
						(PCI_DEVICE_ID, value);
				fprintf(stdout, "%s\n",
					pci_lookup_name(pacc, buf, 128,
					PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
					vendor_id, device_id, 0, 0));
			} else
				fprintf(stdout, "\n");
		} else
			fprintf(stdout, "  %s:\n", device->bus_id);

		if (show_options & (SHOW_ATTRIBUTES | SHOW_ATTRIBUTE_VALUE |
					SHOW_ALL_ATTRIB_VALUES)) {
			attributes = sysfs_get_device_attributes(device);
			if (attributes != NULL) 
				print_device_attributes(attributes);
		}
		if (isalnum(device->driver_name[0])) 
			fprintf (stdout, "\tDriver: %s\n\n", 
						device->driver_name);
	}
}

/**
 * print_driver_attributes: prints out driver attributes .
 * @driver: print this driver's attributes.
 */
void print_driver_attributes(struct sysfs_driver *driver)
{
	struct dlist *attributes = NULL;
	
	if (driver != NULL) {
		attributes = sysfs_get_driver_attributes(driver);
		if (attributes != NULL) {
			struct sysfs_attribute *cur = NULL;

			fprintf (stdout, "\t%s Attributes:\n", driver->name);
			dlist_for_each_data(attributes, cur, 
					struct sysfs_attribute) {
				print_attribute(cur);
			}
		}
	}
}

/**
 * print_driver: prints out driver information.
 * @driver: driver to print.
 */
void print_driver(struct sysfs_driver *driver)
{
	struct dlist *devlist = NULL;
	
	if (driver != NULL) {
		fprintf (stdout, "  %s\n", driver->name);
		devlist = sysfs_get_driver_devices(driver);
		if (devlist != NULL) {
			struct sysfs_device *dev = NULL;
			fprintf(stdout, "\tDevices:\n");
			dlist_for_each_data(devlist, dev,
					struct sysfs_device)
				fprintf(stdout, "\t\t%s\n", dev->bus_id);
		}
		if(show_options & (SHOW_ATTRIBUTES | SHOW_ATTRIBUTE_VALUE
			    | SHOW_ALL_ATTRIB_VALUES))
			print_driver_attributes(driver);
	}
}

/**
 * print_default_bus: prints out everything on a bus.
 * @busname: bus to print.
 * returns 0 with success or 1 with error.
 */
int print_sysfs_bus(unsigned char *busname)
{
	struct sysfs_bus *bus = NULL;
	struct sysfs_device *curdev = NULL;
	struct sysfs_driver *curdrv = NULL;
	struct dlist *devlist = NULL;
	struct dlist *drvlist = NULL;

	if (busname == NULL) {
		errno = EINVAL;
		return 1;
	}
	bus = sysfs_open_bus(busname);
	if (bus == NULL) {
		fprintf(stderr, "Error opening bus %s\n", busname);
		return 1;
	}

	fprintf(stdout, "Bus: %s\n", busname);
	if (show_options & SHOW_DEVICES) {
		devlist = sysfs_get_bus_devices(bus);
		if (devlist != NULL) {
			if (bus_device == NULL)
				fprintf(stdout, "Devices:\n");
			dlist_for_each_data(devlist, curdev,
					struct sysfs_device) {
				if (bus_device == NULL || (strcmp(bus_device,
							curdev->bus_id) == 0)) 
					print_device(curdev);
			}
		}
	}
	if (show_options & SHOW_DRIVERS) {
		drvlist = sysfs_get_bus_drivers(bus);
		if (drvlist != NULL) {
			fprintf(stdout, "Drivers:\n");
			dlist_for_each_data(drvlist, curdrv, 
					struct sysfs_driver) {
				print_driver(curdrv);
			}
		}
	}
	sysfs_close_bus(bus);
	return 0;
}

/**
 * print_sysfs_buses: prints out supported buses.
 * @buses: list of supported system buses.
 * returns 0 with success or 1 with error.
 */
int print_sysfs_buses(void)
{
        unsigned char subsys[SYSFS_NAME_LEN];
        struct dlist *list = NULL;
        unsigned char *cur = NULL;
        int ret = 0;

	strcat(subsys, "/");
        strcpy(subsys, SYSFS_BUS_NAME);
        list = sysfs_open_subsystem_list(subsys);
        if (list != NULL) {
		fprintf(stdout, "Supported sysfs buses:\n");
		dlist_for_each_data(list, cur, char)
			fprintf(stdout, "\t%s\n", cur);
	}
	sysfs_close_list(list);
									
	return ret;
}

/* MAIN */
int main(int argc, char *argv[])
{
	//unsigned char *bus_to_print = NULL;
	int retval = 0;
	int opt;
	extern int optind;
	extern char *optarg;
	/*pci ids*/
	unsigned char *pci_id_file = "/usr/local/share/pci.ids";

	while((opt = getopt(argc, argv, cmd_options)) != EOF) {
		switch(opt) {
		case 'a':
			show_options |= SHOW_ATTRIBUTES;
			break;
		case 'A':
			if ((strlen(optarg) + 1) > SYSFS_NAME_LEN) {
				fprintf(stderr, 
					"Attribute name %s is too long\n",
					optarg);
				exit(1);
			}
			attr_to_show = optarg;
			show_options |= SHOW_ATTRIBUTE_VALUE;
			break;	
		case 'd':
			show_options |= SHOW_DEVICES;
			break;
		case 'D':
			show_options |= SHOW_DRIVERS;
			break;
		case 'h':
			usage();
			exit(0);
			break;
		case 'v':
			show_options |= SHOW_ALL_ATTRIB_VALUES;
			break;
		default:
			usage();
			exit(1);
		}
	}
	argc -= optind;
	argv += optind;

	switch(argc) {
	case 0:
		break;
	case 1:
		/* get bus to view */
		if ((strlen(*argv)+1) < SYSFS_NAME_LEN) 
			bus_to_print = *argv;
		else
			fprintf(stderr, 
				"Invalid argument: busname too long\n");
		break;
	case 2:
		/* get bus and device to view */
		if ((strlen(argv[0])) < SYSFS_NAME_LEN && (strlen(argv[1]))
	 	    < SYSFS_NAME_LEN) {
			bus_to_print = argv[0];
			bus_device = argv[1];
			show_options |= SHOW_DEVICES;
		} else {
			fprintf(stderr, 
				"Invalid argument: bus or device name too long\n");
			exit(1);
		}
		break;
	default:
		usage();
		exit(1);
	}

	if (bus_to_print == NULL && (show_options & (SHOW_ATTRIBUTES 
	    | SHOW_ATTRIBUTE_VALUE | SHOW_DEVICES | SHOW_DRIVERS
	    | SHOW_ALL_ATTRIB_VALUES))) {
		fprintf(stderr, "Please specify a bus\n");
		usage();
		exit(1);
	}
	/* default is to print devices */
	if (!(show_options & (SHOW_DEVICES | SHOW_DRIVERS)))
		show_options |= SHOW_DEVICES;

	if (bus_to_print != NULL) {
		if ((!(strcmp(bus_to_print, "pci"))) && 
				(show_options & SHOW_DEVICES)) {
			pacc = (struct pci_access *)calloc(1, sizeof(struct pci_access));
			pacc->pci_id_file_name = pci_id_file;
			pacc->numeric_ids = 0;
		}
		retval = print_sysfs_bus(bus_to_print);
	} else
		retval = print_sysfs_buses();

	if (bus_to_print != NULL) {
		if ((!(strcmp(bus_to_print, "pci"))) &&
				(show_options & SHOW_DEVICES)) {
			pci_free_name_list(pacc);
			free (pacc);
		}
	}
	exit(retval);
}