linux/drivers/comedi/drivers/ni_routes.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 *  comedi/drivers/ni_routes.c
   4 *  Route information for NI boards.
   5 *
   6 *  COMEDI - Linux Control and Measurement Device Interface
   7 *  Copyright (C) 2016 Spencer E. Olson <olsonse@umich.edu>
   8 *
   9 *  This program is free software; you can redistribute it and/or modify
  10 *  it under the terms of the GNU General Public License as published by
  11 *  the Free Software Foundation; either version 2 of the License, or
  12 *  (at your option) any later version.
  13 *
  14 *  This program is distributed in the hope that it will be useful,
  15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17 *  GNU General Public License for more details.
  18 */
  19
  20#include <linux/module.h>
  21#include <linux/slab.h>
  22#include <linux/bsearch.h>
  23#include <linux/sort.h>
  24
  25#include "../comedi.h"
  26
  27#include "ni_routes.h"
  28#include "ni_routing/ni_route_values.h"
  29#include "ni_routing/ni_device_routes.h"
  30
  31/*
  32 * This is defined in ni_routing/ni_route_values.h:
  33 * #define B(x) ((x) - NI_NAMES_BASE)
  34 */
  35
  36/*
  37 * These are defined in ni_routing/ni_route_values.h to identify clearly
  38 * elements of the table that were set.  In other words, entries that are zero
  39 * are invalid.  To get the value to use for the register, one must mask out the
  40 * high bit.
  41 *
  42 * #define V(x) ((x) | 0x80)
  43 *
  44 * #define UNMARK(x)    ((x) & (~(0x80)))
  45 *
  46 */
  47
  48/* Helper for accessing data. */
  49#define RVi(table, src, dest)   ((table)[(dest) * NI_NUM_NAMES + (src)])
  50
  51/*
  52 * Find the route values for a device family.
  53 */
  54static const u8 *ni_find_route_values(const char *device_family)
  55{
  56        const u8 *rv = NULL;
  57        int i;
  58
  59        for (i = 0; ni_all_route_values[i]; ++i) {
  60                if (memcmp(ni_all_route_values[i]->family, device_family,
  61                           strnlen(device_family, 30)) == 0) {
  62                        rv = &ni_all_route_values[i]->register_values[0][0];
  63                        break;
  64                }
  65        }
  66        return rv;
  67}
  68
  69/*
  70 * Find the valid routes for a board.
  71 */
  72static const struct ni_device_routes *
  73ni_find_valid_routes(const char *board_name)
  74{
  75        const struct ni_device_routes *dr = NULL;
  76        int i;
  77
  78        for (i = 0; ni_device_routes_list[i]; ++i) {
  79                if (memcmp(ni_device_routes_list[i]->device, board_name,
  80                           strnlen(board_name, 30)) == 0) {
  81                        dr = ni_device_routes_list[i];
  82                        break;
  83                }
  84        }
  85        return dr;
  86}
  87
  88/*
  89 * Find the proper route_values and ni_device_routes tables for this particular
  90 * device.  Possibly try an alternate board name if device routes not found
  91 * for the actual board name.
  92 *
  93 * Return: -ENODATA if either was not found; 0 if both were found.
  94 */
  95static int ni_find_device_routes(const char *device_family,
  96                                 const char *board_name,
  97                                 const char *alt_board_name,
  98                                 struct ni_route_tables *tables)
  99{
 100        const struct ni_device_routes *dr;
 101        const u8 *rv;
 102
 103        /* First, find the register_values table for this device family */
 104        rv = ni_find_route_values(device_family);
 105
 106        /* Second, find the set of routes valid for this device. */
 107        dr = ni_find_valid_routes(board_name);
 108        if (!dr && alt_board_name)
 109                dr = ni_find_valid_routes(alt_board_name);
 110
 111        tables->route_values = rv;
 112        tables->valid_routes = dr;
 113
 114        if (!rv || !dr)
 115                return -ENODATA;
 116
 117        return 0;
 118}
 119
 120/**
 121 * ni_assign_device_routes() - Assign the proper lookup table for NI signal
 122 *                             routing to the specified NI device.
 123 * @device_family: Device family name (determines route values).
 124 * @board_name: Board name (determines set of routes).
 125 * @alt_board_name: Optional alternate board name to try on failure.
 126 * @tables: Pointer to assigned routing information.
 127 *
 128 * Finds the route values for the device family and the set of valid routes
 129 * for the board.  If valid routes could not be found for the actual board
 130 * name and an alternate board name has been specified, try that one.
 131 *
 132 * On failure, the assigned routing information may be partially filled
 133 * (for example, with the route values but not the set of valid routes).
 134 *
 135 * Return: -ENODATA if assignment was not successful; 0 if successful.
 136 */
 137int ni_assign_device_routes(const char *device_family,
 138                            const char *board_name,
 139                            const char *alt_board_name,
 140                            struct ni_route_tables *tables)
 141{
 142        memset(tables, 0, sizeof(struct ni_route_tables));
 143        return ni_find_device_routes(device_family, board_name, alt_board_name,
 144                                     tables);
 145}
 146EXPORT_SYMBOL_GPL(ni_assign_device_routes);
 147
 148/**
 149 * ni_count_valid_routes() - Count the number of valid routes.
 150 * @tables: Routing tables for which to count all valid routes.
 151 */
 152unsigned int ni_count_valid_routes(const struct ni_route_tables *tables)
 153{
 154        int total = 0;
 155        int i;
 156
 157        for (i = 0; i < tables->valid_routes->n_route_sets; ++i) {
 158                const struct ni_route_set *R = &tables->valid_routes->routes[i];
 159                int j;
 160
 161                for (j = 0; j < R->n_src; ++j) {
 162                        const int src  = R->src[j];
 163                        const int dest = R->dest;
 164                        const u8 *rv = tables->route_values;
 165
 166                        if (RVi(rv, B(src), B(dest)))
 167                                /* direct routing is valid */
 168                                ++total;
 169                        else if (channel_is_rtsi(dest) &&
 170                                 (RVi(rv, B(src), B(NI_RGOUT0)) ||
 171                                  RVi(rv, B(src), B(NI_RTSI_BRD(0))) ||
 172                                  RVi(rv, B(src), B(NI_RTSI_BRD(1))) ||
 173                                  RVi(rv, B(src), B(NI_RTSI_BRD(2))) ||
 174                                  RVi(rv, B(src), B(NI_RTSI_BRD(3))))) {
 175                                ++total;
 176                        }
 177                }
 178        }
 179        return total;
 180}
 181EXPORT_SYMBOL_GPL(ni_count_valid_routes);
 182
 183/**
 184 * ni_get_valid_routes() - Implements INSN_DEVICE_CONFIG_GET_ROUTES.
 185 * @tables:     pointer to relevant set of routing tables.
 186 * @n_pairs:    Number of pairs for which memory is allocated by the user.  If
 187 *              the user specifies '0', only the number of available pairs is
 188 *              returned.
 189 * @pair_data:  Pointer to memory allocated to return pairs back to user.  Each
 190 *              even, odd indexed member of this array will hold source,
 191 *              destination of a route pair respectively.
 192 *
 193 * Return: the number of valid routes if n_pairs == 0; otherwise, the number of
 194 *      valid routes copied.
 195 */
 196unsigned int ni_get_valid_routes(const struct ni_route_tables *tables,
 197                                 unsigned int n_pairs,
 198                                 unsigned int *pair_data)
 199{
 200        unsigned int n_valid = ni_count_valid_routes(tables);
 201        int i;
 202
 203        if (n_pairs == 0 || n_valid == 0)
 204                return n_valid;
 205
 206        if (!pair_data)
 207                return 0;
 208
 209        n_valid = 0;
 210
 211        for (i = 0; i < tables->valid_routes->n_route_sets; ++i) {
 212                const struct ni_route_set *R = &tables->valid_routes->routes[i];
 213                int j;
 214
 215                for (j = 0; j < R->n_src; ++j) {
 216                        const int src  = R->src[j];
 217                        const int dest = R->dest;
 218                        bool valid = false;
 219                        const u8 *rv = tables->route_values;
 220
 221                        if (RVi(rv, B(src), B(dest)))
 222                                /* direct routing is valid */
 223                                valid = true;
 224                        else if (channel_is_rtsi(dest) &&
 225                                 (RVi(rv, B(src), B(NI_RGOUT0)) ||
 226                                  RVi(rv, B(src), B(NI_RTSI_BRD(0))) ||
 227                                  RVi(rv, B(src), B(NI_RTSI_BRD(1))) ||
 228                                  RVi(rv, B(src), B(NI_RTSI_BRD(2))) ||
 229                                  RVi(rv, B(src), B(NI_RTSI_BRD(3))))) {
 230                                /* indirect routing also valid */
 231                                valid = true;
 232                        }
 233
 234                        if (valid) {
 235                                pair_data[2 * n_valid] = src;
 236                                pair_data[2 * n_valid + 1] = dest;
 237                                ++n_valid;
 238                        }
 239
 240                        if (n_valid >= n_pairs)
 241                                return n_valid;
 242                }
 243        }
 244        return n_valid;
 245}
 246EXPORT_SYMBOL_GPL(ni_get_valid_routes);
 247
 248/*
 249 * List of NI global signal names that, as destinations, are only routeable
 250 * indirectly through the *_arg elements of the comedi_cmd structure.
 251 */
 252static const int NI_CMD_DESTS[] = {
 253        NI_AI_SampleClock,
 254        NI_AI_StartTrigger,
 255        NI_AI_ConvertClock,
 256        NI_AO_SampleClock,
 257        NI_AO_StartTrigger,
 258        NI_DI_SampleClock,
 259        NI_DO_SampleClock,
 260};
 261
 262/**
 263 * ni_is_cmd_dest() - Determine whether the given destination is only
 264 *                    configurable via a comedi_cmd struct.
 265 * @dest: Destination to test.
 266 */
 267bool ni_is_cmd_dest(int dest)
 268{
 269        int i;
 270
 271        for (i = 0; i < ARRAY_SIZE(NI_CMD_DESTS); ++i)
 272                if (NI_CMD_DESTS[i] == dest)
 273                        return true;
 274        return false;
 275}
 276EXPORT_SYMBOL_GPL(ni_is_cmd_dest);
 277
 278/* **** BEGIN Routes sort routines **** */
 279static int _ni_sort_destcmp(const void *va, const void *vb)
 280{
 281        const struct ni_route_set *a = va;
 282        const struct ni_route_set *b = vb;
 283
 284        if (a->dest < b->dest)
 285                return -1;
 286        else if (a->dest > b->dest)
 287                return 1;
 288        return 0;
 289}
 290
 291static int _ni_sort_srccmp(const void *vsrc0, const void *vsrc1)
 292{
 293        const int *src0 = vsrc0;
 294        const int *src1 = vsrc1;
 295
 296        if (*src0 < *src1)
 297                return -1;
 298        else if (*src0 > *src1)
 299                return 1;
 300        return 0;
 301}
 302
 303/**
 304 * ni_sort_device_routes() - Sort the list of valid device signal routes in
 305 *                           preparation for use.
 306 * @valid_routes:       pointer to ni_device_routes struct to sort.
 307 */
 308void ni_sort_device_routes(struct ni_device_routes *valid_routes)
 309{
 310        unsigned int n;
 311
 312        /* 1. Count and set the number of ni_route_set objects. */
 313        valid_routes->n_route_sets = 0;
 314        while (valid_routes->routes[valid_routes->n_route_sets].dest != 0)
 315                ++valid_routes->n_route_sets;
 316
 317        /* 2. sort all ni_route_set objects by destination. */
 318        sort(valid_routes->routes, valid_routes->n_route_sets,
 319             sizeof(struct ni_route_set), _ni_sort_destcmp, NULL);
 320
 321        /* 3. Loop through each route_set for sorting. */
 322        for (n = 0; n < valid_routes->n_route_sets; ++n) {
 323                struct ni_route_set *rs = &valid_routes->routes[n];
 324
 325                /* 3a. Count and set the number of sources. */
 326                rs->n_src = 0;
 327                while (rs->src[rs->n_src])
 328                        ++rs->n_src;
 329
 330                /* 3a. Sort sources. */
 331                sort(valid_routes->routes[n].src, valid_routes->routes[n].n_src,
 332                     sizeof(int), _ni_sort_srccmp, NULL);
 333        }
 334}
 335EXPORT_SYMBOL_GPL(ni_sort_device_routes);
 336
 337/* sort all valid device signal routes in prep for use */
 338static void ni_sort_all_device_routes(void)
 339{
 340        unsigned int i;
 341
 342        for (i = 0; ni_device_routes_list[i]; ++i)
 343                ni_sort_device_routes(ni_device_routes_list[i]);
 344}
 345
 346/* **** BEGIN Routes search routines **** */
 347static int _ni_bsearch_destcmp(const void *vkey, const void *velt)
 348{
 349        const int *key = vkey;
 350        const struct ni_route_set *elt = velt;
 351
 352        if (*key < elt->dest)
 353                return -1;
 354        else if (*key > elt->dest)
 355                return 1;
 356        return 0;
 357}
 358
 359static int _ni_bsearch_srccmp(const void *vkey, const void *velt)
 360{
 361        const int *key = vkey;
 362        const int *elt = velt;
 363
 364        if (*key < *elt)
 365                return -1;
 366        else if (*key > *elt)
 367                return 1;
 368        return 0;
 369}
 370
 371/**
 372 * ni_find_route_set() - Finds the proper route set with the specified
 373 *                       destination.
 374 * @destination: Destination of which to search for the route set.
 375 * @valid_routes: Pointer to device routes within which to search.
 376 *
 377 * Return: NULL if no route_set is found with the specified @destination;
 378 *      otherwise, a pointer to the route_set if found.
 379 */
 380const struct ni_route_set *
 381ni_find_route_set(const int destination,
 382                  const struct ni_device_routes *valid_routes)
 383{
 384        return bsearch(&destination, valid_routes->routes,
 385                       valid_routes->n_route_sets, sizeof(struct ni_route_set),
 386                       _ni_bsearch_destcmp);
 387}
 388EXPORT_SYMBOL_GPL(ni_find_route_set);
 389
 390/*
 391 * ni_route_set_has_source() - Determines whether the given source is in
 392 *                             included given route_set.
 393 *
 394 * Return: true if found; false otherwise.
 395 */
 396bool ni_route_set_has_source(const struct ni_route_set *routes,
 397                             const int source)
 398{
 399        if (!bsearch(&source, routes->src, routes->n_src, sizeof(int),
 400                     _ni_bsearch_srccmp))
 401                return false;
 402        return true;
 403}
 404EXPORT_SYMBOL_GPL(ni_route_set_has_source);
 405
 406/**
 407 * ni_lookup_route_register() - Look up a register value for a particular route
 408 *                              without checking whether the route is valid for
 409 *                              the particular device.
 410 * @src:        global-identifier for route source
 411 * @dest:       global-identifier for route destination
 412 * @tables:     pointer to relevant set of routing tables.
 413 *
 414 * Return: -EINVAL if the specified route is not valid for this device family.
 415 */
 416s8 ni_lookup_route_register(int src, int dest,
 417                            const struct ni_route_tables *tables)
 418{
 419        s8 regval;
 420
 421        /*
 422         * Be sure to use the B() macro to subtract off the NI_NAMES_BASE before
 423         * indexing into the route_values array.
 424         */
 425        src = B(src);
 426        dest = B(dest);
 427        if (src < 0 || src >= NI_NUM_NAMES || dest < 0 || dest >= NI_NUM_NAMES)
 428                return -EINVAL;
 429        regval = RVi(tables->route_values, src, dest);
 430        if (!regval)
 431                return -EINVAL;
 432        /* mask out the valid-value marking bit */
 433        return UNMARK(regval);
 434}
 435EXPORT_SYMBOL_GPL(ni_lookup_route_register);
 436
 437/**
 438 * ni_route_to_register() - Validates and converts the specified signal route
 439 *                          (src-->dest) to the value used at the appropriate
 440 *                          register.
 441 * @src:        global-identifier for route source
 442 * @dest:       global-identifier for route destination
 443 * @tables:     pointer to relevant set of routing tables.
 444 *
 445 * Generally speaking, most routes require the first six bits and a few require
 446 * 7 bits.  Special handling is given for the return value when the route is to
 447 * be handled by the RTSI sub-device.  In this case, the returned register may
 448 * not be sufficient to define the entire route path, but rather may only
 449 * indicate the intermediate route.  For example, if the route must go through
 450 * the RGOUT0 pin, the (src->RGOUT0) register value will be returned.
 451 * Similarly, if the route must go through the NI_RTSI_BRD lines, the BIT(6)
 452 * will be set:
 453 *
 454 * if route does not need RTSI_BRD lines:
 455 *   bits 0:7 : register value
 456 *              for a route that must go through RGOUT0 pin, this will be equal
 457 *              to the (src->RGOUT0) register value.
 458 * else: * route is (src->RTSI_BRD(x), RTSI_BRD(x)->TRIGGER_LINE(i)) *
 459 *   bits 0:5 : zero
 460 *   bits 6   : set to 1
 461 *   bits 7:7 : zero
 462 *
 463 * Return: register value to be used for source at destination with special
 464 *      cases given above; Otherwise, -1 if the specified route is not valid for
 465 *      this particular device.
 466 */
 467s8 ni_route_to_register(const int src, const int dest,
 468                        const struct ni_route_tables *tables)
 469{
 470        const struct ni_route_set *routes =
 471                ni_find_route_set(dest, tables->valid_routes);
 472        const u8 *rv;
 473        s8 regval;
 474
 475        /* first check to see if source is listed with bunch of destinations. */
 476        if (!routes)
 477                return -1;
 478        /* 2nd, check to see if destination is in list of source's targets. */
 479        if (!ni_route_set_has_source(routes, src))
 480                return -1;
 481        /*
 482         * finally, check to see if we know how to route...
 483         * Be sure to use the B() macro to subtract off the NI_NAMES_BASE before
 484         * indexing into the route_values array.
 485         */
 486        rv = tables->route_values;
 487        regval = RVi(rv, B(src), B(dest));
 488
 489        /*
 490         * if we did not validate the route, we'll see if we can route through
 491         * one of the muxes
 492         */
 493        if (!regval && channel_is_rtsi(dest)) {
 494                regval = RVi(rv, B(src), B(NI_RGOUT0));
 495                if (!regval && (RVi(rv, B(src), B(NI_RTSI_BRD(0))) ||
 496                                RVi(rv, B(src), B(NI_RTSI_BRD(1))) ||
 497                                RVi(rv, B(src), B(NI_RTSI_BRD(2))) ||
 498                                RVi(rv, B(src), B(NI_RTSI_BRD(3)))))
 499                        regval = BIT(6);
 500        }
 501
 502        if (!regval)
 503                return -1;
 504        /* mask out the valid-value marking bit */
 505        return UNMARK(regval);
 506}
 507EXPORT_SYMBOL_GPL(ni_route_to_register);
 508
 509/*
 510 * ni_find_route_source() - Finds the signal source corresponding to a signal
 511 *                          route (src-->dest) of the specified routing register
 512 *                          value and the specified route destination on the
 513 *                          specified device.
 514 *
 515 * Note that this function does _not_ validate the source based on device
 516 * routes.
 517 *
 518 * Return: The NI signal value (e.g. NI_PFI(0) or PXI_Clk10) if found.
 519 *      If the source was not found (i.e. the register value is not
 520 *      valid for any routes to the destination), -EINVAL is returned.
 521 */
 522int ni_find_route_source(const u8 src_sel_reg_value, int dest,
 523                         const struct ni_route_tables *tables)
 524{
 525        int src;
 526
 527        if (!tables->route_values)
 528                return -EINVAL;
 529
 530        dest = B(dest); /* subtract NI names offset */
 531        /* ensure we are not going to under/over run the route value table */
 532        if (dest < 0 || dest >= NI_NUM_NAMES)
 533                return -EINVAL;
 534        for (src = 0; src < NI_NUM_NAMES; ++src)
 535                if (RVi(tables->route_values, src, dest) ==
 536                    V(src_sel_reg_value))
 537                        return src + NI_NAMES_BASE;
 538        return -EINVAL;
 539}
 540EXPORT_SYMBOL_GPL(ni_find_route_source);
 541
 542/* **** END Routes search routines **** */
 543
 544/* **** BEGIN simple module entry/exit functions **** */
 545static int __init ni_routes_module_init(void)
 546{
 547        ni_sort_all_device_routes();
 548        return 0;
 549}
 550
 551static void __exit ni_routes_module_exit(void)
 552{
 553}
 554
 555module_init(ni_routes_module_init);
 556module_exit(ni_routes_module_exit);
 557
 558MODULE_AUTHOR("Comedi https://www.comedi.org");
 559MODULE_DESCRIPTION("Comedi helper for routing signals-->terminals for NI");
 560MODULE_LICENSE("GPL");
 561/* **** END simple module entry/exit functions **** */
 562
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.