rocket.c
来自「linux 内核源代码」· C语言 代码 · 共 2,326 行 · 第 1/5 页
C
2,326 行
/* * RocketPort device driver for Linux * * Written by Theodore Ts'o, 1995, 1996, 1997, 1998, 1999, 2000. * * Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2003 by Comtrol, Inc. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* * Kernel Synchronization: * * This driver has 2 kernel control paths - exception handlers (calls into the driver * from user mode) and the timer bottom half (tasklet). This is a polled driver, interrupts * are not used. * * Critical data: * - rp_table[], accessed through passed "info" pointers, is a global (static) array of * serial port state information and the xmit_buf circular buffer. Protected by * a per port spinlock. * - xmit_flags[], an array of ints indexed by line (port) number, indicating that there * is data to be transmitted. Protected by atomic bit operations. * - rp_num_ports, int indicating number of open ports, protected by atomic operations. * * rp_write() and rp_write_char() functions use a per port semaphore to protect against * simultaneous access to the same port by more than one process. *//****** Defines ******/#ifdef PCI_NUM_RESOURCES#define PCI_BASE_ADDRESS(dev, r) ((dev)->resource[r].start)#else#define PCI_BASE_ADDRESS(dev, r) ((dev)->base_address[r])#endif#define ROCKET_PARANOIA_CHECK#define ROCKET_DISABLE_SIMUSAGE#undef ROCKET_SOFT_FLOW#undef ROCKET_DEBUG_OPEN#undef ROCKET_DEBUG_INTR#undef ROCKET_DEBUG_WRITE#undef ROCKET_DEBUG_FLOW#undef ROCKET_DEBUG_THROTTLE#undef ROCKET_DEBUG_WAIT_UNTIL_SENT#undef ROCKET_DEBUG_RECEIVE#undef ROCKET_DEBUG_HANGUP#undef REV_PCI_ORDER#undef ROCKET_DEBUG_IO#define POLL_PERIOD HZ/100 /* Polling period .01 seconds (10ms) *//****** Kernel includes ******/#include <linux/module.h>#include <linux/errno.h>#include <linux/major.h>#include <linux/kernel.h>#include <linux/signal.h>#include <linux/slab.h>#include <linux/mm.h>#include <linux/sched.h>#include <linux/timer.h>#include <linux/interrupt.h>#include <linux/tty.h>#include <linux/tty_driver.h>#include <linux/tty_flip.h>#include <linux/string.h>#include <linux/fcntl.h>#include <linux/ptrace.h>#include <linux/mutex.h>#include <linux/ioport.h>#include <linux/delay.h>#include <linux/completion.h>#include <linux/wait.h>#include <linux/pci.h>#include <asm/uaccess.h>#include <asm/atomic.h>#include <linux/bitops.h>#include <linux/spinlock.h>#include <linux/init.h>/****** RocketPort includes ******/#include "rocket_int.h"#include "rocket.h"#define ROCKET_VERSION "2.09"#define ROCKET_DATE "12-June-2003"/****** RocketPort Local Variables ******/static void rp_do_poll(unsigned long dummy);static struct tty_driver *rocket_driver;static struct rocket_version driver_version = { ROCKET_VERSION, ROCKET_DATE};static struct r_port *rp_table[MAX_RP_PORTS]; /* The main repository of serial port state information. */static unsigned int xmit_flags[NUM_BOARDS]; /* Bit significant, indicates port had data to transmit. */ /* eg. Bit 0 indicates port 0 has xmit data, ... */static atomic_t rp_num_ports_open; /* Number of serial ports open */static DEFINE_TIMER(rocket_timer, rp_do_poll, 0, 0);static unsigned long board1; /* ISA addresses, retrieved from rocketport.conf */static unsigned long board2;static unsigned long board3;static unsigned long board4;static unsigned long controller;static int support_low_speed;static unsigned long modem1;static unsigned long modem2;static unsigned long modem3;static unsigned long modem4;static unsigned long pc104_1[8];static unsigned long pc104_2[8];static unsigned long pc104_3[8];static unsigned long pc104_4[8];static unsigned long *pc104[4] = { pc104_1, pc104_2, pc104_3, pc104_4 };static int rp_baud_base[NUM_BOARDS]; /* Board config info (Someday make a per-board structure) */static unsigned long rcktpt_io_addr[NUM_BOARDS];static int rcktpt_type[NUM_BOARDS];static int is_PCI[NUM_BOARDS];static rocketModel_t rocketModel[NUM_BOARDS];static int max_board;/* * The following arrays define the interrupt bits corresponding to each AIOP. * These bits are different between the ISA and regular PCI boards and the * Universal PCI boards. */static Word_t aiop_intr_bits[AIOP_CTL_SIZE] = { AIOP_INTR_BIT_0, AIOP_INTR_BIT_1, AIOP_INTR_BIT_2, AIOP_INTR_BIT_3};static Word_t upci_aiop_intr_bits[AIOP_CTL_SIZE] = { UPCI_AIOP_INTR_BIT_0, UPCI_AIOP_INTR_BIT_1, UPCI_AIOP_INTR_BIT_2, UPCI_AIOP_INTR_BIT_3};static Byte_t RData[RDATASIZE] = { 0x00, 0x09, 0xf6, 0x82, 0x02, 0x09, 0x86, 0xfb, 0x04, 0x09, 0x00, 0x0a, 0x06, 0x09, 0x01, 0x0a, 0x08, 0x09, 0x8a, 0x13, 0x0a, 0x09, 0xc5, 0x11, 0x0c, 0x09, 0x86, 0x85, 0x0e, 0x09, 0x20, 0x0a, 0x10, 0x09, 0x21, 0x0a, 0x12, 0x09, 0x41, 0xff, 0x14, 0x09, 0x82, 0x00, 0x16, 0x09, 0x82, 0x7b, 0x18, 0x09, 0x8a, 0x7d, 0x1a, 0x09, 0x88, 0x81, 0x1c, 0x09, 0x86, 0x7a, 0x1e, 0x09, 0x84, 0x81, 0x20, 0x09, 0x82, 0x7c, 0x22, 0x09, 0x0a, 0x0a};static Byte_t RRegData[RREGDATASIZE] = { 0x00, 0x09, 0xf6, 0x82, /* 00: Stop Rx processor */ 0x08, 0x09, 0x8a, 0x13, /* 04: Tx software flow control */ 0x0a, 0x09, 0xc5, 0x11, /* 08: XON char */ 0x0c, 0x09, 0x86, 0x85, /* 0c: XANY */ 0x12, 0x09, 0x41, 0xff, /* 10: Rx mask char */ 0x14, 0x09, 0x82, 0x00, /* 14: Compare/Ignore #0 */ 0x16, 0x09, 0x82, 0x7b, /* 18: Compare #1 */ 0x18, 0x09, 0x8a, 0x7d, /* 1c: Compare #2 */ 0x1a, 0x09, 0x88, 0x81, /* 20: Interrupt #1 */ 0x1c, 0x09, 0x86, 0x7a, /* 24: Ignore/Replace #1 */ 0x1e, 0x09, 0x84, 0x81, /* 28: Interrupt #2 */ 0x20, 0x09, 0x82, 0x7c, /* 2c: Ignore/Replace #2 */ 0x22, 0x09, 0x0a, 0x0a /* 30: Rx FIFO Enable */};static CONTROLLER_T sController[CTL_SIZE] = { {-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0}, {-1, -1, -1, -1}, {0, 0, 0, 0}}, {-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0}, {-1, -1, -1, -1}, {0, 0, 0, 0}}, {-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0}, {-1, -1, -1, -1}, {0, 0, 0, 0}}, {-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0}, {-1, -1, -1, -1}, {0, 0, 0, 0}}};static Byte_t sBitMapClrTbl[8] = { 0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f};static Byte_t sBitMapSetTbl[8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};static int sClockPrescale = 0x14;/* * Line number is the ttySIx number (x), the Minor number. We * assign them sequentially, starting at zero. The following * array keeps track of the line number assigned to a given board/aiop/channel. */static unsigned char lineNumbers[MAX_RP_PORTS];static unsigned long nextLineNumber;/***** RocketPort Static Prototypes *********/static int __init init_ISA(int i);static void rp_wait_until_sent(struct tty_struct *tty, int timeout);static void rp_flush_buffer(struct tty_struct *tty);static void rmSpeakerReset(CONTROLLER_T * CtlP, unsigned long model);static unsigned char GetLineNumber(int ctrl, int aiop, int ch);static unsigned char SetLineNumber(int ctrl, int aiop, int ch);static void rp_start(struct tty_struct *tty);static int sInitChan(CONTROLLER_T * CtlP, CHANNEL_T * ChP, int AiopNum, int ChanNum);static void sSetInterfaceMode(CHANNEL_T * ChP, Byte_t mode);static void sFlushRxFIFO(CHANNEL_T * ChP);static void sFlushTxFIFO(CHANNEL_T * ChP);static void sEnInterrupts(CHANNEL_T * ChP, Word_t Flags);static void sDisInterrupts(CHANNEL_T * ChP, Word_t Flags);static void sModemReset(CONTROLLER_T * CtlP, int chan, int on);static void sPCIModemReset(CONTROLLER_T * CtlP, int chan, int on);static int sWriteTxPrioByte(CHANNEL_T * ChP, Byte_t Data);static int sPCIInitController(CONTROLLER_T * CtlP, int CtlNum, ByteIO_t * AiopIOList, int AiopIOListSize, WordIO_t ConfigIO, int IRQNum, Byte_t Frequency, int PeriodicOnly, int altChanRingIndicator, int UPCIRingInd);static int sInitController(CONTROLLER_T * CtlP, int CtlNum, ByteIO_t MudbacIO, ByteIO_t * AiopIOList, int AiopIOListSize, int IRQNum, Byte_t Frequency, int PeriodicOnly);static int sReadAiopID(ByteIO_t io);static int sReadAiopNumChan(WordIO_t io);MODULE_AUTHOR("Theodore Ts'o");MODULE_DESCRIPTION("Comtrol RocketPort driver");module_param(board1, ulong, 0);MODULE_PARM_DESC(board1, "I/O port for (ISA) board #1");module_param(board2, ulong, 0);MODULE_PARM_DESC(board2, "I/O port for (ISA) board #2");module_param(board3, ulong, 0);MODULE_PARM_DESC(board3, "I/O port for (ISA) board #3");module_param(board4, ulong, 0);MODULE_PARM_DESC(board4, "I/O port for (ISA) board #4");module_param(controller, ulong, 0);MODULE_PARM_DESC(controller, "I/O port for (ISA) rocketport controller");module_param(support_low_speed, bool, 0);MODULE_PARM_DESC(support_low_speed, "1 means support 50 baud, 0 means support 460400 baud");module_param(modem1, ulong, 0);MODULE_PARM_DESC(modem1, "1 means (ISA) board #1 is a RocketModem");module_param(modem2, ulong, 0);MODULE_PARM_DESC(modem2, "1 means (ISA) board #2 is a RocketModem");module_param(modem3, ulong, 0);MODULE_PARM_DESC(modem3, "1 means (ISA) board #3 is a RocketModem");module_param(modem4, ulong, 0);MODULE_PARM_DESC(modem4, "1 means (ISA) board #4 is a RocketModem");module_param_array(pc104_1, ulong, NULL, 0);MODULE_PARM_DESC(pc104_1, "set interface types for ISA(PC104) board #1 (e.g. pc104_1=232,232,485,485,...");module_param_array(pc104_2, ulong, NULL, 0);MODULE_PARM_DESC(pc104_2, "set interface types for ISA(PC104) board #2 (e.g. pc104_2=232,232,485,485,...");module_param_array(pc104_3, ulong, NULL, 0);MODULE_PARM_DESC(pc104_3, "set interface types for ISA(PC104) board #3 (e.g. pc104_3=232,232,485,485,...");module_param_array(pc104_4, ulong, NULL, 0);MODULE_PARM_DESC(pc104_4, "set interface types for ISA(PC104) board #4 (e.g. pc104_4=232,232,485,485,...");static int rp_init(void);static void rp_cleanup_module(void);module_init(rp_init);module_exit(rp_cleanup_module);MODULE_LICENSE("Dual BSD/GPL");/*************************************************************************//* Module code starts here */static inline int rocket_paranoia_check(struct r_port *info, const char *routine){#ifdef ROCKET_PARANOIA_CHECK if (!info) return 1; if (info->magic != RPORT_MAGIC) { printk(KERN_INFO "Warning: bad magic number for rocketport struct in %s\n", routine); return 1; }#endif return 0;}/* Serial port receive data function. Called (from timer poll) when an AIOPIC signals * that receive data is present on a serial port. Pulls data from FIFO, moves it into the * tty layer. */static void rp_do_receive(struct r_port *info, struct tty_struct *tty, CHANNEL_t * cp, unsigned int ChanStatus){ unsigned int CharNStat; int ToRecv, wRecv, space; unsigned char *cbuf; ToRecv = sGetRxCnt(cp);#ifdef ROCKET_DEBUG_INTR printk(KERN_INFO "rp_do_receive(%d)...", ToRecv);#endif if (ToRecv == 0) return; /* * if status indicates there are errored characters in the * FIFO, then enter status mode (a word in FIFO holds * character and status). */ if (ChanStatus & (RXFOVERFL | RXBREAK | RXFRAME | RXPARITY)) { if (!(ChanStatus & STATMODE)) {#ifdef ROCKET_DEBUG_RECEIVE printk(KERN_INFO "Entering STATMODE...");#endif ChanStatus |= STATMODE; sEnRxStatusMode(cp); } } /* * if we previously entered status mode, then read down the * FIFO one word at a time, pulling apart the character and * the status. Update error counters depending on status */ if (ChanStatus & STATMODE) {#ifdef ROCKET_DEBUG_RECEIVE printk(KERN_INFO "Ignore %x, read %x...", info->ignore_status_mask, info->read_status_mask);#endif while (ToRecv) { char flag; CharNStat = sInW(sGetTxRxDataIO(cp));#ifdef ROCKET_DEBUG_RECEIVE printk(KERN_INFO "%x...", CharNStat);#endif if (CharNStat & STMBREAKH) CharNStat &= ~(STMFRAMEH | STMPARITYH); if (CharNStat & info->ignore_status_mask) { ToRecv--; continue; } CharNStat &= info->read_status_mask; if (CharNStat & STMBREAKH) flag = TTY_BREAK; else if (CharNStat & STMPARITYH) flag = TTY_PARITY; else if (CharNStat & STMFRAMEH) flag = TTY_FRAME; else if (CharNStat & STMRCVROVRH) flag = TTY_OVERRUN; else flag = TTY_NORMAL; tty_insert_flip_char(tty, CharNStat & 0xff, flag); ToRecv--; } /* * after we've emptied the FIFO in status mode, turn * status mode back off */ if (sGetRxCnt(cp) == 0) {#ifdef ROCKET_DEBUG_RECEIVE printk(KERN_INFO "Status mode off.\n");#endif sDisRxStatusMode(cp); } } else { /* * we aren't in status mode, so read down the FIFO two * characters at time by doing repeated word IO * transfer. */ space = tty_prepare_flip_string(tty, &cbuf, ToRecv); if (space < ToRecv) {#ifdef ROCKET_DEBUG_RECEIVE printk(KERN_INFO "rp_do_receive:insufficient space ToRecv=%d space=%d\n", ToRecv, space);#endif if (space <= 0) return; ToRecv = space; } wRecv = ToRecv >> 1; if (wRecv) sInStrW(sGetTxRxDataIO(cp), (unsigned short *) cbuf, wRecv); if (ToRecv & 1) cbuf[ToRecv - 1] = sInB(sGetTxRxDataIO(cp)); } /* Push the data up to the tty layer */ tty_flip_buffer_push(tty);}/* * Serial port transmit data function. Called from the timer polling loop as a * result of a bit set in xmit_flags[], indicating data (from the tty layer) is ready * to be sent out the serial port. Data is buffered in rp_table[line].xmit_buf, it is * moved to the port's xmit FIFO. *info is critical data, protected by spinlocks. */static void rp_do_transmit(struct r_port *info){ int c; CHANNEL_t *cp = &info->channel; struct tty_struct *tty; unsigned long flags;#ifdef ROCKET_DEBUG_INTR printk(KERN_INFO "rp_do_transmit ");#endif if (!info) return; if (!info->tty) { printk(KERN_INFO "rp: WARNING rp_do_transmit called with info->tty==NULL\n"); clear_bit((info->aiop * 8) + info->chan, (void *) &xmit_flags[info->board]); return; } spin_lock_irqsave(&info->slock, flags); tty = info->tty; info->xmit_fifo_room = TXFIFO_SIZE - sGetTxCnt(cp); /* Loop sending data to FIFO until done or FIFO full */ while (1) { if (tty->stopped || tty->hw_stopped) break; c = min(info->xmit_fifo_room, min(info->xmit_cnt, XMIT_BUF_SIZE - info->xmit_tail)); if (c <= 0 || info->xmit_fifo_room <= 0) break; sOutStrW(sGetTxRxDataIO(cp), (unsigned short *) (info->xmit_buf + info->xmit_tail), c / 2); if (c & 1) sOutB(sGetTxRxDataIO(cp), info->xmit_buf[info->xmit_tail + c - 1]); info->xmit_tail += c; info->xmit_tail &= XMIT_BUF_SIZE - 1; info->xmit_cnt -= c; info->xmit_fifo_room -= c;#ifdef ROCKET_DEBUG_INTR
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?