forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-02-19 1c055e55a242a33e574e48be530e06770a210dcd
kernel/drivers/tty/serial/serial_core.c
....@@ -14,14 +14,17 @@
1414 #include <linux/sched/signal.h>
1515 #include <linux/init.h>
1616 #include <linux/console.h>
17
+#include <linux/gpio/consumer.h>
1718 #include <linux/of.h>
1819 #include <linux/proc_fs.h>
1920 #include <linux/seq_file.h>
2021 #include <linux/device.h>
2122 #include <linux/serial.h> /* for serial_state and serial_icounter_struct */
2223 #include <linux/serial_core.h>
24
+#include <linux/sysrq.h>
2325 #include <linux/delay.h>
2426 #include <linux/mutex.h>
27
+#include <linux/security.h>
2528
2629 #include <linux/irq.h>
2730 #include <linux/uaccess.h>
....@@ -38,6 +41,11 @@
3841 static struct lock_class_key port_lock_key;
3942
4043 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
44
+
45
+/*
46
+ * Max time with active RTS before/after data is sent.
47
+ */
48
+#define RS485_MAX_RTS_DELAY 100 /* msecs */
4149
4250 static void uart_change_speed(struct tty_struct *tty, struct uart_state *state,
4351 struct ktermios *old_termios);
....@@ -144,7 +152,7 @@
144152 spin_lock_irqsave(&port->lock, flags);
145153 old = port->mctrl;
146154 port->mctrl = (old & ~clear) | set;
147
- if (old != port->mctrl)
155
+ if (old != port->mctrl && !(port->rs485.flags & SER_RS485_ENABLED))
148156 port->ops->set_mctrl(port, port->mctrl);
149157 spin_unlock_irqrestore(&port->lock, flags);
150158 }
....@@ -154,23 +162,10 @@
154162
155163 static void uart_port_dtr_rts(struct uart_port *uport, int raise)
156164 {
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
- }
165
+ if (raise)
166
+ uart_set_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
167
+ else
168
+ uart_clear_mctrl(uport, TIOCM_DTR | TIOCM_RTS);
174169 }
175170
176171 /*
....@@ -667,6 +662,20 @@
667662 }
668663
669664 /*
665
+ * This function performs low-level write of high-priority XON/XOFF
666
+ * character and accounting for it.
667
+ *
668
+ * Requires uart_port to implement .serial_out().
669
+ */
670
+void uart_xchar_out(struct uart_port *uport, int offset)
671
+{
672
+ serial_port_out(uport, offset, uport->x_char);
673
+ uport->icount.tx++;
674
+ uport->x_char = 0;
675
+}
676
+EXPORT_SYMBOL_GPL(uart_xchar_out);
677
+
678
+/*
670679 * This function is used to send a high-priority XON/XOFF character to
671680 * the device
672681 */
....@@ -792,17 +801,13 @@
792801 return ret;
793802 }
794803
795
-static int uart_get_info_user(struct tty_port *port,
796
- struct serial_struct __user *retinfo)
804
+static int uart_get_info_user(struct tty_struct *tty,
805
+ struct serial_struct *ss)
797806 {
798
- struct serial_struct tmp;
807
+ struct uart_state *state = tty->driver_data;
808
+ struct tty_port *port = &state->port;
799809
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;
810
+ return uart_get_info(port, ss) < 0 ? -EIO : 0;
806811 }
807812
808813 static int uart_set_info(struct tty_struct *tty, struct tty_port *port,
....@@ -864,6 +869,12 @@
864869 (new_flags & UPF_USR_MASK));
865870 uport->custom_divisor = new_info->custom_divisor;
866871 goto check_and_exit;
872
+ }
873
+
874
+ if (change_irq || change_port) {
875
+ retval = security_locked_down(LOCKDOWN_TIOCSSERIAL);
876
+ if (retval)
877
+ goto exit;
867878 }
868879
869880 /*
....@@ -1004,16 +1015,13 @@
10041015 return retval;
10051016 }
10061017
1007
-static int uart_set_info_user(struct tty_struct *tty, struct uart_state *state,
1008
- struct serial_struct __user *newinfo)
1018
+static int uart_set_info_user(struct tty_struct *tty, struct serial_struct *ss)
10091019 {
1010
- struct serial_struct new_serial;
1020
+ struct uart_state *state = tty->driver_data;
10111021 struct tty_port *port = &state->port;
10121022 int retval;
10131023
1014
- if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
1015
- return -EFAULT;
1016
-
1024
+ down_write(&tty->termios_rwsem);
10171025 /*
10181026 * This semaphore protects port->count. It is also
10191027 * very useful to prevent opens. Also, take the
....@@ -1022,8 +1030,9 @@
10221030 * under us.
10231031 */
10241032 mutex_lock(&port->mutex);
1025
- retval = uart_set_info(tty, port, state, &new_serial);
1033
+ retval = uart_set_info(tty, port, state, ss);
10261034 mutex_unlock(&port->mutex);
1035
+ up_write(&tty->termios_rwsem);
10271036 return retval;
10281037 }
10291038
....@@ -1120,7 +1129,7 @@
11201129 return ret;
11211130 }
11221131
1123
-static int uart_do_autoconfig(struct tty_struct *tty,struct uart_state *state)
1132
+static int uart_do_autoconfig(struct tty_struct *tty, struct uart_state *state)
11241133 {
11251134 struct tty_port *port = &state->port;
11261135 struct uart_port *uport;
....@@ -1305,18 +1314,103 @@
13051314 unsigned long flags;
13061315
13071316 if (!port->rs485_config)
1308
- return -ENOIOCTLCMD;
1317
+ return -ENOTTY;
13091318
13101319 if (copy_from_user(&rs485, rs485_user, sizeof(*rs485_user)))
13111320 return -EFAULT;
13121321
1322
+ /* pick sane settings if the user hasn't */
1323
+ if (!(rs485.flags & SER_RS485_RTS_ON_SEND) ==
1324
+ !(rs485.flags & SER_RS485_RTS_AFTER_SEND)) {
1325
+ dev_warn_ratelimited(port->dev,
1326
+ "%s (%d): invalid RTS setting, using RTS_ON_SEND instead\n",
1327
+ port->name, port->line);
1328
+ rs485.flags |= SER_RS485_RTS_ON_SEND;
1329
+ rs485.flags &= ~SER_RS485_RTS_AFTER_SEND;
1330
+ }
1331
+
1332
+ if (rs485.delay_rts_before_send > RS485_MAX_RTS_DELAY) {
1333
+ rs485.delay_rts_before_send = RS485_MAX_RTS_DELAY;
1334
+ dev_warn_ratelimited(port->dev,
1335
+ "%s (%d): RTS delay before sending clamped to %u ms\n",
1336
+ port->name, port->line, rs485.delay_rts_before_send);
1337
+ }
1338
+
1339
+ if (rs485.delay_rts_after_send > RS485_MAX_RTS_DELAY) {
1340
+ rs485.delay_rts_after_send = RS485_MAX_RTS_DELAY;
1341
+ dev_warn_ratelimited(port->dev,
1342
+ "%s (%d): RTS delay after sending clamped to %u ms\n",
1343
+ port->name, port->line, rs485.delay_rts_after_send);
1344
+ }
1345
+ /* Return clean padding area to userspace */
1346
+ memset(rs485.padding, 0, sizeof(rs485.padding));
1347
+
13131348 spin_lock_irqsave(&port->lock, flags);
13141349 ret = port->rs485_config(port, &rs485);
1350
+ if (!ret) {
1351
+ port->rs485 = rs485;
1352
+
1353
+ /* Reset RTS and other mctrl lines when disabling RS485 */
1354
+ if (!(rs485.flags & SER_RS485_ENABLED))
1355
+ port->ops->set_mctrl(port, port->mctrl);
1356
+ }
13151357 spin_unlock_irqrestore(&port->lock, flags);
13161358 if (ret)
13171359 return ret;
13181360
13191361 if (copy_to_user(rs485_user, &port->rs485, sizeof(port->rs485)))
1362
+ return -EFAULT;
1363
+
1364
+ return 0;
1365
+}
1366
+
1367
+static int uart_get_iso7816_config(struct uart_port *port,
1368
+ struct serial_iso7816 __user *iso7816)
1369
+{
1370
+ unsigned long flags;
1371
+ struct serial_iso7816 aux;
1372
+
1373
+ if (!port->iso7816_config)
1374
+ return -ENOTTY;
1375
+
1376
+ spin_lock_irqsave(&port->lock, flags);
1377
+ aux = port->iso7816;
1378
+ spin_unlock_irqrestore(&port->lock, flags);
1379
+
1380
+ if (copy_to_user(iso7816, &aux, sizeof(aux)))
1381
+ return -EFAULT;
1382
+
1383
+ return 0;
1384
+}
1385
+
1386
+static int uart_set_iso7816_config(struct uart_port *port,
1387
+ struct serial_iso7816 __user *iso7816_user)
1388
+{
1389
+ struct serial_iso7816 iso7816;
1390
+ int i, ret;
1391
+ unsigned long flags;
1392
+
1393
+ if (!port->iso7816_config)
1394
+ return -ENOTTY;
1395
+
1396
+ if (copy_from_user(&iso7816, iso7816_user, sizeof(*iso7816_user)))
1397
+ return -EFAULT;
1398
+
1399
+ /*
1400
+ * There are 5 words reserved for future use. Check that userspace
1401
+ * doesn't put stuff in there to prevent breakages in the future.
1402
+ */
1403
+ for (i = 0; i < 5; i++)
1404
+ if (iso7816.reserved[i])
1405
+ return -EINVAL;
1406
+
1407
+ spin_lock_irqsave(&port->lock, flags);
1408
+ ret = port->iso7816_config(port, &iso7816);
1409
+ spin_unlock_irqrestore(&port->lock, flags);
1410
+ if (ret)
1411
+ return ret;
1412
+
1413
+ if (copy_to_user(iso7816_user, &port->iso7816, sizeof(port->iso7816)))
13201414 return -EFAULT;
13211415
13221416 return 0;
....@@ -1339,25 +1433,10 @@
13391433 * These ioctls don't rely on the hardware to be present.
13401434 */
13411435 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
-
13521436 case TIOCSERCONFIG:
13531437 down_write(&tty->termios_rwsem);
13541438 ret = uart_do_autoconfig(tty, state);
13551439 up_write(&tty->termios_rwsem);
1356
- break;
1357
-
1358
- case TIOCSERGWILD: /* obsolete */
1359
- case TIOCSERSWILD: /* obsolete */
1360
- ret = 0;
13611440 break;
13621441 }
13631442
....@@ -1405,6 +1484,14 @@
14051484
14061485 case TIOCSRS485:
14071486 ret = uart_set_rs485_config(uport, uarg);
1487
+ break;
1488
+
1489
+ case TIOCSISO7816:
1490
+ ret = uart_set_iso7816_config(state->uart_port, uarg);
1491
+ break;
1492
+
1493
+ case TIOCGISO7816:
1494
+ ret = uart_get_iso7816_config(state->uart_port, uarg);
14081495 break;
14091496 default:
14101497 if (uport->ops->ioctl)
....@@ -1473,7 +1560,7 @@
14731560 }
14741561
14751562 uart_change_speed(tty, state, old_termios);
1476
- /* reload cflag from termios; port driver may have overriden flags */
1563
+ /* reload cflag from termios; port driver may have overridden flags */
14771564 cflag = tty->termios.c_cflag;
14781565
14791566 /* Handle transition to B0 status */
....@@ -1482,6 +1569,7 @@
14821569 /* Handle transition away from B0 status */
14831570 else if (!(old_termios->c_cflag & CBAUD) && (cflag & CBAUD)) {
14841571 unsigned int mask = TIOCM_DTR;
1572
+
14851573 if (!(cflag & CRTSCTS) || !tty_throttled(tty))
14861574 mask |= TIOCM_RTS;
14871575 uart_set_mctrl(uport, mask);
....@@ -1879,6 +1967,12 @@
18791967 }
18801968 #endif
18811969
1970
+static void uart_port_spin_lock_init(struct uart_port *port)
1971
+{
1972
+ spin_lock_init(&port->lock);
1973
+ lockdep_set_class(&port->lock, &port_lock_key);
1974
+}
1975
+
18821976 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
18831977 /**
18841978 * uart_console_write - write a console message to a serial port
....@@ -1935,8 +2029,10 @@
19352029 * console=<name>,io|mmio|mmio16|mmio32|mmio32be|mmio32native,<addr>,<options>
19362030 *
19372031 * The optional form
2032
+ *
19382033 * earlycon=<name>,0x<addr>,<options>
19392034 * console=<name>,0x<addr>,<options>
2035
+ *
19402036 * is also accepted; the returned @iotype will be UPIO_MEM.
19412037 *
19422038 * Returns 0 on success or -EINVAL on failure
....@@ -2030,15 +2126,14 @@
20302126 static struct ktermios dummy;
20312127
20322128 /*
2033
- * Ensure that the serial console lock is initialised
2034
- * early.
2035
- * If this port is a console, then the spinlock is already
2036
- * initialised.
2129
+ * Ensure that the serial-console lock is initialised early.
2130
+ *
2131
+ * Note that the console-enabled check is needed because of kgdboc,
2132
+ * which can end up calling uart_set_options() for an already enabled
2133
+ * console via tty_find_polling_driver() and uart_poll_init().
20372134 */
2038
- if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {
2039
- spin_lock_init(&port->lock);
2040
- lockdep_set_class(&port->lock, &port_lock_key);
2041
- }
2135
+ if (!uart_console_enabled(port) && !port->console_reinit)
2136
+ uart_port_spin_lock_init(port);
20422137
20432138 memset(&termios, 0, sizeof(struct ktermios));
20442139
....@@ -2053,7 +2148,7 @@
20532148 switch (parity) {
20542149 case 'o': case 'O':
20552150 termios.c_cflag |= PARODD;
2056
- /*fall through*/
2151
+ fallthrough;
20572152 case 'e': case 'E':
20582153 termios.c_cflag |= PARENB;
20592154 break;
....@@ -2149,7 +2244,8 @@
21492244
21502245 spin_lock_irq(&uport->lock);
21512246 ops->stop_tx(uport);
2152
- ops->set_mctrl(uport, 0);
2247
+ if (!(uport->rs485.flags & SER_RS485_ENABLED))
2248
+ ops->set_mctrl(uport, 0);
21532249 ops->stop_rx(uport);
21542250 spin_unlock_irq(&uport->lock);
21552251
....@@ -2228,17 +2324,22 @@
22282324
22292325 uart_change_pm(state, UART_PM_STATE_ON);
22302326 spin_lock_irq(&uport->lock);
2231
- ops->set_mctrl(uport, 0);
2327
+ if (!(uport->rs485.flags & SER_RS485_ENABLED))
2328
+ ops->set_mctrl(uport, 0);
22322329 spin_unlock_irq(&uport->lock);
22332330 if (console_suspend_enabled || !uart_console(uport)) {
22342331 /* Protected by port mutex for now */
22352332 struct tty_struct *tty = port->tty;
2333
+
22362334 ret = ops->startup(uport);
22372335 if (ret == 0) {
22382336 if (tty)
22392337 uart_change_speed(tty, state, NULL);
22402338 spin_lock_irq(&uport->lock);
2241
- ops->set_mctrl(uport, uport->mctrl);
2339
+ if (!(uport->rs485.flags & SER_RS485_ENABLED))
2340
+ ops->set_mctrl(uport, uport->mctrl);
2341
+ else
2342
+ uport->rs485_config(uport, &uport->rs485);
22422343 ops->start_tx(uport);
22432344 spin_unlock_irq(&uport->lock);
22442345 tty_port_set_initialized(port, 1);
....@@ -2336,7 +2437,10 @@
23362437 */
23372438 spin_lock_irqsave(&port->lock, flags);
23382439 port->mctrl &= TIOCM_DTR;
2339
- port->ops->set_mctrl(port, port->mctrl);
2440
+ if (!(port->rs485.flags & SER_RS485_ENABLED))
2441
+ port->ops->set_mctrl(port, port->mctrl);
2442
+ else
2443
+ port->rs485_config(port, &port->rs485);
23402444 spin_unlock_irqrestore(&port->lock, flags);
23412445
23422446 /*
....@@ -2456,6 +2560,8 @@
24562560 #endif
24572561 .tiocmget = uart_tiocmget,
24582562 .tiocmset = uart_tiocmset,
2563
+ .set_serial = uart_set_info_user,
2564
+ .get_serial = uart_get_info_user,
24592565 .get_icount = uart_get_icount,
24602566 #ifdef CONFIG_CONSOLE_POLL
24612567 .poll_init = uart_poll_init,
....@@ -2487,7 +2593,7 @@
24872593 int uart_register_driver(struct uart_driver *drv)
24882594 {
24892595 struct tty_driver *normal;
2490
- int i, retval;
2596
+ int i, retval = -ENOMEM;
24912597
24922598 BUG_ON(drv->state);
24932599
....@@ -2539,7 +2645,7 @@
25392645 out_kfree:
25402646 kfree(drv->state);
25412647 out:
2542
- return -ENOMEM;
2648
+ return retval;
25432649 }
25442650
25452651 /**
....@@ -2573,36 +2679,37 @@
25732679 }
25742680 EXPORT_SYMBOL_GPL(uart_console_device);
25752681
2576
-static ssize_t uart_get_attr_uartclk(struct device *dev,
2682
+static ssize_t uartclk_show(struct device *dev,
25772683 struct device_attribute *attr, char *buf)
25782684 {
25792685 struct serial_struct tmp;
25802686 struct tty_port *port = dev_get_drvdata(dev);
25812687
25822688 uart_get_info(port, &tmp);
2583
- return snprintf(buf, PAGE_SIZE, "%d\n", tmp.baud_base * 16);
2689
+ return sprintf(buf, "%d\n", tmp.baud_base * 16);
25842690 }
25852691
2586
-static ssize_t uart_get_attr_type(struct device *dev,
2692
+static ssize_t type_show(struct device *dev,
25872693 struct device_attribute *attr, char *buf)
25882694 {
25892695 struct serial_struct tmp;
25902696 struct tty_port *port = dev_get_drvdata(dev);
25912697
25922698 uart_get_info(port, &tmp);
2593
- return snprintf(buf, PAGE_SIZE, "%d\n", tmp.type);
2699
+ return sprintf(buf, "%d\n", tmp.type);
25942700 }
2595
-static ssize_t uart_get_attr_line(struct device *dev,
2701
+
2702
+static ssize_t line_show(struct device *dev,
25962703 struct device_attribute *attr, char *buf)
25972704 {
25982705 struct serial_struct tmp;
25992706 struct tty_port *port = dev_get_drvdata(dev);
26002707
26012708 uart_get_info(port, &tmp);
2602
- return snprintf(buf, PAGE_SIZE, "%d\n", tmp.line);
2709
+ return sprintf(buf, "%d\n", tmp.line);
26032710 }
26042711
2605
-static ssize_t uart_get_attr_port(struct device *dev,
2712
+static ssize_t port_show(struct device *dev,
26062713 struct device_attribute *attr, char *buf)
26072714 {
26082715 struct serial_struct tmp;
....@@ -2613,135 +2720,187 @@
26132720 ioaddr = tmp.port;
26142721 if (HIGH_BITS_OFFSET)
26152722 ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
2616
- return snprintf(buf, PAGE_SIZE, "0x%lX\n", ioaddr);
2723
+ return sprintf(buf, "0x%lX\n", ioaddr);
26172724 }
26182725
2619
-static ssize_t uart_get_attr_irq(struct device *dev,
2726
+static ssize_t irq_show(struct device *dev,
26202727 struct device_attribute *attr, char *buf)
26212728 {
26222729 struct serial_struct tmp;
26232730 struct tty_port *port = dev_get_drvdata(dev);
26242731
26252732 uart_get_info(port, &tmp);
2626
- return snprintf(buf, PAGE_SIZE, "%d\n", tmp.irq);
2733
+ return sprintf(buf, "%d\n", tmp.irq);
26272734 }
26282735
2629
-static ssize_t uart_get_attr_flags(struct device *dev,
2736
+static ssize_t flags_show(struct device *dev,
26302737 struct device_attribute *attr, char *buf)
26312738 {
26322739 struct serial_struct tmp;
26332740 struct tty_port *port = dev_get_drvdata(dev);
26342741
26352742 uart_get_info(port, &tmp);
2636
- return snprintf(buf, PAGE_SIZE, "0x%X\n", tmp.flags);
2743
+ return sprintf(buf, "0x%X\n", tmp.flags);
26372744 }
26382745
2639
-static ssize_t uart_get_attr_xmit_fifo_size(struct device *dev,
2746
+static ssize_t xmit_fifo_size_show(struct device *dev,
26402747 struct device_attribute *attr, char *buf)
26412748 {
26422749 struct serial_struct tmp;
26432750 struct tty_port *port = dev_get_drvdata(dev);
26442751
26452752 uart_get_info(port, &tmp);
2646
- return snprintf(buf, PAGE_SIZE, "%d\n", tmp.xmit_fifo_size);
2753
+ return sprintf(buf, "%d\n", tmp.xmit_fifo_size);
26472754 }
26482755
2649
-
2650
-static ssize_t uart_get_attr_close_delay(struct device *dev,
2756
+static ssize_t close_delay_show(struct device *dev,
26512757 struct device_attribute *attr, char *buf)
26522758 {
26532759 struct serial_struct tmp;
26542760 struct tty_port *port = dev_get_drvdata(dev);
26552761
26562762 uart_get_info(port, &tmp);
2657
- return snprintf(buf, PAGE_SIZE, "%d\n", tmp.close_delay);
2763
+ return sprintf(buf, "%d\n", tmp.close_delay);
26582764 }
26592765
2660
-
2661
-static ssize_t uart_get_attr_closing_wait(struct device *dev,
2766
+static ssize_t closing_wait_show(struct device *dev,
26622767 struct device_attribute *attr, char *buf)
26632768 {
26642769 struct serial_struct tmp;
26652770 struct tty_port *port = dev_get_drvdata(dev);
26662771
26672772 uart_get_info(port, &tmp);
2668
- return snprintf(buf, PAGE_SIZE, "%d\n", tmp.closing_wait);
2773
+ return sprintf(buf, "%d\n", tmp.closing_wait);
26692774 }
26702775
2671
-static ssize_t uart_get_attr_custom_divisor(struct device *dev,
2776
+static ssize_t custom_divisor_show(struct device *dev,
26722777 struct device_attribute *attr, char *buf)
26732778 {
26742779 struct serial_struct tmp;
26752780 struct tty_port *port = dev_get_drvdata(dev);
26762781
26772782 uart_get_info(port, &tmp);
2678
- return snprintf(buf, PAGE_SIZE, "%d\n", tmp.custom_divisor);
2783
+ return sprintf(buf, "%d\n", tmp.custom_divisor);
26792784 }
26802785
2681
-static ssize_t uart_get_attr_io_type(struct device *dev,
2786
+static ssize_t io_type_show(struct device *dev,
26822787 struct device_attribute *attr, char *buf)
26832788 {
26842789 struct serial_struct tmp;
26852790 struct tty_port *port = dev_get_drvdata(dev);
26862791
26872792 uart_get_info(port, &tmp);
2688
- return snprintf(buf, PAGE_SIZE, "%d\n", tmp.io_type);
2793
+ return sprintf(buf, "%d\n", tmp.io_type);
26892794 }
26902795
2691
-static ssize_t uart_get_attr_iomem_base(struct device *dev,
2796
+static ssize_t iomem_base_show(struct device *dev,
26922797 struct device_attribute *attr, char *buf)
26932798 {
26942799 struct serial_struct tmp;
26952800 struct tty_port *port = dev_get_drvdata(dev);
26962801
26972802 uart_get_info(port, &tmp);
2698
- return snprintf(buf, PAGE_SIZE, "0x%lX\n", (unsigned long)tmp.iomem_base);
2803
+ return sprintf(buf, "0x%lX\n", (unsigned long)tmp.iomem_base);
26992804 }
27002805
2701
-static ssize_t uart_get_attr_iomem_reg_shift(struct device *dev,
2806
+static ssize_t iomem_reg_shift_show(struct device *dev,
27022807 struct device_attribute *attr, char *buf)
27032808 {
27042809 struct serial_struct tmp;
27052810 struct tty_port *port = dev_get_drvdata(dev);
27062811
27072812 uart_get_info(port, &tmp);
2708
- return snprintf(buf, PAGE_SIZE, "%d\n", tmp.iomem_reg_shift);
2813
+ return sprintf(buf, "%d\n", tmp.iomem_reg_shift);
27092814 }
27102815
2711
-static DEVICE_ATTR(type, S_IRUSR | S_IRGRP, uart_get_attr_type, NULL);
2712
-static DEVICE_ATTR(line, S_IRUSR | S_IRGRP, uart_get_attr_line, NULL);
2713
-static DEVICE_ATTR(port, S_IRUSR | S_IRGRP, uart_get_attr_port, NULL);
2714
-static DEVICE_ATTR(irq, S_IRUSR | S_IRGRP, uart_get_attr_irq, NULL);
2715
-static DEVICE_ATTR(flags, S_IRUSR | S_IRGRP, uart_get_attr_flags, NULL);
2716
-static DEVICE_ATTR(xmit_fifo_size, S_IRUSR | S_IRGRP, uart_get_attr_xmit_fifo_size, NULL);
2717
-static DEVICE_ATTR(uartclk, S_IRUSR | S_IRGRP, uart_get_attr_uartclk, NULL);
2718
-static DEVICE_ATTR(close_delay, S_IRUSR | S_IRGRP, uart_get_attr_close_delay, NULL);
2719
-static DEVICE_ATTR(closing_wait, S_IRUSR | S_IRGRP, uart_get_attr_closing_wait, NULL);
2720
-static DEVICE_ATTR(custom_divisor, S_IRUSR | S_IRGRP, uart_get_attr_custom_divisor, NULL);
2721
-static DEVICE_ATTR(io_type, S_IRUSR | S_IRGRP, uart_get_attr_io_type, NULL);
2722
-static DEVICE_ATTR(iomem_base, S_IRUSR | S_IRGRP, uart_get_attr_iomem_base, NULL);
2723
-static DEVICE_ATTR(iomem_reg_shift, S_IRUSR | S_IRGRP, uart_get_attr_iomem_reg_shift, NULL);
2816
+static ssize_t console_show(struct device *dev,
2817
+ struct device_attribute *attr, char *buf)
2818
+{
2819
+ struct tty_port *port = dev_get_drvdata(dev);
2820
+ struct uart_state *state = container_of(port, struct uart_state, port);
2821
+ struct uart_port *uport;
2822
+ bool console = false;
2823
+
2824
+ mutex_lock(&port->mutex);
2825
+ uport = uart_port_check(state);
2826
+ if (uport)
2827
+ console = uart_console_enabled(uport);
2828
+ mutex_unlock(&port->mutex);
2829
+
2830
+ return sprintf(buf, "%c\n", console ? 'Y' : 'N');
2831
+}
2832
+
2833
+static ssize_t console_store(struct device *dev,
2834
+ struct device_attribute *attr, const char *buf, size_t count)
2835
+{
2836
+ struct tty_port *port = dev_get_drvdata(dev);
2837
+ struct uart_state *state = container_of(port, struct uart_state, port);
2838
+ struct uart_port *uport;
2839
+ bool oldconsole, newconsole;
2840
+ int ret;
2841
+
2842
+ ret = kstrtobool(buf, &newconsole);
2843
+ if (ret)
2844
+ return ret;
2845
+
2846
+ mutex_lock(&port->mutex);
2847
+ uport = uart_port_check(state);
2848
+ if (uport) {
2849
+ oldconsole = uart_console_enabled(uport);
2850
+ if (oldconsole && !newconsole) {
2851
+ ret = unregister_console(uport->cons);
2852
+ } else if (!oldconsole && newconsole) {
2853
+ if (uart_console(uport)) {
2854
+ uport->console_reinit = 1;
2855
+ register_console(uport->cons);
2856
+ } else {
2857
+ ret = -ENOENT;
2858
+ }
2859
+ }
2860
+ } else {
2861
+ ret = -ENXIO;
2862
+ }
2863
+ mutex_unlock(&port->mutex);
2864
+
2865
+ return ret < 0 ? ret : count;
2866
+}
2867
+
2868
+static DEVICE_ATTR_RO(uartclk);
2869
+static DEVICE_ATTR_RO(type);
2870
+static DEVICE_ATTR_RO(line);
2871
+static DEVICE_ATTR_RO(port);
2872
+static DEVICE_ATTR_RO(irq);
2873
+static DEVICE_ATTR_RO(flags);
2874
+static DEVICE_ATTR_RO(xmit_fifo_size);
2875
+static DEVICE_ATTR_RO(close_delay);
2876
+static DEVICE_ATTR_RO(closing_wait);
2877
+static DEVICE_ATTR_RO(custom_divisor);
2878
+static DEVICE_ATTR_RO(io_type);
2879
+static DEVICE_ATTR_RO(iomem_base);
2880
+static DEVICE_ATTR_RO(iomem_reg_shift);
2881
+static DEVICE_ATTR_RW(console);
27242882
27252883 static struct attribute *tty_dev_attrs[] = {
2884
+ &dev_attr_uartclk.attr,
27262885 &dev_attr_type.attr,
27272886 &dev_attr_line.attr,
27282887 &dev_attr_port.attr,
27292888 &dev_attr_irq.attr,
27302889 &dev_attr_flags.attr,
27312890 &dev_attr_xmit_fifo_size.attr,
2732
- &dev_attr_uartclk.attr,
27332891 &dev_attr_close_delay.attr,
27342892 &dev_attr_closing_wait.attr,
27352893 &dev_attr_custom_divisor.attr,
27362894 &dev_attr_io_type.attr,
27372895 &dev_attr_iomem_base.attr,
27382896 &dev_attr_iomem_reg_shift.attr,
2739
- NULL,
2740
- };
2897
+ &dev_attr_console.attr,
2898
+ NULL
2899
+};
27412900
27422901 static const struct attribute_group tty_dev_attr_group = {
27432902 .attrs = tty_dev_attrs,
2744
- };
2903
+};
27452904
27462905 /**
27472906 * uart_add_one_port - attach a driver-defined port structure
....@@ -2793,13 +2952,12 @@
27932952 }
27942953
27952954 /*
2796
- * If this port is a console, then the spinlock is already
2955
+ * If this port is in use as a console then the spinlock is already
27972956 * initialised.
27982957 */
2799
- if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
2800
- spin_lock_init(&uport->lock);
2801
- lockdep_set_class(&uport->lock, &port_lock_key);
2802
- }
2958
+ if (!uart_console_enabled(uport))
2959
+ uart_port_spin_lock_init(uport);
2960
+
28032961 if (uport->cons && uport->dev)
28042962 of_console_check(uport->dev->of_node, uport->cons->name, uport->line);
28052963
....@@ -2828,7 +2986,7 @@
28282986 */
28292987 tty_dev = tty_port_register_device_attr_serdev(port, drv->tty_driver,
28302988 uport->line, uport->dev, port, uport->tty_groups);
2831
- if (likely(!IS_ERR(tty_dev))) {
2989
+ if (!IS_ERR(tty_dev)) {
28322990 device_set_wakeup_capable(tty_dev, 1);
28332991 } else {
28342992 dev_err(uport->dev, "Cannot register tty device on line %d\n",
....@@ -3050,6 +3208,56 @@
30503208 }
30513209 EXPORT_SYMBOL_GPL(uart_insert_char);
30523210
3211
+#ifdef CONFIG_MAGIC_SYSRQ_SERIAL
3212
+static const char sysrq_toggle_seq[] = CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE;
3213
+
3214
+static void uart_sysrq_on(struct work_struct *w)
3215
+{
3216
+ int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq);
3217
+
3218
+ sysrq_toggle_support(1);
3219
+ pr_info("SysRq is enabled by magic sequence '%*pE' on serial\n",
3220
+ sysrq_toggle_seq_len, sysrq_toggle_seq);
3221
+}
3222
+static DECLARE_WORK(sysrq_enable_work, uart_sysrq_on);
3223
+
3224
+/**
3225
+ * uart_try_toggle_sysrq - Enables SysRq from serial line
3226
+ * @port: uart_port structure where char(s) after BREAK met
3227
+ * @ch: new character in the sequence after received BREAK
3228
+ *
3229
+ * Enables magic SysRq when the required sequence is met on port
3230
+ * (see CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE).
3231
+ *
3232
+ * Returns false if @ch is out of enabling sequence and should be
3233
+ * handled some other way, true if @ch was consumed.
3234
+ */
3235
+bool uart_try_toggle_sysrq(struct uart_port *port, unsigned int ch)
3236
+{
3237
+ int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq);
3238
+
3239
+ if (!sysrq_toggle_seq_len)
3240
+ return false;
3241
+
3242
+ BUILD_BUG_ON(ARRAY_SIZE(sysrq_toggle_seq) >= U8_MAX);
3243
+ if (sysrq_toggle_seq[port->sysrq_seq] != ch) {
3244
+ port->sysrq_seq = 0;
3245
+ return false;
3246
+ }
3247
+
3248
+ if (++port->sysrq_seq < sysrq_toggle_seq_len) {
3249
+ port->sysrq = jiffies + SYSRQ_TIMEOUT;
3250
+ return true;
3251
+ }
3252
+
3253
+ schedule_work(&sysrq_enable_work);
3254
+
3255
+ port->sysrq = 0;
3256
+ return true;
3257
+}
3258
+EXPORT_SYMBOL_GPL(uart_try_toggle_sysrq);
3259
+#endif
3260
+
30533261 EXPORT_SYMBOL(uart_write_wakeup);
30543262 EXPORT_SYMBOL(uart_register_driver);
30553263 EXPORT_SYMBOL(uart_unregister_driver);
....@@ -3060,14 +3268,15 @@
30603268
30613269 /**
30623270 * uart_get_rs485_mode() - retrieve rs485 properties for given uart
3063
- * @dev: uart device
3064
- * @rs485conf: output parameter
3271
+ * @port: uart device's target port
30653272 *
30663273 * This function implements the device tree binding described in
30673274 * Documentation/devicetree/bindings/serial/rs485.txt.
30683275 */
3069
-void uart_get_rs485_mode(struct device *dev, struct serial_rs485 *rs485conf)
3276
+int uart_get_rs485_mode(struct uart_port *port)
30703277 {
3278
+ struct serial_rs485 *rs485conf = &port->rs485;
3279
+ struct device *dev = port->dev;
30713280 u32 rs485_delay[2];
30723281 int ret;
30733282
....@@ -3086,6 +3295,7 @@
30863295 * to get to a defined state with the following properties:
30873296 */
30883297 rs485conf->flags &= ~(SER_RS485_RX_DURING_TX | SER_RS485_ENABLED |
3298
+ SER_RS485_TERMINATE_BUS |
30893299 SER_RS485_RTS_AFTER_SEND);
30903300 rs485conf->flags |= SER_RS485_RTS_ON_SEND;
30913301
....@@ -3099,6 +3309,21 @@
30993309 rs485conf->flags &= ~SER_RS485_RTS_ON_SEND;
31003310 rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
31013311 }
3312
+
3313
+ /*
3314
+ * Disabling termination by default is the safe choice: Else if many
3315
+ * bus participants enable it, no communication is possible at all.
3316
+ * Works fine for short cables and users may enable for longer cables.
3317
+ */
3318
+ port->rs485_term_gpio = devm_gpiod_get_optional(dev, "rs485-term",
3319
+ GPIOD_OUT_LOW);
3320
+ if (IS_ERR(port->rs485_term_gpio)) {
3321
+ ret = PTR_ERR(port->rs485_term_gpio);
3322
+ port->rs485_term_gpio = NULL;
3323
+ return dev_err_probe(dev, ret, "Cannot get rs485-term-gpios\n");
3324
+ }
3325
+
3326
+ return 0;
31023327 }
31033328 EXPORT_SYMBOL_GPL(uart_get_rs485_mode);
31043329