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
;
147 void __iomem
*tableAddr
;
155 #define TXSTOPPED 0x1
157 #define EMPTYWAIT 0x4
160 #define SERIAL_DO_RESTART
162 #define WAKEUP_CHARS 256
164 static int ttymajor
= MOXAMAJOR
;
166 /* Variables for insmod */
168 static unsigned long baseaddr
[MAX_BOARDS
];
169 static unsigned int type
[MAX_BOARDS
];
170 static unsigned int numports
[MAX_BOARDS
];
173 MODULE_AUTHOR("William Chen");
174 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
175 MODULE_LICENSE("GPL");
177 module_param_array(type
, uint
, NULL
, 0);
178 MODULE_PARM_DESC(type
, "card type: C218=2, C320=4");
179 module_param_array(baseaddr
, ulong
, NULL
, 0);
180 MODULE_PARM_DESC(baseaddr
, "base address");
181 module_param_array(numports
, uint
, NULL
, 0);
182 MODULE_PARM_DESC(numports
, "numports (ignored for C218)");
184 module_param(ttymajor
, int, 0);
189 static int moxa_open(struct tty_struct
*, struct file
*);
190 static void moxa_close(struct tty_struct
*, struct file
*);
191 static int moxa_write(struct tty_struct
*, const unsigned char *, int);
192 static int moxa_write_room(struct tty_struct
*);
193 static void moxa_flush_buffer(struct tty_struct
*);
194 static int moxa_chars_in_buffer(struct tty_struct
*);
195 static void moxa_flush_chars(struct tty_struct
*);
196 static void moxa_put_char(struct tty_struct
*, unsigned char);
197 static int moxa_ioctl(struct tty_struct
*, struct file
*, unsigned int, unsigned long);
198 static void moxa_throttle(struct tty_struct
*);
199 static void moxa_unthrottle(struct tty_struct
*);
200 static void moxa_set_termios(struct tty_struct
*, struct ktermios
*);
201 static void moxa_stop(struct tty_struct
*);
202 static void moxa_start(struct tty_struct
*);
203 static void moxa_hangup(struct tty_struct
*);
204 static int moxa_tiocmget(struct tty_struct
*tty
, struct file
*file
);
205 static int moxa_tiocmset(struct tty_struct
*tty
, struct file
*file
,
206 unsigned int set
, unsigned int clear
);
207 static void moxa_poll(unsigned long);
208 static void moxa_set_tty_param(struct tty_struct
*, struct ktermios
*);
209 static int moxa_block_till_ready(struct tty_struct
*, struct file
*,
211 static void moxa_setup_empty_event(struct tty_struct
*);
212 static void moxa_check_xmit_empty(unsigned long);
213 static void moxa_shut_down(struct moxa_port
*);
214 static void moxa_receive_data(struct moxa_port
*);
216 * moxa board interface functions:
218 static int MoxaDriverIoctl(struct tty_struct
*, unsigned int, unsigned long);
219 static int MoxaDriverPoll(void);
220 static int MoxaPortsOfCard(int);
221 static int MoxaPortIsValid(int);
222 static void MoxaPortEnable(struct moxa_port
*);
223 static void MoxaPortDisable(struct moxa_port
*);
224 static int MoxaPortSetTermio(struct moxa_port
*, struct ktermios
*, speed_t
);
225 static int MoxaPortGetLineOut(struct moxa_port
*, int *, int *);
226 static void MoxaPortLineCtrl(struct moxa_port
*, int, int);
227 static void MoxaPortFlowCtrl(struct moxa_port
*, int, int, int, int, int);
228 static int MoxaPortLineStatus(struct moxa_port
*);
229 static int MoxaPortDCDChange(struct moxa_port
*);
230 static int MoxaPortDCDON(struct moxa_port
*);
231 static void MoxaPortFlushData(struct moxa_port
*, int);
232 static int MoxaPortWriteData(struct moxa_port
*, unsigned char *, int);
233 static int MoxaPortReadData(struct moxa_port
*, struct tty_struct
*tty
);
234 static int MoxaPortTxQueue(struct moxa_port
*);
235 static int MoxaPortRxQueue(struct moxa_port
*);
236 static int MoxaPortTxFree(struct moxa_port
*);
237 static void MoxaPortTxDisable(struct moxa_port
*);
238 static void MoxaPortTxEnable(struct moxa_port
*);
239 static int MoxaPortResetBrkCnt(struct moxa_port
*);
240 static void MoxaPortSendBreak(struct moxa_port
*, int);
241 static int moxa_get_serial_info(struct moxa_port
*, struct serial_struct __user
*);
242 static int moxa_set_serial_info(struct moxa_port
*, struct serial_struct __user
*);
243 static void MoxaSetFifo(struct moxa_port
*port
, int enable
);
245 static const struct tty_operations moxa_ops
= {
249 .write_room
= moxa_write_room
,
250 .flush_buffer
= moxa_flush_buffer
,
251 .chars_in_buffer
= moxa_chars_in_buffer
,
252 .flush_chars
= moxa_flush_chars
,
253 .put_char
= moxa_put_char
,
255 .throttle
= moxa_throttle
,
256 .unthrottle
= moxa_unthrottle
,
257 .set_termios
= moxa_set_termios
,
260 .hangup
= moxa_hangup
,
261 .tiocmget
= moxa_tiocmget
,
262 .tiocmset
= moxa_tiocmset
,
265 static struct tty_driver
*moxaDriver
;
266 static struct moxa_port moxa_ports
[MAX_PORTS
];
267 static DEFINE_TIMER(moxaTimer
, moxa_poll
, 0, 0);
268 static DEFINE_SPINLOCK(moxa_lock
);
270 static int moxa_check_fw_model(struct moxa_board_conf
*brd
, u8 model
)
272 switch (brd
->boardType
) {
273 case MOXA_BOARD_C218_ISA
:
274 case MOXA_BOARD_C218_PCI
:
278 case MOXA_BOARD_CP204J
:
292 static int moxa_check_fw(const void *ptr
)
294 const __le16
*lptr
= ptr
;
296 if (*lptr
!= cpu_to_le16(0x7980))
302 static int moxa_load_bios(struct moxa_board_conf
*brd
, const u8
*buf
,
305 void __iomem
*baseAddr
= brd
->basemem
;
308 writeb(HW_reset
, baseAddr
+ Control_reg
); /* reset */
310 memset_io(baseAddr
, 0, 4096);
311 memcpy_toio(baseAddr
, buf
, len
); /* download BIOS */
312 writeb(0, baseAddr
+ Control_reg
); /* restart */
316 switch (brd
->boardType
) {
317 case MOXA_BOARD_C218_ISA
:
318 case MOXA_BOARD_C218_PCI
:
319 tmp
= readw(baseAddr
+ C218_key
);
320 if (tmp
!= C218_KeyCode
)
323 case MOXA_BOARD_CP204J
:
324 tmp
= readw(baseAddr
+ C218_key
);
325 if (tmp
!= CP204J_KeyCode
)
329 tmp
= readw(baseAddr
+ C320_key
);
330 if (tmp
!= C320_KeyCode
)
332 tmp
= readw(baseAddr
+ C320_status
);
333 if (tmp
!= STS_init
) {
334 printk(KERN_ERR
"moxa: bios upload failed -- CPU/Basic "
335 "module not found\n");
343 printk(KERN_ERR
"moxa: bios upload failed -- board not found\n");
347 static int moxa_load_320b(struct moxa_board_conf
*brd
, const u8
*ptr
,
350 void __iomem
*baseAddr
= brd
->basemem
;
353 printk(KERN_ERR
"moxa: invalid 320 bios -- too short\n");
357 writew(len
- 7168 - 2, baseAddr
+ C320bapi_len
);
358 writeb(1, baseAddr
+ Control_reg
); /* Select Page 1 */
359 memcpy_toio(baseAddr
+ DynPage_addr
, ptr
, 7168);
360 writeb(2, baseAddr
+ Control_reg
); /* Select Page 2 */
361 memcpy_toio(baseAddr
+ DynPage_addr
, ptr
+ 7168, len
- 7168);
366 static int moxa_real_load_code(struct moxa_board_conf
*brd
, const void *ptr
,
369 void __iomem
*baseAddr
= brd
->basemem
;
370 const u16
*uptr
= ptr
;
371 size_t wlen
, len2
, j
;
372 unsigned long key
, loadbuf
, loadlen
, checksum
, checksum_ok
;
373 unsigned int i
, retry
, c320
;
376 c320
= brd
->boardType
== MOXA_BOARD_C320_PCI
||
377 brd
->boardType
== MOXA_BOARD_C320_ISA
;
378 keycode
= (brd
->boardType
== MOXA_BOARD_CP204J
) ? CP204J_KeyCode
:
381 switch (brd
->boardType
) {
382 case MOXA_BOARD_CP204J
:
383 case MOXA_BOARD_C218_ISA
:
384 case MOXA_BOARD_C218_PCI
:
386 loadbuf
= C218_LoadBuf
;
387 loadlen
= C218DLoad_len
;
388 checksum
= C218check_sum
;
389 checksum_ok
= C218chksum_ok
;
393 keycode
= C320_KeyCode
;
394 loadbuf
= C320_LoadBuf
;
395 loadlen
= C320DLoad_len
;
396 checksum
= C320check_sum
;
397 checksum_ok
= C320chksum_ok
;
403 for (i
= 0; i
< wlen
; i
++)
404 usum
+= le16_to_cpu(uptr
[i
]);
410 len2
= (wlen
> 2048) ? 2048 : wlen
;
412 memcpy_toio(baseAddr
+ loadbuf
, ptr
+ j
, len2
<< 1);
415 writew(len2
, baseAddr
+ loadlen
);
416 writew(0, baseAddr
+ key
);
417 for (i
= 0; i
< 100; i
++) {
418 if (readw(baseAddr
+ key
) == keycode
)
422 if (readw(baseAddr
+ key
) != keycode
)
425 writew(0, baseAddr
+ loadlen
);
426 writew(usum
, baseAddr
+ checksum
);
427 writew(0, baseAddr
+ key
);
428 for (i
= 0; i
< 100; i
++) {
429 if (readw(baseAddr
+ key
) == keycode
)
434 } while ((readb(baseAddr
+ checksum_ok
) != 1) && (retry
< 3));
435 if (readb(baseAddr
+ checksum_ok
) != 1)
438 writew(0, baseAddr
+ key
);
439 for (i
= 0; i
< 600; i
++) {
440 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
444 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
448 if (brd
->busType
== MOXA_BUS_TYPE_PCI
) { /* ASIC board */
449 writew(0x3800, baseAddr
+ TMS320_PORT1
);
450 writew(0x3900, baseAddr
+ TMS320_PORT2
);
451 writew(28499, baseAddr
+ TMS320_CLOCK
);
453 writew(0x3200, baseAddr
+ TMS320_PORT1
);
454 writew(0x3400, baseAddr
+ TMS320_PORT2
);
455 writew(19999, baseAddr
+ TMS320_CLOCK
);
458 writew(1, baseAddr
+ Disable_IRQ
);
459 writew(0, baseAddr
+ Magic_no
);
460 for (i
= 0; i
< 500; i
++) {
461 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
465 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
469 j
= readw(baseAddr
+ Module_cnt
);
472 brd
->numPorts
= j
* 8;
473 writew(j
, baseAddr
+ Module_no
);
474 writew(0, baseAddr
+ Magic_no
);
475 for (i
= 0; i
< 600; i
++) {
476 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
480 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
484 brd
->intNdx
= baseAddr
+ IRQindex
;
485 brd
->intPend
= baseAddr
+ IRQpending
;
486 brd
->intTable
= baseAddr
+ IRQtable
;
491 static int moxa_load_code(struct moxa_board_conf
*brd
, const void *ptr
,
494 void __iomem
*ofsAddr
, *baseAddr
= brd
->basemem
;
495 struct moxa_port
*port
;
499 printk(KERN_ERR
"moxa: bios length is not even\n");
503 retval
= moxa_real_load_code(brd
, ptr
, len
); /* may change numPorts */
507 switch (brd
->boardType
) {
508 case MOXA_BOARD_C218_ISA
:
509 case MOXA_BOARD_C218_PCI
:
510 case MOXA_BOARD_CP204J
:
512 for (i
= 0; i
< brd
->numPorts
; i
++, port
++) {
516 port
->tableAddr
= baseAddr
+ Extern_table
+
518 ofsAddr
= port
->tableAddr
;
519 writew(C218rx_mask
, ofsAddr
+ RX_mask
);
520 writew(C218tx_mask
, ofsAddr
+ TX_mask
);
521 writew(C218rx_spage
+ i
* C218buf_pageno
, ofsAddr
+ Page_rxb
);
522 writew(readw(ofsAddr
+ Page_rxb
) + C218rx_pageno
, ofsAddr
+ EndPage_rxb
);
524 writew(C218tx_spage
+ i
* C218buf_pageno
, ofsAddr
+ Page_txb
);
525 writew(readw(ofsAddr
+ Page_txb
) + C218tx_pageno
, ofsAddr
+ EndPage_txb
);
531 for (i
= 0; i
< brd
->numPorts
; i
++, port
++) {
535 port
->tableAddr
= baseAddr
+ Extern_table
+
537 ofsAddr
= port
->tableAddr
;
538 switch (brd
->numPorts
) {
540 writew(C320p8rx_mask
, ofsAddr
+ RX_mask
);
541 writew(C320p8tx_mask
, ofsAddr
+ TX_mask
);
542 writew(C320p8rx_spage
+ i
* C320p8buf_pgno
, ofsAddr
+ Page_rxb
);
543 writew(readw(ofsAddr
+ Page_rxb
) + C320p8rx_pgno
, ofsAddr
+ EndPage_rxb
);
544 writew(C320p8tx_spage
+ i
* C320p8buf_pgno
, ofsAddr
+ Page_txb
);
545 writew(readw(ofsAddr
+ Page_txb
) + C320p8tx_pgno
, ofsAddr
+ EndPage_txb
);
549 writew(C320p16rx_mask
, ofsAddr
+ RX_mask
);
550 writew(C320p16tx_mask
, ofsAddr
+ TX_mask
);
551 writew(C320p16rx_spage
+ i
* C320p16buf_pgno
, ofsAddr
+ Page_rxb
);
552 writew(readw(ofsAddr
+ Page_rxb
) + C320p16rx_pgno
, ofsAddr
+ EndPage_rxb
);
553 writew(C320p16tx_spage
+ i
* C320p16buf_pgno
, ofsAddr
+ Page_txb
);
554 writew(readw(ofsAddr
+ Page_txb
) + C320p16tx_pgno
, ofsAddr
+ EndPage_txb
);
558 writew(C320p24rx_mask
, ofsAddr
+ RX_mask
);
559 writew(C320p24tx_mask
, ofsAddr
+ TX_mask
);
560 writew(C320p24rx_spage
+ i
* C320p24buf_pgno
, ofsAddr
+ Page_rxb
);
561 writew(readw(ofsAddr
+ Page_rxb
) + C320p24rx_pgno
, ofsAddr
+ EndPage_rxb
);
562 writew(C320p24tx_spage
+ i
* C320p24buf_pgno
, ofsAddr
+ Page_txb
);
563 writew(readw(ofsAddr
+ Page_txb
), ofsAddr
+ EndPage_txb
);
566 writew(C320p32rx_mask
, ofsAddr
+ RX_mask
);
567 writew(C320p32tx_mask
, ofsAddr
+ TX_mask
);
568 writew(C320p32tx_ofs
, ofsAddr
+ Ofs_txb
);
569 writew(C320p32rx_spage
+ i
* C320p32buf_pgno
, ofsAddr
+ Page_rxb
);
570 writew(readb(ofsAddr
+ Page_rxb
), ofsAddr
+ EndPage_rxb
);
571 writew(C320p32tx_spage
+ i
* C320p32buf_pgno
, ofsAddr
+ Page_txb
);
572 writew(readw(ofsAddr
+ Page_txb
), ofsAddr
+ EndPage_txb
);
582 static int moxa_load_fw(struct moxa_board_conf
*brd
, const struct firmware
*fw
)
584 void *ptr
= fw
->data
;
588 unsigned int a
, lenp
, lencnt
;
591 __le32 magic
; /* 0x34303430 */
593 u8 type
; /* UNIX = 3 */
594 u8 model
; /* C218T=1, C320T=2, CP204=3 */
599 BUILD_BUG_ON(ARRAY_SIZE(hdr
->len
) != ARRAY_SIZE(lens
));
601 if (fw
->size
< MOXA_FW_HDRLEN
) {
602 strcpy(rsn
, "too short (even header won't fit)");
605 if (hdr
->magic
!= cpu_to_le32(0x30343034)) {
606 sprintf(rsn
, "bad magic: %.8x", le32_to_cpu(hdr
->magic
));
609 if (hdr
->type
!= 3) {
610 sprintf(rsn
, "not for linux, type is %u", hdr
->type
);
613 if (moxa_check_fw_model(brd
, hdr
->model
)) {
614 sprintf(rsn
, "not for this card, model is %u", hdr
->model
);
618 len
= MOXA_FW_HDRLEN
;
619 lencnt
= hdr
->model
== 2 ? 5 : 3;
620 for (a
= 0; a
< ARRAY_SIZE(lens
); a
++) {
621 lens
[a
] = le16_to_cpu(hdr
->len
[a
]);
622 if (lens
[a
] && len
+ lens
[a
] <= fw
->size
&&
623 moxa_check_fw(&fw
->data
[len
]))
624 printk(KERN_WARNING
"moxa firmware: unexpected input "
625 "at offset %u, but going on\n", (u32
)len
);
626 if (!lens
[a
] && a
< lencnt
) {
627 sprintf(rsn
, "too few entries in fw file");
633 if (len
!= fw
->size
) {
634 sprintf(rsn
, "bad length: %u (should be %u)", (u32
)fw
->size
,
639 ptr
+= MOXA_FW_HDRLEN
;
642 strcpy(rsn
, "read above");
644 ret
= moxa_load_bios(brd
, ptr
, lens
[lenp
]);
648 /* we skip the tty section (lens[1]), since we don't need it */
649 ptr
+= lens
[lenp
] + lens
[lenp
+ 1];
650 lenp
+= 2; /* comm */
652 if (hdr
->model
== 2) {
653 ret
= moxa_load_320b(brd
, ptr
, lens
[lenp
]);
656 /* skip another tty */
657 ptr
+= lens
[lenp
] + lens
[lenp
+ 1];
661 ret
= moxa_load_code(brd
, ptr
, lens
[lenp
]);
667 printk(KERN_ERR
"firmware failed to load, reason: %s\n", rsn
);
671 static int moxa_init_board(struct moxa_board_conf
*brd
, struct device
*dev
)
673 const struct firmware
*fw
;
677 switch (brd
->boardType
) {
678 case MOXA_BOARD_C218_ISA
:
679 case MOXA_BOARD_C218_PCI
:
680 file
= "c218tunx.cod";
682 case MOXA_BOARD_CP204J
:
683 file
= "cp204unx.cod";
686 file
= "c320tunx.cod";
690 ret
= request_firmware(&fw
, file
, dev
);
692 printk(KERN_ERR
"request_firmware failed\n");
696 ret
= moxa_load_fw(brd
, fw
);
698 release_firmware(fw
);
704 static int __devinit
moxa_pci_probe(struct pci_dev
*pdev
,
705 const struct pci_device_id
*ent
)
707 struct moxa_board_conf
*board
;
709 int board_type
= ent
->driver_data
;
712 retval
= pci_enable_device(pdev
);
714 dev_err(&pdev
->dev
, "can't enable pci device\n");
718 for (i
= 0; i
< MAX_BOARDS
; i
++)
719 if (moxa_boards
[i
].basemem
== NULL
)
723 if (i
>= MAX_BOARDS
) {
724 dev_warn(&pdev
->dev
, "more than %u MOXA Intellio family boards "
725 "found. Board is ignored.\n", MAX_BOARDS
);
729 board
= &moxa_boards
[i
];
730 board
->ports
= &moxa_ports
[i
* MAX_PORTS_PER_BOARD
];
732 retval
= pci_request_region(pdev
, 2, "moxa-base");
734 dev_err(&pdev
->dev
, "can't request pci region 2\n");
738 board
->basemem
= ioremap(pci_resource_start(pdev
, 2), 0x4000);
739 if (board
->basemem
== NULL
) {
740 dev_err(&pdev
->dev
, "can't remap io space 2\n");
744 board
->boardType
= board_type
;
745 switch (board_type
) {
746 case MOXA_BOARD_C218_ISA
:
747 case MOXA_BOARD_C218_PCI
:
751 case MOXA_BOARD_CP204J
:
758 board
->busType
= MOXA_BUS_TYPE_PCI
;
760 retval
= moxa_init_board(board
, &pdev
->dev
);
764 pci_set_drvdata(pdev
, board
);
768 iounmap(board
->basemem
);
769 board
->basemem
= NULL
;
771 pci_release_region(pdev
, 2);
776 static void __devexit
moxa_pci_remove(struct pci_dev
*pdev
)
778 struct moxa_board_conf
*brd
= pci_get_drvdata(pdev
);
780 iounmap(brd
->basemem
);
782 pci_release_region(pdev
, 2);
785 static struct pci_driver moxa_pci_driver
= {
787 .id_table
= moxa_pcibrds
,
788 .probe
= moxa_pci_probe
,
789 .remove
= __devexit_p(moxa_pci_remove
)
791 #endif /* CONFIG_PCI */
793 static int __init
moxa_init(void)
795 struct moxa_port
*ch
;
796 unsigned int i
, isabrds
= 0;
799 printk(KERN_INFO
"MOXA Intellio family driver version %s\n",
801 moxaDriver
= alloc_tty_driver(MAX_PORTS
+ 1);
805 moxaDriver
->owner
= THIS_MODULE
;
806 moxaDriver
->name
= "ttyMX";
807 moxaDriver
->major
= ttymajor
;
808 moxaDriver
->minor_start
= 0;
809 moxaDriver
->type
= TTY_DRIVER_TYPE_SERIAL
;
810 moxaDriver
->subtype
= SERIAL_TYPE_NORMAL
;
811 moxaDriver
->init_termios
= tty_std_termios
;
812 moxaDriver
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| CLOCAL
| HUPCL
;
813 moxaDriver
->init_termios
.c_ispeed
= 9600;
814 moxaDriver
->init_termios
.c_ospeed
= 9600;
815 moxaDriver
->flags
= TTY_DRIVER_REAL_RAW
;
816 tty_set_operations(moxaDriver
, &moxa_ops
);
818 for (i
= 0, ch
= moxa_ports
; i
< MAX_PORTS
; i
++, ch
++) {
819 ch
->type
= PORT_16550A
;
820 ch
->close_delay
= 5 * HZ
/ 10;
821 ch
->cflag
= B9600
| CS8
| CREAD
| CLOCAL
| HUPCL
;
822 init_waitqueue_head(&ch
->open_wait
);
823 init_completion(&ch
->close_wait
);
825 setup_timer(&ch
->emptyTimer
, moxa_check_xmit_empty
,
829 pr_debug("Moxa tty devices major number = %d\n", ttymajor
);
831 if (tty_register_driver(moxaDriver
)) {
832 printk(KERN_ERR
"Couldn't install MOXA Smartio family driver !\n");
833 put_tty_driver(moxaDriver
);
837 mod_timer(&moxaTimer
, jiffies
+ HZ
/ 50);
839 /* Find the boards defined from module args. */
842 struct moxa_board_conf
*brd
= moxa_boards
;
843 for (i
= 0; i
< MAX_BOARDS
; i
++) {
846 if (type
[i
] == MOXA_BOARD_C218_ISA
||
847 type
[i
] == MOXA_BOARD_C320_ISA
) {
848 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
849 isabrds
+ 1, moxa_brdname
[type
[i
] - 1],
851 brd
->boardType
= type
[i
];
852 brd
->ports
= &moxa_ports
[isabrds
* MAX_PORTS_PER_BOARD
];
853 brd
->numPorts
= type
[i
] == MOXA_BOARD_C218_ISA
? 8 :
855 brd
->busType
= MOXA_BUS_TYPE_ISA
;
856 brd
->basemem
= ioremap(baseaddr
[i
], 0x4000);
858 printk(KERN_ERR
"moxa: can't remap %lx\n",
862 if (moxa_init_board(brd
, NULL
)) {
863 iounmap(brd
->basemem
);
876 retval
= pci_register_driver(&moxa_pci_driver
);
878 printk(KERN_ERR
"Can't register moxa pci driver!\n");
887 static void __exit
moxa_exit(void)
891 del_timer_sync(&moxaTimer
);
893 for (i
= 0; i
< MAX_PORTS
; i
++)
894 del_timer_sync(&moxa_ports
[i
].emptyTimer
);
896 if (tty_unregister_driver(moxaDriver
))
897 printk(KERN_ERR
"Couldn't unregister MOXA Intellio family "
899 put_tty_driver(moxaDriver
);
902 pci_unregister_driver(&moxa_pci_driver
);
905 for (i
= 0; i
< MAX_BOARDS
; i
++)
906 if (moxa_boards
[i
].basemem
)
907 iounmap(moxa_boards
[i
].basemem
);
910 module_init(moxa_init
);
911 module_exit(moxa_exit
);
913 static int moxa_open(struct tty_struct
*tty
, struct file
*filp
)
915 struct moxa_port
*ch
;
920 if (port
== MAX_PORTS
) {
923 if (!MoxaPortIsValid(port
)) {
924 tty
->driver_data
= NULL
;
928 ch
= &moxa_ports
[port
];
930 tty
->driver_data
= ch
;
932 if (!(ch
->asyncflags
& ASYNC_INITIALIZED
)) {
934 moxa_set_tty_param(tty
, tty
->termios
);
935 MoxaPortLineCtrl(ch
, 1, 1);
937 ch
->asyncflags
|= ASYNC_INITIALIZED
;
939 retval
= moxa_block_till_ready(tty
, filp
, ch
);
941 moxa_unthrottle(tty
);
943 if (ch
->type
== PORT_16550A
) {
952 static void moxa_close(struct tty_struct
*tty
, struct file
*filp
)
954 struct moxa_port
*ch
;
958 if (port
== MAX_PORTS
) {
961 if (!MoxaPortIsValid(port
)) {
962 pr_debug("Invalid portno in moxa_close\n");
963 tty
->driver_data
= NULL
;
966 if (tty
->driver_data
== NULL
) {
969 if (tty_hung_up_p(filp
)) {
972 ch
= (struct moxa_port
*) tty
->driver_data
;
974 if ((tty
->count
== 1) && (ch
->count
!= 1)) {
975 printk(KERN_WARNING
"moxa_close: bad serial port count; "
976 "tty->count is 1, ch->count is %d\n", ch
->count
);
979 if (--ch
->count
< 0) {
980 printk(KERN_WARNING
"moxa_close: bad serial port count, "
981 "device=%s\n", tty
->name
);
987 ch
->asyncflags
|= ASYNC_CLOSING
;
989 ch
->cflag
= tty
->termios
->c_cflag
;
990 if (ch
->asyncflags
& ASYNC_INITIALIZED
) {
991 moxa_setup_empty_event(tty
);
992 tty_wait_until_sent(tty
, 30 * HZ
); /* 30 seconds timeout */
993 del_timer_sync(&ch
->emptyTimer
);
996 MoxaPortFlushData(ch
, 2);
998 if (tty
->driver
->flush_buffer
)
999 tty
->driver
->flush_buffer(tty
);
1000 tty_ldisc_flush(tty
);
1004 if (ch
->blocked_open
) {
1005 if (ch
->close_delay
) {
1006 msleep_interruptible(jiffies_to_msecs(ch
->close_delay
));
1008 wake_up_interruptible(&ch
->open_wait
);
1010 ch
->asyncflags
&= ~(ASYNC_NORMAL_ACTIVE
| ASYNC_CLOSING
);
1011 complete_all(&ch
->close_wait
);
1014 static int moxa_write(struct tty_struct
*tty
,
1015 const unsigned char *buf
, int count
)
1017 struct moxa_port
*ch
= tty
->driver_data
;
1018 unsigned long flags
;
1024 spin_lock_irqsave(&moxa_lock
, flags
);
1025 len
= MoxaPortWriteData(ch
, (unsigned char *) buf
, count
);
1026 spin_unlock_irqrestore(&moxa_lock
, flags
);
1028 /*********************************************
1029 if ( !(ch->statusflags & LOWWAIT) &&
1030 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
1031 ************************************************/
1032 ch
->statusflags
|= LOWWAIT
;
1036 static int moxa_write_room(struct tty_struct
*tty
)
1038 struct moxa_port
*ch
;
1042 ch
= tty
->driver_data
;
1045 return MoxaPortTxFree(ch
);
1048 static void moxa_flush_buffer(struct tty_struct
*tty
)
1050 struct moxa_port
*ch
= tty
->driver_data
;
1054 MoxaPortFlushData(ch
, 1);
1058 static int moxa_chars_in_buffer(struct tty_struct
*tty
)
1060 struct moxa_port
*ch
= tty
->driver_data
;
1064 * Sigh...I have to check if driver_data is NULL here, because
1065 * if an open() fails, the TTY subsystem eventually calls
1066 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1067 * routine. And since the open() failed, we return 0 here. TDJ
1071 chars
= MoxaPortTxQueue(ch
);
1074 * Make it possible to wakeup anything waiting for output
1075 * in tty_ioctl.c, etc.
1077 if (!(ch
->statusflags
& EMPTYWAIT
))
1078 moxa_setup_empty_event(tty
);
1083 static void moxa_flush_chars(struct tty_struct
*tty
)
1086 * Don't think I need this, because this is called to empty the TX
1087 * buffer for the 16450, 16550, etc.
1091 static void moxa_put_char(struct tty_struct
*tty
, unsigned char c
)
1093 struct moxa_port
*ch
= tty
->driver_data
;
1094 unsigned long flags
;
1098 spin_lock_irqsave(&moxa_lock
, flags
);
1099 MoxaPortWriteData(ch
, &c
, 1);
1100 spin_unlock_irqrestore(&moxa_lock
, flags
);
1101 /************************************************
1102 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
1103 *************************************************/
1104 ch
->statusflags
|= LOWWAIT
;
1107 static int moxa_tiocmget(struct tty_struct
*tty
, struct file
*file
)
1109 struct moxa_port
*ch
= tty
->driver_data
;
1110 int flag
= 0, dtr
, rts
;
1112 if ((tty
->index
!= MAX_PORTS
) && (!ch
))
1115 MoxaPortGetLineOut(ch
, &dtr
, &rts
);
1120 dtr
= MoxaPortLineStatus(ch
);
1130 static int moxa_tiocmset(struct tty_struct
*tty
, struct file
*file
,
1131 unsigned int set
, unsigned int clear
)
1133 struct moxa_port
*ch
= tty
->driver_data
;
1138 if ((port
!= MAX_PORTS
) && (!ch
))
1141 MoxaPortGetLineOut(ch
, &dtr
, &rts
);
1142 if (set
& TIOCM_RTS
)
1144 if (set
& TIOCM_DTR
)
1146 if (clear
& TIOCM_RTS
)
1148 if (clear
& TIOCM_DTR
)
1150 MoxaPortLineCtrl(ch
, dtr
, rts
);
1154 static int moxa_ioctl(struct tty_struct
*tty
, struct file
*file
,
1155 unsigned int cmd
, unsigned long arg
)
1157 struct moxa_port
*ch
= tty
->driver_data
;
1159 void __user
*argp
= (void __user
*)arg
;
1163 if ((port
!= MAX_PORTS
) && (!ch
))
1167 case TCSBRK
: /* SVID version: non-zero arg --> no break */
1168 retval
= tty_check_change(tty
);
1171 moxa_setup_empty_event(tty
);
1172 tty_wait_until_sent(tty
, 0);
1174 MoxaPortSendBreak(ch
, 0);
1176 case TCSBRKP
: /* support for POSIX tcsendbreak() */
1177 retval
= tty_check_change(tty
);
1180 moxa_setup_empty_event(tty
);
1181 tty_wait_until_sent(tty
, 0);
1182 MoxaPortSendBreak(ch
, arg
);
1185 return put_user(C_CLOCAL(tty
) ? 1 : 0, (int __user
*)argp
);
1187 if (get_user(retval
, (int __user
*)argp
))
1190 tty
->termios
->c_cflag
= ((tty
->termios
->c_cflag
& ~CLOCAL
) |
1191 (arg
? CLOCAL
: 0));
1193 ch
->asyncflags
&= ~ASYNC_CHECK_CD
;
1195 ch
->asyncflags
|= ASYNC_CHECK_CD
;
1198 return moxa_get_serial_info(ch
, argp
);
1201 return moxa_set_serial_info(ch
, argp
);
1203 retval
= MoxaDriverIoctl(tty
, cmd
, arg
);
1208 static void moxa_throttle(struct tty_struct
*tty
)
1210 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
1212 ch
->statusflags
|= THROTTLE
;
1215 static void moxa_unthrottle(struct tty_struct
*tty
)
1217 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
1219 ch
->statusflags
&= ~THROTTLE
;
1222 static void moxa_set_termios(struct tty_struct
*tty
,
1223 struct ktermios
*old_termios
)
1225 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
1229 moxa_set_tty_param(tty
, old_termios
);
1230 if (!(old_termios
->c_cflag
& CLOCAL
) &&
1231 (tty
->termios
->c_cflag
& CLOCAL
))
1232 wake_up_interruptible(&ch
->open_wait
);
1235 static void moxa_stop(struct tty_struct
*tty
)
1237 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
1241 MoxaPortTxDisable(ch
);
1242 ch
->statusflags
|= TXSTOPPED
;
1246 static void moxa_start(struct tty_struct
*tty
)
1248 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
1253 if (!(ch
->statusflags
& TXSTOPPED
))
1256 MoxaPortTxEnable(ch
);
1257 ch
->statusflags
&= ~TXSTOPPED
;
1260 static void moxa_hangup(struct tty_struct
*tty
)
1262 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
1264 moxa_flush_buffer(tty
);
1267 ch
->asyncflags
&= ~ASYNC_NORMAL_ACTIVE
;
1269 wake_up_interruptible(&ch
->open_wait
);
1272 static void moxa_poll(unsigned long ignored
)
1275 struct moxa_port
*ch
;
1276 struct tty_struct
*tp
;
1279 del_timer(&moxaTimer
);
1281 if (MoxaDriverPoll() < 0) {
1282 mod_timer(&moxaTimer
, jiffies
+ HZ
/ 50);
1285 for (card
= 0; card
< MAX_BOARDS
; card
++) {
1286 if ((ports
= MoxaPortsOfCard(card
)) <= 0)
1288 ch
= &moxa_ports
[card
* MAX_PORTS_PER_BOARD
];
1289 for (i
= 0; i
< ports
; i
++, ch
++) {
1290 if ((ch
->asyncflags
& ASYNC_INITIALIZED
) == 0)
1292 if (!(ch
->statusflags
& THROTTLE
) &&
1293 (MoxaPortRxQueue(ch
) > 0))
1294 moxa_receive_data(ch
);
1295 if ((tp
= ch
->tty
) == 0)
1297 if (ch
->statusflags
& LOWWAIT
) {
1298 if (MoxaPortTxQueue(ch
) <= WAKEUP_CHARS
) {
1300 ch
->statusflags
&= ~LOWWAIT
;
1305 if (!I_IGNBRK(tp
) && (MoxaPortResetBrkCnt(ch
) > 0)) {
1306 tty_insert_flip_char(tp
, 0, TTY_BREAK
);
1307 tty_schedule_flip(tp
);
1309 if (MoxaPortDCDChange(ch
)) {
1310 if (ch
->asyncflags
& ASYNC_CHECK_CD
) {
1311 if (MoxaPortDCDON(ch
))
1312 wake_up_interruptible(&ch
->open_wait
);
1315 wake_up_interruptible(&ch
->open_wait
);
1316 ch
->asyncflags
&= ~ASYNC_NORMAL_ACTIVE
;
1323 mod_timer(&moxaTimer
, jiffies
+ HZ
/ 50);
1326 /******************************************************************************/
1328 static void moxa_set_tty_param(struct tty_struct
*tty
, struct ktermios
*old_termios
)
1330 register struct ktermios
*ts
;
1331 struct moxa_port
*ch
;
1332 int rts
, cts
, txflow
, rxflow
, xany
, baud
;
1334 ch
= (struct moxa_port
*) tty
->driver_data
;
1336 if (ts
->c_cflag
& CLOCAL
)
1337 ch
->asyncflags
&= ~ASYNC_CHECK_CD
;
1339 ch
->asyncflags
|= ASYNC_CHECK_CD
;
1340 rts
= cts
= txflow
= rxflow
= xany
= 0;
1341 if (ts
->c_cflag
& CRTSCTS
)
1343 if (ts
->c_iflag
& IXON
)
1345 if (ts
->c_iflag
& IXOFF
)
1347 if (ts
->c_iflag
& IXANY
)
1350 /* Clear the features we don't support */
1351 ts
->c_cflag
&= ~CMSPAR
;
1352 MoxaPortFlowCtrl(ch
, rts
, cts
, txflow
, rxflow
, xany
);
1353 baud
= MoxaPortSetTermio(ch
, ts
, tty_get_baud_rate(tty
));
1355 baud
= tty_termios_baud_rate(old_termios
);
1356 /* Not put the baud rate into the termios data */
1357 tty_encode_baud_rate(tty
, baud
, baud
);
1360 static int moxa_block_till_ready(struct tty_struct
*tty
, struct file
*filp
,
1361 struct moxa_port
*ch
)
1363 DECLARE_WAITQUEUE(wait
,current
);
1364 unsigned long flags
;
1366 int do_clocal
= C_CLOCAL(tty
);
1369 * If the device is in the middle of being closed, then block
1370 * until it's done, and then try again.
1372 if (tty_hung_up_p(filp
) || (ch
->asyncflags
& ASYNC_CLOSING
)) {
1373 if (ch
->asyncflags
& ASYNC_CLOSING
)
1374 wait_for_completion_interruptible(&ch
->close_wait
);
1375 #ifdef SERIAL_DO_RESTART
1376 if (ch
->asyncflags
& ASYNC_HUP_NOTIFY
)
1379 return (-ERESTARTSYS
);
1385 * If non-blocking mode is set, then make the check up front
1388 if (filp
->f_flags
& O_NONBLOCK
) {
1389 ch
->asyncflags
|= ASYNC_NORMAL_ACTIVE
;
1393 * Block waiting for the carrier detect and the line to become free
1396 add_wait_queue(&ch
->open_wait
, &wait
);
1397 pr_debug("block_til_ready before block: ttys%d, count = %d\n",
1398 tty
->index
, ch
->count
);
1399 spin_lock_irqsave(&moxa_lock
, flags
);
1400 if (!tty_hung_up_p(filp
))
1403 spin_unlock_irqrestore(&moxa_lock
, flags
);
1406 set_current_state(TASK_INTERRUPTIBLE
);
1407 if (tty_hung_up_p(filp
) ||
1408 !(ch
->asyncflags
& ASYNC_INITIALIZED
)) {
1409 #ifdef SERIAL_DO_RESTART
1410 if (ch
->asyncflags
& ASYNC_HUP_NOTIFY
)
1413 retval
= -ERESTARTSYS
;
1419 if (!(ch
->asyncflags
& ASYNC_CLOSING
) && (do_clocal
||
1423 if (signal_pending(current
)) {
1424 retval
= -ERESTARTSYS
;
1429 set_current_state(TASK_RUNNING
);
1430 remove_wait_queue(&ch
->open_wait
, &wait
);
1432 spin_lock_irqsave(&moxa_lock
, flags
);
1433 if (!tty_hung_up_p(filp
))
1436 spin_unlock_irqrestore(&moxa_lock
, flags
);
1437 pr_debug("block_til_ready after blocking: ttys%d, count = %d\n",
1438 tty
->index
, ch
->count
);
1441 /* FIXME: review to see if we need to use set_bit on these */
1442 ch
->asyncflags
|= ASYNC_NORMAL_ACTIVE
;
1446 static void moxa_setup_empty_event(struct tty_struct
*tty
)
1448 struct moxa_port
*ch
= tty
->driver_data
;
1449 unsigned long flags
;
1451 spin_lock_irqsave(&moxa_lock
, flags
);
1452 ch
->statusflags
|= EMPTYWAIT
;
1453 mod_timer(&ch
->emptyTimer
, jiffies
+ HZ
);
1454 spin_unlock_irqrestore(&moxa_lock
, flags
);
1457 static void moxa_check_xmit_empty(unsigned long data
)
1459 struct moxa_port
*ch
;
1461 ch
= (struct moxa_port
*) data
;
1462 if (ch
->tty
&& (ch
->statusflags
& EMPTYWAIT
)) {
1463 if (MoxaPortTxQueue(ch
) == 0) {
1464 ch
->statusflags
&= ~EMPTYWAIT
;
1465 tty_wakeup(ch
->tty
);
1468 mod_timer(&ch
->emptyTimer
, round_jiffies(jiffies
+ HZ
));
1470 ch
->statusflags
&= ~EMPTYWAIT
;
1473 static void moxa_shut_down(struct moxa_port
*ch
)
1475 struct tty_struct
*tp
;
1477 if (!(ch
->asyncflags
& ASYNC_INITIALIZED
))
1482 MoxaPortDisable(ch
);
1485 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1487 if (tp
->termios
->c_cflag
& HUPCL
)
1488 MoxaPortLineCtrl(ch
, 0, 0);
1490 ch
->asyncflags
&= ~ASYNC_INITIALIZED
;
1493 static void moxa_receive_data(struct moxa_port
*ch
)
1495 struct tty_struct
*tp
;
1496 struct ktermios
*ts
;
1497 unsigned long flags
;
1503 /**************************************************
1504 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1505 *****************************************************/
1507 MoxaPortFlushData(ch
, 0);
1510 spin_lock_irqsave(&moxa_lock
, flags
);
1511 MoxaPortReadData(ch
, tp
);
1512 spin_unlock_irqrestore(&moxa_lock
, flags
);
1513 tty_schedule_flip(tp
);
1522 int rxcnt
[MAX_PORTS
];
1523 int txcnt
[MAX_PORTS
];
1526 #define DCD_changed 0x01
1527 #define DCD_oldstate 0x80
1529 static int moxaLowWaterChk
;
1530 static struct mon_str moxaLog
;
1531 static int moxaFuncTout
= HZ
/ 2;
1533 static void moxafunc(void __iomem
*, int, ushort
);
1534 static void moxa_wait_finish(void __iomem
*);
1535 static void moxa_low_water_check(void __iomem
*);
1537 /*****************************************************************************
1538 * Driver level functions: *
1539 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1540 * 3. MoxaDriverPoll(void); *
1541 *****************************************************************************/
1543 #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1544 #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
1545 #define MOXA_GETDATACOUNT (MOXA + 23)
1546 #define MOXA_GET_IOQUEUE (MOXA + 27)
1547 #define MOXA_FLUSH_QUEUE (MOXA + 28)
1548 #define MOXA_GET_CONF (MOXA + 35) /* configuration */
1549 #define MOXA_GET_MAJOR (MOXA + 63)
1550 #define MOXA_GET_CUMAJOR (MOXA + 64)
1551 #define MOXA_GETMSTATUS (MOXA + 65)
1553 static void MoxaPortFlushData(struct moxa_port
*port
, int mode
)
1555 void __iomem
*ofsAddr
;
1556 if ((mode
< 0) || (mode
> 2))
1558 ofsAddr
= port
->tableAddr
;
1559 moxafunc(ofsAddr
, FC_FlushQueue
, mode
);
1561 port
->lowChkFlag
= 0;
1562 moxa_low_water_check(ofsAddr
);
1566 static int MoxaDriverIoctl(struct tty_struct
*tty
, unsigned int cmd
,
1569 struct moxa_port
*port
= tty
->driver_data
;
1572 void __user
*argp
= (void __user
*)arg
;
1574 if (tty
->index
== MAX_PORTS
) {
1575 if ((cmd
!= MOXA_GET_CONF
) && (cmd
!= MOXA_GETDATACOUNT
) &&
1576 (cmd
!= MOXA_GET_IOQUEUE
) && (cmd
!= MOXA_GET_MAJOR
) &&
1577 (cmd
!= MOXA_GET_CUMAJOR
) && (cmd
!= MOXA_GETMSTATUS
))
1581 case MOXA_GETDATACOUNT
:
1582 moxaLog
.tick
= jiffies
;
1583 if(copy_to_user(argp
, &moxaLog
, sizeof(struct mon_str
)))
1586 case MOXA_FLUSH_QUEUE
:
1587 MoxaPortFlushData(port
, arg
);
1589 case MOXA_GET_IOQUEUE
: {
1590 struct moxaq_str __user
*argm
= argp
;
1591 struct moxaq_str tmp
;
1593 for (i
= 0; i
< MAX_PORTS
; i
++, argm
++) {
1594 memset(&tmp
, 0, sizeof(tmp
));
1595 if (moxa_ports
[i
].chkPort
) {
1596 tmp
.inq
= MoxaPortRxQueue(&moxa_ports
[i
]);
1597 tmp
.outq
= MoxaPortTxQueue(&moxa_ports
[i
]);
1599 if (copy_to_user(argm
, &tmp
, sizeof(tmp
)))
1603 } case MOXA_GET_OQUEUE
:
1604 i
= MoxaPortTxQueue(port
);
1605 return put_user(i
, (unsigned long __user
*)argp
);
1606 case MOXA_GET_IQUEUE
:
1607 i
= MoxaPortRxQueue(port
);
1608 return put_user(i
, (unsigned long __user
*)argp
);
1609 case MOXA_GET_MAJOR
:
1610 if(copy_to_user(argp
, &ttymajor
, sizeof(int)))
1613 case MOXA_GET_CUMAJOR
:
1615 if(copy_to_user(argp
, &i
, sizeof(int)))
1618 case MOXA_GETMSTATUS
: {
1619 struct mxser_mstatus __user
*argm
= argp
;
1620 struct mxser_mstatus tmp
;
1621 struct moxa_port
*p
;
1623 for (i
= 0; i
< MAX_PORTS
; i
++, argm
++) {
1625 memset(&tmp
, 0, sizeof(tmp
));
1629 status
= MoxaPortLineStatus(p
);
1638 if (!p
->tty
|| !p
->tty
->termios
)
1639 tmp
.cflag
= p
->cflag
;
1641 tmp
.cflag
= p
->tty
->termios
->c_cflag
;
1643 if (copy_to_user(argm
, &tmp
, sizeof(tmp
)))
1650 return -ENOIOCTLCMD
;
1653 int MoxaDriverPoll(void)
1655 struct moxa_board_conf
*brd
;
1656 register ushort temp
;
1658 void __iomem
*ofsAddr
;
1664 for (card
= 0; card
< MAX_BOARDS
; card
++) {
1665 brd
= &moxa_boards
[card
];
1666 if (brd
->loadstat
== 0)
1668 if ((ports
= brd
->numPorts
) == 0)
1670 if (readb(brd
->intPend
) == 0xff) {
1671 ip
= brd
->intTable
+ readb(brd
->intNdx
);
1672 p
= card
* MAX_PORTS_PER_BOARD
;
1674 for (port
= 0; port
< ports
; port
+= 2, p
++) {
1675 if ((temp
= readw(ip
+ port
)) != 0) {
1676 writew(0, ip
+ port
);
1677 ofsAddr
= moxa_ports
[p
].tableAddr
;
1679 writew(readw(ofsAddr
+ HostStat
) & ~WakeupTx
, ofsAddr
+ HostStat
);
1680 if (temp
& IntrBreak
) {
1681 moxa_ports
[p
].breakCnt
++;
1683 if (temp
& IntrLine
) {
1684 if (readb(ofsAddr
+ FlagStat
) & DCD_state
) {
1685 if ((moxa_ports
[p
].DCDState
& DCD_oldstate
) == 0)
1686 moxa_ports
[p
].DCDState
= (DCD_oldstate
|
1689 if (moxa_ports
[p
].DCDState
& DCD_oldstate
)
1690 moxa_ports
[p
].DCDState
= DCD_changed
;
1695 writeb(0, brd
->intPend
);
1697 if (moxaLowWaterChk
) {
1698 p
= card
* MAX_PORTS_PER_BOARD
;
1699 for (port
= 0; port
< ports
; port
++, p
++) {
1700 if (moxa_ports
[p
].lowChkFlag
) {
1701 moxa_ports
[p
].lowChkFlag
= 0;
1702 ofsAddr
= moxa_ports
[p
].tableAddr
;
1703 moxa_low_water_check(ofsAddr
);
1708 moxaLowWaterChk
= 0;
1712 /*****************************************************************************
1713 * Card level function: *
1714 * 1. MoxaPortsOfCard(int cardno); *
1715 *****************************************************************************/
1716 int MoxaPortsOfCard(int cardno
)
1719 if (moxa_boards
[cardno
].boardType
== 0)
1721 return (moxa_boards
[cardno
].numPorts
);
1724 /*****************************************************************************
1725 * Port level functions: *
1726 * 1. MoxaPortIsValid(int port); *
1727 * 2. MoxaPortEnable(int port); *
1728 * 3. MoxaPortDisable(int port); *
1729 * 4. MoxaPortGetMaxBaud(int port); *
1730 * 6. MoxaPortSetBaud(int port, long baud); *
1731 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1732 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1733 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1734 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1735 * 12. MoxaPortLineStatus(int port); *
1736 * 13. MoxaPortDCDChange(int port); *
1737 * 14. MoxaPortDCDON(int port); *
1738 * 15. MoxaPortFlushData(int port, int mode); *
1739 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1740 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1741 * 20. MoxaPortTxQueue(int port); *
1742 * 21. MoxaPortTxFree(int port); *
1743 * 22. MoxaPortRxQueue(int port); *
1744 * 24. MoxaPortTxDisable(int port); *
1745 * 25. MoxaPortTxEnable(int port); *
1746 * 27. MoxaPortResetBrkCnt(int port); *
1747 * 30. MoxaPortSendBreak(int port, int ticks); *
1748 *****************************************************************************/
1750 * Moxa Port Number Description:
1752 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1753 * the port number using in MOXA driver functions will be 0 to 31 for
1754 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1755 * to 127 for fourth. For example, if you setup three MOXA boards,
1756 * first board is C218, second board is C320-16 and third board is
1757 * C320-32. The port number of first board (C218 - 8 ports) is from
1758 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1759 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1760 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1761 * 127 will be invalid.
1764 * Moxa Functions Description:
1766 * Function 1: Driver initialization routine, this routine must be
1767 * called when initialized driver.
1769 * void MoxaDriverInit();
1772 * Function 2: Moxa driver private IOCTL command processing.
1774 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1776 * unsigned int cmd : IOCTL command
1777 * unsigned long arg : IOCTL argument
1778 * int port : port number (0 - 127)
1785 * Function 3: Moxa driver polling process routine.
1787 * int MoxaDriverPoll(void);
1789 * return: 0 ; polling O.K.
1790 * -1 : no any Moxa card.
1793 * Function 4: Get the ports of this card.
1795 * int MoxaPortsOfCard(int cardno);
1797 * int cardno : card number (0 - 3)
1799 * return: 0 : this card is invalid
1803 * Function 5: Check this port is valid or invalid
1805 * int MoxaPortIsValid(int port);
1806 * int port : port number (0 - 127, ref port description)
1808 * return: 0 : this port is invalid
1809 * 1 : this port is valid
1812 * Function 6: Enable this port to start Tx/Rx data.
1814 * void MoxaPortEnable(int port);
1815 * int port : port number (0 - 127)
1818 * Function 7: Disable this port
1820 * void MoxaPortDisable(int port);
1821 * int port : port number (0 - 127)
1824 * Function 8: Get the maximun available baud rate of this port.
1826 * long MoxaPortGetMaxBaud(int port);
1827 * int port : port number (0 - 127)
1829 * return: 0 : this port is invalid
1830 * 38400/57600/115200 bps
1833 * Function 10: Setting baud rate of this port.
1835 * long MoxaPortSetBaud(int port, long baud);
1836 * int port : port number (0 - 127)
1837 * long baud : baud rate (50 - 115200)
1839 * return: 0 : this port is invalid or baud < 50
1840 * 50 - 115200 : the real baud rate set to the port, if
1841 * the argument baud is large than maximun
1842 * available baud rate, the real setting
1843 * baud rate will be the maximun baud rate.
1846 * Function 12: Configure the port.
1848 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1849 * int port : port number (0 - 127)
1850 * struct ktermios * termio : termio structure pointer
1851 * speed_t baud : baud rate
1853 * return: -1 : this port is invalid or termio == NULL
1857 * Function 13: Get the DTR/RTS state of this port.
1859 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1860 * int port : port number (0 - 127)
1861 * int * dtrState : pointer to INT to receive the current DTR
1862 * state. (if NULL, this function will not
1863 * write to this address)
1864 * int * rtsState : pointer to INT to receive the current RTS
1865 * state. (if NULL, this function will not
1866 * write to this address)
1868 * return: -1 : this port is invalid
1872 * Function 14: Setting the DTR/RTS output state of this port.
1874 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1875 * int port : port number (0 - 127)
1876 * int dtrState : DTR output state (0: off, 1: on)
1877 * int rtsState : RTS output state (0: off, 1: on)
1880 * Function 15: Setting the flow control of this port.
1882 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1883 * int txFlow,int xany);
1884 * int port : port number (0 - 127)
1885 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1886 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1887 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1888 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1889 * int xany : S/W XANY flow control (0: no, 1: yes)
1892 * Function 16: Get ths line status of this port
1894 * int MoxaPortLineStatus(int port);
1895 * int port : port number (0 - 127)
1897 * return: Bit 0 - CTS state (0: off, 1: on)
1898 * Bit 1 - DSR state (0: off, 1: on)
1899 * Bit 2 - DCD state (0: off, 1: on)
1902 * Function 17: Check the DCD state has changed since the last read
1905 * int MoxaPortDCDChange(int port);
1906 * int port : port number (0 - 127)
1908 * return: 0 : no changed
1909 * 1 : DCD has changed
1912 * Function 18: Check ths current DCD state is ON or not.
1914 * int MoxaPortDCDON(int port);
1915 * int port : port number (0 - 127)
1917 * return: 0 : DCD off
1921 * Function 19: Flush the Rx/Tx buffer data of this port.
1923 * void MoxaPortFlushData(int port, int mode);
1924 * int port : port number (0 - 127)
1926 * 0 : flush the Rx buffer
1927 * 1 : flush the Tx buffer
1928 * 2 : flush the Rx and Tx buffer
1931 * Function 20: Write data.
1933 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1934 * int port : port number (0 - 127)
1935 * unsigned char * buffer : pointer to write data buffer.
1936 * int length : write data length
1938 * return: 0 - length : real write data length
1941 * Function 21: Read data.
1943 * int MoxaPortReadData(int port, struct tty_struct *tty);
1944 * int port : port number (0 - 127)
1945 * struct tty_struct *tty : tty for data
1947 * return: 0 - length : real read data length
1950 * Function 24: Get the Tx buffer current queued data bytes
1952 * int MoxaPortTxQueue(int port);
1953 * int port : port number (0 - 127)
1955 * return: .. : Tx buffer current queued data bytes
1958 * Function 25: Get the Tx buffer current free space
1960 * int MoxaPortTxFree(int port);
1961 * int port : port number (0 - 127)
1963 * return: .. : Tx buffer current free space
1966 * Function 26: Get the Rx buffer current queued data bytes
1968 * int MoxaPortRxQueue(int port);
1969 * int port : port number (0 - 127)
1971 * return: .. : Rx buffer current queued data bytes
1974 * Function 28: Disable port data transmission.
1976 * void MoxaPortTxDisable(int port);
1977 * int port : port number (0 - 127)
1980 * Function 29: Enable port data transmission.
1982 * void MoxaPortTxEnable(int port);
1983 * int port : port number (0 - 127)
1986 * Function 31: Get the received BREAK signal count and reset it.
1988 * int MoxaPortResetBrkCnt(int port);
1989 * int port : port number (0 - 127)
1991 * return: 0 - .. : BREAK signal count
1994 * Function 34: Send out a BREAK signal.
1996 * void MoxaPortSendBreak(int port, int ms100);
1997 * int port : port number (0 - 127)
1998 * int ms100 : break signal time interval.
1999 * unit: 100 mini-second. if ms100 == 0, it will
2000 * send out a about 250 ms BREAK signal.
2003 static int MoxaPortIsValid(int port
)
2007 if (moxa_ports
[port
].chkPort
== 0)
2012 static void MoxaPortEnable(struct moxa_port
*port
)
2014 void __iomem
*ofsAddr
;
2015 short lowwater
= 512;
2017 ofsAddr
= port
->tableAddr
;
2018 writew(lowwater
, ofsAddr
+ Low_water
);
2020 if (port
->board
->boardType
== MOXA_BOARD_C320_ISA
||
2021 port
->board
->boardType
== MOXA_BOARD_C320_PCI
) {
2022 moxafunc(ofsAddr
, FC_SetBreakIrq
, 0);
2024 writew(readw(ofsAddr
+ HostStat
) | WakeupBreak
, ofsAddr
+ HostStat
);
2027 moxafunc(ofsAddr
, FC_SetLineIrq
, Magic_code
);
2028 moxafunc(ofsAddr
, FC_FlushQueue
, 2);
2030 moxafunc(ofsAddr
, FC_EnableCH
, Magic_code
);
2031 MoxaPortLineStatus(port
);
2034 static void MoxaPortDisable(struct moxa_port
*port
)
2036 void __iomem
*ofsAddr
= port
->tableAddr
;
2038 moxafunc(ofsAddr
, FC_SetFlowCtl
, 0); /* disable flow control */
2039 moxafunc(ofsAddr
, FC_ClrLineIrq
, Magic_code
);
2040 writew(0, ofsAddr
+ HostStat
);
2041 moxafunc(ofsAddr
, FC_DisableCH
, Magic_code
);
2044 static long MoxaPortGetMaxBaud(struct moxa_port
*port
)
2046 if (port
->board
->boardType
== MOXA_BOARD_C320_ISA
||
2047 port
->board
->boardType
== MOXA_BOARD_C320_PCI
)
2054 static long MoxaPortSetBaud(struct moxa_port
*port
, long baud
)
2056 void __iomem
*ofsAddr
;
2060 if ((baud
< 50L) || ((max
= MoxaPortGetMaxBaud(port
)) == 0))
2062 ofsAddr
= port
->tableAddr
;
2066 clock
= 614400L; /* for 9.8304 Mhz : max. 38400 bps */
2067 else if (max
== 57600L)
2068 clock
= 691200L; /* for 11.0592 Mhz : max. 57600 bps */
2070 clock
= 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2072 moxafunc(ofsAddr
, FC_SetBaud
, val
);
2077 static int MoxaPortSetTermio(struct moxa_port
*port
, struct ktermios
*termio
,
2080 void __iomem
*ofsAddr
;
2084 if (port
->chkPort
== 0 || termio
== 0)
2086 ofsAddr
= port
->tableAddr
;
2087 cflag
= termio
->c_cflag
; /* termio->c_cflag */
2089 mode
= termio
->c_cflag
& CSIZE
;
2092 else if (mode
== CS6
)
2094 else if (mode
== CS7
)
2096 else if (mode
== CS8
)
2099 if (termio
->c_cflag
& CSTOPB
) {
2107 if (termio
->c_cflag
& PARENB
) {
2108 if (termio
->c_cflag
& PARODD
)
2115 moxafunc(ofsAddr
, FC_SetDataMode
, (ushort
) mode
);
2117 if (port
->board
->boardType
== MOXA_BOARD_C320_ISA
||
2118 port
->board
->boardType
== MOXA_BOARD_C320_PCI
) {
2119 if (baud
>= 921600L)
2122 baud
= MoxaPortSetBaud(port
, baud
);
2124 if (termio
->c_iflag
& (IXON
| IXOFF
| IXANY
)) {
2125 writeb(termio
->c_cc
[VSTART
], ofsAddr
+ FuncArg
);
2126 writeb(termio
->c_cc
[VSTOP
], ofsAddr
+ FuncArg1
);
2127 writeb(FC_SetXonXoff
, ofsAddr
+ FuncCode
);
2128 moxa_wait_finish(ofsAddr
);
2134 static int MoxaPortGetLineOut(struct moxa_port
*port
, int *dtrState
,
2138 if (!MoxaPortIsValid(port
->tty
->index
))
2141 *dtrState
= !!(port
->lineCtrl
& DTR_ON
);
2143 *rtsState
= !!(port
->lineCtrl
& RTS_ON
);
2148 static void MoxaPortLineCtrl(struct moxa_port
*port
, int dtr
, int rts
)
2156 port
->lineCtrl
= mode
;
2157 moxafunc(port
->tableAddr
, FC_LineControl
, mode
);
2160 static void MoxaPortFlowCtrl(struct moxa_port
*port
, int rts
, int cts
,
2161 int txflow
, int rxflow
, int txany
)
2166 mode
|= RTS_FlowCtl
;
2168 mode
|= CTS_FlowCtl
;
2175 moxafunc(port
->tableAddr
, FC_SetFlowCtl
, mode
);
2178 static int MoxaPortLineStatus(struct moxa_port
*port
)
2180 void __iomem
*ofsAddr
;
2183 ofsAddr
= port
->tableAddr
;
2184 if (port
->board
->boardType
== MOXA_BOARD_C320_ISA
||
2185 port
->board
->boardType
== MOXA_BOARD_C320_PCI
) {
2186 moxafunc(ofsAddr
, FC_LineStatus
, 0);
2187 val
= readw(ofsAddr
+ FuncArg
);
2189 val
= readw(ofsAddr
+ FlagStat
) >> 4;
2194 if ((port
->DCDState
& DCD_oldstate
) == 0)
2195 port
->DCDState
= (DCD_oldstate
| DCD_changed
);
2197 if (port
->DCDState
& DCD_oldstate
)
2198 port
->DCDState
= DCD_changed
;
2204 static int MoxaPortDCDChange(struct moxa_port
*port
)
2208 if (port
->chkPort
== 0)
2211 port
->DCDState
&= ~DCD_changed
;
2216 static int MoxaPortDCDON(struct moxa_port
*port
)
2220 if (port
->chkPort
== 0)
2222 if (port
->DCDState
& DCD_oldstate
)
2229 static int MoxaPortWriteData(struct moxa_port
*port
, unsigned char *buffer
,
2235 ushort head
, tx_mask
, spage
, epage
;
2236 ushort pageno
, pageofs
, bufhead
;
2237 void __iomem
*baseAddr
, *ofsAddr
, *ofs
;
2239 ofsAddr
= port
->tableAddr
;
2240 baseAddr
= port
->board
->basemem
;
2241 tx_mask
= readw(ofsAddr
+ TX_mask
);
2242 spage
= readw(ofsAddr
+ Page_txb
);
2243 epage
= readw(ofsAddr
+ EndPage_txb
);
2244 tail
= readw(ofsAddr
+ TXwptr
);
2245 head
= readw(ofsAddr
+ TXrptr
);
2246 c
= (head
> tail
) ? (head
- tail
- 1)
2247 : (head
- tail
+ tx_mask
);
2250 moxaLog
.txcnt
[port
->tty
->index
] += c
;
2252 if (spage
== epage
) {
2253 bufhead
= readw(ofsAddr
+ Ofs_txb
);
2254 writew(spage
, baseAddr
+ Control_reg
);
2257 len
= head
- tail
- 1;
2259 len
= tx_mask
+ 1 - tail
;
2260 len
= (c
> len
) ? len
: c
;
2261 ofs
= baseAddr
+ DynPage_addr
+ bufhead
+ tail
;
2262 for (i
= 0; i
< len
; i
++)
2263 writeb(*buffer
++, ofs
+ i
);
2264 tail
= (tail
+ len
) & tx_mask
;
2267 writew(tail
, ofsAddr
+ TXwptr
);
2270 pageno
= spage
+ (tail
>> 13);
2271 pageofs
= tail
& Page_mask
;
2273 cnt
= Page_size
- pageofs
;
2277 writeb(pageno
, baseAddr
+ Control_reg
);
2278 ofs
= baseAddr
+ DynPage_addr
+ pageofs
;
2279 for (i
= 0; i
< cnt
; i
++)
2280 writeb(*buffer
++, ofs
+ i
);
2282 writew((tail
+ len
) & tx_mask
, ofsAddr
+ TXwptr
);
2285 if (++pageno
== epage
)
2290 writeb(1, ofsAddr
+ CD180TXirq
); /* start to send */
2294 static int MoxaPortReadData(struct moxa_port
*port
, struct tty_struct
*tty
)
2296 register ushort head
, pageofs
;
2297 int i
, count
, cnt
, len
, total
, remain
;
2298 ushort tail
, rx_mask
, spage
, epage
;
2299 ushort pageno
, bufhead
;
2300 void __iomem
*baseAddr
, *ofsAddr
, *ofs
;
2302 ofsAddr
= port
->tableAddr
;
2303 baseAddr
= port
->board
->basemem
;
2304 head
= readw(ofsAddr
+ RXrptr
);
2305 tail
= readw(ofsAddr
+ RXwptr
);
2306 rx_mask
= readw(ofsAddr
+ RX_mask
);
2307 spage
= readw(ofsAddr
+ Page_rxb
);
2308 epage
= readw(ofsAddr
+ EndPage_rxb
);
2309 count
= (tail
>= head
) ? (tail
- head
)
2310 : (tail
- head
+ rx_mask
+ 1);
2315 remain
= count
- total
;
2316 moxaLog
.rxcnt
[port
->tty
->index
] += total
;
2318 if (spage
== epage
) {
2319 bufhead
= readw(ofsAddr
+ Ofs_rxb
);
2320 writew(spage
, baseAddr
+ Control_reg
);
2325 len
= rx_mask
+ 1 - head
;
2326 len
= (count
> len
) ? len
: count
;
2327 ofs
= baseAddr
+ DynPage_addr
+ bufhead
+ head
;
2328 for (i
= 0; i
< len
; i
++)
2329 tty_insert_flip_char(tty
, readb(ofs
+ i
), TTY_NORMAL
);
2330 head
= (head
+ len
) & rx_mask
;
2333 writew(head
, ofsAddr
+ RXrptr
);
2336 pageno
= spage
+ (head
>> 13);
2337 pageofs
= head
& Page_mask
;
2339 cnt
= Page_size
- pageofs
;
2343 writew(pageno
, baseAddr
+ Control_reg
);
2344 ofs
= baseAddr
+ DynPage_addr
+ pageofs
;
2345 for (i
= 0; i
< cnt
; i
++)
2346 tty_insert_flip_char(tty
, readb(ofs
+ i
), TTY_NORMAL
);
2348 writew((head
+ len
) & rx_mask
, ofsAddr
+ RXrptr
);
2351 if (++pageno
== epage
)
2356 if ((readb(ofsAddr
+ FlagStat
) & Xoff_state
) && (remain
< LowWater
)) {
2357 moxaLowWaterChk
= 1;
2358 port
->lowChkFlag
= 1;
2364 static int MoxaPortTxQueue(struct moxa_port
*port
)
2366 void __iomem
*ofsAddr
= port
->tableAddr
;
2367 ushort rptr
, wptr
, mask
;
2370 rptr
= readw(ofsAddr
+ TXrptr
);
2371 wptr
= readw(ofsAddr
+ TXwptr
);
2372 mask
= readw(ofsAddr
+ TX_mask
);
2373 len
= (wptr
- rptr
) & mask
;
2377 static int MoxaPortTxFree(struct moxa_port
*port
)
2379 void __iomem
*ofsAddr
= port
->tableAddr
;
2380 ushort rptr
, wptr
, mask
;
2383 rptr
= readw(ofsAddr
+ TXrptr
);
2384 wptr
= readw(ofsAddr
+ TXwptr
);
2385 mask
= readw(ofsAddr
+ TX_mask
);
2386 len
= mask
- ((wptr
- rptr
) & mask
);
2390 static int MoxaPortRxQueue(struct moxa_port
*port
)
2392 void __iomem
*ofsAddr
= port
->tableAddr
;
2393 ushort rptr
, wptr
, mask
;
2396 rptr
= readw(ofsAddr
+ RXrptr
);
2397 wptr
= readw(ofsAddr
+ RXwptr
);
2398 mask
= readw(ofsAddr
+ RX_mask
);
2399 len
= (wptr
- rptr
) & mask
;
2404 static void MoxaPortTxDisable(struct moxa_port
*port
)
2406 moxafunc(port
->tableAddr
, FC_SetXoffState
, Magic_code
);
2409 static void MoxaPortTxEnable(struct moxa_port
*port
)
2411 moxafunc(port
->tableAddr
, FC_SetXonState
, Magic_code
);
2415 static int MoxaPortResetBrkCnt(struct moxa_port
*port
)
2418 cnt
= port
->breakCnt
;
2424 static void MoxaPortSendBreak(struct moxa_port
*port
, int ms100
)
2426 void __iomem
*ofsAddr
= port
->tableAddr
;
2429 moxafunc(ofsAddr
, FC_SendBreak
, Magic_code
);
2432 moxafunc(ofsAddr
, FC_SendBreak
, Magic_code
);
2435 moxafunc(ofsAddr
, FC_StopBreak
, Magic_code
);
2438 static int moxa_get_serial_info(struct moxa_port
*info
,
2439 struct serial_struct __user
*retinfo
)
2441 struct serial_struct tmp
;
2443 memset(&tmp
, 0, sizeof(tmp
));
2444 tmp
.type
= info
->type
;
2445 tmp
.line
= info
->tty
->index
;
2448 tmp
.flags
= info
->asyncflags
;
2449 tmp
.baud_base
= 921600;
2450 tmp
.close_delay
= info
->close_delay
;
2451 tmp
.custom_divisor
= 0;
2453 if(copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
2459 static int moxa_set_serial_info(struct moxa_port
*info
,
2460 struct serial_struct __user
*new_info
)
2462 struct serial_struct new_serial
;
2464 if(copy_from_user(&new_serial
, new_info
, sizeof(new_serial
)))
2467 if ((new_serial
.irq
!= 0) ||
2468 (new_serial
.port
!= 0) ||
2469 // (new_serial.type != info->type) ||
2470 (new_serial
.custom_divisor
!= 0) ||
2471 (new_serial
.baud_base
!= 921600))
2474 if (!capable(CAP_SYS_ADMIN
)) {
2475 if (((new_serial
.flags
& ~ASYNC_USR_MASK
) !=
2476 (info
->asyncflags
& ~ASYNC_USR_MASK
)))
2479 info
->close_delay
= new_serial
.close_delay
* HZ
/ 100;
2482 new_serial
.flags
= (new_serial
.flags
& ~ASYNC_FLAGS
);
2483 new_serial
.flags
|= (info
->asyncflags
& ASYNC_FLAGS
);
2485 if (new_serial
.type
== PORT_16550A
) {
2486 MoxaSetFifo(info
, 1);
2488 MoxaSetFifo(info
, 0);
2491 info
->type
= new_serial
.type
;
2497 /*****************************************************************************
2498 * Static local functions: *
2499 *****************************************************************************/
2500 static void moxafunc(void __iomem
*ofsAddr
, int cmd
, ushort arg
)
2503 writew(arg
, ofsAddr
+ FuncArg
);
2504 writew(cmd
, ofsAddr
+ FuncCode
);
2505 moxa_wait_finish(ofsAddr
);
2508 static void moxa_wait_finish(void __iomem
*ofsAddr
)
2513 while (readw(ofsAddr
+ FuncCode
) != 0) {
2515 if ((j
- i
) > moxaFuncTout
) {
2521 static void moxa_low_water_check(void __iomem
*ofsAddr
)
2524 ushort rptr
, wptr
, mask
;
2526 if (readb(ofsAddr
+ FlagStat
) & Xoff_state
) {
2527 rptr
= readw(ofsAddr
+ RXrptr
);
2528 wptr
= readw(ofsAddr
+ RXwptr
);
2529 mask
= readw(ofsAddr
+ RX_mask
);
2530 len
= (wptr
- rptr
) & mask
;
2531 if (len
<= Low_water
)
2532 moxafunc(ofsAddr
, FC_SendXon
, 0);
2536 static void MoxaSetFifo(struct moxa_port
*port
, int enable
)
2538 void __iomem
*ofsAddr
= port
->tableAddr
;
2541 moxafunc(ofsAddr
, FC_SetRxFIFOTrig
, 0);
2542 moxafunc(ofsAddr
, FC_SetTxFIFOCnt
, 1);
2544 moxafunc(ofsAddr
, FC_SetRxFIFOTrig
, 3);
2545 moxafunc(ofsAddr
, FC_SetTxFIFOCnt
, 16);