tty: moxa: use guard()s
Use guards in the moxa code. This improves readability, makes error handling easier, and marks locked portions of code explicit. All that while being sure the lock is unlocked. Signed-off-by: Jiri Slaby (SUSE) <jirislaby@kernel.org> Link: https://patch.msgid.link/20251119100140.830761-6-jirislaby@kernel.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>pull/1354/merge
parent
8c03bfcf6b
commit
1c7736dc68
|
|
@ -487,25 +487,20 @@ static void moxa_wait_finish(void __iomem *ofsAddr)
|
||||||
|
|
||||||
static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
|
static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
|
||||||
{
|
{
|
||||||
unsigned long flags;
|
guard(spinlock_irqsave)(&moxafunc_lock);
|
||||||
spin_lock_irqsave(&moxafunc_lock, flags);
|
|
||||||
writew(arg, ofsAddr + FuncArg);
|
writew(arg, ofsAddr + FuncArg);
|
||||||
writew(cmd, ofsAddr + FuncCode);
|
writew(cmd, ofsAddr + FuncCode);
|
||||||
moxa_wait_finish(ofsAddr);
|
moxa_wait_finish(ofsAddr);
|
||||||
spin_unlock_irqrestore(&moxafunc_lock, flags);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int moxafuncret(void __iomem *ofsAddr, u16 cmd, u16 arg)
|
static int moxafuncret(void __iomem *ofsAddr, u16 cmd, u16 arg)
|
||||||
{
|
{
|
||||||
unsigned long flags;
|
guard(spinlock_irqsave)(&moxafunc_lock);
|
||||||
u16 ret;
|
|
||||||
spin_lock_irqsave(&moxafunc_lock, flags);
|
|
||||||
writew(arg, ofsAddr + FuncArg);
|
writew(arg, ofsAddr + FuncArg);
|
||||||
writew(cmd, ofsAddr + FuncCode);
|
writew(cmd, ofsAddr + FuncCode);
|
||||||
moxa_wait_finish(ofsAddr);
|
moxa_wait_finish(ofsAddr);
|
||||||
ret = readw(ofsAddr + FuncArg);
|
|
||||||
spin_unlock_irqrestore(&moxafunc_lock, flags);
|
return readw(ofsAddr + FuncArg);
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void moxa_low_water_check(void __iomem *ofsAddr)
|
static void moxa_low_water_check(void __iomem *ofsAddr)
|
||||||
|
|
@ -1002,11 +997,11 @@ static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
|
||||||
if (ret)
|
if (ret)
|
||||||
goto err_free;
|
goto err_free;
|
||||||
|
|
||||||
spin_lock_bh(&moxa_lock);
|
scoped_guard(spinlock_bh, &moxa_lock) {
|
||||||
brd->ready = 1;
|
brd->ready = 1;
|
||||||
if (!timer_pending(&moxaTimer))
|
if (!timer_pending(&moxaTimer))
|
||||||
mod_timer(&moxaTimer, jiffies + HZ / 50);
|
mod_timer(&moxaTimer, jiffies + HZ / 50);
|
||||||
spin_unlock_bh(&moxa_lock);
|
}
|
||||||
|
|
||||||
first_idx = (brd - moxa_boards) * MAX_PORTS_PER_BOARD;
|
first_idx = (brd - moxa_boards) * MAX_PORTS_PER_BOARD;
|
||||||
for (i = 0; i < brd->numPorts; i++)
|
for (i = 0; i < brd->numPorts; i++)
|
||||||
|
|
@ -1026,10 +1021,9 @@ static void moxa_board_deinit(struct moxa_board_conf *brd)
|
||||||
{
|
{
|
||||||
unsigned int a, opened, first_idx;
|
unsigned int a, opened, first_idx;
|
||||||
|
|
||||||
mutex_lock(&moxa_openlock);
|
scoped_guard(mutex, &moxa_openlock) {
|
||||||
spin_lock_bh(&moxa_lock);
|
scoped_guard(spinlock_bh, &moxa_lock)
|
||||||
brd->ready = 0;
|
brd->ready = 0;
|
||||||
spin_unlock_bh(&moxa_lock);
|
|
||||||
|
|
||||||
/* pci hot-un-plug support */
|
/* pci hot-un-plug support */
|
||||||
for (a = 0; a < brd->numPorts; a++)
|
for (a = 0; a < brd->numPorts; a++)
|
||||||
|
|
@ -1044,12 +1038,13 @@ static void moxa_board_deinit(struct moxa_board_conf *brd)
|
||||||
for (a = 0; a < brd->numPorts; a++)
|
for (a = 0; a < brd->numPorts; a++)
|
||||||
if (tty_port_initialized(&brd->ports[a].port))
|
if (tty_port_initialized(&brd->ports[a].port))
|
||||||
opened++;
|
opened++;
|
||||||
mutex_unlock(&moxa_openlock);
|
|
||||||
if (!opened)
|
if (!opened)
|
||||||
break;
|
break;
|
||||||
|
mutex_unlock(&moxa_openlock);
|
||||||
msleep(50);
|
msleep(50);
|
||||||
mutex_lock(&moxa_openlock);
|
mutex_lock(&moxa_openlock);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
first_idx = (brd - moxa_boards) * MAX_PORTS_PER_BOARD;
|
first_idx = (brd - moxa_boards) * MAX_PORTS_PER_BOARD;
|
||||||
for (a = 0; a < brd->numPorts; a++)
|
for (a = 0; a < brd->numPorts; a++)
|
||||||
|
|
@ -1206,12 +1201,9 @@ static void moxa_shutdown(struct tty_port *port)
|
||||||
static bool moxa_carrier_raised(struct tty_port *port)
|
static bool moxa_carrier_raised(struct tty_port *port)
|
||||||
{
|
{
|
||||||
struct moxa_port *ch = container_of(port, struct moxa_port, port);
|
struct moxa_port *ch = container_of(port, struct moxa_port, port);
|
||||||
int dcd;
|
|
||||||
|
|
||||||
spin_lock_irq(&port->lock);
|
guard(spinlock_irq)(&port->lock);
|
||||||
dcd = ch->DCDState;
|
return ch->DCDState;
|
||||||
spin_unlock_irq(&port->lock);
|
|
||||||
return dcd;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void moxa_dtr_rts(struct tty_port *port, bool active)
|
static void moxa_dtr_rts(struct tty_port *port, bool active)
|
||||||
|
|
@ -1225,27 +1217,22 @@ static int moxa_open(struct tty_struct *tty, struct file *filp)
|
||||||
{
|
{
|
||||||
struct moxa_board_conf *brd;
|
struct moxa_board_conf *brd;
|
||||||
struct moxa_port *ch;
|
struct moxa_port *ch;
|
||||||
int port;
|
int port = tty->index;
|
||||||
|
|
||||||
port = tty->index;
|
scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &moxa_openlock) {
|
||||||
if (mutex_lock_interruptible(&moxa_openlock))
|
|
||||||
return -ERESTARTSYS;
|
|
||||||
brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
|
brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
|
||||||
if (!brd->ready) {
|
if (!brd->ready)
|
||||||
mutex_unlock(&moxa_openlock);
|
|
||||||
return -ENODEV;
|
return -ENODEV;
|
||||||
}
|
|
||||||
|
|
||||||
if (port % MAX_PORTS_PER_BOARD >= brd->numPorts) {
|
if (port % MAX_PORTS_PER_BOARD >= brd->numPorts)
|
||||||
mutex_unlock(&moxa_openlock);
|
|
||||||
return -ENODEV;
|
return -ENODEV;
|
||||||
}
|
|
||||||
|
|
||||||
ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
|
ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
|
||||||
ch->port.count++;
|
ch->port.count++;
|
||||||
tty->driver_data = ch;
|
tty->driver_data = ch;
|
||||||
tty_port_tty_set(&ch->port, tty);
|
tty_port_tty_set(&ch->port, tty);
|
||||||
mutex_lock(&ch->port.mutex);
|
|
||||||
|
guard(mutex)(&ch->port.mutex);
|
||||||
if (!tty_port_initialized(&ch->port)) {
|
if (!tty_port_initialized(&ch->port)) {
|
||||||
ch->statusflags = 0;
|
ch->statusflags = 0;
|
||||||
moxa_set_tty_param(tty, &tty->termios);
|
moxa_set_tty_param(tty, &tty->termios);
|
||||||
|
|
@ -1254,8 +1241,7 @@ static int moxa_open(struct tty_struct *tty, struct file *filp)
|
||||||
MoxaSetFifo(ch, ch->type == PORT_16550A);
|
MoxaSetFifo(ch, ch->type == PORT_16550A);
|
||||||
tty_port_set_initialized(&ch->port, true);
|
tty_port_set_initialized(&ch->port, true);
|
||||||
}
|
}
|
||||||
mutex_unlock(&ch->port.mutex);
|
}
|
||||||
mutex_unlock(&moxa_openlock);
|
|
||||||
|
|
||||||
return tty_port_block_til_ready(&ch->port, tty, filp);
|
return tty_port_block_til_ready(&ch->port, tty, filp);
|
||||||
}
|
}
|
||||||
|
|
@ -1270,15 +1256,13 @@ static void moxa_close(struct tty_struct *tty, struct file *filp)
|
||||||
static ssize_t moxa_write(struct tty_struct *tty, const u8 *buf, size_t count)
|
static ssize_t moxa_write(struct tty_struct *tty, const u8 *buf, size_t count)
|
||||||
{
|
{
|
||||||
struct moxa_port *ch = tty->driver_data;
|
struct moxa_port *ch = tty->driver_data;
|
||||||
unsigned long flags;
|
|
||||||
int len;
|
int len;
|
||||||
|
|
||||||
if (ch == NULL)
|
if (ch == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
spin_lock_irqsave(&moxa_lock, flags);
|
scoped_guard(spinlock_irqsave, &moxa_lock)
|
||||||
len = MoxaPortWriteData(tty, buf, count);
|
len = MoxaPortWriteData(tty, buf, count);
|
||||||
spin_unlock_irqrestore(&moxa_lock, flags);
|
|
||||||
|
|
||||||
set_bit(LOWWAIT, &ch->statusflags);
|
set_bit(LOWWAIT, &ch->statusflags);
|
||||||
return len;
|
return len;
|
||||||
|
|
@ -1349,12 +1333,10 @@ static int moxa_tiocmset(struct tty_struct *tty,
|
||||||
bool dtr_active, rts_active;
|
bool dtr_active, rts_active;
|
||||||
struct moxa_port *ch;
|
struct moxa_port *ch;
|
||||||
|
|
||||||
mutex_lock(&moxa_openlock);
|
guard(mutex)(&moxa_openlock);
|
||||||
ch = tty->driver_data;
|
ch = tty->driver_data;
|
||||||
if (!ch) {
|
if (!ch)
|
||||||
mutex_unlock(&moxa_openlock);
|
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
|
||||||
|
|
||||||
MoxaPortGetLineOut(ch, &dtr_active, &rts_active);
|
MoxaPortGetLineOut(ch, &dtr_active, &rts_active);
|
||||||
if (set & TIOCM_RTS)
|
if (set & TIOCM_RTS)
|
||||||
|
|
@ -1366,7 +1348,7 @@ static int moxa_tiocmset(struct tty_struct *tty,
|
||||||
if (clear & TIOCM_DTR)
|
if (clear & TIOCM_DTR)
|
||||||
dtr_active = false;
|
dtr_active = false;
|
||||||
MoxaPortLineCtrl(ch, dtr_active, rts_active);
|
MoxaPortLineCtrl(ch, dtr_active, rts_active);
|
||||||
mutex_unlock(&moxa_openlock);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1415,19 +1397,18 @@ static void moxa_hangup(struct tty_struct *tty)
|
||||||
|
|
||||||
static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
|
static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
|
||||||
{
|
{
|
||||||
unsigned long flags;
|
|
||||||
dcd = !!dcd;
|
dcd = !!dcd;
|
||||||
|
|
||||||
spin_lock_irqsave(&p->port.lock, flags);
|
scoped_guard(spinlock_irqsave, &p->port.lock) {
|
||||||
if (dcd != p->DCDState) {
|
if (dcd == p->DCDState)
|
||||||
|
return;
|
||||||
|
|
||||||
p->DCDState = dcd;
|
p->DCDState = dcd;
|
||||||
spin_unlock_irqrestore(&p->port.lock, flags);
|
}
|
||||||
|
|
||||||
if (!dcd)
|
if (!dcd)
|
||||||
tty_port_tty_hangup(&p->port, true);
|
tty_port_tty_hangup(&p->port, true);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
spin_unlock_irqrestore(&p->port.lock, flags);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
|
static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
|
||||||
u16 __iomem *ip)
|
u16 __iomem *ip)
|
||||||
|
|
@ -1494,7 +1475,7 @@ static void moxa_poll(struct timer_list *unused)
|
||||||
u16 __iomem *ip;
|
u16 __iomem *ip;
|
||||||
unsigned int card, port, served = 0;
|
unsigned int card, port, served = 0;
|
||||||
|
|
||||||
spin_lock(&moxa_lock);
|
guard(spinlock)(&moxa_lock);
|
||||||
for (card = 0; card < MAX_BOARDS; card++) {
|
for (card = 0; card < MAX_BOARDS; card++) {
|
||||||
brd = &moxa_boards[card];
|
brd = &moxa_boards[card];
|
||||||
if (!brd->ready)
|
if (!brd->ready)
|
||||||
|
|
@ -1525,7 +1506,6 @@ static void moxa_poll(struct timer_list *unused)
|
||||||
|
|
||||||
if (served)
|
if (served)
|
||||||
mod_timer(&moxaTimer, jiffies + HZ / 50);
|
mod_timer(&moxaTimer, jiffies + HZ / 50);
|
||||||
spin_unlock(&moxa_lock);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
@ -1861,13 +1841,11 @@ static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
|
||||||
baud = MoxaPortSetBaud(port, baud);
|
baud = MoxaPortSetBaud(port, baud);
|
||||||
|
|
||||||
if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
|
if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
|
||||||
spin_lock_irq(&moxafunc_lock);
|
guard(spinlock_irq)(&moxafunc_lock);
|
||||||
writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
|
writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
|
||||||
writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
|
writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
|
||||||
writeb(FC_SetXonXoff, ofsAddr + FuncCode);
|
writeb(FC_SetXonXoff, ofsAddr + FuncCode);
|
||||||
moxa_wait_finish(ofsAddr);
|
moxa_wait_finish(ofsAddr);
|
||||||
spin_unlock_irq(&moxafunc_lock);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
return baud;
|
return baud;
|
||||||
}
|
}
|
||||||
|
|
@ -2098,13 +2076,13 @@ static int moxa_get_serial_info(struct tty_struct *tty,
|
||||||
|
|
||||||
if (!info)
|
if (!info)
|
||||||
return -ENODEV;
|
return -ENODEV;
|
||||||
mutex_lock(&info->port.mutex);
|
guard(mutex)(&info->port.mutex);
|
||||||
ss->type = info->type;
|
ss->type = info->type;
|
||||||
ss->line = info->port.tty->index;
|
ss->line = info->port.tty->index;
|
||||||
ss->flags = info->port.flags;
|
ss->flags = info->port.flags;
|
||||||
ss->baud_base = 921600;
|
ss->baud_base = 921600;
|
||||||
ss->close_delay = jiffies_to_msecs(info->port.close_delay) / 10;
|
ss->close_delay = jiffies_to_msecs(info->port.close_delay) / 10;
|
||||||
mutex_unlock(&info->port.mutex);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -2120,13 +2098,12 @@ static int moxa_set_serial_info(struct tty_struct *tty,
|
||||||
|
|
||||||
close_delay = msecs_to_jiffies(ss->close_delay * 10);
|
close_delay = msecs_to_jiffies(ss->close_delay * 10);
|
||||||
|
|
||||||
mutex_lock(&info->port.mutex);
|
guard(mutex)(&info->port.mutex);
|
||||||
if (!capable(CAP_SYS_ADMIN)) {
|
if (!capable(CAP_SYS_ADMIN)) {
|
||||||
if (close_delay != info->port.close_delay ||
|
if (close_delay != info->port.close_delay ||
|
||||||
ss->type != info->type ||
|
ss->type != info->type ||
|
||||||
((ss->flags & ~ASYNC_USR_MASK) !=
|
((ss->flags & ~ASYNC_USR_MASK) !=
|
||||||
(info->port.flags & ~ASYNC_USR_MASK))) {
|
(info->port.flags & ~ASYNC_USR_MASK))) {
|
||||||
mutex_unlock(&info->port.mutex);
|
|
||||||
return -EPERM;
|
return -EPERM;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -2136,7 +2113,7 @@ static int moxa_set_serial_info(struct tty_struct *tty,
|
||||||
|
|
||||||
info->type = ss->type;
|
info->type = ss->type;
|
||||||
}
|
}
|
||||||
mutex_unlock(&info->port.mutex);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue