📄 moxa.c
字号:
#define MOXA_FLUSH_QUEUE (MOXA + 28)#define MOXA_GET_CONF (MOXA + 35) /* configuration */#define MOXA_GET_MAJOR (MOXA + 63)#define MOXA_GET_CUMAJOR (MOXA + 64)#define MOXA_GETMSTATUS (MOXA + 65)struct moxaq_str { int inq; int outq;};struct dl_str { char *buf; int len; int cardno;};static struct moxaq_str temp_queue[MAX_PORTS];static struct dl_str dltmp;void MoxaPortFlushData(int port, int mode){ unsigned long ofsAddr; if ((mode < 0) || (mode > 2)) return; ofsAddr = moxaTableAddr[port]; moxafunc(ofsAddr, FC_FlushQueue, mode); if (mode != 1) { moxaLowChkFlag[port] = 0; low_water_check(ofsAddr); }}void MoxaPortFlushData2(int port, int mode){ unsigned long ofsAddr; if ((mode < 0) || (mode > 2)) return; ofsAddr = moxaTableAddr[port]; moxafunc2(ofsAddr, FC_FlushQueue, mode); if (mode != 1) { moxaLowChkFlag[port] = 0; low_water_check(ofsAddr); }}int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port){ int i, error; int status; int MoxaPortTxQueue(int), MoxaPortRxQueue(int); if (port == QueryPort) { 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: error = verify_area(VERIFY_WRITE, (void *)arg, MAX_BOARDS * sizeof(moxa_board_conf)); if(error) return -EFAULT; copy_to_user((void *)arg,&moxa_boards, MAX_BOARDS*sizeof(moxa_board_conf)); return(0); case MOXA_INIT_DRIVER: if ( (int)arg == 0x404 ) MoxaDriverInit(); return(0); case MOXA_GETDATACOUNT: moxaLog.tick = jiffies; error = verify_area(VERIFY_WRITE, (void *)arg, sizeof(mon_st)); if(error) return -EFAULT; copy_to_user((void *)arg, &moxaLog, sizeof(mon_st)); return(0); case MOXA_FLUSH_QUEUE: MoxaPortFlushData(port,arg); return(0); case MOXA_GET_IOQUEUE: for ( i=0; i<MAX_PORTS; i++ ) { if ( moxaChkPort[i] ) { temp_queue[i].inq = MoxaPortRxQueue(i); temp_queue[i].outq = MoxaPortTxQueue(i); } } error = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct moxaq_str)); if(error) return -EFAULT; copy_to_user((void *)arg,temp_queue, sizeof(struct moxaq_str) * MAX_PORTS); return(0); case MOXA_LOAD_BIOS: error = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct dl_str)); if(error) return -EFAULT; copy_from_user(&dltmp, (void *)arg, sizeof(struct dl_str)); i = moxaloadbios(dltmp.cardno, dltmp.buf, dltmp.len); return(i); case MOXA_FIND_BOARD: error = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct dl_str)); if(error) return -EFAULT; copy_from_user(&dltmp, (void *)arg, sizeof(struct dl_str)); return moxafindcard(dltmp.cardno); case MOXA_LOAD_C320B: error = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct dl_str)); if(error) return -EFAULT; copy_from_user(&dltmp, (void *)arg, sizeof(struct dl_str)); moxaload320b(dltmp.cardno, dltmp.buf, dltmp.len); return(0); case MOXA_LOAD_CODE: error = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct dl_str)); if(error) return -EFAULT; copy_from_user(&dltmp, (void *)arg, sizeof(struct dl_str)); i = moxaloadcode(dltmp.cardno, dltmp.buf, dltmp.len); if ( i == -1 ) return(-EFAULT); return(i); case MOXA_GET_OQUEUE: i = verify_area(VERIFY_WRITE, (void *)arg, sizeof(long)); if ( i ) return(i); i = MoxaPortTxQueue(port); put_to_user(i, (unsigned long *)arg); return 0; case MOXA_GET_IQUEUE: i = verify_area(VERIFY_WRITE, (void *)arg, sizeof(long)); if ( i ) return(i); i = MoxaPortRxQueue(port); put_to_user(i, (unsigned long *)arg); return(0); case MOXA_GET_MAJOR: i = verify_area(VERIFY_WRITE, (void *)arg, sizeof(int)); if ( i ) return(i); copy_to_user((void *)arg,&ttymajor, sizeof(int)); return 0; case MOXA_GET_CUMAJOR: i = verify_area(VERIFY_WRITE, (void *)arg, sizeof(int)); if ( i ) return(i); copy_to_user((void *)arg, &calloutmajor, sizeof(int)); return 0; case MOXA_GETMSTATUS: i = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct mxser_mstatus) * MAX_PORTS); if ( i ) return(i); for(i=0; i<MAX_PORTS; i++){ GMStatus[i].ri = 0; GMStatus[i].dcd = 0; GMStatus[i].dsr = 0; GMStatus[i].cts = 0; if (!moxaChkPort[i]) { continue; } else { status = MoxaPortLineStatus(moxaChannels[i].port); if (status & 1) GMStatus[i].cts = 1; if (status & 2) GMStatus[i].dsr = 1; if (status & 4) GMStatus[i].dcd = 1; } if (!moxaChannels[i].tty || !moxaChannels[i].tty->termios) GMStatus[i].cflag = moxaChannels[i].normal_termios.c_cflag; else GMStatus[i].cflag = moxaChannels[i].tty->termios->c_cflag; } copy_to_user((void *)arg, GMStatus, sizeof(struct mxser_mstatus) * MAX_PORTS); return 0; } return (-ENOIOCTLCMD);}int MoxaDriverPoll(void){ register ushort temp; register int card; unsigned long ip, ofsAddr; int port, p, ports; if (moxaCard == 0) return (-1); for (card = 0; card < MAX_BOARDS; card++) { if(loadstat[card]==0) continue; if ((ports = moxa_boards[card].numPorts) == 0) continue; if (readb(moxaIntPend[card]) == 0xff) { ip = moxaIntTable[card] + readb(moxaIntNdx[card]); 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 = moxaTableAddr[p]; if (temp & IntrTx) writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat); if (temp & IntrBreak) { moxaBreakCnt[p]++; } if (temp & IntrLine) { if (readb(ofsAddr + FlagStat) & DCD_state) { if ((moxaDCDState[p] & DCD_oldstate) == 0) moxaDCDState[p] = (DCD_oldstate | DCD_changed); } else { if (moxaDCDState[p] & DCD_oldstate) moxaDCDState[p] = DCD_changed; } } } } writeb(0, moxaIntPend[card]); } if (moxaLowWaterChk) { p = card * MAX_PORTS_PER_BOARD; for (port = 0; port < ports; port++, p++) { if (moxaLowChkFlag[p]) { moxaLowChkFlag[p] = 0; ofsAddr = moxaTableAddr[p]; 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); * * 5. MoxaPortGetCurBaud(int port); * * 6. MoxaPortSetBaud(int port, long baud); * * 7. MoxaPortSetMode(int port, int databit, int stopbit, int parity); * * 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, unsigned char * buffer, int length); * * 18. MoxaPortTxBufSize(int port); * * 19. MoxaPortRxBufSize(int port); * * 20. MoxaPortTxQueue(int port); * * 21. MoxaPortTxFree(int port); * * 22. MoxaPortRxQueue(int port); * * 23. MoxaPortRxFree(int port); * * 24. MoxaPortTxDisable(int port); * * 25. MoxaPortTxEnable(int port); * * 26. MoxaPortGetBrkCnt(int port); * * 27. MoxaPortResetBrkCnt(int port); * * 28. MoxaPortSetXonXoff(int port, int xonValue, int xoffValue); * * 29. MoxaPortIsTxHold(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 9: Get the current baud rate of this port. * Syntax: * long MoxaPortGetCurBaud(int port); * int port : port number (0 - 127) * * return: 0 : this port is invalid * 50 - 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 11: Setting the data-bits/stop-bits/parity of this port * Syntax: * int MoxaPortSetMode(int port, int databits, int stopbits, int parity); * int port : port number (0 - 127) * int databits : data bits (8/7/6/5) * int stopbits : stop bits (2/1/0, 0 show 1.5 stop bits) int parity : parity (0:None,1:Odd,2:Even,3:Mark,4:Space) * * return: -1 : invalid parameter * 0 : setting O.K. * * * Function 12: Configure the port. * Syntax: * int MoxaPortSetTermio(int port, struct termios *termio); * int port : port number (0 - 127) * struct termios * termio : termio structure pointer * * 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) * Bi
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -