xref: /linux/drivers/firewire/device-attribute-test.c (revision 949268ec542aac728e0284633a202983f8ec340e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // device-attribute-test.c - An application of Kunit to test implementation for device attributes.
4 //
5 // Copyright (c) 2023 Takashi Sakamoto
6 //
7 // This file can not be built independently since it is intentionally included in core-device.c.
8 
9 #include <kunit/test.h>
10 
11 // Configuration ROM for AV/C Devices 1.0 (Dec. 12, 2000, 1394 Trading Association)
12 // Annex C:Configuration ROM example(informative)
13 // C.1 Simple AV/C device
14 //
15 // Copied from the documentation.
16 static const u32 simple_avc_config_rom[] = {
17 	0x0404eabf,
18 	0x31333934,
19 	0xe0646102,
20 	0xffffffff,
21 	0xffffffff,
22 	0x00063287, // root directory.
23 	0x03ffffff,
24 	0x8100000a,
25 	0x17ffffff,
26 	0x8100000e,
27 	0x0c0083c0,
28 	0xd1000001,
29 	0x0004442d, // unit 0 directory.
30 	0x1200a02d,
31 	0x13010001,
32 	0x17ffffff,
33 	0x81000007,
34 	0x0005c915, // leaf for textual descriptor.
35 	0x00000000,
36 	0x00000000,
37 	0x56656e64,
38 	0x6f72204e,
39 	0x616d6500,
40 	0x00057f16, // leaf for textual descriptor.
41 	0x00000000,
42 	0x00000000,
43 	0x4d6f6465,
44 	0x6c204e61,
45 	0x6d650000,
46 };
47 
48 // Ibid.
49 // Annex A:Consideration for configuration ROM reader design (informative)
50 // A.1 Vendor directory
51 //
52 // Written by hand.
53 static const u32 legacy_avc_config_rom[] = {
54 	0x04199fe7,
55 	0x31333934,
56 	0xe0644000,
57 	0x00112233,
58 	0x44556677,
59 	0x0005dace, // root directory.
60 	0x03012345,
61 	0x0c0083c0,
62 	0x8d000009,
63 	0xd1000002,
64 	0xc3000004,
65 	0x0002e107, // unit 0 directory.
66 	0x12abcdef,
67 	0x13543210,
68 	0x0002cb73, // vendor directory.
69 	0x17fedcba,
70 	0x81000004,
71 	0x00026dc1, // leaf for EUI-64.
72 	0x00112233,
73 	0x44556677,
74 	0x00050e84, // leaf for textual descriptor.
75 	0x00000000,
76 	0x00000000,
77 	0x41424344,
78 	0x45464748,
79 	0x494a0000,
80 };
81 
device_attr_simple_avc(struct kunit * test)82 static void device_attr_simple_avc(struct kunit *test)
83 {
84 	static const struct fw_device node = {
85 		.device = {
86 			.type = &fw_device_type,
87 		},
88 		.config_rom = simple_avc_config_rom,
89 		.config_rom_length = sizeof(simple_avc_config_rom),
90 	};
91 	static const struct fw_unit unit0 = {
92 		.device = {
93 			.type = &fw_unit_type,
94 			.parent = (struct device *)&node.device,
95 		},
96 		.directory = &simple_avc_config_rom[12],
97 	};
98 	struct device *node_dev = (struct device *)&node.device;
99 	struct device *unit0_dev = (struct device *)&unit0.device;
100 	static const int unit0_expected_ids[] = {0x00ffffff, 0x00ffffff, 0x0000a02d, 0x00010001};
101 	char *buf = kunit_kzalloc(test, PAGE_SIZE, GFP_KERNEL);
102 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf);
103 	int ids[4] = {0, 0, 0, 0};
104 
105 	// Ensure associations for node and unit devices.
106 
107 	KUNIT_ASSERT_TRUE(test, is_fw_device(node_dev));
108 	KUNIT_ASSERT_FALSE(test, is_fw_unit(node_dev));
109 	KUNIT_ASSERT_PTR_EQ(test, fw_device(node_dev), &node);
110 
111 	KUNIT_ASSERT_FALSE(test, is_fw_device(unit0_dev));
112 	KUNIT_ASSERT_TRUE(test, is_fw_unit(unit0_dev));
113 	KUNIT_ASSERT_PTR_EQ(test, fw_parent_device((&unit0)), &node);
114 	KUNIT_ASSERT_PTR_EQ(test, fw_unit(unit0_dev), &unit0);
115 
116 	// For entries in root directory.
117 
118 	// Vendor immediate entry is found.
119 	KUNIT_EXPECT_GT(test, show_immediate(node_dev, &config_rom_attributes[0].attr, buf), 0);
120 	KUNIT_EXPECT_STREQ(test, buf, "0xffffff\n");
121 
122 	// Model immediate entry is found.
123 	KUNIT_EXPECT_GT(test, show_immediate(node_dev, &config_rom_attributes[4].attr, buf), 0);
124 	KUNIT_EXPECT_STREQ(test, buf, "0xffffff\n");
125 
126 	// Descriptor leaf entry for vendor is found.
127 	KUNIT_EXPECT_GT(test, show_text_leaf(node_dev, &config_rom_attributes[5].attr, buf), 0);
128 	KUNIT_EXPECT_STREQ(test, buf, "Vendor Name\n");
129 
130 	// Descriptor leaf entry for model is found.
131 	KUNIT_EXPECT_GT(test, show_text_leaf(node_dev, &config_rom_attributes[6].attr, buf), 0);
132 	KUNIT_EXPECT_STREQ(test, buf, "Model Name\n");
133 
134 	// For entries in unit 0 directory.
135 
136 	// Vendor immediate entry is not found.
137 	KUNIT_EXPECT_LT(test, show_immediate(unit0_dev, &config_rom_attributes[0].attr, buf), 0);
138 
139 	// Model immediate entry is found.
140 	KUNIT_EXPECT_GT(test, show_immediate(unit0_dev, &config_rom_attributes[4].attr, buf), 0);
141 	KUNIT_EXPECT_STREQ(test, buf, "0xffffff\n");
142 
143 	// Descriptor leaf entry for vendor is not found.
144 	KUNIT_EXPECT_LT(test, show_text_leaf(unit0_dev, &config_rom_attributes[5].attr, buf), 0);
145 
146 	// Descriptor leaf entry for model is found.
147 	KUNIT_EXPECT_GT(test, show_text_leaf(unit0_dev, &config_rom_attributes[6].attr, buf), 0);
148 	KUNIT_EXPECT_STREQ(test, buf, "Model Name\n");
149 
150 	// Specifier_ID immediate entry is found.
151 	KUNIT_EXPECT_GT(test, show_immediate(unit0_dev, &config_rom_attributes[2].attr, buf), 0);
152 	KUNIT_EXPECT_STREQ(test, buf, "0x00a02d\n");
153 
154 	// Version immediate entry is found.
155 	KUNIT_EXPECT_GT(test, show_immediate(unit0_dev, &config_rom_attributes[3].attr, buf), 0);
156 	KUNIT_EXPECT_STREQ(test, buf, "0x010001\n");
157 
158 	kunit_kfree(test, buf);
159 
160 	get_modalias_ids(&unit0, ids);
161 	KUNIT_EXPECT_MEMEQ(test, ids, unit0_expected_ids, sizeof(ids));
162 }
163 
device_attr_legacy_avc(struct kunit * test)164 static void device_attr_legacy_avc(struct kunit *test)
165 {
166 	static const struct fw_device node = {
167 		.device = {
168 			.type = &fw_device_type,
169 		},
170 		.config_rom = legacy_avc_config_rom,
171 		.config_rom_length = sizeof(legacy_avc_config_rom),
172 	};
173 	static const struct fw_unit unit0 = {
174 		.device = {
175 			.type = &fw_unit_type,
176 			.parent = (struct device *)&node.device,
177 		},
178 		.directory = &legacy_avc_config_rom[11],
179 	};
180 	struct device *node_dev = (struct device *)&node.device;
181 	struct device *unit0_dev = (struct device *)&unit0.device;
182 	static const int unit0_expected_ids[] = {0x00012345, 0x00fedcba, 0x00abcdef, 0x00543210};
183 	char *buf = kunit_kzalloc(test, PAGE_SIZE, GFP_KERNEL);
184 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf);
185 	int ids[4] = {0, 0, 0, 0};
186 
187 	// Ensure associations for node and unit devices.
188 
189 	KUNIT_ASSERT_TRUE(test, is_fw_device(node_dev));
190 	KUNIT_ASSERT_FALSE(test, is_fw_unit(node_dev));
191 	KUNIT_ASSERT_PTR_EQ(test, fw_device((node_dev)), &node);
192 
193 	KUNIT_ASSERT_FALSE(test, is_fw_device(unit0_dev));
194 	KUNIT_ASSERT_TRUE(test, is_fw_unit(unit0_dev));
195 	KUNIT_ASSERT_PTR_EQ(test, fw_parent_device((&unit0)), &node);
196 	KUNIT_ASSERT_PTR_EQ(test, fw_unit(unit0_dev), &unit0);
197 
198 	// For entries in root directory.
199 
200 	// Vendor immediate entry is found.
201 	KUNIT_EXPECT_GT(test, show_immediate(node_dev, &config_rom_attributes[0].attr, buf), 0);
202 	KUNIT_EXPECT_STREQ(test, buf, "0x012345\n");
203 
204 	// Model immediate entry is found.
205 	KUNIT_EXPECT_GT(test, show_immediate(node_dev, &config_rom_attributes[4].attr, buf), 0);
206 	KUNIT_EXPECT_STREQ(test, buf, "0xfedcba\n");
207 
208 	// Descriptor leaf entry for vendor is not found.
209 	KUNIT_EXPECT_LT(test, show_text_leaf(node_dev, &config_rom_attributes[5].attr, buf), 0);
210 
211 	// Descriptor leaf entry for model is found.
212 	KUNIT_EXPECT_GT(test, show_text_leaf(node_dev, &config_rom_attributes[6].attr, buf), 0);
213 	KUNIT_EXPECT_STREQ(test, buf, "ABCDEFGHIJ\n");
214 
215 	// For entries in unit 0 directory.
216 
217 	// Vendor immediate entry is not found.
218 	KUNIT_EXPECT_LT(test, show_immediate(unit0_dev, &config_rom_attributes[0].attr, buf), 0);
219 
220 	// Model immediate entry is not found.
221 	KUNIT_EXPECT_LT(test, show_immediate(unit0_dev, &config_rom_attributes[4].attr, buf), 0);
222 
223 	// Descriptor leaf entry for vendor is not found.
224 	KUNIT_EXPECT_LT(test, show_text_leaf(unit0_dev, &config_rom_attributes[5].attr, buf), 0);
225 
226 	// Descriptor leaf entry for model is not found.
227 	KUNIT_EXPECT_LT(test, show_text_leaf(unit0_dev, &config_rom_attributes[6].attr, buf), 0);
228 
229 	// Specifier_ID immediate entry is found.
230 	KUNIT_EXPECT_GT(test, show_immediate(unit0_dev, &config_rom_attributes[2].attr, buf), 0);
231 	KUNIT_EXPECT_STREQ(test, buf, "0xabcdef\n");
232 
233 	// Version immediate entry is found.
234 	KUNIT_EXPECT_GT(test, show_immediate(unit0_dev, &config_rom_attributes[3].attr, buf), 0);
235 	KUNIT_EXPECT_STREQ(test, buf, "0x543210\n");
236 
237 	kunit_kfree(test, buf);
238 
239 	get_modalias_ids(&unit0, ids);
240 	KUNIT_EXPECT_MEMEQ(test, ids, unit0_expected_ids, sizeof(ids));
241 }
242 
243 static struct kunit_case device_attr_test_cases[] = {
244 	KUNIT_CASE(device_attr_simple_avc),
245 	KUNIT_CASE(device_attr_legacy_avc),
246 	{}
247 };
248 
249 static struct kunit_suite device_attr_test_suite = {
250 	.name = "firewire-device-attribute",
251 	.test_cases = device_attr_test_cases,
252 };
253 kunit_test_suite(device_attr_test_suite);
254