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/firmware.h>
29 #include <linux/signal.h>
30 #include <linux/sched.h>
31 #include <linux/timer.h>
32 #include <linux/interrupt.h>
33 #include <linux/tty.h>
34 #include <linux/tty_flip.h>
35 #include <linux/major.h>
36 #include <linux/string.h>
37 #include <linux/fcntl.h>
38 #include <linux/ptrace.h>
39 #include <linux/serial.h>
40 #include <linux/tty_driver.h>
41 #include <linux/delay.h>
42 #include <linux/pci.h>
43 #include <linux/init.h>
44 #include <linux/bitops.h>
45 #include <linux/completion.h>
47 #include <asm/system.h>
49 #include <asm/uaccess.h>
53 #define MOXA_VERSION "5.1k"
55 #define MOXA_FW_HDRLEN 32
58 #define MOXACUMAJOR 173
60 #define MAX_BOARDS 4 /* Don't change this value */
61 #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
62 #define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
65 * Define the Moxa PCI vendor and device IDs.
67 #define MOXA_BUS_TYPE_ISA 0
68 #define MOXA_BUS_TYPE_PCI 1
71 MOXA_BOARD_C218_PCI
= 1,
78 static char *moxa_brdname
[] =
80 "C218 Turbo PCI series",
81 "C218 Turbo ISA series",
82 "C320 Turbo PCI series",
83 "C320 Turbo ISA series",
88 static struct pci_device_id moxa_pcibrds
[] = {
89 { PCI_DEVICE(PCI_VENDOR_ID_MOXA
, PCI_DEVICE_ID_MOXA_C218
),
90 .driver_data
= MOXA_BOARD_C218_PCI
},
91 { PCI_DEVICE(PCI_VENDOR_ID_MOXA
, PCI_DEVICE_ID_MOXA_C320
),
92 .driver_data
= MOXA_BOARD_C320_PCI
},
93 { PCI_DEVICE(PCI_VENDOR_ID_MOXA
, PCI_DEVICE_ID_MOXA_CP204J
),
94 .driver_data
= MOXA_BOARD_CP204J
},
97 MODULE_DEVICE_TABLE(pci
, moxa_pcibrds
);
98 #endif /* CONFIG_PCI */
102 static struct moxa_board_conf
{
109 struct moxa_port
*ports
;
111 void __iomem
*basemem
;
112 void __iomem
*intNdx
;
113 void __iomem
*intPend
;
114 void __iomem
*intTable
;
115 } moxa_boards
[MAX_BOARDS
];
117 struct mxser_mstatus
{
131 struct moxa_board_conf
*board
;
137 unsigned long statusflags
;
138 struct tty_struct
*tty
;
140 wait_queue_head_t open_wait
;
141 struct completion close_wait
;
143 struct timer_list emptyTimer
;
146 void __iomem
*tableAddr
;
154 #define TXSTOPPED 0x1
156 #define EMPTYWAIT 0x4
159 #define SERIAL_DO_RESTART
161 #define WAKEUP_CHARS 256
163 static int ttymajor
= MOXAMAJOR
;
164 /* Variables for insmod */
166 static unsigned long baseaddr
[MAX_BOARDS
];
167 static unsigned int type
[MAX_BOARDS
];
168 static unsigned int numports
[MAX_BOARDS
];
171 MODULE_AUTHOR("William Chen");
172 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
173 MODULE_LICENSE("GPL");
175 module_param_array(type
, uint
, NULL
, 0);
176 MODULE_PARM_DESC(type
, "card type: C218=2, C320=4");
177 module_param_array(baseaddr
, ulong
, NULL
, 0);
178 MODULE_PARM_DESC(baseaddr
, "base address");
179 module_param_array(numports
, uint
, NULL
, 0);
180 MODULE_PARM_DESC(numports
, "numports (ignored for C218)");
182 module_param(ttymajor
, int, 0);
187 static int moxa_open(struct tty_struct
*, struct file
*);
188 static void moxa_close(struct tty_struct
*, struct file
*);
189 static int moxa_write(struct tty_struct
*, const unsigned char *, int);
190 static int moxa_write_room(struct tty_struct
*);
191 static void moxa_flush_buffer(struct tty_struct
*);
192 static int moxa_chars_in_buffer(struct tty_struct
*);
193 static void moxa_flush_chars(struct tty_struct
*);
194 static void moxa_put_char(struct tty_struct
*, unsigned char);
195 static int moxa_ioctl(struct tty_struct
*, struct file
*, unsigned int, unsigned long);
196 static void moxa_throttle(struct tty_struct
*);
197 static void moxa_unthrottle(struct tty_struct
*);
198 static void moxa_set_termios(struct tty_struct
*, struct ktermios
*);
199 static void moxa_stop(struct tty_struct
*);
200 static void moxa_start(struct tty_struct
*);
201 static void moxa_hangup(struct tty_struct
*);
202 static int moxa_tiocmget(struct tty_struct
*tty
, struct file
*file
);
203 static int moxa_tiocmset(struct tty_struct
*tty
, struct file
*file
,
204 unsigned int set
, unsigned int clear
);
205 static void moxa_poll(unsigned long);
206 static void moxa_set_tty_param(struct tty_struct
*, struct ktermios
*);
207 static int moxa_block_till_ready(struct tty_struct
*, struct file
*,
209 static void moxa_setup_empty_event(struct tty_struct
*);
210 static void moxa_check_xmit_empty(unsigned long);
211 static void moxa_shut_down(struct moxa_port
*);
212 static void moxa_receive_data(struct moxa_port
*);
214 * moxa board interface functions:
216 static int MoxaDriverIoctl(struct tty_struct
*, unsigned int, unsigned long);
217 static int MoxaDriverPoll(void);
218 static int MoxaPortsOfCard(int);
219 static void MoxaPortEnable(struct moxa_port
*);
220 static void MoxaPortDisable(struct moxa_port
*);
221 static int MoxaPortSetTermio(struct moxa_port
*, struct ktermios
*, speed_t
);
222 static int MoxaPortGetLineOut(struct moxa_port
*, int *, int *);
223 static void MoxaPortLineCtrl(struct moxa_port
*, int, int);
224 static void MoxaPortFlowCtrl(struct moxa_port
*, int, int, int, int, int);
225 static int MoxaPortLineStatus(struct moxa_port
*);
226 static int MoxaPortDCDChange(struct moxa_port
*);
227 static int MoxaPortDCDON(struct moxa_port
*);
228 static void MoxaPortFlushData(struct moxa_port
*, int);
229 static int MoxaPortWriteData(struct moxa_port
*, unsigned char *, int);
230 static int MoxaPortReadData(struct moxa_port
*, struct tty_struct
*tty
);
231 static int MoxaPortTxQueue(struct moxa_port
*);
232 static int MoxaPortRxQueue(struct moxa_port
*);
233 static int MoxaPortTxFree(struct moxa_port
*);
234 static void MoxaPortTxDisable(struct moxa_port
*);
235 static void MoxaPortTxEnable(struct moxa_port
*);
236 static int MoxaPortResetBrkCnt(struct moxa_port
*);
237 static void MoxaPortSendBreak(struct moxa_port
*, int);
238 static int moxa_get_serial_info(struct moxa_port
*, struct serial_struct __user
*);
239 static int moxa_set_serial_info(struct moxa_port
*, struct serial_struct __user
*);
240 static void MoxaSetFifo(struct moxa_port
*port
, int enable
);
242 static const struct tty_operations moxa_ops
= {
246 .write_room
= moxa_write_room
,
247 .flush_buffer
= moxa_flush_buffer
,
248 .chars_in_buffer
= moxa_chars_in_buffer
,
249 .flush_chars
= moxa_flush_chars
,
250 .put_char
= moxa_put_char
,
252 .throttle
= moxa_throttle
,
253 .unthrottle
= moxa_unthrottle
,
254 .set_termios
= moxa_set_termios
,
257 .hangup
= moxa_hangup
,
258 .tiocmget
= moxa_tiocmget
,
259 .tiocmset
= moxa_tiocmset
,
262 static struct tty_driver
*moxaDriver
;
263 static DEFINE_TIMER(moxaTimer
, moxa_poll
, 0, 0);
264 static DEFINE_SPINLOCK(moxa_lock
);
266 static int moxa_check_fw_model(struct moxa_board_conf
*brd
, u8 model
)
268 switch (brd
->boardType
) {
269 case MOXA_BOARD_C218_ISA
:
270 case MOXA_BOARD_C218_PCI
:
274 case MOXA_BOARD_CP204J
:
288 static int moxa_check_fw(const void *ptr
)
290 const __le16
*lptr
= ptr
;
292 if (*lptr
!= cpu_to_le16(0x7980))
298 static int moxa_load_bios(struct moxa_board_conf
*brd
, const u8
*buf
,
301 void __iomem
*baseAddr
= brd
->basemem
;
304 writeb(HW_reset
, baseAddr
+ Control_reg
); /* reset */
306 memset_io(baseAddr
, 0, 4096);
307 memcpy_toio(baseAddr
, buf
, len
); /* download BIOS */
308 writeb(0, baseAddr
+ Control_reg
); /* restart */
312 switch (brd
->boardType
) {
313 case MOXA_BOARD_C218_ISA
:
314 case MOXA_BOARD_C218_PCI
:
315 tmp
= readw(baseAddr
+ C218_key
);
316 if (tmp
!= C218_KeyCode
)
319 case MOXA_BOARD_CP204J
:
320 tmp
= readw(baseAddr
+ C218_key
);
321 if (tmp
!= CP204J_KeyCode
)
325 tmp
= readw(baseAddr
+ C320_key
);
326 if (tmp
!= C320_KeyCode
)
328 tmp
= readw(baseAddr
+ C320_status
);
329 if (tmp
!= STS_init
) {
330 printk(KERN_ERR
"moxa: bios upload failed -- CPU/Basic "
331 "module not found\n");
339 printk(KERN_ERR
"moxa: bios upload failed -- board not found\n");
343 static int moxa_load_320b(struct moxa_board_conf
*brd
, const u8
*ptr
,
346 void __iomem
*baseAddr
= brd
->basemem
;
349 printk(KERN_ERR
"moxa: invalid 320 bios -- too short\n");
353 writew(len
- 7168 - 2, baseAddr
+ C320bapi_len
);
354 writeb(1, baseAddr
+ Control_reg
); /* Select Page 1 */
355 memcpy_toio(baseAddr
+ DynPage_addr
, ptr
, 7168);
356 writeb(2, baseAddr
+ Control_reg
); /* Select Page 2 */
357 memcpy_toio(baseAddr
+ DynPage_addr
, ptr
+ 7168, len
- 7168);
362 static int moxa_real_load_code(struct moxa_board_conf
*brd
, const void *ptr
,
365 void __iomem
*baseAddr
= brd
->basemem
;
366 const u16
*uptr
= ptr
;
367 size_t wlen
, len2
, j
;
368 unsigned long key
, loadbuf
, loadlen
, checksum
, checksum_ok
;
369 unsigned int i
, retry
, c320
;
372 c320
= brd
->boardType
== MOXA_BOARD_C320_PCI
||
373 brd
->boardType
== MOXA_BOARD_C320_ISA
;
374 keycode
= (brd
->boardType
== MOXA_BOARD_CP204J
) ? CP204J_KeyCode
:
377 switch (brd
->boardType
) {
378 case MOXA_BOARD_CP204J
:
379 case MOXA_BOARD_C218_ISA
:
380 case MOXA_BOARD_C218_PCI
:
382 loadbuf
= C218_LoadBuf
;
383 loadlen
= C218DLoad_len
;
384 checksum
= C218check_sum
;
385 checksum_ok
= C218chksum_ok
;
389 keycode
= C320_KeyCode
;
390 loadbuf
= C320_LoadBuf
;
391 loadlen
= C320DLoad_len
;
392 checksum
= C320check_sum
;
393 checksum_ok
= C320chksum_ok
;
399 for (i
= 0; i
< wlen
; i
++)
400 usum
+= le16_to_cpu(uptr
[i
]);
406 len2
= (wlen
> 2048) ? 2048 : wlen
;
408 memcpy_toio(baseAddr
+ loadbuf
, ptr
+ j
, len2
<< 1);
411 writew(len2
, baseAddr
+ loadlen
);
412 writew(0, baseAddr
+ key
);
413 for (i
= 0; i
< 100; i
++) {
414 if (readw(baseAddr
+ key
) == keycode
)
418 if (readw(baseAddr
+ key
) != keycode
)
421 writew(0, baseAddr
+ loadlen
);
422 writew(usum
, baseAddr
+ checksum
);
423 writew(0, baseAddr
+ key
);
424 for (i
= 0; i
< 100; i
++) {
425 if (readw(baseAddr
+ key
) == keycode
)
430 } while ((readb(baseAddr
+ checksum_ok
) != 1) && (retry
< 3));
431 if (readb(baseAddr
+ checksum_ok
) != 1)
434 writew(0, baseAddr
+ key
);
435 for (i
= 0; i
< 600; i
++) {
436 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
440 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
444 if (brd
->busType
== MOXA_BUS_TYPE_PCI
) { /* ASIC board */
445 writew(0x3800, baseAddr
+ TMS320_PORT1
);
446 writew(0x3900, baseAddr
+ TMS320_PORT2
);
447 writew(28499, baseAddr
+ TMS320_CLOCK
);
449 writew(0x3200, baseAddr
+ TMS320_PORT1
);
450 writew(0x3400, baseAddr
+ TMS320_PORT2
);
451 writew(19999, baseAddr
+ TMS320_CLOCK
);
454 writew(1, baseAddr
+ Disable_IRQ
);
455 writew(0, baseAddr
+ Magic_no
);
456 for (i
= 0; i
< 500; i
++) {
457 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
461 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
465 j
= readw(baseAddr
+ Module_cnt
);
468 brd
->numPorts
= j
* 8;
469 writew(j
, baseAddr
+ Module_no
);
470 writew(0, baseAddr
+ Magic_no
);
471 for (i
= 0; i
< 600; i
++) {
472 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
476 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
479 brd
->intNdx
= baseAddr
+ IRQindex
;
480 brd
->intPend
= baseAddr
+ IRQpending
;
481 brd
->intTable
= baseAddr
+ IRQtable
;
486 static int moxa_load_code(struct moxa_board_conf
*brd
, const void *ptr
,
489 void __iomem
*ofsAddr
, *baseAddr
= brd
->basemem
;
490 struct moxa_port
*port
;
494 printk(KERN_ERR
"moxa: bios length is not even\n");
498 retval
= moxa_real_load_code(brd
, ptr
, len
); /* may change numPorts */
502 switch (brd
->boardType
) {
503 case MOXA_BOARD_C218_ISA
:
504 case MOXA_BOARD_C218_PCI
:
505 case MOXA_BOARD_CP204J
:
507 for (i
= 0; i
< brd
->numPorts
; i
++, port
++) {
510 port
->tableAddr
= baseAddr
+ Extern_table
+
512 ofsAddr
= port
->tableAddr
;
513 writew(C218rx_mask
, ofsAddr
+ RX_mask
);
514 writew(C218tx_mask
, ofsAddr
+ TX_mask
);
515 writew(C218rx_spage
+ i
* C218buf_pageno
, ofsAddr
+ Page_rxb
);
516 writew(readw(ofsAddr
+ Page_rxb
) + C218rx_pageno
, ofsAddr
+ EndPage_rxb
);
518 writew(C218tx_spage
+ i
* C218buf_pageno
, ofsAddr
+ Page_txb
);
519 writew(readw(ofsAddr
+ Page_txb
) + C218tx_pageno
, ofsAddr
+ EndPage_txb
);
525 for (i
= 0; i
< brd
->numPorts
; i
++, port
++) {
528 port
->tableAddr
= baseAddr
+ Extern_table
+
530 ofsAddr
= port
->tableAddr
;
531 switch (brd
->numPorts
) {
533 writew(C320p8rx_mask
, ofsAddr
+ RX_mask
);
534 writew(C320p8tx_mask
, ofsAddr
+ TX_mask
);
535 writew(C320p8rx_spage
+ i
* C320p8buf_pgno
, ofsAddr
+ Page_rxb
);
536 writew(readw(ofsAddr
+ Page_rxb
) + C320p8rx_pgno
, ofsAddr
+ EndPage_rxb
);
537 writew(C320p8tx_spage
+ i
* C320p8buf_pgno
, ofsAddr
+ Page_txb
);
538 writew(readw(ofsAddr
+ Page_txb
) + C320p8tx_pgno
, ofsAddr
+ EndPage_txb
);
542 writew(C320p16rx_mask
, ofsAddr
+ RX_mask
);
543 writew(C320p16tx_mask
, ofsAddr
+ TX_mask
);
544 writew(C320p16rx_spage
+ i
* C320p16buf_pgno
, ofsAddr
+ Page_rxb
);
545 writew(readw(ofsAddr
+ Page_rxb
) + C320p16rx_pgno
, ofsAddr
+ EndPage_rxb
);
546 writew(C320p16tx_spage
+ i
* C320p16buf_pgno
, ofsAddr
+ Page_txb
);
547 writew(readw(ofsAddr
+ Page_txb
) + C320p16tx_pgno
, ofsAddr
+ EndPage_txb
);
551 writew(C320p24rx_mask
, ofsAddr
+ RX_mask
);
552 writew(C320p24tx_mask
, ofsAddr
+ TX_mask
);
553 writew(C320p24rx_spage
+ i
* C320p24buf_pgno
, ofsAddr
+ Page_rxb
);
554 writew(readw(ofsAddr
+ Page_rxb
) + C320p24rx_pgno
, ofsAddr
+ EndPage_rxb
);
555 writew(C320p24tx_spage
+ i
* C320p24buf_pgno
, ofsAddr
+ Page_txb
);
556 writew(readw(ofsAddr
+ Page_txb
), ofsAddr
+ EndPage_txb
);
559 writew(C320p32rx_mask
, ofsAddr
+ RX_mask
);
560 writew(C320p32tx_mask
, ofsAddr
+ TX_mask
);
561 writew(C320p32tx_ofs
, ofsAddr
+ Ofs_txb
);
562 writew(C320p32rx_spage
+ i
* C320p32buf_pgno
, ofsAddr
+ Page_rxb
);
563 writew(readb(ofsAddr
+ Page_rxb
), ofsAddr
+ EndPage_rxb
);
564 writew(C320p32tx_spage
+ i
* C320p32buf_pgno
, ofsAddr
+ Page_txb
);
565 writew(readw(ofsAddr
+ Page_txb
), ofsAddr
+ EndPage_txb
);
574 static int moxa_load_fw(struct moxa_board_conf
*brd
, const struct firmware
*fw
)
576 void *ptr
= fw
->data
;
580 unsigned int a
, lenp
, lencnt
;
583 __le32 magic
; /* 0x34303430 */
585 u8 type
; /* UNIX = 3 */
586 u8 model
; /* C218T=1, C320T=2, CP204=3 */
591 BUILD_BUG_ON(ARRAY_SIZE(hdr
->len
) != ARRAY_SIZE(lens
));
593 if (fw
->size
< MOXA_FW_HDRLEN
) {
594 strcpy(rsn
, "too short (even header won't fit)");
597 if (hdr
->magic
!= cpu_to_le32(0x30343034)) {
598 sprintf(rsn
, "bad magic: %.8x", le32_to_cpu(hdr
->magic
));
601 if (hdr
->type
!= 3) {
602 sprintf(rsn
, "not for linux, type is %u", hdr
->type
);
605 if (moxa_check_fw_model(brd
, hdr
->model
)) {
606 sprintf(rsn
, "not for this card, model is %u", hdr
->model
);
610 len
= MOXA_FW_HDRLEN
;
611 lencnt
= hdr
->model
== 2 ? 5 : 3;
612 for (a
= 0; a
< ARRAY_SIZE(lens
); a
++) {
613 lens
[a
] = le16_to_cpu(hdr
->len
[a
]);
614 if (lens
[a
] && len
+ lens
[a
] <= fw
->size
&&
615 moxa_check_fw(&fw
->data
[len
]))
616 printk(KERN_WARNING
"moxa firmware: unexpected input "
617 "at offset %u, but going on\n", (u32
)len
);
618 if (!lens
[a
] && a
< lencnt
) {
619 sprintf(rsn
, "too few entries in fw file");
625 if (len
!= fw
->size
) {
626 sprintf(rsn
, "bad length: %u (should be %u)", (u32
)fw
->size
,
631 ptr
+= MOXA_FW_HDRLEN
;
634 strcpy(rsn
, "read above");
636 ret
= moxa_load_bios(brd
, ptr
, lens
[lenp
]);
640 /* we skip the tty section (lens[1]), since we don't need it */
641 ptr
+= lens
[lenp
] + lens
[lenp
+ 1];
642 lenp
+= 2; /* comm */
644 if (hdr
->model
== 2) {
645 ret
= moxa_load_320b(brd
, ptr
, lens
[lenp
]);
648 /* skip another tty */
649 ptr
+= lens
[lenp
] + lens
[lenp
+ 1];
653 ret
= moxa_load_code(brd
, ptr
, lens
[lenp
]);
659 printk(KERN_ERR
"firmware failed to load, reason: %s\n", rsn
);
663 static int moxa_init_board(struct moxa_board_conf
*brd
, struct device
*dev
)
665 const struct firmware
*fw
;
671 brd
->ports
= kcalloc(MAX_PORTS_PER_BOARD
, sizeof(*brd
->ports
),
673 if (brd
->ports
== NULL
) {
674 printk(KERN_ERR
"cannot allocate memory for ports\n");
679 for (i
= 0, p
= brd
->ports
; i
< MAX_PORTS_PER_BOARD
; i
++, p
++) {
680 p
->type
= PORT_16550A
;
681 p
->close_delay
= 5 * HZ
/ 10;
682 p
->cflag
= B9600
| CS8
| CREAD
| CLOCAL
| HUPCL
;
683 init_waitqueue_head(&p
->open_wait
);
684 init_completion(&p
->close_wait
);
686 setup_timer(&p
->emptyTimer
, moxa_check_xmit_empty
,
690 switch (brd
->boardType
) {
691 case MOXA_BOARD_C218_ISA
:
692 case MOXA_BOARD_C218_PCI
:
693 file
= "c218tunx.cod";
695 case MOXA_BOARD_CP204J
:
696 file
= "cp204unx.cod";
699 file
= "c320tunx.cod";
703 ret
= request_firmware(&fw
, file
, dev
);
705 printk(KERN_ERR
"request_firmware failed\n");
709 ret
= moxa_load_fw(brd
, fw
);
711 release_firmware(fw
);
725 static void moxa_board_deinit(struct moxa_board_conf
*brd
)
730 for (i
= 0; i
< MAX_PORTS_PER_BOARD
; i
++)
731 del_timer_sync(&brd
->ports
[i
].emptyTimer
);
733 iounmap(brd
->basemem
);
739 static int __devinit
moxa_pci_probe(struct pci_dev
*pdev
,
740 const struct pci_device_id
*ent
)
742 struct moxa_board_conf
*board
;
744 int board_type
= ent
->driver_data
;
747 retval
= pci_enable_device(pdev
);
749 dev_err(&pdev
->dev
, "can't enable pci device\n");
753 for (i
= 0; i
< MAX_BOARDS
; i
++)
754 if (moxa_boards
[i
].basemem
== NULL
)
758 if (i
>= MAX_BOARDS
) {
759 dev_warn(&pdev
->dev
, "more than %u MOXA Intellio family boards "
760 "found. Board is ignored.\n", MAX_BOARDS
);
764 board
= &moxa_boards
[i
];
766 retval
= pci_request_region(pdev
, 2, "moxa-base");
768 dev_err(&pdev
->dev
, "can't request pci region 2\n");
772 board
->basemem
= ioremap(pci_resource_start(pdev
, 2), 0x4000);
773 if (board
->basemem
== NULL
) {
774 dev_err(&pdev
->dev
, "can't remap io space 2\n");
778 board
->boardType
= board_type
;
779 switch (board_type
) {
780 case MOXA_BOARD_C218_ISA
:
781 case MOXA_BOARD_C218_PCI
:
785 case MOXA_BOARD_CP204J
:
792 board
->busType
= MOXA_BUS_TYPE_PCI
;
794 retval
= moxa_init_board(board
, &pdev
->dev
);
798 pci_set_drvdata(pdev
, board
);
802 iounmap(board
->basemem
);
803 board
->basemem
= NULL
;
805 pci_release_region(pdev
, 2);
810 static void __devexit
moxa_pci_remove(struct pci_dev
*pdev
)
812 struct moxa_board_conf
*brd
= pci_get_drvdata(pdev
);
814 moxa_board_deinit(brd
);
816 pci_release_region(pdev
, 2);
819 static struct pci_driver moxa_pci_driver
= {
821 .id_table
= moxa_pcibrds
,
822 .probe
= moxa_pci_probe
,
823 .remove
= __devexit_p(moxa_pci_remove
)
825 #endif /* CONFIG_PCI */
827 static int __init
moxa_init(void)
829 unsigned int isabrds
= 0;
832 printk(KERN_INFO
"MOXA Intellio family driver version %s\n",
834 moxaDriver
= alloc_tty_driver(MAX_PORTS
+ 1);
838 moxaDriver
->owner
= THIS_MODULE
;
839 moxaDriver
->name
= "ttyMX";
840 moxaDriver
->major
= ttymajor
;
841 moxaDriver
->minor_start
= 0;
842 moxaDriver
->type
= TTY_DRIVER_TYPE_SERIAL
;
843 moxaDriver
->subtype
= SERIAL_TYPE_NORMAL
;
844 moxaDriver
->init_termios
= tty_std_termios
;
845 moxaDriver
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| CLOCAL
| HUPCL
;
846 moxaDriver
->init_termios
.c_ispeed
= 9600;
847 moxaDriver
->init_termios
.c_ospeed
= 9600;
848 moxaDriver
->flags
= TTY_DRIVER_REAL_RAW
;
849 tty_set_operations(moxaDriver
, &moxa_ops
);
851 pr_debug("Moxa tty devices major number = %d\n", ttymajor
);
853 if (tty_register_driver(moxaDriver
)) {
854 printk(KERN_ERR
"Couldn't install MOXA Smartio family driver !\n");
855 put_tty_driver(moxaDriver
);
859 mod_timer(&moxaTimer
, jiffies
+ HZ
/ 50);
861 /* Find the boards defined from module args. */
864 struct moxa_board_conf
*brd
= moxa_boards
;
866 for (i
= 0; i
< MAX_BOARDS
; i
++) {
869 if (type
[i
] == MOXA_BOARD_C218_ISA
||
870 type
[i
] == MOXA_BOARD_C320_ISA
) {
871 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
872 isabrds
+ 1, moxa_brdname
[type
[i
] - 1],
874 brd
->boardType
= type
[i
];
875 brd
->numPorts
= type
[i
] == MOXA_BOARD_C218_ISA
? 8 :
877 brd
->busType
= MOXA_BUS_TYPE_ISA
;
878 brd
->basemem
= ioremap(baseaddr
[i
], 0x4000);
880 printk(KERN_ERR
"moxa: can't remap %lx\n",
884 if (moxa_init_board(brd
, NULL
)) {
885 iounmap(brd
->basemem
);
898 retval
= pci_register_driver(&moxa_pci_driver
);
900 printk(KERN_ERR
"Can't register moxa pci driver!\n");
909 static void __exit
moxa_exit(void)
913 del_timer_sync(&moxaTimer
);
915 if (tty_unregister_driver(moxaDriver
))
916 printk(KERN_ERR
"Couldn't unregister MOXA Intellio family "
918 put_tty_driver(moxaDriver
);
921 pci_unregister_driver(&moxa_pci_driver
);
924 for (i
= 0; i
< MAX_BOARDS
; i
++) /* ISA boards */
925 if (moxa_boards
[i
].ready
)
926 moxa_board_deinit(&moxa_boards
[i
]);
929 module_init(moxa_init
);
930 module_exit(moxa_exit
);
932 static int moxa_open(struct tty_struct
*tty
, struct file
*filp
)
934 struct moxa_board_conf
*brd
;
935 struct moxa_port
*ch
;
940 if (port
== MAX_PORTS
) {
943 brd
= &moxa_boards
[port
/ MAX_PORTS_PER_BOARD
];
947 ch
= &brd
->ports
[port
% MAX_PORTS_PER_BOARD
];
949 tty
->driver_data
= ch
;
951 if (!(ch
->asyncflags
& ASYNC_INITIALIZED
)) {
953 moxa_set_tty_param(tty
, tty
->termios
);
954 MoxaPortLineCtrl(ch
, 1, 1);
956 ch
->asyncflags
|= ASYNC_INITIALIZED
;
958 retval
= moxa_block_till_ready(tty
, filp
, ch
);
960 moxa_unthrottle(tty
);
962 if (ch
->type
== PORT_16550A
) {
971 static void moxa_close(struct tty_struct
*tty
, struct file
*filp
)
973 struct moxa_port
*ch
;
977 if (port
== MAX_PORTS
) {
980 if (tty
->driver_data
== NULL
) {
983 if (tty_hung_up_p(filp
)) {
986 ch
= (struct moxa_port
*) tty
->driver_data
;
988 if ((tty
->count
== 1) && (ch
->count
!= 1)) {
989 printk(KERN_WARNING
"moxa_close: bad serial port count; "
990 "tty->count is 1, ch->count is %d\n", ch
->count
);
993 if (--ch
->count
< 0) {
994 printk(KERN_WARNING
"moxa_close: bad serial port count, "
995 "device=%s\n", tty
->name
);
1001 ch
->asyncflags
|= ASYNC_CLOSING
;
1003 ch
->cflag
= tty
->termios
->c_cflag
;
1004 if (ch
->asyncflags
& ASYNC_INITIALIZED
) {
1005 moxa_setup_empty_event(tty
);
1006 tty_wait_until_sent(tty
, 30 * HZ
); /* 30 seconds timeout */
1007 del_timer_sync(&ch
->emptyTimer
);
1010 MoxaPortFlushData(ch
, 2);
1012 if (tty
->driver
->flush_buffer
)
1013 tty
->driver
->flush_buffer(tty
);
1014 tty_ldisc_flush(tty
);
1018 if (ch
->blocked_open
) {
1019 if (ch
->close_delay
) {
1020 msleep_interruptible(jiffies_to_msecs(ch
->close_delay
));
1022 wake_up_interruptible(&ch
->open_wait
);
1024 ch
->asyncflags
&= ~(ASYNC_NORMAL_ACTIVE
| ASYNC_CLOSING
);
1025 complete_all(&ch
->close_wait
);
1028 static int moxa_write(struct tty_struct
*tty
,
1029 const unsigned char *buf
, int count
)
1031 struct moxa_port
*ch
= tty
->driver_data
;
1032 unsigned long flags
;
1038 spin_lock_irqsave(&moxa_lock
, flags
);
1039 len
= MoxaPortWriteData(ch
, (unsigned char *) buf
, count
);
1040 spin_unlock_irqrestore(&moxa_lock
, flags
);
1042 /*********************************************
1043 if ( !(ch->statusflags & LOWWAIT) &&
1044 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
1045 ************************************************/
1046 ch
->statusflags
|= LOWWAIT
;
1050 static int moxa_write_room(struct tty_struct
*tty
)
1052 struct moxa_port
*ch
;
1056 ch
= tty
->driver_data
;
1059 return MoxaPortTxFree(ch
);
1062 static void moxa_flush_buffer(struct tty_struct
*tty
)
1064 struct moxa_port
*ch
= tty
->driver_data
;
1068 MoxaPortFlushData(ch
, 1);
1072 static int moxa_chars_in_buffer(struct tty_struct
*tty
)
1074 struct moxa_port
*ch
= tty
->driver_data
;
1078 * Sigh...I have to check if driver_data is NULL here, because
1079 * if an open() fails, the TTY subsystem eventually calls
1080 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1081 * routine. And since the open() failed, we return 0 here. TDJ
1085 chars
= MoxaPortTxQueue(ch
);
1088 * Make it possible to wakeup anything waiting for output
1089 * in tty_ioctl.c, etc.
1091 if (!(ch
->statusflags
& EMPTYWAIT
))
1092 moxa_setup_empty_event(tty
);
1097 static void moxa_flush_chars(struct tty_struct
*tty
)
1100 * Don't think I need this, because this is called to empty the TX
1101 * buffer for the 16450, 16550, etc.
1105 static void moxa_put_char(struct tty_struct
*tty
, unsigned char c
)
1107 struct moxa_port
*ch
= tty
->driver_data
;
1108 unsigned long flags
;
1112 spin_lock_irqsave(&moxa_lock
, flags
);
1113 MoxaPortWriteData(ch
, &c
, 1);
1114 spin_unlock_irqrestore(&moxa_lock
, flags
);
1115 /************************************************
1116 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
1117 *************************************************/
1118 ch
->statusflags
|= LOWWAIT
;
1121 static int moxa_tiocmget(struct tty_struct
*tty
, struct file
*file
)
1123 struct moxa_port
*ch
= tty
->driver_data
;
1124 int flag
= 0, dtr
, rts
;
1126 if ((tty
->index
!= MAX_PORTS
) && (!ch
))
1129 MoxaPortGetLineOut(ch
, &dtr
, &rts
);
1134 dtr
= MoxaPortLineStatus(ch
);
1144 static int moxa_tiocmset(struct tty_struct
*tty
, struct file
*file
,
1145 unsigned int set
, unsigned int clear
)
1147 struct moxa_port
*ch
= tty
->driver_data
;
1152 if ((port
!= MAX_PORTS
) && (!ch
))
1155 MoxaPortGetLineOut(ch
, &dtr
, &rts
);
1156 if (set
& TIOCM_RTS
)
1158 if (set
& TIOCM_DTR
)
1160 if (clear
& TIOCM_RTS
)
1162 if (clear
& TIOCM_DTR
)
1164 MoxaPortLineCtrl(ch
, dtr
, rts
);
1168 static int moxa_ioctl(struct tty_struct
*tty
, struct file
*file
,
1169 unsigned int cmd
, unsigned long arg
)
1171 struct moxa_port
*ch
= tty
->driver_data
;
1173 void __user
*argp
= (void __user
*)arg
;
1177 if ((port
!= MAX_PORTS
) && (!ch
))
1181 case TCSBRK
: /* SVID version: non-zero arg --> no break */
1182 retval
= tty_check_change(tty
);
1185 moxa_setup_empty_event(tty
);
1186 tty_wait_until_sent(tty
, 0);
1188 MoxaPortSendBreak(ch
, 0);
1190 case TCSBRKP
: /* support for POSIX tcsendbreak() */
1191 retval
= tty_check_change(tty
);
1194 moxa_setup_empty_event(tty
);
1195 tty_wait_until_sent(tty
, 0);
1196 MoxaPortSendBreak(ch
, arg
);
1199 return put_user(C_CLOCAL(tty
) ? 1 : 0, (int __user
*)argp
);
1201 if (get_user(retval
, (int __user
*)argp
))
1204 tty
->termios
->c_cflag
= ((tty
->termios
->c_cflag
& ~CLOCAL
) |
1205 (arg
? CLOCAL
: 0));
1207 ch
->asyncflags
&= ~ASYNC_CHECK_CD
;
1209 ch
->asyncflags
|= ASYNC_CHECK_CD
;
1212 return moxa_get_serial_info(ch
, argp
);
1215 return moxa_set_serial_info(ch
, argp
);
1217 retval
= MoxaDriverIoctl(tty
, cmd
, arg
);
1222 static void moxa_throttle(struct tty_struct
*tty
)
1224 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
1226 ch
->statusflags
|= THROTTLE
;
1229 static void moxa_unthrottle(struct tty_struct
*tty
)
1231 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
1233 ch
->statusflags
&= ~THROTTLE
;
1236 static void moxa_set_termios(struct tty_struct
*tty
,
1237 struct ktermios
*old_termios
)
1239 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
1243 moxa_set_tty_param(tty
, old_termios
);
1244 if (!(old_termios
->c_cflag
& CLOCAL
) &&
1245 (tty
->termios
->c_cflag
& CLOCAL
))
1246 wake_up_interruptible(&ch
->open_wait
);
1249 static void moxa_stop(struct tty_struct
*tty
)
1251 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
1255 MoxaPortTxDisable(ch
);
1256 ch
->statusflags
|= TXSTOPPED
;
1260 static void moxa_start(struct tty_struct
*tty
)
1262 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
1267 if (!(ch
->statusflags
& TXSTOPPED
))
1270 MoxaPortTxEnable(ch
);
1271 ch
->statusflags
&= ~TXSTOPPED
;
1274 static void moxa_hangup(struct tty_struct
*tty
)
1276 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
1278 moxa_flush_buffer(tty
);
1281 ch
->asyncflags
&= ~ASYNC_NORMAL_ACTIVE
;
1283 wake_up_interruptible(&ch
->open_wait
);
1286 static void moxa_poll(unsigned long ignored
)
1289 struct moxa_port
*ch
;
1290 struct tty_struct
*tp
;
1293 del_timer(&moxaTimer
);
1295 if (MoxaDriverPoll() < 0) {
1296 mod_timer(&moxaTimer
, jiffies
+ HZ
/ 50);
1299 for (card
= 0; card
< MAX_BOARDS
; card
++) {
1300 if ((ports
= MoxaPortsOfCard(card
)) <= 0)
1302 ch
= moxa_boards
[card
].ports
;
1303 for (i
= 0; i
< ports
; i
++, ch
++) {
1304 if ((ch
->asyncflags
& ASYNC_INITIALIZED
) == 0)
1306 if (!(ch
->statusflags
& THROTTLE
) &&
1307 (MoxaPortRxQueue(ch
) > 0))
1308 moxa_receive_data(ch
);
1309 if ((tp
= ch
->tty
) == 0)
1311 if (ch
->statusflags
& LOWWAIT
) {
1312 if (MoxaPortTxQueue(ch
) <= WAKEUP_CHARS
) {
1314 ch
->statusflags
&= ~LOWWAIT
;
1319 if (!I_IGNBRK(tp
) && (MoxaPortResetBrkCnt(ch
) > 0)) {
1320 tty_insert_flip_char(tp
, 0, TTY_BREAK
);
1321 tty_schedule_flip(tp
);
1323 if (MoxaPortDCDChange(ch
)) {
1324 if (ch
->asyncflags
& ASYNC_CHECK_CD
) {
1325 if (MoxaPortDCDON(ch
))
1326 wake_up_interruptible(&ch
->open_wait
);
1329 wake_up_interruptible(&ch
->open_wait
);
1330 ch
->asyncflags
&= ~ASYNC_NORMAL_ACTIVE
;
1337 mod_timer(&moxaTimer
, jiffies
+ HZ
/ 50);
1340 /******************************************************************************/
1342 static void moxa_set_tty_param(struct tty_struct
*tty
, struct ktermios
*old_termios
)
1344 register struct ktermios
*ts
;
1345 struct moxa_port
*ch
;
1346 int rts
, cts
, txflow
, rxflow
, xany
, baud
;
1348 ch
= (struct moxa_port
*) tty
->driver_data
;
1350 if (ts
->c_cflag
& CLOCAL
)
1351 ch
->asyncflags
&= ~ASYNC_CHECK_CD
;
1353 ch
->asyncflags
|= ASYNC_CHECK_CD
;
1354 rts
= cts
= txflow
= rxflow
= xany
= 0;
1355 if (ts
->c_cflag
& CRTSCTS
)
1357 if (ts
->c_iflag
& IXON
)
1359 if (ts
->c_iflag
& IXOFF
)
1361 if (ts
->c_iflag
& IXANY
)
1364 /* Clear the features we don't support */
1365 ts
->c_cflag
&= ~CMSPAR
;
1366 MoxaPortFlowCtrl(ch
, rts
, cts
, txflow
, rxflow
, xany
);
1367 baud
= MoxaPortSetTermio(ch
, ts
, tty_get_baud_rate(tty
));
1369 baud
= tty_termios_baud_rate(old_termios
);
1370 /* Not put the baud rate into the termios data */
1371 tty_encode_baud_rate(tty
, baud
, baud
);
1374 static int moxa_block_till_ready(struct tty_struct
*tty
, struct file
*filp
,
1375 struct moxa_port
*ch
)
1377 DECLARE_WAITQUEUE(wait
,current
);
1378 unsigned long flags
;
1380 int do_clocal
= C_CLOCAL(tty
);
1383 * If the device is in the middle of being closed, then block
1384 * until it's done, and then try again.
1386 if (tty_hung_up_p(filp
) || (ch
->asyncflags
& ASYNC_CLOSING
)) {
1387 if (ch
->asyncflags
& ASYNC_CLOSING
)
1388 wait_for_completion_interruptible(&ch
->close_wait
);
1389 #ifdef SERIAL_DO_RESTART
1390 if (ch
->asyncflags
& ASYNC_HUP_NOTIFY
)
1393 return (-ERESTARTSYS
);
1399 * If non-blocking mode is set, then make the check up front
1402 if (filp
->f_flags
& O_NONBLOCK
) {
1403 ch
->asyncflags
|= ASYNC_NORMAL_ACTIVE
;
1407 * Block waiting for the carrier detect and the line to become free
1410 add_wait_queue(&ch
->open_wait
, &wait
);
1411 pr_debug("block_til_ready before block: ttys%d, count = %d\n",
1412 tty
->index
, ch
->count
);
1413 spin_lock_irqsave(&moxa_lock
, flags
);
1414 if (!tty_hung_up_p(filp
))
1417 spin_unlock_irqrestore(&moxa_lock
, flags
);
1420 set_current_state(TASK_INTERRUPTIBLE
);
1421 if (tty_hung_up_p(filp
) ||
1422 !(ch
->asyncflags
& ASYNC_INITIALIZED
)) {
1423 #ifdef SERIAL_DO_RESTART
1424 if (ch
->asyncflags
& ASYNC_HUP_NOTIFY
)
1427 retval
= -ERESTARTSYS
;
1433 if (!(ch
->asyncflags
& ASYNC_CLOSING
) && (do_clocal
||
1437 if (signal_pending(current
)) {
1438 retval
= -ERESTARTSYS
;
1443 set_current_state(TASK_RUNNING
);
1444 remove_wait_queue(&ch
->open_wait
, &wait
);
1446 spin_lock_irqsave(&moxa_lock
, flags
);
1447 if (!tty_hung_up_p(filp
))
1450 spin_unlock_irqrestore(&moxa_lock
, flags
);
1451 pr_debug("block_til_ready after blocking: ttys%d, count = %d\n",
1452 tty
->index
, ch
->count
);
1455 /* FIXME: review to see if we need to use set_bit on these */
1456 ch
->asyncflags
|= ASYNC_NORMAL_ACTIVE
;
1460 static void moxa_setup_empty_event(struct tty_struct
*tty
)
1462 struct moxa_port
*ch
= tty
->driver_data
;
1463 unsigned long flags
;
1465 spin_lock_irqsave(&moxa_lock
, flags
);
1466 ch
->statusflags
|= EMPTYWAIT
;
1467 mod_timer(&ch
->emptyTimer
, jiffies
+ HZ
);
1468 spin_unlock_irqrestore(&moxa_lock
, flags
);
1471 static void moxa_check_xmit_empty(unsigned long data
)
1473 struct moxa_port
*ch
;
1475 ch
= (struct moxa_port
*) data
;
1476 if (ch
->tty
&& (ch
->statusflags
& EMPTYWAIT
)) {
1477 if (MoxaPortTxQueue(ch
) == 0) {
1478 ch
->statusflags
&= ~EMPTYWAIT
;
1479 tty_wakeup(ch
->tty
);
1482 mod_timer(&ch
->emptyTimer
, round_jiffies(jiffies
+ HZ
));
1484 ch
->statusflags
&= ~EMPTYWAIT
;
1487 static void moxa_shut_down(struct moxa_port
*ch
)
1489 struct tty_struct
*tp
;
1491 if (!(ch
->asyncflags
& ASYNC_INITIALIZED
))
1496 MoxaPortDisable(ch
);
1499 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1501 if (tp
->termios
->c_cflag
& HUPCL
)
1502 MoxaPortLineCtrl(ch
, 0, 0);
1504 ch
->asyncflags
&= ~ASYNC_INITIALIZED
;
1507 static void moxa_receive_data(struct moxa_port
*ch
)
1509 struct tty_struct
*tp
;
1510 struct ktermios
*ts
;
1511 unsigned long flags
;
1517 /**************************************************
1518 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1519 *****************************************************/
1521 MoxaPortFlushData(ch
, 0);
1524 spin_lock_irqsave(&moxa_lock
, flags
);
1525 MoxaPortReadData(ch
, tp
);
1526 spin_unlock_irqrestore(&moxa_lock
, flags
);
1527 tty_schedule_flip(tp
);
1536 int rxcnt
[MAX_PORTS
];
1537 int txcnt
[MAX_PORTS
];
1540 #define DCD_changed 0x01
1541 #define DCD_oldstate 0x80
1543 static int moxaLowWaterChk
;
1544 static struct mon_str moxaLog
;
1545 static int moxaFuncTout
= HZ
/ 2;
1547 static void moxafunc(void __iomem
*, int, ushort
);
1548 static void moxa_wait_finish(void __iomem
*);
1549 static void moxa_low_water_check(void __iomem
*);
1551 /*****************************************************************************
1552 * Driver level functions: *
1553 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1554 * 3. MoxaDriverPoll(void); *
1555 *****************************************************************************/
1557 #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1558 #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
1559 #define MOXA_GETDATACOUNT (MOXA + 23)
1560 #define MOXA_GET_IOQUEUE (MOXA + 27)
1561 #define MOXA_FLUSH_QUEUE (MOXA + 28)
1562 #define MOXA_GET_CONF (MOXA + 35) /* configuration */
1563 #define MOXA_GET_MAJOR (MOXA + 63)
1564 #define MOXA_GET_CUMAJOR (MOXA + 64)
1565 #define MOXA_GETMSTATUS (MOXA + 65)
1567 static void MoxaPortFlushData(struct moxa_port
*port
, int mode
)
1569 void __iomem
*ofsAddr
;
1570 if ((mode
< 0) || (mode
> 2))
1572 ofsAddr
= port
->tableAddr
;
1573 moxafunc(ofsAddr
, FC_FlushQueue
, mode
);
1575 port
->lowChkFlag
= 0;
1576 moxa_low_water_check(ofsAddr
);
1580 static int MoxaDriverIoctl(struct tty_struct
*tty
, unsigned int cmd
,
1583 struct moxa_port
*port
= tty
->driver_data
;
1586 void __user
*argp
= (void __user
*)arg
;
1588 if (tty
->index
== MAX_PORTS
) {
1589 if ((cmd
!= MOXA_GET_CONF
) && (cmd
!= MOXA_GETDATACOUNT
) &&
1590 (cmd
!= MOXA_GET_IOQUEUE
) && (cmd
!= MOXA_GET_MAJOR
) &&
1591 (cmd
!= MOXA_GET_CUMAJOR
) && (cmd
!= MOXA_GETMSTATUS
))
1595 case MOXA_GETDATACOUNT
:
1596 moxaLog
.tick
= jiffies
;
1597 if(copy_to_user(argp
, &moxaLog
, sizeof(struct mon_str
)))
1600 case MOXA_FLUSH_QUEUE
:
1601 MoxaPortFlushData(port
, arg
);
1603 case MOXA_GET_IOQUEUE
: {
1604 struct moxaq_str __user
*argm
= argp
;
1605 struct moxaq_str tmp
;
1606 struct moxa_port
*p
;
1609 for (i
= 0; i
< MAX_BOARDS
; i
++) {
1610 p
= moxa_boards
[i
].ports
;
1611 for (j
= 0; j
< MAX_PORTS_PER_BOARD
; j
++, p
++, argm
++) {
1612 memset(&tmp
, 0, sizeof(tmp
));
1613 if (moxa_boards
[i
].ready
) {
1614 tmp
.inq
= MoxaPortRxQueue(p
);
1615 tmp
.outq
= MoxaPortTxQueue(p
);
1617 if (copy_to_user(argm
, &tmp
, sizeof(tmp
)))
1622 } case MOXA_GET_OQUEUE
:
1623 i
= MoxaPortTxQueue(port
);
1624 return put_user(i
, (unsigned long __user
*)argp
);
1625 case MOXA_GET_IQUEUE
:
1626 i
= MoxaPortRxQueue(port
);
1627 return put_user(i
, (unsigned long __user
*)argp
);
1628 case MOXA_GET_MAJOR
:
1629 if(copy_to_user(argp
, &ttymajor
, sizeof(int)))
1632 case MOXA_GET_CUMAJOR
:
1634 if(copy_to_user(argp
, &i
, sizeof(int)))
1637 case MOXA_GETMSTATUS
: {
1638 struct mxser_mstatus __user
*argm
= argp
;
1639 struct mxser_mstatus tmp
;
1640 struct moxa_port
*p
;
1643 for (i
= 0; i
< MAX_BOARDS
; i
++) {
1644 p
= moxa_boards
[i
].ports
;
1645 for (j
= 0; j
< MAX_PORTS_PER_BOARD
; j
++, p
++, argm
++) {
1646 memset(&tmp
, 0, sizeof(tmp
));
1647 if (!moxa_boards
[i
].ready
)
1650 status
= MoxaPortLineStatus(p
);
1658 if (!p
->tty
|| !p
->tty
->termios
)
1659 tmp
.cflag
= p
->cflag
;
1661 tmp
.cflag
= p
->tty
->termios
->c_cflag
;
1663 if (copy_to_user(argm
, &tmp
, sizeof(tmp
)))
1671 return -ENOIOCTLCMD
;
1674 int MoxaDriverPoll(void)
1676 struct moxa_board_conf
*brd
;
1677 struct moxa_port
*p
;
1678 register ushort temp
;
1680 void __iomem
*ofsAddr
;
1684 for (card
= 0; card
< MAX_BOARDS
; card
++) {
1685 brd
= &moxa_boards
[card
];
1686 if (brd
->ready
== 0)
1688 if ((ports
= brd
->numPorts
) == 0)
1690 if (readb(brd
->intPend
) == 0xff) {
1691 ip
= brd
->intTable
+ readb(brd
->intNdx
);
1694 for (port
= 0; port
< ports
; port
+= 2, p
++) {
1695 temp
= readw(ip
+ port
);
1699 writew(0, ip
+ port
);
1700 ofsAddr
= p
->tableAddr
;
1702 writew(readw(ofsAddr
+ HostStat
) &
1703 ~WakeupTx
, ofsAddr
+ HostStat
);
1704 if (temp
& IntrBreak
)
1707 if (temp
& IntrLine
) {
1708 if (readb(ofsAddr
+ FlagStat
) & DCD_state
) {
1709 if ((p
->DCDState
& DCD_oldstate
) == 0)
1710 p
->DCDState
= (DCD_oldstate
|
1713 if (p
->DCDState
& DCD_oldstate
)
1714 p
->DCDState
= DCD_changed
;
1718 writeb(0, brd
->intPend
);
1720 if (moxaLowWaterChk
) {
1722 for (port
= 0; port
< ports
; port
++, p
++) {
1723 if (p
->lowChkFlag
) {
1725 ofsAddr
= p
->tableAddr
;
1726 moxa_low_water_check(ofsAddr
);
1731 moxaLowWaterChk
= 0;
1735 /*****************************************************************************
1736 * Card level function: *
1737 * 1. MoxaPortsOfCard(int cardno); *
1738 *****************************************************************************/
1739 int MoxaPortsOfCard(int cardno
)
1742 if (moxa_boards
[cardno
].boardType
== 0)
1744 return (moxa_boards
[cardno
].numPorts
);
1747 /*****************************************************************************
1748 * Port level functions: *
1749 * 2. MoxaPortEnable(int port); *
1750 * 3. MoxaPortDisable(int port); *
1751 * 4. MoxaPortGetMaxBaud(int port); *
1752 * 6. MoxaPortSetBaud(int port, long baud); *
1753 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1754 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1755 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1756 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1757 * 12. MoxaPortLineStatus(int port); *
1758 * 13. MoxaPortDCDChange(int port); *
1759 * 14. MoxaPortDCDON(int port); *
1760 * 15. MoxaPortFlushData(int port, int mode); *
1761 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1762 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1763 * 20. MoxaPortTxQueue(int port); *
1764 * 21. MoxaPortTxFree(int port); *
1765 * 22. MoxaPortRxQueue(int port); *
1766 * 24. MoxaPortTxDisable(int port); *
1767 * 25. MoxaPortTxEnable(int port); *
1768 * 27. MoxaPortResetBrkCnt(int port); *
1769 * 30. MoxaPortSendBreak(int port, int ticks); *
1770 *****************************************************************************/
1772 * Moxa Port Number Description:
1774 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1775 * the port number using in MOXA driver functions will be 0 to 31 for
1776 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1777 * to 127 for fourth. For example, if you setup three MOXA boards,
1778 * first board is C218, second board is C320-16 and third board is
1779 * C320-32. The port number of first board (C218 - 8 ports) is from
1780 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1781 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1782 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1783 * 127 will be invalid.
1786 * Moxa Functions Description:
1788 * Function 1: Driver initialization routine, this routine must be
1789 * called when initialized driver.
1791 * void MoxaDriverInit();
1794 * Function 2: Moxa driver private IOCTL command processing.
1796 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1798 * unsigned int cmd : IOCTL command
1799 * unsigned long arg : IOCTL argument
1800 * int port : port number (0 - 127)
1807 * Function 3: Moxa driver polling process routine.
1809 * int MoxaDriverPoll(void);
1811 * return: 0 ; polling O.K.
1812 * -1 : no any Moxa card.
1815 * Function 4: Get the ports of this card.
1817 * int MoxaPortsOfCard(int cardno);
1819 * int cardno : card number (0 - 3)
1821 * return: 0 : this card is invalid
1825 * Function 6: Enable this port to start Tx/Rx data.
1827 * void MoxaPortEnable(int port);
1828 * int port : port number (0 - 127)
1831 * Function 7: Disable this port
1833 * void MoxaPortDisable(int port);
1834 * int port : port number (0 - 127)
1837 * Function 8: Get the maximun available baud rate of this port.
1839 * long MoxaPortGetMaxBaud(int port);
1840 * int port : port number (0 - 127)
1842 * return: 0 : this port is invalid
1843 * 38400/57600/115200 bps
1846 * Function 10: Setting baud rate of this port.
1848 * long MoxaPortSetBaud(int port, long baud);
1849 * int port : port number (0 - 127)
1850 * long baud : baud rate (50 - 115200)
1852 * return: 0 : this port is invalid or baud < 50
1853 * 50 - 115200 : the real baud rate set to the port, if
1854 * the argument baud is large than maximun
1855 * available baud rate, the real setting
1856 * baud rate will be the maximun baud rate.
1859 * Function 12: Configure the port.
1861 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1862 * int port : port number (0 - 127)
1863 * struct ktermios * termio : termio structure pointer
1864 * speed_t baud : baud rate
1866 * return: -1 : this port is invalid or termio == NULL
1870 * Function 13: Get the DTR/RTS state of this port.
1872 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1873 * int port : port number (0 - 127)
1874 * int * dtrState : pointer to INT to receive the current DTR
1875 * state. (if NULL, this function will not
1876 * write to this address)
1877 * int * rtsState : pointer to INT to receive the current RTS
1878 * state. (if NULL, this function will not
1879 * write to this address)
1881 * return: -1 : this port is invalid
1885 * Function 14: Setting the DTR/RTS output state of this port.
1887 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1888 * int port : port number (0 - 127)
1889 * int dtrState : DTR output state (0: off, 1: on)
1890 * int rtsState : RTS output state (0: off, 1: on)
1893 * Function 15: Setting the flow control of this port.
1895 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1896 * int txFlow,int xany);
1897 * int port : port number (0 - 127)
1898 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1899 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1900 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1901 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1902 * int xany : S/W XANY flow control (0: no, 1: yes)
1905 * Function 16: Get ths line status of this port
1907 * int MoxaPortLineStatus(int port);
1908 * int port : port number (0 - 127)
1910 * return: Bit 0 - CTS state (0: off, 1: on)
1911 * Bit 1 - DSR state (0: off, 1: on)
1912 * Bit 2 - DCD state (0: off, 1: on)
1915 * Function 17: Check the DCD state has changed since the last read
1918 * int MoxaPortDCDChange(int port);
1919 * int port : port number (0 - 127)
1921 * return: 0 : no changed
1922 * 1 : DCD has changed
1925 * Function 18: Check ths current DCD state is ON or not.
1927 * int MoxaPortDCDON(int port);
1928 * int port : port number (0 - 127)
1930 * return: 0 : DCD off
1934 * Function 19: Flush the Rx/Tx buffer data of this port.
1936 * void MoxaPortFlushData(int port, int mode);
1937 * int port : port number (0 - 127)
1939 * 0 : flush the Rx buffer
1940 * 1 : flush the Tx buffer
1941 * 2 : flush the Rx and Tx buffer
1944 * Function 20: Write data.
1946 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1947 * int port : port number (0 - 127)
1948 * unsigned char * buffer : pointer to write data buffer.
1949 * int length : write data length
1951 * return: 0 - length : real write data length
1954 * Function 21: Read data.
1956 * int MoxaPortReadData(int port, struct tty_struct *tty);
1957 * int port : port number (0 - 127)
1958 * struct tty_struct *tty : tty for data
1960 * return: 0 - length : real read data length
1963 * Function 24: Get the Tx buffer current queued data bytes
1965 * int MoxaPortTxQueue(int port);
1966 * int port : port number (0 - 127)
1968 * return: .. : Tx buffer current queued data bytes
1971 * Function 25: Get the Tx buffer current free space
1973 * int MoxaPortTxFree(int port);
1974 * int port : port number (0 - 127)
1976 * return: .. : Tx buffer current free space
1979 * Function 26: Get the Rx buffer current queued data bytes
1981 * int MoxaPortRxQueue(int port);
1982 * int port : port number (0 - 127)
1984 * return: .. : Rx buffer current queued data bytes
1987 * Function 28: Disable port data transmission.
1989 * void MoxaPortTxDisable(int port);
1990 * int port : port number (0 - 127)
1993 * Function 29: Enable port data transmission.
1995 * void MoxaPortTxEnable(int port);
1996 * int port : port number (0 - 127)
1999 * Function 31: Get the received BREAK signal count and reset it.
2001 * int MoxaPortResetBrkCnt(int port);
2002 * int port : port number (0 - 127)
2004 * return: 0 - .. : BREAK signal count
2007 * Function 34: Send out a BREAK signal.
2009 * void MoxaPortSendBreak(int port, int ms100);
2010 * int port : port number (0 - 127)
2011 * int ms100 : break signal time interval.
2012 * unit: 100 mini-second. if ms100 == 0, it will
2013 * send out a about 250 ms BREAK signal.
2017 static void MoxaPortEnable(struct moxa_port
*port
)
2019 void __iomem
*ofsAddr
;
2020 short lowwater
= 512;
2022 ofsAddr
= port
->tableAddr
;
2023 writew(lowwater
, ofsAddr
+ Low_water
);
2025 if (port
->board
->boardType
== MOXA_BOARD_C320_ISA
||
2026 port
->board
->boardType
== MOXA_BOARD_C320_PCI
) {
2027 moxafunc(ofsAddr
, FC_SetBreakIrq
, 0);
2029 writew(readw(ofsAddr
+ HostStat
) | WakeupBreak
, ofsAddr
+ HostStat
);
2032 moxafunc(ofsAddr
, FC_SetLineIrq
, Magic_code
);
2033 moxafunc(ofsAddr
, FC_FlushQueue
, 2);
2035 moxafunc(ofsAddr
, FC_EnableCH
, Magic_code
);
2036 MoxaPortLineStatus(port
);
2039 static void MoxaPortDisable(struct moxa_port
*port
)
2041 void __iomem
*ofsAddr
= port
->tableAddr
;
2043 moxafunc(ofsAddr
, FC_SetFlowCtl
, 0); /* disable flow control */
2044 moxafunc(ofsAddr
, FC_ClrLineIrq
, Magic_code
);
2045 writew(0, ofsAddr
+ HostStat
);
2046 moxafunc(ofsAddr
, FC_DisableCH
, Magic_code
);
2049 static long MoxaPortGetMaxBaud(struct moxa_port
*port
)
2051 if (port
->board
->boardType
== MOXA_BOARD_C320_ISA
||
2052 port
->board
->boardType
== MOXA_BOARD_C320_PCI
)
2059 static long MoxaPortSetBaud(struct moxa_port
*port
, long baud
)
2061 void __iomem
*ofsAddr
;
2065 if ((baud
< 50L) || ((max
= MoxaPortGetMaxBaud(port
)) == 0))
2067 ofsAddr
= port
->tableAddr
;
2071 clock
= 614400L; /* for 9.8304 Mhz : max. 38400 bps */
2072 else if (max
== 57600L)
2073 clock
= 691200L; /* for 11.0592 Mhz : max. 57600 bps */
2075 clock
= 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2077 moxafunc(ofsAddr
, FC_SetBaud
, val
);
2082 static int MoxaPortSetTermio(struct moxa_port
*port
, struct ktermios
*termio
,
2085 void __iomem
*ofsAddr
;
2089 ofsAddr
= port
->tableAddr
;
2090 cflag
= termio
->c_cflag
; /* termio->c_cflag */
2092 mode
= termio
->c_cflag
& CSIZE
;
2095 else if (mode
== CS6
)
2097 else if (mode
== CS7
)
2099 else if (mode
== CS8
)
2102 if (termio
->c_cflag
& CSTOPB
) {
2110 if (termio
->c_cflag
& PARENB
) {
2111 if (termio
->c_cflag
& PARODD
)
2118 moxafunc(ofsAddr
, FC_SetDataMode
, (ushort
) mode
);
2120 if (port
->board
->boardType
== MOXA_BOARD_C320_ISA
||
2121 port
->board
->boardType
== MOXA_BOARD_C320_PCI
) {
2122 if (baud
>= 921600L)
2125 baud
= MoxaPortSetBaud(port
, baud
);
2127 if (termio
->c_iflag
& (IXON
| IXOFF
| IXANY
)) {
2128 writeb(termio
->c_cc
[VSTART
], ofsAddr
+ FuncArg
);
2129 writeb(termio
->c_cc
[VSTOP
], ofsAddr
+ FuncArg1
);
2130 writeb(FC_SetXonXoff
, ofsAddr
+ FuncCode
);
2131 moxa_wait_finish(ofsAddr
);
2137 static int MoxaPortGetLineOut(struct moxa_port
*port
, int *dtrState
,
2142 *dtrState
= !!(port
->lineCtrl
& DTR_ON
);
2144 *rtsState
= !!(port
->lineCtrl
& RTS_ON
);
2149 static void MoxaPortLineCtrl(struct moxa_port
*port
, int dtr
, int rts
)
2157 port
->lineCtrl
= mode
;
2158 moxafunc(port
->tableAddr
, FC_LineControl
, mode
);
2161 static void MoxaPortFlowCtrl(struct moxa_port
*port
, int rts
, int cts
,
2162 int txflow
, int rxflow
, int txany
)
2167 mode
|= RTS_FlowCtl
;
2169 mode
|= CTS_FlowCtl
;
2176 moxafunc(port
->tableAddr
, FC_SetFlowCtl
, mode
);
2179 static int MoxaPortLineStatus(struct moxa_port
*port
)
2181 void __iomem
*ofsAddr
;
2184 ofsAddr
= port
->tableAddr
;
2185 if (port
->board
->boardType
== MOXA_BOARD_C320_ISA
||
2186 port
->board
->boardType
== MOXA_BOARD_C320_PCI
) {
2187 moxafunc(ofsAddr
, FC_LineStatus
, 0);
2188 val
= readw(ofsAddr
+ FuncArg
);
2190 val
= readw(ofsAddr
+ FlagStat
) >> 4;
2195 if ((port
->DCDState
& DCD_oldstate
) == 0)
2196 port
->DCDState
= (DCD_oldstate
| DCD_changed
);
2198 if (port
->DCDState
& DCD_oldstate
)
2199 port
->DCDState
= DCD_changed
;
2205 static int MoxaPortDCDChange(struct moxa_port
*port
)
2210 port
->DCDState
&= ~DCD_changed
;
2215 static int MoxaPortDCDON(struct moxa_port
*port
)
2219 if (port
->DCDState
& DCD_oldstate
)
2226 static int MoxaPortWriteData(struct moxa_port
*port
, unsigned char *buffer
,
2232 ushort head
, tx_mask
, spage
, epage
;
2233 ushort pageno
, pageofs
, bufhead
;
2234 void __iomem
*baseAddr
, *ofsAddr
, *ofs
;
2236 ofsAddr
= port
->tableAddr
;
2237 baseAddr
= port
->board
->basemem
;
2238 tx_mask
= readw(ofsAddr
+ TX_mask
);
2239 spage
= readw(ofsAddr
+ Page_txb
);
2240 epage
= readw(ofsAddr
+ EndPage_txb
);
2241 tail
= readw(ofsAddr
+ TXwptr
);
2242 head
= readw(ofsAddr
+ TXrptr
);
2243 c
= (head
> tail
) ? (head
- tail
- 1)
2244 : (head
- tail
+ tx_mask
);
2247 moxaLog
.txcnt
[port
->tty
->index
] += c
;
2249 if (spage
== epage
) {
2250 bufhead
= readw(ofsAddr
+ Ofs_txb
);
2251 writew(spage
, baseAddr
+ Control_reg
);
2254 len
= head
- tail
- 1;
2256 len
= tx_mask
+ 1 - tail
;
2257 len
= (c
> len
) ? len
: c
;
2258 ofs
= baseAddr
+ DynPage_addr
+ bufhead
+ tail
;
2259 for (i
= 0; i
< len
; i
++)
2260 writeb(*buffer
++, ofs
+ i
);
2261 tail
= (tail
+ len
) & tx_mask
;
2264 writew(tail
, ofsAddr
+ TXwptr
);
2267 pageno
= spage
+ (tail
>> 13);
2268 pageofs
= tail
& Page_mask
;
2270 cnt
= Page_size
- pageofs
;
2274 writeb(pageno
, baseAddr
+ Control_reg
);
2275 ofs
= baseAddr
+ DynPage_addr
+ pageofs
;
2276 for (i
= 0; i
< cnt
; i
++)
2277 writeb(*buffer
++, ofs
+ i
);
2279 writew((tail
+ len
) & tx_mask
, ofsAddr
+ TXwptr
);
2282 if (++pageno
== epage
)
2287 writeb(1, ofsAddr
+ CD180TXirq
); /* start to send */
2291 static int MoxaPortReadData(struct moxa_port
*port
, struct tty_struct
*tty
)
2293 register ushort head
, pageofs
;
2294 int i
, count
, cnt
, len
, total
, remain
;
2295 ushort tail
, rx_mask
, spage
, epage
;
2296 ushort pageno
, bufhead
;
2297 void __iomem
*baseAddr
, *ofsAddr
, *ofs
;
2299 ofsAddr
= port
->tableAddr
;
2300 baseAddr
= port
->board
->basemem
;
2301 head
= readw(ofsAddr
+ RXrptr
);
2302 tail
= readw(ofsAddr
+ RXwptr
);
2303 rx_mask
= readw(ofsAddr
+ RX_mask
);
2304 spage
= readw(ofsAddr
+ Page_rxb
);
2305 epage
= readw(ofsAddr
+ EndPage_rxb
);
2306 count
= (tail
>= head
) ? (tail
- head
)
2307 : (tail
- head
+ rx_mask
+ 1);
2312 remain
= count
- total
;
2313 moxaLog
.rxcnt
[port
->tty
->index
] += total
;
2315 if (spage
== epage
) {
2316 bufhead
= readw(ofsAddr
+ Ofs_rxb
);
2317 writew(spage
, baseAddr
+ Control_reg
);
2322 len
= rx_mask
+ 1 - head
;
2323 len
= (count
> len
) ? len
: count
;
2324 ofs
= baseAddr
+ DynPage_addr
+ bufhead
+ head
;
2325 for (i
= 0; i
< len
; i
++)
2326 tty_insert_flip_char(tty
, readb(ofs
+ i
), TTY_NORMAL
);
2327 head
= (head
+ len
) & rx_mask
;
2330 writew(head
, ofsAddr
+ RXrptr
);
2333 pageno
= spage
+ (head
>> 13);
2334 pageofs
= head
& Page_mask
;
2336 cnt
= Page_size
- pageofs
;
2340 writew(pageno
, baseAddr
+ Control_reg
);
2341 ofs
= baseAddr
+ DynPage_addr
+ pageofs
;
2342 for (i
= 0; i
< cnt
; i
++)
2343 tty_insert_flip_char(tty
, readb(ofs
+ i
), TTY_NORMAL
);
2345 writew((head
+ len
) & rx_mask
, ofsAddr
+ RXrptr
);
2348 if (++pageno
== epage
)
2353 if ((readb(ofsAddr
+ FlagStat
) & Xoff_state
) && (remain
< LowWater
)) {
2354 moxaLowWaterChk
= 1;
2355 port
->lowChkFlag
= 1;
2361 static int MoxaPortTxQueue(struct moxa_port
*port
)
2363 void __iomem
*ofsAddr
= port
->tableAddr
;
2364 ushort rptr
, wptr
, mask
;
2367 rptr
= readw(ofsAddr
+ TXrptr
);
2368 wptr
= readw(ofsAddr
+ TXwptr
);
2369 mask
= readw(ofsAddr
+ TX_mask
);
2370 len
= (wptr
- rptr
) & mask
;
2374 static int MoxaPortTxFree(struct moxa_port
*port
)
2376 void __iomem
*ofsAddr
= port
->tableAddr
;
2377 ushort rptr
, wptr
, mask
;
2380 rptr
= readw(ofsAddr
+ TXrptr
);
2381 wptr
= readw(ofsAddr
+ TXwptr
);
2382 mask
= readw(ofsAddr
+ TX_mask
);
2383 len
= mask
- ((wptr
- rptr
) & mask
);
2387 static int MoxaPortRxQueue(struct moxa_port
*port
)
2389 void __iomem
*ofsAddr
= port
->tableAddr
;
2390 ushort rptr
, wptr
, mask
;
2393 rptr
= readw(ofsAddr
+ RXrptr
);
2394 wptr
= readw(ofsAddr
+ RXwptr
);
2395 mask
= readw(ofsAddr
+ RX_mask
);
2396 len
= (wptr
- rptr
) & mask
;
2401 static void MoxaPortTxDisable(struct moxa_port
*port
)
2403 moxafunc(port
->tableAddr
, FC_SetXoffState
, Magic_code
);
2406 static void MoxaPortTxEnable(struct moxa_port
*port
)
2408 moxafunc(port
->tableAddr
, FC_SetXonState
, Magic_code
);
2412 static int MoxaPortResetBrkCnt(struct moxa_port
*port
)
2415 cnt
= port
->breakCnt
;
2421 static void MoxaPortSendBreak(struct moxa_port
*port
, int ms100
)
2423 void __iomem
*ofsAddr
= port
->tableAddr
;
2426 moxafunc(ofsAddr
, FC_SendBreak
, Magic_code
);
2429 moxafunc(ofsAddr
, FC_SendBreak
, Magic_code
);
2432 moxafunc(ofsAddr
, FC_StopBreak
, Magic_code
);
2435 static int moxa_get_serial_info(struct moxa_port
*info
,
2436 struct serial_struct __user
*retinfo
)
2438 struct serial_struct tmp
;
2440 memset(&tmp
, 0, sizeof(tmp
));
2441 tmp
.type
= info
->type
;
2442 tmp
.line
= info
->tty
->index
;
2445 tmp
.flags
= info
->asyncflags
;
2446 tmp
.baud_base
= 921600;
2447 tmp
.close_delay
= info
->close_delay
;
2448 tmp
.custom_divisor
= 0;
2450 if(copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
2456 static int moxa_set_serial_info(struct moxa_port
*info
,
2457 struct serial_struct __user
*new_info
)
2459 struct serial_struct new_serial
;
2461 if(copy_from_user(&new_serial
, new_info
, sizeof(new_serial
)))
2464 if ((new_serial
.irq
!= 0) ||
2465 (new_serial
.port
!= 0) ||
2466 // (new_serial.type != info->type) ||
2467 (new_serial
.custom_divisor
!= 0) ||
2468 (new_serial
.baud_base
!= 921600))
2471 if (!capable(CAP_SYS_ADMIN
)) {
2472 if (((new_serial
.flags
& ~ASYNC_USR_MASK
) !=
2473 (info
->asyncflags
& ~ASYNC_USR_MASK
)))
2476 info
->close_delay
= new_serial
.close_delay
* HZ
/ 100;
2479 new_serial
.flags
= (new_serial
.flags
& ~ASYNC_FLAGS
);
2480 new_serial
.flags
|= (info
->asyncflags
& ASYNC_FLAGS
);
2482 if (new_serial
.type
== PORT_16550A
) {
2483 MoxaSetFifo(info
, 1);
2485 MoxaSetFifo(info
, 0);
2488 info
->type
= new_serial
.type
;
2494 /*****************************************************************************
2495 * Static local functions: *
2496 *****************************************************************************/
2497 static void moxafunc(void __iomem
*ofsAddr
, int cmd
, ushort arg
)
2500 writew(arg
, ofsAddr
+ FuncArg
);
2501 writew(cmd
, ofsAddr
+ FuncCode
);
2502 moxa_wait_finish(ofsAddr
);
2505 static void moxa_wait_finish(void __iomem
*ofsAddr
)
2510 while (readw(ofsAddr
+ FuncCode
) != 0) {
2512 if ((j
- i
) > moxaFuncTout
) {
2518 static void moxa_low_water_check(void __iomem
*ofsAddr
)
2521 ushort rptr
, wptr
, mask
;
2523 if (readb(ofsAddr
+ FlagStat
) & Xoff_state
) {
2524 rptr
= readw(ofsAddr
+ RXrptr
);
2525 wptr
= readw(ofsAddr
+ RXwptr
);
2526 mask
= readw(ofsAddr
+ RX_mask
);
2527 len
= (wptr
- rptr
) & mask
;
2528 if (len
<= Low_water
)
2529 moxafunc(ofsAddr
, FC_SendXon
, 0);
2533 static void MoxaSetFifo(struct moxa_port
*port
, int enable
)
2535 void __iomem
*ofsAddr
= port
->tableAddr
;
2538 moxafunc(ofsAddr
, FC_SetRxFIFOTrig
, 0);
2539 moxafunc(ofsAddr
, FC_SetTxFIFOCnt
, 1);
2541 moxafunc(ofsAddr
, FC_SetRxFIFOTrig
, 3);
2542 moxafunc(ofsAddr
, FC_SetTxFIFOCnt
, 16);