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