blob: 7f8773523fbf11876237fcd97fe0502603ba3b4a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*****************************************************************************/
2/*
3 * moxa.c -- MOXA Intellio family multiport serial driver.
4 *
5 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com.tw).
6 *
7 * This code is loosely based on the Linux serial driver, written by
8 * Linus Torvalds, Theodore T'so and others.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
16/*
17 * MOXA Intellio Series Driver
18 * for : LINUX
19 * date : 1999/1/7
20 * version : 5.1
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
24#include <linux/types.h>
25#include <linux/mm.h>
26#include <linux/ioport.h>
27#include <linux/errno.h>
Jiri Slaby03718232008-04-30 00:53:39 -070028#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/signal.h>
30#include <linux/sched.h>
31#include <linux/timer.h>
32#include <linux/interrupt.h>
33#include <linux/tty.h>
34#include <linux/tty_flip.h>
35#include <linux/major.h>
36#include <linux/string.h>
37#include <linux/fcntl.h>
38#include <linux/ptrace.h>
39#include <linux/serial.h>
40#include <linux/tty_driver.h>
41#include <linux/delay.h>
42#include <linux/pci.h>
43#include <linux/init.h>
44#include <linux/bitops.h>
45
46#include <asm/system.h>
47#include <asm/io.h>
48#include <asm/uaccess.h>
49
Jiri Slaby03718232008-04-30 00:53:39 -070050#include "moxa.h"
51
Jiri Slaby11324ed2007-02-10 01:45:31 -080052#define MOXA_VERSION "5.1k"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Jiri Slaby03718232008-04-30 00:53:39 -070054#define MOXA_FW_HDRLEN 32
55
Jiri Slaby11324ed2007-02-10 01:45:31 -080056#define MOXAMAJOR 172
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Jiri Slaby11324ed2007-02-10 01:45:31 -080058#define MAX_BOARDS 4 /* Don't change this value */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
Jiri Slaby11324ed2007-02-10 01:45:31 -080060#define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/*
63 * Define the Moxa PCI vendor and device IDs.
64 */
Jiri Slaby11324ed2007-02-10 01:45:31 -080065#define MOXA_BUS_TYPE_ISA 0
66#define MOXA_BUS_TYPE_PCI 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068enum {
69 MOXA_BOARD_C218_PCI = 1,
70 MOXA_BOARD_C218_ISA,
71 MOXA_BOARD_C320_PCI,
72 MOXA_BOARD_C320_ISA,
73 MOXA_BOARD_CP204J,
74};
75
76static char *moxa_brdname[] =
77{
78 "C218 Turbo PCI series",
79 "C218 Turbo ISA series",
80 "C320 Turbo PCI series",
81 "C320 Turbo ISA series",
82 "CP-204J series",
83};
84
85#ifdef CONFIG_PCI
86static struct pci_device_id moxa_pcibrds[] = {
Jiri Slaby5ebb4072007-02-10 01:45:30 -080087 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
88 .driver_data = MOXA_BOARD_C218_PCI },
89 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
90 .driver_data = MOXA_BOARD_C320_PCI },
91 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
92 .driver_data = MOXA_BOARD_CP204J },
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 { 0 }
94};
95MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
96#endif /* CONFIG_PCI */
97
Jiri Slaby03718232008-04-30 00:53:39 -070098struct moxa_port;
99
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800100static struct moxa_board_conf {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 int boardType;
102 int numPorts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 int busType;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800104
Jiri Slaby810ab092008-04-30 00:53:41 -0700105 unsigned int ready;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800106
Jiri Slaby03718232008-04-30 00:53:39 -0700107 struct moxa_port *ports;
108
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800109 void __iomem *basemem;
110 void __iomem *intNdx;
111 void __iomem *intPend;
112 void __iomem *intTable;
113} moxa_boards[MAX_BOARDS];
114
115struct mxser_mstatus {
116 tcflag_t cflag;
117 int cts;
118 int dsr;
119 int ri;
120 int dcd;
Jiri Slaby9dff89c2007-02-10 01:45:30 -0800121};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800123struct moxaq_str {
124 int inq;
125 int outq;
126};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800128struct moxa_port {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700129 struct moxa_board_conf *board;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700130 struct tty_struct *tty;
131 void __iomem *tableAddr;
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 int close_delay;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700135 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 int asyncflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int cflag;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700138 unsigned long statusflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 wait_queue_head_t open_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700141 u8 DCDState;
142 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 */
153#define TXSTOPPED 0x1
154#define LOWWAIT 0x2
155#define EMPTYWAIT 0x4
156#define THROTTLE 0x8
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158#define SERIAL_DO_RESTART
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#define WAKEUP_CHARS 256
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162static int ttymajor = MOXAMAJOR;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700163static struct mon_str moxaLog;
164static unsigned int moxaFuncTout = HZ / 2;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700165static unsigned int moxaLowWaterChk;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700166static DEFINE_MUTEX(moxa_openlock);
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 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static void moxa_throttle(struct tty_struct *);
197static void moxa_unthrottle(struct tty_struct *);
Alan Cox606d0992006-12-08 02:38:45 -0800198static void moxa_set_termios(struct tty_struct *, struct ktermios *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static void moxa_stop(struct tty_struct *);
200static void moxa_start(struct tty_struct *);
201static void moxa_hangup(struct tty_struct *);
202static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
203static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
204 unsigned int set, unsigned int clear);
205static void moxa_poll(unsigned long);
Alan Coxdb1acaa2008-02-08 04:18:43 -0800206static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
Jiri Slaby6f56b652007-10-18 03:06:24 -0700207static void moxa_setup_empty_event(struct tty_struct *);
Jiri Slaby6f56b652007-10-18 03:06:24 -0700208static void moxa_shut_down(struct moxa_port *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/*
210 * moxa board interface functions:
211 */
Jiri Slabyb4173f42008-04-30 00:53:40 -0700212static void MoxaPortEnable(struct moxa_port *);
213static void MoxaPortDisable(struct moxa_port *);
214static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
215static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
216static void MoxaPortLineCtrl(struct moxa_port *, int, int);
217static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
218static int MoxaPortLineStatus(struct moxa_port *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700219static void MoxaPortFlushData(struct moxa_port *, int);
Jiri Slaby2108eba2008-04-30 00:53:44 -0700220static int MoxaPortWriteData(struct moxa_port *, const unsigned char *, int);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700221static int MoxaPortReadData(struct moxa_port *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700222static int MoxaPortTxQueue(struct moxa_port *);
223static int MoxaPortRxQueue(struct moxa_port *);
224static int MoxaPortTxFree(struct moxa_port *);
225static void MoxaPortTxDisable(struct moxa_port *);
226static void MoxaPortTxEnable(struct moxa_port *);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800227static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
228static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700229static void MoxaSetFifo(struct moxa_port *port, int enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Jiri Slaby74d7d972008-04-30 00:53:43 -0700231/*
232 * I/O functions
233 */
234
235static 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{
248 writew(arg, ofsAddr + FuncArg);
249 writew(cmd, ofsAddr + FuncCode);
250 moxa_wait_finish(ofsAddr);
251}
252
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700253static void moxa_low_water_check(void __iomem *ofsAddr)
254{
255 u16 rptr, wptr, mask, len;
256
257 if (readb(ofsAddr + FlagStat) & Xoff_state) {
258 rptr = readw(ofsAddr + RXrptr);
259 wptr = readw(ofsAddr + RXwptr);
260 mask = readw(ofsAddr + RX_mask);
261 len = (wptr - rptr) & mask;
262 if (len <= Low_water)
263 moxafunc(ofsAddr, FC_SendXon, 0);
264 }
265}
266
Jiri Slaby74d7d972008-04-30 00:53:43 -0700267/*
268 * TTY operations
269 */
270
271static int moxa_ioctl(struct tty_struct *tty, struct file *file,
272 unsigned int cmd, unsigned long arg)
273{
274 struct moxa_port *ch = tty->driver_data;
275 void __user *argp = (void __user *)arg;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700276 int status, ret = 0;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700277
278 if (tty->index == MAX_PORTS) {
279 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
280 cmd != MOXA_GETMSTATUS)
281 return -EINVAL;
282 } else if (!ch)
283 return -ENODEV;
284
285 switch (cmd) {
286 case MOXA_GETDATACOUNT:
287 moxaLog.tick = jiffies;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700288 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
289 ret = -EFAULT;
290 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700291 case MOXA_FLUSH_QUEUE:
292 MoxaPortFlushData(ch, arg);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700293 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700294 case MOXA_GET_IOQUEUE: {
295 struct moxaq_str __user *argm = argp;
296 struct moxaq_str tmp;
297 struct moxa_port *p;
298 unsigned int i, j;
299
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700300 mutex_lock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700301 for (i = 0; i < MAX_BOARDS; i++) {
302 p = moxa_boards[i].ports;
303 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
304 memset(&tmp, 0, sizeof(tmp));
305 if (moxa_boards[i].ready) {
306 tmp.inq = MoxaPortRxQueue(p);
307 tmp.outq = MoxaPortTxQueue(p);
308 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700309 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
310 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700311 return -EFAULT;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700312 }
Jiri Slaby74d7d972008-04-30 00:53:43 -0700313 }
314 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700315 mutex_unlock(&moxa_openlock);
316 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700317 } case MOXA_GET_OQUEUE:
318 status = MoxaPortTxQueue(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700319 ret = put_user(status, (unsigned long __user *)argp);
320 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700321 case MOXA_GET_IQUEUE:
322 status = MoxaPortRxQueue(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700323 ret = put_user(status, (unsigned long __user *)argp);
324 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700325 case MOXA_GETMSTATUS: {
326 struct mxser_mstatus __user *argm = argp;
327 struct mxser_mstatus tmp;
328 struct moxa_port *p;
329 unsigned int i, j;
330
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700331 mutex_lock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700332 for (i = 0; i < MAX_BOARDS; i++) {
333 p = moxa_boards[i].ports;
334 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
335 memset(&tmp, 0, sizeof(tmp));
336 if (!moxa_boards[i].ready)
337 goto copy;
338
339 status = MoxaPortLineStatus(p);
340 if (status & 1)
341 tmp.cts = 1;
342 if (status & 2)
343 tmp.dsr = 1;
344 if (status & 4)
345 tmp.dcd = 1;
346
347 if (!p->tty || !p->tty->termios)
348 tmp.cflag = p->cflag;
349 else
350 tmp.cflag = p->tty->termios->c_cflag;
351copy:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700352 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
353 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700354 return -EFAULT;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700355 }
Jiri Slaby74d7d972008-04-30 00:53:43 -0700356 }
357 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700358 mutex_unlock(&moxa_openlock);
359 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700360 }
361 case TIOCGSERIAL:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700362 mutex_lock(&moxa_openlock);
363 ret = moxa_get_serial_info(ch, argp);
364 mutex_unlock(&moxa_openlock);
365 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700366 case TIOCSSERIAL:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700367 mutex_lock(&moxa_openlock);
368 ret = moxa_set_serial_info(ch, argp);
369 mutex_unlock(&moxa_openlock);
370 break;
371 default:
372 ret = -ENOIOCTLCMD;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700373 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700374 return ret;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700375}
376
377static void moxa_break_ctl(struct tty_struct *tty, int state)
378{
379 struct moxa_port *port = tty->driver_data;
380
381 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
382 Magic_code);
383}
384
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700385static const struct tty_operations moxa_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 .open = moxa_open,
387 .close = moxa_close,
388 .write = moxa_write,
389 .write_room = moxa_write_room,
390 .flush_buffer = moxa_flush_buffer,
391 .chars_in_buffer = moxa_chars_in_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 .ioctl = moxa_ioctl,
393 .throttle = moxa_throttle,
394 .unthrottle = moxa_unthrottle,
395 .set_termios = moxa_set_termios,
396 .stop = moxa_stop,
397 .start = moxa_start,
398 .hangup = moxa_hangup,
Jiri Slaby74d7d972008-04-30 00:53:43 -0700399 .break_ctl = moxa_break_ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 .tiocmget = moxa_tiocmget,
401 .tiocmset = moxa_tiocmset,
402};
403
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800404static struct tty_driver *moxaDriver;
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800405static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
Ingo Molnar34af9462006-06-27 02:53:55 -0700406static DEFINE_SPINLOCK(moxa_lock);
Alan Cox33f0f882006-01-09 20:54:13 -0800407
Jiri Slaby74d7d972008-04-30 00:53:43 -0700408/*
409 * HW init
410 */
411
Jiri Slaby03718232008-04-30 00:53:39 -0700412static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
413{
414 switch (brd->boardType) {
415 case MOXA_BOARD_C218_ISA:
416 case MOXA_BOARD_C218_PCI:
417 if (model != 1)
418 goto err;
419 break;
420 case MOXA_BOARD_CP204J:
421 if (model != 3)
422 goto err;
423 break;
424 default:
425 if (model != 2)
426 goto err;
427 break;
428 }
429 return 0;
430err:
431 return -EINVAL;
432}
433
434static int moxa_check_fw(const void *ptr)
435{
436 const __le16 *lptr = ptr;
437
438 if (*lptr != cpu_to_le16(0x7980))
439 return -EINVAL;
440
441 return 0;
442}
443
444static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
445 size_t len)
446{
447 void __iomem *baseAddr = brd->basemem;
448 u16 tmp;
449
450 writeb(HW_reset, baseAddr + Control_reg); /* reset */
451 msleep(10);
452 memset_io(baseAddr, 0, 4096);
453 memcpy_toio(baseAddr, buf, len); /* download BIOS */
454 writeb(0, baseAddr + Control_reg); /* restart */
455
456 msleep(2000);
457
458 switch (brd->boardType) {
459 case MOXA_BOARD_C218_ISA:
460 case MOXA_BOARD_C218_PCI:
461 tmp = readw(baseAddr + C218_key);
462 if (tmp != C218_KeyCode)
463 goto err;
464 break;
465 case MOXA_BOARD_CP204J:
466 tmp = readw(baseAddr + C218_key);
467 if (tmp != CP204J_KeyCode)
468 goto err;
469 break;
470 default:
471 tmp = readw(baseAddr + C320_key);
472 if (tmp != C320_KeyCode)
473 goto err;
474 tmp = readw(baseAddr + C320_status);
475 if (tmp != STS_init) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700476 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
Jiri Slaby03718232008-04-30 00:53:39 -0700477 "module not found\n");
478 return -EIO;
479 }
480 break;
481 }
482
483 return 0;
484err:
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700485 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700486 return -EIO;
487}
488
489static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
490 size_t len)
491{
492 void __iomem *baseAddr = brd->basemem;
493
494 if (len < 7168) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700495 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700496 return -EINVAL;
497 }
498
499 writew(len - 7168 - 2, baseAddr + C320bapi_len);
500 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
501 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
502 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
503 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
504
505 return 0;
506}
507
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700508static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
Jiri Slaby03718232008-04-30 00:53:39 -0700509 size_t len)
510{
511 void __iomem *baseAddr = brd->basemem;
512 const u16 *uptr = ptr;
513 size_t wlen, len2, j;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700514 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
515 unsigned int i, retry, c320;
Jiri Slaby03718232008-04-30 00:53:39 -0700516 u16 usum, keycode;
517
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700518 c320 = brd->boardType == MOXA_BOARD_C320_PCI ||
519 brd->boardType == MOXA_BOARD_C320_ISA;
520 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
521 C218_KeyCode;
522
523 switch (brd->boardType) {
524 case MOXA_BOARD_CP204J:
525 case MOXA_BOARD_C218_ISA:
526 case MOXA_BOARD_C218_PCI:
527 key = C218_key;
528 loadbuf = C218_LoadBuf;
529 loadlen = C218DLoad_len;
530 checksum = C218check_sum;
531 checksum_ok = C218chksum_ok;
532 break;
533 default:
534 key = C320_key;
535 keycode = C320_KeyCode;
536 loadbuf = C320_LoadBuf;
537 loadlen = C320DLoad_len;
538 checksum = C320check_sum;
539 checksum_ok = C320chksum_ok;
540 break;
541 }
542
Jiri Slaby03718232008-04-30 00:53:39 -0700543 usum = 0;
544 wlen = len >> 1;
545 for (i = 0; i < wlen; i++)
546 usum += le16_to_cpu(uptr[i]);
547 retry = 0;
548 do {
549 wlen = len >> 1;
550 j = 0;
551 while (wlen) {
552 len2 = (wlen > 2048) ? 2048 : wlen;
553 wlen -= len2;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700554 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
Jiri Slaby03718232008-04-30 00:53:39 -0700555 j += len2 << 1;
556
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700557 writew(len2, baseAddr + loadlen);
558 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700559 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700560 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700561 break;
562 msleep(10);
563 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700564 if (readw(baseAddr + key) != keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700565 return -EIO;
566 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700567 writew(0, baseAddr + loadlen);
568 writew(usum, baseAddr + checksum);
569 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700570 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700571 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700572 break;
573 msleep(10);
574 }
575 retry++;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700576 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
577 if (readb(baseAddr + checksum_ok) != 1)
Jiri Slaby03718232008-04-30 00:53:39 -0700578 return -EIO;
579
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700580 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700581 for (i = 0; i < 600; i++) {
582 if (readw(baseAddr + Magic_no) == Magic_code)
583 break;
584 msleep(10);
585 }
586 if (readw(baseAddr + Magic_no) != Magic_code)
587 return -EIO;
588
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700589 if (c320) {
590 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
591 writew(0x3800, baseAddr + TMS320_PORT1);
592 writew(0x3900, baseAddr + TMS320_PORT2);
593 writew(28499, baseAddr + TMS320_CLOCK);
594 } else {
595 writew(0x3200, baseAddr + TMS320_PORT1);
596 writew(0x3400, baseAddr + TMS320_PORT2);
597 writew(19999, baseAddr + TMS320_CLOCK);
598 }
Jiri Slaby03718232008-04-30 00:53:39 -0700599 }
600 writew(1, baseAddr + Disable_IRQ);
601 writew(0, baseAddr + Magic_no);
602 for (i = 0; i < 500; 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 Slaby5292bcd2008-04-30 00:53:39 -0700610 if (c320) {
611 j = readw(baseAddr + Module_cnt);
612 if (j <= 0)
613 return -EIO;
614 brd->numPorts = j * 8;
615 writew(j, baseAddr + Module_no);
616 writew(0, baseAddr + Magic_no);
617 for (i = 0; i < 600; i++) {
618 if (readw(baseAddr + Magic_no) == Magic_code)
619 break;
620 msleep(10);
621 }
622 if (readw(baseAddr + Magic_no) != Magic_code)
623 return -EIO;
Jiri Slaby03718232008-04-30 00:53:39 -0700624 }
Jiri Slaby03718232008-04-30 00:53:39 -0700625 brd->intNdx = baseAddr + IRQindex;
626 brd->intPend = baseAddr + IRQpending;
627 brd->intTable = baseAddr + IRQtable;
628
629 return 0;
630}
631
632static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
633 size_t len)
634{
635 void __iomem *ofsAddr, *baseAddr = brd->basemem;
636 struct moxa_port *port;
637 int retval, i;
638
639 if (len % 2) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700640 printk(KERN_ERR "MOXA: bios length is not even\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700641 return -EINVAL;
642 }
643
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700644 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
645 if (retval)
646 return retval;
647
Jiri Slaby03718232008-04-30 00:53:39 -0700648 switch (brd->boardType) {
649 case MOXA_BOARD_C218_ISA:
650 case MOXA_BOARD_C218_PCI:
651 case MOXA_BOARD_CP204J:
Jiri Slaby03718232008-04-30 00:53:39 -0700652 port = brd->ports;
653 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700654 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700655 port->DCDState = 0;
656 port->tableAddr = baseAddr + Extern_table +
657 Extern_size * i;
658 ofsAddr = port->tableAddr;
659 writew(C218rx_mask, ofsAddr + RX_mask);
660 writew(C218tx_mask, ofsAddr + TX_mask);
661 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
662 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
663
664 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
665 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
666
667 }
668 break;
669 default:
Jiri Slaby03718232008-04-30 00:53:39 -0700670 port = brd->ports;
671 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700672 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700673 port->DCDState = 0;
674 port->tableAddr = baseAddr + Extern_table +
675 Extern_size * i;
676 ofsAddr = port->tableAddr;
677 switch (brd->numPorts) {
678 case 8:
679 writew(C320p8rx_mask, ofsAddr + RX_mask);
680 writew(C320p8tx_mask, ofsAddr + TX_mask);
681 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
682 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
683 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
684 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
685
686 break;
687 case 16:
688 writew(C320p16rx_mask, ofsAddr + RX_mask);
689 writew(C320p16tx_mask, ofsAddr + TX_mask);
690 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
691 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
692 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
693 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
694 break;
695
696 case 24:
697 writew(C320p24rx_mask, ofsAddr + RX_mask);
698 writew(C320p24tx_mask, ofsAddr + TX_mask);
699 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
700 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
701 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
702 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
703 break;
704 case 32:
705 writew(C320p32rx_mask, ofsAddr + RX_mask);
706 writew(C320p32tx_mask, ofsAddr + TX_mask);
707 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
708 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
709 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
710 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
711 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
712 break;
713 }
714 }
715 break;
716 }
Jiri Slaby03718232008-04-30 00:53:39 -0700717 return 0;
718}
719
720static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
721{
722 void *ptr = fw->data;
723 char rsn[64];
724 u16 lens[5];
725 size_t len;
726 unsigned int a, lenp, lencnt;
727 int ret = -EINVAL;
728 struct {
729 __le32 magic; /* 0x34303430 */
730 u8 reserved1[2];
731 u8 type; /* UNIX = 3 */
732 u8 model; /* C218T=1, C320T=2, CP204=3 */
733 u8 reserved2[8];
734 __le16 len[5];
735 } *hdr = ptr;
736
737 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
738
739 if (fw->size < MOXA_FW_HDRLEN) {
740 strcpy(rsn, "too short (even header won't fit)");
741 goto err;
742 }
743 if (hdr->magic != cpu_to_le32(0x30343034)) {
744 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
745 goto err;
746 }
747 if (hdr->type != 3) {
748 sprintf(rsn, "not for linux, type is %u", hdr->type);
749 goto err;
750 }
751 if (moxa_check_fw_model(brd, hdr->model)) {
752 sprintf(rsn, "not for this card, model is %u", hdr->model);
753 goto err;
754 }
755
756 len = MOXA_FW_HDRLEN;
757 lencnt = hdr->model == 2 ? 5 : 3;
758 for (a = 0; a < ARRAY_SIZE(lens); a++) {
759 lens[a] = le16_to_cpu(hdr->len[a]);
760 if (lens[a] && len + lens[a] <= fw->size &&
761 moxa_check_fw(&fw->data[len]))
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700762 printk(KERN_WARNING "MOXA firmware: unexpected input "
Jiri Slaby03718232008-04-30 00:53:39 -0700763 "at offset %u, but going on\n", (u32)len);
764 if (!lens[a] && a < lencnt) {
765 sprintf(rsn, "too few entries in fw file");
766 goto err;
767 }
768 len += lens[a];
769 }
770
771 if (len != fw->size) {
772 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
773 (u32)len);
774 goto err;
775 }
776
777 ptr += MOXA_FW_HDRLEN;
778 lenp = 0; /* bios */
779
780 strcpy(rsn, "read above");
781
782 ret = moxa_load_bios(brd, ptr, lens[lenp]);
783 if (ret)
784 goto err;
785
786 /* we skip the tty section (lens[1]), since we don't need it */
787 ptr += lens[lenp] + lens[lenp + 1];
788 lenp += 2; /* comm */
789
790 if (hdr->model == 2) {
791 ret = moxa_load_320b(brd, ptr, lens[lenp]);
792 if (ret)
793 goto err;
794 /* skip another tty */
795 ptr += lens[lenp] + lens[lenp + 1];
796 lenp += 2;
797 }
798
799 ret = moxa_load_code(brd, ptr, lens[lenp]);
800 if (ret)
801 goto err;
802
803 return 0;
804err:
805 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
806 return ret;
807}
808
809static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
810{
811 const struct firmware *fw;
812 const char *file;
Jiri Slaby810ab092008-04-30 00:53:41 -0700813 struct moxa_port *p;
814 unsigned int i;
Jiri Slaby03718232008-04-30 00:53:39 -0700815 int ret;
816
Jiri Slaby810ab092008-04-30 00:53:41 -0700817 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
818 GFP_KERNEL);
819 if (brd->ports == NULL) {
820 printk(KERN_ERR "cannot allocate memory for ports\n");
821 ret = -ENOMEM;
822 goto err;
823 }
824
825 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
826 p->type = PORT_16550A;
827 p->close_delay = 5 * HZ / 10;
828 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
829 init_waitqueue_head(&p->open_wait);
Jiri Slaby810ab092008-04-30 00:53:41 -0700830 }
831
Jiri Slaby03718232008-04-30 00:53:39 -0700832 switch (brd->boardType) {
833 case MOXA_BOARD_C218_ISA:
834 case MOXA_BOARD_C218_PCI:
835 file = "c218tunx.cod";
836 break;
837 case MOXA_BOARD_CP204J:
838 file = "cp204unx.cod";
839 break;
840 default:
841 file = "c320tunx.cod";
842 break;
843 }
844
845 ret = request_firmware(&fw, file, dev);
846 if (ret) {
847 printk(KERN_ERR "request_firmware failed\n");
Jiri Slaby810ab092008-04-30 00:53:41 -0700848 goto err_free;
Jiri Slaby03718232008-04-30 00:53:39 -0700849 }
850
851 ret = moxa_load_fw(brd, fw);
852
853 release_firmware(fw);
Jiri Slaby810ab092008-04-30 00:53:41 -0700854
855 if (ret)
856 goto err_free;
857
Jiri Slaby2a541342008-04-30 00:53:45 -0700858 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700859 brd->ready = 1;
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700860 if (!timer_pending(&moxaTimer))
861 mod_timer(&moxaTimer, jiffies + HZ / 50);
Jiri Slaby2a541342008-04-30 00:53:45 -0700862 spin_unlock_bh(&moxa_lock);
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700863
Jiri Slaby810ab092008-04-30 00:53:41 -0700864 return 0;
865err_free:
866 kfree(brd->ports);
867err:
Jiri Slaby03718232008-04-30 00:53:39 -0700868 return ret;
869}
870
Jiri Slaby810ab092008-04-30 00:53:41 -0700871static void moxa_board_deinit(struct moxa_board_conf *brd)
872{
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700873 unsigned int a, opened;
874
875 mutex_lock(&moxa_openlock);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700876 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700877 brd->ready = 0;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700878 spin_unlock_bh(&moxa_lock);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700879
880 /* pci hot-un-plug support */
881 for (a = 0; a < brd->numPorts; a++)
882 if (brd->ports[a].asyncflags & ASYNC_INITIALIZED)
883 tty_hangup(brd->ports[a].tty);
884 while (1) {
885 opened = 0;
886 for (a = 0; a < brd->numPorts; a++)
887 if (brd->ports[a].asyncflags & ASYNC_INITIALIZED)
888 opened++;
889 mutex_unlock(&moxa_openlock);
890 if (!opened)
891 break;
892 msleep(50);
893 mutex_lock(&moxa_openlock);
894 }
895
Jiri Slaby810ab092008-04-30 00:53:41 -0700896 iounmap(brd->basemem);
897 brd->basemem = NULL;
898 kfree(brd->ports);
899}
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901#ifdef CONFIG_PCI
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800902static int __devinit moxa_pci_probe(struct pci_dev *pdev,
903 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800905 struct moxa_board_conf *board;
906 unsigned int i;
907 int board_type = ent->driver_data;
908 int retval;
909
910 retval = pci_enable_device(pdev);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700911 if (retval) {
912 dev_err(&pdev->dev, "can't enable pci device\n");
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800913 goto err;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700914 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800915
916 for (i = 0; i < MAX_BOARDS; i++)
917 if (moxa_boards[i].basemem == NULL)
918 break;
919
920 retval = -ENODEV;
921 if (i >= MAX_BOARDS) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700922 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800923 "found. Board is ignored.\n", MAX_BOARDS);
924 goto err;
925 }
926
927 board = &moxa_boards[i];
Jiri Slabye46a5e32008-04-30 00:53:37 -0700928
929 retval = pci_request_region(pdev, 2, "moxa-base");
930 if (retval) {
931 dev_err(&pdev->dev, "can't request pci region 2\n");
932 goto err;
933 }
934
935 board->basemem = ioremap(pci_resource_start(pdev, 2), 0x4000);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700936 if (board->basemem == NULL) {
937 dev_err(&pdev->dev, "can't remap io space 2\n");
Jiri Slabye46a5e32008-04-30 00:53:37 -0700938 goto err_reg;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700939 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 board->boardType = board_type;
942 switch (board_type) {
943 case MOXA_BOARD_C218_ISA:
944 case MOXA_BOARD_C218_PCI:
945 board->numPorts = 8;
946 break;
947
948 case MOXA_BOARD_CP204J:
949 board->numPorts = 4;
950 break;
951 default:
952 board->numPorts = 0;
953 break;
954 }
955 board->busType = MOXA_BUS_TYPE_PCI;
Jiri Slabya784bf72007-02-10 01:45:36 -0800956
Jiri Slaby03718232008-04-30 00:53:39 -0700957 retval = moxa_init_board(board, &pdev->dev);
958 if (retval)
959 goto err_base;
960
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800961 pci_set_drvdata(pdev, board);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700963 return 0;
Jiri Slaby03718232008-04-30 00:53:39 -0700964err_base:
965 iounmap(board->basemem);
966 board->basemem = NULL;
Jiri Slabye46a5e32008-04-30 00:53:37 -0700967err_reg:
968 pci_release_region(pdev, 2);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800969err:
970 return retval;
971}
972
973static void __devexit moxa_pci_remove(struct pci_dev *pdev)
974{
975 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
976
Jiri Slaby810ab092008-04-30 00:53:41 -0700977 moxa_board_deinit(brd);
978
Jiri Slabye46a5e32008-04-30 00:53:37 -0700979 pci_release_region(pdev, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
Jiri Slabya784bf72007-02-10 01:45:36 -0800981
982static struct pci_driver moxa_pci_driver = {
983 .name = "moxa",
984 .id_table = moxa_pcibrds,
985 .probe = moxa_pci_probe,
986 .remove = __devexit_p(moxa_pci_remove)
987};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988#endif /* CONFIG_PCI */
989
990static int __init moxa_init(void)
991{
Jiri Slaby810ab092008-04-30 00:53:41 -0700992 unsigned int isabrds = 0;
Jiri Slabyd353eca2008-04-30 00:53:37 -0700993 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700995 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
996 MOXA_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
998 if (!moxaDriver)
999 return -ENOMEM;
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 moxaDriver->owner = THIS_MODULE;
Sergey Vlasov9b4e3b12005-09-03 16:26:49 +01001002 moxaDriver->name = "ttyMX";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 moxaDriver->major = ttymajor;
1004 moxaDriver->minor_start = 0;
1005 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1006 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1007 moxaDriver->init_termios = tty_std_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Alan Cox606d0992006-12-08 02:38:45 -08001009 moxaDriver->init_termios.c_ispeed = 9600;
1010 moxaDriver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
1012 tty_set_operations(moxaDriver, &moxa_ops);
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (tty_register_driver(moxaDriver)) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001015 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 put_tty_driver(moxaDriver);
1017 return -1;
1018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Jiri Slabyd353eca2008-04-30 00:53:37 -07001020 /* Find the boards defined from module args. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -07001022 {
1023 struct moxa_board_conf *brd = moxa_boards;
Jiri Slaby810ab092008-04-30 00:53:41 -07001024 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 for (i = 0; i < MAX_BOARDS; i++) {
Jiri Slabyd353eca2008-04-30 00:53:37 -07001026 if (!baseaddr[i])
1027 break;
1028 if (type[i] == MOXA_BOARD_C218_ISA ||
1029 type[i] == MOXA_BOARD_C320_ISA) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001030 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001031 isabrds + 1, moxa_brdname[type[i] - 1],
1032 baseaddr[i]);
1033 brd->boardType = type[i];
1034 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1035 numports[i];
1036 brd->busType = MOXA_BUS_TYPE_ISA;
1037 brd->basemem = ioremap(baseaddr[i], 0x4000);
1038 if (!brd->basemem) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001039 printk(KERN_ERR "MOXA: can't remap %lx\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001040 baseaddr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 continue;
1042 }
Jiri Slaby03718232008-04-30 00:53:39 -07001043 if (moxa_init_board(brd, NULL)) {
1044 iounmap(brd->basemem);
1045 brd->basemem = NULL;
1046 continue;
1047 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001048
1049 brd++;
1050 isabrds++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
1052 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001057 retval = pci_register_driver(&moxa_pci_driver);
1058 if (retval) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001059 printk(KERN_ERR "Can't register MOXA pci driver!\n");
Jiri Slabyd353eca2008-04-30 00:53:37 -07001060 if (isabrds)
Jiri Slabya784bf72007-02-10 01:45:36 -08001061 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001064
Jiri Slabya784bf72007-02-10 01:45:36 -08001065 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
1068static void __exit moxa_exit(void)
1069{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001070 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001072#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001073 pci_unregister_driver(&moxa_pci_driver);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001074#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001075
Jiri Slaby810ab092008-04-30 00:53:41 -07001076 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1077 if (moxa_boards[i].ready)
1078 moxa_board_deinit(&moxa_boards[i]);
Jiri Slaby2a541342008-04-30 00:53:45 -07001079
1080 del_timer_sync(&moxaTimer);
1081
1082 if (tty_unregister_driver(moxaDriver))
1083 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1084 "serial driver\n");
1085 put_tty_driver(moxaDriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
1087
1088module_init(moxa_init);
1089module_exit(moxa_exit);
1090
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001091static void moxa_close_port(struct moxa_port *ch)
1092{
1093 moxa_shut_down(ch);
1094 MoxaPortFlushData(ch, 2);
1095 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1096 ch->tty->driver_data = NULL;
1097 ch->tty = NULL;
1098}
1099
1100static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
1101 struct moxa_port *ch)
1102{
1103 DEFINE_WAIT(wait);
1104 int retval = 0;
1105 u8 dcd;
1106
1107 while (1) {
1108 prepare_to_wait(&ch->open_wait, &wait, TASK_INTERRUPTIBLE);
1109 if (tty_hung_up_p(filp)) {
1110#ifdef SERIAL_DO_RESTART
1111 retval = -ERESTARTSYS;
1112#else
1113 retval = -EAGAIN;
1114#endif
1115 break;
1116 }
1117 spin_lock_bh(&moxa_lock);
1118 dcd = ch->DCDState;
1119 spin_unlock_bh(&moxa_lock);
1120 if (dcd)
1121 break;
1122
1123 if (signal_pending(current)) {
1124 retval = -ERESTARTSYS;
1125 break;
1126 }
1127 schedule();
1128 }
1129 finish_wait(&ch->open_wait, &wait);
1130
1131 return retval;
1132}
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134static int moxa_open(struct tty_struct *tty, struct file *filp)
1135{
Jiri Slaby810ab092008-04-30 00:53:41 -07001136 struct moxa_board_conf *brd;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001137 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 int port;
1139 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Jiri Slaby11324ed2007-02-10 01:45:31 -08001141 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (port == MAX_PORTS) {
Jiri Slaby74d7d972008-04-30 00:53:43 -07001143 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001145 if (mutex_lock_interruptible(&moxa_openlock))
1146 return -ERESTARTSYS;
Jiri Slaby810ab092008-04-30 00:53:41 -07001147 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001148 if (!brd->ready) {
1149 mutex_unlock(&moxa_openlock);
Jiri Slaby810ab092008-04-30 00:53:41 -07001150 return -ENODEV;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Jiri Slaby810ab092008-04-30 00:53:41 -07001153 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 ch->count++;
1155 tty->driver_data = ch;
1156 ch->tty = tty;
1157 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
1158 ch->statusflags = 0;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001159 moxa_set_tty_param(tty, tty->termios);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001160 MoxaPortLineCtrl(ch, 1, 1);
1161 MoxaPortEnable(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001162 MoxaSetFifo(ch, ch->type == PORT_16550A);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 ch->asyncflags |= ASYNC_INITIALIZED;
1164 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001165 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001167 retval = 0;
1168 if (!(filp->f_flags & O_NONBLOCK) && !C_CLOCAL(tty))
1169 retval = moxa_block_till_ready(tty, filp, ch);
1170 mutex_lock(&moxa_openlock);
1171 if (retval) {
1172 if (ch->count) /* 0 means already hung up... */
1173 if (--ch->count == 0)
1174 moxa_close_port(ch);
1175 } else
1176 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1177 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001179 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
1181
1182static void moxa_close(struct tty_struct *tty, struct file *filp)
1183{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001184 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 int port;
1186
Jiri Slaby11324ed2007-02-10 01:45:31 -08001187 port = tty->index;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001188 if (port == MAX_PORTS || tty_hung_up_p(filp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001191 mutex_lock(&moxa_openlock);
1192 ch = tty->driver_data;
1193 if (ch == NULL)
1194 goto unlock;
1195 if (tty->count == 1 && ch->count != 1) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001196 printk(KERN_WARNING "moxa_close: bad serial port count; "
1197 "tty->count is 1, ch->count is %d\n", ch->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 ch->count = 1;
1199 }
1200 if (--ch->count < 0) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001201 printk(KERN_WARNING "moxa_close: bad serial port count, "
1202 "device=%s\n", tty->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 ch->count = 0;
1204 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001205 if (ch->count)
1206 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 ch->cflag = tty->termios->c_cflag;
1209 if (ch->asyncflags & ASYNC_INITIALIZED) {
Jiri Slaby6f56b652007-10-18 03:06:24 -07001210 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001214 moxa_close_port(ch);
1215unlock:
1216 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
1218
1219static int moxa_write(struct tty_struct *tty,
1220 const unsigned char *buf, int count)
1221{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001222 struct moxa_port *ch = tty->driver_data;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001223 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 if (ch == NULL)
Jiri Slabyb4173f42008-04-30 00:53:40 -07001226 return 0;
Alan Cox33f0f882006-01-09 20:54:13 -08001227
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001228 spin_lock_bh(&moxa_lock);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001229 len = MoxaPortWriteData(ch, buf, count);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001230 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 ch->statusflags |= LOWWAIT;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001233 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
1236static int moxa_write_room(struct tty_struct *tty)
1237{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001238 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 if (tty->stopped)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001241 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001242 ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (ch == NULL)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001244 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001245 return MoxaPortTxFree(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
1248static void moxa_flush_buffer(struct tty_struct *tty)
1249{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001250 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 if (ch == NULL)
1253 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001254 MoxaPortFlushData(ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 tty_wakeup(tty);
1256}
1257
1258static int moxa_chars_in_buffer(struct tty_struct *tty)
1259{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001260 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 /*
1264 * Sigh...I have to check if driver_data is NULL here, because
1265 * if an open() fails, the TTY subsystem eventually calls
1266 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1267 * routine. And since the open() failed, we return 0 here. TDJ
1268 */
1269 if (ch == NULL)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001270 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001271 chars = MoxaPortTxQueue(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (chars) {
1273 /*
1274 * Make it possible to wakeup anything waiting for output
1275 * in tty_ioctl.c, etc.
1276 */
1277 if (!(ch->statusflags & EMPTYWAIT))
Jiri Slaby6f56b652007-10-18 03:06:24 -07001278 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001280 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1284{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001285 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 int flag = 0, dtr, rts;
1287
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001288 mutex_lock(&moxa_openlock);
1289 ch = tty->driver_data;
1290 if (!ch) {
1291 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -07001292 return -EINVAL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Jiri Slabyb4173f42008-04-30 00:53:40 -07001295 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (dtr)
1297 flag |= TIOCM_DTR;
1298 if (rts)
1299 flag |= TIOCM_RTS;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001300 dtr = MoxaPortLineStatus(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (dtr & 1)
1302 flag |= TIOCM_CTS;
1303 if (dtr & 2)
1304 flag |= TIOCM_DSR;
1305 if (dtr & 4)
1306 flag |= TIOCM_CD;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001307 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return flag;
1309}
1310
1311static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1312 unsigned int set, unsigned int clear)
1313{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001314 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 int port;
1316 int dtr, rts;
1317
Jiri Slaby11324ed2007-02-10 01:45:31 -08001318 port = tty->index;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001319 mutex_lock(&moxa_openlock);
1320 ch = tty->driver_data;
1321 if (!ch) {
1322 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -07001323 return -EINVAL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Jiri Slabyb4173f42008-04-30 00:53:40 -07001326 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (set & TIOCM_RTS)
1328 rts = 1;
1329 if (set & TIOCM_DTR)
1330 dtr = 1;
1331 if (clear & TIOCM_RTS)
1332 rts = 0;
1333 if (clear & TIOCM_DTR)
1334 dtr = 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001335 MoxaPortLineCtrl(ch, dtr, rts);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001336 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return 0;
1338}
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340static void moxa_throttle(struct tty_struct *tty)
1341{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001342 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 ch->statusflags |= THROTTLE;
1345}
1346
1347static void moxa_unthrottle(struct tty_struct *tty)
1348{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001349 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 ch->statusflags &= ~THROTTLE;
1352}
1353
1354static void moxa_set_termios(struct tty_struct *tty,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001355 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001357 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 if (ch == NULL)
1360 return;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001361 moxa_set_tty_param(tty, old_termios);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001362 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 wake_up_interruptible(&ch->open_wait);
1364}
1365
1366static void moxa_stop(struct tty_struct *tty)
1367{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001368 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 if (ch == NULL)
1371 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001372 MoxaPortTxDisable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 ch->statusflags |= TXSTOPPED;
1374}
1375
1376
1377static void moxa_start(struct tty_struct *tty)
1378{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001379 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 if (ch == NULL)
1382 return;
1383
1384 if (!(ch->statusflags & TXSTOPPED))
1385 return;
1386
Jiri Slabyb4173f42008-04-30 00:53:40 -07001387 MoxaPortTxEnable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 ch->statusflags &= ~TXSTOPPED;
1389}
1390
1391static void moxa_hangup(struct tty_struct *tty)
1392{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001393 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001395 mutex_lock(&moxa_openlock);
1396 ch = tty->driver_data;
1397 if (ch == NULL) {
1398 mutex_unlock(&moxa_openlock);
1399 return;
1400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 ch->count = 0;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001402 moxa_close_port(ch);
1403 mutex_unlock(&moxa_openlock);
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 wake_up_interruptible(&ch->open_wait);
1406}
1407
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001408static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1409{
1410 dcd = !!dcd;
1411
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001412 if (dcd != p->DCDState && p->tty && C_CLOCAL(p->tty)) {
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001413 if (!dcd)
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001414 tty_hangup(p->tty);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001415 }
1416 p->DCDState = dcd;
1417}
1418
1419static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1420 u16 __iomem *ip)
1421{
1422 struct tty_struct *tty = p->tty;
1423 void __iomem *ofsAddr;
1424 unsigned int inited = p->asyncflags & ASYNC_INITIALIZED;
1425 u16 intr;
1426
1427 if (tty) {
1428 if ((p->statusflags & EMPTYWAIT) &&
1429 MoxaPortTxQueue(p) == 0) {
1430 p->statusflags &= ~EMPTYWAIT;
1431 tty_wakeup(tty);
1432 }
1433 if ((p->statusflags & LOWWAIT) && !tty->stopped &&
1434 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1435 p->statusflags &= ~LOWWAIT;
1436 tty_wakeup(tty);
1437 }
1438
1439 if (inited && !(p->statusflags & THROTTLE) &&
1440 MoxaPortRxQueue(p) > 0) { /* RX */
1441 MoxaPortReadData(p);
1442 tty_schedule_flip(tty);
1443 }
1444 } else {
1445 p->statusflags &= ~EMPTYWAIT;
1446 MoxaPortFlushData(p, 0); /* flush RX */
1447 }
1448
1449 if (!handle) /* nothing else to do */
1450 return 0;
1451
1452 intr = readw(ip); /* port irq status */
1453 if (intr == 0)
1454 return 0;
1455
1456 writew(0, ip); /* ACK port */
1457 ofsAddr = p->tableAddr;
1458 if (intr & IntrTx) /* disable tx intr */
1459 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1460 ofsAddr + HostStat);
1461
1462 if (!inited)
1463 return 0;
1464
1465 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1466 tty_insert_flip_char(tty, 0, TTY_BREAK);
1467 tty_schedule_flip(tty);
1468 }
1469
1470 if (intr & IntrLine)
1471 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1472
1473 return 0;
1474}
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476static void moxa_poll(unsigned long ignored)
1477{
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001478 struct moxa_board_conf *brd;
1479 u16 __iomem *ip;
Jiri Slaby2a541342008-04-30 00:53:45 -07001480 unsigned int card, port, served = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001482 spin_lock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 for (card = 0; card < MAX_BOARDS; card++) {
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001484 brd = &moxa_boards[card];
1485 if (!brd->ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 continue;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001487
Jiri Slaby2a541342008-04-30 00:53:45 -07001488 served++;
1489
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001490 ip = NULL;
1491 if (readb(brd->intPend) == 0xff)
1492 ip = brd->intTable + readb(brd->intNdx);
1493
1494 for (port = 0; port < brd->numPorts; port++)
1495 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1496
1497 if (ip)
1498 writeb(0, brd->intPend); /* ACK */
1499
1500 if (moxaLowWaterChk) {
1501 struct moxa_port *p = brd->ports;
1502 for (port = 0; port < brd->numPorts; port++, p++)
1503 if (p->lowChkFlag) {
1504 p->lowChkFlag = 0;
1505 moxa_low_water_check(p->tableAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 }
1508 }
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001509 moxaLowWaterChk = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Jiri Slaby2a541342008-04-30 00:53:45 -07001511 if (served)
1512 mod_timer(&moxaTimer, jiffies + HZ / 50);
1513 spin_unlock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514}
1515
1516/******************************************************************************/
1517
Alan Coxdb1acaa2008-02-08 04:18:43 -08001518static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001520 register struct ktermios *ts = tty->termios;
1521 struct moxa_port *ch = tty->driver_data;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001522 int rts, cts, txflow, rxflow, xany, baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 rts = cts = txflow = rxflow = xany = 0;
1525 if (ts->c_cflag & CRTSCTS)
1526 rts = cts = 1;
1527 if (ts->c_iflag & IXON)
1528 txflow = 1;
1529 if (ts->c_iflag & IXOFF)
1530 rxflow = 1;
1531 if (ts->c_iflag & IXANY)
1532 xany = 1;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001533
1534 /* Clear the features we don't support */
1535 ts->c_cflag &= ~CMSPAR;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001536 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1537 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
Alan Coxdb1acaa2008-02-08 04:18:43 -08001538 if (baud == -1)
1539 baud = tty_termios_baud_rate(old_termios);
1540 /* Not put the baud rate into the termios data */
1541 tty_encode_baud_rate(tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543
Jiri Slaby6f56b652007-10-18 03:06:24 -07001544static void moxa_setup_empty_event(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001546 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001548 spin_lock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 ch->statusflags |= EMPTYWAIT;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001550 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
Jiri Slaby6f56b652007-10-18 03:06:24 -07001553static void moxa_shut_down(struct moxa_port *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001555 struct tty_struct *tp = ch->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1558 return;
1559
Jiri Slabyb4173f42008-04-30 00:53:40 -07001560 MoxaPortDisable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 /*
1563 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1564 */
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001565 if (C_HUPCL(tp))
Jiri Slabyb4173f42008-04-30 00:53:40 -07001566 MoxaPortLineCtrl(ch, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001568 spin_lock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 ch->asyncflags &= ~ASYNC_INITIALIZED;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001570 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573/*****************************************************************************
1574 * Driver level functions: *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Jiri Slabyb4173f42008-04-30 00:53:40 -07001577static void MoxaPortFlushData(struct moxa_port *port, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
1579 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001580 if (mode < 0 || mode > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001582 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 moxafunc(ofsAddr, FC_FlushQueue, mode);
1584 if (mode != 1) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001585 port->lowChkFlag = 0;
Jiri Slaby6f56b652007-10-18 03:06:24 -07001586 moxa_low_water_check(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 }
1588}
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590/*
1591 * Moxa Port Number Description:
1592 *
1593 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1594 * the port number using in MOXA driver functions will be 0 to 31 for
1595 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1596 * to 127 for fourth. For example, if you setup three MOXA boards,
1597 * first board is C218, second board is C320-16 and third board is
1598 * C320-32. The port number of first board (C218 - 8 ports) is from
1599 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1600 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1601 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1602 * 127 will be invalid.
1603 *
1604 *
1605 * Moxa Functions Description:
1606 *
1607 * Function 1: Driver initialization routine, this routine must be
1608 * called when initialized driver.
1609 * Syntax:
1610 * void MoxaDriverInit();
1611 *
1612 *
1613 * Function 2: Moxa driver private IOCTL command processing.
1614 * Syntax:
1615 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1616 *
1617 * unsigned int cmd : IOCTL command
1618 * unsigned long arg : IOCTL argument
1619 * int port : port number (0 - 127)
1620 *
1621 * return: 0 (OK)
1622 * -EINVAL
1623 * -ENOIOCTLCMD
1624 *
1625 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 * Function 6: Enable this port to start Tx/Rx data.
1627 * Syntax:
1628 * void MoxaPortEnable(int port);
1629 * int port : port number (0 - 127)
1630 *
1631 *
1632 * Function 7: Disable this port
1633 * Syntax:
1634 * void MoxaPortDisable(int port);
1635 * int port : port number (0 - 127)
1636 *
1637 *
1638 * Function 8: Get the maximun available baud rate of this port.
1639 * Syntax:
1640 * long MoxaPortGetMaxBaud(int port);
1641 * int port : port number (0 - 127)
1642 *
1643 * return: 0 : this port is invalid
1644 * 38400/57600/115200 bps
1645 *
1646 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 * Function 10: Setting baud rate of this port.
1648 * Syntax:
1649 * long MoxaPortSetBaud(int port, long baud);
1650 * int port : port number (0 - 127)
1651 * long baud : baud rate (50 - 115200)
1652 *
1653 * return: 0 : this port is invalid or baud < 50
1654 * 50 - 115200 : the real baud rate set to the port, if
1655 * the argument baud is large than maximun
1656 * available baud rate, the real setting
1657 * baud rate will be the maximun baud rate.
1658 *
1659 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 * Function 12: Configure the port.
1661 * Syntax:
Alan Cox606d0992006-12-08 02:38:45 -08001662 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 * int port : port number (0 - 127)
Alan Cox606d0992006-12-08 02:38:45 -08001664 * struct ktermios * termio : termio structure pointer
Alan Coxc7bce302006-09-30 23:27:24 -07001665 * speed_t baud : baud rate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 *
1667 * return: -1 : this port is invalid or termio == NULL
1668 * 0 : setting O.K.
1669 *
1670 *
1671 * Function 13: Get the DTR/RTS state of this port.
1672 * Syntax:
1673 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1674 * int port : port number (0 - 127)
1675 * int * dtrState : pointer to INT to receive the current DTR
1676 * state. (if NULL, this function will not
1677 * write to this address)
1678 * int * rtsState : pointer to INT to receive the current RTS
1679 * state. (if NULL, this function will not
1680 * write to this address)
1681 *
1682 * return: -1 : this port is invalid
1683 * 0 : O.K.
1684 *
1685 *
1686 * Function 14: Setting the DTR/RTS output state of this port.
1687 * Syntax:
1688 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1689 * int port : port number (0 - 127)
1690 * int dtrState : DTR output state (0: off, 1: on)
1691 * int rtsState : RTS output state (0: off, 1: on)
1692 *
1693 *
1694 * Function 15: Setting the flow control of this port.
1695 * Syntax:
1696 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1697 * int txFlow,int xany);
1698 * int port : port number (0 - 127)
1699 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1700 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1701 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1702 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1703 * int xany : S/W XANY flow control (0: no, 1: yes)
1704 *
1705 *
1706 * Function 16: Get ths line status of this port
1707 * Syntax:
1708 * int MoxaPortLineStatus(int port);
1709 * int port : port number (0 - 127)
1710 *
1711 * return: Bit 0 - CTS state (0: off, 1: on)
1712 * Bit 1 - DSR state (0: off, 1: on)
1713 * Bit 2 - DCD state (0: off, 1: on)
1714 *
1715 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 * Function 19: Flush the Rx/Tx buffer data of this port.
1717 * Syntax:
1718 * void MoxaPortFlushData(int port, int mode);
1719 * int port : port number (0 - 127)
1720 * int mode
1721 * 0 : flush the Rx buffer
1722 * 1 : flush the Tx buffer
1723 * 2 : flush the Rx and Tx buffer
1724 *
1725 *
1726 * Function 20: Write data.
1727 * Syntax:
1728 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1729 * int port : port number (0 - 127)
1730 * unsigned char * buffer : pointer to write data buffer.
1731 * int length : write data length
1732 *
1733 * return: 0 - length : real write data length
1734 *
1735 *
1736 * Function 21: Read data.
1737 * Syntax:
Alan Cox33f0f882006-01-09 20:54:13 -08001738 * int MoxaPortReadData(int port, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 * int port : port number (0 - 127)
Alan Cox33f0f882006-01-09 20:54:13 -08001740 * struct tty_struct *tty : tty for data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 *
1742 * return: 0 - length : real read data length
1743 *
1744 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 * Function 24: Get the Tx buffer current queued data bytes
1746 * Syntax:
1747 * int MoxaPortTxQueue(int port);
1748 * int port : port number (0 - 127)
1749 *
1750 * return: .. : Tx buffer current queued data bytes
1751 *
1752 *
1753 * Function 25: Get the Tx buffer current free space
1754 * Syntax:
1755 * int MoxaPortTxFree(int port);
1756 * int port : port number (0 - 127)
1757 *
1758 * return: .. : Tx buffer current free space
1759 *
1760 *
1761 * Function 26: Get the Rx buffer current queued data bytes
1762 * Syntax:
1763 * int MoxaPortRxQueue(int port);
1764 * int port : port number (0 - 127)
1765 *
1766 * return: .. : Rx buffer current queued data bytes
1767 *
1768 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 * Function 28: Disable port data transmission.
1770 * Syntax:
1771 * void MoxaPortTxDisable(int port);
1772 * int port : port number (0 - 127)
1773 *
1774 *
1775 * Function 29: Enable port data transmission.
1776 * Syntax:
1777 * void MoxaPortTxEnable(int port);
1778 * int port : port number (0 - 127)
1779 *
1780 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 * Function 31: Get the received BREAK signal count and reset it.
1782 * Syntax:
1783 * int MoxaPortResetBrkCnt(int port);
1784 * int port : port number (0 - 127)
1785 *
1786 * return: 0 - .. : BREAK signal count
1787 *
1788 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Jiri Slabyb4173f42008-04-30 00:53:40 -07001791static void MoxaPortEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
1793 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001794 u16 lowwater = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Jiri Slabyb4173f42008-04-30 00:53:40 -07001796 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 writew(lowwater, ofsAddr + Low_water);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001798 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001799 port->board->boardType == MOXA_BOARD_C320_PCI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001801 else
1802 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1803 ofsAddr + HostStat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
1805 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1806 moxafunc(ofsAddr, FC_FlushQueue, 2);
1807
1808 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1809 MoxaPortLineStatus(port);
1810}
1811
Jiri Slabyb4173f42008-04-30 00:53:40 -07001812static void MoxaPortDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001814 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1817 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1818 writew(0, ofsAddr + HostStat);
1819 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1820}
1821
Jiri Slabyb4173f42008-04-30 00:53:40 -07001822static long MoxaPortGetMaxBaud(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001824 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1825 port->board->boardType == MOXA_BOARD_C320_PCI)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001826 return 460800L;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 else
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001828 return 921600L;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829}
1830
1831
Jiri Slabyb4173f42008-04-30 00:53:40 -07001832static long MoxaPortSetBaud(struct moxa_port *port, long baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
1834 void __iomem *ofsAddr;
1835 long max, clock;
1836 unsigned int val;
1837
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001838 if (baud < 50L || (max = MoxaPortGetMaxBaud(port)) == 0)
1839 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001840 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if (baud > max)
1842 baud = max;
1843 if (max == 38400L)
1844 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
1845 else if (max == 57600L)
1846 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
1847 else
1848 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
1849 val = clock / baud;
1850 moxafunc(ofsAddr, FC_SetBaud, val);
1851 baud = clock / val;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001852 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853}
1854
Jiri Slabyb4173f42008-04-30 00:53:40 -07001855static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1856 speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
1858 void __iomem *ofsAddr;
1859 tcflag_t cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 tcflag_t mode = 0;
1861
Jiri Slabyb4173f42008-04-30 00:53:40 -07001862 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 cflag = termio->c_cflag; /* termio->c_cflag */
1864
1865 mode = termio->c_cflag & CSIZE;
1866 if (mode == CS5)
1867 mode = MX_CS5;
1868 else if (mode == CS6)
1869 mode = MX_CS6;
1870 else if (mode == CS7)
1871 mode = MX_CS7;
1872 else if (mode == CS8)
1873 mode = MX_CS8;
1874
1875 if (termio->c_cflag & CSTOPB) {
1876 if (mode == MX_CS5)
1877 mode |= MX_STOP15;
1878 else
1879 mode |= MX_STOP2;
1880 } else
1881 mode |= MX_STOP1;
1882
1883 if (termio->c_cflag & PARENB) {
1884 if (termio->c_cflag & PARODD)
1885 mode |= MX_PARODD;
1886 else
1887 mode |= MX_PAREVEN;
1888 } else
1889 mode |= MX_PARNONE;
1890
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001891 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Jiri Slabyb4173f42008-04-30 00:53:40 -07001893 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1894 port->board->boardType == MOXA_BOARD_C320_PCI) {
Alan Coxc7bce302006-09-30 23:27:24 -07001895 if (baud >= 921600L)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001896 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
Alan Coxdb1acaa2008-02-08 04:18:43 -08001898 baud = MoxaPortSetBaud(port, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1901 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1902 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1903 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
Jiri Slaby6f56b652007-10-18 03:06:24 -07001904 moxa_wait_finish(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 }
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001907 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
1909
Jiri Slabyb4173f42008-04-30 00:53:40 -07001910static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1911 int *rtsState)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001913 if (dtrState)
1914 *dtrState = !!(port->lineCtrl & DTR_ON);
1915 if (rtsState)
1916 *rtsState = !!(port->lineCtrl & RTS_ON);
1917
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001918 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919}
1920
Jiri Slabyb4173f42008-04-30 00:53:40 -07001921static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001923 u8 mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 if (dtr)
1926 mode |= DTR_ON;
1927 if (rts)
1928 mode |= RTS_ON;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001929 port->lineCtrl = mode;
1930 moxafunc(port->tableAddr, FC_LineControl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
Jiri Slabyb4173f42008-04-30 00:53:40 -07001933static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1934 int txflow, int rxflow, int txany)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001936 int mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 if (rts)
1939 mode |= RTS_FlowCtl;
1940 if (cts)
1941 mode |= CTS_FlowCtl;
1942 if (txflow)
1943 mode |= Tx_FlowCtl;
1944 if (rxflow)
1945 mode |= Rx_FlowCtl;
1946 if (txany)
1947 mode |= IXM_IXANY;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001948 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949}
1950
Jiri Slabyb4173f42008-04-30 00:53:40 -07001951static int MoxaPortLineStatus(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
1953 void __iomem *ofsAddr;
1954 int val;
1955
Jiri Slabyb4173f42008-04-30 00:53:40 -07001956 ofsAddr = port->tableAddr;
1957 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1958 port->board->boardType == MOXA_BOARD_C320_PCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 moxafunc(ofsAddr, FC_LineStatus, 0);
1960 val = readw(ofsAddr + FuncArg);
1961 } else {
1962 val = readw(ofsAddr + FlagStat) >> 4;
1963 }
1964 val &= 0x0B;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001965 if (val & 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 val |= 4;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001967 spin_lock_bh(&moxa_lock);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001968 moxa_new_dcdstate(port, val & 8);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001969 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 val &= 7;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001971 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
Jiri Slaby2108eba2008-04-30 00:53:44 -07001974static int MoxaPortWriteData(struct moxa_port *port,
1975 const unsigned char *buffer, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001978 unsigned int c, total;
1979 u16 head, tail, tx_mask, spage, epage;
1980 u16 pageno, pageofs, bufhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Jiri Slabyb4173f42008-04-30 00:53:40 -07001982 ofsAddr = port->tableAddr;
1983 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 tx_mask = readw(ofsAddr + TX_mask);
1985 spage = readw(ofsAddr + Page_txb);
1986 epage = readw(ofsAddr + EndPage_txb);
1987 tail = readw(ofsAddr + TXwptr);
1988 head = readw(ofsAddr + TXrptr);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001989 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if (c > len)
1991 c = len;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001992 moxaLog.txcnt[port->tty->index] += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 total = c;
1994 if (spage == epage) {
1995 bufhead = readw(ofsAddr + Ofs_txb);
1996 writew(spage, baseAddr + Control_reg);
1997 while (c > 0) {
1998 if (head > tail)
1999 len = head - tail - 1;
2000 else
2001 len = tx_mask + 1 - tail;
2002 len = (c > len) ? len : c;
2003 ofs = baseAddr + DynPage_addr + bufhead + tail;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002004 memcpy_toio(ofs, buffer, len);
2005 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 tail = (tail + len) & tx_mask;
2007 c -= len;
2008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 pageno = spage + (tail >> 13);
2011 pageofs = tail & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002012 while (c > 0) {
2013 len = Page_size - pageofs;
2014 if (len > c)
2015 len = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 writeb(pageno, baseAddr + Control_reg);
2017 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002018 memcpy_toio(ofs, buffer, len);
2019 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 if (++pageno == epage)
2021 pageno = spage;
2022 pageofs = 0;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002023 c -= len;
2024 }
2025 tail = (tail + total) & tx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002027 writew(tail, ofsAddr + TXwptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 writeb(1, ofsAddr + CD180TXirq); /* start to send */
Jiri Slaby2108eba2008-04-30 00:53:44 -07002029 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002032static int MoxaPortReadData(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002034 struct tty_struct *tty = port->tty;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002035 unsigned char *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002037 unsigned int count, len, total;
2038 u16 tail, rx_mask, spage, epage;
2039 u16 pageno, pageofs, bufhead, head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Jiri Slabyb4173f42008-04-30 00:53:40 -07002041 ofsAddr = port->tableAddr;
2042 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 head = readw(ofsAddr + RXrptr);
2044 tail = readw(ofsAddr + RXwptr);
2045 rx_mask = readw(ofsAddr + RX_mask);
2046 spage = readw(ofsAddr + Page_rxb);
2047 epage = readw(ofsAddr + EndPage_rxb);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002048 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (count == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08002050 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Alan Cox33f0f882006-01-09 20:54:13 -08002052 total = count;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002053 moxaLog.rxcnt[tty->index] += total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 if (spage == epage) {
2055 bufhead = readw(ofsAddr + Ofs_rxb);
2056 writew(spage, baseAddr + Control_reg);
2057 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 ofs = baseAddr + DynPage_addr + bufhead + head;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002059 len = (tail >= head) ? (tail - head) :
2060 (rx_mask + 1 - head);
2061 len = tty_prepare_flip_string(tty, &dst,
2062 min(len, count));
2063 memcpy_fromio(dst, ofs, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 head = (head + len) & rx_mask;
2065 count -= len;
2066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 pageno = spage + (head >> 13);
2069 pageofs = head & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002070 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 writew(pageno, baseAddr + Control_reg);
2072 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002073 len = tty_prepare_flip_string(tty, &dst,
2074 min(Page_size - pageofs, count));
2075 memcpy_fromio(dst, ofs, len);
2076
2077 count -= len;
2078 pageofs = (pageofs + len) & Page_mask;
2079 if (pageofs == 0 && ++pageno == epage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 pageno = spage;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002081 }
2082 head = (head + total) & rx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002084 writew(head, ofsAddr + RXrptr);
2085 if (readb(ofsAddr + FlagStat) & Xoff_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 moxaLowWaterChk = 1;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002087 port->lowChkFlag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002089 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090}
2091
2092
Jiri Slabyb4173f42008-04-30 00:53:40 -07002093static int MoxaPortTxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002095 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002096 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 rptr = readw(ofsAddr + TXrptr);
2099 wptr = readw(ofsAddr + TXwptr);
2100 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002101 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102}
2103
Jiri Slabyb4173f42008-04-30 00:53:40 -07002104static int MoxaPortTxFree(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002106 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002107 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 rptr = readw(ofsAddr + TXrptr);
2110 wptr = readw(ofsAddr + TXwptr);
2111 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002112 return mask - ((wptr - rptr) & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
2114
Jiri Slabyb4173f42008-04-30 00:53:40 -07002115static int MoxaPortRxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002117 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002118 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 rptr = readw(ofsAddr + RXrptr);
2121 wptr = readw(ofsAddr + RXwptr);
2122 mask = readw(ofsAddr + RX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002123 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124}
2125
Jiri Slabyb4173f42008-04-30 00:53:40 -07002126static void MoxaPortTxDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002128 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129}
2130
Jiri Slabyb4173f42008-04-30 00:53:40 -07002131static void MoxaPortTxEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002133 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
2135
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002136static int moxa_get_serial_info(struct moxa_port *info,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002137 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002139 struct serial_struct tmp = {
2140 .type = info->type,
2141 .line = info->tty->index,
2142 .flags = info->asyncflags,
2143 .baud_base = 921600,
2144 .close_delay = info->close_delay
2145 };
2146 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
2149
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002150static int moxa_set_serial_info(struct moxa_port *info,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002151 struct serial_struct __user *new_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
2153 struct serial_struct new_serial;
2154
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002155 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 return -EFAULT;
2157
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002158 if (new_serial.irq != 0 || new_serial.port != 0 ||
2159 new_serial.custom_divisor != 0 ||
2160 new_serial.baud_base != 921600)
2161 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163 if (!capable(CAP_SYS_ADMIN)) {
2164 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2165 (info->asyncflags & ~ASYNC_USR_MASK)))
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002166 return -EPERM;
2167 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 info->close_delay = new_serial.close_delay * HZ / 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2171 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2172
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002173 MoxaSetFifo(info, new_serial.type == PORT_16550A);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175 info->type = new_serial.type;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
2178
2179
2180
2181/*****************************************************************************
2182 * Static local functions: *
2183 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Jiri Slabyb4173f42008-04-30 00:53:40 -07002185static void MoxaSetFifo(struct moxa_port *port, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002187 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 if (!enable) {
2190 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2191 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2192 } else {
2193 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2194 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2195 }
2196}