From d5803a02693f72a80da64193a729a71ac585df6b Mon Sep 17 00:00:00 2001 From: Bernard Shyu Date: Fri, 14 Nov 2014 14:14:54 +0800 Subject: [PATCH] pd: fix negative timeout for waiting task pd[port].timeout will be 0 immediate after set_state(), thus will cause a negative value result for timeout calculation in the pd_task's ending loop. This will cause task_wait_event to receive a negative timeout value. Obviously it causes no harm to the current implementation, but should be fixed by being paranoid. BUG=none BRANCH=none TEST=make buildall Change-Id: Ib2817e1d95ca6c6eedcaff16a9e7e95033953901 Reviewed-on: https://chromium-review.googlesource.com/229760 Commit-Queue: Bernard Shyu Reviewed-by: Alec Berg Tested-by: Bernard Shyu --- common/usb_pd_protocol.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/common/usb_pd_protocol.c b/common/usb_pd_protocol.c index a160e61a11..7073064c2d 100644 --- a/common/usb_pd_protocol.c +++ b/common/usb_pd_protocol.c @@ -2056,12 +2056,14 @@ void pd_task(void) * timeout value to wake up on the next state timeout. */ now = get_time(); - if (pd[port].timeout && now.val >= pd[port].timeout) { - set_state(port, pd[port].timeout_state); - /* On a state timeout, run next state soon */ - timeout = timeout < 10*MSEC ? timeout : 10*MSEC; - } else if (pd[port].timeout - now.val < timeout) { - timeout = pd[port].timeout - now.val; + if (pd[port].timeout) { + if (now.val >= pd[port].timeout) { + set_state(port, pd[port].timeout_state); + /* On a state timeout, run next state soon */ + timeout = timeout < 10*MSEC ? timeout : 10*MSEC; + } else if (pd[port].timeout - now.val < timeout) { + timeout = pd[port].timeout - now.val; + } } /* Check for disconnection */