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