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