[PATCH 2/2] braille: nbcon: Use nbcon atomic console callbacks

From: Petr Mladek

Date: Tue Sep 22 2026 - 03:38:56 EST


The Braille console is integrated with the virtual terminal (VT) and
writes its data using the legacy con->write() callback of the associated
serial console driver.

However, once the underlying console driver is converted to the NBCON
API, it should use the con->write_atomic() callback, which must be
synchronized by acquiring the nbcon console context.

Adapt braille_write() to handle NBCON consoles:

1. Acquire the nbcon console ownership using NBCON_PRIO_EMERGENCY
priority before printing, disabling local interrupts to prevent
any nested calls into the driver.
2. Call the con->write_atomic() callback to write the buffer.
3. Release the nbcon console ownership and restore local interrupts.

Also, adjust __serial8250_console_write() in the 8250 serial driver to
exclude Braille consoles from the newline prepending logic. The serial
port is not used for standard printk logging which might be interrupted
in the middle of the operation. In the Braille mode, the serial driver
is supposed to write exactly what it gets. In fact, it does not print
any newlines at all in this case.

Fixes: d3539347022a ("serial: 8250: Switch to nbcon console, take 2")
Signed-off-by: Petr Mladek <pmladek@xxxxxxxx>
---
.../accessibility/braille/braille_console.c | 38 ++++++++++++++++++-
drivers/tty/serial/8250/8250_port.c | 5 ++-
2 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/drivers/accessibility/braille/braille_console.c b/drivers/accessibility/braille/braille_console.c
index 06b43b678d6e..cd1019d478db 100644
--- a/drivers/accessibility/braille/braille_console.c
+++ b/drivers/accessibility/braille/braille_console.c
@@ -62,14 +62,32 @@ static void braille_write(u16 *buf)
{
static u16 lastwrite[WIDTH];
unsigned char data[1 + 1 + 2*WIDTH + 2 + 1], csum = 0, *c;
+ struct nbcon_write_context wctxt = { };
+ unsigned long flags;
+
u16 out;
int i;

if (!braille_co)
return;

+ if (braille_co->flags & CON_NBCON) {
+ /*
+ * Braille console might be called from unknown context via
+ * vt_console_print() from console_unlock() from printk().
+ * Use the atomic callback and synchronize it just using
+ * the console context. Disable interrupts to prevent a nested
+ * call into the driver code which might cause a deadlock when
+ * trying to acquire the console ownership, see
+ * __nbcon_atomic_flush_pending_con().
+ */
+ local_irq_save(flags);
+ while (!nbcon_braille_try_acquire(braille_co, &wctxt))
+ cpu_relax();
+ }
+
if (!memcmp(lastwrite, buf, WIDTH * sizeof(*buf)))
- return;
+ goto release_nbcon;
memcpy(lastwrite, buf, WIDTH * sizeof(*buf));

#define SOH 1
@@ -102,7 +120,23 @@ static void braille_write(u16 *buf)
*c++ = csum;
*c++ = ETX;

- braille_co->write(braille_co, data, c - data);
+ if (braille_co->flags & CON_NBCON) {
+ if (braille_co->write_atomic &&
+ !braille_co->flags & CON_NBCON_ATOMIC_UNSAFE) {
+ nbcon_write_context_set_buf(&wctxt, (char *)data, c - data);
+ braille_co->write_atomic(braille_co, &wctxt);
+ } else {
+ pr_warn_once("Braille requires a safe braille_co->write_atomic callback\n");
+ }
+ } else {
+ braille_co->write(braille_co, data, c - data);
+ }
+
+release_nbcon:
+ if (braille_co->flags & CON_NBCON) {
+ nbcon_braille_release(&wctxt);
+ local_irq_restore(flags);
+ }
}

/* Follow the VC cursor*/
diff --git a/drivers/tty/serial/8250/8250_port.c b/drivers/tty/serial/8250/8250_port.c
index 38fa45e74a37..6eb0b439e433 100644
--- a/drivers/tty/serial/8250/8250_port.c
+++ b/drivers/tty/serial/8250/8250_port.c
@@ -3417,8 +3417,11 @@ static void __serial8250_console_write(struct uart_8250_port *up,
* If the console printer did not fully output the previous line, it
* must have been handed or taken over. Insert a newline in order to
* maintain clean output.
+ *
+ * Braille consoles are an exception. The serial port is not used
+ * for printk(). The driver is supposed to write exactly what it gets.
*/
- if (!up->console_line_ended) {
+ if (unlikely(!up->console_line_ended && !nbcon_is_braille(wctxt))) {
if (use_fifo)
__serial8250_console_fifo_write(up, wctxt, "\n", 1);
else
--
2.55.0