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