24 #include <linux/module.h>
25 #include <linux/types.h>
28 #include <linux/errno.h>
30 #include <linux/signal.h>
31 #include <linux/sched.h>
34 #include <linux/tty.h>
37 #include <linux/string.h>
38 #include <linux/fcntl.h>
39 #include <linux/ptrace.h>
40 #include <linux/serial.h>
43 #include <linux/pci.h>
45 #include <linux/bitops.h>
46 #include <linux/slab.h>
50 #include <asm/uaccess.h>
54 #define MOXA_VERSION "6.0k"
56 #define MOXA_FW_HDRLEN 32
61 #define MAX_PORTS_PER_BOARD 32
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)
70 #define MOXA_BUS_TYPE_ISA 0
71 #define MOXA_BUS_TYPE_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",
105 static struct moxa_board_conf {
158 #define SERIAL_DO_RESTART
160 #define WAKEUP_CHARS 256
164 static unsigned int moxaFuncTout =
HZ / 2;
165 static unsigned int moxaLowWaterChk;
172 static struct tty_port moxa_service_port;
195 static int moxa_write(
struct tty_struct *,
const unsigned char *,
int);
196 static int moxa_write_room(
struct tty_struct *);
197 static void moxa_flush_buffer(
struct tty_struct *);
198 static int moxa_chars_in_buffer(
struct tty_struct *);
205 unsigned int set,
unsigned int clear);
206 static void moxa_poll(
unsigned long);
208 static void moxa_shutdown(
struct tty_port *);
209 static int moxa_carrier_raised(
struct tty_port *);
210 static void moxa_dtr_rts(
struct tty_port *,
int);
214 static void MoxaPortEnable(
struct moxa_port *);
215 static void MoxaPortDisable(
struct moxa_port *);
217 static int MoxaPortGetLineOut(
struct moxa_port *,
int *,
int *);
218 static void MoxaPortLineCtrl(
struct moxa_port *,
int,
int);
219 static void MoxaPortFlowCtrl(
struct moxa_port *,
int,
int,
int,
int,
int);
220 static int MoxaPortLineStatus(
struct moxa_port *);
221 static void MoxaPortFlushData(
struct moxa_port *,
int);
222 static int MoxaPortWriteData(
struct tty_struct *,
const unsigned char *,
int);
223 static int MoxaPortReadData(
struct moxa_port *);
224 static int MoxaPortTxQueue(
struct moxa_port *);
225 static int MoxaPortRxQueue(
struct moxa_port *);
226 static int MoxaPortTxFree(
struct moxa_port *);
227 static void MoxaPortTxDisable(
struct moxa_port *);
228 static void MoxaPortTxEnable(
struct moxa_port *);
239 static void moxa_wait_finish(
void __iomem *ofsAddr)
256 moxa_wait_finish(ofsAddr);
257 spin_unlock_irqrestore(&moxafunc_lock, flags);
267 moxa_wait_finish(ofsAddr);
269 spin_unlock_irqrestore(&moxafunc_lock, flags);
273 static void moxa_low_water_check(
void __iomem *ofsAddr)
281 len = (wptr -
rptr) & mask;
292 unsigned int cmd,
unsigned long arg)
312 MoxaPortFlushData(ch, arg);
321 p = moxa_boards[
i].ports;
324 spin_lock_bh(&moxa_lock);
325 if (moxa_boards[i].
ready) {
326 tmp.inq = MoxaPortRxQueue(p);
327 tmp.outq = MoxaPortTxQueue(p);
329 spin_unlock_bh(&moxa_lock);
336 status = MoxaPortTxQueue(ch);
337 ret =
put_user(status, (
unsigned long __user *)argp);
340 status = MoxaPortRxQueue(ch);
341 ret =
put_user(status, (
unsigned long __user *)argp);
350 p = moxa_boards[
i].ports;
354 spin_lock_bh(&moxa_lock);
355 if (!moxa_boards[i].ready) {
356 spin_unlock_bh(&moxa_lock);
360 status = MoxaPortLineStatus(p);
361 spin_unlock_bh(&moxa_lock);
372 tmp.cflag = p->cflag;
374 tmp.cflag = ttyp->termios.c_cflag;
385 ret = moxa_get_serial_info(ch, argp);
390 ret = moxa_set_serial_info(ch, argp);
412 .write_room = moxa_write_room,
413 .flush_buffer = moxa_flush_buffer,
414 .chars_in_buffer = moxa_chars_in_buffer,
416 .set_termios = moxa_set_termios,
419 .hangup = moxa_hangup,
420 .break_ctl = moxa_break_ctl,
421 .tiocmget = moxa_tiocmget,
422 .tiocmset = moxa_tiocmset,
426 .carrier_raised = moxa_carrier_raised,
427 .dtr_rts = moxa_dtr_rts,
428 .shutdown = moxa_shutdown,
438 static int moxa_check_fw_model(
struct moxa_board_conf *brd,
u8 model)
440 switch (brd->boardType) {
460 static int moxa_check_fw(
const void *
ptr)
470 static int moxa_load_bios(
struct moxa_board_conf *brd,
const u8 *
buf,
473 void __iomem *baseAddr = brd->basemem;
484 switch (brd->boardType) {
503 "module not found\n");
515 static int moxa_load_320b(
struct moxa_board_conf *brd,
const u8 *ptr,
518 void __iomem *baseAddr = brd->basemem;
534 static int moxa_real_load_code(
struct moxa_board_conf *brd,
const void *ptr,
537 void __iomem *baseAddr = brd->basemem;
539 size_t wlen, len2,
j;
540 unsigned long key, loadbuf, loadlen,
checksum, checksum_ok;
547 switch (brd->boardType) {
569 for (i = 0; i < wlen; i++)
576 len2 = (wlen > 2048) ? 2048 : wlen;
578 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
581 writew(len2, baseAddr + loadlen);
582 writew(0, baseAddr + key);
583 for (i = 0; i < 100; i++) {
584 if (
readw(baseAddr + key) == keycode)
588 if (
readw(baseAddr + key) != keycode)
591 writew(0, baseAddr + loadlen);
592 writew(usum, baseAddr + checksum);
593 writew(0, baseAddr + key);
594 for (i = 0; i < 100; i++) {
595 if (
readw(baseAddr + key) == keycode)
600 }
while ((
readb(baseAddr + checksum_ok) != 1) && (retry < 3));
601 if (
readb(baseAddr + checksum_ok) != 1)
604 writew(0, baseAddr + key);
605 for (i = 0; i < 600; i++) {
626 for (i = 0; i < 500; i++) {
638 brd->numPorts = j * 8;
641 for (i = 0; i < 600; i++) {
651 brd->intTable = baseAddr +
IRQtable;
656 static int moxa_load_code(
struct moxa_board_conf *brd,
const void *ptr,
659 void __iomem *ofsAddr, *baseAddr = brd->basemem;
668 retval = moxa_real_load_code(brd, ptr, len);
672 switch (brd->boardType) {
677 for (i = 0; i < brd->numPorts; i++, port++) {
695 for (i = 0; i < brd->numPorts; i++, port++) {
701 switch (brd->numPorts) {
744 static int moxa_load_fw(
struct moxa_board_conf *brd,
const struct firmware *
fw)
746 const void *ptr = fw->
data;
750 unsigned int a, lenp, lencnt;
764 strcpy(rsn,
"too short (even header won't fit)");
771 if (
hdr->type != 3) {
772 sprintf(rsn,
"not for linux, type is %u",
hdr->type);
775 if (moxa_check_fw_model(brd,
hdr->model)) {
776 sprintf(rsn,
"not for this card, model is %u",
hdr->model);
781 lencnt =
hdr->model == 2 ? 5 : 3;
784 if (lens[a] && len + lens[a] <= fw->
size &&
785 moxa_check_fw(&fw->
data[len]))
787 "at offset %u, but going on\n", (
u32)len);
788 if (!lens[a] && a < lencnt) {
789 sprintf(rsn,
"too few entries in fw file");
795 if (len != fw->
size) {
804 strcpy(rsn,
"read above");
806 ret = moxa_load_bios(brd, ptr, lens[lenp]);
811 ptr += lens[lenp] + lens[lenp + 1];
814 if (
hdr->model == 2) {
815 ret = moxa_load_320b(brd, ptr, lens[lenp]);
819 ptr += lens[lenp] + lens[lenp + 1];
823 ret = moxa_load_code(brd, ptr, lens[lenp]);
833 static int moxa_init_board(
struct moxa_board_conf *brd,
struct device *
dev)
838 unsigned int i, first_idx;
841 brd->ports = kcalloc(MAX_PORTS_PER_BOARD,
sizeof(*brd->ports),
843 if (brd->ports ==
NULL) {
849 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
851 p->
port.ops = &moxa_port_ops;
856 switch (brd->boardType) {
859 file =
"c218tunx.cod";
862 file =
"cp204unx.cod";
865 file =
"c320tunx.cod";
872 "you've placed '%s' file into your firmware "
873 "loader directory (e.g. /lib/firmware)\n",
878 ret = moxa_load_fw(brd, fw);
885 spin_lock_bh(&moxa_lock);
887 if (!timer_pending(&moxaTimer))
889 spin_unlock_bh(&moxa_lock);
891 first_idx = (brd - moxa_boards) * MAX_PORTS_PER_BOARD;
892 for (i = 0; i < brd->numPorts; i++)
903 static void moxa_board_deinit(
struct moxa_board_conf *brd)
905 unsigned int a, opened, first_idx;
908 spin_lock_bh(&moxa_lock);
910 spin_unlock_bh(&moxa_lock);
913 for (a = 0; a < brd->numPorts; a++)
916 &brd->ports[a].port);
924 for (a = 0; a < brd->numPorts; a++)
925 if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
934 first_idx = (brd - moxa_boards) * MAX_PORTS_PER_BOARD;
935 for (a = 0; a < brd->numPorts; a++)
947 struct moxa_board_conf *
board;
954 dev_err(&pdev->
dev,
"can't enable pci device\n");
959 if (moxa_boards[i].basemem ==
NULL)
963 if (i >= MAX_BOARDS) {
964 dev_warn(&pdev->
dev,
"more than %u MOXA Intellio family boards "
965 "found. Board is ignored.\n", MAX_BOARDS);
969 board = &moxa_boards[
i];
973 dev_err(&pdev->
dev,
"can't request pci region 2\n");
978 if (board->basemem ==
NULL) {
979 dev_err(&pdev->
dev,
"can't remap io space 2\n");
985 switch (board_type) {
1000 retval = moxa_init_board(board, &pdev->
dev);
1004 pci_set_drvdata(pdev, board);
1006 dev_info(&pdev->
dev,
"board '%s' ready (%u ports, firmware loaded)\n",
1007 moxa_brdname[board_type - 1], board->numPorts);
1012 board->basemem =
NULL;
1021 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
1023 moxa_board_deinit(brd);
1030 .id_table = moxa_pcibrds,
1031 .probe = moxa_pci_probe,
1036 static int __init moxa_init(
void)
1038 unsigned int isabrds = 0;
1040 struct moxa_board_conf *brd = moxa_boards;
1051 if (IS_ERR(moxaDriver))
1052 return PTR_ERR(moxaDriver);
1054 moxaDriver->
name =
"ttyMX";
1055 moxaDriver->
major = ttymajor;
1080 pr_debug(
"Moxa board %2d: %s board(baseAddr=%lx)\n",
1081 isabrds + 1, moxa_brdname[
type[i] - 1],
1083 brd->boardType =
type[
i];
1088 if (!brd->basemem) {
1093 if (moxa_init_board(brd,
NULL)) {
1095 brd->basemem =
NULL;
1100 "ready (%u ports, firmware loaded)\n",
1101 baseaddr[i], brd->numPorts);
1109 retval = pci_register_driver(&moxa_pci_driver);
1120 static void __exit moxa_exit(
void)
1129 if (moxa_boards[i].ready)
1130 moxa_board_deinit(&moxa_boards[i]);
1143 static void moxa_shutdown(
struct tty_port *port)
1146 MoxaPortDisable(ch);
1147 MoxaPortFlushData(ch, 2);
1150 static int moxa_carrier_raised(
struct tty_port *port)
1155 spin_lock_irq(&port->
lock);
1157 spin_unlock_irq(&port->
lock);
1164 MoxaPortLineCtrl(ch, onoff, onoff);
1168 static int moxa_open(
struct tty_struct *tty,
struct file *filp)
1170 struct moxa_board_conf *brd;
1186 if (port % MAX_PORTS_PER_BOARD >= brd->numPorts) {
1196 if (!(ch->
port.flags & ASYNC_INITIALIZED)) {
1198 moxa_set_tty_param(tty, &tty->termios);
1199 MoxaPortLineCtrl(ch, 1, 1);
1210 static void moxa_close(
struct tty_struct *tty,
struct file *filp)
1213 ch->
cflag = tty->termios.c_cflag;
1217 static int moxa_write(
struct tty_struct *tty,
1218 const unsigned char *buf,
int count)
1221 unsigned long flags;
1228 len = MoxaPortWriteData(tty, buf, count);
1229 spin_unlock_irqrestore(&moxa_lock, flags);
1235 static int moxa_write_room(
struct tty_struct *tty)
1244 return MoxaPortTxFree(ch);
1247 static void moxa_flush_buffer(
struct tty_struct *tty)
1253 MoxaPortFlushData(ch, 1);
1257 static int moxa_chars_in_buffer(
struct tty_struct *tty)
1262 chars = MoxaPortTxQueue(ch);
1272 static int moxa_tiocmget(
struct tty_struct *tty)
1277 MoxaPortGetLineOut(ch, &
dtr, &rts);
1282 dtr = MoxaPortLineStatus(ch);
1292 static int moxa_tiocmset(
struct tty_struct *tty,
1293 unsigned int set,
unsigned int clear)
1305 MoxaPortGetLineOut(ch, &dtr, &rts);
1314 MoxaPortLineCtrl(ch, dtr, rts);
1319 static void moxa_set_termios(
struct tty_struct *tty,
1326 moxa_set_tty_param(tty, old_termios);
1331 static void moxa_stop(
struct tty_struct *tty)
1337 MoxaPortTxDisable(ch);
1342 static void moxa_start(
struct tty_struct *tty)
1352 MoxaPortTxEnable(ch);
1356 static void moxa_hangup(
struct tty_struct *tty)
1362 static void moxa_new_dcdstate(
struct moxa_port *p,
u8 dcd)
1365 unsigned long flags;
1371 spin_unlock_irqrestore(&p->
port.lock, flags);
1378 spin_unlock_irqrestore(&p->
port.lock, flags);
1391 MoxaPortTxQueue(p) == 0) {
1402 MoxaPortRxQueue(p) > 0) {
1403 MoxaPortReadData(p);
1408 MoxaPortFlushData(p, 0);
1428 tty_insert_flip_char(tty, 0,
TTY_BREAK);
1440 static void moxa_poll(
unsigned long ignored)
1442 struct moxa_board_conf *brd;
1444 unsigned int card,
port, served = 0;
1446 spin_lock(&moxa_lock);
1448 brd = &moxa_boards[
card];
1455 if (
readb(brd->intPend) == 0xff)
1456 ip = brd->intTable +
readb(brd->intNdx);
1458 for (port = 0; port < brd->numPorts; port++)
1459 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1464 if (moxaLowWaterChk) {
1466 for (port = 0; port < brd->numPorts; port++, p++)
1473 moxaLowWaterChk = 0;
1477 spin_unlock(&moxa_lock);
1484 register struct ktermios *
ts = &tty->termios;
1486 int rts, cts, txflow, rxflow, xany,
baud;
1488 rts = cts = txflow = rxflow = xany = 0;
1500 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1512 static void MoxaPortFlushData(
struct moxa_port *port,
int mode)
1515 if (mode < 0 || mode > 2)
1521 moxa_low_water_check(ofsAddr);
1717 static void MoxaPortEnable(
struct moxa_port *port)
1734 MoxaPortLineStatus(port);
1737 static void MoxaPortDisable(
struct moxa_port *port)
1776 else if (mode ==
CS6)
1778 else if (mode ==
CS7)
1780 else if (mode ==
CS8)
1804 baud = MoxaPortSetBaud(port, baud);
1807 spin_lock_irq(&moxafunc_lock);
1811 moxa_wait_finish(ofsAddr);
1812 spin_unlock_irq(&moxafunc_lock);
1818 static int MoxaPortGetLineOut(
struct moxa_port *port,
int *dtrState,
1829 static void MoxaPortLineCtrl(
struct moxa_port *port,
int dtr,
int rts)
1841 static void MoxaPortFlowCtrl(
struct moxa_port *port,
int rts,
int cts,
1842 int txflow,
int rxflow,
int txany)
1859 static int MoxaPortLineStatus(
struct moxa_port *port)
1872 moxa_new_dcdstate(port, val & 8);
1877 static int MoxaPortWriteData(
struct tty_struct *tty,
1878 const unsigned char *
buffer,
int len)
1881 void __iomem *baseAddr, *ofsAddr, *ofs;
1882 unsigned int c, total;
1884 u16 pageno, pageofs, bufhead;
1887 baseAddr = port->
board->basemem;
1893 c = (head >
tail) ? (head - tail - 1) : (head - tail +
tx_mask);
1896 moxaLog.txcnt[port->
port.tty->index] +=
c;
1898 if (spage == epage) {
1903 len = head - tail - 1;
1905 len = tx_mask + 1 -
tail;
1906 len = (c > len) ? len : c;
1910 tail = (tail + len) & tx_mask;
1914 pageno = spage + (tail >> 13);
1924 if (++pageno == epage)
1929 tail = (tail + total) & tx_mask;
1936 static int MoxaPortReadData(
struct moxa_port *port)
1940 void __iomem *baseAddr, *ofsAddr, *ofs;
1941 unsigned int count, len, total;
1943 u16 pageno, pageofs, bufhead,
head;
1946 baseAddr = port->
board->basemem;
1952 count = (tail >=
head) ? (tail - head) : (tail - head + rx_mask + 1);
1957 moxaLog.rxcnt[tty->
index] += total;
1958 if (spage == epage) {
1963 len = (tail >=
head) ? (tail - head) :
1964 (rx_mask + 1 -
head);
1968 head = (head + len) & rx_mask;
1972 pageno = spage + (head >> 13);
1982 pageofs = (pageofs + len) & Page_mask;
1983 if (pageofs == 0 && ++pageno == epage)
1986 head = (head + total) & rx_mask;
1990 moxaLowWaterChk = 1;
1997 static int MoxaPortTxQueue(
struct moxa_port *port)
2005 return (wptr - rptr) &
mask;
2008 static int MoxaPortTxFree(
struct moxa_port *port)
2016 return mask - ((wptr -
rptr) & mask);
2019 static int MoxaPortRxQueue(
struct moxa_port *port)
2027 return (wptr - rptr) &
mask;
2030 static void MoxaPortTxDisable(
struct moxa_port *port)
2035 static void MoxaPortTxEnable(
struct moxa_port *port)
2045 .line = info->
port.tty->index,
2046 .flags = info->
port.flags,
2047 .baud_base = 921600,
2048 .close_delay = info->
port.close_delay
2054 static int moxa_set_serial_info(
struct moxa_port *info,
2062 if (new_serial.irq != 0 || new_serial.port != 0 ||
2063 new_serial.custom_divisor != 0 ||
2064 new_serial.baud_base != 921600)
2072 info->
port.close_delay = new_serial.close_delay *
HZ / 100;
2074 new_serial.flags = (new_serial.flags & ~
ASYNC_FLAGS);
2079 info->
type = new_serial.type;