1 // SPDX-License-Identifier: GPL-2.0-only
3 // device-attribute-test.c - An application of Kunit to test implementation for device attributes.
5 // Copyright (c) 2023 Takashi Sakamoto
7 // This file can not be built independently since it is intentionally included in core-device.c.
9 #include <kunit/test.h>
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
15 // Copied from the documentation.
16 static const u32 simple_avc_config_rom
[] = {
22 0x00063287, // root directory.
29 0x0004442d, // unit 0 directory.
34 0x0005c915, // leaf for textual descriptor.
40 0x00057f16, // leaf for textual descriptor.
49 // Annex A:Consideration for configuration ROM reader design (informative)
50 // A.1 Vendor directory
53 static const u32 legacy_avc_config_rom
[] = {
59 0x0005dace, // root directory.
65 0x0002e107, // unit 0 directory.
68 0x0002cb73, // vendor directory.
71 0x00026dc1, // leaf for EUI-64.
74 0x00050e84, // leaf for textual descriptor.
82 static void device_attr_simple_avc(struct kunit
*test
)
84 static const struct fw_device node
= {
86 .type
= &fw_device_type
,
88 .config_rom
= simple_avc_config_rom
,
89 .config_rom_length
= sizeof(simple_avc_config_rom
),
91 static const struct fw_unit unit0
= {
93 .type
= &fw_unit_type
,
94 .parent
= (struct device
*)&node
.device
,
96 .directory
= &simple_avc_config_rom
[12],
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 int ids
[4] = {0, 0, 0, 0};
104 // Ensure associations for node and unit devices.
106 KUNIT_ASSERT_TRUE(test
, is_fw_device(node_dev
));
107 KUNIT_ASSERT_FALSE(test
, is_fw_unit(node_dev
));
108 KUNIT_ASSERT_PTR_EQ(test
, fw_device(node_dev
), &node
);
110 KUNIT_ASSERT_FALSE(test
, is_fw_device(unit0_dev
));
111 KUNIT_ASSERT_TRUE(test
, is_fw_unit(unit0_dev
));
112 KUNIT_ASSERT_PTR_EQ(test
, fw_parent_device((&unit0
)), &node
);
113 KUNIT_ASSERT_PTR_EQ(test
, fw_unit(unit0_dev
), &unit0
);
115 // For entries in root directory.
117 // Vendor immediate entry is found.
118 KUNIT_EXPECT_GT(test
, show_immediate(node_dev
, &config_rom_attributes
[0].attr
, buf
), 0);
119 KUNIT_EXPECT_STREQ(test
, buf
, "0xffffff\n");
121 // Model immediate entry is found.
122 KUNIT_EXPECT_GT(test
, show_immediate(node_dev
, &config_rom_attributes
[4].attr
, buf
), 0);
123 KUNIT_EXPECT_STREQ(test
, buf
, "0xffffff\n");
125 // Descriptor leaf entry for vendor is found.
126 KUNIT_EXPECT_GT(test
, show_text_leaf(node_dev
, &config_rom_attributes
[5].attr
, buf
), 0);
127 KUNIT_EXPECT_STREQ(test
, buf
, "Vendor Name\n");
129 // Descriptor leaf entry for model is found.
130 KUNIT_EXPECT_GT(test
, show_text_leaf(node_dev
, &config_rom_attributes
[6].attr
, buf
), 0);
131 KUNIT_EXPECT_STREQ(test
, buf
, "Model Name\n");
133 // For entries in unit 0 directory.
135 // Vendor immediate entry is not found.
136 KUNIT_EXPECT_LT(test
, show_immediate(unit0_dev
, &config_rom_attributes
[0].attr
, buf
), 0);
138 // Model immediate entry is found.
139 KUNIT_EXPECT_GT(test
, show_immediate(unit0_dev
, &config_rom_attributes
[4].attr
, buf
), 0);
140 KUNIT_EXPECT_STREQ(test
, buf
, "0xffffff\n");
142 // Descriptor leaf entry for vendor is not found.
143 KUNIT_EXPECT_LT(test
, show_text_leaf(unit0_dev
, &config_rom_attributes
[5].attr
, buf
), 0);
145 // Descriptor leaf entry for model is found.
146 KUNIT_EXPECT_GT(test
, show_text_leaf(unit0_dev
, &config_rom_attributes
[6].attr
, buf
), 0);
147 KUNIT_EXPECT_STREQ(test
, buf
, "Model Name\n");
149 // Specifier_ID immediate entry is found.
150 KUNIT_EXPECT_GT(test
, show_immediate(unit0_dev
, &config_rom_attributes
[2].attr
, buf
), 0);
151 KUNIT_EXPECT_STREQ(test
, buf
, "0x00a02d\n");
153 // Version immediate entry is found.
154 KUNIT_EXPECT_GT(test
, show_immediate(unit0_dev
, &config_rom_attributes
[3].attr
, buf
), 0);
155 KUNIT_EXPECT_STREQ(test
, buf
, "0x010001\n");
157 kunit_kfree(test
, buf
);
159 get_modalias_ids(&unit0
, ids
);
160 KUNIT_EXPECT_MEMEQ(test
, ids
, unit0_expected_ids
, sizeof(ids
));
163 static void device_attr_legacy_avc(struct kunit
*test
)
165 static const struct fw_device node
= {
167 .type
= &fw_device_type
,
169 .config_rom
= legacy_avc_config_rom
,
170 .config_rom_length
= sizeof(legacy_avc_config_rom
),
172 static const struct fw_unit unit0
= {
174 .type
= &fw_unit_type
,
175 .parent
= (struct device
*)&node
.device
,
177 .directory
= &legacy_avc_config_rom
[11],
179 struct device
*node_dev
= (struct device
*)&node
.device
;
180 struct device
*unit0_dev
= (struct device
*)&unit0
.device
;
181 static const int unit0_expected_ids
[] = {0x00012345, 0x00fedcba, 0x00abcdef, 0x00543210};
182 char *buf
= kunit_kzalloc(test
, PAGE_SIZE
, GFP_KERNEL
);
183 int ids
[4] = {0, 0, 0, 0};
185 // Ensure associations for node and unit devices.
187 KUNIT_ASSERT_TRUE(test
, is_fw_device(node_dev
));
188 KUNIT_ASSERT_FALSE(test
, is_fw_unit(node_dev
));
189 KUNIT_ASSERT_PTR_EQ(test
, fw_device((node_dev
)), &node
);
191 KUNIT_ASSERT_FALSE(test
, is_fw_device(unit0_dev
));
192 KUNIT_ASSERT_TRUE(test
, is_fw_unit(unit0_dev
));
193 KUNIT_ASSERT_PTR_EQ(test
, fw_parent_device((&unit0
)), &node
);
194 KUNIT_ASSERT_PTR_EQ(test
, fw_unit(unit0_dev
), &unit0
);
196 // For entries in root directory.
198 // Vendor immediate entry is found.
199 KUNIT_EXPECT_GT(test
, show_immediate(node_dev
, &config_rom_attributes
[0].attr
, buf
), 0);
200 KUNIT_EXPECT_STREQ(test
, buf
, "0x012345\n");
202 // Model immediate entry is found.
203 KUNIT_EXPECT_GT(test
, show_immediate(node_dev
, &config_rom_attributes
[4].attr
, buf
), 0);
204 KUNIT_EXPECT_STREQ(test
, buf
, "0xfedcba\n");
206 // Descriptor leaf entry for vendor is not found.
207 KUNIT_EXPECT_LT(test
, show_text_leaf(node_dev
, &config_rom_attributes
[5].attr
, buf
), 0);
209 // Descriptor leaf entry for model is found.
210 KUNIT_EXPECT_GT(test
, show_text_leaf(node_dev
, &config_rom_attributes
[6].attr
, buf
), 0);
211 KUNIT_EXPECT_STREQ(test
, buf
, "ABCDEFGHIJ\n");
213 // For entries in unit 0 directory.
215 // Vendor immediate entry is not found.
216 KUNIT_EXPECT_LT(test
, show_immediate(unit0_dev
, &config_rom_attributes
[0].attr
, buf
), 0);
218 // Model immediate entry is not found.
219 KUNIT_EXPECT_LT(test
, show_immediate(unit0_dev
, &config_rom_attributes
[4].attr
, buf
), 0);
221 // Descriptor leaf entry for vendor is not found.
222 KUNIT_EXPECT_LT(test
, show_text_leaf(unit0_dev
, &config_rom_attributes
[5].attr
, buf
), 0);
224 // Descriptor leaf entry for model is not found.
225 KUNIT_EXPECT_LT(test
, show_text_leaf(unit0_dev
, &config_rom_attributes
[6].attr
, buf
), 0);
227 // Specifier_ID immediate entry is found.
228 KUNIT_EXPECT_GT(test
, show_immediate(unit0_dev
, &config_rom_attributes
[2].attr
, buf
), 0);
229 KUNIT_EXPECT_STREQ(test
, buf
, "0xabcdef\n");
231 // Version immediate entry is found.
232 KUNIT_EXPECT_GT(test
, show_immediate(unit0_dev
, &config_rom_attributes
[3].attr
, buf
), 0);
233 KUNIT_EXPECT_STREQ(test
, buf
, "0x543210\n");
235 kunit_kfree(test
, buf
);
237 get_modalias_ids(&unit0
, ids
);
238 KUNIT_EXPECT_MEMEQ(test
, ids
, unit0_expected_ids
, sizeof(ids
));
241 static struct kunit_case device_attr_test_cases
[] = {
242 KUNIT_CASE(device_attr_simple_avc
),
243 KUNIT_CASE(device_attr_legacy_avc
),
247 static struct kunit_suite device_attr_test_suite
= {
248 .name
= "firewire-device-attribute",
249 .test_cases
= device_attr_test_cases
,
251 kunit_test_suite(device_attr_test_suite
);