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