xref: /illumos-gate/usr/src/test/i2c-tests/tests/libi2c/claimed-addrs.c (revision 0cbe48189888d02563dba9c90132ac391ba233b6)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2025 Oxide Computer Company
14  */
15 
16 /*
17  * Validate that our fake at24c08 which claims additional addresses properly
18  * reports that it does. This requires that the driver be attached, which we
19  * force by opening up the corresponding eeprom file in /dev/eeprom.
20  */
21 
22 #include <stdlib.h>
23 #include <err.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <sys/debug.h>
27 #include <unistd.h>
28 
29 #include "libi2c_test_util.h"
30 
31 int
main(void)32 main(void)
33 {
34 	int ret = EXIT_SUCCESS;
35 	i2c_hdl_t *hdl = i2c_init();
36 	if (hdl == NULL) {
37 		err(EXIT_FAILURE, "INTERNAL TEST FAILURE: failed to create "
38 		    "libi2c handle");
39 	}
40 
41 	/*
42 	 * Get an initial snapshot of the device information so we can get the
43 	 * driver and instance to open up the device.
44 	 */
45 	i2c_port_t *port;
46 	i2c_dev_info_t *info;
47 
48 	if (!i2c_port_dev_init_by_path(hdl, "i2csim0/0/0x20", false, &port,
49 	    &info)) {
50 		libi2c_test_fatal(hdl, "TEST FAILED: failed to initialize "
51 		    "i2csim0/0/0x20");
52 	}
53 
54 	char path[PATH_MAX];
55 	(void) snprintf(path, sizeof (path), "/dev/eeprom/%s/%d/eeprom",
56 	    i2c_device_info_driver(info), i2c_device_info_instance(info));
57 	int fd = open(path, O_RDONLY);
58 	if (fd < 0) {
59 		err(EXIT_FAILURE, "TEST FAILED: failed to open EEPROM %s",
60 		    path);
61 	}
62 
63 	i2c_device_info_free(info);
64 	i2c_port_fini(port);
65 
66 	/*
67 	 * Take a fresh snapshot of the device information now that we have the
68 	 * device open we're guaranteed the driver is attached and has claimed
69 	 * all of its addresses.
70 	 */
71 	if (!i2c_port_dev_init_by_path(hdl, "i2csim0/0/0x20", false, &port,
72 	    &info)) {
73 		libi2c_test_fatal(hdl, "TEST FAILED: failed to snapshot "
74 		    "i2csim0/0/0x20");
75 	}
76 
77 	if (i2c_device_info_naddrs(info) != 4) {
78 		warnx("TEST FAILED: expected 4 addresses, but found %u",
79 		    i2c_device_info_naddrs(info));
80 		ret = EXIT_FAILURE;
81 	} else {
82 		(void) printf("TEST PASSED: successfully found 4 at24c08 "
83 		    "addresses\n");
84 	}
85 
86 	if (i2c_device_info_addr_source(info, 0) != I2C_ADDR_SOURCE_REG) {
87 		warnx("TEST FAILED: at24c08 address 0 has wrong source 0x%x, "
88 		    "expected reg (0x%x)", i2c_device_info_addr_source(info, 0),
89 		    I2C_ADDR_SOURCE_REG);
90 		ret = EXIT_FAILURE;
91 	} else {
92 		(void) printf("TEST PASSED: at24c08 address 0 correctly noted "
93 		    "as from reg[]\n");
94 	}
95 
96 	for (uint32_t i = 1; i < i2c_device_info_naddrs(info); i++) {
97 		if (i2c_device_info_addr_source(info, i) !=
98 		    I2C_ADDR_SOURCE_CLAIMED) {
99 			warnx("TEST FAILED: at24c08 address %u has wrong "
100 			    "source 0x%x, expected claimed (0x%x)", i,
101 			    i2c_device_info_addr_source(info, i),
102 			    I2C_ADDR_SOURCE_CLAIMED);
103 			ret = EXIT_FAILURE;
104 		} else {
105 			(void) printf("TEST PASSED: at24c08 address %u "
106 			    "correctly noted as claimed\n");
107 		}
108 	}
109 
110 	if (ret == EXIT_SUCCESS) {
111 		(void) printf("All tests passed successfully\n");
112 	}
113 	VERIFY0(close(fd));
114 	i2c_device_info_free(info);
115 	i2c_port_fini(port);
116 	i2c_fini(hdl);
117 	return (ret);
118 }
119