1# Copyright 2016 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 6import time 7 8from autotest_lib.client.common_lib import error 9from autotest_lib.server.cros.faft.firmware_test import FirmwareTest 10from autotest_lib.server.cros.servo import pd_device 11 12 13class firmware_PDResetHard(FirmwareTest): 14 """ 15 USB PD hard reset test. 16 17 Hard resets are issued by both ends of the connection. If the DUT 18 is dualrole capable, then a power role swap is executed, and the 19 test is repeated with the DUT in the opposite power role. Pass 20 criteria is that all attempted hard resets are successful. 21 22 """ 23 version = 1 24 RESET_ITERATIONS = 5 25 DELAY_BETWEEN_ITERATIONS = 1 26 27 def _test_hard_reset(self, port_pair): 28 """Tests hard reset initated by both ends of PD connection 29 30 @param port_pair: list of 2 connected PD devices 31 """ 32 for dev in port_pair: 33 for _ in xrange(self.RESET_ITERATIONS): 34 try: 35 if dev.hard_reset() == False: 36 raise error.TestFail('Hard Reset Failed') 37 time.sleep(self.DELAY_BETWEEN_ITERATIONS) 38 except NotImplementedError: 39 logging.warn('Device cant hard reset ... skipping') 40 break 41 42 def initialize(self, host, cmdline_args, flip_cc=False): 43 super(firmware_PDResetHard, self).initialize(host, cmdline_args) 44 self.setup_pdtester(flip_cc) 45 # Only run in normal mode 46 self.switcher.setup_mode('normal') 47 # Turn off console prints, except for USBPD. 48 self.usbpd.enable_console_channel('usbpd') 49 50 def cleanup(self): 51 self.usbpd.send_command('chan 0xffffffff') 52 super(firmware_PDResetHard, self).cleanup() 53 54 def run_once(self): 55 """Execute Power Role swap test. 56 57 1. Verify that pd console is accessible 58 2. Verify that DUT has a valid PD contract 59 3. Make sure dualrole mode is enabled on both ends 60 4. Test Hard Reset initiated by both ends of connection 61 5. Attempt to change power roles 62 If power role changed, then retest soft resets 63 Else end test. 64 65 """ 66 # Create list of available UART consoles 67 consoles = [self.usbpd, self.pdtester] 68 port_partner = pd_device.PDPortPartner(consoles) 69 # Identify a valid test port pair 70 port_pair = port_partner.identify_pd_devices() 71 if not port_pair: 72 raise error.TestFail('No PD connection found!') 73 74 # Test hard resets initiated by both ends 75 self._test_hard_reset(port_pair) 76 77 # Swap power roles (if possible). Note the pr swap is attempted 78 # for both devices in the connection. This ensures that a device 79 # such as Plankton, which is dualrole capable, but has this mode 80 # disabled by default, won't prevent the device pair from role swapping. 81 swappable_dev = None; 82 for dev in port_pair: 83 try: 84 if dev.pr_swap(): 85 swappable_dev = dev 86 break 87 except NotImplementedError: 88 logging.warn('Power role swap not supported on the device') 89 90 if swappable_dev: 91 try: 92 # Power role has been swapped, retest. 93 self._test_hard_reset(port_pair) 94 finally: 95 # Swap power role again, back to the original 96 if not swappable_dev.pr_swap(): 97 logging.error('Failed to swap power role to the original') 98 else: 99 logging.warn('Device pair could not perform power role swap, ' 100 'ending test') 101