blob: dd10f143d96f95cf8365ee79e9960f6a95726f1f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*****************************************************************************/
2/*
3 * moxa.c -- MOXA Intellio family multiport serial driver.
4 *
Jiri Slabyb9705b62008-04-30 00:53:48 -07005 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com).
6 * Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This code is loosely based on the Linux serial driver, written by
9 * Linus Torvalds, Theodore T'so and others.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
17/*
18 * MOXA Intellio Series Driver
19 * for : LINUX
20 * date : 1999/1/7
21 * version : 5.1
22 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25#include <linux/types.h>
26#include <linux/mm.h>
27#include <linux/ioport.h>
28#include <linux/errno.h>
Jiri Slaby03718232008-04-30 00:53:39 -070029#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/signal.h>
31#include <linux/sched.h>
32#include <linux/timer.h>
33#include <linux/interrupt.h>
34#include <linux/tty.h>
35#include <linux/tty_flip.h>
36#include <linux/major.h>
37#include <linux/string.h>
38#include <linux/fcntl.h>
39#include <linux/ptrace.h>
40#include <linux/serial.h>
41#include <linux/tty_driver.h>
42#include <linux/delay.h>
43#include <linux/pci.h>
44#include <linux/init.h>
45#include <linux/bitops.h>
46
47#include <asm/system.h>
48#include <asm/io.h>
49#include <asm/uaccess.h>
50
Jiri Slaby03718232008-04-30 00:53:39 -070051#include "moxa.h"
52
Jiri Slabyb9705b62008-04-30 00:53:48 -070053#define MOXA_VERSION "6.0k"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Jiri Slaby03718232008-04-30 00:53:39 -070055#define MOXA_FW_HDRLEN 32
56
Jiri Slaby11324ed2007-02-10 01:45:31 -080057#define MOXAMAJOR 172
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Jiri Slaby11324ed2007-02-10 01:45:31 -080059#define MAX_BOARDS 4 /* Don't change this value */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
Jiri Slaby11324ed2007-02-10 01:45:31 -080061#define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Jiri Slaby08d01c72008-04-30 00:53:47 -070063#define MOXA_IS_320(brd) ((brd)->boardType == MOXA_BOARD_C320_ISA || \
64 (brd)->boardType == MOXA_BOARD_C320_PCI)
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/*
67 * Define the Moxa PCI vendor and device IDs.
68 */
Jiri Slaby11324ed2007-02-10 01:45:31 -080069#define MOXA_BUS_TYPE_ISA 0
70#define MOXA_BUS_TYPE_PCI 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072enum {
73 MOXA_BOARD_C218_PCI = 1,
74 MOXA_BOARD_C218_ISA,
75 MOXA_BOARD_C320_PCI,
76 MOXA_BOARD_C320_ISA,
77 MOXA_BOARD_CP204J,
78};
79
80static char *moxa_brdname[] =
81{
82 "C218 Turbo PCI series",
83 "C218 Turbo ISA series",
84 "C320 Turbo PCI series",
85 "C320 Turbo ISA series",
86 "CP-204J series",
87};
88
89#ifdef CONFIG_PCI
90static struct pci_device_id moxa_pcibrds[] = {
Jiri Slaby5ebb4072007-02-10 01:45:30 -080091 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
92 .driver_data = MOXA_BOARD_C218_PCI },
93 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
94 .driver_data = MOXA_BOARD_C320_PCI },
95 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
96 .driver_data = MOXA_BOARD_CP204J },
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 { 0 }
98};
99MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
100#endif /* CONFIG_PCI */
101
Jiri Slaby03718232008-04-30 00:53:39 -0700102struct moxa_port;
103
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800104static struct moxa_board_conf {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 int boardType;
106 int numPorts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 int busType;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800108
Jiri Slaby810ab092008-04-30 00:53:41 -0700109 unsigned int ready;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800110
Jiri Slaby03718232008-04-30 00:53:39 -0700111 struct moxa_port *ports;
112
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800113 void __iomem *basemem;
114 void __iomem *intNdx;
115 void __iomem *intPend;
116 void __iomem *intTable;
117} moxa_boards[MAX_BOARDS];
118
119struct mxser_mstatus {
120 tcflag_t cflag;
121 int cts;
122 int dsr;
123 int ri;
124 int dcd;
Jiri Slaby9dff89c2007-02-10 01:45:30 -0800125};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800127struct moxaq_str {
128 int inq;
129 int outq;
130};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800132struct moxa_port {
Alan Cox9de6a512008-07-16 21:56:02 +0100133 struct tty_port port;
Jiri Slabyb4173f42008-04-30 00:53:40 -0700134 struct moxa_board_conf *board;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700135 void __iomem *tableAddr;
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 int close_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 int cflag;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700140 unsigned long statusflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700142 u8 DCDState;
143 u8 lineCtrl;
144 u8 lowChkFlag;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800145};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Jiri Slaby74d7d972008-04-30 00:53:43 -0700147struct mon_str {
148 int tick;
149 int rxcnt[MAX_PORTS];
150 int txcnt[MAX_PORTS];
151};
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/* statusflags */
154#define TXSTOPPED 0x1
155#define LOWWAIT 0x2
156#define EMPTYWAIT 0x4
157#define THROTTLE 0x8
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#define SERIAL_DO_RESTART
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#define WAKEUP_CHARS 256
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163static int ttymajor = MOXAMAJOR;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700164static struct mon_str moxaLog;
165static unsigned int moxaFuncTout = HZ / 2;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700166static unsigned int moxaLowWaterChk;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700167static DEFINE_MUTEX(moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/* Variables for insmod */
169#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700170static unsigned long baseaddr[MAX_BOARDS];
171static unsigned int type[MAX_BOARDS];
172static unsigned int numports[MAX_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173#endif
174
175MODULE_AUTHOR("William Chen");
176MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
177MODULE_LICENSE("GPL");
178#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700179module_param_array(type, uint, NULL, 0);
180MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
181module_param_array(baseaddr, ulong, NULL, 0);
182MODULE_PARM_DESC(baseaddr, "base address");
183module_param_array(numports, uint, NULL, 0);
184MODULE_PARM_DESC(numports, "numports (ignored for C218)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185#endif
186module_param(ttymajor, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/*
189 * static functions:
190 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191static int moxa_open(struct tty_struct *, struct file *);
192static void moxa_close(struct tty_struct *, struct file *);
193static int moxa_write(struct tty_struct *, const unsigned char *, int);
194static int moxa_write_room(struct tty_struct *);
195static void moxa_flush_buffer(struct tty_struct *);
196static int moxa_chars_in_buffer(struct tty_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197static void moxa_throttle(struct tty_struct *);
198static void moxa_unthrottle(struct tty_struct *);
Alan Cox606d0992006-12-08 02:38:45 -0800199static void moxa_set_termios(struct tty_struct *, struct ktermios *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200static void moxa_stop(struct tty_struct *);
201static void moxa_start(struct tty_struct *);
202static void moxa_hangup(struct tty_struct *);
203static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
204static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
205 unsigned int set, unsigned int clear);
206static void moxa_poll(unsigned long);
Alan Coxdb1acaa2008-02-08 04:18:43 -0800207static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
Jiri Slaby6f56b6582007-10-18 03:06:24 -0700208static void moxa_setup_empty_event(struct tty_struct *);
Jiri Slaby6f56b6582007-10-18 03:06:24 -0700209static void moxa_shut_down(struct moxa_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);
Jiri Slaby2108eba2008-04-30 00:53:44 -0700221static int MoxaPortWriteData(struct moxa_port *, 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++) {
336 memset(&tmp, 0, sizeof(tmp));
337 if (!moxa_boards[i].ready)
338 goto copy;
339
340 status = MoxaPortLineStatus(p);
341 if (status & 1)
342 tmp.cts = 1;
343 if (status & 2)
344 tmp.dsr = 1;
345 if (status & 4)
346 tmp.dcd = 1;
347
Alan Cox9de6a512008-07-16 21:56:02 +0100348 if (!p->port.tty || !p->port.tty->termios)
Jiri Slaby74d7d972008-04-30 00:53:43 -0700349 tmp.cflag = p->cflag;
350 else
Alan Cox9de6a512008-07-16 21:56:02 +0100351 tmp.cflag = p->port.tty->termios->c_cflag;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700352copy:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700353 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
354 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -0700355 return -EFAULT;
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700356 }
Jiri Slaby74d7d972008-04-30 00:53:43 -0700357 }
358 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700359 mutex_unlock(&moxa_openlock);
360 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700361 }
362 case TIOCGSERIAL:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700363 mutex_lock(&moxa_openlock);
364 ret = moxa_get_serial_info(ch, argp);
365 mutex_unlock(&moxa_openlock);
366 break;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700367 case TIOCSSERIAL:
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700368 mutex_lock(&moxa_openlock);
369 ret = moxa_set_serial_info(ch, argp);
370 mutex_unlock(&moxa_openlock);
371 break;
372 default:
373 ret = -ENOIOCTLCMD;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700374 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700375 return ret;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700376}
377
378static void moxa_break_ctl(struct tty_struct *tty, int state)
379{
380 struct moxa_port *port = tty->driver_data;
381
382 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
383 Magic_code);
384}
385
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700386static const struct tty_operations moxa_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 .open = moxa_open,
388 .close = moxa_close,
389 .write = moxa_write,
390 .write_room = moxa_write_room,
391 .flush_buffer = moxa_flush_buffer,
392 .chars_in_buffer = moxa_chars_in_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 .ioctl = moxa_ioctl,
394 .throttle = moxa_throttle,
395 .unthrottle = moxa_unthrottle,
396 .set_termios = moxa_set_termios,
397 .stop = moxa_stop,
398 .start = moxa_start,
399 .hangup = moxa_hangup,
Jiri Slaby74d7d972008-04-30 00:53:43 -0700400 .break_ctl = moxa_break_ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 .tiocmget = moxa_tiocmget,
402 .tiocmset = moxa_tiocmset,
403};
404
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800405static struct tty_driver *moxaDriver;
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800406static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
Ingo Molnar34af9462006-06-27 02:53:55 -0700407static DEFINE_SPINLOCK(moxa_lock);
Alan Cox33f0f882006-01-09 20:54:13 -0800408
Jiri Slaby74d7d972008-04-30 00:53:43 -0700409/*
410 * HW init
411 */
412
Jiri Slaby03718232008-04-30 00:53:39 -0700413static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
414{
415 switch (brd->boardType) {
416 case MOXA_BOARD_C218_ISA:
417 case MOXA_BOARD_C218_PCI:
418 if (model != 1)
419 goto err;
420 break;
421 case MOXA_BOARD_CP204J:
422 if (model != 3)
423 goto err;
424 break;
425 default:
426 if (model != 2)
427 goto err;
428 break;
429 }
430 return 0;
431err:
432 return -EINVAL;
433}
434
435static int moxa_check_fw(const void *ptr)
436{
437 const __le16 *lptr = ptr;
438
439 if (*lptr != cpu_to_le16(0x7980))
440 return -EINVAL;
441
442 return 0;
443}
444
445static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
446 size_t len)
447{
448 void __iomem *baseAddr = brd->basemem;
449 u16 tmp;
450
451 writeb(HW_reset, baseAddr + Control_reg); /* reset */
452 msleep(10);
453 memset_io(baseAddr, 0, 4096);
454 memcpy_toio(baseAddr, buf, len); /* download BIOS */
455 writeb(0, baseAddr + Control_reg); /* restart */
456
457 msleep(2000);
458
459 switch (brd->boardType) {
460 case MOXA_BOARD_C218_ISA:
461 case MOXA_BOARD_C218_PCI:
462 tmp = readw(baseAddr + C218_key);
463 if (tmp != C218_KeyCode)
464 goto err;
465 break;
466 case MOXA_BOARD_CP204J:
467 tmp = readw(baseAddr + C218_key);
468 if (tmp != CP204J_KeyCode)
469 goto err;
470 break;
471 default:
472 tmp = readw(baseAddr + C320_key);
473 if (tmp != C320_KeyCode)
474 goto err;
475 tmp = readw(baseAddr + C320_status);
476 if (tmp != STS_init) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700477 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
Jiri Slaby03718232008-04-30 00:53:39 -0700478 "module not found\n");
479 return -EIO;
480 }
481 break;
482 }
483
484 return 0;
485err:
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700486 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700487 return -EIO;
488}
489
490static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
491 size_t len)
492{
493 void __iomem *baseAddr = brd->basemem;
494
495 if (len < 7168) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700496 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700497 return -EINVAL;
498 }
499
500 writew(len - 7168 - 2, baseAddr + C320bapi_len);
501 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
502 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
503 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
504 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
505
506 return 0;
507}
508
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700509static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
Jiri Slaby03718232008-04-30 00:53:39 -0700510 size_t len)
511{
512 void __iomem *baseAddr = brd->basemem;
513 const u16 *uptr = ptr;
514 size_t wlen, len2, j;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700515 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
Jiri Slaby08d01c72008-04-30 00:53:47 -0700516 unsigned int i, retry;
Jiri Slaby03718232008-04-30 00:53:39 -0700517 u16 usum, keycode;
518
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700519 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
520 C218_KeyCode;
521
522 switch (brd->boardType) {
523 case MOXA_BOARD_CP204J:
524 case MOXA_BOARD_C218_ISA:
525 case MOXA_BOARD_C218_PCI:
526 key = C218_key;
527 loadbuf = C218_LoadBuf;
528 loadlen = C218DLoad_len;
529 checksum = C218check_sum;
530 checksum_ok = C218chksum_ok;
531 break;
532 default:
533 key = C320_key;
534 keycode = C320_KeyCode;
535 loadbuf = C320_LoadBuf;
536 loadlen = C320DLoad_len;
537 checksum = C320check_sum;
538 checksum_ok = C320chksum_ok;
539 break;
540 }
541
Jiri Slaby03718232008-04-30 00:53:39 -0700542 usum = 0;
543 wlen = len >> 1;
544 for (i = 0; i < wlen; i++)
545 usum += le16_to_cpu(uptr[i]);
546 retry = 0;
547 do {
548 wlen = len >> 1;
549 j = 0;
550 while (wlen) {
551 len2 = (wlen > 2048) ? 2048 : wlen;
552 wlen -= len2;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700553 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
Jiri Slaby03718232008-04-30 00:53:39 -0700554 j += len2 << 1;
555
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700556 writew(len2, baseAddr + loadlen);
557 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700558 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700559 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700560 break;
561 msleep(10);
562 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700563 if (readw(baseAddr + key) != keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700564 return -EIO;
565 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700566 writew(0, baseAddr + loadlen);
567 writew(usum, baseAddr + checksum);
568 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700569 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700570 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700571 break;
572 msleep(10);
573 }
574 retry++;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700575 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
576 if (readb(baseAddr + checksum_ok) != 1)
Jiri Slaby03718232008-04-30 00:53:39 -0700577 return -EIO;
578
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700579 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700580 for (i = 0; i < 600; i++) {
581 if (readw(baseAddr + Magic_no) == Magic_code)
582 break;
583 msleep(10);
584 }
585 if (readw(baseAddr + Magic_no) != Magic_code)
586 return -EIO;
587
Jiri Slaby08d01c72008-04-30 00:53:47 -0700588 if (MOXA_IS_320(brd)) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700589 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
590 writew(0x3800, baseAddr + TMS320_PORT1);
591 writew(0x3900, baseAddr + TMS320_PORT2);
592 writew(28499, baseAddr + TMS320_CLOCK);
593 } else {
594 writew(0x3200, baseAddr + TMS320_PORT1);
595 writew(0x3400, baseAddr + TMS320_PORT2);
596 writew(19999, baseAddr + TMS320_CLOCK);
597 }
Jiri Slaby03718232008-04-30 00:53:39 -0700598 }
599 writew(1, baseAddr + Disable_IRQ);
600 writew(0, baseAddr + Magic_no);
601 for (i = 0; i < 500; i++) {
602 if (readw(baseAddr + Magic_no) == Magic_code)
603 break;
604 msleep(10);
605 }
606 if (readw(baseAddr + Magic_no) != Magic_code)
607 return -EIO;
608
Jiri Slaby08d01c72008-04-30 00:53:47 -0700609 if (MOXA_IS_320(brd)) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700610 j = readw(baseAddr + Module_cnt);
611 if (j <= 0)
612 return -EIO;
613 brd->numPorts = j * 8;
614 writew(j, baseAddr + Module_no);
615 writew(0, baseAddr + Magic_no);
616 for (i = 0; i < 600; i++) {
617 if (readw(baseAddr + Magic_no) == Magic_code)
618 break;
619 msleep(10);
620 }
621 if (readw(baseAddr + Magic_no) != Magic_code)
622 return -EIO;
Jiri Slaby03718232008-04-30 00:53:39 -0700623 }
Jiri Slaby03718232008-04-30 00:53:39 -0700624 brd->intNdx = baseAddr + IRQindex;
625 brd->intPend = baseAddr + IRQpending;
626 brd->intTable = baseAddr + IRQtable;
627
628 return 0;
629}
630
631static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
632 size_t len)
633{
634 void __iomem *ofsAddr, *baseAddr = brd->basemem;
635 struct moxa_port *port;
636 int retval, i;
637
638 if (len % 2) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700639 printk(KERN_ERR "MOXA: bios length is not even\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700640 return -EINVAL;
641 }
642
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700643 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
644 if (retval)
645 return retval;
646
Jiri Slaby03718232008-04-30 00:53:39 -0700647 switch (brd->boardType) {
648 case MOXA_BOARD_C218_ISA:
649 case MOXA_BOARD_C218_PCI:
650 case MOXA_BOARD_CP204J:
Jiri Slaby03718232008-04-30 00:53:39 -0700651 port = brd->ports;
652 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700653 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700654 port->DCDState = 0;
655 port->tableAddr = baseAddr + Extern_table +
656 Extern_size * i;
657 ofsAddr = port->tableAddr;
658 writew(C218rx_mask, ofsAddr + RX_mask);
659 writew(C218tx_mask, ofsAddr + TX_mask);
660 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
661 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
662
663 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
664 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
665
666 }
667 break;
668 default:
Jiri Slaby03718232008-04-30 00:53:39 -0700669 port = brd->ports;
670 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700671 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700672 port->DCDState = 0;
673 port->tableAddr = baseAddr + Extern_table +
674 Extern_size * i;
675 ofsAddr = port->tableAddr;
676 switch (brd->numPorts) {
677 case 8:
678 writew(C320p8rx_mask, ofsAddr + RX_mask);
679 writew(C320p8tx_mask, ofsAddr + TX_mask);
680 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
681 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
682 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
683 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
684
685 break;
686 case 16:
687 writew(C320p16rx_mask, ofsAddr + RX_mask);
688 writew(C320p16tx_mask, ofsAddr + TX_mask);
689 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
690 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
691 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
692 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
693 break;
694
695 case 24:
696 writew(C320p24rx_mask, ofsAddr + RX_mask);
697 writew(C320p24tx_mask, ofsAddr + TX_mask);
698 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
699 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
700 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
701 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
702 break;
703 case 32:
704 writew(C320p32rx_mask, ofsAddr + RX_mask);
705 writew(C320p32tx_mask, ofsAddr + TX_mask);
706 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
707 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
708 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
709 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
710 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
711 break;
712 }
713 }
714 break;
715 }
Jiri Slaby03718232008-04-30 00:53:39 -0700716 return 0;
717}
718
719static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
720{
David Howells2bca76e2008-07-08 17:37:15 +0100721 const void *ptr = fw->data;
Jiri Slaby03718232008-04-30 00:53:39 -0700722 char rsn[64];
723 u16 lens[5];
724 size_t len;
725 unsigned int a, lenp, lencnt;
726 int ret = -EINVAL;
727 struct {
728 __le32 magic; /* 0x34303430 */
729 u8 reserved1[2];
730 u8 type; /* UNIX = 3 */
731 u8 model; /* C218T=1, C320T=2, CP204=3 */
732 u8 reserved2[8];
733 __le16 len[5];
David Howells2bca76e2008-07-08 17:37:15 +0100734 } const *hdr = ptr;
Jiri Slaby03718232008-04-30 00:53:39 -0700735
736 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
737
738 if (fw->size < MOXA_FW_HDRLEN) {
739 strcpy(rsn, "too short (even header won't fit)");
740 goto err;
741 }
742 if (hdr->magic != cpu_to_le32(0x30343034)) {
743 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
744 goto err;
745 }
746 if (hdr->type != 3) {
747 sprintf(rsn, "not for linux, type is %u", hdr->type);
748 goto err;
749 }
750 if (moxa_check_fw_model(brd, hdr->model)) {
751 sprintf(rsn, "not for this card, model is %u", hdr->model);
752 goto err;
753 }
754
755 len = MOXA_FW_HDRLEN;
756 lencnt = hdr->model == 2 ? 5 : 3;
757 for (a = 0; a < ARRAY_SIZE(lens); a++) {
758 lens[a] = le16_to_cpu(hdr->len[a]);
759 if (lens[a] && len + lens[a] <= fw->size &&
760 moxa_check_fw(&fw->data[len]))
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700761 printk(KERN_WARNING "MOXA firmware: unexpected input "
Jiri Slaby03718232008-04-30 00:53:39 -0700762 "at offset %u, but going on\n", (u32)len);
763 if (!lens[a] && a < lencnt) {
764 sprintf(rsn, "too few entries in fw file");
765 goto err;
766 }
767 len += lens[a];
768 }
769
770 if (len != fw->size) {
771 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
772 (u32)len);
773 goto err;
774 }
775
776 ptr += MOXA_FW_HDRLEN;
777 lenp = 0; /* bios */
778
779 strcpy(rsn, "read above");
780
781 ret = moxa_load_bios(brd, ptr, lens[lenp]);
782 if (ret)
783 goto err;
784
785 /* we skip the tty section (lens[1]), since we don't need it */
786 ptr += lens[lenp] + lens[lenp + 1];
787 lenp += 2; /* comm */
788
789 if (hdr->model == 2) {
790 ret = moxa_load_320b(brd, ptr, lens[lenp]);
791 if (ret)
792 goto err;
793 /* skip another tty */
794 ptr += lens[lenp] + lens[lenp + 1];
795 lenp += 2;
796 }
797
798 ret = moxa_load_code(brd, ptr, lens[lenp]);
799 if (ret)
800 goto err;
801
802 return 0;
803err:
804 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
805 return ret;
806}
807
808static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
809{
810 const struct firmware *fw;
811 const char *file;
Jiri Slaby810ab092008-04-30 00:53:41 -0700812 struct moxa_port *p;
813 unsigned int i;
Jiri Slaby03718232008-04-30 00:53:39 -0700814 int ret;
815
Jiri Slaby810ab092008-04-30 00:53:41 -0700816 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
817 GFP_KERNEL);
818 if (brd->ports == NULL) {
819 printk(KERN_ERR "cannot allocate memory for ports\n");
820 ret = -ENOMEM;
821 goto err;
822 }
823
824 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
825 p->type = PORT_16550A;
826 p->close_delay = 5 * HZ / 10;
827 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Alan Cox9de6a512008-07-16 21:56:02 +0100828 tty_port_init(&p->port);
Jiri Slaby810ab092008-04-30 00:53:41 -0700829 }
830
Jiri Slaby03718232008-04-30 00:53:39 -0700831 switch (brd->boardType) {
832 case MOXA_BOARD_C218_ISA:
833 case MOXA_BOARD_C218_PCI:
834 file = "c218tunx.cod";
835 break;
836 case MOXA_BOARD_CP204J:
837 file = "cp204unx.cod";
838 break;
839 default:
840 file = "c320tunx.cod";
841 break;
842 }
843
844 ret = request_firmware(&fw, file, dev);
845 if (ret) {
Jiri Slabyec09cd52008-04-30 00:53:49 -0700846 printk(KERN_ERR "MOXA: request_firmware failed. Make sure "
847 "you've placed '%s' file into your firmware "
848 "loader directory (e.g. /lib/firmware)\n",
849 file);
Jiri Slaby810ab092008-04-30 00:53:41 -0700850 goto err_free;
Jiri Slaby03718232008-04-30 00:53:39 -0700851 }
852
853 ret = moxa_load_fw(brd, fw);
854
855 release_firmware(fw);
Jiri Slaby810ab092008-04-30 00:53:41 -0700856
857 if (ret)
858 goto err_free;
859
Jiri Slaby2a541342008-04-30 00:53:45 -0700860 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700861 brd->ready = 1;
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700862 if (!timer_pending(&moxaTimer))
863 mod_timer(&moxaTimer, jiffies + HZ / 50);
Jiri Slaby2a541342008-04-30 00:53:45 -0700864 spin_unlock_bh(&moxa_lock);
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700865
Jiri Slaby810ab092008-04-30 00:53:41 -0700866 return 0;
867err_free:
868 kfree(brd->ports);
869err:
Jiri Slaby03718232008-04-30 00:53:39 -0700870 return ret;
871}
872
Jiri Slaby810ab092008-04-30 00:53:41 -0700873static void moxa_board_deinit(struct moxa_board_conf *brd)
874{
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700875 unsigned int a, opened;
876
877 mutex_lock(&moxa_openlock);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700878 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700879 brd->ready = 0;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700880 spin_unlock_bh(&moxa_lock);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700881
882 /* pci hot-un-plug support */
883 for (a = 0; a < brd->numPorts; a++)
Alan Cox9de6a512008-07-16 21:56:02 +0100884 if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
885 tty_hangup(brd->ports[a].port.tty);
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700886 while (1) {
887 opened = 0;
888 for (a = 0; a < brd->numPorts; a++)
Alan Cox9de6a512008-07-16 21:56:02 +0100889 if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
Jiri Slabya8f5cda2008-04-30 00:53:45 -0700890 opened++;
891 mutex_unlock(&moxa_openlock);
892 if (!opened)
893 break;
894 msleep(50);
895 mutex_lock(&moxa_openlock);
896 }
897
Jiri Slaby810ab092008-04-30 00:53:41 -0700898 iounmap(brd->basemem);
899 brd->basemem = NULL;
900 kfree(brd->ports);
901}
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903#ifdef CONFIG_PCI
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800904static int __devinit moxa_pci_probe(struct pci_dev *pdev,
905 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800907 struct moxa_board_conf *board;
908 unsigned int i;
909 int board_type = ent->driver_data;
910 int retval;
911
912 retval = pci_enable_device(pdev);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700913 if (retval) {
914 dev_err(&pdev->dev, "can't enable pci device\n");
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800915 goto err;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700916 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800917
918 for (i = 0; i < MAX_BOARDS; i++)
919 if (moxa_boards[i].basemem == NULL)
920 break;
921
922 retval = -ENODEV;
923 if (i >= MAX_BOARDS) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700924 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800925 "found. Board is ignored.\n", MAX_BOARDS);
926 goto err;
927 }
928
929 board = &moxa_boards[i];
Jiri Slabye46a5e32008-04-30 00:53:37 -0700930
931 retval = pci_request_region(pdev, 2, "moxa-base");
932 if (retval) {
933 dev_err(&pdev->dev, "can't request pci region 2\n");
934 goto err;
935 }
936
Alan Cox24cb2332008-04-30 00:54:19 -0700937 board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700938 if (board->basemem == NULL) {
939 dev_err(&pdev->dev, "can't remap io space 2\n");
Jiri Slabye46a5e32008-04-30 00:53:37 -0700940 goto err_reg;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700941 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 board->boardType = board_type;
944 switch (board_type) {
945 case MOXA_BOARD_C218_ISA:
946 case MOXA_BOARD_C218_PCI:
947 board->numPorts = 8;
948 break;
949
950 case MOXA_BOARD_CP204J:
951 board->numPorts = 4;
952 break;
953 default:
954 board->numPorts = 0;
955 break;
956 }
957 board->busType = MOXA_BUS_TYPE_PCI;
Jiri Slabya784bf72007-02-10 01:45:36 -0800958
Jiri Slaby03718232008-04-30 00:53:39 -0700959 retval = moxa_init_board(board, &pdev->dev);
960 if (retval)
961 goto err_base;
962
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800963 pci_set_drvdata(pdev, board);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Jiri Slabybb9f9102008-04-30 00:53:48 -0700965 dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n",
966 moxa_brdname[board_type - 1], board->numPorts);
967
Jiri Slabyeaa95a82008-04-30 00:53:46 -0700968 return 0;
Jiri Slaby03718232008-04-30 00:53:39 -0700969err_base:
970 iounmap(board->basemem);
971 board->basemem = NULL;
Jiri Slabye46a5e32008-04-30 00:53:37 -0700972err_reg:
973 pci_release_region(pdev, 2);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800974err:
975 return retval;
976}
977
978static void __devexit moxa_pci_remove(struct pci_dev *pdev)
979{
980 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
981
Jiri Slaby810ab092008-04-30 00:53:41 -0700982 moxa_board_deinit(brd);
983
Jiri Slabye46a5e32008-04-30 00:53:37 -0700984 pci_release_region(pdev, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
Jiri Slabya784bf72007-02-10 01:45:36 -0800986
987static struct pci_driver moxa_pci_driver = {
988 .name = "moxa",
989 .id_table = moxa_pcibrds,
990 .probe = moxa_pci_probe,
991 .remove = __devexit_p(moxa_pci_remove)
992};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993#endif /* CONFIG_PCI */
994
995static int __init moxa_init(void)
996{
Jiri Slaby810ab092008-04-30 00:53:41 -0700997 unsigned int isabrds = 0;
Jiri Slabyd353eca2008-04-30 00:53:37 -0700998 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001000 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
1001 MOXA_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
1003 if (!moxaDriver)
1004 return -ENOMEM;
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 moxaDriver->owner = THIS_MODULE;
Sergey Vlasov9b4e3b12005-09-03 16:26:49 +01001007 moxaDriver->name = "ttyMX";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 moxaDriver->major = ttymajor;
1009 moxaDriver->minor_start = 0;
1010 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1011 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1012 moxaDriver->init_termios = tty_std_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Alan Cox606d0992006-12-08 02:38:45 -08001014 moxaDriver->init_termios.c_ispeed = 9600;
1015 moxaDriver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
1017 tty_set_operations(moxaDriver, &moxa_ops);
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (tty_register_driver(moxaDriver)) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001020 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 put_tty_driver(moxaDriver);
1022 return -1;
1023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Jiri Slabyd353eca2008-04-30 00:53:37 -07001025 /* Find the boards defined from module args. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -07001027 {
1028 struct moxa_board_conf *brd = moxa_boards;
Jiri Slaby810ab092008-04-30 00:53:41 -07001029 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 for (i = 0; i < MAX_BOARDS; i++) {
Jiri Slabyd353eca2008-04-30 00:53:37 -07001031 if (!baseaddr[i])
1032 break;
1033 if (type[i] == MOXA_BOARD_C218_ISA ||
1034 type[i] == MOXA_BOARD_C320_ISA) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001035 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001036 isabrds + 1, moxa_brdname[type[i] - 1],
1037 baseaddr[i]);
1038 brd->boardType = type[i];
1039 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1040 numports[i];
1041 brd->busType = MOXA_BUS_TYPE_ISA;
Alan Cox24cb2332008-04-30 00:54:19 -07001042 brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
Jiri Slabyd353eca2008-04-30 00:53:37 -07001043 if (!brd->basemem) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001044 printk(KERN_ERR "MOXA: can't remap %lx\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001045 baseaddr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 continue;
1047 }
Jiri Slaby03718232008-04-30 00:53:39 -07001048 if (moxa_init_board(brd, NULL)) {
1049 iounmap(brd->basemem);
1050 brd->basemem = NULL;
1051 continue;
1052 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001053
Jiri Slabybb9f9102008-04-30 00:53:48 -07001054 printk(KERN_INFO "MOXA isa board found at 0x%.8lu and "
1055 "ready (%u ports, firmware loaded)\n",
1056 baseaddr[i], brd->numPorts);
1057
Jiri Slabyd353eca2008-04-30 00:53:37 -07001058 brd++;
1059 isabrds++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
1061 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001066 retval = pci_register_driver(&moxa_pci_driver);
1067 if (retval) {
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001068 printk(KERN_ERR "Can't register MOXA pci driver!\n");
Jiri Slabyd353eca2008-04-30 00:53:37 -07001069 if (isabrds)
Jiri Slabya784bf72007-02-10 01:45:36 -08001070 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001073
Jiri Slabya784bf72007-02-10 01:45:36 -08001074 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
1077static void __exit moxa_exit(void)
1078{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001079 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001081#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001082 pci_unregister_driver(&moxa_pci_driver);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001083#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001084
Jiri Slaby810ab092008-04-30 00:53:41 -07001085 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1086 if (moxa_boards[i].ready)
1087 moxa_board_deinit(&moxa_boards[i]);
Jiri Slaby2a541342008-04-30 00:53:45 -07001088
1089 del_timer_sync(&moxaTimer);
1090
1091 if (tty_unregister_driver(moxaDriver))
1092 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1093 "serial driver\n");
1094 put_tty_driver(moxaDriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095}
1096
1097module_init(moxa_init);
1098module_exit(moxa_exit);
1099
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001100static void moxa_close_port(struct moxa_port *ch)
1101{
1102 moxa_shut_down(ch);
1103 MoxaPortFlushData(ch, 2);
Alan Cox9de6a512008-07-16 21:56:02 +01001104 ch->port.flags &= ~ASYNC_NORMAL_ACTIVE;
1105 ch->port.tty->driver_data = NULL;
1106 ch->port.tty = NULL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001107}
1108
1109static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
1110 struct moxa_port *ch)
1111{
1112 DEFINE_WAIT(wait);
1113 int retval = 0;
1114 u8 dcd;
1115
1116 while (1) {
Alan Cox9de6a512008-07-16 21:56:02 +01001117 prepare_to_wait(&ch->port.open_wait, &wait, TASK_INTERRUPTIBLE);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001118 if (tty_hung_up_p(filp)) {
1119#ifdef SERIAL_DO_RESTART
1120 retval = -ERESTARTSYS;
1121#else
1122 retval = -EAGAIN;
1123#endif
1124 break;
1125 }
1126 spin_lock_bh(&moxa_lock);
1127 dcd = ch->DCDState;
1128 spin_unlock_bh(&moxa_lock);
1129 if (dcd)
1130 break;
1131
1132 if (signal_pending(current)) {
1133 retval = -ERESTARTSYS;
1134 break;
1135 }
1136 schedule();
1137 }
Alan Cox9de6a512008-07-16 21:56:02 +01001138 finish_wait(&ch->port.open_wait, &wait);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001139
1140 return retval;
1141}
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143static int moxa_open(struct tty_struct *tty, struct file *filp)
1144{
Jiri Slaby810ab092008-04-30 00:53:41 -07001145 struct moxa_board_conf *brd;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001146 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 int port;
1148 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Jiri Slaby11324ed2007-02-10 01:45:31 -08001150 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (port == MAX_PORTS) {
Jiri Slaby74d7d972008-04-30 00:53:43 -07001152 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001154 if (mutex_lock_interruptible(&moxa_openlock))
1155 return -ERESTARTSYS;
Jiri Slaby810ab092008-04-30 00:53:41 -07001156 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001157 if (!brd->ready) {
1158 mutex_unlock(&moxa_openlock);
Jiri Slaby810ab092008-04-30 00:53:41 -07001159 return -ENODEV;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Jiri Slaby810ab092008-04-30 00:53:41 -07001162 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
Alan Cox9de6a512008-07-16 21:56:02 +01001163 ch->port.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 tty->driver_data = ch;
Alan Cox9de6a512008-07-16 21:56:02 +01001165 ch->port.tty = tty;
1166 if (!(ch->port.flags & ASYNC_INITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 ch->statusflags = 0;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001168 moxa_set_tty_param(tty, tty->termios);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001169 MoxaPortLineCtrl(ch, 1, 1);
1170 MoxaPortEnable(ch);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001171 MoxaSetFifo(ch, ch->type == PORT_16550A);
Alan Cox9de6a512008-07-16 21:56:02 +01001172 ch->port.flags |= ASYNC_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001174 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001176 retval = 0;
1177 if (!(filp->f_flags & O_NONBLOCK) && !C_CLOCAL(tty))
1178 retval = moxa_block_till_ready(tty, filp, ch);
1179 mutex_lock(&moxa_openlock);
1180 if (retval) {
Alan Cox9de6a512008-07-16 21:56:02 +01001181 if (ch->port.count) /* 0 means already hung up... */
1182 if (--ch->port.count == 0)
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001183 moxa_close_port(ch);
1184 } else
Alan Cox9de6a512008-07-16 21:56:02 +01001185 ch->port.flags |= ASYNC_NORMAL_ACTIVE;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001186 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001188 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
1191static void moxa_close(struct tty_struct *tty, struct file *filp)
1192{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001193 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 int port;
1195
Jiri Slaby11324ed2007-02-10 01:45:31 -08001196 port = tty->index;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001197 if (port == MAX_PORTS || tty_hung_up_p(filp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001200 mutex_lock(&moxa_openlock);
1201 ch = tty->driver_data;
1202 if (ch == NULL)
1203 goto unlock;
Alan Cox9de6a512008-07-16 21:56:02 +01001204 if (tty->count == 1 && ch->port.count != 1) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001205 printk(KERN_WARNING "moxa_close: bad serial port count; "
Alan Cox9de6a512008-07-16 21:56:02 +01001206 "tty->count is 1, ch->port.count is %d\n", ch->port.count);
1207 ch->port.count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
Alan Cox9de6a512008-07-16 21:56:02 +01001209 if (--ch->port.count < 0) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001210 printk(KERN_WARNING "moxa_close: bad serial port count, "
1211 "device=%s\n", tty->name);
Alan Cox9de6a512008-07-16 21:56:02 +01001212 ch->port.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
Alan Cox9de6a512008-07-16 21:56:02 +01001214 if (ch->port.count)
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001215 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 ch->cflag = tty->termios->c_cflag;
Alan Cox9de6a512008-07-16 21:56:02 +01001218 if (ch->port.flags & ASYNC_INITIALIZED) {
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001219 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001223 moxa_close_port(ch);
1224unlock:
1225 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
1228static int moxa_write(struct tty_struct *tty,
1229 const unsigned char *buf, int count)
1230{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001231 struct moxa_port *ch = tty->driver_data;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001232 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (ch == NULL)
Jiri Slabyb4173f42008-04-30 00:53:40 -07001235 return 0;
Alan Cox33f0f882006-01-09 20:54:13 -08001236
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001237 spin_lock_bh(&moxa_lock);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001238 len = MoxaPortWriteData(ch, buf, count);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001239 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 ch->statusflags |= LOWWAIT;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001242 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
1245static int moxa_write_room(struct tty_struct *tty)
1246{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001247 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 if (tty->stopped)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001250 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001251 ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (ch == NULL)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001253 return 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001254 return MoxaPortTxFree(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
1257static void moxa_flush_buffer(struct tty_struct *tty)
1258{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001259 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 if (ch == NULL)
1262 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001263 MoxaPortFlushData(ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 tty_wakeup(tty);
1265}
1266
1267static int moxa_chars_in_buffer(struct tty_struct *tty)
1268{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001269 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 /*
1273 * Sigh...I have to check if driver_data is NULL here, because
1274 * if an open() fails, the TTY subsystem eventually calls
1275 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1276 * routine. And since the open() failed, we return 0 here. TDJ
1277 */
1278 if (ch == NULL)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001279 return 0;
Alan Cox978e5952008-04-30 00:53:59 -07001280 lock_kernel();
Jiri Slabyb4173f42008-04-30 00:53:40 -07001281 chars = MoxaPortTxQueue(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if (chars) {
1283 /*
1284 * Make it possible to wakeup anything waiting for output
1285 * in tty_ioctl.c, etc.
1286 */
1287 if (!(ch->statusflags & EMPTYWAIT))
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001288 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
Alan Cox978e5952008-04-30 00:53:59 -07001290 unlock_kernel();
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001291 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1295{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001296 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 int flag = 0, dtr, rts;
1298
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001299 mutex_lock(&moxa_openlock);
1300 ch = tty->driver_data;
1301 if (!ch) {
1302 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -07001303 return -EINVAL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Jiri Slabyb4173f42008-04-30 00:53:40 -07001306 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (dtr)
1308 flag |= TIOCM_DTR;
1309 if (rts)
1310 flag |= TIOCM_RTS;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001311 dtr = MoxaPortLineStatus(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (dtr & 1)
1313 flag |= TIOCM_CTS;
1314 if (dtr & 2)
1315 flag |= TIOCM_DSR;
1316 if (dtr & 4)
1317 flag |= TIOCM_CD;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001318 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return flag;
1320}
1321
1322static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1323 unsigned int set, unsigned int clear)
1324{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001325 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 int port;
1327 int dtr, rts;
1328
Jiri Slaby11324ed2007-02-10 01:45:31 -08001329 port = tty->index;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001330 mutex_lock(&moxa_openlock);
1331 ch = tty->driver_data;
1332 if (!ch) {
1333 mutex_unlock(&moxa_openlock);
Jiri Slaby74d7d972008-04-30 00:53:43 -07001334 return -EINVAL;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Jiri Slabyb4173f42008-04-30 00:53:40 -07001337 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (set & TIOCM_RTS)
1339 rts = 1;
1340 if (set & TIOCM_DTR)
1341 dtr = 1;
1342 if (clear & TIOCM_RTS)
1343 rts = 0;
1344 if (clear & TIOCM_DTR)
1345 dtr = 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001346 MoxaPortLineCtrl(ch, dtr, rts);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001347 mutex_unlock(&moxa_openlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return 0;
1349}
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351static void moxa_throttle(struct tty_struct *tty)
1352{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001353 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 ch->statusflags |= THROTTLE;
1356}
1357
1358static void moxa_unthrottle(struct tty_struct *tty)
1359{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001360 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 ch->statusflags &= ~THROTTLE;
1363}
1364
1365static void moxa_set_termios(struct tty_struct *tty,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001366 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001368 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 if (ch == NULL)
1371 return;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001372 moxa_set_tty_param(tty, old_termios);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001373 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
Alan Cox9de6a512008-07-16 21:56:02 +01001374 wake_up_interruptible(&ch->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
1376
1377static void moxa_stop(struct tty_struct *tty)
1378{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001379 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 if (ch == NULL)
1382 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001383 MoxaPortTxDisable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 ch->statusflags |= TXSTOPPED;
1385}
1386
1387
1388static void moxa_start(struct tty_struct *tty)
1389{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001390 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 if (ch == NULL)
1393 return;
1394
1395 if (!(ch->statusflags & TXSTOPPED))
1396 return;
1397
Jiri Slabyb4173f42008-04-30 00:53:40 -07001398 MoxaPortTxEnable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 ch->statusflags &= ~TXSTOPPED;
1400}
1401
1402static void moxa_hangup(struct tty_struct *tty)
1403{
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001404 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001406 mutex_lock(&moxa_openlock);
1407 ch = tty->driver_data;
1408 if (ch == NULL) {
1409 mutex_unlock(&moxa_openlock);
1410 return;
1411 }
Alan Cox9de6a512008-07-16 21:56:02 +01001412 ch->port.count = 0;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001413 moxa_close_port(ch);
1414 mutex_unlock(&moxa_openlock);
1415
Alan Cox9de6a512008-07-16 21:56:02 +01001416 wake_up_interruptible(&ch->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001419static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1420{
1421 dcd = !!dcd;
1422
Alan Cox9de6a512008-07-16 21:56:02 +01001423 if (dcd != p->DCDState && p->port.tty && C_CLOCAL(p->port.tty)) {
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001424 if (!dcd)
Alan Cox9de6a512008-07-16 21:56:02 +01001425 tty_hangup(p->port.tty);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001426 }
1427 p->DCDState = dcd;
1428}
1429
1430static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1431 u16 __iomem *ip)
1432{
Alan Cox9de6a512008-07-16 21:56:02 +01001433 struct tty_struct *tty = p->port.tty;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001434 void __iomem *ofsAddr;
Alan Cox9de6a512008-07-16 21:56:02 +01001435 unsigned int inited = p->port.flags & ASYNC_INITIALIZED;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001436 u16 intr;
1437
1438 if (tty) {
1439 if ((p->statusflags & EMPTYWAIT) &&
1440 MoxaPortTxQueue(p) == 0) {
1441 p->statusflags &= ~EMPTYWAIT;
1442 tty_wakeup(tty);
1443 }
1444 if ((p->statusflags & LOWWAIT) && !tty->stopped &&
1445 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1446 p->statusflags &= ~LOWWAIT;
1447 tty_wakeup(tty);
1448 }
1449
1450 if (inited && !(p->statusflags & THROTTLE) &&
1451 MoxaPortRxQueue(p) > 0) { /* RX */
1452 MoxaPortReadData(p);
1453 tty_schedule_flip(tty);
1454 }
1455 } else {
1456 p->statusflags &= ~EMPTYWAIT;
1457 MoxaPortFlushData(p, 0); /* flush RX */
1458 }
1459
1460 if (!handle) /* nothing else to do */
1461 return 0;
1462
1463 intr = readw(ip); /* port irq status */
1464 if (intr == 0)
1465 return 0;
1466
1467 writew(0, ip); /* ACK port */
1468 ofsAddr = p->tableAddr;
1469 if (intr & IntrTx) /* disable tx intr */
1470 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1471 ofsAddr + HostStat);
1472
1473 if (!inited)
1474 return 0;
1475
1476 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1477 tty_insert_flip_char(tty, 0, TTY_BREAK);
1478 tty_schedule_flip(tty);
1479 }
1480
1481 if (intr & IntrLine)
1482 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1483
1484 return 0;
1485}
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487static void moxa_poll(unsigned long ignored)
1488{
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001489 struct moxa_board_conf *brd;
1490 u16 __iomem *ip;
Jiri Slaby2a541342008-04-30 00:53:45 -07001491 unsigned int card, port, served = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001493 spin_lock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 for (card = 0; card < MAX_BOARDS; card++) {
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001495 brd = &moxa_boards[card];
1496 if (!brd->ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 continue;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001498
Jiri Slaby2a541342008-04-30 00:53:45 -07001499 served++;
1500
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001501 ip = NULL;
1502 if (readb(brd->intPend) == 0xff)
1503 ip = brd->intTable + readb(brd->intNdx);
1504
1505 for (port = 0; port < brd->numPorts; port++)
1506 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1507
1508 if (ip)
1509 writeb(0, brd->intPend); /* ACK */
1510
1511 if (moxaLowWaterChk) {
1512 struct moxa_port *p = brd->ports;
1513 for (port = 0; port < brd->numPorts; port++, p++)
1514 if (p->lowChkFlag) {
1515 p->lowChkFlag = 0;
1516 moxa_low_water_check(p->tableAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 }
1519 }
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001520 moxaLowWaterChk = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Jiri Slaby2a541342008-04-30 00:53:45 -07001522 if (served)
1523 mod_timer(&moxaTimer, jiffies + HZ / 50);
1524 spin_unlock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
1526
1527/******************************************************************************/
1528
Alan Coxdb1acaa2008-02-08 04:18:43 -08001529static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001531 register struct ktermios *ts = tty->termios;
1532 struct moxa_port *ch = tty->driver_data;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001533 int rts, cts, txflow, rxflow, xany, baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 rts = cts = txflow = rxflow = xany = 0;
1536 if (ts->c_cflag & CRTSCTS)
1537 rts = cts = 1;
1538 if (ts->c_iflag & IXON)
1539 txflow = 1;
1540 if (ts->c_iflag & IXOFF)
1541 rxflow = 1;
1542 if (ts->c_iflag & IXANY)
1543 xany = 1;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001544
1545 /* Clear the features we don't support */
1546 ts->c_cflag &= ~CMSPAR;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001547 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1548 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
Alan Coxdb1acaa2008-02-08 04:18:43 -08001549 if (baud == -1)
1550 baud = tty_termios_baud_rate(old_termios);
1551 /* Not put the baud rate into the termios data */
1552 tty_encode_baud_rate(tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
1554
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001555static void moxa_setup_empty_event(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001557 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001559 spin_lock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 ch->statusflags |= EMPTYWAIT;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001561 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001564static void moxa_shut_down(struct moxa_port *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
Alan Cox9de6a512008-07-16 21:56:02 +01001566 struct tty_struct *tp = ch->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Alan Cox9de6a512008-07-16 21:56:02 +01001568 if (!(ch->port.flags & ASYNC_INITIALIZED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return;
1570
Jiri Slabyb4173f42008-04-30 00:53:40 -07001571 MoxaPortDisable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 /*
1574 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1575 */
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001576 if (C_HUPCL(tp))
Jiri Slabyb4173f42008-04-30 00:53:40 -07001577 MoxaPortLineCtrl(ch, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001579 spin_lock_bh(&moxa_lock);
Alan Cox9de6a512008-07-16 21:56:02 +01001580 ch->port.flags &= ~ASYNC_INITIALIZED;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001581 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584/*****************************************************************************
1585 * Driver level functions: *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Jiri Slabyb4173f42008-04-30 00:53:40 -07001588static void MoxaPortFlushData(struct moxa_port *port, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
1590 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001591 if (mode < 0 || mode > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001593 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 moxafunc(ofsAddr, FC_FlushQueue, mode);
1595 if (mode != 1) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001596 port->lowChkFlag = 0;
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001597 moxa_low_water_check(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
1599}
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601/*
1602 * Moxa Port Number Description:
1603 *
1604 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1605 * the port number using in MOXA driver functions will be 0 to 31 for
1606 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1607 * to 127 for fourth. For example, if you setup three MOXA boards,
1608 * first board is C218, second board is C320-16 and third board is
1609 * C320-32. The port number of first board (C218 - 8 ports) is from
1610 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1611 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1612 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1613 * 127 will be invalid.
1614 *
1615 *
1616 * Moxa Functions Description:
1617 *
1618 * Function 1: Driver initialization routine, this routine must be
1619 * called when initialized driver.
1620 * Syntax:
1621 * void MoxaDriverInit();
1622 *
1623 *
1624 * Function 2: Moxa driver private IOCTL command processing.
1625 * Syntax:
1626 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1627 *
1628 * unsigned int cmd : IOCTL command
1629 * unsigned long arg : IOCTL argument
1630 * int port : port number (0 - 127)
1631 *
1632 * return: 0 (OK)
1633 * -EINVAL
1634 * -ENOIOCTLCMD
1635 *
1636 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 * Function 6: Enable this port to start Tx/Rx data.
1638 * Syntax:
1639 * void MoxaPortEnable(int port);
1640 * int port : port number (0 - 127)
1641 *
1642 *
1643 * Function 7: Disable this port
1644 * Syntax:
1645 * void MoxaPortDisable(int port);
1646 * int port : port number (0 - 127)
1647 *
1648 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 * Function 10: Setting baud rate of this port.
1650 * Syntax:
Jiri Slaby08d01c72008-04-30 00:53:47 -07001651 * speed_t MoxaPortSetBaud(int port, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 * int port : port number (0 - 127)
1653 * long baud : baud rate (50 - 115200)
1654 *
1655 * return: 0 : this port is invalid or baud < 50
1656 * 50 - 115200 : the real baud rate set to the port, if
1657 * the argument baud is large than maximun
1658 * available baud rate, the real setting
1659 * baud rate will be the maximun baud rate.
1660 *
1661 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 * Function 12: Configure the port.
1663 * Syntax:
Alan Cox606d0992006-12-08 02:38:45 -08001664 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 * int port : port number (0 - 127)
Alan Cox606d0992006-12-08 02:38:45 -08001666 * struct ktermios * termio : termio structure pointer
Alan Coxc7bce302006-09-30 23:27:24 -07001667 * speed_t baud : baud rate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 *
1669 * return: -1 : this port is invalid or termio == NULL
1670 * 0 : setting O.K.
1671 *
1672 *
1673 * Function 13: Get the DTR/RTS state of this port.
1674 * Syntax:
1675 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1676 * int port : port number (0 - 127)
1677 * int * dtrState : pointer to INT to receive the current DTR
1678 * state. (if NULL, this function will not
1679 * write to this address)
1680 * int * rtsState : pointer to INT to receive the current RTS
1681 * state. (if NULL, this function will not
1682 * write to this address)
1683 *
1684 * return: -1 : this port is invalid
1685 * 0 : O.K.
1686 *
1687 *
1688 * Function 14: Setting the DTR/RTS output state of this port.
1689 * Syntax:
1690 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1691 * int port : port number (0 - 127)
1692 * int dtrState : DTR output state (0: off, 1: on)
1693 * int rtsState : RTS output state (0: off, 1: on)
1694 *
1695 *
1696 * Function 15: Setting the flow control of this port.
1697 * Syntax:
1698 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1699 * int txFlow,int xany);
1700 * int port : port number (0 - 127)
1701 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1702 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1703 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1704 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1705 * int xany : S/W XANY flow control (0: no, 1: yes)
1706 *
1707 *
1708 * Function 16: Get ths line status of this port
1709 * Syntax:
1710 * int MoxaPortLineStatus(int port);
1711 * int port : port number (0 - 127)
1712 *
1713 * return: Bit 0 - CTS state (0: off, 1: on)
1714 * Bit 1 - DSR state (0: off, 1: on)
1715 * Bit 2 - DCD state (0: off, 1: on)
1716 *
1717 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 * Function 19: Flush the Rx/Tx buffer data of this port.
1719 * Syntax:
1720 * void MoxaPortFlushData(int port, int mode);
1721 * int port : port number (0 - 127)
1722 * int mode
1723 * 0 : flush the Rx buffer
1724 * 1 : flush the Tx buffer
1725 * 2 : flush the Rx and Tx buffer
1726 *
1727 *
1728 * Function 20: Write data.
1729 * Syntax:
1730 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1731 * int port : port number (0 - 127)
1732 * unsigned char * buffer : pointer to write data buffer.
1733 * int length : write data length
1734 *
1735 * return: 0 - length : real write data length
1736 *
1737 *
1738 * Function 21: Read data.
1739 * Syntax:
Alan Cox33f0f882006-01-09 20:54:13 -08001740 * int MoxaPortReadData(int port, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 * int port : port number (0 - 127)
Alan Cox33f0f882006-01-09 20:54:13 -08001742 * struct tty_struct *tty : tty for data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 *
1744 * return: 0 - length : real read data length
1745 *
1746 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 * Function 24: Get the Tx buffer current queued data bytes
1748 * Syntax:
1749 * int MoxaPortTxQueue(int port);
1750 * int port : port number (0 - 127)
1751 *
1752 * return: .. : Tx buffer current queued data bytes
1753 *
1754 *
1755 * Function 25: Get the Tx buffer current free space
1756 * Syntax:
1757 * int MoxaPortTxFree(int port);
1758 * int port : port number (0 - 127)
1759 *
1760 * return: .. : Tx buffer current free space
1761 *
1762 *
1763 * Function 26: Get the Rx buffer current queued data bytes
1764 * Syntax:
1765 * int MoxaPortRxQueue(int port);
1766 * int port : port number (0 - 127)
1767 *
1768 * return: .. : Rx buffer current queued data bytes
1769 *
1770 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 * Function 28: Disable port data transmission.
1772 * Syntax:
1773 * void MoxaPortTxDisable(int port);
1774 * int port : port number (0 - 127)
1775 *
1776 *
1777 * Function 29: Enable port data transmission.
1778 * Syntax:
1779 * void MoxaPortTxEnable(int port);
1780 * int port : port number (0 - 127)
1781 *
1782 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 * Function 31: Get the received BREAK signal count and reset it.
1784 * Syntax:
1785 * int MoxaPortResetBrkCnt(int port);
1786 * int port : port number (0 - 127)
1787 *
1788 * return: 0 - .. : BREAK signal count
1789 *
1790 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Jiri Slabyb4173f42008-04-30 00:53:40 -07001793static void MoxaPortEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
1795 void __iomem *ofsAddr;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001796 u16 lowwater = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Jiri Slabyb4173f42008-04-30 00:53:40 -07001798 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 writew(lowwater, ofsAddr + Low_water);
Jiri Slaby08d01c72008-04-30 00:53:47 -07001800 if (MOXA_IS_320(port->board))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001802 else
1803 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1804 ofsAddr + HostStat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1807 moxafunc(ofsAddr, FC_FlushQueue, 2);
1808
1809 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1810 MoxaPortLineStatus(port);
1811}
1812
Jiri Slabyb4173f42008-04-30 00:53:40 -07001813static void MoxaPortDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001815 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1818 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1819 writew(0, ofsAddr + HostStat);
1820 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1821}
1822
Jiri Slaby08d01c72008-04-30 00:53:47 -07001823static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Jiri Slaby08d01c72008-04-30 00:53:47 -07001825 void __iomem *ofsAddr = port->tableAddr;
1826 unsigned int clock, val;
1827 speed_t max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Jiri Slaby08d01c72008-04-30 00:53:47 -07001829 max = MOXA_IS_320(port->board) ? 460800 : 921600;
1830 if (baud < 50)
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001831 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 if (baud > max)
1833 baud = max;
Jiri Slaby08d01c72008-04-30 00:53:47 -07001834 clock = 921600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 val = clock / baud;
1836 moxafunc(ofsAddr, FC_SetBaud, val);
1837 baud = clock / val;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001838 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839}
1840
Jiri Slabyb4173f42008-04-30 00:53:40 -07001841static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1842 speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
1844 void __iomem *ofsAddr;
1845 tcflag_t cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 tcflag_t mode = 0;
1847
Jiri Slabyb4173f42008-04-30 00:53:40 -07001848 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 cflag = termio->c_cflag; /* termio->c_cflag */
1850
1851 mode = termio->c_cflag & CSIZE;
1852 if (mode == CS5)
1853 mode = MX_CS5;
1854 else if (mode == CS6)
1855 mode = MX_CS6;
1856 else if (mode == CS7)
1857 mode = MX_CS7;
1858 else if (mode == CS8)
1859 mode = MX_CS8;
1860
1861 if (termio->c_cflag & CSTOPB) {
1862 if (mode == MX_CS5)
1863 mode |= MX_STOP15;
1864 else
1865 mode |= MX_STOP2;
1866 } else
1867 mode |= MX_STOP1;
1868
1869 if (termio->c_cflag & PARENB) {
1870 if (termio->c_cflag & PARODD)
1871 mode |= MX_PARODD;
1872 else
1873 mode |= MX_PAREVEN;
1874 } else
1875 mode |= MX_PARNONE;
1876
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001877 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Jiri Slaby08d01c72008-04-30 00:53:47 -07001879 if (MOXA_IS_320(port->board) && baud >= 921600)
1880 return -1;
1881
Alan Coxdb1acaa2008-02-08 04:18:43 -08001882 baud = MoxaPortSetBaud(port, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1885 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1886 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1887 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001888 moxa_wait_finish(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890 }
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001891 return baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892}
1893
Jiri Slabyb4173f42008-04-30 00:53:40 -07001894static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1895 int *rtsState)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001897 if (dtrState)
1898 *dtrState = !!(port->lineCtrl & DTR_ON);
1899 if (rtsState)
1900 *rtsState = !!(port->lineCtrl & RTS_ON);
1901
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001902 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903}
1904
Jiri Slabyb4173f42008-04-30 00:53:40 -07001905static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07001907 u8 mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (dtr)
1910 mode |= DTR_ON;
1911 if (rts)
1912 mode |= RTS_ON;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001913 port->lineCtrl = mode;
1914 moxafunc(port->tableAddr, FC_LineControl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
1916
Jiri Slabyb4173f42008-04-30 00:53:40 -07001917static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1918 int txflow, int rxflow, int txany)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001920 int mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if (rts)
1923 mode |= RTS_FlowCtl;
1924 if (cts)
1925 mode |= CTS_FlowCtl;
1926 if (txflow)
1927 mode |= Tx_FlowCtl;
1928 if (rxflow)
1929 mode |= Rx_FlowCtl;
1930 if (txany)
1931 mode |= IXM_IXANY;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001932 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
1934
Jiri Slabyb4173f42008-04-30 00:53:40 -07001935static int MoxaPortLineStatus(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
1937 void __iomem *ofsAddr;
1938 int val;
1939
Jiri Slabyb4173f42008-04-30 00:53:40 -07001940 ofsAddr = port->tableAddr;
Jiri Slaby08d01c72008-04-30 00:53:47 -07001941 if (MOXA_IS_320(port->board)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 moxafunc(ofsAddr, FC_LineStatus, 0);
1943 val = readw(ofsAddr + FuncArg);
1944 } else {
1945 val = readw(ofsAddr + FlagStat) >> 4;
1946 }
1947 val &= 0x0B;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001948 if (val & 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 val |= 4;
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001950 spin_lock_bh(&moxa_lock);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001951 moxa_new_dcdstate(port, val & 8);
Jiri Slabya8f5cda2008-04-30 00:53:45 -07001952 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 val &= 7;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001954 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956
Jiri Slaby2108eba2008-04-30 00:53:44 -07001957static int MoxaPortWriteData(struct moxa_port *port,
1958 const unsigned char *buffer, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001961 unsigned int c, total;
1962 u16 head, tail, tx_mask, spage, epage;
1963 u16 pageno, pageofs, bufhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Jiri Slabyb4173f42008-04-30 00:53:40 -07001965 ofsAddr = port->tableAddr;
1966 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 tx_mask = readw(ofsAddr + TX_mask);
1968 spage = readw(ofsAddr + Page_txb);
1969 epage = readw(ofsAddr + EndPage_txb);
1970 tail = readw(ofsAddr + TXwptr);
1971 head = readw(ofsAddr + TXrptr);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001972 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 if (c > len)
1974 c = len;
Alan Cox9de6a512008-07-16 21:56:02 +01001975 moxaLog.txcnt[port->port.tty->index] += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 total = c;
1977 if (spage == epage) {
1978 bufhead = readw(ofsAddr + Ofs_txb);
1979 writew(spage, baseAddr + Control_reg);
1980 while (c > 0) {
1981 if (head > tail)
1982 len = head - tail - 1;
1983 else
1984 len = tx_mask + 1 - tail;
1985 len = (c > len) ? len : c;
1986 ofs = baseAddr + DynPage_addr + bufhead + tail;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001987 memcpy_toio(ofs, buffer, len);
1988 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 tail = (tail + len) & tx_mask;
1990 c -= len;
1991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 pageno = spage + (tail >> 13);
1994 pageofs = tail & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07001995 while (c > 0) {
1996 len = Page_size - pageofs;
1997 if (len > c)
1998 len = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 writeb(pageno, baseAddr + Control_reg);
2000 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002001 memcpy_toio(ofs, buffer, len);
2002 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (++pageno == epage)
2004 pageno = spage;
2005 pageofs = 0;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002006 c -= len;
2007 }
2008 tail = (tail + total) & tx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002010 writew(tail, ofsAddr + TXwptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 writeb(1, ofsAddr + CD180TXirq); /* start to send */
Jiri Slaby2108eba2008-04-30 00:53:44 -07002012 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013}
2014
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002015static int MoxaPortReadData(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
Alan Cox9de6a512008-07-16 21:56:02 +01002017 struct tty_struct *tty = port->port.tty;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002018 unsigned char *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002020 unsigned int count, len, total;
2021 u16 tail, rx_mask, spage, epage;
2022 u16 pageno, pageofs, bufhead, head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Jiri Slabyb4173f42008-04-30 00:53:40 -07002024 ofsAddr = port->tableAddr;
2025 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 head = readw(ofsAddr + RXrptr);
2027 tail = readw(ofsAddr + RXwptr);
2028 rx_mask = readw(ofsAddr + RX_mask);
2029 spage = readw(ofsAddr + Page_rxb);
2030 epage = readw(ofsAddr + EndPage_rxb);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002031 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if (count == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08002033 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Alan Cox33f0f882006-01-09 20:54:13 -08002035 total = count;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002036 moxaLog.rxcnt[tty->index] += total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 if (spage == epage) {
2038 bufhead = readw(ofsAddr + Ofs_rxb);
2039 writew(spage, baseAddr + Control_reg);
2040 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 ofs = baseAddr + DynPage_addr + bufhead + head;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002042 len = (tail >= head) ? (tail - head) :
2043 (rx_mask + 1 - head);
2044 len = tty_prepare_flip_string(tty, &dst,
2045 min(len, count));
2046 memcpy_fromio(dst, ofs, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 head = (head + len) & rx_mask;
2048 count -= len;
2049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 pageno = spage + (head >> 13);
2052 pageofs = head & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002053 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 writew(pageno, baseAddr + Control_reg);
2055 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002056 len = tty_prepare_flip_string(tty, &dst,
2057 min(Page_size - pageofs, count));
2058 memcpy_fromio(dst, ofs, len);
2059
2060 count -= len;
2061 pageofs = (pageofs + len) & Page_mask;
2062 if (pageofs == 0 && ++pageno == epage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 pageno = spage;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002064 }
2065 head = (head + total) & rx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002067 writew(head, ofsAddr + RXrptr);
2068 if (readb(ofsAddr + FlagStat) & Xoff_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 moxaLowWaterChk = 1;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002070 port->lowChkFlag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002072 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073}
2074
2075
Jiri Slabyb4173f42008-04-30 00:53:40 -07002076static int MoxaPortTxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002078 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002079 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 rptr = readw(ofsAddr + TXrptr);
2082 wptr = readw(ofsAddr + TXwptr);
2083 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002084 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085}
2086
Jiri Slabyb4173f42008-04-30 00:53:40 -07002087static int MoxaPortTxFree(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002089 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002090 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 rptr = readw(ofsAddr + TXrptr);
2093 wptr = readw(ofsAddr + TXwptr);
2094 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002095 return mask - ((wptr - rptr) & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096}
2097
Jiri Slabyb4173f42008-04-30 00:53:40 -07002098static int MoxaPortRxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002100 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002101 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 rptr = readw(ofsAddr + RXrptr);
2104 wptr = readw(ofsAddr + RXwptr);
2105 mask = readw(ofsAddr + RX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002106 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107}
2108
Jiri Slabyb4173f42008-04-30 00:53:40 -07002109static void MoxaPortTxDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002111 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
Jiri Slabyb4173f42008-04-30 00:53:40 -07002114static void MoxaPortTxEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002116 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117}
2118
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002119static int moxa_get_serial_info(struct moxa_port *info,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002120 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002122 struct serial_struct tmp = {
2123 .type = info->type,
Alan Cox9de6a512008-07-16 21:56:02 +01002124 .line = info->port.tty->index,
2125 .flags = info->port.flags,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002126 .baud_base = 921600,
2127 .close_delay = info->close_delay
2128 };
2129 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130}
2131
2132
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002133static int moxa_set_serial_info(struct moxa_port *info,
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002134 struct serial_struct __user *new_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
2136 struct serial_struct new_serial;
2137
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002138 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 return -EFAULT;
2140
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002141 if (new_serial.irq != 0 || new_serial.port != 0 ||
2142 new_serial.custom_divisor != 0 ||
2143 new_serial.baud_base != 921600)
2144 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 if (!capable(CAP_SYS_ADMIN)) {
2147 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
Alan Cox9de6a512008-07-16 21:56:02 +01002148 (info->port.flags & ~ASYNC_USR_MASK)))
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002149 return -EPERM;
2150 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 info->close_delay = new_serial.close_delay * HZ / 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
2153 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
Alan Cox9de6a512008-07-16 21:56:02 +01002154 new_serial.flags |= (info->port.flags & ASYNC_FLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002156 MoxaSetFifo(info, new_serial.type == PORT_16550A);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158 info->type = new_serial.type;
Jiri Slabyeaa95a82008-04-30 00:53:46 -07002159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160}
2161
2162
2163
2164/*****************************************************************************
2165 * Static local functions: *
2166 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Jiri Slabyb4173f42008-04-30 00:53:40 -07002168static void MoxaSetFifo(struct moxa_port *port, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002170 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 if (!enable) {
2173 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2174 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2175 } else {
2176 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2177 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2178 }
2179}