summaryrefslogtreecommitdiff
path: root/drivers/firewire/device-attribute-test.c
blob: 2f123c6b0a1659cf8615491a8b4134afea0d5081 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// SPDX-License-Identifier: GPL-2.0-only
//
// device-attribute-test.c - An application of Kunit to test implementation for device attributes.
//
// Copyright (c) 2023 Takashi Sakamoto
//
// This file can not be built independently since it is intentionally included in core-device.c.

#include <kunit/test.h>

// Configuration ROM for AV/C Devices 1.0 (Dec. 12, 2000, 1394 Trading Association)
// Annex C:Configuration ROM example(informative)
// C.1 Simple AV/C device
//
// Copied from the documentation.
static const u32 simple_avc_config_rom[] = {
	0x0404eabf,
	0x31333934,
	0xe0646102,
	0xffffffff,
	0xffffffff,
	0x00063287, // root directory.
	0x03ffffff,
	0x8100000a,
	0x17ffffff,
	0x8100000e,
	0x0c0083c0,
	0xd1000001,
	0x0004442d, // unit 0 directory.
	0x1200a02d,
	0x13010001,
	0x17ffffff,
	0x81000007,
	0x0005c915, // leaf for textual descriptor.
	0x00000000,
	0x00000000,
	0x56656e64,
	0x6f72204e,
	0x616d6500,
	0x00057f16, // leaf for textual descriptor.
	0x00000000,
	0x00000000,
	0x4d6f6465,
	0x6c204e61,
	0x6d650000,
};

// Ibid.
// Annex A:Consideration for configuration ROM reader design (informative)
// A.1 Vendor directory
//
// Written by hand.
static const u32 legacy_avc_config_rom[] = {
	0x04199fe7,
	0x31333934,
	0xe0644000,
	0x00112233,
	0x44556677,
	0x0005dace, // root directory.
	0x03012345,
	0x0c0083c0,
	0x8d000009,
	0xd1000002,
	0xc3000004,
	0x0002e107, // unit 0 directory.
	0x12abcdef,
	0x13543210,
	0x0002cb73, // vendor directory.
	0x17fedcba,
	0x81000004,
	0x00026dc1, // leaf for EUI-64.
	0x00112233,
	0x44556677,
	0x00050e84, // leaf for textual descriptor.
	0x00000000,
	0x00000000,
	0x41424344,
	0x45464748,
	0x494a0000,
};

static void device_attr_simple_avc(struct kunit *test)
{
	static const struct fw_device node = {
		.device = {
			.type = &fw_device_type,
		},
		.config_rom = simple_avc_config_rom,
		.config_rom_length = sizeof(simple_avc_config_rom),
	};
	static const struct fw_unit unit0 = {
		.device = {
			.type = &fw_unit_type,
			.parent = (struct device *)&node.device,
		},
		.directory = &simple_avc_config_rom[12],
	};
	struct device *node_dev = (struct device *)&node.device;
	struct device *unit0_dev = (struct device *)&unit0.device;
	static const int unit0_expected_ids[] = {0x00ffffff, 0x00ffffff, 0x0000a02d, 0x00010001};
	char *buf = kunit_kzalloc(test, PAGE_SIZE, GFP_KERNEL);
	int ids[4] = {0, 0, 0, 0};

	// Ensure associations for node and unit devices.

	KUNIT_ASSERT_TRUE(test, is_fw_device(node_dev));
	KUNIT_ASSERT_FALSE(test, is_fw_unit(node_dev));
	KUNIT_ASSERT_PTR_EQ(test, fw_device(node_dev), &node);

	KUNIT_ASSERT_FALSE(test, is_fw_device(unit0_dev));
	KUNIT_ASSERT_TRUE(test, is_fw_unit(unit0_dev));
	KUNIT_ASSERT_PTR_EQ(test, fw_parent_device((&unit0)), &node);
	KUNIT_ASSERT_PTR_EQ(test, fw_unit(unit0_dev), &unit0);

	// For entries in root directory.

	// Vendor immediate entry is found.
	KUNIT_EXPECT_GT(test, show_immediate(node_dev, &config_rom_attributes[0].attr, buf), 0);
	KUNIT_EXPECT_STREQ(test, buf, "0xffffff\n");

	// Model immediate entry is found.
	KUNIT_EXPECT_GT(test, show_immediate(node_dev, &config_rom_attributes[4].attr, buf), 0);
	KUNIT_EXPECT_STREQ(test, buf, "0xffffff\n");

	// Descriptor leaf entry for vendor is found.
	KUNIT_EXPECT_GT(test, show_text_leaf(node_dev, &config_rom_attributes[5].attr, buf), 0);
	KUNIT_EXPECT_STREQ(test, buf, "Vendor Name\n");

	// Descriptor leaf entry for model is found.
	KUNIT_EXPECT_GT(test, show_text_leaf(node_dev, &config_rom_attributes[6].attr, buf), 0);
	KUNIT_EXPECT_STREQ(test, buf, "Model Name\n");

	// For entries in unit 0 directory.

	// Vendor immediate entry is not found.
	KUNIT_EXPECT_LT(test, show_immediate(unit0_dev, &config_rom_attributes[0].attr, buf), 0);

	// Model immediate entry is found.
	KUNIT_EXPECT_GT(test, show_immediate(unit0_dev, &config_rom_attributes[4].attr, buf), 0);
	KUNIT_EXPECT_STREQ(test, buf, "0xffffff\n");

	// Descriptor leaf entry for vendor is not found.
	KUNIT_EXPECT_LT(test, show_text_leaf(unit0_dev, &config_rom_attributes[5].attr, buf), 0);

	// Descriptor leaf entry for model is found.
	KUNIT_EXPECT_GT(test, show_text_leaf(unit0_dev, &config_rom_attributes[6].attr, buf), 0);
	KUNIT_EXPECT_STREQ(test, buf, "Model Name\n");

	// Specifier_ID immediate entry is found.
	KUNIT_EXPECT_GT(test, show_immediate(unit0_dev, &config_rom_attributes[2].attr, buf), 0);
	KUNIT_EXPECT_STREQ(test, buf, "0x00a02d\n");

	// Version immediate entry is found.
	KUNIT_EXPECT_GT(test, show_immediate(unit0_dev, &config_rom_attributes[3].attr, buf), 0);
	KUNIT_EXPECT_STREQ(test, buf, "0x010001\n");

	kunit_kfree(test, buf);

	get_modalias_ids(&unit0, ids);
	KUNIT_EXPECT_MEMEQ(test, ids, unit0_expected_ids, sizeof(ids));
}

static void device_attr_legacy_avc(struct kunit *test)
{
	static const struct fw_device node = {
		.device = {
			.type = &fw_device_type,
		},
		.config_rom = legacy_avc_config_rom,
		.config_rom_length = sizeof(legacy_avc_config_rom),
	};
	static const struct fw_unit unit0 = {
		.device = {
			.type = &fw_unit_type,
			.parent = (struct device *)&node.device,
		},
		.directory = &legacy_avc_config_rom[11],
	};
	struct device *node_dev = (struct device *)&node.device;
	struct device *unit0_dev = (struct device *)&unit0.device;
	static const int unit0_expected_ids[] = {0x00012345, 0x00fedcba, 0x00abcdef, 0x00543210};
	char *buf = kunit_kzalloc(test, PAGE_SIZE, GFP_KERNEL);
	int ids[4] = {0, 0, 0, 0};

	// Ensure associations for node and unit devices.

	KUNIT_ASSERT_TRUE(test, is_fw_device(node_dev));
	KUNIT_ASSERT_FALSE(test, is_fw_unit(node_dev));
	KUNIT_ASSERT_PTR_EQ(test, fw_device((node_dev)), &node);

	KUNIT_ASSERT_FALSE(test, is_fw_device(unit0_dev));
	KUNIT_ASSERT_TRUE(test, is_fw_unit(unit0_dev));
	KUNIT_ASSERT_PTR_EQ(test, fw_parent_device((&unit0)), &node);
	KUNIT_ASSERT_PTR_EQ(test, fw_unit(unit0_dev), &unit0);

	// For entries in root directory.

	// Vendor immediate entry is found.
	KUNIT_EXPECT_GT(test, show_immediate(node_dev, &config_rom_attributes[0].attr, buf), 0);
	KUNIT_EXPECT_STREQ(test, buf, "0x012345\n");

	// Model immediate entry is found.
	KUNIT_EXPECT_GT(test, show_immediate(node_dev, &config_rom_attributes[4].attr, buf), 0);
	KUNIT_EXPECT_STREQ(test, buf, "0xfedcba\n");

	// Descriptor leaf entry for vendor is not found.
	KUNIT_EXPECT_LT(test, show_text_leaf(node_dev, &config_rom_attributes[5].attr, buf), 0);

	// Descriptor leaf entry for model is found.
	KUNIT_EXPECT_GT(test, show_text_leaf(node_dev, &config_rom_attributes[6].attr, buf), 0);
	KUNIT_EXPECT_STREQ(test, buf, "ABCDEFGHIJ\n");

	// For entries in unit 0 directory.

	// Vendor immediate entry is not found.
	KUNIT_EXPECT_LT(test, show_immediate(unit0_dev, &config_rom_attributes[0].attr, buf), 0);

	// Model immediate entry is not found.
	KUNIT_EXPECT_LT(test, show_immediate(unit0_dev, &config_rom_attributes[4].attr, buf), 0);

	// Descriptor leaf entry for vendor is not found.
	KUNIT_EXPECT_LT(test, show_text_leaf(unit0_dev, &config_rom_attributes[5].attr, buf), 0);

	// Descriptor leaf entry for model is not found.
	KUNIT_EXPECT_LT(test, show_text_leaf(unit0_dev, &config_rom_attributes[6].attr, buf), 0);

	// Specifier_ID immediate entry is found.
	KUNIT_EXPECT_GT(test, show_immediate(unit0_dev, &config_rom_attributes[2].attr, buf), 0);
	KUNIT_EXPECT_STREQ(test, buf, "0xabcdef\n");

	// Version immediate entry is found.
	KUNIT_EXPECT_GT(test, show_immediate(unit0_dev, &config_rom_attributes[3].attr, buf), 0);
	KUNIT_EXPECT_STREQ(test, buf, "0x543210\n");

	kunit_kfree(test, buf);

	get_modalias_ids(&unit0, ids);
	KUNIT_EXPECT_MEMEQ(test, ids, unit0_expected_ids, sizeof(ids));
}

static struct kunit_case device_attr_test_cases[] = {
	KUNIT_CASE(device_attr_simple_avc),
	KUNIT_CASE(device_attr_legacy_avc),
	{}
};

static struct kunit_suite device_attr_test_suite = {
	.name = "firewire-device-attribute",
	.test_cases = device_attr_test_cases,
};
kunit_test_suite(device_attr_test_suite);