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
;
134 unsigned short closing_wait
;
137 long event
; /* long req'd for set_bit --RR */
139 unsigned long statusflags
;
140 struct tty_struct
*tty
;
142 wait_queue_head_t open_wait
;
143 struct completion close_wait
;
145 struct timer_list emptyTimer
;
149 void __iomem
*tableAddr
;
158 #define TXSTOPPED 0x1
160 #define EMPTYWAIT 0x4
163 #define SERIAL_DO_RESTART
165 #define WAKEUP_CHARS 256
167 static int ttymajor
= MOXAMAJOR
;
169 /* Variables for insmod */
171 static unsigned long baseaddr
[MAX_BOARDS
];
172 static unsigned int type
[MAX_BOARDS
];
173 static unsigned int numports
[MAX_BOARDS
];
176 MODULE_AUTHOR("William Chen");
177 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
178 MODULE_LICENSE("GPL");
180 module_param_array(type
, uint
, NULL
, 0);
181 MODULE_PARM_DESC(type
, "card type: C218=2, C320=4");
182 module_param_array(baseaddr
, ulong
, NULL
, 0);
183 MODULE_PARM_DESC(baseaddr
, "base address");
184 module_param_array(numports
, uint
, NULL
, 0);
185 MODULE_PARM_DESC(numports
, "numports (ignored for C218)");
187 module_param(ttymajor
, int, 0);
192 static int moxa_open(struct tty_struct
*, struct file
*);
193 static void moxa_close(struct tty_struct
*, struct file
*);
194 static int moxa_write(struct tty_struct
*, const unsigned char *, int);
195 static int moxa_write_room(struct tty_struct
*);
196 static void moxa_flush_buffer(struct tty_struct
*);
197 static int moxa_chars_in_buffer(struct tty_struct
*);
198 static void moxa_flush_chars(struct tty_struct
*);
199 static void moxa_put_char(struct tty_struct
*, unsigned char);
200 static int moxa_ioctl(struct tty_struct
*, struct file
*, unsigned int, unsigned long);
201 static void moxa_throttle(struct tty_struct
*);
202 static void moxa_unthrottle(struct tty_struct
*);
203 static void moxa_set_termios(struct tty_struct
*, struct ktermios
*);
204 static void moxa_stop(struct tty_struct
*);
205 static void moxa_start(struct tty_struct
*);
206 static void moxa_hangup(struct tty_struct
*);
207 static int moxa_tiocmget(struct tty_struct
*tty
, struct file
*file
);
208 static int moxa_tiocmset(struct tty_struct
*tty
, struct file
*file
,
209 unsigned int set
, unsigned int clear
);
210 static void moxa_poll(unsigned long);
211 static void moxa_set_tty_param(struct tty_struct
*, struct ktermios
*);
212 static int moxa_block_till_ready(struct tty_struct
*, struct file
*,
214 static void moxa_setup_empty_event(struct tty_struct
*);
215 static void moxa_check_xmit_empty(unsigned long);
216 static void moxa_shut_down(struct moxa_port
*);
217 static void moxa_receive_data(struct moxa_port
*);
219 * moxa board interface functions:
221 static int MoxaDriverIoctl(struct tty_struct
*, unsigned int, unsigned long);
222 static int MoxaDriverPoll(void);
223 static int MoxaPortsOfCard(int);
224 static int MoxaPortIsValid(int);
225 static void MoxaPortEnable(struct moxa_port
*);
226 static void MoxaPortDisable(struct moxa_port
*);
227 static int MoxaPortSetTermio(struct moxa_port
*, struct ktermios
*, speed_t
);
228 static int MoxaPortGetLineOut(struct moxa_port
*, int *, int *);
229 static void MoxaPortLineCtrl(struct moxa_port
*, int, int);
230 static void MoxaPortFlowCtrl(struct moxa_port
*, int, int, int, int, int);
231 static int MoxaPortLineStatus(struct moxa_port
*);
232 static int MoxaPortDCDChange(struct moxa_port
*);
233 static int MoxaPortDCDON(struct moxa_port
*);
234 static void MoxaPortFlushData(struct moxa_port
*, int);
235 static int MoxaPortWriteData(struct moxa_port
*, unsigned char *, int);
236 static int MoxaPortReadData(struct moxa_port
*, struct tty_struct
*tty
);
237 static int MoxaPortTxQueue(struct moxa_port
*);
238 static int MoxaPortRxQueue(struct moxa_port
*);
239 static int MoxaPortTxFree(struct moxa_port
*);
240 static void MoxaPortTxDisable(struct moxa_port
*);
241 static void MoxaPortTxEnable(struct moxa_port
*);
242 static int MoxaPortResetBrkCnt(struct moxa_port
*);
243 static void MoxaPortSendBreak(struct moxa_port
*, int);
244 static int moxa_get_serial_info(struct moxa_port
*, struct serial_struct __user
*);
245 static int moxa_set_serial_info(struct moxa_port
*, struct serial_struct __user
*);
246 static void MoxaSetFifo(struct moxa_port
*port
, int enable
);
248 static const struct tty_operations moxa_ops
= {
252 .write_room
= moxa_write_room
,
253 .flush_buffer
= moxa_flush_buffer
,
254 .chars_in_buffer
= moxa_chars_in_buffer
,
255 .flush_chars
= moxa_flush_chars
,
256 .put_char
= moxa_put_char
,
258 .throttle
= moxa_throttle
,
259 .unthrottle
= moxa_unthrottle
,
260 .set_termios
= moxa_set_termios
,
263 .hangup
= moxa_hangup
,
264 .tiocmget
= moxa_tiocmget
,
265 .tiocmset
= moxa_tiocmset
,
268 static struct tty_driver
*moxaDriver
;
269 static struct moxa_port moxa_ports
[MAX_PORTS
];
270 static DEFINE_TIMER(moxaTimer
, moxa_poll
, 0, 0);
271 static DEFINE_SPINLOCK(moxa_lock
);
273 static int moxa_check_fw_model(struct moxa_board_conf
*brd
, u8 model
)
275 switch (brd
->boardType
) {
276 case MOXA_BOARD_C218_ISA
:
277 case MOXA_BOARD_C218_PCI
:
281 case MOXA_BOARD_CP204J
:
295 static int moxa_check_fw(const void *ptr
)
297 const __le16
*lptr
= ptr
;
299 if (*lptr
!= cpu_to_le16(0x7980))
305 static int moxa_load_bios(struct moxa_board_conf
*brd
, const u8
*buf
,
308 void __iomem
*baseAddr
= brd
->basemem
;
311 writeb(HW_reset
, baseAddr
+ Control_reg
); /* reset */
313 memset_io(baseAddr
, 0, 4096);
314 memcpy_toio(baseAddr
, buf
, len
); /* download BIOS */
315 writeb(0, baseAddr
+ Control_reg
); /* restart */
319 switch (brd
->boardType
) {
320 case MOXA_BOARD_C218_ISA
:
321 case MOXA_BOARD_C218_PCI
:
322 tmp
= readw(baseAddr
+ C218_key
);
323 if (tmp
!= C218_KeyCode
)
326 case MOXA_BOARD_CP204J
:
327 tmp
= readw(baseAddr
+ C218_key
);
328 if (tmp
!= CP204J_KeyCode
)
332 tmp
= readw(baseAddr
+ C320_key
);
333 if (tmp
!= C320_KeyCode
)
335 tmp
= readw(baseAddr
+ C320_status
);
336 if (tmp
!= STS_init
) {
337 printk(KERN_ERR
"moxa: bios upload failed -- CPU/Basic "
338 "module not found\n");
346 printk(KERN_ERR
"moxa: bios upload failed -- board not found\n");
350 static int moxa_load_320b(struct moxa_board_conf
*brd
, const u8
*ptr
,
353 void __iomem
*baseAddr
= brd
->basemem
;
356 printk(KERN_ERR
"moxa: invalid 320 bios -- too short\n");
360 writew(len
- 7168 - 2, baseAddr
+ C320bapi_len
);
361 writeb(1, baseAddr
+ Control_reg
); /* Select Page 1 */
362 memcpy_toio(baseAddr
+ DynPage_addr
, ptr
, 7168);
363 writeb(2, baseAddr
+ Control_reg
); /* Select Page 2 */
364 memcpy_toio(baseAddr
+ DynPage_addr
, ptr
+ 7168, len
- 7168);
369 static int moxa_real_load_code(struct moxa_board_conf
*brd
, const void *ptr
,
372 void __iomem
*baseAddr
= brd
->basemem
;
373 const u16
*uptr
= ptr
;
374 size_t wlen
, len2
, j
;
375 unsigned long key
, loadbuf
, loadlen
, checksum
, checksum_ok
;
376 unsigned int i
, retry
, c320
;
379 c320
= brd
->boardType
== MOXA_BOARD_C320_PCI
||
380 brd
->boardType
== MOXA_BOARD_C320_ISA
;
381 keycode
= (brd
->boardType
== MOXA_BOARD_CP204J
) ? CP204J_KeyCode
:
384 switch (brd
->boardType
) {
385 case MOXA_BOARD_CP204J
:
386 case MOXA_BOARD_C218_ISA
:
387 case MOXA_BOARD_C218_PCI
:
389 loadbuf
= C218_LoadBuf
;
390 loadlen
= C218DLoad_len
;
391 checksum
= C218check_sum
;
392 checksum_ok
= C218chksum_ok
;
396 keycode
= C320_KeyCode
;
397 loadbuf
= C320_LoadBuf
;
398 loadlen
= C320DLoad_len
;
399 checksum
= C320check_sum
;
400 checksum_ok
= C320chksum_ok
;
406 for (i
= 0; i
< wlen
; i
++)
407 usum
+= le16_to_cpu(uptr
[i
]);
413 len2
= (wlen
> 2048) ? 2048 : wlen
;
415 memcpy_toio(baseAddr
+ loadbuf
, ptr
+ j
, len2
<< 1);
418 writew(len2
, baseAddr
+ loadlen
);
419 writew(0, baseAddr
+ key
);
420 for (i
= 0; i
< 100; i
++) {
421 if (readw(baseAddr
+ key
) == keycode
)
425 if (readw(baseAddr
+ key
) != keycode
)
428 writew(0, baseAddr
+ loadlen
);
429 writew(usum
, baseAddr
+ checksum
);
430 writew(0, baseAddr
+ key
);
431 for (i
= 0; i
< 100; i
++) {
432 if (readw(baseAddr
+ key
) == keycode
)
437 } while ((readb(baseAddr
+ checksum_ok
) != 1) && (retry
< 3));
438 if (readb(baseAddr
+ checksum_ok
) != 1)
441 writew(0, baseAddr
+ key
);
442 for (i
= 0; i
< 600; i
++) {
443 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
447 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
451 if (brd
->busType
== MOXA_BUS_TYPE_PCI
) { /* ASIC board */
452 writew(0x3800, baseAddr
+ TMS320_PORT1
);
453 writew(0x3900, baseAddr
+ TMS320_PORT2
);
454 writew(28499, baseAddr
+ TMS320_CLOCK
);
456 writew(0x3200, baseAddr
+ TMS320_PORT1
);
457 writew(0x3400, baseAddr
+ TMS320_PORT2
);
458 writew(19999, baseAddr
+ TMS320_CLOCK
);
461 writew(1, baseAddr
+ Disable_IRQ
);
462 writew(0, baseAddr
+ Magic_no
);
463 for (i
= 0; i
< 500; i
++) {
464 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
468 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
472 j
= readw(baseAddr
+ Module_cnt
);
475 brd
->numPorts
= j
* 8;
476 writew(j
, baseAddr
+ Module_no
);
477 writew(0, baseAddr
+ Magic_no
);
478 for (i
= 0; i
< 600; i
++) {
479 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
483 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
487 brd
->intNdx
= baseAddr
+ IRQindex
;
488 brd
->intPend
= baseAddr
+ IRQpending
;
489 brd
->intTable
= baseAddr
+ IRQtable
;
494 static int moxa_load_code(struct moxa_board_conf
*brd
, const void *ptr
,
497 void __iomem
*ofsAddr
, *baseAddr
= brd
->basemem
;
498 struct moxa_port
*port
;
502 printk(KERN_ERR
"moxa: bios length is not even\n");
506 retval
= moxa_real_load_code(brd
, ptr
, len
); /* may change numPorts */
510 switch (brd
->boardType
) {
511 case MOXA_BOARD_C218_ISA
:
512 case MOXA_BOARD_C218_PCI
:
513 case MOXA_BOARD_CP204J
:
515 for (i
= 0; i
< brd
->numPorts
; i
++, port
++) {
518 port
->curBaud
= 9600L;
520 port
->tableAddr
= baseAddr
+ Extern_table
+
522 ofsAddr
= port
->tableAddr
;
523 writew(C218rx_mask
, ofsAddr
+ RX_mask
);
524 writew(C218tx_mask
, ofsAddr
+ TX_mask
);
525 writew(C218rx_spage
+ i
* C218buf_pageno
, ofsAddr
+ Page_rxb
);
526 writew(readw(ofsAddr
+ Page_rxb
) + C218rx_pageno
, ofsAddr
+ EndPage_rxb
);
528 writew(C218tx_spage
+ i
* C218buf_pageno
, ofsAddr
+ Page_txb
);
529 writew(readw(ofsAddr
+ Page_txb
) + C218tx_pageno
, ofsAddr
+ EndPage_txb
);
535 for (i
= 0; i
< brd
->numPorts
; i
++, port
++) {
538 port
->curBaud
= 9600L;
540 port
->tableAddr
= baseAddr
+ Extern_table
+
542 ofsAddr
= port
->tableAddr
;
543 switch (brd
->numPorts
) {
545 writew(C320p8rx_mask
, ofsAddr
+ RX_mask
);
546 writew(C320p8tx_mask
, ofsAddr
+ TX_mask
);
547 writew(C320p8rx_spage
+ i
* C320p8buf_pgno
, ofsAddr
+ Page_rxb
);
548 writew(readw(ofsAddr
+ Page_rxb
) + C320p8rx_pgno
, ofsAddr
+ EndPage_rxb
);
549 writew(C320p8tx_spage
+ i
* C320p8buf_pgno
, ofsAddr
+ Page_txb
);
550 writew(readw(ofsAddr
+ Page_txb
) + C320p8tx_pgno
, ofsAddr
+ EndPage_txb
);
554 writew(C320p16rx_mask
, ofsAddr
+ RX_mask
);
555 writew(C320p16tx_mask
, ofsAddr
+ TX_mask
);
556 writew(C320p16rx_spage
+ i
* C320p16buf_pgno
, ofsAddr
+ Page_rxb
);
557 writew(readw(ofsAddr
+ Page_rxb
) + C320p16rx_pgno
, ofsAddr
+ EndPage_rxb
);
558 writew(C320p16tx_spage
+ i
* C320p16buf_pgno
, ofsAddr
+ Page_txb
);
559 writew(readw(ofsAddr
+ Page_txb
) + C320p16tx_pgno
, ofsAddr
+ EndPage_txb
);
563 writew(C320p24rx_mask
, ofsAddr
+ RX_mask
);
564 writew(C320p24tx_mask
, ofsAddr
+ TX_mask
);
565 writew(C320p24rx_spage
+ i
* C320p24buf_pgno
, ofsAddr
+ Page_rxb
);
566 writew(readw(ofsAddr
+ Page_rxb
) + C320p24rx_pgno
, ofsAddr
+ EndPage_rxb
);
567 writew(C320p24tx_spage
+ i
* C320p24buf_pgno
, ofsAddr
+ Page_txb
);
568 writew(readw(ofsAddr
+ Page_txb
), ofsAddr
+ EndPage_txb
);
571 writew(C320p32rx_mask
, ofsAddr
+ RX_mask
);
572 writew(C320p32tx_mask
, ofsAddr
+ TX_mask
);
573 writew(C320p32tx_ofs
, ofsAddr
+ Ofs_txb
);
574 writew(C320p32rx_spage
+ i
* C320p32buf_pgno
, ofsAddr
+ Page_rxb
);
575 writew(readb(ofsAddr
+ Page_rxb
), ofsAddr
+ EndPage_rxb
);
576 writew(C320p32tx_spage
+ i
* C320p32buf_pgno
, ofsAddr
+ Page_txb
);
577 writew(readw(ofsAddr
+ Page_txb
), ofsAddr
+ EndPage_txb
);
587 static int moxa_load_fw(struct moxa_board_conf
*brd
, const struct firmware
*fw
)
589 void *ptr
= fw
->data
;
593 unsigned int a
, lenp
, lencnt
;
596 __le32 magic
; /* 0x34303430 */
598 u8 type
; /* UNIX = 3 */
599 u8 model
; /* C218T=1, C320T=2, CP204=3 */
604 BUILD_BUG_ON(ARRAY_SIZE(hdr
->len
) != ARRAY_SIZE(lens
));
606 if (fw
->size
< MOXA_FW_HDRLEN
) {
607 strcpy(rsn
, "too short (even header won't fit)");
610 if (hdr
->magic
!= cpu_to_le32(0x30343034)) {
611 sprintf(rsn
, "bad magic: %.8x", le32_to_cpu(hdr
->magic
));
614 if (hdr
->type
!= 3) {
615 sprintf(rsn
, "not for linux, type is %u", hdr
->type
);
618 if (moxa_check_fw_model(brd
, hdr
->model
)) {
619 sprintf(rsn
, "not for this card, model is %u", hdr
->model
);
623 len
= MOXA_FW_HDRLEN
;
624 lencnt
= hdr
->model
== 2 ? 5 : 3;
625 for (a
= 0; a
< ARRAY_SIZE(lens
); a
++) {
626 lens
[a
] = le16_to_cpu(hdr
->len
[a
]);
627 if (lens
[a
] && len
+ lens
[a
] <= fw
->size
&&
628 moxa_check_fw(&fw
->data
[len
]))
629 printk(KERN_WARNING
"moxa firmware: unexpected input "
630 "at offset %u, but going on\n", (u32
)len
);
631 if (!lens
[a
] && a
< lencnt
) {
632 sprintf(rsn
, "too few entries in fw file");
638 if (len
!= fw
->size
) {
639 sprintf(rsn
, "bad length: %u (should be %u)", (u32
)fw
->size
,
644 ptr
+= MOXA_FW_HDRLEN
;
647 strcpy(rsn
, "read above");
649 ret
= moxa_load_bios(brd
, ptr
, lens
[lenp
]);
653 /* we skip the tty section (lens[1]), since we don't need it */
654 ptr
+= lens
[lenp
] + lens
[lenp
+ 1];
655 lenp
+= 2; /* comm */
657 if (hdr
->model
== 2) {
658 ret
= moxa_load_320b(brd
, ptr
, lens
[lenp
]);
661 /* skip another tty */
662 ptr
+= lens
[lenp
] + lens
[lenp
+ 1];
666 ret
= moxa_load_code(brd
, ptr
, lens
[lenp
]);
672 printk(KERN_ERR
"firmware failed to load, reason: %s\n", rsn
);
676 static int moxa_init_board(struct moxa_board_conf
*brd
, struct device
*dev
)
678 const struct firmware
*fw
;
682 switch (brd
->boardType
) {
683 case MOXA_BOARD_C218_ISA
:
684 case MOXA_BOARD_C218_PCI
:
685 file
= "c218tunx.cod";
687 case MOXA_BOARD_CP204J
:
688 file
= "cp204unx.cod";
691 file
= "c320tunx.cod";
695 ret
= request_firmware(&fw
, file
, dev
);
697 printk(KERN_ERR
"request_firmware failed\n");
701 ret
= moxa_load_fw(brd
, fw
);
703 release_firmware(fw
);
709 static int __devinit
moxa_pci_probe(struct pci_dev
*pdev
,
710 const struct pci_device_id
*ent
)
712 struct moxa_board_conf
*board
;
714 int board_type
= ent
->driver_data
;
717 retval
= pci_enable_device(pdev
);
719 dev_err(&pdev
->dev
, "can't enable pci device\n");
723 for (i
= 0; i
< MAX_BOARDS
; i
++)
724 if (moxa_boards
[i
].basemem
== NULL
)
728 if (i
>= MAX_BOARDS
) {
729 dev_warn(&pdev
->dev
, "more than %u MOXA Intellio family boards "
730 "found. Board is ignored.\n", MAX_BOARDS
);
734 board
= &moxa_boards
[i
];
735 board
->ports
= &moxa_ports
[i
* MAX_PORTS_PER_BOARD
];
737 retval
= pci_request_region(pdev
, 2, "moxa-base");
739 dev_err(&pdev
->dev
, "can't request pci region 2\n");
743 board
->basemem
= ioremap(pci_resource_start(pdev
, 2), 0x4000);
744 if (board
->basemem
== NULL
) {
745 dev_err(&pdev
->dev
, "can't remap io space 2\n");
749 board
->boardType
= board_type
;
750 switch (board_type
) {
751 case MOXA_BOARD_C218_ISA
:
752 case MOXA_BOARD_C218_PCI
:
756 case MOXA_BOARD_CP204J
:
763 board
->busType
= MOXA_BUS_TYPE_PCI
;
765 retval
= moxa_init_board(board
, &pdev
->dev
);
769 pci_set_drvdata(pdev
, board
);
773 iounmap(board
->basemem
);
774 board
->basemem
= NULL
;
776 pci_release_region(pdev
, 2);
781 static void __devexit
moxa_pci_remove(struct pci_dev
*pdev
)
783 struct moxa_board_conf
*brd
= pci_get_drvdata(pdev
);
785 iounmap(brd
->basemem
);
787 pci_release_region(pdev
, 2);
790 static struct pci_driver moxa_pci_driver
= {
792 .id_table
= moxa_pcibrds
,
793 .probe
= moxa_pci_probe
,
794 .remove
= __devexit_p(moxa_pci_remove
)
796 #endif /* CONFIG_PCI */
798 static int __init
moxa_init(void)
800 struct moxa_port
*ch
;
801 unsigned int i
, isabrds
= 0;
804 printk(KERN_INFO
"MOXA Intellio family driver version %s\n",
806 moxaDriver
= alloc_tty_driver(MAX_PORTS
+ 1);
810 moxaDriver
->owner
= THIS_MODULE
;
811 moxaDriver
->name
= "ttyMX";
812 moxaDriver
->major
= ttymajor
;
813 moxaDriver
->minor_start
= 0;
814 moxaDriver
->type
= TTY_DRIVER_TYPE_SERIAL
;
815 moxaDriver
->subtype
= SERIAL_TYPE_NORMAL
;
816 moxaDriver
->init_termios
= tty_std_termios
;
817 moxaDriver
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| CLOCAL
| HUPCL
;
818 moxaDriver
->init_termios
.c_ispeed
= 9600;
819 moxaDriver
->init_termios
.c_ospeed
= 9600;
820 moxaDriver
->flags
= TTY_DRIVER_REAL_RAW
;
821 tty_set_operations(moxaDriver
, &moxa_ops
);
823 for (i
= 0, ch
= moxa_ports
; i
< MAX_PORTS
; i
++, ch
++) {
824 ch
->type
= PORT_16550A
;
825 ch
->close_delay
= 5 * HZ
/ 10;
826 ch
->closing_wait
= 30 * HZ
;
827 ch
->cflag
= B9600
| CS8
| CREAD
| CLOCAL
| HUPCL
;
828 init_waitqueue_head(&ch
->open_wait
);
829 init_completion(&ch
->close_wait
);
831 setup_timer(&ch
->emptyTimer
, moxa_check_xmit_empty
,
835 pr_debug("Moxa tty devices major number = %d\n", ttymajor
);
837 if (tty_register_driver(moxaDriver
)) {
838 printk(KERN_ERR
"Couldn't install MOXA Smartio family driver !\n");
839 put_tty_driver(moxaDriver
);
843 mod_timer(&moxaTimer
, jiffies
+ HZ
/ 50);
845 /* Find the boards defined from module args. */
848 struct moxa_board_conf
*brd
= moxa_boards
;
849 for (i
= 0; i
< MAX_BOARDS
; i
++) {
852 if (type
[i
] == MOXA_BOARD_C218_ISA
||
853 type
[i
] == MOXA_BOARD_C320_ISA
) {
854 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
855 isabrds
+ 1, moxa_brdname
[type
[i
] - 1],
857 brd
->boardType
= type
[i
];
858 brd
->ports
= &moxa_ports
[isabrds
* MAX_PORTS_PER_BOARD
];
859 brd
->numPorts
= type
[i
] == MOXA_BOARD_C218_ISA
? 8 :
861 brd
->busType
= MOXA_BUS_TYPE_ISA
;
862 brd
->basemem
= ioremap(baseaddr
[i
], 0x4000);
864 printk(KERN_ERR
"moxa: can't remap %lx\n",
868 if (moxa_init_board(brd
, NULL
)) {
869 iounmap(brd
->basemem
);
882 retval
= pci_register_driver(&moxa_pci_driver
);
884 printk(KERN_ERR
"Can't register moxa pci driver!\n");
893 static void __exit
moxa_exit(void)
897 del_timer_sync(&moxaTimer
);
899 for (i
= 0; i
< MAX_PORTS
; i
++)
900 del_timer_sync(&moxa_ports
[i
].emptyTimer
);
902 if (tty_unregister_driver(moxaDriver
))
903 printk(KERN_ERR
"Couldn't unregister MOXA Intellio family "
905 put_tty_driver(moxaDriver
);
908 pci_unregister_driver(&moxa_pci_driver
);
911 for (i
= 0; i
< MAX_BOARDS
; i
++)
912 if (moxa_boards
[i
].basemem
)
913 iounmap(moxa_boards
[i
].basemem
);
916 module_init(moxa_init
);
917 module_exit(moxa_exit
);
919 static int moxa_open(struct tty_struct
*tty
, struct file
*filp
)
921 struct moxa_port
*ch
;
926 if (port
== MAX_PORTS
) {
929 if (!MoxaPortIsValid(port
)) {
930 tty
->driver_data
= NULL
;
934 ch
= &moxa_ports
[port
];
936 tty
->driver_data
= ch
;
938 if (!(ch
->asyncflags
& ASYNC_INITIALIZED
)) {
940 moxa_set_tty_param(tty
, tty
->termios
);
941 MoxaPortLineCtrl(ch
, 1, 1);
943 ch
->asyncflags
|= ASYNC_INITIALIZED
;
945 retval
= moxa_block_till_ready(tty
, filp
, ch
);
947 moxa_unthrottle(tty
);
949 if (ch
->type
== PORT_16550A
) {
958 static void moxa_close(struct tty_struct
*tty
, struct file
*filp
)
960 struct moxa_port
*ch
;
964 if (port
== MAX_PORTS
) {
967 if (!MoxaPortIsValid(port
)) {
968 pr_debug("Invalid portno in moxa_close\n");
969 tty
->driver_data
= NULL
;
972 if (tty
->driver_data
== NULL
) {
975 if (tty_hung_up_p(filp
)) {
978 ch
= (struct moxa_port
*) tty
->driver_data
;
980 if ((tty
->count
== 1) && (ch
->count
!= 1)) {
981 printk(KERN_WARNING
"moxa_close: bad serial port count; "
982 "tty->count is 1, ch->count is %d\n", ch
->count
);
985 if (--ch
->count
< 0) {
986 printk(KERN_WARNING
"moxa_close: bad serial port count, "
987 "device=%s\n", tty
->name
);
993 ch
->asyncflags
|= ASYNC_CLOSING
;
995 ch
->cflag
= tty
->termios
->c_cflag
;
996 if (ch
->asyncflags
& ASYNC_INITIALIZED
) {
997 moxa_setup_empty_event(tty
);
998 tty_wait_until_sent(tty
, 30 * HZ
); /* 30 seconds timeout */
999 del_timer_sync(&ch
->emptyTimer
);
1002 MoxaPortFlushData(ch
, 2);
1004 if (tty
->driver
->flush_buffer
)
1005 tty
->driver
->flush_buffer(tty
);
1006 tty_ldisc_flush(tty
);
1011 if (ch
->blocked_open
) {
1012 if (ch
->close_delay
) {
1013 msleep_interruptible(jiffies_to_msecs(ch
->close_delay
));
1015 wake_up_interruptible(&ch
->open_wait
);
1017 ch
->asyncflags
&= ~(ASYNC_NORMAL_ACTIVE
| ASYNC_CLOSING
);
1018 complete_all(&ch
->close_wait
);
1021 static int moxa_write(struct tty_struct
*tty
,
1022 const unsigned char *buf
, int count
)
1024 struct moxa_port
*ch
= tty
->driver_data
;
1025 unsigned long flags
;
1031 spin_lock_irqsave(&moxa_lock
, flags
);
1032 len
= MoxaPortWriteData(ch
, (unsigned char *) buf
, count
);
1033 spin_unlock_irqrestore(&moxa_lock
, flags
);
1035 /*********************************************
1036 if ( !(ch->statusflags & LOWWAIT) &&
1037 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
1038 ************************************************/
1039 ch
->statusflags
|= LOWWAIT
;
1043 static int moxa_write_room(struct tty_struct
*tty
)
1045 struct moxa_port
*ch
;
1049 ch
= tty
->driver_data
;
1052 return MoxaPortTxFree(ch
);
1055 static void moxa_flush_buffer(struct tty_struct
*tty
)
1057 struct moxa_port
*ch
= tty
->driver_data
;
1061 MoxaPortFlushData(ch
, 1);
1065 static int moxa_chars_in_buffer(struct tty_struct
*tty
)
1067 struct moxa_port
*ch
= tty
->driver_data
;
1071 * Sigh...I have to check if driver_data is NULL here, because
1072 * if an open() fails, the TTY subsystem eventually calls
1073 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1074 * routine. And since the open() failed, we return 0 here. TDJ
1078 chars
= MoxaPortTxQueue(ch
);
1081 * Make it possible to wakeup anything waiting for output
1082 * in tty_ioctl.c, etc.
1084 if (!(ch
->statusflags
& EMPTYWAIT
))
1085 moxa_setup_empty_event(tty
);
1090 static void moxa_flush_chars(struct tty_struct
*tty
)
1093 * Don't think I need this, because this is called to empty the TX
1094 * buffer for the 16450, 16550, etc.
1098 static void moxa_put_char(struct tty_struct
*tty
, unsigned char c
)
1100 struct moxa_port
*ch
= tty
->driver_data
;
1101 unsigned long flags
;
1105 spin_lock_irqsave(&moxa_lock
, flags
);
1106 MoxaPortWriteData(ch
, &c
, 1);
1107 spin_unlock_irqrestore(&moxa_lock
, flags
);
1108 /************************************************
1109 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
1110 *************************************************/
1111 ch
->statusflags
|= LOWWAIT
;
1114 static int moxa_tiocmget(struct tty_struct
*tty
, struct file
*file
)
1116 struct moxa_port
*ch
= tty
->driver_data
;
1117 int flag
= 0, dtr
, rts
;
1119 if ((tty
->index
!= MAX_PORTS
) && (!ch
))
1122 MoxaPortGetLineOut(ch
, &dtr
, &rts
);
1127 dtr
= MoxaPortLineStatus(ch
);
1137 static int moxa_tiocmset(struct tty_struct
*tty
, struct file
*file
,
1138 unsigned int set
, unsigned int clear
)
1140 struct moxa_port
*ch
= tty
->driver_data
;
1145 if ((port
!= MAX_PORTS
) && (!ch
))
1148 MoxaPortGetLineOut(ch
, &dtr
, &rts
);
1149 if (set
& TIOCM_RTS
)
1151 if (set
& TIOCM_DTR
)
1153 if (clear
& TIOCM_RTS
)
1155 if (clear
& TIOCM_DTR
)
1157 MoxaPortLineCtrl(ch
, dtr
, rts
);
1161 static int moxa_ioctl(struct tty_struct
*tty
, struct file
*file
,
1162 unsigned int cmd
, unsigned long arg
)
1164 struct moxa_port
*ch
= tty
->driver_data
;
1166 void __user
*argp
= (void __user
*)arg
;
1170 if ((port
!= MAX_PORTS
) && (!ch
))
1174 case TCSBRK
: /* SVID version: non-zero arg --> no break */
1175 retval
= tty_check_change(tty
);
1178 moxa_setup_empty_event(tty
);
1179 tty_wait_until_sent(tty
, 0);
1181 MoxaPortSendBreak(ch
, 0);
1183 case TCSBRKP
: /* support for POSIX tcsendbreak() */
1184 retval
= tty_check_change(tty
);
1187 moxa_setup_empty_event(tty
);
1188 tty_wait_until_sent(tty
, 0);
1189 MoxaPortSendBreak(ch
, arg
);
1192 return put_user(C_CLOCAL(tty
) ? 1 : 0, (int __user
*)argp
);
1194 if (get_user(retval
, (int __user
*)argp
))
1197 tty
->termios
->c_cflag
= ((tty
->termios
->c_cflag
& ~CLOCAL
) |
1198 (arg
? CLOCAL
: 0));
1200 ch
->asyncflags
&= ~ASYNC_CHECK_CD
;
1202 ch
->asyncflags
|= ASYNC_CHECK_CD
;
1205 return moxa_get_serial_info(ch
, argp
);
1208 return moxa_set_serial_info(ch
, argp
);
1210 retval
= MoxaDriverIoctl(tty
, cmd
, arg
);
1215 static void moxa_throttle(struct tty_struct
*tty
)
1217 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
1219 ch
->statusflags
|= THROTTLE
;
1222 static void moxa_unthrottle(struct tty_struct
*tty
)
1224 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
1226 ch
->statusflags
&= ~THROTTLE
;
1229 static void moxa_set_termios(struct tty_struct
*tty
,
1230 struct ktermios
*old_termios
)
1232 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
1236 moxa_set_tty_param(tty
, old_termios
);
1237 if (!(old_termios
->c_cflag
& CLOCAL
) &&
1238 (tty
->termios
->c_cflag
& CLOCAL
))
1239 wake_up_interruptible(&ch
->open_wait
);
1242 static void moxa_stop(struct tty_struct
*tty
)
1244 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
1248 MoxaPortTxDisable(ch
);
1249 ch
->statusflags
|= TXSTOPPED
;
1253 static void moxa_start(struct tty_struct
*tty
)
1255 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
1260 if (!(ch
->statusflags
& TXSTOPPED
))
1263 MoxaPortTxEnable(ch
);
1264 ch
->statusflags
&= ~TXSTOPPED
;
1267 static void moxa_hangup(struct tty_struct
*tty
)
1269 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
1271 moxa_flush_buffer(tty
);
1275 ch
->asyncflags
&= ~ASYNC_NORMAL_ACTIVE
;
1277 wake_up_interruptible(&ch
->open_wait
);
1280 static void moxa_poll(unsigned long ignored
)
1283 struct moxa_port
*ch
;
1284 struct tty_struct
*tp
;
1287 del_timer(&moxaTimer
);
1289 if (MoxaDriverPoll() < 0) {
1290 mod_timer(&moxaTimer
, jiffies
+ HZ
/ 50);
1293 for (card
= 0; card
< MAX_BOARDS
; card
++) {
1294 if ((ports
= MoxaPortsOfCard(card
)) <= 0)
1296 ch
= &moxa_ports
[card
* MAX_PORTS_PER_BOARD
];
1297 for (i
= 0; i
< ports
; i
++, ch
++) {
1298 if ((ch
->asyncflags
& ASYNC_INITIALIZED
) == 0)
1300 if (!(ch
->statusflags
& THROTTLE
) &&
1301 (MoxaPortRxQueue(ch
) > 0))
1302 moxa_receive_data(ch
);
1303 if ((tp
= ch
->tty
) == 0)
1305 if (ch
->statusflags
& LOWWAIT
) {
1306 if (MoxaPortTxQueue(ch
) <= WAKEUP_CHARS
) {
1308 ch
->statusflags
&= ~LOWWAIT
;
1313 if (!I_IGNBRK(tp
) && (MoxaPortResetBrkCnt(ch
) > 0)) {
1314 tty_insert_flip_char(tp
, 0, TTY_BREAK
);
1315 tty_schedule_flip(tp
);
1317 if (MoxaPortDCDChange(ch
)) {
1318 if (ch
->asyncflags
& ASYNC_CHECK_CD
) {
1319 if (MoxaPortDCDON(ch
))
1320 wake_up_interruptible(&ch
->open_wait
);
1323 wake_up_interruptible(&ch
->open_wait
);
1324 ch
->asyncflags
&= ~ASYNC_NORMAL_ACTIVE
;
1331 mod_timer(&moxaTimer
, jiffies
+ HZ
/ 50);
1334 /******************************************************************************/
1336 static void moxa_set_tty_param(struct tty_struct
*tty
, struct ktermios
*old_termios
)
1338 register struct ktermios
*ts
;
1339 struct moxa_port
*ch
;
1340 int rts
, cts
, txflow
, rxflow
, xany
, baud
;
1342 ch
= (struct moxa_port
*) tty
->driver_data
;
1344 if (ts
->c_cflag
& CLOCAL
)
1345 ch
->asyncflags
&= ~ASYNC_CHECK_CD
;
1347 ch
->asyncflags
|= ASYNC_CHECK_CD
;
1348 rts
= cts
= txflow
= rxflow
= xany
= 0;
1349 if (ts
->c_cflag
& CRTSCTS
)
1351 if (ts
->c_iflag
& IXON
)
1353 if (ts
->c_iflag
& IXOFF
)
1355 if (ts
->c_iflag
& IXANY
)
1358 /* Clear the features we don't support */
1359 ts
->c_cflag
&= ~CMSPAR
;
1360 MoxaPortFlowCtrl(ch
, rts
, cts
, txflow
, rxflow
, xany
);
1361 baud
= MoxaPortSetTermio(ch
, ts
, tty_get_baud_rate(tty
));
1363 baud
= tty_termios_baud_rate(old_termios
);
1364 /* Not put the baud rate into the termios data */
1365 tty_encode_baud_rate(tty
, baud
, baud
);
1368 static int moxa_block_till_ready(struct tty_struct
*tty
, struct file
*filp
,
1369 struct moxa_port
*ch
)
1371 DECLARE_WAITQUEUE(wait
,current
);
1372 unsigned long flags
;
1374 int do_clocal
= C_CLOCAL(tty
);
1377 * If the device is in the middle of being closed, then block
1378 * until it's done, and then try again.
1380 if (tty_hung_up_p(filp
) || (ch
->asyncflags
& ASYNC_CLOSING
)) {
1381 if (ch
->asyncflags
& ASYNC_CLOSING
)
1382 wait_for_completion_interruptible(&ch
->close_wait
);
1383 #ifdef SERIAL_DO_RESTART
1384 if (ch
->asyncflags
& ASYNC_HUP_NOTIFY
)
1387 return (-ERESTARTSYS
);
1393 * If non-blocking mode is set, then make the check up front
1396 if (filp
->f_flags
& O_NONBLOCK
) {
1397 ch
->asyncflags
|= ASYNC_NORMAL_ACTIVE
;
1401 * Block waiting for the carrier detect and the line to become free
1404 add_wait_queue(&ch
->open_wait
, &wait
);
1405 pr_debug("block_til_ready before block: ttys%d, count = %d\n",
1406 tty
->index
, ch
->count
);
1407 spin_lock_irqsave(&moxa_lock
, flags
);
1408 if (!tty_hung_up_p(filp
))
1411 spin_unlock_irqrestore(&moxa_lock
, flags
);
1414 set_current_state(TASK_INTERRUPTIBLE
);
1415 if (tty_hung_up_p(filp
) ||
1416 !(ch
->asyncflags
& ASYNC_INITIALIZED
)) {
1417 #ifdef SERIAL_DO_RESTART
1418 if (ch
->asyncflags
& ASYNC_HUP_NOTIFY
)
1421 retval
= -ERESTARTSYS
;
1427 if (!(ch
->asyncflags
& ASYNC_CLOSING
) && (do_clocal
||
1431 if (signal_pending(current
)) {
1432 retval
= -ERESTARTSYS
;
1437 set_current_state(TASK_RUNNING
);
1438 remove_wait_queue(&ch
->open_wait
, &wait
);
1440 spin_lock_irqsave(&moxa_lock
, flags
);
1441 if (!tty_hung_up_p(filp
))
1444 spin_unlock_irqrestore(&moxa_lock
, flags
);
1445 pr_debug("block_til_ready after blocking: ttys%d, count = %d\n",
1446 tty
->index
, ch
->count
);
1449 /* FIXME: review to see if we need to use set_bit on these */
1450 ch
->asyncflags
|= ASYNC_NORMAL_ACTIVE
;
1454 static void moxa_setup_empty_event(struct tty_struct
*tty
)
1456 struct moxa_port
*ch
= tty
->driver_data
;
1457 unsigned long flags
;
1459 spin_lock_irqsave(&moxa_lock
, flags
);
1460 ch
->statusflags
|= EMPTYWAIT
;
1461 mod_timer(&ch
->emptyTimer
, jiffies
+ HZ
);
1462 spin_unlock_irqrestore(&moxa_lock
, flags
);
1465 static void moxa_check_xmit_empty(unsigned long data
)
1467 struct moxa_port
*ch
;
1469 ch
= (struct moxa_port
*) data
;
1470 if (ch
->tty
&& (ch
->statusflags
& EMPTYWAIT
)) {
1471 if (MoxaPortTxQueue(ch
) == 0) {
1472 ch
->statusflags
&= ~EMPTYWAIT
;
1473 tty_wakeup(ch
->tty
);
1476 mod_timer(&ch
->emptyTimer
, round_jiffies(jiffies
+ HZ
));
1478 ch
->statusflags
&= ~EMPTYWAIT
;
1481 static void moxa_shut_down(struct moxa_port
*ch
)
1483 struct tty_struct
*tp
;
1485 if (!(ch
->asyncflags
& ASYNC_INITIALIZED
))
1490 MoxaPortDisable(ch
);
1493 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1495 if (tp
->termios
->c_cflag
& HUPCL
)
1496 MoxaPortLineCtrl(ch
, 0, 0);
1498 ch
->asyncflags
&= ~ASYNC_INITIALIZED
;
1501 static void moxa_receive_data(struct moxa_port
*ch
)
1503 struct tty_struct
*tp
;
1504 struct ktermios
*ts
;
1505 unsigned long flags
;
1511 /**************************************************
1512 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1513 *****************************************************/
1515 MoxaPortFlushData(ch
, 0);
1518 spin_lock_irqsave(&moxa_lock
, flags
);
1519 MoxaPortReadData(ch
, tp
);
1520 spin_unlock_irqrestore(&moxa_lock
, flags
);
1521 tty_schedule_flip(tp
);
1530 int rxcnt
[MAX_PORTS
];
1531 int txcnt
[MAX_PORTS
];
1534 #define DCD_changed 0x01
1535 #define DCD_oldstate 0x80
1537 static int moxaLowWaterChk
;
1538 static struct mon_str moxaLog
;
1539 static int moxaFuncTout
= HZ
/ 2;
1541 static void moxafunc(void __iomem
*, int, ushort
);
1542 static void moxa_wait_finish(void __iomem
*);
1543 static void moxa_low_water_check(void __iomem
*);
1545 /*****************************************************************************
1546 * Driver level functions: *
1547 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1548 * 3. MoxaDriverPoll(void); *
1549 *****************************************************************************/
1551 #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1552 #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
1553 #define MOXA_GETDATACOUNT (MOXA + 23)
1554 #define MOXA_GET_IOQUEUE (MOXA + 27)
1555 #define MOXA_FLUSH_QUEUE (MOXA + 28)
1556 #define MOXA_GET_CONF (MOXA + 35) /* configuration */
1557 #define MOXA_GET_MAJOR (MOXA + 63)
1558 #define MOXA_GET_CUMAJOR (MOXA + 64)
1559 #define MOXA_GETMSTATUS (MOXA + 65)
1561 static void MoxaPortFlushData(struct moxa_port
*port
, int mode
)
1563 void __iomem
*ofsAddr
;
1564 if ((mode
< 0) || (mode
> 2))
1566 ofsAddr
= port
->tableAddr
;
1567 moxafunc(ofsAddr
, FC_FlushQueue
, mode
);
1569 port
->lowChkFlag
= 0;
1570 moxa_low_water_check(ofsAddr
);
1574 static int MoxaDriverIoctl(struct tty_struct
*tty
, unsigned int cmd
,
1577 struct moxa_port
*port
= tty
->driver_data
;
1580 void __user
*argp
= (void __user
*)arg
;
1582 if (tty
->index
== MAX_PORTS
) {
1583 if ((cmd
!= MOXA_GET_CONF
) && (cmd
!= MOXA_GETDATACOUNT
) &&
1584 (cmd
!= MOXA_GET_IOQUEUE
) && (cmd
!= MOXA_GET_MAJOR
) &&
1585 (cmd
!= MOXA_GET_CUMAJOR
) && (cmd
!= MOXA_GETMSTATUS
))
1589 case MOXA_GETDATACOUNT
:
1590 moxaLog
.tick
= jiffies
;
1591 if(copy_to_user(argp
, &moxaLog
, sizeof(struct mon_str
)))
1594 case MOXA_FLUSH_QUEUE
:
1595 MoxaPortFlushData(port
, arg
);
1597 case MOXA_GET_IOQUEUE
: {
1598 struct moxaq_str __user
*argm
= argp
;
1599 struct moxaq_str tmp
;
1601 for (i
= 0; i
< MAX_PORTS
; i
++, argm
++) {
1602 memset(&tmp
, 0, sizeof(tmp
));
1603 if (moxa_ports
[i
].chkPort
) {
1604 tmp
.inq
= MoxaPortRxQueue(&moxa_ports
[i
]);
1605 tmp
.outq
= MoxaPortTxQueue(&moxa_ports
[i
]);
1607 if (copy_to_user(argm
, &tmp
, sizeof(tmp
)))
1611 } case MOXA_GET_OQUEUE
:
1612 i
= MoxaPortTxQueue(port
);
1613 return put_user(i
, (unsigned long __user
*)argp
);
1614 case MOXA_GET_IQUEUE
:
1615 i
= MoxaPortRxQueue(port
);
1616 return put_user(i
, (unsigned long __user
*)argp
);
1617 case MOXA_GET_MAJOR
:
1618 if(copy_to_user(argp
, &ttymajor
, sizeof(int)))
1621 case MOXA_GET_CUMAJOR
:
1623 if(copy_to_user(argp
, &i
, sizeof(int)))
1626 case MOXA_GETMSTATUS
: {
1627 struct mxser_mstatus __user
*argm
= argp
;
1628 struct mxser_mstatus tmp
;
1629 struct moxa_port
*p
;
1631 for (i
= 0; i
< MAX_PORTS
; i
++, argm
++) {
1633 memset(&tmp
, 0, sizeof(tmp
));
1637 status
= MoxaPortLineStatus(p
);
1646 if (!p
->tty
|| !p
->tty
->termios
)
1647 tmp
.cflag
= p
->cflag
;
1649 tmp
.cflag
= p
->tty
->termios
->c_cflag
;
1651 if (copy_to_user(argm
, &tmp
, sizeof(tmp
)))
1658 return -ENOIOCTLCMD
;
1661 int MoxaDriverPoll(void)
1663 struct moxa_board_conf
*brd
;
1664 register ushort temp
;
1666 void __iomem
*ofsAddr
;
1672 for (card
= 0; card
< MAX_BOARDS
; card
++) {
1673 brd
= &moxa_boards
[card
];
1674 if (brd
->loadstat
== 0)
1676 if ((ports
= brd
->numPorts
) == 0)
1678 if (readb(brd
->intPend
) == 0xff) {
1679 ip
= brd
->intTable
+ readb(brd
->intNdx
);
1680 p
= card
* MAX_PORTS_PER_BOARD
;
1682 for (port
= 0; port
< ports
; port
+= 2, p
++) {
1683 if ((temp
= readw(ip
+ port
)) != 0) {
1684 writew(0, ip
+ port
);
1685 ofsAddr
= moxa_ports
[p
].tableAddr
;
1687 writew(readw(ofsAddr
+ HostStat
) & ~WakeupTx
, ofsAddr
+ HostStat
);
1688 if (temp
& IntrBreak
) {
1689 moxa_ports
[p
].breakCnt
++;
1691 if (temp
& IntrLine
) {
1692 if (readb(ofsAddr
+ FlagStat
) & DCD_state
) {
1693 if ((moxa_ports
[p
].DCDState
& DCD_oldstate
) == 0)
1694 moxa_ports
[p
].DCDState
= (DCD_oldstate
|
1697 if (moxa_ports
[p
].DCDState
& DCD_oldstate
)
1698 moxa_ports
[p
].DCDState
= DCD_changed
;
1703 writeb(0, brd
->intPend
);
1705 if (moxaLowWaterChk
) {
1706 p
= card
* MAX_PORTS_PER_BOARD
;
1707 for (port
= 0; port
< ports
; port
++, p
++) {
1708 if (moxa_ports
[p
].lowChkFlag
) {
1709 moxa_ports
[p
].lowChkFlag
= 0;
1710 ofsAddr
= moxa_ports
[p
].tableAddr
;
1711 moxa_low_water_check(ofsAddr
);
1716 moxaLowWaterChk
= 0;
1720 /*****************************************************************************
1721 * Card level function: *
1722 * 1. MoxaPortsOfCard(int cardno); *
1723 *****************************************************************************/
1724 int MoxaPortsOfCard(int cardno
)
1727 if (moxa_boards
[cardno
].boardType
== 0)
1729 return (moxa_boards
[cardno
].numPorts
);
1732 /*****************************************************************************
1733 * Port level functions: *
1734 * 1. MoxaPortIsValid(int port); *
1735 * 2. MoxaPortEnable(int port); *
1736 * 3. MoxaPortDisable(int port); *
1737 * 4. MoxaPortGetMaxBaud(int port); *
1738 * 6. MoxaPortSetBaud(int port, long baud); *
1739 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1740 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1741 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1742 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1743 * 12. MoxaPortLineStatus(int port); *
1744 * 13. MoxaPortDCDChange(int port); *
1745 * 14. MoxaPortDCDON(int port); *
1746 * 15. MoxaPortFlushData(int port, int mode); *
1747 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1748 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1749 * 20. MoxaPortTxQueue(int port); *
1750 * 21. MoxaPortTxFree(int port); *
1751 * 22. MoxaPortRxQueue(int port); *
1752 * 24. MoxaPortTxDisable(int port); *
1753 * 25. MoxaPortTxEnable(int port); *
1754 * 27. MoxaPortResetBrkCnt(int port); *
1755 * 30. MoxaPortSendBreak(int port, int ticks); *
1756 *****************************************************************************/
1758 * Moxa Port Number Description:
1760 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1761 * the port number using in MOXA driver functions will be 0 to 31 for
1762 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1763 * to 127 for fourth. For example, if you setup three MOXA boards,
1764 * first board is C218, second board is C320-16 and third board is
1765 * C320-32. The port number of first board (C218 - 8 ports) is from
1766 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1767 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1768 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1769 * 127 will be invalid.
1772 * Moxa Functions Description:
1774 * Function 1: Driver initialization routine, this routine must be
1775 * called when initialized driver.
1777 * void MoxaDriverInit();
1780 * Function 2: Moxa driver private IOCTL command processing.
1782 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1784 * unsigned int cmd : IOCTL command
1785 * unsigned long arg : IOCTL argument
1786 * int port : port number (0 - 127)
1793 * Function 3: Moxa driver polling process routine.
1795 * int MoxaDriverPoll(void);
1797 * return: 0 ; polling O.K.
1798 * -1 : no any Moxa card.
1801 * Function 4: Get the ports of this card.
1803 * int MoxaPortsOfCard(int cardno);
1805 * int cardno : card number (0 - 3)
1807 * return: 0 : this card is invalid
1811 * Function 5: Check this port is valid or invalid
1813 * int MoxaPortIsValid(int port);
1814 * int port : port number (0 - 127, ref port description)
1816 * return: 0 : this port is invalid
1817 * 1 : this port is valid
1820 * Function 6: Enable this port to start Tx/Rx data.
1822 * void MoxaPortEnable(int port);
1823 * int port : port number (0 - 127)
1826 * Function 7: Disable this port
1828 * void MoxaPortDisable(int port);
1829 * int port : port number (0 - 127)
1832 * Function 8: Get the maximun available baud rate of this port.
1834 * long MoxaPortGetMaxBaud(int port);
1835 * int port : port number (0 - 127)
1837 * return: 0 : this port is invalid
1838 * 38400/57600/115200 bps
1841 * Function 10: Setting baud rate of this port.
1843 * long MoxaPortSetBaud(int port, long baud);
1844 * int port : port number (0 - 127)
1845 * long baud : baud rate (50 - 115200)
1847 * return: 0 : this port is invalid or baud < 50
1848 * 50 - 115200 : the real baud rate set to the port, if
1849 * the argument baud is large than maximun
1850 * available baud rate, the real setting
1851 * baud rate will be the maximun baud rate.
1854 * Function 12: Configure the port.
1856 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1857 * int port : port number (0 - 127)
1858 * struct ktermios * termio : termio structure pointer
1859 * speed_t baud : baud rate
1861 * return: -1 : this port is invalid or termio == NULL
1865 * Function 13: Get the DTR/RTS state of this port.
1867 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1868 * int port : port number (0 - 127)
1869 * int * dtrState : pointer to INT to receive the current DTR
1870 * state. (if NULL, this function will not
1871 * write to this address)
1872 * int * rtsState : pointer to INT to receive the current RTS
1873 * state. (if NULL, this function will not
1874 * write to this address)
1876 * return: -1 : this port is invalid
1880 * Function 14: Setting the DTR/RTS output state of this port.
1882 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1883 * int port : port number (0 - 127)
1884 * int dtrState : DTR output state (0: off, 1: on)
1885 * int rtsState : RTS output state (0: off, 1: on)
1888 * Function 15: Setting the flow control of this port.
1890 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1891 * int txFlow,int xany);
1892 * int port : port number (0 - 127)
1893 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1894 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1895 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1896 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1897 * int xany : S/W XANY flow control (0: no, 1: yes)
1900 * Function 16: Get ths line status of this port
1902 * int MoxaPortLineStatus(int port);
1903 * int port : port number (0 - 127)
1905 * return: Bit 0 - CTS state (0: off, 1: on)
1906 * Bit 1 - DSR state (0: off, 1: on)
1907 * Bit 2 - DCD state (0: off, 1: on)
1910 * Function 17: Check the DCD state has changed since the last read
1913 * int MoxaPortDCDChange(int port);
1914 * int port : port number (0 - 127)
1916 * return: 0 : no changed
1917 * 1 : DCD has changed
1920 * Function 18: Check ths current DCD state is ON or not.
1922 * int MoxaPortDCDON(int port);
1923 * int port : port number (0 - 127)
1925 * return: 0 : DCD off
1929 * Function 19: Flush the Rx/Tx buffer data of this port.
1931 * void MoxaPortFlushData(int port, int mode);
1932 * int port : port number (0 - 127)
1934 * 0 : flush the Rx buffer
1935 * 1 : flush the Tx buffer
1936 * 2 : flush the Rx and Tx buffer
1939 * Function 20: Write data.
1941 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1942 * int port : port number (0 - 127)
1943 * unsigned char * buffer : pointer to write data buffer.
1944 * int length : write data length
1946 * return: 0 - length : real write data length
1949 * Function 21: Read data.
1951 * int MoxaPortReadData(int port, struct tty_struct *tty);
1952 * int port : port number (0 - 127)
1953 * struct tty_struct *tty : tty for data
1955 * return: 0 - length : real read data length
1958 * Function 24: Get the Tx buffer current queued data bytes
1960 * int MoxaPortTxQueue(int port);
1961 * int port : port number (0 - 127)
1963 * return: .. : Tx buffer current queued data bytes
1966 * Function 25: Get the Tx buffer current free space
1968 * int MoxaPortTxFree(int port);
1969 * int port : port number (0 - 127)
1971 * return: .. : Tx buffer current free space
1974 * Function 26: Get the Rx buffer current queued data bytes
1976 * int MoxaPortRxQueue(int port);
1977 * int port : port number (0 - 127)
1979 * return: .. : Rx buffer current queued data bytes
1982 * Function 28: Disable port data transmission.
1984 * void MoxaPortTxDisable(int port);
1985 * int port : port number (0 - 127)
1988 * Function 29: Enable port data transmission.
1990 * void MoxaPortTxEnable(int port);
1991 * int port : port number (0 - 127)
1994 * Function 31: Get the received BREAK signal count and reset it.
1996 * int MoxaPortResetBrkCnt(int port);
1997 * int port : port number (0 - 127)
1999 * return: 0 - .. : BREAK signal count
2002 * Function 34: Send out a BREAK signal.
2004 * void MoxaPortSendBreak(int port, int ms100);
2005 * int port : port number (0 - 127)
2006 * int ms100 : break signal time interval.
2007 * unit: 100 mini-second. if ms100 == 0, it will
2008 * send out a about 250 ms BREAK signal.
2011 static int MoxaPortIsValid(int port
)
2015 if (moxa_ports
[port
].chkPort
== 0)
2020 static void MoxaPortEnable(struct moxa_port
*port
)
2022 void __iomem
*ofsAddr
;
2023 short lowwater
= 512;
2025 ofsAddr
= port
->tableAddr
;
2026 writew(lowwater
, ofsAddr
+ Low_water
);
2028 if (port
->board
->boardType
== MOXA_BOARD_C320_ISA
||
2029 port
->board
->boardType
== MOXA_BOARD_C320_PCI
) {
2030 moxafunc(ofsAddr
, FC_SetBreakIrq
, 0);
2032 writew(readw(ofsAddr
+ HostStat
) | WakeupBreak
, ofsAddr
+ HostStat
);
2035 moxafunc(ofsAddr
, FC_SetLineIrq
, Magic_code
);
2036 moxafunc(ofsAddr
, FC_FlushQueue
, 2);
2038 moxafunc(ofsAddr
, FC_EnableCH
, Magic_code
);
2039 MoxaPortLineStatus(port
);
2042 static void MoxaPortDisable(struct moxa_port
*port
)
2044 void __iomem
*ofsAddr
= port
->tableAddr
;
2046 moxafunc(ofsAddr
, FC_SetFlowCtl
, 0); /* disable flow control */
2047 moxafunc(ofsAddr
, FC_ClrLineIrq
, Magic_code
);
2048 writew(0, ofsAddr
+ HostStat
);
2049 moxafunc(ofsAddr
, FC_DisableCH
, Magic_code
);
2052 static long MoxaPortGetMaxBaud(struct moxa_port
*port
)
2054 if (port
->board
->boardType
== MOXA_BOARD_C320_ISA
||
2055 port
->board
->boardType
== MOXA_BOARD_C320_PCI
)
2062 static long MoxaPortSetBaud(struct moxa_port
*port
, long baud
)
2064 void __iomem
*ofsAddr
;
2068 if ((baud
< 50L) || ((max
= MoxaPortGetMaxBaud(port
)) == 0))
2070 ofsAddr
= port
->tableAddr
;
2074 clock
= 614400L; /* for 9.8304 Mhz : max. 38400 bps */
2075 else if (max
== 57600L)
2076 clock
= 691200L; /* for 11.0592 Mhz : max. 57600 bps */
2078 clock
= 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2080 moxafunc(ofsAddr
, FC_SetBaud
, val
);
2082 port
->curBaud
= baud
;
2086 static int MoxaPortSetTermio(struct moxa_port
*port
, struct ktermios
*termio
,
2089 void __iomem
*ofsAddr
;
2093 if (port
->chkPort
== 0 || termio
== 0)
2095 ofsAddr
= port
->tableAddr
;
2096 cflag
= termio
->c_cflag
; /* termio->c_cflag */
2098 mode
= termio
->c_cflag
& CSIZE
;
2101 else if (mode
== CS6
)
2103 else if (mode
== CS7
)
2105 else if (mode
== CS8
)
2108 if (termio
->c_cflag
& CSTOPB
) {
2116 if (termio
->c_cflag
& PARENB
) {
2117 if (termio
->c_cflag
& PARODD
)
2124 moxafunc(ofsAddr
, FC_SetDataMode
, (ushort
) mode
);
2126 if (port
->board
->boardType
== MOXA_BOARD_C320_ISA
||
2127 port
->board
->boardType
== MOXA_BOARD_C320_PCI
) {
2128 if (baud
>= 921600L)
2131 baud
= MoxaPortSetBaud(port
, baud
);
2133 if (termio
->c_iflag
& (IXON
| IXOFF
| IXANY
)) {
2134 writeb(termio
->c_cc
[VSTART
], ofsAddr
+ FuncArg
);
2135 writeb(termio
->c_cc
[VSTOP
], ofsAddr
+ FuncArg1
);
2136 writeb(FC_SetXonXoff
, ofsAddr
+ FuncCode
);
2137 moxa_wait_finish(ofsAddr
);
2143 static int MoxaPortGetLineOut(struct moxa_port
*port
, int *dtrState
,
2147 if (!MoxaPortIsValid(port
->tty
->index
))
2150 *dtrState
= !!(port
->lineCtrl
& DTR_ON
);
2152 *rtsState
= !!(port
->lineCtrl
& RTS_ON
);
2157 static void MoxaPortLineCtrl(struct moxa_port
*port
, int dtr
, int rts
)
2165 port
->lineCtrl
= mode
;
2166 moxafunc(port
->tableAddr
, FC_LineControl
, mode
);
2169 static void MoxaPortFlowCtrl(struct moxa_port
*port
, int rts
, int cts
,
2170 int txflow
, int rxflow
, int txany
)
2175 mode
|= RTS_FlowCtl
;
2177 mode
|= CTS_FlowCtl
;
2184 moxafunc(port
->tableAddr
, FC_SetFlowCtl
, mode
);
2187 static int MoxaPortLineStatus(struct moxa_port
*port
)
2189 void __iomem
*ofsAddr
;
2192 ofsAddr
= port
->tableAddr
;
2193 if (port
->board
->boardType
== MOXA_BOARD_C320_ISA
||
2194 port
->board
->boardType
== MOXA_BOARD_C320_PCI
) {
2195 moxafunc(ofsAddr
, FC_LineStatus
, 0);
2196 val
= readw(ofsAddr
+ FuncArg
);
2198 val
= readw(ofsAddr
+ FlagStat
) >> 4;
2203 if ((port
->DCDState
& DCD_oldstate
) == 0)
2204 port
->DCDState
= (DCD_oldstate
| DCD_changed
);
2206 if (port
->DCDState
& DCD_oldstate
)
2207 port
->DCDState
= DCD_changed
;
2213 static int MoxaPortDCDChange(struct moxa_port
*port
)
2217 if (port
->chkPort
== 0)
2220 port
->DCDState
&= ~DCD_changed
;
2225 static int MoxaPortDCDON(struct moxa_port
*port
)
2229 if (port
->chkPort
== 0)
2231 if (port
->DCDState
& DCD_oldstate
)
2238 static int MoxaPortWriteData(struct moxa_port
*port
, unsigned char *buffer
,
2244 ushort head
, tx_mask
, spage
, epage
;
2245 ushort pageno
, pageofs
, bufhead
;
2246 void __iomem
*baseAddr
, *ofsAddr
, *ofs
;
2248 ofsAddr
= port
->tableAddr
;
2249 baseAddr
= port
->board
->basemem
;
2250 tx_mask
= readw(ofsAddr
+ TX_mask
);
2251 spage
= readw(ofsAddr
+ Page_txb
);
2252 epage
= readw(ofsAddr
+ EndPage_txb
);
2253 tail
= readw(ofsAddr
+ TXwptr
);
2254 head
= readw(ofsAddr
+ TXrptr
);
2255 c
= (head
> tail
) ? (head
- tail
- 1)
2256 : (head
- tail
+ tx_mask
);
2259 moxaLog
.txcnt
[port
->tty
->index
] += c
;
2261 if (spage
== epage
) {
2262 bufhead
= readw(ofsAddr
+ Ofs_txb
);
2263 writew(spage
, baseAddr
+ Control_reg
);
2266 len
= head
- tail
- 1;
2268 len
= tx_mask
+ 1 - tail
;
2269 len
= (c
> len
) ? len
: c
;
2270 ofs
= baseAddr
+ DynPage_addr
+ bufhead
+ tail
;
2271 for (i
= 0; i
< len
; i
++)
2272 writeb(*buffer
++, ofs
+ i
);
2273 tail
= (tail
+ len
) & tx_mask
;
2276 writew(tail
, ofsAddr
+ TXwptr
);
2279 pageno
= spage
+ (tail
>> 13);
2280 pageofs
= tail
& Page_mask
;
2282 cnt
= Page_size
- pageofs
;
2286 writeb(pageno
, baseAddr
+ Control_reg
);
2287 ofs
= baseAddr
+ DynPage_addr
+ pageofs
;
2288 for (i
= 0; i
< cnt
; i
++)
2289 writeb(*buffer
++, ofs
+ i
);
2291 writew((tail
+ len
) & tx_mask
, ofsAddr
+ TXwptr
);
2294 if (++pageno
== epage
)
2299 writeb(1, ofsAddr
+ CD180TXirq
); /* start to send */
2303 static int MoxaPortReadData(struct moxa_port
*port
, struct tty_struct
*tty
)
2305 register ushort head
, pageofs
;
2306 int i
, count
, cnt
, len
, total
, remain
;
2307 ushort tail
, rx_mask
, spage
, epage
;
2308 ushort pageno
, bufhead
;
2309 void __iomem
*baseAddr
, *ofsAddr
, *ofs
;
2311 ofsAddr
= port
->tableAddr
;
2312 baseAddr
= port
->board
->basemem
;
2313 head
= readw(ofsAddr
+ RXrptr
);
2314 tail
= readw(ofsAddr
+ RXwptr
);
2315 rx_mask
= readw(ofsAddr
+ RX_mask
);
2316 spage
= readw(ofsAddr
+ Page_rxb
);
2317 epage
= readw(ofsAddr
+ EndPage_rxb
);
2318 count
= (tail
>= head
) ? (tail
- head
)
2319 : (tail
- head
+ rx_mask
+ 1);
2324 remain
= count
- total
;
2325 moxaLog
.rxcnt
[port
->tty
->index
] += total
;
2327 if (spage
== epage
) {
2328 bufhead
= readw(ofsAddr
+ Ofs_rxb
);
2329 writew(spage
, baseAddr
+ Control_reg
);
2334 len
= rx_mask
+ 1 - head
;
2335 len
= (count
> len
) ? len
: count
;
2336 ofs
= baseAddr
+ DynPage_addr
+ bufhead
+ head
;
2337 for (i
= 0; i
< len
; i
++)
2338 tty_insert_flip_char(tty
, readb(ofs
+ i
), TTY_NORMAL
);
2339 head
= (head
+ len
) & rx_mask
;
2342 writew(head
, ofsAddr
+ RXrptr
);
2345 pageno
= spage
+ (head
>> 13);
2346 pageofs
= head
& Page_mask
;
2348 cnt
= Page_size
- pageofs
;
2352 writew(pageno
, baseAddr
+ Control_reg
);
2353 ofs
= baseAddr
+ DynPage_addr
+ pageofs
;
2354 for (i
= 0; i
< cnt
; i
++)
2355 tty_insert_flip_char(tty
, readb(ofs
+ i
), TTY_NORMAL
);
2357 writew((head
+ len
) & rx_mask
, ofsAddr
+ RXrptr
);
2360 if (++pageno
== epage
)
2365 if ((readb(ofsAddr
+ FlagStat
) & Xoff_state
) && (remain
< LowWater
)) {
2366 moxaLowWaterChk
= 1;
2367 port
->lowChkFlag
= 1;
2373 static int MoxaPortTxQueue(struct moxa_port
*port
)
2375 void __iomem
*ofsAddr
= port
->tableAddr
;
2376 ushort rptr
, wptr
, mask
;
2379 rptr
= readw(ofsAddr
+ TXrptr
);
2380 wptr
= readw(ofsAddr
+ TXwptr
);
2381 mask
= readw(ofsAddr
+ TX_mask
);
2382 len
= (wptr
- rptr
) & mask
;
2386 static int MoxaPortTxFree(struct moxa_port
*port
)
2388 void __iomem
*ofsAddr
= port
->tableAddr
;
2389 ushort rptr
, wptr
, mask
;
2392 rptr
= readw(ofsAddr
+ TXrptr
);
2393 wptr
= readw(ofsAddr
+ TXwptr
);
2394 mask
= readw(ofsAddr
+ TX_mask
);
2395 len
= mask
- ((wptr
- rptr
) & mask
);
2399 static int MoxaPortRxQueue(struct moxa_port
*port
)
2401 void __iomem
*ofsAddr
= port
->tableAddr
;
2402 ushort rptr
, wptr
, mask
;
2405 rptr
= readw(ofsAddr
+ RXrptr
);
2406 wptr
= readw(ofsAddr
+ RXwptr
);
2407 mask
= readw(ofsAddr
+ RX_mask
);
2408 len
= (wptr
- rptr
) & mask
;
2413 static void MoxaPortTxDisable(struct moxa_port
*port
)
2415 moxafunc(port
->tableAddr
, FC_SetXoffState
, Magic_code
);
2418 static void MoxaPortTxEnable(struct moxa_port
*port
)
2420 moxafunc(port
->tableAddr
, FC_SetXonState
, Magic_code
);
2424 static int MoxaPortResetBrkCnt(struct moxa_port
*port
)
2427 cnt
= port
->breakCnt
;
2433 static void MoxaPortSendBreak(struct moxa_port
*port
, int ms100
)
2435 void __iomem
*ofsAddr
= port
->tableAddr
;
2438 moxafunc(ofsAddr
, FC_SendBreak
, Magic_code
);
2441 moxafunc(ofsAddr
, FC_SendBreak
, Magic_code
);
2444 moxafunc(ofsAddr
, FC_StopBreak
, Magic_code
);
2447 static int moxa_get_serial_info(struct moxa_port
*info
,
2448 struct serial_struct __user
*retinfo
)
2450 struct serial_struct tmp
;
2452 memset(&tmp
, 0, sizeof(tmp
));
2453 tmp
.type
= info
->type
;
2454 tmp
.line
= info
->tty
->index
;
2457 tmp
.flags
= info
->asyncflags
;
2458 tmp
.baud_base
= 921600;
2459 tmp
.close_delay
= info
->close_delay
;
2460 tmp
.closing_wait
= info
->closing_wait
;
2461 tmp
.custom_divisor
= 0;
2463 if(copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
2469 static int moxa_set_serial_info(struct moxa_port
*info
,
2470 struct serial_struct __user
*new_info
)
2472 struct serial_struct new_serial
;
2474 if(copy_from_user(&new_serial
, new_info
, sizeof(new_serial
)))
2477 if ((new_serial
.irq
!= 0) ||
2478 (new_serial
.port
!= 0) ||
2479 // (new_serial.type != info->type) ||
2480 (new_serial
.custom_divisor
!= 0) ||
2481 (new_serial
.baud_base
!= 921600))
2484 if (!capable(CAP_SYS_ADMIN
)) {
2485 if (((new_serial
.flags
& ~ASYNC_USR_MASK
) !=
2486 (info
->asyncflags
& ~ASYNC_USR_MASK
)))
2489 info
->close_delay
= new_serial
.close_delay
* HZ
/ 100;
2490 info
->closing_wait
= new_serial
.closing_wait
* HZ
/ 100;
2493 new_serial
.flags
= (new_serial
.flags
& ~ASYNC_FLAGS
);
2494 new_serial
.flags
|= (info
->asyncflags
& ASYNC_FLAGS
);
2496 if (new_serial
.type
== PORT_16550A
) {
2497 MoxaSetFifo(info
, 1);
2499 MoxaSetFifo(info
, 0);
2502 info
->type
= new_serial
.type
;
2508 /*****************************************************************************
2509 * Static local functions: *
2510 *****************************************************************************/
2511 static void moxafunc(void __iomem
*ofsAddr
, int cmd
, ushort arg
)
2514 writew(arg
, ofsAddr
+ FuncArg
);
2515 writew(cmd
, ofsAddr
+ FuncCode
);
2516 moxa_wait_finish(ofsAddr
);
2519 static void moxa_wait_finish(void __iomem
*ofsAddr
)
2524 while (readw(ofsAddr
+ FuncCode
) != 0) {
2526 if ((j
- i
) > moxaFuncTout
) {
2532 static void moxa_low_water_check(void __iomem
*ofsAddr
)
2535 ushort rptr
, wptr
, mask
;
2537 if (readb(ofsAddr
+ FlagStat
) & Xoff_state
) {
2538 rptr
= readw(ofsAddr
+ RXrptr
);
2539 wptr
= readw(ofsAddr
+ RXwptr
);
2540 mask
= readw(ofsAddr
+ RX_mask
);
2541 len
= (wptr
- rptr
) & mask
;
2542 if (len
<= Low_water
)
2543 moxafunc(ofsAddr
, FC_SendXon
, 0);
2547 static void MoxaSetFifo(struct moxa_port
*port
, int enable
)
2549 void __iomem
*ofsAddr
= port
->tableAddr
;
2552 moxafunc(ofsAddr
, FC_SetRxFIFOTrig
, 0);
2553 moxafunc(ofsAddr
, FC_SetTxFIFOCnt
, 1);
2555 moxafunc(ofsAddr
, FC_SetRxFIFOTrig
, 3);
2556 moxafunc(ofsAddr
, FC_SetTxFIFOCnt
, 16);