blob: d57d3a61919b310f22e972e97cf1c8797334db1d [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 Slaby08d01c72008-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 {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700133 struct moxa_board_conf *board;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700134 struct tty_struct *tty;
135 void __iomem *tableAddr;
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 int close_delay;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700139 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 int asyncflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 int cflag;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700142 unsigned long statusflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 wait_queue_head_t open_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700145 u8 DCDState;
146 u8 lineCtrl;
147 u8 lowChkFlag;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800148};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Jiri Slaby74d7d972008-04-30 00:53:43 -0700150struct mon_str {
151 int tick;
152 int rxcnt[MAX_PORTS];
153 int txcnt[MAX_PORTS];
154};
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/* statusflags */
157#define TXSTOPPED 0x1
158#define LOWWAIT 0x2
159#define EMPTYWAIT 0x4
160#define THROTTLE 0x8
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#define SERIAL_DO_RESTART
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#define WAKEUP_CHARS 256
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static int ttymajor = MOXAMAJOR;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700167static struct mon_str moxaLog;
168static unsigned int moxaFuncTout = HZ / 2;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700169static unsigned int moxaLowWaterChk;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700170static DEFINE_MUTEX(moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/* Variables for insmod */
172#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700173static unsigned long baseaddr[MAX_BOARDS];
174static unsigned int type[MAX_BOARDS];
175static unsigned int numports[MAX_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#endif
177
178MODULE_AUTHOR("William Chen");
179MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
180MODULE_LICENSE("GPL");
181#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700182module_param_array(type, uint, NULL, 0);
183MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
184module_param_array(baseaddr, ulong, NULL, 0);
185MODULE_PARM_DESC(baseaddr, "base address");
186module_param_array(numports, uint, NULL, 0);
187MODULE_PARM_DESC(numports, "numports (ignored for C218)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188#endif
189module_param(ttymajor, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/*
192 * static functions:
193 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194static int moxa_open(struct tty_struct *, struct file *);
195static void moxa_close(struct tty_struct *, struct file *);
196static int moxa_write(struct tty_struct *, const unsigned char *, int);
197static int moxa_write_room(struct tty_struct *);
198static void moxa_flush_buffer(struct tty_struct *);
199static int moxa_chars_in_buffer(struct tty_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200static void moxa_throttle(struct tty_struct *);
201static void moxa_unthrottle(struct tty_struct *);
Alan Cox606d0992006-12-08 02:38:45 -0800202static void moxa_set_termios(struct tty_struct *, struct ktermios *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203static void moxa_stop(struct tty_struct *);
204static void moxa_start(struct tty_struct *);
205static void moxa_hangup(struct tty_struct *);
206static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
207static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
208 unsigned int set, unsigned int clear);
209static void moxa_poll(unsigned long);
Alan Coxdb1acaa2008-02-08 04:18:43 -0800210static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
Jiri Slaby6f56b6582007-10-18 03:06:24 -0700211static void moxa_setup_empty_event(struct tty_struct *);
Jiri Slaby6f56b6582007-10-18 03:06:24 -0700212static void moxa_shut_down(struct moxa_port *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/*
214 * moxa board interface functions:
215 */
Jiri Slabyb4173f42008-04-30 00:53:40 -0700216static void MoxaPortEnable(struct moxa_port *);
217static void MoxaPortDisable(struct moxa_port *);
218static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
219static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
220static void MoxaPortLineCtrl(struct moxa_port *, int, int);
221static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
222static int MoxaPortLineStatus(struct moxa_port *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700223static void MoxaPortFlushData(struct moxa_port *, int);
Jiri Slaby2108eba2008-04-30 00:53:44 -0700224static int MoxaPortWriteData(struct moxa_port *, const unsigned char *, int);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700225static int MoxaPortReadData(struct moxa_port *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700226static int MoxaPortTxQueue(struct moxa_port *);
227static int MoxaPortRxQueue(struct moxa_port *);
228static int MoxaPortTxFree(struct moxa_port *);
229static void MoxaPortTxDisable(struct moxa_port *);
230static void MoxaPortTxEnable(struct moxa_port *);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800231static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
232static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700233static void MoxaSetFifo(struct moxa_port *port, int enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Jiri Slaby74d7d972008-04-30 00:53:43 -0700235/*
236 * I/O functions
237 */
238
239static void moxa_wait_finish(void __iomem *ofsAddr)
240{
241 unsigned long end = jiffies + moxaFuncTout;
242
243 while (readw(ofsAddr + FuncCode) != 0)
244 if (time_after(jiffies, end))
245 return;
246 if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
247 printk(KERN_WARNING "moxa function expired\n");
248}
249
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700250static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
Jiri Slaby74d7d972008-04-30 00:53:43 -0700251{
252 writew(arg, ofsAddr + FuncArg);
253 writew(cmd, ofsAddr + FuncCode);
254 moxa_wait_finish(ofsAddr);
255}
256
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700257static void moxa_low_water_check(void __iomem *ofsAddr)
258{
259 u16 rptr, wptr, mask, len;
260
261 if (readb(ofsAddr + FlagStat) & Xoff_state) {
262 rptr = readw(ofsAddr + RXrptr);
263 wptr = readw(ofsAddr + RXwptr);
264 mask = readw(ofsAddr + RX_mask);
265 len = (wptr - rptr) & mask;
266 if (len <= Low_water)
267 moxafunc(ofsAddr, FC_SendXon, 0);
268 }
269}
270
Jiri Slaby74d7d972008-04-30 00:53:43 -0700271/*
272 * TTY operations
273 */
274
275static int moxa_ioctl(struct tty_struct *tty, struct file *file,
276 unsigned int cmd, unsigned long arg)
277{
278 struct moxa_port *ch = tty->driver_data;
279 void __user *argp = (void __user *)arg;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700280 int status, ret = 0;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700281
282 if (tty->index == MAX_PORTS) {
283 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
284 cmd != MOXA_GETMSTATUS)
285 return -EINVAL;
286 } else if (!ch)
287 return -ENODEV;
288
289 switch (cmd) {
290 case MOXA_GETDATACOUNT:
291 moxaLog.tick = jiffies;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700292 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
293 ret = -EFAULT;
294 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700295 case MOXA_FLUSH_QUEUE:
296 MoxaPortFlushData(ch, arg);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700297 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700298 case MOXA_GET_IOQUEUE: {
299 struct moxaq_str __user *argm = argp;
300 struct moxaq_str tmp;
301 struct moxa_port *p;
302 unsigned int i, j;
303
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700304 mutex_lock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700305 for (i = 0; i < MAX_BOARDS; i++) {
306 p = moxa_boards[i].ports;
307 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
308 memset(&tmp, 0, sizeof(tmp));
309 if (moxa_boards[i].ready) {
310 tmp.inq = MoxaPortRxQueue(p);
311 tmp.outq = MoxaPortTxQueue(p);
312 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700313 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
314 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700315 return -EFAULT;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700316 }
Jiri Slaby74d7d972008-04-30 00:53:43 -0700317 }
318 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700319 mutex_unlock(&moxa_openlock);
320 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700321 } case MOXA_GET_OQUEUE:
322 status = MoxaPortTxQueue(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_GET_IQUEUE:
326 status = MoxaPortRxQueue(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700327 ret = put_user(status, (unsigned long __user *)argp);
328 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700329 case MOXA_GETMSTATUS: {
330 struct mxser_mstatus __user *argm = argp;
331 struct mxser_mstatus tmp;
332 struct moxa_port *p;
333 unsigned int i, j;
334
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700335 mutex_lock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700336 for (i = 0; i < MAX_BOARDS; i++) {
337 p = moxa_boards[i].ports;
338 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
339 memset(&tmp, 0, sizeof(tmp));
340 if (!moxa_boards[i].ready)
341 goto copy;
342
343 status = MoxaPortLineStatus(p);
344 if (status & 1)
345 tmp.cts = 1;
346 if (status & 2)
347 tmp.dsr = 1;
348 if (status & 4)
349 tmp.dcd = 1;
350
351 if (!p->tty || !p->tty->termios)
352 tmp.cflag = p->cflag;
353 else
354 tmp.cflag = p->tty->termios->c_cflag;
355copy:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700356 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
357 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700358 return -EFAULT;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700359 }
Jiri Slaby74d7d972008-04-30 00:53:43 -0700360 }
361 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700362 mutex_unlock(&moxa_openlock);
363 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700364 }
365 case TIOCGSERIAL:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700366 mutex_lock(&moxa_openlock);
367 ret = moxa_get_serial_info(ch, argp);
368 mutex_unlock(&moxa_openlock);
369 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700370 case TIOCSSERIAL:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700371 mutex_lock(&moxa_openlock);
372 ret = moxa_set_serial_info(ch, argp);
373 mutex_unlock(&moxa_openlock);
374 break;
375 default:
376 ret = -ENOIOCTLCMD;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700377 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700378 return ret;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700379}
380
381static void moxa_break_ctl(struct tty_struct *tty, int state)
382{
383 struct moxa_port *port = tty->driver_data;
384
385 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
386 Magic_code);
387}
388
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700389static const struct tty_operations moxa_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 .open = moxa_open,
391 .close = moxa_close,
392 .write = moxa_write,
393 .write_room = moxa_write_room,
394 .flush_buffer = moxa_flush_buffer,
395 .chars_in_buffer = moxa_chars_in_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 .ioctl = moxa_ioctl,
397 .throttle = moxa_throttle,
398 .unthrottle = moxa_unthrottle,
399 .set_termios = moxa_set_termios,
400 .stop = moxa_stop,
401 .start = moxa_start,
402 .hangup = moxa_hangup,
Jiri Slaby74d7d972008-04-30 00:53:43 -0700403 .break_ctl = moxa_break_ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 .tiocmget = moxa_tiocmget,
405 .tiocmset = moxa_tiocmset,
406};
407
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800408static struct tty_driver *moxaDriver;
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800409static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
Ingo Molnar34af9462006-06-27 02:53:55 -0700410static DEFINE_SPINLOCK(moxa_lock);
Alan Cox33f0f882006-01-09 20:54:13 -0800411
Jiri Slaby74d7d972008-04-30 00:53:43 -0700412/*
413 * HW init
414 */
415
Jiri Slaby03718232008-04-30 00:53:39 -0700416static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
417{
418 switch (brd->boardType) {
419 case MOXA_BOARD_C218_ISA:
420 case MOXA_BOARD_C218_PCI:
421 if (model != 1)
422 goto err;
423 break;
424 case MOXA_BOARD_CP204J:
425 if (model != 3)
426 goto err;
427 break;
428 default:
429 if (model != 2)
430 goto err;
431 break;
432 }
433 return 0;
434err:
435 return -EINVAL;
436}
437
438static int moxa_check_fw(const void *ptr)
439{
440 const __le16 *lptr = ptr;
441
442 if (*lptr != cpu_to_le16(0x7980))
443 return -EINVAL;
444
445 return 0;
446}
447
448static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
449 size_t len)
450{
451 void __iomem *baseAddr = brd->basemem;
452 u16 tmp;
453
454 writeb(HW_reset, baseAddr + Control_reg); /* reset */
455 msleep(10);
456 memset_io(baseAddr, 0, 4096);
457 memcpy_toio(baseAddr, buf, len); /* download BIOS */
458 writeb(0, baseAddr + Control_reg); /* restart */
459
460 msleep(2000);
461
462 switch (brd->boardType) {
463 case MOXA_BOARD_C218_ISA:
464 case MOXA_BOARD_C218_PCI:
465 tmp = readw(baseAddr + C218_key);
466 if (tmp != C218_KeyCode)
467 goto err;
468 break;
469 case MOXA_BOARD_CP204J:
470 tmp = readw(baseAddr + C218_key);
471 if (tmp != CP204J_KeyCode)
472 goto err;
473 break;
474 default:
475 tmp = readw(baseAddr + C320_key);
476 if (tmp != C320_KeyCode)
477 goto err;
478 tmp = readw(baseAddr + C320_status);
479 if (tmp != STS_init) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700480 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
Jiri Slaby03718232008-04-30 00:53:39 -0700481 "module not found\n");
482 return -EIO;
483 }
484 break;
485 }
486
487 return 0;
488err:
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700489 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700490 return -EIO;
491}
492
493static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
494 size_t len)
495{
496 void __iomem *baseAddr = brd->basemem;
497
498 if (len < 7168) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700499 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700500 return -EINVAL;
501 }
502
503 writew(len - 7168 - 2, baseAddr + C320bapi_len);
504 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
505 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
506 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
507 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
508
509 return 0;
510}
511
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700512static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
Jiri Slaby03718232008-04-30 00:53:39 -0700513 size_t len)
514{
515 void __iomem *baseAddr = brd->basemem;
516 const u16 *uptr = ptr;
517 size_t wlen, len2, j;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700518 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
Jiri Slaby08d01c72008-04-30 00:53:47 -0700519 unsigned int i, retry;
Jiri Slaby03718232008-04-30 00:53:39 -0700520 u16 usum, keycode;
521
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700522 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
523 C218_KeyCode;
524
525 switch (brd->boardType) {
526 case MOXA_BOARD_CP204J:
527 case MOXA_BOARD_C218_ISA:
528 case MOXA_BOARD_C218_PCI:
529 key = C218_key;
530 loadbuf = C218_LoadBuf;
531 loadlen = C218DLoad_len;
532 checksum = C218check_sum;
533 checksum_ok = C218chksum_ok;
534 break;
535 default:
536 key = C320_key;
537 keycode = C320_KeyCode;
538 loadbuf = C320_LoadBuf;
539 loadlen = C320DLoad_len;
540 checksum = C320check_sum;
541 checksum_ok = C320chksum_ok;
542 break;
543 }
544
Jiri Slaby03718232008-04-30 00:53:39 -0700545 usum = 0;
546 wlen = len >> 1;
547 for (i = 0; i < wlen; i++)
548 usum += le16_to_cpu(uptr[i]);
549 retry = 0;
550 do {
551 wlen = len >> 1;
552 j = 0;
553 while (wlen) {
554 len2 = (wlen > 2048) ? 2048 : wlen;
555 wlen -= len2;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700556 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
Jiri Slaby03718232008-04-30 00:53:39 -0700557 j += len2 << 1;
558
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700559 writew(len2, baseAddr + loadlen);
560 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700561 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700562 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700563 break;
564 msleep(10);
565 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700566 if (readw(baseAddr + key) != keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700567 return -EIO;
568 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700569 writew(0, baseAddr + loadlen);
570 writew(usum, baseAddr + checksum);
571 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700572 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700573 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700574 break;
575 msleep(10);
576 }
577 retry++;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700578 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
579 if (readb(baseAddr + checksum_ok) != 1)
Jiri Slaby03718232008-04-30 00:53:39 -0700580 return -EIO;
581
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700582 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700583 for (i = 0; i < 600; i++) {
584 if (readw(baseAddr + Magic_no) == Magic_code)
585 break;
586 msleep(10);
587 }
588 if (readw(baseAddr + Magic_no) != Magic_code)
589 return -EIO;
590
Jiri Slaby08d01c72008-04-30 00:53:47 -0700591 if (MOXA_IS_320(brd)) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700592 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
593 writew(0x3800, baseAddr + TMS320_PORT1);
594 writew(0x3900, baseAddr + TMS320_PORT2);
595 writew(28499, baseAddr + TMS320_CLOCK);
596 } else {
597 writew(0x3200, baseAddr + TMS320_PORT1);
598 writew(0x3400, baseAddr + TMS320_PORT2);
599 writew(19999, baseAddr + TMS320_CLOCK);
600 }
Jiri Slaby03718232008-04-30 00:53:39 -0700601 }
602 writew(1, baseAddr + Disable_IRQ);
603 writew(0, baseAddr + Magic_no);
604 for (i = 0; i < 500; i++) {
605 if (readw(baseAddr + Magic_no) == Magic_code)
606 break;
607 msleep(10);
608 }
609 if (readw(baseAddr + Magic_no) != Magic_code)
610 return -EIO;
611
Jiri Slaby08d01c72008-04-30 00:53:47 -0700612 if (MOXA_IS_320(brd)) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700613 j = readw(baseAddr + Module_cnt);
614 if (j <= 0)
615 return -EIO;
616 brd->numPorts = j * 8;
617 writew(j, baseAddr + Module_no);
618 writew(0, baseAddr + Magic_no);
619 for (i = 0; i < 600; i++) {
620 if (readw(baseAddr + Magic_no) == Magic_code)
621 break;
622 msleep(10);
623 }
624 if (readw(baseAddr + Magic_no) != Magic_code)
625 return -EIO;
Jiri Slaby03718232008-04-30 00:53:39 -0700626 }
Jiri Slaby03718232008-04-30 00:53:39 -0700627 brd->intNdx = baseAddr + IRQindex;
628 brd->intPend = baseAddr + IRQpending;
629 brd->intTable = baseAddr + IRQtable;
630
631 return 0;
632}
633
634static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
635 size_t len)
636{
637 void __iomem *ofsAddr, *baseAddr = brd->basemem;
638 struct moxa_port *port;
639 int retval, i;
640
641 if (len % 2) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700642 printk(KERN_ERR "MOXA: bios length is not even\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700643 return -EINVAL;
644 }
645
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700646 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
647 if (retval)
648 return retval;
649
Jiri Slaby03718232008-04-30 00:53:39 -0700650 switch (brd->boardType) {
651 case MOXA_BOARD_C218_ISA:
652 case MOXA_BOARD_C218_PCI:
653 case MOXA_BOARD_CP204J:
Jiri Slaby03718232008-04-30 00:53:39 -0700654 port = brd->ports;
655 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700656 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700657 port->DCDState = 0;
658 port->tableAddr = baseAddr + Extern_table +
659 Extern_size * i;
660 ofsAddr = port->tableAddr;
661 writew(C218rx_mask, ofsAddr + RX_mask);
662 writew(C218tx_mask, ofsAddr + TX_mask);
663 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
664 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
665
666 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
667 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
668
669 }
670 break;
671 default:
Jiri Slaby03718232008-04-30 00:53:39 -0700672 port = brd->ports;
673 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700674 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700675 port->DCDState = 0;
676 port->tableAddr = baseAddr + Extern_table +
677 Extern_size * i;
678 ofsAddr = port->tableAddr;
679 switch (brd->numPorts) {
680 case 8:
681 writew(C320p8rx_mask, ofsAddr + RX_mask);
682 writew(C320p8tx_mask, ofsAddr + TX_mask);
683 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
684 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
685 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
686 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
687
688 break;
689 case 16:
690 writew(C320p16rx_mask, ofsAddr + RX_mask);
691 writew(C320p16tx_mask, ofsAddr + TX_mask);
692 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
693 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
694 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
695 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
696 break;
697
698 case 24:
699 writew(C320p24rx_mask, ofsAddr + RX_mask);
700 writew(C320p24tx_mask, ofsAddr + TX_mask);
701 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
702 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
703 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
704 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
705 break;
706 case 32:
707 writew(C320p32rx_mask, ofsAddr + RX_mask);
708 writew(C320p32tx_mask, ofsAddr + TX_mask);
709 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
710 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
711 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
712 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
713 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
714 break;
715 }
716 }
717 break;
718 }
Jiri Slaby03718232008-04-30 00:53:39 -0700719 return 0;
720}
721
722static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
723{
724 void *ptr = fw->data;
725 char rsn[64];
726 u16 lens[5];
727 size_t len;
728 unsigned int a, lenp, lencnt;
729 int ret = -EINVAL;
730 struct {
731 __le32 magic; /* 0x34303430 */
732 u8 reserved1[2];
733 u8 type; /* UNIX = 3 */
734 u8 model; /* C218T=1, C320T=2, CP204=3 */
735 u8 reserved2[8];
736 __le16 len[5];
737 } *hdr = ptr;
738
739 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
740
741 if (fw->size < MOXA_FW_HDRLEN) {
742 strcpy(rsn, "too short (even header won't fit)");
743 goto err;
744 }
745 if (hdr->magic != cpu_to_le32(0x30343034)) {
746 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
747 goto err;
748 }
749 if (hdr->type != 3) {
750 sprintf(rsn, "not for linux, type is %u", hdr->type);
751 goto err;
752 }
753 if (moxa_check_fw_model(brd, hdr->model)) {
754 sprintf(rsn, "not for this card, model is %u", hdr->model);
755 goto err;
756 }
757
758 len = MOXA_FW_HDRLEN;
759 lencnt = hdr->model == 2 ? 5 : 3;
760 for (a = 0; a < ARRAY_SIZE(lens); a++) {
761 lens[a] = le16_to_cpu(hdr->len[a]);
762 if (lens[a] && len + lens[a] <= fw->size &&
763 moxa_check_fw(&fw->data[len]))
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700764 printk(KERN_WARNING "MOXA firmware: unexpected input "
Jiri Slaby03718232008-04-30 00:53:39 -0700765 "at offset %u, but going on\n", (u32)len);
766 if (!lens[a] && a < lencnt) {
767 sprintf(rsn, "too few entries in fw file");
768 goto err;
769 }
770 len += lens[a];
771 }
772
773 if (len != fw->size) {
774 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
775 (u32)len);
776 goto err;
777 }
778
779 ptr += MOXA_FW_HDRLEN;
780 lenp = 0; /* bios */
781
782 strcpy(rsn, "read above");
783
784 ret = moxa_load_bios(brd, ptr, lens[lenp]);
785 if (ret)
786 goto err;
787
788 /* we skip the tty section (lens[1]), since we don't need it */
789 ptr += lens[lenp] + lens[lenp + 1];
790 lenp += 2; /* comm */
791
792 if (hdr->model == 2) {
793 ret = moxa_load_320b(brd, ptr, lens[lenp]);
794 if (ret)
795 goto err;
796 /* skip another tty */
797 ptr += lens[lenp] + lens[lenp + 1];
798 lenp += 2;
799 }
800
801 ret = moxa_load_code(brd, ptr, lens[lenp]);
802 if (ret)
803 goto err;
804
805 return 0;
806err:
807 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
808 return ret;
809}
810
811static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
812{
813 const struct firmware *fw;
814 const char *file;
Jiri Slaby810ab092008-04-30 00:53:41 -0700815 struct moxa_port *p;
816 unsigned int i;
Jiri Slaby03718232008-04-30 00:53:39 -0700817 int ret;
818
Jiri Slaby810ab092008-04-30 00:53:41 -0700819 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
820 GFP_KERNEL);
821 if (brd->ports == NULL) {
822 printk(KERN_ERR "cannot allocate memory for ports\n");
823 ret = -ENOMEM;
824 goto err;
825 }
826
827 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
828 p->type = PORT_16550A;
829 p->close_delay = 5 * HZ / 10;
830 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
831 init_waitqueue_head(&p->open_wait);
Jiri Slaby810ab092008-04-30 00:53:41 -0700832 }
833
Jiri Slaby03718232008-04-30 00:53:39 -0700834 switch (brd->boardType) {
835 case MOXA_BOARD_C218_ISA:
836 case MOXA_BOARD_C218_PCI:
837 file = "c218tunx.cod";
838 break;
839 case MOXA_BOARD_CP204J:
840 file = "cp204unx.cod";
841 break;
842 default:
843 file = "c320tunx.cod";
844 break;
845 }
846
847 ret = request_firmware(&fw, file, dev);
848 if (ret) {
Jiri Slabyec09cd52008-04-30 00:53:49 -0700849 printk(KERN_ERR "MOXA: request_firmware failed. Make sure "
850 "you've placed '%s' file into your firmware "
851 "loader directory (e.g. /lib/firmware)\n",
852 file);
Jiri Slaby810ab092008-04-30 00:53:41 -0700853 goto err_free;
Jiri Slaby03718232008-04-30 00:53:39 -0700854 }
855
856 ret = moxa_load_fw(brd, fw);
857
858 release_firmware(fw);
Jiri Slaby810ab092008-04-30 00:53:41 -0700859
860 if (ret)
861 goto err_free;
862
Jiri Slaby2a541342008-04-30 00:53:45 -0700863 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700864 brd->ready = 1;
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700865 if (!timer_pending(&moxaTimer))
866 mod_timer(&moxaTimer, jiffies + HZ / 50);
Jiri Slaby2a541342008-04-30 00:53:45 -0700867 spin_unlock_bh(&moxa_lock);
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700868
Jiri Slaby810ab092008-04-30 00:53:41 -0700869 return 0;
870err_free:
871 kfree(brd->ports);
872err:
Jiri Slaby03718232008-04-30 00:53:39 -0700873 return ret;
874}
875
Jiri Slaby810ab092008-04-30 00:53:41 -0700876static void moxa_board_deinit(struct moxa_board_conf *brd)
877{
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700878 unsigned int a, opened;
879
880 mutex_lock(&moxa_openlock);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700881 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700882 brd->ready = 0;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700883 spin_unlock_bh(&moxa_lock);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700884
885 /* pci hot-un-plug support */
886 for (a = 0; a < brd->numPorts; a++)
887 if (brd->ports[a].asyncflags & ASYNC_INITIALIZED)
888 tty_hangup(brd->ports[a].tty);
889 while (1) {
890 opened = 0;
891 for (a = 0; a < brd->numPorts; a++)
892 if (brd->ports[a].asyncflags & ASYNC_INITIALIZED)
893 opened++;
894 mutex_unlock(&moxa_openlock);
895 if (!opened)
896 break;
897 msleep(50);
898 mutex_lock(&moxa_openlock);
899 }
900
Jiri Slaby810ab092008-04-30 00:53:41 -0700901 iounmap(brd->basemem);
902 brd->basemem = NULL;
903 kfree(brd->ports);
904}
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906#ifdef CONFIG_PCI
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800907static int __devinit moxa_pci_probe(struct pci_dev *pdev,
908 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800910 struct moxa_board_conf *board;
911 unsigned int i;
912 int board_type = ent->driver_data;
913 int retval;
914
915 retval = pci_enable_device(pdev);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700916 if (retval) {
917 dev_err(&pdev->dev, "can't enable pci device\n");
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800918 goto err;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700919 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800920
921 for (i = 0; i < MAX_BOARDS; i++)
922 if (moxa_boards[i].basemem == NULL)
923 break;
924
925 retval = -ENODEV;
926 if (i >= MAX_BOARDS) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700927 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800928 "found. Board is ignored.\n", MAX_BOARDS);
929 goto err;
930 }
931
932 board = &moxa_boards[i];
Jiri Slabye46a5e32008-04-30 00:53:37 -0700933
934 retval = pci_request_region(pdev, 2, "moxa-base");
935 if (retval) {
936 dev_err(&pdev->dev, "can't request pci region 2\n");
937 goto err;
938 }
939
Alan Cox24cb2332008-04-30 00:54:19 -0700940 board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700941 if (board->basemem == NULL) {
942 dev_err(&pdev->dev, "can't remap io space 2\n");
Jiri Slabye46a5e32008-04-30 00:53:37 -0700943 goto err_reg;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700944 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 board->boardType = board_type;
947 switch (board_type) {
948 case MOXA_BOARD_C218_ISA:
949 case MOXA_BOARD_C218_PCI:
950 board->numPorts = 8;
951 break;
952
953 case MOXA_BOARD_CP204J:
954 board->numPorts = 4;
955 break;
956 default:
957 board->numPorts = 0;
958 break;
959 }
960 board->busType = MOXA_BUS_TYPE_PCI;
Jiri Slabya784bf72007-02-10 01:45:36 -0800961
Jiri Slaby03718232008-04-30 00:53:39 -0700962 retval = moxa_init_board(board, &pdev->dev);
963 if (retval)
964 goto err_base;
965
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800966 pci_set_drvdata(pdev, board);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Jiri Slabybb9f9102008-04-30 00:53:48 -0700968 dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n",
969 moxa_brdname[board_type - 1], board->numPorts);
970
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700971 return 0;
Jiri Slaby03718232008-04-30 00:53:39 -0700972err_base:
973 iounmap(board->basemem);
974 board->basemem = NULL;
Jiri Slabye46a5e32008-04-30 00:53:37 -0700975err_reg:
976 pci_release_region(pdev, 2);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800977err:
978 return retval;
979}
980
981static void __devexit moxa_pci_remove(struct pci_dev *pdev)
982{
983 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
984
Jiri Slaby810ab092008-04-30 00:53:41 -0700985 moxa_board_deinit(brd);
986
Jiri Slabye46a5e32008-04-30 00:53:37 -0700987 pci_release_region(pdev, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
Jiri Slabya784bf72007-02-10 01:45:36 -0800989
990static struct pci_driver moxa_pci_driver = {
991 .name = "moxa",
992 .id_table = moxa_pcibrds,
993 .probe = moxa_pci_probe,
994 .remove = __devexit_p(moxa_pci_remove)
995};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996#endif /* CONFIG_PCI */
997
998static int __init moxa_init(void)
999{
Jiri Slaby810ab092008-04-30 00:53:41 -07001000 unsigned int isabrds = 0;
Jiri Slabyd353eca2008-04-30 00:53:37 -07001001 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001003 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
1004 MOXA_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
1006 if (!moxaDriver)
1007 return -ENOMEM;
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 moxaDriver->owner = THIS_MODULE;
Sergey Vlasov9b4e3b12005-09-03 16:26:49 +01001010 moxaDriver->name = "ttyMX";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 moxaDriver->major = ttymajor;
1012 moxaDriver->minor_start = 0;
1013 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1014 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1015 moxaDriver->init_termios = tty_std_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Alan Cox606d0992006-12-08 02:38:45 -08001017 moxaDriver->init_termios.c_ispeed = 9600;
1018 moxaDriver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
1020 tty_set_operations(moxaDriver, &moxa_ops);
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if (tty_register_driver(moxaDriver)) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001023 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 put_tty_driver(moxaDriver);
1025 return -1;
1026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Jiri Slabyd353eca2008-04-30 00:53:37 -07001028 /* Find the boards defined from module args. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -07001030 {
1031 struct moxa_board_conf *brd = moxa_boards;
Jiri Slaby810ab092008-04-30 00:53:41 -07001032 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 for (i = 0; i < MAX_BOARDS; i++) {
Jiri Slabyd353eca2008-04-30 00:53:37 -07001034 if (!baseaddr[i])
1035 break;
1036 if (type[i] == MOXA_BOARD_C218_ISA ||
1037 type[i] == MOXA_BOARD_C320_ISA) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001038 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001039 isabrds + 1, moxa_brdname[type[i] - 1],
1040 baseaddr[i]);
1041 brd->boardType = type[i];
1042 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1043 numports[i];
1044 brd->busType = MOXA_BUS_TYPE_ISA;
Alan Cox24cb2332008-04-30 00:54:19 -07001045 brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
Jiri Slabyd353eca2008-04-30 00:53:37 -07001046 if (!brd->basemem) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001047 printk(KERN_ERR "MOXA: can't remap %lx\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001048 baseaddr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 continue;
1050 }
Jiri Slaby03718232008-04-30 00:53:39 -07001051 if (moxa_init_board(brd, NULL)) {
1052 iounmap(brd->basemem);
1053 brd->basemem = NULL;
1054 continue;
1055 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001056
Jiri Slabybb9f9102008-04-30 00:53:48 -07001057 printk(KERN_INFO "MOXA isa board found at 0x%.8lu and "
1058 "ready (%u ports, firmware loaded)\n",
1059 baseaddr[i], brd->numPorts);
1060
Jiri Slabyd353eca2008-04-30 00:53:37 -07001061 brd++;
1062 isabrds++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
1064 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001069 retval = pci_register_driver(&moxa_pci_driver);
1070 if (retval) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001071 printk(KERN_ERR "Can't register MOXA pci driver!\n");
Jiri Slabyd353eca2008-04-30 00:53:37 -07001072 if (isabrds)
Jiri Slabya784bf72007-02-10 01:45:36 -08001073 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
1075#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001076
Jiri Slabya784bf72007-02-10 01:45:36 -08001077 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
1080static void __exit moxa_exit(void)
1081{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001082 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001084#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001085 pci_unregister_driver(&moxa_pci_driver);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001086#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001087
Jiri Slaby810ab092008-04-30 00:53:41 -07001088 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1089 if (moxa_boards[i].ready)
1090 moxa_board_deinit(&moxa_boards[i]);
Jiri Slaby2a541342008-04-30 00:53:45 -07001091
1092 del_timer_sync(&moxaTimer);
1093
1094 if (tty_unregister_driver(moxaDriver))
1095 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1096 "serial driver\n");
1097 put_tty_driver(moxaDriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
1100module_init(moxa_init);
1101module_exit(moxa_exit);
1102
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001103static void moxa_close_port(struct moxa_port *ch)
1104{
1105 moxa_shut_down(ch);
1106 MoxaPortFlushData(ch, 2);
1107 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1108 ch->tty->driver_data = NULL;
1109 ch->tty = NULL;
1110}
1111
1112static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
1113 struct moxa_port *ch)
1114{
1115 DEFINE_WAIT(wait);
1116 int retval = 0;
1117 u8 dcd;
1118
1119 while (1) {
1120 prepare_to_wait(&ch->open_wait, &wait, TASK_INTERRUPTIBLE);
1121 if (tty_hung_up_p(filp)) {
1122#ifdef SERIAL_DO_RESTART
1123 retval = -ERESTARTSYS;
1124#else
1125 retval = -EAGAIN;
1126#endif
1127 break;
1128 }
1129 spin_lock_bh(&moxa_lock);
1130 dcd = ch->DCDState;
1131 spin_unlock_bh(&moxa_lock);
1132 if (dcd)
1133 break;
1134
1135 if (signal_pending(current)) {
1136 retval = -ERESTARTSYS;
1137 break;
1138 }
1139 schedule();
1140 }
1141 finish_wait(&ch->open_wait, &wait);
1142
1143 return retval;
1144}
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146static int moxa_open(struct tty_struct *tty, struct file *filp)
1147{
Jiri Slaby810ab092008-04-30 00:53:41 -07001148 struct moxa_board_conf *brd;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001149 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 int port;
1151 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Jiri Slaby11324ed2007-02-10 01:45:31 -08001153 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (port == MAX_PORTS) {
Jiri Slaby74d7d972008-04-30 00:53:43 -07001155 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001157 if (mutex_lock_interruptible(&moxa_openlock))
1158 return -ERESTARTSYS;
Jiri Slaby810ab092008-04-30 00:53:41 -07001159 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001160 if (!brd->ready) {
1161 mutex_unlock(&moxa_openlock);
Jiri Slaby810ab092008-04-30 00:53:41 -07001162 return -ENODEV;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Jiri Slaby810ab092008-04-30 00:53:41 -07001165 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 ch->count++;
1167 tty->driver_data = ch;
1168 ch->tty = tty;
1169 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
1170 ch->statusflags = 0;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001171 moxa_set_tty_param(tty, tty->termios);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001172 MoxaPortLineCtrl(ch, 1, 1);
1173 MoxaPortEnable(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001174 MoxaSetFifo(ch, ch->type == PORT_16550A);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 ch->asyncflags |= ASYNC_INITIALIZED;
1176 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001177 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001179 retval = 0;
1180 if (!(filp->f_flags & O_NONBLOCK) && !C_CLOCAL(tty))
1181 retval = moxa_block_till_ready(tty, filp, ch);
1182 mutex_lock(&moxa_openlock);
1183 if (retval) {
1184 if (ch->count) /* 0 means already hung up... */
1185 if (--ch->count == 0)
1186 moxa_close_port(ch);
1187 } else
1188 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1189 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001191 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
1194static void moxa_close(struct tty_struct *tty, struct file *filp)
1195{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001196 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 int port;
1198
Jiri Slaby11324ed2007-02-10 01:45:31 -08001199 port = tty->index;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001200 if (port == MAX_PORTS || tty_hung_up_p(filp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001203 mutex_lock(&moxa_openlock);
1204 ch = tty->driver_data;
1205 if (ch == NULL)
1206 goto unlock;
1207 if (tty->count == 1 && ch->count != 1) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001208 printk(KERN_WARNING "moxa_close: bad serial port count; "
1209 "tty->count is 1, ch->count is %d\n", ch->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 ch->count = 1;
1211 }
1212 if (--ch->count < 0) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001213 printk(KERN_WARNING "moxa_close: bad serial port count, "
1214 "device=%s\n", tty->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 ch->count = 0;
1216 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001217 if (ch->count)
1218 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 ch->cflag = tty->termios->c_cflag;
1221 if (ch->asyncflags & ASYNC_INITIALIZED) {
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001222 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001226 moxa_close_port(ch);
1227unlock:
1228 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
1231static int moxa_write(struct tty_struct *tty,
1232 const unsigned char *buf, int count)
1233{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001234 struct moxa_port *ch = tty->driver_data;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001235 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (ch == NULL)
Jiri Slabyb4173f42008-04-30 00:53:40 -07001238 return 0;
Alan Cox33f0f882006-01-09 20:54:13 -08001239
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001240 spin_lock_bh(&moxa_lock);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001241 len = MoxaPortWriteData(ch, buf, count);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001242 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 ch->statusflags |= LOWWAIT;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001245 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
1248static int moxa_write_room(struct tty_struct *tty)
1249{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001250 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 if (tty->stopped)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001253 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001254 ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (ch == NULL)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001256 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001257 return MoxaPortTxFree(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
1260static void moxa_flush_buffer(struct tty_struct *tty)
1261{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001262 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 if (ch == NULL)
1265 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001266 MoxaPortFlushData(ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 tty_wakeup(tty);
1268}
1269
1270static int moxa_chars_in_buffer(struct tty_struct *tty)
1271{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001272 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 /*
1276 * Sigh...I have to check if driver_data is NULL here, because
1277 * if an open() fails, the TTY subsystem eventually calls
1278 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1279 * routine. And since the open() failed, we return 0 here. TDJ
1280 */
1281 if (ch == NULL)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001282 return 0;
Alan Cox978e5952008-04-30 00:53:59 -07001283 lock_kernel();
Jiri Slabyb4173f42008-04-30 00:53:40 -07001284 chars = MoxaPortTxQueue(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 if (chars) {
1286 /*
1287 * Make it possible to wakeup anything waiting for output
1288 * in tty_ioctl.c, etc.
1289 */
1290 if (!(ch->statusflags & EMPTYWAIT))
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001291 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
Alan Cox978e5952008-04-30 00:53:59 -07001293 unlock_kernel();
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001294 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1298{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001299 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 int flag = 0, dtr, rts;
1301
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001302 mutex_lock(&moxa_openlock);
1303 ch = tty->driver_data;
1304 if (!ch) {
1305 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -07001306 return -EINVAL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Jiri Slabyb4173f42008-04-30 00:53:40 -07001309 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (dtr)
1311 flag |= TIOCM_DTR;
1312 if (rts)
1313 flag |= TIOCM_RTS;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001314 dtr = MoxaPortLineStatus(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if (dtr & 1)
1316 flag |= TIOCM_CTS;
1317 if (dtr & 2)
1318 flag |= TIOCM_DSR;
1319 if (dtr & 4)
1320 flag |= TIOCM_CD;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001321 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return flag;
1323}
1324
1325static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1326 unsigned int set, unsigned int clear)
1327{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001328 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 int port;
1330 int dtr, rts;
1331
Jiri Slaby11324ed2007-02-10 01:45:31 -08001332 port = tty->index;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001333 mutex_lock(&moxa_openlock);
1334 ch = tty->driver_data;
1335 if (!ch) {
1336 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -07001337 return -EINVAL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Jiri Slabyb4173f42008-04-30 00:53:40 -07001340 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (set & TIOCM_RTS)
1342 rts = 1;
1343 if (set & TIOCM_DTR)
1344 dtr = 1;
1345 if (clear & TIOCM_RTS)
1346 rts = 0;
1347 if (clear & TIOCM_DTR)
1348 dtr = 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001349 MoxaPortLineCtrl(ch, dtr, rts);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001350 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return 0;
1352}
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354static void moxa_throttle(struct tty_struct *tty)
1355{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001356 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 ch->statusflags |= THROTTLE;
1359}
1360
1361static void moxa_unthrottle(struct tty_struct *tty)
1362{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001363 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 ch->statusflags &= ~THROTTLE;
1366}
1367
1368static void moxa_set_termios(struct tty_struct *tty,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001369 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001371 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 if (ch == NULL)
1374 return;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001375 moxa_set_tty_param(tty, old_termios);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001376 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 wake_up_interruptible(&ch->open_wait);
1378}
1379
1380static void moxa_stop(struct tty_struct *tty)
1381{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001382 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 if (ch == NULL)
1385 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001386 MoxaPortTxDisable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 ch->statusflags |= TXSTOPPED;
1388}
1389
1390
1391static void moxa_start(struct tty_struct *tty)
1392{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001393 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 if (ch == NULL)
1396 return;
1397
1398 if (!(ch->statusflags & TXSTOPPED))
1399 return;
1400
Jiri Slabyb4173f42008-04-30 00:53:40 -07001401 MoxaPortTxEnable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 ch->statusflags &= ~TXSTOPPED;
1403}
1404
1405static void moxa_hangup(struct tty_struct *tty)
1406{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001407 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001409 mutex_lock(&moxa_openlock);
1410 ch = tty->driver_data;
1411 if (ch == NULL) {
1412 mutex_unlock(&moxa_openlock);
1413 return;
1414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 ch->count = 0;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001416 moxa_close_port(ch);
1417 mutex_unlock(&moxa_openlock);
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 wake_up_interruptible(&ch->open_wait);
1420}
1421
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001422static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1423{
1424 dcd = !!dcd;
1425
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001426 if (dcd != p->DCDState && p->tty && C_CLOCAL(p->tty)) {
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001427 if (!dcd)
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001428 tty_hangup(p->tty);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001429 }
1430 p->DCDState = dcd;
1431}
1432
1433static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1434 u16 __iomem *ip)
1435{
1436 struct tty_struct *tty = p->tty;
1437 void __iomem *ofsAddr;
1438 unsigned int inited = p->asyncflags & ASYNC_INITIALIZED;
1439 u16 intr;
1440
1441 if (tty) {
1442 if ((p->statusflags & EMPTYWAIT) &&
1443 MoxaPortTxQueue(p) == 0) {
1444 p->statusflags &= ~EMPTYWAIT;
1445 tty_wakeup(tty);
1446 }
1447 if ((p->statusflags & LOWWAIT) && !tty->stopped &&
1448 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1449 p->statusflags &= ~LOWWAIT;
1450 tty_wakeup(tty);
1451 }
1452
1453 if (inited && !(p->statusflags & THROTTLE) &&
1454 MoxaPortRxQueue(p) > 0) { /* RX */
1455 MoxaPortReadData(p);
1456 tty_schedule_flip(tty);
1457 }
1458 } else {
1459 p->statusflags &= ~EMPTYWAIT;
1460 MoxaPortFlushData(p, 0); /* flush RX */
1461 }
1462
1463 if (!handle) /* nothing else to do */
1464 return 0;
1465
1466 intr = readw(ip); /* port irq status */
1467 if (intr == 0)
1468 return 0;
1469
1470 writew(0, ip); /* ACK port */
1471 ofsAddr = p->tableAddr;
1472 if (intr & IntrTx) /* disable tx intr */
1473 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1474 ofsAddr + HostStat);
1475
1476 if (!inited)
1477 return 0;
1478
1479 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1480 tty_insert_flip_char(tty, 0, TTY_BREAK);
1481 tty_schedule_flip(tty);
1482 }
1483
1484 if (intr & IntrLine)
1485 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1486
1487 return 0;
1488}
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490static void moxa_poll(unsigned long ignored)
1491{
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001492 struct moxa_board_conf *brd;
1493 u16 __iomem *ip;
Jiri Slaby2a541342008-04-30 00:53:45 -07001494 unsigned int card, port, served = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001496 spin_lock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 for (card = 0; card < MAX_BOARDS; card++) {
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001498 brd = &moxa_boards[card];
1499 if (!brd->ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 continue;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001501
Jiri Slaby2a541342008-04-30 00:53:45 -07001502 served++;
1503
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001504 ip = NULL;
1505 if (readb(brd->intPend) == 0xff)
1506 ip = brd->intTable + readb(brd->intNdx);
1507
1508 for (port = 0; port < brd->numPorts; port++)
1509 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1510
1511 if (ip)
1512 writeb(0, brd->intPend); /* ACK */
1513
1514 if (moxaLowWaterChk) {
1515 struct moxa_port *p = brd->ports;
1516 for (port = 0; port < brd->numPorts; port++, p++)
1517 if (p->lowChkFlag) {
1518 p->lowChkFlag = 0;
1519 moxa_low_water_check(p->tableAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
1522 }
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001523 moxaLowWaterChk = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Jiri Slaby2a541342008-04-30 00:53:45 -07001525 if (served)
1526 mod_timer(&moxaTimer, jiffies + HZ / 50);
1527 spin_unlock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
1529
1530/******************************************************************************/
1531
Alan Coxdb1acaa2008-02-08 04:18:43 -08001532static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001534 register struct ktermios *ts = tty->termios;
1535 struct moxa_port *ch = tty->driver_data;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001536 int rts, cts, txflow, rxflow, xany, baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 rts = cts = txflow = rxflow = xany = 0;
1539 if (ts->c_cflag & CRTSCTS)
1540 rts = cts = 1;
1541 if (ts->c_iflag & IXON)
1542 txflow = 1;
1543 if (ts->c_iflag & IXOFF)
1544 rxflow = 1;
1545 if (ts->c_iflag & IXANY)
1546 xany = 1;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001547
1548 /* Clear the features we don't support */
1549 ts->c_cflag &= ~CMSPAR;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001550 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1551 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
Alan Coxdb1acaa2008-02-08 04:18:43 -08001552 if (baud == -1)
1553 baud = tty_termios_baud_rate(old_termios);
1554 /* Not put the baud rate into the termios data */
1555 tty_encode_baud_rate(tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001558static void moxa_setup_empty_event(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001560 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001562 spin_lock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 ch->statusflags |= EMPTYWAIT;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001564 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001567static void moxa_shut_down(struct moxa_port *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001569 struct tty_struct *tp = ch->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1572 return;
1573
Jiri Slabyb4173f42008-04-30 00:53:40 -07001574 MoxaPortDisable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 /*
1577 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1578 */
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001579 if (C_HUPCL(tp))
Jiri Slabyb4173f42008-04-30 00:53:40 -07001580 MoxaPortLineCtrl(ch, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001582 spin_lock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 ch->asyncflags &= ~ASYNC_INITIALIZED;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001584 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585}
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587/*****************************************************************************
1588 * Driver level functions: *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Jiri Slabyb4173f42008-04-30 00:53:40 -07001591static void MoxaPortFlushData(struct moxa_port *port, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
1593 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001594 if (mode < 0 || mode > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001596 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 moxafunc(ofsAddr, FC_FlushQueue, mode);
1598 if (mode != 1) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001599 port->lowChkFlag = 0;
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001600 moxa_low_water_check(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 }
1602}
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604/*
1605 * Moxa Port Number Description:
1606 *
1607 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1608 * the port number using in MOXA driver functions will be 0 to 31 for
1609 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1610 * to 127 for fourth. For example, if you setup three MOXA boards,
1611 * first board is C218, second board is C320-16 and third board is
1612 * C320-32. The port number of first board (C218 - 8 ports) is from
1613 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1614 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1615 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1616 * 127 will be invalid.
1617 *
1618 *
1619 * Moxa Functions Description:
1620 *
1621 * Function 1: Driver initialization routine, this routine must be
1622 * called when initialized driver.
1623 * Syntax:
1624 * void MoxaDriverInit();
1625 *
1626 *
1627 * Function 2: Moxa driver private IOCTL command processing.
1628 * Syntax:
1629 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1630 *
1631 * unsigned int cmd : IOCTL command
1632 * unsigned long arg : IOCTL argument
1633 * int port : port number (0 - 127)
1634 *
1635 * return: 0 (OK)
1636 * -EINVAL
1637 * -ENOIOCTLCMD
1638 *
1639 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 * Function 6: Enable this port to start Tx/Rx data.
1641 * Syntax:
1642 * void MoxaPortEnable(int port);
1643 * int port : port number (0 - 127)
1644 *
1645 *
1646 * Function 7: Disable this port
1647 * Syntax:
1648 * void MoxaPortDisable(int port);
1649 * int port : port number (0 - 127)
1650 *
1651 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 * Function 10: Setting baud rate of this port.
1653 * Syntax:
Jiri Slaby08d01c72008-04-30 00:53:47 -07001654 * speed_t MoxaPortSetBaud(int port, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 * int port : port number (0 - 127)
1656 * long baud : baud rate (50 - 115200)
1657 *
1658 * return: 0 : this port is invalid or baud < 50
1659 * 50 - 115200 : the real baud rate set to the port, if
1660 * the argument baud is large than maximun
1661 * available baud rate, the real setting
1662 * baud rate will be the maximun baud rate.
1663 *
1664 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 * Function 12: Configure the port.
1666 * Syntax:
Alan Cox606d0992006-12-08 02:38:45 -08001667 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 * int port : port number (0 - 127)
Alan Cox606d0992006-12-08 02:38:45 -08001669 * struct ktermios * termio : termio structure pointer
Alan Coxc7bce302006-09-30 23:27:24 -07001670 * speed_t baud : baud rate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 *
1672 * return: -1 : this port is invalid or termio == NULL
1673 * 0 : setting O.K.
1674 *
1675 *
1676 * Function 13: Get the DTR/RTS state of this port.
1677 * Syntax:
1678 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1679 * int port : port number (0 - 127)
1680 * int * dtrState : pointer to INT to receive the current DTR
1681 * state. (if NULL, this function will not
1682 * write to this address)
1683 * int * rtsState : pointer to INT to receive the current RTS
1684 * state. (if NULL, this function will not
1685 * write to this address)
1686 *
1687 * return: -1 : this port is invalid
1688 * 0 : O.K.
1689 *
1690 *
1691 * Function 14: Setting the DTR/RTS output state of this port.
1692 * Syntax:
1693 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1694 * int port : port number (0 - 127)
1695 * int dtrState : DTR output state (0: off, 1: on)
1696 * int rtsState : RTS output state (0: off, 1: on)
1697 *
1698 *
1699 * Function 15: Setting the flow control of this port.
1700 * Syntax:
1701 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1702 * int txFlow,int xany);
1703 * int port : port number (0 - 127)
1704 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1705 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1706 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1707 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1708 * int xany : S/W XANY flow control (0: no, 1: yes)
1709 *
1710 *
1711 * Function 16: Get ths line status of this port
1712 * Syntax:
1713 * int MoxaPortLineStatus(int port);
1714 * int port : port number (0 - 127)
1715 *
1716 * return: Bit 0 - CTS state (0: off, 1: on)
1717 * Bit 1 - DSR state (0: off, 1: on)
1718 * Bit 2 - DCD state (0: off, 1: on)
1719 *
1720 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 * Function 19: Flush the Rx/Tx buffer data of this port.
1722 * Syntax:
1723 * void MoxaPortFlushData(int port, int mode);
1724 * int port : port number (0 - 127)
1725 * int mode
1726 * 0 : flush the Rx buffer
1727 * 1 : flush the Tx buffer
1728 * 2 : flush the Rx and Tx buffer
1729 *
1730 *
1731 * Function 20: Write data.
1732 * Syntax:
1733 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1734 * int port : port number (0 - 127)
1735 * unsigned char * buffer : pointer to write data buffer.
1736 * int length : write data length
1737 *
1738 * return: 0 - length : real write data length
1739 *
1740 *
1741 * Function 21: Read data.
1742 * Syntax:
Alan Cox33f0f882006-01-09 20:54:13 -08001743 * int MoxaPortReadData(int port, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 * int port : port number (0 - 127)
Alan Cox33f0f882006-01-09 20:54:13 -08001745 * struct tty_struct *tty : tty for data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 *
1747 * return: 0 - length : real read data length
1748 *
1749 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 * Function 24: Get the Tx buffer current queued data bytes
1751 * Syntax:
1752 * int MoxaPortTxQueue(int port);
1753 * int port : port number (0 - 127)
1754 *
1755 * return: .. : Tx buffer current queued data bytes
1756 *
1757 *
1758 * Function 25: Get the Tx buffer current free space
1759 * Syntax:
1760 * int MoxaPortTxFree(int port);
1761 * int port : port number (0 - 127)
1762 *
1763 * return: .. : Tx buffer current free space
1764 *
1765 *
1766 * Function 26: Get the Rx buffer current queued data bytes
1767 * Syntax:
1768 * int MoxaPortRxQueue(int port);
1769 * int port : port number (0 - 127)
1770 *
1771 * return: .. : Rx buffer current queued data bytes
1772 *
1773 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 * Function 28: Disable port data transmission.
1775 * Syntax:
1776 * void MoxaPortTxDisable(int port);
1777 * int port : port number (0 - 127)
1778 *
1779 *
1780 * Function 29: Enable port data transmission.
1781 * Syntax:
1782 * void MoxaPortTxEnable(int port);
1783 * int port : port number (0 - 127)
1784 *
1785 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 * Function 31: Get the received BREAK signal count and reset it.
1787 * Syntax:
1788 * int MoxaPortResetBrkCnt(int port);
1789 * int port : port number (0 - 127)
1790 *
1791 * return: 0 - .. : BREAK signal count
1792 *
1793 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Jiri Slabyb4173f42008-04-30 00:53:40 -07001796static void MoxaPortEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
1798 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001799 u16 lowwater = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Jiri Slabyb4173f42008-04-30 00:53:40 -07001801 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 writew(lowwater, ofsAddr + Low_water);
Jiri Slaby08d01c72008-04-30 00:53:47 -07001803 if (MOXA_IS_320(port->board))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001805 else
1806 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1807 ofsAddr + HostStat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1810 moxafunc(ofsAddr, FC_FlushQueue, 2);
1811
1812 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1813 MoxaPortLineStatus(port);
1814}
1815
Jiri Slabyb4173f42008-04-30 00:53:40 -07001816static void MoxaPortDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001818 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
1820 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1821 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1822 writew(0, ofsAddr + HostStat);
1823 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1824}
1825
Jiri Slaby08d01c72008-04-30 00:53:47 -07001826static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
Jiri Slaby08d01c72008-04-30 00:53:47 -07001828 void __iomem *ofsAddr = port->tableAddr;
1829 unsigned int clock, val;
1830 speed_t max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Jiri Slaby08d01c72008-04-30 00:53:47 -07001832 max = MOXA_IS_320(port->board) ? 460800 : 921600;
1833 if (baud < 50)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001834 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if (baud > max)
1836 baud = max;
Jiri Slaby08d01c72008-04-30 00:53:47 -07001837 clock = 921600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 val = clock / baud;
1839 moxafunc(ofsAddr, FC_SetBaud, val);
1840 baud = clock / val;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001841 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
1843
Jiri Slabyb4173f42008-04-30 00:53:40 -07001844static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1845 speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
1847 void __iomem *ofsAddr;
1848 tcflag_t cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 tcflag_t mode = 0;
1850
Jiri Slabyb4173f42008-04-30 00:53:40 -07001851 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 cflag = termio->c_cflag; /* termio->c_cflag */
1853
1854 mode = termio->c_cflag & CSIZE;
1855 if (mode == CS5)
1856 mode = MX_CS5;
1857 else if (mode == CS6)
1858 mode = MX_CS6;
1859 else if (mode == CS7)
1860 mode = MX_CS7;
1861 else if (mode == CS8)
1862 mode = MX_CS8;
1863
1864 if (termio->c_cflag & CSTOPB) {
1865 if (mode == MX_CS5)
1866 mode |= MX_STOP15;
1867 else
1868 mode |= MX_STOP2;
1869 } else
1870 mode |= MX_STOP1;
1871
1872 if (termio->c_cflag & PARENB) {
1873 if (termio->c_cflag & PARODD)
1874 mode |= MX_PARODD;
1875 else
1876 mode |= MX_PAREVEN;
1877 } else
1878 mode |= MX_PARNONE;
1879
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001880 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Jiri Slaby08d01c72008-04-30 00:53:47 -07001882 if (MOXA_IS_320(port->board) && baud >= 921600)
1883 return -1;
1884
Alan Coxdb1acaa2008-02-08 04:18:43 -08001885 baud = MoxaPortSetBaud(port, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1888 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1889 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1890 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001891 moxa_wait_finish(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 }
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001894 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895}
1896
Jiri Slabyb4173f42008-04-30 00:53:40 -07001897static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1898 int *rtsState)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001900 if (dtrState)
1901 *dtrState = !!(port->lineCtrl & DTR_ON);
1902 if (rtsState)
1903 *rtsState = !!(port->lineCtrl & RTS_ON);
1904
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001905 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
Jiri Slabyb4173f42008-04-30 00:53:40 -07001908static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001910 u8 mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 if (dtr)
1913 mode |= DTR_ON;
1914 if (rts)
1915 mode |= RTS_ON;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001916 port->lineCtrl = mode;
1917 moxafunc(port->tableAddr, FC_LineControl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918}
1919
Jiri Slabyb4173f42008-04-30 00:53:40 -07001920static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1921 int txflow, int rxflow, int txany)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001923 int mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 if (rts)
1926 mode |= RTS_FlowCtl;
1927 if (cts)
1928 mode |= CTS_FlowCtl;
1929 if (txflow)
1930 mode |= Tx_FlowCtl;
1931 if (rxflow)
1932 mode |= Rx_FlowCtl;
1933 if (txany)
1934 mode |= IXM_IXANY;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001935 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}
1937
Jiri Slabyb4173f42008-04-30 00:53:40 -07001938static int MoxaPortLineStatus(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 void __iomem *ofsAddr;
1941 int val;
1942
Jiri Slabyb4173f42008-04-30 00:53:40 -07001943 ofsAddr = port->tableAddr;
Jiri Slaby08d01c72008-04-30 00:53:47 -07001944 if (MOXA_IS_320(port->board)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 moxafunc(ofsAddr, FC_LineStatus, 0);
1946 val = readw(ofsAddr + FuncArg);
1947 } else {
1948 val = readw(ofsAddr + FlagStat) >> 4;
1949 }
1950 val &= 0x0B;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001951 if (val & 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 val |= 4;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001953 spin_lock_bh(&moxa_lock);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001954 moxa_new_dcdstate(port, val & 8);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001955 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 val &= 7;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001957 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958}
1959
Jiri Slaby2108eba2008-04-30 00:53:44 -07001960static int MoxaPortWriteData(struct moxa_port *port,
1961 const unsigned char *buffer, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001964 unsigned int c, total;
1965 u16 head, tail, tx_mask, spage, epage;
1966 u16 pageno, pageofs, bufhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Jiri Slabyb4173f42008-04-30 00:53:40 -07001968 ofsAddr = port->tableAddr;
1969 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 tx_mask = readw(ofsAddr + TX_mask);
1971 spage = readw(ofsAddr + Page_txb);
1972 epage = readw(ofsAddr + EndPage_txb);
1973 tail = readw(ofsAddr + TXwptr);
1974 head = readw(ofsAddr + TXrptr);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001975 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 if (c > len)
1977 c = len;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001978 moxaLog.txcnt[port->tty->index] += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 total = c;
1980 if (spage == epage) {
1981 bufhead = readw(ofsAddr + Ofs_txb);
1982 writew(spage, baseAddr + Control_reg);
1983 while (c > 0) {
1984 if (head > tail)
1985 len = head - tail - 1;
1986 else
1987 len = tx_mask + 1 - tail;
1988 len = (c > len) ? len : c;
1989 ofs = baseAddr + DynPage_addr + bufhead + tail;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001990 memcpy_toio(ofs, buffer, len);
1991 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 tail = (tail + len) & tx_mask;
1993 c -= len;
1994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 pageno = spage + (tail >> 13);
1997 pageofs = tail & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001998 while (c > 0) {
1999 len = Page_size - pageofs;
2000 if (len > c)
2001 len = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 writeb(pageno, baseAddr + Control_reg);
2003 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002004 memcpy_toio(ofs, buffer, len);
2005 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 if (++pageno == epage)
2007 pageno = spage;
2008 pageofs = 0;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002009 c -= len;
2010 }
2011 tail = (tail + total) & tx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002013 writew(tail, ofsAddr + TXwptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 writeb(1, ofsAddr + CD180TXirq); /* start to send */
Jiri Slaby2108eba2008-04-30 00:53:44 -07002015 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016}
2017
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002018static int MoxaPortReadData(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002020 struct tty_struct *tty = port->tty;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002021 unsigned char *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002023 unsigned int count, len, total;
2024 u16 tail, rx_mask, spage, epage;
2025 u16 pageno, pageofs, bufhead, head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Jiri Slabyb4173f42008-04-30 00:53:40 -07002027 ofsAddr = port->tableAddr;
2028 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 head = readw(ofsAddr + RXrptr);
2030 tail = readw(ofsAddr + RXwptr);
2031 rx_mask = readw(ofsAddr + RX_mask);
2032 spage = readw(ofsAddr + Page_rxb);
2033 epage = readw(ofsAddr + EndPage_rxb);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002034 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (count == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08002036 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Alan Cox33f0f882006-01-09 20:54:13 -08002038 total = count;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002039 moxaLog.rxcnt[tty->index] += total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 if (spage == epage) {
2041 bufhead = readw(ofsAddr + Ofs_rxb);
2042 writew(spage, baseAddr + Control_reg);
2043 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 ofs = baseAddr + DynPage_addr + bufhead + head;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002045 len = (tail >= head) ? (tail - head) :
2046 (rx_mask + 1 - head);
2047 len = tty_prepare_flip_string(tty, &dst,
2048 min(len, count));
2049 memcpy_fromio(dst, ofs, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 head = (head + len) & rx_mask;
2051 count -= len;
2052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 pageno = spage + (head >> 13);
2055 pageofs = head & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002056 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 writew(pageno, baseAddr + Control_reg);
2058 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002059 len = tty_prepare_flip_string(tty, &dst,
2060 min(Page_size - pageofs, count));
2061 memcpy_fromio(dst, ofs, len);
2062
2063 count -= len;
2064 pageofs = (pageofs + len) & Page_mask;
2065 if (pageofs == 0 && ++pageno == epage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 pageno = spage;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002067 }
2068 head = (head + total) & rx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002070 writew(head, ofsAddr + RXrptr);
2071 if (readb(ofsAddr + FlagStat) & Xoff_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 moxaLowWaterChk = 1;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002073 port->lowChkFlag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002075 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076}
2077
2078
Jiri Slabyb4173f42008-04-30 00:53:40 -07002079static int MoxaPortTxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002081 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002082 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 rptr = readw(ofsAddr + TXrptr);
2085 wptr = readw(ofsAddr + TXwptr);
2086 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002087 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088}
2089
Jiri Slabyb4173f42008-04-30 00:53:40 -07002090static int MoxaPortTxFree(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002092 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002093 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 rptr = readw(ofsAddr + TXrptr);
2096 wptr = readw(ofsAddr + TXwptr);
2097 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002098 return mask - ((wptr - rptr) & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
2100
Jiri Slabyb4173f42008-04-30 00:53:40 -07002101static int MoxaPortRxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002103 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002104 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 rptr = readw(ofsAddr + RXrptr);
2107 wptr = readw(ofsAddr + RXwptr);
2108 mask = readw(ofsAddr + RX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002109 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
Jiri Slabyb4173f42008-04-30 00:53:40 -07002112static void MoxaPortTxDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002114 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115}
2116
Jiri Slabyb4173f42008-04-30 00:53:40 -07002117static void MoxaPortTxEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002119 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
2121
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002122static int moxa_get_serial_info(struct moxa_port *info,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002123 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002125 struct serial_struct tmp = {
2126 .type = info->type,
2127 .line = info->tty->index,
2128 .flags = info->asyncflags,
2129 .baud_base = 921600,
2130 .close_delay = info->close_delay
2131 };
2132 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133}
2134
2135
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002136static int moxa_set_serial_info(struct moxa_port *info,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002137 struct serial_struct __user *new_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
2139 struct serial_struct new_serial;
2140
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002141 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 return -EFAULT;
2143
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002144 if (new_serial.irq != 0 || new_serial.port != 0 ||
2145 new_serial.custom_divisor != 0 ||
2146 new_serial.baud_base != 921600)
2147 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 if (!capable(CAP_SYS_ADMIN)) {
2150 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2151 (info->asyncflags & ~ASYNC_USR_MASK)))
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002152 return -EPERM;
2153 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 info->close_delay = new_serial.close_delay * HZ / 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2157 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2158
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002159 MoxaSetFifo(info, new_serial.type == PORT_16550A);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
2161 info->type = new_serial.type;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163}
2164
2165
2166
2167/*****************************************************************************
2168 * Static local functions: *
2169 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Jiri Slabyb4173f42008-04-30 00:53:40 -07002171static void MoxaSetFifo(struct moxa_port *port, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002173 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175 if (!enable) {
2176 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2177 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2178 } else {
2179 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2180 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2181 }
2182}