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