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