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
;
141 u8 DCDState
; /* Protected by the port lock */
148 int rxcnt
[MAX_PORTS
];
149 int txcnt
[MAX_PORTS
];
157 #define SERIAL_DO_RESTART
159 #define WAKEUP_CHARS 256
161 static int ttymajor
= MOXAMAJOR
;
162 static struct mon_str moxaLog
;
163 static unsigned int moxaFuncTout
= HZ
/ 2;
164 static unsigned int moxaLowWaterChk
;
165 static DEFINE_MUTEX(moxa_openlock
);
166 static DEFINE_SPINLOCK(moxa_lock
);
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_set_termios(struct tty_struct
*, struct ktermios
*);
197 static void moxa_stop(struct tty_struct
*);
198 static void moxa_start(struct tty_struct
*);
199 static void moxa_hangup(struct tty_struct
*);
200 static int moxa_tiocmget(struct tty_struct
*tty
, struct file
*file
);
201 static int moxa_tiocmset(struct tty_struct
*tty
, struct file
*file
,
202 unsigned int set
, unsigned int clear
);
203 static void moxa_poll(unsigned long);
204 static void moxa_set_tty_param(struct tty_struct
*, struct ktermios
*);
205 static void moxa_shutdown(struct tty_port
*);
206 static int moxa_carrier_raised(struct tty_port
*);
207 static void moxa_dtr_rts(struct tty_port
*, int);
209 * moxa board interface functions:
211 static void MoxaPortEnable(struct moxa_port
*);
212 static void MoxaPortDisable(struct moxa_port
*);
213 static int MoxaPortSetTermio(struct moxa_port
*, struct ktermios
*, speed_t
);
214 static int MoxaPortGetLineOut(struct moxa_port
*, int *, int *);
215 static void MoxaPortLineCtrl(struct moxa_port
*, int, int);
216 static void MoxaPortFlowCtrl(struct moxa_port
*, int, int, int, int, int);
217 static int MoxaPortLineStatus(struct moxa_port
*);
218 static void MoxaPortFlushData(struct moxa_port
*, int);
219 static int MoxaPortWriteData(struct tty_struct
*, const unsigned char *, int);
220 static int MoxaPortReadData(struct moxa_port
*);
221 static int MoxaPortTxQueue(struct moxa_port
*);
222 static int MoxaPortRxQueue(struct moxa_port
*);
223 static int MoxaPortTxFree(struct moxa_port
*);
224 static void MoxaPortTxDisable(struct moxa_port
*);
225 static void MoxaPortTxEnable(struct moxa_port
*);
226 static int moxa_get_serial_info(struct moxa_port
*, struct serial_struct __user
*);
227 static int moxa_set_serial_info(struct moxa_port
*, struct serial_struct __user
*);
228 static void MoxaSetFifo(struct moxa_port
*port
, int enable
);
234 static DEFINE_SPINLOCK(moxafunc_lock
);
236 static void moxa_wait_finish(void __iomem
*ofsAddr
)
238 unsigned long end
= jiffies
+ moxaFuncTout
;
240 while (readw(ofsAddr
+ FuncCode
) != 0)
241 if (time_after(jiffies
, end
))
243 if (readw(ofsAddr
+ FuncCode
) != 0 && printk_ratelimit())
244 printk(KERN_WARNING
"moxa function expired\n");
247 static void moxafunc(void __iomem
*ofsAddr
, u16 cmd
, u16 arg
)
250 spin_lock_irqsave(&moxafunc_lock
, flags
);
251 writew(arg
, ofsAddr
+ FuncArg
);
252 writew(cmd
, ofsAddr
+ FuncCode
);
253 moxa_wait_finish(ofsAddr
);
254 spin_unlock_irqrestore(&moxafunc_lock
, flags
);
257 static int moxafuncret(void __iomem
*ofsAddr
, u16 cmd
, u16 arg
)
261 spin_lock_irqsave(&moxafunc_lock
, flags
);
262 writew(arg
, ofsAddr
+ FuncArg
);
263 writew(cmd
, ofsAddr
+ FuncCode
);
264 moxa_wait_finish(ofsAddr
);
265 ret
= readw(ofsAddr
+ FuncArg
);
266 spin_unlock_irqrestore(&moxafunc_lock
, flags
);
270 static void moxa_low_water_check(void __iomem
*ofsAddr
)
272 u16 rptr
, wptr
, mask
, len
;
274 if (readb(ofsAddr
+ FlagStat
) & Xoff_state
) {
275 rptr
= readw(ofsAddr
+ RXrptr
);
276 wptr
= readw(ofsAddr
+ RXwptr
);
277 mask
= readw(ofsAddr
+ RX_mask
);
278 len
= (wptr
- rptr
) & mask
;
279 if (len
<= Low_water
)
280 moxafunc(ofsAddr
, FC_SendXon
, 0);
288 static int moxa_ioctl(struct tty_struct
*tty
, struct file
*file
,
289 unsigned int cmd
, unsigned long arg
)
291 struct moxa_port
*ch
= tty
->driver_data
;
292 void __user
*argp
= (void __user
*)arg
;
295 if (tty
->index
== MAX_PORTS
) {
296 if (cmd
!= MOXA_GETDATACOUNT
&& cmd
!= MOXA_GET_IOQUEUE
&&
297 cmd
!= MOXA_GETMSTATUS
)
303 case MOXA_GETDATACOUNT
:
304 moxaLog
.tick
= jiffies
;
305 if (copy_to_user(argp
, &moxaLog
, sizeof(moxaLog
)))
308 case MOXA_FLUSH_QUEUE
:
309 MoxaPortFlushData(ch
, arg
);
311 case MOXA_GET_IOQUEUE
: {
312 struct moxaq_str __user
*argm
= argp
;
313 struct moxaq_str tmp
;
317 for (i
= 0; i
< MAX_BOARDS
; i
++) {
318 p
= moxa_boards
[i
].ports
;
319 for (j
= 0; j
< MAX_PORTS_PER_BOARD
; j
++, p
++, argm
++) {
320 memset(&tmp
, 0, sizeof(tmp
));
321 spin_lock_bh(&moxa_lock
);
322 if (moxa_boards
[i
].ready
) {
323 tmp
.inq
= MoxaPortRxQueue(p
);
324 tmp
.outq
= MoxaPortTxQueue(p
);
326 spin_unlock_bh(&moxa_lock
);
327 if (copy_to_user(argm
, &tmp
, sizeof(tmp
)))
332 } case MOXA_GET_OQUEUE
:
333 status
= MoxaPortTxQueue(ch
);
334 ret
= put_user(status
, (unsigned long __user
*)argp
);
336 case MOXA_GET_IQUEUE
:
337 status
= MoxaPortRxQueue(ch
);
338 ret
= put_user(status
, (unsigned long __user
*)argp
);
340 case MOXA_GETMSTATUS
: {
341 struct mxser_mstatus __user
*argm
= argp
;
342 struct mxser_mstatus tmp
;
346 for (i
= 0; i
< MAX_BOARDS
; i
++) {
347 p
= moxa_boards
[i
].ports
;
348 for (j
= 0; j
< MAX_PORTS_PER_BOARD
; j
++, p
++, argm
++) {
349 struct tty_struct
*ttyp
;
350 memset(&tmp
, 0, sizeof(tmp
));
351 spin_lock_bh(&moxa_lock
);
352 if (!moxa_boards
[i
].ready
) {
353 spin_unlock_bh(&moxa_lock
);
357 status
= MoxaPortLineStatus(p
);
358 spin_unlock_bh(&moxa_lock
);
367 ttyp
= tty_port_tty_get(&p
->port
);
368 if (!ttyp
|| !ttyp
->termios
)
369 tmp
.cflag
= p
->cflag
;
371 tmp
.cflag
= ttyp
->termios
->c_cflag
;
374 if (copy_to_user(argm
, &tmp
, sizeof(tmp
)))
381 mutex_lock(&ch
->port
.mutex
);
382 ret
= moxa_get_serial_info(ch
, argp
);
383 mutex_unlock(&ch
->port
.mutex
);
386 mutex_lock(&ch
->port
.mutex
);
387 ret
= moxa_set_serial_info(ch
, argp
);
388 mutex_unlock(&ch
->port
.mutex
);
396 static int moxa_break_ctl(struct tty_struct
*tty
, int state
)
398 struct moxa_port
*port
= tty
->driver_data
;
400 moxafunc(port
->tableAddr
, state
? FC_SendBreak
: FC_StopBreak
,
405 static const struct tty_operations moxa_ops
= {
409 .write_room
= moxa_write_room
,
410 .flush_buffer
= moxa_flush_buffer
,
411 .chars_in_buffer
= moxa_chars_in_buffer
,
413 .set_termios
= moxa_set_termios
,
416 .hangup
= moxa_hangup
,
417 .break_ctl
= moxa_break_ctl
,
418 .tiocmget
= moxa_tiocmget
,
419 .tiocmset
= moxa_tiocmset
,
422 static const struct tty_port_operations moxa_port_ops
= {
423 .carrier_raised
= moxa_carrier_raised
,
424 .dtr_rts
= moxa_dtr_rts
,
425 .shutdown
= moxa_shutdown
,
428 static struct tty_driver
*moxaDriver
;
429 static DEFINE_TIMER(moxaTimer
, moxa_poll
, 0, 0);
435 static int moxa_check_fw_model(struct moxa_board_conf
*brd
, u8 model
)
437 switch (brd
->boardType
) {
438 case MOXA_BOARD_C218_ISA
:
439 case MOXA_BOARD_C218_PCI
:
443 case MOXA_BOARD_CP204J
:
457 static int moxa_check_fw(const void *ptr
)
459 const __le16
*lptr
= ptr
;
461 if (*lptr
!= cpu_to_le16(0x7980))
467 static int moxa_load_bios(struct moxa_board_conf
*brd
, const u8
*buf
,
470 void __iomem
*baseAddr
= brd
->basemem
;
473 writeb(HW_reset
, baseAddr
+ Control_reg
); /* reset */
475 memset_io(baseAddr
, 0, 4096);
476 memcpy_toio(baseAddr
, buf
, len
); /* download BIOS */
477 writeb(0, baseAddr
+ Control_reg
); /* restart */
481 switch (brd
->boardType
) {
482 case MOXA_BOARD_C218_ISA
:
483 case MOXA_BOARD_C218_PCI
:
484 tmp
= readw(baseAddr
+ C218_key
);
485 if (tmp
!= C218_KeyCode
)
488 case MOXA_BOARD_CP204J
:
489 tmp
= readw(baseAddr
+ C218_key
);
490 if (tmp
!= CP204J_KeyCode
)
494 tmp
= readw(baseAddr
+ C320_key
);
495 if (tmp
!= C320_KeyCode
)
497 tmp
= readw(baseAddr
+ C320_status
);
498 if (tmp
!= STS_init
) {
499 printk(KERN_ERR
"MOXA: bios upload failed -- CPU/Basic "
500 "module not found\n");
508 printk(KERN_ERR
"MOXA: bios upload failed -- board not found\n");
512 static int moxa_load_320b(struct moxa_board_conf
*brd
, const u8
*ptr
,
515 void __iomem
*baseAddr
= brd
->basemem
;
518 printk(KERN_ERR
"MOXA: invalid 320 bios -- too short\n");
522 writew(len
- 7168 - 2, baseAddr
+ C320bapi_len
);
523 writeb(1, baseAddr
+ Control_reg
); /* Select Page 1 */
524 memcpy_toio(baseAddr
+ DynPage_addr
, ptr
, 7168);
525 writeb(2, baseAddr
+ Control_reg
); /* Select Page 2 */
526 memcpy_toio(baseAddr
+ DynPage_addr
, ptr
+ 7168, len
- 7168);
531 static int moxa_real_load_code(struct moxa_board_conf
*brd
, const void *ptr
,
534 void __iomem
*baseAddr
= brd
->basemem
;
535 const __le16
*uptr
= ptr
;
536 size_t wlen
, len2
, j
;
537 unsigned long key
, loadbuf
, loadlen
, checksum
, checksum_ok
;
538 unsigned int i
, retry
;
541 keycode
= (brd
->boardType
== MOXA_BOARD_CP204J
) ? CP204J_KeyCode
:
544 switch (brd
->boardType
) {
545 case MOXA_BOARD_CP204J
:
546 case MOXA_BOARD_C218_ISA
:
547 case MOXA_BOARD_C218_PCI
:
549 loadbuf
= C218_LoadBuf
;
550 loadlen
= C218DLoad_len
;
551 checksum
= C218check_sum
;
552 checksum_ok
= C218chksum_ok
;
556 keycode
= C320_KeyCode
;
557 loadbuf
= C320_LoadBuf
;
558 loadlen
= C320DLoad_len
;
559 checksum
= C320check_sum
;
560 checksum_ok
= C320chksum_ok
;
566 for (i
= 0; i
< wlen
; i
++)
567 usum
+= le16_to_cpu(uptr
[i
]);
573 len2
= (wlen
> 2048) ? 2048 : wlen
;
575 memcpy_toio(baseAddr
+ loadbuf
, ptr
+ j
, len2
<< 1);
578 writew(len2
, baseAddr
+ loadlen
);
579 writew(0, baseAddr
+ key
);
580 for (i
= 0; i
< 100; i
++) {
581 if (readw(baseAddr
+ key
) == keycode
)
585 if (readw(baseAddr
+ key
) != keycode
)
588 writew(0, baseAddr
+ loadlen
);
589 writew(usum
, baseAddr
+ checksum
);
590 writew(0, baseAddr
+ key
);
591 for (i
= 0; i
< 100; i
++) {
592 if (readw(baseAddr
+ key
) == keycode
)
597 } while ((readb(baseAddr
+ checksum_ok
) != 1) && (retry
< 3));
598 if (readb(baseAddr
+ checksum_ok
) != 1)
601 writew(0, baseAddr
+ key
);
602 for (i
= 0; i
< 600; i
++) {
603 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
607 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
610 if (MOXA_IS_320(brd
)) {
611 if (brd
->busType
== MOXA_BUS_TYPE_PCI
) { /* ASIC board */
612 writew(0x3800, baseAddr
+ TMS320_PORT1
);
613 writew(0x3900, baseAddr
+ TMS320_PORT2
);
614 writew(28499, baseAddr
+ TMS320_CLOCK
);
616 writew(0x3200, baseAddr
+ TMS320_PORT1
);
617 writew(0x3400, baseAddr
+ TMS320_PORT2
);
618 writew(19999, baseAddr
+ TMS320_CLOCK
);
621 writew(1, baseAddr
+ Disable_IRQ
);
622 writew(0, baseAddr
+ Magic_no
);
623 for (i
= 0; i
< 500; i
++) {
624 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
628 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
631 if (MOXA_IS_320(brd
)) {
632 j
= readw(baseAddr
+ Module_cnt
);
635 brd
->numPorts
= j
* 8;
636 writew(j
, baseAddr
+ Module_no
);
637 writew(0, baseAddr
+ Magic_no
);
638 for (i
= 0; i
< 600; i
++) {
639 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
643 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
646 brd
->intNdx
= baseAddr
+ IRQindex
;
647 brd
->intPend
= baseAddr
+ IRQpending
;
648 brd
->intTable
= baseAddr
+ IRQtable
;
653 static int moxa_load_code(struct moxa_board_conf
*brd
, const void *ptr
,
656 void __iomem
*ofsAddr
, *baseAddr
= brd
->basemem
;
657 struct moxa_port
*port
;
661 printk(KERN_ERR
"MOXA: bios length is not even\n");
665 retval
= moxa_real_load_code(brd
, ptr
, len
); /* may change numPorts */
669 switch (brd
->boardType
) {
670 case MOXA_BOARD_C218_ISA
:
671 case MOXA_BOARD_C218_PCI
:
672 case MOXA_BOARD_CP204J
:
674 for (i
= 0; i
< brd
->numPorts
; i
++, port
++) {
677 port
->tableAddr
= baseAddr
+ Extern_table
+
679 ofsAddr
= port
->tableAddr
;
680 writew(C218rx_mask
, ofsAddr
+ RX_mask
);
681 writew(C218tx_mask
, ofsAddr
+ TX_mask
);
682 writew(C218rx_spage
+ i
* C218buf_pageno
, ofsAddr
+ Page_rxb
);
683 writew(readw(ofsAddr
+ Page_rxb
) + C218rx_pageno
, ofsAddr
+ EndPage_rxb
);
685 writew(C218tx_spage
+ i
* C218buf_pageno
, ofsAddr
+ Page_txb
);
686 writew(readw(ofsAddr
+ Page_txb
) + C218tx_pageno
, ofsAddr
+ EndPage_txb
);
692 for (i
= 0; i
< brd
->numPorts
; i
++, port
++) {
695 port
->tableAddr
= baseAddr
+ Extern_table
+
697 ofsAddr
= port
->tableAddr
;
698 switch (brd
->numPorts
) {
700 writew(C320p8rx_mask
, ofsAddr
+ RX_mask
);
701 writew(C320p8tx_mask
, ofsAddr
+ TX_mask
);
702 writew(C320p8rx_spage
+ i
* C320p8buf_pgno
, ofsAddr
+ Page_rxb
);
703 writew(readw(ofsAddr
+ Page_rxb
) + C320p8rx_pgno
, ofsAddr
+ EndPage_rxb
);
704 writew(C320p8tx_spage
+ i
* C320p8buf_pgno
, ofsAddr
+ Page_txb
);
705 writew(readw(ofsAddr
+ Page_txb
) + C320p8tx_pgno
, ofsAddr
+ EndPage_txb
);
709 writew(C320p16rx_mask
, ofsAddr
+ RX_mask
);
710 writew(C320p16tx_mask
, ofsAddr
+ TX_mask
);
711 writew(C320p16rx_spage
+ i
* C320p16buf_pgno
, ofsAddr
+ Page_rxb
);
712 writew(readw(ofsAddr
+ Page_rxb
) + C320p16rx_pgno
, ofsAddr
+ EndPage_rxb
);
713 writew(C320p16tx_spage
+ i
* C320p16buf_pgno
, ofsAddr
+ Page_txb
);
714 writew(readw(ofsAddr
+ Page_txb
) + C320p16tx_pgno
, ofsAddr
+ EndPage_txb
);
718 writew(C320p24rx_mask
, ofsAddr
+ RX_mask
);
719 writew(C320p24tx_mask
, ofsAddr
+ TX_mask
);
720 writew(C320p24rx_spage
+ i
* C320p24buf_pgno
, ofsAddr
+ Page_rxb
);
721 writew(readw(ofsAddr
+ Page_rxb
) + C320p24rx_pgno
, ofsAddr
+ EndPage_rxb
);
722 writew(C320p24tx_spage
+ i
* C320p24buf_pgno
, ofsAddr
+ Page_txb
);
723 writew(readw(ofsAddr
+ Page_txb
), ofsAddr
+ EndPage_txb
);
726 writew(C320p32rx_mask
, ofsAddr
+ RX_mask
);
727 writew(C320p32tx_mask
, ofsAddr
+ TX_mask
);
728 writew(C320p32tx_ofs
, ofsAddr
+ Ofs_txb
);
729 writew(C320p32rx_spage
+ i
* C320p32buf_pgno
, ofsAddr
+ Page_rxb
);
730 writew(readb(ofsAddr
+ Page_rxb
), ofsAddr
+ EndPage_rxb
);
731 writew(C320p32tx_spage
+ i
* C320p32buf_pgno
, ofsAddr
+ Page_txb
);
732 writew(readw(ofsAddr
+ Page_txb
), ofsAddr
+ EndPage_txb
);
741 static int moxa_load_fw(struct moxa_board_conf
*brd
, const struct firmware
*fw
)
743 const void *ptr
= fw
->data
;
747 unsigned int a
, lenp
, lencnt
;
750 __le32 magic
; /* 0x34303430 */
752 u8 type
; /* UNIX = 3 */
753 u8 model
; /* C218T=1, C320T=2, CP204=3 */
758 BUILD_BUG_ON(ARRAY_SIZE(hdr
->len
) != ARRAY_SIZE(lens
));
760 if (fw
->size
< MOXA_FW_HDRLEN
) {
761 strcpy(rsn
, "too short (even header won't fit)");
764 if (hdr
->magic
!= cpu_to_le32(0x30343034)) {
765 sprintf(rsn
, "bad magic: %.8x", le32_to_cpu(hdr
->magic
));
768 if (hdr
->type
!= 3) {
769 sprintf(rsn
, "not for linux, type is %u", hdr
->type
);
772 if (moxa_check_fw_model(brd
, hdr
->model
)) {
773 sprintf(rsn
, "not for this card, model is %u", hdr
->model
);
777 len
= MOXA_FW_HDRLEN
;
778 lencnt
= hdr
->model
== 2 ? 5 : 3;
779 for (a
= 0; a
< ARRAY_SIZE(lens
); a
++) {
780 lens
[a
] = le16_to_cpu(hdr
->len
[a
]);
781 if (lens
[a
] && len
+ lens
[a
] <= fw
->size
&&
782 moxa_check_fw(&fw
->data
[len
]))
783 printk(KERN_WARNING
"MOXA firmware: unexpected input "
784 "at offset %u, but going on\n", (u32
)len
);
785 if (!lens
[a
] && a
< lencnt
) {
786 sprintf(rsn
, "too few entries in fw file");
792 if (len
!= fw
->size
) {
793 sprintf(rsn
, "bad length: %u (should be %u)", (u32
)fw
->size
,
798 ptr
+= MOXA_FW_HDRLEN
;
801 strcpy(rsn
, "read above");
803 ret
= moxa_load_bios(brd
, ptr
, lens
[lenp
]);
807 /* we skip the tty section (lens[1]), since we don't need it */
808 ptr
+= lens
[lenp
] + lens
[lenp
+ 1];
809 lenp
+= 2; /* comm */
811 if (hdr
->model
== 2) {
812 ret
= moxa_load_320b(brd
, ptr
, lens
[lenp
]);
815 /* skip another tty */
816 ptr
+= lens
[lenp
] + lens
[lenp
+ 1];
820 ret
= moxa_load_code(brd
, ptr
, lens
[lenp
]);
826 printk(KERN_ERR
"firmware failed to load, reason: %s\n", rsn
);
830 static int moxa_init_board(struct moxa_board_conf
*brd
, struct device
*dev
)
832 const struct firmware
*fw
;
838 brd
->ports
= kcalloc(MAX_PORTS_PER_BOARD
, sizeof(*brd
->ports
),
840 if (brd
->ports
== NULL
) {
841 printk(KERN_ERR
"cannot allocate memory for ports\n");
846 for (i
= 0, p
= brd
->ports
; i
< MAX_PORTS_PER_BOARD
; i
++, p
++) {
847 tty_port_init(&p
->port
);
848 p
->port
.ops
= &moxa_port_ops
;
849 p
->type
= PORT_16550A
;
850 p
->cflag
= B9600
| CS8
| CREAD
| CLOCAL
| HUPCL
;
853 switch (brd
->boardType
) {
854 case MOXA_BOARD_C218_ISA
:
855 case MOXA_BOARD_C218_PCI
:
856 file
= "c218tunx.cod";
858 case MOXA_BOARD_CP204J
:
859 file
= "cp204unx.cod";
862 file
= "c320tunx.cod";
866 ret
= request_firmware(&fw
, file
, dev
);
868 printk(KERN_ERR
"MOXA: request_firmware failed. Make sure "
869 "you've placed '%s' file into your firmware "
870 "loader directory (e.g. /lib/firmware)\n",
875 ret
= moxa_load_fw(brd
, fw
);
877 release_firmware(fw
);
882 spin_lock_bh(&moxa_lock
);
884 if (!timer_pending(&moxaTimer
))
885 mod_timer(&moxaTimer
, jiffies
+ HZ
/ 50);
886 spin_unlock_bh(&moxa_lock
);
895 static void moxa_board_deinit(struct moxa_board_conf
*brd
)
897 unsigned int a
, opened
;
899 mutex_lock(&moxa_openlock
);
900 spin_lock_bh(&moxa_lock
);
902 spin_unlock_bh(&moxa_lock
);
904 /* pci hot-un-plug support */
905 for (a
= 0; a
< brd
->numPorts
; a
++)
906 if (brd
->ports
[a
].port
.flags
& ASYNC_INITIALIZED
) {
907 struct tty_struct
*tty
= tty_port_tty_get(
908 &brd
->ports
[a
].port
);
916 for (a
= 0; a
< brd
->numPorts
; a
++)
917 if (brd
->ports
[a
].port
.flags
& ASYNC_INITIALIZED
)
919 mutex_unlock(&moxa_openlock
);
923 mutex_lock(&moxa_openlock
);
926 iounmap(brd
->basemem
);
932 static int __devinit
moxa_pci_probe(struct pci_dev
*pdev
,
933 const struct pci_device_id
*ent
)
935 struct moxa_board_conf
*board
;
937 int board_type
= ent
->driver_data
;
940 retval
= pci_enable_device(pdev
);
942 dev_err(&pdev
->dev
, "can't enable pci device\n");
946 for (i
= 0; i
< MAX_BOARDS
; i
++)
947 if (moxa_boards
[i
].basemem
== NULL
)
951 if (i
>= MAX_BOARDS
) {
952 dev_warn(&pdev
->dev
, "more than %u MOXA Intellio family boards "
953 "found. Board is ignored.\n", MAX_BOARDS
);
957 board
= &moxa_boards
[i
];
959 retval
= pci_request_region(pdev
, 2, "moxa-base");
961 dev_err(&pdev
->dev
, "can't request pci region 2\n");
965 board
->basemem
= ioremap_nocache(pci_resource_start(pdev
, 2), 0x4000);
966 if (board
->basemem
== NULL
) {
967 dev_err(&pdev
->dev
, "can't remap io space 2\n");
971 board
->boardType
= board_type
;
972 switch (board_type
) {
973 case MOXA_BOARD_C218_ISA
:
974 case MOXA_BOARD_C218_PCI
:
978 case MOXA_BOARD_CP204J
:
985 board
->busType
= MOXA_BUS_TYPE_PCI
;
987 retval
= moxa_init_board(board
, &pdev
->dev
);
991 pci_set_drvdata(pdev
, board
);
993 dev_info(&pdev
->dev
, "board '%s' ready (%u ports, firmware loaded)\n",
994 moxa_brdname
[board_type
- 1], board
->numPorts
);
998 iounmap(board
->basemem
);
999 board
->basemem
= NULL
;
1001 pci_release_region(pdev
, 2);
1006 static void __devexit
moxa_pci_remove(struct pci_dev
*pdev
)
1008 struct moxa_board_conf
*brd
= pci_get_drvdata(pdev
);
1010 moxa_board_deinit(brd
);
1012 pci_release_region(pdev
, 2);
1015 static struct pci_driver moxa_pci_driver
= {
1017 .id_table
= moxa_pcibrds
,
1018 .probe
= moxa_pci_probe
,
1019 .remove
= __devexit_p(moxa_pci_remove
)
1021 #endif /* CONFIG_PCI */
1023 static int __init
moxa_init(void)
1025 unsigned int isabrds
= 0;
1028 printk(KERN_INFO
"MOXA Intellio family driver version %s\n",
1030 moxaDriver
= alloc_tty_driver(MAX_PORTS
+ 1);
1034 moxaDriver
->owner
= THIS_MODULE
;
1035 moxaDriver
->name
= "ttyMX";
1036 moxaDriver
->major
= ttymajor
;
1037 moxaDriver
->minor_start
= 0;
1038 moxaDriver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1039 moxaDriver
->subtype
= SERIAL_TYPE_NORMAL
;
1040 moxaDriver
->init_termios
= tty_std_termios
;
1041 moxaDriver
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| CLOCAL
| HUPCL
;
1042 moxaDriver
->init_termios
.c_ispeed
= 9600;
1043 moxaDriver
->init_termios
.c_ospeed
= 9600;
1044 moxaDriver
->flags
= TTY_DRIVER_REAL_RAW
;
1045 tty_set_operations(moxaDriver
, &moxa_ops
);
1047 if (tty_register_driver(moxaDriver
)) {
1048 printk(KERN_ERR
"can't register MOXA Smartio tty driver!\n");
1049 put_tty_driver(moxaDriver
);
1053 /* Find the boards defined from module args. */
1056 struct moxa_board_conf
*brd
= moxa_boards
;
1058 for (i
= 0; i
< MAX_BOARDS
; i
++) {
1061 if (type
[i
] == MOXA_BOARD_C218_ISA
||
1062 type
[i
] == MOXA_BOARD_C320_ISA
) {
1063 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
1064 isabrds
+ 1, moxa_brdname
[type
[i
] - 1],
1066 brd
->boardType
= type
[i
];
1067 brd
->numPorts
= type
[i
] == MOXA_BOARD_C218_ISA
? 8 :
1069 brd
->busType
= MOXA_BUS_TYPE_ISA
;
1070 brd
->basemem
= ioremap_nocache(baseaddr
[i
], 0x4000);
1071 if (!brd
->basemem
) {
1072 printk(KERN_ERR
"MOXA: can't remap %lx\n",
1076 if (moxa_init_board(brd
, NULL
)) {
1077 iounmap(brd
->basemem
);
1078 brd
->basemem
= NULL
;
1082 printk(KERN_INFO
"MOXA isa board found at 0x%.8lu and "
1083 "ready (%u ports, firmware loaded)\n",
1084 baseaddr
[i
], brd
->numPorts
);
1094 retval
= pci_register_driver(&moxa_pci_driver
);
1096 printk(KERN_ERR
"Can't register MOXA pci driver!\n");
1105 static void __exit
moxa_exit(void)
1110 pci_unregister_driver(&moxa_pci_driver
);
1113 for (i
= 0; i
< MAX_BOARDS
; i
++) /* ISA boards */
1114 if (moxa_boards
[i
].ready
)
1115 moxa_board_deinit(&moxa_boards
[i
]);
1117 del_timer_sync(&moxaTimer
);
1119 if (tty_unregister_driver(moxaDriver
))
1120 printk(KERN_ERR
"Couldn't unregister MOXA Intellio family "
1122 put_tty_driver(moxaDriver
);
1125 module_init(moxa_init
);
1126 module_exit(moxa_exit
);
1128 static void moxa_shutdown(struct tty_port
*port
)
1130 struct moxa_port
*ch
= container_of(port
, struct moxa_port
, port
);
1131 MoxaPortDisable(ch
);
1132 MoxaPortFlushData(ch
, 2);
1133 clear_bit(ASYNCB_NORMAL_ACTIVE
, &port
->flags
);
1136 static int moxa_carrier_raised(struct tty_port
*port
)
1138 struct moxa_port
*ch
= container_of(port
, struct moxa_port
, port
);
1141 spin_lock_irq(&port
->lock
);
1143 spin_unlock_irq(&port
->lock
);
1147 static void moxa_dtr_rts(struct tty_port
*port
, int onoff
)
1149 struct moxa_port
*ch
= container_of(port
, struct moxa_port
, port
);
1150 MoxaPortLineCtrl(ch
, onoff
, onoff
);
1154 static int moxa_open(struct tty_struct
*tty
, struct file
*filp
)
1156 struct moxa_board_conf
*brd
;
1157 struct moxa_port
*ch
;
1162 if (port
== MAX_PORTS
) {
1163 return capable(CAP_SYS_ADMIN
) ? 0 : -EPERM
;
1165 if (mutex_lock_interruptible(&moxa_openlock
))
1166 return -ERESTARTSYS
;
1167 brd
= &moxa_boards
[port
/ MAX_PORTS_PER_BOARD
];
1169 mutex_unlock(&moxa_openlock
);
1173 if (port
% MAX_PORTS_PER_BOARD
>= brd
->numPorts
) {
1174 mutex_unlock(&moxa_openlock
);
1178 ch
= &brd
->ports
[port
% MAX_PORTS_PER_BOARD
];
1180 tty
->driver_data
= ch
;
1181 tty_port_tty_set(&ch
->port
, tty
);
1182 mutex_lock(&ch
->port
.mutex
);
1183 if (!(ch
->port
.flags
& ASYNC_INITIALIZED
)) {
1184 ch
->statusflags
= 0;
1185 moxa_set_tty_param(tty
, tty
->termios
);
1186 MoxaPortLineCtrl(ch
, 1, 1);
1188 MoxaSetFifo(ch
, ch
->type
== PORT_16550A
);
1189 ch
->port
.flags
|= ASYNC_INITIALIZED
;
1191 mutex_unlock(&ch
->port
.mutex
);
1192 mutex_unlock(&moxa_openlock
);
1194 retval
= tty_port_block_til_ready(&ch
->port
, tty
, filp
);
1196 set_bit(ASYNCB_NORMAL_ACTIVE
, &ch
->port
.flags
);
1200 static void moxa_close(struct tty_struct
*tty
, struct file
*filp
)
1202 struct moxa_port
*ch
= tty
->driver_data
;
1203 ch
->cflag
= tty
->termios
->c_cflag
;
1204 tty_port_close(&ch
->port
, tty
, filp
);
1207 static int moxa_write(struct tty_struct
*tty
,
1208 const unsigned char *buf
, int count
)
1210 struct moxa_port
*ch
= tty
->driver_data
;
1216 spin_lock_bh(&moxa_lock
);
1217 len
= MoxaPortWriteData(tty
, buf
, count
);
1218 spin_unlock_bh(&moxa_lock
);
1220 set_bit(LOWWAIT
, &ch
->statusflags
);
1224 static int moxa_write_room(struct tty_struct
*tty
)
1226 struct moxa_port
*ch
;
1230 ch
= tty
->driver_data
;
1233 return MoxaPortTxFree(ch
);
1236 static void moxa_flush_buffer(struct tty_struct
*tty
)
1238 struct moxa_port
*ch
= tty
->driver_data
;
1242 MoxaPortFlushData(ch
, 1);
1246 static int moxa_chars_in_buffer(struct tty_struct
*tty
)
1248 struct moxa_port
*ch
= tty
->driver_data
;
1251 chars
= MoxaPortTxQueue(ch
);
1254 * Make it possible to wakeup anything waiting for output
1255 * in tty_ioctl.c, etc.
1257 set_bit(EMPTYWAIT
, &ch
->statusflags
);
1261 static int moxa_tiocmget(struct tty_struct
*tty
, struct file
*file
)
1263 struct moxa_port
*ch
= tty
->driver_data
;
1264 int flag
= 0, dtr
, rts
;
1266 MoxaPortGetLineOut(ch
, &dtr
, &rts
);
1271 dtr
= MoxaPortLineStatus(ch
);
1281 static int moxa_tiocmset(struct tty_struct
*tty
, struct file
*file
,
1282 unsigned int set
, unsigned int clear
)
1284 struct moxa_port
*ch
;
1289 mutex_lock(&moxa_openlock
);
1290 ch
= tty
->driver_data
;
1292 mutex_unlock(&moxa_openlock
);
1296 MoxaPortGetLineOut(ch
, &dtr
, &rts
);
1297 if (set
& TIOCM_RTS
)
1299 if (set
& TIOCM_DTR
)
1301 if (clear
& TIOCM_RTS
)
1303 if (clear
& TIOCM_DTR
)
1305 MoxaPortLineCtrl(ch
, dtr
, rts
);
1306 mutex_unlock(&moxa_openlock
);
1310 static void moxa_set_termios(struct tty_struct
*tty
,
1311 struct ktermios
*old_termios
)
1313 struct moxa_port
*ch
= tty
->driver_data
;
1317 moxa_set_tty_param(tty
, old_termios
);
1318 if (!(old_termios
->c_cflag
& CLOCAL
) && C_CLOCAL(tty
))
1319 wake_up_interruptible(&ch
->port
.open_wait
);
1322 static void moxa_stop(struct tty_struct
*tty
)
1324 struct moxa_port
*ch
= tty
->driver_data
;
1328 MoxaPortTxDisable(ch
);
1329 set_bit(TXSTOPPED
, &ch
->statusflags
);
1333 static void moxa_start(struct tty_struct
*tty
)
1335 struct moxa_port
*ch
= tty
->driver_data
;
1340 if (!(ch
->statusflags
& TXSTOPPED
))
1343 MoxaPortTxEnable(ch
);
1344 clear_bit(TXSTOPPED
, &ch
->statusflags
);
1347 static void moxa_hangup(struct tty_struct
*tty
)
1349 struct moxa_port
*ch
= tty
->driver_data
;
1350 tty_port_hangup(&ch
->port
);
1353 static void moxa_new_dcdstate(struct moxa_port
*p
, u8 dcd
)
1355 struct tty_struct
*tty
;
1356 unsigned long flags
;
1359 spin_lock_irqsave(&p
->port
.lock
, flags
);
1360 if (dcd
!= p
->DCDState
) {
1362 spin_unlock_irqrestore(&p
->port
.lock
, flags
);
1363 tty
= tty_port_tty_get(&p
->port
);
1364 if (tty
&& C_CLOCAL(tty
) && !dcd
)
1369 spin_unlock_irqrestore(&p
->port
.lock
, flags
);
1372 static int moxa_poll_port(struct moxa_port
*p
, unsigned int handle
,
1375 struct tty_struct
*tty
= tty_port_tty_get(&p
->port
);
1376 void __iomem
*ofsAddr
;
1377 unsigned int inited
= p
->port
.flags
& ASYNC_INITIALIZED
;
1381 if (test_bit(EMPTYWAIT
, &p
->statusflags
) &&
1382 MoxaPortTxQueue(p
) == 0) {
1383 clear_bit(EMPTYWAIT
, &p
->statusflags
);
1386 if (test_bit(LOWWAIT
, &p
->statusflags
) && !tty
->stopped
&&
1387 MoxaPortTxQueue(p
) <= WAKEUP_CHARS
) {
1388 clear_bit(LOWWAIT
, &p
->statusflags
);
1392 if (inited
&& !test_bit(TTY_THROTTLED
, &tty
->flags
) &&
1393 MoxaPortRxQueue(p
) > 0) { /* RX */
1394 MoxaPortReadData(p
);
1395 tty_schedule_flip(tty
);
1398 clear_bit(EMPTYWAIT
, &p
->statusflags
);
1399 MoxaPortFlushData(p
, 0); /* flush RX */
1402 if (!handle
) /* nothing else to do */
1405 intr
= readw(ip
); /* port irq status */
1409 writew(0, ip
); /* ACK port */
1410 ofsAddr
= p
->tableAddr
;
1411 if (intr
& IntrTx
) /* disable tx intr */
1412 writew(readw(ofsAddr
+ HostStat
) & ~WakeupTx
,
1413 ofsAddr
+ HostStat
);
1418 if (tty
&& (intr
& IntrBreak
) && !I_IGNBRK(tty
)) { /* BREAK */
1419 tty_insert_flip_char(tty
, 0, TTY_BREAK
);
1420 tty_schedule_flip(tty
);
1423 if (intr
& IntrLine
)
1424 moxa_new_dcdstate(p
, readb(ofsAddr
+ FlagStat
) & DCD_state
);
1431 static void moxa_poll(unsigned long ignored
)
1433 struct moxa_board_conf
*brd
;
1435 unsigned int card
, port
, served
= 0;
1437 spin_lock(&moxa_lock
);
1438 for (card
= 0; card
< MAX_BOARDS
; card
++) {
1439 brd
= &moxa_boards
[card
];
1446 if (readb(brd
->intPend
) == 0xff)
1447 ip
= brd
->intTable
+ readb(brd
->intNdx
);
1449 for (port
= 0; port
< brd
->numPorts
; port
++)
1450 moxa_poll_port(&brd
->ports
[port
], !!ip
, ip
+ port
);
1453 writeb(0, brd
->intPend
); /* ACK */
1455 if (moxaLowWaterChk
) {
1456 struct moxa_port
*p
= brd
->ports
;
1457 for (port
= 0; port
< brd
->numPorts
; port
++, p
++)
1458 if (p
->lowChkFlag
) {
1460 moxa_low_water_check(p
->tableAddr
);
1464 moxaLowWaterChk
= 0;
1467 mod_timer(&moxaTimer
, jiffies
+ HZ
/ 50);
1468 spin_unlock(&moxa_lock
);
1471 /******************************************************************************/
1473 static void moxa_set_tty_param(struct tty_struct
*tty
, struct ktermios
*old_termios
)
1475 register struct ktermios
*ts
= tty
->termios
;
1476 struct moxa_port
*ch
= tty
->driver_data
;
1477 int rts
, cts
, txflow
, rxflow
, xany
, baud
;
1479 rts
= cts
= txflow
= rxflow
= xany
= 0;
1480 if (ts
->c_cflag
& CRTSCTS
)
1482 if (ts
->c_iflag
& IXON
)
1484 if (ts
->c_iflag
& IXOFF
)
1486 if (ts
->c_iflag
& IXANY
)
1489 /* Clear the features we don't support */
1490 ts
->c_cflag
&= ~CMSPAR
;
1491 MoxaPortFlowCtrl(ch
, rts
, cts
, txflow
, rxflow
, xany
);
1492 baud
= MoxaPortSetTermio(ch
, ts
, tty_get_baud_rate(tty
));
1494 baud
= tty_termios_baud_rate(old_termios
);
1495 /* Not put the baud rate into the termios data */
1496 tty_encode_baud_rate(tty
, baud
, baud
);
1499 /*****************************************************************************
1500 * Driver level functions: *
1501 *****************************************************************************/
1503 static void MoxaPortFlushData(struct moxa_port
*port
, int mode
)
1505 void __iomem
*ofsAddr
;
1506 if (mode
< 0 || mode
> 2)
1508 ofsAddr
= port
->tableAddr
;
1509 moxafunc(ofsAddr
, FC_FlushQueue
, mode
);
1511 port
->lowChkFlag
= 0;
1512 moxa_low_water_check(ofsAddr
);
1517 * Moxa Port Number Description:
1519 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1520 * the port number using in MOXA driver functions will be 0 to 31 for
1521 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1522 * to 127 for fourth. For example, if you setup three MOXA boards,
1523 * first board is C218, second board is C320-16 and third board is
1524 * C320-32. The port number of first board (C218 - 8 ports) is from
1525 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1526 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1527 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1528 * 127 will be invalid.
1531 * Moxa Functions Description:
1533 * Function 1: Driver initialization routine, this routine must be
1534 * called when initialized driver.
1536 * void MoxaDriverInit();
1539 * Function 2: Moxa driver private IOCTL command processing.
1541 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1543 * unsigned int cmd : IOCTL command
1544 * unsigned long arg : IOCTL argument
1545 * int port : port number (0 - 127)
1552 * Function 6: Enable this port to start Tx/Rx data.
1554 * void MoxaPortEnable(int port);
1555 * int port : port number (0 - 127)
1558 * Function 7: Disable this port
1560 * void MoxaPortDisable(int port);
1561 * int port : port number (0 - 127)
1564 * Function 10: Setting baud rate of this port.
1566 * speed_t MoxaPortSetBaud(int port, speed_t baud);
1567 * int port : port number (0 - 127)
1568 * long baud : baud rate (50 - 115200)
1570 * return: 0 : this port is invalid or baud < 50
1571 * 50 - 115200 : the real baud rate set to the port, if
1572 * the argument baud is large than maximun
1573 * available baud rate, the real setting
1574 * baud rate will be the maximun baud rate.
1577 * Function 12: Configure the port.
1579 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1580 * int port : port number (0 - 127)
1581 * struct ktermios * termio : termio structure pointer
1582 * speed_t baud : baud rate
1584 * return: -1 : this port is invalid or termio == NULL
1588 * Function 13: Get the DTR/RTS state of this port.
1590 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1591 * int port : port number (0 - 127)
1592 * int * dtrState : pointer to INT to receive the current DTR
1593 * state. (if NULL, this function will not
1594 * write to this address)
1595 * int * rtsState : pointer to INT to receive the current RTS
1596 * state. (if NULL, this function will not
1597 * write to this address)
1599 * return: -1 : this port is invalid
1603 * Function 14: Setting the DTR/RTS output state of this port.
1605 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1606 * int port : port number (0 - 127)
1607 * int dtrState : DTR output state (0: off, 1: on)
1608 * int rtsState : RTS output state (0: off, 1: on)
1611 * Function 15: Setting the flow control of this port.
1613 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1614 * int txFlow,int xany);
1615 * int port : port number (0 - 127)
1616 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1617 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1618 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1619 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1620 * int xany : S/W XANY flow control (0: no, 1: yes)
1623 * Function 16: Get ths line status of this port
1625 * int MoxaPortLineStatus(int port);
1626 * int port : port number (0 - 127)
1628 * return: Bit 0 - CTS state (0: off, 1: on)
1629 * Bit 1 - DSR state (0: off, 1: on)
1630 * Bit 2 - DCD state (0: off, 1: on)
1633 * Function 19: Flush the Rx/Tx buffer data of this port.
1635 * void MoxaPortFlushData(int port, int mode);
1636 * int port : port number (0 - 127)
1638 * 0 : flush the Rx buffer
1639 * 1 : flush the Tx buffer
1640 * 2 : flush the Rx and Tx buffer
1643 * Function 20: Write data.
1645 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1646 * int port : port number (0 - 127)
1647 * unsigned char * buffer : pointer to write data buffer.
1648 * int length : write data length
1650 * return: 0 - length : real write data length
1653 * Function 21: Read data.
1655 * int MoxaPortReadData(int port, struct tty_struct *tty);
1656 * int port : port number (0 - 127)
1657 * struct tty_struct *tty : tty for data
1659 * return: 0 - length : real read data length
1662 * Function 24: Get the Tx buffer current queued data bytes
1664 * int MoxaPortTxQueue(int port);
1665 * int port : port number (0 - 127)
1667 * return: .. : Tx buffer current queued data bytes
1670 * Function 25: Get the Tx buffer current free space
1672 * int MoxaPortTxFree(int port);
1673 * int port : port number (0 - 127)
1675 * return: .. : Tx buffer current free space
1678 * Function 26: Get the Rx buffer current queued data bytes
1680 * int MoxaPortRxQueue(int port);
1681 * int port : port number (0 - 127)
1683 * return: .. : Rx buffer current queued data bytes
1686 * Function 28: Disable port data transmission.
1688 * void MoxaPortTxDisable(int port);
1689 * int port : port number (0 - 127)
1692 * Function 29: Enable port data transmission.
1694 * void MoxaPortTxEnable(int port);
1695 * int port : port number (0 - 127)
1698 * Function 31: Get the received BREAK signal count and reset it.
1700 * int MoxaPortResetBrkCnt(int port);
1701 * int port : port number (0 - 127)
1703 * return: 0 - .. : BREAK signal count
1708 static void MoxaPortEnable(struct moxa_port
*port
)
1710 void __iomem
*ofsAddr
;
1713 ofsAddr
= port
->tableAddr
;
1714 writew(lowwater
, ofsAddr
+ Low_water
);
1715 if (MOXA_IS_320(port
->board
))
1716 moxafunc(ofsAddr
, FC_SetBreakIrq
, 0);
1718 writew(readw(ofsAddr
+ HostStat
) | WakeupBreak
,
1719 ofsAddr
+ HostStat
);
1721 moxafunc(ofsAddr
, FC_SetLineIrq
, Magic_code
);
1722 moxafunc(ofsAddr
, FC_FlushQueue
, 2);
1724 moxafunc(ofsAddr
, FC_EnableCH
, Magic_code
);
1725 MoxaPortLineStatus(port
);
1728 static void MoxaPortDisable(struct moxa_port
*port
)
1730 void __iomem
*ofsAddr
= port
->tableAddr
;
1732 moxafunc(ofsAddr
, FC_SetFlowCtl
, 0); /* disable flow control */
1733 moxafunc(ofsAddr
, FC_ClrLineIrq
, Magic_code
);
1734 writew(0, ofsAddr
+ HostStat
);
1735 moxafunc(ofsAddr
, FC_DisableCH
, Magic_code
);
1738 static speed_t
MoxaPortSetBaud(struct moxa_port
*port
, speed_t baud
)
1740 void __iomem
*ofsAddr
= port
->tableAddr
;
1741 unsigned int clock
, val
;
1744 max
= MOXA_IS_320(port
->board
) ? 460800 : 921600;
1751 moxafunc(ofsAddr
, FC_SetBaud
, val
);
1756 static int MoxaPortSetTermio(struct moxa_port
*port
, struct ktermios
*termio
,
1759 void __iomem
*ofsAddr
;
1763 ofsAddr
= port
->tableAddr
;
1764 cflag
= termio
->c_cflag
; /* termio->c_cflag */
1766 mode
= termio
->c_cflag
& CSIZE
;
1769 else if (mode
== CS6
)
1771 else if (mode
== CS7
)
1773 else if (mode
== CS8
)
1776 if (termio
->c_cflag
& CSTOPB
) {
1784 if (termio
->c_cflag
& PARENB
) {
1785 if (termio
->c_cflag
& PARODD
)
1792 moxafunc(ofsAddr
, FC_SetDataMode
, (u16
)mode
);
1794 if (MOXA_IS_320(port
->board
) && baud
>= 921600)
1797 baud
= MoxaPortSetBaud(port
, baud
);
1799 if (termio
->c_iflag
& (IXON
| IXOFF
| IXANY
)) {
1800 spin_lock_irq(&moxafunc_lock
);
1801 writeb(termio
->c_cc
[VSTART
], ofsAddr
+ FuncArg
);
1802 writeb(termio
->c_cc
[VSTOP
], ofsAddr
+ FuncArg1
);
1803 writeb(FC_SetXonXoff
, ofsAddr
+ FuncCode
);
1804 moxa_wait_finish(ofsAddr
);
1805 spin_unlock_irq(&moxafunc_lock
);
1811 static int MoxaPortGetLineOut(struct moxa_port
*port
, int *dtrState
,
1815 *dtrState
= !!(port
->lineCtrl
& DTR_ON
);
1817 *rtsState
= !!(port
->lineCtrl
& RTS_ON
);
1822 static void MoxaPortLineCtrl(struct moxa_port
*port
, int dtr
, int rts
)
1830 port
->lineCtrl
= mode
;
1831 moxafunc(port
->tableAddr
, FC_LineControl
, mode
);
1834 static void MoxaPortFlowCtrl(struct moxa_port
*port
, int rts
, int cts
,
1835 int txflow
, int rxflow
, int txany
)
1840 mode
|= RTS_FlowCtl
;
1842 mode
|= CTS_FlowCtl
;
1849 moxafunc(port
->tableAddr
, FC_SetFlowCtl
, mode
);
1852 static int MoxaPortLineStatus(struct moxa_port
*port
)
1854 void __iomem
*ofsAddr
;
1857 ofsAddr
= port
->tableAddr
;
1858 if (MOXA_IS_320(port
->board
))
1859 val
= moxafuncret(ofsAddr
, FC_LineStatus
, 0);
1861 val
= readw(ofsAddr
+ FlagStat
) >> 4;
1865 moxa_new_dcdstate(port
, val
& 8);
1870 static int MoxaPortWriteData(struct tty_struct
*tty
,
1871 const unsigned char *buffer
, int len
)
1873 struct moxa_port
*port
= tty
->driver_data
;
1874 void __iomem
*baseAddr
, *ofsAddr
, *ofs
;
1875 unsigned int c
, total
;
1876 u16 head
, tail
, tx_mask
, spage
, epage
;
1877 u16 pageno
, pageofs
, bufhead
;
1879 ofsAddr
= port
->tableAddr
;
1880 baseAddr
= port
->board
->basemem
;
1881 tx_mask
= readw(ofsAddr
+ TX_mask
);
1882 spage
= readw(ofsAddr
+ Page_txb
);
1883 epage
= readw(ofsAddr
+ EndPage_txb
);
1884 tail
= readw(ofsAddr
+ TXwptr
);
1885 head
= readw(ofsAddr
+ TXrptr
);
1886 c
= (head
> tail
) ? (head
- tail
- 1) : (head
- tail
+ tx_mask
);
1889 moxaLog
.txcnt
[port
->port
.tty
->index
] += c
;
1891 if (spage
== epage
) {
1892 bufhead
= readw(ofsAddr
+ Ofs_txb
);
1893 writew(spage
, baseAddr
+ Control_reg
);
1896 len
= head
- tail
- 1;
1898 len
= tx_mask
+ 1 - tail
;
1899 len
= (c
> len
) ? len
: c
;
1900 ofs
= baseAddr
+ DynPage_addr
+ bufhead
+ tail
;
1901 memcpy_toio(ofs
, buffer
, len
);
1903 tail
= (tail
+ len
) & tx_mask
;
1907 pageno
= spage
+ (tail
>> 13);
1908 pageofs
= tail
& Page_mask
;
1910 len
= Page_size
- pageofs
;
1913 writeb(pageno
, baseAddr
+ Control_reg
);
1914 ofs
= baseAddr
+ DynPage_addr
+ pageofs
;
1915 memcpy_toio(ofs
, buffer
, len
);
1917 if (++pageno
== epage
)
1922 tail
= (tail
+ total
) & tx_mask
;
1924 writew(tail
, ofsAddr
+ TXwptr
);
1925 writeb(1, ofsAddr
+ CD180TXirq
); /* start to send */
1929 static int MoxaPortReadData(struct moxa_port
*port
)
1931 struct tty_struct
*tty
= port
->port
.tty
;
1933 void __iomem
*baseAddr
, *ofsAddr
, *ofs
;
1934 unsigned int count
, len
, total
;
1935 u16 tail
, rx_mask
, spage
, epage
;
1936 u16 pageno
, pageofs
, bufhead
, head
;
1938 ofsAddr
= port
->tableAddr
;
1939 baseAddr
= port
->board
->basemem
;
1940 head
= readw(ofsAddr
+ RXrptr
);
1941 tail
= readw(ofsAddr
+ RXwptr
);
1942 rx_mask
= readw(ofsAddr
+ RX_mask
);
1943 spage
= readw(ofsAddr
+ Page_rxb
);
1944 epage
= readw(ofsAddr
+ EndPage_rxb
);
1945 count
= (tail
>= head
) ? (tail
- head
) : (tail
- head
+ rx_mask
+ 1);
1950 moxaLog
.rxcnt
[tty
->index
] += total
;
1951 if (spage
== epage
) {
1952 bufhead
= readw(ofsAddr
+ Ofs_rxb
);
1953 writew(spage
, baseAddr
+ Control_reg
);
1955 ofs
= baseAddr
+ DynPage_addr
+ bufhead
+ head
;
1956 len
= (tail
>= head
) ? (tail
- head
) :
1957 (rx_mask
+ 1 - head
);
1958 len
= tty_prepare_flip_string(tty
, &dst
,
1960 memcpy_fromio(dst
, ofs
, len
);
1961 head
= (head
+ len
) & rx_mask
;
1965 pageno
= spage
+ (head
>> 13);
1966 pageofs
= head
& Page_mask
;
1968 writew(pageno
, baseAddr
+ Control_reg
);
1969 ofs
= baseAddr
+ DynPage_addr
+ pageofs
;
1970 len
= tty_prepare_flip_string(tty
, &dst
,
1971 min(Page_size
- pageofs
, count
));
1972 memcpy_fromio(dst
, ofs
, len
);
1975 pageofs
= (pageofs
+ len
) & Page_mask
;
1976 if (pageofs
== 0 && ++pageno
== epage
)
1979 head
= (head
+ total
) & rx_mask
;
1981 writew(head
, ofsAddr
+ RXrptr
);
1982 if (readb(ofsAddr
+ FlagStat
) & Xoff_state
) {
1983 moxaLowWaterChk
= 1;
1984 port
->lowChkFlag
= 1;
1990 static int MoxaPortTxQueue(struct moxa_port
*port
)
1992 void __iomem
*ofsAddr
= port
->tableAddr
;
1993 u16 rptr
, wptr
, mask
;
1995 rptr
= readw(ofsAddr
+ TXrptr
);
1996 wptr
= readw(ofsAddr
+ TXwptr
);
1997 mask
= readw(ofsAddr
+ TX_mask
);
1998 return (wptr
- rptr
) & mask
;
2001 static int MoxaPortTxFree(struct moxa_port
*port
)
2003 void __iomem
*ofsAddr
= port
->tableAddr
;
2004 u16 rptr
, wptr
, mask
;
2006 rptr
= readw(ofsAddr
+ TXrptr
);
2007 wptr
= readw(ofsAddr
+ TXwptr
);
2008 mask
= readw(ofsAddr
+ TX_mask
);
2009 return mask
- ((wptr
- rptr
) & mask
);
2012 static int MoxaPortRxQueue(struct moxa_port
*port
)
2014 void __iomem
*ofsAddr
= port
->tableAddr
;
2015 u16 rptr
, wptr
, mask
;
2017 rptr
= readw(ofsAddr
+ RXrptr
);
2018 wptr
= readw(ofsAddr
+ RXwptr
);
2019 mask
= readw(ofsAddr
+ RX_mask
);
2020 return (wptr
- rptr
) & mask
;
2023 static void MoxaPortTxDisable(struct moxa_port
*port
)
2025 moxafunc(port
->tableAddr
, FC_SetXoffState
, Magic_code
);
2028 static void MoxaPortTxEnable(struct moxa_port
*port
)
2030 moxafunc(port
->tableAddr
, FC_SetXonState
, Magic_code
);
2033 static int moxa_get_serial_info(struct moxa_port
*info
,
2034 struct serial_struct __user
*retinfo
)
2036 struct serial_struct tmp
= {
2038 .line
= info
->port
.tty
->index
,
2039 .flags
= info
->port
.flags
,
2040 .baud_base
= 921600,
2041 .close_delay
= info
->port
.close_delay
2043 return copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)) ? -EFAULT
: 0;
2047 static int moxa_set_serial_info(struct moxa_port
*info
,
2048 struct serial_struct __user
*new_info
)
2050 struct serial_struct new_serial
;
2052 if (copy_from_user(&new_serial
, new_info
, sizeof(new_serial
)))
2055 if (new_serial
.irq
!= 0 || new_serial
.port
!= 0 ||
2056 new_serial
.custom_divisor
!= 0 ||
2057 new_serial
.baud_base
!= 921600)
2060 if (!capable(CAP_SYS_ADMIN
)) {
2061 if (((new_serial
.flags
& ~ASYNC_USR_MASK
) !=
2062 (info
->port
.flags
& ~ASYNC_USR_MASK
)))
2065 info
->port
.close_delay
= new_serial
.close_delay
* HZ
/ 100;
2067 new_serial
.flags
= (new_serial
.flags
& ~ASYNC_FLAGS
);
2068 new_serial
.flags
|= (info
->port
.flags
& ASYNC_FLAGS
);
2070 MoxaSetFifo(info
, new_serial
.type
== PORT_16550A
);
2072 info
->type
= new_serial
.type
;
2078 /*****************************************************************************
2079 * Static local functions: *
2080 *****************************************************************************/
2082 static void MoxaSetFifo(struct moxa_port
*port
, int enable
)
2084 void __iomem
*ofsAddr
= port
->tableAddr
;
2087 moxafunc(ofsAddr
, FC_SetRxFIFOTrig
, 0);
2088 moxafunc(ofsAddr
, FC_SetTxFIFOCnt
, 1);
2090 moxafunc(ofsAddr
, FC_SetRxFIFOTrig
, 3);
2091 moxafunc(ofsAddr
, FC_SetTxFIFOCnt
, 16);