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