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