projects/linux·blob master · refs

3669 lines · 93.3 KiB

1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Driver core for serial ports
4 *
5 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6 *
7 * Copyright 1999 ARM Limited
8 * Copyright (C) 2000-2001 Deep Blue Solutions Ltd.
9 */
10#include <linux/module.h>
11#include <linux/tty.h>
12#include <linux/tty_flip.h>
13#include <linux/slab.h>
14#include <linux/sched/signal.h>
15#include <linux/init.h>
16#include <linux/console.h>
17#include <linux/gpio/consumer.h>
18#include <linux/kernel.h>
19#include <linux/of.h>
20#include <linux/pm_runtime.h>
21#include <linux/proc_fs.h>
22#include <linux/seq_file.h>
23#include <linux/device.h>
24#include <linux/serial.h> /* for serial_state and serial_icounter_struct */
25#include <linux/serial_core.h>
26#include <linux/sysrq.h>
27#include <linux/delay.h>
28#include <linux/mutex.h>
29#include <linux/math64.h>
30#include <linux/security.h>
31
32#include <linux/irq.h>
33#include <linux/uaccess.h>
34
35#include "serial_base.h"
36#include "8250/8250.h" /* For hub6_match_port() */
37
38/*
39 * This is used to lock changes in serial line configuration.
40 */
41static DEFINE_MUTEX(port_mutex);
42
43/*
44 * lockdep: port->lock is initialized in two places, but we
45 * want only one lock-class:
46 */
47static struct lock_class_key port_lock_key;
48
49#define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
50
51/*
52 * Max time with active RTS before/after data is sent.
53 */
54#define RS485_MAX_RTS_DELAY 100 /* msecs */
55
56static void uart_change_pm(struct uart_state *state,
57 enum uart_pm_state pm_state);
58
59static void uart_port_shutdown(struct tty_port *port);
60
61static int uart_dcd_enabled(struct uart_port *uport)
62{
63 return !!(uport->status & UPSTAT_DCD_ENABLE);
64}
65
66static inline struct uart_port *uart_port_ref(struct uart_state *state)
67{
68 if (atomic_add_unless(&state->refcount, 1, 0))
69 return state->uart_port;
70 return NULL;
71}
72
73static inline void uart_port_deref(struct uart_port *uport)
74{
75 if (atomic_dec_and_test(&uport->state->refcount))
76 wake_up(&uport->state->remove_wait);
77}
78
79static inline struct uart_port *uart_port_ref_lock(struct uart_state *state, unsigned long *flags)
80{
81 struct uart_port *uport = uart_port_ref(state);
82
83 if (uport)
84 uart_port_lock_irqsave(uport, flags);
85
86 return uport;
87}
88
89static inline void uart_port_unlock_deref(struct uart_port *uport, unsigned long flags)
90{
91 if (uport) {
92 uart_port_unlock_irqrestore(uport, flags);
93 uart_port_deref(uport);
94 }
95}
96
97static inline struct uart_port *uart_port_check(struct uart_state *state)
98{
99 lockdep_assert_held(&state->port.mutex);
100 return state->uart_port;
101}
102
103/**
104 * uart_write_wakeup - schedule write processing
105 * @port: port to be processed
106 *
107 * This routine is used by the interrupt handler to schedule processing in the
108 * software interrupt portion of the driver. A driver is expected to call this
109 * function when the number of characters in the transmit buffer have dropped
110 * below a threshold.
111 *
112 * Locking: @port->lock should be held
113 */
114void uart_write_wakeup(struct uart_port *port)
115{
116 struct uart_state *state = port->state;
117 /*
118 * This means you called this function _after_ the port was
119 * closed. No cookie for you.
120 */
121 BUG_ON(!state);
122 tty_port_tty_wakeup(&state->port);
123}
124EXPORT_SYMBOL(uart_write_wakeup);
125
126static void uart_stop(struct tty_struct *tty)
127{
128 struct uart_state *state = tty->driver_data;
129 struct uart_port *port;
130 unsigned long flags;
131
132 port = uart_port_ref_lock(state, &flags);
133 if (port)
134 port->ops->stop_tx(port);
135 uart_port_unlock_deref(port, flags);
136}
137
138static void __uart_start(struct uart_state *state)
139{
140 struct uart_port *port = state->uart_port;
141 struct serial_port_device *port_dev;
142 int err;
143
144 if (!port || port->flags & UPF_DEAD || uart_tx_stopped(port))
145 return;
146
147 port_dev = port->port_dev;
148
149 /* Increment the runtime PM usage count for the active check below */
150 err = pm_runtime_get(&port_dev->dev);
151 if (err < 0 && err != -EINPROGRESS) {
152 pm_runtime_put_noidle(&port_dev->dev);
153 return;
154 }
155
156 /*
157 * Start TX if enabled, and kick runtime PM. If the device is not
158 * enabled, serial_port_runtime_resume() calls start_tx() again
159 * after enabling the device.
160 */
161 if (!pm_runtime_enabled(port->dev) || pm_runtime_active(&port_dev->dev))
162 port->ops->start_tx(port);
163 pm_runtime_mark_last_busy(&port_dev->dev);
164 pm_runtime_put_autosuspend(&port_dev->dev);
165}
166
167static void uart_start(struct tty_struct *tty)
168{
169 struct uart_state *state = tty->driver_data;
170 struct uart_port *port;
171 unsigned long flags;
172
173 port = uart_port_ref_lock(state, &flags);
174 __uart_start(state);
175 uart_port_unlock_deref(port, flags);
176}
177
178static void
179uart_update_mctrl(struct uart_port *port, unsigned int set, unsigned int clear)
180{
181 unsigned int old;
182
183 guard(uart_port_lock_irqsave)(port);
184 old = port->mctrl;
185 port->mctrl = (old & ~clear) | set;
186 if (old != port->mctrl && !(port->rs485.flags & SER_RS485_ENABLED))
187 port->ops->set_mctrl(port, port->mctrl);
188}
189
190#define uart_set_mctrl(port, set) uart_update_mctrl(port, set, 0)
191#define uart_clear_mctrl(port, clear) uart_update_mctrl(port, 0, clear)
192
193static void uart_port_dtr_rts(struct uart_port *uport, bool active)
194{
195 if (active)
196 uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
197 else
198 uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
199}
200
201/* Caller holds port mutex */
202static void uart_change_line_settings(struct tty_struct *tty, struct uart_state *state,
203 const struct ktermios *old_termios)
204{
205 struct uart_port *uport = uart_port_check(state);
206 struct ktermios *termios;
207 bool old_hw_stopped;
208
209 /*
210 * If we have no tty, termios, or the port does not exist,
211 * then we can't set the parameters for this port.
212 */
213 if (!tty || uport->type == PORT_UNKNOWN)
214 return;
215
216 termios = &tty->termios;
217 uport->ops->set_termios(uport, termios, old_termios);
218
219 /*
220 * Set modem status enables based on termios cflag
221 */
222 guard(uart_port_lock_irq)(uport);
223 if (termios->c_cflag & CRTSCTS)
224 uport->status |= UPSTAT_CTS_ENABLE;
225 else
226 uport->status &= ~UPSTAT_CTS_ENABLE;
227
228 if (termios->c_cflag & CLOCAL)
229 uport->status &= ~UPSTAT_DCD_ENABLE;
230 else
231 uport->status |= UPSTAT_DCD_ENABLE;
232
233 /* reset sw-assisted CTS flow control based on (possibly) new mode */
234 old_hw_stopped = uport->hw_stopped;
235 uport->hw_stopped = uart_softcts_mode(uport) &&
236 !(uport->ops->get_mctrl(uport) & TIOCM_CTS);
237 if (uport->hw_stopped != old_hw_stopped) {
238 if (!old_hw_stopped)
239 uport->ops->stop_tx(uport);
240 else
241 __uart_start(state);
242 }
243}
244
245static int uart_alloc_xmit_buf(struct tty_port *port)
246{
247 struct uart_state *state = container_of(port, struct uart_state, port);
248 struct uart_port *uport;
249 unsigned long flags;
250 unsigned long page;
251
252 /*
253 * Initialise and allocate the transmit and temporary
254 * buffer.
255 */
256 page = get_zeroed_page(GFP_KERNEL);
257 if (!page)
258 return -ENOMEM;
259
260 uport = uart_port_ref_lock(state, &flags);
261 if (!state->port.xmit_buf) {
262 state->port.xmit_buf = (unsigned char *)page;
263 kfifo_init(&state->port.xmit_fifo, state->port.xmit_buf,
264 PAGE_SIZE);
265 uart_port_unlock_deref(uport, flags);
266 } else {
267 uart_port_unlock_deref(uport, flags);
268 /*
269 * Do not free() the page under the port lock, see
270 * uart_free_xmit_buf().
271 */
272 free_page(page);
273 }
274
275 return 0;
276}
277
278static void uart_free_xmit_buf(struct tty_port *port)
279{
280 struct uart_state *state = container_of(port, struct uart_state, port);
281 struct uart_port *uport;
282 unsigned long flags;
283 char *xmit_buf;
284
285 /*
286 * Do not free() the transmit buffer page under the port lock since
287 * this can create various circular locking scenarios. For instance,
288 * console driver may need to allocate/free a debug object, which
289 * can end up in printk() recursion.
290 */
291 uport = uart_port_ref_lock(state, &flags);
292 xmit_buf = port->xmit_buf;
293 port->xmit_buf = NULL;
294 INIT_KFIFO(port->xmit_fifo);
295 uart_port_unlock_deref(uport, flags);
296
297 free_page((unsigned long)xmit_buf);
298}
299
300/*
301 * Startup the port. This will be called once per open. All calls
302 * will be serialised by the per-port mutex.
303 */
304static int uart_port_startup(struct tty_struct *tty, struct uart_state *state,
305 bool init_hw)
306{
307 struct uart_port *uport = uart_port_check(state);
308 int retval;
309
310 if (uport->type == PORT_UNKNOWN)
311 return 1;
312
313 /*
314 * Make sure the device is in D0 state.
315 */
316 uart_change_pm(state, UART_PM_STATE_ON);
317
318 retval = uart_alloc_xmit_buf(&state->port);
319 if (retval)
320 return retval;
321
322 retval = uport->ops->startup(uport);
323 if (retval == 0) {
324 if (uart_console(uport) && uport->cons->cflag) {
325 tty->termios.c_cflag = uport->cons->cflag;
326 tty->termios.c_ispeed = uport->cons->ispeed;
327 tty->termios.c_ospeed = uport->cons->ospeed;
328 uport->cons->cflag = 0;
329 uport->cons->ispeed = 0;
330 uport->cons->ospeed = 0;
331 }
332 /*
333 * Initialise the hardware port settings.
334 */
335 uart_change_line_settings(tty, state, NULL);
336
337 /*
338 * Setup the RTS and DTR signals once the
339 * port is open and ready to respond.
340 */
341 if (init_hw && C_BAUD(tty))
342 uart_port_dtr_rts(uport, true);
343 }
344
345 /*
346 * This is to allow setserial on this port. People may want to set
347 * port/irq/type and then reconfigure the port properly if it failed
348 * now.
349 */
350 if (retval && capable(CAP_SYS_ADMIN))
351 return 1;
352
353 return retval;
354}
355
356static int uart_startup(struct tty_struct *tty, struct uart_state *state,
357 bool init_hw)
358{
359 struct tty_port *port = &state->port;
360 struct uart_port *uport;
361 int retval;
362
363 if (tty_port_initialized(port))
364 goto out_base_port_startup;
365
366 retval = uart_port_startup(tty, state, init_hw);
367 if (retval) {
368 set_bit(TTY_IO_ERROR, &tty->flags);
369 return retval;
370 }
371
372out_base_port_startup:
373 uport = uart_port_check(state);
374 if (!uport)
375 return -EIO;
376
377 serial_base_port_startup(uport);
378
379 return 0;
380}
381
382/*
383 * This routine will shutdown a serial port; interrupts are disabled, and
384 * DTR is dropped if the hangup on close termio flag is on. Calls to
385 * uart_shutdown are serialised by the per-port semaphore.
386 *
387 * uport == NULL if uart_port has already been removed
388 */
389static void uart_shutdown(struct tty_struct *tty, struct uart_state *state)
390{
391 struct uart_port *uport = uart_port_check(state);
392 struct tty_port *port = &state->port;
393
394 /*
395 * Set the TTY IO error marker
396 */
397 if (tty)
398 set_bit(TTY_IO_ERROR, &tty->flags);
399
400 if (uport)
401 serial_base_port_shutdown(uport);
402
403 if (tty_port_initialized(port)) {
404 tty_port_set_initialized(port, false);
405
406 /*
407 * Turn off DTR and RTS early.
408 */
409 if (uport) {
410 if (uart_console(uport) && tty) {
411 uport->cons->cflag = tty->termios.c_cflag;
412 uport->cons->ispeed = tty->termios.c_ispeed;
413 uport->cons->ospeed = tty->termios.c_ospeed;
414 }
415
416 if (!tty || C_HUPCL(tty))
417 uart_port_dtr_rts(uport, false);
418 }
419
420 uart_port_shutdown(port);
421 }
422
423 /*
424 * It's possible for shutdown to be called after suspend if we get
425 * a DCD drop (hangup) at just the right time. Clear suspended bit so
426 * we don't try to resume a port that has been shutdown.
427 */
428 tty_port_set_suspended(port, false);
429
430 uart_free_xmit_buf(port);
431}
432
433/**
434 * uart_update_timeout - update per-port frame timing information
435 * @port: uart_port structure describing the port
436 * @cflag: termios cflag value
437 * @baud: speed of the port
438 *
439 * Set the @port frame timing information from which the FIFO timeout value is
440 * derived. The @cflag value should reflect the actual hardware settings as
441 * number of bits, parity, stop bits and baud rate is taken into account here.
442 *
443 * Locking: caller is expected to take @port->lock
444 */
445void
446uart_update_timeout(struct uart_port *port, unsigned int cflag,
447 unsigned int baud)
448{
449 u64 temp = tty_get_frame_size(cflag);
450
451 temp *= NSEC_PER_SEC;
452 port->frame_time = (unsigned int)DIV64_U64_ROUND_UP(temp, baud);
453}
454EXPORT_SYMBOL(uart_update_timeout);
455
456/**
457 * uart_get_baud_rate - return baud rate for a particular port
458 * @port: uart_port structure describing the port in question.
459 * @termios: desired termios settings
460 * @old: old termios (or %NULL)
461 * @min: minimum acceptable baud rate
462 * @max: maximum acceptable baud rate
463 *
464 * Decode the termios structure into a numeric baud rate, taking account of the
465 * magic 38400 baud rate (with spd_* flags), and mapping the %B0 rate to 9600
466 * baud or min argument, whichever is greater.
467 *
468 * If the new baud rate is invalid, try the @old termios setting. If it's still
469 * invalid, clip to the nearest chip supported rate.
470 *
471 * The @termios structure is updated to reflect the baud rate we're actually
472 * going to be using. Don't do this for the case where B0 is requested ("hang
473 * up").
474 *
475 * Locking: caller dependent
476 */
477unsigned int
478uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
479 const struct ktermios *old, unsigned int min, unsigned int max)
480{
481 unsigned int try;
482 unsigned int baud;
483 unsigned int altbaud;
484 upf_t flags = port->flags & UPF_SPD_MASK;
485
486 switch (flags) {
487 case UPF_SPD_HI:
488 altbaud = 57600;
489 break;
490 case UPF_SPD_VHI:
491 altbaud = 115200;
492 break;
493 case UPF_SPD_SHI:
494 altbaud = 230400;
495 break;
496 case UPF_SPD_WARP:
497 altbaud = 460800;
498 break;
499 default:
500 altbaud = 38400;
501 break;
502 }
503
504 for (try = 0; try < 2; try++) {
505 baud = tty_termios_baud_rate(termios);
506
507 /*
508 * The spd_hi, spd_vhi, spd_shi, spd_warp kludge...
509 * Die! Die! Die!
510 */
511 if (try == 0 && baud == 38400)
512 baud = altbaud;
513
514 /*
515 * Special case: B0 rate.
516 */
517 if (baud == 0)
518 return max(min, 9600);
519
520 if (baud >= min && baud <= max)
521 return baud;
522
523 /*
524 * If the range cannot be met then try again with
525 * the old baud rate if possible.
526 */
527 termios->c_cflag &= ~CBAUD;
528 if (old) {
529 baud = tty_termios_baud_rate(old);
530 tty_termios_encode_baud_rate(termios, baud, baud);
531 old = NULL;
532 continue;
533 }
534
535 /*
536 * As a last resort, if the range cannot be met then clip to
537 * the nearest chip supported rate.
538 */
539 if (baud <= min)
540 baud = min + 1;
541 else
542 baud = max - 1;
543
544 tty_termios_encode_baud_rate(termios, baud, baud);
545 }
546
547 /* Should never happen */
548 WARN_ON(1);
549 return 0;
550}
551EXPORT_SYMBOL(uart_get_baud_rate);
552
553/**
554 * uart_get_divisor - return uart clock divisor
555 * @port: uart_port structure describing the port
556 * @baud: desired baud rate
557 *
558 * Calculate the divisor (baud_base / baud) for the specified @baud,
559 * appropriately rounded.
560 *
561 * If 38400 baud and custom divisor is selected, return the custom divisor
562 * instead.
563 *
564 * Locking: caller dependent
565 */
566unsigned int
567uart_get_divisor(struct uart_port *port, unsigned int baud)
568{
569 unsigned int quot;
570
571 /*
572 * Old custom speed handling.
573 */
574 if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
575 quot = port->custom_divisor;
576 else
577 quot = DIV_ROUND_CLOSEST(port->uartclk, 16 * baud);
578
579 return quot;
580}
581EXPORT_SYMBOL(uart_get_divisor);
582
583static int uart_put_char(struct tty_struct *tty, u8 c)
584{
585 struct uart_state *state = tty->driver_data;
586 struct uart_port *port;
587 unsigned long flags;
588 int ret = 0;
589
590 port = uart_port_ref_lock(state, &flags);
591 if (!state->port.xmit_buf) {
592 uart_port_unlock_deref(port, flags);
593 return 0;
594 }
595
596 if (port)
597 ret = kfifo_put(&state->port.xmit_fifo, c);
598 uart_port_unlock_deref(port, flags);
599 return ret;
600}
601
602static void uart_flush_chars(struct tty_struct *tty)
603{
604 uart_start(tty);
605}
606
607static ssize_t uart_write(struct tty_struct *tty, const u8 *buf, size_t count)
608{
609 struct uart_state *state = tty->driver_data;
610 struct uart_port *port;
611 unsigned long flags;
612 int ret = 0;
613
614 /*
615 * This means you called this function _after_ the port was
616 * closed. No cookie for you.
617 */
618 if (WARN_ON(!state))
619 return -EL3HLT;
620
621 port = uart_port_ref_lock(state, &flags);
622 if (!state->port.xmit_buf) {
623 uart_port_unlock_deref(port, flags);
624 return 0;
625 }
626
627 if (port)
628 ret = kfifo_in(&state->port.xmit_fifo, buf, count);
629
630 __uart_start(state);
631 uart_port_unlock_deref(port, flags);
632 return ret;
633}
634
635static unsigned int uart_write_room(struct tty_struct *tty)
636{
637 struct uart_state *state = tty->driver_data;
638 struct uart_port *port;
639 unsigned long flags;
640 unsigned int ret;
641
642 port = uart_port_ref_lock(state, &flags);
643 if (!state->port.xmit_buf)
644 ret = 0;
645 else
646 ret = kfifo_avail(&state->port.xmit_fifo);
647 uart_port_unlock_deref(port, flags);
648 return ret;
649}
650
651static unsigned int uart_chars_in_buffer(struct tty_struct *tty)
652{
653 struct uart_state *state = tty->driver_data;
654 struct uart_port *port;
655 unsigned long flags;
656 unsigned int ret;
657
658 port = uart_port_ref_lock(state, &flags);
659 ret = kfifo_len(&state->port.xmit_fifo);
660 uart_port_unlock_deref(port, flags);
661 return ret;
662}
663
664static void uart_flush_buffer(struct tty_struct *tty)
665{
666 struct uart_state *state = tty->driver_data;
667 struct uart_port *port;
668 unsigned long flags;
669
670 /*
671 * This means you called this function _after_ the port was
672 * closed. No cookie for you.
673 */
674 if (WARN_ON(!state))
675 return;
676
677 pr_debug("uart_flush_buffer(%d) called\n", tty->index);
678
679 port = uart_port_ref_lock(state, &flags);
680 if (!port)
681 return;
682 kfifo_reset(&state->port.xmit_fifo);
683 if (port->ops->flush_buffer)
684 port->ops->flush_buffer(port);
685 uart_port_unlock_deref(port, flags);
686 tty_port_tty_wakeup(&state->port);
687}
688
689/*
690 * This function performs low-level write of high-priority XON/XOFF
691 * character and accounting for it.
692 *
693 * Requires uart_port to implement .serial_out().
694 */
695void uart_xchar_out(struct uart_port *uport, int offset)
696{
697 serial_port_out(uport, offset, uport->x_char);
698 uport->icount.tx++;
699 uport->x_char = 0;
700}
701EXPORT_SYMBOL_GPL(uart_xchar_out);
702
703/*
704 * This function is used to send a high-priority XON/XOFF character to
705 * the device
706 */
707static void uart_send_xchar(struct tty_struct *tty, u8 ch)
708{
709 struct uart_state *state = tty->driver_data;
710 struct uart_port *port;
711
712 port = uart_port_ref(state);
713 if (!port)
714 return;
715
716 if (port->ops->send_xchar)
717 port->ops->send_xchar(port, ch);
718 else {
719 guard(uart_port_lock_irqsave)(port);
720 port->x_char = ch;
721 if (ch)
722 port->ops->start_tx(port);
723 }
724 uart_port_deref(port);
725}
726
727static void uart_throttle(struct tty_struct *tty)
728{
729 struct uart_state *state = tty->driver_data;
730 upstat_t mask = UPSTAT_SYNC_FIFO;
731 struct uart_port *port;
732
733 port = uart_port_ref(state);
734 if (!port)
735 return;
736
737 if (I_IXOFF(tty))
738 mask |= UPSTAT_AUTOXOFF;
739 if (C_CRTSCTS(tty))
740 mask |= UPSTAT_AUTORTS;
741
742 if (port->status & mask) {
743 port->ops->throttle(port);
744 mask &= ~port->status;
745 }
746
747 if (mask & UPSTAT_AUTORTS)
748 uart_clear_mctrl(port, TIOCM_RTS);
749
750 if (mask & UPSTAT_AUTOXOFF)
751 uart_send_xchar(tty, STOP_CHAR(tty));
752
753 uart_port_deref(port);
754}
755
756static void uart_unthrottle(struct tty_struct *tty)
757{
758 struct uart_state *state = tty->driver_data;
759 upstat_t mask = UPSTAT_SYNC_FIFO;
760 struct uart_port *port;
761
762 port = uart_port_ref(state);
763 if (!port)
764 return;
765
766 if (I_IXOFF(tty))
767 mask |= UPSTAT_AUTOXOFF;
768 if (C_CRTSCTS(tty))
769 mask |= UPSTAT_AUTORTS;
770
771 if (port->status & mask) {
772 port->ops->unthrottle(port);
773 mask &= ~port->status;
774 }
775
776 if (mask & UPSTAT_AUTORTS)
777 uart_set_mctrl(port, TIOCM_RTS);
778
779 if (mask & UPSTAT_AUTOXOFF)
780 uart_send_xchar(tty, START_CHAR(tty));
781
782 uart_port_deref(port);
783}
784
785static int uart_get_info(struct tty_port *port, struct serial_struct *retinfo)
786{
787 struct uart_state *state = container_of(port, struct uart_state, port);
788 struct uart_port *uport;
789
790 /* Initialize structure in case we error out later to prevent any stack info leakage. */
791 *retinfo = (struct serial_struct){};
792
793 /*
794 * Ensure the state we copy is consistent and no hardware changes
795 * occur as we go
796 */
797 guard(mutex)(&port->mutex);
798 uport = uart_port_check(state);
799 if (!uport)
800 return -ENODEV;
801
802 retinfo->type = uport->type;
803 retinfo->line = uport->line;
804 retinfo->port = uport->iobase;
805 if (HIGH_BITS_OFFSET)
806 retinfo->port_high = (long) uport->iobase >> HIGH_BITS_OFFSET;
807 retinfo->irq = uport->irq;
808 retinfo->flags = (__force int)uport->flags;
809 retinfo->xmit_fifo_size = uport->fifosize;
810 retinfo->baud_base = uport->uartclk / 16;
811 retinfo->close_delay = jiffies_to_msecs(port->close_delay) / 10;
812 retinfo->closing_wait = port->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
813 ASYNC_CLOSING_WAIT_NONE :
814 jiffies_to_msecs(port->closing_wait) / 10;
815 retinfo->custom_divisor = uport->custom_divisor;
816 retinfo->hub6 = uport->hub6;
817 retinfo->io_type = uport->iotype;
818 retinfo->iomem_reg_shift = uport->regshift;
819 retinfo->iomem_base = (void *)(unsigned long)uport->mapbase;
820
821 return 0;
822}
823
824static int uart_get_info_user(struct tty_struct *tty,
825 struct serial_struct *ss)
826{
827 struct uart_state *state = tty->driver_data;
828 struct tty_port *port = &state->port;
829
830 return uart_get_info(port, ss) < 0 ? -EIO : 0;
831}
832
833static int uart_change_port(struct uart_port *uport,
834 const struct serial_struct *new_info,
835 unsigned long new_port)
836{
837 unsigned long old_iobase, old_mapbase;
838 unsigned int old_type, old_iotype, old_hub6, old_shift;
839 int retval;
840
841 old_iobase = uport->iobase;
842 old_mapbase = uport->mapbase;
843 old_type = uport->type;
844 old_hub6 = uport->hub6;
845 old_iotype = uport->iotype;
846 old_shift = uport->regshift;
847
848 if (old_type != PORT_UNKNOWN && uport->ops->release_port)
849 uport->ops->release_port(uport);
850
851 uport->iobase = new_port;
852 uport->type = new_info->type;
853 uport->hub6 = new_info->hub6;
854 uport->iotype = new_info->io_type;
855 uport->regshift = new_info->iomem_reg_shift;
856 uport->mapbase = (unsigned long)new_info->iomem_base;
857
858 if (uport->type == PORT_UNKNOWN || !uport->ops->request_port)
859 return 0;
860
861 retval = uport->ops->request_port(uport);
862 if (retval == 0)
863 return 0; /* succeeded => done */
864
865 /*
866 * If we fail to request resources for the new port, try to restore the
867 * old settings.
868 */
869 uport->iobase = old_iobase;
870 uport->type = old_type;
871 uport->hub6 = old_hub6;
872 uport->iotype = old_iotype;
873 uport->regshift = old_shift;
874 uport->mapbase = old_mapbase;
875
876 if (old_type == PORT_UNKNOWN)
877 return retval;
878
879 retval = uport->ops->request_port(uport);
880 /* If we failed to restore the old settings, we fail like this. */
881 if (retval)
882 uport->type = PORT_UNKNOWN;
883
884 /* We failed anyway. */
885 return -EBUSY;
886}
887
888static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
889 struct uart_state *state,
890 struct serial_struct *new_info)
891{
892 struct uart_port *uport = uart_port_check(state);
893 unsigned long new_port;
894 unsigned int old_custom_divisor, close_delay, closing_wait;
895 bool change_irq, change_port;
896 upf_t old_flags, new_flags;
897 int retval;
898
899 if (!uport || tty_io_error(tty))
900 return -EIO;
901
902 new_port = new_info->port;
903 if (HIGH_BITS_OFFSET)
904 new_port += (unsigned long) new_info->port_high << HIGH_BITS_OFFSET;
905
906 new_info->irq = irq_canonicalize(new_info->irq);
907 close_delay = msecs_to_jiffies(new_info->close_delay * 10);
908 closing_wait = new_info->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
909 ASYNC_CLOSING_WAIT_NONE :
910 msecs_to_jiffies(new_info->closing_wait * 10);
911
912
913 change_irq = !(uport->flags & UPF_FIXED_PORT)
914 && new_info->irq != uport->irq;
915
916 /*
917 * Since changing the 'type' of the port changes its resource
918 * allocations, we should treat type changes the same as
919 * IO port changes.
920 */
921 change_port = !(uport->flags & UPF_FIXED_PORT)
922 && (new_port != uport->iobase ||
923 (unsigned long)new_info->iomem_base != uport->mapbase ||
924 new_info->hub6 != uport->hub6 ||
925 new_info->io_type != uport->iotype ||
926 new_info->iomem_reg_shift != uport->regshift ||
927 new_info->type != uport->type);
928
929 old_flags = uport->flags;
930 new_flags = (__force upf_t)new_info->flags;
931 old_custom_divisor = uport->custom_divisor;
932
933 if (!(uport->flags & UPF_FIXED_PORT)) {
934 unsigned int uartclk = new_info->baud_base * 16;
935 /* check needs to be done here before other settings made */
936 if (uartclk == 0)
937 return -EINVAL;
938 }
939 if (!capable(CAP_SYS_ADMIN)) {
940 if (change_irq || change_port ||
941 (new_info->baud_base != uport->uartclk / 16) ||
942 (close_delay != port->close_delay) ||
943 (closing_wait != port->closing_wait) ||
944 (new_info->xmit_fifo_size &&
945 new_info->xmit_fifo_size != uport->fifosize) ||
946 (((new_flags ^ old_flags) & ~UPF_USR_MASK) != 0))
947 return -EPERM;
948 uport->flags = ((uport->flags & ~UPF_USR_MASK) |
949 (new_flags & UPF_USR_MASK));
950 uport->custom_divisor = new_info->custom_divisor;
951 goto check_and_exit;
952 }
953
954 if (change_irq || change_port) {
955 retval = security_locked_down(LOCKDOWN_TIOCSSERIAL);
956 if (retval)
957 return retval;
958 }
959
960 /* Ask the low level driver to verify the settings. */
961 if (uport->ops->verify_port) {
962 retval = uport->ops->verify_port(uport, new_info);
963 if (retval)
964 return retval;
965 }
966
967 if ((new_info->irq >= irq_get_nr_irqs()) || (new_info->irq < 0) ||
968 (new_info->baud_base < 9600))
969 return -EINVAL;
970
971 if (change_port || change_irq) {
972 /* Make sure that we are the sole user of this port. */
973 if (tty_port_users(port) > 1)
974 return -EBUSY;
975
976 /*
977 * We need to shutdown the serial port at the old
978 * port/type/irq combination.
979 */
980 uart_shutdown(tty, state);
981 }
982
983 if (change_port) {
984 retval = uart_change_port(uport, new_info, new_port);
985 if (retval)
986 return retval;
987 }
988
989 if (change_irq)
990 uport->irq = new_info->irq;
991 if (!(uport->flags & UPF_FIXED_PORT))
992 uport->uartclk = new_info->baud_base * 16;
993 uport->flags = (uport->flags & ~UPF_CHANGE_MASK) |
994 (new_flags & UPF_CHANGE_MASK);
995 uport->custom_divisor = new_info->custom_divisor;
996 port->close_delay = close_delay;
997 port->closing_wait = closing_wait;
998 if (new_info->xmit_fifo_size)
999 uport->fifosize = new_info->xmit_fifo_size;
1000
1001 check_and_exit:
1002 if (uport->type == PORT_UNKNOWN)
1003 return 0;
1004
1005 if (tty_port_initialized(port)) {
1006 if (((old_flags ^ uport->flags) & UPF_SPD_MASK) ||
1007 old_custom_divisor != uport->custom_divisor) {
1008 /*
1009 * If they're setting up a custom divisor or speed,
1010 * instead of clearing it, then bitch about it.
1011 */
1012 if (uport->flags & UPF_SPD_MASK) {
1013 dev_notice_ratelimited(uport->dev,
1014 "%s sets custom speed on %s. This is deprecated.\n",
1015 current->comm,
1016 tty_name(port->tty));
1017 }
1018 uart_change_line_settings(tty, state, NULL);
1019 }
1020
1021 return 0;
1022 }
1023
1024 retval = uart_startup(tty, state, true);
1025 if (retval < 0)
1026 return retval;
1027 if (retval == 0)
1028 tty_port_set_initialized(port, true);
1029
1030 return 0;
1031}
1032
1033static int uart_set_info_user(struct tty_struct *tty, struct serial_struct *ss)
1034{
1035 struct uart_state *state = tty->driver_data;
1036 struct tty_port *port = &state->port;
1037
1038 guard(rwsem_write)(&tty->termios_rwsem);
1039 /*
1040 * This semaphore protects port->count. It is also
1041 * very useful to prevent opens. Also, take the
1042 * port configuration semaphore to make sure that a
1043 * module insertion/removal doesn't change anything
1044 * under us.
1045 */
1046 guard(mutex)(&port->mutex);
1047 return uart_set_info(tty, port, state, ss);
1048}
1049
1050/**
1051 * uart_get_lsr_info - get line status register info
1052 * @tty: tty associated with the UART
1053 * @state: UART being queried
1054 * @value: returned modem value
1055 */
1056static int uart_get_lsr_info(struct tty_struct *tty,
1057 struct uart_state *state, unsigned int __user *value)
1058{
1059 struct uart_port *uport = uart_port_check(state);
1060 unsigned int result;
1061
1062 result = uport->ops->tx_empty(uport);
1063
1064 /*
1065 * If we're about to load something into the transmit
1066 * register, we'll pretend the transmitter isn't empty to
1067 * avoid a race condition (depending on when the transmit
1068 * interrupt happens).
1069 */
1070 if (uport->x_char ||
1071 (!kfifo_is_empty(&state->port.xmit_fifo) &&
1072 !uart_tx_stopped(uport)))
1073 result &= ~TIOCSER_TEMT;
1074
1075 return put_user(result, value);
1076}
1077
1078static int uart_tiocmget(struct tty_struct *tty)
1079{
1080 struct uart_state *state = tty->driver_data;
1081 struct tty_port *port = &state->port;
1082 struct uart_port *uport;
1083
1084 guard(mutex)(&port->mutex);
1085
1086 uport = uart_port_check(state);
1087 if (!uport || tty_io_error(tty))
1088 return -EIO;
1089
1090 guard(uart_port_lock_irq)(uport);
1091
1092 return uport->mctrl | uport->ops->get_mctrl(uport);
1093}
1094
1095static int
1096uart_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear)
1097{
1098 struct uart_state *state = tty->driver_data;
1099 struct tty_port *port = &state->port;
1100 struct uart_port *uport;
1101
1102 guard(mutex)(&port->mutex);
1103
1104 uport = uart_port_check(state);
1105 if (!uport || tty_io_error(tty))
1106 return -EIO;
1107
1108 uart_update_mctrl(uport, set, clear);
1109
1110 return 0;
1111}
1112
1113static int uart_break_ctl(struct tty_struct *tty, int break_state)
1114{
1115 struct uart_state *state = tty->driver_data;
1116 struct tty_port *port = &state->port;
1117 struct uart_port *uport;
1118
1119 guard(mutex)(&port->mutex);
1120
1121 uport = uart_port_check(state);
1122 if (!uport || tty_io_error(tty))
1123 return -EIO;
1124
1125 if (uport->type != PORT_UNKNOWN && uport->ops->break_ctl)
1126 uport->ops->break_ctl(uport, break_state);
1127
1128 return 0;
1129}
1130
1131static int uart_do_autoconfig(struct tty_struct *tty, struct uart_state *state)
1132{
1133 struct tty_port *port = &state->port;
1134 struct uart_port *uport;
1135 int flags, ret;
1136
1137 if (!capable(CAP_SYS_ADMIN))
1138 return -EPERM;
1139
1140 /*
1141 * Take the per-port semaphore. This prevents count from
1142 * changing, and hence any extra opens of the port while
1143 * we're auto-configuring.
1144 */
1145 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &port->mutex) {
1146 uport = uart_port_check(state);
1147 if (!uport || tty_io_error(tty))
1148 return -EIO;
1149
1150 if (tty_port_users(port) != 1)
1151 return -EBUSY;
1152
1153 uart_shutdown(tty, state);
1154
1155 /*
1156 * If we already have a port type configured,
1157 * we must release its resources.
1158 */
1159 if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
1160 uport->ops->release_port(uport);
1161
1162 flags = UART_CONFIG_TYPE;
1163 if (uport->flags & UPF_AUTO_IRQ)
1164 flags |= UART_CONFIG_IRQ;
1165
1166 /*
1167 * This will claim the ports resources if
1168 * a port is found.
1169 */
1170 uport->ops->config_port(uport, flags);
1171
1172 ret = uart_startup(tty, state, true);
1173 if (ret < 0)
1174 return ret;
1175 if (ret > 0)
1176 return 0;
1177
1178 tty_port_set_initialized(port, true);
1179 }
1180
1181 return 0;
1182}
1183
1184static void uart_enable_ms(struct uart_port *uport)
1185{
1186 /*
1187 * Force modem status interrupts on
1188 */
1189 if (uport->ops->enable_ms)
1190 uport->ops->enable_ms(uport);
1191}
1192
1193/*
1194 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1195 * - mask passed in arg for lines of interest
1196 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1197 * Caller should use TIOCGICOUNT to see which one it was
1198 *
1199 * FIXME: This wants extracting into a common all driver implementation
1200 * of TIOCMWAIT using tty_port.
1201 */
1202static int uart_wait_modem_status(struct tty_struct *tty, struct uart_state *state, unsigned long arg)
1203{
1204 struct uart_port *uport;
1205 struct tty_port *port = &state->port;
1206 DECLARE_WAITQUEUE(wait, current);
1207 struct uart_icount cprev, cnow;
1208 int ret;
1209
1210 /*
1211 * note the counters on entry
1212 */
1213 uport = uart_port_ref(state);
1214 if (!uport)
1215 return -EIO;
1216
1217 mutex_lock(&port->mutex);
1218 if (tty_io_error(tty)) {
1219 mutex_unlock(&port->mutex);
1220 ret = -EIO;
1221 goto out_deref;
1222 }
1223
1224 uart_port_lock_irq(uport);
1225 memcpy(&cprev, &uport->icount, sizeof(struct uart_icount));
1226 uart_enable_ms(uport);
1227 uart_port_unlock_irq(uport);
1228
1229 mutex_unlock(&port->mutex);
1230
1231 add_wait_queue(&port->delta_msr_wait, &wait);
1232 for (;;) {
1233 set_current_state(TASK_INTERRUPTIBLE);
1234
1235 uart_port_lock_irq(uport);
1236 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1237 uart_port_unlock_irq(uport);
1238
1239 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1240 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1241 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1242 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1243 ret = 0;
1244 break;
1245 }
1246
1247 if (tty_io_error(tty)) {
1248 ret = -EIO;
1249 break;
1250 }
1251
1252 schedule();
1253
1254 /* see if a signal did it */
1255 if (signal_pending(current)) {
1256 ret = -ERESTARTSYS;
1257 break;
1258 }
1259
1260 cprev = cnow;
1261 }
1262 __set_current_state(TASK_RUNNING);
1263 remove_wait_queue(&port->delta_msr_wait, &wait);
1264out_deref:
1265 uart_port_deref(uport);
1266
1267 return ret;
1268}
1269
1270/*
1271 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1272 * Return: write counters to the user passed counter struct
1273 * NB: both 1->0 and 0->1 transitions are counted except for
1274 * RI where only 0->1 is counted.
1275 */
1276static int uart_get_icount(struct tty_struct *tty,
1277 struct serial_icounter_struct *icount)
1278{
1279 struct uart_state *state = tty->driver_data;
1280 struct uart_icount cnow;
1281 struct uart_port *uport;
1282 unsigned long flags;
1283
1284 uport = uart_port_ref_lock(state, &flags);
1285 if (!uport)
1286 return -EIO;
1287 memcpy(&cnow, &uport->icount, sizeof(struct uart_icount));
1288 uart_port_unlock_deref(uport, flags);
1289
1290 icount->cts = cnow.cts;
1291 icount->dsr = cnow.dsr;
1292 icount->rng = cnow.rng;
1293 icount->dcd = cnow.dcd;
1294 icount->rx = cnow.rx;
1295 icount->tx = cnow.tx;
1296 icount->frame = cnow.frame;
1297 icount->overrun = cnow.overrun;
1298 icount->parity = cnow.parity;
1299 icount->brk = cnow.brk;
1300 icount->buf_overrun = cnow.buf_overrun;
1301
1302 return 0;
1303}
1304
1305#define SER_RS485_LEGACY_FLAGS (SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | \
1306 SER_RS485_RTS_AFTER_SEND | SER_RS485_RX_DURING_TX | \
1307 SER_RS485_TERMINATE_BUS)
1308
1309static int uart_check_rs485_flags(struct uart_port *port, struct serial_rs485 *rs485)
1310{
1311 u32 flags = rs485->flags;
1312
1313 /* Don't return -EINVAL for unsupported legacy flags */
1314 flags &= ~SER_RS485_LEGACY_FLAGS;
1315
1316 /*
1317 * For any bit outside of the legacy ones that is not supported by
1318 * the driver, return -EINVAL.
1319 */
1320 if (flags & ~port->rs485_supported.flags)
1321 return -EINVAL;
1322
1323 /* Asking for address w/o addressing mode? */
1324 if (!(rs485->flags & SER_RS485_ADDRB) &&
1325 (rs485->flags & (SER_RS485_ADDR_RECV|SER_RS485_ADDR_DEST)))
1326 return -EINVAL;
1327
1328 /* Address given but not enabled? */
1329 if (!(rs485->flags & SER_RS485_ADDR_RECV) && rs485->addr_recv)
1330 return -EINVAL;
1331 if (!(rs485->flags & SER_RS485_ADDR_DEST) && rs485->addr_dest)
1332 return -EINVAL;
1333
1334 return 0;
1335}
1336
1337static void uart_sanitize_serial_rs485_delays(struct uart_port *port,
1338 struct serial_rs485 *rs485)
1339{
1340 if (!port->rs485_supported.delay_rts_before_send) {
1341 if (rs485->delay_rts_before_send) {
1342 dev_warn_ratelimited(port->dev,
1343 "%s (%u): RTS delay before sending not supported\n",
1344 port->name, port->line);
1345 }
1346 rs485->delay_rts_before_send = 0;
1347 } else if (rs485->delay_rts_before_send > RS485_MAX_RTS_DELAY) {
1348 rs485->delay_rts_before_send = RS485_MAX_RTS_DELAY;
1349 dev_warn_ratelimited(port->dev,
1350 "%s (%u): RTS delay before sending clamped to %u ms\n",
1351 port->name, port->line, rs485->delay_rts_before_send);
1352 }
1353
1354 if (!port->rs485_supported.delay_rts_after_send) {
1355 if (rs485->delay_rts_after_send) {
1356 dev_warn_ratelimited(port->dev,
1357 "%s (%u): RTS delay after sending not supported\n",
1358 port->name, port->line);
1359 }
1360 rs485->delay_rts_after_send = 0;
1361 } else if (rs485->delay_rts_after_send > RS485_MAX_RTS_DELAY) {
1362 rs485->delay_rts_after_send = RS485_MAX_RTS_DELAY;
1363 dev_warn_ratelimited(port->dev,
1364 "%s (%u): RTS delay after sending clamped to %u ms\n",
1365 port->name, port->line, rs485->delay_rts_after_send);
1366 }
1367}
1368
1369static void uart_sanitize_serial_rs485(struct uart_port *port, struct serial_rs485 *rs485)
1370{
1371 u32 supported_flags = port->rs485_supported.flags;
1372
1373 if (!(rs485->flags & SER_RS485_ENABLED)) {
1374 memset(rs485, 0, sizeof(*rs485));
1375 return;
1376 }
1377
1378 /* Clear other RS485 flags but SER_RS485_TERMINATE_BUS and return if enabling RS422 */
1379 if (rs485->flags & SER_RS485_MODE_RS422) {
1380 rs485->flags &= (SER_RS485_ENABLED | SER_RS485_MODE_RS422 | SER_RS485_TERMINATE_BUS);
1381 return;
1382 }
1383
1384 rs485->flags &= supported_flags;
1385
1386 /* Pick sane settings if the user hasn't */
1387 if (!(rs485->flags & SER_RS485_RTS_ON_SEND) ==
1388 !(rs485->flags & SER_RS485_RTS_AFTER_SEND)) {
1389 if (supported_flags & SER_RS485_RTS_ON_SEND) {
1390 rs485->flags |= SER_RS485_RTS_ON_SEND;
1391 rs485->flags &= ~SER_RS485_RTS_AFTER_SEND;
1392
1393 dev_warn_ratelimited(port->dev,
1394 "%s (%u): invalid RTS setting, using RTS_ON_SEND instead\n",
1395 port->name, port->line);
1396 } else {
1397 rs485->flags |= SER_RS485_RTS_AFTER_SEND;
1398 rs485->flags &= ~SER_RS485_RTS_ON_SEND;
1399
1400 dev_warn_ratelimited(port->dev,
1401 "%s (%u): invalid RTS setting, using RTS_AFTER_SEND instead\n",
1402 port->name, port->line);
1403 }
1404 }
1405
1406 uart_sanitize_serial_rs485_delays(port, rs485);
1407
1408 /* Return clean padding area to userspace */
1409 memset(rs485->padding0, 0, sizeof(rs485->padding0));
1410 memset(rs485->padding1, 0, sizeof(rs485->padding1));
1411}
1412
1413static void uart_set_rs485_termination(struct uart_port *port,
1414 const struct serial_rs485 *rs485)
1415{
1416 if (!(rs485->flags & SER_RS485_ENABLED))
1417 return;
1418
1419 gpiod_set_value_cansleep(port->rs485_term_gpio,
1420 !!(rs485->flags & SER_RS485_TERMINATE_BUS));
1421}
1422
1423static void uart_set_rs485_rx_during_tx(struct uart_port *port,
1424 const struct serial_rs485 *rs485)
1425{
1426 if (!(rs485->flags & SER_RS485_ENABLED))
1427 return;
1428
1429 gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio,
1430 !!(rs485->flags & SER_RS485_RX_DURING_TX));
1431}
1432
1433static int uart_rs485_config(struct uart_port *port)
1434{
1435 struct serial_rs485 *rs485 = &port->rs485;
1436 int ret;
1437
1438 if (!(rs485->flags & SER_RS485_ENABLED))
1439 return 0;
1440
1441 uart_sanitize_serial_rs485(port, rs485);
1442 uart_set_rs485_termination(port, rs485);
1443 uart_set_rs485_rx_during_tx(port, rs485);
1444
1445 scoped_guard(uart_port_lock_irqsave, port)
1446 ret = port->rs485_config(port, NULL, rs485);
1447 if (ret) {
1448 memset(rs485, 0, sizeof(*rs485));
1449 /* unset GPIOs */
1450 gpiod_set_value_cansleep(port->rs485_term_gpio, 0);
1451 gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio, 0);
1452 }
1453
1454 return ret;
1455}
1456
1457static int uart_get_rs485_config(struct uart_port *port,
1458 struct serial_rs485 __user *rs485)
1459{
1460 struct serial_rs485 aux;
1461
1462 scoped_guard(uart_port_lock_irqsave, port)
1463 aux = port->rs485;
1464
1465 if (copy_to_user(rs485, &aux, sizeof(aux)))
1466 return -EFAULT;
1467
1468 return 0;
1469}
1470
1471static int uart_set_rs485_config(struct tty_struct *tty, struct uart_port *port,
1472 struct serial_rs485 __user *rs485_user)
1473{
1474 struct serial_rs485 rs485;
1475 int ret;
1476
1477 if (!(port->rs485_supported.flags & SER_RS485_ENABLED))
1478 return -ENOTTY;
1479
1480 if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
1481 return -EFAULT;
1482
1483 ret = uart_check_rs485_flags(port, &rs485);
1484 if (ret)
1485 return ret;
1486 uart_sanitize_serial_rs485(port, &rs485);
1487 uart_set_rs485_termination(port, &rs485);
1488 uart_set_rs485_rx_during_tx(port, &rs485);
1489
1490 scoped_guard(uart_port_lock_irqsave, port) {
1491 ret = port->rs485_config(port, &tty->termios, &rs485);
1492 if (!ret) {
1493 port->rs485 = rs485;
1494
1495 /* Reset RTS and other mctrl lines when disabling RS485 */
1496 if (!(rs485.flags & SER_RS485_ENABLED))
1497 port->ops->set_mctrl(port, port->mctrl);
1498 }
1499 }
1500 if (ret) {
1501 /* restore old GPIO settings */
1502 gpiod_set_value_cansleep(port->rs485_term_gpio,
1503 !!(port->rs485.flags & SER_RS485_TERMINATE_BUS));
1504 gpiod_set_value_cansleep(port->rs485_rx_during_tx_gpio,
1505 !!(port->rs485.flags & SER_RS485_RX_DURING_TX));
1506 return ret;
1507 }
1508
1509 if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485)))
1510 return -EFAULT;
1511
1512 return 0;
1513}
1514
1515static int uart_get_iso7816_config(struct uart_port *port,
1516 struct serial_iso7816 __user *iso7816)
1517{
1518 struct serial_iso7816 aux;
1519
1520 if (!port->iso7816_config)
1521 return -ENOTTY;
1522
1523 scoped_guard(uart_port_lock_irqsave, port)
1524 aux = port->iso7816;
1525
1526 if (copy_to_user(iso7816, &aux, sizeof(aux)))
1527 return -EFAULT;
1528
1529 return 0;
1530}
1531
1532static int uart_set_iso7816_config(struct uart_port *port,
1533 struct serial_iso7816 __user *iso7816_user)
1534{
1535 struct serial_iso7816 iso7816;
1536 int i;
1537
1538 if (!port->iso7816_config)
1539 return -ENOTTY;
1540
1541 if (copy_from_user(&iso7816, iso7816_user, sizeof(*iso7816_user)))
1542 return -EFAULT;
1543
1544 /*
1545 * There are 5 words reserved for future use. Check that userspace
1546 * doesn't put stuff in there to prevent breakages in the future.
1547 */
1548 for (i = 0; i < ARRAY_SIZE(iso7816.reserved); i++)
1549 if (iso7816.reserved[i])
1550 return -EINVAL;
1551
1552 scoped_guard(uart_port_lock_irqsave, port) {
1553 int ret = port->iso7816_config(port, &iso7816);
1554 if (ret)
1555 return ret;
1556 }
1557
1558 if (copy_to_user(iso7816_user, &port->iso7816, sizeof(port->iso7816)))
1559 return -EFAULT;
1560
1561 return 0;
1562}
1563
1564/*
1565 * Called via sys_ioctl. We can use spin_lock_irq() here.
1566 */
1567static int
1568uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
1569{
1570 struct uart_state *state = tty->driver_data;
1571 struct tty_port *port = &state->port;
1572 struct uart_port *uport;
1573 void __user *uarg = (void __user *)arg;
1574 int ret = -ENOIOCTLCMD;
1575
1576 /* This ioctl doesn't rely on the hardware to be present. */
1577 if (cmd == TIOCSERCONFIG) {
1578 guard(rwsem_write)(&tty->termios_rwsem);
1579 return uart_do_autoconfig(tty, state);
1580 }
1581
1582 if (tty_io_error(tty))
1583 return -EIO;
1584
1585 /* This should only be used when the hardware is present. */
1586 if (cmd == TIOCMIWAIT)
1587 return uart_wait_modem_status(tty, state, arg);
1588
1589 /* rs485_config requires more locking than others */
1590 if (cmd == TIOCSRS485)
1591 down_write(&tty->termios_rwsem);
1592
1593 scoped_guard(mutex, &port->mutex) {
1594 uport = uart_port_check(state);
1595
1596 if (!uport || tty_io_error(tty)) {
1597 ret = -EIO;
1598 break;
1599 }
1600
1601 /*
1602 * All these rely on hardware being present and need to be
1603 * protected against the tty being hung up.
1604 */
1605
1606 switch (cmd) {
1607 case TIOCSERGETLSR: /* Get line status register */
1608 ret = uart_get_lsr_info(tty, state, uarg);
1609 break;
1610
1611 case TIOCGRS485:
1612 ret = uart_get_rs485_config(uport, uarg);
1613 break;
1614
1615 case TIOCSRS485:
1616 ret = uart_set_rs485_config(tty, uport, uarg);
1617 break;
1618
1619 case TIOCSISO7816:
1620 ret = uart_set_iso7816_config(state->uart_port, uarg);
1621 break;
1622
1623 case TIOCGISO7816:
1624 ret = uart_get_iso7816_config(state->uart_port, uarg);
1625 break;
1626 default:
1627 if (uport->ops->ioctl)
1628 ret = uport->ops->ioctl(uport, cmd, arg);
1629 break;
1630 }
1631 }
1632
1633 if (cmd == TIOCSRS485)
1634 up_write(&tty->termios_rwsem);
1635
1636 return ret;
1637}
1638
1639static void uart_set_ldisc(struct tty_struct *tty)
1640{
1641 struct uart_state *state = tty->driver_data;
1642 struct uart_port *uport;
1643
1644 guard(mutex)(&state->port.mutex);
1645 uport = uart_port_check(state);
1646 if (!uport || tty_io_error(tty))
1647 return;
1648
1649 if (uport->ops->set_ldisc)
1650 uport->ops->set_ldisc(uport, &tty->termios);
1651}
1652
1653static void uart_set_termios(struct tty_struct *tty,
1654 const struct ktermios *old_termios)
1655{
1656 struct uart_state *state = tty->driver_data;
1657 struct uart_port *uport;
1658 unsigned int cflag = tty->termios.c_cflag;
1659 unsigned int iflag_mask = IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK;
1660 bool sw_changed = false;
1661
1662 guard(mutex)(&state->port.mutex);
1663
1664 uport = uart_port_check(state);
1665 if (!uport || tty_io_error(tty))
1666 return;
1667
1668 /*
1669 * Drivers doing software flow control also need to know
1670 * about changes to these input settings.
1671 */
1672 if (uport->flags & UPF_SOFT_FLOW) {
1673 iflag_mask |= IXANY|IXON|IXOFF;
1674 sw_changed =
1675 tty->termios.c_cc[VSTART] != old_termios->c_cc[VSTART] ||
1676 tty->termios.c_cc[VSTOP] != old_termios->c_cc[VSTOP];
1677 }
1678
1679 /*
1680 * These are the bits that are used to setup various
1681 * flags in the low level driver. We can ignore the Bfoo
1682 * bits in c_cflag; c_[io]speed will always be set
1683 * appropriately by set_termios() in tty_ioctl.c
1684 */
1685 if ((cflag ^ old_termios->c_cflag) == 0 &&
1686 tty->termios.c_ospeed == old_termios->c_ospeed &&
1687 tty->termios.c_ispeed == old_termios->c_ispeed &&
1688 ((tty->termios.c_iflag ^ old_termios->c_iflag) & iflag_mask) == 0 &&
1689 !sw_changed)
1690 return;
1691
1692 uart_change_line_settings(tty, state, old_termios);
1693 /* reload cflag from termios; port driver may have overridden flags */
1694 cflag = tty->termios.c_cflag;
1695
1696 /* Handle transition to B0 status */
1697 if (((old_termios->c_cflag & CBAUD) != B0) && ((cflag & CBAUD) == B0))
1698 uart_clear_mctrl(uport, TIOCM_RTS | TIOCM_DTR);
1699 /* Handle transition away from B0 status */
1700 else if (((old_termios->c_cflag & CBAUD) == B0) && ((cflag & CBAUD) != B0)) {
1701 unsigned int mask = TIOCM_DTR;
1702
1703 if (!(cflag & CRTSCTS) || !tty_throttled(tty))
1704 mask |= TIOCM_RTS;
1705 uart_set_mctrl(uport, mask);
1706 }
1707}
1708
1709/*
1710 * Calls to uart_close() are serialised via the tty_lock in
1711 * drivers/tty/tty_io.c:tty_release()
1712 * drivers/tty/tty_io.c:do_tty_hangup()
1713 */
1714static void uart_close(struct tty_struct *tty, struct file *filp)
1715{
1716 struct uart_state *state = tty->driver_data;
1717
1718 if (!state) {
1719 struct uart_driver *drv = tty->driver->driver_state;
1720 struct tty_port *port;
1721
1722 state = drv->state + tty->index;
1723 port = &state->port;
1724 guard(spinlock_irq)(&port->lock);
1725 --port->count;
1726 return;
1727 }
1728
1729 pr_debug("uart_close(%d) called\n", tty->index);
1730
1731 tty_port_close(tty->port, tty, filp);
1732}
1733
1734static void uart_tty_port_shutdown(struct tty_port *port)
1735{
1736 struct uart_state *state = container_of(port, struct uart_state, port);
1737 struct uart_port *uport = uart_port_check(state);
1738
1739 /*
1740 * At this point, we stop accepting input. To do this, we
1741 * disable the receive line status interrupts.
1742 */
1743 if (WARN(!uport, "detached port still initialized!\n"))
1744 return;
1745
1746 scoped_guard(uart_port_lock_irq, uport)
1747 uport->ops->stop_rx(uport);
1748
1749 serial_base_port_shutdown(uport);
1750 uart_port_shutdown(port);
1751
1752 /*
1753 * It's possible for shutdown to be called after suspend if we get
1754 * a DCD drop (hangup) at just the right time. Clear suspended bit so
1755 * we don't try to resume a port that has been shutdown.
1756 */
1757 tty_port_set_suspended(port, false);
1758
1759 uart_free_xmit_buf(port);
1760
1761 uart_change_pm(state, UART_PM_STATE_OFF);
1762}
1763
1764static void uart_wait_until_sent(struct tty_struct *tty, int timeout)
1765{
1766 struct uart_state *state = tty->driver_data;
1767 struct uart_port *port;
1768 unsigned long char_time, expire, fifo_timeout;
1769
1770 port = uart_port_ref(state);
1771 if (!port)
1772 return;
1773
1774 if (port->type == PORT_UNKNOWN || port->fifosize == 0) {
1775 uart_port_deref(port);
1776 return;
1777 }
1778
1779 /*
1780 * Set the check interval to be 1/5 of the estimated time to
1781 * send a single character, and make it at least 1. The check
1782 * interval should also be less than the timeout.
1783 *
1784 * Note: we have to use pretty tight timings here to satisfy
1785 * the NIST-PCTS.
1786 */
1787 char_time = max(nsecs_to_jiffies(port->frame_time / 5), 1UL);
1788
1789 if (timeout && timeout < char_time)
1790 char_time = timeout;
1791
1792 if (!uart_cts_enabled(port)) {
1793 /*
1794 * If the transmitter hasn't cleared in twice the approximate
1795 * amount of time to send the entire FIFO, it probably won't
1796 * ever clear. This assumes the UART isn't doing flow
1797 * control, which is currently the case. Hence, if it ever
1798 * takes longer than FIFO timeout, this is probably due to a
1799 * UART bug of some kind. So, we clamp the timeout parameter at
1800 * 2 * FIFO timeout.
1801 */
1802 fifo_timeout = uart_fifo_timeout(port);
1803 if (timeout == 0 || timeout > 2 * fifo_timeout)
1804 timeout = 2 * fifo_timeout;
1805 }
1806
1807 expire = jiffies + timeout;
1808
1809 pr_debug("uart_wait_until_sent(%u), jiffies=%lu, expire=%lu...\n",
1810 port->line, jiffies, expire);
1811
1812 /*
1813 * Check whether the transmitter is empty every 'char_time'.
1814 * 'timeout' / 'expire' give us the maximum amount of time
1815 * we wait.
1816 */
1817 for (;;) {
1818 mutex_lock(&state->port.mutex);
1819 if (tty_io_error(tty) || port->ops->tx_empty(port)) {
1820 mutex_unlock(&state->port.mutex);
1821 break;
1822 }
1823 mutex_unlock(&state->port.mutex);
1824
1825 msleep_interruptible(jiffies_to_msecs(char_time));
1826 if (signal_pending(current))
1827 break;
1828 if (timeout && time_after(jiffies, expire))
1829 break;
1830 }
1831 uart_port_deref(port);
1832}
1833
1834/*
1835 * Calls to uart_hangup() are serialised by the tty_lock in
1836 * drivers/tty/tty_io.c:do_tty_hangup()
1837 * This runs from a workqueue and can sleep for a _short_ time only.
1838 */
1839static void uart_hangup(struct tty_struct *tty)
1840{
1841 struct uart_state *state = tty->driver_data;
1842 struct tty_port *port = &state->port;
1843 struct uart_port *uport;
1844
1845 pr_debug("uart_hangup(%d)\n", tty->index);
1846
1847 guard(mutex)(&port->mutex);
1848 uport = uart_port_check(state);
1849 WARN(!uport, "hangup of detached port!\n");
1850
1851 if (tty_port_active(port)) {
1852 uart_flush_buffer(tty);
1853 uart_shutdown(tty, state);
1854 scoped_guard(spinlock_irqsave, &port->lock)
1855 port->count = 0;
1856 tty_port_set_active(port, false);
1857 tty_port_tty_set(port, NULL);
1858 if (uport && !uart_console(uport))
1859 uart_change_pm(state, UART_PM_STATE_OFF);
1860 wake_up_interruptible(&port->open_wait);
1861 wake_up_interruptible(&port->delta_msr_wait);
1862 }
1863}
1864
1865/* uport == NULL if uart_port has already been removed */
1866static void uart_port_shutdown(struct tty_port *port)
1867{
1868 struct uart_state *state = container_of(port, struct uart_state, port);
1869 struct uart_port *uport = uart_port_check(state);
1870
1871 /*
1872 * clear delta_msr_wait queue to avoid mem leaks: we may free
1873 * the irq here so the queue might never be woken up. Note
1874 * that we won't end up waiting on delta_msr_wait again since
1875 * any outstanding file descriptors should be pointing at
1876 * hung_up_tty_fops now.
1877 */
1878 wake_up_interruptible(&port->delta_msr_wait);
1879
1880 if (uport) {
1881 /* Free the IRQ and disable the port. */
1882 uport->ops->shutdown(uport);
1883
1884 /* Ensure that the IRQ handler isn't running on another CPU. */
1885 synchronize_irq(uport->irq);
1886 }
1887}
1888
1889static bool uart_carrier_raised(struct tty_port *port)
1890{
1891 struct uart_state *state = container_of(port, struct uart_state, port);
1892 struct uart_port *uport;
1893 unsigned long flags;
1894 int mctrl;
1895
1896 uport = uart_port_ref_lock(state, &flags);
1897 /*
1898 * Should never observe uport == NULL since checks for hangup should
1899 * abort the tty_port_block_til_ready() loop before checking for carrier
1900 * raised -- but report carrier raised if it does anyway so open will
1901 * continue and not sleep
1902 */
1903 if (WARN_ON(!uport))
1904 return true;
1905 uart_enable_ms(uport);
1906 mctrl = uport->ops->get_mctrl(uport);
1907 uart_port_unlock_deref(uport, flags);
1908
1909 return mctrl & TIOCM_CAR;
1910}
1911
1912static void uart_dtr_rts(struct tty_port *port, bool active)
1913{
1914 struct uart_state *state = container_of(port, struct uart_state, port);
1915 struct uart_port *uport;
1916
1917 uport = uart_port_ref(state);
1918 if (!uport)
1919 return;
1920 uart_port_dtr_rts(uport, active);
1921 uart_port_deref(uport);
1922}
1923
1924static int uart_install(struct tty_driver *driver, struct tty_struct *tty)
1925{
1926 struct uart_driver *drv = driver->driver_state;
1927 struct uart_state *state = drv->state + tty->index;
1928
1929 tty->driver_data = state;
1930
1931 return tty_standard_install(driver, tty);
1932}
1933
1934/*
1935 * Calls to uart_open are serialised by the tty_lock in
1936 * drivers/tty/tty_io.c:tty_open()
1937 * Note that if this fails, then uart_close() _will_ be called.
1938 *
1939 * In time, we want to scrap the "opening nonpresent ports"
1940 * behaviour and implement an alternative way for setserial
1941 * to set base addresses/ports/types. This will allow us to
1942 * get rid of a certain amount of extra tests.
1943 */
1944static int uart_open(struct tty_struct *tty, struct file *filp)
1945{
1946 struct uart_state *state = tty->driver_data;
1947 int retval;
1948
1949 retval = tty_port_open(&state->port, tty, filp);
1950 if (retval > 0)
1951 retval = 0;
1952
1953 return retval;
1954}
1955
1956static int uart_port_activate(struct tty_port *port, struct tty_struct *tty)
1957{
1958 struct uart_state *state = container_of(port, struct uart_state, port);
1959 struct uart_port *uport;
1960 int ret;
1961
1962 uport = uart_port_check(state);
1963 if (!uport || uport->flags & UPF_DEAD)
1964 return -ENXIO;
1965
1966 /*
1967 * Start up the serial port.
1968 */
1969 ret = uart_startup(tty, state, false);
1970 if (ret > 0)
1971 tty_port_set_active(port, true);
1972
1973 return ret;
1974}
1975
1976static const char *uart_type(struct uart_port *port)
1977{
1978 const char *str = NULL;
1979
1980 if (port->ops->type)
1981 str = port->ops->type(port);
1982
1983 if (!str)
1984 str = "unknown";
1985
1986 return str;
1987}
1988
1989bool uart_iotype_mmio(enum uart_iotype iotype)
1990{
1991 switch (iotype) {
1992 case UPIO_MEM:
1993 case UPIO_MEM32:
1994 case UPIO_AU:
1995 case UPIO_TSI:
1996 case UPIO_MEM32BE:
1997 case UPIO_MEM16:
1998 return true;
1999 default:
2000 return false;
2001 }
2002}
2003EXPORT_SYMBOL_GPL(uart_iotype_mmio);
2004
2005bool uart_iotype_io(enum uart_iotype iotype)
2006{
2007 switch (iotype) {
2008 case UPIO_PORT:
2009 case UPIO_HUB6:
2010 return true;
2011 default:
2012 return false;
2013 }
2014}
2015EXPORT_SYMBOL_GPL(uart_iotype_io);
2016
2017#ifdef CONFIG_PROC_FS
2018
2019static void uart_line_info(struct seq_file *m, struct uart_state *state)
2020{
2021 struct tty_port *port = &state->port;
2022 enum uart_pm_state pm_state;
2023 struct uart_port *uport;
2024 char ioinfos[64];
2025 char stat_buf[32];
2026 unsigned int status;
2027
2028 guard(mutex)(&port->mutex);
2029
2030 uport = uart_port_check(state);
2031 if (!uport)
2032 return;
2033
2034 seq_printf(m, "%u: uart:%s", uport->line, uart_type(uport));
2035 uart_get_ioinfos(uport, ioinfos, sizeof(ioinfos));
2036 seq_printf(m, "%s", ioinfos);
2037 seq_printf(m, " irq:%u", uport->irq);
2038
2039 if (uport->type == PORT_UNKNOWN) {
2040 seq_putc(m, '\n');
2041 return;
2042 }
2043
2044 if (capable(CAP_SYS_ADMIN)) {
2045 pm_state = state->pm_state;
2046 if (pm_state != UART_PM_STATE_ON)
2047 uart_change_pm(state, UART_PM_STATE_ON);
2048 scoped_guard(uart_port_lock_irq, uport)
2049 status = uport->ops->get_mctrl(uport);
2050 if (pm_state != UART_PM_STATE_ON)
2051 uart_change_pm(state, pm_state);
2052
2053 seq_printf(m, " tx:%u rx:%u",
2054 uport->icount.tx, uport->icount.rx);
2055 if (uport->icount.frame)
2056 seq_printf(m, " fe:%u", uport->icount.frame);
2057 if (uport->icount.parity)
2058 seq_printf(m, " pe:%u", uport->icount.parity);
2059 if (uport->icount.brk)
2060 seq_printf(m, " brk:%u", uport->icount.brk);
2061 if (uport->icount.overrun)
2062 seq_printf(m, " oe:%u", uport->icount.overrun);
2063 if (uport->icount.buf_overrun)
2064 seq_printf(m, " bo:%u", uport->icount.buf_overrun);
2065
2066#define INFOBIT(bit, str) \
2067 if (uport->mctrl & (bit)) \
2068 strncat(stat_buf, (str), sizeof(stat_buf) - \
2069 strlen(stat_buf) - 2)
2070#define STATBIT(bit, str) \
2071 if (status & (bit)) \
2072 strncat(stat_buf, (str), sizeof(stat_buf) - \
2073 strlen(stat_buf) - 2)
2074
2075 stat_buf[0] = '\0';
2076 stat_buf[1] = '\0';
2077 INFOBIT(TIOCM_RTS, "|RTS");
2078 STATBIT(TIOCM_CTS, "|CTS");
2079 INFOBIT(TIOCM_DTR, "|DTR");
2080 STATBIT(TIOCM_DSR, "|DSR");
2081 STATBIT(TIOCM_CAR, "|CD");
2082 STATBIT(TIOCM_RNG, "|RI");
2083 if (stat_buf[0])
2084 stat_buf[0] = ' ';
2085
2086 seq_puts(m, stat_buf);
2087 }
2088 seq_putc(m, '\n');
2089#undef STATBIT
2090#undef INFOBIT
2091}
2092
2093static int uart_proc_show(struct seq_file *m, void *v)
2094{
2095 struct tty_driver *ttydrv = m->private;
2096 struct uart_driver *drv = ttydrv->driver_state;
2097 int i;
2098
2099 seq_printf(m, "serinfo:1.0 driver%s%s revision:%s\n", "", "", "");
2100 for (i = 0; i < drv->nr; i++)
2101 uart_line_info(m, drv->state + i);
2102 return 0;
2103}
2104#endif
2105
2106static void uart_port_spin_lock_init(struct uart_port *port)
2107{
2108 spin_lock_init(&port->lock);
2109 lockdep_set_class(&port->lock, &port_lock_key);
2110}
2111
2112#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
2113/**
2114 * uart_console_write - write a console message to a serial port
2115 * @port: the port to write the message
2116 * @s: array of characters
2117 * @count: number of characters in string to write
2118 * @putchar: function to write character to port
2119 */
2120void uart_console_write(struct uart_port *port, const char *s,
2121 unsigned int count,
2122 void (*putchar)(struct uart_port *, unsigned char))
2123{
2124 unsigned int i;
2125
2126 for (i = 0; i < count; i++, s++) {
2127 if (*s == '\n')
2128 putchar(port, '\r');
2129 putchar(port, *s);
2130 }
2131}
2132EXPORT_SYMBOL_GPL(uart_console_write);
2133
2134/**
2135 * uart_parse_earlycon - Parse earlycon options
2136 * @p: ptr to 2nd field (ie., just beyond '<name>,')
2137 * @iotype: ptr for decoded iotype (out)
2138 * @addr: ptr for decoded mapbase/iobase (out)
2139 * @options: ptr for <options> field; %NULL if not present (out)
2140 *
2141 * Decodes earlycon kernel command line parameters of the form:
2142 * * earlycon=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
2143 * * console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
2144 *
2145 * The optional form:
2146 * * earlycon=<name>,0x<addr>,<options>
2147 * * console=<name>,0x<addr>,<options>
2148 *
2149 * is also accepted; the returned @iotype will be %UPIO_MEM.
2150 *
2151 * Returns: 0 on success or -%EINVAL on failure
2152 */
2153int uart_parse_earlycon(char *p, enum uart_iotype *iotype,
2154 resource_size_t *addr, char **options)
2155{
2156 if (strncmp(p, "mmio,", 5) == 0) {
2157 *iotype = UPIO_MEM;
2158 p += 5;
2159 } else if (strncmp(p, "mmio16,", 7) == 0) {
2160 *iotype = UPIO_MEM16;
2161 p += 7;
2162 } else if (strncmp(p, "mmio32,", 7) == 0) {
2163 *iotype = UPIO_MEM32;
2164 p += 7;
2165 } else if (strncmp(p, "mmio32be,", 9) == 0) {
2166 *iotype = UPIO_MEM32BE;
2167 p += 9;
2168 } else if (strncmp(p, "mmio32native,", 13) == 0) {
2169 *iotype = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ?
2170 UPIO_MEM32BE : UPIO_MEM32;
2171 p += 13;
2172 } else if (strncmp(p, "io,", 3) == 0) {
2173 *iotype = UPIO_PORT;
2174 p += 3;
2175 } else if (strncmp(p, "0x", 2) == 0) {
2176 *iotype = UPIO_MEM;
2177 } else {
2178 return -EINVAL;
2179 }
2180
2181 /*
2182 * Before you replace it with kstrtoull(), think about options separator
2183 * (',') it will not tolerate
2184 */
2185 *addr = simple_strtoull(p, NULL, 0);
2186 p = strchr(p, ',');
2187 if (p)
2188 p++;
2189
2190 *options = p;
2191 return 0;
2192}
2193EXPORT_SYMBOL_GPL(uart_parse_earlycon);
2194
2195/**
2196 * uart_parse_options - Parse serial port baud/parity/bits/flow control.
2197 * @options: pointer to option string
2198 * @baud: pointer to an 'int' variable for the baud rate.
2199 * @parity: pointer to an 'int' variable for the parity.
2200 * @bits: pointer to an 'int' variable for the number of data bits.
2201 * @flow: pointer to an 'int' variable for the flow control character.
2202 *
2203 * uart_parse_options() decodes a string containing the serial console
2204 * options. The format of the string is <baud><parity><bits><flow>,
2205 * eg: 115200n8r
2206 */
2207void
2208uart_parse_options(const char *options, int *baud, int *parity,
2209 int *bits, int *flow)
2210{
2211 const char *s = options;
2212
2213 *baud = simple_strtoul(s, NULL, 10);
2214 while (*s >= '0' && *s <= '9')
2215 s++;
2216 if (*s)
2217 *parity = *s++;
2218 if (*s)
2219 *bits = *s++ - '0';
2220 if (*s)
2221 *flow = *s;
2222}
2223EXPORT_SYMBOL_GPL(uart_parse_options);
2224
2225/**
2226 * uart_set_options - setup the serial console parameters
2227 * @port: pointer to the serial ports uart_port structure
2228 * @co: console pointer
2229 * @baud: baud rate
2230 * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even)
2231 * @bits: number of data bits
2232 * @flow: flow control character - 'r' (rts)
2233 *
2234 * Locking: Caller must hold console_list_lock in order to serialize
2235 * early initialization of the serial-console lock.
2236 */
2237int
2238uart_set_options(struct uart_port *port, struct console *co,
2239 int baud, int parity, int bits, int flow)
2240{
2241 struct ktermios termios;
2242 static struct ktermios dummy;
2243
2244 /*
2245 * Ensure that the serial-console lock is initialised early.
2246 *
2247 * Note that the console-registered check is needed because
2248 * kgdboc can call uart_set_options() for an already registered
2249 * console via tty_find_polling_driver() and uart_poll_init().
2250 */
2251 if (!uart_console_registered_locked(port) && !port->console_reinit)
2252 uart_port_spin_lock_init(port);
2253
2254 memset(&termios, 0, sizeof(struct ktermios));
2255
2256 termios.c_cflag |= CREAD | HUPCL | CLOCAL;
2257 tty_termios_encode_baud_rate(&termios, baud, baud);
2258
2259 if (bits == 7)
2260 termios.c_cflag |= CS7;
2261 else
2262 termios.c_cflag |= CS8;
2263
2264 switch (parity) {
2265 case 'o': case 'O':
2266 termios.c_cflag |= PARODD;
2267 fallthrough;
2268 case 'e': case 'E':
2269 termios.c_cflag |= PARENB;
2270 break;
2271 }
2272
2273 if (flow == 'r')
2274 termios.c_cflag |= CRTSCTS;
2275
2276 /*
2277 * some uarts on other side don't support no flow control.
2278 * So we set * DTR in host uart to make them happy
2279 */
2280 port->mctrl |= TIOCM_DTR;
2281
2282 port->ops->set_termios(port, &termios, &dummy);
2283
2284 /*
2285 * If console hardware flow control was specified and is supported,
2286 * the related policy UPSTAT_CTS_ENABLE must be set to allow console
2287 * drivers to identify if CTS should be used for polling.
2288 */
2289 if (flow == 'r' && (termios.c_cflag & CRTSCTS)) {
2290 /* Synchronize @status RMW update against the console. */
2291 guard(uart_port_lock_irqsave)(port);
2292 port->status |= UPSTAT_CTS_ENABLE;
2293 }
2294
2295 /*
2296 * Allow the setting of the UART parameters with a NULL console
2297 * too:
2298 */
2299 if (co) {
2300 co->cflag = termios.c_cflag;
2301 co->ispeed = termios.c_ispeed;
2302 co->ospeed = termios.c_ospeed;
2303 }
2304
2305 return 0;
2306}
2307EXPORT_SYMBOL_GPL(uart_set_options);
2308#endif /* CONFIG_SERIAL_CORE_CONSOLE */
2309
2310/**
2311 * uart_change_pm - set power state of the port
2312 *
2313 * @state: port descriptor
2314 * @pm_state: new state
2315 *
2316 * Locking: port->mutex has to be held
2317 */
2318static void uart_change_pm(struct uart_state *state,
2319 enum uart_pm_state pm_state)
2320{
2321 struct uart_port *port = uart_port_check(state);
2322
2323 if (state->pm_state != pm_state) {
2324 if (port && port->ops->pm)
2325 port->ops->pm(port, pm_state, state->pm_state);
2326 state->pm_state = pm_state;
2327 }
2328}
2329
2330struct uart_match {
2331 struct uart_port *port;
2332 struct uart_driver *driver;
2333};
2334
2335static int serial_match_port(struct device *dev, const void *data)
2336{
2337 const struct uart_match *match = data;
2338 struct tty_driver *tty_drv = match->driver->tty_driver;
2339 dev_t devt = MKDEV(tty_drv->major, tty_drv->minor_start) +
2340 match->port->line;
2341
2342 return dev->devt == devt; /* Actually, only one tty per port */
2343}
2344
2345int uart_suspend_port(struct uart_driver *drv, struct uart_port *uport)
2346{
2347 struct uart_state *state = drv->state + uport->line;
2348 struct tty_port *port = &state->port;
2349 struct device *tty_dev;
2350 struct uart_match match = {uport, drv};
2351
2352 guard(mutex)(&port->mutex);
2353
2354 tty_dev = device_find_child(&uport->port_dev->dev, &match, serial_match_port);
2355 if (tty_dev && device_may_wakeup(tty_dev)) {
2356 enable_irq_wake(uport->irq);
2357 put_device(tty_dev);
2358 return 0;
2359 }
2360 put_device(tty_dev);
2361
2362 /*
2363 * Nothing to do if the console is not suspending
2364 * except stop_rx to prevent any asynchronous data
2365 * over RX line. However ensure that we will be
2366 * able to Re-start_rx later.
2367 */
2368 if (!console_suspend_enabled && uart_console(uport)) {
2369 if (uport->ops->start_rx) {
2370 guard(uart_port_lock_irq)(uport);
2371 uport->ops->stop_rx(uport);
2372 }
2373 device_set_awake_path(uport->dev);
2374 return 0;
2375 }
2376
2377 uport->suspended = 1;
2378
2379 if (tty_port_initialized(port)) {
2380 const struct uart_ops *ops = uport->ops;
2381 int tries;
2382 unsigned int mctrl;
2383
2384 tty_port_set_suspended(port, true);
2385 tty_port_set_initialized(port, false);
2386
2387 scoped_guard(uart_port_lock_irq, uport) {
2388 ops->stop_tx(uport);
2389 if (!(uport->rs485.flags & SER_RS485_ENABLED))
2390 ops->set_mctrl(uport, 0);
2391 /* save mctrl so it can be restored on resume */
2392 mctrl = uport->mctrl;
2393 uport->mctrl = 0;
2394 ops->stop_rx(uport);
2395 }
2396
2397 /*
2398 * Wait for the transmitter to empty.
2399 */
2400 for (tries = 3; !ops->tx_empty(uport) && tries; tries--)
2401 msleep(10);
2402 if (!tries)
2403 dev_err(uport->dev, "%s: Unable to drain transmitter\n",
2404 uport->name);
2405
2406 ops->shutdown(uport);
2407 uport->mctrl = mctrl;
2408 }
2409
2410 /*
2411 * Suspend the console device before suspending the port.
2412 */
2413 if (uart_console(uport))
2414 console_suspend(uport->cons);
2415
2416 uart_change_pm(state, UART_PM_STATE_OFF);
2417
2418 return 0;
2419}
2420EXPORT_SYMBOL(uart_suspend_port);
2421
2422int uart_resume_port(struct uart_driver *drv, struct uart_port *uport)
2423{
2424 struct uart_state *state = drv->state + uport->line;
2425 struct tty_port *port = &state->port;
2426 struct device *tty_dev;
2427 struct uart_match match = {uport, drv};
2428 struct ktermios termios;
2429
2430 guard(mutex)(&port->mutex);
2431
2432 tty_dev = device_find_child(&uport->port_dev->dev, &match, serial_match_port);
2433 if (!uport->suspended && device_may_wakeup(tty_dev)) {
2434 if (irqd_is_wakeup_set(irq_get_irq_data((uport->irq))))
2435 disable_irq_wake(uport->irq);
2436 put_device(tty_dev);
2437 return 0;
2438 }
2439 put_device(tty_dev);
2440 uport->suspended = 0;
2441
2442 /*
2443 * Re-enable the console device after suspending.
2444 */
2445 if (uart_console(uport)) {
2446 /*
2447 * First try to use the console cflag setting.
2448 */
2449 memset(&termios, 0, sizeof(struct ktermios));
2450 termios.c_cflag = uport->cons->cflag;
2451 termios.c_ispeed = uport->cons->ispeed;
2452 termios.c_ospeed = uport->cons->ospeed;
2453
2454 /*
2455 * If that's unset, use the tty termios setting.
2456 */
2457 if (port->tty && termios.c_cflag == 0)
2458 termios = port->tty->termios;
2459
2460 if (console_suspend_enabled)
2461 uart_change_pm(state, UART_PM_STATE_ON);
2462 uport->ops->set_termios(uport, &termios, NULL);
2463 if (!console_suspend_enabled && uport->ops->start_rx) {
2464 guard(uart_port_lock_irq)(uport);
2465 uport->ops->start_rx(uport);
2466 }
2467 if (console_suspend_enabled)
2468 console_resume(uport->cons);
2469 }
2470
2471 if (tty_port_suspended(port)) {
2472 const struct uart_ops *ops = uport->ops;
2473 int ret;
2474
2475 uart_change_pm(state, UART_PM_STATE_ON);
2476 scoped_guard(uart_port_lock_irq, uport)
2477 if (!(uport->rs485.flags & SER_RS485_ENABLED))
2478 ops->set_mctrl(uport, 0);
2479 if (console_suspend_enabled || !uart_console(uport)) {
2480 /* Protected by port mutex for now */
2481 struct tty_struct *tty = port->tty;
2482
2483 ret = ops->startup(uport);
2484 if (ret == 0) {
2485 if (tty)
2486 uart_change_line_settings(tty, state, NULL);
2487 uart_rs485_config(uport);
2488 scoped_guard(uart_port_lock_irq, uport) {
2489 if (!(uport->rs485.flags & SER_RS485_ENABLED))
2490 ops->set_mctrl(uport, uport->mctrl);
2491 ops->start_tx(uport);
2492 }
2493 tty_port_set_initialized(port, true);
2494 } else {
2495 /*
2496 * Failed to resume - maybe hardware went away?
2497 * Clear the "initialized" flag so we won't try
2498 * to call the low level drivers shutdown method.
2499 */
2500 uart_shutdown(tty, state);
2501 }
2502 }
2503
2504 tty_port_set_suspended(port, false);
2505 }
2506
2507 return 0;
2508}
2509EXPORT_SYMBOL(uart_resume_port);
2510
2511static const char *uart_get_mmio_width(struct uart_port *port)
2512{
2513 switch (port->iotype) {
2514 case UPIO_MEM16:
2515 return "16";
2516 case UPIO_MEM32:
2517 case UPIO_MEM32BE:
2518 return "32";
2519 case UPIO_AU:
2520 case UPIO_MEM:
2521 default:
2522 return "";
2523 }
2524}
2525
2526void uart_get_ioinfos(struct uart_port *port, char *buf, size_t size)
2527{
2528 buf[0] = '\0';
2529
2530 if (uart_iotype_mmio(port->iotype)) {
2531 scnprintf(buf, size, " MMIO%s:%pa", uart_get_mmio_width(port), &port->mapbase);
2532 } else if (uart_iotype_io(port->iotype)) {
2533 if (port->iotype == UPIO_PORT)
2534 scnprintf(buf, size, " I/O:0x%lx", port->iobase);
2535 else if (port->iotype == UPIO_HUB6)
2536 scnprintf(buf, size, " I/O:0x%lx, offset 0x%x", port->iobase, port->hub6);
2537 }
2538}
2539EXPORT_SYMBOL(uart_get_ioinfos);
2540
2541static inline void
2542uart_report_port(struct uart_driver *drv, struct uart_port *port)
2543{
2544 char ioinfos[64];
2545
2546 uart_get_ioinfos(port, ioinfos, sizeof(ioinfos));
2547
2548 pr_info("%s%s%s%s (irq = %u, base_baud = %u) is a %s\n",
2549 port->dev ? dev_name(port->dev) : "",
2550 port->dev ? ": " : "",
2551 port->name, ioinfos, port->irq, port->uartclk / 16, uart_type(port));
2552
2553 /* The magic multiplier feature is a bit obscure, so report it too. */
2554 if (port->flags & UPF_MAGIC_MULTIPLIER)
2555 pr_info("%s%s%s extra baud rates supported: %u, %u",
2556 port->dev ? dev_name(port->dev) : "",
2557 port->dev ? ": " : "",
2558 port->name,
2559 port->uartclk / 8, port->uartclk / 4);
2560}
2561
2562static void
2563uart_configure_port(struct uart_driver *drv, struct uart_state *state,
2564 struct uart_port *port)
2565{
2566 unsigned int flags;
2567
2568 /* If there isn't a port here, don't do anything further. */
2569 if (uart_iotype_mmio(port->iotype) || uart_iotype_io(port->iotype))
2570 if (!port->iobase && !port->mapbase && !port->membase)
2571 return;
2572
2573 /*
2574 * Now do the auto configuration stuff. Note that config_port
2575 * is expected to claim the resources and map the port for us.
2576 */
2577 flags = 0;
2578 if (port->flags & UPF_AUTO_IRQ)
2579 flags |= UART_CONFIG_IRQ;
2580 if (port->flags & UPF_BOOT_AUTOCONF) {
2581 if (!(port->flags & UPF_FIXED_TYPE)) {
2582 port->type = PORT_UNKNOWN;
2583 flags |= UART_CONFIG_TYPE;
2584 }
2585 /* Synchronize with possible boot console. */
2586 if (uart_console(port))
2587 console_lock();
2588 port->ops->config_port(port, flags);
2589 if (uart_console(port))
2590 console_unlock();
2591 }
2592
2593 if (port->type != PORT_UNKNOWN) {
2594 uart_report_port(drv, port);
2595
2596 /* Synchronize with possible boot console. */
2597 if (uart_console(port))
2598 console_lock();
2599
2600 /* Power up port for set_mctrl() */
2601 uart_change_pm(state, UART_PM_STATE_ON);
2602
2603 /*
2604 * Ensure that the modem control lines are de-activated.
2605 * keep the DTR setting that is set in uart_set_options()
2606 * We probably don't need a spinlock around this, but
2607 */
2608 scoped_guard(uart_port_lock_irqsave, port) {
2609 unsigned int mask = TIOCM_DTR;
2610
2611 /* Console hardware flow control polls CTS. */
2612 if (uart_console_hwflow_active(port))
2613 mask |= TIOCM_RTS;
2614
2615 port->mctrl &= mask;
2616
2617 if (!(port->rs485.flags & SER_RS485_ENABLED))
2618 port->ops->set_mctrl(port, port->mctrl);
2619 }
2620
2621 uart_rs485_config(port);
2622
2623 if (uart_console(port))
2624 console_unlock();
2625
2626 /*
2627 * If this driver supports console, and it hasn't been
2628 * successfully registered yet, try to re-register it.
2629 * It may be that the port was not available.
2630 */
2631 if (port->cons && !console_is_registered(port->cons))
2632 register_console(port->cons);
2633
2634 /*
2635 * Power down all ports by default, except the
2636 * console if we have one.
2637 */
2638 if (!uart_console(port))
2639 uart_change_pm(state, UART_PM_STATE_OFF);
2640 }
2641}
2642
2643#ifdef CONFIG_CONSOLE_POLL
2644
2645static int uart_poll_init(struct tty_driver *driver, int line, char *options)
2646{
2647 struct uart_driver *drv = driver->driver_state;
2648 struct uart_state *state = drv->state + line;
2649 enum uart_pm_state pm_state;
2650 struct tty_port *tport;
2651 struct uart_port *port;
2652 int baud = 9600;
2653 int bits = 8;
2654 int parity = 'n';
2655 int flow = 'n';
2656 int ret = 0;
2657
2658 tport = &state->port;
2659
2660 guard(mutex)(&tport->mutex);
2661
2662 port = uart_port_check(state);
2663 if (!port || port->type == PORT_UNKNOWN ||
2664 !(port->ops->poll_get_char && port->ops->poll_put_char))
2665 return -1;
2666
2667 pm_state = state->pm_state;
2668 uart_change_pm(state, UART_PM_STATE_ON);
2669
2670 if (port->ops->poll_init) {
2671 /*
2672 * We don't set initialized as we only initialized the hw,
2673 * e.g. state->xmit is still uninitialized.
2674 */
2675 if (!tty_port_initialized(tport))
2676 ret = port->ops->poll_init(port);
2677 }
2678
2679 if (!ret && options) {
2680 uart_parse_options(options, &baud, &parity, &bits, &flow);
2681 console_list_lock();
2682 ret = uart_set_options(port, NULL, baud, parity, bits, flow);
2683 console_list_unlock();
2684 }
2685
2686 if (ret)
2687 uart_change_pm(state, pm_state);
2688
2689 return ret;
2690}
2691
2692static int uart_poll_get_char(struct tty_driver *driver, int line)
2693{
2694 struct uart_driver *drv = driver->driver_state;
2695 struct uart_state *state = drv->state + line;
2696 struct uart_port *port;
2697 int ret = -1;
2698
2699 port = uart_port_ref(state);
2700 if (port) {
2701 ret = port->ops->poll_get_char(port);
2702 uart_port_deref(port);
2703 }
2704
2705 return ret;
2706}
2707
2708static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
2709{
2710 struct uart_driver *drv = driver->driver_state;
2711 struct uart_state *state = drv->state + line;
2712 struct uart_port *port;
2713
2714 port = uart_port_ref(state);
2715 if (!port)
2716 return;
2717
2718 if (ch == '\n')
2719 port->ops->poll_put_char(port, '\r');
2720 port->ops->poll_put_char(port, ch);
2721 uart_port_deref(port);
2722}
2723#endif
2724
2725static const struct tty_operations uart_ops = {
2726 .install = uart_install,
2727 .open = uart_open,
2728 .close = uart_close,
2729 .write = uart_write,
2730 .put_char = uart_put_char,
2731 .flush_chars = uart_flush_chars,
2732 .write_room = uart_write_room,
2733 .chars_in_buffer= uart_chars_in_buffer,
2734 .flush_buffer = uart_flush_buffer,
2735 .ioctl = uart_ioctl,
2736 .throttle = uart_throttle,
2737 .unthrottle = uart_unthrottle,
2738 .send_xchar = uart_send_xchar,
2739 .set_termios = uart_set_termios,
2740 .set_ldisc = uart_set_ldisc,
2741 .stop = uart_stop,
2742 .start = uart_start,
2743 .hangup = uart_hangup,
2744 .break_ctl = uart_break_ctl,
2745 .wait_until_sent= uart_wait_until_sent,
2746#ifdef CONFIG_PROC_FS
2747 .proc_show = uart_proc_show,
2748#endif
2749 .tiocmget = uart_tiocmget,
2750 .tiocmset = uart_tiocmset,
2751 .set_serial = uart_set_info_user,
2752 .get_serial = uart_get_info_user,
2753 .get_icount = uart_get_icount,
2754#ifdef CONFIG_CONSOLE_POLL
2755 .poll_init = uart_poll_init,
2756 .poll_get_char = uart_poll_get_char,
2757 .poll_put_char = uart_poll_put_char,
2758#endif
2759};
2760
2761static const struct tty_port_operations uart_port_ops = {
2762 .carrier_raised = uart_carrier_raised,
2763 .dtr_rts = uart_dtr_rts,
2764 .activate = uart_port_activate,
2765 .shutdown = uart_tty_port_shutdown,
2766};
2767
2768/**
2769 * uart_register_driver - register a driver with the uart core layer
2770 * @drv: low level driver structure
2771 *
2772 * Register a uart driver with the core driver. We in turn register with the
2773 * tty layer, and initialise the core driver per-port state.
2774 *
2775 * We have a proc file in /proc/tty/driver which is named after the normal
2776 * driver.
2777 *
2778 * @drv->port should be %NULL, and the per-port structures should be registered
2779 * using uart_add_one_port() after this call has succeeded.
2780 *
2781 * Locking: none, Interrupts: enabled
2782 */
2783int uart_register_driver(struct uart_driver *drv)
2784{
2785 struct tty_driver *normal;
2786 int i, retval = -ENOMEM;
2787
2788 BUG_ON(drv->state);
2789
2790 /*
2791 * Maybe we should be using a slab cache for this, especially if
2792 * we have a large number of ports to handle.
2793 */
2794 drv->state = kzalloc_objs(struct uart_state, drv->nr);
2795 if (!drv->state)
2796 goto out;
2797
2798 normal = tty_alloc_driver(drv->nr, TTY_DRIVER_REAL_RAW |
2799 TTY_DRIVER_DYNAMIC_DEV);
2800 if (IS_ERR(normal)) {
2801 retval = PTR_ERR(normal);
2802 goto out_kfree;
2803 }
2804
2805 drv->tty_driver = normal;
2806
2807 normal->driver_name = drv->driver_name;
2808 normal->name = drv->dev_name;
2809 normal->major = drv->major;
2810 normal->minor_start = drv->minor;
2811 normal->type = TTY_DRIVER_TYPE_SERIAL;
2812 normal->subtype = SERIAL_TYPE_NORMAL;
2813 normal->init_termios = tty_std_termios;
2814 normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2815 normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;
2816 normal->driver_state = drv;
2817 tty_set_operations(normal, &uart_ops);
2818
2819 /*
2820 * Initialise the UART state(s).
2821 */
2822 for (i = 0; i < drv->nr; i++) {
2823 struct uart_state *state = drv->state + i;
2824 struct tty_port *port = &state->port;
2825
2826 tty_port_init(port);
2827 port->ops = &uart_port_ops;
2828 }
2829
2830 retval = tty_register_driver(normal);
2831 if (retval >= 0)
2832 return retval;
2833
2834 for (i = 0; i < drv->nr; i++)
2835 tty_port_destroy(&drv->state[i].port);
2836 tty_driver_kref_put(normal);
2837 drv->tty_driver = NULL;
2838out_kfree:
2839 kfree(drv->state);
2840 drv->state = NULL;
2841out:
2842 return retval;
2843}
2844EXPORT_SYMBOL(uart_register_driver);
2845
2846/**
2847 * uart_unregister_driver - remove a driver from the uart core layer
2848 * @drv: low level driver structure
2849 *
2850 * Remove all references to a driver from the core driver. The low level
2851 * driver must have removed all its ports via the uart_remove_one_port() if it
2852 * registered them with uart_add_one_port(). (I.e. @drv->port is %NULL.)
2853 *
2854 * Locking: none, Interrupts: enabled
2855 */
2856void uart_unregister_driver(struct uart_driver *drv)
2857{
2858 struct tty_driver *p = drv->tty_driver;
2859 unsigned int i;
2860
2861 tty_unregister_driver(p);
2862 tty_driver_kref_put(p);
2863 for (i = 0; i < drv->nr; i++)
2864 tty_port_destroy(&drv->state[i].port);
2865 kfree(drv->state);
2866 drv->state = NULL;
2867 drv->tty_driver = NULL;
2868}
2869EXPORT_SYMBOL(uart_unregister_driver);
2870
2871struct tty_driver *uart_console_device(struct console *co, int *index)
2872{
2873 struct uart_driver *p = co->data;
2874 *index = co->index;
2875 return p->tty_driver;
2876}
2877EXPORT_SYMBOL_GPL(uart_console_device);
2878
2879static ssize_t uartclk_show(struct device *dev,
2880 struct device_attribute *attr, char *buf)
2881{
2882 struct serial_struct tmp;
2883 struct tty_port *port = dev_get_drvdata(dev);
2884
2885 uart_get_info(port, &tmp);
2886 return sprintf(buf, "%d\n", tmp.baud_base * 16);
2887}
2888
2889static ssize_t type_show(struct device *dev,
2890 struct device_attribute *attr, char *buf)
2891{
2892 struct serial_struct tmp;
2893 struct tty_port *port = dev_get_drvdata(dev);
2894
2895 uart_get_info(port, &tmp);
2896 return sprintf(buf, "%d\n", tmp.type);
2897}
2898
2899static ssize_t line_show(struct device *dev,
2900 struct device_attribute *attr, char *buf)
2901{
2902 struct serial_struct tmp;
2903 struct tty_port *port = dev_get_drvdata(dev);
2904
2905 uart_get_info(port, &tmp);
2906 return sprintf(buf, "%d\n", tmp.line);
2907}
2908
2909static ssize_t port_show(struct device *dev,
2910 struct device_attribute *attr, char *buf)
2911{
2912 struct serial_struct tmp;
2913 struct tty_port *port = dev_get_drvdata(dev);
2914 unsigned long ioaddr;
2915
2916 uart_get_info(port, &tmp);
2917 ioaddr = tmp.port;
2918 if (HIGH_BITS_OFFSET)
2919 ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
2920 return sprintf(buf, "0x%lX\n", ioaddr);
2921}
2922
2923static ssize_t irq_show(struct device *dev,
2924 struct device_attribute *attr, char *buf)
2925{
2926 struct serial_struct tmp;
2927 struct tty_port *port = dev_get_drvdata(dev);
2928
2929 uart_get_info(port, &tmp);
2930 return sprintf(buf, "%d\n", tmp.irq);
2931}
2932
2933static ssize_t flags_show(struct device *dev,
2934 struct device_attribute *attr, char *buf)
2935{
2936 struct serial_struct tmp;
2937 struct tty_port *port = dev_get_drvdata(dev);
2938
2939 uart_get_info(port, &tmp);
2940 return sprintf(buf, "0x%X\n", tmp.flags);
2941}
2942
2943static ssize_t xmit_fifo_size_show(struct device *dev,
2944 struct device_attribute *attr, char *buf)
2945{
2946 struct serial_struct tmp;
2947 struct tty_port *port = dev_get_drvdata(dev);
2948
2949 uart_get_info(port, &tmp);
2950 return sprintf(buf, "%d\n", tmp.xmit_fifo_size);
2951}
2952
2953static ssize_t close_delay_show(struct device *dev,
2954 struct device_attribute *attr, char *buf)
2955{
2956 struct serial_struct tmp;
2957 struct tty_port *port = dev_get_drvdata(dev);
2958
2959 uart_get_info(port, &tmp);
2960 return sprintf(buf, "%u\n", tmp.close_delay);
2961}
2962
2963static ssize_t closing_wait_show(struct device *dev,
2964 struct device_attribute *attr, char *buf)
2965{
2966 struct serial_struct tmp;
2967 struct tty_port *port = dev_get_drvdata(dev);
2968
2969 uart_get_info(port, &tmp);
2970 return sprintf(buf, "%u\n", tmp.closing_wait);
2971}
2972
2973static ssize_t custom_divisor_show(struct device *dev,
2974 struct device_attribute *attr, char *buf)
2975{
2976 struct serial_struct tmp;
2977 struct tty_port *port = dev_get_drvdata(dev);
2978
2979 uart_get_info(port, &tmp);
2980 return sprintf(buf, "%d\n", tmp.custom_divisor);
2981}
2982
2983static ssize_t io_type_show(struct device *dev,
2984 struct device_attribute *attr, char *buf)
2985{
2986 struct serial_struct tmp;
2987 struct tty_port *port = dev_get_drvdata(dev);
2988
2989 uart_get_info(port, &tmp);
2990 return sprintf(buf, "%u\n", tmp.io_type);
2991}
2992
2993static ssize_t iomem_base_show(struct device *dev,
2994 struct device_attribute *attr, char *buf)
2995{
2996 struct serial_struct tmp;
2997 struct tty_port *port = dev_get_drvdata(dev);
2998
2999 uart_get_info(port, &tmp);
3000 return sprintf(buf, "0x%lX\n", (unsigned long)tmp.iomem_base);
3001}
3002
3003static ssize_t iomem_reg_shift_show(struct device *dev,
3004 struct device_attribute *attr, char *buf)
3005{
3006 struct serial_struct tmp;
3007 struct tty_port *port = dev_get_drvdata(dev);
3008
3009 uart_get_info(port, &tmp);
3010 return sprintf(buf, "%u\n", tmp.iomem_reg_shift);
3011}
3012
3013static ssize_t console_show(struct device *dev,
3014 struct device_attribute *attr, char *buf)
3015{
3016 struct tty_port *port = dev_get_drvdata(dev);
3017 struct uart_state *state = container_of(port, struct uart_state, port);
3018 struct uart_port *uport;
3019 bool console = false;
3020
3021 scoped_guard(mutex, &port->mutex) {
3022 uport = uart_port_check(state);
3023 if (uport)
3024 console = uart_console_registered(uport);
3025 }
3026
3027 return sprintf(buf, "%c\n", console ? 'Y' : 'N');
3028}
3029
3030static ssize_t console_store(struct device *dev,
3031 struct device_attribute *attr, const char *buf, size_t count)
3032{
3033 struct tty_port *port = dev_get_drvdata(dev);
3034 struct uart_state *state = container_of(port, struct uart_state, port);
3035 struct uart_port *uport;
3036 bool oldconsole, newconsole;
3037 int ret;
3038
3039 ret = kstrtobool(buf, &newconsole);
3040 if (ret)
3041 return ret;
3042
3043 guard(mutex)(&port->mutex);
3044 uport = uart_port_check(state);
3045 if (!uport)
3046 return -ENXIO;
3047
3048 oldconsole = uart_console_registered(uport);
3049 if (oldconsole && !newconsole) {
3050 ret = unregister_console(uport->cons);
3051 if (ret < 0)
3052 return ret;
3053 } else if (!oldconsole && newconsole) {
3054 if (!uart_console(uport))
3055 return -ENOENT;
3056
3057 uport->console_reinit = 1;
3058 register_console(uport->cons);
3059 }
3060
3061 return count;
3062}
3063
3064static DEVICE_ATTR_RO(uartclk);
3065static DEVICE_ATTR_RO(type);
3066static DEVICE_ATTR_RO(line);
3067static DEVICE_ATTR_RO(port);
3068static DEVICE_ATTR_RO(irq);
3069static DEVICE_ATTR_RO(flags);
3070static DEVICE_ATTR_RO(xmit_fifo_size);
3071static DEVICE_ATTR_RO(close_delay);
3072static DEVICE_ATTR_RO(closing_wait);
3073static DEVICE_ATTR_RO(custom_divisor);
3074static DEVICE_ATTR_RO(io_type);
3075static DEVICE_ATTR_RO(iomem_base);
3076static DEVICE_ATTR_RO(iomem_reg_shift);
3077static DEVICE_ATTR_RW(console);
3078
3079static struct attribute *tty_dev_attrs[] = {
3080 &dev_attr_uartclk.attr,
3081 &dev_attr_type.attr,
3082 &dev_attr_line.attr,
3083 &dev_attr_port.attr,
3084 &dev_attr_irq.attr,
3085 &dev_attr_flags.attr,
3086 &dev_attr_xmit_fifo_size.attr,
3087 &dev_attr_close_delay.attr,
3088 &dev_attr_closing_wait.attr,
3089 &dev_attr_custom_divisor.attr,
3090 &dev_attr_io_type.attr,
3091 &dev_attr_iomem_base.attr,
3092 &dev_attr_iomem_reg_shift.attr,
3093 &dev_attr_console.attr,
3094 NULL
3095};
3096
3097static const struct attribute_group tty_dev_attr_group = {
3098 .attrs = tty_dev_attrs,
3099};
3100
3101/**
3102 * serial_core_add_one_port - attach a driver-defined port structure
3103 * @drv: pointer to the uart low level driver structure for this port
3104 * @uport: uart port structure to use for this port.
3105 *
3106 * Context: task context, might sleep
3107 *
3108 * This allows the driver @drv to register its own uart_port structure with the
3109 * core driver. The main purpose is to allow the low level uart drivers to
3110 * expand uart_port, rather than having yet more levels of structures.
3111 * Caller must hold port_mutex.
3112 */
3113static int serial_core_add_one_port(struct uart_driver *drv, struct uart_port *uport)
3114{
3115 struct uart_state *state;
3116 struct tty_port *port;
3117 struct device *tty_dev;
3118
3119 if (uport->line >= drv->nr)
3120 return -EINVAL;
3121
3122 state = drv->state + uport->line;
3123 port = &state->port;
3124
3125 guard(mutex)(&port->mutex);
3126 if (state->uart_port)
3127 return -EINVAL;
3128
3129 uport->name = kasprintf(GFP_KERNEL, "%s%u", drv->dev_name,
3130 drv->tty_driver->name_base + uport->line);
3131 if (!uport->name)
3132 return -ENOMEM;
3133
3134 /*
3135 * uart_configure_port() may set uport->attr_group and register the
3136 * console. Allocate room for both groups and a NULL terminator first.
3137 */
3138 uport->tty_groups = kzalloc_objs(*uport->tty_groups, 3);
3139 if (!uport->tty_groups) {
3140 kfree(uport->name);
3141 return -ENOMEM;
3142 }
3143 uport->tty_groups[0] = &tty_dev_attr_group;
3144
3145 /* Link the port to the driver state table and vice versa */
3146 atomic_set(&state->refcount, 1);
3147 init_waitqueue_head(&state->remove_wait);
3148 state->uart_port = uport;
3149 uport->state = state;
3150
3151 /*
3152 * If this port is in use as a console then the spinlock is already
3153 * initialised.
3154 */
3155 if (!uart_console_registered(uport))
3156 uart_port_spin_lock_init(uport);
3157
3158 state->pm_state = UART_PM_STATE_UNDEFINED;
3159 uart_port_set_cons(uport, drv->cons);
3160 uport->minor = drv->tty_driver->minor_start + uport->line;
3161
3162 if (uport->cons && uport->dev)
3163 of_console_check(uport->dev->of_node, uport->cons->name, uport->line);
3164
3165 /*
3166 * TTY port has to be linked with the driver before register_console()
3167 * in uart_configure_port(), because user-space could open the console
3168 * immediately after.
3169 */
3170 tty_port_link_device(port, drv->tty_driver, uport->line);
3171 uart_configure_port(drv, state, uport);
3172
3173 port->console = uart_console(uport);
3174
3175 if (uport->attr_group)
3176 uport->tty_groups[1] = uport->attr_group;
3177
3178 /* Ensure serdev drivers can call serdev_device_open() right away */
3179 uport->flags &= ~UPF_DEAD;
3180
3181 /*
3182 * Register the port whether it's detected or not. This allows
3183 * setserial to be used to alter this port's parameters.
3184 */
3185 tty_dev = tty_port_register_device_attr_serdev(port, drv->tty_driver,
3186 uport->line, uport->dev, &uport->port_dev->dev, port,
3187 uport->tty_groups);
3188 if (!IS_ERR(tty_dev)) {
3189 device_set_wakeup_capable(tty_dev, 1);
3190 } else {
3191 uport->flags |= UPF_DEAD;
3192 dev_err(uport->dev, "Cannot register tty device on line %u\n",
3193 uport->line);
3194 }
3195
3196 return 0;
3197}
3198
3199/**
3200 * serial_core_remove_one_port - detach a driver defined port structure
3201 * @drv: pointer to the uart low level driver structure for this port
3202 * @uport: uart port structure for this port
3203 *
3204 * Context: task context, might sleep
3205 *
3206 * This unhooks (and hangs up) the specified port structure from the core
3207 * driver. No further calls will be made to the low-level code for this port.
3208 * Caller must hold port_mutex.
3209 */
3210static void serial_core_remove_one_port(struct uart_driver *drv,
3211 struct uart_port *uport)
3212{
3213 struct uart_state *state = drv->state + uport->line;
3214 struct tty_port *port = &state->port;
3215 struct uart_port *uart_port;
3216
3217 scoped_guard(mutex, &port->mutex) {
3218 uart_port = uart_port_check(state);
3219 if (uart_port != uport)
3220 dev_alert(uport->dev, "Removing wrong port: %p != %p\n", uart_port, uport);
3221
3222 if (!uart_port)
3223 return;
3224 }
3225
3226 /*
3227 * Remove the devices from the tty layer
3228 */
3229 tty_port_unregister_device(port, drv->tty_driver, uport->line);
3230
3231 tty_port_tty_vhangup(port);
3232
3233 /*
3234 * If the port is used as a console, unregister it
3235 */
3236 if (uart_console(uport))
3237 unregister_console(uport->cons);
3238
3239 /*
3240 * Free the port IO and memory resources, if any.
3241 */
3242 if (uport->type != PORT_UNKNOWN && uport->ops->release_port)
3243 uport->ops->release_port(uport);
3244 kfree(uport->tty_groups);
3245 kfree(uport->name);
3246
3247 /*
3248 * Indicate that there isn't a port here anymore.
3249 */
3250 uport->type = PORT_UNKNOWN;
3251 uport->port_dev = NULL;
3252
3253 guard(mutex)(&port->mutex);
3254 WARN_ON(atomic_dec_return(&state->refcount) < 0);
3255 wait_event(state->remove_wait, !atomic_read(&state->refcount));
3256 state->uart_port = NULL;
3257}
3258
3259/**
3260 * uart_match_port - are the two ports equivalent?
3261 * @port1: first port
3262 * @port2: second port
3263 *
3264 * This utility function can be used to determine whether two uart_port
3265 * structures describe the same port.
3266 */
3267bool uart_match_port(const struct uart_port *port1,
3268 const struct uart_port *port2)
3269{
3270 if (port1->iotype != port2->iotype)
3271 return false;
3272 else if (port1->iotype == UPIO_PORT)
3273 return port1->iobase == port2->iobase;
3274 else if (port1->iotype == UPIO_HUB6)
3275 return hub6_match_port(port1, port2);
3276 else if (uart_iotype_mmio(port1->iotype))
3277 return port1->mapbase == port2->mapbase;
3278 else if (port1->iotype == UPIO_BUS)
3279 return true;
3280 else
3281 return false;
3282}
3283EXPORT_SYMBOL(uart_match_port);
3284
3285static struct serial_ctrl_device *
3286serial_core_get_ctrl_dev(struct serial_port_device *port_dev)
3287{
3288 struct device *dev = &port_dev->dev;
3289
3290 return to_serial_base_ctrl_device(dev->parent);
3291}
3292
3293/*
3294 * Find a registered serial core controller device if one exists. Returns
3295 * the first device matching the ctrl_id. Caller must hold port_mutex.
3296 */
3297static struct serial_ctrl_device *serial_core_ctrl_find(struct uart_driver *drv,
3298 struct device *phys_dev,
3299 int ctrl_id)
3300{
3301 struct uart_state *state;
3302 int i;
3303
3304 lockdep_assert_held(&port_mutex);
3305
3306 for (i = 0; i < drv->nr; i++) {
3307 state = drv->state + i;
3308 if (!state->uart_port || !state->uart_port->port_dev)
3309 continue;
3310
3311 if (state->uart_port->dev == phys_dev &&
3312 state->uart_port->ctrl_id == ctrl_id)
3313 return serial_core_get_ctrl_dev(state->uart_port->port_dev);
3314 }
3315
3316 return NULL;
3317}
3318
3319static struct serial_ctrl_device *serial_core_ctrl_device_add(struct uart_port *port)
3320{
3321 return serial_base_ctrl_add(port, port->dev);
3322}
3323
3324static int serial_core_port_device_add(struct serial_ctrl_device *ctrl_dev,
3325 struct uart_port *port)
3326{
3327 struct serial_port_device *port_dev;
3328
3329 port_dev = serial_base_port_add(port, ctrl_dev);
3330 if (IS_ERR(port_dev))
3331 return PTR_ERR(port_dev);
3332
3333 port->port_dev = port_dev;
3334
3335 return 0;
3336}
3337
3338/*
3339 * Initialize a serial core port device, and a controller device if needed.
3340 */
3341int serial_core_register_port(struct uart_driver *drv, struct uart_port *port)
3342{
3343 struct serial_ctrl_device *ctrl_dev, *new_ctrl_dev = NULL;
3344 int ret;
3345
3346 guard(mutex)(&port_mutex);
3347
3348 /*
3349 * Prevent serial_port_runtime_resume() from trying to use the port
3350 * until serial_core_add_one_port() has completed
3351 */
3352 port->flags |= UPF_DEAD;
3353
3354 /* Inititalize a serial core controller device if needed */
3355 ctrl_dev = serial_core_ctrl_find(drv, port->dev, port->ctrl_id);
3356 if (!ctrl_dev) {
3357 new_ctrl_dev = serial_core_ctrl_device_add(port);
3358 if (IS_ERR(new_ctrl_dev))
3359 return PTR_ERR(new_ctrl_dev);
3360 ctrl_dev = new_ctrl_dev;
3361 }
3362
3363 /*
3364 * Initialize a serial core port device. Tag the port dead to prevent
3365 * serial_port_runtime_resume() trying to do anything until port has
3366 * been registered. It gets cleared by serial_core_add_one_port().
3367 */
3368 ret = serial_core_port_device_add(ctrl_dev, port);
3369 if (ret)
3370 goto err_unregister_ctrl_dev;
3371
3372 ret = serial_base_match_and_update_preferred_console(drv, port);
3373 if (ret)
3374 goto err_unregister_port_dev;
3375
3376 ret = serial_core_add_one_port(drv, port);
3377 if (ret)
3378 goto err_unregister_port_dev;
3379
3380 return 0;
3381
3382err_unregister_port_dev:
3383 serial_base_port_device_remove(port->port_dev);
3384 port->port_dev = NULL;
3385
3386err_unregister_ctrl_dev:
3387 serial_base_ctrl_device_remove(new_ctrl_dev);
3388
3389 return ret;
3390}
3391
3392/*
3393 * Removes a serial core port device, and the related serial core controller
3394 * device if the last instance.
3395 */
3396void serial_core_unregister_port(struct uart_driver *drv, struct uart_port *port)
3397{
3398 struct device *phys_dev = port->dev;
3399 struct serial_port_device *port_dev;
3400 struct serial_ctrl_device *ctrl_dev;
3401 int ctrl_id = port->ctrl_id;
3402
3403 guard(mutex)(&port_mutex);
3404
3405 /*
3406 * A NULL port device means there is no registered port device to
3407 * remove: serial_core_remove_one_port() clears port_dev on
3408 * teardown, and it is never set if registration failed before
3409 * serial_core_port_device_add().
3410 */
3411 port_dev = port->port_dev;
3412 if (!port_dev)
3413 return;
3414
3415 ctrl_dev = serial_core_get_ctrl_dev(port_dev);
3416
3417 port->flags |= UPF_DEAD;
3418
3419 serial_core_remove_one_port(drv, port);
3420
3421 /* Note that struct uart_port *port is no longer valid at this point */
3422 serial_base_port_device_remove(port_dev);
3423
3424 /* Drop the serial core controller device if no ports are using it */
3425 if (!serial_core_ctrl_find(drv, phys_dev, ctrl_id))
3426 serial_base_ctrl_device_remove(ctrl_dev);
3427}
3428
3429/**
3430 * uart_handle_dcd_change - handle a change of carrier detect state
3431 * @uport: uart_port structure for the open port
3432 * @active: new carrier detect status
3433 *
3434 * Caller must hold uport->lock.
3435 */
3436void uart_handle_dcd_change(struct uart_port *uport, bool active)
3437{
3438 struct tty_port *port = &uport->state->port;
3439 struct tty_struct *tty = port->tty;
3440 struct tty_ldisc *ld;
3441
3442 lockdep_assert_held_once(&uport->lock);
3443
3444 if (tty) {
3445 ld = tty_ldisc_ref(tty);
3446 if (ld) {
3447 if (ld->ops->dcd_change)
3448 ld->ops->dcd_change(tty, active);
3449 tty_ldisc_deref(ld);
3450 }
3451 }
3452
3453 uport->icount.dcd++;
3454
3455 if (uart_dcd_enabled(uport)) {
3456 if (active)
3457 wake_up_interruptible(&port->open_wait);
3458 else if (tty)
3459 tty_hangup(tty);
3460 }
3461}
3462EXPORT_SYMBOL_GPL(uart_handle_dcd_change);
3463
3464/**
3465 * uart_handle_cts_change - handle a change of clear-to-send state
3466 * @uport: uart_port structure for the open port
3467 * @active: new clear-to-send status
3468 *
3469 * Caller must hold uport->lock.
3470 */
3471void uart_handle_cts_change(struct uart_port *uport, bool active)
3472{
3473 lockdep_assert_held_once(&uport->lock);
3474
3475 uport->icount.cts++;
3476
3477 if (uart_softcts_mode(uport)) {
3478 if (uport->hw_stopped) {
3479 if (active) {
3480 uport->hw_stopped = false;
3481 uport->ops->start_tx(uport);
3482 uart_write_wakeup(uport);
3483 }
3484 } else {
3485 if (!active) {
3486 uport->hw_stopped = true;
3487 uport->ops->stop_tx(uport);
3488 }
3489 }
3490
3491 }
3492}
3493EXPORT_SYMBOL_GPL(uart_handle_cts_change);
3494
3495/**
3496 * uart_insert_char - push a char to the uart layer
3497 *
3498 * User is responsible to call tty_flip_buffer_push when they are done with
3499 * insertion.
3500 *
3501 * @port: corresponding port
3502 * @status: state of the serial port RX buffer (LSR for 8250)
3503 * @overrun: mask of overrun bits in @status
3504 * @ch: character to push
3505 * @flag: flag for the character (see TTY_NORMAL and friends)
3506 */
3507void uart_insert_char(struct uart_port *port, unsigned int status,
3508 unsigned int overrun, u8 ch, u8 flag)
3509{
3510 struct tty_port *tport = &port->state->port;
3511
3512 if ((status & port->ignore_status_mask & ~overrun) == 0)
3513 if (tty_insert_flip_char(tport, ch, flag) == 0)
3514 ++port->icount.buf_overrun;
3515
3516 /*
3517 * Overrun is special. Since it's reported immediately,
3518 * it doesn't affect the current character.
3519 */
3520 if (status & ~port->ignore_status_mask & overrun)
3521 if (tty_insert_flip_char(tport, 0, TTY_OVERRUN) == 0)
3522 ++port->icount.buf_overrun;
3523}
3524EXPORT_SYMBOL_GPL(uart_insert_char);
3525
3526#ifdef CONFIG_MAGIC_SYSRQ_SERIAL
3527static const u8 sysrq_toggle_seq[] = CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE;
3528
3529static void uart_sysrq_on(struct work_struct *w)
3530{
3531 int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq);
3532
3533 sysrq_toggle_support(1);
3534 pr_info("SysRq is enabled by magic sequence '%*pE' on serial\n",
3535 sysrq_toggle_seq_len, sysrq_toggle_seq);
3536}
3537static DECLARE_WORK(sysrq_enable_work, uart_sysrq_on);
3538
3539/**
3540 * uart_try_toggle_sysrq - Enables SysRq from serial line
3541 * @port: uart_port structure where char(s) after BREAK met
3542 * @ch: new character in the sequence after received BREAK
3543 *
3544 * Enables magic SysRq when the required sequence is met on port
3545 * (see CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE).
3546 *
3547 * Returns: %false if @ch is out of enabling sequence and should be
3548 * handled some other way, %true if @ch was consumed.
3549 */
3550bool uart_try_toggle_sysrq(struct uart_port *port, u8 ch)
3551{
3552 int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq);
3553
3554 if (!sysrq_toggle_seq_len)
3555 return false;
3556
3557 BUILD_BUG_ON(ARRAY_SIZE(sysrq_toggle_seq) >= U8_MAX);
3558 if (sysrq_toggle_seq[port->sysrq_seq] != ch) {
3559 port->sysrq_seq = 0;
3560 return false;
3561 }
3562
3563 if (++port->sysrq_seq < sysrq_toggle_seq_len) {
3564 port->sysrq = jiffies + SYSRQ_TIMEOUT;
3565 return true;
3566 }
3567
3568 schedule_work(&sysrq_enable_work);
3569
3570 port->sysrq = 0;
3571 return true;
3572}
3573EXPORT_SYMBOL_GPL(uart_try_toggle_sysrq);
3574#endif
3575
3576/**
3577 * uart_get_rs485_mode() - retrieve rs485 properties for given uart
3578 * @port: uart device's target port
3579 *
3580 * This function implements the device tree binding described in
3581 * Documentation/devicetree/bindings/serial/rs485.yaml.
3582 */
3583int uart_get_rs485_mode(struct uart_port *port)
3584{
3585 struct serial_rs485 *rs485conf = &port->rs485;
3586 struct device *dev = port->dev;
3587 enum gpiod_flags dflags;
3588 struct gpio_desc *desc;
3589 u32 rs485_delay[2];
3590 int ret;
3591
3592 if (!(port->rs485_supported.flags & SER_RS485_ENABLED))
3593 return 0;
3594
3595 /*
3596 * Retrieve properties only if a firmware node exists. If no firmware
3597 * node exists, then don't touch rs485 config and keep initial rs485
3598 * properties set by driver.
3599 */
3600 if (!dev_fwnode(dev))
3601 return 0;
3602
3603 ret = device_property_read_u32_array(dev, "rs485-rts-delay",
3604 rs485_delay, 2);
3605 if (!ret) {
3606 rs485conf->delay_rts_before_send = rs485_delay[0];
3607 rs485conf->delay_rts_after_send = rs485_delay[1];
3608 } else {
3609 rs485conf->delay_rts_before_send = 0;
3610 rs485conf->delay_rts_after_send = 0;
3611 }
3612
3613 uart_sanitize_serial_rs485_delays(port, rs485conf);
3614
3615 /*
3616 * Clear full-duplex and enabled flags, set RTS polarity to active high
3617 * to get to a defined state with the following properties:
3618 */
3619 rs485conf->flags &= ~(SER_RS485_RX_DURING_TX | SER_RS485_ENABLED |
3620 SER_RS485_TERMINATE_BUS |
3621 SER_RS485_RTS_AFTER_SEND);
3622 rs485conf->flags |= SER_RS485_RTS_ON_SEND;
3623
3624 if (device_property_read_bool(dev, "rs485-rx-during-tx"))
3625 rs485conf->flags |= SER_RS485_RX_DURING_TX;
3626
3627 if (device_property_read_bool(dev, "linux,rs485-enabled-at-boot-time"))
3628 rs485conf->flags |= SER_RS485_ENABLED;
3629
3630 if (device_property_read_bool(dev, "rs485-rts-active-low")) {
3631 rs485conf->flags &= ~SER_RS485_RTS_ON_SEND;
3632 rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
3633 }
3634
3635 /*
3636 * Disabling termination by default is the safe choice: Else if many
3637 * bus participants enable it, no communication is possible at all.
3638 * Works fine for short cables and users may enable for longer cables.
3639 */
3640 desc = devm_gpiod_get_optional(dev, "rs485-term", GPIOD_OUT_LOW);
3641 if (IS_ERR(desc))
3642 return dev_err_probe(dev, PTR_ERR(desc), "Cannot get rs485-term-gpios\n");
3643 port->rs485_term_gpio = desc;
3644 if (port->rs485_term_gpio)
3645 port->rs485_supported.flags |= SER_RS485_TERMINATE_BUS;
3646
3647 dflags = (rs485conf->flags & SER_RS485_RX_DURING_TX) ?
3648 GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
3649 desc = devm_gpiod_get_optional(dev, "rs485-rx-during-tx", dflags);
3650 if (IS_ERR(desc))
3651 return dev_err_probe(dev, PTR_ERR(desc), "Cannot get rs485-rx-during-tx-gpios\n");
3652 port->rs485_rx_during_tx_gpio = desc;
3653 if (port->rs485_rx_during_tx_gpio)
3654 port->rs485_supported.flags |= SER_RS485_RX_DURING_TX;
3655
3656 return 0;
3657}
3658EXPORT_SYMBOL_GPL(uart_get_rs485_mode);
3659
3660/* Compile-time assertions for serial_rs485 layout */
3661static_assert(offsetof(struct serial_rs485, padding) ==
3662 (offsetof(struct serial_rs485, delay_rts_after_send) + sizeof(__u32)));
3663static_assert(offsetof(struct serial_rs485, padding1) ==
3664 offsetof(struct serial_rs485, padding[1]));
3665static_assert((offsetof(struct serial_rs485, padding[4]) + sizeof(__u32)) ==
3666 sizeof(struct serial_rs485));
3667
3668MODULE_DESCRIPTION("Serial driver core");
3669MODULE_LICENSE("GPL");