blob: d53fac5229bf76ff094787877d5b1b5954a1a5b9 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/* Variables for insmod */
167#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700168static unsigned long baseaddr[MAX_BOARDS];
169static unsigned int type[MAX_BOARDS];
170static unsigned int numports[MAX_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171#endif
172
173MODULE_AUTHOR("William Chen");
174MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
175MODULE_LICENSE("GPL");
176#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700177module_param_array(type, uint, NULL, 0);
178MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
179module_param_array(baseaddr, ulong, NULL, 0);
180MODULE_PARM_DESC(baseaddr, "base address");
181module_param_array(numports, uint, NULL, 0);
182MODULE_PARM_DESC(numports, "numports (ignored for C218)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183#endif
184module_param(ttymajor, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/*
187 * static functions:
188 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static int moxa_open(struct tty_struct *, struct file *);
190static void moxa_close(struct tty_struct *, struct file *);
191static int moxa_write(struct tty_struct *, const unsigned char *, int);
192static int moxa_write_room(struct tty_struct *);
193static void moxa_flush_buffer(struct tty_struct *);
194static int moxa_chars_in_buffer(struct tty_struct *);
Alan Cox606d0992006-12-08 02:38:45 -0800195static void moxa_set_termios(struct tty_struct *, struct ktermios *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static void moxa_stop(struct tty_struct *);
197static void moxa_start(struct tty_struct *);
198static void moxa_hangup(struct tty_struct *);
199static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
200static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
201 unsigned int set, unsigned int clear);
202static void moxa_poll(unsigned long);
Alan Coxdb1acaa2008-02-08 04:18:43 -0800203static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
Alan Coxf1761782009-11-30 13:17:51 +0000204static void moxa_shutdown(struct tty_port *);
Alan Cox31f35932009-01-02 13:45:05 +0000205static int moxa_carrier_raised(struct tty_port *);
Alan Coxf1761782009-11-30 13:17:51 +0000206static void moxa_dtr_rts(struct tty_port *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207/*
208 * moxa board interface functions:
209 */
Jiri Slabyb4173f42008-04-30 00:53:40 -0700210static void MoxaPortEnable(struct moxa_port *);
211static void MoxaPortDisable(struct moxa_port *);
212static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
213static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
214static void MoxaPortLineCtrl(struct moxa_port *, int, int);
215static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
216static int MoxaPortLineStatus(struct moxa_port *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700217static void MoxaPortFlushData(struct moxa_port *, int);
Alan Coxd450b5a2008-10-13 10:39:58 +0100218static int MoxaPortWriteData(struct tty_struct *, const unsigned char *, int);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700219static int MoxaPortReadData(struct moxa_port *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700220static int MoxaPortTxQueue(struct moxa_port *);
221static int MoxaPortRxQueue(struct moxa_port *);
222static int MoxaPortTxFree(struct moxa_port *);
223static void MoxaPortTxDisable(struct moxa_port *);
224static void MoxaPortTxEnable(struct moxa_port *);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800225static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
226static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700227static void MoxaSetFifo(struct moxa_port *port, int enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Jiri Slaby74d7d972008-04-30 00:53:43 -0700229/*
230 * I/O functions
231 */
232
Alan Coxa808ac02009-11-30 13:18:02 +0000233static DEFINE_SPINLOCK(moxafunc_lock);
234
Jiri Slaby74d7d972008-04-30 00:53:43 -0700235static void moxa_wait_finish(void __iomem *ofsAddr)
236{
237 unsigned long end = jiffies + moxaFuncTout;
238
239 while (readw(ofsAddr + FuncCode) != 0)
240 if (time_after(jiffies, end))
241 return;
242 if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
243 printk(KERN_WARNING "moxa function expired\n");
244}
245
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700246static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
Jiri Slaby74d7d972008-04-30 00:53:43 -0700247{
Alan Coxf5c5a362009-11-30 13:17:57 +0000248 unsigned long flags;
249 spin_lock_irqsave(&moxafunc_lock, flags);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700250 writew(arg, ofsAddr + FuncArg);
251 writew(cmd, ofsAddr + FuncCode);
252 moxa_wait_finish(ofsAddr);
Alan Coxf5c5a362009-11-30 13:17:57 +0000253 spin_unlock_irqrestore(&moxafunc_lock, flags);
254}
255
256static int moxafuncret(void __iomem *ofsAddr, u16 cmd, u16 arg)
257{
258 unsigned long flags;
259 u16 ret;
260 spin_lock_irqsave(&moxafunc_lock, flags);
261 writew(arg, ofsAddr + FuncArg);
262 writew(cmd, ofsAddr + FuncCode);
263 moxa_wait_finish(ofsAddr);
264 ret = readw(ofsAddr + FuncArg);
265 spin_unlock_irqrestore(&moxafunc_lock, flags);
266 return ret;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700267}
268
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700269static void moxa_low_water_check(void __iomem *ofsAddr)
270{
271 u16 rptr, wptr, mask, len;
272
273 if (readb(ofsAddr + FlagStat) & Xoff_state) {
274 rptr = readw(ofsAddr + RXrptr);
275 wptr = readw(ofsAddr + RXwptr);
276 mask = readw(ofsAddr + RX_mask);
277 len = (wptr - rptr) & mask;
278 if (len <= Low_water)
279 moxafunc(ofsAddr, FC_SendXon, 0);
280 }
281}
282
Jiri Slaby74d7d972008-04-30 00:53:43 -0700283/*
284 * TTY operations
285 */
286
287static int moxa_ioctl(struct tty_struct *tty, struct file *file,
288 unsigned int cmd, unsigned long arg)
289{
290 struct moxa_port *ch = tty->driver_data;
291 void __user *argp = (void __user *)arg;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700292 int status, ret = 0;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700293
294 if (tty->index == MAX_PORTS) {
295 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
296 cmd != MOXA_GETMSTATUS)
297 return -EINVAL;
298 } else if (!ch)
299 return -ENODEV;
300
301 switch (cmd) {
302 case MOXA_GETDATACOUNT:
303 moxaLog.tick = jiffies;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700304 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
305 ret = -EFAULT;
306 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700307 case MOXA_FLUSH_QUEUE:
308 MoxaPortFlushData(ch, arg);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700309 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700310 case MOXA_GET_IOQUEUE: {
311 struct moxaq_str __user *argm = argp;
312 struct moxaq_str tmp;
313 struct moxa_port *p;
314 unsigned int i, j;
315
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700316 mutex_lock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700317 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));
321 if (moxa_boards[i].ready) {
322 tmp.inq = MoxaPortRxQueue(p);
323 tmp.outq = MoxaPortTxQueue(p);
324 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700325 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
326 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700327 return -EFAULT;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700328 }
Jiri Slaby74d7d972008-04-30 00:53:43 -0700329 }
330 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700331 mutex_unlock(&moxa_openlock);
332 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700333 } case MOXA_GET_OQUEUE:
334 status = MoxaPortTxQueue(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700335 ret = put_user(status, (unsigned long __user *)argp);
336 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700337 case MOXA_GET_IQUEUE:
338 status = MoxaPortRxQueue(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700339 ret = put_user(status, (unsigned long __user *)argp);
340 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700341 case MOXA_GETMSTATUS: {
342 struct mxser_mstatus __user *argm = argp;
343 struct mxser_mstatus tmp;
344 struct moxa_port *p;
345 unsigned int i, j;
346
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700347 mutex_lock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700348 for (i = 0; i < MAX_BOARDS; i++) {
349 p = moxa_boards[i].ports;
350 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
Alan Coxd450b5a2008-10-13 10:39:58 +0100351 struct tty_struct *ttyp;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700352 memset(&tmp, 0, sizeof(tmp));
353 if (!moxa_boards[i].ready)
354 goto copy;
355
356 status = MoxaPortLineStatus(p);
357 if (status & 1)
358 tmp.cts = 1;
359 if (status & 2)
360 tmp.dsr = 1;
361 if (status & 4)
362 tmp.dcd = 1;
363
Alan Coxd450b5a2008-10-13 10:39:58 +0100364 ttyp = tty_port_tty_get(&p->port);
365 if (!ttyp || !ttyp->termios)
Jiri Slaby74d7d972008-04-30 00:53:43 -0700366 tmp.cflag = p->cflag;
367 else
Alan Coxd450b5a2008-10-13 10:39:58 +0100368 tmp.cflag = ttyp->termios->c_cflag;
369 tty_kref_put(tty);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700370copy:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700371 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
372 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700373 return -EFAULT;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700374 }
Jiri Slaby74d7d972008-04-30 00:53:43 -0700375 }
376 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700377 mutex_unlock(&moxa_openlock);
378 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);
Ingo Molnar34af9462006-06-27 02:53:55 -0700430static DEFINE_SPINLOCK(moxa_lock);
Alan Cox33f0f882006-01-09 20:54:13 -0800431
Jiri Slaby74d7d972008-04-30 00:53:43 -0700432/*
433 * HW init
434 */
435
Jiri Slaby03718232008-04-30 00:53:39 -0700436static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
437{
438 switch (brd->boardType) {
439 case MOXA_BOARD_C218_ISA:
440 case MOXA_BOARD_C218_PCI:
441 if (model != 1)
442 goto err;
443 break;
444 case MOXA_BOARD_CP204J:
445 if (model != 3)
446 goto err;
447 break;
448 default:
449 if (model != 2)
450 goto err;
451 break;
452 }
453 return 0;
454err:
455 return -EINVAL;
456}
457
458static int moxa_check_fw(const void *ptr)
459{
460 const __le16 *lptr = ptr;
461
462 if (*lptr != cpu_to_le16(0x7980))
463 return -EINVAL;
464
465 return 0;
466}
467
468static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
469 size_t len)
470{
471 void __iomem *baseAddr = brd->basemem;
472 u16 tmp;
473
474 writeb(HW_reset, baseAddr + Control_reg); /* reset */
475 msleep(10);
476 memset_io(baseAddr, 0, 4096);
477 memcpy_toio(baseAddr, buf, len); /* download BIOS */
478 writeb(0, baseAddr + Control_reg); /* restart */
479
480 msleep(2000);
481
482 switch (brd->boardType) {
483 case MOXA_BOARD_C218_ISA:
484 case MOXA_BOARD_C218_PCI:
485 tmp = readw(baseAddr + C218_key);
486 if (tmp != C218_KeyCode)
487 goto err;
488 break;
489 case MOXA_BOARD_CP204J:
490 tmp = readw(baseAddr + C218_key);
491 if (tmp != CP204J_KeyCode)
492 goto err;
493 break;
494 default:
495 tmp = readw(baseAddr + C320_key);
496 if (tmp != C320_KeyCode)
497 goto err;
498 tmp = readw(baseAddr + C320_status);
499 if (tmp != STS_init) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700500 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
Jiri Slaby03718232008-04-30 00:53:39 -0700501 "module not found\n");
502 return -EIO;
503 }
504 break;
505 }
506
507 return 0;
508err:
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700509 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700510 return -EIO;
511}
512
513static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
514 size_t len)
515{
516 void __iomem *baseAddr = brd->basemem;
517
518 if (len < 7168) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700519 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700520 return -EINVAL;
521 }
522
523 writew(len - 7168 - 2, baseAddr + C320bapi_len);
524 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
525 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
526 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
527 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
528
529 return 0;
530}
531
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700532static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
Jiri Slaby03718232008-04-30 00:53:39 -0700533 size_t len)
534{
535 void __iomem *baseAddr = brd->basemem;
Harvey Harrisonb46f69c2008-10-15 22:04:19 -0700536 const __le16 *uptr = ptr;
Jiri Slaby03718232008-04-30 00:53:39 -0700537 size_t wlen, len2, j;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700538 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
Jiri Slaby08d01c792008-04-30 00:53:47 -0700539 unsigned int i, retry;
Jiri Slaby03718232008-04-30 00:53:39 -0700540 u16 usum, keycode;
541
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700542 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
543 C218_KeyCode;
544
545 switch (brd->boardType) {
546 case MOXA_BOARD_CP204J:
547 case MOXA_BOARD_C218_ISA:
548 case MOXA_BOARD_C218_PCI:
549 key = C218_key;
550 loadbuf = C218_LoadBuf;
551 loadlen = C218DLoad_len;
552 checksum = C218check_sum;
553 checksum_ok = C218chksum_ok;
554 break;
555 default:
556 key = C320_key;
557 keycode = C320_KeyCode;
558 loadbuf = C320_LoadBuf;
559 loadlen = C320DLoad_len;
560 checksum = C320check_sum;
561 checksum_ok = C320chksum_ok;
562 break;
563 }
564
Jiri Slaby03718232008-04-30 00:53:39 -0700565 usum = 0;
566 wlen = len >> 1;
567 for (i = 0; i < wlen; i++)
568 usum += le16_to_cpu(uptr[i]);
569 retry = 0;
570 do {
571 wlen = len >> 1;
572 j = 0;
573 while (wlen) {
574 len2 = (wlen > 2048) ? 2048 : wlen;
575 wlen -= len2;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700576 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
Jiri Slaby03718232008-04-30 00:53:39 -0700577 j += len2 << 1;
578
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700579 writew(len2, baseAddr + loadlen);
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 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700586 if (readw(baseAddr + key) != keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700587 return -EIO;
588 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700589 writew(0, baseAddr + loadlen);
590 writew(usum, baseAddr + checksum);
591 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700592 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700593 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700594 break;
595 msleep(10);
596 }
597 retry++;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700598 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
599 if (readb(baseAddr + checksum_ok) != 1)
Jiri Slaby03718232008-04-30 00:53:39 -0700600 return -EIO;
601
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700602 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700603 for (i = 0; i < 600; i++) {
604 if (readw(baseAddr + Magic_no) == Magic_code)
605 break;
606 msleep(10);
607 }
608 if (readw(baseAddr + Magic_no) != Magic_code)
609 return -EIO;
610
Jiri Slaby08d01c792008-04-30 00:53:47 -0700611 if (MOXA_IS_320(brd)) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700612 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
613 writew(0x3800, baseAddr + TMS320_PORT1);
614 writew(0x3900, baseAddr + TMS320_PORT2);
615 writew(28499, baseAddr + TMS320_CLOCK);
616 } else {
617 writew(0x3200, baseAddr + TMS320_PORT1);
618 writew(0x3400, baseAddr + TMS320_PORT2);
619 writew(19999, baseAddr + TMS320_CLOCK);
620 }
Jiri Slaby03718232008-04-30 00:53:39 -0700621 }
622 writew(1, baseAddr + Disable_IRQ);
623 writew(0, baseAddr + Magic_no);
624 for (i = 0; i < 500; i++) {
625 if (readw(baseAddr + Magic_no) == Magic_code)
626 break;
627 msleep(10);
628 }
629 if (readw(baseAddr + Magic_no) != Magic_code)
630 return -EIO;
631
Jiri Slaby08d01c792008-04-30 00:53:47 -0700632 if (MOXA_IS_320(brd)) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700633 j = readw(baseAddr + Module_cnt);
634 if (j <= 0)
635 return -EIO;
636 brd->numPorts = j * 8;
637 writew(j, baseAddr + Module_no);
638 writew(0, baseAddr + Magic_no);
639 for (i = 0; i < 600; i++) {
640 if (readw(baseAddr + Magic_no) == Magic_code)
641 break;
642 msleep(10);
643 }
644 if (readw(baseAddr + Magic_no) != Magic_code)
645 return -EIO;
Jiri Slaby03718232008-04-30 00:53:39 -0700646 }
Jiri Slaby03718232008-04-30 00:53:39 -0700647 brd->intNdx = baseAddr + IRQindex;
648 brd->intPend = baseAddr + IRQpending;
649 brd->intTable = baseAddr + IRQtable;
650
651 return 0;
652}
653
654static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
655 size_t len)
656{
657 void __iomem *ofsAddr, *baseAddr = brd->basemem;
658 struct moxa_port *port;
659 int retval, i;
660
661 if (len % 2) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700662 printk(KERN_ERR "MOXA: bios length is not even\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700663 return -EINVAL;
664 }
665
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700666 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
667 if (retval)
668 return retval;
669
Jiri Slaby03718232008-04-30 00:53:39 -0700670 switch (brd->boardType) {
671 case MOXA_BOARD_C218_ISA:
672 case MOXA_BOARD_C218_PCI:
673 case MOXA_BOARD_CP204J:
Jiri Slaby03718232008-04-30 00:53:39 -0700674 port = brd->ports;
675 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700676 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700677 port->DCDState = 0;
678 port->tableAddr = baseAddr + Extern_table +
679 Extern_size * i;
680 ofsAddr = port->tableAddr;
681 writew(C218rx_mask, ofsAddr + RX_mask);
682 writew(C218tx_mask, ofsAddr + TX_mask);
683 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
684 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
685
686 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
687 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
688
689 }
690 break;
691 default:
Jiri Slaby03718232008-04-30 00:53:39 -0700692 port = brd->ports;
693 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700694 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700695 port->DCDState = 0;
696 port->tableAddr = baseAddr + Extern_table +
697 Extern_size * i;
698 ofsAddr = port->tableAddr;
699 switch (brd->numPorts) {
700 case 8:
701 writew(C320p8rx_mask, ofsAddr + RX_mask);
702 writew(C320p8tx_mask, ofsAddr + TX_mask);
703 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
704 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
705 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
706 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
707
708 break;
709 case 16:
710 writew(C320p16rx_mask, ofsAddr + RX_mask);
711 writew(C320p16tx_mask, ofsAddr + TX_mask);
712 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
713 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
714 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
715 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
716 break;
717
718 case 24:
719 writew(C320p24rx_mask, ofsAddr + RX_mask);
720 writew(C320p24tx_mask, ofsAddr + TX_mask);
721 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
722 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
723 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
724 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
725 break;
726 case 32:
727 writew(C320p32rx_mask, ofsAddr + RX_mask);
728 writew(C320p32tx_mask, ofsAddr + TX_mask);
729 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
730 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
731 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
732 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
733 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
734 break;
735 }
736 }
737 break;
738 }
Jiri Slaby03718232008-04-30 00:53:39 -0700739 return 0;
740}
741
742static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
743{
David Howells2bca76e2008-07-08 17:37:15 +0100744 const void *ptr = fw->data;
Jiri Slaby03718232008-04-30 00:53:39 -0700745 char rsn[64];
746 u16 lens[5];
747 size_t len;
748 unsigned int a, lenp, lencnt;
749 int ret = -EINVAL;
750 struct {
751 __le32 magic; /* 0x34303430 */
752 u8 reserved1[2];
753 u8 type; /* UNIX = 3 */
754 u8 model; /* C218T=1, C320T=2, CP204=3 */
755 u8 reserved2[8];
756 __le16 len[5];
David Howells2bca76e2008-07-08 17:37:15 +0100757 } const *hdr = ptr;
Jiri Slaby03718232008-04-30 00:53:39 -0700758
759 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
760
761 if (fw->size < MOXA_FW_HDRLEN) {
762 strcpy(rsn, "too short (even header won't fit)");
763 goto err;
764 }
765 if (hdr->magic != cpu_to_le32(0x30343034)) {
766 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
767 goto err;
768 }
769 if (hdr->type != 3) {
770 sprintf(rsn, "not for linux, type is %u", hdr->type);
771 goto err;
772 }
773 if (moxa_check_fw_model(brd, hdr->model)) {
774 sprintf(rsn, "not for this card, model is %u", hdr->model);
775 goto err;
776 }
777
778 len = MOXA_FW_HDRLEN;
779 lencnt = hdr->model == 2 ? 5 : 3;
780 for (a = 0; a < ARRAY_SIZE(lens); a++) {
781 lens[a] = le16_to_cpu(hdr->len[a]);
782 if (lens[a] && len + lens[a] <= fw->size &&
783 moxa_check_fw(&fw->data[len]))
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700784 printk(KERN_WARNING "MOXA firmware: unexpected input "
Jiri Slaby03718232008-04-30 00:53:39 -0700785 "at offset %u, but going on\n", (u32)len);
786 if (!lens[a] && a < lencnt) {
787 sprintf(rsn, "too few entries in fw file");
788 goto err;
789 }
790 len += lens[a];
791 }
792
793 if (len != fw->size) {
794 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
795 (u32)len);
796 goto err;
797 }
798
799 ptr += MOXA_FW_HDRLEN;
800 lenp = 0; /* bios */
801
802 strcpy(rsn, "read above");
803
804 ret = moxa_load_bios(brd, ptr, lens[lenp]);
805 if (ret)
806 goto err;
807
808 /* we skip the tty section (lens[1]), since we don't need it */
809 ptr += lens[lenp] + lens[lenp + 1];
810 lenp += 2; /* comm */
811
812 if (hdr->model == 2) {
813 ret = moxa_load_320b(brd, ptr, lens[lenp]);
814 if (ret)
815 goto err;
816 /* skip another tty */
817 ptr += lens[lenp] + lens[lenp + 1];
818 lenp += 2;
819 }
820
821 ret = moxa_load_code(brd, ptr, lens[lenp]);
822 if (ret)
823 goto err;
824
825 return 0;
826err:
827 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
828 return ret;
829}
830
831static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
832{
833 const struct firmware *fw;
834 const char *file;
Jiri Slaby810ab092008-04-30 00:53:41 -0700835 struct moxa_port *p;
836 unsigned int i;
Jiri Slaby03718232008-04-30 00:53:39 -0700837 int ret;
838
Jiri Slaby810ab092008-04-30 00:53:41 -0700839 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
840 GFP_KERNEL);
841 if (brd->ports == NULL) {
842 printk(KERN_ERR "cannot allocate memory for ports\n");
843 ret = -ENOMEM;
844 goto err;
845 }
846
847 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
Alan Cox9de6a512008-07-16 21:56:02 +0100848 tty_port_init(&p->port);
Alan Cox31f35932009-01-02 13:45:05 +0000849 p->port.ops = &moxa_port_ops;
Alan Cox44b7d1b2008-07-16 21:57:18 +0100850 p->type = PORT_16550A;
851 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Jiri Slaby810ab092008-04-30 00:53:41 -0700852 }
853
Jiri Slaby03718232008-04-30 00:53:39 -0700854 switch (brd->boardType) {
855 case MOXA_BOARD_C218_ISA:
856 case MOXA_BOARD_C218_PCI:
857 file = "c218tunx.cod";
858 break;
859 case MOXA_BOARD_CP204J:
860 file = "cp204unx.cod";
861 break;
862 default:
863 file = "c320tunx.cod";
864 break;
865 }
866
867 ret = request_firmware(&fw, file, dev);
868 if (ret) {
Jiri Slabyec09cd52008-04-30 00:53:49 -0700869 printk(KERN_ERR "MOXA: request_firmware failed. Make sure "
870 "you've placed '%s' file into your firmware "
871 "loader directory (e.g. /lib/firmware)\n",
872 file);
Jiri Slaby810ab092008-04-30 00:53:41 -0700873 goto err_free;
Jiri Slaby03718232008-04-30 00:53:39 -0700874 }
875
876 ret = moxa_load_fw(brd, fw);
877
878 release_firmware(fw);
Jiri Slaby810ab092008-04-30 00:53:41 -0700879
880 if (ret)
881 goto err_free;
882
Jiri Slaby2a541342008-04-30 00:53:45 -0700883 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700884 brd->ready = 1;
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700885 if (!timer_pending(&moxaTimer))
886 mod_timer(&moxaTimer, jiffies + HZ / 50);
Jiri Slaby2a541342008-04-30 00:53:45 -0700887 spin_unlock_bh(&moxa_lock);
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700888
Jiri Slaby810ab092008-04-30 00:53:41 -0700889 return 0;
890err_free:
891 kfree(brd->ports);
892err:
Jiri Slaby03718232008-04-30 00:53:39 -0700893 return ret;
894}
895
Jiri Slaby810ab092008-04-30 00:53:41 -0700896static void moxa_board_deinit(struct moxa_board_conf *brd)
897{
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700898 unsigned int a, opened;
899
900 mutex_lock(&moxa_openlock);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700901 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700902 brd->ready = 0;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700903 spin_unlock_bh(&moxa_lock);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700904
905 /* pci hot-un-plug support */
906 for (a = 0; a < brd->numPorts; a++)
Alan Coxd450b5a2008-10-13 10:39:58 +0100907 if (brd->ports[a].port.flags & ASYNC_INITIALIZED) {
908 struct tty_struct *tty = tty_port_tty_get(
909 &brd->ports[a].port);
910 if (tty) {
911 tty_hangup(tty);
912 tty_kref_put(tty);
913 }
914 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700915 while (1) {
916 opened = 0;
917 for (a = 0; a < brd->numPorts; a++)
Alan Cox9de6a512008-07-16 21:56:02 +0100918 if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700919 opened++;
920 mutex_unlock(&moxa_openlock);
921 if (!opened)
922 break;
923 msleep(50);
924 mutex_lock(&moxa_openlock);
925 }
926
Jiri Slaby810ab092008-04-30 00:53:41 -0700927 iounmap(brd->basemem);
928 brd->basemem = NULL;
929 kfree(brd->ports);
930}
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932#ifdef CONFIG_PCI
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800933static int __devinit moxa_pci_probe(struct pci_dev *pdev,
934 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800936 struct moxa_board_conf *board;
937 unsigned int i;
938 int board_type = ent->driver_data;
939 int retval;
940
941 retval = pci_enable_device(pdev);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700942 if (retval) {
943 dev_err(&pdev->dev, "can't enable pci device\n");
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800944 goto err;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700945 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800946
947 for (i = 0; i < MAX_BOARDS; i++)
948 if (moxa_boards[i].basemem == NULL)
949 break;
950
951 retval = -ENODEV;
952 if (i >= MAX_BOARDS) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700953 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800954 "found. Board is ignored.\n", MAX_BOARDS);
955 goto err;
956 }
957
958 board = &moxa_boards[i];
Jiri Slabye46a5e32008-04-30 00:53:37 -0700959
960 retval = pci_request_region(pdev, 2, "moxa-base");
961 if (retval) {
962 dev_err(&pdev->dev, "can't request pci region 2\n");
963 goto err;
964 }
965
Alan Cox24cb2332008-04-30 00:54:19 -0700966 board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700967 if (board->basemem == NULL) {
968 dev_err(&pdev->dev, "can't remap io space 2\n");
Jiri Slabye46a5e32008-04-30 00:53:37 -0700969 goto err_reg;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700970 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 board->boardType = board_type;
973 switch (board_type) {
974 case MOXA_BOARD_C218_ISA:
975 case MOXA_BOARD_C218_PCI:
976 board->numPorts = 8;
977 break;
978
979 case MOXA_BOARD_CP204J:
980 board->numPorts = 4;
981 break;
982 default:
983 board->numPorts = 0;
984 break;
985 }
986 board->busType = MOXA_BUS_TYPE_PCI;
Jiri Slabya784bf72007-02-10 01:45:36 -0800987
Jiri Slaby03718232008-04-30 00:53:39 -0700988 retval = moxa_init_board(board, &pdev->dev);
989 if (retval)
990 goto err_base;
991
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800992 pci_set_drvdata(pdev, board);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Jiri Slabybb9f9102008-04-30 00:53:48 -0700994 dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n",
995 moxa_brdname[board_type - 1], board->numPorts);
996
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700997 return 0;
Jiri Slaby03718232008-04-30 00:53:39 -0700998err_base:
999 iounmap(board->basemem);
1000 board->basemem = NULL;
Jiri Slabye46a5e32008-04-30 00:53:37 -07001001err_reg:
1002 pci_release_region(pdev, 2);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001003err:
1004 return retval;
1005}
1006
1007static void __devexit moxa_pci_remove(struct pci_dev *pdev)
1008{
1009 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
1010
Jiri Slaby810ab092008-04-30 00:53:41 -07001011 moxa_board_deinit(brd);
1012
Jiri Slabye46a5e32008-04-30 00:53:37 -07001013 pci_release_region(pdev, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
Jiri Slabya784bf72007-02-10 01:45:36 -08001015
1016static struct pci_driver moxa_pci_driver = {
1017 .name = "moxa",
1018 .id_table = moxa_pcibrds,
1019 .probe = moxa_pci_probe,
1020 .remove = __devexit_p(moxa_pci_remove)
1021};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022#endif /* CONFIG_PCI */
1023
1024static int __init moxa_init(void)
1025{
Jiri Slaby810ab092008-04-30 00:53:41 -07001026 unsigned int isabrds = 0;
Jiri Slabyd353eca2008-04-30 00:53:37 -07001027 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001029 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
1030 MOXA_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
1032 if (!moxaDriver)
1033 return -ENOMEM;
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 moxaDriver->owner = THIS_MODULE;
Sergey Vlasov9b4e3b12005-09-03 16:26:49 +01001036 moxaDriver->name = "ttyMX";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 moxaDriver->major = ttymajor;
1038 moxaDriver->minor_start = 0;
1039 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1040 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1041 moxaDriver->init_termios = tty_std_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Alan Cox606d0992006-12-08 02:38:45 -08001043 moxaDriver->init_termios.c_ispeed = 9600;
1044 moxaDriver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
1046 tty_set_operations(moxaDriver, &moxa_ops);
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (tty_register_driver(moxaDriver)) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001049 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 put_tty_driver(moxaDriver);
1051 return -1;
1052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Jiri Slabyd353eca2008-04-30 00:53:37 -07001054 /* Find the boards defined from module args. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -07001056 {
1057 struct moxa_board_conf *brd = moxa_boards;
Jiri Slaby810ab092008-04-30 00:53:41 -07001058 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 for (i = 0; i < MAX_BOARDS; i++) {
Jiri Slabyd353eca2008-04-30 00:53:37 -07001060 if (!baseaddr[i])
1061 break;
1062 if (type[i] == MOXA_BOARD_C218_ISA ||
1063 type[i] == MOXA_BOARD_C320_ISA) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001064 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001065 isabrds + 1, moxa_brdname[type[i] - 1],
1066 baseaddr[i]);
1067 brd->boardType = type[i];
1068 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1069 numports[i];
1070 brd->busType = MOXA_BUS_TYPE_ISA;
Alan Cox24cb2332008-04-30 00:54:19 -07001071 brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
Jiri Slabyd353eca2008-04-30 00:53:37 -07001072 if (!brd->basemem) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001073 printk(KERN_ERR "MOXA: can't remap %lx\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001074 baseaddr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 continue;
1076 }
Jiri Slaby03718232008-04-30 00:53:39 -07001077 if (moxa_init_board(brd, NULL)) {
1078 iounmap(brd->basemem);
1079 brd->basemem = NULL;
1080 continue;
1081 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001082
Jiri Slabybb9f9102008-04-30 00:53:48 -07001083 printk(KERN_INFO "MOXA isa board found at 0x%.8lu and "
1084 "ready (%u ports, firmware loaded)\n",
1085 baseaddr[i], brd->numPorts);
1086
Jiri Slabyd353eca2008-04-30 00:53:37 -07001087 brd++;
1088 isabrds++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001095 retval = pci_register_driver(&moxa_pci_driver);
1096 if (retval) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001097 printk(KERN_ERR "Can't register MOXA pci driver!\n");
Jiri Slabyd353eca2008-04-30 00:53:37 -07001098 if (isabrds)
Jiri Slabya784bf72007-02-10 01:45:36 -08001099 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
1101#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001102
Jiri Slabya784bf72007-02-10 01:45:36 -08001103 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
1106static void __exit moxa_exit(void)
1107{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001108 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001110#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001111 pci_unregister_driver(&moxa_pci_driver);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001112#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001113
Jiri Slaby810ab092008-04-30 00:53:41 -07001114 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1115 if (moxa_boards[i].ready)
1116 moxa_board_deinit(&moxa_boards[i]);
Jiri Slaby2a541342008-04-30 00:53:45 -07001117
1118 del_timer_sync(&moxaTimer);
1119
1120 if (tty_unregister_driver(moxaDriver))
1121 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1122 "serial driver\n");
1123 put_tty_driver(moxaDriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
1125
1126module_init(moxa_init);
1127module_exit(moxa_exit);
1128
Alan Coxf1761782009-11-30 13:17:51 +00001129static void moxa_shutdown(struct tty_port *port)
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001130{
Alan Coxf1761782009-11-30 13:17:51 +00001131 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1132 MoxaPortDisable(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001133 MoxaPortFlushData(ch, 2);
Alan Coxf1761782009-11-30 13:17:51 +00001134 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
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;
1160 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Jiri Slaby11324ed2007-02-10 01:45:31 -08001162 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if (port == MAX_PORTS) {
Jiri Slaby74d7d972008-04-30 00:53:43 -07001164 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001166 if (mutex_lock_interruptible(&moxa_openlock))
1167 return -ERESTARTSYS;
Jiri Slaby810ab092008-04-30 00:53:41 -07001168 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001169 if (!brd->ready) {
1170 mutex_unlock(&moxa_openlock);
Jiri Slaby810ab092008-04-30 00:53:41 -07001171 return -ENODEV;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Dirk Eibachf0e85272009-06-11 14:56:44 +01001174 if (port % MAX_PORTS_PER_BOARD >= brd->numPorts) {
1175 mutex_unlock(&moxa_openlock);
1176 return -ENODEV;
1177 }
1178
Jiri Slaby810ab092008-04-30 00:53:41 -07001179 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
Alan Cox9de6a512008-07-16 21:56:02 +01001180 ch->port.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 tty->driver_data = ch;
Alan Coxd450b5a2008-10-13 10:39:58 +01001182 tty_port_tty_set(&ch->port, tty);
Alan Coxf1761782009-11-30 13:17:51 +00001183 mutex_lock(&ch->port.mutex);
Alan Cox9de6a512008-07-16 21:56:02 +01001184 if (!(ch->port.flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 ch->statusflags = 0;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001186 moxa_set_tty_param(tty, tty->termios);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001187 MoxaPortLineCtrl(ch, 1, 1);
1188 MoxaPortEnable(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001189 MoxaSetFifo(ch, ch->type == PORT_16550A);
Alan Cox9de6a512008-07-16 21:56:02 +01001190 ch->port.flags |= ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
Alan Coxf1761782009-11-30 13:17:51 +00001192 mutex_unlock(&ch->port.mutex);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001193 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Alan Coxf1761782009-11-30 13:17:51 +00001195 retval = tty_port_block_til_ready(&ch->port, tty, filp);
1196 if (retval == 0)
1197 set_bit(ASYNCB_NORMAL_ACTIVE, &ch->port.flags);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001198 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
1201static void moxa_close(struct tty_struct *tty, struct file *filp)
1202{
Alan Coxf1761782009-11-30 13:17:51 +00001203 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 ch->cflag = tty->termios->c_cflag;
Alan Coxf1761782009-11-30 13:17:51 +00001205 tty_port_close(&ch->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
1207
1208static int moxa_write(struct tty_struct *tty,
1209 const unsigned char *buf, int count)
1210{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001211 struct moxa_port *ch = tty->driver_data;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001212 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (ch == NULL)
Jiri Slabyb4173f42008-04-30 00:53:40 -07001215 return 0;
Alan Cox33f0f882006-01-09 20:54:13 -08001216
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001217 spin_lock_bh(&moxa_lock);
Alan Coxd450b5a2008-10-13 10:39:58 +01001218 len = MoxaPortWriteData(tty, buf, count);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001219 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Alan Coxa808ac02009-11-30 13:18:02 +00001221 set_bit(LOWWAIT, &ch->statusflags);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001222 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
1225static int moxa_write_room(struct tty_struct *tty)
1226{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001227 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 if (tty->stopped)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001230 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001231 ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (ch == NULL)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001233 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001234 return MoxaPortTxFree(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
1237static void moxa_flush_buffer(struct tty_struct *tty)
1238{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001239 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 if (ch == NULL)
1242 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001243 MoxaPortFlushData(ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 tty_wakeup(tty);
1245}
1246
1247static int moxa_chars_in_buffer(struct tty_struct *tty)
1248{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001249 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Jiri Slabyb4173f42008-04-30 00:53:40 -07001252 chars = MoxaPortTxQueue(ch);
Alan Coxf710ebd2009-11-30 13:18:18 +00001253 if (chars)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 /*
1255 * Make it possible to wakeup anything waiting for output
1256 * in tty_ioctl.c, etc.
1257 */
Alan Coxf710ebd2009-11-30 13:18:18 +00001258 set_bit(EMPTYWAIT, &ch->statusflags);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001259 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1263{
Alan Cox8482bcd2009-11-30 13:18:13 +00001264 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 int flag = 0, dtr, rts;
1266
Jiri Slabyb4173f42008-04-30 00:53:40 -07001267 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (dtr)
1269 flag |= TIOCM_DTR;
1270 if (rts)
1271 flag |= TIOCM_RTS;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001272 dtr = MoxaPortLineStatus(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (dtr & 1)
1274 flag |= TIOCM_CTS;
1275 if (dtr & 2)
1276 flag |= TIOCM_DSR;
1277 if (dtr & 4)
1278 flag |= TIOCM_CD;
1279 return flag;
1280}
1281
1282static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1283 unsigned int set, unsigned int clear)
1284{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001285 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 int port;
1287 int dtr, rts;
1288
Jiri Slaby11324ed2007-02-10 01:45:31 -08001289 port = tty->index;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001290 mutex_lock(&moxa_openlock);
1291 ch = tty->driver_data;
1292 if (!ch) {
1293 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -07001294 return -EINVAL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Jiri Slabyb4173f42008-04-30 00:53:40 -07001297 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if (set & TIOCM_RTS)
1299 rts = 1;
1300 if (set & TIOCM_DTR)
1301 dtr = 1;
1302 if (clear & TIOCM_RTS)
1303 rts = 0;
1304 if (clear & TIOCM_DTR)
1305 dtr = 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001306 MoxaPortLineCtrl(ch, dtr, rts);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001307 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return 0;
1309}
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311static void moxa_set_termios(struct tty_struct *tty,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001312 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001314 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 if (ch == NULL)
1317 return;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001318 moxa_set_tty_param(tty, old_termios);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001319 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
Alan Cox9de6a512008-07-16 21:56:02 +01001320 wake_up_interruptible(&ch->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
1322
1323static void moxa_stop(struct tty_struct *tty)
1324{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001325 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 if (ch == NULL)
1328 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001329 MoxaPortTxDisable(ch);
Alan Coxa808ac02009-11-30 13:18:02 +00001330 set_bit(TXSTOPPED, &ch->statusflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
1332
1333
1334static void moxa_start(struct tty_struct *tty)
1335{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001336 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 if (ch == NULL)
1339 return;
1340
1341 if (!(ch->statusflags & TXSTOPPED))
1342 return;
1343
Jiri Slabyb4173f42008-04-30 00:53:40 -07001344 MoxaPortTxEnable(ch);
Alan Coxa808ac02009-11-30 13:18:02 +00001345 clear_bit(TXSTOPPED, &ch->statusflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
1347
1348static void moxa_hangup(struct tty_struct *tty)
1349{
Alan Coxa808ac02009-11-30 13:18:02 +00001350 struct moxa_port *ch = tty->driver_data;
Alan Coxf1761782009-11-30 13:17:51 +00001351 tty_port_hangup(&ch->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352}
1353
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001354static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1355{
Alan Coxd450b5a2008-10-13 10:39:58 +01001356 struct tty_struct *tty;
Alan Cox8482bcd2009-11-30 13:18:13 +00001357 unsigned long flags;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001358 dcd = !!dcd;
1359
Alan Cox8482bcd2009-11-30 13:18:13 +00001360 spin_lock_irqsave(&p->port.lock, flags);
Alan Coxd450b5a2008-10-13 10:39:58 +01001361 if (dcd != p->DCDState) {
Alan Cox8482bcd2009-11-30 13:18:13 +00001362 p->DCDState = dcd;
1363 spin_unlock_irqrestore(&p->port.lock, flags);
Alan Coxd450b5a2008-10-13 10:39:58 +01001364 tty = tty_port_tty_get(&p->port);
1365 if (tty && C_CLOCAL(tty) && !dcd)
1366 tty_hangup(tty);
1367 tty_kref_put(tty);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001368 }
Alan Cox8482bcd2009-11-30 13:18:13 +00001369 else
1370 spin_unlock_irqrestore(&p->port.lock, flags);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001371}
1372
1373static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1374 u16 __iomem *ip)
1375{
Alan Coxd450b5a2008-10-13 10:39:58 +01001376 struct tty_struct *tty = tty_port_tty_get(&p->port);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001377 void __iomem *ofsAddr;
Alan Cox9de6a512008-07-16 21:56:02 +01001378 unsigned int inited = p->port.flags & ASYNC_INITIALIZED;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001379 u16 intr;
1380
1381 if (tty) {
Alan Coxa808ac02009-11-30 13:18:02 +00001382 if (test_bit(EMPTYWAIT, &p->statusflags) &&
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001383 MoxaPortTxQueue(p) == 0) {
Alan Coxa808ac02009-11-30 13:18:02 +00001384 clear_bit(EMPTYWAIT, &p->statusflags);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001385 tty_wakeup(tty);
1386 }
Alan Coxa808ac02009-11-30 13:18:02 +00001387 if (test_bit(LOWWAIT, &p->statusflags) && !tty->stopped &&
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001388 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
Alan Coxa808ac02009-11-30 13:18:02 +00001389 clear_bit(LOWWAIT, &p->statusflags);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001390 tty_wakeup(tty);
1391 }
1392
Alan Coxf9b412a2009-11-30 13:18:08 +00001393 if (inited && !test_bit(TTY_THROTTLED, &tty->flags) &&
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001394 MoxaPortRxQueue(p) > 0) { /* RX */
1395 MoxaPortReadData(p);
1396 tty_schedule_flip(tty);
1397 }
1398 } else {
Alan Coxa808ac02009-11-30 13:18:02 +00001399 clear_bit(EMPTYWAIT, &p->statusflags);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001400 MoxaPortFlushData(p, 0); /* flush RX */
1401 }
1402
1403 if (!handle) /* nothing else to do */
Jiri Slaby0e0fd7d2009-04-06 17:34:04 +01001404 goto put;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001405
1406 intr = readw(ip); /* port irq status */
1407 if (intr == 0)
Jiri Slaby0e0fd7d2009-04-06 17:34:04 +01001408 goto put;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001409
1410 writew(0, ip); /* ACK port */
1411 ofsAddr = p->tableAddr;
1412 if (intr & IntrTx) /* disable tx intr */
1413 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1414 ofsAddr + HostStat);
1415
1416 if (!inited)
Jiri Slaby0e0fd7d2009-04-06 17:34:04 +01001417 goto put;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001418
1419 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1420 tty_insert_flip_char(tty, 0, TTY_BREAK);
1421 tty_schedule_flip(tty);
1422 }
1423
1424 if (intr & IntrLine)
1425 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
Jiri Slaby0e0fd7d2009-04-06 17:34:04 +01001426put:
1427 tty_kref_put(tty);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001428
1429 return 0;
1430}
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432static void moxa_poll(unsigned long ignored)
1433{
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001434 struct moxa_board_conf *brd;
1435 u16 __iomem *ip;
Jiri Slaby2a541342008-04-30 00:53:45 -07001436 unsigned int card, port, served = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001438 spin_lock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 for (card = 0; card < MAX_BOARDS; card++) {
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001440 brd = &moxa_boards[card];
1441 if (!brd->ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 continue;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001443
Jiri Slaby2a541342008-04-30 00:53:45 -07001444 served++;
1445
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001446 ip = NULL;
1447 if (readb(brd->intPend) == 0xff)
1448 ip = brd->intTable + readb(brd->intNdx);
1449
1450 for (port = 0; port < brd->numPorts; port++)
1451 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1452
1453 if (ip)
1454 writeb(0, brd->intPend); /* ACK */
1455
1456 if (moxaLowWaterChk) {
1457 struct moxa_port *p = brd->ports;
1458 for (port = 0; port < brd->numPorts; port++, p++)
1459 if (p->lowChkFlag) {
1460 p->lowChkFlag = 0;
1461 moxa_low_water_check(p->tableAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
1464 }
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001465 moxaLowWaterChk = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Jiri Slaby2a541342008-04-30 00:53:45 -07001467 if (served)
1468 mod_timer(&moxaTimer, jiffies + HZ / 50);
1469 spin_unlock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
1471
1472/******************************************************************************/
1473
Alan Coxdb1acaa2008-02-08 04:18:43 -08001474static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001476 register struct ktermios *ts = tty->termios;
1477 struct moxa_port *ch = tty->driver_data;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001478 int rts, cts, txflow, rxflow, xany, baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 rts = cts = txflow = rxflow = xany = 0;
1481 if (ts->c_cflag & CRTSCTS)
1482 rts = cts = 1;
1483 if (ts->c_iflag & IXON)
1484 txflow = 1;
1485 if (ts->c_iflag & IXOFF)
1486 rxflow = 1;
1487 if (ts->c_iflag & IXANY)
1488 xany = 1;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001489
1490 /* Clear the features we don't support */
1491 ts->c_cflag &= ~CMSPAR;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001492 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1493 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
Alan Coxdb1acaa2008-02-08 04:18:43 -08001494 if (baud == -1)
1495 baud = tty_termios_baud_rate(old_termios);
1496 /* Not put the baud rate into the termios data */
1497 tty_encode_baud_rate(tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500/*****************************************************************************
1501 * Driver level functions: *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Jiri Slabyb4173f42008-04-30 00:53:40 -07001504static void MoxaPortFlushData(struct moxa_port *port, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
1506 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001507 if (mode < 0 || mode > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001509 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 moxafunc(ofsAddr, FC_FlushQueue, mode);
1511 if (mode != 1) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001512 port->lowChkFlag = 0;
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001513 moxa_low_water_check(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 }
1515}
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517/*
1518 * Moxa Port Number Description:
1519 *
1520 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1521 * the port number using in MOXA driver functions will be 0 to 31 for
1522 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1523 * to 127 for fourth. For example, if you setup three MOXA boards,
1524 * first board is C218, second board is C320-16 and third board is
1525 * C320-32. The port number of first board (C218 - 8 ports) is from
1526 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1527 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1528 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1529 * 127 will be invalid.
1530 *
1531 *
1532 * Moxa Functions Description:
1533 *
1534 * Function 1: Driver initialization routine, this routine must be
1535 * called when initialized driver.
1536 * Syntax:
1537 * void MoxaDriverInit();
1538 *
1539 *
1540 * Function 2: Moxa driver private IOCTL command processing.
1541 * Syntax:
1542 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1543 *
1544 * unsigned int cmd : IOCTL command
1545 * unsigned long arg : IOCTL argument
1546 * int port : port number (0 - 127)
1547 *
1548 * return: 0 (OK)
1549 * -EINVAL
1550 * -ENOIOCTLCMD
1551 *
1552 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 * Function 6: Enable this port to start Tx/Rx data.
1554 * Syntax:
1555 * void MoxaPortEnable(int port);
1556 * int port : port number (0 - 127)
1557 *
1558 *
1559 * Function 7: Disable this port
1560 * Syntax:
1561 * void MoxaPortDisable(int port);
1562 * int port : port number (0 - 127)
1563 *
1564 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 * Function 10: Setting baud rate of this port.
1566 * Syntax:
Jiri Slaby08d01c792008-04-30 00:53:47 -07001567 * speed_t MoxaPortSetBaud(int port, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 * int port : port number (0 - 127)
1569 * long baud : baud rate (50 - 115200)
1570 *
1571 * return: 0 : this port is invalid or baud < 50
1572 * 50 - 115200 : the real baud rate set to the port, if
1573 * the argument baud is large than maximun
1574 * available baud rate, the real setting
1575 * baud rate will be the maximun baud rate.
1576 *
1577 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 * Function 12: Configure the port.
1579 * Syntax:
Alan Cox606d0992006-12-08 02:38:45 -08001580 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 * int port : port number (0 - 127)
Alan Cox606d0992006-12-08 02:38:45 -08001582 * struct ktermios * termio : termio structure pointer
Alan Coxc7bce302006-09-30 23:27:24 -07001583 * speed_t baud : baud rate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 *
1585 * return: -1 : this port is invalid or termio == NULL
1586 * 0 : setting O.K.
1587 *
1588 *
1589 * Function 13: Get the DTR/RTS state of this port.
1590 * Syntax:
1591 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1592 * int port : port number (0 - 127)
1593 * int * dtrState : pointer to INT to receive the current DTR
1594 * state. (if NULL, this function will not
1595 * write to this address)
1596 * int * rtsState : pointer to INT to receive the current RTS
1597 * state. (if NULL, this function will not
1598 * write to this address)
1599 *
1600 * return: -1 : this port is invalid
1601 * 0 : O.K.
1602 *
1603 *
1604 * Function 14: Setting the DTR/RTS output state of this port.
1605 * Syntax:
1606 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1607 * int port : port number (0 - 127)
1608 * int dtrState : DTR output state (0: off, 1: on)
1609 * int rtsState : RTS output state (0: off, 1: on)
1610 *
1611 *
1612 * Function 15: Setting the flow control of this port.
1613 * Syntax:
1614 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1615 * int txFlow,int xany);
1616 * int port : port number (0 - 127)
1617 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1618 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1619 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1620 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1621 * int xany : S/W XANY flow control (0: no, 1: yes)
1622 *
1623 *
1624 * Function 16: Get ths line status of this port
1625 * Syntax:
1626 * int MoxaPortLineStatus(int port);
1627 * int port : port number (0 - 127)
1628 *
1629 * return: Bit 0 - CTS state (0: off, 1: on)
1630 * Bit 1 - DSR state (0: off, 1: on)
1631 * Bit 2 - DCD state (0: off, 1: on)
1632 *
1633 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 * Function 19: Flush the Rx/Tx buffer data of this port.
1635 * Syntax:
1636 * void MoxaPortFlushData(int port, int mode);
1637 * int port : port number (0 - 127)
1638 * int mode
1639 * 0 : flush the Rx buffer
1640 * 1 : flush the Tx buffer
1641 * 2 : flush the Rx and Tx buffer
1642 *
1643 *
1644 * Function 20: Write data.
1645 * Syntax:
1646 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1647 * int port : port number (0 - 127)
1648 * unsigned char * buffer : pointer to write data buffer.
1649 * int length : write data length
1650 *
1651 * return: 0 - length : real write data length
1652 *
1653 *
1654 * Function 21: Read data.
1655 * Syntax:
Alan Cox33f0f882006-01-09 20:54:13 -08001656 * int MoxaPortReadData(int port, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 * int port : port number (0 - 127)
Alan Cox33f0f882006-01-09 20:54:13 -08001658 * struct tty_struct *tty : tty for data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 *
1660 * return: 0 - length : real read data length
1661 *
1662 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 * Function 24: Get the Tx buffer current queued data bytes
1664 * Syntax:
1665 * int MoxaPortTxQueue(int port);
1666 * int port : port number (0 - 127)
1667 *
1668 * return: .. : Tx buffer current queued data bytes
1669 *
1670 *
1671 * Function 25: Get the Tx buffer current free space
1672 * Syntax:
1673 * int MoxaPortTxFree(int port);
1674 * int port : port number (0 - 127)
1675 *
1676 * return: .. : Tx buffer current free space
1677 *
1678 *
1679 * Function 26: Get the Rx buffer current queued data bytes
1680 * Syntax:
1681 * int MoxaPortRxQueue(int port);
1682 * int port : port number (0 - 127)
1683 *
1684 * return: .. : Rx buffer current queued data bytes
1685 *
1686 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 * Function 28: Disable port data transmission.
1688 * Syntax:
1689 * void MoxaPortTxDisable(int port);
1690 * int port : port number (0 - 127)
1691 *
1692 *
1693 * Function 29: Enable port data transmission.
1694 * Syntax:
1695 * void MoxaPortTxEnable(int port);
1696 * int port : port number (0 - 127)
1697 *
1698 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 * Function 31: Get the received BREAK signal count and reset it.
1700 * Syntax:
1701 * int MoxaPortResetBrkCnt(int port);
1702 * int port : port number (0 - 127)
1703 *
1704 * return: 0 - .. : BREAK signal count
1705 *
1706 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Jiri Slabyb4173f42008-04-30 00:53:40 -07001709static void MoxaPortEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
1711 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001712 u16 lowwater = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Jiri Slabyb4173f42008-04-30 00:53:40 -07001714 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 writew(lowwater, ofsAddr + Low_water);
Jiri Slaby08d01c792008-04-30 00:53:47 -07001716 if (MOXA_IS_320(port->board))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001718 else
1719 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1720 ofsAddr + HostStat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1723 moxafunc(ofsAddr, FC_FlushQueue, 2);
1724
1725 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1726 MoxaPortLineStatus(port);
1727}
1728
Jiri Slabyb4173f42008-04-30 00:53:40 -07001729static void MoxaPortDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001731 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1734 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1735 writew(0, ofsAddr + HostStat);
1736 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1737}
1738
Jiri Slaby08d01c792008-04-30 00:53:47 -07001739static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
Jiri Slaby08d01c792008-04-30 00:53:47 -07001741 void __iomem *ofsAddr = port->tableAddr;
1742 unsigned int clock, val;
1743 speed_t max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Jiri Slaby08d01c792008-04-30 00:53:47 -07001745 max = MOXA_IS_320(port->board) ? 460800 : 921600;
1746 if (baud < 50)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001747 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 if (baud > max)
1749 baud = max;
Jiri Slaby08d01c792008-04-30 00:53:47 -07001750 clock = 921600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 val = clock / baud;
1752 moxafunc(ofsAddr, FC_SetBaud, val);
1753 baud = clock / val;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001754 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755}
1756
Jiri Slabyb4173f42008-04-30 00:53:40 -07001757static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1758 speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
1760 void __iomem *ofsAddr;
1761 tcflag_t cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 tcflag_t mode = 0;
1763
Jiri Slabyb4173f42008-04-30 00:53:40 -07001764 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 cflag = termio->c_cflag; /* termio->c_cflag */
1766
1767 mode = termio->c_cflag & CSIZE;
1768 if (mode == CS5)
1769 mode = MX_CS5;
1770 else if (mode == CS6)
1771 mode = MX_CS6;
1772 else if (mode == CS7)
1773 mode = MX_CS7;
1774 else if (mode == CS8)
1775 mode = MX_CS8;
1776
1777 if (termio->c_cflag & CSTOPB) {
1778 if (mode == MX_CS5)
1779 mode |= MX_STOP15;
1780 else
1781 mode |= MX_STOP2;
1782 } else
1783 mode |= MX_STOP1;
1784
1785 if (termio->c_cflag & PARENB) {
1786 if (termio->c_cflag & PARODD)
1787 mode |= MX_PARODD;
1788 else
1789 mode |= MX_PAREVEN;
1790 } else
1791 mode |= MX_PARNONE;
1792
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001793 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Jiri Slaby08d01c792008-04-30 00:53:47 -07001795 if (MOXA_IS_320(port->board) && baud >= 921600)
1796 return -1;
1797
Alan Coxdb1acaa2008-02-08 04:18:43 -08001798 baud = MoxaPortSetBaud(port, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
Alan Coxf5c5a362009-11-30 13:17:57 +00001801 spin_lock_irq(&moxafunc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1803 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1804 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001805 moxa_wait_finish(ofsAddr);
Alan Coxa808ac02009-11-30 13:18:02 +00001806 spin_unlock_irq(&moxafunc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 }
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001809 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810}
1811
Jiri Slabyb4173f42008-04-30 00:53:40 -07001812static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1813 int *rtsState)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001815 if (dtrState)
1816 *dtrState = !!(port->lineCtrl & DTR_ON);
1817 if (rtsState)
1818 *rtsState = !!(port->lineCtrl & RTS_ON);
1819
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001820 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821}
1822
Jiri Slabyb4173f42008-04-30 00:53:40 -07001823static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001825 u8 mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 if (dtr)
1828 mode |= DTR_ON;
1829 if (rts)
1830 mode |= RTS_ON;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001831 port->lineCtrl = mode;
1832 moxafunc(port->tableAddr, FC_LineControl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
Jiri Slabyb4173f42008-04-30 00:53:40 -07001835static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1836 int txflow, int rxflow, int txany)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001838 int mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 if (rts)
1841 mode |= RTS_FlowCtl;
1842 if (cts)
1843 mode |= CTS_FlowCtl;
1844 if (txflow)
1845 mode |= Tx_FlowCtl;
1846 if (rxflow)
1847 mode |= Rx_FlowCtl;
1848 if (txany)
1849 mode |= IXM_IXANY;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001850 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
Jiri Slabyb4173f42008-04-30 00:53:40 -07001853static int MoxaPortLineStatus(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
1855 void __iomem *ofsAddr;
1856 int val;
1857
Jiri Slabyb4173f42008-04-30 00:53:40 -07001858 ofsAddr = port->tableAddr;
Alan Coxf5c5a362009-11-30 13:17:57 +00001859 if (MOXA_IS_320(port->board))
1860 val = moxafuncret(ofsAddr, FC_LineStatus, 0);
1861 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 val = readw(ofsAddr + FlagStat) >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 val &= 0x0B;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001864 if (val & 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 val |= 4;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001866 moxa_new_dcdstate(port, val & 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 val &= 7;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001868 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869}
1870
Alan Coxd450b5a2008-10-13 10:39:58 +01001871static int MoxaPortWriteData(struct tty_struct *tty,
Jiri Slaby2108eba2008-04-30 00:53:44 -07001872 const unsigned char *buffer, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Alan Coxd450b5a2008-10-13 10:39:58 +01001874 struct moxa_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001876 unsigned int c, total;
1877 u16 head, tail, tx_mask, spage, epage;
1878 u16 pageno, pageofs, bufhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Jiri Slabyb4173f42008-04-30 00:53:40 -07001880 ofsAddr = port->tableAddr;
1881 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 tx_mask = readw(ofsAddr + TX_mask);
1883 spage = readw(ofsAddr + Page_txb);
1884 epage = readw(ofsAddr + EndPage_txb);
1885 tail = readw(ofsAddr + TXwptr);
1886 head = readw(ofsAddr + TXrptr);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001887 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if (c > len)
1889 c = len;
Alan Cox9de6a512008-07-16 21:56:02 +01001890 moxaLog.txcnt[port->port.tty->index] += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 total = c;
1892 if (spage == epage) {
1893 bufhead = readw(ofsAddr + Ofs_txb);
1894 writew(spage, baseAddr + Control_reg);
1895 while (c > 0) {
1896 if (head > tail)
1897 len = head - tail - 1;
1898 else
1899 len = tx_mask + 1 - tail;
1900 len = (c > len) ? len : c;
1901 ofs = baseAddr + DynPage_addr + bufhead + tail;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001902 memcpy_toio(ofs, buffer, len);
1903 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 tail = (tail + len) & tx_mask;
1905 c -= len;
1906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 pageno = spage + (tail >> 13);
1909 pageofs = tail & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001910 while (c > 0) {
1911 len = Page_size - pageofs;
1912 if (len > c)
1913 len = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 writeb(pageno, baseAddr + Control_reg);
1915 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001916 memcpy_toio(ofs, buffer, len);
1917 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 if (++pageno == epage)
1919 pageno = spage;
1920 pageofs = 0;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001921 c -= len;
1922 }
1923 tail = (tail + total) & tx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07001925 writew(tail, ofsAddr + TXwptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 writeb(1, ofsAddr + CD180TXirq); /* start to send */
Jiri Slaby2108eba2008-04-30 00:53:44 -07001927 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928}
1929
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001930static int MoxaPortReadData(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
Alan Cox9de6a512008-07-16 21:56:02 +01001932 struct tty_struct *tty = port->port.tty;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001933 unsigned char *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001935 unsigned int count, len, total;
1936 u16 tail, rx_mask, spage, epage;
1937 u16 pageno, pageofs, bufhead, head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Jiri Slabyb4173f42008-04-30 00:53:40 -07001939 ofsAddr = port->tableAddr;
1940 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 head = readw(ofsAddr + RXrptr);
1942 tail = readw(ofsAddr + RXwptr);
1943 rx_mask = readw(ofsAddr + RX_mask);
1944 spage = readw(ofsAddr + Page_rxb);
1945 epage = readw(ofsAddr + EndPage_rxb);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001946 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 if (count == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08001948 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Alan Cox33f0f882006-01-09 20:54:13 -08001950 total = count;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001951 moxaLog.rxcnt[tty->index] += total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (spage == epage) {
1953 bufhead = readw(ofsAddr + Ofs_rxb);
1954 writew(spage, baseAddr + Control_reg);
1955 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 ofs = baseAddr + DynPage_addr + bufhead + head;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001957 len = (tail >= head) ? (tail - head) :
1958 (rx_mask + 1 - head);
1959 len = tty_prepare_flip_string(tty, &dst,
1960 min(len, count));
1961 memcpy_fromio(dst, ofs, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 head = (head + len) & rx_mask;
1963 count -= len;
1964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 pageno = spage + (head >> 13);
1967 pageofs = head & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001968 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 writew(pageno, baseAddr + Control_reg);
1970 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001971 len = tty_prepare_flip_string(tty, &dst,
1972 min(Page_size - pageofs, count));
1973 memcpy_fromio(dst, ofs, len);
1974
1975 count -= len;
1976 pageofs = (pageofs + len) & Page_mask;
1977 if (pageofs == 0 && ++pageno == epage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 pageno = spage;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001979 }
1980 head = (head + total) & rx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07001982 writew(head, ofsAddr + RXrptr);
1983 if (readb(ofsAddr + FlagStat) & Xoff_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 moxaLowWaterChk = 1;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001985 port->lowChkFlag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07001987 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988}
1989
1990
Jiri Slabyb4173f42008-04-30 00:53:40 -07001991static int MoxaPortTxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001993 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001994 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 rptr = readw(ofsAddr + TXrptr);
1997 wptr = readw(ofsAddr + TXwptr);
1998 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001999 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000}
2001
Jiri Slabyb4173f42008-04-30 00:53:40 -07002002static int MoxaPortTxFree(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002004 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002005 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 rptr = readw(ofsAddr + TXrptr);
2008 wptr = readw(ofsAddr + TXwptr);
2009 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002010 return mask - ((wptr - rptr) & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
2012
Jiri Slabyb4173f42008-04-30 00:53:40 -07002013static int MoxaPortRxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002015 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002016 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 rptr = readw(ofsAddr + RXrptr);
2019 wptr = readw(ofsAddr + RXwptr);
2020 mask = readw(ofsAddr + RX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002021 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022}
2023
Jiri Slabyb4173f42008-04-30 00:53:40 -07002024static void MoxaPortTxDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002026 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027}
2028
Jiri Slabyb4173f42008-04-30 00:53:40 -07002029static void MoxaPortTxEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002031 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002034static int moxa_get_serial_info(struct moxa_port *info,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002035 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002037 struct serial_struct tmp = {
2038 .type = info->type,
Alan Cox9de6a512008-07-16 21:56:02 +01002039 .line = info->port.tty->index,
2040 .flags = info->port.flags,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002041 .baud_base = 921600,
Alan Cox44b7d1b2008-07-16 21:57:18 +01002042 .close_delay = info->port.close_delay
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002043 };
2044 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045}
2046
2047
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002048static int moxa_set_serial_info(struct moxa_port *info,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002049 struct serial_struct __user *new_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
2051 struct serial_struct new_serial;
2052
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002053 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 return -EFAULT;
2055
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002056 if (new_serial.irq != 0 || new_serial.port != 0 ||
2057 new_serial.custom_divisor != 0 ||
2058 new_serial.baud_base != 921600)
2059 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 if (!capable(CAP_SYS_ADMIN)) {
2062 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
Alan Cox9de6a512008-07-16 21:56:02 +01002063 (info->port.flags & ~ASYNC_USR_MASK)))
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002064 return -EPERM;
2065 } else
Alan Cox44b7d1b2008-07-16 21:57:18 +01002066 info->port.close_delay = new_serial.close_delay * HZ / 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
2068 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
Alan Cox9de6a512008-07-16 21:56:02 +01002069 new_serial.flags |= (info->port.flags & ASYNC_FLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002071 MoxaSetFifo(info, new_serial.type == PORT_16550A);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 info->type = new_serial.type;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002074 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075}
2076
2077
2078
2079/*****************************************************************************
2080 * Static local functions: *
2081 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Jiri Slabyb4173f42008-04-30 00:53:40 -07002083static void MoxaSetFifo(struct moxa_port *port, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002085 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 if (!enable) {
2088 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2089 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2090 } else {
2091 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2092 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2093 }
2094}