xref: /aosp_15_r20/external/autotest/server/cros/usb_mux_controller.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1# Lint as: python2, python3
2# Copyriht (c) 2014 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6from __future__ import absolute_import
7from __future__ import division
8from __future__ import print_function
9import logging
10import six
11
12# Following Beaglebone GPIO pins are used to control the 8 port multiplexer.
13MUX_EN = '20'
14MUX_S0 = '115'
15MUX_S1 = '117'
16MUX_S2 = '49'
17
18ENABLE_MUX = '1'
19DISABLE_MUX = '0'
20
21# Various commands used to control the GPIO pins at the kernel level.
22LS_GPIO_DIRECTORY = 'ls /sys/class/gpio'
23EXPORT_GPIO_PIN = 'echo %s > /sys/class/gpio/export'
24SET_GPIO_DIRECTION = 'echo high > /sys/class/gpio/gpio%s/direction'
25SET_GPIO_VALUE = 'echo %s > /sys/class/gpio/gpio%s/value'
26UNEXPORT_GPIO_PIN = 'echo %s > /sys/class/gpio/unexport'
27
28# Values passed to each GPIO pin to enable a specific port.
29# Bit sequence: MUX_S2, MUX_S1, MUX_S0
30# Example: To enable port 5, MUX_S2 will be set to 1, MUX_S1 will be set to 0
31# and MUX_S0 will be set to 1
32ports = {0:'000', 1:'001', 2:'010', 3:'011', 4:'100', 5:'101', 6:'110', 7:'111'}
33
34class USBMuxController(object):
35    """Class to control individual ports on a 8 port USB switch/hub.
36
37    This class is responsible for enabling all the GPIO pins on the beaglebone
38    needed to control the 8 port USB switch/hub. In order to use this USB mux
39    controller you need custom hardware setup which connects to the beaglebone
40    and drives the 8 port relay switch to turn the individual ports on the USB
41    hub 'on'/'off'.
42
43    TODO(harpreet) Write a USB mux hardware design document and provide a link
44    here.
45
46    """
47
48    version = 1
49
50    def __init__(self, host):
51        """Initializes this USB Mux Controller instance.
52
53        @param host: Host where the test will be run.
54
55        """
56        self.host = host
57
58    def __del__(self):
59        """Destructor of USBMuxController.
60
61        Disables all GPIO pins used that control the multiplexer.
62
63        """
64        self.mux_teardown()
65
66    def mux_setup(self):
67        """
68        Enable GPIO pins that control the multiplexer.
69
70        """
71        logging.info('Enable GPIO pins that control the multiplexer.')
72        self.enable_gpio_pins(MUX_EN)
73        self.disable_all_ports()
74        self.enable_gpio_pins(MUX_S2)
75        self.enable_gpio_pins(MUX_S1)
76        self.enable_gpio_pins(MUX_S0)
77
78    def mux_teardown(self):
79        """
80        Disable the multiplexer and unexport all GPIO pins.
81
82        """
83        logging.info('Start USB multiplexer teardown.')
84        self.disable_all_ports()
85        # unexport gpio pins
86        logging.info('Unexport all GPIO pins.')
87        self.host.servo.system(UNEXPORT_GPIO_PIN % MUX_S0)
88        self.host.servo.system(UNEXPORT_GPIO_PIN % MUX_S1)
89        self.host.servo.system(UNEXPORT_GPIO_PIN % MUX_S2)
90        self.host.servo.system(UNEXPORT_GPIO_PIN % MUX_EN)
91        logging.info('Completed USB multiplexer teardown. All USB ports should'
92                     'now be turned off.')
93
94    def enable_gpio_pins(self, pin):
95        """
96        Enables the given GPIO pin by exporting the pin and setting the
97        direction.
98
99        @param pin: GPIO pin to be enabled.
100
101        """
102        if 'gpio' + pin not in self.host.servo.system_output(LS_GPIO_DIRECTORY):
103            self.host.servo.system(EXPORT_GPIO_PIN % pin)
104            self.host.servo.system(SET_GPIO_DIRECTION % pin)
105
106    def enable_port(self, usb_port):
107        """
108        Enables the given port on the USB hub.
109
110        @param usb_port: USB port to be enabled.
111
112        """
113        port = ports[usb_port]
114        logging.info('Enable port %s.', port)
115        self.mux_setup()
116        self.disable_all_ports()
117
118        logging.info('Set GPIO pins to correct logic levels.')
119        self.host.servo.system(SET_GPIO_VALUE % (port[0], MUX_S2))
120        self.host.servo.system(SET_GPIO_VALUE % (port[1], MUX_S1))
121        self.host.servo.system(SET_GPIO_VALUE % (port[2], MUX_S0))
122
123        logging.info('Enable USB multiplexer. Appropriate port should now be'
124                     'enabled')
125        self.host.servo.system(SET_GPIO_VALUE % (ENABLE_MUX, MUX_EN))
126
127    def disable_all_ports(self):
128        """
129        Disables all USB ports that are currently enabled.
130
131        """
132        if 'gpio20' in self.host.servo.system_output(LS_GPIO_DIRECTORY):
133            logging.info('Disable USB ports.')
134            self.host.servo.system(SET_GPIO_VALUE % (DISABLE_MUX, MUX_EN))
135