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 = (r' *{: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 tree = u_boot_console.run_command('dm tree')
29 assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
30 assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False)
31 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
33 #Unbind child #1. No error expected and all devices should be there except for bind-test-child1
34 response = u_boot_console.run_command('unbind /bind-test/bind-test-child1')
36 tree = u_boot_console.run_command('dm tree')
37 assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
38 assert 'bind-test-child1' not in tree
39 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
41 #bind child #1. No error expected and all devices should be there
42 response = u_boot_console.run_command('bind /bind-test/bind-test-child1 phy_sandbox')
44 tree = u_boot_console.run_command('dm tree')
45 assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
46 assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, True)
47 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, False)
49 #Unbind child #2. No error expected and all devices should be there except for bind-test-child2
50 response = u_boot_console.run_command('unbind /bind-test/bind-test-child2')
52 tree = u_boot_console.run_command('dm tree')
53 assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
54 assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, True)
55 assert 'bind-test-child2' not in tree
58 #Bind child #2. No error expected and all devices should be there
59 response = u_boot_console.run_command('bind /bind-test/bind-test-child2 simple_bus')
61 tree = u_boot_console.run_command('dm tree')
62 assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
63 assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False)
64 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
66 #Unbind parent. No error expected. All devices should be removed and unbound
67 response = u_boot_console.run_command('unbind /bind-test')
69 tree = u_boot_console.run_command('dm tree')
70 assert 'bind-test' not in tree
71 assert 'bind-test-child1' not in tree
72 assert 'bind-test-child2' not in tree
74 #try binding invalid node with valid driver
75 response = u_boot_console.run_command('bind /not-a-valid-node simple_bus')
77 tree = u_boot_console.run_command('dm tree')
78 assert 'not-a-valid-node' not in tree
80 #try binding valid node with invalid driver
81 response = u_boot_console.run_command('bind /bind-test not_a_driver')
83 tree = u_boot_console.run_command('dm tree')
84 assert 'bind-test' not in tree
86 #bind /bind-test. Device should come up as well as its children
87 response = u_boot_console.run_command('bind /bind-test simple_bus')
89 tree = u_boot_console.run_command('dm tree')
90 assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
91 assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False)
92 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
94 response = u_boot_console.run_command('unbind /bind-test')
97 def get_next_line(tree, name):
98 treelines = [x.strip() for x in tree.splitlines() if x.strip()]
100 for idx, line in enumerate(treelines):
101 if ('-- ' + name) in line:
103 child_line = treelines[idx+1]
109 @pytest.mark.buildconfigspec('cmd_bind')
110 def test_bind_unbind_with_uclass(u_boot_console):
112 response = u_boot_console.run_command('bind /bind-test simple_bus')
113 assert response == ''
115 #make sure bind-test-child2 is there and get its uclass/index pair
116 tree = u_boot_console.run_command('dm tree')
117 child2_line = [x.strip() for x in tree.splitlines() if '-- bind-test-child2' in x]
118 assert len(child2_line) == 1
120 child2_uclass = child2_line[0].split()[0]
121 child2_index = int(child2_line[0].split()[1])
123 #bind simple_bus as a child of bind-test-child2
124 response = u_boot_console.run_command('bind {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus'))
126 #check that the child is there and its uclass/index pair is right
127 tree = u_boot_console.run_command('dm tree')
129 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
130 assert child_of_child2_line
131 child_of_child2_index = int(child_of_child2_line.split()[1])
132 assert in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True)
133 assert child_of_child2_index == child2_index + 1
135 #unbind the child and check it has been removed
136 response = u_boot_console.run_command('unbind simple_bus {}'.format(child_of_child2_index))
137 assert response == ''
138 tree = u_boot_console.run_command('dm tree')
139 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
140 assert not in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True)
141 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
142 assert child_of_child2_line == ''
144 #bind simple_bus as a child of bind-test-child2
145 response = u_boot_console.run_command('bind {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus'))
147 #check that the child is there and its uclass/index pair is right
148 tree = u_boot_console.run_command('dm tree')
149 treelines = [x.strip() for x in tree.splitlines() if x.strip()]
151 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
152 assert child_of_child2_line
153 child_of_child2_index = int(child_of_child2_line.split()[1])
154 assert in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True)
155 assert child_of_child2_index == child2_index + 1
157 #unbind the child and check it has been removed
158 response = u_boot_console.run_command('unbind {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus'))
159 assert response == ''
161 tree = u_boot_console.run_command('dm tree')
162 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
164 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
165 assert child_of_child2_line == ''
167 #unbind the child again and check it doesn't change the tree
168 tree_old = u_boot_console.run_command('dm tree')
169 response = u_boot_console.run_command('unbind {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus'))
170 tree_new = u_boot_console.run_command('dm tree')
172 assert response == ''
173 assert tree_old == tree_new
175 response = u_boot_console.run_command('unbind /bind-test')
176 assert response == ''