1 /*****************************************************************************/
3 * moxa.c -- MOXA Intellio family multiport serial driver.
5 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com).
6 * Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
8 * This code is loosely based on the Linux serial driver, written by
9 * Linus Torvalds, Theodore T'so and others.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
18 * MOXA Intellio Series Driver
24 #include <linux/module.h>
25 #include <linux/types.h>
27 #include <linux/ioport.h>
28 #include <linux/errno.h>
29 #include <linux/firmware.h>
30 #include <linux/signal.h>
31 #include <linux/sched.h>
32 #include <linux/timer.h>
33 #include <linux/interrupt.h>
34 #include <linux/tty.h>
35 #include <linux/tty_flip.h>
36 #include <linux/major.h>
37 #include <linux/string.h>
38 #include <linux/fcntl.h>
39 #include <linux/ptrace.h>
40 #include <linux/serial.h>
41 #include <linux/tty_driver.h>
42 #include <linux/delay.h>
43 #include <linux/pci.h>
44 #include <linux/init.h>
45 #include <linux/bitops.h>
47 #include <asm/system.h>
49 #include <asm/uaccess.h>
53 #define MOXA_VERSION "6.0k"
55 #define MOXA_FW_HDRLEN 32
59 #define MAX_BOARDS 4 /* Don't change this value */
60 #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
61 #define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
63 #define MOXA_IS_320(brd) ((brd)->boardType == MOXA_BOARD_C320_ISA || \
64 (brd)->boardType == MOXA_BOARD_C320_PCI)
67 * Define the Moxa PCI vendor and device IDs.
69 #define MOXA_BUS_TYPE_ISA 0
70 #define MOXA_BUS_TYPE_PCI 1
73 MOXA_BOARD_C218_PCI
= 1,
80 static char *moxa_brdname
[] =
82 "C218 Turbo PCI series",
83 "C218 Turbo ISA series",
84 "C320 Turbo PCI series",
85 "C320 Turbo ISA series",
90 static struct pci_device_id moxa_pcibrds
[] = {
91 { PCI_DEVICE(PCI_VENDOR_ID_MOXA
, PCI_DEVICE_ID_MOXA_C218
),
92 .driver_data
= MOXA_BOARD_C218_PCI
},
93 { PCI_DEVICE(PCI_VENDOR_ID_MOXA
, PCI_DEVICE_ID_MOXA_C320
),
94 .driver_data
= MOXA_BOARD_C320_PCI
},
95 { PCI_DEVICE(PCI_VENDOR_ID_MOXA
, PCI_DEVICE_ID_MOXA_CP204J
),
96 .driver_data
= MOXA_BOARD_CP204J
},
99 MODULE_DEVICE_TABLE(pci
, moxa_pcibrds
);
100 #endif /* CONFIG_PCI */
104 static struct moxa_board_conf
{
111 struct moxa_port
*ports
;
113 void __iomem
*basemem
;
114 void __iomem
*intNdx
;
115 void __iomem
*intPend
;
116 void __iomem
*intTable
;
117 } moxa_boards
[MAX_BOARDS
];
119 struct mxser_mstatus
{
133 struct tty_port port
;
134 struct moxa_board_conf
*board
;
135 void __iomem
*tableAddr
;
139 unsigned long statusflags
;
148 int rxcnt
[MAX_PORTS
];
149 int txcnt
[MAX_PORTS
];
153 #define TXSTOPPED 0x1
155 #define EMPTYWAIT 0x4
158 #define SERIAL_DO_RESTART
160 #define WAKEUP_CHARS 256
162 static int ttymajor
= MOXAMAJOR
;
163 static struct mon_str moxaLog
;
164 static unsigned int moxaFuncTout
= HZ
/ 2;
165 static unsigned int moxaLowWaterChk
;
166 static DEFINE_MUTEX(moxa_openlock
);
167 /* Variables for insmod */
169 static unsigned long baseaddr
[MAX_BOARDS
];
170 static unsigned int type
[MAX_BOARDS
];
171 static unsigned int numports
[MAX_BOARDS
];
174 MODULE_AUTHOR("William Chen");
175 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
176 MODULE_LICENSE("GPL");
178 module_param_array(type
, uint
, NULL
, 0);
179 MODULE_PARM_DESC(type
, "card type: C218=2, C320=4");
180 module_param_array(baseaddr
, ulong
, NULL
, 0);
181 MODULE_PARM_DESC(baseaddr
, "base address");
182 module_param_array(numports
, uint
, NULL
, 0);
183 MODULE_PARM_DESC(numports
, "numports (ignored for C218)");
185 module_param(ttymajor
, int, 0);
190 static int moxa_open(struct tty_struct
*, struct file
*);
191 static void moxa_close(struct tty_struct
*, struct file
*);
192 static int moxa_write(struct tty_struct
*, const unsigned char *, int);
193 static int moxa_write_room(struct tty_struct
*);
194 static void moxa_flush_buffer(struct tty_struct
*);
195 static int moxa_chars_in_buffer(struct tty_struct
*);
196 static void moxa_throttle(struct tty_struct
*);
197 static void moxa_unthrottle(struct tty_struct
*);
198 static void moxa_set_termios(struct tty_struct
*, struct ktermios
*);
199 static void moxa_stop(struct tty_struct
*);
200 static void moxa_start(struct tty_struct
*);
201 static void moxa_hangup(struct tty_struct
*);
202 static int moxa_tiocmget(struct tty_struct
*tty
, struct file
*file
);
203 static int moxa_tiocmset(struct tty_struct
*tty
, struct file
*file
,
204 unsigned int set
, unsigned int clear
);
205 static void moxa_poll(unsigned long);
206 static void moxa_set_tty_param(struct tty_struct
*, struct ktermios
*);
207 static void moxa_setup_empty_event(struct tty_struct
*);
208 static void moxa_shut_down(struct tty_struct
*);
210 * moxa board interface functions:
212 static void MoxaPortEnable(struct moxa_port
*);
213 static void MoxaPortDisable(struct moxa_port
*);
214 static int MoxaPortSetTermio(struct moxa_port
*, struct ktermios
*, speed_t
);
215 static int MoxaPortGetLineOut(struct moxa_port
*, int *, int *);
216 static void MoxaPortLineCtrl(struct moxa_port
*, int, int);
217 static void MoxaPortFlowCtrl(struct moxa_port
*, int, int, int, int, int);
218 static int MoxaPortLineStatus(struct moxa_port
*);
219 static void MoxaPortFlushData(struct moxa_port
*, int);
220 static int MoxaPortWriteData(struct tty_struct
*, const unsigned char *, int);
221 static int MoxaPortReadData(struct moxa_port
*);
222 static int MoxaPortTxQueue(struct moxa_port
*);
223 static int MoxaPortRxQueue(struct moxa_port
*);
224 static int MoxaPortTxFree(struct moxa_port
*);
225 static void MoxaPortTxDisable(struct moxa_port
*);
226 static void MoxaPortTxEnable(struct moxa_port
*);
227 static int moxa_get_serial_info(struct moxa_port
*, struct serial_struct __user
*);
228 static int moxa_set_serial_info(struct moxa_port
*, struct serial_struct __user
*);
229 static void MoxaSetFifo(struct moxa_port
*port
, int enable
);
235 static void moxa_wait_finish(void __iomem
*ofsAddr
)
237 unsigned long end
= jiffies
+ moxaFuncTout
;
239 while (readw(ofsAddr
+ FuncCode
) != 0)
240 if (time_after(jiffies
, end
))
242 if (readw(ofsAddr
+ FuncCode
) != 0 && printk_ratelimit())
243 printk(KERN_WARNING
"moxa function expired\n");
246 static void moxafunc(void __iomem
*ofsAddr
, u16 cmd
, u16 arg
)
248 writew(arg
, ofsAddr
+ FuncArg
);
249 writew(cmd
, ofsAddr
+ FuncCode
);
250 moxa_wait_finish(ofsAddr
);
253 static void moxa_low_water_check(void __iomem
*ofsAddr
)
255 u16 rptr
, wptr
, mask
, len
;
257 if (readb(ofsAddr
+ FlagStat
) & Xoff_state
) {
258 rptr
= readw(ofsAddr
+ RXrptr
);
259 wptr
= readw(ofsAddr
+ RXwptr
);
260 mask
= readw(ofsAddr
+ RX_mask
);
261 len
= (wptr
- rptr
) & mask
;
262 if (len
<= Low_water
)
263 moxafunc(ofsAddr
, FC_SendXon
, 0);
271 static int moxa_ioctl(struct tty_struct
*tty
, struct file
*file
,
272 unsigned int cmd
, unsigned long arg
)
274 struct moxa_port
*ch
= tty
->driver_data
;
275 void __user
*argp
= (void __user
*)arg
;
278 if (tty
->index
== MAX_PORTS
) {
279 if (cmd
!= MOXA_GETDATACOUNT
&& cmd
!= MOXA_GET_IOQUEUE
&&
280 cmd
!= MOXA_GETMSTATUS
)
286 case MOXA_GETDATACOUNT
:
287 moxaLog
.tick
= jiffies
;
288 if (copy_to_user(argp
, &moxaLog
, sizeof(moxaLog
)))
291 case MOXA_FLUSH_QUEUE
:
292 MoxaPortFlushData(ch
, arg
);
294 case MOXA_GET_IOQUEUE
: {
295 struct moxaq_str __user
*argm
= argp
;
296 struct moxaq_str tmp
;
300 mutex_lock(&moxa_openlock
);
301 for (i
= 0; i
< MAX_BOARDS
; i
++) {
302 p
= moxa_boards
[i
].ports
;
303 for (j
= 0; j
< MAX_PORTS_PER_BOARD
; j
++, p
++, argm
++) {
304 memset(&tmp
, 0, sizeof(tmp
));
305 if (moxa_boards
[i
].ready
) {
306 tmp
.inq
= MoxaPortRxQueue(p
);
307 tmp
.outq
= MoxaPortTxQueue(p
);
309 if (copy_to_user(argm
, &tmp
, sizeof(tmp
))) {
310 mutex_unlock(&moxa_openlock
);
315 mutex_unlock(&moxa_openlock
);
317 } case MOXA_GET_OQUEUE
:
318 status
= MoxaPortTxQueue(ch
);
319 ret
= put_user(status
, (unsigned long __user
*)argp
);
321 case MOXA_GET_IQUEUE
:
322 status
= MoxaPortRxQueue(ch
);
323 ret
= put_user(status
, (unsigned long __user
*)argp
);
325 case MOXA_GETMSTATUS
: {
326 struct mxser_mstatus __user
*argm
= argp
;
327 struct mxser_mstatus tmp
;
331 mutex_lock(&moxa_openlock
);
332 for (i
= 0; i
< MAX_BOARDS
; i
++) {
333 p
= moxa_boards
[i
].ports
;
334 for (j
= 0; j
< MAX_PORTS_PER_BOARD
; j
++, p
++, argm
++) {
335 struct tty_struct
*ttyp
;
336 memset(&tmp
, 0, sizeof(tmp
));
337 if (!moxa_boards
[i
].ready
)
340 status
= MoxaPortLineStatus(p
);
348 ttyp
= tty_port_tty_get(&p
->port
);
349 if (!ttyp
|| !ttyp
->termios
)
350 tmp
.cflag
= p
->cflag
;
352 tmp
.cflag
= ttyp
->termios
->c_cflag
;
355 if (copy_to_user(argm
, &tmp
, sizeof(tmp
))) {
356 mutex_unlock(&moxa_openlock
);
361 mutex_unlock(&moxa_openlock
);
365 mutex_lock(&moxa_openlock
);
366 ret
= moxa_get_serial_info(ch
, argp
);
367 mutex_unlock(&moxa_openlock
);
370 mutex_lock(&moxa_openlock
);
371 ret
= moxa_set_serial_info(ch
, argp
);
372 mutex_unlock(&moxa_openlock
);
380 static int moxa_break_ctl(struct tty_struct
*tty
, int state
)
382 struct moxa_port
*port
= tty
->driver_data
;
384 moxafunc(port
->tableAddr
, state
? FC_SendBreak
: FC_StopBreak
,
389 static const struct tty_operations moxa_ops
= {
393 .write_room
= moxa_write_room
,
394 .flush_buffer
= moxa_flush_buffer
,
395 .chars_in_buffer
= moxa_chars_in_buffer
,
397 .throttle
= moxa_throttle
,
398 .unthrottle
= moxa_unthrottle
,
399 .set_termios
= moxa_set_termios
,
402 .hangup
= moxa_hangup
,
403 .break_ctl
= moxa_break_ctl
,
404 .tiocmget
= moxa_tiocmget
,
405 .tiocmset
= moxa_tiocmset
,
408 static struct tty_driver
*moxaDriver
;
409 static DEFINE_TIMER(moxaTimer
, moxa_poll
, 0, 0);
410 static DEFINE_SPINLOCK(moxa_lock
);
416 static int moxa_check_fw_model(struct moxa_board_conf
*brd
, u8 model
)
418 switch (brd
->boardType
) {
419 case MOXA_BOARD_C218_ISA
:
420 case MOXA_BOARD_C218_PCI
:
424 case MOXA_BOARD_CP204J
:
438 static int moxa_check_fw(const void *ptr
)
440 const __le16
*lptr
= ptr
;
442 if (*lptr
!= cpu_to_le16(0x7980))
448 static int moxa_load_bios(struct moxa_board_conf
*brd
, const u8
*buf
,
451 void __iomem
*baseAddr
= brd
->basemem
;
454 writeb(HW_reset
, baseAddr
+ Control_reg
); /* reset */
456 memset_io(baseAddr
, 0, 4096);
457 memcpy_toio(baseAddr
, buf
, len
); /* download BIOS */
458 writeb(0, baseAddr
+ Control_reg
); /* restart */
462 switch (brd
->boardType
) {
463 case MOXA_BOARD_C218_ISA
:
464 case MOXA_BOARD_C218_PCI
:
465 tmp
= readw(baseAddr
+ C218_key
);
466 if (tmp
!= C218_KeyCode
)
469 case MOXA_BOARD_CP204J
:
470 tmp
= readw(baseAddr
+ C218_key
);
471 if (tmp
!= CP204J_KeyCode
)
475 tmp
= readw(baseAddr
+ C320_key
);
476 if (tmp
!= C320_KeyCode
)
478 tmp
= readw(baseAddr
+ C320_status
);
479 if (tmp
!= STS_init
) {
480 printk(KERN_ERR
"MOXA: bios upload failed -- CPU/Basic "
481 "module not found\n");
489 printk(KERN_ERR
"MOXA: bios upload failed -- board not found\n");
493 static int moxa_load_320b(struct moxa_board_conf
*brd
, const u8
*ptr
,
496 void __iomem
*baseAddr
= brd
->basemem
;
499 printk(KERN_ERR
"MOXA: invalid 320 bios -- too short\n");
503 writew(len
- 7168 - 2, baseAddr
+ C320bapi_len
);
504 writeb(1, baseAddr
+ Control_reg
); /* Select Page 1 */
505 memcpy_toio(baseAddr
+ DynPage_addr
, ptr
, 7168);
506 writeb(2, baseAddr
+ Control_reg
); /* Select Page 2 */
507 memcpy_toio(baseAddr
+ DynPage_addr
, ptr
+ 7168, len
- 7168);
512 static int moxa_real_load_code(struct moxa_board_conf
*brd
, const void *ptr
,
515 void __iomem
*baseAddr
= brd
->basemem
;
516 const __le16
*uptr
= ptr
;
517 size_t wlen
, len2
, j
;
518 unsigned long key
, loadbuf
, loadlen
, checksum
, checksum_ok
;
519 unsigned int i
, retry
;
522 keycode
= (brd
->boardType
== MOXA_BOARD_CP204J
) ? CP204J_KeyCode
:
525 switch (brd
->boardType
) {
526 case MOXA_BOARD_CP204J
:
527 case MOXA_BOARD_C218_ISA
:
528 case MOXA_BOARD_C218_PCI
:
530 loadbuf
= C218_LoadBuf
;
531 loadlen
= C218DLoad_len
;
532 checksum
= C218check_sum
;
533 checksum_ok
= C218chksum_ok
;
537 keycode
= C320_KeyCode
;
538 loadbuf
= C320_LoadBuf
;
539 loadlen
= C320DLoad_len
;
540 checksum
= C320check_sum
;
541 checksum_ok
= C320chksum_ok
;
547 for (i
= 0; i
< wlen
; i
++)
548 usum
+= le16_to_cpu(uptr
[i
]);
554 len2
= (wlen
> 2048) ? 2048 : wlen
;
556 memcpy_toio(baseAddr
+ loadbuf
, ptr
+ j
, len2
<< 1);
559 writew(len2
, baseAddr
+ loadlen
);
560 writew(0, baseAddr
+ key
);
561 for (i
= 0; i
< 100; i
++) {
562 if (readw(baseAddr
+ key
) == keycode
)
566 if (readw(baseAddr
+ key
) != keycode
)
569 writew(0, baseAddr
+ loadlen
);
570 writew(usum
, baseAddr
+ checksum
);
571 writew(0, baseAddr
+ key
);
572 for (i
= 0; i
< 100; i
++) {
573 if (readw(baseAddr
+ key
) == keycode
)
578 } while ((readb(baseAddr
+ checksum_ok
) != 1) && (retry
< 3));
579 if (readb(baseAddr
+ checksum_ok
) != 1)
582 writew(0, baseAddr
+ key
);
583 for (i
= 0; i
< 600; i
++) {
584 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
588 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
591 if (MOXA_IS_320(brd
)) {
592 if (brd
->busType
== MOXA_BUS_TYPE_PCI
) { /* ASIC board */
593 writew(0x3800, baseAddr
+ TMS320_PORT1
);
594 writew(0x3900, baseAddr
+ TMS320_PORT2
);
595 writew(28499, baseAddr
+ TMS320_CLOCK
);
597 writew(0x3200, baseAddr
+ TMS320_PORT1
);
598 writew(0x3400, baseAddr
+ TMS320_PORT2
);
599 writew(19999, baseAddr
+ TMS320_CLOCK
);
602 writew(1, baseAddr
+ Disable_IRQ
);
603 writew(0, baseAddr
+ Magic_no
);
604 for (i
= 0; i
< 500; i
++) {
605 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
609 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
612 if (MOXA_IS_320(brd
)) {
613 j
= readw(baseAddr
+ Module_cnt
);
616 brd
->numPorts
= j
* 8;
617 writew(j
, baseAddr
+ Module_no
);
618 writew(0, baseAddr
+ Magic_no
);
619 for (i
= 0; i
< 600; i
++) {
620 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
624 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
627 brd
->intNdx
= baseAddr
+ IRQindex
;
628 brd
->intPend
= baseAddr
+ IRQpending
;
629 brd
->intTable
= baseAddr
+ IRQtable
;
634 static int moxa_load_code(struct moxa_board_conf
*brd
, const void *ptr
,
637 void __iomem
*ofsAddr
, *baseAddr
= brd
->basemem
;
638 struct moxa_port
*port
;
642 printk(KERN_ERR
"MOXA: bios length is not even\n");
646 retval
= moxa_real_load_code(brd
, ptr
, len
); /* may change numPorts */
650 switch (brd
->boardType
) {
651 case MOXA_BOARD_C218_ISA
:
652 case MOXA_BOARD_C218_PCI
:
653 case MOXA_BOARD_CP204J
:
655 for (i
= 0; i
< brd
->numPorts
; i
++, port
++) {
658 port
->tableAddr
= baseAddr
+ Extern_table
+
660 ofsAddr
= port
->tableAddr
;
661 writew(C218rx_mask
, ofsAddr
+ RX_mask
);
662 writew(C218tx_mask
, ofsAddr
+ TX_mask
);
663 writew(C218rx_spage
+ i
* C218buf_pageno
, ofsAddr
+ Page_rxb
);
664 writew(readw(ofsAddr
+ Page_rxb
) + C218rx_pageno
, ofsAddr
+ EndPage_rxb
);
666 writew(C218tx_spage
+ i
* C218buf_pageno
, ofsAddr
+ Page_txb
);
667 writew(readw(ofsAddr
+ Page_txb
) + C218tx_pageno
, ofsAddr
+ EndPage_txb
);
673 for (i
= 0; i
< brd
->numPorts
; i
++, port
++) {
676 port
->tableAddr
= baseAddr
+ Extern_table
+
678 ofsAddr
= port
->tableAddr
;
679 switch (brd
->numPorts
) {
681 writew(C320p8rx_mask
, ofsAddr
+ RX_mask
);
682 writew(C320p8tx_mask
, ofsAddr
+ TX_mask
);
683 writew(C320p8rx_spage
+ i
* C320p8buf_pgno
, ofsAddr
+ Page_rxb
);
684 writew(readw(ofsAddr
+ Page_rxb
) + C320p8rx_pgno
, ofsAddr
+ EndPage_rxb
);
685 writew(C320p8tx_spage
+ i
* C320p8buf_pgno
, ofsAddr
+ Page_txb
);
686 writew(readw(ofsAddr
+ Page_txb
) + C320p8tx_pgno
, ofsAddr
+ EndPage_txb
);
690 writew(C320p16rx_mask
, ofsAddr
+ RX_mask
);
691 writew(C320p16tx_mask
, ofsAddr
+ TX_mask
);
692 writew(C320p16rx_spage
+ i
* C320p16buf_pgno
, ofsAddr
+ Page_rxb
);
693 writew(readw(ofsAddr
+ Page_rxb
) + C320p16rx_pgno
, ofsAddr
+ EndPage_rxb
);
694 writew(C320p16tx_spage
+ i
* C320p16buf_pgno
, ofsAddr
+ Page_txb
);
695 writew(readw(ofsAddr
+ Page_txb
) + C320p16tx_pgno
, ofsAddr
+ EndPage_txb
);
699 writew(C320p24rx_mask
, ofsAddr
+ RX_mask
);
700 writew(C320p24tx_mask
, ofsAddr
+ TX_mask
);
701 writew(C320p24rx_spage
+ i
* C320p24buf_pgno
, ofsAddr
+ Page_rxb
);
702 writew(readw(ofsAddr
+ Page_rxb
) + C320p24rx_pgno
, ofsAddr
+ EndPage_rxb
);
703 writew(C320p24tx_spage
+ i
* C320p24buf_pgno
, ofsAddr
+ Page_txb
);
704 writew(readw(ofsAddr
+ Page_txb
), ofsAddr
+ EndPage_txb
);
707 writew(C320p32rx_mask
, ofsAddr
+ RX_mask
);
708 writew(C320p32tx_mask
, ofsAddr
+ TX_mask
);
709 writew(C320p32tx_ofs
, ofsAddr
+ Ofs_txb
);
710 writew(C320p32rx_spage
+ i
* C320p32buf_pgno
, ofsAddr
+ Page_rxb
);
711 writew(readb(ofsAddr
+ Page_rxb
), ofsAddr
+ EndPage_rxb
);
712 writew(C320p32tx_spage
+ i
* C320p32buf_pgno
, ofsAddr
+ Page_txb
);
713 writew(readw(ofsAddr
+ Page_txb
), ofsAddr
+ EndPage_txb
);
722 static int moxa_load_fw(struct moxa_board_conf
*brd
, const struct firmware
*fw
)
724 const void *ptr
= fw
->data
;
728 unsigned int a
, lenp
, lencnt
;
731 __le32 magic
; /* 0x34303430 */
733 u8 type
; /* UNIX = 3 */
734 u8 model
; /* C218T=1, C320T=2, CP204=3 */
739 BUILD_BUG_ON(ARRAY_SIZE(hdr
->len
) != ARRAY_SIZE(lens
));
741 if (fw
->size
< MOXA_FW_HDRLEN
) {
742 strcpy(rsn
, "too short (even header won't fit)");
745 if (hdr
->magic
!= cpu_to_le32(0x30343034)) {
746 sprintf(rsn
, "bad magic: %.8x", le32_to_cpu(hdr
->magic
));
749 if (hdr
->type
!= 3) {
750 sprintf(rsn
, "not for linux, type is %u", hdr
->type
);
753 if (moxa_check_fw_model(brd
, hdr
->model
)) {
754 sprintf(rsn
, "not for this card, model is %u", hdr
->model
);
758 len
= MOXA_FW_HDRLEN
;
759 lencnt
= hdr
->model
== 2 ? 5 : 3;
760 for (a
= 0; a
< ARRAY_SIZE(lens
); a
++) {
761 lens
[a
] = le16_to_cpu(hdr
->len
[a
]);
762 if (lens
[a
] && len
+ lens
[a
] <= fw
->size
&&
763 moxa_check_fw(&fw
->data
[len
]))
764 printk(KERN_WARNING
"MOXA firmware: unexpected input "
765 "at offset %u, but going on\n", (u32
)len
);
766 if (!lens
[a
] && a
< lencnt
) {
767 sprintf(rsn
, "too few entries in fw file");
773 if (len
!= fw
->size
) {
774 sprintf(rsn
, "bad length: %u (should be %u)", (u32
)fw
->size
,
779 ptr
+= MOXA_FW_HDRLEN
;
782 strcpy(rsn
, "read above");
784 ret
= moxa_load_bios(brd
, ptr
, lens
[lenp
]);
788 /* we skip the tty section (lens[1]), since we don't need it */
789 ptr
+= lens
[lenp
] + lens
[lenp
+ 1];
790 lenp
+= 2; /* comm */
792 if (hdr
->model
== 2) {
793 ret
= moxa_load_320b(brd
, ptr
, lens
[lenp
]);
796 /* skip another tty */
797 ptr
+= lens
[lenp
] + lens
[lenp
+ 1];
801 ret
= moxa_load_code(brd
, ptr
, lens
[lenp
]);
807 printk(KERN_ERR
"firmware failed to load, reason: %s\n", rsn
);
811 static int moxa_init_board(struct moxa_board_conf
*brd
, struct device
*dev
)
813 const struct firmware
*fw
;
819 brd
->ports
= kcalloc(MAX_PORTS_PER_BOARD
, sizeof(*brd
->ports
),
821 if (brd
->ports
== NULL
) {
822 printk(KERN_ERR
"cannot allocate memory for ports\n");
827 for (i
= 0, p
= brd
->ports
; i
< MAX_PORTS_PER_BOARD
; i
++, p
++) {
828 tty_port_init(&p
->port
);
829 p
->type
= PORT_16550A
;
830 p
->cflag
= B9600
| CS8
| CREAD
| CLOCAL
| HUPCL
;
833 switch (brd
->boardType
) {
834 case MOXA_BOARD_C218_ISA
:
835 case MOXA_BOARD_C218_PCI
:
836 file
= "c218tunx.cod";
838 case MOXA_BOARD_CP204J
:
839 file
= "cp204unx.cod";
842 file
= "c320tunx.cod";
846 ret
= request_firmware(&fw
, file
, dev
);
848 printk(KERN_ERR
"MOXA: request_firmware failed. Make sure "
849 "you've placed '%s' file into your firmware "
850 "loader directory (e.g. /lib/firmware)\n",
855 ret
= moxa_load_fw(brd
, fw
);
857 release_firmware(fw
);
862 spin_lock_bh(&moxa_lock
);
864 if (!timer_pending(&moxaTimer
))
865 mod_timer(&moxaTimer
, jiffies
+ HZ
/ 50);
866 spin_unlock_bh(&moxa_lock
);
875 static void moxa_board_deinit(struct moxa_board_conf
*brd
)
877 unsigned int a
, opened
;
879 mutex_lock(&moxa_openlock
);
880 spin_lock_bh(&moxa_lock
);
882 spin_unlock_bh(&moxa_lock
);
884 /* pci hot-un-plug support */
885 for (a
= 0; a
< brd
->numPorts
; a
++)
886 if (brd
->ports
[a
].port
.flags
& ASYNC_INITIALIZED
) {
887 struct tty_struct
*tty
= tty_port_tty_get(
888 &brd
->ports
[a
].port
);
896 for (a
= 0; a
< brd
->numPorts
; a
++)
897 if (brd
->ports
[a
].port
.flags
& ASYNC_INITIALIZED
)
899 mutex_unlock(&moxa_openlock
);
903 mutex_lock(&moxa_openlock
);
906 iounmap(brd
->basemem
);
912 static int __devinit
moxa_pci_probe(struct pci_dev
*pdev
,
913 const struct pci_device_id
*ent
)
915 struct moxa_board_conf
*board
;
917 int board_type
= ent
->driver_data
;
920 retval
= pci_enable_device(pdev
);
922 dev_err(&pdev
->dev
, "can't enable pci device\n");
926 for (i
= 0; i
< MAX_BOARDS
; i
++)
927 if (moxa_boards
[i
].basemem
== NULL
)
931 if (i
>= MAX_BOARDS
) {
932 dev_warn(&pdev
->dev
, "more than %u MOXA Intellio family boards "
933 "found. Board is ignored.\n", MAX_BOARDS
);
937 board
= &moxa_boards
[i
];
939 retval
= pci_request_region(pdev
, 2, "moxa-base");
941 dev_err(&pdev
->dev
, "can't request pci region 2\n");
945 board
->basemem
= ioremap_nocache(pci_resource_start(pdev
, 2), 0x4000);
946 if (board
->basemem
== NULL
) {
947 dev_err(&pdev
->dev
, "can't remap io space 2\n");
951 board
->boardType
= board_type
;
952 switch (board_type
) {
953 case MOXA_BOARD_C218_ISA
:
954 case MOXA_BOARD_C218_PCI
:
958 case MOXA_BOARD_CP204J
:
965 board
->busType
= MOXA_BUS_TYPE_PCI
;
967 retval
= moxa_init_board(board
, &pdev
->dev
);
971 pci_set_drvdata(pdev
, board
);
973 dev_info(&pdev
->dev
, "board '%s' ready (%u ports, firmware loaded)\n",
974 moxa_brdname
[board_type
- 1], board
->numPorts
);
978 iounmap(board
->basemem
);
979 board
->basemem
= NULL
;
981 pci_release_region(pdev
, 2);
986 static void __devexit
moxa_pci_remove(struct pci_dev
*pdev
)
988 struct moxa_board_conf
*brd
= pci_get_drvdata(pdev
);
990 moxa_board_deinit(brd
);
992 pci_release_region(pdev
, 2);
995 static struct pci_driver moxa_pci_driver
= {
997 .id_table
= moxa_pcibrds
,
998 .probe
= moxa_pci_probe
,
999 .remove
= __devexit_p(moxa_pci_remove
)
1001 #endif /* CONFIG_PCI */
1003 static int __init
moxa_init(void)
1005 unsigned int isabrds
= 0;
1008 printk(KERN_INFO
"MOXA Intellio family driver version %s\n",
1010 moxaDriver
= alloc_tty_driver(MAX_PORTS
+ 1);
1014 moxaDriver
->owner
= THIS_MODULE
;
1015 moxaDriver
->name
= "ttyMX";
1016 moxaDriver
->major
= ttymajor
;
1017 moxaDriver
->minor_start
= 0;
1018 moxaDriver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1019 moxaDriver
->subtype
= SERIAL_TYPE_NORMAL
;
1020 moxaDriver
->init_termios
= tty_std_termios
;
1021 moxaDriver
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| CLOCAL
| HUPCL
;
1022 moxaDriver
->init_termios
.c_ispeed
= 9600;
1023 moxaDriver
->init_termios
.c_ospeed
= 9600;
1024 moxaDriver
->flags
= TTY_DRIVER_REAL_RAW
;
1025 tty_set_operations(moxaDriver
, &moxa_ops
);
1027 if (tty_register_driver(moxaDriver
)) {
1028 printk(KERN_ERR
"can't register MOXA Smartio tty driver!\n");
1029 put_tty_driver(moxaDriver
);
1033 /* Find the boards defined from module args. */
1036 struct moxa_board_conf
*brd
= moxa_boards
;
1038 for (i
= 0; i
< MAX_BOARDS
; i
++) {
1041 if (type
[i
] == MOXA_BOARD_C218_ISA
||
1042 type
[i
] == MOXA_BOARD_C320_ISA
) {
1043 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
1044 isabrds
+ 1, moxa_brdname
[type
[i
] - 1],
1046 brd
->boardType
= type
[i
];
1047 brd
->numPorts
= type
[i
] == MOXA_BOARD_C218_ISA
? 8 :
1049 brd
->busType
= MOXA_BUS_TYPE_ISA
;
1050 brd
->basemem
= ioremap_nocache(baseaddr
[i
], 0x4000);
1051 if (!brd
->basemem
) {
1052 printk(KERN_ERR
"MOXA: can't remap %lx\n",
1056 if (moxa_init_board(brd
, NULL
)) {
1057 iounmap(brd
->basemem
);
1058 brd
->basemem
= NULL
;
1062 printk(KERN_INFO
"MOXA isa board found at 0x%.8lu and "
1063 "ready (%u ports, firmware loaded)\n",
1064 baseaddr
[i
], brd
->numPorts
);
1074 retval
= pci_register_driver(&moxa_pci_driver
);
1076 printk(KERN_ERR
"Can't register MOXA pci driver!\n");
1085 static void __exit
moxa_exit(void)
1090 pci_unregister_driver(&moxa_pci_driver
);
1093 for (i
= 0; i
< MAX_BOARDS
; i
++) /* ISA boards */
1094 if (moxa_boards
[i
].ready
)
1095 moxa_board_deinit(&moxa_boards
[i
]);
1097 del_timer_sync(&moxaTimer
);
1099 if (tty_unregister_driver(moxaDriver
))
1100 printk(KERN_ERR
"Couldn't unregister MOXA Intellio family "
1102 put_tty_driver(moxaDriver
);
1105 module_init(moxa_init
);
1106 module_exit(moxa_exit
);
1108 static void moxa_close_port(struct tty_struct
*tty
)
1110 struct moxa_port
*ch
= tty
->driver_data
;
1111 moxa_shut_down(tty
);
1112 MoxaPortFlushData(ch
, 2);
1113 ch
->port
.flags
&= ~ASYNC_NORMAL_ACTIVE
;
1114 tty
->driver_data
= NULL
;
1115 tty_port_tty_set(&ch
->port
, NULL
);
1118 static int moxa_block_till_ready(struct tty_struct
*tty
, struct file
*filp
,
1119 struct moxa_port
*ch
)
1126 prepare_to_wait(&ch
->port
.open_wait
, &wait
, TASK_INTERRUPTIBLE
);
1127 if (tty_hung_up_p(filp
)) {
1128 #ifdef SERIAL_DO_RESTART
1129 retval
= -ERESTARTSYS
;
1135 spin_lock_bh(&moxa_lock
);
1137 spin_unlock_bh(&moxa_lock
);
1141 if (signal_pending(current
)) {
1142 retval
= -ERESTARTSYS
;
1147 finish_wait(&ch
->port
.open_wait
, &wait
);
1152 static int moxa_open(struct tty_struct
*tty
, struct file
*filp
)
1154 struct moxa_board_conf
*brd
;
1155 struct moxa_port
*ch
;
1160 if (port
== MAX_PORTS
) {
1161 return capable(CAP_SYS_ADMIN
) ? 0 : -EPERM
;
1163 if (mutex_lock_interruptible(&moxa_openlock
))
1164 return -ERESTARTSYS
;
1165 brd
= &moxa_boards
[port
/ MAX_PORTS_PER_BOARD
];
1167 mutex_unlock(&moxa_openlock
);
1171 ch
= &brd
->ports
[port
% MAX_PORTS_PER_BOARD
];
1173 tty
->driver_data
= ch
;
1174 tty_port_tty_set(&ch
->port
, tty
);
1175 if (!(ch
->port
.flags
& ASYNC_INITIALIZED
)) {
1176 ch
->statusflags
= 0;
1177 moxa_set_tty_param(tty
, tty
->termios
);
1178 MoxaPortLineCtrl(ch
, 1, 1);
1180 MoxaSetFifo(ch
, ch
->type
== PORT_16550A
);
1181 ch
->port
.flags
|= ASYNC_INITIALIZED
;
1183 mutex_unlock(&moxa_openlock
);
1186 if (!(filp
->f_flags
& O_NONBLOCK
) && !C_CLOCAL(tty
))
1187 retval
= moxa_block_till_ready(tty
, filp
, ch
);
1188 mutex_lock(&moxa_openlock
);
1190 if (ch
->port
.count
) /* 0 means already hung up... */
1191 if (--ch
->port
.count
== 0)
1192 moxa_close_port(tty
);
1194 ch
->port
.flags
|= ASYNC_NORMAL_ACTIVE
;
1195 mutex_unlock(&moxa_openlock
);
1200 static void moxa_close(struct tty_struct
*tty
, struct file
*filp
)
1202 struct moxa_port
*ch
;
1206 if (port
== MAX_PORTS
|| tty_hung_up_p(filp
))
1209 mutex_lock(&moxa_openlock
);
1210 ch
= tty
->driver_data
;
1213 if (tty
->count
== 1 && ch
->port
.count
!= 1) {
1214 printk(KERN_WARNING
"moxa_close: bad serial port count; "
1215 "tty->count is 1, ch->port.count is %d\n", ch
->port
.count
);
1218 if (--ch
->port
.count
< 0) {
1219 printk(KERN_WARNING
"moxa_close: bad serial port count, "
1220 "device=%s\n", tty
->name
);
1226 ch
->cflag
= tty
->termios
->c_cflag
;
1227 if (ch
->port
.flags
& ASYNC_INITIALIZED
) {
1228 moxa_setup_empty_event(tty
);
1229 tty_wait_until_sent(tty
, 30 * HZ
); /* 30 seconds timeout */
1232 moxa_close_port(tty
);
1234 mutex_unlock(&moxa_openlock
);
1237 static int moxa_write(struct tty_struct
*tty
,
1238 const unsigned char *buf
, int count
)
1240 struct moxa_port
*ch
= tty
->driver_data
;
1246 spin_lock_bh(&moxa_lock
);
1247 len
= MoxaPortWriteData(tty
, buf
, count
);
1248 spin_unlock_bh(&moxa_lock
);
1250 ch
->statusflags
|= LOWWAIT
;
1254 static int moxa_write_room(struct tty_struct
*tty
)
1256 struct moxa_port
*ch
;
1260 ch
= tty
->driver_data
;
1263 return MoxaPortTxFree(ch
);
1266 static void moxa_flush_buffer(struct tty_struct
*tty
)
1268 struct moxa_port
*ch
= tty
->driver_data
;
1272 MoxaPortFlushData(ch
, 1);
1276 static int moxa_chars_in_buffer(struct tty_struct
*tty
)
1278 struct moxa_port
*ch
= tty
->driver_data
;
1282 * Sigh...I have to check if driver_data is NULL here, because
1283 * if an open() fails, the TTY subsystem eventually calls
1284 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1285 * routine. And since the open() failed, we return 0 here. TDJ
1290 chars
= MoxaPortTxQueue(ch
);
1293 * Make it possible to wakeup anything waiting for output
1294 * in tty_ioctl.c, etc.
1296 if (!(ch
->statusflags
& EMPTYWAIT
))
1297 moxa_setup_empty_event(tty
);
1303 static int moxa_tiocmget(struct tty_struct
*tty
, struct file
*file
)
1305 struct moxa_port
*ch
;
1306 int flag
= 0, dtr
, rts
;
1308 mutex_lock(&moxa_openlock
);
1309 ch
= tty
->driver_data
;
1311 mutex_unlock(&moxa_openlock
);
1315 MoxaPortGetLineOut(ch
, &dtr
, &rts
);
1320 dtr
= MoxaPortLineStatus(ch
);
1327 mutex_unlock(&moxa_openlock
);
1331 static int moxa_tiocmset(struct tty_struct
*tty
, struct file
*file
,
1332 unsigned int set
, unsigned int clear
)
1334 struct moxa_port
*ch
;
1339 mutex_lock(&moxa_openlock
);
1340 ch
= tty
->driver_data
;
1342 mutex_unlock(&moxa_openlock
);
1346 MoxaPortGetLineOut(ch
, &dtr
, &rts
);
1347 if (set
& TIOCM_RTS
)
1349 if (set
& TIOCM_DTR
)
1351 if (clear
& TIOCM_RTS
)
1353 if (clear
& TIOCM_DTR
)
1355 MoxaPortLineCtrl(ch
, dtr
, rts
);
1356 mutex_unlock(&moxa_openlock
);
1360 static void moxa_throttle(struct tty_struct
*tty
)
1362 struct moxa_port
*ch
= tty
->driver_data
;
1364 ch
->statusflags
|= THROTTLE
;
1367 static void moxa_unthrottle(struct tty_struct
*tty
)
1369 struct moxa_port
*ch
= tty
->driver_data
;
1371 ch
->statusflags
&= ~THROTTLE
;
1374 static void moxa_set_termios(struct tty_struct
*tty
,
1375 struct ktermios
*old_termios
)
1377 struct moxa_port
*ch
= tty
->driver_data
;
1381 moxa_set_tty_param(tty
, old_termios
);
1382 if (!(old_termios
->c_cflag
& CLOCAL
) && C_CLOCAL(tty
))
1383 wake_up_interruptible(&ch
->port
.open_wait
);
1386 static void moxa_stop(struct tty_struct
*tty
)
1388 struct moxa_port
*ch
= tty
->driver_data
;
1392 MoxaPortTxDisable(ch
);
1393 ch
->statusflags
|= TXSTOPPED
;
1397 static void moxa_start(struct tty_struct
*tty
)
1399 struct moxa_port
*ch
= tty
->driver_data
;
1404 if (!(ch
->statusflags
& TXSTOPPED
))
1407 MoxaPortTxEnable(ch
);
1408 ch
->statusflags
&= ~TXSTOPPED
;
1411 static void moxa_hangup(struct tty_struct
*tty
)
1413 struct moxa_port
*ch
;
1415 mutex_lock(&moxa_openlock
);
1416 ch
= tty
->driver_data
;
1418 mutex_unlock(&moxa_openlock
);
1422 moxa_close_port(tty
);
1423 mutex_unlock(&moxa_openlock
);
1425 wake_up_interruptible(&ch
->port
.open_wait
);
1428 static void moxa_new_dcdstate(struct moxa_port
*p
, u8 dcd
)
1430 struct tty_struct
*tty
;
1433 if (dcd
!= p
->DCDState
) {
1434 tty
= tty_port_tty_get(&p
->port
);
1435 if (tty
&& C_CLOCAL(tty
) && !dcd
)
1442 static int moxa_poll_port(struct moxa_port
*p
, unsigned int handle
,
1445 struct tty_struct
*tty
= tty_port_tty_get(&p
->port
);
1446 void __iomem
*ofsAddr
;
1447 unsigned int inited
= p
->port
.flags
& ASYNC_INITIALIZED
;
1451 if ((p
->statusflags
& EMPTYWAIT
) &&
1452 MoxaPortTxQueue(p
) == 0) {
1453 p
->statusflags
&= ~EMPTYWAIT
;
1456 if ((p
->statusflags
& LOWWAIT
) && !tty
->stopped
&&
1457 MoxaPortTxQueue(p
) <= WAKEUP_CHARS
) {
1458 p
->statusflags
&= ~LOWWAIT
;
1462 if (inited
&& !(p
->statusflags
& THROTTLE
) &&
1463 MoxaPortRxQueue(p
) > 0) { /* RX */
1464 MoxaPortReadData(p
);
1465 tty_schedule_flip(tty
);
1468 p
->statusflags
&= ~EMPTYWAIT
;
1469 MoxaPortFlushData(p
, 0); /* flush RX */
1472 if (!handle
) /* nothing else to do */
1475 intr
= readw(ip
); /* port irq status */
1479 writew(0, ip
); /* ACK port */
1480 ofsAddr
= p
->tableAddr
;
1481 if (intr
& IntrTx
) /* disable tx intr */
1482 writew(readw(ofsAddr
+ HostStat
) & ~WakeupTx
,
1483 ofsAddr
+ HostStat
);
1488 if (tty
&& (intr
& IntrBreak
) && !I_IGNBRK(tty
)) { /* BREAK */
1489 tty_insert_flip_char(tty
, 0, TTY_BREAK
);
1490 tty_schedule_flip(tty
);
1494 if (intr
& IntrLine
)
1495 moxa_new_dcdstate(p
, readb(ofsAddr
+ FlagStat
) & DCD_state
);
1500 static void moxa_poll(unsigned long ignored
)
1502 struct moxa_board_conf
*brd
;
1504 unsigned int card
, port
, served
= 0;
1506 spin_lock(&moxa_lock
);
1507 for (card
= 0; card
< MAX_BOARDS
; card
++) {
1508 brd
= &moxa_boards
[card
];
1515 if (readb(brd
->intPend
) == 0xff)
1516 ip
= brd
->intTable
+ readb(brd
->intNdx
);
1518 for (port
= 0; port
< brd
->numPorts
; port
++)
1519 moxa_poll_port(&brd
->ports
[port
], !!ip
, ip
+ port
);
1522 writeb(0, brd
->intPend
); /* ACK */
1524 if (moxaLowWaterChk
) {
1525 struct moxa_port
*p
= brd
->ports
;
1526 for (port
= 0; port
< brd
->numPorts
; port
++, p
++)
1527 if (p
->lowChkFlag
) {
1529 moxa_low_water_check(p
->tableAddr
);
1533 moxaLowWaterChk
= 0;
1536 mod_timer(&moxaTimer
, jiffies
+ HZ
/ 50);
1537 spin_unlock(&moxa_lock
);
1540 /******************************************************************************/
1542 static void moxa_set_tty_param(struct tty_struct
*tty
, struct ktermios
*old_termios
)
1544 register struct ktermios
*ts
= tty
->termios
;
1545 struct moxa_port
*ch
= tty
->driver_data
;
1546 int rts
, cts
, txflow
, rxflow
, xany
, baud
;
1548 rts
= cts
= txflow
= rxflow
= xany
= 0;
1549 if (ts
->c_cflag
& CRTSCTS
)
1551 if (ts
->c_iflag
& IXON
)
1553 if (ts
->c_iflag
& IXOFF
)
1555 if (ts
->c_iflag
& IXANY
)
1558 /* Clear the features we don't support */
1559 ts
->c_cflag
&= ~CMSPAR
;
1560 MoxaPortFlowCtrl(ch
, rts
, cts
, txflow
, rxflow
, xany
);
1561 baud
= MoxaPortSetTermio(ch
, ts
, tty_get_baud_rate(tty
));
1563 baud
= tty_termios_baud_rate(old_termios
);
1564 /* Not put the baud rate into the termios data */
1565 tty_encode_baud_rate(tty
, baud
, baud
);
1568 static void moxa_setup_empty_event(struct tty_struct
*tty
)
1570 struct moxa_port
*ch
= tty
->driver_data
;
1572 spin_lock_bh(&moxa_lock
);
1573 ch
->statusflags
|= EMPTYWAIT
;
1574 spin_unlock_bh(&moxa_lock
);
1577 static void moxa_shut_down(struct tty_struct
*tty
)
1579 struct moxa_port
*ch
= tty
->driver_data
;
1581 if (!(ch
->port
.flags
& ASYNC_INITIALIZED
))
1584 MoxaPortDisable(ch
);
1587 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1590 MoxaPortLineCtrl(ch
, 0, 0);
1592 spin_lock_bh(&moxa_lock
);
1593 ch
->port
.flags
&= ~ASYNC_INITIALIZED
;
1594 spin_unlock_bh(&moxa_lock
);
1597 /*****************************************************************************
1598 * Driver level functions: *
1599 *****************************************************************************/
1601 static void MoxaPortFlushData(struct moxa_port
*port
, int mode
)
1603 void __iomem
*ofsAddr
;
1604 if (mode
< 0 || mode
> 2)
1606 ofsAddr
= port
->tableAddr
;
1607 moxafunc(ofsAddr
, FC_FlushQueue
, mode
);
1609 port
->lowChkFlag
= 0;
1610 moxa_low_water_check(ofsAddr
);
1615 * Moxa Port Number Description:
1617 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1618 * the port number using in MOXA driver functions will be 0 to 31 for
1619 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1620 * to 127 for fourth. For example, if you setup three MOXA boards,
1621 * first board is C218, second board is C320-16 and third board is
1622 * C320-32. The port number of first board (C218 - 8 ports) is from
1623 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1624 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1625 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1626 * 127 will be invalid.
1629 * Moxa Functions Description:
1631 * Function 1: Driver initialization routine, this routine must be
1632 * called when initialized driver.
1634 * void MoxaDriverInit();
1637 * Function 2: Moxa driver private IOCTL command processing.
1639 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1641 * unsigned int cmd : IOCTL command
1642 * unsigned long arg : IOCTL argument
1643 * int port : port number (0 - 127)
1650 * Function 6: Enable this port to start Tx/Rx data.
1652 * void MoxaPortEnable(int port);
1653 * int port : port number (0 - 127)
1656 * Function 7: Disable this port
1658 * void MoxaPortDisable(int port);
1659 * int port : port number (0 - 127)
1662 * Function 10: Setting baud rate of this port.
1664 * speed_t MoxaPortSetBaud(int port, speed_t baud);
1665 * int port : port number (0 - 127)
1666 * long baud : baud rate (50 - 115200)
1668 * return: 0 : this port is invalid or baud < 50
1669 * 50 - 115200 : the real baud rate set to the port, if
1670 * the argument baud is large than maximun
1671 * available baud rate, the real setting
1672 * baud rate will be the maximun baud rate.
1675 * Function 12: Configure the port.
1677 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1678 * int port : port number (0 - 127)
1679 * struct ktermios * termio : termio structure pointer
1680 * speed_t baud : baud rate
1682 * return: -1 : this port is invalid or termio == NULL
1686 * Function 13: Get the DTR/RTS state of this port.
1688 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1689 * int port : port number (0 - 127)
1690 * int * dtrState : pointer to INT to receive the current DTR
1691 * state. (if NULL, this function will not
1692 * write to this address)
1693 * int * rtsState : pointer to INT to receive the current RTS
1694 * state. (if NULL, this function will not
1695 * write to this address)
1697 * return: -1 : this port is invalid
1701 * Function 14: Setting the DTR/RTS output state of this port.
1703 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1704 * int port : port number (0 - 127)
1705 * int dtrState : DTR output state (0: off, 1: on)
1706 * int rtsState : RTS output state (0: off, 1: on)
1709 * Function 15: Setting the flow control of this port.
1711 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1712 * int txFlow,int xany);
1713 * int port : port number (0 - 127)
1714 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1715 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1716 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1717 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1718 * int xany : S/W XANY flow control (0: no, 1: yes)
1721 * Function 16: Get ths line status of this port
1723 * int MoxaPortLineStatus(int port);
1724 * int port : port number (0 - 127)
1726 * return: Bit 0 - CTS state (0: off, 1: on)
1727 * Bit 1 - DSR state (0: off, 1: on)
1728 * Bit 2 - DCD state (0: off, 1: on)
1731 * Function 19: Flush the Rx/Tx buffer data of this port.
1733 * void MoxaPortFlushData(int port, int mode);
1734 * int port : port number (0 - 127)
1736 * 0 : flush the Rx buffer
1737 * 1 : flush the Tx buffer
1738 * 2 : flush the Rx and Tx buffer
1741 * Function 20: Write data.
1743 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1744 * int port : port number (0 - 127)
1745 * unsigned char * buffer : pointer to write data buffer.
1746 * int length : write data length
1748 * return: 0 - length : real write data length
1751 * Function 21: Read data.
1753 * int MoxaPortReadData(int port, struct tty_struct *tty);
1754 * int port : port number (0 - 127)
1755 * struct tty_struct *tty : tty for data
1757 * return: 0 - length : real read data length
1760 * Function 24: Get the Tx buffer current queued data bytes
1762 * int MoxaPortTxQueue(int port);
1763 * int port : port number (0 - 127)
1765 * return: .. : Tx buffer current queued data bytes
1768 * Function 25: Get the Tx buffer current free space
1770 * int MoxaPortTxFree(int port);
1771 * int port : port number (0 - 127)
1773 * return: .. : Tx buffer current free space
1776 * Function 26: Get the Rx buffer current queued data bytes
1778 * int MoxaPortRxQueue(int port);
1779 * int port : port number (0 - 127)
1781 * return: .. : Rx buffer current queued data bytes
1784 * Function 28: Disable port data transmission.
1786 * void MoxaPortTxDisable(int port);
1787 * int port : port number (0 - 127)
1790 * Function 29: Enable port data transmission.
1792 * void MoxaPortTxEnable(int port);
1793 * int port : port number (0 - 127)
1796 * Function 31: Get the received BREAK signal count and reset it.
1798 * int MoxaPortResetBrkCnt(int port);
1799 * int port : port number (0 - 127)
1801 * return: 0 - .. : BREAK signal count
1806 static void MoxaPortEnable(struct moxa_port
*port
)
1808 void __iomem
*ofsAddr
;
1811 ofsAddr
= port
->tableAddr
;
1812 writew(lowwater
, ofsAddr
+ Low_water
);
1813 if (MOXA_IS_320(port
->board
))
1814 moxafunc(ofsAddr
, FC_SetBreakIrq
, 0);
1816 writew(readw(ofsAddr
+ HostStat
) | WakeupBreak
,
1817 ofsAddr
+ HostStat
);
1819 moxafunc(ofsAddr
, FC_SetLineIrq
, Magic_code
);
1820 moxafunc(ofsAddr
, FC_FlushQueue
, 2);
1822 moxafunc(ofsAddr
, FC_EnableCH
, Magic_code
);
1823 MoxaPortLineStatus(port
);
1826 static void MoxaPortDisable(struct moxa_port
*port
)
1828 void __iomem
*ofsAddr
= port
->tableAddr
;
1830 moxafunc(ofsAddr
, FC_SetFlowCtl
, 0); /* disable flow control */
1831 moxafunc(ofsAddr
, FC_ClrLineIrq
, Magic_code
);
1832 writew(0, ofsAddr
+ HostStat
);
1833 moxafunc(ofsAddr
, FC_DisableCH
, Magic_code
);
1836 static speed_t
MoxaPortSetBaud(struct moxa_port
*port
, speed_t baud
)
1838 void __iomem
*ofsAddr
= port
->tableAddr
;
1839 unsigned int clock
, val
;
1842 max
= MOXA_IS_320(port
->board
) ? 460800 : 921600;
1849 moxafunc(ofsAddr
, FC_SetBaud
, val
);
1854 static int MoxaPortSetTermio(struct moxa_port
*port
, struct ktermios
*termio
,
1857 void __iomem
*ofsAddr
;
1861 ofsAddr
= port
->tableAddr
;
1862 cflag
= termio
->c_cflag
; /* termio->c_cflag */
1864 mode
= termio
->c_cflag
& CSIZE
;
1867 else if (mode
== CS6
)
1869 else if (mode
== CS7
)
1871 else if (mode
== CS8
)
1874 if (termio
->c_cflag
& CSTOPB
) {
1882 if (termio
->c_cflag
& PARENB
) {
1883 if (termio
->c_cflag
& PARODD
)
1890 moxafunc(ofsAddr
, FC_SetDataMode
, (u16
)mode
);
1892 if (MOXA_IS_320(port
->board
) && baud
>= 921600)
1895 baud
= MoxaPortSetBaud(port
, baud
);
1897 if (termio
->c_iflag
& (IXON
| IXOFF
| IXANY
)) {
1898 writeb(termio
->c_cc
[VSTART
], ofsAddr
+ FuncArg
);
1899 writeb(termio
->c_cc
[VSTOP
], ofsAddr
+ FuncArg1
);
1900 writeb(FC_SetXonXoff
, ofsAddr
+ FuncCode
);
1901 moxa_wait_finish(ofsAddr
);
1907 static int MoxaPortGetLineOut(struct moxa_port
*port
, int *dtrState
,
1911 *dtrState
= !!(port
->lineCtrl
& DTR_ON
);
1913 *rtsState
= !!(port
->lineCtrl
& RTS_ON
);
1918 static void MoxaPortLineCtrl(struct moxa_port
*port
, int dtr
, int rts
)
1926 port
->lineCtrl
= mode
;
1927 moxafunc(port
->tableAddr
, FC_LineControl
, mode
);
1930 static void MoxaPortFlowCtrl(struct moxa_port
*port
, int rts
, int cts
,
1931 int txflow
, int rxflow
, int txany
)
1936 mode
|= RTS_FlowCtl
;
1938 mode
|= CTS_FlowCtl
;
1945 moxafunc(port
->tableAddr
, FC_SetFlowCtl
, mode
);
1948 static int MoxaPortLineStatus(struct moxa_port
*port
)
1950 void __iomem
*ofsAddr
;
1953 ofsAddr
= port
->tableAddr
;
1954 if (MOXA_IS_320(port
->board
)) {
1955 moxafunc(ofsAddr
, FC_LineStatus
, 0);
1956 val
= readw(ofsAddr
+ FuncArg
);
1958 val
= readw(ofsAddr
+ FlagStat
) >> 4;
1963 spin_lock_bh(&moxa_lock
);
1964 moxa_new_dcdstate(port
, val
& 8);
1965 spin_unlock_bh(&moxa_lock
);
1970 static int MoxaPortWriteData(struct tty_struct
*tty
,
1971 const unsigned char *buffer
, int len
)
1973 struct moxa_port
*port
= tty
->driver_data
;
1974 void __iomem
*baseAddr
, *ofsAddr
, *ofs
;
1975 unsigned int c
, total
;
1976 u16 head
, tail
, tx_mask
, spage
, epage
;
1977 u16 pageno
, pageofs
, bufhead
;
1979 ofsAddr
= port
->tableAddr
;
1980 baseAddr
= port
->board
->basemem
;
1981 tx_mask
= readw(ofsAddr
+ TX_mask
);
1982 spage
= readw(ofsAddr
+ Page_txb
);
1983 epage
= readw(ofsAddr
+ EndPage_txb
);
1984 tail
= readw(ofsAddr
+ TXwptr
);
1985 head
= readw(ofsAddr
+ TXrptr
);
1986 c
= (head
> tail
) ? (head
- tail
- 1) : (head
- tail
+ tx_mask
);
1989 moxaLog
.txcnt
[port
->port
.tty
->index
] += c
;
1991 if (spage
== epage
) {
1992 bufhead
= readw(ofsAddr
+ Ofs_txb
);
1993 writew(spage
, baseAddr
+ Control_reg
);
1996 len
= head
- tail
- 1;
1998 len
= tx_mask
+ 1 - tail
;
1999 len
= (c
> len
) ? len
: c
;
2000 ofs
= baseAddr
+ DynPage_addr
+ bufhead
+ tail
;
2001 memcpy_toio(ofs
, buffer
, len
);
2003 tail
= (tail
+ len
) & tx_mask
;
2007 pageno
= spage
+ (tail
>> 13);
2008 pageofs
= tail
& Page_mask
;
2010 len
= Page_size
- pageofs
;
2013 writeb(pageno
, baseAddr
+ Control_reg
);
2014 ofs
= baseAddr
+ DynPage_addr
+ pageofs
;
2015 memcpy_toio(ofs
, buffer
, len
);
2017 if (++pageno
== epage
)
2022 tail
= (tail
+ total
) & tx_mask
;
2024 writew(tail
, ofsAddr
+ TXwptr
);
2025 writeb(1, ofsAddr
+ CD180TXirq
); /* start to send */
2029 static int MoxaPortReadData(struct moxa_port
*port
)
2031 struct tty_struct
*tty
= port
->port
.tty
;
2033 void __iomem
*baseAddr
, *ofsAddr
, *ofs
;
2034 unsigned int count
, len
, total
;
2035 u16 tail
, rx_mask
, spage
, epage
;
2036 u16 pageno
, pageofs
, bufhead
, head
;
2038 ofsAddr
= port
->tableAddr
;
2039 baseAddr
= port
->board
->basemem
;
2040 head
= readw(ofsAddr
+ RXrptr
);
2041 tail
= readw(ofsAddr
+ RXwptr
);
2042 rx_mask
= readw(ofsAddr
+ RX_mask
);
2043 spage
= readw(ofsAddr
+ Page_rxb
);
2044 epage
= readw(ofsAddr
+ EndPage_rxb
);
2045 count
= (tail
>= head
) ? (tail
- head
) : (tail
- head
+ rx_mask
+ 1);
2050 moxaLog
.rxcnt
[tty
->index
] += total
;
2051 if (spage
== epage
) {
2052 bufhead
= readw(ofsAddr
+ Ofs_rxb
);
2053 writew(spage
, baseAddr
+ Control_reg
);
2055 ofs
= baseAddr
+ DynPage_addr
+ bufhead
+ head
;
2056 len
= (tail
>= head
) ? (tail
- head
) :
2057 (rx_mask
+ 1 - head
);
2058 len
= tty_prepare_flip_string(tty
, &dst
,
2060 memcpy_fromio(dst
, ofs
, len
);
2061 head
= (head
+ len
) & rx_mask
;
2065 pageno
= spage
+ (head
>> 13);
2066 pageofs
= head
& Page_mask
;
2068 writew(pageno
, baseAddr
+ Control_reg
);
2069 ofs
= baseAddr
+ DynPage_addr
+ pageofs
;
2070 len
= tty_prepare_flip_string(tty
, &dst
,
2071 min(Page_size
- pageofs
, count
));
2072 memcpy_fromio(dst
, ofs
, len
);
2075 pageofs
= (pageofs
+ len
) & Page_mask
;
2076 if (pageofs
== 0 && ++pageno
== epage
)
2079 head
= (head
+ total
) & rx_mask
;
2081 writew(head
, ofsAddr
+ RXrptr
);
2082 if (readb(ofsAddr
+ FlagStat
) & Xoff_state
) {
2083 moxaLowWaterChk
= 1;
2084 port
->lowChkFlag
= 1;
2090 static int MoxaPortTxQueue(struct moxa_port
*port
)
2092 void __iomem
*ofsAddr
= port
->tableAddr
;
2093 u16 rptr
, wptr
, mask
;
2095 rptr
= readw(ofsAddr
+ TXrptr
);
2096 wptr
= readw(ofsAddr
+ TXwptr
);
2097 mask
= readw(ofsAddr
+ TX_mask
);
2098 return (wptr
- rptr
) & mask
;
2101 static int MoxaPortTxFree(struct moxa_port
*port
)
2103 void __iomem
*ofsAddr
= port
->tableAddr
;
2104 u16 rptr
, wptr
, mask
;
2106 rptr
= readw(ofsAddr
+ TXrptr
);
2107 wptr
= readw(ofsAddr
+ TXwptr
);
2108 mask
= readw(ofsAddr
+ TX_mask
);
2109 return mask
- ((wptr
- rptr
) & mask
);
2112 static int MoxaPortRxQueue(struct moxa_port
*port
)
2114 void __iomem
*ofsAddr
= port
->tableAddr
;
2115 u16 rptr
, wptr
, mask
;
2117 rptr
= readw(ofsAddr
+ RXrptr
);
2118 wptr
= readw(ofsAddr
+ RXwptr
);
2119 mask
= readw(ofsAddr
+ RX_mask
);
2120 return (wptr
- rptr
) & mask
;
2123 static void MoxaPortTxDisable(struct moxa_port
*port
)
2125 moxafunc(port
->tableAddr
, FC_SetXoffState
, Magic_code
);
2128 static void MoxaPortTxEnable(struct moxa_port
*port
)
2130 moxafunc(port
->tableAddr
, FC_SetXonState
, Magic_code
);
2133 static int moxa_get_serial_info(struct moxa_port
*info
,
2134 struct serial_struct __user
*retinfo
)
2136 struct serial_struct tmp
= {
2138 .line
= info
->port
.tty
->index
,
2139 .flags
= info
->port
.flags
,
2140 .baud_base
= 921600,
2141 .close_delay
= info
->port
.close_delay
2143 return copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)) ? -EFAULT
: 0;
2147 static int moxa_set_serial_info(struct moxa_port
*info
,
2148 struct serial_struct __user
*new_info
)
2150 struct serial_struct new_serial
;
2152 if (copy_from_user(&new_serial
, new_info
, sizeof(new_serial
)))
2155 if (new_serial
.irq
!= 0 || new_serial
.port
!= 0 ||
2156 new_serial
.custom_divisor
!= 0 ||
2157 new_serial
.baud_base
!= 921600)
2160 if (!capable(CAP_SYS_ADMIN
)) {
2161 if (((new_serial
.flags
& ~ASYNC_USR_MASK
) !=
2162 (info
->port
.flags
& ~ASYNC_USR_MASK
)))
2165 info
->port
.close_delay
= new_serial
.close_delay
* HZ
/ 100;
2167 new_serial
.flags
= (new_serial
.flags
& ~ASYNC_FLAGS
);
2168 new_serial
.flags
|= (info
->port
.flags
& ASYNC_FLAGS
);
2170 MoxaSetFifo(info
, new_serial
.type
== PORT_16550A
);
2172 info
->type
= new_serial
.type
;
2178 /*****************************************************************************
2179 * Static local functions: *
2180 *****************************************************************************/
2182 static void MoxaSetFifo(struct moxa_port
*port
, int enable
)
2184 void __iomem
*ofsAddr
= port
->tableAddr
;
2187 moxafunc(ofsAddr
, FC_SetRxFIFOTrig
, 0);
2188 moxafunc(ofsAddr
, FC_SetTxFIFOCnt
, 1);
2190 moxafunc(ofsAddr
, FC_SetRxFIFOTrig
, 3);
2191 moxafunc(ofsAddr
, FC_SetTxFIFOCnt
, 16);