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