[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH] util-linux/lspci.c util-linux/lsusb.c - human readable output
From:       Sven Dickert <Sven.Dickert () remote ! org>
Date:       2010-04-20 11:52:04
Message-ID: 20100420115204.GA23830 () remote ! org
[Download RAW message or body]

Hello,

  ever thought about getting real vendor and device names out of 
lspci or lsusb? 

Patch the attached file against BusyBox 1.16.[0-1], 
set CONFIG_LSIDS=y in .config,
compile and get:

--------------------- snip -------------------
user@home:/home/user $ lspci
00:00.0 Host bridge: Intel Corporation Mobile 4 Series Chipset Memory Controller Hub
00:02.0 VGA compatible controller: Intel Corporation Mobile 4 Series Chipset \
Integrated Graphics Controller 00:02.1 Display controller: Intel Corporation Mobile 4 \
Series Chipset Integrated Graphics Controller 00:03.0 Communication controller: Intel \
Corporation Mobile 4 Series Chipset MEI Controller 00:19.0 Ethernet controller: Intel \
Corporation 82567LF Gigabit Network Connection 00:1a.0 USB Controller: Intel \
Corporation 82801I (ICH9 Family) USB UHCI Controller #4 00:1a.1 USB Controller: Intel \
Corporation 82801I (ICH9 Family) USB UHCI Controller #5 00:1a.2 USB Controller: Intel \
Corporation 82801I (ICH9 Family) USB UHCI Controller #6 00:1a.7 USB Controller: Intel \
Corporation 82801I (ICH9 Family) USB2 EHCI Controller #2 00:1b.0 Audio device: Intel \
Corporation 82801I (ICH9 Family) HD Audio Controller 00:1c.0 PCI bridge: Intel \
Corporation 82801I (ICH9 Family) PCI Express Port 1 00:1c.1 PCI bridge: Intel \
Corporation 82801I (ICH9 Family) PCI Express Port 2 00:1c.3 PCI bridge: Intel \
Corporation 82801I (ICH9 Family) PCI Express Port 4 00:1c.4 PCI bridge: Intel \
Corporation 82801I (ICH9 Family) PCI Express Port 5 00:1d.0 USB Controller: Intel \
Corporation 82801I (ICH9 Family) USB UHCI Controller #1 00:1d.1 USB Controller: Intel \
Corporation 82801I (ICH9 Family) USB UHCI Controller #2 00:1d.2 USB Controller: Intel \
Corporation 82801I (ICH9 Family) USB UHCI Controller #3 00:1d.7 USB Controller: Intel \
Corporation 82801I (ICH9 Family) USB2 EHCI Controller #1 00:1e.0 PCI bridge: Intel \
Corporation 82801 Mobile PCI Bridge 00:1f.0 ISA bridge: Intel Corporation ICH9M LPC \
Interface Controller 00:1f.2 SATA controller: Intel Corporation ICH9M/M-E SATA AHCI \
Controller 00:1f.3 SMBus: Intel Corporation 82801I (ICH9 Family) SMBus Controller
03:00.0 Network controller: Intel Corporation Wireless WiFi Link 5300
15:00.0 CardBus bridge: Ricoh Co Ltd RL5c476 II
15:00.1 FireWire (IEEE 1394): Ricoh Co Ltd R5C832 IEEE 1394 Controller
15:00.2 SD Host controller: Ricoh Co Ltd R5C822 SD/SDIO/MMC/MS/MSPro Host Adapter
15:00.3 System peripheral: Ricoh Co Ltd R5C843 MMC Host Controller
15:00.4 System peripheral: Ricoh Co Ltd R5C592 Memory Stick Bus Host Adapter
15:00.5 System peripheral: Ricoh Co Ltd xD-Picture Card Controller
--------------------- snap -------------------

instead of 

--------------------- snip -------------------
sven@home:/home/user $ lspci
00:00.0 Class 0600: 8086:2a40
00:02.0 Class 0300: 8086:2a42
00:02.1 Class 0380: 8086:2a43
00:03.0 Class 0780: 8086:2a44
00:19.0 Class 0200: 8086:10bf
00:1a.0 Class 0c03: 8086:2937
00:1a.1 Class 0c03: 8086:2938
00:1a.2 Class 0c03: 8086:2939
00:1a.7 Class 0c03: 8086:293c
00:1b.0 Class 0403: 8086:293e
00:1c.0 Class 0604: 8086:2940
00:1c.1 Class 0604: 8086:2942
00:1c.3 Class 0604: 8086:2946
00:1c.4 Class 0604: 8086:2948
00:1d.0 Class 0c03: 8086:2934
00:1d.1 Class 0c03: 8086:2935
00:1d.2 Class 0c03: 8086:2936
00:1d.7 Class 0c03: 8086:293a
00:1e.0 Class 0604: 8086:2448
00:1f.0 Class 0601: 8086:2919
00:1f.2 Class 0106: 8086:2929
00:1f.3 Class 0c05: 8086:2930
03:00.0 Class 0280: 8086:4236
15:00.0 Class 0607: 1180:0476
15:00.1 Class 0c00: 1180:0832
15:00.2 Class 0805: 1180:0822
15:00.3 Class 0880: 1180:0843
15:00.4 Class 0880: 1180:0592
15:00.5 Class 0880: 1180:0852
--------------------- snap -------------------

If they aren't already installed on your system, you need to add the PCI and 
USB ID Repository:

wget -O /usr/share/hwdata/usb.ids http://www.linux-usb.org/usb.ids
wget -O /usr/share/hwdata/pci.ids http://pciids.sourceforge.net/v2.2/pci.ids

I welcome comments on that. TIA.

Good luck,
Sven


["busybox-1.16.0-human_readable_lspci_lsusb.patch" (text/x-diff)]

diff -Nruw busybox-1.16.0/util-linux/Config.in \
                busybox-1.16.0.sdi/util-linux/Config.in
--- busybox-1.16.0/util-linux/Config.in	2010-04-20 11:15:03.000000000 +0200
+++ busybox-1.16.0.sdi/util-linux/Config.in	2010-04-20 10:41:11.000000000 +0200
@@ -366,6 +366,18 @@
 
 	  This version uses sysfs (/sys/bus/usb/devices) only.
 
+config LSIDS
+	bool "lsids"
+	default n
+	depends on LSPCI
+	depends on LSUSB
+	help
+	  use human readable form when displaying information about PCI and
+	  USB busses in the system and devices connected to them.
+
+	  http://pciids.sourceforge.net/
+	  http://www.linux-usb.org/usb-ids.html
+
 config MDEV
 	bool "mdev"
 	default n
diff -Nruw busybox-1.16.0/util-linux/Kbuild busybox-1.16.0.sdi/util-linux/Kbuild
--- busybox-1.16.0/util-linux/Kbuild	2010-04-20 11:15:03.000000000 +0200
+++ busybox-1.16.0.sdi/util-linux/Kbuild	2010-04-20 10:40:19.000000000 +0200
@@ -23,6 +23,7 @@
 lib-$(CONFIG_LOSETUP)           += losetup.o
 lib-$(CONFIG_LSPCI)             += lspci.o
 lib-$(CONFIG_LSUSB)             += lsusb.o
+lib-$(CONFIG_LSIDS)             += sprintid.o
 lib-$(CONFIG_MDEV)              += mdev.o
 lib-$(CONFIG_MKFS_EXT2)         += mkfs_ext2.o
 lib-$(CONFIG_MKFS_MINIX)        += mkfs_minix.o
diff -Nruw busybox-1.16.0/util-linux/lspci.c busybox-1.16.0.sdi/util-linux/lspci.c
--- busybox-1.16.0/util-linux/lspci.c	2010-04-20 11:15:03.000000000 +0200
+++ busybox-1.16.0.sdi/util-linux/lspci.c	2010-04-20 12:28:34.000000000 +0200
@@ -13,6 +13,8 @@
 	OPT_k = (1 << 1),
 };
 
+#include "sprintid.h"
+
 /*
  * PCI_SLOT_NAME PCI_CLASS: PCI_VID:PCI_DID [PCI_SUBSYS_VID:PCI_SUBSYS_DID] [DRIVER]
  */
@@ -27,6 +29,7 @@
 	char *pci_slot_name = NULL, *driver = NULL;
 	int pci_class = 0, pci_vid = 0, pci_did = 0;
 	int pci_subsys_vid = 0, pci_subsys_did = 0;
+	char *pci_human_readable;
 
 	char *uevent_filename = concat_path_file(fileName, "/uevent");
 	parser = config_open2(uevent_filename, fopen_for_read);
@@ -68,9 +71,16 @@
 		       pci_slot_name, pci_class, pci_vid, pci_did,
 		       pci_subsys_vid, pci_subsys_did);
 	} else {
+		pci_human_readable=sprintid(pci_class, pci_vid, pci_did);
+		if(pci_human_readable!=NULL) {
+			printf("%s %s",pci_slot_name, pci_human_readable);
+		}
+		else
+		{	
 		printf("%s Class %04x: %04x:%04x",
 		       pci_slot_name, pci_class, pci_vid, pci_did);
 	}
+	}
 
 	if ((option_mask32 & OPT_k) && driver) {
 		if (option_mask32 & OPT_m) {
@@ -92,12 +102,13 @@
 {
 	getopt32(argv, "m" /*non-compat:*/ "k" /*ignored:*/ "nv");
 
+	sprintid_init("/usr/share/hwdata/pci.ids");
 	recursive_action("/sys/bus/pci/devices",
 			ACTION_RECURSE,
 			fileAction,
 			NULL, /* dirAction */
 			NULL, /* userData */
 			0 /* depth */);
-
+	sprintid_cleanup();
 	return EXIT_SUCCESS;
 }
diff -Nruw busybox-1.16.0/util-linux/lsusb.c busybox-1.16.0.sdi/util-linux/lsusb.c
--- busybox-1.16.0/util-linux/lsusb.c	2010-01-25 01:59:39.000000000 +0100
+++ busybox-1.16.0.sdi/util-linux/lsusb.c	2010-04-20 12:53:27.000000000 +0200
@@ -1,12 +1,13 @@
 /* vi: set sw=4 ts=4: */
 /*
-* lspci implementation for busybox
+* lsusb implementation for busybox
 *
 * Copyright (C) 2009  Malek Degachi <malek-degachi@laposte.net>
 *
 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
 */
 #include <libbb.h>
+#include "sprintid.h"
 
 static int FAST_FUNC fileAction(
 		const char *fileName,
@@ -18,6 +19,7 @@
 	char *tokens[6];
 	char *bus = NULL, *device = NULL;
 	int product_vid = 0, product_did = 0;
+	char *usb_human_readable;
 
 	char *uevent_filename = concat_path_file(fileName, "/uevent");
 	parser = config_open2(uevent_filename, fopen_for_read);
@@ -43,7 +45,12 @@
 	config_close(parser);
 
 	if (bus) {
+		usb_human_readable=sprintid(-1, product_vid, product_did);
+		if(usb_human_readable!=NULL) {
+			printf("Bus %s Device %s: ID %04x:%04x %s %s\n",bus, device, product_vid, \
product_did, usb_human_readable); +		} else {       
 		printf("Bus %s Device %s: ID %04x:%04x\n", bus, device, product_vid, product_did);
+		}
 		free(bus);
 		free(device);
 	}
@@ -55,13 +62,13 @@
 int lsusb_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 {
 	/* no options, no getopt */
-
+        sprintid_init("/usr/share/hwdata/usb.ids");
 	recursive_action("/sys/bus/usb/devices",
 			ACTION_RECURSE,
 			fileAction,
 			NULL, /* dirAction */
 			NULL, /* userData */
 			0 /* depth */);
-
+        sprintid_cleanup();            
 	return EXIT_SUCCESS;
 }
diff -Nruw busybox-1.16.0/util-linux/sprintid.c \
                busybox-1.16.0.sdi/util-linux/sprintid.c
--- busybox-1.16.0/util-linux/sprintid.c	1970-01-01 01:00:00.000000000 +0100
+++ busybox-1.16.0.sdi/util-linux/sprintid.c	2010-04-20 12:28:34.000000000 +0200
@@ -0,0 +1,110 @@
+#include "sprintid.h"
+
+static FILE *sprintid_db;
+static char *sprintid_line;
+
+#define SPRINTID_NAMELEN 128
+
+inline int sprintid_init(char *dbname) {
+	sprintid_db=fopen(dbname,"r");
+	sprintid_line=malloc(3*SPRINTID_NAMELEN);
+	if(sprintid_db==NULL || sprintid_line==NULL)
+		return(1);
+	else
+		return(0);
+}
+
+inline int sprintid_cleanup(void) {
+	if(sprintid_db!=NULL) {
+		fclose(sprintid_db);
+	}
+	if(sprintid_line!=NULL) {
+		free(sprintid_line);
+	}
+	return(0);
+}
+
+char *sprintid(int cidnum, int vidnum, int didnum) {
+	char line[3*SPRINTID_NAMELEN+1];
+	char vendor[SPRINTID_NAMELEN+1];
+	char device[SPRINTID_NAMELEN+1];
+	char class[SPRINTID_NAMELEN+1];
+
+	char cid[5];
+	char vid[5];
+	char did[5];
+
+	int havedevice=0;
+
+	if(sprintid_line==NULL) {
+		return(NULL);
+	}
+
+	sprintf(vid,"%04x",vidnum);
+	sprintf(did,"%04x",didnum);
+
+	if(cidnum>0)
+	{
+		sprintf(cid,"%04x",cidnum);
+		sprintf(sprintid_line,"Class %s: %s:%s",cid,vid,did);
+	} else {
+		sprintf(cid,"");
+		sprintf(sprintid_line,"%s:%s",vid,did);
+	}
+
+	if(sprintid_db==NULL) {
+		return(sprintid_line);
+	}
+
+	if(cidnum>0) {
+		sprintf(class,"Class unknown class %s: ",cid);
+	}
+	sprintf(vendor,"unknown vendor %s",vid);
+	sprintf(device,"unknown device %s",did);
+
+	fseek(sprintid_db,0L,SEEK_SET);
+
+	while(fgets(line, 3*SPRINTID_NAMELEN, sprintid_db)!=NULL) { 
+		if(line[0]!='#' && line[0]!='\r' && line[0]!='\n')
+		{
+	    		if((havedevice==0) && ((line[0]>='0' && line[0]<='9') || (line[0]>='a' && \
line[0]<='f'))) { // Vendor +				if(strncmp(line,vid,4)==0) {
+					strncpy(vendor,&line[6],SPRINTID_NAMELEN);
+					while(fgets(line, 3*SPRINTID_NAMELEN, sprintid_db)!=NULL && !((line[0]>='0' && \
line[0]<='9') || (line[0]>='a' && line[0]<='f'))) {  +						if(line[0]=='\t' && \
((line[1]>='0' && line[1]<='9') || (line[1]>='a' && line[1]<='f'))) { // Device \
+							if(strncmp(&line[1],did,4)==0) { \
+								strncpy(device,&line[7],SPRINTID_NAMELEN); +								break;
+							}
+						}
+					}
+				}
+			}
+
+			if(cidnum>0 && line[0]=='C') { // Classes
+				if(strncmp(&line[2],cid,2)==0) { // found Major 
+					while(fgets(line, 3*SPRINTID_NAMELEN, sprintid_db)!=NULL && !(line[0]=='C')) {
+						if(line[0]=='\t' && ((line[1]>='0' && line[1]<='9') || (line[1]>='a' && \
line[1]<='f'))) { // subclass +							if(strncmp(&line[1],&cid[2],2)==0) {
+								strncpy(class,&line[5],SPRINTID_NAMELEN);
+								break;
+							}
+						}
+					}
+					break;
+				}
+			}
+		}
+	}
+	chomp(class);
+	chomp(vendor);
+	chomp(device);
+	if(cidnum>0)
+	{
+		sprintf(sprintid_line,"Class %s: %s:%s",class,vendor,device);
+	} else {
+		sprintf(sprintid_line,"%s:%s",vendor,device);
+	}
+	return(sprintid_line); 
+}
+
diff -Nruw busybox-1.16.0/util-linux/sprintid.h \
                busybox-1.16.0.sdi/util-linux/sprintid.h
--- busybox-1.16.0/util-linux/sprintid.h	1970-01-01 01:00:00.000000000 +0100
+++ busybox-1.16.0.sdi/util-linux/sprintid.h	2010-04-20 11:35:40.000000000 +0200
@@ -0,0 +1,13 @@
+#include <stdio.h>
+#include <string.h>
+#include <libbb.h>
+
+#if CONFIG_LSIDS
+extern char *sprintid(int cidnum, int vidnum, int didnum);
+extern int sprintid_init(char *dbname);
+extern int sprintid_cleanup(void);
+#else
+static inline char *sprintid(int cidnum, int vidnum, int didnum) {}
+static inline int sprintid_init(char *dbname) {}
+static inline int sprintid_cleanup() {}
+#endif



_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic