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