Adds interface and associated unittests for accessing/looking-up/validating
the new ni routing table information.

Signed-off-by: Spencer E. Olson <olso...@umich.edu>
---
 drivers/staging/comedi/Kconfig                     |   4 +
 drivers/staging/comedi/drivers/Makefile            |   1 +
 drivers/staging/comedi/drivers/ni_routes.c         | 321 +++++++++++
 drivers/staging/comedi/drivers/ni_routes.h         | 315 +++++++++++
 drivers/staging/comedi/drivers/ni_stc.h            |   4 +
 drivers/staging/comedi/drivers/tests/Makefile      |   3 +-
 .../staging/comedi/drivers/tests/ni_routes_test.c  | 596 +++++++++++++++++++++
 7 files changed, 1243 insertions(+), 1 deletion(-)
 create mode 100644 drivers/staging/comedi/drivers/ni_routes.c
 create mode 100644 drivers/staging/comedi/drivers/ni_routes.h
 create mode 100644 drivers/staging/comedi/drivers/tests/ni_routes_test.c

diff --git a/drivers/staging/comedi/Kconfig b/drivers/staging/comedi/Kconfig
index e7255f8..7d614f4 100644
--- a/drivers/staging/comedi/Kconfig
+++ b/drivers/staging/comedi/Kconfig
@@ -1105,6 +1105,7 @@ config COMEDI_NI_TIOCMD
        depends on HAS_DMA
        select COMEDI_NI_TIO
        select COMEDI_MITE
+       select COMEDI_NI_ROUTES
 
 endif # COMEDI_PCI_DRIVERS
 
@@ -1319,4 +1320,7 @@ config COMEDI_NI_LABPC_ISADMA
 config COMEDI_NI_TIO
        tristate
 
+config COMEDI_NI_ROUTES
+       tristate
+
 endif # COMEDI
diff --git a/drivers/staging/comedi/drivers/Makefile 
b/drivers/staging/comedi/drivers/Makefile
index ff4fb78..f374739 100644
--- a/drivers/staging/comedi/drivers/Makefile
+++ b/drivers/staging/comedi/drivers/Makefile
@@ -137,6 +137,7 @@ obj-$(CONFIG_COMEDI_VMK80XX)                += vmk80xx.o
 obj-$(CONFIG_COMEDI_MITE)              += mite.o
 obj-$(CONFIG_COMEDI_NI_TIO)            += ni_tio.o
 obj-$(CONFIG_COMEDI_NI_TIOCMD)         += ni_tiocmd.o
+obj-$(CONFIG_COMEDI_NI_ROUTES)         += ni_routes.o
 obj-$(CONFIG_COMEDI_NI_LABPC)          += ni_labpc_common.o
 obj-$(CONFIG_COMEDI_NI_LABPC_ISADMA)   += ni_labpc_isadma.o
 
diff --git a/drivers/staging/comedi/drivers/ni_routes.c 
b/drivers/staging/comedi/drivers/ni_routes.c
new file mode 100644
index 0000000..3f3c3db
--- /dev/null
+++ b/drivers/staging/comedi/drivers/ni_routes.c
@@ -0,0 +1,321 @@
+/* vim: set ts=8 sw=8 noet tw=80 nowrap: */
+/*
+ *  comedi/drivers/ni_routes.c
+ *  Route information for NI boards.
+ *
+ *  COMEDI - Linux Control and Measurement Device Interface
+ *  Copyright (C) 2016 Spencer E. Olson <olso...@umich.edu>
+ *
+ *  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; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  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.
+ */
+
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/bsearch.h>
+
+#include "../comedi.h"
+
+#include "ni_routes.h"
+#include "ni_routing/ni_route_values.c"
+#include "ni_routing/ni_device_routes.c"
+
+/*
+ * This is defined in ni_routing/ni_route_values.c:
+ * #define B(x)        ((x) - NI_NAMES_BASE)
+ */
+
+/*
+ * This is defined in ni_routing/ni_route_values.c to identify clearly elements
+ * of the table that were set.  In other words, entries that are zero are
+ * invalid.  To get the value to use for the register, one must mask out the
+ * high bit.
+ *
+ * #define V(x)        ((x) | 0x80)
+ */
+
+/* Mask out the valid-value marking bit. */
+#define UnV(x) ((x) & (~(0x80)))
+
+#define RVi(table, src, dest)  (table)[(src) * NI_NUM_NAMES + (dest)]
+
+static const size_t route_table_size = NI_NUM_NAMES * NI_NUM_NAMES;
+
+/*
+ * Find the proper route_values and ni_device_routes tables for this particular
+ * device.
+ *
+ * Return: -ENODATA if either was not found; 0 if both were found.
+ */
+static inline int ni_find_device_routes(const char *device_family,
+                                       const char *board_name,
+                                       struct ni_route_tables *tables)
+{
+       const struct ni_device_routes *dr = NULL;
+       const u8 *rv = NULL;
+       int i;
+
+       /* First, find the register_values table for this device family */
+       for (i = 0; i < ARRAY_SIZE(all_route_values); ++i) {
+               if (memcmp(all_route_values[i].family, device_family,
+                          strnlen(device_family, 30)) == 0) {
+                       rv = &all_route_values[i].register_values[0][0];
+                       break;
+               }
+       }
+
+       if (!rv)
+               return -ENODATA;
+
+       /* Second, find the set of routes valid for this device. */
+       for (i = 0; i < ARRAY_SIZE(device_routes_list); ++i) {
+               if (memcmp(device_routes_list[i].device, board_name,
+                          strnlen(board_name, 30)) == 0) {
+                       dr = &device_routes_list[i];
+                       break;
+               }
+       }
+
+       if (!dr)
+               return -ENODATA;
+
+       tables->route_values = rv;
+       tables->valid_routes = dr;
+
+       return 0;
+}
+
+/*
+ * Assign the proper routing tables to this particular device.
+ * Return: -ENODATA if assignment was not successful; 0 if successful.
+ */
+int ni_assign_device_routes(const char *device_family,
+                           const char *board_name,
+                           struct ni_route_tables *tables)
+{
+       memset(tables, 0, sizeof(struct ni_route_tables));
+       return ni_find_device_routes(device_family, board_name, tables);
+}
+EXPORT_SYMBOL_GPL(ni_assign_device_routes);
+
+inline unsigned int ni_count_valid_routes(const struct ni_route_tables *tables)
+{
+       int total = 0;
+       int i;
+
+       for (i = 0; i < tables->valid_routes->n_route_sets; ++i) {
+               const struct ni_route_set *R = &tables->valid_routes->routes[i];
+               int j;
+
+               for (j = 0; j < R->n_dest; ++j) {
+                       const int src  = R->src;
+                       const int dest = R->dest[j];
+                       const u8 *V = tables->route_values;
+
+                       if (RVi(V, B(src), B(dest)))
+                               /* direct routing is valid */
+                               ++total;
+                       else if (channel_is_rtsi(dest) &&
+                                (RVi(V, B(src), B(NI_RGOUT0)) ||
+                                 RVi(V, B(src), B(NI_RTSI_BRD(0))) ||
+                                 RVi(V, B(src), B(NI_RTSI_BRD(1))) ||
+                                 RVi(V, B(src), B(NI_RTSI_BRD(2))) ||
+                                 RVi(V, B(src), B(NI_RTSI_BRD(3))))) {
+                               ++total;
+                       }
+               }
+       }
+       return total;
+}
+EXPORT_SYMBOL_GPL(ni_count_valid_routes);
+
+unsigned int ni_get_valid_routes(const struct ni_route_tables *tables,
+                                struct comedi_route_pair **pptr)
+{
+       unsigned int n_valid = ni_count_valid_routes(tables);
+       int i;
+
+       if (!pptr || n_valid == 0)
+               return n_valid;
+
+       *pptr = kcalloc(n_valid, sizeof(struct comedi_route_pair), GFP_KERNEL);
+       n_valid = 0;
+
+       for (i = 0; i < tables->valid_routes->n_route_sets; ++i) {
+               const struct ni_route_set *R = &tables->valid_routes->routes[i];
+               int j;
+
+               for (j = 0; j < R->n_dest; ++j) {
+                       const int src  = R->src;
+                       const int dest = R->dest[j];
+                       bool valid = false;
+                       const u8 *V = tables->route_values;
+
+                       if (RVi(V, B(src), B(dest)))
+                               /* direct routing is valid */
+                               valid = true;
+                       else if (channel_is_rtsi(dest) &&
+                                (RVi(V, B(src), B(NI_RGOUT0)) ||
+                                 RVi(V, B(src), B(NI_RTSI_BRD(0))) ||
+                                 RVi(V, B(src), B(NI_RTSI_BRD(1))) ||
+                                 RVi(V, B(src), B(NI_RTSI_BRD(2))) ||
+                                 RVi(V, B(src), B(NI_RTSI_BRD(3))))) {
+                               valid = true;
+                       }
+
+                       if (valid)
+                               (*pptr)[n_valid++] =
+                                       (struct comedi_route_pair){src, dest};
+               }
+       }
+       return n_valid;
+}
+EXPORT_SYMBOL_GPL(ni_get_valid_routes);
+
+/* **** BEGIN Routes search routines **** */
+static inline int _ni_srccmp(const int *key,
+                            const struct ni_route_set *elt)
+{
+       if (*key < elt->src)
+               return -1;
+       else if (*key > elt->src)
+               return 1;
+       return 0;
+}
+
+static inline int _ni_destcmp(const int *key,
+                             const int *elt)
+{
+       if (*key < *elt)
+               return -1;
+       else if (*key > *elt)
+               return 1;
+       return 0;
+}
+
+inline const struct ni_route_set *
+ni_find_route_set(const int source,
+                 const struct ni_device_routes *valid_routes)
+{
+       return bsearch(&source, valid_routes->routes,
+                      valid_routes->n_route_sets, sizeof(struct ni_route_set),
+                      (int (*)(const void *, const void *))_ni_srccmp);
+}
+EXPORT_SYMBOL_GPL(ni_find_route_set);
+
+inline bool ni_route_set_has_destination(const struct ni_route_set *routes,
+                                        const int dest)
+{
+       if (!bsearch(&dest, routes->dest, routes->n_dest, sizeof(int),
+                    (int (*)(const void *, const void *))_ni_destcmp))
+               return false;
+       return true;
+}
+EXPORT_SYMBOL_GPL(ni_route_set_has_destination);
+
+s8 ni_lookup_route_register(int src, int dest,
+                           const struct ni_route_tables *tables)
+{
+       s8 regval;
+
+       /*
+        * Be sure to use the B() macro to subtract off the NI_NAMES_BASE before
+        * indexing into the route_values array.
+        */
+       src = B(src);
+       dest = B(dest);
+       if (src < 0 || src >= NI_NUM_NAMES || dest < 0 || dest >= NI_NUM_NAMES)
+               return -EINVAL;
+       regval = RVi(tables->route_values, src, dest);
+       if (!regval)
+               return -EINVAL;
+       /* mask out the valid-value marking bit */
+       return UnV(regval);
+}
+EXPORT_SYMBOL_GPL(ni_lookup_route_register);
+
+inline s8 ni_route_to_register(const int src, const int dest,
+                              const struct ni_route_tables *tables)
+{
+       const struct ni_route_set *routes =
+               ni_find_route_set(src, tables->valid_routes);
+       const u8 *V;
+       s8 regval;
+
+       /* first check to see if source is listed with bunch of destinations. */
+       if (!routes)
+               return -1;
+       /* 2nd, check to see if destination is in list of source's targets. */
+       if (!ni_route_set_has_destination(routes, dest))
+               return -1;
+       /*
+        * finally, check to see if we know how to route...
+        * Be sure to use the B() macro to subtract off the NI_NAMES_BASE before
+        * indexing into the route_values array.
+        */
+       V = tables->route_values;
+       regval = RVi(V, B(src), B(dest));
+
+       /*
+        * if we did not validate the route, we'll see if we can route through
+        * one of the muxes
+        */
+       if (!regval && channel_is_rtsi(dest)) {
+               regval = RVi(V, B(src), B(NI_RGOUT0));
+               if (!regval && (RVi(V, B(src), B(NI_RTSI_BRD(0))) ||
+                               RVi(V, B(src), B(NI_RTSI_BRD(1))) ||
+                               RVi(V, B(src), B(NI_RTSI_BRD(2))) ||
+                               RVi(V, B(src), B(NI_RTSI_BRD(3)))))
+                       regval = BIT(6);
+       }
+
+       if (!regval)
+               return -1;
+       /* mask out the valid-value marking bit */
+       return UnV(regval);
+}
+EXPORT_SYMBOL_GPL(ni_route_to_register);
+
+int ni_find_route_source(const u8 src_sel_reg_value, int dest,
+                        const struct ni_route_tables *tables)
+{
+       int src;
+
+       dest = B(dest); /* subtract NI names offset */
+       /* ensure we are not going to under/over run the route value table */
+       if (dest < 0 || dest >= NI_NUM_NAMES)
+               return -EINVAL;
+       for (src = 0; src < NI_NUM_NAMES; ++src)
+               if (RVi(tables->route_values, src, dest) ==
+                   V(src_sel_reg_value))
+                       return src + NI_NAMES_BASE;
+       return -EINVAL;
+}
+EXPORT_SYMBOL_GPL(ni_find_route_source);
+
+/* **** END Routes search routines **** */
+
+/* **** BEGIN simple module entry/exit functions **** */
+static int __init ni_routes_module_init(void)
+{
+       return 0;
+}
+
+static void __exit ni_routes_module_exit(void)
+{
+}
+
+module_init(ni_routes_module_init);
+module_exit(ni_routes_module_exit);
+
+MODULE_AUTHOR("Comedi http://www.comedi.org";);
+MODULE_DESCRIPTION("Comedi helper for routing signals-->terminals for NI");
+MODULE_LICENSE("GPL");
+/* **** END simple module entry/exit functions **** */
diff --git a/drivers/staging/comedi/drivers/ni_routes.h 
b/drivers/staging/comedi/drivers/ni_routes.h
new file mode 100644
index 0000000..9422cf5
--- /dev/null
+++ b/drivers/staging/comedi/drivers/ni_routes.h
@@ -0,0 +1,315 @@
+/* vim: set ts=8 sw=8 noet tw=80 nowrap: */
+/*
+ *  comedi/drivers/ni_routes.h
+ *  Route information for NI boards.
+ *
+ *  COMEDI - Linux Control and Measurement Device Interface
+ *  Copyright (C) 2016 Spencer E. Olson <olso...@umich.edu>
+ *
+ *  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; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  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.
+*/
+
+#ifndef _COMEDI_DRIVERS_NI_ROUTES_H
+#define _COMEDI_DRIVERS_NI_ROUTES_H
+
+/** Set of destinations with a common source. */
+struct ni_route_set {
+       int src;
+       int n_dest;
+       const int *dest;
+};
+
+/** List of all src->dest sets for a particular device. */
+struct ni_device_routes {
+       const char *device;
+       int n_route_sets;
+       const struct ni_route_set *routes;
+};
+
+/**
+ * Link to the valid src->dest routes and the register values used to assign
+ * such routes for that particular device.
+ */
+struct ni_route_tables {
+       const struct ni_device_routes *valid_routes;
+       const u8 *route_values;
+};
+
+/**
+ * Assign the proper lookup table for NI signal routing to the specified NI
+ * device.
+ * Return: -ENODATA if assignment was not successful; 0 if successful.
+ */
+int ni_assign_device_routes(const char *device_family,
+                           const char *board_name,
+                           struct ni_route_tables *tables);
+
+/**
+ * Finds the proper route_set with the specified source.
+ * Return: NULL if no route_set is found with the specified source;
+ *     otherwise, a pointer to the route_set if found.
+ */
+const struct ni_route_set *
+ni_find_route_set(const int source,
+                 const struct ni_device_routes *valid_routes);
+
+/**
+ * Determines whether the given desintation in included given route_set.
+ * Return: true if found; false otherwise.
+ */
+bool ni_route_set_has_destination(const struct ni_route_set *routes,
+                                 const int dest);
+
+/**
+ * Validates and converts the specified signal route (src-->dest) to the value
+ * used at the appropriate register.
+ * @src:       global-identifier for route source
+ * @dest:      global-identifier for route destination
+ * @tables:    pointer to relevant set of routing tables.
+ *
+ * Generally speaking, most routes require the first six bits and a few require
+ * 7 bits.  Special handling is given for the return value when the route is to
+ * be handled by the RTSI sub-device.  In this case, the returned register may
+ * not be sufficient to define the entire route path, but rather may only
+ * indicate the intermediate route.  For example, if the route must go through
+ * the RGOUT0 pin, the (src->RGOUT0) register value will be returned.
+ * Similarly, if the route must go through the NI_RTSI_BRD lines, the BIT(6)
+ * will be set:
+ *
+ * if route does not need RTSI_BRD lines:
+ *   bits 0:7 : register value
+ *              for a route that must go through RGOUT0 pin, this will be equal
+ *              to the (src->RGOUT0) register value.
+ * else: * route is (src->RTSI_BRD(x), RTSI_BRD(x)->TRIGGER_LINE(i)) *
+ *   bits 0:5 : zero
+ *   bits 6   : set to 1
+ *   bits 7:7 : zero
+ *
+ * Return: register value to be used for source at destination with special
+ *     cases given above; Otherwise, -1 if the specified route is not valid for
+ *     this particular device.
+ */
+s8 ni_route_to_register(const int src, const int dest,
+                       const struct ni_route_tables *tables);
+
+static inline bool ni_rtsi_route_requires_mux(s8 value)
+{
+       return value & BIT(6);
+}
+
+/**
+ * Look up a register value for a particular route without checking whether the
+ * route is valid for the particular device.
+ * @src:       global-identifier for route source
+ * @dest:      global-identifier for route destination
+ * @tables:    pointer to relevant set of routing tables.
+ *
+ * Return: -EINVAL if the specified route is not valid for this device family.
+ */
+s8 ni_lookup_route_register(int src, int dest,
+                           const struct ni_route_tables *tables);
+
+/**
+ * Determines whether the specified signal route (src-->dest) is valid for the
+ * given NI comedi_device.
+ * @src:       global-identifier for route source
+ * @dest:      global-identifier for route destination
+ * @tables:    pointer to relevant set of routing tables.
+ *
+ * Return: True if the route is valid, otherwise false.
+ */
+static inline bool route_is_valid(const int src, const int dest,
+                                 const struct ni_route_tables *tables)
+{
+       return ni_route_to_register(src, dest, tables) >= 0;
+}
+
+/**
+ * Simply tests for whether the value exists in the given list of the given
+ * size.
+ * @value: item to search for.
+ * @list: list to search from.
+ * @n: length of list to search from.
+ *
+ * TODO:  Is there already an implementation of this in the kernel headers?
+ */
+static inline bool in_list(const int value, const int *list, const size_t n)
+{
+       int i;
+
+       for (i = 0; i < n; ++i)
+               if (list[i] == value)
+                       return true;
+       return false;
+}
+
+/**
+ * List of NI global signal names that, as destinations, are only routeable
+ * indirectly through the *_arg elements of the comedi_cmd structure.
+ */
+static const int NI_CMD_DESTS[] = {
+       NI_AI_SampleClock,
+       NI_AI_StartTrigger,
+       NI_AI_ConvertClock,
+       NI_AO_SampleClock,
+       NI_AO_StartTrigger,
+       NI_DI_SampleClock,
+       NI_DO_SampleClock,
+};
+
+/**
+ * Determine whether the given destination is only configurable via a 
comedi_cmd
+ * struct.
+ */
+static inline bool ni_is_cmd_dest(int dest)
+{
+       return in_list(dest, NI_CMD_DESTS, sizeof(NI_CMD_DESTS));
+}
+
+static inline bool channel_is_pfi(int channel)
+{
+       return NI_PFI(0) <= channel && channel <= NI_PFI(-1);
+}
+
+static inline bool channel_is_rtsi(int channel)
+{
+       return TRIGGER_LINE(0) <= channel && channel <= TRIGGER_LINE(-1);
+}
+
+static inline bool channel_is_ctr(int channel)
+{
+       return channel >= NI_COUNTER_NAMES_BASE &&
+              channel <= NI_COUNTER_NAMES_MAX;
+}
+
+/**
+ * Count the number of valid routes.
+ */
+unsigned int ni_count_valid_routes(const struct ni_route_tables *tables);
+
+/**
+ * Implements INSN_DEVICE_CONFIG_GET_ROUTES.
+ * @tables:    pointer to relevant set of routing tables.
+ * @pptr:      pointer to which an allocated list of routes will be attached.
+ *
+ * Note that the caller must free the data returned in *pptr.
+ *
+ * Return: the number of valid routes if pptr == NULL; otherwise, the number of
+ *     valid routes copied if pptr != NULL.
+ */
+unsigned int ni_get_valid_routes(const struct ni_route_tables *tables,
+                                struct comedi_route_pair **pptr);
+
+/**
+ * Finds the signal source corresponding to a signal route (src-->dest) of the
+ * specified routing register value and the specified route destination on
+ * the specified device.
+ *
+ * Note that this function does _not_ validate the source based on device
+ * routes.
+ *
+ * Return: The NI signal value (e.g. NI_PFI(0) or PXI_Clk10) if found.
+ *     If the source was not found (i.e. the register value is not
+ *     valid for any routes to the destination), -EINVAL is returned.
+ */
+int ni_find_route_source(const u8 src_sel_reg_value, const int dest,
+                        const struct ni_route_tables *tables);
+
+/**
+ * Determines whether the register value for the specified route destination on
+ * the specified device is valid.
+ */
+static inline bool route_register_is_valid(const u8 src_sel_reg_value,
+                                          const int dest,
+                                          const struct ni_route_tables *tables)
+{
+       return ni_find_route_source(src_sel_reg_value, dest, tables) >= 0;
+}
+
+/**
+ * Determines the proper register value for a particular valid NI
+ * signal/terminal route.
+ * @src:       Either a direct register value or one of NI_* signal names.
+ * @dest:      global-identifier for route destination
+ * @tables:    pointer to relevant set of routing tables.
+ * @direct_reg_offset:
+ *             Compatibility compensation argument.  This argument allows us to
+ *             arbitrarily apply an offset to src if src is a direct register
+ *             value reference.  This is necessary to be compatible with
+ *             definitions of register values as previously exported directly
+ *             to user space.
+ *
+ * Return: the register value (>0) to be used at the destination if the src is
+ *     valid for the given destination; -1 otherwise.
+ */
+static inline s8 ni_get_reg_value_roffs(int src, const int dest,
+                                       const struct ni_route_tables *tables,
+                                       const int direct_reg_offset)
+{
+       if (src < NI_NAMES_BASE) {
+               src += direct_reg_offset;
+               /*
+                * In this case, the src is expected to actually be a register
+                * value.
+                */
+               if (route_register_is_valid(src, dest, tables))
+                       return src;
+               return -1;
+       }
+
+       /*
+        * Otherwise, the src is expected to be one of the abstracted NI
+        * signal/terminal names.
+        */
+       return ni_route_to_register(src, dest, tables);
+}
+
+static inline int ni_get_reg_value(const int src, const int dest,
+                                  const struct ni_route_tables *tables)
+{
+       return ni_get_reg_value_roffs(src, dest, tables, 0);
+}
+
+/**
+ * Checks the trigger argument (*_arg) of an NI device to ensure that the *_arg
+ * value corresponds to _either_ a valid register value to defines a trigger
+ * source, _or_ a valid NI signal/terminal name that has a valid route to the
+ * destination on the particular device.
+ * @src:       Either a direct register value or one of NI_* signal names.
+ * @dest:      global-identifier for route destination
+ * @tables:    pointer to relevant set of routing tables.
+ * @direct_reg_offset:
+ *             Compatibility compensation argument.  This argument allows us to
+ *             arbitrarily apply an offset to src if src is a direct register
+ *             value reference.  This is necessary to be compatible with
+ *             definitions of register values as previously exported directly
+ *             to user space.
+ *
+ * Return: 0 if the src (either register value or NI signal/terminal name) is
+ *     valid for the destination; -EINVAL otherwise.
+ */
+static inline
+int ni_check_trigger_arg_roffs(int src, const int dest,
+                              const struct ni_route_tables *tables,
+                              const int direct_reg_offset)
+{
+       if (ni_get_reg_value_roffs(src, dest, tables, direct_reg_offset) < 0)
+               return -EINVAL;
+       return 0;
+}
+
+static inline int ni_check_trigger_arg(const int src, const int dest,
+                                      const struct ni_route_tables *tables)
+{
+       return ni_check_trigger_arg_roffs(src, dest, tables, 0);
+}
+
+#endif /* _COMEDI_DRIVERS_NI_ROUTES_H */
diff --git a/drivers/staging/comedi/drivers/ni_stc.h 
b/drivers/staging/comedi/drivers/ni_stc.h
index f27b545..79b287d 100644
--- a/drivers/staging/comedi/drivers/ni_stc.h
+++ b/drivers/staging/comedi/drivers/ni_stc.h
@@ -24,6 +24,7 @@
 #define _COMEDI_NI_STC_H
 
 #include "ni_tio.h"
+#include "ni_routes.h"
 
 /*
  * Registers in the National Instruments DAQ-STC chip
@@ -1067,6 +1068,9 @@ struct ni_private {
         * possible.
         */
        unsigned int ao_needs_arming:1;
+
+       /* device signal route tables */
+       struct ni_route_tables routing_tables;
 };
 
 static const struct comedi_lrange range_ni_E_ao_ext;
diff --git a/drivers/staging/comedi/drivers/tests/Makefile 
b/drivers/staging/comedi/drivers/tests/Makefile
index 67281af..9efbfa1 100644
--- a/drivers/staging/comedi/drivers/tests/Makefile
+++ b/drivers/staging/comedi/drivers/tests/Makefile
@@ -2,4 +2,5 @@
 #
 ccflags-$(CONFIG_COMEDI_DEBUG)         := -DDEBUG
 
-obj-$(CONFIG_COMEDI_TESTS)             += example_test.o
+obj-$(CONFIG_COMEDI_TESTS)             += example_test.o ni_routes_test.o
+CFLAGS_ni_routes_test.o                        := -DDEBUG
diff --git a/drivers/staging/comedi/drivers/tests/ni_routes_test.c 
b/drivers/staging/comedi/drivers/tests/ni_routes_test.c
new file mode 100644
index 0000000..37d5375
--- /dev/null
+++ b/drivers/staging/comedi/drivers/tests/ni_routes_test.c
@@ -0,0 +1,596 @@
+/* vim: set ts=8 sw=8 noet tw=80 nowrap: */
+/*
+ *  comedi/drivers/tests/ni_routes_test.c
+ *  Unit tests for NI routes (ni_routes.c module).
+ *
+ *  COMEDI - Linux Control and Measurement Device Interface
+ *  Copyright (C) 2016 Spencer E. Olson <olso...@umich.edu>
+ *
+ *  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; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  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.
+*/
+
+#include <linux/module.h>
+
+#include "../ni_stc.h"
+#include "../ni_routes.h"
+#include "unittest.h"
+
+#define RVi(table, src, dest)  (table)[(src) * NI_NUM_NAMES + (dest)]
+#define O(x)   ((x) + NI_NAMES_BASE)
+#define B(x)   ((x) - NI_NAMES_BASE)
+#define V(x)   ((x) | 0x80)
+
+/* *** BEGIN fake board data *** */
+static const char *pci_6070e = "pci-6070e";
+static const char *pci_6220 = "pci-6220";
+static const char *pci_fake = "pci-fake";
+
+static const char *ni_eseries = "ni_eseries";
+static const char *ni_mseries = "ni_mseries";
+
+static struct ni_board_struct board = {
+       .name = NULL,
+};
+
+static struct ni_private private = {
+       .is_m_series = 0,
+};
+
+static const int badsrc = O(8), src0 = O(0), srci = O(5);
+static const int ith_src_index = 2;
+static const int no_val_src = O(7), no_val_index = 4;
+
+/* These have to be defs to be used in init code below */
+#define rgout0_src0    (O(100))
+#define rgout0_src1    (O(101))
+#define brd0_src0      (O(110))
+#define brd0_src1      (O(111))
+#define brd1_src0      (O(120))
+#define brd1_src1      (O(121))
+#define brd2_src0      (O(130))
+#define brd2_src1      (O(131))
+#define brd3_src0      (O(140))
+#define brd3_src1      (O(141))
+
+/* I1 and I2 should not call O(...).  Mostly here to shut checkpatch.pl up */
+#define I1(x1) \
+       (const int[]){ \
+               x1 \
+       }
+#define I2(x1, x2)     \
+       (const int[]){ \
+               (x1), (x2) \
+       }
+
+/* O9 is build to call O(...) for each arg */
+#define O9(x1, x2, x3, x4, x5, x6, x7, x8, x9) \
+       (const int[]){ \
+               O(x1), O(x2), O(x3), O(x4), O(x5), O(x6), O(x7), O(x8), O(x9) \
+       }
+
+static const struct ni_device_routes DR = {
+       .device = "testdev",
+       .n_route_sets = 16,
+       .routes = (const struct ni_route_set[]){
+               {.src = O(0), .n_dest = 9,
+                .dest = O9(/**/1, 2, 3, 4, 5, 6, 7, 8, 9)},
+               {.src = O(1), .n_dest = 9,
+                .dest = O9(0, /**/2, 3, 4, 5, 6, 7, 8, 9)},
+               /* ith route_set */
+               {.src = O(5), .n_dest = 9,
+                .dest = O9(0, 1, 2, 3, 4,/**/ 6, 7, 8, 9)},
+               {.src = O(6), .n_dest = 9,
+                .dest = O9(0, 1, 2, 3, 4, 5,/**/ 7, 8, 9)},
+               /* next one will not have valid reg values */
+               {.src = O(7), .n_dest = 9,
+                .dest = O9(0, 1, 2, 3, 4, 5, 6,/**/ 8, 9)},
+               {.src = O(9), .n_dest = 9,
+                .dest = O9(0, 1, 2, 3, 4, 5, 6, 7, 8/**/)},
+
+               /* indirect routes done through muxes */
+               {.src = rgout0_src0, .n_dest = 2,
+                .dest = I2(TRIGGER_LINE(0), TRIGGER_LINE(1))},
+               {.src = rgout0_src1, .n_dest = 2,
+                .dest = I2(TRIGGER_LINE(2), TRIGGER_LINE(3))},
+               {.src = brd0_src0, .n_dest = 1,
+                .dest = I1(TRIGGER_LINE(4))},
+               {.src = brd0_src1, .n_dest = 1,
+                .dest = I1(TRIGGER_LINE(4))},
+               {.src = brd1_src0, .n_dest = 1,
+                .dest = I1(TRIGGER_LINE(3))},
+               {.src = brd1_src1, .n_dest = 1,
+                .dest = I1(TRIGGER_LINE(3))},
+               {.src = brd2_src0, .n_dest = 1,
+                .dest = I1(TRIGGER_LINE(2))},
+               {.src = brd2_src1, .n_dest = 1,
+                .dest = I1(TRIGGER_LINE(2))},
+               {.src = brd3_src0, .n_dest = 1,
+                .dest = I1(TRIGGER_LINE(1))},
+               {.src = brd3_src1, .n_dest = 1,
+                .dest = I1(TRIGGER_LINE(1))},
+       },
+};
+
+#undef I1
+#undef I2
+#undef O9
+
+#define RV9(x1, x2, x3, x4, x5, x6, x7, x8, x9, val) \
+       [x1] = val, [x2] = val, [x3] = val, [x4] = val, [x5] = val, \
+       [x6] = val, [x7] = val, [x8] = val, [x9] = val,
+
+static const u8 RV[NI_NUM_NAMES][NI_NUM_NAMES] = {
+       [0] = {RV9(/**/1, 2, 3, 4, 5, 6, 7, 8, 9, V(0))},
+       [1] = {RV9(0,/**/ 2, 3, 4, 5, 6, 7, 8, 9, V(1))},
+       [2] = {RV9(0,  1,/**/3, 4, 5, 6, 7, 8, 9, V(2))},
+       [3] = {RV9(0,  1, 2,/**/4, 5, 6, 7, 8, 9, V(3))},
+       [4] = {RV9(0,  1, 2, 3,/**/5, 6, 7, 8, 9, V(4))},
+       [5] = {RV9(0,  1, 2, 3, 4,/**/6, 7, 8, 9, V(5))},
+       [6] = {RV9(0,  1, 2, 3, 4, 5,/**/7, 8, 9, V(6))},
+       /* [7] is intentionaly left absent to test invalid routes */
+       [8] = {RV9(0,  1, 2, 3, 4, 5, 6, 7,/**/9, V(8))},
+       [9] = {RV9(0,  1, 2, 3, 4, 5, 6, 7, 8,/**/V(9))},
+       /* some tests for needing extra muxes */
+       [B(rgout0_src0)] = {[B(NI_RGOUT0)]      = V(0)},
+       [B(rgout0_src1)] = {[B(NI_RGOUT0)]      = V(1)},
+       [B(brd0_src0)]   = {[B(NI_RTSI_BRD(0))] = V(0)},
+       [B(brd0_src1)]   = {[B(NI_RTSI_BRD(0))] = V(1)},
+       [B(brd1_src0)]   = {[B(NI_RTSI_BRD(1))] = V(0)},
+       [B(brd1_src1)]   = {[B(NI_RTSI_BRD(1))] = V(1)},
+       [B(brd2_src0)]   = {[B(NI_RTSI_BRD(2))] = V(0)},
+       [B(brd2_src1)]   = {[B(NI_RTSI_BRD(2))] = V(1)},
+       [B(brd3_src0)]   = {[B(NI_RTSI_BRD(3))] = V(0)},
+       [B(brd3_src1)]   = {[B(NI_RTSI_BRD(3))] = V(1)},
+};
+
+#undef RV9
+
+/* *** END fake board data *** */
+
+/* *** BEGIN board data initializers *** */
+static inline void init_private(void)
+{
+       memset(&private, 0, sizeof(struct ni_private));
+}
+
+static inline void init_pci_6070e(void)
+{
+       board.name = pci_6070e;
+       init_private();
+       private.is_m_series = 0;
+}
+
+static inline void init_pci_6220(void)
+{
+       board.name = pci_6220;
+       init_private();
+       private.is_m_series = 1;
+}
+
+static inline void init_pci_fake(void)
+{
+       board.name = pci_fake;
+       init_private();
+       private.routing_tables.route_values = &RV[0][0];
+       private.routing_tables.valid_routes = &DR;
+}
+
+/* *** END board data initializers *** */
+
+/* Tests that route_sets are in order of the source signal. */
+static inline bool
+route_set_sources_in_order(const struct ni_device_routes *devroutes)
+{
+       int i;
+       int last = NI_NAMES_BASE - 1;
+
+       for (i = 0; i < devroutes->n_route_sets; ++i) {
+               if (last >= devroutes->routes[i].src)
+                       return false;
+               last = devroutes->routes[i].src;
+       }
+       return true;
+}
+
+/* Tests that all route_set->dests are in order of the dest terminal. */
+bool route_set_dests_in_order(const struct ni_device_routes *devroutes)
+{
+       int i;
+
+       for (i = 0; i < devroutes->n_route_sets; ++i) {
+               int j;
+               int last = NI_NAMES_BASE - 1;
+
+               for (j = 0; j < devroutes->routes[i].n_dest; ++j) {
+                       if (last >= devroutes->routes[i].dest[j])
+                               return false;
+                       last = devroutes->routes[i].dest[j];
+               }
+       }
+       return true;
+}
+
+void test_ni_assign_device_routes(void)
+{
+       const struct ni_device_routes *devroutes, *olddevroutes;
+       const u8 *table, *oldtable;
+
+       init_pci_6070e();
+       ni_assign_device_routes(ni_eseries, pci_6070e, &private.routing_tables);
+       devroutes = private.routing_tables.valid_routes;
+       table = private.routing_tables.route_values;
+
+       unittest(strncmp(devroutes->device, pci_6070e, 10) == 0,
+                "find device pci-6070e\n");
+       unittest(devroutes->n_route_sets == 39,
+                "number of pci-6070e route_sets == 39\n");
+       unittest(devroutes->routes->src == NI_PFI(0),
+                "first pci-6070e route_set is for NI_PFI(0)\n");
+       unittest(devroutes->routes->n_dest == 14,
+                "first pci-6070e route_set length == 14\n");
+       unittest(devroutes->routes->dest[0] == NI_CtrSource(0),
+                "first pci-6070e route_set dest. == NI_CtrSource(0)\n");
+       unittest(route_set_sources_in_order(devroutes),
+                "all pci-6070e route_sets in order of source signal values\n");
+       unittest(route_set_dests_in_order(devroutes),
+                "all pci-6070e route_set->dests in order of dest terminal\n");
+
+       unittest(
+         RVi(table, B(NI_10MHzRefClock), B(NI_MasterTimebase)) == V(0) &&
+         RVi(table, B(NI_10MHzRefClock), B(TRIGGER_LINE(0))) == 0 &&
+         RVi(table, B(NI_AI_ConvertClock), B(NI_PFI(0))) == 0 &&
+         RVi(table, B(NI_AI_ConvertClock), B(NI_PFI(2))) ==
+               V(NI_PFI_OUTPUT_AI_CONVERT),
+         "pci-6070e finds e-series route_values table\n");
+
+       olddevroutes = devroutes;
+       oldtable = table;
+       init_pci_6220();
+       ni_assign_device_routes(ni_mseries, pci_6220, &private.routing_tables);
+       devroutes = private.routing_tables.valid_routes;
+       table = private.routing_tables.route_values;
+
+       unittest(strncmp(devroutes->device, pci_6220, 10) == 0,
+                "find device pci-6220\n");
+       unittest(oldtable != table, "pci-6220 find other route_values table\n");
+       unittest((devroutes - olddevroutes) == 1,
+                "pci-6070e,pci-6220 consecutive elements in 
device_routes_list\n");
+
+       unittest(
+         RVi(table, B(NI_10MHzRefClock), B(NI_MasterTimebase)) == V(0) &&
+         RVi(table, B(NI_10MHzRefClock), B(TRIGGER_LINE(0))) == V(12) &&
+         RVi(table, B(NI_AI_ConvertClock), B(NI_PFI(0))) == V(3) &&
+         RVi(table, B(NI_AI_ConvertClock), B(NI_PFI(2))) == V(3),
+         "pci-6220 finds m-series route_values table\n");
+}
+
+void test_ni_find_route_set(void)
+{
+       unittest(ni_find_route_set(badsrc, &DR) == NULL,
+                "check for nonexistent route_set\n");
+       unittest(ni_find_route_set(src0, &DR) == &DR.routes[0],
+                "find first route_set\n");
+       unittest(ni_find_route_set(srci, &DR) == &DR.routes[ith_src_index],
+                "find ith route_set\n");
+       unittest(ni_find_route_set(no_val_src, &DR) == &DR.routes[no_val_index],
+                "find no_val route_set in spite of missing values\n");
+       unittest(ni_find_route_set(DR.routes[DR.n_route_sets - 1].src, &DR) ==
+                &DR.routes[DR.n_route_sets - 1],
+                "find last route_set\n");
+}
+
+void test_ni_route_set_has_destination(void)
+{
+       unittest(!ni_route_set_has_destination(&DR.routes[0], O(0)),
+                "check for bad destination\n");
+       unittest(ni_route_set_has_destination(&DR.routes[0], O(1)),
+                "find first destination\n");
+       unittest(ni_route_set_has_destination(&DR.routes[0], O(5)),
+                "find fifth destination\n");
+       unittest(ni_route_set_has_destination(&DR.routes[0], O(9)),
+                "find last destination\n");
+}
+
+void test_ni_route_to_register(void)
+{
+       const struct ni_route_tables *T = &private.routing_tables;
+
+       init_pci_fake();
+       unittest(ni_route_to_register(O(0), O(0), T) < 0,
+                "check for bad route 0-->0\n");
+       unittest(ni_route_to_register(O(0), O(1), T) == 0,
+                "validate first destination\n");
+       unittest(ni_route_to_register(O(5), O(6), T) == 5,
+                "validate middle destination\n");
+       unittest(ni_route_to_register(O(9), O(8), T) == 9,
+                "validate last destination\n");
+
+       /* choice of trigger line in the following is somewhat random */
+       unittest(ni_route_to_register(rgout0_src0, TRIGGER_LINE(0), T) == 0,
+                "validate indirect route through rgout0 to TRIGGER_LINE(0)\n");
+       unittest(ni_route_to_register(rgout0_src0, TRIGGER_LINE(1), T) == 0,
+                "validate indirect route through rgout0 to TRIGGER_LINE(1)\n");
+       unittest(ni_route_to_register(rgout0_src1, TRIGGER_LINE(2), T) == 1,
+                "validate indirect route through rgout0 to TRIGGER_LINE(2)\n");
+       unittest(ni_route_to_register(rgout0_src1, TRIGGER_LINE(3), T) == 1,
+                "validate indirect route through rgout0 to TRIGGER_LINE(3)\n");
+
+       unittest(ni_route_to_register(brd0_src0, TRIGGER_LINE(4), T) ==
+                BIT(6),
+                "validate indirect route through brd0 to TRIGGER_LINE(4)\n");
+       unittest(ni_route_to_register(brd0_src1, TRIGGER_LINE(4), T) ==
+                BIT(6),
+                "validate indirect route through brd0 to TRIGGER_LINE(4)\n");
+       unittest(ni_route_to_register(brd1_src0, TRIGGER_LINE(3), T) ==
+                BIT(6),
+                "validate indirect route through brd1 to TRIGGER_LINE(3)\n");
+       unittest(ni_route_to_register(brd1_src1, TRIGGER_LINE(3), T) ==
+                BIT(6),
+                "validate indirect route through brd1 to TRIGGER_LINE(3)\n");
+       unittest(ni_route_to_register(brd2_src0, TRIGGER_LINE(2), T) ==
+                BIT(6),
+                "validate indirect route through brd2 to TRIGGER_LINE(2)\n");
+       unittest(ni_route_to_register(brd2_src1, TRIGGER_LINE(2), T) ==
+                BIT(6),
+                "validate indirect route through brd2 to TRIGGER_LINE(2)\n");
+       unittest(ni_route_to_register(brd3_src0, TRIGGER_LINE(1), T) ==
+                BIT(6),
+                "validate indirect route through brd3 to TRIGGER_LINE(1)\n");
+       unittest(ni_route_to_register(brd3_src1, TRIGGER_LINE(1), T) ==
+                BIT(6),
+                "validate indirect route through brd3 to TRIGGER_LINE(1)\n");
+}
+
+void test_ni_lookup_route_register(void)
+{
+       const struct ni_route_tables *T = &private.routing_tables;
+
+       init_pci_fake();
+       unittest(ni_lookup_route_register(O(0), O(0), T) == -EINVAL,
+                "check for bad route 0-->0\n");
+       unittest(ni_lookup_route_register(O(0), O(1), T) == 0,
+                "validate first destination\n");
+       unittest(ni_lookup_route_register(O(5), O(6), T) == 5,
+                "validate middle destination\n");
+       unittest(ni_lookup_route_register(O(9), O(8), T) == 9,
+                "validate last destination\n");
+       unittest(ni_lookup_route_register(O(9), O(10), T) == -EINVAL,
+                "lookup invalid desination\n");
+
+       unittest(ni_lookup_route_register(rgout0_src0, TRIGGER_LINE(0), T) ==
+                -EINVAL,
+                "rgout0_src0: directly lookup indirect route register (should 
fail)\n");
+       unittest(ni_lookup_route_register(rgout0_src0, NI_RGOUT0, T) == 0,
+                "rgout0_src0: lookup indirect route register\n");
+       unittest(ni_lookup_route_register(rgout0_src1, TRIGGER_LINE(2), T) ==
+                -EINVAL,
+                "rgout0_src1: directly lookup indirect route register (should 
fail)\n");
+       unittest(ni_lookup_route_register(rgout0_src1, NI_RGOUT0, T) == 1,
+                "rgout0_src1: lookup indirect route register\n");
+
+       unittest(ni_lookup_route_register(brd0_src0, TRIGGER_LINE(4), T) ==
+                -EINVAL,
+                "brd0_src0: directly lookup indirect route register (should 
fail)\n");
+       unittest(ni_lookup_route_register(brd0_src0, NI_RTSI_BRD(0), T) == 0,
+                "brd0_src0: lookup indirect route register\n");
+       unittest(ni_lookup_route_register(brd0_src1, TRIGGER_LINE(4), T) ==
+                -EINVAL,
+                "brd0_src1: directly lookup indirect route register (should 
fail)\n");
+       unittest(ni_lookup_route_register(brd0_src1, NI_RTSI_BRD(0), T) == 1,
+                "brd0_src1: lookup indirect route register\n");
+}
+
+void test_route_is_valid(void)
+{
+       const struct ni_route_tables *T = &private.routing_tables;
+
+       init_pci_fake();
+       unittest(!route_is_valid(O(0), O(0), T),
+                "check for bad route 0-->0\n");
+       unittest(route_is_valid(O(0), O(1), T),
+                "validate first destination\n");
+       unittest(route_is_valid(O(5), O(6), T),
+                "validate middle destination\n");
+       unittest(route_is_valid(O(9), O(8), T),
+                "validate last destination\n");
+}
+
+void test_ni_is_cmd_dest(void)
+{
+       init_pci_fake();
+       unittest(ni_is_cmd_dest(NI_AI_SampleClock),
+                "check that AI/SampleClock is cmd destination\n");
+       unittest(ni_is_cmd_dest(NI_AI_StartTrigger),
+                "check that AI/StartTrigger is cmd destination\n");
+       unittest(ni_is_cmd_dest(NI_AI_ConvertClock),
+                "check that AI/ConvertClock is cmd destination\n");
+       unittest(ni_is_cmd_dest(NI_AO_SampleClock),
+                "check that AO/SampleClock is cmd destination\n");
+       unittest(ni_is_cmd_dest(NI_DO_SampleClock),
+                "check that DO/SampleClock is cmd destination\n");
+       unittest(!ni_is_cmd_dest(NI_AO_SampleClockTimebase),
+                "check that AO/SampleClockTimebase _not_ cmd destination\n");
+}
+
+void test_channel_is_pfi(void)
+{
+       init_pci_fake();
+       unittest(channel_is_pfi(NI_PFI(0)), "check First pfi channel\n");
+       unittest(channel_is_pfi(NI_PFI(10)), "check 10th pfi channel\n");
+       unittest(channel_is_pfi(NI_PFI(-1)), "check last pfi channel\n");
+       unittest(!channel_is_pfi(NI_PFI(-1) + 1),
+                "check first non pfi channel\n");
+}
+
+void test_channel_is_rtsi(void)
+{
+       init_pci_fake();
+       unittest(channel_is_rtsi(TRIGGER_LINE(0)),
+                "check First rtsi channel\n");
+       unittest(channel_is_rtsi(TRIGGER_LINE(3)),
+                "check 3rd rtsi channel\n");
+       unittest(channel_is_rtsi(TRIGGER_LINE(-1)),
+                "check last rtsi channel\n");
+       unittest(!channel_is_rtsi(TRIGGER_LINE(-1) + 1),
+                "check first non rtsi channel\n");
+}
+
+void test_ni_count_valid_routes(void)
+{
+       const struct ni_route_tables *T = &private.routing_tables;
+
+       init_pci_fake();
+       unittest(ni_count_valid_routes(T) == 57, "count all valid routes\n");
+}
+
+void test_ni_get_valid_routes(void)
+{
+       const struct ni_route_tables *T = &private.routing_tables;
+
+       init_pci_fake();
+       unittest(ni_get_valid_routes(T, NULL) == 57,
+                "count all valid routes through ni_get_valid_routes\n");
+}
+
+void test_ni_find_route_source(void)
+{
+       const struct ni_route_tables *T = &private.routing_tables;
+
+       init_pci_fake();
+       unittest(ni_find_route_source(4, O(4), T) == -EINVAL,
+                "check for bad source 4-->4\n");
+       unittest(ni_find_route_source(0, O(1), T) == O(0),
+                "find first source\n");
+       unittest(ni_find_route_source(4, O(6), T) == O(4),
+                "find middle source\n");
+       unittest(ni_find_route_source(9, O(8), T) == O(9),
+                "find last source");
+
+       unittest(ni_find_route_source(8, O(9), T) == O(8),
+                "find invalid source (without checking device routes)\n");
+}
+
+void test_route_register_is_valid(void)
+{
+       const struct ni_route_tables *T = &private.routing_tables;
+
+       init_pci_fake();
+       unittest(route_register_is_valid(4, O(4), T) == false,
+                "check for bad source 4-->4\n");
+       unittest(route_register_is_valid(0, O(1), T) == true,
+                "find first source\n");
+       unittest(route_register_is_valid(4, O(6), T) == true,
+                "find middle source\n");
+       unittest(route_register_is_valid(9, O(8), T) == true,
+                "find last source");
+}
+
+void test_ni_check_trigger_arg(void)
+{
+       const struct ni_route_tables *T = &private.routing_tables;
+
+       init_pci_fake();
+       unittest(ni_check_trigger_arg(0, O(0), T) == -EINVAL,
+                "check bad direct trigger arg for first reg->dest\n");
+       unittest(ni_check_trigger_arg(0, O(1), T) == 0,
+                "check direct trigger arg for first reg->dest\n");
+       unittest(ni_check_trigger_arg(4, O(6), T) == 0,
+                "check direct trigger arg for middle reg->dest\n");
+       unittest(ni_check_trigger_arg(9, O(8), T) == 0,
+                "check direct trigger arg for last reg->dest\n");
+
+       unittest(ni_check_trigger_arg_roffs(-1, O(0), T, 1) == -EINVAL,
+                "check bad direct trigger arg for first reg->dest w/offs\n");
+       unittest(ni_check_trigger_arg_roffs(0, O(1), T, 0) == 0,
+                "check direct trigger arg for first reg->dest w/offs\n");
+       unittest(ni_check_trigger_arg_roffs(3, O(6), T, 1) == 0,
+                "check direct trigger arg for middle reg->dest w/offs\n");
+       unittest(ni_check_trigger_arg_roffs(7, O(8), T, 2) == 0,
+                "check direct trigger arg for last reg->dest w/offs\n");
+
+       unittest(ni_check_trigger_arg(O(0), O(0), T) == -EINVAL,
+                "check bad trigger arg for first src->dest\n");
+       unittest(ni_check_trigger_arg(O(0), O(1), T) == 0,
+                "check trigger arg for first src->dest\n");
+       unittest(ni_check_trigger_arg(O(5), O(6), T) == 0,
+                "check trigger arg for middle src->dest\n");
+       unittest(ni_check_trigger_arg(O(9), O(8), T) == 0,
+                "check trigger arg for last src->dest\n");
+}
+
+void test_ni_get_reg_value(void)
+{
+       const struct ni_route_tables *T = &private.routing_tables;
+
+       init_pci_fake();
+       unittest(ni_get_reg_value(0, O(0), T) == -1,
+                "check bad direct trigger arg for first reg->dest\n");
+       unittest(ni_get_reg_value(0, O(1), T) == 0,
+                "check direct trigger arg for first reg->dest\n");
+       unittest(ni_get_reg_value(4, O(6), T) == 4,
+                "check direct trigger arg for middle reg->dest\n");
+       unittest(ni_get_reg_value(9, O(8), T) == 9,
+                "check direct trigger arg for last reg->dest\n");
+
+       unittest(ni_get_reg_value_roffs(-1, O(0), T, 1) == -1,
+                "check bad direct trigger arg for first reg->dest w/offs\n");
+       unittest(ni_get_reg_value_roffs(0, O(1), T, 0) == 0,
+                "check direct trigger arg for first reg->dest w/offs\n");
+       unittest(ni_get_reg_value_roffs(3, O(6), T, 1) == 4,
+                "check direct trigger arg for middle reg->dest w/offs\n");
+       unittest(ni_get_reg_value_roffs(7, O(8), T, 2) == 9,
+                "check direct trigger arg for last reg->dest w/offs\n");
+
+       unittest(ni_get_reg_value(O(0), O(0), T) == -1,
+                "check bad trigger arg for first src->dest\n");
+       unittest(ni_get_reg_value(O(0), O(1), T) == 0,
+                "check trigger arg for first src->dest\n");
+       unittest(ni_get_reg_value(O(5), O(6), T) == 5,
+                "check trigger arg for middle src->dest\n");
+       unittest(ni_get_reg_value(O(9), O(8), T) == 9,
+                "check trigger arg for last src->dest\n");
+}
+
+/* **** BEGIN simple module entry/exit functions **** */
+static int ni_routes_unittest(void)
+{
+       const unittest_fptr unit_tests[] = {
+               (unittest_fptr)test_ni_assign_device_routes,
+               (unittest_fptr)test_ni_find_route_set,
+               (unittest_fptr)test_ni_route_set_has_destination,
+               (unittest_fptr)test_ni_route_to_register,
+               (unittest_fptr)test_ni_lookup_route_register,
+               (unittest_fptr)test_route_is_valid,
+               (unittest_fptr)test_ni_is_cmd_dest,
+               (unittest_fptr)test_channel_is_pfi,
+               (unittest_fptr)test_channel_is_rtsi,
+               (unittest_fptr)test_ni_count_valid_routes,
+               (unittest_fptr)test_ni_get_valid_routes,
+               (unittest_fptr)test_ni_find_route_source,
+               (unittest_fptr)test_route_register_is_valid,
+               (unittest_fptr)test_ni_check_trigger_arg,
+               (unittest_fptr)test_ni_get_reg_value,
+               NULL,
+       };
+
+       exec_unittests("ni_routes", unit_tests);
+       return 0;
+}
+
+static void __exit ni_routes_unittest_exit(void) { }
+
+module_init(ni_routes_unittest);
+module_exit(ni_routes_unittest_exit);
+
+MODULE_AUTHOR("Comedi http://www.comedi.org";);
+MODULE_DESCRIPTION("Comedi unit-tests for ni_routes module");
+MODULE_LICENSE("GPL");
+/* **** END simple module entry/exit functions **** */
-- 
1.9.1

_______________________________________________
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

Reply via email to