[haiku-commits] haiku: hrev43749 - src/apps/devices headers/private/shared src/bin

hrev43749 adds 1 changeset to branch 'master'
old head: c3f7a9bca5b6831977b3c883b1515ec840dd7393
new head: 4ea3e0d3b8edc49382c8612c7957503d7145c914

----------------------------------------------------------------------------

4ea3e0d: listusb: display USB vendor and devices names based on usb.ids
  
  * download usb.ids, processed like pci.ids to generate a header.
  * best would be to load and parse the file at runtime with shared code.

                                   [ Jérôme Duval <jerome.duval@xxxxxxxxx> ]

----------------------------------------------------------------------------

Revision:    hrev43749
Commit:      4ea3e0d3b8edc49382c8612c7957503d7145c914
URL:         http://cgit.haiku-os.org/haiku/commit/?id=4ea3e0d
Author:      Jérôme Duval <jerome.duval@xxxxxxxxx>
Date:        Sat Feb 18 13:25:10 2012 UTC

----------------------------------------------------------------------------

6 files changed, 154 insertions(+), 26 deletions(-)
headers/private/shared/usb-utils.h |   28 +++++-----
src/apps/devices/Jamfile           |   37 ++++++++++---
src/apps/devices/pci-header.awk    |    1 +
src/apps/devices/usb-header.awk    |   95 ++++++++++++++++++++++++++++++++
src/bin/Jamfile                    |    4 +-
src/bin/listusb.cpp                |   15 ++++-

----------------------------------------------------------------------------

diff --git a/headers/private/shared/usb-utils.h 
b/headers/private/shared/usb-utils.h
index fe6749a..4ce85ea 100644
--- a/headers/private/shared/usb-utils.h
+++ b/headers/private/shared/usb-utils.h
@@ -1,23 +1,25 @@
 /*
- * Copyright 2008, Haiku, Inc. All Rights Reserved.
+ * Copyright 2008-2012, Haiku, Inc. All Rights Reserved.
  * Distributed under the terms of the MIT License.
  *
  * Authors:
  *     Jérôme Duval
  */
 
+
 #include <stdio.h>
 
-#include "usbdevs.h"
-#include "usbdevs_data.h"
+#include "usbhdr.h"
+
 
 void
 usb_get_vendor_info(uint16 vendorID, const char **vendorName)
 {
        int i;
-       for (i = 0; usb_knowndevs[i].vendor != 0; i++) {
-               if (usb_knowndevs[i].vendor == vendorID) {
-                       *vendorName = usb_knowndevs[i].vendorname;
+       for (i = 0; i < (int)USB_VENTABLE_LEN; i++) {
+               if (UsbVenTable[i].VenId == vendorID) {
+                       *vendorName = UsbVenTable[i].VenName && 
UsbVenTable[i].VenName[0]
+                               ? UsbVenTable[i].VenName : NULL;
                        return;
                }
        }
@@ -26,18 +28,16 @@ usb_get_vendor_info(uint16 vendorID, const char 
**vendorName)
 
 
 void
-usb_get_vendor_device_info(uint16 vendorID, uint16 deviceID, const char 
**vendorName, const char **deviceName)
+usb_get_device_info(uint16 vendorID, uint16 deviceID, const char **deviceName)
 {
        int i;
-       for (i = 0; usb_knowndevs[i].vendor != 0; i++) {
-               if (usb_knowndevs[i].vendor == vendorID
-                       && usb_knowndevs[i].product == deviceID
-                       && usb_knowndevs[i].flags != USB_KNOWNDEV_NOPROD) {
-                       *deviceName = usb_knowndevs[i].productname;
-                       *vendorName = usb_knowndevs[i].vendorname;
+       // search for the device
+       for (i = 0; i < (int)USB_DEVTABLE_LEN; i++) {
+               if (UsbDevTable[i].VenId == vendorID && UsbDevTable[i].DevId == 
deviceID ) {
+                       *deviceName = UsbDevTable[i].ChipDesc && 
UsbDevTable[i].ChipDesc[0]
+                               ? UsbDevTable[i].ChipDesc : NULL;
                        return;
                }
        }
-       *vendorName = NULL;
        *deviceName = NULL;
 }
diff --git a/src/apps/devices/Jamfile b/src/apps/devices/Jamfile
index e69b600..89f4582 100644
--- a/src/apps/devices/Jamfile
+++ b/src/apps/devices/Jamfile
@@ -19,28 +19,30 @@ rule ISAPnPHeaderGen
 
 actions ISAPnPHeaderGen1
 {
-       grep '^PNP[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]] '  $(2[1]) | 
gawk -f $(2[2]) > $(1) ;
+       grep '^PNP[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]] '  $(2[1]) \
+| gawk -f $(2[2]) > $(1) ;
 }
 
 ISAPnPHeaderGen [ FGristFiles isapnpids.h ] : isapnp_devids.txt : 
devlist2h.awk ;
 
-rule USBHeaderGen
+rule USBDevsHeaderGen
 {
        SEARCH on $(2) = $(SEARCH_SOURCE) ;
        SEARCH on $(3) = $(SEARCH_SOURCE) ;
 
        Depends $(1) : $(2) $(3) ;
        MakeLocateArch $(<) ;
-       USBHeaderGen1 $(1) : $(2) $(3) ;
+       USBDevsHeaderGen1 $(1) : $(2) $(3) ;
        LocalClean clean : $(<) ;
 }
 
-actions USBHeaderGen1
+actions USBDevsHeaderGen1
 {
        gawk -v HEADERFILE=$(1[1]) -v DATAFILE=$(1[2]) -f $(2[2]) $(2[1])
 }
 
-USBHeaderGen [ FGristFiles usbdevs.h usbdevs_data.h ] : usbdevs : 
usb_devlist2h.awk ;
+USBDevsHeaderGen [ FGristFiles usbdevs.h usbdevs_data.h ] : usbdevs
+       : usb_devlist2h.awk ;
 
 rule PCIHeaderGen
 {
@@ -63,6 +65,27 @@ local pciidsFile = [ DownloadFile pci.ids
 
 PCIHeaderGen [ FGristFiles pcihdr.h ] : $(pciidsFile) : pci-header.awk ;
 
+rule USBHeaderGen
+{
+       SEARCH on $(2) = $(SEARCH_SOURCE) ;
+       SEARCH on $(3) = $(SEARCH_SOURCE) ;
+
+       Depends $(1) : $(2) $(3) ;
+       MakeLocateArch $(<) ;
+       USBHeaderGen1 $(1) : $(2) $(3) ;
+       LocalClean clean : $(<) ;
+}
+
+actions USBHeaderGen1
+{
+       gawk -v HEADERFILE=$(1) -f $(2[2]) $(2[1])
+}
+
+local usbidsFile = [ DownloadFile usb.ids
+                       : http://www.linux-usb.org/usb.ids ] ;
+
+USBHeaderGen [ FGristFiles usbhdr.h ] : $(usbidsFile) : usb-header.awk ;
+
 Application Devices :
        DevicesApplication.cpp
        DevicesView.cpp
@@ -91,7 +114,5 @@ DoCatalogs Devices :
        Device.cpp
 ;
 
-Includes [ FGristFiles DevicesInfo.cpp ] : [ FGristFiles isapnpids.h usbdevs.h
-       usbdevs_data.h ] ;
-Includes [ FGristFiles ConfigurationWindow.cpp ] : [ FGristFiles pcihdr.h ] ;
+Includes [ FGristFiles DevicePCI.cpp ] : [ FGristFiles pcihdr.h ] ;
 
diff --git a/src/apps/devices/pci-header.awk b/src/apps/devices/pci-header.awk
index 2ee6426..2d3fad9 100644
--- a/src/apps/devices/pci-header.awk
+++ b/src/apps/devices/pci-header.awk
@@ -56,6 +56,7 @@ BEGIN {
 /^\t[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]] / { 
 
        device = substr($0, 8)
+       gsub( /\\/, "\\\\", device )
        gsub( /\"/, "\\\"", device )
 
        # store device ID for possible devices afterwards
diff --git a/src/apps/devices/usb-header.awk b/src/apps/devices/usb-header.awk
new file mode 100644
index 0000000..1aef370
--- /dev/null
+++ b/src/apps/devices/usb-header.awk
@@ -0,0 +1,95 @@
+# USB Header script
+#
+# Copyright 2006-2012, Haiku.
+# Distributed under the terms of the MIT License.
+#
+# Authors:
+#              John Drinkwater, john@xxxxxxxxxxxxx
+#
+# Use with http://www.linux-usb.org/usb.ids
+# run as: awk -v HEADERFILE=usbhdr.h -f usb-header.awk usb.ids
+
+BEGIN {
+
+       # field separator, defining because user could have overridden
+       FS = " "
+
+       # Pass this in from outside with -v HEADERFILE=filenametouse
+       # we require usbhdr.h for our system
+       ofile = HEADERFILE
+
+       # possibly use this in the future
+       cleanvalues = "[^A-Za-z0-9{}\"'&@?!*.,:;+<> \\t\\/_\\[\\]=#()-]"
+       # ToDo: currently IDs aren't checked, we dumbly assume the source is 
clean
+
+       # descriptive output header
+       print "#if 0" > ofile
+       print "#\tUSBHDR.H: USB Vendors, Devices\n#" > ofile
+       print "#\tGenerated by usb-header.awk, source data from the following 
URI:\n#\thttp://www.linux-usb.org/usb.ids\n#"; > ofile
+       print "#\tHeader created on " strftime( "%A, %d %b %Y %H:%M:%S %Z", 
systime() ) > ofile
+       print "#endif" > ofile
+
+       # and we start with vendors..
+       print "\ntypedef struct _USB_VENTABLE\n{\n\tunsigned short\tVenId 
;\n\tconst char *\tVenName ;\n}  USB_VENTABLE, *PUSB_VENTABLE ;\n" > ofile
+       print "USB_VENTABLE\tUsbVenTable [] =\n{" > ofile
+}
+
+# matches vendor - starts with an id as first thing on the line
+# because this occurs first in the header file, we output it without worry
+/^[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]] / { 
+
+       if ( vendorcount++ > 0 ) { 
+               formatting = ",\n"
+       } else {
+               formatting = ""
+       }
+
+       # store vendor ID for possible devices afterwards
+       vendorid = $1
+       vendor = substr($0, 7)
+       gsub( /\"/, "\\\"", vendor )
+
+       printf formatting "\t{ 0x" vendorid ", \"" vendor "\" }" > ofile
+}
+
+# matches device 
+/^\t[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]] / { 
+
+       device = substr($0, 8)
+       gsub( /\\/, "\\\\", device )
+       gsub( /\"/, "\\\"", device )
+
+       # store device ID for possible devices afterwards
+       deviceid = $1
+       devicecount++
+       devices[devicecount, 1] = vendorid
+       devices[devicecount, 2] = $1
+       devices[devicecount, 3] = device 
+}
+
+# We've processed the file, now output.
+END {
+
+       print "\n};\n\n// Use this value for loop control during 
searching:\n#define\tUSB_VENTABLE_LEN\t(sizeof(UsbVenTable)/sizeof(USB_VENTABLE))\n"
 > ofile
+
+       if ( devicecount > 0 ) {
+
+               print "typedef struct _USB_DEVTABLE\n{\n\tunsigned short        
VenId ;\n\tunsigned short       DevId ;\n\tconst char *\tChipDesc ;\n}  
USB_DEVTABLE, *PUSB_DEVTABLE ;\n"  > ofile
+               print "USB_DEVTABLE\tUsbDevTable [] =\n{" > ofile
+               for (i = 1; i <= devicecount; i++) {
+
+                       if (i != 1) {
+                               formatting = ",\n"
+                       } else {
+                               formatting = ""
+                       }
+                       printf formatting "\t{ 0x" devices[i, 1] ", 0x" 
devices[i, 2] ", \"" devices[i, 3] "\" }" > ofile
+               }
+               print "\n} ;\n\n// Use this value for loop control during 
searching:\n#define   USB_DEVTABLE_LEN        
(sizeof(UsbDevTable)/sizeof(USB_DEVTABLE))\n" > ofile
+
+       }
+       
+       close(ofile)
+}
+
+
diff --git a/src/bin/Jamfile b/src/bin/Jamfile
index c8e033f..4558932 100644
--- a/src/bin/Jamfile
+++ b/src/bin/Jamfile
@@ -198,7 +198,9 @@ StdBinCommands
        : be libdevice.so : $(haiku-utils_rsrc) ;
 
 ObjectHdrs [ FGristFiles listusb$(SUFOBJ) ]
-       : [ FDirName $(SUBDIR) $(DOTDOT) add-ons kernel bus_managers usb ] ;
+       : [ FDirName $(SUBDIR) $(DOTDOT) add-ons kernel bus_managers usb ] 
+               [ FDirName $(TARGET_COMMON_DEBUG_OBJECT_DIR) apps devices ] ;
+Includes [ FGristFiles listusb.cpp ] : <src!apps!devices>usbhdr.h ;
 
 # standard commands that need libbluetooth.so, due the Bluetooth Kit
 StdBinCommands
diff --git a/src/bin/listusb.cpp b/src/bin/listusb.cpp
index f1e9782..b30f899 100644
--- a/src/bin/listusb.cpp
+++ b/src/bin/listusb.cpp
@@ -14,6 +14,7 @@
 #include <stdio.h>
 
 #include "usbspec_private.h"
+#include "usb-utils.h"
 
 
 const char*
@@ -152,6 +153,10 @@ DumpInfo(BUSBDevice &device, bool verbose)
                return;
        }
 
+       const char *vendorName = NULL, *deviceName = NULL;
+       usb_get_vendor_info(device.VendorID(), &vendorName);
+       usb_get_device_info(device.VendorID(), device.ProductID(), &deviceName);
+
        printf("[Device /dev/bus/usb%s]\n", device.Location());
        printf("    Class .................. 0x%02x (%s)\n", device.Class(),
                ClassName(device.Class()));
@@ -159,9 +164,13 @@ DumpInfo(BUSBDevice &device, bool verbose)
        printf("    Protocol ............... 0x%02x\n", device.Protocol());
        printf("    Max Endpoint 0 Packet .. %d\n", 
device.MaxEndpoint0PacketSize());
        printf("    USB Version ............ 0x%04x\n", device.USBVersion());
-       printf("    Vendor ID .............. 0x%04x\n", device.VendorID());
-       printf("    Product ID ............. 0x%04x\n", device.ProductID());
-       printf("    Product Version ........ 0x%04x\n", device.Version());
+       printf("    Vendor ID .............. 0x%04x", device.VendorID());
+       if (vendorName != NULL)
+               printf(" (%s)", vendorName);
+       printf("\n    Product ID ............. 0x%04x", device.ProductID());
+       if (deviceName != NULL)
+               printf(" (%s)", deviceName);
+       printf("\n    Product Version ........ 0x%04x\n", device.Version());
        printf("    Manufacturer String .... \"%s\"\n", 
device.ManufacturerString());
        printf("    Product String ......... \"%s\"\n", device.ProductString());
        printf("    Serial Number .......... \"%s\"\n", 
device.SerialNumberString());


Other related posts:

  • » [haiku-commits] haiku: hrev43749 - src/apps/devices headers/private/shared src/bin - korli