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