Home | History | Annotate | Line # | Download | only in umcpmioctl
      1 /*	$NetBSD: printumcpmio.c,v 1.3 2025/03/22 06:09:48 rillig Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2024 Brad Spencer <brad (at) 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 #include <sys/cdefs.h>
     20 #ifdef __RCSID
     21 __RCSID("$NetBSD: printumcpmio.c,v 1.3 2025/03/22 06:09:48 rillig Exp $");
     22 #endif
     23 
     24 /* Functions to print stuff */
     25 
     26 #include <stdio.h>
     27 
     28 #include <dev/usb/umcpmio_hid_reports.h>
     29 
     30 #undef EXTERN
     31 #define EXTERN
     32 #include "printumcpmio.h"
     33 
     34 /* This is all cheaply done */
     35 
     36 void
     37 print_status(struct mcp2221_status_res *r)
     38 {
     39 	uint8_t *br = (uint8_t *)r;
     40 
     41 	const char *outputs[] = {
     42 		"cmd:\t\t\t\t",
     43 		"completion:\t\t\t",
     44 		"cancel_transfer:\t\t",
     45 		"set_i2c_speed:\t\t",
     46 		"i2c_clock_divider:\t\t",
     47 		NULL,
     48 		NULL,
     49 		NULL,
     50 		"internal_i2c_state:\t\t",
     51 		"lsb_i2c_req_len:\t\t",
     52 		"msb_i2c_req_len:\t\t",
     53 		"lsb_i2c_trans_len:\t\t",
     54 		"msb_i2c_trans_len:\t\t",
     55 		"internal_i2c_bcount:\t\t",
     56 		"i2c_speed_divider:\t\t",
     57 		"i2c_timeout_value:\t\t",
     58 		"lsb_i2c_address:\t\t",
     59 		"msb_i2c_address:\t\t",
     60 		NULL,
     61 		NULL,
     62 		"internal_i2c_state20:\t",
     63 		NULL,
     64 		"scl_line_value:\t\t",
     65 		"sda_line_value:\t\t",
     66 		"interrupt_edge_state:\t",
     67 		"i2c_read_pending:\t\t",
     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 		NULL,
     88 		"mcp2221_hardware_rev_major:\t",
     89 		"mcp2221_hardware_rev_minor:\t",
     90 		"mcp2221_firmware_rev_major:\t",
     91 		"mcp2221_firmware_rev_minor:\t",
     92 		"adc_channel0_lsb:\t\t",
     93 		"adc_channel0_msb:\t\t",
     94 		"adc_channel1_lsb:\t\t",
     95 		"adc_channel1_msb:\t\t",
     96 		"adc_channel2_lsb:\t\t",
     97 		"adc_channel2_msb:\t\t",
     98 		NULL,
     99 		NULL,
    100 		NULL,
    101 		NULL,
    102 		NULL,
    103 		NULL,
    104 		NULL,
    105 		NULL
    106 	};
    107 
    108 	for (int n = 0; n < MCP2221_RES_BUFFER_SIZE; n++) {
    109 		if (outputs[n] != NULL)
    110 			printf("%02d:%s%d (0x%02X)\n", n, outputs[n], br[n], br[n]);
    111 	}
    112 }
    113 
    114 void
    115 print_sram(struct mcp2221_get_sram_res *r)
    116 {
    117 	uint8_t *br = (uint8_t *)r;
    118 
    119 	const char *outputs[] = {
    120 		"cmd:\t\t\t\t",
    121 		"completion:\t\t\t",
    122 		"len_chip_setting:\t\t",
    123 		"len_gpio_setting:\t\t",
    124 		"sn_initial_ps_cs:\t\t",
    125 		"clock_divider:\t\t",
    126 		"dac_reference_voltage:\t",
    127 		"irq_adc_reference_voltage:\t",
    128 		"lsb_usb_vid:\t\t\t",
    129 		"msb_usb_vid:\t\t\t",
    130 		"lsb_usb_pid:\t\t\t",
    131 		"msb_usb_pid:\t\t\t",
    132 		"usb_power_attributes:\t",
    133 		"usb_requested_ma:\t\t",
    134 		"current_password_byte_1:\t",
    135 		"current_password_byte_2:\t",
    136 		"current_password_byte_3:\t",
    137 		"current_password_byte_4:\t",
    138 		"current_password_byte_5:\t",
    139 		"current_password_byte_6:\t",
    140 		"current_password_byte_7:\t",
    141 		"current_password_byte_8:\t",
    142 		"gp0_settings:\t\t",
    143 		"gp1_settings:\t\t",
    144 		"gp2_settings:\t\t",
    145 		"gp3_settings:\t\t",
    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 		NULL
    184 	};
    185 
    186 	for (int n = 0; n < MCP2221_RES_BUFFER_SIZE; n++) {
    187 		if (outputs[n] != NULL)
    188 			printf("%02d:%s%d (0x%02X)\n", n, outputs[n], br[n], br[n]);
    189 	}
    190 }
    191 
    192 void
    193 print_gpio_cfg(struct mcp2221_get_gpio_cfg_res *r)
    194 {
    195 	uint8_t *br = (uint8_t *)r;
    196 
    197 	const char *outputs[] = {
    198 		"cmd:\t\t\t",
    199 		"completion:\t\t",
    200 		"gp0_pin_value:\t",
    201 		"gp0_pin_dir:\t\t",
    202 		"gp1_pin_value:\t",
    203 		"gp1_pin_dir:\t\t",
    204 		"gp2_pin_value:\t",
    205 		"gp2_pin_dir:\t\t",
    206 		"gp3_pin_value:\t",
    207 		"gp3_pin_dir:\t\t",
    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 		NULL
    262 	};
    263 
    264 	for (int n = 0; n < MCP2221_RES_BUFFER_SIZE; n++) {
    265 		if (outputs[n] != NULL)
    266 			printf("%02d:%s%d (0x%02X)\n", n, outputs[n], br[n], br[n]);
    267 	}
    268 }
    269 
    270 void
    271 print_flash(struct mcp2221_get_flash_res *r, int subcode)
    272 {
    273 	uint8_t *br = (uint8_t *)r;
    274 
    275 	const char *outputs1[] = {
    276 		"cmd:\t\t\t\t",
    277 		"completion:\t\t\t",
    278 		"res_len:\t\t\t"
    279 	};
    280 
    281 	const char *outputs2[][64] = {
    282 		{
    283 			NULL,
    284 			"uartenum_led_protection:\t",
    285 			"clock_divider:\t\t",
    286 			"dac_reference_voltage:\t",
    287 			"irq_adc_reference_voltage:\t",
    288 			"lsb_usb_vid:\t\t\t",
    289 			"msb_usb_vid:\t\t\t",
    290 			"lsb_usb_pid:\t\t\t",
    291 			"msb_usb_pid:\t\t\t",
    292 			"usb_power_attributes:\t",
    293 			"usb_requested_ma:\t\t",
    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 			NULL
    344 		},
    345 		{
    346 			NULL,
    347 			"gp0_settings:\t\t",
    348 			"gp1_settings:\t\t",
    349 			"gp2_settings:\t\t",
    350 			"gp3_settings:\t\t",
    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 			NULL
    407 		},
    408 		{
    409 			"always0x03:\t\t\t\t",
    410 			"unicode_man_descriptor"
    411 		},
    412 		{
    413 			"always0x03:\t\t\t\t\t",
    414 			"unicode_product_descriptor"
    415 		},
    416 		{
    417 			"always0x03:\t\t\t\t",
    418 			"unicode_serial_number"
    419 		},
    420 		{
    421 			"always0x03:\t\t\t\t",
    422 			"factory_serial_number"
    423 		},
    424 	};
    425 
    426 	int n = 0;
    427 	for (; n <= 2; n++) {
    428 		if (outputs1[n] != NULL)
    429 			printf("%02d:%s%d (0x%02X)\n", n, outputs1[n], br[n], br[n]);
    430 	}
    431 
    432 	if (subcode == 0 ||
    433 	    subcode == 1) {
    434 		for (; n < MCP2221_RES_BUFFER_SIZE; n++) {
    435 			if (outputs2[subcode][n - 3] != NULL)
    436 				printf("%02d:%s%d (0x%02X)\n", n, outputs2[subcode][n - 3], br[n], br[n]);
    437 		}
    438 	} else {
    439 		int c = 1;
    440 		int l = br[2];
    441 		printf("%02d:%s%d (0x%02X)\n", n, outputs2[subcode][n - 3], br[n], br[n]);
    442 		n++;
    443 		for (c = 1; c <= l; c++) {
    444 			printf("%02d:%s%02d:\t\t%d (0x%02X)\n", n, outputs2[subcode][1], c, br[n], br[n]);
    445 			n++;
    446 		}
    447 	}
    448 }
    449