blob: 3a1a5e0ee93f1d4bd4738fdade69b791376e6851 [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*****************************************************************************/
3/*
4 * moxa.c -- MOXA Intellio family multiport serial driver.
5 *
Jiri Slabyb9705b62008-04-30 00:53:48 -07006 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com).
7 * Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This code is loosely based on the Linux serial driver, written by
10 * Linus Torvalds, Theodore T'so and others.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13/*
14 * MOXA Intellio Series Driver
15 * for : LINUX
16 * date : 1999/1/7
17 * version : 5.1
18 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/mm.h>
23#include <linux/ioport.h>
24#include <linux/errno.h>
Jiri Slaby03718232008-04-30 00:53:39 -070025#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/signal.h>
27#include <linux/sched.h>
28#include <linux/timer.h>
29#include <linux/interrupt.h>
30#include <linux/tty.h>
31#include <linux/tty_flip.h>
32#include <linux/major.h>
33#include <linux/string.h>
34#include <linux/fcntl.h>
35#include <linux/ptrace.h>
36#include <linux/serial.h>
37#include <linux/tty_driver.h>
38#include <linux/delay.h>
39#include <linux/pci.h>
40#include <linux/init.h>
41#include <linux/bitops.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Manuel Zerpies5a3c6b2512011-06-16 14:07:22 +020043#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/io.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080046#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Jiri Slaby03718232008-04-30 00:53:39 -070048#include "moxa.h"
49
Jiri Slabyb9705b62008-04-30 00:53:48 -070050#define MOXA_VERSION "6.0k"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Jiri Slaby03718232008-04-30 00:53:39 -070052#define MOXA_FW_HDRLEN 32
53
Jiri Slaby11324ed2007-02-10 01:45:31 -080054#define MOXAMAJOR 172
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Jiri Slaby11324ed2007-02-10 01:45:31 -080056#define MAX_BOARDS 4 /* Don't change this value */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
Jiri Slaby11324ed2007-02-10 01:45:31 -080058#define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Jiri Slaby08d01c72008-04-30 00:53:47 -070060#define MOXA_IS_320(brd) ((brd)->boardType == MOXA_BOARD_C320_ISA || \
61 (brd)->boardType == MOXA_BOARD_C320_PCI)
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/*
64 * Define the Moxa PCI vendor and device IDs.
65 */
Jiri Slaby11324ed2007-02-10 01:45:31 -080066#define MOXA_BUS_TYPE_ISA 0
67#define MOXA_BUS_TYPE_PCI 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069enum {
70 MOXA_BOARD_C218_PCI = 1,
71 MOXA_BOARD_C218_ISA,
72 MOXA_BOARD_C320_PCI,
73 MOXA_BOARD_C320_ISA,
74 MOXA_BOARD_CP204J,
75};
76
77static char *moxa_brdname[] =
78{
79 "C218 Turbo PCI series",
80 "C218 Turbo ISA series",
81 "C320 Turbo PCI series",
82 "C320 Turbo ISA series",
83 "CP-204J series",
84};
85
86#ifdef CONFIG_PCI
Arvind Yadav50bd8422017-07-23 15:31:05 +053087static const struct pci_device_id moxa_pcibrds[] = {
Jiri Slaby5ebb4072007-02-10 01:45:30 -080088 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
89 .driver_data = MOXA_BOARD_C218_PCI },
90 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
91 .driver_data = MOXA_BOARD_C320_PCI },
92 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
93 .driver_data = MOXA_BOARD_CP204J },
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 { 0 }
95};
96MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
97#endif /* CONFIG_PCI */
98
Jiri Slaby03718232008-04-30 00:53:39 -070099struct moxa_port;
100
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800101static struct moxa_board_conf {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 int boardType;
103 int numPorts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 int busType;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800105
Jiri Slaby810ab092008-04-30 00:53:41 -0700106 unsigned int ready;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800107
Jiri Slaby03718232008-04-30 00:53:39 -0700108 struct moxa_port *ports;
109
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800110 void __iomem *basemem;
111 void __iomem *intNdx;
112 void __iomem *intPend;
113 void __iomem *intTable;
114} moxa_boards[MAX_BOARDS];
115
116struct mxser_mstatus {
117 tcflag_t cflag;
118 int cts;
119 int dsr;
120 int ri;
121 int dcd;
Jiri Slaby9dff89c2007-02-10 01:45:30 -0800122};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800124struct moxaq_str {
125 int inq;
126 int outq;
127};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800129struct moxa_port {
Alan Cox9de6a512008-07-16 21:56:02 +0100130 struct tty_port port;
Jiri Slabyb4173f42008-04-30 00:53:40 -0700131 struct moxa_board_conf *board;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700132 void __iomem *tableAddr;
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 int cflag;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700136 unsigned long statusflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Alan Cox8482bcd2009-11-30 13:18:13 +0000138 u8 DCDState; /* Protected by the port lock */
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700139 u8 lineCtrl;
140 u8 lowChkFlag;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800141};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Jiri Slaby74d7d972008-04-30 00:53:43 -0700143struct mon_str {
144 int tick;
145 int rxcnt[MAX_PORTS];
146 int txcnt[MAX_PORTS];
147};
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/* statusflags */
Alan Coxa808ac02009-11-30 13:18:02 +0000150#define TXSTOPPED 1
151#define LOWWAIT 2
152#define EMPTYWAIT 3
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#define WAKEUP_CHARS 256
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static int ttymajor = MOXAMAJOR;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700158static struct mon_str moxaLog;
159static unsigned int moxaFuncTout = HZ / 2;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700160static unsigned int moxaLowWaterChk;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700161static DEFINE_MUTEX(moxa_openlock);
Alan Coxe8c62102009-11-30 13:18:24 +0000162static DEFINE_SPINLOCK(moxa_lock);
Rakib Mullickc6fc8262009-12-09 12:34:18 -0800163
Jiri Slabyd353eca2008-04-30 00:53:37 -0700164static unsigned long baseaddr[MAX_BOARDS];
165static unsigned int type[MAX_BOARDS];
166static unsigned int numports[MAX_BOARDS];
Jiri Slaby793be892012-08-07 21:47:55 +0200167static struct tty_port moxa_service_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169MODULE_AUTHOR("William Chen");
170MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
171MODULE_LICENSE("GPL");
Ben Hutchingse6c4ef92010-01-13 23:34:18 +0000172MODULE_FIRMWARE("c218tunx.cod");
173MODULE_FIRMWARE("cp204unx.cod");
174MODULE_FIRMWARE("c320tunx.cod");
Rakib Mullickc6fc8262009-12-09 12:34:18 -0800175
Jiri Slabyd353eca2008-04-30 00:53:37 -0700176module_param_array(type, uint, NULL, 0);
177MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
David Howells3b60daf82017-04-04 16:54:29 +0100178module_param_hw_array(baseaddr, ulong, ioport, NULL, 0);
Jiri Slabyd353eca2008-04-30 00:53:37 -0700179MODULE_PARM_DESC(baseaddr, "base address");
180module_param_array(numports, uint, NULL, 0);
181MODULE_PARM_DESC(numports, "numports (ignored for C218)");
Rakib Mullickc6fc8262009-12-09 12:34:18 -0800182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183module_param(ttymajor, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/*
186 * static functions:
187 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188static int moxa_open(struct tty_struct *, struct file *);
189static void moxa_close(struct tty_struct *, struct file *);
190static int moxa_write(struct tty_struct *, const unsigned char *, int);
191static int moxa_write_room(struct tty_struct *);
192static void moxa_flush_buffer(struct tty_struct *);
193static int moxa_chars_in_buffer(struct tty_struct *);
Alan Cox606d0992006-12-08 02:38:45 -0800194static void moxa_set_termios(struct tty_struct *, struct ktermios *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195static void moxa_stop(struct tty_struct *);
196static void moxa_start(struct tty_struct *);
197static void moxa_hangup(struct tty_struct *);
Alan Cox60b33c12011-02-14 16:26:14 +0000198static int moxa_tiocmget(struct tty_struct *tty);
Alan Cox20b9d172011-02-14 16:26:50 +0000199static int moxa_tiocmset(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 unsigned int set, unsigned int clear);
Kees Cook24ed9602017-08-28 11:28:21 -0700201static void moxa_poll(struct timer_list *);
Alan Coxdb1acaa2008-02-08 04:18:43 -0800202static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
Alan Coxf1761782009-11-30 13:17:51 +0000203static void moxa_shutdown(struct tty_port *);
Alan Cox31f35932009-01-02 13:45:05 +0000204static int moxa_carrier_raised(struct tty_port *);
Alan Coxf1761782009-11-30 13:17:51 +0000205static void moxa_dtr_rts(struct tty_port *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/*
207 * moxa board interface functions:
208 */
Jiri Slabyb4173f42008-04-30 00:53:40 -0700209static void MoxaPortEnable(struct moxa_port *);
210static void MoxaPortDisable(struct moxa_port *);
211static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
212static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
213static void MoxaPortLineCtrl(struct moxa_port *, int, int);
214static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
215static int MoxaPortLineStatus(struct moxa_port *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700216static void MoxaPortFlushData(struct moxa_port *, int);
Alan Coxd450b5a2008-10-13 10:39:58 +0100217static int MoxaPortWriteData(struct tty_struct *, const unsigned char *, int);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700218static int MoxaPortReadData(struct moxa_port *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700219static int MoxaPortTxQueue(struct moxa_port *);
220static int MoxaPortRxQueue(struct moxa_port *);
221static int MoxaPortTxFree(struct moxa_port *);
222static void MoxaPortTxDisable(struct moxa_port *);
223static void MoxaPortTxEnable(struct moxa_port *);
Al Viro1c729ab2018-09-11 22:01:00 -0400224static int moxa_get_serial_info(struct tty_struct *, struct serial_struct *);
225static int moxa_set_serial_info(struct tty_struct *, struct serial_struct *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700226static void MoxaSetFifo(struct moxa_port *port, int enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Jiri Slaby74d7d972008-04-30 00:53:43 -0700228/*
229 * I/O functions
230 */
231
Alan Coxa808ac02009-11-30 13:18:02 +0000232static DEFINE_SPINLOCK(moxafunc_lock);
233
Jiri Slaby74d7d972008-04-30 00:53:43 -0700234static void moxa_wait_finish(void __iomem *ofsAddr)
235{
236 unsigned long end = jiffies + moxaFuncTout;
237
238 while (readw(ofsAddr + FuncCode) != 0)
239 if (time_after(jiffies, end))
240 return;
Manuel Zerpies5a3c6b2512011-06-16 14:07:22 +0200241 if (readw(ofsAddr + FuncCode) != 0)
242 printk_ratelimited(KERN_WARNING "moxa function expired\n");
Jiri Slaby74d7d972008-04-30 00:53:43 -0700243}
244
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700245static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
Jiri Slaby74d7d972008-04-30 00:53:43 -0700246{
Alan Coxf5c5a362009-11-30 13:17:57 +0000247 unsigned long flags;
248 spin_lock_irqsave(&moxafunc_lock, flags);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700249 writew(arg, ofsAddr + FuncArg);
250 writew(cmd, ofsAddr + FuncCode);
251 moxa_wait_finish(ofsAddr);
Alan Coxf5c5a362009-11-30 13:17:57 +0000252 spin_unlock_irqrestore(&moxafunc_lock, flags);
253}
254
255static int moxafuncret(void __iomem *ofsAddr, u16 cmd, u16 arg)
256{
257 unsigned long flags;
258 u16 ret;
259 spin_lock_irqsave(&moxafunc_lock, flags);
260 writew(arg, ofsAddr + FuncArg);
261 writew(cmd, ofsAddr + FuncCode);
262 moxa_wait_finish(ofsAddr);
263 ret = readw(ofsAddr + FuncArg);
264 spin_unlock_irqrestore(&moxafunc_lock, flags);
265 return ret;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700266}
267
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700268static void moxa_low_water_check(void __iomem *ofsAddr)
269{
270 u16 rptr, wptr, mask, len;
271
272 if (readb(ofsAddr + FlagStat) & Xoff_state) {
273 rptr = readw(ofsAddr + RXrptr);
274 wptr = readw(ofsAddr + RXwptr);
275 mask = readw(ofsAddr + RX_mask);
276 len = (wptr - rptr) & mask;
277 if (len <= Low_water)
278 moxafunc(ofsAddr, FC_SendXon, 0);
279 }
280}
281
Jiri Slaby74d7d972008-04-30 00:53:43 -0700282/*
283 * TTY operations
284 */
285
Alan Cox6caa76b2011-02-14 16:27:22 +0000286static int moxa_ioctl(struct tty_struct *tty,
Jiri Slaby74d7d972008-04-30 00:53:43 -0700287 unsigned int cmd, unsigned long arg)
288{
289 struct moxa_port *ch = tty->driver_data;
290 void __user *argp = (void __user *)arg;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700291 int status, ret = 0;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700292
293 if (tty->index == MAX_PORTS) {
294 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
295 cmd != MOXA_GETMSTATUS)
296 return -EINVAL;
297 } else if (!ch)
298 return -ENODEV;
299
300 switch (cmd) {
301 case MOXA_GETDATACOUNT:
302 moxaLog.tick = jiffies;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700303 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
304 ret = -EFAULT;
305 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700306 case MOXA_FLUSH_QUEUE:
307 MoxaPortFlushData(ch, arg);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700308 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700309 case MOXA_GET_IOQUEUE: {
310 struct moxaq_str __user *argm = argp;
311 struct moxaq_str tmp;
312 struct moxa_port *p;
313 unsigned int i, j;
314
315 for (i = 0; i < MAX_BOARDS; i++) {
316 p = moxa_boards[i].ports;
317 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
318 memset(&tmp, 0, sizeof(tmp));
Alan Coxe8c62102009-11-30 13:18:24 +0000319 spin_lock_bh(&moxa_lock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700320 if (moxa_boards[i].ready) {
321 tmp.inq = MoxaPortRxQueue(p);
322 tmp.outq = MoxaPortTxQueue(p);
323 }
Alan Coxe8c62102009-11-30 13:18:24 +0000324 spin_unlock_bh(&moxa_lock);
325 if (copy_to_user(argm, &tmp, sizeof(tmp)))
Jiri Slaby74d7d972008-04-30 00:53:43 -0700326 return -EFAULT;
327 }
328 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700329 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700330 } case MOXA_GET_OQUEUE:
331 status = MoxaPortTxQueue(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700332 ret = put_user(status, (unsigned long __user *)argp);
333 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700334 case MOXA_GET_IQUEUE:
335 status = MoxaPortRxQueue(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700336 ret = put_user(status, (unsigned long __user *)argp);
337 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700338 case MOXA_GETMSTATUS: {
339 struct mxser_mstatus __user *argm = argp;
340 struct mxser_mstatus tmp;
341 struct moxa_port *p;
342 unsigned int i, j;
343
344 for (i = 0; i < MAX_BOARDS; i++) {
345 p = moxa_boards[i].ports;
346 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
Alan Coxd450b5a2008-10-13 10:39:58 +0100347 struct tty_struct *ttyp;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700348 memset(&tmp, 0, sizeof(tmp));
Alan Coxe8c62102009-11-30 13:18:24 +0000349 spin_lock_bh(&moxa_lock);
350 if (!moxa_boards[i].ready) {
351 spin_unlock_bh(&moxa_lock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700352 goto copy;
Alan Coxe8c62102009-11-30 13:18:24 +0000353 }
Jiri Slaby74d7d972008-04-30 00:53:43 -0700354
355 status = MoxaPortLineStatus(p);
Alan Coxe8c62102009-11-30 13:18:24 +0000356 spin_unlock_bh(&moxa_lock);
357
Jiri Slaby74d7d972008-04-30 00:53:43 -0700358 if (status & 1)
359 tmp.cts = 1;
360 if (status & 2)
361 tmp.dsr = 1;
362 if (status & 4)
363 tmp.dcd = 1;
364
Alan Coxd450b5a2008-10-13 10:39:58 +0100365 ttyp = tty_port_tty_get(&p->port);
Alan Coxadc8d742012-07-14 15:31:47 +0100366 if (!ttyp)
Jiri Slaby74d7d972008-04-30 00:53:43 -0700367 tmp.cflag = p->cflag;
368 else
Alan Coxadc8d742012-07-14 15:31:47 +0100369 tmp.cflag = ttyp->termios.c_cflag;
Julia Lawalldf43daa2011-04-22 22:46:21 +0200370 tty_kref_put(ttyp);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700371copy:
Alan Coxe8c62102009-11-30 13:18:24 +0000372 if (copy_to_user(argm, &tmp, sizeof(tmp)))
Jiri Slaby74d7d972008-04-30 00:53:43 -0700373 return -EFAULT;
374 }
375 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700376 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700377 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700378 default:
379 ret = -ENOIOCTLCMD;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700380 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700381 return ret;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700382}
383
Alan Cox9e98966c2008-07-22 11:18:03 +0100384static int moxa_break_ctl(struct tty_struct *tty, int state)
Jiri Slaby74d7d972008-04-30 00:53:43 -0700385{
386 struct moxa_port *port = tty->driver_data;
387
388 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
389 Magic_code);
Alan Cox9e98966c2008-07-22 11:18:03 +0100390 return 0;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700391}
392
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700393static const struct tty_operations moxa_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 .open = moxa_open,
395 .close = moxa_close,
396 .write = moxa_write,
397 .write_room = moxa_write_room,
398 .flush_buffer = moxa_flush_buffer,
399 .chars_in_buffer = moxa_chars_in_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 .ioctl = moxa_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 .set_termios = moxa_set_termios,
402 .stop = moxa_stop,
403 .start = moxa_start,
404 .hangup = moxa_hangup,
Jiri Slaby74d7d972008-04-30 00:53:43 -0700405 .break_ctl = moxa_break_ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 .tiocmget = moxa_tiocmget,
407 .tiocmset = moxa_tiocmset,
Al Viro1c729ab2018-09-11 22:01:00 -0400408 .set_serial = moxa_set_serial_info,
409 .get_serial = moxa_get_serial_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410};
411
Alan Cox31f35932009-01-02 13:45:05 +0000412static const struct tty_port_operations moxa_port_ops = {
413 .carrier_raised = moxa_carrier_raised,
Alan Coxf1761782009-11-30 13:17:51 +0000414 .dtr_rts = moxa_dtr_rts,
415 .shutdown = moxa_shutdown,
Alan Cox31f35932009-01-02 13:45:05 +0000416};
417
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800418static struct tty_driver *moxaDriver;
Kees Cook1d27e3e2017-10-04 16:27:04 -0700419static DEFINE_TIMER(moxaTimer, moxa_poll);
Alan Cox33f0f882006-01-09 20:54:13 -0800420
Jiri Slaby74d7d972008-04-30 00:53:43 -0700421/*
422 * HW init
423 */
424
Jiri Slaby03718232008-04-30 00:53:39 -0700425static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
426{
427 switch (brd->boardType) {
428 case MOXA_BOARD_C218_ISA:
429 case MOXA_BOARD_C218_PCI:
430 if (model != 1)
431 goto err;
432 break;
433 case MOXA_BOARD_CP204J:
434 if (model != 3)
435 goto err;
436 break;
437 default:
438 if (model != 2)
439 goto err;
440 break;
441 }
442 return 0;
443err:
444 return -EINVAL;
445}
446
447static int moxa_check_fw(const void *ptr)
448{
449 const __le16 *lptr = ptr;
450
451 if (*lptr != cpu_to_le16(0x7980))
452 return -EINVAL;
453
454 return 0;
455}
456
457static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
458 size_t len)
459{
460 void __iomem *baseAddr = brd->basemem;
461 u16 tmp;
462
463 writeb(HW_reset, baseAddr + Control_reg); /* reset */
464 msleep(10);
465 memset_io(baseAddr, 0, 4096);
466 memcpy_toio(baseAddr, buf, len); /* download BIOS */
467 writeb(0, baseAddr + Control_reg); /* restart */
468
469 msleep(2000);
470
471 switch (brd->boardType) {
472 case MOXA_BOARD_C218_ISA:
473 case MOXA_BOARD_C218_PCI:
474 tmp = readw(baseAddr + C218_key);
475 if (tmp != C218_KeyCode)
476 goto err;
477 break;
478 case MOXA_BOARD_CP204J:
479 tmp = readw(baseAddr + C218_key);
480 if (tmp != CP204J_KeyCode)
481 goto err;
482 break;
483 default:
484 tmp = readw(baseAddr + C320_key);
485 if (tmp != C320_KeyCode)
486 goto err;
487 tmp = readw(baseAddr + C320_status);
488 if (tmp != STS_init) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700489 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
Jiri Slaby03718232008-04-30 00:53:39 -0700490 "module not found\n");
491 return -EIO;
492 }
493 break;
494 }
495
496 return 0;
497err:
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700498 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700499 return -EIO;
500}
501
502static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
503 size_t len)
504{
505 void __iomem *baseAddr = brd->basemem;
506
507 if (len < 7168) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700508 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700509 return -EINVAL;
510 }
511
512 writew(len - 7168 - 2, baseAddr + C320bapi_len);
513 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
514 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
515 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
516 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
517
518 return 0;
519}
520
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700521static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
Jiri Slaby03718232008-04-30 00:53:39 -0700522 size_t len)
523{
524 void __iomem *baseAddr = brd->basemem;
Harvey Harrisonb46f69c2008-10-15 22:04:19 -0700525 const __le16 *uptr = ptr;
Jiri Slaby03718232008-04-30 00:53:39 -0700526 size_t wlen, len2, j;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700527 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
Jiri Slaby08d01c72008-04-30 00:53:47 -0700528 unsigned int i, retry;
Jiri Slaby03718232008-04-30 00:53:39 -0700529 u16 usum, keycode;
530
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700531 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
532 C218_KeyCode;
533
534 switch (brd->boardType) {
535 case MOXA_BOARD_CP204J:
536 case MOXA_BOARD_C218_ISA:
537 case MOXA_BOARD_C218_PCI:
538 key = C218_key;
539 loadbuf = C218_LoadBuf;
540 loadlen = C218DLoad_len;
541 checksum = C218check_sum;
542 checksum_ok = C218chksum_ok;
543 break;
544 default:
545 key = C320_key;
546 keycode = C320_KeyCode;
547 loadbuf = C320_LoadBuf;
548 loadlen = C320DLoad_len;
549 checksum = C320check_sum;
550 checksum_ok = C320chksum_ok;
551 break;
552 }
553
Jiri Slaby03718232008-04-30 00:53:39 -0700554 usum = 0;
555 wlen = len >> 1;
556 for (i = 0; i < wlen; i++)
557 usum += le16_to_cpu(uptr[i]);
558 retry = 0;
559 do {
560 wlen = len >> 1;
561 j = 0;
562 while (wlen) {
563 len2 = (wlen > 2048) ? 2048 : wlen;
564 wlen -= len2;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700565 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
Jiri Slaby03718232008-04-30 00:53:39 -0700566 j += len2 << 1;
567
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700568 writew(len2, baseAddr + loadlen);
569 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700570 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700571 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700572 break;
573 msleep(10);
574 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700575 if (readw(baseAddr + key) != keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700576 return -EIO;
577 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700578 writew(0, baseAddr + loadlen);
579 writew(usum, baseAddr + checksum);
580 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700581 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700582 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700583 break;
584 msleep(10);
585 }
586 retry++;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700587 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
588 if (readb(baseAddr + checksum_ok) != 1)
Jiri Slaby03718232008-04-30 00:53:39 -0700589 return -EIO;
590
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700591 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700592 for (i = 0; i < 600; i++) {
593 if (readw(baseAddr + Magic_no) == Magic_code)
594 break;
595 msleep(10);
596 }
597 if (readw(baseAddr + Magic_no) != Magic_code)
598 return -EIO;
599
Jiri Slaby08d01c72008-04-30 00:53:47 -0700600 if (MOXA_IS_320(brd)) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700601 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
602 writew(0x3800, baseAddr + TMS320_PORT1);
603 writew(0x3900, baseAddr + TMS320_PORT2);
604 writew(28499, baseAddr + TMS320_CLOCK);
605 } else {
606 writew(0x3200, baseAddr + TMS320_PORT1);
607 writew(0x3400, baseAddr + TMS320_PORT2);
608 writew(19999, baseAddr + TMS320_CLOCK);
609 }
Jiri Slaby03718232008-04-30 00:53:39 -0700610 }
611 writew(1, baseAddr + Disable_IRQ);
612 writew(0, baseAddr + Magic_no);
613 for (i = 0; i < 500; i++) {
614 if (readw(baseAddr + Magic_no) == Magic_code)
615 break;
616 msleep(10);
617 }
618 if (readw(baseAddr + Magic_no) != Magic_code)
619 return -EIO;
620
Jiri Slaby08d01c72008-04-30 00:53:47 -0700621 if (MOXA_IS_320(brd)) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700622 j = readw(baseAddr + Module_cnt);
623 if (j <= 0)
624 return -EIO;
625 brd->numPorts = j * 8;
626 writew(j, baseAddr + Module_no);
627 writew(0, baseAddr + Magic_no);
628 for (i = 0; i < 600; i++) {
629 if (readw(baseAddr + Magic_no) == Magic_code)
630 break;
631 msleep(10);
632 }
633 if (readw(baseAddr + Magic_no) != Magic_code)
634 return -EIO;
Jiri Slaby03718232008-04-30 00:53:39 -0700635 }
Jiri Slaby03718232008-04-30 00:53:39 -0700636 brd->intNdx = baseAddr + IRQindex;
637 brd->intPend = baseAddr + IRQpending;
638 brd->intTable = baseAddr + IRQtable;
639
640 return 0;
641}
642
643static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
644 size_t len)
645{
646 void __iomem *ofsAddr, *baseAddr = brd->basemem;
647 struct moxa_port *port;
648 int retval, i;
649
650 if (len % 2) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700651 printk(KERN_ERR "MOXA: bios length is not even\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700652 return -EINVAL;
653 }
654
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700655 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
656 if (retval)
657 return retval;
658
Jiri Slaby03718232008-04-30 00:53:39 -0700659 switch (brd->boardType) {
660 case MOXA_BOARD_C218_ISA:
661 case MOXA_BOARD_C218_PCI:
662 case MOXA_BOARD_CP204J:
Jiri Slaby03718232008-04-30 00:53:39 -0700663 port = brd->ports;
664 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700665 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700666 port->DCDState = 0;
667 port->tableAddr = baseAddr + Extern_table +
668 Extern_size * i;
669 ofsAddr = port->tableAddr;
670 writew(C218rx_mask, ofsAddr + RX_mask);
671 writew(C218tx_mask, ofsAddr + TX_mask);
672 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
673 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
674
675 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
676 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
677
678 }
679 break;
680 default:
Jiri Slaby03718232008-04-30 00:53:39 -0700681 port = brd->ports;
682 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700683 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700684 port->DCDState = 0;
685 port->tableAddr = baseAddr + Extern_table +
686 Extern_size * i;
687 ofsAddr = port->tableAddr;
688 switch (brd->numPorts) {
689 case 8:
690 writew(C320p8rx_mask, ofsAddr + RX_mask);
691 writew(C320p8tx_mask, ofsAddr + TX_mask);
692 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
693 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
694 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
695 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
696
697 break;
698 case 16:
699 writew(C320p16rx_mask, ofsAddr + RX_mask);
700 writew(C320p16tx_mask, ofsAddr + TX_mask);
701 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
702 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
703 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
704 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
705 break;
706
707 case 24:
708 writew(C320p24rx_mask, ofsAddr + RX_mask);
709 writew(C320p24tx_mask, ofsAddr + TX_mask);
710 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
711 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
712 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
713 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
714 break;
715 case 32:
716 writew(C320p32rx_mask, ofsAddr + RX_mask);
717 writew(C320p32tx_mask, ofsAddr + TX_mask);
718 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
719 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
720 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
721 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
722 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
723 break;
724 }
725 }
726 break;
727 }
Jiri Slaby03718232008-04-30 00:53:39 -0700728 return 0;
729}
730
731static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
732{
David Howells2bca76e2008-07-08 17:37:15 +0100733 const void *ptr = fw->data;
Jiri Slaby03718232008-04-30 00:53:39 -0700734 char rsn[64];
735 u16 lens[5];
736 size_t len;
737 unsigned int a, lenp, lencnt;
738 int ret = -EINVAL;
739 struct {
740 __le32 magic; /* 0x34303430 */
741 u8 reserved1[2];
742 u8 type; /* UNIX = 3 */
743 u8 model; /* C218T=1, C320T=2, CP204=3 */
744 u8 reserved2[8];
745 __le16 len[5];
David Howells2bca76e2008-07-08 17:37:15 +0100746 } const *hdr = ptr;
Jiri Slaby03718232008-04-30 00:53:39 -0700747
748 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
749
750 if (fw->size < MOXA_FW_HDRLEN) {
751 strcpy(rsn, "too short (even header won't fit)");
752 goto err;
753 }
754 if (hdr->magic != cpu_to_le32(0x30343034)) {
755 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
756 goto err;
757 }
758 if (hdr->type != 3) {
759 sprintf(rsn, "not for linux, type is %u", hdr->type);
760 goto err;
761 }
762 if (moxa_check_fw_model(brd, hdr->model)) {
763 sprintf(rsn, "not for this card, model is %u", hdr->model);
764 goto err;
765 }
766
767 len = MOXA_FW_HDRLEN;
768 lencnt = hdr->model == 2 ? 5 : 3;
769 for (a = 0; a < ARRAY_SIZE(lens); a++) {
770 lens[a] = le16_to_cpu(hdr->len[a]);
771 if (lens[a] && len + lens[a] <= fw->size &&
772 moxa_check_fw(&fw->data[len]))
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700773 printk(KERN_WARNING "MOXA firmware: unexpected input "
Jiri Slaby03718232008-04-30 00:53:39 -0700774 "at offset %u, but going on\n", (u32)len);
775 if (!lens[a] && a < lencnt) {
776 sprintf(rsn, "too few entries in fw file");
777 goto err;
778 }
779 len += lens[a];
780 }
781
782 if (len != fw->size) {
783 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
784 (u32)len);
785 goto err;
786 }
787
788 ptr += MOXA_FW_HDRLEN;
789 lenp = 0; /* bios */
790
791 strcpy(rsn, "read above");
792
793 ret = moxa_load_bios(brd, ptr, lens[lenp]);
794 if (ret)
795 goto err;
796
797 /* we skip the tty section (lens[1]), since we don't need it */
798 ptr += lens[lenp] + lens[lenp + 1];
799 lenp += 2; /* comm */
800
801 if (hdr->model == 2) {
802 ret = moxa_load_320b(brd, ptr, lens[lenp]);
803 if (ret)
804 goto err;
805 /* skip another tty */
806 ptr += lens[lenp] + lens[lenp + 1];
807 lenp += 2;
808 }
809
810 ret = moxa_load_code(brd, ptr, lens[lenp]);
811 if (ret)
812 goto err;
813
814 return 0;
815err:
816 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
817 return ret;
818}
819
820static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
821{
822 const struct firmware *fw;
823 const char *file;
Jiri Slaby810ab092008-04-30 00:53:41 -0700824 struct moxa_port *p;
Jiri Slaby793be892012-08-07 21:47:55 +0200825 unsigned int i, first_idx;
Jiri Slaby03718232008-04-30 00:53:39 -0700826 int ret;
827
Jiri Slaby810ab092008-04-30 00:53:41 -0700828 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
829 GFP_KERNEL);
830 if (brd->ports == NULL) {
831 printk(KERN_ERR "cannot allocate memory for ports\n");
832 ret = -ENOMEM;
833 goto err;
834 }
835
836 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
Alan Cox9de6a512008-07-16 21:56:02 +0100837 tty_port_init(&p->port);
Alan Cox31f35932009-01-02 13:45:05 +0000838 p->port.ops = &moxa_port_ops;
Alan Cox44b7d1b2008-07-16 21:57:18 +0100839 p->type = PORT_16550A;
840 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Jiri Slaby810ab092008-04-30 00:53:41 -0700841 }
842
Jiri Slaby03718232008-04-30 00:53:39 -0700843 switch (brd->boardType) {
844 case MOXA_BOARD_C218_ISA:
845 case MOXA_BOARD_C218_PCI:
846 file = "c218tunx.cod";
847 break;
848 case MOXA_BOARD_CP204J:
849 file = "cp204unx.cod";
850 break;
851 default:
852 file = "c320tunx.cod";
853 break;
854 }
855
856 ret = request_firmware(&fw, file, dev);
857 if (ret) {
Jiri Slabyec09cd52008-04-30 00:53:49 -0700858 printk(KERN_ERR "MOXA: request_firmware failed. Make sure "
859 "you've placed '%s' file into your firmware "
860 "loader directory (e.g. /lib/firmware)\n",
861 file);
Jiri Slaby810ab092008-04-30 00:53:41 -0700862 goto err_free;
Jiri Slaby03718232008-04-30 00:53:39 -0700863 }
864
865 ret = moxa_load_fw(brd, fw);
866
867 release_firmware(fw);
Jiri Slaby810ab092008-04-30 00:53:41 -0700868
869 if (ret)
870 goto err_free;
871
Jiri Slaby2a541342008-04-30 00:53:45 -0700872 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700873 brd->ready = 1;
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700874 if (!timer_pending(&moxaTimer))
875 mod_timer(&moxaTimer, jiffies + HZ / 50);
Jiri Slaby2a541342008-04-30 00:53:45 -0700876 spin_unlock_bh(&moxa_lock);
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700877
Jiri Slaby793be892012-08-07 21:47:55 +0200878 first_idx = (brd - moxa_boards) * MAX_PORTS_PER_BOARD;
879 for (i = 0; i < brd->numPorts; i++)
880 tty_port_register_device(&brd->ports[i].port, moxaDriver,
881 first_idx + i, dev);
882
Jiri Slaby810ab092008-04-30 00:53:41 -0700883 return 0;
884err_free:
Jiri Slaby191c5f12012-11-15 09:49:56 +0100885 for (i = 0; i < MAX_PORTS_PER_BOARD; i++)
886 tty_port_destroy(&brd->ports[i].port);
Jiri Slaby810ab092008-04-30 00:53:41 -0700887 kfree(brd->ports);
888err:
Jiri Slaby03718232008-04-30 00:53:39 -0700889 return ret;
890}
891
Jiri Slaby810ab092008-04-30 00:53:41 -0700892static void moxa_board_deinit(struct moxa_board_conf *brd)
893{
Jiri Slaby793be892012-08-07 21:47:55 +0200894 unsigned int a, opened, first_idx;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700895
896 mutex_lock(&moxa_openlock);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700897 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700898 brd->ready = 0;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700899 spin_unlock_bh(&moxa_lock);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700900
901 /* pci hot-un-plug support */
902 for (a = 0; a < brd->numPorts; a++)
Peter Hurleyd41861c2016-04-09 17:53:25 -0700903 if (tty_port_initialized(&brd->ports[a].port))
Jiri Slabyaa27a092013-03-07 13:12:30 +0100904 tty_port_tty_hangup(&brd->ports[a].port, false);
905
Jiri Slaby191c5f12012-11-15 09:49:56 +0100906 for (a = 0; a < MAX_PORTS_PER_BOARD; a++)
907 tty_port_destroy(&brd->ports[a].port);
Jiri Slabyaa27a092013-03-07 13:12:30 +0100908
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700909 while (1) {
910 opened = 0;
911 for (a = 0; a < brd->numPorts; a++)
Peter Hurleyd41861c2016-04-09 17:53:25 -0700912 if (tty_port_initialized(&brd->ports[a].port))
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700913 opened++;
914 mutex_unlock(&moxa_openlock);
915 if (!opened)
916 break;
917 msleep(50);
918 mutex_lock(&moxa_openlock);
919 }
920
Jiri Slaby793be892012-08-07 21:47:55 +0200921 first_idx = (brd - moxa_boards) * MAX_PORTS_PER_BOARD;
922 for (a = 0; a < brd->numPorts; a++)
923 tty_unregister_device(moxaDriver, first_idx + a);
924
Jiri Slaby810ab092008-04-30 00:53:41 -0700925 iounmap(brd->basemem);
926 brd->basemem = NULL;
927 kfree(brd->ports);
928}
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930#ifdef CONFIG_PCI
Bill Pemberton9671f092012-11-19 13:21:50 -0500931static int moxa_pci_probe(struct pci_dev *pdev,
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800932 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800934 struct moxa_board_conf *board;
935 unsigned int i;
936 int board_type = ent->driver_data;
937 int retval;
938
939 retval = pci_enable_device(pdev);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700940 if (retval) {
941 dev_err(&pdev->dev, "can't enable pci device\n");
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800942 goto err;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700943 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800944
945 for (i = 0; i < MAX_BOARDS; i++)
946 if (moxa_boards[i].basemem == NULL)
947 break;
948
949 retval = -ENODEV;
950 if (i >= MAX_BOARDS) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700951 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800952 "found. Board is ignored.\n", MAX_BOARDS);
953 goto err;
954 }
955
956 board = &moxa_boards[i];
Jiri Slabye46a5e32008-04-30 00:53:37 -0700957
958 retval = pci_request_region(pdev, 2, "moxa-base");
959 if (retval) {
960 dev_err(&pdev->dev, "can't request pci region 2\n");
961 goto err;
962 }
963
Alan Cox24cb2332008-04-30 00:54:19 -0700964 board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700965 if (board->basemem == NULL) {
966 dev_err(&pdev->dev, "can't remap io space 2\n");
Julia Lawall386d95b2012-08-14 08:47:35 +0200967 retval = -ENOMEM;
Jiri Slabye46a5e32008-04-30 00:53:37 -0700968 goto err_reg;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700969 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 board->boardType = board_type;
972 switch (board_type) {
973 case MOXA_BOARD_C218_ISA:
974 case MOXA_BOARD_C218_PCI:
975 board->numPorts = 8;
976 break;
977
978 case MOXA_BOARD_CP204J:
979 board->numPorts = 4;
980 break;
981 default:
982 board->numPorts = 0;
983 break;
984 }
985 board->busType = MOXA_BUS_TYPE_PCI;
Jiri Slabya784bf72007-02-10 01:45:36 -0800986
Jiri Slaby03718232008-04-30 00:53:39 -0700987 retval = moxa_init_board(board, &pdev->dev);
988 if (retval)
989 goto err_base;
990
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800991 pci_set_drvdata(pdev, board);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Jiri Slabybb9f9102008-04-30 00:53:48 -0700993 dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n",
994 moxa_brdname[board_type - 1], board->numPorts);
995
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700996 return 0;
Jiri Slaby03718232008-04-30 00:53:39 -0700997err_base:
998 iounmap(board->basemem);
999 board->basemem = NULL;
Jiri Slabye46a5e32008-04-30 00:53:37 -07001000err_reg:
1001 pci_release_region(pdev, 2);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001002err:
1003 return retval;
1004}
1005
Bill Pembertonae8d8a12012-11-19 13:26:18 -05001006static void moxa_pci_remove(struct pci_dev *pdev)
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001007{
1008 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
1009
Jiri Slaby810ab092008-04-30 00:53:41 -07001010 moxa_board_deinit(brd);
1011
Jiri Slabye46a5e32008-04-30 00:53:37 -07001012 pci_release_region(pdev, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
Jiri Slabya784bf72007-02-10 01:45:36 -08001014
1015static struct pci_driver moxa_pci_driver = {
1016 .name = "moxa",
1017 .id_table = moxa_pcibrds,
1018 .probe = moxa_pci_probe,
Bill Pemberton91116cb2012-11-19 13:21:06 -05001019 .remove = moxa_pci_remove
Jiri Slabya784bf72007-02-10 01:45:36 -08001020};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021#endif /* CONFIG_PCI */
1022
1023static int __init moxa_init(void)
1024{
Jiri Slaby810ab092008-04-30 00:53:41 -07001025 unsigned int isabrds = 0;
Jiri Slabyd353eca2008-04-30 00:53:37 -07001026 int retval = 0;
Rakib Mullickc6fc8262009-12-09 12:34:18 -08001027 struct moxa_board_conf *brd = moxa_boards;
1028 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001030 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
1031 MOXA_VERSION);
Jiri Slaby793be892012-08-07 21:47:55 +02001032
1033 tty_port_init(&moxa_service_port);
1034
1035 moxaDriver = tty_alloc_driver(MAX_PORTS + 1,
1036 TTY_DRIVER_REAL_RAW |
1037 TTY_DRIVER_DYNAMIC_DEV);
Dan Carpenterc3a63442012-08-16 16:16:56 +03001038 if (IS_ERR(moxaDriver))
1039 return PTR_ERR(moxaDriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Sergey Vlasov9b4e3b12005-09-03 16:26:49 +01001041 moxaDriver->name = "ttyMX";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 moxaDriver->major = ttymajor;
1043 moxaDriver->minor_start = 0;
1044 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1045 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1046 moxaDriver->init_termios = tty_std_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Alan Cox606d0992006-12-08 02:38:45 -08001048 moxaDriver->init_termios.c_ispeed = 9600;
1049 moxaDriver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 tty_set_operations(moxaDriver, &moxa_ops);
Jiri Slaby793be892012-08-07 21:47:55 +02001051 /* Having one more port only for ioctls is ugly */
1052 tty_port_link_device(&moxa_service_port, moxaDriver, MAX_PORTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (tty_register_driver(moxaDriver)) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001055 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 put_tty_driver(moxaDriver);
1057 return -1;
1058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Jiri Slabyd353eca2008-04-30 00:53:37 -07001060 /* Find the boards defined from module args. */
Rakib Mullickc6fc8262009-12-09 12:34:18 -08001061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 for (i = 0; i < MAX_BOARDS; i++) {
Jiri Slabyd353eca2008-04-30 00:53:37 -07001063 if (!baseaddr[i])
1064 break;
1065 if (type[i] == MOXA_BOARD_C218_ISA ||
1066 type[i] == MOXA_BOARD_C320_ISA) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001067 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001068 isabrds + 1, moxa_brdname[type[i] - 1],
1069 baseaddr[i]);
1070 brd->boardType = type[i];
1071 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1072 numports[i];
1073 brd->busType = MOXA_BUS_TYPE_ISA;
Alan Cox24cb2332008-04-30 00:54:19 -07001074 brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
Jiri Slabyd353eca2008-04-30 00:53:37 -07001075 if (!brd->basemem) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001076 printk(KERN_ERR "MOXA: can't remap %lx\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001077 baseaddr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 continue;
1079 }
Jiri Slaby03718232008-04-30 00:53:39 -07001080 if (moxa_init_board(brd, NULL)) {
1081 iounmap(brd->basemem);
1082 brd->basemem = NULL;
1083 continue;
1084 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001085
Hans Wennborge0525392014-08-05 21:43:42 -07001086 printk(KERN_INFO "MOXA isa board found at 0x%.8lx and "
Jiri Slabybb9f9102008-04-30 00:53:48 -07001087 "ready (%u ports, firmware loaded)\n",
1088 baseaddr[i], brd->numPorts);
1089
Jiri Slabyd353eca2008-04-30 00:53:37 -07001090 brd++;
1091 isabrds++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
1093 }
Jiri Slabya784bf72007-02-10 01:45:36 -08001094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001096 retval = pci_register_driver(&moxa_pci_driver);
1097 if (retval) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001098 printk(KERN_ERR "Can't register MOXA pci driver!\n");
Jiri Slabyd353eca2008-04-30 00:53:37 -07001099 if (isabrds)
Jiri Slabya784bf72007-02-10 01:45:36 -08001100 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
1102#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001103
Jiri Slabya784bf72007-02-10 01:45:36 -08001104 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
1107static void __exit moxa_exit(void)
1108{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001109 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001111#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001112 pci_unregister_driver(&moxa_pci_driver);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001113#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001114
Jiri Slaby810ab092008-04-30 00:53:41 -07001115 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1116 if (moxa_boards[i].ready)
1117 moxa_board_deinit(&moxa_boards[i]);
Jiri Slaby2a541342008-04-30 00:53:45 -07001118
1119 del_timer_sync(&moxaTimer);
1120
1121 if (tty_unregister_driver(moxaDriver))
1122 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1123 "serial driver\n");
1124 put_tty_driver(moxaDriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
1127module_init(moxa_init);
1128module_exit(moxa_exit);
1129
Alan Coxf1761782009-11-30 13:17:51 +00001130static void moxa_shutdown(struct tty_port *port)
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001131{
Alan Coxf1761782009-11-30 13:17:51 +00001132 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1133 MoxaPortDisable(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001134 MoxaPortFlushData(ch, 2);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001135}
1136
Alan Cox31f35932009-01-02 13:45:05 +00001137static int moxa_carrier_raised(struct tty_port *port)
1138{
1139 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1140 int dcd;
1141
Alan Cox8482bcd2009-11-30 13:18:13 +00001142 spin_lock_irq(&port->lock);
Alan Cox31f35932009-01-02 13:45:05 +00001143 dcd = ch->DCDState;
Alan Cox8482bcd2009-11-30 13:18:13 +00001144 spin_unlock_irq(&port->lock);
Alan Cox31f35932009-01-02 13:45:05 +00001145 return dcd;
1146}
1147
Alan Coxf1761782009-11-30 13:17:51 +00001148static void moxa_dtr_rts(struct tty_port *port, int onoff)
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001149{
Alan Coxf1761782009-11-30 13:17:51 +00001150 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1151 MoxaPortLineCtrl(ch, onoff, onoff);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001152}
1153
Alan Coxf1761782009-11-30 13:17:51 +00001154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155static int moxa_open(struct tty_struct *tty, struct file *filp)
1156{
Jiri Slaby810ab092008-04-30 00:53:41 -07001157 struct moxa_board_conf *brd;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001158 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 int port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Jiri Slaby11324ed2007-02-10 01:45:31 -08001161 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (port == MAX_PORTS) {
Jiri Slaby74d7d972008-04-30 00:53:43 -07001163 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001165 if (mutex_lock_interruptible(&moxa_openlock))
1166 return -ERESTARTSYS;
Jiri Slaby810ab092008-04-30 00:53:41 -07001167 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001168 if (!brd->ready) {
1169 mutex_unlock(&moxa_openlock);
Jiri Slaby810ab092008-04-30 00:53:41 -07001170 return -ENODEV;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Dirk Eibachf0e85272009-06-11 14:56:44 +01001173 if (port % MAX_PORTS_PER_BOARD >= brd->numPorts) {
1174 mutex_unlock(&moxa_openlock);
1175 return -ENODEV;
1176 }
1177
Jiri Slaby810ab092008-04-30 00:53:41 -07001178 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
Alan Cox9de6a512008-07-16 21:56:02 +01001179 ch->port.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 tty->driver_data = ch;
Alan Coxd450b5a2008-10-13 10:39:58 +01001181 tty_port_tty_set(&ch->port, tty);
Alan Coxf1761782009-11-30 13:17:51 +00001182 mutex_lock(&ch->port.mutex);
Peter Hurleyd41861c2016-04-09 17:53:25 -07001183 if (!tty_port_initialized(&ch->port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 ch->statusflags = 0;
Alan Coxadc8d742012-07-14 15:31:47 +01001185 moxa_set_tty_param(tty, &tty->termios);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001186 MoxaPortLineCtrl(ch, 1, 1);
1187 MoxaPortEnable(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001188 MoxaSetFifo(ch, ch->type == PORT_16550A);
Peter Hurleyd41861c2016-04-09 17:53:25 -07001189 tty_port_set_initialized(&ch->port, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
Alan Coxf1761782009-11-30 13:17:51 +00001191 mutex_unlock(&ch->port.mutex);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001192 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Jiri Slaby7c31bdb2011-03-29 23:23:41 +02001194 return tty_port_block_til_ready(&ch->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
1197static void moxa_close(struct tty_struct *tty, struct file *filp)
1198{
Alan Coxf1761782009-11-30 13:17:51 +00001199 struct moxa_port *ch = tty->driver_data;
Alan Coxadc8d742012-07-14 15:31:47 +01001200 ch->cflag = tty->termios.c_cflag;
Alan Coxf1761782009-11-30 13:17:51 +00001201 tty_port_close(&ch->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
1204static int moxa_write(struct tty_struct *tty,
1205 const unsigned char *buf, int count)
1206{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001207 struct moxa_port *ch = tty->driver_data;
Jiri Slaby0ad7c9a2011-04-20 10:43:15 +02001208 unsigned long flags;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001209 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (ch == NULL)
Jiri Slabyb4173f42008-04-30 00:53:40 -07001212 return 0;
Alan Cox33f0f882006-01-09 20:54:13 -08001213
Jiri Slaby0ad7c9a2011-04-20 10:43:15 +02001214 spin_lock_irqsave(&moxa_lock, flags);
Alan Coxd450b5a2008-10-13 10:39:58 +01001215 len = MoxaPortWriteData(tty, buf, count);
Jiri Slaby0ad7c9a2011-04-20 10:43:15 +02001216 spin_unlock_irqrestore(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Alan Coxa808ac02009-11-30 13:18:02 +00001218 set_bit(LOWWAIT, &ch->statusflags);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001219 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
1222static int moxa_write_room(struct tty_struct *tty)
1223{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001224 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 if (tty->stopped)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001227 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001228 ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (ch == NULL)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001230 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001231 return MoxaPortTxFree(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
1234static void moxa_flush_buffer(struct tty_struct *tty)
1235{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001236 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 if (ch == NULL)
1239 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001240 MoxaPortFlushData(ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 tty_wakeup(tty);
1242}
1243
1244static int moxa_chars_in_buffer(struct tty_struct *tty)
1245{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001246 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Jiri Slabyb4173f42008-04-30 00:53:40 -07001249 chars = MoxaPortTxQueue(ch);
Alan Coxf710ebd2009-11-30 13:18:18 +00001250 if (chars)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 /*
1252 * Make it possible to wakeup anything waiting for output
1253 * in tty_ioctl.c, etc.
1254 */
Alan Coxf710ebd2009-11-30 13:18:18 +00001255 set_bit(EMPTYWAIT, &ch->statusflags);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001256 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
Alan Cox60b33c12011-02-14 16:26:14 +00001259static int moxa_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Alan Cox8482bcd2009-11-30 13:18:13 +00001261 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 int flag = 0, dtr, rts;
1263
Jiri Slabyb4173f42008-04-30 00:53:40 -07001264 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (dtr)
1266 flag |= TIOCM_DTR;
1267 if (rts)
1268 flag |= TIOCM_RTS;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001269 dtr = MoxaPortLineStatus(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (dtr & 1)
1271 flag |= TIOCM_CTS;
1272 if (dtr & 2)
1273 flag |= TIOCM_DSR;
1274 if (dtr & 4)
1275 flag |= TIOCM_CD;
1276 return flag;
1277}
1278
Alan Cox20b9d172011-02-14 16:26:50 +00001279static int moxa_tiocmset(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 unsigned int set, unsigned int clear)
1281{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001282 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 int dtr, rts;
1284
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001285 mutex_lock(&moxa_openlock);
1286 ch = tty->driver_data;
1287 if (!ch) {
1288 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -07001289 return -EINVAL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Jiri Slabyb4173f42008-04-30 00:53:40 -07001292 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (set & TIOCM_RTS)
1294 rts = 1;
1295 if (set & TIOCM_DTR)
1296 dtr = 1;
1297 if (clear & TIOCM_RTS)
1298 rts = 0;
1299 if (clear & TIOCM_DTR)
1300 dtr = 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001301 MoxaPortLineCtrl(ch, dtr, rts);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001302 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 return 0;
1304}
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306static void moxa_set_termios(struct tty_struct *tty,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001307 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001309 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 if (ch == NULL)
1312 return;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001313 moxa_set_tty_param(tty, old_termios);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001314 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
Alan Cox9de6a512008-07-16 21:56:02 +01001315 wake_up_interruptible(&ch->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
1318static void moxa_stop(struct tty_struct *tty)
1319{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001320 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 if (ch == NULL)
1323 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001324 MoxaPortTxDisable(ch);
Alan Coxa808ac02009-11-30 13:18:02 +00001325 set_bit(TXSTOPPED, &ch->statusflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
1327
1328
1329static void moxa_start(struct tty_struct *tty)
1330{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001331 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 if (ch == NULL)
1334 return;
1335
Dan Carpenter58112dfb2012-03-07 13:05:00 +03001336 if (!test_bit(TXSTOPPED, &ch->statusflags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return;
1338
Jiri Slabyb4173f42008-04-30 00:53:40 -07001339 MoxaPortTxEnable(ch);
Alan Coxa808ac02009-11-30 13:18:02 +00001340 clear_bit(TXSTOPPED, &ch->statusflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
1342
1343static void moxa_hangup(struct tty_struct *tty)
1344{
Alan Coxa808ac02009-11-30 13:18:02 +00001345 struct moxa_port *ch = tty->driver_data;
Alan Coxf1761782009-11-30 13:17:51 +00001346 tty_port_hangup(&ch->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347}
1348
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001349static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1350{
Alan Cox8482bcd2009-11-30 13:18:13 +00001351 unsigned long flags;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001352 dcd = !!dcd;
1353
Alan Cox8482bcd2009-11-30 13:18:13 +00001354 spin_lock_irqsave(&p->port.lock, flags);
Alan Coxd450b5a2008-10-13 10:39:58 +01001355 if (dcd != p->DCDState) {
Alan Cox8482bcd2009-11-30 13:18:13 +00001356 p->DCDState = dcd;
1357 spin_unlock_irqrestore(&p->port.lock, flags);
Jiri Slabyaa27a092013-03-07 13:12:30 +01001358 if (!dcd)
1359 tty_port_tty_hangup(&p->port, true);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001360 }
Alan Cox8482bcd2009-11-30 13:18:13 +00001361 else
1362 spin_unlock_irqrestore(&p->port.lock, flags);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001363}
1364
1365static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1366 u16 __iomem *ip)
1367{
Alan Coxd450b5a2008-10-13 10:39:58 +01001368 struct tty_struct *tty = tty_port_tty_get(&p->port);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001369 void __iomem *ofsAddr;
Peter Hurleyd41861c2016-04-09 17:53:25 -07001370 unsigned int inited = tty_port_initialized(&p->port);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001371 u16 intr;
1372
1373 if (tty) {
Alan Coxa808ac02009-11-30 13:18:02 +00001374 if (test_bit(EMPTYWAIT, &p->statusflags) &&
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001375 MoxaPortTxQueue(p) == 0) {
Alan Coxa808ac02009-11-30 13:18:02 +00001376 clear_bit(EMPTYWAIT, &p->statusflags);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001377 tty_wakeup(tty);
1378 }
Alan Coxa808ac02009-11-30 13:18:02 +00001379 if (test_bit(LOWWAIT, &p->statusflags) && !tty->stopped &&
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001380 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
Alan Coxa808ac02009-11-30 13:18:02 +00001381 clear_bit(LOWWAIT, &p->statusflags);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001382 tty_wakeup(tty);
1383 }
1384
Peter Hurley97ef38b2016-04-09 17:11:36 -07001385 if (inited && !tty_throttled(tty) &&
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001386 MoxaPortRxQueue(p) > 0) { /* RX */
1387 MoxaPortReadData(p);
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001388 tty_schedule_flip(&p->port);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001389 }
1390 } else {
Alan Coxa808ac02009-11-30 13:18:02 +00001391 clear_bit(EMPTYWAIT, &p->statusflags);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001392 MoxaPortFlushData(p, 0); /* flush RX */
1393 }
1394
1395 if (!handle) /* nothing else to do */
Jiri Slaby0e0fd7d2009-04-06 17:34:04 +01001396 goto put;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001397
1398 intr = readw(ip); /* port irq status */
1399 if (intr == 0)
Jiri Slaby0e0fd7d2009-04-06 17:34:04 +01001400 goto put;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001401
1402 writew(0, ip); /* ACK port */
1403 ofsAddr = p->tableAddr;
1404 if (intr & IntrTx) /* disable tx intr */
1405 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1406 ofsAddr + HostStat);
1407
1408 if (!inited)
Jiri Slaby0e0fd7d2009-04-06 17:34:04 +01001409 goto put;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001410
1411 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
Jiri Slaby92a19f92013-01-03 15:53:03 +01001412 tty_insert_flip_char(&p->port, 0, TTY_BREAK);
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001413 tty_schedule_flip(&p->port);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001414 }
1415
1416 if (intr & IntrLine)
1417 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
Jiri Slaby0e0fd7d2009-04-06 17:34:04 +01001418put:
1419 tty_kref_put(tty);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001420
1421 return 0;
1422}
1423
Kees Cook24ed9602017-08-28 11:28:21 -07001424static void moxa_poll(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001426 struct moxa_board_conf *brd;
1427 u16 __iomem *ip;
Jiri Slaby2a541342008-04-30 00:53:45 -07001428 unsigned int card, port, served = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001430 spin_lock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 for (card = 0; card < MAX_BOARDS; card++) {
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001432 brd = &moxa_boards[card];
1433 if (!brd->ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 continue;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001435
Jiri Slaby2a541342008-04-30 00:53:45 -07001436 served++;
1437
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001438 ip = NULL;
1439 if (readb(brd->intPend) == 0xff)
1440 ip = brd->intTable + readb(brd->intNdx);
1441
1442 for (port = 0; port < brd->numPorts; port++)
1443 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1444
1445 if (ip)
1446 writeb(0, brd->intPend); /* ACK */
1447
1448 if (moxaLowWaterChk) {
1449 struct moxa_port *p = brd->ports;
1450 for (port = 0; port < brd->numPorts; port++, p++)
1451 if (p->lowChkFlag) {
1452 p->lowChkFlag = 0;
1453 moxa_low_water_check(p->tableAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
1456 }
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001457 moxaLowWaterChk = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Jiri Slaby2a541342008-04-30 00:53:45 -07001459 if (served)
1460 mod_timer(&moxaTimer, jiffies + HZ / 50);
1461 spin_unlock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
1464/******************************************************************************/
1465
Alan Coxdb1acaa2008-02-08 04:18:43 -08001466static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Alan Coxadc8d742012-07-14 15:31:47 +01001468 register struct ktermios *ts = &tty->termios;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001469 struct moxa_port *ch = tty->driver_data;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001470 int rts, cts, txflow, rxflow, xany, baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 rts = cts = txflow = rxflow = xany = 0;
1473 if (ts->c_cflag & CRTSCTS)
1474 rts = cts = 1;
1475 if (ts->c_iflag & IXON)
1476 txflow = 1;
1477 if (ts->c_iflag & IXOFF)
1478 rxflow = 1;
1479 if (ts->c_iflag & IXANY)
1480 xany = 1;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001481
Jiri Slabyb4173f42008-04-30 00:53:40 -07001482 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1483 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
Alan Coxdb1acaa2008-02-08 04:18:43 -08001484 if (baud == -1)
1485 baud = tty_termios_baud_rate(old_termios);
1486 /* Not put the baud rate into the termios data */
1487 tty_encode_baud_rate(tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490/*****************************************************************************
1491 * Driver level functions: *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Jiri Slabyb4173f42008-04-30 00:53:40 -07001494static void MoxaPortFlushData(struct moxa_port *port, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
1496 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001497 if (mode < 0 || mode > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001499 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 moxafunc(ofsAddr, FC_FlushQueue, mode);
1501 if (mode != 1) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001502 port->lowChkFlag = 0;
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001503 moxa_low_water_check(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
1505}
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507/*
1508 * Moxa Port Number Description:
1509 *
1510 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1511 * the port number using in MOXA driver functions will be 0 to 31 for
1512 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1513 * to 127 for fourth. For example, if you setup three MOXA boards,
1514 * first board is C218, second board is C320-16 and third board is
1515 * C320-32. The port number of first board (C218 - 8 ports) is from
1516 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1517 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1518 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1519 * 127 will be invalid.
1520 *
1521 *
1522 * Moxa Functions Description:
1523 *
1524 * Function 1: Driver initialization routine, this routine must be
1525 * called when initialized driver.
1526 * Syntax:
1527 * void MoxaDriverInit();
1528 *
1529 *
1530 * Function 2: Moxa driver private IOCTL command processing.
1531 * Syntax:
1532 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1533 *
1534 * unsigned int cmd : IOCTL command
1535 * unsigned long arg : IOCTL argument
1536 * int port : port number (0 - 127)
1537 *
1538 * return: 0 (OK)
1539 * -EINVAL
1540 * -ENOIOCTLCMD
1541 *
1542 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 * Function 6: Enable this port to start Tx/Rx data.
1544 * Syntax:
1545 * void MoxaPortEnable(int port);
1546 * int port : port number (0 - 127)
1547 *
1548 *
1549 * Function 7: Disable this port
1550 * Syntax:
1551 * void MoxaPortDisable(int port);
1552 * int port : port number (0 - 127)
1553 *
1554 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 * Function 10: Setting baud rate of this port.
1556 * Syntax:
Jiri Slaby08d01c72008-04-30 00:53:47 -07001557 * speed_t MoxaPortSetBaud(int port, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 * int port : port number (0 - 127)
1559 * long baud : baud rate (50 - 115200)
1560 *
1561 * return: 0 : this port is invalid or baud < 50
1562 * 50 - 115200 : the real baud rate set to the port, if
1563 * the argument baud is large than maximun
1564 * available baud rate, the real setting
1565 * baud rate will be the maximun baud rate.
1566 *
1567 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 * Function 12: Configure the port.
1569 * Syntax:
Alan Cox606d0992006-12-08 02:38:45 -08001570 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 * int port : port number (0 - 127)
Alan Cox606d0992006-12-08 02:38:45 -08001572 * struct ktermios * termio : termio structure pointer
Alan Coxc7bce302006-09-30 23:27:24 -07001573 * speed_t baud : baud rate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 *
1575 * return: -1 : this port is invalid or termio == NULL
1576 * 0 : setting O.K.
1577 *
1578 *
1579 * Function 13: Get the DTR/RTS state of this port.
1580 * Syntax:
1581 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1582 * int port : port number (0 - 127)
1583 * int * dtrState : pointer to INT to receive the current DTR
1584 * state. (if NULL, this function will not
1585 * write to this address)
1586 * int * rtsState : pointer to INT to receive the current RTS
1587 * state. (if NULL, this function will not
1588 * write to this address)
1589 *
1590 * return: -1 : this port is invalid
1591 * 0 : O.K.
1592 *
1593 *
1594 * Function 14: Setting the DTR/RTS output state of this port.
1595 * Syntax:
1596 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1597 * int port : port number (0 - 127)
1598 * int dtrState : DTR output state (0: off, 1: on)
1599 * int rtsState : RTS output state (0: off, 1: on)
1600 *
1601 *
1602 * Function 15: Setting the flow control of this port.
1603 * Syntax:
1604 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1605 * int txFlow,int xany);
1606 * int port : port number (0 - 127)
1607 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1608 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1609 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1610 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1611 * int xany : S/W XANY flow control (0: no, 1: yes)
1612 *
1613 *
1614 * Function 16: Get ths line status of this port
1615 * Syntax:
1616 * int MoxaPortLineStatus(int port);
1617 * int port : port number (0 - 127)
1618 *
1619 * return: Bit 0 - CTS state (0: off, 1: on)
1620 * Bit 1 - DSR state (0: off, 1: on)
1621 * Bit 2 - DCD state (0: off, 1: on)
1622 *
1623 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 * Function 19: Flush the Rx/Tx buffer data of this port.
1625 * Syntax:
1626 * void MoxaPortFlushData(int port, int mode);
1627 * int port : port number (0 - 127)
1628 * int mode
1629 * 0 : flush the Rx buffer
1630 * 1 : flush the Tx buffer
1631 * 2 : flush the Rx and Tx buffer
1632 *
1633 *
1634 * Function 20: Write data.
1635 * Syntax:
1636 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1637 * int port : port number (0 - 127)
1638 * unsigned char * buffer : pointer to write data buffer.
1639 * int length : write data length
1640 *
1641 * return: 0 - length : real write data length
1642 *
1643 *
1644 * Function 21: Read data.
1645 * Syntax:
Alan Cox33f0f882006-01-09 20:54:13 -08001646 * int MoxaPortReadData(int port, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 * int port : port number (0 - 127)
Alan Cox33f0f882006-01-09 20:54:13 -08001648 * struct tty_struct *tty : tty for data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 *
1650 * return: 0 - length : real read data length
1651 *
1652 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 * Function 24: Get the Tx buffer current queued data bytes
1654 * Syntax:
1655 * int MoxaPortTxQueue(int port);
1656 * int port : port number (0 - 127)
1657 *
1658 * return: .. : Tx buffer current queued data bytes
1659 *
1660 *
1661 * Function 25: Get the Tx buffer current free space
1662 * Syntax:
1663 * int MoxaPortTxFree(int port);
1664 * int port : port number (0 - 127)
1665 *
1666 * return: .. : Tx buffer current free space
1667 *
1668 *
1669 * Function 26: Get the Rx buffer current queued data bytes
1670 * Syntax:
1671 * int MoxaPortRxQueue(int port);
1672 * int port : port number (0 - 127)
1673 *
1674 * return: .. : Rx buffer current queued data bytes
1675 *
1676 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 * Function 28: Disable port data transmission.
1678 * Syntax:
1679 * void MoxaPortTxDisable(int port);
1680 * int port : port number (0 - 127)
1681 *
1682 *
1683 * Function 29: Enable port data transmission.
1684 * Syntax:
1685 * void MoxaPortTxEnable(int port);
1686 * int port : port number (0 - 127)
1687 *
1688 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 * Function 31: Get the received BREAK signal count and reset it.
1690 * Syntax:
1691 * int MoxaPortResetBrkCnt(int port);
1692 * int port : port number (0 - 127)
1693 *
1694 * return: 0 - .. : BREAK signal count
1695 *
1696 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Jiri Slabyb4173f42008-04-30 00:53:40 -07001699static void MoxaPortEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
1701 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001702 u16 lowwater = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Jiri Slabyb4173f42008-04-30 00:53:40 -07001704 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 writew(lowwater, ofsAddr + Low_water);
Jiri Slaby08d01c72008-04-30 00:53:47 -07001706 if (MOXA_IS_320(port->board))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001708 else
1709 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1710 ofsAddr + HostStat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1713 moxafunc(ofsAddr, FC_FlushQueue, 2);
1714
1715 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1716 MoxaPortLineStatus(port);
1717}
1718
Jiri Slabyb4173f42008-04-30 00:53:40 -07001719static void MoxaPortDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001721 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1724 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1725 writew(0, ofsAddr + HostStat);
1726 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1727}
1728
Jiri Slaby08d01c72008-04-30 00:53:47 -07001729static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
Jiri Slaby08d01c72008-04-30 00:53:47 -07001731 void __iomem *ofsAddr = port->tableAddr;
1732 unsigned int clock, val;
1733 speed_t max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Jiri Slaby08d01c72008-04-30 00:53:47 -07001735 max = MOXA_IS_320(port->board) ? 460800 : 921600;
1736 if (baud < 50)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001737 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 if (baud > max)
1739 baud = max;
Jiri Slaby08d01c72008-04-30 00:53:47 -07001740 clock = 921600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 val = clock / baud;
1742 moxafunc(ofsAddr, FC_SetBaud, val);
1743 baud = clock / val;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001744 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
1746
Jiri Slabyb4173f42008-04-30 00:53:40 -07001747static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1748 speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
1750 void __iomem *ofsAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 tcflag_t mode = 0;
1752
Jiri Slabyb4173f42008-04-30 00:53:40 -07001753 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
1755 mode = termio->c_cflag & CSIZE;
1756 if (mode == CS5)
1757 mode = MX_CS5;
1758 else if (mode == CS6)
1759 mode = MX_CS6;
1760 else if (mode == CS7)
1761 mode = MX_CS7;
1762 else if (mode == CS8)
1763 mode = MX_CS8;
1764
1765 if (termio->c_cflag & CSTOPB) {
1766 if (mode == MX_CS5)
1767 mode |= MX_STOP15;
1768 else
1769 mode |= MX_STOP2;
1770 } else
1771 mode |= MX_STOP1;
1772
1773 if (termio->c_cflag & PARENB) {
Lars Kanisd8dcbdd2017-11-28 11:55:17 +01001774 if (termio->c_cflag & PARODD) {
1775 if (termio->c_cflag & CMSPAR)
1776 mode |= MX_PARMARK;
1777 else
1778 mode |= MX_PARODD;
1779 } else {
1780 if (termio->c_cflag & CMSPAR)
1781 mode |= MX_PARSPACE;
1782 else
1783 mode |= MX_PAREVEN;
1784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 } else
1786 mode |= MX_PARNONE;
1787
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001788 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Jiri Slaby08d01c72008-04-30 00:53:47 -07001790 if (MOXA_IS_320(port->board) && baud >= 921600)
1791 return -1;
1792
Alan Coxdb1acaa2008-02-08 04:18:43 -08001793 baud = MoxaPortSetBaud(port, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
1795 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
Alan Coxf5c5a362009-11-30 13:17:57 +00001796 spin_lock_irq(&moxafunc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1798 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1799 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001800 moxa_wait_finish(ofsAddr);
Alan Coxa808ac02009-11-30 13:18:02 +00001801 spin_unlock_irq(&moxafunc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 }
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001804 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
Jiri Slabyb4173f42008-04-30 00:53:40 -07001807static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1808 int *rtsState)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001810 if (dtrState)
1811 *dtrState = !!(port->lineCtrl & DTR_ON);
1812 if (rtsState)
1813 *rtsState = !!(port->lineCtrl & RTS_ON);
1814
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001815 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
Jiri Slabyb4173f42008-04-30 00:53:40 -07001818static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001820 u8 mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (dtr)
1823 mode |= DTR_ON;
1824 if (rts)
1825 mode |= RTS_ON;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001826 port->lineCtrl = mode;
1827 moxafunc(port->tableAddr, FC_LineControl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
1829
Jiri Slabyb4173f42008-04-30 00:53:40 -07001830static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1831 int txflow, int rxflow, int txany)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001833 int mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if (rts)
1836 mode |= RTS_FlowCtl;
1837 if (cts)
1838 mode |= CTS_FlowCtl;
1839 if (txflow)
1840 mode |= Tx_FlowCtl;
1841 if (rxflow)
1842 mode |= Rx_FlowCtl;
1843 if (txany)
1844 mode |= IXM_IXANY;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001845 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
Jiri Slabyb4173f42008-04-30 00:53:40 -07001848static int MoxaPortLineStatus(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
1850 void __iomem *ofsAddr;
1851 int val;
1852
Jiri Slabyb4173f42008-04-30 00:53:40 -07001853 ofsAddr = port->tableAddr;
Alan Coxf5c5a362009-11-30 13:17:57 +00001854 if (MOXA_IS_320(port->board))
1855 val = moxafuncret(ofsAddr, FC_LineStatus, 0);
1856 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 val = readw(ofsAddr + FlagStat) >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 val &= 0x0B;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001859 if (val & 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 val |= 4;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001861 moxa_new_dcdstate(port, val & 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 val &= 7;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001863 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864}
1865
Alan Coxd450b5a2008-10-13 10:39:58 +01001866static int MoxaPortWriteData(struct tty_struct *tty,
Jiri Slaby2108eba2008-04-30 00:53:44 -07001867 const unsigned char *buffer, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
Alan Coxd450b5a2008-10-13 10:39:58 +01001869 struct moxa_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001871 unsigned int c, total;
1872 u16 head, tail, tx_mask, spage, epage;
1873 u16 pageno, pageofs, bufhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Jiri Slabyb4173f42008-04-30 00:53:40 -07001875 ofsAddr = port->tableAddr;
1876 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 tx_mask = readw(ofsAddr + TX_mask);
1878 spage = readw(ofsAddr + Page_txb);
1879 epage = readw(ofsAddr + EndPage_txb);
1880 tail = readw(ofsAddr + TXwptr);
1881 head = readw(ofsAddr + TXrptr);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001882 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 if (c > len)
1884 c = len;
Alan Cox9de6a512008-07-16 21:56:02 +01001885 moxaLog.txcnt[port->port.tty->index] += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 total = c;
1887 if (spage == epage) {
1888 bufhead = readw(ofsAddr + Ofs_txb);
1889 writew(spage, baseAddr + Control_reg);
1890 while (c > 0) {
1891 if (head > tail)
1892 len = head - tail - 1;
1893 else
1894 len = tx_mask + 1 - tail;
1895 len = (c > len) ? len : c;
1896 ofs = baseAddr + DynPage_addr + bufhead + tail;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001897 memcpy_toio(ofs, buffer, len);
1898 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 tail = (tail + len) & tx_mask;
1900 c -= len;
1901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 pageno = spage + (tail >> 13);
1904 pageofs = tail & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001905 while (c > 0) {
1906 len = Page_size - pageofs;
1907 if (len > c)
1908 len = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 writeb(pageno, baseAddr + Control_reg);
1910 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001911 memcpy_toio(ofs, buffer, len);
1912 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 if (++pageno == epage)
1914 pageno = spage;
1915 pageofs = 0;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001916 c -= len;
1917 }
1918 tail = (tail + total) & tx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07001920 writew(tail, ofsAddr + TXwptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 writeb(1, ofsAddr + CD180TXirq); /* start to send */
Jiri Slaby2108eba2008-04-30 00:53:44 -07001922 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
1924
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001925static int MoxaPortReadData(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
Alan Cox9de6a512008-07-16 21:56:02 +01001927 struct tty_struct *tty = port->port.tty;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001928 unsigned char *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001930 unsigned int count, len, total;
1931 u16 tail, rx_mask, spage, epage;
1932 u16 pageno, pageofs, bufhead, head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Jiri Slabyb4173f42008-04-30 00:53:40 -07001934 ofsAddr = port->tableAddr;
1935 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 head = readw(ofsAddr + RXrptr);
1937 tail = readw(ofsAddr + RXwptr);
1938 rx_mask = readw(ofsAddr + RX_mask);
1939 spage = readw(ofsAddr + Page_rxb);
1940 epage = readw(ofsAddr + EndPage_rxb);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001941 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (count == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08001943 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Alan Cox33f0f882006-01-09 20:54:13 -08001945 total = count;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001946 moxaLog.rxcnt[tty->index] += total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 if (spage == epage) {
1948 bufhead = readw(ofsAddr + Ofs_rxb);
1949 writew(spage, baseAddr + Control_reg);
1950 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 ofs = baseAddr + DynPage_addr + bufhead + head;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001952 len = (tail >= head) ? (tail - head) :
1953 (rx_mask + 1 - head);
Jiri Slaby2f693352013-01-03 15:53:02 +01001954 len = tty_prepare_flip_string(&port->port, &dst,
Jiri Slaby2108eba2008-04-30 00:53:44 -07001955 min(len, count));
1956 memcpy_fromio(dst, ofs, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 head = (head + len) & rx_mask;
1958 count -= len;
1959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 pageno = spage + (head >> 13);
1962 pageofs = head & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001963 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 writew(pageno, baseAddr + Control_reg);
1965 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2f693352013-01-03 15:53:02 +01001966 len = tty_prepare_flip_string(&port->port, &dst,
Jiri Slaby2108eba2008-04-30 00:53:44 -07001967 min(Page_size - pageofs, count));
1968 memcpy_fromio(dst, ofs, len);
1969
1970 count -= len;
1971 pageofs = (pageofs + len) & Page_mask;
1972 if (pageofs == 0 && ++pageno == epage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 pageno = spage;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001974 }
1975 head = (head + total) & rx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07001977 writew(head, ofsAddr + RXrptr);
1978 if (readb(ofsAddr + FlagStat) & Xoff_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 moxaLowWaterChk = 1;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001980 port->lowChkFlag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07001982 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
1984
1985
Jiri Slabyb4173f42008-04-30 00:53:40 -07001986static int MoxaPortTxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001988 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001989 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 rptr = readw(ofsAddr + TXrptr);
1992 wptr = readw(ofsAddr + TXwptr);
1993 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001994 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995}
1996
Jiri Slabyb4173f42008-04-30 00:53:40 -07001997static int MoxaPortTxFree(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001999 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002000 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 rptr = readw(ofsAddr + TXrptr);
2003 wptr = readw(ofsAddr + TXwptr);
2004 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002005 return mask - ((wptr - rptr) & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006}
2007
Jiri Slabyb4173f42008-04-30 00:53:40 -07002008static int MoxaPortRxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002010 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002011 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 rptr = readw(ofsAddr + RXrptr);
2014 wptr = readw(ofsAddr + RXwptr);
2015 mask = readw(ofsAddr + RX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002016 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
Jiri Slabyb4173f42008-04-30 00:53:40 -07002019static void MoxaPortTxDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002021 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022}
2023
Jiri Slabyb4173f42008-04-30 00:53:40 -07002024static void MoxaPortTxEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002026 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027}
2028
Al Viro1c729ab2018-09-11 22:01:00 -04002029static int moxa_get_serial_info(struct tty_struct *tty,
2030 struct serial_struct *ss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
Al Viro1c729ab2018-09-11 22:01:00 -04002032 struct moxa_port *info = tty->driver_data;
2033
2034 if (tty->index == MAX_PORTS)
2035 return -EINVAL;
2036 if (!info)
2037 return -ENODEV;
2038 mutex_lock(&info->port.mutex);
2039 ss->type = info->type,
2040 ss->line = info->port.tty->index,
2041 ss->flags = info->port.flags,
2042 ss->baud_base = 921600,
2043 ss->close_delay = info->port.close_delay;
2044 mutex_unlock(&info->port.mutex);
2045 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046}
2047
2048
Al Viro1c729ab2018-09-11 22:01:00 -04002049static int moxa_set_serial_info(struct tty_struct *tty,
2050 struct serial_struct *ss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051{
Al Viro1c729ab2018-09-11 22:01:00 -04002052 struct moxa_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Al Viro1c729ab2018-09-11 22:01:00 -04002054 if (tty->index == MAX_PORTS)
2055 return -EINVAL;
2056 if (!info)
2057 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Al Viro1c729ab2018-09-11 22:01:00 -04002059 if (ss->irq != 0 || ss->port != 0 ||
2060 ss->custom_divisor != 0 ||
2061 ss->baud_base != 921600)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002062 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Al Viro1c729ab2018-09-11 22:01:00 -04002064 mutex_lock(&info->port.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if (!capable(CAP_SYS_ADMIN)) {
Al Viro1c729ab2018-09-11 22:01:00 -04002066 if (((ss->flags & ~ASYNC_USR_MASK) !=
2067 (info->port.flags & ~ASYNC_USR_MASK))) {
2068 mutex_unlock(&info->port.mutex);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002069 return -EPERM;
Al Viro1c729ab2018-09-11 22:01:00 -04002070 }
2071 }
2072 info->port.close_delay = ss->close_delay * HZ / 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Al Viro1c729ab2018-09-11 22:01:00 -04002074 MoxaSetFifo(info, ss->type == PORT_16550A);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Al Viro1c729ab2018-09-11 22:01:00 -04002076 info->type = ss->type;
2077 mutex_unlock(&info->port.mutex);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002078 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079}
2080
2081
2082
2083/*****************************************************************************
2084 * Static local functions: *
2085 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Jiri Slabyb4173f42008-04-30 00:53:40 -07002087static void MoxaSetFifo(struct moxa_port *port, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002089 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091 if (!enable) {
2092 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2093 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2094 } else {
2095 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2096 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2097 }
2098}