Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp949658pxb; Sun, 11 Apr 2021 02:40:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx8EmY2mvAG8JovSw/Kb9tuYCuUQ7EKZyyxy0U2jTiarnE5RJjg3RsM43VVlgAOURbuFdKK X-Received: by 2002:a17:906:804b:: with SMTP id x11mr4150981ejw.388.1618134055173; Sun, 11 Apr 2021 02:40:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618134055; cv=none; d=google.com; s=arc-20160816; b=ZoBtOzYI12EYRBQ8b0LhKUqzg481AHQLEq3wt4X9skRZ9baxvcdzuz4ypSmvLkoiMY qNx40RX4+j6qse3KFtCRFVbMglNWg4/2dMHPu459uUd8U6cROeIsiu6rOY6JXr9RYkTR 8RMXs8O9scWW9BFGmsD3gCcg3tApiX4d/53YiI/s3kUgCaN2GuxfOxFGJdNvXd5CS80o 8NHLMVbeIQbDimovR3ceXPJsR2fFmhLfHeqJaq2LWJ8i10t3QEmfdg0BqY6bq5amQGrw jEXdG3zU1NnSdN4RIIXJYFcLImiQfLQTQD2HQGkKezpTjTxKBm5a1m9g9lxgTI1cmELu 48Bg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from :dkim-signature; bh=ID7z0ghRJGcMLBNI8gWtXJSotHesf24UhofLjYl2wx8=; b=QvNnsPVl3QQTCwDHKa9wMkZP6M9uWW1eqbHrAiD2Opw5l9e7IXy5ZWexebM/x3eJ+D gS63nL31puCDmJRMx+DLufdn7RQjsJgy+M+9mEpKre52A5lWO31Vx7pvodBaCcXNxhA4 enfcnXDqSKBiNcVZrE1++O3FOTrkqPLiY1y0pv0Z+NiXfV2NGNoL6qWQNi98WKdwU7Ol 8n+mGloHWOPgh60xtkQ2+2iBecNJhHc5mfk4mANKHOTSHTGU0baaWevda0KEuopCR+YP jqegPzK0QjsLvPkmiztMZlrCvleM78bLTLoaARuPkMVePnszfiEhfJ0dUBkcRZlPo/bH uqkg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@libero.it header.s=s2021 header.b=UZ+wvpPu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=libero.it Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 4si6326978edv.334.2021.04.11.02.40.31; Sun, 11 Apr 2021 02:40:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@libero.it header.s=s2021 header.b=UZ+wvpPu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=libero.it Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235291AbhDKJjT (ORCPT + 99 others); Sun, 11 Apr 2021 05:39:19 -0400 Received: from smtp-17.italiaonline.it ([213.209.10.17]:44620 "EHLO libero.it" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S229804AbhDKJjS (ORCPT ); Sun, 11 Apr 2021 05:39:18 -0400 Received: from passgat-Modern-14-A10M.homenet.telecomitalia.it ([87.20.116.197]) by smtp-17.iol.local with ESMTPA id VWYTl8bZQtpGHVWYXlq27L; Sun, 11 Apr 2021 11:39:01 +0200 x-libjamoibt: 1601 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=libero.it; s=s2021; t=1618133941; bh=ID7z0ghRJGcMLBNI8gWtXJSotHesf24UhofLjYl2wx8=; h=From; b=UZ+wvpPuWwpZkSC0JUFedTWSOtYFoYVsablxjA2A+YzuTSr4cqGY+TYVhPlR1cykm 8/79L4VjHtIZDS/BFNSngOHEJPRM5rt6pXaAV1Qs2wHjrNCfj7BdqoKIGwKGX9KQ1Q xZE7Yw4lWjicUaV6WSCr5axEy5PTbWfbBGUyodcGXvzbMcqSe2j0A+riAWUHwLdQWv yOO3PzI5cWp0N+M4bXxS+Anm0/M+WWCeYKcyDIfJsY6nZI8bmKZnkEducW5lgGnCLd 4fj2hkklBAW4Tzcj9A5rkShoK8SMoHD0XfYTYggmPUz6em8ci7evylavEkelJH+tiU 2zw2ZM4XpCsWA== X-CNFS-Analysis: v=2.4 cv=Q7IXX66a c=1 sm=1 tr=0 ts=6072c3b5 cx=a_exe a=AVqmXbCQpuNSdJmApS5GbQ==:117 a=AVqmXbCQpuNSdJmApS5GbQ==:17 a=RNOFN41U3FZ75c9ZyJUA:9 From: Dario Binacchi To: linux-kernel@vger.kernel.org Cc: Dario Binacchi , Greg Kroah-Hartman , Jiri Slaby , linux-serial@vger.kernel.org Subject: [PATCH v2] serial: omap: fix rs485 half-duplex filtering Date: Sun, 11 Apr 2021 11:38:55 +0200 Message-Id: <20210411093855.1053-1-dariobin@libero.it> X-Mailer: git-send-email 2.17.1 X-CMAE-Envelope: MS4xfK6aa9mZXnTp+boQAxFGXkxCvo5YQNqWCJGZ2IZNt4ryu072ADMDFiHmz2oxlejkrAvBwiRn7gydEVSJPYP7cWp8Yf3mcBTeARGwI7N2TiK+zgGjO2Km xgNJOWZX2LrjWEpFcrDcNC36yiFghFkoLyxtimOtunNunBcjmcFyqEyqrlY+vZgA8BVoqMw617leFNh2Mh/Rg7mpLD9v7KqT9w5Z7/TpTO8Ojh+XF0hHboLV tt4RVWyi/A9abZnjXiVEzTHTUtyd36B5lyssSOKC3qpm5XqilrMEN+5CTun+YOR0n4XliTpDa1tGf1ggIZqj/mLMJdQRMp/IlWwCaoKNrtTyFT2Ikhhq72AD ia2LyvfxbLB55GR7k8+eZq2nS9tm9athriHA/k57ulVR9d/rpXU= Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Data received during half-duplex transmission must be filtered. If the target device responds quickly, emptying the FIFO at the end of the transmission can erase not only the echo characters but also part of the response message. By keeping the receive interrupt enabled even during transmission, it allows you to filter each echo character and only in a number equal to those transmitted. The issue was generated by a target device that started responding 240us later having received a request in communication at 115200bps. Sometimes, some messages received by the target were missing some of the first bytes. Signed-off-by: Dario Binacchi --- Changes in v2: - Fix compiling error drivers/tty/serial/omap-serial.c | 39 ++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/drivers/tty/serial/omap-serial.c b/drivers/tty/serial/omap-serial.c index 76b94d0ff586..c0df22b7ea5e 100644 --- a/drivers/tty/serial/omap-serial.c +++ b/drivers/tty/serial/omap-serial.c @@ -159,6 +159,8 @@ struct uart_omap_port { u32 calc_latency; struct work_struct qos_work; bool is_suspending; + + atomic_t rs485_tx_filter_count; }; #define to_uart_omap_port(p) ((container_of((p), struct uart_omap_port, port))) @@ -328,19 +330,6 @@ static void serial_omap_stop_tx(struct uart_port *port) serial_out(up, UART_IER, up->ier); } - if ((port->rs485.flags & SER_RS485_ENABLED) && - !(port->rs485.flags & SER_RS485_RX_DURING_TX)) { - /* - * Empty the RX FIFO, we are not interested in anything - * received during the half-duplex transmission. - */ - serial_out(up, UART_FCR, up->fcr | UART_FCR_CLEAR_RCVR); - /* Re-enable RX interrupts */ - up->ier |= UART_IER_RLSI | UART_IER_RDI; - up->port.read_status_mask |= UART_LSR_DR; - serial_out(up, UART_IER, up->ier); - } - pm_runtime_mark_last_busy(up->dev); pm_runtime_put_autosuspend(up->dev); } @@ -366,6 +355,10 @@ static void transmit_chars(struct uart_omap_port *up, unsigned int lsr) serial_out(up, UART_TX, up->port.x_char); up->port.icount.tx++; up->port.x_char = 0; + if ((up->port.rs485.flags & SER_RS485_ENABLED) && + !(up->port.rs485.flags & SER_RS485_RX_DURING_TX)) + atomic_inc(&up->rs485_tx_filter_count); + return; } if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) { @@ -377,6 +370,10 @@ static void transmit_chars(struct uart_omap_port *up, unsigned int lsr) serial_out(up, UART_TX, xmit->buf[xmit->tail]); xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); up->port.icount.tx++; + if ((up->port.rs485.flags & SER_RS485_ENABLED) && + !(up->port.rs485.flags & SER_RS485_RX_DURING_TX)) + atomic_inc(&up->rs485_tx_filter_count); + if (uart_circ_empty(xmit)) break; } while (--count > 0); @@ -420,7 +417,7 @@ static void serial_omap_start_tx(struct uart_port *port) if ((port->rs485.flags & SER_RS485_ENABLED) && !(port->rs485.flags & SER_RS485_RX_DURING_TX)) - serial_omap_stop_rx(port); + atomic_set(&up->rs485_tx_filter_count, 0); serial_omap_enable_ier_thri(up); pm_runtime_mark_last_busy(up->dev); @@ -491,8 +488,13 @@ static void serial_omap_rlsi(struct uart_omap_port *up, unsigned int lsr) * Read one data character out to avoid stalling the receiver according * to the table 23-246 of the omap4 TRM. */ - if (likely(lsr & UART_LSR_DR)) + if (likely(lsr & UART_LSR_DR)) { serial_in(up, UART_RX); + if ((up->port.rs485.flags & SER_RS485_ENABLED) && + !(up->port.rs485.flags & SER_RS485_RX_DURING_TX) && + atomic_read(&up->rs485_tx_filter_count)) + atomic_dec(&up->rs485_tx_filter_count); + } up->port.icount.rx++; flag = TTY_NORMAL; @@ -543,6 +545,13 @@ static void serial_omap_rdi(struct uart_omap_port *up, unsigned int lsr) return; ch = serial_in(up, UART_RX); + if ((up->port.rs485.flags & SER_RS485_ENABLED) && + !(up->port.rs485.flags & SER_RS485_RX_DURING_TX) && + atomic_read(&up->rs485_tx_filter_count)) { + atomic_dec(&up->rs485_tx_filter_count); + return; + } + flag = TTY_NORMAL; up->port.icount.rx++; -- 2.17.1