blob: 63ee3bbc1ce4169654624980c3281e28fe84ae7c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*****************************************************************************/
2/*
3 * moxa.c -- MOXA Intellio family multiport serial driver.
4 *
Jiri Slabyb9705b62008-04-30 00:53:48 -07005 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com).
6 * Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This code is loosely based on the Linux serial driver, written by
9 * Linus Torvalds, Theodore T'so and others.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
17/*
18 * MOXA Intellio Series Driver
19 * for : LINUX
20 * date : 1999/1/7
21 * version : 5.1
22 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <linux/types.h>
26#include <linux/mm.h>
27#include <linux/ioport.h>
28#include <linux/errno.h>
Jiri Slaby03718232008-04-30 00:53:39 -070029#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/signal.h>
31#include <linux/sched.h>
32#include <linux/timer.h>
33#include <linux/interrupt.h>
34#include <linux/tty.h>
35#include <linux/tty_flip.h>
36#include <linux/major.h>
37#include <linux/string.h>
38#include <linux/fcntl.h>
39#include <linux/ptrace.h>
40#include <linux/serial.h>
41#include <linux/tty_driver.h>
42#include <linux/delay.h>
43#include <linux/pci.h>
44#include <linux/init.h>
45#include <linux/bitops.h>
46
47#include <asm/system.h>
48#include <asm/io.h>
49#include <asm/uaccess.h>
50
Jiri Slaby03718232008-04-30 00:53:39 -070051#include "moxa.h"
52
Jiri Slabyb9705b62008-04-30 00:53:48 -070053#define MOXA_VERSION "6.0k"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Jiri Slaby03718232008-04-30 00:53:39 -070055#define MOXA_FW_HDRLEN 32
56
Jiri Slaby11324ed2007-02-10 01:45:31 -080057#define MOXAMAJOR 172
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Jiri Slaby11324ed2007-02-10 01:45:31 -080059#define MAX_BOARDS 4 /* Don't change this value */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
Jiri Slaby11324ed2007-02-10 01:45:31 -080061#define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Jiri Slaby08d01c792008-04-30 00:53:47 -070063#define MOXA_IS_320(brd) ((brd)->boardType == MOXA_BOARD_C320_ISA || \
64 (brd)->boardType == MOXA_BOARD_C320_PCI)
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/*
67 * Define the Moxa PCI vendor and device IDs.
68 */
Jiri Slaby11324ed2007-02-10 01:45:31 -080069#define MOXA_BUS_TYPE_ISA 0
70#define MOXA_BUS_TYPE_PCI 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072enum {
73 MOXA_BOARD_C218_PCI = 1,
74 MOXA_BOARD_C218_ISA,
75 MOXA_BOARD_C320_PCI,
76 MOXA_BOARD_C320_ISA,
77 MOXA_BOARD_CP204J,
78};
79
80static char *moxa_brdname[] =
81{
82 "C218 Turbo PCI series",
83 "C218 Turbo ISA series",
84 "C320 Turbo PCI series",
85 "C320 Turbo ISA series",
86 "CP-204J series",
87};
88
89#ifdef CONFIG_PCI
90static struct pci_device_id moxa_pcibrds[] = {
Jiri Slaby5ebb4072007-02-10 01:45:30 -080091 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
92 .driver_data = MOXA_BOARD_C218_PCI },
93 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
94 .driver_data = MOXA_BOARD_C320_PCI },
95 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
96 .driver_data = MOXA_BOARD_CP204J },
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 { 0 }
98};
99MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
100#endif /* CONFIG_PCI */
101
Jiri Slaby03718232008-04-30 00:53:39 -0700102struct moxa_port;
103
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800104static struct moxa_board_conf {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 int boardType;
106 int numPorts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 int busType;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800108
Jiri Slaby810ab092008-04-30 00:53:41 -0700109 unsigned int ready;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800110
Jiri Slaby03718232008-04-30 00:53:39 -0700111 struct moxa_port *ports;
112
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800113 void __iomem *basemem;
114 void __iomem *intNdx;
115 void __iomem *intPend;
116 void __iomem *intTable;
117} moxa_boards[MAX_BOARDS];
118
119struct mxser_mstatus {
120 tcflag_t cflag;
121 int cts;
122 int dsr;
123 int ri;
124 int dcd;
Jiri Slaby9dff89c2007-02-10 01:45:30 -0800125};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800127struct moxaq_str {
128 int inq;
129 int outq;
130};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800132struct moxa_port {
Alan Cox9de6a512008-07-16 21:56:02 +0100133 struct tty_port port;
Jiri Slabyb4173f42008-04-30 00:53:40 -0700134 struct moxa_board_conf *board;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700135 void __iomem *tableAddr;
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 int cflag;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700139 unsigned long statusflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Alan Cox8482bcd2009-11-30 13:18:13 +0000141 u8 DCDState; /* Protected by the port lock */
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700142 u8 lineCtrl;
143 u8 lowChkFlag;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800144};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Jiri Slaby74d7d972008-04-30 00:53:43 -0700146struct mon_str {
147 int tick;
148 int rxcnt[MAX_PORTS];
149 int txcnt[MAX_PORTS];
150};
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* statusflags */
Alan Coxa808ac02009-11-30 13:18:02 +0000153#define TXSTOPPED 1
154#define LOWWAIT 2
155#define EMPTYWAIT 3
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#define SERIAL_DO_RESTART
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#define WAKEUP_CHARS 256
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static int ttymajor = MOXAMAJOR;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700162static struct mon_str moxaLog;
163static unsigned int moxaFuncTout = HZ / 2;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700164static unsigned int moxaLowWaterChk;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700165static DEFINE_MUTEX(moxa_openlock);
Alan Coxe8c62102009-11-30 13:18:24 +0000166static DEFINE_SPINLOCK(moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/* Variables for insmod */
168#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700169static unsigned long baseaddr[MAX_BOARDS];
170static unsigned int type[MAX_BOARDS];
171static unsigned int numports[MAX_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172#endif
173
174MODULE_AUTHOR("William Chen");
175MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
176MODULE_LICENSE("GPL");
177#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700178module_param_array(type, uint, NULL, 0);
179MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
180module_param_array(baseaddr, ulong, NULL, 0);
181MODULE_PARM_DESC(baseaddr, "base address");
182module_param_array(numports, uint, NULL, 0);
183MODULE_PARM_DESC(numports, "numports (ignored for C218)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184#endif
185module_param(ttymajor, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/*
188 * static functions:
189 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190static int moxa_open(struct tty_struct *, struct file *);
191static void moxa_close(struct tty_struct *, struct file *);
192static int moxa_write(struct tty_struct *, const unsigned char *, int);
193static int moxa_write_room(struct tty_struct *);
194static void moxa_flush_buffer(struct tty_struct *);
195static int moxa_chars_in_buffer(struct tty_struct *);
Alan Cox606d0992006-12-08 02:38:45 -0800196static void moxa_set_termios(struct tty_struct *, struct ktermios *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197static void moxa_stop(struct tty_struct *);
198static void moxa_start(struct tty_struct *);
199static void moxa_hangup(struct tty_struct *);
200static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
201static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
202 unsigned int set, unsigned int clear);
203static void moxa_poll(unsigned long);
Alan Coxdb1acaa2008-02-08 04:18:43 -0800204static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
Alan Coxf1761782009-11-30 13:17:51 +0000205static void moxa_shutdown(struct tty_port *);
Alan Cox31f35932009-01-02 13:45:05 +0000206static int moxa_carrier_raised(struct tty_port *);
Alan Coxf1761782009-11-30 13:17:51 +0000207static void moxa_dtr_rts(struct tty_port *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208/*
209 * moxa board interface functions:
210 */
Jiri Slabyb4173f42008-04-30 00:53:40 -0700211static void MoxaPortEnable(struct moxa_port *);
212static void MoxaPortDisable(struct moxa_port *);
213static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
214static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
215static void MoxaPortLineCtrl(struct moxa_port *, int, int);
216static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
217static int MoxaPortLineStatus(struct moxa_port *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700218static void MoxaPortFlushData(struct moxa_port *, int);
Alan Coxd450b5a2008-10-13 10:39:58 +0100219static int MoxaPortWriteData(struct tty_struct *, const unsigned char *, int);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700220static int MoxaPortReadData(struct moxa_port *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700221static int MoxaPortTxQueue(struct moxa_port *);
222static int MoxaPortRxQueue(struct moxa_port *);
223static int MoxaPortTxFree(struct moxa_port *);
224static void MoxaPortTxDisable(struct moxa_port *);
225static void MoxaPortTxEnable(struct moxa_port *);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800226static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
227static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700228static void MoxaSetFifo(struct moxa_port *port, int enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Jiri Slaby74d7d972008-04-30 00:53:43 -0700230/*
231 * I/O functions
232 */
233
Alan Coxa808ac02009-11-30 13:18:02 +0000234static DEFINE_SPINLOCK(moxafunc_lock);
235
Jiri Slaby74d7d972008-04-30 00:53:43 -0700236static void moxa_wait_finish(void __iomem *ofsAddr)
237{
238 unsigned long end = jiffies + moxaFuncTout;
239
240 while (readw(ofsAddr + FuncCode) != 0)
241 if (time_after(jiffies, end))
242 return;
243 if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
244 printk(KERN_WARNING "moxa function expired\n");
245}
246
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700247static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
Jiri Slaby74d7d972008-04-30 00:53:43 -0700248{
Alan Coxf5c5a362009-11-30 13:17:57 +0000249 unsigned long flags;
250 spin_lock_irqsave(&moxafunc_lock, flags);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700251 writew(arg, ofsAddr + FuncArg);
252 writew(cmd, ofsAddr + FuncCode);
253 moxa_wait_finish(ofsAddr);
Alan Coxf5c5a362009-11-30 13:17:57 +0000254 spin_unlock_irqrestore(&moxafunc_lock, flags);
255}
256
257static int moxafuncret(void __iomem *ofsAddr, u16 cmd, u16 arg)
258{
259 unsigned long flags;
260 u16 ret;
261 spin_lock_irqsave(&moxafunc_lock, flags);
262 writew(arg, ofsAddr + FuncArg);
263 writew(cmd, ofsAddr + FuncCode);
264 moxa_wait_finish(ofsAddr);
265 ret = readw(ofsAddr + FuncArg);
266 spin_unlock_irqrestore(&moxafunc_lock, flags);
267 return ret;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700268}
269
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700270static void moxa_low_water_check(void __iomem *ofsAddr)
271{
272 u16 rptr, wptr, mask, len;
273
274 if (readb(ofsAddr + FlagStat) & Xoff_state) {
275 rptr = readw(ofsAddr + RXrptr);
276 wptr = readw(ofsAddr + RXwptr);
277 mask = readw(ofsAddr + RX_mask);
278 len = (wptr - rptr) & mask;
279 if (len <= Low_water)
280 moxafunc(ofsAddr, FC_SendXon, 0);
281 }
282}
283
Jiri Slaby74d7d972008-04-30 00:53:43 -0700284/*
285 * TTY operations
286 */
287
288static int moxa_ioctl(struct tty_struct *tty, struct file *file,
289 unsigned int cmd, unsigned long arg)
290{
291 struct moxa_port *ch = tty->driver_data;
292 void __user *argp = (void __user *)arg;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700293 int status, ret = 0;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700294
295 if (tty->index == MAX_PORTS) {
296 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
297 cmd != MOXA_GETMSTATUS)
298 return -EINVAL;
299 } else if (!ch)
300 return -ENODEV;
301
302 switch (cmd) {
303 case MOXA_GETDATACOUNT:
304 moxaLog.tick = jiffies;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700305 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
306 ret = -EFAULT;
307 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700308 case MOXA_FLUSH_QUEUE:
309 MoxaPortFlushData(ch, arg);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700310 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700311 case MOXA_GET_IOQUEUE: {
312 struct moxaq_str __user *argm = argp;
313 struct moxaq_str tmp;
314 struct moxa_port *p;
315 unsigned int i, j;
316
317 for (i = 0; i < MAX_BOARDS; i++) {
318 p = moxa_boards[i].ports;
319 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
320 memset(&tmp, 0, sizeof(tmp));
Alan Coxe8c62102009-11-30 13:18:24 +0000321 spin_lock_bh(&moxa_lock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700322 if (moxa_boards[i].ready) {
323 tmp.inq = MoxaPortRxQueue(p);
324 tmp.outq = MoxaPortTxQueue(p);
325 }
Alan Coxe8c62102009-11-30 13:18:24 +0000326 spin_unlock_bh(&moxa_lock);
327 if (copy_to_user(argm, &tmp, sizeof(tmp)))
Jiri Slaby74d7d972008-04-30 00:53:43 -0700328 return -EFAULT;
329 }
330 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700331 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700332 } case MOXA_GET_OQUEUE:
333 status = MoxaPortTxQueue(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700334 ret = put_user(status, (unsigned long __user *)argp);
335 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700336 case MOXA_GET_IQUEUE:
337 status = MoxaPortRxQueue(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700338 ret = put_user(status, (unsigned long __user *)argp);
339 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700340 case MOXA_GETMSTATUS: {
341 struct mxser_mstatus __user *argm = argp;
342 struct mxser_mstatus tmp;
343 struct moxa_port *p;
344 unsigned int i, j;
345
346 for (i = 0; i < MAX_BOARDS; i++) {
347 p = moxa_boards[i].ports;
348 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
Alan Coxd450b5a2008-10-13 10:39:58 +0100349 struct tty_struct *ttyp;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700350 memset(&tmp, 0, sizeof(tmp));
Alan Coxe8c62102009-11-30 13:18:24 +0000351 spin_lock_bh(&moxa_lock);
352 if (!moxa_boards[i].ready) {
353 spin_unlock_bh(&moxa_lock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700354 goto copy;
Alan Coxe8c62102009-11-30 13:18:24 +0000355 }
Jiri Slaby74d7d972008-04-30 00:53:43 -0700356
357 status = MoxaPortLineStatus(p);
Alan Coxe8c62102009-11-30 13:18:24 +0000358 spin_unlock_bh(&moxa_lock);
359
Jiri Slaby74d7d972008-04-30 00:53:43 -0700360 if (status & 1)
361 tmp.cts = 1;
362 if (status & 2)
363 tmp.dsr = 1;
364 if (status & 4)
365 tmp.dcd = 1;
366
Alan Coxd450b5a2008-10-13 10:39:58 +0100367 ttyp = tty_port_tty_get(&p->port);
368 if (!ttyp || !ttyp->termios)
Jiri Slaby74d7d972008-04-30 00:53:43 -0700369 tmp.cflag = p->cflag;
370 else
Alan Coxd450b5a2008-10-13 10:39:58 +0100371 tmp.cflag = ttyp->termios->c_cflag;
372 tty_kref_put(tty);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700373copy:
Alan Coxe8c62102009-11-30 13:18:24 +0000374 if (copy_to_user(argm, &tmp, sizeof(tmp)))
Jiri Slaby74d7d972008-04-30 00:53:43 -0700375 return -EFAULT;
376 }
377 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700378 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700379 }
380 case TIOCGSERIAL:
Alan Coxa808ac02009-11-30 13:18:02 +0000381 mutex_lock(&ch->port.mutex);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700382 ret = moxa_get_serial_info(ch, argp);
Alan Coxa808ac02009-11-30 13:18:02 +0000383 mutex_unlock(&ch->port.mutex);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700384 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700385 case TIOCSSERIAL:
Alan Coxa808ac02009-11-30 13:18:02 +0000386 mutex_lock(&ch->port.mutex);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700387 ret = moxa_set_serial_info(ch, argp);
Alan Coxa808ac02009-11-30 13:18:02 +0000388 mutex_unlock(&ch->port.mutex);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700389 break;
390 default:
391 ret = -ENOIOCTLCMD;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700392 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700393 return ret;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700394}
395
Alan Cox9e989662008-07-22 11:18:03 +0100396static int moxa_break_ctl(struct tty_struct *tty, int state)
Jiri Slaby74d7d972008-04-30 00:53:43 -0700397{
398 struct moxa_port *port = tty->driver_data;
399
400 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
401 Magic_code);
Alan Cox9e989662008-07-22 11:18:03 +0100402 return 0;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700403}
404
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700405static const struct tty_operations moxa_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 .open = moxa_open,
407 .close = moxa_close,
408 .write = moxa_write,
409 .write_room = moxa_write_room,
410 .flush_buffer = moxa_flush_buffer,
411 .chars_in_buffer = moxa_chars_in_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 .ioctl = moxa_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 .set_termios = moxa_set_termios,
414 .stop = moxa_stop,
415 .start = moxa_start,
416 .hangup = moxa_hangup,
Jiri Slaby74d7d972008-04-30 00:53:43 -0700417 .break_ctl = moxa_break_ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 .tiocmget = moxa_tiocmget,
419 .tiocmset = moxa_tiocmset,
420};
421
Alan Cox31f35932009-01-02 13:45:05 +0000422static const struct tty_port_operations moxa_port_ops = {
423 .carrier_raised = moxa_carrier_raised,
Alan Coxf1761782009-11-30 13:17:51 +0000424 .dtr_rts = moxa_dtr_rts,
425 .shutdown = moxa_shutdown,
Alan Cox31f35932009-01-02 13:45:05 +0000426};
427
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800428static struct tty_driver *moxaDriver;
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800429static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
Alan Cox33f0f882006-01-09 20:54:13 -0800430
Jiri Slaby74d7d972008-04-30 00:53:43 -0700431/*
432 * HW init
433 */
434
Jiri Slaby03718232008-04-30 00:53:39 -0700435static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
436{
437 switch (brd->boardType) {
438 case MOXA_BOARD_C218_ISA:
439 case MOXA_BOARD_C218_PCI:
440 if (model != 1)
441 goto err;
442 break;
443 case MOXA_BOARD_CP204J:
444 if (model != 3)
445 goto err;
446 break;
447 default:
448 if (model != 2)
449 goto err;
450 break;
451 }
452 return 0;
453err:
454 return -EINVAL;
455}
456
457static int moxa_check_fw(const void *ptr)
458{
459 const __le16 *lptr = ptr;
460
461 if (*lptr != cpu_to_le16(0x7980))
462 return -EINVAL;
463
464 return 0;
465}
466
467static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
468 size_t len)
469{
470 void __iomem *baseAddr = brd->basemem;
471 u16 tmp;
472
473 writeb(HW_reset, baseAddr + Control_reg); /* reset */
474 msleep(10);
475 memset_io(baseAddr, 0, 4096);
476 memcpy_toio(baseAddr, buf, len); /* download BIOS */
477 writeb(0, baseAddr + Control_reg); /* restart */
478
479 msleep(2000);
480
481 switch (brd->boardType) {
482 case MOXA_BOARD_C218_ISA:
483 case MOXA_BOARD_C218_PCI:
484 tmp = readw(baseAddr + C218_key);
485 if (tmp != C218_KeyCode)
486 goto err;
487 break;
488 case MOXA_BOARD_CP204J:
489 tmp = readw(baseAddr + C218_key);
490 if (tmp != CP204J_KeyCode)
491 goto err;
492 break;
493 default:
494 tmp = readw(baseAddr + C320_key);
495 if (tmp != C320_KeyCode)
496 goto err;
497 tmp = readw(baseAddr + C320_status);
498 if (tmp != STS_init) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700499 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
Jiri Slaby03718232008-04-30 00:53:39 -0700500 "module not found\n");
501 return -EIO;
502 }
503 break;
504 }
505
506 return 0;
507err:
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700508 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700509 return -EIO;
510}
511
512static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
513 size_t len)
514{
515 void __iomem *baseAddr = brd->basemem;
516
517 if (len < 7168) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700518 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700519 return -EINVAL;
520 }
521
522 writew(len - 7168 - 2, baseAddr + C320bapi_len);
523 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
524 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
525 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
526 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
527
528 return 0;
529}
530
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700531static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
Jiri Slaby03718232008-04-30 00:53:39 -0700532 size_t len)
533{
534 void __iomem *baseAddr = brd->basemem;
Harvey Harrisonb46f69c2008-10-15 22:04:19 -0700535 const __le16 *uptr = ptr;
Jiri Slaby03718232008-04-30 00:53:39 -0700536 size_t wlen, len2, j;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700537 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
Jiri Slaby08d01c792008-04-30 00:53:47 -0700538 unsigned int i, retry;
Jiri Slaby03718232008-04-30 00:53:39 -0700539 u16 usum, keycode;
540
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700541 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
542 C218_KeyCode;
543
544 switch (brd->boardType) {
545 case MOXA_BOARD_CP204J:
546 case MOXA_BOARD_C218_ISA:
547 case MOXA_BOARD_C218_PCI:
548 key = C218_key;
549 loadbuf = C218_LoadBuf;
550 loadlen = C218DLoad_len;
551 checksum = C218check_sum;
552 checksum_ok = C218chksum_ok;
553 break;
554 default:
555 key = C320_key;
556 keycode = C320_KeyCode;
557 loadbuf = C320_LoadBuf;
558 loadlen = C320DLoad_len;
559 checksum = C320check_sum;
560 checksum_ok = C320chksum_ok;
561 break;
562 }
563
Jiri Slaby03718232008-04-30 00:53:39 -0700564 usum = 0;
565 wlen = len >> 1;
566 for (i = 0; i < wlen; i++)
567 usum += le16_to_cpu(uptr[i]);
568 retry = 0;
569 do {
570 wlen = len >> 1;
571 j = 0;
572 while (wlen) {
573 len2 = (wlen > 2048) ? 2048 : wlen;
574 wlen -= len2;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700575 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
Jiri Slaby03718232008-04-30 00:53:39 -0700576 j += len2 << 1;
577
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700578 writew(len2, baseAddr + loadlen);
579 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700580 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700581 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700582 break;
583 msleep(10);
584 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700585 if (readw(baseAddr + key) != keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700586 return -EIO;
587 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700588 writew(0, baseAddr + loadlen);
589 writew(usum, baseAddr + checksum);
590 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700591 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700592 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700593 break;
594 msleep(10);
595 }
596 retry++;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700597 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
598 if (readb(baseAddr + checksum_ok) != 1)
Jiri Slaby03718232008-04-30 00:53:39 -0700599 return -EIO;
600
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700601 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700602 for (i = 0; i < 600; i++) {
603 if (readw(baseAddr + Magic_no) == Magic_code)
604 break;
605 msleep(10);
606 }
607 if (readw(baseAddr + Magic_no) != Magic_code)
608 return -EIO;
609
Jiri Slaby08d01c792008-04-30 00:53:47 -0700610 if (MOXA_IS_320(brd)) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700611 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
612 writew(0x3800, baseAddr + TMS320_PORT1);
613 writew(0x3900, baseAddr + TMS320_PORT2);
614 writew(28499, baseAddr + TMS320_CLOCK);
615 } else {
616 writew(0x3200, baseAddr + TMS320_PORT1);
617 writew(0x3400, baseAddr + TMS320_PORT2);
618 writew(19999, baseAddr + TMS320_CLOCK);
619 }
Jiri Slaby03718232008-04-30 00:53:39 -0700620 }
621 writew(1, baseAddr + Disable_IRQ);
622 writew(0, baseAddr + Magic_no);
623 for (i = 0; i < 500; i++) {
624 if (readw(baseAddr + Magic_no) == Magic_code)
625 break;
626 msleep(10);
627 }
628 if (readw(baseAddr + Magic_no) != Magic_code)
629 return -EIO;
630
Jiri Slaby08d01c792008-04-30 00:53:47 -0700631 if (MOXA_IS_320(brd)) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700632 j = readw(baseAddr + Module_cnt);
633 if (j <= 0)
634 return -EIO;
635 brd->numPorts = j * 8;
636 writew(j, baseAddr + Module_no);
637 writew(0, baseAddr + Magic_no);
638 for (i = 0; i < 600; i++) {
639 if (readw(baseAddr + Magic_no) == Magic_code)
640 break;
641 msleep(10);
642 }
643 if (readw(baseAddr + Magic_no) != Magic_code)
644 return -EIO;
Jiri Slaby03718232008-04-30 00:53:39 -0700645 }
Jiri Slaby03718232008-04-30 00:53:39 -0700646 brd->intNdx = baseAddr + IRQindex;
647 brd->intPend = baseAddr + IRQpending;
648 brd->intTable = baseAddr + IRQtable;
649
650 return 0;
651}
652
653static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
654 size_t len)
655{
656 void __iomem *ofsAddr, *baseAddr = brd->basemem;
657 struct moxa_port *port;
658 int retval, i;
659
660 if (len % 2) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700661 printk(KERN_ERR "MOXA: bios length is not even\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700662 return -EINVAL;
663 }
664
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700665 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
666 if (retval)
667 return retval;
668
Jiri Slaby03718232008-04-30 00:53:39 -0700669 switch (brd->boardType) {
670 case MOXA_BOARD_C218_ISA:
671 case MOXA_BOARD_C218_PCI:
672 case MOXA_BOARD_CP204J:
Jiri Slaby03718232008-04-30 00:53:39 -0700673 port = brd->ports;
674 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700675 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700676 port->DCDState = 0;
677 port->tableAddr = baseAddr + Extern_table +
678 Extern_size * i;
679 ofsAddr = port->tableAddr;
680 writew(C218rx_mask, ofsAddr + RX_mask);
681 writew(C218tx_mask, ofsAddr + TX_mask);
682 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
683 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
684
685 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
686 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
687
688 }
689 break;
690 default:
Jiri Slaby03718232008-04-30 00:53:39 -0700691 port = brd->ports;
692 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700693 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700694 port->DCDState = 0;
695 port->tableAddr = baseAddr + Extern_table +
696 Extern_size * i;
697 ofsAddr = port->tableAddr;
698 switch (brd->numPorts) {
699 case 8:
700 writew(C320p8rx_mask, ofsAddr + RX_mask);
701 writew(C320p8tx_mask, ofsAddr + TX_mask);
702 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
703 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
704 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
705 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
706
707 break;
708 case 16:
709 writew(C320p16rx_mask, ofsAddr + RX_mask);
710 writew(C320p16tx_mask, ofsAddr + TX_mask);
711 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
712 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
713 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
714 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
715 break;
716
717 case 24:
718 writew(C320p24rx_mask, ofsAddr + RX_mask);
719 writew(C320p24tx_mask, ofsAddr + TX_mask);
720 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
721 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
722 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
723 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
724 break;
725 case 32:
726 writew(C320p32rx_mask, ofsAddr + RX_mask);
727 writew(C320p32tx_mask, ofsAddr + TX_mask);
728 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
729 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
730 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
731 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
732 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
733 break;
734 }
735 }
736 break;
737 }
Jiri Slaby03718232008-04-30 00:53:39 -0700738 return 0;
739}
740
741static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
742{
David Howells2bca76e2008-07-08 17:37:15 +0100743 const void *ptr = fw->data;
Jiri Slaby03718232008-04-30 00:53:39 -0700744 char rsn[64];
745 u16 lens[5];
746 size_t len;
747 unsigned int a, lenp, lencnt;
748 int ret = -EINVAL;
749 struct {
750 __le32 magic; /* 0x34303430 */
751 u8 reserved1[2];
752 u8 type; /* UNIX = 3 */
753 u8 model; /* C218T=1, C320T=2, CP204=3 */
754 u8 reserved2[8];
755 __le16 len[5];
David Howells2bca76e2008-07-08 17:37:15 +0100756 } const *hdr = ptr;
Jiri Slaby03718232008-04-30 00:53:39 -0700757
758 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
759
760 if (fw->size < MOXA_FW_HDRLEN) {
761 strcpy(rsn, "too short (even header won't fit)");
762 goto err;
763 }
764 if (hdr->magic != cpu_to_le32(0x30343034)) {
765 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
766 goto err;
767 }
768 if (hdr->type != 3) {
769 sprintf(rsn, "not for linux, type is %u", hdr->type);
770 goto err;
771 }
772 if (moxa_check_fw_model(brd, hdr->model)) {
773 sprintf(rsn, "not for this card, model is %u", hdr->model);
774 goto err;
775 }
776
777 len = MOXA_FW_HDRLEN;
778 lencnt = hdr->model == 2 ? 5 : 3;
779 for (a = 0; a < ARRAY_SIZE(lens); a++) {
780 lens[a] = le16_to_cpu(hdr->len[a]);
781 if (lens[a] && len + lens[a] <= fw->size &&
782 moxa_check_fw(&fw->data[len]))
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700783 printk(KERN_WARNING "MOXA firmware: unexpected input "
Jiri Slaby03718232008-04-30 00:53:39 -0700784 "at offset %u, but going on\n", (u32)len);
785 if (!lens[a] && a < lencnt) {
786 sprintf(rsn, "too few entries in fw file");
787 goto err;
788 }
789 len += lens[a];
790 }
791
792 if (len != fw->size) {
793 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
794 (u32)len);
795 goto err;
796 }
797
798 ptr += MOXA_FW_HDRLEN;
799 lenp = 0; /* bios */
800
801 strcpy(rsn, "read above");
802
803 ret = moxa_load_bios(brd, ptr, lens[lenp]);
804 if (ret)
805 goto err;
806
807 /* we skip the tty section (lens[1]), since we don't need it */
808 ptr += lens[lenp] + lens[lenp + 1];
809 lenp += 2; /* comm */
810
811 if (hdr->model == 2) {
812 ret = moxa_load_320b(brd, ptr, lens[lenp]);
813 if (ret)
814 goto err;
815 /* skip another tty */
816 ptr += lens[lenp] + lens[lenp + 1];
817 lenp += 2;
818 }
819
820 ret = moxa_load_code(brd, ptr, lens[lenp]);
821 if (ret)
822 goto err;
823
824 return 0;
825err:
826 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
827 return ret;
828}
829
830static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
831{
832 const struct firmware *fw;
833 const char *file;
Jiri Slaby810ab092008-04-30 00:53:41 -0700834 struct moxa_port *p;
835 unsigned int i;
Jiri Slaby03718232008-04-30 00:53:39 -0700836 int ret;
837
Jiri Slaby810ab092008-04-30 00:53:41 -0700838 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
839 GFP_KERNEL);
840 if (brd->ports == NULL) {
841 printk(KERN_ERR "cannot allocate memory for ports\n");
842 ret = -ENOMEM;
843 goto err;
844 }
845
846 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
Alan Cox9de6a512008-07-16 21:56:02 +0100847 tty_port_init(&p->port);
Alan Cox31f35932009-01-02 13:45:05 +0000848 p->port.ops = &moxa_port_ops;
Alan Cox44b7d1b2008-07-16 21:57:18 +0100849 p->type = PORT_16550A;
850 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Jiri Slaby810ab092008-04-30 00:53:41 -0700851 }
852
Jiri Slaby03718232008-04-30 00:53:39 -0700853 switch (brd->boardType) {
854 case MOXA_BOARD_C218_ISA:
855 case MOXA_BOARD_C218_PCI:
856 file = "c218tunx.cod";
857 break;
858 case MOXA_BOARD_CP204J:
859 file = "cp204unx.cod";
860 break;
861 default:
862 file = "c320tunx.cod";
863 break;
864 }
865
866 ret = request_firmware(&fw, file, dev);
867 if (ret) {
Jiri Slabyec09cd52008-04-30 00:53:49 -0700868 printk(KERN_ERR "MOXA: request_firmware failed. Make sure "
869 "you've placed '%s' file into your firmware "
870 "loader directory (e.g. /lib/firmware)\n",
871 file);
Jiri Slaby810ab092008-04-30 00:53:41 -0700872 goto err_free;
Jiri Slaby03718232008-04-30 00:53:39 -0700873 }
874
875 ret = moxa_load_fw(brd, fw);
876
877 release_firmware(fw);
Jiri Slaby810ab092008-04-30 00:53:41 -0700878
879 if (ret)
880 goto err_free;
881
Jiri Slaby2a541342008-04-30 00:53:45 -0700882 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700883 brd->ready = 1;
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700884 if (!timer_pending(&moxaTimer))
885 mod_timer(&moxaTimer, jiffies + HZ / 50);
Jiri Slaby2a541342008-04-30 00:53:45 -0700886 spin_unlock_bh(&moxa_lock);
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700887
Jiri Slaby810ab092008-04-30 00:53:41 -0700888 return 0;
889err_free:
890 kfree(brd->ports);
891err:
Jiri Slaby03718232008-04-30 00:53:39 -0700892 return ret;
893}
894
Jiri Slaby810ab092008-04-30 00:53:41 -0700895static void moxa_board_deinit(struct moxa_board_conf *brd)
896{
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700897 unsigned int a, opened;
898
899 mutex_lock(&moxa_openlock);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700900 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700901 brd->ready = 0;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700902 spin_unlock_bh(&moxa_lock);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700903
904 /* pci hot-un-plug support */
905 for (a = 0; a < brd->numPorts; a++)
Alan Coxd450b5a2008-10-13 10:39:58 +0100906 if (brd->ports[a].port.flags & ASYNC_INITIALIZED) {
907 struct tty_struct *tty = tty_port_tty_get(
908 &brd->ports[a].port);
909 if (tty) {
910 tty_hangup(tty);
911 tty_kref_put(tty);
912 }
913 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700914 while (1) {
915 opened = 0;
916 for (a = 0; a < brd->numPorts; a++)
Alan Cox9de6a512008-07-16 21:56:02 +0100917 if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700918 opened++;
919 mutex_unlock(&moxa_openlock);
920 if (!opened)
921 break;
922 msleep(50);
923 mutex_lock(&moxa_openlock);
924 }
925
Jiri Slaby810ab092008-04-30 00:53:41 -0700926 iounmap(brd->basemem);
927 brd->basemem = NULL;
928 kfree(brd->ports);
929}
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931#ifdef CONFIG_PCI
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800932static int __devinit moxa_pci_probe(struct pci_dev *pdev,
933 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800935 struct moxa_board_conf *board;
936 unsigned int i;
937 int board_type = ent->driver_data;
938 int retval;
939
940 retval = pci_enable_device(pdev);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700941 if (retval) {
942 dev_err(&pdev->dev, "can't enable pci device\n");
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800943 goto err;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700944 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800945
946 for (i = 0; i < MAX_BOARDS; i++)
947 if (moxa_boards[i].basemem == NULL)
948 break;
949
950 retval = -ENODEV;
951 if (i >= MAX_BOARDS) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700952 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800953 "found. Board is ignored.\n", MAX_BOARDS);
954 goto err;
955 }
956
957 board = &moxa_boards[i];
Jiri Slabye46a5e32008-04-30 00:53:37 -0700958
959 retval = pci_request_region(pdev, 2, "moxa-base");
960 if (retval) {
961 dev_err(&pdev->dev, "can't request pci region 2\n");
962 goto err;
963 }
964
Alan Cox24cb2332008-04-30 00:54:19 -0700965 board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700966 if (board->basemem == NULL) {
967 dev_err(&pdev->dev, "can't remap io space 2\n");
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
1006static void __devexit moxa_pci_remove(struct pci_dev *pdev)
1007{
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,
1019 .remove = __devexit_p(moxa_pci_remove)
1020};
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001028 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
1029 MOXA_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
1031 if (!moxaDriver)
1032 return -ENOMEM;
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 moxaDriver->owner = THIS_MODULE;
Sergey Vlasov9b4e3b12005-09-03 16:26:49 +01001035 moxaDriver->name = "ttyMX";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 moxaDriver->major = ttymajor;
1037 moxaDriver->minor_start = 0;
1038 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1039 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1040 moxaDriver->init_termios = tty_std_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Alan Cox606d0992006-12-08 02:38:45 -08001042 moxaDriver->init_termios.c_ispeed = 9600;
1043 moxaDriver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
1045 tty_set_operations(moxaDriver, &moxa_ops);
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (tty_register_driver(moxaDriver)) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001048 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 put_tty_driver(moxaDriver);
1050 return -1;
1051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Jiri Slabyd353eca2008-04-30 00:53:37 -07001053 /* Find the boards defined from module args. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -07001055 {
1056 struct moxa_board_conf *brd = moxa_boards;
Jiri Slaby810ab092008-04-30 00:53:41 -07001057 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 for (i = 0; i < MAX_BOARDS; i++) {
Jiri Slabyd353eca2008-04-30 00:53:37 -07001059 if (!baseaddr[i])
1060 break;
1061 if (type[i] == MOXA_BOARD_C218_ISA ||
1062 type[i] == MOXA_BOARD_C320_ISA) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001063 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001064 isabrds + 1, moxa_brdname[type[i] - 1],
1065 baseaddr[i]);
1066 brd->boardType = type[i];
1067 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1068 numports[i];
1069 brd->busType = MOXA_BUS_TYPE_ISA;
Alan Cox24cb2332008-04-30 00:54:19 -07001070 brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
Jiri Slabyd353eca2008-04-30 00:53:37 -07001071 if (!brd->basemem) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001072 printk(KERN_ERR "MOXA: can't remap %lx\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001073 baseaddr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 continue;
1075 }
Jiri Slaby03718232008-04-30 00:53:39 -07001076 if (moxa_init_board(brd, NULL)) {
1077 iounmap(brd->basemem);
1078 brd->basemem = NULL;
1079 continue;
1080 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001081
Jiri Slabybb9f9102008-04-30 00:53:48 -07001082 printk(KERN_INFO "MOXA isa board found at 0x%.8lu and "
1083 "ready (%u ports, firmware loaded)\n",
1084 baseaddr[i], brd->numPorts);
1085
Jiri Slabyd353eca2008-04-30 00:53:37 -07001086 brd++;
1087 isabrds++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
1089 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001094 retval = pci_register_driver(&moxa_pci_driver);
1095 if (retval) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001096 printk(KERN_ERR "Can't register MOXA pci driver!\n");
Jiri Slabyd353eca2008-04-30 00:53:37 -07001097 if (isabrds)
Jiri Slabya784bf72007-02-10 01:45:36 -08001098 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001101
Jiri Slabya784bf72007-02-10 01:45:36 -08001102 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
1105static void __exit moxa_exit(void)
1106{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001107 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001109#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001110 pci_unregister_driver(&moxa_pci_driver);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001111#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001112
Jiri Slaby810ab092008-04-30 00:53:41 -07001113 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1114 if (moxa_boards[i].ready)
1115 moxa_board_deinit(&moxa_boards[i]);
Jiri Slaby2a541342008-04-30 00:53:45 -07001116
1117 del_timer_sync(&moxaTimer);
1118
1119 if (tty_unregister_driver(moxaDriver))
1120 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1121 "serial driver\n");
1122 put_tty_driver(moxaDriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123}
1124
1125module_init(moxa_init);
1126module_exit(moxa_exit);
1127
Alan Coxf1761782009-11-30 13:17:51 +00001128static void moxa_shutdown(struct tty_port *port)
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001129{
Alan Coxf1761782009-11-30 13:17:51 +00001130 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1131 MoxaPortDisable(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001132 MoxaPortFlushData(ch, 2);
Alan Coxf1761782009-11-30 13:17:51 +00001133 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001134}
1135
Alan Cox31f35932009-01-02 13:45:05 +00001136static int moxa_carrier_raised(struct tty_port *port)
1137{
1138 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1139 int dcd;
1140
Alan Cox8482bcd2009-11-30 13:18:13 +00001141 spin_lock_irq(&port->lock);
Alan Cox31f35932009-01-02 13:45:05 +00001142 dcd = ch->DCDState;
Alan Cox8482bcd2009-11-30 13:18:13 +00001143 spin_unlock_irq(&port->lock);
Alan Cox31f35932009-01-02 13:45:05 +00001144 return dcd;
1145}
1146
Alan Coxf1761782009-11-30 13:17:51 +00001147static void moxa_dtr_rts(struct tty_port *port, int onoff)
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001148{
Alan Coxf1761782009-11-30 13:17:51 +00001149 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1150 MoxaPortLineCtrl(ch, onoff, onoff);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001151}
1152
Alan Coxf1761782009-11-30 13:17:51 +00001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154static int moxa_open(struct tty_struct *tty, struct file *filp)
1155{
Jiri Slaby810ab092008-04-30 00:53:41 -07001156 struct moxa_board_conf *brd;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001157 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 int port;
1159 int retval;
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);
Alan Cox9de6a512008-07-16 21:56:02 +01001183 if (!(ch->port.flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 ch->statusflags = 0;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001185 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);
Alan Cox9de6a512008-07-16 21:56:02 +01001189 ch->port.flags |= ASYNC_INITIALIZED;
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
Alan Coxf1761782009-11-30 13:17:51 +00001194 retval = tty_port_block_til_ready(&ch->port, tty, filp);
1195 if (retval == 0)
1196 set_bit(ASYNCB_NORMAL_ACTIVE, &ch->port.flags);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001197 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
1199
1200static void moxa_close(struct tty_struct *tty, struct file *filp)
1201{
Alan Coxf1761782009-11-30 13:17:51 +00001202 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 ch->cflag = tty->termios->c_cflag;
Alan Coxf1761782009-11-30 13:17:51 +00001204 tty_port_close(&ch->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
1207static int moxa_write(struct tty_struct *tty,
1208 const unsigned char *buf, int count)
1209{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001210 struct moxa_port *ch = tty->driver_data;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001211 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 if (ch == NULL)
Jiri Slabyb4173f42008-04-30 00:53:40 -07001214 return 0;
Alan Cox33f0f882006-01-09 20:54:13 -08001215
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001216 spin_lock_bh(&moxa_lock);
Alan Coxd450b5a2008-10-13 10:39:58 +01001217 len = MoxaPortWriteData(tty, buf, count);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001218 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Alan Coxa808ac02009-11-30 13:18:02 +00001220 set_bit(LOWWAIT, &ch->statusflags);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001221 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
1224static int moxa_write_room(struct tty_struct *tty)
1225{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001226 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 if (tty->stopped)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001229 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001230 ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (ch == NULL)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001232 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001233 return MoxaPortTxFree(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
1236static void moxa_flush_buffer(struct tty_struct *tty)
1237{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001238 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 if (ch == NULL)
1241 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001242 MoxaPortFlushData(ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 tty_wakeup(tty);
1244}
1245
1246static int moxa_chars_in_buffer(struct tty_struct *tty)
1247{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001248 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Jiri Slabyb4173f42008-04-30 00:53:40 -07001251 chars = MoxaPortTxQueue(ch);
Alan Coxf710ebd2009-11-30 13:18:18 +00001252 if (chars)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 /*
1254 * Make it possible to wakeup anything waiting for output
1255 * in tty_ioctl.c, etc.
1256 */
Alan Coxf710ebd2009-11-30 13:18:18 +00001257 set_bit(EMPTYWAIT, &ch->statusflags);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001258 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259}
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1262{
Alan Cox8482bcd2009-11-30 13:18:13 +00001263 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 int flag = 0, dtr, rts;
1265
Jiri Slabyb4173f42008-04-30 00:53:40 -07001266 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (dtr)
1268 flag |= TIOCM_DTR;
1269 if (rts)
1270 flag |= TIOCM_RTS;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001271 dtr = MoxaPortLineStatus(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (dtr & 1)
1273 flag |= TIOCM_CTS;
1274 if (dtr & 2)
1275 flag |= TIOCM_DSR;
1276 if (dtr & 4)
1277 flag |= TIOCM_CD;
1278 return flag;
1279}
1280
1281static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1282 unsigned int set, unsigned int clear)
1283{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001284 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 int port;
1286 int dtr, rts;
1287
Jiri Slaby11324ed2007-02-10 01:45:31 -08001288 port = tty->index;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001289 mutex_lock(&moxa_openlock);
1290 ch = tty->driver_data;
1291 if (!ch) {
1292 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -07001293 return -EINVAL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Jiri Slabyb4173f42008-04-30 00:53:40 -07001296 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 if (set & TIOCM_RTS)
1298 rts = 1;
1299 if (set & TIOCM_DTR)
1300 dtr = 1;
1301 if (clear & TIOCM_RTS)
1302 rts = 0;
1303 if (clear & TIOCM_DTR)
1304 dtr = 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001305 MoxaPortLineCtrl(ch, dtr, rts);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001306 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return 0;
1308}
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310static void moxa_set_termios(struct tty_struct *tty,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001311 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001313 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 if (ch == NULL)
1316 return;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001317 moxa_set_tty_param(tty, old_termios);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001318 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
Alan Cox9de6a512008-07-16 21:56:02 +01001319 wake_up_interruptible(&ch->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
1321
1322static void moxa_stop(struct tty_struct *tty)
1323{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001324 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 if (ch == NULL)
1327 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001328 MoxaPortTxDisable(ch);
Alan Coxa808ac02009-11-30 13:18:02 +00001329 set_bit(TXSTOPPED, &ch->statusflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330}
1331
1332
1333static void moxa_start(struct tty_struct *tty)
1334{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001335 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 if (ch == NULL)
1338 return;
1339
1340 if (!(ch->statusflags & TXSTOPPED))
1341 return;
1342
Jiri Slabyb4173f42008-04-30 00:53:40 -07001343 MoxaPortTxEnable(ch);
Alan Coxa808ac02009-11-30 13:18:02 +00001344 clear_bit(TXSTOPPED, &ch->statusflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
1346
1347static void moxa_hangup(struct tty_struct *tty)
1348{
Alan Coxa808ac02009-11-30 13:18:02 +00001349 struct moxa_port *ch = tty->driver_data;
Alan Coxf1761782009-11-30 13:17:51 +00001350 tty_port_hangup(&ch->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
1352
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001353static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1354{
Alan Coxd450b5a2008-10-13 10:39:58 +01001355 struct tty_struct *tty;
Alan Cox8482bcd2009-11-30 13:18:13 +00001356 unsigned long flags;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001357 dcd = !!dcd;
1358
Alan Cox8482bcd2009-11-30 13:18:13 +00001359 spin_lock_irqsave(&p->port.lock, flags);
Alan Coxd450b5a2008-10-13 10:39:58 +01001360 if (dcd != p->DCDState) {
Alan Cox8482bcd2009-11-30 13:18:13 +00001361 p->DCDState = dcd;
1362 spin_unlock_irqrestore(&p->port.lock, flags);
Alan Coxd450b5a2008-10-13 10:39:58 +01001363 tty = tty_port_tty_get(&p->port);
1364 if (tty && C_CLOCAL(tty) && !dcd)
1365 tty_hangup(tty);
1366 tty_kref_put(tty);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001367 }
Alan Cox8482bcd2009-11-30 13:18:13 +00001368 else
1369 spin_unlock_irqrestore(&p->port.lock, flags);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001370}
1371
1372static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1373 u16 __iomem *ip)
1374{
Alan Coxd450b5a2008-10-13 10:39:58 +01001375 struct tty_struct *tty = tty_port_tty_get(&p->port);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001376 void __iomem *ofsAddr;
Alan Cox9de6a512008-07-16 21:56:02 +01001377 unsigned int inited = p->port.flags & ASYNC_INITIALIZED;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001378 u16 intr;
1379
1380 if (tty) {
Alan Coxa808ac02009-11-30 13:18:02 +00001381 if (test_bit(EMPTYWAIT, &p->statusflags) &&
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001382 MoxaPortTxQueue(p) == 0) {
Alan Coxa808ac02009-11-30 13:18:02 +00001383 clear_bit(EMPTYWAIT, &p->statusflags);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001384 tty_wakeup(tty);
1385 }
Alan Coxa808ac02009-11-30 13:18:02 +00001386 if (test_bit(LOWWAIT, &p->statusflags) && !tty->stopped &&
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001387 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
Alan Coxa808ac02009-11-30 13:18:02 +00001388 clear_bit(LOWWAIT, &p->statusflags);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001389 tty_wakeup(tty);
1390 }
1391
Alan Coxf9b412a2009-11-30 13:18:08 +00001392 if (inited && !test_bit(TTY_THROTTLED, &tty->flags) &&
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001393 MoxaPortRxQueue(p) > 0) { /* RX */
1394 MoxaPortReadData(p);
1395 tty_schedule_flip(tty);
1396 }
1397 } else {
Alan Coxa808ac02009-11-30 13:18:02 +00001398 clear_bit(EMPTYWAIT, &p->statusflags);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001399 MoxaPortFlushData(p, 0); /* flush RX */
1400 }
1401
1402 if (!handle) /* nothing else to do */
Jiri Slaby0e0fd7d2009-04-06 17:34:04 +01001403 goto put;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001404
1405 intr = readw(ip); /* port irq status */
1406 if (intr == 0)
Jiri Slaby0e0fd7d2009-04-06 17:34:04 +01001407 goto put;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001408
1409 writew(0, ip); /* ACK port */
1410 ofsAddr = p->tableAddr;
1411 if (intr & IntrTx) /* disable tx intr */
1412 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1413 ofsAddr + HostStat);
1414
1415 if (!inited)
Jiri Slaby0e0fd7d2009-04-06 17:34:04 +01001416 goto put;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001417
1418 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1419 tty_insert_flip_char(tty, 0, TTY_BREAK);
1420 tty_schedule_flip(tty);
1421 }
1422
1423 if (intr & IntrLine)
1424 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
Jiri Slaby0e0fd7d2009-04-06 17:34:04 +01001425put:
1426 tty_kref_put(tty);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001427
1428 return 0;
1429}
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431static void moxa_poll(unsigned long ignored)
1432{
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001433 struct moxa_board_conf *brd;
1434 u16 __iomem *ip;
Jiri Slaby2a541342008-04-30 00:53:45 -07001435 unsigned int card, port, served = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001437 spin_lock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 for (card = 0; card < MAX_BOARDS; card++) {
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001439 brd = &moxa_boards[card];
1440 if (!brd->ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 continue;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001442
Jiri Slaby2a541342008-04-30 00:53:45 -07001443 served++;
1444
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001445 ip = NULL;
1446 if (readb(brd->intPend) == 0xff)
1447 ip = brd->intTable + readb(brd->intNdx);
1448
1449 for (port = 0; port < brd->numPorts; port++)
1450 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1451
1452 if (ip)
1453 writeb(0, brd->intPend); /* ACK */
1454
1455 if (moxaLowWaterChk) {
1456 struct moxa_port *p = brd->ports;
1457 for (port = 0; port < brd->numPorts; port++, p++)
1458 if (p->lowChkFlag) {
1459 p->lowChkFlag = 0;
1460 moxa_low_water_check(p->tableAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
1463 }
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001464 moxaLowWaterChk = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Jiri Slaby2a541342008-04-30 00:53:45 -07001466 if (served)
1467 mod_timer(&moxaTimer, jiffies + HZ / 50);
1468 spin_unlock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
1471/******************************************************************************/
1472
Alan Coxdb1acaa2008-02-08 04:18:43 -08001473static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001475 register struct ktermios *ts = tty->termios;
1476 struct moxa_port *ch = tty->driver_data;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001477 int rts, cts, txflow, rxflow, xany, baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 rts = cts = txflow = rxflow = xany = 0;
1480 if (ts->c_cflag & CRTSCTS)
1481 rts = cts = 1;
1482 if (ts->c_iflag & IXON)
1483 txflow = 1;
1484 if (ts->c_iflag & IXOFF)
1485 rxflow = 1;
1486 if (ts->c_iflag & IXANY)
1487 xany = 1;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001488
1489 /* Clear the features we don't support */
1490 ts->c_cflag &= ~CMSPAR;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001491 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1492 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
Alan Coxdb1acaa2008-02-08 04:18:43 -08001493 if (baud == -1)
1494 baud = tty_termios_baud_rate(old_termios);
1495 /* Not put the baud rate into the termios data */
1496 tty_encode_baud_rate(tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499/*****************************************************************************
1500 * Driver level functions: *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Jiri Slabyb4173f42008-04-30 00:53:40 -07001503static void MoxaPortFlushData(struct moxa_port *port, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
1505 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001506 if (mode < 0 || mode > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001508 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 moxafunc(ofsAddr, FC_FlushQueue, mode);
1510 if (mode != 1) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001511 port->lowChkFlag = 0;
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001512 moxa_low_water_check(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
1514}
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516/*
1517 * Moxa Port Number Description:
1518 *
1519 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1520 * the port number using in MOXA driver functions will be 0 to 31 for
1521 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1522 * to 127 for fourth. For example, if you setup three MOXA boards,
1523 * first board is C218, second board is C320-16 and third board is
1524 * C320-32. The port number of first board (C218 - 8 ports) is from
1525 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1526 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1527 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1528 * 127 will be invalid.
1529 *
1530 *
1531 * Moxa Functions Description:
1532 *
1533 * Function 1: Driver initialization routine, this routine must be
1534 * called when initialized driver.
1535 * Syntax:
1536 * void MoxaDriverInit();
1537 *
1538 *
1539 * Function 2: Moxa driver private IOCTL command processing.
1540 * Syntax:
1541 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1542 *
1543 * unsigned int cmd : IOCTL command
1544 * unsigned long arg : IOCTL argument
1545 * int port : port number (0 - 127)
1546 *
1547 * return: 0 (OK)
1548 * -EINVAL
1549 * -ENOIOCTLCMD
1550 *
1551 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 * Function 6: Enable this port to start Tx/Rx data.
1553 * Syntax:
1554 * void MoxaPortEnable(int port);
1555 * int port : port number (0 - 127)
1556 *
1557 *
1558 * Function 7: Disable this port
1559 * Syntax:
1560 * void MoxaPortDisable(int port);
1561 * int port : port number (0 - 127)
1562 *
1563 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 * Function 10: Setting baud rate of this port.
1565 * Syntax:
Jiri Slaby08d01c792008-04-30 00:53:47 -07001566 * speed_t MoxaPortSetBaud(int port, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 * int port : port number (0 - 127)
1568 * long baud : baud rate (50 - 115200)
1569 *
1570 * return: 0 : this port is invalid or baud < 50
1571 * 50 - 115200 : the real baud rate set to the port, if
1572 * the argument baud is large than maximun
1573 * available baud rate, the real setting
1574 * baud rate will be the maximun baud rate.
1575 *
1576 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 * Function 12: Configure the port.
1578 * Syntax:
Alan Cox606d0992006-12-08 02:38:45 -08001579 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 * int port : port number (0 - 127)
Alan Cox606d0992006-12-08 02:38:45 -08001581 * struct ktermios * termio : termio structure pointer
Alan Coxc7bce302006-09-30 23:27:24 -07001582 * speed_t baud : baud rate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 *
1584 * return: -1 : this port is invalid or termio == NULL
1585 * 0 : setting O.K.
1586 *
1587 *
1588 * Function 13: Get the DTR/RTS state of this port.
1589 * Syntax:
1590 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1591 * int port : port number (0 - 127)
1592 * int * dtrState : pointer to INT to receive the current DTR
1593 * state. (if NULL, this function will not
1594 * write to this address)
1595 * int * rtsState : pointer to INT to receive the current RTS
1596 * state. (if NULL, this function will not
1597 * write to this address)
1598 *
1599 * return: -1 : this port is invalid
1600 * 0 : O.K.
1601 *
1602 *
1603 * Function 14: Setting the DTR/RTS output state of this port.
1604 * Syntax:
1605 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1606 * int port : port number (0 - 127)
1607 * int dtrState : DTR output state (0: off, 1: on)
1608 * int rtsState : RTS output state (0: off, 1: on)
1609 *
1610 *
1611 * Function 15: Setting the flow control of this port.
1612 * Syntax:
1613 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1614 * int txFlow,int xany);
1615 * int port : port number (0 - 127)
1616 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1617 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1618 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1619 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1620 * int xany : S/W XANY flow control (0: no, 1: yes)
1621 *
1622 *
1623 * Function 16: Get ths line status of this port
1624 * Syntax:
1625 * int MoxaPortLineStatus(int port);
1626 * int port : port number (0 - 127)
1627 *
1628 * return: Bit 0 - CTS state (0: off, 1: on)
1629 * Bit 1 - DSR state (0: off, 1: on)
1630 * Bit 2 - DCD state (0: off, 1: on)
1631 *
1632 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 * Function 19: Flush the Rx/Tx buffer data of this port.
1634 * Syntax:
1635 * void MoxaPortFlushData(int port, int mode);
1636 * int port : port number (0 - 127)
1637 * int mode
1638 * 0 : flush the Rx buffer
1639 * 1 : flush the Tx buffer
1640 * 2 : flush the Rx and Tx buffer
1641 *
1642 *
1643 * Function 20: Write data.
1644 * Syntax:
1645 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1646 * int port : port number (0 - 127)
1647 * unsigned char * buffer : pointer to write data buffer.
1648 * int length : write data length
1649 *
1650 * return: 0 - length : real write data length
1651 *
1652 *
1653 * Function 21: Read data.
1654 * Syntax:
Alan Cox33f0f882006-01-09 20:54:13 -08001655 * int MoxaPortReadData(int port, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 * int port : port number (0 - 127)
Alan Cox33f0f882006-01-09 20:54:13 -08001657 * struct tty_struct *tty : tty for data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 *
1659 * return: 0 - length : real read data length
1660 *
1661 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 * Function 24: Get the Tx buffer current queued data bytes
1663 * Syntax:
1664 * int MoxaPortTxQueue(int port);
1665 * int port : port number (0 - 127)
1666 *
1667 * return: .. : Tx buffer current queued data bytes
1668 *
1669 *
1670 * Function 25: Get the Tx buffer current free space
1671 * Syntax:
1672 * int MoxaPortTxFree(int port);
1673 * int port : port number (0 - 127)
1674 *
1675 * return: .. : Tx buffer current free space
1676 *
1677 *
1678 * Function 26: Get the Rx buffer current queued data bytes
1679 * Syntax:
1680 * int MoxaPortRxQueue(int port);
1681 * int port : port number (0 - 127)
1682 *
1683 * return: .. : Rx buffer current queued data bytes
1684 *
1685 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 * Function 28: Disable port data transmission.
1687 * Syntax:
1688 * void MoxaPortTxDisable(int port);
1689 * int port : port number (0 - 127)
1690 *
1691 *
1692 * Function 29: Enable port data transmission.
1693 * Syntax:
1694 * void MoxaPortTxEnable(int port);
1695 * int port : port number (0 - 127)
1696 *
1697 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 * Function 31: Get the received BREAK signal count and reset it.
1699 * Syntax:
1700 * int MoxaPortResetBrkCnt(int port);
1701 * int port : port number (0 - 127)
1702 *
1703 * return: 0 - .. : BREAK signal count
1704 *
1705 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Jiri Slabyb4173f42008-04-30 00:53:40 -07001708static void MoxaPortEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
1710 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001711 u16 lowwater = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Jiri Slabyb4173f42008-04-30 00:53:40 -07001713 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 writew(lowwater, ofsAddr + Low_water);
Jiri Slaby08d01c792008-04-30 00:53:47 -07001715 if (MOXA_IS_320(port->board))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001717 else
1718 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1719 ofsAddr + HostStat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1722 moxafunc(ofsAddr, FC_FlushQueue, 2);
1723
1724 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1725 MoxaPortLineStatus(port);
1726}
1727
Jiri Slabyb4173f42008-04-30 00:53:40 -07001728static void MoxaPortDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001730 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1733 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1734 writew(0, ofsAddr + HostStat);
1735 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1736}
1737
Jiri Slaby08d01c792008-04-30 00:53:47 -07001738static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
Jiri Slaby08d01c792008-04-30 00:53:47 -07001740 void __iomem *ofsAddr = port->tableAddr;
1741 unsigned int clock, val;
1742 speed_t max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Jiri Slaby08d01c792008-04-30 00:53:47 -07001744 max = MOXA_IS_320(port->board) ? 460800 : 921600;
1745 if (baud < 50)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001746 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 if (baud > max)
1748 baud = max;
Jiri Slaby08d01c792008-04-30 00:53:47 -07001749 clock = 921600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 val = clock / baud;
1751 moxafunc(ofsAddr, FC_SetBaud, val);
1752 baud = clock / val;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001753 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
Jiri Slabyb4173f42008-04-30 00:53:40 -07001756static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1757 speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
1759 void __iomem *ofsAddr;
1760 tcflag_t cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 tcflag_t mode = 0;
1762
Jiri Slabyb4173f42008-04-30 00:53:40 -07001763 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 cflag = termio->c_cflag; /* termio->c_cflag */
1765
1766 mode = termio->c_cflag & CSIZE;
1767 if (mode == CS5)
1768 mode = MX_CS5;
1769 else if (mode == CS6)
1770 mode = MX_CS6;
1771 else if (mode == CS7)
1772 mode = MX_CS7;
1773 else if (mode == CS8)
1774 mode = MX_CS8;
1775
1776 if (termio->c_cflag & CSTOPB) {
1777 if (mode == MX_CS5)
1778 mode |= MX_STOP15;
1779 else
1780 mode |= MX_STOP2;
1781 } else
1782 mode |= MX_STOP1;
1783
1784 if (termio->c_cflag & PARENB) {
1785 if (termio->c_cflag & PARODD)
1786 mode |= MX_PARODD;
1787 else
1788 mode |= MX_PAREVEN;
1789 } else
1790 mode |= MX_PARNONE;
1791
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001792 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Jiri Slaby08d01c792008-04-30 00:53:47 -07001794 if (MOXA_IS_320(port->board) && baud >= 921600)
1795 return -1;
1796
Alan Coxdb1acaa2008-02-08 04:18:43 -08001797 baud = MoxaPortSetBaud(port, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
1799 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
Alan Coxf5c5a362009-11-30 13:17:57 +00001800 spin_lock_irq(&moxafunc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1802 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1803 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001804 moxa_wait_finish(ofsAddr);
Alan Coxa808ac02009-11-30 13:18:02 +00001805 spin_unlock_irq(&moxafunc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
1807 }
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001808 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809}
1810
Jiri Slabyb4173f42008-04-30 00:53:40 -07001811static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1812 int *rtsState)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001814 if (dtrState)
1815 *dtrState = !!(port->lineCtrl & DTR_ON);
1816 if (rtsState)
1817 *rtsState = !!(port->lineCtrl & RTS_ON);
1818
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001819 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
1821
Jiri Slabyb4173f42008-04-30 00:53:40 -07001822static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001824 u8 mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 if (dtr)
1827 mode |= DTR_ON;
1828 if (rts)
1829 mode |= RTS_ON;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001830 port->lineCtrl = mode;
1831 moxafunc(port->tableAddr, FC_LineControl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832}
1833
Jiri Slabyb4173f42008-04-30 00:53:40 -07001834static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1835 int txflow, int rxflow, int txany)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001837 int mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 if (rts)
1840 mode |= RTS_FlowCtl;
1841 if (cts)
1842 mode |= CTS_FlowCtl;
1843 if (txflow)
1844 mode |= Tx_FlowCtl;
1845 if (rxflow)
1846 mode |= Rx_FlowCtl;
1847 if (txany)
1848 mode |= IXM_IXANY;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001849 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850}
1851
Jiri Slabyb4173f42008-04-30 00:53:40 -07001852static int MoxaPortLineStatus(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
1854 void __iomem *ofsAddr;
1855 int val;
1856
Jiri Slabyb4173f42008-04-30 00:53:40 -07001857 ofsAddr = port->tableAddr;
Alan Coxf5c5a362009-11-30 13:17:57 +00001858 if (MOXA_IS_320(port->board))
1859 val = moxafuncret(ofsAddr, FC_LineStatus, 0);
1860 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 val = readw(ofsAddr + FlagStat) >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 val &= 0x0B;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001863 if (val & 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 val |= 4;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001865 moxa_new_dcdstate(port, val & 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 val &= 7;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001867 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
Alan Coxd450b5a2008-10-13 10:39:58 +01001870static int MoxaPortWriteData(struct tty_struct *tty,
Jiri Slaby2108eba2008-04-30 00:53:44 -07001871 const unsigned char *buffer, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
Alan Coxd450b5a2008-10-13 10:39:58 +01001873 struct moxa_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001875 unsigned int c, total;
1876 u16 head, tail, tx_mask, spage, epage;
1877 u16 pageno, pageofs, bufhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Jiri Slabyb4173f42008-04-30 00:53:40 -07001879 ofsAddr = port->tableAddr;
1880 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 tx_mask = readw(ofsAddr + TX_mask);
1882 spage = readw(ofsAddr + Page_txb);
1883 epage = readw(ofsAddr + EndPage_txb);
1884 tail = readw(ofsAddr + TXwptr);
1885 head = readw(ofsAddr + TXrptr);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001886 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 if (c > len)
1888 c = len;
Alan Cox9de6a512008-07-16 21:56:02 +01001889 moxaLog.txcnt[port->port.tty->index] += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 total = c;
1891 if (spage == epage) {
1892 bufhead = readw(ofsAddr + Ofs_txb);
1893 writew(spage, baseAddr + Control_reg);
1894 while (c > 0) {
1895 if (head > tail)
1896 len = head - tail - 1;
1897 else
1898 len = tx_mask + 1 - tail;
1899 len = (c > len) ? len : c;
1900 ofs = baseAddr + DynPage_addr + bufhead + tail;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001901 memcpy_toio(ofs, buffer, len);
1902 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 tail = (tail + len) & tx_mask;
1904 c -= len;
1905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 pageno = spage + (tail >> 13);
1908 pageofs = tail & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001909 while (c > 0) {
1910 len = Page_size - pageofs;
1911 if (len > c)
1912 len = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 writeb(pageno, baseAddr + Control_reg);
1914 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001915 memcpy_toio(ofs, buffer, len);
1916 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (++pageno == epage)
1918 pageno = spage;
1919 pageofs = 0;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001920 c -= len;
1921 }
1922 tail = (tail + total) & tx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07001924 writew(tail, ofsAddr + TXwptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 writeb(1, ofsAddr + CD180TXirq); /* start to send */
Jiri Slaby2108eba2008-04-30 00:53:44 -07001926 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
1928
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001929static int MoxaPortReadData(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
Alan Cox9de6a512008-07-16 21:56:02 +01001931 struct tty_struct *tty = port->port.tty;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001932 unsigned char *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001934 unsigned int count, len, total;
1935 u16 tail, rx_mask, spage, epage;
1936 u16 pageno, pageofs, bufhead, head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Jiri Slabyb4173f42008-04-30 00:53:40 -07001938 ofsAddr = port->tableAddr;
1939 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 head = readw(ofsAddr + RXrptr);
1941 tail = readw(ofsAddr + RXwptr);
1942 rx_mask = readw(ofsAddr + RX_mask);
1943 spage = readw(ofsAddr + Page_rxb);
1944 epage = readw(ofsAddr + EndPage_rxb);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001945 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 if (count == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08001947 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Alan Cox33f0f882006-01-09 20:54:13 -08001949 total = count;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001950 moxaLog.rxcnt[tty->index] += total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 if (spage == epage) {
1952 bufhead = readw(ofsAddr + Ofs_rxb);
1953 writew(spage, baseAddr + Control_reg);
1954 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 ofs = baseAddr + DynPage_addr + bufhead + head;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001956 len = (tail >= head) ? (tail - head) :
1957 (rx_mask + 1 - head);
1958 len = tty_prepare_flip_string(tty, &dst,
1959 min(len, count));
1960 memcpy_fromio(dst, ofs, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 head = (head + len) & rx_mask;
1962 count -= len;
1963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 pageno = spage + (head >> 13);
1966 pageofs = head & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001967 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 writew(pageno, baseAddr + Control_reg);
1969 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001970 len = tty_prepare_flip_string(tty, &dst,
1971 min(Page_size - pageofs, count));
1972 memcpy_fromio(dst, ofs, len);
1973
1974 count -= len;
1975 pageofs = (pageofs + len) & Page_mask;
1976 if (pageofs == 0 && ++pageno == epage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 pageno = spage;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001978 }
1979 head = (head + total) & rx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07001981 writew(head, ofsAddr + RXrptr);
1982 if (readb(ofsAddr + FlagStat) & Xoff_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 moxaLowWaterChk = 1;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001984 port->lowChkFlag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07001986 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
1988
1989
Jiri Slabyb4173f42008-04-30 00:53:40 -07001990static int MoxaPortTxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001992 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001993 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 rptr = readw(ofsAddr + TXrptr);
1996 wptr = readw(ofsAddr + TXwptr);
1997 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001998 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
2000
Jiri Slabyb4173f42008-04-30 00:53:40 -07002001static int MoxaPortTxFree(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002003 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002004 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 rptr = readw(ofsAddr + TXrptr);
2007 wptr = readw(ofsAddr + TXwptr);
2008 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002009 return mask - ((wptr - rptr) & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010}
2011
Jiri Slabyb4173f42008-04-30 00:53:40 -07002012static int MoxaPortRxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002014 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002015 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 rptr = readw(ofsAddr + RXrptr);
2018 wptr = readw(ofsAddr + RXwptr);
2019 mask = readw(ofsAddr + RX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002020 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021}
2022
Jiri Slabyb4173f42008-04-30 00:53:40 -07002023static void MoxaPortTxDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002025 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
Jiri Slabyb4173f42008-04-30 00:53:40 -07002028static void MoxaPortTxEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002030 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
2032
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002033static int moxa_get_serial_info(struct moxa_port *info,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002034 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002036 struct serial_struct tmp = {
2037 .type = info->type,
Alan Cox9de6a512008-07-16 21:56:02 +01002038 .line = info->port.tty->index,
2039 .flags = info->port.flags,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002040 .baud_base = 921600,
Alan Cox44b7d1b2008-07-16 21:57:18 +01002041 .close_delay = info->port.close_delay
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002042 };
2043 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044}
2045
2046
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002047static int moxa_set_serial_info(struct moxa_port *info,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002048 struct serial_struct __user *new_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
2050 struct serial_struct new_serial;
2051
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002052 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 return -EFAULT;
2054
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002055 if (new_serial.irq != 0 || new_serial.port != 0 ||
2056 new_serial.custom_divisor != 0 ||
2057 new_serial.baud_base != 921600)
2058 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 if (!capable(CAP_SYS_ADMIN)) {
2061 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
Alan Cox9de6a512008-07-16 21:56:02 +01002062 (info->port.flags & ~ASYNC_USR_MASK)))
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002063 return -EPERM;
2064 } else
Alan Cox44b7d1b2008-07-16 21:57:18 +01002065 info->port.close_delay = new_serial.close_delay * HZ / 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
Alan Cox9de6a512008-07-16 21:56:02 +01002068 new_serial.flags |= (info->port.flags & ASYNC_FLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002070 MoxaSetFifo(info, new_serial.type == PORT_16550A);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072 info->type = new_serial.type;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002073 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074}
2075
2076
2077
2078/*****************************************************************************
2079 * Static local functions: *
2080 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Jiri Slabyb4173f42008-04-30 00:53:40 -07002082static void MoxaSetFifo(struct moxa_port *port, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002084 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086 if (!enable) {
2087 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2088 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2089 } else {
2090 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2091 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2092 }
2093}