1 // SPDX-License-Identifier: GPL-2.0-only 2 // 3 // config-rom-parser-test.c - An application of Kunit to test configuration ROM parser. 4 // 5 // Copyright (c) 2026 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 #include <kunit/static_stub.h> 11 #include <kunit/device.h> 12 13 static const u32 sony_dcr_trv310k_config_rom[] = { 14 0x0404e552, 15 0x31333934, 16 0xe0648100, 17 0x00008500, 18 0x005eb597, 19 0x0007cdd0, 20 0x03000085, 21 0x8100000d, 22 0x17000002, 23 0x81000010, 24 0x0c0083c0, 25 0xd8000002, 26 0xd1000003, 27 0x0001ce96, 28 0xd1000001, 29 0x0004bbee, 30 0x1200a02d, 31 0x13010001, 32 0x17000002, 33 0x81000006, 34 0x00046dc8, 35 0x00000000, 36 0x00000000, 37 0x43616e6f, 38 0x6e000000, 39 0x000621ee, 40 0x00000000, 41 0x00000000, 42 0x4d563569, 43 0x204d4300, 44 0x00000000, 45 0x00000000, 46 }; 47 48 static const u32 canon_mv5i_mc_config_rom[] = { 49 0x0404e552, 50 0x31333934, 51 0xe0648100, 52 0x00008500, 53 0x005eb597, 54 0x0007cdd0, 55 0x03000085, 56 0x8100000d, 57 0x17000002, 58 0x81000010, 59 0x0c0083c0, 60 0xd8000002, 61 0xd1000003, 62 0x0001ce96, 63 0xd1000001, 64 0x0004bbee, 65 0x1200a02d, 66 0x13010001, 67 0x17000002, 68 0x81000006, 69 0x00046dc8, 70 0x00000000, 71 0x00000000, 72 0x43616e6f, 73 0x6e000000, 74 0x000621ee, 75 0x00000000, 76 0x00000000, 77 0x4d563569, 78 0x204d4300, 79 0x00000000, 80 0x00000000, 81 }; 82 83 static const u32 motu_audioexpress_config_rom[] = { 84 0x0410a756, 85 0x31333934, 86 0x20ff7000, 87 0x0001f200, 88 0x000a8a7b, 89 0x0004ef04, 90 0x030001f2, 91 0x0c0083c0, 92 0xd1000002, 93 0x8d000005, 94 0x00031680, 95 0x120001f2, 96 0x13000033, 97 0x17104800, 98 0x00025ef3, 99 0x0001f200, 100 0x000a8a7b, 101 }; 102 103 static const u32 tascam_fw1884_config_rom[] = { 104 0x040f23c0, 105 0x31333934, 106 0x20ff7002, 107 0x00022eff, 108 0xfe800000, 109 0x0004bccc, 110 0x0300022e, 111 0x0c0083c0, 112 0x8d000006, 113 0xd1000001, 114 0x000347f5, 115 0x1200022e, 116 0x13800000, 117 0xd4000004, 118 0x000289aa, 119 0x00022eff, 120 0xfe800000, 121 0x0002ae47, 122 0x81000002, 123 0x82000006, 124 0x0004a79e, 125 0x00000000, 126 0x00000000, 127 0x54415343, 128 0x414d0000, 129 0x00045443, 130 0x00000000, 131 0x00000000, 132 0x46572d31, 133 0x38383400, 134 }; 135 136 static const struct parser_test_case { 137 const char *name; 138 const u32 *quadlets; 139 size_t quadlet_length; 140 int phy_speed_in_self_id; 141 int expected_speed; 142 int expected_quirk; 143 unsigned int expected_max_rec; 144 bool expected_cmc; 145 bool expected_irmc; 146 } parser_test_cases[] = { 147 { 148 .name = "detect_irm_is_1394_1995_only_quirk", 149 .quadlets = sony_dcr_trv310k_config_rom, 150 .quadlet_length = ARRAY_SIZE(sony_dcr_trv310k_config_rom), 151 .phy_speed_in_self_id = SCODE_100, 152 .expected_speed = SCODE_100, 153 .expected_quirk = FW_DEVICE_QUIRK_IRM_IS_1394_1995_ONLY, 154 .expected_max_rec = 8, 155 .expected_cmc = true, 156 .expected_irmc = true, 157 }, 158 { 159 .name = "detect_irm_ignores_bus_manager_quirk", 160 .quadlets = canon_mv5i_mc_config_rom, 161 .quadlet_length = ARRAY_SIZE(canon_mv5i_mc_config_rom), 162 .phy_speed_in_self_id = SCODE_100, 163 .expected_speed = SCODE_100, 164 .expected_quirk = FW_DEVICE_QUIRK_IRM_IGNORES_BUS_MANAGER, 165 .expected_max_rec = 8, 166 .expected_cmc = true, 167 .expected_irmc = true, 168 }, 169 { 170 .name = "detect_ack_packet_with_invalid_pending_code_quirk", 171 .quadlets = motu_audioexpress_config_rom, 172 .quadlet_length = ARRAY_SIZE(motu_audioexpress_config_rom), 173 .phy_speed_in_self_id = SCODE_400, 174 .expected_speed = SCODE_400, 175 .expected_quirk = FW_DEVICE_QUIRK_ACK_PACKET_WITH_INVALID_PENDING_CODE, 176 .expected_max_rec = 7, 177 .expected_cmc = false, 178 .expected_irmc = false, 179 }, 180 { 181 .name = "detect_unstable_at_s400_quirk", 182 .quadlets = tascam_fw1884_config_rom, 183 .quadlet_length = ARRAY_SIZE(tascam_fw1884_config_rom), 184 .phy_speed_in_self_id = SCODE_400, 185 .expected_speed = SCODE_200, 186 .expected_quirk = FW_DEVICE_QUIRK_UNSTABLE_AT_S400, 187 .expected_max_rec = 7, 188 .expected_cmc = false, 189 .expected_irmc = false, 190 }, 191 }; 192 193 // Define parser_test_gen_params. 194 KUNIT_ARRAY_PARAM_DESC(parser_test, parser_test_cases, name); 195 196 static int stub_run_transaction_regular(struct fw_card *card, int tcode, int destination_id, 197 int generation, int speed, unsigned long long offset, 198 void *payload, size_t length) 199 { 200 struct kunit *test = kunit_get_current_test(); 201 const struct parser_test_case *param = test->param_value; 202 203 KUNIT_ASSERT_GE(test, offset, CSR_REGISTER_BASE | CSR_CONFIG_ROM); 204 KUNIT_ASSERT_LT(test, offset, CSR_REGISTER_BASE | CSR_CONFIG_ROM_END); 205 KUNIT_ASSERT_NOT_NULL(test, payload); 206 KUNIT_ASSERT_EQ(test, length, 4); 207 208 unsigned int index = (offset - (CSR_REGISTER_BASE | CSR_CONFIG_ROM)) / sizeof(u32); 209 __be32 *quadlet = payload; 210 211 KUNIT_EXPECT_LE(test, speed, param->expected_speed); 212 KUNIT_EXPECT_LT(test, index, param->quadlet_length); 213 214 *quadlet = cpu_to_be32(param->quadlets[index]); 215 216 return RCODE_COMPLETE; 217 } 218 219 static void test_parser_with_regular_cases(struct kunit *test) 220 { 221 struct fw_device *device = test->priv; 222 const struct parser_test_case *param = test->param_value; 223 224 kunit_activate_static_stub(test, fw_run_transaction, stub_run_transaction_regular); 225 226 device->card->link_speed = SCODE_BETA; 227 device->node->max_speed = param->phy_speed_in_self_id; 228 229 KUNIT_EXPECT_EQ(test, read_config_rom(device, 0), RCODE_COMPLETE); 230 231 KUNIT_EXPECT_EQ(test, device->config_rom_length, param->quadlet_length); 232 KUNIT_EXPECT_MEMEQ(test, device->config_rom, param->quadlets, param->quadlet_length); 233 234 KUNIT_EXPECT_TRUE(test, device->quirks & param->expected_quirk); 235 KUNIT_EXPECT_EQ(test, device->max_speed, param->expected_speed); 236 KUNIT_EXPECT_EQ(test, (unsigned int)device->max_rec, param->expected_max_rec); 237 KUNIT_EXPECT_EQ(test, (bool)device->cmc, param->expected_cmc); 238 KUNIT_EXPECT_EQ(test, (bool)device->irmc, param->expected_irmc); 239 240 kunit_deactivate_static_stub(test, fw_run_transaction); 241 } 242 243 static int stub_run_transaction_malformed(struct fw_card *card, int tcode, int destination_id, 244 int generation, int speed, unsigned long long offset, 245 void *payload, size_t length) 246 { 247 static const u32 config_rom_first_part[] = { 248 0x04000000, 249 0x31333934, 250 0x00008002, 251 0x00000000, 252 0x00000000, 253 0x00030000, 254 (CSR_VENDOR << 24) | 0x00123456, // Regular entry. 255 ((CSR_LEAF | CSR_DESCRIPTOR) << 24) | 0x000000f9, // Beyond the upper limit. 256 ((CSR_DIRECTORY | CSR_UNIT) << 24) | 0x00000001, 257 0xffff0000, // Over the upper limit. 258 }; 259 struct kunit *test = kunit_get_current_test(); 260 261 KUNIT_ASSERT_GE(test, offset, CSR_REGISTER_BASE | CSR_CONFIG_ROM); 262 KUNIT_ASSERT_LT(test, offset, CSR_REGISTER_BASE | CSR_CONFIG_ROM_END); 263 KUNIT_ASSERT_NOT_NULL(test, payload); 264 KUNIT_ASSERT_EQ(test, length, 4); 265 266 unsigned int index = (offset - (CSR_REGISTER_BASE | CSR_CONFIG_ROM)) / sizeof(u32); 267 __be32 *quadlet = payload; 268 269 if (index < ARRAY_SIZE(config_rom_first_part)) 270 *quadlet = cpu_to_be32(config_rom_first_part[index]); 271 else 272 *quadlet = 0; 273 274 return RCODE_COMPLETE; 275 } 276 277 static void test_parser_with_overflowed_case(struct kunit *test) 278 { 279 static const u32 corrected_config_rom[] = { 280 0x04000000, 281 0x31333934, 282 0x00008002, 283 0x00000000, 284 0x00000000, 285 0x00030000, 286 0x03123456, 287 0x00000000, // Sanitized. 288 0xd1000001, 289 0x00000000, // Sanitized. 290 }; 291 struct fw_device *device = test->priv; 292 293 kunit_activate_static_stub(test, fw_run_transaction, stub_run_transaction_malformed); 294 295 device->card->link_speed = SCODE_BETA; 296 297 KUNIT_EXPECT_EQ(test, read_config_rom(device, 0), RCODE_COMPLETE); 298 299 KUNIT_EXPECT_EQ(test, device->config_rom_length, ARRAY_SIZE(corrected_config_rom)); 300 KUNIT_EXPECT_MEMEQ(test, device->config_rom, corrected_config_rom, 301 sizeof(corrected_config_rom)); 302 303 kunit_deactivate_static_stub(test, fw_run_transaction); 304 } 305 306 static const struct fw_card_driver dummy_card_driver; 307 308 static int config_rom_parser_test_init(struct kunit *test) 309 { 310 struct fw_device *device; 311 struct device *dev; 312 313 device = kunit_kzalloc(test, sizeof(*device), GFP_KERNEL); 314 KUNIT_ASSERT_NOT_NULL(test, device); 315 316 device->node = kunit_kzalloc(test, sizeof(*device->node), GFP_KERNEL); 317 KUNIT_ASSERT_NOT_NULL(test, device->node); 318 kref_init(&device->node->kref); 319 320 device->card = kunit_kzalloc(test, sizeof(*device->card), GFP_KERNEL); 321 KUNIT_ASSERT_NOT_NULL(test, device->card); 322 323 dev = kunit_device_register(test, "dummy-device"); 324 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev); 325 326 fw_card_initialize(device->card, &dummy_card_driver, dev); 327 328 test->priv = device; 329 330 return 0; 331 } 332 333 static void config_rom_parser_test_exit(struct kunit *test) 334 { 335 struct fw_device *device = test->priv; 336 337 kunit_device_unregister(test, device->card->device); 338 kunit_kfree(test, device->card); 339 kunit_kfree(test, device->node); 340 kunit_kfree(test, device); 341 } 342 343 static struct kunit_case config_rom_parser_test_cases[] = { 344 KUNIT_CASE_PARAM(test_parser_with_regular_cases, parser_test_gen_params), 345 KUNIT_CASE(test_parser_with_overflowed_case), 346 {} 347 }; 348 349 static struct kunit_suite config_rom_parser_test_suite = { 350 .name = "firewire-config-rom-parser", 351 .init = config_rom_parser_test_init, 352 .exit = config_rom_parser_test_exit, 353 .test_cases = config_rom_parser_test_cases, 354 }; 355 kunit_test_suite(config_rom_parser_test_suite); 356