moxa: use tty_port
[pandora-kernel.git] / drivers / char / moxa.c
1 /*****************************************************************************/
2 /*
3  *           moxa.c  -- MOXA Intellio family multiport serial driver.
4  *
5  *      Copyright (C) 1999-2000  Moxa Technologies (support@moxa.com).
6  *      Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
7  *
8  *      This code is loosely based on the Linux serial driver, written by
9  *      Linus Torvalds, Theodore T'so and others.
10  *
11  *      This program is free software; you can redistribute it and/or modify
12  *      it under the terms of the GNU General Public License as published by
13  *      the Free Software Foundation; either version 2 of the License, or
14  *      (at your option) any later version.
15  */
16
17 /*
18  *    MOXA Intellio Series Driver
19  *      for             : LINUX
20  *      date            : 1999/1/7
21  *      version         : 5.1
22  */
23
24 #include <linux/module.h>
25 #include <linux/types.h>
26 #include <linux/mm.h>
27 #include <linux/ioport.h>
28 #include <linux/errno.h>
29 #include <linux/firmware.h>
30 #include <linux/signal.h>
31 #include <linux/sched.h>
32 #include <linux/timer.h>
33 #include <linux/interrupt.h>
34 #include <linux/tty.h>
35 #include <linux/tty_flip.h>
36 #include <linux/major.h>
37 #include <linux/string.h>
38 #include <linux/fcntl.h>
39 #include <linux/ptrace.h>
40 #include <linux/serial.h>
41 #include <linux/tty_driver.h>
42 #include <linux/delay.h>
43 #include <linux/pci.h>
44 #include <linux/init.h>
45 #include <linux/bitops.h>
46
47 #include <asm/system.h>
48 #include <asm/io.h>
49 #include <asm/uaccess.h>
50
51 #include "moxa.h"
52
53 #define MOXA_VERSION            "6.0k"
54
55 #define MOXA_FW_HDRLEN          32
56
57 #define MOXAMAJOR               172
58
59 #define MAX_BOARDS              4       /* Don't change this value */
60 #define MAX_PORTS_PER_BOARD     32      /* Don't change this value */
61 #define MAX_PORTS               (MAX_BOARDS * MAX_PORTS_PER_BOARD)
62
63 #define MOXA_IS_320(brd) ((brd)->boardType == MOXA_BOARD_C320_ISA || \
64                 (brd)->boardType == MOXA_BOARD_C320_PCI)
65
66 /*
67  *    Define the Moxa PCI vendor and device IDs.
68  */
69 #define MOXA_BUS_TYPE_ISA       0
70 #define MOXA_BUS_TYPE_PCI       1
71
72 enum {
73         MOXA_BOARD_C218_PCI = 1,
74         MOXA_BOARD_C218_ISA,
75         MOXA_BOARD_C320_PCI,
76         MOXA_BOARD_C320_ISA,
77         MOXA_BOARD_CP204J,
78 };
79
80 static char *moxa_brdname[] =
81 {
82         "C218 Turbo PCI series",
83         "C218 Turbo ISA series",
84         "C320 Turbo PCI series",
85         "C320 Turbo ISA series",
86         "CP-204J series",
87 };
88
89 #ifdef CONFIG_PCI
90 static struct pci_device_id moxa_pcibrds[] = {
91         { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
92                 .driver_data = MOXA_BOARD_C218_PCI },
93         { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
94                 .driver_data = MOXA_BOARD_C320_PCI },
95         { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
96                 .driver_data = MOXA_BOARD_CP204J },
97         { 0 }
98 };
99 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
100 #endif /* CONFIG_PCI */
101
102 struct moxa_port;
103
104 static struct moxa_board_conf {
105         int boardType;
106         int numPorts;
107         int busType;
108
109         unsigned int ready;
110
111         struct moxa_port *ports;
112
113         void __iomem *basemem;
114         void __iomem *intNdx;
115         void __iomem *intPend;
116         void __iomem *intTable;
117 } moxa_boards[MAX_BOARDS];
118
119 struct mxser_mstatus {
120         tcflag_t cflag;
121         int cts;
122         int dsr;
123         int ri;
124         int dcd;
125 };
126
127 struct moxaq_str {
128         int inq;
129         int outq;
130 };
131
132 struct moxa_port {
133         struct tty_port port;
134         struct moxa_board_conf *board;
135         void __iomem *tableAddr;
136
137         int type;
138         int close_delay;
139         int cflag;
140         unsigned long statusflags;
141
142         u8 DCDState;
143         u8 lineCtrl;
144         u8 lowChkFlag;
145 };
146
147 struct mon_str {
148         int tick;
149         int rxcnt[MAX_PORTS];
150         int txcnt[MAX_PORTS];
151 };
152
153 /* statusflags */
154 #define TXSTOPPED       0x1
155 #define LOWWAIT         0x2
156 #define EMPTYWAIT       0x4
157 #define THROTTLE        0x8
158
159 #define SERIAL_DO_RESTART
160
161 #define WAKEUP_CHARS            256
162
163 static int ttymajor = MOXAMAJOR;
164 static struct mon_str moxaLog;
165 static unsigned int moxaFuncTout = HZ / 2;
166 static unsigned int moxaLowWaterChk;
167 static DEFINE_MUTEX(moxa_openlock);
168 /* Variables for insmod */
169 #ifdef MODULE
170 static unsigned long baseaddr[MAX_BOARDS];
171 static unsigned int type[MAX_BOARDS];
172 static unsigned int numports[MAX_BOARDS];
173 #endif
174
175 MODULE_AUTHOR("William Chen");
176 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
177 MODULE_LICENSE("GPL");
178 #ifdef MODULE
179 module_param_array(type, uint, NULL, 0);
180 MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
181 module_param_array(baseaddr, ulong, NULL, 0);
182 MODULE_PARM_DESC(baseaddr, "base address");
183 module_param_array(numports, uint, NULL, 0);
184 MODULE_PARM_DESC(numports, "numports (ignored for C218)");
185 #endif
186 module_param(ttymajor, int, 0);
187
188 /*
189  * static functions:
190  */
191 static int moxa_open(struct tty_struct *, struct file *);
192 static void moxa_close(struct tty_struct *, struct file *);
193 static int moxa_write(struct tty_struct *, const unsigned char *, int);
194 static int moxa_write_room(struct tty_struct *);
195 static void moxa_flush_buffer(struct tty_struct *);
196 static int moxa_chars_in_buffer(struct tty_struct *);
197 static void moxa_throttle(struct tty_struct *);
198 static void moxa_unthrottle(struct tty_struct *);
199 static void moxa_set_termios(struct tty_struct *, struct ktermios *);
200 static void moxa_stop(struct tty_struct *);
201 static void moxa_start(struct tty_struct *);
202 static void moxa_hangup(struct tty_struct *);
203 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
204 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
205                          unsigned int set, unsigned int clear);
206 static void moxa_poll(unsigned long);
207 static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
208 static void moxa_setup_empty_event(struct tty_struct *);
209 static void moxa_shut_down(struct moxa_port *);
210 /*
211  * moxa board interface functions:
212  */
213 static void MoxaPortEnable(struct moxa_port *);
214 static void MoxaPortDisable(struct moxa_port *);
215 static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
216 static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
217 static void MoxaPortLineCtrl(struct moxa_port *, int, int);
218 static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
219 static int MoxaPortLineStatus(struct moxa_port *);
220 static void MoxaPortFlushData(struct moxa_port *, int);
221 static int MoxaPortWriteData(struct moxa_port *, const unsigned char *, int);
222 static int MoxaPortReadData(struct moxa_port *);
223 static int MoxaPortTxQueue(struct moxa_port *);
224 static int MoxaPortRxQueue(struct moxa_port *);
225 static int MoxaPortTxFree(struct moxa_port *);
226 static void MoxaPortTxDisable(struct moxa_port *);
227 static void MoxaPortTxEnable(struct moxa_port *);
228 static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
229 static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
230 static void MoxaSetFifo(struct moxa_port *port, int enable);
231
232 /*
233  * I/O functions
234  */
235
236 static void moxa_wait_finish(void __iomem *ofsAddr)
237 {
238         unsigned long end = jiffies + moxaFuncTout;
239
240         while (readw(ofsAddr + FuncCode) != 0)
241                 if (time_after(jiffies, end))
242                         return;
243         if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
244                 printk(KERN_WARNING "moxa function expired\n");
245 }
246
247 static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
248 {
249         writew(arg, ofsAddr + FuncArg);
250         writew(cmd, ofsAddr + FuncCode);
251         moxa_wait_finish(ofsAddr);
252 }
253
254 static void moxa_low_water_check(void __iomem *ofsAddr)
255 {
256         u16 rptr, wptr, mask, len;
257
258         if (readb(ofsAddr + FlagStat) & Xoff_state) {
259                 rptr = readw(ofsAddr + RXrptr);
260                 wptr = readw(ofsAddr + RXwptr);
261                 mask = readw(ofsAddr + RX_mask);
262                 len = (wptr - rptr) & mask;
263                 if (len <= Low_water)
264                         moxafunc(ofsAddr, FC_SendXon, 0);
265         }
266 }
267
268 /*
269  * TTY operations
270  */
271
272 static 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, ret = 0;
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                 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
290                         ret = -EFAULT;
291                 break;
292         case MOXA_FLUSH_QUEUE:
293                 MoxaPortFlushData(ch, arg);
294                 break;
295         case MOXA_GET_IOQUEUE: {
296                 struct moxaq_str __user *argm = argp;
297                 struct moxaq_str tmp;
298                 struct moxa_port *p;
299                 unsigned int i, j;
300
301                 mutex_lock(&moxa_openlock);
302                 for (i = 0; i < MAX_BOARDS; i++) {
303                         p = moxa_boards[i].ports;
304                         for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
305                                 memset(&tmp, 0, sizeof(tmp));
306                                 if (moxa_boards[i].ready) {
307                                         tmp.inq = MoxaPortRxQueue(p);
308                                         tmp.outq = MoxaPortTxQueue(p);
309                                 }
310                                 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
311                                         mutex_unlock(&moxa_openlock);
312                                         return -EFAULT;
313                                 }
314                         }
315                 }
316                 mutex_unlock(&moxa_openlock);
317                 break;
318         } case MOXA_GET_OQUEUE:
319                 status = MoxaPortTxQueue(ch);
320                 ret = put_user(status, (unsigned long __user *)argp);
321                 break;
322         case MOXA_GET_IQUEUE:
323                 status = MoxaPortRxQueue(ch);
324                 ret = put_user(status, (unsigned long __user *)argp);
325                 break;
326         case MOXA_GETMSTATUS: {
327                 struct mxser_mstatus __user *argm = argp;
328                 struct mxser_mstatus tmp;
329                 struct moxa_port *p;
330                 unsigned int i, j;
331
332                 mutex_lock(&moxa_openlock);
333                 for (i = 0; i < MAX_BOARDS; i++) {
334                         p = moxa_boards[i].ports;
335                         for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
336                                 memset(&tmp, 0, sizeof(tmp));
337                                 if (!moxa_boards[i].ready)
338                                         goto copy;
339
340                                 status = MoxaPortLineStatus(p);
341                                 if (status & 1)
342                                         tmp.cts = 1;
343                                 if (status & 2)
344                                         tmp.dsr = 1;
345                                 if (status & 4)
346                                         tmp.dcd = 1;
347
348                                 if (!p->port.tty || !p->port.tty->termios)
349                                         tmp.cflag = p->cflag;
350                                 else
351                                         tmp.cflag = p->port.tty->termios->c_cflag;
352 copy:
353                                 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
354                                         mutex_unlock(&moxa_openlock);
355                                         return -EFAULT;
356                                 }
357                         }
358                 }
359                 mutex_unlock(&moxa_openlock);
360                 break;
361         }
362         case TIOCGSERIAL:
363                 mutex_lock(&moxa_openlock);
364                 ret = moxa_get_serial_info(ch, argp);
365                 mutex_unlock(&moxa_openlock);
366                 break;
367         case TIOCSSERIAL:
368                 mutex_lock(&moxa_openlock);
369                 ret = moxa_set_serial_info(ch, argp);
370                 mutex_unlock(&moxa_openlock);
371                 break;
372         default:
373                 ret = -ENOIOCTLCMD;
374         }
375         return ret;
376 }
377
378 static void moxa_break_ctl(struct tty_struct *tty, int state)
379 {
380         struct moxa_port *port = tty->driver_data;
381
382         moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
383                         Magic_code);
384 }
385
386 static const struct tty_operations moxa_ops = {
387         .open = moxa_open,
388         .close = moxa_close,
389         .write = moxa_write,
390         .write_room = moxa_write_room,
391         .flush_buffer = moxa_flush_buffer,
392         .chars_in_buffer = moxa_chars_in_buffer,
393         .ioctl = moxa_ioctl,
394         .throttle = moxa_throttle,
395         .unthrottle = moxa_unthrottle,
396         .set_termios = moxa_set_termios,
397         .stop = moxa_stop,
398         .start = moxa_start,
399         .hangup = moxa_hangup,
400         .break_ctl = moxa_break_ctl,
401         .tiocmget = moxa_tiocmget,
402         .tiocmset = moxa_tiocmset,
403 };
404
405 static struct tty_driver *moxaDriver;
406 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
407 static DEFINE_SPINLOCK(moxa_lock);
408
409 /*
410  * HW init
411  */
412
413 static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
414 {
415         switch (brd->boardType) {
416         case MOXA_BOARD_C218_ISA:
417         case MOXA_BOARD_C218_PCI:
418                 if (model != 1)
419                         goto err;
420                 break;
421         case MOXA_BOARD_CP204J:
422                 if (model != 3)
423                         goto err;
424                 break;
425         default:
426                 if (model != 2)
427                         goto err;
428                 break;
429         }
430         return 0;
431 err:
432         return -EINVAL;
433 }
434
435 static int moxa_check_fw(const void *ptr)
436 {
437         const __le16 *lptr = ptr;
438
439         if (*lptr != cpu_to_le16(0x7980))
440                 return -EINVAL;
441
442         return 0;
443 }
444
445 static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
446                 size_t len)
447 {
448         void __iomem *baseAddr = brd->basemem;
449         u16 tmp;
450
451         writeb(HW_reset, baseAddr + Control_reg);       /* reset */
452         msleep(10);
453         memset_io(baseAddr, 0, 4096);
454         memcpy_toio(baseAddr, buf, len);        /* download BIOS */
455         writeb(0, baseAddr + Control_reg);      /* restart */
456
457         msleep(2000);
458
459         switch (brd->boardType) {
460         case MOXA_BOARD_C218_ISA:
461         case MOXA_BOARD_C218_PCI:
462                 tmp = readw(baseAddr + C218_key);
463                 if (tmp != C218_KeyCode)
464                         goto err;
465                 break;
466         case MOXA_BOARD_CP204J:
467                 tmp = readw(baseAddr + C218_key);
468                 if (tmp != CP204J_KeyCode)
469                         goto err;
470                 break;
471         default:
472                 tmp = readw(baseAddr + C320_key);
473                 if (tmp != C320_KeyCode)
474                         goto err;
475                 tmp = readw(baseAddr + C320_status);
476                 if (tmp != STS_init) {
477                         printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
478                                         "module not found\n");
479                         return -EIO;
480                 }
481                 break;
482         }
483
484         return 0;
485 err:
486         printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
487         return -EIO;
488 }
489
490 static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
491                 size_t len)
492 {
493         void __iomem *baseAddr = brd->basemem;
494
495         if (len < 7168) {
496                 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
497                 return -EINVAL;
498         }
499
500         writew(len - 7168 - 2, baseAddr + C320bapi_len);
501         writeb(1, baseAddr + Control_reg);      /* Select Page 1 */
502         memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
503         writeb(2, baseAddr + Control_reg);      /* Select Page 2 */
504         memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
505
506         return 0;
507 }
508
509 static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
510                 size_t len)
511 {
512         void __iomem *baseAddr = brd->basemem;
513         const u16 *uptr = ptr;
514         size_t wlen, len2, j;
515         unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
516         unsigned int i, retry;
517         u16 usum, keycode;
518
519         keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
520                                 C218_KeyCode;
521
522         switch (brd->boardType) {
523         case MOXA_BOARD_CP204J:
524         case MOXA_BOARD_C218_ISA:
525         case MOXA_BOARD_C218_PCI:
526                 key = C218_key;
527                 loadbuf = C218_LoadBuf;
528                 loadlen = C218DLoad_len;
529                 checksum = C218check_sum;
530                 checksum_ok = C218chksum_ok;
531                 break;
532         default:
533                 key = C320_key;
534                 keycode = C320_KeyCode;
535                 loadbuf = C320_LoadBuf;
536                 loadlen = C320DLoad_len;
537                 checksum = C320check_sum;
538                 checksum_ok = C320chksum_ok;
539                 break;
540         }
541
542         usum = 0;
543         wlen = len >> 1;
544         for (i = 0; i < wlen; i++)
545                 usum += le16_to_cpu(uptr[i]);
546         retry = 0;
547         do {
548                 wlen = len >> 1;
549                 j = 0;
550                 while (wlen) {
551                         len2 = (wlen > 2048) ? 2048 : wlen;
552                         wlen -= len2;
553                         memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
554                         j += len2 << 1;
555
556                         writew(len2, baseAddr + loadlen);
557                         writew(0, baseAddr + key);
558                         for (i = 0; i < 100; i++) {
559                                 if (readw(baseAddr + key) == keycode)
560                                         break;
561                                 msleep(10);
562                         }
563                         if (readw(baseAddr + key) != keycode)
564                                 return -EIO;
565                 }
566                 writew(0, baseAddr + loadlen);
567                 writew(usum, baseAddr + checksum);
568                 writew(0, baseAddr + key);
569                 for (i = 0; i < 100; i++) {
570                         if (readw(baseAddr + key) == keycode)
571                                 break;
572                         msleep(10);
573                 }
574                 retry++;
575         } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
576         if (readb(baseAddr + checksum_ok) != 1)
577                 return -EIO;
578
579         writew(0, baseAddr + key);
580         for (i = 0; i < 600; i++) {
581                 if (readw(baseAddr + Magic_no) == Magic_code)
582                         break;
583                 msleep(10);
584         }
585         if (readw(baseAddr + Magic_no) != Magic_code)
586                 return -EIO;
587
588         if (MOXA_IS_320(brd)) {
589                 if (brd->busType == MOXA_BUS_TYPE_PCI) {        /* ASIC board */
590                         writew(0x3800, baseAddr + TMS320_PORT1);
591                         writew(0x3900, baseAddr + TMS320_PORT2);
592                         writew(28499, baseAddr + TMS320_CLOCK);
593                 } else {
594                         writew(0x3200, baseAddr + TMS320_PORT1);
595                         writew(0x3400, baseAddr + TMS320_PORT2);
596                         writew(19999, baseAddr + TMS320_CLOCK);
597                 }
598         }
599         writew(1, baseAddr + Disable_IRQ);
600         writew(0, baseAddr + Magic_no);
601         for (i = 0; i < 500; i++) {
602                 if (readw(baseAddr + Magic_no) == Magic_code)
603                         break;
604                 msleep(10);
605         }
606         if (readw(baseAddr + Magic_no) != Magic_code)
607                 return -EIO;
608
609         if (MOXA_IS_320(brd)) {
610                 j = readw(baseAddr + Module_cnt);
611                 if (j <= 0)
612                         return -EIO;
613                 brd->numPorts = j * 8;
614                 writew(j, baseAddr + Module_no);
615                 writew(0, baseAddr + Magic_no);
616                 for (i = 0; i < 600; i++) {
617                         if (readw(baseAddr + Magic_no) == Magic_code)
618                                 break;
619                         msleep(10);
620                 }
621                 if (readw(baseAddr + Magic_no) != Magic_code)
622                         return -EIO;
623         }
624         brd->intNdx = baseAddr + IRQindex;
625         brd->intPend = baseAddr + IRQpending;
626         brd->intTable = baseAddr + IRQtable;
627
628         return 0;
629 }
630
631 static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
632                 size_t len)
633 {
634         void __iomem *ofsAddr, *baseAddr = brd->basemem;
635         struct moxa_port *port;
636         int retval, i;
637
638         if (len % 2) {
639                 printk(KERN_ERR "MOXA: bios length is not even\n");
640                 return -EINVAL;
641         }
642
643         retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
644         if (retval)
645                 return retval;
646
647         switch (brd->boardType) {
648         case MOXA_BOARD_C218_ISA:
649         case MOXA_BOARD_C218_PCI:
650         case MOXA_BOARD_CP204J:
651                 port = brd->ports;
652                 for (i = 0; i < brd->numPorts; i++, port++) {
653                         port->board = brd;
654                         port->DCDState = 0;
655                         port->tableAddr = baseAddr + Extern_table +
656                                         Extern_size * i;
657                         ofsAddr = port->tableAddr;
658                         writew(C218rx_mask, ofsAddr + RX_mask);
659                         writew(C218tx_mask, ofsAddr + TX_mask);
660                         writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
661                         writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
662
663                         writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
664                         writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
665
666                 }
667                 break;
668         default:
669                 port = brd->ports;
670                 for (i = 0; i < brd->numPorts; i++, port++) {
671                         port->board = brd;
672                         port->DCDState = 0;
673                         port->tableAddr = baseAddr + Extern_table +
674                                         Extern_size * i;
675                         ofsAddr = port->tableAddr;
676                         switch (brd->numPorts) {
677                         case 8:
678                                 writew(C320p8rx_mask, ofsAddr + RX_mask);
679                                 writew(C320p8tx_mask, ofsAddr + TX_mask);
680                                 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
681                                 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
682                                 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
683                                 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
684
685                                 break;
686                         case 16:
687                                 writew(C320p16rx_mask, ofsAddr + RX_mask);
688                                 writew(C320p16tx_mask, ofsAddr + TX_mask);
689                                 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
690                                 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
691                                 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
692                                 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
693                                 break;
694
695                         case 24:
696                                 writew(C320p24rx_mask, ofsAddr + RX_mask);
697                                 writew(C320p24tx_mask, ofsAddr + TX_mask);
698                                 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
699                                 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
700                                 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
701                                 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
702                                 break;
703                         case 32:
704                                 writew(C320p32rx_mask, ofsAddr + RX_mask);
705                                 writew(C320p32tx_mask, ofsAddr + TX_mask);
706                                 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
707                                 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
708                                 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
709                                 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
710                                 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
711                                 break;
712                         }
713                 }
714                 break;
715         }
716         return 0;
717 }
718
719 static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
720 {
721         const void *ptr = fw->data;
722         char rsn[64];
723         u16 lens[5];
724         size_t len;
725         unsigned int a, lenp, lencnt;
726         int ret = -EINVAL;
727         struct {
728                 __le32 magic;   /* 0x34303430 */
729                 u8 reserved1[2];
730                 u8 type;        /* UNIX = 3 */
731                 u8 model;       /* C218T=1, C320T=2, CP204=3 */
732                 u8 reserved2[8];
733                 __le16 len[5];
734         } const *hdr = ptr;
735
736         BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
737
738         if (fw->size < MOXA_FW_HDRLEN) {
739                 strcpy(rsn, "too short (even header won't fit)");
740                 goto err;
741         }
742         if (hdr->magic != cpu_to_le32(0x30343034)) {
743                 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
744                 goto err;
745         }
746         if (hdr->type != 3) {
747                 sprintf(rsn, "not for linux, type is %u", hdr->type);
748                 goto err;
749         }
750         if (moxa_check_fw_model(brd, hdr->model)) {
751                 sprintf(rsn, "not for this card, model is %u", hdr->model);
752                 goto err;
753         }
754
755         len = MOXA_FW_HDRLEN;
756         lencnt = hdr->model == 2 ? 5 : 3;
757         for (a = 0; a < ARRAY_SIZE(lens); a++) {
758                 lens[a] = le16_to_cpu(hdr->len[a]);
759                 if (lens[a] && len + lens[a] <= fw->size &&
760                                 moxa_check_fw(&fw->data[len]))
761                         printk(KERN_WARNING "MOXA firmware: unexpected input "
762                                 "at offset %u, but going on\n", (u32)len);
763                 if (!lens[a] && a < lencnt) {
764                         sprintf(rsn, "too few entries in fw file");
765                         goto err;
766                 }
767                 len += lens[a];
768         }
769
770         if (len != fw->size) {
771                 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
772                                 (u32)len);
773                 goto err;
774         }
775
776         ptr += MOXA_FW_HDRLEN;
777         lenp = 0; /* bios */
778
779         strcpy(rsn, "read above");
780
781         ret = moxa_load_bios(brd, ptr, lens[lenp]);
782         if (ret)
783                 goto err;
784
785         /* we skip the tty section (lens[1]), since we don't need it */
786         ptr += lens[lenp] + lens[lenp + 1];
787         lenp += 2; /* comm */
788
789         if (hdr->model == 2) {
790                 ret = moxa_load_320b(brd, ptr, lens[lenp]);
791                 if (ret)
792                         goto err;
793                 /* skip another tty */
794                 ptr += lens[lenp] + lens[lenp + 1];
795                 lenp += 2;
796         }
797
798         ret = moxa_load_code(brd, ptr, lens[lenp]);
799         if (ret)
800                 goto err;
801
802         return 0;
803 err:
804         printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
805         return ret;
806 }
807
808 static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
809 {
810         const struct firmware *fw;
811         const char *file;
812         struct moxa_port *p;
813         unsigned int i;
814         int ret;
815
816         brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
817                         GFP_KERNEL);
818         if (brd->ports == NULL) {
819                 printk(KERN_ERR "cannot allocate memory for ports\n");
820                 ret = -ENOMEM;
821                 goto err;
822         }
823
824         for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
825                 p->type = PORT_16550A;
826                 p->close_delay = 5 * HZ / 10;
827                 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
828                 tty_port_init(&p->port);
829         }
830
831         switch (brd->boardType) {
832         case MOXA_BOARD_C218_ISA:
833         case MOXA_BOARD_C218_PCI:
834                 file = "c218tunx.cod";
835                 break;
836         case MOXA_BOARD_CP204J:
837                 file = "cp204unx.cod";
838                 break;
839         default:
840                 file = "c320tunx.cod";
841                 break;
842         }
843
844         ret = request_firmware(&fw, file, dev);
845         if (ret) {
846                 printk(KERN_ERR "MOXA: request_firmware failed. Make sure "
847                                 "you've placed '%s' file into your firmware "
848                                 "loader directory (e.g. /lib/firmware)\n",
849                                 file);
850                 goto err_free;
851         }
852
853         ret = moxa_load_fw(brd, fw);
854
855         release_firmware(fw);
856
857         if (ret)
858                 goto err_free;
859
860         spin_lock_bh(&moxa_lock);
861         brd->ready = 1;
862         if (!timer_pending(&moxaTimer))
863                 mod_timer(&moxaTimer, jiffies + HZ / 50);
864         spin_unlock_bh(&moxa_lock);
865
866         return 0;
867 err_free:
868         kfree(brd->ports);
869 err:
870         return ret;
871 }
872
873 static void moxa_board_deinit(struct moxa_board_conf *brd)
874 {
875         unsigned int a, opened;
876
877         mutex_lock(&moxa_openlock);
878         spin_lock_bh(&moxa_lock);
879         brd->ready = 0;
880         spin_unlock_bh(&moxa_lock);
881
882         /* pci hot-un-plug support */
883         for (a = 0; a < brd->numPorts; a++)
884                 if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
885                         tty_hangup(brd->ports[a].port.tty);
886         while (1) {
887                 opened = 0;
888                 for (a = 0; a < brd->numPorts; a++)
889                         if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
890                                 opened++;
891                 mutex_unlock(&moxa_openlock);
892                 if (!opened)
893                         break;
894                 msleep(50);
895                 mutex_lock(&moxa_openlock);
896         }
897
898         iounmap(brd->basemem);
899         brd->basemem = NULL;
900         kfree(brd->ports);
901 }
902
903 #ifdef CONFIG_PCI
904 static int __devinit moxa_pci_probe(struct pci_dev *pdev,
905                 const struct pci_device_id *ent)
906 {
907         struct moxa_board_conf *board;
908         unsigned int i;
909         int board_type = ent->driver_data;
910         int retval;
911
912         retval = pci_enable_device(pdev);
913         if (retval) {
914                 dev_err(&pdev->dev, "can't enable pci device\n");
915                 goto err;
916         }
917
918         for (i = 0; i < MAX_BOARDS; i++)
919                 if (moxa_boards[i].basemem == NULL)
920                         break;
921
922         retval = -ENODEV;
923         if (i >= MAX_BOARDS) {
924                 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
925                                 "found. Board is ignored.\n", MAX_BOARDS);
926                 goto err;
927         }
928
929         board = &moxa_boards[i];
930
931         retval = pci_request_region(pdev, 2, "moxa-base");
932         if (retval) {
933                 dev_err(&pdev->dev, "can't request pci region 2\n");
934                 goto err;
935         }
936
937         board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000);
938         if (board->basemem == NULL) {
939                 dev_err(&pdev->dev, "can't remap io space 2\n");
940                 goto err_reg;
941         }
942
943         board->boardType = board_type;
944         switch (board_type) {
945         case MOXA_BOARD_C218_ISA:
946         case MOXA_BOARD_C218_PCI:
947                 board->numPorts = 8;
948                 break;
949
950         case MOXA_BOARD_CP204J:
951                 board->numPorts = 4;
952                 break;
953         default:
954                 board->numPorts = 0;
955                 break;
956         }
957         board->busType = MOXA_BUS_TYPE_PCI;
958
959         retval = moxa_init_board(board, &pdev->dev);
960         if (retval)
961                 goto err_base;
962
963         pci_set_drvdata(pdev, board);
964
965         dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n",
966                         moxa_brdname[board_type - 1], board->numPorts);
967
968         return 0;
969 err_base:
970         iounmap(board->basemem);
971         board->basemem = NULL;
972 err_reg:
973         pci_release_region(pdev, 2);
974 err:
975         return retval;
976 }
977
978 static void __devexit moxa_pci_remove(struct pci_dev *pdev)
979 {
980         struct moxa_board_conf *brd = pci_get_drvdata(pdev);
981
982         moxa_board_deinit(brd);
983
984         pci_release_region(pdev, 2);
985 }
986
987 static struct pci_driver moxa_pci_driver = {
988         .name = "moxa",
989         .id_table = moxa_pcibrds,
990         .probe = moxa_pci_probe,
991         .remove = __devexit_p(moxa_pci_remove)
992 };
993 #endif /* CONFIG_PCI */
994
995 static int __init moxa_init(void)
996 {
997         unsigned int isabrds = 0;
998         int retval = 0;
999
1000         printk(KERN_INFO "MOXA Intellio family driver version %s\n",
1001                         MOXA_VERSION);
1002         moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
1003         if (!moxaDriver)
1004                 return -ENOMEM;
1005
1006         moxaDriver->owner = THIS_MODULE;
1007         moxaDriver->name = "ttyMX";
1008         moxaDriver->major = ttymajor;
1009         moxaDriver->minor_start = 0;
1010         moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1011         moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1012         moxaDriver->init_termios = tty_std_termios;
1013         moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
1014         moxaDriver->init_termios.c_ispeed = 9600;
1015         moxaDriver->init_termios.c_ospeed = 9600;
1016         moxaDriver->flags = TTY_DRIVER_REAL_RAW;
1017         tty_set_operations(moxaDriver, &moxa_ops);
1018
1019         if (tty_register_driver(moxaDriver)) {
1020                 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
1021                 put_tty_driver(moxaDriver);
1022                 return -1;
1023         }
1024
1025         /* Find the boards defined from module args. */
1026 #ifdef MODULE
1027         {
1028         struct moxa_board_conf *brd = moxa_boards;
1029         unsigned int i;
1030         for (i = 0; i < MAX_BOARDS; i++) {
1031                 if (!baseaddr[i])
1032                         break;
1033                 if (type[i] == MOXA_BOARD_C218_ISA ||
1034                                 type[i] == MOXA_BOARD_C320_ISA) {
1035                         pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
1036                                         isabrds + 1, moxa_brdname[type[i] - 1],
1037                                         baseaddr[i]);
1038                         brd->boardType = type[i];
1039                         brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1040                                         numports[i];
1041                         brd->busType = MOXA_BUS_TYPE_ISA;
1042                         brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
1043                         if (!brd->basemem) {
1044                                 printk(KERN_ERR "MOXA: can't remap %lx\n",
1045                                                 baseaddr[i]);
1046                                 continue;
1047                         }
1048                         if (moxa_init_board(brd, NULL)) {
1049                                 iounmap(brd->basemem);
1050                                 brd->basemem = NULL;
1051                                 continue;
1052                         }
1053
1054                         printk(KERN_INFO "MOXA isa board found at 0x%.8lu and "
1055                                         "ready (%u ports, firmware loaded)\n",
1056                                         baseaddr[i], brd->numPorts);
1057
1058                         brd++;
1059                         isabrds++;
1060                 }
1061         }
1062         }
1063 #endif
1064
1065 #ifdef CONFIG_PCI
1066         retval = pci_register_driver(&moxa_pci_driver);
1067         if (retval) {
1068                 printk(KERN_ERR "Can't register MOXA pci driver!\n");
1069                 if (isabrds)
1070                         retval = 0;
1071         }
1072 #endif
1073
1074         return retval;
1075 }
1076
1077 static void __exit moxa_exit(void)
1078 {
1079         unsigned int i;
1080
1081 #ifdef CONFIG_PCI
1082         pci_unregister_driver(&moxa_pci_driver);
1083 #endif
1084
1085         for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1086                 if (moxa_boards[i].ready)
1087                         moxa_board_deinit(&moxa_boards[i]);
1088
1089         del_timer_sync(&moxaTimer);
1090
1091         if (tty_unregister_driver(moxaDriver))
1092                 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1093                                 "serial driver\n");
1094         put_tty_driver(moxaDriver);
1095 }
1096
1097 module_init(moxa_init);
1098 module_exit(moxa_exit);
1099
1100 static void moxa_close_port(struct moxa_port *ch)
1101 {
1102         moxa_shut_down(ch);
1103         MoxaPortFlushData(ch, 2);
1104         ch->port.flags &= ~ASYNC_NORMAL_ACTIVE;
1105         ch->port.tty->driver_data = NULL;
1106         ch->port.tty = NULL;
1107 }
1108
1109 static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
1110                             struct moxa_port *ch)
1111 {
1112         DEFINE_WAIT(wait);
1113         int retval = 0;
1114         u8 dcd;
1115
1116         while (1) {
1117                 prepare_to_wait(&ch->port.open_wait, &wait, TASK_INTERRUPTIBLE);
1118                 if (tty_hung_up_p(filp)) {
1119 #ifdef SERIAL_DO_RESTART
1120                         retval = -ERESTARTSYS;
1121 #else
1122                         retval = -EAGAIN;
1123 #endif
1124                         break;
1125                 }
1126                 spin_lock_bh(&moxa_lock);
1127                 dcd = ch->DCDState;
1128                 spin_unlock_bh(&moxa_lock);
1129                 if (dcd)
1130                         break;
1131
1132                 if (signal_pending(current)) {
1133                         retval = -ERESTARTSYS;
1134                         break;
1135                 }
1136                 schedule();
1137         }
1138         finish_wait(&ch->port.open_wait, &wait);
1139
1140         return retval;
1141 }
1142
1143 static int moxa_open(struct tty_struct *tty, struct file *filp)
1144 {
1145         struct moxa_board_conf *brd;
1146         struct moxa_port *ch;
1147         int port;
1148         int retval;
1149
1150         port = tty->index;
1151         if (port == MAX_PORTS) {
1152                 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
1153         }
1154         if (mutex_lock_interruptible(&moxa_openlock))
1155                 return -ERESTARTSYS;
1156         brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
1157         if (!brd->ready) {
1158                 mutex_unlock(&moxa_openlock);
1159                 return -ENODEV;
1160         }
1161
1162         ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
1163         ch->port.count++;
1164         tty->driver_data = ch;
1165         ch->port.tty = tty;
1166         if (!(ch->port.flags & ASYNC_INITIALIZED)) {
1167                 ch->statusflags = 0;
1168                 moxa_set_tty_param(tty, tty->termios);
1169                 MoxaPortLineCtrl(ch, 1, 1);
1170                 MoxaPortEnable(ch);
1171                 MoxaSetFifo(ch, ch->type == PORT_16550A);
1172                 ch->port.flags |= ASYNC_INITIALIZED;
1173         }
1174         mutex_unlock(&moxa_openlock);
1175
1176         retval = 0;
1177         if (!(filp->f_flags & O_NONBLOCK) && !C_CLOCAL(tty))
1178                 retval = moxa_block_till_ready(tty, filp, ch);
1179         mutex_lock(&moxa_openlock);
1180         if (retval) {
1181                 if (ch->port.count) /* 0 means already hung up... */
1182                         if (--ch->port.count == 0)
1183                                 moxa_close_port(ch);
1184         } else
1185                 ch->port.flags |= ASYNC_NORMAL_ACTIVE;
1186         mutex_unlock(&moxa_openlock);
1187
1188         return retval;
1189 }
1190
1191 static void moxa_close(struct tty_struct *tty, struct file *filp)
1192 {
1193         struct moxa_port *ch;
1194         int port;
1195
1196         port = tty->index;
1197         if (port == MAX_PORTS || tty_hung_up_p(filp))
1198                 return;
1199
1200         mutex_lock(&moxa_openlock);
1201         ch = tty->driver_data;
1202         if (ch == NULL)
1203                 goto unlock;
1204         if (tty->count == 1 && ch->port.count != 1) {
1205                 printk(KERN_WARNING "moxa_close: bad serial port count; "
1206                         "tty->count is 1, ch->port.count is %d\n", ch->port.count);
1207                 ch->port.count = 1;
1208         }
1209         if (--ch->port.count < 0) {
1210                 printk(KERN_WARNING "moxa_close: bad serial port count, "
1211                         "device=%s\n", tty->name);
1212                 ch->port.count = 0;
1213         }
1214         if (ch->port.count)
1215                 goto unlock;
1216
1217         ch->cflag = tty->termios->c_cflag;
1218         if (ch->port.flags & ASYNC_INITIALIZED) {
1219                 moxa_setup_empty_event(tty);
1220                 tty_wait_until_sent(tty, 30 * HZ);      /* 30 seconds timeout */
1221         }
1222
1223         moxa_close_port(ch);
1224 unlock:
1225         mutex_unlock(&moxa_openlock);
1226 }
1227
1228 static int moxa_write(struct tty_struct *tty,
1229                       const unsigned char *buf, int count)
1230 {
1231         struct moxa_port *ch = tty->driver_data;
1232         int len;
1233
1234         if (ch == NULL)
1235                 return 0;
1236
1237         spin_lock_bh(&moxa_lock);
1238         len = MoxaPortWriteData(ch, buf, count);
1239         spin_unlock_bh(&moxa_lock);
1240
1241         ch->statusflags |= LOWWAIT;
1242         return len;
1243 }
1244
1245 static int moxa_write_room(struct tty_struct *tty)
1246 {
1247         struct moxa_port *ch;
1248
1249         if (tty->stopped)
1250                 return 0;
1251         ch = tty->driver_data;
1252         if (ch == NULL)
1253                 return 0;
1254         return MoxaPortTxFree(ch);
1255 }
1256
1257 static void moxa_flush_buffer(struct tty_struct *tty)
1258 {
1259         struct moxa_port *ch = tty->driver_data;
1260
1261         if (ch == NULL)
1262                 return;
1263         MoxaPortFlushData(ch, 1);
1264         tty_wakeup(tty);
1265 }
1266
1267 static int moxa_chars_in_buffer(struct tty_struct *tty)
1268 {
1269         struct moxa_port *ch = tty->driver_data;
1270         int chars;
1271
1272         /*
1273          * Sigh...I have to check if driver_data is NULL here, because
1274          * if an open() fails, the TTY subsystem eventually calls
1275          * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1276          * routine.  And since the open() failed, we return 0 here.  TDJ
1277          */
1278         if (ch == NULL)
1279                 return 0;
1280         lock_kernel();
1281         chars = MoxaPortTxQueue(ch);
1282         if (chars) {
1283                 /*
1284                  * Make it possible to wakeup anything waiting for output
1285                  * in tty_ioctl.c, etc.
1286                  */
1287                 if (!(ch->statusflags & EMPTYWAIT))
1288                         moxa_setup_empty_event(tty);
1289         }
1290         unlock_kernel();
1291         return chars;
1292 }
1293
1294 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1295 {
1296         struct moxa_port *ch;
1297         int flag = 0, dtr, rts;
1298
1299         mutex_lock(&moxa_openlock);
1300         ch = tty->driver_data;
1301         if (!ch) {
1302                 mutex_unlock(&moxa_openlock);
1303                 return -EINVAL;
1304         }
1305
1306         MoxaPortGetLineOut(ch, &dtr, &rts);
1307         if (dtr)
1308                 flag |= TIOCM_DTR;
1309         if (rts)
1310                 flag |= TIOCM_RTS;
1311         dtr = MoxaPortLineStatus(ch);
1312         if (dtr & 1)
1313                 flag |= TIOCM_CTS;
1314         if (dtr & 2)
1315                 flag |= TIOCM_DSR;
1316         if (dtr & 4)
1317                 flag |= TIOCM_CD;
1318         mutex_unlock(&moxa_openlock);
1319         return flag;
1320 }
1321
1322 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1323                          unsigned int set, unsigned int clear)
1324 {
1325         struct moxa_port *ch;
1326         int port;
1327         int dtr, rts;
1328
1329         port = tty->index;
1330         mutex_lock(&moxa_openlock);
1331         ch = tty->driver_data;
1332         if (!ch) {
1333                 mutex_unlock(&moxa_openlock);
1334                 return -EINVAL;
1335         }
1336
1337         MoxaPortGetLineOut(ch, &dtr, &rts);
1338         if (set & TIOCM_RTS)
1339                 rts = 1;
1340         if (set & TIOCM_DTR)
1341                 dtr = 1;
1342         if (clear & TIOCM_RTS)
1343                 rts = 0;
1344         if (clear & TIOCM_DTR)
1345                 dtr = 0;
1346         MoxaPortLineCtrl(ch, dtr, rts);
1347         mutex_unlock(&moxa_openlock);
1348         return 0;
1349 }
1350
1351 static void moxa_throttle(struct tty_struct *tty)
1352 {
1353         struct moxa_port *ch = tty->driver_data;
1354
1355         ch->statusflags |= THROTTLE;
1356 }
1357
1358 static void moxa_unthrottle(struct tty_struct *tty)
1359 {
1360         struct moxa_port *ch = tty->driver_data;
1361
1362         ch->statusflags &= ~THROTTLE;
1363 }
1364
1365 static void moxa_set_termios(struct tty_struct *tty,
1366                 struct ktermios *old_termios)
1367 {
1368         struct moxa_port *ch = tty->driver_data;
1369
1370         if (ch == NULL)
1371                 return;
1372         moxa_set_tty_param(tty, old_termios);
1373         if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
1374                 wake_up_interruptible(&ch->port.open_wait);
1375 }
1376
1377 static void moxa_stop(struct tty_struct *tty)
1378 {
1379         struct moxa_port *ch = tty->driver_data;
1380
1381         if (ch == NULL)
1382                 return;
1383         MoxaPortTxDisable(ch);
1384         ch->statusflags |= TXSTOPPED;
1385 }
1386
1387
1388 static void moxa_start(struct tty_struct *tty)
1389 {
1390         struct moxa_port *ch = tty->driver_data;
1391
1392         if (ch == NULL)
1393                 return;
1394
1395         if (!(ch->statusflags & TXSTOPPED))
1396                 return;
1397
1398         MoxaPortTxEnable(ch);
1399         ch->statusflags &= ~TXSTOPPED;
1400 }
1401
1402 static void moxa_hangup(struct tty_struct *tty)
1403 {
1404         struct moxa_port *ch;
1405
1406         mutex_lock(&moxa_openlock);
1407         ch = tty->driver_data;
1408         if (ch == NULL) {
1409                 mutex_unlock(&moxa_openlock);
1410                 return;
1411         }
1412         ch->port.count = 0;
1413         moxa_close_port(ch);
1414         mutex_unlock(&moxa_openlock);
1415
1416         wake_up_interruptible(&ch->port.open_wait);
1417 }
1418
1419 static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1420 {
1421         dcd = !!dcd;
1422
1423         if (dcd != p->DCDState && p->port.tty && C_CLOCAL(p->port.tty)) {
1424                 if (!dcd)
1425                         tty_hangup(p->port.tty);
1426         }
1427         p->DCDState = dcd;
1428 }
1429
1430 static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1431                 u16 __iomem *ip)
1432 {
1433         struct tty_struct *tty = p->port.tty;
1434         void __iomem *ofsAddr;
1435         unsigned int inited = p->port.flags & ASYNC_INITIALIZED;
1436         u16 intr;
1437
1438         if (tty) {
1439                 if ((p->statusflags & EMPTYWAIT) &&
1440                                 MoxaPortTxQueue(p) == 0) {
1441                         p->statusflags &= ~EMPTYWAIT;
1442                         tty_wakeup(tty);
1443                 }
1444                 if ((p->statusflags & LOWWAIT) && !tty->stopped &&
1445                                 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1446                         p->statusflags &= ~LOWWAIT;
1447                         tty_wakeup(tty);
1448                 }
1449
1450                 if (inited && !(p->statusflags & THROTTLE) &&
1451                                 MoxaPortRxQueue(p) > 0) { /* RX */
1452                         MoxaPortReadData(p);
1453                         tty_schedule_flip(tty);
1454                 }
1455         } else {
1456                 p->statusflags &= ~EMPTYWAIT;
1457                 MoxaPortFlushData(p, 0); /* flush RX */
1458         }
1459
1460         if (!handle) /* nothing else to do */
1461                 return 0;
1462
1463         intr = readw(ip); /* port irq status */
1464         if (intr == 0)
1465                 return 0;
1466
1467         writew(0, ip); /* ACK port */
1468         ofsAddr = p->tableAddr;
1469         if (intr & IntrTx) /* disable tx intr */
1470                 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1471                                 ofsAddr + HostStat);
1472
1473         if (!inited)
1474                 return 0;
1475
1476         if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1477                 tty_insert_flip_char(tty, 0, TTY_BREAK);
1478                 tty_schedule_flip(tty);
1479         }
1480
1481         if (intr & IntrLine)
1482                 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1483
1484         return 0;
1485 }
1486
1487 static void moxa_poll(unsigned long ignored)
1488 {
1489         struct moxa_board_conf *brd;
1490         u16 __iomem *ip;
1491         unsigned int card, port, served = 0;
1492
1493         spin_lock(&moxa_lock);
1494         for (card = 0; card < MAX_BOARDS; card++) {
1495                 brd = &moxa_boards[card];
1496                 if (!brd->ready)
1497                         continue;
1498
1499                 served++;
1500
1501                 ip = NULL;
1502                 if (readb(brd->intPend) == 0xff)
1503                         ip = brd->intTable + readb(brd->intNdx);
1504
1505                 for (port = 0; port < brd->numPorts; port++)
1506                         moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1507
1508                 if (ip)
1509                         writeb(0, brd->intPend); /* ACK */
1510
1511                 if (moxaLowWaterChk) {
1512                         struct moxa_port *p = brd->ports;
1513                         for (port = 0; port < brd->numPorts; port++, p++)
1514                                 if (p->lowChkFlag) {
1515                                         p->lowChkFlag = 0;
1516                                         moxa_low_water_check(p->tableAddr);
1517                                 }
1518                 }
1519         }
1520         moxaLowWaterChk = 0;
1521
1522         if (served)
1523                 mod_timer(&moxaTimer, jiffies + HZ / 50);
1524         spin_unlock(&moxa_lock);
1525 }
1526
1527 /******************************************************************************/
1528
1529 static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
1530 {
1531         register struct ktermios *ts = tty->termios;
1532         struct moxa_port *ch = tty->driver_data;
1533         int rts, cts, txflow, rxflow, xany, baud;
1534
1535         rts = cts = txflow = rxflow = xany = 0;
1536         if (ts->c_cflag & CRTSCTS)
1537                 rts = cts = 1;
1538         if (ts->c_iflag & IXON)
1539                 txflow = 1;
1540         if (ts->c_iflag & IXOFF)
1541                 rxflow = 1;
1542         if (ts->c_iflag & IXANY)
1543                 xany = 1;
1544
1545         /* Clear the features we don't support */
1546         ts->c_cflag &= ~CMSPAR;
1547         MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1548         baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
1549         if (baud == -1)
1550                 baud = tty_termios_baud_rate(old_termios);
1551         /* Not put the baud rate into the termios data */
1552         tty_encode_baud_rate(tty, baud, baud);
1553 }
1554
1555 static void moxa_setup_empty_event(struct tty_struct *tty)
1556 {
1557         struct moxa_port *ch = tty->driver_data;
1558
1559         spin_lock_bh(&moxa_lock);
1560         ch->statusflags |= EMPTYWAIT;
1561         spin_unlock_bh(&moxa_lock);
1562 }
1563
1564 static void moxa_shut_down(struct moxa_port *ch)
1565 {
1566         struct tty_struct *tp = ch->port.tty;
1567
1568         if (!(ch->port.flags & ASYNC_INITIALIZED))
1569                 return;
1570
1571         MoxaPortDisable(ch);
1572
1573         /*
1574          * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1575          */
1576         if (C_HUPCL(tp))
1577                 MoxaPortLineCtrl(ch, 0, 0);
1578
1579         spin_lock_bh(&moxa_lock);
1580         ch->port.flags &= ~ASYNC_INITIALIZED;
1581         spin_unlock_bh(&moxa_lock);
1582 }
1583
1584 /*****************************************************************************
1585  *      Driver level functions:                                              *
1586  *****************************************************************************/
1587
1588 static void MoxaPortFlushData(struct moxa_port *port, int mode)
1589 {
1590         void __iomem *ofsAddr;
1591         if (mode < 0 || mode > 2)
1592                 return;
1593         ofsAddr = port->tableAddr;
1594         moxafunc(ofsAddr, FC_FlushQueue, mode);
1595         if (mode != 1) {
1596                 port->lowChkFlag = 0;
1597                 moxa_low_water_check(ofsAddr);
1598         }
1599 }
1600
1601 /*
1602  *    Moxa Port Number Description:
1603  *
1604  *      MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1605  *      the port number using in MOXA driver functions will be 0 to 31 for
1606  *      first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1607  *      to 127 for fourth. For example, if you setup three MOXA boards,
1608  *      first board is C218, second board is C320-16 and third board is
1609  *      C320-32. The port number of first board (C218 - 8 ports) is from
1610  *      0 to 7. The port number of second board (C320 - 16 ports) is form
1611  *      32 to 47. The port number of third board (C320 - 32 ports) is from
1612  *      64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1613  *      127 will be invalid.
1614  *
1615  *
1616  *      Moxa Functions Description:
1617  *
1618  *      Function 1:     Driver initialization routine, this routine must be
1619  *                      called when initialized driver.
1620  *      Syntax:
1621  *      void MoxaDriverInit();
1622  *
1623  *
1624  *      Function 2:     Moxa driver private IOCTL command processing.
1625  *      Syntax:
1626  *      int  MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1627  *
1628  *           unsigned int cmd   : IOCTL command
1629  *           unsigned long arg  : IOCTL argument
1630  *           int port           : port number (0 - 127)
1631  *
1632  *           return:    0  (OK)
1633  *                      -EINVAL
1634  *                      -ENOIOCTLCMD
1635  *
1636  *
1637  *      Function 6:     Enable this port to start Tx/Rx data.
1638  *      Syntax:
1639  *      void MoxaPortEnable(int port);
1640  *           int port           : port number (0 - 127)
1641  *
1642  *
1643  *      Function 7:     Disable this port
1644  *      Syntax:
1645  *      void MoxaPortDisable(int port);
1646  *           int port           : port number (0 - 127)
1647  *
1648  *
1649  *      Function 10:    Setting baud rate of this port.
1650  *      Syntax:
1651  *      speed_t MoxaPortSetBaud(int port, speed_t baud);
1652  *           int port           : port number (0 - 127)
1653  *           long baud          : baud rate (50 - 115200)
1654  *
1655  *           return:    0       : this port is invalid or baud < 50
1656  *                      50 - 115200 : the real baud rate set to the port, if
1657  *                                    the argument baud is large than maximun
1658  *                                    available baud rate, the real setting
1659  *                                    baud rate will be the maximun baud rate.
1660  *
1661  *
1662  *      Function 12:    Configure the port.
1663  *      Syntax:
1664  *      int  MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1665  *           int port           : port number (0 - 127)
1666  *           struct ktermios * termio : termio structure pointer
1667  *           speed_t baud       : baud rate
1668  *
1669  *           return:    -1      : this port is invalid or termio == NULL
1670  *                      0       : setting O.K.
1671  *
1672  *
1673  *      Function 13:    Get the DTR/RTS state of this port.
1674  *      Syntax:
1675  *      int  MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1676  *           int port           : port number (0 - 127)
1677  *           int * dtrState     : pointer to INT to receive the current DTR
1678  *                                state. (if NULL, this function will not
1679  *                                write to this address)
1680  *           int * rtsState     : pointer to INT to receive the current RTS
1681  *                                state. (if NULL, this function will not
1682  *                                write to this address)
1683  *
1684  *           return:    -1      : this port is invalid
1685  *                      0       : O.K.
1686  *
1687  *
1688  *      Function 14:    Setting the DTR/RTS output state of this port.
1689  *      Syntax:
1690  *      void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1691  *           int port           : port number (0 - 127)
1692  *           int dtrState       : DTR output state (0: off, 1: on)
1693  *           int rtsState       : RTS output state (0: off, 1: on)
1694  *
1695  *
1696  *      Function 15:    Setting the flow control of this port.
1697  *      Syntax:
1698  *      void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1699  *                            int txFlow,int xany);
1700  *           int port           : port number (0 - 127)
1701  *           int rtsFlow        : H/W RTS flow control (0: no, 1: yes)
1702  *           int ctsFlow        : H/W CTS flow control (0: no, 1: yes)
1703  *           int rxFlow         : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1704  *           int txFlow         : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1705  *           int xany           : S/W XANY flow control (0: no, 1: yes)
1706  *
1707  *
1708  *      Function 16:    Get ths line status of this port
1709  *      Syntax:
1710  *      int  MoxaPortLineStatus(int port);
1711  *           int port           : port number (0 - 127)
1712  *
1713  *           return:    Bit 0 - CTS state (0: off, 1: on)
1714  *                      Bit 1 - DSR state (0: off, 1: on)
1715  *                      Bit 2 - DCD state (0: off, 1: on)
1716  *
1717  *
1718  *      Function 19:    Flush the Rx/Tx buffer data of this port.
1719  *      Syntax:
1720  *      void MoxaPortFlushData(int port, int mode);
1721  *           int port           : port number (0 - 127)
1722  *           int mode    
1723  *                      0       : flush the Rx buffer 
1724  *                      1       : flush the Tx buffer 
1725  *                      2       : flush the Rx and Tx buffer 
1726  *
1727  *
1728  *      Function 20:    Write data.
1729  *      Syntax:
1730  *      int  MoxaPortWriteData(int port, unsigned char * buffer, int length);
1731  *           int port           : port number (0 - 127)
1732  *           unsigned char * buffer     : pointer to write data buffer.
1733  *           int length         : write data length
1734  *
1735  *           return:    0 - length      : real write data length
1736  *
1737  *
1738  *      Function 21:    Read data.
1739  *      Syntax:
1740  *      int  MoxaPortReadData(int port, struct tty_struct *tty);
1741  *           int port           : port number (0 - 127)
1742  *           struct tty_struct *tty : tty for data
1743  *
1744  *           return:    0 - length      : real read data length
1745  *
1746  *
1747  *      Function 24:    Get the Tx buffer current queued data bytes
1748  *      Syntax:
1749  *      int  MoxaPortTxQueue(int port);
1750  *           int port           : port number (0 - 127)
1751  *
1752  *           return:    ..      : Tx buffer current queued data bytes
1753  *
1754  *
1755  *      Function 25:    Get the Tx buffer current free space
1756  *      Syntax:
1757  *      int  MoxaPortTxFree(int port);
1758  *           int port           : port number (0 - 127)
1759  *
1760  *           return:    ..      : Tx buffer current free space
1761  *
1762  *
1763  *      Function 26:    Get the Rx buffer current queued data bytes
1764  *      Syntax:
1765  *      int  MoxaPortRxQueue(int port);
1766  *           int port           : port number (0 - 127)
1767  *
1768  *           return:    ..      : Rx buffer current queued data bytes
1769  *
1770  *
1771  *      Function 28:    Disable port data transmission.
1772  *      Syntax:
1773  *      void MoxaPortTxDisable(int port);
1774  *           int port           : port number (0 - 127)
1775  *
1776  *
1777  *      Function 29:    Enable port data transmission.
1778  *      Syntax:
1779  *      void MoxaPortTxEnable(int port);
1780  *           int port           : port number (0 - 127)
1781  *
1782  *
1783  *      Function 31:    Get the received BREAK signal count and reset it.
1784  *      Syntax:
1785  *      int  MoxaPortResetBrkCnt(int port);
1786  *           int port           : port number (0 - 127)
1787  *
1788  *           return:    0 - ..  : BREAK signal count
1789  *
1790  *
1791  */
1792
1793 static void MoxaPortEnable(struct moxa_port *port)
1794 {
1795         void __iomem *ofsAddr;
1796         u16 lowwater = 512;
1797
1798         ofsAddr = port->tableAddr;
1799         writew(lowwater, ofsAddr + Low_water);
1800         if (MOXA_IS_320(port->board))
1801                 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1802         else
1803                 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1804                                 ofsAddr + HostStat);
1805
1806         moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1807         moxafunc(ofsAddr, FC_FlushQueue, 2);
1808
1809         moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1810         MoxaPortLineStatus(port);
1811 }
1812
1813 static void MoxaPortDisable(struct moxa_port *port)
1814 {
1815         void __iomem *ofsAddr = port->tableAddr;
1816
1817         moxafunc(ofsAddr, FC_SetFlowCtl, 0);    /* disable flow control */
1818         moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1819         writew(0, ofsAddr + HostStat);
1820         moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1821 }
1822
1823 static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
1824 {
1825         void __iomem *ofsAddr = port->tableAddr;
1826         unsigned int clock, val;
1827         speed_t max;
1828
1829         max = MOXA_IS_320(port->board) ? 460800 : 921600;
1830         if (baud < 50)
1831                 return 0;
1832         if (baud > max)
1833                 baud = max;
1834         clock = 921600;
1835         val = clock / baud;
1836         moxafunc(ofsAddr, FC_SetBaud, val);
1837         baud = clock / val;
1838         return baud;
1839 }
1840
1841 static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1842                 speed_t baud)
1843 {
1844         void __iomem *ofsAddr;
1845         tcflag_t cflag;
1846         tcflag_t mode = 0;
1847
1848         ofsAddr = port->tableAddr;
1849         cflag = termio->c_cflag;        /* termio->c_cflag */
1850
1851         mode = termio->c_cflag & CSIZE;
1852         if (mode == CS5)
1853                 mode = MX_CS5;
1854         else if (mode == CS6)
1855                 mode = MX_CS6;
1856         else if (mode == CS7)
1857                 mode = MX_CS7;
1858         else if (mode == CS8)
1859                 mode = MX_CS8;
1860
1861         if (termio->c_cflag & CSTOPB) {
1862                 if (mode == MX_CS5)
1863                         mode |= MX_STOP15;
1864                 else
1865                         mode |= MX_STOP2;
1866         } else
1867                 mode |= MX_STOP1;
1868
1869         if (termio->c_cflag & PARENB) {
1870                 if (termio->c_cflag & PARODD)
1871                         mode |= MX_PARODD;
1872                 else
1873                         mode |= MX_PAREVEN;
1874         } else
1875                 mode |= MX_PARNONE;
1876
1877         moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
1878
1879         if (MOXA_IS_320(port->board) && baud >= 921600)
1880                 return -1;
1881
1882         baud = MoxaPortSetBaud(port, baud);
1883
1884         if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1885                 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1886                 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1887                 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
1888                 moxa_wait_finish(ofsAddr);
1889
1890         }
1891         return baud;
1892 }
1893
1894 static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1895                 int *rtsState)
1896 {
1897         if (dtrState)
1898                 *dtrState = !!(port->lineCtrl & DTR_ON);
1899         if (rtsState)
1900                 *rtsState = !!(port->lineCtrl & RTS_ON);
1901
1902         return 0;
1903 }
1904
1905 static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
1906 {
1907         u8 mode = 0;
1908
1909         if (dtr)
1910                 mode |= DTR_ON;
1911         if (rts)
1912                 mode |= RTS_ON;
1913         port->lineCtrl = mode;
1914         moxafunc(port->tableAddr, FC_LineControl, mode);
1915 }
1916
1917 static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1918                 int txflow, int rxflow, int txany)
1919 {
1920         int mode = 0;
1921
1922         if (rts)
1923                 mode |= RTS_FlowCtl;
1924         if (cts)
1925                 mode |= CTS_FlowCtl;
1926         if (txflow)
1927                 mode |= Tx_FlowCtl;
1928         if (rxflow)
1929                 mode |= Rx_FlowCtl;
1930         if (txany)
1931                 mode |= IXM_IXANY;
1932         moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
1933 }
1934
1935 static int MoxaPortLineStatus(struct moxa_port *port)
1936 {
1937         void __iomem *ofsAddr;
1938         int val;
1939
1940         ofsAddr = port->tableAddr;
1941         if (MOXA_IS_320(port->board)) {
1942                 moxafunc(ofsAddr, FC_LineStatus, 0);
1943                 val = readw(ofsAddr + FuncArg);
1944         } else {
1945                 val = readw(ofsAddr + FlagStat) >> 4;
1946         }
1947         val &= 0x0B;
1948         if (val & 8)
1949                 val |= 4;
1950         spin_lock_bh(&moxa_lock);
1951         moxa_new_dcdstate(port, val & 8);
1952         spin_unlock_bh(&moxa_lock);
1953         val &= 7;
1954         return val;
1955 }
1956
1957 static int MoxaPortWriteData(struct moxa_port *port,
1958                 const unsigned char *buffer, int len)
1959 {
1960         void __iomem *baseAddr, *ofsAddr, *ofs;
1961         unsigned int c, total;
1962         u16 head, tail, tx_mask, spage, epage;
1963         u16 pageno, pageofs, bufhead;
1964
1965         ofsAddr = port->tableAddr;
1966         baseAddr = port->board->basemem;
1967         tx_mask = readw(ofsAddr + TX_mask);
1968         spage = readw(ofsAddr + Page_txb);
1969         epage = readw(ofsAddr + EndPage_txb);
1970         tail = readw(ofsAddr + TXwptr);
1971         head = readw(ofsAddr + TXrptr);
1972         c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
1973         if (c > len)
1974                 c = len;
1975         moxaLog.txcnt[port->port.tty->index] += c;
1976         total = c;
1977         if (spage == epage) {
1978                 bufhead = readw(ofsAddr + Ofs_txb);
1979                 writew(spage, baseAddr + Control_reg);
1980                 while (c > 0) {
1981                         if (head > tail)
1982                                 len = head - tail - 1;
1983                         else
1984                                 len = tx_mask + 1 - tail;
1985                         len = (c > len) ? len : c;
1986                         ofs = baseAddr + DynPage_addr + bufhead + tail;
1987                         memcpy_toio(ofs, buffer, len);
1988                         buffer += len;
1989                         tail = (tail + len) & tx_mask;
1990                         c -= len;
1991                 }
1992         } else {
1993                 pageno = spage + (tail >> 13);
1994                 pageofs = tail & Page_mask;
1995                 while (c > 0) {
1996                         len = Page_size - pageofs;
1997                         if (len > c)
1998                                 len = c;
1999                         writeb(pageno, baseAddr + Control_reg);
2000                         ofs = baseAddr + DynPage_addr + pageofs;
2001                         memcpy_toio(ofs, buffer, len);
2002                         buffer += len;
2003                         if (++pageno == epage)
2004                                 pageno = spage;
2005                         pageofs = 0;
2006                         c -= len;
2007                 }
2008                 tail = (tail + total) & tx_mask;
2009         }
2010         writew(tail, ofsAddr + TXwptr);
2011         writeb(1, ofsAddr + CD180TXirq);        /* start to send */
2012         return total;
2013 }
2014
2015 static int MoxaPortReadData(struct moxa_port *port)
2016 {
2017         struct tty_struct *tty = port->port.tty;
2018         unsigned char *dst;
2019         void __iomem *baseAddr, *ofsAddr, *ofs;
2020         unsigned int count, len, total;
2021         u16 tail, rx_mask, spage, epage;
2022         u16 pageno, pageofs, bufhead, head;
2023
2024         ofsAddr = port->tableAddr;
2025         baseAddr = port->board->basemem;
2026         head = readw(ofsAddr + RXrptr);
2027         tail = readw(ofsAddr + RXwptr);
2028         rx_mask = readw(ofsAddr + RX_mask);
2029         spage = readw(ofsAddr + Page_rxb);
2030         epage = readw(ofsAddr + EndPage_rxb);
2031         count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
2032         if (count == 0)
2033                 return 0;
2034
2035         total = count;
2036         moxaLog.rxcnt[tty->index] += total;
2037         if (spage == epage) {
2038                 bufhead = readw(ofsAddr + Ofs_rxb);
2039                 writew(spage, baseAddr + Control_reg);
2040                 while (count > 0) {
2041                         ofs = baseAddr + DynPage_addr + bufhead + head;
2042                         len = (tail >= head) ? (tail - head) :
2043                                         (rx_mask + 1 - head);
2044                         len = tty_prepare_flip_string(tty, &dst,
2045                                         min(len, count));
2046                         memcpy_fromio(dst, ofs, len);
2047                         head = (head + len) & rx_mask;
2048                         count -= len;
2049                 }
2050         } else {
2051                 pageno = spage + (head >> 13);
2052                 pageofs = head & Page_mask;
2053                 while (count > 0) {
2054                         writew(pageno, baseAddr + Control_reg);
2055                         ofs = baseAddr + DynPage_addr + pageofs;
2056                         len = tty_prepare_flip_string(tty, &dst,
2057                                         min(Page_size - pageofs, count));
2058                         memcpy_fromio(dst, ofs, len);
2059
2060                         count -= len;
2061                         pageofs = (pageofs + len) & Page_mask;
2062                         if (pageofs == 0 && ++pageno == epage)
2063                                 pageno = spage;
2064                 }
2065                 head = (head + total) & rx_mask;
2066         }
2067         writew(head, ofsAddr + RXrptr);
2068         if (readb(ofsAddr + FlagStat) & Xoff_state) {
2069                 moxaLowWaterChk = 1;
2070                 port->lowChkFlag = 1;
2071         }
2072         return total;
2073 }
2074
2075
2076 static int MoxaPortTxQueue(struct moxa_port *port)
2077 {
2078         void __iomem *ofsAddr = port->tableAddr;
2079         u16 rptr, wptr, mask;
2080
2081         rptr = readw(ofsAddr + TXrptr);
2082         wptr = readw(ofsAddr + TXwptr);
2083         mask = readw(ofsAddr + TX_mask);
2084         return (wptr - rptr) & mask;
2085 }
2086
2087 static int MoxaPortTxFree(struct moxa_port *port)
2088 {
2089         void __iomem *ofsAddr = port->tableAddr;
2090         u16 rptr, wptr, mask;
2091
2092         rptr = readw(ofsAddr + TXrptr);
2093         wptr = readw(ofsAddr + TXwptr);
2094         mask = readw(ofsAddr + TX_mask);
2095         return mask - ((wptr - rptr) & mask);
2096 }
2097
2098 static int MoxaPortRxQueue(struct moxa_port *port)
2099 {
2100         void __iomem *ofsAddr = port->tableAddr;
2101         u16 rptr, wptr, mask;
2102
2103         rptr = readw(ofsAddr + RXrptr);
2104         wptr = readw(ofsAddr + RXwptr);
2105         mask = readw(ofsAddr + RX_mask);
2106         return (wptr - rptr) & mask;
2107 }
2108
2109 static void MoxaPortTxDisable(struct moxa_port *port)
2110 {
2111         moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
2112 }
2113
2114 static void MoxaPortTxEnable(struct moxa_port *port)
2115 {
2116         moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
2117 }
2118
2119 static int moxa_get_serial_info(struct moxa_port *info,
2120                 struct serial_struct __user *retinfo)
2121 {
2122         struct serial_struct tmp = {
2123                 .type = info->type,
2124                 .line = info->port.tty->index,
2125                 .flags = info->port.flags,
2126                 .baud_base = 921600,
2127                 .close_delay = info->close_delay
2128         };
2129         return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
2130 }
2131
2132
2133 static int moxa_set_serial_info(struct moxa_port *info,
2134                 struct serial_struct __user *new_info)
2135 {
2136         struct serial_struct new_serial;
2137
2138         if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2139                 return -EFAULT;
2140
2141         if (new_serial.irq != 0 || new_serial.port != 0 ||
2142                         new_serial.custom_divisor != 0 ||
2143                         new_serial.baud_base != 921600)
2144                 return -EPERM;
2145
2146         if (!capable(CAP_SYS_ADMIN)) {
2147                 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2148                      (info->port.flags & ~ASYNC_USR_MASK)))
2149                         return -EPERM;
2150         } else
2151                 info->close_delay = new_serial.close_delay * HZ / 100;
2152
2153         new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2154         new_serial.flags |= (info->port.flags & ASYNC_FLAGS);
2155
2156         MoxaSetFifo(info, new_serial.type == PORT_16550A);
2157
2158         info->type = new_serial.type;
2159         return 0;
2160 }
2161
2162
2163
2164 /*****************************************************************************
2165  *      Static local functions:                                              *
2166  *****************************************************************************/
2167
2168 static void MoxaSetFifo(struct moxa_port *port, int enable)
2169 {
2170         void __iomem *ofsAddr = port->tableAddr;
2171
2172         if (!enable) {
2173                 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2174                 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2175         } else {
2176                 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2177                 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2178         }
2179 }