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