1 /*****************************************************************************/
3 * moxa.c -- MOXA Intellio family multiport serial driver.
5 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com.tw).
7 * This code is loosely based on the Linux serial driver, written by
8 * Linus Torvalds, Theodore T'so and others.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
17 * MOXA Intellio Series Driver
23 #include <linux/module.h>
24 #include <linux/types.h>
26 #include <linux/ioport.h>
27 #include <linux/errno.h>
28 #include <linux/firmware.h>
29 #include <linux/signal.h>
30 #include <linux/sched.h>
31 #include <linux/timer.h>
32 #include <linux/interrupt.h>
33 #include <linux/tty.h>
34 #include <linux/tty_flip.h>
35 #include <linux/major.h>
36 #include <linux/string.h>
37 #include <linux/fcntl.h>
38 #include <linux/ptrace.h>
39 #include <linux/serial.h>
40 #include <linux/tty_driver.h>
41 #include <linux/delay.h>
42 #include <linux/pci.h>
43 #include <linux/init.h>
44 #include <linux/bitops.h>
45 #include <linux/completion.h>
47 #include <asm/system.h>
49 #include <asm/uaccess.h>
53 #define MOXA_VERSION "5.1k"
55 #define MOXA_FW_HDRLEN 32
58 #define MOXACUMAJOR 173
60 #define MAX_BOARDS 4 /* Don't change this value */
61 #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
62 #define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
65 * Define the Moxa PCI vendor and device IDs.
67 #define MOXA_BUS_TYPE_ISA 0
68 #define MOXA_BUS_TYPE_PCI 1
71 MOXA_BOARD_C218_PCI = 1,
78 static char *moxa_brdname[] =
80 "C218 Turbo PCI series",
81 "C218 Turbo ISA series",
82 "C320 Turbo PCI series",
83 "C320 Turbo ISA series",
88 static struct pci_device_id moxa_pcibrds[] = {
89 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
90 .driver_data = MOXA_BOARD_C218_PCI },
91 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
92 .driver_data = MOXA_BOARD_C320_PCI },
93 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
94 .driver_data = MOXA_BOARD_CP204J },
97 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
98 #endif /* CONFIG_PCI */
102 static struct moxa_board_conf {
109 struct moxa_port *ports;
111 void __iomem *basemem;
112 void __iomem *intNdx;
113 void __iomem *intPend;
114 void __iomem *intTable;
115 } moxa_boards[MAX_BOARDS];
117 struct mxser_mstatus {
131 struct moxa_board_conf *board;
137 unsigned long statusflags;
138 struct tty_struct *tty;
140 wait_queue_head_t open_wait;
141 struct completion close_wait;
143 struct timer_list emptyTimer;
146 void __iomem *tableAddr;
154 #define TXSTOPPED 0x1
156 #define EMPTYWAIT 0x4
159 #define SERIAL_DO_RESTART
161 #define WAKEUP_CHARS 256
163 static int ttymajor = MOXAMAJOR;
164 /* Variables for insmod */
166 static unsigned long baseaddr[MAX_BOARDS];
167 static unsigned int type[MAX_BOARDS];
168 static unsigned int numports[MAX_BOARDS];
171 MODULE_AUTHOR("William Chen");
172 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
173 MODULE_LICENSE("GPL");
175 module_param_array(type, uint, NULL, 0);
176 MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
177 module_param_array(baseaddr, ulong, NULL, 0);
178 MODULE_PARM_DESC(baseaddr, "base address");
179 module_param_array(numports, uint, NULL, 0);
180 MODULE_PARM_DESC(numports, "numports (ignored for C218)");
182 module_param(ttymajor, int, 0);
187 static int moxa_open(struct tty_struct *, struct file *);
188 static void moxa_close(struct tty_struct *, struct file *);
189 static int moxa_write(struct tty_struct *, const unsigned char *, int);
190 static int moxa_write_room(struct tty_struct *);
191 static void moxa_flush_buffer(struct tty_struct *);
192 static int moxa_chars_in_buffer(struct tty_struct *);
193 static void moxa_flush_chars(struct tty_struct *);
194 static void moxa_put_char(struct tty_struct *, unsigned char);
195 static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
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 int moxa_block_till_ready(struct tty_struct *, struct file *,
209 static void moxa_setup_empty_event(struct tty_struct *);
210 static void moxa_check_xmit_empty(unsigned long);
211 static void moxa_shut_down(struct moxa_port *);
212 static void moxa_receive_data(struct moxa_port *);
214 * moxa board interface functions:
216 static int MoxaDriverIoctl(struct tty_struct *, unsigned int, unsigned long);
217 static int MoxaDriverPoll(void);
218 static void MoxaPortEnable(struct moxa_port *);
219 static void MoxaPortDisable(struct moxa_port *);
220 static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
221 static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
222 static void MoxaPortLineCtrl(struct moxa_port *, int, int);
223 static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
224 static int MoxaPortLineStatus(struct moxa_port *);
225 static int MoxaPortDCDChange(struct moxa_port *);
226 static int MoxaPortDCDON(struct moxa_port *);
227 static void MoxaPortFlushData(struct moxa_port *, int);
228 static int MoxaPortWriteData(struct moxa_port *, unsigned char *, int);
229 static int MoxaPortReadData(struct moxa_port *, struct tty_struct *tty);
230 static int MoxaPortTxQueue(struct moxa_port *);
231 static int MoxaPortRxQueue(struct moxa_port *);
232 static int MoxaPortTxFree(struct moxa_port *);
233 static void MoxaPortTxDisable(struct moxa_port *);
234 static void MoxaPortTxEnable(struct moxa_port *);
235 static int MoxaPortResetBrkCnt(struct moxa_port *);
236 static void MoxaPortSendBreak(struct moxa_port *, int);
237 static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
238 static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
239 static void MoxaSetFifo(struct moxa_port *port, int enable);
241 static const struct tty_operations moxa_ops = {
245 .write_room = moxa_write_room,
246 .flush_buffer = moxa_flush_buffer,
247 .chars_in_buffer = moxa_chars_in_buffer,
248 .flush_chars = moxa_flush_chars,
249 .put_char = moxa_put_char,
251 .throttle = moxa_throttle,
252 .unthrottle = moxa_unthrottle,
253 .set_termios = moxa_set_termios,
256 .hangup = moxa_hangup,
257 .tiocmget = moxa_tiocmget,
258 .tiocmset = moxa_tiocmset,
261 static struct tty_driver *moxaDriver;
262 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
263 static DEFINE_SPINLOCK(moxa_lock);
265 static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
267 switch (brd->boardType) {
268 case MOXA_BOARD_C218_ISA:
269 case MOXA_BOARD_C218_PCI:
273 case MOXA_BOARD_CP204J:
287 static int moxa_check_fw(const void *ptr)
289 const __le16 *lptr = ptr;
291 if (*lptr != cpu_to_le16(0x7980))
297 static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
300 void __iomem *baseAddr = brd->basemem;
303 writeb(HW_reset, baseAddr + Control_reg); /* reset */
305 memset_io(baseAddr, 0, 4096);
306 memcpy_toio(baseAddr, buf, len); /* download BIOS */
307 writeb(0, baseAddr + Control_reg); /* restart */
311 switch (brd->boardType) {
312 case MOXA_BOARD_C218_ISA:
313 case MOXA_BOARD_C218_PCI:
314 tmp = readw(baseAddr + C218_key);
315 if (tmp != C218_KeyCode)
318 case MOXA_BOARD_CP204J:
319 tmp = readw(baseAddr + C218_key);
320 if (tmp != CP204J_KeyCode)
324 tmp = readw(baseAddr + C320_key);
325 if (tmp != C320_KeyCode)
327 tmp = readw(baseAddr + C320_status);
328 if (tmp != STS_init) {
329 printk(KERN_ERR "moxa: bios upload failed -- CPU/Basic "
330 "module not found\n");
338 printk(KERN_ERR "moxa: bios upload failed -- board not found\n");
342 static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
345 void __iomem *baseAddr = brd->basemem;
348 printk(KERN_ERR "moxa: invalid 320 bios -- too short\n");
352 writew(len - 7168 - 2, baseAddr + C320bapi_len);
353 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
354 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
355 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
356 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
361 static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
364 void __iomem *baseAddr = brd->basemem;
365 const u16 *uptr = ptr;
366 size_t wlen, len2, j;
367 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
368 unsigned int i, retry, c320;
371 c320 = brd->boardType == MOXA_BOARD_C320_PCI ||
372 brd->boardType == MOXA_BOARD_C320_ISA;
373 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
376 switch (brd->boardType) {
377 case MOXA_BOARD_CP204J:
378 case MOXA_BOARD_C218_ISA:
379 case MOXA_BOARD_C218_PCI:
381 loadbuf = C218_LoadBuf;
382 loadlen = C218DLoad_len;
383 checksum = C218check_sum;
384 checksum_ok = C218chksum_ok;
388 keycode = C320_KeyCode;
389 loadbuf = C320_LoadBuf;
390 loadlen = C320DLoad_len;
391 checksum = C320check_sum;
392 checksum_ok = C320chksum_ok;
398 for (i = 0; i < wlen; i++)
399 usum += le16_to_cpu(uptr[i]);
405 len2 = (wlen > 2048) ? 2048 : wlen;
407 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
410 writew(len2, baseAddr + loadlen);
411 writew(0, baseAddr + key);
412 for (i = 0; i < 100; i++) {
413 if (readw(baseAddr + key) == keycode)
417 if (readw(baseAddr + key) != keycode)
420 writew(0, baseAddr + loadlen);
421 writew(usum, baseAddr + checksum);
422 writew(0, baseAddr + key);
423 for (i = 0; i < 100; i++) {
424 if (readw(baseAddr + key) == keycode)
429 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
430 if (readb(baseAddr + checksum_ok) != 1)
433 writew(0, baseAddr + key);
434 for (i = 0; i < 600; i++) {
435 if (readw(baseAddr + Magic_no) == Magic_code)
439 if (readw(baseAddr + Magic_no) != Magic_code)
443 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
444 writew(0x3800, baseAddr + TMS320_PORT1);
445 writew(0x3900, baseAddr + TMS320_PORT2);
446 writew(28499, baseAddr + TMS320_CLOCK);
448 writew(0x3200, baseAddr + TMS320_PORT1);
449 writew(0x3400, baseAddr + TMS320_PORT2);
450 writew(19999, baseAddr + TMS320_CLOCK);
453 writew(1, baseAddr + Disable_IRQ);
454 writew(0, baseAddr + Magic_no);
455 for (i = 0; i < 500; i++) {
456 if (readw(baseAddr + Magic_no) == Magic_code)
460 if (readw(baseAddr + Magic_no) != Magic_code)
464 j = readw(baseAddr + Module_cnt);
467 brd->numPorts = j * 8;
468 writew(j, baseAddr + Module_no);
469 writew(0, baseAddr + Magic_no);
470 for (i = 0; i < 600; i++) {
471 if (readw(baseAddr + Magic_no) == Magic_code)
475 if (readw(baseAddr + Magic_no) != Magic_code)
478 brd->intNdx = baseAddr + IRQindex;
479 brd->intPend = baseAddr + IRQpending;
480 brd->intTable = baseAddr + IRQtable;
485 static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
488 void __iomem *ofsAddr, *baseAddr = brd->basemem;
489 struct moxa_port *port;
493 printk(KERN_ERR "moxa: bios length is not even\n");
497 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
501 switch (brd->boardType) {
502 case MOXA_BOARD_C218_ISA:
503 case MOXA_BOARD_C218_PCI:
504 case MOXA_BOARD_CP204J:
506 for (i = 0; i < brd->numPorts; i++, port++) {
509 port->tableAddr = baseAddr + Extern_table +
511 ofsAddr = port->tableAddr;
512 writew(C218rx_mask, ofsAddr + RX_mask);
513 writew(C218tx_mask, ofsAddr + TX_mask);
514 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
515 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
517 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
518 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
524 for (i = 0; i < brd->numPorts; i++, port++) {
527 port->tableAddr = baseAddr + Extern_table +
529 ofsAddr = port->tableAddr;
530 switch (brd->numPorts) {
532 writew(C320p8rx_mask, ofsAddr + RX_mask);
533 writew(C320p8tx_mask, ofsAddr + TX_mask);
534 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
535 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
536 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
537 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
541 writew(C320p16rx_mask, ofsAddr + RX_mask);
542 writew(C320p16tx_mask, ofsAddr + TX_mask);
543 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
544 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
545 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
546 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
550 writew(C320p24rx_mask, ofsAddr + RX_mask);
551 writew(C320p24tx_mask, ofsAddr + TX_mask);
552 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
553 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
554 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
555 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
558 writew(C320p32rx_mask, ofsAddr + RX_mask);
559 writew(C320p32tx_mask, ofsAddr + TX_mask);
560 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
561 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
562 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
563 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
564 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
573 static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
575 void *ptr = fw->data;
579 unsigned int a, lenp, lencnt;
582 __le32 magic; /* 0x34303430 */
584 u8 type; /* UNIX = 3 */
585 u8 model; /* C218T=1, C320T=2, CP204=3 */
590 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
592 if (fw->size < MOXA_FW_HDRLEN) {
593 strcpy(rsn, "too short (even header won't fit)");
596 if (hdr->magic != cpu_to_le32(0x30343034)) {
597 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
600 if (hdr->type != 3) {
601 sprintf(rsn, "not for linux, type is %u", hdr->type);
604 if (moxa_check_fw_model(brd, hdr->model)) {
605 sprintf(rsn, "not for this card, model is %u", hdr->model);
609 len = MOXA_FW_HDRLEN;
610 lencnt = hdr->model == 2 ? 5 : 3;
611 for (a = 0; a < ARRAY_SIZE(lens); a++) {
612 lens[a] = le16_to_cpu(hdr->len[a]);
613 if (lens[a] && len + lens[a] <= fw->size &&
614 moxa_check_fw(&fw->data[len]))
615 printk(KERN_WARNING "moxa firmware: unexpected input "
616 "at offset %u, but going on\n", (u32)len);
617 if (!lens[a] && a < lencnt) {
618 sprintf(rsn, "too few entries in fw file");
624 if (len != fw->size) {
625 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
630 ptr += MOXA_FW_HDRLEN;
633 strcpy(rsn, "read above");
635 ret = moxa_load_bios(brd, ptr, lens[lenp]);
639 /* we skip the tty section (lens[1]), since we don't need it */
640 ptr += lens[lenp] + lens[lenp + 1];
641 lenp += 2; /* comm */
643 if (hdr->model == 2) {
644 ret = moxa_load_320b(brd, ptr, lens[lenp]);
647 /* skip another tty */
648 ptr += lens[lenp] + lens[lenp + 1];
652 ret = moxa_load_code(brd, ptr, lens[lenp]);
658 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
662 static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
664 const struct firmware *fw;
670 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
672 if (brd->ports == NULL) {
673 printk(KERN_ERR "cannot allocate memory for ports\n");
678 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
679 p->type = PORT_16550A;
680 p->close_delay = 5 * HZ / 10;
681 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
682 init_waitqueue_head(&p->open_wait);
683 init_completion(&p->close_wait);
685 setup_timer(&p->emptyTimer, moxa_check_xmit_empty,
689 switch (brd->boardType) {
690 case MOXA_BOARD_C218_ISA:
691 case MOXA_BOARD_C218_PCI:
692 file = "c218tunx.cod";
694 case MOXA_BOARD_CP204J:
695 file = "cp204unx.cod";
698 file = "c320tunx.cod";
702 ret = request_firmware(&fw, file, dev);
704 printk(KERN_ERR "request_firmware failed\n");
708 ret = moxa_load_fw(brd, fw);
710 release_firmware(fw);
717 if (!timer_pending(&moxaTimer))
718 mod_timer(&moxaTimer, jiffies + HZ / 50);
727 static void moxa_board_deinit(struct moxa_board_conf *brd)
732 for (i = 0; i < MAX_PORTS_PER_BOARD; i++)
733 del_timer_sync(&brd->ports[i].emptyTimer);
735 iounmap(brd->basemem);
741 static int __devinit moxa_pci_probe(struct pci_dev *pdev,
742 const struct pci_device_id *ent)
744 struct moxa_board_conf *board;
746 int board_type = ent->driver_data;
749 retval = pci_enable_device(pdev);
751 dev_err(&pdev->dev, "can't enable pci device\n");
755 for (i = 0; i < MAX_BOARDS; i++)
756 if (moxa_boards[i].basemem == NULL)
760 if (i >= MAX_BOARDS) {
761 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
762 "found. Board is ignored.\n", MAX_BOARDS);
766 board = &moxa_boards[i];
768 retval = pci_request_region(pdev, 2, "moxa-base");
770 dev_err(&pdev->dev, "can't request pci region 2\n");
774 board->basemem = ioremap(pci_resource_start(pdev, 2), 0x4000);
775 if (board->basemem == NULL) {
776 dev_err(&pdev->dev, "can't remap io space 2\n");
780 board->boardType = board_type;
781 switch (board_type) {
782 case MOXA_BOARD_C218_ISA:
783 case MOXA_BOARD_C218_PCI:
787 case MOXA_BOARD_CP204J:
794 board->busType = MOXA_BUS_TYPE_PCI;
796 retval = moxa_init_board(board, &pdev->dev);
800 pci_set_drvdata(pdev, board);
804 iounmap(board->basemem);
805 board->basemem = NULL;
807 pci_release_region(pdev, 2);
812 static void __devexit moxa_pci_remove(struct pci_dev *pdev)
814 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
816 moxa_board_deinit(brd);
818 pci_release_region(pdev, 2);
821 static struct pci_driver moxa_pci_driver = {
823 .id_table = moxa_pcibrds,
824 .probe = moxa_pci_probe,
825 .remove = __devexit_p(moxa_pci_remove)
827 #endif /* CONFIG_PCI */
829 static int __init moxa_init(void)
831 unsigned int isabrds = 0;
834 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
836 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
840 moxaDriver->owner = THIS_MODULE;
841 moxaDriver->name = "ttyMX";
842 moxaDriver->major = ttymajor;
843 moxaDriver->minor_start = 0;
844 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
845 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
846 moxaDriver->init_termios = tty_std_termios;
847 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
848 moxaDriver->init_termios.c_ispeed = 9600;
849 moxaDriver->init_termios.c_ospeed = 9600;
850 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
851 tty_set_operations(moxaDriver, &moxa_ops);
853 pr_debug("Moxa tty devices major number = %d\n", ttymajor);
855 if (tty_register_driver(moxaDriver)) {
856 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
857 put_tty_driver(moxaDriver);
861 /* Find the boards defined from module args. */
864 struct moxa_board_conf *brd = moxa_boards;
866 for (i = 0; i < MAX_BOARDS; i++) {
869 if (type[i] == MOXA_BOARD_C218_ISA ||
870 type[i] == MOXA_BOARD_C320_ISA) {
871 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
872 isabrds + 1, moxa_brdname[type[i] - 1],
874 brd->boardType = type[i];
875 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
877 brd->busType = MOXA_BUS_TYPE_ISA;
878 brd->basemem = ioremap(baseaddr[i], 0x4000);
880 printk(KERN_ERR "moxa: can't remap %lx\n",
884 if (moxa_init_board(brd, NULL)) {
885 iounmap(brd->basemem);
898 retval = pci_register_driver(&moxa_pci_driver);
900 printk(KERN_ERR "Can't register moxa pci driver!\n");
909 static void __exit moxa_exit(void)
913 del_timer_sync(&moxaTimer);
915 if (tty_unregister_driver(moxaDriver))
916 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
918 put_tty_driver(moxaDriver);
921 pci_unregister_driver(&moxa_pci_driver);
924 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
925 if (moxa_boards[i].ready)
926 moxa_board_deinit(&moxa_boards[i]);
929 module_init(moxa_init);
930 module_exit(moxa_exit);
932 static int moxa_open(struct tty_struct *tty, struct file *filp)
934 struct moxa_board_conf *brd;
935 struct moxa_port *ch;
940 if (port == MAX_PORTS) {
943 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
947 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
949 tty->driver_data = ch;
951 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
953 moxa_set_tty_param(tty, tty->termios);
954 MoxaPortLineCtrl(ch, 1, 1);
956 ch->asyncflags |= ASYNC_INITIALIZED;
958 retval = moxa_block_till_ready(tty, filp, ch);
960 moxa_unthrottle(tty);
962 if (ch->type == PORT_16550A) {
971 static void moxa_close(struct tty_struct *tty, struct file *filp)
973 struct moxa_port *ch;
977 if (port == MAX_PORTS) {
980 if (tty->driver_data == NULL) {
983 if (tty_hung_up_p(filp)) {
986 ch = (struct moxa_port *) tty->driver_data;
988 if ((tty->count == 1) && (ch->count != 1)) {
989 printk(KERN_WARNING "moxa_close: bad serial port count; "
990 "tty->count is 1, ch->count is %d\n", ch->count);
993 if (--ch->count < 0) {
994 printk(KERN_WARNING "moxa_close: bad serial port count, "
995 "device=%s\n", tty->name);
1001 ch->asyncflags |= ASYNC_CLOSING;
1003 ch->cflag = tty->termios->c_cflag;
1004 if (ch->asyncflags & ASYNC_INITIALIZED) {
1005 moxa_setup_empty_event(tty);
1006 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
1007 del_timer_sync(&ch->emptyTimer);
1010 MoxaPortFlushData(ch, 2);
1012 if (tty->driver->flush_buffer)
1013 tty->driver->flush_buffer(tty);
1014 tty_ldisc_flush(tty);
1018 if (ch->blocked_open) {
1019 if (ch->close_delay) {
1020 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
1022 wake_up_interruptible(&ch->open_wait);
1024 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1025 complete_all(&ch->close_wait);
1028 static int moxa_write(struct tty_struct *tty,
1029 const unsigned char *buf, int count)
1031 struct moxa_port *ch = tty->driver_data;
1032 unsigned long flags;
1038 spin_lock_irqsave(&moxa_lock, flags);
1039 len = MoxaPortWriteData(ch, (unsigned char *) buf, count);
1040 spin_unlock_irqrestore(&moxa_lock, flags);
1042 /*********************************************
1043 if ( !(ch->statusflags & LOWWAIT) &&
1044 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
1045 ************************************************/
1046 ch->statusflags |= LOWWAIT;
1050 static int moxa_write_room(struct tty_struct *tty)
1052 struct moxa_port *ch;
1056 ch = tty->driver_data;
1059 return MoxaPortTxFree(ch);
1062 static void moxa_flush_buffer(struct tty_struct *tty)
1064 struct moxa_port *ch = tty->driver_data;
1068 MoxaPortFlushData(ch, 1);
1072 static int moxa_chars_in_buffer(struct tty_struct *tty)
1074 struct moxa_port *ch = tty->driver_data;
1078 * Sigh...I have to check if driver_data is NULL here, because
1079 * if an open() fails, the TTY subsystem eventually calls
1080 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1081 * routine. And since the open() failed, we return 0 here. TDJ
1085 chars = MoxaPortTxQueue(ch);
1088 * Make it possible to wakeup anything waiting for output
1089 * in tty_ioctl.c, etc.
1091 if (!(ch->statusflags & EMPTYWAIT))
1092 moxa_setup_empty_event(tty);
1097 static void moxa_flush_chars(struct tty_struct *tty)
1100 * Don't think I need this, because this is called to empty the TX
1101 * buffer for the 16450, 16550, etc.
1105 static void moxa_put_char(struct tty_struct *tty, unsigned char c)
1107 struct moxa_port *ch = tty->driver_data;
1108 unsigned long flags;
1112 spin_lock_irqsave(&moxa_lock, flags);
1113 MoxaPortWriteData(ch, &c, 1);
1114 spin_unlock_irqrestore(&moxa_lock, flags);
1115 /************************************************
1116 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
1117 *************************************************/
1118 ch->statusflags |= LOWWAIT;
1121 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1123 struct moxa_port *ch = tty->driver_data;
1124 int flag = 0, dtr, rts;
1126 if ((tty->index != MAX_PORTS) && (!ch))
1129 MoxaPortGetLineOut(ch, &dtr, &rts);
1134 dtr = MoxaPortLineStatus(ch);
1144 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1145 unsigned int set, unsigned int clear)
1147 struct moxa_port *ch = tty->driver_data;
1152 if ((port != MAX_PORTS) && (!ch))
1155 MoxaPortGetLineOut(ch, &dtr, &rts);
1156 if (set & TIOCM_RTS)
1158 if (set & TIOCM_DTR)
1160 if (clear & TIOCM_RTS)
1162 if (clear & TIOCM_DTR)
1164 MoxaPortLineCtrl(ch, dtr, rts);
1168 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
1169 unsigned int cmd, unsigned long arg)
1171 struct moxa_port *ch = tty->driver_data;
1173 void __user *argp = (void __user *)arg;
1177 if ((port != MAX_PORTS) && (!ch))
1181 case TCSBRK: /* SVID version: non-zero arg --> no break */
1182 retval = tty_check_change(tty);
1185 moxa_setup_empty_event(tty);
1186 tty_wait_until_sent(tty, 0);
1188 MoxaPortSendBreak(ch, 0);
1190 case TCSBRKP: /* support for POSIX tcsendbreak() */
1191 retval = tty_check_change(tty);
1194 moxa_setup_empty_event(tty);
1195 tty_wait_until_sent(tty, 0);
1196 MoxaPortSendBreak(ch, arg);
1199 return put_user(C_CLOCAL(tty) ? 1 : 0, (int __user *)argp);
1201 if (get_user(retval, (int __user *)argp))
1204 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
1205 (arg ? CLOCAL : 0));
1207 ch->asyncflags &= ~ASYNC_CHECK_CD;
1209 ch->asyncflags |= ASYNC_CHECK_CD;
1212 return moxa_get_serial_info(ch, argp);
1215 return moxa_set_serial_info(ch, argp);
1217 retval = MoxaDriverIoctl(tty, cmd, arg);
1222 static void moxa_throttle(struct tty_struct *tty)
1224 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1226 ch->statusflags |= THROTTLE;
1229 static void moxa_unthrottle(struct tty_struct *tty)
1231 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1233 ch->statusflags &= ~THROTTLE;
1236 static void moxa_set_termios(struct tty_struct *tty,
1237 struct ktermios *old_termios)
1239 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1243 moxa_set_tty_param(tty, old_termios);
1244 if (!(old_termios->c_cflag & CLOCAL) &&
1245 (tty->termios->c_cflag & CLOCAL))
1246 wake_up_interruptible(&ch->open_wait);
1249 static void moxa_stop(struct tty_struct *tty)
1251 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1255 MoxaPortTxDisable(ch);
1256 ch->statusflags |= TXSTOPPED;
1260 static void moxa_start(struct tty_struct *tty)
1262 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1267 if (!(ch->statusflags & TXSTOPPED))
1270 MoxaPortTxEnable(ch);
1271 ch->statusflags &= ~TXSTOPPED;
1274 static void moxa_hangup(struct tty_struct *tty)
1276 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1278 moxa_flush_buffer(tty);
1281 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1283 wake_up_interruptible(&ch->open_wait);
1286 static void moxa_poll(unsigned long ignored)
1288 struct moxa_port *ch;
1289 struct tty_struct *tty;
1293 del_timer(&moxaTimer);
1295 if (MoxaDriverPoll() < 0) {
1296 mod_timer(&moxaTimer, jiffies + HZ / 50);
1300 for (card = 0; card < MAX_BOARDS; card++) {
1301 if (!moxa_boards[card].ready)
1303 ch = moxa_boards[card].ports;
1304 for (i = 0; i < moxa_boards[card].numPorts; i++, ch++) {
1305 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
1307 if (!(ch->statusflags & THROTTLE) &&
1308 (MoxaPortRxQueue(ch) > 0))
1309 moxa_receive_data(ch);
1313 if (ch->statusflags & LOWWAIT) {
1314 if (MoxaPortTxQueue(ch) <= WAKEUP_CHARS) {
1315 if (!tty->stopped) {
1316 ch->statusflags &= ~LOWWAIT;
1321 if (!I_IGNBRK(tty) && (MoxaPortResetBrkCnt(ch) > 0)) {
1322 tty_insert_flip_char(tty, 0, TTY_BREAK);
1323 tty_schedule_flip(tty);
1325 if (MoxaPortDCDChange(ch)) {
1326 if (ch->asyncflags & ASYNC_CHECK_CD) {
1327 if (MoxaPortDCDON(ch))
1328 wake_up_interruptible(&ch->open_wait);
1331 wake_up_interruptible(&ch->open_wait);
1332 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1339 mod_timer(&moxaTimer, jiffies + HZ / 50);
1342 /******************************************************************************/
1344 static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
1346 register struct ktermios *ts;
1347 struct moxa_port *ch;
1348 int rts, cts, txflow, rxflow, xany, baud;
1350 ch = (struct moxa_port *) tty->driver_data;
1352 if (ts->c_cflag & CLOCAL)
1353 ch->asyncflags &= ~ASYNC_CHECK_CD;
1355 ch->asyncflags |= ASYNC_CHECK_CD;
1356 rts = cts = txflow = rxflow = xany = 0;
1357 if (ts->c_cflag & CRTSCTS)
1359 if (ts->c_iflag & IXON)
1361 if (ts->c_iflag & IXOFF)
1363 if (ts->c_iflag & IXANY)
1366 /* Clear the features we don't support */
1367 ts->c_cflag &= ~CMSPAR;
1368 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1369 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
1371 baud = tty_termios_baud_rate(old_termios);
1372 /* Not put the baud rate into the termios data */
1373 tty_encode_baud_rate(tty, baud, baud);
1376 static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
1377 struct moxa_port *ch)
1379 DECLARE_WAITQUEUE(wait,current);
1380 unsigned long flags;
1382 int do_clocal = C_CLOCAL(tty);
1385 * If the device is in the middle of being closed, then block
1386 * until it's done, and then try again.
1388 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
1389 if (ch->asyncflags & ASYNC_CLOSING)
1390 wait_for_completion_interruptible(&ch->close_wait);
1391 #ifdef SERIAL_DO_RESTART
1392 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1395 return (-ERESTARTSYS);
1401 * If non-blocking mode is set, then make the check up front
1404 if (filp->f_flags & O_NONBLOCK) {
1405 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1409 * Block waiting for the carrier detect and the line to become free
1412 add_wait_queue(&ch->open_wait, &wait);
1413 pr_debug("block_til_ready before block: ttys%d, count = %d\n",
1414 tty->index, ch->count);
1415 spin_lock_irqsave(&moxa_lock, flags);
1416 if (!tty_hung_up_p(filp))
1419 spin_unlock_irqrestore(&moxa_lock, flags);
1422 set_current_state(TASK_INTERRUPTIBLE);
1423 if (tty_hung_up_p(filp) ||
1424 !(ch->asyncflags & ASYNC_INITIALIZED)) {
1425 #ifdef SERIAL_DO_RESTART
1426 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1429 retval = -ERESTARTSYS;
1435 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
1439 if (signal_pending(current)) {
1440 retval = -ERESTARTSYS;
1445 set_current_state(TASK_RUNNING);
1446 remove_wait_queue(&ch->open_wait, &wait);
1448 spin_lock_irqsave(&moxa_lock, flags);
1449 if (!tty_hung_up_p(filp))
1452 spin_unlock_irqrestore(&moxa_lock, flags);
1453 pr_debug("block_til_ready after blocking: ttys%d, count = %d\n",
1454 tty->index, ch->count);
1457 /* FIXME: review to see if we need to use set_bit on these */
1458 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1462 static void moxa_setup_empty_event(struct tty_struct *tty)
1464 struct moxa_port *ch = tty->driver_data;
1465 unsigned long flags;
1467 spin_lock_irqsave(&moxa_lock, flags);
1468 ch->statusflags |= EMPTYWAIT;
1469 mod_timer(&ch->emptyTimer, jiffies + HZ);
1470 spin_unlock_irqrestore(&moxa_lock, flags);
1473 static void moxa_check_xmit_empty(unsigned long data)
1475 struct moxa_port *ch;
1477 ch = (struct moxa_port *) data;
1478 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
1479 if (MoxaPortTxQueue(ch) == 0) {
1480 ch->statusflags &= ~EMPTYWAIT;
1481 tty_wakeup(ch->tty);
1484 mod_timer(&ch->emptyTimer, round_jiffies(jiffies + HZ));
1486 ch->statusflags &= ~EMPTYWAIT;
1489 static void moxa_shut_down(struct moxa_port *ch)
1491 struct tty_struct *tp;
1493 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1498 MoxaPortDisable(ch);
1501 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1503 if (tp->termios->c_cflag & HUPCL)
1504 MoxaPortLineCtrl(ch, 0, 0);
1506 ch->asyncflags &= ~ASYNC_INITIALIZED;
1509 static void moxa_receive_data(struct moxa_port *ch)
1511 struct tty_struct *tp;
1512 struct ktermios *ts;
1513 unsigned long flags;
1519 /**************************************************
1520 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1521 *****************************************************/
1523 MoxaPortFlushData(ch, 0);
1526 spin_lock_irqsave(&moxa_lock, flags);
1527 MoxaPortReadData(ch, tp);
1528 spin_unlock_irqrestore(&moxa_lock, flags);
1529 tty_schedule_flip(tp);
1538 int rxcnt[MAX_PORTS];
1539 int txcnt[MAX_PORTS];
1542 #define DCD_changed 0x01
1543 #define DCD_oldstate 0x80
1545 static int moxaLowWaterChk;
1546 static struct mon_str moxaLog;
1547 static int moxaFuncTout = HZ / 2;
1549 static void moxafunc(void __iomem *, int, ushort);
1550 static void moxa_wait_finish(void __iomem *);
1551 static void moxa_low_water_check(void __iomem *);
1553 /*****************************************************************************
1554 * Driver level functions: *
1555 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1556 * 3. MoxaDriverPoll(void); *
1557 *****************************************************************************/
1559 #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1560 #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
1561 #define MOXA_GETDATACOUNT (MOXA + 23)
1562 #define MOXA_GET_IOQUEUE (MOXA + 27)
1563 #define MOXA_FLUSH_QUEUE (MOXA + 28)
1564 #define MOXA_GET_CONF (MOXA + 35) /* configuration */
1565 #define MOXA_GET_MAJOR (MOXA + 63)
1566 #define MOXA_GET_CUMAJOR (MOXA + 64)
1567 #define MOXA_GETMSTATUS (MOXA + 65)
1569 static void MoxaPortFlushData(struct moxa_port *port, int mode)
1571 void __iomem *ofsAddr;
1572 if ((mode < 0) || (mode > 2))
1574 ofsAddr = port->tableAddr;
1575 moxafunc(ofsAddr, FC_FlushQueue, mode);
1577 port->lowChkFlag = 0;
1578 moxa_low_water_check(ofsAddr);
1582 static int MoxaDriverIoctl(struct tty_struct *tty, unsigned int cmd,
1585 struct moxa_port *port = tty->driver_data;
1588 void __user *argp = (void __user *)arg;
1590 if (tty->index == MAX_PORTS) {
1591 if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_GETDATACOUNT) &&
1592 (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
1593 (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
1597 case MOXA_GETDATACOUNT:
1598 moxaLog.tick = jiffies;
1599 if(copy_to_user(argp, &moxaLog, sizeof(struct mon_str)))
1602 case MOXA_FLUSH_QUEUE:
1603 MoxaPortFlushData(port, arg);
1605 case MOXA_GET_IOQUEUE: {
1606 struct moxaq_str __user *argm = argp;
1607 struct moxaq_str tmp;
1608 struct moxa_port *p;
1611 for (i = 0; i < MAX_BOARDS; i++) {
1612 p = moxa_boards[i].ports;
1613 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
1614 memset(&tmp, 0, sizeof(tmp));
1615 if (moxa_boards[i].ready) {
1616 tmp.inq = MoxaPortRxQueue(p);
1617 tmp.outq = MoxaPortTxQueue(p);
1619 if (copy_to_user(argm, &tmp, sizeof(tmp)))
1624 } case MOXA_GET_OQUEUE:
1625 i = MoxaPortTxQueue(port);
1626 return put_user(i, (unsigned long __user *)argp);
1627 case MOXA_GET_IQUEUE:
1628 i = MoxaPortRxQueue(port);
1629 return put_user(i, (unsigned long __user *)argp);
1630 case MOXA_GET_MAJOR:
1631 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1634 case MOXA_GET_CUMAJOR:
1636 if(copy_to_user(argp, &i, sizeof(int)))
1639 case MOXA_GETMSTATUS: {
1640 struct mxser_mstatus __user *argm = argp;
1641 struct mxser_mstatus tmp;
1642 struct moxa_port *p;
1645 for (i = 0; i < MAX_BOARDS; i++) {
1646 p = moxa_boards[i].ports;
1647 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
1648 memset(&tmp, 0, sizeof(tmp));
1649 if (!moxa_boards[i].ready)
1652 status = MoxaPortLineStatus(p);
1660 if (!p->tty || !p->tty->termios)
1661 tmp.cflag = p->cflag;
1663 tmp.cflag = p->tty->termios->c_cflag;
1665 if (copy_to_user(argm, &tmp, sizeof(tmp)))
1673 return -ENOIOCTLCMD;
1676 static int MoxaDriverPoll(void)
1678 struct moxa_board_conf *brd;
1679 struct moxa_port *p;
1680 void __iomem *ofsAddr;
1682 unsigned int port, ports, card;
1685 for (card = 0; card < MAX_BOARDS; card++) {
1686 brd = &moxa_boards[card];
1687 if (brd->ready == 0)
1689 if ((ports = brd->numPorts) == 0)
1691 if (readb(brd->intPend) == 0xff) {
1692 ip = brd->intTable + readb(brd->intNdx);
1695 for (port = 0; port < ports; port += 2, p++) {
1696 temp = readw(ip + port);
1700 writew(0, ip + port);
1701 ofsAddr = p->tableAddr;
1703 writew(readw(ofsAddr + HostStat) &
1704 ~WakeupTx, ofsAddr + HostStat);
1705 if (temp & IntrBreak)
1708 if (temp & IntrLine) {
1709 if (readb(ofsAddr + FlagStat) & DCD_state) {
1710 if ((p->DCDState & DCD_oldstate) == 0)
1711 p->DCDState = (DCD_oldstate |
1714 if (p->DCDState & DCD_oldstate)
1715 p->DCDState = DCD_changed;
1719 writeb(0, brd->intPend);
1721 if (moxaLowWaterChk) {
1723 for (port = 0; port < ports; port++, p++) {
1724 if (p->lowChkFlag) {
1726 ofsAddr = p->tableAddr;
1727 moxa_low_water_check(ofsAddr);
1732 moxaLowWaterChk = 0;
1737 /*****************************************************************************
1738 * Port level functions: *
1739 * 2. MoxaPortEnable(int port); *
1740 * 3. MoxaPortDisable(int port); *
1741 * 4. MoxaPortGetMaxBaud(int port); *
1742 * 6. MoxaPortSetBaud(int port, long baud); *
1743 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1744 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1745 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1746 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1747 * 12. MoxaPortLineStatus(int port); *
1748 * 13. MoxaPortDCDChange(int port); *
1749 * 14. MoxaPortDCDON(int port); *
1750 * 15. MoxaPortFlushData(int port, int mode); *
1751 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1752 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1753 * 20. MoxaPortTxQueue(int port); *
1754 * 21. MoxaPortTxFree(int port); *
1755 * 22. MoxaPortRxQueue(int port); *
1756 * 24. MoxaPortTxDisable(int port); *
1757 * 25. MoxaPortTxEnable(int port); *
1758 * 27. MoxaPortResetBrkCnt(int port); *
1759 * 30. MoxaPortSendBreak(int port, int ticks); *
1760 *****************************************************************************/
1762 * Moxa Port Number Description:
1764 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1765 * the port number using in MOXA driver functions will be 0 to 31 for
1766 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1767 * to 127 for fourth. For example, if you setup three MOXA boards,
1768 * first board is C218, second board is C320-16 and third board is
1769 * C320-32. The port number of first board (C218 - 8 ports) is from
1770 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1771 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1772 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1773 * 127 will be invalid.
1776 * Moxa Functions Description:
1778 * Function 1: Driver initialization routine, this routine must be
1779 * called when initialized driver.
1781 * void MoxaDriverInit();
1784 * Function 2: Moxa driver private IOCTL command processing.
1786 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1788 * unsigned int cmd : IOCTL command
1789 * unsigned long arg : IOCTL argument
1790 * int port : port number (0 - 127)
1797 * Function 3: Moxa driver polling process routine.
1799 * int MoxaDriverPoll(void);
1801 * return: 0 ; polling O.K.
1802 * -1 : no any Moxa card.
1805 * Function 6: Enable this port to start Tx/Rx data.
1807 * void MoxaPortEnable(int port);
1808 * int port : port number (0 - 127)
1811 * Function 7: Disable this port
1813 * void MoxaPortDisable(int port);
1814 * int port : port number (0 - 127)
1817 * Function 8: Get the maximun available baud rate of this port.
1819 * long MoxaPortGetMaxBaud(int port);
1820 * int port : port number (0 - 127)
1822 * return: 0 : this port is invalid
1823 * 38400/57600/115200 bps
1826 * Function 10: Setting baud rate of this port.
1828 * long MoxaPortSetBaud(int port, long baud);
1829 * int port : port number (0 - 127)
1830 * long baud : baud rate (50 - 115200)
1832 * return: 0 : this port is invalid or baud < 50
1833 * 50 - 115200 : the real baud rate set to the port, if
1834 * the argument baud is large than maximun
1835 * available baud rate, the real setting
1836 * baud rate will be the maximun baud rate.
1839 * Function 12: Configure the port.
1841 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1842 * int port : port number (0 - 127)
1843 * struct ktermios * termio : termio structure pointer
1844 * speed_t baud : baud rate
1846 * return: -1 : this port is invalid or termio == NULL
1850 * Function 13: Get the DTR/RTS state of this port.
1852 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1853 * int port : port number (0 - 127)
1854 * int * dtrState : pointer to INT to receive the current DTR
1855 * state. (if NULL, this function will not
1856 * write to this address)
1857 * int * rtsState : pointer to INT to receive the current RTS
1858 * state. (if NULL, this function will not
1859 * write to this address)
1861 * return: -1 : this port is invalid
1865 * Function 14: Setting the DTR/RTS output state of this port.
1867 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1868 * int port : port number (0 - 127)
1869 * int dtrState : DTR output state (0: off, 1: on)
1870 * int rtsState : RTS output state (0: off, 1: on)
1873 * Function 15: Setting the flow control of this port.
1875 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1876 * int txFlow,int xany);
1877 * int port : port number (0 - 127)
1878 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1879 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1880 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1881 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1882 * int xany : S/W XANY flow control (0: no, 1: yes)
1885 * Function 16: Get ths line status of this port
1887 * int MoxaPortLineStatus(int port);
1888 * int port : port number (0 - 127)
1890 * return: Bit 0 - CTS state (0: off, 1: on)
1891 * Bit 1 - DSR state (0: off, 1: on)
1892 * Bit 2 - DCD state (0: off, 1: on)
1895 * Function 17: Check the DCD state has changed since the last read
1898 * int MoxaPortDCDChange(int port);
1899 * int port : port number (0 - 127)
1901 * return: 0 : no changed
1902 * 1 : DCD has changed
1905 * Function 18: Check ths current DCD state is ON or not.
1907 * int MoxaPortDCDON(int port);
1908 * int port : port number (0 - 127)
1910 * return: 0 : DCD off
1914 * Function 19: Flush the Rx/Tx buffer data of this port.
1916 * void MoxaPortFlushData(int port, int mode);
1917 * int port : port number (0 - 127)
1919 * 0 : flush the Rx buffer
1920 * 1 : flush the Tx buffer
1921 * 2 : flush the Rx and Tx buffer
1924 * Function 20: Write data.
1926 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1927 * int port : port number (0 - 127)
1928 * unsigned char * buffer : pointer to write data buffer.
1929 * int length : write data length
1931 * return: 0 - length : real write data length
1934 * Function 21: Read data.
1936 * int MoxaPortReadData(int port, struct tty_struct *tty);
1937 * int port : port number (0 - 127)
1938 * struct tty_struct *tty : tty for data
1940 * return: 0 - length : real read data length
1943 * Function 24: Get the Tx buffer current queued data bytes
1945 * int MoxaPortTxQueue(int port);
1946 * int port : port number (0 - 127)
1948 * return: .. : Tx buffer current queued data bytes
1951 * Function 25: Get the Tx buffer current free space
1953 * int MoxaPortTxFree(int port);
1954 * int port : port number (0 - 127)
1956 * return: .. : Tx buffer current free space
1959 * Function 26: Get the Rx buffer current queued data bytes
1961 * int MoxaPortRxQueue(int port);
1962 * int port : port number (0 - 127)
1964 * return: .. : Rx buffer current queued data bytes
1967 * Function 28: Disable port data transmission.
1969 * void MoxaPortTxDisable(int port);
1970 * int port : port number (0 - 127)
1973 * Function 29: Enable port data transmission.
1975 * void MoxaPortTxEnable(int port);
1976 * int port : port number (0 - 127)
1979 * Function 31: Get the received BREAK signal count and reset it.
1981 * int MoxaPortResetBrkCnt(int port);
1982 * int port : port number (0 - 127)
1984 * return: 0 - .. : BREAK signal count
1987 * Function 34: Send out a BREAK signal.
1989 * void MoxaPortSendBreak(int port, int ms100);
1990 * int port : port number (0 - 127)
1991 * int ms100 : break signal time interval.
1992 * unit: 100 mini-second. if ms100 == 0, it will
1993 * send out a about 250 ms BREAK signal.
1997 static void MoxaPortEnable(struct moxa_port *port)
1999 void __iomem *ofsAddr;
2000 short lowwater = 512;
2002 ofsAddr = port->tableAddr;
2003 writew(lowwater, ofsAddr + Low_water);
2005 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
2006 port->board->boardType == MOXA_BOARD_C320_PCI) {
2007 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
2009 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
2012 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
2013 moxafunc(ofsAddr, FC_FlushQueue, 2);
2015 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
2016 MoxaPortLineStatus(port);
2019 static void MoxaPortDisable(struct moxa_port *port)
2021 void __iomem *ofsAddr = port->tableAddr;
2023 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
2024 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
2025 writew(0, ofsAddr + HostStat);
2026 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
2029 static long MoxaPortGetMaxBaud(struct moxa_port *port)
2031 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
2032 port->board->boardType == MOXA_BOARD_C320_PCI)
2039 static long MoxaPortSetBaud(struct moxa_port *port, long baud)
2041 void __iomem *ofsAddr;
2045 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
2047 ofsAddr = port->tableAddr;
2051 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
2052 else if (max == 57600L)
2053 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
2055 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2057 moxafunc(ofsAddr, FC_SetBaud, val);
2062 static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
2065 void __iomem *ofsAddr;
2069 ofsAddr = port->tableAddr;
2070 cflag = termio->c_cflag; /* termio->c_cflag */
2072 mode = termio->c_cflag & CSIZE;
2075 else if (mode == CS6)
2077 else if (mode == CS7)
2079 else if (mode == CS8)
2082 if (termio->c_cflag & CSTOPB) {
2090 if (termio->c_cflag & PARENB) {
2091 if (termio->c_cflag & PARODD)
2098 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2100 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
2101 port->board->boardType == MOXA_BOARD_C320_PCI) {
2102 if (baud >= 921600L)
2105 baud = MoxaPortSetBaud(port, baud);
2107 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2108 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2109 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2110 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
2111 moxa_wait_finish(ofsAddr);
2117 static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
2122 *dtrState = !!(port->lineCtrl & DTR_ON);
2124 *rtsState = !!(port->lineCtrl & RTS_ON);
2129 static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
2137 port->lineCtrl = mode;
2138 moxafunc(port->tableAddr, FC_LineControl, mode);
2141 static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
2142 int txflow, int rxflow, int txany)
2147 mode |= RTS_FlowCtl;
2149 mode |= CTS_FlowCtl;
2156 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
2159 static int MoxaPortLineStatus(struct moxa_port *port)
2161 void __iomem *ofsAddr;
2164 ofsAddr = port->tableAddr;
2165 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
2166 port->board->boardType == MOXA_BOARD_C320_PCI) {
2167 moxafunc(ofsAddr, FC_LineStatus, 0);
2168 val = readw(ofsAddr + FuncArg);
2170 val = readw(ofsAddr + FlagStat) >> 4;
2175 if ((port->DCDState & DCD_oldstate) == 0)
2176 port->DCDState = (DCD_oldstate | DCD_changed);
2178 if (port->DCDState & DCD_oldstate)
2179 port->DCDState = DCD_changed;
2185 static int MoxaPortDCDChange(struct moxa_port *port)
2190 port->DCDState &= ~DCD_changed;
2195 static int MoxaPortDCDON(struct moxa_port *port)
2199 if (port->DCDState & DCD_oldstate)
2206 static int MoxaPortWriteData(struct moxa_port *port, unsigned char *buffer,
2212 ushort head, tx_mask, spage, epage;
2213 ushort pageno, pageofs, bufhead;
2214 void __iomem *baseAddr, *ofsAddr, *ofs;
2216 ofsAddr = port->tableAddr;
2217 baseAddr = port->board->basemem;
2218 tx_mask = readw(ofsAddr + TX_mask);
2219 spage = readw(ofsAddr + Page_txb);
2220 epage = readw(ofsAddr + EndPage_txb);
2221 tail = readw(ofsAddr + TXwptr);
2222 head = readw(ofsAddr + TXrptr);
2223 c = (head > tail) ? (head - tail - 1)
2224 : (head - tail + tx_mask);
2227 moxaLog.txcnt[port->tty->index] += c;
2229 if (spage == epage) {
2230 bufhead = readw(ofsAddr + Ofs_txb);
2231 writew(spage, baseAddr + Control_reg);
2234 len = head - tail - 1;
2236 len = tx_mask + 1 - tail;
2237 len = (c > len) ? len : c;
2238 ofs = baseAddr + DynPage_addr + bufhead + tail;
2239 for (i = 0; i < len; i++)
2240 writeb(*buffer++, ofs + i);
2241 tail = (tail + len) & tx_mask;
2244 writew(tail, ofsAddr + TXwptr);
2247 pageno = spage + (tail >> 13);
2248 pageofs = tail & Page_mask;
2250 cnt = Page_size - pageofs;
2254 writeb(pageno, baseAddr + Control_reg);
2255 ofs = baseAddr + DynPage_addr + pageofs;
2256 for (i = 0; i < cnt; i++)
2257 writeb(*buffer++, ofs + i);
2259 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2262 if (++pageno == epage)
2267 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2271 static int MoxaPortReadData(struct moxa_port *port, struct tty_struct *tty)
2273 register ushort head, pageofs;
2274 int i, count, cnt, len, total, remain;
2275 ushort tail, rx_mask, spage, epage;
2276 ushort pageno, bufhead;
2277 void __iomem *baseAddr, *ofsAddr, *ofs;
2279 ofsAddr = port->tableAddr;
2280 baseAddr = port->board->basemem;
2281 head = readw(ofsAddr + RXrptr);
2282 tail = readw(ofsAddr + RXwptr);
2283 rx_mask = readw(ofsAddr + RX_mask);
2284 spage = readw(ofsAddr + Page_rxb);
2285 epage = readw(ofsAddr + EndPage_rxb);
2286 count = (tail >= head) ? (tail - head)
2287 : (tail - head + rx_mask + 1);
2292 remain = count - total;
2293 moxaLog.rxcnt[port->tty->index] += total;
2295 if (spage == epage) {
2296 bufhead = readw(ofsAddr + Ofs_rxb);
2297 writew(spage, baseAddr + Control_reg);
2302 len = rx_mask + 1 - head;
2303 len = (count > len) ? len : count;
2304 ofs = baseAddr + DynPage_addr + bufhead + head;
2305 for (i = 0; i < len; i++)
2306 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2307 head = (head + len) & rx_mask;
2310 writew(head, ofsAddr + RXrptr);
2313 pageno = spage + (head >> 13);
2314 pageofs = head & Page_mask;
2316 cnt = Page_size - pageofs;
2320 writew(pageno, baseAddr + Control_reg);
2321 ofs = baseAddr + DynPage_addr + pageofs;
2322 for (i = 0; i < cnt; i++)
2323 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2325 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2328 if (++pageno == epage)
2333 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2334 moxaLowWaterChk = 1;
2335 port->lowChkFlag = 1;
2341 static int MoxaPortTxQueue(struct moxa_port *port)
2343 void __iomem *ofsAddr = port->tableAddr;
2344 ushort rptr, wptr, mask;
2347 rptr = readw(ofsAddr + TXrptr);
2348 wptr = readw(ofsAddr + TXwptr);
2349 mask = readw(ofsAddr + TX_mask);
2350 len = (wptr - rptr) & mask;
2354 static int MoxaPortTxFree(struct moxa_port *port)
2356 void __iomem *ofsAddr = port->tableAddr;
2357 ushort rptr, wptr, mask;
2360 rptr = readw(ofsAddr + TXrptr);
2361 wptr = readw(ofsAddr + TXwptr);
2362 mask = readw(ofsAddr + TX_mask);
2363 len = mask - ((wptr - rptr) & mask);
2367 static int MoxaPortRxQueue(struct moxa_port *port)
2369 void __iomem *ofsAddr = port->tableAddr;
2370 ushort rptr, wptr, mask;
2373 rptr = readw(ofsAddr + RXrptr);
2374 wptr = readw(ofsAddr + RXwptr);
2375 mask = readw(ofsAddr + RX_mask);
2376 len = (wptr - rptr) & mask;
2381 static void MoxaPortTxDisable(struct moxa_port *port)
2383 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
2386 static void MoxaPortTxEnable(struct moxa_port *port)
2388 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
2392 static int MoxaPortResetBrkCnt(struct moxa_port *port)
2395 cnt = port->breakCnt;
2401 static void MoxaPortSendBreak(struct moxa_port *port, int ms100)
2403 void __iomem *ofsAddr = port->tableAddr;
2406 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2409 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2412 moxafunc(ofsAddr, FC_StopBreak, Magic_code);
2415 static int moxa_get_serial_info(struct moxa_port *info,
2416 struct serial_struct __user *retinfo)
2418 struct serial_struct tmp;
2420 memset(&tmp, 0, sizeof(tmp));
2421 tmp.type = info->type;
2422 tmp.line = info->tty->index;
2425 tmp.flags = info->asyncflags;
2426 tmp.baud_base = 921600;
2427 tmp.close_delay = info->close_delay;
2428 tmp.custom_divisor = 0;
2430 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2436 static int moxa_set_serial_info(struct moxa_port *info,
2437 struct serial_struct __user *new_info)
2439 struct serial_struct new_serial;
2441 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2444 if ((new_serial.irq != 0) ||
2445 (new_serial.port != 0) ||
2446 // (new_serial.type != info->type) ||
2447 (new_serial.custom_divisor != 0) ||
2448 (new_serial.baud_base != 921600))
2451 if (!capable(CAP_SYS_ADMIN)) {
2452 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2453 (info->asyncflags & ~ASYNC_USR_MASK)))
2456 info->close_delay = new_serial.close_delay * HZ / 100;
2459 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2460 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2462 if (new_serial.type == PORT_16550A) {
2463 MoxaSetFifo(info, 1);
2465 MoxaSetFifo(info, 0);
2468 info->type = new_serial.type;
2474 /*****************************************************************************
2475 * Static local functions: *
2476 *****************************************************************************/
2477 static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
2480 writew(arg, ofsAddr + FuncArg);
2481 writew(cmd, ofsAddr + FuncCode);
2482 moxa_wait_finish(ofsAddr);
2485 static void moxa_wait_finish(void __iomem *ofsAddr)
2490 while (readw(ofsAddr + FuncCode) != 0) {
2492 if ((j - i) > moxaFuncTout) {
2498 static void moxa_low_water_check(void __iomem *ofsAddr)
2501 ushort rptr, wptr, mask;
2503 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2504 rptr = readw(ofsAddr + RXrptr);
2505 wptr = readw(ofsAddr + RXwptr);
2506 mask = readw(ofsAddr + RX_mask);
2507 len = (wptr - rptr) & mask;
2508 if (len <= Low_water)
2509 moxafunc(ofsAddr, FC_SendXon, 0);
2513 static void MoxaSetFifo(struct moxa_port *port, int enable)
2515 void __iomem *ofsAddr = port->tableAddr;
2518 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2519 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2521 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2522 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);