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/smp_lock.h>
38 #include <linux/string.h>
39 #include <linux/fcntl.h>
40 #include <linux/ptrace.h>
41 #include <linux/serial.h>
42 #include <linux/tty_driver.h>
43 #include <linux/delay.h>
44 #include <linux/pci.h>
45 #include <linux/init.h>
46 #include <linux/bitops.h>
48 #include <asm/system.h>
50 #include <asm/uaccess.h>
54 #define MOXA_VERSION "6.0k"
56 #define MOXA_FW_HDRLEN 32
60 #define MAX_BOARDS 4 /* Don't change this value */
61 #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
62 #define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
64 #define MOXA_IS_320(brd) ((brd)->boardType == MOXA_BOARD_C320_ISA || \
65 (brd)->boardType == MOXA_BOARD_C320_PCI)
68 * Define the Moxa PCI vendor and device IDs.
70 #define MOXA_BUS_TYPE_ISA 0
71 #define MOXA_BUS_TYPE_PCI 1
74 MOXA_BOARD_C218_PCI = 1,
81 static char *moxa_brdname[] =
83 "C218 Turbo PCI series",
84 "C218 Turbo ISA series",
85 "C320 Turbo PCI series",
86 "C320 Turbo ISA series",
91 static struct pci_device_id moxa_pcibrds[] = {
92 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
93 .driver_data = MOXA_BOARD_C218_PCI },
94 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
95 .driver_data = MOXA_BOARD_C320_PCI },
96 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
97 .driver_data = MOXA_BOARD_CP204J },
100 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
101 #endif /* CONFIG_PCI */
105 static struct moxa_board_conf {
112 struct moxa_port *ports;
114 void __iomem *basemem;
115 void __iomem *intNdx;
116 void __iomem *intPend;
117 void __iomem *intTable;
118 } moxa_boards[MAX_BOARDS];
120 struct mxser_mstatus {
134 struct tty_port port;
135 struct moxa_board_conf *board;
136 void __iomem *tableAddr;
140 unsigned long statusflags;
149 int rxcnt[MAX_PORTS];
150 int txcnt[MAX_PORTS];
154 #define TXSTOPPED 0x1
156 #define EMPTYWAIT 0x4
159 #define SERIAL_DO_RESTART
161 #define WAKEUP_CHARS 256
163 static int ttymajor = MOXAMAJOR;
164 static struct mon_str moxaLog;
165 static unsigned int moxaFuncTout = HZ / 2;
166 static unsigned int moxaLowWaterChk;
167 static DEFINE_MUTEX(moxa_openlock);
168 /* Variables for insmod */
170 static unsigned long baseaddr[MAX_BOARDS];
171 static unsigned int type[MAX_BOARDS];
172 static unsigned int numports[MAX_BOARDS];
175 MODULE_AUTHOR("William Chen");
176 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
177 MODULE_LICENSE("GPL");
179 module_param_array(type, uint, NULL, 0);
180 MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
181 module_param_array(baseaddr, ulong, NULL, 0);
182 MODULE_PARM_DESC(baseaddr, "base address");
183 module_param_array(numports, uint, NULL, 0);
184 MODULE_PARM_DESC(numports, "numports (ignored for C218)");
186 module_param(ttymajor, int, 0);
191 static int moxa_open(struct tty_struct *, struct file *);
192 static void moxa_close(struct tty_struct *, struct file *);
193 static int moxa_write(struct tty_struct *, const unsigned char *, int);
194 static int moxa_write_room(struct tty_struct *);
195 static void moxa_flush_buffer(struct tty_struct *);
196 static int moxa_chars_in_buffer(struct tty_struct *);
197 static void moxa_throttle(struct tty_struct *);
198 static void moxa_unthrottle(struct tty_struct *);
199 static void moxa_set_termios(struct tty_struct *, struct ktermios *);
200 static void moxa_stop(struct tty_struct *);
201 static void moxa_start(struct tty_struct *);
202 static void moxa_hangup(struct tty_struct *);
203 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
204 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
205 unsigned int set, unsigned int clear);
206 static void moxa_poll(unsigned long);
207 static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
208 static void moxa_setup_empty_event(struct tty_struct *);
209 static void moxa_shutdown(struct tty_port *);
210 static int moxa_carrier_raised(struct tty_port *);
211 static void moxa_dtr_rts(struct tty_port *, int);
213 * moxa board interface functions:
215 static void MoxaPortEnable(struct moxa_port *);
216 static void MoxaPortDisable(struct moxa_port *);
217 static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
218 static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
219 static void MoxaPortLineCtrl(struct moxa_port *, int, int);
220 static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
221 static int MoxaPortLineStatus(struct moxa_port *);
222 static void MoxaPortFlushData(struct moxa_port *, int);
223 static int MoxaPortWriteData(struct tty_struct *, const unsigned char *, int);
224 static int MoxaPortReadData(struct moxa_port *);
225 static int MoxaPortTxQueue(struct moxa_port *);
226 static int MoxaPortRxQueue(struct moxa_port *);
227 static int MoxaPortTxFree(struct moxa_port *);
228 static void MoxaPortTxDisable(struct moxa_port *);
229 static void MoxaPortTxEnable(struct moxa_port *);
230 static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
231 static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
232 static void MoxaSetFifo(struct moxa_port *port, int enable);
238 static void moxa_wait_finish(void __iomem *ofsAddr)
240 unsigned long end = jiffies + moxaFuncTout;
242 while (readw(ofsAddr + FuncCode) != 0)
243 if (time_after(jiffies, end))
245 if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
246 printk(KERN_WARNING "moxa function expired\n");
249 static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
251 writew(arg, ofsAddr + FuncArg);
252 writew(cmd, ofsAddr + FuncCode);
253 moxa_wait_finish(ofsAddr);
256 static void moxa_low_water_check(void __iomem *ofsAddr)
258 u16 rptr, wptr, mask, len;
260 if (readb(ofsAddr + FlagStat) & Xoff_state) {
261 rptr = readw(ofsAddr + RXrptr);
262 wptr = readw(ofsAddr + RXwptr);
263 mask = readw(ofsAddr + RX_mask);
264 len = (wptr - rptr) & mask;
265 if (len <= Low_water)
266 moxafunc(ofsAddr, FC_SendXon, 0);
274 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
275 unsigned int cmd, unsigned long arg)
277 struct moxa_port *ch = tty->driver_data;
278 void __user *argp = (void __user *)arg;
281 if (tty->index == MAX_PORTS) {
282 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
283 cmd != MOXA_GETMSTATUS)
289 case MOXA_GETDATACOUNT:
290 moxaLog.tick = jiffies;
291 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
294 case MOXA_FLUSH_QUEUE:
295 MoxaPortFlushData(ch, arg);
297 case MOXA_GET_IOQUEUE: {
298 struct moxaq_str __user *argm = argp;
299 struct moxaq_str tmp;
303 mutex_lock(&moxa_openlock);
304 for (i = 0; i < MAX_BOARDS; i++) {
305 p = moxa_boards[i].ports;
306 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
307 memset(&tmp, 0, sizeof(tmp));
308 if (moxa_boards[i].ready) {
309 tmp.inq = MoxaPortRxQueue(p);
310 tmp.outq = MoxaPortTxQueue(p);
312 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
313 mutex_unlock(&moxa_openlock);
318 mutex_unlock(&moxa_openlock);
320 } case MOXA_GET_OQUEUE:
321 status = MoxaPortTxQueue(ch);
322 ret = put_user(status, (unsigned long __user *)argp);
324 case MOXA_GET_IQUEUE:
325 status = MoxaPortRxQueue(ch);
326 ret = put_user(status, (unsigned long __user *)argp);
328 case MOXA_GETMSTATUS: {
329 struct mxser_mstatus __user *argm = argp;
330 struct mxser_mstatus tmp;
334 mutex_lock(&moxa_openlock);
335 for (i = 0; i < MAX_BOARDS; i++) {
336 p = moxa_boards[i].ports;
337 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
338 struct tty_struct *ttyp;
339 memset(&tmp, 0, sizeof(tmp));
340 if (!moxa_boards[i].ready)
343 status = MoxaPortLineStatus(p);
351 ttyp = tty_port_tty_get(&p->port);
352 if (!ttyp || !ttyp->termios)
353 tmp.cflag = p->cflag;
355 tmp.cflag = ttyp->termios->c_cflag;
358 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
359 mutex_unlock(&moxa_openlock);
364 mutex_unlock(&moxa_openlock);
368 mutex_lock(&moxa_openlock);
369 ret = moxa_get_serial_info(ch, argp);
370 mutex_unlock(&moxa_openlock);
373 mutex_lock(&moxa_openlock);
374 ret = moxa_set_serial_info(ch, argp);
375 mutex_unlock(&moxa_openlock);
383 static int moxa_break_ctl(struct tty_struct *tty, int state)
385 struct moxa_port *port = tty->driver_data;
387 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
392 static const struct tty_operations moxa_ops = {
396 .write_room = moxa_write_room,
397 .flush_buffer = moxa_flush_buffer,
398 .chars_in_buffer = moxa_chars_in_buffer,
400 .throttle = moxa_throttle,
401 .unthrottle = moxa_unthrottle,
402 .set_termios = moxa_set_termios,
405 .hangup = moxa_hangup,
406 .break_ctl = moxa_break_ctl,
407 .tiocmget = moxa_tiocmget,
408 .tiocmset = moxa_tiocmset,
411 static const struct tty_port_operations moxa_port_ops = {
412 .carrier_raised = moxa_carrier_raised,
413 .dtr_rts = moxa_dtr_rts,
414 .shutdown = moxa_shutdown,
417 static struct tty_driver *moxaDriver;
418 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
419 static DEFINE_SPINLOCK(moxa_lock);
425 static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
427 switch (brd->boardType) {
428 case MOXA_BOARD_C218_ISA:
429 case MOXA_BOARD_C218_PCI:
433 case MOXA_BOARD_CP204J:
447 static int moxa_check_fw(const void *ptr)
449 const __le16 *lptr = ptr;
451 if (*lptr != cpu_to_le16(0x7980))
457 static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
460 void __iomem *baseAddr = brd->basemem;
463 writeb(HW_reset, baseAddr + Control_reg); /* reset */
465 memset_io(baseAddr, 0, 4096);
466 memcpy_toio(baseAddr, buf, len); /* download BIOS */
467 writeb(0, baseAddr + Control_reg); /* restart */
471 switch (brd->boardType) {
472 case MOXA_BOARD_C218_ISA:
473 case MOXA_BOARD_C218_PCI:
474 tmp = readw(baseAddr + C218_key);
475 if (tmp != C218_KeyCode)
478 case MOXA_BOARD_CP204J:
479 tmp = readw(baseAddr + C218_key);
480 if (tmp != CP204J_KeyCode)
484 tmp = readw(baseAddr + C320_key);
485 if (tmp != C320_KeyCode)
487 tmp = readw(baseAddr + C320_status);
488 if (tmp != STS_init) {
489 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
490 "module not found\n");
498 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
502 static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
505 void __iomem *baseAddr = brd->basemem;
508 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
512 writew(len - 7168 - 2, baseAddr + C320bapi_len);
513 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
514 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
515 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
516 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
521 static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
524 void __iomem *baseAddr = brd->basemem;
525 const __le16 *uptr = ptr;
526 size_t wlen, len2, j;
527 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
528 unsigned int i, retry;
531 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
534 switch (brd->boardType) {
535 case MOXA_BOARD_CP204J:
536 case MOXA_BOARD_C218_ISA:
537 case MOXA_BOARD_C218_PCI:
539 loadbuf = C218_LoadBuf;
540 loadlen = C218DLoad_len;
541 checksum = C218check_sum;
542 checksum_ok = C218chksum_ok;
546 keycode = C320_KeyCode;
547 loadbuf = C320_LoadBuf;
548 loadlen = C320DLoad_len;
549 checksum = C320check_sum;
550 checksum_ok = C320chksum_ok;
556 for (i = 0; i < wlen; i++)
557 usum += le16_to_cpu(uptr[i]);
563 len2 = (wlen > 2048) ? 2048 : wlen;
565 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
568 writew(len2, baseAddr + loadlen);
569 writew(0, baseAddr + key);
570 for (i = 0; i < 100; i++) {
571 if (readw(baseAddr + key) == keycode)
575 if (readw(baseAddr + key) != keycode)
578 writew(0, baseAddr + loadlen);
579 writew(usum, baseAddr + checksum);
580 writew(0, baseAddr + key);
581 for (i = 0; i < 100; i++) {
582 if (readw(baseAddr + key) == keycode)
587 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
588 if (readb(baseAddr + checksum_ok) != 1)
591 writew(0, baseAddr + key);
592 for (i = 0; i < 600; i++) {
593 if (readw(baseAddr + Magic_no) == Magic_code)
597 if (readw(baseAddr + Magic_no) != Magic_code)
600 if (MOXA_IS_320(brd)) {
601 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
602 writew(0x3800, baseAddr + TMS320_PORT1);
603 writew(0x3900, baseAddr + TMS320_PORT2);
604 writew(28499, baseAddr + TMS320_CLOCK);
606 writew(0x3200, baseAddr + TMS320_PORT1);
607 writew(0x3400, baseAddr + TMS320_PORT2);
608 writew(19999, baseAddr + TMS320_CLOCK);
611 writew(1, baseAddr + Disable_IRQ);
612 writew(0, baseAddr + Magic_no);
613 for (i = 0; i < 500; i++) {
614 if (readw(baseAddr + Magic_no) == Magic_code)
618 if (readw(baseAddr + Magic_no) != Magic_code)
621 if (MOXA_IS_320(brd)) {
622 j = readw(baseAddr + Module_cnt);
625 brd->numPorts = j * 8;
626 writew(j, baseAddr + Module_no);
627 writew(0, baseAddr + Magic_no);
628 for (i = 0; i < 600; i++) {
629 if (readw(baseAddr + Magic_no) == Magic_code)
633 if (readw(baseAddr + Magic_no) != Magic_code)
636 brd->intNdx = baseAddr + IRQindex;
637 brd->intPend = baseAddr + IRQpending;
638 brd->intTable = baseAddr + IRQtable;
643 static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
646 void __iomem *ofsAddr, *baseAddr = brd->basemem;
647 struct moxa_port *port;
651 printk(KERN_ERR "MOXA: bios length is not even\n");
655 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
659 switch (brd->boardType) {
660 case MOXA_BOARD_C218_ISA:
661 case MOXA_BOARD_C218_PCI:
662 case MOXA_BOARD_CP204J:
664 for (i = 0; i < brd->numPorts; i++, port++) {
667 port->tableAddr = baseAddr + Extern_table +
669 ofsAddr = port->tableAddr;
670 writew(C218rx_mask, ofsAddr + RX_mask);
671 writew(C218tx_mask, ofsAddr + TX_mask);
672 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
673 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
675 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
676 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
682 for (i = 0; i < brd->numPorts; i++, port++) {
685 port->tableAddr = baseAddr + Extern_table +
687 ofsAddr = port->tableAddr;
688 switch (brd->numPorts) {
690 writew(C320p8rx_mask, ofsAddr + RX_mask);
691 writew(C320p8tx_mask, ofsAddr + TX_mask);
692 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
693 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
694 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
695 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
699 writew(C320p16rx_mask, ofsAddr + RX_mask);
700 writew(C320p16tx_mask, ofsAddr + TX_mask);
701 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
702 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
703 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
704 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
708 writew(C320p24rx_mask, ofsAddr + RX_mask);
709 writew(C320p24tx_mask, ofsAddr + TX_mask);
710 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
711 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
712 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
713 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
716 writew(C320p32rx_mask, ofsAddr + RX_mask);
717 writew(C320p32tx_mask, ofsAddr + TX_mask);
718 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
719 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
720 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
721 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
722 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
731 static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
733 const void *ptr = fw->data;
737 unsigned int a, lenp, lencnt;
740 __le32 magic; /* 0x34303430 */
742 u8 type; /* UNIX = 3 */
743 u8 model; /* C218T=1, C320T=2, CP204=3 */
748 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
750 if (fw->size < MOXA_FW_HDRLEN) {
751 strcpy(rsn, "too short (even header won't fit)");
754 if (hdr->magic != cpu_to_le32(0x30343034)) {
755 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
758 if (hdr->type != 3) {
759 sprintf(rsn, "not for linux, type is %u", hdr->type);
762 if (moxa_check_fw_model(brd, hdr->model)) {
763 sprintf(rsn, "not for this card, model is %u", hdr->model);
767 len = MOXA_FW_HDRLEN;
768 lencnt = hdr->model == 2 ? 5 : 3;
769 for (a = 0; a < ARRAY_SIZE(lens); a++) {
770 lens[a] = le16_to_cpu(hdr->len[a]);
771 if (lens[a] && len + lens[a] <= fw->size &&
772 moxa_check_fw(&fw->data[len]))
773 printk(KERN_WARNING "MOXA firmware: unexpected input "
774 "at offset %u, but going on\n", (u32)len);
775 if (!lens[a] && a < lencnt) {
776 sprintf(rsn, "too few entries in fw file");
782 if (len != fw->size) {
783 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
788 ptr += MOXA_FW_HDRLEN;
791 strcpy(rsn, "read above");
793 ret = moxa_load_bios(brd, ptr, lens[lenp]);
797 /* we skip the tty section (lens[1]), since we don't need it */
798 ptr += lens[lenp] + lens[lenp + 1];
799 lenp += 2; /* comm */
801 if (hdr->model == 2) {
802 ret = moxa_load_320b(brd, ptr, lens[lenp]);
805 /* skip another tty */
806 ptr += lens[lenp] + lens[lenp + 1];
810 ret = moxa_load_code(brd, ptr, lens[lenp]);
816 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
820 static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
822 const struct firmware *fw;
828 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
830 if (brd->ports == NULL) {
831 printk(KERN_ERR "cannot allocate memory for ports\n");
836 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
837 tty_port_init(&p->port);
838 p->port.ops = &moxa_port_ops;
839 p->type = PORT_16550A;
840 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
843 switch (brd->boardType) {
844 case MOXA_BOARD_C218_ISA:
845 case MOXA_BOARD_C218_PCI:
846 file = "c218tunx.cod";
848 case MOXA_BOARD_CP204J:
849 file = "cp204unx.cod";
852 file = "c320tunx.cod";
856 ret = request_firmware(&fw, file, dev);
858 printk(KERN_ERR "MOXA: request_firmware failed. Make sure "
859 "you've placed '%s' file into your firmware "
860 "loader directory (e.g. /lib/firmware)\n",
865 ret = moxa_load_fw(brd, fw);
867 release_firmware(fw);
872 spin_lock_bh(&moxa_lock);
874 if (!timer_pending(&moxaTimer))
875 mod_timer(&moxaTimer, jiffies + HZ / 50);
876 spin_unlock_bh(&moxa_lock);
885 static void moxa_board_deinit(struct moxa_board_conf *brd)
887 unsigned int a, opened;
889 mutex_lock(&moxa_openlock);
890 spin_lock_bh(&moxa_lock);
892 spin_unlock_bh(&moxa_lock);
894 /* pci hot-un-plug support */
895 for (a = 0; a < brd->numPorts; a++)
896 if (brd->ports[a].port.flags & ASYNC_INITIALIZED) {
897 struct tty_struct *tty = tty_port_tty_get(
898 &brd->ports[a].port);
906 for (a = 0; a < brd->numPorts; a++)
907 if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
909 mutex_unlock(&moxa_openlock);
913 mutex_lock(&moxa_openlock);
916 iounmap(brd->basemem);
922 static int __devinit moxa_pci_probe(struct pci_dev *pdev,
923 const struct pci_device_id *ent)
925 struct moxa_board_conf *board;
927 int board_type = ent->driver_data;
930 retval = pci_enable_device(pdev);
932 dev_err(&pdev->dev, "can't enable pci device\n");
936 for (i = 0; i < MAX_BOARDS; i++)
937 if (moxa_boards[i].basemem == NULL)
941 if (i >= MAX_BOARDS) {
942 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
943 "found. Board is ignored.\n", MAX_BOARDS);
947 board = &moxa_boards[i];
949 retval = pci_request_region(pdev, 2, "moxa-base");
951 dev_err(&pdev->dev, "can't request pci region 2\n");
955 board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000);
956 if (board->basemem == NULL) {
957 dev_err(&pdev->dev, "can't remap io space 2\n");
961 board->boardType = board_type;
962 switch (board_type) {
963 case MOXA_BOARD_C218_ISA:
964 case MOXA_BOARD_C218_PCI:
968 case MOXA_BOARD_CP204J:
975 board->busType = MOXA_BUS_TYPE_PCI;
977 retval = moxa_init_board(board, &pdev->dev);
981 pci_set_drvdata(pdev, board);
983 dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n",
984 moxa_brdname[board_type - 1], board->numPorts);
988 iounmap(board->basemem);
989 board->basemem = NULL;
991 pci_release_region(pdev, 2);
996 static void __devexit moxa_pci_remove(struct pci_dev *pdev)
998 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
1000 moxa_board_deinit(brd);
1002 pci_release_region(pdev, 2);
1005 static struct pci_driver moxa_pci_driver = {
1007 .id_table = moxa_pcibrds,
1008 .probe = moxa_pci_probe,
1009 .remove = __devexit_p(moxa_pci_remove)
1011 #endif /* CONFIG_PCI */
1013 static int __init moxa_init(void)
1015 unsigned int isabrds = 0;
1018 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
1020 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
1024 moxaDriver->owner = THIS_MODULE;
1025 moxaDriver->name = "ttyMX";
1026 moxaDriver->major = ttymajor;
1027 moxaDriver->minor_start = 0;
1028 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1029 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1030 moxaDriver->init_termios = tty_std_termios;
1031 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
1032 moxaDriver->init_termios.c_ispeed = 9600;
1033 moxaDriver->init_termios.c_ospeed = 9600;
1034 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
1035 tty_set_operations(moxaDriver, &moxa_ops);
1037 if (tty_register_driver(moxaDriver)) {
1038 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
1039 put_tty_driver(moxaDriver);
1043 /* Find the boards defined from module args. */
1046 struct moxa_board_conf *brd = moxa_boards;
1048 for (i = 0; i < MAX_BOARDS; i++) {
1051 if (type[i] == MOXA_BOARD_C218_ISA ||
1052 type[i] == MOXA_BOARD_C320_ISA) {
1053 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
1054 isabrds + 1, moxa_brdname[type[i] - 1],
1056 brd->boardType = type[i];
1057 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1059 brd->busType = MOXA_BUS_TYPE_ISA;
1060 brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
1061 if (!brd->basemem) {
1062 printk(KERN_ERR "MOXA: can't remap %lx\n",
1066 if (moxa_init_board(brd, NULL)) {
1067 iounmap(brd->basemem);
1068 brd->basemem = NULL;
1072 printk(KERN_INFO "MOXA isa board found at 0x%.8lu and "
1073 "ready (%u ports, firmware loaded)\n",
1074 baseaddr[i], brd->numPorts);
1084 retval = pci_register_driver(&moxa_pci_driver);
1086 printk(KERN_ERR "Can't register MOXA pci driver!\n");
1095 static void __exit moxa_exit(void)
1100 pci_unregister_driver(&moxa_pci_driver);
1103 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1104 if (moxa_boards[i].ready)
1105 moxa_board_deinit(&moxa_boards[i]);
1107 del_timer_sync(&moxaTimer);
1109 if (tty_unregister_driver(moxaDriver))
1110 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1112 put_tty_driver(moxaDriver);
1115 module_init(moxa_init);
1116 module_exit(moxa_exit);
1118 static void moxa_shutdown(struct tty_port *port)
1120 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1121 MoxaPortDisable(ch);
1122 MoxaPortFlushData(ch, 2);
1123 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1126 static int moxa_carrier_raised(struct tty_port *port)
1128 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1131 spin_lock_bh(&moxa_lock);
1133 spin_unlock_bh(&moxa_lock);
1137 static void moxa_dtr_rts(struct tty_port *port, int onoff)
1139 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1140 MoxaPortLineCtrl(ch, onoff, onoff);
1144 static int moxa_open(struct tty_struct *tty, struct file *filp)
1146 struct moxa_board_conf *brd;
1147 struct moxa_port *ch;
1152 if (port == MAX_PORTS) {
1153 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
1155 if (mutex_lock_interruptible(&moxa_openlock))
1156 return -ERESTARTSYS;
1157 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
1159 mutex_unlock(&moxa_openlock);
1163 if (port % MAX_PORTS_PER_BOARD >= brd->numPorts) {
1164 mutex_unlock(&moxa_openlock);
1168 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
1170 tty->driver_data = ch;
1171 tty_port_tty_set(&ch->port, tty);
1172 mutex_lock(&ch->port.mutex);
1173 if (!(ch->port.flags & ASYNC_INITIALIZED)) {
1174 ch->statusflags = 0;
1175 moxa_set_tty_param(tty, tty->termios);
1176 MoxaPortLineCtrl(ch, 1, 1);
1178 MoxaSetFifo(ch, ch->type == PORT_16550A);
1179 ch->port.flags |= ASYNC_INITIALIZED;
1181 mutex_unlock(&ch->port.mutex);
1182 mutex_unlock(&moxa_openlock);
1184 retval = tty_port_block_til_ready(&ch->port, tty, filp);
1186 set_bit(ASYNCB_NORMAL_ACTIVE, &ch->port.flags);
1190 static void moxa_close(struct tty_struct *tty, struct file *filp)
1192 struct moxa_port *ch = tty->driver_data;
1193 ch->cflag = tty->termios->c_cflag;
1194 mutex_lock(&moxa_openlock);
1195 tty_port_close(&ch->port, tty, filp);
1196 mutex_unlock(&moxa_openlock);
1199 static int moxa_write(struct tty_struct *tty,
1200 const unsigned char *buf, int count)
1202 struct moxa_port *ch = tty->driver_data;
1208 spin_lock_bh(&moxa_lock);
1209 len = MoxaPortWriteData(tty, buf, count);
1210 spin_unlock_bh(&moxa_lock);
1212 ch->statusflags |= LOWWAIT;
1216 static int moxa_write_room(struct tty_struct *tty)
1218 struct moxa_port *ch;
1222 ch = tty->driver_data;
1225 return MoxaPortTxFree(ch);
1228 static void moxa_flush_buffer(struct tty_struct *tty)
1230 struct moxa_port *ch = tty->driver_data;
1234 MoxaPortFlushData(ch, 1);
1238 static int moxa_chars_in_buffer(struct tty_struct *tty)
1240 struct moxa_port *ch = tty->driver_data;
1244 chars = MoxaPortTxQueue(ch);
1247 * Make it possible to wakeup anything waiting for output
1248 * in tty_ioctl.c, etc.
1250 if (!(ch->statusflags & EMPTYWAIT))
1251 moxa_setup_empty_event(tty);
1257 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1259 struct moxa_port *ch;
1260 int flag = 0, dtr, rts;
1262 mutex_lock(&moxa_openlock);
1263 ch = tty->driver_data;
1265 mutex_unlock(&moxa_openlock);
1269 MoxaPortGetLineOut(ch, &dtr, &rts);
1274 dtr = MoxaPortLineStatus(ch);
1281 mutex_unlock(&moxa_openlock);
1285 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1286 unsigned int set, unsigned int clear)
1288 struct moxa_port *ch;
1293 mutex_lock(&moxa_openlock);
1294 ch = tty->driver_data;
1296 mutex_unlock(&moxa_openlock);
1300 MoxaPortGetLineOut(ch, &dtr, &rts);
1301 if (set & TIOCM_RTS)
1303 if (set & TIOCM_DTR)
1305 if (clear & TIOCM_RTS)
1307 if (clear & TIOCM_DTR)
1309 MoxaPortLineCtrl(ch, dtr, rts);
1310 mutex_unlock(&moxa_openlock);
1314 static void moxa_throttle(struct tty_struct *tty)
1316 struct moxa_port *ch = tty->driver_data;
1318 ch->statusflags |= THROTTLE;
1321 static void moxa_unthrottle(struct tty_struct *tty)
1323 struct moxa_port *ch = tty->driver_data;
1325 ch->statusflags &= ~THROTTLE;
1328 static void moxa_set_termios(struct tty_struct *tty,
1329 struct ktermios *old_termios)
1331 struct moxa_port *ch = tty->driver_data;
1335 moxa_set_tty_param(tty, old_termios);
1336 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
1337 wake_up_interruptible(&ch->port.open_wait);
1340 static void moxa_stop(struct tty_struct *tty)
1342 struct moxa_port *ch = tty->driver_data;
1346 MoxaPortTxDisable(ch);
1347 ch->statusflags |= TXSTOPPED;
1351 static void moxa_start(struct tty_struct *tty)
1353 struct moxa_port *ch = tty->driver_data;
1358 if (!(ch->statusflags & TXSTOPPED))
1361 MoxaPortTxEnable(ch);
1362 ch->statusflags &= ~TXSTOPPED;
1365 static void moxa_hangup(struct tty_struct *tty)
1367 struct moxa_port *ch;
1369 mutex_lock(&moxa_openlock);
1370 ch = tty->driver_data;
1371 tty_port_hangup(&ch->port);
1372 mutex_unlock(&moxa_openlock);
1375 static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1377 struct tty_struct *tty;
1380 if (dcd != p->DCDState) {
1381 tty = tty_port_tty_get(&p->port);
1382 if (tty && C_CLOCAL(tty) && !dcd)
1389 static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1392 struct tty_struct *tty = tty_port_tty_get(&p->port);
1393 void __iomem *ofsAddr;
1394 unsigned int inited = p->port.flags & ASYNC_INITIALIZED;
1398 if ((p->statusflags & EMPTYWAIT) &&
1399 MoxaPortTxQueue(p) == 0) {
1400 p->statusflags &= ~EMPTYWAIT;
1403 if ((p->statusflags & LOWWAIT) && !tty->stopped &&
1404 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1405 p->statusflags &= ~LOWWAIT;
1409 if (inited && !(p->statusflags & THROTTLE) &&
1410 MoxaPortRxQueue(p) > 0) { /* RX */
1411 MoxaPortReadData(p);
1412 tty_schedule_flip(tty);
1415 p->statusflags &= ~EMPTYWAIT;
1416 MoxaPortFlushData(p, 0); /* flush RX */
1419 if (!handle) /* nothing else to do */
1422 intr = readw(ip); /* port irq status */
1426 writew(0, ip); /* ACK port */
1427 ofsAddr = p->tableAddr;
1428 if (intr & IntrTx) /* disable tx intr */
1429 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1430 ofsAddr + HostStat);
1435 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1436 tty_insert_flip_char(tty, 0, TTY_BREAK);
1437 tty_schedule_flip(tty);
1440 if (intr & IntrLine)
1441 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1448 static void moxa_poll(unsigned long ignored)
1450 struct moxa_board_conf *brd;
1452 unsigned int card, port, served = 0;
1454 spin_lock(&moxa_lock);
1455 for (card = 0; card < MAX_BOARDS; card++) {
1456 brd = &moxa_boards[card];
1463 if (readb(brd->intPend) == 0xff)
1464 ip = brd->intTable + readb(brd->intNdx);
1466 for (port = 0; port < brd->numPorts; port++)
1467 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1470 writeb(0, brd->intPend); /* ACK */
1472 if (moxaLowWaterChk) {
1473 struct moxa_port *p = brd->ports;
1474 for (port = 0; port < brd->numPorts; port++, p++)
1475 if (p->lowChkFlag) {
1477 moxa_low_water_check(p->tableAddr);
1481 moxaLowWaterChk = 0;
1484 mod_timer(&moxaTimer, jiffies + HZ / 50);
1485 spin_unlock(&moxa_lock);
1488 /******************************************************************************/
1490 static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
1492 register struct ktermios *ts = tty->termios;
1493 struct moxa_port *ch = tty->driver_data;
1494 int rts, cts, txflow, rxflow, xany, baud;
1496 rts = cts = txflow = rxflow = xany = 0;
1497 if (ts->c_cflag & CRTSCTS)
1499 if (ts->c_iflag & IXON)
1501 if (ts->c_iflag & IXOFF)
1503 if (ts->c_iflag & IXANY)
1506 /* Clear the features we don't support */
1507 ts->c_cflag &= ~CMSPAR;
1508 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1509 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
1511 baud = tty_termios_baud_rate(old_termios);
1512 /* Not put the baud rate into the termios data */
1513 tty_encode_baud_rate(tty, baud, baud);
1516 static void moxa_setup_empty_event(struct tty_struct *tty)
1518 struct moxa_port *ch = tty->driver_data;
1520 spin_lock_bh(&moxa_lock);
1521 ch->statusflags |= EMPTYWAIT;
1522 spin_unlock_bh(&moxa_lock);
1525 /*****************************************************************************
1526 * Driver level functions: *
1527 *****************************************************************************/
1529 static void MoxaPortFlushData(struct moxa_port *port, int mode)
1531 void __iomem *ofsAddr;
1532 if (mode < 0 || mode > 2)
1534 ofsAddr = port->tableAddr;
1535 moxafunc(ofsAddr, FC_FlushQueue, mode);
1537 port->lowChkFlag = 0;
1538 moxa_low_water_check(ofsAddr);
1543 * Moxa Port Number Description:
1545 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1546 * the port number using in MOXA driver functions will be 0 to 31 for
1547 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1548 * to 127 for fourth. For example, if you setup three MOXA boards,
1549 * first board is C218, second board is C320-16 and third board is
1550 * C320-32. The port number of first board (C218 - 8 ports) is from
1551 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1552 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1553 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1554 * 127 will be invalid.
1557 * Moxa Functions Description:
1559 * Function 1: Driver initialization routine, this routine must be
1560 * called when initialized driver.
1562 * void MoxaDriverInit();
1565 * Function 2: Moxa driver private IOCTL command processing.
1567 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1569 * unsigned int cmd : IOCTL command
1570 * unsigned long arg : IOCTL argument
1571 * int port : port number (0 - 127)
1578 * Function 6: Enable this port to start Tx/Rx data.
1580 * void MoxaPortEnable(int port);
1581 * int port : port number (0 - 127)
1584 * Function 7: Disable this port
1586 * void MoxaPortDisable(int port);
1587 * int port : port number (0 - 127)
1590 * Function 10: Setting baud rate of this port.
1592 * speed_t MoxaPortSetBaud(int port, speed_t baud);
1593 * int port : port number (0 - 127)
1594 * long baud : baud rate (50 - 115200)
1596 * return: 0 : this port is invalid or baud < 50
1597 * 50 - 115200 : the real baud rate set to the port, if
1598 * the argument baud is large than maximun
1599 * available baud rate, the real setting
1600 * baud rate will be the maximun baud rate.
1603 * Function 12: Configure the port.
1605 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1606 * int port : port number (0 - 127)
1607 * struct ktermios * termio : termio structure pointer
1608 * speed_t baud : baud rate
1610 * return: -1 : this port is invalid or termio == NULL
1614 * Function 13: Get the DTR/RTS state of this port.
1616 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1617 * int port : port number (0 - 127)
1618 * int * dtrState : pointer to INT to receive the current DTR
1619 * state. (if NULL, this function will not
1620 * write to this address)
1621 * int * rtsState : pointer to INT to receive the current RTS
1622 * state. (if NULL, this function will not
1623 * write to this address)
1625 * return: -1 : this port is invalid
1629 * Function 14: Setting the DTR/RTS output state of this port.
1631 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1632 * int port : port number (0 - 127)
1633 * int dtrState : DTR output state (0: off, 1: on)
1634 * int rtsState : RTS output state (0: off, 1: on)
1637 * Function 15: Setting the flow control of this port.
1639 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1640 * int txFlow,int xany);
1641 * int port : port number (0 - 127)
1642 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1643 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1644 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1645 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1646 * int xany : S/W XANY flow control (0: no, 1: yes)
1649 * Function 16: Get ths line status of this port
1651 * int MoxaPortLineStatus(int port);
1652 * int port : port number (0 - 127)
1654 * return: Bit 0 - CTS state (0: off, 1: on)
1655 * Bit 1 - DSR state (0: off, 1: on)
1656 * Bit 2 - DCD state (0: off, 1: on)
1659 * Function 19: Flush the Rx/Tx buffer data of this port.
1661 * void MoxaPortFlushData(int port, int mode);
1662 * int port : port number (0 - 127)
1664 * 0 : flush the Rx buffer
1665 * 1 : flush the Tx buffer
1666 * 2 : flush the Rx and Tx buffer
1669 * Function 20: Write data.
1671 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1672 * int port : port number (0 - 127)
1673 * unsigned char * buffer : pointer to write data buffer.
1674 * int length : write data length
1676 * return: 0 - length : real write data length
1679 * Function 21: Read data.
1681 * int MoxaPortReadData(int port, struct tty_struct *tty);
1682 * int port : port number (0 - 127)
1683 * struct tty_struct *tty : tty for data
1685 * return: 0 - length : real read data length
1688 * Function 24: Get the Tx buffer current queued data bytes
1690 * int MoxaPortTxQueue(int port);
1691 * int port : port number (0 - 127)
1693 * return: .. : Tx buffer current queued data bytes
1696 * Function 25: Get the Tx buffer current free space
1698 * int MoxaPortTxFree(int port);
1699 * int port : port number (0 - 127)
1701 * return: .. : Tx buffer current free space
1704 * Function 26: Get the Rx buffer current queued data bytes
1706 * int MoxaPortRxQueue(int port);
1707 * int port : port number (0 - 127)
1709 * return: .. : Rx buffer current queued data bytes
1712 * Function 28: Disable port data transmission.
1714 * void MoxaPortTxDisable(int port);
1715 * int port : port number (0 - 127)
1718 * Function 29: Enable port data transmission.
1720 * void MoxaPortTxEnable(int port);
1721 * int port : port number (0 - 127)
1724 * Function 31: Get the received BREAK signal count and reset it.
1726 * int MoxaPortResetBrkCnt(int port);
1727 * int port : port number (0 - 127)
1729 * return: 0 - .. : BREAK signal count
1734 static void MoxaPortEnable(struct moxa_port *port)
1736 void __iomem *ofsAddr;
1739 ofsAddr = port->tableAddr;
1740 writew(lowwater, ofsAddr + Low_water);
1741 if (MOXA_IS_320(port->board))
1742 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1744 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1745 ofsAddr + HostStat);
1747 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1748 moxafunc(ofsAddr, FC_FlushQueue, 2);
1750 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1751 MoxaPortLineStatus(port);
1754 static void MoxaPortDisable(struct moxa_port *port)
1756 void __iomem *ofsAddr = port->tableAddr;
1758 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1759 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1760 writew(0, ofsAddr + HostStat);
1761 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1764 static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
1766 void __iomem *ofsAddr = port->tableAddr;
1767 unsigned int clock, val;
1770 max = MOXA_IS_320(port->board) ? 460800 : 921600;
1777 moxafunc(ofsAddr, FC_SetBaud, val);
1782 static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1785 void __iomem *ofsAddr;
1789 ofsAddr = port->tableAddr;
1790 cflag = termio->c_cflag; /* termio->c_cflag */
1792 mode = termio->c_cflag & CSIZE;
1795 else if (mode == CS6)
1797 else if (mode == CS7)
1799 else if (mode == CS8)
1802 if (termio->c_cflag & CSTOPB) {
1810 if (termio->c_cflag & PARENB) {
1811 if (termio->c_cflag & PARODD)
1818 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
1820 if (MOXA_IS_320(port->board) && baud >= 921600)
1823 baud = MoxaPortSetBaud(port, baud);
1825 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1826 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1827 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1828 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
1829 moxa_wait_finish(ofsAddr);
1835 static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1839 *dtrState = !!(port->lineCtrl & DTR_ON);
1841 *rtsState = !!(port->lineCtrl & RTS_ON);
1846 static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
1854 port->lineCtrl = mode;
1855 moxafunc(port->tableAddr, FC_LineControl, mode);
1858 static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1859 int txflow, int rxflow, int txany)
1864 mode |= RTS_FlowCtl;
1866 mode |= CTS_FlowCtl;
1873 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
1876 static int MoxaPortLineStatus(struct moxa_port *port)
1878 void __iomem *ofsAddr;
1881 ofsAddr = port->tableAddr;
1882 if (MOXA_IS_320(port->board)) {
1883 moxafunc(ofsAddr, FC_LineStatus, 0);
1884 val = readw(ofsAddr + FuncArg);
1886 val = readw(ofsAddr + FlagStat) >> 4;
1891 spin_lock_bh(&moxa_lock);
1892 moxa_new_dcdstate(port, val & 8);
1893 spin_unlock_bh(&moxa_lock);
1898 static int MoxaPortWriteData(struct tty_struct *tty,
1899 const unsigned char *buffer, int len)
1901 struct moxa_port *port = tty->driver_data;
1902 void __iomem *baseAddr, *ofsAddr, *ofs;
1903 unsigned int c, total;
1904 u16 head, tail, tx_mask, spage, epage;
1905 u16 pageno, pageofs, bufhead;
1907 ofsAddr = port->tableAddr;
1908 baseAddr = port->board->basemem;
1909 tx_mask = readw(ofsAddr + TX_mask);
1910 spage = readw(ofsAddr + Page_txb);
1911 epage = readw(ofsAddr + EndPage_txb);
1912 tail = readw(ofsAddr + TXwptr);
1913 head = readw(ofsAddr + TXrptr);
1914 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
1917 moxaLog.txcnt[port->port.tty->index] += c;
1919 if (spage == epage) {
1920 bufhead = readw(ofsAddr + Ofs_txb);
1921 writew(spage, baseAddr + Control_reg);
1924 len = head - tail - 1;
1926 len = tx_mask + 1 - tail;
1927 len = (c > len) ? len : c;
1928 ofs = baseAddr + DynPage_addr + bufhead + tail;
1929 memcpy_toio(ofs, buffer, len);
1931 tail = (tail + len) & tx_mask;
1935 pageno = spage + (tail >> 13);
1936 pageofs = tail & Page_mask;
1938 len = Page_size - pageofs;
1941 writeb(pageno, baseAddr + Control_reg);
1942 ofs = baseAddr + DynPage_addr + pageofs;
1943 memcpy_toio(ofs, buffer, len);
1945 if (++pageno == epage)
1950 tail = (tail + total) & tx_mask;
1952 writew(tail, ofsAddr + TXwptr);
1953 writeb(1, ofsAddr + CD180TXirq); /* start to send */
1957 static int MoxaPortReadData(struct moxa_port *port)
1959 struct tty_struct *tty = port->port.tty;
1961 void __iomem *baseAddr, *ofsAddr, *ofs;
1962 unsigned int count, len, total;
1963 u16 tail, rx_mask, spage, epage;
1964 u16 pageno, pageofs, bufhead, head;
1966 ofsAddr = port->tableAddr;
1967 baseAddr = port->board->basemem;
1968 head = readw(ofsAddr + RXrptr);
1969 tail = readw(ofsAddr + RXwptr);
1970 rx_mask = readw(ofsAddr + RX_mask);
1971 spage = readw(ofsAddr + Page_rxb);
1972 epage = readw(ofsAddr + EndPage_rxb);
1973 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
1978 moxaLog.rxcnt[tty->index] += total;
1979 if (spage == epage) {
1980 bufhead = readw(ofsAddr + Ofs_rxb);
1981 writew(spage, baseAddr + Control_reg);
1983 ofs = baseAddr + DynPage_addr + bufhead + head;
1984 len = (tail >= head) ? (tail - head) :
1985 (rx_mask + 1 - head);
1986 len = tty_prepare_flip_string(tty, &dst,
1988 memcpy_fromio(dst, ofs, len);
1989 head = (head + len) & rx_mask;
1993 pageno = spage + (head >> 13);
1994 pageofs = head & Page_mask;
1996 writew(pageno, baseAddr + Control_reg);
1997 ofs = baseAddr + DynPage_addr + pageofs;
1998 len = tty_prepare_flip_string(tty, &dst,
1999 min(Page_size - pageofs, count));
2000 memcpy_fromio(dst, ofs, len);
2003 pageofs = (pageofs + len) & Page_mask;
2004 if (pageofs == 0 && ++pageno == epage)
2007 head = (head + total) & rx_mask;
2009 writew(head, ofsAddr + RXrptr);
2010 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2011 moxaLowWaterChk = 1;
2012 port->lowChkFlag = 1;
2018 static int MoxaPortTxQueue(struct moxa_port *port)
2020 void __iomem *ofsAddr = port->tableAddr;
2021 u16 rptr, wptr, mask;
2023 rptr = readw(ofsAddr + TXrptr);
2024 wptr = readw(ofsAddr + TXwptr);
2025 mask = readw(ofsAddr + TX_mask);
2026 return (wptr - rptr) & mask;
2029 static int MoxaPortTxFree(struct moxa_port *port)
2031 void __iomem *ofsAddr = port->tableAddr;
2032 u16 rptr, wptr, mask;
2034 rptr = readw(ofsAddr + TXrptr);
2035 wptr = readw(ofsAddr + TXwptr);
2036 mask = readw(ofsAddr + TX_mask);
2037 return mask - ((wptr - rptr) & mask);
2040 static int MoxaPortRxQueue(struct moxa_port *port)
2042 void __iomem *ofsAddr = port->tableAddr;
2043 u16 rptr, wptr, mask;
2045 rptr = readw(ofsAddr + RXrptr);
2046 wptr = readw(ofsAddr + RXwptr);
2047 mask = readw(ofsAddr + RX_mask);
2048 return (wptr - rptr) & mask;
2051 static void MoxaPortTxDisable(struct moxa_port *port)
2053 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
2056 static void MoxaPortTxEnable(struct moxa_port *port)
2058 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
2061 static int moxa_get_serial_info(struct moxa_port *info,
2062 struct serial_struct __user *retinfo)
2064 struct serial_struct tmp = {
2066 .line = info->port.tty->index,
2067 .flags = info->port.flags,
2068 .baud_base = 921600,
2069 .close_delay = info->port.close_delay
2071 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
2075 static int moxa_set_serial_info(struct moxa_port *info,
2076 struct serial_struct __user *new_info)
2078 struct serial_struct new_serial;
2080 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2083 if (new_serial.irq != 0 || new_serial.port != 0 ||
2084 new_serial.custom_divisor != 0 ||
2085 new_serial.baud_base != 921600)
2088 if (!capable(CAP_SYS_ADMIN)) {
2089 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2090 (info->port.flags & ~ASYNC_USR_MASK)))
2093 info->port.close_delay = new_serial.close_delay * HZ / 100;
2095 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2096 new_serial.flags |= (info->port.flags & ASYNC_FLAGS);
2098 MoxaSetFifo(info, new_serial.type == PORT_16550A);
2100 info->type = new_serial.type;
2106 /*****************************************************************************
2107 * Static local functions: *
2108 *****************************************************************************/
2110 static void MoxaSetFifo(struct moxa_port *port, int enable)
2112 void __iomem *ofsAddr = port->tableAddr;
2115 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2116 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2118 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2119 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);