blob: 1de125ce459cb5b328dcc3425bd6f3ae89fd6349 [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
Simon Glass871bf7d2018-12-27 08:11:13 -070028 tree = u_boot_console.run_command('dm tree')
Walter Lozanoe3e24702020-06-25 01:10:04 -030029 assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
Simon Glass871bf7d2018-12-27 08:11:13 -070030 assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False)
Walter Lozanoe3e24702020-06-25 01:10:04 -030031 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020032
33 #Unbind child #1. No error expected and all devices should be there except for bind-test-child1
Simon Glass871bf7d2018-12-27 08:11:13 -070034 response = u_boot_console.run_command('unbind /bind-test/bind-test-child1')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020035 assert response == ''
Simon Glass871bf7d2018-12-27 08:11:13 -070036 tree = u_boot_console.run_command('dm tree')
Walter Lozanoe3e24702020-06-25 01:10:04 -030037 assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
Simon Glass871bf7d2018-12-27 08:11:13 -070038 assert 'bind-test-child1' not in tree
Walter Lozanoe3e24702020-06-25 01:10:04 -030039 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020040
41 #bind child #1. No error expected and all devices should be there
Simon Glass871bf7d2018-12-27 08:11:13 -070042 response = u_boot_console.run_command('bind /bind-test/bind-test-child1 phy_sandbox')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020043 assert response == ''
Simon Glass871bf7d2018-12-27 08:11:13 -070044 tree = u_boot_console.run_command('dm tree')
Walter Lozanoe3e24702020-06-25 01:10:04 -030045 assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
Simon Glass871bf7d2018-12-27 08:11:13 -070046 assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, True)
Walter Lozanoe3e24702020-06-25 01:10:04 -030047 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, False)
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020048
49 #Unbind child #2. No error expected and all devices should be there except for bind-test-child2
Simon Glass871bf7d2018-12-27 08:11:13 -070050 response = u_boot_console.run_command('unbind /bind-test/bind-test-child2')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020051 assert response == ''
Simon Glass871bf7d2018-12-27 08:11:13 -070052 tree = u_boot_console.run_command('dm tree')
Walter Lozanoe3e24702020-06-25 01:10:04 -030053 assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
Simon Glass871bf7d2018-12-27 08:11:13 -070054 assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, True)
55 assert 'bind-test-child2' not in tree
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020056
57
58 #Bind child #2. No error expected and all devices should be there
Walter Lozanoe3e24702020-06-25 01:10:04 -030059 response = u_boot_console.run_command('bind /bind-test/bind-test-child2 simple_bus')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020060 assert response == ''
Simon Glass871bf7d2018-12-27 08:11:13 -070061 tree = u_boot_console.run_command('dm tree')
Walter Lozanoe3e24702020-06-25 01:10:04 -030062 assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
Simon Glass871bf7d2018-12-27 08:11:13 -070063 assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False)
Walter Lozanoe3e24702020-06-25 01:10:04 -030064 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020065
66 #Unbind parent. No error expected. All devices should be removed and unbound
Simon Glass871bf7d2018-12-27 08:11:13 -070067 response = u_boot_console.run_command('unbind /bind-test')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020068 assert response == ''
Simon Glass871bf7d2018-12-27 08:11:13 -070069 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
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020073
74 #try binding invalid node with valid driver
Walter Lozanoe3e24702020-06-25 01:10:04 -030075 response = u_boot_console.run_command('bind /not-a-valid-node simple_bus')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020076 assert response != ''
Simon Glass871bf7d2018-12-27 08:11:13 -070077 tree = u_boot_console.run_command('dm tree')
78 assert 'not-a-valid-node' not in tree
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020079
80 #try binding valid node with invalid driver
Simon Glass871bf7d2018-12-27 08:11:13 -070081 response = u_boot_console.run_command('bind /bind-test not_a_driver')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020082 assert response != ''
Simon Glass871bf7d2018-12-27 08:11:13 -070083 tree = u_boot_console.run_command('dm tree')
84 assert 'bind-test' not in tree
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020085
86 #bind /bind-test. Device should come up as well as its children
Walter Lozanoe3e24702020-06-25 01:10:04 -030087 response = u_boot_console.run_command('bind /bind-test simple_bus')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020088 assert response == ''
Simon Glass871bf7d2018-12-27 08:11:13 -070089 tree = u_boot_console.run_command('dm tree')
Walter Lozanoe3e24702020-06-25 01:10:04 -030090 assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
Simon Glass871bf7d2018-12-27 08:11:13 -070091 assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False)
Walter Lozanoe3e24702020-06-25 01:10:04 -030092 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020093
Simon Glass871bf7d2018-12-27 08:11:13 -070094 response = u_boot_console.run_command('unbind /bind-test')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +020095 assert response == ''
96
97def get_next_line(tree, name):
98 treelines = [x.strip() for x in tree.splitlines() if x.strip()]
Simon Glass871bf7d2018-12-27 08:11:13 -070099 child_line = ''
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200100 for idx, line in enumerate(treelines):
Simon Glass871bf7d2018-12-27 08:11:13 -0700101 if ('-- ' + name) in line:
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200102 try:
103 child_line = treelines[idx+1]
104 except:
105 pass
106 break
107 return child_line
108
109@pytest.mark.buildconfigspec('cmd_bind')
110def test_bind_unbind_with_uclass(u_boot_console):
111 #bind /bind-test
Walter Lozanoe3e24702020-06-25 01:10:04 -0300112 response = u_boot_console.run_command('bind /bind-test simple_bus')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200113 assert response == ''
114
115 #make sure bind-test-child2 is there and get its uclass/index pair
Simon Glass871bf7d2018-12-27 08:11:13 -0700116 tree = u_boot_console.run_command('dm tree')
117 child2_line = [x.strip() for x in tree.splitlines() if '-- bind-test-child2' in x]
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200118 assert len(child2_line) == 1
119
120 child2_uclass = child2_line[0].split()[0]
121 child2_index = int(child2_line[0].split()[1])
122
Walter Lozanoe3e24702020-06-25 01:10:04 -0300123 #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'))
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200125
126 #check that the child is there and its uclass/index pair is right
Simon Glass871bf7d2018-12-27 08:11:13 -0700127 tree = u_boot_console.run_command('dm tree')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200128
Simon Glass871bf7d2018-12-27 08:11:13 -0700129 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200130 assert child_of_child2_line
131 child_of_child2_index = int(child_of_child2_line.split()[1])
Walter Lozanoe3e24702020-06-25 01:10:04 -0300132 assert in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True)
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200133 assert child_of_child2_index == child2_index + 1
134
135 #unbind the child and check it has been removed
Simon Glass871bf7d2018-12-27 08:11:13 -0700136 response = u_boot_console.run_command('unbind simple_bus {}'.format(child_of_child2_index))
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200137 assert response == ''
Simon Glass871bf7d2018-12-27 08:11:13 -0700138 tree = u_boot_console.run_command('dm tree')
Walter Lozanoe3e24702020-06-25 01:10:04 -0300139 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)
Simon Glass871bf7d2018-12-27 08:11:13 -0700141 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
142 assert child_of_child2_line == ''
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200143
Walter Lozanoe3e24702020-06-25 01:10:04 -0300144 #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'))
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200146
147 #check that the child is there and its uclass/index pair is right
Simon Glass871bf7d2018-12-27 08:11:13 -0700148 tree = u_boot_console.run_command('dm tree')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200149 treelines = [x.strip() for x in tree.splitlines() if x.strip()]
150
Simon Glass871bf7d2018-12-27 08:11:13 -0700151 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200152 assert child_of_child2_line
153 child_of_child2_index = int(child_of_child2_line.split()[1])
Walter Lozanoe3e24702020-06-25 01:10:04 -0300154 assert in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True)
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200155 assert child_of_child2_index == child2_index + 1
156
157 #unbind the child and check it has been removed
Walter Lozanoe3e24702020-06-25 01:10:04 -0300158 response = u_boot_console.run_command('unbind {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus'))
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200159 assert response == ''
160
Simon Glass871bf7d2018-12-27 08:11:13 -0700161 tree = u_boot_console.run_command('dm tree')
Walter Lozanoe3e24702020-06-25 01:10:04 -0300162 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200163
Simon Glass871bf7d2018-12-27 08:11:13 -0700164 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
165 assert child_of_child2_line == ''
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200166
167 #unbind the child again and check it doesn't change the tree
Simon Glass871bf7d2018-12-27 08:11:13 -0700168 tree_old = u_boot_console.run_command('dm tree')
Walter Lozanoe3e24702020-06-25 01:10:04 -0300169 response = u_boot_console.run_command('unbind {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus'))
Simon Glass871bf7d2018-12-27 08:11:13 -0700170 tree_new = u_boot_console.run_command('dm tree')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200171
172 assert response == ''
173 assert tree_old == tree_new
174
Simon Glass871bf7d2018-12-27 08:11:13 -0700175 response = u_boot_console.run_command('unbind /bind-test')
Jean-Jacques Hiblot49c752c2018-08-09 16:17:46 +0200176 assert response == ''