summaryrefslogtreecommitdiffstats
path: root/pcilib
diff options
context:
space:
mode:
authorSuren A. Chilingaryan <csa@suren.me>2015-09-10 05:08:04 +0200
committerSuren A. Chilingaryan <csa@suren.me>2015-09-10 05:08:04 +0200
commitfcc0da28faca832a5d10572ae62ffa0e25436b19 (patch)
tree2869158673db4028dfb89ef85461719ef2bcffd9 /pcilib
parent4535ac71e075946f374aef0bb1e2997869f148fe (diff)
parentd996fab54c59ca0b34d4ff7c4ab5ab8247559db0 (diff)
downloadpcitool-fcc0da28faca832a5d10572ae62ffa0e25436b19.tar.gz
pcitool-fcc0da28faca832a5d10572ae62ffa0e25436b19.tar.bz2
pcitool-fcc0da28faca832a5d10572ae62ffa0e25436b19.tar.xz
pcitool-fcc0da28faca832a5d10572ae62ffa0e25436b19.zip
Initial integration of XML support
Diffstat (limited to 'pcilib')
-rw-r--r--pcilib/CMakeLists.txt9
-rw-r--r--pcilib/bank.c75
-rw-r--r--pcilib/bank.h14
-rw-r--r--pcilib/error.h3
-rw-r--r--pcilib/pci.c35
-rw-r--r--pcilib/pci.h6
-rw-r--r--pcilib/pcilib.h1
-rw-r--r--pcilib/register.c11
-rw-r--r--pcilib/register.h6
-rw-r--r--pcilib/xml.c656
-rw-r--r--pcilib/xml.h66
11 files changed, 835 insertions, 47 deletions
diff --git a/pcilib/CMakeLists.txt b/pcilib/CMakeLists.txt
index e21f1ba..8b11d60 100644
--- a/pcilib/CMakeLists.txt
+++ b/pcilib/CMakeLists.txt
@@ -3,11 +3,12 @@ include_directories(
${CMAKE_BINARY_DIR}
${CMAKE_SOURCE_DIR}/pcilib
${CMAKE_BINARY_DIR}/pcilib
+ ${LIBXML2_INCLUDE_DIRS}
)
-set(HEADERS pcilib.h pci.h export.h bar.h fifo.h model.h bank.h register.h kmem.h irq.h locking.h lock.h dma.h event.h plugin.h tools.h error.h debug.h env.h version.h config.h)
-add_library(pcilib SHARED pci.c export.c bar.c fifo.c model.c bank.c register.c kmem.c irq.c locking.c lock.c dma.c event.c plugin.c tools.c error.c debug.c env.c )
-target_link_libraries(pcilib dma protocols ${CMAKE_THREAD_LIBS_INIT} ${UFODECODE_LIBRARIES} ${CMAKE_DL_LIBS} ${EXTRA_SYSTEM_LIBS})
+set(HEADERS pcilib.h pci.h export.h bar.h fifo.h model.h bank.h register.h xml.h kmem.h irq.h locking.h lock.h dma.h event.h plugin.h tools.h error.h debug.h env.h version.h config.h)
+add_library(pcilib SHARED pci.c export.c bar.c fifo.c model.c bank.c register.c xml.c kmem.c irq.c locking.c lock.c dma.c event.c plugin.c tools.c error.c debug.c env.c )
+target_link_libraries(pcilib dma protocols ${CMAKE_THREAD_LIBS_INIT} ${UFODECODE_LIBRARIES} ${CMAKE_DL_LIBS} ${EXTRA_SYSTEM_LIBS} ${LIBXML2_LIBRARIES})
add_dependencies(pcilib dma protocols)
install(TARGETS pcilib
@@ -18,6 +19,6 @@ install(FILES pcilib.h
DESTINATION include
)
-install(FILES bar.h kmem.h locking.h lock.h bank.h register.h dma.h event.h model.h error.h debug.h env.h tools.h export.h version.h
+install(FILES bar.h kmem.h locking.h lock.h bank.h register.h xml.h dma.h event.h model.h error.h debug.h env.h tools.h export.h version.h
DESTINATION include/pcilib
)
diff --git a/pcilib/bank.c b/pcilib/bank.c
index 3d53db2..66ff739 100644
--- a/pcilib/bank.c
+++ b/pcilib/bank.c
@@ -51,6 +51,7 @@ int pcilib_init_register_banks(pcilib_t *ctx) {
bank_ctx->bank = ctx->banks + ctx->num_banks_init;
bank_ctx->api = bapi;
+ bank_ctx->xml = ctx->xml.bank_nodes[ctx->num_banks_init];
ctx->bank_ctx[ctx->num_banks_init] = bank_ctx;
}
@@ -76,9 +77,11 @@ void pcilib_free_register_banks(pcilib_t *ctx) {
ctx->num_banks_init = 0;
}
-
-int pcilib_add_register_banks(pcilib_t *ctx, size_t n, const pcilib_register_bank_description_t *banks) {
- // DS: Override existing banks
+int pcilib_add_register_banks(pcilib_t *ctx, pcilib_model_modification_flags_t flags, size_t n, const pcilib_register_bank_description_t *banks, pcilib_register_bank_t *ids) {
+ size_t i;
+ pcilib_register_bank_t bank;
+ size_t dyn_banks = ctx->dyn_banks;
+ size_t num_banks = ctx->num_banks;
if (!n) {
for (n = 0; banks[n].access; n++);
@@ -86,9 +89,42 @@ int pcilib_add_register_banks(pcilib_t *ctx, size_t n, const pcilib_register_ban
if ((ctx->num_banks + n + 1) > PCILIB_MAX_REGISTER_BANKS)
return PCILIB_ERROR_TOOBIG;
-
+
+/*
memcpy(ctx->banks + ctx->num_banks, banks, n * sizeof(pcilib_register_bank_description_t));
ctx->num_banks += n;
+*/
+
+ for (i = 0; i < n; i++) {
+ // Try to find if the bank is already existing...
+ bank = pcilib_find_register_bank_by_name(ctx, banks[i].name);
+ if ((bank == PCILIB_REGISTER_BANK_INVALID)&&(banks[i].addr != PCILIB_REGISTER_BANK_DYNAMIC))
+ bank = pcilib_find_register_bank_by_addr(ctx, banks[i].addr);
+
+ if (bank == PCILIB_REGISTER_BANK_INVALID) {
+ bank = num_banks++;
+ } else if (flags&PCILIB_MODEL_MODIFICATION_FLAG_SKIP_EXISTING) {
+ if (ids) ids[i] = bank;
+ continue;
+ } else if ((flags&PCILIB_MODEL_MODIFICATION_FLAG_OVERRIDE) == 0) {
+ if (pcilib_find_register_bank_by_name(ctx, banks[i].name) == PCILIB_REGISTER_BANK_INVALID)
+ pcilib_error("The bank %s is already existing and override flag is not set", banks[i].name);
+ else
+ pcilib_error("The bank with address 0x%lx is already existing and override flag is not set", banks[i].addr);
+ return PCILIB_ERROR_EXIST;
+ }
+
+ memcpy(ctx->banks + bank, banks + i, sizeof(pcilib_register_bank_description_t));
+ if (ids) ids[i] = bank;
+
+ if (banks[i].addr == PCILIB_REGISTER_BANK_DYNAMIC) {
+ ctx->banks[bank].addr = PCILIB_REGISTER_BANK_DYNAMIC + dyn_banks + 1;
+ dyn_banks++;
+ }
+ }
+
+ ctx->num_banks = num_banks;
+ ctx->dyn_banks = dyn_banks;
// If banks are already initialized, we need to re-run the initialization code
// DS: Locking is currently missing
@@ -100,7 +136,7 @@ int pcilib_add_register_banks(pcilib_t *ctx, size_t n, const pcilib_register_ban
return 0;
}
-int pcilib_add_register_protocols(pcilib_t *ctx, size_t n, const pcilib_register_protocol_description_t *protocols) {
+int pcilib_add_register_protocols(pcilib_t *ctx, pcilib_model_modification_flags_t flags, size_t n, const pcilib_register_protocol_description_t *protocols, pcilib_register_protocol_t *ids) {
// DS: Override existing banks
if (!n) {
@@ -116,10 +152,7 @@ int pcilib_add_register_protocols(pcilib_t *ctx, size_t n, const pcilib_register
return 0;
}
-
-int pcilib_add_register_ranges(pcilib_t *ctx, size_t n, const pcilib_register_range_t *ranges) {
- // DS: Override existing banks
-
+int pcilib_add_register_ranges(pcilib_t *ctx, pcilib_model_modification_flags_t flags, size_t n, const pcilib_register_range_t *ranges) {
if (!n) {
for (n = 0; ranges[n].end; n++);
}
@@ -135,22 +168,18 @@ int pcilib_add_register_ranges(pcilib_t *ctx, size_t n, const pcilib_register_ra
pcilib_register_bank_t pcilib_find_register_bank_by_addr(pcilib_t *ctx, pcilib_register_bank_addr_t bank) {
pcilib_register_bank_t i;
- const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
- const pcilib_register_bank_description_t *banks = model_info->banks;
- for (i = 0; banks[i].access; i++)
- if (banks[i].addr == bank) return i;
+ for (i = 0; ctx->banks[i].access; i++)
+ if (ctx->banks[i].addr == bank) return i;
return PCILIB_REGISTER_BANK_INVALID;
}
pcilib_register_bank_t pcilib_find_register_bank_by_name(pcilib_t *ctx, const char *bankname) {
pcilib_register_bank_t i;
- const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
- const pcilib_register_bank_description_t *banks = model_info->banks;
- for (i = 0; banks[i].access; i++)
- if (!strcasecmp(banks[i].name, bankname)) return i;
+ for (i = 0; ctx->banks[i].access; i++)
+ if (!strcasecmp(ctx->banks[i].name, bankname)) return i;
return PCILIB_REGISTER_BANK_INVALID;
}
@@ -203,22 +232,18 @@ pcilib_register_t pcilib_find_register(pcilib_t *ctx, const char *bank, const ch
pcilib_register_protocol_t pcilib_find_register_protocol_by_addr(pcilib_t *ctx, pcilib_register_protocol_addr_t protocol) {
pcilib_register_protocol_t i;
- const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
- const pcilib_register_protocol_description_t *protocols = model_info->protocols;
- for (i = 0; protocols[i].api; i++)
- if (protocols[i].addr == protocol) return i;
+ for (i = 0; ctx->protocols[i].api; i++)
+ if (ctx->protocols[i].addr == protocol) return i;
return PCILIB_REGISTER_PROTOCOL_INVALID;
}
pcilib_register_protocol_t pcilib_find_register_protocol_by_name(pcilib_t *ctx, const char *name) {
pcilib_register_protocol_t i;
- const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
- const pcilib_register_protocol_description_t *protocols = model_info->protocols;
- for (i = 0; protocols[i].api; i++)
- if (!strcasecmp(protocols[i].name, name)) return i;
+ for (i = 0; ctx->protocols[i].api; i++)
+ if (!strcasecmp(ctx->protocols[i].name, name)) return i;
return PCILIB_REGISTER_PROTOCOL_INVALID;
}
diff --git a/pcilib/bank.h b/pcilib/bank.h
index a0ba9fe..602fa67 100644
--- a/pcilib/bank.h
+++ b/pcilib/bank.h
@@ -23,9 +23,14 @@ typedef uint8_t pcilib_register_bank_addr_t; /**< Type holding the bank addr
typedef uint8_t pcilib_register_protocol_t; /**< Type holding the protocol position within the field listing register protocols in the model */
typedef uint8_t pcilib_register_protocol_addr_t; /**< Type holding the protocol address */
-
typedef struct pcilib_register_bank_context_s pcilib_register_bank_context_t;
+typedef enum {
+ PCILIB_MODEL_MODIFICATON_FLAGS_DEFAULT = 0,
+ PCILIB_MODEL_MODIFICATION_FLAG_OVERRIDE = 1, /**< Instructs to override the existing registers/banks/etc... */
+ PCILIB_MODEL_MODIFICATION_FLAG_SKIP_EXISTING = 2 /**< If flag is set, pcilib will just skip existing registers/banks/etc instead of reporting a error */
+} pcilib_model_modification_flags_t;
+
typedef struct {
pcilib_version_t version;
@@ -84,6 +89,7 @@ typedef struct {
struct pcilib_register_bank_context_s {
const pcilib_register_bank_description_t *bank; /**< Corresponding bank description */
const pcilib_register_protocol_api_description_t *api; /**< API functions */
+ pcilib_xml_node_t *xml; /**< Additional XML properties */
};
#ifdef __cplusplus
@@ -94,9 +100,9 @@ extern "C" {
int pcilib_init_register_banks(pcilib_t *ctx);
void pcilib_free_register_banks(pcilib_t *ctx);
-int pcilib_add_register_banks(pcilib_t *ctx, size_t n, const pcilib_register_bank_description_t *banks);
-int pcilib_add_register_protocols(pcilib_t *ctx, size_t n, const pcilib_register_protocol_description_t *protocols);
-int pcilib_add_register_ranges(pcilib_t *ctx, size_t n, const pcilib_register_range_t *ranges);
+int pcilib_add_register_banks(pcilib_t *ctx, pcilib_model_modification_flags_t flags, size_t n, const pcilib_register_bank_description_t *banks, pcilib_register_bank_t *ids);
+int pcilib_add_register_protocols(pcilib_t *ctx, pcilib_model_modification_flags_t flags, size_t n, const pcilib_register_protocol_description_t *protocols, pcilib_register_protocol_t *ids);
+int pcilib_add_register_ranges(pcilib_t *ctx, pcilib_model_modification_flags_t flags, size_t n, const pcilib_register_range_t *ranges);
pcilib_register_bank_t pcilib_find_register_bank_by_addr(pcilib_t *ctx, pcilib_register_bank_addr_t bank);
pcilib_register_bank_t pcilib_find_register_bank_by_name(pcilib_t *ctx, const char *bankname);
diff --git a/pcilib/error.h b/pcilib/error.h
index 9b5492b..4ac0967 100644
--- a/pcilib/error.h
+++ b/pcilib/error.h
@@ -28,7 +28,8 @@ enum {
PCILIB_ERROR_NOTINITIALIZED = EBADFD,
PCILIB_ERROR_TOOBIG = EFBIG,
PCILIB_ERROR_OVERWRITTEN = ESTALE,
- PCILIB_ERROR_BUSY = EBUSY
+ PCILIB_ERROR_BUSY = EBUSY,
+ PCILIB_ERROR_EXIST = EEXIST
} pcilib_errot_t;
#ifdef __cplusplus
diff --git a/pcilib/pci.c b/pcilib/pci.c
index ca17931..b7dcbcd 100644
--- a/pcilib/pci.c
+++ b/pcilib/pci.c
@@ -26,6 +26,7 @@
#include "model.h"
#include "plugin.h"
#include "bar.h"
+#include "xml.h"
#include "locking.h"
static int pcilib_detect_model(pcilib_t *ctx, const char *model) {
@@ -59,10 +60,10 @@ static int pcilib_detect_model(pcilib_t *ctx, const char *model) {
if (dma) {
if (dma->banks)
- pcilib_add_register_banks(ctx, 0, dma->banks);
+ pcilib_add_register_banks(ctx, PCILIB_MODEL_MODIFICATON_FLAGS_DEFAULT, 0, dma->banks, NULL);
if (dma->registers)
- pcilib_add_registers(ctx, 0, dma->registers);
+ pcilib_add_registers(ctx, PCILIB_MODEL_MODIFICATON_FLAGS_DEFAULT, 0, dma->registers, NULL);
if (dma->engines) {
for (j = 0; dma->engines[j].addr_bits; j++);
@@ -73,16 +74,16 @@ static int pcilib_detect_model(pcilib_t *ctx, const char *model) {
}
if (model_info->protocols)
- pcilib_add_register_protocols(ctx, 0, model_info->protocols);
+ pcilib_add_register_protocols(ctx, PCILIB_MODEL_MODIFICATON_FLAGS_DEFAULT, 0, model_info->protocols, NULL);
if (model_info->banks)
- pcilib_add_register_banks(ctx, 0, model_info->banks);
+ pcilib_add_register_banks(ctx, PCILIB_MODEL_MODIFICATON_FLAGS_DEFAULT, 0, model_info->banks, NULL);
if (model_info->registers)
- pcilib_add_registers(ctx, 0, model_info->registers);
+ pcilib_add_registers(ctx, PCILIB_MODEL_MODIFICATON_FLAGS_DEFAULT, 0, model_info->registers, NULL);
if (model_info->ranges)
- pcilib_add_register_ranges(ctx, 0, model_info->ranges);
+ pcilib_add_register_ranges(ctx, PCILIB_MODEL_MODIFICATON_FLAGS_DEFAULT, 0, model_info->ranges);
}
// Load XML registers
@@ -105,10 +106,10 @@ static int pcilib_detect_model(pcilib_t *ctx, const char *model) {
pcilib_t *pcilib_open(const char *device, const char *model) {
- int err;
+ int err, xmlerr;
size_t i;
pcilib_t *ctx = malloc(sizeof(pcilib_t));
-
+
if (!model)
model = getenv("PCILIB_MODEL");
@@ -159,7 +160,7 @@ pcilib_t *pcilib_open(const char *device, const char *model) {
ctx->num_protocols = i;
err = pcilib_detect_model(ctx, model);
- if (err) {
+ if ((err)&&(err != PCILIB_ERROR_NOTFOUND)) {
const pcilib_board_info_t *board_info = pcilib_get_board_info(ctx);
if (board_info)
pcilib_error("Error (%i) configuring model %s (%x:%x)", err, (model?model:""), board_info->vendor_id, board_info->device_id);
@@ -171,7 +172,21 @@ pcilib_t *pcilib_open(const char *device, const char *model) {
if (!ctx->model)
ctx->model = strdup(model?model:"pci");
+
+ xmlerr = pcilib_init_xml(ctx, ctx->model);
+ if ((xmlerr)&&(xmlerr != PCILIB_ERROR_NOTFOUND)) {
+ pcilib_error("Error (%i) initializing XML subsystem for model %s", xmlerr, ctx->model);
+ pcilib_close(ctx);
+ return NULL;
+ }
+ // We have found neither standard model nor XML
+ if ((err)&&(xmlerr)) {
+ pcilib_error("The specified model (%s) is not available", model);
+ pcilib_close(ctx);
+ return NULL;
+ }
+
ctx->model_info.registers = ctx->registers;
ctx->model_info.banks = ctx->banks;
ctx->model_info.protocols = ctx->protocols;
@@ -354,6 +369,8 @@ void pcilib_close(pcilib_t *ctx) {
}
pcilib_free_register_banks(ctx);
+
+ pcilib_free_xml(ctx);
if (ctx->register_ctx)
free(ctx->register_ctx);
diff --git a/pcilib/pci.h b/pcilib/pci.h
index 340abd3..00528e1 100644
--- a/pcilib/pci.h
+++ b/pcilib/pci.h
@@ -25,6 +25,7 @@
#include "model.h"
#include "export.h"
#include "locking.h"
+#include "xml.h"
typedef struct {
uint8_t max_link_speed, link_speed;
@@ -59,7 +60,9 @@ struct pcilib_s {
size_t num_banks_init; /**< Number of initialized banks */
size_t num_reg, alloc_reg; /**< Number of registered and allocated registers */
size_t num_banks, num_protocols, num_ranges; /**< Number of registered banks, protocols, and register ranges */
- size_t num_engines; /**> Number of configured DMA engines */
+ size_t num_engines; /**< Number of configured DMA engines */
+ size_t dyn_banks; /**< Number of configured dynamic banks */
+
pcilib_register_description_t *registers; /**< List of currently defined registers (from all sources) */
pcilib_register_bank_description_t banks[PCILIB_MAX_REGISTER_BANKS + 1]; /**< List of currently defined register banks (from all sources) */
pcilib_register_range_t ranges[PCILIB_MAX_REGISTER_RANGES + 1]; /**< List of currently defined register ranges (from all sources) */
@@ -76,6 +79,7 @@ struct pcilib_s {
pcilib_lock_t *dma_wlock[PCILIB_MAX_DMA_ENGINES]; /**< Per-engine locks to serialize write operations */
struct pcilib_locking_s locks; /**< Context of locking subsystem */
+ struct pcilib_xml_s xml; /**< XML context */
#ifdef PCILIB_FILE_IO
int file_io_handle;
diff --git a/pcilib/pcilib.h b/pcilib/pcilib.h
index 01f3324..6bb018d 100644
--- a/pcilib/pcilib.h
+++ b/pcilib/pcilib.h
@@ -23,6 +23,7 @@ typedef uint32_t pcilib_event_t;
typedef uint64_t pcilib_timeout_t; /**< In microseconds */
typedef unsigned int pcilib_irq_hw_source_t;
typedef uint32_t pcilib_irq_source_t;
+typedef struct _xmlNode pcilib_xml_node_t;
typedef enum {
PCILIB_LOG_DEBUG = 0, /**< Debug messages will be always printed as they should be filtered based on setting of corresponding environmental variable */
diff --git a/pcilib/register.c b/pcilib/register.c
index 347bf7c..68e5d36 100644
--- a/pcilib/register.c
+++ b/pcilib/register.c
@@ -19,7 +19,7 @@
#include "error.h"
-int pcilib_add_registers(pcilib_t *ctx, size_t n, const pcilib_register_description_t *registers) {
+int pcilib_add_registers(pcilib_t *ctx, pcilib_model_modification_flags_t flags, size_t n, const pcilib_register_description_t *registers, pcilib_register_t *ids) {
// DS: Overrride existing registers
// Registers identified by addr + offset + size + type or name
@@ -52,7 +52,16 @@ int pcilib_add_registers(pcilib_t *ctx, size_t n, const pcilib_register_descript
memcpy(ctx->registers + ctx->num_reg, registers, n * sizeof(pcilib_register_description_t));
memset(ctx->registers + ctx->num_reg + n, 0, sizeof(pcilib_register_description_t));
+
+ if (ids) {
+ size_t i;
+
+ for (i = 0; i < n; i++)
+ ids[i] = ctx->num_reg + i;
+ }
+
ctx->num_reg += n;
+
return 0;
}
diff --git a/pcilib/register.h b/pcilib/register.h
index 535e9d0..e7cbae9 100644
--- a/pcilib/register.h
+++ b/pcilib/register.h
@@ -44,7 +44,9 @@ typedef struct {
typedef struct {
- pcilib_register_bank_t bank;
+ pcilib_register_bank_t bank; /**< Reference to bank containing the register */
+ pcilib_register_value_t min, max; /**< Minimum & maximum allowed values */
+ pcilib_xml_node_t *xml; /**< Additional XML properties */
} pcilib_register_context_t;
@@ -52,7 +54,7 @@ typedef struct {
extern "C" {
#endif
-int pcilib_add_registers(pcilib_t *ctx, size_t n, const pcilib_register_description_t *registers);
+int pcilib_add_registers(pcilib_t *ctx, pcilib_model_modification_flags_t flags, size_t n, const pcilib_register_description_t *registers, pcilib_register_t *ids);
#ifdef __cplusplus
}
diff --git a/pcilib/xml.c b/pcilib/xml.c
new file mode 100644
index 0000000..73a3deb
--- /dev/null
+++ b/pcilib/xml.c
@@ -0,0 +1,656 @@
+/**
+ * @file pcilib_xml.c
+ * @version 1.0
+ *
+ * @brief this file is the main source file for the implementation of dynamic registers using xml and several funtionalities for the "pci-tool" line command tool from XML files. the xml part has been implemented using libxml2
+ *
+ * @details registers and banks nodes are parsed using xpath expression, but their properties is get by recursively getting all properties nodes. In this sense, if a property changes, the algorithm has to be changed, but if it's registers or banks nodes, just the xpath expression modification should be enough.
+
+ Libxml2 considers blank spaces within the XML as node natively and this code as been produced considering blank spaces in the XML files. In case XML files would not contain blank spaces anymore, please change the code.
+
+ In case the performance is not good enough, please consider the following : hard code of formulas
+ */
+#define _XOPEN_SOURCE 700
+#define _BSD_SOURCE
+#define _DEFAULT_SOURCE
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <dirent.h>
+#include <errno.h>
+#include <alloca.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+
+#include <libxml/xmlschemastypes.h>
+#include <libxml/tree.h>
+#include <libxml/parser.h>
+#include <libxml/xpath.h>
+#include <libxml/xpathInternals.h>
+
+#include "config.h"
+
+#include "pci.h"
+#include "bank.h"
+#include "register.h"
+#include "xml.h"
+#include "error.h"
+
+#define BANKS_PATH ((xmlChar*)"/model/banks/bank/bank_description") /**< path to complete nodes of banks.*/
+//#define REGISTERS_PATH ((xmlChar*)"/model/banks/bank/registers/register") /**< all standard registers nodes.*/
+//#define BIT_REGISTERS_PATH ((xmlChar*)"/model/banks/bank/registers/register/registers_bits/register_bits") /**< all bits registers nodes.*/
+#define REGISTERS_PATH ((xmlChar*)"../registers/register") /**< all standard registers nodes.*/
+#define BIT_REGISTERS_PATH ((xmlChar*)"./registers_bits/register_bits") /**< all bits registers nodes.*/
+
+static char *pcilib_xml_bank_default_format = "0x%lx";
+
+typedef struct {
+ pcilib_register_description_t base;
+ pcilib_register_value_t min, max;
+} pcilib_xml_register_description_t;
+
+/*
+static xmlNodePtr pcilib_xml_get_parent_bank_node(xmlDocPtr doc, xmlNodePtr node) {
+ xmlNodePtr bank_node;
+ bank_node = node->parent->parent;
+ // bank_description is always a first node according to the XSD schema
+ return xmlFirstElementChild(bank_node);
+
+}
+
+static xmlNodePtr pcilib_xml_get_parent_register_node(xmlDocPtr doc, xmlNodePtr node) {
+ return node->parent->parent;
+}
+*/
+
+static int pcilib_xml_parse_register(pcilib_t *ctx, pcilib_xml_register_description_t *xml_desc, xmlDocPtr doc, xmlNodePtr node, pcilib_register_bank_description_t *bdesc) {
+ pcilib_register_description_t *desc = (pcilib_register_description_t*)xml_desc;
+
+ xmlNodePtr cur;
+ char *value, *name;
+ char *endptr;
+
+ for (cur = node->children; cur != NULL; cur = cur->next) {
+ if (!cur->children) continue;
+ if (!xmlNodeIsText(cur->children)) continue;
+
+ name = (char*)cur->name;
+ value = (char*)cur->children->content;
+ if (!value) continue;
+
+ if (!strcasecmp((char*)name, "address")) {
+ uintptr_t addr = strtol(value, &endptr, 0);
+ if ((strlen(endptr) > 0)) {
+ pcilib_error("Invalid address (%s) is specified in the XML register description", value);
+ return PCILIB_ERROR_INVALID_DATA;
+ }
+ desc->addr = addr;
+ } else if(!strcasecmp(name, "offset")) {
+ int offset = strtol(value, &endptr, 0);
+ if ((strlen(endptr) > 0)||(offset < 0)||(offset > (8 * sizeof(pcilib_register_value_t)))) {
+ pcilib_error("Invalid offset (%s) is specified in the XML register description", value);
+ return PCILIB_ERROR_INVALID_DATA;
+ }
+ desc->offset = (pcilib_register_size_t)offset;
+ } else if (!strcasecmp(name,"size")) {
+ int size = strtol(value, &endptr, 0);
+ if ((strlen(endptr) > 0)||(size <= 0)||(size > (8 * sizeof(pcilib_register_value_t)))) {
+ pcilib_error("Invalid size (%s) is specified in the XML register description", value);
+ return PCILIB_ERROR_INVALID_DATA;
+ }
+ desc->bits = (pcilib_register_size_t)size;
+ } else if (!strcasecmp(name, "default")) {
+ pcilib_register_value_t val = strtol(value, &endptr, 0);
+ if ((strlen(endptr) > 0)) {
+ pcilib_error("Invalid default value (%s) is specified in the XML register description", value);
+ return PCILIB_ERROR_INVALID_DATA;
+ }
+ desc->defvalue = val;
+ } else if (!strcasecmp(name,"min")) {
+ pcilib_register_value_t min = strtol(value, &endptr, 0);
+ if ((strlen(endptr) > 0)) {
+ pcilib_error("Invalid minimum value (%s) is specified in the XML register description", value);
+ return PCILIB_ERROR_INVALID_DATA;
+ }
+ xml_desc->min = min;
+ } else if (!strcasecmp(name, "max")) {
+ pcilib_register_value_t max = strtol(value, &endptr, 0);
+ if ((strlen(endptr) > 0)) {
+ pcilib_error("Invalid minimum value (%s) is specified in the XML register description", value);
+ return PCILIB_ERROR_INVALID_DATA;
+ }
+ xml_desc->max = max;
+ } else if (!strcasecmp((char*)name,"rwmask")) {
+ if (!strcasecmp(value, "all")) {
+ desc->rwmask = PCILIB_REGISTER_ALL_BITS;
+ } else if (!strcasecmp(value, "none")) {
+ desc->rwmask = 0;
+ } else {
+ uintptr_t mask = strtol(value, &endptr, 0);
+ if ((strlen(endptr) > 0)) {
+ pcilib_error("Invalid mask (%s) is specified in the XML register description", value);
+ return PCILIB_ERROR_INVALID_DATA;
+ }
+ desc->rwmask = mask;
+ }
+ } else if (!strcasecmp(name, "mode")) {
+ if (!strcasecmp(value, "R")) {
+ desc->mode = PCILIB_REGISTER_R;
+ } else if (!strcasecmp(value, "W")) {
+ desc->mode = PCILIB_REGISTER_W;
+ } else if (!strcasecmp(value, "RW")) {
+ desc->mode = PCILIB_REGISTER_RW;
+ } else if (!strcasecmp(value, "W")) {
+ desc->mode = PCILIB_REGISTER_W;
+ } else if (!strcasecmp(value, "RW1C")) {
+ desc->mode = PCILIB_REGISTER_RW1C;
+ } else if (!strcasecmp(value, "W1C")) {
+ desc->mode = PCILIB_REGISTER_W1C;
+ } else {
+ pcilib_error("Invalid access mode (%s) is specified in the XML register description", value);
+ return PCILIB_ERROR_INVALID_DATA;
+ }
+ } else if (!strcasecmp(name, "type")) {
+ if (!strcasecmp(value, "fifo")) {
+ desc->type = PCILIB_REGISTER_FIFO;
+ } else {
+ pcilib_error("Invalid register type (%s) is specified in the XML register description", value);
+ return PCILIB_ERROR_INVALID_DATA;
+ }
+ } else if (!strcasecmp(name,"name")) {
+ desc->name = value;
+ } else if (!strcasecmp(name,"description")) {
+ desc->description = value;
+ }
+ }
+
+ return 0;
+}
+
+static int pcilib_xml_create_register(pcilib_t *ctx, pcilib_register_bank_t bank, xmlXPathContextPtr xpath, xmlDocPtr doc, xmlNodePtr node) {
+ int err;
+
+ xmlXPathObjectPtr nodes;
+ xmlNodeSetPtr nodeset;
+
+ pcilib_xml_register_description_t desc = {{0}};
+ pcilib_xml_register_description_t fdesc;
+
+ pcilib_register_t reg;
+
+ desc.base.bank = ctx->banks[bank].addr;
+ desc.base.rwmask = PCILIB_REGISTER_ALL_BITS;
+ desc.base.mode = PCILIB_REGISTER_R;
+ desc.base.type = PCILIB_REGISTER_STANDARD;
+
+ err = pcilib_xml_parse_register(ctx, &desc, doc, node, &ctx->banks[bank]);
+ if (err) {
+ pcilib_error("Error (%i) parsing an XML register", err);
+ return err;
+ }
+
+ err = pcilib_add_registers(ctx, PCILIB_MODEL_MODIFICATION_FLAG_OVERRIDE, 1, &desc.base, &reg);
+ if (err) {
+ pcilib_error("Error (%i) adding a new XML register (%s) to the model", err, desc.base.name);
+ return err;
+ }
+
+ ctx->register_ctx[reg].xml = node;
+ ctx->register_ctx[reg].min = desc.min;
+ ctx->register_ctx[reg].max = desc.max;
+
+ xpath->node = node;
+ nodes = xmlXPathEvalExpression(BIT_REGISTERS_PATH, xpath);
+ xpath->node = NULL;
+
+ if (!nodes) {
+ xmlErrorPtr xmlerr = xmlGetLastError();
+
+ if (xmlerr) pcilib_error("Failed to parse XPath expression %s, xmlXPathEvalExpression reported error %d - %s", BIT_REGISTERS_PATH, xmlerr->code, xmlerr->message);
+ else pcilib_error("Failed to parse XPath expression %s", BIT_REGISTERS_PATH);
+ return PCILIB_ERROR_FAILED;
+ }
+
+ nodeset = nodes->nodesetval;
+ if (!xmlXPathNodeSetIsEmpty(nodeset)) {
+ int i;
+
+ for (i = 0; i < nodeset->nodeNr; i++) {
+ memset(&fdesc, 0, sizeof(pcilib_xml_register_description_t));
+
+ fdesc.base.bank = desc.base.bank;
+ fdesc.base.addr = desc.base.addr;
+ fdesc.base.mode = desc.base.mode;
+ fdesc.base.rwmask = desc.base.rwmask;
+ fdesc.base.type = PCILIB_REGISTER_BITS;
+
+ err = pcilib_xml_parse_register(ctx, &fdesc, doc, nodeset->nodeTab[i], &ctx->banks[bank]);
+ if (err) {
+ pcilib_error("Error parsing field in the XML register %s", desc.base.name);
+ continue;
+ }
+
+ err = pcilib_add_registers(ctx, PCILIB_MODEL_MODIFICATION_FLAG_OVERRIDE, 1, &fdesc.base, &reg);
+ if (err) {
+ pcilib_error("Error (%i) adding a new XML register (%s) to the model", err, fdesc.base.name);
+ continue;
+ }
+
+ ctx->register_ctx[reg].xml = nodeset->nodeTab[i];
+ ctx->register_ctx[reg].min = fdesc.min;
+ ctx->register_ctx[reg].max = fdesc.max;
+ }
+ }
+ xmlXPathFreeObject(nodes);
+
+ return 0;
+}
+
+static int pcilib_xml_create_bank(pcilib_t *ctx, xmlXPathContextPtr xpath, xmlDocPtr doc, xmlNodePtr node) {
+ int err;
+
+ int override = 0;
+ pcilib_register_bank_description_t desc = {0};
+ pcilib_register_bank_t bank;
+ xmlNodePtr cur;
+ char *value, *name;
+ char *endptr;
+
+ xmlXPathObjectPtr nodes;
+ xmlNodeSetPtr nodeset;
+
+
+ desc.format = pcilib_xml_bank_default_format;
+ desc.addr = PCILIB_REGISTER_BANK_DYNAMIC;
+ desc.bar = PCILIB_BAR_NOBAR;
+ desc.size = 0x1000;
+ desc.protocol = PCILIB_REGISTER_PROTOCOL_DEFAULT;
+ desc.access = 32;
+ desc.endianess = PCILIB_HOST_ENDIAN;
+ desc.raw_endianess = PCILIB_HOST_ENDIAN;
+
+ // iterate through all children, representing bank properties, to fill the structure
+ for (cur = node->children; cur != NULL; cur = cur->next) {
+ if (!cur->children) continue;
+ if (!xmlNodeIsText(cur->children)) continue;
+
+ name = (char*)cur->name;
+ value = (char*)cur->children->content;
+ if (!value) continue;
+
+ if (!strcasecmp(name, "bar")) {
+ char bar = value[0]-'0';
+ if ((strlen(value) != 1)||(bar < 0)||(bar > 5)) {
+ pcilib_error("Invalid BAR (%s) is specified in the XML bank description", value);
+ return PCILIB_ERROR_INVALID_DATA;
+ }
+ desc.bar = (pcilib_bar_t)bar;
+ override = 1;
+ } else if (!strcasecmp(name,"size")) {
+ long size = strtol(value, &endptr, 0);
+ if ((strlen(endptr) > 0)||(size<=0)) {
+ pcilib_error("Invalid bank size (%s) is specified in the XML bank description", value);
+ return PCILIB_ERROR_INVALID_DATA;
+ }
+ desc.size = (size_t)size;
+ override = 1;
+ } else if (!strcasecmp(name,"protocol")) {
+ pcilib_register_protocol_t protocol = pcilib_find_register_protocol_by_name(ctx, value);
+ if (protocol == PCILIB_REGISTER_PROTOCOL_INVALID) {
+ pcilib_error("Unsupported protocol (%s) is specified in the XML bank description", value);
+ return PCILIB_ERROR_NOTSUPPORTED;
+ }
+ desc.protocol = ctx->protocols[protocol].addr;
+ override = 1;
+ } else if (!strcasecmp(name,"address")) {
+ uintptr_t addr = strtol(value, &endptr, 0);
+ if ((strlen(endptr) > 0)) {
+ pcilib_error("Invalid address (%s) is specified in the XML bank description", value);
+ return PCILIB_ERROR_INVALID_DATA;
+ }
+ desc.read_addr = addr;
+ desc.write_addr = addr;
+ override = 1;
+ } else if (!strcasecmp(name,"read_address")) {
+ uintptr_t addr = strtol(value, &endptr, 0);
+ if ((strlen(endptr) > 0)) {
+ pcilib_error("Invalid address (%s) is specified in the XML bank description", value);
+ return PCILIB_ERROR_INVALID_DATA;
+ }
+ desc.read_addr = addr;
+ override = 1;
+ } else if (!strcasecmp(name,"write_address")) {
+ uintptr_t addr = strtol(value, &endptr, 0);
+ if ((strlen(endptr) > 0)) {
+ pcilib_error("Invalid address (%s) is specified in the XML bank description", value);
+ return PCILIB_ERROR_INVALID_DATA;
+ }
+ desc.write_addr = addr;
+ override = 1;
+ } else if(strcasecmp((char*)name,"word_size")==0) {
+ int access = strtol(value, &endptr, 0);
+ if ((strlen(endptr) > 0)||(access%8)||(access<=0)||(access>(8 * sizeof(pcilib_register_value_t)))) {
+ pcilib_error("Invalid word size (%s) is specified in the XML bank description", value);
+ return PCILIB_ERROR_INVALID_DATA;
+ }
+ desc.access = access;
+ override = 1;
+ } else if (!strcasecmp(name,"endianess")) {
+ if (!strcasecmp(value,"little")) desc.endianess = PCILIB_LITTLE_ENDIAN;
+ else if (!strcasecmp(value,"big")) desc.endianess = PCILIB_BIG_ENDIAN;
+ else if (!strcasecmp(value,"host")) desc.endianess = PCILIB_HOST_ENDIAN;
+ else {
+ pcilib_error("Invalid endianess (%s) is specified in the XML bank description", value);
+ return PCILIB_ERROR_INVALID_DATA;
+ }
+ override = 1;
+ } else if (!strcasecmp(name,"format")) {
+ desc.format = value;
+ override = 1;
+ } else if (!strcasecmp((char*)name,"name")) {
+ desc.name = value;
+ } else if (!strcasecmp((char*)name,"description")) {
+ desc.description = value;
+ override = 1;
+ } else if (!strcasecmp((char*)name,"override")) {
+ override = 1;
+ }
+ }
+
+ err = pcilib_add_register_banks(ctx, override?PCILIB_MODEL_MODIFICATION_FLAG_OVERRIDE:PCILIB_MODEL_MODIFICATION_FLAG_SKIP_EXISTING, 1, &desc, &bank);
+ if (err) {
+ pcilib_error("Error adding register bank (%s) specified in the XML bank description", desc.name);
+ return err;
+ }
+
+ ctx->xml.bank_nodes[bank] = node;
+ if (ctx->bank_ctx[bank]) {
+ ctx->bank_ctx[bank]->xml = node;
+ }
+
+ xpath->node = node;
+ nodes = xmlXPathEvalExpression(REGISTERS_PATH, xpath);
+ xpath->node = NULL;
+
+ if (!nodes) {
+ xmlErrorPtr xmlerr = xmlGetLastError();
+ if (xmlerr) pcilib_error("Failed to parse XPath expression %s, xmlXPathEvalExpression reported error %d - %s", REGISTERS_PATH, xmlerr->code, xmlerr->message);
+ else pcilib_error("Failed to parse XPath expression %s", REGISTERS_PATH);
+ return PCILIB_ERROR_FAILED;
+ }
+
+ nodeset = nodes->nodesetval;
+ if (!xmlXPathNodeSetIsEmpty(nodeset)) {
+ int i;
+ for (i = 0; i < nodeset->nodeNr; i++) {
+ err = pcilib_xml_create_register(ctx, bank, xpath, doc, nodeset->nodeTab[i]);
+ if (err) pcilib_error("Error creating XML registers for bank %s", desc.name);
+ }
+ }
+ xmlXPathFreeObject(nodes);
+
+ return 0;
+}
+
+
+/** pcilib_xml_initialize_banks
+ *
+ * function to create the structures to store the banks from the AST
+ * @see pcilib_xml_create_bank
+ * @param[in] doc the AST of the xml file.
+ * @param[in] pci the pcilib_t running, which will be filled
+ */
+static int pcilib_xml_process_document(pcilib_t *ctx, xmlDocPtr doc, xmlXPathContextPtr xpath) {
+ xmlXPathObjectPtr bank_nodes;
+ xmlNodeSetPtr nodeset;
+
+ bank_nodes = xmlXPathEvalExpression(BANKS_PATH, xpath);
+ if (!bank_nodes) {
+ xmlErrorPtr xmlerr = xmlGetLastError();
+ if (xmlerr) pcilib_error("Failed to parse XPath expression %s, xmlXPathEvalExpression reported error %d - %s", BANKS_PATH, xmlerr->code, xmlerr->message);
+ else pcilib_error("Failed to parse XPath expression %s", BANKS_PATH);
+ return PCILIB_ERROR_FAILED;
+ }
+
+
+ nodeset = bank_nodes->nodesetval;
+ if (!xmlXPathNodeSetIsEmpty(nodeset)) {
+ int i;
+ for (i = 0; i < nodeset->nodeNr; i++) {
+ pcilib_xml_create_bank(ctx, xpath, doc, nodeset->nodeTab[i]);
+ }
+ }
+ xmlXPathFreeObject(bank_nodes);
+
+ return 0;
+}
+
+static int pcilib_xml_load_xsd(pcilib_t *ctx, char *xsd_filename) {
+ int err;
+
+ xmlSchemaParserCtxtPtr ctxt;
+
+ /** we first parse the xsd file for AST with validation*/
+ ctxt = xmlSchemaNewParserCtxt(xsd_filename);
+ if (!ctxt) {
+ xmlErrorPtr xmlerr = xmlGetLastError();
+ if (xmlerr) pcilib_error("xmlSchemaNewParserCtxt reported error %d - %s", xmlerr->code, xmlerr->message);
+ else pcilib_error("Failed to create a parser for XML schemas");
+ return PCILIB_ERROR_FAILED;
+ }
+
+ ctx->xml.schema = xmlSchemaParse(ctxt);
+ if (!ctx->xml.schema) {
+ xmlErrorPtr xmlerr = xmlGetLastError();
+ xmlSchemaFreeParserCtxt(ctxt);
+ if (xmlerr) pcilib_error("Failed to parse XML schema, xmlSchemaParse reported error %d - %s", xmlerr->code, xmlerr->message);
+ else pcilib_error("Failed to parse XML schema");
+ return PCILIB_ERROR_INVALID_DATA;
+ }
+
+ xmlSchemaFreeParserCtxt(ctxt);
+
+ ctx->xml.validator = xmlSchemaNewValidCtxt(ctx->xml.schema);
+ if (!ctx->xml.validator) {
+ xmlErrorPtr xmlerr = xmlGetLastError();
+ if (xmlerr) pcilib_error("xmlSchemaNewValidCtxt reported error %d - %s", xmlerr->code, xmlerr->message);
+ else pcilib_error("Failed to create a validation context");
+ return PCILIB_ERROR_FAILED;
+ }
+
+ err = xmlSchemaSetValidOptions(ctx->xml.validator, XML_SCHEMA_VAL_VC_I_CREATE);
+ if (err) {
+ xmlErrorPtr xmlerr = xmlGetLastError();
+ if (xmlerr) pcilib_error("xmlSchemaSetValidOptions reported error %d - %s", xmlerr->code, xmlerr->message);
+ else pcilib_error("Failed to configure the validation context to populate default attributes");
+ return PCILIB_ERROR_FAILED;
+ }
+
+ return 0;
+}
+
+static int pcilib_xml_load_file(pcilib_t *ctx, const char *path, const char *name) {
+ int err;
+ char *full_name;
+ xmlDocPtr doc;
+ xmlXPathContextPtr xpath;
+
+ full_name = (char*)alloca(strlen(path) + strlen(name) + 2);
+ if (!name) {
+ pcilib_error("Error allocating %zu bytes of memory in stack to create a file name", strlen(path) + strlen(name) + 2);
+ return PCILIB_ERROR_MEMORY;
+ }
+
+ sprintf(full_name, "%s/%s", path, name);
+
+ doc = xmlCtxtReadFile(ctx->xml.parser, full_name, NULL, 0);
+ if (!doc) {
+ xmlErrorPtr xmlerr = xmlCtxtGetLastError(ctx->xml.parser);
+ if (xmlerr) pcilib_error("Error parsing %s, xmlCtxtReadFile reported error %d - %s", full_name, xmlerr->code, xmlerr->message);
+ else pcilib_error("Error parsing %s", full_name);
+ return PCILIB_ERROR_INVALID_DATA;
+ }
+
+ err = xmlSchemaValidateDoc(ctx->xml.validator, doc);
+ if (err) {
+ xmlErrorPtr xmlerr = xmlCtxtGetLastError(ctx->xml.parser);
+ xmlFreeDoc(doc);
+ if (xmlerr) pcilib_error("Error validating %s, xmlSchemaValidateDoc reported error %d - %s", full_name, xmlerr->code, xmlerr->message);
+ else pcilib_error("Error validating %s", full_name);
+ return PCILIB_ERROR_VERIFY;
+ }
+
+ xpath = xmlXPathNewContext(doc);
+ if (!xpath) {
+ xmlErrorPtr xmlerr = xmlGetLastError();
+ xmlFreeDoc(doc);
+ if (xmlerr) pcilib_error("Document %s: xmlXpathNewContext reported error %d - %s for document %s", full_name, xmlerr->code, xmlerr->message);
+ else pcilib_error("Error creating XPath context for %s", full_name);
+ return PCILIB_ERROR_FAILED;
+ }
+
+ // This can only partially fail... Therefore we need to keep XML and just return the error...
+ err = pcilib_xml_process_document(ctx, doc, xpath);
+
+ if (ctx->xml.num_files == PCILIB_MAX_MODEL_FILES) {
+ xmlFreeDoc(doc);
+ xmlXPathFreeContext(xpath);
+ pcilib_error("Too many XML files for a model, only up to %zu are supported", PCILIB_MAX_MODEL_FILES);
+ return PCILIB_ERROR_TOOBIG;
+ }
+
+ ctx->xml.docs[ctx->xml.num_files] = doc;
+ ctx->xml.xpath[ctx->xml.num_files] = xpath;
+ ctx->xml.num_files++;
+
+ return err;
+}
+
+
+int pcilib_process_xml(pcilib_t *ctx, const char *location) {
+ int err;
+
+ DIR *rep;
+ struct dirent *file = NULL;
+ char *model_dir, *model_path;
+
+ model_dir = getenv("PCILIB_MODEL_DIR");
+ if (!model_dir) model_dir = PCILIB_MODEL_DIR;
+
+ model_path = (char*)alloca(strlen(model_dir) + strlen(location) + 2);
+ if (!model_path) return PCILIB_ERROR_MEMORY;
+
+ sprintf(model_path, "%s/%s", model_dir, location);
+
+ rep = opendir(model_path);
+ if (!rep) return PCILIB_ERROR_NOTFOUND;
+
+ while ((file = readdir(rep)) != NULL) {
+ size_t len = strlen(file->d_name);
+ if ((len < 4)||(strcasecmp(file->d_name + len - 4, ".xml"))) continue;
+ if (file->d_type != DT_REG) continue;
+
+ err = pcilib_xml_load_file(ctx, model_path, file->d_name);
+ if (err) pcilib_error("Error processing XML file %s", file->d_name);
+ }
+
+ closedir(rep);
+
+ return 0;
+}
+
+
+/** pcilib_init_xml
+ * this function will initialize the registers and banks from the xml files
+ * @param[in,out] ctx the pciilib_t running that gets filled with structures
+ * @param[in] model the current model of ctx
+ * @return an error code
+ */
+int pcilib_init_xml(pcilib_t *ctx, const char *model) {
+ int err;
+
+ char *model_dir;
+ char *xsd_path;
+
+ struct stat st;
+
+ model_dir = getenv("PCILIB_MODEL_DIR");
+ if (!model_dir) model_dir = PCILIB_MODEL_DIR;
+
+ xsd_path = (char*)alloca(strlen(model_dir) + 16);
+ if (!xsd_path) return PCILIB_ERROR_MEMORY;
+
+ sprintf(xsd_path, "%s/model.xsd", model_dir);
+ if (stat(xsd_path, &st)) {
+ pcilib_info("XML models are not present");
+ return PCILIB_ERROR_NOTFOUND;
+ }
+
+ ctx->xml.parser = xmlNewParserCtxt();
+ if (!ctx->xml.parser) {
+ xmlErrorPtr xmlerr = xmlGetLastError();
+ if (xmlerr) pcilib_error("xmlNewParserCtxt reported error %d (%s)", xmlerr->code, xmlerr->message);
+ else pcilib_error("Failed to create an XML parser context");
+ return PCILIB_ERROR_FAILED;
+ }
+
+ err = pcilib_xml_load_xsd(ctx, xsd_path);
+ if (err) return err;
+
+ return pcilib_process_xml(ctx, model);
+}
+
+/** pcilib_free_xml
+ * this function free the xml parts of the pcilib_t running, and some libxml ashes
+ * @param[in] pci the pcilib_t running
+*/
+void pcilib_free_xml(pcilib_t *ctx) {
+ int i;
+
+ memset(ctx->xml.bank_nodes, 0, sizeof(ctx->xml.bank_nodes));
+ for (i = 0; i < ctx->num_banks; i++) {
+ if (ctx->bank_ctx[i])
+ ctx->bank_ctx[i]->xml = NULL;
+ }
+
+ for (i = 0; i < ctx->num_reg; i++) {
+ ctx->register_ctx[i].xml = NULL;
+ }
+
+ for (i = 0; i < ctx->xml.num_files; i++) {
+ if (ctx->xml.docs[i]) {
+ xmlFreeDoc(ctx->xml.docs[i]);
+ ctx->xml.docs[i] = NULL;
+ }
+ if (ctx->xml.xpath[i]) {
+ xmlXPathFreeContext(ctx->xml.xpath[i]);
+ ctx->xml.xpath[i] = NULL;
+ }
+ }
+ ctx->xml.num_files = 0;
+
+ if (ctx->xml.validator) {
+ xmlSchemaFreeValidCtxt(ctx->xml.validator);
+ ctx->xml.validator = NULL;
+ }
+
+ if (ctx->xml.schema) {
+ xmlSchemaFree(ctx->xml.schema);
+ ctx->xml.schema = NULL;
+
+ }
+
+ if (ctx->xml.parser) {
+ xmlFreeParserCtxt(ctx->xml.parser);
+ ctx->xml.parser = NULL;
+ }
+/*
+ xmlSchemaCleanupTypes();
+ xmlCleanupParser();
+ xmlMemoryDump();
+*/
+}
diff --git a/pcilib/xml.h b/pcilib/xml.h
new file mode 100644
index 0000000..6ad8676
--- /dev/null
+++ b/pcilib/xml.h
@@ -0,0 +1,66 @@
+/**
+ * @file xml.h
+ * @version 1.0
+ * @brief header file to support of xml configuration.
+ *
+ * @details this file is the header file for the implementation of dynamic registers using xml and several funtionalities for the "pci-tool" line command tool from XML files. the xml part has been implemented using libxml2.
+ *
+ * this code was meant to be evolutive as the XML files evolute. In this meaning, most of the xml parsing is realized with XPath expressions(when it was possible), so that changing the xml xould result mainly in changing the XPAth pathes present here.
+ * @todo cf compilation chain
+ */
+
+#ifndef _PCILIB_XML_H
+#define _PCILIB_XML_H
+
+#include <pcilib.h>
+#include <libxml/tree.h>
+#include <libxml/xpath.h>
+#include <libxml/xmlschemas.h>
+
+#define PCILIB_MAX_MODEL_FILES 32
+
+typedef struct pcilib_xml_s pcilib_xml_t;
+
+struct pcilib_xml_s {
+ size_t num_files; /**< Number of currently loaded XML documents */
+
+ xmlDocPtr docs[PCILIB_MAX_MODEL_FILES]; /**< Pointers to parsed XML documents */
+ xmlXPathContextPtr xpath[PCILIB_MAX_MODEL_FILES]; /**< Per-file XPath context */
+
+ xmlParserCtxtPtr parser; /**< Pointer to the XML parser context */
+ xmlSchemaPtr schema; /**< Pointer to the parsed xsd schema */
+ xmlSchemaValidCtxtPtr validator; /**< Pointer to the XML validation context */
+
+ xmlNodePtr bank_nodes[PCILIB_MAX_REGISTER_BANKS]; /**< pointer to xml nodes of banks in the xml file */
+};
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * this function gets the xml files and validates them, before filling the pcilib_t struct with the registers and banks of those files
+ *@param[in,out] ctx the pcilib_t struct running that gets filled with banks and registers
+ *@param[in] model the name of the model
+ */
+int pcilib_init_xml(pcilib_t *ctx, const char *model);
+
+/** pcilib_free_xml
+ * this function free the xml parts of the pcilib_t running, and some libxml ashes
+ * @param[in] ctx the pcilib_t running
+*/
+void pcilib_free_xml(pcilib_t *ctx);
+
+
+/** pcilib_process_xml
+ * this function free the xml parts of the pcilib_t running, and some libxml ashes
+ * @param[in] ctx the pcilib_t running
+ * @param[in] location of XML files relative to the PCILIB_MODEL_DIR
+*/
+int pcilib_process_xml(pcilib_t *ctx, const char *location);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /*_PCILIB_XML_H */