blob: 7ab720f1f30ca8e0a50397a3c6d6088763579561 [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 */
Alan Coxa808ac02009-11-30 13:18:02 +0000154#define TXSTOPPED 1
155#define LOWWAIT 2
156#define EMPTYWAIT 3
157#define THROTTLE 4
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
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 Slaby6f56b652007-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
Alan Coxa808ac02009-11-30 13:18:02 +0000238static DEFINE_SPINLOCK(moxafunc_lock);
239
Jiri Slaby74d7d972008-04-30 00:53:43 -0700240static void moxa_wait_finish(void __iomem *ofsAddr)
241{
242 unsigned long end = jiffies + moxaFuncTout;
243
244 while (readw(ofsAddr + FuncCode) != 0)
245 if (time_after(jiffies, end))
246 return;
247 if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
248 printk(KERN_WARNING "moxa function expired\n");
249}
250
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700251static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
Jiri Slaby74d7d972008-04-30 00:53:43 -0700252{
Alan Coxf5c5a362009-11-30 13:17:57 +0000253 unsigned long flags;
254 spin_lock_irqsave(&moxafunc_lock, flags);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700255 writew(arg, ofsAddr + FuncArg);
256 writew(cmd, ofsAddr + FuncCode);
257 moxa_wait_finish(ofsAddr);
Alan Coxf5c5a362009-11-30 13:17:57 +0000258 spin_unlock_irqrestore(&moxafunc_lock, flags);
259}
260
261static int moxafuncret(void __iomem *ofsAddr, u16 cmd, u16 arg)
262{
263 unsigned long flags;
264 u16 ret;
265 spin_lock_irqsave(&moxafunc_lock, flags);
266 writew(arg, ofsAddr + FuncArg);
267 writew(cmd, ofsAddr + FuncCode);
268 moxa_wait_finish(ofsAddr);
269 ret = readw(ofsAddr + FuncArg);
270 spin_unlock_irqrestore(&moxafunc_lock, flags);
271 return ret;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700272}
273
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700274static void moxa_low_water_check(void __iomem *ofsAddr)
275{
276 u16 rptr, wptr, mask, len;
277
278 if (readb(ofsAddr + FlagStat) & Xoff_state) {
279 rptr = readw(ofsAddr + RXrptr);
280 wptr = readw(ofsAddr + RXwptr);
281 mask = readw(ofsAddr + RX_mask);
282 len = (wptr - rptr) & mask;
283 if (len <= Low_water)
284 moxafunc(ofsAddr, FC_SendXon, 0);
285 }
286}
287
Jiri Slaby74d7d972008-04-30 00:53:43 -0700288/*
289 * TTY operations
290 */
291
292static int moxa_ioctl(struct tty_struct *tty, struct file *file,
293 unsigned int cmd, unsigned long arg)
294{
295 struct moxa_port *ch = tty->driver_data;
296 void __user *argp = (void __user *)arg;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700297 int status, ret = 0;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700298
299 if (tty->index == MAX_PORTS) {
300 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
301 cmd != MOXA_GETMSTATUS)
302 return -EINVAL;
303 } else if (!ch)
304 return -ENODEV;
305
306 switch (cmd) {
307 case MOXA_GETDATACOUNT:
308 moxaLog.tick = jiffies;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700309 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
310 ret = -EFAULT;
311 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700312 case MOXA_FLUSH_QUEUE:
313 MoxaPortFlushData(ch, arg);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700314 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700315 case MOXA_GET_IOQUEUE: {
316 struct moxaq_str __user *argm = argp;
317 struct moxaq_str tmp;
318 struct moxa_port *p;
319 unsigned int i, j;
320
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700321 mutex_lock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700322 for (i = 0; i < MAX_BOARDS; i++) {
323 p = moxa_boards[i].ports;
324 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
325 memset(&tmp, 0, sizeof(tmp));
326 if (moxa_boards[i].ready) {
327 tmp.inq = MoxaPortRxQueue(p);
328 tmp.outq = MoxaPortTxQueue(p);
329 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700330 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
331 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700332 return -EFAULT;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700333 }
Jiri Slaby74d7d972008-04-30 00:53:43 -0700334 }
335 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700336 mutex_unlock(&moxa_openlock);
337 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700338 } case MOXA_GET_OQUEUE:
339 status = MoxaPortTxQueue(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700340 ret = put_user(status, (unsigned long __user *)argp);
341 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700342 case MOXA_GET_IQUEUE:
343 status = MoxaPortRxQueue(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700344 ret = put_user(status, (unsigned long __user *)argp);
345 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700346 case MOXA_GETMSTATUS: {
347 struct mxser_mstatus __user *argm = argp;
348 struct mxser_mstatus tmp;
349 struct moxa_port *p;
350 unsigned int i, j;
351
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700352 mutex_lock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700353 for (i = 0; i < MAX_BOARDS; i++) {
354 p = moxa_boards[i].ports;
355 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
Alan Coxd450b5a2008-10-13 10:39:58 +0100356 struct tty_struct *ttyp;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700357 memset(&tmp, 0, sizeof(tmp));
358 if (!moxa_boards[i].ready)
359 goto copy;
360
361 status = MoxaPortLineStatus(p);
362 if (status & 1)
363 tmp.cts = 1;
364 if (status & 2)
365 tmp.dsr = 1;
366 if (status & 4)
367 tmp.dcd = 1;
368
Alan Coxd450b5a2008-10-13 10:39:58 +0100369 ttyp = tty_port_tty_get(&p->port);
370 if (!ttyp || !ttyp->termios)
Jiri Slaby74d7d972008-04-30 00:53:43 -0700371 tmp.cflag = p->cflag;
372 else
Alan Coxd450b5a2008-10-13 10:39:58 +0100373 tmp.cflag = ttyp->termios->c_cflag;
374 tty_kref_put(tty);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700375copy:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700376 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
377 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700378 return -EFAULT;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700379 }
Jiri Slaby74d7d972008-04-30 00:53:43 -0700380 }
381 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700382 mutex_unlock(&moxa_openlock);
383 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700384 }
385 case TIOCGSERIAL:
Alan Coxa808ac02009-11-30 13:18:02 +0000386 mutex_lock(&ch->port.mutex);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700387 ret = moxa_get_serial_info(ch, argp);
Alan Coxa808ac02009-11-30 13:18:02 +0000388 mutex_unlock(&ch->port.mutex);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700389 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700390 case TIOCSSERIAL:
Alan Coxa808ac02009-11-30 13:18:02 +0000391 mutex_lock(&ch->port.mutex);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700392 ret = moxa_set_serial_info(ch, argp);
Alan Coxa808ac02009-11-30 13:18:02 +0000393 mutex_unlock(&ch->port.mutex);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700394 break;
395 default:
396 ret = -ENOIOCTLCMD;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700397 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700398 return ret;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700399}
400
Alan Cox9e989662008-07-22 11:18:03 +0100401static int moxa_break_ctl(struct tty_struct *tty, int state)
Jiri Slaby74d7d972008-04-30 00:53:43 -0700402{
403 struct moxa_port *port = tty->driver_data;
404
405 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
406 Magic_code);
Alan Cox9e989662008-07-22 11:18:03 +0100407 return 0;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700408}
409
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700410static const struct tty_operations moxa_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 .open = moxa_open,
412 .close = moxa_close,
413 .write = moxa_write,
414 .write_room = moxa_write_room,
415 .flush_buffer = moxa_flush_buffer,
416 .chars_in_buffer = moxa_chars_in_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 .ioctl = moxa_ioctl,
418 .throttle = moxa_throttle,
419 .unthrottle = moxa_unthrottle,
420 .set_termios = moxa_set_termios,
421 .stop = moxa_stop,
422 .start = moxa_start,
423 .hangup = moxa_hangup,
Jiri Slaby74d7d972008-04-30 00:53:43 -0700424 .break_ctl = moxa_break_ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 .tiocmget = moxa_tiocmget,
426 .tiocmset = moxa_tiocmset,
427};
428
Alan Cox31f35932009-01-02 13:45:05 +0000429static const struct tty_port_operations moxa_port_ops = {
430 .carrier_raised = moxa_carrier_raised,
Alan Coxf1761782009-11-30 13:17:51 +0000431 .dtr_rts = moxa_dtr_rts,
432 .shutdown = moxa_shutdown,
Alan Cox31f35932009-01-02 13:45:05 +0000433};
434
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800435static struct tty_driver *moxaDriver;
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800436static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
Ingo Molnar34af9462006-06-27 02:53:55 -0700437static DEFINE_SPINLOCK(moxa_lock);
Alan Cox33f0f882006-01-09 20:54:13 -0800438
Jiri Slaby74d7d972008-04-30 00:53:43 -0700439/*
440 * HW init
441 */
442
Jiri Slaby03718232008-04-30 00:53:39 -0700443static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
444{
445 switch (brd->boardType) {
446 case MOXA_BOARD_C218_ISA:
447 case MOXA_BOARD_C218_PCI:
448 if (model != 1)
449 goto err;
450 break;
451 case MOXA_BOARD_CP204J:
452 if (model != 3)
453 goto err;
454 break;
455 default:
456 if (model != 2)
457 goto err;
458 break;
459 }
460 return 0;
461err:
462 return -EINVAL;
463}
464
465static int moxa_check_fw(const void *ptr)
466{
467 const __le16 *lptr = ptr;
468
469 if (*lptr != cpu_to_le16(0x7980))
470 return -EINVAL;
471
472 return 0;
473}
474
475static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
476 size_t len)
477{
478 void __iomem *baseAddr = brd->basemem;
479 u16 tmp;
480
481 writeb(HW_reset, baseAddr + Control_reg); /* reset */
482 msleep(10);
483 memset_io(baseAddr, 0, 4096);
484 memcpy_toio(baseAddr, buf, len); /* download BIOS */
485 writeb(0, baseAddr + Control_reg); /* restart */
486
487 msleep(2000);
488
489 switch (brd->boardType) {
490 case MOXA_BOARD_C218_ISA:
491 case MOXA_BOARD_C218_PCI:
492 tmp = readw(baseAddr + C218_key);
493 if (tmp != C218_KeyCode)
494 goto err;
495 break;
496 case MOXA_BOARD_CP204J:
497 tmp = readw(baseAddr + C218_key);
498 if (tmp != CP204J_KeyCode)
499 goto err;
500 break;
501 default:
502 tmp = readw(baseAddr + C320_key);
503 if (tmp != C320_KeyCode)
504 goto err;
505 tmp = readw(baseAddr + C320_status);
506 if (tmp != STS_init) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700507 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
Jiri Slaby03718232008-04-30 00:53:39 -0700508 "module not found\n");
509 return -EIO;
510 }
511 break;
512 }
513
514 return 0;
515err:
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700516 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700517 return -EIO;
518}
519
520static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
521 size_t len)
522{
523 void __iomem *baseAddr = brd->basemem;
524
525 if (len < 7168) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700526 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700527 return -EINVAL;
528 }
529
530 writew(len - 7168 - 2, baseAddr + C320bapi_len);
531 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
532 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
533 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
534 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
535
536 return 0;
537}
538
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700539static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
Jiri Slaby03718232008-04-30 00:53:39 -0700540 size_t len)
541{
542 void __iomem *baseAddr = brd->basemem;
Harvey Harrisonb46f69c2008-10-15 22:04:19 -0700543 const __le16 *uptr = ptr;
Jiri Slaby03718232008-04-30 00:53:39 -0700544 size_t wlen, len2, j;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700545 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
Jiri Slaby08d01c72008-04-30 00:53:47 -0700546 unsigned int i, retry;
Jiri Slaby03718232008-04-30 00:53:39 -0700547 u16 usum, keycode;
548
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700549 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
550 C218_KeyCode;
551
552 switch (brd->boardType) {
553 case MOXA_BOARD_CP204J:
554 case MOXA_BOARD_C218_ISA:
555 case MOXA_BOARD_C218_PCI:
556 key = C218_key;
557 loadbuf = C218_LoadBuf;
558 loadlen = C218DLoad_len;
559 checksum = C218check_sum;
560 checksum_ok = C218chksum_ok;
561 break;
562 default:
563 key = C320_key;
564 keycode = C320_KeyCode;
565 loadbuf = C320_LoadBuf;
566 loadlen = C320DLoad_len;
567 checksum = C320check_sum;
568 checksum_ok = C320chksum_ok;
569 break;
570 }
571
Jiri Slaby03718232008-04-30 00:53:39 -0700572 usum = 0;
573 wlen = len >> 1;
574 for (i = 0; i < wlen; i++)
575 usum += le16_to_cpu(uptr[i]);
576 retry = 0;
577 do {
578 wlen = len >> 1;
579 j = 0;
580 while (wlen) {
581 len2 = (wlen > 2048) ? 2048 : wlen;
582 wlen -= len2;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700583 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
Jiri Slaby03718232008-04-30 00:53:39 -0700584 j += len2 << 1;
585
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700586 writew(len2, baseAddr + loadlen);
587 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700588 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700589 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700590 break;
591 msleep(10);
592 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700593 if (readw(baseAddr + key) != keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700594 return -EIO;
595 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700596 writew(0, baseAddr + loadlen);
597 writew(usum, baseAddr + checksum);
598 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700599 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700600 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700601 break;
602 msleep(10);
603 }
604 retry++;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700605 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
606 if (readb(baseAddr + checksum_ok) != 1)
Jiri Slaby03718232008-04-30 00:53:39 -0700607 return -EIO;
608
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700609 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700610 for (i = 0; i < 600; i++) {
611 if (readw(baseAddr + Magic_no) == Magic_code)
612 break;
613 msleep(10);
614 }
615 if (readw(baseAddr + Magic_no) != Magic_code)
616 return -EIO;
617
Jiri Slaby08d01c72008-04-30 00:53:47 -0700618 if (MOXA_IS_320(brd)) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700619 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
620 writew(0x3800, baseAddr + TMS320_PORT1);
621 writew(0x3900, baseAddr + TMS320_PORT2);
622 writew(28499, baseAddr + TMS320_CLOCK);
623 } else {
624 writew(0x3200, baseAddr + TMS320_PORT1);
625 writew(0x3400, baseAddr + TMS320_PORT2);
626 writew(19999, baseAddr + TMS320_CLOCK);
627 }
Jiri Slaby03718232008-04-30 00:53:39 -0700628 }
629 writew(1, baseAddr + Disable_IRQ);
630 writew(0, baseAddr + Magic_no);
631 for (i = 0; i < 500; i++) {
632 if (readw(baseAddr + Magic_no) == Magic_code)
633 break;
634 msleep(10);
635 }
636 if (readw(baseAddr + Magic_no) != Magic_code)
637 return -EIO;
638
Jiri Slaby08d01c72008-04-30 00:53:47 -0700639 if (MOXA_IS_320(brd)) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700640 j = readw(baseAddr + Module_cnt);
641 if (j <= 0)
642 return -EIO;
643 brd->numPorts = j * 8;
644 writew(j, baseAddr + Module_no);
645 writew(0, baseAddr + Magic_no);
646 for (i = 0; i < 600; i++) {
647 if (readw(baseAddr + Magic_no) == Magic_code)
648 break;
649 msleep(10);
650 }
651 if (readw(baseAddr + Magic_no) != Magic_code)
652 return -EIO;
Jiri Slaby03718232008-04-30 00:53:39 -0700653 }
Jiri Slaby03718232008-04-30 00:53:39 -0700654 brd->intNdx = baseAddr + IRQindex;
655 brd->intPend = baseAddr + IRQpending;
656 brd->intTable = baseAddr + IRQtable;
657
658 return 0;
659}
660
661static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
662 size_t len)
663{
664 void __iomem *ofsAddr, *baseAddr = brd->basemem;
665 struct moxa_port *port;
666 int retval, i;
667
668 if (len % 2) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700669 printk(KERN_ERR "MOXA: bios length is not even\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700670 return -EINVAL;
671 }
672
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700673 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
674 if (retval)
675 return retval;
676
Jiri Slaby03718232008-04-30 00:53:39 -0700677 switch (brd->boardType) {
678 case MOXA_BOARD_C218_ISA:
679 case MOXA_BOARD_C218_PCI:
680 case MOXA_BOARD_CP204J:
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 writew(C218rx_mask, ofsAddr + RX_mask);
689 writew(C218tx_mask, ofsAddr + TX_mask);
690 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
691 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
692
693 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
694 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
695
696 }
697 break;
698 default:
Jiri Slaby03718232008-04-30 00:53:39 -0700699 port = brd->ports;
700 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700701 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700702 port->DCDState = 0;
703 port->tableAddr = baseAddr + Extern_table +
704 Extern_size * i;
705 ofsAddr = port->tableAddr;
706 switch (brd->numPorts) {
707 case 8:
708 writew(C320p8rx_mask, ofsAddr + RX_mask);
709 writew(C320p8tx_mask, ofsAddr + TX_mask);
710 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
711 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
712 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
713 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
714
715 break;
716 case 16:
717 writew(C320p16rx_mask, ofsAddr + RX_mask);
718 writew(C320p16tx_mask, ofsAddr + TX_mask);
719 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
720 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
721 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
722 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
723 break;
724
725 case 24:
726 writew(C320p24rx_mask, ofsAddr + RX_mask);
727 writew(C320p24tx_mask, ofsAddr + TX_mask);
728 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
729 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
730 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
731 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
732 break;
733 case 32:
734 writew(C320p32rx_mask, ofsAddr + RX_mask);
735 writew(C320p32tx_mask, ofsAddr + TX_mask);
736 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
737 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
738 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
739 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
740 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
741 break;
742 }
743 }
744 break;
745 }
Jiri Slaby03718232008-04-30 00:53:39 -0700746 return 0;
747}
748
749static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
750{
David Howells2bca76e2008-07-08 17:37:15 +0100751 const void *ptr = fw->data;
Jiri Slaby03718232008-04-30 00:53:39 -0700752 char rsn[64];
753 u16 lens[5];
754 size_t len;
755 unsigned int a, lenp, lencnt;
756 int ret = -EINVAL;
757 struct {
758 __le32 magic; /* 0x34303430 */
759 u8 reserved1[2];
760 u8 type; /* UNIX = 3 */
761 u8 model; /* C218T=1, C320T=2, CP204=3 */
762 u8 reserved2[8];
763 __le16 len[5];
David Howells2bca76e2008-07-08 17:37:15 +0100764 } const *hdr = ptr;
Jiri Slaby03718232008-04-30 00:53:39 -0700765
766 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
767
768 if (fw->size < MOXA_FW_HDRLEN) {
769 strcpy(rsn, "too short (even header won't fit)");
770 goto err;
771 }
772 if (hdr->magic != cpu_to_le32(0x30343034)) {
773 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
774 goto err;
775 }
776 if (hdr->type != 3) {
777 sprintf(rsn, "not for linux, type is %u", hdr->type);
778 goto err;
779 }
780 if (moxa_check_fw_model(brd, hdr->model)) {
781 sprintf(rsn, "not for this card, model is %u", hdr->model);
782 goto err;
783 }
784
785 len = MOXA_FW_HDRLEN;
786 lencnt = hdr->model == 2 ? 5 : 3;
787 for (a = 0; a < ARRAY_SIZE(lens); a++) {
788 lens[a] = le16_to_cpu(hdr->len[a]);
789 if (lens[a] && len + lens[a] <= fw->size &&
790 moxa_check_fw(&fw->data[len]))
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700791 printk(KERN_WARNING "MOXA firmware: unexpected input "
Jiri Slaby03718232008-04-30 00:53:39 -0700792 "at offset %u, but going on\n", (u32)len);
793 if (!lens[a] && a < lencnt) {
794 sprintf(rsn, "too few entries in fw file");
795 goto err;
796 }
797 len += lens[a];
798 }
799
800 if (len != fw->size) {
801 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
802 (u32)len);
803 goto err;
804 }
805
806 ptr += MOXA_FW_HDRLEN;
807 lenp = 0; /* bios */
808
809 strcpy(rsn, "read above");
810
811 ret = moxa_load_bios(brd, ptr, lens[lenp]);
812 if (ret)
813 goto err;
814
815 /* we skip the tty section (lens[1]), since we don't need it */
816 ptr += lens[lenp] + lens[lenp + 1];
817 lenp += 2; /* comm */
818
819 if (hdr->model == 2) {
820 ret = moxa_load_320b(brd, ptr, lens[lenp]);
821 if (ret)
822 goto err;
823 /* skip another tty */
824 ptr += lens[lenp] + lens[lenp + 1];
825 lenp += 2;
826 }
827
828 ret = moxa_load_code(brd, ptr, lens[lenp]);
829 if (ret)
830 goto err;
831
832 return 0;
833err:
834 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
835 return ret;
836}
837
838static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
839{
840 const struct firmware *fw;
841 const char *file;
Jiri Slaby810ab092008-04-30 00:53:41 -0700842 struct moxa_port *p;
843 unsigned int i;
Jiri Slaby03718232008-04-30 00:53:39 -0700844 int ret;
845
Jiri Slaby810ab092008-04-30 00:53:41 -0700846 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
847 GFP_KERNEL);
848 if (brd->ports == NULL) {
849 printk(KERN_ERR "cannot allocate memory for ports\n");
850 ret = -ENOMEM;
851 goto err;
852 }
853
854 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
Alan Cox9de6a512008-07-16 21:56:02 +0100855 tty_port_init(&p->port);
Alan Cox31f35932009-01-02 13:45:05 +0000856 p->port.ops = &moxa_port_ops;
Alan Cox44b7d1b2008-07-16 21:57:18 +0100857 p->type = PORT_16550A;
858 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Jiri Slaby810ab092008-04-30 00:53:41 -0700859 }
860
Jiri Slaby03718232008-04-30 00:53:39 -0700861 switch (brd->boardType) {
862 case MOXA_BOARD_C218_ISA:
863 case MOXA_BOARD_C218_PCI:
864 file = "c218tunx.cod";
865 break;
866 case MOXA_BOARD_CP204J:
867 file = "cp204unx.cod";
868 break;
869 default:
870 file = "c320tunx.cod";
871 break;
872 }
873
874 ret = request_firmware(&fw, file, dev);
875 if (ret) {
Jiri Slabyec09cd52008-04-30 00:53:49 -0700876 printk(KERN_ERR "MOXA: request_firmware failed. Make sure "
877 "you've placed '%s' file into your firmware "
878 "loader directory (e.g. /lib/firmware)\n",
879 file);
Jiri Slaby810ab092008-04-30 00:53:41 -0700880 goto err_free;
Jiri Slaby03718232008-04-30 00:53:39 -0700881 }
882
883 ret = moxa_load_fw(brd, fw);
884
885 release_firmware(fw);
Jiri Slaby810ab092008-04-30 00:53:41 -0700886
887 if (ret)
888 goto err_free;
889
Jiri Slaby2a541342008-04-30 00:53:45 -0700890 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700891 brd->ready = 1;
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700892 if (!timer_pending(&moxaTimer))
893 mod_timer(&moxaTimer, jiffies + HZ / 50);
Jiri Slaby2a541342008-04-30 00:53:45 -0700894 spin_unlock_bh(&moxa_lock);
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700895
Jiri Slaby810ab092008-04-30 00:53:41 -0700896 return 0;
897err_free:
898 kfree(brd->ports);
899err:
Jiri Slaby03718232008-04-30 00:53:39 -0700900 return ret;
901}
902
Jiri Slaby810ab092008-04-30 00:53:41 -0700903static void moxa_board_deinit(struct moxa_board_conf *brd)
904{
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700905 unsigned int a, opened;
906
907 mutex_lock(&moxa_openlock);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700908 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700909 brd->ready = 0;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700910 spin_unlock_bh(&moxa_lock);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700911
912 /* pci hot-un-plug support */
913 for (a = 0; a < brd->numPorts; a++)
Alan Coxd450b5a2008-10-13 10:39:58 +0100914 if (brd->ports[a].port.flags & ASYNC_INITIALIZED) {
915 struct tty_struct *tty = tty_port_tty_get(
916 &brd->ports[a].port);
917 if (tty) {
918 tty_hangup(tty);
919 tty_kref_put(tty);
920 }
921 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700922 while (1) {
923 opened = 0;
924 for (a = 0; a < brd->numPorts; a++)
Alan Cox9de6a512008-07-16 21:56:02 +0100925 if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700926 opened++;
927 mutex_unlock(&moxa_openlock);
928 if (!opened)
929 break;
930 msleep(50);
931 mutex_lock(&moxa_openlock);
932 }
933
Jiri Slaby810ab092008-04-30 00:53:41 -0700934 iounmap(brd->basemem);
935 brd->basemem = NULL;
936 kfree(brd->ports);
937}
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939#ifdef CONFIG_PCI
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800940static int __devinit moxa_pci_probe(struct pci_dev *pdev,
941 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800943 struct moxa_board_conf *board;
944 unsigned int i;
945 int board_type = ent->driver_data;
946 int retval;
947
948 retval = pci_enable_device(pdev);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700949 if (retval) {
950 dev_err(&pdev->dev, "can't enable pci device\n");
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800951 goto err;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700952 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800953
954 for (i = 0; i < MAX_BOARDS; i++)
955 if (moxa_boards[i].basemem == NULL)
956 break;
957
958 retval = -ENODEV;
959 if (i >= MAX_BOARDS) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700960 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800961 "found. Board is ignored.\n", MAX_BOARDS);
962 goto err;
963 }
964
965 board = &moxa_boards[i];
Jiri Slabye46a5e32008-04-30 00:53:37 -0700966
967 retval = pci_request_region(pdev, 2, "moxa-base");
968 if (retval) {
969 dev_err(&pdev->dev, "can't request pci region 2\n");
970 goto err;
971 }
972
Alan Cox24cb2332008-04-30 00:54:19 -0700973 board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700974 if (board->basemem == NULL) {
975 dev_err(&pdev->dev, "can't remap io space 2\n");
Jiri Slabye46a5e32008-04-30 00:53:37 -0700976 goto err_reg;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700977 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 board->boardType = board_type;
980 switch (board_type) {
981 case MOXA_BOARD_C218_ISA:
982 case MOXA_BOARD_C218_PCI:
983 board->numPorts = 8;
984 break;
985
986 case MOXA_BOARD_CP204J:
987 board->numPorts = 4;
988 break;
989 default:
990 board->numPorts = 0;
991 break;
992 }
993 board->busType = MOXA_BUS_TYPE_PCI;
Jiri Slabya784bf72007-02-10 01:45:36 -0800994
Jiri Slaby03718232008-04-30 00:53:39 -0700995 retval = moxa_init_board(board, &pdev->dev);
996 if (retval)
997 goto err_base;
998
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800999 pci_set_drvdata(pdev, board);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Jiri Slabybb9f9102008-04-30 00:53:48 -07001001 dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n",
1002 moxa_brdname[board_type - 1], board->numPorts);
1003
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001004 return 0;
Jiri Slaby03718232008-04-30 00:53:39 -07001005err_base:
1006 iounmap(board->basemem);
1007 board->basemem = NULL;
Jiri Slabye46a5e32008-04-30 00:53:37 -07001008err_reg:
1009 pci_release_region(pdev, 2);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001010err:
1011 return retval;
1012}
1013
1014static void __devexit moxa_pci_remove(struct pci_dev *pdev)
1015{
1016 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
1017
Jiri Slaby810ab092008-04-30 00:53:41 -07001018 moxa_board_deinit(brd);
1019
Jiri Slabye46a5e32008-04-30 00:53:37 -07001020 pci_release_region(pdev, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
Jiri Slabya784bf72007-02-10 01:45:36 -08001022
1023static struct pci_driver moxa_pci_driver = {
1024 .name = "moxa",
1025 .id_table = moxa_pcibrds,
1026 .probe = moxa_pci_probe,
1027 .remove = __devexit_p(moxa_pci_remove)
1028};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029#endif /* CONFIG_PCI */
1030
1031static int __init moxa_init(void)
1032{
Jiri Slaby810ab092008-04-30 00:53:41 -07001033 unsigned int isabrds = 0;
Jiri Slabyd353eca2008-04-30 00:53:37 -07001034 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001036 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
1037 MOXA_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
1039 if (!moxaDriver)
1040 return -ENOMEM;
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 moxaDriver->owner = THIS_MODULE;
Sergey Vlasov9b4e3b12005-09-03 16:26:49 +01001043 moxaDriver->name = "ttyMX";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 moxaDriver->major = ttymajor;
1045 moxaDriver->minor_start = 0;
1046 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1047 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1048 moxaDriver->init_termios = tty_std_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Alan Cox606d0992006-12-08 02:38:45 -08001050 moxaDriver->init_termios.c_ispeed = 9600;
1051 moxaDriver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
1053 tty_set_operations(moxaDriver, &moxa_ops);
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (tty_register_driver(moxaDriver)) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001056 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 put_tty_driver(moxaDriver);
1058 return -1;
1059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Jiri Slabyd353eca2008-04-30 00:53:37 -07001061 /* Find the boards defined from module args. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -07001063 {
1064 struct moxa_board_conf *brd = moxa_boards;
Jiri Slaby810ab092008-04-30 00:53:41 -07001065 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 for (i = 0; i < MAX_BOARDS; i++) {
Jiri Slabyd353eca2008-04-30 00:53:37 -07001067 if (!baseaddr[i])
1068 break;
1069 if (type[i] == MOXA_BOARD_C218_ISA ||
1070 type[i] == MOXA_BOARD_C320_ISA) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001071 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001072 isabrds + 1, moxa_brdname[type[i] - 1],
1073 baseaddr[i]);
1074 brd->boardType = type[i];
1075 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1076 numports[i];
1077 brd->busType = MOXA_BUS_TYPE_ISA;
Alan Cox24cb2332008-04-30 00:54:19 -07001078 brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
Jiri Slabyd353eca2008-04-30 00:53:37 -07001079 if (!brd->basemem) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001080 printk(KERN_ERR "MOXA: can't remap %lx\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001081 baseaddr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 continue;
1083 }
Jiri Slaby03718232008-04-30 00:53:39 -07001084 if (moxa_init_board(brd, NULL)) {
1085 iounmap(brd->basemem);
1086 brd->basemem = NULL;
1087 continue;
1088 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001089
Jiri Slabybb9f9102008-04-30 00:53:48 -07001090 printk(KERN_INFO "MOXA isa board found at 0x%.8lu and "
1091 "ready (%u ports, firmware loaded)\n",
1092 baseaddr[i], brd->numPorts);
1093
Jiri Slabyd353eca2008-04-30 00:53:37 -07001094 brd++;
1095 isabrds++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001102 retval = pci_register_driver(&moxa_pci_driver);
1103 if (retval) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001104 printk(KERN_ERR "Can't register MOXA pci driver!\n");
Jiri Slabyd353eca2008-04-30 00:53:37 -07001105 if (isabrds)
Jiri Slabya784bf72007-02-10 01:45:36 -08001106 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
1108#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001109
Jiri Slabya784bf72007-02-10 01:45:36 -08001110 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
1112
1113static void __exit moxa_exit(void)
1114{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001115 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001117#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001118 pci_unregister_driver(&moxa_pci_driver);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001119#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001120
Jiri Slaby810ab092008-04-30 00:53:41 -07001121 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1122 if (moxa_boards[i].ready)
1123 moxa_board_deinit(&moxa_boards[i]);
Jiri Slaby2a541342008-04-30 00:53:45 -07001124
1125 del_timer_sync(&moxaTimer);
1126
1127 if (tty_unregister_driver(moxaDriver))
1128 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1129 "serial driver\n");
1130 put_tty_driver(moxaDriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
1132
1133module_init(moxa_init);
1134module_exit(moxa_exit);
1135
Alan Coxf1761782009-11-30 13:17:51 +00001136static void moxa_shutdown(struct tty_port *port)
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001137{
Alan Coxf1761782009-11-30 13:17:51 +00001138 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1139 MoxaPortDisable(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001140 MoxaPortFlushData(ch, 2);
Alan Coxf1761782009-11-30 13:17:51 +00001141 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001142}
1143
Alan Cox31f35932009-01-02 13:45:05 +00001144static int moxa_carrier_raised(struct tty_port *port)
1145{
1146 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1147 int dcd;
1148
1149 spin_lock_bh(&moxa_lock);
1150 dcd = ch->DCDState;
1151 spin_unlock_bh(&moxa_lock);
1152 return dcd;
1153}
1154
Alan Coxf1761782009-11-30 13:17:51 +00001155static void moxa_dtr_rts(struct tty_port *port, int onoff)
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001156{
Alan Coxf1761782009-11-30 13:17:51 +00001157 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1158 MoxaPortLineCtrl(ch, onoff, onoff);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001159}
1160
Alan Coxf1761782009-11-30 13:17:51 +00001161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162static int moxa_open(struct tty_struct *tty, struct file *filp)
1163{
Jiri Slaby810ab092008-04-30 00:53:41 -07001164 struct moxa_board_conf *brd;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001165 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 int port;
1167 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Jiri Slaby11324ed2007-02-10 01:45:31 -08001169 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (port == MAX_PORTS) {
Jiri Slaby74d7d972008-04-30 00:53:43 -07001171 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001173 if (mutex_lock_interruptible(&moxa_openlock))
1174 return -ERESTARTSYS;
Jiri Slaby810ab092008-04-30 00:53:41 -07001175 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001176 if (!brd->ready) {
1177 mutex_unlock(&moxa_openlock);
Jiri Slaby810ab092008-04-30 00:53:41 -07001178 return -ENODEV;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Dirk Eibachf0e85272009-06-11 14:56:44 +01001181 if (port % MAX_PORTS_PER_BOARD >= brd->numPorts) {
1182 mutex_unlock(&moxa_openlock);
1183 return -ENODEV;
1184 }
1185
Jiri Slaby810ab092008-04-30 00:53:41 -07001186 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
Alan Cox9de6a512008-07-16 21:56:02 +01001187 ch->port.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 tty->driver_data = ch;
Alan Coxd450b5a2008-10-13 10:39:58 +01001189 tty_port_tty_set(&ch->port, tty);
Alan Coxf1761782009-11-30 13:17:51 +00001190 mutex_lock(&ch->port.mutex);
Alan Cox9de6a512008-07-16 21:56:02 +01001191 if (!(ch->port.flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 ch->statusflags = 0;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001193 moxa_set_tty_param(tty, tty->termios);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001194 MoxaPortLineCtrl(ch, 1, 1);
1195 MoxaPortEnable(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001196 MoxaSetFifo(ch, ch->type == PORT_16550A);
Alan Cox9de6a512008-07-16 21:56:02 +01001197 ch->port.flags |= ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
Alan Coxf1761782009-11-30 13:17:51 +00001199 mutex_unlock(&ch->port.mutex);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001200 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Alan Coxf1761782009-11-30 13:17:51 +00001202 retval = tty_port_block_til_ready(&ch->port, tty, filp);
1203 if (retval == 0)
1204 set_bit(ASYNCB_NORMAL_ACTIVE, &ch->port.flags);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001205 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
1207
1208static void moxa_close(struct tty_struct *tty, struct file *filp)
1209{
Alan Coxf1761782009-11-30 13:17:51 +00001210 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 ch->cflag = tty->termios->c_cflag;
Alan Coxf1761782009-11-30 13:17:51 +00001212 tty_port_close(&ch->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
1215static int moxa_write(struct tty_struct *tty,
1216 const unsigned char *buf, int count)
1217{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001218 struct moxa_port *ch = tty->driver_data;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001219 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (ch == NULL)
Jiri Slabyb4173f42008-04-30 00:53:40 -07001222 return 0;
Alan Cox33f0f882006-01-09 20:54:13 -08001223
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001224 spin_lock_bh(&moxa_lock);
Alan Coxd450b5a2008-10-13 10:39:58 +01001225 len = MoxaPortWriteData(tty, buf, count);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001226 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Alan Coxa808ac02009-11-30 13:18:02 +00001228 set_bit(LOWWAIT, &ch->statusflags);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001229 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230}
1231
1232static int moxa_write_room(struct tty_struct *tty)
1233{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001234 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 if (tty->stopped)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001237 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001238 ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (ch == NULL)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001240 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001241 return MoxaPortTxFree(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
1244static void moxa_flush_buffer(struct tty_struct *tty)
1245{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001246 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 if (ch == NULL)
1249 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001250 MoxaPortFlushData(ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 tty_wakeup(tty);
1252}
1253
1254static int moxa_chars_in_buffer(struct tty_struct *tty)
1255{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001256 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Alan Cox978e5952008-04-30 00:53:59 -07001259 lock_kernel();
Jiri Slabyb4173f42008-04-30 00:53:40 -07001260 chars = MoxaPortTxQueue(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (chars) {
1262 /*
1263 * Make it possible to wakeup anything waiting for output
1264 * in tty_ioctl.c, etc.
1265 */
Alan Coxa808ac02009-11-30 13:18:02 +00001266 if (!test_bit(EMPTYWAIT, &ch->statusflags))
Jiri Slaby6f56b652007-10-18 03:06:24 -07001267 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
Alan Cox978e5952008-04-30 00:53:59 -07001269 unlock_kernel();
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001270 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1274{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001275 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 int flag = 0, dtr, rts;
1277
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001278 mutex_lock(&moxa_openlock);
1279 ch = tty->driver_data;
1280 if (!ch) {
1281 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -07001282 return -EINVAL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Jiri Slabyb4173f42008-04-30 00:53:40 -07001285 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (dtr)
1287 flag |= TIOCM_DTR;
1288 if (rts)
1289 flag |= TIOCM_RTS;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001290 dtr = MoxaPortLineStatus(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (dtr & 1)
1292 flag |= TIOCM_CTS;
1293 if (dtr & 2)
1294 flag |= TIOCM_DSR;
1295 if (dtr & 4)
1296 flag |= TIOCM_CD;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001297 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return flag;
1299}
1300
1301static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1302 unsigned int set, unsigned int clear)
1303{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001304 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 int port;
1306 int dtr, rts;
1307
Jiri Slaby11324ed2007-02-10 01:45:31 -08001308 port = tty->index;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001309 mutex_lock(&moxa_openlock);
1310 ch = tty->driver_data;
1311 if (!ch) {
1312 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -07001313 return -EINVAL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Jiri Slabyb4173f42008-04-30 00:53:40 -07001316 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (set & TIOCM_RTS)
1318 rts = 1;
1319 if (set & TIOCM_DTR)
1320 dtr = 1;
1321 if (clear & TIOCM_RTS)
1322 rts = 0;
1323 if (clear & TIOCM_DTR)
1324 dtr = 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001325 MoxaPortLineCtrl(ch, dtr, rts);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001326 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 return 0;
1328}
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330static void moxa_throttle(struct tty_struct *tty)
1331{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001332 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Alan Coxa808ac02009-11-30 13:18:02 +00001334 set_bit(THROTTLE, &ch->statusflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
1337static void moxa_unthrottle(struct tty_struct *tty)
1338{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001339 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Alan Coxa808ac02009-11-30 13:18:02 +00001341 clear_bit(THROTTLE, &ch->statusflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
1343
1344static void moxa_set_termios(struct tty_struct *tty,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001345 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001347 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 if (ch == NULL)
1350 return;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001351 moxa_set_tty_param(tty, old_termios);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001352 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
Alan Cox9de6a512008-07-16 21:56:02 +01001353 wake_up_interruptible(&ch->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
1355
1356static void moxa_stop(struct tty_struct *tty)
1357{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001358 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 if (ch == NULL)
1361 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001362 MoxaPortTxDisable(ch);
Alan Coxa808ac02009-11-30 13:18:02 +00001363 set_bit(TXSTOPPED, &ch->statusflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364}
1365
1366
1367static void moxa_start(struct tty_struct *tty)
1368{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001369 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 if (ch == NULL)
1372 return;
1373
1374 if (!(ch->statusflags & TXSTOPPED))
1375 return;
1376
Jiri Slabyb4173f42008-04-30 00:53:40 -07001377 MoxaPortTxEnable(ch);
Alan Coxa808ac02009-11-30 13:18:02 +00001378 clear_bit(TXSTOPPED, &ch->statusflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
1380
1381static void moxa_hangup(struct tty_struct *tty)
1382{
Alan Coxa808ac02009-11-30 13:18:02 +00001383 struct moxa_port *ch = tty->driver_data;
Alan Coxf1761782009-11-30 13:17:51 +00001384 tty_port_hangup(&ch->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385}
1386
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001387static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1388{
Alan Coxd450b5a2008-10-13 10:39:58 +01001389 struct tty_struct *tty;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001390 dcd = !!dcd;
1391
Alan Coxd450b5a2008-10-13 10:39:58 +01001392 if (dcd != p->DCDState) {
1393 tty = tty_port_tty_get(&p->port);
1394 if (tty && C_CLOCAL(tty) && !dcd)
1395 tty_hangup(tty);
1396 tty_kref_put(tty);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001397 }
1398 p->DCDState = dcd;
1399}
1400
1401static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1402 u16 __iomem *ip)
1403{
Alan Coxd450b5a2008-10-13 10:39:58 +01001404 struct tty_struct *tty = tty_port_tty_get(&p->port);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001405 void __iomem *ofsAddr;
Alan Cox9de6a512008-07-16 21:56:02 +01001406 unsigned int inited = p->port.flags & ASYNC_INITIALIZED;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001407 u16 intr;
1408
1409 if (tty) {
Alan Coxa808ac02009-11-30 13:18:02 +00001410 if (test_bit(EMPTYWAIT, &p->statusflags) &&
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001411 MoxaPortTxQueue(p) == 0) {
Alan Coxa808ac02009-11-30 13:18:02 +00001412 clear_bit(EMPTYWAIT, &p->statusflags);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001413 tty_wakeup(tty);
1414 }
Alan Coxa808ac02009-11-30 13:18:02 +00001415 if (test_bit(LOWWAIT, &p->statusflags) && !tty->stopped &&
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001416 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
Alan Coxa808ac02009-11-30 13:18:02 +00001417 clear_bit(LOWWAIT, &p->statusflags);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001418 tty_wakeup(tty);
1419 }
1420
Alan Coxa808ac02009-11-30 13:18:02 +00001421 if (inited && !test_bit(THROTTLE, &p->statusflags) &&
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001422 MoxaPortRxQueue(p) > 0) { /* RX */
1423 MoxaPortReadData(p);
1424 tty_schedule_flip(tty);
1425 }
1426 } else {
Alan Coxa808ac02009-11-30 13:18:02 +00001427 clear_bit(EMPTYWAIT, &p->statusflags);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001428 MoxaPortFlushData(p, 0); /* flush RX */
1429 }
1430
1431 if (!handle) /* nothing else to do */
Jiri Slaby0e0fd7d2009-04-06 17:34:04 +01001432 goto put;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001433
1434 intr = readw(ip); /* port irq status */
1435 if (intr == 0)
Jiri Slaby0e0fd7d2009-04-06 17:34:04 +01001436 goto put;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001437
1438 writew(0, ip); /* ACK port */
1439 ofsAddr = p->tableAddr;
1440 if (intr & IntrTx) /* disable tx intr */
1441 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1442 ofsAddr + HostStat);
1443
1444 if (!inited)
Jiri Slaby0e0fd7d2009-04-06 17:34:04 +01001445 goto put;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001446
1447 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1448 tty_insert_flip_char(tty, 0, TTY_BREAK);
1449 tty_schedule_flip(tty);
1450 }
1451
1452 if (intr & IntrLine)
1453 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
Jiri Slaby0e0fd7d2009-04-06 17:34:04 +01001454put:
1455 tty_kref_put(tty);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001456
1457 return 0;
1458}
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460static void moxa_poll(unsigned long ignored)
1461{
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001462 struct moxa_board_conf *brd;
1463 u16 __iomem *ip;
Jiri Slaby2a541342008-04-30 00:53:45 -07001464 unsigned int card, port, served = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001466 spin_lock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 for (card = 0; card < MAX_BOARDS; card++) {
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001468 brd = &moxa_boards[card];
1469 if (!brd->ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 continue;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001471
Jiri Slaby2a541342008-04-30 00:53:45 -07001472 served++;
1473
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001474 ip = NULL;
1475 if (readb(brd->intPend) == 0xff)
1476 ip = brd->intTable + readb(brd->intNdx);
1477
1478 for (port = 0; port < brd->numPorts; port++)
1479 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1480
1481 if (ip)
1482 writeb(0, brd->intPend); /* ACK */
1483
1484 if (moxaLowWaterChk) {
1485 struct moxa_port *p = brd->ports;
1486 for (port = 0; port < brd->numPorts; port++, p++)
1487 if (p->lowChkFlag) {
1488 p->lowChkFlag = 0;
1489 moxa_low_water_check(p->tableAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 }
1492 }
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001493 moxaLowWaterChk = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Jiri Slaby2a541342008-04-30 00:53:45 -07001495 if (served)
1496 mod_timer(&moxaTimer, jiffies + HZ / 50);
1497 spin_unlock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
1500/******************************************************************************/
1501
Alan Coxdb1acaa2008-02-08 04:18:43 -08001502static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001504 register struct ktermios *ts = tty->termios;
1505 struct moxa_port *ch = tty->driver_data;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001506 int rts, cts, txflow, rxflow, xany, baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 rts = cts = txflow = rxflow = xany = 0;
1509 if (ts->c_cflag & CRTSCTS)
1510 rts = cts = 1;
1511 if (ts->c_iflag & IXON)
1512 txflow = 1;
1513 if (ts->c_iflag & IXOFF)
1514 rxflow = 1;
1515 if (ts->c_iflag & IXANY)
1516 xany = 1;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001517
1518 /* Clear the features we don't support */
1519 ts->c_cflag &= ~CMSPAR;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001520 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1521 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
Alan Coxdb1acaa2008-02-08 04:18:43 -08001522 if (baud == -1)
1523 baud = tty_termios_baud_rate(old_termios);
1524 /* Not put the baud rate into the termios data */
1525 tty_encode_baud_rate(tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
1527
Jiri Slaby6f56b652007-10-18 03:06:24 -07001528static void moxa_setup_empty_event(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001530 struct moxa_port *ch = tty->driver_data;
Alan Coxa808ac02009-11-30 13:18:02 +00001531 set_bit(EMPTYWAIT, &ch->statusflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534/*****************************************************************************
1535 * Driver level functions: *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Jiri Slabyb4173f42008-04-30 00:53:40 -07001538static void MoxaPortFlushData(struct moxa_port *port, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
1540 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001541 if (mode < 0 || mode > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001543 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 moxafunc(ofsAddr, FC_FlushQueue, mode);
1545 if (mode != 1) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001546 port->lowChkFlag = 0;
Jiri Slaby6f56b652007-10-18 03:06:24 -07001547 moxa_low_water_check(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
1549}
1550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551/*
1552 * Moxa Port Number Description:
1553 *
1554 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1555 * the port number using in MOXA driver functions will be 0 to 31 for
1556 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1557 * to 127 for fourth. For example, if you setup three MOXA boards,
1558 * first board is C218, second board is C320-16 and third board is
1559 * C320-32. The port number of first board (C218 - 8 ports) is from
1560 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1561 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1562 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1563 * 127 will be invalid.
1564 *
1565 *
1566 * Moxa Functions Description:
1567 *
1568 * Function 1: Driver initialization routine, this routine must be
1569 * called when initialized driver.
1570 * Syntax:
1571 * void MoxaDriverInit();
1572 *
1573 *
1574 * Function 2: Moxa driver private IOCTL command processing.
1575 * Syntax:
1576 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1577 *
1578 * unsigned int cmd : IOCTL command
1579 * unsigned long arg : IOCTL argument
1580 * int port : port number (0 - 127)
1581 *
1582 * return: 0 (OK)
1583 * -EINVAL
1584 * -ENOIOCTLCMD
1585 *
1586 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 * Function 6: Enable this port to start Tx/Rx data.
1588 * Syntax:
1589 * void MoxaPortEnable(int port);
1590 * int port : port number (0 - 127)
1591 *
1592 *
1593 * Function 7: Disable this port
1594 * Syntax:
1595 * void MoxaPortDisable(int port);
1596 * int port : port number (0 - 127)
1597 *
1598 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 * Function 10: Setting baud rate of this port.
1600 * Syntax:
Jiri Slaby08d01c72008-04-30 00:53:47 -07001601 * speed_t MoxaPortSetBaud(int port, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 * int port : port number (0 - 127)
1603 * long baud : baud rate (50 - 115200)
1604 *
1605 * return: 0 : this port is invalid or baud < 50
1606 * 50 - 115200 : the real baud rate set to the port, if
1607 * the argument baud is large than maximun
1608 * available baud rate, the real setting
1609 * baud rate will be the maximun baud rate.
1610 *
1611 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 * Function 12: Configure the port.
1613 * Syntax:
Alan Cox606d0992006-12-08 02:38:45 -08001614 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 * int port : port number (0 - 127)
Alan Cox606d0992006-12-08 02:38:45 -08001616 * struct ktermios * termio : termio structure pointer
Alan Coxc7bce302006-09-30 23:27:24 -07001617 * speed_t baud : baud rate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 *
1619 * return: -1 : this port is invalid or termio == NULL
1620 * 0 : setting O.K.
1621 *
1622 *
1623 * Function 13: Get the DTR/RTS state of this port.
1624 * Syntax:
1625 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1626 * int port : port number (0 - 127)
1627 * int * dtrState : pointer to INT to receive the current DTR
1628 * state. (if NULL, this function will not
1629 * write to this address)
1630 * int * rtsState : pointer to INT to receive the current RTS
1631 * state. (if NULL, this function will not
1632 * write to this address)
1633 *
1634 * return: -1 : this port is invalid
1635 * 0 : O.K.
1636 *
1637 *
1638 * Function 14: Setting the DTR/RTS output state of this port.
1639 * Syntax:
1640 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1641 * int port : port number (0 - 127)
1642 * int dtrState : DTR output state (0: off, 1: on)
1643 * int rtsState : RTS output state (0: off, 1: on)
1644 *
1645 *
1646 * Function 15: Setting the flow control of this port.
1647 * Syntax:
1648 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1649 * int txFlow,int xany);
1650 * int port : port number (0 - 127)
1651 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1652 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1653 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1654 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1655 * int xany : S/W XANY flow control (0: no, 1: yes)
1656 *
1657 *
1658 * Function 16: Get ths line status of this port
1659 * Syntax:
1660 * int MoxaPortLineStatus(int port);
1661 * int port : port number (0 - 127)
1662 *
1663 * return: Bit 0 - CTS state (0: off, 1: on)
1664 * Bit 1 - DSR state (0: off, 1: on)
1665 * Bit 2 - DCD state (0: off, 1: on)
1666 *
1667 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 * Function 19: Flush the Rx/Tx buffer data of this port.
1669 * Syntax:
1670 * void MoxaPortFlushData(int port, int mode);
1671 * int port : port number (0 - 127)
1672 * int mode
1673 * 0 : flush the Rx buffer
1674 * 1 : flush the Tx buffer
1675 * 2 : flush the Rx and Tx buffer
1676 *
1677 *
1678 * Function 20: Write data.
1679 * Syntax:
1680 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1681 * int port : port number (0 - 127)
1682 * unsigned char * buffer : pointer to write data buffer.
1683 * int length : write data length
1684 *
1685 * return: 0 - length : real write data length
1686 *
1687 *
1688 * Function 21: Read data.
1689 * Syntax:
Alan Cox33f0f882006-01-09 20:54:13 -08001690 * int MoxaPortReadData(int port, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 * int port : port number (0 - 127)
Alan Cox33f0f882006-01-09 20:54:13 -08001692 * struct tty_struct *tty : tty for data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 *
1694 * return: 0 - length : real read data length
1695 *
1696 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 * Function 24: Get the Tx buffer current queued data bytes
1698 * Syntax:
1699 * int MoxaPortTxQueue(int port);
1700 * int port : port number (0 - 127)
1701 *
1702 * return: .. : Tx buffer current queued data bytes
1703 *
1704 *
1705 * Function 25: Get the Tx buffer current free space
1706 * Syntax:
1707 * int MoxaPortTxFree(int port);
1708 * int port : port number (0 - 127)
1709 *
1710 * return: .. : Tx buffer current free space
1711 *
1712 *
1713 * Function 26: Get the Rx buffer current queued data bytes
1714 * Syntax:
1715 * int MoxaPortRxQueue(int port);
1716 * int port : port number (0 - 127)
1717 *
1718 * return: .. : Rx buffer current queued data bytes
1719 *
1720 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 * Function 28: Disable port data transmission.
1722 * Syntax:
1723 * void MoxaPortTxDisable(int port);
1724 * int port : port number (0 - 127)
1725 *
1726 *
1727 * Function 29: Enable port data transmission.
1728 * Syntax:
1729 * void MoxaPortTxEnable(int port);
1730 * int port : port number (0 - 127)
1731 *
1732 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 * Function 31: Get the received BREAK signal count and reset it.
1734 * Syntax:
1735 * int MoxaPortResetBrkCnt(int port);
1736 * int port : port number (0 - 127)
1737 *
1738 * return: 0 - .. : BREAK signal count
1739 *
1740 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Jiri Slabyb4173f42008-04-30 00:53:40 -07001743static void MoxaPortEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
1745 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001746 u16 lowwater = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Jiri Slabyb4173f42008-04-30 00:53:40 -07001748 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 writew(lowwater, ofsAddr + Low_water);
Jiri Slaby08d01c72008-04-30 00:53:47 -07001750 if (MOXA_IS_320(port->board))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001752 else
1753 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1754 ofsAddr + HostStat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1757 moxafunc(ofsAddr, FC_FlushQueue, 2);
1758
1759 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1760 MoxaPortLineStatus(port);
1761}
1762
Jiri Slabyb4173f42008-04-30 00:53:40 -07001763static void MoxaPortDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001765 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1768 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1769 writew(0, ofsAddr + HostStat);
1770 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1771}
1772
Jiri Slaby08d01c72008-04-30 00:53:47 -07001773static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
Jiri Slaby08d01c72008-04-30 00:53:47 -07001775 void __iomem *ofsAddr = port->tableAddr;
1776 unsigned int clock, val;
1777 speed_t max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Jiri Slaby08d01c72008-04-30 00:53:47 -07001779 max = MOXA_IS_320(port->board) ? 460800 : 921600;
1780 if (baud < 50)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001781 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (baud > max)
1783 baud = max;
Jiri Slaby08d01c72008-04-30 00:53:47 -07001784 clock = 921600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 val = clock / baud;
1786 moxafunc(ofsAddr, FC_SetBaud, val);
1787 baud = clock / val;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001788 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
1790
Jiri Slabyb4173f42008-04-30 00:53:40 -07001791static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1792 speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
1794 void __iomem *ofsAddr;
1795 tcflag_t cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 tcflag_t mode = 0;
1797
Jiri Slabyb4173f42008-04-30 00:53:40 -07001798 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 cflag = termio->c_cflag; /* termio->c_cflag */
1800
1801 mode = termio->c_cflag & CSIZE;
1802 if (mode == CS5)
1803 mode = MX_CS5;
1804 else if (mode == CS6)
1805 mode = MX_CS6;
1806 else if (mode == CS7)
1807 mode = MX_CS7;
1808 else if (mode == CS8)
1809 mode = MX_CS8;
1810
1811 if (termio->c_cflag & CSTOPB) {
1812 if (mode == MX_CS5)
1813 mode |= MX_STOP15;
1814 else
1815 mode |= MX_STOP2;
1816 } else
1817 mode |= MX_STOP1;
1818
1819 if (termio->c_cflag & PARENB) {
1820 if (termio->c_cflag & PARODD)
1821 mode |= MX_PARODD;
1822 else
1823 mode |= MX_PAREVEN;
1824 } else
1825 mode |= MX_PARNONE;
1826
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001827 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Jiri Slaby08d01c72008-04-30 00:53:47 -07001829 if (MOXA_IS_320(port->board) && baud >= 921600)
1830 return -1;
1831
Alan Coxdb1acaa2008-02-08 04:18:43 -08001832 baud = MoxaPortSetBaud(port, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
1834 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
Alan Coxf5c5a362009-11-30 13:17:57 +00001835 spin_lock_irq(&moxafunc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1837 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1838 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
Jiri Slaby6f56b652007-10-18 03:06:24 -07001839 moxa_wait_finish(ofsAddr);
Alan Coxa808ac02009-11-30 13:18:02 +00001840 spin_unlock_irq(&moxafunc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 }
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001843 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
1845
Jiri Slabyb4173f42008-04-30 00:53:40 -07001846static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1847 int *rtsState)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001849 if (dtrState)
1850 *dtrState = !!(port->lineCtrl & DTR_ON);
1851 if (rtsState)
1852 *rtsState = !!(port->lineCtrl & RTS_ON);
1853
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001854 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855}
1856
Jiri Slabyb4173f42008-04-30 00:53:40 -07001857static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001859 u8 mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (dtr)
1862 mode |= DTR_ON;
1863 if (rts)
1864 mode |= RTS_ON;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001865 port->lineCtrl = mode;
1866 moxafunc(port->tableAddr, FC_LineControl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}
1868
Jiri Slabyb4173f42008-04-30 00:53:40 -07001869static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1870 int txflow, int rxflow, int txany)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001872 int mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (rts)
1875 mode |= RTS_FlowCtl;
1876 if (cts)
1877 mode |= CTS_FlowCtl;
1878 if (txflow)
1879 mode |= Tx_FlowCtl;
1880 if (rxflow)
1881 mode |= Rx_FlowCtl;
1882 if (txany)
1883 mode |= IXM_IXANY;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001884 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
1886
Jiri Slabyb4173f42008-04-30 00:53:40 -07001887static int MoxaPortLineStatus(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
1889 void __iomem *ofsAddr;
1890 int val;
1891
Jiri Slabyb4173f42008-04-30 00:53:40 -07001892 ofsAddr = port->tableAddr;
Alan Coxf5c5a362009-11-30 13:17:57 +00001893 if (MOXA_IS_320(port->board))
1894 val = moxafuncret(ofsAddr, FC_LineStatus, 0);
1895 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 val = readw(ofsAddr + FlagStat) >> 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 val &= 0x0B;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001898 if (val & 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 val |= 4;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001900 spin_lock_bh(&moxa_lock);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001901 moxa_new_dcdstate(port, val & 8);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001902 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 val &= 7;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001904 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905}
1906
Alan Coxd450b5a2008-10-13 10:39:58 +01001907static int MoxaPortWriteData(struct tty_struct *tty,
Jiri Slaby2108eba2008-04-30 00:53:44 -07001908 const unsigned char *buffer, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
Alan Coxd450b5a2008-10-13 10:39:58 +01001910 struct moxa_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001912 unsigned int c, total;
1913 u16 head, tail, tx_mask, spage, epage;
1914 u16 pageno, pageofs, bufhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Jiri Slabyb4173f42008-04-30 00:53:40 -07001916 ofsAddr = port->tableAddr;
1917 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 tx_mask = readw(ofsAddr + TX_mask);
1919 spage = readw(ofsAddr + Page_txb);
1920 epage = readw(ofsAddr + EndPage_txb);
1921 tail = readw(ofsAddr + TXwptr);
1922 head = readw(ofsAddr + TXrptr);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001923 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 if (c > len)
1925 c = len;
Alan Cox9de6a512008-07-16 21:56:02 +01001926 moxaLog.txcnt[port->port.tty->index] += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 total = c;
1928 if (spage == epage) {
1929 bufhead = readw(ofsAddr + Ofs_txb);
1930 writew(spage, baseAddr + Control_reg);
1931 while (c > 0) {
1932 if (head > tail)
1933 len = head - tail - 1;
1934 else
1935 len = tx_mask + 1 - tail;
1936 len = (c > len) ? len : c;
1937 ofs = baseAddr + DynPage_addr + bufhead + tail;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001938 memcpy_toio(ofs, buffer, len);
1939 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 tail = (tail + len) & tx_mask;
1941 c -= len;
1942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 pageno = spage + (tail >> 13);
1945 pageofs = tail & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001946 while (c > 0) {
1947 len = Page_size - pageofs;
1948 if (len > c)
1949 len = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 writeb(pageno, baseAddr + Control_reg);
1951 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001952 memcpy_toio(ofs, buffer, len);
1953 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 if (++pageno == epage)
1955 pageno = spage;
1956 pageofs = 0;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001957 c -= len;
1958 }
1959 tail = (tail + total) & tx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07001961 writew(tail, ofsAddr + TXwptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 writeb(1, ofsAddr + CD180TXirq); /* start to send */
Jiri Slaby2108eba2008-04-30 00:53:44 -07001963 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
1965
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001966static int MoxaPortReadData(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
Alan Cox9de6a512008-07-16 21:56:02 +01001968 struct tty_struct *tty = port->port.tty;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001969 unsigned char *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001971 unsigned int count, len, total;
1972 u16 tail, rx_mask, spage, epage;
1973 u16 pageno, pageofs, bufhead, head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Jiri Slabyb4173f42008-04-30 00:53:40 -07001975 ofsAddr = port->tableAddr;
1976 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 head = readw(ofsAddr + RXrptr);
1978 tail = readw(ofsAddr + RXwptr);
1979 rx_mask = readw(ofsAddr + RX_mask);
1980 spage = readw(ofsAddr + Page_rxb);
1981 epage = readw(ofsAddr + EndPage_rxb);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001982 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 if (count == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08001984 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Alan Cox33f0f882006-01-09 20:54:13 -08001986 total = count;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001987 moxaLog.rxcnt[tty->index] += total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 if (spage == epage) {
1989 bufhead = readw(ofsAddr + Ofs_rxb);
1990 writew(spage, baseAddr + Control_reg);
1991 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 ofs = baseAddr + DynPage_addr + bufhead + head;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001993 len = (tail >= head) ? (tail - head) :
1994 (rx_mask + 1 - head);
1995 len = tty_prepare_flip_string(tty, &dst,
1996 min(len, count));
1997 memcpy_fromio(dst, ofs, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 head = (head + len) & rx_mask;
1999 count -= len;
2000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 pageno = spage + (head >> 13);
2003 pageofs = head & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002004 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 writew(pageno, baseAddr + Control_reg);
2006 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002007 len = tty_prepare_flip_string(tty, &dst,
2008 min(Page_size - pageofs, count));
2009 memcpy_fromio(dst, ofs, len);
2010
2011 count -= len;
2012 pageofs = (pageofs + len) & Page_mask;
2013 if (pageofs == 0 && ++pageno == epage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 pageno = spage;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002015 }
2016 head = (head + total) & rx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002018 writew(head, ofsAddr + RXrptr);
2019 if (readb(ofsAddr + FlagStat) & Xoff_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 moxaLowWaterChk = 1;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002021 port->lowChkFlag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002023 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
2025
2026
Jiri Slabyb4173f42008-04-30 00:53:40 -07002027static int MoxaPortTxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002029 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002030 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 rptr = readw(ofsAddr + TXrptr);
2033 wptr = readw(ofsAddr + TXwptr);
2034 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002035 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036}
2037
Jiri Slabyb4173f42008-04-30 00:53:40 -07002038static int MoxaPortTxFree(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002040 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002041 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 rptr = readw(ofsAddr + TXrptr);
2044 wptr = readw(ofsAddr + TXwptr);
2045 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002046 return mask - ((wptr - rptr) & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047}
2048
Jiri Slabyb4173f42008-04-30 00:53:40 -07002049static int MoxaPortRxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002051 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002052 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 rptr = readw(ofsAddr + RXrptr);
2055 wptr = readw(ofsAddr + RXwptr);
2056 mask = readw(ofsAddr + RX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002057 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058}
2059
Jiri Slabyb4173f42008-04-30 00:53:40 -07002060static void MoxaPortTxDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002062 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
2064
Jiri Slabyb4173f42008-04-30 00:53:40 -07002065static void MoxaPortTxEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002067 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068}
2069
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002070static int moxa_get_serial_info(struct moxa_port *info,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002071 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002073 struct serial_struct tmp = {
2074 .type = info->type,
Alan Cox9de6a512008-07-16 21:56:02 +01002075 .line = info->port.tty->index,
2076 .flags = info->port.flags,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002077 .baud_base = 921600,
Alan Cox44b7d1b2008-07-16 21:57:18 +01002078 .close_delay = info->port.close_delay
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002079 };
2080 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081}
2082
2083
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002084static int moxa_set_serial_info(struct moxa_port *info,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002085 struct serial_struct __user *new_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
2087 struct serial_struct new_serial;
2088
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002089 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 return -EFAULT;
2091
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002092 if (new_serial.irq != 0 || new_serial.port != 0 ||
2093 new_serial.custom_divisor != 0 ||
2094 new_serial.baud_base != 921600)
2095 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 if (!capable(CAP_SYS_ADMIN)) {
2098 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
Alan Cox9de6a512008-07-16 21:56:02 +01002099 (info->port.flags & ~ASYNC_USR_MASK)))
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002100 return -EPERM;
2101 } else
Alan Cox44b7d1b2008-07-16 21:57:18 +01002102 info->port.close_delay = new_serial.close_delay * HZ / 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
Alan Cox9de6a512008-07-16 21:56:02 +01002105 new_serial.flags |= (info->port.flags & ASYNC_FLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002107 MoxaSetFifo(info, new_serial.type == PORT_16550A);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
2109 info->type = new_serial.type;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002110 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
2112
2113
2114
2115/*****************************************************************************
2116 * Static local functions: *
2117 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Jiri Slabyb4173f42008-04-30 00:53:40 -07002119static void MoxaSetFifo(struct moxa_port *port, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002121 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
2123 if (!enable) {
2124 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2125 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2126 } else {
2127 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2128 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2129 }
2130}