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