1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  comedi/drivers/tests/ni_routes_test.c
4  *  Unit tests for NI routes (ni_routes.c module).
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 
22 #include "../ni_stc.h"
23 #include "../ni_routes.h"
24 #include "unittest.h"
25 
26 #define RVI(table, src, dest)	((table)[(dest) * NI_NUM_NAMES + (src)])
27 #define O(x)	((x) + NI_NAMES_BASE)
28 #define B(x)	((x) - NI_NAMES_BASE)
29 #define V(x)	((x) | 0x80)
30 
31 /* *** BEGIN fake board data *** */
32 static const char *pci_6070e = "pci-6070e";
33 static const char *pci_6220 = "pci-6220";
34 static const char *pci_fake = "pci-fake";
35 
36 static const char *ni_eseries = "ni_eseries";
37 static const char *ni_mseries = "ni_mseries";
38 
39 static struct ni_board_struct board = {
40 	.name = NULL,
41 };
42 
43 static struct ni_private private = {
44 	.is_m_series = 0,
45 };
46 
47 static const int bad_dest = O(8), dest0 = O(0), desti = O(5);
48 static const int ith_dest_index = 2;
49 static const int no_val_dest = O(7), no_val_index = 4;
50 
51 /* These have to be defs to be used in init code below */
52 #define rgout0_src0	(O(100))
53 #define rgout0_src1	(O(101))
54 #define brd0_src0	(O(110))
55 #define brd0_src1	(O(111))
56 #define brd1_src0	(O(120))
57 #define brd1_src1	(O(121))
58 #define brd2_src0	(O(130))
59 #define brd2_src1	(O(131))
60 #define brd3_src0	(O(140))
61 #define brd3_src1	(O(141))
62 
63 /* I1 and I2 should not call O(...).  Mostly here to shut checkpatch.pl up */
64 #define I1(x1)	\
65 	((int[]){ \
66 		(x1), 0 \
67 	 })
68 #define I2(x1, x2)	\
69 	((int[]){ \
70 		(x1), (x2), 0 \
71 	 })
72 #define I3(x1, x2, x3)	\
73 	((int[]){ \
74 		(x1), (x2), (x3), 0 \
75 	 })
76 
77 /* O9 is build to call O(...) for each arg */
78 #define O9(x1, x2, x3, x4, x5, x6, x7, x8, x9)	\
79 	((int[]){ \
80 		O(x1), O(x2), O(x3), O(x4), O(x5), O(x6), O(x7), O(x8), O(x9), \
81 		0 \
82 	 })
83 
84 static struct ni_device_routes DR = {
85 	.device = "testdev",
86 	.routes = (struct ni_route_set[]){
87 		{.dest = O(0), .src = O9(/**/1, 2, 3, 4, 5, 6, 7, 8, 9)},
88 		{.dest = O(1), .src = O9(0, /**/2, 3, 4, 5, 6, 7, 8, 9)},
89 		/* ith route_set */
90 		{.dest = O(5), .src = O9(0, 1, 2, 3, 4,/**/ 6, 7, 8, 9)},
91 		{.dest = O(6), .src = O9(0, 1, 2, 3, 4, 5,/**/ 7, 8, 9)},
92 		/* next one will not have valid reg values */
93 		{.dest = O(7), .src = O9(0, 1, 2, 3, 4, 5, 6,/**/ 8, 9)},
94 		{.dest = O(9), .src = O9(0, 1, 2, 3, 4, 5, 6, 7, 8/**/)},
95 
96 		/* indirect routes done through muxes */
97 		{.dest = TRIGGER_LINE(0), .src = I1(rgout0_src0)},
98 		{.dest = TRIGGER_LINE(1), .src = I3(rgout0_src0,
99 						    brd3_src0,
100 						    brd3_src1)},
101 		{.dest = TRIGGER_LINE(2), .src = I3(rgout0_src1,
102 						    brd2_src0,
103 						    brd2_src1)},
104 		{.dest = TRIGGER_LINE(3), .src = I3(rgout0_src1,
105 						    brd1_src0,
106 						    brd1_src1)},
107 		{.dest = TRIGGER_LINE(4), .src = I2(brd0_src0,
108 						    brd0_src1)},
109 		{.dest = 0},
110 	},
111 };
112 
113 #undef I1
114 #undef I2
115 #undef O9
116 
117 #define RV9(x1, x2, x3, x4, x5, x6, x7, x8, x9) \
118 	[x1] = V(x1), [x2] = V(x2), [x3] = V(x3), [x4] = V(x4), \
119 	[x5] = V(x5), [x6] = V(x6), [x7] = V(x7), [x8] = V(x8), \
120 	[x9] = V(x9),
121 
122 /* This table is indexed as RV[destination][source] */
123 static const u8 RV[NI_NUM_NAMES][NI_NUM_NAMES] = {
124 	[0] = {RV9(/**/1, 2, 3, 4, 5, 6, 7, 8, 9)},
125 	[1] = {RV9(0,/**/ 2, 3, 4, 5, 6, 7, 8, 9)},
126 	[2] = {RV9(0,  1,/**/3, 4, 5, 6, 7, 8, 9)},
127 	[3] = {RV9(0,  1, 2,/**/4, 5, 6, 7, 8, 9)},
128 	[4] = {RV9(0,  1, 2, 3,/**/5, 6, 7, 8, 9)},
129 	[5] = {RV9(0,  1, 2, 3, 4,/**/6, 7, 8, 9)},
130 	[6] = {RV9(0,  1, 2, 3, 4, 5,/**/7, 8, 9)},
131 	/* [7] is intentionaly left absent to test invalid routes */
132 	[8] = {RV9(0,  1, 2, 3, 4, 5, 6, 7,/**/9)},
133 	[9] = {RV9(0,  1, 2, 3, 4, 5, 6, 7, 8/**/)},
134 	/* some tests for needing extra muxes */
135 	[B(NI_RGOUT0)]	= {[B(rgout0_src0)]   = V(0),
136 			   [B(rgout0_src1)]   = V(1)},
137 	[B(NI_RTSI_BRD(0))] = {[B(brd0_src0)] = V(0),
138 			       [B(brd0_src1)] = V(1)},
139 	[B(NI_RTSI_BRD(1))] = {[B(brd1_src0)] = V(0),
140 			       [B(brd1_src1)] = V(1)},
141 	[B(NI_RTSI_BRD(2))] = {[B(brd2_src0)] = V(0),
142 			       [B(brd2_src1)] = V(1)},
143 	[B(NI_RTSI_BRD(3))] = {[B(brd3_src0)] = V(0),
144 			       [B(brd3_src1)] = V(1)},
145 };
146 
147 #undef RV9
148 
149 /* *** END fake board data *** */
150 
151 /* *** BEGIN board data initializers *** */
init_private(void)152 static void init_private(void)
153 {
154 	memset(&private, 0, sizeof(struct ni_private));
155 }
156 
init_pci_6070e(void)157 static void init_pci_6070e(void)
158 {
159 	board.name = pci_6070e;
160 	init_private();
161 	private.is_m_series = 0;
162 }
163 
init_pci_6220(void)164 static void init_pci_6220(void)
165 {
166 	board.name = pci_6220;
167 	init_private();
168 	private.is_m_series = 1;
169 }
170 
init_pci_fake(void)171 static void init_pci_fake(void)
172 {
173 	board.name = pci_fake;
174 	init_private();
175 	private.routing_tables.route_values = &RV[0][0];
176 	private.routing_tables.valid_routes = &DR;
177 }
178 
179 /* *** END board data initializers *** */
180 
181 /* Tests that route_sets are in order of the signal destination. */
route_set_dests_in_order(const struct ni_device_routes * devroutes)182 static bool route_set_dests_in_order(const struct ni_device_routes *devroutes)
183 {
184 	int i;
185 	int last = NI_NAMES_BASE - 1;
186 
187 	for (i = 0; i < devroutes->n_route_sets; ++i) {
188 		if (last >= devroutes->routes[i].dest)
189 			return false;
190 		last = devroutes->routes[i].dest;
191 	}
192 	return true;
193 }
194 
195 /* Tests that all route_set->src are in order of the signal source. */
route_set_sources_in_order(const struct ni_device_routes * devroutes)196 static bool route_set_sources_in_order(const struct ni_device_routes *devroutes)
197 {
198 	int i;
199 
200 	for (i = 0; i < devroutes->n_route_sets; ++i) {
201 		int j;
202 		int last = NI_NAMES_BASE - 1;
203 
204 		for (j = 0; j < devroutes->routes[i].n_src; ++j) {
205 			if (last >= devroutes->routes[i].src[j])
206 				return false;
207 			last = devroutes->routes[i].src[j];
208 		}
209 	}
210 	return true;
211 }
212 
test_ni_assign_device_routes(void)213 static void test_ni_assign_device_routes(void)
214 {
215 	const struct ni_device_routes *devroutes;
216 	const u8 *table, *oldtable;
217 
218 	init_pci_6070e();
219 	ni_assign_device_routes(ni_eseries, pci_6070e, NULL,
220 				&private.routing_tables);
221 	devroutes = private.routing_tables.valid_routes;
222 	table = private.routing_tables.route_values;
223 
224 	unittest(strncmp(devroutes->device, pci_6070e, 10) == 0,
225 		 "find device pci-6070e\n");
226 	unittest(devroutes->n_route_sets == 37,
227 		 "number of pci-6070e route_sets == 37\n");
228 	unittest(devroutes->routes->dest == NI_PFI(0),
229 		 "first pci-6070e route_set is for NI_PFI(0)\n");
230 	unittest(devroutes->routes->n_src == 1,
231 		 "first pci-6070e route_set length == 1\n");
232 	unittest(devroutes->routes->src[0] == NI_AI_StartTrigger,
233 		 "first pci-6070e route_set src. == NI_AI_StartTrigger\n");
234 	unittest(devroutes->routes[10].dest == TRIGGER_LINE(0),
235 		 "10th pci-6070e route_set is for TRIGGER_LINE(0)\n");
236 	unittest(devroutes->routes[10].n_src == 10,
237 		 "10th pci-6070e route_set length == 10\n");
238 	unittest(devroutes->routes[10].src[0] == NI_CtrSource(0),
239 		 "10th pci-6070e route_set src. == NI_CtrSource(0)\n");
240 	unittest(route_set_dests_in_order(devroutes),
241 		 "all pci-6070e route_sets in order of signal destination\n");
242 	unittest(route_set_sources_in_order(devroutes),
243 		 "all pci-6070e route_set->src's in order of signal source\n");
244 
245 	unittest(RVI(table, B(PXI_Star), B(NI_AI_SampleClock)) == V(17) &&
246 		 RVI(table, B(NI_10MHzRefClock), B(TRIGGER_LINE(0))) == 0 &&
247 		 RVI(table, B(NI_AI_ConvertClock), B(NI_PFI(0))) == 0 &&
248 		 RVI(table, B(NI_AI_ConvertClock), B(NI_PFI(2))) == V(NI_PFI_OUTPUT_AI_CONVERT),
249 		 "pci-6070e finds e-series route_values table\n");
250 
251 	oldtable = table;
252 	init_pci_6220();
253 	ni_assign_device_routes(ni_mseries, pci_6220, NULL,
254 				&private.routing_tables);
255 	devroutes = private.routing_tables.valid_routes;
256 	table = private.routing_tables.route_values;
257 
258 	unittest(strncmp(devroutes->device, pci_6220, 10) == 0,
259 		 "find device pci-6220\n");
260 	unittest(oldtable != table, "pci-6220 find other route_values table\n");
261 
262 	unittest(RVI(table, B(PXI_Star), B(NI_AI_SampleClock)) == V(20) &&
263 		 RVI(table, B(NI_10MHzRefClock), B(TRIGGER_LINE(0))) == V(12) &&
264 		 RVI(table, B(NI_AI_ConvertClock), B(NI_PFI(0))) == V(3) &&
265 		 RVI(table, B(NI_AI_ConvertClock), B(NI_PFI(2))) == V(3),
266 		 "pci-6220 finds m-series route_values table\n");
267 }
268 
test_ni_sort_device_routes(void)269 static void test_ni_sort_device_routes(void)
270 {
271 	/* We begin by sorting the device routes for use in later tests */
272 	ni_sort_device_routes(&DR);
273 	/* now we test that sorting. */
274 	unittest(route_set_dests_in_order(&DR),
275 		 "all route_sets of fake data in order of sig. destination\n");
276 	unittest(route_set_sources_in_order(&DR),
277 		 "all route_set->src's of fake data in order of sig. source\n");
278 }
279 
test_ni_find_route_set(void)280 static void test_ni_find_route_set(void)
281 {
282 	unittest(!ni_find_route_set(bad_dest, &DR),
283 		 "check for nonexistent route_set\n");
284 	unittest(ni_find_route_set(dest0, &DR) == &DR.routes[0],
285 		 "find first route_set\n");
286 	unittest(ni_find_route_set(desti, &DR) == &DR.routes[ith_dest_index],
287 		 "find ith route_set\n");
288 	unittest(ni_find_route_set(no_val_dest, &DR) ==
289 		 &DR.routes[no_val_index],
290 		 "find no_val route_set in spite of missing values\n");
291 	unittest(ni_find_route_set(DR.routes[DR.n_route_sets - 1].dest, &DR) ==
292 		 &DR.routes[DR.n_route_sets - 1],
293 		 "find last route_set\n");
294 }
295 
test_ni_route_set_has_source(void)296 static void test_ni_route_set_has_source(void)
297 {
298 	unittest(!ni_route_set_has_source(&DR.routes[0], O(0)),
299 		 "check for bad source\n");
300 	unittest(ni_route_set_has_source(&DR.routes[0], O(1)),
301 		 "find first source\n");
302 	unittest(ni_route_set_has_source(&DR.routes[0], O(5)),
303 		 "find fifth source\n");
304 	unittest(ni_route_set_has_source(&DR.routes[0], O(9)),
305 		 "find last source\n");
306 }
307 
test_ni_route_to_register(void)308 static void test_ni_route_to_register(void)
309 {
310 	const struct ni_route_tables *T = &private.routing_tables;
311 
312 	init_pci_fake();
313 	unittest(ni_route_to_register(O(0), O(0), T) < 0,
314 		 "check for bad route 0-->0\n");
315 	unittest(ni_route_to_register(O(1), O(0), T) == 1,
316 		 "validate first destination\n");
317 	unittest(ni_route_to_register(O(6), O(5), T) == 6,
318 		 "validate middle destination\n");
319 	unittest(ni_route_to_register(O(8), O(9), T) == 8,
320 		 "validate last destination\n");
321 
322 	/* choice of trigger line in the following is somewhat random */
323 	unittest(ni_route_to_register(rgout0_src0, TRIGGER_LINE(0), T) == 0,
324 		 "validate indirect route through rgout0 to TRIGGER_LINE(0)\n");
325 	unittest(ni_route_to_register(rgout0_src0, TRIGGER_LINE(1), T) == 0,
326 		 "validate indirect route through rgout0 to TRIGGER_LINE(1)\n");
327 	unittest(ni_route_to_register(rgout0_src1, TRIGGER_LINE(2), T) == 1,
328 		 "validate indirect route through rgout0 to TRIGGER_LINE(2)\n");
329 	unittest(ni_route_to_register(rgout0_src1, TRIGGER_LINE(3), T) == 1,
330 		 "validate indirect route through rgout0 to TRIGGER_LINE(3)\n");
331 
332 	unittest(ni_route_to_register(brd0_src0, TRIGGER_LINE(4), T) ==
333 		 BIT(6),
334 		 "validate indirect route through brd0 to TRIGGER_LINE(4)\n");
335 	unittest(ni_route_to_register(brd0_src1, TRIGGER_LINE(4), T) ==
336 		 BIT(6),
337 		 "validate indirect route through brd0 to TRIGGER_LINE(4)\n");
338 	unittest(ni_route_to_register(brd1_src0, TRIGGER_LINE(3), T) ==
339 		 BIT(6),
340 		 "validate indirect route through brd1 to TRIGGER_LINE(3)\n");
341 	unittest(ni_route_to_register(brd1_src1, TRIGGER_LINE(3), T) ==
342 		 BIT(6),
343 		 "validate indirect route through brd1 to TRIGGER_LINE(3)\n");
344 	unittest(ni_route_to_register(brd2_src0, TRIGGER_LINE(2), T) ==
345 		 BIT(6),
346 		 "validate indirect route through brd2 to TRIGGER_LINE(2)\n");
347 	unittest(ni_route_to_register(brd2_src1, TRIGGER_LINE(2), T) ==
348 		 BIT(6),
349 		 "validate indirect route through brd2 to TRIGGER_LINE(2)\n");
350 	unittest(ni_route_to_register(brd3_src0, TRIGGER_LINE(1), T) ==
351 		 BIT(6),
352 		 "validate indirect route through brd3 to TRIGGER_LINE(1)\n");
353 	unittest(ni_route_to_register(brd3_src1, TRIGGER_LINE(1), T) ==
354 		 BIT(6),
355 		 "validate indirect route through brd3 to TRIGGER_LINE(1)\n");
356 }
357 
test_ni_lookup_route_register(void)358 static void test_ni_lookup_route_register(void)
359 {
360 	const struct ni_route_tables *T = &private.routing_tables;
361 
362 	init_pci_fake();
363 	unittest(ni_lookup_route_register(O(0), O(0), T) == -EINVAL,
364 		 "check for bad route 0-->0\n");
365 	unittest(ni_lookup_route_register(O(1), O(0), T) == 1,
366 		 "validate first destination\n");
367 	unittest(ni_lookup_route_register(O(6), O(5), T) == 6,
368 		 "validate middle destination\n");
369 	unittest(ni_lookup_route_register(O(8), O(9), T) == 8,
370 		 "validate last destination\n");
371 	unittest(ni_lookup_route_register(O(10), O(9), T) == -EINVAL,
372 		 "lookup invalid destination\n");
373 
374 	unittest(ni_lookup_route_register(rgout0_src0, TRIGGER_LINE(0), T) ==
375 		 -EINVAL,
376 		 "rgout0_src0: no direct lookup of indirect route\n");
377 	unittest(ni_lookup_route_register(rgout0_src0, NI_RGOUT0, T) == 0,
378 		 "rgout0_src0: lookup indirect route register\n");
379 	unittest(ni_lookup_route_register(rgout0_src1, TRIGGER_LINE(2), T) ==
380 		 -EINVAL,
381 		 "rgout0_src1: no direct lookup of indirect route\n");
382 	unittest(ni_lookup_route_register(rgout0_src1, NI_RGOUT0, T) == 1,
383 		 "rgout0_src1: lookup indirect route register\n");
384 
385 	unittest(ni_lookup_route_register(brd0_src0, TRIGGER_LINE(4), T) ==
386 		 -EINVAL,
387 		 "brd0_src0: no direct lookup of indirect route\n");
388 	unittest(ni_lookup_route_register(brd0_src0, NI_RTSI_BRD(0), T) == 0,
389 		 "brd0_src0: lookup indirect route register\n");
390 	unittest(ni_lookup_route_register(brd0_src1, TRIGGER_LINE(4), T) ==
391 		 -EINVAL,
392 		 "brd0_src1: no direct lookup of indirect route\n");
393 	unittest(ni_lookup_route_register(brd0_src1, NI_RTSI_BRD(0), T) == 1,
394 		 "brd0_src1: lookup indirect route register\n");
395 }
396 
test_route_is_valid(void)397 static void test_route_is_valid(void)
398 {
399 	const struct ni_route_tables *T = &private.routing_tables;
400 
401 	init_pci_fake();
402 	unittest(!route_is_valid(O(0), O(0), T),
403 		 "check for bad route 0-->0\n");
404 	unittest(route_is_valid(O(0), O(1), T),
405 		 "validate first destination\n");
406 	unittest(route_is_valid(O(5), O(6), T),
407 		 "validate middle destination\n");
408 	unittest(route_is_valid(O(8), O(9), T),
409 		 "validate last destination\n");
410 }
411 
test_ni_is_cmd_dest(void)412 static void test_ni_is_cmd_dest(void)
413 {
414 	init_pci_fake();
415 	unittest(ni_is_cmd_dest(NI_AI_SampleClock),
416 		 "check that AI/SampleClock is cmd destination\n");
417 	unittest(ni_is_cmd_dest(NI_AI_StartTrigger),
418 		 "check that AI/StartTrigger is cmd destination\n");
419 	unittest(ni_is_cmd_dest(NI_AI_ConvertClock),
420 		 "check that AI/ConvertClock is cmd destination\n");
421 	unittest(ni_is_cmd_dest(NI_AO_SampleClock),
422 		 "check that AO/SampleClock is cmd destination\n");
423 	unittest(ni_is_cmd_dest(NI_DO_SampleClock),
424 		 "check that DO/SampleClock is cmd destination\n");
425 	unittest(!ni_is_cmd_dest(NI_AO_SampleClockTimebase),
426 		 "check that AO/SampleClockTimebase _not_ cmd destination\n");
427 }
428 
test_channel_is_pfi(void)429 static void test_channel_is_pfi(void)
430 {
431 	init_pci_fake();
432 	unittest(channel_is_pfi(NI_PFI(0)), "check First pfi channel\n");
433 	unittest(channel_is_pfi(NI_PFI(10)), "check 10th pfi channel\n");
434 	unittest(channel_is_pfi(NI_PFI(-1)), "check last pfi channel\n");
435 	unittest(!channel_is_pfi(NI_PFI(-1) + 1),
436 		 "check first non pfi channel\n");
437 }
438 
test_channel_is_rtsi(void)439 static void test_channel_is_rtsi(void)
440 {
441 	init_pci_fake();
442 	unittest(channel_is_rtsi(TRIGGER_LINE(0)),
443 		 "check First rtsi channel\n");
444 	unittest(channel_is_rtsi(TRIGGER_LINE(3)),
445 		 "check 3rd rtsi channel\n");
446 	unittest(channel_is_rtsi(TRIGGER_LINE(-1)),
447 		 "check last rtsi channel\n");
448 	unittest(!channel_is_rtsi(TRIGGER_LINE(-1) + 1),
449 		 "check first non rtsi channel\n");
450 }
451 
test_ni_count_valid_routes(void)452 static void test_ni_count_valid_routes(void)
453 {
454 	const struct ni_route_tables *T = &private.routing_tables;
455 
456 	init_pci_fake();
457 	unittest(ni_count_valid_routes(T) == 57, "count all valid routes\n");
458 }
459 
test_ni_get_valid_routes(void)460 static void test_ni_get_valid_routes(void)
461 {
462 	const struct ni_route_tables *T = &private.routing_tables;
463 	unsigned int pair_data[2];
464 
465 	init_pci_fake();
466 	unittest(ni_get_valid_routes(T, 0, NULL) == 57,
467 		 "count all valid routes through ni_get_valid_routes\n");
468 
469 	unittest(ni_get_valid_routes(T, 1, pair_data) == 1,
470 		 "copied first valid route from ni_get_valid_routes\n");
471 	unittest(pair_data[0] == O(1),
472 		 "source of first valid pair from ni_get_valid_routes\n");
473 	unittest(pair_data[1] == O(0),
474 		 "destination of first valid pair from ni_get_valid_routes\n");
475 }
476 
test_ni_find_route_source(void)477 static void test_ni_find_route_source(void)
478 {
479 	const struct ni_route_tables *T = &private.routing_tables;
480 
481 	init_pci_fake();
482 	unittest(ni_find_route_source(4, O(4), T) == -EINVAL,
483 		 "check for bad source 4-->4\n");
484 	unittest(ni_find_route_source(0, O(1), T) == O(0),
485 		 "find first source\n");
486 	unittest(ni_find_route_source(4, O(6), T) == O(4),
487 		 "find middle source\n");
488 	unittest(ni_find_route_source(9, O(8), T) == O(9),
489 		 "find last source");
490 	unittest(ni_find_route_source(8, O(9), T) == O(8),
491 		 "find invalid source (without checking device routes)\n");
492 }
493 
test_route_register_is_valid(void)494 static void test_route_register_is_valid(void)
495 {
496 	const struct ni_route_tables *T = &private.routing_tables;
497 
498 	init_pci_fake();
499 	unittest(!route_register_is_valid(4, O(4), T),
500 		 "check for bad source 4-->4\n");
501 	unittest(route_register_is_valid(0, O(1), T),
502 		 "find first source\n");
503 	unittest(route_register_is_valid(4, O(6), T),
504 		 "find middle source\n");
505 	unittest(route_register_is_valid(9, O(8), T),
506 		 "find last source");
507 }
508 
test_ni_check_trigger_arg(void)509 static void test_ni_check_trigger_arg(void)
510 {
511 	const struct ni_route_tables *T = &private.routing_tables;
512 
513 	init_pci_fake();
514 	unittest(ni_check_trigger_arg(0, O(0), T) == -EINVAL,
515 		 "check bad direct trigger arg for first reg->dest\n");
516 	unittest(ni_check_trigger_arg(0, O(1), T) == 0,
517 		 "check direct trigger arg for first reg->dest\n");
518 	unittest(ni_check_trigger_arg(4, O(6), T) == 0,
519 		 "check direct trigger arg for middle reg->dest\n");
520 	unittest(ni_check_trigger_arg(9, O(8), T) == 0,
521 		 "check direct trigger arg for last reg->dest\n");
522 
523 	unittest(ni_check_trigger_arg_roffs(-1, O(0), T, 1) == -EINVAL,
524 		 "check bad direct trigger arg for first reg->dest w/offs\n");
525 	unittest(ni_check_trigger_arg_roffs(0, O(1), T, 0) == 0,
526 		 "check direct trigger arg for first reg->dest w/offs\n");
527 	unittest(ni_check_trigger_arg_roffs(3, O(6), T, 1) == 0,
528 		 "check direct trigger arg for middle reg->dest w/offs\n");
529 	unittest(ni_check_trigger_arg_roffs(7, O(8), T, 2) == 0,
530 		 "check direct trigger arg for last reg->dest w/offs\n");
531 
532 	unittest(ni_check_trigger_arg(O(0), O(0), T) == -EINVAL,
533 		 "check bad trigger arg for first src->dest\n");
534 	unittest(ni_check_trigger_arg(O(0), O(1), T) == 0,
535 		 "check trigger arg for first src->dest\n");
536 	unittest(ni_check_trigger_arg(O(5), O(6), T) == 0,
537 		 "check trigger arg for middle src->dest\n");
538 	unittest(ni_check_trigger_arg(O(8), O(9), T) == 0,
539 		 "check trigger arg for last src->dest\n");
540 }
541 
test_ni_get_reg_value(void)542 static void test_ni_get_reg_value(void)
543 {
544 	const struct ni_route_tables *T = &private.routing_tables;
545 
546 	init_pci_fake();
547 	unittest(ni_get_reg_value(0, O(0), T) == -1,
548 		 "check bad direct trigger arg for first reg->dest\n");
549 	unittest(ni_get_reg_value(0, O(1), T) == 0,
550 		 "check direct trigger arg for first reg->dest\n");
551 	unittest(ni_get_reg_value(4, O(6), T) == 4,
552 		 "check direct trigger arg for middle reg->dest\n");
553 	unittest(ni_get_reg_value(9, O(8), T) == 9,
554 		 "check direct trigger arg for last reg->dest\n");
555 
556 	unittest(ni_get_reg_value_roffs(-1, O(0), T, 1) == -1,
557 		 "check bad direct trigger arg for first reg->dest w/offs\n");
558 	unittest(ni_get_reg_value_roffs(0, O(1), T, 0) == 0,
559 		 "check direct trigger arg for first reg->dest w/offs\n");
560 	unittest(ni_get_reg_value_roffs(3, O(6), T, 1) == 4,
561 		 "check direct trigger arg for middle reg->dest w/offs\n");
562 	unittest(ni_get_reg_value_roffs(7, O(8), T, 2) == 9,
563 		 "check direct trigger arg for last reg->dest w/offs\n");
564 
565 	unittest(ni_get_reg_value(O(0), O(0), T) == -1,
566 		 "check bad trigger arg for first src->dest\n");
567 	unittest(ni_get_reg_value(O(0), O(1), T) == 0,
568 		 "check trigger arg for first src->dest\n");
569 	unittest(ni_get_reg_value(O(5), O(6), T) == 5,
570 		 "check trigger arg for middle src->dest\n");
571 	unittest(ni_get_reg_value(O(8), O(9), T) == 8,
572 		 "check trigger arg for last src->dest\n");
573 }
574 
575 /* **** BEGIN simple module entry/exit functions **** */
ni_routes_unittest(void)576 static int __init ni_routes_unittest(void)
577 {
578 	static const unittest_fptr unit_tests[] = {
579 		test_ni_assign_device_routes,
580 		test_ni_sort_device_routes,
581 		test_ni_find_route_set,
582 		test_ni_route_set_has_source,
583 		test_ni_route_to_register,
584 		test_ni_lookup_route_register,
585 		test_route_is_valid,
586 		test_ni_is_cmd_dest,
587 		test_channel_is_pfi,
588 		test_channel_is_rtsi,
589 		test_ni_count_valid_routes,
590 		test_ni_get_valid_routes,
591 		test_ni_find_route_source,
592 		test_route_register_is_valid,
593 		test_ni_check_trigger_arg,
594 		test_ni_get_reg_value,
595 		NULL,
596 	};
597 
598 	exec_unittests("ni_routes", unit_tests);
599 	return 0;
600 }
601 
ni_routes_unittest_exit(void)602 static void __exit ni_routes_unittest_exit(void) { }
603 
604 module_init(ni_routes_unittest);
605 module_exit(ni_routes_unittest_exit);
606 
607 MODULE_AUTHOR("Comedi https://www.comedi.org");
608 MODULE_DESCRIPTION("Comedi unit-tests for ni_routes module");
609 MODULE_LICENSE("GPL");
610 /* **** END simple module entry/exit functions **** */
611