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 moxa_port
*);
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 moxa_port
*, 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 memset(&tmp
, 0, sizeof(tmp
));
336 if (!moxa_boards
[i
].ready
)
339 status
= MoxaPortLineStatus(p
);
347 if (!p
->port
.tty
|| !p
->port
.tty
->termios
)
348 tmp
.cflag
= p
->cflag
;
350 tmp
.cflag
= p
->port
.tty
->termios
->c_cflag
;
352 if (copy_to_user(argm
, &tmp
, sizeof(tmp
))) {
353 mutex_unlock(&moxa_openlock
);
358 mutex_unlock(&moxa_openlock
);
362 mutex_lock(&moxa_openlock
);
363 ret
= moxa_get_serial_info(ch
, argp
);
364 mutex_unlock(&moxa_openlock
);
367 mutex_lock(&moxa_openlock
);
368 ret
= moxa_set_serial_info(ch
, argp
);
369 mutex_unlock(&moxa_openlock
);
377 static void moxa_break_ctl(struct tty_struct
*tty
, int state
)
379 struct moxa_port
*port
= tty
->driver_data
;
381 moxafunc(port
->tableAddr
, state
? FC_SendBreak
: FC_StopBreak
,
385 static const struct tty_operations moxa_ops
= {
389 .write_room
= moxa_write_room
,
390 .flush_buffer
= moxa_flush_buffer
,
391 .chars_in_buffer
= moxa_chars_in_buffer
,
393 .throttle
= moxa_throttle
,
394 .unthrottle
= moxa_unthrottle
,
395 .set_termios
= moxa_set_termios
,
398 .hangup
= moxa_hangup
,
399 .break_ctl
= moxa_break_ctl
,
400 .tiocmget
= moxa_tiocmget
,
401 .tiocmset
= moxa_tiocmset
,
404 static struct tty_driver
*moxaDriver
;
405 static DEFINE_TIMER(moxaTimer
, moxa_poll
, 0, 0);
406 static DEFINE_SPINLOCK(moxa_lock
);
412 static int moxa_check_fw_model(struct moxa_board_conf
*brd
, u8 model
)
414 switch (brd
->boardType
) {
415 case MOXA_BOARD_C218_ISA
:
416 case MOXA_BOARD_C218_PCI
:
420 case MOXA_BOARD_CP204J
:
434 static int moxa_check_fw(const void *ptr
)
436 const __le16
*lptr
= ptr
;
438 if (*lptr
!= cpu_to_le16(0x7980))
444 static int moxa_load_bios(struct moxa_board_conf
*brd
, const u8
*buf
,
447 void __iomem
*baseAddr
= brd
->basemem
;
450 writeb(HW_reset
, baseAddr
+ Control_reg
); /* reset */
452 memset_io(baseAddr
, 0, 4096);
453 memcpy_toio(baseAddr
, buf
, len
); /* download BIOS */
454 writeb(0, baseAddr
+ Control_reg
); /* restart */
458 switch (brd
->boardType
) {
459 case MOXA_BOARD_C218_ISA
:
460 case MOXA_BOARD_C218_PCI
:
461 tmp
= readw(baseAddr
+ C218_key
);
462 if (tmp
!= C218_KeyCode
)
465 case MOXA_BOARD_CP204J
:
466 tmp
= readw(baseAddr
+ C218_key
);
467 if (tmp
!= CP204J_KeyCode
)
471 tmp
= readw(baseAddr
+ C320_key
);
472 if (tmp
!= C320_KeyCode
)
474 tmp
= readw(baseAddr
+ C320_status
);
475 if (tmp
!= STS_init
) {
476 printk(KERN_ERR
"MOXA: bios upload failed -- CPU/Basic "
477 "module not found\n");
485 printk(KERN_ERR
"MOXA: bios upload failed -- board not found\n");
489 static int moxa_load_320b(struct moxa_board_conf
*brd
, const u8
*ptr
,
492 void __iomem
*baseAddr
= brd
->basemem
;
495 printk(KERN_ERR
"MOXA: invalid 320 bios -- too short\n");
499 writew(len
- 7168 - 2, baseAddr
+ C320bapi_len
);
500 writeb(1, baseAddr
+ Control_reg
); /* Select Page 1 */
501 memcpy_toio(baseAddr
+ DynPage_addr
, ptr
, 7168);
502 writeb(2, baseAddr
+ Control_reg
); /* Select Page 2 */
503 memcpy_toio(baseAddr
+ DynPage_addr
, ptr
+ 7168, len
- 7168);
508 static int moxa_real_load_code(struct moxa_board_conf
*brd
, const void *ptr
,
511 void __iomem
*baseAddr
= brd
->basemem
;
512 const u16
*uptr
= ptr
;
513 size_t wlen
, len2
, j
;
514 unsigned long key
, loadbuf
, loadlen
, checksum
, checksum_ok
;
515 unsigned int i
, retry
;
518 keycode
= (brd
->boardType
== MOXA_BOARD_CP204J
) ? CP204J_KeyCode
:
521 switch (brd
->boardType
) {
522 case MOXA_BOARD_CP204J
:
523 case MOXA_BOARD_C218_ISA
:
524 case MOXA_BOARD_C218_PCI
:
526 loadbuf
= C218_LoadBuf
;
527 loadlen
= C218DLoad_len
;
528 checksum
= C218check_sum
;
529 checksum_ok
= C218chksum_ok
;
533 keycode
= C320_KeyCode
;
534 loadbuf
= C320_LoadBuf
;
535 loadlen
= C320DLoad_len
;
536 checksum
= C320check_sum
;
537 checksum_ok
= C320chksum_ok
;
543 for (i
= 0; i
< wlen
; i
++)
544 usum
+= le16_to_cpu(uptr
[i
]);
550 len2
= (wlen
> 2048) ? 2048 : wlen
;
552 memcpy_toio(baseAddr
+ loadbuf
, ptr
+ j
, len2
<< 1);
555 writew(len2
, baseAddr
+ loadlen
);
556 writew(0, baseAddr
+ key
);
557 for (i
= 0; i
< 100; i
++) {
558 if (readw(baseAddr
+ key
) == keycode
)
562 if (readw(baseAddr
+ key
) != keycode
)
565 writew(0, baseAddr
+ loadlen
);
566 writew(usum
, baseAddr
+ checksum
);
567 writew(0, baseAddr
+ key
);
568 for (i
= 0; i
< 100; i
++) {
569 if (readw(baseAddr
+ key
) == keycode
)
574 } while ((readb(baseAddr
+ checksum_ok
) != 1) && (retry
< 3));
575 if (readb(baseAddr
+ checksum_ok
) != 1)
578 writew(0, baseAddr
+ key
);
579 for (i
= 0; i
< 600; i
++) {
580 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
584 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
587 if (MOXA_IS_320(brd
)) {
588 if (brd
->busType
== MOXA_BUS_TYPE_PCI
) { /* ASIC board */
589 writew(0x3800, baseAddr
+ TMS320_PORT1
);
590 writew(0x3900, baseAddr
+ TMS320_PORT2
);
591 writew(28499, baseAddr
+ TMS320_CLOCK
);
593 writew(0x3200, baseAddr
+ TMS320_PORT1
);
594 writew(0x3400, baseAddr
+ TMS320_PORT2
);
595 writew(19999, baseAddr
+ TMS320_CLOCK
);
598 writew(1, baseAddr
+ Disable_IRQ
);
599 writew(0, baseAddr
+ Magic_no
);
600 for (i
= 0; i
< 500; i
++) {
601 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
605 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
608 if (MOXA_IS_320(brd
)) {
609 j
= readw(baseAddr
+ Module_cnt
);
612 brd
->numPorts
= j
* 8;
613 writew(j
, baseAddr
+ Module_no
);
614 writew(0, baseAddr
+ Magic_no
);
615 for (i
= 0; i
< 600; i
++) {
616 if (readw(baseAddr
+ Magic_no
) == Magic_code
)
620 if (readw(baseAddr
+ Magic_no
) != Magic_code
)
623 brd
->intNdx
= baseAddr
+ IRQindex
;
624 brd
->intPend
= baseAddr
+ IRQpending
;
625 brd
->intTable
= baseAddr
+ IRQtable
;
630 static int moxa_load_code(struct moxa_board_conf
*brd
, const void *ptr
,
633 void __iomem
*ofsAddr
, *baseAddr
= brd
->basemem
;
634 struct moxa_port
*port
;
638 printk(KERN_ERR
"MOXA: bios length is not even\n");
642 retval
= moxa_real_load_code(brd
, ptr
, len
); /* may change numPorts */
646 switch (brd
->boardType
) {
647 case MOXA_BOARD_C218_ISA
:
648 case MOXA_BOARD_C218_PCI
:
649 case MOXA_BOARD_CP204J
:
651 for (i
= 0; i
< brd
->numPorts
; i
++, port
++) {
654 port
->tableAddr
= baseAddr
+ Extern_table
+
656 ofsAddr
= port
->tableAddr
;
657 writew(C218rx_mask
, ofsAddr
+ RX_mask
);
658 writew(C218tx_mask
, ofsAddr
+ TX_mask
);
659 writew(C218rx_spage
+ i
* C218buf_pageno
, ofsAddr
+ Page_rxb
);
660 writew(readw(ofsAddr
+ Page_rxb
) + C218rx_pageno
, ofsAddr
+ EndPage_rxb
);
662 writew(C218tx_spage
+ i
* C218buf_pageno
, ofsAddr
+ Page_txb
);
663 writew(readw(ofsAddr
+ Page_txb
) + C218tx_pageno
, ofsAddr
+ EndPage_txb
);
669 for (i
= 0; i
< brd
->numPorts
; i
++, port
++) {
672 port
->tableAddr
= baseAddr
+ Extern_table
+
674 ofsAddr
= port
->tableAddr
;
675 switch (brd
->numPorts
) {
677 writew(C320p8rx_mask
, ofsAddr
+ RX_mask
);
678 writew(C320p8tx_mask
, ofsAddr
+ TX_mask
);
679 writew(C320p8rx_spage
+ i
* C320p8buf_pgno
, ofsAddr
+ Page_rxb
);
680 writew(readw(ofsAddr
+ Page_rxb
) + C320p8rx_pgno
, ofsAddr
+ EndPage_rxb
);
681 writew(C320p8tx_spage
+ i
* C320p8buf_pgno
, ofsAddr
+ Page_txb
);
682 writew(readw(ofsAddr
+ Page_txb
) + C320p8tx_pgno
, ofsAddr
+ EndPage_txb
);
686 writew(C320p16rx_mask
, ofsAddr
+ RX_mask
);
687 writew(C320p16tx_mask
, ofsAddr
+ TX_mask
);
688 writew(C320p16rx_spage
+ i
* C320p16buf_pgno
, ofsAddr
+ Page_rxb
);
689 writew(readw(ofsAddr
+ Page_rxb
) + C320p16rx_pgno
, ofsAddr
+ EndPage_rxb
);
690 writew(C320p16tx_spage
+ i
* C320p16buf_pgno
, ofsAddr
+ Page_txb
);
691 writew(readw(ofsAddr
+ Page_txb
) + C320p16tx_pgno
, ofsAddr
+ EndPage_txb
);
695 writew(C320p24rx_mask
, ofsAddr
+ RX_mask
);
696 writew(C320p24tx_mask
, ofsAddr
+ TX_mask
);
697 writew(C320p24rx_spage
+ i
* C320p24buf_pgno
, ofsAddr
+ Page_rxb
);
698 writew(readw(ofsAddr
+ Page_rxb
) + C320p24rx_pgno
, ofsAddr
+ EndPage_rxb
);
699 writew(C320p24tx_spage
+ i
* C320p24buf_pgno
, ofsAddr
+ Page_txb
);
700 writew(readw(ofsAddr
+ Page_txb
), ofsAddr
+ EndPage_txb
);
703 writew(C320p32rx_mask
, ofsAddr
+ RX_mask
);
704 writew(C320p32tx_mask
, ofsAddr
+ TX_mask
);
705 writew(C320p32tx_ofs
, ofsAddr
+ Ofs_txb
);
706 writew(C320p32rx_spage
+ i
* C320p32buf_pgno
, ofsAddr
+ Page_rxb
);
707 writew(readb(ofsAddr
+ Page_rxb
), ofsAddr
+ EndPage_rxb
);
708 writew(C320p32tx_spage
+ i
* C320p32buf_pgno
, ofsAddr
+ Page_txb
);
709 writew(readw(ofsAddr
+ Page_txb
), ofsAddr
+ EndPage_txb
);
718 static int moxa_load_fw(struct moxa_board_conf
*brd
, const struct firmware
*fw
)
720 const void *ptr
= fw
->data
;
724 unsigned int a
, lenp
, lencnt
;
727 __le32 magic
; /* 0x34303430 */
729 u8 type
; /* UNIX = 3 */
730 u8 model
; /* C218T=1, C320T=2, CP204=3 */
735 BUILD_BUG_ON(ARRAY_SIZE(hdr
->len
) != ARRAY_SIZE(lens
));
737 if (fw
->size
< MOXA_FW_HDRLEN
) {
738 strcpy(rsn
, "too short (even header won't fit)");
741 if (hdr
->magic
!= cpu_to_le32(0x30343034)) {
742 sprintf(rsn
, "bad magic: %.8x", le32_to_cpu(hdr
->magic
));
745 if (hdr
->type
!= 3) {
746 sprintf(rsn
, "not for linux, type is %u", hdr
->type
);
749 if (moxa_check_fw_model(brd
, hdr
->model
)) {
750 sprintf(rsn
, "not for this card, model is %u", hdr
->model
);
754 len
= MOXA_FW_HDRLEN
;
755 lencnt
= hdr
->model
== 2 ? 5 : 3;
756 for (a
= 0; a
< ARRAY_SIZE(lens
); a
++) {
757 lens
[a
] = le16_to_cpu(hdr
->len
[a
]);
758 if (lens
[a
] && len
+ lens
[a
] <= fw
->size
&&
759 moxa_check_fw(&fw
->data
[len
]))
760 printk(KERN_WARNING
"MOXA firmware: unexpected input "
761 "at offset %u, but going on\n", (u32
)len
);
762 if (!lens
[a
] && a
< lencnt
) {
763 sprintf(rsn
, "too few entries in fw file");
769 if (len
!= fw
->size
) {
770 sprintf(rsn
, "bad length: %u (should be %u)", (u32
)fw
->size
,
775 ptr
+= MOXA_FW_HDRLEN
;
778 strcpy(rsn
, "read above");
780 ret
= moxa_load_bios(brd
, ptr
, lens
[lenp
]);
784 /* we skip the tty section (lens[1]), since we don't need it */
785 ptr
+= lens
[lenp
] + lens
[lenp
+ 1];
786 lenp
+= 2; /* comm */
788 if (hdr
->model
== 2) {
789 ret
= moxa_load_320b(brd
, ptr
, lens
[lenp
]);
792 /* skip another tty */
793 ptr
+= lens
[lenp
] + lens
[lenp
+ 1];
797 ret
= moxa_load_code(brd
, ptr
, lens
[lenp
]);
803 printk(KERN_ERR
"firmware failed to load, reason: %s\n", rsn
);
807 static int moxa_init_board(struct moxa_board_conf
*brd
, struct device
*dev
)
809 const struct firmware
*fw
;
815 brd
->ports
= kcalloc(MAX_PORTS_PER_BOARD
, sizeof(*brd
->ports
),
817 if (brd
->ports
== NULL
) {
818 printk(KERN_ERR
"cannot allocate memory for ports\n");
823 for (i
= 0, p
= brd
->ports
; i
< MAX_PORTS_PER_BOARD
; i
++, p
++) {
824 tty_port_init(&p
->port
);
825 p
->type
= PORT_16550A
;
826 p
->cflag
= B9600
| CS8
| CREAD
| CLOCAL
| HUPCL
;
829 switch (brd
->boardType
) {
830 case MOXA_BOARD_C218_ISA
:
831 case MOXA_BOARD_C218_PCI
:
832 file
= "c218tunx.cod";
834 case MOXA_BOARD_CP204J
:
835 file
= "cp204unx.cod";
838 file
= "c320tunx.cod";
842 ret
= request_firmware(&fw
, file
, dev
);
844 printk(KERN_ERR
"MOXA: request_firmware failed. Make sure "
845 "you've placed '%s' file into your firmware "
846 "loader directory (e.g. /lib/firmware)\n",
851 ret
= moxa_load_fw(brd
, fw
);
853 release_firmware(fw
);
858 spin_lock_bh(&moxa_lock
);
860 if (!timer_pending(&moxaTimer
))
861 mod_timer(&moxaTimer
, jiffies
+ HZ
/ 50);
862 spin_unlock_bh(&moxa_lock
);
871 static void moxa_board_deinit(struct moxa_board_conf
*brd
)
873 unsigned int a
, opened
;
875 mutex_lock(&moxa_openlock
);
876 spin_lock_bh(&moxa_lock
);
878 spin_unlock_bh(&moxa_lock
);
880 /* pci hot-un-plug support */
881 for (a
= 0; a
< brd
->numPorts
; a
++)
882 if (brd
->ports
[a
].port
.flags
& ASYNC_INITIALIZED
)
883 tty_hangup(brd
->ports
[a
].port
.tty
);
886 for (a
= 0; a
< brd
->numPorts
; a
++)
887 if (brd
->ports
[a
].port
.flags
& ASYNC_INITIALIZED
)
889 mutex_unlock(&moxa_openlock
);
893 mutex_lock(&moxa_openlock
);
896 iounmap(brd
->basemem
);
902 static int __devinit
moxa_pci_probe(struct pci_dev
*pdev
,
903 const struct pci_device_id
*ent
)
905 struct moxa_board_conf
*board
;
907 int board_type
= ent
->driver_data
;
910 retval
= pci_enable_device(pdev
);
912 dev_err(&pdev
->dev
, "can't enable pci device\n");
916 for (i
= 0; i
< MAX_BOARDS
; i
++)
917 if (moxa_boards
[i
].basemem
== NULL
)
921 if (i
>= MAX_BOARDS
) {
922 dev_warn(&pdev
->dev
, "more than %u MOXA Intellio family boards "
923 "found. Board is ignored.\n", MAX_BOARDS
);
927 board
= &moxa_boards
[i
];
929 retval
= pci_request_region(pdev
, 2, "moxa-base");
931 dev_err(&pdev
->dev
, "can't request pci region 2\n");
935 board
->basemem
= ioremap_nocache(pci_resource_start(pdev
, 2), 0x4000);
936 if (board
->basemem
== NULL
) {
937 dev_err(&pdev
->dev
, "can't remap io space 2\n");
941 board
->boardType
= board_type
;
942 switch (board_type
) {
943 case MOXA_BOARD_C218_ISA
:
944 case MOXA_BOARD_C218_PCI
:
948 case MOXA_BOARD_CP204J
:
955 board
->busType
= MOXA_BUS_TYPE_PCI
;
957 retval
= moxa_init_board(board
, &pdev
->dev
);
961 pci_set_drvdata(pdev
, board
);
963 dev_info(&pdev
->dev
, "board '%s' ready (%u ports, firmware loaded)\n",
964 moxa_brdname
[board_type
- 1], board
->numPorts
);
968 iounmap(board
->basemem
);
969 board
->basemem
= NULL
;
971 pci_release_region(pdev
, 2);
976 static void __devexit
moxa_pci_remove(struct pci_dev
*pdev
)
978 struct moxa_board_conf
*brd
= pci_get_drvdata(pdev
);
980 moxa_board_deinit(brd
);
982 pci_release_region(pdev
, 2);
985 static struct pci_driver moxa_pci_driver
= {
987 .id_table
= moxa_pcibrds
,
988 .probe
= moxa_pci_probe
,
989 .remove
= __devexit_p(moxa_pci_remove
)
991 #endif /* CONFIG_PCI */
993 static int __init
moxa_init(void)
995 unsigned int isabrds
= 0;
998 printk(KERN_INFO
"MOXA Intellio family driver version %s\n",
1000 moxaDriver
= alloc_tty_driver(MAX_PORTS
+ 1);
1004 moxaDriver
->owner
= THIS_MODULE
;
1005 moxaDriver
->name
= "ttyMX";
1006 moxaDriver
->major
= ttymajor
;
1007 moxaDriver
->minor_start
= 0;
1008 moxaDriver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1009 moxaDriver
->subtype
= SERIAL_TYPE_NORMAL
;
1010 moxaDriver
->init_termios
= tty_std_termios
;
1011 moxaDriver
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| CLOCAL
| HUPCL
;
1012 moxaDriver
->init_termios
.c_ispeed
= 9600;
1013 moxaDriver
->init_termios
.c_ospeed
= 9600;
1014 moxaDriver
->flags
= TTY_DRIVER_REAL_RAW
;
1015 tty_set_operations(moxaDriver
, &moxa_ops
);
1017 if (tty_register_driver(moxaDriver
)) {
1018 printk(KERN_ERR
"can't register MOXA Smartio tty driver!\n");
1019 put_tty_driver(moxaDriver
);
1023 /* Find the boards defined from module args. */
1026 struct moxa_board_conf
*brd
= moxa_boards
;
1028 for (i
= 0; i
< MAX_BOARDS
; i
++) {
1031 if (type
[i
] == MOXA_BOARD_C218_ISA
||
1032 type
[i
] == MOXA_BOARD_C320_ISA
) {
1033 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
1034 isabrds
+ 1, moxa_brdname
[type
[i
] - 1],
1036 brd
->boardType
= type
[i
];
1037 brd
->numPorts
= type
[i
] == MOXA_BOARD_C218_ISA
? 8 :
1039 brd
->busType
= MOXA_BUS_TYPE_ISA
;
1040 brd
->basemem
= ioremap_nocache(baseaddr
[i
], 0x4000);
1041 if (!brd
->basemem
) {
1042 printk(KERN_ERR
"MOXA: can't remap %lx\n",
1046 if (moxa_init_board(brd
, NULL
)) {
1047 iounmap(brd
->basemem
);
1048 brd
->basemem
= NULL
;
1052 printk(KERN_INFO
"MOXA isa board found at 0x%.8lu and "
1053 "ready (%u ports, firmware loaded)\n",
1054 baseaddr
[i
], brd
->numPorts
);
1064 retval
= pci_register_driver(&moxa_pci_driver
);
1066 printk(KERN_ERR
"Can't register MOXA pci driver!\n");
1075 static void __exit
moxa_exit(void)
1080 pci_unregister_driver(&moxa_pci_driver
);
1083 for (i
= 0; i
< MAX_BOARDS
; i
++) /* ISA boards */
1084 if (moxa_boards
[i
].ready
)
1085 moxa_board_deinit(&moxa_boards
[i
]);
1087 del_timer_sync(&moxaTimer
);
1089 if (tty_unregister_driver(moxaDriver
))
1090 printk(KERN_ERR
"Couldn't unregister MOXA Intellio family "
1092 put_tty_driver(moxaDriver
);
1095 module_init(moxa_init
);
1096 module_exit(moxa_exit
);
1098 static void moxa_close_port(struct moxa_port
*ch
)
1101 MoxaPortFlushData(ch
, 2);
1102 ch
->port
.flags
&= ~ASYNC_NORMAL_ACTIVE
;
1103 ch
->port
.tty
->driver_data
= NULL
;
1104 ch
->port
.tty
= NULL
;
1107 static int moxa_block_till_ready(struct tty_struct
*tty
, struct file
*filp
,
1108 struct moxa_port
*ch
)
1115 prepare_to_wait(&ch
->port
.open_wait
, &wait
, TASK_INTERRUPTIBLE
);
1116 if (tty_hung_up_p(filp
)) {
1117 #ifdef SERIAL_DO_RESTART
1118 retval
= -ERESTARTSYS
;
1124 spin_lock_bh(&moxa_lock
);
1126 spin_unlock_bh(&moxa_lock
);
1130 if (signal_pending(current
)) {
1131 retval
= -ERESTARTSYS
;
1136 finish_wait(&ch
->port
.open_wait
, &wait
);
1141 static int moxa_open(struct tty_struct
*tty
, struct file
*filp
)
1143 struct moxa_board_conf
*brd
;
1144 struct moxa_port
*ch
;
1149 if (port
== MAX_PORTS
) {
1150 return capable(CAP_SYS_ADMIN
) ? 0 : -EPERM
;
1152 if (mutex_lock_interruptible(&moxa_openlock
))
1153 return -ERESTARTSYS
;
1154 brd
= &moxa_boards
[port
/ MAX_PORTS_PER_BOARD
];
1156 mutex_unlock(&moxa_openlock
);
1160 ch
= &brd
->ports
[port
% MAX_PORTS_PER_BOARD
];
1162 tty
->driver_data
= ch
;
1164 if (!(ch
->port
.flags
& ASYNC_INITIALIZED
)) {
1165 ch
->statusflags
= 0;
1166 moxa_set_tty_param(tty
, tty
->termios
);
1167 MoxaPortLineCtrl(ch
, 1, 1);
1169 MoxaSetFifo(ch
, ch
->type
== PORT_16550A
);
1170 ch
->port
.flags
|= ASYNC_INITIALIZED
;
1172 mutex_unlock(&moxa_openlock
);
1175 if (!(filp
->f_flags
& O_NONBLOCK
) && !C_CLOCAL(tty
))
1176 retval
= moxa_block_till_ready(tty
, filp
, ch
);
1177 mutex_lock(&moxa_openlock
);
1179 if (ch
->port
.count
) /* 0 means already hung up... */
1180 if (--ch
->port
.count
== 0)
1181 moxa_close_port(ch
);
1183 ch
->port
.flags
|= ASYNC_NORMAL_ACTIVE
;
1184 mutex_unlock(&moxa_openlock
);
1189 static void moxa_close(struct tty_struct
*tty
, struct file
*filp
)
1191 struct moxa_port
*ch
;
1195 if (port
== MAX_PORTS
|| tty_hung_up_p(filp
))
1198 mutex_lock(&moxa_openlock
);
1199 ch
= tty
->driver_data
;
1202 if (tty
->count
== 1 && ch
->port
.count
!= 1) {
1203 printk(KERN_WARNING
"moxa_close: bad serial port count; "
1204 "tty->count is 1, ch->port.count is %d\n", ch
->port
.count
);
1207 if (--ch
->port
.count
< 0) {
1208 printk(KERN_WARNING
"moxa_close: bad serial port count, "
1209 "device=%s\n", tty
->name
);
1215 ch
->cflag
= tty
->termios
->c_cflag
;
1216 if (ch
->port
.flags
& ASYNC_INITIALIZED
) {
1217 moxa_setup_empty_event(tty
);
1218 tty_wait_until_sent(tty
, 30 * HZ
); /* 30 seconds timeout */
1221 moxa_close_port(ch
);
1223 mutex_unlock(&moxa_openlock
);
1226 static int moxa_write(struct tty_struct
*tty
,
1227 const unsigned char *buf
, int count
)
1229 struct moxa_port
*ch
= tty
->driver_data
;
1235 spin_lock_bh(&moxa_lock
);
1236 len
= MoxaPortWriteData(ch
, buf
, count
);
1237 spin_unlock_bh(&moxa_lock
);
1239 ch
->statusflags
|= LOWWAIT
;
1243 static int moxa_write_room(struct tty_struct
*tty
)
1245 struct moxa_port
*ch
;
1249 ch
= tty
->driver_data
;
1252 return MoxaPortTxFree(ch
);
1255 static void moxa_flush_buffer(struct tty_struct
*tty
)
1257 struct moxa_port
*ch
= tty
->driver_data
;
1261 MoxaPortFlushData(ch
, 1);
1265 static int moxa_chars_in_buffer(struct tty_struct
*tty
)
1267 struct moxa_port
*ch
= tty
->driver_data
;
1271 * Sigh...I have to check if driver_data is NULL here, because
1272 * if an open() fails, the TTY subsystem eventually calls
1273 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1274 * routine. And since the open() failed, we return 0 here. TDJ
1279 chars
= MoxaPortTxQueue(ch
);
1282 * Make it possible to wakeup anything waiting for output
1283 * in tty_ioctl.c, etc.
1285 if (!(ch
->statusflags
& EMPTYWAIT
))
1286 moxa_setup_empty_event(tty
);
1292 static int moxa_tiocmget(struct tty_struct
*tty
, struct file
*file
)
1294 struct moxa_port
*ch
;
1295 int flag
= 0, dtr
, rts
;
1297 mutex_lock(&moxa_openlock
);
1298 ch
= tty
->driver_data
;
1300 mutex_unlock(&moxa_openlock
);
1304 MoxaPortGetLineOut(ch
, &dtr
, &rts
);
1309 dtr
= MoxaPortLineStatus(ch
);
1316 mutex_unlock(&moxa_openlock
);
1320 static int moxa_tiocmset(struct tty_struct
*tty
, struct file
*file
,
1321 unsigned int set
, unsigned int clear
)
1323 struct moxa_port
*ch
;
1328 mutex_lock(&moxa_openlock
);
1329 ch
= tty
->driver_data
;
1331 mutex_unlock(&moxa_openlock
);
1335 MoxaPortGetLineOut(ch
, &dtr
, &rts
);
1336 if (set
& TIOCM_RTS
)
1338 if (set
& TIOCM_DTR
)
1340 if (clear
& TIOCM_RTS
)
1342 if (clear
& TIOCM_DTR
)
1344 MoxaPortLineCtrl(ch
, dtr
, rts
);
1345 mutex_unlock(&moxa_openlock
);
1349 static void moxa_throttle(struct tty_struct
*tty
)
1351 struct moxa_port
*ch
= tty
->driver_data
;
1353 ch
->statusflags
|= THROTTLE
;
1356 static void moxa_unthrottle(struct tty_struct
*tty
)
1358 struct moxa_port
*ch
= tty
->driver_data
;
1360 ch
->statusflags
&= ~THROTTLE
;
1363 static void moxa_set_termios(struct tty_struct
*tty
,
1364 struct ktermios
*old_termios
)
1366 struct moxa_port
*ch
= tty
->driver_data
;
1370 moxa_set_tty_param(tty
, old_termios
);
1371 if (!(old_termios
->c_cflag
& CLOCAL
) && C_CLOCAL(tty
))
1372 wake_up_interruptible(&ch
->port
.open_wait
);
1375 static void moxa_stop(struct tty_struct
*tty
)
1377 struct moxa_port
*ch
= tty
->driver_data
;
1381 MoxaPortTxDisable(ch
);
1382 ch
->statusflags
|= TXSTOPPED
;
1386 static void moxa_start(struct tty_struct
*tty
)
1388 struct moxa_port
*ch
= tty
->driver_data
;
1393 if (!(ch
->statusflags
& TXSTOPPED
))
1396 MoxaPortTxEnable(ch
);
1397 ch
->statusflags
&= ~TXSTOPPED
;
1400 static void moxa_hangup(struct tty_struct
*tty
)
1402 struct moxa_port
*ch
;
1404 mutex_lock(&moxa_openlock
);
1405 ch
= tty
->driver_data
;
1407 mutex_unlock(&moxa_openlock
);
1411 moxa_close_port(ch
);
1412 mutex_unlock(&moxa_openlock
);
1414 wake_up_interruptible(&ch
->port
.open_wait
);
1417 static void moxa_new_dcdstate(struct moxa_port
*p
, u8 dcd
)
1421 if (dcd
!= p
->DCDState
&& p
->port
.tty
&& C_CLOCAL(p
->port
.tty
)) {
1423 tty_hangup(p
->port
.tty
);
1428 static int moxa_poll_port(struct moxa_port
*p
, unsigned int handle
,
1431 struct tty_struct
*tty
= p
->port
.tty
;
1432 void __iomem
*ofsAddr
;
1433 unsigned int inited
= p
->port
.flags
& ASYNC_INITIALIZED
;
1437 if ((p
->statusflags
& EMPTYWAIT
) &&
1438 MoxaPortTxQueue(p
) == 0) {
1439 p
->statusflags
&= ~EMPTYWAIT
;
1442 if ((p
->statusflags
& LOWWAIT
) && !tty
->stopped
&&
1443 MoxaPortTxQueue(p
) <= WAKEUP_CHARS
) {
1444 p
->statusflags
&= ~LOWWAIT
;
1448 if (inited
&& !(p
->statusflags
& THROTTLE
) &&
1449 MoxaPortRxQueue(p
) > 0) { /* RX */
1450 MoxaPortReadData(p
);
1451 tty_schedule_flip(tty
);
1454 p
->statusflags
&= ~EMPTYWAIT
;
1455 MoxaPortFlushData(p
, 0); /* flush RX */
1458 if (!handle
) /* nothing else to do */
1461 intr
= readw(ip
); /* port irq status */
1465 writew(0, ip
); /* ACK port */
1466 ofsAddr
= p
->tableAddr
;
1467 if (intr
& IntrTx
) /* disable tx intr */
1468 writew(readw(ofsAddr
+ HostStat
) & ~WakeupTx
,
1469 ofsAddr
+ HostStat
);
1474 if (tty
&& (intr
& IntrBreak
) && !I_IGNBRK(tty
)) { /* BREAK */
1475 tty_insert_flip_char(tty
, 0, TTY_BREAK
);
1476 tty_schedule_flip(tty
);
1479 if (intr
& IntrLine
)
1480 moxa_new_dcdstate(p
, readb(ofsAddr
+ FlagStat
) & DCD_state
);
1485 static void moxa_poll(unsigned long ignored
)
1487 struct moxa_board_conf
*brd
;
1489 unsigned int card
, port
, served
= 0;
1491 spin_lock(&moxa_lock
);
1492 for (card
= 0; card
< MAX_BOARDS
; card
++) {
1493 brd
= &moxa_boards
[card
];
1500 if (readb(brd
->intPend
) == 0xff)
1501 ip
= brd
->intTable
+ readb(brd
->intNdx
);
1503 for (port
= 0; port
< brd
->numPorts
; port
++)
1504 moxa_poll_port(&brd
->ports
[port
], !!ip
, ip
+ port
);
1507 writeb(0, brd
->intPend
); /* ACK */
1509 if (moxaLowWaterChk
) {
1510 struct moxa_port
*p
= brd
->ports
;
1511 for (port
= 0; port
< brd
->numPorts
; port
++, p
++)
1512 if (p
->lowChkFlag
) {
1514 moxa_low_water_check(p
->tableAddr
);
1518 moxaLowWaterChk
= 0;
1521 mod_timer(&moxaTimer
, jiffies
+ HZ
/ 50);
1522 spin_unlock(&moxa_lock
);
1525 /******************************************************************************/
1527 static void moxa_set_tty_param(struct tty_struct
*tty
, struct ktermios
*old_termios
)
1529 register struct ktermios
*ts
= tty
->termios
;
1530 struct moxa_port
*ch
= tty
->driver_data
;
1531 int rts
, cts
, txflow
, rxflow
, xany
, baud
;
1533 rts
= cts
= txflow
= rxflow
= xany
= 0;
1534 if (ts
->c_cflag
& CRTSCTS
)
1536 if (ts
->c_iflag
& IXON
)
1538 if (ts
->c_iflag
& IXOFF
)
1540 if (ts
->c_iflag
& IXANY
)
1543 /* Clear the features we don't support */
1544 ts
->c_cflag
&= ~CMSPAR
;
1545 MoxaPortFlowCtrl(ch
, rts
, cts
, txflow
, rxflow
, xany
);
1546 baud
= MoxaPortSetTermio(ch
, ts
, tty_get_baud_rate(tty
));
1548 baud
= tty_termios_baud_rate(old_termios
);
1549 /* Not put the baud rate into the termios data */
1550 tty_encode_baud_rate(tty
, baud
, baud
);
1553 static void moxa_setup_empty_event(struct tty_struct
*tty
)
1555 struct moxa_port
*ch
= tty
->driver_data
;
1557 spin_lock_bh(&moxa_lock
);
1558 ch
->statusflags
|= EMPTYWAIT
;
1559 spin_unlock_bh(&moxa_lock
);
1562 static void moxa_shut_down(struct moxa_port
*ch
)
1564 struct tty_struct
*tp
= ch
->port
.tty
;
1566 if (!(ch
->port
.flags
& ASYNC_INITIALIZED
))
1569 MoxaPortDisable(ch
);
1572 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1575 MoxaPortLineCtrl(ch
, 0, 0);
1577 spin_lock_bh(&moxa_lock
);
1578 ch
->port
.flags
&= ~ASYNC_INITIALIZED
;
1579 spin_unlock_bh(&moxa_lock
);
1582 /*****************************************************************************
1583 * Driver level functions: *
1584 *****************************************************************************/
1586 static void MoxaPortFlushData(struct moxa_port
*port
, int mode
)
1588 void __iomem
*ofsAddr
;
1589 if (mode
< 0 || mode
> 2)
1591 ofsAddr
= port
->tableAddr
;
1592 moxafunc(ofsAddr
, FC_FlushQueue
, mode
);
1594 port
->lowChkFlag
= 0;
1595 moxa_low_water_check(ofsAddr
);
1600 * Moxa Port Number Description:
1602 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1603 * the port number using in MOXA driver functions will be 0 to 31 for
1604 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1605 * to 127 for fourth. For example, if you setup three MOXA boards,
1606 * first board is C218, second board is C320-16 and third board is
1607 * C320-32. The port number of first board (C218 - 8 ports) is from
1608 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1609 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1610 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1611 * 127 will be invalid.
1614 * Moxa Functions Description:
1616 * Function 1: Driver initialization routine, this routine must be
1617 * called when initialized driver.
1619 * void MoxaDriverInit();
1622 * Function 2: Moxa driver private IOCTL command processing.
1624 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1626 * unsigned int cmd : IOCTL command
1627 * unsigned long arg : IOCTL argument
1628 * int port : port number (0 - 127)
1635 * Function 6: Enable this port to start Tx/Rx data.
1637 * void MoxaPortEnable(int port);
1638 * int port : port number (0 - 127)
1641 * Function 7: Disable this port
1643 * void MoxaPortDisable(int port);
1644 * int port : port number (0 - 127)
1647 * Function 10: Setting baud rate of this port.
1649 * speed_t MoxaPortSetBaud(int port, speed_t baud);
1650 * int port : port number (0 - 127)
1651 * long baud : baud rate (50 - 115200)
1653 * return: 0 : this port is invalid or baud < 50
1654 * 50 - 115200 : the real baud rate set to the port, if
1655 * the argument baud is large than maximun
1656 * available baud rate, the real setting
1657 * baud rate will be the maximun baud rate.
1660 * Function 12: Configure the port.
1662 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1663 * int port : port number (0 - 127)
1664 * struct ktermios * termio : termio structure pointer
1665 * speed_t baud : baud rate
1667 * return: -1 : this port is invalid or termio == NULL
1671 * Function 13: Get the DTR/RTS state of this port.
1673 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1674 * int port : port number (0 - 127)
1675 * int * dtrState : pointer to INT to receive the current DTR
1676 * state. (if NULL, this function will not
1677 * write to this address)
1678 * int * rtsState : pointer to INT to receive the current RTS
1679 * state. (if NULL, this function will not
1680 * write to this address)
1682 * return: -1 : this port is invalid
1686 * Function 14: Setting the DTR/RTS output state of this port.
1688 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1689 * int port : port number (0 - 127)
1690 * int dtrState : DTR output state (0: off, 1: on)
1691 * int rtsState : RTS output state (0: off, 1: on)
1694 * Function 15: Setting the flow control of this port.
1696 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1697 * int txFlow,int xany);
1698 * int port : port number (0 - 127)
1699 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1700 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1701 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1702 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1703 * int xany : S/W XANY flow control (0: no, 1: yes)
1706 * Function 16: Get ths line status of this port
1708 * int MoxaPortLineStatus(int port);
1709 * int port : port number (0 - 127)
1711 * return: Bit 0 - CTS state (0: off, 1: on)
1712 * Bit 1 - DSR state (0: off, 1: on)
1713 * Bit 2 - DCD state (0: off, 1: on)
1716 * Function 19: Flush the Rx/Tx buffer data of this port.
1718 * void MoxaPortFlushData(int port, int mode);
1719 * int port : port number (0 - 127)
1721 * 0 : flush the Rx buffer
1722 * 1 : flush the Tx buffer
1723 * 2 : flush the Rx and Tx buffer
1726 * Function 20: Write data.
1728 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1729 * int port : port number (0 - 127)
1730 * unsigned char * buffer : pointer to write data buffer.
1731 * int length : write data length
1733 * return: 0 - length : real write data length
1736 * Function 21: Read data.
1738 * int MoxaPortReadData(int port, struct tty_struct *tty);
1739 * int port : port number (0 - 127)
1740 * struct tty_struct *tty : tty for data
1742 * return: 0 - length : real read data length
1745 * Function 24: Get the Tx buffer current queued data bytes
1747 * int MoxaPortTxQueue(int port);
1748 * int port : port number (0 - 127)
1750 * return: .. : Tx buffer current queued data bytes
1753 * Function 25: Get the Tx buffer current free space
1755 * int MoxaPortTxFree(int port);
1756 * int port : port number (0 - 127)
1758 * return: .. : Tx buffer current free space
1761 * Function 26: Get the Rx buffer current queued data bytes
1763 * int MoxaPortRxQueue(int port);
1764 * int port : port number (0 - 127)
1766 * return: .. : Rx buffer current queued data bytes
1769 * Function 28: Disable port data transmission.
1771 * void MoxaPortTxDisable(int port);
1772 * int port : port number (0 - 127)
1775 * Function 29: Enable port data transmission.
1777 * void MoxaPortTxEnable(int port);
1778 * int port : port number (0 - 127)
1781 * Function 31: Get the received BREAK signal count and reset it.
1783 * int MoxaPortResetBrkCnt(int port);
1784 * int port : port number (0 - 127)
1786 * return: 0 - .. : BREAK signal count
1791 static void MoxaPortEnable(struct moxa_port
*port
)
1793 void __iomem
*ofsAddr
;
1796 ofsAddr
= port
->tableAddr
;
1797 writew(lowwater
, ofsAddr
+ Low_water
);
1798 if (MOXA_IS_320(port
->board
))
1799 moxafunc(ofsAddr
, FC_SetBreakIrq
, 0);
1801 writew(readw(ofsAddr
+ HostStat
) | WakeupBreak
,
1802 ofsAddr
+ HostStat
);
1804 moxafunc(ofsAddr
, FC_SetLineIrq
, Magic_code
);
1805 moxafunc(ofsAddr
, FC_FlushQueue
, 2);
1807 moxafunc(ofsAddr
, FC_EnableCH
, Magic_code
);
1808 MoxaPortLineStatus(port
);
1811 static void MoxaPortDisable(struct moxa_port
*port
)
1813 void __iomem
*ofsAddr
= port
->tableAddr
;
1815 moxafunc(ofsAddr
, FC_SetFlowCtl
, 0); /* disable flow control */
1816 moxafunc(ofsAddr
, FC_ClrLineIrq
, Magic_code
);
1817 writew(0, ofsAddr
+ HostStat
);
1818 moxafunc(ofsAddr
, FC_DisableCH
, Magic_code
);
1821 static speed_t
MoxaPortSetBaud(struct moxa_port
*port
, speed_t baud
)
1823 void __iomem
*ofsAddr
= port
->tableAddr
;
1824 unsigned int clock
, val
;
1827 max
= MOXA_IS_320(port
->board
) ? 460800 : 921600;
1834 moxafunc(ofsAddr
, FC_SetBaud
, val
);
1839 static int MoxaPortSetTermio(struct moxa_port
*port
, struct ktermios
*termio
,
1842 void __iomem
*ofsAddr
;
1846 ofsAddr
= port
->tableAddr
;
1847 cflag
= termio
->c_cflag
; /* termio->c_cflag */
1849 mode
= termio
->c_cflag
& CSIZE
;
1852 else if (mode
== CS6
)
1854 else if (mode
== CS7
)
1856 else if (mode
== CS8
)
1859 if (termio
->c_cflag
& CSTOPB
) {
1867 if (termio
->c_cflag
& PARENB
) {
1868 if (termio
->c_cflag
& PARODD
)
1875 moxafunc(ofsAddr
, FC_SetDataMode
, (u16
)mode
);
1877 if (MOXA_IS_320(port
->board
) && baud
>= 921600)
1880 baud
= MoxaPortSetBaud(port
, baud
);
1882 if (termio
->c_iflag
& (IXON
| IXOFF
| IXANY
)) {
1883 writeb(termio
->c_cc
[VSTART
], ofsAddr
+ FuncArg
);
1884 writeb(termio
->c_cc
[VSTOP
], ofsAddr
+ FuncArg1
);
1885 writeb(FC_SetXonXoff
, ofsAddr
+ FuncCode
);
1886 moxa_wait_finish(ofsAddr
);
1892 static int MoxaPortGetLineOut(struct moxa_port
*port
, int *dtrState
,
1896 *dtrState
= !!(port
->lineCtrl
& DTR_ON
);
1898 *rtsState
= !!(port
->lineCtrl
& RTS_ON
);
1903 static void MoxaPortLineCtrl(struct moxa_port
*port
, int dtr
, int rts
)
1911 port
->lineCtrl
= mode
;
1912 moxafunc(port
->tableAddr
, FC_LineControl
, mode
);
1915 static void MoxaPortFlowCtrl(struct moxa_port
*port
, int rts
, int cts
,
1916 int txflow
, int rxflow
, int txany
)
1921 mode
|= RTS_FlowCtl
;
1923 mode
|= CTS_FlowCtl
;
1930 moxafunc(port
->tableAddr
, FC_SetFlowCtl
, mode
);
1933 static int MoxaPortLineStatus(struct moxa_port
*port
)
1935 void __iomem
*ofsAddr
;
1938 ofsAddr
= port
->tableAddr
;
1939 if (MOXA_IS_320(port
->board
)) {
1940 moxafunc(ofsAddr
, FC_LineStatus
, 0);
1941 val
= readw(ofsAddr
+ FuncArg
);
1943 val
= readw(ofsAddr
+ FlagStat
) >> 4;
1948 spin_lock_bh(&moxa_lock
);
1949 moxa_new_dcdstate(port
, val
& 8);
1950 spin_unlock_bh(&moxa_lock
);
1955 static int MoxaPortWriteData(struct moxa_port
*port
,
1956 const unsigned char *buffer
, int len
)
1958 void __iomem
*baseAddr
, *ofsAddr
, *ofs
;
1959 unsigned int c
, total
;
1960 u16 head
, tail
, tx_mask
, spage
, epage
;
1961 u16 pageno
, pageofs
, bufhead
;
1963 ofsAddr
= port
->tableAddr
;
1964 baseAddr
= port
->board
->basemem
;
1965 tx_mask
= readw(ofsAddr
+ TX_mask
);
1966 spage
= readw(ofsAddr
+ Page_txb
);
1967 epage
= readw(ofsAddr
+ EndPage_txb
);
1968 tail
= readw(ofsAddr
+ TXwptr
);
1969 head
= readw(ofsAddr
+ TXrptr
);
1970 c
= (head
> tail
) ? (head
- tail
- 1) : (head
- tail
+ tx_mask
);
1973 moxaLog
.txcnt
[port
->port
.tty
->index
] += c
;
1975 if (spage
== epage
) {
1976 bufhead
= readw(ofsAddr
+ Ofs_txb
);
1977 writew(spage
, baseAddr
+ Control_reg
);
1980 len
= head
- tail
- 1;
1982 len
= tx_mask
+ 1 - tail
;
1983 len
= (c
> len
) ? len
: c
;
1984 ofs
= baseAddr
+ DynPage_addr
+ bufhead
+ tail
;
1985 memcpy_toio(ofs
, buffer
, len
);
1987 tail
= (tail
+ len
) & tx_mask
;
1991 pageno
= spage
+ (tail
>> 13);
1992 pageofs
= tail
& Page_mask
;
1994 len
= Page_size
- pageofs
;
1997 writeb(pageno
, baseAddr
+ Control_reg
);
1998 ofs
= baseAddr
+ DynPage_addr
+ pageofs
;
1999 memcpy_toio(ofs
, buffer
, len
);
2001 if (++pageno
== epage
)
2006 tail
= (tail
+ total
) & tx_mask
;
2008 writew(tail
, ofsAddr
+ TXwptr
);
2009 writeb(1, ofsAddr
+ CD180TXirq
); /* start to send */
2013 static int MoxaPortReadData(struct moxa_port
*port
)
2015 struct tty_struct
*tty
= port
->port
.tty
;
2017 void __iomem
*baseAddr
, *ofsAddr
, *ofs
;
2018 unsigned int count
, len
, total
;
2019 u16 tail
, rx_mask
, spage
, epage
;
2020 u16 pageno
, pageofs
, bufhead
, head
;
2022 ofsAddr
= port
->tableAddr
;
2023 baseAddr
= port
->board
->basemem
;
2024 head
= readw(ofsAddr
+ RXrptr
);
2025 tail
= readw(ofsAddr
+ RXwptr
);
2026 rx_mask
= readw(ofsAddr
+ RX_mask
);
2027 spage
= readw(ofsAddr
+ Page_rxb
);
2028 epage
= readw(ofsAddr
+ EndPage_rxb
);
2029 count
= (tail
>= head
) ? (tail
- head
) : (tail
- head
+ rx_mask
+ 1);
2034 moxaLog
.rxcnt
[tty
->index
] += total
;
2035 if (spage
== epage
) {
2036 bufhead
= readw(ofsAddr
+ Ofs_rxb
);
2037 writew(spage
, baseAddr
+ Control_reg
);
2039 ofs
= baseAddr
+ DynPage_addr
+ bufhead
+ head
;
2040 len
= (tail
>= head
) ? (tail
- head
) :
2041 (rx_mask
+ 1 - head
);
2042 len
= tty_prepare_flip_string(tty
, &dst
,
2044 memcpy_fromio(dst
, ofs
, len
);
2045 head
= (head
+ len
) & rx_mask
;
2049 pageno
= spage
+ (head
>> 13);
2050 pageofs
= head
& Page_mask
;
2052 writew(pageno
, baseAddr
+ Control_reg
);
2053 ofs
= baseAddr
+ DynPage_addr
+ pageofs
;
2054 len
= tty_prepare_flip_string(tty
, &dst
,
2055 min(Page_size
- pageofs
, count
));
2056 memcpy_fromio(dst
, ofs
, len
);
2059 pageofs
= (pageofs
+ len
) & Page_mask
;
2060 if (pageofs
== 0 && ++pageno
== epage
)
2063 head
= (head
+ total
) & rx_mask
;
2065 writew(head
, ofsAddr
+ RXrptr
);
2066 if (readb(ofsAddr
+ FlagStat
) & Xoff_state
) {
2067 moxaLowWaterChk
= 1;
2068 port
->lowChkFlag
= 1;
2074 static int MoxaPortTxQueue(struct moxa_port
*port
)
2076 void __iomem
*ofsAddr
= port
->tableAddr
;
2077 u16 rptr
, wptr
, mask
;
2079 rptr
= readw(ofsAddr
+ TXrptr
);
2080 wptr
= readw(ofsAddr
+ TXwptr
);
2081 mask
= readw(ofsAddr
+ TX_mask
);
2082 return (wptr
- rptr
) & mask
;
2085 static int MoxaPortTxFree(struct moxa_port
*port
)
2087 void __iomem
*ofsAddr
= port
->tableAddr
;
2088 u16 rptr
, wptr
, mask
;
2090 rptr
= readw(ofsAddr
+ TXrptr
);
2091 wptr
= readw(ofsAddr
+ TXwptr
);
2092 mask
= readw(ofsAddr
+ TX_mask
);
2093 return mask
- ((wptr
- rptr
) & mask
);
2096 static int MoxaPortRxQueue(struct moxa_port
*port
)
2098 void __iomem
*ofsAddr
= port
->tableAddr
;
2099 u16 rptr
, wptr
, mask
;
2101 rptr
= readw(ofsAddr
+ RXrptr
);
2102 wptr
= readw(ofsAddr
+ RXwptr
);
2103 mask
= readw(ofsAddr
+ RX_mask
);
2104 return (wptr
- rptr
) & mask
;
2107 static void MoxaPortTxDisable(struct moxa_port
*port
)
2109 moxafunc(port
->tableAddr
, FC_SetXoffState
, Magic_code
);
2112 static void MoxaPortTxEnable(struct moxa_port
*port
)
2114 moxafunc(port
->tableAddr
, FC_SetXonState
, Magic_code
);
2117 static int moxa_get_serial_info(struct moxa_port
*info
,
2118 struct serial_struct __user
*retinfo
)
2120 struct serial_struct tmp
= {
2122 .line
= info
->port
.tty
->index
,
2123 .flags
= info
->port
.flags
,
2124 .baud_base
= 921600,
2125 .close_delay
= info
->port
.close_delay
2127 return copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)) ? -EFAULT
: 0;
2131 static int moxa_set_serial_info(struct moxa_port
*info
,
2132 struct serial_struct __user
*new_info
)
2134 struct serial_struct new_serial
;
2136 if (copy_from_user(&new_serial
, new_info
, sizeof(new_serial
)))
2139 if (new_serial
.irq
!= 0 || new_serial
.port
!= 0 ||
2140 new_serial
.custom_divisor
!= 0 ||
2141 new_serial
.baud_base
!= 921600)
2144 if (!capable(CAP_SYS_ADMIN
)) {
2145 if (((new_serial
.flags
& ~ASYNC_USR_MASK
) !=
2146 (info
->port
.flags
& ~ASYNC_USR_MASK
)))
2149 info
->port
.close_delay
= new_serial
.close_delay
* HZ
/ 100;
2151 new_serial
.flags
= (new_serial
.flags
& ~ASYNC_FLAGS
);
2152 new_serial
.flags
|= (info
->port
.flags
& ASYNC_FLAGS
);
2154 MoxaSetFifo(info
, new_serial
.type
== PORT_16550A
);
2156 info
->type
= new_serial
.type
;
2162 /*****************************************************************************
2163 * Static local functions: *
2164 *****************************************************************************/
2166 static void MoxaSetFifo(struct moxa_port
*port
, int enable
)
2168 void __iomem
*ofsAddr
= port
->tableAddr
;
2171 moxafunc(ofsAddr
, FC_SetRxFIFOTrig
, 0);
2172 moxafunc(ofsAddr
, FC_SetTxFIFOCnt
, 1);
2174 moxafunc(ofsAddr
, FC_SetRxFIFOTrig
, 3);
2175 moxafunc(ofsAddr
, FC_SetTxFIFOCnt
, 16);