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