device-attribute-test.c 7.9 KB

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