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