Commit 9fae5f85 authored by Jiri Slaby's avatar Jiri Slaby Committed by Greg Kroah-Hartman
Browse files

mxser: extract mxser_ioctl_op_mode



This ioctl code sets RS232, RS422 or RS485 mode. Move the code to a
separate function (mxser_ioctl_op_mode) with a note that these
mxser-special ioctls should be likely replaced eventually by TIOCGRS485
and TIOCSRS485.

Signed-off-by: default avatarJiri Slaby <jslaby@suse.cz>
Link: https://lore.kernel.org/r/20210618061516.662-39-jslaby@suse.cz


Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 2c21832b
Loading
Loading
Loading
Loading
+45 −38
Original line number Diff line number Diff line
@@ -1407,53 +1407,60 @@ static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
	return ret;
}

static int mxser_ioctl(struct tty_struct *tty,
		unsigned int cmd, unsigned long arg)
/* We should likely switch to TIOCGRS485/TIOCSRS485. */
static int mxser_ioctl_op_mode(struct mxser_port *port, int index, bool set,
		int __user *u_opmode)
{
	struct mxser_port *info = tty->driver_data;
	struct async_icount cnow;
	unsigned long flags;
	void __user *argp = (void __user *)arg;

	if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
		int p;
		unsigned long opmode;
		static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
		int shiftbit;
	static const unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
	int opmode, p = index % 4;
	int shiftbit = p * 2;
	unsigned char val, mask;

		if (info->board->must_hwid != MOXA_MUST_MU860_HWID)
	if (port->board->must_hwid != MOXA_MUST_MU860_HWID)
		return -EFAULT;

		p = tty->index % 4;
		if (cmd == MOXA_SET_OP_MODE) {
			if (get_user(opmode, (int __user *) argp))
	if (set) {
		if (get_user(opmode, u_opmode))
			return -EFAULT;
			if (opmode != RS232_MODE &&
					opmode != RS485_2WIRE_MODE &&

		if (opmode != RS232_MODE && opmode != RS485_2WIRE_MODE &&
				opmode != RS422_MODE &&
				opmode != RS485_4WIRE_MODE)
			return -EFAULT;

		mask = ModeMask[p];
			shiftbit = p * 2;
			spin_lock_irq(&info->slock);
			val = inb(info->opmode_ioaddr);

		spin_lock_irq(&port->slock);
		val = inb(port->opmode_ioaddr);
		val &= mask;
		val |= (opmode << shiftbit);
			outb(val, info->opmode_ioaddr);
			spin_unlock_irq(&info->slock);
		outb(val, port->opmode_ioaddr);
		spin_unlock_irq(&port->slock);
	} else {
			shiftbit = p * 2;
			spin_lock_irq(&info->slock);
			opmode = inb(info->opmode_ioaddr) >> shiftbit;
			spin_unlock_irq(&info->slock);
		spin_lock_irq(&port->slock);
		opmode = inb(port->opmode_ioaddr) >> shiftbit;
		spin_unlock_irq(&port->slock);

		opmode &= OP_MODE_MASK;
			if (put_user(opmode, (int __user *)argp))
		if (put_user(opmode, u_opmode))
			return -EFAULT;
	}

	return 0;
}

static int mxser_ioctl(struct tty_struct *tty,
		unsigned int cmd, unsigned long arg)
{
	struct mxser_port *info = tty->driver_data;
	struct async_icount cnow;
	unsigned long flags;
	void __user *argp = (void __user *)arg;

	if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE)
		return mxser_ioctl_op_mode(info, tty->index,
				cmd == MOXA_SET_OP_MODE, argp);

	if (cmd != TIOCMIWAIT && tty_io_error(tty))
		return -EIO;