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
;
132 struct tty_struct
*tty
;
133 void __iomem
*tableAddr
;
141 unsigned long statusflags
;
142 wait_queue_head_t open_wait
;
143 struct completion close_wait
;
152 int rxcnt
[MAX_PORTS
];
153 int txcnt
[MAX_PORTS
];
157 #define TXSTOPPED 0x1
159 #define EMPTYWAIT 0x4
162 #define SERIAL_DO_RESTART
164 #define WAKEUP_CHARS 256
166 static int ttymajor
= MOXAMAJOR
;
167 static struct mon_str moxaLog
;
168 static unsigned int moxaFuncTout
= HZ
/ 2;
169 static unsigned int moxaLowWaterChk
;
170 /* Variables for insmod */
172 static unsigned long baseaddr
[MAX_BOARDS
];
173 static unsigned int type
[MAX_BOARDS
];
174 static unsigned int numports
[MAX_BOARDS
];
177 MODULE_AUTHOR("William Chen");
178 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
179 MODULE_LICENSE("GPL");
181 module_param_array(type
, uint
, NULL
, 0);
182 MODULE_PARM_DESC(type
, "card type: C218=2, C320=4");
183 module_param_array(baseaddr
, ulong
, NULL
, 0);
184 MODULE_PARM_DESC(baseaddr
, "base address");
185 module_param_array(numports
, uint
, NULL
, 0);
186 MODULE_PARM_DESC(numports
, "numports (ignored for C218)");
188 module_param(ttymajor
, int, 0);
193 static int moxa_open(struct tty_struct
*, struct file
*);
194 static void moxa_close(struct tty_struct
*, struct file
*);
195 static int moxa_write(struct tty_struct
*, const unsigned char *, int);
196 static int moxa_write_room(struct tty_struct
*);
197 static void moxa_flush_buffer(struct tty_struct
*);
198 static int moxa_chars_in_buffer(struct tty_struct
*);
199 static void moxa_flush_chars(struct tty_struct
*);
200 static void moxa_put_char(struct tty_struct
*, unsigned char);
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_shut_down(struct moxa_port
*);
217 * moxa board interface functions:
219 static void MoxaPortEnable(struct moxa_port
*);
220 static void MoxaPortDisable(struct moxa_port
*);
221 static int MoxaPortSetTermio(struct moxa_port
*, struct ktermios
*, speed_t
);
222 static int MoxaPortGetLineOut(struct moxa_port
*, int *, int *);
223 static void MoxaPortLineCtrl(struct moxa_port
*, int, int);
224 static void MoxaPortFlowCtrl(struct moxa_port
*, int, int, int, int, int);
225 static int MoxaPortLineStatus(struct moxa_port
*);
226 static void MoxaPortFlushData(struct moxa_port
*, int);
227 static int MoxaPortWriteData(struct moxa_port
*, const unsigned char *, int);
228 static int MoxaPortReadData(struct moxa_port
*);
229 static int MoxaPortTxQueue(struct moxa_port
*);
230 static int MoxaPortRxQueue(struct moxa_port
*);
231 static int MoxaPortTxFree(struct moxa_port
*);
232 static void MoxaPortTxDisable(struct moxa_port
*);
233 static void MoxaPortTxEnable(struct moxa_port
*);
234 static int moxa_get_serial_info(struct moxa_port
*, struct serial_struct __user
*);
235 static int moxa_set_serial_info(struct moxa_port
*, struct serial_struct __user
*);
236 static void MoxaSetFifo(struct moxa_port
*port
, int enable
);
242 static void moxa_wait_finish(void __iomem
*ofsAddr
)
244 unsigned long end
= jiffies
+ moxaFuncTout
;
246 while (readw(ofsAddr
+ FuncCode
) != 0)
247 if (time_after(jiffies
, end
))
249 if (readw(ofsAddr
+ FuncCode
) != 0 && printk_ratelimit())
250 printk(KERN_WARNING
"moxa function expired\n");
253 static void moxafunc(void __iomem
*ofsAddr
, int cmd
, ushort arg
)
255 writew(arg
, ofsAddr
+ FuncArg
);
256 writew(cmd
, ofsAddr
+ FuncCode
);
257 moxa_wait_finish(ofsAddr
);
260 static void moxa_low_water_check(void __iomem
*ofsAddr
)
262 u16 rptr
, wptr
, mask
, len
;
264 if (readb(ofsAddr
+ FlagStat
) & Xoff_state
) {
265 rptr
= readw(ofsAddr
+ RXrptr
);
266 wptr
= readw(ofsAddr
+ RXwptr
);
267 mask
= readw(ofsAddr
+ RX_mask
);
268 len
= (wptr
- rptr
) & mask
;
269 if (len
<= Low_water
)
270 moxafunc(ofsAddr
, FC_SendXon
, 0);
278 static int moxa_ioctl(struct tty_struct
*tty
, struct file
*file
,
279 unsigned int cmd
, unsigned long arg
)
281 struct moxa_port
*ch
= tty
->driver_data
;
282 void __user
*argp
= (void __user
*)arg
;
285 if (tty
->index
== MAX_PORTS
) {
286 if (cmd
!= MOXA_GETDATACOUNT
&& cmd
!= MOXA_GET_IOQUEUE
&&
287 cmd
!= MOXA_GETMSTATUS
)
293 case MOXA_GETDATACOUNT
:
294 moxaLog
.tick
= jiffies
;
295 return copy_to_user(argp
, &moxaLog
, sizeof(moxaLog
)) ?
297 case MOXA_FLUSH_QUEUE
:
298 MoxaPortFlushData(ch
, arg
);
300 case MOXA_GET_IOQUEUE
: {
301 struct moxaq_str __user
*argm
= argp
;
302 struct moxaq_str tmp
;
306 for (i
= 0; i
< MAX_BOARDS
; i
++) {
307 p
= moxa_boards
[i
].ports
;
308 for (j
= 0; j
< MAX_PORTS_PER_BOARD
; j
++, p
++, argm
++) {
309 memset(&tmp
, 0, sizeof(tmp
));
310 if (moxa_boards
[i
].ready
) {
311 tmp
.inq
= MoxaPortRxQueue(p
);
312 tmp
.outq
= MoxaPortTxQueue(p
);
314 if (copy_to_user(argm
, &tmp
, sizeof(tmp
)))
319 } case MOXA_GET_OQUEUE
:
320 status
= MoxaPortTxQueue(ch
);
321 return put_user(status
, (unsigned long __user
*)argp
);
322 case MOXA_GET_IQUEUE
:
323 status
= MoxaPortRxQueue(ch
);
324 return put_user(status
, (unsigned long __user
*)argp
);
325 case MOXA_GETMSTATUS
: {
326 struct mxser_mstatus __user
*argm
= argp
;
327 struct mxser_mstatus tmp
;
331 for (i
= 0; i
< MAX_BOARDS
; i
++) {
332 p
= moxa_boards
[i
].ports
;
333 for (j
= 0; j
< MAX_PORTS_PER_BOARD
; j
++, p
++, argm
++) {
334 memset(&tmp
, 0, sizeof(tmp
));
335 if (!moxa_boards
[i
].ready
)
338 status
= MoxaPortLineStatus(p
);
346 if (!p
->tty
|| !p
->tty
->termios
)
347 tmp
.cflag
= p
->cflag
;
349 tmp
.cflag
= p
->tty
->termios
->c_cflag
;
351 if (copy_to_user(argm
, &tmp
, sizeof(tmp
)))
358 return moxa_get_serial_info(ch
, argp
);
360 return moxa_set_serial_info(ch
, argp
);
365 static void moxa_break_ctl(struct tty_struct
*tty
, int state
)
367 struct moxa_port
*port
= tty
->driver_data
;
369 moxafunc(port
->tableAddr
, state
? FC_SendBreak
: FC_StopBreak
,
373 static const struct tty_operations moxa_ops
= {
377 .write_room
= moxa_write_room
,
378 .flush_buffer
= moxa_flush_buffer
,
379 .chars_in_buffer
= moxa_chars_in_buffer
,
380 .flush_chars
= moxa_flush_chars
,
381 .put_char
= moxa_put_char
,
383 .throttle
= moxa_throttle
,
384 .unthrottle
= moxa_unthrottle
,
385 .set_termios
= moxa_set_termios
,
388 .hangup
= moxa_hangup
,
389 .break_ctl
= moxa_break_ctl
,
390 .tiocmget
= moxa_tiocmget
,
391 .tiocmset
= moxa_tiocmset
,
394 static struct tty_driver
*moxaDriver
;
395 static DEFINE_TIMER(moxaTimer
, moxa_poll
, 0, 0);
396 static DEFINE_SPINLOCK(moxa_lock
);
402 static int moxa_check_fw_model(struct moxa_board_conf
*brd
, u8 model
)
404 switch (brd
->boardType
) {
405 case MOXA_BOARD_C218_ISA
:
406 case MOXA_BOARD_C218_PCI
:
410 case MOXA_BOARD_CP204J
:
424 static int moxa_check_fw(const void *ptr
)
426 const __le16
*lptr
= ptr
;
428 if (*lptr
!= cpu_to_le16(0x7980))
434 static int moxa_load_bios(struct moxa_board_conf
*brd
, const u8
*buf
,
437 void __iomem
*baseAddr
= brd
->basemem
;
440 writeb(HW_reset
, baseAddr
+ Control_reg
); /* reset */
442 memset_io(baseAddr
, 0, 4096);
443 memcpy_toio(baseAddr
, buf
, len
); /* download BIOS */
444 writeb(0, baseAddr
+ Control_reg
); /* restart */
448 switch (brd
->boardType
) {
449 case MOXA_BOARD_C218_ISA
:
450 case MOXA_BOARD_C218_PCI
:
451 tmp
= readw(baseAddr
+ C218_key
);
452 if (tmp
!= C218_KeyCode
)
455 case MOXA_BOARD_CP204J
:
456 tmp
= readw(baseAddr
+ C218_key
);
457 if (tmp
!= CP204J_KeyCode
)
461 tmp
= readw(baseAddr
+ C320_key
);
462 if (tmp
!= C320_KeyCode
)
464 tmp
= readw(baseAddr
+ C320_status
);
465 if (tmp
!= STS_init
) {
466 printk(KERN_ERR
"moxa: bios upload failed -- CPU/Basic "
467 "module not found\n");
475 printk(KERN_ERR
"moxa: bios upload failed -- board not found\n");
479 static int moxa_load_320b(struct moxa_board_conf
*brd
, const u8
*ptr
,
482 void __iomem
*baseAddr
= brd
->basemem
;
485 printk(KERN_ERR
"moxa: invalid 320 bios -- too short\n");
489 writew(len
- 7168 - 2, baseAddr
+ C320bapi_len
);
490 writeb(1, baseAddr
+ Control_reg
); /* Select Page 1 */
491 memcpy_toio(baseAddr
+ DynPage_addr
, ptr
, 7168);
492 writeb(2, baseAddr
+ Control_reg
); /* Select Page 2 */
493 memcpy_toio(baseAddr
+ DynPage_addr
, ptr
+ 7168, len
- 7168);
498 static int moxa_real_load_code(struct moxa_board_conf
*brd
, const void *ptr
,
501 void __iomem
*baseAddr
= brd
->basemem
;
502 const u16
*uptr
= ptr
;
503 size_t wlen
, len2
, j
;
504 unsigned long key
, loadbuf
, loadlen
, checksum
, checksum_ok
;
505 unsigned int i
, retry
, c320
;
508 c320
= brd
->boardType
== MOXA_BOARD_C320_PCI
||
509 brd
->boardType
== MOXA_BOARD_C320_ISA
;
510 keycode
= (brd
->boardType
== MOXA_BOARD_CP204J
) ? CP204J_KeyCode
:
513 switch (brd
->boardType
) {
514 case MOXA_BOARD_CP204J
:
515 case MOXA_BOARD_C218_ISA
:
516 case MOXA_BOARD_C218_PCI
:
518 loadbuf
= C218_LoadBuf
;
519 loadlen
= C218DLoad_len
;
520 checksum
= C218check_sum
;
521 checksum_ok
= C218chksum_ok
;
525 keycode
= C320_KeyCode
;
526 loadbuf
= C320_LoadBuf
;
527 loadlen
= C320DLoad_len
;
528 checksum
= C320check_sum
;
529 checksum_ok
= C320chksum_ok
;
535 for (i
= 0; i
< wlen
; i
++)
536 usum
+= le16_to_cpu(uptr
[i
]);
542 len2
= (wlen
> 2048) ? 2048 : wlen
;
544 memcpy_toio(baseAddr
+ loadbuf
, ptr
+ j
, len2
<< 1);
547 writew(len2
, baseAddr
+ loadlen
);
548 writew(0, baseAddr
+ key
);
549 for (i
= 0; i
< 100; i
++) {
550 if (readw(baseAddr
+ key
) == keycode
)
554 if (readw(baseAddr
+ key
) != keycode
)
557 writew(0, baseAddr
+ loadlen
);
558 writew(usum
, baseAddr
+ checksum
);
559 writew(0, baseAddr
+ key
);
560 for (i
= 0; i
< 100; i
++) {
561 if (readw(baseAddr
+ key
) == keycode
)
566 } while ((readb(baseAddr
+ checksum_ok
) != 1) && (retry
< 3));
567 if (readb(baseAddr
+ checksum_ok
) != 1)
570 writew(0, baseAddr
+ key
);
571 for (i
= 0; i
< 600; i
++) {
572 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
576 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
580 if (brd
->busType
== MOXA_BUS_TYPE_PCI
) { /* ASIC board */
581 writew(0x3800, baseAddr
+ TMS320_PORT1
);
582 writew(0x3900, baseAddr
+ TMS320_PORT2
);
583 writew(28499, baseAddr
+ TMS320_CLOCK
);
585 writew(0x3200, baseAddr
+ TMS320_PORT1
);
586 writew(0x3400, baseAddr
+ TMS320_PORT2
);
587 writew(19999, baseAddr
+ TMS320_CLOCK
);
590 writew(1, baseAddr
+ Disable_IRQ
);
591 writew(0, baseAddr
+ Magic_no
);
592 for (i
= 0; i
< 500; i
++) {
593 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
597 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
601 j
= readw(baseAddr
+ Module_cnt
);
604 brd
->numPorts
= j
* 8;
605 writew(j
, baseAddr
+ Module_no
);
606 writew(0, baseAddr
+ Magic_no
);
607 for (i
= 0; i
< 600; i
++) {
608 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
612 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
615 brd
->intNdx
= baseAddr
+ IRQindex
;
616 brd
->intPend
= baseAddr
+ IRQpending
;
617 brd
->intTable
= baseAddr
+ IRQtable
;
622 static int moxa_load_code(struct moxa_board_conf
*brd
, const void *ptr
,
625 void __iomem
*ofsAddr
, *baseAddr
= brd
->basemem
;
626 struct moxa_port
*port
;
630 printk(KERN_ERR
"moxa: bios length is not even\n");
634 retval
= moxa_real_load_code(brd
, ptr
, len
); /* may change numPorts */
638 switch (brd
->boardType
) {
639 case MOXA_BOARD_C218_ISA
:
640 case MOXA_BOARD_C218_PCI
:
641 case MOXA_BOARD_CP204J
:
643 for (i
= 0; i
< brd
->numPorts
; i
++, port
++) {
646 port
->tableAddr
= baseAddr
+ Extern_table
+
648 ofsAddr
= port
->tableAddr
;
649 writew(C218rx_mask
, ofsAddr
+ RX_mask
);
650 writew(C218tx_mask
, ofsAddr
+ TX_mask
);
651 writew(C218rx_spage
+ i
* C218buf_pageno
, ofsAddr
+ Page_rxb
);
652 writew(readw(ofsAddr
+ Page_rxb
) + C218rx_pageno
, ofsAddr
+ EndPage_rxb
);
654 writew(C218tx_spage
+ i
* C218buf_pageno
, ofsAddr
+ Page_txb
);
655 writew(readw(ofsAddr
+ Page_txb
) + C218tx_pageno
, ofsAddr
+ EndPage_txb
);
661 for (i
= 0; i
< brd
->numPorts
; i
++, port
++) {
664 port
->tableAddr
= baseAddr
+ Extern_table
+
666 ofsAddr
= port
->tableAddr
;
667 switch (brd
->numPorts
) {
669 writew(C320p8rx_mask
, ofsAddr
+ RX_mask
);
670 writew(C320p8tx_mask
, ofsAddr
+ TX_mask
);
671 writew(C320p8rx_spage
+ i
* C320p8buf_pgno
, ofsAddr
+ Page_rxb
);
672 writew(readw(ofsAddr
+ Page_rxb
) + C320p8rx_pgno
, ofsAddr
+ EndPage_rxb
);
673 writew(C320p8tx_spage
+ i
* C320p8buf_pgno
, ofsAddr
+ Page_txb
);
674 writew(readw(ofsAddr
+ Page_txb
) + C320p8tx_pgno
, ofsAddr
+ EndPage_txb
);
678 writew(C320p16rx_mask
, ofsAddr
+ RX_mask
);
679 writew(C320p16tx_mask
, ofsAddr
+ TX_mask
);
680 writew(C320p16rx_spage
+ i
* C320p16buf_pgno
, ofsAddr
+ Page_rxb
);
681 writew(readw(ofsAddr
+ Page_rxb
) + C320p16rx_pgno
, ofsAddr
+ EndPage_rxb
);
682 writew(C320p16tx_spage
+ i
* C320p16buf_pgno
, ofsAddr
+ Page_txb
);
683 writew(readw(ofsAddr
+ Page_txb
) + C320p16tx_pgno
, ofsAddr
+ EndPage_txb
);
687 writew(C320p24rx_mask
, ofsAddr
+ RX_mask
);
688 writew(C320p24tx_mask
, ofsAddr
+ TX_mask
);
689 writew(C320p24rx_spage
+ i
* C320p24buf_pgno
, ofsAddr
+ Page_rxb
);
690 writew(readw(ofsAddr
+ Page_rxb
) + C320p24rx_pgno
, ofsAddr
+ EndPage_rxb
);
691 writew(C320p24tx_spage
+ i
* C320p24buf_pgno
, ofsAddr
+ Page_txb
);
692 writew(readw(ofsAddr
+ Page_txb
), ofsAddr
+ EndPage_txb
);
695 writew(C320p32rx_mask
, ofsAddr
+ RX_mask
);
696 writew(C320p32tx_mask
, ofsAddr
+ TX_mask
);
697 writew(C320p32tx_ofs
, ofsAddr
+ Ofs_txb
);
698 writew(C320p32rx_spage
+ i
* C320p32buf_pgno
, ofsAddr
+ Page_rxb
);
699 writew(readb(ofsAddr
+ Page_rxb
), ofsAddr
+ EndPage_rxb
);
700 writew(C320p32tx_spage
+ i
* C320p32buf_pgno
, ofsAddr
+ Page_txb
);
701 writew(readw(ofsAddr
+ Page_txb
), ofsAddr
+ EndPage_txb
);
710 static int moxa_load_fw(struct moxa_board_conf
*brd
, const struct firmware
*fw
)
712 void *ptr
= fw
->data
;
716 unsigned int a
, lenp
, lencnt
;
719 __le32 magic
; /* 0x34303430 */
721 u8 type
; /* UNIX = 3 */
722 u8 model
; /* C218T=1, C320T=2, CP204=3 */
727 BUILD_BUG_ON(ARRAY_SIZE(hdr
->len
) != ARRAY_SIZE(lens
));
729 if (fw
->size
< MOXA_FW_HDRLEN
) {
730 strcpy(rsn
, "too short (even header won't fit)");
733 if (hdr
->magic
!= cpu_to_le32(0x30343034)) {
734 sprintf(rsn
, "bad magic: %.8x", le32_to_cpu(hdr
->magic
));
737 if (hdr
->type
!= 3) {
738 sprintf(rsn
, "not for linux, type is %u", hdr
->type
);
741 if (moxa_check_fw_model(brd
, hdr
->model
)) {
742 sprintf(rsn
, "not for this card, model is %u", hdr
->model
);
746 len
= MOXA_FW_HDRLEN
;
747 lencnt
= hdr
->model
== 2 ? 5 : 3;
748 for (a
= 0; a
< ARRAY_SIZE(lens
); a
++) {
749 lens
[a
] = le16_to_cpu(hdr
->len
[a
]);
750 if (lens
[a
] && len
+ lens
[a
] <= fw
->size
&&
751 moxa_check_fw(&fw
->data
[len
]))
752 printk(KERN_WARNING
"moxa firmware: unexpected input "
753 "at offset %u, but going on\n", (u32
)len
);
754 if (!lens
[a
] && a
< lencnt
) {
755 sprintf(rsn
, "too few entries in fw file");
761 if (len
!= fw
->size
) {
762 sprintf(rsn
, "bad length: %u (should be %u)", (u32
)fw
->size
,
767 ptr
+= MOXA_FW_HDRLEN
;
770 strcpy(rsn
, "read above");
772 ret
= moxa_load_bios(brd
, ptr
, lens
[lenp
]);
776 /* we skip the tty section (lens[1]), since we don't need it */
777 ptr
+= lens
[lenp
] + lens
[lenp
+ 1];
778 lenp
+= 2; /* comm */
780 if (hdr
->model
== 2) {
781 ret
= moxa_load_320b(brd
, ptr
, lens
[lenp
]);
784 /* skip another tty */
785 ptr
+= lens
[lenp
] + lens
[lenp
+ 1];
789 ret
= moxa_load_code(brd
, ptr
, lens
[lenp
]);
795 printk(KERN_ERR
"firmware failed to load, reason: %s\n", rsn
);
799 static int moxa_init_board(struct moxa_board_conf
*brd
, struct device
*dev
)
801 const struct firmware
*fw
;
807 brd
->ports
= kcalloc(MAX_PORTS_PER_BOARD
, sizeof(*brd
->ports
),
809 if (brd
->ports
== NULL
) {
810 printk(KERN_ERR
"cannot allocate memory for ports\n");
815 for (i
= 0, p
= brd
->ports
; i
< MAX_PORTS_PER_BOARD
; i
++, p
++) {
816 p
->type
= PORT_16550A
;
817 p
->close_delay
= 5 * HZ
/ 10;
818 p
->cflag
= B9600
| CS8
| CREAD
| CLOCAL
| HUPCL
;
819 init_waitqueue_head(&p
->open_wait
);
820 init_completion(&p
->close_wait
);
823 switch (brd
->boardType
) {
824 case MOXA_BOARD_C218_ISA
:
825 case MOXA_BOARD_C218_PCI
:
826 file
= "c218tunx.cod";
828 case MOXA_BOARD_CP204J
:
829 file
= "cp204unx.cod";
832 file
= "c320tunx.cod";
836 ret
= request_firmware(&fw
, file
, dev
);
838 printk(KERN_ERR
"request_firmware failed\n");
842 ret
= moxa_load_fw(brd
, fw
);
844 release_firmware(fw
);
849 spin_lock_bh(&moxa_lock
);
851 if (!timer_pending(&moxaTimer
))
852 mod_timer(&moxaTimer
, jiffies
+ HZ
/ 50);
853 spin_unlock_bh(&moxa_lock
);
862 static void moxa_board_deinit(struct moxa_board_conf
*brd
)
864 spin_lock_bh(&moxa_lock
);
866 spin_unlock_bh(&moxa_lock
);
867 iounmap(brd
->basemem
);
873 static int __devinit
moxa_pci_probe(struct pci_dev
*pdev
,
874 const struct pci_device_id
*ent
)
876 struct moxa_board_conf
*board
;
878 int board_type
= ent
->driver_data
;
881 retval
= pci_enable_device(pdev
);
883 dev_err(&pdev
->dev
, "can't enable pci device\n");
887 for (i
= 0; i
< MAX_BOARDS
; i
++)
888 if (moxa_boards
[i
].basemem
== NULL
)
892 if (i
>= MAX_BOARDS
) {
893 dev_warn(&pdev
->dev
, "more than %u MOXA Intellio family boards "
894 "found. Board is ignored.\n", MAX_BOARDS
);
898 board
= &moxa_boards
[i
];
900 retval
= pci_request_region(pdev
, 2, "moxa-base");
902 dev_err(&pdev
->dev
, "can't request pci region 2\n");
906 board
->basemem
= ioremap(pci_resource_start(pdev
, 2), 0x4000);
907 if (board
->basemem
== NULL
) {
908 dev_err(&pdev
->dev
, "can't remap io space 2\n");
912 board
->boardType
= board_type
;
913 switch (board_type
) {
914 case MOXA_BOARD_C218_ISA
:
915 case MOXA_BOARD_C218_PCI
:
919 case MOXA_BOARD_CP204J
:
926 board
->busType
= MOXA_BUS_TYPE_PCI
;
928 retval
= moxa_init_board(board
, &pdev
->dev
);
932 pci_set_drvdata(pdev
, board
);
936 iounmap(board
->basemem
);
937 board
->basemem
= NULL
;
939 pci_release_region(pdev
, 2);
944 static void __devexit
moxa_pci_remove(struct pci_dev
*pdev
)
946 struct moxa_board_conf
*brd
= pci_get_drvdata(pdev
);
948 moxa_board_deinit(brd
);
950 pci_release_region(pdev
, 2);
953 static struct pci_driver moxa_pci_driver
= {
955 .id_table
= moxa_pcibrds
,
956 .probe
= moxa_pci_probe
,
957 .remove
= __devexit_p(moxa_pci_remove
)
959 #endif /* CONFIG_PCI */
961 static int __init
moxa_init(void)
963 unsigned int isabrds
= 0;
966 printk(KERN_INFO
"MOXA Intellio family driver version %s\n",
968 moxaDriver
= alloc_tty_driver(MAX_PORTS
+ 1);
972 moxaDriver
->owner
= THIS_MODULE
;
973 moxaDriver
->name
= "ttyMX";
974 moxaDriver
->major
= ttymajor
;
975 moxaDriver
->minor_start
= 0;
976 moxaDriver
->type
= TTY_DRIVER_TYPE_SERIAL
;
977 moxaDriver
->subtype
= SERIAL_TYPE_NORMAL
;
978 moxaDriver
->init_termios
= tty_std_termios
;
979 moxaDriver
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| CLOCAL
| HUPCL
;
980 moxaDriver
->init_termios
.c_ispeed
= 9600;
981 moxaDriver
->init_termios
.c_ospeed
= 9600;
982 moxaDriver
->flags
= TTY_DRIVER_REAL_RAW
;
983 tty_set_operations(moxaDriver
, &moxa_ops
);
985 pr_debug("Moxa tty devices major number = %d\n", ttymajor
);
987 if (tty_register_driver(moxaDriver
)) {
988 printk(KERN_ERR
"Couldn't install MOXA Smartio family driver !\n");
989 put_tty_driver(moxaDriver
);
993 /* Find the boards defined from module args. */
996 struct moxa_board_conf
*brd
= moxa_boards
;
998 for (i
= 0; i
< MAX_BOARDS
; i
++) {
1001 if (type
[i
] == MOXA_BOARD_C218_ISA
||
1002 type
[i
] == MOXA_BOARD_C320_ISA
) {
1003 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
1004 isabrds
+ 1, moxa_brdname
[type
[i
] - 1],
1006 brd
->boardType
= type
[i
];
1007 brd
->numPorts
= type
[i
] == MOXA_BOARD_C218_ISA
? 8 :
1009 brd
->busType
= MOXA_BUS_TYPE_ISA
;
1010 brd
->basemem
= ioremap(baseaddr
[i
], 0x4000);
1011 if (!brd
->basemem
) {
1012 printk(KERN_ERR
"moxa: can't remap %lx\n",
1016 if (moxa_init_board(brd
, NULL
)) {
1017 iounmap(brd
->basemem
);
1018 brd
->basemem
= NULL
;
1030 retval
= pci_register_driver(&moxa_pci_driver
);
1032 printk(KERN_ERR
"Can't register moxa pci driver!\n");
1041 static void __exit
moxa_exit(void)
1046 pci_unregister_driver(&moxa_pci_driver
);
1049 for (i
= 0; i
< MAX_BOARDS
; i
++) /* ISA boards */
1050 if (moxa_boards
[i
].ready
)
1051 moxa_board_deinit(&moxa_boards
[i
]);
1053 del_timer_sync(&moxaTimer
);
1055 if (tty_unregister_driver(moxaDriver
))
1056 printk(KERN_ERR
"Couldn't unregister MOXA Intellio family "
1058 put_tty_driver(moxaDriver
);
1061 module_init(moxa_init
);
1062 module_exit(moxa_exit
);
1064 static int moxa_open(struct tty_struct
*tty
, struct file
*filp
)
1066 struct moxa_board_conf
*brd
;
1067 struct moxa_port
*ch
;
1072 if (port
== MAX_PORTS
) {
1073 return capable(CAP_SYS_ADMIN
) ? 0 : -EPERM
;
1075 brd
= &moxa_boards
[port
/ MAX_PORTS_PER_BOARD
];
1079 ch
= &brd
->ports
[port
% MAX_PORTS_PER_BOARD
];
1081 tty
->driver_data
= ch
;
1083 if (!(ch
->asyncflags
& ASYNC_INITIALIZED
)) {
1084 ch
->statusflags
= 0;
1085 moxa_set_tty_param(tty
, tty
->termios
);
1086 MoxaPortLineCtrl(ch
, 1, 1);
1088 ch
->asyncflags
|= ASYNC_INITIALIZED
;
1090 retval
= moxa_block_till_ready(tty
, filp
, ch
);
1092 moxa_unthrottle(tty
);
1094 if (ch
->type
== PORT_16550A
) {
1103 static void moxa_close(struct tty_struct
*tty
, struct file
*filp
)
1105 struct moxa_port
*ch
;
1109 if (port
== MAX_PORTS
) {
1112 if (tty
->driver_data
== NULL
) {
1115 if (tty_hung_up_p(filp
)) {
1118 ch
= (struct moxa_port
*) tty
->driver_data
;
1120 if ((tty
->count
== 1) && (ch
->count
!= 1)) {
1121 printk(KERN_WARNING
"moxa_close: bad serial port count; "
1122 "tty->count is 1, ch->count is %d\n", ch
->count
);
1125 if (--ch
->count
< 0) {
1126 printk(KERN_WARNING
"moxa_close: bad serial port count, "
1127 "device=%s\n", tty
->name
);
1133 ch
->asyncflags
|= ASYNC_CLOSING
;
1135 ch
->cflag
= tty
->termios
->c_cflag
;
1136 if (ch
->asyncflags
& ASYNC_INITIALIZED
) {
1137 moxa_setup_empty_event(tty
);
1138 tty_wait_until_sent(tty
, 30 * HZ
); /* 30 seconds timeout */
1141 MoxaPortFlushData(ch
, 2);
1143 if (tty
->driver
->flush_buffer
)
1144 tty
->driver
->flush_buffer(tty
);
1145 tty_ldisc_flush(tty
);
1149 if (ch
->blocked_open
) {
1150 if (ch
->close_delay
) {
1151 msleep_interruptible(jiffies_to_msecs(ch
->close_delay
));
1153 wake_up_interruptible(&ch
->open_wait
);
1155 ch
->asyncflags
&= ~(ASYNC_NORMAL_ACTIVE
| ASYNC_CLOSING
);
1156 complete_all(&ch
->close_wait
);
1159 static int moxa_write(struct tty_struct
*tty
,
1160 const unsigned char *buf
, int count
)
1162 struct moxa_port
*ch
= tty
->driver_data
;
1168 spin_lock_bh(&moxa_lock
);
1169 len
= MoxaPortWriteData(ch
, buf
, count
);
1170 spin_unlock_bh(&moxa_lock
);
1172 /*********************************************
1173 if ( !(ch->statusflags & LOWWAIT) &&
1174 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
1175 ************************************************/
1176 ch
->statusflags
|= LOWWAIT
;
1180 static int moxa_write_room(struct tty_struct
*tty
)
1182 struct moxa_port
*ch
;
1186 ch
= tty
->driver_data
;
1189 return MoxaPortTxFree(ch
);
1192 static void moxa_flush_buffer(struct tty_struct
*tty
)
1194 struct moxa_port
*ch
= tty
->driver_data
;
1198 MoxaPortFlushData(ch
, 1);
1202 static int moxa_chars_in_buffer(struct tty_struct
*tty
)
1204 struct moxa_port
*ch
= tty
->driver_data
;
1208 * Sigh...I have to check if driver_data is NULL here, because
1209 * if an open() fails, the TTY subsystem eventually calls
1210 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1211 * routine. And since the open() failed, we return 0 here. TDJ
1215 chars
= MoxaPortTxQueue(ch
);
1218 * Make it possible to wakeup anything waiting for output
1219 * in tty_ioctl.c, etc.
1221 if (!(ch
->statusflags
& EMPTYWAIT
))
1222 moxa_setup_empty_event(tty
);
1227 static void moxa_flush_chars(struct tty_struct
*tty
)
1230 * Don't think I need this, because this is called to empty the TX
1231 * buffer for the 16450, 16550, etc.
1235 static void moxa_put_char(struct tty_struct
*tty
, unsigned char c
)
1237 struct moxa_port
*ch
= tty
->driver_data
;
1241 spin_lock_bh(&moxa_lock
);
1242 MoxaPortWriteData(ch
, &c
, 1);
1243 spin_unlock_bh(&moxa_lock
);
1244 /************************************************
1245 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
1246 *************************************************/
1247 ch
->statusflags
|= LOWWAIT
;
1250 static int moxa_tiocmget(struct tty_struct
*tty
, struct file
*file
)
1252 struct moxa_port
*ch
= tty
->driver_data
;
1253 int flag
= 0, dtr
, rts
;
1258 MoxaPortGetLineOut(ch
, &dtr
, &rts
);
1263 dtr
= MoxaPortLineStatus(ch
);
1273 static int moxa_tiocmset(struct tty_struct
*tty
, struct file
*file
,
1274 unsigned int set
, unsigned int clear
)
1276 struct moxa_port
*ch
= tty
->driver_data
;
1284 MoxaPortGetLineOut(ch
, &dtr
, &rts
);
1285 if (set
& TIOCM_RTS
)
1287 if (set
& TIOCM_DTR
)
1289 if (clear
& TIOCM_RTS
)
1291 if (clear
& TIOCM_DTR
)
1293 MoxaPortLineCtrl(ch
, dtr
, rts
);
1297 static void moxa_throttle(struct tty_struct
*tty
)
1299 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
1301 ch
->statusflags
|= THROTTLE
;
1304 static void moxa_unthrottle(struct tty_struct
*tty
)
1306 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
1308 ch
->statusflags
&= ~THROTTLE
;
1311 static void moxa_set_termios(struct tty_struct
*tty
,
1312 struct ktermios
*old_termios
)
1314 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
1318 moxa_set_tty_param(tty
, old_termios
);
1319 if (!(old_termios
->c_cflag
& CLOCAL
) &&
1320 (tty
->termios
->c_cflag
& CLOCAL
))
1321 wake_up_interruptible(&ch
->open_wait
);
1324 static void moxa_stop(struct tty_struct
*tty
)
1326 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
1330 MoxaPortTxDisable(ch
);
1331 ch
->statusflags
|= TXSTOPPED
;
1335 static void moxa_start(struct tty_struct
*tty
)
1337 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
1342 if (!(ch
->statusflags
& TXSTOPPED
))
1345 MoxaPortTxEnable(ch
);
1346 ch
->statusflags
&= ~TXSTOPPED
;
1349 static void moxa_hangup(struct tty_struct
*tty
)
1351 struct moxa_port
*ch
= (struct moxa_port
*) tty
->driver_data
;
1353 moxa_flush_buffer(tty
);
1356 ch
->asyncflags
&= ~ASYNC_NORMAL_ACTIVE
;
1358 wake_up_interruptible(&ch
->open_wait
);
1361 static void moxa_new_dcdstate(struct moxa_port
*p
, u8 dcd
)
1365 if ((dcd
!= p
->DCDState
) && p
->tty
&& C_CLOCAL(p
->tty
)) {
1368 p
->asyncflags
&= ~ASYNC_NORMAL_ACTIVE
;
1370 wake_up_interruptible(&p
->open_wait
);
1375 static int moxa_poll_port(struct moxa_port
*p
, unsigned int handle
,
1378 struct tty_struct
*tty
= p
->tty
;
1379 void __iomem
*ofsAddr
;
1380 unsigned int inited
= p
->asyncflags
& ASYNC_INITIALIZED
;
1384 if ((p
->statusflags
& EMPTYWAIT
) &&
1385 MoxaPortTxQueue(p
) == 0) {
1386 p
->statusflags
&= ~EMPTYWAIT
;
1389 if ((p
->statusflags
& LOWWAIT
) && !tty
->stopped
&&
1390 MoxaPortTxQueue(p
) <= WAKEUP_CHARS
) {
1391 p
->statusflags
&= ~LOWWAIT
;
1395 if (inited
&& !(p
->statusflags
& THROTTLE
) &&
1396 MoxaPortRxQueue(p
) > 0) { /* RX */
1397 MoxaPortReadData(p
);
1398 tty_schedule_flip(tty
);
1401 p
->statusflags
&= ~EMPTYWAIT
;
1402 MoxaPortFlushData(p
, 0); /* flush RX */
1405 if (!handle
) /* nothing else to do */
1408 intr
= readw(ip
); /* port irq status */
1412 writew(0, ip
); /* ACK port */
1413 ofsAddr
= p
->tableAddr
;
1414 if (intr
& IntrTx
) /* disable tx intr */
1415 writew(readw(ofsAddr
+ HostStat
) & ~WakeupTx
,
1416 ofsAddr
+ HostStat
);
1421 if (tty
&& (intr
& IntrBreak
) && !I_IGNBRK(tty
)) { /* BREAK */
1422 tty_insert_flip_char(tty
, 0, TTY_BREAK
);
1423 tty_schedule_flip(tty
);
1426 if (intr
& IntrLine
)
1427 moxa_new_dcdstate(p
, readb(ofsAddr
+ FlagStat
) & DCD_state
);
1432 static void moxa_poll(unsigned long ignored
)
1434 struct moxa_board_conf
*brd
;
1436 unsigned int card
, port
, served
= 0;
1438 spin_lock(&moxa_lock
);
1439 for (card
= 0; card
< MAX_BOARDS
; card
++) {
1440 brd
= &moxa_boards
[card
];
1447 if (readb(brd
->intPend
) == 0xff)
1448 ip
= brd
->intTable
+ readb(brd
->intNdx
);
1450 for (port
= 0; port
< brd
->numPorts
; port
++)
1451 moxa_poll_port(&brd
->ports
[port
], !!ip
, ip
+ port
);
1454 writeb(0, brd
->intPend
); /* ACK */
1456 if (moxaLowWaterChk
) {
1457 struct moxa_port
*p
= brd
->ports
;
1458 for (port
= 0; port
< brd
->numPorts
; port
++, p
++)
1459 if (p
->lowChkFlag
) {
1461 moxa_low_water_check(p
->tableAddr
);
1465 moxaLowWaterChk
= 0;
1468 mod_timer(&moxaTimer
, jiffies
+ HZ
/ 50);
1469 spin_unlock(&moxa_lock
);
1472 /******************************************************************************/
1474 static void moxa_set_tty_param(struct tty_struct
*tty
, struct ktermios
*old_termios
)
1476 register struct ktermios
*ts
;
1477 struct moxa_port
*ch
;
1478 int rts
, cts
, txflow
, rxflow
, xany
, baud
;
1480 ch
= (struct moxa_port
*) tty
->driver_data
;
1482 rts
= cts
= txflow
= rxflow
= xany
= 0;
1483 if (ts
->c_cflag
& CRTSCTS
)
1485 if (ts
->c_iflag
& IXON
)
1487 if (ts
->c_iflag
& IXOFF
)
1489 if (ts
->c_iflag
& IXANY
)
1492 /* Clear the features we don't support */
1493 ts
->c_cflag
&= ~CMSPAR
;
1494 MoxaPortFlowCtrl(ch
, rts
, cts
, txflow
, rxflow
, xany
);
1495 baud
= MoxaPortSetTermio(ch
, ts
, tty_get_baud_rate(tty
));
1497 baud
= tty_termios_baud_rate(old_termios
);
1498 /* Not put the baud rate into the termios data */
1499 tty_encode_baud_rate(tty
, baud
, baud
);
1502 static int moxa_block_till_ready(struct tty_struct
*tty
, struct file
*filp
,
1503 struct moxa_port
*ch
)
1505 DECLARE_WAITQUEUE(wait
,current
);
1507 int do_clocal
= C_CLOCAL(tty
);
1510 * If the device is in the middle of being closed, then block
1511 * until it's done, and then try again.
1513 if (tty_hung_up_p(filp
) || (ch
->asyncflags
& ASYNC_CLOSING
)) {
1514 if (ch
->asyncflags
& ASYNC_CLOSING
)
1515 wait_for_completion_interruptible(&ch
->close_wait
);
1516 #ifdef SERIAL_DO_RESTART
1517 if (ch
->asyncflags
& ASYNC_HUP_NOTIFY
)
1520 return (-ERESTARTSYS
);
1526 * If non-blocking mode is set, then make the check up front
1529 if (filp
->f_flags
& O_NONBLOCK
) {
1530 ch
->asyncflags
|= ASYNC_NORMAL_ACTIVE
;
1534 * Block waiting for the carrier detect and the line to become free
1537 add_wait_queue(&ch
->open_wait
, &wait
);
1538 pr_debug("block_til_ready before block: ttys%d, count = %d\n",
1539 tty
->index
, ch
->count
);
1540 spin_lock_bh(&moxa_lock
);
1541 if (!tty_hung_up_p(filp
))
1544 spin_unlock_bh(&moxa_lock
);
1547 set_current_state(TASK_INTERRUPTIBLE
);
1548 if (tty_hung_up_p(filp
) ||
1549 !(ch
->asyncflags
& ASYNC_INITIALIZED
)) {
1550 #ifdef SERIAL_DO_RESTART
1551 if (ch
->asyncflags
& ASYNC_HUP_NOTIFY
)
1554 retval
= -ERESTARTSYS
;
1560 if (!(ch
->asyncflags
& ASYNC_CLOSING
) && (do_clocal
||
1564 if (signal_pending(current
)) {
1565 retval
= -ERESTARTSYS
;
1570 set_current_state(TASK_RUNNING
);
1571 remove_wait_queue(&ch
->open_wait
, &wait
);
1573 spin_lock_bh(&moxa_lock
);
1574 if (!tty_hung_up_p(filp
))
1577 spin_unlock_bh(&moxa_lock
);
1578 pr_debug("block_til_ready after blocking: ttys%d, count = %d\n",
1579 tty
->index
, ch
->count
);
1582 /* FIXME: review to see if we need to use set_bit on these */
1583 ch
->asyncflags
|= ASYNC_NORMAL_ACTIVE
;
1587 static void moxa_setup_empty_event(struct tty_struct
*tty
)
1589 struct moxa_port
*ch
= tty
->driver_data
;
1591 spin_lock_bh(&moxa_lock
);
1592 ch
->statusflags
|= EMPTYWAIT
;
1593 spin_unlock_bh(&moxa_lock
);
1596 static void moxa_shut_down(struct moxa_port
*ch
)
1598 struct tty_struct
*tp
;
1600 if (!(ch
->asyncflags
& ASYNC_INITIALIZED
))
1605 MoxaPortDisable(ch
);
1608 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1610 if (tp
->termios
->c_cflag
& HUPCL
)
1611 MoxaPortLineCtrl(ch
, 0, 0);
1613 ch
->asyncflags
&= ~ASYNC_INITIALIZED
;
1616 /*****************************************************************************
1617 * Driver level functions: *
1618 *****************************************************************************/
1620 static void MoxaPortFlushData(struct moxa_port
*port
, int mode
)
1622 void __iomem
*ofsAddr
;
1623 if ((mode
< 0) || (mode
> 2))
1625 ofsAddr
= port
->tableAddr
;
1626 moxafunc(ofsAddr
, FC_FlushQueue
, mode
);
1628 port
->lowChkFlag
= 0;
1629 moxa_low_water_check(ofsAddr
);
1633 /*****************************************************************************
1634 * Port level functions: *
1635 * 2. MoxaPortEnable(int port); *
1636 * 3. MoxaPortDisable(int port); *
1637 * 4. MoxaPortGetMaxBaud(int port); *
1638 * 6. MoxaPortSetBaud(int port, long baud); *
1639 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1640 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1641 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1642 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1643 * 12. MoxaPortLineStatus(int port); *
1644 * 15. MoxaPortFlushData(int port, int mode); *
1645 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1646 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1647 * 20. MoxaPortTxQueue(int port); *
1648 * 21. MoxaPortTxFree(int port); *
1649 * 22. MoxaPortRxQueue(int port); *
1650 * 24. MoxaPortTxDisable(int port); *
1651 * 25. MoxaPortTxEnable(int port); *
1652 * 27. MoxaPortResetBrkCnt(int port); *
1653 *****************************************************************************/
1655 * Moxa Port Number Description:
1657 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1658 * the port number using in MOXA driver functions will be 0 to 31 for
1659 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1660 * to 127 for fourth. For example, if you setup three MOXA boards,
1661 * first board is C218, second board is C320-16 and third board is
1662 * C320-32. The port number of first board (C218 - 8 ports) is from
1663 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1664 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1665 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1666 * 127 will be invalid.
1669 * Moxa Functions Description:
1671 * Function 1: Driver initialization routine, this routine must be
1672 * called when initialized driver.
1674 * void MoxaDriverInit();
1677 * Function 2: Moxa driver private IOCTL command processing.
1679 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1681 * unsigned int cmd : IOCTL command
1682 * unsigned long arg : IOCTL argument
1683 * int port : port number (0 - 127)
1690 * Function 6: Enable this port to start Tx/Rx data.
1692 * void MoxaPortEnable(int port);
1693 * int port : port number (0 - 127)
1696 * Function 7: Disable this port
1698 * void MoxaPortDisable(int port);
1699 * int port : port number (0 - 127)
1702 * Function 8: Get the maximun available baud rate of this port.
1704 * long MoxaPortGetMaxBaud(int port);
1705 * int port : port number (0 - 127)
1707 * return: 0 : this port is invalid
1708 * 38400/57600/115200 bps
1711 * Function 10: Setting baud rate of this port.
1713 * long MoxaPortSetBaud(int port, long baud);
1714 * int port : port number (0 - 127)
1715 * long baud : baud rate (50 - 115200)
1717 * return: 0 : this port is invalid or baud < 50
1718 * 50 - 115200 : the real baud rate set to the port, if
1719 * the argument baud is large than maximun
1720 * available baud rate, the real setting
1721 * baud rate will be the maximun baud rate.
1724 * Function 12: Configure the port.
1726 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1727 * int port : port number (0 - 127)
1728 * struct ktermios * termio : termio structure pointer
1729 * speed_t baud : baud rate
1731 * return: -1 : this port is invalid or termio == NULL
1735 * Function 13: Get the DTR/RTS state of this port.
1737 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1738 * int port : port number (0 - 127)
1739 * int * dtrState : pointer to INT to receive the current DTR
1740 * state. (if NULL, this function will not
1741 * write to this address)
1742 * int * rtsState : pointer to INT to receive the current RTS
1743 * state. (if NULL, this function will not
1744 * write to this address)
1746 * return: -1 : this port is invalid
1750 * Function 14: Setting the DTR/RTS output state of this port.
1752 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1753 * int port : port number (0 - 127)
1754 * int dtrState : DTR output state (0: off, 1: on)
1755 * int rtsState : RTS output state (0: off, 1: on)
1758 * Function 15: Setting the flow control of this port.
1760 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1761 * int txFlow,int xany);
1762 * int port : port number (0 - 127)
1763 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1764 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1765 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1766 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1767 * int xany : S/W XANY flow control (0: no, 1: yes)
1770 * Function 16: Get ths line status of this port
1772 * int MoxaPortLineStatus(int port);
1773 * int port : port number (0 - 127)
1775 * return: Bit 0 - CTS state (0: off, 1: on)
1776 * Bit 1 - DSR state (0: off, 1: on)
1777 * Bit 2 - DCD state (0: off, 1: on)
1780 * Function 19: Flush the Rx/Tx buffer data of this port.
1782 * void MoxaPortFlushData(int port, int mode);
1783 * int port : port number (0 - 127)
1785 * 0 : flush the Rx buffer
1786 * 1 : flush the Tx buffer
1787 * 2 : flush the Rx and Tx buffer
1790 * Function 20: Write data.
1792 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1793 * int port : port number (0 - 127)
1794 * unsigned char * buffer : pointer to write data buffer.
1795 * int length : write data length
1797 * return: 0 - length : real write data length
1800 * Function 21: Read data.
1802 * int MoxaPortReadData(int port, struct tty_struct *tty);
1803 * int port : port number (0 - 127)
1804 * struct tty_struct *tty : tty for data
1806 * return: 0 - length : real read data length
1809 * Function 24: Get the Tx buffer current queued data bytes
1811 * int MoxaPortTxQueue(int port);
1812 * int port : port number (0 - 127)
1814 * return: .. : Tx buffer current queued data bytes
1817 * Function 25: Get the Tx buffer current free space
1819 * int MoxaPortTxFree(int port);
1820 * int port : port number (0 - 127)
1822 * return: .. : Tx buffer current free space
1825 * Function 26: Get the Rx buffer current queued data bytes
1827 * int MoxaPortRxQueue(int port);
1828 * int port : port number (0 - 127)
1830 * return: .. : Rx buffer current queued data bytes
1833 * Function 28: Disable port data transmission.
1835 * void MoxaPortTxDisable(int port);
1836 * int port : port number (0 - 127)
1839 * Function 29: Enable port data transmission.
1841 * void MoxaPortTxEnable(int port);
1842 * int port : port number (0 - 127)
1845 * Function 31: Get the received BREAK signal count and reset it.
1847 * int MoxaPortResetBrkCnt(int port);
1848 * int port : port number (0 - 127)
1850 * return: 0 - .. : BREAK signal count
1855 static void MoxaPortEnable(struct moxa_port
*port
)
1857 void __iomem
*ofsAddr
;
1858 short lowwater
= 512;
1860 ofsAddr
= port
->tableAddr
;
1861 writew(lowwater
, ofsAddr
+ Low_water
);
1862 if (port
->board
->boardType
== MOXA_BOARD_C320_ISA
||
1863 port
->board
->boardType
== MOXA_BOARD_C320_PCI
) {
1864 moxafunc(ofsAddr
, FC_SetBreakIrq
, 0);
1866 writew(readw(ofsAddr
+ HostStat
) | WakeupBreak
, ofsAddr
+ HostStat
);
1869 moxafunc(ofsAddr
, FC_SetLineIrq
, Magic_code
);
1870 moxafunc(ofsAddr
, FC_FlushQueue
, 2);
1872 moxafunc(ofsAddr
, FC_EnableCH
, Magic_code
);
1873 MoxaPortLineStatus(port
);
1876 static void MoxaPortDisable(struct moxa_port
*port
)
1878 void __iomem
*ofsAddr
= port
->tableAddr
;
1880 moxafunc(ofsAddr
, FC_SetFlowCtl
, 0); /* disable flow control */
1881 moxafunc(ofsAddr
, FC_ClrLineIrq
, Magic_code
);
1882 writew(0, ofsAddr
+ HostStat
);
1883 moxafunc(ofsAddr
, FC_DisableCH
, Magic_code
);
1886 static long MoxaPortGetMaxBaud(struct moxa_port
*port
)
1888 if (port
->board
->boardType
== MOXA_BOARD_C320_ISA
||
1889 port
->board
->boardType
== MOXA_BOARD_C320_PCI
)
1896 static long MoxaPortSetBaud(struct moxa_port
*port
, long baud
)
1898 void __iomem
*ofsAddr
;
1902 if ((baud
< 50L) || ((max
= MoxaPortGetMaxBaud(port
)) == 0))
1904 ofsAddr
= port
->tableAddr
;
1908 clock
= 614400L; /* for 9.8304 Mhz : max. 38400 bps */
1909 else if (max
== 57600L)
1910 clock
= 691200L; /* for 11.0592 Mhz : max. 57600 bps */
1912 clock
= 921600L; /* for 14.7456 Mhz : max. 115200 bps */
1914 moxafunc(ofsAddr
, FC_SetBaud
, val
);
1919 static int MoxaPortSetTermio(struct moxa_port
*port
, struct ktermios
*termio
,
1922 void __iomem
*ofsAddr
;
1926 ofsAddr
= port
->tableAddr
;
1927 cflag
= termio
->c_cflag
; /* termio->c_cflag */
1929 mode
= termio
->c_cflag
& CSIZE
;
1932 else if (mode
== CS6
)
1934 else if (mode
== CS7
)
1936 else if (mode
== CS8
)
1939 if (termio
->c_cflag
& CSTOPB
) {
1947 if (termio
->c_cflag
& PARENB
) {
1948 if (termio
->c_cflag
& PARODD
)
1955 moxafunc(ofsAddr
, FC_SetDataMode
, (ushort
) mode
);
1957 if (port
->board
->boardType
== MOXA_BOARD_C320_ISA
||
1958 port
->board
->boardType
== MOXA_BOARD_C320_PCI
) {
1959 if (baud
>= 921600L)
1962 baud
= MoxaPortSetBaud(port
, baud
);
1964 if (termio
->c_iflag
& (IXON
| IXOFF
| IXANY
)) {
1965 writeb(termio
->c_cc
[VSTART
], ofsAddr
+ FuncArg
);
1966 writeb(termio
->c_cc
[VSTOP
], ofsAddr
+ FuncArg1
);
1967 writeb(FC_SetXonXoff
, ofsAddr
+ FuncCode
);
1968 moxa_wait_finish(ofsAddr
);
1974 static int MoxaPortGetLineOut(struct moxa_port
*port
, int *dtrState
,
1979 *dtrState
= !!(port
->lineCtrl
& DTR_ON
);
1981 *rtsState
= !!(port
->lineCtrl
& RTS_ON
);
1986 static void MoxaPortLineCtrl(struct moxa_port
*port
, int dtr
, int rts
)
1994 port
->lineCtrl
= mode
;
1995 moxafunc(port
->tableAddr
, FC_LineControl
, mode
);
1998 static void MoxaPortFlowCtrl(struct moxa_port
*port
, int rts
, int cts
,
1999 int txflow
, int rxflow
, int txany
)
2004 mode
|= RTS_FlowCtl
;
2006 mode
|= CTS_FlowCtl
;
2013 moxafunc(port
->tableAddr
, FC_SetFlowCtl
, mode
);
2016 static int MoxaPortLineStatus(struct moxa_port
*port
)
2018 void __iomem
*ofsAddr
;
2021 ofsAddr
= port
->tableAddr
;
2022 if (port
->board
->boardType
== MOXA_BOARD_C320_ISA
||
2023 port
->board
->boardType
== MOXA_BOARD_C320_PCI
) {
2024 moxafunc(ofsAddr
, FC_LineStatus
, 0);
2025 val
= readw(ofsAddr
+ FuncArg
);
2027 val
= readw(ofsAddr
+ FlagStat
) >> 4;
2032 moxa_new_dcdstate(port
, val
& 8);
2037 static int MoxaPortWriteData(struct moxa_port
*port
,
2038 const unsigned char *buffer
, int len
)
2040 void __iomem
*baseAddr
, *ofsAddr
, *ofs
;
2041 unsigned int c
, total
;
2042 u16 head
, tail
, tx_mask
, spage
, epage
;
2043 u16 pageno
, pageofs
, bufhead
;
2045 ofsAddr
= port
->tableAddr
;
2046 baseAddr
= port
->board
->basemem
;
2047 tx_mask
= readw(ofsAddr
+ TX_mask
);
2048 spage
= readw(ofsAddr
+ Page_txb
);
2049 epage
= readw(ofsAddr
+ EndPage_txb
);
2050 tail
= readw(ofsAddr
+ TXwptr
);
2051 head
= readw(ofsAddr
+ TXrptr
);
2052 c
= (head
> tail
) ? (head
- tail
- 1) : (head
- tail
+ tx_mask
);
2055 moxaLog
.txcnt
[port
->tty
->index
] += c
;
2057 if (spage
== epage
) {
2058 bufhead
= readw(ofsAddr
+ Ofs_txb
);
2059 writew(spage
, baseAddr
+ Control_reg
);
2062 len
= head
- tail
- 1;
2064 len
= tx_mask
+ 1 - tail
;
2065 len
= (c
> len
) ? len
: c
;
2066 ofs
= baseAddr
+ DynPage_addr
+ bufhead
+ tail
;
2067 memcpy_toio(ofs
, buffer
, len
);
2069 tail
= (tail
+ len
) & tx_mask
;
2073 pageno
= spage
+ (tail
>> 13);
2074 pageofs
= tail
& Page_mask
;
2076 len
= Page_size
- pageofs
;
2079 writeb(pageno
, baseAddr
+ Control_reg
);
2080 ofs
= baseAddr
+ DynPage_addr
+ pageofs
;
2081 memcpy_toio(ofs
, buffer
, len
);
2083 if (++pageno
== epage
)
2088 tail
= (tail
+ total
) & tx_mask
;
2090 writew(tail
, ofsAddr
+ TXwptr
);
2091 writeb(1, ofsAddr
+ CD180TXirq
); /* start to send */
2095 static int MoxaPortReadData(struct moxa_port
*port
)
2097 struct tty_struct
*tty
= port
->tty
;
2099 void __iomem
*baseAddr
, *ofsAddr
, *ofs
;
2100 unsigned int count
, len
, total
;
2101 u16 tail
, rx_mask
, spage
, epage
;
2102 u16 pageno
, pageofs
, bufhead
, head
;
2104 ofsAddr
= port
->tableAddr
;
2105 baseAddr
= port
->board
->basemem
;
2106 head
= readw(ofsAddr
+ RXrptr
);
2107 tail
= readw(ofsAddr
+ RXwptr
);
2108 rx_mask
= readw(ofsAddr
+ RX_mask
);
2109 spage
= readw(ofsAddr
+ Page_rxb
);
2110 epage
= readw(ofsAddr
+ EndPage_rxb
);
2111 count
= (tail
>= head
) ? (tail
- head
) : (tail
- head
+ rx_mask
+ 1);
2116 moxaLog
.rxcnt
[tty
->index
] += total
;
2117 if (spage
== epage
) {
2118 bufhead
= readw(ofsAddr
+ Ofs_rxb
);
2119 writew(spage
, baseAddr
+ Control_reg
);
2121 ofs
= baseAddr
+ DynPage_addr
+ bufhead
+ head
;
2122 len
= (tail
>= head
) ? (tail
- head
) :
2123 (rx_mask
+ 1 - head
);
2124 len
= tty_prepare_flip_string(tty
, &dst
,
2126 memcpy_fromio(dst
, ofs
, len
);
2127 head
= (head
+ len
) & rx_mask
;
2131 pageno
= spage
+ (head
>> 13);
2132 pageofs
= head
& Page_mask
;
2134 writew(pageno
, baseAddr
+ Control_reg
);
2135 ofs
= baseAddr
+ DynPage_addr
+ pageofs
;
2136 len
= tty_prepare_flip_string(tty
, &dst
,
2137 min(Page_size
- pageofs
, count
));
2138 memcpy_fromio(dst
, ofs
, len
);
2141 pageofs
= (pageofs
+ len
) & Page_mask
;
2142 if (pageofs
== 0 && ++pageno
== epage
)
2145 head
= (head
+ total
) & rx_mask
;
2147 writew(head
, ofsAddr
+ RXrptr
);
2148 if (readb(ofsAddr
+ FlagStat
) & Xoff_state
) {
2149 moxaLowWaterChk
= 1;
2150 port
->lowChkFlag
= 1;
2156 static int MoxaPortTxQueue(struct moxa_port
*port
)
2158 void __iomem
*ofsAddr
= port
->tableAddr
;
2159 u16 rptr
, wptr
, mask
;
2161 rptr
= readw(ofsAddr
+ TXrptr
);
2162 wptr
= readw(ofsAddr
+ TXwptr
);
2163 mask
= readw(ofsAddr
+ TX_mask
);
2164 return (wptr
- rptr
) & mask
;
2167 static int MoxaPortTxFree(struct moxa_port
*port
)
2169 void __iomem
*ofsAddr
= port
->tableAddr
;
2170 u16 rptr
, wptr
, mask
;
2172 rptr
= readw(ofsAddr
+ TXrptr
);
2173 wptr
= readw(ofsAddr
+ TXwptr
);
2174 mask
= readw(ofsAddr
+ TX_mask
);
2175 return mask
- ((wptr
- rptr
) & mask
);
2178 static int MoxaPortRxQueue(struct moxa_port
*port
)
2180 void __iomem
*ofsAddr
= port
->tableAddr
;
2181 u16 rptr
, wptr
, mask
;
2183 rptr
= readw(ofsAddr
+ RXrptr
);
2184 wptr
= readw(ofsAddr
+ RXwptr
);
2185 mask
= readw(ofsAddr
+ RX_mask
);
2186 return (wptr
- rptr
) & mask
;
2189 static void MoxaPortTxDisable(struct moxa_port
*port
)
2191 moxafunc(port
->tableAddr
, FC_SetXoffState
, Magic_code
);
2194 static void MoxaPortTxEnable(struct moxa_port
*port
)
2196 moxafunc(port
->tableAddr
, FC_SetXonState
, Magic_code
);
2199 static int moxa_get_serial_info(struct moxa_port
*info
,
2200 struct serial_struct __user
*retinfo
)
2202 struct serial_struct tmp
;
2204 memset(&tmp
, 0, sizeof(tmp
));
2205 tmp
.type
= info
->type
;
2206 tmp
.line
= info
->tty
->index
;
2209 tmp
.flags
= info
->asyncflags
;
2210 tmp
.baud_base
= 921600;
2211 tmp
.close_delay
= info
->close_delay
;
2212 tmp
.custom_divisor
= 0;
2214 if(copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
2220 static int moxa_set_serial_info(struct moxa_port
*info
,
2221 struct serial_struct __user
*new_info
)
2223 struct serial_struct new_serial
;
2225 if(copy_from_user(&new_serial
, new_info
, sizeof(new_serial
)))
2228 if ((new_serial
.irq
!= 0) ||
2229 (new_serial
.port
!= 0) ||
2230 // (new_serial.type != info->type) ||
2231 (new_serial
.custom_divisor
!= 0) ||
2232 (new_serial
.baud_base
!= 921600))
2235 if (!capable(CAP_SYS_ADMIN
)) {
2236 if (((new_serial
.flags
& ~ASYNC_USR_MASK
) !=
2237 (info
->asyncflags
& ~ASYNC_USR_MASK
)))
2240 info
->close_delay
= new_serial
.close_delay
* HZ
/ 100;
2243 new_serial
.flags
= (new_serial
.flags
& ~ASYNC_FLAGS
);
2244 new_serial
.flags
|= (info
->asyncflags
& ASYNC_FLAGS
);
2246 if (new_serial
.type
== PORT_16550A
) {
2247 MoxaSetFifo(info
, 1);
2249 MoxaSetFifo(info
, 0);
2252 info
->type
= new_serial
.type
;
2258 /*****************************************************************************
2259 * Static local functions: *
2260 *****************************************************************************/
2262 static void MoxaSetFifo(struct moxa_port
*port
, int enable
)
2264 void __iomem
*ofsAddr
= port
->tableAddr
;
2267 moxafunc(ofsAddr
, FC_SetRxFIFOTrig
, 0);
2268 moxafunc(ofsAddr
, FC_SetTxFIFOCnt
, 1);
2270 moxafunc(ofsAddr
, FC_SetRxFIFOTrig
, 3);
2271 moxafunc(ofsAddr
, FC_SetTxFIFOCnt
, 16);