From 591f35be8717ce023a8f2bb95d433c6ca612a001 Mon Sep 17 00:00:00 2001 From: Stefan Kolb Date: Wed, 10 May 2017 07:56:17 -0600 Subject: [PATCH] =?UTF-8?q?I=20discovered=20a=20problem=20in=20the=20file?= =?UTF-8?q?=20drivers/serial/serial.c=20concerning=20the=20function=20uart?= =?UTF-8?q?=5Fclose(=E2=80=A6).=20In=20the=20case=20that=20a=20serial=20de?= =?UTF-8?q?vice=20is=20opened=20with=20the=20flag=20O=5FNONBLOCK=20the=20f?= =?UTF-8?q?unction=20uart=5Fclose(=E2=80=A6)=20blocks=20until=20all=20data?= =?UTF-8?q?=20in=20the=20buffer=20is=20transmitted.=20The=20function=20clo?= =?UTF-8?q?se(=E2=80=A6)=20called=20on=20an=20handle=20opened=20with=20O?= =?UTF-8?q?=5FNONBLOCK=20should=20not=20block.=20The=20problem=20occurred?= =?UTF-8?q?=20with=20a=20CDC/ACM=20device.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Documentation/README.html | 2 +- drivers/serial/serial.c | 35 ++++++++++++++++++++--------------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/Documentation/README.html b/Documentation/README.html index 02a3042b484..fe4dced9adb 100644 --- a/Documentation/README.html +++ b/Documentation/README.html @@ -8,7 +8,7 @@

NuttX README Files

-

Last Updated: April 18, 2017

+

Last Updated: May 9, 2017

diff --git a/drivers/serial/serial.c b/drivers/serial/serial.c index 85fded4e470..775fa3a97a7 100644 --- a/drivers/serial/serial.c +++ b/drivers/serial/serial.c @@ -1,7 +1,7 @@ /************************************************************************************ * drivers/serial/serial.c * - * Copyright (C) 2007-2009, 2011-2013, 2016 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009, 2011-2013, 2016-2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -1207,26 +1207,31 @@ static int uart_close(FAR struct file *filep) uart_disablerxint(dev); - /* Now we wait for the transmit buffer to clear */ + /* Prevent blocking if the device is opened with O_NONBLOCK */ - while (dev->xmit.head != dev->xmit.tail) + if ((filep->f_oflags & O_NONBLOCK) == 0) { -#ifndef CONFIG_DISABLE_SIGNALS - usleep(HALF_SECOND_USEC); -#else - up_mdelay(HALF_SECOND_MSEC); -#endif - } + /* Now we wait for the transmit buffer to clear */ - /* And wait for the TX fifo to drain */ - - while (!uart_txempty(dev)) - { + while (dev->xmit.head != dev->xmit.tail) + { #ifndef CONFIG_DISABLE_SIGNALS - usleep(HALF_SECOND_USEC); + usleep(HALF_SECOND_USEC); #else - up_mdelay(HALF_SECOND_MSEC); + up_mdelay(HALF_SECOND_MSEC); #endif + } + + /* And wait for the TX fifo to drain */ + + while (!uart_txempty(dev)) + { +#ifndef CONFIG_DISABLE_SIGNALS + usleep(HALF_SECOND_USEC); +#else + up_mdelay(HALF_SECOND_MSEC); +#endif + } } /* Free the IRQ and disable the UART */