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