From 41235ac25a859880d87ea2747b95ab44d8b0057a Mon Sep 17 00:00:00 2001 From: Bill Richardson Date: Wed, 4 Nov 2015 10:44:19 -0800 Subject: [PATCH] Cr50: Fix bug in print_later, add overflow detection Oops. I was losing one of the args when the USB debugging output was enabled. And with a lot of messages I was also losing some of the output. BUG=chrome-os-partner:34893 BRANCH=none TEST=make buildall, manual test of Cr50 USB: 1. Plug into a USB jack on a Linux host. 2. In src/platform/ec/extra/usb_console, run make ./usb_console -p 5014 -e 1 3. Type something, hit return 4. See whatever you typed come back with swapped case 5. ^D to quit Change-Id: I284606aa91a76262644cfce60913a91ccc36ae60 Signed-off-by: Bill Richardson Reviewed-on: https://chromium-review.googlesource.com/310846 Reviewed-by: Vadim Bendebury --- chip/g/usb.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/chip/g/usb.c b/chip/g/usb.c index 2cd1df1e4c..476ec574b9 100644 --- a/chip/g/usb.c +++ b/chip/g/usb.c @@ -54,38 +54,49 @@ * Fill a circular buffer with things to print when we get the chance. The * number of args is fixed, and there's no rollover detection. */ -#define MAX_ENTRIES 256 +#define MAX_ENTRIES 512 static struct { timestamp_t t; const char *fmt; int a0, a1, a2, a3, a4; } stuff_to_print[MAX_ENTRIES]; -static int stuff_in, stuff_out; +static int stuff_in, stuff_out, stuff_overflow; /* Call this only from within interrupt handler! */ void print_later(const char *fmt, int a0, int a1, int a2, int a3, int a4) { + int next; + stuff_to_print[stuff_in].t = get_time(); stuff_to_print[stuff_in].fmt = fmt; stuff_to_print[stuff_in].a0 = a0; stuff_to_print[stuff_in].a1 = a1; stuff_to_print[stuff_in].a2 = a2; stuff_to_print[stuff_in].a3 = a3; - stuff_to_print[stuff_in].a3 = a4; - stuff_in++; - if (stuff_in >= MAX_ENTRIES) - stuff_in = 0; + stuff_to_print[stuff_in].a4 = a4; + + next = (stuff_in + 1) % MAX_ENTRIES; + if (next == stuff_out) + stuff_overflow = 1; + else + stuff_in = next; } static void do_print_later(void) { int lines_per_loop = 32; /* too much at once fails */ int copy_of_stuff_in; + int copy_of_overflow; interrupt_disable(); copy_of_stuff_in = stuff_in; + copy_of_overflow = stuff_overflow; + stuff_overflow = 0; interrupt_enable(); + if (copy_of_overflow) + ccprintf("*** WARNING: SOME MESSAGES WERE LOST ***\n"); + while (lines_per_loop && stuff_out != copy_of_stuff_in) { ccprintf("at %.6ld: ", stuff_to_print[stuff_out].t); ccprintf(stuff_to_print[stuff_out].fmt, @@ -95,9 +106,7 @@ static void do_print_later(void) stuff_to_print[stuff_out].a3, stuff_to_print[stuff_out].a4); ccprintf("\n"); - stuff_out++; - if (stuff_out >= MAX_ENTRIES) - stuff_out = 0; + stuff_out = (stuff_out + 1) % MAX_ENTRIES; lines_per_loop--; } }