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 | |
| 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 | f0c9d1a | 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 | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 18 | leaf = leaf + '-- ' + name |
Pratyush Yadav | 4ddc2a4 | 2020-09-24 10:04:17 +0530 | [diff] [blame] | 19 | line = (r' *{:10.10} *[0-9]* \[ [ +] \] {:20.20} [` |]{}$' |
Simon Glass | df60424 | 2018-12-05 18:42:52 -0700 | [diff] [blame] | 20 | .format(uclass, drv, leaf)) |
Jean-Jacques Hiblot | e83a31b | 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 | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 31 | tree = u_boot_console.run_command('dm tree') |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 32 | assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True) |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 33 | assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False) |
Walter Lozano | 2901ac6 | 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 | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 35 | |
Patrice Chotard | 4ffd03a | 2021-09-10 16:16:24 +0200 | [diff] [blame] | 36 | #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 Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 43 | #Unbind child #1. No error expected and all devices should be there except for bind-test-child1 |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 44 | response = u_boot_console.run_command('unbind /bind-test/bind-test-child1') |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 45 | assert response == '' |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 46 | tree = u_boot_console.run_command('dm tree') |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 47 | assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True) |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 48 | assert 'bind-test-child1' not in tree |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 49 | 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] | 50 | |
| 51 | #bind child #1. No error expected and all devices should be there |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 52 | response = u_boot_console.run_command('bind /bind-test/bind-test-child1 phy_sandbox') |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 53 | assert response == '' |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 54 | tree = u_boot_console.run_command('dm tree') |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 55 | assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True) |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 56 | assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, True) |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 57 | 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] | 58 | |
| 59 | #Unbind child #2. No error expected and all devices should be there except for bind-test-child2 |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 60 | response = u_boot_console.run_command('unbind /bind-test/bind-test-child2') |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 61 | assert response == '' |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 62 | tree = u_boot_console.run_command('dm tree') |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 63 | assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True) |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 64 | assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, True) |
| 65 | assert 'bind-test-child2' not in tree |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 66 | |
| 67 | |
| 68 | #Bind child #2. No error expected and all devices should be there |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 69 | response = u_boot_console.run_command('bind /bind-test/bind-test-child2 simple_bus') |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 70 | assert response == '' |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 71 | tree = u_boot_console.run_command('dm tree') |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 72 | assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True) |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 73 | assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False) |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 74 | 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] | 75 | |
| 76 | #Unbind parent. No error expected. All devices should be removed and unbound |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 77 | response = u_boot_console.run_command('unbind /bind-test') |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 78 | assert response == '' |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 79 | 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 Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 83 | |
| 84 | #try binding invalid node with valid driver |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 85 | response = u_boot_console.run_command('bind /not-a-valid-node simple_bus') |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 86 | assert response != '' |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 87 | tree = u_boot_console.run_command('dm tree') |
| 88 | assert 'not-a-valid-node' not in tree |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 89 | |
| 90 | #try binding valid node with invalid driver |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 91 | response = u_boot_console.run_command('bind /bind-test not_a_driver') |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 92 | assert response != '' |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 93 | tree = u_boot_console.run_command('dm tree') |
| 94 | assert 'bind-test' not in tree |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 95 | |
| 96 | #bind /bind-test. Device should come up as well as its children |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 97 | response = u_boot_console.run_command('bind /bind-test simple_bus') |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 98 | assert response == '' |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 99 | tree = u_boot_console.run_command('dm tree') |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 100 | assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True) |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 101 | assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False) |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 102 | 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] | 103 | |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 104 | response = u_boot_console.run_command('unbind /bind-test') |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 105 | assert response == '' |
| 106 | |
| 107 | def get_next_line(tree, name): |
| 108 | treelines = [x.strip() for x in tree.splitlines() if x.strip()] |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 109 | child_line = '' |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 110 | for idx, line in enumerate(treelines): |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 111 | if ('-- ' + name) in line: |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 112 | try: |
| 113 | child_line = treelines[idx+1] |
| 114 | except: |
| 115 | pass |
| 116 | break |
| 117 | return child_line |
| 118 | |
| 119 | @pytest.mark.buildconfigspec('cmd_bind') |
| 120 | def test_bind_unbind_with_uclass(u_boot_console): |
| 121 | #bind /bind-test |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 122 | response = u_boot_console.run_command('bind /bind-test simple_bus') |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 123 | assert response == '' |
| 124 | |
| 125 | #make sure bind-test-child2 is there and get its uclass/index pair |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 126 | 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 Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 128 | 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 Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 133 | #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 Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 135 | |
| 136 | #check that the child is there and its uclass/index pair is right |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 137 | tree = u_boot_console.run_command('dm tree') |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 138 | |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 139 | child_of_child2_line = get_next_line(tree, 'bind-test-child2') |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 140 | assert child_of_child2_line |
| 141 | child_of_child2_index = int(child_of_child2_line.split()[1]) |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 142 | assert in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True) |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 143 | assert child_of_child2_index == child2_index + 1 |
| 144 | |
| 145 | #unbind the child and check it has been removed |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 146 | response = u_boot_console.run_command('unbind simple_bus {}'.format(child_of_child2_index)) |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 147 | assert response == '' |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 148 | tree = u_boot_console.run_command('dm tree') |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 149 | 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 Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 151 | child_of_child2_line = get_next_line(tree, 'bind-test-child2') |
| 152 | assert child_of_child2_line == '' |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 153 | |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 154 | #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 Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 156 | |
| 157 | #check that the child is there and its uclass/index pair is right |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 158 | tree = u_boot_console.run_command('dm tree') |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 159 | treelines = [x.strip() for x in tree.splitlines() if x.strip()] |
| 160 | |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 161 | child_of_child2_line = get_next_line(tree, 'bind-test-child2') |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 162 | assert child_of_child2_line |
| 163 | child_of_child2_index = int(child_of_child2_line.split()[1]) |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 164 | assert in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True) |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 165 | assert child_of_child2_index == child2_index + 1 |
| 166 | |
| 167 | #unbind the child and check it has been removed |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 168 | response = u_boot_console.run_command('unbind {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus')) |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 169 | assert response == '' |
| 170 | |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 171 | tree = u_boot_console.run_command('dm tree') |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 172 | 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] | 173 | |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 174 | child_of_child2_line = get_next_line(tree, 'bind-test-child2') |
| 175 | assert child_of_child2_line == '' |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 176 | |
| 177 | #unbind the child again and check it doesn't change the tree |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 178 | tree_old = u_boot_console.run_command('dm tree') |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 179 | response = u_boot_console.run_command('unbind {} {} simple_bus'.format(child2_uclass, child2_index, 'simple_bus')) |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 180 | tree_new = u_boot_console.run_command('dm tree') |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 181 | |
| 182 | assert response == '' |
| 183 | assert tree_old == tree_new |
| 184 | |
Simon Glass | e9f4d87 | 2018-12-27 08:11:13 -0700 | [diff] [blame] | 185 | response = u_boot_console.run_command('unbind /bind-test') |
Jean-Jacques Hiblot | e83a31b | 2018-08-09 16:17:46 +0200 | [diff] [blame] | 186 | assert response == '' |