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