blob: 6e1feb80ae0d72159d1941e24954f59e9628b30a [file] [log] [blame]
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +02001# SPDX-License-Identifier: GPL-2.0
2# Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
3
4import os.path
5import pytest
6import re
7
8def in_tree(response, name, uclass, drv, depth, last_child):
9 lines = [x.strip() for x in response.splitlines()]
10 leaf = ' ' * 4 * depth;
11 if not last_child:
12 leaf = leaf + '\|'
13 else:
14 leaf = leaf + '`'
15 leaf = leaf + '-- ' + name
Liviu Dudau5197daf2018-10-15 10:03:06 +010016 line = ' *{:10.10} [0-9]* \[ [ +] \] {:20.20} {}$'.format(uclass, drv, leaf)
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020017 prog = re.compile(line)
18 for l in lines:
19 if prog.match(l):
20 return True
21 return False
22
23
24@pytest.mark.buildconfigspec('cmd_bind')
25def test_bind_unbind_with_node(u_boot_console):
26
27 #bind /bind-test. Device should come up as well as its children
Simon Glass871bf7d2018-12-27 08:11:13 -070028 response = u_boot_console.run_command('bind /bind-test generic_simple_bus')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020029 assert response == ''
Simon Glass871bf7d2018-12-27 08:11:13 -070030 tree = u_boot_console.run_command('dm tree')
31 assert in_tree(tree, 'bind-test', 'simple_bus', 'generic_simple_bus', 0, True)
32 assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False)
33 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'generic_simple_bus', 1, True)
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020034
35 #Unbind child #1. No error expected and all devices should be there except for bind-test-child1
Simon Glass871bf7d2018-12-27 08:11:13 -070036 response = u_boot_console.run_command('unbind /bind-test/bind-test-child1')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020037 assert response == ''
Simon Glass871bf7d2018-12-27 08:11:13 -070038 tree = u_boot_console.run_command('dm tree')
39 assert in_tree(tree, 'bind-test', 'simple_bus', 'generic_simple_bus', 0, True)
40 assert 'bind-test-child1' not in tree
41 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'generic_simple_bus', 1, True)
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020042
43 #bind child #1. No error expected and all devices should be there
Simon Glass871bf7d2018-12-27 08:11:13 -070044 response = u_boot_console.run_command('bind /bind-test/bind-test-child1 phy_sandbox')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020045 assert response == ''
Simon Glass871bf7d2018-12-27 08:11:13 -070046 tree = u_boot_console.run_command('dm tree')
47 assert in_tree(tree, 'bind-test', 'simple_bus', 'generic_simple_bus', 0, True)
48 assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, True)
49 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'generic_simple_bus', 1, False)
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020050
51 #Unbind child #2. No error expected and all devices should be there except for bind-test-child2
Simon Glass871bf7d2018-12-27 08:11:13 -070052 response = u_boot_console.run_command('unbind /bind-test/bind-test-child2')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020053 assert response == ''
Simon Glass871bf7d2018-12-27 08:11:13 -070054 tree = u_boot_console.run_command('dm tree')
55 assert in_tree(tree, 'bind-test', 'simple_bus', 'generic_simple_bus', 0, True)
56 assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, True)
57 assert 'bind-test-child2' not in tree
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020058
59
60 #Bind child #2. No error expected and all devices should be there
Simon Glass871bf7d2018-12-27 08:11:13 -070061 response = u_boot_console.run_command('bind /bind-test/bind-test-child2 generic_simple_bus')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020062 assert response == ''
Simon Glass871bf7d2018-12-27 08:11:13 -070063 tree = u_boot_console.run_command('dm tree')
64 assert in_tree(tree, 'bind-test', 'simple_bus', 'generic_simple_bus', 0, True)
65 assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False)
66 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'generic_simple_bus', 1, True)
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020067
68 #Unbind parent. No error expected. All devices should be removed and unbound
Simon Glass871bf7d2018-12-27 08:11:13 -070069 response = u_boot_console.run_command('unbind /bind-test')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020070 assert response == ''
Simon Glass871bf7d2018-12-27 08:11:13 -070071 tree = u_boot_console.run_command('dm tree')
72 assert 'bind-test' not in tree
73 assert 'bind-test-child1' not in tree
74 assert 'bind-test-child2' not in tree
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020075
76 #try binding invalid node with valid driver
Simon Glass871bf7d2018-12-27 08:11:13 -070077 response = u_boot_console.run_command('bind /not-a-valid-node generic_simple_bus')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020078 assert response != ''
Simon Glass871bf7d2018-12-27 08:11:13 -070079 tree = u_boot_console.run_command('dm tree')
80 assert 'not-a-valid-node' not in tree
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020081
82 #try binding valid node with invalid driver
Simon Glass871bf7d2018-12-27 08:11:13 -070083 response = u_boot_console.run_command('bind /bind-test not_a_driver')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020084 assert response != ''
Simon Glass871bf7d2018-12-27 08:11:13 -070085 tree = u_boot_console.run_command('dm tree')
86 assert 'bind-test' not in tree
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020087
88 #bind /bind-test. Device should come up as well as its children
Simon Glass871bf7d2018-12-27 08:11:13 -070089 response = u_boot_console.run_command('bind /bind-test generic_simple_bus')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020090 assert response == ''
Simon Glass871bf7d2018-12-27 08:11:13 -070091 tree = u_boot_console.run_command('dm tree')
92 assert in_tree(tree, 'bind-test', 'simple_bus', 'generic_simple_bus', 0, True)
93 assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False)
94 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'generic_simple_bus', 1, True)
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020095
Simon Glass871bf7d2018-12-27 08:11:13 -070096 response = u_boot_console.run_command('unbind /bind-test')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020097 assert response == ''
98
99def get_next_line(tree, name):
100 treelines = [x.strip() for x in tree.splitlines() if x.strip()]
Simon Glass871bf7d2018-12-27 08:11:13 -0700101 child_line = ''
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200102 for idx, line in enumerate(treelines):
Simon Glass871bf7d2018-12-27 08:11:13 -0700103 if ('-- ' + name) in line:
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200104 try:
105 child_line = treelines[idx+1]
106 except:
107 pass
108 break
109 return child_line
110
111@pytest.mark.buildconfigspec('cmd_bind')
112def test_bind_unbind_with_uclass(u_boot_console):
113 #bind /bind-test
Simon Glass871bf7d2018-12-27 08:11:13 -0700114 response = u_boot_console.run_command('bind /bind-test generic_simple_bus')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200115 assert response == ''
116
117 #make sure bind-test-child2 is there and get its uclass/index pair
Simon Glass871bf7d2018-12-27 08:11:13 -0700118 tree = u_boot_console.run_command('dm tree')
119 child2_line = [x.strip() for x in tree.splitlines() if '-- bind-test-child2' in x]
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200120 assert len(child2_line) == 1
121
122 child2_uclass = child2_line[0].split()[0]
123 child2_index = int(child2_line[0].split()[1])
124
125 #bind generic_simple_bus as a child of bind-test-child2
Simon Glass871bf7d2018-12-27 08:11:13 -0700126 response = u_boot_console.run_command('bind {} {} generic_simple_bus'.format(child2_uclass, child2_index, 'generic_simple_bus'))
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200127
128 #check that the child is there and its uclass/index pair is right
Simon Glass871bf7d2018-12-27 08:11:13 -0700129 tree = u_boot_console.run_command('dm tree')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200130
Simon Glass871bf7d2018-12-27 08:11:13 -0700131 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200132 assert child_of_child2_line
133 child_of_child2_index = int(child_of_child2_line.split()[1])
Simon Glass871bf7d2018-12-27 08:11:13 -0700134 assert in_tree(tree, 'generic_simple_bus', 'simple_bus', 'generic_simple_bus', 2, True)
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200135 assert child_of_child2_index == child2_index + 1
136
137 #unbind the child and check it has been removed
Simon Glass871bf7d2018-12-27 08:11:13 -0700138 response = u_boot_console.run_command('unbind simple_bus {}'.format(child_of_child2_index))
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200139 assert response == ''
Simon Glass871bf7d2018-12-27 08:11:13 -0700140 tree = u_boot_console.run_command('dm tree')
141 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'generic_simple_bus', 1, True)
142 assert not in_tree(tree, 'generic_simple_bus', 'simple_bus', 'generic_simple_bus', 2, True)
143 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
144 assert child_of_child2_line == ''
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200145
146 #bind generic_simple_bus as a child of bind-test-child2
Simon Glass871bf7d2018-12-27 08:11:13 -0700147 response = u_boot_console.run_command('bind {} {} generic_simple_bus'.format(child2_uclass, child2_index, 'generic_simple_bus'))
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200148
149 #check that the child is there and its uclass/index pair is right
Simon Glass871bf7d2018-12-27 08:11:13 -0700150 tree = u_boot_console.run_command('dm tree')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200151 treelines = [x.strip() for x in tree.splitlines() if x.strip()]
152
Simon Glass871bf7d2018-12-27 08:11:13 -0700153 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200154 assert child_of_child2_line
155 child_of_child2_index = int(child_of_child2_line.split()[1])
Simon Glass871bf7d2018-12-27 08:11:13 -0700156 assert in_tree(tree, 'generic_simple_bus', 'simple_bus', 'generic_simple_bus', 2, True)
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200157 assert child_of_child2_index == child2_index + 1
158
159 #unbind the child and check it has been removed
Simon Glass871bf7d2018-12-27 08:11:13 -0700160 response = u_boot_console.run_command('unbind {} {} generic_simple_bus'.format(child2_uclass, child2_index, 'generic_simple_bus'))
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200161 assert response == ''
162
Simon Glass871bf7d2018-12-27 08:11:13 -0700163 tree = u_boot_console.run_command('dm tree')
164 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'generic_simple_bus', 1, True)
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200165
Simon Glass871bf7d2018-12-27 08:11:13 -0700166 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
167 assert child_of_child2_line == ''
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200168
169 #unbind the child again and check it doesn't change the tree
Simon Glass871bf7d2018-12-27 08:11:13 -0700170 tree_old = u_boot_console.run_command('dm tree')
171 response = u_boot_console.run_command('unbind {} {} generic_simple_bus'.format(child2_uclass, child2_index, 'generic_simple_bus'))
172 tree_new = u_boot_console.run_command('dm tree')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200173
174 assert response == ''
175 assert tree_old == tree_new
176
Simon Glass871bf7d2018-12-27 08:11:13 -0700177 response = u_boot_console.run_command('unbind /bind-test')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200178 assert response == ''