1 /*****************************************************************************/
3 * moxa.c -- MOXA Intellio family multiport serial driver.
5 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com.tw).
7 * This code is loosely based on the Linux serial driver, written by
8 * Linus Torvalds, Theodore T'so and others.
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.
17 * MOXA Intellio Series Driver
23 #include <linux/module.h>
24 #include <linux/types.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>
46 #include <asm/system.h>
48 #include <asm/uaccess.h>
50 #define MOXA_VERSION "5.1k"
53 #define MOXACUMAJOR 173
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)
60 * Define the Moxa PCI vendor and device IDs.
62 #define MOXA_BUS_TYPE_ISA 0
63 #define MOXA_BUS_TYPE_PCI 1
66 MOXA_BOARD_C218_PCI
= 1,
73 static char *moxa_brdname
[] =
75 "C218 Turbo PCI series",
76 "C218 Turbo ISA series",
77 "C320 Turbo PCI series",
78 "C320 Turbo ISA series",
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
},
92 MODULE_DEVICE_TABLE(pci
, moxa_pcibrds
);
93 #endif /* CONFIG_PCI */
95 static struct moxa_board_conf
{
98 unsigned long baseAddr
;
103 void __iomem
*basemem
;
104 void __iomem
*intNdx
;
105 void __iomem
*intPend
;
106 void __iomem
*intTable
;
107 } moxa_boards
[MAX_BOARDS
];
109 struct mxser_mstatus
{
126 unsigned short closing_wait
;
129 long event
; /* long req'd for set_bit --RR */
131 unsigned long statusflags
;
132 struct tty_struct
*tty
;
134 wait_queue_head_t open_wait
;
135 struct completion close_wait
;
137 struct timer_list emptyTimer
;
141 void __iomem
*tableAddr
;
150 #define TXSTOPPED 0x1
152 #define EMPTYWAIT 0x4
155 #define SERIAL_DO_RESTART
157 #define WAKEUP_CHARS 256
159 static int ttymajor
= MOXAMAJOR
;
160 /* Variables for insmod */
162 static int baseaddr
[4];
164 static int numports
[4];
167 MODULE_AUTHOR("William Chen");
168 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
169 MODULE_LICENSE("GPL");
171 module_param_array(type
, int, NULL
, 0);
172 module_param_array(baseaddr
, int, NULL
, 0);
173 module_param_array(numports
, int, NULL
, 0);
175 module_param(ttymajor
, int, 0);
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
*,
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
*);
207 * moxa board interface functions:
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
);
239 static const struct tty_operations moxa_ops
= {
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
,
249 .throttle
= moxa_throttle
,
250 .unthrottle
= moxa_unthrottle
,
251 .set_termios
= moxa_set_termios
,
254 .hangup
= moxa_hangup
,
255 .tiocmget
= moxa_tiocmget
,
256 .tiocmset
= moxa_tiocmset
,
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
);
265 static int __devinit
moxa_pci_probe(struct pci_dev
*pdev
,
266 const struct pci_device_id
*ent
)
268 struct moxa_board_conf
*board
;
270 int board_type
= ent
->driver_data
;
273 retval
= pci_enable_device(pdev
);
275 dev_err(&pdev
->dev
, "can't enable pci device\n");
279 for (i
= 0; i
< MAX_BOARDS
; i
++)
280 if (moxa_boards
[i
].basemem
== NULL
)
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
);
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");
297 board
->boardType
= board_type
;
298 switch (board_type
) {
299 case MOXA_BOARD_C218_ISA
:
300 case MOXA_BOARD_C218_PCI
:
304 case MOXA_BOARD_CP204J
:
311 board
->busType
= MOXA_BUS_TYPE_PCI
;
313 pci_set_drvdata(pdev
, board
);
320 static void __devexit
moxa_pci_remove(struct pci_dev
*pdev
)
322 struct moxa_board_conf
*brd
= pci_get_drvdata(pdev
);
324 pci_iounmap(pdev
, brd
->basemem
);
328 static struct pci_driver moxa_pci_driver
= {
330 .id_table
= moxa_pcibrds
,
331 .probe
= moxa_pci_probe
,
332 .remove
= __devexit_p(moxa_pci_remove
)
334 #endif /* CONFIG_PCI */
336 static int __init
moxa_init(void)
338 int i
, numBoards
= 0, retval
= 0;
339 struct moxa_port
*ch
;
341 printk(KERN_INFO
"MOXA Intellio family driver version %s\n",
343 moxaDriver
= alloc_tty_driver(MAX_PORTS
+ 1);
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
);
360 for (i
= 0, ch
= moxa_ports
; i
< MAX_PORTS
; i
++, ch
++) {
361 ch
->type
= PORT_16550A
;
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
);
369 setup_timer(&ch
->emptyTimer
, moxa_check_xmit_empty
,
373 pr_debug("Moxa tty devices major number = %d\n", ttymajor
);
375 if (tty_register_driver(moxaDriver
)) {
376 printk(KERN_ERR
"Couldn't install MOXA Smartio family driver !\n");
377 put_tty_driver(moxaDriver
);
381 mod_timer(&moxaTimer
, jiffies
+ HZ
/ 50);
383 /* Find the boards defined form module args. */
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
);
397 moxa_boards
[numBoards
].boardType
= type
[i
];
398 if (type
[i
] == MOXA_BOARD_C218_ISA
)
399 moxa_boards
[numBoards
].numPorts
= 8;
401 moxa_boards
[numBoards
].numPorts
= numports
[i
];
402 moxa_boards
[numBoards
].busType
= MOXA_BUS_TYPE_ISA
;
403 moxa_boards
[numBoards
].baseAddr
= baseaddr
[i
];
410 retval
= pci_register_driver(&moxa_pci_driver
);
412 printk(KERN_ERR
"Can't register moxa pci driver!\n");
418 for (i
= 0; i
< numBoards
; i
++) {
419 moxa_boards
[i
].basemem
= ioremap(moxa_boards
[i
].baseAddr
,
426 static void __exit
moxa_exit(void)
430 del_timer_sync(&moxaTimer
);
432 for (i
= 0; i
< MAX_PORTS
; i
++)
433 del_timer_sync(&moxa_ports
[i
].emptyTimer
);
435 if (tty_unregister_driver(moxaDriver
))
436 printk(KERN_ERR
"Couldn't unregister MOXA Intellio family "
438 put_tty_driver(moxaDriver
);
441 pci_unregister_driver(&moxa_pci_driver
);
444 for (i
= 0; i
< MAX_BOARDS
; i
++)
445 if (moxa_boards
[i
].basemem
)
446 iounmap(moxa_boards
[i
].basemem
);
449 module_init(moxa_init
);
450 module_exit(moxa_exit
);
452 static int moxa_open(struct tty_struct
*tty
, struct file
*filp
)
454 struct moxa_port
*ch
;
459 if (port
== MAX_PORTS
) {
462 if (!MoxaPortIsValid(port
)) {
463 tty
->driver_data
= NULL
;
467 ch
= &moxa_ports
[port
];
469 tty
->driver_data
= ch
;
471 if (!(ch
->asyncflags
& ASYNC_INITIALIZED
)) {
473 moxa_set_tty_param(tty
, tty
->termios
);
474 MoxaPortLineCtrl(ch
->port
, 1, 1);
475 MoxaPortEnable(ch
->port
);
476 ch
->asyncflags
|= ASYNC_INITIALIZED
;
478 retval
= moxa_block_till_ready(tty
, filp
, ch
);
480 moxa_unthrottle(tty
);
482 if (ch
->type
== PORT_16550A
) {
483 MoxaSetFifo(ch
->port
, 1);
485 MoxaSetFifo(ch
->port
, 0);
491 static void moxa_close(struct tty_struct
*tty
, struct file
*filp
)
493 struct moxa_port
*ch
;
497 if (port
== MAX_PORTS
) {
500 if (!MoxaPortIsValid(port
)) {
501 pr_debug("Invalid portno in moxa_close\n");
502 tty
->driver_data
= NULL
;
505 if (tty
->driver_data
== NULL
) {
508 if (tty_hung_up_p(filp
)) {
511 ch
= (struct moxa_port
*) tty
->driver_data
;
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
);
518 if (--ch
->count
< 0) {
519 printk(KERN_WARNING
"moxa_close: bad serial port count, "
520 "device=%s\n", tty
->name
);
526 ch
->asyncflags
|= ASYNC_CLOSING
;
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
);
535 MoxaPortFlushData(port
, 2);
537 if (tty
->driver
->flush_buffer
)
538 tty
->driver
->flush_buffer(tty
);
539 tty_ldisc_flush(tty
);
544 if (ch
->blocked_open
) {
545 if (ch
->close_delay
) {
546 msleep_interruptible(jiffies_to_msecs(ch
->close_delay
));
548 wake_up_interruptible(&ch
->open_wait
);
550 ch
->asyncflags
&= ~(ASYNC_NORMAL_ACTIVE
| ASYNC_CLOSING
);
551 complete_all(&ch
->close_wait
);
554 static int moxa_write(struct tty_struct
*tty
,
555 const unsigned char *buf
, int count
)
557 struct moxa_port
*ch
;
561 ch
= (struct moxa_port
*) tty
->driver_data
;
566 spin_lock_irqsave(&moxa_lock
, flags
);
567 len
= MoxaPortWriteData(port
, (unsigned char *) buf
, count
);
568 spin_unlock_irqrestore(&moxa_lock
, flags
);
570 /*********************************************
571 if ( !(ch->statusflags & LOWWAIT) &&
572 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
573 ************************************************/
574 ch
->statusflags
|= LOWWAIT
;
578 static int moxa_write_room(struct tty_struct
*tty
)
580 struct moxa_port
*ch
;
584 ch
= (struct moxa_port
*) tty
->driver_data
;
587 return (MoxaPortTxFree(ch
->port
));
590 static void moxa_flush_buffer(struct tty_struct
*tty
)
592 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
596 MoxaPortFlushData(ch
->port
, 1);
600 static int moxa_chars_in_buffer(struct tty_struct
*tty
)
603 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
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
613 chars
= MoxaPortTxQueue(ch
->port
);
616 * Make it possible to wakeup anything waiting for output
617 * in tty_ioctl.c, etc.
619 if (!(ch
->statusflags
& EMPTYWAIT
))
620 moxa_setup_empty_event(tty
);
625 static void moxa_flush_chars(struct tty_struct
*tty
)
628 * Don't think I need this, because this is called to empty the TX
629 * buffer for the 16450, 16550, etc.
633 static void moxa_put_char(struct tty_struct
*tty
, unsigned char c
)
635 struct moxa_port
*ch
;
639 ch
= (struct moxa_port
*) tty
->driver_data
;
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
;
652 static int moxa_tiocmget(struct tty_struct
*tty
, struct file
*file
)
654 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
656 int flag
= 0, dtr
, rts
;
659 if ((port
!= MAX_PORTS
) && (!ch
))
662 MoxaPortGetLineOut(ch
->port
, &dtr
, &rts
);
667 dtr
= MoxaPortLineStatus(ch
->port
);
677 static int moxa_tiocmset(struct tty_struct
*tty
, struct file
*file
,
678 unsigned int set
, unsigned int clear
)
680 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
685 if ((port
!= MAX_PORTS
) && (!ch
))
688 MoxaPortGetLineOut(ch
->port
, &dtr
, &rts
);
693 if (clear
& TIOCM_RTS
)
695 if (clear
& TIOCM_DTR
)
697 MoxaPortLineCtrl(ch
->port
, dtr
, rts
);
701 static int moxa_ioctl(struct tty_struct
*tty
, struct file
*file
,
702 unsigned int cmd
, unsigned long arg
)
704 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
706 void __user
*argp
= (void __user
*)arg
;
710 if ((port
!= MAX_PORTS
) && (!ch
))
714 case TCSBRK
: /* SVID version: non-zero arg --> no break */
715 retval
= tty_check_change(tty
);
718 moxa_setup_empty_event(tty
);
719 tty_wait_until_sent(tty
, 0);
721 MoxaPortSendBreak(ch
->port
, 0);
723 case TCSBRKP
: /* support for POSIX tcsendbreak() */
724 retval
= tty_check_change(tty
);
727 moxa_setup_empty_event(tty
);
728 tty_wait_until_sent(tty
, 0);
729 MoxaPortSendBreak(ch
->port
, arg
);
732 return put_user(C_CLOCAL(tty
) ? 1 : 0, (unsigned long __user
*) argp
);
734 if(get_user(retval
, (unsigned long __user
*) argp
))
737 tty
->termios
->c_cflag
= ((tty
->termios
->c_cflag
& ~CLOCAL
) |
740 ch
->asyncflags
&= ~ASYNC_CHECK_CD
;
742 ch
->asyncflags
|= ASYNC_CHECK_CD
;
745 return moxa_get_serial_info(ch
, argp
);
748 return moxa_set_serial_info(ch
, argp
);
750 retval
= MoxaDriverIoctl(cmd
, arg
, port
);
755 static void moxa_throttle(struct tty_struct
*tty
)
757 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
759 ch
->statusflags
|= THROTTLE
;
762 static void moxa_unthrottle(struct tty_struct
*tty
)
764 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
766 ch
->statusflags
&= ~THROTTLE
;
769 static void moxa_set_termios(struct tty_struct
*tty
,
770 struct ktermios
*old_termios
)
772 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
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
);
782 static void moxa_stop(struct tty_struct
*tty
)
784 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
788 MoxaPortTxDisable(ch
->port
);
789 ch
->statusflags
|= TXSTOPPED
;
793 static void moxa_start(struct tty_struct
*tty
)
795 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
800 if (!(ch
->statusflags
& TXSTOPPED
))
803 MoxaPortTxEnable(ch
->port
);
804 ch
->statusflags
&= ~TXSTOPPED
;
807 static void moxa_hangup(struct tty_struct
*tty
)
809 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
811 moxa_flush_buffer(tty
);
815 ch
->asyncflags
&= ~ASYNC_NORMAL_ACTIVE
;
817 wake_up_interruptible(&ch
->open_wait
);
820 static void moxa_poll(unsigned long ignored
)
823 struct moxa_port
*ch
;
824 struct tty_struct
*tp
;
827 del_timer(&moxaTimer
);
829 if (MoxaDriverPoll() < 0) {
830 mod_timer(&moxaTimer
, jiffies
+ HZ
/ 50);
833 for (card
= 0; card
< MAX_BOARDS
; card
++) {
834 if ((ports
= MoxaPortsOfCard(card
)) <= 0)
836 ch
= &moxa_ports
[card
* MAX_PORTS_PER_BOARD
];
837 for (i
= 0; i
< ports
; i
++, ch
++) {
838 if ((ch
->asyncflags
& ASYNC_INITIALIZED
) == 0)
840 if (!(ch
->statusflags
& THROTTLE
) &&
841 (MoxaPortRxQueue(ch
->port
) > 0))
842 moxa_receive_data(ch
);
843 if ((tp
= ch
->tty
) == 0)
845 if (ch
->statusflags
& LOWWAIT
) {
846 if (MoxaPortTxQueue(ch
->port
) <= WAKEUP_CHARS
) {
848 ch
->statusflags
&= ~LOWWAIT
;
853 if (!I_IGNBRK(tp
) && (MoxaPortResetBrkCnt(ch
->port
) > 0)) {
854 tty_insert_flip_char(tp
, 0, TTY_BREAK
);
855 tty_schedule_flip(tp
);
857 if (MoxaPortDCDChange(ch
->port
)) {
858 if (ch
->asyncflags
& ASYNC_CHECK_CD
) {
859 if (MoxaPortDCDON(ch
->port
))
860 wake_up_interruptible(&ch
->open_wait
);
863 wake_up_interruptible(&ch
->open_wait
);
864 ch
->asyncflags
&= ~ASYNC_NORMAL_ACTIVE
;
871 mod_timer(&moxaTimer
, jiffies
+ HZ
/ 50);
874 /******************************************************************************/
876 static void moxa_set_tty_param(struct tty_struct
*tty
, struct ktermios
*old_termios
)
878 register struct ktermios
*ts
;
879 struct moxa_port
*ch
;
880 int rts
, cts
, txflow
, rxflow
, xany
, baud
;
882 ch
= (struct moxa_port
*) tty
->driver_data
;
884 if (ts
->c_cflag
& CLOCAL
)
885 ch
->asyncflags
&= ~ASYNC_CHECK_CD
;
887 ch
->asyncflags
|= ASYNC_CHECK_CD
;
888 rts
= cts
= txflow
= rxflow
= xany
= 0;
889 if (ts
->c_cflag
& CRTSCTS
)
891 if (ts
->c_iflag
& IXON
)
893 if (ts
->c_iflag
& IXOFF
)
895 if (ts
->c_iflag
& IXANY
)
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
));
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
);
908 static int moxa_block_till_ready(struct tty_struct
*tty
, struct file
*filp
,
909 struct moxa_port
*ch
)
911 DECLARE_WAITQUEUE(wait
,current
);
914 int do_clocal
= C_CLOCAL(tty
);
917 * If the device is in the middle of being closed, then block
918 * until it's done, and then try again.
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
)
927 return (-ERESTARTSYS
);
933 * If non-blocking mode is set, then make the check up front
936 if (filp
->f_flags
& O_NONBLOCK
) {
937 ch
->asyncflags
|= ASYNC_NORMAL_ACTIVE
;
941 * Block waiting for the carrier detect and the line to become free
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
))
951 spin_unlock_irqrestore(&moxa_lock
, flags
);
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
)
961 retval
= -ERESTARTSYS
;
967 if (!(ch
->asyncflags
& ASYNC_CLOSING
) && (do_clocal
||
968 MoxaPortDCDON(ch
->port
)))
971 if (signal_pending(current
)) {
972 retval
= -ERESTARTSYS
;
977 set_current_state(TASK_RUNNING
);
978 remove_wait_queue(&ch
->open_wait
, &wait
);
980 spin_lock_irqsave(&moxa_lock
, flags
);
981 if (!tty_hung_up_p(filp
))
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
);
989 /* FIXME: review to see if we need to use set_bit on these */
990 ch
->asyncflags
|= ASYNC_NORMAL_ACTIVE
;
994 static void moxa_setup_empty_event(struct tty_struct
*tty
)
996 struct moxa_port
*ch
= tty
->driver_data
;
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
);
1005 static void moxa_check_xmit_empty(unsigned long data
)
1007 struct moxa_port
*ch
;
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
);
1016 mod_timer(&moxa_ports
[ch
->port
].emptyTimer
,
1017 round_jiffies(jiffies
+ HZ
));
1019 ch
->statusflags
&= ~EMPTYWAIT
;
1022 static void moxa_shut_down(struct moxa_port
*ch
)
1024 struct tty_struct
*tp
;
1026 if (!(ch
->asyncflags
& ASYNC_INITIALIZED
))
1031 MoxaPortDisable(ch
->port
);
1034 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1036 if (tp
->termios
->c_cflag
& HUPCL
)
1037 MoxaPortLineCtrl(ch
->port
, 0, 0);
1039 ch
->asyncflags
&= ~ASYNC_INITIALIZED
;
1042 static void moxa_receive_data(struct moxa_port
*ch
)
1044 struct tty_struct
*tp
;
1045 struct ktermios
*ts
;
1046 unsigned long flags
;
1052 /**************************************************
1053 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1054 *****************************************************/
1056 MoxaPortFlushData(ch
->port
, 0);
1059 spin_lock_irqsave(&moxa_lock
, flags
);
1060 MoxaPortReadData(ch
->port
, tp
);
1061 spin_unlock_irqrestore(&moxa_lock
, flags
);
1062 tty_schedule_flip(tp
);
1065 #define Magic_code 0x404
1068 * System Configuration
1071 * for C218 BIOS initialization
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 */
1085 #define C218_LoadBuf 0x0F00
1086 #define C218_KeyCode 0x218
1087 #define CP204J_KeyCode 0x204
1090 * for C320 BIOS initialization
1092 #define C320_ConfBase 0x800
1093 #define C320_LoadBuf 0x0f00
1094 #define STS_init 0x05 /* for C320_status */
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 */
1105 #define C320_KeyCode 0x320
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
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
1163 #define RxFIFOTrig1 0
1164 #define RxFIFOTrig4 1
1165 #define RxFIFOTrig8 2
1166 #define RxFIFOTrig14 3
1171 #define DRAM_global 0
1172 #define INT_data (DRAM_global + 0)
1173 #define Config_base (DRAM_global + 0x108)
1175 #define IRQindex (INT_data + 0)
1176 #define IRQpending (INT_data + 4)
1177 #define IRQtable (INT_data + 8)
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
1188 #define IntrIntr 0x20 /* received INTR code */
1189 #define IntrQuit 0x40 /* received QUIT code */
1190 #define IntrEOF 0x80 /* received EOF code */
1192 #define IntrRxTrigger 0x100 /* rx data count reach tigger value */
1193 #define IntrTxTrigger 0x200 /* tx data count below trigger value */
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)
1207 * DATA BUFFER in DRAM
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
1239 #define rRXwptr 0x34
1240 #define Low_water 0x36
1242 #define FuncCode 0x40
1243 #define FuncArg 0x42
1244 #define FuncArg1 0x44
1246 #define C218rx_size 0x2000 /* 8K bytes */
1247 #define C218tx_size 0x8000 /* 32K bytes */
1249 #define C218rx_mask (C218rx_size - 1)
1250 #define C218tx_mask (C218tx_size - 1)
1252 #define C320p8rx_size 0x2000
1253 #define C320p8tx_size 0x8000
1254 #define C320p8rx_mask (C320p8rx_size - 1)
1255 #define C320p8tx_mask (C320p8tx_size - 1)
1257 #define C320p16rx_size 0x2000
1258 #define C320p16tx_size 0x4000
1259 #define C320p16rx_mask (C320p16rx_size - 1)
1260 #define C320p16tx_mask (C320p16tx_size - 1)
1262 #define C320p24rx_size 0x2000
1263 #define C320p24tx_size 0x2000
1264 #define C320p24rx_mask (C320p24rx_size - 1)
1265 #define C320p24tx_mask (C320p24tx_size - 1)
1267 #define C320p32rx_size 0x1000
1268 #define C320p32tx_size 0x1000
1269 #define C320p32rx_mask (C320p32rx_size - 1)
1270 #define C320p32tx_mask (C320p32tx_size - 1)
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
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
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
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
1298 #define C320p32rx_spage 3
1299 #define C320p32tx_ofs C320p32rx_size
1300 #define C320p32tx_spage 3
1301 #define C320p32buf_pgno 1
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
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
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
1334 #define LowWater 128
1342 /* mode definition */
1348 #define MX_STOP1 0x00
1349 #define MX_STOP15 0x04
1350 #define MX_STOP2 0x08
1352 #define MX_PARNONE 0x00
1353 #define MX_PAREVEN 0x40
1354 #define MX_PARODD 0xC0
1362 int rxcnt
[MAX_PORTS
];
1363 int txcnt
[MAX_PORTS
];
1366 #define DCD_changed 0x01
1367 #define DCD_oldstate 0x80
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;
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 *);
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)
1393 struct moxa_port
*p
;
1396 moxaFuncTout
= HZ
/ 2; /* 500 mini-seconds */
1399 moxaLowWaterChk
= 0;
1400 for (i
= 0; i
< MAX_PORTS
; i
++) {
1405 moxaLog
.rxcnt
[i
] = 0;
1406 moxaLog
.txcnt
[i
] = 0;
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)
1432 static struct dl_str dltmp
;
1434 void MoxaPortFlushData(int port
, int mode
)
1436 void __iomem
*ofsAddr
;
1437 if ((mode
< 0) || (mode
> 2))
1439 ofsAddr
= moxa_ports
[port
].tableAddr
;
1440 moxafunc(ofsAddr
, FC_FlushQueue
, mode
);
1442 moxa_ports
[port
].lowChkFlag
= 0;
1443 moxa_low_water_check(ofsAddr
);
1447 int MoxaDriverIoctl(unsigned int cmd
, unsigned long arg
, int port
)
1451 int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
1452 void __user
*argp
= (void __user
*)arg
;
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
))
1464 if(copy_to_user(argp
, &moxa_boards
, MAX_BOARDS
*
1465 sizeof(struct moxa_board_conf
)))
1468 case MOXA_INIT_DRIVER
:
1469 if ((int) arg
== 0x404)
1472 case MOXA_GETDATACOUNT
:
1473 moxaLog
.tick
= jiffies
;
1474 if(copy_to_user(argp
, &moxaLog
, sizeof(struct mon_str
)))
1477 case MOXA_FLUSH_QUEUE
:
1478 MoxaPortFlushData(port
, arg
);
1480 case MOXA_GET_IOQUEUE
: {
1481 struct moxaq_str __user
*argm
= argp
;
1482 struct moxaq_str tmp
;
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
);
1490 if (copy_to_user(argm
, &tmp
, sizeof(tmp
)))
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)))
1504 case MOXA_GET_CUMAJOR
:
1506 if(copy_to_user(argp
, &i
, sizeof(int)))
1509 case MOXA_GETMSTATUS
: {
1510 struct mxser_mstatus __user
*argm
= argp
;
1511 struct mxser_mstatus tmp
;
1512 struct moxa_port
*p
;
1514 for (i
= 0; i
< MAX_PORTS
; i
++, argm
++) {
1516 memset(&tmp
, 0, sizeof(tmp
));
1520 status
= MoxaPortLineStatus(p
->port
);
1529 if (!p
->tty
|| !p
->tty
->termios
)
1530 tmp
.cflag
= p
->cflag
;
1532 tmp
.cflag
= p
->tty
->termios
->c_cflag
;
1534 if (copy_to_user(argm
, &tmp
, sizeof(tmp
)))
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
))
1549 if(copy_from_user(&dltmp
, argp
, sizeof(struct dl_str
)))
1551 if(dltmp
.cardno
< 0 || dltmp
.cardno
>= MAX_BOARDS
|| dltmp
.len
< 0)
1556 case MOXA_LOAD_BIOS
:
1557 i
= moxaloadbios(dltmp
.cardno
, dltmp
.buf
, dltmp
.len
);
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 */
1565 case MOXA_LOAD_CODE
:
1566 i
= moxaloadcode(dltmp
.cardno
, dltmp
.buf
, dltmp
.len
);
1574 int MoxaDriverPoll(void)
1576 struct moxa_board_conf
*brd
;
1577 register ushort temp
;
1579 void __iomem
*ofsAddr
;
1585 for (card
= 0; card
< MAX_BOARDS
; card
++) {
1586 brd
= &moxa_boards
[card
];
1587 if (brd
->loadstat
== 0)
1589 if ((ports
= brd
->numPorts
) == 0)
1591 if (readb(brd
->intPend
) == 0xff) {
1592 ip
= brd
->intTable
+ readb(brd
->intNdx
);
1593 p
= card
* MAX_PORTS_PER_BOARD
;
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
;
1600 writew(readw(ofsAddr
+ HostStat
) & ~WakeupTx
, ofsAddr
+ HostStat
);
1601 if (temp
& IntrBreak
) {
1602 moxa_ports
[p
].breakCnt
++;
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
|
1610 if (moxa_ports
[p
].DCDState
& DCD_oldstate
)
1611 moxa_ports
[p
].DCDState
= DCD_changed
;
1616 writeb(0, brd
->intPend
);
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
);
1629 moxaLowWaterChk
= 0;
1633 /*****************************************************************************
1634 * Card level function: *
1635 * 1. MoxaPortsOfCard(int cardno); *
1636 *****************************************************************************/
1637 int MoxaPortsOfCard(int cardno
)
1640 if (moxa_boards
[cardno
].boardType
== 0)
1642 return (moxa_boards
[cardno
].numPorts
);
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 *****************************************************************************/
1671 * Moxa Port Number Description:
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.
1685 * Moxa Functions Description:
1687 * Function 1: Driver initialization routine, this routine must be
1688 * called when initialized driver.
1690 * void MoxaDriverInit();
1693 * Function 2: Moxa driver private IOCTL command processing.
1695 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1697 * unsigned int cmd : IOCTL command
1698 * unsigned long arg : IOCTL argument
1699 * int port : port number (0 - 127)
1706 * Function 3: Moxa driver polling process routine.
1708 * int MoxaDriverPoll(void);
1710 * return: 0 ; polling O.K.
1711 * -1 : no any Moxa card.
1714 * Function 4: Get the ports of this card.
1716 * int MoxaPortsOfCard(int cardno);
1718 * int cardno : card number (0 - 3)
1720 * return: 0 : this card is invalid
1724 * Function 5: Check this port is valid or invalid
1726 * int MoxaPortIsValid(int port);
1727 * int port : port number (0 - 127, ref port description)
1729 * return: 0 : this port is invalid
1730 * 1 : this port is valid
1733 * Function 6: Enable this port to start Tx/Rx data.
1735 * void MoxaPortEnable(int port);
1736 * int port : port number (0 - 127)
1739 * Function 7: Disable this port
1741 * void MoxaPortDisable(int port);
1742 * int port : port number (0 - 127)
1745 * Function 8: Get the maximun available baud rate of this port.
1747 * long MoxaPortGetMaxBaud(int port);
1748 * int port : port number (0 - 127)
1750 * return: 0 : this port is invalid
1751 * 38400/57600/115200 bps
1754 * Function 10: Setting baud rate of this port.
1756 * long MoxaPortSetBaud(int port, long baud);
1757 * int port : port number (0 - 127)
1758 * long baud : baud rate (50 - 115200)
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.
1767 * Function 12: Configure the port.
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
1774 * return: -1 : this port is invalid or termio == NULL
1778 * Function 13: Get the DTR/RTS state of this port.
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)
1789 * return: -1 : this port is invalid
1793 * Function 14: Setting the DTR/RTS output state of this port.
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)
1801 * Function 15: Setting the flow control of this port.
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)
1813 * Function 16: Get ths line status of this port
1815 * int MoxaPortLineStatus(int port);
1816 * int port : port number (0 - 127)
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)
1823 * Function 17: Check the DCD state has changed since the last read
1826 * int MoxaPortDCDChange(int port);
1827 * int port : port number (0 - 127)
1829 * return: 0 : no changed
1830 * 1 : DCD has changed
1833 * Function 18: Check ths current DCD state is ON or not.
1835 * int MoxaPortDCDON(int port);
1836 * int port : port number (0 - 127)
1838 * return: 0 : DCD off
1842 * Function 19: Flush the Rx/Tx buffer data of this port.
1844 * void MoxaPortFlushData(int port, int mode);
1845 * int port : port number (0 - 127)
1847 * 0 : flush the Rx buffer
1848 * 1 : flush the Tx buffer
1849 * 2 : flush the Rx and Tx buffer
1852 * Function 20: Write data.
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
1859 * return: 0 - length : real write data length
1862 * Function 21: Read data.
1864 * int MoxaPortReadData(int port, struct tty_struct *tty);
1865 * int port : port number (0 - 127)
1866 * struct tty_struct *tty : tty for data
1868 * return: 0 - length : real read data length
1871 * Function 24: Get the Tx buffer current queued data bytes
1873 * int MoxaPortTxQueue(int port);
1874 * int port : port number (0 - 127)
1876 * return: .. : Tx buffer current queued data bytes
1879 * Function 25: Get the Tx buffer current free space
1881 * int MoxaPortTxFree(int port);
1882 * int port : port number (0 - 127)
1884 * return: .. : Tx buffer current free space
1887 * Function 26: Get the Rx buffer current queued data bytes
1889 * int MoxaPortRxQueue(int port);
1890 * int port : port number (0 - 127)
1892 * return: .. : Rx buffer current queued data bytes
1895 * Function 28: Disable port data transmission.
1897 * void MoxaPortTxDisable(int port);
1898 * int port : port number (0 - 127)
1901 * Function 29: Enable port data transmission.
1903 * void MoxaPortTxEnable(int port);
1904 * int port : port number (0 - 127)
1907 * Function 31: Get the received BREAK signal count and reset it.
1909 * int MoxaPortResetBrkCnt(int port);
1910 * int port : port number (0 - 127)
1912 * return: 0 - .. : BREAK signal count
1915 * Function 34: Send out a BREAK signal.
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.
1924 int MoxaPortIsValid(int port
)
1929 if (moxa_ports
[port
].chkPort
== 0)
1934 void MoxaPortEnable(int port
)
1936 void __iomem
*ofsAddr
;
1937 int MoxaPortLineStatus(int);
1938 short lowwater
= 512;
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);
1947 writew(readw(ofsAddr
+ HostStat
) | WakeupBreak
, ofsAddr
+ HostStat
);
1950 moxafunc(ofsAddr
, FC_SetLineIrq
, Magic_code
);
1951 moxafunc(ofsAddr
, FC_FlushQueue
, 2);
1953 moxafunc(ofsAddr
, FC_EnableCH
, Magic_code
);
1954 MoxaPortLineStatus(port
);
1957 void MoxaPortDisable(int port
)
1959 void __iomem
*ofsAddr
= moxa_ports
[port
].tableAddr
;
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
);
1967 long MoxaPortGetMaxBaud(int port
)
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
))
1977 long MoxaPortSetBaud(int port
, long baud
)
1979 void __iomem
*ofsAddr
;
1983 if ((baud
< 50L) || ((max
= MoxaPortGetMaxBaud(port
)) == 0))
1985 ofsAddr
= moxa_ports
[port
].tableAddr
;
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 */
1993 clock
= 921600L; /* for 14.7456 Mhz : max. 115200 bps */
1995 moxafunc(ofsAddr
, FC_SetBaud
, val
);
1997 moxa_ports
[port
].curBaud
= baud
;
2001 int MoxaPortSetTermio(int port
, struct ktermios
*termio
, speed_t baud
)
2003 void __iomem
*ofsAddr
;
2007 if (moxa_ports
[port
].chkPort
== 0 || termio
== 0)
2009 ofsAddr
= moxa_ports
[port
].tableAddr
;
2010 cflag
= termio
->c_cflag
; /* termio->c_cflag */
2012 mode
= termio
->c_cflag
& CSIZE
;
2015 else if (mode
== CS6
)
2017 else if (mode
== CS7
)
2019 else if (mode
== CS8
)
2022 if (termio
->c_cflag
& CSTOPB
) {
2030 if (termio
->c_cflag
& PARENB
) {
2031 if (termio
->c_cflag
& PARODD
)
2038 moxafunc(ofsAddr
, FC_SetDataMode
, (ushort
) mode
);
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)
2045 baud
= MoxaPortSetBaud(port
, baud
);
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
);
2057 int MoxaPortGetLineOut(int port
, int *dtrState
, int *rtsState
)
2060 if (!MoxaPortIsValid(port
))
2063 if (moxa_ports
[port
].lineCtrl
& DTR_ON
)
2069 if (moxa_ports
[port
].lineCtrl
& RTS_ON
)
2077 void MoxaPortLineCtrl(int port
, int dtr
, int rts
)
2079 void __iomem
*ofsAddr
;
2082 ofsAddr
= moxa_ports
[port
].tableAddr
;
2088 moxa_ports
[port
].lineCtrl
= mode
;
2089 moxafunc(ofsAddr
, FC_LineControl
, mode
);
2092 void MoxaPortFlowCtrl(int port
, int rts
, int cts
, int txflow
, int rxflow
, int txany
)
2094 void __iomem
*ofsAddr
;
2097 ofsAddr
= moxa_ports
[port
].tableAddr
;
2100 mode
|= RTS_FlowCtl
;
2102 mode
|= CTS_FlowCtl
;
2109 moxafunc(ofsAddr
, FC_SetFlowCtl
, mode
);
2112 int MoxaPortLineStatus(int port
)
2114 void __iomem
*ofsAddr
;
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
);
2123 val
= readw(ofsAddr
+ FlagStat
) >> 4;
2128 if ((moxa_ports
[port
].DCDState
& DCD_oldstate
) == 0)
2129 moxa_ports
[port
].DCDState
= (DCD_oldstate
| DCD_changed
);
2131 if (moxa_ports
[port
].DCDState
& DCD_oldstate
)
2132 moxa_ports
[port
].DCDState
= DCD_changed
;
2138 int MoxaPortDCDChange(int port
)
2142 if (moxa_ports
[port
].chkPort
== 0)
2144 n
= moxa_ports
[port
].DCDState
;
2145 moxa_ports
[port
].DCDState
&= ~DCD_changed
;
2150 int MoxaPortDCDON(int port
)
2154 if (moxa_ports
[port
].chkPort
== 0)
2156 if (moxa_ports
[port
].DCDState
& DCD_oldstate
)
2163 int MoxaPortWriteData(int port
, unsigned char * buffer
, int len
)
2168 ushort head
, tx_mask
, spage
, epage
;
2169 ushort pageno
, pageofs
, bufhead
;
2170 void __iomem
*baseAddr
, *ofsAddr
, *ofs
;
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
);
2183 moxaLog
.txcnt
[port
] += c
;
2185 if (spage
== epage
) {
2186 bufhead
= readw(ofsAddr
+ Ofs_txb
);
2187 writew(spage
, baseAddr
+ Control_reg
);
2190 len
= head
- tail
- 1;
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
;
2200 writew(tail
, ofsAddr
+ TXwptr
);
2203 pageno
= spage
+ (tail
>> 13);
2204 pageofs
= tail
& Page_mask
;
2206 cnt
= Page_size
- pageofs
;
2210 writeb(pageno
, baseAddr
+ Control_reg
);
2211 ofs
= baseAddr
+ DynPage_addr
+ pageofs
;
2212 for (i
= 0; i
< cnt
; i
++)
2213 writeb(*buffer
++, ofs
+ i
);
2215 writew((tail
+ len
) & tx_mask
, ofsAddr
+ TXwptr
);
2218 if (++pageno
== epage
)
2223 writeb(1, ofsAddr
+ CD180TXirq
); /* start to send */
2227 int MoxaPortReadData(int port
, struct tty_struct
*tty
)
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
;
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);
2248 remain
= count
- total
;
2249 moxaLog
.rxcnt
[port
] += total
;
2251 if (spage
== epage
) {
2252 bufhead
= readw(ofsAddr
+ Ofs_rxb
);
2253 writew(spage
, baseAddr
+ Control_reg
);
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
;
2266 writew(head
, ofsAddr
+ RXrptr
);
2269 pageno
= spage
+ (head
>> 13);
2270 pageofs
= head
& Page_mask
;
2272 cnt
= Page_size
- pageofs
;
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
);
2281 writew((head
+ len
) & rx_mask
, ofsAddr
+ RXrptr
);
2284 if (++pageno
== epage
)
2289 if ((readb(ofsAddr
+ FlagStat
) & Xoff_state
) && (remain
< LowWater
)) {
2290 moxaLowWaterChk
= 1;
2291 moxa_ports
[port
].lowChkFlag
= 1;
2297 int MoxaPortTxQueue(int port
)
2299 void __iomem
*ofsAddr
;
2300 ushort rptr
, wptr
, mask
;
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
;
2311 int MoxaPortTxFree(int port
)
2313 void __iomem
*ofsAddr
;
2314 ushort rptr
, wptr
, mask
;
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
);
2325 int MoxaPortRxQueue(int port
)
2327 void __iomem
*ofsAddr
;
2328 ushort rptr
, wptr
, mask
;
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
;
2340 void MoxaPortTxDisable(int port
)
2342 void __iomem
*ofsAddr
;
2344 ofsAddr
= moxa_ports
[port
].tableAddr
;
2345 moxafunc(ofsAddr
, FC_SetXoffState
, Magic_code
);
2348 void MoxaPortTxEnable(int port
)
2350 void __iomem
*ofsAddr
;
2352 ofsAddr
= moxa_ports
[port
].tableAddr
;
2353 moxafunc(ofsAddr
, FC_SetXonState
, Magic_code
);
2357 int MoxaPortResetBrkCnt(int port
)
2360 cnt
= moxa_ports
[port
].breakCnt
;
2361 moxa_ports
[port
].breakCnt
= 0;
2366 void MoxaPortSendBreak(int port
, int ms100
)
2368 void __iomem
*ofsAddr
;
2370 ofsAddr
= moxa_ports
[port
].tableAddr
;
2372 moxafunc(ofsAddr
, FC_SendBreak
, Magic_code
);
2375 moxafunc(ofsAddr
, FC_SendBreak
, Magic_code
);
2378 moxafunc(ofsAddr
, FC_StopBreak
, Magic_code
);
2381 static int moxa_get_serial_info(struct moxa_port
*info
,
2382 struct serial_struct __user
*retinfo
)
2384 struct serial_struct tmp
;
2386 memset(&tmp
, 0, sizeof(tmp
));
2387 tmp
.type
= info
->type
;
2388 tmp
.line
= info
->port
;
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;
2397 if(copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
2403 static int moxa_set_serial_info(struct moxa_port
*info
,
2404 struct serial_struct __user
*new_info
)
2406 struct serial_struct new_serial
;
2408 if(copy_from_user(&new_serial
, new_info
, sizeof(new_serial
)))
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))
2418 if (!capable(CAP_SYS_ADMIN
)) {
2419 if (((new_serial
.flags
& ~ASYNC_USR_MASK
) !=
2420 (info
->asyncflags
& ~ASYNC_USR_MASK
)))
2423 info
->close_delay
= new_serial
.close_delay
* HZ
/ 100;
2424 info
->closing_wait
= new_serial
.closing_wait
* HZ
/ 100;
2427 new_serial
.flags
= (new_serial
.flags
& ~ASYNC_FLAGS
);
2428 new_serial
.flags
|= (info
->asyncflags
& ASYNC_FLAGS
);
2430 if (new_serial
.type
== PORT_16550A
) {
2431 MoxaSetFifo(info
->port
, 1);
2433 MoxaSetFifo(info
->port
, 0);
2436 info
->type
= new_serial
.type
;
2442 /*****************************************************************************
2443 * Static local functions: *
2444 *****************************************************************************/
2445 static void moxafunc(void __iomem
*ofsAddr
, int cmd
, ushort arg
)
2448 writew(arg
, ofsAddr
+ FuncArg
);
2449 writew(cmd
, ofsAddr
+ FuncCode
);
2450 moxa_wait_finish(ofsAddr
);
2453 static void moxa_wait_finish(void __iomem
*ofsAddr
)
2458 while (readw(ofsAddr
+ FuncCode
) != 0) {
2460 if ((j
- i
) > moxaFuncTout
) {
2466 static void moxa_low_water_check(void __iomem
*ofsAddr
)
2469 ushort rptr
, wptr
, mask
;
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);
2481 static int moxaloadbios(int cardno
, unsigned char __user
*tmp
, int len
)
2483 void __iomem
*baseAddr
;
2486 if(len
< 0 || len
> sizeof(moxaBuff
))
2488 if(copy_from_user(moxaBuff
, tmp
, len
))
2490 baseAddr
= moxa_boards
[cardno
].basemem
;
2491 writeb(HW_reset
, baseAddr
+ Control_reg
); /* reset */
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 */
2501 static int moxafindcard(int cardno
)
2503 void __iomem
*baseAddr
;
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
) {
2514 case MOXA_BOARD_CP204J
:
2515 if ((tmp
= readw(baseAddr
+ C218_key
)) != CP204J_KeyCode
) {
2520 if ((tmp
= readw(baseAddr
+ C320_key
)) != C320_KeyCode
) {
2523 if ((tmp
= readw(baseAddr
+ C320_status
)) != STS_init
) {
2530 static int moxaload320b(int cardno
, unsigned char __user
*tmp
, int len
)
2532 void __iomem
*baseAddr
;
2535 if(len
< 0 || len
> sizeof(moxaBuff
))
2537 if(copy_from_user(moxaBuff
, tmp
, len
))
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
);
2550 static int moxaloadcode(int cardno
, unsigned char __user
*tmp
, int len
)
2552 void __iomem
*baseAddr
, *ofsAddr
;
2553 int retval
, port
, i
;
2555 if(len
< 0 || len
> sizeof(moxaBuff
))
2557 if(copy_from_user(moxaBuff
, tmp
, len
))
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
);
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
];
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
);
2581 writew(C218tx_spage
+ i
* C218buf_pageno
, ofsAddr
+ Page_txb
);
2582 writew(readw(ofsAddr
+ Page_txb
) + C218tx_pageno
, ofsAddr
+ EndPage_txb
);
2587 retval
= moxaloadc320(cardno
, baseAddr
, len
,
2588 &moxa_boards
[cardno
].numPorts
);
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
];
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
);
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
);
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
);
2635 moxa_boards
[cardno
].loadstat
= 1;
2639 static int moxaloadc218(int cardno
, void __iomem
*baseAddr
, int len
)
2642 int i
, j
, len1
, len2
;
2643 ushort usum
, *ptr
, keycode
;
2645 if (moxa_boards
[cardno
].boardType
== MOXA_BOARD_CP204J
)
2646 keycode
= CP204J_KeyCode
;
2648 keycode
= C218_KeyCode
;
2651 ptr
= (ushort
*) moxaBuff
;
2652 for (i
= 0; i
< len1
; i
++)
2653 usum
+= le16_to_cpu(*(ptr
+ i
));
2659 len2
= (len1
> 2048) ? 2048 : len1
;
2661 for (i
= 0; i
< len2
<< 1; i
++)
2662 writeb(moxaBuff
[i
+ j
], baseAddr
+ C218_LoadBuf
+ i
);
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
)
2672 if (readw(baseAddr
+ C218_key
) != keycode
) {
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
)
2685 } while ((readb(baseAddr
+ C218chksum_ok
) != 1) && (retry
< 3));
2686 if (readb(baseAddr
+ C218chksum_ok
) != 1) {
2689 writew(0, baseAddr
+ C218_key
);
2690 for (i
= 0; i
< 100; i
++) {
2691 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
2695 if (readw(baseAddr
+ Magic_no
) != Magic_code
) {
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
)
2705 if (readw(baseAddr
+ Magic_no
) != Magic_code
) {
2709 moxa_boards
[cardno
].intNdx
= baseAddr
+ IRQindex
;
2710 moxa_boards
[cardno
].intPend
= baseAddr
+ IRQpending
;
2711 moxa_boards
[cardno
].intTable
= baseAddr
+ IRQtable
;
2715 static int moxaloadc320(int cardno
, void __iomem
*baseAddr
, int len
, int *numPorts
)
2718 int i
, j
, wlen
, len2
, retry
;
2723 uptr
= (ushort
*) moxaBuff
;
2724 for (i
= 0; i
< wlen
; i
++)
2725 usum
+= le16_to_cpu(uptr
[i
]);
2736 for (i
= 0; i
< len2
; i
++)
2737 writeb(moxaBuff
[j
+ i
], baseAddr
+ C320_LoadBuf
+ 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
)
2747 if (readw(baseAddr
+ C320_key
) != C320_KeyCode
)
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
)
2759 } while ((readb(baseAddr
+ C320chksum_ok
) != 1) && (retry
< 3));
2760 if (readb(baseAddr
+ C320chksum_ok
) != 1)
2762 writew(0, baseAddr
+ C320_key
);
2763 for (i
= 0; i
< 600; i
++) {
2764 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
2768 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
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
);
2776 writew(0x3200, baseAddr
+ TMS320_PORT1
);
2777 writew(0x3400, baseAddr
+ TMS320_PORT2
);
2778 writew(19999, baseAddr
+ TMS320_CLOCK
);
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
)
2787 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
2790 j
= readw(baseAddr
+ Module_cnt
);
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
)
2801 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
2804 moxa_boards
[cardno
].intNdx
= baseAddr
+ IRQindex
;
2805 moxa_boards
[cardno
].intPend
= baseAddr
+ IRQpending
;
2806 moxa_boards
[cardno
].intTable
= baseAddr
+ IRQtable
;
2810 static void MoxaSetFifo(int port
, int enable
)
2812 void __iomem
*ofsAddr
= moxa_ports
[port
].tableAddr
;
2815 moxafunc(ofsAddr
, FC_SetRxFIFOTrig
, 0);
2816 moxafunc(ofsAddr
, FC_SetTxFIFOCnt
, 1);
2818 moxafunc(ofsAddr
, FC_SetRxFIFOTrig
, 3);
2819 moxafunc(ofsAddr
, FC_SetTxFIFOCnt
, 16);