forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
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;
....@@ -2228,17 +2323,22 @@
22282323
22292324 uart_change_pm(state, UART_PM_STATE_ON);
22302325 spin_lock_irq(&uport->lock);
2231
- ops->set_mctrl(uport, 0);
2326
+ if (!(uport->rs485.flags & SER_RS485_ENABLED))
2327
+ ops->set_mctrl(uport, 0);
22322328 spin_unlock_irq(&uport->lock);
22332329 if (console_suspend_enabled || !uart_console(uport)) {
22342330 /* Protected by port mutex for now */
22352331 struct tty_struct *tty = port->tty;
2332
+
22362333 ret = ops->startup(uport);
22372334 if (ret == 0) {
22382335 if (tty)
22392336 uart_change_speed(tty, state, NULL);
22402337 spin_lock_irq(&uport->lock);
2241
- ops->set_mctrl(uport, uport->mctrl);
2338
+ if (!(uport->rs485.flags & SER_RS485_ENABLED))
2339
+ ops->set_mctrl(uport, uport->mctrl);
2340
+ else
2341
+ uport->rs485_config(uport, &uport->rs485);
22422342 ops->start_tx(uport);
22432343 spin_unlock_irq(&uport->lock);
22442344 tty_port_set_initialized(port, 1);
....@@ -2336,7 +2436,10 @@
23362436 */
23372437 spin_lock_irqsave(&port->lock, flags);
23382438 port->mctrl &= TIOCM_DTR;
2339
- port->ops->set_mctrl(port, port->mctrl);
2439
+ if (!(port->rs485.flags & SER_RS485_ENABLED))
2440
+ port->ops->set_mctrl(port, port->mctrl);
2441
+ else
2442
+ port->rs485_config(port, &port->rs485);
23402443 spin_unlock_irqrestore(&port->lock, flags);
23412444
23422445 /*
....@@ -2456,6 +2559,8 @@
24562559 #endif
24572560 .tiocmget = uart_tiocmget,
24582561 .tiocmset = uart_tiocmset,
2562
+ .set_serial = uart_set_info_user,
2563
+ .get_serial = uart_get_info_user,
24592564 .get_icount = uart_get_icount,
24602565 #ifdef CONFIG_CONSOLE_POLL
24612566 .poll_init = uart_poll_init,
....@@ -2487,7 +2592,7 @@
24872592 int uart_register_driver(struct uart_driver *drv)
24882593 {
24892594 struct tty_driver *normal;
2490
- int i, retval;
2595
+ int i, retval = -ENOMEM;
24912596
24922597 BUG_ON(drv->state);
24932598
....@@ -2539,7 +2644,7 @@
25392644 out_kfree:
25402645 kfree(drv->state);
25412646 out:
2542
- return -ENOMEM;
2647
+ return retval;
25432648 }
25442649
25452650 /**
....@@ -2573,36 +2678,37 @@
25732678 }
25742679 EXPORT_SYMBOL_GPL(uart_console_device);
25752680
2576
-static ssize_t uart_get_attr_uartclk(struct device *dev,
2681
+static ssize_t uartclk_show(struct device *dev,
25772682 struct device_attribute *attr, char *buf)
25782683 {
25792684 struct serial_struct tmp;
25802685 struct tty_port *port = dev_get_drvdata(dev);
25812686
25822687 uart_get_info(port, &tmp);
2583
- return snprintf(buf, PAGE_SIZE, "%d\n", tmp.baud_base * 16);
2688
+ return sprintf(buf, "%d\n", tmp.baud_base * 16);
25842689 }
25852690
2586
-static ssize_t uart_get_attr_type(struct device *dev,
2691
+static ssize_t type_show(struct device *dev,
25872692 struct device_attribute *attr, char *buf)
25882693 {
25892694 struct serial_struct tmp;
25902695 struct tty_port *port = dev_get_drvdata(dev);
25912696
25922697 uart_get_info(port, &tmp);
2593
- return snprintf(buf, PAGE_SIZE, "%d\n", tmp.type);
2698
+ return sprintf(buf, "%d\n", tmp.type);
25942699 }
2595
-static ssize_t uart_get_attr_line(struct device *dev,
2700
+
2701
+static ssize_t line_show(struct device *dev,
25962702 struct device_attribute *attr, char *buf)
25972703 {
25982704 struct serial_struct tmp;
25992705 struct tty_port *port = dev_get_drvdata(dev);
26002706
26012707 uart_get_info(port, &tmp);
2602
- return snprintf(buf, PAGE_SIZE, "%d\n", tmp.line);
2708
+ return sprintf(buf, "%d\n", tmp.line);
26032709 }
26042710
2605
-static ssize_t uart_get_attr_port(struct device *dev,
2711
+static ssize_t port_show(struct device *dev,
26062712 struct device_attribute *attr, char *buf)
26072713 {
26082714 struct serial_struct tmp;
....@@ -2613,135 +2719,187 @@
26132719 ioaddr = tmp.port;
26142720 if (HIGH_BITS_OFFSET)
26152721 ioaddr |= (unsigned long)tmp.port_high << HIGH_BITS_OFFSET;
2616
- return snprintf(buf, PAGE_SIZE, "0x%lX\n", ioaddr);
2722
+ return sprintf(buf, "0x%lX\n", ioaddr);
26172723 }
26182724
2619
-static ssize_t uart_get_attr_irq(struct device *dev,
2725
+static ssize_t irq_show(struct device *dev,
26202726 struct device_attribute *attr, char *buf)
26212727 {
26222728 struct serial_struct tmp;
26232729 struct tty_port *port = dev_get_drvdata(dev);
26242730
26252731 uart_get_info(port, &tmp);
2626
- return snprintf(buf, PAGE_SIZE, "%d\n", tmp.irq);
2732
+ return sprintf(buf, "%d\n", tmp.irq);
26272733 }
26282734
2629
-static ssize_t uart_get_attr_flags(struct device *dev,
2735
+static ssize_t flags_show(struct device *dev,
26302736 struct device_attribute *attr, char *buf)
26312737 {
26322738 struct serial_struct tmp;
26332739 struct tty_port *port = dev_get_drvdata(dev);
26342740
26352741 uart_get_info(port, &tmp);
2636
- return snprintf(buf, PAGE_SIZE, "0x%X\n", tmp.flags);
2742
+ return sprintf(buf, "0x%X\n", tmp.flags);
26372743 }
26382744
2639
-static ssize_t uart_get_attr_xmit_fifo_size(struct device *dev,
2745
+static ssize_t xmit_fifo_size_show(struct device *dev,
26402746 struct device_attribute *attr, char *buf)
26412747 {
26422748 struct serial_struct tmp;
26432749 struct tty_port *port = dev_get_drvdata(dev);
26442750
26452751 uart_get_info(port, &tmp);
2646
- return snprintf(buf, PAGE_SIZE, "%d\n", tmp.xmit_fifo_size);
2752
+ return sprintf(buf, "%d\n", tmp.xmit_fifo_size);
26472753 }
26482754
2649
-
2650
-static ssize_t uart_get_attr_close_delay(struct device *dev,
2755
+static ssize_t close_delay_show(struct device *dev,
26512756 struct device_attribute *attr, char *buf)
26522757 {
26532758 struct serial_struct tmp;
26542759 struct tty_port *port = dev_get_drvdata(dev);
26552760
26562761 uart_get_info(port, &tmp);
2657
- return snprintf(buf, PAGE_SIZE, "%d\n", tmp.close_delay);
2762
+ return sprintf(buf, "%d\n", tmp.close_delay);
26582763 }
26592764
2660
-
2661
-static ssize_t uart_get_attr_closing_wait(struct device *dev,
2765
+static ssize_t closing_wait_show(struct device *dev,
26622766 struct device_attribute *attr, char *buf)
26632767 {
26642768 struct serial_struct tmp;
26652769 struct tty_port *port = dev_get_drvdata(dev);
26662770
26672771 uart_get_info(port, &tmp);
2668
- return snprintf(buf, PAGE_SIZE, "%d\n", tmp.closing_wait);
2772
+ return sprintf(buf, "%d\n", tmp.closing_wait);
26692773 }
26702774
2671
-static ssize_t uart_get_attr_custom_divisor(struct device *dev,
2775
+static ssize_t custom_divisor_show(struct device *dev,
26722776 struct device_attribute *attr, char *buf)
26732777 {
26742778 struct serial_struct tmp;
26752779 struct tty_port *port = dev_get_drvdata(dev);
26762780
26772781 uart_get_info(port, &tmp);
2678
- return snprintf(buf, PAGE_SIZE, "%d\n", tmp.custom_divisor);
2782
+ return sprintf(buf, "%d\n", tmp.custom_divisor);
26792783 }
26802784
2681
-static ssize_t uart_get_attr_io_type(struct device *dev,
2785
+static ssize_t io_type_show(struct device *dev,
26822786 struct device_attribute *attr, char *buf)
26832787 {
26842788 struct serial_struct tmp;
26852789 struct tty_port *port = dev_get_drvdata(dev);
26862790
26872791 uart_get_info(port, &tmp);
2688
- return snprintf(buf, PAGE_SIZE, "%d\n", tmp.io_type);
2792
+ return sprintf(buf, "%d\n", tmp.io_type);
26892793 }
26902794
2691
-static ssize_t uart_get_attr_iomem_base(struct device *dev,
2795
+static ssize_t iomem_base_show(struct device *dev,
26922796 struct device_attribute *attr, char *buf)
26932797 {
26942798 struct serial_struct tmp;
26952799 struct tty_port *port = dev_get_drvdata(dev);
26962800
26972801 uart_get_info(port, &tmp);
2698
- return snprintf(buf, PAGE_SIZE, "0x%lX\n", (unsigned long)tmp.iomem_base);
2802
+ return sprintf(buf, "0x%lX\n", (unsigned long)tmp.iomem_base);
26992803 }
27002804
2701
-static ssize_t uart_get_attr_iomem_reg_shift(struct device *dev,
2805
+static ssize_t iomem_reg_shift_show(struct device *dev,
27022806 struct device_attribute *attr, char *buf)
27032807 {
27042808 struct serial_struct tmp;
27052809 struct tty_port *port = dev_get_drvdata(dev);
27062810
27072811 uart_get_info(port, &tmp);
2708
- return snprintf(buf, PAGE_SIZE, "%d\n", tmp.iomem_reg_shift);
2812
+ return sprintf(buf, "%d\n", tmp.iomem_reg_shift);
27092813 }
27102814
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);
2815
+static ssize_t console_show(struct device *dev,
2816
+ struct device_attribute *attr, char *buf)
2817
+{
2818
+ struct tty_port *port = dev_get_drvdata(dev);
2819
+ struct uart_state *state = container_of(port, struct uart_state, port);
2820
+ struct uart_port *uport;
2821
+ bool console = false;
2822
+
2823
+ mutex_lock(&port->mutex);
2824
+ uport = uart_port_check(state);
2825
+ if (uport)
2826
+ console = uart_console_enabled(uport);
2827
+ mutex_unlock(&port->mutex);
2828
+
2829
+ return sprintf(buf, "%c\n", console ? 'Y' : 'N');
2830
+}
2831
+
2832
+static ssize_t console_store(struct device *dev,
2833
+ struct device_attribute *attr, const char *buf, size_t count)
2834
+{
2835
+ struct tty_port *port = dev_get_drvdata(dev);
2836
+ struct uart_state *state = container_of(port, struct uart_state, port);
2837
+ struct uart_port *uport;
2838
+ bool oldconsole, newconsole;
2839
+ int ret;
2840
+
2841
+ ret = kstrtobool(buf, &newconsole);
2842
+ if (ret)
2843
+ return ret;
2844
+
2845
+ mutex_lock(&port->mutex);
2846
+ uport = uart_port_check(state);
2847
+ if (uport) {
2848
+ oldconsole = uart_console_enabled(uport);
2849
+ if (oldconsole && !newconsole) {
2850
+ ret = unregister_console(uport->cons);
2851
+ } else if (!oldconsole && newconsole) {
2852
+ if (uart_console(uport)) {
2853
+ uport->console_reinit = 1;
2854
+ register_console(uport->cons);
2855
+ } else {
2856
+ ret = -ENOENT;
2857
+ }
2858
+ }
2859
+ } else {
2860
+ ret = -ENXIO;
2861
+ }
2862
+ mutex_unlock(&port->mutex);
2863
+
2864
+ return ret < 0 ? ret : count;
2865
+}
2866
+
2867
+static DEVICE_ATTR_RO(uartclk);
2868
+static DEVICE_ATTR_RO(type);
2869
+static DEVICE_ATTR_RO(line);
2870
+static DEVICE_ATTR_RO(port);
2871
+static DEVICE_ATTR_RO(irq);
2872
+static DEVICE_ATTR_RO(flags);
2873
+static DEVICE_ATTR_RO(xmit_fifo_size);
2874
+static DEVICE_ATTR_RO(close_delay);
2875
+static DEVICE_ATTR_RO(closing_wait);
2876
+static DEVICE_ATTR_RO(custom_divisor);
2877
+static DEVICE_ATTR_RO(io_type);
2878
+static DEVICE_ATTR_RO(iomem_base);
2879
+static DEVICE_ATTR_RO(iomem_reg_shift);
2880
+static DEVICE_ATTR_RW(console);
27242881
27252882 static struct attribute *tty_dev_attrs[] = {
2883
+ &dev_attr_uartclk.attr,
27262884 &dev_attr_type.attr,
27272885 &dev_attr_line.attr,
27282886 &dev_attr_port.attr,
27292887 &dev_attr_irq.attr,
27302888 &dev_attr_flags.attr,
27312889 &dev_attr_xmit_fifo_size.attr,
2732
- &dev_attr_uartclk.attr,
27332890 &dev_attr_close_delay.attr,
27342891 &dev_attr_closing_wait.attr,
27352892 &dev_attr_custom_divisor.attr,
27362893 &dev_attr_io_type.attr,
27372894 &dev_attr_iomem_base.attr,
27382895 &dev_attr_iomem_reg_shift.attr,
2739
- NULL,
2740
- };
2896
+ &dev_attr_console.attr,
2897
+ NULL
2898
+};
27412899
27422900 static const struct attribute_group tty_dev_attr_group = {
27432901 .attrs = tty_dev_attrs,
2744
- };
2902
+};
27452903
27462904 /**
27472905 * uart_add_one_port - attach a driver-defined port structure
....@@ -2793,13 +2951,12 @@
27932951 }
27942952
27952953 /*
2796
- * If this port is a console, then the spinlock is already
2954
+ * If this port is in use as a console then the spinlock is already
27972955 * initialised.
27982956 */
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
- }
2957
+ if (!uart_console_enabled(uport))
2958
+ uart_port_spin_lock_init(uport);
2959
+
28032960 if (uport->cons && uport->dev)
28042961 of_console_check(uport->dev->of_node, uport->cons->name, uport->line);
28052962
....@@ -2828,7 +2985,7 @@
28282985 */
28292986 tty_dev = tty_port_register_device_attr_serdev(port, drv->tty_driver,
28302987 uport->line, uport->dev, port, uport->tty_groups);
2831
- if (likely(!IS_ERR(tty_dev))) {
2988
+ if (!IS_ERR(tty_dev)) {
28322989 device_set_wakeup_capable(tty_dev, 1);
28332990 } else {
28342991 dev_err(uport->dev, "Cannot register tty device on line %d\n",
....@@ -3050,6 +3207,56 @@
30503207 }
30513208 EXPORT_SYMBOL_GPL(uart_insert_char);
30523209
3210
+#ifdef CONFIG_MAGIC_SYSRQ_SERIAL
3211
+static const char sysrq_toggle_seq[] = CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE;
3212
+
3213
+static void uart_sysrq_on(struct work_struct *w)
3214
+{
3215
+ int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq);
3216
+
3217
+ sysrq_toggle_support(1);
3218
+ pr_info("SysRq is enabled by magic sequence '%*pE' on serial\n",
3219
+ sysrq_toggle_seq_len, sysrq_toggle_seq);
3220
+}
3221
+static DECLARE_WORK(sysrq_enable_work, uart_sysrq_on);
3222
+
3223
+/**
3224
+ * uart_try_toggle_sysrq - Enables SysRq from serial line
3225
+ * @port: uart_port structure where char(s) after BREAK met
3226
+ * @ch: new character in the sequence after received BREAK
3227
+ *
3228
+ * Enables magic SysRq when the required sequence is met on port
3229
+ * (see CONFIG_MAGIC_SYSRQ_SERIAL_SEQUENCE).
3230
+ *
3231
+ * Returns false if @ch is out of enabling sequence and should be
3232
+ * handled some other way, true if @ch was consumed.
3233
+ */
3234
+bool uart_try_toggle_sysrq(struct uart_port *port, unsigned int ch)
3235
+{
3236
+ int sysrq_toggle_seq_len = strlen(sysrq_toggle_seq);
3237
+
3238
+ if (!sysrq_toggle_seq_len)
3239
+ return false;
3240
+
3241
+ BUILD_BUG_ON(ARRAY_SIZE(sysrq_toggle_seq) >= U8_MAX);
3242
+ if (sysrq_toggle_seq[port->sysrq_seq] != ch) {
3243
+ port->sysrq_seq = 0;
3244
+ return false;
3245
+ }
3246
+
3247
+ if (++port->sysrq_seq < sysrq_toggle_seq_len) {
3248
+ port->sysrq = jiffies + SYSRQ_TIMEOUT;
3249
+ return true;
3250
+ }
3251
+
3252
+ schedule_work(&sysrq_enable_work);
3253
+
3254
+ port->sysrq = 0;
3255
+ return true;
3256
+}
3257
+EXPORT_SYMBOL_GPL(uart_try_toggle_sysrq);
3258
+#endif
3259
+
30533260 EXPORT_SYMBOL(uart_write_wakeup);
30543261 EXPORT_SYMBOL(uart_register_driver);
30553262 EXPORT_SYMBOL(uart_unregister_driver);
....@@ -3060,14 +3267,15 @@
30603267
30613268 /**
30623269 * uart_get_rs485_mode() - retrieve rs485 properties for given uart
3063
- * @dev: uart device
3064
- * @rs485conf: output parameter
3270
+ * @port: uart device's target port
30653271 *
30663272 * This function implements the device tree binding described in
30673273 * Documentation/devicetree/bindings/serial/rs485.txt.
30683274 */
3069
-void uart_get_rs485_mode(struct device *dev, struct serial_rs485 *rs485conf)
3275
+int uart_get_rs485_mode(struct uart_port *port)
30703276 {
3277
+ struct serial_rs485 *rs485conf = &port->rs485;
3278
+ struct device *dev = port->dev;
30713279 u32 rs485_delay[2];
30723280 int ret;
30733281
....@@ -3086,6 +3294,7 @@
30863294 * to get to a defined state with the following properties:
30873295 */
30883296 rs485conf->flags &= ~(SER_RS485_RX_DURING_TX | SER_RS485_ENABLED |
3297
+ SER_RS485_TERMINATE_BUS |
30893298 SER_RS485_RTS_AFTER_SEND);
30903299 rs485conf->flags |= SER_RS485_RTS_ON_SEND;
30913300
....@@ -3099,6 +3308,21 @@
30993308 rs485conf->flags &= ~SER_RS485_RTS_ON_SEND;
31003309 rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
31013310 }
3311
+
3312
+ /*
3313
+ * Disabling termination by default is the safe choice: Else if many
3314
+ * bus participants enable it, no communication is possible at all.
3315
+ * Works fine for short cables and users may enable for longer cables.
3316
+ */
3317
+ port->rs485_term_gpio = devm_gpiod_get_optional(dev, "rs485-term",
3318
+ GPIOD_OUT_LOW);
3319
+ if (IS_ERR(port->rs485_term_gpio)) {
3320
+ ret = PTR_ERR(port->rs485_term_gpio);
3321
+ port->rs485_term_gpio = NULL;
3322
+ return dev_err_probe(dev, ret, "Cannot get rs485-term-gpios\n");
3323
+ }
3324
+
3325
+ return 0;
31023326 }
31033327 EXPORT_SYMBOL_GPL(uart_get_rs485_mode);
31043328