xref: /aosp_15_r20/external/ltp/testcases/kernel/device-drivers/acpi/ltp_acpi.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 /*
2  * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Author: Alexey Kodanev <[email protected]>
19  */
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 
24 #include "test.h"
25 #include "old_module.h"
26 #include "safe_macros.h"
27 
28 #include "ltp_acpi.h"
29 
30 char *TCID = "ltp_acpi";
31 int TST_TOTAL = ACPI_TC_NUM;
32 
33 static const char dev_result[]	= "/sys/devices/" ACPI_TEST_NAME "/result";
34 static const char dev_path[]	= "/sys/devices/" ACPI_TEST_NAME "/path";
35 static const char dev_str[]	= "/sys/devices/" ACPI_TEST_NAME "/str";
36 static const char dev_tcase[]	= "/sys/devices/" ACPI_TEST_NAME "/tcase";
37 static const char dev_acpi_disabled[] = "/sys/devices/" ACPI_TEST_NAME "/acpi_disabled";
38 static const char module_name[]	= "ltp_acpi_cmds.ko";
39 static int module_loaded;
40 
cleanup(void)41 static void cleanup(void)
42 {
43 	if (module_loaded)
44 		tst_module_unload(NULL, module_name);
45 }
46 
read_sysfs_file(const char * name,char * buf,int size)47 static int read_sysfs_file(const char *name, char *buf, int size)
48 {
49 	FILE *f = SAFE_FOPEN(cleanup, name, "r");
50 	char *res = fgets(buf, size, f);
51 	SAFE_FCLOSE(cleanup, f);
52 	return (res) ? 0 : 1;
53 }
54 
tc_acpi_str(void)55 static int tc_acpi_str(void)
56 {
57 	int res, ret = 0;
58 	char descr[4096], sysfs_path[4096];
59 
60 	while (1) {
61 
62 		SAFE_FILE_PRINTF(cleanup, dev_tcase, "%d", ACPI_TRAVERSE);
63 		SAFE_FILE_SCANF(cleanup, dev_result, "%d", &res);
64 		if (res)
65 			return TFAIL;
66 		/*
67 		 * if device has _STR object, we should get
68 		 * a valid string from 'str' sysfs file and then can
69 		 * find it in sysfs.
70 		 */
71 		if (read_sysfs_file(dev_str, descr, 4096)) {
72 			/* None of left devices has _STR */
73 			break;
74 		}
75 		tst_resm(TINFO, "read description %s", descr);
76 
77 		/* device's sysfs path */
78 		strcpy(sysfs_path, "/sys");
79 		if (read_sysfs_file(dev_path, sysfs_path + 4, 4092)) {
80 			/*
81 			 * Device doesn't have sysfs entry
82 			 * continue, because others might have it
83 			 */
84 			continue;
85 		}
86 
87 		strcat(sysfs_path, "/description");
88 		if (access(sysfs_path, R_OK)) {
89 			tst_resm(TINFO, "can't find description file '%s'",
90 				sysfs_path);
91 			return TFAIL;
92 		}
93 		tst_resm(TINFO, "found description file '%s'", sysfs_path);
94 
95 		char sysfs_descr[4096];
96 		if (read_sysfs_file(sysfs_path, sysfs_descr, 4096))
97 			return TFAIL;
98 
99 		/*
100 		 * Compare sysfs file and description from test driver
101 		 */
102 		int res = strncmp(descr, sysfs_descr, strlen(descr));
103 
104 		ret |= res ? TFAIL : TPASS;
105 	}
106 
107 	return ret;
108 }
109 
test_run(void)110 static void test_run(void)
111 {
112 	int i, res;
113 
114 	for (i = 0; i < TST_TOTAL; ++i) {
115 
116 		if (i == ACPI_TRAVERSE) {
117 			res = tc_acpi_str();
118 		} else {
119 			SAFE_FILE_PRINTF(cleanup, dev_tcase, "%d", i);
120 			SAFE_FILE_SCANF(cleanup, dev_result, "%d", &res);
121 			res = res ? TFAIL : TPASS;
122 		}
123 
124 		tst_resm(res, "Test-case '%d'", i);
125 	}
126 }
127 
main(int argc,char * argv[])128 int main(int argc, char *argv[])
129 {
130 	int acpi_disabled;
131 
132 	tst_parse_opts(argc, argv, NULL, NULL);
133 
134 	tst_require_root();
135 
136 	tst_sig(FORK, DEF_HANDLER, cleanup);
137 
138 	tst_module_load(NULL, module_name, NULL);
139 	module_loaded = 1;
140 
141 	SAFE_FILE_SCANF(cleanup, dev_acpi_disabled, "%d", &acpi_disabled);
142 	if (acpi_disabled)
143 		tst_brkm(TCONF, cleanup, "ACPI is disabled on the system");
144 
145 	test_run();
146 
147 	cleanup();
148 
149 	tst_exit();
150 }
151