blob: 876c79b1cee0ff1f772421d97c193877298b3058 [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 *);
196static void moxa_flush_chars(struct tty_struct *);
197static void moxa_put_char(struct tty_struct *, unsigned char);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198static void moxa_throttle(struct tty_struct *);
199static void moxa_unthrottle(struct tty_struct *);
Alan Cox606d0992006-12-08 02:38:45 -0800200static void moxa_set_termios(struct tty_struct *, struct ktermios *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static void moxa_stop(struct tty_struct *);
202static void moxa_start(struct tty_struct *);
203static void moxa_hangup(struct tty_struct *);
204static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
205static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
206 unsigned int set, unsigned int clear);
207static void moxa_poll(unsigned long);
Alan Coxdb1acaa2008-02-08 04:18:43 -0800208static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
Jiri Slaby6f56b652007-10-18 03:06:24 -0700209static void moxa_setup_empty_event(struct tty_struct *);
Jiri Slaby6f56b652007-10-18 03:06:24 -0700210static void moxa_shut_down(struct moxa_port *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/*
212 * moxa board interface functions:
213 */
Jiri Slabyb4173f42008-04-30 00:53:40 -0700214static void MoxaPortEnable(struct moxa_port *);
215static void MoxaPortDisable(struct moxa_port *);
216static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
217static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
218static void MoxaPortLineCtrl(struct moxa_port *, int, int);
219static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
220static int MoxaPortLineStatus(struct moxa_port *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700221static void MoxaPortFlushData(struct moxa_port *, int);
Jiri Slaby2108eba2008-04-30 00:53:44 -0700222static int MoxaPortWriteData(struct moxa_port *, const unsigned char *, int);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700223static int MoxaPortReadData(struct moxa_port *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700224static int MoxaPortTxQueue(struct moxa_port *);
225static int MoxaPortRxQueue(struct moxa_port *);
226static int MoxaPortTxFree(struct moxa_port *);
227static void MoxaPortTxDisable(struct moxa_port *);
228static void MoxaPortTxEnable(struct moxa_port *);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800229static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
230static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700231static void MoxaSetFifo(struct moxa_port *port, int enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Jiri Slaby74d7d972008-04-30 00:53:43 -0700233/*
234 * I/O functions
235 */
236
237static void moxa_wait_finish(void __iomem *ofsAddr)
238{
239 unsigned long end = jiffies + moxaFuncTout;
240
241 while (readw(ofsAddr + FuncCode) != 0)
242 if (time_after(jiffies, end))
243 return;
244 if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
245 printk(KERN_WARNING "moxa function expired\n");
246}
247
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700248static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
Jiri Slaby74d7d972008-04-30 00:53:43 -0700249{
250 writew(arg, ofsAddr + FuncArg);
251 writew(cmd, ofsAddr + FuncCode);
252 moxa_wait_finish(ofsAddr);
253}
254
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700255static void moxa_low_water_check(void __iomem *ofsAddr)
256{
257 u16 rptr, wptr, mask, len;
258
259 if (readb(ofsAddr + FlagStat) & Xoff_state) {
260 rptr = readw(ofsAddr + RXrptr);
261 wptr = readw(ofsAddr + RXwptr);
262 mask = readw(ofsAddr + RX_mask);
263 len = (wptr - rptr) & mask;
264 if (len <= Low_water)
265 moxafunc(ofsAddr, FC_SendXon, 0);
266 }
267}
268
Jiri Slaby74d7d972008-04-30 00:53:43 -0700269/*
270 * TTY operations
271 */
272
273static int moxa_ioctl(struct tty_struct *tty, struct file *file,
274 unsigned int cmd, unsigned long arg)
275{
276 struct moxa_port *ch = tty->driver_data;
277 void __user *argp = (void __user *)arg;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700278 int status, ret = 0;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700279
280 if (tty->index == MAX_PORTS) {
281 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
282 cmd != MOXA_GETMSTATUS)
283 return -EINVAL;
284 } else if (!ch)
285 return -ENODEV;
286
287 switch (cmd) {
288 case MOXA_GETDATACOUNT:
289 moxaLog.tick = jiffies;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700290 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
291 ret = -EFAULT;
292 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700293 case MOXA_FLUSH_QUEUE:
294 MoxaPortFlushData(ch, arg);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700295 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700296 case MOXA_GET_IOQUEUE: {
297 struct moxaq_str __user *argm = argp;
298 struct moxaq_str tmp;
299 struct moxa_port *p;
300 unsigned int i, j;
301
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700302 mutex_lock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700303 for (i = 0; i < MAX_BOARDS; i++) {
304 p = moxa_boards[i].ports;
305 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
306 memset(&tmp, 0, sizeof(tmp));
307 if (moxa_boards[i].ready) {
308 tmp.inq = MoxaPortRxQueue(p);
309 tmp.outq = MoxaPortTxQueue(p);
310 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700311 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
312 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700313 return -EFAULT;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700314 }
Jiri Slaby74d7d972008-04-30 00:53:43 -0700315 }
316 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700317 mutex_unlock(&moxa_openlock);
318 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700319 } case MOXA_GET_OQUEUE:
320 status = MoxaPortTxQueue(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700321 ret = put_user(status, (unsigned long __user *)argp);
322 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700323 case MOXA_GET_IQUEUE:
324 status = MoxaPortRxQueue(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700325 ret = put_user(status, (unsigned long __user *)argp);
326 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700327 case MOXA_GETMSTATUS: {
328 struct mxser_mstatus __user *argm = argp;
329 struct mxser_mstatus tmp;
330 struct moxa_port *p;
331 unsigned int i, j;
332
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700333 mutex_lock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700334 for (i = 0; i < MAX_BOARDS; i++) {
335 p = moxa_boards[i].ports;
336 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
337 memset(&tmp, 0, sizeof(tmp));
338 if (!moxa_boards[i].ready)
339 goto copy;
340
341 status = MoxaPortLineStatus(p);
342 if (status & 1)
343 tmp.cts = 1;
344 if (status & 2)
345 tmp.dsr = 1;
346 if (status & 4)
347 tmp.dcd = 1;
348
349 if (!p->tty || !p->tty->termios)
350 tmp.cflag = p->cflag;
351 else
352 tmp.cflag = p->tty->termios->c_cflag;
353copy:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700354 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
355 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700356 return -EFAULT;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700357 }
Jiri Slaby74d7d972008-04-30 00:53:43 -0700358 }
359 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700360 mutex_unlock(&moxa_openlock);
361 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700362 }
363 case TIOCGSERIAL:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700364 mutex_lock(&moxa_openlock);
365 ret = moxa_get_serial_info(ch, argp);
366 mutex_unlock(&moxa_openlock);
367 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700368 case TIOCSSERIAL:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700369 mutex_lock(&moxa_openlock);
370 ret = moxa_set_serial_info(ch, argp);
371 mutex_unlock(&moxa_openlock);
372 break;
373 default:
374 ret = -ENOIOCTLCMD;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700375 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700376 return ret;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700377}
378
379static void moxa_break_ctl(struct tty_struct *tty, int state)
380{
381 struct moxa_port *port = tty->driver_data;
382
383 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
384 Magic_code);
385}
386
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700387static const struct tty_operations moxa_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 .open = moxa_open,
389 .close = moxa_close,
390 .write = moxa_write,
391 .write_room = moxa_write_room,
392 .flush_buffer = moxa_flush_buffer,
393 .chars_in_buffer = moxa_chars_in_buffer,
394 .flush_chars = moxa_flush_chars,
395 .put_char = moxa_put_char,
396 .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;
519 unsigned int i, retry, c320;
Jiri Slaby03718232008-04-30 00:53:39 -0700520 u16 usum, keycode;
521
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700522 c320 = brd->boardType == MOXA_BOARD_C320_PCI ||
523 brd->boardType == MOXA_BOARD_C320_ISA;
524 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
525 C218_KeyCode;
526
527 switch (brd->boardType) {
528 case MOXA_BOARD_CP204J:
529 case MOXA_BOARD_C218_ISA:
530 case MOXA_BOARD_C218_PCI:
531 key = C218_key;
532 loadbuf = C218_LoadBuf;
533 loadlen = C218DLoad_len;
534 checksum = C218check_sum;
535 checksum_ok = C218chksum_ok;
536 break;
537 default:
538 key = C320_key;
539 keycode = C320_KeyCode;
540 loadbuf = C320_LoadBuf;
541 loadlen = C320DLoad_len;
542 checksum = C320check_sum;
543 checksum_ok = C320chksum_ok;
544 break;
545 }
546
Jiri Slaby03718232008-04-30 00:53:39 -0700547 usum = 0;
548 wlen = len >> 1;
549 for (i = 0; i < wlen; i++)
550 usum += le16_to_cpu(uptr[i]);
551 retry = 0;
552 do {
553 wlen = len >> 1;
554 j = 0;
555 while (wlen) {
556 len2 = (wlen > 2048) ? 2048 : wlen;
557 wlen -= len2;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700558 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
Jiri Slaby03718232008-04-30 00:53:39 -0700559 j += len2 << 1;
560
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700561 writew(len2, baseAddr + loadlen);
562 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700563 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700564 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700565 break;
566 msleep(10);
567 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700568 if (readw(baseAddr + key) != keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700569 return -EIO;
570 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700571 writew(0, baseAddr + loadlen);
572 writew(usum, baseAddr + checksum);
573 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700574 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700575 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700576 break;
577 msleep(10);
578 }
579 retry++;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700580 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
581 if (readb(baseAddr + checksum_ok) != 1)
Jiri Slaby03718232008-04-30 00:53:39 -0700582 return -EIO;
583
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700584 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700585 for (i = 0; i < 600; i++) {
586 if (readw(baseAddr + Magic_no) == Magic_code)
587 break;
588 msleep(10);
589 }
590 if (readw(baseAddr + Magic_no) != Magic_code)
591 return -EIO;
592
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700593 if (c320) {
594 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
595 writew(0x3800, baseAddr + TMS320_PORT1);
596 writew(0x3900, baseAddr + TMS320_PORT2);
597 writew(28499, baseAddr + TMS320_CLOCK);
598 } else {
599 writew(0x3200, baseAddr + TMS320_PORT1);
600 writew(0x3400, baseAddr + TMS320_PORT2);
601 writew(19999, baseAddr + TMS320_CLOCK);
602 }
Jiri Slaby03718232008-04-30 00:53:39 -0700603 }
604 writew(1, baseAddr + Disable_IRQ);
605 writew(0, baseAddr + Magic_no);
606 for (i = 0; i < 500; i++) {
607 if (readw(baseAddr + Magic_no) == Magic_code)
608 break;
609 msleep(10);
610 }
611 if (readw(baseAddr + Magic_no) != Magic_code)
612 return -EIO;
613
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700614 if (c320) {
615 j = readw(baseAddr + Module_cnt);
616 if (j <= 0)
617 return -EIO;
618 brd->numPorts = j * 8;
619 writew(j, baseAddr + Module_no);
620 writew(0, baseAddr + Magic_no);
621 for (i = 0; i < 600; i++) {
622 if (readw(baseAddr + Magic_no) == Magic_code)
623 break;
624 msleep(10);
625 }
626 if (readw(baseAddr + Magic_no) != Magic_code)
627 return -EIO;
Jiri Slaby03718232008-04-30 00:53:39 -0700628 }
Jiri Slaby03718232008-04-30 00:53:39 -0700629 brd->intNdx = baseAddr + IRQindex;
630 brd->intPend = baseAddr + IRQpending;
631 brd->intTable = baseAddr + IRQtable;
632
633 return 0;
634}
635
636static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
637 size_t len)
638{
639 void __iomem *ofsAddr, *baseAddr = brd->basemem;
640 struct moxa_port *port;
641 int retval, i;
642
643 if (len % 2) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700644 printk(KERN_ERR "MOXA: bios length is not even\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700645 return -EINVAL;
646 }
647
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700648 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
649 if (retval)
650 return retval;
651
Jiri Slaby03718232008-04-30 00:53:39 -0700652 switch (brd->boardType) {
653 case MOXA_BOARD_C218_ISA:
654 case MOXA_BOARD_C218_PCI:
655 case MOXA_BOARD_CP204J:
Jiri Slaby03718232008-04-30 00:53:39 -0700656 port = brd->ports;
657 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700658 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700659 port->DCDState = 0;
660 port->tableAddr = baseAddr + Extern_table +
661 Extern_size * i;
662 ofsAddr = port->tableAddr;
663 writew(C218rx_mask, ofsAddr + RX_mask);
664 writew(C218tx_mask, ofsAddr + TX_mask);
665 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
666 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
667
668 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
669 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
670
671 }
672 break;
673 default:
Jiri Slaby03718232008-04-30 00:53:39 -0700674 port = brd->ports;
675 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700676 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700677 port->DCDState = 0;
678 port->tableAddr = baseAddr + Extern_table +
679 Extern_size * i;
680 ofsAddr = port->tableAddr;
681 switch (brd->numPorts) {
682 case 8:
683 writew(C320p8rx_mask, ofsAddr + RX_mask);
684 writew(C320p8tx_mask, ofsAddr + TX_mask);
685 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
686 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
687 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
688 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
689
690 break;
691 case 16:
692 writew(C320p16rx_mask, ofsAddr + RX_mask);
693 writew(C320p16tx_mask, ofsAddr + TX_mask);
694 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
695 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
696 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
697 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
698 break;
699
700 case 24:
701 writew(C320p24rx_mask, ofsAddr + RX_mask);
702 writew(C320p24tx_mask, ofsAddr + TX_mask);
703 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
704 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
705 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
706 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
707 break;
708 case 32:
709 writew(C320p32rx_mask, ofsAddr + RX_mask);
710 writew(C320p32tx_mask, ofsAddr + TX_mask);
711 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
712 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
713 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
714 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
715 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
716 break;
717 }
718 }
719 break;
720 }
Jiri Slaby03718232008-04-30 00:53:39 -0700721 return 0;
722}
723
724static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
725{
726 void *ptr = fw->data;
727 char rsn[64];
728 u16 lens[5];
729 size_t len;
730 unsigned int a, lenp, lencnt;
731 int ret = -EINVAL;
732 struct {
733 __le32 magic; /* 0x34303430 */
734 u8 reserved1[2];
735 u8 type; /* UNIX = 3 */
736 u8 model; /* C218T=1, C320T=2, CP204=3 */
737 u8 reserved2[8];
738 __le16 len[5];
739 } *hdr = ptr;
740
741 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
742
743 if (fw->size < MOXA_FW_HDRLEN) {
744 strcpy(rsn, "too short (even header won't fit)");
745 goto err;
746 }
747 if (hdr->magic != cpu_to_le32(0x30343034)) {
748 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
749 goto err;
750 }
751 if (hdr->type != 3) {
752 sprintf(rsn, "not for linux, type is %u", hdr->type);
753 goto err;
754 }
755 if (moxa_check_fw_model(brd, hdr->model)) {
756 sprintf(rsn, "not for this card, model is %u", hdr->model);
757 goto err;
758 }
759
760 len = MOXA_FW_HDRLEN;
761 lencnt = hdr->model == 2 ? 5 : 3;
762 for (a = 0; a < ARRAY_SIZE(lens); a++) {
763 lens[a] = le16_to_cpu(hdr->len[a]);
764 if (lens[a] && len + lens[a] <= fw->size &&
765 moxa_check_fw(&fw->data[len]))
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700766 printk(KERN_WARNING "MOXA firmware: unexpected input "
Jiri Slaby03718232008-04-30 00:53:39 -0700767 "at offset %u, but going on\n", (u32)len);
768 if (!lens[a] && a < lencnt) {
769 sprintf(rsn, "too few entries in fw file");
770 goto err;
771 }
772 len += lens[a];
773 }
774
775 if (len != fw->size) {
776 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
777 (u32)len);
778 goto err;
779 }
780
781 ptr += MOXA_FW_HDRLEN;
782 lenp = 0; /* bios */
783
784 strcpy(rsn, "read above");
785
786 ret = moxa_load_bios(brd, ptr, lens[lenp]);
787 if (ret)
788 goto err;
789
790 /* we skip the tty section (lens[1]), since we don't need it */
791 ptr += lens[lenp] + lens[lenp + 1];
792 lenp += 2; /* comm */
793
794 if (hdr->model == 2) {
795 ret = moxa_load_320b(brd, ptr, lens[lenp]);
796 if (ret)
797 goto err;
798 /* skip another tty */
799 ptr += lens[lenp] + lens[lenp + 1];
800 lenp += 2;
801 }
802
803 ret = moxa_load_code(brd, ptr, lens[lenp]);
804 if (ret)
805 goto err;
806
807 return 0;
808err:
809 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
810 return ret;
811}
812
813static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
814{
815 const struct firmware *fw;
816 const char *file;
Jiri Slaby810ab092008-04-30 00:53:41 -0700817 struct moxa_port *p;
818 unsigned int i;
Jiri Slaby03718232008-04-30 00:53:39 -0700819 int ret;
820
Jiri Slaby810ab092008-04-30 00:53:41 -0700821 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
822 GFP_KERNEL);
823 if (brd->ports == NULL) {
824 printk(KERN_ERR "cannot allocate memory for ports\n");
825 ret = -ENOMEM;
826 goto err;
827 }
828
829 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
830 p->type = PORT_16550A;
831 p->close_delay = 5 * HZ / 10;
832 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
833 init_waitqueue_head(&p->open_wait);
Jiri Slaby810ab092008-04-30 00:53:41 -0700834 }
835
Jiri Slaby03718232008-04-30 00:53:39 -0700836 switch (brd->boardType) {
837 case MOXA_BOARD_C218_ISA:
838 case MOXA_BOARD_C218_PCI:
839 file = "c218tunx.cod";
840 break;
841 case MOXA_BOARD_CP204J:
842 file = "cp204unx.cod";
843 break;
844 default:
845 file = "c320tunx.cod";
846 break;
847 }
848
849 ret = request_firmware(&fw, file, dev);
850 if (ret) {
851 printk(KERN_ERR "request_firmware failed\n");
Jiri Slaby810ab092008-04-30 00:53:41 -0700852 goto err_free;
Jiri Slaby03718232008-04-30 00:53:39 -0700853 }
854
855 ret = moxa_load_fw(brd, fw);
856
857 release_firmware(fw);
Jiri Slaby810ab092008-04-30 00:53:41 -0700858
859 if (ret)
860 goto err_free;
861
Jiri Slaby2a541342008-04-30 00:53:45 -0700862 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700863 brd->ready = 1;
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700864 if (!timer_pending(&moxaTimer))
865 mod_timer(&moxaTimer, jiffies + HZ / 50);
Jiri Slaby2a541342008-04-30 00:53:45 -0700866 spin_unlock_bh(&moxa_lock);
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700867
Jiri Slaby810ab092008-04-30 00:53:41 -0700868 return 0;
869err_free:
870 kfree(brd->ports);
871err:
Jiri Slaby03718232008-04-30 00:53:39 -0700872 return ret;
873}
874
Jiri Slaby810ab092008-04-30 00:53:41 -0700875static void moxa_board_deinit(struct moxa_board_conf *brd)
876{
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700877 unsigned int a, opened;
878
879 mutex_lock(&moxa_openlock);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700880 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700881 brd->ready = 0;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700882 spin_unlock_bh(&moxa_lock);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700883
884 /* pci hot-un-plug support */
885 for (a = 0; a < brd->numPorts; a++)
886 if (brd->ports[a].asyncflags & ASYNC_INITIALIZED)
887 tty_hangup(brd->ports[a].tty);
888 while (1) {
889 opened = 0;
890 for (a = 0; a < brd->numPorts; a++)
891 if (brd->ports[a].asyncflags & ASYNC_INITIALIZED)
892 opened++;
893 mutex_unlock(&moxa_openlock);
894 if (!opened)
895 break;
896 msleep(50);
897 mutex_lock(&moxa_openlock);
898 }
899
Jiri Slaby810ab092008-04-30 00:53:41 -0700900 iounmap(brd->basemem);
901 brd->basemem = NULL;
902 kfree(brd->ports);
903}
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905#ifdef CONFIG_PCI
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800906static int __devinit moxa_pci_probe(struct pci_dev *pdev,
907 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800909 struct moxa_board_conf *board;
910 unsigned int i;
911 int board_type = ent->driver_data;
912 int retval;
913
914 retval = pci_enable_device(pdev);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700915 if (retval) {
916 dev_err(&pdev->dev, "can't enable pci device\n");
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800917 goto err;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700918 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800919
920 for (i = 0; i < MAX_BOARDS; i++)
921 if (moxa_boards[i].basemem == NULL)
922 break;
923
924 retval = -ENODEV;
925 if (i >= MAX_BOARDS) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700926 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800927 "found. Board is ignored.\n", MAX_BOARDS);
928 goto err;
929 }
930
931 board = &moxa_boards[i];
Jiri Slabye46a5e32008-04-30 00:53:37 -0700932
933 retval = pci_request_region(pdev, 2, "moxa-base");
934 if (retval) {
935 dev_err(&pdev->dev, "can't request pci region 2\n");
936 goto err;
937 }
938
939 board->basemem = ioremap(pci_resource_start(pdev, 2), 0x4000);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700940 if (board->basemem == NULL) {
941 dev_err(&pdev->dev, "can't remap io space 2\n");
Jiri Slabye46a5e32008-04-30 00:53:37 -0700942 goto err_reg;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700943 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 board->boardType = board_type;
946 switch (board_type) {
947 case MOXA_BOARD_C218_ISA:
948 case MOXA_BOARD_C218_PCI:
949 board->numPorts = 8;
950 break;
951
952 case MOXA_BOARD_CP204J:
953 board->numPorts = 4;
954 break;
955 default:
956 board->numPorts = 0;
957 break;
958 }
959 board->busType = MOXA_BUS_TYPE_PCI;
Jiri Slabya784bf72007-02-10 01:45:36 -0800960
Jiri Slaby03718232008-04-30 00:53:39 -0700961 retval = moxa_init_board(board, &pdev->dev);
962 if (retval)
963 goto err_base;
964
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800965 pci_set_drvdata(pdev, board);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700967 return 0;
Jiri Slaby03718232008-04-30 00:53:39 -0700968err_base:
969 iounmap(board->basemem);
970 board->basemem = NULL;
Jiri Slabye46a5e32008-04-30 00:53:37 -0700971err_reg:
972 pci_release_region(pdev, 2);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800973err:
974 return retval;
975}
976
977static void __devexit moxa_pci_remove(struct pci_dev *pdev)
978{
979 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
980
Jiri Slaby810ab092008-04-30 00:53:41 -0700981 moxa_board_deinit(brd);
982
Jiri Slabye46a5e32008-04-30 00:53:37 -0700983 pci_release_region(pdev, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
Jiri Slabya784bf72007-02-10 01:45:36 -0800985
986static struct pci_driver moxa_pci_driver = {
987 .name = "moxa",
988 .id_table = moxa_pcibrds,
989 .probe = moxa_pci_probe,
990 .remove = __devexit_p(moxa_pci_remove)
991};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992#endif /* CONFIG_PCI */
993
994static int __init moxa_init(void)
995{
Jiri Slaby810ab092008-04-30 00:53:41 -0700996 unsigned int isabrds = 0;
Jiri Slabyd353eca2008-04-30 00:53:37 -0700997 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700999 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
1000 MOXA_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
1002 if (!moxaDriver)
1003 return -ENOMEM;
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 moxaDriver->owner = THIS_MODULE;
Sergey Vlasov9b4e3b12005-09-03 16:26:49 +01001006 moxaDriver->name = "ttyMX";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 moxaDriver->major = ttymajor;
1008 moxaDriver->minor_start = 0;
1009 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1010 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1011 moxaDriver->init_termios = tty_std_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Alan Cox606d0992006-12-08 02:38:45 -08001013 moxaDriver->init_termios.c_ispeed = 9600;
1014 moxaDriver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
1016 tty_set_operations(moxaDriver, &moxa_ops);
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (tty_register_driver(moxaDriver)) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001019 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 put_tty_driver(moxaDriver);
1021 return -1;
1022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Jiri Slabyd353eca2008-04-30 00:53:37 -07001024 /* Find the boards defined from module args. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -07001026 {
1027 struct moxa_board_conf *brd = moxa_boards;
Jiri Slaby810ab092008-04-30 00:53:41 -07001028 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 for (i = 0; i < MAX_BOARDS; i++) {
Jiri Slabyd353eca2008-04-30 00:53:37 -07001030 if (!baseaddr[i])
1031 break;
1032 if (type[i] == MOXA_BOARD_C218_ISA ||
1033 type[i] == MOXA_BOARD_C320_ISA) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001034 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001035 isabrds + 1, moxa_brdname[type[i] - 1],
1036 baseaddr[i]);
1037 brd->boardType = type[i];
1038 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1039 numports[i];
1040 brd->busType = MOXA_BUS_TYPE_ISA;
1041 brd->basemem = ioremap(baseaddr[i], 0x4000);
1042 if (!brd->basemem) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001043 printk(KERN_ERR "MOXA: can't remap %lx\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001044 baseaddr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 continue;
1046 }
Jiri Slaby03718232008-04-30 00:53:39 -07001047 if (moxa_init_board(brd, NULL)) {
1048 iounmap(brd->basemem);
1049 brd->basemem = NULL;
1050 continue;
1051 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001052
1053 brd++;
1054 isabrds++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001061 retval = pci_register_driver(&moxa_pci_driver);
1062 if (retval) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001063 printk(KERN_ERR "Can't register MOXA pci driver!\n");
Jiri Slabyd353eca2008-04-30 00:53:37 -07001064 if (isabrds)
Jiri Slabya784bf72007-02-10 01:45:36 -08001065 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001068
Jiri Slabya784bf72007-02-10 01:45:36 -08001069 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070}
1071
1072static void __exit moxa_exit(void)
1073{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001074 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001076#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001077 pci_unregister_driver(&moxa_pci_driver);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001078#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001079
Jiri Slaby810ab092008-04-30 00:53:41 -07001080 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1081 if (moxa_boards[i].ready)
1082 moxa_board_deinit(&moxa_boards[i]);
Jiri Slaby2a541342008-04-30 00:53:45 -07001083
1084 del_timer_sync(&moxaTimer);
1085
1086 if (tty_unregister_driver(moxaDriver))
1087 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1088 "serial driver\n");
1089 put_tty_driver(moxaDriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
1092module_init(moxa_init);
1093module_exit(moxa_exit);
1094
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001095static void moxa_close_port(struct moxa_port *ch)
1096{
1097 moxa_shut_down(ch);
1098 MoxaPortFlushData(ch, 2);
1099 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1100 ch->tty->driver_data = NULL;
1101 ch->tty = NULL;
1102}
1103
1104static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
1105 struct moxa_port *ch)
1106{
1107 DEFINE_WAIT(wait);
1108 int retval = 0;
1109 u8 dcd;
1110
1111 while (1) {
1112 prepare_to_wait(&ch->open_wait, &wait, TASK_INTERRUPTIBLE);
1113 if (tty_hung_up_p(filp)) {
1114#ifdef SERIAL_DO_RESTART
1115 retval = -ERESTARTSYS;
1116#else
1117 retval = -EAGAIN;
1118#endif
1119 break;
1120 }
1121 spin_lock_bh(&moxa_lock);
1122 dcd = ch->DCDState;
1123 spin_unlock_bh(&moxa_lock);
1124 if (dcd)
1125 break;
1126
1127 if (signal_pending(current)) {
1128 retval = -ERESTARTSYS;
1129 break;
1130 }
1131 schedule();
1132 }
1133 finish_wait(&ch->open_wait, &wait);
1134
1135 return retval;
1136}
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138static int moxa_open(struct tty_struct *tty, struct file *filp)
1139{
Jiri Slaby810ab092008-04-30 00:53:41 -07001140 struct moxa_board_conf *brd;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001141 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 int port;
1143 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Jiri Slaby11324ed2007-02-10 01:45:31 -08001145 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (port == MAX_PORTS) {
Jiri Slaby74d7d972008-04-30 00:53:43 -07001147 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001149 if (mutex_lock_interruptible(&moxa_openlock))
1150 return -ERESTARTSYS;
Jiri Slaby810ab092008-04-30 00:53:41 -07001151 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001152 if (!brd->ready) {
1153 mutex_unlock(&moxa_openlock);
Jiri Slaby810ab092008-04-30 00:53:41 -07001154 return -ENODEV;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Jiri Slaby810ab092008-04-30 00:53:41 -07001157 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 ch->count++;
1159 tty->driver_data = ch;
1160 ch->tty = tty;
1161 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
1162 ch->statusflags = 0;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001163 moxa_set_tty_param(tty, tty->termios);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001164 MoxaPortLineCtrl(ch, 1, 1);
1165 MoxaPortEnable(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001166 MoxaSetFifo(ch, ch->type == PORT_16550A);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 ch->asyncflags |= ASYNC_INITIALIZED;
1168 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001169 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001171 retval = 0;
1172 if (!(filp->f_flags & O_NONBLOCK) && !C_CLOCAL(tty))
1173 retval = moxa_block_till_ready(tty, filp, ch);
1174 mutex_lock(&moxa_openlock);
1175 if (retval) {
1176 if (ch->count) /* 0 means already hung up... */
1177 if (--ch->count == 0)
1178 moxa_close_port(ch);
1179 } else
1180 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1181 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001183 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184}
1185
1186static void moxa_close(struct tty_struct *tty, struct file *filp)
1187{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001188 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 int port;
1190
Jiri Slaby11324ed2007-02-10 01:45:31 -08001191 port = tty->index;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001192 if (port == MAX_PORTS || tty_hung_up_p(filp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001195 mutex_lock(&moxa_openlock);
1196 ch = tty->driver_data;
1197 if (ch == NULL)
1198 goto unlock;
1199 if (tty->count == 1 && ch->count != 1) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001200 printk(KERN_WARNING "moxa_close: bad serial port count; "
1201 "tty->count is 1, ch->count is %d\n", ch->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 ch->count = 1;
1203 }
1204 if (--ch->count < 0) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001205 printk(KERN_WARNING "moxa_close: bad serial port count, "
1206 "device=%s\n", tty->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 ch->count = 0;
1208 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001209 if (ch->count)
1210 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 ch->cflag = tty->termios->c_cflag;
1213 if (ch->asyncflags & ASYNC_INITIALIZED) {
Jiri Slaby6f56b652007-10-18 03:06:24 -07001214 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001218 moxa_close_port(ch);
1219unlock:
1220 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
1223static int moxa_write(struct tty_struct *tty,
1224 const unsigned char *buf, int count)
1225{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001226 struct moxa_port *ch = tty->driver_data;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001227 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (ch == NULL)
Jiri Slabyb4173f42008-04-30 00:53:40 -07001230 return 0;
Alan Cox33f0f882006-01-09 20:54:13 -08001231
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001232 spin_lock_bh(&moxa_lock);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001233 len = MoxaPortWriteData(ch, buf, count);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001234 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 ch->statusflags |= LOWWAIT;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001237 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
1240static int moxa_write_room(struct tty_struct *tty)
1241{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001242 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 if (tty->stopped)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001245 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001246 ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (ch == NULL)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001248 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001249 return MoxaPortTxFree(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250}
1251
1252static void moxa_flush_buffer(struct tty_struct *tty)
1253{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001254 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 if (ch == NULL)
1257 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001258 MoxaPortFlushData(ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 tty_wakeup(tty);
1260}
1261
1262static int moxa_chars_in_buffer(struct tty_struct *tty)
1263{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001264 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 /*
1268 * Sigh...I have to check if driver_data is NULL here, because
1269 * if an open() fails, the TTY subsystem eventually calls
1270 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1271 * routine. And since the open() failed, we return 0 here. TDJ
1272 */
1273 if (ch == NULL)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001274 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001275 chars = MoxaPortTxQueue(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (chars) {
1277 /*
1278 * Make it possible to wakeup anything waiting for output
1279 * in tty_ioctl.c, etc.
1280 */
1281 if (!(ch->statusflags & EMPTYWAIT))
Jiri Slaby6f56b652007-10-18 03:06:24 -07001282 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001284 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
1287static void moxa_flush_chars(struct tty_struct *tty)
1288{
1289 /*
1290 * Don't think I need this, because this is called to empty the TX
1291 * buffer for the 16450, 16550, etc.
1292 */
1293}
1294
1295static void moxa_put_char(struct tty_struct *tty, unsigned char c)
1296{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001297 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (ch == NULL)
1300 return;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001301 spin_lock_bh(&moxa_lock);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001302 MoxaPortWriteData(ch, &c, 1);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001303 spin_unlock_bh(&moxa_lock);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 ch->statusflags |= LOWWAIT;
1306}
1307
1308static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1309{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001310 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 int flag = 0, dtr, rts;
1312
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001313 mutex_lock(&moxa_openlock);
1314 ch = tty->driver_data;
1315 if (!ch) {
1316 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -07001317 return -EINVAL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Jiri Slabyb4173f42008-04-30 00:53:40 -07001320 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (dtr)
1322 flag |= TIOCM_DTR;
1323 if (rts)
1324 flag |= TIOCM_RTS;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001325 dtr = MoxaPortLineStatus(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if (dtr & 1)
1327 flag |= TIOCM_CTS;
1328 if (dtr & 2)
1329 flag |= TIOCM_DSR;
1330 if (dtr & 4)
1331 flag |= TIOCM_CD;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001332 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 return flag;
1334}
1335
1336static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1337 unsigned int set, unsigned int clear)
1338{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001339 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 int port;
1341 int dtr, rts;
1342
Jiri Slaby11324ed2007-02-10 01:45:31 -08001343 port = tty->index;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001344 mutex_lock(&moxa_openlock);
1345 ch = tty->driver_data;
1346 if (!ch) {
1347 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -07001348 return -EINVAL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Jiri Slabyb4173f42008-04-30 00:53:40 -07001351 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (set & TIOCM_RTS)
1353 rts = 1;
1354 if (set & TIOCM_DTR)
1355 dtr = 1;
1356 if (clear & TIOCM_RTS)
1357 rts = 0;
1358 if (clear & TIOCM_DTR)
1359 dtr = 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001360 MoxaPortLineCtrl(ch, dtr, rts);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001361 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return 0;
1363}
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365static void moxa_throttle(struct tty_struct *tty)
1366{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001367 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 ch->statusflags |= THROTTLE;
1370}
1371
1372static void moxa_unthrottle(struct tty_struct *tty)
1373{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001374 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 ch->statusflags &= ~THROTTLE;
1377}
1378
1379static void moxa_set_termios(struct tty_struct *tty,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001380 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
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;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001386 moxa_set_tty_param(tty, old_termios);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001387 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 wake_up_interruptible(&ch->open_wait);
1389}
1390
1391static void moxa_stop(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;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001397 MoxaPortTxDisable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 ch->statusflags |= TXSTOPPED;
1399}
1400
1401
1402static void moxa_start(struct tty_struct *tty)
1403{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001404 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 if (ch == NULL)
1407 return;
1408
1409 if (!(ch->statusflags & TXSTOPPED))
1410 return;
1411
Jiri Slabyb4173f42008-04-30 00:53:40 -07001412 MoxaPortTxEnable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 ch->statusflags &= ~TXSTOPPED;
1414}
1415
1416static void moxa_hangup(struct tty_struct *tty)
1417{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001418 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001420 mutex_lock(&moxa_openlock);
1421 ch = tty->driver_data;
1422 if (ch == NULL) {
1423 mutex_unlock(&moxa_openlock);
1424 return;
1425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 ch->count = 0;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001427 moxa_close_port(ch);
1428 mutex_unlock(&moxa_openlock);
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 wake_up_interruptible(&ch->open_wait);
1431}
1432
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001433static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1434{
1435 dcd = !!dcd;
1436
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001437 if (dcd != p->DCDState && p->tty && C_CLOCAL(p->tty)) {
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001438 if (!dcd)
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001439 tty_hangup(p->tty);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001440 }
1441 p->DCDState = dcd;
1442}
1443
1444static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1445 u16 __iomem *ip)
1446{
1447 struct tty_struct *tty = p->tty;
1448 void __iomem *ofsAddr;
1449 unsigned int inited = p->asyncflags & ASYNC_INITIALIZED;
1450 u16 intr;
1451
1452 if (tty) {
1453 if ((p->statusflags & EMPTYWAIT) &&
1454 MoxaPortTxQueue(p) == 0) {
1455 p->statusflags &= ~EMPTYWAIT;
1456 tty_wakeup(tty);
1457 }
1458 if ((p->statusflags & LOWWAIT) && !tty->stopped &&
1459 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1460 p->statusflags &= ~LOWWAIT;
1461 tty_wakeup(tty);
1462 }
1463
1464 if (inited && !(p->statusflags & THROTTLE) &&
1465 MoxaPortRxQueue(p) > 0) { /* RX */
1466 MoxaPortReadData(p);
1467 tty_schedule_flip(tty);
1468 }
1469 } else {
1470 p->statusflags &= ~EMPTYWAIT;
1471 MoxaPortFlushData(p, 0); /* flush RX */
1472 }
1473
1474 if (!handle) /* nothing else to do */
1475 return 0;
1476
1477 intr = readw(ip); /* port irq status */
1478 if (intr == 0)
1479 return 0;
1480
1481 writew(0, ip); /* ACK port */
1482 ofsAddr = p->tableAddr;
1483 if (intr & IntrTx) /* disable tx intr */
1484 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1485 ofsAddr + HostStat);
1486
1487 if (!inited)
1488 return 0;
1489
1490 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1491 tty_insert_flip_char(tty, 0, TTY_BREAK);
1492 tty_schedule_flip(tty);
1493 }
1494
1495 if (intr & IntrLine)
1496 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1497
1498 return 0;
1499}
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501static void moxa_poll(unsigned long ignored)
1502{
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001503 struct moxa_board_conf *brd;
1504 u16 __iomem *ip;
Jiri Slaby2a541342008-04-30 00:53:45 -07001505 unsigned int card, port, served = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001507 spin_lock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 for (card = 0; card < MAX_BOARDS; card++) {
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001509 brd = &moxa_boards[card];
1510 if (!brd->ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 continue;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001512
Jiri Slaby2a541342008-04-30 00:53:45 -07001513 served++;
1514
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001515 ip = NULL;
1516 if (readb(brd->intPend) == 0xff)
1517 ip = brd->intTable + readb(brd->intNdx);
1518
1519 for (port = 0; port < brd->numPorts; port++)
1520 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1521
1522 if (ip)
1523 writeb(0, brd->intPend); /* ACK */
1524
1525 if (moxaLowWaterChk) {
1526 struct moxa_port *p = brd->ports;
1527 for (port = 0; port < brd->numPorts; port++, p++)
1528 if (p->lowChkFlag) {
1529 p->lowChkFlag = 0;
1530 moxa_low_water_check(p->tableAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
1533 }
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001534 moxaLowWaterChk = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Jiri Slaby2a541342008-04-30 00:53:45 -07001536 if (served)
1537 mod_timer(&moxaTimer, jiffies + HZ / 50);
1538 spin_unlock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
1541/******************************************************************************/
1542
Alan Coxdb1acaa2008-02-08 04:18:43 -08001543static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001545 register struct ktermios *ts = tty->termios;
1546 struct moxa_port *ch = tty->driver_data;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001547 int rts, cts, txflow, rxflow, xany, baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 rts = cts = txflow = rxflow = xany = 0;
1550 if (ts->c_cflag & CRTSCTS)
1551 rts = cts = 1;
1552 if (ts->c_iflag & IXON)
1553 txflow = 1;
1554 if (ts->c_iflag & IXOFF)
1555 rxflow = 1;
1556 if (ts->c_iflag & IXANY)
1557 xany = 1;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001558
1559 /* Clear the features we don't support */
1560 ts->c_cflag &= ~CMSPAR;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001561 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1562 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
Alan Coxdb1acaa2008-02-08 04:18:43 -08001563 if (baud == -1)
1564 baud = tty_termios_baud_rate(old_termios);
1565 /* Not put the baud rate into the termios data */
1566 tty_encode_baud_rate(tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
1568
Jiri Slaby6f56b652007-10-18 03:06:24 -07001569static void moxa_setup_empty_event(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001571 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001573 spin_lock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 ch->statusflags |= EMPTYWAIT;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001575 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576}
1577
Jiri Slaby6f56b652007-10-18 03:06:24 -07001578static void moxa_shut_down(struct moxa_port *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001580 struct tty_struct *tp = ch->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1583 return;
1584
Jiri Slabyb4173f42008-04-30 00:53:40 -07001585 MoxaPortDisable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 /*
1588 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1589 */
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001590 if (C_HUPCL(tp))
Jiri Slabyb4173f42008-04-30 00:53:40 -07001591 MoxaPortLineCtrl(ch, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001593 spin_lock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 ch->asyncflags &= ~ASYNC_INITIALIZED;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001595 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596}
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598/*****************************************************************************
1599 * Driver level functions: *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Jiri Slabyb4173f42008-04-30 00:53:40 -07001602static void MoxaPortFlushData(struct moxa_port *port, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
1604 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001605 if (mode < 0 || mode > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001607 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 moxafunc(ofsAddr, FC_FlushQueue, mode);
1609 if (mode != 1) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001610 port->lowChkFlag = 0;
Jiri Slaby6f56b652007-10-18 03:06:24 -07001611 moxa_low_water_check(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 }
1613}
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615/*
1616 * Moxa Port Number Description:
1617 *
1618 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1619 * the port number using in MOXA driver functions will be 0 to 31 for
1620 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1621 * to 127 for fourth. For example, if you setup three MOXA boards,
1622 * first board is C218, second board is C320-16 and third board is
1623 * C320-32. The port number of first board (C218 - 8 ports) is from
1624 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1625 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1626 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1627 * 127 will be invalid.
1628 *
1629 *
1630 * Moxa Functions Description:
1631 *
1632 * Function 1: Driver initialization routine, this routine must be
1633 * called when initialized driver.
1634 * Syntax:
1635 * void MoxaDriverInit();
1636 *
1637 *
1638 * Function 2: Moxa driver private IOCTL command processing.
1639 * Syntax:
1640 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1641 *
1642 * unsigned int cmd : IOCTL command
1643 * unsigned long arg : IOCTL argument
1644 * int port : port number (0 - 127)
1645 *
1646 * return: 0 (OK)
1647 * -EINVAL
1648 * -ENOIOCTLCMD
1649 *
1650 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 * Function 6: Enable this port to start Tx/Rx data.
1652 * Syntax:
1653 * void MoxaPortEnable(int port);
1654 * int port : port number (0 - 127)
1655 *
1656 *
1657 * Function 7: Disable this port
1658 * Syntax:
1659 * void MoxaPortDisable(int port);
1660 * int port : port number (0 - 127)
1661 *
1662 *
1663 * Function 8: Get the maximun available baud rate of this port.
1664 * Syntax:
1665 * long MoxaPortGetMaxBaud(int port);
1666 * int port : port number (0 - 127)
1667 *
1668 * return: 0 : this port is invalid
1669 * 38400/57600/115200 bps
1670 *
1671 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 * Function 10: Setting baud rate of this port.
1673 * Syntax:
1674 * long MoxaPortSetBaud(int port, long baud);
1675 * int port : port number (0 - 127)
1676 * long baud : baud rate (50 - 115200)
1677 *
1678 * return: 0 : this port is invalid or baud < 50
1679 * 50 - 115200 : the real baud rate set to the port, if
1680 * the argument baud is large than maximun
1681 * available baud rate, the real setting
1682 * baud rate will be the maximun baud rate.
1683 *
1684 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 * Function 12: Configure the port.
1686 * Syntax:
Alan Cox606d0992006-12-08 02:38:45 -08001687 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 * int port : port number (0 - 127)
Alan Cox606d0992006-12-08 02:38:45 -08001689 * struct ktermios * termio : termio structure pointer
Alan Coxc7bce302006-09-30 23:27:24 -07001690 * speed_t baud : baud rate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 *
1692 * return: -1 : this port is invalid or termio == NULL
1693 * 0 : setting O.K.
1694 *
1695 *
1696 * Function 13: Get the DTR/RTS state of this port.
1697 * Syntax:
1698 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1699 * int port : port number (0 - 127)
1700 * int * dtrState : pointer to INT to receive the current DTR
1701 * state. (if NULL, this function will not
1702 * write to this address)
1703 * int * rtsState : pointer to INT to receive the current RTS
1704 * state. (if NULL, this function will not
1705 * write to this address)
1706 *
1707 * return: -1 : this port is invalid
1708 * 0 : O.K.
1709 *
1710 *
1711 * Function 14: Setting the DTR/RTS output state of this port.
1712 * Syntax:
1713 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1714 * int port : port number (0 - 127)
1715 * int dtrState : DTR output state (0: off, 1: on)
1716 * int rtsState : RTS output state (0: off, 1: on)
1717 *
1718 *
1719 * Function 15: Setting the flow control of this port.
1720 * Syntax:
1721 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1722 * int txFlow,int xany);
1723 * int port : port number (0 - 127)
1724 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1725 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1726 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1727 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1728 * int xany : S/W XANY flow control (0: no, 1: yes)
1729 *
1730 *
1731 * Function 16: Get ths line status of this port
1732 * Syntax:
1733 * int MoxaPortLineStatus(int port);
1734 * int port : port number (0 - 127)
1735 *
1736 * return: Bit 0 - CTS state (0: off, 1: on)
1737 * Bit 1 - DSR state (0: off, 1: on)
1738 * Bit 2 - DCD state (0: off, 1: on)
1739 *
1740 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 * Function 19: Flush the Rx/Tx buffer data of this port.
1742 * Syntax:
1743 * void MoxaPortFlushData(int port, int mode);
1744 * int port : port number (0 - 127)
1745 * int mode
1746 * 0 : flush the Rx buffer
1747 * 1 : flush the Tx buffer
1748 * 2 : flush the Rx and Tx buffer
1749 *
1750 *
1751 * Function 20: Write data.
1752 * Syntax:
1753 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1754 * int port : port number (0 - 127)
1755 * unsigned char * buffer : pointer to write data buffer.
1756 * int length : write data length
1757 *
1758 * return: 0 - length : real write data length
1759 *
1760 *
1761 * Function 21: Read data.
1762 * Syntax:
Alan Cox33f0f882006-01-09 20:54:13 -08001763 * int MoxaPortReadData(int port, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 * int port : port number (0 - 127)
Alan Cox33f0f882006-01-09 20:54:13 -08001765 * struct tty_struct *tty : tty for data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 *
1767 * return: 0 - length : real read data length
1768 *
1769 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 * Function 24: Get the Tx buffer current queued data bytes
1771 * Syntax:
1772 * int MoxaPortTxQueue(int port);
1773 * int port : port number (0 - 127)
1774 *
1775 * return: .. : Tx buffer current queued data bytes
1776 *
1777 *
1778 * Function 25: Get the Tx buffer current free space
1779 * Syntax:
1780 * int MoxaPortTxFree(int port);
1781 * int port : port number (0 - 127)
1782 *
1783 * return: .. : Tx buffer current free space
1784 *
1785 *
1786 * Function 26: Get the Rx buffer current queued data bytes
1787 * Syntax:
1788 * int MoxaPortRxQueue(int port);
1789 * int port : port number (0 - 127)
1790 *
1791 * return: .. : Rx buffer current queued data bytes
1792 *
1793 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 * Function 28: Disable port data transmission.
1795 * Syntax:
1796 * void MoxaPortTxDisable(int port);
1797 * int port : port number (0 - 127)
1798 *
1799 *
1800 * Function 29: Enable port data transmission.
1801 * Syntax:
1802 * void MoxaPortTxEnable(int port);
1803 * int port : port number (0 - 127)
1804 *
1805 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 * Function 31: Get the received BREAK signal count and reset it.
1807 * Syntax:
1808 * int MoxaPortResetBrkCnt(int port);
1809 * int port : port number (0 - 127)
1810 *
1811 * return: 0 - .. : BREAK signal count
1812 *
1813 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Jiri Slabyb4173f42008-04-30 00:53:40 -07001816static void MoxaPortEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
1818 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001819 u16 lowwater = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Jiri Slabyb4173f42008-04-30 00:53:40 -07001821 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 writew(lowwater, ofsAddr + Low_water);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001823 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001824 port->board->boardType == MOXA_BOARD_C320_PCI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001826 else
1827 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1828 ofsAddr + HostStat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1831 moxafunc(ofsAddr, FC_FlushQueue, 2);
1832
1833 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1834 MoxaPortLineStatus(port);
1835}
1836
Jiri Slabyb4173f42008-04-30 00:53:40 -07001837static void MoxaPortDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001839 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
1841 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1842 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1843 writew(0, ofsAddr + HostStat);
1844 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1845}
1846
Jiri Slabyb4173f42008-04-30 00:53:40 -07001847static long MoxaPortGetMaxBaud(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001849 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1850 port->board->boardType == MOXA_BOARD_C320_PCI)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001851 return 460800L;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 else
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001853 return 921600L;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855
1856
Jiri Slabyb4173f42008-04-30 00:53:40 -07001857static long MoxaPortSetBaud(struct moxa_port *port, long baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
1859 void __iomem *ofsAddr;
1860 long max, clock;
1861 unsigned int val;
1862
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001863 if (baud < 50L || (max = MoxaPortGetMaxBaud(port)) == 0)
1864 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001865 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 if (baud > max)
1867 baud = max;
1868 if (max == 38400L)
1869 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
1870 else if (max == 57600L)
1871 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
1872 else
1873 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
1874 val = clock / baud;
1875 moxafunc(ofsAddr, FC_SetBaud, val);
1876 baud = clock / val;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001877 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878}
1879
Jiri Slabyb4173f42008-04-30 00:53:40 -07001880static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1881 speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
1883 void __iomem *ofsAddr;
1884 tcflag_t cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 tcflag_t mode = 0;
1886
Jiri Slabyb4173f42008-04-30 00:53:40 -07001887 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 cflag = termio->c_cflag; /* termio->c_cflag */
1889
1890 mode = termio->c_cflag & CSIZE;
1891 if (mode == CS5)
1892 mode = MX_CS5;
1893 else if (mode == CS6)
1894 mode = MX_CS6;
1895 else if (mode == CS7)
1896 mode = MX_CS7;
1897 else if (mode == CS8)
1898 mode = MX_CS8;
1899
1900 if (termio->c_cflag & CSTOPB) {
1901 if (mode == MX_CS5)
1902 mode |= MX_STOP15;
1903 else
1904 mode |= MX_STOP2;
1905 } else
1906 mode |= MX_STOP1;
1907
1908 if (termio->c_cflag & PARENB) {
1909 if (termio->c_cflag & PARODD)
1910 mode |= MX_PARODD;
1911 else
1912 mode |= MX_PAREVEN;
1913 } else
1914 mode |= MX_PARNONE;
1915
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001916 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Jiri Slabyb4173f42008-04-30 00:53:40 -07001918 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1919 port->board->boardType == MOXA_BOARD_C320_PCI) {
Alan Coxc7bce302006-09-30 23:27:24 -07001920 if (baud >= 921600L)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001921 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 }
Alan Coxdb1acaa2008-02-08 04:18:43 -08001923 baud = MoxaPortSetBaud(port, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1926 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1927 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1928 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
Jiri Slaby6f56b652007-10-18 03:06:24 -07001929 moxa_wait_finish(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 }
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001932 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
1934
Jiri Slabyb4173f42008-04-30 00:53:40 -07001935static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1936 int *rtsState)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001938 if (dtrState)
1939 *dtrState = !!(port->lineCtrl & DTR_ON);
1940 if (rtsState)
1941 *rtsState = !!(port->lineCtrl & RTS_ON);
1942
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001943 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944}
1945
Jiri Slabyb4173f42008-04-30 00:53:40 -07001946static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001948 u8 mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 if (dtr)
1951 mode |= DTR_ON;
1952 if (rts)
1953 mode |= RTS_ON;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001954 port->lineCtrl = mode;
1955 moxafunc(port->tableAddr, FC_LineControl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956}
1957
Jiri Slabyb4173f42008-04-30 00:53:40 -07001958static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1959 int txflow, int rxflow, int txany)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001961 int mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 if (rts)
1964 mode |= RTS_FlowCtl;
1965 if (cts)
1966 mode |= CTS_FlowCtl;
1967 if (txflow)
1968 mode |= Tx_FlowCtl;
1969 if (rxflow)
1970 mode |= Rx_FlowCtl;
1971 if (txany)
1972 mode |= IXM_IXANY;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001973 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974}
1975
Jiri Slabyb4173f42008-04-30 00:53:40 -07001976static int MoxaPortLineStatus(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
1978 void __iomem *ofsAddr;
1979 int val;
1980
Jiri Slabyb4173f42008-04-30 00:53:40 -07001981 ofsAddr = port->tableAddr;
1982 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1983 port->board->boardType == MOXA_BOARD_C320_PCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 moxafunc(ofsAddr, FC_LineStatus, 0);
1985 val = readw(ofsAddr + FuncArg);
1986 } else {
1987 val = readw(ofsAddr + FlagStat) >> 4;
1988 }
1989 val &= 0x0B;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001990 if (val & 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 val |= 4;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001992 spin_lock_bh(&moxa_lock);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001993 moxa_new_dcdstate(port, val & 8);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001994 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 val &= 7;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001996 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
Jiri Slaby2108eba2008-04-30 00:53:44 -07001999static int MoxaPortWriteData(struct moxa_port *port,
2000 const unsigned char *buffer, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002003 unsigned int c, total;
2004 u16 head, tail, tx_mask, spage, epage;
2005 u16 pageno, pageofs, bufhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Jiri Slabyb4173f42008-04-30 00:53:40 -07002007 ofsAddr = port->tableAddr;
2008 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 tx_mask = readw(ofsAddr + TX_mask);
2010 spage = readw(ofsAddr + Page_txb);
2011 epage = readw(ofsAddr + EndPage_txb);
2012 tail = readw(ofsAddr + TXwptr);
2013 head = readw(ofsAddr + TXrptr);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002014 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (c > len)
2016 c = len;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002017 moxaLog.txcnt[port->tty->index] += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 total = c;
2019 if (spage == epage) {
2020 bufhead = readw(ofsAddr + Ofs_txb);
2021 writew(spage, baseAddr + Control_reg);
2022 while (c > 0) {
2023 if (head > tail)
2024 len = head - tail - 1;
2025 else
2026 len = tx_mask + 1 - tail;
2027 len = (c > len) ? len : c;
2028 ofs = baseAddr + DynPage_addr + bufhead + tail;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002029 memcpy_toio(ofs, buffer, len);
2030 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 tail = (tail + len) & tx_mask;
2032 c -= len;
2033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 pageno = spage + (tail >> 13);
2036 pageofs = tail & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002037 while (c > 0) {
2038 len = Page_size - pageofs;
2039 if (len > c)
2040 len = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 writeb(pageno, baseAddr + Control_reg);
2042 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002043 memcpy_toio(ofs, buffer, len);
2044 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 if (++pageno == epage)
2046 pageno = spage;
2047 pageofs = 0;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002048 c -= len;
2049 }
2050 tail = (tail + total) & tx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002052 writew(tail, ofsAddr + TXwptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 writeb(1, ofsAddr + CD180TXirq); /* start to send */
Jiri Slaby2108eba2008-04-30 00:53:44 -07002054 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002057static int MoxaPortReadData(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002059 struct tty_struct *tty = port->tty;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002060 unsigned char *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002062 unsigned int count, len, total;
2063 u16 tail, rx_mask, spage, epage;
2064 u16 pageno, pageofs, bufhead, head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Jiri Slabyb4173f42008-04-30 00:53:40 -07002066 ofsAddr = port->tableAddr;
2067 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 head = readw(ofsAddr + RXrptr);
2069 tail = readw(ofsAddr + RXwptr);
2070 rx_mask = readw(ofsAddr + RX_mask);
2071 spage = readw(ofsAddr + Page_rxb);
2072 epage = readw(ofsAddr + EndPage_rxb);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002073 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 if (count == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08002075 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Alan Cox33f0f882006-01-09 20:54:13 -08002077 total = count;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002078 moxaLog.rxcnt[tty->index] += total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if (spage == epage) {
2080 bufhead = readw(ofsAddr + Ofs_rxb);
2081 writew(spage, baseAddr + Control_reg);
2082 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 ofs = baseAddr + DynPage_addr + bufhead + head;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002084 len = (tail >= head) ? (tail - head) :
2085 (rx_mask + 1 - head);
2086 len = tty_prepare_flip_string(tty, &dst,
2087 min(len, count));
2088 memcpy_fromio(dst, ofs, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 head = (head + len) & rx_mask;
2090 count -= len;
2091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 pageno = spage + (head >> 13);
2094 pageofs = head & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002095 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 writew(pageno, baseAddr + Control_reg);
2097 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002098 len = tty_prepare_flip_string(tty, &dst,
2099 min(Page_size - pageofs, count));
2100 memcpy_fromio(dst, ofs, len);
2101
2102 count -= len;
2103 pageofs = (pageofs + len) & Page_mask;
2104 if (pageofs == 0 && ++pageno == epage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 pageno = spage;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002106 }
2107 head = (head + total) & rx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002109 writew(head, ofsAddr + RXrptr);
2110 if (readb(ofsAddr + FlagStat) & Xoff_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 moxaLowWaterChk = 1;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002112 port->lowChkFlag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002114 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115}
2116
2117
Jiri Slabyb4173f42008-04-30 00:53:40 -07002118static int MoxaPortTxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002120 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002121 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 rptr = readw(ofsAddr + TXrptr);
2124 wptr = readw(ofsAddr + TXwptr);
2125 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002126 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
2128
Jiri Slabyb4173f42008-04-30 00:53:40 -07002129static int MoxaPortTxFree(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002131 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002132 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 rptr = readw(ofsAddr + TXrptr);
2135 wptr = readw(ofsAddr + TXwptr);
2136 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002137 return mask - ((wptr - rptr) & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138}
2139
Jiri Slabyb4173f42008-04-30 00:53:40 -07002140static int MoxaPortRxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002142 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002143 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 rptr = readw(ofsAddr + RXrptr);
2146 wptr = readw(ofsAddr + RXwptr);
2147 mask = readw(ofsAddr + RX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002148 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149}
2150
Jiri Slabyb4173f42008-04-30 00:53:40 -07002151static void MoxaPortTxDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002153 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
2155
Jiri Slabyb4173f42008-04-30 00:53:40 -07002156static void MoxaPortTxEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002158 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159}
2160
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002161static int moxa_get_serial_info(struct moxa_port *info,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002162 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002164 struct serial_struct tmp = {
2165 .type = info->type,
2166 .line = info->tty->index,
2167 .flags = info->asyncflags,
2168 .baud_base = 921600,
2169 .close_delay = info->close_delay
2170 };
2171 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
2173
2174
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002175static int moxa_set_serial_info(struct moxa_port *info,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002176 struct serial_struct __user *new_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
2178 struct serial_struct new_serial;
2179
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002180 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 return -EFAULT;
2182
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002183 if (new_serial.irq != 0 || new_serial.port != 0 ||
2184 new_serial.custom_divisor != 0 ||
2185 new_serial.baud_base != 921600)
2186 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 if (!capable(CAP_SYS_ADMIN)) {
2189 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2190 (info->asyncflags & ~ASYNC_USR_MASK)))
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002191 return -EPERM;
2192 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 info->close_delay = new_serial.close_delay * HZ / 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2196 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2197
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002198 MoxaSetFifo(info, new_serial.type == PORT_16550A);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
2200 info->type = new_serial.type;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202}
2203
2204
2205
2206/*****************************************************************************
2207 * Static local functions: *
2208 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Jiri Slabyb4173f42008-04-30 00:53:40 -07002210static void MoxaSetFifo(struct moxa_port *port, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002212 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 if (!enable) {
2215 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2216 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2217 } else {
2218 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2219 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2220 }
2221}