blob: d8bcbed6d28ddf213ed2d8d87cb4040083a10f01 [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>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040037#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/string.h>
39#include <linux/fcntl.h>
40#include <linux/ptrace.h>
41#include <linux/serial.h>
42#include <linux/tty_driver.h>
43#include <linux/delay.h>
44#include <linux/pci.h>
45#include <linux/init.h>
46#include <linux/bitops.h>
47
48#include <asm/system.h>
49#include <asm/io.h>
50#include <asm/uaccess.h>
51
Jiri Slaby03718232008-04-30 00:53:39 -070052#include "moxa.h"
53
Jiri Slabyb9705b62008-04-30 00:53:48 -070054#define MOXA_VERSION "6.0k"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Jiri Slaby03718232008-04-30 00:53:39 -070056#define MOXA_FW_HDRLEN 32
57
Jiri Slaby11324ed2007-02-10 01:45:31 -080058#define MOXAMAJOR 172
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Jiri Slaby11324ed2007-02-10 01:45:31 -080060#define MAX_BOARDS 4 /* Don't change this value */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
Jiri Slaby11324ed2007-02-10 01:45:31 -080062#define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Jiri Slaby08d01c792008-04-30 00:53:47 -070064#define MOXA_IS_320(brd) ((brd)->boardType == MOXA_BOARD_C320_ISA || \
65 (brd)->boardType == MOXA_BOARD_C320_PCI)
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
68 * Define the Moxa PCI vendor and device IDs.
69 */
Jiri Slaby11324ed2007-02-10 01:45:31 -080070#define MOXA_BUS_TYPE_ISA 0
71#define MOXA_BUS_TYPE_PCI 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073enum {
74 MOXA_BOARD_C218_PCI = 1,
75 MOXA_BOARD_C218_ISA,
76 MOXA_BOARD_C320_PCI,
77 MOXA_BOARD_C320_ISA,
78 MOXA_BOARD_CP204J,
79};
80
81static char *moxa_brdname[] =
82{
83 "C218 Turbo PCI series",
84 "C218 Turbo ISA series",
85 "C320 Turbo PCI series",
86 "C320 Turbo ISA series",
87 "CP-204J series",
88};
89
90#ifdef CONFIG_PCI
91static struct pci_device_id moxa_pcibrds[] = {
Jiri Slaby5ebb4072007-02-10 01:45:30 -080092 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
93 .driver_data = MOXA_BOARD_C218_PCI },
94 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
95 .driver_data = MOXA_BOARD_C320_PCI },
96 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
97 .driver_data = MOXA_BOARD_CP204J },
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 { 0 }
99};
100MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
101#endif /* CONFIG_PCI */
102
Jiri Slaby03718232008-04-30 00:53:39 -0700103struct moxa_port;
104
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800105static struct moxa_board_conf {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 int boardType;
107 int numPorts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 int busType;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800109
Jiri Slaby810ab092008-04-30 00:53:41 -0700110 unsigned int ready;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800111
Jiri Slaby03718232008-04-30 00:53:39 -0700112 struct moxa_port *ports;
113
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800114 void __iomem *basemem;
115 void __iomem *intNdx;
116 void __iomem *intPend;
117 void __iomem *intTable;
118} moxa_boards[MAX_BOARDS];
119
120struct mxser_mstatus {
121 tcflag_t cflag;
122 int cts;
123 int dsr;
124 int ri;
125 int dcd;
Jiri Slaby9dff89c2007-02-10 01:45:30 -0800126};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800128struct moxaq_str {
129 int inq;
130 int outq;
131};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800133struct moxa_port {
Alan Cox9de6a512008-07-16 21:56:02 +0100134 struct tty_port port;
Jiri Slabyb4173f42008-04-30 00:53:40 -0700135 struct moxa_board_conf *board;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700136 void __iomem *tableAddr;
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 int cflag;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700140 unsigned long statusflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700142 u8 DCDState;
143 u8 lineCtrl;
144 u8 lowChkFlag;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800145};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Jiri Slaby74d7d972008-04-30 00:53:43 -0700147struct mon_str {
148 int tick;
149 int rxcnt[MAX_PORTS];
150 int txcnt[MAX_PORTS];
151};
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/* statusflags */
154#define TXSTOPPED 0x1
155#define LOWWAIT 0x2
156#define EMPTYWAIT 0x4
157#define THROTTLE 0x8
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#define SERIAL_DO_RESTART
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#define WAKEUP_CHARS 256
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163static int ttymajor = MOXAMAJOR;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700164static struct mon_str moxaLog;
165static unsigned int moxaFuncTout = HZ / 2;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700166static unsigned int moxaLowWaterChk;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700167static DEFINE_MUTEX(moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/* Variables for insmod */
169#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700170static unsigned long baseaddr[MAX_BOARDS];
171static unsigned int type[MAX_BOARDS];
172static unsigned int numports[MAX_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173#endif
174
175MODULE_AUTHOR("William Chen");
176MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
177MODULE_LICENSE("GPL");
178#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700179module_param_array(type, uint, NULL, 0);
180MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
181module_param_array(baseaddr, ulong, NULL, 0);
182MODULE_PARM_DESC(baseaddr, "base address");
183module_param_array(numports, uint, NULL, 0);
184MODULE_PARM_DESC(numports, "numports (ignored for C218)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185#endif
186module_param(ttymajor, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/*
189 * static functions:
190 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191static int moxa_open(struct tty_struct *, struct file *);
192static void moxa_close(struct tty_struct *, struct file *);
193static int moxa_write(struct tty_struct *, const unsigned char *, int);
194static int moxa_write_room(struct tty_struct *);
195static void moxa_flush_buffer(struct tty_struct *);
196static int moxa_chars_in_buffer(struct tty_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197static void moxa_throttle(struct tty_struct *);
198static void moxa_unthrottle(struct tty_struct *);
Alan Cox606d0992006-12-08 02:38:45 -0800199static void moxa_set_termios(struct tty_struct *, struct ktermios *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200static void moxa_stop(struct tty_struct *);
201static void moxa_start(struct tty_struct *);
202static void moxa_hangup(struct tty_struct *);
203static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
204static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
205 unsigned int set, unsigned int clear);
206static void moxa_poll(unsigned long);
Alan Coxdb1acaa2008-02-08 04:18:43 -0800207static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
Jiri Slaby6f56b6582007-10-18 03:06:24 -0700208static void moxa_setup_empty_event(struct tty_struct *);
Alan Coxf1761782009-11-30 13:17:51 +0000209static void moxa_shutdown(struct tty_port *);
Alan Cox31f35932009-01-02 13:45:05 +0000210static int moxa_carrier_raised(struct tty_port *);
Alan Coxf1761782009-11-30 13:17:51 +0000211static void moxa_dtr_rts(struct tty_port *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212/*
213 * moxa board interface functions:
214 */
Jiri Slabyb4173f42008-04-30 00:53:40 -0700215static void MoxaPortEnable(struct moxa_port *);
216static void MoxaPortDisable(struct moxa_port *);
217static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
218static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
219static void MoxaPortLineCtrl(struct moxa_port *, int, int);
220static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
221static int MoxaPortLineStatus(struct moxa_port *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700222static void MoxaPortFlushData(struct moxa_port *, int);
Alan Coxd450b5a2008-10-13 10:39:58 +0100223static int MoxaPortWriteData(struct tty_struct *, const unsigned char *, int);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700224static int MoxaPortReadData(struct moxa_port *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700225static int MoxaPortTxQueue(struct moxa_port *);
226static int MoxaPortRxQueue(struct moxa_port *);
227static int MoxaPortTxFree(struct moxa_port *);
228static void MoxaPortTxDisable(struct moxa_port *);
229static void MoxaPortTxEnable(struct moxa_port *);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800230static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
231static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700232static void MoxaSetFifo(struct moxa_port *port, int enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Jiri Slaby74d7d972008-04-30 00:53:43 -0700234/*
235 * I/O functions
236 */
237
238static void moxa_wait_finish(void __iomem *ofsAddr)
239{
240 unsigned long end = jiffies + moxaFuncTout;
241
242 while (readw(ofsAddr + FuncCode) != 0)
243 if (time_after(jiffies, end))
244 return;
245 if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
246 printk(KERN_WARNING "moxa function expired\n");
247}
248
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700249static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
Jiri Slaby74d7d972008-04-30 00:53:43 -0700250{
251 writew(arg, ofsAddr + FuncArg);
252 writew(cmd, ofsAddr + FuncCode);
253 moxa_wait_finish(ofsAddr);
254}
255
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700256static void moxa_low_water_check(void __iomem *ofsAddr)
257{
258 u16 rptr, wptr, mask, len;
259
260 if (readb(ofsAddr + FlagStat) & Xoff_state) {
261 rptr = readw(ofsAddr + RXrptr);
262 wptr = readw(ofsAddr + RXwptr);
263 mask = readw(ofsAddr + RX_mask);
264 len = (wptr - rptr) & mask;
265 if (len <= Low_water)
266 moxafunc(ofsAddr, FC_SendXon, 0);
267 }
268}
269
Jiri Slaby74d7d972008-04-30 00:53:43 -0700270/*
271 * TTY operations
272 */
273
274static int moxa_ioctl(struct tty_struct *tty, struct file *file,
275 unsigned int cmd, unsigned long arg)
276{
277 struct moxa_port *ch = tty->driver_data;
278 void __user *argp = (void __user *)arg;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700279 int status, ret = 0;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700280
281 if (tty->index == MAX_PORTS) {
282 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
283 cmd != MOXA_GETMSTATUS)
284 return -EINVAL;
285 } else if (!ch)
286 return -ENODEV;
287
288 switch (cmd) {
289 case MOXA_GETDATACOUNT:
290 moxaLog.tick = jiffies;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700291 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
292 ret = -EFAULT;
293 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700294 case MOXA_FLUSH_QUEUE:
295 MoxaPortFlushData(ch, arg);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700296 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700297 case MOXA_GET_IOQUEUE: {
298 struct moxaq_str __user *argm = argp;
299 struct moxaq_str tmp;
300 struct moxa_port *p;
301 unsigned int i, j;
302
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700303 mutex_lock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700304 for (i = 0; i < MAX_BOARDS; i++) {
305 p = moxa_boards[i].ports;
306 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
307 memset(&tmp, 0, sizeof(tmp));
308 if (moxa_boards[i].ready) {
309 tmp.inq = MoxaPortRxQueue(p);
310 tmp.outq = MoxaPortTxQueue(p);
311 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700312 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
313 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700314 return -EFAULT;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700315 }
Jiri Slaby74d7d972008-04-30 00:53:43 -0700316 }
317 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700318 mutex_unlock(&moxa_openlock);
319 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700320 } case MOXA_GET_OQUEUE:
321 status = MoxaPortTxQueue(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700322 ret = put_user(status, (unsigned long __user *)argp);
323 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700324 case MOXA_GET_IQUEUE:
325 status = MoxaPortRxQueue(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700326 ret = put_user(status, (unsigned long __user *)argp);
327 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700328 case MOXA_GETMSTATUS: {
329 struct mxser_mstatus __user *argm = argp;
330 struct mxser_mstatus tmp;
331 struct moxa_port *p;
332 unsigned int i, j;
333
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700334 mutex_lock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700335 for (i = 0; i < MAX_BOARDS; i++) {
336 p = moxa_boards[i].ports;
337 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
Alan Coxd450b5a2008-10-13 10:39:58 +0100338 struct tty_struct *ttyp;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700339 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
Alan Coxd450b5a2008-10-13 10:39:58 +0100351 ttyp = tty_port_tty_get(&p->port);
352 if (!ttyp || !ttyp->termios)
Jiri Slaby74d7d972008-04-30 00:53:43 -0700353 tmp.cflag = p->cflag;
354 else
Alan Coxd450b5a2008-10-13 10:39:58 +0100355 tmp.cflag = ttyp->termios->c_cflag;
356 tty_kref_put(tty);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700357copy:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700358 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
359 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700360 return -EFAULT;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700361 }
Jiri Slaby74d7d972008-04-30 00:53:43 -0700362 }
363 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700364 mutex_unlock(&moxa_openlock);
365 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700366 }
367 case TIOCGSERIAL:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700368 mutex_lock(&moxa_openlock);
369 ret = moxa_get_serial_info(ch, argp);
370 mutex_unlock(&moxa_openlock);
371 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700372 case TIOCSSERIAL:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700373 mutex_lock(&moxa_openlock);
374 ret = moxa_set_serial_info(ch, argp);
375 mutex_unlock(&moxa_openlock);
376 break;
377 default:
378 ret = -ENOIOCTLCMD;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700379 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700380 return ret;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700381}
382
Alan Cox9e989662008-07-22 11:18:03 +0100383static int moxa_break_ctl(struct tty_struct *tty, int state)
Jiri Slaby74d7d972008-04-30 00:53:43 -0700384{
385 struct moxa_port *port = tty->driver_data;
386
387 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
388 Magic_code);
Alan Cox9e989662008-07-22 11:18:03 +0100389 return 0;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700390}
391
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700392static const struct tty_operations moxa_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 .open = moxa_open,
394 .close = moxa_close,
395 .write = moxa_write,
396 .write_room = moxa_write_room,
397 .flush_buffer = moxa_flush_buffer,
398 .chars_in_buffer = moxa_chars_in_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 .ioctl = moxa_ioctl,
400 .throttle = moxa_throttle,
401 .unthrottle = moxa_unthrottle,
402 .set_termios = moxa_set_termios,
403 .stop = moxa_stop,
404 .start = moxa_start,
405 .hangup = moxa_hangup,
Jiri Slaby74d7d972008-04-30 00:53:43 -0700406 .break_ctl = moxa_break_ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 .tiocmget = moxa_tiocmget,
408 .tiocmset = moxa_tiocmset,
409};
410
Alan Cox31f35932009-01-02 13:45:05 +0000411static const struct tty_port_operations moxa_port_ops = {
412 .carrier_raised = moxa_carrier_raised,
Alan Coxf1761782009-11-30 13:17:51 +0000413 .dtr_rts = moxa_dtr_rts,
414 .shutdown = moxa_shutdown,
Alan Cox31f35932009-01-02 13:45:05 +0000415};
416
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800417static struct tty_driver *moxaDriver;
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800418static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
Ingo Molnar34af9462006-06-27 02:53:55 -0700419static DEFINE_SPINLOCK(moxa_lock);
Alan Cox33f0f882006-01-09 20:54:13 -0800420
Jiri Slaby74d7d972008-04-30 00:53:43 -0700421/*
422 * HW init
423 */
424
Jiri Slaby03718232008-04-30 00:53:39 -0700425static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
426{
427 switch (brd->boardType) {
428 case MOXA_BOARD_C218_ISA:
429 case MOXA_BOARD_C218_PCI:
430 if (model != 1)
431 goto err;
432 break;
433 case MOXA_BOARD_CP204J:
434 if (model != 3)
435 goto err;
436 break;
437 default:
438 if (model != 2)
439 goto err;
440 break;
441 }
442 return 0;
443err:
444 return -EINVAL;
445}
446
447static int moxa_check_fw(const void *ptr)
448{
449 const __le16 *lptr = ptr;
450
451 if (*lptr != cpu_to_le16(0x7980))
452 return -EINVAL;
453
454 return 0;
455}
456
457static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
458 size_t len)
459{
460 void __iomem *baseAddr = brd->basemem;
461 u16 tmp;
462
463 writeb(HW_reset, baseAddr + Control_reg); /* reset */
464 msleep(10);
465 memset_io(baseAddr, 0, 4096);
466 memcpy_toio(baseAddr, buf, len); /* download BIOS */
467 writeb(0, baseAddr + Control_reg); /* restart */
468
469 msleep(2000);
470
471 switch (brd->boardType) {
472 case MOXA_BOARD_C218_ISA:
473 case MOXA_BOARD_C218_PCI:
474 tmp = readw(baseAddr + C218_key);
475 if (tmp != C218_KeyCode)
476 goto err;
477 break;
478 case MOXA_BOARD_CP204J:
479 tmp = readw(baseAddr + C218_key);
480 if (tmp != CP204J_KeyCode)
481 goto err;
482 break;
483 default:
484 tmp = readw(baseAddr + C320_key);
485 if (tmp != C320_KeyCode)
486 goto err;
487 tmp = readw(baseAddr + C320_status);
488 if (tmp != STS_init) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700489 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
Jiri Slaby03718232008-04-30 00:53:39 -0700490 "module not found\n");
491 return -EIO;
492 }
493 break;
494 }
495
496 return 0;
497err:
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700498 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700499 return -EIO;
500}
501
502static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
503 size_t len)
504{
505 void __iomem *baseAddr = brd->basemem;
506
507 if (len < 7168) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700508 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700509 return -EINVAL;
510 }
511
512 writew(len - 7168 - 2, baseAddr + C320bapi_len);
513 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
514 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
515 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
516 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
517
518 return 0;
519}
520
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700521static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
Jiri Slaby03718232008-04-30 00:53:39 -0700522 size_t len)
523{
524 void __iomem *baseAddr = brd->basemem;
Harvey Harrisonb46f69c2008-10-15 22:04:19 -0700525 const __le16 *uptr = ptr;
Jiri Slaby03718232008-04-30 00:53:39 -0700526 size_t wlen, len2, j;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700527 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
Jiri Slaby08d01c792008-04-30 00:53:47 -0700528 unsigned int i, retry;
Jiri Slaby03718232008-04-30 00:53:39 -0700529 u16 usum, keycode;
530
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700531 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
532 C218_KeyCode;
533
534 switch (brd->boardType) {
535 case MOXA_BOARD_CP204J:
536 case MOXA_BOARD_C218_ISA:
537 case MOXA_BOARD_C218_PCI:
538 key = C218_key;
539 loadbuf = C218_LoadBuf;
540 loadlen = C218DLoad_len;
541 checksum = C218check_sum;
542 checksum_ok = C218chksum_ok;
543 break;
544 default:
545 key = C320_key;
546 keycode = C320_KeyCode;
547 loadbuf = C320_LoadBuf;
548 loadlen = C320DLoad_len;
549 checksum = C320check_sum;
550 checksum_ok = C320chksum_ok;
551 break;
552 }
553
Jiri Slaby03718232008-04-30 00:53:39 -0700554 usum = 0;
555 wlen = len >> 1;
556 for (i = 0; i < wlen; i++)
557 usum += le16_to_cpu(uptr[i]);
558 retry = 0;
559 do {
560 wlen = len >> 1;
561 j = 0;
562 while (wlen) {
563 len2 = (wlen > 2048) ? 2048 : wlen;
564 wlen -= len2;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700565 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
Jiri Slaby03718232008-04-30 00:53:39 -0700566 j += len2 << 1;
567
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700568 writew(len2, baseAddr + loadlen);
569 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700570 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700571 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700572 break;
573 msleep(10);
574 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700575 if (readw(baseAddr + key) != keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700576 return -EIO;
577 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700578 writew(0, baseAddr + loadlen);
579 writew(usum, baseAddr + checksum);
580 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700581 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700582 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700583 break;
584 msleep(10);
585 }
586 retry++;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700587 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
588 if (readb(baseAddr + checksum_ok) != 1)
Jiri Slaby03718232008-04-30 00:53:39 -0700589 return -EIO;
590
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700591 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700592 for (i = 0; i < 600; i++) {
593 if (readw(baseAddr + Magic_no) == Magic_code)
594 break;
595 msleep(10);
596 }
597 if (readw(baseAddr + Magic_no) != Magic_code)
598 return -EIO;
599
Jiri Slaby08d01c792008-04-30 00:53:47 -0700600 if (MOXA_IS_320(brd)) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700601 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
602 writew(0x3800, baseAddr + TMS320_PORT1);
603 writew(0x3900, baseAddr + TMS320_PORT2);
604 writew(28499, baseAddr + TMS320_CLOCK);
605 } else {
606 writew(0x3200, baseAddr + TMS320_PORT1);
607 writew(0x3400, baseAddr + TMS320_PORT2);
608 writew(19999, baseAddr + TMS320_CLOCK);
609 }
Jiri Slaby03718232008-04-30 00:53:39 -0700610 }
611 writew(1, baseAddr + Disable_IRQ);
612 writew(0, baseAddr + Magic_no);
613 for (i = 0; i < 500; i++) {
614 if (readw(baseAddr + Magic_no) == Magic_code)
615 break;
616 msleep(10);
617 }
618 if (readw(baseAddr + Magic_no) != Magic_code)
619 return -EIO;
620
Jiri Slaby08d01c792008-04-30 00:53:47 -0700621 if (MOXA_IS_320(brd)) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700622 j = readw(baseAddr + Module_cnt);
623 if (j <= 0)
624 return -EIO;
625 brd->numPorts = j * 8;
626 writew(j, baseAddr + Module_no);
627 writew(0, baseAddr + Magic_no);
628 for (i = 0; i < 600; i++) {
629 if (readw(baseAddr + Magic_no) == Magic_code)
630 break;
631 msleep(10);
632 }
633 if (readw(baseAddr + Magic_no) != Magic_code)
634 return -EIO;
Jiri Slaby03718232008-04-30 00:53:39 -0700635 }
Jiri Slaby03718232008-04-30 00:53:39 -0700636 brd->intNdx = baseAddr + IRQindex;
637 brd->intPend = baseAddr + IRQpending;
638 brd->intTable = baseAddr + IRQtable;
639
640 return 0;
641}
642
643static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
644 size_t len)
645{
646 void __iomem *ofsAddr, *baseAddr = brd->basemem;
647 struct moxa_port *port;
648 int retval, i;
649
650 if (len % 2) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700651 printk(KERN_ERR "MOXA: bios length is not even\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700652 return -EINVAL;
653 }
654
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700655 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
656 if (retval)
657 return retval;
658
Jiri Slaby03718232008-04-30 00:53:39 -0700659 switch (brd->boardType) {
660 case MOXA_BOARD_C218_ISA:
661 case MOXA_BOARD_C218_PCI:
662 case MOXA_BOARD_CP204J:
Jiri Slaby03718232008-04-30 00:53:39 -0700663 port = brd->ports;
664 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700665 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700666 port->DCDState = 0;
667 port->tableAddr = baseAddr + Extern_table +
668 Extern_size * i;
669 ofsAddr = port->tableAddr;
670 writew(C218rx_mask, ofsAddr + RX_mask);
671 writew(C218tx_mask, ofsAddr + TX_mask);
672 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
673 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
674
675 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
676 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
677
678 }
679 break;
680 default:
Jiri Slaby03718232008-04-30 00:53:39 -0700681 port = brd->ports;
682 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700683 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700684 port->DCDState = 0;
685 port->tableAddr = baseAddr + Extern_table +
686 Extern_size * i;
687 ofsAddr = port->tableAddr;
688 switch (brd->numPorts) {
689 case 8:
690 writew(C320p8rx_mask, ofsAddr + RX_mask);
691 writew(C320p8tx_mask, ofsAddr + TX_mask);
692 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
693 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
694 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
695 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
696
697 break;
698 case 16:
699 writew(C320p16rx_mask, ofsAddr + RX_mask);
700 writew(C320p16tx_mask, ofsAddr + TX_mask);
701 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
702 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
703 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
704 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
705 break;
706
707 case 24:
708 writew(C320p24rx_mask, ofsAddr + RX_mask);
709 writew(C320p24tx_mask, ofsAddr + TX_mask);
710 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
711 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
712 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
713 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
714 break;
715 case 32:
716 writew(C320p32rx_mask, ofsAddr + RX_mask);
717 writew(C320p32tx_mask, ofsAddr + TX_mask);
718 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
719 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
720 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
721 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
722 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
723 break;
724 }
725 }
726 break;
727 }
Jiri Slaby03718232008-04-30 00:53:39 -0700728 return 0;
729}
730
731static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
732{
David Howells2bca76e2008-07-08 17:37:15 +0100733 const void *ptr = fw->data;
Jiri Slaby03718232008-04-30 00:53:39 -0700734 char rsn[64];
735 u16 lens[5];
736 size_t len;
737 unsigned int a, lenp, lencnt;
738 int ret = -EINVAL;
739 struct {
740 __le32 magic; /* 0x34303430 */
741 u8 reserved1[2];
742 u8 type; /* UNIX = 3 */
743 u8 model; /* C218T=1, C320T=2, CP204=3 */
744 u8 reserved2[8];
745 __le16 len[5];
David Howells2bca76e2008-07-08 17:37:15 +0100746 } const *hdr = ptr;
Jiri Slaby03718232008-04-30 00:53:39 -0700747
748 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
749
750 if (fw->size < MOXA_FW_HDRLEN) {
751 strcpy(rsn, "too short (even header won't fit)");
752 goto err;
753 }
754 if (hdr->magic != cpu_to_le32(0x30343034)) {
755 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
756 goto err;
757 }
758 if (hdr->type != 3) {
759 sprintf(rsn, "not for linux, type is %u", hdr->type);
760 goto err;
761 }
762 if (moxa_check_fw_model(brd, hdr->model)) {
763 sprintf(rsn, "not for this card, model is %u", hdr->model);
764 goto err;
765 }
766
767 len = MOXA_FW_HDRLEN;
768 lencnt = hdr->model == 2 ? 5 : 3;
769 for (a = 0; a < ARRAY_SIZE(lens); a++) {
770 lens[a] = le16_to_cpu(hdr->len[a]);
771 if (lens[a] && len + lens[a] <= fw->size &&
772 moxa_check_fw(&fw->data[len]))
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700773 printk(KERN_WARNING "MOXA firmware: unexpected input "
Jiri Slaby03718232008-04-30 00:53:39 -0700774 "at offset %u, but going on\n", (u32)len);
775 if (!lens[a] && a < lencnt) {
776 sprintf(rsn, "too few entries in fw file");
777 goto err;
778 }
779 len += lens[a];
780 }
781
782 if (len != fw->size) {
783 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
784 (u32)len);
785 goto err;
786 }
787
788 ptr += MOXA_FW_HDRLEN;
789 lenp = 0; /* bios */
790
791 strcpy(rsn, "read above");
792
793 ret = moxa_load_bios(brd, ptr, lens[lenp]);
794 if (ret)
795 goto err;
796
797 /* we skip the tty section (lens[1]), since we don't need it */
798 ptr += lens[lenp] + lens[lenp + 1];
799 lenp += 2; /* comm */
800
801 if (hdr->model == 2) {
802 ret = moxa_load_320b(brd, ptr, lens[lenp]);
803 if (ret)
804 goto err;
805 /* skip another tty */
806 ptr += lens[lenp] + lens[lenp + 1];
807 lenp += 2;
808 }
809
810 ret = moxa_load_code(brd, ptr, lens[lenp]);
811 if (ret)
812 goto err;
813
814 return 0;
815err:
816 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
817 return ret;
818}
819
820static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
821{
822 const struct firmware *fw;
823 const char *file;
Jiri Slaby810ab092008-04-30 00:53:41 -0700824 struct moxa_port *p;
825 unsigned int i;
Jiri Slaby03718232008-04-30 00:53:39 -0700826 int ret;
827
Jiri Slaby810ab092008-04-30 00:53:41 -0700828 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
829 GFP_KERNEL);
830 if (brd->ports == NULL) {
831 printk(KERN_ERR "cannot allocate memory for ports\n");
832 ret = -ENOMEM;
833 goto err;
834 }
835
836 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
Alan Cox9de6a512008-07-16 21:56:02 +0100837 tty_port_init(&p->port);
Alan Cox31f35932009-01-02 13:45:05 +0000838 p->port.ops = &moxa_port_ops;
Alan Cox44b7d1b2008-07-16 21:57:18 +0100839 p->type = PORT_16550A;
840 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Jiri Slaby810ab092008-04-30 00:53:41 -0700841 }
842
Jiri Slaby03718232008-04-30 00:53:39 -0700843 switch (brd->boardType) {
844 case MOXA_BOARD_C218_ISA:
845 case MOXA_BOARD_C218_PCI:
846 file = "c218tunx.cod";
847 break;
848 case MOXA_BOARD_CP204J:
849 file = "cp204unx.cod";
850 break;
851 default:
852 file = "c320tunx.cod";
853 break;
854 }
855
856 ret = request_firmware(&fw, file, dev);
857 if (ret) {
Jiri Slabyec09cd52008-04-30 00:53:49 -0700858 printk(KERN_ERR "MOXA: request_firmware failed. Make sure "
859 "you've placed '%s' file into your firmware "
860 "loader directory (e.g. /lib/firmware)\n",
861 file);
Jiri Slaby810ab092008-04-30 00:53:41 -0700862 goto err_free;
Jiri Slaby03718232008-04-30 00:53:39 -0700863 }
864
865 ret = moxa_load_fw(brd, fw);
866
867 release_firmware(fw);
Jiri Slaby810ab092008-04-30 00:53:41 -0700868
869 if (ret)
870 goto err_free;
871
Jiri Slaby2a541342008-04-30 00:53:45 -0700872 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700873 brd->ready = 1;
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700874 if (!timer_pending(&moxaTimer))
875 mod_timer(&moxaTimer, jiffies + HZ / 50);
Jiri Slaby2a541342008-04-30 00:53:45 -0700876 spin_unlock_bh(&moxa_lock);
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700877
Jiri Slaby810ab092008-04-30 00:53:41 -0700878 return 0;
879err_free:
880 kfree(brd->ports);
881err:
Jiri Slaby03718232008-04-30 00:53:39 -0700882 return ret;
883}
884
Jiri Slaby810ab092008-04-30 00:53:41 -0700885static void moxa_board_deinit(struct moxa_board_conf *brd)
886{
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700887 unsigned int a, opened;
888
889 mutex_lock(&moxa_openlock);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700890 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700891 brd->ready = 0;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700892 spin_unlock_bh(&moxa_lock);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700893
894 /* pci hot-un-plug support */
895 for (a = 0; a < brd->numPorts; a++)
Alan Coxd450b5a2008-10-13 10:39:58 +0100896 if (brd->ports[a].port.flags & ASYNC_INITIALIZED) {
897 struct tty_struct *tty = tty_port_tty_get(
898 &brd->ports[a].port);
899 if (tty) {
900 tty_hangup(tty);
901 tty_kref_put(tty);
902 }
903 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700904 while (1) {
905 opened = 0;
906 for (a = 0; a < brd->numPorts; a++)
Alan Cox9de6a512008-07-16 21:56:02 +0100907 if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700908 opened++;
909 mutex_unlock(&moxa_openlock);
910 if (!opened)
911 break;
912 msleep(50);
913 mutex_lock(&moxa_openlock);
914 }
915
Jiri Slaby810ab092008-04-30 00:53:41 -0700916 iounmap(brd->basemem);
917 brd->basemem = NULL;
918 kfree(brd->ports);
919}
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921#ifdef CONFIG_PCI
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800922static int __devinit moxa_pci_probe(struct pci_dev *pdev,
923 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800925 struct moxa_board_conf *board;
926 unsigned int i;
927 int board_type = ent->driver_data;
928 int retval;
929
930 retval = pci_enable_device(pdev);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700931 if (retval) {
932 dev_err(&pdev->dev, "can't enable pci device\n");
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800933 goto err;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700934 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800935
936 for (i = 0; i < MAX_BOARDS; i++)
937 if (moxa_boards[i].basemem == NULL)
938 break;
939
940 retval = -ENODEV;
941 if (i >= MAX_BOARDS) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700942 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800943 "found. Board is ignored.\n", MAX_BOARDS);
944 goto err;
945 }
946
947 board = &moxa_boards[i];
Jiri Slabye46a5e32008-04-30 00:53:37 -0700948
949 retval = pci_request_region(pdev, 2, "moxa-base");
950 if (retval) {
951 dev_err(&pdev->dev, "can't request pci region 2\n");
952 goto err;
953 }
954
Alan Cox24cb2332008-04-30 00:54:19 -0700955 board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700956 if (board->basemem == NULL) {
957 dev_err(&pdev->dev, "can't remap io space 2\n");
Jiri Slabye46a5e32008-04-30 00:53:37 -0700958 goto err_reg;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700959 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 board->boardType = board_type;
962 switch (board_type) {
963 case MOXA_BOARD_C218_ISA:
964 case MOXA_BOARD_C218_PCI:
965 board->numPorts = 8;
966 break;
967
968 case MOXA_BOARD_CP204J:
969 board->numPorts = 4;
970 break;
971 default:
972 board->numPorts = 0;
973 break;
974 }
975 board->busType = MOXA_BUS_TYPE_PCI;
Jiri Slabya784bf72007-02-10 01:45:36 -0800976
Jiri Slaby03718232008-04-30 00:53:39 -0700977 retval = moxa_init_board(board, &pdev->dev);
978 if (retval)
979 goto err_base;
980
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800981 pci_set_drvdata(pdev, board);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Jiri Slabybb9f9102008-04-30 00:53:48 -0700983 dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n",
984 moxa_brdname[board_type - 1], board->numPorts);
985
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700986 return 0;
Jiri Slaby03718232008-04-30 00:53:39 -0700987err_base:
988 iounmap(board->basemem);
989 board->basemem = NULL;
Jiri Slabye46a5e32008-04-30 00:53:37 -0700990err_reg:
991 pci_release_region(pdev, 2);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800992err:
993 return retval;
994}
995
996static void __devexit moxa_pci_remove(struct pci_dev *pdev)
997{
998 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
999
Jiri Slaby810ab092008-04-30 00:53:41 -07001000 moxa_board_deinit(brd);
1001
Jiri Slabye46a5e32008-04-30 00:53:37 -07001002 pci_release_region(pdev, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003}
Jiri Slabya784bf72007-02-10 01:45:36 -08001004
1005static struct pci_driver moxa_pci_driver = {
1006 .name = "moxa",
1007 .id_table = moxa_pcibrds,
1008 .probe = moxa_pci_probe,
1009 .remove = __devexit_p(moxa_pci_remove)
1010};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011#endif /* CONFIG_PCI */
1012
1013static int __init moxa_init(void)
1014{
Jiri Slaby810ab092008-04-30 00:53:41 -07001015 unsigned int isabrds = 0;
Jiri Slabyd353eca2008-04-30 00:53:37 -07001016 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001018 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
1019 MOXA_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
1021 if (!moxaDriver)
1022 return -ENOMEM;
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 moxaDriver->owner = THIS_MODULE;
Sergey Vlasov9b4e3b12005-09-03 16:26:49 +01001025 moxaDriver->name = "ttyMX";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 moxaDriver->major = ttymajor;
1027 moxaDriver->minor_start = 0;
1028 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1029 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1030 moxaDriver->init_termios = tty_std_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Alan Cox606d0992006-12-08 02:38:45 -08001032 moxaDriver->init_termios.c_ispeed = 9600;
1033 moxaDriver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
1035 tty_set_operations(moxaDriver, &moxa_ops);
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (tty_register_driver(moxaDriver)) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001038 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 put_tty_driver(moxaDriver);
1040 return -1;
1041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Jiri Slabyd353eca2008-04-30 00:53:37 -07001043 /* Find the boards defined from module args. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -07001045 {
1046 struct moxa_board_conf *brd = moxa_boards;
Jiri Slaby810ab092008-04-30 00:53:41 -07001047 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 for (i = 0; i < MAX_BOARDS; i++) {
Jiri Slabyd353eca2008-04-30 00:53:37 -07001049 if (!baseaddr[i])
1050 break;
1051 if (type[i] == MOXA_BOARD_C218_ISA ||
1052 type[i] == MOXA_BOARD_C320_ISA) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001053 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001054 isabrds + 1, moxa_brdname[type[i] - 1],
1055 baseaddr[i]);
1056 brd->boardType = type[i];
1057 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1058 numports[i];
1059 brd->busType = MOXA_BUS_TYPE_ISA;
Alan Cox24cb2332008-04-30 00:54:19 -07001060 brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
Jiri Slabyd353eca2008-04-30 00:53:37 -07001061 if (!brd->basemem) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001062 printk(KERN_ERR "MOXA: can't remap %lx\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001063 baseaddr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 continue;
1065 }
Jiri Slaby03718232008-04-30 00:53:39 -07001066 if (moxa_init_board(brd, NULL)) {
1067 iounmap(brd->basemem);
1068 brd->basemem = NULL;
1069 continue;
1070 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001071
Jiri Slabybb9f9102008-04-30 00:53:48 -07001072 printk(KERN_INFO "MOXA isa board found at 0x%.8lu and "
1073 "ready (%u ports, firmware loaded)\n",
1074 baseaddr[i], brd->numPorts);
1075
Jiri Slabyd353eca2008-04-30 00:53:37 -07001076 brd++;
1077 isabrds++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
1079 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001084 retval = pci_register_driver(&moxa_pci_driver);
1085 if (retval) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001086 printk(KERN_ERR "Can't register MOXA pci driver!\n");
Jiri Slabyd353eca2008-04-30 00:53:37 -07001087 if (isabrds)
Jiri Slabya784bf72007-02-10 01:45:36 -08001088 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001091
Jiri Slabya784bf72007-02-10 01:45:36 -08001092 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
1095static void __exit moxa_exit(void)
1096{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001097 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001099#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001100 pci_unregister_driver(&moxa_pci_driver);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001101#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001102
Jiri Slaby810ab092008-04-30 00:53:41 -07001103 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1104 if (moxa_boards[i].ready)
1105 moxa_board_deinit(&moxa_boards[i]);
Jiri Slaby2a541342008-04-30 00:53:45 -07001106
1107 del_timer_sync(&moxaTimer);
1108
1109 if (tty_unregister_driver(moxaDriver))
1110 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1111 "serial driver\n");
1112 put_tty_driver(moxaDriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
1115module_init(moxa_init);
1116module_exit(moxa_exit);
1117
Alan Coxf1761782009-11-30 13:17:51 +00001118static void moxa_shutdown(struct tty_port *port)
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001119{
Alan Coxf1761782009-11-30 13:17:51 +00001120 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1121 MoxaPortDisable(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001122 MoxaPortFlushData(ch, 2);
Alan Coxf1761782009-11-30 13:17:51 +00001123 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001124}
1125
Alan Cox31f35932009-01-02 13:45:05 +00001126static int moxa_carrier_raised(struct tty_port *port)
1127{
1128 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1129 int dcd;
1130
1131 spin_lock_bh(&moxa_lock);
1132 dcd = ch->DCDState;
1133 spin_unlock_bh(&moxa_lock);
1134 return dcd;
1135}
1136
Alan Coxf1761782009-11-30 13:17:51 +00001137static void moxa_dtr_rts(struct tty_port *port, int onoff)
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001138{
Alan Coxf1761782009-11-30 13:17:51 +00001139 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1140 MoxaPortLineCtrl(ch, onoff, onoff);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001141}
1142
Alan Coxf1761782009-11-30 13:17:51 +00001143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144static int moxa_open(struct tty_struct *tty, struct file *filp)
1145{
Jiri Slaby810ab092008-04-30 00:53:41 -07001146 struct moxa_board_conf *brd;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001147 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 int port;
1149 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Jiri Slaby11324ed2007-02-10 01:45:31 -08001151 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (port == MAX_PORTS) {
Jiri Slaby74d7d972008-04-30 00:53:43 -07001153 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001155 if (mutex_lock_interruptible(&moxa_openlock))
1156 return -ERESTARTSYS;
Jiri Slaby810ab092008-04-30 00:53:41 -07001157 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001158 if (!brd->ready) {
1159 mutex_unlock(&moxa_openlock);
Jiri Slaby810ab092008-04-30 00:53:41 -07001160 return -ENODEV;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Dirk Eibachf0e85272009-06-11 14:56:44 +01001163 if (port % MAX_PORTS_PER_BOARD >= brd->numPorts) {
1164 mutex_unlock(&moxa_openlock);
1165 return -ENODEV;
1166 }
1167
Jiri Slaby810ab092008-04-30 00:53:41 -07001168 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
Alan Cox9de6a512008-07-16 21:56:02 +01001169 ch->port.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 tty->driver_data = ch;
Alan Coxd450b5a2008-10-13 10:39:58 +01001171 tty_port_tty_set(&ch->port, tty);
Alan Coxf1761782009-11-30 13:17:51 +00001172 mutex_lock(&ch->port.mutex);
Alan Cox9de6a512008-07-16 21:56:02 +01001173 if (!(ch->port.flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 ch->statusflags = 0;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001175 moxa_set_tty_param(tty, tty->termios);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001176 MoxaPortLineCtrl(ch, 1, 1);
1177 MoxaPortEnable(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001178 MoxaSetFifo(ch, ch->type == PORT_16550A);
Alan Cox9de6a512008-07-16 21:56:02 +01001179 ch->port.flags |= ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
Alan Coxf1761782009-11-30 13:17:51 +00001181 mutex_unlock(&ch->port.mutex);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001182 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Alan Coxf1761782009-11-30 13:17:51 +00001184 retval = tty_port_block_til_ready(&ch->port, tty, filp);
1185 if (retval == 0)
1186 set_bit(ASYNCB_NORMAL_ACTIVE, &ch->port.flags);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001187 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
1190static void moxa_close(struct tty_struct *tty, struct file *filp)
1191{
Alan Coxf1761782009-11-30 13:17:51 +00001192 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 ch->cflag = tty->termios->c_cflag;
Alan Coxf1761782009-11-30 13:17:51 +00001194 mutex_lock(&moxa_openlock);
1195 tty_port_close(&ch->port, tty, filp);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001196 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
1199static int moxa_write(struct tty_struct *tty,
1200 const unsigned char *buf, int count)
1201{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001202 struct moxa_port *ch = tty->driver_data;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001203 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 if (ch == NULL)
Jiri Slabyb4173f42008-04-30 00:53:40 -07001206 return 0;
Alan Cox33f0f882006-01-09 20:54:13 -08001207
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001208 spin_lock_bh(&moxa_lock);
Alan Coxd450b5a2008-10-13 10:39:58 +01001209 len = MoxaPortWriteData(tty, buf, count);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001210 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 ch->statusflags |= LOWWAIT;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001213 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
1216static int moxa_write_room(struct tty_struct *tty)
1217{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001218 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 if (tty->stopped)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001221 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001222 ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (ch == NULL)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001224 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001225 return MoxaPortTxFree(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
1228static void moxa_flush_buffer(struct tty_struct *tty)
1229{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001230 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 if (ch == NULL)
1233 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001234 MoxaPortFlushData(ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 tty_wakeup(tty);
1236}
1237
1238static int moxa_chars_in_buffer(struct tty_struct *tty)
1239{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001240 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Alan Cox978e5952008-04-30 00:53:59 -07001243 lock_kernel();
Jiri Slabyb4173f42008-04-30 00:53:40 -07001244 chars = MoxaPortTxQueue(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (chars) {
1246 /*
1247 * Make it possible to wakeup anything waiting for output
1248 * in tty_ioctl.c, etc.
1249 */
1250 if (!(ch->statusflags & EMPTYWAIT))
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001251 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
Alan Cox978e5952008-04-30 00:53:59 -07001253 unlock_kernel();
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001254 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1258{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001259 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 int flag = 0, dtr, rts;
1261
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001262 mutex_lock(&moxa_openlock);
1263 ch = tty->driver_data;
1264 if (!ch) {
1265 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -07001266 return -EINVAL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Jiri Slabyb4173f42008-04-30 00:53:40 -07001269 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (dtr)
1271 flag |= TIOCM_DTR;
1272 if (rts)
1273 flag |= TIOCM_RTS;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001274 dtr = MoxaPortLineStatus(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (dtr & 1)
1276 flag |= TIOCM_CTS;
1277 if (dtr & 2)
1278 flag |= TIOCM_DSR;
1279 if (dtr & 4)
1280 flag |= TIOCM_CD;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001281 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return flag;
1283}
1284
1285static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1286 unsigned int set, unsigned int clear)
1287{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001288 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 int port;
1290 int dtr, rts;
1291
Jiri Slaby11324ed2007-02-10 01:45:31 -08001292 port = tty->index;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001293 mutex_lock(&moxa_openlock);
1294 ch = tty->driver_data;
1295 if (!ch) {
1296 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -07001297 return -EINVAL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Jiri Slabyb4173f42008-04-30 00:53:40 -07001300 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (set & TIOCM_RTS)
1302 rts = 1;
1303 if (set & TIOCM_DTR)
1304 dtr = 1;
1305 if (clear & TIOCM_RTS)
1306 rts = 0;
1307 if (clear & TIOCM_DTR)
1308 dtr = 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001309 MoxaPortLineCtrl(ch, dtr, rts);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001310 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return 0;
1312}
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314static void moxa_throttle(struct tty_struct *tty)
1315{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001316 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 ch->statusflags |= THROTTLE;
1319}
1320
1321static void moxa_unthrottle(struct tty_struct *tty)
1322{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001323 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 ch->statusflags &= ~THROTTLE;
1326}
1327
1328static void moxa_set_termios(struct tty_struct *tty,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001329 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001331 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 if (ch == NULL)
1334 return;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001335 moxa_set_tty_param(tty, old_termios);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001336 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
Alan Cox9de6a512008-07-16 21:56:02 +01001337 wake_up_interruptible(&ch->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
1340static void moxa_stop(struct tty_struct *tty)
1341{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001342 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 if (ch == NULL)
1345 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001346 MoxaPortTxDisable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 ch->statusflags |= TXSTOPPED;
1348}
1349
1350
1351static void moxa_start(struct tty_struct *tty)
1352{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001353 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 if (ch == NULL)
1356 return;
1357
1358 if (!(ch->statusflags & TXSTOPPED))
1359 return;
1360
Jiri Slabyb4173f42008-04-30 00:53:40 -07001361 MoxaPortTxEnable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 ch->statusflags &= ~TXSTOPPED;
1363}
1364
1365static void moxa_hangup(struct tty_struct *tty)
1366{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001367 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001369 mutex_lock(&moxa_openlock);
1370 ch = tty->driver_data;
Alan Coxf1761782009-11-30 13:17:51 +00001371 tty_port_hangup(&ch->port);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001372 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001375static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1376{
Alan Coxd450b5a2008-10-13 10:39:58 +01001377 struct tty_struct *tty;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001378 dcd = !!dcd;
1379
Alan Coxd450b5a2008-10-13 10:39:58 +01001380 if (dcd != p->DCDState) {
1381 tty = tty_port_tty_get(&p->port);
1382 if (tty && C_CLOCAL(tty) && !dcd)
1383 tty_hangup(tty);
1384 tty_kref_put(tty);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001385 }
1386 p->DCDState = dcd;
1387}
1388
1389static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1390 u16 __iomem *ip)
1391{
Alan Coxd450b5a2008-10-13 10:39:58 +01001392 struct tty_struct *tty = tty_port_tty_get(&p->port);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001393 void __iomem *ofsAddr;
Alan Cox9de6a512008-07-16 21:56:02 +01001394 unsigned int inited = p->port.flags & ASYNC_INITIALIZED;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001395 u16 intr;
1396
1397 if (tty) {
1398 if ((p->statusflags & EMPTYWAIT) &&
1399 MoxaPortTxQueue(p) == 0) {
1400 p->statusflags &= ~EMPTYWAIT;
1401 tty_wakeup(tty);
1402 }
1403 if ((p->statusflags & LOWWAIT) && !tty->stopped &&
1404 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1405 p->statusflags &= ~LOWWAIT;
1406 tty_wakeup(tty);
1407 }
1408
1409 if (inited && !(p->statusflags & THROTTLE) &&
1410 MoxaPortRxQueue(p) > 0) { /* RX */
1411 MoxaPortReadData(p);
1412 tty_schedule_flip(tty);
1413 }
1414 } else {
1415 p->statusflags &= ~EMPTYWAIT;
1416 MoxaPortFlushData(p, 0); /* flush RX */
1417 }
1418
1419 if (!handle) /* nothing else to do */
Jiri Slaby0e0fd7d2009-04-06 17:34:04 +01001420 goto put;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001421
1422 intr = readw(ip); /* port irq status */
1423 if (intr == 0)
Jiri Slaby0e0fd7d2009-04-06 17:34:04 +01001424 goto put;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001425
1426 writew(0, ip); /* ACK port */
1427 ofsAddr = p->tableAddr;
1428 if (intr & IntrTx) /* disable tx intr */
1429 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1430 ofsAddr + HostStat);
1431
1432 if (!inited)
Jiri Slaby0e0fd7d2009-04-06 17:34:04 +01001433 goto put;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001434
1435 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1436 tty_insert_flip_char(tty, 0, TTY_BREAK);
1437 tty_schedule_flip(tty);
1438 }
1439
1440 if (intr & IntrLine)
1441 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
Jiri Slaby0e0fd7d2009-04-06 17:34:04 +01001442put:
1443 tty_kref_put(tty);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001444
1445 return 0;
1446}
1447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448static void moxa_poll(unsigned long ignored)
1449{
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001450 struct moxa_board_conf *brd;
1451 u16 __iomem *ip;
Jiri Slaby2a541342008-04-30 00:53:45 -07001452 unsigned int card, port, served = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001454 spin_lock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 for (card = 0; card < MAX_BOARDS; card++) {
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001456 brd = &moxa_boards[card];
1457 if (!brd->ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 continue;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001459
Jiri Slaby2a541342008-04-30 00:53:45 -07001460 served++;
1461
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001462 ip = NULL;
1463 if (readb(brd->intPend) == 0xff)
1464 ip = brd->intTable + readb(brd->intNdx);
1465
1466 for (port = 0; port < brd->numPorts; port++)
1467 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1468
1469 if (ip)
1470 writeb(0, brd->intPend); /* ACK */
1471
1472 if (moxaLowWaterChk) {
1473 struct moxa_port *p = brd->ports;
1474 for (port = 0; port < brd->numPorts; port++, p++)
1475 if (p->lowChkFlag) {
1476 p->lowChkFlag = 0;
1477 moxa_low_water_check(p->tableAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
1480 }
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001481 moxaLowWaterChk = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Jiri Slaby2a541342008-04-30 00:53:45 -07001483 if (served)
1484 mod_timer(&moxaTimer, jiffies + HZ / 50);
1485 spin_unlock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
1487
1488/******************************************************************************/
1489
Alan Coxdb1acaa2008-02-08 04:18:43 -08001490static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001492 register struct ktermios *ts = tty->termios;
1493 struct moxa_port *ch = tty->driver_data;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001494 int rts, cts, txflow, rxflow, xany, baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 rts = cts = txflow = rxflow = xany = 0;
1497 if (ts->c_cflag & CRTSCTS)
1498 rts = cts = 1;
1499 if (ts->c_iflag & IXON)
1500 txflow = 1;
1501 if (ts->c_iflag & IXOFF)
1502 rxflow = 1;
1503 if (ts->c_iflag & IXANY)
1504 xany = 1;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001505
1506 /* Clear the features we don't support */
1507 ts->c_cflag &= ~CMSPAR;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001508 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1509 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
Alan Coxdb1acaa2008-02-08 04:18:43 -08001510 if (baud == -1)
1511 baud = tty_termios_baud_rate(old_termios);
1512 /* Not put the baud rate into the termios data */
1513 tty_encode_baud_rate(tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514}
1515
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001516static void moxa_setup_empty_event(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001518 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001520 spin_lock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 ch->statusflags |= EMPTYWAIT;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001522 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525/*****************************************************************************
1526 * Driver level functions: *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Jiri Slabyb4173f42008-04-30 00:53:40 -07001529static void MoxaPortFlushData(struct moxa_port *port, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
1531 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001532 if (mode < 0 || mode > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001534 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 moxafunc(ofsAddr, FC_FlushQueue, mode);
1536 if (mode != 1) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001537 port->lowChkFlag = 0;
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001538 moxa_low_water_check(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
1540}
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542/*
1543 * Moxa Port Number Description:
1544 *
1545 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1546 * the port number using in MOXA driver functions will be 0 to 31 for
1547 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1548 * to 127 for fourth. For example, if you setup three MOXA boards,
1549 * first board is C218, second board is C320-16 and third board is
1550 * C320-32. The port number of first board (C218 - 8 ports) is from
1551 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1552 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1553 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1554 * 127 will be invalid.
1555 *
1556 *
1557 * Moxa Functions Description:
1558 *
1559 * Function 1: Driver initialization routine, this routine must be
1560 * called when initialized driver.
1561 * Syntax:
1562 * void MoxaDriverInit();
1563 *
1564 *
1565 * Function 2: Moxa driver private IOCTL command processing.
1566 * Syntax:
1567 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1568 *
1569 * unsigned int cmd : IOCTL command
1570 * unsigned long arg : IOCTL argument
1571 * int port : port number (0 - 127)
1572 *
1573 * return: 0 (OK)
1574 * -EINVAL
1575 * -ENOIOCTLCMD
1576 *
1577 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 * Function 6: Enable this port to start Tx/Rx data.
1579 * Syntax:
1580 * void MoxaPortEnable(int port);
1581 * int port : port number (0 - 127)
1582 *
1583 *
1584 * Function 7: Disable this port
1585 * Syntax:
1586 * void MoxaPortDisable(int port);
1587 * int port : port number (0 - 127)
1588 *
1589 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 * Function 10: Setting baud rate of this port.
1591 * Syntax:
Jiri Slaby08d01c792008-04-30 00:53:47 -07001592 * speed_t MoxaPortSetBaud(int port, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 * int port : port number (0 - 127)
1594 * long baud : baud rate (50 - 115200)
1595 *
1596 * return: 0 : this port is invalid or baud < 50
1597 * 50 - 115200 : the real baud rate set to the port, if
1598 * the argument baud is large than maximun
1599 * available baud rate, the real setting
1600 * baud rate will be the maximun baud rate.
1601 *
1602 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 * Function 12: Configure the port.
1604 * Syntax:
Alan Cox606d0992006-12-08 02:38:45 -08001605 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 * int port : port number (0 - 127)
Alan Cox606d0992006-12-08 02:38:45 -08001607 * struct ktermios * termio : termio structure pointer
Alan Coxc7bce302006-09-30 23:27:24 -07001608 * speed_t baud : baud rate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 *
1610 * return: -1 : this port is invalid or termio == NULL
1611 * 0 : setting O.K.
1612 *
1613 *
1614 * Function 13: Get the DTR/RTS state of this port.
1615 * Syntax:
1616 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1617 * int port : port number (0 - 127)
1618 * int * dtrState : pointer to INT to receive the current DTR
1619 * state. (if NULL, this function will not
1620 * write to this address)
1621 * int * rtsState : pointer to INT to receive the current RTS
1622 * state. (if NULL, this function will not
1623 * write to this address)
1624 *
1625 * return: -1 : this port is invalid
1626 * 0 : O.K.
1627 *
1628 *
1629 * Function 14: Setting the DTR/RTS output state of this port.
1630 * Syntax:
1631 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1632 * int port : port number (0 - 127)
1633 * int dtrState : DTR output state (0: off, 1: on)
1634 * int rtsState : RTS output state (0: off, 1: on)
1635 *
1636 *
1637 * Function 15: Setting the flow control of this port.
1638 * Syntax:
1639 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1640 * int txFlow,int xany);
1641 * int port : port number (0 - 127)
1642 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1643 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1644 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1645 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1646 * int xany : S/W XANY flow control (0: no, 1: yes)
1647 *
1648 *
1649 * Function 16: Get ths line status of this port
1650 * Syntax:
1651 * int MoxaPortLineStatus(int port);
1652 * int port : port number (0 - 127)
1653 *
1654 * return: Bit 0 - CTS state (0: off, 1: on)
1655 * Bit 1 - DSR state (0: off, 1: on)
1656 * Bit 2 - DCD state (0: off, 1: on)
1657 *
1658 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 * Function 19: Flush the Rx/Tx buffer data of this port.
1660 * Syntax:
1661 * void MoxaPortFlushData(int port, int mode);
1662 * int port : port number (0 - 127)
1663 * int mode
1664 * 0 : flush the Rx buffer
1665 * 1 : flush the Tx buffer
1666 * 2 : flush the Rx and Tx buffer
1667 *
1668 *
1669 * Function 20: Write data.
1670 * Syntax:
1671 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1672 * int port : port number (0 - 127)
1673 * unsigned char * buffer : pointer to write data buffer.
1674 * int length : write data length
1675 *
1676 * return: 0 - length : real write data length
1677 *
1678 *
1679 * Function 21: Read data.
1680 * Syntax:
Alan Cox33f0f882006-01-09 20:54:13 -08001681 * int MoxaPortReadData(int port, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 * int port : port number (0 - 127)
Alan Cox33f0f882006-01-09 20:54:13 -08001683 * struct tty_struct *tty : tty for data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 *
1685 * return: 0 - length : real read data length
1686 *
1687 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 * Function 24: Get the Tx buffer current queued data bytes
1689 * Syntax:
1690 * int MoxaPortTxQueue(int port);
1691 * int port : port number (0 - 127)
1692 *
1693 * return: .. : Tx buffer current queued data bytes
1694 *
1695 *
1696 * Function 25: Get the Tx buffer current free space
1697 * Syntax:
1698 * int MoxaPortTxFree(int port);
1699 * int port : port number (0 - 127)
1700 *
1701 * return: .. : Tx buffer current free space
1702 *
1703 *
1704 * Function 26: Get the Rx buffer current queued data bytes
1705 * Syntax:
1706 * int MoxaPortRxQueue(int port);
1707 * int port : port number (0 - 127)
1708 *
1709 * return: .. : Rx buffer current queued data bytes
1710 *
1711 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 * Function 28: Disable port data transmission.
1713 * Syntax:
1714 * void MoxaPortTxDisable(int port);
1715 * int port : port number (0 - 127)
1716 *
1717 *
1718 * Function 29: Enable port data transmission.
1719 * Syntax:
1720 * void MoxaPortTxEnable(int port);
1721 * int port : port number (0 - 127)
1722 *
1723 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 * Function 31: Get the received BREAK signal count and reset it.
1725 * Syntax:
1726 * int MoxaPortResetBrkCnt(int port);
1727 * int port : port number (0 - 127)
1728 *
1729 * return: 0 - .. : BREAK signal count
1730 *
1731 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Jiri Slabyb4173f42008-04-30 00:53:40 -07001734static void MoxaPortEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
1736 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001737 u16 lowwater = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Jiri Slabyb4173f42008-04-30 00:53:40 -07001739 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 writew(lowwater, ofsAddr + Low_water);
Jiri Slaby08d01c792008-04-30 00:53:47 -07001741 if (MOXA_IS_320(port->board))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001743 else
1744 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1745 ofsAddr + HostStat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1748 moxafunc(ofsAddr, FC_FlushQueue, 2);
1749
1750 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1751 MoxaPortLineStatus(port);
1752}
1753
Jiri Slabyb4173f42008-04-30 00:53:40 -07001754static void MoxaPortDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001756 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
1758 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1759 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1760 writew(0, ofsAddr + HostStat);
1761 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1762}
1763
Jiri Slaby08d01c792008-04-30 00:53:47 -07001764static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Jiri Slaby08d01c792008-04-30 00:53:47 -07001766 void __iomem *ofsAddr = port->tableAddr;
1767 unsigned int clock, val;
1768 speed_t max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Jiri Slaby08d01c792008-04-30 00:53:47 -07001770 max = MOXA_IS_320(port->board) ? 460800 : 921600;
1771 if (baud < 50)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001772 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 if (baud > max)
1774 baud = max;
Jiri Slaby08d01c792008-04-30 00:53:47 -07001775 clock = 921600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 val = clock / baud;
1777 moxafunc(ofsAddr, FC_SetBaud, val);
1778 baud = clock / val;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001779 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
Jiri Slabyb4173f42008-04-30 00:53:40 -07001782static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1783 speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
1785 void __iomem *ofsAddr;
1786 tcflag_t cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 tcflag_t mode = 0;
1788
Jiri Slabyb4173f42008-04-30 00:53:40 -07001789 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 cflag = termio->c_cflag; /* termio->c_cflag */
1791
1792 mode = termio->c_cflag & CSIZE;
1793 if (mode == CS5)
1794 mode = MX_CS5;
1795 else if (mode == CS6)
1796 mode = MX_CS6;
1797 else if (mode == CS7)
1798 mode = MX_CS7;
1799 else if (mode == CS8)
1800 mode = MX_CS8;
1801
1802 if (termio->c_cflag & CSTOPB) {
1803 if (mode == MX_CS5)
1804 mode |= MX_STOP15;
1805 else
1806 mode |= MX_STOP2;
1807 } else
1808 mode |= MX_STOP1;
1809
1810 if (termio->c_cflag & PARENB) {
1811 if (termio->c_cflag & PARODD)
1812 mode |= MX_PARODD;
1813 else
1814 mode |= MX_PAREVEN;
1815 } else
1816 mode |= MX_PARNONE;
1817
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001818 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Jiri Slaby08d01c792008-04-30 00:53:47 -07001820 if (MOXA_IS_320(port->board) && baud >= 921600)
1821 return -1;
1822
Alan Coxdb1acaa2008-02-08 04:18:43 -08001823 baud = MoxaPortSetBaud(port, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1826 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1827 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1828 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001829 moxa_wait_finish(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831 }
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001832 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
Jiri Slabyb4173f42008-04-30 00:53:40 -07001835static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1836 int *rtsState)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001838 if (dtrState)
1839 *dtrState = !!(port->lineCtrl & DTR_ON);
1840 if (rtsState)
1841 *rtsState = !!(port->lineCtrl & RTS_ON);
1842
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001843 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
1845
Jiri Slabyb4173f42008-04-30 00:53:40 -07001846static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001848 u8 mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 if (dtr)
1851 mode |= DTR_ON;
1852 if (rts)
1853 mode |= RTS_ON;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001854 port->lineCtrl = mode;
1855 moxafunc(port->tableAddr, FC_LineControl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856}
1857
Jiri Slabyb4173f42008-04-30 00:53:40 -07001858static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1859 int txflow, int rxflow, int txany)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001861 int mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (rts)
1864 mode |= RTS_FlowCtl;
1865 if (cts)
1866 mode |= CTS_FlowCtl;
1867 if (txflow)
1868 mode |= Tx_FlowCtl;
1869 if (rxflow)
1870 mode |= Rx_FlowCtl;
1871 if (txany)
1872 mode |= IXM_IXANY;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001873 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874}
1875
Jiri Slabyb4173f42008-04-30 00:53:40 -07001876static int MoxaPortLineStatus(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
1878 void __iomem *ofsAddr;
1879 int val;
1880
Jiri Slabyb4173f42008-04-30 00:53:40 -07001881 ofsAddr = port->tableAddr;
Jiri Slaby08d01c792008-04-30 00:53:47 -07001882 if (MOXA_IS_320(port->board)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 moxafunc(ofsAddr, FC_LineStatus, 0);
1884 val = readw(ofsAddr + FuncArg);
1885 } else {
1886 val = readw(ofsAddr + FlagStat) >> 4;
1887 }
1888 val &= 0x0B;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001889 if (val & 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 val |= 4;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001891 spin_lock_bh(&moxa_lock);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001892 moxa_new_dcdstate(port, val & 8);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001893 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 val &= 7;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001895 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896}
1897
Alan Coxd450b5a2008-10-13 10:39:58 +01001898static int MoxaPortWriteData(struct tty_struct *tty,
Jiri Slaby2108eba2008-04-30 00:53:44 -07001899 const unsigned char *buffer, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
Alan Coxd450b5a2008-10-13 10:39:58 +01001901 struct moxa_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001903 unsigned int c, total;
1904 u16 head, tail, tx_mask, spage, epage;
1905 u16 pageno, pageofs, bufhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Jiri Slabyb4173f42008-04-30 00:53:40 -07001907 ofsAddr = port->tableAddr;
1908 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 tx_mask = readw(ofsAddr + TX_mask);
1910 spage = readw(ofsAddr + Page_txb);
1911 epage = readw(ofsAddr + EndPage_txb);
1912 tail = readw(ofsAddr + TXwptr);
1913 head = readw(ofsAddr + TXrptr);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001914 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (c > len)
1916 c = len;
Alan Cox9de6a512008-07-16 21:56:02 +01001917 moxaLog.txcnt[port->port.tty->index] += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 total = c;
1919 if (spage == epage) {
1920 bufhead = readw(ofsAddr + Ofs_txb);
1921 writew(spage, baseAddr + Control_reg);
1922 while (c > 0) {
1923 if (head > tail)
1924 len = head - tail - 1;
1925 else
1926 len = tx_mask + 1 - tail;
1927 len = (c > len) ? len : c;
1928 ofs = baseAddr + DynPage_addr + bufhead + tail;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001929 memcpy_toio(ofs, buffer, len);
1930 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 tail = (tail + len) & tx_mask;
1932 c -= len;
1933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 pageno = spage + (tail >> 13);
1936 pageofs = tail & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001937 while (c > 0) {
1938 len = Page_size - pageofs;
1939 if (len > c)
1940 len = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 writeb(pageno, baseAddr + Control_reg);
1942 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001943 memcpy_toio(ofs, buffer, len);
1944 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (++pageno == epage)
1946 pageno = spage;
1947 pageofs = 0;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001948 c -= len;
1949 }
1950 tail = (tail + total) & tx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07001952 writew(tail, ofsAddr + TXwptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 writeb(1, ofsAddr + CD180TXirq); /* start to send */
Jiri Slaby2108eba2008-04-30 00:53:44 -07001954 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001957static int MoxaPortReadData(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
Alan Cox9de6a512008-07-16 21:56:02 +01001959 struct tty_struct *tty = port->port.tty;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001960 unsigned char *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001962 unsigned int count, len, total;
1963 u16 tail, rx_mask, spage, epage;
1964 u16 pageno, pageofs, bufhead, head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Jiri Slabyb4173f42008-04-30 00:53:40 -07001966 ofsAddr = port->tableAddr;
1967 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 head = readw(ofsAddr + RXrptr);
1969 tail = readw(ofsAddr + RXwptr);
1970 rx_mask = readw(ofsAddr + RX_mask);
1971 spage = readw(ofsAddr + Page_rxb);
1972 epage = readw(ofsAddr + EndPage_rxb);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001973 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (count == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08001975 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Alan Cox33f0f882006-01-09 20:54:13 -08001977 total = count;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001978 moxaLog.rxcnt[tty->index] += total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 if (spage == epage) {
1980 bufhead = readw(ofsAddr + Ofs_rxb);
1981 writew(spage, baseAddr + Control_reg);
1982 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 ofs = baseAddr + DynPage_addr + bufhead + head;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001984 len = (tail >= head) ? (tail - head) :
1985 (rx_mask + 1 - head);
1986 len = tty_prepare_flip_string(tty, &dst,
1987 min(len, count));
1988 memcpy_fromio(dst, ofs, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 head = (head + len) & rx_mask;
1990 count -= len;
1991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 pageno = spage + (head >> 13);
1994 pageofs = head & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001995 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 writew(pageno, baseAddr + Control_reg);
1997 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001998 len = tty_prepare_flip_string(tty, &dst,
1999 min(Page_size - pageofs, count));
2000 memcpy_fromio(dst, ofs, len);
2001
2002 count -= len;
2003 pageofs = (pageofs + len) & Page_mask;
2004 if (pageofs == 0 && ++pageno == epage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 pageno = spage;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002006 }
2007 head = (head + total) & rx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002009 writew(head, ofsAddr + RXrptr);
2010 if (readb(ofsAddr + FlagStat) & Xoff_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 moxaLowWaterChk = 1;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002012 port->lowChkFlag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002014 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015}
2016
2017
Jiri Slabyb4173f42008-04-30 00:53:40 -07002018static int MoxaPortTxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002020 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002021 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 rptr = readw(ofsAddr + TXrptr);
2024 wptr = readw(ofsAddr + TXwptr);
2025 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002026 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027}
2028
Jiri Slabyb4173f42008-04-30 00:53:40 -07002029static int MoxaPortTxFree(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002031 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002032 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 rptr = readw(ofsAddr + TXrptr);
2035 wptr = readw(ofsAddr + TXwptr);
2036 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002037 return mask - ((wptr - rptr) & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
2039
Jiri Slabyb4173f42008-04-30 00:53:40 -07002040static int MoxaPortRxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002042 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002043 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 rptr = readw(ofsAddr + RXrptr);
2046 wptr = readw(ofsAddr + RXwptr);
2047 mask = readw(ofsAddr + RX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002048 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049}
2050
Jiri Slabyb4173f42008-04-30 00:53:40 -07002051static void MoxaPortTxDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002053 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054}
2055
Jiri Slabyb4173f42008-04-30 00:53:40 -07002056static void MoxaPortTxEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002058 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
2060
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002061static int moxa_get_serial_info(struct moxa_port *info,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002062 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002064 struct serial_struct tmp = {
2065 .type = info->type,
Alan Cox9de6a512008-07-16 21:56:02 +01002066 .line = info->port.tty->index,
2067 .flags = info->port.flags,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002068 .baud_base = 921600,
Alan Cox44b7d1b2008-07-16 21:57:18 +01002069 .close_delay = info->port.close_delay
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002070 };
2071 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
2074
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002075static int moxa_set_serial_info(struct moxa_port *info,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002076 struct serial_struct __user *new_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
2078 struct serial_struct new_serial;
2079
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002080 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 return -EFAULT;
2082
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002083 if (new_serial.irq != 0 || new_serial.port != 0 ||
2084 new_serial.custom_divisor != 0 ||
2085 new_serial.baud_base != 921600)
2086 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 if (!capable(CAP_SYS_ADMIN)) {
2089 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
Alan Cox9de6a512008-07-16 21:56:02 +01002090 (info->port.flags & ~ASYNC_USR_MASK)))
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002091 return -EPERM;
2092 } else
Alan Cox44b7d1b2008-07-16 21:57:18 +01002093 info->port.close_delay = new_serial.close_delay * HZ / 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
Alan Cox9de6a512008-07-16 21:56:02 +01002096 new_serial.flags |= (info->port.flags & ASYNC_FLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002098 MoxaSetFifo(info, new_serial.type == PORT_16550A);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
2100 info->type = new_serial.type;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102}
2103
2104
2105
2106/*****************************************************************************
2107 * Static local functions: *
2108 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
Jiri Slabyb4173f42008-04-30 00:53:40 -07002110static void MoxaSetFifo(struct moxa_port *port, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002112 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
2114 if (!enable) {
2115 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2116 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2117 } else {
2118 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2119 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2120 }
2121}