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