xref: /netbsd-src/usr.sbin/umcpmioctl/printumcpmio.c (revision 3bfaa97146632a7fff2425e11c47d4964bacef79)
1 /*	$NetBSD: printumcpmio.c,v 1.1 2024/12/16 16:37:40 brad Exp $	*/
2 
3 /*
4  * Copyright (c) 2024 Brad Spencer <brad@anduin.eldar.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifdef __RCSID
20 __RCSID("$NetBSD: printumcpmio.c,v 1.1 2024/12/16 16:37:40 brad Exp $");
21 #endif
22 
23 /* Functions to print stuff */
24 
25 #include <stdio.h>
26 
27 #include <dev/usb/umcpmio_hid_reports.h>
28 
29 #undef EXTERN
30 #define EXTERN
31 #include "printumcpmio.h"
32 
33 /* This is all cheaply done */
34 
35 void
36 print_status(struct mcp2221_status_res *r)
37 {
38 	uint8_t *br = (uint8_t *)r;
39 
40 	const char *outputs[] = {
41 		"cmd:\t\t\t\t",
42 		"completion:\t\t\t",
43 		"cancel_transfer:\t\t",
44 		"set_i2c_speed:\t\t",
45 		"i2c_clock_divider:\t\t",
46 		NULL,
47 		NULL,
48 		NULL,
49 		"internal_i2c_state:\t\t",
50 		"lsb_i2c_req_len:\t\t",
51 		"msb_i2c_req_len:\t\t",
52 		"lsb_i2c_trans_len:\t\t",
53 		"msb_i2c_trans_len:\t\t",
54 		"internal_i2c_bcount:\t\t",
55 		"i2c_speed_divider:\t\t",
56 		"i2c_timeout_value:\t\t",
57 		"lsb_i2c_address:\t\t",
58 		"msb_i2c_address:\t\t",
59 		NULL,
60 		NULL,
61 		"internal_i2c_state20:\t",
62 		NULL,
63 		"scl_line_value:\t\t",
64 		"sda_line_value:\t\t",
65 		"interrupt_edge_state:\t",
66 		"i2c_read_pending:\t\t",
67 		NULL,
68 		NULL,
69 		NULL,
70 		NULL,
71 		NULL,
72 		NULL,
73 		NULL,
74 		NULL,
75 		NULL,
76 		NULL,
77 		NULL,
78 		NULL,
79 		NULL,
80 		NULL,
81 		NULL,
82 		NULL,
83 		NULL,
84 		NULL,
85 		NULL,
86 		NULL,
87 		"mcp2221_hardware_rev_major:\t",
88 		"mcp2221_hardware_rev_minor:\t",
89 		"mcp2221_firmware_rev_major:\t",
90 		"mcp2221_firmware_rev_minor:\t",
91 		"adc_channel0_lsb:\t\t",
92 		"adc_channel0_msb:\t\t",
93 		"adc_channel1_lsb:\t\t",
94 		"adc_channel1_msb:\t\t",
95 		"adc_channel2_lsb:\t\t",
96 		"adc_channel2_msb:\t\t",
97 		NULL,
98 		NULL,
99 		NULL,
100 		NULL,
101 		NULL,
102 		NULL,
103 		NULL,
104 		NULL
105 	};
106 
107 	for(int n = 0; n < MCP2221_RES_BUFFER_SIZE;n++) {
108 		if (outputs[n] != NULL)
109 			printf("%02d:%s%d (0x%02X)\n",n,outputs[n],br[n],br[n]);
110 	}
111 }
112 
113 void
114 print_sram(struct mcp2221_get_sram_res *r)
115 {
116 	uint8_t *br = (uint8_t *)r;
117 
118 	const char *outputs[] = {
119 		"cmd:\t\t\t\t",
120 		"completion:\t\t\t",
121 		"len_chip_setting:\t\t",
122 		"len_gpio_setting:\t\t",
123 		"sn_initial_ps_cs:\t\t",
124 		"clock_divider:\t\t",
125 		"dac_reference_voltage:\t",
126 		"irq_adc_reference_voltage:\t",
127 		"lsb_usb_vid:\t\t\t",
128 		"msb_usb_vid:\t\t\t",
129 		"lsb_usb_pid:\t\t\t",
130 		"msb_usb_pid:\t\t\t",
131 		"usb_power_attributes:\t",
132 		"usb_requested_ma:\t\t",
133 		"current_password_byte_1:\t",
134 		"current_password_byte_2:\t",
135 		"current_password_byte_3:\t",
136 		"current_password_byte_4:\t",
137 		"current_password_byte_5:\t",
138 		"current_password_byte_6:\t",
139 		"current_password_byte_7:\t",
140 		"current_password_byte_8:\t",
141 		"gp0_settings:\t\t",
142 		"gp1_settings:\t\t",
143 		"gp2_settings:\t\t",
144 		"gp3_settings:\t\t",
145 		NULL,
146 		NULL,
147 		NULL,
148 		NULL,
149 		NULL,
150 		NULL,
151 		NULL,
152 		NULL,
153 		NULL,
154 		NULL,
155 		NULL,
156 		NULL,
157 		NULL,
158 		NULL,
159 		NULL,
160 		NULL,
161 		NULL,
162 		NULL,
163 		NULL,
164 		NULL,
165 		NULL,
166 		NULL,
167 		NULL,
168 		NULL,
169 		NULL,
170 		NULL,
171 		NULL,
172 		NULL,
173 		NULL,
174 		NULL,
175 		NULL,
176 		NULL,
177 		NULL,
178 		NULL,
179 		NULL,
180 		NULL,
181 		NULL,
182 		NULL
183 	};
184 
185 	for(int n = 0; n < MCP2221_RES_BUFFER_SIZE;n++) {
186 		if (outputs[n] != NULL)
187 			printf("%02d:%s%d (0x%02X)\n",n,outputs[n],br[n],br[n]);
188 	}
189 }
190 
191 void
192 print_gpio_cfg(struct mcp2221_get_gpio_cfg_res *r)
193 {
194 	uint8_t *br = (uint8_t *)r;
195 
196 	const char *outputs[] = {
197 		"cmd:\t\t\t",
198 		"completion:\t\t",
199 		"gp0_pin_value:\t",
200 		"gp0_pin_dir:\t\t",
201 		"gp1_pin_value:\t",
202 		"gp1_pin_dir:\t\t",
203 		"gp2_pin_value:\t",
204 		"gp2_pin_dir:\t\t",
205 		"gp3_pin_value:\t",
206 		"gp3_pin_dir:\t\t",
207 		NULL,
208 		NULL,
209 		NULL,
210 		NULL,
211 		NULL,
212 		NULL,
213 		NULL,
214 		NULL,
215 		NULL,
216 		NULL,
217 		NULL,
218 		NULL,
219 		NULL,
220 		NULL,
221 		NULL,
222 		NULL,
223 		NULL,
224 		NULL,
225 		NULL,
226 		NULL,
227 		NULL,
228 		NULL,
229 		NULL,
230 		NULL,
231 		NULL,
232 		NULL,
233 		NULL,
234 		NULL,
235 		NULL,
236 		NULL,
237 		NULL,
238 		NULL,
239 		NULL,
240 		NULL,
241 		NULL,
242 		NULL,
243 		NULL,
244 		NULL,
245 		NULL,
246 		NULL,
247 		NULL,
248 		NULL,
249 		NULL,
250 		NULL,
251 		NULL,
252 		NULL,
253 		NULL,
254 		NULL,
255 		NULL,
256 		NULL,
257 		NULL,
258 		NULL,
259 		NULL,
260 		NULL
261 	};
262 
263 	for(int n = 0; n < MCP2221_RES_BUFFER_SIZE;n++) {
264 		if (outputs[n] != NULL)
265 			printf("%02d:%s%d (0x%02X)\n",n,outputs[n],br[n],br[n]);
266 	}
267 }
268 
269 void
270 print_flash(struct mcp2221_get_flash_res *r, int subcode)
271 {
272 	uint8_t *br = (uint8_t *)r;
273 
274 	const char *outputs1[] = {
275 		"cmd:\t\t\t\t",
276 		"completion:\t\t\t",
277 		"res_len:\t\t\t"
278 	};
279 
280 	const char *outputs2[][64] = {
281 		{
282 			NULL,
283 			"uartenum_led_protection:\t",
284 			"clock_divider:\t\t",
285 			"dac_reference_voltage:\t",
286 			"irq_adc_reference_voltage:\t",
287 			"lsb_usb_vid:\t\t\t",
288 			"msb_usb_vid:\t\t\t",
289 			"lsb_usb_pid:\t\t\t",
290 			"msb_usb_pid:\t\t\t",
291 			"usb_power_attributes:\t",
292 			"usb_requested_ma:\t\t",
293 			NULL,
294 			NULL,
295 			NULL,
296 			NULL,
297 			NULL,
298 			NULL,
299 			NULL,
300 			NULL,
301 			NULL,
302 			NULL,
303 			NULL,
304 			NULL,
305 			NULL,
306 			NULL,
307 			NULL,
308 			NULL,
309 			NULL,
310 			NULL,
311 			NULL,
312 			NULL,
313 			NULL,
314 			NULL,
315 			NULL,
316 			NULL,
317 			NULL,
318 			NULL,
319 			NULL,
320 			NULL,
321 			NULL,
322 			NULL,
323 			NULL,
324 			NULL,
325 			NULL,
326 			NULL,
327 			NULL,
328 			NULL,
329 			NULL,
330 			NULL,
331 			NULL,
332 			NULL,
333 			NULL,
334 			NULL,
335 			NULL,
336 			NULL,
337 			NULL,
338 			NULL,
339 			NULL,
340 			NULL,
341 			NULL,
342 			NULL
343 		},
344 		{
345 			NULL,
346 			"gp0_settings:\t\t",
347 			"gp1_settings:\t\t",
348 			"gp2_settings:\t\t",
349 			"gp3_settings:\t\t",
350 			NULL,
351 			NULL,
352 			NULL,
353 			NULL,
354 			NULL,
355 			NULL,
356 			NULL,
357 			NULL,
358 			NULL,
359 			NULL,
360 			NULL,
361 			NULL,
362 			NULL,
363 			NULL,
364 			NULL,
365 			NULL,
366 			NULL,
367 			NULL,
368 			NULL,
369 			NULL,
370 			NULL,
371 			NULL,
372 			NULL,
373 			NULL,
374 			NULL,
375 			NULL,
376 			NULL,
377 			NULL,
378 			NULL,
379 			NULL,
380 			NULL,
381 			NULL,
382 			NULL,
383 			NULL,
384 			NULL,
385 			NULL,
386 			NULL,
387 			NULL,
388 			NULL,
389 			NULL,
390 			NULL,
391 			NULL,
392 			NULL,
393 			NULL,
394 			NULL,
395 			NULL,
396 			NULL,
397 			NULL,
398 			NULL,
399 			NULL,
400 			NULL,
401 			NULL,
402 			NULL,
403 			NULL,
404 			NULL,
405 			NULL
406 		},
407 		{
408 			"always0x03:\t\t\t\t",
409 			"unicode_man_descriptor"
410 		},
411 		{
412 			"always0x03:\t\t\t\t\t",
413 			"unicode_product_descriptor"
414 		},
415 		{
416 			"always0x03:\t\t\t\t",
417 			"unicode_serial_number"
418 		},
419 		{
420 			"always0x03:\t\t\t\t",
421 			"factory_serial_number"
422 		},
423 	};
424 
425 	int n = 0;
426 	for(;n <= 2;n++) {
427 		if (outputs1[n] != NULL)
428 			printf("%02d:%s%d (0x%02X)\n",n,outputs1[n],br[n],br[n]);
429 	}
430 
431 	if (subcode == 0 ||
432 	    subcode == 1) {
433 		for(;n < MCP2221_RES_BUFFER_SIZE;n++) {
434 			if (outputs2[subcode][n-3] != NULL)
435 				printf("%02d:%s%d (0x%02X)\n",n,outputs2[subcode][n-3],br[n],br[n]);
436 		}
437 	} else {
438 		int c = 1;
439 		int l = br[2];
440 		printf("%02d:%s%d (0x%02X)\n",n,outputs2[subcode][n-3],br[n],br[n]);
441 		n++;
442 		for(c = 1;c <= l;c++) {
443 			printf("%02d:%s%02d:\t\t%d (0x%02X)\n",n,outputs2[subcode][1],c,br[n],br[n]);
444 			n++;
445 		}
446 	}
447 }
448