blob: 16c63ae9684aacf505d3a406aea65601e29ca947 [file] [log] [blame]
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +02001# SPDX-License-Identifier: GPL-2.0
2# Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
3
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +02004""" Test for bind command """
5
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +02006import re
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +02007import pytest
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +02008
9def in_tree(response, name, uclass, drv, depth, last_child):
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020010 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 Chotardf0c9d1a2020-07-28 09:13:34 +020018
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020019 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 Hiblote83a31b2018-08-09 16:17:46 +020027
Michal Simek901f5ba2022-07-07 12:52:26 +020028@pytest.mark.boardspec('sandbox')
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020029@pytest.mark.buildconfigspec('cmd_bind')
Simon Glassddba5202025-02-09 09:07:14 -070030def test_bind_unbind_with_node(ubman):
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020031
Simon Glassddba5202025-02-09 09:07:14 -070032 tree = ubman.run_command('dm tree')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020033 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 Hiblote83a31b2018-08-09 16:17:46 +020036
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020037 #bind usb_ether driver (which has no compatible) to usb@1 node.
38 ##New entry usb_ether should appear in the dm tree
Simon Glassddba5202025-02-09 09:07:14 -070039 response = ubman.run_command('bind /usb@1 usb_ether')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020040 assert response == ''
Simon Glassddba5202025-02-09 09:07:14 -070041 tree = ubman.run_command('dm tree')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020042 assert in_tree(tree, 'usb@1', 'ethernet', 'usb_ether', 1, True)
Patrice Chotard4ffd03a2021-09-10 16:16:24 +020043
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020044 #Unbind child #1. No error expected and all devices should be there except for bind-test-child1
Simon Glassddba5202025-02-09 09:07:14 -070045 response = ubman.run_command('unbind /bind-test/bind-test-child1')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020046 assert response == ''
Simon Glassddba5202025-02-09 09:07:14 -070047 tree = ubman.run_command('dm tree')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020048 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 Hiblote83a31b2018-08-09 16:17:46 +020051
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020052 #bind child #1. No error expected and all devices should be there
Simon Glassddba5202025-02-09 09:07:14 -070053 response = ubman.run_command('bind /bind-test/bind-test-child1 phy_sandbox')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020054 assert response == ''
Simon Glassddba5202025-02-09 09:07:14 -070055 tree = ubman.run_command('dm tree')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020056 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 Hiblote83a31b2018-08-09 16:17:46 +020059
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020060 #Unbind child #2. No error expected and all devices should be there except for bind-test-child2
Simon Glassddba5202025-02-09 09:07:14 -070061 response = ubman.run_command('unbind /bind-test/bind-test-child2')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020062 assert response == ''
Simon Glassddba5202025-02-09 09:07:14 -070063 tree = ubman.run_command('dm tree')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020064 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 Hiblote83a31b2018-08-09 16:17:46 +020067
68
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020069 #Bind child #2. No error expected and all devices should be there
Simon Glassddba5202025-02-09 09:07:14 -070070 response = ubman.run_command('bind /bind-test/bind-test-child2 simple_bus')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020071 assert response == ''
Simon Glassddba5202025-02-09 09:07:14 -070072 tree = ubman.run_command('dm tree')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020073 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 Hiblote83a31b2018-08-09 16:17:46 +020076
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020077 #Unbind parent. No error expected. All devices should be removed and unbound
Simon Glassddba5202025-02-09 09:07:14 -070078 response = ubman.run_command('unbind /bind-test')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020079 assert response == ''
Simon Glassddba5202025-02-09 09:07:14 -070080 tree = ubman.run_command('dm tree')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020081 assert 'bind-test' not in tree
82 assert 'bind-test-child1' not in tree
83 assert 'bind-test-child2' not in tree
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020084
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020085 #try binding invalid node with valid driver
Simon Glassddba5202025-02-09 09:07:14 -070086 response = ubman.run_command('bind /not-a-valid-node simple_bus')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020087 assert response != ''
Simon Glassddba5202025-02-09 09:07:14 -070088 tree = ubman.run_command('dm tree')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020089 assert 'not-a-valid-node' not in tree
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020090
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020091 #try binding valid node with invalid driver
Simon Glassddba5202025-02-09 09:07:14 -070092 response = ubman.run_command('bind /bind-test not_a_driver')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020093 assert response != ''
Simon Glassddba5202025-02-09 09:07:14 -070094 tree = ubman.run_command('dm tree')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020095 assert 'bind-test' not in tree
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020096
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020097 #bind /bind-test. Device should come up as well as its children
Simon Glassddba5202025-02-09 09:07:14 -070098 response = ubman.run_command('bind /bind-test simple_bus')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020099 assert response == ''
Simon Glassddba5202025-02-09 09:07:14 -0700100 tree = ubman.run_command('dm tree')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200101 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 Hiblote83a31b2018-08-09 16:17:46 +0200104
Simon Glassddba5202025-02-09 09:07:14 -0700105 response = ubman.run_command('unbind /bind-test')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200106 assert response == ''
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200107
108def get_next_line(tree, name):
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200109 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 Hiblote83a31b2018-08-09 16:17:46 +0200119
Michal Simek901f5ba2022-07-07 12:52:26 +0200120@pytest.mark.boardspec('sandbox')
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200121@pytest.mark.buildconfigspec('cmd_bind')
Simon Glassae3db3d2022-08-06 17:51:48 -0600122@pytest.mark.singlethread
Simon Glassddba5202025-02-09 09:07:14 -0700123def test_bind_unbind_with_uclass(ubman):
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200124 #bind /bind-test
Simon Glassddba5202025-02-09 09:07:14 -0700125 response = ubman.run_command('bind /bind-test simple_bus')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200126 assert response == ''
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200127
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200128 #make sure bind-test-child2 is there and get its uclass/index pair
Simon Glassddba5202025-02-09 09:07:14 -0700129 tree = ubman.run_command('dm tree')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200130 child2_line = [x.strip() for x in tree.splitlines() if '-- bind-test-child2' in x]
131 assert len(child2_line) == 1
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200132
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200133 child2_uclass = child2_line[0].split()[0]
134 child2_index = int(child2_line[0].split()[1])
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200135
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200136 #bind simple_bus as a child of bind-test-child2
Simon Glassddba5202025-02-09 09:07:14 -0700137 response = ubman.run_command(
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200138 'bind {} {} simple_bus'.format(child2_uclass, child2_index))
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200139
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200140 #check that the child is there and its uclass/index pair is right
Simon Glassddba5202025-02-09 09:07:14 -0700141 tree = ubman.run_command('dm tree')
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200142
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200143 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
144 assert child_of_child2_line
145 child_of_child2_index = int(child_of_child2_line.split()[1])
146 assert in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True)
147 assert child_of_child2_index == child2_index + 1
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200148
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200149 #unbind the child and check it has been removed
Simon Glassddba5202025-02-09 09:07:14 -0700150 response = ubman.run_command('unbind simple_bus {}'.format(child_of_child2_index))
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200151 assert response == ''
Simon Glassddba5202025-02-09 09:07:14 -0700152 tree = ubman.run_command('dm tree')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200153 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
154 assert not in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True)
155 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
156 assert child_of_child2_line == ''
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200157
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200158 #bind simple_bus as a child of bind-test-child2
Simon Glassddba5202025-02-09 09:07:14 -0700159 response = ubman.run_command(
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200160 'bind {} {} simple_bus'.format(child2_uclass, child2_index))
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200161
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200162 #check that the child is there and its uclass/index pair is right
Simon Glassddba5202025-02-09 09:07:14 -0700163 tree = ubman.run_command('dm tree')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200164 treelines = [x.strip() for x in tree.splitlines() if x.strip()]
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200165
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200166 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
167 assert child_of_child2_line
168 child_of_child2_index = int(child_of_child2_line.split()[1])
169 assert in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True)
170 assert child_of_child2_index == child2_index + 1
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200171
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200172 #unbind the child and check it has been removed
Simon Glassddba5202025-02-09 09:07:14 -0700173 response = ubman.run_command(
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200174 'unbind {} {} simple_bus'.format(child2_uclass, child2_index))
175 assert response == ''
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200176
Simon Glassddba5202025-02-09 09:07:14 -0700177 tree = ubman.run_command('dm tree')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200178 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200179
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200180 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
181 assert child_of_child2_line == ''
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200182
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200183 #unbind the child again and check it doesn't change the tree
Simon Glassddba5202025-02-09 09:07:14 -0700184 tree_old = ubman.run_command('dm tree')
185 response = ubman.run_command(
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200186 'unbind {} {} simple_bus'.format(child2_uclass, child2_index))
Simon Glassddba5202025-02-09 09:07:14 -0700187 tree_new = ubman.run_command('dm tree')
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200188
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200189 assert response == ''
190 assert tree_old == tree_new
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200191
Simon Glassddba5202025-02-09 09:07:14 -0700192 response = ubman.run_command('unbind /bind-test')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200193 assert response == ''