blob: 850fe113fe2f8c59e80012285adfb007a55773f6 [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):
Tom Rinicaae73a2025-05-08 15:34:43 -060010 """A helper function to confirm contents of the device tree """
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020011 lines = [x.strip() for x in response.splitlines()]
12 leaf = ''
13 if depth != 0:
14 leaf = ' ' + ' ' * (depth - 1)
15 if not last_child:
16 leaf = leaf + r'\|'
17 else:
18 leaf = leaf + '`'
Patrice Chotardf0c9d1a2020-07-28 09:13:34 +020019
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020020 leaf = leaf + '-- ' + name
21 line = (r' *{:10.10} *[0-9]* \[ [ +] \] {:20.20} [` |]{}$'
22 .format(uclass, drv, leaf))
23 prog = re.compile(line)
24 for l in lines:
25 if prog.match(l):
26 return True
27 return False
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020028
Michal Simek901f5ba2022-07-07 12:52:26 +020029@pytest.mark.boardspec('sandbox')
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020030@pytest.mark.buildconfigspec('cmd_bind')
Simon Glassddba5202025-02-09 09:07:14 -070031def test_bind_unbind_with_node(ubman):
Tom Rinicaae73a2025-05-08 15:34:43 -060032 """Test the bind and unbind commands of a node
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020033
Tom Rinicaae73a2025-05-08 15:34:43 -060034 Verify that the dm tree output contains some expected nodes, and then bind
35 and unbind a USB via node device while verifying that the dm tree output
36 matches the expected values at each step.
37 """
Simon Glassddba5202025-02-09 09:07:14 -070038 tree = ubman.run_command('dm tree')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020039 assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
40 assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False)
41 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020042
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020043 #bind usb_ether driver (which has no compatible) to usb@1 node.
44 ##New entry usb_ether should appear in the dm tree
Simon Glassddba5202025-02-09 09:07:14 -070045 response = ubman.run_command('bind /usb@1 usb_ether')
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, 'usb@1', 'ethernet', 'usb_ether', 1, True)
Patrice Chotard4ffd03a2021-09-10 16:16:24 +020049
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020050 #Unbind child #1. No error expected and all devices should be there except for bind-test-child1
Simon Glassddba5202025-02-09 09:07:14 -070051 response = ubman.run_command('unbind /bind-test/bind-test-child1')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020052 assert response == ''
Simon Glassddba5202025-02-09 09:07:14 -070053 tree = ubman.run_command('dm tree')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020054 assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
55 assert 'bind-test-child1' not in tree
56 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020057
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020058 #bind child #1. No error expected and all devices should be there
Simon Glassddba5202025-02-09 09:07:14 -070059 response = ubman.run_command('bind /bind-test/bind-test-child1 phy_sandbox')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020060 assert response == ''
Simon Glassddba5202025-02-09 09:07:14 -070061 tree = ubman.run_command('dm tree')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020062 assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
63 assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, True)
64 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, False)
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020065
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020066 #Unbind child #2. No error expected and all devices should be there except for bind-test-child2
Simon Glassddba5202025-02-09 09:07:14 -070067 response = ubman.run_command('unbind /bind-test/bind-test-child2')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020068 assert response == ''
Simon Glassddba5202025-02-09 09:07:14 -070069 tree = ubman.run_command('dm tree')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020070 assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
71 assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, True)
72 assert 'bind-test-child2' not in tree
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020073
74
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020075 #Bind child #2. No error expected and all devices should be there
Simon Glassddba5202025-02-09 09:07:14 -070076 response = ubman.run_command('bind /bind-test/bind-test-child2 simple_bus')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020077 assert response == ''
Simon Glassddba5202025-02-09 09:07:14 -070078 tree = ubman.run_command('dm tree')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020079 assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
80 assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False)
81 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020082
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020083 #Unbind parent. No error expected. All devices should be removed and unbound
Simon Glassddba5202025-02-09 09:07:14 -070084 response = ubman.run_command('unbind /bind-test')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020085 assert response == ''
Simon Glassddba5202025-02-09 09:07:14 -070086 tree = ubman.run_command('dm tree')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020087 assert 'bind-test' not in tree
88 assert 'bind-test-child1' not in tree
89 assert 'bind-test-child2' not in tree
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020090
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020091 #try binding invalid node with valid driver
Simon Glassddba5202025-02-09 09:07:14 -070092 response = ubman.run_command('bind /not-a-valid-node simple_bus')
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 'not-a-valid-node' not in tree
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +020096
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +020097 #try binding valid node with invalid driver
Simon Glassddba5202025-02-09 09:07:14 -070098 response = ubman.run_command('bind /bind-test not_a_driver')
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 'bind-test' not in tree
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200102
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200103 #bind /bind-test. Device should come up as well as its children
Simon Glassddba5202025-02-09 09:07:14 -0700104 response = ubman.run_command('bind /bind-test simple_bus')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200105 assert response == ''
Simon Glassddba5202025-02-09 09:07:14 -0700106 tree = ubman.run_command('dm tree')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200107 assert in_tree(tree, 'bind-test', 'simple_bus', 'simple_bus', 0, True)
108 assert in_tree(tree, 'bind-test-child1', 'phy', 'phy_sandbox', 1, False)
109 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200110
Simon Glassddba5202025-02-09 09:07:14 -0700111 response = ubman.run_command('unbind /bind-test')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200112 assert response == ''
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200113
114def get_next_line(tree, name):
Tom Rinicaae73a2025-05-08 15:34:43 -0600115 """A helper function to strip content out of dm tree output"""
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200116 treelines = [x.strip() for x in tree.splitlines() if x.strip()]
117 child_line = ''
118 for idx, line in enumerate(treelines):
119 if '-- ' + name in line:
120 try:
121 child_line = treelines[idx+1]
122 except:
123 pass
124 break
125 return child_line
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200126
Michal Simek901f5ba2022-07-07 12:52:26 +0200127@pytest.mark.boardspec('sandbox')
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200128@pytest.mark.buildconfigspec('cmd_bind')
Simon Glassae3db3d2022-08-06 17:51:48 -0600129@pytest.mark.singlethread
Simon Glassddba5202025-02-09 09:07:14 -0700130def test_bind_unbind_with_uclass(ubman):
Tom Rinicaae73a2025-05-08 15:34:43 -0600131 """Test the bind and unbind commands of a class
132
133 Bind and unbind the simple_bus class while verifying that the dm tree
134 output matches the expected values at each step.
135 """
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200136 #bind /bind-test
Simon Glassddba5202025-02-09 09:07:14 -0700137 response = ubman.run_command('bind /bind-test simple_bus')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200138 assert response == ''
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200139
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200140 #make sure bind-test-child2 is there and get its uclass/index pair
Simon Glassddba5202025-02-09 09:07:14 -0700141 tree = ubman.run_command('dm tree')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200142 child2_line = [x.strip() for x in tree.splitlines() if '-- bind-test-child2' in x]
143 assert len(child2_line) == 1
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200144
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200145 child2_uclass = child2_line[0].split()[0]
146 child2_index = int(child2_line[0].split()[1])
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200147
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200148 #bind simple_bus as a child of bind-test-child2
Simon Glassddba5202025-02-09 09:07:14 -0700149 response = ubman.run_command(
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200150 'bind {} {} simple_bus'.format(child2_uclass, child2_index))
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200151
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200152 #check that the child is there and its uclass/index pair is right
Simon Glassddba5202025-02-09 09:07:14 -0700153 tree = ubman.run_command('dm tree')
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200154
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200155 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
156 assert child_of_child2_line
157 child_of_child2_index = int(child_of_child2_line.split()[1])
158 assert in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True)
159 assert child_of_child2_index == child2_index + 1
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200160
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200161 #unbind the child and check it has been removed
Simon Glassddba5202025-02-09 09:07:14 -0700162 response = ubman.run_command('unbind simple_bus {}'.format(child_of_child2_index))
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200163 assert response == ''
Simon Glassddba5202025-02-09 09:07:14 -0700164 tree = ubman.run_command('dm tree')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200165 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
166 assert not in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True)
167 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
168 assert child_of_child2_line == ''
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200169
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200170 #bind simple_bus as a child of bind-test-child2
Simon Glassddba5202025-02-09 09:07:14 -0700171 response = ubman.run_command(
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200172 'bind {} {} simple_bus'.format(child2_uclass, child2_index))
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200173
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200174 #check that the child is there and its uclass/index pair is right
Simon Glassddba5202025-02-09 09:07:14 -0700175 tree = ubman.run_command('dm tree')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200176 treelines = [x.strip() for x in tree.splitlines() if x.strip()]
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200177
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200178 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
179 assert child_of_child2_line
180 child_of_child2_index = int(child_of_child2_line.split()[1])
181 assert in_tree(tree, 'simple_bus', 'simple_bus', 'simple_bus', 2, True)
182 assert child_of_child2_index == child2_index + 1
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200183
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200184 #unbind the child and check it has been removed
Simon Glassddba5202025-02-09 09:07:14 -0700185 response = ubman.run_command(
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200186 'unbind {} {} simple_bus'.format(child2_uclass, child2_index))
187 assert response == ''
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200188
Simon Glassddba5202025-02-09 09:07:14 -0700189 tree = ubman.run_command('dm tree')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200190 assert in_tree(tree, 'bind-test-child2', 'simple_bus', 'simple_bus', 1, True)
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200191
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200192 child_of_child2_line = get_next_line(tree, 'bind-test-child2')
193 assert child_of_child2_line == ''
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200194
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200195 #unbind the child again and check it doesn't change the tree
Simon Glassddba5202025-02-09 09:07:14 -0700196 tree_old = ubman.run_command('dm tree')
197 response = ubman.run_command(
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200198 'unbind {} {} simple_bus'.format(child2_uclass, child2_index))
Simon Glassddba5202025-02-09 09:07:14 -0700199 tree_new = ubman.run_command('dm tree')
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200200
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200201 assert response == ''
202 assert tree_old == tree_new
Jean-Jacques Hiblote83a31b2018-08-09 16:17:46 +0200203
Simon Glassddba5202025-02-09 09:07:14 -0700204 response = ubman.run_command('unbind /bind-test')
Heinrich Schuchardtdeccc812022-04-30 11:26:23 +0200205 assert response == ''