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