blob: 20c6050342a9061ffb1bd33437a008be427259dd [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:
Tom Rini15579632019-10-24 11:59:28 -040012 leaf = leaf + r'\|'
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020013 else:
14 leaf = leaf + '`'
15 leaf = leaf + '-- ' + name
Tom Rini15579632019-10-24 11:59:28 -040016 line = (r' *{:10.10} [0-9]* \[ [ +] \] {:20.20} {}$'
Simon Glass54d2cfe2018-12-05 18:42:52 -070017 .format(uclass, drv, leaf))
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020018 prog = re.compile(line)
19 for l in lines:
20 if prog.match(l):
21 return True
22 return False
23
24
25@pytest.mark.buildconfigspec('cmd_bind')
26def test_bind_unbind_with_node(u_boot_console):
27
28 #bind /bind-test. Device should come up as well as its children
Simon Glass871bf7d2018-12-27 08:11:13 -070029 response = u_boot_console.run_command('bind /bind-test generic_simple_bus')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020030 assert response == ''
Simon Glass871bf7d2018-12-27 08:11:13 -070031 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)
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020035
36 #Unbind child #1. No error expected and all devices should be there except for bind-test-child1
Simon Glass871bf7d2018-12-27 08:11:13 -070037 response = u_boot_console.run_command('unbind /bind-test/bind-test-child1')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020038 assert response == ''
Simon Glass871bf7d2018-12-27 08:11:13 -070039 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)
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020043
44 #bind child #1. No error expected and all devices should be there
Simon Glass871bf7d2018-12-27 08:11:13 -070045 response = u_boot_console.run_command('bind /bind-test/bind-test-child1 phy_sandbox')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020046 assert response == ''
Simon Glass871bf7d2018-12-27 08:11:13 -070047 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)
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020051
52 #Unbind child #2. No error expected and all devices should be there except for bind-test-child2
Simon Glass871bf7d2018-12-27 08:11:13 -070053 response = u_boot_console.run_command('unbind /bind-test/bind-test-child2')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020054 assert response == ''
Simon Glass871bf7d2018-12-27 08:11:13 -070055 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
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020059
60
61 #Bind child #2. No error expected and all devices should be there
Simon Glass871bf7d2018-12-27 08:11:13 -070062 response = u_boot_console.run_command('bind /bind-test/bind-test-child2 generic_simple_bus')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020063 assert response == ''
Simon Glass871bf7d2018-12-27 08:11:13 -070064 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)
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020068
69 #Unbind parent. No error expected. All devices should be removed and unbound
Simon Glass871bf7d2018-12-27 08:11:13 -070070 response = u_boot_console.run_command('unbind /bind-test')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020071 assert response == ''
Simon Glass871bf7d2018-12-27 08:11:13 -070072 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
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020076
77 #try binding invalid node with valid driver
Simon Glass871bf7d2018-12-27 08:11:13 -070078 response = u_boot_console.run_command('bind /not-a-valid-node generic_simple_bus')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020079 assert response != ''
Simon Glass871bf7d2018-12-27 08:11:13 -070080 tree = u_boot_console.run_command('dm tree')
81 assert 'not-a-valid-node' not in tree
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020082
83 #try binding valid node with invalid driver
Simon Glass871bf7d2018-12-27 08:11:13 -070084 response = u_boot_console.run_command('bind /bind-test not_a_driver')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020085 assert response != ''
Simon Glass871bf7d2018-12-27 08:11:13 -070086 tree = u_boot_console.run_command('dm tree')
87 assert 'bind-test' not in tree
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020088
89 #bind /bind-test. Device should come up as well as its children
Simon Glass871bf7d2018-12-27 08:11:13 -070090 response = u_boot_console.run_command('bind /bind-test generic_simple_bus')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020091 assert response == ''
Simon Glass871bf7d2018-12-27 08:11:13 -070092 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)
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020096
Simon Glass871bf7d2018-12-27 08:11:13 -070097 response = u_boot_console.run_command('unbind /bind-test')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020098 assert response == ''
99
100def get_next_line(tree, name):
101 treelines = [x.strip() for x in tree.splitlines() if x.strip()]
Simon Glass871bf7d2018-12-27 08:11:13 -0700102 child_line = ''
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200103 for idx, line in enumerate(treelines):
Simon Glass871bf7d2018-12-27 08:11:13 -0700104 if ('-- ' + name) in line:
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200105 try:
106 child_line = treelines[idx+1]
107 except:
108 pass
109 break
110 return child_line
111
112@pytest.mark.buildconfigspec('cmd_bind')
113def test_bind_unbind_with_uclass(u_boot_console):
114 #bind /bind-test
Simon Glass871bf7d2018-12-27 08:11:13 -0700115 response = u_boot_console.run_command('bind /bind-test generic_simple_bus')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200116 assert response == ''
117
118 #make sure bind-test-child2 is there and get its uclass/index pair
Simon Glass871bf7d2018-12-27 08:11:13 -0700119 tree = u_boot_console.run_command('dm tree')
120 child2_line = [x.strip() for x in tree.splitlines() if '-- bind-test-child2' in x]
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200121 assert len(child2_line) == 1
122
123 child2_uclass = child2_line[0].split()[0]
124 child2_index = int(child2_line[0].split()[1])
125
126 #bind generic_simple_bus as a child of bind-test-child2
Simon Glass871bf7d2018-12-27 08:11:13 -0700127 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 +0200128
129 #check that the child is there and its uclass/index pair is right
Simon Glass871bf7d2018-12-27 08:11:13 -0700130 tree = u_boot_console.run_command('dm tree')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200131
Simon Glass871bf7d2018-12-27 08:11:13 -0700132 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200133 assert child_of_child2_line
134 child_of_child2_index = int(child_of_child2_line.split()[1])
Simon Glass871bf7d2018-12-27 08:11:13 -0700135 assert in_tree(tree, 'generic_simple_bus', 'simple_bus', 'generic_simple_bus', 2, True)
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200136 assert child_of_child2_index == child2_index + 1
137
138 #unbind the child and check it has been removed
Simon Glass871bf7d2018-12-27 08:11:13 -0700139 response = u_boot_console.run_command('unbind simple_bus {}'.format(child_of_child2_index))
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200140 assert response == ''
Simon Glass871bf7d2018-12-27 08:11:13 -0700141 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 == ''
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200146
147 #bind generic_simple_bus as a child of bind-test-child2
Simon Glass871bf7d2018-12-27 08:11:13 -0700148 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 +0200149
150 #check that the child is there and its uclass/index pair is right
Simon Glass871bf7d2018-12-27 08:11:13 -0700151 tree = u_boot_console.run_command('dm tree')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200152 treelines = [x.strip() for x in tree.splitlines() if x.strip()]
153
Simon Glass871bf7d2018-12-27 08:11:13 -0700154 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200155 assert child_of_child2_line
156 child_of_child2_index = int(child_of_child2_line.split()[1])
Simon Glass871bf7d2018-12-27 08:11:13 -0700157 assert in_tree(tree, 'generic_simple_bus', 'simple_bus', 'generic_simple_bus', 2, True)
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200158 assert child_of_child2_index == child2_index + 1
159
160 #unbind the child and check it has been removed
Simon Glass871bf7d2018-12-27 08:11:13 -0700161 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 +0200162 assert response == ''
163
Simon Glass871bf7d2018-12-27 08:11:13 -0700164 tree = u_boot_console.run_command('dm tree')
165 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'generic_simple_bus', 1, True)
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200166
Simon Glass871bf7d2018-12-27 08:11:13 -0700167 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
168 assert child_of_child2_line == ''
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200169
170 #unbind the child again and check it doesn't change the tree
Simon Glass871bf7d2018-12-27 08:11:13 -0700171 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')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200174
175 assert response == ''
176 assert tree_old == tree_new
177
Simon Glass871bf7d2018-12-27 08:11:13 -0700178 response = u_boot_console.run_command('unbind /bind-test')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200179 assert response == ''