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