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