serial167.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:80k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * linux/drivers/char/serial167.c
  3.  *
  4.  * Driver for MVME166/7 board serial ports, which are via a CD2401.
  5.  * Based very much on cyclades.c.
  6.  *
  7.  * MVME166/7 work by Richard Hirst [richard@sleepie.demon.co.uk]
  8.  *
  9.  * ==============================================================
  10.  *
  11.  * static char rcsid[] =
  12.  * "$Revision: 1.36.1.4 $$Date: 1995/03/29 06:14:14 $";
  13.  *
  14.  *  linux/kernel/cyclades.c
  15.  *
  16.  * Maintained by Marcio Saito (cyclades@netcom.com) and
  17.  * Randolph Bentson (bentson@grieg.seaslug.org)
  18.  *
  19.  * Much of the design and some of the code came from serial.c
  20.  * which was copyright (C) 1991, 1992  Linus Torvalds.  It was
  21.  * extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92,
  22.  * and then fixed as suggested by Michael K. Johnson 12/12/92.
  23.  *
  24.  * This version does not support shared irq's.
  25.  *
  26.  * This module exports the following rs232 io functions:
  27.  *   int cy_init(void);
  28.  *   int  cy_open(struct tty_struct *tty, struct file *filp);
  29.  *
  30.  * $Log: cyclades.c,v $
  31.  * Revision 1.36.1.4  1995/03/29  06:14:14  bentson
  32.  * disambiguate between Cyclom-16Y and Cyclom-32Ye;
  33.  *
  34.  * Changes:
  35.  *
  36.  * 200 lines of changes record removed - RGH 11-10-95, starting work on
  37.  * converting this to drive serial ports on mvme166 (cd2401).
  38.  *
  39.  * Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 2000/08/25
  40.  * - get rid of verify_area
  41.  * - use get_user to access memory from userspace in set_threshold,
  42.  *   set_default_threshold and set_timeout
  43.  * - don't use the panic function in serial167_init
  44.  * - do resource release on failure on serial167_init
  45.  * - include missing restore_flags in mvme167_serial_console_setup
  46.  */
  47. #include <linux/config.h>
  48. #include <linux/errno.h>
  49. #include <linux/signal.h>
  50. #include <linux/sched.h>
  51. #include <linux/timer.h>
  52. #include <linux/tty.h>
  53. #include <linux/interrupt.h>
  54. #include <linux/serial.h>
  55. #include <linux/serialP.h>
  56. #include <linux/string.h>
  57. #include <linux/fcntl.h>
  58. #include <linux/ptrace.h>
  59. #include <linux/serial167.h>
  60. #include <linux/delay.h>
  61. #include <linux/major.h>
  62. #include <linux/mm.h>
  63. #include <linux/console.h>
  64. #include <linux/module.h>
  65. #include <asm/system.h>
  66. #include <asm/io.h>
  67. #include <asm/segment.h>
  68. #include <asm/bitops.h>
  69. #include <asm/mvme16xhw.h>
  70. #include <asm/bootinfo.h>
  71. #include <asm/setup.h>
  72. #include <linux/types.h>
  73. #include <linux/kernel.h>
  74. #include <linux/version.h>
  75. #include <asm/uaccess.h>
  76. #include <linux/init.h>
  77. #define SERIAL_PARANOIA_CHECK
  78. #undef  SERIAL_DEBUG_OPEN
  79. #undef  SERIAL_DEBUG_THROTTLE
  80. #undef  SERIAL_DEBUG_OTHER
  81. #undef  SERIAL_DEBUG_IO
  82. #undef  SERIAL_DEBUG_COUNT
  83. #undef  SERIAL_DEBUG_DTR
  84. #undef  CYCLOM_16Y_HACK
  85. #define  CYCLOM_ENABLE_MONITORING
  86. #ifndef MIN
  87. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  88. #endif
  89. #define WAKEUP_CHARS 256
  90. #define STD_COM_FLAGS (0)
  91. #define SERIAL_TYPE_NORMAL  1
  92. #define SERIAL_TYPE_CALLOUT 2
  93. DECLARE_TASK_QUEUE(tq_cyclades);
  94. struct tty_driver cy_serial_driver, cy_callout_driver;
  95. extern int serial_console;
  96. static struct cyclades_port *serial_console_info = NULL;
  97. static unsigned int serial_console_cflag = 0;
  98. u_char initial_console_speed;
  99. /* Base address of cd2401 chip on mvme166/7 */
  100. #define BASE_ADDR (0xfff45000)
  101. #define pcc2chip ((volatile u_char *)0xfff42000)
  102. #define PccSCCMICR 0x1d
  103. #define PccSCCTICR 0x1e
  104. #define PccSCCRICR 0x1f
  105. #define PccTPIACKR 0x25
  106. #define PccRPIACKR 0x27
  107. #define PccIMLR 0x3f
  108. /* This is the per-port data structure */
  109. struct cyclades_port cy_port[] = {
  110.       /* CARD#  */
  111.         {-1 },      /* ttyS0 */
  112.         {-1 },      /* ttyS1 */
  113.         {-1 },      /* ttyS2 */
  114.         {-1 },      /* ttyS3 */
  115. };
  116. #define NR_PORTS        (sizeof(cy_port)/sizeof(struct cyclades_port))
  117. static int serial_refcount;
  118. static struct tty_struct *serial_table[NR_PORTS];
  119. static struct termios *serial_termios[NR_PORTS];
  120. static struct termios *serial_termios_locked[NR_PORTS];
  121. /*
  122.  * tmp_buf is used as a temporary buffer by serial_write.  We need to
  123.  * lock it in case the copy_from_user blocks while swapping in a page,
  124.  * and some other program tries to do a serial write at the same time.
  125.  * Since the lock will only come under contention when the system is
  126.  * swapping and available memory is low, it makes sense to share one
  127.  * buffer across all the serial ports, since it significantly saves
  128.  * memory if large numbers of serial ports are open.
  129.  */
  130. static unsigned char *tmp_buf = 0;
  131. DECLARE_MUTEX(tmp_buf_sem);
  132. /*
  133.  * This is used to look up the divisor speeds and the timeouts
  134.  * We're normally limited to 15 distinct baud rates.  The extra
  135.  * are accessed via settings in info->flags.
  136.  *         0,     1,     2,     3,     4,     5,     6,     7,     8,     9,
  137.  *        10,    11,    12,    13,    14,    15,    16,    17,    18,    19,
  138.  *                                                  HI            VHI
  139.  */
  140. static int baud_table[] = {
  141.            0,    50,    75,   110,   134,   150,   200,   300,   600,  1200,
  142.         1800,  2400,  4800,  9600, 19200, 38400, 57600, 76800,115200,150000,
  143.         0};
  144. #if 0
  145. static char baud_co[] = {  /* 25 MHz clock option table */
  146.         /* value =>    00    01   02    03    04 */
  147.         /* divide by    8    32   128   512  2048 */
  148.         0x00,  0x04,  0x04,  0x04,  0x04,  0x04,  0x03,  0x03,  0x03,  0x02,
  149.         0x02,  0x02,  0x01,  0x01,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00};
  150. static char baud_bpr[] = {  /* 25 MHz baud rate period table */
  151.         0x00,  0xf5,  0xa3,  0x6f,  0x5c,  0x51,  0xf5,  0xa3,  0x51,  0xa3,
  152.         0x6d,  0x51,  0xa3,  0x51,  0xa3,  0x51,  0x36,  0x29,  0x1b,  0x15};
  153. #endif
  154. /* I think 166 brd clocks 2401 at 20MHz.... */
  155. /* These values are written directly to tcor, and >> 5 for writing to rcor */
  156. static u_char baud_co[] = {  /* 20 MHz clock option table */
  157.         0x00,  0x80,  0x80,  0x80,  0x80,  0x80,  0x80,  0x60,  0x60,  0x40,
  158.         0x40,  0x40,  0x20,  0x20,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00};
  159. /* These values written directly to tbpr/rbpr */
  160. static u_char baud_bpr[] = {  /* 20 MHz baud rate period table */
  161.         0x00,  0xc0,  0x80,  0x58,  0x6c,  0x40,  0xc0,  0x81,  0x40,  0x81,
  162.         0x57,  0x40,  0x81,  0x40,  0x81,  0x40,  0x2b,  0x20,  0x15,  0x10};
  163. static u_char baud_cor4[] = {  /* receive threshold */
  164.         0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,
  165.         0x0a,  0x0a,  0x0a,  0x09,  0x09,  0x08,  0x08,  0x08,  0x08,  0x07};
  166. static void shutdown(struct cyclades_port *);
  167. static int startup (struct cyclades_port *);
  168. static void cy_throttle(struct tty_struct *);
  169. static void cy_unthrottle(struct tty_struct *);
  170. static void config_setup(struct cyclades_port *);
  171. extern void console_print(const char *);
  172. #ifdef CYCLOM_SHOW_STATUS
  173. static void show_status(int);
  174. #endif
  175. #ifdef CONFIG_REMOTE_DEBUG
  176. static void debug_setup(void);
  177. void queueDebugChar (int c);
  178. int getDebugChar(void);
  179. #define DEBUG_PORT 1
  180. #define DEBUG_LEN 256
  181. typedef struct {
  182. int in;
  183. int out;
  184. unsigned char buf[DEBUG_LEN];
  185. } debugq;
  186. debugq debugiq;
  187. #endif
  188. /*
  189.  * I have my own version of udelay(), as it is needed when initialising
  190.  * the chip, before the delay loop has been calibrated.  Should probably
  191.  * reference one of the vmechip2 or pccchip2 counter for an accurate
  192.  * delay, but this wild guess will do for now.
  193.  */
  194. void my_udelay (long us)
  195. {
  196. u_char x;
  197. volatile u_char *p = &x;
  198. int i;
  199. while (us--)
  200. for (i = 100; i; i--)
  201. x |= *p;
  202. }
  203. static inline int
  204. serial_paranoia_check(struct cyclades_port *info,
  205. dev_t device, const char *routine)
  206. {
  207. #ifdef SERIAL_PARANOIA_CHECK
  208.     static const char *badmagic =
  209. "Warning: bad magic number for serial struct (%d, %d) in %sn";
  210.     static const char *badinfo =
  211. "Warning: null cyclades_port for (%d, %d) in %sn";
  212.     static const char *badrange =
  213. "Warning: cyclades_port out of range for (%d, %d) in %sn";
  214.     if (!info) {
  215. printk(badinfo, MAJOR(device), MINOR(device), routine);
  216. return 1;
  217.     }
  218.     if( (long)info < (long)(&cy_port[0])
  219.     || (long)(&cy_port[NR_PORTS]) < (long)info ){
  220. printk(badrange, MAJOR(device), MINOR(device), routine);
  221. return 1;
  222.     }
  223.     if (info->magic != CYCLADES_MAGIC) {
  224. printk(badmagic, MAJOR(device), MINOR(device), routine);
  225. return 1;
  226.     }
  227. #endif
  228. return 0;
  229. } /* serial_paranoia_check */
  230. #if 0
  231. /* The following diagnostic routines allow the driver to spew
  232.    information on the screen, even (especially!) during interrupts.
  233.  */
  234. void
  235. SP(char *data){
  236.   unsigned long flags;
  237.     save_flags(flags); cli();
  238.         console_print(data);
  239.     restore_flags(flags);
  240. }
  241. char scrn[2];
  242. void
  243. CP(char data){
  244.   unsigned long flags;
  245.     save_flags(flags); cli();
  246.         scrn[0] = data;
  247.         console_print(scrn);
  248.     restore_flags(flags);
  249. }/* CP */
  250. void CP1(int data) { (data<10)?  CP(data+'0'): CP(data+'A'-10); }/* CP1 */
  251. void CP2(int data) { CP1((data>>4) & 0x0f); CP1( data & 0x0f); }/* CP2 */
  252. void CP4(int data) { CP2((data>>8) & 0xff); CP2(data & 0xff); }/* CP4 */
  253. void CP8(long data) { CP4((data>>16) & 0xffff); CP4(data & 0xffff); }/* CP8 */
  254. #endif
  255. /* This routine waits up to 1000 micro-seconds for the previous
  256.    command to the Cirrus chip to complete and then issues the
  257.    new command.  An error is returned if the previous command
  258.    didn't finish within the time limit.
  259.  */
  260. u_short
  261. write_cy_cmd(volatile u_char *base_addr, u_char cmd)
  262. {
  263.   unsigned long flags;
  264.   volatile int  i;
  265.     save_flags(flags); cli();
  266. /* Check to see that the previous command has completed */
  267. for(i = 0 ; i < 100 ; i++){
  268.     if (base_addr[CyCCR] == 0){
  269. break;
  270.     }
  271.     my_udelay(10L);
  272. }
  273. /* if the CCR never cleared, the previous command
  274.     didn't finish within the "reasonable time" */
  275. if ( i == 10 ) {
  276.     restore_flags(flags);
  277.     return (-1);
  278. }
  279. /* Issue the new command */
  280. base_addr[CyCCR] = cmd;
  281.     restore_flags(flags);
  282.     return(0);
  283. } /* write_cy_cmd */
  284. /* cy_start and cy_stop provide software output flow control as a
  285.    function of XON/XOFF, software CTS, and other such stuff. */
  286. static void
  287. cy_stop(struct tty_struct *tty)
  288. {
  289.   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
  290.   volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
  291.   int channel;
  292.   unsigned long flags;
  293. #ifdef SERIAL_DEBUG_OTHER
  294.     printk("cy_stop ttyS%dn", info->line); /* */
  295. #endif
  296.     if (serial_paranoia_check(info, tty->device, "cy_stop"))
  297. return;
  298.     channel = info->line;
  299.     save_flags(flags); cli();
  300.         base_addr[CyCAR] = (u_char)(channel); /* index channel */
  301.         base_addr[CyIER] &= ~(CyTxMpty|CyTxRdy);
  302.     restore_flags(flags);
  303.     return;
  304. } /* cy_stop */
  305. static void
  306. cy_start(struct tty_struct *tty)
  307. {
  308.   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
  309.   volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
  310.   int channel;
  311.   unsigned long flags;
  312. #ifdef SERIAL_DEBUG_OTHER
  313.     printk("cy_start ttyS%dn", info->line); /* */
  314. #endif
  315.     if (serial_paranoia_check(info, tty->device, "cy_start"))
  316. return;
  317.     channel = info->line;
  318.     save_flags(flags); cli();
  319.         base_addr[CyCAR] = (u_char)(channel);
  320.         base_addr[CyIER] |= CyTxMpty;
  321.     restore_flags(flags);
  322.     return;
  323. } /* cy_start */
  324. /*
  325.  * This routine is used by the interrupt handler to schedule
  326.  * processing in the software interrupt portion of the driver
  327.  * (also known as the "bottom half").  This can be called any
  328.  * number of times for any channel without harm.
  329.  */
  330. static inline void
  331. cy_sched_event(struct cyclades_port *info, int event)
  332. {
  333.     info->event |= 1 << event; /* remember what kind of event and who */
  334.     queue_task(&info->tqueue, &tq_cyclades); /* it belongs to */
  335.     mark_bh(CYCLADES_BH);                       /* then trigger event */
  336. } /* cy_sched_event */
  337. /* The real interrupt service routines are called
  338.    whenever the card wants its hand held--chars
  339.    received, out buffer empty, modem change, etc.
  340.  */
  341. static void
  342. cd2401_rxerr_interrupt(int irq, void *dev_id, struct pt_regs *fp)
  343. {
  344.     struct tty_struct *tty;
  345.     struct cyclades_port *info;
  346.     volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
  347.     unsigned char err, rfoc;
  348.     int channel;
  349.     char data;
  350.     /* determine the channel and change to that context */
  351.     channel = (u_short ) (base_addr[CyLICR] >> 2);
  352.     info = &cy_port[channel];
  353.     info->last_active = jiffies;
  354.     if ((err = base_addr[CyRISR]) & CyTIMEOUT) {
  355. /* This is a receive timeout interrupt, ignore it */
  356. base_addr[CyREOIR] = CyNOTRANS;
  357. return;
  358.     }
  359.     /* Read a byte of data if there is any - assume the error
  360.      * is associated with this character */
  361.     if ((rfoc = base_addr[CyRFOC]) != 0)
  362. data = base_addr[CyRDR];
  363.     else
  364. data = 0;
  365.     /* if there is nowhere to put the data, discard it */
  366.     if(info->tty == 0) {
  367. base_addr[CyREOIR] = rfoc ? 0 : CyNOTRANS;
  368. return;
  369.     }
  370.     else { /* there is an open port for this data */
  371. tty = info->tty;
  372. if(err & info->ignore_status_mask){
  373.     base_addr[CyREOIR] = rfoc ? 0 : CyNOTRANS;
  374.     return;
  375. }
  376. if (tty->flip.count < TTY_FLIPBUF_SIZE){
  377.     tty->flip.count++;
  378.     if (err & info->read_status_mask){
  379. if(err & CyBREAK){
  380.     *tty->flip.flag_buf_ptr++ = TTY_BREAK;
  381.     *tty->flip.char_buf_ptr++ = data;
  382.     if (info->flags & ASYNC_SAK){
  383. do_SAK(tty);
  384.     }
  385. }else if(err & CyFRAME){
  386.     *tty->flip.flag_buf_ptr++ = TTY_FRAME;
  387.     *tty->flip.char_buf_ptr++ = data;
  388. }else if(err & CyPARITY){
  389.     *tty->flip.flag_buf_ptr++ = TTY_PARITY;
  390.     *tty->flip.char_buf_ptr++ = data;
  391. }else if(err & CyOVERRUN){
  392.     *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
  393.     *tty->flip.char_buf_ptr++ = 0;
  394.     /*
  395.        If the flip buffer itself is
  396.        overflowing, we still loose
  397.        the next incoming character.
  398.      */
  399.     if(tty->flip.count < TTY_FLIPBUF_SIZE){
  400. tty->flip.count++;
  401. *tty->flip.flag_buf_ptr++ = TTY_NORMAL;
  402. *tty->flip.char_buf_ptr++ = data;
  403.     }
  404. /* These two conditions may imply */
  405. /* a normal read should be done. */
  406. /* else if(data & CyTIMEOUT) */
  407. /* else if(data & CySPECHAR) */
  408. }else{
  409.     *tty->flip.flag_buf_ptr++ = 0;
  410.     *tty->flip.char_buf_ptr++ = 0;
  411. }
  412.     }else{
  413. *tty->flip.flag_buf_ptr++ = 0;
  414. *tty->flip.char_buf_ptr++ = 0;
  415.     }
  416. }else{
  417.     /* there was a software buffer overrun
  418.        and nothing could be done about it!!! */
  419. }
  420.     }
  421.     queue_task(&tty->flip.tqueue, &tq_timer);
  422.     /* end of service */
  423.     base_addr[CyREOIR] = rfoc ? 0 : CyNOTRANS;
  424. } /* cy_rxerr_interrupt */
  425. static void
  426. cd2401_modem_interrupt(int irq, void *dev_id, struct pt_regs *fp)
  427. {
  428.     struct cyclades_port *info;
  429.     volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
  430.     int channel;
  431.     int mdm_change;
  432.     int mdm_status;
  433.     /* determine the channel and change to that context */
  434.     channel = (u_short ) (base_addr[CyLICR] >> 2);
  435.     info = &cy_port[channel];
  436.     info->last_active = jiffies;
  437.     mdm_change = base_addr[CyMISR];
  438.     mdm_status = base_addr[CyMSVR1];
  439.     if(info->tty == 0){ /* nowhere to put the data, ignore it */
  440. ;
  441.     }else{
  442. if((mdm_change & CyDCD)
  443. && (info->flags & ASYNC_CHECK_CD)){
  444.     if(mdm_status & CyDCD){
  445. /* CP('!'); */
  446. cy_sched_event(info, Cy_EVENT_OPEN_WAKEUP);
  447.     }else if(!((info->flags & ASYNC_CALLOUT_ACTIVE)
  448.      &&(info->flags & ASYNC_CALLOUT_NOHUP))){
  449. /* CP('@'); */
  450. cy_sched_event(info, Cy_EVENT_HANGUP);
  451.     }
  452. }
  453. if((mdm_change & CyCTS)
  454. && (info->flags & ASYNC_CTS_FLOW)){
  455.     if(info->tty->stopped){
  456. if(mdm_status & CyCTS){
  457.     /* !!! cy_start isn't used because... */
  458.     info->tty->stopped = 0;
  459.              base_addr[CyIER] |= CyTxMpty;
  460.     cy_sched_event(info, Cy_EVENT_WRITE_WAKEUP);
  461. }
  462.     }else{
  463. if(!(mdm_status & CyCTS)){
  464.     /* !!! cy_stop isn't used because... */
  465.     info->tty->stopped = 1;
  466.              base_addr[CyIER] &= ~(CyTxMpty|CyTxRdy);
  467. }
  468.     }
  469. }
  470. if(mdm_status & CyDSR){
  471. }
  472.     }
  473.     base_addr[CyMEOIR] = 0;
  474. } /* cy_modem_interrupt */
  475. static void
  476. cd2401_tx_interrupt(int irq, void *dev_id, struct pt_regs *fp)
  477. {
  478.     struct cyclades_port *info;
  479.     volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
  480.     int channel;
  481.     int char_count, saved_cnt;
  482.     int outch;
  483.     /* determine the channel and change to that context */
  484.     channel = (u_short ) (base_addr[CyLICR] >> 2);
  485. #ifdef CONFIG_REMOTE_DEBUG
  486.     if (channel == DEBUG_PORT) {
  487. panic ("TxInt on debug port!!!");
  488.     }
  489. #endif
  490.     info = &cy_port[channel];
  491.     /* validate the port number (as configured and open) */
  492.     if( (channel < 0) || (NR_PORTS <= channel) ){
  493. base_addr[CyIER] &= ~(CyTxMpty|CyTxRdy);
  494. base_addr[CyTEOIR] = CyNOTRANS;
  495. return;
  496.     }
  497.     info->last_active = jiffies;
  498.     if(info->tty == 0){
  499. base_addr[CyIER] &= ~(CyTxMpty|CyTxRdy);
  500.         if (info->xmit_cnt < WAKEUP_CHARS) {
  501.     cy_sched_event(info, Cy_EVENT_WRITE_WAKEUP);
  502.         }
  503. base_addr[CyTEOIR] = CyNOTRANS;
  504. return;
  505.     }
  506.     /* load the on-chip space available for outbound data */
  507.     saved_cnt = char_count = base_addr[CyTFTC];
  508.     if(info->x_char) { /* send special char */
  509. outch = info->x_char;
  510. base_addr[CyTDR] = outch;
  511. char_count--;
  512. info->x_char = 0;
  513.     }
  514.     if (info->x_break){
  515. /*  The Cirrus chip requires the "Embedded Transmit
  516.     Commands" of start break, delay, and end break
  517.     sequences to be sent.  The duration of the
  518.     break is given in TICs, which runs at HZ
  519.     (typically 100) and the PPR runs at 200 Hz,
  520.     so the delay is duration * 200/HZ, and thus a
  521.     break can run from 1/100 sec to about 5/4 sec.
  522.     Need to check these values - RGH 141095.
  523.  */
  524. base_addr[CyTDR] = 0; /* start break */
  525. base_addr[CyTDR] = 0x81;
  526. base_addr[CyTDR] = 0; /* delay a bit */
  527. base_addr[CyTDR] = 0x82;
  528. base_addr[CyTDR] = info->x_break*200/HZ;
  529. base_addr[CyTDR] = 0; /* terminate break */
  530. base_addr[CyTDR] = 0x83;
  531. char_count -= 7;
  532. info->x_break = 0;
  533.     }
  534.     while (char_count > 0){
  535. if (!info->xmit_cnt){
  536.     base_addr[CyIER] &= ~(CyTxMpty|CyTxRdy);
  537.     break;
  538. }
  539. if (info->xmit_buf == 0){
  540.     base_addr[CyIER] &= ~(CyTxMpty|CyTxRdy);
  541.     break;
  542. }
  543. if (info->tty->stopped || info->tty->hw_stopped){
  544.     base_addr[CyIER] &= ~(CyTxMpty|CyTxRdy);
  545.     break;
  546. }
  547. /* Because the Embedded Transmit Commands have been
  548.    enabled, we must check to see if the escape
  549.    character, NULL, is being sent.  If it is, we
  550.    must ensure that there is room for it to be
  551.    doubled in the output stream.  Therefore we
  552.    no longer advance the pointer when the character
  553.    is fetched, but rather wait until after the check
  554.    for a NULL output character. (This is necessary
  555.    because there may not be room for the two chars
  556.    needed to send a NULL.
  557.  */
  558. outch = info->xmit_buf[info->xmit_tail];
  559. if( outch ){
  560.     info->xmit_cnt--;
  561.     info->xmit_tail = (info->xmit_tail + 1)
  562.       & (PAGE_SIZE - 1);
  563.     base_addr[CyTDR] = outch;
  564.     char_count--;
  565. }else{
  566.     if(char_count > 1){
  567. info->xmit_cnt--;
  568. info->xmit_tail = (info->xmit_tail + 1)
  569.   & (PAGE_SIZE - 1);
  570. base_addr[CyTDR] = outch;
  571. base_addr[CyTDR] = 0;
  572. char_count--;
  573. char_count--;
  574.     }else{
  575. break;
  576.     }
  577. }
  578.     }
  579.     if (info->xmit_cnt < WAKEUP_CHARS) {
  580. cy_sched_event(info, Cy_EVENT_WRITE_WAKEUP);
  581.     }
  582.     base_addr[CyTEOIR] = (char_count != saved_cnt) ? 0 : CyNOTRANS;
  583. } /* cy_tx_interrupt */
  584. static void
  585. cd2401_rx_interrupt(int irq, void *dev_id, struct pt_regs *fp)
  586. {
  587.     struct tty_struct *tty;
  588.     struct cyclades_port *info;
  589.     volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
  590.     int channel;
  591.     char data;
  592.     int char_count;
  593.     int save_cnt;
  594.     /* determine the channel and change to that context */
  595.     channel = (u_short ) (base_addr[CyLICR] >> 2);
  596.     info = &cy_port[channel];
  597.     info->last_active = jiffies;
  598.     save_cnt = char_count = base_addr[CyRFOC];
  599. #ifdef CONFIG_REMOTE_DEBUG
  600.     if (channel == DEBUG_PORT) {
  601. while (char_count--) {
  602.             data = base_addr[CyRDR];
  603.     queueDebugChar(data);
  604. }
  605.     }
  606.     else
  607. #endif
  608.     /* if there is nowhere to put the data, discard it */
  609.     if(info->tty == 0){
  610. while(char_count--){
  611.     data = base_addr[CyRDR];
  612. }
  613.     }else{ /* there is an open port for this data */
  614. tty = info->tty;
  615. /* load # characters available from the chip */
  616. #ifdef CYCLOM_ENABLE_MONITORING
  617. ++info->mon.int_count;
  618. info->mon.char_count += char_count;
  619. if (char_count > info->mon.char_max)
  620.     info->mon.char_max = char_count;
  621. info->mon.char_last = char_count;
  622. #endif
  623. while(char_count--){
  624.     data = base_addr[CyRDR];
  625.     if (tty->flip.count >= TTY_FLIPBUF_SIZE){
  626. continue;
  627.     }
  628.     tty->flip.count++;
  629.     *tty->flip.flag_buf_ptr++ = TTY_NORMAL;
  630.     *tty->flip.char_buf_ptr++ = data;
  631. #ifdef CYCLOM_16Y_HACK
  632.     udelay(10L);
  633. #endif
  634.         }
  635. queue_task(&tty->flip.tqueue, &tq_timer);
  636.     }
  637.     /* end of service */
  638.     base_addr[CyREOIR] = save_cnt ? 0 : CyNOTRANS;
  639. } /* cy_rx_interrupt */
  640. /*
  641.  * This routine is used to handle the "bottom half" processing for the
  642.  * serial driver, known also the "software interrupt" processing.
  643.  * This processing is done at the kernel interrupt level, after the
  644.  * cy_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
  645.  * is where time-consuming activities which can not be done in the
  646.  * interrupt driver proper are done; the interrupt driver schedules
  647.  * them using cy_sched_event(), and they get done here.
  648.  *
  649.  * This is done through one level of indirection--the task queue.
  650.  * When a hardware interrupt service routine wants service by the
  651.  * driver's bottom half, it enqueues the appropriate tq_struct (one
  652.  * per port) to the tq_cyclades work queue and sets a request flag
  653.  * via mark_bh for processing that queue.  When the time is right,
  654.  * do_cyclades_bh is called (because of the mark_bh) and it requests
  655.  * that the work queue be processed.
  656.  *
  657.  * Although this may seem unwieldy, it gives the system a way to
  658.  * pass an argument (in this case the pointer to the cyclades_port
  659.  * structure) to the bottom half of the driver.  Previous kernels
  660.  * had to poll every port to see if that port needed servicing.
  661.  */
  662. static void
  663. do_cyclades_bh(void)
  664. {
  665.     run_task_queue(&tq_cyclades);
  666. } /* do_cyclades_bh */
  667. static void
  668. do_softint(void *private_)
  669. {
  670.   struct cyclades_port *info = (struct cyclades_port *) private_;
  671.   struct tty_struct    *tty;
  672.     tty = info->tty;
  673.     if (!tty)
  674. return;
  675.     if (test_and_clear_bit(Cy_EVENT_HANGUP, &info->event)) {
  676. tty_hangup(info->tty);
  677. wake_up_interruptible(&info->open_wait);
  678. info->flags &= ~(ASYNC_NORMAL_ACTIVE|
  679.      ASYNC_CALLOUT_ACTIVE);
  680.     }
  681.     if (test_and_clear_bit(Cy_EVENT_OPEN_WAKEUP, &info->event)) {
  682. wake_up_interruptible(&info->open_wait);
  683.     }
  684.     if (test_and_clear_bit(Cy_EVENT_WRITE_WAKEUP, &info->event)) {
  685. if((tty->flags & (1<< TTY_DO_WRITE_WAKEUP))
  686. && tty->ldisc.write_wakeup){
  687.     (tty->ldisc.write_wakeup)(tty);
  688. }
  689. wake_up_interruptible(&tty->write_wait);
  690.     }
  691. } /* do_softint */
  692. /* This is called whenever a port becomes active;
  693.    interrupts are enabled and DTR & RTS are turned on.
  694.  */
  695. static int
  696. startup(struct cyclades_port * info)
  697. {
  698.   unsigned long flags;
  699.   volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
  700.   int channel;
  701.     if (info->flags & ASYNC_INITIALIZED){
  702. return 0;
  703.     }
  704.     if (!info->type){
  705. if (info->tty){
  706.     set_bit(TTY_IO_ERROR, &info->tty->flags);
  707. }
  708. return 0;
  709.     }
  710.     if (!info->xmit_buf){
  711. info->xmit_buf = (unsigned char *) get_free_page (GFP_KERNEL);
  712. if (!info->xmit_buf){
  713.     return -ENOMEM;
  714. }
  715.     }
  716.     config_setup(info);
  717.     channel = info->line;
  718. #ifdef SERIAL_DEBUG_OPEN
  719.     printk("startup channel %dn", channel);
  720. #endif
  721.     save_flags(flags); cli();
  722. base_addr[CyCAR] = (u_char)channel;
  723. write_cy_cmd(base_addr,CyENB_RCVR|CyENB_XMTR);
  724. base_addr[CyCAR] = (u_char)channel; /* !!! Is this needed? */
  725. base_addr[CyMSVR1] = CyRTS;
  726. /* CP('S');CP('1'); */
  727. base_addr[CyMSVR2] = CyDTR;
  728. #ifdef SERIAL_DEBUG_DTR
  729.         printk("cyc: %d: raising DTRn", __LINE__);
  730.         printk("     status: 0x%x, 0x%xn", base_addr[CyMSVR1], base_addr[CyMSVR2]);
  731. #endif
  732. base_addr[CyIER] |= CyRxData;
  733. info->flags |= ASYNC_INITIALIZED;
  734. if (info->tty){
  735.     clear_bit(TTY_IO_ERROR, &info->tty->flags);
  736. }
  737. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  738.     restore_flags(flags);
  739. #ifdef SERIAL_DEBUG_OPEN
  740.     printk(" donen");
  741. #endif
  742.     return 0;
  743. } /* startup */
  744. void
  745. start_xmit( struct cyclades_port *info )
  746. {
  747.   unsigned long flags;
  748.   volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
  749.   int channel;
  750.     channel = info->line;
  751.     save_flags(flags); cli();
  752. base_addr[CyCAR] = channel;
  753. base_addr[CyIER] |= CyTxMpty;
  754.     restore_flags(flags);
  755. } /* start_xmit */
  756. /*
  757.  * This routine shuts down a serial port; interrupts are disabled,
  758.  * and DTR is dropped if the hangup on close termio flag is on.
  759.  */
  760. static void
  761. shutdown(struct cyclades_port * info)
  762. {
  763.   unsigned long flags;
  764.   volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
  765.   int channel;
  766.     if (!(info->flags & ASYNC_INITIALIZED)){
  767. /* CP('$'); */
  768. return;
  769.     }
  770.     channel = info->line;
  771. #ifdef SERIAL_DEBUG_OPEN
  772.     printk("shutdown channel %dn", channel);
  773. #endif
  774.     /* !!! REALLY MUST WAIT FOR LAST CHARACTER TO BE
  775.        SENT BEFORE DROPPING THE LINE !!!  (Perhaps
  776.        set some flag that is read when XMTY happens.)
  777.        Other choices are to delay some fixed interval
  778.        or schedule some later processing.
  779.      */
  780.     save_flags(flags); cli();
  781. if (info->xmit_buf){
  782.     free_page((unsigned long) info->xmit_buf);
  783.     info->xmit_buf = 0;
  784. }
  785. base_addr[CyCAR] = (u_char)channel;
  786. if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
  787.     base_addr[CyMSVR1] = 0;
  788. /* CP('C');CP('1'); */
  789.     base_addr[CyMSVR2] = 0;
  790. #ifdef SERIAL_DEBUG_DTR
  791.             printk("cyc: %d: dropping DTRn", __LINE__);
  792.             printk("     status: 0x%x, 0x%xn", base_addr[CyMSVR1], base_addr[CyMSVR2]);
  793. #endif
  794.         }
  795. write_cy_cmd(base_addr,CyDIS_RCVR);
  796.          /* it may be appropriate to clear _XMIT at
  797.            some later date (after testing)!!! */
  798. if (info->tty){
  799.     set_bit(TTY_IO_ERROR, &info->tty->flags);
  800. }
  801. info->flags &= ~ASYNC_INITIALIZED;
  802.     restore_flags(flags);
  803. #ifdef SERIAL_DEBUG_OPEN
  804.     printk(" donen");
  805. #endif
  806.     return;
  807. } /* shutdown */
  808. /*
  809.  * This routine finds or computes the various line characteristics.
  810.  */
  811. static void
  812. config_setup(struct cyclades_port * info)
  813. {
  814.   unsigned long flags;
  815.   volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
  816.   int channel;
  817.   unsigned cflag;
  818.   int   i;
  819.   unsigned char ti, need_init_chan = 0;
  820.     if (!info->tty || !info->tty->termios){
  821.         return;
  822.     }
  823.     if (info->line == -1){
  824.         return;
  825.     }
  826.     cflag = info->tty->termios->c_cflag;
  827.     /* baud rate */
  828.     i = cflag & CBAUD;
  829. #ifdef CBAUDEX
  830. /* Starting with kernel 1.1.65, there is direct support for
  831.    higher baud rates.  The following code supports those
  832.    changes.  The conditional aspect allows this driver to be
  833.    used for earlier as well as later kernel versions.  (The
  834.    mapping is slightly different from serial.c because there
  835.    is still the possibility of supporting 75 kbit/sec with
  836.    the Cyclades board.)
  837.  */
  838.     if (i & CBAUDEX) {
  839. if (i == B57600)
  840.     i = 16;
  841. else if(i == B115200) 
  842.     i = 18;
  843. #ifdef B78600
  844. else if(i == B78600) 
  845.     i = 17;
  846. #endif
  847. else
  848.     info->tty->termios->c_cflag &= ~CBAUDEX;
  849.     }
  850. #endif
  851.     if (i == 15) {
  852.     if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  853.     i += 1;
  854.     if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  855.     i += 3;
  856.     }
  857.     /* Don't ever change the speed of the console port.  It will
  858.      * run at the speed specified in bootinfo, or at 19.2K */
  859.     /* Actually, it should run at whatever speed 166Bug was using */
  860.     /* Note info->timeout isn't used at present */
  861.     if (info != serial_console_info) {
  862. info->tbpr = baud_bpr[i]; /* Tx BPR */
  863. info->tco = baud_co[i]; /* Tx CO */
  864. info->rbpr = baud_bpr[i]; /* Rx BPR */
  865. info->rco = baud_co[i] >> 5; /* Rx CO */
  866. if (baud_table[i] == 134) {
  867.             info->timeout = (info->xmit_fifo_size*HZ*30/269) + 2;
  868.             /* get it right for 134.5 baud */
  869. } else if (baud_table[i]) {
  870.             info->timeout = (info->xmit_fifo_size*HZ*15/baud_table[i]) + 2;
  871.         /* this needs to be propagated into the card info */
  872. } else {
  873.             info->timeout = 0;
  874. }
  875.     }
  876.     /* By tradition (is it a standard?) a baud rate of zero
  877.        implies the line should be/has been closed.  A bit
  878.        later in this routine such a test is performed. */
  879.     /* byte size and parity */
  880.     info->cor7 = 0;
  881.     info->cor6 = 0;
  882.     info->cor5 = 0;
  883.     info->cor4 = (info->default_threshold
  884.   ? info->default_threshold
  885.   : baud_cor4[i]); /* receive threshold */
  886.     /* Following two lines added 101295, RGH. */
  887.     /* It is obviously wrong to access CyCORx, and not info->corx here,
  888.      * try and remember to fix it later! */
  889.     channel = info->line;
  890.     base_addr[CyCAR] = (u_char)channel;
  891.     if (C_CLOCAL(info->tty)) {
  892. if (base_addr[CyIER] & CyMdmCh)
  893.     base_addr[CyIER] &= ~CyMdmCh; /* without modem intr */
  894.        /* ignore 1->0 modem transitions */
  895. if (base_addr[CyCOR4] & (CyDSR|CyCTS|CyDCD))
  896.     base_addr[CyCOR4] &= ~(CyDSR|CyCTS|CyDCD);
  897.        /* ignore 0->1 modem transitions */
  898. if (base_addr[CyCOR5] & (CyDSR|CyCTS|CyDCD))
  899.     base_addr[CyCOR5] &= ~(CyDSR|CyCTS|CyDCD);
  900.     } else {
  901. if ((base_addr[CyIER] & CyMdmCh) != CyMdmCh)
  902.     base_addr[CyIER] |= CyMdmCh; /* with modem intr */
  903.        /* act on 1->0 modem transitions */
  904. if ((base_addr[CyCOR4] & (CyDSR|CyCTS|CyDCD)) != (CyDSR|CyCTS|CyDCD))
  905.     base_addr[CyCOR4] |= CyDSR|CyCTS|CyDCD;
  906.        /* act on 0->1 modem transitions */
  907. if ((base_addr[CyCOR5] & (CyDSR|CyCTS|CyDCD)) != (CyDSR|CyCTS|CyDCD))
  908.     base_addr[CyCOR5] |= CyDSR|CyCTS|CyDCD;
  909.     }
  910.     info->cor3 = (cflag & CSTOPB) ? Cy_2_STOP : Cy_1_STOP;
  911.     info->cor2 = CyETC;
  912.     switch(cflag & CSIZE){
  913.     case CS5:
  914.         info->cor1 = Cy_5_BITS;
  915.         break;
  916.     case CS6:
  917.         info->cor1 = Cy_6_BITS;
  918.         break;
  919.     case CS7:
  920.         info->cor1 = Cy_7_BITS;
  921.         break;
  922.     case CS8:
  923.         info->cor1 = Cy_8_BITS;
  924.         break;
  925.     }
  926.     if (cflag & PARENB){
  927.         if (cflag & PARODD){
  928.             info->cor1 |= CyPARITY_O;
  929.         }else{
  930.             info->cor1 |= CyPARITY_E;
  931.         }
  932.     }else{
  933.         info->cor1 |= CyPARITY_NONE;
  934.     }
  935.     /* CTS flow control flag */
  936. #if 0
  937.     /* Don't complcate matters for now! RGH 141095 */
  938.     if (cflag & CRTSCTS){
  939. info->flags |= ASYNC_CTS_FLOW;
  940. info->cor2 |= CyCtsAE;
  941.     }else{
  942. info->flags &= ~ASYNC_CTS_FLOW;
  943. info->cor2 &= ~CyCtsAE;
  944.     }
  945. #endif
  946.     if (cflag & CLOCAL)
  947. info->flags &= ~ASYNC_CHECK_CD;
  948.     else
  949. info->flags |= ASYNC_CHECK_CD;
  950.      /***********************************************
  951. The hardware option, CyRtsAO, presents RTS when
  952. the chip has characters to send.  Since most modems
  953. use RTS as reverse (inbound) flow control, this
  954. option is not used.  If inbound flow control is
  955. necessary, DTR can be programmed to provide the
  956. appropriate signals for use with a non-standard
  957. cable.  Contact Marcio Saito for details.
  958.      ***********************************************/
  959.     channel = info->line;
  960.     save_flags(flags); cli();
  961. base_addr[CyCAR] = (u_char)channel;
  962. /* CyCMR set once only in mvme167_init_serial() */
  963. if (base_addr[CyLICR] != channel << 2)
  964.     base_addr[CyLICR] = channel << 2;
  965. if (base_addr[CyLIVR] != 0x5c)
  966.     base_addr[CyLIVR] = 0x5c;
  967.        /* tx and rx baud rate */
  968. if (base_addr[CyCOR1] != info->cor1)
  969.     need_init_chan = 1;
  970. if (base_addr[CyTCOR] != info->tco)
  971.     base_addr[CyTCOR] = info->tco;
  972. if (base_addr[CyTBPR] != info->tbpr)
  973.     base_addr[CyTBPR] = info->tbpr;
  974. if (base_addr[CyRCOR] != info->rco)
  975.     base_addr[CyRCOR] = info->rco;
  976. if (base_addr[CyRBPR] != info->rbpr)
  977.     base_addr[CyRBPR] = info->rbpr;
  978. /* set line characteristics  according configuration */
  979. if (base_addr[CySCHR1] != START_CHAR(info->tty))
  980.     base_addr[CySCHR1] = START_CHAR(info->tty);
  981. if (base_addr[CySCHR2] != STOP_CHAR(info->tty))
  982.     base_addr[CySCHR2] = STOP_CHAR(info->tty);
  983. if (base_addr[CySCRL] != START_CHAR(info->tty))
  984.     base_addr[CySCRL] = START_CHAR(info->tty);
  985. if (base_addr[CySCRH] != START_CHAR(info->tty))
  986.     base_addr[CySCRH] = START_CHAR(info->tty);
  987. if (base_addr[CyCOR1] != info->cor1)
  988.     base_addr[CyCOR1] = info->cor1;
  989. if (base_addr[CyCOR2] != info->cor2)
  990.     base_addr[CyCOR2] = info->cor2;
  991. if (base_addr[CyCOR3] != info->cor3)
  992.     base_addr[CyCOR3] = info->cor3;
  993. if (base_addr[CyCOR4] != info->cor4)
  994.     base_addr[CyCOR4] = info->cor4;
  995. if (base_addr[CyCOR5] != info->cor5)
  996.     base_addr[CyCOR5] = info->cor5;
  997. if (base_addr[CyCOR6] != info->cor6)
  998.     base_addr[CyCOR6] = info->cor6;
  999. if (base_addr[CyCOR7] != info->cor7)
  1000.     base_addr[CyCOR7] = info->cor7;
  1001. if (need_init_chan)
  1002.     write_cy_cmd(base_addr,CyINIT_CHAN);
  1003. base_addr[CyCAR] = (u_char)channel; /* !!! Is this needed? */
  1004. /* 2ms default rx timeout */
  1005. ti = info->default_timeout ? info->default_timeout : 0x02;
  1006. if (base_addr[CyRTPRL] != ti)
  1007.     base_addr[CyRTPRL] = ti;
  1008. if (base_addr[CyRTPRH] != 0)
  1009.     base_addr[CyRTPRH] = 0;
  1010. /* Set up RTS here also ????? RGH 141095 */
  1011. if(i == 0){ /* baud rate is zero, turn off line */
  1012.     if ((base_addr[CyMSVR2] & CyDTR) == CyDTR)
  1013.         base_addr[CyMSVR2] = 0;
  1014. #ifdef SERIAL_DEBUG_DTR
  1015.             printk("cyc: %d: dropping DTRn", __LINE__);
  1016.             printk("     status: 0x%x, 0x%xn", base_addr[CyMSVR1], base_addr[CyMSVR2]);
  1017. #endif
  1018. }else{
  1019.     if ((base_addr[CyMSVR2] & CyDTR) != CyDTR)
  1020.         base_addr[CyMSVR2] = CyDTR;
  1021. #ifdef SERIAL_DEBUG_DTR
  1022.             printk("cyc: %d: raising DTRn", __LINE__);
  1023.             printk("     status: 0x%x, 0x%xn", base_addr[CyMSVR1], base_addr[CyMSVR2]);
  1024. #endif
  1025. }
  1026. if (info->tty){
  1027.     clear_bit(TTY_IO_ERROR, &info->tty->flags);
  1028. }
  1029.     restore_flags(flags);
  1030. } /* config_setup */
  1031. static void
  1032. cy_put_char(struct tty_struct *tty, unsigned char ch)
  1033. {
  1034.   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
  1035.   unsigned long flags;
  1036. #ifdef SERIAL_DEBUG_IO
  1037.     printk("cy_put_char ttyS%d(0x%02x)n", info->line, ch);
  1038. #endif
  1039.     if (serial_paranoia_check(info, tty->device, "cy_put_char"))
  1040. return;
  1041.     if (!tty || !info->xmit_buf)
  1042. return;
  1043.     save_flags(flags); cli();
  1044. if (info->xmit_cnt >= PAGE_SIZE - 1) {
  1045.     restore_flags(flags);
  1046.     return;
  1047. }
  1048. info->xmit_buf[info->xmit_head++] = ch;
  1049. info->xmit_head &= PAGE_SIZE - 1;
  1050. info->xmit_cnt++;
  1051.     restore_flags(flags);
  1052. } /* cy_put_char */
  1053. static void
  1054. cy_flush_chars(struct tty_struct *tty)
  1055. {
  1056.   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
  1057.   unsigned long flags;
  1058.   volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
  1059.   int channel;
  1060. #ifdef SERIAL_DEBUG_IO
  1061.     printk("cy_flush_chars ttyS%dn", info->line); /* */
  1062. #endif
  1063.     if (serial_paranoia_check(info, tty->device, "cy_flush_chars"))
  1064. return;
  1065.     if (info->xmit_cnt <= 0 || tty->stopped
  1066.     || tty->hw_stopped || !info->xmit_buf)
  1067. return;
  1068.     channel = info->line;
  1069.     save_flags(flags); cli();
  1070. base_addr[CyCAR] = channel;
  1071. base_addr[CyIER] |= CyTxMpty;
  1072.     restore_flags(flags);
  1073. } /* cy_flush_chars */
  1074. /* This routine gets called when tty_write has put something into
  1075.     the write_queue.  If the port is not already transmitting stuff,
  1076.     start it off by enabling interrupts.  The interrupt service
  1077.     routine will then ensure that the characters are sent.  If the
  1078.     port is already active, there is no need to kick it.
  1079.  */
  1080. static int
  1081. cy_write(struct tty_struct * tty, int from_user,
  1082.            const unsigned char *buf, int count)
  1083. {
  1084.   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
  1085.   unsigned long flags;
  1086.   int c, total = 0;
  1087. #ifdef SERIAL_DEBUG_IO
  1088.     printk("cy_write ttyS%dn", info->line); /* */
  1089. #endif
  1090.     if (serial_paranoia_check(info, tty->device, "cy_write")){
  1091. return 0;
  1092.     }
  1093.     if (!tty || !info->xmit_buf || !tmp_buf){
  1094.         return 0;
  1095.     }
  1096.     if (from_user) {
  1097.     down(&tmp_buf_sem);
  1098.     while (1) {
  1099.     c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
  1100.        SERIAL_XMIT_SIZE - info->xmit_head));
  1101.     if (c <= 0)
  1102.     break;
  1103.     c -= copy_from_user(tmp_buf, buf, c);
  1104.     if (!c) {
  1105.     if (!total)
  1106.     total = -EFAULT;
  1107.     break;
  1108.     }
  1109.     save_flags(flags); cli();
  1110.     c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
  1111.    SERIAL_XMIT_SIZE - info->xmit_head));
  1112.     memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
  1113.     info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
  1114.     info->xmit_cnt += c;
  1115.     restore_flags(flags);
  1116.     buf += c;
  1117.     count -= c;
  1118.     total += c;
  1119.     }
  1120.     up(&tmp_buf_sem);
  1121.     } else {
  1122.     while (1) {
  1123.     save_flags(flags); cli();
  1124.     c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
  1125.        SERIAL_XMIT_SIZE - info->xmit_head));
  1126.     if (c <= 0) {
  1127.     restore_flags(flags);
  1128.     break;
  1129.     }
  1130.     memcpy(info->xmit_buf + info->xmit_head, buf, c);
  1131.     info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
  1132.     info->xmit_cnt += c;
  1133.     restore_flags(flags);
  1134.     buf += c;
  1135.     count -= c;
  1136.     total += c;
  1137.     }
  1138.     }
  1139.     if (info->xmit_cnt
  1140.     && !tty->stopped
  1141.     && !tty->hw_stopped ) {
  1142.         start_xmit(info);
  1143.     }
  1144.     return total;
  1145. } /* cy_write */
  1146. static int
  1147. cy_write_room(struct tty_struct *tty)
  1148. {
  1149.   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
  1150.   int ret;
  1151. #ifdef SERIAL_DEBUG_IO
  1152.     printk("cy_write_room ttyS%dn", info->line); /* */
  1153. #endif
  1154.     if (serial_paranoia_check(info, tty->device, "cy_write_room"))
  1155. return 0;
  1156.     ret = PAGE_SIZE - info->xmit_cnt - 1;
  1157.     if (ret < 0)
  1158. ret = 0;
  1159.     return ret;
  1160. } /* cy_write_room */
  1161. static int
  1162. cy_chars_in_buffer(struct tty_struct *tty)
  1163. {
  1164.   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
  1165. #ifdef SERIAL_DEBUG_IO
  1166.     printk("cy_chars_in_buffer ttyS%d %dn", info->line, info->xmit_cnt); /* */
  1167. #endif
  1168.     if (serial_paranoia_check(info, tty->device, "cy_chars_in_buffer"))
  1169. return 0;
  1170.     return info->xmit_cnt;
  1171. } /* cy_chars_in_buffer */
  1172. static void
  1173. cy_flush_buffer(struct tty_struct *tty)
  1174. {
  1175.   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
  1176.   unsigned long flags;
  1177. #ifdef SERIAL_DEBUG_IO
  1178.     printk("cy_flush_buffer ttyS%dn", info->line); /* */
  1179. #endif
  1180.     if (serial_paranoia_check(info, tty->device, "cy_flush_buffer"))
  1181. return;
  1182.     save_flags(flags); cli();
  1183. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  1184.     restore_flags(flags);
  1185.     wake_up_interruptible(&tty->write_wait);
  1186.     if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP))
  1187.     && tty->ldisc.write_wakeup)
  1188. (tty->ldisc.write_wakeup)(tty);
  1189. } /* cy_flush_buffer */
  1190. /* This routine is called by the upper-layer tty layer to signal
  1191.    that incoming characters should be throttled or that the
  1192.    throttle should be released.
  1193.  */
  1194. static void
  1195. cy_throttle(struct tty_struct * tty)
  1196. {
  1197.   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
  1198.   unsigned long flags;
  1199.   volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
  1200.   int channel;
  1201. #ifdef SERIAL_DEBUG_THROTTLE
  1202.   char buf[64];
  1203.     printk("throttle %s: %d....n", tty_name(tty, buf),
  1204.    tty->ldisc.chars_in_buffer(tty));
  1205.     printk("cy_throttle ttyS%dn", info->line);
  1206. #endif
  1207.     if (serial_paranoia_check(info, tty->device, "cy_nthrottle")){
  1208.     return;
  1209.     }
  1210.     if (I_IXOFF(tty)) {
  1211. info->x_char = STOP_CHAR(tty);
  1212.     /* Should use the "Send Special Character" feature!!! */
  1213.     }
  1214.     channel = info->line;
  1215.     save_flags(flags); cli();
  1216. base_addr[CyCAR] = (u_char)channel;
  1217. base_addr[CyMSVR1] = 0;
  1218.     restore_flags(flags);
  1219.     return;
  1220. } /* cy_throttle */
  1221. static void
  1222. cy_unthrottle(struct tty_struct * tty)
  1223. {
  1224.   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
  1225.   unsigned long flags;
  1226.   volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
  1227.   int channel;
  1228. #ifdef SERIAL_DEBUG_THROTTLE
  1229.   char buf[64];
  1230.     printk("throttle %s: %d....n", tty_name(tty, buf),
  1231.    tty->ldisc.chars_in_buffer(tty));
  1232.     printk("cy_unthrottle ttyS%dn", info->line);
  1233. #endif
  1234.     if (serial_paranoia_check(info, tty->device, "cy_nthrottle")){
  1235.     return;
  1236.     }
  1237.     if (I_IXOFF(tty)) {
  1238. info->x_char = START_CHAR(tty);
  1239. /* Should use the "Send Special Character" feature!!! */
  1240.     }
  1241.     channel = info->line;
  1242.     save_flags(flags); cli();
  1243. base_addr[CyCAR] = (u_char)channel;
  1244. base_addr[CyMSVR1] = CyRTS;
  1245.     restore_flags(flags);
  1246.     return;
  1247. } /* cy_unthrottle */
  1248. static int
  1249. get_serial_info(struct cyclades_port * info,
  1250.                            struct serial_struct * retinfo)
  1251. {
  1252.   struct serial_struct tmp;
  1253. /* CP('g'); */
  1254.     if (!retinfo)
  1255.             return -EFAULT;
  1256.     memset(&tmp, 0, sizeof(tmp));
  1257.     tmp.type = info->type;
  1258.     tmp.line = info->line;
  1259.     tmp.port = info->line;
  1260.     tmp.irq = 0;
  1261.     tmp.flags = info->flags;
  1262.     tmp.baud_base = 0;          /*!!!*/
  1263.     tmp.close_delay = info->close_delay;
  1264.     tmp.custom_divisor = 0;     /*!!!*/
  1265.     tmp.hub6 = 0;               /*!!!*/
  1266.     return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
  1267. } /* get_serial_info */
  1268. static int
  1269. set_serial_info(struct cyclades_port * info,
  1270.                            struct serial_struct * new_info)
  1271. {
  1272.   struct serial_struct new_serial;
  1273.   struct cyclades_port old_info;
  1274. /* CP('s'); */
  1275.     if (!new_info)
  1276.     return -EFAULT;
  1277.     if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
  1278.     return -EFAULT;
  1279.     old_info = *info;
  1280.     if (!suser()) {
  1281.     if ((new_serial.close_delay != info->close_delay) ||
  1282. ((new_serial.flags & ASYNC_FLAGS & ~ASYNC_USR_MASK) !=
  1283.  (info->flags & ASYNC_FLAGS & ~ASYNC_USR_MASK)))
  1284.     return -EPERM;
  1285.     info->flags = ((info->flags & ~ASYNC_USR_MASK) |
  1286.    (new_serial.flags & ASYNC_USR_MASK));
  1287.     goto check_and_exit;
  1288.     }
  1289.     /*
  1290.      * OK, past this point, all the error checking has been done.
  1291.      * At this point, we start making changes.....
  1292.      */
  1293.     info->flags = ((info->flags & ~ASYNC_FLAGS) |
  1294.     (new_serial.flags & ASYNC_FLAGS));
  1295.     info->close_delay = new_serial.close_delay;
  1296. check_and_exit:
  1297.     if (info->flags & ASYNC_INITIALIZED){
  1298. config_setup(info);
  1299. return 0;
  1300.     }else{
  1301.         return startup(info);
  1302.     }
  1303. } /* set_serial_info */
  1304. static int
  1305. get_modem_info(struct cyclades_port * info, unsigned int *value)
  1306. {
  1307.   int channel;
  1308.   volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
  1309.   unsigned long flags;
  1310.   unsigned char status;
  1311.   unsigned int result;
  1312.     channel = info->line;
  1313.     save_flags(flags); cli();
  1314.         base_addr[CyCAR] = (u_char)channel;
  1315.         status = base_addr[CyMSVR1] | base_addr[CyMSVR2];
  1316.     restore_flags(flags);
  1317.     result =  ((status  & CyRTS) ? TIOCM_RTS : 0)
  1318.             | ((status  & CyDTR) ? TIOCM_DTR : 0)
  1319.             | ((status  & CyDCD) ? TIOCM_CAR : 0)
  1320.             | ((status  & CyDSR) ? TIOCM_DSR : 0)
  1321.             | ((status  & CyCTS) ? TIOCM_CTS : 0);
  1322.     return put_user(result,(unsigned int *) value);
  1323. } /* get_modem_info */
  1324. static int
  1325. set_modem_info(struct cyclades_port * info, unsigned int cmd,
  1326.                           unsigned int *value)
  1327. {
  1328.   int channel;
  1329.   volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
  1330.   unsigned long flags;
  1331.   unsigned int arg;
  1332.   
  1333.     if (get_user(arg, (unsigned long *) value))
  1334. return -EFAULT;
  1335.     channel = info->line;
  1336.     switch (cmd) {
  1337.     case TIOCMBIS:
  1338. if (arg & TIOCM_RTS){
  1339.     save_flags(flags); cli();
  1340. base_addr[CyCAR] = (u_char)channel;
  1341. base_addr[CyMSVR1] = CyRTS;
  1342.     restore_flags(flags);
  1343. }
  1344. if (arg & TIOCM_DTR){
  1345.     save_flags(flags); cli();
  1346.     base_addr[CyCAR] = (u_char)channel;
  1347. /* CP('S');CP('2'); */
  1348.     base_addr[CyMSVR2] = CyDTR;
  1349. #ifdef SERIAL_DEBUG_DTR
  1350.             printk("cyc: %d: raising DTRn", __LINE__);
  1351.             printk("     status: 0x%x, 0x%xn", base_addr[CyMSVR1], base_addr[CyMSVR2]);
  1352. #endif
  1353.     restore_flags(flags);
  1354. }
  1355. break;
  1356.     case TIOCMBIC:
  1357. if (arg & TIOCM_RTS){
  1358.     save_flags(flags); cli();
  1359. base_addr[CyCAR] = (u_char)channel;
  1360. base_addr[CyMSVR1] = 0;
  1361.     restore_flags(flags);
  1362. }
  1363. if (arg & TIOCM_DTR){
  1364.     save_flags(flags); cli();
  1365.     base_addr[CyCAR] = (u_char)channel;
  1366. /* CP('C');CP('2'); */
  1367.     base_addr[CyMSVR2] = 0;
  1368. #ifdef SERIAL_DEBUG_DTR
  1369.             printk("cyc: %d: dropping DTRn", __LINE__);
  1370.             printk("     status: 0x%x, 0x%xn", base_addr[CyMSVR1], base_addr[CyMSVR2]);
  1371. #endif
  1372.     restore_flags(flags);
  1373. }
  1374. break;
  1375.     case TIOCMSET:
  1376. if (arg & TIOCM_RTS){
  1377.     save_flags(flags); cli();
  1378. base_addr[CyCAR] = (u_char)channel;
  1379. base_addr[CyMSVR1] = CyRTS;
  1380.     restore_flags(flags);
  1381. }else{
  1382.     save_flags(flags); cli();
  1383. base_addr[CyCAR] = (u_char)channel;
  1384. base_addr[CyMSVR1] = 0;
  1385.     restore_flags(flags);
  1386. }
  1387. if (arg & TIOCM_DTR){
  1388.     save_flags(flags); cli();
  1389.     base_addr[CyCAR] = (u_char)channel;
  1390. /* CP('S');CP('3'); */
  1391.     base_addr[CyMSVR2] = CyDTR;
  1392. #ifdef SERIAL_DEBUG_DTR
  1393.             printk("cyc: %d: raising DTRn", __LINE__);
  1394.             printk("     status: 0x%x, 0x%xn", base_addr[CyMSVR1], base_addr[CyMSVR2]);
  1395. #endif
  1396.     restore_flags(flags);
  1397. }else{
  1398.     save_flags(flags); cli();
  1399.     base_addr[CyCAR] = (u_char)channel;
  1400. /* CP('C');CP('3'); */
  1401.     base_addr[CyMSVR2] = 0;
  1402. #ifdef SERIAL_DEBUG_DTR
  1403.             printk("cyc: %d: dropping DTRn", __LINE__);
  1404.             printk("     status: 0x%x, 0x%xn", base_addr[CyMSVR1], base_addr[CyMSVR2]);
  1405. #endif
  1406.     restore_flags(flags);
  1407. }
  1408. break;
  1409.     default:
  1410. return -EINVAL;
  1411.         }
  1412.     return 0;
  1413. } /* set_modem_info */
  1414. static void
  1415. send_break( struct cyclades_port * info, int duration)
  1416. { /* Let the transmit ISR take care of this (since it
  1417.      requires stuffing characters into the output stream).
  1418.    */
  1419.     info->x_break = duration;
  1420.     if (!info->xmit_cnt ) {
  1421. start_xmit(info);
  1422.     }
  1423. } /* send_break */
  1424. static int
  1425. get_mon_info(struct cyclades_port * info, struct cyclades_monitor * mon)
  1426. {
  1427.    if (copy_to_user(mon, &info->mon, sizeof(struct cyclades_monitor)))
  1428.    return -EFAULT;
  1429.    info->mon.int_count  = 0;
  1430.    info->mon.char_count = 0;
  1431.    info->mon.char_max   = 0;
  1432.    info->mon.char_last  = 0;
  1433.    return 0;
  1434. }
  1435. static int
  1436. set_threshold(struct cyclades_port * info, unsigned long *arg)
  1437. {
  1438.    volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
  1439.    unsigned long value;
  1440.    int channel;
  1441.    
  1442.    if (get_user(value, arg))
  1443.    return -EFAULT;
  1444.    channel = info->line;
  1445.    info->cor4 &= ~CyREC_FIFO;
  1446.    info->cor4 |= value & CyREC_FIFO;
  1447.    base_addr[CyCOR4] = info->cor4;
  1448.    return 0;
  1449. }
  1450. static int
  1451. get_threshold(struct cyclades_port * info, unsigned long *value)
  1452. {
  1453.    volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
  1454.    int channel;
  1455.    unsigned long tmp;
  1456.    
  1457.    channel = info->line;
  1458.    tmp = base_addr[CyCOR4] & CyREC_FIFO;
  1459.    return put_user(tmp,value);
  1460. }
  1461. static int
  1462. set_default_threshold(struct cyclades_port * info, unsigned long *arg)
  1463. {
  1464.    unsigned long value;
  1465.    if (get_user(value, arg))
  1466. return -EFAULT;
  1467.    info->default_threshold = value & 0x0f;
  1468.    return 0;
  1469. }
  1470. static int
  1471. get_default_threshold(struct cyclades_port * info, unsigned long *value)
  1472. {
  1473.    return put_user(info->default_threshold,value);
  1474. }
  1475. static int
  1476. set_timeout(struct cyclades_port * info, unsigned long *arg)
  1477. {
  1478.    volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
  1479.    int channel;
  1480.    unsigned long value;
  1481.    if (get_user(value, arg))
  1482.    return -EFAULT;
  1483.    
  1484.    channel = info->line;
  1485.    base_addr[CyRTPRL] = value & 0xff;
  1486.    base_addr[CyRTPRH] = (value >> 8) & 0xff;
  1487.    return 0;
  1488. }
  1489. static int
  1490. get_timeout(struct cyclades_port * info, unsigned long *value)
  1491. {
  1492.    volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
  1493.    int channel;
  1494.    unsigned long tmp;
  1495.    
  1496.    channel = info->line;
  1497.    tmp = base_addr[CyRTPRL];
  1498.    return put_user(tmp,value);
  1499. }
  1500. static int
  1501. set_default_timeout(struct cyclades_port * info, unsigned long value)
  1502. {
  1503.    info->default_timeout = value & 0xff;
  1504.    return 0;
  1505. }
  1506. static int
  1507. get_default_timeout(struct cyclades_port * info, unsigned long *value)
  1508. {
  1509.    return put_user(info->default_timeout,value);
  1510. }
  1511. static int
  1512. cy_ioctl(struct tty_struct *tty, struct file * file,
  1513.             unsigned int cmd, unsigned long arg)
  1514. {
  1515.   unsigned long val;
  1516.   struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
  1517.   int ret_val = 0;
  1518. #ifdef SERIAL_DEBUG_OTHER
  1519.     printk("cy_ioctl ttyS%d, cmd = %x arg = %lxn", info->line, cmd, arg); /* */
  1520. #endif
  1521.     switch (cmd) {
  1522.         case CYGETMON:
  1523.             ret_val = get_mon_info(info, (struct cyclades_monitor *)arg);
  1524.     break;
  1525.         case CYGETTHRESH:
  1526.     ret_val = get_threshold(info, (unsigned long *)arg);
  1527.       break;
  1528.         case CYSETTHRESH:
  1529.             ret_val = set_threshold(info, (unsigned long *)arg);
  1530.     break;
  1531.         case CYGETDEFTHRESH:
  1532.     ret_val = get_default_threshold(info, (unsigned long *)arg);
  1533.       break;
  1534.         case CYSETDEFTHRESH:
  1535.             ret_val = set_default_threshold(info, (unsigned long *)arg);
  1536.     break;
  1537.         case CYGETTIMEOUT:
  1538.     ret_val = get_timeout(info, (unsigned long *)arg);
  1539.       break;
  1540.         case CYSETTIMEOUT:
  1541.             ret_val = set_timeout(info, (unsigned long *)arg);
  1542.     break;
  1543.         case CYGETDEFTIMEOUT:
  1544.     ret_val = get_default_timeout(info, (unsigned long *)arg);
  1545.       break;
  1546.         case CYSETDEFTIMEOUT:
  1547.             ret_val = set_default_timeout(info, (unsigned long)arg);
  1548.     break;
  1549.         case TCSBRK:    /* SVID version: non-zero arg --> no break */
  1550.     ret_val = tty_check_change(tty);
  1551.     if (ret_val)
  1552.     break;
  1553.             tty_wait_until_sent(tty,0);
  1554.             if (!arg)
  1555.                 send_break(info, HZ/4); /* 1/4 second */
  1556.             break;
  1557.         case TCSBRKP:   /* support for POSIX tcsendbreak() */
  1558.     ret_val = tty_check_change(tty);
  1559.     if (ret_val)
  1560. break;
  1561.             tty_wait_until_sent(tty,0);
  1562.             send_break(info, arg ? arg*(HZ/10) : HZ/4);
  1563.             break;
  1564.         case TIOCMBIS:
  1565.         case TIOCMBIC:
  1566.         case TIOCMSET:
  1567.             ret_val = set_modem_info(info, cmd, (unsigned int *) arg);
  1568.             break;
  1569. /* The following commands are incompletely implemented!!! */
  1570.         case TIOCGSOFTCAR:
  1571.             ret_val = put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long *) arg);
  1572.             break;
  1573.         case TIOCSSOFTCAR:
  1574.             ret_val = get_user(val, (unsigned long *) arg);
  1575.     if (ret_val)
  1576.     break;
  1577.             tty->termios->c_cflag =
  1578.                     ((tty->termios->c_cflag & ~CLOCAL) | (val ? CLOCAL : 0));
  1579.             break;
  1580.         case TIOCMGET:
  1581.             ret_val = get_modem_info(info, (unsigned int *) arg);
  1582.             break;
  1583.         case TIOCGSERIAL:
  1584.             ret_val = get_serial_info(info, (struct serial_struct *) arg);
  1585.             break;
  1586.         case TIOCSSERIAL:
  1587.             ret_val = set_serial_info(info,
  1588.                                    (struct serial_struct *) arg);
  1589.             break;
  1590.         default:
  1591.     ret_val = -ENOIOCTLCMD;
  1592.     }
  1593. #ifdef SERIAL_DEBUG_OTHER
  1594.     printk("cy_ioctl donen");
  1595. #endif
  1596.     return ret_val;
  1597. } /* cy_ioctl */
  1598. static void
  1599. cy_set_termios(struct tty_struct *tty, struct termios * old_termios)
  1600. {
  1601.   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
  1602. #ifdef SERIAL_DEBUG_OTHER
  1603.     printk("cy_set_termios ttyS%dn", info->line);
  1604. #endif
  1605.     if (tty->termios->c_cflag == old_termios->c_cflag)
  1606.         return;
  1607.     config_setup(info);
  1608.     if ((old_termios->c_cflag & CRTSCTS) &&
  1609.         !(tty->termios->c_cflag & CRTSCTS)) {
  1610.             tty->stopped = 0;
  1611.             cy_start(tty);
  1612.     }
  1613. #ifdef tytso_patch_94Nov25_1726
  1614.     if (!(old_termios->c_cflag & CLOCAL) &&
  1615.         (tty->termios->c_cflag & CLOCAL))
  1616.             wake_up_interruptible(&info->open_wait);
  1617. #endif
  1618.     return;
  1619. } /* cy_set_termios */
  1620. static void
  1621. cy_close(struct tty_struct * tty, struct file * filp)
  1622. {
  1623.   struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
  1624. /* CP('C'); */
  1625. #ifdef SERIAL_DEBUG_OTHER
  1626.     printk("cy_close ttyS%dn", info->line);
  1627. #endif
  1628.     if (!info
  1629.     || serial_paranoia_check(info, tty->device, "cy_close")){
  1630.         return;
  1631.     }
  1632. #ifdef SERIAL_DEBUG_OPEN
  1633.     printk("cy_close ttyS%d, count = %dn", info->line, info->count);
  1634. #endif
  1635.     if ((tty->count == 1) && (info->count != 1)) {
  1636. /*
  1637.  * Uh, oh.  tty->count is 1, which means that the tty
  1638.  * structure will be freed.  Info->count should always
  1639.  * be one in these conditions.  If it's greater than
  1640.  * one, we've got real problems, since it means the
  1641.  * serial port won't be shutdown.
  1642.  */
  1643. printk("cy_close: bad serial port count; tty->count is 1, "
  1644.    "info->count is %dn", info->count);
  1645. info->count = 1;
  1646.     }
  1647. #ifdef SERIAL_DEBUG_COUNT
  1648.     printk("cyc: %d: decrementing count to %dn", __LINE__, info->count - 1);
  1649. #endif
  1650.     if (--info->count < 0) {
  1651. printk("cy_close: bad serial port count for ttys%d: %dn",
  1652.        info->line, info->count);
  1653. #ifdef SERIAL_DEBUG_COUNT
  1654.     printk("cyc: %d: setting count to 0n", __LINE__);
  1655. #endif
  1656. info->count = 0;
  1657.     }
  1658.     if (info->count)
  1659. return;
  1660.     info->flags |= ASYNC_CLOSING;
  1661.     /*
  1662.      * Save the termios structure, since this port may have
  1663.      * separate termios for callout and dialin.
  1664.      */
  1665.     if (info->flags & ASYNC_NORMAL_ACTIVE)
  1666. info->normal_termios = *tty->termios;
  1667.     if (info->flags & ASYNC_CALLOUT_ACTIVE)
  1668. info->callout_termios = *tty->termios;
  1669.     if (info->flags & ASYNC_INITIALIZED)
  1670. tty_wait_until_sent(tty, 3000); /* 30 seconds timeout */
  1671.     shutdown(info);
  1672.     if (tty->driver.flush_buffer)
  1673. tty->driver.flush_buffer(tty);
  1674.     if (tty->ldisc.flush_buffer)
  1675. tty->ldisc.flush_buffer(tty);
  1676.     info->event = 0;
  1677.     info->tty = 0;
  1678.     if (tty->ldisc.num != ldiscs[N_TTY].num) {
  1679. if (tty->ldisc.close)
  1680.     (tty->ldisc.close)(tty);
  1681. tty->ldisc = ldiscs[N_TTY];
  1682. tty->termios->c_line = N_TTY;
  1683. if (tty->ldisc.open)
  1684.     (tty->ldisc.open)(tty);
  1685.     }
  1686.     if (info->blocked_open) {
  1687. if (info->close_delay) {
  1688.     current->state = TASK_INTERRUPTIBLE;
  1689.     schedule_timeout(info->close_delay);
  1690. }
  1691. wake_up_interruptible(&info->open_wait);
  1692.     }
  1693.     info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
  1694.      ASYNC_CLOSING);
  1695.     wake_up_interruptible(&info->close_wait);
  1696. #ifdef SERIAL_DEBUG_OTHER
  1697.     printk("cy_close donen");
  1698. #endif
  1699.     return;
  1700. } /* cy_close */
  1701. /*
  1702.  * cy_hangup() --- called by tty_hangup() when a hangup is signaled.
  1703.  */
  1704. void
  1705. cy_hangup(struct tty_struct *tty)
  1706. {
  1707.   struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
  1708. #ifdef SERIAL_DEBUG_OTHER
  1709.     printk("cy_hangup ttyS%dn", info->line); /* */
  1710. #endif
  1711.     if (serial_paranoia_check(info, tty->device, "cy_hangup"))
  1712. return;
  1713.     
  1714.     shutdown(info);
  1715. #if 0
  1716.     info->event = 0;
  1717.     info->count = 0;
  1718. #ifdef SERIAL_DEBUG_COUNT
  1719.     printk("cyc: %d: setting count to 0n", __LINE__);
  1720. #endif
  1721.     info->tty = 0;
  1722. #endif
  1723.     info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
  1724.     wake_up_interruptible(&info->open_wait);
  1725. } /* cy_hangup */
  1726. /*
  1727.  * ------------------------------------------------------------
  1728.  * cy_open() and friends
  1729.  * ------------------------------------------------------------
  1730.  */
  1731. static int
  1732. block_til_ready(struct tty_struct *tty, struct file * filp,
  1733.                            struct cyclades_port *info)
  1734. {
  1735.   DECLARE_WAITQUEUE(wait, current);
  1736.   unsigned long flags;
  1737.   int channel;
  1738.   int retval;
  1739.   volatile u_char *base_addr = (u_char *)BASE_ADDR;
  1740.     /*
  1741.      * If the device is in the middle of being closed, then block
  1742.      * until it's done, and then try again.
  1743.      */
  1744.     if (info->flags & ASYNC_CLOSING) {
  1745. interruptible_sleep_on(&info->close_wait);
  1746. if (info->flags & ASYNC_HUP_NOTIFY){
  1747.     return -EAGAIN;
  1748. }else{
  1749.     return -ERESTARTSYS;
  1750. }
  1751.     }
  1752.     /*
  1753.      * If this is a callout device, then just make sure the normal
  1754.      * device isn't being used.
  1755.      */
  1756.     if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
  1757. if (info->flags & ASYNC_NORMAL_ACTIVE){
  1758.     return -EBUSY;
  1759. }
  1760. if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
  1761.     (info->flags & ASYNC_SESSION_LOCKOUT) &&
  1762.     (info->session != current->session)){
  1763.     return -EBUSY;
  1764. }
  1765. if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
  1766.     (info->flags & ASYNC_PGRP_LOCKOUT) &&
  1767.     (info->pgrp != current->pgrp)){
  1768.     return -EBUSY;
  1769. }
  1770. info->flags |= ASYNC_CALLOUT_ACTIVE;
  1771. return 0;
  1772.     }
  1773.     /*
  1774.      * If non-blocking mode is set, then make the check up front
  1775.      * and then exit.
  1776.      */
  1777.     if (filp->f_flags & O_NONBLOCK) {
  1778. if (info->flags & ASYNC_CALLOUT_ACTIVE){
  1779.     return -EBUSY;
  1780. }
  1781. info->flags |= ASYNC_NORMAL_ACTIVE;
  1782. return 0;
  1783.     }
  1784.     /*
  1785.      * Block waiting for the carrier detect and the line to become
  1786.      * free (i.e., not in use by the callout).  While we are in
  1787.      * this loop, info->count is dropped by one, so that
  1788.      * cy_close() knows when to free things.  We restore it upon
  1789.      * exit, either normal or abnormal.
  1790.      */
  1791.     retval = 0;
  1792.     add_wait_queue(&info->open_wait, &wait);
  1793. #ifdef SERIAL_DEBUG_OPEN
  1794.     printk("block_til_ready before block: ttyS%d, count = %dn",
  1795.    info->line, info->count);/**/
  1796. #endif
  1797.     info->count--;
  1798. #ifdef SERIAL_DEBUG_COUNT
  1799.     printk("cyc: %d: decrementing count to %dn", __LINE__, info->count);
  1800. #endif
  1801.     info->blocked_open++;
  1802.     channel = info->line;
  1803.     while (1) {
  1804. save_flags(flags); cli();
  1805.     if (!(info->flags & ASYNC_CALLOUT_ACTIVE)){
  1806. base_addr[CyCAR] = (u_char)channel;
  1807. base_addr[CyMSVR1] = CyRTS;
  1808. /* CP('S');CP('4'); */
  1809. base_addr[CyMSVR2] = CyDTR;
  1810. #ifdef SERIAL_DEBUG_DTR
  1811.                 printk("cyc: %d: raising DTRn", __LINE__);
  1812.                 printk("     status: 0x%x, 0x%xn", base_addr[CyMSVR1], base_addr[CyMSVR2]);
  1813. #endif
  1814.     }
  1815. restore_flags(flags);
  1816. set_current_state(TASK_INTERRUPTIBLE);
  1817. if (tty_hung_up_p(filp)
  1818. || !(info->flags & ASYNC_INITIALIZED) ){
  1819.     if (info->flags & ASYNC_HUP_NOTIFY) {
  1820. retval = -EAGAIN;
  1821.     }else{
  1822. retval = -ERESTARTSYS;
  1823.     }
  1824.     break;
  1825. }
  1826. save_flags(flags); cli();
  1827.     base_addr[CyCAR] = (u_char)channel;
  1828. /* CP('L');CP1(1 && C_CLOCAL(tty)); CP1(1 && (base_addr[CyMSVR1] & CyDCD) ); */
  1829.     if (!(info->flags & ASYNC_CALLOUT_ACTIVE)
  1830.     && !(info->flags & ASYNC_CLOSING)
  1831.     && (C_CLOCAL(tty)
  1832.         || (base_addr[CyMSVR1] & CyDCD))) {
  1833.     restore_flags(flags);
  1834.     break;
  1835.     }
  1836. restore_flags(flags);
  1837. if (signal_pending(current)) {
  1838.     retval = -ERESTARTSYS;
  1839.     break;
  1840. }
  1841. #ifdef SERIAL_DEBUG_OPEN
  1842. printk("block_til_ready blocking: ttyS%d, count = %dn",
  1843.        info->line, info->count);/**/
  1844. #endif
  1845. schedule();
  1846.     }
  1847.     current->state = TASK_RUNNING;
  1848.     remove_wait_queue(&info->open_wait, &wait);
  1849.     if (!tty_hung_up_p(filp)){
  1850. info->count++;
  1851. #ifdef SERIAL_DEBUG_COUNT
  1852.     printk("cyc: %d: incrementing count to %dn", __LINE__, info->count);
  1853. #endif
  1854.     }
  1855.     info->blocked_open--;
  1856. #ifdef SERIAL_DEBUG_OPEN
  1857.     printk("block_til_ready after blocking: ttyS%d, count = %dn",
  1858.    info->line, info->count);/**/
  1859. #endif
  1860.     if (retval)
  1861.     return retval;
  1862.     info->flags |= ASYNC_NORMAL_ACTIVE;
  1863.     return 0;
  1864. } /* block_til_ready */
  1865. /*
  1866.  * This routine is called whenever a serial port is opened.  It
  1867.  * performs the serial-specific initialization for the tty structure.
  1868.  */
  1869. int
  1870. cy_open(struct tty_struct *tty, struct file * filp)
  1871. {
  1872.   struct cyclades_port  *info;
  1873.   int retval, line;
  1874. /* CP('O'); */
  1875.     line = MINOR(tty->device) - tty->driver.minor_start;
  1876.     if ((line < 0) || (NR_PORTS <= line)){
  1877.         return -ENODEV;
  1878.     }
  1879.     info = &cy_port[line];
  1880.     if (info->line < 0){
  1881.         return -ENODEV;
  1882.     }
  1883. #ifdef SERIAL_DEBUG_OTHER
  1884.     printk("cy_open ttyS%dn", info->line); /* */
  1885. #endif
  1886.     if (serial_paranoia_check(info, tty->device, "cy_open")){
  1887.         return -ENODEV;
  1888.     }
  1889. #ifdef SERIAL_DEBUG_OPEN
  1890.     printk("cy_open ttyS%d, count = %dn", info->line, info->count);/**/
  1891. #endif
  1892.     info->count++;
  1893. #ifdef SERIAL_DEBUG_COUNT
  1894.     printk("cyc: %d: incrementing count to %dn", __LINE__, info->count);
  1895. #endif
  1896.     tty->driver_data = info;
  1897.     info->tty = tty;
  1898.     if (!tmp_buf) {
  1899. tmp_buf = (unsigned char *) get_free_page(GFP_KERNEL);
  1900. if (!tmp_buf){
  1901.     return -ENOMEM;
  1902.         }
  1903.     }
  1904.     if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
  1905. if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
  1906.     *tty->termios = info->normal_termios;
  1907. else 
  1908.     *tty->termios = info->callout_termios;
  1909.     }
  1910.     /*
  1911.      * Start up serial port
  1912.      */
  1913.     retval = startup(info);
  1914.     if (retval){
  1915. return retval;
  1916.     }
  1917.     retval = block_til_ready(tty, filp, info);
  1918.     if (retval) {
  1919. #ifdef SERIAL_DEBUG_OPEN
  1920. printk("cy_open returning after block_til_ready with %dn",
  1921.        retval);
  1922. #endif
  1923. return retval;
  1924.     }
  1925.     info->session = current->session;
  1926.     info->pgrp = current->pgrp;
  1927. #ifdef SERIAL_DEBUG_OPEN
  1928.     printk("cy_open donen");/**/
  1929. #endif
  1930.     return 0;
  1931. } /* cy_open */
  1932. /*
  1933.  * ---------------------------------------------------------------------
  1934.  * serial167_init() and friends
  1935.  *
  1936.  * serial167_init() is called at boot-time to initialize the serial driver.
  1937.  * ---------------------------------------------------------------------
  1938.  */
  1939. /*
  1940.  * This routine prints out the appropriate serial driver version
  1941.  * number, and identifies which options were configured into this
  1942.  * driver.
  1943.  */
  1944. static void
  1945. show_version(void)
  1946. {
  1947.     printk("MVME166/167 cd2401 drivern");
  1948. } /* show_version */
  1949. /* initialize chips on card -- return number of valid
  1950.    chips (which is number of ports/4) */
  1951. /*
  1952.  * This initialises the hardware to a reasonable state.  It should
  1953.  * probe the chip first so as to copy 166-Bug setup as a default for
  1954.  * port 0.  It initialises CMR to CyASYNC; that is never done again, so
  1955.  * as to limit the number of CyINIT_CHAN commands in normal running.
  1956.  *
  1957.  * ... I wonder what I should do if this fails ...
  1958.  */
  1959. void
  1960. mvme167_serial_console_setup(int cflag)
  1961. {
  1962. volatile unsigned char* base_addr = (u_char *)BASE_ADDR;
  1963. int ch;
  1964. u_char spd;
  1965. u_char rcor, rbpr, badspeed = 0;
  1966. unsigned long flags;
  1967. save_flags(flags); cli();
  1968. /*
  1969.  * First probe channel zero of the chip, to see what speed has
  1970.  * been selected.
  1971.  */
  1972. base_addr[CyCAR] = 0;
  1973. rcor = base_addr[CyRCOR] << 5;
  1974. rbpr = base_addr[CyRBPR];
  1975. for (spd = 0; spd < sizeof(baud_bpr); spd++)
  1976. if (rbpr == baud_bpr[spd] && rcor == baud_co[spd])
  1977. break;
  1978. if (spd >= sizeof(baud_bpr)) {
  1979. spd = 14; /* 19200 */
  1980. badspeed = 1; /* Failed to identify speed */
  1981. }
  1982. initial_console_speed = spd;
  1983. /* OK, we have chosen a speed, now reset and reinitialise */
  1984.         my_udelay(20000L); /* Allow time for any active o/p to complete */
  1985.         if(base_addr[CyCCR] != 0x00){
  1986.             restore_flags(flags);
  1987.             /* printk(" chip is never idle (CCR != 0)n"); */
  1988.             return;
  1989.         }
  1990.         base_addr[CyCCR] = CyCHIP_RESET; /* Reset the chip */
  1991.         my_udelay(1000L);
  1992.         if(base_addr[CyGFRCR] == 0x00){
  1993.             restore_flags(flags);
  1994.             /* printk(" chip is not responding (GFRCR stayed 0)n"); */
  1995.             return;
  1996.         }
  1997. /*
  1998.  * System clock is 20Mhz, divided by 2048, so divide by 10 for a 1.0ms
  1999.  * tick
  2000.  */
  2001. base_addr[CyTPR] = 10;
  2002. base_addr[CyPILR1] = 0x01;    /* Interrupt level for modem change */
  2003. base_addr[CyPILR2] = 0x02;    /* Interrupt level for tx ints */
  2004. base_addr[CyPILR3] = 0x03;    /* Interrupt level for rx ints */
  2005. /*
  2006.  * Attempt to set up all channels to something reasonable, and
  2007.  * bang out a INIT_CHAN command.  We should then be able to limit
  2008.  * the ammount of fiddling we have to do in normal running.
  2009.  */
  2010. for (ch = 3; ch >= 0 ; ch--) {
  2011. base_addr[CyCAR] = (u_char)ch;
  2012. base_addr[CyIER] = 0;
  2013. base_addr[CyCMR] = CyASYNC;
  2014. base_addr[CyLICR] = (u_char)ch << 2;
  2015. base_addr[CyLIVR] = 0x5c;
  2016. base_addr[CyTCOR] = baud_co[spd];
  2017. base_addr[CyTBPR] = baud_bpr[spd];
  2018. base_addr[CyRCOR] = baud_co[spd] >> 5;
  2019. base_addr[CyRBPR] = baud_bpr[spd];
  2020. base_addr[CySCHR1] = 'Q' & 0x1f;
  2021. base_addr[CySCHR2] = 'X' & 0x1f;
  2022. base_addr[CySCRL] = 0;
  2023. base_addr[CySCRH] = 0;
  2024. base_addr[CyCOR1] = Cy_8_BITS | CyPARITY_NONE;
  2025. base_addr[CyCOR2] = 0;
  2026. base_addr[CyCOR3] = Cy_1_STOP;
  2027. base_addr[CyCOR4] = baud_cor4[spd];
  2028. base_addr[CyCOR5] = 0;
  2029. base_addr[CyCOR6] = 0;
  2030. base_addr[CyCOR7] = 0;
  2031. base_addr[CyRTPRL] = 2;
  2032. base_addr[CyRTPRH] = 0;
  2033.         base_addr[CyMSVR1] = 0;
  2034.         base_addr[CyMSVR2] = 0;
  2035. write_cy_cmd(base_addr,CyINIT_CHAN|CyDIS_RCVR|CyDIS_XMTR);
  2036. }
  2037. /*
  2038.  * Now do specials for channel zero....
  2039.  */
  2040.         base_addr[CyMSVR1] = CyRTS;
  2041.         base_addr[CyMSVR2] = CyDTR;
  2042. base_addr[CyIER] = CyRxData;
  2043. write_cy_cmd(base_addr,CyENB_RCVR|CyENB_XMTR);
  2044. restore_flags(flags);
  2045. my_udelay(20000L); /* Let it all settle down */
  2046.         printk("CD2401 initialised,  chip is rev 0x%02xn", base_addr[CyGFRCR]);
  2047. if (badspeed)
  2048.          printk("  WARNING:  Failed to identify line speed, rcor=%02x,rbpr=%02xn",
  2049. rcor >> 5, rbpr);
  2050. } /* serial_console_init */
  2051. /* The serial driver boot-time initialization code!
  2052.     Hardware I/O ports are mapped to character special devices on a
  2053.     first found, first allocated manner.  That is, this code searches
  2054.     for Cyclom cards in the system.  As each is found, it is probed
  2055.     to discover how many chips (and thus how many ports) are present.
  2056.     These ports are mapped to the tty ports 64 and upward in monotonic
  2057.     fashion.  If an 8-port card is replaced with a 16-port card, the
  2058.     port mapping on a following card will shift.
  2059.     This approach is different from what is used in the other serial
  2060.     device driver because the Cyclom is more properly a multiplexer,
  2061.     not just an aggregation of serial ports on one card.
  2062.     If there are more cards with more ports than have been statically
  2063.     allocated above, a warning is printed and the extra ports are ignored.
  2064.  */
  2065. int
  2066. serial167_init(void)
  2067. {
  2068.   struct cyclades_port *info;
  2069.   int ret = 0;
  2070.   int good_ports = 0;
  2071.   int port_num = 0;
  2072.   int index;
  2073.   int DefSpeed;
  2074. #ifdef notyet
  2075.   struct sigaction sa;
  2076. #endif
  2077.     if (!(mvme16x_config &MVME16x_CONFIG_GOT_CD2401))
  2078. return 0;
  2079. #if 0
  2080. scrn[1] = '';
  2081. #endif
  2082.     show_version();
  2083.     /* Has "console=0,9600n8" been used in bootinfo to change speed? */
  2084.     if (serial_console_cflag)
  2085. DefSpeed = serial_console_cflag & 0017;
  2086.     else {
  2087. DefSpeed = initial_console_speed;
  2088. serial_console_info = &cy_port[0];
  2089. serial_console_cflag = DefSpeed | CS8;
  2090. #if 0
  2091. serial_console = 64; /*callout_driver.minor_start*/
  2092. #endif
  2093.     }
  2094.     /* Initialize the tty_driver structure */
  2095.     
  2096.     memset(&cy_serial_driver, 0, sizeof(struct tty_driver));
  2097.     cy_serial_driver.magic = TTY_DRIVER_MAGIC;
  2098. #ifdef CONFIG_DEVFS_FS
  2099.     cy_serial_driver.name = "tts/%d";
  2100. #else
  2101.     cy_serial_driver.name = "ttyS";
  2102. #endif
  2103.     cy_serial_driver.major = TTY_MAJOR;
  2104.     cy_serial_driver.minor_start = 64;
  2105.     cy_serial_driver.num = NR_PORTS;
  2106.     cy_serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
  2107.     cy_serial_driver.subtype = SERIAL_TYPE_NORMAL;
  2108.     cy_serial_driver.init_termios = tty_std_termios;
  2109.     cy_serial_driver.init_termios.c_cflag =
  2110.     B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  2111.     cy_serial_driver.flags = TTY_DRIVER_REAL_RAW;
  2112.     cy_serial_driver.refcount = &serial_refcount;
  2113.     cy_serial_driver.table = serial_table;
  2114.     cy_serial_driver.termios = serial_termios;
  2115.     cy_serial_driver.termios_locked = serial_termios_locked;
  2116.     cy_serial_driver.open = cy_open;
  2117.     cy_serial_driver.close = cy_close;
  2118.     cy_serial_driver.write = cy_write;
  2119.     cy_serial_driver.put_char = cy_put_char;
  2120.     cy_serial_driver.flush_chars = cy_flush_chars;
  2121.     cy_serial_driver.write_room = cy_write_room;
  2122.     cy_serial_driver.chars_in_buffer = cy_chars_in_buffer;
  2123.     cy_serial_driver.flush_buffer = cy_flush_buffer;
  2124.     cy_serial_driver.ioctl = cy_ioctl;
  2125.     cy_serial_driver.throttle = cy_throttle;
  2126.     cy_serial_driver.unthrottle = cy_unthrottle;
  2127.     cy_serial_driver.set_termios = cy_set_termios;
  2128.     cy_serial_driver.stop = cy_stop;
  2129.     cy_serial_driver.start = cy_start;
  2130.     cy_serial_driver.hangup = cy_hangup;
  2131.     /*
  2132.      * The callout device is just like normal device except for
  2133.      * major number and the subtype code.
  2134.      */
  2135.     cy_callout_driver = cy_serial_driver;
  2136. #ifdef CONFIG_DEVFS_FS
  2137.     cy_callout_driver.name = "cua/%d";
  2138. #else
  2139.     cy_callout_driver.name = "cua";
  2140. #endif
  2141.     cy_callout_driver.major = TTYAUX_MAJOR;
  2142.     cy_callout_driver.subtype = SERIAL_TYPE_CALLOUT;
  2143.     ret = tty_register_driver(&cy_serial_driver);
  2144.     if (ret) {
  2145.     printk(KERN_ERR "Couldn't register MVME166/7 serial drivern");
  2146.     return ret;
  2147.     }
  2148.     ret = tty_register_driver(&cy_callout_driver);
  2149.     if (ret) {
  2150.     printk(KERN_ERR "Couldn't register MVME166/7 callout drivern");
  2151.     goto cleanup_serial_driver;
  2152.     }
  2153.     init_bh(CYCLADES_BH, do_cyclades_bh);
  2154.     port_num = 0;
  2155.     info = cy_port;
  2156.     for (index = 0; index < 1; index++) {
  2157. good_ports = 4;
  2158. if(port_num < NR_PORTS){
  2159.     while( good_ports-- && port_num < NR_PORTS){
  2160. /*** initialize port ***/
  2161. info->magic = CYCLADES_MAGIC;
  2162. info->type = PORT_CIRRUS;
  2163. info->card = index;
  2164. info->line = port_num;
  2165. info->flags = STD_COM_FLAGS;
  2166. info->tty = 0;
  2167. info->xmit_fifo_size = 12;
  2168. info->cor1 = CyPARITY_NONE|Cy_8_BITS;
  2169. info->cor2 = CyETC;
  2170. info->cor3 = Cy_1_STOP;
  2171. info->cor4 = 0x08; /* _very_ small receive threshold */
  2172. info->cor5 = 0;
  2173. info->cor6 = 0;
  2174. info->cor7 = 0;
  2175. info->tbpr = baud_bpr[DefSpeed]; /* Tx BPR */
  2176. info->tco = baud_co[DefSpeed]; /* Tx CO */
  2177. info->rbpr = baud_bpr[DefSpeed]; /* Rx BPR */
  2178. info->rco = baud_co[DefSpeed] >> 5; /* Rx CO */
  2179. info->close_delay = 0;
  2180. info->x_char = 0;
  2181. info->event = 0;
  2182. info->count = 0;
  2183. #ifdef SERIAL_DEBUG_COUNT
  2184.     printk("cyc: %d: setting count to 0n", __LINE__);
  2185. #endif
  2186. info->blocked_open = 0;
  2187. info->default_threshold = 0;
  2188. info->default_timeout = 0;
  2189. info->tqueue.routine = do_softint;
  2190. info->tqueue.data = info;
  2191. info->callout_termios =cy_callout_driver.init_termios;
  2192. info->normal_termios = cy_serial_driver.init_termios;
  2193. init_waitqueue_head(&info->open_wait);
  2194. init_waitqueue_head(&info->close_wait);
  2195. /* info->session */
  2196. /* info->pgrp */
  2197. /*** !!!!!!!! this may expose new bugs !!!!!!!!! *********/
  2198. info->read_status_mask = CyTIMEOUT| CySPECHAR| CyBREAK
  2199.                                        | CyPARITY| CyFRAME| CyOVERRUN;
  2200. /* info->timeout */
  2201. printk("ttyS%1d ", info->line);
  2202. port_num++;info++;
  2203. if(!(port_num & 7)){
  2204.     printk("n               ");
  2205. }
  2206.     }
  2207. }
  2208. printk("n");
  2209.     }
  2210.     while( port_num < NR_PORTS){
  2211. info->line = -1;
  2212. port_num++;info++;
  2213.     }
  2214. #ifdef CONFIG_REMOTE_DEBUG
  2215.     debug_setup();
  2216. #endif
  2217.     ret = request_irq(MVME167_IRQ_SER_ERR, cd2401_rxerr_interrupt, 0,
  2218. "cd2401_errors", cd2401_rxerr_interrupt);
  2219.     if (ret) {
  2220.     printk(KERN_ERR "Could't get cd2401_errors IRQ");
  2221.     goto cleanup_callout_driver;
  2222.     }
  2223.     ret = request_irq(MVME167_IRQ_SER_MODEM, cd2401_modem_interrupt, 0,
  2224. "cd2401_modem", cd2401_modem_interrupt);
  2225.     if (ret) {
  2226.     printk(KERN_ERR "Could't get cd2401_modem IRQ");
  2227.     goto cleanup_irq_cd2401_errors;
  2228.     }
  2229.     ret = request_irq(MVME167_IRQ_SER_TX, cd2401_tx_interrupt, 0,
  2230. "cd2401_txints", cd2401_tx_interrupt);
  2231.     if (ret) {
  2232.     printk(KERN_ERR "Could't get cd2401_txints IRQ");
  2233.     goto cleanup_irq_cd2401_modem;
  2234.     }
  2235.     ret = request_irq(MVME167_IRQ_SER_RX, cd2401_rx_interrupt, 0,
  2236. "cd2401_rxints", cd2401_rx_interrupt);
  2237.     if (ret) {
  2238.     printk(KERN_ERR "Could't get cd2401_rxints IRQ");
  2239.     goto cleanup_irq_cd2401_txints;
  2240.     }
  2241.     /* Now we have registered the interrupt handlers, allow the interrupts */
  2242.     pcc2chip[PccSCCMICR] = 0x15; /* Serial ints are level 5 */
  2243.     pcc2chip[PccSCCTICR] = 0x15;
  2244.     pcc2chip[PccSCCRICR] = 0x15;
  2245.     pcc2chip[PccIMLR] = 3; /* Allow PCC2 ints above 3!? */
  2246.     return 0;
  2247. cleanup_irq_cd2401_txints:
  2248.     free_irq(MVME167_IRQ_SER_TX, cd2401_tx_interrupt);
  2249. cleanup_irq_cd2401_modem:
  2250.     free_irq(MVME167_IRQ_SER_MODEM, cd2401_modem_interrupt);
  2251. cleanup_irq_cd2401_errors:
  2252.     free_irq(MVME167_IRQ_SER_ERR, cd2401_rxerr_interrupt);
  2253. cleanup_callout_driver:
  2254.     if (tty_unregister_driver(&cy_callout_driver))
  2255.     printk(KERN_ERR "Couldn't unregister MVME166/7 callout drivern");
  2256. cleanup_serial_driver:
  2257.     if (tty_unregister_driver(&cy_serial_driver))
  2258.     printk(KERN_ERR "Couldn't unregister MVME166/7 serial drivern");
  2259.     return ret;
  2260. } /* serial167_init */
  2261. #ifdef CYCLOM_SHOW_STATUS
  2262. static void
  2263. show_status(int line_num)
  2264. {
  2265.   volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
  2266.   int channel;
  2267.   struct cyclades_port * info;
  2268.   unsigned long flags;
  2269.     info = &cy_port[line_num];
  2270.     channel = info->line;
  2271.     printk("  channel %dn", channel);/**/
  2272.     printk(" cy_portn");
  2273.     printk("  card line flags = %d %d %xn",
  2274.                  info->card, info->line, info->flags);
  2275.     printk("  *tty read_status_mask timeout xmit_fifo_size = %lx %x %x %xn",
  2276.                  (long)info->tty, info->read_status_mask,
  2277.                  info->timeout, info->xmit_fifo_size);
  2278.     printk("  cor1,cor2,cor3,cor4,cor5,cor6,cor7 = %x %x %x %x %x %x %xn",
  2279.              info->cor1, info->cor2, info->cor3, info->cor4, info->cor5,
  2280. info->cor6, info->cor7);
  2281.     printk("  tbpr,tco,rbpr,rco = %d %d %d %dn",
  2282.              info->tbpr, info->tco, info->rbpr, info->rco);
  2283.     printk("  close_delay event count = %d %d %dn",
  2284.              info->close_delay, info->event, info->count);
  2285.     printk("  x_char blocked_open = %x %xn",
  2286.              info->x_char, info->blocked_open);
  2287.     printk("  session pgrp open_wait = %lx %lx %lxn",
  2288.              info->session, info->pgrp, (long)info->open_wait);
  2289.     save_flags(flags); cli();
  2290. /* Global Registers */
  2291. printk(" CyGFRCR %xn", base_addr[CyGFRCR]);
  2292. printk(" CyCAR %xn", base_addr[CyCAR]);
  2293. printk(" CyRISR %xn", base_addr[CyRISR]);
  2294. printk(" CyTISR %xn", base_addr[CyTISR]);
  2295. printk(" CyMISR %xn", base_addr[CyMISR]);
  2296. printk(" CyRIR %xn", base_addr[CyRIR]);
  2297. printk(" CyTIR %xn", base_addr[CyTIR]);
  2298. printk(" CyMIR %xn", base_addr[CyMIR]);
  2299. printk(" CyTPR %xn", base_addr[CyTPR]);
  2300. base_addr[CyCAR] = (u_char)channel;
  2301. /* Virtual Registers */
  2302. #if 0
  2303. printk(" CyRIVR %xn", base_addr[CyRIVR]);
  2304. printk(" CyTIVR %xn", base_addr[CyTIVR]);
  2305. printk(" CyMIVR %xn", base_addr[CyMIVR]);
  2306. printk(" CyMISR %xn", base_addr[CyMISR]);
  2307. #endif
  2308. /* Channel Registers */
  2309. printk(" CyCCR %xn", base_addr[CyCCR]);
  2310. printk(" CyIER %xn", base_addr[CyIER]);
  2311. printk(" CyCOR1 %xn", base_addr[CyCOR1]);
  2312. printk(" CyCOR2 %xn", base_addr[CyCOR2]);
  2313. printk(" CyCOR3 %xn", base_addr[CyCOR3]);
  2314. printk(" CyCOR4 %xn", base_addr[CyCOR4]);
  2315. printk(" CyCOR5 %xn", base_addr[CyCOR5]);
  2316. #if 0
  2317. printk(" CyCCSR %xn", base_addr[CyCCSR]);
  2318. printk(" CyRDCR %xn", base_addr[CyRDCR]);
  2319. #endif
  2320. printk(" CySCHR1 %xn", base_addr[CySCHR1]);
  2321. printk(" CySCHR2 %xn", base_addr[CySCHR2]);
  2322. #if 0
  2323. printk(" CySCHR3 %xn", base_addr[CySCHR3]);
  2324. printk(" CySCHR4 %xn", base_addr[CySCHR4]);
  2325. printk(" CySCRL %xn", base_addr[CySCRL]);
  2326. printk(" CySCRH %xn", base_addr[CySCRH]);
  2327. printk(" CyLNC %xn", base_addr[CyLNC]);
  2328. printk(" CyMCOR1 %xn", base_addr[CyMCOR1]);
  2329. printk(" CyMCOR2 %xn", base_addr[CyMCOR2]);
  2330. #endif
  2331. printk(" CyRTPRL %xn", base_addr[CyRTPRL]);
  2332. printk(" CyRTPRH %xn", base_addr[CyRTPRH]);
  2333. printk(" CyMSVR1 %xn", base_addr[CyMSVR1]);
  2334. printk(" CyMSVR2 %xn", base_addr[CyMSVR2]);
  2335. printk(" CyRBPR %xn", base_addr[CyRBPR]);
  2336. printk(" CyRCOR %xn", base_addr[CyRCOR]);
  2337. printk(" CyTBPR %xn", base_addr[CyTBPR]);
  2338. printk(" CyTCOR %xn", base_addr[CyTCOR]);
  2339.     restore_flags(flags);
  2340. } /* show_status */
  2341. #endif
  2342. #if 0
  2343. /* Dummy routine in mvme16x/config.c for now */
  2344. /* Serial console setup. Called from linux/init/main.c */
  2345. void console_setup(char *str, int *ints)
  2346. {
  2347. char *s;
  2348. int baud, bits, parity;
  2349. int cflag = 0;
  2350. /* Sanity check. */
  2351. if (ints[0] > 3 || ints[1] > 3) return;
  2352. /* Get baud, bits and parity */
  2353. baud = 2400;
  2354. bits = 8;
  2355. parity = 'n';
  2356. if (ints[2]) baud = ints[2];
  2357. if ((s = strchr(str, ','))) {
  2358. do {
  2359. s++;
  2360. } while(*s >= '0' && *s <= '9');
  2361. if (*s) parity = *s++;
  2362. if (*s) bits   = *s - '0';
  2363. }
  2364. /* Now construct a cflag setting. */
  2365. switch(baud) {
  2366. case 1200:
  2367. cflag |= B1200;
  2368. break;
  2369. case 9600:
  2370. cflag |= B9600;
  2371. break;
  2372. case 19200:
  2373. cflag |= B19200;
  2374. break;
  2375. case 38400:
  2376. cflag |= B38400;
  2377. break;
  2378. case 2400:
  2379. default:
  2380. cflag |= B2400;
  2381. break;
  2382. }
  2383. switch(bits) {
  2384. case 7:
  2385. cflag |= CS7;
  2386. break;
  2387. default:
  2388. case 8:
  2389. cflag |= CS8;
  2390. break;
  2391. }
  2392. switch(parity) {
  2393. case 'o': case 'O':
  2394. cflag |= PARODD;
  2395. break;
  2396. case 'e': case 'E':
  2397. cflag |= PARENB;
  2398. break;
  2399. }
  2400. serial_console_info = &cy_port[ints[1]];
  2401. serial_console_cflag = cflag;
  2402. serial_console = ints[1] + 64; /*callout_driver.minor_start*/
  2403. }
  2404. #endif
  2405. /*
  2406.  * The following is probably out of date for 2.1.x serial console stuff.
  2407.  *
  2408.  * The console is registered early on from arch/m68k/kernel/setup.c, and
  2409.  * it therefore relies on the chip being setup correctly by 166-Bug.  This
  2410.  * seems reasonable, as the serial port has been used to invoke the system
  2411.  * boot.  It also means that this function must not rely on any data
  2412.  * initialisation performed by serial167_init() etc.
  2413.  *
  2414.  * Of course, once the console has been registered, we had better ensure
  2415.  * that serial167_init() doesn't leave the chip non-functional.
  2416.  *
  2417.  * The console must be locked when we get here.
  2418.  */
  2419. void serial167_console_write(struct console *co, const char *str, unsigned count)
  2420. {
  2421. volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
  2422. unsigned long flags;
  2423. volatile u_char sink;
  2424. u_char ier;
  2425. int port;
  2426. u_char do_lf = 0;
  2427. int i = 0;
  2428. save_flags(flags); cli();
  2429. /* Ensure transmitter is enabled! */
  2430. port = 0;
  2431. base_addr[CyCAR] = (u_char)port;
  2432. while (base_addr[CyCCR])
  2433. ;
  2434. base_addr[CyCCR] = CyENB_XMTR;
  2435. ier = base_addr[CyIER];
  2436. base_addr[CyIER] = CyTxMpty;
  2437. while (1) {
  2438. if (pcc2chip[PccSCCTICR] & 0x20)
  2439. {
  2440. /* We have a Tx int. Acknowledge it */
  2441. sink = pcc2chip[PccTPIACKR];
  2442. if ((base_addr[CyLICR] >> 2) == port) {
  2443. if (i == count) {
  2444. /* Last char of string is now output */
  2445. base_addr[CyTEOIR] = CyNOTRANS;
  2446. break;
  2447. }
  2448. if (do_lf) {
  2449. base_addr[CyTDR] = 'n';
  2450. str++;
  2451. i++;
  2452. do_lf = 0;
  2453. }
  2454. else if (*str == 'n') {
  2455. base_addr[CyTDR] = 'r';
  2456. do_lf = 1;
  2457. }
  2458. else {
  2459. base_addr[CyTDR] = *str++;
  2460. i++;
  2461. }
  2462. base_addr[CyTEOIR] = 0;
  2463. }
  2464. else
  2465. base_addr[CyTEOIR] = CyNOTRANS;
  2466. }
  2467. }
  2468. base_addr[CyIER] = ier;
  2469. restore_flags(flags);
  2470. }
  2471. static kdev_t serial167_console_device(struct console *c)
  2472. {
  2473. return MKDEV(TTY_MAJOR, 64 + c->index);
  2474. }
  2475. static int __init serial167_console_setup(struct console *co, char *options)
  2476. {
  2477. return 0;
  2478. }
  2479. static struct console sercons = {
  2480. name: "ttyS",
  2481. write: serial167_console_write,
  2482. device: serial167_console_device,
  2483. setup: serial167_console_setup,
  2484. flags: CON_PRINTBUFFER,
  2485. index: -1,
  2486. };
  2487. void __init serial167_console_init(void)
  2488. {
  2489. if (vme_brdtype == VME_TYPE_MVME166 ||
  2490. vme_brdtype == VME_TYPE_MVME167 ||
  2491. vme_brdtype == VME_TYPE_MVME177) {
  2492. mvme167_serial_console_setup(0);
  2493. register_console(&sercons);
  2494. }
  2495. }
  2496. #ifdef CONFIG_REMOTE_DEBUG
  2497. void putDebugChar (int c)
  2498. {
  2499. volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
  2500. unsigned long flags;
  2501. volatile u_char sink;
  2502. u_char ier;
  2503. int port;
  2504. save_flags(flags); cli();
  2505. /* Ensure transmitter is enabled! */
  2506. port = DEBUG_PORT;
  2507. base_addr[CyCAR] = (u_char)port;
  2508. while (base_addr[CyCCR])
  2509. ;
  2510. base_addr[CyCCR] = CyENB_XMTR;
  2511. ier = base_addr[CyIER];
  2512. base_addr[CyIER] = CyTxMpty;
  2513. while (1) {
  2514. if (pcc2chip[PccSCCTICR] & 0x20)
  2515. {
  2516. /* We have a Tx int. Acknowledge it */
  2517. sink = pcc2chip[PccTPIACKR];
  2518. if ((base_addr[CyLICR] >> 2) == port) {
  2519. base_addr[CyTDR] = c;
  2520. base_addr[CyTEOIR] = 0;
  2521. break;
  2522. }
  2523. else
  2524. base_addr[CyTEOIR] = CyNOTRANS;
  2525. }
  2526. }
  2527. base_addr[CyIER] = ier;
  2528. restore_flags(flags);
  2529. }
  2530. int getDebugChar()
  2531. {
  2532. volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
  2533. unsigned long flags;
  2534. volatile u_char sink;
  2535. u_char ier;
  2536. int port;
  2537. int i, c;
  2538. i = debugiq.out;
  2539. if (i != debugiq.in) {
  2540. c = debugiq.buf[i];
  2541. if (++i == DEBUG_LEN)
  2542. i = 0;
  2543. debugiq.out = i;
  2544. return c;
  2545. }
  2546. /* OK, nothing in queue, wait in poll loop */
  2547. save_flags(flags); cli();
  2548. /* Ensure receiver is enabled! */
  2549. port = DEBUG_PORT;
  2550. base_addr[CyCAR] = (u_char)port;
  2551. #if 0
  2552. while (base_addr[CyCCR])
  2553. ;
  2554. base_addr[CyCCR] = CyENB_RCVR;
  2555. #endif
  2556. ier = base_addr[CyIER];
  2557. base_addr[CyIER] = CyRxData;
  2558. while (1) {
  2559. if (pcc2chip[PccSCCRICR] & 0x20)
  2560. {
  2561. /* We have a Rx int. Acknowledge it */
  2562. sink = pcc2chip[PccRPIACKR];
  2563. if ((base_addr[CyLICR] >> 2) == port) {
  2564. int cnt = base_addr[CyRFOC];
  2565. while (cnt-- > 0)
  2566. {
  2567. c = base_addr[CyRDR];
  2568. if (c == 0)
  2569. printk ("!! debug char is null (cnt=%d) !!", cnt);
  2570. else
  2571. queueDebugChar (c);
  2572. }
  2573. base_addr[CyREOIR] = 0;
  2574. i = debugiq.out;
  2575. if (i == debugiq.in)
  2576. panic ("Debug input queue empty!");
  2577. c = debugiq.buf[i];
  2578. if (++i == DEBUG_LEN)
  2579. i = 0;
  2580. debugiq.out = i;
  2581. break;
  2582. }
  2583. else
  2584. base_addr[CyREOIR] = CyNOTRANS;
  2585. }
  2586. }
  2587. base_addr[CyIER] = ier;
  2588. restore_flags(flags);
  2589. return (c);
  2590. }
  2591. void queueDebugChar (int c)
  2592. {
  2593. int i;
  2594. i = debugiq.in;
  2595. debugiq.buf[i] = c;
  2596. if (++i == DEBUG_LEN)
  2597. i = 0;
  2598. if (i != debugiq.out)
  2599. debugiq.in = i;
  2600. }
  2601. static void
  2602. debug_setup()
  2603. {
  2604.   unsigned long flags;
  2605.   volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
  2606.   int   i, cflag;
  2607.     cflag = B19200;
  2608.     save_flags(flags); cli();
  2609.     for (i = 0; i < 4; i++)
  2610.     {
  2611. base_addr[CyCAR] = i;
  2612. base_addr[CyLICR] = i << 2;
  2613.     }
  2614.     debugiq.in = debugiq.out = 0;
  2615.     base_addr[CyCAR] = DEBUG_PORT;
  2616.     /* baud rate */
  2617.     i = cflag & CBAUD;
  2618.     base_addr[CyIER] = 0;
  2619.     base_addr[CyCMR] = CyASYNC;
  2620.     base_addr[CyLICR] = DEBUG_PORT << 2;
  2621.     base_addr[CyLIVR] = 0x5c;
  2622.     /* tx and rx baud rate */
  2623.     base_addr[CyTCOR] = baud_co[i];
  2624.     base_addr[CyTBPR] = baud_bpr[i];
  2625.     base_addr[CyRCOR] = baud_co[i] >> 5;
  2626.     base_addr[CyRBPR] = baud_bpr[i];
  2627.     /* set line characteristics  according configuration */
  2628.     base_addr[CySCHR1] = 0;
  2629.     base_addr[CySCHR2] = 0;
  2630.     base_addr[CySCRL] = 0;
  2631.     base_addr[CySCRH] = 0;
  2632.     base_addr[CyCOR1] = Cy_8_BITS | CyPARITY_NONE;
  2633.     base_addr[CyCOR2] = 0;
  2634.     base_addr[CyCOR3] = Cy_1_STOP;
  2635.     base_addr[CyCOR4] = baud_cor4[i];
  2636.     base_addr[CyCOR5] = 0;
  2637.     base_addr[CyCOR6] = 0;
  2638.     base_addr[CyCOR7] = 0;
  2639.     write_cy_cmd(base_addr,CyINIT_CHAN);
  2640.     write_cy_cmd(base_addr,CyENB_RCVR);
  2641.     base_addr[CyCAR] = DEBUG_PORT; /* !!! Is this needed? */
  2642.     base_addr[CyRTPRL] = 2;
  2643.     base_addr[CyRTPRH] = 0;
  2644.     base_addr[CyMSVR1] = CyRTS;
  2645.     base_addr[CyMSVR2] = CyDTR;
  2646.     base_addr[CyIER] = CyRxData;
  2647.     restore_flags(flags);
  2648. } /* debug_setup */
  2649. #endif
  2650. MODULE_LICENSE("GPL");