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