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