blob: f737fbb8598c24d9e34c0572f3107c30b2f2a0a8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*****************************************************************************/
2/*
3 * moxa.c -- MOXA Intellio family multiport serial driver.
4 *
5 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com.tw).
6 *
7 * This code is loosely based on the Linux serial driver, written by
8 * Linus Torvalds, Theodore T'so and others.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
16/*
17 * MOXA Intellio Series Driver
18 * for : LINUX
19 * date : 1999/1/7
20 * version : 5.1
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
24#include <linux/types.h>
25#include <linux/mm.h>
26#include <linux/ioport.h>
27#include <linux/errno.h>
Jiri Slaby03718232008-04-30 00:53:39 -070028#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/signal.h>
30#include <linux/sched.h>
31#include <linux/timer.h>
32#include <linux/interrupt.h>
33#include <linux/tty.h>
34#include <linux/tty_flip.h>
35#include <linux/major.h>
36#include <linux/string.h>
37#include <linux/fcntl.h>
38#include <linux/ptrace.h>
39#include <linux/serial.h>
40#include <linux/tty_driver.h>
41#include <linux/delay.h>
42#include <linux/pci.h>
43#include <linux/init.h>
44#include <linux/bitops.h>
Jiri Slaby95e07912007-10-18 03:06:25 -070045#include <linux/completion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
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 Slaby11324ed2007-02-10 01:45:31 -080053#define MOXA_VERSION "5.1k"
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
58#define MOXACUMAJOR 173
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Jiri Slaby11324ed2007-02-10 01:45:31 -080060#define MAX_BOARDS 4 /* Don't change this value */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
Jiri Slaby11324ed2007-02-10 01:45:31 -080062#define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/*
65 * Define the Moxa PCI vendor and device IDs.
66 */
Jiri Slaby11324ed2007-02-10 01:45:31 -080067#define MOXA_BUS_TYPE_ISA 0
68#define MOXA_BUS_TYPE_PCI 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070enum {
71 MOXA_BOARD_C218_PCI = 1,
72 MOXA_BOARD_C218_ISA,
73 MOXA_BOARD_C320_PCI,
74 MOXA_BOARD_C320_ISA,
75 MOXA_BOARD_CP204J,
76};
77
78static char *moxa_brdname[] =
79{
80 "C218 Turbo PCI series",
81 "C218 Turbo ISA series",
82 "C320 Turbo PCI series",
83 "C320 Turbo ISA series",
84 "CP-204J series",
85};
86
87#ifdef CONFIG_PCI
88static struct pci_device_id moxa_pcibrds[] = {
Jiri Slaby5ebb4072007-02-10 01:45:30 -080089 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
90 .driver_data = MOXA_BOARD_C218_PCI },
91 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
92 .driver_data = MOXA_BOARD_C320_PCI },
93 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
94 .driver_data = MOXA_BOARD_CP204J },
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 { 0 }
96};
97MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
98#endif /* CONFIG_PCI */
99
Jiri Slaby03718232008-04-30 00:53:39 -0700100struct moxa_port;
101
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800102static struct moxa_board_conf {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 int boardType;
104 int numPorts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 int busType;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800106
Jiri Slaby810ab092008-04-30 00:53:41 -0700107 unsigned int ready;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800108
Jiri Slaby03718232008-04-30 00:53:39 -0700109 struct moxa_port *ports;
110
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800111 void __iomem *basemem;
112 void __iomem *intNdx;
113 void __iomem *intPend;
114 void __iomem *intTable;
115} moxa_boards[MAX_BOARDS];
116
117struct mxser_mstatus {
118 tcflag_t cflag;
119 int cts;
120 int dsr;
121 int ri;
122 int dcd;
Jiri Slaby9dff89c2007-02-10 01:45:30 -0800123};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800125struct moxaq_str {
126 int inq;
127 int outq;
128};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800130struct moxa_port {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700131 struct moxa_board_conf *board;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 int close_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 int count;
135 int blocked_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 int asyncflags;
137 unsigned long statusflags;
138 struct tty_struct *tty;
139 int cflag;
140 wait_queue_head_t open_wait;
Jiri Slaby95e07912007-10-18 03:06:25 -0700141 struct completion close_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800143 struct timer_list emptyTimer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800145 char lineCtrl;
146 void __iomem *tableAddr;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800147 char DCDState;
148 char lowChkFlag;
149
150 ushort breakCnt;
151};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Jiri Slaby74d7d972008-04-30 00:53:43 -0700153struct mon_str {
154 int tick;
155 int rxcnt[MAX_PORTS];
156 int txcnt[MAX_PORTS];
157};
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/* statusflags */
160#define TXSTOPPED 0x1
161#define LOWWAIT 0x2
162#define EMPTYWAIT 0x4
163#define THROTTLE 0x8
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#define SERIAL_DO_RESTART
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#define WAKEUP_CHARS 256
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169static int ttymajor = MOXAMAJOR;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700170static struct mon_str moxaLog;
171static unsigned int moxaFuncTout = HZ / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/* Variables for insmod */
173#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700174static unsigned long baseaddr[MAX_BOARDS];
175static unsigned int type[MAX_BOARDS];
176static unsigned int numports[MAX_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177#endif
178
179MODULE_AUTHOR("William Chen");
180MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
181MODULE_LICENSE("GPL");
182#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700183module_param_array(type, uint, NULL, 0);
184MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
185module_param_array(baseaddr, ulong, NULL, 0);
186MODULE_PARM_DESC(baseaddr, "base address");
187module_param_array(numports, uint, NULL, 0);
188MODULE_PARM_DESC(numports, "numports (ignored for C218)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#endif
190module_param(ttymajor, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/*
193 * static functions:
194 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195static int moxa_open(struct tty_struct *, struct file *);
196static void moxa_close(struct tty_struct *, struct file *);
197static int moxa_write(struct tty_struct *, const unsigned char *, int);
198static int moxa_write_room(struct tty_struct *);
199static void moxa_flush_buffer(struct tty_struct *);
200static int moxa_chars_in_buffer(struct tty_struct *);
201static void moxa_flush_chars(struct tty_struct *);
202static void moxa_put_char(struct tty_struct *, unsigned char);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203static void moxa_throttle(struct tty_struct *);
204static void moxa_unthrottle(struct tty_struct *);
Alan Cox606d0992006-12-08 02:38:45 -0800205static void moxa_set_termios(struct tty_struct *, struct ktermios *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206static void moxa_stop(struct tty_struct *);
207static void moxa_start(struct tty_struct *);
208static void moxa_hangup(struct tty_struct *);
209static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
210static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
211 unsigned int set, unsigned int clear);
212static void moxa_poll(unsigned long);
Alan Coxdb1acaa2008-02-08 04:18:43 -0800213static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
Jiri Slaby6f56b6582007-10-18 03:06:24 -0700214static int moxa_block_till_ready(struct tty_struct *, struct file *,
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800215 struct moxa_port *);
Jiri Slaby6f56b6582007-10-18 03:06:24 -0700216static void moxa_setup_empty_event(struct tty_struct *);
217static void moxa_check_xmit_empty(unsigned long);
218static void moxa_shut_down(struct moxa_port *);
219static void moxa_receive_data(struct moxa_port *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220/*
221 * moxa board interface functions:
222 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223static int MoxaDriverPoll(void);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700224static void MoxaPortEnable(struct moxa_port *);
225static void MoxaPortDisable(struct moxa_port *);
226static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
227static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
228static void MoxaPortLineCtrl(struct moxa_port *, int, int);
229static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
230static int MoxaPortLineStatus(struct moxa_port *);
231static int MoxaPortDCDChange(struct moxa_port *);
232static int MoxaPortDCDON(struct moxa_port *);
233static void MoxaPortFlushData(struct moxa_port *, int);
234static int MoxaPortWriteData(struct moxa_port *, unsigned char *, int);
235static int MoxaPortReadData(struct moxa_port *, struct tty_struct *tty);
236static int MoxaPortTxQueue(struct moxa_port *);
237static int MoxaPortRxQueue(struct moxa_port *);
238static int MoxaPortTxFree(struct moxa_port *);
239static void MoxaPortTxDisable(struct moxa_port *);
240static void MoxaPortTxEnable(struct moxa_port *);
241static int MoxaPortResetBrkCnt(struct moxa_port *);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800242static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
243static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700244static void MoxaSetFifo(struct moxa_port *port, int enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Jiri Slaby74d7d972008-04-30 00:53:43 -0700246/*
247 * I/O functions
248 */
249
250static void moxa_wait_finish(void __iomem *ofsAddr)
251{
252 unsigned long end = jiffies + moxaFuncTout;
253
254 while (readw(ofsAddr + FuncCode) != 0)
255 if (time_after(jiffies, end))
256 return;
257 if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
258 printk(KERN_WARNING "moxa function expired\n");
259}
260
261static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
262{
263 writew(arg, ofsAddr + FuncArg);
264 writew(cmd, ofsAddr + FuncCode);
265 moxa_wait_finish(ofsAddr);
266}
267
268/*
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;
277 int status;
278
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;
289 return copy_to_user(argp, &moxaLog, sizeof(moxaLog)) ?
290 -EFAULT : 0;
291 case MOXA_FLUSH_QUEUE:
292 MoxaPortFlushData(ch, arg);
293 return 0;
294 case MOXA_GET_IOQUEUE: {
295 struct moxaq_str __user *argm = argp;
296 struct moxaq_str tmp;
297 struct moxa_port *p;
298 unsigned int i, j;
299
300 for (i = 0; i < MAX_BOARDS; i++) {
301 p = moxa_boards[i].ports;
302 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
303 memset(&tmp, 0, sizeof(tmp));
304 if (moxa_boards[i].ready) {
305 tmp.inq = MoxaPortRxQueue(p);
306 tmp.outq = MoxaPortTxQueue(p);
307 }
308 if (copy_to_user(argm, &tmp, sizeof(tmp)))
309 return -EFAULT;
310 }
311 }
312 return 0;
313 } case MOXA_GET_OQUEUE:
314 status = MoxaPortTxQueue(ch);
315 return put_user(status, (unsigned long __user *)argp);
316 case MOXA_GET_IQUEUE:
317 status = MoxaPortRxQueue(ch);
318 return put_user(status, (unsigned long __user *)argp);
319 case MOXA_GETMSTATUS: {
320 struct mxser_mstatus __user *argm = argp;
321 struct mxser_mstatus tmp;
322 struct moxa_port *p;
323 unsigned int i, j;
324
325 for (i = 0; i < MAX_BOARDS; i++) {
326 p = moxa_boards[i].ports;
327 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
328 memset(&tmp, 0, sizeof(tmp));
329 if (!moxa_boards[i].ready)
330 goto copy;
331
332 status = MoxaPortLineStatus(p);
333 if (status & 1)
334 tmp.cts = 1;
335 if (status & 2)
336 tmp.dsr = 1;
337 if (status & 4)
338 tmp.dcd = 1;
339
340 if (!p->tty || !p->tty->termios)
341 tmp.cflag = p->cflag;
342 else
343 tmp.cflag = p->tty->termios->c_cflag;
344copy:
345 if (copy_to_user(argm, &tmp, sizeof(tmp)))
346 return -EFAULT;
347 }
348 }
349 return 0;
350 }
351 case TIOCGSERIAL:
352 return moxa_get_serial_info(ch, argp);
353 case TIOCSSERIAL:
354 return moxa_set_serial_info(ch, argp);
355 }
356 return -ENOIOCTLCMD;
357}
358
359static void moxa_break_ctl(struct tty_struct *tty, int state)
360{
361 struct moxa_port *port = tty->driver_data;
362
363 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
364 Magic_code);
365}
366
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700367static const struct tty_operations moxa_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 .open = moxa_open,
369 .close = moxa_close,
370 .write = moxa_write,
371 .write_room = moxa_write_room,
372 .flush_buffer = moxa_flush_buffer,
373 .chars_in_buffer = moxa_chars_in_buffer,
374 .flush_chars = moxa_flush_chars,
375 .put_char = moxa_put_char,
376 .ioctl = moxa_ioctl,
377 .throttle = moxa_throttle,
378 .unthrottle = moxa_unthrottle,
379 .set_termios = moxa_set_termios,
380 .stop = moxa_stop,
381 .start = moxa_start,
382 .hangup = moxa_hangup,
Jiri Slaby74d7d972008-04-30 00:53:43 -0700383 .break_ctl = moxa_break_ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 .tiocmget = moxa_tiocmget,
385 .tiocmset = moxa_tiocmset,
386};
387
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800388static struct tty_driver *moxaDriver;
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800389static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
Ingo Molnar34af9462006-06-27 02:53:55 -0700390static DEFINE_SPINLOCK(moxa_lock);
Alan Cox33f0f882006-01-09 20:54:13 -0800391
Jiri Slaby74d7d972008-04-30 00:53:43 -0700392/*
393 * HW init
394 */
395
Jiri Slaby03718232008-04-30 00:53:39 -0700396static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
397{
398 switch (brd->boardType) {
399 case MOXA_BOARD_C218_ISA:
400 case MOXA_BOARD_C218_PCI:
401 if (model != 1)
402 goto err;
403 break;
404 case MOXA_BOARD_CP204J:
405 if (model != 3)
406 goto err;
407 break;
408 default:
409 if (model != 2)
410 goto err;
411 break;
412 }
413 return 0;
414err:
415 return -EINVAL;
416}
417
418static int moxa_check_fw(const void *ptr)
419{
420 const __le16 *lptr = ptr;
421
422 if (*lptr != cpu_to_le16(0x7980))
423 return -EINVAL;
424
425 return 0;
426}
427
428static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
429 size_t len)
430{
431 void __iomem *baseAddr = brd->basemem;
432 u16 tmp;
433
434 writeb(HW_reset, baseAddr + Control_reg); /* reset */
435 msleep(10);
436 memset_io(baseAddr, 0, 4096);
437 memcpy_toio(baseAddr, buf, len); /* download BIOS */
438 writeb(0, baseAddr + Control_reg); /* restart */
439
440 msleep(2000);
441
442 switch (brd->boardType) {
443 case MOXA_BOARD_C218_ISA:
444 case MOXA_BOARD_C218_PCI:
445 tmp = readw(baseAddr + C218_key);
446 if (tmp != C218_KeyCode)
447 goto err;
448 break;
449 case MOXA_BOARD_CP204J:
450 tmp = readw(baseAddr + C218_key);
451 if (tmp != CP204J_KeyCode)
452 goto err;
453 break;
454 default:
455 tmp = readw(baseAddr + C320_key);
456 if (tmp != C320_KeyCode)
457 goto err;
458 tmp = readw(baseAddr + C320_status);
459 if (tmp != STS_init) {
460 printk(KERN_ERR "moxa: bios upload failed -- CPU/Basic "
461 "module not found\n");
462 return -EIO;
463 }
464 break;
465 }
466
467 return 0;
468err:
469 printk(KERN_ERR "moxa: bios upload failed -- board not found\n");
470 return -EIO;
471}
472
473static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
474 size_t len)
475{
476 void __iomem *baseAddr = brd->basemem;
477
478 if (len < 7168) {
479 printk(KERN_ERR "moxa: invalid 320 bios -- too short\n");
480 return -EINVAL;
481 }
482
483 writew(len - 7168 - 2, baseAddr + C320bapi_len);
484 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
485 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
486 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
487 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
488
489 return 0;
490}
491
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700492static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
Jiri Slaby03718232008-04-30 00:53:39 -0700493 size_t len)
494{
495 void __iomem *baseAddr = brd->basemem;
496 const u16 *uptr = ptr;
497 size_t wlen, len2, j;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700498 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
499 unsigned int i, retry, c320;
Jiri Slaby03718232008-04-30 00:53:39 -0700500 u16 usum, keycode;
501
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700502 c320 = brd->boardType == MOXA_BOARD_C320_PCI ||
503 brd->boardType == MOXA_BOARD_C320_ISA;
504 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
505 C218_KeyCode;
506
507 switch (brd->boardType) {
508 case MOXA_BOARD_CP204J:
509 case MOXA_BOARD_C218_ISA:
510 case MOXA_BOARD_C218_PCI:
511 key = C218_key;
512 loadbuf = C218_LoadBuf;
513 loadlen = C218DLoad_len;
514 checksum = C218check_sum;
515 checksum_ok = C218chksum_ok;
516 break;
517 default:
518 key = C320_key;
519 keycode = C320_KeyCode;
520 loadbuf = C320_LoadBuf;
521 loadlen = C320DLoad_len;
522 checksum = C320check_sum;
523 checksum_ok = C320chksum_ok;
524 break;
525 }
526
Jiri Slaby03718232008-04-30 00:53:39 -0700527 usum = 0;
528 wlen = len >> 1;
529 for (i = 0; i < wlen; i++)
530 usum += le16_to_cpu(uptr[i]);
531 retry = 0;
532 do {
533 wlen = len >> 1;
534 j = 0;
535 while (wlen) {
536 len2 = (wlen > 2048) ? 2048 : wlen;
537 wlen -= len2;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700538 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
Jiri Slaby03718232008-04-30 00:53:39 -0700539 j += len2 << 1;
540
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700541 writew(len2, baseAddr + loadlen);
542 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700543 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700544 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700545 break;
546 msleep(10);
547 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700548 if (readw(baseAddr + key) != keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700549 return -EIO;
550 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700551 writew(0, baseAddr + loadlen);
552 writew(usum, baseAddr + checksum);
553 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700554 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700555 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700556 break;
557 msleep(10);
558 }
559 retry++;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700560 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
561 if (readb(baseAddr + checksum_ok) != 1)
Jiri Slaby03718232008-04-30 00:53:39 -0700562 return -EIO;
563
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700564 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700565 for (i = 0; i < 600; i++) {
566 if (readw(baseAddr + Magic_no) == Magic_code)
567 break;
568 msleep(10);
569 }
570 if (readw(baseAddr + Magic_no) != Magic_code)
571 return -EIO;
572
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700573 if (c320) {
574 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
575 writew(0x3800, baseAddr + TMS320_PORT1);
576 writew(0x3900, baseAddr + TMS320_PORT2);
577 writew(28499, baseAddr + TMS320_CLOCK);
578 } else {
579 writew(0x3200, baseAddr + TMS320_PORT1);
580 writew(0x3400, baseAddr + TMS320_PORT2);
581 writew(19999, baseAddr + TMS320_CLOCK);
582 }
Jiri Slaby03718232008-04-30 00:53:39 -0700583 }
584 writew(1, baseAddr + Disable_IRQ);
585 writew(0, baseAddr + Magic_no);
586 for (i = 0; i < 500; i++) {
587 if (readw(baseAddr + Magic_no) == Magic_code)
588 break;
589 msleep(10);
590 }
591 if (readw(baseAddr + Magic_no) != Magic_code)
592 return -EIO;
593
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700594 if (c320) {
595 j = readw(baseAddr + Module_cnt);
596 if (j <= 0)
597 return -EIO;
598 brd->numPorts = j * 8;
599 writew(j, baseAddr + Module_no);
600 writew(0, baseAddr + Magic_no);
601 for (i = 0; i < 600; 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;
Jiri Slaby03718232008-04-30 00:53:39 -0700608 }
Jiri Slaby03718232008-04-30 00:53:39 -0700609 brd->intNdx = baseAddr + IRQindex;
610 brd->intPend = baseAddr + IRQpending;
611 brd->intTable = baseAddr + IRQtable;
612
613 return 0;
614}
615
616static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
617 size_t len)
618{
619 void __iomem *ofsAddr, *baseAddr = brd->basemem;
620 struct moxa_port *port;
621 int retval, i;
622
623 if (len % 2) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700624 printk(KERN_ERR "moxa: bios length is not even\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700625 return -EINVAL;
626 }
627
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700628 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
629 if (retval)
630 return retval;
631
Jiri Slaby03718232008-04-30 00:53:39 -0700632 switch (brd->boardType) {
633 case MOXA_BOARD_C218_ISA:
634 case MOXA_BOARD_C218_PCI:
635 case MOXA_BOARD_CP204J:
Jiri Slaby03718232008-04-30 00:53:39 -0700636 port = brd->ports;
637 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700638 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700639 port->DCDState = 0;
640 port->tableAddr = baseAddr + Extern_table +
641 Extern_size * i;
642 ofsAddr = port->tableAddr;
643 writew(C218rx_mask, ofsAddr + RX_mask);
644 writew(C218tx_mask, ofsAddr + TX_mask);
645 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
646 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
647
648 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
649 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
650
651 }
652 break;
653 default:
Jiri Slaby03718232008-04-30 00:53:39 -0700654 port = brd->ports;
655 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700656 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700657 port->DCDState = 0;
658 port->tableAddr = baseAddr + Extern_table +
659 Extern_size * i;
660 ofsAddr = port->tableAddr;
661 switch (brd->numPorts) {
662 case 8:
663 writew(C320p8rx_mask, ofsAddr + RX_mask);
664 writew(C320p8tx_mask, ofsAddr + TX_mask);
665 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
666 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
667 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
668 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
669
670 break;
671 case 16:
672 writew(C320p16rx_mask, ofsAddr + RX_mask);
673 writew(C320p16tx_mask, ofsAddr + TX_mask);
674 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
675 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
676 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
677 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
678 break;
679
680 case 24:
681 writew(C320p24rx_mask, ofsAddr + RX_mask);
682 writew(C320p24tx_mask, ofsAddr + TX_mask);
683 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
684 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
685 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
686 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
687 break;
688 case 32:
689 writew(C320p32rx_mask, ofsAddr + RX_mask);
690 writew(C320p32tx_mask, ofsAddr + TX_mask);
691 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
692 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
693 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
694 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
695 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
696 break;
697 }
698 }
699 break;
700 }
Jiri Slaby03718232008-04-30 00:53:39 -0700701 return 0;
702}
703
704static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
705{
706 void *ptr = fw->data;
707 char rsn[64];
708 u16 lens[5];
709 size_t len;
710 unsigned int a, lenp, lencnt;
711 int ret = -EINVAL;
712 struct {
713 __le32 magic; /* 0x34303430 */
714 u8 reserved1[2];
715 u8 type; /* UNIX = 3 */
716 u8 model; /* C218T=1, C320T=2, CP204=3 */
717 u8 reserved2[8];
718 __le16 len[5];
719 } *hdr = ptr;
720
721 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
722
723 if (fw->size < MOXA_FW_HDRLEN) {
724 strcpy(rsn, "too short (even header won't fit)");
725 goto err;
726 }
727 if (hdr->magic != cpu_to_le32(0x30343034)) {
728 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
729 goto err;
730 }
731 if (hdr->type != 3) {
732 sprintf(rsn, "not for linux, type is %u", hdr->type);
733 goto err;
734 }
735 if (moxa_check_fw_model(brd, hdr->model)) {
736 sprintf(rsn, "not for this card, model is %u", hdr->model);
737 goto err;
738 }
739
740 len = MOXA_FW_HDRLEN;
741 lencnt = hdr->model == 2 ? 5 : 3;
742 for (a = 0; a < ARRAY_SIZE(lens); a++) {
743 lens[a] = le16_to_cpu(hdr->len[a]);
744 if (lens[a] && len + lens[a] <= fw->size &&
745 moxa_check_fw(&fw->data[len]))
746 printk(KERN_WARNING "moxa firmware: unexpected input "
747 "at offset %u, but going on\n", (u32)len);
748 if (!lens[a] && a < lencnt) {
749 sprintf(rsn, "too few entries in fw file");
750 goto err;
751 }
752 len += lens[a];
753 }
754
755 if (len != fw->size) {
756 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
757 (u32)len);
758 goto err;
759 }
760
761 ptr += MOXA_FW_HDRLEN;
762 lenp = 0; /* bios */
763
764 strcpy(rsn, "read above");
765
766 ret = moxa_load_bios(brd, ptr, lens[lenp]);
767 if (ret)
768 goto err;
769
770 /* we skip the tty section (lens[1]), since we don't need it */
771 ptr += lens[lenp] + lens[lenp + 1];
772 lenp += 2; /* comm */
773
774 if (hdr->model == 2) {
775 ret = moxa_load_320b(brd, ptr, lens[lenp]);
776 if (ret)
777 goto err;
778 /* skip another tty */
779 ptr += lens[lenp] + lens[lenp + 1];
780 lenp += 2;
781 }
782
783 ret = moxa_load_code(brd, ptr, lens[lenp]);
784 if (ret)
785 goto err;
786
787 return 0;
788err:
789 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
790 return ret;
791}
792
793static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
794{
795 const struct firmware *fw;
796 const char *file;
Jiri Slaby810ab092008-04-30 00:53:41 -0700797 struct moxa_port *p;
798 unsigned int i;
Jiri Slaby03718232008-04-30 00:53:39 -0700799 int ret;
800
Jiri Slaby810ab092008-04-30 00:53:41 -0700801 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
802 GFP_KERNEL);
803 if (brd->ports == NULL) {
804 printk(KERN_ERR "cannot allocate memory for ports\n");
805 ret = -ENOMEM;
806 goto err;
807 }
808
809 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
810 p->type = PORT_16550A;
811 p->close_delay = 5 * HZ / 10;
812 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
813 init_waitqueue_head(&p->open_wait);
814 init_completion(&p->close_wait);
815
816 setup_timer(&p->emptyTimer, moxa_check_xmit_empty,
817 (unsigned long)p);
818 }
819
Jiri Slaby03718232008-04-30 00:53:39 -0700820 switch (brd->boardType) {
821 case MOXA_BOARD_C218_ISA:
822 case MOXA_BOARD_C218_PCI:
823 file = "c218tunx.cod";
824 break;
825 case MOXA_BOARD_CP204J:
826 file = "cp204unx.cod";
827 break;
828 default:
829 file = "c320tunx.cod";
830 break;
831 }
832
833 ret = request_firmware(&fw, file, dev);
834 if (ret) {
835 printk(KERN_ERR "request_firmware failed\n");
Jiri Slaby810ab092008-04-30 00:53:41 -0700836 goto err_free;
Jiri Slaby03718232008-04-30 00:53:39 -0700837 }
838
839 ret = moxa_load_fw(brd, fw);
840
841 release_firmware(fw);
Jiri Slaby810ab092008-04-30 00:53:41 -0700842
843 if (ret)
844 goto err_free;
845
846 brd->ready = 1;
847
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700848 if (!timer_pending(&moxaTimer))
849 mod_timer(&moxaTimer, jiffies + HZ / 50);
850
Jiri Slaby810ab092008-04-30 00:53:41 -0700851 return 0;
852err_free:
853 kfree(brd->ports);
854err:
Jiri Slaby03718232008-04-30 00:53:39 -0700855 return ret;
856}
857
Jiri Slaby810ab092008-04-30 00:53:41 -0700858static void moxa_board_deinit(struct moxa_board_conf *brd)
859{
860 unsigned int i;
861
862 brd->ready = 0;
863 for (i = 0; i < MAX_PORTS_PER_BOARD; i++)
864 del_timer_sync(&brd->ports[i].emptyTimer);
865
866 iounmap(brd->basemem);
867 brd->basemem = NULL;
868 kfree(brd->ports);
869}
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871#ifdef CONFIG_PCI
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800872static int __devinit moxa_pci_probe(struct pci_dev *pdev,
873 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800875 struct moxa_board_conf *board;
876 unsigned int i;
877 int board_type = ent->driver_data;
878 int retval;
879
880 retval = pci_enable_device(pdev);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700881 if (retval) {
882 dev_err(&pdev->dev, "can't enable pci device\n");
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800883 goto err;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700884 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800885
886 for (i = 0; i < MAX_BOARDS; i++)
887 if (moxa_boards[i].basemem == NULL)
888 break;
889
890 retval = -ENODEV;
891 if (i >= MAX_BOARDS) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700892 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800893 "found. Board is ignored.\n", MAX_BOARDS);
894 goto err;
895 }
896
897 board = &moxa_boards[i];
Jiri Slabye46a5e32008-04-30 00:53:37 -0700898
899 retval = pci_request_region(pdev, 2, "moxa-base");
900 if (retval) {
901 dev_err(&pdev->dev, "can't request pci region 2\n");
902 goto err;
903 }
904
905 board->basemem = ioremap(pci_resource_start(pdev, 2), 0x4000);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700906 if (board->basemem == NULL) {
907 dev_err(&pdev->dev, "can't remap io space 2\n");
Jiri Slabye46a5e32008-04-30 00:53:37 -0700908 goto err_reg;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700909 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 board->boardType = board_type;
912 switch (board_type) {
913 case MOXA_BOARD_C218_ISA:
914 case MOXA_BOARD_C218_PCI:
915 board->numPorts = 8;
916 break;
917
918 case MOXA_BOARD_CP204J:
919 board->numPorts = 4;
920 break;
921 default:
922 board->numPorts = 0;
923 break;
924 }
925 board->busType = MOXA_BUS_TYPE_PCI;
Jiri Slabya784bf72007-02-10 01:45:36 -0800926
Jiri Slaby03718232008-04-30 00:53:39 -0700927 retval = moxa_init_board(board, &pdev->dev);
928 if (retval)
929 goto err_base;
930
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800931 pci_set_drvdata(pdev, board);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 return (0);
Jiri Slaby03718232008-04-30 00:53:39 -0700934err_base:
935 iounmap(board->basemem);
936 board->basemem = NULL;
Jiri Slabye46a5e32008-04-30 00:53:37 -0700937err_reg:
938 pci_release_region(pdev, 2);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800939err:
940 return retval;
941}
942
943static void __devexit moxa_pci_remove(struct pci_dev *pdev)
944{
945 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
946
Jiri Slaby810ab092008-04-30 00:53:41 -0700947 moxa_board_deinit(brd);
948
Jiri Slabye46a5e32008-04-30 00:53:37 -0700949 pci_release_region(pdev, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
Jiri Slabya784bf72007-02-10 01:45:36 -0800951
952static struct pci_driver moxa_pci_driver = {
953 .name = "moxa",
954 .id_table = moxa_pcibrds,
955 .probe = moxa_pci_probe,
956 .remove = __devexit_p(moxa_pci_remove)
957};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958#endif /* CONFIG_PCI */
959
960static int __init moxa_init(void)
961{
Jiri Slaby810ab092008-04-30 00:53:41 -0700962 unsigned int isabrds = 0;
Jiri Slabyd353eca2008-04-30 00:53:37 -0700963 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700965 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
966 MOXA_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
968 if (!moxaDriver)
969 return -ENOMEM;
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 moxaDriver->owner = THIS_MODULE;
Sergey Vlasov9b4e3b12005-09-03 16:26:49 +0100972 moxaDriver->name = "ttyMX";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 moxaDriver->major = ttymajor;
974 moxaDriver->minor_start = 0;
975 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
976 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
977 moxaDriver->init_termios = tty_std_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Alan Cox606d0992006-12-08 02:38:45 -0800979 moxaDriver->init_termios.c_ispeed = 9600;
980 moxaDriver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
982 tty_set_operations(moxaDriver, &moxa_ops);
983
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700984 pr_debug("Moxa tty devices major number = %d\n", ttymajor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 if (tty_register_driver(moxaDriver)) {
987 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
988 put_tty_driver(moxaDriver);
989 return -1;
990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Jiri Slabyd353eca2008-04-30 00:53:37 -0700992 /* Find the boards defined from module args. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700994 {
995 struct moxa_board_conf *brd = moxa_boards;
Jiri Slaby810ab092008-04-30 00:53:41 -0700996 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 for (i = 0; i < MAX_BOARDS; i++) {
Jiri Slabyd353eca2008-04-30 00:53:37 -0700998 if (!baseaddr[i])
999 break;
1000 if (type[i] == MOXA_BOARD_C218_ISA ||
1001 type[i] == MOXA_BOARD_C320_ISA) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001002 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001003 isabrds + 1, moxa_brdname[type[i] - 1],
1004 baseaddr[i]);
1005 brd->boardType = type[i];
1006 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1007 numports[i];
1008 brd->busType = MOXA_BUS_TYPE_ISA;
1009 brd->basemem = ioremap(baseaddr[i], 0x4000);
1010 if (!brd->basemem) {
1011 printk(KERN_ERR "moxa: can't remap %lx\n",
1012 baseaddr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 continue;
1014 }
Jiri Slaby03718232008-04-30 00:53:39 -07001015 if (moxa_init_board(brd, NULL)) {
1016 iounmap(brd->basemem);
1017 brd->basemem = NULL;
1018 continue;
1019 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001020
1021 brd++;
1022 isabrds++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
1024 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001029 retval = pci_register_driver(&moxa_pci_driver);
1030 if (retval) {
1031 printk(KERN_ERR "Can't register moxa pci driver!\n");
Jiri Slabyd353eca2008-04-30 00:53:37 -07001032 if (isabrds)
Jiri Slabya784bf72007-02-10 01:45:36 -08001033 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001036
Jiri Slabya784bf72007-02-10 01:45:36 -08001037 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
1040static void __exit moxa_exit(void)
1041{
1042 int i;
1043
Jiri Slabyc251ae02007-02-10 01:45:32 -08001044 del_timer_sync(&moxaTimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (tty_unregister_driver(moxaDriver))
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001047 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1048 "serial driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 put_tty_driver(moxaDriver);
Jiri Slaby86fbf142006-10-21 10:24:01 -07001050
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001051#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001052 pci_unregister_driver(&moxa_pci_driver);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001053#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001054
Jiri Slaby810ab092008-04-30 00:53:41 -07001055 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1056 if (moxa_boards[i].ready)
1057 moxa_board_deinit(&moxa_boards[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
1060module_init(moxa_init);
1061module_exit(moxa_exit);
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063static int moxa_open(struct tty_struct *tty, struct file *filp)
1064{
Jiri Slaby810ab092008-04-30 00:53:41 -07001065 struct moxa_board_conf *brd;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001066 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 int port;
1068 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Jiri Slaby11324ed2007-02-10 01:45:31 -08001070 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (port == MAX_PORTS) {
Jiri Slaby74d7d972008-04-30 00:53:43 -07001072 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
Jiri Slaby810ab092008-04-30 00:53:41 -07001074 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
1075 if (!brd->ready)
1076 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Jiri Slaby810ab092008-04-30 00:53:41 -07001078 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 ch->count++;
1080 tty->driver_data = ch;
1081 ch->tty = tty;
1082 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
1083 ch->statusflags = 0;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001084 moxa_set_tty_param(tty, tty->termios);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001085 MoxaPortLineCtrl(ch, 1, 1);
1086 MoxaPortEnable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 ch->asyncflags |= ASYNC_INITIALIZED;
1088 }
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001089 retval = moxa_block_till_ready(tty, filp, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 moxa_unthrottle(tty);
1092
1093 if (ch->type == PORT_16550A) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001094 MoxaSetFifo(ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 } else {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001096 MoxaSetFifo(ch, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
1098
1099 return (retval);
1100}
1101
1102static void moxa_close(struct tty_struct *tty, struct file *filp)
1103{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001104 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 int port;
1106
Jiri Slaby11324ed2007-02-10 01:45:31 -08001107 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (port == MAX_PORTS) {
1109 return;
1110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (tty->driver_data == NULL) {
1112 return;
1113 }
1114 if (tty_hung_up_p(filp)) {
1115 return;
1116 }
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001117 ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 if ((tty->count == 1) && (ch->count != 1)) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001120 printk(KERN_WARNING "moxa_close: bad serial port count; "
1121 "tty->count is 1, ch->count is %d\n", ch->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 ch->count = 1;
1123 }
1124 if (--ch->count < 0) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001125 printk(KERN_WARNING "moxa_close: bad serial port count, "
1126 "device=%s\n", tty->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 ch->count = 0;
1128 }
1129 if (ch->count) {
1130 return;
1131 }
1132 ch->asyncflags |= ASYNC_CLOSING;
1133
1134 ch->cflag = tty->termios->c_cflag;
1135 if (ch->asyncflags & ASYNC_INITIALIZED) {
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001136 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
Jiri Slabyb4173f42008-04-30 00:53:40 -07001138 del_timer_sync(&ch->emptyTimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001140 moxa_shut_down(ch);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001141 MoxaPortFlushData(ch, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 if (tty->driver->flush_buffer)
1144 tty->driver->flush_buffer(tty);
1145 tty_ldisc_flush(tty);
1146
1147 tty->closing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 ch->tty = NULL;
1149 if (ch->blocked_open) {
1150 if (ch->close_delay) {
1151 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
1152 }
1153 wake_up_interruptible(&ch->open_wait);
1154 }
1155 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
Jiri Slaby95e07912007-10-18 03:06:25 -07001156 complete_all(&ch->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
1159static int moxa_write(struct tty_struct *tty,
1160 const unsigned char *buf, int count)
1161{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001162 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 unsigned long flags;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001164 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (ch == NULL)
Jiri Slabyb4173f42008-04-30 00:53:40 -07001167 return 0;
Alan Cox33f0f882006-01-09 20:54:13 -08001168
1169 spin_lock_irqsave(&moxa_lock, flags);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001170 len = MoxaPortWriteData(ch, (unsigned char *) buf, count);
Alan Cox33f0f882006-01-09 20:54:13 -08001171 spin_unlock_irqrestore(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 /*********************************************
1174 if ( !(ch->statusflags & LOWWAIT) &&
1175 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
1176 ************************************************/
1177 ch->statusflags |= LOWWAIT;
1178 return (len);
1179}
1180
1181static int moxa_write_room(struct tty_struct *tty)
1182{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001183 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 if (tty->stopped)
1186 return (0);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001187 ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (ch == NULL)
1189 return (0);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001190 return MoxaPortTxFree(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
1192
1193static void moxa_flush_buffer(struct tty_struct *tty)
1194{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001195 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 if (ch == NULL)
1198 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001199 MoxaPortFlushData(ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 tty_wakeup(tty);
1201}
1202
1203static int moxa_chars_in_buffer(struct tty_struct *tty)
1204{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001205 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 /*
1209 * Sigh...I have to check if driver_data is NULL here, because
1210 * if an open() fails, the TTY subsystem eventually calls
1211 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1212 * routine. And since the open() failed, we return 0 here. TDJ
1213 */
1214 if (ch == NULL)
1215 return (0);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001216 chars = MoxaPortTxQueue(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (chars) {
1218 /*
1219 * Make it possible to wakeup anything waiting for output
1220 * in tty_ioctl.c, etc.
1221 */
1222 if (!(ch->statusflags & EMPTYWAIT))
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001223 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
1225 return (chars);
1226}
1227
1228static void moxa_flush_chars(struct tty_struct *tty)
1229{
1230 /*
1231 * Don't think I need this, because this is called to empty the TX
1232 * buffer for the 16450, 16550, etc.
1233 */
1234}
1235
1236static void moxa_put_char(struct tty_struct *tty, unsigned char c)
1237{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001238 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 unsigned long flags;
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (ch == NULL)
1242 return;
Alan Cox33f0f882006-01-09 20:54:13 -08001243 spin_lock_irqsave(&moxa_lock, flags);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001244 MoxaPortWriteData(ch, &c, 1);
Alan Cox33f0f882006-01-09 20:54:13 -08001245 spin_unlock_irqrestore(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 /************************************************
1247 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
1248 *************************************************/
1249 ch->statusflags |= LOWWAIT;
1250}
1251
1252static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1253{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001254 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 int flag = 0, dtr, rts;
1256
Jiri Slaby74d7d972008-04-30 00:53:43 -07001257 if (!ch)
1258 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Jiri Slabyb4173f42008-04-30 00:53:40 -07001260 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (dtr)
1262 flag |= TIOCM_DTR;
1263 if (rts)
1264 flag |= TIOCM_RTS;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001265 dtr = MoxaPortLineStatus(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (dtr & 1)
1267 flag |= TIOCM_CTS;
1268 if (dtr & 2)
1269 flag |= TIOCM_DSR;
1270 if (dtr & 4)
1271 flag |= TIOCM_CD;
1272 return flag;
1273}
1274
1275static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1276 unsigned int set, unsigned int clear)
1277{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001278 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 int port;
1280 int dtr, rts;
1281
Jiri Slaby11324ed2007-02-10 01:45:31 -08001282 port = tty->index;
Jiri Slaby74d7d972008-04-30 00:53:43 -07001283 if (!ch)
1284 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Jiri Slabyb4173f42008-04-30 00:53:40 -07001286 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (set & TIOCM_RTS)
1288 rts = 1;
1289 if (set & TIOCM_DTR)
1290 dtr = 1;
1291 if (clear & TIOCM_RTS)
1292 rts = 0;
1293 if (clear & TIOCM_DTR)
1294 dtr = 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001295 MoxaPortLineCtrl(ch, dtr, rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return 0;
1297}
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299static void moxa_throttle(struct tty_struct *tty)
1300{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001301 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 ch->statusflags |= THROTTLE;
1304}
1305
1306static void moxa_unthrottle(struct tty_struct *tty)
1307{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001308 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 ch->statusflags &= ~THROTTLE;
1311}
1312
1313static void moxa_set_termios(struct tty_struct *tty,
Alan Cox606d0992006-12-08 02:38:45 -08001314 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001316 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 if (ch == NULL)
1319 return;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001320 moxa_set_tty_param(tty, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (!(old_termios->c_cflag & CLOCAL) &&
1322 (tty->termios->c_cflag & CLOCAL))
1323 wake_up_interruptible(&ch->open_wait);
1324}
1325
1326static void moxa_stop(struct tty_struct *tty)
1327{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001328 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 if (ch == NULL)
1331 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001332 MoxaPortTxDisable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 ch->statusflags |= TXSTOPPED;
1334}
1335
1336
1337static void moxa_start(struct tty_struct *tty)
1338{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001339 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 if (ch == NULL)
1342 return;
1343
1344 if (!(ch->statusflags & TXSTOPPED))
1345 return;
1346
Jiri Slabyb4173f42008-04-30 00:53:40 -07001347 MoxaPortTxEnable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 ch->statusflags &= ~TXSTOPPED;
1349}
1350
1351static void moxa_hangup(struct tty_struct *tty)
1352{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001353 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 moxa_flush_buffer(tty);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001356 moxa_shut_down(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 ch->count = 0;
1358 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1359 ch->tty = NULL;
1360 wake_up_interruptible(&ch->open_wait);
1361}
1362
1363static void moxa_poll(unsigned long ignored)
1364{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001365 struct moxa_port *ch;
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -07001366 struct tty_struct *tty;
1367 unsigned int card;
1368 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 del_timer(&moxaTimer);
1371
1372 if (MoxaDriverPoll() < 0) {
Jiri Slabyaa7e5222007-02-10 01:45:27 -08001373 mod_timer(&moxaTimer, jiffies + HZ / 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return;
1375 }
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -07001376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 for (card = 0; card < MAX_BOARDS; card++) {
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -07001378 if (!moxa_boards[card].ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 continue;
Jiri Slaby810ab092008-04-30 00:53:41 -07001380 ch = moxa_boards[card].ports;
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -07001381 for (i = 0; i < moxa_boards[card].numPorts; i++, ch++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
1383 continue;
1384 if (!(ch->statusflags & THROTTLE) &&
Jiri Slabyb4173f42008-04-30 00:53:40 -07001385 (MoxaPortRxQueue(ch) > 0))
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001386 moxa_receive_data(ch);
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -07001387 tty = ch->tty;
1388 if (tty == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 continue;
1390 if (ch->statusflags & LOWWAIT) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001391 if (MoxaPortTxQueue(ch) <= WAKEUP_CHARS) {
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -07001392 if (!tty->stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 ch->statusflags &= ~LOWWAIT;
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -07001394 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
1396 }
1397 }
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -07001398 if (!I_IGNBRK(tty) && (MoxaPortResetBrkCnt(ch) > 0)) {
1399 tty_insert_flip_char(tty, 0, TTY_BREAK);
1400 tty_schedule_flip(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
Jiri Slabyb4173f42008-04-30 00:53:40 -07001402 if (MoxaPortDCDChange(ch)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (ch->asyncflags & ASYNC_CHECK_CD) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001404 if (MoxaPortDCDON(ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 wake_up_interruptible(&ch->open_wait);
1406 else {
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -07001407 tty_hangup(tty);
Jiri Slabyba196df2007-02-10 01:45:28 -08001408 wake_up_interruptible(&ch->open_wait);
1409 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411 }
1412 }
1413 }
1414 }
1415
Jiri Slabyaa7e5222007-02-10 01:45:27 -08001416 mod_timer(&moxaTimer, jiffies + HZ / 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
1419/******************************************************************************/
1420
Alan Coxdb1acaa2008-02-08 04:18:43 -08001421static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
Alan Cox606d0992006-12-08 02:38:45 -08001423 register struct ktermios *ts;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001424 struct moxa_port *ch;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001425 int rts, cts, txflow, rxflow, xany, baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001427 ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 ts = tty->termios;
1429 if (ts->c_cflag & CLOCAL)
1430 ch->asyncflags &= ~ASYNC_CHECK_CD;
1431 else
1432 ch->asyncflags |= ASYNC_CHECK_CD;
1433 rts = cts = txflow = rxflow = xany = 0;
1434 if (ts->c_cflag & CRTSCTS)
1435 rts = cts = 1;
1436 if (ts->c_iflag & IXON)
1437 txflow = 1;
1438 if (ts->c_iflag & IXOFF)
1439 rxflow = 1;
1440 if (ts->c_iflag & IXANY)
1441 xany = 1;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001442
1443 /* Clear the features we don't support */
1444 ts->c_cflag &= ~CMSPAR;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001445 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1446 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
Alan Coxdb1acaa2008-02-08 04:18:43 -08001447 if (baud == -1)
1448 baud = tty_termios_baud_rate(old_termios);
1449 /* Not put the baud rate into the termios data */
1450 tty_encode_baud_rate(tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
1452
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001453static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001454 struct moxa_port *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
1456 DECLARE_WAITQUEUE(wait,current);
1457 unsigned long flags;
1458 int retval;
1459 int do_clocal = C_CLOCAL(tty);
1460
1461 /*
1462 * If the device is in the middle of being closed, then block
1463 * until it's done, and then try again.
1464 */
1465 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
1466 if (ch->asyncflags & ASYNC_CLOSING)
Jiri Slaby95e07912007-10-18 03:06:25 -07001467 wait_for_completion_interruptible(&ch->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468#ifdef SERIAL_DO_RESTART
1469 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1470 return (-EAGAIN);
1471 else
1472 return (-ERESTARTSYS);
1473#else
1474 return (-EAGAIN);
1475#endif
1476 }
1477 /*
1478 * If non-blocking mode is set, then make the check up front
1479 * and then exit.
1480 */
1481 if (filp->f_flags & O_NONBLOCK) {
1482 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1483 return (0);
1484 }
1485 /*
1486 * Block waiting for the carrier detect and the line to become free
1487 */
1488 retval = 0;
1489 add_wait_queue(&ch->open_wait, &wait);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001490 pr_debug("block_til_ready before block: ttys%d, count = %d\n",
Jiri Slabyb4173f42008-04-30 00:53:40 -07001491 tty->index, ch->count);
Alan Cox33f0f882006-01-09 20:54:13 -08001492 spin_lock_irqsave(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (!tty_hung_up_p(filp))
1494 ch->count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 ch->blocked_open++;
Alan Cox33f0f882006-01-09 20:54:13 -08001496 spin_unlock_irqrestore(&moxa_lock, flags);
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 while (1) {
1499 set_current_state(TASK_INTERRUPTIBLE);
1500 if (tty_hung_up_p(filp) ||
1501 !(ch->asyncflags & ASYNC_INITIALIZED)) {
1502#ifdef SERIAL_DO_RESTART
1503 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1504 retval = -EAGAIN;
1505 else
1506 retval = -ERESTARTSYS;
1507#else
1508 retval = -EAGAIN;
1509#endif
1510 break;
1511 }
1512 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
Jiri Slabyb4173f42008-04-30 00:53:40 -07001513 MoxaPortDCDON(ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 break;
1515
1516 if (signal_pending(current)) {
1517 retval = -ERESTARTSYS;
1518 break;
1519 }
1520 schedule();
1521 }
1522 set_current_state(TASK_RUNNING);
1523 remove_wait_queue(&ch->open_wait, &wait);
Alan Cox33f0f882006-01-09 20:54:13 -08001524
1525 spin_lock_irqsave(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (!tty_hung_up_p(filp))
1527 ch->count++;
1528 ch->blocked_open--;
Alan Cox33f0f882006-01-09 20:54:13 -08001529 spin_unlock_irqrestore(&moxa_lock, flags);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001530 pr_debug("block_til_ready after blocking: ttys%d, count = %d\n",
Jiri Slabyb4173f42008-04-30 00:53:40 -07001531 tty->index, ch->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (retval)
1533 return (retval);
Alan Cox33f0f882006-01-09 20:54:13 -08001534 /* FIXME: review to see if we need to use set_bit on these */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
Alan Cox33f0f882006-01-09 20:54:13 -08001536 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001539static void moxa_setup_empty_event(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001541 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 unsigned long flags;
1543
Alan Cox33f0f882006-01-09 20:54:13 -08001544 spin_lock_irqsave(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 ch->statusflags |= EMPTYWAIT;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001546 mod_timer(&ch->emptyTimer, jiffies + HZ);
Alan Cox33f0f882006-01-09 20:54:13 -08001547 spin_unlock_irqrestore(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001550static void moxa_check_xmit_empty(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001552 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001554 ch = (struct moxa_port *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001556 if (MoxaPortTxQueue(ch) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 ch->statusflags &= ~EMPTYWAIT;
1558 tty_wakeup(ch->tty);
1559 return;
1560 }
Jiri Slabyb4173f42008-04-30 00:53:40 -07001561 mod_timer(&ch->emptyTimer, round_jiffies(jiffies + HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 } else
1563 ch->statusflags &= ~EMPTYWAIT;
1564}
1565
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001566static void moxa_shut_down(struct moxa_port *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
1568 struct tty_struct *tp;
1569
1570 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1571 return;
1572
1573 tp = ch->tty;
1574
Jiri Slabyb4173f42008-04-30 00:53:40 -07001575 MoxaPortDisable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 /*
1578 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1579 */
1580 if (tp->termios->c_cflag & HUPCL)
Jiri Slabyb4173f42008-04-30 00:53:40 -07001581 MoxaPortLineCtrl(ch, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 ch->asyncflags &= ~ASYNC_INITIALIZED;
1584}
1585
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001586static void moxa_receive_data(struct moxa_port *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
1588 struct tty_struct *tp;
Alan Cox606d0992006-12-08 02:38:45 -08001589 struct ktermios *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 unsigned long flags;
1591
1592 ts = NULL;
1593 tp = ch->tty;
1594 if (tp)
1595 ts = tp->termios;
1596 /**************************************************
1597 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1598 *****************************************************/
1599 if (!tp || !ts) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001600 MoxaPortFlushData(ch, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 return;
1602 }
Alan Cox33f0f882006-01-09 20:54:13 -08001603 spin_lock_irqsave(&moxa_lock, flags);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001604 MoxaPortReadData(ch, tp);
Alan Cox33f0f882006-01-09 20:54:13 -08001605 spin_unlock_irqrestore(&moxa_lock, flags);
1606 tty_schedule_flip(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609/*
1610 * Query
1611 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613#define DCD_changed 0x01
1614#define DCD_oldstate 0x80
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616static int moxaLowWaterChk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001618static void moxa_low_water_check(void __iomem *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
1620/*****************************************************************************
1621 * Driver level functions: *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 * 3. MoxaDriverPoll(void); *
1623 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Jiri Slabyb4173f42008-04-30 00:53:40 -07001625static void MoxaPortFlushData(struct moxa_port *port, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
1627 void __iomem *ofsAddr;
1628 if ((mode < 0) || (mode > 2))
1629 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001630 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 moxafunc(ofsAddr, FC_FlushQueue, mode);
1632 if (mode != 1) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001633 port->lowChkFlag = 0;
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001634 moxa_low_water_check(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
1636}
1637
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -07001638static int MoxaDriverPoll(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001640 struct moxa_board_conf *brd;
Jiri Slaby810ab092008-04-30 00:53:41 -07001641 struct moxa_port *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 void __iomem *ofsAddr;
1643 void __iomem *ip;
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -07001644 unsigned int port, ports, card;
1645 ushort temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 for (card = 0; card < MAX_BOARDS; card++) {
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001648 brd = &moxa_boards[card];
Jiri Slaby810ab092008-04-30 00:53:41 -07001649 if (brd->ready == 0)
Dirk Eibach01cfaf02006-08-27 01:23:36 -07001650 continue;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001651 if ((ports = brd->numPorts) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 continue;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001653 if (readb(brd->intPend) == 0xff) {
1654 ip = brd->intTable + readb(brd->intNdx);
Jiri Slaby810ab092008-04-30 00:53:41 -07001655 p = brd->ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 ports <<= 1;
1657 for (port = 0; port < ports; port += 2, p++) {
Jiri Slaby810ab092008-04-30 00:53:41 -07001658 temp = readw(ip + port);
1659 if (temp == 0)
1660 continue;
1661
1662 writew(0, ip + port);
1663 ofsAddr = p->tableAddr;
1664 if (temp & IntrTx)
1665 writew(readw(ofsAddr + HostStat) &
1666 ~WakeupTx, ofsAddr + HostStat);
1667 if (temp & IntrBreak)
1668 p->breakCnt++;
1669
1670 if (temp & IntrLine) {
1671 if (readb(ofsAddr + FlagStat) & DCD_state) {
1672 if ((p->DCDState & DCD_oldstate) == 0)
1673 p->DCDState = (DCD_oldstate |
1674 DCD_changed);
1675 } else {
1676 if (p->DCDState & DCD_oldstate)
1677 p->DCDState = DCD_changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 }
1679 }
1680 }
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001681 writeb(0, brd->intPend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
1683 if (moxaLowWaterChk) {
Jiri Slaby810ab092008-04-30 00:53:41 -07001684 p = brd->ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 for (port = 0; port < ports; port++, p++) {
Jiri Slaby810ab092008-04-30 00:53:41 -07001686 if (p->lowChkFlag) {
1687 p->lowChkFlag = 0;
1688 ofsAddr = p->tableAddr;
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001689 moxa_low_water_check(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
1691 }
1692 }
1693 }
1694 moxaLowWaterChk = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -07001696 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
1698
1699/*****************************************************************************
1700 * Port level functions: *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 * 2. MoxaPortEnable(int port); *
1702 * 3. MoxaPortDisable(int port); *
1703 * 4. MoxaPortGetMaxBaud(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 * 6. MoxaPortSetBaud(int port, long baud); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1706 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1707 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1708 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1709 * 12. MoxaPortLineStatus(int port); *
1710 * 13. MoxaPortDCDChange(int port); *
1711 * 14. MoxaPortDCDON(int port); *
1712 * 15. MoxaPortFlushData(int port, int mode); *
1713 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
Alan Cox33f0f882006-01-09 20:54:13 -08001714 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 * 20. MoxaPortTxQueue(int port); *
1716 * 21. MoxaPortTxFree(int port); *
1717 * 22. MoxaPortRxQueue(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 * 24. MoxaPortTxDisable(int port); *
1719 * 25. MoxaPortTxEnable(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 * 27. MoxaPortResetBrkCnt(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 *****************************************************************************/
1722/*
1723 * Moxa Port Number Description:
1724 *
1725 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1726 * the port number using in MOXA driver functions will be 0 to 31 for
1727 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1728 * to 127 for fourth. For example, if you setup three MOXA boards,
1729 * first board is C218, second board is C320-16 and third board is
1730 * C320-32. The port number of first board (C218 - 8 ports) is from
1731 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1732 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1733 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1734 * 127 will be invalid.
1735 *
1736 *
1737 * Moxa Functions Description:
1738 *
1739 * Function 1: Driver initialization routine, this routine must be
1740 * called when initialized driver.
1741 * Syntax:
1742 * void MoxaDriverInit();
1743 *
1744 *
1745 * Function 2: Moxa driver private IOCTL command processing.
1746 * Syntax:
1747 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1748 *
1749 * unsigned int cmd : IOCTL command
1750 * unsigned long arg : IOCTL argument
1751 * int port : port number (0 - 127)
1752 *
1753 * return: 0 (OK)
1754 * -EINVAL
1755 * -ENOIOCTLCMD
1756 *
1757 *
1758 * Function 3: Moxa driver polling process routine.
1759 * Syntax:
1760 * int MoxaDriverPoll(void);
1761 *
1762 * return: 0 ; polling O.K.
1763 * -1 : no any Moxa card.
1764 *
1765 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 * Function 6: Enable this port to start Tx/Rx data.
1767 * Syntax:
1768 * void MoxaPortEnable(int port);
1769 * int port : port number (0 - 127)
1770 *
1771 *
1772 * Function 7: Disable this port
1773 * Syntax:
1774 * void MoxaPortDisable(int port);
1775 * int port : port number (0 - 127)
1776 *
1777 *
1778 * Function 8: Get the maximun available baud rate of this port.
1779 * Syntax:
1780 * long MoxaPortGetMaxBaud(int port);
1781 * int port : port number (0 - 127)
1782 *
1783 * return: 0 : this port is invalid
1784 * 38400/57600/115200 bps
1785 *
1786 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 * Function 10: Setting baud rate of this port.
1788 * Syntax:
1789 * long MoxaPortSetBaud(int port, long baud);
1790 * int port : port number (0 - 127)
1791 * long baud : baud rate (50 - 115200)
1792 *
1793 * return: 0 : this port is invalid or baud < 50
1794 * 50 - 115200 : the real baud rate set to the port, if
1795 * the argument baud is large than maximun
1796 * available baud rate, the real setting
1797 * baud rate will be the maximun baud rate.
1798 *
1799 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 * Function 12: Configure the port.
1801 * Syntax:
Alan Cox606d0992006-12-08 02:38:45 -08001802 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 * int port : port number (0 - 127)
Alan Cox606d0992006-12-08 02:38:45 -08001804 * struct ktermios * termio : termio structure pointer
Alan Coxc7bce302006-09-30 23:27:24 -07001805 * speed_t baud : baud rate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 *
1807 * return: -1 : this port is invalid or termio == NULL
1808 * 0 : setting O.K.
1809 *
1810 *
1811 * Function 13: Get the DTR/RTS state of this port.
1812 * Syntax:
1813 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1814 * int port : port number (0 - 127)
1815 * int * dtrState : pointer to INT to receive the current DTR
1816 * state. (if NULL, this function will not
1817 * write to this address)
1818 * int * rtsState : pointer to INT to receive the current RTS
1819 * state. (if NULL, this function will not
1820 * write to this address)
1821 *
1822 * return: -1 : this port is invalid
1823 * 0 : O.K.
1824 *
1825 *
1826 * Function 14: Setting the DTR/RTS output state of this port.
1827 * Syntax:
1828 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1829 * int port : port number (0 - 127)
1830 * int dtrState : DTR output state (0: off, 1: on)
1831 * int rtsState : RTS output state (0: off, 1: on)
1832 *
1833 *
1834 * Function 15: Setting the flow control of this port.
1835 * Syntax:
1836 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1837 * int txFlow,int xany);
1838 * int port : port number (0 - 127)
1839 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1840 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1841 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1842 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1843 * int xany : S/W XANY flow control (0: no, 1: yes)
1844 *
1845 *
1846 * Function 16: Get ths line status of this port
1847 * Syntax:
1848 * int MoxaPortLineStatus(int port);
1849 * int port : port number (0 - 127)
1850 *
1851 * return: Bit 0 - CTS state (0: off, 1: on)
1852 * Bit 1 - DSR state (0: off, 1: on)
1853 * Bit 2 - DCD state (0: off, 1: on)
1854 *
1855 *
1856 * Function 17: Check the DCD state has changed since the last read
1857 * of this function.
1858 * Syntax:
1859 * int MoxaPortDCDChange(int port);
1860 * int port : port number (0 - 127)
1861 *
1862 * return: 0 : no changed
1863 * 1 : DCD has changed
1864 *
1865 *
1866 * Function 18: Check ths current DCD state is ON or not.
1867 * Syntax:
1868 * int MoxaPortDCDON(int port);
1869 * int port : port number (0 - 127)
1870 *
1871 * return: 0 : DCD off
1872 * 1 : DCD on
1873 *
1874 *
1875 * Function 19: Flush the Rx/Tx buffer data of this port.
1876 * Syntax:
1877 * void MoxaPortFlushData(int port, int mode);
1878 * int port : port number (0 - 127)
1879 * int mode
1880 * 0 : flush the Rx buffer
1881 * 1 : flush the Tx buffer
1882 * 2 : flush the Rx and Tx buffer
1883 *
1884 *
1885 * Function 20: Write data.
1886 * Syntax:
1887 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1888 * int port : port number (0 - 127)
1889 * unsigned char * buffer : pointer to write data buffer.
1890 * int length : write data length
1891 *
1892 * return: 0 - length : real write data length
1893 *
1894 *
1895 * Function 21: Read data.
1896 * Syntax:
Alan Cox33f0f882006-01-09 20:54:13 -08001897 * int MoxaPortReadData(int port, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 * int port : port number (0 - 127)
Alan Cox33f0f882006-01-09 20:54:13 -08001899 * struct tty_struct *tty : tty for data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 *
1901 * return: 0 - length : real read data length
1902 *
1903 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 * Function 24: Get the Tx buffer current queued data bytes
1905 * Syntax:
1906 * int MoxaPortTxQueue(int port);
1907 * int port : port number (0 - 127)
1908 *
1909 * return: .. : Tx buffer current queued data bytes
1910 *
1911 *
1912 * Function 25: Get the Tx buffer current free space
1913 * Syntax:
1914 * int MoxaPortTxFree(int port);
1915 * int port : port number (0 - 127)
1916 *
1917 * return: .. : Tx buffer current free space
1918 *
1919 *
1920 * Function 26: Get the Rx buffer current queued data bytes
1921 * Syntax:
1922 * int MoxaPortRxQueue(int port);
1923 * int port : port number (0 - 127)
1924 *
1925 * return: .. : Rx buffer current queued data bytes
1926 *
1927 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 * Function 28: Disable port data transmission.
1929 * Syntax:
1930 * void MoxaPortTxDisable(int port);
1931 * int port : port number (0 - 127)
1932 *
1933 *
1934 * Function 29: Enable port data transmission.
1935 * Syntax:
1936 * void MoxaPortTxEnable(int port);
1937 * int port : port number (0 - 127)
1938 *
1939 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 * Function 31: Get the received BREAK signal count and reset it.
1941 * Syntax:
1942 * int MoxaPortResetBrkCnt(int port);
1943 * int port : port number (0 - 127)
1944 *
1945 * return: 0 - .. : BREAK signal count
1946 *
1947 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Jiri Slabyb4173f42008-04-30 00:53:40 -07001950static void MoxaPortEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
1952 void __iomem *ofsAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 short lowwater = 512;
1954
Jiri Slabyb4173f42008-04-30 00:53:40 -07001955 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 writew(lowwater, ofsAddr + Low_water);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001957 port->breakCnt = 0;
1958 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1959 port->board->boardType == MOXA_BOARD_C320_PCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1961 } else {
1962 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
1963 }
1964
1965 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1966 moxafunc(ofsAddr, FC_FlushQueue, 2);
1967
1968 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1969 MoxaPortLineStatus(port);
1970}
1971
Jiri Slabyb4173f42008-04-30 00:53:40 -07001972static void MoxaPortDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001974 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
1976 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1977 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1978 writew(0, ofsAddr + HostStat);
1979 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1980}
1981
Jiri Slabyb4173f42008-04-30 00:53:40 -07001982static long MoxaPortGetMaxBaud(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001984 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1985 port->board->boardType == MOXA_BOARD_C320_PCI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return (460800L);
1987 else
1988 return (921600L);
1989}
1990
1991
Jiri Slabyb4173f42008-04-30 00:53:40 -07001992static long MoxaPortSetBaud(struct moxa_port *port, long baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
1994 void __iomem *ofsAddr;
1995 long max, clock;
1996 unsigned int val;
1997
1998 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
1999 return (0);
Jiri Slabyb4173f42008-04-30 00:53:40 -07002000 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 if (baud > max)
2002 baud = max;
2003 if (max == 38400L)
2004 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
2005 else if (max == 57600L)
2006 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
2007 else
2008 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2009 val = clock / baud;
2010 moxafunc(ofsAddr, FC_SetBaud, val);
2011 baud = clock / val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 return (baud);
2013}
2014
Jiri Slabyb4173f42008-04-30 00:53:40 -07002015static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
2016 speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
2018 void __iomem *ofsAddr;
2019 tcflag_t cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 tcflag_t mode = 0;
2021
Jiri Slabyb4173f42008-04-30 00:53:40 -07002022 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 cflag = termio->c_cflag; /* termio->c_cflag */
2024
2025 mode = termio->c_cflag & CSIZE;
2026 if (mode == CS5)
2027 mode = MX_CS5;
2028 else if (mode == CS6)
2029 mode = MX_CS6;
2030 else if (mode == CS7)
2031 mode = MX_CS7;
2032 else if (mode == CS8)
2033 mode = MX_CS8;
2034
2035 if (termio->c_cflag & CSTOPB) {
2036 if (mode == MX_CS5)
2037 mode |= MX_STOP15;
2038 else
2039 mode |= MX_STOP2;
2040 } else
2041 mode |= MX_STOP1;
2042
2043 if (termio->c_cflag & PARENB) {
2044 if (termio->c_cflag & PARODD)
2045 mode |= MX_PARODD;
2046 else
2047 mode |= MX_PAREVEN;
2048 } else
2049 mode |= MX_PARNONE;
2050
2051 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2052
Jiri Slabyb4173f42008-04-30 00:53:40 -07002053 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
2054 port->board->boardType == MOXA_BOARD_C320_PCI) {
Alan Coxc7bce302006-09-30 23:27:24 -07002055 if (baud >= 921600L)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 return (-1);
2057 }
Alan Coxdb1acaa2008-02-08 04:18:43 -08002058 baud = MoxaPortSetBaud(port, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2061 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2062 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2063 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07002064 moxa_wait_finish(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
2066 }
Alan Coxdb1acaa2008-02-08 04:18:43 -08002067 return (baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068}
2069
Jiri Slabyb4173f42008-04-30 00:53:40 -07002070static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
2071 int *rtsState)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
2073
Jiri Slabyb4173f42008-04-30 00:53:40 -07002074 if (dtrState)
2075 *dtrState = !!(port->lineCtrl & DTR_ON);
2076 if (rtsState)
2077 *rtsState = !!(port->lineCtrl & RTS_ON);
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 return (0);
2080}
2081
Jiri Slabyb4173f42008-04-30 00:53:40 -07002082static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002084 int mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 if (dtr)
2087 mode |= DTR_ON;
2088 if (rts)
2089 mode |= RTS_ON;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002090 port->lineCtrl = mode;
2091 moxafunc(port->tableAddr, FC_LineControl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
2093
Jiri Slabyb4173f42008-04-30 00:53:40 -07002094static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
2095 int txflow, int rxflow, int txany)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002097 int mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 if (rts)
2100 mode |= RTS_FlowCtl;
2101 if (cts)
2102 mode |= CTS_FlowCtl;
2103 if (txflow)
2104 mode |= Tx_FlowCtl;
2105 if (rxflow)
2106 mode |= Rx_FlowCtl;
2107 if (txany)
2108 mode |= IXM_IXANY;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002109 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
Jiri Slabyb4173f42008-04-30 00:53:40 -07002112static int MoxaPortLineStatus(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
2114 void __iomem *ofsAddr;
2115 int val;
2116
Jiri Slabyb4173f42008-04-30 00:53:40 -07002117 ofsAddr = port->tableAddr;
2118 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
2119 port->board->boardType == MOXA_BOARD_C320_PCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 moxafunc(ofsAddr, FC_LineStatus, 0);
2121 val = readw(ofsAddr + FuncArg);
2122 } else {
2123 val = readw(ofsAddr + FlagStat) >> 4;
2124 }
2125 val &= 0x0B;
2126 if (val & 8) {
2127 val |= 4;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002128 if ((port->DCDState & DCD_oldstate) == 0)
2129 port->DCDState = (DCD_oldstate | DCD_changed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 } else {
Jiri Slabyb4173f42008-04-30 00:53:40 -07002131 if (port->DCDState & DCD_oldstate)
2132 port->DCDState = DCD_changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
2134 val &= 7;
2135 return (val);
2136}
2137
Jiri Slabyb4173f42008-04-30 00:53:40 -07002138static int MoxaPortDCDChange(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
2140 int n;
2141
Jiri Slabyb4173f42008-04-30 00:53:40 -07002142 n = port->DCDState;
2143 port->DCDState &= ~DCD_changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 n &= DCD_changed;
2145 return (n);
2146}
2147
Jiri Slabyb4173f42008-04-30 00:53:40 -07002148static int MoxaPortDCDON(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149{
2150 int n;
2151
Jiri Slabyb4173f42008-04-30 00:53:40 -07002152 if (port->DCDState & DCD_oldstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 n = 1;
2154 else
2155 n = 0;
2156 return (n);
2157}
2158
Jiri Slabyb4173f42008-04-30 00:53:40 -07002159static int MoxaPortWriteData(struct moxa_port *port, unsigned char *buffer,
2160 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161{
2162 int c, total, i;
2163 ushort tail;
2164 int cnt;
2165 ushort head, tx_mask, spage, epage;
2166 ushort pageno, pageofs, bufhead;
2167 void __iomem *baseAddr, *ofsAddr, *ofs;
2168
Jiri Slabyb4173f42008-04-30 00:53:40 -07002169 ofsAddr = port->tableAddr;
2170 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 tx_mask = readw(ofsAddr + TX_mask);
2172 spage = readw(ofsAddr + Page_txb);
2173 epage = readw(ofsAddr + EndPage_txb);
2174 tail = readw(ofsAddr + TXwptr);
2175 head = readw(ofsAddr + TXrptr);
2176 c = (head > tail) ? (head - tail - 1)
2177 : (head - tail + tx_mask);
2178 if (c > len)
2179 c = len;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002180 moxaLog.txcnt[port->tty->index] += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 total = c;
2182 if (spage == epage) {
2183 bufhead = readw(ofsAddr + Ofs_txb);
2184 writew(spage, baseAddr + Control_reg);
2185 while (c > 0) {
2186 if (head > tail)
2187 len = head - tail - 1;
2188 else
2189 len = tx_mask + 1 - tail;
2190 len = (c > len) ? len : c;
2191 ofs = baseAddr + DynPage_addr + bufhead + tail;
2192 for (i = 0; i < len; i++)
2193 writeb(*buffer++, ofs + i);
2194 tail = (tail + len) & tx_mask;
2195 c -= len;
2196 }
2197 writew(tail, ofsAddr + TXwptr);
2198 } else {
2199 len = c;
2200 pageno = spage + (tail >> 13);
2201 pageofs = tail & Page_mask;
2202 do {
2203 cnt = Page_size - pageofs;
2204 if (cnt > c)
2205 cnt = c;
2206 c -= cnt;
2207 writeb(pageno, baseAddr + Control_reg);
2208 ofs = baseAddr + DynPage_addr + pageofs;
2209 for (i = 0; i < cnt; i++)
2210 writeb(*buffer++, ofs + i);
2211 if (c == 0) {
2212 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2213 break;
2214 }
2215 if (++pageno == epage)
2216 pageno = spage;
2217 pageofs = 0;
2218 } while (1);
2219 }
2220 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2221 return (total);
2222}
2223
Jiri Slabyb4173f42008-04-30 00:53:40 -07002224static int MoxaPortReadData(struct moxa_port *port, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
2226 register ushort head, pageofs;
2227 int i, count, cnt, len, total, remain;
2228 ushort tail, rx_mask, spage, epage;
2229 ushort pageno, bufhead;
2230 void __iomem *baseAddr, *ofsAddr, *ofs;
2231
Jiri Slabyb4173f42008-04-30 00:53:40 -07002232 ofsAddr = port->tableAddr;
2233 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 head = readw(ofsAddr + RXrptr);
2235 tail = readw(ofsAddr + RXwptr);
2236 rx_mask = readw(ofsAddr + RX_mask);
2237 spage = readw(ofsAddr + Page_rxb);
2238 epage = readw(ofsAddr + EndPage_rxb);
2239 count = (tail >= head) ? (tail - head)
2240 : (tail - head + rx_mask + 1);
2241 if (count == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08002242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Alan Cox33f0f882006-01-09 20:54:13 -08002244 total = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 remain = count - total;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002246 moxaLog.rxcnt[port->tty->index] += total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 count = total;
2248 if (spage == epage) {
2249 bufhead = readw(ofsAddr + Ofs_rxb);
2250 writew(spage, baseAddr + Control_reg);
2251 while (count > 0) {
2252 if (tail >= head)
2253 len = tail - head;
2254 else
2255 len = rx_mask + 1 - head;
2256 len = (count > len) ? len : count;
2257 ofs = baseAddr + DynPage_addr + bufhead + head;
2258 for (i = 0; i < len; i++)
Alan Cox33f0f882006-01-09 20:54:13 -08002259 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 head = (head + len) & rx_mask;
2261 count -= len;
2262 }
2263 writew(head, ofsAddr + RXrptr);
2264 } else {
2265 len = count;
2266 pageno = spage + (head >> 13);
2267 pageofs = head & Page_mask;
2268 do {
2269 cnt = Page_size - pageofs;
2270 if (cnt > count)
2271 cnt = count;
2272 count -= cnt;
2273 writew(pageno, baseAddr + Control_reg);
2274 ofs = baseAddr + DynPage_addr + pageofs;
2275 for (i = 0; i < cnt; i++)
Alan Cox33f0f882006-01-09 20:54:13 -08002276 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 if (count == 0) {
2278 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2279 break;
2280 }
2281 if (++pageno == epage)
2282 pageno = spage;
2283 pageofs = 0;
2284 } while (1);
2285 }
2286 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2287 moxaLowWaterChk = 1;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002288 port->lowChkFlag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 }
2290 return (total);
2291}
2292
2293
Jiri Slabyb4173f42008-04-30 00:53:40 -07002294static int MoxaPortTxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002296 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 ushort rptr, wptr, mask;
2298 int len;
2299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 rptr = readw(ofsAddr + TXrptr);
2301 wptr = readw(ofsAddr + TXwptr);
2302 mask = readw(ofsAddr + TX_mask);
2303 len = (wptr - rptr) & mask;
2304 return (len);
2305}
2306
Jiri Slabyb4173f42008-04-30 00:53:40 -07002307static int MoxaPortTxFree(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002309 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 ushort rptr, wptr, mask;
2311 int len;
2312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 rptr = readw(ofsAddr + TXrptr);
2314 wptr = readw(ofsAddr + TXwptr);
2315 mask = readw(ofsAddr + TX_mask);
2316 len = mask - ((wptr - rptr) & mask);
2317 return (len);
2318}
2319
Jiri Slabyb4173f42008-04-30 00:53:40 -07002320static int MoxaPortRxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002322 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 ushort rptr, wptr, mask;
2324 int len;
2325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 rptr = readw(ofsAddr + RXrptr);
2327 wptr = readw(ofsAddr + RXwptr);
2328 mask = readw(ofsAddr + RX_mask);
2329 len = (wptr - rptr) & mask;
2330 return (len);
2331}
2332
2333
Jiri Slabyb4173f42008-04-30 00:53:40 -07002334static void MoxaPortTxDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002336 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337}
2338
Jiri Slabyb4173f42008-04-30 00:53:40 -07002339static void MoxaPortTxEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002341 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342}
2343
2344
Jiri Slabyb4173f42008-04-30 00:53:40 -07002345static int MoxaPortResetBrkCnt(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
2347 ushort cnt;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002348 cnt = port->breakCnt;
2349 port->breakCnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 return (cnt);
2351}
2352
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002353static int moxa_get_serial_info(struct moxa_port *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 struct serial_struct __user *retinfo)
2355{
2356 struct serial_struct tmp;
2357
2358 memset(&tmp, 0, sizeof(tmp));
2359 tmp.type = info->type;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002360 tmp.line = info->tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 tmp.port = 0;
2362 tmp.irq = 0;
2363 tmp.flags = info->asyncflags;
2364 tmp.baud_base = 921600;
2365 tmp.close_delay = info->close_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 tmp.custom_divisor = 0;
2367 tmp.hub6 = 0;
2368 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2369 return -EFAULT;
2370 return (0);
2371}
2372
2373
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002374static int moxa_set_serial_info(struct moxa_port *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 struct serial_struct __user *new_info)
2376{
2377 struct serial_struct new_serial;
2378
2379 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2380 return -EFAULT;
2381
2382 if ((new_serial.irq != 0) ||
2383 (new_serial.port != 0) ||
2384// (new_serial.type != info->type) ||
2385 (new_serial.custom_divisor != 0) ||
2386 (new_serial.baud_base != 921600))
2387 return (-EPERM);
2388
2389 if (!capable(CAP_SYS_ADMIN)) {
2390 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2391 (info->asyncflags & ~ASYNC_USR_MASK)))
2392 return (-EPERM);
2393 } else {
2394 info->close_delay = new_serial.close_delay * HZ / 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 }
2396
2397 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2398 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2399
2400 if (new_serial.type == PORT_16550A) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07002401 MoxaSetFifo(info, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 } else {
Jiri Slabyb4173f42008-04-30 00:53:40 -07002403 MoxaSetFifo(info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 }
2405
2406 info->type = new_serial.type;
2407 return (0);
2408}
2409
2410
2411
2412/*****************************************************************************
2413 * Static local functions: *
2414 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
Jiri Slaby6f56b6582007-10-18 03:06:24 -07002416static void moxa_low_water_check(void __iomem *ofsAddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
2418 int len;
2419 ushort rptr, wptr, mask;
2420
2421 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2422 rptr = readw(ofsAddr + RXrptr);
2423 wptr = readw(ofsAddr + RXwptr);
2424 mask = readw(ofsAddr + RX_mask);
2425 len = (wptr - rptr) & mask;
2426 if (len <= Low_water)
2427 moxafunc(ofsAddr, FC_SendXon, 0);
2428 }
2429}
2430
Jiri Slabyb4173f42008-04-30 00:53:40 -07002431static void MoxaSetFifo(struct moxa_port *port, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002433 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
2435 if (!enable) {
2436 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2437 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2438 } else {
2439 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2440 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2441 }
2442}