📄 moxa.c
字号:
{ int i; int status; int MoxaPortTxQueue(int), MoxaPortRxQueue(int); void __user *argp = (void __user *)arg; if (port == MAX_PORTS) { if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_INIT_DRIVER) && (cmd != MOXA_LOAD_BIOS) && (cmd != MOXA_FIND_BOARD) && (cmd != MOXA_LOAD_C320B) && (cmd != MOXA_LOAD_CODE) && (cmd != MOXA_GETDATACOUNT) && (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) && (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS)) return (-EINVAL); } switch (cmd) { case MOXA_GET_CONF: if(copy_to_user(argp, &moxa_boards, MAX_BOARDS * sizeof(struct moxa_board_conf))) return -EFAULT; return (0); case MOXA_INIT_DRIVER: if ((int) arg == 0x404) MoxaDriverInit(); return (0); case MOXA_GETDATACOUNT: moxaLog.tick = jiffies; if(copy_to_user(argp, &moxaLog, sizeof(struct mon_str))) return -EFAULT; return (0); case MOXA_FLUSH_QUEUE: MoxaPortFlushData(port, arg); return (0); case MOXA_GET_IOQUEUE: { struct moxaq_str __user *argm = argp; struct moxaq_str tmp; for (i = 0; i < MAX_PORTS; i++, argm++) { memset(&tmp, 0, sizeof(tmp)); if (moxa_ports[i].chkPort) { tmp.inq = MoxaPortRxQueue(i); tmp.outq = MoxaPortTxQueue(i); } if (copy_to_user(argm, &tmp, sizeof(tmp))) return -EFAULT; } return (0); } case MOXA_GET_OQUEUE: i = MoxaPortTxQueue(port); return put_user(i, (unsigned long __user *)argp); case MOXA_GET_IQUEUE: i = MoxaPortRxQueue(port); return put_user(i, (unsigned long __user *)argp); case MOXA_GET_MAJOR: if(copy_to_user(argp, &ttymajor, sizeof(int))) return -EFAULT; return 0; case MOXA_GET_CUMAJOR: i = 0; if(copy_to_user(argp, &i, sizeof(int))) return -EFAULT; return 0; case MOXA_GETMSTATUS: { struct mxser_mstatus __user *argm = argp; struct mxser_mstatus tmp; struct moxa_port *p; for (i = 0; i < MAX_PORTS; i++, argm++) { p = &moxa_ports[i]; memset(&tmp, 0, sizeof(tmp)); if (!p->chkPort) { goto copy; } else { status = MoxaPortLineStatus(p->port); if (status & 1) tmp.cts = 1; if (status & 2) tmp.dsr = 1; if (status & 4) tmp.dcd = 1; } if (!p->tty || !p->tty->termios) tmp.cflag = p->cflag; else tmp.cflag = p->tty->termios->c_cflag;copy: if (copy_to_user(argm, &tmp, sizeof(tmp))) return -EFAULT; } return 0; } default: return (-ENOIOCTLCMD); case MOXA_LOAD_BIOS: case MOXA_FIND_BOARD: case MOXA_LOAD_C320B: case MOXA_LOAD_CODE: if (!capable(CAP_SYS_RAWIO)) return -EPERM; break; } if(copy_from_user(&dltmp, argp, sizeof(struct dl_str))) return -EFAULT; if(dltmp.cardno < 0 || dltmp.cardno >= MAX_BOARDS || dltmp.len < 0) return -EINVAL; switch(cmd) { case MOXA_LOAD_BIOS: i = moxaloadbios(dltmp.cardno, dltmp.buf, dltmp.len); return (i); case MOXA_FIND_BOARD: return moxafindcard(dltmp.cardno); case MOXA_LOAD_C320B: moxaload320b(dltmp.cardno, dltmp.buf, dltmp.len); default: /* to keep gcc happy */ return (0); case MOXA_LOAD_CODE: i = moxaloadcode(dltmp.cardno, dltmp.buf, dltmp.len); if (i == -1) return (-EFAULT); return (i); }}int MoxaDriverPoll(void){ struct moxa_board_conf *brd; register ushort temp; register int card; void __iomem *ofsAddr; void __iomem *ip; int port, p, ports; if (moxaCard == 0) return (-1); for (card = 0; card < MAX_BOARDS; card++) { brd = &moxa_boards[card]; if (brd->loadstat == 0) continue; if ((ports = brd->numPorts) == 0) continue; if (readb(brd->intPend) == 0xff) { ip = brd->intTable + readb(brd->intNdx); p = card * MAX_PORTS_PER_BOARD; ports <<= 1; for (port = 0; port < ports; port += 2, p++) { if ((temp = readw(ip + port)) != 0) { writew(0, ip + port); ofsAddr = moxa_ports[p].tableAddr; if (temp & IntrTx) writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat); if (temp & IntrBreak) { moxa_ports[p].breakCnt++; } if (temp & IntrLine) { if (readb(ofsAddr + FlagStat) & DCD_state) { if ((moxa_ports[p].DCDState & DCD_oldstate) == 0) moxa_ports[p].DCDState = (DCD_oldstate | DCD_changed); } else { if (moxa_ports[p].DCDState & DCD_oldstate) moxa_ports[p].DCDState = DCD_changed; } } } } writeb(0, brd->intPend); } if (moxaLowWaterChk) { p = card * MAX_PORTS_PER_BOARD; for (port = 0; port < ports; port++, p++) { if (moxa_ports[p].lowChkFlag) { moxa_ports[p].lowChkFlag = 0; ofsAddr = moxa_ports[p].tableAddr; moxa_low_water_check(ofsAddr); } } } } moxaLowWaterChk = 0; return (0);}/***************************************************************************** * Card level function: * * 1. MoxaPortsOfCard(int cardno); * *****************************************************************************/int MoxaPortsOfCard(int cardno){ if (moxa_boards[cardno].boardType == 0) return (0); return (moxa_boards[cardno].numPorts);}/***************************************************************************** * Port level functions: * * 1. MoxaPortIsValid(int port); * * 2. MoxaPortEnable(int port); * * 3. MoxaPortDisable(int port); * * 4. MoxaPortGetMaxBaud(int port); * * 6. MoxaPortSetBaud(int port, long baud); * * 8. MoxaPortSetTermio(int port, unsigned char *termio); * * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); * * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); * * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); * * 12. MoxaPortLineStatus(int port); * * 13. MoxaPortDCDChange(int port); * * 14. MoxaPortDCDON(int port); * * 15. MoxaPortFlushData(int port, int mode); * * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); * * 17. MoxaPortReadData(int port, struct tty_struct *tty); * * 20. MoxaPortTxQueue(int port); * * 21. MoxaPortTxFree(int port); * * 22. MoxaPortRxQueue(int port); * * 24. MoxaPortTxDisable(int port); * * 25. MoxaPortTxEnable(int port); * * 27. MoxaPortResetBrkCnt(int port); * * 30. MoxaPortSendBreak(int port, int ticks); * *****************************************************************************//* * Moxa Port Number Description: * * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And, * the port number using in MOXA driver functions will be 0 to 31 for * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96 * to 127 for fourth. For example, if you setup three MOXA boards, * first board is C218, second board is C320-16 and third board is * C320-32. The port number of first board (C218 - 8 ports) is from * 0 to 7. The port number of second board (C320 - 16 ports) is form * 32 to 47. The port number of third board (C320 - 32 ports) is from * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to * 127 will be invalid. * * * Moxa Functions Description: * * Function 1: Driver initialization routine, this routine must be * called when initialized driver. * Syntax: * void MoxaDriverInit(); * * * Function 2: Moxa driver private IOCTL command processing. * Syntax: * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); * * unsigned int cmd : IOCTL command * unsigned long arg : IOCTL argument * int port : port number (0 - 127) * * return: 0 (OK) * -EINVAL * -ENOIOCTLCMD * * * Function 3: Moxa driver polling process routine. * Syntax: * int MoxaDriverPoll(void); * * return: 0 ; polling O.K. * -1 : no any Moxa card. * * * Function 4: Get the ports of this card. * Syntax: * int MoxaPortsOfCard(int cardno); * * int cardno : card number (0 - 3) * * return: 0 : this card is invalid * 8/16/24/32 * * * Function 5: Check this port is valid or invalid * Syntax: * int MoxaPortIsValid(int port); * int port : port number (0 - 127, ref port description) * * return: 0 : this port is invalid * 1 : this port is valid * * * Function 6: Enable this port to start Tx/Rx data. * Syntax: * void MoxaPortEnable(int port); * int port : port number (0 - 127) * * * Function 7: Disable this port * Syntax: * void MoxaPortDisable(int port); * int port : port number (0 - 127) * * * Function 8: Get the maximun available baud rate of this port. * Syntax: * long MoxaPortGetMaxBaud(int port); * int port : port number (0 - 127) * * return: 0 : this port is invalid * 38400/57600/115200 bps * * * Function 10: Setting baud rate of this port. * Syntax: * long MoxaPortSetBaud(int port, long baud); * int port : port number (0 - 127) * long baud : baud rate (50 - 115200) * * return: 0 : this port is invalid or baud < 50 * 50 - 115200 : the real baud rate set to the port, if * the argument baud is large than maximun * available baud rate, the real setting * baud rate will be the maximun baud rate. * * * Function 12: Configure the port. * Syntax: * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud); * int port : port number (0 - 127) * struct ktermios * termio : termio structure pointer * speed_t baud : baud rate * * return: -1 : this port is invalid or termio == NULL * 0 : setting O.K. * * * Function 13: Get the DTR/RTS state of this port. * Syntax: * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); * int port : port number (0 - 127) * int * dtrState : pointer to INT to receive the current DTR * state. (if NULL, this function will not * write to this address) * int * rtsState : pointer to INT to receive the current RTS * state. (if NULL, this function will not * write to this address) * * return: -1 : this port is invalid * 0 : O.K. * * * Function 14: Setting the DTR/RTS output state of this port. * Syntax: * void MoxaPortLineCtrl(int port, int dtrState, int rtsState); * int port : port number (0 - 127) * int dtrState : DTR output state (0: off, 1: on) * int rtsState : RTS output state (0: off, 1: on) * * * Function 15: Setting the flow control of this port. * Syntax: * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow, * int txFlow,int xany); * int port : port number (0 - 127) * int rtsFlow : H/W RTS flow control (0: no, 1: yes) * int ctsFlow : H/W CTS flow control (0: no, 1: yes) * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes) * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes) * int xany : S/W XANY flow control (0: no, 1: yes) * * * Function 16: Get ths line status of this port * Syntax: * int MoxaPortLineStatus(int port); * int port : port number (0 - 127) * * return: Bit 0 - CTS state (0: off, 1: on) * Bit 1 - DSR state (0: off, 1: on) * Bit 2 - DCD state (0: off, 1: on) * * * Function 17: Check the DCD state has changed since the last read * of this function. * Syntax: * int MoxaPortDCDChange(int port); * int port : port number (0 - 127) * * return: 0 : no changed * 1 : DCD has changed * * * Function 18: Check ths current DCD state is ON or not. * Syntax: * int MoxaPortDCDON(int port); * int port : port number (0 - 127) * * return: 0 : DCD off * 1 : DCD on * * * Function 19: Flush the Rx/Tx buffer data of this port. * Syntax: * void MoxaPortFlushData(int port, int mode); * int port : port number (0 - 127) * int mode * 0 : flush the Rx buffer * 1 : flush the Tx buffer * 2 : flush the Rx and Tx buffer * * * Function 20: Write data. * Syntax: * int MoxaPortWriteData(int port, unsigned char * buffer, int length); * int port : port number (0 - 127) * unsigned char * buffer : pointer to write data buffer. * int length : write data length * * return: 0 - length : real write data length * * * Function 21: Read data. * Syntax: * int MoxaPortReadData(int port, struct tty_struct *tty); * int port : port number (0 - 127) * struct tty_struct *tty : tty for data * * return: 0 - length : real read data length * * * Function 24: Get the Tx buffer current queued data bytes * Syntax: * int MoxaPortTxQueue(int port); * int port : port number (0 - 127) * * return: .. : Tx buffer current queued data bytes * * * Function 25: Get the Tx buffer current free space * Syntax: * int MoxaPortTxFree(int port); * int port : port number (0 - 127) * * return: .. : Tx buffer current free space * * * Function 26: Get the Rx buffer current queued data bytes * Syntax: * int MoxaPortRxQueue(int port); * int port : port number (0 - 127) * * return: .. : Rx buffer current queued data bytes * * * Function 28: Disable port data transmission. * Syntax: * void MoxaPortTxDisable(int port); * int port : port number (0 - 127) * * * Function 29: Enable port data transmission. * Syntax: * void MoxaPortTxEnable(int port); * int port : port number (0 - 127) * * * Function 31: Get the received BREAK signal count and reset it. * Syntax: * int MoxaPortResetBrkCnt(int port); * int port : port number (0 - 127) * * return: 0 - .. : BREAK signal count * * * Function 34: Send out a BREAK signal. * Syntax: * void MoxaPortSendBreak(int port, int ms100); * int port : port number (0 - 127) * int ms100 : break signal time interval. * unit: 100 mini-second. if ms100 == 0, it will * send out a about 250 ms BREAK signal. * */int MoxaPortIsValid(int port){ if (moxaCard == 0) return (0); if (moxa_ports[port].chkPort == 0) return (0); return (1);}void MoxaPortEnable(int port){ void __iomem *ofsAddr; int MoxaPortLineStatus(int);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -