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