Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'tty-6.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty

Pull tty/serial updates from Greg KH:
"Here is the big set of tty/serial driver changes for 6.19-rc1. Nothing
major at all, just small constant churn to make the tty layer
"cleaner" as well as serial driver updates and even a new test added!
Included in here are:

- More tty/serial cleanups from Jiri

- tty tiocsti test added to hopefully ensure we don't regress in this
area again

- sc16is7xx driver updates

- imx serial driver updates

- 8250 driver updates

- new hardware device ids added

- other minor serial/tty driver cleanups and tweaks

All of these have been in linux-next for a while with no reported
issues"

* tag 'tty-6.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty: (60 commits)
serial: sh-sci: Fix deadlock during RSCI FIFO overrun error
dt-bindings: serial: rsci: Drop "uart-has-rtscts: false"
LoongArch: dts: Add uart new compatible string
serial: 8250: Add Loongson uart driver support
dt-bindings: serial: 8250: Add Loongson uart compatible
serial: 8250: add driver for KEBA UART
serial: Keep rs485 settings for devices without firmware node
serial: qcom-geni: Enable Serial on SA8255p Qualcomm platforms
serial: qcom-geni: Enable PM runtime for serial driver
serial: sprd: Return -EPROBE_DEFER when uart clock is not ready
tty: serial: samsung: Declare earlycon for Exynos850
serial: icom: Convert PCIBIOS_* return codes to errnos
serial: 8250-of: Fix style issues in 8250_of.c
serial: add support of CPCI cards
serial: mux: Fix kernel doc for mux_poll()
tty: replace use of system_unbound_wq with system_dfl_wq
serial: 8250_platform: simplify IRQF_SHARED handling
serial: 8250: make share_irqs local to 8250_platform
serial: 8250: move skip_txen_test to core
serial: drop SERIAL_8250_DEPRECATED_OPTIONS
...

+2338 -1161
+14
Documentation/devicetree/bindings/serial/8250.yaml
··· 125 125 - nxp,lpc1850-uart 126 126 - opencores,uart16550-rtlsvn105 127 127 - ti,da830-uart 128 + - loongson,ls2k0500-uart 129 + - loongson,ls2k1500-uart 128 130 - const: ns16550a 129 131 - items: 130 132 - enum: ··· 171 169 - nvidia,tegra194-uart 172 170 - nvidia,tegra234-uart 173 171 - const: nvidia,tegra20-uart 172 + - items: 173 + - enum: 174 + - loongson,ls2k1000-uart 175 + - const: loongson,ls2k0500-uart 176 + - const: ns16550a 177 + - items: 178 + - enum: 179 + - loongson,ls3a5000-uart 180 + - loongson,ls3a6000-uart 181 + - loongson,ls2k2000-uart 182 + - const: loongson,ls2k1500-uart 183 + - const: ns16550a 174 184 175 185 reg: 176 186 maxItems: 1
-2
Documentation/devicetree/bindings/serial/renesas,rsci.yaml
··· 54 54 power-domains: 55 55 maxItems: 1 56 56 57 - uart-has-rtscts: false 58 - 59 57 required: 60 58 - compatible 61 59 - reg
+2
Documentation/devicetree/bindings/serial/samsung_uart.yaml
··· 48 48 - const: samsung,exynos850-uart 49 49 - items: 50 50 - enum: 51 + - axis,artpec9-uart 51 52 - samsung,exynos7870-uart 53 + - samsung,exynos8890-uart 52 54 - const: samsung,exynos8895-uart 53 55 54 56 reg:
+1
Documentation/devicetree/bindings/serial/snps-dw-apb-uart.yaml
··· 65 65 - rockchip,rk3328-uart 66 66 - rockchip,rk3368-uart 67 67 - rockchip,rk3399-uart 68 + - rockchip,rk3506-uart 68 69 - rockchip,rk3528-uart 69 70 - rockchip,rk3562-uart 70 71 - rockchip,rk3568-uart
-1
arch/arm/configs/aspeed_g4_defconfig
··· 117 117 # CONFIG_VT is not set 118 118 # CONFIG_LEGACY_PTYS is not set 119 119 CONFIG_SERIAL_8250=y 120 - # CONFIG_SERIAL_8250_DEPRECATED_OPTIONS is not set 121 120 CONFIG_SERIAL_8250_CONSOLE=y 122 121 CONFIG_SERIAL_8250_NR_UARTS=6 123 122 CONFIG_SERIAL_8250_RUNTIME_UARTS=6
-1
arch/arm/configs/aspeed_g5_defconfig
··· 138 138 # CONFIG_VT is not set 139 139 # CONFIG_LEGACY_PTYS is not set 140 140 CONFIG_SERIAL_8250=y 141 - # CONFIG_SERIAL_8250_DEPRECATED_OPTIONS is not set 142 141 CONFIG_SERIAL_8250_CONSOLE=y 143 142 CONFIG_SERIAL_8250_NR_UARTS=6 144 143 CONFIG_SERIAL_8250_RUNTIME_UARTS=6
-1
arch/arm/configs/hisi_defconfig
··· 35 35 CONFIG_HIX5HD2_GMAC=y 36 36 CONFIG_HIP04_ETH=y 37 37 CONFIG_SERIAL_8250=y 38 - CONFIG_SERIAL_8250_DEPRECATED_OPTIONS=y 39 38 CONFIG_SERIAL_8250_CONSOLE=y 40 39 CONFIG_SERIAL_8250_NR_UARTS=2 41 40 CONFIG_SERIAL_8250_RUNTIME_UARTS=2
-1
arch/arm/configs/lpc18xx_defconfig
··· 90 90 # CONFIG_UNIX98_PTYS is not set 91 91 # CONFIG_LEGACY_PTYS is not set 92 92 CONFIG_SERIAL_8250=y 93 - # CONFIG_SERIAL_8250_DEPRECATED_OPTIONS is not set 94 93 CONFIG_SERIAL_8250_CONSOLE=y 95 94 CONFIG_SERIAL_OF_PLATFORM=y 96 95 CONFIG_SERIAL_NONSTANDARD=y
-1
arch/arm/configs/shmobile_defconfig
··· 75 75 CONFIG_INPUT_ADXL34X=y 76 76 # CONFIG_LEGACY_PTYS is not set 77 77 CONFIG_SERIAL_8250=y 78 - # CONFIG_SERIAL_8250_DEPRECATED_OPTIONS is not set 79 78 # CONFIG_SERIAL_8250_16550A_VARIANTS is not set 80 79 CONFIG_SERIAL_8250_CONSOLE=y 81 80 # CONFIG_SERIAL_8250_PCI is not set
+1 -1
arch/loongarch/boot/dts/loongson-2k0500.dtsi
··· 380 380 }; 381 381 382 382 uart0: serial@1ff40800 { 383 - compatible = "ns16550a"; 383 + compatible = "loongson,ls2k0500-uart", "ns16550a"; 384 384 reg = <0x0 0x1ff40800 0x0 0x10>; 385 385 clock-frequency = <100000000>; 386 386 interrupt-parent = <&eiointc>;
+1 -1
arch/loongarch/boot/dts/loongson-2k1000.dtsi
··· 297 297 }; 298 298 299 299 uart0: serial@1fe20000 { 300 - compatible = "ns16550a"; 300 + compatible = "loongson,ls2k1000-uart", "loongson,ls2k0500-uart", "ns16550a"; 301 301 reg = <0x0 0x1fe20000 0x0 0x10>; 302 302 clock-frequency = <125000000>; 303 303 interrupt-parent = <&liointc0>;
+1 -1
arch/loongarch/boot/dts/loongson-2k2000.dtsi
··· 250 250 }; 251 251 252 252 uart0: serial@1fe001e0 { 253 - compatible = "ns16550a"; 253 + compatible = "loongson,ls2k2000-uart", "loongson,ls2k1500-uart", "ns16550a"; 254 254 reg = <0x0 0x1fe001e0 0x0 0x10>; 255 255 clock-frequency = <100000000>; 256 256 interrupt-parent = <&liointc>;
-1
arch/mips/configs/bcm47xx_defconfig
··· 51 51 CONFIG_BRCMSMAC=y 52 52 CONFIG_ISDN=y 53 53 CONFIG_SERIAL_8250=y 54 - # CONFIG_SERIAL_8250_DEPRECATED_OPTIONS is not set 55 54 CONFIG_SERIAL_8250_CONSOLE=y 56 55 # CONFIG_SERIAL_8250_PCI is not set 57 56 CONFIG_SERIAL_8250_NR_UARTS=2
-1
arch/mips/configs/bmips_stb_defconfig
··· 119 119 CONFIG_VT=y 120 120 CONFIG_VT_HW_CONSOLE_BINDING=y 121 121 CONFIG_SERIAL_8250=y 122 - # CONFIG_SERIAL_8250_DEPRECATED_OPTIONS is not set 123 122 CONFIG_SERIAL_8250_CONSOLE=y 124 123 CONFIG_SERIAL_OF_PLATFORM=y 125 124 # CONFIG_HW_RANDOM is not set
-1
arch/mips/configs/gcw0_defconfig
··· 52 52 CONFIG_INPUT_PWM_VIBRA=y 53 53 # CONFIG_SERIO is not set 54 54 CONFIG_SERIAL_8250=y 55 - # CONFIG_SERIAL_8250_DEPRECATED_OPTIONS is not set 56 55 CONFIG_SERIAL_8250_CONSOLE=y 57 56 CONFIG_SERIAL_8250_INGENIC=y 58 57 CONFIG_HW_RANDOM=y
-1
arch/nios2/configs/10m50_defconfig
··· 51 51 # CONFIG_SERIO_SERPORT is not set 52 52 # CONFIG_VT is not set 53 53 CONFIG_SERIAL_8250=y 54 - # CONFIG_SERIAL_8250_DEPRECATED_OPTIONS is not set 55 54 CONFIG_SERIAL_8250_CONSOLE=y 56 55 CONFIG_SERIAL_OF_PLATFORM=y 57 56 CONFIG_SERIAL_ALTERA_JTAGUART=y
-1
arch/parisc/configs/generic-32bit_defconfig
··· 119 119 CONFIG_INPUT_UINPUT=m 120 120 CONFIG_LEGACY_PTY_COUNT=64 121 121 CONFIG_SERIAL_8250=y 122 - # CONFIG_SERIAL_8250_DEPRECATED_OPTIONS is not set 123 122 CONFIG_SERIAL_8250_CONSOLE=y 124 123 CONFIG_SERIAL_8250_NR_UARTS=8 125 124 CONFIG_SERIAL_8250_EXTENDED=y
-1
arch/parisc/configs/generic-64bit_defconfig
··· 158 158 CONFIG_SERIO_RAW=m 159 159 # CONFIG_LEGACY_PTYS is not set 160 160 CONFIG_SERIAL_8250=y 161 - # CONFIG_SERIAL_8250_DEPRECATED_OPTIONS is not set 162 161 CONFIG_SERIAL_8250_CONSOLE=y 163 162 CONFIG_SERIAL_8250_NR_UARTS=8 164 163 CONFIG_SERIAL_8250_RUNTIME_UARTS=8
-1
arch/powerpc/configs/44x/akebono_defconfig
··· 85 85 # CONFIG_SERIO is not set 86 86 # CONFIG_VT is not set 87 87 CONFIG_SERIAL_8250=y 88 - # CONFIG_SERIAL_8250_DEPRECATED_OPTIONS is not set 89 88 CONFIG_SERIAL_8250_CONSOLE=y 90 89 CONFIG_SERIAL_8250_EXTENDED=y 91 90 CONFIG_SERIAL_8250_SHARE_IRQ=y
-1
arch/powerpc/configs/microwatt_defconfig
··· 62 62 # CONFIG_VT is not set 63 63 # CONFIG_LEGACY_PTYS is not set 64 64 CONFIG_SERIAL_8250=y 65 - # CONFIG_SERIAL_8250_DEPRECATED_OPTIONS is not set 66 65 CONFIG_SERIAL_8250_CONSOLE=y 67 66 CONFIG_SERIAL_OF_PLATFORM=y 68 67 CONFIG_SERIAL_NONSTANDARD=y
-1
arch/riscv/configs/nommu_virt_defconfig
··· 48 48 # CONFIG_LEGACY_PTYS is not set 49 49 # CONFIG_LDISC_AUTOLOAD is not set 50 50 CONFIG_SERIAL_8250=y 51 - # CONFIG_SERIAL_8250_DEPRECATED_OPTIONS is not set 52 51 CONFIG_SERIAL_8250_CONSOLE=y 53 52 CONFIG_SERIAL_8250_NR_UARTS=1 54 53 CONFIG_SERIAL_8250_RUNTIME_UARTS=1
-1
arch/xtensa/configs/audio_kc705_defconfig
··· 81 81 # CONFIG_INPUT_MOUSE is not set 82 82 # CONFIG_SERIO is not set 83 83 CONFIG_SERIAL_8250=y 84 - # CONFIG_SERIAL_8250_DEPRECATED_OPTIONS is not set 85 84 CONFIG_SERIAL_8250_CONSOLE=y 86 85 CONFIG_SERIAL_OF_PLATFORM=y 87 86 CONFIG_HW_RANDOM=y
-1
arch/xtensa/configs/generic_kc705_defconfig
··· 79 79 # CONFIG_INPUT_MOUSE is not set 80 80 # CONFIG_SERIO is not set 81 81 CONFIG_SERIAL_8250=y 82 - # CONFIG_SERIAL_8250_DEPRECATED_OPTIONS is not set 83 82 CONFIG_SERIAL_8250_CONSOLE=y 84 83 CONFIG_SERIAL_OF_PLATFORM=y 85 84 CONFIG_HW_RANDOM=y
-1
arch/xtensa/configs/nommu_kc705_defconfig
··· 81 81 # CONFIG_INPUT_MOUSE is not set 82 82 # CONFIG_SERIO is not set 83 83 CONFIG_SERIAL_8250=y 84 - # CONFIG_SERIAL_8250_DEPRECATED_OPTIONS is not set 85 84 CONFIG_SERIAL_8250_CONSOLE=y 86 85 CONFIG_SERIAL_OF_PLATFORM=y 87 86 CONFIG_HW_RANDOM=y
-1
arch/xtensa/configs/smp_lx200_defconfig
··· 83 83 # CONFIG_INPUT_MOUSE is not set 84 84 # CONFIG_SERIO is not set 85 85 CONFIG_SERIAL_8250=y 86 - # CONFIG_SERIAL_8250_DEPRECATED_OPTIONS is not set 87 86 CONFIG_SERIAL_8250_CONSOLE=y 88 87 CONFIG_SERIAL_OF_PLATFORM=y 89 88 CONFIG_HW_RANDOM=y
-1
arch/xtensa/configs/xip_kc705_defconfig
··· 72 72 # CONFIG_INPUT_MOUSE is not set 73 73 # CONFIG_SERIO is not set 74 74 CONFIG_SERIAL_8250=y 75 - # CONFIG_SERIAL_8250_DEPRECATED_OPTIONS is not set 76 75 CONFIG_SERIAL_8250_CONSOLE=y 77 76 CONFIG_SERIAL_OF_PLATFORM=y 78 77 # CONFIG_HWMON is not set
+71 -94
drivers/tty/moxa.c
··· 487 487 488 488 static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg) 489 489 { 490 - unsigned long flags; 491 - spin_lock_irqsave(&moxafunc_lock, flags); 490 + guard(spinlock_irqsave)(&moxafunc_lock); 492 491 writew(arg, ofsAddr + FuncArg); 493 492 writew(cmd, ofsAddr + FuncCode); 494 493 moxa_wait_finish(ofsAddr); 495 - spin_unlock_irqrestore(&moxafunc_lock, flags); 496 494 } 497 495 498 496 static int moxafuncret(void __iomem *ofsAddr, u16 cmd, u16 arg) 499 497 { 500 - unsigned long flags; 501 - u16 ret; 502 - spin_lock_irqsave(&moxafunc_lock, flags); 498 + guard(spinlock_irqsave)(&moxafunc_lock); 503 499 writew(arg, ofsAddr + FuncArg); 504 500 writew(cmd, ofsAddr + FuncCode); 505 501 moxa_wait_finish(ofsAddr); 506 - ret = readw(ofsAddr + FuncArg); 507 - spin_unlock_irqrestore(&moxafunc_lock, flags); 508 - return ret; 502 + 503 + return readw(ofsAddr + FuncArg); 509 504 } 510 505 511 506 static void moxa_low_water_check(void __iomem *ofsAddr) ··· 997 1002 if (ret) 998 1003 goto err_free; 999 1004 1000 - spin_lock_bh(&moxa_lock); 1001 - brd->ready = 1; 1002 - if (!timer_pending(&moxaTimer)) 1003 - mod_timer(&moxaTimer, jiffies + HZ / 50); 1004 - spin_unlock_bh(&moxa_lock); 1005 + scoped_guard(spinlock_bh, &moxa_lock) { 1006 + brd->ready = 1; 1007 + if (!timer_pending(&moxaTimer)) 1008 + mod_timer(&moxaTimer, jiffies + HZ / 50); 1009 + } 1005 1010 1006 1011 first_idx = (brd - moxa_boards) * MAX_PORTS_PER_BOARD; 1007 1012 for (i = 0; i < brd->numPorts; i++) ··· 1021 1026 { 1022 1027 unsigned int a, opened, first_idx; 1023 1028 1024 - mutex_lock(&moxa_openlock); 1025 - spin_lock_bh(&moxa_lock); 1026 - brd->ready = 0; 1027 - spin_unlock_bh(&moxa_lock); 1029 + scoped_guard(mutex, &moxa_openlock) { 1030 + scoped_guard(spinlock_bh, &moxa_lock) 1031 + brd->ready = 0; 1028 1032 1029 - /* pci hot-un-plug support */ 1030 - for (a = 0; a < brd->numPorts; a++) 1031 - if (tty_port_initialized(&brd->ports[a].port)) 1032 - tty_port_tty_hangup(&brd->ports[a].port, false); 1033 - 1034 - for (a = 0; a < MAX_PORTS_PER_BOARD; a++) 1035 - tty_port_destroy(&brd->ports[a].port); 1036 - 1037 - while (1) { 1038 - opened = 0; 1033 + /* pci hot-un-plug support */ 1039 1034 for (a = 0; a < brd->numPorts; a++) 1040 1035 if (tty_port_initialized(&brd->ports[a].port)) 1041 - opened++; 1042 - mutex_unlock(&moxa_openlock); 1043 - if (!opened) 1044 - break; 1045 - msleep(50); 1046 - mutex_lock(&moxa_openlock); 1036 + tty_port_tty_hangup(&brd->ports[a].port, false); 1037 + 1038 + for (a = 0; a < MAX_PORTS_PER_BOARD; a++) 1039 + tty_port_destroy(&brd->ports[a].port); 1040 + 1041 + while (1) { 1042 + opened = 0; 1043 + for (a = 0; a < brd->numPorts; a++) 1044 + if (tty_port_initialized(&brd->ports[a].port)) 1045 + opened++; 1046 + if (!opened) 1047 + break; 1048 + mutex_unlock(&moxa_openlock); 1049 + msleep(50); 1050 + mutex_lock(&moxa_openlock); 1051 + } 1047 1052 } 1048 1053 1049 1054 first_idx = (brd - moxa_boards) * MAX_PORTS_PER_BOARD; ··· 1201 1206 static bool moxa_carrier_raised(struct tty_port *port) 1202 1207 { 1203 1208 struct moxa_port *ch = container_of(port, struct moxa_port, port); 1204 - int dcd; 1205 1209 1206 - spin_lock_irq(&port->lock); 1207 - dcd = ch->DCDState; 1208 - spin_unlock_irq(&port->lock); 1209 - return dcd; 1210 + guard(spinlock_irq)(&port->lock); 1211 + return ch->DCDState; 1210 1212 } 1211 1213 1212 1214 static void moxa_dtr_rts(struct tty_port *port, bool active) ··· 1217 1225 { 1218 1226 struct moxa_board_conf *brd; 1219 1227 struct moxa_port *ch; 1220 - int port; 1228 + int port = tty->index; 1221 1229 1222 - port = tty->index; 1223 - if (mutex_lock_interruptible(&moxa_openlock)) 1224 - return -ERESTARTSYS; 1225 - brd = &moxa_boards[port / MAX_PORTS_PER_BOARD]; 1226 - if (!brd->ready) { 1227 - mutex_unlock(&moxa_openlock); 1228 - return -ENODEV; 1229 - } 1230 + scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &moxa_openlock) { 1231 + brd = &moxa_boards[port / MAX_PORTS_PER_BOARD]; 1232 + if (!brd->ready) 1233 + return -ENODEV; 1230 1234 1231 - if (port % MAX_PORTS_PER_BOARD >= brd->numPorts) { 1232 - mutex_unlock(&moxa_openlock); 1233 - return -ENODEV; 1234 - } 1235 + if (port % MAX_PORTS_PER_BOARD >= brd->numPorts) 1236 + return -ENODEV; 1235 1237 1236 - ch = &brd->ports[port % MAX_PORTS_PER_BOARD]; 1237 - ch->port.count++; 1238 - tty->driver_data = ch; 1239 - tty_port_tty_set(&ch->port, tty); 1240 - mutex_lock(&ch->port.mutex); 1241 - if (!tty_port_initialized(&ch->port)) { 1242 - ch->statusflags = 0; 1243 - moxa_set_tty_param(tty, &tty->termios); 1244 - MoxaPortLineCtrl(ch, true, true); 1245 - MoxaPortEnable(ch); 1246 - MoxaSetFifo(ch, ch->type == PORT_16550A); 1247 - tty_port_set_initialized(&ch->port, true); 1238 + ch = &brd->ports[port % MAX_PORTS_PER_BOARD]; 1239 + ch->port.count++; 1240 + tty->driver_data = ch; 1241 + tty_port_tty_set(&ch->port, tty); 1242 + 1243 + guard(mutex)(&ch->port.mutex); 1244 + if (!tty_port_initialized(&ch->port)) { 1245 + ch->statusflags = 0; 1246 + moxa_set_tty_param(tty, &tty->termios); 1247 + MoxaPortLineCtrl(ch, true, true); 1248 + MoxaPortEnable(ch); 1249 + MoxaSetFifo(ch, ch->type == PORT_16550A); 1250 + tty_port_set_initialized(&ch->port, true); 1251 + } 1248 1252 } 1249 - mutex_unlock(&ch->port.mutex); 1250 - mutex_unlock(&moxa_openlock); 1251 1253 1252 1254 return tty_port_block_til_ready(&ch->port, tty, filp); 1253 1255 } ··· 1256 1270 static ssize_t moxa_write(struct tty_struct *tty, const u8 *buf, size_t count) 1257 1271 { 1258 1272 struct moxa_port *ch = tty->driver_data; 1259 - unsigned long flags; 1260 1273 int len; 1261 1274 1262 1275 if (ch == NULL) 1263 1276 return 0; 1264 1277 1265 - spin_lock_irqsave(&moxa_lock, flags); 1266 - len = MoxaPortWriteData(tty, buf, count); 1267 - spin_unlock_irqrestore(&moxa_lock, flags); 1278 + scoped_guard(spinlock_irqsave, &moxa_lock) 1279 + len = MoxaPortWriteData(tty, buf, count); 1268 1280 1269 1281 set_bit(LOWWAIT, &ch->statusflags); 1270 1282 return len; ··· 1333 1349 bool dtr_active, rts_active; 1334 1350 struct moxa_port *ch; 1335 1351 1336 - mutex_lock(&moxa_openlock); 1352 + guard(mutex)(&moxa_openlock); 1337 1353 ch = tty->driver_data; 1338 - if (!ch) { 1339 - mutex_unlock(&moxa_openlock); 1354 + if (!ch) 1340 1355 return -EINVAL; 1341 - } 1342 1356 1343 1357 MoxaPortGetLineOut(ch, &dtr_active, &rts_active); 1344 1358 if (set & TIOCM_RTS) ··· 1348 1366 if (clear & TIOCM_DTR) 1349 1367 dtr_active = false; 1350 1368 MoxaPortLineCtrl(ch, dtr_active, rts_active); 1351 - mutex_unlock(&moxa_openlock); 1369 + 1352 1370 return 0; 1353 1371 } 1354 1372 ··· 1397 1415 1398 1416 static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd) 1399 1417 { 1400 - unsigned long flags; 1401 1418 dcd = !!dcd; 1402 1419 1403 - spin_lock_irqsave(&p->port.lock, flags); 1404 - if (dcd != p->DCDState) { 1405 - p->DCDState = dcd; 1406 - spin_unlock_irqrestore(&p->port.lock, flags); 1407 - if (!dcd) 1408 - tty_port_tty_hangup(&p->port, true); 1420 + scoped_guard(spinlock_irqsave, &p->port.lock) { 1421 + if (dcd == p->DCDState) 1422 + return; 1423 + 1424 + p->DCDState = dcd; 1409 1425 } 1410 - else 1411 - spin_unlock_irqrestore(&p->port.lock, flags); 1426 + 1427 + if (!dcd) 1428 + tty_port_tty_hangup(&p->port, true); 1412 1429 } 1413 1430 1414 1431 static int moxa_poll_port(struct moxa_port *p, unsigned int handle, ··· 1475 1494 u16 __iomem *ip; 1476 1495 unsigned int card, port, served = 0; 1477 1496 1478 - spin_lock(&moxa_lock); 1497 + guard(spinlock)(&moxa_lock); 1479 1498 for (card = 0; card < MAX_BOARDS; card++) { 1480 1499 brd = &moxa_boards[card]; 1481 1500 if (!brd->ready) ··· 1506 1525 1507 1526 if (served) 1508 1527 mod_timer(&moxaTimer, jiffies + HZ / 50); 1509 - spin_unlock(&moxa_lock); 1510 1528 } 1511 1529 1512 1530 /******************************************************************************/ ··· 1841 1861 baud = MoxaPortSetBaud(port, baud); 1842 1862 1843 1863 if (termio->c_iflag & (IXON | IXOFF | IXANY)) { 1844 - spin_lock_irq(&moxafunc_lock); 1864 + guard(spinlock_irq)(&moxafunc_lock); 1845 1865 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg); 1846 1866 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1); 1847 1867 writeb(FC_SetXonXoff, ofsAddr + FuncCode); 1848 1868 moxa_wait_finish(ofsAddr); 1849 - spin_unlock_irq(&moxafunc_lock); 1850 - 1851 1869 } 1852 1870 return baud; 1853 1871 } ··· 2076 2098 2077 2099 if (!info) 2078 2100 return -ENODEV; 2079 - mutex_lock(&info->port.mutex); 2101 + guard(mutex)(&info->port.mutex); 2080 2102 ss->type = info->type; 2081 2103 ss->line = info->port.tty->index; 2082 2104 ss->flags = info->port.flags; 2083 2105 ss->baud_base = 921600; 2084 2106 ss->close_delay = jiffies_to_msecs(info->port.close_delay) / 10; 2085 - mutex_unlock(&info->port.mutex); 2107 + 2086 2108 return 0; 2087 2109 } 2088 2110 ··· 2098 2120 2099 2121 close_delay = msecs_to_jiffies(ss->close_delay * 10); 2100 2122 2101 - mutex_lock(&info->port.mutex); 2123 + guard(mutex)(&info->port.mutex); 2102 2124 if (!capable(CAP_SYS_ADMIN)) { 2103 2125 if (close_delay != info->port.close_delay || 2104 2126 ss->type != info->type || 2105 2127 ((ss->flags & ~ASYNC_USR_MASK) != 2106 2128 (info->port.flags & ~ASYNC_USR_MASK))) { 2107 - mutex_unlock(&info->port.mutex); 2108 2129 return -EPERM; 2109 2130 } 2110 2131 } else { ··· 2113 2136 2114 2137 info->type = ss->type; 2115 2138 } 2116 - mutex_unlock(&info->port.mutex); 2139 + 2117 2140 return 0; 2118 2141 } 2119 2142
+1 -1
drivers/tty/n_gsm.c
··· 4165 4165 /** 4166 4166 * gsm_modem_send_initial_msc - Send initial modem status message 4167 4167 * 4168 - * @dlci channel 4168 + * @dlci: channel 4169 4169 * 4170 4170 * Send an initial MSC message after DLCI open to set the initial 4171 4171 * modem status lines. This is only done for basic mode.
+29 -50
drivers/tty/n_hdlc.c
··· 263 263 */ 264 264 static void n_hdlc_send_frames(struct n_hdlc *n_hdlc, struct tty_struct *tty) 265 265 { 266 - unsigned long flags; 267 266 struct n_hdlc_buf *tbuf; 268 267 ssize_t actual; 269 268 270 269 check_again: 271 - 272 - spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags); 273 - if (n_hdlc->tbusy) { 274 - n_hdlc->woke_up = true; 275 - spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); 276 - return; 270 + scoped_guard(spinlock_irqsave, &n_hdlc->tx_buf_list.spinlock) { 271 + if (n_hdlc->tbusy) { 272 + n_hdlc->woke_up = true; 273 + return; 274 + } 275 + n_hdlc->tbusy = true; 276 + n_hdlc->woke_up = false; 277 277 } 278 - n_hdlc->tbusy = true; 279 - n_hdlc->woke_up = false; 280 - spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); 281 278 282 279 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list); 283 280 while (tbuf) { ··· 321 324 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); 322 325 323 326 /* Clear the re-entry flag */ 324 - spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags); 325 - n_hdlc->tbusy = false; 326 - spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); 327 + scoped_guard(spinlock_irqsave, &n_hdlc->tx_buf_list.spinlock) 328 + n_hdlc->tbusy = false; 327 329 328 330 if (n_hdlc->woke_up) 329 331 goto check_again; ··· 580 584 unsigned long arg) 581 585 { 582 586 struct n_hdlc *n_hdlc = tty->disc_data; 583 - int error = 0; 584 587 int count; 585 - unsigned long flags; 586 588 struct n_hdlc_buf *buf = NULL; 587 589 588 590 pr_debug("%s() called %d\n", __func__, cmd); ··· 589 595 case FIONREAD: 590 596 /* report count of read data available */ 591 597 /* in next available frame (if any) */ 592 - spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock, flags); 593 - buf = list_first_entry_or_null(&n_hdlc->rx_buf_list.list, 594 - struct n_hdlc_buf, list_item); 595 - if (buf) 596 - count = buf->count; 597 - else 598 - count = 0; 599 - spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock, flags); 600 - error = put_user(count, (int __user *)arg); 601 - break; 598 + scoped_guard(spinlock_irqsave, &n_hdlc->rx_buf_list.spinlock) { 599 + buf = list_first_entry_or_null(&n_hdlc->rx_buf_list.list, 600 + struct n_hdlc_buf, list_item); 601 + if (buf) 602 + count = buf->count; 603 + else 604 + count = 0; 605 + } 606 + return put_user(count, (int __user *)arg); 602 607 603 608 case TIOCOUTQ: 604 609 /* get the pending tx byte count in the driver */ 605 610 count = tty_chars_in_buffer(tty); 606 611 /* add size of next output frame in queue */ 607 - spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags); 608 - buf = list_first_entry_or_null(&n_hdlc->tx_buf_list.list, 609 - struct n_hdlc_buf, list_item); 610 - if (buf) 611 - count += buf->count; 612 - spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); 613 - error = put_user(count, (int __user *)arg); 614 - break; 612 + scoped_guard(spinlock_irqsave, &n_hdlc->tx_buf_list.spinlock) { 613 + buf = list_first_entry_or_null(&n_hdlc->tx_buf_list.list, 614 + struct n_hdlc_buf, list_item); 615 + if (buf) 616 + count += buf->count; 617 + } 618 + return put_user(count, (int __user *)arg); 615 619 616 620 case TCFLSH: 617 621 switch (arg) { ··· 620 628 fallthrough; /* to default */ 621 629 622 630 default: 623 - error = n_tty_ioctl_helper(tty, cmd, arg); 624 - break; 631 + return n_tty_ioctl_helper(tty, cmd, arg); 625 632 } 626 - return error; 627 - 628 633 } /* end of n_hdlc_tty_ioctl() */ 629 634 630 635 /** ··· 715 726 static void n_hdlc_buf_return(struct n_hdlc_buf_list *buf_list, 716 727 struct n_hdlc_buf *buf) 717 728 { 718 - unsigned long flags; 719 - 720 - spin_lock_irqsave(&buf_list->spinlock, flags); 729 + guard(spinlock_irqsave)(&buf_list->spinlock); 721 730 722 731 list_add(&buf->list_item, &buf_list->list); 723 732 buf_list->count++; 724 - 725 - spin_unlock_irqrestore(&buf_list->spinlock, flags); 726 733 } 727 734 728 735 /** ··· 729 744 static void n_hdlc_buf_put(struct n_hdlc_buf_list *buf_list, 730 745 struct n_hdlc_buf *buf) 731 746 { 732 - unsigned long flags; 733 - 734 - spin_lock_irqsave(&buf_list->spinlock, flags); 747 + guard(spinlock_irqsave)(&buf_list->spinlock); 735 748 736 749 list_add_tail(&buf->list_item, &buf_list->list); 737 750 buf_list->count++; 738 - 739 - spin_unlock_irqrestore(&buf_list->spinlock, flags); 740 751 } /* end of n_hdlc_buf_put() */ 741 752 742 753 /** ··· 745 764 */ 746 765 static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *buf_list) 747 766 { 748 - unsigned long flags; 749 767 struct n_hdlc_buf *buf; 750 768 751 - spin_lock_irqsave(&buf_list->spinlock, flags); 769 + guard(spinlock_irqsave)(&buf_list->spinlock); 752 770 753 771 buf = list_first_entry_or_null(&buf_list->list, 754 772 struct n_hdlc_buf, list_item); ··· 756 776 buf_list->count--; 757 777 } 758 778 759 - spin_unlock_irqrestore(&buf_list->spinlock, flags); 760 779 return buf; 761 780 } /* end of n_hdlc_buf_get() */ 762 781
+47 -60
drivers/tty/n_tty.c
··· 324 324 325 325 static void n_tty_packet_mode_flush(struct tty_struct *tty) 326 326 { 327 - unsigned long flags; 328 - 329 327 if (tty->link->ctrl.packet) { 330 - spin_lock_irqsave(&tty->ctrl.lock, flags); 331 - tty->ctrl.pktstatus |= TIOCPKT_FLUSHREAD; 332 - spin_unlock_irqrestore(&tty->ctrl.lock, flags); 328 + scoped_guard(spinlock_irqsave, &tty->ctrl.lock) 329 + tty->ctrl.pktstatus |= TIOCPKT_FLUSHREAD; 333 330 wake_up_interruptible(&tty->link->read_wait); 334 331 } 335 332 } ··· 346 349 */ 347 350 static void n_tty_flush_buffer(struct tty_struct *tty) 348 351 { 349 - down_write(&tty->termios_rwsem); 352 + guard(rwsem_write)(&tty->termios_rwsem); 350 353 reset_buffer_flags(tty->disc_data); 351 354 n_tty_kick_worker(tty); 352 355 353 356 if (tty->link) 354 357 n_tty_packet_mode_flush(tty); 355 - up_write(&tty->termios_rwsem); 356 358 } 357 359 358 360 /** ··· 733 737 size_t nr, old, echoed; 734 738 size_t head; 735 739 736 - mutex_lock(&ldata->output_lock); 737 - head = ldata->echo_head; 738 - ldata->echo_mark = head; 739 - old = ldata->echo_commit - ldata->echo_tail; 740 + scoped_guard(mutex, &ldata->output_lock) { 741 + head = ldata->echo_head; 742 + ldata->echo_mark = head; 743 + old = ldata->echo_commit - ldata->echo_tail; 740 744 741 - /* Process committed echoes if the accumulated # of bytes 742 - * is over the threshold (and try again each time another 743 - * block is accumulated) */ 744 - nr = head - ldata->echo_tail; 745 - if (nr < ECHO_COMMIT_WATERMARK || 746 - (nr % ECHO_BLOCK > old % ECHO_BLOCK)) { 747 - mutex_unlock(&ldata->output_lock); 748 - return; 745 + /* 746 + * Process committed echoes if the accumulated # of bytes is over the threshold 747 + * (and try again each time another block is accumulated) 748 + */ 749 + nr = head - ldata->echo_tail; 750 + if (nr < ECHO_COMMIT_WATERMARK || (nr % ECHO_BLOCK > old % ECHO_BLOCK)) 751 + return; 752 + 753 + ldata->echo_commit = head; 754 + echoed = __process_echoes(tty); 749 755 } 750 - 751 - ldata->echo_commit = head; 752 - echoed = __process_echoes(tty); 753 - mutex_unlock(&ldata->output_lock); 754 756 755 757 if (echoed && tty->ops->flush_chars) 756 758 tty->ops->flush_chars(tty); ··· 762 768 if (ldata->echo_mark == ldata->echo_tail) 763 769 return; 764 770 765 - mutex_lock(&ldata->output_lock); 766 - ldata->echo_commit = ldata->echo_mark; 767 - echoed = __process_echoes(tty); 768 - mutex_unlock(&ldata->output_lock); 771 + scoped_guard(mutex, &ldata->output_lock) { 772 + ldata->echo_commit = ldata->echo_mark; 773 + echoed = __process_echoes(tty); 774 + } 769 775 770 776 if (echoed && tty->ops->flush_chars) 771 777 tty->ops->flush_chars(tty); ··· 780 786 ldata->echo_commit == ldata->echo_head) 781 787 return; 782 788 783 - mutex_lock(&ldata->output_lock); 789 + guard(mutex)(&ldata->output_lock); 784 790 ldata->echo_commit = ldata->echo_head; 785 791 __process_echoes(tty); 786 - mutex_unlock(&ldata->output_lock); 787 792 } 788 793 789 794 /** ··· 1071 1078 if (L_NOFLSH(tty)) { 1072 1079 /* signal only */ 1073 1080 __isig(sig, tty); 1081 + return; 1082 + } 1074 1083 1075 - } else { /* signal and flush */ 1076 - up_read(&tty->termios_rwsem); 1077 - down_write(&tty->termios_rwsem); 1078 - 1084 + /* signal and flush */ 1085 + up_read(&tty->termios_rwsem); 1086 + scoped_guard(rwsem_write, &tty->termios_rwsem) { 1079 1087 __isig(sig, tty); 1080 1088 1081 1089 /* clear echo buffer */ 1082 - mutex_lock(&ldata->output_lock); 1083 - ldata->echo_head = ldata->echo_tail = 0; 1084 - ldata->echo_mark = ldata->echo_commit = 0; 1085 - mutex_unlock(&ldata->output_lock); 1090 + scoped_guard(mutex, &ldata->output_lock) { 1091 + ldata->echo_head = ldata->echo_tail = 0; 1092 + ldata->echo_mark = ldata->echo_commit = 0; 1093 + } 1086 1094 1087 1095 /* clear output buffer */ 1088 1096 tty_driver_flush_buffer(tty); ··· 1094 1100 /* notify pty master of flush */ 1095 1101 if (tty->link) 1096 1102 n_tty_packet_mode_flush(tty); 1097 - 1098 - up_write(&tty->termios_rwsem); 1099 - down_read(&tty->termios_rwsem); 1100 1103 } 1104 + down_read(&tty->termios_rwsem); 1101 1105 } 1102 1106 1103 1107 /** ··· 1675 1683 size_t n, rcvd = 0; 1676 1684 int room, overflow; 1677 1685 1678 - down_read(&tty->termios_rwsem); 1686 + guard(rwsem_read)(&tty->termios_rwsem); 1679 1687 1680 1688 do { 1681 1689 /* ··· 1743 1751 if (!chars_in_buffer(tty)) 1744 1752 n_tty_kick_worker(tty); 1745 1753 } 1746 - 1747 - up_read(&tty->termios_rwsem); 1748 1754 1749 1755 return rcvd; 1750 1756 } ··· 1869 1879 if (tty->link) 1870 1880 n_tty_packet_mode_flush(tty); 1871 1881 1872 - down_write(&tty->termios_rwsem); 1882 + guard(rwsem_write)(&tty->termios_rwsem); 1873 1883 vfree(ldata); 1874 1884 tty->disc_data = NULL; 1875 - up_write(&tty->termios_rwsem); 1876 1885 } 1877 1886 1878 1887 /** ··· 2236 2247 u8 cs; 2237 2248 if (kb != kbuf) 2238 2249 break; 2239 - spin_lock_irq(&tty->link->ctrl.lock); 2240 - cs = tty->link->ctrl.pktstatus; 2241 - tty->link->ctrl.pktstatus = 0; 2242 - spin_unlock_irq(&tty->link->ctrl.lock); 2250 + scoped_guard(spinlock_irq, &tty->link->ctrl.lock) { 2251 + cs = tty->link->ctrl.pktstatus; 2252 + tty->link->ctrl.pktstatus = 0; 2253 + } 2243 2254 *kb++ = cs; 2244 2255 nr--; 2245 2256 break; ··· 2346 2357 return retval; 2347 2358 } 2348 2359 2349 - down_read(&tty->termios_rwsem); 2360 + guard(rwsem_read)(&tty->termios_rwsem); 2350 2361 2351 2362 /* Write out any echoed characters that are still pending */ 2352 2363 process_echoes(tty); ··· 2384 2395 struct n_tty_data *ldata = tty->disc_data; 2385 2396 2386 2397 while (nr > 0) { 2387 - mutex_lock(&ldata->output_lock); 2388 - num = tty->ops->write(tty, b, nr); 2389 - mutex_unlock(&ldata->output_lock); 2398 + scoped_guard(mutex, &ldata->output_lock) 2399 + num = tty->ops->write(tty, b, nr); 2390 2400 if (num < 0) { 2391 2401 retval = num; 2392 2402 goto break_out; ··· 2412 2424 remove_wait_queue(&tty->write_wait, &wait); 2413 2425 if (nr && tty->fasync) 2414 2426 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); 2415 - up_read(&tty->termios_rwsem); 2427 + 2416 2428 return (b - buf) ? b - buf : retval; 2417 2429 } 2418 2430 ··· 2486 2498 case TIOCOUTQ: 2487 2499 return put_user(tty_chars_in_buffer(tty), (int __user *) arg); 2488 2500 case TIOCINQ: 2489 - down_write(&tty->termios_rwsem); 2490 - if (L_ICANON(tty) && !L_EXTPROC(tty)) 2491 - num = inq_canon(ldata); 2492 - else 2493 - num = read_cnt(ldata); 2494 - up_write(&tty->termios_rwsem); 2501 + scoped_guard(rwsem_write, &tty->termios_rwsem) 2502 + if (L_ICANON(tty) && !L_EXTPROC(tty)) 2503 + num = inq_canon(ldata); 2504 + else 2505 + num = read_cnt(ldata); 2495 2506 return put_user(num, (unsigned int __user *) arg); 2496 2507 default: 2497 2508 return n_tty_ioctl_helper(tty, cmd, arg);
+45 -58
drivers/tty/pty.c
··· 57 57 set_bit(TTY_IO_ERROR, &tty->flags); 58 58 wake_up_interruptible(&tty->read_wait); 59 59 wake_up_interruptible(&tty->write_wait); 60 - spin_lock_irq(&tty->ctrl.lock); 61 - tty->ctrl.packet = false; 62 - spin_unlock_irq(&tty->ctrl.lock); 60 + scoped_guard(spinlock_irq, &tty->ctrl.lock) 61 + tty->ctrl.packet = false; 63 62 /* Review - krefs on tty_link ?? */ 64 63 if (!tty->link) 65 64 return; ··· 69 70 set_bit(TTY_OTHER_CLOSED, &tty->flags); 70 71 #ifdef CONFIG_UNIX98_PTYS 71 72 if (tty->driver == ptm_driver) { 72 - mutex_lock(&devpts_mutex); 73 + guard(mutex)(&devpts_mutex); 73 74 if (tty->link->driver_data) 74 75 devpts_pty_kill(tty->link->driver_data); 75 - mutex_unlock(&devpts_mutex); 76 76 } 77 77 #endif 78 78 tty_vhangup(tty->link); ··· 155 157 /* Set the packet mode on a pty */ 156 158 static int pty_set_pktmode(struct tty_struct *tty, int __user *arg) 157 159 { 158 - int pktmode; 160 + int want_pktmode; 159 161 160 - if (get_user(pktmode, arg)) 162 + if (get_user(want_pktmode, arg)) 161 163 return -EFAULT; 162 164 163 - spin_lock_irq(&tty->ctrl.lock); 164 - if (pktmode) { 165 - if (!tty->ctrl.packet) { 166 - tty->link->ctrl.pktstatus = 0; 167 - smp_mb(); 168 - tty->ctrl.packet = true; 169 - } 170 - } else 165 + guard(spinlock_irq)(&tty->ctrl.lock); 166 + if (!want_pktmode) { 171 167 tty->ctrl.packet = false; 172 - spin_unlock_irq(&tty->ctrl.lock); 168 + return 0; 169 + } 170 + 171 + if (tty->ctrl.packet) 172 + return 0; 173 + 174 + tty->link->ctrl.pktstatus = 0; 175 + smp_mb(); 176 + tty->ctrl.packet = true; 173 177 174 178 return 0; 175 179 } ··· 210 210 211 211 tty_buffer_flush(to, NULL); 212 212 if (to->ctrl.packet) { 213 - spin_lock_irq(&tty->ctrl.lock); 213 + guard(spinlock_irq)(&tty->ctrl.lock); 214 214 tty->ctrl.pktstatus |= TIOCPKT_FLUSHWRITE; 215 215 wake_up_interruptible(&to->read_wait); 216 - spin_unlock_irq(&tty->ctrl.lock); 217 216 } 218 217 } 219 218 ··· 251 252 STOP_CHAR(tty) == '\023' && 252 253 START_CHAR(tty) == '\021'); 253 254 if ((old_flow != new_flow) || extproc) { 254 - spin_lock_irq(&tty->ctrl.lock); 255 - if (old_flow != new_flow) { 256 - tty->ctrl.pktstatus &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP); 257 - if (new_flow) 258 - tty->ctrl.pktstatus |= TIOCPKT_DOSTOP; 259 - else 260 - tty->ctrl.pktstatus |= TIOCPKT_NOSTOP; 255 + scoped_guard(spinlock_irq, &tty->ctrl.lock) { 256 + if (old_flow != new_flow) { 257 + tty->ctrl.pktstatus &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP); 258 + if (new_flow) 259 + tty->ctrl.pktstatus |= TIOCPKT_DOSTOP; 260 + else 261 + tty->ctrl.pktstatus |= TIOCPKT_NOSTOP; 262 + } 263 + if (extproc) 264 + tty->ctrl.pktstatus |= TIOCPKT_IOCTL; 261 265 } 262 - if (extproc) 263 - tty->ctrl.pktstatus |= TIOCPKT_IOCTL; 264 - spin_unlock_irq(&tty->ctrl.lock); 265 266 wake_up_interruptible(&tty->link->read_wait); 266 267 } 267 268 } ··· 285 286 struct tty_struct *pty = tty->link; 286 287 287 288 /* For a PTY we need to lock the tty side */ 288 - mutex_lock(&tty->winsize_mutex); 289 + guard(mutex)(&tty->winsize_mutex); 289 290 if (!memcmp(ws, &tty->winsize, sizeof(*ws))) 290 - goto done; 291 + return 0; 291 292 292 293 /* Signal the foreground process group of both ptys */ 293 294 pgrp = tty_get_pgrp(tty); ··· 303 304 304 305 tty->winsize = *ws; 305 306 pty->winsize = *ws; /* Never used so will go away soon */ 306 - done: 307 - mutex_unlock(&tty->winsize_mutex); 307 + 308 308 return 0; 309 309 } 310 310 ··· 319 321 */ 320 322 static void pty_start(struct tty_struct *tty) 321 323 { 322 - unsigned long flags; 324 + if (!tty->link || !tty->link->ctrl.packet) 325 + return; 323 326 324 - if (tty->link && tty->link->ctrl.packet) { 325 - spin_lock_irqsave(&tty->ctrl.lock, flags); 327 + scoped_guard(spinlock_irqsave, &tty->ctrl.lock) { 326 328 tty->ctrl.pktstatus &= ~TIOCPKT_STOP; 327 329 tty->ctrl.pktstatus |= TIOCPKT_START; 328 - spin_unlock_irqrestore(&tty->ctrl.lock, flags); 329 - wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN); 330 330 } 331 + wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN); 331 332 } 332 333 333 334 static void pty_stop(struct tty_struct *tty) 334 335 { 335 - unsigned long flags; 336 + if (!tty->link || !tty->link->ctrl.packet) 337 + return; 336 338 337 - if (tty->link && tty->link->ctrl.packet) { 338 - spin_lock_irqsave(&tty->ctrl.lock, flags); 339 + scoped_guard(spinlock_irqsave, &tty->ctrl.lock) { 339 340 tty->ctrl.pktstatus &= ~TIOCPKT_START; 340 341 tty->ctrl.pktstatus |= TIOCPKT_STOP; 341 - spin_unlock_irqrestore(&tty->ctrl.lock, flags); 342 - wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN); 343 342 } 343 + wake_up_interruptible_poll(&tty->link->read_wait, EPOLLIN); 344 344 } 345 345 346 346 /** ··· 686 690 static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver, 687 691 struct file *file, int idx) 688 692 { 689 - struct tty_struct *tty; 690 - 691 - mutex_lock(&devpts_mutex); 692 - tty = devpts_get_priv(file->f_path.dentry); 693 - mutex_unlock(&devpts_mutex); 693 + guard(mutex)(&devpts_mutex); 694 694 /* Master must be open before slave */ 695 - if (!tty) 696 - return ERR_PTR(-EIO); 697 - return tty; 695 + return devpts_get_priv(file->f_path.dentry) ? : ERR_PTR(-EIO); 698 696 } 699 697 700 698 static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty) ··· 786 796 } 787 797 788 798 /* find a device that is not in use. */ 789 - mutex_lock(&devpts_mutex); 790 - index = devpts_new_index(fsi); 791 - mutex_unlock(&devpts_mutex); 799 + scoped_guard(mutex, &devpts_mutex) 800 + index = devpts_new_index(fsi); 792 801 793 802 retval = index; 794 803 if (index < 0) 795 804 goto out_put_fsi; 796 805 797 806 798 - mutex_lock(&tty_mutex); 799 - tty = tty_init_dev(ptm_driver, index); 800 - /* The tty returned here is locked so we can safely 801 - drop the mutex */ 802 - mutex_unlock(&tty_mutex); 807 + /* The tty returned here is locked so we can safely drop the mutex */ 808 + scoped_guard(mutex, &tty_mutex) 809 + tty = tty_init_dev(ptm_driver, index); 803 810 804 811 retval = PTR_ERR(tty); 805 812 if (IS_ERR(tty))
-9
drivers/tty/serial/8250/8250.h
··· 98 98 99 99 extern unsigned int nr_uarts; 100 100 101 - #ifdef CONFIG_SERIAL_8250_SHARE_IRQ 102 - #define SERIAL8250_SHARE_IRQS 1 103 - #else 104 - #define SERIAL8250_SHARE_IRQS 0 105 - #endif 106 - 107 - extern unsigned int share_irqs; 108 - extern unsigned int skip_txen_test; 109 - 110 101 #define SERIAL8250_PORT_FLAGS(_base, _irq, _flags) \ 111 102 { \ 112 103 .iobase = _base, \
+4
drivers/tty/serial/8250/8250_core.c
··· 52 52 static DEFINE_HASHTABLE(irq_lists, IRQ_HASH_BITS); 53 53 static DEFINE_MUTEX(hash_mutex); /* Used to walk the hash */ 54 54 55 + static bool skip_txen_test; 56 + module_param(skip_txen_test, bool, 0644); 57 + MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time"); 58 + 55 59 /* 56 60 * This is the serial driver's interrupt routine. 57 61 *
+2 -2
drivers/tty/serial/8250/8250_dw.c
··· 361 361 * deferred event handling complication. 362 362 */ 363 363 if (event == POST_RATE_CHANGE) { 364 - queue_work(system_unbound_wq, &d->clk_work); 364 + queue_work(system_dfl_wq, &d->clk_work); 365 365 return NOTIFY_OK; 366 366 } 367 367 ··· 680 680 err = clk_notifier_register(data->clk, &data->clk_notifier); 681 681 if (err) 682 682 return dev_err_probe(dev, err, "Failed to set the clock notifier\n"); 683 - queue_work(system_unbound_wq, &data->clk_work); 683 + queue_work(system_dfl_wq, &data->clk_work); 684 684 } 685 685 686 686 platform_set_drvdata(pdev, data);
+2 -2
drivers/tty/serial/8250/8250_exar.c
··· 505 505 unsigned char status; 506 506 int err; 507 507 508 - err = serial8250_pci_setup_port(pcidev, port, 0, offset, board->reg_shift); 508 + err = serial8250_pci_setup_port(pcidev, port, 0, offset, board->reg_shift, priv->virt); 509 509 if (err) 510 510 return err; 511 511 ··· 833 833 port->port.port_id = idx; 834 834 port->port.uartclk = priv->osc_freq; 835 835 836 - ret = serial8250_pci_setup_port(pcidev, port, 0, offset, 0); 836 + ret = serial8250_pci_setup_port(pcidev, port, 0, offset, 0, priv->virt); 837 837 if (ret) 838 838 return ret; 839 839
+280
drivers/tty/serial/8250/8250_keba.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Copyright (C) 2025 KEBA Industrial Automation GmbH 4 + * 5 + * Driver for KEBA UART FPGA IP core 6 + */ 7 + 8 + #include <linux/auxiliary_bus.h> 9 + #include <linux/device.h> 10 + #include <linux/io.h> 11 + #include <linux/misc/keba.h> 12 + #include <linux/module.h> 13 + 14 + #include "8250.h" 15 + 16 + #define KUART "kuart" 17 + 18 + /* flags */ 19 + #define KUART_RS485 BIT(0) 20 + #define KUART_USE_CAPABILITY BIT(1) 21 + 22 + /* registers */ 23 + #define KUART_VERSION 0x0000 24 + #define KUART_REVISION 0x0001 25 + #define KUART_CAPABILITY 0x0002 26 + #define KUART_CONTROL 0x0004 27 + #define KUART_BASE 0x000C 28 + #define KUART_REGSHIFT 2 29 + #define KUART_CLK 1843200 30 + 31 + /* mode flags */ 32 + enum kuart_mode { 33 + KUART_MODE_NONE = 0, 34 + KUART_MODE_RS485, 35 + KUART_MODE_RS422, 36 + KUART_MODE_RS232 37 + }; 38 + 39 + /* capability flags */ 40 + #define KUART_CAPABILITY_NONE BIT(KUART_MODE_NONE) 41 + #define KUART_CAPABILITY_RS485 BIT(KUART_MODE_RS485) 42 + #define KUART_CAPABILITY_RS422 BIT(KUART_MODE_RS422) 43 + #define KUART_CAPABILITY_RS232 BIT(KUART_MODE_RS232) 44 + #define KUART_CAPABILITY_MASK GENMASK(3, 0) 45 + 46 + /* Additional Control Register DTR line configuration */ 47 + #define UART_ACR_DTRLC_MASK 0x18 48 + #define UART_ACR_DTRLC_COMPAT 0x00 49 + #define UART_ACR_DTRLC_ENABLE_LOW 0x10 50 + 51 + struct kuart { 52 + struct keba_uart_auxdev *auxdev; 53 + void __iomem *base; 54 + unsigned int line; 55 + 56 + unsigned int flags; 57 + u8 capability; 58 + enum kuart_mode mode; 59 + }; 60 + 61 + static void kuart_set_phy_mode(struct kuart *kuart, enum kuart_mode mode) 62 + { 63 + iowrite8(mode, kuart->base + KUART_CONTROL); 64 + } 65 + 66 + static void kuart_enhanced_mode(struct uart_8250_port *up, bool enable) 67 + { 68 + u8 lcr, efr; 69 + 70 + /* backup LCR register */ 71 + lcr = serial_in(up, UART_LCR); 72 + 73 + /* enable 650 compatible register set (EFR, ...) */ 74 + serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B); 75 + 76 + /* enable/disable enhanced mode with indexed control registers */ 77 + efr = serial_in(up, UART_EFR); 78 + if (enable) 79 + efr |= UART_EFR_ECB; 80 + else 81 + efr &= ~UART_EFR_ECB; 82 + serial_out(up, UART_EFR, efr); 83 + 84 + /* disable 650 compatible register set, restore LCR */ 85 + serial_out(up, UART_LCR, lcr); 86 + } 87 + 88 + static void kuart_dtr_line_config(struct uart_8250_port *up, u8 dtrlc) 89 + { 90 + u8 acr; 91 + 92 + /* set index register to 0 to access ACR register */ 93 + serial_out(up, UART_SCR, UART_ACR); 94 + 95 + /* set value register to 0x10 writing DTR mode (1,0) */ 96 + acr = serial_in(up, UART_LSR); 97 + acr &= ~UART_ACR_DTRLC_MASK; 98 + acr |= dtrlc; 99 + serial_out(up, UART_LSR, acr); 100 + } 101 + 102 + static int kuart_rs485_config(struct uart_port *port, struct ktermios *termios, 103 + struct serial_rs485 *rs485) 104 + { 105 + struct uart_8250_port *up = up_to_u8250p(port); 106 + struct kuart *kuart = port->private_data; 107 + enum kuart_mode mode; 108 + u8 dtrlc; 109 + 110 + if (rs485->flags & SER_RS485_ENABLED) { 111 + if (rs485->flags & SER_RS485_MODE_RS422) 112 + mode = KUART_MODE_RS422; 113 + else 114 + mode = KUART_MODE_RS485; 115 + } else { 116 + mode = KUART_MODE_RS232; 117 + } 118 + 119 + if (mode == kuart->mode) 120 + return 0; 121 + 122 + if (kuart->flags & KUART_USE_CAPABILITY) { 123 + /* deactivate physical interface, break before make */ 124 + kuart_set_phy_mode(kuart, KUART_MODE_NONE); 125 + } 126 + 127 + if (mode == KUART_MODE_RS485) { 128 + /* 129 + * Set DTR line configuration of 95x UART to DTR mode (1,0). 130 + * In this mode the DTR pin drives the active-low enable pin of 131 + * an external RS485 buffer. The DTR pin will be forced low 132 + * whenever the transmitter is not empty, otherwise DTR pin is 133 + * high. 134 + */ 135 + dtrlc = UART_ACR_DTRLC_ENABLE_LOW; 136 + } else { 137 + /* 138 + * Set DTR line configuration of 95x UART to DTR mode (0,0). 139 + * In this mode the DTR pin is compatible with 16C450, 16C550, 140 + * 16C650 and 16c670 (i.e. normal). 141 + */ 142 + dtrlc = UART_ACR_DTRLC_COMPAT; 143 + } 144 + 145 + kuart_enhanced_mode(up, true); 146 + kuart_dtr_line_config(up, dtrlc); 147 + kuart_enhanced_mode(up, false); 148 + 149 + if (kuart->flags & KUART_USE_CAPABILITY) { 150 + /* activate selected physical interface */ 151 + kuart_set_phy_mode(kuart, mode); 152 + } 153 + 154 + kuart->mode = mode; 155 + 156 + return 0; 157 + } 158 + 159 + static int kuart_probe(struct auxiliary_device *auxdev, 160 + const struct auxiliary_device_id *id) 161 + { 162 + struct device *dev = &auxdev->dev; 163 + struct uart_8250_port uart = {}; 164 + struct resource res; 165 + struct kuart *kuart; 166 + int retval; 167 + 168 + kuart = devm_kzalloc(dev, sizeof(*kuart), GFP_KERNEL); 169 + if (!kuart) 170 + return -ENOMEM; 171 + kuart->auxdev = container_of(auxdev, struct keba_uart_auxdev, auxdev); 172 + kuart->flags = id->driver_data; 173 + auxiliary_set_drvdata(auxdev, kuart); 174 + 175 + /* 176 + * map only memory in front of UART registers, UART registers will be 177 + * mapped by serial port 178 + */ 179 + res = kuart->auxdev->io; 180 + res.end = res.start + KUART_BASE - 1; 181 + kuart->base = devm_ioremap_resource(dev, &res); 182 + if (IS_ERR(kuart->base)) 183 + return PTR_ERR(kuart->base); 184 + 185 + if (kuart->flags & KUART_USE_CAPABILITY) { 186 + /* 187 + * supported modes are read from capability register, at least 188 + * one mode other than none must be supported 189 + */ 190 + kuart->capability = ioread8(kuart->base + KUART_CAPABILITY) & 191 + KUART_CAPABILITY_MASK; 192 + if ((kuart->capability & ~KUART_CAPABILITY_NONE) == 0) 193 + return -EIO; 194 + } 195 + 196 + spin_lock_init(&uart.port.lock); 197 + uart.port.dev = dev; 198 + uart.port.mapbase = kuart->auxdev->io.start + KUART_BASE; 199 + uart.port.irq = kuart->auxdev->irq; 200 + uart.port.uartclk = KUART_CLK; 201 + uart.port.private_data = kuart; 202 + 203 + /* 8 bit registers are 32 bit aligned => shift register offset */ 204 + uart.port.iotype = UPIO_MEM32; 205 + uart.port.regshift = KUART_REGSHIFT; 206 + 207 + /* 208 + * UART mixes 16550, 16750 and 16C950 (for RS485) standard => auto 209 + * configuration works best 210 + */ 211 + uart.port.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF | UPF_IOREMAP; 212 + 213 + /* 214 + * UART supports RS485, RS422 and RS232 with switching of physical 215 + * interface 216 + */ 217 + uart.port.rs485_config = kuart_rs485_config; 218 + if (kuart->flags & KUART_RS485) { 219 + uart.port.rs485_supported.flags = SER_RS485_ENABLED | 220 + SER_RS485_RTS_ON_SEND; 221 + uart.port.rs485.flags = SER_RS485_ENABLED | 222 + SER_RS485_RTS_ON_SEND; 223 + } 224 + if (kuart->flags & KUART_USE_CAPABILITY) { 225 + /* default mode priority is RS485 > RS422 > RS232 */ 226 + if (kuart->capability & KUART_CAPABILITY_RS422) { 227 + uart.port.rs485_supported.flags |= SER_RS485_ENABLED | 228 + SER_RS485_RTS_ON_SEND | 229 + SER_RS485_MODE_RS422; 230 + uart.port.rs485.flags = SER_RS485_ENABLED | 231 + SER_RS485_RTS_ON_SEND | 232 + SER_RS485_MODE_RS422; 233 + } 234 + if (kuart->capability & KUART_CAPABILITY_RS485) { 235 + uart.port.rs485_supported.flags |= SER_RS485_ENABLED | 236 + SER_RS485_RTS_ON_SEND; 237 + uart.port.rs485.flags = SER_RS485_ENABLED | 238 + SER_RS485_RTS_ON_SEND; 239 + } 240 + } 241 + 242 + retval = serial8250_register_8250_port(&uart); 243 + if (retval < 0) { 244 + dev_err(&auxdev->dev, "UART registration failed!\n"); 245 + return retval; 246 + } 247 + kuart->line = retval; 248 + 249 + return 0; 250 + } 251 + 252 + static void kuart_remove(struct auxiliary_device *auxdev) 253 + { 254 + struct kuart *kuart = auxiliary_get_drvdata(auxdev); 255 + 256 + if (kuart->flags & KUART_USE_CAPABILITY) 257 + kuart_set_phy_mode(kuart, KUART_MODE_NONE); 258 + 259 + serial8250_unregister_port(kuart->line); 260 + } 261 + 262 + static const struct auxiliary_device_id kuart_devtype_aux[] = { 263 + { .name = "keba.rs485-uart", .driver_data = KUART_RS485 }, 264 + { .name = "keba.rs232-uart", .driver_data = 0 }, 265 + { .name = "keba.uart", .driver_data = KUART_USE_CAPABILITY }, 266 + {} 267 + }; 268 + MODULE_DEVICE_TABLE(auxiliary, kuart_devtype_aux); 269 + 270 + static struct auxiliary_driver kuart_driver_aux = { 271 + .name = KUART, 272 + .id_table = kuart_devtype_aux, 273 + .probe = kuart_probe, 274 + .remove = kuart_remove, 275 + }; 276 + module_auxiliary_driver(kuart_driver_aux); 277 + 278 + MODULE_AUTHOR("Gerhard Engleder <eg@keba.com>"); 279 + MODULE_DESCRIPTION("KEBA 8250 serial port driver"); 280 + MODULE_LICENSE("GPL");
+238
drivers/tty/serial/8250/8250_loongson.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Serial Port driver for Loongson family chips 4 + * 5 + * Copyright (C) 2020-2025 Loongson Technology Corporation Limited 6 + */ 7 + 8 + #include <linux/bitfield.h> 9 + #include <linux/bits.h> 10 + #include <linux/clk.h> 11 + #include <linux/console.h> 12 + #include <linux/module.h> 13 + #include <linux/io.h> 14 + #include <linux/property.h> 15 + #include <linux/math.h> 16 + #include <linux/mod_devicetable.h> 17 + #include <linux/pm.h> 18 + #include <linux/reset.h> 19 + 20 + #include "8250.h" 21 + 22 + /* Divisor Latch Fraction Register */ 23 + #define LOONGSON_UART_DLF 0x2 24 + 25 + #define LOONGSON_QUOT_FRAC_MASK GENMASK(7, 0) 26 + #define LOONGSON_QUOT_DIV_MASK GENMASK(15, 8) 27 + 28 + struct loongson_uart_ddata { 29 + bool has_frac; 30 + u8 mcr_invert; 31 + u8 msr_invert; 32 + }; 33 + 34 + static const struct loongson_uart_ddata ls2k0500_uart_data = { 35 + .has_frac = false, 36 + .mcr_invert = UART_MCR_RTS | UART_MCR_DTR, 37 + .msr_invert = UART_MSR_CTS | UART_MSR_DSR, 38 + }; 39 + 40 + static const struct loongson_uart_ddata ls2k1500_uart_data = { 41 + .has_frac = true, 42 + .mcr_invert = UART_MCR_RTS | UART_MCR_DTR, 43 + .msr_invert = 0, 44 + }; 45 + 46 + struct loongson_uart_priv { 47 + int line; 48 + struct clk *clk; 49 + struct resource *res; 50 + struct reset_control *rst; 51 + const struct loongson_uart_ddata *ddata; 52 + }; 53 + 54 + static u8 serial_fixup(struct uart_port *p, unsigned int offset, u8 val) 55 + { 56 + struct loongson_uart_priv *priv = p->private_data; 57 + 58 + switch (offset) { 59 + case UART_MCR: 60 + return val ^ priv->ddata->mcr_invert; 61 + case UART_MSR: 62 + return val ^ priv->ddata->msr_invert; 63 + default: 64 + return val; 65 + } 66 + } 67 + 68 + static u32 loongson_serial_in(struct uart_port *p, unsigned int offset) 69 + { 70 + u8 val; 71 + 72 + val = readb(p->membase + (offset << p->regshift)); 73 + 74 + return serial_fixup(p, offset, val); 75 + } 76 + 77 + static void loongson_serial_out(struct uart_port *p, unsigned int offset, unsigned int value) 78 + { 79 + u8 val; 80 + 81 + offset <<= p->regshift; 82 + val = serial_fixup(p, offset, value); 83 + writeb(val, p->membase + offset); 84 + } 85 + 86 + static unsigned int loongson_frac_get_divisor(struct uart_port *port, unsigned int baud, 87 + unsigned int *frac) 88 + { 89 + unsigned int quot; 90 + 91 + quot = DIV_ROUND_CLOSEST((port->uartclk << 4), baud); 92 + *frac = FIELD_GET(LOONGSON_QUOT_FRAC_MASK, quot); 93 + 94 + return FIELD_GET(LOONGSON_QUOT_DIV_MASK, quot); 95 + } 96 + 97 + static void loongson_frac_set_divisor(struct uart_port *port, unsigned int baud, 98 + unsigned int quot, unsigned int quot_frac) 99 + { 100 + struct uart_8250_port *up = up_to_u8250p(port); 101 + 102 + serial_port_out(port, UART_LCR, up->lcr | UART_LCR_DLAB); 103 + serial_dl_write(up, quot); 104 + serial_port_out(port, LOONGSON_UART_DLF, quot_frac); 105 + } 106 + 107 + static int loongson_uart_probe(struct platform_device *pdev) 108 + { 109 + struct device *dev = &pdev->dev; 110 + struct uart_8250_port uart = {}; 111 + struct loongson_uart_priv *priv; 112 + struct uart_port *port; 113 + int ret; 114 + 115 + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 116 + if (!priv) 117 + return -ENOMEM; 118 + 119 + priv->ddata = device_get_match_data(dev); 120 + 121 + port = &uart.port; 122 + spin_lock_init(&port->lock); 123 + port->flags = UPF_SHARE_IRQ | UPF_FIXED_PORT | UPF_FIXED_TYPE | UPF_IOREMAP; 124 + port->iotype = UPIO_MEM; 125 + port->regshift = 0; 126 + port->dev = dev; 127 + port->type = PORT_16550A; 128 + port->private_data = priv; 129 + 130 + port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, &priv->res); 131 + if (!port->membase) 132 + return -ENOMEM; 133 + 134 + port->mapbase = priv->res->start; 135 + port->mapsize = resource_size(priv->res); 136 + port->serial_in = loongson_serial_in; 137 + port->serial_out = loongson_serial_out; 138 + 139 + if (priv->ddata->has_frac) { 140 + port->get_divisor = loongson_frac_get_divisor; 141 + port->set_divisor = loongson_frac_set_divisor; 142 + } 143 + 144 + ret = uart_read_port_properties(port); 145 + if (ret) 146 + return ret; 147 + 148 + if (!port->uartclk) { 149 + priv->clk = devm_clk_get_enabled(dev, NULL); 150 + if (IS_ERR(priv->clk)) 151 + return dev_err_probe(dev, PTR_ERR(priv->clk), 152 + "Unable to determine clock frequency!\n"); 153 + port->uartclk = clk_get_rate(priv->clk); 154 + } 155 + 156 + priv->rst = devm_reset_control_get_optional_shared(dev, NULL); 157 + if (IS_ERR(priv->rst)) 158 + return PTR_ERR(priv->rst); 159 + 160 + ret = reset_control_deassert(priv->rst); 161 + if (ret) 162 + return ret; 163 + 164 + ret = serial8250_register_8250_port(&uart); 165 + if (ret < 0) { 166 + reset_control_assert(priv->rst); 167 + return ret; 168 + } 169 + 170 + priv->line = ret; 171 + platform_set_drvdata(pdev, priv); 172 + 173 + return 0; 174 + } 175 + 176 + static void loongson_uart_remove(struct platform_device *pdev) 177 + { 178 + struct loongson_uart_priv *priv = platform_get_drvdata(pdev); 179 + 180 + serial8250_unregister_port(priv->line); 181 + reset_control_assert(priv->rst); 182 + } 183 + 184 + static int loongson_uart_suspend(struct device *dev) 185 + { 186 + struct loongson_uart_priv *priv = dev_get_drvdata(dev); 187 + struct uart_8250_port *up = serial8250_get_port(priv->line); 188 + 189 + serial8250_suspend_port(priv->line); 190 + 191 + if (!uart_console(&up->port) || console_suspend_enabled) 192 + clk_disable_unprepare(priv->clk); 193 + 194 + return 0; 195 + } 196 + 197 + static int loongson_uart_resume(struct device *dev) 198 + { 199 + struct loongson_uart_priv *priv = dev_get_drvdata(dev); 200 + struct uart_8250_port *up = serial8250_get_port(priv->line); 201 + int ret; 202 + 203 + if (!uart_console(&up->port) || console_suspend_enabled) { 204 + ret = clk_prepare_enable(priv->clk); 205 + if (ret) 206 + return ret; 207 + } 208 + 209 + serial8250_resume_port(priv->line); 210 + 211 + return 0; 212 + } 213 + 214 + static DEFINE_SIMPLE_DEV_PM_OPS(loongson_uart_pm_ops, loongson_uart_suspend, 215 + loongson_uart_resume); 216 + 217 + static const struct of_device_id loongson_uart_of_ids[] = { 218 + { .compatible = "loongson,ls2k0500-uart", .data = &ls2k0500_uart_data }, 219 + { .compatible = "loongson,ls2k1500-uart", .data = &ls2k1500_uart_data }, 220 + { }, 221 + }; 222 + MODULE_DEVICE_TABLE(of, loongson_uart_of_ids); 223 + 224 + static struct platform_driver loongson_uart_driver = { 225 + .probe = loongson_uart_probe, 226 + .remove = loongson_uart_remove, 227 + .driver = { 228 + .name = "loongson-uart", 229 + .pm = pm_ptr(&loongson_uart_pm_ops), 230 + .of_match_table = loongson_uart_of_ids, 231 + }, 232 + }; 233 + 234 + module_platform_driver(loongson_uart_driver); 235 + 236 + MODULE_DESCRIPTION("Loongson UART driver"); 237 + MODULE_AUTHOR("Loongson Technology Corporation Limited."); 238 + MODULE_LICENSE("GPL");
+1 -1
drivers/tty/serial/8250/8250_of.c
··· 95 95 u32 spd; 96 96 int ret; 97 97 98 - memset(port, 0, sizeof *port); 98 + memset(port, 0, sizeof(*port)); 99 99 100 100 pm_runtime_enable(&ofdev->dev); 101 101 pm_runtime_get_sync(&ofdev->dev);
+46 -1
drivers/tty/serial/8250/8250_pci.c
··· 95 95 #define PCI_DEVICE_ID_MOXA_CP138E_A 0x1381 96 96 #define PCI_DEVICE_ID_MOXA_CP168EL_A 0x1683 97 97 98 + #define PCI_DEVICE_ID_ADDIDATA_CPCI7500 0x7003 99 + #define PCI_DEVICE_ID_ADDIDATA_CPCI7500_NG 0x7024 100 + #define PCI_DEVICE_ID_ADDIDATA_CPCI7420_NG 0x7025 101 + #define PCI_DEVICE_ID_ADDIDATA_CPCI7300_NG 0x7026 102 + 98 103 /* Unknown vendors/cards - this should not be in linux/pci_ids.h */ 99 104 #define PCI_SUBDEVICE_ID_UNKNOWN_0x1584 0x1584 100 105 #define PCI_SUBDEVICE_ID_UNKNOWN_0x1588 0x1588 ··· 170 165 setup_port(struct serial_private *priv, struct uart_8250_port *port, 171 166 u8 bar, unsigned int offset, int regshift) 172 167 { 173 - return serial8250_pci_setup_port(priv->dev, port, bar, offset, regshift); 168 + void __iomem *iomem = NULL; 169 + 170 + if (pci_resource_flags(priv->dev, bar) & IORESOURCE_MEM) { 171 + iomem = pcim_iomap(priv->dev, bar, 0); 172 + if (!iomem) 173 + return -ENOMEM; 174 + } 175 + 176 + return serial8250_pci_setup_port(priv->dev, port, bar, offset, regshift, iomem); 174 177 } 175 178 176 179 /* ··· 6008 5995 0, 6009 5996 0, 6010 5997 pbn_ADDIDATA_PCIe_8_3906250 }, 5998 + 5999 + { PCI_VENDOR_ID_ADDIDATA, 6000 + PCI_DEVICE_ID_ADDIDATA_CPCI7500, 6001 + PCI_ANY_ID, 6002 + PCI_ANY_ID, 6003 + 0, 6004 + 0, 6005 + pbn_b0_4_115200 }, 6006 + 6007 + { PCI_VENDOR_ID_ADDIDATA, 6008 + PCI_DEVICE_ID_ADDIDATA_CPCI7500_NG, 6009 + PCI_ANY_ID, 6010 + PCI_ANY_ID, 6011 + 0, 6012 + 0, 6013 + pbn_b0_4_115200 }, 6014 + 6015 + { PCI_VENDOR_ID_ADDIDATA, 6016 + PCI_DEVICE_ID_ADDIDATA_CPCI7420_NG, 6017 + PCI_ANY_ID, 6018 + PCI_ANY_ID, 6019 + 0, 6020 + 0, 6021 + pbn_b0_2_115200 }, 6022 + 6023 + { PCI_VENDOR_ID_ADDIDATA, 6024 + PCI_DEVICE_ID_ADDIDATA_CPCI7300_NG, 6025 + PCI_ANY_ID, 6026 + PCI_ANY_ID, 6027 + 0, 6028 + 0, 6029 + pbn_b0_1_115200 }, 6011 6030 6012 6031 { PCI_VENDOR_ID_NETMOS, PCI_DEVICE_ID_NETMOS_9835, 6013 6032 PCI_VENDOR_ID_IBM, 0x0299,
+5 -5
drivers/tty/serial/8250/8250_pci1xxxx.c
··· 671 671 } 672 672 673 673 static int pci1xxxx_setup(struct pci_dev *pdev, 674 - struct uart_8250_port *port, int port_idx, int rev) 674 + struct uart_8250_port *port, int port_idx, struct pci1xxxx_8250 *priv) 675 675 { 676 676 int ret; 677 677 ··· 698 698 * C0 and later revisions support Burst operation. 699 699 * RTS workaround in mctrl is applicable only to B0. 700 700 */ 701 - if (rev >= 0xC0) 701 + if (priv->dev_rev >= 0xC0) 702 702 port->port.handle_irq = pci1xxxx_handle_irq; 703 - else if (rev == 0xB0) 703 + else if (priv->dev_rev == 0xB0) 704 704 port->port.set_mctrl = pci1xxxx_set_mctrl; 705 705 706 - ret = serial8250_pci_setup_port(pdev, port, 0, PORT_OFFSET * port_idx, 0); 706 + ret = serial8250_pci_setup_port(pdev, port, 0, PORT_OFFSET * port_idx, 0, priv->membase); 707 707 if (ret < 0) 708 708 return ret; 709 709 ··· 821 821 else 822 822 uart.port.irq = pci_irq_vector(pdev, 0); 823 823 824 - rc = pci1xxxx_setup(pdev, &uart, port_idx, priv->dev_rev); 824 + rc = pci1xxxx_setup(pdev, &uart, port_idx, priv); 825 825 if (rc) { 826 826 dev_warn(dev, "Failed to setup port %u\n", i); 827 827 continue;
+2 -5
drivers/tty/serial/8250/8250_pcilib.c
··· 22 22 EXPORT_SYMBOL_NS_GPL(serial_8250_warn_need_ioport, "SERIAL_8250_PCI"); 23 23 24 24 int serial8250_pci_setup_port(struct pci_dev *dev, struct uart_8250_port *port, 25 - u8 bar, unsigned int offset, int regshift) 25 + u8 bar, unsigned int offset, int regshift, void __iomem *iomem) 26 26 { 27 27 if (bar >= PCI_STD_NUM_BARS) 28 28 return -EINVAL; 29 29 30 30 if (pci_resource_flags(dev, bar) & IORESOURCE_MEM) { 31 - if (!pcim_iomap(dev, bar, 0) && !pcim_iomap_table(dev)) 32 - return -ENOMEM; 33 - 34 31 port->port.iotype = UPIO_MEM; 35 32 port->port.iobase = 0; 36 33 port->port.mapbase = pci_resource_start(dev, bar) + offset; 37 - port->port.membase = pcim_iomap_table(dev)[bar] + offset; 34 + port->port.membase = iomem + offset; 38 35 port->port.regshift = regshift; 39 36 } else if (IS_ENABLED(CONFIG_HAS_IOPORT)) { 40 37 port->port.iotype = UPIO_PORT;
+1 -1
drivers/tty/serial/8250/8250_pcilib.h
··· 12 12 struct uart_8250_port; 13 13 14 14 int serial8250_pci_setup_port(struct pci_dev *dev, struct uart_8250_port *port, u8 bar, 15 - unsigned int offset, int regshift); 15 + unsigned int offset, int regshift, void __iomem *iomem); 16 16 17 17 int serial_8250_warn_need_ioport(struct pci_dev *dev);
+11 -44
drivers/tty/serial/8250/8250_platform.c
··· 29 29 * Configuration: 30 30 * share_irqs: Whether we pass IRQF_SHARED to request_irq(). 31 31 * This option is unsafe when used on edge-triggered interrupts. 32 - * skip_txen_test: Force skip of txen test at init time. 33 32 */ 34 - unsigned int share_irqs = SERIAL8250_SHARE_IRQS; 35 - unsigned int skip_txen_test; 33 + static bool share_irqs = IS_ENABLED(CONFIG_SERIAL_8250_SHARE_IRQ); 36 34 37 35 unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS; 38 36 ··· 58 60 59 61 static void __init __serial8250_isa_init_ports(void) 60 62 { 61 - int i, irqflag = 0; 63 + int i; 62 64 63 65 if (nr_uarts > UART_NR) 64 66 nr_uarts = UART_NR; ··· 75 77 univ8250_port_ops = *univ8250_port_base_ops; 76 78 univ8250_rsa_support(&univ8250_port_ops, univ8250_port_base_ops); 77 79 78 - if (share_irqs) 79 - irqflag = IRQF_SHARED; 80 - 81 80 for (i = 0; i < ARRAY_SIZE(old_serial_port) && i < nr_uarts; i++) { 82 81 struct uart_8250_port *up = serial8250_get_port(i); 83 82 struct uart_port *port = &up->port; ··· 89 94 port->iotype = old_serial_port[i].io_type; 90 95 port->regshift = old_serial_port[i].iomem_reg_shift; 91 96 92 - port->irqflags |= irqflag; 97 + if (share_irqs) 98 + port->irqflags |= IRQF_SHARED; 99 + 93 100 if (serial8250_isa_config != NULL) 94 101 serial8250_isa_config(i, &up->port, &up->capabilities); 95 102 } ··· 154 157 155 158 static int serial8250_probe_platform(struct platform_device *dev, struct plat_serial8250_port *p) 156 159 { 157 - int ret, i, irqflag = 0; 160 + int ret, i; 158 161 159 162 struct uart_8250_port *uart __free(kfree) = kzalloc(sizeof(*uart), GFP_KERNEL); 160 163 if (!uart) 161 164 return -ENOMEM; 162 - 163 - if (share_irqs) 164 - irqflag = IRQF_SHARED; 165 165 166 166 for (i = 0; p && p->flags != 0; p++, i++) { 167 167 uart->port.iobase = p->iobase; ··· 187 193 uart->port.get_mctrl = p->get_mctrl; 188 194 uart->port.pm = p->pm; 189 195 uart->port.dev = &dev->dev; 190 - uart->port.irqflags |= irqflag; 196 + 197 + if (share_irqs) 198 + uart->port.irqflags |= IRQF_SHARED; 199 + 191 200 ret = serial8250_register_8250_port(uart); 192 201 if (ret < 0) { 193 202 dev_err(&dev->dev, "unable to register port at index %d " ··· 377 380 MODULE_LICENSE("GPL"); 378 381 MODULE_DESCRIPTION("Generic 8250/16x50 serial platform driver"); 379 382 380 - module_param_hw(share_irqs, uint, other, 0644); 383 + module_param_hw(share_irqs, bool, other, 0644); 381 384 MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices (unsafe)"); 382 385 383 386 module_param(nr_uarts, uint, 0644); 384 387 MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")"); 385 388 386 - module_param(skip_txen_test, uint, 0644); 387 - MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time"); 388 - 389 389 MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR); 390 - 391 - #ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS 392 - #ifndef MODULE 393 - /* 394 - * This module was renamed to 8250_core in 3.7. Keep the old "8250" name 395 - * working as well for the module options so we don't break people. We 396 - * need to keep the names identical and the convenient macros will happily 397 - * refuse to let us do that by failing the build with redefinition errors 398 - * of global variables. So we stick them inside a dummy function to avoid 399 - * those conflicts. The options still get parsed, and the redefined 400 - * MODULE_PARAM_PREFIX lets us keep the "8250." syntax alive. 401 - * 402 - * This is hacky. I'm sorry. 403 - */ 404 - static void __used s8250_options(void) 405 - { 406 - #undef MODULE_PARAM_PREFIX 407 - #define MODULE_PARAM_PREFIX "8250_core." 408 - 409 - module_param_cb(share_irqs, &param_ops_uint, &share_irqs, 0644); 410 - module_param_cb(nr_uarts, &param_ops_uint, &nr_uarts, 0644); 411 - module_param_cb(skip_txen_test, &param_ops_uint, &skip_txen_test, 0644); 412 - } 413 - #else 414 - MODULE_ALIAS("8250_core"); 415 - #endif 416 - #endif
-24
drivers/tty/serial/8250/8250_rsa.c
··· 209 209 210 210 serial_out(up, UART_RSA_FRR, 0); 211 211 } 212 - 213 - #ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS 214 - #ifndef MODULE 215 - /* 216 - * Keep the old "8250" name working as well for the module options so we don't 217 - * break people. We need to keep the names identical and the convenient macros 218 - * will happily refuse to let us do that by failing the build with redefinition 219 - * errors of global variables. So we stick them inside a dummy function to 220 - * avoid those conflicts. The options still get parsed, and the redefined 221 - * MODULE_PARAM_PREFIX lets us keep the "8250." syntax alive. 222 - * 223 - * This is hacky. I'm sorry. 224 - */ 225 - static void __used rsa8250_options(void) 226 - { 227 - #undef MODULE_PARAM_PREFIX 228 - #define MODULE_PARAM_PREFIX "8250_core." 229 - 230 - __module_param_call(MODULE_PARAM_PREFIX, probe_rsa, 231 - &param_array_ops, .arr = &__param_arr_probe_rsa, 232 - 0444, -1, 0); 233 - } 234 - #endif 235 - #endif
+23 -17
drivers/tty/serial/8250/Kconfig
··· 34 34 Most people will say Y or M here, so that they can use serial mice, 35 35 modems and similar devices connecting to the standard serial ports. 36 36 37 - config SERIAL_8250_DEPRECATED_OPTIONS 38 - bool "Support 8250_core.* kernel options (DEPRECATED)" 39 - depends on SERIAL_8250 40 - default y 41 - help 42 - In 3.7 we renamed 8250 to 8250_core by mistake, so now we have to 43 - accept kernel parameters in both forms like 8250_core.nr_uarts=4 and 44 - 8250.nr_uarts=4. We now renamed the module back to 8250, but if 45 - anybody noticed in 3.7 and changed their userspace we still have to 46 - keep the 8250_core.* options around until they revert the changes 47 - they already did. 48 - 49 - If 8250 is built as a module, this adds 8250_core alias instead. 50 - 51 - If you did not notice yet and/or you have userspace from pre-3.7, it 52 - is safe (and recommended) to say N here. 53 - 54 37 config SERIAL_8250_PNP 55 38 bool "8250/16550 PNP device support" if EXPERT 56 39 depends on SERIAL_8250 && PNP ··· 413 430 behind the IOC3 device on those systems. Maximum baud speed is 414 431 38400bps using this driver. 415 432 433 + config SERIAL_8250_KEBA 434 + tristate "Support for KEBA 8250 UART" 435 + depends on SERIAL_8250 436 + depends on KEBA_CP500 437 + help 438 + Selecting this option will add support for KEBA UARTs. These UARTs 439 + are used for the serial interfaces of KEBA PLCs. 440 + 441 + This driver can also be built as a module. If so, the module will 442 + be called 8250_keba. 443 + 444 + If unsure, say N. 445 + 416 446 config SERIAL_8250_RT288X 417 447 bool "Ralink RT288x/RT305x/RT3662/RT3883 serial port support" 418 448 depends on SERIAL_8250 ··· 463 467 to keep this option enabled. Otherwise people might complain about a 464 468 not booting kernel because the serial console remains silent in case 465 469 they forgot to update the command line. 470 + 471 + config SERIAL_8250_LOONGSON 472 + tristate "Loongson 8250 based serial port" 473 + depends on SERIAL_8250 474 + depends on LOONGARCH || COMPILE_TEST 475 + help 476 + If you have a machine based on LoongArch CPU you can enable 477 + its onboard serial ports by enabling this option. The option 478 + is applicable to both devicetree and ACPI, say Y to this option. 479 + If unsure, say N. 466 480 467 481 config SERIAL_8250_LPC18XX 468 482 tristate "NXP LPC18xx/43xx serial port support"
+2
drivers/tty/serial/8250/Makefile
··· 38 38 obj-$(CONFIG_SERIAL_8250_HUB6) += 8250_hub6.o 39 39 obj-$(CONFIG_SERIAL_8250_INGENIC) += 8250_ingenic.o 40 40 obj-$(CONFIG_SERIAL_8250_IOC3) += 8250_ioc3.o 41 + obj-$(CONFIG_SERIAL_8250_KEBA) += 8250_keba.o 42 + obj-$(CONFIG_SERIAL_8250_LOONGSON) += 8250_loongson.o 41 43 obj-$(CONFIG_SERIAL_8250_LPC18XX) += 8250_lpc18xx.o 42 44 obj-$(CONFIG_SERIAL_8250_LPSS) += 8250_lpss.o 43 45 obj-$(CONFIG_SERIAL_8250_MEN_MCB) += 8250_men_mcb.o
+1 -1
drivers/tty/serial/Kconfig
··· 1044 1044 1045 1045 config SERIAL_SC16IS7XX 1046 1046 tristate "NXP SC16IS7xx UART support" 1047 - depends on SPI_MASTER || I2C 1047 + depends on SPI_MASTER || I2C || COMPILE_TEST 1048 1048 select SERIAL_CORE 1049 1049 select SERIAL_SC16IS7XX_SPI if SPI_MASTER 1050 1050 select SERIAL_SC16IS7XX_I2C if I2C
+62
drivers/tty/serial/ar933x_uart.c
··· 560 560 return 0; 561 561 } 562 562 563 + #ifdef CONFIG_CONSOLE_POLL 564 + static int ar933x_poll_get_char(struct uart_port *port) 565 + { 566 + struct ar933x_uart_port *up = 567 + container_of(port, struct ar933x_uart_port, port); 568 + unsigned int rdata; 569 + unsigned char ch; 570 + u32 imr; 571 + 572 + /* Disable all interrupts */ 573 + imr = ar933x_uart_read(up, AR933X_UART_INT_EN_REG); 574 + ar933x_uart_write(up, AR933X_UART_INT_EN_REG, 0); 575 + 576 + rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG); 577 + if ((rdata & AR933X_UART_DATA_RX_CSR) == 0) { 578 + /* Enable interrupts */ 579 + ar933x_uart_write(up, AR933X_UART_INT_EN_REG, imr); 580 + return NO_POLL_CHAR; 581 + } 582 + 583 + /* remove the character from the FIFO */ 584 + ar933x_uart_write(up, AR933X_UART_DATA_REG, 585 + AR933X_UART_DATA_RX_CSR); 586 + 587 + ch = rdata & AR933X_UART_DATA_TX_RX_MASK; 588 + 589 + /* Enable interrupts */ 590 + ar933x_uart_write(up, AR933X_UART_INT_EN_REG, imr); 591 + 592 + return ch; 593 + } 594 + 595 + static void ar933x_poll_put_char(struct uart_port *port, unsigned char c) 596 + { 597 + struct ar933x_uart_port *up = 598 + container_of(port, struct ar933x_uart_port, port); 599 + u32 imr; 600 + 601 + /* Disable all interrupts */ 602 + imr = ar933x_uart_read(up, AR933X_UART_INT_EN_REG); 603 + ar933x_uart_write(up, AR933X_UART_INT_EN_REG, 0); 604 + 605 + /* Wait until FIFO is empty */ 606 + while (!(ar933x_uart_read(up, AR933X_UART_DATA_REG) & AR933X_UART_DATA_TX_CSR)) 607 + cpu_relax(); 608 + 609 + /* Write a character */ 610 + ar933x_uart_putc(up, c); 611 + 612 + /* Wait until FIFO is empty */ 613 + while (!(ar933x_uart_read(up, AR933X_UART_DATA_REG) & AR933X_UART_DATA_TX_CSR)) 614 + cpu_relax(); 615 + 616 + /* Enable interrupts */ 617 + ar933x_uart_write(up, AR933X_UART_INT_EN_REG, imr); 618 + } 619 + #endif 620 + 563 621 static const struct uart_ops ar933x_uart_ops = { 564 622 .tx_empty = ar933x_uart_tx_empty, 565 623 .set_mctrl = ar933x_uart_set_mctrl, ··· 634 576 .request_port = ar933x_uart_request_port, 635 577 .config_port = ar933x_uart_config_port, 636 578 .verify_port = ar933x_uart_verify_port, 579 + #ifdef CONFIG_CONSOLE_POLL 580 + .poll_get_char = ar933x_poll_get_char, 581 + .poll_put_char = ar933x_poll_put_char, 582 + #endif 637 583 }; 638 584 639 585 static int ar933x_config_rs485(struct uart_port *port, struct ktermios *termios,
+8
drivers/tty/serial/fsl_lpuart.c
··· 3087 3087 static int lpuart_resume_noirq(struct device *dev) 3088 3088 { 3089 3089 struct lpuart_port *sport = dev_get_drvdata(dev); 3090 + struct tty_port *port = &sport->port.state->port; 3091 + bool wake_active; 3090 3092 u32 stat; 3091 3093 3092 3094 pinctrl_pm_select_default_state(dev); ··· 3100 3098 if (lpuart_is_32(sport)) { 3101 3099 stat = lpuart32_read(&sport->port, UARTSTAT); 3102 3100 lpuart32_write(&sport->port, stat, UARTSTAT); 3101 + 3102 + /* check whether lpuart wakeup was triggered */ 3103 + wake_active = stat & (UARTSTAT_RDRF | UARTSTAT_RXEDGIF); 3104 + 3105 + if (wake_active && irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq))) 3106 + pm_wakeup_event(tty_port_tty_get(port)->dev, 0); 3103 3107 } 3104 3108 } 3105 3109
+1
drivers/tty/serial/icom.c
··· 1723 1723 retval = pci_read_config_dword(dev, PCI_COMMAND, &command_reg); 1724 1724 if (retval) { 1725 1725 dev_err(&dev->dev, "PCI Config read FAILED\n"); 1726 + retval = pcibios_err_to_errno(retval); 1726 1727 goto probe_exit0; 1727 1728 } 1728 1729
+22 -2
drivers/tty/serial/imx.c
··· 30 30 #include <linux/iopoll.h> 31 31 #include <linux/dma-mapping.h> 32 32 33 - #include <asm/irq.h> 33 + #include <linux/irq.h> 34 34 #include <linux/dma/imx-dma.h> 35 35 36 36 #include "serial_mctrl_gpio.h" ··· 2697 2697 /* called with irq off */ 2698 2698 static void imx_uart_enable_wakeup(struct imx_port *sport, bool on) 2699 2699 { 2700 - u32 ucr3; 2700 + struct tty_port *port = &sport->port.state->port; 2701 + struct device *tty_dev; 2702 + bool may_wake = false, wake_active = false; 2703 + u32 ucr3, usr1; 2704 + 2705 + scoped_guard(tty_port_tty, port) { 2706 + struct tty_struct *tty = scoped_tty(); 2707 + 2708 + tty_dev = tty->dev; 2709 + may_wake = tty_dev && device_may_wakeup(tty_dev); 2710 + } 2711 + 2712 + /* only configure the wake register when device set as wakeup source */ 2713 + if (!may_wake) 2714 + return; 2701 2715 2702 2716 uart_port_lock_irq(&sport->port); 2703 2717 2718 + usr1 = imx_uart_readl(sport, USR1); 2704 2719 ucr3 = imx_uart_readl(sport, UCR3); 2705 2720 if (on) { 2706 2721 imx_uart_writel(sport, USR1_AWAKE, USR1); 2707 2722 ucr3 |= UCR3_AWAKEN; 2708 2723 } else { 2709 2724 ucr3 &= ~UCR3_AWAKEN; 2725 + wake_active = usr1 & USR1_AWAKE; 2710 2726 } 2711 2727 imx_uart_writel(sport, ucr3, UCR3); 2712 2728 ··· 2733 2717 ucr1 |= UCR1_RTSDEN; 2734 2718 } else { 2735 2719 ucr1 &= ~UCR1_RTSDEN; 2720 + wake_active = wake_active || (usr1 & USR1_RTSD); 2736 2721 } 2737 2722 imx_uart_writel(sport, ucr1, UCR1); 2738 2723 } 2724 + 2725 + if (wake_active && irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq))) 2726 + pm_wakeup_event(tty_port_tty_get(port)->dev, 0); 2739 2727 2740 2728 uart_port_unlock_irq(&sport->port); 2741 2729 }
+1 -1
drivers/tty/serial/mux.c
··· 343 343 } 344 344 345 345 /** 346 - * mux_drv_poll - Mux poll function. 346 + * mux_poll - Mux poll function. 347 347 * @unused: Unused variable 348 348 * 349 349 * This function periodically polls the Serial MUX to check for new data.
+161 -17
drivers/tty/serial/qcom_geni_serial.c
··· 14 14 #include <linux/irq.h> 15 15 #include <linux/module.h> 16 16 #include <linux/of.h> 17 + #include <linux/pm_domain.h> 17 18 #include <linux/pm_opp.h> 18 19 #include <linux/platform_device.h> 19 20 #include <linux/pm_runtime.h> ··· 102 101 #define DMA_RX_BUF_SIZE 2048 103 102 104 103 static DEFINE_IDA(port_ida); 104 + #define DOMAIN_IDX_POWER 0 105 + #define DOMAIN_IDX_PERF 1 105 106 106 107 struct qcom_geni_device_data { 107 108 bool console; 108 109 enum geni_se_xfer_mode mode; 110 + struct dev_pm_domain_attach_data pd_data; 111 + int (*resources_init)(struct uart_port *uport); 112 + int (*set_rate)(struct uart_port *uport, unsigned int baud); 113 + int (*power_state)(struct uart_port *uport, bool state); 109 114 }; 110 115 111 116 struct qcom_geni_private_data { ··· 149 142 150 143 struct qcom_geni_private_data private_data; 151 144 const struct qcom_geni_device_data *dev_data; 145 + struct dev_pm_domain_list *pd_list; 152 146 }; 153 147 154 148 static const struct uart_ops qcom_geni_console_pops; ··· 1307 1299 return 0; 1308 1300 } 1309 1301 1302 + static int geni_serial_set_level(struct uart_port *uport, unsigned int baud) 1303 + { 1304 + struct qcom_geni_serial_port *port = to_dev_port(uport); 1305 + struct device *perf_dev = port->pd_list->pd_devs[DOMAIN_IDX_PERF]; 1306 + 1307 + /* 1308 + * The performance protocol sets UART communication 1309 + * speeds by selecting different performance levels 1310 + * through the OPP framework. 1311 + * 1312 + * Supported perf levels for baudrates in firmware are below 1313 + * +---------------------+--------------------+ 1314 + * | Perf level value | Baudrate values | 1315 + * +---------------------+--------------------+ 1316 + * | 300 | 300 | 1317 + * | 1200 | 1200 | 1318 + * | 2400 | 2400 | 1319 + * | 4800 | 4800 | 1320 + * | 9600 | 9600 | 1321 + * | 19200 | 19200 | 1322 + * | 38400 | 38400 | 1323 + * | 57600 | 57600 | 1324 + * | 115200 | 115200 | 1325 + * | 230400 | 230400 | 1326 + * | 460800 | 460800 | 1327 + * | 921600 | 921600 | 1328 + * | 2000000 | 2000000 | 1329 + * | 3000000 | 3000000 | 1330 + * | 3200000 | 3200000 | 1331 + * | 4000000 | 4000000 | 1332 + * +---------------------+--------------------+ 1333 + */ 1334 + 1335 + return dev_pm_opp_set_level(perf_dev, baud); 1336 + } 1337 + 1310 1338 static void qcom_geni_serial_set_termios(struct uart_port *uport, 1311 1339 struct ktermios *termios, 1312 1340 const struct ktermios *old) ··· 1361 1317 /* baud rate */ 1362 1318 baud = uart_get_baud_rate(uport, termios, old, 300, 8000000); 1363 1319 1364 - ret = geni_serial_set_rate(uport, baud); 1320 + ret = port->dev_data->set_rate(uport, baud); 1365 1321 if (ret) 1366 1322 return; 1367 1323 ··· 1648 1604 return 0; 1649 1605 } 1650 1606 1651 - static int geni_serial_resource_init(struct qcom_geni_serial_port *port) 1607 + static int geni_serial_resource_state(struct uart_port *uport, bool power_on) 1652 1608 { 1609 + return power_on ? geni_serial_resources_on(uport) : geni_serial_resources_off(uport); 1610 + } 1611 + 1612 + static int geni_serial_pwr_init(struct uart_port *uport) 1613 + { 1614 + struct qcom_geni_serial_port *port = to_dev_port(uport); 1615 + int ret; 1616 + 1617 + ret = dev_pm_domain_attach_list(port->se.dev, 1618 + &port->dev_data->pd_data, &port->pd_list); 1619 + if (ret <= 0) 1620 + return -EINVAL; 1621 + 1622 + return 0; 1623 + } 1624 + 1625 + static int geni_serial_resource_init(struct uart_port *uport) 1626 + { 1627 + struct qcom_geni_serial_port *port = to_dev_port(uport); 1653 1628 int ret; 1654 1629 1655 1630 port->se.clk = devm_clk_get(port->se.dev, "se"); ··· 1713 1650 old_state = UART_PM_STATE_OFF; 1714 1651 1715 1652 if (new_state == UART_PM_STATE_ON && old_state == UART_PM_STATE_OFF) 1716 - geni_serial_resources_on(uport); 1653 + pm_runtime_resume_and_get(uport->dev); 1717 1654 else if (new_state == UART_PM_STATE_OFF && 1718 1655 old_state == UART_PM_STATE_ON) 1719 - geni_serial_resources_off(uport); 1656 + pm_runtime_put_sync(uport->dev); 1720 1657 1721 1658 } 1722 1659 ··· 1819 1756 port->se.dev = &pdev->dev; 1820 1757 port->se.wrapper = dev_get_drvdata(pdev->dev.parent); 1821 1758 1822 - ret = geni_serial_resource_init(port); 1759 + ret = port->dev_data->resources_init(uport); 1823 1760 if (ret) 1824 1761 return ret; 1825 1762 1826 1763 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1827 - if (!res) 1828 - return -EINVAL; 1764 + if (!res) { 1765 + ret = -EINVAL; 1766 + goto error; 1767 + } 1768 + 1829 1769 uport->mapbase = res->start; 1830 1770 1831 1771 uport->rs485_config = qcom_geni_rs485_config; ··· 1840 1774 if (!data->console) { 1841 1775 port->rx_buf = devm_kzalloc(uport->dev, 1842 1776 DMA_RX_BUF_SIZE, GFP_KERNEL); 1843 - if (!port->rx_buf) 1844 - return -ENOMEM; 1777 + if (!port->rx_buf) { 1778 + ret = -ENOMEM; 1779 + goto error; 1780 + } 1845 1781 } 1846 1782 1847 1783 port->name = devm_kasprintf(uport->dev, GFP_KERNEL, 1848 1784 "qcom_geni_serial_%s%d", 1849 1785 uart_console(uport) ? "console" : "uart", uport->line); 1850 - if (!port->name) 1851 - return -ENOMEM; 1786 + if (!port->name) { 1787 + ret = -ENOMEM; 1788 + goto error; 1789 + } 1852 1790 1853 1791 irq = platform_get_irq(pdev, 0); 1854 - if (irq < 0) 1855 - return irq; 1792 + if (irq < 0) { 1793 + ret = irq; 1794 + goto error; 1795 + } 1796 + 1856 1797 uport->irq = irq; 1857 1798 uport->has_sysrq = IS_ENABLED(CONFIG_SERIAL_QCOM_GENI_CONSOLE); 1858 1799 ··· 1881 1808 IRQF_TRIGGER_HIGH, port->name, uport); 1882 1809 if (ret) { 1883 1810 dev_err(uport->dev, "Failed to get IRQ ret %d\n", ret); 1884 - return ret; 1811 + goto error; 1885 1812 } 1886 1813 1887 1814 ret = uart_get_rs485_mode(uport); 1888 1815 if (ret) 1889 - return ret; 1816 + goto error; 1817 + 1818 + devm_pm_runtime_enable(port->se.dev); 1890 1819 1891 1820 ret = uart_add_one_port(drv, uport); 1892 1821 if (ret) 1893 - return ret; 1822 + goto error; 1894 1823 1895 1824 if (port->wakeup_irq > 0) { 1896 1825 device_init_wakeup(&pdev->dev, true); ··· 1902 1827 device_init_wakeup(&pdev->dev, false); 1903 1828 ida_free(&port_ida, uport->line); 1904 1829 uart_remove_one_port(drv, uport); 1905 - return ret; 1830 + goto error; 1906 1831 } 1907 1832 } 1908 1833 1909 1834 return 0; 1835 + 1836 + error: 1837 + dev_pm_domain_detach_list(port->pd_list); 1838 + return ret; 1910 1839 } 1911 1840 1912 1841 static void qcom_geni_serial_remove(struct platform_device *pdev) ··· 1923 1844 device_init_wakeup(&pdev->dev, false); 1924 1845 ida_free(&port_ida, uport->line); 1925 1846 uart_remove_one_port(drv, &port->uport); 1847 + dev_pm_domain_detach_list(port->pd_list); 1848 + } 1849 + 1850 + static int __maybe_unused qcom_geni_serial_runtime_suspend(struct device *dev) 1851 + { 1852 + struct qcom_geni_serial_port *port = dev_get_drvdata(dev); 1853 + struct uart_port *uport = &port->uport; 1854 + int ret = 0; 1855 + 1856 + if (port->dev_data->power_state) 1857 + ret = port->dev_data->power_state(uport, false); 1858 + 1859 + return ret; 1860 + } 1861 + 1862 + static int __maybe_unused qcom_geni_serial_runtime_resume(struct device *dev) 1863 + { 1864 + struct qcom_geni_serial_port *port = dev_get_drvdata(dev); 1865 + struct uart_port *uport = &port->uport; 1866 + int ret = 0; 1867 + 1868 + if (port->dev_data->power_state) 1869 + ret = port->dev_data->power_state(uport, true); 1870 + 1871 + return ret; 1926 1872 } 1927 1873 1928 1874 static int qcom_geni_serial_suspend(struct device *dev) ··· 1985 1881 static const struct qcom_geni_device_data qcom_geni_console_data = { 1986 1882 .console = true, 1987 1883 .mode = GENI_SE_FIFO, 1884 + .resources_init = geni_serial_resource_init, 1885 + .set_rate = geni_serial_set_rate, 1886 + .power_state = geni_serial_resource_state, 1988 1887 }; 1989 1888 1990 1889 static const struct qcom_geni_device_data qcom_geni_uart_data = { 1991 1890 .console = false, 1992 1891 .mode = GENI_SE_DMA, 1892 + .resources_init = geni_serial_resource_init, 1893 + .set_rate = geni_serial_set_rate, 1894 + .power_state = geni_serial_resource_state, 1895 + }; 1896 + 1897 + static const struct qcom_geni_device_data sa8255p_qcom_geni_console_data = { 1898 + .console = true, 1899 + .mode = GENI_SE_FIFO, 1900 + .pd_data = { 1901 + .pd_flags = PD_FLAG_DEV_LINK_ON, 1902 + .pd_names = (const char*[]) { "power", "perf" }, 1903 + .num_pd_names = 2, 1904 + }, 1905 + .resources_init = geni_serial_pwr_init, 1906 + .set_rate = geni_serial_set_level, 1907 + }; 1908 + 1909 + static const struct qcom_geni_device_data sa8255p_qcom_geni_uart_data = { 1910 + .console = false, 1911 + .mode = GENI_SE_DMA, 1912 + .pd_data = { 1913 + .pd_flags = PD_FLAG_DEV_LINK_ON, 1914 + .pd_names = (const char*[]) { "power", "perf" }, 1915 + .num_pd_names = 2, 1916 + }, 1917 + .resources_init = geni_serial_pwr_init, 1918 + .set_rate = geni_serial_set_level, 1993 1919 }; 1994 1920 1995 1921 static const struct dev_pm_ops qcom_geni_serial_pm_ops = { 1922 + SET_RUNTIME_PM_OPS(qcom_geni_serial_runtime_suspend, 1923 + qcom_geni_serial_runtime_resume, NULL) 1996 1924 SYSTEM_SLEEP_PM_OPS(qcom_geni_serial_suspend, qcom_geni_serial_resume) 1997 1925 }; 1998 1926 ··· 2034 1898 .data = &qcom_geni_console_data, 2035 1899 }, 2036 1900 { 1901 + .compatible = "qcom,sa8255p-geni-debug-uart", 1902 + .data = &sa8255p_qcom_geni_console_data, 1903 + }, 1904 + { 2037 1905 .compatible = "qcom,geni-uart", 2038 1906 .data = &qcom_geni_uart_data, 1907 + }, 1908 + { 1909 + .compatible = "qcom,sa8255p-geni-uart", 1910 + .data = &sa8255p_qcom_geni_uart_data, 2039 1911 }, 2040 1912 {} 2041 1913 };
+2
drivers/tty/serial/samsung_tty.c
··· 2830 2830 s5pv210_early_console_setup); 2831 2831 OF_EARLYCON_DECLARE(artpec8, "axis,artpec8-uart", 2832 2832 s5pv210_early_console_setup); 2833 + OF_EARLYCON_DECLARE(exynos850, "samsung,exynos850-uart", 2834 + s5pv210_early_console_setup); 2833 2835 2834 2836 static int __init gs101_early_console_setup(struct earlycon_device *device, 2835 2837 const char *opt)
+182 -231
drivers/tty/serial/sc16is7xx.c
··· 11 11 #define DEFAULT_SYMBOL_NAMESPACE "SERIAL_NXP_SC16IS7XX" 12 12 13 13 #include <linux/bits.h> 14 + #include <linux/cleanup.h> 14 15 #include <linux/clk.h> 15 16 #include <linux/delay.h> 16 17 #include <linux/device.h> ··· 50 49 #define SC16IS7XX_SPR_REG (0x07) /* Scratch Pad */ 51 50 #define SC16IS7XX_TXLVL_REG (0x08) /* TX FIFO level */ 52 51 #define SC16IS7XX_RXLVL_REG (0x09) /* RX FIFO level */ 53 - #define SC16IS7XX_IODIR_REG (0x0a) /* I/O Direction 54 - * - only on 75x/76x 55 - */ 56 - #define SC16IS7XX_IOSTATE_REG (0x0b) /* I/O State 57 - * - only on 75x/76x 58 - */ 59 - #define SC16IS7XX_IOINTENA_REG (0x0c) /* I/O Interrupt Enable 60 - * - only on 75x/76x 61 - */ 62 - #define SC16IS7XX_IOCONTROL_REG (0x0e) /* I/O Control 63 - * - only on 75x/76x 64 - */ 52 + #define SC16IS7XX_IODIR_REG (0x0a) /* I/O Direction - only on 75x/76x */ 53 + #define SC16IS7XX_IOSTATE_REG (0x0b) /* I/O State - only on 75x/76x */ 54 + #define SC16IS7XX_IOINTENA_REG (0x0c) /* I/O Interrupt Enable - only on 75x/76x */ 55 + #define SC16IS7XX_IOCONTROL_REG (0x0e) /* I/O Control - only on 75x/76x */ 65 56 #define SC16IS7XX_EFCR_REG (0x0f) /* Extra Features Control */ 66 57 67 58 /* TCR/TLR Register set: Only if ((MCR[2] == 1) && (EFR[4] == 1)) */ ··· 73 80 74 81 /* IER register bits */ 75 82 #define SC16IS7XX_IER_RDI_BIT BIT(0) /* Enable RX data interrupt */ 76 - #define SC16IS7XX_IER_THRI_BIT BIT(1) /* Enable TX holding register 77 - * interrupt */ 78 - #define SC16IS7XX_IER_RLSI_BIT BIT(2) /* Enable RX line status 79 - * interrupt */ 80 - #define SC16IS7XX_IER_MSI_BIT BIT(3) /* Enable Modem status 81 - * interrupt */ 83 + #define SC16IS7XX_IER_THRI_BIT BIT(1) /* Enable TX holding register interrupt */ 84 + #define SC16IS7XX_IER_RLSI_BIT BIT(2) /* Enable RX line status interrupt */ 85 + #define SC16IS7XX_IER_MSI_BIT BIT(3) /* Enable Modem status interrupt */ 82 86 83 87 /* IER register bits - write only if (EFR[4] == 1) */ 84 88 #define SC16IS7XX_IER_SLEEP_BIT BIT(4) /* Enable Sleep mode */ ··· 108 118 * - only on 75x/76x 109 119 */ 110 120 #define SC16IS7XX_IIR_XOFFI_SRC 0x10 /* Received Xoff */ 111 - #define SC16IS7XX_IIR_CTSRTS_SRC 0x20 /* nCTS,nRTS change of state 112 - * from active (LOW) 113 - * to inactive (HIGH) 121 + #define SC16IS7XX_IIR_CTSRTS_SRC 0x20 /* nCTS,nRTS change of state from active 122 + * (LOW) to inactive (HIGH) 114 123 */ 115 124 /* LCR register bits */ 116 125 #define SC16IS7XX_LCR_LENGTH0_BIT BIT(0) /* Word length bit 0 */ ··· 125 136 * 126 137 * STOP length bit table: 127 138 * 0 -> 1 stop bit 128 - * 1 -> 1-1.5 stop bits if 129 - * word length is 5, 139 + * 1 -> 1-1.5 stop bits if word length is 5, 130 140 * 2 stop bits otherwise 131 141 */ 132 142 #define SC16IS7XX_LCR_PARITY_BIT BIT(3) /* Parity bit enable */ ··· 137 149 #define SC16IS7XX_LCR_WORD_LEN_6 (0x01) 138 150 #define SC16IS7XX_LCR_WORD_LEN_7 (0x02) 139 151 #define SC16IS7XX_LCR_WORD_LEN_8 (0x03) 140 - #define SC16IS7XX_LCR_CONF_MODE_A SC16IS7XX_LCR_DLAB_BIT /* Special 141 - * reg set */ 142 - #define SC16IS7XX_LCR_CONF_MODE_B 0xBF /* Enhanced 143 - * reg set */ 152 + #define SC16IS7XX_LCR_REG_SET_SPECIAL SC16IS7XX_LCR_DLAB_BIT /* Special reg set */ 153 + #define SC16IS7XX_LCR_REG_SET_ENHANCED 0xBF /* Enhanced reg set */ 144 154 145 155 /* MCR register bits */ 146 - #define SC16IS7XX_MCR_DTR_BIT BIT(0) /* DTR complement 147 - * - only on 75x/76x 148 - */ 156 + #define SC16IS7XX_MCR_DTR_BIT BIT(0) /* DTR complement - only on 75x/76x */ 149 157 #define SC16IS7XX_MCR_RTS_BIT BIT(1) /* RTS complement */ 150 - #define SC16IS7XX_MCR_TCRTLR_BIT BIT(2) /* TCR/TLR register enable */ 158 + #define SC16IS7XX_MCR_TCRTLR_BIT BIT(2) /* TCR/TLR registers enable */ 151 159 #define SC16IS7XX_MCR_LOOP_BIT BIT(4) /* Enable loopback test mode */ 152 160 #define SC16IS7XX_MCR_XONANY_BIT BIT(5) /* Enable Xon Any 153 - * - write enabled 154 - * if (EFR[4] == 1) 161 + * - write enabled if (EFR[4] == 1) 155 162 */ 156 163 #define SC16IS7XX_MCR_IRDA_BIT BIT(6) /* Enable IrDA mode 157 - * - write enabled 158 - * if (EFR[4] == 1) 164 + * - write enabled if (EFR[4] == 1) 159 165 */ 160 166 #define SC16IS7XX_MCR_CLKSEL_BIT BIT(7) /* Divide clock by 4 161 - * - write enabled 162 - * if (EFR[4] == 1) 167 + * - write enabled if (EFR[4] == 1) 163 168 */ 164 169 165 170 /* LSR register bits */ ··· 173 192 174 193 /* MSR register bits */ 175 194 #define SC16IS7XX_MSR_DCTS_BIT BIT(0) /* Delta CTS Clear To Send */ 176 - #define SC16IS7XX_MSR_DDSR_BIT BIT(1) /* Delta DSR Data Set Ready 177 - * or (IO4) 195 + #define SC16IS7XX_MSR_DDSR_BIT BIT(1) /* Delta DSR Data Set Ready or (IO4) 178 196 * - only on 75x/76x 179 197 */ 180 - #define SC16IS7XX_MSR_DRI_BIT BIT(2) /* Delta RI Ring Indicator 181 - * or (IO7) 198 + #define SC16IS7XX_MSR_DRI_BIT BIT(2) /* Delta RI Ring Indicator or (IO7) 182 199 * - only on 75x/76x 183 200 */ 184 - #define SC16IS7XX_MSR_DCD_BIT BIT(3) /* Delta CD Carrier Detect 185 - * or (IO6) 201 + #define SC16IS7XX_MSR_DCD_BIT BIT(3) /* Delta CD Carrier Detect or (IO6) 186 202 * - only on 75x/76x 187 203 */ 188 204 #define SC16IS7XX_MSR_CTS_BIT BIT(4) /* CTS */ 189 - #define SC16IS7XX_MSR_DSR_BIT BIT(5) /* DSR (IO4) 190 - * - only on 75x/76x 191 - */ 192 - #define SC16IS7XX_MSR_RI_BIT BIT(6) /* RI (IO7) 193 - * - only on 75x/76x 194 - */ 195 - #define SC16IS7XX_MSR_CD_BIT BIT(7) /* CD (IO6) 196 - * - only on 75x/76x 197 - */ 205 + #define SC16IS7XX_MSR_DSR_BIT BIT(5) /* DSR (IO4) - only on 75x/76x */ 206 + #define SC16IS7XX_MSR_RI_BIT BIT(6) /* RI (IO7) - only on 75x/76x */ 207 + #define SC16IS7XX_MSR_CD_BIT BIT(7) /* CD (IO6) - only on 75x/76x */ 198 208 199 209 /* 200 210 * TCR register bits ··· 224 252 #define SC16IS7XX_IOCONTROL_SRESET_BIT BIT(3) /* Software Reset */ 225 253 226 254 /* EFCR register bits */ 227 - #define SC16IS7XX_EFCR_9BIT_MODE_BIT BIT(0) /* Enable 9-bit or Multidrop 228 - * mode (RS485) */ 255 + #define SC16IS7XX_EFCR_9BIT_MODE_BIT BIT(0) /* Enable 9-bit or Multidrop mode (RS485) */ 229 256 #define SC16IS7XX_EFCR_RXDISABLE_BIT BIT(1) /* Disable receiver */ 230 257 #define SC16IS7XX_EFCR_TXDISABLE_BIT BIT(2) /* Disable transmitter */ 231 258 #define SC16IS7XX_EFCR_AUTO_RS485_BIT BIT(4) /* Auto RS485 RTS direction */ 232 259 #define SC16IS7XX_EFCR_RTS_INVERT_BIT BIT(5) /* RTS output inversion */ 233 260 #define SC16IS7XX_EFCR_IRDA_MODE_BIT BIT(7) /* IrDA mode 234 - * 0 = rate upto 115.2 kbit/s 235 - * - Only 75x/76x 236 - * 1 = rate upto 1.152 Mbit/s 237 - * - Only 76x 261 + * 0 = rate up to 115.2 kbit/s - Only 75x/76x 262 + * 1 = rate up to 1.152 Mbit/s - Only 76x 238 263 */ 239 264 240 265 /* EFR register bits */ 241 266 #define SC16IS7XX_EFR_AUTORTS_BIT BIT(6) /* Auto RTS flow ctrl enable */ 242 267 #define SC16IS7XX_EFR_AUTOCTS_BIT BIT(7) /* Auto CTS flow ctrl enable */ 243 268 #define SC16IS7XX_EFR_XOFF2_DETECT_BIT BIT(5) /* Enable Xoff2 detection */ 244 - #define SC16IS7XX_EFR_ENABLE_BIT BIT(4) /* Enable enhanced functions 245 - * and writing to IER[7:4], 246 - * FCR[5:4], MCR[7:5] 269 + #define SC16IS7XX_EFR_ENABLE_BIT BIT(4) /* Enable enhanced functions and writing to 270 + * IER[7:4], FCR[5:4], MCR[7:5] 247 271 */ 248 272 #define SC16IS7XX_EFR_SWFLOW3_BIT BIT(3) 249 273 #define SC16IS7XX_EFR_SWFLOW2_BIT BIT(2) 250 274 /* 251 275 * SWFLOW bits 3 & 2 table: 252 - * 00 -> no transmitter flow 253 - * control 254 - * 01 -> transmitter generates 255 - * XON2 and XOFF2 256 - * 10 -> transmitter generates 257 - * XON1 and XOFF1 258 - * 11 -> transmitter generates 259 - * XON1, XON2, XOFF1 and 260 - * XOFF2 276 + * 00 -> no transmitter flow control 277 + * 01 -> transmitter generates XON2 and XOFF2 278 + * 10 -> transmitter generates XON1 and XOFF1 279 + * 11 -> transmitter generates XON1, XON2, 280 + * XOFF1 and XOFF2 261 281 */ 262 282 #define SC16IS7XX_EFR_SWFLOW1_BIT BIT(1) 263 283 #define SC16IS7XX_EFR_SWFLOW0_BIT BIT(0) 264 284 /* 265 285 * SWFLOW bits 1 & 0 table: 266 - * 00 -> no received flow 267 - * control 268 - * 01 -> receiver compares 269 - * XON2 and XOFF2 270 - * 10 -> receiver compares 271 - * XON1 and XOFF1 272 - * 11 -> receiver compares 273 - * XON1, XON2, XOFF1 and 274 - * XOFF2 286 + * 00 -> no received flow control 287 + * 01 -> receiver compares XON2 and XOFF2 288 + * 10 -> receiver compares XON1 and XOFF1 289 + * 11 -> receiver compares XON1, XON2, 290 + * XOFF1 and XOFF2 275 291 */ 276 292 #define SC16IS7XX_EFR_FLOWCTRL_BITS (SC16IS7XX_EFR_AUTORTS_BIT | \ 277 293 SC16IS7XX_EFR_AUTOCTS_BIT | \ ··· 288 328 struct sc16is7xx_one { 289 329 struct uart_port port; 290 330 struct regmap *regmap; 291 - struct mutex efr_lock; /* EFR registers access */ 331 + struct mutex lock; /* For registers sharing same address space. */ 292 332 struct kthread_work tx_work; 293 333 struct kthread_work reg_work; 294 334 struct kthread_delayed_work ms_work; ··· 318 358 319 359 static struct uart_driver sc16is7xx_uart = { 320 360 .owner = THIS_MODULE, 321 - .driver_name = SC16IS7XX_NAME, 361 + .driver_name = KBUILD_MODNAME, 322 362 .dev_name = "ttySC", 323 363 .nr = SC16IS7XX_MAX_DEVS, 324 364 }; 325 365 326 - #define to_sc16is7xx_one(p,e) ((container_of((p), struct sc16is7xx_one, e))) 366 + #define to_sc16is7xx_one(p) container_of((p), struct sc16is7xx_one, port) 327 367 328 368 static u8 sc16is7xx_port_read(struct uart_port *port, u8 reg) 329 369 { 330 - struct sc16is7xx_one *one = to_sc16is7xx_one(port, port); 370 + struct sc16is7xx_one *one = to_sc16is7xx_one(port); 331 371 unsigned int val = 0; 332 372 333 373 regmap_read(one->regmap, reg, &val); ··· 337 377 338 378 static void sc16is7xx_port_write(struct uart_port *port, u8 reg, u8 val) 339 379 { 340 - struct sc16is7xx_one *one = to_sc16is7xx_one(port, port); 380 + struct sc16is7xx_one *one = to_sc16is7xx_one(port); 341 381 342 382 regmap_write(one->regmap, reg, val); 343 383 } 344 384 345 385 static void sc16is7xx_fifo_read(struct uart_port *port, u8 *rxbuf, unsigned int rxlen) 346 386 { 347 - struct sc16is7xx_one *one = to_sc16is7xx_one(port, port); 387 + struct sc16is7xx_one *one = to_sc16is7xx_one(port); 348 388 349 389 regmap_noinc_read(one->regmap, SC16IS7XX_RHR_REG, rxbuf, rxlen); 350 390 } 351 391 352 392 static void sc16is7xx_fifo_write(struct uart_port *port, u8 *txbuf, u8 to_send) 353 393 { 354 - struct sc16is7xx_one *one = to_sc16is7xx_one(port, port); 394 + struct sc16is7xx_one *one = to_sc16is7xx_one(port); 355 395 356 396 /* 357 397 * Don't send zero-length data, at least on SPI it confuses the chip ··· 366 406 static void sc16is7xx_port_update(struct uart_port *port, u8 reg, 367 407 u8 mask, u8 val) 368 408 { 369 - struct sc16is7xx_one *one = to_sc16is7xx_one(port, port); 409 + struct sc16is7xx_one *one = to_sc16is7xx_one(port); 370 410 371 411 regmap_update_bits(one->regmap, reg, mask, val); 372 412 } ··· 379 419 } 380 420 381 421 /* 382 - * In an amazing feat of design, the Enhanced Features Register (EFR) 383 - * shares the address of the Interrupt Identification Register (IIR). 384 - * Access to EFR is switched on by writing a magic value (0xbf) to the 385 - * Line Control Register (LCR). Any interrupt firing during this time will 386 - * see the EFR where it expects the IIR to be, leading to 422 + * In an amazing feat of design, the enhanced register set shares the 423 + * addresses 0x02 and 0x04-0x07 with the general register set. 424 + * The special register set also shares the addresses 0x00-0x01 with the 425 + * general register set. 426 + * 427 + * Access to the enhanced or special register set is enabled by writing a magic 428 + * value to the Line Control Register (LCR). When enhanced register set access 429 + * is enabled, for example, any interrupt firing during this time will see the 430 + * EFR where it expects the IIR to be, leading to 387 431 * "Unexpected interrupt" messages. 388 432 * 389 - * Prevent this possibility by claiming a mutex while accessing the EFR, 390 - * and claiming the same mutex from within the interrupt handler. This is 391 - * similar to disabling the interrupt, but that doesn't work because the 392 - * bulk of the interrupt processing is run as a workqueue job in thread 393 - * context. 433 + * Prevent this possibility by claiming a mutex when access to the enhanced 434 + * or special register set is enabled, and claiming the same mutex from within 435 + * the interrupt handler. This is similar to disabling the interrupt, but that 436 + * doesn't work because the bulk of the interrupt processing is run as a 437 + * workqueue job in thread context. 394 438 */ 395 - static void sc16is7xx_efr_lock(struct uart_port *port) 439 + static void sc16is7xx_regs_lock(struct uart_port *port, u8 register_set) 396 440 { 397 - struct sc16is7xx_one *one = to_sc16is7xx_one(port, port); 441 + struct sc16is7xx_one *one = to_sc16is7xx_one(port); 398 442 399 - mutex_lock(&one->efr_lock); 443 + mutex_lock(&one->lock); 400 444 401 445 /* Backup content of LCR. */ 402 446 one->old_lcr = sc16is7xx_port_read(port, SC16IS7XX_LCR_REG); 403 447 404 - /* Enable access to Enhanced register set */ 405 - sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, SC16IS7XX_LCR_CONF_MODE_B); 448 + /* Enable access to the desired register set */ 449 + sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, register_set); 406 450 407 - /* Disable cache updates when writing to EFR registers */ 451 + /* Disable cache updates when writing to non-general registers */ 408 452 regcache_cache_bypass(one->regmap, true); 409 453 } 410 454 411 - static void sc16is7xx_efr_unlock(struct uart_port *port) 455 + static void sc16is7xx_regs_unlock(struct uart_port *port) 412 456 { 413 - struct sc16is7xx_one *one = to_sc16is7xx_one(port, port); 457 + struct sc16is7xx_one *one = to_sc16is7xx_one(port); 414 458 415 - /* Re-enable cache updates when writing to normal registers */ 459 + /* Re-enable cache updates when writing to general registers */ 416 460 regcache_cache_bypass(one->regmap, false); 417 461 418 462 /* Restore original content of LCR */ 419 463 sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, one->old_lcr); 420 464 421 - mutex_unlock(&one->efr_lock); 465 + mutex_unlock(&one->lock); 422 466 } 423 467 424 468 static void sc16is7xx_ier_clear(struct uart_port *port, u8 bit) 425 469 { 426 470 struct sc16is7xx_port *s = dev_get_drvdata(port->dev); 427 - struct sc16is7xx_one *one = to_sc16is7xx_one(port, port); 471 + struct sc16is7xx_one *one = to_sc16is7xx_one(port); 428 472 429 473 lockdep_assert_held_once(&port->lock); 430 474 ··· 441 477 static void sc16is7xx_ier_set(struct uart_port *port, u8 bit) 442 478 { 443 479 struct sc16is7xx_port *s = dev_get_drvdata(port->dev); 444 - struct sc16is7xx_one *one = to_sc16is7xx_one(port, port); 480 + struct sc16is7xx_one *one = to_sc16is7xx_one(port); 445 481 446 482 lockdep_assert_held_once(&port->lock); 447 483 ··· 499 535 static bool sc16is7xx_regmap_volatile(struct device *dev, unsigned int reg) 500 536 { 501 537 switch (reg) { 502 - case SC16IS7XX_RHR_REG: 503 - case SC16IS7XX_IIR_REG: 504 - case SC16IS7XX_LSR_REG: 505 - case SC16IS7XX_MSR_REG: 538 + case SC16IS7XX_RHR_REG: /* Shared address space with THR & DLL */ 539 + case SC16IS7XX_IIR_REG: /* Shared address space with FCR & EFR */ 540 + case SC16IS7XX_LSR_REG: /* Shared address space with XON2 */ 541 + case SC16IS7XX_MSR_REG: /* Shared address space with TCR & XOFF1 */ 542 + case SC16IS7XX_SPR_REG: /* Shared address space with TLR & XOFF2 */ 506 543 case SC16IS7XX_TXLVL_REG: 507 544 case SC16IS7XX_RXLVL_REG: 508 545 case SC16IS7XX_IOSTATE_REG: ··· 543 578 */ 544 579 static int sc16is7xx_set_baud(struct uart_port *port, int baud) 545 580 { 546 - struct sc16is7xx_one *one = to_sc16is7xx_one(port, port); 547 - u8 lcr; 548 581 unsigned int prescaler = 1; 549 582 unsigned long clk = port->uartclk, div = clk / 16 / baud; 550 583 ··· 556 593 SC16IS7XX_MCR_CLKSEL_BIT, 557 594 prescaler == 1 ? 0 : SC16IS7XX_MCR_CLKSEL_BIT); 558 595 559 - mutex_lock(&one->efr_lock); 560 - 561 - /* Backup LCR and access special register set (DLL/DLH) */ 562 - lcr = sc16is7xx_port_read(port, SC16IS7XX_LCR_REG); 563 - sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, 564 - SC16IS7XX_LCR_CONF_MODE_A); 596 + /* Access special register set (DLL/DLH) */ 597 + sc16is7xx_regs_lock(port, SC16IS7XX_LCR_REG_SET_SPECIAL); 565 598 566 599 /* Write the new divisor */ 567 - regcache_cache_bypass(one->regmap, true); 568 600 sc16is7xx_port_write(port, SC16IS7XX_DLH_REG, div / 256); 569 601 sc16is7xx_port_write(port, SC16IS7XX_DLL_REG, div % 256); 570 - regcache_cache_bypass(one->regmap, false); 571 602 572 - /* Restore LCR and access to general register set */ 573 - sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, lcr); 574 - 575 - mutex_unlock(&one->efr_lock); 603 + /* Restore access to general register set */ 604 + sc16is7xx_regs_unlock(port); 576 605 577 606 return DIV_ROUND_CLOSEST((clk / prescaler) / 16, div); 578 607 } ··· 572 617 static void sc16is7xx_handle_rx(struct uart_port *port, unsigned int rxlen, 573 618 unsigned int iir) 574 619 { 575 - struct sc16is7xx_one *one = to_sc16is7xx_one(port, port); 620 + struct sc16is7xx_one *one = to_sc16is7xx_one(port); 576 621 unsigned int lsr = 0, bytes_read, i; 577 622 bool read_lsr = (iir == SC16IS7XX_IIR_RLSE_SRC); 578 623 u8 ch, flag; ··· 711 756 unsigned long flags; 712 757 unsigned int status, changed; 713 758 714 - lockdep_assert_held_once(&one->efr_lock); 759 + /* Lock required as MSR address is shared with TCR and XOFF1. */ 760 + lockdep_assert_held_once(&one->lock); 715 761 716 762 status = sc16is7xx_get_hwmctrl(port); 717 763 changed = status ^ one->old_mctrl; ··· 738 782 739 783 static bool sc16is7xx_port_irq(struct sc16is7xx_port *s, int portno) 740 784 { 741 - bool rc = true; 742 785 unsigned int iir, rxlen; 743 786 struct uart_port *port = &s->p[portno].port; 744 - struct sc16is7xx_one *one = to_sc16is7xx_one(port, port); 787 + struct sc16is7xx_one *one = to_sc16is7xx_one(port); 745 788 746 - mutex_lock(&one->efr_lock); 789 + guard(mutex)(&one->lock); 747 790 748 791 iir = sc16is7xx_port_read(port, SC16IS7XX_IIR_REG); 749 - if (iir & SC16IS7XX_IIR_NO_INT_BIT) { 750 - rc = false; 751 - goto out_port_irq; 752 - } 792 + if (iir & SC16IS7XX_IIR_NO_INT_BIT) 793 + return false; 753 794 754 795 iir &= SC16IS7XX_IIR_ID_MASK; 755 796 ··· 786 833 break; 787 834 } 788 835 789 - out_port_irq: 790 - mutex_unlock(&one->efr_lock); 791 - 792 - return rc; 836 + return true; 793 837 } 794 838 795 839 static irqreturn_t sc16is7xx_irq(int irq, void *dev_id) 796 840 { 841 + struct sc16is7xx_port *s = dev_id; 797 842 bool keep_polling; 798 - 799 - struct sc16is7xx_port *s = (struct sc16is7xx_port *)dev_id; 800 843 801 844 do { 802 845 int i; ··· 820 871 821 872 static void sc16is7xx_tx_proc(struct kthread_work *ws) 822 873 { 823 - struct uart_port *port = &(to_sc16is7xx_one(ws, tx_work)->port); 824 - struct sc16is7xx_one *one = to_sc16is7xx_one(port, port); 874 + struct sc16is7xx_one *one = container_of(ws, struct sc16is7xx_one, tx_work); 875 + struct uart_port *port = &one->port; 825 876 826 877 if ((port->rs485.flags & SER_RS485_ENABLED) && 827 878 (port->rs485.delay_rts_before_send > 0)) 828 879 msleep(port->rs485.delay_rts_before_send); 829 880 830 - mutex_lock(&one->efr_lock); 881 + guard(mutex)(&one->lock); 831 882 sc16is7xx_handle_tx(port); 832 - mutex_unlock(&one->efr_lock); 833 883 } 834 884 835 885 static void sc16is7xx_reconf_rs485(struct uart_port *port) ··· 853 905 854 906 static void sc16is7xx_reg_proc(struct kthread_work *ws) 855 907 { 856 - struct sc16is7xx_one *one = to_sc16is7xx_one(ws, reg_work); 908 + struct sc16is7xx_one *one = container_of(ws, struct sc16is7xx_one, reg_work); 857 909 struct sc16is7xx_one_config config; 858 910 unsigned long irqflags; 859 911 ··· 891 943 892 944 static void sc16is7xx_ms_proc(struct kthread_work *ws) 893 945 { 894 - struct sc16is7xx_one *one = to_sc16is7xx_one(ws, ms_work.work); 946 + struct sc16is7xx_one *one = container_of(ws, struct sc16is7xx_one, ms_work.work); 895 947 struct sc16is7xx_port *s = dev_get_drvdata(one->port.dev); 896 948 897 949 if (one->port.state) { 898 - mutex_lock(&one->efr_lock); 899 - sc16is7xx_update_mlines(one); 900 - mutex_unlock(&one->efr_lock); 950 + scoped_guard(mutex, &one->lock) 951 + sc16is7xx_update_mlines(one); 901 952 902 953 kthread_queue_delayed_work(&s->kworker, &one->ms_work, HZ); 903 954 } ··· 904 957 905 958 static void sc16is7xx_enable_ms(struct uart_port *port) 906 959 { 907 - struct sc16is7xx_one *one = to_sc16is7xx_one(port, port); 960 + struct sc16is7xx_one *one = to_sc16is7xx_one(port); 908 961 struct sc16is7xx_port *s = dev_get_drvdata(port->dev); 909 962 910 963 lockdep_assert_held_once(&port->lock); ··· 915 968 static void sc16is7xx_start_tx(struct uart_port *port) 916 969 { 917 970 struct sc16is7xx_port *s = dev_get_drvdata(port->dev); 918 - struct sc16is7xx_one *one = to_sc16is7xx_one(port, port); 971 + struct sc16is7xx_one *one = to_sc16is7xx_one(port); 919 972 920 973 kthread_queue_work(&s->kworker, &one->tx_work); 921 974 } ··· 954 1007 955 1008 static unsigned int sc16is7xx_get_mctrl(struct uart_port *port) 956 1009 { 957 - struct sc16is7xx_one *one = to_sc16is7xx_one(port, port); 1010 + struct sc16is7xx_one *one = to_sc16is7xx_one(port); 958 1011 959 1012 /* Called with port lock taken so we can only return cached value */ 960 1013 return one->old_mctrl; ··· 963 1016 static void sc16is7xx_set_mctrl(struct uart_port *port, unsigned int mctrl) 964 1017 { 965 1018 struct sc16is7xx_port *s = dev_get_drvdata(port->dev); 966 - struct sc16is7xx_one *one = to_sc16is7xx_one(port, port); 1019 + struct sc16is7xx_one *one = to_sc16is7xx_one(port); 967 1020 968 1021 one->config.flags |= SC16IS7XX_RECONF_MD; 969 1022 kthread_queue_work(&s->kworker, &one->reg_work); ··· 980 1033 struct ktermios *termios, 981 1034 const struct ktermios *old) 982 1035 { 983 - struct sc16is7xx_one *one = to_sc16is7xx_one(port, port); 1036 + struct sc16is7xx_one *one = to_sc16is7xx_one(port); 984 1037 unsigned int lcr, flow = 0; 985 1038 int baud; 986 1039 unsigned long flags; ··· 1053 1106 sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, lcr); 1054 1107 1055 1108 /* Update EFR registers */ 1056 - sc16is7xx_efr_lock(port); 1109 + sc16is7xx_regs_lock(port, SC16IS7XX_LCR_REG_SET_ENHANCED); 1057 1110 sc16is7xx_port_write(port, SC16IS7XX_XON1_REG, termios->c_cc[VSTART]); 1058 1111 sc16is7xx_port_write(port, SC16IS7XX_XOFF1_REG, termios->c_cc[VSTOP]); 1059 1112 sc16is7xx_port_update(port, SC16IS7XX_EFR_REG, 1060 1113 SC16IS7XX_EFR_FLOWCTRL_BITS, flow); 1061 - sc16is7xx_efr_unlock(port); 1114 + sc16is7xx_regs_unlock(port); 1062 1115 1063 1116 /* Get baud rate generator configuration */ 1064 1117 baud = uart_get_baud_rate(port, termios, old, ··· 1083 1136 struct serial_rs485 *rs485) 1084 1137 { 1085 1138 struct sc16is7xx_port *s = dev_get_drvdata(port->dev); 1086 - struct sc16is7xx_one *one = to_sc16is7xx_one(port, port); 1139 + struct sc16is7xx_one *one = to_sc16is7xx_one(port); 1087 1140 1088 1141 if (rs485->flags & SER_RS485_ENABLED) { 1089 1142 /* ··· 1103 1156 1104 1157 static int sc16is7xx_startup(struct uart_port *port) 1105 1158 { 1106 - struct sc16is7xx_one *one = to_sc16is7xx_one(port, port); 1159 + struct sc16is7xx_one *one = to_sc16is7xx_one(port); 1107 1160 struct sc16is7xx_port *s = dev_get_drvdata(port->dev); 1108 1161 unsigned int val; 1109 1162 unsigned long flags; 1110 1163 1111 1164 sc16is7xx_power(port, 1); 1112 1165 1113 - /* Reset FIFOs*/ 1166 + /* Reset FIFOs */ 1114 1167 val = SC16IS7XX_FCR_RXRESET_BIT | SC16IS7XX_FCR_TXRESET_BIT; 1115 1168 sc16is7xx_port_write(port, SC16IS7XX_FCR_REG, val); 1116 1169 udelay(5); ··· 1138 1191 /* This bit must be written with LCR[7] = 0 */ 1139 1192 sc16is7xx_port_update(port, SC16IS7XX_MCR_REG, 1140 1193 SC16IS7XX_MCR_IRDA_BIT, 1141 - one->irda_mode ? 1142 - SC16IS7XX_MCR_IRDA_BIT : 0); 1194 + one->irda_mode ? SC16IS7XX_MCR_IRDA_BIT : 0); 1143 1195 1144 1196 /* Enable the Rx and Tx FIFO */ 1145 1197 sc16is7xx_port_update(port, SC16IS7XX_EFCR_REG, ··· 1166 1220 static void sc16is7xx_shutdown(struct uart_port *port) 1167 1221 { 1168 1222 struct sc16is7xx_port *s = dev_get_drvdata(port->dev); 1169 - struct sc16is7xx_one *one = to_sc16is7xx_one(port, port); 1223 + struct sc16is7xx_one *one = to_sc16is7xx_one(port); 1170 1224 1171 1225 kthread_cancel_delayed_work_sync(&one->ms_work); 1172 1226 ··· 1456 1510 return 0; 1457 1511 } 1458 1512 1513 + static int sc16is7xx_setup_channel(struct sc16is7xx_one *one, int i, 1514 + bool *port_registered) 1515 + { 1516 + struct uart_port *port = &one->port; 1517 + int ret; 1518 + 1519 + ret = ida_alloc_max(&sc16is7xx_lines, SC16IS7XX_MAX_DEVS - 1, GFP_KERNEL); 1520 + if (ret < 0) 1521 + return ret; 1522 + 1523 + port->line = ret; 1524 + 1525 + /* Initialize port data */ 1526 + port->type = PORT_SC16IS7XX; 1527 + port->fifosize = SC16IS7XX_FIFO_SIZE; 1528 + port->flags = UPF_FIXED_TYPE | UPF_LOW_LATENCY; 1529 + port->iobase = i; 1530 + /* 1531 + * Use all ones as membase to make sure uart_configure_port() in 1532 + * serial_core.c does not abort for SPI/I2C devices where the 1533 + * membase address is not applicable. 1534 + */ 1535 + port->membase = (void __iomem *)~0; 1536 + port->iotype = UPIO_PORT; 1537 + port->rs485_config = sc16is7xx_config_rs485; 1538 + port->rs485_supported = sc16is7xx_rs485_supported; 1539 + port->ops = &sc16is7xx_ops; 1540 + one->old_mctrl = 0; 1541 + 1542 + mutex_init(&one->lock); 1543 + 1544 + ret = uart_get_rs485_mode(port); 1545 + if (ret) 1546 + return ret; 1547 + 1548 + /* Enable access to general register set */ 1549 + sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, 0x00); 1550 + 1551 + /* Disable all interrupts */ 1552 + sc16is7xx_port_write(port, SC16IS7XX_IER_REG, 0); 1553 + /* Disable TX/RX */ 1554 + sc16is7xx_port_write(port, SC16IS7XX_EFCR_REG, 1555 + SC16IS7XX_EFCR_RXDISABLE_BIT | 1556 + SC16IS7XX_EFCR_TXDISABLE_BIT); 1557 + 1558 + /* Initialize kthread work structs */ 1559 + kthread_init_work(&one->tx_work, sc16is7xx_tx_proc); 1560 + kthread_init_work(&one->reg_work, sc16is7xx_reg_proc); 1561 + kthread_init_delayed_work(&one->ms_work, sc16is7xx_ms_proc); 1562 + 1563 + /* Register port */ 1564 + ret = uart_add_one_port(&sc16is7xx_uart, port); 1565 + if (ret) 1566 + return ret; 1567 + 1568 + *port_registered = true; 1569 + 1570 + sc16is7xx_regs_lock(port, SC16IS7XX_LCR_REG_SET_ENHANCED); 1571 + /* Enable write access to enhanced features */ 1572 + sc16is7xx_port_write(port, SC16IS7XX_EFR_REG, 1573 + SC16IS7XX_EFR_ENABLE_BIT); 1574 + sc16is7xx_regs_unlock(port); 1575 + 1576 + /* Go to suspend mode */ 1577 + sc16is7xx_power(port, 0); 1578 + 1579 + return 0; 1580 + } 1581 + 1459 1582 int sc16is7xx_probe(struct device *dev, const struct sc16is7xx_devtype *devtype, 1460 1583 struct regmap *regmaps[], int irq) 1461 1584 { ··· 1554 1539 1555 1540 /* Alloc port structure */ 1556 1541 s = devm_kzalloc(dev, struct_size(s, p, devtype->nr_uart), GFP_KERNEL); 1557 - if (!s) { 1558 - dev_err(dev, "Error allocating port structure\n"); 1542 + if (!s) 1559 1543 return -ENOMEM; 1560 - } 1561 1544 1562 1545 /* Always ask for fixed clock rate from a property. */ 1563 1546 device_property_read_u32(dev, "clock-frequency", &uartclk); ··· 1608 1595 } 1609 1596 1610 1597 for (i = 0; i < devtype->nr_uart; ++i) { 1611 - ret = ida_alloc_max(&sc16is7xx_lines, 1612 - SC16IS7XX_MAX_DEVS - 1, GFP_KERNEL); 1613 - if (ret < 0) 1614 - goto out_ports; 1615 - 1616 - s->p[i].port.line = ret; 1617 - 1618 - /* Initialize port data */ 1619 1598 s->p[i].port.dev = dev; 1620 1599 s->p[i].port.irq = irq; 1621 - s->p[i].port.type = PORT_SC16IS7XX; 1622 - s->p[i].port.fifosize = SC16IS7XX_FIFO_SIZE; 1623 - s->p[i].port.flags = UPF_FIXED_TYPE | UPF_LOW_LATENCY; 1624 - s->p[i].port.iobase = i; 1625 - /* 1626 - * Use all ones as membase to make sure uart_configure_port() in 1627 - * serial_core.c does not abort for SPI/I2C devices where the 1628 - * membase address is not applicable. 1629 - */ 1630 - s->p[i].port.membase = (void __iomem *)~0; 1631 - s->p[i].port.iotype = UPIO_PORT; 1632 1600 s->p[i].port.uartclk = freq; 1633 - s->p[i].port.rs485_config = sc16is7xx_config_rs485; 1634 - s->p[i].port.rs485_supported = sc16is7xx_rs485_supported; 1635 - s->p[i].port.ops = &sc16is7xx_ops; 1636 - s->p[i].old_mctrl = 0; 1637 1601 s->p[i].regmap = regmaps[i]; 1638 1602 1639 - mutex_init(&s->p[i].efr_lock); 1640 - 1641 - ret = uart_get_rs485_mode(&s->p[i].port); 1603 + ret = sc16is7xx_setup_channel(&s->p[i], i, &port_registered[i]); 1642 1604 if (ret) 1643 1605 goto out_ports; 1644 - 1645 - /* Disable all interrupts */ 1646 - sc16is7xx_port_write(&s->p[i].port, SC16IS7XX_IER_REG, 0); 1647 - /* Disable TX/RX */ 1648 - sc16is7xx_port_write(&s->p[i].port, SC16IS7XX_EFCR_REG, 1649 - SC16IS7XX_EFCR_RXDISABLE_BIT | 1650 - SC16IS7XX_EFCR_TXDISABLE_BIT); 1651 - 1652 - /* Initialize kthread work structs */ 1653 - kthread_init_work(&s->p[i].tx_work, sc16is7xx_tx_proc); 1654 - kthread_init_work(&s->p[i].reg_work, sc16is7xx_reg_proc); 1655 - kthread_init_delayed_work(&s->p[i].ms_work, sc16is7xx_ms_proc); 1656 - 1657 - /* Register port */ 1658 - ret = uart_add_one_port(&sc16is7xx_uart, &s->p[i].port); 1659 - if (ret) 1660 - goto out_ports; 1661 - 1662 - port_registered[i] = true; 1663 - 1664 - /* Enable EFR */ 1665 - sc16is7xx_port_write(&s->p[i].port, SC16IS7XX_LCR_REG, 1666 - SC16IS7XX_LCR_CONF_MODE_B); 1667 - 1668 - regcache_cache_bypass(regmaps[i], true); 1669 - 1670 - /* Enable write access to enhanced features */ 1671 - sc16is7xx_port_write(&s->p[i].port, SC16IS7XX_EFR_REG, 1672 - SC16IS7XX_EFR_ENABLE_BIT); 1673 - 1674 - regcache_cache_bypass(regmaps[i], false); 1675 - 1676 - /* Restore access to general registers */ 1677 - sc16is7xx_port_write(&s->p[i].port, SC16IS7XX_LCR_REG, 0x00); 1678 - 1679 - /* Go to suspend mode */ 1680 - sc16is7xx_power(&s->p[i].port, 0); 1681 1606 } 1682 1607 1683 1608 sc16is7xx_setup_irda_ports(s); ··· 1765 1814 1766 1815 MODULE_LICENSE("GPL"); 1767 1816 MODULE_AUTHOR("Jon Ringle <jringle@gridpoint.com>"); 1768 - MODULE_DESCRIPTION("SC16IS7xx tty serial core driver"); 1817 + MODULE_DESCRIPTION(KBUILD_MODNAME " tty serial core driver");
-1
drivers/tty/serial/sc16is7xx.h
··· 8 8 #include <linux/regmap.h> 9 9 #include <linux/types.h> 10 10 11 - #define SC16IS7XX_NAME "sc16is7xx" 12 11 #define SC16IS7XX_MAX_PORTS 2 /* Maximum number of UART ports per IC. */ 13 12 14 13 struct device;
+2 -2
drivers/tty/serial/sc16is7xx_i2c.c
··· 52 52 53 53 static struct i2c_driver sc16is7xx_i2c_driver = { 54 54 .driver = { 55 - .name = SC16IS7XX_NAME, 55 + .name = KBUILD_MODNAME, 56 56 .of_match_table = sc16is7xx_dt_ids, 57 57 }, 58 58 .probe = sc16is7xx_i2c_probe, ··· 63 63 module_i2c_driver(sc16is7xx_i2c_driver); 64 64 65 65 MODULE_LICENSE("GPL"); 66 - MODULE_DESCRIPTION("SC16IS7xx I2C interface driver"); 66 + MODULE_DESCRIPTION(KBUILD_MODNAME " interface driver"); 67 67 MODULE_IMPORT_NS("SERIAL_NXP_SC16IS7XX");
+2 -2
drivers/tty/serial/sc16is7xx_spi.c
··· 75 75 76 76 static struct spi_driver sc16is7xx_spi_driver = { 77 77 .driver = { 78 - .name = SC16IS7XX_NAME, 78 + .name = KBUILD_MODNAME, 79 79 .of_match_table = sc16is7xx_dt_ids, 80 80 }, 81 81 .probe = sc16is7xx_spi_probe, ··· 86 86 module_spi_driver(sc16is7xx_spi_driver); 87 87 88 88 MODULE_LICENSE("GPL"); 89 - MODULE_DESCRIPTION("SC16IS7xx SPI interface driver"); 89 + MODULE_DESCRIPTION(KBUILD_MODNAME " interface driver"); 90 90 MODULE_IMPORT_NS("SERIAL_NXP_SC16IS7XX");
+76 -102
drivers/tty/serial/serial_core.c
··· 1034 1034 { 1035 1035 struct uart_state *state = tty->driver_data; 1036 1036 struct tty_port *port = &state->port; 1037 - int retval; 1038 1037 1039 - down_write(&tty->termios_rwsem); 1038 + guard(rwsem_write)(&tty->termios_rwsem); 1040 1039 /* 1041 1040 * This semaphore protects port->count. It is also 1042 1041 * very useful to prevent opens. Also, take the ··· 1043 1044 * module insertion/removal doesn't change anything 1044 1045 * under us. 1045 1046 */ 1046 - mutex_lock(&port->mutex); 1047 - retval = uart_set_info(tty, port, state, ss); 1048 - mutex_unlock(&port->mutex); 1049 - up_write(&tty->termios_rwsem); 1050 - return retval; 1047 + guard(mutex)(&port->mutex); 1048 + return uart_set_info(tty, port, state, ss); 1051 1049 } 1052 1050 1053 1051 /** ··· 1556 1560 void __user *uarg = (void __user *)arg; 1557 1561 int ret = -ENOIOCTLCMD; 1558 1562 1559 - 1560 - /* 1561 - * These ioctls don't rely on the hardware to be present. 1562 - */ 1563 - switch (cmd) { 1564 - case TIOCSERCONFIG: 1565 - down_write(&tty->termios_rwsem); 1566 - ret = uart_do_autoconfig(tty, state); 1567 - up_write(&tty->termios_rwsem); 1568 - break; 1563 + /* This ioctl doesn't rely on the hardware to be present. */ 1564 + if (cmd == TIOCSERCONFIG) { 1565 + guard(rwsem_write)(&tty->termios_rwsem); 1566 + return uart_do_autoconfig(tty, state); 1569 1567 } 1570 1568 1571 - if (ret != -ENOIOCTLCMD) 1572 - goto out; 1569 + if (tty_io_error(tty)) 1570 + return -EIO; 1573 1571 1574 - if (tty_io_error(tty)) { 1575 - ret = -EIO; 1576 - goto out; 1577 - } 1578 - 1579 - /* 1580 - * The following should only be used when hardware is present. 1581 - */ 1582 - switch (cmd) { 1583 - case TIOCMIWAIT: 1584 - ret = uart_wait_modem_status(state, arg); 1585 - break; 1586 - } 1587 - 1588 - if (ret != -ENOIOCTLCMD) 1589 - goto out; 1572 + /* This should only be used when the hardware is present. */ 1573 + if (cmd == TIOCMIWAIT) 1574 + return uart_wait_modem_status(state, arg); 1590 1575 1591 1576 /* rs485_config requires more locking than others */ 1592 1577 if (cmd == TIOCSRS485) 1593 1578 down_write(&tty->termios_rwsem); 1594 1579 1595 - mutex_lock(&port->mutex); 1596 - uport = uart_port_check(state); 1580 + scoped_guard(mutex, &port->mutex) { 1581 + uport = uart_port_check(state); 1597 1582 1598 - if (!uport || tty_io_error(tty)) { 1599 - ret = -EIO; 1600 - goto out_up; 1583 + if (!uport || tty_io_error(tty)) { 1584 + ret = -EIO; 1585 + break; 1586 + } 1587 + 1588 + /* 1589 + * All these rely on hardware being present and need to be 1590 + * protected against the tty being hung up. 1591 + */ 1592 + 1593 + switch (cmd) { 1594 + case TIOCSERGETLSR: /* Get line status register */ 1595 + ret = uart_get_lsr_info(tty, state, uarg); 1596 + break; 1597 + 1598 + case TIOCGRS485: 1599 + ret = uart_get_rs485_config(uport, uarg); 1600 + break; 1601 + 1602 + case TIOCSRS485: 1603 + ret = uart_set_rs485_config(tty, uport, uarg); 1604 + break; 1605 + 1606 + case TIOCSISO7816: 1607 + ret = uart_set_iso7816_config(state->uart_port, uarg); 1608 + break; 1609 + 1610 + case TIOCGISO7816: 1611 + ret = uart_get_iso7816_config(state->uart_port, uarg); 1612 + break; 1613 + default: 1614 + if (uport->ops->ioctl) 1615 + ret = uport->ops->ioctl(uport, cmd, arg); 1616 + break; 1617 + } 1601 1618 } 1602 1619 1603 - /* 1604 - * All these rely on hardware being present and need to be 1605 - * protected against the tty being hung up. 1606 - */ 1607 - 1608 - switch (cmd) { 1609 - case TIOCSERGETLSR: /* Get line status register */ 1610 - ret = uart_get_lsr_info(tty, state, uarg); 1611 - break; 1612 - 1613 - case TIOCGRS485: 1614 - ret = uart_get_rs485_config(uport, uarg); 1615 - break; 1616 - 1617 - case TIOCSRS485: 1618 - ret = uart_set_rs485_config(tty, uport, uarg); 1619 - break; 1620 - 1621 - case TIOCSISO7816: 1622 - ret = uart_set_iso7816_config(state->uart_port, uarg); 1623 - break; 1624 - 1625 - case TIOCGISO7816: 1626 - ret = uart_get_iso7816_config(state->uart_port, uarg); 1627 - break; 1628 - default: 1629 - if (uport->ops->ioctl) 1630 - ret = uport->ops->ioctl(uport, cmd, arg); 1631 - break; 1632 - } 1633 - out_up: 1634 - mutex_unlock(&port->mutex); 1635 1620 if (cmd == TIOCSRS485) 1636 1621 up_write(&tty->termios_rwsem); 1637 - out: 1622 + 1638 1623 return ret; 1639 1624 } 1640 1625 ··· 1628 1651 if (!tty_port_initialized(port)) 1629 1652 return; 1630 1653 1631 - mutex_lock(&state->port.mutex); 1654 + guard(mutex)(&state->port.mutex); 1632 1655 uport = uart_port_check(state); 1633 1656 if (uport && uport->ops->set_ldisc) 1634 1657 uport->ops->set_ldisc(uport, &tty->termios); 1635 - mutex_unlock(&state->port.mutex); 1636 1658 } 1637 1659 1638 1660 static void uart_set_termios(struct tty_struct *tty, ··· 1705 1729 1706 1730 state = drv->state + tty->index; 1707 1731 port = &state->port; 1708 - spin_lock_irq(&port->lock); 1732 + guard(spinlock_irq)(&port->lock); 1709 1733 --port->count; 1710 - spin_unlock_irq(&port->lock); 1711 1734 return; 1712 1735 } 1713 1736 ··· 1818 1843 struct uart_state *state = tty->driver_data; 1819 1844 struct tty_port *port = &state->port; 1820 1845 struct uart_port *uport; 1821 - unsigned long flags; 1822 1846 1823 1847 pr_debug("uart_hangup(%d)\n", tty->index); 1824 1848 1825 - mutex_lock(&port->mutex); 1849 + guard(mutex)(&port->mutex); 1826 1850 uport = uart_port_check(state); 1827 1851 WARN(!uport, "hangup of detached port!\n"); 1828 1852 1829 1853 if (tty_port_active(port)) { 1830 1854 uart_flush_buffer(tty); 1831 1855 uart_shutdown(tty, state); 1832 - spin_lock_irqsave(&port->lock, flags); 1833 - port->count = 0; 1834 - spin_unlock_irqrestore(&port->lock, flags); 1856 + scoped_guard(spinlock_irqsave, &port->lock) 1857 + port->count = 0; 1835 1858 tty_port_set_active(port, false); 1836 1859 tty_port_tty_set(port, NULL); 1837 1860 if (uport && !uart_console(uport)) ··· 1837 1864 wake_up_interruptible(&port->open_wait); 1838 1865 wake_up_interruptible(&port->delta_msr_wait); 1839 1866 } 1840 - mutex_unlock(&port->mutex); 1841 1867 } 1842 1868 1843 1869 /* uport == NULL if uart_port has already been removed */ ··· 2941 2969 struct uart_port *uport; 2942 2970 bool console = false; 2943 2971 2944 - mutex_lock(&port->mutex); 2945 - uport = uart_port_check(state); 2946 - if (uport) 2947 - console = uart_console_registered(uport); 2948 - mutex_unlock(&port->mutex); 2972 + scoped_guard(mutex, &port->mutex) { 2973 + uport = uart_port_check(state); 2974 + if (uport) 2975 + console = uart_console_registered(uport); 2976 + } 2949 2977 2950 2978 return sprintf(buf, "%c\n", console ? 'Y' : 'N'); 2951 2979 } ··· 3130 3158 struct tty_port *port = &state->port; 3131 3159 struct uart_port *uart_port; 3132 3160 3133 - mutex_lock(&port->mutex); 3134 - uart_port = uart_port_check(state); 3135 - if (uart_port != uport) 3136 - dev_alert(uport->dev, "Removing wrong port: %p != %p\n", 3137 - uart_port, uport); 3161 + scoped_guard(mutex, &port->mutex) { 3162 + uart_port = uart_port_check(state); 3163 + if (uart_port != uport) 3164 + dev_alert(uport->dev, "Removing wrong port: %p != %p\n", uart_port, uport); 3138 3165 3139 - if (!uart_port) { 3140 - mutex_unlock(&port->mutex); 3141 - return; 3166 + if (!uart_port) 3167 + return; 3142 3168 } 3143 - mutex_unlock(&port->mutex); 3144 3169 3145 3170 /* 3146 3171 * Remove the devices from the tty layer ··· 3166 3197 uport->type = PORT_UNKNOWN; 3167 3198 uport->port_dev = NULL; 3168 3199 3169 - mutex_lock(&port->mutex); 3200 + guard(mutex)(&port->mutex); 3170 3201 WARN_ON(atomic_dec_return(&state->refcount) < 0); 3171 3202 wait_event(state->remove_wait, !atomic_read(&state->refcount)); 3172 3203 state->uart_port = NULL; 3173 - mutex_unlock(&port->mutex); 3174 3204 } 3175 3205 3176 3206 /** ··· 3322 3354 struct serial_ctrl_device *ctrl_dev = serial_core_get_ctrl_dev(port_dev); 3323 3355 int ctrl_id = port->ctrl_id; 3324 3356 3325 - mutex_lock(&port_mutex); 3357 + guard(mutex)(&port_mutex); 3326 3358 3327 3359 port->flags |= UPF_DEAD; 3328 3360 ··· 3334 3366 /* Drop the serial core controller device if no ports are using it */ 3335 3367 if (!serial_core_ctrl_find(drv, phys_dev, ctrl_id)) 3336 3368 serial_base_ctrl_device_remove(ctrl_dev); 3337 - 3338 - mutex_unlock(&port_mutex); 3339 3369 } 3340 3370 3341 3371 /** ··· 3500 3534 int ret; 3501 3535 3502 3536 if (!(port->rs485_supported.flags & SER_RS485_ENABLED)) 3537 + return 0; 3538 + 3539 + /* 3540 + * Retrieve properties only if a firmware node exists. If no firmware 3541 + * node exists, then don't touch rs485 config and keep initial rs485 3542 + * properties set by driver. 3543 + */ 3544 + if (!dev_fwnode(dev)) 3503 3545 return 0; 3504 3546 3505 3547 ret = device_property_read_u32_array(dev, "rs485-rts-delay",
+189 -7
drivers/tty/serial/sh-sci.c
··· 17 17 */ 18 18 #undef DEBUG 19 19 20 + #include <linux/bitops.h> 20 21 #include <linux/clk.h> 21 22 #include <linux/console.h> 22 - #include <linux/ctype.h> 23 23 #include <linux/cpufreq.h> 24 + #include <linux/ctype.h> 24 25 #include <linux/delay.h> 25 - #include <linux/dmaengine.h> 26 26 #include <linux/dma-mapping.h> 27 + #include <linux/dmaengine.h> 27 28 #include <linux/err.h> 28 29 #include <linux/errno.h> 29 30 #include <linux/init.h> 30 31 #include <linux/interrupt.h> 32 + #include <linux/io.h> 31 33 #include <linux/ioport.h> 32 34 #include <linux/ktime.h> 33 35 #include <linux/major.h> 34 36 #include <linux/minmax.h> 35 - #include <linux/module.h> 36 37 #include <linux/mm.h> 38 + #include <linux/module.h> 37 39 #include <linux/of.h> 38 40 #include <linux/platform_device.h> 39 41 #include <linux/pm_runtime.h> 40 42 #include <linux/reset.h> 41 43 #include <linux/scatterlist.h> 42 44 #include <linux/serial.h> 45 + #include <linux/serial_core.h> 43 46 #include <linux/serial_sci.h> 44 47 #include <linux/sh_dma.h> 45 48 #include <linux/slab.h> ··· 53 50 #include <linux/tty_flip.h> 54 51 55 52 #ifdef CONFIG_SUPERH 56 - #include <asm/sh_bios.h> 57 53 #include <asm/platform_early.h> 54 + #include <asm/sh_bios.h> 58 55 #endif 59 56 60 57 #include "rsci.h" 61 58 #include "serial_mctrl_gpio.h" 62 - #include "sh-sci.h" 63 59 #include "sh-sci-common.h" 60 + 61 + #define SCI_MAJOR 204 62 + #define SCI_MINOR_START 8 63 + 64 + /* 65 + * SCI register subset common for all port types. 66 + * Not all registers will exist on all parts. 67 + */ 68 + enum { 69 + SCSMR, /* Serial Mode Register */ 70 + SCBRR, /* Bit Rate Register */ 71 + SCSCR, /* Serial Control Register */ 72 + SCxSR, /* Serial Status Register */ 73 + SCFCR, /* FIFO Control Register */ 74 + SCFDR, /* FIFO Data Count Register */ 75 + SCxTDR, /* Transmit (FIFO) Data Register */ 76 + SCxRDR, /* Receive (FIFO) Data Register */ 77 + SCLSR, /* Line Status Register */ 78 + SCTFDR, /* Transmit FIFO Data Count Register */ 79 + SCRFDR, /* Receive FIFO Data Count Register */ 80 + SCSPTR, /* Serial Port Register */ 81 + HSSRR, /* Sampling Rate Register */ 82 + SCPCR, /* Serial Port Control Register */ 83 + SCPDR, /* Serial Port Data Register */ 84 + SCDL, /* BRG Frequency Division Register */ 85 + SCCKS, /* BRG Clock Select Register */ 86 + HSRTRGR, /* Rx FIFO Data Count Trigger Register */ 87 + HSTTRGR, /* Tx FIFO Data Count Trigger Register */ 88 + SEMR, /* Serial extended mode register */ 89 + }; 90 + 91 + /* SCSMR (Serial Mode Register) */ 92 + #define SCSMR_C_A BIT(7) /* Communication Mode */ 93 + #define SCSMR_CSYNC BIT(7) /* - Clocked synchronous mode */ 94 + #define SCSMR_ASYNC 0 /* - Asynchronous mode */ 95 + #define SCSMR_CHR BIT(6) /* 7-bit Character Length */ 96 + #define SCSMR_PE BIT(5) /* Parity Enable */ 97 + #define SCSMR_ODD BIT(4) /* Odd Parity */ 98 + #define SCSMR_STOP BIT(3) /* Stop Bit Length */ 99 + #define SCSMR_CKS 0x0003 /* Clock Select */ 100 + 101 + /* Serial Mode Register, SCIFA/SCIFB only bits */ 102 + #define SCSMR_CKEDG BIT(12) /* Transmit/Receive Clock Edge Select */ 103 + #define SCSMR_SRC_MASK 0x0700 /* Sampling Control */ 104 + #define SCSMR_SRC_16 0x0000 /* Sampling rate 1/16 */ 105 + #define SCSMR_SRC_5 0x0100 /* Sampling rate 1/5 */ 106 + #define SCSMR_SRC_7 0x0200 /* Sampling rate 1/7 */ 107 + #define SCSMR_SRC_11 0x0300 /* Sampling rate 1/11 */ 108 + #define SCSMR_SRC_13 0x0400 /* Sampling rate 1/13 */ 109 + #define SCSMR_SRC_17 0x0500 /* Sampling rate 1/17 */ 110 + #define SCSMR_SRC_19 0x0600 /* Sampling rate 1/19 */ 111 + #define SCSMR_SRC_27 0x0700 /* Sampling rate 1/27 */ 112 + 113 + /* Serial Control Register, SCI only bits */ 114 + #define SCSCR_TEIE BIT(2) /* Transmit End Interrupt Enable */ 115 + 116 + /* Serial Control Register, SCIFA/SCIFB only bits */ 117 + #define SCSCR_TDRQE BIT(15) /* Tx Data Transfer Request Enable */ 118 + #define SCSCR_RDRQE BIT(14) /* Rx Data Transfer Request Enable */ 119 + 120 + /* Serial Control Register, HSCIF-only bits */ 121 + #define HSSCR_TOT_SHIFT 14 122 + 123 + /* SCxSR (Serial Status Register) on SCI */ 124 + #define SCI_TDRE BIT(7) /* Transmit Data Register Empty */ 125 + #define SCI_RDRF BIT(6) /* Receive Data Register Full */ 126 + #define SCI_ORER BIT(5) /* Overrun Error */ 127 + #define SCI_FER BIT(4) /* Framing Error */ 128 + #define SCI_PER BIT(3) /* Parity Error */ 129 + #define SCI_TEND BIT(2) /* Transmit End */ 130 + #define SCI_RESERVED 0x03 /* All reserved bits */ 131 + 132 + #define SCI_DEFAULT_ERROR_MASK (SCI_PER | SCI_FER) 133 + 134 + #define SCI_RDxF_CLEAR (u32)(~(SCI_RESERVED | SCI_RDRF)) 135 + #define SCI_ERROR_CLEAR (u32)(~(SCI_RESERVED | SCI_PER | SCI_FER | SCI_ORER)) 136 + #define SCI_TDxE_CLEAR (u32)(~(SCI_RESERVED | SCI_TEND | SCI_TDRE)) 137 + #define SCI_BREAK_CLEAR (u32)(~(SCI_RESERVED | SCI_PER | SCI_FER | SCI_ORER)) 138 + 139 + /* SCxSR (Serial Status Register) on SCIF, SCIFA, SCIFB, HSCIF */ 140 + #define SCIF_ER BIT(7) /* Receive Error */ 141 + #define SCIF_TEND BIT(6) /* Transmission End */ 142 + #define SCIF_TDFE BIT(5) /* Transmit FIFO Data Empty */ 143 + #define SCIF_BRK BIT(4) /* Break Detect */ 144 + #define SCIF_FER BIT(3) /* Framing Error */ 145 + #define SCIF_PER BIT(2) /* Parity Error */ 146 + #define SCIF_RDF BIT(1) /* Receive FIFO Data Full */ 147 + #define SCIF_DR BIT(0) /* Receive Data Ready */ 148 + /* SCIF only (optional) */ 149 + #define SCIF_PERC 0xf000 /* Number of Parity Errors */ 150 + #define SCIF_FERC 0x0f00 /* Number of Framing Errors */ 151 + /*SCIFA/SCIFB and SCIF on SH7705/SH7720/SH7721 only */ 152 + #define SCIFA_ORER BIT(9) /* Overrun Error */ 153 + 154 + #define SCIF_DEFAULT_ERROR_MASK (SCIF_PER | SCIF_FER | SCIF_BRK | SCIF_ER) 155 + 156 + #define SCIF_RDxF_CLEAR (u32)(~(SCIF_DR | SCIF_RDF)) 157 + #define SCIF_ERROR_CLEAR (u32)(~(SCIF_PER | SCIF_FER | SCIF_ER)) 158 + #define SCIF_TDxE_CLEAR (u32)(~(SCIF_TDFE)) 159 + #define SCIF_BREAK_CLEAR (u32)(~(SCIF_PER | SCIF_FER | SCIF_BRK)) 160 + 161 + /* SCFCR (FIFO Control Register) */ 162 + #define SCFCR_RTRG1 BIT(7) /* Receive FIFO Data Count Trigger */ 163 + #define SCFCR_RTRG0 BIT(6) 164 + #define SCFCR_TTRG1 BIT(5) /* Transmit FIFO Data Count Trigger */ 165 + #define SCFCR_TTRG0 BIT(4) 166 + #define SCFCR_MCE BIT(3) /* Modem Control Enable */ 167 + #define SCFCR_TFRST BIT(2) /* Transmit FIFO Data Register Reset */ 168 + #define SCFCR_RFRST BIT(1) /* Receive FIFO Data Register Reset */ 169 + #define SCFCR_LOOP BIT(0) /* Loopback Test */ 170 + 171 + /* SCLSR (Line Status Register) on (H)SCIF */ 172 + #define SCLSR_TO BIT(2) /* Timeout */ 173 + #define SCLSR_ORER BIT(0) /* Overrun Error */ 174 + 175 + /* SCSPTR (Serial Port Register), optional */ 176 + #define SCSPTR_RTSIO BIT(7) /* Serial Port RTS# Pin Input/Output */ 177 + #define SCSPTR_RTSDT BIT(6) /* Serial Port RTS# Pin Data */ 178 + #define SCSPTR_CTSIO BIT(5) /* Serial Port CTS# Pin Input/Output */ 179 + #define SCSPTR_CTSDT BIT(4) /* Serial Port CTS# Pin Data */ 180 + #define SCSPTR_SCKIO BIT(3) /* Serial Port Clock Pin Input/Output */ 181 + #define SCSPTR_SCKDT BIT(2) /* Serial Port Clock Pin Data */ 182 + #define SCSPTR_SPB2IO BIT(1) /* Serial Port Break Input/Output */ 183 + #define SCSPTR_SPB2DT BIT(0) /* Serial Port Break Data */ 184 + 185 + /* HSSRR HSCIF */ 186 + #define HSCIF_SRE BIT(15) /* Sampling Rate Register Enable */ 187 + #define HSCIF_SRDE BIT(14) /* Sampling Point Register Enable */ 188 + 189 + #define HSCIF_SRHP_SHIFT 8 190 + #define HSCIF_SRHP_MASK 0x0f00 191 + 192 + /* SCPCR (Serial Port Control Register), SCIFA/SCIFB only */ 193 + #define SCPCR_RTSC BIT(4) /* Serial Port RTS# Pin / Output Pin */ 194 + #define SCPCR_CTSC BIT(3) /* Serial Port CTS# Pin / Input Pin */ 195 + #define SCPCR_SCKC BIT(2) /* Serial Port SCK Pin / Output Pin */ 196 + #define SCPCR_RXDC BIT(1) /* Serial Port RXD Pin / Input Pin */ 197 + #define SCPCR_TXDC BIT(0) /* Serial Port TXD Pin / Output Pin */ 198 + 199 + /* SCPDR (Serial Port Data Register), SCIFA/SCIFB only */ 200 + #define SCPDR_RTSD BIT(4) /* Serial Port RTS# Output Pin Data */ 201 + #define SCPDR_CTSD BIT(3) /* Serial Port CTS# Input Pin Data */ 202 + #define SCPDR_SCKD BIT(2) /* Serial Port SCK Output Pin Data */ 203 + #define SCPDR_RXDD BIT(1) /* Serial Port RXD Input Pin Data */ 204 + #define SCPDR_TXDD BIT(0) /* Serial Port TXD Output Pin Data */ 205 + 206 + /* 207 + * BRG Clock Select Register (Some SCIF and HSCIF) 208 + * The Baud Rate Generator for external clock can provide a clock source for 209 + * the sampling clock. It outputs either its frequency divided clock, or the 210 + * (undivided) (H)SCK external clock. 211 + */ 212 + #define SCCKS_CKS BIT(15) /* Select (H)SCK (1) or divided SC_CLK (0) */ 213 + #define SCCKS_XIN BIT(14) /* SC_CLK uses bus clock (1) or SCIF_CLK (0) */ 214 + 215 + #define SCxSR_TEND(port) (((port)->type == PORT_SCI) ? SCI_TEND : SCIF_TEND) 216 + #define SCxSR_RDxF(port) (((port)->type == PORT_SCI) ? SCI_RDRF : SCIF_DR | SCIF_RDF) 217 + #define SCxSR_TDxE(port) (((port)->type == PORT_SCI) ? SCI_TDRE : SCIF_TDFE) 218 + #define SCxSR_FER(port) (((port)->type == PORT_SCI) ? SCI_FER : SCIF_FER) 219 + #define SCxSR_PER(port) (((port)->type == PORT_SCI) ? SCI_PER : SCIF_PER) 220 + #define SCxSR_BRK(port) (((port)->type == PORT_SCI) ? 0x00 : SCIF_BRK) 221 + 222 + #define SCxSR_ERRORS(port) (to_sci_port(port)->params->error_mask) 223 + 224 + #define SCxSR_RDxF_CLEAR(port) \ 225 + (((port)->type == PORT_SCI) ? SCI_RDxF_CLEAR : SCIF_RDxF_CLEAR) 226 + #define SCxSR_ERROR_CLEAR(port) \ 227 + (to_sci_port(port)->params->error_clear) 228 + #define SCxSR_TDxE_CLEAR(port) \ 229 + (((port)->type == PORT_SCI) ? SCI_TDxE_CLEAR : SCIF_TDxE_CLEAR) 230 + #define SCxSR_BREAK_CLEAR(port) \ 231 + (((port)->type == PORT_SCI) ? SCI_BREAK_CLEAR : SCIF_BREAK_CLEAR) 64 232 65 233 #define SCIx_IRQ_IS_MUXED(port) \ 66 234 ((port)->irqs[SCIx_ERI_IRQ] == \ ··· 1198 1024 1199 1025 status = s->ops->read_reg(port, s->params->overrun_reg); 1200 1026 if (status & s->params->overrun_mask) { 1201 - status &= ~s->params->overrun_mask; 1202 - s->ops->write_reg(port, s->params->overrun_reg, status); 1027 + if (s->type == SCI_PORT_RSCI) { 1028 + /* 1029 + * All of the CFCLR_*C clearing bits match the corresponding 1030 + * CSR_*status bits. So, reuse the overrun mask for clearing. 1031 + */ 1032 + s->ops->clear_SCxSR(port, s->params->overrun_mask); 1033 + } else { 1034 + status &= ~s->params->overrun_mask; 1035 + s->ops->write_reg(port, s->params->overrun_reg, status); 1036 + } 1203 1037 1204 1038 port->icount.overrun++; 1205 1039
-178
drivers/tty/serial/sh-sci.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0 */ 2 - #include <linux/bitops.h> 3 - #include <linux/serial_core.h> 4 - #include <linux/io.h> 5 - 6 - #define SCI_MAJOR 204 7 - #define SCI_MINOR_START 8 8 - 9 - 10 - /* 11 - * SCI register subset common for all port types. 12 - * Not all registers will exist on all parts. 13 - */ 14 - enum { 15 - SCSMR, /* Serial Mode Register */ 16 - SCBRR, /* Bit Rate Register */ 17 - SCSCR, /* Serial Control Register */ 18 - SCxSR, /* Serial Status Register */ 19 - SCFCR, /* FIFO Control Register */ 20 - SCFDR, /* FIFO Data Count Register */ 21 - SCxTDR, /* Transmit (FIFO) Data Register */ 22 - SCxRDR, /* Receive (FIFO) Data Register */ 23 - SCLSR, /* Line Status Register */ 24 - SCTFDR, /* Transmit FIFO Data Count Register */ 25 - SCRFDR, /* Receive FIFO Data Count Register */ 26 - SCSPTR, /* Serial Port Register */ 27 - HSSRR, /* Sampling Rate Register */ 28 - SCPCR, /* Serial Port Control Register */ 29 - SCPDR, /* Serial Port Data Register */ 30 - SCDL, /* BRG Frequency Division Register */ 31 - SCCKS, /* BRG Clock Select Register */ 32 - HSRTRGR, /* Rx FIFO Data Count Trigger Register */ 33 - HSTTRGR, /* Tx FIFO Data Count Trigger Register */ 34 - SEMR, /* Serial extended mode register */ 35 - }; 36 - 37 - 38 - /* SCSMR (Serial Mode Register) */ 39 - #define SCSMR_C_A BIT(7) /* Communication Mode */ 40 - #define SCSMR_CSYNC BIT(7) /* - Clocked synchronous mode */ 41 - #define SCSMR_ASYNC 0 /* - Asynchronous mode */ 42 - #define SCSMR_CHR BIT(6) /* 7-bit Character Length */ 43 - #define SCSMR_PE BIT(5) /* Parity Enable */ 44 - #define SCSMR_ODD BIT(4) /* Odd Parity */ 45 - #define SCSMR_STOP BIT(3) /* Stop Bit Length */ 46 - #define SCSMR_CKS 0x0003 /* Clock Select */ 47 - 48 - /* Serial Mode Register, SCIFA/SCIFB only bits */ 49 - #define SCSMR_CKEDG BIT(12) /* Transmit/Receive Clock Edge Select */ 50 - #define SCSMR_SRC_MASK 0x0700 /* Sampling Control */ 51 - #define SCSMR_SRC_16 0x0000 /* Sampling rate 1/16 */ 52 - #define SCSMR_SRC_5 0x0100 /* Sampling rate 1/5 */ 53 - #define SCSMR_SRC_7 0x0200 /* Sampling rate 1/7 */ 54 - #define SCSMR_SRC_11 0x0300 /* Sampling rate 1/11 */ 55 - #define SCSMR_SRC_13 0x0400 /* Sampling rate 1/13 */ 56 - #define SCSMR_SRC_17 0x0500 /* Sampling rate 1/17 */ 57 - #define SCSMR_SRC_19 0x0600 /* Sampling rate 1/19 */ 58 - #define SCSMR_SRC_27 0x0700 /* Sampling rate 1/27 */ 59 - 60 - /* Serial Control Register, SCI only bits */ 61 - #define SCSCR_TEIE BIT(2) /* Transmit End Interrupt Enable */ 62 - 63 - /* Serial Control Register, SCIFA/SCIFB only bits */ 64 - #define SCSCR_TDRQE BIT(15) /* Tx Data Transfer Request Enable */ 65 - #define SCSCR_RDRQE BIT(14) /* Rx Data Transfer Request Enable */ 66 - 67 - /* Serial Control Register, HSCIF-only bits */ 68 - #define HSSCR_TOT_SHIFT 14 69 - 70 - /* SCxSR (Serial Status Register) on SCI */ 71 - #define SCI_TDRE BIT(7) /* Transmit Data Register Empty */ 72 - #define SCI_RDRF BIT(6) /* Receive Data Register Full */ 73 - #define SCI_ORER BIT(5) /* Overrun Error */ 74 - #define SCI_FER BIT(4) /* Framing Error */ 75 - #define SCI_PER BIT(3) /* Parity Error */ 76 - #define SCI_TEND BIT(2) /* Transmit End */ 77 - #define SCI_RESERVED 0x03 /* All reserved bits */ 78 - 79 - #define SCI_DEFAULT_ERROR_MASK (SCI_PER | SCI_FER) 80 - 81 - #define SCI_RDxF_CLEAR (u32)(~(SCI_RESERVED | SCI_RDRF)) 82 - #define SCI_ERROR_CLEAR (u32)(~(SCI_RESERVED | SCI_PER | SCI_FER | SCI_ORER)) 83 - #define SCI_TDxE_CLEAR (u32)(~(SCI_RESERVED | SCI_TEND | SCI_TDRE)) 84 - #define SCI_BREAK_CLEAR (u32)(~(SCI_RESERVED | SCI_PER | SCI_FER | SCI_ORER)) 85 - 86 - /* SCxSR (Serial Status Register) on SCIF, SCIFA, SCIFB, HSCIF */ 87 - #define SCIF_ER BIT(7) /* Receive Error */ 88 - #define SCIF_TEND BIT(6) /* Transmission End */ 89 - #define SCIF_TDFE BIT(5) /* Transmit FIFO Data Empty */ 90 - #define SCIF_BRK BIT(4) /* Break Detect */ 91 - #define SCIF_FER BIT(3) /* Framing Error */ 92 - #define SCIF_PER BIT(2) /* Parity Error */ 93 - #define SCIF_RDF BIT(1) /* Receive FIFO Data Full */ 94 - #define SCIF_DR BIT(0) /* Receive Data Ready */ 95 - /* SCIF only (optional) */ 96 - #define SCIF_PERC 0xf000 /* Number of Parity Errors */ 97 - #define SCIF_FERC 0x0f00 /* Number of Framing Errors */ 98 - /*SCIFA/SCIFB and SCIF on SH7705/SH7720/SH7721 only */ 99 - #define SCIFA_ORER BIT(9) /* Overrun Error */ 100 - 101 - #define SCIF_DEFAULT_ERROR_MASK (SCIF_PER | SCIF_FER | SCIF_BRK | SCIF_ER) 102 - 103 - #define SCIF_RDxF_CLEAR (u32)(~(SCIF_DR | SCIF_RDF)) 104 - #define SCIF_ERROR_CLEAR (u32)(~(SCIF_PER | SCIF_FER | SCIF_ER)) 105 - #define SCIF_TDxE_CLEAR (u32)(~(SCIF_TDFE)) 106 - #define SCIF_BREAK_CLEAR (u32)(~(SCIF_PER | SCIF_FER | SCIF_BRK)) 107 - 108 - /* SCFCR (FIFO Control Register) */ 109 - #define SCFCR_RTRG1 BIT(7) /* Receive FIFO Data Count Trigger */ 110 - #define SCFCR_RTRG0 BIT(6) 111 - #define SCFCR_TTRG1 BIT(5) /* Transmit FIFO Data Count Trigger */ 112 - #define SCFCR_TTRG0 BIT(4) 113 - #define SCFCR_MCE BIT(3) /* Modem Control Enable */ 114 - #define SCFCR_TFRST BIT(2) /* Transmit FIFO Data Register Reset */ 115 - #define SCFCR_RFRST BIT(1) /* Receive FIFO Data Register Reset */ 116 - #define SCFCR_LOOP BIT(0) /* Loopback Test */ 117 - 118 - /* SCLSR (Line Status Register) on (H)SCIF */ 119 - #define SCLSR_TO BIT(2) /* Timeout */ 120 - #define SCLSR_ORER BIT(0) /* Overrun Error */ 121 - 122 - /* SCSPTR (Serial Port Register), optional */ 123 - #define SCSPTR_RTSIO BIT(7) /* Serial Port RTS# Pin Input/Output */ 124 - #define SCSPTR_RTSDT BIT(6) /* Serial Port RTS# Pin Data */ 125 - #define SCSPTR_CTSIO BIT(5) /* Serial Port CTS# Pin Input/Output */ 126 - #define SCSPTR_CTSDT BIT(4) /* Serial Port CTS# Pin Data */ 127 - #define SCSPTR_SCKIO BIT(3) /* Serial Port Clock Pin Input/Output */ 128 - #define SCSPTR_SCKDT BIT(2) /* Serial Port Clock Pin Data */ 129 - #define SCSPTR_SPB2IO BIT(1) /* Serial Port Break Input/Output */ 130 - #define SCSPTR_SPB2DT BIT(0) /* Serial Port Break Data */ 131 - 132 - /* HSSRR HSCIF */ 133 - #define HSCIF_SRE BIT(15) /* Sampling Rate Register Enable */ 134 - #define HSCIF_SRDE BIT(14) /* Sampling Point Register Enable */ 135 - 136 - #define HSCIF_SRHP_SHIFT 8 137 - #define HSCIF_SRHP_MASK 0x0f00 138 - 139 - /* SCPCR (Serial Port Control Register), SCIFA/SCIFB only */ 140 - #define SCPCR_RTSC BIT(4) /* Serial Port RTS# Pin / Output Pin */ 141 - #define SCPCR_CTSC BIT(3) /* Serial Port CTS# Pin / Input Pin */ 142 - #define SCPCR_SCKC BIT(2) /* Serial Port SCK Pin / Output Pin */ 143 - #define SCPCR_RXDC BIT(1) /* Serial Port RXD Pin / Input Pin */ 144 - #define SCPCR_TXDC BIT(0) /* Serial Port TXD Pin / Output Pin */ 145 - 146 - /* SCPDR (Serial Port Data Register), SCIFA/SCIFB only */ 147 - #define SCPDR_RTSD BIT(4) /* Serial Port RTS# Output Pin Data */ 148 - #define SCPDR_CTSD BIT(3) /* Serial Port CTS# Input Pin Data */ 149 - #define SCPDR_SCKD BIT(2) /* Serial Port SCK Output Pin Data */ 150 - #define SCPDR_RXDD BIT(1) /* Serial Port RXD Input Pin Data */ 151 - #define SCPDR_TXDD BIT(0) /* Serial Port TXD Output Pin Data */ 152 - 153 - /* 154 - * BRG Clock Select Register (Some SCIF and HSCIF) 155 - * The Baud Rate Generator for external clock can provide a clock source for 156 - * the sampling clock. It outputs either its frequency divided clock, or the 157 - * (undivided) (H)SCK external clock. 158 - */ 159 - #define SCCKS_CKS BIT(15) /* Select (H)SCK (1) or divided SC_CLK (0) */ 160 - #define SCCKS_XIN BIT(14) /* SC_CLK uses bus clock (1) or SCIF_CLK (0) */ 161 - 162 - #define SCxSR_TEND(port) (((port)->type == PORT_SCI) ? SCI_TEND : SCIF_TEND) 163 - #define SCxSR_RDxF(port) (((port)->type == PORT_SCI) ? SCI_RDRF : SCIF_DR | SCIF_RDF) 164 - #define SCxSR_TDxE(port) (((port)->type == PORT_SCI) ? SCI_TDRE : SCIF_TDFE) 165 - #define SCxSR_FER(port) (((port)->type == PORT_SCI) ? SCI_FER : SCIF_FER) 166 - #define SCxSR_PER(port) (((port)->type == PORT_SCI) ? SCI_PER : SCIF_PER) 167 - #define SCxSR_BRK(port) (((port)->type == PORT_SCI) ? 0x00 : SCIF_BRK) 168 - 169 - #define SCxSR_ERRORS(port) (to_sci_port(port)->params->error_mask) 170 - 171 - #define SCxSR_RDxF_CLEAR(port) \ 172 - (((port)->type == PORT_SCI) ? SCI_RDxF_CLEAR : SCIF_RDxF_CLEAR) 173 - #define SCxSR_ERROR_CLEAR(port) \ 174 - (to_sci_port(port)->params->error_clear) 175 - #define SCxSR_TDxE_CLEAR(port) \ 176 - (((port)->type == PORT_SCI) ? SCI_TDxE_CLEAR : SCIF_TDxE_CLEAR) 177 - #define SCxSR_BREAK_CLEAR(port) \ 178 - (((port)->type == PORT_SCI) ? SCI_BREAK_CLEAR : SCIF_BREAK_CLEAR)
+6
drivers/tty/serial/sprd_serial.c
··· 1133 1133 1134 1134 clk_uart = devm_clk_get(uport->dev, "uart"); 1135 1135 if (IS_ERR(clk_uart)) { 1136 + if (PTR_ERR(clk_uart) == -EPROBE_DEFER) 1137 + return -EPROBE_DEFER; 1138 + 1136 1139 dev_warn(uport->dev, "uart%d can't get uart clock\n", 1137 1140 uport->line); 1138 1141 clk_uart = NULL; ··· 1143 1140 1144 1141 clk_parent = devm_clk_get(uport->dev, "source"); 1145 1142 if (IS_ERR(clk_parent)) { 1143 + if (PTR_ERR(clk_parent) == -EPROBE_DEFER) 1144 + return -EPROBE_DEFER; 1145 + 1146 1146 dev_warn(uport->dev, "uart%d can't get source clock\n", 1147 1147 uport->line); 1148 1148 clk_parent = NULL;
+5 -10
drivers/tty/serial/xilinx_uartps.c
··· 190 190 * @port: Pointer to the UART port 191 191 * @uartclk: Reference clock 192 192 * @pclk: APB clock 193 - * @cdns_uart_driver: Pointer to UART driver 194 193 * @baud: Current baud rate 195 194 * @clk_rate_change_nb: Notifier block for clock changes 196 195 * @quirks: Flags for RXBS support. ··· 203 204 struct uart_port *port; 204 205 struct clk *uartclk; 205 206 struct clk *pclk; 206 - struct uart_driver *cdns_uart_driver; 207 207 unsigned int baud; 208 208 struct notifier_block clk_rate_change_nb; 209 209 u32 quirks; ··· 1463 1465 static int cdns_uart_suspend(struct device *device) 1464 1466 { 1465 1467 struct uart_port *port = dev_get_drvdata(device); 1466 - struct cdns_uart *cdns_uart = port->private_data; 1467 1468 int may_wake; 1468 1469 1469 1470 may_wake = device_may_wakeup(device); ··· 1486 1489 * Call the API provided in serial_core.c file which handles 1487 1490 * the suspend. 1488 1491 */ 1489 - return uart_suspend_port(cdns_uart->cdns_uart_driver, port); 1492 + return uart_suspend_port(&cdns_uart_uart_driver, port); 1490 1493 } 1491 1494 1492 1495 /** ··· 1547 1550 uart_port_unlock_irqrestore(port, flags); 1548 1551 } 1549 1552 1550 - return uart_resume_port(cdns_uart->cdns_uart_driver, port); 1553 + return uart_resume_port(&cdns_uart_uart_driver, port); 1551 1554 } 1552 1555 #endif /* ! CONFIG_PM_SLEEP */ 1553 1556 static int __maybe_unused cdns_runtime_suspend(struct device *dev) ··· 1682 1685 return rc; 1683 1686 } 1684 1687 } 1685 - 1686 - cdns_uart_data->cdns_uart_driver = &cdns_uart_uart_driver; 1687 1688 1688 1689 match = of_match_node(cdns_uart_of_match, pdev->dev.of_node); 1689 1690 if (match && match->data) { ··· 1857 1862 clk_disable_unprepare(cdns_uart_data->pclk); 1858 1863 err_out_unregister_driver: 1859 1864 if (!instances) 1860 - uart_unregister_driver(cdns_uart_data->cdns_uart_driver); 1865 + uart_unregister_driver(&cdns_uart_uart_driver); 1861 1866 return rc; 1862 1867 } 1863 1868 ··· 1875 1880 clk_notifier_unregister(cdns_uart_data->uartclk, 1876 1881 &cdns_uart_data->clk_rate_change_nb); 1877 1882 #endif 1878 - uart_remove_one_port(cdns_uart_data->cdns_uart_driver, port); 1883 + uart_remove_one_port(&cdns_uart_uart_driver, port); 1879 1884 port->mapbase = 0; 1880 1885 clk_disable_unprepare(cdns_uart_data->uartclk); 1881 1886 clk_disable_unprepare(cdns_uart_data->pclk); ··· 1891 1896 reset_control_assert(cdns_uart_data->rstc); 1892 1897 1893 1898 if (!--instances) 1894 - uart_unregister_driver(cdns_uart_data->cdns_uart_driver); 1899 + uart_unregister_driver(&cdns_uart_uart_driver); 1895 1900 } 1896 1901 1897 1902 static struct platform_driver cdns_uart_platform_driver = {
+4 -4
drivers/tty/tty_buffer.c
··· 76 76 mutex_unlock(&buf->lock); 77 77 78 78 if (restart) 79 - queue_work(system_unbound_wq, &buf->work); 79 + queue_work(system_dfl_wq, &buf->work); 80 80 } 81 81 EXPORT_SYMBOL_GPL(tty_buffer_unlock_exclusive); 82 82 ··· 530 530 struct tty_bufhead *buf = &port->buf; 531 531 532 532 tty_flip_buffer_commit(buf->tail); 533 - queue_work(system_unbound_wq, &buf->work); 533 + queue_work(system_dfl_wq, &buf->work); 534 534 } 535 535 EXPORT_SYMBOL(tty_flip_buffer_push); 536 536 ··· 560 560 tty_flip_buffer_commit(buf->tail); 561 561 spin_unlock_irqrestore(&port->lock, flags); 562 562 563 - queue_work(system_unbound_wq, &buf->work); 563 + queue_work(system_dfl_wq, &buf->work); 564 564 565 565 return size; 566 566 } ··· 613 613 614 614 bool tty_buffer_restart_work(struct tty_port *port) 615 615 { 616 - return queue_work(system_unbound_wq, &port->buf.work); 616 + return queue_work(system_dfl_wq, &port->buf.work); 617 617 } 618 618 619 619 bool tty_buffer_cancel_work(struct tty_port *port)
+120 -198
drivers/tty/vt/keyboard.c
··· 424 424 /* We still have to export this method to vt.c */ 425 425 void vt_set_leds_compute_shiftstate(void) 426 426 { 427 - unsigned long flags; 428 - 429 427 /* 430 428 * When VT is switched, the keyboard led needs to be set once. 431 429 * Ensure that after the switch is completed, the state of the ··· 432 434 vt_switch = true; 433 435 set_leds(); 434 436 435 - spin_lock_irqsave(&kbd_event_lock, flags); 437 + guard(spinlock_irqsave)(&kbd_event_lock); 436 438 do_compute_shiftstate(); 437 - spin_unlock_irqrestore(&kbd_event_lock, flags); 438 439 } 439 440 440 441 /* ··· 622 625 623 626 static void fn_spawn_con(struct vc_data *vc) 624 627 { 625 - spin_lock(&vt_spawn_con.lock); 628 + guard(spinlock)(&vt_spawn_con.lock); 626 629 if (vt_spawn_con.pid) 627 630 if (kill_pid(vt_spawn_con.pid, vt_spawn_con.sig, 1)) { 628 631 put_pid(vt_spawn_con.pid); 629 632 vt_spawn_con.pid = NULL; 630 633 } 631 - spin_unlock(&vt_spawn_con.lock); 632 634 } 633 635 634 636 static void fn_SAK(struct vc_data *vc) ··· 758 762 return; 759 763 760 764 if ((unsigned)value < ARRAY_SIZE(func_table)) { 761 - unsigned long flags; 762 - 763 - spin_lock_irqsave(&func_buf_lock, flags); 765 + guard(spinlock_irqsave)(&func_buf_lock); 764 766 if (func_table[value]) 765 767 puts_queue(vc, func_table[value]); 766 - spin_unlock_irqrestore(&func_buf_lock, flags); 767 - 768 768 } else 769 769 pr_err("k_fn called with value=%d\n", value); 770 770 } ··· 1132 1140 1133 1141 void setledstate(struct kbd_struct *kb, unsigned int led) 1134 1142 { 1135 - unsigned long flags; 1136 - spin_lock_irqsave(&led_lock, flags); 1143 + guard(spinlock_irqsave)(&led_lock); 1137 1144 if (!(led & ~7)) { 1138 1145 ledioctl = led; 1139 1146 kb->ledmode = LED_SHOW_IOCTL; ··· 1140 1149 kb->ledmode = LED_SHOW_FLAGS; 1141 1150 1142 1151 set_leds(); 1143 - spin_unlock_irqrestore(&led_lock, flags); 1144 1152 } 1145 1153 1146 1154 static inline unsigned char getleds(void) ··· 1162 1172 int vt_get_leds(unsigned int console, int flag) 1163 1173 { 1164 1174 struct kbd_struct *kb = &kbd_table[console]; 1165 - int ret; 1166 - unsigned long flags; 1167 1175 1168 - spin_lock_irqsave(&led_lock, flags); 1169 - ret = vc_kbd_led(kb, flag); 1170 - spin_unlock_irqrestore(&led_lock, flags); 1171 - 1172 - return ret; 1176 + guard(spinlock_irqsave)(&led_lock); 1177 + return vc_kbd_led(kb, flag); 1173 1178 } 1174 1179 EXPORT_SYMBOL_GPL(vt_get_leds); 1175 1180 ··· 1198 1213 void vt_kbd_con_start(unsigned int console) 1199 1214 { 1200 1215 struct kbd_struct *kb = &kbd_table[console]; 1201 - unsigned long flags; 1202 - spin_lock_irqsave(&led_lock, flags); 1216 + 1217 + guard(spinlock_irqsave)(&led_lock); 1203 1218 clr_vc_kbd_led(kb, VC_SCROLLOCK); 1204 1219 set_leds(); 1205 - spin_unlock_irqrestore(&led_lock, flags); 1206 1220 } 1207 1221 1208 1222 /** ··· 1214 1230 void vt_kbd_con_stop(unsigned int console) 1215 1231 { 1216 1232 struct kbd_struct *kb = &kbd_table[console]; 1217 - unsigned long flags; 1218 - spin_lock_irqsave(&led_lock, flags); 1233 + 1234 + guard(spinlock_irqsave)(&led_lock); 1219 1235 set_vc_kbd_led(kb, VC_SCROLLOCK); 1220 1236 set_leds(); 1221 - spin_unlock_irqrestore(&led_lock, flags); 1222 1237 } 1223 1238 1224 1239 /* ··· 1229 1246 static void kbd_bh(struct tasklet_struct *unused) 1230 1247 { 1231 1248 unsigned int leds; 1232 - unsigned long flags; 1233 1249 1234 - spin_lock_irqsave(&led_lock, flags); 1235 - leds = getleds(); 1236 - leds |= (unsigned int)kbd->lockstate << 8; 1237 - spin_unlock_irqrestore(&led_lock, flags); 1250 + scoped_guard(spinlock_irqsave, &led_lock) { 1251 + leds = getleds(); 1252 + leds |= (unsigned int)kbd->lockstate << 8; 1253 + } 1238 1254 1239 1255 if (vt_switch) { 1240 1256 ledstate = ~leds; ··· 1507 1525 unsigned int event_code, int value) 1508 1526 { 1509 1527 /* We are called with interrupts disabled, just take the lock */ 1510 - spin_lock(&kbd_event_lock); 1511 - 1512 - if (event_type == EV_MSC && event_code == MSC_RAW && 1513 - kbd_is_hw_raw(handle->dev)) 1514 - kbd_rawcode(value); 1515 - if (event_type == EV_KEY && event_code <= KEY_MAX) 1516 - kbd_keycode(event_code, value, kbd_is_hw_raw(handle->dev)); 1517 - 1518 - spin_unlock(&kbd_event_lock); 1528 + scoped_guard(spinlock, &kbd_event_lock) { 1529 + if (event_type == EV_MSC && event_code == MSC_RAW && 1530 + kbd_is_hw_raw(handle->dev)) 1531 + kbd_rawcode(value); 1532 + if (event_type == EV_KEY && event_code <= KEY_MAX) 1533 + kbd_keycode(event_code, value, kbd_is_hw_raw(handle->dev)); 1534 + } 1519 1535 1520 1536 tasklet_schedule(&keyboard_tasklet); 1521 1537 do_poke_blanked_console = 1; ··· 1546 1566 static int kbd_connect(struct input_handler *handler, struct input_dev *dev, 1547 1567 const struct input_device_id *id) 1548 1568 { 1549 - struct input_handle *handle; 1550 1569 int error; 1551 1570 1552 - handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL); 1571 + struct input_handle __free(kfree) *handle = kzalloc(sizeof(*handle), GFP_KERNEL); 1553 1572 if (!handle) 1554 1573 return -ENOMEM; 1555 1574 ··· 1558 1579 1559 1580 error = input_register_handle(handle); 1560 1581 if (error) 1561 - goto err_free_handle; 1582 + return error; 1562 1583 1563 1584 error = input_open_device(handle); 1564 1585 if (error) 1565 1586 goto err_unregister_handle; 1566 1587 1588 + retain_and_null_ptr(handle); 1589 + 1567 1590 return 0; 1568 1591 1569 1592 err_unregister_handle: 1570 1593 input_unregister_handle(handle); 1571 - err_free_handle: 1572 - kfree(handle); 1573 1594 return error; 1574 1595 } 1575 1596 ··· 1660 1681 */ 1661 1682 int vt_do_diacrit(unsigned int cmd, void __user *udp, int perm) 1662 1683 { 1663 - unsigned long flags; 1664 1684 int asize; 1665 - int ret = 0; 1666 1685 1667 1686 switch (cmd) { 1668 1687 case KDGKBDIACR: 1669 1688 { 1670 1689 struct kbdiacrs __user *a = udp; 1671 - struct kbdiacr *dia; 1672 1690 int i; 1673 1691 1674 - dia = kmalloc_array(MAX_DIACR, sizeof(struct kbdiacr), 1675 - GFP_KERNEL); 1692 + struct kbdiacr __free(kfree) *dia = kmalloc_array(MAX_DIACR, sizeof(struct kbdiacr), 1693 + GFP_KERNEL); 1676 1694 if (!dia) 1677 1695 return -ENOMEM; 1678 1696 1679 1697 /* Lock the diacriticals table, make a copy and then 1680 1698 copy it after we unlock */ 1681 - spin_lock_irqsave(&kbd_event_lock, flags); 1682 - 1683 - asize = accent_table_size; 1684 - for (i = 0; i < asize; i++) { 1685 - dia[i].diacr = conv_uni_to_8bit( 1686 - accent_table[i].diacr); 1687 - dia[i].base = conv_uni_to_8bit( 1688 - accent_table[i].base); 1689 - dia[i].result = conv_uni_to_8bit( 1690 - accent_table[i].result); 1699 + scoped_guard(spinlock_irqsave, &kbd_event_lock) { 1700 + asize = accent_table_size; 1701 + for (i = 0; i < asize; i++) { 1702 + dia[i].diacr = conv_uni_to_8bit(accent_table[i].diacr); 1703 + dia[i].base = conv_uni_to_8bit(accent_table[i].base); 1704 + dia[i].result = conv_uni_to_8bit(accent_table[i].result); 1705 + } 1691 1706 } 1692 - spin_unlock_irqrestore(&kbd_event_lock, flags); 1693 1707 1694 1708 if (put_user(asize, &a->kb_cnt)) 1695 - ret = -EFAULT; 1696 - else if (copy_to_user(a->kbdiacr, dia, 1697 - asize * sizeof(struct kbdiacr))) 1698 - ret = -EFAULT; 1699 - kfree(dia); 1700 - return ret; 1709 + return -EFAULT; 1710 + if (copy_to_user(a->kbdiacr, dia, asize * sizeof(struct kbdiacr))) 1711 + return -EFAULT; 1712 + return 0; 1701 1713 } 1702 1714 case KDGKBDIACRUC: 1703 1715 { 1704 1716 struct kbdiacrsuc __user *a = udp; 1705 - void *buf; 1706 1717 1707 - buf = kmalloc_array(MAX_DIACR, sizeof(struct kbdiacruc), 1708 - GFP_KERNEL); 1718 + void __free(kfree) *buf = kmalloc_array(MAX_DIACR, sizeof(struct kbdiacruc), 1719 + GFP_KERNEL); 1709 1720 if (buf == NULL) 1710 1721 return -ENOMEM; 1711 1722 1712 1723 /* Lock the diacriticals table, make a copy and then 1713 1724 copy it after we unlock */ 1714 - spin_lock_irqsave(&kbd_event_lock, flags); 1715 - 1716 - asize = accent_table_size; 1717 - memcpy(buf, accent_table, asize * sizeof(struct kbdiacruc)); 1718 - 1719 - spin_unlock_irqrestore(&kbd_event_lock, flags); 1725 + scoped_guard(spinlock_irqsave, &kbd_event_lock) { 1726 + asize = accent_table_size; 1727 + memcpy(buf, accent_table, asize * sizeof(struct kbdiacruc)); 1728 + } 1720 1729 1721 1730 if (put_user(asize, &a->kb_cnt)) 1722 - ret = -EFAULT; 1723 - else if (copy_to_user(a->kbdiacruc, buf, 1724 - asize*sizeof(struct kbdiacruc))) 1725 - ret = -EFAULT; 1726 - kfree(buf); 1727 - return ret; 1731 + return -EFAULT; 1732 + if (copy_to_user(a->kbdiacruc, buf, asize * sizeof(struct kbdiacruc))) 1733 + return -EFAULT; 1734 + 1735 + return 0; 1728 1736 } 1729 1737 1730 1738 case KDSKBDIACR: 1731 1739 { 1732 1740 struct kbdiacrs __user *a = udp; 1733 - struct kbdiacr *dia = NULL; 1741 + struct kbdiacr __free(kfree) *dia = NULL; 1734 1742 unsigned int ct; 1735 1743 int i; 1736 1744 ··· 1735 1769 return PTR_ERR(dia); 1736 1770 } 1737 1771 1738 - spin_lock_irqsave(&kbd_event_lock, flags); 1772 + guard(spinlock_irqsave)(&kbd_event_lock); 1739 1773 accent_table_size = ct; 1740 1774 for (i = 0; i < ct; i++) { 1741 1775 accent_table[i].diacr = ··· 1745 1779 accent_table[i].result = 1746 1780 conv_8bit_to_uni(dia[i].result); 1747 1781 } 1748 - spin_unlock_irqrestore(&kbd_event_lock, flags); 1749 - kfree(dia); 1782 + 1750 1783 return 0; 1751 1784 } 1752 1785 ··· 1753 1788 { 1754 1789 struct kbdiacrsuc __user *a = udp; 1755 1790 unsigned int ct; 1756 - void *buf = NULL; 1791 + void __free(kfree) *buf = NULL; 1757 1792 1758 1793 if (!perm) 1759 1794 return -EPERM; ··· 1769 1804 ct, sizeof(struct kbdiacruc)); 1770 1805 if (IS_ERR(buf)) 1771 1806 return PTR_ERR(buf); 1772 - } 1773 - spin_lock_irqsave(&kbd_event_lock, flags); 1807 + } 1808 + guard(spinlock_irqsave)(&kbd_event_lock); 1774 1809 if (ct) 1775 1810 memcpy(accent_table, buf, 1776 1811 ct * sizeof(struct kbdiacruc)); 1777 1812 accent_table_size = ct; 1778 - spin_unlock_irqrestore(&kbd_event_lock, flags); 1779 - kfree(buf); 1780 1813 return 0; 1781 1814 } 1782 1815 } 1783 - return ret; 1816 + return 0; 1784 1817 } 1785 1818 1786 1819 /** ··· 1792 1829 int vt_do_kdskbmode(unsigned int console, unsigned int arg) 1793 1830 { 1794 1831 struct kbd_struct *kb = &kbd_table[console]; 1795 - int ret = 0; 1796 - unsigned long flags; 1797 1832 1798 - spin_lock_irqsave(&kbd_event_lock, flags); 1833 + guard(spinlock_irqsave)(&kbd_event_lock); 1799 1834 switch(arg) { 1800 1835 case K_RAW: 1801 1836 kb->kbdmode = VC_RAW; 1802 - break; 1837 + return 0; 1803 1838 case K_MEDIUMRAW: 1804 1839 kb->kbdmode = VC_MEDIUMRAW; 1805 - break; 1840 + return 0; 1806 1841 case K_XLATE: 1807 1842 kb->kbdmode = VC_XLATE; 1808 1843 do_compute_shiftstate(); 1809 - break; 1844 + return 0; 1810 1845 case K_UNICODE: 1811 1846 kb->kbdmode = VC_UNICODE; 1812 1847 do_compute_shiftstate(); 1813 - break; 1848 + return 0; 1814 1849 case K_OFF: 1815 1850 kb->kbdmode = VC_OFF; 1816 - break; 1851 + return 0; 1817 1852 default: 1818 - ret = -EINVAL; 1853 + return -EINVAL; 1819 1854 } 1820 - spin_unlock_irqrestore(&kbd_event_lock, flags); 1821 - return ret; 1822 1855 } 1823 1856 1824 1857 /** ··· 1828 1869 int vt_do_kdskbmeta(unsigned int console, unsigned int arg) 1829 1870 { 1830 1871 struct kbd_struct *kb = &kbd_table[console]; 1831 - int ret = 0; 1832 - unsigned long flags; 1833 1872 1834 - spin_lock_irqsave(&kbd_event_lock, flags); 1873 + guard(spinlock_irqsave)(&kbd_event_lock); 1835 1874 switch(arg) { 1836 1875 case K_METABIT: 1837 1876 clr_vc_kbd_mode(kb, VC_META); 1838 - break; 1877 + return 0; 1839 1878 case K_ESCPREFIX: 1840 1879 set_vc_kbd_mode(kb, VC_META); 1841 - break; 1880 + return 0; 1842 1881 default: 1843 - ret = -EINVAL; 1882 + return -EINVAL; 1844 1883 } 1845 - spin_unlock_irqrestore(&kbd_event_lock, flags); 1846 - return ret; 1847 1884 } 1848 1885 1849 - int vt_do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, 1850 - int perm) 1886 + int vt_do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, int perm) 1851 1887 { 1852 1888 struct kbkeycode tmp; 1853 - int kc = 0; 1889 + int kc; 1854 1890 1855 1891 if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode))) 1856 1892 return -EFAULT; 1893 + 1857 1894 switch (cmd) { 1858 1895 case KDGETKEYCODE: 1859 1896 kc = getkeycode(tmp.scancode); 1860 - if (kc >= 0) 1861 - kc = put_user(kc, &user_kbkc->keycode); 1862 - break; 1897 + if (kc < 0) 1898 + return kc; 1899 + return put_user(kc, &user_kbkc->keycode); 1863 1900 case KDSETKEYCODE: 1864 1901 if (!perm) 1865 1902 return -EPERM; 1866 - kc = setkeycode(tmp.scancode, tmp.keycode); 1867 - break; 1903 + return setkeycode(tmp.scancode, tmp.keycode); 1868 1904 } 1869 - return kc; 1905 + 1906 + return 0; 1870 1907 } 1871 1908 1872 1909 static unsigned short vt_kdgkbent(unsigned char kbdmode, unsigned char idx, 1873 1910 unsigned char map) 1874 1911 { 1875 - unsigned short *key_map, val; 1876 - unsigned long flags; 1912 + unsigned short *key_map; 1877 1913 1878 1914 /* Ensure another thread doesn't free it under us */ 1879 - spin_lock_irqsave(&kbd_event_lock, flags); 1915 + guard(spinlock_irqsave)(&kbd_event_lock); 1880 1916 key_map = key_maps[map]; 1881 1917 if (key_map) { 1882 - val = U(key_map[idx]); 1918 + unsigned short val = U(key_map[idx]); 1883 1919 if (kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES) 1884 - val = K_HOLE; 1885 - } else 1886 - val = idx ? K_HOLE : K_NOSUCHMAP; 1887 - spin_unlock_irqrestore(&kbd_event_lock, flags); 1920 + return K_HOLE; 1921 + return val; 1922 + } 1888 1923 1889 - return val; 1924 + return idx ? K_HOLE : K_NOSUCHMAP; 1890 1925 } 1891 1926 1892 1927 static int vt_kdskbent(unsigned char kbdmode, unsigned char idx, 1893 1928 unsigned char map, unsigned short val) 1894 1929 { 1895 - unsigned long flags; 1896 - unsigned short *key_map, *new_map, oldval; 1930 + unsigned short *key_map, oldval; 1897 1931 1898 1932 if (!idx && val == K_NOSUCHMAP) { 1899 - spin_lock_irqsave(&kbd_event_lock, flags); 1933 + guard(spinlock_irqsave)(&kbd_event_lock); 1900 1934 /* deallocate map */ 1901 1935 key_map = key_maps[map]; 1902 1936 if (map && key_map) { ··· 1899 1947 keymap_count--; 1900 1948 } 1901 1949 } 1902 - spin_unlock_irqrestore(&kbd_event_lock, flags); 1903 1950 1904 1951 return 0; 1905 1952 } ··· 1916 1965 return 0; 1917 1966 #endif 1918 1967 1919 - new_map = kmalloc(sizeof(plain_map), GFP_KERNEL); 1968 + unsigned short __free(kfree) *new_map = kmalloc(sizeof(plain_map), GFP_KERNEL); 1920 1969 if (!new_map) 1921 1970 return -ENOMEM; 1922 1971 1923 - spin_lock_irqsave(&kbd_event_lock, flags); 1972 + guard(spinlock_irqsave)(&kbd_event_lock); 1924 1973 key_map = key_maps[map]; 1925 1974 if (key_map == NULL) { 1926 1975 int j; 1927 1976 1928 - if (keymap_count >= MAX_NR_OF_USER_KEYMAPS && 1929 - !capable(CAP_SYS_RESOURCE)) { 1930 - spin_unlock_irqrestore(&kbd_event_lock, flags); 1931 - kfree(new_map); 1977 + if (keymap_count >= MAX_NR_OF_USER_KEYMAPS && !capable(CAP_SYS_RESOURCE)) 1932 1978 return -EPERM; 1933 - } 1934 - key_maps[map] = new_map; 1935 - key_map = new_map; 1979 + 1980 + key_map = key_maps[map] = no_free_ptr(new_map); 1936 1981 key_map[0] = U(K_ALLOCATED); 1937 1982 for (j = 1; j < NR_KEYS; j++) 1938 1983 key_map[j] = U(K_HOLE); 1939 1984 keymap_count++; 1940 - } else 1941 - kfree(new_map); 1985 + } 1942 1986 1943 1987 oldval = U(key_map[idx]); 1944 1988 if (val == oldval) 1945 - goto out; 1989 + return 0; 1946 1990 1947 1991 /* Attention Key */ 1948 - if ((oldval == K_SAK || val == K_SAK) && !capable(CAP_SYS_ADMIN)) { 1949 - spin_unlock_irqrestore(&kbd_event_lock, flags); 1992 + if ((oldval == K_SAK || val == K_SAK) && !capable(CAP_SYS_ADMIN)) 1950 1993 return -EPERM; 1951 - } 1952 1994 1953 1995 key_map[idx] = U(val); 1954 1996 if (!map && (KTYP(oldval) == KT_SHIFT || KTYP(val) == KT_SHIFT)) 1955 1997 do_compute_shiftstate(); 1956 - out: 1957 - spin_unlock_irqrestore(&kbd_event_lock, flags); 1958 1998 1959 1999 return 0; 1960 2000 } ··· 1991 2049 int vt_do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm) 1992 2050 { 1993 2051 unsigned char kb_func; 1994 - unsigned long flags; 1995 - char *kbs; 1996 - int ret; 1997 2052 1998 2053 if (get_user(kb_func, &user_kdgkb->kb_func)) 1999 2054 return -EFAULT; ··· 2002 2063 /* size should have been a struct member */ 2003 2064 ssize_t len = sizeof(user_kdgkb->kb_string); 2004 2065 2005 - kbs = kmalloc(len, GFP_KERNEL); 2066 + char __free(kfree) *kbs = kmalloc(len, GFP_KERNEL); 2006 2067 if (!kbs) 2007 2068 return -ENOMEM; 2008 2069 2009 - spin_lock_irqsave(&func_buf_lock, flags); 2010 - len = strscpy(kbs, func_table[kb_func] ? : "", len); 2011 - spin_unlock_irqrestore(&func_buf_lock, flags); 2070 + scoped_guard(spinlock_irqsave, &func_buf_lock) 2071 + len = strscpy(kbs, func_table[kb_func] ? : "", len); 2012 2072 2013 - if (len < 0) { 2014 - ret = -ENOSPC; 2015 - break; 2016 - } 2017 - ret = copy_to_user(user_kdgkb->kb_string, kbs, len + 1) ? 2018 - -EFAULT : 0; 2019 - break; 2073 + if (len < 0) 2074 + return -ENOSPC; 2075 + 2076 + if (copy_to_user(user_kdgkb->kb_string, kbs, len + 1)) 2077 + return -EFAULT; 2078 + 2079 + return 0; 2020 2080 } 2021 2081 case KDSKBSENT: 2022 2082 if (!perm || !capable(CAP_SYS_TTY_CONFIG)) 2023 2083 return -EPERM; 2024 2084 2025 - kbs = strndup_user(user_kdgkb->kb_string, 2026 - sizeof(user_kdgkb->kb_string)); 2085 + char __free(kfree) *kbs = strndup_user(user_kdgkb->kb_string, 2086 + sizeof(user_kdgkb->kb_string)); 2027 2087 if (IS_ERR(kbs)) 2028 2088 return PTR_ERR(kbs); 2029 2089 2030 - spin_lock_irqsave(&func_buf_lock, flags); 2090 + guard(spinlock_irqsave)(&func_buf_lock); 2031 2091 kbs = vt_kdskbsent(kbs, kb_func); 2032 - spin_unlock_irqrestore(&func_buf_lock, flags); 2033 2092 2034 - ret = 0; 2035 - break; 2093 + return 0; 2036 2094 } 2037 2095 2038 - kfree(kbs); 2039 - 2040 - return ret; 2096 + return 0; 2041 2097 } 2042 2098 2043 2099 int vt_do_kdskled(unsigned int console, int cmd, unsigned long arg, int perm) 2044 2100 { 2045 2101 struct kbd_struct *kb = &kbd_table[console]; 2046 - unsigned long flags; 2047 2102 unsigned char ucval; 2048 2103 2049 2104 switch(cmd) { 2050 2105 /* the ioctls below read/set the flags usually shown in the leds */ 2051 2106 /* don't use them - they will go away without warning */ 2052 2107 case KDGKBLED: 2053 - spin_lock_irqsave(&kbd_event_lock, flags); 2054 - ucval = kb->ledflagstate | (kb->default_ledflagstate << 4); 2055 - spin_unlock_irqrestore(&kbd_event_lock, flags); 2108 + scoped_guard(spinlock_irqsave, &kbd_event_lock) 2109 + ucval = kb->ledflagstate | (kb->default_ledflagstate << 4); 2056 2110 return put_user(ucval, (char __user *)arg); 2057 2111 2058 2112 case KDSKBLED: ··· 2053 2121 return -EPERM; 2054 2122 if (arg & ~0x77) 2055 2123 return -EINVAL; 2056 - spin_lock_irqsave(&led_lock, flags); 2057 - kb->ledflagstate = (arg & 7); 2058 - kb->default_ledflagstate = ((arg >> 4) & 7); 2059 - set_leds(); 2060 - spin_unlock_irqrestore(&led_lock, flags); 2124 + scoped_guard(spinlock_irqsave, &led_lock) { 2125 + kb->ledflagstate = (arg & 7); 2126 + kb->default_ledflagstate = ((arg >> 4) & 7); 2127 + set_leds(); 2128 + } 2061 2129 return 0; 2062 2130 2063 2131 /* the ioctls below only set the lights, not the functions */ ··· 2114 2182 */ 2115 2183 void vt_reset_unicode(unsigned int console) 2116 2184 { 2117 - unsigned long flags; 2118 - 2119 - spin_lock_irqsave(&kbd_event_lock, flags); 2185 + guard(spinlock_irqsave)(&kbd_event_lock); 2120 2186 kbd_table[console].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE; 2121 - spin_unlock_irqrestore(&kbd_event_lock, flags); 2122 2187 } 2123 2188 2124 2189 /** ··· 2140 2211 void vt_reset_keyboard(unsigned int console) 2141 2212 { 2142 2213 struct kbd_struct *kb = &kbd_table[console]; 2143 - unsigned long flags; 2144 2214 2145 - spin_lock_irqsave(&kbd_event_lock, flags); 2215 + guard(spinlock_irqsave)(&kbd_event_lock); 2146 2216 set_vc_kbd_mode(kb, VC_REPEAT); 2147 2217 clr_vc_kbd_mode(kb, VC_CKMODE); 2148 2218 clr_vc_kbd_mode(kb, VC_APPLIC); 2149 2219 clr_vc_kbd_mode(kb, VC_CRLF); 2150 2220 kb->lockstate = 0; 2151 2221 kb->slockstate = 0; 2152 - spin_lock(&led_lock); 2222 + guard(spinlock)(&led_lock); 2153 2223 kb->ledmode = LED_SHOW_FLAGS; 2154 2224 kb->ledflagstate = kb->default_ledflagstate; 2155 - spin_unlock(&led_lock); 2156 2225 /* do not do set_leds here because this causes an endless tasklet loop 2157 2226 when the keyboard hasn't been initialized yet */ 2158 - spin_unlock_irqrestore(&kbd_event_lock, flags); 2159 2227 } 2160 2228 2161 2229 /** ··· 2182 2256 void vt_set_kbd_mode_bit(unsigned int console, int bit) 2183 2257 { 2184 2258 struct kbd_struct *kb = &kbd_table[console]; 2185 - unsigned long flags; 2186 2259 2187 - spin_lock_irqsave(&kbd_event_lock, flags); 2260 + guard(spinlock_irqsave)(&kbd_event_lock); 2188 2261 set_vc_kbd_mode(kb, bit); 2189 - spin_unlock_irqrestore(&kbd_event_lock, flags); 2190 2262 } 2191 2263 2192 2264 /** ··· 2199 2275 void vt_clr_kbd_mode_bit(unsigned int console, int bit) 2200 2276 { 2201 2277 struct kbd_struct *kb = &kbd_table[console]; 2202 - unsigned long flags; 2203 2278 2204 - spin_lock_irqsave(&kbd_event_lock, flags); 2279 + guard(spinlock_irqsave)(&kbd_event_lock); 2205 2280 clr_vc_kbd_mode(kb, bit); 2206 - spin_unlock_irqrestore(&kbd_event_lock, flags); 2207 2281 }
+5 -4
drivers/tty/vt/selection.c
··· 348 348 return 0; 349 349 } 350 350 351 - v->xs = min_t(u16, v->xs - 1, vc->vc_cols - 1); 352 - v->ys = min_t(u16, v->ys - 1, vc->vc_rows - 1); 353 - v->xe = min_t(u16, v->xe - 1, vc->vc_cols - 1); 354 - v->ye = min_t(u16, v->ye - 1, vc->vc_rows - 1); 351 + /* Historically 0 => max value */ 352 + v->xs = umin(v->xs - 1, vc->vc_cols - 1); 353 + v->ys = umin(v->ys - 1, vc->vc_rows - 1); 354 + v->xe = umin(v->xe - 1, vc->vc_cols - 1); 355 + v->ye = umin(v->ye - 1, vc->vc_rows - 1); 355 356 356 357 if (mouse_reporting() && (v->sel_mode & TIOCL_SELMOUSEREPORT)) { 357 358 mouse_report(tty, v->sel_mode & TIOCL_SELBUTTONMASK, v->xs,
+2 -2
drivers/tty/vt/vt.c
··· 4862 4862 return ret; 4863 4863 } 4864 4864 4865 - c = (font.width+7)/8 * vpitch * font.charcount; 4865 + c = DIV_ROUND_UP(font.width, 8) * vpitch * font.charcount; 4866 4866 4867 4867 if (op->data && font.charcount > op->charcount) 4868 4868 return -ENOSPC; ··· 4894 4894 return -EINVAL; 4895 4895 if (vpitch < op->height) 4896 4896 return -EINVAL; 4897 - size = (op->width+7)/8 * vpitch * op->charcount; 4897 + size = DIV_ROUND_UP(op->width, 8) * vpitch * op->charcount; 4898 4898 if (size > max_font_size) 4899 4899 return -ENOSPC; 4900 4900
+1
tools/testing/selftests/tty/.gitignore
··· 1 1 # SPDX-License-Identifier: GPL-2.0-only 2 + tty_tiocsti_test 2 3 tty_tstamp_update
+5 -1
tools/testing/selftests/tty/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0 2 2 CFLAGS = -O2 -Wall 3 - TEST_GEN_PROGS := tty_tstamp_update 3 + TEST_GEN_PROGS := tty_tstamp_update tty_tiocsti_test 4 + LDLIBS += -lcap 4 5 5 6 include ../lib.mk 7 + 8 + # Add libcap for TIOCSTI test 9 + $(OUTPUT)/tty_tiocsti_test: LDLIBS += -lcap
+1
tools/testing/selftests/tty/config
··· 1 + CONFIG_LEGACY_TIOCSTI=y
+650
tools/testing/selftests/tty/tty_tiocsti_test.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * TTY Tests - TIOCSTI 4 + * 5 + * Copyright © 2025 Abhinav Saxena <xandfury@gmail.com> 6 + */ 7 + 8 + #include <stdio.h> 9 + #include <stdlib.h> 10 + #include <unistd.h> 11 + #include <fcntl.h> 12 + #include <sys/ioctl.h> 13 + #include <errno.h> 14 + #include <stdbool.h> 15 + #include <string.h> 16 + #include <sys/socket.h> 17 + #include <sys/wait.h> 18 + #include <pwd.h> 19 + #include <termios.h> 20 + #include <grp.h> 21 + #include <sys/capability.h> 22 + #include <sys/prctl.h> 23 + #include <pty.h> 24 + #include <utmp.h> 25 + 26 + #include "../kselftest_harness.h" 27 + 28 + enum test_type { 29 + TEST_PTY_TIOCSTI_BASIC, 30 + TEST_PTY_TIOCSTI_FD_PASSING, 31 + /* other tests cases such as serial may be added. */ 32 + }; 33 + 34 + /* 35 + * Test Strategy: 36 + * - Basic tests: Use PTY with/without TIOCSCTTY (controlling terminal for 37 + * current process) 38 + * - FD passing tests: Child creates PTY, parent receives FD (demonstrates 39 + * security issue) 40 + * 41 + * SECURITY VULNERABILITY DEMONSTRATION: 42 + * FD passing tests show that TIOCSTI uses CURRENT process credentials, not 43 + * opener credentials. This means privileged processes can be given FDs from 44 + * unprivileged processes and successfully perform TIOCSTI operations that the 45 + * unprivileged process couldn't do directly. 46 + * 47 + * Attack scenario: 48 + * 1. Unprivileged process opens TTY (direct TIOCSTI fails due to lack of 49 + * privileges) 50 + * 2. Unprivileged process passes FD to privileged process via SCM_RIGHTS 51 + * 3. Privileged process can use TIOCSTI on the FD (succeeds due to its 52 + * privileges) 53 + * 4. Result: Effective privilege escalation via file descriptor passing 54 + * 55 + * This matches the kernel logic in tiocsti(): 56 + * 1. if (!tty_legacy_tiocsti && !capable(CAP_SYS_ADMIN)) return -EIO; 57 + * 2. if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN)) 58 + * return -EPERM; 59 + * Note: Both checks use capable() on CURRENT process, not FD opener! 60 + * 61 + * If the file credentials were also checked along with the capable() checks 62 + * then the results for FD pass tests would be consistent with the basic tests. 63 + */ 64 + 65 + FIXTURE(tiocsti) 66 + { 67 + int pty_master_fd; /* PTY - for basic tests */ 68 + int pty_slave_fd; 69 + bool has_pty; 70 + bool initial_cap_sys_admin; 71 + int original_legacy_tiocsti_setting; 72 + bool can_modify_sysctl; 73 + }; 74 + 75 + FIXTURE_VARIANT(tiocsti) 76 + { 77 + const enum test_type test_type; 78 + const bool controlling_tty; /* true=current->signal->tty == tty */ 79 + const int legacy_tiocsti; /* 0=restricted, 1=permissive */ 80 + const bool requires_cap; /* true=with CAP_SYS_ADMIN, false=without */ 81 + const int expected_success; /* 0=success, -EIO/-EPERM=specific error */ 82 + }; 83 + 84 + /* 85 + * Tests Controlling Terminal Variants (current->signal->tty == tty) 86 + * 87 + * TIOCSTI Test Matrix: 88 + * 89 + * | legacy_tiocsti | CAP_SYS_ADMIN | Expected Result | Error | 90 + * |----------------|---------------|-----------------|-------| 91 + * | 1 (permissive) | true | SUCCESS | - | 92 + * | 1 (permissive) | false | SUCCESS | - | 93 + * | 0 (restricted) | true | SUCCESS | - | 94 + * | 0 (restricted) | false | FAILURE | -EIO | 95 + */ 96 + 97 + /* clang-format off */ 98 + FIXTURE_VARIANT_ADD(tiocsti, basic_pty_permissive_withcap) { 99 + .test_type = TEST_PTY_TIOCSTI_BASIC, 100 + .controlling_tty = true, 101 + .legacy_tiocsti = 1, 102 + .requires_cap = true, 103 + .expected_success = 0, 104 + }; 105 + 106 + FIXTURE_VARIANT_ADD(tiocsti, basic_pty_permissive_nocap) { 107 + .test_type = TEST_PTY_TIOCSTI_BASIC, 108 + .controlling_tty = true, 109 + .legacy_tiocsti = 1, 110 + .requires_cap = false, 111 + .expected_success = 0, 112 + }; 113 + 114 + FIXTURE_VARIANT_ADD(tiocsti, basic_pty_restricted_withcap) { 115 + .test_type = TEST_PTY_TIOCSTI_BASIC, 116 + .controlling_tty = true, 117 + .legacy_tiocsti = 0, 118 + .requires_cap = true, 119 + .expected_success = 0, 120 + }; 121 + 122 + FIXTURE_VARIANT_ADD(tiocsti, basic_pty_restricted_nocap) { 123 + .test_type = TEST_PTY_TIOCSTI_BASIC, 124 + .controlling_tty = true, 125 + .legacy_tiocsti = 0, 126 + .requires_cap = false, 127 + .expected_success = -EIO, /* FAILURE: legacy restriction */ 128 + }; /* clang-format on */ 129 + 130 + /* 131 + * Note for FD Passing Test Variants 132 + * Since we're testing the scenario where an unprivileged process pass an FD 133 + * to a privileged one, .requires_cap here means the caps of the child process. 134 + * Not the parent; parent would always be privileged. 135 + */ 136 + 137 + /* clang-format off */ 138 + FIXTURE_VARIANT_ADD(tiocsti, fdpass_pty_permissive_withcap) { 139 + .test_type = TEST_PTY_TIOCSTI_FD_PASSING, 140 + .controlling_tty = true, 141 + .legacy_tiocsti = 1, 142 + .requires_cap = true, 143 + .expected_success = 0, 144 + }; 145 + 146 + FIXTURE_VARIANT_ADD(tiocsti, fdpass_pty_permissive_nocap) { 147 + .test_type = TEST_PTY_TIOCSTI_FD_PASSING, 148 + .controlling_tty = true, 149 + .legacy_tiocsti = 1, 150 + .requires_cap = false, 151 + .expected_success = 0, 152 + }; 153 + 154 + FIXTURE_VARIANT_ADD(tiocsti, fdpass_pty_restricted_withcap) { 155 + .test_type = TEST_PTY_TIOCSTI_FD_PASSING, 156 + .controlling_tty = true, 157 + .legacy_tiocsti = 0, 158 + .requires_cap = true, 159 + .expected_success = 0, 160 + }; 161 + 162 + FIXTURE_VARIANT_ADD(tiocsti, fdpass_pty_restricted_nocap) { 163 + .test_type = TEST_PTY_TIOCSTI_FD_PASSING, 164 + .controlling_tty = true, 165 + .legacy_tiocsti = 0, 166 + .requires_cap = false, 167 + .expected_success = -EIO, 168 + }; /* clang-format on */ 169 + 170 + /* 171 + * Non-Controlling Terminal Variants (current->signal->tty != tty) 172 + * 173 + * TIOCSTI Test Matrix: 174 + * 175 + * | legacy_tiocsti | CAP_SYS_ADMIN | Expected Result | Error | 176 + * |----------------|---------------|-----------------|-------| 177 + * | 1 (permissive) | true | SUCCESS | - | 178 + * | 1 (permissive) | false | FAILURE | -EPERM| 179 + * | 0 (restricted) | true | SUCCESS | - | 180 + * | 0 (restricted) | false | FAILURE | -EIO | 181 + */ 182 + 183 + /* clang-format off */ 184 + FIXTURE_VARIANT_ADD(tiocsti, basic_nopty_permissive_withcap) { 185 + .test_type = TEST_PTY_TIOCSTI_BASIC, 186 + .controlling_tty = false, 187 + .legacy_tiocsti = 1, 188 + .requires_cap = true, 189 + .expected_success = 0, 190 + }; 191 + 192 + FIXTURE_VARIANT_ADD(tiocsti, basic_nopty_permissive_nocap) { 193 + .test_type = TEST_PTY_TIOCSTI_BASIC, 194 + .controlling_tty = false, 195 + .legacy_tiocsti = 1, 196 + .requires_cap = false, 197 + .expected_success = -EPERM, 198 + }; 199 + 200 + FIXTURE_VARIANT_ADD(tiocsti, basic_nopty_restricted_withcap) { 201 + .test_type = TEST_PTY_TIOCSTI_BASIC, 202 + .controlling_tty = false, 203 + .legacy_tiocsti = 0, 204 + .requires_cap = true, 205 + .expected_success = 0, 206 + }; 207 + 208 + FIXTURE_VARIANT_ADD(tiocsti, basic_nopty_restricted_nocap) { 209 + .test_type = TEST_PTY_TIOCSTI_BASIC, 210 + .controlling_tty = false, 211 + .legacy_tiocsti = 0, 212 + .requires_cap = false, 213 + .expected_success = -EIO, 214 + }; 215 + 216 + FIXTURE_VARIANT_ADD(tiocsti, fdpass_nopty_permissive_withcap) { 217 + .test_type = TEST_PTY_TIOCSTI_FD_PASSING, 218 + .controlling_tty = false, 219 + .legacy_tiocsti = 1, 220 + .requires_cap = true, 221 + .expected_success = 0, 222 + }; 223 + 224 + FIXTURE_VARIANT_ADD(tiocsti, fdpass_nopty_permissive_nocap) { 225 + .test_type = TEST_PTY_TIOCSTI_FD_PASSING, 226 + .controlling_tty = false, 227 + .legacy_tiocsti = 1, 228 + .requires_cap = false, 229 + .expected_success = -EPERM, 230 + }; 231 + 232 + FIXTURE_VARIANT_ADD(tiocsti, fdpass_nopty_restricted_withcap) { 233 + .test_type = TEST_PTY_TIOCSTI_FD_PASSING, 234 + .controlling_tty = false, 235 + .legacy_tiocsti = 0, 236 + .requires_cap = true, 237 + .expected_success = 0, 238 + }; 239 + 240 + FIXTURE_VARIANT_ADD(tiocsti, fdpass_nopty_restricted_nocap) { 241 + .test_type = TEST_PTY_TIOCSTI_FD_PASSING, 242 + .controlling_tty = false, 243 + .legacy_tiocsti = 0, 244 + .requires_cap = false, 245 + .expected_success = -EIO, 246 + }; /* clang-format on */ 247 + 248 + /* Helper function to send FD via SCM_RIGHTS */ 249 + static int send_fd_via_socket(int socket_fd, int fd_to_send) 250 + { 251 + struct msghdr msg = { 0 }; 252 + struct cmsghdr *cmsg; 253 + char cmsg_buf[CMSG_SPACE(sizeof(int))]; 254 + char dummy_data = 'F'; 255 + struct iovec iov = { .iov_base = &dummy_data, .iov_len = 1 }; 256 + 257 + msg.msg_iov = &iov; 258 + msg.msg_iovlen = 1; 259 + msg.msg_control = cmsg_buf; 260 + msg.msg_controllen = sizeof(cmsg_buf); 261 + 262 + cmsg = CMSG_FIRSTHDR(&msg); 263 + cmsg->cmsg_level = SOL_SOCKET; 264 + cmsg->cmsg_type = SCM_RIGHTS; 265 + cmsg->cmsg_len = CMSG_LEN(sizeof(int)); 266 + 267 + memcpy(CMSG_DATA(cmsg), &fd_to_send, sizeof(int)); 268 + 269 + return sendmsg(socket_fd, &msg, 0) < 0 ? -1 : 0; 270 + } 271 + 272 + /* Helper function to receive FD via SCM_RIGHTS */ 273 + static int recv_fd_via_socket(int socket_fd) 274 + { 275 + struct msghdr msg = { 0 }; 276 + struct cmsghdr *cmsg; 277 + char cmsg_buf[CMSG_SPACE(sizeof(int))]; 278 + char dummy_data; 279 + struct iovec iov = { .iov_base = &dummy_data, .iov_len = 1 }; 280 + int received_fd = -1; 281 + 282 + msg.msg_iov = &iov; 283 + msg.msg_iovlen = 1; 284 + msg.msg_control = cmsg_buf; 285 + msg.msg_controllen = sizeof(cmsg_buf); 286 + 287 + if (recvmsg(socket_fd, &msg, 0) < 0) 288 + return -1; 289 + 290 + for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { 291 + if (cmsg->cmsg_level == SOL_SOCKET && 292 + cmsg->cmsg_type == SCM_RIGHTS) { 293 + memcpy(&received_fd, CMSG_DATA(cmsg), sizeof(int)); 294 + break; 295 + } 296 + } 297 + 298 + return received_fd; 299 + } 300 + 301 + static inline bool has_cap_sys_admin(void) 302 + { 303 + cap_t caps = cap_get_proc(); 304 + 305 + if (!caps) 306 + return false; 307 + 308 + cap_flag_value_t cap_val; 309 + bool has_cap = (cap_get_flag(caps, CAP_SYS_ADMIN, CAP_EFFECTIVE, 310 + &cap_val) == 0) && 311 + (cap_val == CAP_SET); 312 + 313 + cap_free(caps); 314 + return has_cap; 315 + } 316 + 317 + /* 318 + * Switch to non-root user and clear all capabilities 319 + */ 320 + static inline bool drop_all_privs(struct __test_metadata *_metadata) 321 + { 322 + /* Drop supplementary groups */ 323 + ASSERT_EQ(setgroups(0, NULL), 0); 324 + 325 + /* Switch to non-root user */ 326 + ASSERT_EQ(setgid(1000), 0); 327 + ASSERT_EQ(setuid(1000), 0); 328 + 329 + /* Clear all capabilities */ 330 + cap_t empty = cap_init(); 331 + 332 + ASSERT_NE(empty, NULL); 333 + ASSERT_EQ(cap_set_proc(empty), 0); 334 + cap_free(empty); 335 + 336 + /* Prevent privilege regain */ 337 + ASSERT_EQ(prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0), 0); 338 + 339 + /* Verify privilege drop */ 340 + ASSERT_FALSE(has_cap_sys_admin()); 341 + return true; 342 + } 343 + 344 + static inline int get_legacy_tiocsti_setting(struct __test_metadata *_metadata) 345 + { 346 + FILE *fp; 347 + int value = -1; 348 + 349 + fp = fopen("/proc/sys/dev/tty/legacy_tiocsti", "r"); 350 + if (!fp) { 351 + /* legacy_tiocsti sysctl not available (kernel < 6.2) */ 352 + return -1; 353 + } 354 + 355 + if (fscanf(fp, "%d", &value) == 1 && fclose(fp) == 0) { 356 + if (value < 0 || value > 1) 357 + value = -1; /* Invalid value */ 358 + } else { 359 + value = -1; /* Failed to parse */ 360 + } 361 + 362 + return value; 363 + } 364 + 365 + static inline bool set_legacy_tiocsti_setting(struct __test_metadata *_metadata, 366 + int value) 367 + { 368 + FILE *fp; 369 + bool success = false; 370 + 371 + /* Sanity-check the value */ 372 + ASSERT_GE(value, 0); 373 + ASSERT_LE(value, 1); 374 + 375 + /* 376 + * Try to open for writing; if we lack permission, return false so 377 + * the test harness will skip variants that need to change it 378 + */ 379 + fp = fopen("/proc/sys/dev/tty/legacy_tiocsti", "w"); 380 + if (!fp) 381 + return false; 382 + 383 + /* Write the new setting */ 384 + if (fprintf(fp, "%d\n", value) > 0 && fclose(fp) == 0) 385 + success = true; 386 + else 387 + TH_LOG("Failed to write legacy_tiocsti: %s", strerror(errno)); 388 + 389 + return success; 390 + } 391 + 392 + /* 393 + * TIOCSTI injection test function 394 + * @tty_fd: TTY slave file descriptor to test TIOCSTI on 395 + * Returns: 0 on success, -errno on failure 396 + */ 397 + static inline int test_tiocsti_injection(struct __test_metadata *_metadata, 398 + int tty_fd) 399 + { 400 + int ret; 401 + char inject_char = 'V'; 402 + 403 + errno = 0; 404 + ret = ioctl(tty_fd, TIOCSTI, &inject_char); 405 + return ret == 0 ? 0 : -errno; 406 + } 407 + 408 + /* 409 + * Child process: test TIOCSTI directly with capability/controlling 410 + * terminal setup 411 + */ 412 + static void run_basic_tiocsti_test(struct __test_metadata *_metadata, 413 + FIXTURE_DATA(tiocsti) * self, 414 + const FIXTURE_VARIANT(tiocsti) * variant) 415 + { 416 + /* Handle capability requirements */ 417 + if (self->initial_cap_sys_admin && !variant->requires_cap) 418 + ASSERT_TRUE(drop_all_privs(_metadata)); 419 + 420 + if (variant->controlling_tty) { 421 + /* 422 + * Create new session and set PTY as 423 + * controlling terminal 424 + */ 425 + pid_t sid = setsid(); 426 + 427 + ASSERT_GE(sid, 0); 428 + ASSERT_EQ(ioctl(self->pty_slave_fd, TIOCSCTTY, 0), 0); 429 + } 430 + 431 + /* 432 + * Validate test environment setup and verify final 433 + * capability state matches expectation 434 + * after potential drop. 435 + */ 436 + ASSERT_TRUE(self->has_pty); 437 + ASSERT_EQ(has_cap_sys_admin(), variant->requires_cap); 438 + 439 + /* Test TIOCSTI and validate result */ 440 + int result = test_tiocsti_injection(_metadata, self->pty_slave_fd); 441 + 442 + /* Check against expected result from variant */ 443 + EXPECT_EQ(result, variant->expected_success); 444 + _exit(0); 445 + } 446 + 447 + /* 448 + * Child process: create PTY and then pass FD to parent via SCM_RIGHTS 449 + */ 450 + static void run_fdpass_tiocsti_test(struct __test_metadata *_metadata, 451 + const FIXTURE_VARIANT(tiocsti) * variant, 452 + int sockfd) 453 + { 454 + signal(SIGHUP, SIG_IGN); 455 + 456 + /* Handle privilege dropping */ 457 + if (!variant->requires_cap && has_cap_sys_admin()) 458 + ASSERT_TRUE(drop_all_privs(_metadata)); 459 + 460 + /* Create child's PTY */ 461 + int child_master_fd, child_slave_fd; 462 + 463 + ASSERT_EQ(openpty(&child_master_fd, &child_slave_fd, NULL, NULL, NULL), 464 + 0); 465 + 466 + if (variant->controlling_tty) { 467 + pid_t sid = setsid(); 468 + 469 + ASSERT_GE(sid, 0); 470 + ASSERT_EQ(ioctl(child_slave_fd, TIOCSCTTY, 0), 0); 471 + } 472 + 473 + /* Test child's direct TIOCSTI for reference */ 474 + int direct_result = test_tiocsti_injection(_metadata, child_slave_fd); 475 + 476 + EXPECT_EQ(direct_result, variant->expected_success); 477 + 478 + /* Send FD to parent */ 479 + ASSERT_EQ(send_fd_via_socket(sockfd, child_slave_fd), 0); 480 + 481 + /* Wait for parent completion signal */ 482 + char sync_byte; 483 + ssize_t bytes_read = read(sockfd, &sync_byte, 1); 484 + 485 + ASSERT_EQ(bytes_read, 1); 486 + 487 + close(child_master_fd); 488 + close(child_slave_fd); 489 + close(sockfd); 490 + _exit(0); 491 + } 492 + 493 + FIXTURE_SETUP(tiocsti) 494 + { 495 + /* Create PTY pair for basic tests */ 496 + self->has_pty = (openpty(&self->pty_master_fd, &self->pty_slave_fd, 497 + NULL, NULL, NULL) == 0); 498 + if (!self->has_pty) { 499 + self->pty_master_fd = -1; 500 + self->pty_slave_fd = -1; 501 + } 502 + 503 + self->initial_cap_sys_admin = has_cap_sys_admin(); 504 + self->original_legacy_tiocsti_setting = 505 + get_legacy_tiocsti_setting(_metadata); 506 + 507 + if (self->original_legacy_tiocsti_setting < 0) 508 + SKIP(return, 509 + "legacy_tiocsti sysctl not available (kernel < 6.2)"); 510 + 511 + /* Common skip conditions */ 512 + if (variant->test_type == TEST_PTY_TIOCSTI_BASIC && !self->has_pty) 513 + SKIP(return, "PTY not available for controlling terminal test"); 514 + 515 + if (variant->test_type == TEST_PTY_TIOCSTI_FD_PASSING && 516 + !self->initial_cap_sys_admin) 517 + SKIP(return, "FD Pass tests require CAP_SYS_ADMIN"); 518 + 519 + if (variant->requires_cap && !self->initial_cap_sys_admin) 520 + SKIP(return, "Test requires initial CAP_SYS_ADMIN"); 521 + 522 + /* Test if we can modify the sysctl (requires appropriate privileges) */ 523 + self->can_modify_sysctl = set_legacy_tiocsti_setting( 524 + _metadata, self->original_legacy_tiocsti_setting); 525 + 526 + /* Sysctl setup based on variant */ 527 + if (self->can_modify_sysctl && 528 + self->original_legacy_tiocsti_setting != variant->legacy_tiocsti) { 529 + if (!set_legacy_tiocsti_setting(_metadata, 530 + variant->legacy_tiocsti)) 531 + SKIP(return, "Failed to set legacy_tiocsti sysctl"); 532 + 533 + } else if (!self->can_modify_sysctl && 534 + self->original_legacy_tiocsti_setting != 535 + variant->legacy_tiocsti) 536 + SKIP(return, "legacy_tiocsti setting mismatch"); 537 + } 538 + 539 + FIXTURE_TEARDOWN(tiocsti) 540 + { 541 + /* 542 + * Backup restoration - 543 + * each test should restore its own sysctl changes 544 + */ 545 + if (self->can_modify_sysctl) { 546 + int current_value = get_legacy_tiocsti_setting(_metadata); 547 + 548 + if (current_value != self->original_legacy_tiocsti_setting) { 549 + TH_LOG("Backup: Restoring legacy_tiocsti from %d to %d", 550 + current_value, 551 + self->original_legacy_tiocsti_setting); 552 + set_legacy_tiocsti_setting( 553 + _metadata, 554 + self->original_legacy_tiocsti_setting); 555 + } 556 + } 557 + 558 + if (self->has_pty) { 559 + if (self->pty_master_fd >= 0) 560 + close(self->pty_master_fd); 561 + if (self->pty_slave_fd >= 0) 562 + close(self->pty_slave_fd); 563 + } 564 + } 565 + 566 + TEST_F(tiocsti, test) 567 + { 568 + int status; 569 + pid_t child_pid; 570 + 571 + if (variant->test_type == TEST_PTY_TIOCSTI_BASIC) { 572 + /* ===== BASIC TIOCSTI TEST ===== */ 573 + child_pid = fork(); 574 + ASSERT_GE(child_pid, 0); 575 + 576 + /* Perform the actual test in the child process */ 577 + if (child_pid == 0) 578 + run_basic_tiocsti_test(_metadata, self, variant); 579 + 580 + } else { 581 + /* ===== FD PASSING SECURITY TEST ===== */ 582 + int sockpair[2]; 583 + 584 + ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sockpair), 0); 585 + 586 + child_pid = fork(); 587 + ASSERT_GE(child_pid, 0); 588 + 589 + if (child_pid == 0) { 590 + /* Child process - create PTY and send FD */ 591 + close(sockpair[0]); 592 + run_fdpass_tiocsti_test(_metadata, variant, 593 + sockpair[1]); 594 + } 595 + 596 + /* Parent process - receive FD and test TIOCSTI */ 597 + close(sockpair[1]); 598 + 599 + int received_fd = recv_fd_via_socket(sockpair[0]); 600 + 601 + ASSERT_GE(received_fd, 0); 602 + 603 + bool parent_has_cap = self->initial_cap_sys_admin; 604 + 605 + TH_LOG("=== TIOCSTI FD Passing Test Context ==="); 606 + TH_LOG("legacy_tiocsti: %d, Parent CAP_SYS_ADMIN: %s, Child: %s", 607 + variant->legacy_tiocsti, parent_has_cap ? "yes" : "no", 608 + variant->requires_cap ? "kept" : "dropped"); 609 + 610 + /* SECURITY TEST: Try TIOCSTI with FD opened by child */ 611 + int result = test_tiocsti_injection(_metadata, received_fd); 612 + 613 + /* Log security concern if demonstrated */ 614 + if (result == 0 && !variant->requires_cap) { 615 + TH_LOG("*** SECURITY CONCERN DEMONSTRATED ***"); 616 + TH_LOG("Privileged parent can use TIOCSTI on FD from unprivileged child"); 617 + TH_LOG("This shows current process credentials are used, not opener credentials"); 618 + } 619 + 620 + EXPECT_EQ(result, variant->expected_success) 621 + { 622 + TH_LOG("FD passing: expected error %d, got %d", 623 + variant->expected_success, result); 624 + } 625 + 626 + /* Signal child completion */ 627 + char sync_byte = 'D'; 628 + ssize_t bytes_written = write(sockpair[0], &sync_byte, 1); 629 + 630 + ASSERT_EQ(bytes_written, 1); 631 + 632 + close(received_fd); 633 + close(sockpair[0]); 634 + } 635 + 636 + /* Common child process cleanup for both test types */ 637 + ASSERT_EQ(waitpid(child_pid, &status, 0), child_pid); 638 + 639 + if (WIFSIGNALED(status)) { 640 + TH_LOG("Child terminated by signal %d", WTERMSIG(status)); 641 + ASSERT_FALSE(WIFSIGNALED(status)) 642 + { 643 + TH_LOG("Child process failed assertion"); 644 + } 645 + } else { 646 + EXPECT_EQ(WEXITSTATUS(status), 0); 647 + } 648 + } 649 + 650 + TEST_HARNESS_MAIN