Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0 |
| 2 | # Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. |
| 3 | |
| 4 | import os.path |
| 5 | import pytest |
| 6 | import re |
| 7 | |
| 8 | def in_tree(response, name, uclass, drv, depth, last_child): |
| 9 | lines = [x.strip() for x in response.splitlines()] |
Patrice Chotard | e37d4c4 | 2020-07-28 09:13:34 +0200 | [diff] [blame] | 10 | 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 Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 18 | leaf = leaf + '-- ' + name |
Patrice Chotard | e37d4c4 | 2020-07-28 09:13:34 +0200 | [diff] [blame] | 19 | line = (r' *{:10.10} [0-9]* \[ [ +] \] {:20.20} [` |]{}$' |
Simon Glass | 54d2cfe | 2018-12-05 18:42:52 -0700 | [diff] [blame] | 20 | .format(uclass, drv, leaf)) |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 21 | 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') |
| 29 | def test_bind_unbind_with_node(u_boot_console): |
| 30 | |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 31 | tree = u_boot_console.run_command('dm tree') |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 32 | assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True) |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 33 | assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False) |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 34 | assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True) |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 35 | |
| 36 | #Unbind child #1. No error expected and all devices should be there except for bind-test-child1 |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 37 | response = u_boot_console.run_command('unbind /bind-test/bind-test-child1') |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 38 | assert response == '' |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 39 | tree = u_boot_console.run_command('dm tree') |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 40 | assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True) |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 41 | assert 'bind-test-child1' not in tree |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 42 | assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True) |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 43 | |
| 44 | #bind child #1. No error expected and all devices should be there |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 45 | response = u_boot_console.run_command('bind /bind-test/bind-test-child1 phy_sandbox') |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 46 | assert response == '' |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 47 | tree = u_boot_console.run_command('dm tree') |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 48 | assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True) |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 49 | assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, True) |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 50 | assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, False) |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 51 | |
| 52 | #Unbind child #2. No error expected and all devices should be there except for bind-test-child2 |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 53 | response = u_boot_console.run_command('unbind /bind-test/bind-test-child2') |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 54 | assert response == '' |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 55 | tree = u_boot_console.run_command('dm tree') |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 56 | assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True) |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 57 | assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, True) |
| 58 | assert 'bind-test-child2' not in tree |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 59 | |
| 60 | |
| 61 | #Bind child #2. No error expected and all devices should be there |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 62 | response = u_boot_console.run_command('bind /bind-test/bind-test-child2 simple_bus') |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 63 | assert response == '' |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 64 | tree = u_boot_console.run_command('dm tree') |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 65 | assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True) |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 66 | assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False) |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 67 | assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True) |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 68 | |
| 69 | #Unbind parent. No error expected. All devices should be removed and unbound |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 70 | response = u_boot_console.run_command('unbind /bind-test') |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 71 | assert response == '' |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 72 | 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 Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 76 | |
| 77 | #try binding invalid node with valid driver |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 78 | response = u_boot_console.run_command('bind /not-a-valid-node simple_bus') |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 79 | assert response != '' |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 80 | tree = u_boot_console.run_command('dm tree') |
| 81 | assert 'not-a-valid-node' not in tree |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 82 | |
| 83 | #try binding valid node with invalid driver |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 84 | response = u_boot_console.run_command('bind /bind-test not_a_driver') |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 85 | assert response != '' |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 86 | tree = u_boot_console.run_command('dm tree') |
| 87 | assert 'bind-test' not in tree |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 88 | |
| 89 | #bind /bind-test. Device should come up as well as its children |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 90 | response = u_boot_console.run_command('bind /bind-test simple_bus') |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 91 | assert response == '' |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 92 | tree = u_boot_console.run_command('dm tree') |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 93 | assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True) |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 94 | assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False) |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 95 | assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True) |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 96 | |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 97 | response = u_boot_console.run_command('unbind /bind-test') |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 98 | assert response == '' |
| 99 | |
| 100 | def get_next_line(tree, name): |
| 101 | treelines = [x.strip() for x in tree.splitlines() if x.strip()] |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 102 | child_line = '' |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 103 | for idx, line in enumerate(treelines): |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 104 | if ('-- ' + name) in line: |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 105 | try: |
| 106 | child_line = treelines[idx+1] |
| 107 | except: |
| 108 | pass |
| 109 | break |
| 110 | return child_line |
| 111 | |
| 112 | @pytest.mark.buildconfigspec('cmd_bind') |
| 113 | def test_bind_unbind_with_uclass(u_boot_console): |
| 114 | #bind /bind-test |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 115 | response = u_boot_console.run_command('bind /bind-test simple_bus') |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 116 | assert response == '' |
| 117 | |
| 118 | #make sure bind-test-child2 is there and get its uclass/index pair |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 119 | 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 Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 121 | assert len(child2_line) == 1 |
| 122 | |
| 123 | child2_uclass = child2_line[0].split()[0] |
| 124 | child2_index = int(child2_line[0].split()[1]) |
| 125 | |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 126 | #bind simple_bus as a child of bind-test-child2 |
| 127 | response = u_boot_console.run_command('bind {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus')) |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 128 | |
| 129 | #check that the child is there and its uclass/index pair is right |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 130 | tree = u_boot_console.run_command('dm tree') |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 131 | |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 132 | child_of_child2_line = get_next_line(tree, 'bind-test-child2') |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 133 | assert child_of_child2_line |
| 134 | child_of_child2_index = int(child_of_child2_line.split()[1]) |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 135 | assert in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True) |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 136 | assert child_of_child2_index == child2_index + 1 |
| 137 | |
| 138 | #unbind the child and check it has been removed |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 139 | response = u_boot_console.run_command('unbind simple_bus {}'.format(child_of_child2_index)) |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 140 | assert response == '' |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 141 | tree = u_boot_console.run_command('dm tree') |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 142 | assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True) |
| 143 | assert not in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True) |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 144 | child_of_child2_line = get_next_line(tree, 'bind-test-child2') |
| 145 | assert child_of_child2_line == '' |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 146 | |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 147 | #bind simple_bus as a child of bind-test-child2 |
| 148 | response = u_boot_console.run_command('bind {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus')) |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 149 | |
| 150 | #check that the child is there and its uclass/index pair is right |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 151 | tree = u_boot_console.run_command('dm tree') |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 152 | treelines = [x.strip() for x in tree.splitlines() if x.strip()] |
| 153 | |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 154 | child_of_child2_line = get_next_line(tree, 'bind-test-child2') |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 155 | assert child_of_child2_line |
| 156 | child_of_child2_index = int(child_of_child2_line.split()[1]) |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 157 | assert in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True) |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 158 | assert child_of_child2_index == child2_index + 1 |
| 159 | |
| 160 | #unbind the child and check it has been removed |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 161 | response = u_boot_console.run_command('unbind {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus')) |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 162 | assert response == '' |
| 163 | |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 164 | tree = u_boot_console.run_command('dm tree') |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 165 | assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True) |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 166 | |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 167 | child_of_child2_line = get_next_line(tree, 'bind-test-child2') |
| 168 | assert child_of_child2_line == '' |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 169 | |
| 170 | #unbind the child again and check it doesn't change the tree |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 171 | tree_old = u_boot_console.run_command('dm tree') |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 172 | response = u_boot_console.run_command('unbind {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus')) |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 173 | tree_new = u_boot_console.run_command('dm tree') |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 174 | |
| 175 | assert response == '' |
| 176 | assert tree_old == tree_new |
| 177 | |
Simon Glass | 871bf7d | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 178 | response = u_boot_console.run_command('unbind /bind-test') |
Jean-Jacques Hiblot | 49c752c | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 179 | assert response == '' |