1 # SPDX-License-Identifier: GPL-2.0
2 # Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
8 def in_tree(response, name, uclass, drv, depth, last_child):
9 lines = [x.strip() for x in response.splitlines()]
10 leaf = ' ' * 4 * depth;
15 leaf = leaf + '-- ' + name
16 line = (' *{:10.10} [0-9]* \[ [ +] \] {:20.20} {}$'
17 .format(uclass, drv, leaf))
18 prog = re.compile(line)
25 @pytest.mark.buildconfigspec('cmd_bind')
26 def test_bind_unbind_with_node(u_boot_console):
28 #bind /bind-test. Device should come up as well as its children
29 response = u_boot_console.run_command("bind /bind-test generic_simple_bus")
31 tree = u_boot_console.run_command("dm tree")
32 assert in_tree(tree, "bind-test", "simple_bus", "generic_simple_bus", 0, True)
33 assert in_tree(tree, "bind-test-child1", "phy", "phy_sandbox", 1, False)
34 assert in_tree(tree, "bind-test-child2", "simple_bus", "generic_simple_bus", 1, True)
36 #Unbind child #1. No error expected and all devices should be there except for bind-test-child1
37 response = u_boot_console.run_command("unbind /bind-test/bind-test-child1")
39 tree = u_boot_console.run_command("dm tree")
40 assert in_tree(tree, "bind-test", "simple_bus", "generic_simple_bus", 0, True)
41 assert "bind-test-child1" not in tree
42 assert in_tree(tree, "bind-test-child2", "simple_bus", "generic_simple_bus", 1, True)
44 #bind child #1. No error expected and all devices should be there
45 response = u_boot_console.run_command("bind /bind-test/bind-test-child1 phy_sandbox")
47 tree = u_boot_console.run_command("dm tree")
48 assert in_tree(tree, "bind-test", "simple_bus", "generic_simple_bus", 0, True)
49 assert in_tree(tree, "bind-test-child1", "phy", "phy_sandbox", 1, True)
50 assert in_tree(tree, "bind-test-child2", "simple_bus", "generic_simple_bus", 1, False)
52 #Unbind child #2. No error expected and all devices should be there except for bind-test-child2
53 response = u_boot_console.run_command("unbind /bind-test/bind-test-child2")
55 tree = u_boot_console.run_command("dm tree")
56 assert in_tree(tree, "bind-test", "simple_bus", "generic_simple_bus", 0, True)
57 assert in_tree(tree, "bind-test-child1", "phy", "phy_sandbox", 1, True)
58 assert "bind-test-child2" not in tree
61 #Bind child #2. No error expected and all devices should be there
62 response = u_boot_console.run_command("bind /bind-test/bind-test-child2 generic_simple_bus")
64 tree = u_boot_console.run_command("dm tree")
65 assert in_tree(tree, "bind-test", "simple_bus", "generic_simple_bus", 0, True)
66 assert in_tree(tree, "bind-test-child1", "phy", "phy_sandbox", 1, False)
67 assert in_tree(tree, "bind-test-child2", "simple_bus", "generic_simple_bus", 1, True)
69 #Unbind parent. No error expected. All devices should be removed and unbound
70 response = u_boot_console.run_command("unbind /bind-test")
72 tree = u_boot_console.run_command("dm tree")
73 assert "bind-test" not in tree
74 assert "bind-test-child1" not in tree
75 assert "bind-test-child2" not in tree
77 #try binding invalid node with valid driver
78 response = u_boot_console.run_command("bind /not-a-valid-node generic_simple_bus")
80 tree = u_boot_console.run_command("dm tree")
81 assert "not-a-valid-node" not in tree
83 #try binding valid node with invalid driver
84 response = u_boot_console.run_command("bind /bind-test not_a_driver")
86 tree = u_boot_console.run_command("dm tree")
87 assert "bind-test" not in tree
89 #bind /bind-test. Device should come up as well as its children
90 response = u_boot_console.run_command("bind /bind-test generic_simple_bus")
92 tree = u_boot_console.run_command("dm tree")
93 assert in_tree(tree, "bind-test", "simple_bus", "generic_simple_bus", 0, True)
94 assert in_tree(tree, "bind-test-child1", "phy", "phy_sandbox", 1, False)
95 assert in_tree(tree, "bind-test-child2", "simple_bus", "generic_simple_bus", 1, True)
97 response = u_boot_console.run_command("unbind /bind-test")
100 def get_next_line(tree, name):
101 treelines = [x.strip() for x in tree.splitlines() if x.strip()]
103 for idx, line in enumerate(treelines):
104 if ("-- " + name) in line:
106 child_line = treelines[idx+1]
112 @pytest.mark.buildconfigspec('cmd_bind')
113 def test_bind_unbind_with_uclass(u_boot_console):
115 response = u_boot_console.run_command("bind /bind-test generic_simple_bus")
116 assert response == ''
118 #make sure bind-test-child2 is there and get its uclass/index pair
119 tree = u_boot_console.run_command("dm tree")
120 child2_line = [x.strip() for x in tree.splitlines() if "-- bind-test-child2" in x]
121 assert len(child2_line) == 1
123 child2_uclass = child2_line[0].split()[0]
124 child2_index = int(child2_line[0].split()[1])
126 #bind generic_simple_bus as a child of bind-test-child2
127 response = u_boot_console.run_command("bind {} {} generic_simple_bus".format(child2_uclass, child2_index, "generic_simple_bus"))
129 #check that the child is there and its uclass/index pair is right
130 tree = u_boot_console.run_command("dm tree")
132 child_of_child2_line = get_next_line(tree, "bind-test-child2")
133 assert child_of_child2_line
134 child_of_child2_index = int(child_of_child2_line.split()[1])
135 assert in_tree(tree, "generic_simple_bus", "simple_bus", "generic_simple_bus", 2, True)
136 assert child_of_child2_index == child2_index + 1
138 #unbind the child and check it has been removed
139 response = u_boot_console.run_command("unbind simple_bus {}".format(child_of_child2_index))
140 assert response == ''
141 tree = u_boot_console.run_command("dm tree")
142 assert in_tree(tree, "bind-test-child2", "simple_bus", "generic_simple_bus", 1, True)
143 assert not in_tree(tree, "generic_simple_bus", "simple_bus", "generic_simple_bus", 2, True)
144 child_of_child2_line = get_next_line(tree, "bind-test-child2")
145 assert child_of_child2_line == ""
147 #bind generic_simple_bus as a child of bind-test-child2
148 response = u_boot_console.run_command("bind {} {} generic_simple_bus".format(child2_uclass, child2_index, "generic_simple_bus"))
150 #check that the child is there and its uclass/index pair is right
151 tree = u_boot_console.run_command("dm tree")
152 treelines = [x.strip() for x in tree.splitlines() if x.strip()]
154 child_of_child2_line = get_next_line(tree, "bind-test-child2")
155 assert child_of_child2_line
156 child_of_child2_index = int(child_of_child2_line.split()[1])
157 assert in_tree(tree, "generic_simple_bus", "simple_bus", "generic_simple_bus", 2, True)
158 assert child_of_child2_index == child2_index + 1
160 #unbind the child and check it has been removed
161 response = u_boot_console.run_command("unbind {} {} generic_simple_bus".format(child2_uclass, child2_index, "generic_simple_bus"))
162 assert response == ''
164 tree = u_boot_console.run_command("dm tree")
165 assert in_tree(tree, "bind-test-child2", "simple_bus", "generic_simple_bus", 1, True)
167 child_of_child2_line = get_next_line(tree, "bind-test-child2")
168 assert child_of_child2_line == ""
170 #unbind the child again and check it doesn't change the tree
171 tree_old = u_boot_console.run_command("dm tree")
172 response = u_boot_console.run_command("unbind {} {} generic_simple_bus".format(child2_uclass, child2_index, "generic_simple_bus"))
173 tree_new = u_boot_console.run_command("dm tree")
175 assert response == ''
176 assert tree_old == tree_new
178 response = u_boot_console.run_command("unbind /bind-test")
179 assert response == ''