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>
45 #include <asm/system.h>
47 #include <asm/uaccess.h>
49 #define MOXA_VERSION "5.1k"
52 #define MOXACUMAJOR 173
54 #define MAX_BOARDS 4 /* Don't change this value */
55 #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
56 #define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
59 * Define the Moxa PCI vendor and device IDs.
61 #define MOXA_BUS_TYPE_ISA 0
62 #define MOXA_BUS_TYPE_PCI 1
65 MOXA_BOARD_C218_PCI
= 1,
72 static char *moxa_brdname
[] =
74 "C218 Turbo PCI series",
75 "C218 Turbo ISA series",
76 "C320 Turbo PCI series",
77 "C320 Turbo ISA series",
82 static struct pci_device_id moxa_pcibrds
[] = {
83 { PCI_DEVICE(PCI_VENDOR_ID_MOXA
, PCI_DEVICE_ID_MOXA_C218
),
84 .driver_data
= MOXA_BOARD_C218_PCI
},
85 { PCI_DEVICE(PCI_VENDOR_ID_MOXA
, PCI_DEVICE_ID_MOXA_C320
),
86 .driver_data
= MOXA_BOARD_C320_PCI
},
87 { PCI_DEVICE(PCI_VENDOR_ID_MOXA
, PCI_DEVICE_ID_MOXA_CP204J
),
88 .driver_data
= MOXA_BOARD_CP204J
},
91 MODULE_DEVICE_TABLE(pci
, moxa_pcibrds
);
92 #endif /* CONFIG_PCI */
94 struct moxa_isa_board_conf
{
97 unsigned long baseAddr
;
100 static struct moxa_isa_board_conf moxa_isa_boards
[] =
102 /* {MOXA_BOARD_C218_ISA,8,0xDC000}, */
105 struct moxa_board_conf
{
108 unsigned long baseAddr
;
110 struct pci_dev
*pdev
;
113 static struct moxa_board_conf moxa_boards
[MAX_BOARDS
];
114 static void __iomem
*moxaBaseAddr
[MAX_BOARDS
];
115 static int loadstat
[MAX_BOARDS
];
121 unsigned short closing_wait
;
124 long event
; /* long req'd for set_bit --RR */
126 unsigned long statusflags
;
127 struct tty_struct
*tty
;
129 wait_queue_head_t open_wait
;
130 wait_queue_head_t close_wait
;
133 struct mxser_mstatus
{
141 static struct mxser_mstatus GMStatus
[MAX_PORTS
];
144 #define TXSTOPPED 0x1
146 #define EMPTYWAIT 0x4
149 #define SERIAL_DO_RESTART
151 #define WAKEUP_CHARS 256
153 static int verbose
= 0;
154 static int ttymajor
= MOXAMAJOR
;
155 /* Variables for insmod */
157 static int baseaddr
[4];
159 static int numports
[4];
162 MODULE_AUTHOR("William Chen");
163 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
164 MODULE_LICENSE("GPL");
166 module_param_array(type
, int, NULL
, 0);
167 module_param_array(baseaddr
, int, NULL
, 0);
168 module_param_array(numports
, int, NULL
, 0);
170 module_param(ttymajor
, int, 0);
171 module_param(verbose
, bool, 0644);
176 static int moxa_open(struct tty_struct
*, struct file
*);
177 static void moxa_close(struct tty_struct
*, struct file
*);
178 static int moxa_write(struct tty_struct
*, const unsigned char *, int);
179 static int moxa_write_room(struct tty_struct
*);
180 static void moxa_flush_buffer(struct tty_struct
*);
181 static int moxa_chars_in_buffer(struct tty_struct
*);
182 static void moxa_flush_chars(struct tty_struct
*);
183 static void moxa_put_char(struct tty_struct
*, unsigned char);
184 static int moxa_ioctl(struct tty_struct
*, struct file
*, unsigned int, unsigned long);
185 static void moxa_throttle(struct tty_struct
*);
186 static void moxa_unthrottle(struct tty_struct
*);
187 static void moxa_set_termios(struct tty_struct
*, struct ktermios
*);
188 static void moxa_stop(struct tty_struct
*);
189 static void moxa_start(struct tty_struct
*);
190 static void moxa_hangup(struct tty_struct
*);
191 static int moxa_tiocmget(struct tty_struct
*tty
, struct file
*file
);
192 static int moxa_tiocmset(struct tty_struct
*tty
, struct file
*file
,
193 unsigned int set
, unsigned int clear
);
194 static void moxa_poll(unsigned long);
195 static void set_tty_param(struct tty_struct
*);
196 static int block_till_ready(struct tty_struct
*, struct file
*,
198 static void setup_empty_event(struct tty_struct
*);
199 static void check_xmit_empty(unsigned long);
200 static void shut_down(struct moxa_str
*);
201 static void receive_data(struct moxa_str
*);
203 * moxa board interface functions:
205 static void MoxaDriverInit(void);
206 static int MoxaDriverIoctl(unsigned int, unsigned long, int);
207 static int MoxaDriverPoll(void);
208 static int MoxaPortsOfCard(int);
209 static int MoxaPortIsValid(int);
210 static void MoxaPortEnable(int);
211 static void MoxaPortDisable(int);
212 static long MoxaPortGetMaxBaud(int);
213 static long MoxaPortSetBaud(int, long);
214 static int MoxaPortSetTermio(int, struct ktermios
*, speed_t
);
215 static int MoxaPortGetLineOut(int, int *, int *);
216 static void MoxaPortLineCtrl(int, int, int);
217 static void MoxaPortFlowCtrl(int, int, int, int, int, int);
218 static int MoxaPortLineStatus(int);
219 static int MoxaPortDCDChange(int);
220 static int MoxaPortDCDON(int);
221 static void MoxaPortFlushData(int, int);
222 static int MoxaPortWriteData(int, unsigned char *, int);
223 static int MoxaPortReadData(int, struct tty_struct
*tty
);
224 static int MoxaPortTxQueue(int);
225 static int MoxaPortRxQueue(int);
226 static int MoxaPortTxFree(int);
227 static void MoxaPortTxDisable(int);
228 static void MoxaPortTxEnable(int);
229 static int MoxaPortResetBrkCnt(int);
230 static void MoxaPortSendBreak(int, int);
231 static int moxa_get_serial_info(struct moxa_str
*, struct serial_struct __user
*);
232 static int moxa_set_serial_info(struct moxa_str
*, struct serial_struct __user
*);
233 static void MoxaSetFifo(int port
, int enable
);
235 static const struct tty_operations moxa_ops
= {
239 .write_room
= moxa_write_room
,
240 .flush_buffer
= moxa_flush_buffer
,
241 .chars_in_buffer
= moxa_chars_in_buffer
,
242 .flush_chars
= moxa_flush_chars
,
243 .put_char
= moxa_put_char
,
245 .throttle
= moxa_throttle
,
246 .unthrottle
= moxa_unthrottle
,
247 .set_termios
= moxa_set_termios
,
250 .hangup
= moxa_hangup
,
251 .tiocmget
= moxa_tiocmget
,
252 .tiocmset
= moxa_tiocmset
,
255 static struct tty_driver
*moxaDriver
;
256 static struct moxa_str moxaChannels
[MAX_PORTS
];
257 static DEFINE_TIMER(moxaTimer
, moxa_poll
, 0, 0);
258 static struct timer_list moxaEmptyTimer
[MAX_PORTS
];
259 static DEFINE_SPINLOCK(moxa_lock
);
262 static int moxa_get_PCI_conf(struct pci_dev
*p
, int board_type
,
263 struct moxa_board_conf
*board
)
265 board
->baseAddr
= pci_resource_start (p
, 2);
266 board
->boardType
= board_type
;
267 switch (board_type
) {
268 case MOXA_BOARD_C218_ISA
:
269 case MOXA_BOARD_C218_PCI
:
273 case MOXA_BOARD_CP204J
:
280 board
->busType
= MOXA_BUS_TYPE_PCI
;
281 /* don't lose the reference in the next pci_get_device iteration */
282 board
->pdev
= pci_dev_get(p
);
286 #endif /* CONFIG_PCI */
288 static int __init
moxa_init(void)
293 printk(KERN_INFO
"MOXA Intellio family driver version %s\n", MOXA_VERSION
);
294 moxaDriver
= alloc_tty_driver(MAX_PORTS
+ 1);
298 moxaDriver
->owner
= THIS_MODULE
;
299 moxaDriver
->name
= "ttyMX";
300 moxaDriver
->major
= ttymajor
;
301 moxaDriver
->minor_start
= 0;
302 moxaDriver
->type
= TTY_DRIVER_TYPE_SERIAL
;
303 moxaDriver
->subtype
= SERIAL_TYPE_NORMAL
;
304 moxaDriver
->init_termios
= tty_std_termios
;
305 moxaDriver
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| CLOCAL
| HUPCL
;
306 moxaDriver
->init_termios
.c_ispeed
= 9600;
307 moxaDriver
->init_termios
.c_ospeed
= 9600;
308 moxaDriver
->flags
= TTY_DRIVER_REAL_RAW
;
309 tty_set_operations(moxaDriver
, &moxa_ops
);
311 for (i
= 0, ch
= moxaChannels
; i
< MAX_PORTS
; i
++, ch
++) {
312 ch
->type
= PORT_16550A
;
314 ch
->close_delay
= 5 * HZ
/ 10;
315 ch
->closing_wait
= 30 * HZ
;
316 ch
->cflag
= B9600
| CS8
| CREAD
| CLOCAL
| HUPCL
;
317 init_waitqueue_head(&ch
->open_wait
);
318 init_waitqueue_head(&ch
->close_wait
);
321 printk("Tty devices major number = %d\n", ttymajor
);
323 if (tty_register_driver(moxaDriver
)) {
324 printk(KERN_ERR
"Couldn't install MOXA Smartio family driver !\n");
325 put_tty_driver(moxaDriver
);
328 for (i
= 0; i
< MAX_PORTS
; i
++)
329 setup_timer(&moxaEmptyTimer
[i
], check_xmit_empty
,
330 (unsigned long)&moxaChannels
[i
]);
332 mod_timer(&moxaTimer
, jiffies
+ HZ
/ 50);
334 /* Find the boards defined in source code */
336 for (i
= 0; i
< MAX_BOARDS
; i
++) {
337 if ((moxa_isa_boards
[i
].boardType
== MOXA_BOARD_C218_ISA
) ||
338 (moxa_isa_boards
[i
].boardType
== MOXA_BOARD_C320_ISA
)) {
339 moxa_boards
[numBoards
].boardType
= moxa_isa_boards
[i
].boardType
;
340 if (moxa_isa_boards
[i
].boardType
== MOXA_BOARD_C218_ISA
)
341 moxa_boards
[numBoards
].numPorts
= 8;
343 moxa_boards
[numBoards
].numPorts
= moxa_isa_boards
[i
].numPorts
;
344 moxa_boards
[numBoards
].busType
= MOXA_BUS_TYPE_ISA
;
345 moxa_boards
[numBoards
].baseAddr
= moxa_isa_boards
[i
].baseAddr
;
347 printk("Board %2d: %s board(baseAddr=%lx)\n",
349 moxa_brdname
[moxa_boards
[numBoards
].boardType
- 1],
350 moxa_boards
[numBoards
].baseAddr
);
354 /* Find the boards defined form module args. */
356 for (i
= 0; i
< MAX_BOARDS
; i
++) {
357 if ((type
[i
] == MOXA_BOARD_C218_ISA
) ||
358 (type
[i
] == MOXA_BOARD_C320_ISA
)) {
360 printk("Board %2d: %s board(baseAddr=%lx)\n",
362 moxa_brdname
[type
[i
] - 1],
363 (unsigned long) baseaddr
[i
]);
364 if (numBoards
>= MAX_BOARDS
) {
366 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS
);
369 moxa_boards
[numBoards
].boardType
= type
[i
];
370 if (moxa_isa_boards
[i
].boardType
== MOXA_BOARD_C218_ISA
)
371 moxa_boards
[numBoards
].numPorts
= 8;
373 moxa_boards
[numBoards
].numPorts
= numports
[i
];
374 moxa_boards
[numBoards
].busType
= MOXA_BUS_TYPE_ISA
;
375 moxa_boards
[numBoards
].baseAddr
= baseaddr
[i
];
380 /* Find PCI boards here */
383 struct pci_dev
*p
= NULL
;
384 int n
= ARRAY_SIZE(moxa_pcibrds
) - 1;
387 while ((p
= pci_get_device(moxa_pcibrds
[i
].vendor
, moxa_pcibrds
[i
].device
, p
))!=NULL
)
389 if (pci_enable_device(p
))
391 if (numBoards
>= MAX_BOARDS
) {
393 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS
);
395 moxa_get_PCI_conf(p
, moxa_pcibrds
[i
].driver_data
,
396 &moxa_boards
[numBoards
]);
404 for (i
= 0; i
< numBoards
; i
++) {
405 moxaBaseAddr
[i
] = ioremap((unsigned long) moxa_boards
[i
].baseAddr
, 0x4000);
411 static void __exit
moxa_exit(void)
416 printk("Unloading module moxa ...\n");
418 del_timer_sync(&moxaTimer
);
420 for (i
= 0; i
< MAX_PORTS
; i
++)
421 del_timer_sync(&moxaEmptyTimer
[i
]);
423 if (tty_unregister_driver(moxaDriver
))
424 printk("Couldn't unregister MOXA Intellio family serial driver\n");
425 put_tty_driver(moxaDriver
);
427 for (i
= 0; i
< MAX_BOARDS
; i
++) {
429 iounmap(moxaBaseAddr
[i
]);
430 if (moxa_boards
[i
].busType
== MOXA_BUS_TYPE_PCI
)
431 pci_dev_put(moxa_boards
[i
].pdev
);
438 module_init(moxa_init
);
439 module_exit(moxa_exit
);
441 static int moxa_open(struct tty_struct
*tty
, struct file
*filp
)
448 if (port
== MAX_PORTS
) {
451 if (!MoxaPortIsValid(port
)) {
452 tty
->driver_data
= NULL
;
456 ch
= &moxaChannels
[port
];
458 tty
->driver_data
= ch
;
460 if (!(ch
->asyncflags
& ASYNC_INITIALIZED
)) {
463 MoxaPortLineCtrl(ch
->port
, 1, 1);
464 MoxaPortEnable(ch
->port
);
465 ch
->asyncflags
|= ASYNC_INITIALIZED
;
467 retval
= block_till_ready(tty
, filp
, ch
);
469 moxa_unthrottle(tty
);
471 if (ch
->type
== PORT_16550A
) {
472 MoxaSetFifo(ch
->port
, 1);
474 MoxaSetFifo(ch
->port
, 0);
480 static void moxa_close(struct tty_struct
*tty
, struct file
*filp
)
486 if (port
== MAX_PORTS
) {
489 if (!MoxaPortIsValid(port
)) {
490 #ifdef SERIAL_DEBUG_CLOSE
491 printk("Invalid portno in moxa_close\n");
493 tty
->driver_data
= NULL
;
496 if (tty
->driver_data
== NULL
) {
499 if (tty_hung_up_p(filp
)) {
502 ch
= (struct moxa_str
*) tty
->driver_data
;
504 if ((tty
->count
== 1) && (ch
->count
!= 1)) {
505 printk("moxa_close: bad serial port count; tty->count is 1, "
506 "ch->count is %d\n", ch
->count
);
509 if (--ch
->count
< 0) {
510 printk("moxa_close: bad serial port count, device=%s\n",
517 ch
->asyncflags
|= ASYNC_CLOSING
;
519 ch
->cflag
= tty
->termios
->c_cflag
;
520 if (ch
->asyncflags
& ASYNC_INITIALIZED
) {
521 setup_empty_event(tty
);
522 tty_wait_until_sent(tty
, 30 * HZ
); /* 30 seconds timeout */
523 del_timer_sync(&moxaEmptyTimer
[ch
->port
]);
526 MoxaPortFlushData(port
, 2);
528 if (tty
->driver
->flush_buffer
)
529 tty
->driver
->flush_buffer(tty
);
530 tty_ldisc_flush(tty
);
535 if (ch
->blocked_open
) {
536 if (ch
->close_delay
) {
537 msleep_interruptible(jiffies_to_msecs(ch
->close_delay
));
539 wake_up_interruptible(&ch
->open_wait
);
541 ch
->asyncflags
&= ~(ASYNC_NORMAL_ACTIVE
| ASYNC_CLOSING
);
542 wake_up_interruptible(&ch
->close_wait
);
545 static int moxa_write(struct tty_struct
*tty
,
546 const unsigned char *buf
, int count
)
552 ch
= (struct moxa_str
*) tty
->driver_data
;
557 spin_lock_irqsave(&moxa_lock
, flags
);
558 len
= MoxaPortWriteData(port
, (unsigned char *) buf
, count
);
559 spin_unlock_irqrestore(&moxa_lock
, flags
);
561 /*********************************************
562 if ( !(ch->statusflags & LOWWAIT) &&
563 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
564 ************************************************/
565 ch
->statusflags
|= LOWWAIT
;
569 static int moxa_write_room(struct tty_struct
*tty
)
575 ch
= (struct moxa_str
*) tty
->driver_data
;
578 return (MoxaPortTxFree(ch
->port
));
581 static void moxa_flush_buffer(struct tty_struct
*tty
)
583 struct moxa_str
*ch
= (struct moxa_str
*) tty
->driver_data
;
587 MoxaPortFlushData(ch
->port
, 1);
591 static int moxa_chars_in_buffer(struct tty_struct
*tty
)
594 struct moxa_str
*ch
= (struct moxa_str
*) tty
->driver_data
;
597 * Sigh...I have to check if driver_data is NULL here, because
598 * if an open() fails, the TTY subsystem eventually calls
599 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
600 * routine. And since the open() failed, we return 0 here. TDJ
604 chars
= MoxaPortTxQueue(ch
->port
);
607 * Make it possible to wakeup anything waiting for output
608 * in tty_ioctl.c, etc.
610 if (!(ch
->statusflags
& EMPTYWAIT
))
611 setup_empty_event(tty
);
616 static void moxa_flush_chars(struct tty_struct
*tty
)
619 * Don't think I need this, because this is called to empty the TX
620 * buffer for the 16450, 16550, etc.
624 static void moxa_put_char(struct tty_struct
*tty
, unsigned char c
)
630 ch
= (struct moxa_str
*) tty
->driver_data
;
634 spin_lock_irqsave(&moxa_lock
, flags
);
635 MoxaPortWriteData(port
, &c
, 1);
636 spin_unlock_irqrestore(&moxa_lock
, flags
);
637 /************************************************
638 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
639 *************************************************/
640 ch
->statusflags
|= LOWWAIT
;
643 static int moxa_tiocmget(struct tty_struct
*tty
, struct file
*file
)
645 struct moxa_str
*ch
= (struct moxa_str
*) tty
->driver_data
;
647 int flag
= 0, dtr
, rts
;
650 if ((port
!= MAX_PORTS
) && (!ch
))
653 MoxaPortGetLineOut(ch
->port
, &dtr
, &rts
);
658 dtr
= MoxaPortLineStatus(ch
->port
);
668 static int moxa_tiocmset(struct tty_struct
*tty
, struct file
*file
,
669 unsigned int set
, unsigned int clear
)
671 struct moxa_str
*ch
= (struct moxa_str
*) tty
->driver_data
;
676 if ((port
!= MAX_PORTS
) && (!ch
))
679 MoxaPortGetLineOut(ch
->port
, &dtr
, &rts
);
684 if (clear
& TIOCM_RTS
)
686 if (clear
& TIOCM_DTR
)
688 MoxaPortLineCtrl(ch
->port
, dtr
, rts
);
692 static int moxa_ioctl(struct tty_struct
*tty
, struct file
*file
,
693 unsigned int cmd
, unsigned long arg
)
695 struct moxa_str
*ch
= (struct moxa_str
*) tty
->driver_data
;
697 void __user
*argp
= (void __user
*)arg
;
701 if ((port
!= MAX_PORTS
) && (!ch
))
705 case TCSBRK
: /* SVID version: non-zero arg --> no break */
706 retval
= tty_check_change(tty
);
709 setup_empty_event(tty
);
710 tty_wait_until_sent(tty
, 0);
712 MoxaPortSendBreak(ch
->port
, 0);
714 case TCSBRKP
: /* support for POSIX tcsendbreak() */
715 retval
= tty_check_change(tty
);
718 setup_empty_event(tty
);
719 tty_wait_until_sent(tty
, 0);
720 MoxaPortSendBreak(ch
->port
, arg
);
723 return put_user(C_CLOCAL(tty
) ? 1 : 0, (unsigned long __user
*) argp
);
725 if(get_user(retval
, (unsigned long __user
*) argp
))
728 tty
->termios
->c_cflag
= ((tty
->termios
->c_cflag
& ~CLOCAL
) |
731 ch
->asyncflags
&= ~ASYNC_CHECK_CD
;
733 ch
->asyncflags
|= ASYNC_CHECK_CD
;
736 return moxa_get_serial_info(ch
, argp
);
739 return moxa_set_serial_info(ch
, argp
);
741 retval
= MoxaDriverIoctl(cmd
, arg
, port
);
746 static void moxa_throttle(struct tty_struct
*tty
)
748 struct moxa_str
*ch
= (struct moxa_str
*) tty
->driver_data
;
750 ch
->statusflags
|= THROTTLE
;
753 static void moxa_unthrottle(struct tty_struct
*tty
)
755 struct moxa_str
*ch
= (struct moxa_str
*) tty
->driver_data
;
757 ch
->statusflags
&= ~THROTTLE
;
760 static void moxa_set_termios(struct tty_struct
*tty
,
761 struct ktermios
*old_termios
)
763 struct moxa_str
*ch
= (struct moxa_str
*) tty
->driver_data
;
768 if (!(old_termios
->c_cflag
& CLOCAL
) &&
769 (tty
->termios
->c_cflag
& CLOCAL
))
770 wake_up_interruptible(&ch
->open_wait
);
773 static void moxa_stop(struct tty_struct
*tty
)
775 struct moxa_str
*ch
= (struct moxa_str
*) tty
->driver_data
;
779 MoxaPortTxDisable(ch
->port
);
780 ch
->statusflags
|= TXSTOPPED
;
784 static void moxa_start(struct tty_struct
*tty
)
786 struct moxa_str
*ch
= (struct moxa_str
*) tty
->driver_data
;
791 if (!(ch
->statusflags
& TXSTOPPED
))
794 MoxaPortTxEnable(ch
->port
);
795 ch
->statusflags
&= ~TXSTOPPED
;
798 static void moxa_hangup(struct tty_struct
*tty
)
800 struct moxa_str
*ch
= (struct moxa_str
*) tty
->driver_data
;
802 moxa_flush_buffer(tty
);
806 ch
->asyncflags
&= ~ASYNC_NORMAL_ACTIVE
;
808 wake_up_interruptible(&ch
->open_wait
);
811 static void moxa_poll(unsigned long ignored
)
815 struct tty_struct
*tp
;
818 del_timer(&moxaTimer
);
820 if (MoxaDriverPoll() < 0) {
821 mod_timer(&moxaTimer
, jiffies
+ HZ
/ 50);
824 for (card
= 0; card
< MAX_BOARDS
; card
++) {
825 if ((ports
= MoxaPortsOfCard(card
)) <= 0)
827 ch
= &moxaChannels
[card
* MAX_PORTS_PER_BOARD
];
828 for (i
= 0; i
< ports
; i
++, ch
++) {
829 if ((ch
->asyncflags
& ASYNC_INITIALIZED
) == 0)
831 if (!(ch
->statusflags
& THROTTLE
) &&
832 (MoxaPortRxQueue(ch
->port
) > 0))
834 if ((tp
= ch
->tty
) == 0)
836 if (ch
->statusflags
& LOWWAIT
) {
837 if (MoxaPortTxQueue(ch
->port
) <= WAKEUP_CHARS
) {
839 ch
->statusflags
&= ~LOWWAIT
;
844 if (!I_IGNBRK(tp
) && (MoxaPortResetBrkCnt(ch
->port
) > 0)) {
845 tty_insert_flip_char(tp
, 0, TTY_BREAK
);
846 tty_schedule_flip(tp
);
848 if (MoxaPortDCDChange(ch
->port
)) {
849 if (ch
->asyncflags
& ASYNC_CHECK_CD
) {
850 if (MoxaPortDCDON(ch
->port
))
851 wake_up_interruptible(&ch
->open_wait
);
854 wake_up_interruptible(&ch
->open_wait
);
855 ch
->asyncflags
&= ~ASYNC_NORMAL_ACTIVE
;
862 mod_timer(&moxaTimer
, jiffies
+ HZ
/ 50);
865 /******************************************************************************/
867 static void set_tty_param(struct tty_struct
*tty
)
869 register struct ktermios
*ts
;
871 int rts
, cts
, txflow
, rxflow
, xany
;
873 ch
= (struct moxa_str
*) tty
->driver_data
;
875 if (ts
->c_cflag
& CLOCAL
)
876 ch
->asyncflags
&= ~ASYNC_CHECK_CD
;
878 ch
->asyncflags
|= ASYNC_CHECK_CD
;
879 rts
= cts
= txflow
= rxflow
= xany
= 0;
880 if (ts
->c_cflag
& CRTSCTS
)
882 if (ts
->c_iflag
& IXON
)
884 if (ts
->c_iflag
& IXOFF
)
886 if (ts
->c_iflag
& IXANY
)
888 MoxaPortFlowCtrl(ch
->port
, rts
, cts
, txflow
, rxflow
, xany
);
889 MoxaPortSetTermio(ch
->port
, ts
, tty_get_baud_rate(tty
));
892 static int block_till_ready(struct tty_struct
*tty
, struct file
*filp
,
895 DECLARE_WAITQUEUE(wait
,current
);
898 int do_clocal
= C_CLOCAL(tty
);
901 * If the device is in the middle of being closed, then block
902 * until it's done, and then try again.
904 if (tty_hung_up_p(filp
) || (ch
->asyncflags
& ASYNC_CLOSING
)) {
905 if (ch
->asyncflags
& ASYNC_CLOSING
)
906 interruptible_sleep_on(&ch
->close_wait
);
907 #ifdef SERIAL_DO_RESTART
908 if (ch
->asyncflags
& ASYNC_HUP_NOTIFY
)
911 return (-ERESTARTSYS
);
917 * If non-blocking mode is set, then make the check up front
920 if (filp
->f_flags
& O_NONBLOCK
) {
921 ch
->asyncflags
|= ASYNC_NORMAL_ACTIVE
;
925 * Block waiting for the carrier detect and the line to become free
928 add_wait_queue(&ch
->open_wait
, &wait
);
929 #ifdef SERIAL_DEBUG_OPEN
930 printk("block_til_ready before block: ttys%d, count = %d\n",
931 ch
->line
, ch
->count
);
933 spin_lock_irqsave(&moxa_lock
, flags
);
934 if (!tty_hung_up_p(filp
))
937 spin_unlock_irqrestore(&moxa_lock
, flags
);
940 set_current_state(TASK_INTERRUPTIBLE
);
941 if (tty_hung_up_p(filp
) ||
942 !(ch
->asyncflags
& ASYNC_INITIALIZED
)) {
943 #ifdef SERIAL_DO_RESTART
944 if (ch
->asyncflags
& ASYNC_HUP_NOTIFY
)
947 retval
= -ERESTARTSYS
;
953 if (!(ch
->asyncflags
& ASYNC_CLOSING
) && (do_clocal
||
954 MoxaPortDCDON(ch
->port
)))
957 if (signal_pending(current
)) {
958 retval
= -ERESTARTSYS
;
963 set_current_state(TASK_RUNNING
);
964 remove_wait_queue(&ch
->open_wait
, &wait
);
966 spin_lock_irqsave(&moxa_lock
, flags
);
967 if (!tty_hung_up_p(filp
))
970 spin_unlock_irqrestore(&moxa_lock
, flags
);
971 #ifdef SERIAL_DEBUG_OPEN
972 printk("block_til_ready after blocking: ttys%d, count = %d\n",
973 ch
->line
, ch
->count
);
977 /* FIXME: review to see if we need to use set_bit on these */
978 ch
->asyncflags
|= ASYNC_NORMAL_ACTIVE
;
982 static void setup_empty_event(struct tty_struct
*tty
)
984 struct moxa_str
*ch
= tty
->driver_data
;
987 spin_lock_irqsave(&moxa_lock
, flags
);
988 ch
->statusflags
|= EMPTYWAIT
;
989 mod_timer(&moxaEmptyTimer
[ch
->port
], jiffies
+ HZ
);
990 spin_unlock_irqrestore(&moxa_lock
, flags
);
993 static void check_xmit_empty(unsigned long data
)
997 ch
= (struct moxa_str
*) data
;
998 del_timer_sync(&moxaEmptyTimer
[ch
->port
]);
999 if (ch
->tty
&& (ch
->statusflags
& EMPTYWAIT
)) {
1000 if (MoxaPortTxQueue(ch
->port
) == 0) {
1001 ch
->statusflags
&= ~EMPTYWAIT
;
1002 tty_wakeup(ch
->tty
);
1005 mod_timer(&moxaEmptyTimer
[ch
->port
], jiffies
+ HZ
);
1007 ch
->statusflags
&= ~EMPTYWAIT
;
1010 static void shut_down(struct moxa_str
*ch
)
1012 struct tty_struct
*tp
;
1014 if (!(ch
->asyncflags
& ASYNC_INITIALIZED
))
1019 MoxaPortDisable(ch
->port
);
1022 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1024 if (tp
->termios
->c_cflag
& HUPCL
)
1025 MoxaPortLineCtrl(ch
->port
, 0, 0);
1027 ch
->asyncflags
&= ~ASYNC_INITIALIZED
;
1030 static void receive_data(struct moxa_str
*ch
)
1032 struct tty_struct
*tp
;
1033 struct ktermios
*ts
;
1034 unsigned long flags
;
1040 /**************************************************
1041 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1042 *****************************************************/
1044 MoxaPortFlushData(ch
->port
, 0);
1047 spin_lock_irqsave(&moxa_lock
, flags
);
1048 MoxaPortReadData(ch
->port
, tp
);
1049 spin_unlock_irqrestore(&moxa_lock
, flags
);
1050 tty_schedule_flip(tp
);
1053 #define Magic_code 0x404
1056 * System Configuration
1059 * for C218 BIOS initialization
1061 #define C218_ConfBase 0x800
1062 #define C218_status (C218_ConfBase + 0) /* BIOS running status */
1063 #define C218_diag (C218_ConfBase + 2) /* diagnostic status */
1064 #define C218_key (C218_ConfBase + 4) /* WORD (0x218 for C218) */
1065 #define C218DLoad_len (C218_ConfBase + 6) /* WORD */
1066 #define C218check_sum (C218_ConfBase + 8) /* BYTE */
1067 #define C218chksum_ok (C218_ConfBase + 0x0a) /* BYTE (1:ok) */
1068 #define C218_TestRx (C218_ConfBase + 0x10) /* 8 bytes for 8 ports */
1069 #define C218_TestTx (C218_ConfBase + 0x18) /* 8 bytes for 8 ports */
1070 #define C218_RXerr (C218_ConfBase + 0x20) /* 8 bytes for 8 ports */
1071 #define C218_ErrFlag (C218_ConfBase + 0x28) /* 8 bytes for 8 ports */
1073 #define C218_LoadBuf 0x0F00
1074 #define C218_KeyCode 0x218
1075 #define CP204J_KeyCode 0x204
1078 * for C320 BIOS initialization
1080 #define C320_ConfBase 0x800
1081 #define C320_LoadBuf 0x0f00
1082 #define STS_init 0x05 /* for C320_status */
1084 #define C320_status C320_ConfBase + 0 /* BIOS running status */
1085 #define C320_diag C320_ConfBase + 2 /* diagnostic status */
1086 #define C320_key C320_ConfBase + 4 /* WORD (0320H for C320) */
1087 #define C320DLoad_len C320_ConfBase + 6 /* WORD */
1088 #define C320check_sum C320_ConfBase + 8 /* WORD */
1089 #define C320chksum_ok C320_ConfBase + 0x0a /* WORD (1:ok) */
1090 #define C320bapi_len C320_ConfBase + 0x0c /* WORD */
1091 #define C320UART_no C320_ConfBase + 0x0e /* WORD */
1093 #define C320_KeyCode 0x320
1095 #define FixPage_addr 0x0000 /* starting addr of static page */
1096 #define DynPage_addr 0x2000 /* starting addr of dynamic page */
1097 #define C218_start 0x3000 /* starting addr of C218 BIOS prg */
1098 #define Control_reg 0x1ff0 /* select page and reset control */
1099 #define HW_reset 0x80
1104 #define FC_CardReset 0x80
1105 #define FC_ChannelReset 1 /* C320 firmware not supported */
1106 #define FC_EnableCH 2
1107 #define FC_DisableCH 3
1108 #define FC_SetParam 4
1109 #define FC_SetMode 5
1110 #define FC_SetRate 6
1111 #define FC_LineControl 7
1112 #define FC_LineStatus 8
1113 #define FC_XmitControl 9
1114 #define FC_FlushQueue 10
1115 #define FC_SendBreak 11
1116 #define FC_StopBreak 12
1117 #define FC_LoopbackON 13
1118 #define FC_LoopbackOFF 14
1119 #define FC_ClrIrqTable 15
1120 #define FC_SendXon 16
1121 #define FC_SetTermIrq 17 /* C320 firmware not supported */
1122 #define FC_SetCntIrq 18 /* C320 firmware not supported */
1123 #define FC_SetBreakIrq 19
1124 #define FC_SetLineIrq 20
1125 #define FC_SetFlowCtl 21
1126 #define FC_GenIrq 22
1127 #define FC_InCD180 23
1128 #define FC_OutCD180 24
1129 #define FC_InUARTreg 23
1130 #define FC_OutUARTreg 24
1131 #define FC_SetXonXoff 25
1132 #define FC_OutCD180CCR 26
1133 #define FC_ExtIQueue 27
1134 #define FC_ExtOQueue 28
1135 #define FC_ClrLineIrq 29
1136 #define FC_HWFlowCtl 30
1137 #define FC_GetClockRate 35
1138 #define FC_SetBaud 36
1139 #define FC_SetDataMode 41
1140 #define FC_GetCCSR 43
1141 #define FC_GetDataError 45
1142 #define FC_RxControl 50
1143 #define FC_ImmSend 51
1144 #define FC_SetXonState 52
1145 #define FC_SetXoffState 53
1146 #define FC_SetRxFIFOTrig 54
1147 #define FC_SetTxFIFOCnt 55
1148 #define FC_UnixRate 56
1149 #define FC_UnixResetTimer 57
1151 #define RxFIFOTrig1 0
1152 #define RxFIFOTrig4 1
1153 #define RxFIFOTrig8 2
1154 #define RxFIFOTrig14 3
1159 #define DRAM_global 0
1160 #define INT_data (DRAM_global + 0)
1161 #define Config_base (DRAM_global + 0x108)
1163 #define IRQindex (INT_data + 0)
1164 #define IRQpending (INT_data + 4)
1165 #define IRQtable (INT_data + 8)
1170 #define IntrRx 0x01 /* receiver data O.K. */
1171 #define IntrTx 0x02 /* transmit buffer empty */
1172 #define IntrFunc 0x04 /* function complete */
1173 #define IntrBreak 0x08 /* received break */
1174 #define IntrLine 0x10 /* line status change
1176 #define IntrIntr 0x20 /* received INTR code */
1177 #define IntrQuit 0x40 /* received QUIT code */
1178 #define IntrEOF 0x80 /* received EOF code */
1180 #define IntrRxTrigger 0x100 /* rx data count reach tigger value */
1181 #define IntrTxTrigger 0x200 /* tx data count below trigger value */
1183 #define Magic_no (Config_base + 0)
1184 #define Card_model_no (Config_base + 2)
1185 #define Total_ports (Config_base + 4)
1186 #define Module_cnt (Config_base + 8)
1187 #define Module_no (Config_base + 10)
1188 #define Timer_10ms (Config_base + 14)
1189 #define Disable_IRQ (Config_base + 20)
1190 #define TMS320_PORT1 (Config_base + 22)
1191 #define TMS320_PORT2 (Config_base + 24)
1192 #define TMS320_CLOCK (Config_base + 26)
1195 * DATA BUFFER in DRAM
1197 #define Extern_table 0x400 /* Base address of the external table
1198 (24 words * 64) total 3K bytes
1199 (24 words * 128) total 6K bytes */
1200 #define Extern_size 0x60 /* 96 bytes */
1201 #define RXrptr 0x00 /* read pointer for RX buffer */
1202 #define RXwptr 0x02 /* write pointer for RX buffer */
1203 #define TXrptr 0x04 /* read pointer for TX buffer */
1204 #define TXwptr 0x06 /* write pointer for TX buffer */
1205 #define HostStat 0x08 /* IRQ flag and general flag */
1206 #define FlagStat 0x0A
1207 #define FlowControl 0x0C /* B7 B6 B5 B4 B3 B2 B1 B0 */
1208 /* x x x x | | | | */
1209 /* | | | + CTS flow */
1210 /* | | +--- RTS flow */
1211 /* | +------ TX Xon/Xoff */
1212 /* +--------- RX Xon/Xoff */
1213 #define Break_cnt 0x0E /* received break count */
1214 #define CD180TXirq 0x10 /* if non-0: enable TX irq */
1215 #define RX_mask 0x12
1216 #define TX_mask 0x14
1217 #define Ofs_rxb 0x16
1218 #define Ofs_txb 0x18
1219 #define Page_rxb 0x1A
1220 #define Page_txb 0x1C
1221 #define EndPage_rxb 0x1E
1222 #define EndPage_txb 0x20
1223 #define Data_error 0x22
1224 #define RxTrigger 0x28
1225 #define TxTrigger 0x2a
1227 #define rRXwptr 0x34
1228 #define Low_water 0x36
1230 #define FuncCode 0x40
1231 #define FuncArg 0x42
1232 #define FuncArg1 0x44
1234 #define C218rx_size 0x2000 /* 8K bytes */
1235 #define C218tx_size 0x8000 /* 32K bytes */
1237 #define C218rx_mask (C218rx_size - 1)
1238 #define C218tx_mask (C218tx_size - 1)
1240 #define C320p8rx_size 0x2000
1241 #define C320p8tx_size 0x8000
1242 #define C320p8rx_mask (C320p8rx_size - 1)
1243 #define C320p8tx_mask (C320p8tx_size - 1)
1245 #define C320p16rx_size 0x2000
1246 #define C320p16tx_size 0x4000
1247 #define C320p16rx_mask (C320p16rx_size - 1)
1248 #define C320p16tx_mask (C320p16tx_size - 1)
1250 #define C320p24rx_size 0x2000
1251 #define C320p24tx_size 0x2000
1252 #define C320p24rx_mask (C320p24rx_size - 1)
1253 #define C320p24tx_mask (C320p24tx_size - 1)
1255 #define C320p32rx_size 0x1000
1256 #define C320p32tx_size 0x1000
1257 #define C320p32rx_mask (C320p32rx_size - 1)
1258 #define C320p32tx_mask (C320p32tx_size - 1)
1260 #define Page_size 0x2000
1261 #define Page_mask (Page_size - 1)
1262 #define C218rx_spage 3
1263 #define C218tx_spage 4
1264 #define C218rx_pageno 1
1265 #define C218tx_pageno 4
1266 #define C218buf_pageno 5
1268 #define C320p8rx_spage 3
1269 #define C320p8tx_spage 4
1270 #define C320p8rx_pgno 1
1271 #define C320p8tx_pgno 4
1272 #define C320p8buf_pgno 5
1274 #define C320p16rx_spage 3
1275 #define C320p16tx_spage 4
1276 #define C320p16rx_pgno 1
1277 #define C320p16tx_pgno 2
1278 #define C320p16buf_pgno 3
1280 #define C320p24rx_spage 3
1281 #define C320p24tx_spage 4
1282 #define C320p24rx_pgno 1
1283 #define C320p24tx_pgno 1
1284 #define C320p24buf_pgno 2
1286 #define C320p32rx_spage 3
1287 #define C320p32tx_ofs C320p32rx_size
1288 #define C320p32tx_spage 3
1289 #define C320p32buf_pgno 1
1294 #define WakeupRx 0x01
1295 #define WakeupTx 0x02
1296 #define WakeupBreak 0x08
1297 #define WakeupLine 0x10
1298 #define WakeupIntr 0x20
1299 #define WakeupQuit 0x40
1300 #define WakeupEOF 0x80 /* used in VTIME control */
1301 #define WakeupRxTrigger 0x100
1302 #define WakeupTxTrigger 0x200
1306 #define Rx_over 0x01
1307 #define Xoff_state 0x02
1308 #define Tx_flowOff 0x04
1309 #define Tx_enable 0x08
1310 #define CTS_state 0x10
1311 #define DSR_state 0x20
1312 #define DCD_state 0x80
1316 #define CTS_FlowCtl 1
1317 #define RTS_FlowCtl 2
1318 #define Tx_FlowCtl 4
1319 #define Rx_FlowCtl 8
1320 #define IXM_IXANY 0x10
1322 #define LowWater 128
1330 /* mode definition */
1336 #define MX_STOP1 0x00
1337 #define MX_STOP15 0x04
1338 #define MX_STOP2 0x08
1340 #define MX_PARNONE 0x00
1341 #define MX_PAREVEN 0x40
1342 #define MX_PARODD 0xC0
1350 int rxcnt
[MAX_PORTS
];
1351 int txcnt
[MAX_PORTS
];
1354 #define DCD_changed 0x01
1355 #define DCD_oldstate 0x80
1357 static unsigned char moxaBuff
[10240];
1358 static void __iomem
*moxaIntNdx
[MAX_BOARDS
];
1359 static void __iomem
*moxaIntPend
[MAX_BOARDS
];
1360 static void __iomem
*moxaIntTable
[MAX_BOARDS
];
1361 static char moxaChkPort
[MAX_PORTS
];
1362 static char moxaLineCtrl
[MAX_PORTS
];
1363 static void __iomem
*moxaTableAddr
[MAX_PORTS
];
1364 static long moxaCurBaud
[MAX_PORTS
];
1365 static char moxaDCDState
[MAX_PORTS
];
1366 static char moxaLowChkFlag
[MAX_PORTS
];
1367 static int moxaLowWaterChk
;
1368 static int moxaCard
;
1369 static struct mon_str moxaLog
;
1370 static int moxaFuncTout
= HZ
/ 2;
1371 static ushort moxaBreakCnt
[MAX_PORTS
];
1373 static void moxadelay(int);
1374 static void moxafunc(void __iomem
*, int, ushort
);
1375 static void wait_finish(void __iomem
*);
1376 static void low_water_check(void __iomem
*);
1377 static int moxaloadbios(int, unsigned char __user
*, int);
1378 static int moxafindcard(int);
1379 static int moxaload320b(int, unsigned char __user
*, int);
1380 static int moxaloadcode(int, unsigned char __user
*, int);
1381 static int moxaloadc218(int, void __iomem
*, int);
1382 static int moxaloadc320(int, void __iomem
*, int, int *);
1384 /*****************************************************************************
1385 * Driver level functions: *
1386 * 1. MoxaDriverInit(void); *
1387 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1388 * 3. MoxaDriverPoll(void); *
1389 *****************************************************************************/
1390 void MoxaDriverInit(void)
1394 moxaFuncTout
= HZ
/ 2; /* 500 mini-seconds */
1397 moxaLowWaterChk
= 0;
1398 for (i
= 0; i
< MAX_PORTS
; i
++) {
1400 moxaLowChkFlag
[i
] = 0;
1401 moxaLineCtrl
[i
] = 0;
1402 moxaLog
.rxcnt
[i
] = 0;
1403 moxaLog
.txcnt
[i
] = 0;
1408 #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1409 #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
1410 #define MOXA_INIT_DRIVER (MOXA + 6) /* moxaCard=0 */
1411 #define MOXA_LOAD_BIOS (MOXA + 9) /* download BIOS */
1412 #define MOXA_FIND_BOARD (MOXA + 10) /* Check if MOXA card exist? */
1413 #define MOXA_LOAD_C320B (MOXA + 11) /* download 320B firmware */
1414 #define MOXA_LOAD_CODE (MOXA + 12) /* download firmware */
1415 #define MOXA_GETDATACOUNT (MOXA + 23)
1416 #define MOXA_GET_IOQUEUE (MOXA + 27)
1417 #define MOXA_FLUSH_QUEUE (MOXA + 28)
1418 #define MOXA_GET_CONF (MOXA + 35) /* configuration */
1419 #define MOXA_GET_MAJOR (MOXA + 63)
1420 #define MOXA_GET_CUMAJOR (MOXA + 64)
1421 #define MOXA_GETMSTATUS (MOXA + 65)
1435 static struct moxaq_str temp_queue
[MAX_PORTS
];
1436 static struct dl_str dltmp
;
1438 void MoxaPortFlushData(int port
, int mode
)
1440 void __iomem
*ofsAddr
;
1441 if ((mode
< 0) || (mode
> 2))
1443 ofsAddr
= moxaTableAddr
[port
];
1444 moxafunc(ofsAddr
, FC_FlushQueue
, mode
);
1446 moxaLowChkFlag
[port
] = 0;
1447 low_water_check(ofsAddr
);
1451 int MoxaDriverIoctl(unsigned int cmd
, unsigned long arg
, int port
)
1455 int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
1456 void __user
*argp
= (void __user
*)arg
;
1458 if (port
== MAX_PORTS
) {
1459 if ((cmd
!= MOXA_GET_CONF
) && (cmd
!= MOXA_INIT_DRIVER
) &&
1460 (cmd
!= MOXA_LOAD_BIOS
) && (cmd
!= MOXA_FIND_BOARD
) && (cmd
!= MOXA_LOAD_C320B
) &&
1461 (cmd
!= MOXA_LOAD_CODE
) && (cmd
!= MOXA_GETDATACOUNT
) &&
1462 (cmd
!= MOXA_GET_IOQUEUE
) && (cmd
!= MOXA_GET_MAJOR
) &&
1463 (cmd
!= MOXA_GET_CUMAJOR
) && (cmd
!= MOXA_GETMSTATUS
))
1468 if(copy_to_user(argp
, &moxa_boards
, MAX_BOARDS
*
1469 sizeof(struct moxa_board_conf
)))
1472 case MOXA_INIT_DRIVER
:
1473 if ((int) arg
== 0x404)
1476 case MOXA_GETDATACOUNT
:
1477 moxaLog
.tick
= jiffies
;
1478 if(copy_to_user(argp
, &moxaLog
, sizeof(struct mon_str
)))
1481 case MOXA_FLUSH_QUEUE
:
1482 MoxaPortFlushData(port
, arg
);
1484 case MOXA_GET_IOQUEUE
:
1485 for (i
= 0; i
< MAX_PORTS
; i
++) {
1486 if (moxaChkPort
[i
]) {
1487 temp_queue
[i
].inq
= MoxaPortRxQueue(i
);
1488 temp_queue
[i
].outq
= MoxaPortTxQueue(i
);
1491 if(copy_to_user(argp
, temp_queue
, sizeof(struct moxaq_str
) * MAX_PORTS
))
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 for (i
= 0; i
< MAX_PORTS
; i
++) {
1512 GMStatus
[i
].dcd
= 0;
1513 GMStatus
[i
].dsr
= 0;
1514 GMStatus
[i
].cts
= 0;
1515 if (!moxaChkPort
[i
]) {
1518 status
= MoxaPortLineStatus(moxaChannels
[i
].port
);
1520 GMStatus
[i
].cts
= 1;
1522 GMStatus
[i
].dsr
= 1;
1524 GMStatus
[i
].dcd
= 1;
1527 if (!moxaChannels
[i
].tty
|| !moxaChannels
[i
].tty
->termios
)
1528 GMStatus
[i
].cflag
= moxaChannels
[i
].cflag
;
1530 GMStatus
[i
].cflag
= moxaChannels
[i
].tty
->termios
->c_cflag
;
1532 if(copy_to_user(argp
, GMStatus
, sizeof(struct mxser_mstatus
) * MAX_PORTS
))
1536 return (-ENOIOCTLCMD
);
1537 case MOXA_LOAD_BIOS
:
1538 case MOXA_FIND_BOARD
:
1539 case MOXA_LOAD_C320B
:
1540 case MOXA_LOAD_CODE
:
1541 if (!capable(CAP_SYS_RAWIO
))
1546 if(copy_from_user(&dltmp
, argp
, sizeof(struct dl_str
)))
1548 if(dltmp
.cardno
< 0 || dltmp
.cardno
>= MAX_BOARDS
)
1553 case MOXA_LOAD_BIOS
:
1554 i
= moxaloadbios(dltmp
.cardno
, dltmp
.buf
, dltmp
.len
);
1556 case MOXA_FIND_BOARD
:
1557 return moxafindcard(dltmp
.cardno
);
1558 case MOXA_LOAD_C320B
:
1559 moxaload320b(dltmp
.cardno
, dltmp
.buf
, dltmp
.len
);
1560 default: /* to keep gcc happy */
1562 case MOXA_LOAD_CODE
:
1563 i
= moxaloadcode(dltmp
.cardno
, dltmp
.buf
, dltmp
.len
);
1571 int MoxaDriverPoll(void)
1573 register ushort temp
;
1575 void __iomem
*ofsAddr
;
1581 for (card
= 0; card
< MAX_BOARDS
; card
++) {
1582 if (loadstat
[card
] == 0)
1584 if ((ports
= moxa_boards
[card
].numPorts
) == 0)
1586 if (readb(moxaIntPend
[card
]) == 0xff) {
1587 ip
= moxaIntTable
[card
] + readb(moxaIntNdx
[card
]);
1588 p
= card
* MAX_PORTS_PER_BOARD
;
1590 for (port
= 0; port
< ports
; port
+= 2, p
++) {
1591 if ((temp
= readw(ip
+ port
)) != 0) {
1592 writew(0, ip
+ port
);
1593 ofsAddr
= moxaTableAddr
[p
];
1595 writew(readw(ofsAddr
+ HostStat
) & ~WakeupTx
, ofsAddr
+ HostStat
);
1596 if (temp
& IntrBreak
) {
1599 if (temp
& IntrLine
) {
1600 if (readb(ofsAddr
+ FlagStat
) & DCD_state
) {
1601 if ((moxaDCDState
[p
] & DCD_oldstate
) == 0)
1602 moxaDCDState
[p
] = (DCD_oldstate
|
1605 if (moxaDCDState
[p
] & DCD_oldstate
)
1606 moxaDCDState
[p
] = DCD_changed
;
1611 writeb(0, moxaIntPend
[card
]);
1613 if (moxaLowWaterChk
) {
1614 p
= card
* MAX_PORTS_PER_BOARD
;
1615 for (port
= 0; port
< ports
; port
++, p
++) {
1616 if (moxaLowChkFlag
[p
]) {
1617 moxaLowChkFlag
[p
] = 0;
1618 ofsAddr
= moxaTableAddr
[p
];
1619 low_water_check(ofsAddr
);
1624 moxaLowWaterChk
= 0;
1628 /*****************************************************************************
1629 * Card level function: *
1630 * 1. MoxaPortsOfCard(int cardno); *
1631 *****************************************************************************/
1632 int MoxaPortsOfCard(int cardno
)
1635 if (moxa_boards
[cardno
].boardType
== 0)
1637 return (moxa_boards
[cardno
].numPorts
);
1640 /*****************************************************************************
1641 * Port level functions: *
1642 * 1. MoxaPortIsValid(int port); *
1643 * 2. MoxaPortEnable(int port); *
1644 * 3. MoxaPortDisable(int port); *
1645 * 4. MoxaPortGetMaxBaud(int port); *
1646 * 6. MoxaPortSetBaud(int port, long baud); *
1647 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1648 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1649 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1650 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1651 * 12. MoxaPortLineStatus(int port); *
1652 * 13. MoxaPortDCDChange(int port); *
1653 * 14. MoxaPortDCDON(int port); *
1654 * 15. MoxaPortFlushData(int port, int mode); *
1655 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1656 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1657 * 20. MoxaPortTxQueue(int port); *
1658 * 21. MoxaPortTxFree(int port); *
1659 * 22. MoxaPortRxQueue(int port); *
1660 * 24. MoxaPortTxDisable(int port); *
1661 * 25. MoxaPortTxEnable(int port); *
1662 * 27. MoxaPortResetBrkCnt(int port); *
1663 * 30. MoxaPortSendBreak(int port, int ticks); *
1664 *****************************************************************************/
1666 * Moxa Port Number Description:
1668 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1669 * the port number using in MOXA driver functions will be 0 to 31 for
1670 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1671 * to 127 for fourth. For example, if you setup three MOXA boards,
1672 * first board is C218, second board is C320-16 and third board is
1673 * C320-32. The port number of first board (C218 - 8 ports) is from
1674 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1675 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1676 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1677 * 127 will be invalid.
1680 * Moxa Functions Description:
1682 * Function 1: Driver initialization routine, this routine must be
1683 * called when initialized driver.
1685 * void MoxaDriverInit();
1688 * Function 2: Moxa driver private IOCTL command processing.
1690 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1692 * unsigned int cmd : IOCTL command
1693 * unsigned long arg : IOCTL argument
1694 * int port : port number (0 - 127)
1701 * Function 3: Moxa driver polling process routine.
1703 * int MoxaDriverPoll(void);
1705 * return: 0 ; polling O.K.
1706 * -1 : no any Moxa card.
1709 * Function 4: Get the ports of this card.
1711 * int MoxaPortsOfCard(int cardno);
1713 * int cardno : card number (0 - 3)
1715 * return: 0 : this card is invalid
1719 * Function 5: Check this port is valid or invalid
1721 * int MoxaPortIsValid(int port);
1722 * int port : port number (0 - 127, ref port description)
1724 * return: 0 : this port is invalid
1725 * 1 : this port is valid
1728 * Function 6: Enable this port to start Tx/Rx data.
1730 * void MoxaPortEnable(int port);
1731 * int port : port number (0 - 127)
1734 * Function 7: Disable this port
1736 * void MoxaPortDisable(int port);
1737 * int port : port number (0 - 127)
1740 * Function 8: Get the maximun available baud rate of this port.
1742 * long MoxaPortGetMaxBaud(int port);
1743 * int port : port number (0 - 127)
1745 * return: 0 : this port is invalid
1746 * 38400/57600/115200 bps
1749 * Function 10: Setting baud rate of this port.
1751 * long MoxaPortSetBaud(int port, long baud);
1752 * int port : port number (0 - 127)
1753 * long baud : baud rate (50 - 115200)
1755 * return: 0 : this port is invalid or baud < 50
1756 * 50 - 115200 : the real baud rate set to the port, if
1757 * the argument baud is large than maximun
1758 * available baud rate, the real setting
1759 * baud rate will be the maximun baud rate.
1762 * Function 12: Configure the port.
1764 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1765 * int port : port number (0 - 127)
1766 * struct ktermios * termio : termio structure pointer
1767 * speed_t baud : baud rate
1769 * return: -1 : this port is invalid or termio == NULL
1773 * Function 13: Get the DTR/RTS state of this port.
1775 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1776 * int port : port number (0 - 127)
1777 * int * dtrState : pointer to INT to receive the current DTR
1778 * state. (if NULL, this function will not
1779 * write to this address)
1780 * int * rtsState : pointer to INT to receive the current RTS
1781 * state. (if NULL, this function will not
1782 * write to this address)
1784 * return: -1 : this port is invalid
1788 * Function 14: Setting the DTR/RTS output state of this port.
1790 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1791 * int port : port number (0 - 127)
1792 * int dtrState : DTR output state (0: off, 1: on)
1793 * int rtsState : RTS output state (0: off, 1: on)
1796 * Function 15: Setting the flow control of this port.
1798 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1799 * int txFlow,int xany);
1800 * int port : port number (0 - 127)
1801 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1802 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1803 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1804 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1805 * int xany : S/W XANY flow control (0: no, 1: yes)
1808 * Function 16: Get ths line status of this port
1810 * int MoxaPortLineStatus(int port);
1811 * int port : port number (0 - 127)
1813 * return: Bit 0 - CTS state (0: off, 1: on)
1814 * Bit 1 - DSR state (0: off, 1: on)
1815 * Bit 2 - DCD state (0: off, 1: on)
1818 * Function 17: Check the DCD state has changed since the last read
1821 * int MoxaPortDCDChange(int port);
1822 * int port : port number (0 - 127)
1824 * return: 0 : no changed
1825 * 1 : DCD has changed
1828 * Function 18: Check ths current DCD state is ON or not.
1830 * int MoxaPortDCDON(int port);
1831 * int port : port number (0 - 127)
1833 * return: 0 : DCD off
1837 * Function 19: Flush the Rx/Tx buffer data of this port.
1839 * void MoxaPortFlushData(int port, int mode);
1840 * int port : port number (0 - 127)
1842 * 0 : flush the Rx buffer
1843 * 1 : flush the Tx buffer
1844 * 2 : flush the Rx and Tx buffer
1847 * Function 20: Write data.
1849 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1850 * int port : port number (0 - 127)
1851 * unsigned char * buffer : pointer to write data buffer.
1852 * int length : write data length
1854 * return: 0 - length : real write data length
1857 * Function 21: Read data.
1859 * int MoxaPortReadData(int port, struct tty_struct *tty);
1860 * int port : port number (0 - 127)
1861 * struct tty_struct *tty : tty for data
1863 * return: 0 - length : real read data length
1866 * Function 24: Get the Tx buffer current queued data bytes
1868 * int MoxaPortTxQueue(int port);
1869 * int port : port number (0 - 127)
1871 * return: .. : Tx buffer current queued data bytes
1874 * Function 25: Get the Tx buffer current free space
1876 * int MoxaPortTxFree(int port);
1877 * int port : port number (0 - 127)
1879 * return: .. : Tx buffer current free space
1882 * Function 26: Get the Rx buffer current queued data bytes
1884 * int MoxaPortRxQueue(int port);
1885 * int port : port number (0 - 127)
1887 * return: .. : Rx buffer current queued data bytes
1890 * Function 28: Disable port data transmission.
1892 * void MoxaPortTxDisable(int port);
1893 * int port : port number (0 - 127)
1896 * Function 29: Enable port data transmission.
1898 * void MoxaPortTxEnable(int port);
1899 * int port : port number (0 - 127)
1902 * Function 31: Get the received BREAK signal count and reset it.
1904 * int MoxaPortResetBrkCnt(int port);
1905 * int port : port number (0 - 127)
1907 * return: 0 - .. : BREAK signal count
1910 * Function 34: Send out a BREAK signal.
1912 * void MoxaPortSendBreak(int port, int ms100);
1913 * int port : port number (0 - 127)
1914 * int ms100 : break signal time interval.
1915 * unit: 100 mini-second. if ms100 == 0, it will
1916 * send out a about 250 ms BREAK signal.
1919 int MoxaPortIsValid(int port
)
1924 if (moxaChkPort
[port
] == 0)
1929 void MoxaPortEnable(int port
)
1931 void __iomem
*ofsAddr
;
1932 int MoxaPortLineStatus(int);
1933 short lowwater
= 512;
1935 ofsAddr
= moxaTableAddr
[port
];
1936 writew(lowwater
, ofsAddr
+ Low_water
);
1937 moxaBreakCnt
[port
] = 0;
1938 if ((moxa_boards
[port
/ MAX_PORTS_PER_BOARD
].boardType
== MOXA_BOARD_C320_ISA
) ||
1939 (moxa_boards
[port
/ MAX_PORTS_PER_BOARD
].boardType
== MOXA_BOARD_C320_PCI
)) {
1940 moxafunc(ofsAddr
, FC_SetBreakIrq
, 0);
1942 writew(readw(ofsAddr
+ HostStat
) | WakeupBreak
, ofsAddr
+ HostStat
);
1945 moxafunc(ofsAddr
, FC_SetLineIrq
, Magic_code
);
1946 moxafunc(ofsAddr
, FC_FlushQueue
, 2);
1948 moxafunc(ofsAddr
, FC_EnableCH
, Magic_code
);
1949 MoxaPortLineStatus(port
);
1952 void MoxaPortDisable(int port
)
1954 void __iomem
*ofsAddr
= moxaTableAddr
[port
];
1956 moxafunc(ofsAddr
, FC_SetFlowCtl
, 0); /* disable flow control */
1957 moxafunc(ofsAddr
, FC_ClrLineIrq
, Magic_code
);
1958 writew(0, ofsAddr
+ HostStat
);
1959 moxafunc(ofsAddr
, FC_DisableCH
, Magic_code
);
1962 long MoxaPortGetMaxBaud(int port
)
1964 if ((moxa_boards
[port
/ MAX_PORTS_PER_BOARD
].boardType
== MOXA_BOARD_C320_ISA
) ||
1965 (moxa_boards
[port
/ MAX_PORTS_PER_BOARD
].boardType
== MOXA_BOARD_C320_PCI
))
1972 long MoxaPortSetBaud(int port
, long baud
)
1974 void __iomem
*ofsAddr
;
1978 if ((baud
< 50L) || ((max
= MoxaPortGetMaxBaud(port
)) == 0))
1980 ofsAddr
= moxaTableAddr
[port
];
1984 clock
= 614400L; /* for 9.8304 Mhz : max. 38400 bps */
1985 else if (max
== 57600L)
1986 clock
= 691200L; /* for 11.0592 Mhz : max. 57600 bps */
1988 clock
= 921600L; /* for 14.7456 Mhz : max. 115200 bps */
1990 moxafunc(ofsAddr
, FC_SetBaud
, val
);
1992 moxaCurBaud
[port
] = baud
;
1996 int MoxaPortSetTermio(int port
, struct ktermios
*termio
, speed_t baud
)
1998 void __iomem
*ofsAddr
;
2002 if (moxaChkPort
[port
] == 0 || termio
== 0)
2004 ofsAddr
= moxaTableAddr
[port
];
2005 cflag
= termio
->c_cflag
; /* termio->c_cflag */
2007 mode
= termio
->c_cflag
& CSIZE
;
2010 else if (mode
== CS6
)
2012 else if (mode
== CS7
)
2014 else if (mode
== CS8
)
2017 if (termio
->c_cflag
& CSTOPB
) {
2025 if (termio
->c_cflag
& PARENB
) {
2026 if (termio
->c_cflag
& PARODD
)
2033 moxafunc(ofsAddr
, FC_SetDataMode
, (ushort
) mode
);
2035 if ((moxa_boards
[port
/ MAX_PORTS_PER_BOARD
].boardType
== MOXA_BOARD_C320_ISA
) ||
2036 (moxa_boards
[port
/ MAX_PORTS_PER_BOARD
].boardType
== MOXA_BOARD_C320_PCI
)) {
2037 if (baud
>= 921600L)
2040 MoxaPortSetBaud(port
, baud
);
2042 if (termio
->c_iflag
& (IXON
| IXOFF
| IXANY
)) {
2043 writeb(termio
->c_cc
[VSTART
], ofsAddr
+ FuncArg
);
2044 writeb(termio
->c_cc
[VSTOP
], ofsAddr
+ FuncArg1
);
2045 writeb(FC_SetXonXoff
, ofsAddr
+ FuncCode
);
2046 wait_finish(ofsAddr
);
2052 int MoxaPortGetLineOut(int port
, int *dtrState
, int *rtsState
)
2055 if (!MoxaPortIsValid(port
))
2058 if (moxaLineCtrl
[port
] & DTR_ON
)
2064 if (moxaLineCtrl
[port
] & RTS_ON
)
2072 void MoxaPortLineCtrl(int port
, int dtr
, int rts
)
2074 void __iomem
*ofsAddr
;
2077 ofsAddr
= moxaTableAddr
[port
];
2083 moxaLineCtrl
[port
] = mode
;
2084 moxafunc(ofsAddr
, FC_LineControl
, mode
);
2087 void MoxaPortFlowCtrl(int port
, int rts
, int cts
, int txflow
, int rxflow
, int txany
)
2089 void __iomem
*ofsAddr
;
2092 ofsAddr
= moxaTableAddr
[port
];
2095 mode
|= RTS_FlowCtl
;
2097 mode
|= CTS_FlowCtl
;
2104 moxafunc(ofsAddr
, FC_SetFlowCtl
, mode
);
2107 int MoxaPortLineStatus(int port
)
2109 void __iomem
*ofsAddr
;
2112 ofsAddr
= moxaTableAddr
[port
];
2113 if ((moxa_boards
[port
/ MAX_PORTS_PER_BOARD
].boardType
== MOXA_BOARD_C320_ISA
) ||
2114 (moxa_boards
[port
/ MAX_PORTS_PER_BOARD
].boardType
== MOXA_BOARD_C320_PCI
)) {
2115 moxafunc(ofsAddr
, FC_LineStatus
, 0);
2116 val
= readw(ofsAddr
+ FuncArg
);
2118 val
= readw(ofsAddr
+ FlagStat
) >> 4;
2123 if ((moxaDCDState
[port
] & DCD_oldstate
) == 0)
2124 moxaDCDState
[port
] = (DCD_oldstate
| DCD_changed
);
2126 if (moxaDCDState
[port
] & DCD_oldstate
)
2127 moxaDCDState
[port
] = DCD_changed
;
2133 int MoxaPortDCDChange(int port
)
2137 if (moxaChkPort
[port
] == 0)
2139 n
= moxaDCDState
[port
];
2140 moxaDCDState
[port
] &= ~DCD_changed
;
2145 int MoxaPortDCDON(int port
)
2149 if (moxaChkPort
[port
] == 0)
2151 if (moxaDCDState
[port
] & DCD_oldstate
)
2158 int MoxaPortWriteData(int port
, unsigned char * buffer
, int len
)
2163 ushort head
, tx_mask
, spage
, epage
;
2164 ushort pageno
, pageofs
, bufhead
;
2165 void __iomem
*baseAddr
, *ofsAddr
, *ofs
;
2167 ofsAddr
= moxaTableAddr
[port
];
2168 baseAddr
= moxaBaseAddr
[port
/ MAX_PORTS_PER_BOARD
];
2169 tx_mask
= readw(ofsAddr
+ TX_mask
);
2170 spage
= readw(ofsAddr
+ Page_txb
);
2171 epage
= readw(ofsAddr
+ EndPage_txb
);
2172 tail
= readw(ofsAddr
+ TXwptr
);
2173 head
= readw(ofsAddr
+ TXrptr
);
2174 c
= (head
> tail
) ? (head
- tail
- 1)
2175 : (head
- tail
+ tx_mask
);
2178 moxaLog
.txcnt
[port
] += c
;
2180 if (spage
== epage
) {
2181 bufhead
= readw(ofsAddr
+ Ofs_txb
);
2182 writew(spage
, baseAddr
+ Control_reg
);
2185 len
= head
- tail
- 1;
2187 len
= tx_mask
+ 1 - tail
;
2188 len
= (c
> len
) ? len
: c
;
2189 ofs
= baseAddr
+ DynPage_addr
+ bufhead
+ tail
;
2190 for (i
= 0; i
< len
; i
++)
2191 writeb(*buffer
++, ofs
+ i
);
2192 tail
= (tail
+ len
) & tx_mask
;
2195 writew(tail
, ofsAddr
+ TXwptr
);
2198 pageno
= spage
+ (tail
>> 13);
2199 pageofs
= tail
& Page_mask
;
2201 cnt
= Page_size
- pageofs
;
2205 writeb(pageno
, baseAddr
+ Control_reg
);
2206 ofs
= baseAddr
+ DynPage_addr
+ pageofs
;
2207 for (i
= 0; i
< cnt
; i
++)
2208 writeb(*buffer
++, ofs
+ i
);
2210 writew((tail
+ len
) & tx_mask
, ofsAddr
+ TXwptr
);
2213 if (++pageno
== epage
)
2218 writeb(1, ofsAddr
+ CD180TXirq
); /* start to send */
2222 int MoxaPortReadData(int port
, struct tty_struct
*tty
)
2224 register ushort head
, pageofs
;
2225 int i
, count
, cnt
, len
, total
, remain
;
2226 ushort tail
, rx_mask
, spage
, epage
;
2227 ushort pageno
, bufhead
;
2228 void __iomem
*baseAddr
, *ofsAddr
, *ofs
;
2230 ofsAddr
= moxaTableAddr
[port
];
2231 baseAddr
= moxaBaseAddr
[port
/ MAX_PORTS_PER_BOARD
];
2232 head
= readw(ofsAddr
+ RXrptr
);
2233 tail
= readw(ofsAddr
+ RXwptr
);
2234 rx_mask
= readw(ofsAddr
+ RX_mask
);
2235 spage
= readw(ofsAddr
+ Page_rxb
);
2236 epage
= readw(ofsAddr
+ EndPage_rxb
);
2237 count
= (tail
>= head
) ? (tail
- head
)
2238 : (tail
- head
+ rx_mask
+ 1);
2243 remain
= count
- total
;
2244 moxaLog
.rxcnt
[port
] += total
;
2246 if (spage
== epage
) {
2247 bufhead
= readw(ofsAddr
+ Ofs_rxb
);
2248 writew(spage
, baseAddr
+ Control_reg
);
2253 len
= rx_mask
+ 1 - head
;
2254 len
= (count
> len
) ? len
: count
;
2255 ofs
= baseAddr
+ DynPage_addr
+ bufhead
+ head
;
2256 for (i
= 0; i
< len
; i
++)
2257 tty_insert_flip_char(tty
, readb(ofs
+ i
), TTY_NORMAL
);
2258 head
= (head
+ len
) & rx_mask
;
2261 writew(head
, ofsAddr
+ RXrptr
);
2264 pageno
= spage
+ (head
>> 13);
2265 pageofs
= head
& Page_mask
;
2267 cnt
= Page_size
- pageofs
;
2271 writew(pageno
, baseAddr
+ Control_reg
);
2272 ofs
= baseAddr
+ DynPage_addr
+ pageofs
;
2273 for (i
= 0; i
< cnt
; i
++)
2274 tty_insert_flip_char(tty
, readb(ofs
+ i
), TTY_NORMAL
);
2276 writew((head
+ len
) & rx_mask
, ofsAddr
+ RXrptr
);
2279 if (++pageno
== epage
)
2284 if ((readb(ofsAddr
+ FlagStat
) & Xoff_state
) && (remain
< LowWater
)) {
2285 moxaLowWaterChk
= 1;
2286 moxaLowChkFlag
[port
] = 1;
2292 int MoxaPortTxQueue(int port
)
2294 void __iomem
*ofsAddr
;
2295 ushort rptr
, wptr
, mask
;
2298 ofsAddr
= moxaTableAddr
[port
];
2299 rptr
= readw(ofsAddr
+ TXrptr
);
2300 wptr
= readw(ofsAddr
+ TXwptr
);
2301 mask
= readw(ofsAddr
+ TX_mask
);
2302 len
= (wptr
- rptr
) & mask
;
2306 int MoxaPortTxFree(int port
)
2308 void __iomem
*ofsAddr
;
2309 ushort rptr
, wptr
, mask
;
2312 ofsAddr
= moxaTableAddr
[port
];
2313 rptr
= readw(ofsAddr
+ TXrptr
);
2314 wptr
= readw(ofsAddr
+ TXwptr
);
2315 mask
= readw(ofsAddr
+ TX_mask
);
2316 len
= mask
- ((wptr
- rptr
) & mask
);
2320 int MoxaPortRxQueue(int port
)
2322 void __iomem
*ofsAddr
;
2323 ushort rptr
, wptr
, mask
;
2326 ofsAddr
= moxaTableAddr
[port
];
2327 rptr
= readw(ofsAddr
+ RXrptr
);
2328 wptr
= readw(ofsAddr
+ RXwptr
);
2329 mask
= readw(ofsAddr
+ RX_mask
);
2330 len
= (wptr
- rptr
) & mask
;
2335 void MoxaPortTxDisable(int port
)
2337 void __iomem
*ofsAddr
;
2339 ofsAddr
= moxaTableAddr
[port
];
2340 moxafunc(ofsAddr
, FC_SetXoffState
, Magic_code
);
2343 void MoxaPortTxEnable(int port
)
2345 void __iomem
*ofsAddr
;
2347 ofsAddr
= moxaTableAddr
[port
];
2348 moxafunc(ofsAddr
, FC_SetXonState
, Magic_code
);
2352 int MoxaPortResetBrkCnt(int port
)
2355 cnt
= moxaBreakCnt
[port
];
2356 moxaBreakCnt
[port
] = 0;
2361 void MoxaPortSendBreak(int port
, int ms100
)
2363 void __iomem
*ofsAddr
;
2365 ofsAddr
= moxaTableAddr
[port
];
2367 moxafunc(ofsAddr
, FC_SendBreak
, Magic_code
);
2368 moxadelay(ms100
* (HZ
/ 10));
2370 moxafunc(ofsAddr
, FC_SendBreak
, Magic_code
);
2371 moxadelay(HZ
/ 4); /* 250 ms */
2373 moxafunc(ofsAddr
, FC_StopBreak
, Magic_code
);
2376 static int moxa_get_serial_info(struct moxa_str
*info
,
2377 struct serial_struct __user
*retinfo
)
2379 struct serial_struct tmp
;
2381 memset(&tmp
, 0, sizeof(tmp
));
2382 tmp
.type
= info
->type
;
2383 tmp
.line
= info
->port
;
2386 tmp
.flags
= info
->asyncflags
;
2387 tmp
.baud_base
= 921600;
2388 tmp
.close_delay
= info
->close_delay
;
2389 tmp
.closing_wait
= info
->closing_wait
;
2390 tmp
.custom_divisor
= 0;
2392 if(copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
2398 static int moxa_set_serial_info(struct moxa_str
*info
,
2399 struct serial_struct __user
*new_info
)
2401 struct serial_struct new_serial
;
2403 if(copy_from_user(&new_serial
, new_info
, sizeof(new_serial
)))
2406 if ((new_serial
.irq
!= 0) ||
2407 (new_serial
.port
!= 0) ||
2408 // (new_serial.type != info->type) ||
2409 (new_serial
.custom_divisor
!= 0) ||
2410 (new_serial
.baud_base
!= 921600))
2413 if (!capable(CAP_SYS_ADMIN
)) {
2414 if (((new_serial
.flags
& ~ASYNC_USR_MASK
) !=
2415 (info
->asyncflags
& ~ASYNC_USR_MASK
)))
2418 info
->close_delay
= new_serial
.close_delay
* HZ
/ 100;
2419 info
->closing_wait
= new_serial
.closing_wait
* HZ
/ 100;
2422 new_serial
.flags
= (new_serial
.flags
& ~ASYNC_FLAGS
);
2423 new_serial
.flags
|= (info
->asyncflags
& ASYNC_FLAGS
);
2425 if (new_serial
.type
== PORT_16550A
) {
2426 MoxaSetFifo(info
->port
, 1);
2428 MoxaSetFifo(info
->port
, 0);
2431 info
->type
= new_serial
.type
;
2437 /*****************************************************************************
2438 * Static local functions: *
2439 *****************************************************************************/
2441 * moxadelay - delays a specified number ticks
2443 static void moxadelay(int tick
)
2445 unsigned long st
, et
;
2449 while (time_before(jiffies
, et
));
2452 static void moxafunc(void __iomem
*ofsAddr
, int cmd
, ushort arg
)
2455 writew(arg
, ofsAddr
+ FuncArg
);
2456 writew(cmd
, ofsAddr
+ FuncCode
);
2457 wait_finish(ofsAddr
);
2460 static void wait_finish(void __iomem
*ofsAddr
)
2465 while (readw(ofsAddr
+ FuncCode
) != 0) {
2467 if ((j
- i
) > moxaFuncTout
) {
2473 static void low_water_check(void __iomem
*ofsAddr
)
2476 ushort rptr
, wptr
, mask
;
2478 if (readb(ofsAddr
+ FlagStat
) & Xoff_state
) {
2479 rptr
= readw(ofsAddr
+ RXrptr
);
2480 wptr
= readw(ofsAddr
+ RXwptr
);
2481 mask
= readw(ofsAddr
+ RX_mask
);
2482 len
= (wptr
- rptr
) & mask
;
2483 if (len
<= Low_water
)
2484 moxafunc(ofsAddr
, FC_SendXon
, 0);
2488 static int moxaloadbios(int cardno
, unsigned char __user
*tmp
, int len
)
2490 void __iomem
*baseAddr
;
2493 if(copy_from_user(moxaBuff
, tmp
, len
))
2495 baseAddr
= moxaBaseAddr
[cardno
];
2496 writeb(HW_reset
, baseAddr
+ Control_reg
); /* reset */
2497 moxadelay(1); /* delay 10 ms */
2498 for (i
= 0; i
< 4096; i
++)
2499 writeb(0, baseAddr
+ i
); /* clear fix page */
2500 for (i
= 0; i
< len
; i
++)
2501 writeb(moxaBuff
[i
], baseAddr
+ i
); /* download BIOS */
2502 writeb(0, baseAddr
+ Control_reg
); /* restart */
2506 static int moxafindcard(int cardno
)
2508 void __iomem
*baseAddr
;
2511 baseAddr
= moxaBaseAddr
[cardno
];
2512 switch (moxa_boards
[cardno
].boardType
) {
2513 case MOXA_BOARD_C218_ISA
:
2514 case MOXA_BOARD_C218_PCI
:
2515 if ((tmp
= readw(baseAddr
+ C218_key
)) != C218_KeyCode
) {
2519 case MOXA_BOARD_CP204J
:
2520 if ((tmp
= readw(baseAddr
+ C218_key
)) != CP204J_KeyCode
) {
2525 if ((tmp
= readw(baseAddr
+ C320_key
)) != C320_KeyCode
) {
2528 if ((tmp
= readw(baseAddr
+ C320_status
)) != STS_init
) {
2535 static int moxaload320b(int cardno
, unsigned char __user
*tmp
, int len
)
2537 void __iomem
*baseAddr
;
2540 if(len
> sizeof(moxaBuff
))
2542 if(copy_from_user(moxaBuff
, tmp
, len
))
2544 baseAddr
= moxaBaseAddr
[cardno
];
2545 writew(len
- 7168 - 2, baseAddr
+ C320bapi_len
);
2546 writeb(1, baseAddr
+ Control_reg
); /* Select Page 1 */
2547 for (i
= 0; i
< 7168; i
++)
2548 writeb(moxaBuff
[i
], baseAddr
+ DynPage_addr
+ i
);
2549 writeb(2, baseAddr
+ Control_reg
); /* Select Page 2 */
2550 for (i
= 0; i
< (len
- 7168); i
++)
2551 writeb(moxaBuff
[i
+ 7168], baseAddr
+ DynPage_addr
+ i
);
2555 static int moxaloadcode(int cardno
, unsigned char __user
*tmp
, int len
)
2557 void __iomem
*baseAddr
, *ofsAddr
;
2558 int retval
, port
, i
;
2560 if(copy_from_user(moxaBuff
, tmp
, len
))
2562 baseAddr
= moxaBaseAddr
[cardno
];
2563 switch (moxa_boards
[cardno
].boardType
) {
2564 case MOXA_BOARD_C218_ISA
:
2565 case MOXA_BOARD_C218_PCI
:
2566 case MOXA_BOARD_CP204J
:
2567 retval
= moxaloadc218(cardno
, baseAddr
, len
);
2570 port
= cardno
* MAX_PORTS_PER_BOARD
;
2571 for (i
= 0; i
< moxa_boards
[cardno
].numPorts
; i
++, port
++) {
2572 moxaChkPort
[port
] = 1;
2573 moxaCurBaud
[port
] = 9600L;
2574 moxaDCDState
[port
] = 0;
2575 moxaTableAddr
[port
] = baseAddr
+ Extern_table
+ Extern_size
* i
;
2576 ofsAddr
= moxaTableAddr
[port
];
2577 writew(C218rx_mask
, ofsAddr
+ RX_mask
);
2578 writew(C218tx_mask
, ofsAddr
+ TX_mask
);
2579 writew(C218rx_spage
+ i
* C218buf_pageno
, ofsAddr
+ Page_rxb
);
2580 writew(readw(ofsAddr
+ Page_rxb
) + C218rx_pageno
, ofsAddr
+ EndPage_rxb
);
2582 writew(C218tx_spage
+ i
* C218buf_pageno
, ofsAddr
+ Page_txb
);
2583 writew(readw(ofsAddr
+ Page_txb
) + C218tx_pageno
, ofsAddr
+ EndPage_txb
);
2588 retval
= moxaloadc320(cardno
, baseAddr
, len
,
2589 &moxa_boards
[cardno
].numPorts
);
2592 port
= cardno
* MAX_PORTS_PER_BOARD
;
2593 for (i
= 0; i
< moxa_boards
[cardno
].numPorts
; i
++, port
++) {
2594 moxaChkPort
[port
] = 1;
2595 moxaCurBaud
[port
] = 9600L;
2596 moxaDCDState
[port
] = 0;
2597 moxaTableAddr
[port
] = baseAddr
+ Extern_table
+ Extern_size
* i
;
2598 ofsAddr
= moxaTableAddr
[port
];
2599 if (moxa_boards
[cardno
].numPorts
== 8) {
2600 writew(C320p8rx_mask
, ofsAddr
+ RX_mask
);
2601 writew(C320p8tx_mask
, ofsAddr
+ TX_mask
);
2602 writew(C320p8rx_spage
+ i
* C320p8buf_pgno
, ofsAddr
+ Page_rxb
);
2603 writew(readw(ofsAddr
+ Page_rxb
) + C320p8rx_pgno
, ofsAddr
+ EndPage_rxb
);
2604 writew(C320p8tx_spage
+ i
* C320p8buf_pgno
, ofsAddr
+ Page_txb
);
2605 writew(readw(ofsAddr
+ Page_txb
) + C320p8tx_pgno
, ofsAddr
+ EndPage_txb
);
2607 } else if (moxa_boards
[cardno
].numPorts
== 16) {
2608 writew(C320p16rx_mask
, ofsAddr
+ RX_mask
);
2609 writew(C320p16tx_mask
, ofsAddr
+ TX_mask
);
2610 writew(C320p16rx_spage
+ i
* C320p16buf_pgno
, ofsAddr
+ Page_rxb
);
2611 writew(readw(ofsAddr
+ Page_rxb
) + C320p16rx_pgno
, ofsAddr
+ EndPage_rxb
);
2612 writew(C320p16tx_spage
+ i
* C320p16buf_pgno
, ofsAddr
+ Page_txb
);
2613 writew(readw(ofsAddr
+ Page_txb
) + C320p16tx_pgno
, ofsAddr
+ EndPage_txb
);
2615 } else if (moxa_boards
[cardno
].numPorts
== 24) {
2616 writew(C320p24rx_mask
, ofsAddr
+ RX_mask
);
2617 writew(C320p24tx_mask
, ofsAddr
+ TX_mask
);
2618 writew(C320p24rx_spage
+ i
* C320p24buf_pgno
, ofsAddr
+ Page_rxb
);
2619 writew(readw(ofsAddr
+ Page_rxb
) + C320p24rx_pgno
, ofsAddr
+ EndPage_rxb
);
2620 writew(C320p24tx_spage
+ i
* C320p24buf_pgno
, ofsAddr
+ Page_txb
);
2621 writew(readw(ofsAddr
+ Page_txb
), ofsAddr
+ EndPage_txb
);
2622 } else if (moxa_boards
[cardno
].numPorts
== 32) {
2623 writew(C320p32rx_mask
, ofsAddr
+ RX_mask
);
2624 writew(C320p32tx_mask
, ofsAddr
+ TX_mask
);
2625 writew(C320p32tx_ofs
, ofsAddr
+ Ofs_txb
);
2626 writew(C320p32rx_spage
+ i
* C320p32buf_pgno
, ofsAddr
+ Page_rxb
);
2627 writew(readb(ofsAddr
+ Page_rxb
), ofsAddr
+ EndPage_rxb
);
2628 writew(C320p32tx_spage
+ i
* C320p32buf_pgno
, ofsAddr
+ Page_txb
);
2629 writew(readw(ofsAddr
+ Page_txb
), ofsAddr
+ EndPage_txb
);
2634 loadstat
[cardno
] = 1;
2638 static int moxaloadc218(int cardno
, void __iomem
*baseAddr
, int len
)
2641 int i
, j
, len1
, len2
;
2642 ushort usum
, *ptr
, keycode
;
2644 if (moxa_boards
[cardno
].boardType
== MOXA_BOARD_CP204J
)
2645 keycode
= CP204J_KeyCode
;
2647 keycode
= C218_KeyCode
;
2650 ptr
= (ushort
*) moxaBuff
;
2651 for (i
= 0; i
< len1
; i
++)
2652 usum
+= le16_to_cpu(*(ptr
+ i
));
2658 len2
= (len1
> 2048) ? 2048 : len1
;
2660 for (i
= 0; i
< len2
<< 1; i
++)
2661 writeb(moxaBuff
[i
+ j
], baseAddr
+ C218_LoadBuf
+ i
);
2664 writew(len2
, baseAddr
+ C218DLoad_len
);
2665 writew(0, baseAddr
+ C218_key
);
2666 for (i
= 0; i
< 100; i
++) {
2667 if (readw(baseAddr
+ C218_key
) == keycode
)
2669 moxadelay(1); /* delay 10 ms */
2671 if (readw(baseAddr
+ C218_key
) != keycode
) {
2675 writew(0, baseAddr
+ C218DLoad_len
);
2676 writew(usum
, baseAddr
+ C218check_sum
);
2677 writew(0, baseAddr
+ C218_key
);
2678 for (i
= 0; i
< 100; i
++) {
2679 if (readw(baseAddr
+ C218_key
) == keycode
)
2681 moxadelay(1); /* delay 10 ms */
2684 } while ((readb(baseAddr
+ C218chksum_ok
) != 1) && (retry
< 3));
2685 if (readb(baseAddr
+ C218chksum_ok
) != 1) {
2688 writew(0, baseAddr
+ C218_key
);
2689 for (i
= 0; i
< 100; i
++) {
2690 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
2692 moxadelay(1); /* delay 10 ms */
2694 if (readw(baseAddr
+ Magic_no
) != Magic_code
) {
2697 writew(1, baseAddr
+ Disable_IRQ
);
2698 writew(0, baseAddr
+ Magic_no
);
2699 for (i
= 0; i
< 100; i
++) {
2700 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
2702 moxadelay(1); /* delay 10 ms */
2704 if (readw(baseAddr
+ Magic_no
) != Magic_code
) {
2708 moxaIntNdx
[cardno
] = baseAddr
+ IRQindex
;
2709 moxaIntPend
[cardno
] = baseAddr
+ IRQpending
;
2710 moxaIntTable
[cardno
] = baseAddr
+ IRQtable
;
2714 static int moxaloadc320(int cardno
, void __iomem
*baseAddr
, int len
, int *numPorts
)
2717 int i
, j
, wlen
, len2
, retry
;
2722 uptr
= (ushort
*) moxaBuff
;
2723 for (i
= 0; i
< wlen
; i
++)
2724 usum
+= le16_to_cpu(uptr
[i
]);
2735 for (i
= 0; i
< len2
; i
++)
2736 writeb(moxaBuff
[j
+ i
], baseAddr
+ C320_LoadBuf
+ i
);
2739 writew(len2
, baseAddr
+ C320DLoad_len
);
2740 writew(0, baseAddr
+ C320_key
);
2741 for (i
= 0; i
< 10; i
++) {
2742 if (readw(baseAddr
+ C320_key
) == C320_KeyCode
)
2746 if (readw(baseAddr
+ C320_key
) != C320_KeyCode
)
2749 writew(0, baseAddr
+ C320DLoad_len
);
2750 writew(usum
, baseAddr
+ C320check_sum
);
2751 writew(0, baseAddr
+ C320_key
);
2752 for (i
= 0; i
< 10; i
++) {
2753 if (readw(baseAddr
+ C320_key
) == C320_KeyCode
)
2758 } while ((readb(baseAddr
+ C320chksum_ok
) != 1) && (retry
< 3));
2759 if (readb(baseAddr
+ C320chksum_ok
) != 1)
2761 writew(0, baseAddr
+ C320_key
);
2762 for (i
= 0; i
< 600; i
++) {
2763 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
2767 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
2770 if (moxa_boards
[cardno
].busType
== MOXA_BUS_TYPE_PCI
) { /* ASIC board */
2771 writew(0x3800, baseAddr
+ TMS320_PORT1
);
2772 writew(0x3900, baseAddr
+ TMS320_PORT2
);
2773 writew(28499, baseAddr
+ TMS320_CLOCK
);
2775 writew(0x3200, baseAddr
+ TMS320_PORT1
);
2776 writew(0x3400, baseAddr
+ TMS320_PORT2
);
2777 writew(19999, baseAddr
+ TMS320_CLOCK
);
2779 writew(1, baseAddr
+ Disable_IRQ
);
2780 writew(0, baseAddr
+ Magic_no
);
2781 for (i
= 0; i
< 500; i
++) {
2782 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
2786 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
2789 j
= readw(baseAddr
+ Module_cnt
);
2793 writew(j
, baseAddr
+ Module_no
);
2794 writew(0, baseAddr
+ Magic_no
);
2795 for (i
= 0; i
< 600; i
++) {
2796 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
2800 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
2803 moxaIntNdx
[cardno
] = baseAddr
+ IRQindex
;
2804 moxaIntPend
[cardno
] = baseAddr
+ IRQpending
;
2805 moxaIntTable
[cardno
] = baseAddr
+ IRQtable
;
2809 static void MoxaSetFifo(int port
, int enable
)
2811 void __iomem
*ofsAddr
= moxaTableAddr
[port
];
2814 moxafunc(ofsAddr
, FC_SetRxFIFOTrig
, 0);
2815 moxafunc(ofsAddr
, FC_SetTxFIFOCnt
, 1);
2817 moxafunc(ofsAddr
, FC_SetRxFIFOTrig
, 3);
2818 moxafunc(ofsAddr
, FC_SetTxFIFOCnt
, 16);