blob: 474d936d3b31935f44fca52262e93a1e3db3f32f [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 Slaby08d01c72008-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{
Alan Coxf5c5a362009-11-30 13:17:57 +0000251 unsigned long flags;
252 spin_lock_irqsave(&moxafunc_lock, flags);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700253 writew(arg, ofsAddr + FuncArg);
254 writew(cmd, ofsAddr + FuncCode);
255 moxa_wait_finish(ofsAddr);
Alan Coxf5c5a362009-11-30 13:17:57 +0000256 spin_unlock_irqrestore(&moxafunc_lock, flags);
257}
258
259static int moxafuncret(void __iomem *ofsAddr, u16 cmd, u16 arg)
260{
261 unsigned long flags;
262 u16 ret;
263 spin_lock_irqsave(&moxafunc_lock, flags);
264 writew(arg, ofsAddr + FuncArg);
265 writew(cmd, ofsAddr + FuncCode);
266 moxa_wait_finish(ofsAddr);
267 ret = readw(ofsAddr + FuncArg);
268 spin_unlock_irqrestore(&moxafunc_lock, flags);
269 return ret;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700270}
271
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700272static void moxa_low_water_check(void __iomem *ofsAddr)
273{
274 u16 rptr, wptr, mask, len;
275
276 if (readb(ofsAddr + FlagStat) & Xoff_state) {
277 rptr = readw(ofsAddr + RXrptr);
278 wptr = readw(ofsAddr + RXwptr);
279 mask = readw(ofsAddr + RX_mask);
280 len = (wptr - rptr) & mask;
281 if (len <= Low_water)
282 moxafunc(ofsAddr, FC_SendXon, 0);
283 }
284}
285
Jiri Slaby74d7d972008-04-30 00:53:43 -0700286/*
287 * TTY operations
288 */
289
290static int moxa_ioctl(struct tty_struct *tty, struct file *file,
291 unsigned int cmd, unsigned long arg)
292{
293 struct moxa_port *ch = tty->driver_data;
294 void __user *argp = (void __user *)arg;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700295 int status, ret = 0;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700296
297 if (tty->index == MAX_PORTS) {
298 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
299 cmd != MOXA_GETMSTATUS)
300 return -EINVAL;
301 } else if (!ch)
302 return -ENODEV;
303
304 switch (cmd) {
305 case MOXA_GETDATACOUNT:
306 moxaLog.tick = jiffies;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700307 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
308 ret = -EFAULT;
309 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700310 case MOXA_FLUSH_QUEUE:
311 MoxaPortFlushData(ch, arg);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700312 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700313 case MOXA_GET_IOQUEUE: {
314 struct moxaq_str __user *argm = argp;
315 struct moxaq_str tmp;
316 struct moxa_port *p;
317 unsigned int i, j;
318
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700319 mutex_lock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700320 for (i = 0; i < MAX_BOARDS; i++) {
321 p = moxa_boards[i].ports;
322 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
323 memset(&tmp, 0, sizeof(tmp));
324 if (moxa_boards[i].ready) {
325 tmp.inq = MoxaPortRxQueue(p);
326 tmp.outq = MoxaPortTxQueue(p);
327 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700328 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
329 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700330 return -EFAULT;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700331 }
Jiri Slaby74d7d972008-04-30 00:53:43 -0700332 }
333 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700334 mutex_unlock(&moxa_openlock);
335 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700336 } case MOXA_GET_OQUEUE:
337 status = MoxaPortTxQueue(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700338 ret = put_user(status, (unsigned long __user *)argp);
339 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700340 case MOXA_GET_IQUEUE:
341 status = MoxaPortRxQueue(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700342 ret = put_user(status, (unsigned long __user *)argp);
343 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700344 case MOXA_GETMSTATUS: {
345 struct mxser_mstatus __user *argm = argp;
346 struct mxser_mstatus tmp;
347 struct moxa_port *p;
348 unsigned int i, j;
349
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700350 mutex_lock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700351 for (i = 0; i < MAX_BOARDS; i++) {
352 p = moxa_boards[i].ports;
353 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
Alan Coxd450b5a2008-10-13 10:39:58 +0100354 struct tty_struct *ttyp;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700355 memset(&tmp, 0, sizeof(tmp));
356 if (!moxa_boards[i].ready)
357 goto copy;
358
359 status = MoxaPortLineStatus(p);
360 if (status & 1)
361 tmp.cts = 1;
362 if (status & 2)
363 tmp.dsr = 1;
364 if (status & 4)
365 tmp.dcd = 1;
366
Alan Coxd450b5a2008-10-13 10:39:58 +0100367 ttyp = tty_port_tty_get(&p->port);
368 if (!ttyp || !ttyp->termios)
Jiri Slaby74d7d972008-04-30 00:53:43 -0700369 tmp.cflag = p->cflag;
370 else
Alan Coxd450b5a2008-10-13 10:39:58 +0100371 tmp.cflag = ttyp->termios->c_cflag;
372 tty_kref_put(tty);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700373copy:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700374 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
375 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700376 return -EFAULT;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700377 }
Jiri Slaby74d7d972008-04-30 00:53:43 -0700378 }
379 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700380 mutex_unlock(&moxa_openlock);
381 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700382 }
383 case TIOCGSERIAL:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700384 mutex_lock(&moxa_openlock);
385 ret = moxa_get_serial_info(ch, argp);
386 mutex_unlock(&moxa_openlock);
387 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700388 case TIOCSSERIAL:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700389 mutex_lock(&moxa_openlock);
390 ret = moxa_set_serial_info(ch, argp);
391 mutex_unlock(&moxa_openlock);
392 break;
393 default:
394 ret = -ENOIOCTLCMD;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700395 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700396 return ret;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700397}
398
Alan Cox9e98966c2008-07-22 11:18:03 +0100399static int moxa_break_ctl(struct tty_struct *tty, int state)
Jiri Slaby74d7d972008-04-30 00:53:43 -0700400{
401 struct moxa_port *port = tty->driver_data;
402
403 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
404 Magic_code);
Alan Cox9e98966c2008-07-22 11:18:03 +0100405 return 0;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700406}
407
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700408static const struct tty_operations moxa_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 .open = moxa_open,
410 .close = moxa_close,
411 .write = moxa_write,
412 .write_room = moxa_write_room,
413 .flush_buffer = moxa_flush_buffer,
414 .chars_in_buffer = moxa_chars_in_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 .ioctl = moxa_ioctl,
416 .throttle = moxa_throttle,
417 .unthrottle = moxa_unthrottle,
418 .set_termios = moxa_set_termios,
419 .stop = moxa_stop,
420 .start = moxa_start,
421 .hangup = moxa_hangup,
Jiri Slaby74d7d972008-04-30 00:53:43 -0700422 .break_ctl = moxa_break_ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 .tiocmget = moxa_tiocmget,
424 .tiocmset = moxa_tiocmset,
425};
426
Alan Cox31f35932009-01-02 13:45:05 +0000427static const struct tty_port_operations moxa_port_ops = {
428 .carrier_raised = moxa_carrier_raised,
Alan Coxf1761782009-11-30 13:17:51 +0000429 .dtr_rts = moxa_dtr_rts,
430 .shutdown = moxa_shutdown,
Alan Cox31f35932009-01-02 13:45:05 +0000431};
432
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800433static struct tty_driver *moxaDriver;
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800434static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
Ingo Molnar34af9462006-06-27 02:53:55 -0700435static DEFINE_SPINLOCK(moxa_lock);
Alan Coxf5c5a362009-11-30 13:17:57 +0000436static DEFINE_SPINLOCK(moxafunc_lock);
Alan Cox33f0f882006-01-09 20:54:13 -0800437
Jiri Slaby74d7d972008-04-30 00:53:43 -0700438/*
439 * HW init
440 */
441
Jiri Slaby03718232008-04-30 00:53:39 -0700442static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
443{
444 switch (brd->boardType) {
445 case MOXA_BOARD_C218_ISA:
446 case MOXA_BOARD_C218_PCI:
447 if (model != 1)
448 goto err;
449 break;
450 case MOXA_BOARD_CP204J:
451 if (model != 3)
452 goto err;
453 break;
454 default:
455 if (model != 2)
456 goto err;
457 break;
458 }
459 return 0;
460err:
461 return -EINVAL;
462}
463
464static int moxa_check_fw(const void *ptr)
465{
466 const __le16 *lptr = ptr;
467
468 if (*lptr != cpu_to_le16(0x7980))
469 return -EINVAL;
470
471 return 0;
472}
473
474static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
475 size_t len)
476{
477 void __iomem *baseAddr = brd->basemem;
478 u16 tmp;
479
480 writeb(HW_reset, baseAddr + Control_reg); /* reset */
481 msleep(10);
482 memset_io(baseAddr, 0, 4096);
483 memcpy_toio(baseAddr, buf, len); /* download BIOS */
484 writeb(0, baseAddr + Control_reg); /* restart */
485
486 msleep(2000);
487
488 switch (brd->boardType) {
489 case MOXA_BOARD_C218_ISA:
490 case MOXA_BOARD_C218_PCI:
491 tmp = readw(baseAddr + C218_key);
492 if (tmp != C218_KeyCode)
493 goto err;
494 break;
495 case MOXA_BOARD_CP204J:
496 tmp = readw(baseAddr + C218_key);
497 if (tmp != CP204J_KeyCode)
498 goto err;
499 break;
500 default:
501 tmp = readw(baseAddr + C320_key);
502 if (tmp != C320_KeyCode)
503 goto err;
504 tmp = readw(baseAddr + C320_status);
505 if (tmp != STS_init) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700506 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
Jiri Slaby03718232008-04-30 00:53:39 -0700507 "module not found\n");
508 return -EIO;
509 }
510 break;
511 }
512
513 return 0;
514err:
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700515 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700516 return -EIO;
517}
518
519static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
520 size_t len)
521{
522 void __iomem *baseAddr = brd->basemem;
523
524 if (len < 7168) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700525 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700526 return -EINVAL;
527 }
528
529 writew(len - 7168 - 2, baseAddr + C320bapi_len);
530 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
531 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
532 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
533 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
534
535 return 0;
536}
537
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700538static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
Jiri Slaby03718232008-04-30 00:53:39 -0700539 size_t len)
540{
541 void __iomem *baseAddr = brd->basemem;
Harvey Harrisonb46f69c2008-10-15 22:04:19 -0700542 const __le16 *uptr = ptr;
Jiri Slaby03718232008-04-30 00:53:39 -0700543 size_t wlen, len2, j;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700544 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
Jiri Slaby08d01c72008-04-30 00:53:47 -0700545 unsigned int i, retry;
Jiri Slaby03718232008-04-30 00:53:39 -0700546 u16 usum, keycode;
547
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700548 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
549 C218_KeyCode;
550
551 switch (brd->boardType) {
552 case MOXA_BOARD_CP204J:
553 case MOXA_BOARD_C218_ISA:
554 case MOXA_BOARD_C218_PCI:
555 key = C218_key;
556 loadbuf = C218_LoadBuf;
557 loadlen = C218DLoad_len;
558 checksum = C218check_sum;
559 checksum_ok = C218chksum_ok;
560 break;
561 default:
562 key = C320_key;
563 keycode = C320_KeyCode;
564 loadbuf = C320_LoadBuf;
565 loadlen = C320DLoad_len;
566 checksum = C320check_sum;
567 checksum_ok = C320chksum_ok;
568 break;
569 }
570
Jiri Slaby03718232008-04-30 00:53:39 -0700571 usum = 0;
572 wlen = len >> 1;
573 for (i = 0; i < wlen; i++)
574 usum += le16_to_cpu(uptr[i]);
575 retry = 0;
576 do {
577 wlen = len >> 1;
578 j = 0;
579 while (wlen) {
580 len2 = (wlen > 2048) ? 2048 : wlen;
581 wlen -= len2;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700582 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
Jiri Slaby03718232008-04-30 00:53:39 -0700583 j += len2 << 1;
584
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700585 writew(len2, baseAddr + loadlen);
586 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700587 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700588 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700589 break;
590 msleep(10);
591 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700592 if (readw(baseAddr + key) != keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700593 return -EIO;
594 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700595 writew(0, baseAddr + loadlen);
596 writew(usum, baseAddr + checksum);
597 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700598 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700599 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700600 break;
601 msleep(10);
602 }
603 retry++;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700604 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
605 if (readb(baseAddr + checksum_ok) != 1)
Jiri Slaby03718232008-04-30 00:53:39 -0700606 return -EIO;
607
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700608 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700609 for (i = 0; i < 600; i++) {
610 if (readw(baseAddr + Magic_no) == Magic_code)
611 break;
612 msleep(10);
613 }
614 if (readw(baseAddr + Magic_no) != Magic_code)
615 return -EIO;
616
Jiri Slaby08d01c72008-04-30 00:53:47 -0700617 if (MOXA_IS_320(brd)) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700618 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
619 writew(0x3800, baseAddr + TMS320_PORT1);
620 writew(0x3900, baseAddr + TMS320_PORT2);
621 writew(28499, baseAddr + TMS320_CLOCK);
622 } else {
623 writew(0x3200, baseAddr + TMS320_PORT1);
624 writew(0x3400, baseAddr + TMS320_PORT2);
625 writew(19999, baseAddr + TMS320_CLOCK);
626 }
Jiri Slaby03718232008-04-30 00:53:39 -0700627 }
628 writew(1, baseAddr + Disable_IRQ);
629 writew(0, baseAddr + Magic_no);
630 for (i = 0; i < 500; i++) {
631 if (readw(baseAddr + Magic_no) == Magic_code)
632 break;
633 msleep(10);
634 }
635 if (readw(baseAddr + Magic_no) != Magic_code)
636 return -EIO;
637
Jiri Slaby08d01c72008-04-30 00:53:47 -0700638 if (MOXA_IS_320(brd)) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700639 j = readw(baseAddr + Module_cnt);
640 if (j <= 0)
641 return -EIO;
642 brd->numPorts = j * 8;
643 writew(j, baseAddr + Module_no);
644 writew(0, baseAddr + Magic_no);
645 for (i = 0; i < 600; i++) {
646 if (readw(baseAddr + Magic_no) == Magic_code)
647 break;
648 msleep(10);
649 }
650 if (readw(baseAddr + Magic_no) != Magic_code)
651 return -EIO;
Jiri Slaby03718232008-04-30 00:53:39 -0700652 }
Jiri Slaby03718232008-04-30 00:53:39 -0700653 brd->intNdx = baseAddr + IRQindex;
654 brd->intPend = baseAddr + IRQpending;
655 brd->intTable = baseAddr + IRQtable;
656
657 return 0;
658}
659
660static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
661 size_t len)
662{
663 void __iomem *ofsAddr, *baseAddr = brd->basemem;
664 struct moxa_port *port;
665 int retval, i;
666
667 if (len % 2) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700668 printk(KERN_ERR "MOXA: bios length is not even\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700669 return -EINVAL;
670 }
671
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700672 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
673 if (retval)
674 return retval;
675
Jiri Slaby03718232008-04-30 00:53:39 -0700676 switch (brd->boardType) {
677 case MOXA_BOARD_C218_ISA:
678 case MOXA_BOARD_C218_PCI:
679 case MOXA_BOARD_CP204J:
Jiri Slaby03718232008-04-30 00:53:39 -0700680 port = brd->ports;
681 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700682 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700683 port->DCDState = 0;
684 port->tableAddr = baseAddr + Extern_table +
685 Extern_size * i;
686 ofsAddr = port->tableAddr;
687 writew(C218rx_mask, ofsAddr + RX_mask);
688 writew(C218tx_mask, ofsAddr + TX_mask);
689 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
690 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
691
692 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
693 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
694
695 }
696 break;
697 default:
Jiri Slaby03718232008-04-30 00:53:39 -0700698 port = brd->ports;
699 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700700 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700701 port->DCDState = 0;
702 port->tableAddr = baseAddr + Extern_table +
703 Extern_size * i;
704 ofsAddr = port->tableAddr;
705 switch (brd->numPorts) {
706 case 8:
707 writew(C320p8rx_mask, ofsAddr + RX_mask);
708 writew(C320p8tx_mask, ofsAddr + TX_mask);
709 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
710 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
711 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
712 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
713
714 break;
715 case 16:
716 writew(C320p16rx_mask, ofsAddr + RX_mask);
717 writew(C320p16tx_mask, ofsAddr + TX_mask);
718 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
719 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
720 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
721 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
722 break;
723
724 case 24:
725 writew(C320p24rx_mask, ofsAddr + RX_mask);
726 writew(C320p24tx_mask, ofsAddr + TX_mask);
727 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
728 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
729 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
730 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
731 break;
732 case 32:
733 writew(C320p32rx_mask, ofsAddr + RX_mask);
734 writew(C320p32tx_mask, ofsAddr + TX_mask);
735 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
736 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
737 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
738 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
739 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
740 break;
741 }
742 }
743 break;
744 }
Jiri Slaby03718232008-04-30 00:53:39 -0700745 return 0;
746}
747
748static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
749{
David Howells2bca76e2008-07-08 17:37:15 +0100750 const void *ptr = fw->data;
Jiri Slaby03718232008-04-30 00:53:39 -0700751 char rsn[64];
752 u16 lens[5];
753 size_t len;
754 unsigned int a, lenp, lencnt;
755 int ret = -EINVAL;
756 struct {
757 __le32 magic; /* 0x34303430 */
758 u8 reserved1[2];
759 u8 type; /* UNIX = 3 */
760 u8 model; /* C218T=1, C320T=2, CP204=3 */
761 u8 reserved2[8];
762 __le16 len[5];
David Howells2bca76e2008-07-08 17:37:15 +0100763 } const *hdr = ptr;
Jiri Slaby03718232008-04-30 00:53:39 -0700764
765 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
766
767 if (fw->size < MOXA_FW_HDRLEN) {
768 strcpy(rsn, "too short (even header won't fit)");
769 goto err;
770 }
771 if (hdr->magic != cpu_to_le32(0x30343034)) {
772 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
773 goto err;
774 }
775 if (hdr->type != 3) {
776 sprintf(rsn, "not for linux, type is %u", hdr->type);
777 goto err;
778 }
779 if (moxa_check_fw_model(brd, hdr->model)) {
780 sprintf(rsn, "not for this card, model is %u", hdr->model);
781 goto err;
782 }
783
784 len = MOXA_FW_HDRLEN;
785 lencnt = hdr->model == 2 ? 5 : 3;
786 for (a = 0; a < ARRAY_SIZE(lens); a++) {
787 lens[a] = le16_to_cpu(hdr->len[a]);
788 if (lens[a] && len + lens[a] <= fw->size &&
789 moxa_check_fw(&fw->data[len]))
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700790 printk(KERN_WARNING "MOXA firmware: unexpected input "
Jiri Slaby03718232008-04-30 00:53:39 -0700791 "at offset %u, but going on\n", (u32)len);
792 if (!lens[a] && a < lencnt) {
793 sprintf(rsn, "too few entries in fw file");
794 goto err;
795 }
796 len += lens[a];
797 }
798
799 if (len != fw->size) {
800 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
801 (u32)len);
802 goto err;
803 }
804
805 ptr += MOXA_FW_HDRLEN;
806 lenp = 0; /* bios */
807
808 strcpy(rsn, "read above");
809
810 ret = moxa_load_bios(brd, ptr, lens[lenp]);
811 if (ret)
812 goto err;
813
814 /* we skip the tty section (lens[1]), since we don't need it */
815 ptr += lens[lenp] + lens[lenp + 1];
816 lenp += 2; /* comm */
817
818 if (hdr->model == 2) {
819 ret = moxa_load_320b(brd, ptr, lens[lenp]);
820 if (ret)
821 goto err;
822 /* skip another tty */
823 ptr += lens[lenp] + lens[lenp + 1];
824 lenp += 2;
825 }
826
827 ret = moxa_load_code(brd, ptr, lens[lenp]);
828 if (ret)
829 goto err;
830
831 return 0;
832err:
833 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
834 return ret;
835}
836
837static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
838{
839 const struct firmware *fw;
840 const char *file;
Jiri Slaby810ab092008-04-30 00:53:41 -0700841 struct moxa_port *p;
842 unsigned int i;
Jiri Slaby03718232008-04-30 00:53:39 -0700843 int ret;
844
Jiri Slaby810ab092008-04-30 00:53:41 -0700845 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
846 GFP_KERNEL);
847 if (brd->ports == NULL) {
848 printk(KERN_ERR "cannot allocate memory for ports\n");
849 ret = -ENOMEM;
850 goto err;
851 }
852
853 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
Alan Cox9de6a512008-07-16 21:56:02 +0100854 tty_port_init(&p->port);
Alan Cox31f35932009-01-02 13:45:05 +0000855 p->port.ops = &moxa_port_ops;
Alan Cox44b7d1b2008-07-16 21:57:18 +0100856 p->type = PORT_16550A;
857 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Jiri Slaby810ab092008-04-30 00:53:41 -0700858 }
859
Jiri Slaby03718232008-04-30 00:53:39 -0700860 switch (brd->boardType) {
861 case MOXA_BOARD_C218_ISA:
862 case MOXA_BOARD_C218_PCI:
863 file = "c218tunx.cod";
864 break;
865 case MOXA_BOARD_CP204J:
866 file = "cp204unx.cod";
867 break;
868 default:
869 file = "c320tunx.cod";
870 break;
871 }
872
873 ret = request_firmware(&fw, file, dev);
874 if (ret) {
Jiri Slabyec09cd52008-04-30 00:53:49 -0700875 printk(KERN_ERR "MOXA: request_firmware failed. Make sure "
876 "you've placed '%s' file into your firmware "
877 "loader directory (e.g. /lib/firmware)\n",
878 file);
Jiri Slaby810ab092008-04-30 00:53:41 -0700879 goto err_free;
Jiri Slaby03718232008-04-30 00:53:39 -0700880 }
881
882 ret = moxa_load_fw(brd, fw);
883
884 release_firmware(fw);
Jiri Slaby810ab092008-04-30 00:53:41 -0700885
886 if (ret)
887 goto err_free;
888
Jiri Slaby2a541342008-04-30 00:53:45 -0700889 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700890 brd->ready = 1;
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700891 if (!timer_pending(&moxaTimer))
892 mod_timer(&moxaTimer, jiffies + HZ / 50);
Jiri Slaby2a541342008-04-30 00:53:45 -0700893 spin_unlock_bh(&moxa_lock);
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700894
Jiri Slaby810ab092008-04-30 00:53:41 -0700895 return 0;
896err_free:
897 kfree(brd->ports);
898err:
Jiri Slaby03718232008-04-30 00:53:39 -0700899 return ret;
900}
901
Jiri Slaby810ab092008-04-30 00:53:41 -0700902static void moxa_board_deinit(struct moxa_board_conf *brd)
903{
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700904 unsigned int a, opened;
905
906 mutex_lock(&moxa_openlock);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700907 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700908 brd->ready = 0;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700909 spin_unlock_bh(&moxa_lock);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700910
911 /* pci hot-un-plug support */
912 for (a = 0; a < brd->numPorts; a++)
Alan Coxd450b5a2008-10-13 10:39:58 +0100913 if (brd->ports[a].port.flags & ASYNC_INITIALIZED) {
914 struct tty_struct *tty = tty_port_tty_get(
915 &brd->ports[a].port);
916 if (tty) {
917 tty_hangup(tty);
918 tty_kref_put(tty);
919 }
920 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700921 while (1) {
922 opened = 0;
923 for (a = 0; a < brd->numPorts; a++)
Alan Cox9de6a512008-07-16 21:56:02 +0100924 if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700925 opened++;
926 mutex_unlock(&moxa_openlock);
927 if (!opened)
928 break;
929 msleep(50);
930 mutex_lock(&moxa_openlock);
931 }
932
Jiri Slaby810ab092008-04-30 00:53:41 -0700933 iounmap(brd->basemem);
934 brd->basemem = NULL;
935 kfree(brd->ports);
936}
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938#ifdef CONFIG_PCI
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800939static int __devinit moxa_pci_probe(struct pci_dev *pdev,
940 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800942 struct moxa_board_conf *board;
943 unsigned int i;
944 int board_type = ent->driver_data;
945 int retval;
946
947 retval = pci_enable_device(pdev);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700948 if (retval) {
949 dev_err(&pdev->dev, "can't enable pci device\n");
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800950 goto err;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700951 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800952
953 for (i = 0; i < MAX_BOARDS; i++)
954 if (moxa_boards[i].basemem == NULL)
955 break;
956
957 retval = -ENODEV;
958 if (i >= MAX_BOARDS) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700959 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800960 "found. Board is ignored.\n", MAX_BOARDS);
961 goto err;
962 }
963
964 board = &moxa_boards[i];
Jiri Slabye46a5e32008-04-30 00:53:37 -0700965
966 retval = pci_request_region(pdev, 2, "moxa-base");
967 if (retval) {
968 dev_err(&pdev->dev, "can't request pci region 2\n");
969 goto err;
970 }
971
Alan Cox24cb2332008-04-30 00:54:19 -0700972 board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700973 if (board->basemem == NULL) {
974 dev_err(&pdev->dev, "can't remap io space 2\n");
Jiri Slabye46a5e32008-04-30 00:53:37 -0700975 goto err_reg;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700976 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 board->boardType = board_type;
979 switch (board_type) {
980 case MOXA_BOARD_C218_ISA:
981 case MOXA_BOARD_C218_PCI:
982 board->numPorts = 8;
983 break;
984
985 case MOXA_BOARD_CP204J:
986 board->numPorts = 4;
987 break;
988 default:
989 board->numPorts = 0;
990 break;
991 }
992 board->busType = MOXA_BUS_TYPE_PCI;
Jiri Slabya784bf72007-02-10 01:45:36 -0800993
Jiri Slaby03718232008-04-30 00:53:39 -0700994 retval = moxa_init_board(board, &pdev->dev);
995 if (retval)
996 goto err_base;
997
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800998 pci_set_drvdata(pdev, board);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Jiri Slabybb9f9102008-04-30 00:53:48 -07001000 dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n",
1001 moxa_brdname[board_type - 1], board->numPorts);
1002
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001003 return 0;
Jiri Slaby03718232008-04-30 00:53:39 -07001004err_base:
1005 iounmap(board->basemem);
1006 board->basemem = NULL;
Jiri Slabye46a5e32008-04-30 00:53:37 -07001007err_reg:
1008 pci_release_region(pdev, 2);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001009err:
1010 return retval;
1011}
1012
1013static void __devexit moxa_pci_remove(struct pci_dev *pdev)
1014{
1015 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
1016
Jiri Slaby810ab092008-04-30 00:53:41 -07001017 moxa_board_deinit(brd);
1018
Jiri Slabye46a5e32008-04-30 00:53:37 -07001019 pci_release_region(pdev, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
Jiri Slabya784bf72007-02-10 01:45:36 -08001021
1022static struct pci_driver moxa_pci_driver = {
1023 .name = "moxa",
1024 .id_table = moxa_pcibrds,
1025 .probe = moxa_pci_probe,
1026 .remove = __devexit_p(moxa_pci_remove)
1027};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028#endif /* CONFIG_PCI */
1029
1030static int __init moxa_init(void)
1031{
Jiri Slaby810ab092008-04-30 00:53:41 -07001032 unsigned int isabrds = 0;
Jiri Slabyd353eca2008-04-30 00:53:37 -07001033 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001035 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
1036 MOXA_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
1038 if (!moxaDriver)
1039 return -ENOMEM;
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 moxaDriver->owner = THIS_MODULE;
Sergey Vlasov9b4e3b12005-09-03 16:26:49 +01001042 moxaDriver->name = "ttyMX";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 moxaDriver->major = ttymajor;
1044 moxaDriver->minor_start = 0;
1045 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1046 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1047 moxaDriver->init_termios = tty_std_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Alan Cox606d0992006-12-08 02:38:45 -08001049 moxaDriver->init_termios.c_ispeed = 9600;
1050 moxaDriver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
1052 tty_set_operations(moxaDriver, &moxa_ops);
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (tty_register_driver(moxaDriver)) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001055 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 put_tty_driver(moxaDriver);
1057 return -1;
1058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Jiri Slabyd353eca2008-04-30 00:53:37 -07001060 /* Find the boards defined from module args. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -07001062 {
1063 struct moxa_board_conf *brd = moxa_boards;
Jiri Slaby810ab092008-04-30 00:53:41 -07001064 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 for (i = 0; i < MAX_BOARDS; i++) {
Jiri Slabyd353eca2008-04-30 00:53:37 -07001066 if (!baseaddr[i])
1067 break;
1068 if (type[i] == MOXA_BOARD_C218_ISA ||
1069 type[i] == MOXA_BOARD_C320_ISA) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001070 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001071 isabrds + 1, moxa_brdname[type[i] - 1],
1072 baseaddr[i]);
1073 brd->boardType = type[i];
1074 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1075 numports[i];
1076 brd->busType = MOXA_BUS_TYPE_ISA;
Alan Cox24cb2332008-04-30 00:54:19 -07001077 brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
Jiri Slabyd353eca2008-04-30 00:53:37 -07001078 if (!brd->basemem) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001079 printk(KERN_ERR "MOXA: can't remap %lx\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001080 baseaddr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 continue;
1082 }
Jiri Slaby03718232008-04-30 00:53:39 -07001083 if (moxa_init_board(brd, NULL)) {
1084 iounmap(brd->basemem);
1085 brd->basemem = NULL;
1086 continue;
1087 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001088
Jiri Slabybb9f9102008-04-30 00:53:48 -07001089 printk(KERN_INFO "MOXA isa board found at 0x%.8lu and "
1090 "ready (%u ports, firmware loaded)\n",
1091 baseaddr[i], brd->numPorts);
1092
Jiri Slabyd353eca2008-04-30 00:53:37 -07001093 brd++;
1094 isabrds++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
1096 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001101 retval = pci_register_driver(&moxa_pci_driver);
1102 if (retval) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001103 printk(KERN_ERR "Can't register MOXA pci driver!\n");
Jiri Slabyd353eca2008-04-30 00:53:37 -07001104 if (isabrds)
Jiri Slabya784bf72007-02-10 01:45:36 -08001105 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
1107#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001108
Jiri Slabya784bf72007-02-10 01:45:36 -08001109 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
1112static void __exit moxa_exit(void)
1113{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001114 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001116#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001117 pci_unregister_driver(&moxa_pci_driver);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001118#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001119
Jiri Slaby810ab092008-04-30 00:53:41 -07001120 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1121 if (moxa_boards[i].ready)
1122 moxa_board_deinit(&moxa_boards[i]);
Jiri Slaby2a541342008-04-30 00:53:45 -07001123
1124 del_timer_sync(&moxaTimer);
1125
1126 if (tty_unregister_driver(moxaDriver))
1127 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1128 "serial driver\n");
1129 put_tty_driver(moxaDriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
1132module_init(moxa_init);
1133module_exit(moxa_exit);
1134
Alan Coxf1761782009-11-30 13:17:51 +00001135static void moxa_shutdown(struct tty_port *port)
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001136{
Alan Coxf1761782009-11-30 13:17:51 +00001137 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1138 MoxaPortDisable(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001139 MoxaPortFlushData(ch, 2);
Alan Coxf1761782009-11-30 13:17:51 +00001140 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001141}
1142
Alan Cox31f35932009-01-02 13:45:05 +00001143static int moxa_carrier_raised(struct tty_port *port)
1144{
1145 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1146 int dcd;
1147
1148 spin_lock_bh(&moxa_lock);
1149 dcd = ch->DCDState;
1150 spin_unlock_bh(&moxa_lock);
1151 return dcd;
1152}
1153
Alan Coxf1761782009-11-30 13:17:51 +00001154static void moxa_dtr_rts(struct tty_port *port, int onoff)
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001155{
Alan Coxf1761782009-11-30 13:17:51 +00001156 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1157 MoxaPortLineCtrl(ch, onoff, onoff);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001158}
1159
Alan Coxf1761782009-11-30 13:17:51 +00001160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161static int moxa_open(struct tty_struct *tty, struct file *filp)
1162{
Jiri Slaby810ab092008-04-30 00:53:41 -07001163 struct moxa_board_conf *brd;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001164 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 int port;
1166 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Jiri Slaby11324ed2007-02-10 01:45:31 -08001168 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (port == MAX_PORTS) {
Jiri Slaby74d7d972008-04-30 00:53:43 -07001170 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001172 if (mutex_lock_interruptible(&moxa_openlock))
1173 return -ERESTARTSYS;
Jiri Slaby810ab092008-04-30 00:53:41 -07001174 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001175 if (!brd->ready) {
1176 mutex_unlock(&moxa_openlock);
Jiri Slaby810ab092008-04-30 00:53:41 -07001177 return -ENODEV;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Dirk Eibachf0e85272009-06-11 14:56:44 +01001180 if (port % MAX_PORTS_PER_BOARD >= brd->numPorts) {
1181 mutex_unlock(&moxa_openlock);
1182 return -ENODEV;
1183 }
1184
Jiri Slaby810ab092008-04-30 00:53:41 -07001185 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
Alan Cox9de6a512008-07-16 21:56:02 +01001186 ch->port.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 tty->driver_data = ch;
Alan Coxd450b5a2008-10-13 10:39:58 +01001188 tty_port_tty_set(&ch->port, tty);
Alan Coxf1761782009-11-30 13:17:51 +00001189 mutex_lock(&ch->port.mutex);
Alan Cox9de6a512008-07-16 21:56:02 +01001190 if (!(ch->port.flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 ch->statusflags = 0;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001192 moxa_set_tty_param(tty, tty->termios);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001193 MoxaPortLineCtrl(ch, 1, 1);
1194 MoxaPortEnable(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001195 MoxaSetFifo(ch, ch->type == PORT_16550A);
Alan Cox9de6a512008-07-16 21:56:02 +01001196 ch->port.flags |= ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
Alan Coxf1761782009-11-30 13:17:51 +00001198 mutex_unlock(&ch->port.mutex);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001199 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Alan Coxf1761782009-11-30 13:17:51 +00001201 retval = tty_port_block_til_ready(&ch->port, tty, filp);
1202 if (retval == 0)
1203 set_bit(ASYNCB_NORMAL_ACTIVE, &ch->port.flags);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001204 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
1207static void moxa_close(struct tty_struct *tty, struct file *filp)
1208{
Alan Coxf1761782009-11-30 13:17:51 +00001209 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 ch->cflag = tty->termios->c_cflag;
Alan Coxf1761782009-11-30 13:17:51 +00001211 mutex_lock(&moxa_openlock);
1212 tty_port_close(&ch->port, tty, filp);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001213 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
1216static int moxa_write(struct tty_struct *tty,
1217 const unsigned char *buf, int count)
1218{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001219 struct moxa_port *ch = tty->driver_data;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001220 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (ch == NULL)
Jiri Slabyb4173f42008-04-30 00:53:40 -07001223 return 0;
Alan Cox33f0f882006-01-09 20:54:13 -08001224
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001225 spin_lock_bh(&moxa_lock);
Alan Coxd450b5a2008-10-13 10:39:58 +01001226 len = MoxaPortWriteData(tty, buf, count);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001227 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 ch->statusflags |= LOWWAIT;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001230 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
1233static int moxa_write_room(struct tty_struct *tty)
1234{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001235 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 if (tty->stopped)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001238 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001239 ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (ch == NULL)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001241 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001242 return MoxaPortTxFree(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
1245static void moxa_flush_buffer(struct tty_struct *tty)
1246{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001247 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 if (ch == NULL)
1250 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001251 MoxaPortFlushData(ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 tty_wakeup(tty);
1253}
1254
1255static int moxa_chars_in_buffer(struct tty_struct *tty)
1256{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001257 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Alan Cox978e5952008-04-30 00:53:59 -07001260 lock_kernel();
Jiri Slabyb4173f42008-04-30 00:53:40 -07001261 chars = MoxaPortTxQueue(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (chars) {
1263 /*
1264 * Make it possible to wakeup anything waiting for output
1265 * in tty_ioctl.c, etc.
1266 */
1267 if (!(ch->statusflags & EMPTYWAIT))
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001268 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
Alan Cox978e5952008-04-30 00:53:59 -07001270 unlock_kernel();
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001271 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1275{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001276 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 int flag = 0, dtr, rts;
1278
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001279 mutex_lock(&moxa_openlock);
1280 ch = tty->driver_data;
1281 if (!ch) {
1282 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -07001283 return -EINVAL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Jiri Slabyb4173f42008-04-30 00:53:40 -07001286 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (dtr)
1288 flag |= TIOCM_DTR;
1289 if (rts)
1290 flag |= TIOCM_RTS;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001291 dtr = MoxaPortLineStatus(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (dtr & 1)
1293 flag |= TIOCM_CTS;
1294 if (dtr & 2)
1295 flag |= TIOCM_DSR;
1296 if (dtr & 4)
1297 flag |= TIOCM_CD;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001298 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return flag;
1300}
1301
1302static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1303 unsigned int set, unsigned int clear)
1304{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001305 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 int port;
1307 int dtr, rts;
1308
Jiri Slaby11324ed2007-02-10 01:45:31 -08001309 port = tty->index;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001310 mutex_lock(&moxa_openlock);
1311 ch = tty->driver_data;
1312 if (!ch) {
1313 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -07001314 return -EINVAL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Jiri Slabyb4173f42008-04-30 00:53:40 -07001317 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (set & TIOCM_RTS)
1319 rts = 1;
1320 if (set & TIOCM_DTR)
1321 dtr = 1;
1322 if (clear & TIOCM_RTS)
1323 rts = 0;
1324 if (clear & TIOCM_DTR)
1325 dtr = 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001326 MoxaPortLineCtrl(ch, dtr, rts);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001327 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return 0;
1329}
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331static void moxa_throttle(struct tty_struct *tty)
1332{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001333 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 ch->statusflags |= THROTTLE;
1336}
1337
1338static void moxa_unthrottle(struct tty_struct *tty)
1339{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001340 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 ch->statusflags &= ~THROTTLE;
1343}
1344
1345static void moxa_set_termios(struct tty_struct *tty,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001346 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001348 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 if (ch == NULL)
1351 return;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001352 moxa_set_tty_param(tty, old_termios);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001353 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
Alan Cox9de6a512008-07-16 21:56:02 +01001354 wake_up_interruptible(&ch->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
1357static void moxa_stop(struct tty_struct *tty)
1358{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001359 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 if (ch == NULL)
1362 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001363 MoxaPortTxDisable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 ch->statusflags |= TXSTOPPED;
1365}
1366
1367
1368static void moxa_start(struct tty_struct *tty)
1369{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001370 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 if (ch == NULL)
1373 return;
1374
1375 if (!(ch->statusflags & TXSTOPPED))
1376 return;
1377
Jiri Slabyb4173f42008-04-30 00:53:40 -07001378 MoxaPortTxEnable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 ch->statusflags &= ~TXSTOPPED;
1380}
1381
1382static void moxa_hangup(struct tty_struct *tty)
1383{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001384 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001386 mutex_lock(&moxa_openlock);
1387 ch = tty->driver_data;
Alan Coxf1761782009-11-30 13:17:51 +00001388 tty_port_hangup(&ch->port);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001389 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
1391
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001392static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1393{
Alan Coxd450b5a2008-10-13 10:39:58 +01001394 struct tty_struct *tty;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001395 dcd = !!dcd;
1396
Alan Coxd450b5a2008-10-13 10:39:58 +01001397 if (dcd != p->DCDState) {
1398 tty = tty_port_tty_get(&p->port);
1399 if (tty && C_CLOCAL(tty) && !dcd)
1400 tty_hangup(tty);
1401 tty_kref_put(tty);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001402 }
1403 p->DCDState = dcd;
1404}
1405
1406static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1407 u16 __iomem *ip)
1408{
Alan Coxd450b5a2008-10-13 10:39:58 +01001409 struct tty_struct *tty = tty_port_tty_get(&p->port);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001410 void __iomem *ofsAddr;
Alan Cox9de6a512008-07-16 21:56:02 +01001411 unsigned int inited = p->port.flags & ASYNC_INITIALIZED;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001412 u16 intr;
1413
1414 if (tty) {
1415 if ((p->statusflags & EMPTYWAIT) &&
1416 MoxaPortTxQueue(p) == 0) {
1417 p->statusflags &= ~EMPTYWAIT;
1418 tty_wakeup(tty);
1419 }
1420 if ((p->statusflags & LOWWAIT) && !tty->stopped &&
1421 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1422 p->statusflags &= ~LOWWAIT;
1423 tty_wakeup(tty);
1424 }
1425
1426 if (inited && !(p->statusflags & THROTTLE) &&
1427 MoxaPortRxQueue(p) > 0) { /* RX */
1428 MoxaPortReadData(p);
1429 tty_schedule_flip(tty);
1430 }
1431 } else {
1432 p->statusflags &= ~EMPTYWAIT;
1433 MoxaPortFlushData(p, 0); /* flush RX */
1434 }
1435
1436 if (!handle) /* nothing else to do */
Jiri Slaby0e0fd7d2009-04-06 17:34:04 +01001437 goto put;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001438
1439 intr = readw(ip); /* port irq status */
1440 if (intr == 0)
Jiri Slaby0e0fd7d2009-04-06 17:34:04 +01001441 goto put;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001442
1443 writew(0, ip); /* ACK port */
1444 ofsAddr = p->tableAddr;
1445 if (intr & IntrTx) /* disable tx intr */
1446 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1447 ofsAddr + HostStat);
1448
1449 if (!inited)
Jiri Slaby0e0fd7d2009-04-06 17:34:04 +01001450 goto put;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001451
1452 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1453 tty_insert_flip_char(tty, 0, TTY_BREAK);
1454 tty_schedule_flip(tty);
1455 }
1456
1457 if (intr & IntrLine)
1458 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
Jiri Slaby0e0fd7d2009-04-06 17:34:04 +01001459put:
1460 tty_kref_put(tty);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001461
1462 return 0;
1463}
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465static void moxa_poll(unsigned long ignored)
1466{
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001467 struct moxa_board_conf *brd;
1468 u16 __iomem *ip;
Jiri Slaby2a541342008-04-30 00:53:45 -07001469 unsigned int card, port, served = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001471 spin_lock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 for (card = 0; card < MAX_BOARDS; card++) {
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001473 brd = &moxa_boards[card];
1474 if (!brd->ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 continue;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001476
Jiri Slaby2a541342008-04-30 00:53:45 -07001477 served++;
1478
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001479 ip = NULL;
1480 if (readb(brd->intPend) == 0xff)
1481 ip = brd->intTable + readb(brd->intNdx);
1482
1483 for (port = 0; port < brd->numPorts; port++)
1484 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1485
1486 if (ip)
1487 writeb(0, brd->intPend); /* ACK */
1488
1489 if (moxaLowWaterChk) {
1490 struct moxa_port *p = brd->ports;
1491 for (port = 0; port < brd->numPorts; port++, p++)
1492 if (p->lowChkFlag) {
1493 p->lowChkFlag = 0;
1494 moxa_low_water_check(p->tableAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 }
1497 }
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001498 moxaLowWaterChk = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Jiri Slaby2a541342008-04-30 00:53:45 -07001500 if (served)
1501 mod_timer(&moxaTimer, jiffies + HZ / 50);
1502 spin_unlock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
1504
1505/******************************************************************************/
1506
Alan Coxdb1acaa2008-02-08 04:18:43 -08001507static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001509 register struct ktermios *ts = tty->termios;
1510 struct moxa_port *ch = tty->driver_data;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001511 int rts, cts, txflow, rxflow, xany, baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 rts = cts = txflow = rxflow = xany = 0;
1514 if (ts->c_cflag & CRTSCTS)
1515 rts = cts = 1;
1516 if (ts->c_iflag & IXON)
1517 txflow = 1;
1518 if (ts->c_iflag & IXOFF)
1519 rxflow = 1;
1520 if (ts->c_iflag & IXANY)
1521 xany = 1;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001522
1523 /* Clear the features we don't support */
1524 ts->c_cflag &= ~CMSPAR;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001525 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1526 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
Alan Coxdb1acaa2008-02-08 04:18:43 -08001527 if (baud == -1)
1528 baud = tty_termios_baud_rate(old_termios);
1529 /* Not put the baud rate into the termios data */
1530 tty_encode_baud_rate(tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
1532
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001533static void moxa_setup_empty_event(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001535 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001537 spin_lock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 ch->statusflags |= EMPTYWAIT;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001539 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542/*****************************************************************************
1543 * Driver level functions: *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Jiri Slabyb4173f42008-04-30 00:53:40 -07001546static void MoxaPortFlushData(struct moxa_port *port, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001549 if (mode < 0 || mode > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001551 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 moxafunc(ofsAddr, FC_FlushQueue, mode);
1553 if (mode != 1) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001554 port->lowChkFlag = 0;
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001555 moxa_low_water_check(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
1557}
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559/*
1560 * Moxa Port Number Description:
1561 *
1562 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1563 * the port number using in MOXA driver functions will be 0 to 31 for
1564 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1565 * to 127 for fourth. For example, if you setup three MOXA boards,
1566 * first board is C218, second board is C320-16 and third board is
1567 * C320-32. The port number of first board (C218 - 8 ports) is from
1568 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1569 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1570 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1571 * 127 will be invalid.
1572 *
1573 *
1574 * Moxa Functions Description:
1575 *
1576 * Function 1: Driver initialization routine, this routine must be
1577 * called when initialized driver.
1578 * Syntax:
1579 * void MoxaDriverInit();
1580 *
1581 *
1582 * Function 2: Moxa driver private IOCTL command processing.
1583 * Syntax:
1584 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1585 *
1586 * unsigned int cmd : IOCTL command
1587 * unsigned long arg : IOCTL argument
1588 * int port : port number (0 - 127)
1589 *
1590 * return: 0 (OK)
1591 * -EINVAL
1592 * -ENOIOCTLCMD
1593 *
1594 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 * Function 6: Enable this port to start Tx/Rx data.
1596 * Syntax:
1597 * void MoxaPortEnable(int port);
1598 * int port : port number (0 - 127)
1599 *
1600 *
1601 * Function 7: Disable this port
1602 * Syntax:
1603 * void MoxaPortDisable(int port);
1604 * int port : port number (0 - 127)
1605 *
1606 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 * Function 10: Setting baud rate of this port.
1608 * Syntax:
Jiri Slaby08d01c72008-04-30 00:53:47 -07001609 * speed_t MoxaPortSetBaud(int port, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 * int port : port number (0 - 127)
1611 * long baud : baud rate (50 - 115200)
1612 *
1613 * return: 0 : this port is invalid or baud < 50
1614 * 50 - 115200 : the real baud rate set to the port, if
1615 * the argument baud is large than maximun
1616 * available baud rate, the real setting
1617 * baud rate will be the maximun baud rate.
1618 *
1619 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 * Function 12: Configure the port.
1621 * Syntax:
Alan Cox606d0992006-12-08 02:38:45 -08001622 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 * int port : port number (0 - 127)
Alan Cox606d0992006-12-08 02:38:45 -08001624 * struct ktermios * termio : termio structure pointer
Alan Coxc7bce302006-09-30 23:27:24 -07001625 * speed_t baud : baud rate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 *
1627 * return: -1 : this port is invalid or termio == NULL
1628 * 0 : setting O.K.
1629 *
1630 *
1631 * Function 13: Get the DTR/RTS state of this port.
1632 * Syntax:
1633 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1634 * int port : port number (0 - 127)
1635 * int * dtrState : pointer to INT to receive the current DTR
1636 * state. (if NULL, this function will not
1637 * write to this address)
1638 * int * rtsState : pointer to INT to receive the current RTS
1639 * state. (if NULL, this function will not
1640 * write to this address)
1641 *
1642 * return: -1 : this port is invalid
1643 * 0 : O.K.
1644 *
1645 *
1646 * Function 14: Setting the DTR/RTS output state of this port.
1647 * Syntax:
1648 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1649 * int port : port number (0 - 127)
1650 * int dtrState : DTR output state (0: off, 1: on)
1651 * int rtsState : RTS output state (0: off, 1: on)
1652 *
1653 *
1654 * Function 15: Setting the flow control of this port.
1655 * Syntax:
1656 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1657 * int txFlow,int xany);
1658 * int port : port number (0 - 127)
1659 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1660 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1661 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1662 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1663 * int xany : S/W XANY flow control (0: no, 1: yes)
1664 *
1665 *
1666 * Function 16: Get ths line status of this port
1667 * Syntax:
1668 * int MoxaPortLineStatus(int port);
1669 * int port : port number (0 - 127)
1670 *
1671 * return: Bit 0 - CTS state (0: off, 1: on)
1672 * Bit 1 - DSR state (0: off, 1: on)
1673 * Bit 2 - DCD state (0: off, 1: on)
1674 *
1675 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 * Function 19: Flush the Rx/Tx buffer data of this port.
1677 * Syntax:
1678 * void MoxaPortFlushData(int port, int mode);
1679 * int port : port number (0 - 127)
1680 * int mode
1681 * 0 : flush the Rx buffer
1682 * 1 : flush the Tx buffer
1683 * 2 : flush the Rx and Tx buffer
1684 *
1685 *
1686 * Function 20: Write data.
1687 * Syntax:
1688 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1689 * int port : port number (0 - 127)
1690 * unsigned char * buffer : pointer to write data buffer.
1691 * int length : write data length
1692 *
1693 * return: 0 - length : real write data length
1694 *
1695 *
1696 * Function 21: Read data.
1697 * Syntax:
Alan Cox33f0f882006-01-09 20:54:13 -08001698 * int MoxaPortReadData(int port, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 * int port : port number (0 - 127)
Alan Cox33f0f882006-01-09 20:54:13 -08001700 * struct tty_struct *tty : tty for data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 *
1702 * return: 0 - length : real read data length
1703 *
1704 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 * Function 24: Get the Tx buffer current queued data bytes
1706 * Syntax:
1707 * int MoxaPortTxQueue(int port);
1708 * int port : port number (0 - 127)
1709 *
1710 * return: .. : Tx buffer current queued data bytes
1711 *
1712 *
1713 * Function 25: Get the Tx buffer current free space
1714 * Syntax:
1715 * int MoxaPortTxFree(int port);
1716 * int port : port number (0 - 127)
1717 *
1718 * return: .. : Tx buffer current free space
1719 *
1720 *
1721 * Function 26: Get the Rx buffer current queued data bytes
1722 * Syntax:
1723 * int MoxaPortRxQueue(int port);
1724 * int port : port number (0 - 127)
1725 *
1726 * return: .. : Rx buffer current queued data bytes
1727 *
1728 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 * Function 28: Disable port data transmission.
1730 * Syntax:
1731 * void MoxaPortTxDisable(int port);
1732 * int port : port number (0 - 127)
1733 *
1734 *
1735 * Function 29: Enable port data transmission.
1736 * Syntax:
1737 * void MoxaPortTxEnable(int port);
1738 * int port : port number (0 - 127)
1739 *
1740 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 * Function 31: Get the received BREAK signal count and reset it.
1742 * Syntax:
1743 * int MoxaPortResetBrkCnt(int port);
1744 * int port : port number (0 - 127)
1745 *
1746 * return: 0 - .. : BREAK signal count
1747 *
1748 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Jiri Slabyb4173f42008-04-30 00:53:40 -07001751static void MoxaPortEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
1753 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001754 u16 lowwater = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Jiri Slabyb4173f42008-04-30 00:53:40 -07001756 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 writew(lowwater, ofsAddr + Low_water);
Jiri Slaby08d01c72008-04-30 00:53:47 -07001758 if (MOXA_IS_320(port->board))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001760 else
1761 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1762 ofsAddr + HostStat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1765 moxafunc(ofsAddr, FC_FlushQueue, 2);
1766
1767 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1768 MoxaPortLineStatus(port);
1769}
1770
Jiri Slabyb4173f42008-04-30 00:53:40 -07001771static void MoxaPortDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001773 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
1775 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1776 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1777 writew(0, ofsAddr + HostStat);
1778 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1779}
1780
Jiri Slaby08d01c72008-04-30 00:53:47 -07001781static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
Jiri Slaby08d01c72008-04-30 00:53:47 -07001783 void __iomem *ofsAddr = port->tableAddr;
1784 unsigned int clock, val;
1785 speed_t max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Jiri Slaby08d01c72008-04-30 00:53:47 -07001787 max = MOXA_IS_320(port->board) ? 460800 : 921600;
1788 if (baud < 50)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001789 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if (baud > max)
1791 baud = max;
Jiri Slaby08d01c72008-04-30 00:53:47 -07001792 clock = 921600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 val = clock / baud;
1794 moxafunc(ofsAddr, FC_SetBaud, val);
1795 baud = clock / val;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001796 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797}
1798
Jiri Slabyb4173f42008-04-30 00:53:40 -07001799static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1800 speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
1802 void __iomem *ofsAddr;
1803 tcflag_t cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 tcflag_t mode = 0;
1805
Jiri Slabyb4173f42008-04-30 00:53:40 -07001806 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 cflag = termio->c_cflag; /* termio->c_cflag */
1808
1809 mode = termio->c_cflag & CSIZE;
1810 if (mode == CS5)
1811 mode = MX_CS5;
1812 else if (mode == CS6)
1813 mode = MX_CS6;
1814 else if (mode == CS7)
1815 mode = MX_CS7;
1816 else if (mode == CS8)
1817 mode = MX_CS8;
1818
1819 if (termio->c_cflag & CSTOPB) {
1820 if (mode == MX_CS5)
1821 mode |= MX_STOP15;
1822 else
1823 mode |= MX_STOP2;
1824 } else
1825 mode |= MX_STOP1;
1826
1827 if (termio->c_cflag & PARENB) {
1828 if (termio->c_cflag & PARODD)
1829 mode |= MX_PARODD;
1830 else
1831 mode |= MX_PAREVEN;
1832 } else
1833 mode |= MX_PARNONE;
1834
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001835 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Jiri Slaby08d01c72008-04-30 00:53:47 -07001837 if (MOXA_IS_320(port->board) && baud >= 921600)
1838 return -1;
1839
Alan Coxdb1acaa2008-02-08 04:18:43 -08001840 baud = MoxaPortSetBaud(port, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
Alan Coxf5c5a362009-11-30 13:17:57 +00001843 spin_lock_irq(&moxafunc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1845 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1846 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001847 moxa_wait_finish(ofsAddr);
Alan Coxf5c5a362009-11-30 13:17:57 +00001848 spin_unlock_irqrestore(&moxafunc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850 }
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001851 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852}
1853
Jiri Slabyb4173f42008-04-30 00:53:40 -07001854static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1855 int *rtsState)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001857 if (dtrState)
1858 *dtrState = !!(port->lineCtrl & DTR_ON);
1859 if (rtsState)
1860 *rtsState = !!(port->lineCtrl & RTS_ON);
1861
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001862 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863}
1864
Jiri Slabyb4173f42008-04-30 00:53:40 -07001865static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001867 u8 mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 if (dtr)
1870 mode |= DTR_ON;
1871 if (rts)
1872 mode |= RTS_ON;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001873 port->lineCtrl = mode;
1874 moxafunc(port->tableAddr, FC_LineControl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875}
1876
Jiri Slabyb4173f42008-04-30 00:53:40 -07001877static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1878 int txflow, int rxflow, int txany)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001880 int mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (rts)
1883 mode |= RTS_FlowCtl;
1884 if (cts)
1885 mode |= CTS_FlowCtl;
1886 if (txflow)
1887 mode |= Tx_FlowCtl;
1888 if (rxflow)
1889 mode |= Rx_FlowCtl;
1890 if (txany)
1891 mode |= IXM_IXANY;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001892 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
Jiri Slabyb4173f42008-04-30 00:53:40 -07001895static int MoxaPortLineStatus(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
1897 void __iomem *ofsAddr;
1898 int val;
1899
Jiri Slabyb4173f42008-04-30 00:53:40 -07001900 ofsAddr = port->tableAddr;
Alan Coxf5c5a362009-11-30 13:17:57 +00001901 if (MOXA_IS_320(port->board))
1902 val = moxafuncret(ofsAddr, FC_LineStatus, 0);
1903 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 val = readw(ofsAddr + FlagStat) >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 val &= 0x0B;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001906 if (val & 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 val |= 4;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001908 spin_lock_bh(&moxa_lock);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001909 moxa_new_dcdstate(port, val & 8);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001910 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 val &= 7;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001912 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913}
1914
Alan Coxd450b5a2008-10-13 10:39:58 +01001915static int MoxaPortWriteData(struct tty_struct *tty,
Jiri Slaby2108eba2008-04-30 00:53:44 -07001916 const unsigned char *buffer, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
Alan Coxd450b5a2008-10-13 10:39:58 +01001918 struct moxa_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001920 unsigned int c, total;
1921 u16 head, tail, tx_mask, spage, epage;
1922 u16 pageno, pageofs, bufhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Jiri Slabyb4173f42008-04-30 00:53:40 -07001924 ofsAddr = port->tableAddr;
1925 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 tx_mask = readw(ofsAddr + TX_mask);
1927 spage = readw(ofsAddr + Page_txb);
1928 epage = readw(ofsAddr + EndPage_txb);
1929 tail = readw(ofsAddr + TXwptr);
1930 head = readw(ofsAddr + TXrptr);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001931 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 if (c > len)
1933 c = len;
Alan Cox9de6a512008-07-16 21:56:02 +01001934 moxaLog.txcnt[port->port.tty->index] += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 total = c;
1936 if (spage == epage) {
1937 bufhead = readw(ofsAddr + Ofs_txb);
1938 writew(spage, baseAddr + Control_reg);
1939 while (c > 0) {
1940 if (head > tail)
1941 len = head - tail - 1;
1942 else
1943 len = tx_mask + 1 - tail;
1944 len = (c > len) ? len : c;
1945 ofs = baseAddr + DynPage_addr + bufhead + tail;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001946 memcpy_toio(ofs, buffer, len);
1947 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 tail = (tail + len) & tx_mask;
1949 c -= len;
1950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 pageno = spage + (tail >> 13);
1953 pageofs = tail & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001954 while (c > 0) {
1955 len = Page_size - pageofs;
1956 if (len > c)
1957 len = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 writeb(pageno, baseAddr + Control_reg);
1959 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001960 memcpy_toio(ofs, buffer, len);
1961 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 if (++pageno == epage)
1963 pageno = spage;
1964 pageofs = 0;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001965 c -= len;
1966 }
1967 tail = (tail + total) & tx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07001969 writew(tail, ofsAddr + TXwptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 writeb(1, ofsAddr + CD180TXirq); /* start to send */
Jiri Slaby2108eba2008-04-30 00:53:44 -07001971 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001974static int MoxaPortReadData(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
Alan Cox9de6a512008-07-16 21:56:02 +01001976 struct tty_struct *tty = port->port.tty;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001977 unsigned char *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001979 unsigned int count, len, total;
1980 u16 tail, rx_mask, spage, epage;
1981 u16 pageno, pageofs, bufhead, head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Jiri Slabyb4173f42008-04-30 00:53:40 -07001983 ofsAddr = port->tableAddr;
1984 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 head = readw(ofsAddr + RXrptr);
1986 tail = readw(ofsAddr + RXwptr);
1987 rx_mask = readw(ofsAddr + RX_mask);
1988 spage = readw(ofsAddr + Page_rxb);
1989 epage = readw(ofsAddr + EndPage_rxb);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001990 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 if (count == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08001992 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Alan Cox33f0f882006-01-09 20:54:13 -08001994 total = count;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001995 moxaLog.rxcnt[tty->index] += total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if (spage == epage) {
1997 bufhead = readw(ofsAddr + Ofs_rxb);
1998 writew(spage, baseAddr + Control_reg);
1999 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 ofs = baseAddr + DynPage_addr + bufhead + head;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002001 len = (tail >= head) ? (tail - head) :
2002 (rx_mask + 1 - head);
2003 len = tty_prepare_flip_string(tty, &dst,
2004 min(len, count));
2005 memcpy_fromio(dst, ofs, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 head = (head + len) & rx_mask;
2007 count -= len;
2008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 pageno = spage + (head >> 13);
2011 pageofs = head & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002012 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 writew(pageno, baseAddr + Control_reg);
2014 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002015 len = tty_prepare_flip_string(tty, &dst,
2016 min(Page_size - pageofs, count));
2017 memcpy_fromio(dst, ofs, len);
2018
2019 count -= len;
2020 pageofs = (pageofs + len) & Page_mask;
2021 if (pageofs == 0 && ++pageno == epage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 pageno = spage;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002023 }
2024 head = (head + total) & rx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002026 writew(head, ofsAddr + RXrptr);
2027 if (readb(ofsAddr + FlagStat) & Xoff_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 moxaLowWaterChk = 1;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002029 port->lowChkFlag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002031 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
2034
Jiri Slabyb4173f42008-04-30 00:53:40 -07002035static int MoxaPortTxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002037 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002038 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 rptr = readw(ofsAddr + TXrptr);
2041 wptr = readw(ofsAddr + TXwptr);
2042 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002043 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044}
2045
Jiri Slabyb4173f42008-04-30 00:53:40 -07002046static int MoxaPortTxFree(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002048 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002049 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 rptr = readw(ofsAddr + TXrptr);
2052 wptr = readw(ofsAddr + TXwptr);
2053 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002054 return mask - ((wptr - rptr) & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
Jiri Slabyb4173f42008-04-30 00:53:40 -07002057static int MoxaPortRxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002059 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002060 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 rptr = readw(ofsAddr + RXrptr);
2063 wptr = readw(ofsAddr + RXwptr);
2064 mask = readw(ofsAddr + RX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002065 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066}
2067
Jiri Slabyb4173f42008-04-30 00:53:40 -07002068static void MoxaPortTxDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002070 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071}
2072
Jiri Slabyb4173f42008-04-30 00:53:40 -07002073static void MoxaPortTxEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002075 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076}
2077
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002078static int moxa_get_serial_info(struct moxa_port *info,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002079 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002081 struct serial_struct tmp = {
2082 .type = info->type,
Alan Cox9de6a512008-07-16 21:56:02 +01002083 .line = info->port.tty->index,
2084 .flags = info->port.flags,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002085 .baud_base = 921600,
Alan Cox44b7d1b2008-07-16 21:57:18 +01002086 .close_delay = info->port.close_delay
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002087 };
2088 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089}
2090
2091
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002092static int moxa_set_serial_info(struct moxa_port *info,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002093 struct serial_struct __user *new_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
2095 struct serial_struct new_serial;
2096
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002097 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 return -EFAULT;
2099
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002100 if (new_serial.irq != 0 || new_serial.port != 0 ||
2101 new_serial.custom_divisor != 0 ||
2102 new_serial.baud_base != 921600)
2103 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 if (!capable(CAP_SYS_ADMIN)) {
2106 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
Alan Cox9de6a512008-07-16 21:56:02 +01002107 (info->port.flags & ~ASYNC_USR_MASK)))
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002108 return -EPERM;
2109 } else
Alan Cox44b7d1b2008-07-16 21:57:18 +01002110 info->port.close_delay = new_serial.close_delay * HZ / 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
Alan Cox9de6a512008-07-16 21:56:02 +01002113 new_serial.flags |= (info->port.flags & ASYNC_FLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002115 MoxaSetFifo(info, new_serial.type == PORT_16550A);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 info->type = new_serial.type;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002118 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119}
2120
2121
2122
2123/*****************************************************************************
2124 * Static local functions: *
2125 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Jiri Slabyb4173f42008-04-30 00:53:40 -07002127static void MoxaSetFifo(struct moxa_port *port, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002129 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
2131 if (!enable) {
2132 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2133 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2134 } else {
2135 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2136 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2137 }
2138}