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