1 // SPDX-License-Identifier: GPL-2.0+
3 * Tests for the core driver model code
5 * Copyright (c) 2013 Google, Inc
14 #include <dm/device-internal.h>
18 #include <dm/uclass-internal.h>
19 #include <test/test.h>
22 DECLARE_GLOBAL_DATA_PTR;
28 TEST_INTVAL_MANUAL = 101112,
29 TEST_INTVAL_PRE_RELOC = 7,
32 static const struct dm_test_pdata test_pdata[] = {
33 { .ping_add = TEST_INTVAL1, },
34 { .ping_add = TEST_INTVAL2, },
35 { .ping_add = TEST_INTVAL3, },
38 static const struct dm_test_pdata test_pdata_manual = {
39 .ping_add = TEST_INTVAL_MANUAL,
42 static const struct dm_test_pdata test_pdata_pre_reloc = {
43 .ping_add = TEST_INTVAL_PRE_RELOC,
46 U_BOOT_DEVICE(dm_test_info1) = {
48 .platdata = &test_pdata[0],
51 U_BOOT_DEVICE(dm_test_info2) = {
53 .platdata = &test_pdata[1],
56 U_BOOT_DEVICE(dm_test_info3) = {
58 .platdata = &test_pdata[2],
61 static struct driver_info driver_info_manual = {
62 .name = "test_manual_drv",
63 .platdata = &test_pdata_manual,
66 static struct driver_info driver_info_pre_reloc = {
67 .name = "test_pre_reloc_drv",
68 .platdata = &test_pdata_pre_reloc,
71 static struct driver_info driver_info_act_dma = {
72 .name = "test_act_dma_drv",
75 void dm_leak_check_start(struct unit_test_state *uts)
77 uts->start = mallinfo();
78 if (!uts->start.uordblks)
79 puts("Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c\n");
82 int dm_leak_check_end(struct unit_test_state *uts)
87 /* Don't delete the root class, since we started with that */
88 for (id = UCLASS_ROOT + 1; id < UCLASS_COUNT; id++) {
94 ut_assertok(uclass_destroy(uc));
98 diff = end.uordblks - uts->start.uordblks;
100 printf("Leak: lost %#xd bytes\n", diff);
102 printf("Leak: gained %#xd bytes\n", -diff);
103 ut_asserteq(uts->start.uordblks, end.uordblks);
108 /* Test that binding with platdata occurs correctly */
109 static int dm_test_autobind(struct unit_test_state *uts)
111 struct dm_test_state *dms = uts->priv;
115 * We should have a single class (UCLASS_ROOT) and a single root
116 * device with no children.
118 ut_assert(dms->root);
119 ut_asserteq(1, list_count_items(&gd->uclass_root));
120 ut_asserteq(0, list_count_items(&gd->dm_root->child_head));
121 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
123 ut_assertok(dm_scan_platdata(false));
125 /* We should have our test class now at least, plus more children */
126 ut_assert(1 < list_count_items(&gd->uclass_root));
127 ut_assert(0 < list_count_items(&gd->dm_root->child_head));
129 /* Our 3 dm_test_infox children should be bound to the test uclass */
130 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
132 /* No devices should be probed */
133 list_for_each_entry(dev, &gd->dm_root->child_head, sibling_node)
134 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
136 /* Our test driver should have been bound 3 times */
137 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] == 3);
141 DM_TEST(dm_test_autobind, 0);
143 /* Test that binding with uclass platdata allocation occurs correctly */
144 static int dm_test_autobind_uclass_pdata_alloc(struct unit_test_state *uts)
146 struct dm_test_perdev_uc_pdata *uc_pdata;
150 ut_assertok(uclass_get(UCLASS_TEST, &uc));
154 * Test if test uclass driver requires allocation for the uclass
155 * platform data and then check the dev->uclass_platdata pointer.
157 ut_assert(uc->uc_drv->per_device_platdata_auto_alloc_size);
159 for (uclass_find_first_device(UCLASS_TEST, &dev);
161 uclass_find_next_device(&dev)) {
162 ut_assertnonnull(dev);
164 uc_pdata = dev_get_uclass_platdata(dev);
170 DM_TEST(dm_test_autobind_uclass_pdata_alloc, DM_TESTF_SCAN_PDATA);
172 /* Test that binding with uclass platdata setting occurs correctly */
173 static int dm_test_autobind_uclass_pdata_valid(struct unit_test_state *uts)
175 struct dm_test_perdev_uc_pdata *uc_pdata;
179 * In the test_postbind() method of test uclass driver, the uclass
180 * platform data should be set to three test int values - test it.
182 for (uclass_find_first_device(UCLASS_TEST, &dev);
184 uclass_find_next_device(&dev)) {
185 ut_assertnonnull(dev);
187 uc_pdata = dev_get_uclass_platdata(dev);
189 ut_assert(uc_pdata->intval1 == TEST_UC_PDATA_INTVAL1);
190 ut_assert(uc_pdata->intval2 == TEST_UC_PDATA_INTVAL2);
191 ut_assert(uc_pdata->intval3 == TEST_UC_PDATA_INTVAL3);
196 DM_TEST(dm_test_autobind_uclass_pdata_valid, DM_TESTF_SCAN_PDATA);
198 /* Test that autoprobe finds all the expected devices */
199 static int dm_test_autoprobe(struct unit_test_state *uts)
201 struct dm_test_state *dms = uts->priv;
202 int expected_base_add;
207 ut_assertok(uclass_get(UCLASS_TEST, &uc));
210 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
211 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
212 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
214 /* The root device should not be activated until needed */
215 ut_assert(dms->root->flags & DM_FLAG_ACTIVATED);
218 * We should be able to find the three test devices, and they should
219 * all be activated as they are used (lazy activation, required by
222 for (i = 0; i < 3; i++) {
223 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
225 ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED),
226 "Driver %d/%s already activated", i, dev->name);
228 /* This should activate it */
229 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
231 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
233 /* Activating a device should activate the root device */
235 ut_assert(dms->root->flags & DM_FLAG_ACTIVATED);
239 * Our 3 dm_test_info children should be passed to pre_probe and
242 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
243 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
245 /* Also we can check the per-device data */
246 expected_base_add = 0;
247 for (i = 0; i < 3; i++) {
248 struct dm_test_uclass_perdev_priv *priv;
249 struct dm_test_pdata *pdata;
251 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
254 priv = dev_get_uclass_priv(dev);
256 ut_asserteq(expected_base_add, priv->base_add);
258 pdata = dev->platdata;
259 expected_base_add += pdata->ping_add;
264 DM_TEST(dm_test_autoprobe, DM_TESTF_SCAN_PDATA);
266 /* Check that we see the correct platdata in each device */
267 static int dm_test_platdata(struct unit_test_state *uts)
269 const struct dm_test_pdata *pdata;
273 for (i = 0; i < 3; i++) {
274 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
276 pdata = dev->platdata;
277 ut_assert(pdata->ping_add == test_pdata[i].ping_add);
282 DM_TEST(dm_test_platdata, DM_TESTF_SCAN_PDATA);
284 /* Test that we can bind, probe, remove, unbind a driver */
285 static int dm_test_lifecycle(struct unit_test_state *uts)
287 struct dm_test_state *dms = uts->priv;
288 int op_count[DM_TEST_OP_COUNT];
289 struct udevice *dev, *test_dev;
293 memcpy(op_count, dm_testdrv_op_count, sizeof(op_count));
295 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
298 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND]
299 == op_count[DM_TEST_OP_BIND] + 1);
300 ut_assert(!dev->priv);
302 /* Probe the device - it should fail allocating private data */
303 dms->force_fail_alloc = 1;
304 ret = device_probe(dev);
305 ut_assert(ret == -ENOMEM);
306 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
307 == op_count[DM_TEST_OP_PROBE] + 1);
308 ut_assert(!dev->priv);
310 /* Try again without the alloc failure */
311 dms->force_fail_alloc = 0;
312 ut_assertok(device_probe(dev));
313 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
314 == op_count[DM_TEST_OP_PROBE] + 2);
315 ut_assert(dev->priv);
317 /* This should be device 3 in the uclass */
318 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
319 ut_assert(dev == test_dev);
322 ut_assertok(test_ping(dev, 100, &pingret));
323 ut_assert(pingret == 102);
325 /* Now remove device 3 */
326 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
327 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
328 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
330 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
331 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
332 ut_assertok(device_unbind(dev));
333 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
334 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
338 DM_TEST(dm_test_lifecycle, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST);
340 /* Test that we can bind/unbind and the lists update correctly */
341 static int dm_test_ordering(struct unit_test_state *uts)
343 struct dm_test_state *dms = uts->priv;
344 struct udevice *dev, *dev_penultimate, *dev_last, *test_dev;
347 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
351 /* Bind two new devices (numbers 4 and 5) */
352 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
354 ut_assert(dev_penultimate);
355 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
359 /* Now remove device 3 */
360 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
361 ut_assertok(device_unbind(dev));
363 /* The device numbering should have shifted down one */
364 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
365 ut_assert(dev_penultimate == test_dev);
366 ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev));
367 ut_assert(dev_last == test_dev);
369 /* Add back the original device 3, now in position 5 */
370 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
375 ut_assertok(test_ping(dev, 100, &pingret));
376 ut_assert(pingret == 102);
379 ut_assertok(device_remove(dev_penultimate, DM_REMOVE_NORMAL));
380 ut_assertok(device_unbind(dev_penultimate));
381 ut_assertok(device_remove(dev_last, DM_REMOVE_NORMAL));
382 ut_assertok(device_unbind(dev_last));
384 /* Our device should now be in position 3 */
385 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
386 ut_assert(dev == test_dev);
388 /* Now remove device 3 */
389 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
390 ut_assertok(device_unbind(dev));
394 DM_TEST(dm_test_ordering, DM_TESTF_SCAN_PDATA);
396 /* Check that we can perform operations on a device (do a ping) */
397 int dm_check_operations(struct unit_test_state *uts, struct udevice *dev,
398 uint32_t base, struct dm_test_priv *priv)
403 /* Getting the child device should allocate platdata / priv */
404 ut_assertok(testfdt_ping(dev, 10, &pingret));
405 ut_assert(dev->priv);
406 ut_assert(dev->platdata);
408 expected = 10 + base;
409 ut_asserteq(expected, pingret);
411 /* Do another ping */
412 ut_assertok(testfdt_ping(dev, 20, &pingret));
413 expected = 20 + base;
414 ut_asserteq(expected, pingret);
416 /* Now check the ping_total */
418 ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2,
424 /* Check that we can perform operations on devices */
425 static int dm_test_operations(struct unit_test_state *uts)
431 * Now check that the ping adds are what we expect. This is using the
432 * ping-add property in each node.
434 for (i = 0; i < ARRAY_SIZE(test_pdata); i++) {
437 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
440 * Get the 'reg' property, which tells us what the ping add
441 * should be. We don't use the platdata because we want
442 * to test the code that sets that up (testfdt_drv_probe()).
444 base = test_pdata[i].ping_add;
445 debug("dev=%d, base=%d\n", i, base);
447 ut_assert(!dm_check_operations(uts, dev, base, dev->priv));
452 DM_TEST(dm_test_operations, DM_TESTF_SCAN_PDATA);
454 /* Remove all drivers and check that things work */
455 static int dm_test_remove(struct unit_test_state *uts)
460 for (i = 0; i < 3; i++) {
461 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
463 ut_assertf(dev->flags & DM_FLAG_ACTIVATED,
464 "Driver %d/%s not activated", i, dev->name);
465 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
466 ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED),
467 "Driver %d/%s should have deactivated", i,
469 ut_assert(!dev->priv);
474 DM_TEST(dm_test_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST);
476 /* Remove and recreate everything, check for memory leaks */
477 static int dm_test_leak(struct unit_test_state *uts)
481 for (i = 0; i < 2; i++) {
486 dm_leak_check_start(uts);
488 ut_assertok(dm_scan_platdata(false));
489 ut_assertok(dm_scan_fdt(gd->fdt_blob, false));
491 /* Scanning the uclass is enough to probe all the devices */
492 for (id = UCLASS_ROOT; id < UCLASS_COUNT; id++) {
493 for (ret = uclass_first_device(UCLASS_TEST, &dev);
495 ret = uclass_next_device(&dev))
500 ut_assertok(dm_leak_check_end(uts));
505 DM_TEST(dm_test_leak, 0);
507 /* Test uclass init/destroy methods */
508 static int dm_test_uclass(struct unit_test_state *uts)
512 ut_assertok(uclass_get(UCLASS_TEST, &uc));
513 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
514 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
517 ut_assertok(uclass_destroy(uc));
518 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
519 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
523 DM_TEST(dm_test_uclass, 0);
526 * create_children() - Create children of a parent node
528 * @dms: Test system state
529 * @parent: Parent device
530 * @count: Number of children to create
531 * @key: Key value to put in first child. Subsequence children
532 * receive an incrementing value
533 * @child: If not NULL, then the child device pointers are written into
535 * @return 0 if OK, -ve on error
537 static int create_children(struct unit_test_state *uts, struct udevice *parent,
538 int count, int key, struct udevice *child[])
543 for (i = 0; i < count; i++) {
544 struct dm_test_pdata *pdata;
546 ut_assertok(device_bind_by_name(parent, false,
547 &driver_info_manual, &dev));
548 pdata = calloc(1, sizeof(*pdata));
549 pdata->ping_add = key + i;
550 dev->platdata = pdata;
558 #define NODE_COUNT 10
560 static int dm_test_children(struct unit_test_state *uts)
562 struct dm_test_state *dms = uts->priv;
563 struct udevice *top[NODE_COUNT];
564 struct udevice *child[NODE_COUNT];
565 struct udevice *grandchild[NODE_COUNT];
571 /* We don't care about the numbering for this test */
572 dms->skip_post_probe = 1;
574 ut_assert(NODE_COUNT > 5);
576 /* First create 10 top-level children */
577 ut_assertok(create_children(uts, dms->root, NODE_COUNT, 0, top));
579 /* Now a few have their own children */
580 ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
581 ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
583 /* And grandchildren */
584 for (i = 0; i < NODE_COUNT; i++)
585 ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
586 i == 2 ? grandchild : NULL));
588 /* Check total number of devices */
589 total = NODE_COUNT * (3 + NODE_COUNT);
590 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
592 /* Try probing one of the grandchildren */
593 ut_assertok(uclass_get_device(UCLASS_TEST,
594 NODE_COUNT * 3 + 2 * NODE_COUNT, &dev));
595 ut_asserteq_ptr(grandchild[0], dev);
598 * This should have probed the child and top node also, for a total
601 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
603 /* Probe the other grandchildren */
604 for (i = 1; i < NODE_COUNT; i++)
605 ut_assertok(device_probe(grandchild[i]));
607 ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
609 /* Probe everything */
610 for (ret = uclass_first_device(UCLASS_TEST, &dev);
612 ret = uclass_next_device(&dev))
616 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
618 /* Remove a top-level child and check that the children are removed */
619 ut_assertok(device_remove(top[2], DM_REMOVE_NORMAL));
620 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
621 dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0;
623 /* Try one with grandchildren */
624 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
625 ut_asserteq_ptr(dev, top[5]);
626 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
627 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
628 dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
630 /* Try the same with unbind */
631 ut_assertok(device_unbind(top[2]));
632 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
633 dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0;
635 /* Try one with grandchildren */
636 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
637 ut_asserteq_ptr(dev, top[6]);
638 ut_assertok(device_unbind(top[5]));
639 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
640 dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
644 DM_TEST(dm_test_children, 0);
646 /* Test that pre-relocation devices work as expected */
647 static int dm_test_pre_reloc(struct unit_test_state *uts)
649 struct dm_test_state *dms = uts->priv;
652 /* The normal driver should refuse to bind before relocation */
653 ut_asserteq(-EPERM, device_bind_by_name(dms->root, true,
654 &driver_info_manual, &dev));
656 /* But this one is marked pre-reloc */
657 ut_assertok(device_bind_by_name(dms->root, true,
658 &driver_info_pre_reloc, &dev));
662 DM_TEST(dm_test_pre_reloc, 0);
665 * Test that removal of devices, either via the "normal" device_remove()
666 * API or via the device driver selective flag works as expected
668 static int dm_test_remove_active_dma(struct unit_test_state *uts)
670 struct dm_test_state *dms = uts->priv;
673 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_act_dma,
677 /* Probe the device */
678 ut_assertok(device_probe(dev));
680 /* Test if device is active right now */
681 ut_asserteq(true, device_active(dev));
683 /* Remove the device via selective remove flag */
684 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
686 /* Test if device is inactive right now */
687 ut_asserteq(false, device_active(dev));
689 /* Probe the device again */
690 ut_assertok(device_probe(dev));
692 /* Test if device is active right now */
693 ut_asserteq(true, device_active(dev));
695 /* Remove the device via "normal" remove API */
696 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
698 /* Test if device is inactive right now */
699 ut_asserteq(false, device_active(dev));
702 * Test if a device without the active DMA flags is not removed upon
703 * the active DMA remove call
705 ut_assertok(device_unbind(dev));
706 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
710 /* Probe the device */
711 ut_assertok(device_probe(dev));
713 /* Test if device is active right now */
714 ut_asserteq(true, device_active(dev));
716 /* Remove the device via selective remove flag */
717 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
719 /* Test if device is still active right now */
720 ut_asserteq(true, device_active(dev));
724 DM_TEST(dm_test_remove_active_dma, 0);
726 static int dm_test_uclass_before_ready(struct unit_test_state *uts)
730 ut_assertok(uclass_get(UCLASS_TEST, &uc));
733 gd->dm_root_f = NULL;
734 memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root));
736 ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST));
740 DM_TEST(dm_test_uclass_before_ready, 0);
742 static int dm_test_uclass_devices_find(struct unit_test_state *uts)
747 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
749 ret = uclass_find_next_device(&dev)) {
751 ut_assertnonnull(dev);
754 ut_assertok(uclass_find_first_device(UCLASS_TEST_DUMMY, &dev));
759 DM_TEST(dm_test_uclass_devices_find, DM_TESTF_SCAN_PDATA);
761 static int dm_test_uclass_devices_find_by_name(struct unit_test_state *uts)
763 struct udevice *finddev;
764 struct udevice *testdev;
768 * For each test device found in fdt like: "a-test", "b-test", etc.,
769 * use its name and try to find it by uclass_find_device_by_name().
770 * Then, on success check if:
771 * - current 'testdev' name is equal to the returned 'finddev' name
772 * - current 'testdev' pointer is equal to the returned 'finddev'
774 * We assume that, each uclass's device name is unique, so if not, then
775 * this will fail on checking condition: testdev == finddev, since the
776 * uclass_find_device_by_name(), returns the first device by given name.
778 for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev);
780 ret = uclass_find_next_device(&testdev)) {
782 ut_assertnonnull(testdev);
784 findret = uclass_find_device_by_name(UCLASS_TEST_FDT,
788 ut_assertok(findret);
790 ut_asserteq_str(testdev->name, finddev->name);
791 ut_asserteq_ptr(testdev, finddev);
796 DM_TEST(dm_test_uclass_devices_find_by_name, DM_TESTF_SCAN_FDT);
798 static int dm_test_uclass_devices_get(struct unit_test_state *uts)
803 for (ret = uclass_first_device(UCLASS_TEST, &dev);
805 ret = uclass_next_device(&dev)) {
808 ut_assert(device_active(dev));
813 DM_TEST(dm_test_uclass_devices_get, DM_TESTF_SCAN_PDATA);
815 static int dm_test_uclass_devices_get_by_name(struct unit_test_state *uts)
817 struct udevice *finddev;
818 struct udevice *testdev;
822 * For each test device found in fdt like: "a-test", "b-test", etc.,
823 * use its name and try to get it by uclass_get_device_by_name().
824 * On success check if:
825 * - returned finddev' is active
826 * - current 'testdev' name is equal to the returned 'finddev' name
827 * - current 'testdev' pointer is equal to the returned 'finddev'
829 * We asserts that the 'testdev' is active on each loop entry, so we
830 * could be sure that the 'finddev' is activated too, but for sure
833 * We assume that, each uclass's device name is unique, so if not, then
834 * this will fail on checking condition: testdev == finddev, since the
835 * uclass_get_device_by_name(), returns the first device by given name.
837 for (ret = uclass_first_device(UCLASS_TEST_FDT, &testdev);
839 ret = uclass_next_device(&testdev)) {
842 ut_assert(device_active(testdev));
844 findret = uclass_get_device_by_name(UCLASS_TEST_FDT,
848 ut_assertok(findret);
850 ut_assert(device_active(finddev));
851 ut_asserteq_str(testdev->name, finddev->name);
852 ut_asserteq_ptr(testdev, finddev);
857 DM_TEST(dm_test_uclass_devices_get_by_name, DM_TESTF_SCAN_FDT);
859 static int dm_test_device_get_uclass_id(struct unit_test_state *uts)
863 ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev));
864 ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev));
868 DM_TEST(dm_test_device_get_uclass_id, DM_TESTF_SCAN_PDATA);
870 static int dm_test_uclass_names(struct unit_test_state *uts)
872 ut_asserteq_str("test", uclass_get_name(UCLASS_TEST));
873 ut_asserteq(UCLASS_TEST, uclass_get_by_name("test"));
877 DM_TEST(dm_test_uclass_names, DM_TESTF_SCAN_PDATA);
879 static int dm_test_inactive_child(struct unit_test_state *uts)
881 struct dm_test_state *dms = uts->priv;
882 struct udevice *parent, *dev1, *dev2;
884 /* Skip the behaviour in test_post_probe() */
885 dms->skip_post_probe = 1;
887 ut_assertok(uclass_first_device_err(UCLASS_TEST, &parent));
890 * Create a child but do not activate it. Calling the function again
891 * should return the same child.
893 ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
894 UCLASS_TEST, &dev1));
895 ut_assertok(device_bind_ofnode(parent, DM_GET_DRIVER(test_drv),
896 "test_child", 0, ofnode_null(), &dev1));
898 ut_assertok(device_find_first_inactive_child(parent, UCLASS_TEST,
900 ut_asserteq_ptr(dev1, dev2);
902 ut_assertok(device_probe(dev1));
903 ut_asserteq(-ENODEV, device_find_first_inactive_child(parent,
904 UCLASS_TEST, &dev2));
908 DM_TEST(dm_test_inactive_child, DM_TESTF_SCAN_PDATA);