blob: d3d7864e0c1ef566b86d9a5cfb72c45d2aaf5836 [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 Slaby08d01c72008-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
Alan Cox9e989662008-07-22 11:18:03 +0100377static int moxa_break_ctl(struct tty_struct *tty, int state)
Jiri Slaby74d7d972008-04-30 00:53:43 -0700378{
379 struct moxa_port *port = tty->driver_data;
380
381 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
382 Magic_code);
Alan Cox9e989662008-07-22 11:18:03 +0100383 return 0;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700384}
385
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700386static const struct tty_operations moxa_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 .open = moxa_open,
388 .close = moxa_close,
389 .write = moxa_write,
390 .write_room = moxa_write_room,
391 .flush_buffer = moxa_flush_buffer,
392 .chars_in_buffer = moxa_chars_in_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 .ioctl = moxa_ioctl,
394 .throttle = moxa_throttle,
395 .unthrottle = moxa_unthrottle,
396 .set_termios = moxa_set_termios,
397 .stop = moxa_stop,
398 .start = moxa_start,
399 .hangup = moxa_hangup,
Jiri Slaby74d7d972008-04-30 00:53:43 -0700400 .break_ctl = moxa_break_ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 .tiocmget = moxa_tiocmget,
402 .tiocmset = moxa_tiocmset,
403};
404
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800405static struct tty_driver *moxaDriver;
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800406static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
Ingo Molnar34af9462006-06-27 02:53:55 -0700407static DEFINE_SPINLOCK(moxa_lock);
Alan Cox33f0f882006-01-09 20:54:13 -0800408
Jiri Slaby74d7d972008-04-30 00:53:43 -0700409/*
410 * HW init
411 */
412
Jiri Slaby03718232008-04-30 00:53:39 -0700413static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
414{
415 switch (brd->boardType) {
416 case MOXA_BOARD_C218_ISA:
417 case MOXA_BOARD_C218_PCI:
418 if (model != 1)
419 goto err;
420 break;
421 case MOXA_BOARD_CP204J:
422 if (model != 3)
423 goto err;
424 break;
425 default:
426 if (model != 2)
427 goto err;
428 break;
429 }
430 return 0;
431err:
432 return -EINVAL;
433}
434
435static int moxa_check_fw(const void *ptr)
436{
437 const __le16 *lptr = ptr;
438
439 if (*lptr != cpu_to_le16(0x7980))
440 return -EINVAL;
441
442 return 0;
443}
444
445static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
446 size_t len)
447{
448 void __iomem *baseAddr = brd->basemem;
449 u16 tmp;
450
451 writeb(HW_reset, baseAddr + Control_reg); /* reset */
452 msleep(10);
453 memset_io(baseAddr, 0, 4096);
454 memcpy_toio(baseAddr, buf, len); /* download BIOS */
455 writeb(0, baseAddr + Control_reg); /* restart */
456
457 msleep(2000);
458
459 switch (brd->boardType) {
460 case MOXA_BOARD_C218_ISA:
461 case MOXA_BOARD_C218_PCI:
462 tmp = readw(baseAddr + C218_key);
463 if (tmp != C218_KeyCode)
464 goto err;
465 break;
466 case MOXA_BOARD_CP204J:
467 tmp = readw(baseAddr + C218_key);
468 if (tmp != CP204J_KeyCode)
469 goto err;
470 break;
471 default:
472 tmp = readw(baseAddr + C320_key);
473 if (tmp != C320_KeyCode)
474 goto err;
475 tmp = readw(baseAddr + C320_status);
476 if (tmp != STS_init) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700477 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
Jiri Slaby03718232008-04-30 00:53:39 -0700478 "module not found\n");
479 return -EIO;
480 }
481 break;
482 }
483
484 return 0;
485err:
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700486 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700487 return -EIO;
488}
489
490static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
491 size_t len)
492{
493 void __iomem *baseAddr = brd->basemem;
494
495 if (len < 7168) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700496 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700497 return -EINVAL;
498 }
499
500 writew(len - 7168 - 2, baseAddr + C320bapi_len);
501 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
502 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
503 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
504 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
505
506 return 0;
507}
508
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700509static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
Jiri Slaby03718232008-04-30 00:53:39 -0700510 size_t len)
511{
512 void __iomem *baseAddr = brd->basemem;
513 const u16 *uptr = ptr;
514 size_t wlen, len2, j;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700515 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
Jiri Slaby08d01c72008-04-30 00:53:47 -0700516 unsigned int i, retry;
Jiri Slaby03718232008-04-30 00:53:39 -0700517 u16 usum, keycode;
518
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700519 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
520 C218_KeyCode;
521
522 switch (brd->boardType) {
523 case MOXA_BOARD_CP204J:
524 case MOXA_BOARD_C218_ISA:
525 case MOXA_BOARD_C218_PCI:
526 key = C218_key;
527 loadbuf = C218_LoadBuf;
528 loadlen = C218DLoad_len;
529 checksum = C218check_sum;
530 checksum_ok = C218chksum_ok;
531 break;
532 default:
533 key = C320_key;
534 keycode = C320_KeyCode;
535 loadbuf = C320_LoadBuf;
536 loadlen = C320DLoad_len;
537 checksum = C320check_sum;
538 checksum_ok = C320chksum_ok;
539 break;
540 }
541
Jiri Slaby03718232008-04-30 00:53:39 -0700542 usum = 0;
543 wlen = len >> 1;
544 for (i = 0; i < wlen; i++)
545 usum += le16_to_cpu(uptr[i]);
546 retry = 0;
547 do {
548 wlen = len >> 1;
549 j = 0;
550 while (wlen) {
551 len2 = (wlen > 2048) ? 2048 : wlen;
552 wlen -= len2;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700553 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
Jiri Slaby03718232008-04-30 00:53:39 -0700554 j += len2 << 1;
555
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700556 writew(len2, baseAddr + loadlen);
557 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700558 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700559 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700560 break;
561 msleep(10);
562 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700563 if (readw(baseAddr + key) != keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700564 return -EIO;
565 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700566 writew(0, baseAddr + loadlen);
567 writew(usum, baseAddr + checksum);
568 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700569 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700570 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700571 break;
572 msleep(10);
573 }
574 retry++;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700575 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
576 if (readb(baseAddr + checksum_ok) != 1)
Jiri Slaby03718232008-04-30 00:53:39 -0700577 return -EIO;
578
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700579 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700580 for (i = 0; i < 600; i++) {
581 if (readw(baseAddr + Magic_no) == Magic_code)
582 break;
583 msleep(10);
584 }
585 if (readw(baseAddr + Magic_no) != Magic_code)
586 return -EIO;
587
Jiri Slaby08d01c72008-04-30 00:53:47 -0700588 if (MOXA_IS_320(brd)) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700589 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
590 writew(0x3800, baseAddr + TMS320_PORT1);
591 writew(0x3900, baseAddr + TMS320_PORT2);
592 writew(28499, baseAddr + TMS320_CLOCK);
593 } else {
594 writew(0x3200, baseAddr + TMS320_PORT1);
595 writew(0x3400, baseAddr + TMS320_PORT2);
596 writew(19999, baseAddr + TMS320_CLOCK);
597 }
Jiri Slaby03718232008-04-30 00:53:39 -0700598 }
599 writew(1, baseAddr + Disable_IRQ);
600 writew(0, baseAddr + Magic_no);
601 for (i = 0; i < 500; i++) {
602 if (readw(baseAddr + Magic_no) == Magic_code)
603 break;
604 msleep(10);
605 }
606 if (readw(baseAddr + Magic_no) != Magic_code)
607 return -EIO;
608
Jiri Slaby08d01c72008-04-30 00:53:47 -0700609 if (MOXA_IS_320(brd)) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700610 j = readw(baseAddr + Module_cnt);
611 if (j <= 0)
612 return -EIO;
613 brd->numPorts = j * 8;
614 writew(j, baseAddr + Module_no);
615 writew(0, baseAddr + Magic_no);
616 for (i = 0; i < 600; i++) {
617 if (readw(baseAddr + Magic_no) == Magic_code)
618 break;
619 msleep(10);
620 }
621 if (readw(baseAddr + Magic_no) != Magic_code)
622 return -EIO;
Jiri Slaby03718232008-04-30 00:53:39 -0700623 }
Jiri Slaby03718232008-04-30 00:53:39 -0700624 brd->intNdx = baseAddr + IRQindex;
625 brd->intPend = baseAddr + IRQpending;
626 brd->intTable = baseAddr + IRQtable;
627
628 return 0;
629}
630
631static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
632 size_t len)
633{
634 void __iomem *ofsAddr, *baseAddr = brd->basemem;
635 struct moxa_port *port;
636 int retval, i;
637
638 if (len % 2) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700639 printk(KERN_ERR "MOXA: bios length is not even\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700640 return -EINVAL;
641 }
642
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700643 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
644 if (retval)
645 return retval;
646
Jiri Slaby03718232008-04-30 00:53:39 -0700647 switch (brd->boardType) {
648 case MOXA_BOARD_C218_ISA:
649 case MOXA_BOARD_C218_PCI:
650 case MOXA_BOARD_CP204J:
Jiri Slaby03718232008-04-30 00:53:39 -0700651 port = brd->ports;
652 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700653 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700654 port->DCDState = 0;
655 port->tableAddr = baseAddr + Extern_table +
656 Extern_size * i;
657 ofsAddr = port->tableAddr;
658 writew(C218rx_mask, ofsAddr + RX_mask);
659 writew(C218tx_mask, ofsAddr + TX_mask);
660 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
661 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
662
663 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
664 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
665
666 }
667 break;
668 default:
Jiri Slaby03718232008-04-30 00:53:39 -0700669 port = brd->ports;
670 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700671 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700672 port->DCDState = 0;
673 port->tableAddr = baseAddr + Extern_table +
674 Extern_size * i;
675 ofsAddr = port->tableAddr;
676 switch (brd->numPorts) {
677 case 8:
678 writew(C320p8rx_mask, ofsAddr + RX_mask);
679 writew(C320p8tx_mask, ofsAddr + TX_mask);
680 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
681 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
682 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
683 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
684
685 break;
686 case 16:
687 writew(C320p16rx_mask, ofsAddr + RX_mask);
688 writew(C320p16tx_mask, ofsAddr + TX_mask);
689 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
690 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
691 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
692 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
693 break;
694
695 case 24:
696 writew(C320p24rx_mask, ofsAddr + RX_mask);
697 writew(C320p24tx_mask, ofsAddr + TX_mask);
698 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
699 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
700 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
701 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
702 break;
703 case 32:
704 writew(C320p32rx_mask, ofsAddr + RX_mask);
705 writew(C320p32tx_mask, ofsAddr + TX_mask);
706 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
707 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
708 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
709 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
710 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
711 break;
712 }
713 }
714 break;
715 }
Jiri Slaby03718232008-04-30 00:53:39 -0700716 return 0;
717}
718
719static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
720{
David Howells2bca76e2008-07-08 17:37:15 +0100721 const void *ptr = fw->data;
Jiri Slaby03718232008-04-30 00:53:39 -0700722 char rsn[64];
723 u16 lens[5];
724 size_t len;
725 unsigned int a, lenp, lencnt;
726 int ret = -EINVAL;
727 struct {
728 __le32 magic; /* 0x34303430 */
729 u8 reserved1[2];
730 u8 type; /* UNIX = 3 */
731 u8 model; /* C218T=1, C320T=2, CP204=3 */
732 u8 reserved2[8];
733 __le16 len[5];
David Howells2bca76e2008-07-08 17:37:15 +0100734 } const *hdr = ptr;
Jiri Slaby03718232008-04-30 00:53:39 -0700735
736 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
737
738 if (fw->size < MOXA_FW_HDRLEN) {
739 strcpy(rsn, "too short (even header won't fit)");
740 goto err;
741 }
742 if (hdr->magic != cpu_to_le32(0x30343034)) {
743 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
744 goto err;
745 }
746 if (hdr->type != 3) {
747 sprintf(rsn, "not for linux, type is %u", hdr->type);
748 goto err;
749 }
750 if (moxa_check_fw_model(brd, hdr->model)) {
751 sprintf(rsn, "not for this card, model is %u", hdr->model);
752 goto err;
753 }
754
755 len = MOXA_FW_HDRLEN;
756 lencnt = hdr->model == 2 ? 5 : 3;
757 for (a = 0; a < ARRAY_SIZE(lens); a++) {
758 lens[a] = le16_to_cpu(hdr->len[a]);
759 if (lens[a] && len + lens[a] <= fw->size &&
760 moxa_check_fw(&fw->data[len]))
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700761 printk(KERN_WARNING "MOXA firmware: unexpected input "
Jiri Slaby03718232008-04-30 00:53:39 -0700762 "at offset %u, but going on\n", (u32)len);
763 if (!lens[a] && a < lencnt) {
764 sprintf(rsn, "too few entries in fw file");
765 goto err;
766 }
767 len += lens[a];
768 }
769
770 if (len != fw->size) {
771 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
772 (u32)len);
773 goto err;
774 }
775
776 ptr += MOXA_FW_HDRLEN;
777 lenp = 0; /* bios */
778
779 strcpy(rsn, "read above");
780
781 ret = moxa_load_bios(brd, ptr, lens[lenp]);
782 if (ret)
783 goto err;
784
785 /* we skip the tty section (lens[1]), since we don't need it */
786 ptr += lens[lenp] + lens[lenp + 1];
787 lenp += 2; /* comm */
788
789 if (hdr->model == 2) {
790 ret = moxa_load_320b(brd, ptr, lens[lenp]);
791 if (ret)
792 goto err;
793 /* skip another tty */
794 ptr += lens[lenp] + lens[lenp + 1];
795 lenp += 2;
796 }
797
798 ret = moxa_load_code(brd, ptr, lens[lenp]);
799 if (ret)
800 goto err;
801
802 return 0;
803err:
804 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
805 return ret;
806}
807
808static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
809{
810 const struct firmware *fw;
811 const char *file;
Jiri Slaby810ab092008-04-30 00:53:41 -0700812 struct moxa_port *p;
813 unsigned int i;
Jiri Slaby03718232008-04-30 00:53:39 -0700814 int ret;
815
Jiri Slaby810ab092008-04-30 00:53:41 -0700816 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
817 GFP_KERNEL);
818 if (brd->ports == NULL) {
819 printk(KERN_ERR "cannot allocate memory for ports\n");
820 ret = -ENOMEM;
821 goto err;
822 }
823
824 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
Alan Cox9de6a512008-07-16 21:56:02 +0100825 tty_port_init(&p->port);
Alan Cox44b7d1b2008-07-16 21:57:18 +0100826 p->type = PORT_16550A;
827 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Jiri Slaby810ab092008-04-30 00:53:41 -0700828 }
829
Jiri Slaby03718232008-04-30 00:53:39 -0700830 switch (brd->boardType) {
831 case MOXA_BOARD_C218_ISA:
832 case MOXA_BOARD_C218_PCI:
833 file = "c218tunx.cod";
834 break;
835 case MOXA_BOARD_CP204J:
836 file = "cp204unx.cod";
837 break;
838 default:
839 file = "c320tunx.cod";
840 break;
841 }
842
843 ret = request_firmware(&fw, file, dev);
844 if (ret) {
Jiri Slabyec09cd52008-04-30 00:53:49 -0700845 printk(KERN_ERR "MOXA: request_firmware failed. Make sure "
846 "you've placed '%s' file into your firmware "
847 "loader directory (e.g. /lib/firmware)\n",
848 file);
Jiri Slaby810ab092008-04-30 00:53:41 -0700849 goto err_free;
Jiri Slaby03718232008-04-30 00:53:39 -0700850 }
851
852 ret = moxa_load_fw(brd, fw);
853
854 release_firmware(fw);
Jiri Slaby810ab092008-04-30 00:53:41 -0700855
856 if (ret)
857 goto err_free;
858
Jiri Slaby2a541342008-04-30 00:53:45 -0700859 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700860 brd->ready = 1;
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700861 if (!timer_pending(&moxaTimer))
862 mod_timer(&moxaTimer, jiffies + HZ / 50);
Jiri Slaby2a541342008-04-30 00:53:45 -0700863 spin_unlock_bh(&moxa_lock);
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700864
Jiri Slaby810ab092008-04-30 00:53:41 -0700865 return 0;
866err_free:
867 kfree(brd->ports);
868err:
Jiri Slaby03718232008-04-30 00:53:39 -0700869 return ret;
870}
871
Jiri Slaby810ab092008-04-30 00:53:41 -0700872static void moxa_board_deinit(struct moxa_board_conf *brd)
873{
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700874 unsigned int a, opened;
875
876 mutex_lock(&moxa_openlock);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700877 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700878 brd->ready = 0;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700879 spin_unlock_bh(&moxa_lock);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700880
881 /* pci hot-un-plug support */
882 for (a = 0; a < brd->numPorts; a++)
Alan Cox9de6a512008-07-16 21:56:02 +0100883 if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
884 tty_hangup(brd->ports[a].port.tty);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700885 while (1) {
886 opened = 0;
887 for (a = 0; a < brd->numPorts; a++)
Alan Cox9de6a512008-07-16 21:56:02 +0100888 if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700889 opened++;
890 mutex_unlock(&moxa_openlock);
891 if (!opened)
892 break;
893 msleep(50);
894 mutex_lock(&moxa_openlock);
895 }
896
Jiri Slaby810ab092008-04-30 00:53:41 -0700897 iounmap(brd->basemem);
898 brd->basemem = NULL;
899 kfree(brd->ports);
900}
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902#ifdef CONFIG_PCI
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800903static int __devinit moxa_pci_probe(struct pci_dev *pdev,
904 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800906 struct moxa_board_conf *board;
907 unsigned int i;
908 int board_type = ent->driver_data;
909 int retval;
910
911 retval = pci_enable_device(pdev);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700912 if (retval) {
913 dev_err(&pdev->dev, "can't enable pci device\n");
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800914 goto err;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700915 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800916
917 for (i = 0; i < MAX_BOARDS; i++)
918 if (moxa_boards[i].basemem == NULL)
919 break;
920
921 retval = -ENODEV;
922 if (i >= MAX_BOARDS) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700923 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800924 "found. Board is ignored.\n", MAX_BOARDS);
925 goto err;
926 }
927
928 board = &moxa_boards[i];
Jiri Slabye46a5e32008-04-30 00:53:37 -0700929
930 retval = pci_request_region(pdev, 2, "moxa-base");
931 if (retval) {
932 dev_err(&pdev->dev, "can't request pci region 2\n");
933 goto err;
934 }
935
Alan Cox24cb2332008-04-30 00:54:19 -0700936 board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700937 if (board->basemem == NULL) {
938 dev_err(&pdev->dev, "can't remap io space 2\n");
Jiri Slabye46a5e32008-04-30 00:53:37 -0700939 goto err_reg;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700940 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 board->boardType = board_type;
943 switch (board_type) {
944 case MOXA_BOARD_C218_ISA:
945 case MOXA_BOARD_C218_PCI:
946 board->numPorts = 8;
947 break;
948
949 case MOXA_BOARD_CP204J:
950 board->numPorts = 4;
951 break;
952 default:
953 board->numPorts = 0;
954 break;
955 }
956 board->busType = MOXA_BUS_TYPE_PCI;
Jiri Slabya784bf72007-02-10 01:45:36 -0800957
Jiri Slaby03718232008-04-30 00:53:39 -0700958 retval = moxa_init_board(board, &pdev->dev);
959 if (retval)
960 goto err_base;
961
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800962 pci_set_drvdata(pdev, board);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Jiri Slabybb9f9102008-04-30 00:53:48 -0700964 dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n",
965 moxa_brdname[board_type - 1], board->numPorts);
966
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700967 return 0;
Jiri Slaby03718232008-04-30 00:53:39 -0700968err_base:
969 iounmap(board->basemem);
970 board->basemem = NULL;
Jiri Slabye46a5e32008-04-30 00:53:37 -0700971err_reg:
972 pci_release_region(pdev, 2);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800973err:
974 return retval;
975}
976
977static void __devexit moxa_pci_remove(struct pci_dev *pdev)
978{
979 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
980
Jiri Slaby810ab092008-04-30 00:53:41 -0700981 moxa_board_deinit(brd);
982
Jiri Slabye46a5e32008-04-30 00:53:37 -0700983 pci_release_region(pdev, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
Jiri Slabya784bf72007-02-10 01:45:36 -0800985
986static struct pci_driver moxa_pci_driver = {
987 .name = "moxa",
988 .id_table = moxa_pcibrds,
989 .probe = moxa_pci_probe,
990 .remove = __devexit_p(moxa_pci_remove)
991};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992#endif /* CONFIG_PCI */
993
994static int __init moxa_init(void)
995{
Jiri Slaby810ab092008-04-30 00:53:41 -0700996 unsigned int isabrds = 0;
Jiri Slabyd353eca2008-04-30 00:53:37 -0700997 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700999 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
1000 MOXA_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
1002 if (!moxaDriver)
1003 return -ENOMEM;
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 moxaDriver->owner = THIS_MODULE;
Sergey Vlasov9b4e3b12005-09-03 16:26:49 +01001006 moxaDriver->name = "ttyMX";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 moxaDriver->major = ttymajor;
1008 moxaDriver->minor_start = 0;
1009 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1010 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1011 moxaDriver->init_termios = tty_std_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Alan Cox606d0992006-12-08 02:38:45 -08001013 moxaDriver->init_termios.c_ispeed = 9600;
1014 moxaDriver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
1016 tty_set_operations(moxaDriver, &moxa_ops);
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (tty_register_driver(moxaDriver)) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001019 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 put_tty_driver(moxaDriver);
1021 return -1;
1022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Jiri Slabyd353eca2008-04-30 00:53:37 -07001024 /* Find the boards defined from module args. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -07001026 {
1027 struct moxa_board_conf *brd = moxa_boards;
Jiri Slaby810ab092008-04-30 00:53:41 -07001028 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 for (i = 0; i < MAX_BOARDS; i++) {
Jiri Slabyd353eca2008-04-30 00:53:37 -07001030 if (!baseaddr[i])
1031 break;
1032 if (type[i] == MOXA_BOARD_C218_ISA ||
1033 type[i] == MOXA_BOARD_C320_ISA) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001034 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001035 isabrds + 1, moxa_brdname[type[i] - 1],
1036 baseaddr[i]);
1037 brd->boardType = type[i];
1038 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1039 numports[i];
1040 brd->busType = MOXA_BUS_TYPE_ISA;
Alan Cox24cb2332008-04-30 00:54:19 -07001041 brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
Jiri Slabyd353eca2008-04-30 00:53:37 -07001042 if (!brd->basemem) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001043 printk(KERN_ERR "MOXA: can't remap %lx\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001044 baseaddr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 continue;
1046 }
Jiri Slaby03718232008-04-30 00:53:39 -07001047 if (moxa_init_board(brd, NULL)) {
1048 iounmap(brd->basemem);
1049 brd->basemem = NULL;
1050 continue;
1051 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001052
Jiri Slabybb9f9102008-04-30 00:53:48 -07001053 printk(KERN_INFO "MOXA isa board found at 0x%.8lu and "
1054 "ready (%u ports, firmware loaded)\n",
1055 baseaddr[i], brd->numPorts);
1056
Jiri Slabyd353eca2008-04-30 00:53:37 -07001057 brd++;
1058 isabrds++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001065 retval = pci_register_driver(&moxa_pci_driver);
1066 if (retval) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001067 printk(KERN_ERR "Can't register MOXA pci driver!\n");
Jiri Slabyd353eca2008-04-30 00:53:37 -07001068 if (isabrds)
Jiri Slabya784bf72007-02-10 01:45:36 -08001069 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001072
Jiri Slabya784bf72007-02-10 01:45:36 -08001073 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
1076static void __exit moxa_exit(void)
1077{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001078 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001080#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001081 pci_unregister_driver(&moxa_pci_driver);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001082#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001083
Jiri Slaby810ab092008-04-30 00:53:41 -07001084 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1085 if (moxa_boards[i].ready)
1086 moxa_board_deinit(&moxa_boards[i]);
Jiri Slaby2a541342008-04-30 00:53:45 -07001087
1088 del_timer_sync(&moxaTimer);
1089
1090 if (tty_unregister_driver(moxaDriver))
1091 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1092 "serial driver\n");
1093 put_tty_driver(moxaDriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
1095
1096module_init(moxa_init);
1097module_exit(moxa_exit);
1098
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001099static void moxa_close_port(struct moxa_port *ch)
1100{
1101 moxa_shut_down(ch);
1102 MoxaPortFlushData(ch, 2);
Alan Cox9de6a512008-07-16 21:56:02 +01001103 ch->port.flags &= ~ASYNC_NORMAL_ACTIVE;
1104 ch->port.tty->driver_data = NULL;
1105 ch->port.tty = NULL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001106}
1107
1108static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
1109 struct moxa_port *ch)
1110{
1111 DEFINE_WAIT(wait);
1112 int retval = 0;
1113 u8 dcd;
1114
1115 while (1) {
Alan Cox9de6a512008-07-16 21:56:02 +01001116 prepare_to_wait(&ch->port.open_wait, &wait, TASK_INTERRUPTIBLE);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001117 if (tty_hung_up_p(filp)) {
1118#ifdef SERIAL_DO_RESTART
1119 retval = -ERESTARTSYS;
1120#else
1121 retval = -EAGAIN;
1122#endif
1123 break;
1124 }
1125 spin_lock_bh(&moxa_lock);
1126 dcd = ch->DCDState;
1127 spin_unlock_bh(&moxa_lock);
1128 if (dcd)
1129 break;
1130
1131 if (signal_pending(current)) {
1132 retval = -ERESTARTSYS;
1133 break;
1134 }
1135 schedule();
1136 }
Alan Cox9de6a512008-07-16 21:56:02 +01001137 finish_wait(&ch->port.open_wait, &wait);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001138
1139 return retval;
1140}
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142static int moxa_open(struct tty_struct *tty, struct file *filp)
1143{
Jiri Slaby810ab092008-04-30 00:53:41 -07001144 struct moxa_board_conf *brd;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001145 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 int port;
1147 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Jiri Slaby11324ed2007-02-10 01:45:31 -08001149 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (port == MAX_PORTS) {
Jiri Slaby74d7d972008-04-30 00:53:43 -07001151 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001153 if (mutex_lock_interruptible(&moxa_openlock))
1154 return -ERESTARTSYS;
Jiri Slaby810ab092008-04-30 00:53:41 -07001155 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001156 if (!brd->ready) {
1157 mutex_unlock(&moxa_openlock);
Jiri Slaby810ab092008-04-30 00:53:41 -07001158 return -ENODEV;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Jiri Slaby810ab092008-04-30 00:53:41 -07001161 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
Alan Cox9de6a512008-07-16 21:56:02 +01001162 ch->port.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 tty->driver_data = ch;
Alan Cox9de6a512008-07-16 21:56:02 +01001164 ch->port.tty = tty;
1165 if (!(ch->port.flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 ch->statusflags = 0;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001167 moxa_set_tty_param(tty, tty->termios);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001168 MoxaPortLineCtrl(ch, 1, 1);
1169 MoxaPortEnable(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001170 MoxaSetFifo(ch, ch->type == PORT_16550A);
Alan Cox9de6a512008-07-16 21:56:02 +01001171 ch->port.flags |= ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001173 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001175 retval = 0;
1176 if (!(filp->f_flags & O_NONBLOCK) && !C_CLOCAL(tty))
1177 retval = moxa_block_till_ready(tty, filp, ch);
1178 mutex_lock(&moxa_openlock);
1179 if (retval) {
Alan Cox9de6a512008-07-16 21:56:02 +01001180 if (ch->port.count) /* 0 means already hung up... */
1181 if (--ch->port.count == 0)
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001182 moxa_close_port(ch);
1183 } else
Alan Cox9de6a512008-07-16 21:56:02 +01001184 ch->port.flags |= ASYNC_NORMAL_ACTIVE;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001185 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001187 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
1190static void moxa_close(struct tty_struct *tty, struct file *filp)
1191{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001192 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 int port;
1194
Jiri Slaby11324ed2007-02-10 01:45:31 -08001195 port = tty->index;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001196 if (port == MAX_PORTS || tty_hung_up_p(filp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001199 mutex_lock(&moxa_openlock);
1200 ch = tty->driver_data;
1201 if (ch == NULL)
1202 goto unlock;
Alan Cox9de6a512008-07-16 21:56:02 +01001203 if (tty->count == 1 && ch->port.count != 1) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001204 printk(KERN_WARNING "moxa_close: bad serial port count; "
Alan Cox9de6a512008-07-16 21:56:02 +01001205 "tty->count is 1, ch->port.count is %d\n", ch->port.count);
1206 ch->port.count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
Alan Cox9de6a512008-07-16 21:56:02 +01001208 if (--ch->port.count < 0) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001209 printk(KERN_WARNING "moxa_close: bad serial port count, "
1210 "device=%s\n", tty->name);
Alan Cox9de6a512008-07-16 21:56:02 +01001211 ch->port.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
Alan Cox9de6a512008-07-16 21:56:02 +01001213 if (ch->port.count)
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001214 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 ch->cflag = tty->termios->c_cflag;
Alan Cox9de6a512008-07-16 21:56:02 +01001217 if (ch->port.flags & ASYNC_INITIALIZED) {
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001218 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001222 moxa_close_port(ch);
1223unlock:
1224 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
1227static int moxa_write(struct tty_struct *tty,
1228 const unsigned char *buf, int count)
1229{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001230 struct moxa_port *ch = tty->driver_data;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001231 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (ch == NULL)
Jiri Slabyb4173f42008-04-30 00:53:40 -07001234 return 0;
Alan Cox33f0f882006-01-09 20:54:13 -08001235
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001236 spin_lock_bh(&moxa_lock);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001237 len = MoxaPortWriteData(ch, buf, count);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001238 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 ch->statusflags |= LOWWAIT;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001241 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
1244static int moxa_write_room(struct tty_struct *tty)
1245{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001246 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 if (tty->stopped)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001249 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001250 ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if (ch == NULL)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001252 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001253 return MoxaPortTxFree(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
1255
1256static void moxa_flush_buffer(struct tty_struct *tty)
1257{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001258 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 if (ch == NULL)
1261 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001262 MoxaPortFlushData(ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 tty_wakeup(tty);
1264}
1265
1266static int moxa_chars_in_buffer(struct tty_struct *tty)
1267{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001268 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 /*
1272 * Sigh...I have to check if driver_data is NULL here, because
1273 * if an open() fails, the TTY subsystem eventually calls
1274 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1275 * routine. And since the open() failed, we return 0 here. TDJ
1276 */
1277 if (ch == NULL)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001278 return 0;
Alan Cox978e5952008-04-30 00:53:59 -07001279 lock_kernel();
Jiri Slabyb4173f42008-04-30 00:53:40 -07001280 chars = MoxaPortTxQueue(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (chars) {
1282 /*
1283 * Make it possible to wakeup anything waiting for output
1284 * in tty_ioctl.c, etc.
1285 */
1286 if (!(ch->statusflags & EMPTYWAIT))
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001287 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
Alan Cox978e5952008-04-30 00:53:59 -07001289 unlock_kernel();
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001290 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1294{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001295 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 int flag = 0, dtr, rts;
1297
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001298 mutex_lock(&moxa_openlock);
1299 ch = tty->driver_data;
1300 if (!ch) {
1301 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -07001302 return -EINVAL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Jiri Slabyb4173f42008-04-30 00:53:40 -07001305 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (dtr)
1307 flag |= TIOCM_DTR;
1308 if (rts)
1309 flag |= TIOCM_RTS;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001310 dtr = MoxaPortLineStatus(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (dtr & 1)
1312 flag |= TIOCM_CTS;
1313 if (dtr & 2)
1314 flag |= TIOCM_DSR;
1315 if (dtr & 4)
1316 flag |= TIOCM_CD;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001317 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return flag;
1319}
1320
1321static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1322 unsigned int set, unsigned int clear)
1323{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001324 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 int port;
1326 int dtr, rts;
1327
Jiri Slaby11324ed2007-02-10 01:45:31 -08001328 port = tty->index;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001329 mutex_lock(&moxa_openlock);
1330 ch = tty->driver_data;
1331 if (!ch) {
1332 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -07001333 return -EINVAL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Jiri Slabyb4173f42008-04-30 00:53:40 -07001336 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (set & TIOCM_RTS)
1338 rts = 1;
1339 if (set & TIOCM_DTR)
1340 dtr = 1;
1341 if (clear & TIOCM_RTS)
1342 rts = 0;
1343 if (clear & TIOCM_DTR)
1344 dtr = 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001345 MoxaPortLineCtrl(ch, dtr, rts);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001346 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return 0;
1348}
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350static void moxa_throttle(struct tty_struct *tty)
1351{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001352 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 ch->statusflags |= THROTTLE;
1355}
1356
1357static void moxa_unthrottle(struct tty_struct *tty)
1358{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001359 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 ch->statusflags &= ~THROTTLE;
1362}
1363
1364static void moxa_set_termios(struct tty_struct *tty,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001365 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001367 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 if (ch == NULL)
1370 return;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001371 moxa_set_tty_param(tty, old_termios);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001372 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
Alan Cox9de6a512008-07-16 21:56:02 +01001373 wake_up_interruptible(&ch->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
1376static void moxa_stop(struct tty_struct *tty)
1377{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001378 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 if (ch == NULL)
1381 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001382 MoxaPortTxDisable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 ch->statusflags |= TXSTOPPED;
1384}
1385
1386
1387static void moxa_start(struct tty_struct *tty)
1388{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001389 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 if (ch == NULL)
1392 return;
1393
1394 if (!(ch->statusflags & TXSTOPPED))
1395 return;
1396
Jiri Slabyb4173f42008-04-30 00:53:40 -07001397 MoxaPortTxEnable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 ch->statusflags &= ~TXSTOPPED;
1399}
1400
1401static void moxa_hangup(struct tty_struct *tty)
1402{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001403 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001405 mutex_lock(&moxa_openlock);
1406 ch = tty->driver_data;
1407 if (ch == NULL) {
1408 mutex_unlock(&moxa_openlock);
1409 return;
1410 }
Alan Cox9de6a512008-07-16 21:56:02 +01001411 ch->port.count = 0;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001412 moxa_close_port(ch);
1413 mutex_unlock(&moxa_openlock);
1414
Alan Cox9de6a512008-07-16 21:56:02 +01001415 wake_up_interruptible(&ch->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
1417
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001418static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1419{
1420 dcd = !!dcd;
1421
Alan Cox9de6a512008-07-16 21:56:02 +01001422 if (dcd != p->DCDState && p->port.tty && C_CLOCAL(p->port.tty)) {
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001423 if (!dcd)
Alan Cox9de6a512008-07-16 21:56:02 +01001424 tty_hangup(p->port.tty);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001425 }
1426 p->DCDState = dcd;
1427}
1428
1429static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1430 u16 __iomem *ip)
1431{
Alan Cox9de6a512008-07-16 21:56:02 +01001432 struct tty_struct *tty = p->port.tty;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001433 void __iomem *ofsAddr;
Alan Cox9de6a512008-07-16 21:56:02 +01001434 unsigned int inited = p->port.flags & ASYNC_INITIALIZED;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001435 u16 intr;
1436
1437 if (tty) {
1438 if ((p->statusflags & EMPTYWAIT) &&
1439 MoxaPortTxQueue(p) == 0) {
1440 p->statusflags &= ~EMPTYWAIT;
1441 tty_wakeup(tty);
1442 }
1443 if ((p->statusflags & LOWWAIT) && !tty->stopped &&
1444 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1445 p->statusflags &= ~LOWWAIT;
1446 tty_wakeup(tty);
1447 }
1448
1449 if (inited && !(p->statusflags & THROTTLE) &&
1450 MoxaPortRxQueue(p) > 0) { /* RX */
1451 MoxaPortReadData(p);
1452 tty_schedule_flip(tty);
1453 }
1454 } else {
1455 p->statusflags &= ~EMPTYWAIT;
1456 MoxaPortFlushData(p, 0); /* flush RX */
1457 }
1458
1459 if (!handle) /* nothing else to do */
1460 return 0;
1461
1462 intr = readw(ip); /* port irq status */
1463 if (intr == 0)
1464 return 0;
1465
1466 writew(0, ip); /* ACK port */
1467 ofsAddr = p->tableAddr;
1468 if (intr & IntrTx) /* disable tx intr */
1469 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1470 ofsAddr + HostStat);
1471
1472 if (!inited)
1473 return 0;
1474
1475 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1476 tty_insert_flip_char(tty, 0, TTY_BREAK);
1477 tty_schedule_flip(tty);
1478 }
1479
1480 if (intr & IntrLine)
1481 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1482
1483 return 0;
1484}
1485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486static void moxa_poll(unsigned long ignored)
1487{
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001488 struct moxa_board_conf *brd;
1489 u16 __iomem *ip;
Jiri Slaby2a541342008-04-30 00:53:45 -07001490 unsigned int card, port, served = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001492 spin_lock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 for (card = 0; card < MAX_BOARDS; card++) {
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001494 brd = &moxa_boards[card];
1495 if (!brd->ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 continue;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001497
Jiri Slaby2a541342008-04-30 00:53:45 -07001498 served++;
1499
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001500 ip = NULL;
1501 if (readb(brd->intPend) == 0xff)
1502 ip = brd->intTable + readb(brd->intNdx);
1503
1504 for (port = 0; port < brd->numPorts; port++)
1505 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1506
1507 if (ip)
1508 writeb(0, brd->intPend); /* ACK */
1509
1510 if (moxaLowWaterChk) {
1511 struct moxa_port *p = brd->ports;
1512 for (port = 0; port < brd->numPorts; port++, p++)
1513 if (p->lowChkFlag) {
1514 p->lowChkFlag = 0;
1515 moxa_low_water_check(p->tableAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
1518 }
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001519 moxaLowWaterChk = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Jiri Slaby2a541342008-04-30 00:53:45 -07001521 if (served)
1522 mod_timer(&moxaTimer, jiffies + HZ / 50);
1523 spin_unlock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
1526/******************************************************************************/
1527
Alan Coxdb1acaa2008-02-08 04:18:43 -08001528static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001530 register struct ktermios *ts = tty->termios;
1531 struct moxa_port *ch = tty->driver_data;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001532 int rts, cts, txflow, rxflow, xany, baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 rts = cts = txflow = rxflow = xany = 0;
1535 if (ts->c_cflag & CRTSCTS)
1536 rts = cts = 1;
1537 if (ts->c_iflag & IXON)
1538 txflow = 1;
1539 if (ts->c_iflag & IXOFF)
1540 rxflow = 1;
1541 if (ts->c_iflag & IXANY)
1542 xany = 1;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001543
1544 /* Clear the features we don't support */
1545 ts->c_cflag &= ~CMSPAR;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001546 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1547 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
Alan Coxdb1acaa2008-02-08 04:18:43 -08001548 if (baud == -1)
1549 baud = tty_termios_baud_rate(old_termios);
1550 /* Not put the baud rate into the termios data */
1551 tty_encode_baud_rate(tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
1553
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001554static void moxa_setup_empty_event(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001556 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001558 spin_lock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 ch->statusflags |= EMPTYWAIT;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001560 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
1562
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001563static void moxa_shut_down(struct moxa_port *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
Alan Cox9de6a512008-07-16 21:56:02 +01001565 struct tty_struct *tp = ch->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Alan Cox9de6a512008-07-16 21:56:02 +01001567 if (!(ch->port.flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 return;
1569
Jiri Slabyb4173f42008-04-30 00:53:40 -07001570 MoxaPortDisable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 /*
1573 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1574 */
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001575 if (C_HUPCL(tp))
Jiri Slabyb4173f42008-04-30 00:53:40 -07001576 MoxaPortLineCtrl(ch, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001578 spin_lock_bh(&moxa_lock);
Alan Cox9de6a512008-07-16 21:56:02 +01001579 ch->port.flags &= ~ASYNC_INITIALIZED;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001580 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583/*****************************************************************************
1584 * Driver level functions: *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Jiri Slabyb4173f42008-04-30 00:53:40 -07001587static void MoxaPortFlushData(struct moxa_port *port, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
1589 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001590 if (mode < 0 || mode > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001592 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 moxafunc(ofsAddr, FC_FlushQueue, mode);
1594 if (mode != 1) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001595 port->lowChkFlag = 0;
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001596 moxa_low_water_check(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 }
1598}
1599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600/*
1601 * Moxa Port Number Description:
1602 *
1603 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1604 * the port number using in MOXA driver functions will be 0 to 31 for
1605 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1606 * to 127 for fourth. For example, if you setup three MOXA boards,
1607 * first board is C218, second board is C320-16 and third board is
1608 * C320-32. The port number of first board (C218 - 8 ports) is from
1609 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1610 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1611 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1612 * 127 will be invalid.
1613 *
1614 *
1615 * Moxa Functions Description:
1616 *
1617 * Function 1: Driver initialization routine, this routine must be
1618 * called when initialized driver.
1619 * Syntax:
1620 * void MoxaDriverInit();
1621 *
1622 *
1623 * Function 2: Moxa driver private IOCTL command processing.
1624 * Syntax:
1625 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1626 *
1627 * unsigned int cmd : IOCTL command
1628 * unsigned long arg : IOCTL argument
1629 * int port : port number (0 - 127)
1630 *
1631 * return: 0 (OK)
1632 * -EINVAL
1633 * -ENOIOCTLCMD
1634 *
1635 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 * Function 6: Enable this port to start Tx/Rx data.
1637 * Syntax:
1638 * void MoxaPortEnable(int port);
1639 * int port : port number (0 - 127)
1640 *
1641 *
1642 * Function 7: Disable this port
1643 * Syntax:
1644 * void MoxaPortDisable(int port);
1645 * int port : port number (0 - 127)
1646 *
1647 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 * Function 10: Setting baud rate of this port.
1649 * Syntax:
Jiri Slaby08d01c72008-04-30 00:53:47 -07001650 * speed_t MoxaPortSetBaud(int port, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 * int port : port number (0 - 127)
1652 * long baud : baud rate (50 - 115200)
1653 *
1654 * return: 0 : this port is invalid or baud < 50
1655 * 50 - 115200 : the real baud rate set to the port, if
1656 * the argument baud is large than maximun
1657 * available baud rate, the real setting
1658 * baud rate will be the maximun baud rate.
1659 *
1660 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 * Function 12: Configure the port.
1662 * Syntax:
Alan Cox606d0992006-12-08 02:38:45 -08001663 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 * int port : port number (0 - 127)
Alan Cox606d0992006-12-08 02:38:45 -08001665 * struct ktermios * termio : termio structure pointer
Alan Coxc7bce302006-09-30 23:27:24 -07001666 * speed_t baud : baud rate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 *
1668 * return: -1 : this port is invalid or termio == NULL
1669 * 0 : setting O.K.
1670 *
1671 *
1672 * Function 13: Get the DTR/RTS state of this port.
1673 * Syntax:
1674 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1675 * int port : port number (0 - 127)
1676 * int * dtrState : pointer to INT to receive the current DTR
1677 * state. (if NULL, this function will not
1678 * write to this address)
1679 * int * rtsState : pointer to INT to receive the current RTS
1680 * state. (if NULL, this function will not
1681 * write to this address)
1682 *
1683 * return: -1 : this port is invalid
1684 * 0 : O.K.
1685 *
1686 *
1687 * Function 14: Setting the DTR/RTS output state of this port.
1688 * Syntax:
1689 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1690 * int port : port number (0 - 127)
1691 * int dtrState : DTR output state (0: off, 1: on)
1692 * int rtsState : RTS output state (0: off, 1: on)
1693 *
1694 *
1695 * Function 15: Setting the flow control of this port.
1696 * Syntax:
1697 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1698 * int txFlow,int xany);
1699 * int port : port number (0 - 127)
1700 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1701 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1702 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1703 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1704 * int xany : S/W XANY flow control (0: no, 1: yes)
1705 *
1706 *
1707 * Function 16: Get ths line status of this port
1708 * Syntax:
1709 * int MoxaPortLineStatus(int port);
1710 * int port : port number (0 - 127)
1711 *
1712 * return: Bit 0 - CTS state (0: off, 1: on)
1713 * Bit 1 - DSR state (0: off, 1: on)
1714 * Bit 2 - DCD state (0: off, 1: on)
1715 *
1716 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 * Function 19: Flush the Rx/Tx buffer data of this port.
1718 * Syntax:
1719 * void MoxaPortFlushData(int port, int mode);
1720 * int port : port number (0 - 127)
1721 * int mode
1722 * 0 : flush the Rx buffer
1723 * 1 : flush the Tx buffer
1724 * 2 : flush the Rx and Tx buffer
1725 *
1726 *
1727 * Function 20: Write data.
1728 * Syntax:
1729 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1730 * int port : port number (0 - 127)
1731 * unsigned char * buffer : pointer to write data buffer.
1732 * int length : write data length
1733 *
1734 * return: 0 - length : real write data length
1735 *
1736 *
1737 * Function 21: Read data.
1738 * Syntax:
Alan Cox33f0f882006-01-09 20:54:13 -08001739 * int MoxaPortReadData(int port, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 * int port : port number (0 - 127)
Alan Cox33f0f882006-01-09 20:54:13 -08001741 * struct tty_struct *tty : tty for data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 *
1743 * return: 0 - length : real read data length
1744 *
1745 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 * Function 24: Get the Tx buffer current queued data bytes
1747 * Syntax:
1748 * int MoxaPortTxQueue(int port);
1749 * int port : port number (0 - 127)
1750 *
1751 * return: .. : Tx buffer current queued data bytes
1752 *
1753 *
1754 * Function 25: Get the Tx buffer current free space
1755 * Syntax:
1756 * int MoxaPortTxFree(int port);
1757 * int port : port number (0 - 127)
1758 *
1759 * return: .. : Tx buffer current free space
1760 *
1761 *
1762 * Function 26: Get the Rx buffer current queued data bytes
1763 * Syntax:
1764 * int MoxaPortRxQueue(int port);
1765 * int port : port number (0 - 127)
1766 *
1767 * return: .. : Rx buffer current queued data bytes
1768 *
1769 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 * Function 28: Disable port data transmission.
1771 * Syntax:
1772 * void MoxaPortTxDisable(int port);
1773 * int port : port number (0 - 127)
1774 *
1775 *
1776 * Function 29: Enable port data transmission.
1777 * Syntax:
1778 * void MoxaPortTxEnable(int port);
1779 * int port : port number (0 - 127)
1780 *
1781 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 * Function 31: Get the received BREAK signal count and reset it.
1783 * Syntax:
1784 * int MoxaPortResetBrkCnt(int port);
1785 * int port : port number (0 - 127)
1786 *
1787 * return: 0 - .. : BREAK signal count
1788 *
1789 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Jiri Slabyb4173f42008-04-30 00:53:40 -07001792static void MoxaPortEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
1794 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001795 u16 lowwater = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Jiri Slabyb4173f42008-04-30 00:53:40 -07001797 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 writew(lowwater, ofsAddr + Low_water);
Jiri Slaby08d01c72008-04-30 00:53:47 -07001799 if (MOXA_IS_320(port->board))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001801 else
1802 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1803 ofsAddr + HostStat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
1805 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1806 moxafunc(ofsAddr, FC_FlushQueue, 2);
1807
1808 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1809 MoxaPortLineStatus(port);
1810}
1811
Jiri Slabyb4173f42008-04-30 00:53:40 -07001812static void MoxaPortDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001814 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1817 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1818 writew(0, ofsAddr + HostStat);
1819 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1820}
1821
Jiri Slaby08d01c72008-04-30 00:53:47 -07001822static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Jiri Slaby08d01c72008-04-30 00:53:47 -07001824 void __iomem *ofsAddr = port->tableAddr;
1825 unsigned int clock, val;
1826 speed_t max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Jiri Slaby08d01c72008-04-30 00:53:47 -07001828 max = MOXA_IS_320(port->board) ? 460800 : 921600;
1829 if (baud < 50)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001830 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (baud > max)
1832 baud = max;
Jiri Slaby08d01c72008-04-30 00:53:47 -07001833 clock = 921600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 val = clock / baud;
1835 moxafunc(ofsAddr, FC_SetBaud, val);
1836 baud = clock / val;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001837 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
Jiri Slabyb4173f42008-04-30 00:53:40 -07001840static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1841 speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
1843 void __iomem *ofsAddr;
1844 tcflag_t cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 tcflag_t mode = 0;
1846
Jiri Slabyb4173f42008-04-30 00:53:40 -07001847 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 cflag = termio->c_cflag; /* termio->c_cflag */
1849
1850 mode = termio->c_cflag & CSIZE;
1851 if (mode == CS5)
1852 mode = MX_CS5;
1853 else if (mode == CS6)
1854 mode = MX_CS6;
1855 else if (mode == CS7)
1856 mode = MX_CS7;
1857 else if (mode == CS8)
1858 mode = MX_CS8;
1859
1860 if (termio->c_cflag & CSTOPB) {
1861 if (mode == MX_CS5)
1862 mode |= MX_STOP15;
1863 else
1864 mode |= MX_STOP2;
1865 } else
1866 mode |= MX_STOP1;
1867
1868 if (termio->c_cflag & PARENB) {
1869 if (termio->c_cflag & PARODD)
1870 mode |= MX_PARODD;
1871 else
1872 mode |= MX_PAREVEN;
1873 } else
1874 mode |= MX_PARNONE;
1875
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001876 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Jiri Slaby08d01c72008-04-30 00:53:47 -07001878 if (MOXA_IS_320(port->board) && baud >= 921600)
1879 return -1;
1880
Alan Coxdb1acaa2008-02-08 04:18:43 -08001881 baud = MoxaPortSetBaud(port, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1884 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1885 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1886 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001887 moxa_wait_finish(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
1889 }
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001890 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891}
1892
Jiri Slabyb4173f42008-04-30 00:53:40 -07001893static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1894 int *rtsState)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001896 if (dtrState)
1897 *dtrState = !!(port->lineCtrl & DTR_ON);
1898 if (rtsState)
1899 *rtsState = !!(port->lineCtrl & RTS_ON);
1900
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001901 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902}
1903
Jiri Slabyb4173f42008-04-30 00:53:40 -07001904static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001906 u8 mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 if (dtr)
1909 mode |= DTR_ON;
1910 if (rts)
1911 mode |= RTS_ON;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001912 port->lineCtrl = mode;
1913 moxafunc(port->tableAddr, FC_LineControl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914}
1915
Jiri Slabyb4173f42008-04-30 00:53:40 -07001916static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1917 int txflow, int rxflow, int txany)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001919 int mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 if (rts)
1922 mode |= RTS_FlowCtl;
1923 if (cts)
1924 mode |= CTS_FlowCtl;
1925 if (txflow)
1926 mode |= Tx_FlowCtl;
1927 if (rxflow)
1928 mode |= Rx_FlowCtl;
1929 if (txany)
1930 mode |= IXM_IXANY;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001931 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932}
1933
Jiri Slabyb4173f42008-04-30 00:53:40 -07001934static int MoxaPortLineStatus(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
1936 void __iomem *ofsAddr;
1937 int val;
1938
Jiri Slabyb4173f42008-04-30 00:53:40 -07001939 ofsAddr = port->tableAddr;
Jiri Slaby08d01c72008-04-30 00:53:47 -07001940 if (MOXA_IS_320(port->board)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 moxafunc(ofsAddr, FC_LineStatus, 0);
1942 val = readw(ofsAddr + FuncArg);
1943 } else {
1944 val = readw(ofsAddr + FlagStat) >> 4;
1945 }
1946 val &= 0x0B;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001947 if (val & 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 val |= 4;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001949 spin_lock_bh(&moxa_lock);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001950 moxa_new_dcdstate(port, val & 8);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001951 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 val &= 7;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001953 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955
Jiri Slaby2108eba2008-04-30 00:53:44 -07001956static int MoxaPortWriteData(struct moxa_port *port,
1957 const unsigned char *buffer, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001960 unsigned int c, total;
1961 u16 head, tail, tx_mask, spage, epage;
1962 u16 pageno, pageofs, bufhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Jiri Slabyb4173f42008-04-30 00:53:40 -07001964 ofsAddr = port->tableAddr;
1965 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 tx_mask = readw(ofsAddr + TX_mask);
1967 spage = readw(ofsAddr + Page_txb);
1968 epage = readw(ofsAddr + EndPage_txb);
1969 tail = readw(ofsAddr + TXwptr);
1970 head = readw(ofsAddr + TXrptr);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001971 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 if (c > len)
1973 c = len;
Alan Cox9de6a512008-07-16 21:56:02 +01001974 moxaLog.txcnt[port->port.tty->index] += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 total = c;
1976 if (spage == epage) {
1977 bufhead = readw(ofsAddr + Ofs_txb);
1978 writew(spage, baseAddr + Control_reg);
1979 while (c > 0) {
1980 if (head > tail)
1981 len = head - tail - 1;
1982 else
1983 len = tx_mask + 1 - tail;
1984 len = (c > len) ? len : c;
1985 ofs = baseAddr + DynPage_addr + bufhead + tail;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001986 memcpy_toio(ofs, buffer, len);
1987 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 tail = (tail + len) & tx_mask;
1989 c -= len;
1990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 pageno = spage + (tail >> 13);
1993 pageofs = tail & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001994 while (c > 0) {
1995 len = Page_size - pageofs;
1996 if (len > c)
1997 len = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 writeb(pageno, baseAddr + Control_reg);
1999 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002000 memcpy_toio(ofs, buffer, len);
2001 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 if (++pageno == epage)
2003 pageno = spage;
2004 pageofs = 0;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002005 c -= len;
2006 }
2007 tail = (tail + total) & tx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002009 writew(tail, ofsAddr + TXwptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 writeb(1, ofsAddr + CD180TXirq); /* start to send */
Jiri Slaby2108eba2008-04-30 00:53:44 -07002011 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
2013
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002014static int MoxaPortReadData(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
Alan Cox9de6a512008-07-16 21:56:02 +01002016 struct tty_struct *tty = port->port.tty;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002017 unsigned char *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002019 unsigned int count, len, total;
2020 u16 tail, rx_mask, spage, epage;
2021 u16 pageno, pageofs, bufhead, head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Jiri Slabyb4173f42008-04-30 00:53:40 -07002023 ofsAddr = port->tableAddr;
2024 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 head = readw(ofsAddr + RXrptr);
2026 tail = readw(ofsAddr + RXwptr);
2027 rx_mask = readw(ofsAddr + RX_mask);
2028 spage = readw(ofsAddr + Page_rxb);
2029 epage = readw(ofsAddr + EndPage_rxb);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002030 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 if (count == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08002032 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Alan Cox33f0f882006-01-09 20:54:13 -08002034 total = count;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002035 moxaLog.rxcnt[tty->index] += total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (spage == epage) {
2037 bufhead = readw(ofsAddr + Ofs_rxb);
2038 writew(spage, baseAddr + Control_reg);
2039 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 ofs = baseAddr + DynPage_addr + bufhead + head;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002041 len = (tail >= head) ? (tail - head) :
2042 (rx_mask + 1 - head);
2043 len = tty_prepare_flip_string(tty, &dst,
2044 min(len, count));
2045 memcpy_fromio(dst, ofs, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 head = (head + len) & rx_mask;
2047 count -= len;
2048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 pageno = spage + (head >> 13);
2051 pageofs = head & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002052 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 writew(pageno, baseAddr + Control_reg);
2054 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002055 len = tty_prepare_flip_string(tty, &dst,
2056 min(Page_size - pageofs, count));
2057 memcpy_fromio(dst, ofs, len);
2058
2059 count -= len;
2060 pageofs = (pageofs + len) & Page_mask;
2061 if (pageofs == 0 && ++pageno == epage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 pageno = spage;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002063 }
2064 head = (head + total) & rx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002066 writew(head, ofsAddr + RXrptr);
2067 if (readb(ofsAddr + FlagStat) & Xoff_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 moxaLowWaterChk = 1;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002069 port->lowChkFlag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002071 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
2074
Jiri Slabyb4173f42008-04-30 00:53:40 -07002075static int MoxaPortTxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002077 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002078 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 rptr = readw(ofsAddr + TXrptr);
2081 wptr = readw(ofsAddr + TXwptr);
2082 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002083 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084}
2085
Jiri Slabyb4173f42008-04-30 00:53:40 -07002086static int MoxaPortTxFree(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002088 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002089 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 rptr = readw(ofsAddr + TXrptr);
2092 wptr = readw(ofsAddr + TXwptr);
2093 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002094 return mask - ((wptr - rptr) & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095}
2096
Jiri Slabyb4173f42008-04-30 00:53:40 -07002097static int MoxaPortRxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002099 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002100 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 rptr = readw(ofsAddr + RXrptr);
2103 wptr = readw(ofsAddr + RXwptr);
2104 mask = readw(ofsAddr + RX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002105 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106}
2107
Jiri Slabyb4173f42008-04-30 00:53:40 -07002108static void MoxaPortTxDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002110 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
2112
Jiri Slabyb4173f42008-04-30 00:53:40 -07002113static void MoxaPortTxEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002115 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116}
2117
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002118static int moxa_get_serial_info(struct moxa_port *info,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002119 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002121 struct serial_struct tmp = {
2122 .type = info->type,
Alan Cox9de6a512008-07-16 21:56:02 +01002123 .line = info->port.tty->index,
2124 .flags = info->port.flags,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002125 .baud_base = 921600,
Alan Cox44b7d1b2008-07-16 21:57:18 +01002126 .close_delay = info->port.close_delay
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002127 };
2128 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129}
2130
2131
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002132static int moxa_set_serial_info(struct moxa_port *info,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002133 struct serial_struct __user *new_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
2135 struct serial_struct new_serial;
2136
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002137 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 return -EFAULT;
2139
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002140 if (new_serial.irq != 0 || new_serial.port != 0 ||
2141 new_serial.custom_divisor != 0 ||
2142 new_serial.baud_base != 921600)
2143 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 if (!capable(CAP_SYS_ADMIN)) {
2146 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
Alan Cox9de6a512008-07-16 21:56:02 +01002147 (info->port.flags & ~ASYNC_USR_MASK)))
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002148 return -EPERM;
2149 } else
Alan Cox44b7d1b2008-07-16 21:57:18 +01002150 info->port.close_delay = new_serial.close_delay * HZ / 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
Alan Cox9de6a512008-07-16 21:56:02 +01002153 new_serial.flags |= (info->port.flags & ASYNC_FLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002155 MoxaSetFifo(info, new_serial.type == PORT_16550A);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157 info->type = new_serial.type;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002158 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159}
2160
2161
2162
2163/*****************************************************************************
2164 * Static local functions: *
2165 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Jiri Slabyb4173f42008-04-30 00:53:40 -07002167static void MoxaSetFifo(struct moxa_port *port, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002169 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
2171 if (!enable) {
2172 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2173 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2174 } else {
2175 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2176 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2177 }
2178}