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:
19  * Alexey Kodanev <alexey.kodanev@oracle.com>
20  *
21  * This module is trying to load external test firmware files (n#_load_tst.fw).
22  * In the end, it writes results to /sys/devices/ltp_fw_load/result file.
23  */
24 
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/device.h>
29 #include <linux/string.h>
30 #include <linux/firmware.h>
31 
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("Alexey Kodanev <alexey.kodanev@oracle.com>");
34 MODULE_DESCRIPTION("This module is checking device firmware loading");
35 
36 #define TCID	"ltp_fw_load"
37 
38 static char *fw_name	= "load_tst.fw";
39 static int fw_size	= 0x1000;
40 static int max_name	= 64;
41 static int fw;
42 
43 module_param(fw_name, charp, 0444);
44 MODULE_PARM_DESC(fw_name, "Template firmware file name: n#_name");
45 
46 module_param(fw_size, int, 0444);
47 MODULE_PARM_DESC(fw_size, "Firmware file size");
48 
49 /*
50  * bit mask for each test-case,
51  * if test is passed, bit will be set to 1
52  */
53 static int test_result;
54 
device_release(struct device * dev)55 static void device_release(struct device *dev)
56 {
57 	pr_info(TCID ": device released\n");
58 }
59 
60 static struct device tdev = {
61 	.init_name	= TCID,
62 	.release	= device_release,
63 };
64 
65 /* read and print firmware data */
fw_read(const u8 * data,size_t size)66 static int fw_read(const u8 *data, size_t size)
67 {
68 	size_t i;
69 	pr_info(TCID ": Firmware has size '%zu'\n", size);
70 	if (size != fw_size) {
71 		pr_err(TCID ": Expected firmware size '%d'\n", fw_size);
72 		return -1;
73 	}
74 	for (i = 0; i < size; ++i) {
75 		if (data[i] != (u8)fw) {
76 			pr_err(TCID ": Unexpected firmware data\n");
77 			return -1;
78 		}
79 	}
80 	return 0;
81 }
82 
try_request_fw(const char * name)83 static int try_request_fw(const char *name)
84 {
85 	int err;
86 	const struct firmware *fw_entry = NULL;
87 	err = request_firmware(&fw_entry, name, &tdev);
88 	if (!err) {
89 		pr_info(TCID ": firmware '%s' requested\n", name);
90 		err = fw_read(fw_entry->data, fw_entry->size);
91 	} else
92 		pr_err(TCID ": Can't request firmware '%s'\n", name);
93 	release_firmware(fw_entry);
94 	return err;
95 }
96 
97 /* print test result to sysfs file */
sys_result(struct device * dev,struct device_attribute * attr,char * buf)98 static ssize_t sys_result(struct device *dev,
99 	struct device_attribute *attr, char *buf)
100 {
101 	return scnprintf(buf, PAGE_SIZE, "%d\n", test_result);
102 }
103 static DEVICE_ATTR(result, S_IRUSR, sys_result, NULL);
104 
105 /*
106  * get the number of firmware files and
107  * perform firmware requests
108  */
sys_fwnum(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)109 static ssize_t sys_fwnum(struct device *dev,
110 	struct device_attribute *attr,  const char *buf, size_t count)
111 {
112 	int err, fw_num = 0;
113 
114 	sscanf(buf, "%d", &fw_num);
115 	if (fw_num <= 0 || fw_num > 32) {
116 		pr_err(TCID ": Unexpected number of firmwares '%d'", fw_num);
117 		return count;
118 	}
119 	for (fw = 0; fw < fw_num; ++fw) {
120 		char name[max_name];
121 		snprintf(name, max_name, "n%d_%s", fw, fw_name);
122 		err = try_request_fw(name);
123 		test_result |= (err == 0) << fw;
124 	}
125 	return count;
126 }
127 static DEVICE_ATTR(fwnum, S_IWUSR, NULL, sys_fwnum);
128 
test_init(void)129 static int test_init(void)
130 {
131 	int err;
132 
133 	err = device_register(&tdev);
134 	if (err) {
135 		pr_err(TCID ": Unable to register device\n");
136 		return err;
137 	}
138 	pr_info(TCID ": device registered\n");
139 
140 	err = device_create_file(&tdev, &dev_attr_result);
141 	if (err) {
142 		pr_err(TCID ": Can't create sysfs file 'result'\n");
143 		device_unregister(&tdev);
144 		return err;
145 	}
146 	err = device_create_file(&tdev, &dev_attr_fwnum);
147 	if (err) {
148 		pr_err(TCID ": Can't create sysfs file 'fwnum'\n");
149 		device_remove_file(&tdev, &dev_attr_result);
150 		device_unregister(&tdev);
151 	}
152 	return err;
153 }
154 module_init(test_init);
155 
test_exit(void)156 static void test_exit(void)
157 {
158 	device_remove_file(&tdev, &dev_attr_result);
159 	device_remove_file(&tdev, &dev_attr_fwnum);
160 
161 	device_unregister(&tdev);
162 	pr_info(TCID ": module exited\n");
163 }
164 module_exit(test_exit);
165