Tools: Use bsearch to look up vendor in pci-ids.
This commit is contained in:
parent
a9064a1048
commit
f91c6bc812
|
@ -30,6 +30,7 @@
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "pci-ids.h"
|
#include "pci-ids.h"
|
||||||
|
|
||||||
|
@ -46525,38 +46526,10 @@ static pci_vid_index_t const pci_vid_index[] = {
|
||||||
{0xFFFF, 1, pci_vid_FFFF },
|
{0xFFFF, 1, pci_vid_FFFF },
|
||||||
}; /* We have 2376 VIDs */
|
}; /* We have 2376 VIDs */
|
||||||
|
|
||||||
static pci_vid_index_t const *get_vid_index(uint16_t vid)
|
static int vid_search(const void *key, const void *tbl_entry)
|
||||||
{
|
{
|
||||||
uint32_t start_index = 0;
|
return (int)*(const uint16_t *)key -
|
||||||
uint32_t end_index = 0;
|
(int)((const pci_vid_index_t *)tbl_entry)->vid;
|
||||||
uint32_t idx = 0;
|
|
||||||
|
|
||||||
end_index = sizeof(pci_vid_index)/sizeof(pci_vid_index[0]);
|
|
||||||
|
|
||||||
while(start_index != end_index)
|
|
||||||
{
|
|
||||||
if(end_index - start_index == 1)
|
|
||||||
{
|
|
||||||
if(pci_vid_index[start_index].vid == vid)
|
|
||||||
return &pci_vid_index[start_index];
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
idx = (start_index + end_index)/2;
|
|
||||||
|
|
||||||
if(pci_vid_index[idx].vid < vid)
|
|
||||||
start_index = idx;
|
|
||||||
else
|
|
||||||
if(pci_vid_index[idx].vid > vid)
|
|
||||||
end_index = idx;
|
|
||||||
else
|
|
||||||
return &pci_vid_index[idx];
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *pci_id_str(uint16_t vid, uint16_t did, uint16_t svid, uint16_t ssid)
|
const char *pci_id_str(uint16_t vid, uint16_t did, uint16_t svid, uint16_t ssid)
|
||||||
|
@ -46566,7 +46539,7 @@ const char *pci_id_str(uint16_t vid, uint16_t did, uint16_t svid, uint16_t ssid)
|
||||||
pci_vid_index_t const *index_ptr;
|
pci_vid_index_t const *index_ptr;
|
||||||
pci_id_t const *ids_ptr;
|
pci_id_t const *ids_ptr;
|
||||||
|
|
||||||
index_ptr = get_vid_index(vid);
|
index_ptr = bsearch(&vid, pci_vid_index, sizeof pci_vid_index / sizeof pci_vid_index[0], sizeof pci_vid_index[0], vid_search);
|
||||||
|
|
||||||
if(index_ptr == NULL)
|
if(index_ptr == NULL)
|
||||||
return not_found;
|
return not_found;
|
||||||
|
|
|
@ -35,6 +35,7 @@ CODE_PREFIX = """\
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "pci-ids.h"
|
#include "pci-ids.h"
|
||||||
|
|
||||||
|
@ -59,38 +60,10 @@ typedef struct
|
||||||
"""
|
"""
|
||||||
|
|
||||||
CODE_POSTFIX = """
|
CODE_POSTFIX = """
|
||||||
static pci_vid_index_t const *get_vid_index(uint16_t vid)
|
static int vid_search(const void *key, const void *tbl_entry)
|
||||||
{
|
{
|
||||||
uint32_t start_index = 0;
|
return (int)*(const uint16_t *)key -
|
||||||
uint32_t end_index = 0;
|
(int)((const pci_vid_index_t *)tbl_entry)->vid;
|
||||||
uint32_t idx = 0;
|
|
||||||
|
|
||||||
end_index = sizeof(pci_vid_index)/sizeof(pci_vid_index[0]);
|
|
||||||
|
|
||||||
while(start_index != end_index)
|
|
||||||
{
|
|
||||||
if(end_index - start_index == 1)
|
|
||||||
{
|
|
||||||
if(pci_vid_index[start_index].vid == vid)
|
|
||||||
return &pci_vid_index[start_index];
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
idx = (start_index + end_index)/2;
|
|
||||||
|
|
||||||
if(pci_vid_index[idx].vid < vid)
|
|
||||||
start_index = idx;
|
|
||||||
else
|
|
||||||
if(pci_vid_index[idx].vid > vid)
|
|
||||||
end_index = idx;
|
|
||||||
else
|
|
||||||
return &pci_vid_index[idx];
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *pci_id_str(uint16_t vid, uint16_t did, uint16_t svid, uint16_t ssid)
|
const char *pci_id_str(uint16_t vid, uint16_t did, uint16_t svid, uint16_t ssid)
|
||||||
|
@ -100,7 +73,7 @@ const char *pci_id_str(uint16_t vid, uint16_t did, uint16_t svid, uint16_t ssid)
|
||||||
pci_vid_index_t const *index_ptr;
|
pci_vid_index_t const *index_ptr;
|
||||||
pci_id_t const *ids_ptr;
|
pci_id_t const *ids_ptr;
|
||||||
|
|
||||||
index_ptr = get_vid_index(vid);
|
index_ptr = bsearch(&vid, pci_vid_index, sizeof pci_vid_index / sizeof pci_vid_index[0], sizeof pci_vid_index[0], vid_search);
|
||||||
|
|
||||||
if(index_ptr == NULL)
|
if(index_ptr == NULL)
|
||||||
return not_found;
|
return not_found;
|
||||||
|
|
Loading…
Reference in New Issue