Path: blob/master/drivers/firewire/device-attribute-test.c
26378 views
// SPDX-License-Identifier: GPL-2.0-only1//2// device-attribute-test.c - An application of Kunit to test implementation for device attributes.3//4// Copyright (c) 2023 Takashi Sakamoto5//6// This file can not be built independently since it is intentionally included in core-device.c.78#include <kunit/test.h>910// Configuration ROM for AV/C Devices 1.0 (Dec. 12, 2000, 1394 Trading Association)11// Annex C:Configuration ROM example(informative)12// C.1 Simple AV/C device13//14// Copied from the documentation.15static const u32 simple_avc_config_rom[] = {160x0404eabf,170x31333934,180xe0646102,190xffffffff,200xffffffff,210x00063287, // root directory.220x03ffffff,230x8100000a,240x17ffffff,250x8100000e,260x0c0083c0,270xd1000001,280x0004442d, // unit 0 directory.290x1200a02d,300x13010001,310x17ffffff,320x81000007,330x0005c915, // leaf for textual descriptor.340x00000000,350x00000000,360x56656e64,370x6f72204e,380x616d6500,390x00057f16, // leaf for textual descriptor.400x00000000,410x00000000,420x4d6f6465,430x6c204e61,440x6d650000,45};4647// Ibid.48// Annex A:Consideration for configuration ROM reader design (informative)49// A.1 Vendor directory50//51// Written by hand.52static const u32 legacy_avc_config_rom[] = {530x04199fe7,540x31333934,550xe0644000,560x00112233,570x44556677,580x0005dace, // root directory.590x03012345,600x0c0083c0,610x8d000009,620xd1000002,630xc3000004,640x0002e107, // unit 0 directory.650x12abcdef,660x13543210,670x0002cb73, // vendor directory.680x17fedcba,690x81000004,700x00026dc1, // leaf for EUI-64.710x00112233,720x44556677,730x00050e84, // leaf for textual descriptor.740x00000000,750x00000000,760x41424344,770x45464748,780x494a0000,79};8081static void device_attr_simple_avc(struct kunit *test)82{83static const struct fw_device node = {84.device = {85.type = &fw_device_type,86},87.config_rom = simple_avc_config_rom,88.config_rom_length = sizeof(simple_avc_config_rom),89};90static const struct fw_unit unit0 = {91.device = {92.type = &fw_unit_type,93.parent = (struct device *)&node.device,94},95.directory = &simple_avc_config_rom[12],96};97struct device *node_dev = (struct device *)&node.device;98struct device *unit0_dev = (struct device *)&unit0.device;99static const int unit0_expected_ids[] = {0x00ffffff, 0x00ffffff, 0x0000a02d, 0x00010001};100char *buf = kunit_kzalloc(test, PAGE_SIZE, GFP_KERNEL);101KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf);102int ids[4] = {0, 0, 0, 0};103104// Ensure associations for node and unit devices.105106KUNIT_ASSERT_TRUE(test, is_fw_device(node_dev));107KUNIT_ASSERT_FALSE(test, is_fw_unit(node_dev));108KUNIT_ASSERT_PTR_EQ(test, fw_device(node_dev), &node);109110KUNIT_ASSERT_FALSE(test, is_fw_device(unit0_dev));111KUNIT_ASSERT_TRUE(test, is_fw_unit(unit0_dev));112KUNIT_ASSERT_PTR_EQ(test, fw_parent_device((&unit0)), &node);113KUNIT_ASSERT_PTR_EQ(test, fw_unit(unit0_dev), &unit0);114115// For entries in root directory.116117// Vendor immediate entry is found.118KUNIT_EXPECT_GT(test, show_immediate(node_dev, &config_rom_attributes[0].attr, buf), 0);119KUNIT_EXPECT_STREQ(test, buf, "0xffffff\n");120121// Model immediate entry is found.122KUNIT_EXPECT_GT(test, show_immediate(node_dev, &config_rom_attributes[4].attr, buf), 0);123KUNIT_EXPECT_STREQ(test, buf, "0xffffff\n");124125// Descriptor leaf entry for vendor is found.126KUNIT_EXPECT_GT(test, show_text_leaf(node_dev, &config_rom_attributes[5].attr, buf), 0);127KUNIT_EXPECT_STREQ(test, buf, "Vendor Name\n");128129// Descriptor leaf entry for model is found.130KUNIT_EXPECT_GT(test, show_text_leaf(node_dev, &config_rom_attributes[6].attr, buf), 0);131KUNIT_EXPECT_STREQ(test, buf, "Model Name\n");132133// For entries in unit 0 directory.134135// Vendor immediate entry is not found.136KUNIT_EXPECT_LT(test, show_immediate(unit0_dev, &config_rom_attributes[0].attr, buf), 0);137138// Model immediate entry is found.139KUNIT_EXPECT_GT(test, show_immediate(unit0_dev, &config_rom_attributes[4].attr, buf), 0);140KUNIT_EXPECT_STREQ(test, buf, "0xffffff\n");141142// Descriptor leaf entry for vendor is not found.143KUNIT_EXPECT_LT(test, show_text_leaf(unit0_dev, &config_rom_attributes[5].attr, buf), 0);144145// Descriptor leaf entry for model is found.146KUNIT_EXPECT_GT(test, show_text_leaf(unit0_dev, &config_rom_attributes[6].attr, buf), 0);147KUNIT_EXPECT_STREQ(test, buf, "Model Name\n");148149// Specifier_ID immediate entry is found.150KUNIT_EXPECT_GT(test, show_immediate(unit0_dev, &config_rom_attributes[2].attr, buf), 0);151KUNIT_EXPECT_STREQ(test, buf, "0x00a02d\n");152153// Version immediate entry is found.154KUNIT_EXPECT_GT(test, show_immediate(unit0_dev, &config_rom_attributes[3].attr, buf), 0);155KUNIT_EXPECT_STREQ(test, buf, "0x010001\n");156157kunit_kfree(test, buf);158159get_modalias_ids(&unit0, ids);160KUNIT_EXPECT_MEMEQ(test, ids, unit0_expected_ids, sizeof(ids));161}162163static void device_attr_legacy_avc(struct kunit *test)164{165static const struct fw_device node = {166.device = {167.type = &fw_device_type,168},169.config_rom = legacy_avc_config_rom,170.config_rom_length = sizeof(legacy_avc_config_rom),171};172static const struct fw_unit unit0 = {173.device = {174.type = &fw_unit_type,175.parent = (struct device *)&node.device,176},177.directory = &legacy_avc_config_rom[11],178};179struct device *node_dev = (struct device *)&node.device;180struct device *unit0_dev = (struct device *)&unit0.device;181static const int unit0_expected_ids[] = {0x00012345, 0x00fedcba, 0x00abcdef, 0x00543210};182char *buf = kunit_kzalloc(test, PAGE_SIZE, GFP_KERNEL);183KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf);184int ids[4] = {0, 0, 0, 0};185186// Ensure associations for node and unit devices.187188KUNIT_ASSERT_TRUE(test, is_fw_device(node_dev));189KUNIT_ASSERT_FALSE(test, is_fw_unit(node_dev));190KUNIT_ASSERT_PTR_EQ(test, fw_device((node_dev)), &node);191192KUNIT_ASSERT_FALSE(test, is_fw_device(unit0_dev));193KUNIT_ASSERT_TRUE(test, is_fw_unit(unit0_dev));194KUNIT_ASSERT_PTR_EQ(test, fw_parent_device((&unit0)), &node);195KUNIT_ASSERT_PTR_EQ(test, fw_unit(unit0_dev), &unit0);196197// For entries in root directory.198199// Vendor immediate entry is found.200KUNIT_EXPECT_GT(test, show_immediate(node_dev, &config_rom_attributes[0].attr, buf), 0);201KUNIT_EXPECT_STREQ(test, buf, "0x012345\n");202203// Model immediate entry is found.204KUNIT_EXPECT_GT(test, show_immediate(node_dev, &config_rom_attributes[4].attr, buf), 0);205KUNIT_EXPECT_STREQ(test, buf, "0xfedcba\n");206207// Descriptor leaf entry for vendor is not found.208KUNIT_EXPECT_LT(test, show_text_leaf(node_dev, &config_rom_attributes[5].attr, buf), 0);209210// Descriptor leaf entry for model is found.211KUNIT_EXPECT_GT(test, show_text_leaf(node_dev, &config_rom_attributes[6].attr, buf), 0);212KUNIT_EXPECT_STREQ(test, buf, "ABCDEFGHIJ\n");213214// For entries in unit 0 directory.215216// Vendor immediate entry is not found.217KUNIT_EXPECT_LT(test, show_immediate(unit0_dev, &config_rom_attributes[0].attr, buf), 0);218219// Model immediate entry is not found.220KUNIT_EXPECT_LT(test, show_immediate(unit0_dev, &config_rom_attributes[4].attr, buf), 0);221222// Descriptor leaf entry for vendor is not found.223KUNIT_EXPECT_LT(test, show_text_leaf(unit0_dev, &config_rom_attributes[5].attr, buf), 0);224225// Descriptor leaf entry for model is not found.226KUNIT_EXPECT_LT(test, show_text_leaf(unit0_dev, &config_rom_attributes[6].attr, buf), 0);227228// Specifier_ID immediate entry is found.229KUNIT_EXPECT_GT(test, show_immediate(unit0_dev, &config_rom_attributes[2].attr, buf), 0);230KUNIT_EXPECT_STREQ(test, buf, "0xabcdef\n");231232// Version immediate entry is found.233KUNIT_EXPECT_GT(test, show_immediate(unit0_dev, &config_rom_attributes[3].attr, buf), 0);234KUNIT_EXPECT_STREQ(test, buf, "0x543210\n");235236kunit_kfree(test, buf);237238get_modalias_ids(&unit0, ids);239KUNIT_EXPECT_MEMEQ(test, ids, unit0_expected_ids, sizeof(ids));240}241242static struct kunit_case device_attr_test_cases[] = {243KUNIT_CASE(device_attr_simple_avc),244KUNIT_CASE(device_attr_legacy_avc),245{}246};247248static struct kunit_suite device_attr_test_suite = {249.name = "firewire-device-attribute",250.test_cases = device_attr_test_cases,251};252kunit_test_suite(device_attr_test_suite);253254255