blob: 9f234fb635077d30f3a6549a95e5f667177c97ee [file] [log] [blame]
Jean-Jacques Hiblote83a31b2018-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()]
Patrice Chotardf0c9d1a2020-07-28 09:13:34 +020010 leaf = ''
11 if depth != 0:
12 leaf = ' ' + ' ' * (depth - 1) ;
13 if not last_child:
14 leaf = leaf + r'\|'
15 else:
16 leaf = leaf + '`'
17
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020018 leaf = leaf + '-- ' + name
Pratyush Yadav4ddc2a42020-09-24 10:04:17 +053019 line = (r' *{:10.10} *[0-9]* \[ [ +] \] {:20.20} [` |]{}$'
Simon Glassdf604242018-12-05 18:42:52 -070020 .format(uclass, drv, leaf))
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020021 prog = re.compile(line)
22 for l in lines:
23 if prog.match(l):
24 return True
25 return False
26
27
28@pytest.mark.buildconfigspec('cmd_bind')
29def test_bind_unbind_with_node(u_boot_console):
30
Simon Glasse9f4d872018-12-27 08:11:13 -070031 tree = u_boot_console.run_command('dm tree')
Walter Lozano2901ac62020-06-25 01:10:04 -030032 assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
Simon Glasse9f4d872018-12-27 08:11:13 -070033 assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False)
Walter Lozano2901ac62020-06-25 01:10:04 -030034 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020035
Patrice Chotard4ffd03a2021-09-10 16:16:24 +020036 #bind usb_ether driver (which has no compatible) to usb@1 node.
37 ##New entry usb_ether should appear in the dm tree
38 response = u_boot_console.run_command('bind /usb@1 usb_ether')
39 assert response == ''
40 tree = u_boot_console.run_command('dm tree')
41 assert in_tree(tree, 'usb@1', 'ethernet', 'usb_ether', 1, True)
42
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020043 #Unbind child #1. No error expected and all devices should be there except for bind-test-child1
Simon Glasse9f4d872018-12-27 08:11:13 -070044 response = u_boot_console.run_command('unbind /bind-test/bind-test-child1')
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020045 assert response == ''
Simon Glasse9f4d872018-12-27 08:11:13 -070046 tree = u_boot_console.run_command('dm tree')
Walter Lozano2901ac62020-06-25 01:10:04 -030047 assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
Simon Glasse9f4d872018-12-27 08:11:13 -070048 assert 'bind-test-child1' not in tree
Walter Lozano2901ac62020-06-25 01:10:04 -030049 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020050
51 #bind child #1. No error expected and all devices should be there
Simon Glasse9f4d872018-12-27 08:11:13 -070052 response = u_boot_console.run_command('bind /bind-test/bind-test-child1 phy_sandbox')
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020053 assert response == ''
Simon Glasse9f4d872018-12-27 08:11:13 -070054 tree = u_boot_console.run_command('dm tree')
Walter Lozano2901ac62020-06-25 01:10:04 -030055 assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
Simon Glasse9f4d872018-12-27 08:11:13 -070056 assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, True)
Walter Lozano2901ac62020-06-25 01:10:04 -030057 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, False)
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020058
59 #Unbind child #2. No error expected and all devices should be there except for bind-test-child2
Simon Glasse9f4d872018-12-27 08:11:13 -070060 response = u_boot_console.run_command('unbind /bind-test/bind-test-child2')
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020061 assert response == ''
Simon Glasse9f4d872018-12-27 08:11:13 -070062 tree = u_boot_console.run_command('dm tree')
Walter Lozano2901ac62020-06-25 01:10:04 -030063 assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
Simon Glasse9f4d872018-12-27 08:11:13 -070064 assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, True)
65 assert 'bind-test-child2' not in tree
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020066
67
68 #Bind child #2. No error expected and all devices should be there
Walter Lozano2901ac62020-06-25 01:10:04 -030069 response = u_boot_console.run_command('bind /bind-test/bind-test-child2 simple_bus')
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020070 assert response == ''
Simon Glasse9f4d872018-12-27 08:11:13 -070071 tree = u_boot_console.run_command('dm tree')
Walter Lozano2901ac62020-06-25 01:10:04 -030072 assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
Simon Glasse9f4d872018-12-27 08:11:13 -070073 assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False)
Walter Lozano2901ac62020-06-25 01:10:04 -030074 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020075
76 #Unbind parent. No error expected. All devices should be removed and unbound
Simon Glasse9f4d872018-12-27 08:11:13 -070077 response = u_boot_console.run_command('unbind /bind-test')
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020078 assert response == ''
Simon Glasse9f4d872018-12-27 08:11:13 -070079 tree = u_boot_console.run_command('dm tree')
80 assert 'bind-test' not in tree
81 assert 'bind-test-child1' not in tree
82 assert 'bind-test-child2' not in tree
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020083
84 #try binding invalid node with valid driver
Walter Lozano2901ac62020-06-25 01:10:04 -030085 response = u_boot_console.run_command('bind /not-a-valid-node simple_bus')
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020086 assert response != ''
Simon Glasse9f4d872018-12-27 08:11:13 -070087 tree = u_boot_console.run_command('dm tree')
88 assert 'not-a-valid-node' not in tree
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020089
90 #try binding valid node with invalid driver
Simon Glasse9f4d872018-12-27 08:11:13 -070091 response = u_boot_console.run_command('bind /bind-test not_a_driver')
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020092 assert response != ''
Simon Glasse9f4d872018-12-27 08:11:13 -070093 tree = u_boot_console.run_command('dm tree')
94 assert 'bind-test' not in tree
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020095
96 #bind /bind-test. Device should come up as well as its children
Walter Lozano2901ac62020-06-25 01:10:04 -030097 response = u_boot_console.run_command('bind /bind-test simple_bus')
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020098 assert response == ''
Simon Glasse9f4d872018-12-27 08:11:13 -070099 tree = u_boot_console.run_command('dm tree')
Walter Lozano2901ac62020-06-25 01:10:04 -0300100 assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
Simon Glasse9f4d872018-12-27 08:11:13 -0700101 assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False)
Walter Lozano2901ac62020-06-25 01:10:04 -0300102 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200103
Simon Glasse9f4d872018-12-27 08:11:13 -0700104 response = u_boot_console.run_command('unbind /bind-test')
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200105 assert response == ''
106
107def get_next_line(tree, name):
108 treelines = [x.strip() for x in tree.splitlines() if x.strip()]
Simon Glasse9f4d872018-12-27 08:11:13 -0700109 child_line = ''
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200110 for idx, line in enumerate(treelines):
Simon Glasse9f4d872018-12-27 08:11:13 -0700111 if ('-- ' + name) in line:
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200112 try:
113 child_line = treelines[idx+1]
114 except:
115 pass
116 break
117 return child_line
118
119@pytest.mark.buildconfigspec('cmd_bind')
120def test_bind_unbind_with_uclass(u_boot_console):
121 #bind /bind-test
Walter Lozano2901ac62020-06-25 01:10:04 -0300122 response = u_boot_console.run_command('bind /bind-test simple_bus')
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200123 assert response == ''
124
125 #make sure bind-test-child2 is there and get its uclass/index pair
Simon Glasse9f4d872018-12-27 08:11:13 -0700126 tree = u_boot_console.run_command('dm tree')
127 child2_line = [x.strip() for x in tree.splitlines() if '-- bind-test-child2' in x]
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200128 assert len(child2_line) == 1
129
130 child2_uclass = child2_line[0].split()[0]
131 child2_index = int(child2_line[0].split()[1])
132
Walter Lozano2901ac62020-06-25 01:10:04 -0300133 #bind simple_bus as a child of bind-test-child2
134 response = u_boot_console.run_command('bind {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus'))
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200135
136 #check that the child is there and its uclass/index pair is right
Simon Glasse9f4d872018-12-27 08:11:13 -0700137 tree = u_boot_console.run_command('dm tree')
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200138
Simon Glasse9f4d872018-12-27 08:11:13 -0700139 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200140 assert child_of_child2_line
141 child_of_child2_index = int(child_of_child2_line.split()[1])
Walter Lozano2901ac62020-06-25 01:10:04 -0300142 assert in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True)
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200143 assert child_of_child2_index == child2_index + 1
144
145 #unbind the child and check it has been removed
Simon Glasse9f4d872018-12-27 08:11:13 -0700146 response = u_boot_console.run_command('unbind simple_bus {}'.format(child_of_child2_index))
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200147 assert response == ''
Simon Glasse9f4d872018-12-27 08:11:13 -0700148 tree = u_boot_console.run_command('dm tree')
Walter Lozano2901ac62020-06-25 01:10:04 -0300149 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
150 assert not in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True)
Simon Glasse9f4d872018-12-27 08:11:13 -0700151 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
152 assert child_of_child2_line == ''
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200153
Walter Lozano2901ac62020-06-25 01:10:04 -0300154 #bind simple_bus as a child of bind-test-child2
155 response = u_boot_console.run_command('bind {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus'))
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200156
157 #check that the child is there and its uclass/index pair is right
Simon Glasse9f4d872018-12-27 08:11:13 -0700158 tree = u_boot_console.run_command('dm tree')
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200159 treelines = [x.strip() for x in tree.splitlines() if x.strip()]
160
Simon Glasse9f4d872018-12-27 08:11:13 -0700161 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200162 assert child_of_child2_line
163 child_of_child2_index = int(child_of_child2_line.split()[1])
Walter Lozano2901ac62020-06-25 01:10:04 -0300164 assert in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True)
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200165 assert child_of_child2_index == child2_index + 1
166
167 #unbind the child and check it has been removed
Walter Lozano2901ac62020-06-25 01:10:04 -0300168 response = u_boot_console.run_command('unbind {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus'))
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200169 assert response == ''
170
Simon Glasse9f4d872018-12-27 08:11:13 -0700171 tree = u_boot_console.run_command('dm tree')
Walter Lozano2901ac62020-06-25 01:10:04 -0300172 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200173
Simon Glasse9f4d872018-12-27 08:11:13 -0700174 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
175 assert child_of_child2_line == ''
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200176
177 #unbind the child again and check it doesn't change the tree
Simon Glasse9f4d872018-12-27 08:11:13 -0700178 tree_old = u_boot_console.run_command('dm tree')
Walter Lozano2901ac62020-06-25 01:10:04 -0300179 response = u_boot_console.run_command('unbind {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus'))
Simon Glasse9f4d872018-12-27 08:11:13 -0700180 tree_new = u_boot_console.run_command('dm tree')
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200181
182 assert response == ''
183 assert tree_old == tree_new
184
Simon Glasse9f4d872018-12-27 08:11:13 -0700185 response = u_boot_console.run_command('unbind /bind-test')
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200186 assert response == ''