vserver 1.9.3
[linux-2.6.git] / drivers / char / moxa.c
1 /*****************************************************************************/
2 /*
3  *           moxa.c  -- MOXA Intellio family multiport serial driver.
4  *
5  *      Copyright (C) 1999-2000  Moxa Technologies (support@moxa.com.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  *      This program is distributed in the hope that it will be useful,
16  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *      GNU General Public License for more details.
19  *
20  *      You should have received a copy of the GNU General Public License
21  *      along with this program; if not, write to the Free Software
22  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 /*
26  *    MOXA Intellio Series Driver
27  *      for             : LINUX
28  *      date            : 1999/1/7
29  *      version         : 5.1
30  */
31
32 #include <linux/config.h>
33 #include <linux/module.h>
34 #include <linux/types.h>
35 #include <linux/mm.h>
36 #include <linux/ioport.h>
37 #include <linux/errno.h>
38 #include <linux/signal.h>
39 #include <linux/sched.h>
40 #include <linux/timer.h>
41 #include <linux/interrupt.h>
42 #include <linux/tty.h>
43 #include <linux/tty_flip.h>
44 #include <linux/major.h>
45 #include <linux/string.h>
46 #include <linux/fcntl.h>
47 #include <linux/ptrace.h>
48 #include <linux/serial.h>
49 #include <linux/tty_driver.h>
50 #include <linux/delay.h>
51 #include <linux/pci.h>
52 #include <linux/init.h>
53
54 #include <asm/system.h>
55 #include <asm/io.h>
56 #include <asm/bitops.h>
57 #include <asm/uaccess.h>
58
59 #define         MOXA_VERSION            "5.1k"
60
61 #define MOXAMAJOR       172
62 #define MOXACUMAJOR     173
63
64 #define put_to_user(arg1, arg2) put_user(arg1, (unsigned long *)arg2)
65 #define get_from_user(arg1, arg2) get_user(arg1, (unsigned int *)arg2)
66
67 #define MAX_BOARDS              4       /* Don't change this value */
68 #define MAX_PORTS_PER_BOARD     32      /* Don't change this value */
69 #define MAX_PORTS               128     /* Don't change this value */
70
71 /*
72  *    Define the Moxa PCI vendor and device IDs.
73  */
74 #define MOXA_BUS_TYPE_ISA               0
75 #define MOXA_BUS_TYPE_PCI               1
76
77 #ifndef PCI_VENDOR_ID_MOXA
78 #define PCI_VENDOR_ID_MOXA      0x1393
79 #endif
80 #ifndef PCI_DEVICE_ID_CP204J
81 #define PCI_DEVICE_ID_CP204J    0x2040
82 #endif
83 #ifndef PCI_DEVICE_ID_C218
84 #define PCI_DEVICE_ID_C218      0x2180
85 #endif
86 #ifndef PCI_DEVICE_ID_C320
87 #define PCI_DEVICE_ID_C320      0x3200
88 #endif
89
90 enum {
91         MOXA_BOARD_C218_PCI = 1,
92         MOXA_BOARD_C218_ISA,
93         MOXA_BOARD_C320_PCI,
94         MOXA_BOARD_C320_ISA,
95         MOXA_BOARD_CP204J,
96 };
97
98 static char *moxa_brdname[] =
99 {
100         "C218 Turbo PCI series",
101         "C218 Turbo ISA series",
102         "C320 Turbo PCI series",
103         "C320 Turbo ISA series",
104         "CP-204J series",
105 };
106
107 #ifdef CONFIG_PCI
108 static struct pci_device_id moxa_pcibrds[] = {
109         { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C218, PCI_ANY_ID, PCI_ANY_ID, 
110           0, 0, MOXA_BOARD_C218_PCI },
111         { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C320, PCI_ANY_ID, PCI_ANY_ID, 
112           0, 0, MOXA_BOARD_C320_PCI },
113         { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP204J, PCI_ANY_ID, PCI_ANY_ID, 
114           0, 0, MOXA_BOARD_CP204J },
115         { 0 }
116 };
117 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
118 #endif /* CONFIG_PCI */
119
120 typedef struct _moxa_isa_board_conf {
121         int boardType;
122         int numPorts;
123         unsigned long baseAddr;
124 } moxa_isa_board_conf;
125
126 static moxa_isa_board_conf moxa_isa_boards[] =
127 {
128 /*       {MOXA_BOARD_C218_ISA,8,0xDC000}, */
129 };
130
131 typedef struct _moxa_pci_devinfo {
132         ushort busNum;
133         ushort devNum;
134 } moxa_pci_devinfo;
135
136 typedef struct _moxa_board_conf {
137         int boardType;
138         int numPorts;
139         unsigned long baseAddr;
140         int busType;
141         moxa_pci_devinfo pciInfo;
142 } moxa_board_conf;
143
144 static moxa_board_conf moxa_boards[MAX_BOARDS];
145 static unsigned long moxaBaseAddr[MAX_BOARDS];
146
147 struct moxa_str {
148         int type;
149         int port;
150         int close_delay;
151         unsigned short closing_wait;
152         int count;
153         int blocked_open;
154         long event; /* long req'd for set_bit --RR */
155         int asyncflags;
156         unsigned long statusflags;
157         struct tty_struct *tty;
158         int cflag;
159         wait_queue_head_t open_wait;
160         wait_queue_head_t close_wait;
161         struct work_struct tqueue;
162 };
163
164 struct mxser_mstatus {
165         tcflag_t cflag;
166         int cts;
167         int dsr;
168         int ri;
169         int dcd;
170 };
171
172 static struct mxser_mstatus GMStatus[MAX_PORTS];
173
174 /* statusflags */
175 #define TXSTOPPED       0x1
176 #define LOWWAIT         0x2
177 #define EMPTYWAIT       0x4
178 #define THROTTLE        0x8
179
180 /* event */
181 #define MOXA_EVENT_HANGUP       1
182
183 #define SERIAL_DO_RESTART
184
185
186 #define SERIAL_TYPE_NORMAL      1
187
188 #define WAKEUP_CHARS            256
189
190 #define PORTNO(x)               ((x)->index)
191
192 static int verbose = 0;
193 static int ttymajor = MOXAMAJOR;
194 /* Variables for insmod */
195 #ifdef MODULE
196 static int baseaddr[]   =       {0, 0, 0, 0};
197 static int type[]       =       {0, 0, 0, 0};
198 static int numports[]   =       {0, 0, 0, 0};
199 #endif
200
201 MODULE_AUTHOR("William Chen");
202 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
203 MODULE_LICENSE("GPL");
204 MODULE_PARM(type, "1-4i");
205 MODULE_PARM(baseaddr, "1-4i");
206 MODULE_PARM(numports, "1-4i");
207 MODULE_PARM(ttymajor, "i");
208 MODULE_PARM(verbose, "i");
209
210 static struct tty_driver *moxaDriver;
211 static struct moxa_str moxaChannels[MAX_PORTS];
212 static unsigned char *moxaXmitBuff;
213 static int moxaTimer_on;
214 static struct timer_list moxaTimer;
215 static int moxaEmptyTimer_on[MAX_PORTS];
216 static struct timer_list moxaEmptyTimer[MAX_PORTS];
217 static struct semaphore moxaBuffSem;
218
219 /*
220  * static functions:
221  */
222 static void do_moxa_softint(void *);
223 static int moxa_open(struct tty_struct *, struct file *);
224 static void moxa_close(struct tty_struct *, struct file *);
225 static int moxa_write(struct tty_struct *, int, const unsigned char *, int);
226 static int moxa_write_room(struct tty_struct *);
227 static void moxa_flush_buffer(struct tty_struct *);
228 static int moxa_chars_in_buffer(struct tty_struct *);
229 static void moxa_flush_chars(struct tty_struct *);
230 static void moxa_put_char(struct tty_struct *, unsigned char);
231 static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
232 static void moxa_throttle(struct tty_struct *);
233 static void moxa_unthrottle(struct tty_struct *);
234 static void moxa_set_termios(struct tty_struct *, struct termios *);
235 static void moxa_stop(struct tty_struct *);
236 static void moxa_start(struct tty_struct *);
237 static void moxa_hangup(struct tty_struct *);
238 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
239 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
240                          unsigned int set, unsigned int clear);
241 static void moxa_poll(unsigned long);
242 static void set_tty_param(struct tty_struct *);
243 static int block_till_ready(struct tty_struct *, struct file *,
244                             struct moxa_str *);
245 static void setup_empty_event(struct tty_struct *);
246 static void check_xmit_empty(unsigned long);
247 static void shut_down(struct moxa_str *);
248 static void receive_data(struct moxa_str *);
249 /*
250  * moxa board interface functions:
251  */
252 static void MoxaDriverInit(void);
253 static int MoxaDriverIoctl(unsigned int, unsigned long, int);
254 static int MoxaDriverPoll(void);
255 static int MoxaPortsOfCard(int);
256 static int MoxaPortIsValid(int);
257 static void MoxaPortEnable(int);
258 static void MoxaPortDisable(int);
259 static long MoxaPortGetMaxBaud(int);
260 static long MoxaPortSetBaud(int, long);
261 static int MoxaPortSetTermio(int, struct termios *);
262 static int MoxaPortGetLineOut(int, int *, int *);
263 static void MoxaPortLineCtrl(int, int, int);
264 static void MoxaPortFlowCtrl(int, int, int, int, int, int);
265 static int MoxaPortLineStatus(int);
266 static int MoxaPortDCDChange(int);
267 static int MoxaPortDCDON(int);
268 static void MoxaPortFlushData(int, int);
269 static int MoxaPortWriteData(int, unsigned char *, int);
270 static int MoxaPortReadData(int, unsigned char *, int);
271 static int MoxaPortTxQueue(int);
272 static int MoxaPortRxQueue(int);
273 static int MoxaPortTxFree(int);
274 static void MoxaPortTxDisable(int);
275 static void MoxaPortTxEnable(int);
276 static int MoxaPortResetBrkCnt(int);
277 static void MoxaPortSendBreak(int, int);
278 static int moxa_get_serial_info(struct moxa_str *, struct serial_struct __user *);
279 static int moxa_set_serial_info(struct moxa_str *, struct serial_struct __user *);
280 static void MoxaSetFifo(int port, int enable);
281
282 static struct tty_operations moxa_ops = {
283         .open = moxa_open,
284         .close = moxa_close,
285         .write = moxa_write,
286         .write_room = moxa_write_room,
287         .flush_buffer = moxa_flush_buffer,
288         .chars_in_buffer = moxa_chars_in_buffer,
289         .flush_chars = moxa_flush_chars,
290         .put_char = moxa_put_char,
291         .ioctl = moxa_ioctl,
292         .throttle = moxa_throttle,
293         .unthrottle = moxa_unthrottle,
294         .set_termios = moxa_set_termios,
295         .stop = moxa_stop,
296         .start = moxa_start,
297         .hangup = moxa_hangup,
298         .tiocmget = moxa_tiocmget,
299         .tiocmset = moxa_tiocmset,
300 };
301
302 #ifdef CONFIG_PCI
303 static int moxa_get_PCI_conf(struct pci_dev *p, int board_type, moxa_board_conf * board)
304 {
305         board->baseAddr = pci_resource_start (p, 2);
306         board->boardType = board_type;
307         switch (board_type) {
308         case MOXA_BOARD_C218_ISA:
309         case MOXA_BOARD_C218_PCI:
310                 board->numPorts = 8;
311                 break;
312
313         case MOXA_BOARD_CP204J:
314                 board->numPorts = 4;
315                 break;
316         default:
317                 board->numPorts = 0;
318                 break;
319         }
320         board->busType = MOXA_BUS_TYPE_PCI;
321         board->pciInfo.busNum = p->bus->number;
322         board->pciInfo.devNum = p->devfn >> 3;
323
324         return (0);
325 }
326 #endif /* CONFIG_PCI */
327
328 static int __init moxa_init(void)
329 {
330         int i, numBoards;
331         struct moxa_str *ch;
332
333         printk(KERN_INFO "MOXA Intellio family driver version %s\n", MOXA_VERSION);
334         moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
335         if (!moxaDriver)
336                 return -ENOMEM;
337
338         init_MUTEX(&moxaBuffSem);
339         moxaDriver->owner = THIS_MODULE;
340         moxaDriver->name = "ttya";
341         moxaDriver->devfs_name = "tts/a";
342         moxaDriver->major = ttymajor;
343         moxaDriver->minor_start = 0;
344         moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
345         moxaDriver->subtype = SERIAL_TYPE_NORMAL;
346         moxaDriver->init_termios = tty_std_termios;
347         moxaDriver->init_termios.c_iflag = 0;
348         moxaDriver->init_termios.c_oflag = 0;
349         moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
350         moxaDriver->init_termios.c_lflag = 0;
351         moxaDriver->flags = TTY_DRIVER_REAL_RAW;
352         tty_set_operations(moxaDriver, &moxa_ops);
353
354         moxaXmitBuff = NULL;
355
356         for (i = 0, ch = moxaChannels; i < MAX_PORTS; i++, ch++) {
357                 ch->type = PORT_16550A;
358                 ch->port = i;
359                 INIT_WORK(&ch->tqueue, do_moxa_softint, ch);
360                 ch->tty = NULL;
361                 ch->close_delay = 5 * HZ / 10;
362                 ch->closing_wait = 30 * HZ;
363                 ch->count = 0;
364                 ch->blocked_open = 0;
365                 ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
366                 init_waitqueue_head(&ch->open_wait);
367                 init_waitqueue_head(&ch->close_wait);
368         }
369
370         for (i = 0; i < MAX_BOARDS; i++) {
371                 moxa_boards[i].boardType = 0;
372                 moxa_boards[i].numPorts = 0;
373                 moxa_boards[i].baseAddr = 0;
374                 moxa_boards[i].busType = 0;
375                 moxa_boards[i].pciInfo.busNum = 0;
376                 moxa_boards[i].pciInfo.devNum = 0;
377         }
378         MoxaDriverInit();
379         printk("Tty devices major number = %d\n", ttymajor);
380
381         if (tty_register_driver(moxaDriver)) {
382                 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
383                 put_tty_driver(moxaDriver);
384                 return -1;
385         }
386         for (i = 0; i < MAX_PORTS; i++) {
387                 init_timer(&moxaEmptyTimer[i]);
388                 moxaEmptyTimer[i].function = check_xmit_empty;
389                 moxaEmptyTimer[i].data = (unsigned long) & moxaChannels[i];
390                 moxaEmptyTimer_on[i] = 0;
391         }
392
393         init_timer(&moxaTimer);
394         moxaTimer.function = moxa_poll;
395         moxaTimer.expires = jiffies + (HZ / 50);
396         moxaTimer_on = 1;
397         add_timer(&moxaTimer);
398
399         /* Find the boards defined in source code */
400         numBoards = 0;
401         for (i = 0; i < MAX_BOARDS; i++) {
402                 if ((moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA) ||
403                  (moxa_isa_boards[i].boardType == MOXA_BOARD_C320_ISA)) {
404                         moxa_boards[numBoards].boardType = moxa_isa_boards[i].boardType;
405                         if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
406                                 moxa_boards[numBoards].numPorts = 8;
407                         else
408                                 moxa_boards[numBoards].numPorts = moxa_isa_boards[i].numPorts;
409                         moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
410                         moxa_boards[numBoards].baseAddr = moxa_isa_boards[i].baseAddr;
411                         if (verbose)
412                                 printk("Board %2d: %s board(baseAddr=%lx)\n",
413                                        numBoards + 1,
414                                        moxa_brdname[moxa_boards[numBoards].boardType - 1],
415                                        moxa_boards[numBoards].baseAddr);
416                         numBoards++;
417                 }
418         }
419         /* Find the boards defined form module args. */
420 #ifdef MODULE
421         for (i = 0; i < MAX_BOARDS; i++) {
422                 if ((type[i] == MOXA_BOARD_C218_ISA) ||
423                     (type[i] == MOXA_BOARD_C320_ISA)) {
424                         if (verbose)
425                                 printk("Board %2d: %s board(baseAddr=%lx)\n",
426                                        numBoards + 1,
427                                        moxa_brdname[type[i] - 1],
428                                        (unsigned long) baseaddr[i]);
429                         if (numBoards >= MAX_BOARDS) {
430                                 if (verbose)
431                                         printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
432                                 continue;
433                         }
434                         moxa_boards[numBoards].boardType = type[i];
435                         if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
436                                 moxa_boards[numBoards].numPorts = 8;
437                         else
438                                 moxa_boards[numBoards].numPorts = numports[i];
439                         moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
440                         moxa_boards[numBoards].baseAddr = baseaddr[i];
441                         numBoards++;
442                 }
443         }
444 #endif
445         /* Find PCI boards here */
446 #ifdef CONFIG_PCI
447         {
448                 struct pci_dev *p = NULL;
449                 int n = (sizeof(moxa_pcibrds) / sizeof(moxa_pcibrds[0])) - 1;
450                 i = 0;
451                 while (i < n) {
452                         while ((p = pci_find_device(moxa_pcibrds[i].vendor, moxa_pcibrds[i].device, p))!=NULL)
453                         {
454                                 if (pci_enable_device(p))
455                                         continue;
456                                 if (numBoards >= MAX_BOARDS) {
457                                         if (verbose)
458                                                 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
459                                 } else {
460                                         moxa_get_PCI_conf(p, moxa_pcibrds[i].driver_data,
461                                                 &moxa_boards[numBoards]);
462                                         numBoards++;
463                                 }
464                         }
465                         i++;
466                 }
467         }
468 #endif
469         for (i = 0; i < numBoards; i++) {
470                 moxaBaseAddr[i] = (unsigned long) ioremap((unsigned long) moxa_boards[i].baseAddr, 0x4000);
471         }
472
473         return (0);
474 }
475
476 static void __exit moxa_exit(void)
477 {
478         int i;
479
480         if (verbose)
481                 printk("Unloading module moxa ...\n");
482
483         if (moxaTimer_on)
484                 del_timer(&moxaTimer);
485
486         for (i = 0; i < MAX_PORTS; i++)
487                 if (moxaEmptyTimer_on[i])
488                         del_timer(&moxaEmptyTimer[i]);
489
490         if (tty_unregister_driver(moxaDriver))
491                 printk("Couldn't unregister MOXA Intellio family serial driver\n");
492         put_tty_driver(moxaDriver);
493         if (verbose)
494                 printk("Done\n");
495
496 }
497
498 module_init(moxa_init);
499 module_exit(moxa_exit);
500
501 static void do_moxa_softint(void *private_)
502 {
503         struct moxa_str *ch = (struct moxa_str *) private_;
504         struct tty_struct *tty;
505
506         if (ch && (tty = ch->tty)) {
507                 if (test_and_clear_bit(MOXA_EVENT_HANGUP, &ch->event)) {
508                         tty_hangup(tty);        /* FIXME: module removal race here - AKPM */
509                         wake_up_interruptible(&ch->open_wait);
510                         ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
511                 }
512         }
513 }
514
515 static int moxa_open(struct tty_struct *tty, struct file *filp)
516 {
517         struct moxa_str *ch;
518         int port;
519         int retval;
520         unsigned long page;
521
522         port = PORTNO(tty);
523         if (port == MAX_PORTS) {
524                 return (0);
525         }
526         if (!MoxaPortIsValid(port)) {
527                 tty->driver_data = NULL;
528                 return (-ENODEV);
529         }
530         down(&moxaBuffSem);
531         if (!moxaXmitBuff) {
532                 page = get_zeroed_page(GFP_KERNEL);
533                 if (!page) {
534                         up(&moxaBuffSem);
535                         return (-ENOMEM);
536                 }
537                 /* This test is guarded by the BuffSem so no longer needed
538                    delete me in 2.5 */
539                 if (moxaXmitBuff)
540                         free_page(page);
541                 else
542                         moxaXmitBuff = (unsigned char *) page;
543         }
544         up(&moxaBuffSem);
545
546         ch = &moxaChannels[port];
547         ch->count++;
548         tty->driver_data = ch;
549         ch->tty = tty;
550         if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
551                 ch->statusflags = 0;
552                 set_tty_param(tty);
553                 MoxaPortLineCtrl(ch->port, 1, 1);
554                 MoxaPortEnable(ch->port);
555                 ch->asyncflags |= ASYNC_INITIALIZED;
556         }
557         retval = block_till_ready(tty, filp, ch);
558
559         moxa_unthrottle(tty);
560
561         if (ch->type == PORT_16550A) {
562                 MoxaSetFifo(ch->port, 1);
563         } else {
564                 MoxaSetFifo(ch->port, 0);
565         }
566
567         return (retval);
568 }
569
570 static void moxa_close(struct tty_struct *tty, struct file *filp)
571 {
572         struct moxa_str *ch;
573         int port;
574
575         port = PORTNO(tty);
576         if (port == MAX_PORTS) {
577                 return;
578         }
579         if (!MoxaPortIsValid(port)) {
580 #ifdef SERIAL_DEBUG_CLOSE
581                 printk("Invalid portno in moxa_close\n");
582 #endif
583                 tty->driver_data = NULL;
584                 return;
585         }
586         if (tty->driver_data == NULL) {
587                 return;
588         }
589         if (tty_hung_up_p(filp)) {
590                 return;
591         }
592         ch = (struct moxa_str *) tty->driver_data;
593
594         if ((tty->count == 1) && (ch->count != 1)) {
595                 printk("moxa_close: bad serial port count; tty->count is 1, "
596                        "ch->count is %d\n", ch->count);
597                 ch->count = 1;
598         }
599         if (--ch->count < 0) {
600                 printk("moxa_close: bad serial port count, device=%s\n",
601                        tty->name);
602                 ch->count = 0;
603         }
604         if (ch->count) {
605                 return;
606         }
607         ch->asyncflags |= ASYNC_CLOSING;
608
609         ch->cflag = tty->termios->c_cflag;
610         if (ch->asyncflags & ASYNC_INITIALIZED) {
611                 setup_empty_event(tty);
612                 tty_wait_until_sent(tty, 30 * HZ);      /* 30 seconds timeout */
613                 moxaEmptyTimer_on[ch->port] = 0;
614                 del_timer(&moxaEmptyTimer[ch->port]);
615         }
616         shut_down(ch);
617         MoxaPortFlushData(port, 2);
618
619         if (tty->driver->flush_buffer)
620                 tty->driver->flush_buffer(tty);
621         tty_ldisc_flush(tty);
622                         
623         tty->closing = 0;
624         ch->event = 0;
625         ch->tty = NULL;
626         if (ch->blocked_open) {
627                 if (ch->close_delay) {
628                         set_current_state(TASK_INTERRUPTIBLE);
629                         schedule_timeout(ch->close_delay);
630                 }
631                 wake_up_interruptible(&ch->open_wait);
632         }
633         ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
634         wake_up_interruptible(&ch->close_wait);
635 }
636
637 static int moxa_write(struct tty_struct *tty, int from_user,
638                       const unsigned char *buf, int count)
639 {
640         struct moxa_str *ch;
641         int len, port;
642         unsigned long flags;
643
644         ch = (struct moxa_str *) tty->driver_data;
645         if (ch == NULL)
646                 return (0);
647         port = ch->port;
648         save_flags(flags);
649         if (from_user) {
650                 if (count > PAGE_SIZE)
651                         count = PAGE_SIZE;
652                 down(&moxaBuffSem);
653                 if (copy_from_user(moxaXmitBuff, buf, count)) {
654                         len = -EFAULT;
655                 } else {
656                         cli();
657                         len = MoxaPortWriteData(port, moxaXmitBuff, count);
658                         restore_flags(flags);
659                 }
660                 up(&moxaBuffSem);
661                 if (len < 0)
662                         return len;
663         } else {
664                 cli();
665                 len = MoxaPortWriteData(port, (unsigned char *) buf, count);
666                 restore_flags(flags);
667         }
668
669         /*********************************************
670         if ( !(ch->statusflags & LOWWAIT) &&
671              ((len != count) || (MoxaPortTxFree(port) <= 100)) )
672         ************************************************/
673         ch->statusflags |= LOWWAIT;
674         return (len);
675 }
676
677 static int moxa_write_room(struct tty_struct *tty)
678 {
679         struct moxa_str *ch;
680
681         if (tty->stopped)
682                 return (0);
683         ch = (struct moxa_str *) tty->driver_data;
684         if (ch == NULL)
685                 return (0);
686         return (MoxaPortTxFree(ch->port));
687 }
688
689 static void moxa_flush_buffer(struct tty_struct *tty)
690 {
691         struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
692
693         if (ch == NULL)
694                 return;
695         MoxaPortFlushData(ch->port, 1);
696         tty_wakeup(tty);
697         wake_up_interruptible(&tty->write_wait);
698 }
699
700 static int moxa_chars_in_buffer(struct tty_struct *tty)
701 {
702         int chars;
703         struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
704
705         /*
706          * Sigh...I have to check if driver_data is NULL here, because
707          * if an open() fails, the TTY subsystem eventually calls
708          * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
709          * routine.  And since the open() failed, we return 0 here.  TDJ
710          */
711         if (ch == NULL)
712                 return (0);
713         chars = MoxaPortTxQueue(ch->port);
714         if (chars) {
715                 /*
716                  * Make it possible to wakeup anything waiting for output
717                  * in tty_ioctl.c, etc.
718                  */
719                 if (!(ch->statusflags & EMPTYWAIT))
720                         setup_empty_event(tty);
721         }
722         return (chars);
723 }
724
725 static void moxa_flush_chars(struct tty_struct *tty)
726 {
727         /*
728          * Don't think I need this, because this is called to empty the TX
729          * buffer for the 16450, 16550, etc.
730          */
731 }
732
733 static void moxa_put_char(struct tty_struct *tty, unsigned char c)
734 {
735         struct moxa_str *ch;
736         int port;
737         unsigned long flags;
738
739         ch = (struct moxa_str *) tty->driver_data;
740         if (ch == NULL)
741                 return;
742         port = ch->port;
743         save_flags(flags);
744         cli();
745         moxaXmitBuff[0] = c;
746         MoxaPortWriteData(port, moxaXmitBuff, 1);
747         restore_flags(flags);
748         /************************************************
749         if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
750         *************************************************/
751         ch->statusflags |= LOWWAIT;
752 }
753
754 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
755 {
756         struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
757         int port;
758         int flag = 0, dtr, rts;
759
760         port = PORTNO(tty);
761         if ((port != MAX_PORTS) && (!ch))
762                 return (-EINVAL);
763
764         MoxaPortGetLineOut(ch->port, &dtr, &rts);
765         if (dtr)
766                 flag |= TIOCM_DTR;
767         if (rts)
768                 flag |= TIOCM_RTS;
769         dtr = MoxaPortLineStatus(ch->port);
770         if (dtr & 1)
771                 flag |= TIOCM_CTS;
772         if (dtr & 2)
773                 flag |= TIOCM_DSR;
774         if (dtr & 4)
775                 flag |= TIOCM_CD;
776         return flag;
777 }
778
779 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
780                          unsigned int set, unsigned int clear)
781 {
782         struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
783         int port;
784         int dtr, rts;
785
786         port = PORTNO(tty);
787         if ((port != MAX_PORTS) && (!ch))
788                 return (-EINVAL);
789
790         MoxaPortGetLineOut(ch->port, &dtr, &rts);
791         if (set & TIOCM_RTS)
792                 rts = 1;
793         if (set & TIOCM_DTR)
794                 dtr = 1;
795         if (clear & TIOCM_RTS)
796                 rts = 0;
797         if (clear & TIOCM_DTR)
798                 dtr = 0;
799         MoxaPortLineCtrl(ch->port, dtr, rts);
800         return 0;
801 }
802
803 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
804                       unsigned int cmd, unsigned long arg)
805 {
806         struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
807         register int port;
808         void __user *argp = (void __user *)arg;
809         int retval;
810
811         port = PORTNO(tty);
812         if ((port != MAX_PORTS) && (!ch))
813                 return (-EINVAL);
814
815         switch (cmd) {
816         case TCSBRK:            /* SVID version: non-zero arg --> no break */
817                 retval = tty_check_change(tty);
818                 if (retval)
819                         return (retval);
820                 setup_empty_event(tty);
821                 tty_wait_until_sent(tty, 0);
822                 if (!arg)
823                         MoxaPortSendBreak(ch->port, 0);
824                 return (0);
825         case TCSBRKP:           /* support for POSIX tcsendbreak() */
826                 retval = tty_check_change(tty);
827                 if (retval)
828                         return (retval);
829                 setup_empty_event(tty);
830                 tty_wait_until_sent(tty, 0);
831                 MoxaPortSendBreak(ch->port, arg);
832                 return (0);
833         case TIOCGSOFTCAR:
834                 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) argp);
835         case TIOCSSOFTCAR:
836                 if(get_user(retval, (unsigned long __user *) argp))
837                         return -EFAULT;
838                 arg = retval;
839                 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
840                                          (arg ? CLOCAL : 0));
841                 if (C_CLOCAL(tty))
842                         ch->asyncflags &= ~ASYNC_CHECK_CD;
843                 else
844                         ch->asyncflags |= ASYNC_CHECK_CD;
845                 return (0);
846         case TIOCGSERIAL:
847                 return moxa_get_serial_info(ch, argp);
848
849         case TIOCSSERIAL:
850                 return moxa_set_serial_info(ch, argp);
851         default:
852                 retval = MoxaDriverIoctl(cmd, arg, port);
853         }
854         return (retval);
855 }
856
857 static void moxa_throttle(struct tty_struct *tty)
858 {
859         struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
860
861         ch->statusflags |= THROTTLE;
862 }
863
864 static void moxa_unthrottle(struct tty_struct *tty)
865 {
866         struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
867
868         ch->statusflags &= ~THROTTLE;
869 }
870
871 static void moxa_set_termios(struct tty_struct *tty,
872                              struct termios *old_termios)
873 {
874         struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
875
876         if (ch == NULL)
877                 return;
878         set_tty_param(tty);
879         if (!(old_termios->c_cflag & CLOCAL) &&
880             (tty->termios->c_cflag & CLOCAL))
881                 wake_up_interruptible(&ch->open_wait);
882 }
883
884 static void moxa_stop(struct tty_struct *tty)
885 {
886         struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
887
888         if (ch == NULL)
889                 return;
890         MoxaPortTxDisable(ch->port);
891         ch->statusflags |= TXSTOPPED;
892 }
893
894
895 static void moxa_start(struct tty_struct *tty)
896 {
897         struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
898
899         if (ch == NULL)
900                 return;
901
902         if (!(ch->statusflags & TXSTOPPED))
903                 return;
904
905         MoxaPortTxEnable(ch->port);
906         ch->statusflags &= ~TXSTOPPED;
907 }
908
909 static void moxa_hangup(struct tty_struct *tty)
910 {
911         struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
912
913         moxa_flush_buffer(tty);
914         shut_down(ch);
915         ch->event = 0;
916         ch->count = 0;
917         ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
918         ch->tty = NULL;
919         wake_up_interruptible(&ch->open_wait);
920 }
921
922 static void moxa_poll(unsigned long ignored)
923 {
924         register int card;
925         struct moxa_str *ch;
926         struct tty_struct *tp;
927         int i, ports;
928
929         moxaTimer_on = 0;
930         del_timer(&moxaTimer);
931
932         if (MoxaDriverPoll() < 0) {
933                 moxaTimer.function = moxa_poll;
934                 moxaTimer.expires = jiffies + (HZ / 50);
935                 moxaTimer_on = 1;
936                 add_timer(&moxaTimer);
937                 return;
938         }
939         for (card = 0; card < MAX_BOARDS; card++) {
940                 if ((ports = MoxaPortsOfCard(card)) <= 0)
941                         continue;
942                 ch = &moxaChannels[card * MAX_PORTS_PER_BOARD];
943                 for (i = 0; i < ports; i++, ch++) {
944                         if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
945                                 continue;
946                         if (!(ch->statusflags & THROTTLE) &&
947                             (MoxaPortRxQueue(ch->port) > 0))
948                                 receive_data(ch);
949                         if ((tp = ch->tty) == 0)
950                                 continue;
951                         if (ch->statusflags & LOWWAIT) {
952                                 if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
953                                         if (!tp->stopped) {
954                                                 ch->statusflags &= ~LOWWAIT;
955                                                 tty_wakeup(tp);
956                                                 wake_up_interruptible(&tp->write_wait);
957                                         }
958                                 }
959                         }
960                         if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
961                                 tty_insert_flip_char(tp, 0, TTY_BREAK);
962                                 tty_schedule_flip(tp);
963                         }
964                         if (MoxaPortDCDChange(ch->port)) {
965                                 if (ch->asyncflags & ASYNC_CHECK_CD) {
966                                         if (MoxaPortDCDON(ch->port))
967                                                 wake_up_interruptible(&ch->open_wait);
968                                         else {
969                                                 set_bit(MOXA_EVENT_HANGUP, &ch->event);
970                                                 schedule_work(&ch->tqueue);
971                                         }
972                                 }
973                         }
974                 }
975         }
976
977         moxaTimer.function = moxa_poll;
978         moxaTimer.expires = jiffies + (HZ / 50);
979         moxaTimer_on = 1;
980         add_timer(&moxaTimer);
981 }
982
983 /******************************************************************************/
984
985 static void set_tty_param(struct tty_struct *tty)
986 {
987         register struct termios *ts;
988         struct moxa_str *ch;
989         int rts, cts, txflow, rxflow, xany;
990
991         ch = (struct moxa_str *) tty->driver_data;
992         ts = tty->termios;
993         if (ts->c_cflag & CLOCAL)
994                 ch->asyncflags &= ~ASYNC_CHECK_CD;
995         else
996                 ch->asyncflags |= ASYNC_CHECK_CD;
997         rts = cts = txflow = rxflow = xany = 0;
998         if (ts->c_cflag & CRTSCTS)
999                 rts = cts = 1;
1000         if (ts->c_iflag & IXON)
1001                 txflow = 1;
1002         if (ts->c_iflag & IXOFF)
1003                 rxflow = 1;
1004         if (ts->c_iflag & IXANY)
1005                 xany = 1;
1006         MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
1007         MoxaPortSetTermio(ch->port, ts);
1008 }
1009
1010 static int block_till_ready(struct tty_struct *tty, struct file *filp,
1011                             struct moxa_str *ch)
1012 {
1013         DECLARE_WAITQUEUE(wait,current);
1014         unsigned long flags;
1015         int retval;
1016         int do_clocal = C_CLOCAL(tty);
1017
1018         /*
1019          * If the device is in the middle of being closed, then block
1020          * until it's done, and then try again.
1021          */
1022         if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
1023                 if (ch->asyncflags & ASYNC_CLOSING)
1024                         interruptible_sleep_on(&ch->close_wait);
1025 #ifdef SERIAL_DO_RESTART
1026                 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1027                         return (-EAGAIN);
1028                 else
1029                         return (-ERESTARTSYS);
1030 #else
1031                 return (-EAGAIN);
1032 #endif
1033         }
1034         /*
1035          * If non-blocking mode is set, then make the check up front
1036          * and then exit.
1037          */
1038         if (filp->f_flags & O_NONBLOCK) {
1039                 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1040                 return (0);
1041         }
1042         /*
1043          * Block waiting for the carrier detect and the line to become free
1044          */
1045         retval = 0;
1046         add_wait_queue(&ch->open_wait, &wait);
1047 #ifdef SERIAL_DEBUG_OPEN
1048         printk("block_til_ready before block: ttys%d, count = %d\n",
1049                ch->line, ch->count);
1050 #endif
1051         save_flags(flags);
1052         cli();
1053         if (!tty_hung_up_p(filp))
1054                 ch->count--;
1055         restore_flags(flags);
1056         ch->blocked_open++;
1057         while (1) {
1058                 set_current_state(TASK_INTERRUPTIBLE);
1059                 if (tty_hung_up_p(filp) ||
1060                     !(ch->asyncflags & ASYNC_INITIALIZED)) {
1061 #ifdef SERIAL_DO_RESTART
1062                         if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1063                                 retval = -EAGAIN;
1064                         else
1065                                 retval = -ERESTARTSYS;
1066 #else
1067                         retval = -EAGAIN;
1068 #endif
1069                         break;
1070                 }
1071                 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
1072                                                 MoxaPortDCDON(ch->port)))
1073                         break;
1074
1075                 if (signal_pending(current)) {
1076                         retval = -ERESTARTSYS;
1077                         break;
1078                 }
1079                 schedule();
1080         }
1081         set_current_state(TASK_RUNNING);
1082         remove_wait_queue(&ch->open_wait, &wait);
1083         if (!tty_hung_up_p(filp))
1084                 ch->count++;
1085         ch->blocked_open--;
1086 #ifdef SERIAL_DEBUG_OPEN
1087         printk("block_til_ready after blocking: ttys%d, count = %d\n",
1088                ch->line, ch->count);
1089 #endif
1090         if (retval)
1091                 return (retval);
1092         ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1093         return (0);
1094 }
1095
1096 static void setup_empty_event(struct tty_struct *tty)
1097 {
1098         struct moxa_str *ch = tty->driver_data;
1099         unsigned long flags;
1100
1101         save_flags(flags);
1102         cli();
1103         ch->statusflags |= EMPTYWAIT;
1104         moxaEmptyTimer_on[ch->port] = 0;
1105         del_timer(&moxaEmptyTimer[ch->port]);
1106         moxaEmptyTimer[ch->port].expires = jiffies + HZ;
1107         moxaEmptyTimer_on[ch->port] = 1;
1108         add_timer(&moxaEmptyTimer[ch->port]);
1109         restore_flags(flags);
1110 }
1111
1112 static void check_xmit_empty(unsigned long data)
1113 {
1114         struct moxa_str *ch;
1115
1116         ch = (struct moxa_str *) data;
1117         moxaEmptyTimer_on[ch->port] = 0;
1118         del_timer(&moxaEmptyTimer[ch->port]);
1119         if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
1120                 if (MoxaPortTxQueue(ch->port) == 0) {
1121                         ch->statusflags &= ~EMPTYWAIT;
1122                         tty_wakeup(ch->tty);
1123                         wake_up_interruptible(&ch->tty->write_wait);
1124                         return;
1125                 }
1126                 moxaEmptyTimer[ch->port].expires = jiffies + HZ;
1127                 moxaEmptyTimer_on[ch->port] = 1;
1128                 add_timer(&moxaEmptyTimer[ch->port]);
1129         } else
1130                 ch->statusflags &= ~EMPTYWAIT;
1131 }
1132
1133 static void shut_down(struct moxa_str *ch)
1134 {
1135         struct tty_struct *tp;
1136
1137         if (!(ch->asyncflags & ASYNC_INITIALIZED))
1138                 return;
1139
1140         tp = ch->tty;
1141
1142         MoxaPortDisable(ch->port);
1143
1144         /*
1145          * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1146          */
1147         if (tp->termios->c_cflag & HUPCL)
1148                 MoxaPortLineCtrl(ch->port, 0, 0);
1149
1150         ch->asyncflags &= ~ASYNC_INITIALIZED;
1151 }
1152
1153 static void receive_data(struct moxa_str *ch)
1154 {
1155         struct tty_struct *tp;
1156         struct termios *ts;
1157         int i, count, rc, space;
1158         unsigned char *charptr, *flagptr;
1159         unsigned long flags;
1160
1161         ts = NULL;
1162         tp = ch->tty;
1163         if (tp)
1164                 ts = tp->termios;
1165         /**************************************************
1166         if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1167         *****************************************************/
1168         if (!tp || !ts) {
1169                 MoxaPortFlushData(ch->port, 0);
1170                 return;
1171         }
1172         space = TTY_FLIPBUF_SIZE - tp->flip.count;
1173         if (space <= 0)
1174                 return;
1175         charptr = tp->flip.char_buf_ptr;
1176         flagptr = tp->flip.flag_buf_ptr;
1177         rc = tp->flip.count;
1178         save_flags(flags);
1179         cli();
1180         count = MoxaPortReadData(ch->port, charptr, space);
1181         restore_flags(flags);
1182         for (i = 0; i < count; i++)
1183                 *flagptr++ = 0;
1184         charptr += count;
1185         rc += count;
1186         tp->flip.count = rc;
1187         tp->flip.char_buf_ptr = charptr;
1188         tp->flip.flag_buf_ptr = flagptr;
1189         tty_schedule_flip(ch->tty);
1190 }
1191
1192 #define Magic_code      0x404
1193
1194 /*
1195  *    System Configuration
1196  */
1197 /*
1198  *    for C218 BIOS initialization
1199  */
1200 #define C218_ConfBase   0x800
1201 #define C218_status     (C218_ConfBase + 0)     /* BIOS running status    */
1202 #define C218_diag       (C218_ConfBase + 2)     /* diagnostic status      */
1203 #define C218_key        (C218_ConfBase + 4)     /* WORD (0x218 for C218) */
1204 #define C218DLoad_len   (C218_ConfBase + 6)     /* WORD           */
1205 #define C218check_sum   (C218_ConfBase + 8)     /* BYTE           */
1206 #define C218chksum_ok   (C218_ConfBase + 0x0a)  /* BYTE (1:ok)            */
1207 #define C218_TestRx     (C218_ConfBase + 0x10)  /* 8 bytes for 8 ports    */
1208 #define C218_TestTx     (C218_ConfBase + 0x18)  /* 8 bytes for 8 ports    */
1209 #define C218_RXerr      (C218_ConfBase + 0x20)  /* 8 bytes for 8 ports    */
1210 #define C218_ErrFlag    (C218_ConfBase + 0x28)  /* 8 bytes for 8 ports    */
1211
1212 #define C218_LoadBuf    0x0F00
1213 #define C218_KeyCode    0x218
1214 #define CP204J_KeyCode  0x204
1215
1216 /*
1217  *    for C320 BIOS initialization
1218  */
1219 #define C320_ConfBase   0x800
1220 #define C320_LoadBuf    0x0f00
1221 #define STS_init        0x05    /* for C320_status        */
1222
1223 #define C320_status     C320_ConfBase + 0       /* BIOS running status    */
1224 #define C320_diag       C320_ConfBase + 2       /* diagnostic status      */
1225 #define C320_key        C320_ConfBase + 4       /* WORD (0320H for C320) */
1226 #define C320DLoad_len   C320_ConfBase + 6       /* WORD           */
1227 #define C320check_sum   C320_ConfBase + 8       /* WORD           */
1228 #define C320chksum_ok   C320_ConfBase + 0x0a    /* WORD (1:ok)            */
1229 #define C320bapi_len    C320_ConfBase + 0x0c    /* WORD           */
1230 #define C320UART_no     C320_ConfBase + 0x0e    /* WORD           */
1231
1232 #define C320_KeyCode    0x320
1233
1234 #define FixPage_addr    0x0000  /* starting addr of static page  */
1235 #define DynPage_addr    0x2000  /* starting addr of dynamic page */
1236 #define C218_start      0x3000  /* starting addr of C218 BIOS prg */
1237 #define Control_reg     0x1ff0  /* select page and reset control */
1238 #define HW_reset        0x80
1239
1240 /*
1241  *    Function Codes
1242  */
1243 #define FC_CardReset    0x80
1244 #define FC_ChannelReset 1       /* C320 firmware not supported */
1245 #define FC_EnableCH     2
1246 #define FC_DisableCH    3
1247 #define FC_SetParam     4
1248 #define FC_SetMode      5
1249 #define FC_SetRate      6
1250 #define FC_LineControl  7
1251 #define FC_LineStatus   8
1252 #define FC_XmitControl  9
1253 #define FC_FlushQueue   10
1254 #define FC_SendBreak    11
1255 #define FC_StopBreak    12
1256 #define FC_LoopbackON   13
1257 #define FC_LoopbackOFF  14
1258 #define FC_ClrIrqTable  15
1259 #define FC_SendXon      16
1260 #define FC_SetTermIrq   17      /* C320 firmware not supported */
1261 #define FC_SetCntIrq    18      /* C320 firmware not supported */
1262 #define FC_SetBreakIrq  19
1263 #define FC_SetLineIrq   20
1264 #define FC_SetFlowCtl   21
1265 #define FC_GenIrq       22
1266 #define FC_InCD180      23
1267 #define FC_OutCD180     24
1268 #define FC_InUARTreg    23
1269 #define FC_OutUARTreg   24
1270 #define FC_SetXonXoff   25
1271 #define FC_OutCD180CCR  26
1272 #define FC_ExtIQueue    27
1273 #define FC_ExtOQueue    28
1274 #define FC_ClrLineIrq   29
1275 #define FC_HWFlowCtl    30
1276 #define FC_GetClockRate 35
1277 #define FC_SetBaud      36
1278 #define FC_SetDataMode  41
1279 #define FC_GetCCSR      43
1280 #define FC_GetDataError 45
1281 #define FC_RxControl    50
1282 #define FC_ImmSend      51
1283 #define FC_SetXonState  52
1284 #define FC_SetXoffState 53
1285 #define FC_SetRxFIFOTrig 54
1286 #define FC_SetTxFIFOCnt 55
1287 #define FC_UnixRate     56
1288 #define FC_UnixResetTimer 57
1289
1290 #define RxFIFOTrig1     0
1291 #define RxFIFOTrig4     1
1292 #define RxFIFOTrig8     2
1293 #define RxFIFOTrig14    3
1294
1295 /*
1296  *    Dual-Ported RAM
1297  */
1298 #define DRAM_global     0
1299 #define INT_data        (DRAM_global + 0)
1300 #define Config_base     (DRAM_global + 0x108)
1301
1302 #define IRQindex        (INT_data + 0)
1303 #define IRQpending      (INT_data + 4)
1304 #define IRQtable        (INT_data + 8)
1305
1306 /*
1307  *    Interrupt Status
1308  */
1309 #define IntrRx          0x01    /* receiver data O.K.             */
1310 #define IntrTx          0x02    /* transmit buffer empty  */
1311 #define IntrFunc        0x04    /* function complete              */
1312 #define IntrBreak       0x08    /* received break         */
1313 #define IntrLine        0x10    /* line status change
1314                                    for transmitter                */
1315 #define IntrIntr        0x20    /* received INTR code             */
1316 #define IntrQuit        0x40    /* received QUIT code             */
1317 #define IntrEOF         0x80    /* received EOF code              */
1318
1319 #define IntrRxTrigger   0x100   /* rx data count reach tigger value */
1320 #define IntrTxTrigger   0x200   /* tx data count below trigger value */
1321
1322 #define Magic_no        (Config_base + 0)
1323 #define Card_model_no   (Config_base + 2)
1324 #define Total_ports     (Config_base + 4)
1325 #define Module_cnt      (Config_base + 8)
1326 #define Module_no       (Config_base + 10)
1327 #define Timer_10ms      (Config_base + 14)
1328 #define Disable_IRQ     (Config_base + 20)
1329 #define TMS320_PORT1    (Config_base + 22)
1330 #define TMS320_PORT2    (Config_base + 24)
1331 #define TMS320_CLOCK    (Config_base + 26)
1332
1333 /*
1334  *    DATA BUFFER in DRAM
1335  */
1336 #define Extern_table    0x400   /* Base address of the external table
1337                                    (24 words *    64) total 3K bytes
1338                                    (24 words * 128) total 6K bytes */
1339 #define Extern_size     0x60    /* 96 bytes                       */
1340 #define RXrptr          0x00    /* read pointer for RX buffer     */
1341 #define RXwptr          0x02    /* write pointer for RX buffer    */
1342 #define TXrptr          0x04    /* read pointer for TX buffer     */
1343 #define TXwptr          0x06    /* write pointer for TX buffer    */
1344 #define HostStat        0x08    /* IRQ flag and general flag      */
1345 #define FlagStat        0x0A
1346 #define FlowControl     0x0C    /* B7 B6 B5 B4 B3 B2 B1 B0              */
1347                                         /*  x  x  x  x  |  |  |  |            */
1348                                         /*              |  |  |  + CTS flow   */
1349                                         /*              |  |  +--- RTS flow   */
1350                                         /*              |  +------ TX Xon/Xoff */
1351                                         /*              +--------- RX Xon/Xoff */
1352 #define Break_cnt       0x0E    /* received break count   */
1353 #define CD180TXirq      0x10    /* if non-0: enable TX irq        */
1354 #define RX_mask         0x12
1355 #define TX_mask         0x14
1356 #define Ofs_rxb         0x16
1357 #define Ofs_txb         0x18
1358 #define Page_rxb        0x1A
1359 #define Page_txb        0x1C
1360 #define EndPage_rxb     0x1E
1361 #define EndPage_txb     0x20
1362 #define Data_error      0x22
1363 #define RxTrigger       0x28
1364 #define TxTrigger       0x2a
1365
1366 #define rRXwptr         0x34
1367 #define Low_water       0x36
1368
1369 #define FuncCode        0x40
1370 #define FuncArg         0x42
1371 #define FuncArg1        0x44
1372
1373 #define C218rx_size     0x2000  /* 8K bytes */
1374 #define C218tx_size     0x8000  /* 32K bytes */
1375
1376 #define C218rx_mask     (C218rx_size - 1)
1377 #define C218tx_mask     (C218tx_size - 1)
1378
1379 #define C320p8rx_size   0x2000
1380 #define C320p8tx_size   0x8000
1381 #define C320p8rx_mask   (C320p8rx_size - 1)
1382 #define C320p8tx_mask   (C320p8tx_size - 1)
1383
1384 #define C320p16rx_size  0x2000
1385 #define C320p16tx_size  0x4000
1386 #define C320p16rx_mask  (C320p16rx_size - 1)
1387 #define C320p16tx_mask  (C320p16tx_size - 1)
1388
1389 #define C320p24rx_size  0x2000
1390 #define C320p24tx_size  0x2000
1391 #define C320p24rx_mask  (C320p24rx_size - 1)
1392 #define C320p24tx_mask  (C320p24tx_size - 1)
1393
1394 #define C320p32rx_size  0x1000
1395 #define C320p32tx_size  0x1000
1396 #define C320p32rx_mask  (C320p32rx_size - 1)
1397 #define C320p32tx_mask  (C320p32tx_size - 1)
1398
1399 #define Page_size       0x2000
1400 #define Page_mask       (Page_size - 1)
1401 #define C218rx_spage    3
1402 #define C218tx_spage    4
1403 #define C218rx_pageno   1
1404 #define C218tx_pageno   4
1405 #define C218buf_pageno  5
1406
1407 #define C320p8rx_spage  3
1408 #define C320p8tx_spage  4
1409 #define C320p8rx_pgno   1
1410 #define C320p8tx_pgno   4
1411 #define C320p8buf_pgno  5
1412
1413 #define C320p16rx_spage 3
1414 #define C320p16tx_spage 4
1415 #define C320p16rx_pgno  1
1416 #define C320p16tx_pgno  2
1417 #define C320p16buf_pgno 3
1418
1419 #define C320p24rx_spage 3
1420 #define C320p24tx_spage 4
1421 #define C320p24rx_pgno  1
1422 #define C320p24tx_pgno  1
1423 #define C320p24buf_pgno 2
1424
1425 #define C320p32rx_spage 3
1426 #define C320p32tx_ofs   C320p32rx_size
1427 #define C320p32tx_spage 3
1428 #define C320p32buf_pgno 1
1429
1430 /*
1431  *    Host Status
1432  */
1433 #define WakeupRx        0x01
1434 #define WakeupTx        0x02
1435 #define WakeupBreak     0x08
1436 #define WakeupLine      0x10
1437 #define WakeupIntr      0x20
1438 #define WakeupQuit      0x40
1439 #define WakeupEOF       0x80    /* used in VTIME control */
1440 #define WakeupRxTrigger 0x100
1441 #define WakeupTxTrigger 0x200
1442 /*
1443  *    Flag status
1444  */
1445 #define Rx_over         0x01
1446 #define Xoff_state      0x02
1447 #define Tx_flowOff      0x04
1448 #define Tx_enable       0x08
1449 #define CTS_state       0x10
1450 #define DSR_state       0x20
1451 #define DCD_state       0x80
1452 /*
1453  *    FlowControl
1454  */
1455 #define CTS_FlowCtl     1
1456 #define RTS_FlowCtl     2
1457 #define Tx_FlowCtl      4
1458 #define Rx_FlowCtl      8
1459 #define IXM_IXANY       0x10
1460
1461 #define LowWater        128
1462
1463 #define DTR_ON          1
1464 #define RTS_ON          2
1465 #define CTS_ON          1
1466 #define DSR_ON          2
1467 #define DCD_ON          8
1468
1469 /* mode definition */
1470 #define MX_CS8          0x03
1471 #define MX_CS7          0x02
1472 #define MX_CS6          0x01
1473 #define MX_CS5          0x00
1474
1475 #define MX_STOP1        0x00
1476 #define MX_STOP15       0x04
1477 #define MX_STOP2        0x08
1478
1479 #define MX_PARNONE      0x00
1480 #define MX_PAREVEN      0x40
1481 #define MX_PARODD       0xC0
1482
1483 /*
1484  *    Query
1485  */
1486 #define QueryPort       MAX_PORTS
1487
1488
1489
1490 struct mon_str {
1491         int tick;
1492         int rxcnt[MAX_PORTS];
1493         int txcnt[MAX_PORTS];
1494 };
1495 typedef struct mon_str mon_st;
1496
1497 #define         DCD_changed     0x01
1498 #define         DCD_oldstate    0x80
1499
1500 static unsigned char moxaBuff[10240];
1501 static unsigned long moxaIntNdx[MAX_BOARDS];
1502 static unsigned long moxaIntPend[MAX_BOARDS];
1503 static unsigned long moxaIntTable[MAX_BOARDS];
1504 static char moxaChkPort[MAX_PORTS];
1505 static char moxaLineCtrl[MAX_PORTS];
1506 static unsigned long moxaTableAddr[MAX_PORTS];
1507 static long moxaCurBaud[MAX_PORTS];
1508 static char moxaDCDState[MAX_PORTS];
1509 static char moxaLowChkFlag[MAX_PORTS];
1510 static int moxaLowWaterChk;
1511 static int moxaCard;
1512 static mon_st moxaLog;
1513 static int moxaFuncTout;
1514 static ushort moxaBreakCnt[MAX_PORTS];
1515
1516 static void moxadelay(int);
1517 static void moxafunc(unsigned long, int, ushort);
1518 static void wait_finish(unsigned long);
1519 static void low_water_check(unsigned long);
1520 static int moxaloadbios(int, unsigned char __user *, int);
1521 static int moxafindcard(int);
1522 static int moxaload320b(int, unsigned char __user *, int);
1523 static int moxaloadcode(int, unsigned char __user *, int);
1524 static int moxaloadc218(int, unsigned long, int);
1525 static int moxaloadc320(int, unsigned long, int, int *);
1526
1527 /*****************************************************************************
1528  *      Driver level functions:                                              *
1529  *      1. MoxaDriverInit(void);                                             *
1530  *      2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);   *
1531  *      3. MoxaDriverPoll(void);                                             *
1532  *****************************************************************************/
1533 void MoxaDriverInit(void)
1534 {
1535         int i;
1536
1537         moxaFuncTout = HZ / 2;  /* 500 mini-seconds */
1538         moxaCard = 0;
1539         moxaLog.tick = 0;
1540         moxaLowWaterChk = 0;
1541         for (i = 0; i < MAX_PORTS; i++) {
1542                 moxaChkPort[i] = 0;
1543                 moxaLowChkFlag[i] = 0;
1544                 moxaLineCtrl[i] = 0;
1545                 moxaLog.rxcnt[i] = 0;
1546                 moxaLog.txcnt[i] = 0;
1547         }
1548 }
1549
1550 #define MOXA            0x400
1551 #define MOXA_GET_IQUEUE         (MOXA + 1)      /* get input buffered count */
1552 #define MOXA_GET_OQUEUE         (MOXA + 2)      /* get output buffered count */
1553 #define MOXA_INIT_DRIVER        (MOXA + 6)      /* moxaCard=0 */
1554 #define MOXA_LOAD_BIOS          (MOXA + 9)      /* download BIOS */
1555 #define MOXA_FIND_BOARD         (MOXA + 10)     /* Check if MOXA card exist? */
1556 #define MOXA_LOAD_C320B         (MOXA + 11)     /* download 320B firmware */
1557 #define MOXA_LOAD_CODE          (MOXA + 12)     /* download firmware */
1558 #define MOXA_GETDATACOUNT       (MOXA + 23)
1559 #define MOXA_GET_IOQUEUE        (MOXA + 27)
1560 #define MOXA_FLUSH_QUEUE        (MOXA + 28)
1561 #define MOXA_GET_CONF           (MOXA + 35)     /* configuration */
1562 #define MOXA_GET_MAJOR          (MOXA + 63)
1563 #define MOXA_GET_CUMAJOR        (MOXA + 64)
1564 #define MOXA_GETMSTATUS         (MOXA + 65)
1565
1566
1567 struct moxaq_str {
1568         int inq;
1569         int outq;
1570 };
1571
1572 struct dl_str {
1573         char __user *buf;
1574         int len;
1575         int cardno;
1576 };
1577
1578 static struct moxaq_str temp_queue[MAX_PORTS];
1579 static struct dl_str dltmp;
1580
1581 void MoxaPortFlushData(int port, int mode)
1582 {
1583         unsigned long ofsAddr;
1584         if ((mode < 0) || (mode > 2))
1585                 return;
1586         ofsAddr = moxaTableAddr[port];
1587         moxafunc(ofsAddr, FC_FlushQueue, mode);
1588         if (mode != 1) {
1589                 moxaLowChkFlag[port] = 0;
1590                 low_water_check(ofsAddr);
1591         }
1592 }
1593
1594 int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
1595 {
1596         int i;
1597         int status;
1598         int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
1599         void __user *argp = (void __user *)arg;
1600
1601         if (port == QueryPort) {
1602                 if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_INIT_DRIVER) &&
1603                     (cmd != MOXA_LOAD_BIOS) && (cmd != MOXA_FIND_BOARD) && (cmd != MOXA_LOAD_C320B) &&
1604                  (cmd != MOXA_LOAD_CODE) && (cmd != MOXA_GETDATACOUNT) &&
1605                   (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
1606                     (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
1607                         return (-EINVAL);
1608         }
1609         switch (cmd) {
1610         case MOXA_GET_CONF:
1611                 if(copy_to_user(argp, &moxa_boards, MAX_BOARDS * sizeof(moxa_board_conf)))
1612                         return -EFAULT;
1613                 return (0);
1614         case MOXA_INIT_DRIVER:
1615                 if ((int) arg == 0x404)
1616                         MoxaDriverInit();
1617                 return (0);
1618         case MOXA_GETDATACOUNT:
1619                 moxaLog.tick = jiffies;
1620                 if(copy_to_user(argp, &moxaLog, sizeof(mon_st)))
1621                         return -EFAULT;
1622                 return (0);
1623         case MOXA_FLUSH_QUEUE:
1624                 MoxaPortFlushData(port, arg);
1625                 return (0);
1626         case MOXA_GET_IOQUEUE:
1627                 for (i = 0; i < MAX_PORTS; i++) {
1628                         if (moxaChkPort[i]) {
1629                                 temp_queue[i].inq = MoxaPortRxQueue(i);
1630                                 temp_queue[i].outq = MoxaPortTxQueue(i);
1631                         }
1632                 }
1633                 if(copy_to_user(argp, temp_queue, sizeof(struct moxaq_str) * MAX_PORTS))
1634                         return -EFAULT;
1635                 return (0);
1636         case MOXA_GET_OQUEUE:
1637                 i = MoxaPortTxQueue(port);
1638                 return put_user(i, (unsigned long __user *)argp);
1639         case MOXA_GET_IQUEUE:
1640                 i = MoxaPortRxQueue(port);
1641                 return put_user(i, (unsigned long __user *)argp);
1642         case MOXA_GET_MAJOR:
1643                 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1644                         return -EFAULT;
1645                 return 0;
1646         case MOXA_GET_CUMAJOR:
1647                 i = 0;
1648                 if(copy_to_user(argp, &i, sizeof(int)))
1649                         return -EFAULT;
1650                 return 0;
1651         case MOXA_GETMSTATUS:
1652                 for (i = 0; i < MAX_PORTS; i++) {
1653                         GMStatus[i].ri = 0;
1654                         GMStatus[i].dcd = 0;
1655                         GMStatus[i].dsr = 0;
1656                         GMStatus[i].cts = 0;
1657                         if (!moxaChkPort[i]) {
1658                                 continue;
1659                         } else {
1660                                 status = MoxaPortLineStatus(moxaChannels[i].port);
1661                                 if (status & 1)
1662                                         GMStatus[i].cts = 1;
1663                                 if (status & 2)
1664                                         GMStatus[i].dsr = 1;
1665                                 if (status & 4)
1666                                         GMStatus[i].dcd = 1;
1667                         }
1668
1669                         if (!moxaChannels[i].tty || !moxaChannels[i].tty->termios)
1670                                 GMStatus[i].cflag = moxaChannels[i].cflag;
1671                         else
1672                                 GMStatus[i].cflag = moxaChannels[i].tty->termios->c_cflag;
1673                 }
1674                 if(copy_to_user(argp, GMStatus, sizeof(struct mxser_mstatus) * MAX_PORTS))
1675                         return -EFAULT;
1676                 return 0;
1677         default:
1678                 return (-ENOIOCTLCMD);
1679         case MOXA_LOAD_BIOS:
1680         case MOXA_FIND_BOARD:
1681         case MOXA_LOAD_C320B:
1682         case MOXA_LOAD_CODE:
1683                 break;
1684         }
1685
1686         if(copy_from_user(&dltmp, argp, sizeof(struct dl_str)))
1687                 return -EFAULT;
1688         if(dltmp.cardno < 0 || dltmp.cardno >= MAX_BOARDS)
1689                 return -EINVAL;
1690
1691         switch(cmd)
1692         {
1693         case MOXA_LOAD_BIOS:
1694                 i = moxaloadbios(dltmp.cardno, dltmp.buf, dltmp.len);
1695                 return (i);
1696         case MOXA_FIND_BOARD:
1697                 return moxafindcard(dltmp.cardno);
1698         case MOXA_LOAD_C320B:
1699                 moxaload320b(dltmp.cardno, dltmp.buf, dltmp.len);
1700         default: /* to keep gcc happy */
1701                 return (0);
1702         case MOXA_LOAD_CODE:
1703                 i = moxaloadcode(dltmp.cardno, dltmp.buf, dltmp.len);
1704                 if (i == -1)
1705                         return (-EFAULT);
1706                 return (i);
1707
1708         }
1709 }
1710
1711 int MoxaDriverPoll(void)
1712 {
1713         register ushort temp;
1714         register int card;
1715         unsigned long ip, ofsAddr;
1716         int port, p, ports;
1717
1718         if (moxaCard == 0)
1719                 return (-1);
1720         for (card = 0; card < MAX_BOARDS; card++) {
1721                 if ((ports = moxa_boards[card].numPorts) == 0)
1722                         continue;
1723                 if (readb(moxaIntPend[card]) == 0xff) {
1724                         ip = moxaIntTable[card] + readb(moxaIntNdx[card]);
1725                         p = card * MAX_PORTS_PER_BOARD;
1726                         ports <<= 1;
1727                         for (port = 0; port < ports; port += 2, p++) {
1728                                 if ((temp = readw(ip + port)) != 0) {
1729                                         writew(0, ip + port);
1730                                         ofsAddr = moxaTableAddr[p];
1731                                         if (temp & IntrTx)
1732                                                 writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
1733                                         if (temp & IntrBreak) {
1734                                                 moxaBreakCnt[p]++;
1735                                         }
1736                                         if (temp & IntrLine) {
1737                                                 if (readb(ofsAddr + FlagStat) & DCD_state) {
1738                                                         if ((moxaDCDState[p] & DCD_oldstate) == 0)
1739                                                                 moxaDCDState[p] = (DCD_oldstate |
1740                                                                                    DCD_changed);
1741                                                 } else {
1742                                                         if (moxaDCDState[p] & DCD_oldstate)
1743                                                                 moxaDCDState[p] = DCD_changed;
1744                                                 }
1745                                         }
1746                                 }
1747                         }
1748                         writeb(0, moxaIntPend[card]);
1749                 }
1750                 if (moxaLowWaterChk) {
1751                         p = card * MAX_PORTS_PER_BOARD;
1752                         for (port = 0; port < ports; port++, p++) {
1753                                 if (moxaLowChkFlag[p]) {
1754                                         moxaLowChkFlag[p] = 0;
1755                                         ofsAddr = moxaTableAddr[p];
1756                                         low_water_check(ofsAddr);
1757                                 }
1758                         }
1759                 }
1760         }
1761         moxaLowWaterChk = 0;
1762         return (0);
1763 }
1764
1765 /*****************************************************************************
1766  *      Card level function:                                                 *
1767  *      1. MoxaPortsOfCard(int cardno);                                      *
1768  *****************************************************************************/
1769 int MoxaPortsOfCard(int cardno)
1770 {
1771
1772         if (moxa_boards[cardno].boardType == 0)
1773                 return (0);
1774         return (moxa_boards[cardno].numPorts);
1775 }
1776
1777 /*****************************************************************************
1778  *      Port level functions:                                                *
1779  *      1.  MoxaPortIsValid(int port);                                       *
1780  *      2.  MoxaPortEnable(int port);                                        *
1781  *      3.  MoxaPortDisable(int port);                                       *
1782  *      4.  MoxaPortGetMaxBaud(int port);                                    *
1783  *      5.  MoxaPortGetCurBaud(int port);                                    *
1784  *      6.  MoxaPortSetBaud(int port, long baud);                            *
1785  *      7.  MoxaPortSetMode(int port, int databit, int stopbit, int parity); *
1786  *      8.  MoxaPortSetTermio(int port, unsigned char *termio);              *
1787  *      9.  MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);      *
1788  *      10. MoxaPortLineCtrl(int port, int dtrState, int rtsState);          *
1789  *      11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany);    *
1790  *      12. MoxaPortLineStatus(int port);                                    *
1791  *      13. MoxaPortDCDChange(int port);                                     *
1792  *      14. MoxaPortDCDON(int port);                                         *
1793  *      15. MoxaPortFlushData(int port, int mode);                           *
1794  *      16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1795  *      17. MoxaPortReadData(int port, unsigned char * buffer, int length);  *
1796  *      18. MoxaPortTxBufSize(int port);                                     *
1797  *      19. MoxaPortRxBufSize(int port);                                     *
1798  *      20. MoxaPortTxQueue(int port);                                       *
1799  *      21. MoxaPortTxFree(int port);                                        *
1800  *      22. MoxaPortRxQueue(int port);                                       *
1801  *      23. MoxaPortRxFree(int port);                                        *
1802  *      24. MoxaPortTxDisable(int port);                                     *
1803  *      25. MoxaPortTxEnable(int port);                                      *
1804  *      26. MoxaPortGetBrkCnt(int port);                                     *
1805  *      27. MoxaPortResetBrkCnt(int port);                                   *
1806  *      28. MoxaPortSetXonXoff(int port, int xonValue, int xoffValue);       *
1807  *      29. MoxaPortIsTxHold(int port);                                      *
1808  *      30. MoxaPortSendBreak(int port, int ticks);                          *
1809  *****************************************************************************/
1810 /*
1811  *    Moxa Port Number Description:
1812  *
1813  *      MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1814  *      the port number using in MOXA driver functions will be 0 to 31 for
1815  *      first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1816  *      to 127 for fourth. For example, if you setup three MOXA boards,
1817  *      first board is C218, second board is C320-16 and third board is
1818  *      C320-32. The port number of first board (C218 - 8 ports) is from
1819  *      0 to 7. The port number of second board (C320 - 16 ports) is form
1820  *      32 to 47. The port number of third board (C320 - 32 ports) is from
1821  *      64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1822  *      127 will be invalid.
1823  *
1824  *
1825  *      Moxa Functions Description:
1826  *
1827  *      Function 1:     Driver initialization routine, this routine must be
1828  *                      called when initialized driver.
1829  *      Syntax:
1830  *      void MoxaDriverInit();
1831  *
1832  *
1833  *      Function 2:     Moxa driver private IOCTL command processing.
1834  *      Syntax:
1835  *      int  MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1836  *
1837  *           unsigned int cmd   : IOCTL command
1838  *           unsigned long arg  : IOCTL argument
1839  *           int port           : port number (0 - 127)
1840  *
1841  *           return:    0  (OK)
1842  *                      -EINVAL
1843  *                      -ENOIOCTLCMD
1844  *
1845  *
1846  *      Function 3:     Moxa driver polling process routine.
1847  *      Syntax:
1848  *      int  MoxaDriverPoll(void);
1849  *
1850  *           return:    0       ; polling O.K.
1851  *                      -1      : no any Moxa card.             
1852  *
1853  *
1854  *      Function 4:     Get the ports of this card.
1855  *      Syntax:
1856  *      int  MoxaPortsOfCard(int cardno);
1857  *
1858  *           int cardno         : card number (0 - 3)
1859  *
1860  *           return:    0       : this card is invalid
1861  *                      8/16/24/32
1862  *
1863  *
1864  *      Function 5:     Check this port is valid or invalid
1865  *      Syntax:
1866  *      int  MoxaPortIsValid(int port);
1867  *           int port           : port number (0 - 127, ref port description)
1868  *
1869  *           return:    0       : this port is invalid
1870  *                      1       : this port is valid
1871  *
1872  *
1873  *      Function 6:     Enable this port to start Tx/Rx data.
1874  *      Syntax:
1875  *      void MoxaPortEnable(int port);
1876  *           int port           : port number (0 - 127)
1877  *
1878  *
1879  *      Function 7:     Disable this port
1880  *      Syntax:
1881  *      void MoxaPortDisable(int port);
1882  *           int port           : port number (0 - 127)
1883  *
1884  *
1885  *      Function 8:     Get the maximun available baud rate of this port.
1886  *      Syntax:
1887  *      long MoxaPortGetMaxBaud(int port);
1888  *           int port           : port number (0 - 127)
1889  *
1890  *           return:    0       : this port is invalid
1891  *                      38400/57600/115200 bps
1892  *
1893  *
1894  *      Function 9:     Get the current baud rate of this port.
1895  *      Syntax:
1896  *      long MoxaPortGetCurBaud(int port);
1897  *           int port           : port number (0 - 127)
1898  *
1899  *           return:    0       : this port is invalid
1900  *                      50 - 115200 bps
1901  *
1902  *
1903  *      Function 10:    Setting baud rate of this port.
1904  *      Syntax:
1905  *      long MoxaPortSetBaud(int port, long baud);
1906  *           int port           : port number (0 - 127)
1907  *           long baud          : baud rate (50 - 115200)
1908  *
1909  *           return:    0       : this port is invalid or baud < 50
1910  *                      50 - 115200 : the real baud rate set to the port, if
1911  *                                    the argument baud is large than maximun
1912  *                                    available baud rate, the real setting
1913  *                                    baud rate will be the maximun baud rate.
1914  *
1915  *
1916  *      Function 11:    Setting the data-bits/stop-bits/parity of this port
1917  *      Syntax:
1918  *      int  MoxaPortSetMode(int port, int databits, int stopbits, int parity);
1919  *           int port           : port number (0 - 127)
1920  *           int databits       : data bits (8/7/6/5)
1921  *           int stopbits       : stop bits (2/1/0, 0 show 1.5 stop bits)
1922  int parity     : parity (0:None,1:Odd,2:Even,3:Mark,4:Space)
1923  *
1924  *           return:    -1      : invalid parameter
1925  *                      0       : setting O.K.
1926  *
1927  *
1928  *      Function 12:    Configure the port.
1929  *      Syntax:
1930  *      int  MoxaPortSetTermio(int port, struct termios *termio);
1931  *           int port           : port number (0 - 127)
1932  *           struct termios * termio : termio structure pointer
1933  *
1934  *           return:    -1      : this port is invalid or termio == NULL
1935  *                      0       : setting O.K.
1936  *
1937  *
1938  *      Function 13:    Get the DTR/RTS state of this port.
1939  *      Syntax:
1940  *      int  MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1941  *           int port           : port number (0 - 127)
1942  *           int * dtrState     : pointer to INT to receive the current DTR
1943  *                                state. (if NULL, this function will not
1944  *                                write to this address)
1945  *           int * rtsState     : pointer to INT to receive the current RTS
1946  *                                state. (if NULL, this function will not
1947  *                                write to this address)
1948  *
1949  *           return:    -1      : this port is invalid
1950  *                      0       : O.K.
1951  *
1952  *
1953  *      Function 14:    Setting the DTR/RTS output state of this port.
1954  *      Syntax:
1955  *      void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1956  *           int port           : port number (0 - 127)
1957  *           int dtrState       : DTR output state (0: off, 1: on)
1958  *           int rtsState       : RTS output state (0: off, 1: on)
1959  *
1960  *
1961  *      Function 15:    Setting the flow control of this port.
1962  *      Syntax:
1963  *      void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1964  *                            int txFlow,int xany);
1965  *           int port           : port number (0 - 127)
1966  *           int rtsFlow        : H/W RTS flow control (0: no, 1: yes)
1967  *           int ctsFlow        : H/W CTS flow control (0: no, 1: yes)
1968  *           int rxFlow         : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1969  *           int txFlow         : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1970  *           int xany           : S/W XANY flow control (0: no, 1: yes)
1971  *
1972  *
1973  *      Function 16:    Get ths line status of this port
1974  *      Syntax:
1975  *      int  MoxaPortLineStatus(int port);
1976  *           int port           : port number (0 - 127)
1977  *
1978  *           return:    Bit 0 - CTS state (0: off, 1: on)
1979  *                      Bit 1 - DSR state (0: off, 1: on)
1980  *                      Bit 2 - DCD state (0: off, 1: on)
1981  *
1982  *
1983  *      Function 17:    Check the DCD state has changed since the last read
1984  *                      of this function.
1985  *      Syntax:
1986  *      int  MoxaPortDCDChange(int port);
1987  *           int port           : port number (0 - 127)
1988  *
1989  *           return:    0       : no changed
1990  *                      1       : DCD has changed
1991  *
1992  *
1993  *      Function 18:    Check ths current DCD state is ON or not.
1994  *      Syntax:
1995  *      int  MoxaPortDCDON(int port);
1996  *           int port           : port number (0 - 127)
1997  *
1998  *           return:    0       : DCD off
1999  *                      1       : DCD on
2000  *
2001  *
2002  *      Function 19:    Flush the Rx/Tx buffer data of this port.
2003  *      Syntax:
2004  *      void MoxaPortFlushData(int port, int mode);
2005  *           int port           : port number (0 - 127)
2006  *           int mode    
2007  *                      0       : flush the Rx buffer 
2008  *                      1       : flush the Tx buffer 
2009  *                      2       : flush the Rx and Tx buffer 
2010  *
2011  *
2012  *      Function 20:    Write data.
2013  *      Syntax:
2014  *      int  MoxaPortWriteData(int port, unsigned char * buffer, int length);
2015  *           int port           : port number (0 - 127)
2016  *           unsigned char * buffer     : pointer to write data buffer.
2017  *           int length         : write data length
2018  *
2019  *           return:    0 - length      : real write data length
2020  *
2021  *
2022  *      Function 21:    Read data.
2023  *      Syntax:
2024  *      int  MoxaPortReadData(int port, unsigned char * buffer, int length);
2025  *           int port           : port number (0 - 127)
2026  *           unsigned char * buffer     : pointer to read data buffer.
2027  *           int length         : read data buffer length
2028  *
2029  *           return:    0 - length      : real read data length
2030  *
2031  *
2032  *      Function 22:    Get the Tx buffer size of this port
2033  *      Syntax:
2034  *      int  MoxaPortTxBufSize(int port);
2035  *           int port           : port number (0 - 127)
2036  *
2037  *           return:    ..      : Tx buffer size
2038  *
2039  *
2040  *      Function 23:    Get the Rx buffer size of this port
2041  *      Syntax:
2042  *      int  MoxaPortRxBufSize(int port);
2043  *           int port           : port number (0 - 127)
2044  *
2045  *           return:    ..      : Rx buffer size
2046  *
2047  *
2048  *      Function 24:    Get the Tx buffer current queued data bytes
2049  *      Syntax:
2050  *      int  MoxaPortTxQueue(int port);
2051  *           int port           : port number (0 - 127)
2052  *
2053  *           return:    ..      : Tx buffer current queued data bytes
2054  *
2055  *
2056  *      Function 25:    Get the Tx buffer current free space
2057  *      Syntax:
2058  *      int  MoxaPortTxFree(int port);
2059  *           int port           : port number (0 - 127)
2060  *
2061  *           return:    ..      : Tx buffer current free space
2062  *
2063  *
2064  *      Function 26:    Get the Rx buffer current queued data bytes
2065  *      Syntax:
2066  *      int  MoxaPortRxQueue(int port);
2067  *           int port           : port number (0 - 127)
2068  *
2069  *           return:    ..      : Rx buffer current queued data bytes
2070  *
2071  *
2072  *      Function 27:    Get the Rx buffer current free space
2073  *      Syntax:
2074  *      int  MoxaPortRxFree(int port);
2075  *           int port           : port number (0 - 127)
2076  *
2077  *           return:    ..      : Rx buffer current free space
2078  *
2079  *
2080  *      Function 28:    Disable port data transmission.
2081  *      Syntax:
2082  *      void MoxaPortTxDisable(int port);
2083  *           int port           : port number (0 - 127)
2084  *
2085  *
2086  *      Function 29:    Enable port data transmission.
2087  *      Syntax:
2088  *      void MoxaPortTxEnable(int port);
2089  *           int port           : port number (0 - 127)
2090  *
2091  *
2092  *      Function 30:    Get the received BREAK signal count.
2093  *      Syntax:
2094  *      int  MoxaPortGetBrkCnt(int port);
2095  *           int port           : port number (0 - 127)
2096  *
2097  *           return:    0 - ..  : BREAK signal count
2098  *
2099  *
2100  *      Function 31:    Get the received BREAK signal count and reset it.
2101  *      Syntax:
2102  *      int  MoxaPortResetBrkCnt(int port);
2103  *           int port           : port number (0 - 127)
2104  *
2105  *           return:    0 - ..  : BREAK signal count
2106  *
2107  *
2108  *      Function 32:    Set the S/W flow control new XON/XOFF value, default
2109  *                      XON is 0x11 & XOFF is 0x13.
2110  *      Syntax:
2111  *      void MoxaPortSetXonXoff(int port, int xonValue, int xoffValue);
2112  *           int port           : port number (0 - 127)
2113  *           int xonValue       : new XON value (0 - 255)
2114  *           int xoffValue      : new XOFF value (0 - 255)
2115  *
2116  *
2117  *      Function 33:    Check this port's transmission is hold by remote site
2118  *                      because the flow control.
2119  *      Syntax:
2120  *      int  MoxaPortIsTxHold(int port);
2121  *           int port           : port number (0 - 127)
2122  *
2123  *           return:    0       : normal
2124  *                      1       : hold by remote site
2125  *
2126  *
2127  *      Function 34:    Send out a BREAK signal.
2128  *      Syntax:
2129  *      void MoxaPortSendBreak(int port, int ms100);
2130  *           int port           : port number (0 - 127)
2131  *           int ms100          : break signal time interval.
2132  *                                unit: 100 mini-second. if ms100 == 0, it will
2133  *                                send out a about 250 ms BREAK signal.
2134  *
2135  */
2136 int MoxaPortIsValid(int port)
2137 {
2138
2139         if (moxaCard == 0)
2140                 return (0);
2141         if (moxaChkPort[port] == 0)
2142                 return (0);
2143         return (1);
2144 }
2145
2146 void MoxaPortEnable(int port)
2147 {
2148         unsigned long ofsAddr;
2149         int MoxaPortLineStatus(int);
2150         short lowwater = 512;
2151
2152         ofsAddr = moxaTableAddr[port];
2153         writew(lowwater, ofsAddr + Low_water);
2154         moxaBreakCnt[port] = 0;
2155         if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2156             (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2157                 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
2158         } else {
2159                 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
2160         }
2161
2162         moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
2163         moxafunc(ofsAddr, FC_FlushQueue, 2);
2164
2165         moxafunc(ofsAddr, FC_EnableCH, Magic_code);
2166         MoxaPortLineStatus(port);
2167 }
2168
2169 void MoxaPortDisable(int port)
2170 {
2171         unsigned long ofsAddr;
2172
2173         ofsAddr = moxaTableAddr[port];
2174         moxafunc(ofsAddr, FC_SetFlowCtl, 0);    /* disable flow control */
2175         moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
2176         writew(0, ofsAddr + HostStat);
2177         moxafunc(ofsAddr, FC_DisableCH, Magic_code);
2178 }
2179
2180 long MoxaPortGetMaxBaud(int port)
2181 {
2182         if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2183             (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
2184                 return (460800L);
2185         else
2186                 return (921600L);
2187 }
2188
2189
2190 long MoxaPortSetBaud(int port, long baud)
2191 {
2192         unsigned long ofsAddr;
2193         long max, clock;
2194         unsigned int val;
2195
2196         if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
2197                 return (0);
2198         ofsAddr = moxaTableAddr[port];
2199         if (baud > max)
2200                 baud = max;
2201         if (max == 38400L)
2202                 clock = 614400L;        /* for 9.8304 Mhz : max. 38400 bps */
2203         else if (max == 57600L)
2204                 clock = 691200L;        /* for 11.0592 Mhz : max. 57600 bps */
2205         else
2206                 clock = 921600L;        /* for 14.7456 Mhz : max. 115200 bps */
2207         val = clock / baud;
2208         moxafunc(ofsAddr, FC_SetBaud, val);
2209         baud = clock / val;
2210         moxaCurBaud[port] = baud;
2211         return (baud);
2212 }
2213
2214 int MoxaPortSetTermio(int port, struct termios *termio)
2215 {
2216         unsigned long ofsAddr;
2217         tcflag_t cflag;
2218         long baud;
2219         tcflag_t mode = 0;
2220
2221         if (moxaChkPort[port] == 0 || termio == 0)
2222                 return (-1);
2223         ofsAddr = moxaTableAddr[port];
2224         cflag = termio->c_cflag;        /* termio->c_cflag */
2225
2226         mode = termio->c_cflag & CSIZE;
2227         if (mode == CS5)
2228                 mode = MX_CS5;
2229         else if (mode == CS6)
2230                 mode = MX_CS6;
2231         else if (mode == CS7)
2232                 mode = MX_CS7;
2233         else if (mode == CS8)
2234                 mode = MX_CS8;
2235
2236         if (termio->c_cflag & CSTOPB) {
2237                 if (mode == MX_CS5)
2238                         mode |= MX_STOP15;
2239                 else
2240                         mode |= MX_STOP2;
2241         } else
2242                 mode |= MX_STOP1;
2243
2244         if (termio->c_cflag & PARENB) {
2245                 if (termio->c_cflag & PARODD)
2246                         mode |= MX_PARODD;
2247                 else
2248                         mode |= MX_PAREVEN;
2249         } else
2250                 mode |= MX_PARNONE;
2251
2252         moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2253
2254         cflag &= (CBAUD | CBAUDEX);
2255 #ifndef B921600
2256 #define B921600 (B460800+1)
2257 #endif
2258         switch (cflag) {
2259         case B921600:
2260                 baud = 921600L;
2261                 break;
2262         case B460800:
2263                 baud = 460800L;
2264                 break;
2265         case B230400:
2266                 baud = 230400L;
2267                 break;
2268         case B115200:
2269                 baud = 115200L;
2270                 break;
2271         case B57600:
2272                 baud = 57600L;
2273                 break;
2274         case B38400:
2275                 baud = 38400L;
2276                 break;
2277         case B19200:
2278                 baud = 19200L;
2279                 break;
2280         case B9600:
2281                 baud = 9600L;
2282                 break;
2283         case B4800:
2284                 baud = 4800L;
2285                 break;
2286         case B2400:
2287                 baud = 2400L;
2288                 break;
2289         case B1800:
2290                 baud = 1800L;
2291                 break;
2292         case B1200:
2293                 baud = 1200L;
2294                 break;
2295         case B600:
2296                 baud = 600L;
2297                 break;
2298         case B300:
2299                 baud = 300L;
2300                 break;
2301         case B200:
2302                 baud = 200L;
2303                 break;
2304         case B150:
2305                 baud = 150L;
2306                 break;
2307         case B134:
2308                 baud = 134L;
2309                 break;
2310         case B110:
2311                 baud = 110L;
2312                 break;
2313         case B75:
2314                 baud = 75L;
2315                 break;
2316         case B50:
2317                 baud = 50L;
2318                 break;
2319         default:
2320                 baud = 0;
2321         }
2322         if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2323             (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2324                 if (baud == 921600L)
2325                         return (-1);
2326         }
2327         MoxaPortSetBaud(port, baud);
2328
2329         if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2330                 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2331                 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2332                 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
2333                 wait_finish(ofsAddr);
2334
2335         }
2336         return (0);
2337 }
2338
2339 int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
2340 {
2341
2342         if (!MoxaPortIsValid(port))
2343                 return (-1);
2344         if (dtrState) {
2345                 if (moxaLineCtrl[port] & DTR_ON)
2346                         *dtrState = 1;
2347                 else
2348                         *dtrState = 0;
2349         }
2350         if (rtsState) {
2351                 if (moxaLineCtrl[port] & RTS_ON)
2352                         *rtsState = 1;
2353                 else
2354                         *rtsState = 0;
2355         }
2356         return (0);
2357 }
2358
2359 void MoxaPortLineCtrl(int port, int dtr, int rts)
2360 {
2361         unsigned long ofsAddr;
2362         int mode;
2363
2364         ofsAddr = moxaTableAddr[port];
2365         mode = 0;
2366         if (dtr)
2367                 mode |= DTR_ON;
2368         if (rts)
2369                 mode |= RTS_ON;
2370         moxaLineCtrl[port] = mode;
2371         moxafunc(ofsAddr, FC_LineControl, mode);
2372 }
2373
2374 void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
2375 {
2376         unsigned long ofsAddr;
2377         int mode;
2378
2379         ofsAddr = moxaTableAddr[port];
2380         mode = 0;
2381         if (rts)
2382                 mode |= RTS_FlowCtl;
2383         if (cts)
2384                 mode |= CTS_FlowCtl;
2385         if (txflow)
2386                 mode |= Tx_FlowCtl;
2387         if (rxflow)
2388                 mode |= Rx_FlowCtl;
2389         if (txany)
2390                 mode |= IXM_IXANY;
2391         moxafunc(ofsAddr, FC_SetFlowCtl, mode);
2392 }
2393
2394 int MoxaPortLineStatus(int port)
2395 {
2396         unsigned long ofsAddr;
2397         int val;
2398
2399         ofsAddr = moxaTableAddr[port];
2400         if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2401             (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2402                 moxafunc(ofsAddr, FC_LineStatus, 0);
2403                 val = readw(ofsAddr + FuncArg);
2404         } else {
2405                 val = readw(ofsAddr + FlagStat) >> 4;
2406         }
2407         val &= 0x0B;
2408         if (val & 8) {
2409                 val |= 4;
2410                 if ((moxaDCDState[port] & DCD_oldstate) == 0)
2411                         moxaDCDState[port] = (DCD_oldstate | DCD_changed);
2412         } else {
2413                 if (moxaDCDState[port] & DCD_oldstate)
2414                         moxaDCDState[port] = DCD_changed;
2415         }
2416         val &= 7;
2417         return (val);
2418 }
2419
2420 int MoxaPortDCDChange(int port)
2421 {
2422         int n;
2423
2424         if (moxaChkPort[port] == 0)
2425                 return (0);
2426         n = moxaDCDState[port];
2427         moxaDCDState[port] &= ~DCD_changed;
2428         n &= DCD_changed;
2429         return (n);
2430 }
2431
2432 int MoxaPortDCDON(int port)
2433 {
2434         int n;
2435
2436         if (moxaChkPort[port] == 0)
2437                 return (0);
2438         if (moxaDCDState[port] & DCD_oldstate)
2439                 n = 1;
2440         else
2441                 n = 0;
2442         return (n);
2443 }
2444
2445
2446 /*
2447    int MoxaDumpMem(int port, unsigned char * buffer, int len)
2448    {
2449    int          i;
2450    unsigned long                baseAddr,ofsAddr,ofs;
2451
2452    baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2453    ofs = baseAddr + DynPage_addr + pageofs;
2454    if (len > 0x2000L)
2455    len = 0x2000L;
2456    for (i = 0; i < len; i++)
2457    buffer[i] = readb(ofs+i);
2458    }
2459  */
2460
2461
2462 int MoxaPortWriteData(int port, unsigned char * buffer, int len)
2463 {
2464         int c, total, i;
2465         ushort tail;
2466         int cnt;
2467         ushort head, tx_mask, spage, epage;
2468         ushort pageno, pageofs, bufhead;
2469         unsigned long baseAddr, ofsAddr, ofs;
2470
2471         ofsAddr = moxaTableAddr[port];
2472         baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2473         tx_mask = readw(ofsAddr + TX_mask);
2474         spage = readw(ofsAddr + Page_txb);
2475         epage = readw(ofsAddr + EndPage_txb);
2476         tail = readw(ofsAddr + TXwptr);
2477         head = readw(ofsAddr + TXrptr);
2478         c = (head > tail) ? (head - tail - 1)
2479             : (head - tail + tx_mask);
2480         if (c > len)
2481                 c = len;
2482         moxaLog.txcnt[port] += c;
2483         total = c;
2484         if (spage == epage) {
2485                 bufhead = readw(ofsAddr + Ofs_txb);
2486                 writew(spage, baseAddr + Control_reg);
2487                 while (c > 0) {
2488                         if (head > tail)
2489                                 len = head - tail - 1;
2490                         else
2491                                 len = tx_mask + 1 - tail;
2492                         len = (c > len) ? len : c;
2493                         ofs = baseAddr + DynPage_addr + bufhead + tail;
2494                         for (i = 0; i < len; i++)
2495                                 writeb(*buffer++, ofs + i);
2496                         tail = (tail + len) & tx_mask;
2497                         c -= len;
2498                 }
2499                 writew(tail, ofsAddr + TXwptr);
2500         } else {
2501                 len = c;
2502                 pageno = spage + (tail >> 13);
2503                 pageofs = tail & Page_mask;
2504                 do {
2505                         cnt = Page_size - pageofs;
2506                         if (cnt > c)
2507                                 cnt = c;
2508                         c -= cnt;
2509                         writeb(pageno, baseAddr + Control_reg);
2510                         ofs = baseAddr + DynPage_addr + pageofs;
2511                         for (i = 0; i < cnt; i++)
2512                                 writeb(*buffer++, ofs + i);
2513                         if (c == 0) {
2514                                 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2515                                 break;
2516                         }
2517                         if (++pageno == epage)
2518                                 pageno = spage;
2519                         pageofs = 0;
2520                 } while (1);
2521         }
2522         writeb(1, ofsAddr + CD180TXirq);        /* start to send */
2523         return (total);
2524 }
2525
2526 int MoxaPortReadData(int port, unsigned char * buffer, int space)
2527 {
2528         register ushort head, pageofs;
2529         int i, count, cnt, len, total, remain;
2530         ushort tail, rx_mask, spage, epage;
2531         ushort pageno, bufhead;
2532         unsigned long baseAddr, ofsAddr, ofs;
2533
2534         ofsAddr = moxaTableAddr[port];
2535         baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2536         head = readw(ofsAddr + RXrptr);
2537         tail = readw(ofsAddr + RXwptr);
2538         rx_mask = readw(ofsAddr + RX_mask);
2539         spage = readw(ofsAddr + Page_rxb);
2540         epage = readw(ofsAddr + EndPage_rxb);
2541         count = (tail >= head) ? (tail - head)
2542             : (tail - head + rx_mask + 1);
2543         if (count == 0)
2544                 return (0);
2545
2546         total = (space > count) ? count : space;
2547         remain = count - total;
2548         moxaLog.rxcnt[port] += total;
2549         count = total;
2550         if (spage == epage) {
2551                 bufhead = readw(ofsAddr + Ofs_rxb);
2552                 writew(spage, baseAddr + Control_reg);
2553                 while (count > 0) {
2554                         if (tail >= head)
2555                                 len = tail - head;
2556                         else
2557                                 len = rx_mask + 1 - head;
2558                         len = (count > len) ? len : count;
2559                         ofs = baseAddr + DynPage_addr + bufhead + head;
2560                         for (i = 0; i < len; i++)
2561                                 *buffer++ = readb(ofs + i);
2562                         head = (head + len) & rx_mask;
2563                         count -= len;
2564                 }
2565                 writew(head, ofsAddr + RXrptr);
2566         } else {
2567                 len = count;
2568                 pageno = spage + (head >> 13);
2569                 pageofs = head & Page_mask;
2570                 do {
2571                         cnt = Page_size - pageofs;
2572                         if (cnt > count)
2573                                 cnt = count;
2574                         count -= cnt;
2575                         writew(pageno, baseAddr + Control_reg);
2576                         ofs = baseAddr + DynPage_addr + pageofs;
2577                         for (i = 0; i < cnt; i++)
2578                                 *buffer++ = readb(ofs + i);
2579                         if (count == 0) {
2580                                 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2581                                 break;
2582                         }
2583                         if (++pageno == epage)
2584                                 pageno = spage;
2585                         pageofs = 0;
2586                 } while (1);
2587         }
2588         if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2589                 moxaLowWaterChk = 1;
2590                 moxaLowChkFlag[port] = 1;
2591         }
2592         return (total);
2593 }
2594
2595
2596 int MoxaPortTxQueue(int port)
2597 {
2598         unsigned long ofsAddr;
2599         ushort rptr, wptr, mask;
2600         int len;
2601
2602         ofsAddr = moxaTableAddr[port];
2603         rptr = readw(ofsAddr + TXrptr);
2604         wptr = readw(ofsAddr + TXwptr);
2605         mask = readw(ofsAddr + TX_mask);
2606         len = (wptr - rptr) & mask;
2607         return (len);
2608 }
2609
2610 int MoxaPortTxFree(int port)
2611 {
2612         unsigned long ofsAddr;
2613         ushort rptr, wptr, mask;
2614         int len;
2615
2616         ofsAddr = moxaTableAddr[port];
2617         rptr = readw(ofsAddr + TXrptr);
2618         wptr = readw(ofsAddr + TXwptr);
2619         mask = readw(ofsAddr + TX_mask);
2620         len = mask - ((wptr - rptr) & mask);
2621         return (len);
2622 }
2623
2624 int MoxaPortRxQueue(int port)
2625 {
2626         unsigned long ofsAddr;
2627         ushort rptr, wptr, mask;
2628         int len;
2629
2630         ofsAddr = moxaTableAddr[port];
2631         rptr = readw(ofsAddr + RXrptr);
2632         wptr = readw(ofsAddr + RXwptr);
2633         mask = readw(ofsAddr + RX_mask);
2634         len = (wptr - rptr) & mask;
2635         return (len);
2636 }
2637
2638
2639 void MoxaPortTxDisable(int port)
2640 {
2641         unsigned long ofsAddr;
2642
2643         ofsAddr = moxaTableAddr[port];
2644         moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
2645 }
2646
2647 void MoxaPortTxEnable(int port)
2648 {
2649         unsigned long ofsAddr;
2650
2651         ofsAddr = moxaTableAddr[port];
2652         moxafunc(ofsAddr, FC_SetXonState, Magic_code);
2653 }
2654
2655
2656 int MoxaPortResetBrkCnt(int port)
2657 {
2658         ushort cnt;
2659         cnt = moxaBreakCnt[port];
2660         moxaBreakCnt[port] = 0;
2661         return (cnt);
2662 }
2663
2664
2665 void MoxaPortSendBreak(int port, int ms100)
2666 {
2667         unsigned long ofsAddr;
2668
2669         ofsAddr = moxaTableAddr[port];
2670         if (ms100) {
2671                 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2672                 moxadelay(ms100 * (HZ / 10));
2673         } else {
2674                 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2675                 moxadelay(HZ / 4);      /* 250 ms */
2676         }
2677         moxafunc(ofsAddr, FC_StopBreak, Magic_code);
2678 }
2679
2680 static int moxa_get_serial_info(struct moxa_str *info,
2681                                 struct serial_struct __user *retinfo)
2682 {
2683         struct serial_struct tmp;
2684
2685         memset(&tmp, 0, sizeof(tmp));
2686         tmp.type = info->type;
2687         tmp.line = info->port;
2688         tmp.port = 0;
2689         tmp.irq = 0;
2690         tmp.flags = info->asyncflags;
2691         tmp.baud_base = 921600;
2692         tmp.close_delay = info->close_delay;
2693         tmp.closing_wait = info->closing_wait;
2694         tmp.custom_divisor = 0;
2695         tmp.hub6 = 0;
2696         if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2697                 return -EFAULT;
2698         return (0);
2699 }
2700
2701
2702 static int moxa_set_serial_info(struct moxa_str *info,
2703                                 struct serial_struct __user *new_info)
2704 {
2705         struct serial_struct new_serial;
2706
2707         if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2708                 return -EFAULT;
2709
2710         if ((new_serial.irq != 0) ||
2711             (new_serial.port != 0) ||
2712 //           (new_serial.type != info->type) ||
2713             (new_serial.custom_divisor != 0) ||
2714             (new_serial.baud_base != 921600))
2715                 return (-EPERM);
2716
2717         if (!capable(CAP_SYS_ADMIN)) {
2718                 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2719                      (info->asyncflags & ~ASYNC_USR_MASK)))
2720                         return (-EPERM);
2721         } else {
2722                 info->close_delay = new_serial.close_delay * HZ / 100;
2723                 info->closing_wait = new_serial.closing_wait * HZ / 100;
2724         }
2725
2726         new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2727         new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2728
2729         if (new_serial.type == PORT_16550A) {
2730                 MoxaSetFifo(info->port, 1);
2731         } else {
2732                 MoxaSetFifo(info->port, 0);
2733         }
2734
2735         info->type = new_serial.type;
2736         return (0);
2737 }
2738
2739
2740
2741 /*****************************************************************************
2742  *      Static local functions:                                              *
2743  *****************************************************************************/
2744 /*
2745  * moxadelay - delays a specified number ticks
2746  */
2747 static void moxadelay(int tick)
2748 {
2749         unsigned long st, et;
2750
2751         st = jiffies;
2752         et = st + tick;
2753         while (time_before(jiffies, et));
2754 }
2755
2756 static void moxafunc(unsigned long ofsAddr, int cmd, ushort arg)
2757 {
2758
2759         writew(arg, ofsAddr + FuncArg);
2760         writew(cmd, ofsAddr + FuncCode);
2761         wait_finish(ofsAddr);
2762 }
2763
2764 static void wait_finish(unsigned long ofsAddr)
2765 {
2766         unsigned long i, j;
2767
2768         i = jiffies;
2769         while (readw(ofsAddr + FuncCode) != 0) {
2770                 j = jiffies;
2771                 if ((j - i) > moxaFuncTout) {
2772                         return;
2773                 }
2774         }
2775 }
2776
2777 static void low_water_check(unsigned long ofsAddr)
2778 {
2779         int len;
2780         ushort rptr, wptr, mask;
2781
2782         if (readb(ofsAddr + FlagStat) & Xoff_state) {
2783                 rptr = readw(ofsAddr + RXrptr);
2784                 wptr = readw(ofsAddr + RXwptr);
2785                 mask = readw(ofsAddr + RX_mask);
2786                 len = (wptr - rptr) & mask;
2787                 if (len <= Low_water)
2788                         moxafunc(ofsAddr, FC_SendXon, 0);
2789         }
2790 }
2791
2792 static int moxaloadbios(int cardno, unsigned char __user *tmp, int len)
2793 {
2794         unsigned long baseAddr;
2795         int i;
2796
2797         if(copy_from_user(moxaBuff, tmp, len))
2798                 return -EFAULT;
2799         baseAddr = moxaBaseAddr[cardno];
2800         writeb(HW_reset, baseAddr + Control_reg);       /* reset */
2801         moxadelay(1);           /* delay 10 ms */
2802         for (i = 0; i < 4096; i++)
2803                 writeb(0, baseAddr + i);        /* clear fix page */
2804         for (i = 0; i < len; i++)
2805                 writeb(moxaBuff[i], baseAddr + i);      /* download BIOS */
2806         writeb(0, baseAddr + Control_reg);      /* restart */
2807         return (0);
2808 }
2809
2810 static int moxafindcard(int cardno)
2811 {
2812         unsigned long baseAddr;
2813         ushort tmp;
2814
2815         baseAddr = moxaBaseAddr[cardno];
2816         switch (moxa_boards[cardno].boardType) {
2817         case MOXA_BOARD_C218_ISA:
2818         case MOXA_BOARD_C218_PCI:
2819                 if ((tmp = readw(baseAddr + C218_key)) != C218_KeyCode) {
2820                         return (-1);
2821                 }
2822                 break;
2823         case MOXA_BOARD_CP204J:
2824                 if ((tmp = readw(baseAddr + C218_key)) != CP204J_KeyCode) {
2825                         return (-1);
2826                 }
2827                 break;
2828         default:
2829                 if ((tmp = readw(baseAddr + C320_key)) != C320_KeyCode) {
2830                         return (-1);
2831                 }
2832                 if ((tmp = readw(baseAddr + C320_status)) != STS_init) {
2833                         return (-2);
2834                 }
2835         }
2836         return (0);
2837 }
2838
2839 static int moxaload320b(int cardno, unsigned char __user *tmp, int len)
2840 {
2841         unsigned long baseAddr;
2842         int i;
2843
2844         if(len > sizeof(moxaBuff))
2845                 return -EINVAL;
2846         if(copy_from_user(moxaBuff, tmp, len))
2847                 return -EFAULT;
2848         baseAddr = moxaBaseAddr[cardno];
2849         writew(len - 7168 - 2, baseAddr + C320bapi_len);
2850         writeb(1, baseAddr + Control_reg);      /* Select Page 1 */
2851         for (i = 0; i < 7168; i++)
2852                 writeb(moxaBuff[i], baseAddr + DynPage_addr + i);
2853         writeb(2, baseAddr + Control_reg);      /* Select Page 2 */
2854         for (i = 0; i < (len - 7168); i++)
2855                 writeb(moxaBuff[i + 7168], baseAddr + DynPage_addr + i);
2856         return (0);
2857 }
2858
2859 static int moxaloadcode(int cardno, unsigned char __user *tmp, int len)
2860 {
2861         unsigned long baseAddr, ofsAddr;
2862         int retval, port, i;
2863
2864         if(copy_from_user(moxaBuff, tmp, len))
2865                 return -EFAULT;
2866         baseAddr = moxaBaseAddr[cardno];
2867         switch (moxa_boards[cardno].boardType) {
2868         case MOXA_BOARD_C218_ISA:
2869         case MOXA_BOARD_C218_PCI:
2870         case MOXA_BOARD_CP204J:
2871                 retval = moxaloadc218(cardno, baseAddr, len);
2872                 if (retval)
2873                         return (retval);
2874                 port = cardno * MAX_PORTS_PER_BOARD;
2875                 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2876                         moxaChkPort[port] = 1;
2877                         moxaCurBaud[port] = 9600L;
2878                         moxaDCDState[port] = 0;
2879                         moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
2880                         ofsAddr = moxaTableAddr[port];
2881                         writew(C218rx_mask, ofsAddr + RX_mask);
2882                         writew(C218tx_mask, ofsAddr + TX_mask);
2883                         writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
2884                         writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
2885
2886                         writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
2887                         writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
2888
2889                 }
2890                 break;
2891         default:
2892                 retval = moxaloadc320(cardno, baseAddr, len,
2893                                       &moxa_boards[cardno].numPorts);
2894                 if (retval)
2895                         return (retval);
2896                 port = cardno * MAX_PORTS_PER_BOARD;
2897                 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2898                         moxaChkPort[port] = 1;
2899                         moxaCurBaud[port] = 9600L;
2900                         moxaDCDState[port] = 0;
2901                         moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
2902                         ofsAddr = moxaTableAddr[port];
2903                         if (moxa_boards[cardno].numPorts == 8) {
2904                                 writew(C320p8rx_mask, ofsAddr + RX_mask);
2905                                 writew(C320p8tx_mask, ofsAddr + TX_mask);
2906                                 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
2907                                 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
2908                                 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
2909                                 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
2910
2911                         } else if (moxa_boards[cardno].numPorts == 16) {
2912                                 writew(C320p16rx_mask, ofsAddr + RX_mask);
2913                                 writew(C320p16tx_mask, ofsAddr + TX_mask);
2914                                 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
2915                                 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
2916                                 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
2917                                 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
2918
2919                         } else if (moxa_boards[cardno].numPorts == 24) {
2920                                 writew(C320p24rx_mask, ofsAddr + RX_mask);
2921                                 writew(C320p24tx_mask, ofsAddr + TX_mask);
2922                                 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
2923                                 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
2924                                 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
2925                                 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2926                         } else if (moxa_boards[cardno].numPorts == 32) {
2927                                 writew(C320p32rx_mask, ofsAddr + RX_mask);
2928                                 writew(C320p32tx_mask, ofsAddr + TX_mask);
2929                                 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
2930                                 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
2931                                 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
2932                                 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
2933                                 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2934                         }
2935                 }
2936                 break;
2937         }
2938         return (0);
2939 }
2940
2941 static int moxaloadc218(int cardno, unsigned long baseAddr, int len)
2942 {
2943         char retry;
2944         int i, j, len1, len2;
2945         ushort usum, *ptr, keycode;
2946
2947         if (moxa_boards[cardno].boardType == MOXA_BOARD_CP204J)
2948                 keycode = CP204J_KeyCode;
2949         else
2950                 keycode = C218_KeyCode;
2951         usum = 0;
2952         len1 = len >> 1;
2953         ptr = (ushort *) moxaBuff;
2954         for (i = 0; i < len1; i++)
2955                 usum += *(ptr + i);
2956         retry = 0;
2957         do {
2958                 len1 = len >> 1;
2959                 j = 0;
2960                 while (len1) {
2961                         len2 = (len1 > 2048) ? 2048 : len1;
2962                         len1 -= len2;
2963                         for (i = 0; i < len2 << 1; i++)
2964                                 writeb(moxaBuff[i + j], baseAddr + C218_LoadBuf + i);
2965                         j += i;
2966
2967                         writew(len2, baseAddr + C218DLoad_len);
2968                         writew(0, baseAddr + C218_key);
2969                         for (i = 0; i < 100; i++) {
2970                                 if (readw(baseAddr + C218_key) == keycode)
2971                                         break;
2972                                 moxadelay(1);   /* delay 10 ms */
2973                         }
2974                         if (readw(baseAddr + C218_key) != keycode) {
2975                                 return (-1);
2976                         }
2977                 }
2978                 writew(0, baseAddr + C218DLoad_len);
2979                 writew(usum, baseAddr + C218check_sum);
2980                 writew(0, baseAddr + C218_key);
2981                 for (i = 0; i < 100; i++) {
2982                         if (readw(baseAddr + C218_key) == keycode)
2983                                 break;
2984                         moxadelay(1);   /* delay 10 ms */
2985                 }
2986                 retry++;
2987         } while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
2988         if (readb(baseAddr + C218chksum_ok) != 1) {
2989                 return (-1);
2990         }
2991         writew(0, baseAddr + C218_key);
2992         for (i = 0; i < 100; i++) {
2993                 if (readw(baseAddr + Magic_no) == Magic_code)
2994                         break;
2995                 moxadelay(1);   /* delay 10 ms */
2996         }
2997         if (readw(baseAddr + Magic_no) != Magic_code) {
2998                 return (-1);
2999         }
3000         writew(1, baseAddr + Disable_IRQ);
3001         writew(0, baseAddr + Magic_no);
3002         for (i = 0; i < 100; i++) {
3003                 if (readw(baseAddr + Magic_no) == Magic_code)
3004                         break;
3005                 moxadelay(1);   /* delay 10 ms */
3006         }
3007         if (readw(baseAddr + Magic_no) != Magic_code) {
3008                 return (-1);
3009         }
3010         moxaCard = 1;
3011         moxaIntNdx[cardno] = baseAddr + IRQindex;
3012         moxaIntPend[cardno] = baseAddr + IRQpending;
3013         moxaIntTable[cardno] = baseAddr + IRQtable;
3014         return (0);
3015 }
3016
3017 static int moxaloadc320(int cardno, unsigned long baseAddr, int len, int *numPorts)
3018 {
3019         ushort usum;
3020         int i, j, wlen, len2, retry;
3021         ushort *uptr;
3022
3023         usum = 0;
3024         wlen = len >> 1;
3025         uptr = (ushort *) moxaBuff;
3026         for (i = 0; i < wlen; i++)
3027                 usum += uptr[i];
3028         retry = 0;
3029         j = 0;
3030         do {
3031                 while (wlen) {
3032                         if (wlen > 2048)
3033                                 len2 = 2048;
3034                         else
3035                                 len2 = wlen;
3036                         wlen -= len2;
3037                         len2 <<= 1;
3038                         for (i = 0; i < len2; i++)
3039                                 writeb(moxaBuff[j + i], baseAddr + C320_LoadBuf + i);
3040                         len2 >>= 1;
3041                         j += i;
3042                         writew(len2, baseAddr + C320DLoad_len);
3043                         writew(0, baseAddr + C320_key);
3044                         for (i = 0; i < 10; i++) {
3045                                 if (readw(baseAddr + C320_key) == C320_KeyCode)
3046                                         break;
3047                                 moxadelay(1);
3048                         }
3049                         if (readw(baseAddr + C320_key) != C320_KeyCode)
3050                                 return (-1);
3051                 }
3052                 writew(0, baseAddr + C320DLoad_len);
3053                 writew(usum, baseAddr + C320check_sum);
3054                 writew(0, baseAddr + C320_key);
3055                 for (i = 0; i < 10; i++) {
3056                         if (readw(baseAddr + C320_key) == C320_KeyCode)
3057                                 break;
3058                         moxadelay(1);
3059                 }
3060                 retry++;
3061         } while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
3062         if (readb(baseAddr + C320chksum_ok) != 1)
3063                 return (-1);
3064         writew(0, baseAddr + C320_key);
3065         for (i = 0; i < 600; i++) {
3066                 if (readw(baseAddr + Magic_no) == Magic_code)
3067                         break;
3068                 moxadelay(1);
3069         }
3070         if (readw(baseAddr + Magic_no) != Magic_code)
3071                 return (-100);
3072
3073         if (moxa_boards[cardno].busType == MOXA_BUS_TYPE_PCI) {         /* ASIC board */
3074                 writew(0x3800, baseAddr + TMS320_PORT1);
3075                 writew(0x3900, baseAddr + TMS320_PORT2);
3076                 writew(28499, baseAddr + TMS320_CLOCK);
3077         } else {
3078                 writew(0x3200, baseAddr + TMS320_PORT1);
3079                 writew(0x3400, baseAddr + TMS320_PORT2);
3080                 writew(19999, baseAddr + TMS320_CLOCK);
3081         }
3082         writew(1, baseAddr + Disable_IRQ);
3083         writew(0, baseAddr + Magic_no);
3084         for (i = 0; i < 500; i++) {
3085                 if (readw(baseAddr + Magic_no) == Magic_code)
3086                         break;
3087                 moxadelay(1);
3088         }
3089         if (readw(baseAddr + Magic_no) != Magic_code)
3090                 return (-102);
3091
3092         j = readw(baseAddr + Module_cnt);
3093         if (j <= 0)
3094                 return (-101);
3095         *numPorts = j * 8;
3096         writew(j, baseAddr + Module_no);
3097         writew(0, baseAddr + Magic_no);
3098         for (i = 0; i < 600; i++) {
3099                 if (readw(baseAddr + Magic_no) == Magic_code)
3100                         break;
3101                 moxadelay(1);
3102         }
3103         if (readw(baseAddr + Magic_no) != Magic_code)
3104                 return (-102);
3105         moxaCard = 1;
3106         moxaIntNdx[cardno] = baseAddr + IRQindex;
3107         moxaIntPend[cardno] = baseAddr + IRQpending;
3108         moxaIntTable[cardno] = baseAddr + IRQtable;
3109         return (0);
3110 }
3111
3112 long MoxaPortGetCurBaud(int port)
3113 {
3114
3115         if (moxaChkPort[port] == 0)
3116                 return (0);
3117         return (moxaCurBaud[port]);
3118 }
3119
3120 static void MoxaSetFifo(int port, int enable)
3121 {
3122         unsigned long ofsAddr = moxaTableAddr[port];
3123
3124         if (!enable) {
3125                 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
3126                 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
3127         } else {
3128                 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
3129                 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
3130         }
3131 }
3132
3133 #if 0
3134 int MoxaPortSetMode(int port, int databits, int stopbits, int parity)
3135 {
3136         unsigned long ofsAddr;
3137         int val;
3138
3139         val = 0;
3140         switch (databits) {
3141         case 5:
3142                 val |= 0;
3143                 break;
3144         case 6:
3145                 val |= 1;
3146                 break;
3147         case 7:
3148                 val |= 2;
3149                 break;
3150         case 8:
3151                 val |= 3;
3152                 break;
3153         default:
3154                 return (-1);
3155         }
3156         switch (stopbits) {
3157         case 0:
3158                 val |= 0;
3159                 break;          /* stop bits 1.5 */
3160         case 1:
3161                 val |= 0;
3162                 break;
3163         case 2:
3164                 val |= 4;
3165                 break;
3166         default:
3167                 return (-1);
3168         }
3169         switch (parity) {
3170         case 0:
3171                 val |= 0x00;
3172                 break;          /* None  */
3173         case 1:
3174                 val |= 0x08;
3175                 break;          /* Odd   */
3176         case 2:
3177                 val |= 0x18;
3178                 break;          /* Even  */
3179         case 3:
3180                 val |= 0x28;
3181                 break;          /* Mark  */
3182         case 4:
3183                 val |= 0x38;
3184                 break;          /* Space */
3185         default:
3186                 return (-1);
3187         }
3188         ofsAddr = moxaTableAddr[port];
3189         moxafunc(ofsAddr, FC_SetMode, val);
3190         return (0);
3191 }
3192
3193 int MoxaPortTxBufSize(int port)
3194 {
3195         unsigned long ofsAddr;
3196         int size;
3197
3198         ofsAddr = moxaTableAddr[port];
3199         size = readw(ofsAddr + TX_mask);
3200         return (size);
3201 }
3202
3203 int MoxaPortRxBufSize(int port)
3204 {
3205         unsigned long ofsAddr;
3206         int size;
3207
3208         ofsAddr = moxaTableAddr[port];
3209         size = readw(ofsAddr + RX_mask);
3210         return (size);
3211 }
3212
3213 int MoxaPortRxFree(int port)
3214 {
3215         unsigned long ofsAddr;
3216         ushort rptr, wptr, mask;
3217         int len;
3218
3219         ofsAddr = moxaTableAddr[port];
3220         rptr = readw(ofsAddr + RXrptr);
3221         wptr = readw(ofsAddr + RXwptr);
3222         mask = readw(ofsAddr + RX_mask);
3223         len = mask - ((wptr - rptr) & mask);
3224         return (len);
3225 }
3226 int MoxaPortGetBrkCnt(int port)
3227 {
3228         return (moxaBreakCnt[port]);
3229 }
3230
3231 void MoxaPortSetXonXoff(int port, int xonValue, int xoffValue)
3232 {
3233         unsigned long ofsAddr;
3234
3235         ofsAddr = moxaTableAddr[port];
3236         writew(xonValue, ofsAddr + FuncArg);
3237         writew(xoffValue, ofsAddr + FuncArg1);
3238         writew(FC_SetXonXoff, ofsAddr + FuncCode);
3239         wait_finish(ofsAddr);
3240 }
3241
3242 int MoxaPortIsTxHold(int port)
3243 {
3244         unsigned long ofsAddr;
3245         int val;
3246
3247         ofsAddr = moxaTableAddr[port];
3248         if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
3249             (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
3250                 moxafunc(ofsAddr, FC_GetCCSR, 0);
3251                 val = readw(ofsAddr + FuncArg);
3252                 if (val & 0x04)
3253                         return (1);
3254         } else {
3255                 if (readw(ofsAddr + FlagStat) & Tx_flowOff)
3256                         return (1);
3257         }
3258         return (0);
3259 }
3260 #endif