blob: 8b0da97d5293b58f09b267131455e714b5d8344c [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 *);
Alan Coxd450b5a2008-10-13 10:39:58 +0100208static void moxa_shut_down(struct tty_struct *);
Alan Cox31f35932009-01-02 13:45:05 +0000209static int moxa_carrier_raised(struct tty_port *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/*
211 * moxa board interface functions:
212 */
Jiri Slabyb4173f42008-04-30 00:53:40 -0700213static void MoxaPortEnable(struct moxa_port *);
214static void MoxaPortDisable(struct moxa_port *);
215static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
216static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
217static void MoxaPortLineCtrl(struct moxa_port *, int, int);
218static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
219static int MoxaPortLineStatus(struct moxa_port *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700220static void MoxaPortFlushData(struct moxa_port *, int);
Alan Coxd450b5a2008-10-13 10:39:58 +0100221static int MoxaPortWriteData(struct tty_struct *, const unsigned char *, int);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700222static int MoxaPortReadData(struct moxa_port *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700223static int MoxaPortTxQueue(struct moxa_port *);
224static int MoxaPortRxQueue(struct moxa_port *);
225static int MoxaPortTxFree(struct moxa_port *);
226static void MoxaPortTxDisable(struct moxa_port *);
227static void MoxaPortTxEnable(struct moxa_port *);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800228static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
229static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700230static void MoxaSetFifo(struct moxa_port *port, int enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Jiri Slaby74d7d972008-04-30 00:53:43 -0700232/*
233 * I/O functions
234 */
235
236static void moxa_wait_finish(void __iomem *ofsAddr)
237{
238 unsigned long end = jiffies + moxaFuncTout;
239
240 while (readw(ofsAddr + FuncCode) != 0)
241 if (time_after(jiffies, end))
242 return;
243 if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
244 printk(KERN_WARNING "moxa function expired\n");
245}
246
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700247static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
Jiri Slaby74d7d972008-04-30 00:53:43 -0700248{
249 writew(arg, ofsAddr + FuncArg);
250 writew(cmd, ofsAddr + FuncCode);
251 moxa_wait_finish(ofsAddr);
252}
253
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700254static void moxa_low_water_check(void __iomem *ofsAddr)
255{
256 u16 rptr, wptr, mask, len;
257
258 if (readb(ofsAddr + FlagStat) & Xoff_state) {
259 rptr = readw(ofsAddr + RXrptr);
260 wptr = readw(ofsAddr + RXwptr);
261 mask = readw(ofsAddr + RX_mask);
262 len = (wptr - rptr) & mask;
263 if (len <= Low_water)
264 moxafunc(ofsAddr, FC_SendXon, 0);
265 }
266}
267
Jiri Slaby74d7d972008-04-30 00:53:43 -0700268/*
269 * TTY operations
270 */
271
272static int moxa_ioctl(struct tty_struct *tty, struct file *file,
273 unsigned int cmd, unsigned long arg)
274{
275 struct moxa_port *ch = tty->driver_data;
276 void __user *argp = (void __user *)arg;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700277 int status, ret = 0;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700278
279 if (tty->index == MAX_PORTS) {
280 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
281 cmd != MOXA_GETMSTATUS)
282 return -EINVAL;
283 } else if (!ch)
284 return -ENODEV;
285
286 switch (cmd) {
287 case MOXA_GETDATACOUNT:
288 moxaLog.tick = jiffies;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700289 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
290 ret = -EFAULT;
291 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700292 case MOXA_FLUSH_QUEUE:
293 MoxaPortFlushData(ch, arg);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700294 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700295 case MOXA_GET_IOQUEUE: {
296 struct moxaq_str __user *argm = argp;
297 struct moxaq_str tmp;
298 struct moxa_port *p;
299 unsigned int i, j;
300
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700301 mutex_lock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700302 for (i = 0; i < MAX_BOARDS; i++) {
303 p = moxa_boards[i].ports;
304 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
305 memset(&tmp, 0, sizeof(tmp));
306 if (moxa_boards[i].ready) {
307 tmp.inq = MoxaPortRxQueue(p);
308 tmp.outq = MoxaPortTxQueue(p);
309 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700310 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
311 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700312 return -EFAULT;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700313 }
Jiri Slaby74d7d972008-04-30 00:53:43 -0700314 }
315 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700316 mutex_unlock(&moxa_openlock);
317 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700318 } case MOXA_GET_OQUEUE:
319 status = MoxaPortTxQueue(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700320 ret = put_user(status, (unsigned long __user *)argp);
321 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700322 case MOXA_GET_IQUEUE:
323 status = MoxaPortRxQueue(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700324 ret = put_user(status, (unsigned long __user *)argp);
325 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700326 case MOXA_GETMSTATUS: {
327 struct mxser_mstatus __user *argm = argp;
328 struct mxser_mstatus tmp;
329 struct moxa_port *p;
330 unsigned int i, j;
331
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700332 mutex_lock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700333 for (i = 0; i < MAX_BOARDS; i++) {
334 p = moxa_boards[i].ports;
335 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
Alan Coxd450b5a2008-10-13 10:39:58 +0100336 struct tty_struct *ttyp;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700337 memset(&tmp, 0, sizeof(tmp));
338 if (!moxa_boards[i].ready)
339 goto copy;
340
341 status = MoxaPortLineStatus(p);
342 if (status & 1)
343 tmp.cts = 1;
344 if (status & 2)
345 tmp.dsr = 1;
346 if (status & 4)
347 tmp.dcd = 1;
348
Alan Coxd450b5a2008-10-13 10:39:58 +0100349 ttyp = tty_port_tty_get(&p->port);
350 if (!ttyp || !ttyp->termios)
Jiri Slaby74d7d972008-04-30 00:53:43 -0700351 tmp.cflag = p->cflag;
352 else
Alan Coxd450b5a2008-10-13 10:39:58 +0100353 tmp.cflag = ttyp->termios->c_cflag;
354 tty_kref_put(tty);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700355copy:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700356 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
357 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700358 return -EFAULT;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700359 }
Jiri Slaby74d7d972008-04-30 00:53:43 -0700360 }
361 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700362 mutex_unlock(&moxa_openlock);
363 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700364 }
365 case TIOCGSERIAL:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700366 mutex_lock(&moxa_openlock);
367 ret = moxa_get_serial_info(ch, argp);
368 mutex_unlock(&moxa_openlock);
369 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700370 case TIOCSSERIAL:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700371 mutex_lock(&moxa_openlock);
372 ret = moxa_set_serial_info(ch, argp);
373 mutex_unlock(&moxa_openlock);
374 break;
375 default:
376 ret = -ENOIOCTLCMD;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700377 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700378 return ret;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700379}
380
Alan Cox9e989662008-07-22 11:18:03 +0100381static int moxa_break_ctl(struct tty_struct *tty, int state)
Jiri Slaby74d7d972008-04-30 00:53:43 -0700382{
383 struct moxa_port *port = tty->driver_data;
384
385 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
386 Magic_code);
Alan Cox9e989662008-07-22 11:18:03 +0100387 return 0;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700388}
389
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700390static const struct tty_operations moxa_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 .open = moxa_open,
392 .close = moxa_close,
393 .write = moxa_write,
394 .write_room = moxa_write_room,
395 .flush_buffer = moxa_flush_buffer,
396 .chars_in_buffer = moxa_chars_in_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 .ioctl = moxa_ioctl,
398 .throttle = moxa_throttle,
399 .unthrottle = moxa_unthrottle,
400 .set_termios = moxa_set_termios,
401 .stop = moxa_stop,
402 .start = moxa_start,
403 .hangup = moxa_hangup,
Jiri Slaby74d7d972008-04-30 00:53:43 -0700404 .break_ctl = moxa_break_ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 .tiocmget = moxa_tiocmget,
406 .tiocmset = moxa_tiocmset,
407};
408
Alan Cox31f35932009-01-02 13:45:05 +0000409static const struct tty_port_operations moxa_port_ops = {
410 .carrier_raised = moxa_carrier_raised,
411};
412
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800413static struct tty_driver *moxaDriver;
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800414static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
Ingo Molnar34af9462006-06-27 02:53:55 -0700415static DEFINE_SPINLOCK(moxa_lock);
Alan Cox33f0f882006-01-09 20:54:13 -0800416
Jiri Slaby74d7d972008-04-30 00:53:43 -0700417/*
418 * HW init
419 */
420
Jiri Slaby03718232008-04-30 00:53:39 -0700421static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
422{
423 switch (brd->boardType) {
424 case MOXA_BOARD_C218_ISA:
425 case MOXA_BOARD_C218_PCI:
426 if (model != 1)
427 goto err;
428 break;
429 case MOXA_BOARD_CP204J:
430 if (model != 3)
431 goto err;
432 break;
433 default:
434 if (model != 2)
435 goto err;
436 break;
437 }
438 return 0;
439err:
440 return -EINVAL;
441}
442
443static int moxa_check_fw(const void *ptr)
444{
445 const __le16 *lptr = ptr;
446
447 if (*lptr != cpu_to_le16(0x7980))
448 return -EINVAL;
449
450 return 0;
451}
452
453static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
454 size_t len)
455{
456 void __iomem *baseAddr = brd->basemem;
457 u16 tmp;
458
459 writeb(HW_reset, baseAddr + Control_reg); /* reset */
460 msleep(10);
461 memset_io(baseAddr, 0, 4096);
462 memcpy_toio(baseAddr, buf, len); /* download BIOS */
463 writeb(0, baseAddr + Control_reg); /* restart */
464
465 msleep(2000);
466
467 switch (brd->boardType) {
468 case MOXA_BOARD_C218_ISA:
469 case MOXA_BOARD_C218_PCI:
470 tmp = readw(baseAddr + C218_key);
471 if (tmp != C218_KeyCode)
472 goto err;
473 break;
474 case MOXA_BOARD_CP204J:
475 tmp = readw(baseAddr + C218_key);
476 if (tmp != CP204J_KeyCode)
477 goto err;
478 break;
479 default:
480 tmp = readw(baseAddr + C320_key);
481 if (tmp != C320_KeyCode)
482 goto err;
483 tmp = readw(baseAddr + C320_status);
484 if (tmp != STS_init) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700485 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
Jiri Slaby03718232008-04-30 00:53:39 -0700486 "module not found\n");
487 return -EIO;
488 }
489 break;
490 }
491
492 return 0;
493err:
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700494 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700495 return -EIO;
496}
497
498static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
499 size_t len)
500{
501 void __iomem *baseAddr = brd->basemem;
502
503 if (len < 7168) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700504 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700505 return -EINVAL;
506 }
507
508 writew(len - 7168 - 2, baseAddr + C320bapi_len);
509 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
510 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
511 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
512 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
513
514 return 0;
515}
516
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700517static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
Jiri Slaby03718232008-04-30 00:53:39 -0700518 size_t len)
519{
520 void __iomem *baseAddr = brd->basemem;
Harvey Harrisonb46f69c2008-10-15 22:04:19 -0700521 const __le16 *uptr = ptr;
Jiri Slaby03718232008-04-30 00:53:39 -0700522 size_t wlen, len2, j;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700523 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
Jiri Slaby08d01c792008-04-30 00:53:47 -0700524 unsigned int i, retry;
Jiri Slaby03718232008-04-30 00:53:39 -0700525 u16 usum, keycode;
526
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700527 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
528 C218_KeyCode;
529
530 switch (brd->boardType) {
531 case MOXA_BOARD_CP204J:
532 case MOXA_BOARD_C218_ISA:
533 case MOXA_BOARD_C218_PCI:
534 key = C218_key;
535 loadbuf = C218_LoadBuf;
536 loadlen = C218DLoad_len;
537 checksum = C218check_sum;
538 checksum_ok = C218chksum_ok;
539 break;
540 default:
541 key = C320_key;
542 keycode = C320_KeyCode;
543 loadbuf = C320_LoadBuf;
544 loadlen = C320DLoad_len;
545 checksum = C320check_sum;
546 checksum_ok = C320chksum_ok;
547 break;
548 }
549
Jiri Slaby03718232008-04-30 00:53:39 -0700550 usum = 0;
551 wlen = len >> 1;
552 for (i = 0; i < wlen; i++)
553 usum += le16_to_cpu(uptr[i]);
554 retry = 0;
555 do {
556 wlen = len >> 1;
557 j = 0;
558 while (wlen) {
559 len2 = (wlen > 2048) ? 2048 : wlen;
560 wlen -= len2;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700561 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
Jiri Slaby03718232008-04-30 00:53:39 -0700562 j += len2 << 1;
563
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700564 writew(len2, baseAddr + loadlen);
565 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700566 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700567 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700568 break;
569 msleep(10);
570 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700571 if (readw(baseAddr + key) != keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700572 return -EIO;
573 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700574 writew(0, baseAddr + loadlen);
575 writew(usum, baseAddr + checksum);
576 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700577 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700578 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700579 break;
580 msleep(10);
581 }
582 retry++;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700583 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
584 if (readb(baseAddr + checksum_ok) != 1)
Jiri Slaby03718232008-04-30 00:53:39 -0700585 return -EIO;
586
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700587 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700588 for (i = 0; i < 600; i++) {
589 if (readw(baseAddr + Magic_no) == Magic_code)
590 break;
591 msleep(10);
592 }
593 if (readw(baseAddr + Magic_no) != Magic_code)
594 return -EIO;
595
Jiri Slaby08d01c792008-04-30 00:53:47 -0700596 if (MOXA_IS_320(brd)) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700597 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
598 writew(0x3800, baseAddr + TMS320_PORT1);
599 writew(0x3900, baseAddr + TMS320_PORT2);
600 writew(28499, baseAddr + TMS320_CLOCK);
601 } else {
602 writew(0x3200, baseAddr + TMS320_PORT1);
603 writew(0x3400, baseAddr + TMS320_PORT2);
604 writew(19999, baseAddr + TMS320_CLOCK);
605 }
Jiri Slaby03718232008-04-30 00:53:39 -0700606 }
607 writew(1, baseAddr + Disable_IRQ);
608 writew(0, baseAddr + Magic_no);
609 for (i = 0; i < 500; i++) {
610 if (readw(baseAddr + Magic_no) == Magic_code)
611 break;
612 msleep(10);
613 }
614 if (readw(baseAddr + Magic_no) != Magic_code)
615 return -EIO;
616
Jiri Slaby08d01c792008-04-30 00:53:47 -0700617 if (MOXA_IS_320(brd)) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700618 j = readw(baseAddr + Module_cnt);
619 if (j <= 0)
620 return -EIO;
621 brd->numPorts = j * 8;
622 writew(j, baseAddr + Module_no);
623 writew(0, baseAddr + Magic_no);
624 for (i = 0; i < 600; i++) {
625 if (readw(baseAddr + Magic_no) == Magic_code)
626 break;
627 msleep(10);
628 }
629 if (readw(baseAddr + Magic_no) != Magic_code)
630 return -EIO;
Jiri Slaby03718232008-04-30 00:53:39 -0700631 }
Jiri Slaby03718232008-04-30 00:53:39 -0700632 brd->intNdx = baseAddr + IRQindex;
633 brd->intPend = baseAddr + IRQpending;
634 brd->intTable = baseAddr + IRQtable;
635
636 return 0;
637}
638
639static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
640 size_t len)
641{
642 void __iomem *ofsAddr, *baseAddr = brd->basemem;
643 struct moxa_port *port;
644 int retval, i;
645
646 if (len % 2) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700647 printk(KERN_ERR "MOXA: bios length is not even\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700648 return -EINVAL;
649 }
650
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700651 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
652 if (retval)
653 return retval;
654
Jiri Slaby03718232008-04-30 00:53:39 -0700655 switch (brd->boardType) {
656 case MOXA_BOARD_C218_ISA:
657 case MOXA_BOARD_C218_PCI:
658 case MOXA_BOARD_CP204J:
Jiri Slaby03718232008-04-30 00:53:39 -0700659 port = brd->ports;
660 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700661 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700662 port->DCDState = 0;
663 port->tableAddr = baseAddr + Extern_table +
664 Extern_size * i;
665 ofsAddr = port->tableAddr;
666 writew(C218rx_mask, ofsAddr + RX_mask);
667 writew(C218tx_mask, ofsAddr + TX_mask);
668 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
669 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
670
671 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
672 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
673
674 }
675 break;
676 default:
Jiri Slaby03718232008-04-30 00:53:39 -0700677 port = brd->ports;
678 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700679 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700680 port->DCDState = 0;
681 port->tableAddr = baseAddr + Extern_table +
682 Extern_size * i;
683 ofsAddr = port->tableAddr;
684 switch (brd->numPorts) {
685 case 8:
686 writew(C320p8rx_mask, ofsAddr + RX_mask);
687 writew(C320p8tx_mask, ofsAddr + TX_mask);
688 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
689 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
690 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
691 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
692
693 break;
694 case 16:
695 writew(C320p16rx_mask, ofsAddr + RX_mask);
696 writew(C320p16tx_mask, ofsAddr + TX_mask);
697 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
698 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
699 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
700 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
701 break;
702
703 case 24:
704 writew(C320p24rx_mask, ofsAddr + RX_mask);
705 writew(C320p24tx_mask, ofsAddr + TX_mask);
706 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
707 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
708 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
709 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
710 break;
711 case 32:
712 writew(C320p32rx_mask, ofsAddr + RX_mask);
713 writew(C320p32tx_mask, ofsAddr + TX_mask);
714 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
715 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
716 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
717 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
718 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
719 break;
720 }
721 }
722 break;
723 }
Jiri Slaby03718232008-04-30 00:53:39 -0700724 return 0;
725}
726
727static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
728{
David Howells2bca76e2008-07-08 17:37:15 +0100729 const void *ptr = fw->data;
Jiri Slaby03718232008-04-30 00:53:39 -0700730 char rsn[64];
731 u16 lens[5];
732 size_t len;
733 unsigned int a, lenp, lencnt;
734 int ret = -EINVAL;
735 struct {
736 __le32 magic; /* 0x34303430 */
737 u8 reserved1[2];
738 u8 type; /* UNIX = 3 */
739 u8 model; /* C218T=1, C320T=2, CP204=3 */
740 u8 reserved2[8];
741 __le16 len[5];
David Howells2bca76e2008-07-08 17:37:15 +0100742 } const *hdr = ptr;
Jiri Slaby03718232008-04-30 00:53:39 -0700743
744 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
745
746 if (fw->size < MOXA_FW_HDRLEN) {
747 strcpy(rsn, "too short (even header won't fit)");
748 goto err;
749 }
750 if (hdr->magic != cpu_to_le32(0x30343034)) {
751 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
752 goto err;
753 }
754 if (hdr->type != 3) {
755 sprintf(rsn, "not for linux, type is %u", hdr->type);
756 goto err;
757 }
758 if (moxa_check_fw_model(brd, hdr->model)) {
759 sprintf(rsn, "not for this card, model is %u", hdr->model);
760 goto err;
761 }
762
763 len = MOXA_FW_HDRLEN;
764 lencnt = hdr->model == 2 ? 5 : 3;
765 for (a = 0; a < ARRAY_SIZE(lens); a++) {
766 lens[a] = le16_to_cpu(hdr->len[a]);
767 if (lens[a] && len + lens[a] <= fw->size &&
768 moxa_check_fw(&fw->data[len]))
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700769 printk(KERN_WARNING "MOXA firmware: unexpected input "
Jiri Slaby03718232008-04-30 00:53:39 -0700770 "at offset %u, but going on\n", (u32)len);
771 if (!lens[a] && a < lencnt) {
772 sprintf(rsn, "too few entries in fw file");
773 goto err;
774 }
775 len += lens[a];
776 }
777
778 if (len != fw->size) {
779 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
780 (u32)len);
781 goto err;
782 }
783
784 ptr += MOXA_FW_HDRLEN;
785 lenp = 0; /* bios */
786
787 strcpy(rsn, "read above");
788
789 ret = moxa_load_bios(brd, ptr, lens[lenp]);
790 if (ret)
791 goto err;
792
793 /* we skip the tty section (lens[1]), since we don't need it */
794 ptr += lens[lenp] + lens[lenp + 1];
795 lenp += 2; /* comm */
796
797 if (hdr->model == 2) {
798 ret = moxa_load_320b(brd, ptr, lens[lenp]);
799 if (ret)
800 goto err;
801 /* skip another tty */
802 ptr += lens[lenp] + lens[lenp + 1];
803 lenp += 2;
804 }
805
806 ret = moxa_load_code(brd, ptr, lens[lenp]);
807 if (ret)
808 goto err;
809
810 return 0;
811err:
812 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
813 return ret;
814}
815
816static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
817{
818 const struct firmware *fw;
819 const char *file;
Jiri Slaby810ab092008-04-30 00:53:41 -0700820 struct moxa_port *p;
821 unsigned int i;
Jiri Slaby03718232008-04-30 00:53:39 -0700822 int ret;
823
Jiri Slaby810ab092008-04-30 00:53:41 -0700824 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
825 GFP_KERNEL);
826 if (brd->ports == NULL) {
827 printk(KERN_ERR "cannot allocate memory for ports\n");
828 ret = -ENOMEM;
829 goto err;
830 }
831
832 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
Alan Cox9de6a512008-07-16 21:56:02 +0100833 tty_port_init(&p->port);
Alan Cox31f35932009-01-02 13:45:05 +0000834 p->port.ops = &moxa_port_ops;
Alan Cox44b7d1b2008-07-16 21:57:18 +0100835 p->type = PORT_16550A;
836 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Jiri Slaby810ab092008-04-30 00:53:41 -0700837 }
838
Jiri Slaby03718232008-04-30 00:53:39 -0700839 switch (brd->boardType) {
840 case MOXA_BOARD_C218_ISA:
841 case MOXA_BOARD_C218_PCI:
842 file = "c218tunx.cod";
843 break;
844 case MOXA_BOARD_CP204J:
845 file = "cp204unx.cod";
846 break;
847 default:
848 file = "c320tunx.cod";
849 break;
850 }
851
852 ret = request_firmware(&fw, file, dev);
853 if (ret) {
Jiri Slabyec09cd52008-04-30 00:53:49 -0700854 printk(KERN_ERR "MOXA: request_firmware failed. Make sure "
855 "you've placed '%s' file into your firmware "
856 "loader directory (e.g. /lib/firmware)\n",
857 file);
Jiri Slaby810ab092008-04-30 00:53:41 -0700858 goto err_free;
Jiri Slaby03718232008-04-30 00:53:39 -0700859 }
860
861 ret = moxa_load_fw(brd, fw);
862
863 release_firmware(fw);
Jiri Slaby810ab092008-04-30 00:53:41 -0700864
865 if (ret)
866 goto err_free;
867
Jiri Slaby2a541342008-04-30 00:53:45 -0700868 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700869 brd->ready = 1;
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700870 if (!timer_pending(&moxaTimer))
871 mod_timer(&moxaTimer, jiffies + HZ / 50);
Jiri Slaby2a541342008-04-30 00:53:45 -0700872 spin_unlock_bh(&moxa_lock);
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700873
Jiri Slaby810ab092008-04-30 00:53:41 -0700874 return 0;
875err_free:
876 kfree(brd->ports);
877err:
Jiri Slaby03718232008-04-30 00:53:39 -0700878 return ret;
879}
880
Jiri Slaby810ab092008-04-30 00:53:41 -0700881static void moxa_board_deinit(struct moxa_board_conf *brd)
882{
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700883 unsigned int a, opened;
884
885 mutex_lock(&moxa_openlock);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700886 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700887 brd->ready = 0;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700888 spin_unlock_bh(&moxa_lock);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700889
890 /* pci hot-un-plug support */
891 for (a = 0; a < brd->numPorts; a++)
Alan Coxd450b5a2008-10-13 10:39:58 +0100892 if (brd->ports[a].port.flags & ASYNC_INITIALIZED) {
893 struct tty_struct *tty = tty_port_tty_get(
894 &brd->ports[a].port);
895 if (tty) {
896 tty_hangup(tty);
897 tty_kref_put(tty);
898 }
899 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700900 while (1) {
901 opened = 0;
902 for (a = 0; a < brd->numPorts; a++)
Alan Cox9de6a512008-07-16 21:56:02 +0100903 if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700904 opened++;
905 mutex_unlock(&moxa_openlock);
906 if (!opened)
907 break;
908 msleep(50);
909 mutex_lock(&moxa_openlock);
910 }
911
Jiri Slaby810ab092008-04-30 00:53:41 -0700912 iounmap(brd->basemem);
913 brd->basemem = NULL;
914 kfree(brd->ports);
915}
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917#ifdef CONFIG_PCI
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800918static int __devinit moxa_pci_probe(struct pci_dev *pdev,
919 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800921 struct moxa_board_conf *board;
922 unsigned int i;
923 int board_type = ent->driver_data;
924 int retval;
925
926 retval = pci_enable_device(pdev);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700927 if (retval) {
928 dev_err(&pdev->dev, "can't enable pci device\n");
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800929 goto err;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700930 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800931
932 for (i = 0; i < MAX_BOARDS; i++)
933 if (moxa_boards[i].basemem == NULL)
934 break;
935
936 retval = -ENODEV;
937 if (i >= MAX_BOARDS) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700938 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800939 "found. Board is ignored.\n", MAX_BOARDS);
940 goto err;
941 }
942
943 board = &moxa_boards[i];
Jiri Slabye46a5e32008-04-30 00:53:37 -0700944
945 retval = pci_request_region(pdev, 2, "moxa-base");
946 if (retval) {
947 dev_err(&pdev->dev, "can't request pci region 2\n");
948 goto err;
949 }
950
Alan Cox24cb2332008-04-30 00:54:19 -0700951 board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700952 if (board->basemem == NULL) {
953 dev_err(&pdev->dev, "can't remap io space 2\n");
Jiri Slabye46a5e32008-04-30 00:53:37 -0700954 goto err_reg;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700955 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 board->boardType = board_type;
958 switch (board_type) {
959 case MOXA_BOARD_C218_ISA:
960 case MOXA_BOARD_C218_PCI:
961 board->numPorts = 8;
962 break;
963
964 case MOXA_BOARD_CP204J:
965 board->numPorts = 4;
966 break;
967 default:
968 board->numPorts = 0;
969 break;
970 }
971 board->busType = MOXA_BUS_TYPE_PCI;
Jiri Slabya784bf72007-02-10 01:45:36 -0800972
Jiri Slaby03718232008-04-30 00:53:39 -0700973 retval = moxa_init_board(board, &pdev->dev);
974 if (retval)
975 goto err_base;
976
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800977 pci_set_drvdata(pdev, board);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Jiri Slabybb9f9102008-04-30 00:53:48 -0700979 dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n",
980 moxa_brdname[board_type - 1], board->numPorts);
981
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700982 return 0;
Jiri Slaby03718232008-04-30 00:53:39 -0700983err_base:
984 iounmap(board->basemem);
985 board->basemem = NULL;
Jiri Slabye46a5e32008-04-30 00:53:37 -0700986err_reg:
987 pci_release_region(pdev, 2);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800988err:
989 return retval;
990}
991
992static void __devexit moxa_pci_remove(struct pci_dev *pdev)
993{
994 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
995
Jiri Slaby810ab092008-04-30 00:53:41 -0700996 moxa_board_deinit(brd);
997
Jiri Slabye46a5e32008-04-30 00:53:37 -0700998 pci_release_region(pdev, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
Jiri Slabya784bf72007-02-10 01:45:36 -08001000
1001static struct pci_driver moxa_pci_driver = {
1002 .name = "moxa",
1003 .id_table = moxa_pcibrds,
1004 .probe = moxa_pci_probe,
1005 .remove = __devexit_p(moxa_pci_remove)
1006};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007#endif /* CONFIG_PCI */
1008
1009static int __init moxa_init(void)
1010{
Jiri Slaby810ab092008-04-30 00:53:41 -07001011 unsigned int isabrds = 0;
Jiri Slabyd353eca2008-04-30 00:53:37 -07001012 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001014 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
1015 MOXA_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
1017 if (!moxaDriver)
1018 return -ENOMEM;
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 moxaDriver->owner = THIS_MODULE;
Sergey Vlasov9b4e3b12005-09-03 16:26:49 +01001021 moxaDriver->name = "ttyMX";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 moxaDriver->major = ttymajor;
1023 moxaDriver->minor_start = 0;
1024 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1025 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1026 moxaDriver->init_termios = tty_std_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Alan Cox606d0992006-12-08 02:38:45 -08001028 moxaDriver->init_termios.c_ispeed = 9600;
1029 moxaDriver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
1031 tty_set_operations(moxaDriver, &moxa_ops);
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (tty_register_driver(moxaDriver)) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001034 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 put_tty_driver(moxaDriver);
1036 return -1;
1037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Jiri Slabyd353eca2008-04-30 00:53:37 -07001039 /* Find the boards defined from module args. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -07001041 {
1042 struct moxa_board_conf *brd = moxa_boards;
Jiri Slaby810ab092008-04-30 00:53:41 -07001043 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 for (i = 0; i < MAX_BOARDS; i++) {
Jiri Slabyd353eca2008-04-30 00:53:37 -07001045 if (!baseaddr[i])
1046 break;
1047 if (type[i] == MOXA_BOARD_C218_ISA ||
1048 type[i] == MOXA_BOARD_C320_ISA) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001049 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001050 isabrds + 1, moxa_brdname[type[i] - 1],
1051 baseaddr[i]);
1052 brd->boardType = type[i];
1053 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1054 numports[i];
1055 brd->busType = MOXA_BUS_TYPE_ISA;
Alan Cox24cb2332008-04-30 00:54:19 -07001056 brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
Jiri Slabyd353eca2008-04-30 00:53:37 -07001057 if (!brd->basemem) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001058 printk(KERN_ERR "MOXA: can't remap %lx\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001059 baseaddr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 continue;
1061 }
Jiri Slaby03718232008-04-30 00:53:39 -07001062 if (moxa_init_board(brd, NULL)) {
1063 iounmap(brd->basemem);
1064 brd->basemem = NULL;
1065 continue;
1066 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001067
Jiri Slabybb9f9102008-04-30 00:53:48 -07001068 printk(KERN_INFO "MOXA isa board found at 0x%.8lu and "
1069 "ready (%u ports, firmware loaded)\n",
1070 baseaddr[i], brd->numPorts);
1071
Jiri Slabyd353eca2008-04-30 00:53:37 -07001072 brd++;
1073 isabrds++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
1075 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001080 retval = pci_register_driver(&moxa_pci_driver);
1081 if (retval) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001082 printk(KERN_ERR "Can't register MOXA pci driver!\n");
Jiri Slabyd353eca2008-04-30 00:53:37 -07001083 if (isabrds)
Jiri Slabya784bf72007-02-10 01:45:36 -08001084 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001087
Jiri Slabya784bf72007-02-10 01:45:36 -08001088 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
1091static void __exit moxa_exit(void)
1092{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001093 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001095#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001096 pci_unregister_driver(&moxa_pci_driver);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001097#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001098
Jiri Slaby810ab092008-04-30 00:53:41 -07001099 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1100 if (moxa_boards[i].ready)
1101 moxa_board_deinit(&moxa_boards[i]);
Jiri Slaby2a541342008-04-30 00:53:45 -07001102
1103 del_timer_sync(&moxaTimer);
1104
1105 if (tty_unregister_driver(moxaDriver))
1106 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1107 "serial driver\n");
1108 put_tty_driver(moxaDriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
1110
1111module_init(moxa_init);
1112module_exit(moxa_exit);
1113
Alan Coxd450b5a2008-10-13 10:39:58 +01001114static void moxa_close_port(struct tty_struct *tty)
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001115{
Alan Coxd450b5a2008-10-13 10:39:58 +01001116 struct moxa_port *ch = tty->driver_data;
1117 moxa_shut_down(tty);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001118 MoxaPortFlushData(ch, 2);
Alan Cox9de6a512008-07-16 21:56:02 +01001119 ch->port.flags &= ~ASYNC_NORMAL_ACTIVE;
Alan Coxd450b5a2008-10-13 10:39:58 +01001120 tty->driver_data = NULL;
1121 tty_port_tty_set(&ch->port, NULL);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001122}
1123
Alan Cox31f35932009-01-02 13:45:05 +00001124static int moxa_carrier_raised(struct tty_port *port)
1125{
1126 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1127 int dcd;
1128
1129 spin_lock_bh(&moxa_lock);
1130 dcd = ch->DCDState;
1131 spin_unlock_bh(&moxa_lock);
1132 return dcd;
1133}
1134
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001135static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
1136 struct moxa_port *ch)
1137{
Alan Cox31f35932009-01-02 13:45:05 +00001138 struct tty_port *port = &ch->port;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001139 DEFINE_WAIT(wait);
1140 int retval = 0;
1141 u8 dcd;
1142
1143 while (1) {
Alan Cox31f35932009-01-02 13:45:05 +00001144 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001145 if (tty_hung_up_p(filp)) {
1146#ifdef SERIAL_DO_RESTART
1147 retval = -ERESTARTSYS;
1148#else
1149 retval = -EAGAIN;
1150#endif
1151 break;
1152 }
Alan Cox31f35932009-01-02 13:45:05 +00001153 dcd = tty_port_carrier_raised(port);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001154 if (dcd)
1155 break;
1156
1157 if (signal_pending(current)) {
1158 retval = -ERESTARTSYS;
1159 break;
1160 }
1161 schedule();
1162 }
Alan Cox31f35932009-01-02 13:45:05 +00001163 finish_wait(&port->open_wait, &wait);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001164
1165 return retval;
1166}
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168static int moxa_open(struct tty_struct *tty, struct file *filp)
1169{
Jiri Slaby810ab092008-04-30 00:53:41 -07001170 struct moxa_board_conf *brd;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001171 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 int port;
1173 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Jiri Slaby11324ed2007-02-10 01:45:31 -08001175 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (port == MAX_PORTS) {
Jiri Slaby74d7d972008-04-30 00:53:43 -07001177 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001179 if (mutex_lock_interruptible(&moxa_openlock))
1180 return -ERESTARTSYS;
Jiri Slaby810ab092008-04-30 00:53:41 -07001181 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001182 if (!brd->ready) {
1183 mutex_unlock(&moxa_openlock);
Jiri Slaby810ab092008-04-30 00:53:41 -07001184 return -ENODEV;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Jiri Slaby810ab092008-04-30 00:53:41 -07001187 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
Alan Cox9de6a512008-07-16 21:56:02 +01001188 ch->port.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 tty->driver_data = ch;
Alan Coxd450b5a2008-10-13 10:39:58 +01001190 tty_port_tty_set(&ch->port, tty);
Alan Cox9de6a512008-07-16 21:56:02 +01001191 if (!(ch->port.flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 ch->statusflags = 0;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001193 moxa_set_tty_param(tty, tty->termios);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001194 MoxaPortLineCtrl(ch, 1, 1);
1195 MoxaPortEnable(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001196 MoxaSetFifo(ch, ch->type == PORT_16550A);
Alan Cox9de6a512008-07-16 21:56:02 +01001197 ch->port.flags |= ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001199 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001201 retval = 0;
1202 if (!(filp->f_flags & O_NONBLOCK) && !C_CLOCAL(tty))
1203 retval = moxa_block_till_ready(tty, filp, ch);
1204 mutex_lock(&moxa_openlock);
1205 if (retval) {
Alan Cox9de6a512008-07-16 21:56:02 +01001206 if (ch->port.count) /* 0 means already hung up... */
1207 if (--ch->port.count == 0)
Alan Coxd450b5a2008-10-13 10:39:58 +01001208 moxa_close_port(tty);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001209 } else
Alan Cox9de6a512008-07-16 21:56:02 +01001210 ch->port.flags |= ASYNC_NORMAL_ACTIVE;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001211 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001213 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
1216static void moxa_close(struct tty_struct *tty, struct file *filp)
1217{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001218 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 int port;
1220
Jiri Slaby11324ed2007-02-10 01:45:31 -08001221 port = tty->index;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001222 if (port == MAX_PORTS || tty_hung_up_p(filp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001225 mutex_lock(&moxa_openlock);
1226 ch = tty->driver_data;
1227 if (ch == NULL)
1228 goto unlock;
Alan Cox9de6a512008-07-16 21:56:02 +01001229 if (tty->count == 1 && ch->port.count != 1) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001230 printk(KERN_WARNING "moxa_close: bad serial port count; "
Alan Cox9de6a512008-07-16 21:56:02 +01001231 "tty->count is 1, ch->port.count is %d\n", ch->port.count);
1232 ch->port.count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
Alan Cox9de6a512008-07-16 21:56:02 +01001234 if (--ch->port.count < 0) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001235 printk(KERN_WARNING "moxa_close: bad serial port count, "
1236 "device=%s\n", tty->name);
Alan Cox9de6a512008-07-16 21:56:02 +01001237 ch->port.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
Alan Cox9de6a512008-07-16 21:56:02 +01001239 if (ch->port.count)
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001240 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 ch->cflag = tty->termios->c_cflag;
Alan Cox9de6a512008-07-16 21:56:02 +01001243 if (ch->port.flags & ASYNC_INITIALIZED) {
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001244 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Alan Coxd450b5a2008-10-13 10:39:58 +01001248 moxa_close_port(tty);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001249unlock:
1250 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}
1252
1253static int moxa_write(struct tty_struct *tty,
1254 const unsigned char *buf, int count)
1255{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001256 struct moxa_port *ch = tty->driver_data;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001257 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (ch == NULL)
Jiri Slabyb4173f42008-04-30 00:53:40 -07001260 return 0;
Alan Cox33f0f882006-01-09 20:54:13 -08001261
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001262 spin_lock_bh(&moxa_lock);
Alan Coxd450b5a2008-10-13 10:39:58 +01001263 len = MoxaPortWriteData(tty, buf, count);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001264 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 ch->statusflags |= LOWWAIT;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001267 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
1270static int moxa_write_room(struct tty_struct *tty)
1271{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001272 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 if (tty->stopped)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001275 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001276 ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (ch == NULL)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001278 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001279 return MoxaPortTxFree(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
1282static void moxa_flush_buffer(struct tty_struct *tty)
1283{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001284 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 if (ch == NULL)
1287 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001288 MoxaPortFlushData(ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 tty_wakeup(tty);
1290}
1291
1292static int moxa_chars_in_buffer(struct tty_struct *tty)
1293{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001294 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 /*
1298 * Sigh...I have to check if driver_data is NULL here, because
1299 * if an open() fails, the TTY subsystem eventually calls
1300 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1301 * routine. And since the open() failed, we return 0 here. TDJ
1302 */
1303 if (ch == NULL)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001304 return 0;
Alan Cox978e5952008-04-30 00:53:59 -07001305 lock_kernel();
Jiri Slabyb4173f42008-04-30 00:53:40 -07001306 chars = MoxaPortTxQueue(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (chars) {
1308 /*
1309 * Make it possible to wakeup anything waiting for output
1310 * in tty_ioctl.c, etc.
1311 */
1312 if (!(ch->statusflags & EMPTYWAIT))
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001313 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
Alan Cox978e5952008-04-30 00:53:59 -07001315 unlock_kernel();
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001316 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1320{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001321 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 int flag = 0, dtr, rts;
1323
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001324 mutex_lock(&moxa_openlock);
1325 ch = tty->driver_data;
1326 if (!ch) {
1327 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -07001328 return -EINVAL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Jiri Slabyb4173f42008-04-30 00:53:40 -07001331 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (dtr)
1333 flag |= TIOCM_DTR;
1334 if (rts)
1335 flag |= TIOCM_RTS;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001336 dtr = MoxaPortLineStatus(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (dtr & 1)
1338 flag |= TIOCM_CTS;
1339 if (dtr & 2)
1340 flag |= TIOCM_DSR;
1341 if (dtr & 4)
1342 flag |= TIOCM_CD;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001343 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return flag;
1345}
1346
1347static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1348 unsigned int set, unsigned int clear)
1349{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001350 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 int port;
1352 int dtr, rts;
1353
Jiri Slaby11324ed2007-02-10 01:45:31 -08001354 port = tty->index;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001355 mutex_lock(&moxa_openlock);
1356 ch = tty->driver_data;
1357 if (!ch) {
1358 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -07001359 return -EINVAL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Jiri Slabyb4173f42008-04-30 00:53:40 -07001362 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (set & TIOCM_RTS)
1364 rts = 1;
1365 if (set & TIOCM_DTR)
1366 dtr = 1;
1367 if (clear & TIOCM_RTS)
1368 rts = 0;
1369 if (clear & TIOCM_DTR)
1370 dtr = 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001371 MoxaPortLineCtrl(ch, dtr, rts);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001372 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return 0;
1374}
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376static void moxa_throttle(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 ch->statusflags |= THROTTLE;
1381}
1382
1383static void moxa_unthrottle(struct tty_struct *tty)
1384{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001385 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 ch->statusflags &= ~THROTTLE;
1388}
1389
1390static void moxa_set_termios(struct tty_struct *tty,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001391 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001393 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 if (ch == NULL)
1396 return;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001397 moxa_set_tty_param(tty, old_termios);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001398 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
Alan Cox9de6a512008-07-16 21:56:02 +01001399 wake_up_interruptible(&ch->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
1401
1402static void moxa_stop(struct tty_struct *tty)
1403{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001404 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 if (ch == NULL)
1407 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001408 MoxaPortTxDisable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 ch->statusflags |= TXSTOPPED;
1410}
1411
1412
1413static void moxa_start(struct tty_struct *tty)
1414{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001415 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 if (ch == NULL)
1418 return;
1419
1420 if (!(ch->statusflags & TXSTOPPED))
1421 return;
1422
Jiri Slabyb4173f42008-04-30 00:53:40 -07001423 MoxaPortTxEnable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 ch->statusflags &= ~TXSTOPPED;
1425}
1426
1427static void moxa_hangup(struct tty_struct *tty)
1428{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001429 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001431 mutex_lock(&moxa_openlock);
1432 ch = tty->driver_data;
1433 if (ch == NULL) {
1434 mutex_unlock(&moxa_openlock);
1435 return;
1436 }
Alan Cox9de6a512008-07-16 21:56:02 +01001437 ch->port.count = 0;
Alan Coxd450b5a2008-10-13 10:39:58 +01001438 moxa_close_port(tty);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001439 mutex_unlock(&moxa_openlock);
1440
Alan Cox9de6a512008-07-16 21:56:02 +01001441 wake_up_interruptible(&ch->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001444static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1445{
Alan Coxd450b5a2008-10-13 10:39:58 +01001446 struct tty_struct *tty;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001447 dcd = !!dcd;
1448
Alan Coxd450b5a2008-10-13 10:39:58 +01001449 if (dcd != p->DCDState) {
1450 tty = tty_port_tty_get(&p->port);
1451 if (tty && C_CLOCAL(tty) && !dcd)
1452 tty_hangup(tty);
1453 tty_kref_put(tty);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001454 }
1455 p->DCDState = dcd;
1456}
1457
1458static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1459 u16 __iomem *ip)
1460{
Alan Coxd450b5a2008-10-13 10:39:58 +01001461 struct tty_struct *tty = tty_port_tty_get(&p->port);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001462 void __iomem *ofsAddr;
Alan Cox9de6a512008-07-16 21:56:02 +01001463 unsigned int inited = p->port.flags & ASYNC_INITIALIZED;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001464 u16 intr;
1465
1466 if (tty) {
1467 if ((p->statusflags & EMPTYWAIT) &&
1468 MoxaPortTxQueue(p) == 0) {
1469 p->statusflags &= ~EMPTYWAIT;
1470 tty_wakeup(tty);
1471 }
1472 if ((p->statusflags & LOWWAIT) && !tty->stopped &&
1473 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1474 p->statusflags &= ~LOWWAIT;
1475 tty_wakeup(tty);
1476 }
1477
1478 if (inited && !(p->statusflags & THROTTLE) &&
1479 MoxaPortRxQueue(p) > 0) { /* RX */
1480 MoxaPortReadData(p);
1481 tty_schedule_flip(tty);
1482 }
1483 } else {
1484 p->statusflags &= ~EMPTYWAIT;
1485 MoxaPortFlushData(p, 0); /* flush RX */
1486 }
1487
1488 if (!handle) /* nothing else to do */
1489 return 0;
1490
1491 intr = readw(ip); /* port irq status */
1492 if (intr == 0)
1493 return 0;
1494
1495 writew(0, ip); /* ACK port */
1496 ofsAddr = p->tableAddr;
1497 if (intr & IntrTx) /* disable tx intr */
1498 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1499 ofsAddr + HostStat);
1500
1501 if (!inited)
1502 return 0;
1503
1504 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1505 tty_insert_flip_char(tty, 0, TTY_BREAK);
1506 tty_schedule_flip(tty);
1507 }
Alan Coxd450b5a2008-10-13 10:39:58 +01001508 tty_kref_put(tty);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001509
1510 if (intr & IntrLine)
1511 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1512
1513 return 0;
1514}
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516static void moxa_poll(unsigned long ignored)
1517{
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001518 struct moxa_board_conf *brd;
1519 u16 __iomem *ip;
Jiri Slaby2a541342008-04-30 00:53:45 -07001520 unsigned int card, port, served = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001522 spin_lock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 for (card = 0; card < MAX_BOARDS; card++) {
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001524 brd = &moxa_boards[card];
1525 if (!brd->ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 continue;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001527
Jiri Slaby2a541342008-04-30 00:53:45 -07001528 served++;
1529
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001530 ip = NULL;
1531 if (readb(brd->intPend) == 0xff)
1532 ip = brd->intTable + readb(brd->intNdx);
1533
1534 for (port = 0; port < brd->numPorts; port++)
1535 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1536
1537 if (ip)
1538 writeb(0, brd->intPend); /* ACK */
1539
1540 if (moxaLowWaterChk) {
1541 struct moxa_port *p = brd->ports;
1542 for (port = 0; port < brd->numPorts; port++, p++)
1543 if (p->lowChkFlag) {
1544 p->lowChkFlag = 0;
1545 moxa_low_water_check(p->tableAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 }
1548 }
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001549 moxaLowWaterChk = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Jiri Slaby2a541342008-04-30 00:53:45 -07001551 if (served)
1552 mod_timer(&moxaTimer, jiffies + HZ / 50);
1553 spin_unlock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
1555
1556/******************************************************************************/
1557
Alan Coxdb1acaa2008-02-08 04:18:43 -08001558static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001560 register struct ktermios *ts = tty->termios;
1561 struct moxa_port *ch = tty->driver_data;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001562 int rts, cts, txflow, rxflow, xany, baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 rts = cts = txflow = rxflow = xany = 0;
1565 if (ts->c_cflag & CRTSCTS)
1566 rts = cts = 1;
1567 if (ts->c_iflag & IXON)
1568 txflow = 1;
1569 if (ts->c_iflag & IXOFF)
1570 rxflow = 1;
1571 if (ts->c_iflag & IXANY)
1572 xany = 1;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001573
1574 /* Clear the features we don't support */
1575 ts->c_cflag &= ~CMSPAR;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001576 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1577 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
Alan Coxdb1acaa2008-02-08 04:18:43 -08001578 if (baud == -1)
1579 baud = tty_termios_baud_rate(old_termios);
1580 /* Not put the baud rate into the termios data */
1581 tty_encode_baud_rate(tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001584static void moxa_setup_empty_event(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001586 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001588 spin_lock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 ch->statusflags |= EMPTYWAIT;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001590 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
Alan Coxd450b5a2008-10-13 10:39:58 +01001593static void moxa_shut_down(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
Alan Coxd450b5a2008-10-13 10:39:58 +01001595 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Alan Cox9de6a512008-07-16 21:56:02 +01001597 if (!(ch->port.flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 return;
1599
Jiri Slabyb4173f42008-04-30 00:53:40 -07001600 MoxaPortDisable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 /*
1603 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1604 */
Alan Coxd450b5a2008-10-13 10:39:58 +01001605 if (C_HUPCL(tty))
Jiri Slabyb4173f42008-04-30 00:53:40 -07001606 MoxaPortLineCtrl(ch, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001608 spin_lock_bh(&moxa_lock);
Alan Cox9de6a512008-07-16 21:56:02 +01001609 ch->port.flags &= ~ASYNC_INITIALIZED;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001610 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613/*****************************************************************************
1614 * Driver level functions: *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Jiri Slabyb4173f42008-04-30 00:53:40 -07001617static void MoxaPortFlushData(struct moxa_port *port, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
1619 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001620 if (mode < 0 || mode > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001622 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 moxafunc(ofsAddr, FC_FlushQueue, mode);
1624 if (mode != 1) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001625 port->lowChkFlag = 0;
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001626 moxa_low_water_check(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 }
1628}
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630/*
1631 * Moxa Port Number Description:
1632 *
1633 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1634 * the port number using in MOXA driver functions will be 0 to 31 for
1635 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1636 * to 127 for fourth. For example, if you setup three MOXA boards,
1637 * first board is C218, second board is C320-16 and third board is
1638 * C320-32. The port number of first board (C218 - 8 ports) is from
1639 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1640 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1641 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1642 * 127 will be invalid.
1643 *
1644 *
1645 * Moxa Functions Description:
1646 *
1647 * Function 1: Driver initialization routine, this routine must be
1648 * called when initialized driver.
1649 * Syntax:
1650 * void MoxaDriverInit();
1651 *
1652 *
1653 * Function 2: Moxa driver private IOCTL command processing.
1654 * Syntax:
1655 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1656 *
1657 * unsigned int cmd : IOCTL command
1658 * unsigned long arg : IOCTL argument
1659 * int port : port number (0 - 127)
1660 *
1661 * return: 0 (OK)
1662 * -EINVAL
1663 * -ENOIOCTLCMD
1664 *
1665 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 * Function 6: Enable this port to start Tx/Rx data.
1667 * Syntax:
1668 * void MoxaPortEnable(int port);
1669 * int port : port number (0 - 127)
1670 *
1671 *
1672 * Function 7: Disable this port
1673 * Syntax:
1674 * void MoxaPortDisable(int port);
1675 * int port : port number (0 - 127)
1676 *
1677 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 * Function 10: Setting baud rate of this port.
1679 * Syntax:
Jiri Slaby08d01c792008-04-30 00:53:47 -07001680 * speed_t MoxaPortSetBaud(int port, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 * int port : port number (0 - 127)
1682 * long baud : baud rate (50 - 115200)
1683 *
1684 * return: 0 : this port is invalid or baud < 50
1685 * 50 - 115200 : the real baud rate set to the port, if
1686 * the argument baud is large than maximun
1687 * available baud rate, the real setting
1688 * baud rate will be the maximun baud rate.
1689 *
1690 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 * Function 12: Configure the port.
1692 * Syntax:
Alan Cox606d0992006-12-08 02:38:45 -08001693 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 * int port : port number (0 - 127)
Alan Cox606d0992006-12-08 02:38:45 -08001695 * struct ktermios * termio : termio structure pointer
Alan Coxc7bce302006-09-30 23:27:24 -07001696 * speed_t baud : baud rate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 *
1698 * return: -1 : this port is invalid or termio == NULL
1699 * 0 : setting O.K.
1700 *
1701 *
1702 * Function 13: Get the DTR/RTS state of this port.
1703 * Syntax:
1704 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1705 * int port : port number (0 - 127)
1706 * int * dtrState : pointer to INT to receive the current DTR
1707 * state. (if NULL, this function will not
1708 * write to this address)
1709 * int * rtsState : pointer to INT to receive the current RTS
1710 * state. (if NULL, this function will not
1711 * write to this address)
1712 *
1713 * return: -1 : this port is invalid
1714 * 0 : O.K.
1715 *
1716 *
1717 * Function 14: Setting the DTR/RTS output state of this port.
1718 * Syntax:
1719 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1720 * int port : port number (0 - 127)
1721 * int dtrState : DTR output state (0: off, 1: on)
1722 * int rtsState : RTS output state (0: off, 1: on)
1723 *
1724 *
1725 * Function 15: Setting the flow control of this port.
1726 * Syntax:
1727 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1728 * int txFlow,int xany);
1729 * int port : port number (0 - 127)
1730 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1731 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1732 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1733 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1734 * int xany : S/W XANY flow control (0: no, 1: yes)
1735 *
1736 *
1737 * Function 16: Get ths line status of this port
1738 * Syntax:
1739 * int MoxaPortLineStatus(int port);
1740 * int port : port number (0 - 127)
1741 *
1742 * return: Bit 0 - CTS state (0: off, 1: on)
1743 * Bit 1 - DSR state (0: off, 1: on)
1744 * Bit 2 - DCD state (0: off, 1: on)
1745 *
1746 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 * Function 19: Flush the Rx/Tx buffer data of this port.
1748 * Syntax:
1749 * void MoxaPortFlushData(int port, int mode);
1750 * int port : port number (0 - 127)
1751 * int mode
1752 * 0 : flush the Rx buffer
1753 * 1 : flush the Tx buffer
1754 * 2 : flush the Rx and Tx buffer
1755 *
1756 *
1757 * Function 20: Write data.
1758 * Syntax:
1759 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1760 * int port : port number (0 - 127)
1761 * unsigned char * buffer : pointer to write data buffer.
1762 * int length : write data length
1763 *
1764 * return: 0 - length : real write data length
1765 *
1766 *
1767 * Function 21: Read data.
1768 * Syntax:
Alan Cox33f0f882006-01-09 20:54:13 -08001769 * int MoxaPortReadData(int port, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 * int port : port number (0 - 127)
Alan Cox33f0f882006-01-09 20:54:13 -08001771 * struct tty_struct *tty : tty for data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 *
1773 * return: 0 - length : real read data length
1774 *
1775 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 * Function 24: Get the Tx buffer current queued data bytes
1777 * Syntax:
1778 * int MoxaPortTxQueue(int port);
1779 * int port : port number (0 - 127)
1780 *
1781 * return: .. : Tx buffer current queued data bytes
1782 *
1783 *
1784 * Function 25: Get the Tx buffer current free space
1785 * Syntax:
1786 * int MoxaPortTxFree(int port);
1787 * int port : port number (0 - 127)
1788 *
1789 * return: .. : Tx buffer current free space
1790 *
1791 *
1792 * Function 26: Get the Rx buffer current queued data bytes
1793 * Syntax:
1794 * int MoxaPortRxQueue(int port);
1795 * int port : port number (0 - 127)
1796 *
1797 * return: .. : Rx buffer current queued data bytes
1798 *
1799 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 * Function 28: Disable port data transmission.
1801 * Syntax:
1802 * void MoxaPortTxDisable(int port);
1803 * int port : port number (0 - 127)
1804 *
1805 *
1806 * Function 29: Enable port data transmission.
1807 * Syntax:
1808 * void MoxaPortTxEnable(int port);
1809 * int port : port number (0 - 127)
1810 *
1811 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 * Function 31: Get the received BREAK signal count and reset it.
1813 * Syntax:
1814 * int MoxaPortResetBrkCnt(int port);
1815 * int port : port number (0 - 127)
1816 *
1817 * return: 0 - .. : BREAK signal count
1818 *
1819 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Jiri Slabyb4173f42008-04-30 00:53:40 -07001822static void MoxaPortEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
1824 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001825 u16 lowwater = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Jiri Slabyb4173f42008-04-30 00:53:40 -07001827 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 writew(lowwater, ofsAddr + Low_water);
Jiri Slaby08d01c792008-04-30 00:53:47 -07001829 if (MOXA_IS_320(port->board))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001831 else
1832 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1833 ofsAddr + HostStat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1836 moxafunc(ofsAddr, FC_FlushQueue, 2);
1837
1838 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1839 MoxaPortLineStatus(port);
1840}
1841
Jiri Slabyb4173f42008-04-30 00:53:40 -07001842static void MoxaPortDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001844 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1847 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1848 writew(0, ofsAddr + HostStat);
1849 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1850}
1851
Jiri Slaby08d01c792008-04-30 00:53:47 -07001852static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
Jiri Slaby08d01c792008-04-30 00:53:47 -07001854 void __iomem *ofsAddr = port->tableAddr;
1855 unsigned int clock, val;
1856 speed_t max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Jiri Slaby08d01c792008-04-30 00:53:47 -07001858 max = MOXA_IS_320(port->board) ? 460800 : 921600;
1859 if (baud < 50)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001860 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (baud > max)
1862 baud = max;
Jiri Slaby08d01c792008-04-30 00:53:47 -07001863 clock = 921600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 val = clock / baud;
1865 moxafunc(ofsAddr, FC_SetBaud, val);
1866 baud = clock / val;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001867 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
Jiri Slabyb4173f42008-04-30 00:53:40 -07001870static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1871 speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
1873 void __iomem *ofsAddr;
1874 tcflag_t cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 tcflag_t mode = 0;
1876
Jiri Slabyb4173f42008-04-30 00:53:40 -07001877 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 cflag = termio->c_cflag; /* termio->c_cflag */
1879
1880 mode = termio->c_cflag & CSIZE;
1881 if (mode == CS5)
1882 mode = MX_CS5;
1883 else if (mode == CS6)
1884 mode = MX_CS6;
1885 else if (mode == CS7)
1886 mode = MX_CS7;
1887 else if (mode == CS8)
1888 mode = MX_CS8;
1889
1890 if (termio->c_cflag & CSTOPB) {
1891 if (mode == MX_CS5)
1892 mode |= MX_STOP15;
1893 else
1894 mode |= MX_STOP2;
1895 } else
1896 mode |= MX_STOP1;
1897
1898 if (termio->c_cflag & PARENB) {
1899 if (termio->c_cflag & PARODD)
1900 mode |= MX_PARODD;
1901 else
1902 mode |= MX_PAREVEN;
1903 } else
1904 mode |= MX_PARNONE;
1905
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001906 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Jiri Slaby08d01c792008-04-30 00:53:47 -07001908 if (MOXA_IS_320(port->board) && baud >= 921600)
1909 return -1;
1910
Alan Coxdb1acaa2008-02-08 04:18:43 -08001911 baud = MoxaPortSetBaud(port, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
1913 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1914 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1915 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1916 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001917 moxa_wait_finish(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 }
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001920 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921}
1922
Jiri Slabyb4173f42008-04-30 00:53:40 -07001923static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1924 int *rtsState)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001926 if (dtrState)
1927 *dtrState = !!(port->lineCtrl & DTR_ON);
1928 if (rtsState)
1929 *rtsState = !!(port->lineCtrl & RTS_ON);
1930
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001931 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932}
1933
Jiri Slabyb4173f42008-04-30 00:53:40 -07001934static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001936 u8 mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 if (dtr)
1939 mode |= DTR_ON;
1940 if (rts)
1941 mode |= RTS_ON;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001942 port->lineCtrl = mode;
1943 moxafunc(port->tableAddr, FC_LineControl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944}
1945
Jiri Slabyb4173f42008-04-30 00:53:40 -07001946static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1947 int txflow, int rxflow, int txany)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001949 int mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 if (rts)
1952 mode |= RTS_FlowCtl;
1953 if (cts)
1954 mode |= CTS_FlowCtl;
1955 if (txflow)
1956 mode |= Tx_FlowCtl;
1957 if (rxflow)
1958 mode |= Rx_FlowCtl;
1959 if (txany)
1960 mode |= IXM_IXANY;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001961 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962}
1963
Jiri Slabyb4173f42008-04-30 00:53:40 -07001964static int MoxaPortLineStatus(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
1966 void __iomem *ofsAddr;
1967 int val;
1968
Jiri Slabyb4173f42008-04-30 00:53:40 -07001969 ofsAddr = port->tableAddr;
Jiri Slaby08d01c792008-04-30 00:53:47 -07001970 if (MOXA_IS_320(port->board)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 moxafunc(ofsAddr, FC_LineStatus, 0);
1972 val = readw(ofsAddr + FuncArg);
1973 } else {
1974 val = readw(ofsAddr + FlagStat) >> 4;
1975 }
1976 val &= 0x0B;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001977 if (val & 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 val |= 4;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001979 spin_lock_bh(&moxa_lock);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001980 moxa_new_dcdstate(port, val & 8);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001981 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 val &= 7;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001983 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984}
1985
Alan Coxd450b5a2008-10-13 10:39:58 +01001986static int MoxaPortWriteData(struct tty_struct *tty,
Jiri Slaby2108eba2008-04-30 00:53:44 -07001987 const unsigned char *buffer, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
Alan Coxd450b5a2008-10-13 10:39:58 +01001989 struct moxa_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001991 unsigned int c, total;
1992 u16 head, tail, tx_mask, spage, epage;
1993 u16 pageno, pageofs, bufhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Jiri Slabyb4173f42008-04-30 00:53:40 -07001995 ofsAddr = port->tableAddr;
1996 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 tx_mask = readw(ofsAddr + TX_mask);
1998 spage = readw(ofsAddr + Page_txb);
1999 epage = readw(ofsAddr + EndPage_txb);
2000 tail = readw(ofsAddr + TXwptr);
2001 head = readw(ofsAddr + TXrptr);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002002 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (c > len)
2004 c = len;
Alan Cox9de6a512008-07-16 21:56:02 +01002005 moxaLog.txcnt[port->port.tty->index] += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 total = c;
2007 if (spage == epage) {
2008 bufhead = readw(ofsAddr + Ofs_txb);
2009 writew(spage, baseAddr + Control_reg);
2010 while (c > 0) {
2011 if (head > tail)
2012 len = head - tail - 1;
2013 else
2014 len = tx_mask + 1 - tail;
2015 len = (c > len) ? len : c;
2016 ofs = baseAddr + DynPage_addr + bufhead + tail;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002017 memcpy_toio(ofs, buffer, len);
2018 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 tail = (tail + len) & tx_mask;
2020 c -= len;
2021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 pageno = spage + (tail >> 13);
2024 pageofs = tail & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002025 while (c > 0) {
2026 len = Page_size - pageofs;
2027 if (len > c)
2028 len = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 writeb(pageno, baseAddr + Control_reg);
2030 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002031 memcpy_toio(ofs, buffer, len);
2032 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 if (++pageno == epage)
2034 pageno = spage;
2035 pageofs = 0;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002036 c -= len;
2037 }
2038 tail = (tail + total) & tx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002040 writew(tail, ofsAddr + TXwptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 writeb(1, ofsAddr + CD180TXirq); /* start to send */
Jiri Slaby2108eba2008-04-30 00:53:44 -07002042 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043}
2044
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002045static int MoxaPortReadData(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
Alan Cox9de6a512008-07-16 21:56:02 +01002047 struct tty_struct *tty = port->port.tty;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002048 unsigned char *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002050 unsigned int count, len, total;
2051 u16 tail, rx_mask, spage, epage;
2052 u16 pageno, pageofs, bufhead, head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Jiri Slabyb4173f42008-04-30 00:53:40 -07002054 ofsAddr = port->tableAddr;
2055 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 head = readw(ofsAddr + RXrptr);
2057 tail = readw(ofsAddr + RXwptr);
2058 rx_mask = readw(ofsAddr + RX_mask);
2059 spage = readw(ofsAddr + Page_rxb);
2060 epage = readw(ofsAddr + EndPage_rxb);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002061 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 if (count == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08002063 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Alan Cox33f0f882006-01-09 20:54:13 -08002065 total = count;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002066 moxaLog.rxcnt[tty->index] += total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 if (spage == epage) {
2068 bufhead = readw(ofsAddr + Ofs_rxb);
2069 writew(spage, baseAddr + Control_reg);
2070 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 ofs = baseAddr + DynPage_addr + bufhead + head;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002072 len = (tail >= head) ? (tail - head) :
2073 (rx_mask + 1 - head);
2074 len = tty_prepare_flip_string(tty, &dst,
2075 min(len, count));
2076 memcpy_fromio(dst, ofs, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 head = (head + len) & rx_mask;
2078 count -= len;
2079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 pageno = spage + (head >> 13);
2082 pageofs = head & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002083 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 writew(pageno, baseAddr + Control_reg);
2085 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002086 len = tty_prepare_flip_string(tty, &dst,
2087 min(Page_size - pageofs, count));
2088 memcpy_fromio(dst, ofs, len);
2089
2090 count -= len;
2091 pageofs = (pageofs + len) & Page_mask;
2092 if (pageofs == 0 && ++pageno == epage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 pageno = spage;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002094 }
2095 head = (head + total) & rx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002097 writew(head, ofsAddr + RXrptr);
2098 if (readb(ofsAddr + FlagStat) & Xoff_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 moxaLowWaterChk = 1;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002100 port->lowChkFlag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002102 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103}
2104
2105
Jiri Slabyb4173f42008-04-30 00:53:40 -07002106static int MoxaPortTxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002108 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002109 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 rptr = readw(ofsAddr + TXrptr);
2112 wptr = readw(ofsAddr + TXwptr);
2113 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002114 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115}
2116
Jiri Slabyb4173f42008-04-30 00:53:40 -07002117static int MoxaPortTxFree(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002119 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002120 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 rptr = readw(ofsAddr + TXrptr);
2123 wptr = readw(ofsAddr + TXwptr);
2124 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002125 return mask - ((wptr - rptr) & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
Jiri Slabyb4173f42008-04-30 00:53:40 -07002128static int MoxaPortRxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002130 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002131 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 rptr = readw(ofsAddr + RXrptr);
2134 wptr = readw(ofsAddr + RXwptr);
2135 mask = readw(ofsAddr + RX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002136 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137}
2138
Jiri Slabyb4173f42008-04-30 00:53:40 -07002139static void MoxaPortTxDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002141 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
2143
Jiri Slabyb4173f42008-04-30 00:53:40 -07002144static void MoxaPortTxEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002146 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002149static int moxa_get_serial_info(struct moxa_port *info,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002150 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002152 struct serial_struct tmp = {
2153 .type = info->type,
Alan Cox9de6a512008-07-16 21:56:02 +01002154 .line = info->port.tty->index,
2155 .flags = info->port.flags,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002156 .baud_base = 921600,
Alan Cox44b7d1b2008-07-16 21:57:18 +01002157 .close_delay = info->port.close_delay
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002158 };
2159 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160}
2161
2162
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002163static int moxa_set_serial_info(struct moxa_port *info,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002164 struct serial_struct __user *new_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
2166 struct serial_struct new_serial;
2167
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002168 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 return -EFAULT;
2170
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002171 if (new_serial.irq != 0 || new_serial.port != 0 ||
2172 new_serial.custom_divisor != 0 ||
2173 new_serial.baud_base != 921600)
2174 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
2176 if (!capable(CAP_SYS_ADMIN)) {
2177 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
Alan Cox9de6a512008-07-16 21:56:02 +01002178 (info->port.flags & ~ASYNC_USR_MASK)))
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002179 return -EPERM;
2180 } else
Alan Cox44b7d1b2008-07-16 21:57:18 +01002181 info->port.close_delay = new_serial.close_delay * HZ / 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
2183 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
Alan Cox9de6a512008-07-16 21:56:02 +01002184 new_serial.flags |= (info->port.flags & ASYNC_FLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002186 MoxaSetFifo(info, new_serial.type == PORT_16550A);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 info->type = new_serial.type;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002189 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190}
2191
2192
2193
2194/*****************************************************************************
2195 * Static local functions: *
2196 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Jiri Slabyb4173f42008-04-30 00:53:40 -07002198static void MoxaSetFifo(struct moxa_port *port, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002200 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
2202 if (!enable) {
2203 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2204 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2205 } else {
2206 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2207 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2208 }
2209}