blob: 2bba250ffc8e03f5e357bf42731307104539b258 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*****************************************************************************/
2/*
3 * moxa.c -- MOXA Intellio family multiport serial driver.
4 *
Jiri Slabyb9705b62008-04-30 00:53:48 -07005 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com).
6 * Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This code is loosely based on the Linux serial driver, written by
9 * Linus Torvalds, Theodore T'so and others.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
17/*
18 * MOXA Intellio Series Driver
19 * for : LINUX
20 * date : 1999/1/7
21 * version : 5.1
22 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <linux/types.h>
26#include <linux/mm.h>
27#include <linux/ioport.h>
28#include <linux/errno.h>
Jiri Slaby03718232008-04-30 00:53:39 -070029#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/signal.h>
31#include <linux/sched.h>
32#include <linux/timer.h>
33#include <linux/interrupt.h>
34#include <linux/tty.h>
35#include <linux/tty_flip.h>
36#include <linux/major.h>
37#include <linux/string.h>
38#include <linux/fcntl.h>
39#include <linux/ptrace.h>
40#include <linux/serial.h>
41#include <linux/tty_driver.h>
42#include <linux/delay.h>
43#include <linux/pci.h>
44#include <linux/init.h>
45#include <linux/bitops.h>
46
47#include <asm/system.h>
48#include <asm/io.h>
49#include <asm/uaccess.h>
50
Jiri Slaby03718232008-04-30 00:53:39 -070051#include "moxa.h"
52
Jiri Slabyb9705b62008-04-30 00:53:48 -070053#define MOXA_VERSION "6.0k"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Jiri Slaby03718232008-04-30 00:53:39 -070055#define MOXA_FW_HDRLEN 32
56
Jiri Slaby11324ed2007-02-10 01:45:31 -080057#define MOXAMAJOR 172
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Jiri Slaby11324ed2007-02-10 01:45:31 -080059#define MAX_BOARDS 4 /* Don't change this value */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
Jiri Slaby11324ed2007-02-10 01:45:31 -080061#define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Jiri Slaby08d01c792008-04-30 00:53:47 -070063#define MOXA_IS_320(brd) ((brd)->boardType == MOXA_BOARD_C320_ISA || \
64 (brd)->boardType == MOXA_BOARD_C320_PCI)
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/*
67 * Define the Moxa PCI vendor and device IDs.
68 */
Jiri Slaby11324ed2007-02-10 01:45:31 -080069#define MOXA_BUS_TYPE_ISA 0
70#define MOXA_BUS_TYPE_PCI 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072enum {
73 MOXA_BOARD_C218_PCI = 1,
74 MOXA_BOARD_C218_ISA,
75 MOXA_BOARD_C320_PCI,
76 MOXA_BOARD_C320_ISA,
77 MOXA_BOARD_CP204J,
78};
79
80static char *moxa_brdname[] =
81{
82 "C218 Turbo PCI series",
83 "C218 Turbo ISA series",
84 "C320 Turbo PCI series",
85 "C320 Turbo ISA series",
86 "CP-204J series",
87};
88
89#ifdef CONFIG_PCI
90static struct pci_device_id moxa_pcibrds[] = {
Jiri Slaby5ebb4072007-02-10 01:45:30 -080091 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
92 .driver_data = MOXA_BOARD_C218_PCI },
93 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
94 .driver_data = MOXA_BOARD_C320_PCI },
95 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
96 .driver_data = MOXA_BOARD_CP204J },
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 { 0 }
98};
99MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
100#endif /* CONFIG_PCI */
101
Jiri Slaby03718232008-04-30 00:53:39 -0700102struct moxa_port;
103
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800104static struct moxa_board_conf {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 int boardType;
106 int numPorts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 int busType;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800108
Jiri Slaby810ab092008-04-30 00:53:41 -0700109 unsigned int ready;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800110
Jiri Slaby03718232008-04-30 00:53:39 -0700111 struct moxa_port *ports;
112
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800113 void __iomem *basemem;
114 void __iomem *intNdx;
115 void __iomem *intPend;
116 void __iomem *intTable;
117} moxa_boards[MAX_BOARDS];
118
119struct mxser_mstatus {
120 tcflag_t cflag;
121 int cts;
122 int dsr;
123 int ri;
124 int dcd;
Jiri Slaby9dff89c2007-02-10 01:45:30 -0800125};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800127struct moxaq_str {
128 int inq;
129 int outq;
130};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800132struct moxa_port {
Alan Cox9de6a512008-07-16 21:56:02 +0100133 struct tty_port port;
Jiri Slabyb4173f42008-04-30 00:53:40 -0700134 struct moxa_board_conf *board;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700135 void __iomem *tableAddr;
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 int cflag;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700139 unsigned long statusflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700141 u8 DCDState;
142 u8 lineCtrl;
143 u8 lowChkFlag;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800144};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Jiri Slaby74d7d972008-04-30 00:53:43 -0700146struct mon_str {
147 int tick;
148 int rxcnt[MAX_PORTS];
149 int txcnt[MAX_PORTS];
150};
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* statusflags */
153#define TXSTOPPED 0x1
154#define LOWWAIT 0x2
155#define EMPTYWAIT 0x4
156#define THROTTLE 0x8
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158#define SERIAL_DO_RESTART
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#define WAKEUP_CHARS 256
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162static int ttymajor = MOXAMAJOR;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700163static struct mon_str moxaLog;
164static unsigned int moxaFuncTout = HZ / 2;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700165static unsigned int moxaLowWaterChk;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700166static DEFINE_MUTEX(moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/* Variables for insmod */
168#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700169static unsigned long baseaddr[MAX_BOARDS];
170static unsigned int type[MAX_BOARDS];
171static unsigned int numports[MAX_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172#endif
173
174MODULE_AUTHOR("William Chen");
175MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
176MODULE_LICENSE("GPL");
177#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700178module_param_array(type, uint, NULL, 0);
179MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
180module_param_array(baseaddr, ulong, NULL, 0);
181MODULE_PARM_DESC(baseaddr, "base address");
182module_param_array(numports, uint, NULL, 0);
183MODULE_PARM_DESC(numports, "numports (ignored for C218)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184#endif
185module_param(ttymajor, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/*
188 * static functions:
189 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190static int moxa_open(struct tty_struct *, struct file *);
191static void moxa_close(struct tty_struct *, struct file *);
192static int moxa_write(struct tty_struct *, const unsigned char *, int);
193static int moxa_write_room(struct tty_struct *);
194static void moxa_flush_buffer(struct tty_struct *);
195static int moxa_chars_in_buffer(struct tty_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static void moxa_throttle(struct tty_struct *);
197static void moxa_unthrottle(struct tty_struct *);
Alan Cox606d0992006-12-08 02:38:45 -0800198static void moxa_set_termios(struct tty_struct *, struct ktermios *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static void moxa_stop(struct tty_struct *);
200static void moxa_start(struct tty_struct *);
201static void moxa_hangup(struct tty_struct *);
202static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
203static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
204 unsigned int set, unsigned int clear);
205static void moxa_poll(unsigned long);
Alan Coxdb1acaa2008-02-08 04:18:43 -0800206static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
Jiri Slaby6f56b6582007-10-18 03:06:24 -0700207static void moxa_setup_empty_event(struct tty_struct *);
Jiri Slaby6f56b6582007-10-18 03:06:24 -0700208static void moxa_shut_down(struct moxa_port *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/*
210 * moxa board interface functions:
211 */
Jiri Slabyb4173f42008-04-30 00:53:40 -0700212static void MoxaPortEnable(struct moxa_port *);
213static void MoxaPortDisable(struct moxa_port *);
214static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
215static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
216static void MoxaPortLineCtrl(struct moxa_port *, int, int);
217static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
218static int MoxaPortLineStatus(struct moxa_port *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700219static void MoxaPortFlushData(struct moxa_port *, int);
Jiri Slaby2108eba2008-04-30 00:53:44 -0700220static int MoxaPortWriteData(struct moxa_port *, const unsigned char *, int);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700221static int MoxaPortReadData(struct moxa_port *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700222static int MoxaPortTxQueue(struct moxa_port *);
223static int MoxaPortRxQueue(struct moxa_port *);
224static int MoxaPortTxFree(struct moxa_port *);
225static void MoxaPortTxDisable(struct moxa_port *);
226static void MoxaPortTxEnable(struct moxa_port *);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800227static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
228static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700229static void MoxaSetFifo(struct moxa_port *port, int enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Jiri Slaby74d7d972008-04-30 00:53:43 -0700231/*
232 * I/O functions
233 */
234
235static void moxa_wait_finish(void __iomem *ofsAddr)
236{
237 unsigned long end = jiffies + moxaFuncTout;
238
239 while (readw(ofsAddr + FuncCode) != 0)
240 if (time_after(jiffies, end))
241 return;
242 if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
243 printk(KERN_WARNING "moxa function expired\n");
244}
245
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700246static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
Jiri Slaby74d7d972008-04-30 00:53:43 -0700247{
248 writew(arg, ofsAddr + FuncArg);
249 writew(cmd, ofsAddr + FuncCode);
250 moxa_wait_finish(ofsAddr);
251}
252
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700253static void moxa_low_water_check(void __iomem *ofsAddr)
254{
255 u16 rptr, wptr, mask, len;
256
257 if (readb(ofsAddr + FlagStat) & Xoff_state) {
258 rptr = readw(ofsAddr + RXrptr);
259 wptr = readw(ofsAddr + RXwptr);
260 mask = readw(ofsAddr + RX_mask);
261 len = (wptr - rptr) & mask;
262 if (len <= Low_water)
263 moxafunc(ofsAddr, FC_SendXon, 0);
264 }
265}
266
Jiri Slaby74d7d972008-04-30 00:53:43 -0700267/*
268 * TTY operations
269 */
270
271static int moxa_ioctl(struct tty_struct *tty, struct file *file,
272 unsigned int cmd, unsigned long arg)
273{
274 struct moxa_port *ch = tty->driver_data;
275 void __user *argp = (void __user *)arg;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700276 int status, ret = 0;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700277
278 if (tty->index == MAX_PORTS) {
279 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
280 cmd != MOXA_GETMSTATUS)
281 return -EINVAL;
282 } else if (!ch)
283 return -ENODEV;
284
285 switch (cmd) {
286 case MOXA_GETDATACOUNT:
287 moxaLog.tick = jiffies;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700288 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
289 ret = -EFAULT;
290 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700291 case MOXA_FLUSH_QUEUE:
292 MoxaPortFlushData(ch, arg);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700293 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700294 case MOXA_GET_IOQUEUE: {
295 struct moxaq_str __user *argm = argp;
296 struct moxaq_str tmp;
297 struct moxa_port *p;
298 unsigned int i, j;
299
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700300 mutex_lock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700301 for (i = 0; i < MAX_BOARDS; i++) {
302 p = moxa_boards[i].ports;
303 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
304 memset(&tmp, 0, sizeof(tmp));
305 if (moxa_boards[i].ready) {
306 tmp.inq = MoxaPortRxQueue(p);
307 tmp.outq = MoxaPortTxQueue(p);
308 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700309 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
310 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700311 return -EFAULT;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700312 }
Jiri Slaby74d7d972008-04-30 00:53:43 -0700313 }
314 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700315 mutex_unlock(&moxa_openlock);
316 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700317 } case MOXA_GET_OQUEUE:
318 status = MoxaPortTxQueue(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700319 ret = put_user(status, (unsigned long __user *)argp);
320 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700321 case MOXA_GET_IQUEUE:
322 status = MoxaPortRxQueue(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700323 ret = put_user(status, (unsigned long __user *)argp);
324 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700325 case MOXA_GETMSTATUS: {
326 struct mxser_mstatus __user *argm = argp;
327 struct mxser_mstatus tmp;
328 struct moxa_port *p;
329 unsigned int i, j;
330
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700331 mutex_lock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700332 for (i = 0; i < MAX_BOARDS; i++) {
333 p = moxa_boards[i].ports;
334 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
335 memset(&tmp, 0, sizeof(tmp));
336 if (!moxa_boards[i].ready)
337 goto copy;
338
339 status = MoxaPortLineStatus(p);
340 if (status & 1)
341 tmp.cts = 1;
342 if (status & 2)
343 tmp.dsr = 1;
344 if (status & 4)
345 tmp.dcd = 1;
346
Alan Cox9de6a512008-07-16 21:56:02 +0100347 if (!p->port.tty || !p->port.tty->termios)
Jiri Slaby74d7d972008-04-30 00:53:43 -0700348 tmp.cflag = p->cflag;
349 else
Alan Cox9de6a512008-07-16 21:56:02 +0100350 tmp.cflag = p->port.tty->termios->c_cflag;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700351copy:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700352 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
353 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700354 return -EFAULT;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700355 }
Jiri Slaby74d7d972008-04-30 00:53:43 -0700356 }
357 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700358 mutex_unlock(&moxa_openlock);
359 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700360 }
361 case TIOCGSERIAL:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700362 mutex_lock(&moxa_openlock);
363 ret = moxa_get_serial_info(ch, argp);
364 mutex_unlock(&moxa_openlock);
365 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700366 case TIOCSSERIAL:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700367 mutex_lock(&moxa_openlock);
368 ret = moxa_set_serial_info(ch, argp);
369 mutex_unlock(&moxa_openlock);
370 break;
371 default:
372 ret = -ENOIOCTLCMD;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700373 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700374 return ret;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700375}
376
377static void moxa_break_ctl(struct tty_struct *tty, int state)
378{
379 struct moxa_port *port = tty->driver_data;
380
381 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
382 Magic_code);
383}
384
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700385static const struct tty_operations moxa_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 .open = moxa_open,
387 .close = moxa_close,
388 .write = moxa_write,
389 .write_room = moxa_write_room,
390 .flush_buffer = moxa_flush_buffer,
391 .chars_in_buffer = moxa_chars_in_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 .ioctl = moxa_ioctl,
393 .throttle = moxa_throttle,
394 .unthrottle = moxa_unthrottle,
395 .set_termios = moxa_set_termios,
396 .stop = moxa_stop,
397 .start = moxa_start,
398 .hangup = moxa_hangup,
Jiri Slaby74d7d972008-04-30 00:53:43 -0700399 .break_ctl = moxa_break_ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 .tiocmget = moxa_tiocmget,
401 .tiocmset = moxa_tiocmset,
402};
403
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800404static struct tty_driver *moxaDriver;
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800405static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
Ingo Molnar34af9462006-06-27 02:53:55 -0700406static DEFINE_SPINLOCK(moxa_lock);
Alan Cox33f0f882006-01-09 20:54:13 -0800407
Jiri Slaby74d7d972008-04-30 00:53:43 -0700408/*
409 * HW init
410 */
411
Jiri Slaby03718232008-04-30 00:53:39 -0700412static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
413{
414 switch (brd->boardType) {
415 case MOXA_BOARD_C218_ISA:
416 case MOXA_BOARD_C218_PCI:
417 if (model != 1)
418 goto err;
419 break;
420 case MOXA_BOARD_CP204J:
421 if (model != 3)
422 goto err;
423 break;
424 default:
425 if (model != 2)
426 goto err;
427 break;
428 }
429 return 0;
430err:
431 return -EINVAL;
432}
433
434static int moxa_check_fw(const void *ptr)
435{
436 const __le16 *lptr = ptr;
437
438 if (*lptr != cpu_to_le16(0x7980))
439 return -EINVAL;
440
441 return 0;
442}
443
444static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
445 size_t len)
446{
447 void __iomem *baseAddr = brd->basemem;
448 u16 tmp;
449
450 writeb(HW_reset, baseAddr + Control_reg); /* reset */
451 msleep(10);
452 memset_io(baseAddr, 0, 4096);
453 memcpy_toio(baseAddr, buf, len); /* download BIOS */
454 writeb(0, baseAddr + Control_reg); /* restart */
455
456 msleep(2000);
457
458 switch (brd->boardType) {
459 case MOXA_BOARD_C218_ISA:
460 case MOXA_BOARD_C218_PCI:
461 tmp = readw(baseAddr + C218_key);
462 if (tmp != C218_KeyCode)
463 goto err;
464 break;
465 case MOXA_BOARD_CP204J:
466 tmp = readw(baseAddr + C218_key);
467 if (tmp != CP204J_KeyCode)
468 goto err;
469 break;
470 default:
471 tmp = readw(baseAddr + C320_key);
472 if (tmp != C320_KeyCode)
473 goto err;
474 tmp = readw(baseAddr + C320_status);
475 if (tmp != STS_init) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700476 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
Jiri Slaby03718232008-04-30 00:53:39 -0700477 "module not found\n");
478 return -EIO;
479 }
480 break;
481 }
482
483 return 0;
484err:
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700485 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700486 return -EIO;
487}
488
489static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
490 size_t len)
491{
492 void __iomem *baseAddr = brd->basemem;
493
494 if (len < 7168) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700495 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700496 return -EINVAL;
497 }
498
499 writew(len - 7168 - 2, baseAddr + C320bapi_len);
500 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
501 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
502 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
503 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
504
505 return 0;
506}
507
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700508static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
Jiri Slaby03718232008-04-30 00:53:39 -0700509 size_t len)
510{
511 void __iomem *baseAddr = brd->basemem;
512 const u16 *uptr = ptr;
513 size_t wlen, len2, j;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700514 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
Jiri Slaby08d01c792008-04-30 00:53:47 -0700515 unsigned int i, retry;
Jiri Slaby03718232008-04-30 00:53:39 -0700516 u16 usum, keycode;
517
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700518 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
519 C218_KeyCode;
520
521 switch (brd->boardType) {
522 case MOXA_BOARD_CP204J:
523 case MOXA_BOARD_C218_ISA:
524 case MOXA_BOARD_C218_PCI:
525 key = C218_key;
526 loadbuf = C218_LoadBuf;
527 loadlen = C218DLoad_len;
528 checksum = C218check_sum;
529 checksum_ok = C218chksum_ok;
530 break;
531 default:
532 key = C320_key;
533 keycode = C320_KeyCode;
534 loadbuf = C320_LoadBuf;
535 loadlen = C320DLoad_len;
536 checksum = C320check_sum;
537 checksum_ok = C320chksum_ok;
538 break;
539 }
540
Jiri Slaby03718232008-04-30 00:53:39 -0700541 usum = 0;
542 wlen = len >> 1;
543 for (i = 0; i < wlen; i++)
544 usum += le16_to_cpu(uptr[i]);
545 retry = 0;
546 do {
547 wlen = len >> 1;
548 j = 0;
549 while (wlen) {
550 len2 = (wlen > 2048) ? 2048 : wlen;
551 wlen -= len2;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700552 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
Jiri Slaby03718232008-04-30 00:53:39 -0700553 j += len2 << 1;
554
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700555 writew(len2, baseAddr + loadlen);
556 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700557 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700558 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700559 break;
560 msleep(10);
561 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700562 if (readw(baseAddr + key) != keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700563 return -EIO;
564 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700565 writew(0, baseAddr + loadlen);
566 writew(usum, baseAddr + checksum);
567 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700568 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700569 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700570 break;
571 msleep(10);
572 }
573 retry++;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700574 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
575 if (readb(baseAddr + checksum_ok) != 1)
Jiri Slaby03718232008-04-30 00:53:39 -0700576 return -EIO;
577
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700578 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700579 for (i = 0; i < 600; i++) {
580 if (readw(baseAddr + Magic_no) == Magic_code)
581 break;
582 msleep(10);
583 }
584 if (readw(baseAddr + Magic_no) != Magic_code)
585 return -EIO;
586
Jiri Slaby08d01c792008-04-30 00:53:47 -0700587 if (MOXA_IS_320(brd)) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700588 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
589 writew(0x3800, baseAddr + TMS320_PORT1);
590 writew(0x3900, baseAddr + TMS320_PORT2);
591 writew(28499, baseAddr + TMS320_CLOCK);
592 } else {
593 writew(0x3200, baseAddr + TMS320_PORT1);
594 writew(0x3400, baseAddr + TMS320_PORT2);
595 writew(19999, baseAddr + TMS320_CLOCK);
596 }
Jiri Slaby03718232008-04-30 00:53:39 -0700597 }
598 writew(1, baseAddr + Disable_IRQ);
599 writew(0, baseAddr + Magic_no);
600 for (i = 0; i < 500; i++) {
601 if (readw(baseAddr + Magic_no) == Magic_code)
602 break;
603 msleep(10);
604 }
605 if (readw(baseAddr + Magic_no) != Magic_code)
606 return -EIO;
607
Jiri Slaby08d01c792008-04-30 00:53:47 -0700608 if (MOXA_IS_320(brd)) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700609 j = readw(baseAddr + Module_cnt);
610 if (j <= 0)
611 return -EIO;
612 brd->numPorts = j * 8;
613 writew(j, baseAddr + Module_no);
614 writew(0, baseAddr + Magic_no);
615 for (i = 0; i < 600; i++) {
616 if (readw(baseAddr + Magic_no) == Magic_code)
617 break;
618 msleep(10);
619 }
620 if (readw(baseAddr + Magic_no) != Magic_code)
621 return -EIO;
Jiri Slaby03718232008-04-30 00:53:39 -0700622 }
Jiri Slaby03718232008-04-30 00:53:39 -0700623 brd->intNdx = baseAddr + IRQindex;
624 brd->intPend = baseAddr + IRQpending;
625 brd->intTable = baseAddr + IRQtable;
626
627 return 0;
628}
629
630static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
631 size_t len)
632{
633 void __iomem *ofsAddr, *baseAddr = brd->basemem;
634 struct moxa_port *port;
635 int retval, i;
636
637 if (len % 2) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700638 printk(KERN_ERR "MOXA: bios length is not even\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700639 return -EINVAL;
640 }
641
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700642 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
643 if (retval)
644 return retval;
645
Jiri Slaby03718232008-04-30 00:53:39 -0700646 switch (brd->boardType) {
647 case MOXA_BOARD_C218_ISA:
648 case MOXA_BOARD_C218_PCI:
649 case MOXA_BOARD_CP204J:
Jiri Slaby03718232008-04-30 00:53:39 -0700650 port = brd->ports;
651 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700652 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700653 port->DCDState = 0;
654 port->tableAddr = baseAddr + Extern_table +
655 Extern_size * i;
656 ofsAddr = port->tableAddr;
657 writew(C218rx_mask, ofsAddr + RX_mask);
658 writew(C218tx_mask, ofsAddr + TX_mask);
659 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
660 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
661
662 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
663 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
664
665 }
666 break;
667 default:
Jiri Slaby03718232008-04-30 00:53:39 -0700668 port = brd->ports;
669 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700670 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700671 port->DCDState = 0;
672 port->tableAddr = baseAddr + Extern_table +
673 Extern_size * i;
674 ofsAddr = port->tableAddr;
675 switch (brd->numPorts) {
676 case 8:
677 writew(C320p8rx_mask, ofsAddr + RX_mask);
678 writew(C320p8tx_mask, ofsAddr + TX_mask);
679 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
680 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
681 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
682 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
683
684 break;
685 case 16:
686 writew(C320p16rx_mask, ofsAddr + RX_mask);
687 writew(C320p16tx_mask, ofsAddr + TX_mask);
688 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
689 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
690 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
691 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
692 break;
693
694 case 24:
695 writew(C320p24rx_mask, ofsAddr + RX_mask);
696 writew(C320p24tx_mask, ofsAddr + TX_mask);
697 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
698 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
699 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
700 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
701 break;
702 case 32:
703 writew(C320p32rx_mask, ofsAddr + RX_mask);
704 writew(C320p32tx_mask, ofsAddr + TX_mask);
705 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
706 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
707 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
708 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
709 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
710 break;
711 }
712 }
713 break;
714 }
Jiri Slaby03718232008-04-30 00:53:39 -0700715 return 0;
716}
717
718static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
719{
David Howells2bca76e2008-07-08 17:37:15 +0100720 const void *ptr = fw->data;
Jiri Slaby03718232008-04-30 00:53:39 -0700721 char rsn[64];
722 u16 lens[5];
723 size_t len;
724 unsigned int a, lenp, lencnt;
725 int ret = -EINVAL;
726 struct {
727 __le32 magic; /* 0x34303430 */
728 u8 reserved1[2];
729 u8 type; /* UNIX = 3 */
730 u8 model; /* C218T=1, C320T=2, CP204=3 */
731 u8 reserved2[8];
732 __le16 len[5];
David Howells2bca76e2008-07-08 17:37:15 +0100733 } const *hdr = ptr;
Jiri Slaby03718232008-04-30 00:53:39 -0700734
735 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
736
737 if (fw->size < MOXA_FW_HDRLEN) {
738 strcpy(rsn, "too short (even header won't fit)");
739 goto err;
740 }
741 if (hdr->magic != cpu_to_le32(0x30343034)) {
742 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
743 goto err;
744 }
745 if (hdr->type != 3) {
746 sprintf(rsn, "not for linux, type is %u", hdr->type);
747 goto err;
748 }
749 if (moxa_check_fw_model(brd, hdr->model)) {
750 sprintf(rsn, "not for this card, model is %u", hdr->model);
751 goto err;
752 }
753
754 len = MOXA_FW_HDRLEN;
755 lencnt = hdr->model == 2 ? 5 : 3;
756 for (a = 0; a < ARRAY_SIZE(lens); a++) {
757 lens[a] = le16_to_cpu(hdr->len[a]);
758 if (lens[a] && len + lens[a] <= fw->size &&
759 moxa_check_fw(&fw->data[len]))
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700760 printk(KERN_WARNING "MOXA firmware: unexpected input "
Jiri Slaby03718232008-04-30 00:53:39 -0700761 "at offset %u, but going on\n", (u32)len);
762 if (!lens[a] && a < lencnt) {
763 sprintf(rsn, "too few entries in fw file");
764 goto err;
765 }
766 len += lens[a];
767 }
768
769 if (len != fw->size) {
770 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
771 (u32)len);
772 goto err;
773 }
774
775 ptr += MOXA_FW_HDRLEN;
776 lenp = 0; /* bios */
777
778 strcpy(rsn, "read above");
779
780 ret = moxa_load_bios(brd, ptr, lens[lenp]);
781 if (ret)
782 goto err;
783
784 /* we skip the tty section (lens[1]), since we don't need it */
785 ptr += lens[lenp] + lens[lenp + 1];
786 lenp += 2; /* comm */
787
788 if (hdr->model == 2) {
789 ret = moxa_load_320b(brd, ptr, lens[lenp]);
790 if (ret)
791 goto err;
792 /* skip another tty */
793 ptr += lens[lenp] + lens[lenp + 1];
794 lenp += 2;
795 }
796
797 ret = moxa_load_code(brd, ptr, lens[lenp]);
798 if (ret)
799 goto err;
800
801 return 0;
802err:
803 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
804 return ret;
805}
806
807static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
808{
809 const struct firmware *fw;
810 const char *file;
Jiri Slaby810ab092008-04-30 00:53:41 -0700811 struct moxa_port *p;
812 unsigned int i;
Jiri Slaby03718232008-04-30 00:53:39 -0700813 int ret;
814
Jiri Slaby810ab092008-04-30 00:53:41 -0700815 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
816 GFP_KERNEL);
817 if (brd->ports == NULL) {
818 printk(KERN_ERR "cannot allocate memory for ports\n");
819 ret = -ENOMEM;
820 goto err;
821 }
822
823 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
Alan Cox9de6a512008-07-16 21:56:02 +0100824 tty_port_init(&p->port);
Alan Cox44b7d1b2008-07-16 21:57:18 +0100825 p->type = PORT_16550A;
826 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Jiri Slaby810ab092008-04-30 00:53:41 -0700827 }
828
Jiri Slaby03718232008-04-30 00:53:39 -0700829 switch (brd->boardType) {
830 case MOXA_BOARD_C218_ISA:
831 case MOXA_BOARD_C218_PCI:
832 file = "c218tunx.cod";
833 break;
834 case MOXA_BOARD_CP204J:
835 file = "cp204unx.cod";
836 break;
837 default:
838 file = "c320tunx.cod";
839 break;
840 }
841
842 ret = request_firmware(&fw, file, dev);
843 if (ret) {
Jiri Slabyec09cd52008-04-30 00:53:49 -0700844 printk(KERN_ERR "MOXA: request_firmware failed. Make sure "
845 "you've placed '%s' file into your firmware "
846 "loader directory (e.g. /lib/firmware)\n",
847 file);
Jiri Slaby810ab092008-04-30 00:53:41 -0700848 goto err_free;
Jiri Slaby03718232008-04-30 00:53:39 -0700849 }
850
851 ret = moxa_load_fw(brd, fw);
852
853 release_firmware(fw);
Jiri Slaby810ab092008-04-30 00:53:41 -0700854
855 if (ret)
856 goto err_free;
857
Jiri Slaby2a541342008-04-30 00:53:45 -0700858 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700859 brd->ready = 1;
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700860 if (!timer_pending(&moxaTimer))
861 mod_timer(&moxaTimer, jiffies + HZ / 50);
Jiri Slaby2a541342008-04-30 00:53:45 -0700862 spin_unlock_bh(&moxa_lock);
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700863
Jiri Slaby810ab092008-04-30 00:53:41 -0700864 return 0;
865err_free:
866 kfree(brd->ports);
867err:
Jiri Slaby03718232008-04-30 00:53:39 -0700868 return ret;
869}
870
Jiri Slaby810ab092008-04-30 00:53:41 -0700871static void moxa_board_deinit(struct moxa_board_conf *brd)
872{
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700873 unsigned int a, opened;
874
875 mutex_lock(&moxa_openlock);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700876 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700877 brd->ready = 0;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700878 spin_unlock_bh(&moxa_lock);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700879
880 /* pci hot-un-plug support */
881 for (a = 0; a < brd->numPorts; a++)
Alan Cox9de6a512008-07-16 21:56:02 +0100882 if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
883 tty_hangup(brd->ports[a].port.tty);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700884 while (1) {
885 opened = 0;
886 for (a = 0; a < brd->numPorts; a++)
Alan Cox9de6a512008-07-16 21:56:02 +0100887 if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700888 opened++;
889 mutex_unlock(&moxa_openlock);
890 if (!opened)
891 break;
892 msleep(50);
893 mutex_lock(&moxa_openlock);
894 }
895
Jiri Slaby810ab092008-04-30 00:53:41 -0700896 iounmap(brd->basemem);
897 brd->basemem = NULL;
898 kfree(brd->ports);
899}
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901#ifdef CONFIG_PCI
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800902static int __devinit moxa_pci_probe(struct pci_dev *pdev,
903 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800905 struct moxa_board_conf *board;
906 unsigned int i;
907 int board_type = ent->driver_data;
908 int retval;
909
910 retval = pci_enable_device(pdev);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700911 if (retval) {
912 dev_err(&pdev->dev, "can't enable pci device\n");
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800913 goto err;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700914 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800915
916 for (i = 0; i < MAX_BOARDS; i++)
917 if (moxa_boards[i].basemem == NULL)
918 break;
919
920 retval = -ENODEV;
921 if (i >= MAX_BOARDS) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700922 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800923 "found. Board is ignored.\n", MAX_BOARDS);
924 goto err;
925 }
926
927 board = &moxa_boards[i];
Jiri Slabye46a5e32008-04-30 00:53:37 -0700928
929 retval = pci_request_region(pdev, 2, "moxa-base");
930 if (retval) {
931 dev_err(&pdev->dev, "can't request pci region 2\n");
932 goto err;
933 }
934
Alan Cox24cb2332008-04-30 00:54:19 -0700935 board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700936 if (board->basemem == NULL) {
937 dev_err(&pdev->dev, "can't remap io space 2\n");
Jiri Slabye46a5e32008-04-30 00:53:37 -0700938 goto err_reg;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700939 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 board->boardType = board_type;
942 switch (board_type) {
943 case MOXA_BOARD_C218_ISA:
944 case MOXA_BOARD_C218_PCI:
945 board->numPorts = 8;
946 break;
947
948 case MOXA_BOARD_CP204J:
949 board->numPorts = 4;
950 break;
951 default:
952 board->numPorts = 0;
953 break;
954 }
955 board->busType = MOXA_BUS_TYPE_PCI;
Jiri Slabya784bf72007-02-10 01:45:36 -0800956
Jiri Slaby03718232008-04-30 00:53:39 -0700957 retval = moxa_init_board(board, &pdev->dev);
958 if (retval)
959 goto err_base;
960
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800961 pci_set_drvdata(pdev, board);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Jiri Slabybb9f9102008-04-30 00:53:48 -0700963 dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n",
964 moxa_brdname[board_type - 1], board->numPorts);
965
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700966 return 0;
Jiri Slaby03718232008-04-30 00:53:39 -0700967err_base:
968 iounmap(board->basemem);
969 board->basemem = NULL;
Jiri Slabye46a5e32008-04-30 00:53:37 -0700970err_reg:
971 pci_release_region(pdev, 2);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800972err:
973 return retval;
974}
975
976static void __devexit moxa_pci_remove(struct pci_dev *pdev)
977{
978 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
979
Jiri Slaby810ab092008-04-30 00:53:41 -0700980 moxa_board_deinit(brd);
981
Jiri Slabye46a5e32008-04-30 00:53:37 -0700982 pci_release_region(pdev, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
Jiri Slabya784bf72007-02-10 01:45:36 -0800984
985static struct pci_driver moxa_pci_driver = {
986 .name = "moxa",
987 .id_table = moxa_pcibrds,
988 .probe = moxa_pci_probe,
989 .remove = __devexit_p(moxa_pci_remove)
990};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991#endif /* CONFIG_PCI */
992
993static int __init moxa_init(void)
994{
Jiri Slaby810ab092008-04-30 00:53:41 -0700995 unsigned int isabrds = 0;
Jiri Slabyd353eca2008-04-30 00:53:37 -0700996 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700998 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
999 MOXA_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
1001 if (!moxaDriver)
1002 return -ENOMEM;
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 moxaDriver->owner = THIS_MODULE;
Sergey Vlasov9b4e3b12005-09-03 16:26:49 +01001005 moxaDriver->name = "ttyMX";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 moxaDriver->major = ttymajor;
1007 moxaDriver->minor_start = 0;
1008 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1009 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1010 moxaDriver->init_termios = tty_std_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Alan Cox606d0992006-12-08 02:38:45 -08001012 moxaDriver->init_termios.c_ispeed = 9600;
1013 moxaDriver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
1015 tty_set_operations(moxaDriver, &moxa_ops);
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (tty_register_driver(moxaDriver)) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001018 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 put_tty_driver(moxaDriver);
1020 return -1;
1021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Jiri Slabyd353eca2008-04-30 00:53:37 -07001023 /* Find the boards defined from module args. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -07001025 {
1026 struct moxa_board_conf *brd = moxa_boards;
Jiri Slaby810ab092008-04-30 00:53:41 -07001027 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 for (i = 0; i < MAX_BOARDS; i++) {
Jiri Slabyd353eca2008-04-30 00:53:37 -07001029 if (!baseaddr[i])
1030 break;
1031 if (type[i] == MOXA_BOARD_C218_ISA ||
1032 type[i] == MOXA_BOARD_C320_ISA) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001033 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001034 isabrds + 1, moxa_brdname[type[i] - 1],
1035 baseaddr[i]);
1036 brd->boardType = type[i];
1037 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1038 numports[i];
1039 brd->busType = MOXA_BUS_TYPE_ISA;
Alan Cox24cb2332008-04-30 00:54:19 -07001040 brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
Jiri Slabyd353eca2008-04-30 00:53:37 -07001041 if (!brd->basemem) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001042 printk(KERN_ERR "MOXA: can't remap %lx\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001043 baseaddr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 continue;
1045 }
Jiri Slaby03718232008-04-30 00:53:39 -07001046 if (moxa_init_board(brd, NULL)) {
1047 iounmap(brd->basemem);
1048 brd->basemem = NULL;
1049 continue;
1050 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001051
Jiri Slabybb9f9102008-04-30 00:53:48 -07001052 printk(KERN_INFO "MOXA isa board found at 0x%.8lu and "
1053 "ready (%u ports, firmware loaded)\n",
1054 baseaddr[i], brd->numPorts);
1055
Jiri Slabyd353eca2008-04-30 00:53:37 -07001056 brd++;
1057 isabrds++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001064 retval = pci_register_driver(&moxa_pci_driver);
1065 if (retval) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001066 printk(KERN_ERR "Can't register MOXA pci driver!\n");
Jiri Slabyd353eca2008-04-30 00:53:37 -07001067 if (isabrds)
Jiri Slabya784bf72007-02-10 01:45:36 -08001068 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
1070#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001071
Jiri Slabya784bf72007-02-10 01:45:36 -08001072 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
1075static void __exit moxa_exit(void)
1076{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001077 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001079#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001080 pci_unregister_driver(&moxa_pci_driver);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001081#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001082
Jiri Slaby810ab092008-04-30 00:53:41 -07001083 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1084 if (moxa_boards[i].ready)
1085 moxa_board_deinit(&moxa_boards[i]);
Jiri Slaby2a541342008-04-30 00:53:45 -07001086
1087 del_timer_sync(&moxaTimer);
1088
1089 if (tty_unregister_driver(moxaDriver))
1090 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1091 "serial driver\n");
1092 put_tty_driver(moxaDriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
1095module_init(moxa_init);
1096module_exit(moxa_exit);
1097
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001098static void moxa_close_port(struct moxa_port *ch)
1099{
1100 moxa_shut_down(ch);
1101 MoxaPortFlushData(ch, 2);
Alan Cox9de6a512008-07-16 21:56:02 +01001102 ch->port.flags &= ~ASYNC_NORMAL_ACTIVE;
1103 ch->port.tty->driver_data = NULL;
1104 ch->port.tty = NULL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001105}
1106
1107static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
1108 struct moxa_port *ch)
1109{
1110 DEFINE_WAIT(wait);
1111 int retval = 0;
1112 u8 dcd;
1113
1114 while (1) {
Alan Cox9de6a512008-07-16 21:56:02 +01001115 prepare_to_wait(&ch->port.open_wait, &wait, TASK_INTERRUPTIBLE);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001116 if (tty_hung_up_p(filp)) {
1117#ifdef SERIAL_DO_RESTART
1118 retval = -ERESTARTSYS;
1119#else
1120 retval = -EAGAIN;
1121#endif
1122 break;
1123 }
1124 spin_lock_bh(&moxa_lock);
1125 dcd = ch->DCDState;
1126 spin_unlock_bh(&moxa_lock);
1127 if (dcd)
1128 break;
1129
1130 if (signal_pending(current)) {
1131 retval = -ERESTARTSYS;
1132 break;
1133 }
1134 schedule();
1135 }
Alan Cox9de6a512008-07-16 21:56:02 +01001136 finish_wait(&ch->port.open_wait, &wait);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001137
1138 return retval;
1139}
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141static int moxa_open(struct tty_struct *tty, struct file *filp)
1142{
Jiri Slaby810ab092008-04-30 00:53:41 -07001143 struct moxa_board_conf *brd;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001144 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 int port;
1146 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Jiri Slaby11324ed2007-02-10 01:45:31 -08001148 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (port == MAX_PORTS) {
Jiri Slaby74d7d972008-04-30 00:53:43 -07001150 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001152 if (mutex_lock_interruptible(&moxa_openlock))
1153 return -ERESTARTSYS;
Jiri Slaby810ab092008-04-30 00:53:41 -07001154 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001155 if (!brd->ready) {
1156 mutex_unlock(&moxa_openlock);
Jiri Slaby810ab092008-04-30 00:53:41 -07001157 return -ENODEV;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Jiri Slaby810ab092008-04-30 00:53:41 -07001160 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
Alan Cox9de6a512008-07-16 21:56:02 +01001161 ch->port.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 tty->driver_data = ch;
Alan Cox9de6a512008-07-16 21:56:02 +01001163 ch->port.tty = tty;
1164 if (!(ch->port.flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 ch->statusflags = 0;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001166 moxa_set_tty_param(tty, tty->termios);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001167 MoxaPortLineCtrl(ch, 1, 1);
1168 MoxaPortEnable(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001169 MoxaSetFifo(ch, ch->type == PORT_16550A);
Alan Cox9de6a512008-07-16 21:56:02 +01001170 ch->port.flags |= ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001172 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001174 retval = 0;
1175 if (!(filp->f_flags & O_NONBLOCK) && !C_CLOCAL(tty))
1176 retval = moxa_block_till_ready(tty, filp, ch);
1177 mutex_lock(&moxa_openlock);
1178 if (retval) {
Alan Cox9de6a512008-07-16 21:56:02 +01001179 if (ch->port.count) /* 0 means already hung up... */
1180 if (--ch->port.count == 0)
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001181 moxa_close_port(ch);
1182 } else
Alan Cox9de6a512008-07-16 21:56:02 +01001183 ch->port.flags |= ASYNC_NORMAL_ACTIVE;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001184 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001186 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
1189static void moxa_close(struct tty_struct *tty, struct file *filp)
1190{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001191 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 int port;
1193
Jiri Slaby11324ed2007-02-10 01:45:31 -08001194 port = tty->index;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001195 if (port == MAX_PORTS || tty_hung_up_p(filp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001198 mutex_lock(&moxa_openlock);
1199 ch = tty->driver_data;
1200 if (ch == NULL)
1201 goto unlock;
Alan Cox9de6a512008-07-16 21:56:02 +01001202 if (tty->count == 1 && ch->port.count != 1) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001203 printk(KERN_WARNING "moxa_close: bad serial port count; "
Alan Cox9de6a512008-07-16 21:56:02 +01001204 "tty->count is 1, ch->port.count is %d\n", ch->port.count);
1205 ch->port.count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
Alan Cox9de6a512008-07-16 21:56:02 +01001207 if (--ch->port.count < 0) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001208 printk(KERN_WARNING "moxa_close: bad serial port count, "
1209 "device=%s\n", tty->name);
Alan Cox9de6a512008-07-16 21:56:02 +01001210 ch->port.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
Alan Cox9de6a512008-07-16 21:56:02 +01001212 if (ch->port.count)
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001213 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 ch->cflag = tty->termios->c_cflag;
Alan Cox9de6a512008-07-16 21:56:02 +01001216 if (ch->port.flags & ASYNC_INITIALIZED) {
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001217 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001221 moxa_close_port(ch);
1222unlock:
1223 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
1226static int moxa_write(struct tty_struct *tty,
1227 const unsigned char *buf, int count)
1228{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001229 struct moxa_port *ch = tty->driver_data;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001230 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (ch == NULL)
Jiri Slabyb4173f42008-04-30 00:53:40 -07001233 return 0;
Alan Cox33f0f882006-01-09 20:54:13 -08001234
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001235 spin_lock_bh(&moxa_lock);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001236 len = MoxaPortWriteData(ch, buf, count);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001237 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 ch->statusflags |= LOWWAIT;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001240 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
1243static int moxa_write_room(struct tty_struct *tty)
1244{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001245 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 if (tty->stopped)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001248 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001249 ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (ch == NULL)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001251 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001252 return MoxaPortTxFree(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
1255static void moxa_flush_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
1259 if (ch == NULL)
1260 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001261 MoxaPortFlushData(ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 tty_wakeup(tty);
1263}
1264
1265static int moxa_chars_in_buffer(struct tty_struct *tty)
1266{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001267 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 /*
1271 * Sigh...I have to check if driver_data is NULL here, because
1272 * if an open() fails, the TTY subsystem eventually calls
1273 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1274 * routine. And since the open() failed, we return 0 here. TDJ
1275 */
1276 if (ch == NULL)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001277 return 0;
Alan Cox978e5952008-04-30 00:53:59 -07001278 lock_kernel();
Jiri Slabyb4173f42008-04-30 00:53:40 -07001279 chars = MoxaPortTxQueue(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (chars) {
1281 /*
1282 * Make it possible to wakeup anything waiting for output
1283 * in tty_ioctl.c, etc.
1284 */
1285 if (!(ch->statusflags & EMPTYWAIT))
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001286 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
Alan Cox978e5952008-04-30 00:53:59 -07001288 unlock_kernel();
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001289 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1293{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001294 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 int flag = 0, dtr, rts;
1296
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001297 mutex_lock(&moxa_openlock);
1298 ch = tty->driver_data;
1299 if (!ch) {
1300 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -07001301 return -EINVAL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Jiri Slabyb4173f42008-04-30 00:53:40 -07001304 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (dtr)
1306 flag |= TIOCM_DTR;
1307 if (rts)
1308 flag |= TIOCM_RTS;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001309 dtr = MoxaPortLineStatus(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (dtr & 1)
1311 flag |= TIOCM_CTS;
1312 if (dtr & 2)
1313 flag |= TIOCM_DSR;
1314 if (dtr & 4)
1315 flag |= TIOCM_CD;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001316 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 return flag;
1318}
1319
1320static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1321 unsigned int set, unsigned int clear)
1322{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001323 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 int port;
1325 int dtr, rts;
1326
Jiri Slaby11324ed2007-02-10 01:45:31 -08001327 port = tty->index;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001328 mutex_lock(&moxa_openlock);
1329 ch = tty->driver_data;
1330 if (!ch) {
1331 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -07001332 return -EINVAL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Jiri Slabyb4173f42008-04-30 00:53:40 -07001335 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if (set & TIOCM_RTS)
1337 rts = 1;
1338 if (set & TIOCM_DTR)
1339 dtr = 1;
1340 if (clear & TIOCM_RTS)
1341 rts = 0;
1342 if (clear & TIOCM_DTR)
1343 dtr = 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001344 MoxaPortLineCtrl(ch, dtr, rts);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001345 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return 0;
1347}
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349static void moxa_throttle(struct tty_struct *tty)
1350{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001351 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 ch->statusflags |= THROTTLE;
1354}
1355
1356static void moxa_unthrottle(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 ch->statusflags &= ~THROTTLE;
1361}
1362
1363static void moxa_set_termios(struct tty_struct *tty,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001364 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001366 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 if (ch == NULL)
1369 return;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001370 moxa_set_tty_param(tty, old_termios);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001371 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
Alan Cox9de6a512008-07-16 21:56:02 +01001372 wake_up_interruptible(&ch->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
1375static void moxa_stop(struct tty_struct *tty)
1376{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001377 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 if (ch == NULL)
1380 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001381 MoxaPortTxDisable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 ch->statusflags |= TXSTOPPED;
1383}
1384
1385
1386static void moxa_start(struct tty_struct *tty)
1387{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001388 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 if (ch == NULL)
1391 return;
1392
1393 if (!(ch->statusflags & TXSTOPPED))
1394 return;
1395
Jiri Slabyb4173f42008-04-30 00:53:40 -07001396 MoxaPortTxEnable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 ch->statusflags &= ~TXSTOPPED;
1398}
1399
1400static void moxa_hangup(struct tty_struct *tty)
1401{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001402 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001404 mutex_lock(&moxa_openlock);
1405 ch = tty->driver_data;
1406 if (ch == NULL) {
1407 mutex_unlock(&moxa_openlock);
1408 return;
1409 }
Alan Cox9de6a512008-07-16 21:56:02 +01001410 ch->port.count = 0;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001411 moxa_close_port(ch);
1412 mutex_unlock(&moxa_openlock);
1413
Alan Cox9de6a512008-07-16 21:56:02 +01001414 wake_up_interruptible(&ch->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001417static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1418{
1419 dcd = !!dcd;
1420
Alan Cox9de6a512008-07-16 21:56:02 +01001421 if (dcd != p->DCDState && p->port.tty && C_CLOCAL(p->port.tty)) {
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001422 if (!dcd)
Alan Cox9de6a512008-07-16 21:56:02 +01001423 tty_hangup(p->port.tty);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001424 }
1425 p->DCDState = dcd;
1426}
1427
1428static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1429 u16 __iomem *ip)
1430{
Alan Cox9de6a512008-07-16 21:56:02 +01001431 struct tty_struct *tty = p->port.tty;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001432 void __iomem *ofsAddr;
Alan Cox9de6a512008-07-16 21:56:02 +01001433 unsigned int inited = p->port.flags & ASYNC_INITIALIZED;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001434 u16 intr;
1435
1436 if (tty) {
1437 if ((p->statusflags & EMPTYWAIT) &&
1438 MoxaPortTxQueue(p) == 0) {
1439 p->statusflags &= ~EMPTYWAIT;
1440 tty_wakeup(tty);
1441 }
1442 if ((p->statusflags & LOWWAIT) && !tty->stopped &&
1443 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1444 p->statusflags &= ~LOWWAIT;
1445 tty_wakeup(tty);
1446 }
1447
1448 if (inited && !(p->statusflags & THROTTLE) &&
1449 MoxaPortRxQueue(p) > 0) { /* RX */
1450 MoxaPortReadData(p);
1451 tty_schedule_flip(tty);
1452 }
1453 } else {
1454 p->statusflags &= ~EMPTYWAIT;
1455 MoxaPortFlushData(p, 0); /* flush RX */
1456 }
1457
1458 if (!handle) /* nothing else to do */
1459 return 0;
1460
1461 intr = readw(ip); /* port irq status */
1462 if (intr == 0)
1463 return 0;
1464
1465 writew(0, ip); /* ACK port */
1466 ofsAddr = p->tableAddr;
1467 if (intr & IntrTx) /* disable tx intr */
1468 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1469 ofsAddr + HostStat);
1470
1471 if (!inited)
1472 return 0;
1473
1474 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1475 tty_insert_flip_char(tty, 0, TTY_BREAK);
1476 tty_schedule_flip(tty);
1477 }
1478
1479 if (intr & IntrLine)
1480 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1481
1482 return 0;
1483}
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485static void moxa_poll(unsigned long ignored)
1486{
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001487 struct moxa_board_conf *brd;
1488 u16 __iomem *ip;
Jiri Slaby2a541342008-04-30 00:53:45 -07001489 unsigned int card, port, served = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001491 spin_lock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 for (card = 0; card < MAX_BOARDS; card++) {
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001493 brd = &moxa_boards[card];
1494 if (!brd->ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 continue;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001496
Jiri Slaby2a541342008-04-30 00:53:45 -07001497 served++;
1498
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001499 ip = NULL;
1500 if (readb(brd->intPend) == 0xff)
1501 ip = brd->intTable + readb(brd->intNdx);
1502
1503 for (port = 0; port < brd->numPorts; port++)
1504 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1505
1506 if (ip)
1507 writeb(0, brd->intPend); /* ACK */
1508
1509 if (moxaLowWaterChk) {
1510 struct moxa_port *p = brd->ports;
1511 for (port = 0; port < brd->numPorts; port++, p++)
1512 if (p->lowChkFlag) {
1513 p->lowChkFlag = 0;
1514 moxa_low_water_check(p->tableAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 }
1517 }
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001518 moxaLowWaterChk = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Jiri Slaby2a541342008-04-30 00:53:45 -07001520 if (served)
1521 mod_timer(&moxaTimer, jiffies + HZ / 50);
1522 spin_unlock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
1525/******************************************************************************/
1526
Alan Coxdb1acaa2008-02-08 04:18:43 -08001527static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001529 register struct ktermios *ts = tty->termios;
1530 struct moxa_port *ch = tty->driver_data;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001531 int rts, cts, txflow, rxflow, xany, baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 rts = cts = txflow = rxflow = xany = 0;
1534 if (ts->c_cflag & CRTSCTS)
1535 rts = cts = 1;
1536 if (ts->c_iflag & IXON)
1537 txflow = 1;
1538 if (ts->c_iflag & IXOFF)
1539 rxflow = 1;
1540 if (ts->c_iflag & IXANY)
1541 xany = 1;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001542
1543 /* Clear the features we don't support */
1544 ts->c_cflag &= ~CMSPAR;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001545 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1546 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
Alan Coxdb1acaa2008-02-08 04:18:43 -08001547 if (baud == -1)
1548 baud = tty_termios_baud_rate(old_termios);
1549 /* Not put the baud rate into the termios data */
1550 tty_encode_baud_rate(tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001553static void moxa_setup_empty_event(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001555 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001557 spin_lock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 ch->statusflags |= EMPTYWAIT;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001559 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001562static void moxa_shut_down(struct moxa_port *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Alan Cox9de6a512008-07-16 21:56:02 +01001564 struct tty_struct *tp = ch->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Alan Cox9de6a512008-07-16 21:56:02 +01001566 if (!(ch->port.flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 return;
1568
Jiri Slabyb4173f42008-04-30 00:53:40 -07001569 MoxaPortDisable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 /*
1572 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1573 */
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001574 if (C_HUPCL(tp))
Jiri Slabyb4173f42008-04-30 00:53:40 -07001575 MoxaPortLineCtrl(ch, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001577 spin_lock_bh(&moxa_lock);
Alan Cox9de6a512008-07-16 21:56:02 +01001578 ch->port.flags &= ~ASYNC_INITIALIZED;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001579 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582/*****************************************************************************
1583 * Driver level functions: *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Jiri Slabyb4173f42008-04-30 00:53:40 -07001586static void MoxaPortFlushData(struct moxa_port *port, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
1588 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001589 if (mode < 0 || mode > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001591 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 moxafunc(ofsAddr, FC_FlushQueue, mode);
1593 if (mode != 1) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001594 port->lowChkFlag = 0;
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001595 moxa_low_water_check(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 }
1597}
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599/*
1600 * Moxa Port Number Description:
1601 *
1602 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1603 * the port number using in MOXA driver functions will be 0 to 31 for
1604 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1605 * to 127 for fourth. For example, if you setup three MOXA boards,
1606 * first board is C218, second board is C320-16 and third board is
1607 * C320-32. The port number of first board (C218 - 8 ports) is from
1608 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1609 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1610 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1611 * 127 will be invalid.
1612 *
1613 *
1614 * Moxa Functions Description:
1615 *
1616 * Function 1: Driver initialization routine, this routine must be
1617 * called when initialized driver.
1618 * Syntax:
1619 * void MoxaDriverInit();
1620 *
1621 *
1622 * Function 2: Moxa driver private IOCTL command processing.
1623 * Syntax:
1624 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1625 *
1626 * unsigned int cmd : IOCTL command
1627 * unsigned long arg : IOCTL argument
1628 * int port : port number (0 - 127)
1629 *
1630 * return: 0 (OK)
1631 * -EINVAL
1632 * -ENOIOCTLCMD
1633 *
1634 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 * Function 6: Enable this port to start Tx/Rx data.
1636 * Syntax:
1637 * void MoxaPortEnable(int port);
1638 * int port : port number (0 - 127)
1639 *
1640 *
1641 * Function 7: Disable this port
1642 * Syntax:
1643 * void MoxaPortDisable(int port);
1644 * int port : port number (0 - 127)
1645 *
1646 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 * Function 10: Setting baud rate of this port.
1648 * Syntax:
Jiri Slaby08d01c792008-04-30 00:53:47 -07001649 * speed_t MoxaPortSetBaud(int port, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 * int port : port number (0 - 127)
1651 * long baud : baud rate (50 - 115200)
1652 *
1653 * return: 0 : this port is invalid or baud < 50
1654 * 50 - 115200 : the real baud rate set to the port, if
1655 * the argument baud is large than maximun
1656 * available baud rate, the real setting
1657 * baud rate will be the maximun baud rate.
1658 *
1659 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 * Function 12: Configure the port.
1661 * Syntax:
Alan Cox606d0992006-12-08 02:38:45 -08001662 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 * int port : port number (0 - 127)
Alan Cox606d0992006-12-08 02:38:45 -08001664 * struct ktermios * termio : termio structure pointer
Alan Coxc7bce302006-09-30 23:27:24 -07001665 * speed_t baud : baud rate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 *
1667 * return: -1 : this port is invalid or termio == NULL
1668 * 0 : setting O.K.
1669 *
1670 *
1671 * Function 13: Get the DTR/RTS state of this port.
1672 * Syntax:
1673 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1674 * int port : port number (0 - 127)
1675 * int * dtrState : pointer to INT to receive the current DTR
1676 * state. (if NULL, this function will not
1677 * write to this address)
1678 * int * rtsState : pointer to INT to receive the current RTS
1679 * state. (if NULL, this function will not
1680 * write to this address)
1681 *
1682 * return: -1 : this port is invalid
1683 * 0 : O.K.
1684 *
1685 *
1686 * Function 14: Setting the DTR/RTS output state of this port.
1687 * Syntax:
1688 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1689 * int port : port number (0 - 127)
1690 * int dtrState : DTR output state (0: off, 1: on)
1691 * int rtsState : RTS output state (0: off, 1: on)
1692 *
1693 *
1694 * Function 15: Setting the flow control of this port.
1695 * Syntax:
1696 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1697 * int txFlow,int xany);
1698 * int port : port number (0 - 127)
1699 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1700 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1701 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1702 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1703 * int xany : S/W XANY flow control (0: no, 1: yes)
1704 *
1705 *
1706 * Function 16: Get ths line status of this port
1707 * Syntax:
1708 * int MoxaPortLineStatus(int port);
1709 * int port : port number (0 - 127)
1710 *
1711 * return: Bit 0 - CTS state (0: off, 1: on)
1712 * Bit 1 - DSR state (0: off, 1: on)
1713 * Bit 2 - DCD state (0: off, 1: on)
1714 *
1715 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 * Function 19: Flush the Rx/Tx buffer data of this port.
1717 * Syntax:
1718 * void MoxaPortFlushData(int port, int mode);
1719 * int port : port number (0 - 127)
1720 * int mode
1721 * 0 : flush the Rx buffer
1722 * 1 : flush the Tx buffer
1723 * 2 : flush the Rx and Tx buffer
1724 *
1725 *
1726 * Function 20: Write data.
1727 * Syntax:
1728 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1729 * int port : port number (0 - 127)
1730 * unsigned char * buffer : pointer to write data buffer.
1731 * int length : write data length
1732 *
1733 * return: 0 - length : real write data length
1734 *
1735 *
1736 * Function 21: Read data.
1737 * Syntax:
Alan Cox33f0f882006-01-09 20:54:13 -08001738 * int MoxaPortReadData(int port, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 * int port : port number (0 - 127)
Alan Cox33f0f882006-01-09 20:54:13 -08001740 * struct tty_struct *tty : tty for data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 *
1742 * return: 0 - length : real read data length
1743 *
1744 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 * Function 24: Get the Tx buffer current queued data bytes
1746 * Syntax:
1747 * int MoxaPortTxQueue(int port);
1748 * int port : port number (0 - 127)
1749 *
1750 * return: .. : Tx buffer current queued data bytes
1751 *
1752 *
1753 * Function 25: Get the Tx buffer current free space
1754 * Syntax:
1755 * int MoxaPortTxFree(int port);
1756 * int port : port number (0 - 127)
1757 *
1758 * return: .. : Tx buffer current free space
1759 *
1760 *
1761 * Function 26: Get the Rx buffer current queued data bytes
1762 * Syntax:
1763 * int MoxaPortRxQueue(int port);
1764 * int port : port number (0 - 127)
1765 *
1766 * return: .. : Rx buffer current queued data bytes
1767 *
1768 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 * Function 28: Disable port data transmission.
1770 * Syntax:
1771 * void MoxaPortTxDisable(int port);
1772 * int port : port number (0 - 127)
1773 *
1774 *
1775 * Function 29: Enable port data transmission.
1776 * Syntax:
1777 * void MoxaPortTxEnable(int port);
1778 * int port : port number (0 - 127)
1779 *
1780 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 * Function 31: Get the received BREAK signal count and reset it.
1782 * Syntax:
1783 * int MoxaPortResetBrkCnt(int port);
1784 * int port : port number (0 - 127)
1785 *
1786 * return: 0 - .. : BREAK signal count
1787 *
1788 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Jiri Slabyb4173f42008-04-30 00:53:40 -07001791static void MoxaPortEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
1793 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001794 u16 lowwater = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Jiri Slabyb4173f42008-04-30 00:53:40 -07001796 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 writew(lowwater, ofsAddr + Low_water);
Jiri Slaby08d01c792008-04-30 00:53:47 -07001798 if (MOXA_IS_320(port->board))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001800 else
1801 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1802 ofsAddr + HostStat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
1804 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1805 moxafunc(ofsAddr, FC_FlushQueue, 2);
1806
1807 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1808 MoxaPortLineStatus(port);
1809}
1810
Jiri Slabyb4173f42008-04-30 00:53:40 -07001811static void MoxaPortDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001813 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
1815 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1816 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1817 writew(0, ofsAddr + HostStat);
1818 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1819}
1820
Jiri Slaby08d01c792008-04-30 00:53:47 -07001821static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
Jiri Slaby08d01c792008-04-30 00:53:47 -07001823 void __iomem *ofsAddr = port->tableAddr;
1824 unsigned int clock, val;
1825 speed_t max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Jiri Slaby08d01c792008-04-30 00:53:47 -07001827 max = MOXA_IS_320(port->board) ? 460800 : 921600;
1828 if (baud < 50)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001829 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (baud > max)
1831 baud = max;
Jiri Slaby08d01c792008-04-30 00:53:47 -07001832 clock = 921600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 val = clock / baud;
1834 moxafunc(ofsAddr, FC_SetBaud, val);
1835 baud = clock / val;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001836 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837}
1838
Jiri Slabyb4173f42008-04-30 00:53:40 -07001839static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1840 speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
1842 void __iomem *ofsAddr;
1843 tcflag_t cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 tcflag_t mode = 0;
1845
Jiri Slabyb4173f42008-04-30 00:53:40 -07001846 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 cflag = termio->c_cflag; /* termio->c_cflag */
1848
1849 mode = termio->c_cflag & CSIZE;
1850 if (mode == CS5)
1851 mode = MX_CS5;
1852 else if (mode == CS6)
1853 mode = MX_CS6;
1854 else if (mode == CS7)
1855 mode = MX_CS7;
1856 else if (mode == CS8)
1857 mode = MX_CS8;
1858
1859 if (termio->c_cflag & CSTOPB) {
1860 if (mode == MX_CS5)
1861 mode |= MX_STOP15;
1862 else
1863 mode |= MX_STOP2;
1864 } else
1865 mode |= MX_STOP1;
1866
1867 if (termio->c_cflag & PARENB) {
1868 if (termio->c_cflag & PARODD)
1869 mode |= MX_PARODD;
1870 else
1871 mode |= MX_PAREVEN;
1872 } else
1873 mode |= MX_PARNONE;
1874
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001875 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Jiri Slaby08d01c792008-04-30 00:53:47 -07001877 if (MOXA_IS_320(port->board) && baud >= 921600)
1878 return -1;
1879
Alan Coxdb1acaa2008-02-08 04:18:43 -08001880 baud = MoxaPortSetBaud(port, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1883 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1884 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1885 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001886 moxa_wait_finish(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 }
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001889 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890}
1891
Jiri Slabyb4173f42008-04-30 00:53:40 -07001892static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1893 int *rtsState)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001895 if (dtrState)
1896 *dtrState = !!(port->lineCtrl & DTR_ON);
1897 if (rtsState)
1898 *rtsState = !!(port->lineCtrl & RTS_ON);
1899
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001900 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901}
1902
Jiri Slabyb4173f42008-04-30 00:53:40 -07001903static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001905 u8 mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 if (dtr)
1908 mode |= DTR_ON;
1909 if (rts)
1910 mode |= RTS_ON;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001911 port->lineCtrl = mode;
1912 moxafunc(port->tableAddr, FC_LineControl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913}
1914
Jiri Slabyb4173f42008-04-30 00:53:40 -07001915static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1916 int txflow, int rxflow, int txany)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001918 int mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 if (rts)
1921 mode |= RTS_FlowCtl;
1922 if (cts)
1923 mode |= CTS_FlowCtl;
1924 if (txflow)
1925 mode |= Tx_FlowCtl;
1926 if (rxflow)
1927 mode |= Rx_FlowCtl;
1928 if (txany)
1929 mode |= IXM_IXANY;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001930 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
Jiri Slabyb4173f42008-04-30 00:53:40 -07001933static int MoxaPortLineStatus(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
1935 void __iomem *ofsAddr;
1936 int val;
1937
Jiri Slabyb4173f42008-04-30 00:53:40 -07001938 ofsAddr = port->tableAddr;
Jiri Slaby08d01c792008-04-30 00:53:47 -07001939 if (MOXA_IS_320(port->board)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 moxafunc(ofsAddr, FC_LineStatus, 0);
1941 val = readw(ofsAddr + FuncArg);
1942 } else {
1943 val = readw(ofsAddr + FlagStat) >> 4;
1944 }
1945 val &= 0x0B;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001946 if (val & 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 val |= 4;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001948 spin_lock_bh(&moxa_lock);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001949 moxa_new_dcdstate(port, val & 8);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001950 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 val &= 7;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001952 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953}
1954
Jiri Slaby2108eba2008-04-30 00:53:44 -07001955static int MoxaPortWriteData(struct moxa_port *port,
1956 const unsigned char *buffer, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001959 unsigned int c, total;
1960 u16 head, tail, tx_mask, spage, epage;
1961 u16 pageno, pageofs, bufhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Jiri Slabyb4173f42008-04-30 00:53:40 -07001963 ofsAddr = port->tableAddr;
1964 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 tx_mask = readw(ofsAddr + TX_mask);
1966 spage = readw(ofsAddr + Page_txb);
1967 epage = readw(ofsAddr + EndPage_txb);
1968 tail = readw(ofsAddr + TXwptr);
1969 head = readw(ofsAddr + TXrptr);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001970 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if (c > len)
1972 c = len;
Alan Cox9de6a512008-07-16 21:56:02 +01001973 moxaLog.txcnt[port->port.tty->index] += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 total = c;
1975 if (spage == epage) {
1976 bufhead = readw(ofsAddr + Ofs_txb);
1977 writew(spage, baseAddr + Control_reg);
1978 while (c > 0) {
1979 if (head > tail)
1980 len = head - tail - 1;
1981 else
1982 len = tx_mask + 1 - tail;
1983 len = (c > len) ? len : c;
1984 ofs = baseAddr + DynPage_addr + bufhead + tail;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001985 memcpy_toio(ofs, buffer, len);
1986 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 tail = (tail + len) & tx_mask;
1988 c -= len;
1989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 pageno = spage + (tail >> 13);
1992 pageofs = tail & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001993 while (c > 0) {
1994 len = Page_size - pageofs;
1995 if (len > c)
1996 len = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 writeb(pageno, baseAddr + Control_reg);
1998 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001999 memcpy_toio(ofs, buffer, len);
2000 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 if (++pageno == epage)
2002 pageno = spage;
2003 pageofs = 0;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002004 c -= len;
2005 }
2006 tail = (tail + total) & tx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002008 writew(tail, ofsAddr + TXwptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 writeb(1, ofsAddr + CD180TXirq); /* start to send */
Jiri Slaby2108eba2008-04-30 00:53:44 -07002010 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
2012
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002013static int MoxaPortReadData(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
Alan Cox9de6a512008-07-16 21:56:02 +01002015 struct tty_struct *tty = port->port.tty;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002016 unsigned char *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002018 unsigned int count, len, total;
2019 u16 tail, rx_mask, spage, epage;
2020 u16 pageno, pageofs, bufhead, head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Jiri Slabyb4173f42008-04-30 00:53:40 -07002022 ofsAddr = port->tableAddr;
2023 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 head = readw(ofsAddr + RXrptr);
2025 tail = readw(ofsAddr + RXwptr);
2026 rx_mask = readw(ofsAddr + RX_mask);
2027 spage = readw(ofsAddr + Page_rxb);
2028 epage = readw(ofsAddr + EndPage_rxb);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002029 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 if (count == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08002031 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Alan Cox33f0f882006-01-09 20:54:13 -08002033 total = count;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002034 moxaLog.rxcnt[tty->index] += total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (spage == epage) {
2036 bufhead = readw(ofsAddr + Ofs_rxb);
2037 writew(spage, baseAddr + Control_reg);
2038 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 ofs = baseAddr + DynPage_addr + bufhead + head;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002040 len = (tail >= head) ? (tail - head) :
2041 (rx_mask + 1 - head);
2042 len = tty_prepare_flip_string(tty, &dst,
2043 min(len, count));
2044 memcpy_fromio(dst, ofs, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 head = (head + len) & rx_mask;
2046 count -= len;
2047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 pageno = spage + (head >> 13);
2050 pageofs = head & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002051 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 writew(pageno, baseAddr + Control_reg);
2053 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002054 len = tty_prepare_flip_string(tty, &dst,
2055 min(Page_size - pageofs, count));
2056 memcpy_fromio(dst, ofs, len);
2057
2058 count -= len;
2059 pageofs = (pageofs + len) & Page_mask;
2060 if (pageofs == 0 && ++pageno == epage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 pageno = spage;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002062 }
2063 head = (head + total) & rx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002065 writew(head, ofsAddr + RXrptr);
2066 if (readb(ofsAddr + FlagStat) & Xoff_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 moxaLowWaterChk = 1;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002068 port->lowChkFlag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002070 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071}
2072
2073
Jiri Slabyb4173f42008-04-30 00:53:40 -07002074static int MoxaPortTxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002076 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002077 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 rptr = readw(ofsAddr + TXrptr);
2080 wptr = readw(ofsAddr + TXwptr);
2081 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002082 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083}
2084
Jiri Slabyb4173f42008-04-30 00:53:40 -07002085static int MoxaPortTxFree(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002087 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002088 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 rptr = readw(ofsAddr + TXrptr);
2091 wptr = readw(ofsAddr + TXwptr);
2092 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002093 return mask - ((wptr - rptr) & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
2095
Jiri Slabyb4173f42008-04-30 00:53:40 -07002096static int MoxaPortRxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002098 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002099 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 rptr = readw(ofsAddr + RXrptr);
2102 wptr = readw(ofsAddr + RXwptr);
2103 mask = readw(ofsAddr + RX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002104 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
Jiri Slabyb4173f42008-04-30 00:53:40 -07002107static void MoxaPortTxDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002109 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
Jiri Slabyb4173f42008-04-30 00:53:40 -07002112static void MoxaPortTxEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002114 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115}
2116
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002117static int moxa_get_serial_info(struct moxa_port *info,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002118 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002120 struct serial_struct tmp = {
2121 .type = info->type,
Alan Cox9de6a512008-07-16 21:56:02 +01002122 .line = info->port.tty->index,
2123 .flags = info->port.flags,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002124 .baud_base = 921600,
Alan Cox44b7d1b2008-07-16 21:57:18 +01002125 .close_delay = info->port.close_delay
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002126 };
2127 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
2129
2130
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002131static int moxa_set_serial_info(struct moxa_port *info,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002132 struct serial_struct __user *new_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
2134 struct serial_struct new_serial;
2135
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002136 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 return -EFAULT;
2138
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002139 if (new_serial.irq != 0 || new_serial.port != 0 ||
2140 new_serial.custom_divisor != 0 ||
2141 new_serial.baud_base != 921600)
2142 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
2144 if (!capable(CAP_SYS_ADMIN)) {
2145 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
Alan Cox9de6a512008-07-16 21:56:02 +01002146 (info->port.flags & ~ASYNC_USR_MASK)))
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002147 return -EPERM;
2148 } else
Alan Cox44b7d1b2008-07-16 21:57:18 +01002149 info->port.close_delay = new_serial.close_delay * HZ / 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
Alan Cox9de6a512008-07-16 21:56:02 +01002152 new_serial.flags |= (info->port.flags & ASYNC_FLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002154 MoxaSetFifo(info, new_serial.type == PORT_16550A);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 info->type = new_serial.type;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002157 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158}
2159
2160
2161
2162/*****************************************************************************
2163 * Static local functions: *
2164 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Jiri Slabyb4173f42008-04-30 00:53:40 -07002166static void MoxaSetFifo(struct moxa_port *port, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002168 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 if (!enable) {
2171 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2172 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2173 } else {
2174 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2175 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2176 }
2177}