xref: /linux/drivers/comedi/drivers/ni_routing/tools/convert_c_to_py.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1 // SPDX-License-Identifier: GPL-2.0+
2 
3 #include <stdint.h>
4 #include <stdbool.h>
5 #include <stddef.h>
6 #include <errno.h>
7 #include <stdlib.h>
8 
9 typedef uint8_t u8;
10 typedef uint16_t u16;
11 typedef int8_t  s8;
12 #define __user
13 #define BIT(x)  (1UL << (x))
14 
15 #define NI_ROUTE_VALUE_EXTERNAL_CONVERSION 1
16 
17 #include "../ni_route_values.c"
18 #include "../ni_device_routes.c"
19 #include "all_cfiles.c"
20 
21 #include <stdio.h>
22 
23 #define RVij(rv, src, dest)	((rv)->register_values[(dest)][(src)])
24 
25 /*
26  * write out
27  * {
28  *   "family" : "<family-name>",
29  *   "register_values": {
30  *      <destination0>:[src0, src1, ...],
31  *      <destination0>:[src0, src1, ...],
32  *      ...
33  *   }
34  * }
35  */
family_write(const struct family_route_values * rv,FILE * fp)36 void family_write(const struct family_route_values *rv, FILE *fp)
37 {
38 	fprintf(fp,
39 		"  \"%s\" : {\n"
40 		"    # dest -> {src0:val0, src1:val1, ...}\n"
41 		, rv->family);
42 	for (unsigned int dest = NI_NAMES_BASE;
43 	     dest < (NI_NAMES_BASE + NI_NUM_NAMES);
44 	     ++dest) {
45 		unsigned int src = NI_NAMES_BASE;
46 
47 		for (; src < (NI_NAMES_BASE + NI_NUM_NAMES) &&
48 		     RVij(rv, B(src), B(dest)) == 0; ++src)
49 			;
50 
51 		if (src >= (NI_NAMES_BASE + NI_NUM_NAMES))
52 			continue; /* no data here */
53 
54 		fprintf(fp, "    %u : {\n", dest);
55 		for (src = NI_NAMES_BASE; src < (NI_NAMES_BASE + NI_NUM_NAMES);
56 		     ++src) {
57 			register_type r = RVij(rv, B(src), B(dest));
58 			const char *M;
59 
60 			if (r == 0) {
61 				continue;
62 			} else if (MARKED_V(r)) {
63 				M = "V";
64 			} else if (MARKED_I(r)) {
65 				M = "I";
66 			} else if (MARKED_U(r)) {
67 				M = "U";
68 			} else {
69 				fprintf(stderr,
70 					"Invalid register marking %s[%u][%u] = %u\n",
71 					rv->family, dest, src, r);
72 				exit(1);
73 			}
74 
75 			fprintf(fp, "      %u : \"%s(%u)\",\n",
76 				src, M, UNMARK(r));
77 		}
78 		fprintf(fp, "    },\n");
79 	}
80 	fprintf(fp, "  },\n\n");
81 }
82 
is_valid_ni_sig(unsigned int sig)83 bool is_valid_ni_sig(unsigned int sig)
84 {
85 	return (sig >= NI_NAMES_BASE) && (sig < (NI_NAMES_BASE + NI_NUM_NAMES));
86 }
87 
88 /*
89  * write out
90  * {
91  *   "family" : "<family-name>",
92  *   "register_values": {
93  *      <destination0>:[src0, src1, ...],
94  *      <destination0>:[src0, src1, ...],
95  *      ...
96  *   }
97  * }
98  */
device_write(const struct ni_device_routes * dR,FILE * fp)99 void device_write(const struct ni_device_routes *dR, FILE *fp)
100 {
101 	fprintf(fp,
102 		"  \"%s\" : {\n"
103 		"    # dest -> [src0, src1, ...]\n"
104 		, dR->device);
105 
106 	unsigned int i = 0;
107 
108 	while (dR->routes[i].dest != 0) {
109 		if (!is_valid_ni_sig(dR->routes[i].dest)) {
110 			fprintf(stderr,
111 				"Invalid NI signal value [%u] for destination %s.[%u]\n",
112 				dR->routes[i].dest, dR->device, i);
113 			exit(1);
114 		}
115 
116 		fprintf(fp, "    %u : [", dR->routes[i].dest);
117 
118 		unsigned int j = 0;
119 
120 		while (dR->routes[i].src[j] != 0) {
121 			if (!is_valid_ni_sig(dR->routes[i].src[j])) {
122 				fprintf(stderr,
123 					"Invalid NI signal value [%u] for source %s.[%u].[%u]\n",
124 					dR->routes[i].src[j], dR->device, i, j);
125 				exit(1);
126 			}
127 
128 			fprintf(fp, "%u,", dR->routes[i].src[j]);
129 
130 			++j;
131 		}
132 		fprintf(fp, "],\n");
133 
134 		++i;
135 	}
136 	fprintf(fp, "  },\n\n");
137 }
138 
main(void)139 int main(void)
140 {
141 	FILE *fp = fopen("ni_values.py", "w");
142 
143 	if (fp == NULL) {
144 		fprintf(stderr, "Could not open file!");
145 		return -1;
146 	}
147 
148 	/* write route register values */
149 	fprintf(fp, "ni_route_values = {\n");
150 	for (int i = 0; ni_all_route_values[i]; ++i)
151 		family_write(ni_all_route_values[i], fp);
152 	fprintf(fp, "}\n\n");
153 
154 	/* write valid device routes */
155 	fprintf(fp, "ni_device_routes = {\n");
156 	for (int i = 0; ni_device_routes_list[i]; ++i)
157 		device_write(ni_device_routes_list[i], fp);
158 	fprintf(fp, "}\n");
159 
160 	/* finish; close file */
161 	fclose(fp);
162 	return 0;
163 }
164