Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / drivers / char / rio / riointr.c
1 /*
2 ** -----------------------------------------------------------------------------
3 **
4 **  Perle Specialix driver for Linux
5 **  Ported from existing RIO Driver for SCO sources.
6  *
7  *  (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8  *
9  *      This program is free software; you can redistribute it and/or modify
10  *      it under the terms of the GNU General Public License as published by
11  *      the Free Software Foundation; either version 2 of the License, or
12  *      (at your option) any later version.
13  *
14  *      This program is distributed in the hope that it will be useful,
15  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *      GNU General Public License for more details.
18  *
19  *      You should have received a copy of the GNU General Public License
20  *      along with this program; if not, write to the Free Software
21  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 **
23 **      Module          : riointr.c
24 **      SID             : 1.2
25 **      Last Modified   : 11/6/98 10:33:44
26 **      Retrieved       : 11/6/98 10:33:49
27 **
28 **  ident @(#)riointr.c 1.2
29 **
30 ** -----------------------------------------------------------------------------
31 */
32 #ifdef SCCS_LABELS
33 static char *_riointr_c_sccs_ = "@(#)riointr.c  1.2";
34 #endif
35
36
37 #include <linux/module.h>
38 #include <linux/slab.h>
39 #include <linux/errno.h>
40 #include <linux/tty.h>
41 #include <asm/io.h>
42 #include <asm/system.h>
43 #include <asm/string.h>
44 #include <asm/semaphore.h>
45 #include <asm/uaccess.h>
46
47 #include <linux/termios.h>
48 #include <linux/serial.h>
49
50 #include <linux/generic_serial.h>
51
52 #include <linux/delay.h>
53
54 #include "linux_compat.h"
55 #include "rio_linux.h"
56 #include "typdef.h"
57 #include "pkt.h"
58 #include "daemon.h"
59 #include "rio.h"
60 #include "riospace.h"
61 #include "top.h"
62 #include "cmdpkt.h"
63 #include "map.h"
64 #include "riotypes.h"
65 #include "rup.h"
66 #include "port.h"
67 #include "riodrvr.h"
68 #include "rioinfo.h"
69 #include "func.h"
70 #include "errors.h"
71 #include "pci.h"
72
73 #include "parmmap.h"
74 #include "unixrup.h"
75 #include "board.h"
76 #include "host.h"
77 #include "error.h"
78 #include "phb.h"
79 #include "link.h"
80 #include "cmdblk.h"
81 #include "route.h"
82 #include "control.h"
83 #include "cirrus.h"
84 #include "rioioctl.h"
85
86
87 static void RIOReceive(struct rio_info *, struct Port *);
88
89
90 static char *firstchars (char *p, int nch)
91 {
92   static char buf[2][128];
93   static int t=0;
94   t = ! t;
95   memcpy (buf[t], p, nch);
96   buf[t][nch] = 0;
97   return buf[t];
98 }
99
100
101 #define INCR( P, I )    ((P) = (((P)+(I)) & p->RIOBufferMask))
102 /* Enable and start the transmission of packets */
103 void
104 RIOTxEnable(en)
105 char *          en;
106 {
107   struct Port * PortP;
108   struct rio_info *p;
109   struct tty_struct* tty;
110   int c;
111   struct PKT *  PacketP;
112   unsigned long flags;
113
114   PortP = (struct Port *)en; 
115   p = (struct rio_info *)PortP->p;
116   tty = PortP->gs.tty;
117
118
119   rio_dprintk (RIO_DEBUG_INTR, "tx port %d: %d chars queued.\n", 
120               PortP->PortNum, PortP->gs.xmit_cnt);
121
122   if (!PortP->gs.xmit_cnt) return;
123   
124
125   /* This routine is an order of magnitude simpler than the specialix
126      version. One of the disadvantages is that this version will send
127      an incomplete packet (usually 64 bytes instead of 72) once for
128      every 4k worth of data. Let's just say that this won't influence
129      performance significantly..... */
130
131   rio_spin_lock_irqsave(&PortP->portSem, flags);
132
133   while (can_add_transmit( &PacketP, PortP )) {
134     c = PortP->gs.xmit_cnt;
135     if (c > PKT_MAX_DATA_LEN) c = PKT_MAX_DATA_LEN;
136
137     /* Don't copy past the end of the source buffer */
138     if (c > SERIAL_XMIT_SIZE - PortP->gs.xmit_tail) 
139       c = SERIAL_XMIT_SIZE - PortP->gs.xmit_tail;
140
141     { int t;
142     t = (c > 10)?10:c;
143     
144     rio_dprintk (RIO_DEBUG_INTR, "rio: tx port %d: copying %d chars: %s - %s\n", 
145                  PortP->PortNum, c, 
146                  firstchars (PortP->gs.xmit_buf + PortP->gs.xmit_tail      , t),
147                  firstchars (PortP->gs.xmit_buf + PortP->gs.xmit_tail + c-t, t));
148     }
149     /* If for one reason or another, we can't copy more data, 
150        we're done! */
151     if (c == 0) break;
152
153     rio_memcpy_toio (PortP->HostP->Caddr, (caddr_t)PacketP->data, 
154                  PortP->gs.xmit_buf + PortP->gs.xmit_tail, c);
155     /*    udelay (1); */
156
157     writeb (c, &(PacketP->len));
158     if (!( PortP->State & RIO_DELETED ) ) {
159       add_transmit ( PortP );
160       /*
161       ** Count chars tx'd for port statistics reporting
162       */
163       if ( PortP->statsGather )
164         PortP->txchars += c;
165     }
166     PortP->gs.xmit_tail = (PortP->gs.xmit_tail + c) & (SERIAL_XMIT_SIZE-1);
167     PortP->gs.xmit_cnt -= c;
168   }
169
170   rio_spin_unlock_irqrestore(&PortP->portSem, flags);
171
172   if (PortP->gs.xmit_cnt <= (PortP->gs.wakeup_chars + 2*PKT_MAX_DATA_LEN)) {
173     rio_dprintk (RIO_DEBUG_INTR, "Waking up.... ldisc:%d (%d/%d)....",
174                  (int)(PortP->gs.tty->flags & (1 << TTY_DO_WRITE_WAKEUP)),
175                  PortP->gs.wakeup_chars, PortP->gs.xmit_cnt); 
176     if ((PortP->gs.tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
177         PortP->gs.tty->ldisc.write_wakeup)
178       (PortP->gs.tty->ldisc.write_wakeup)(PortP->gs.tty);
179     rio_dprintk (RIO_DEBUG_INTR, "(%d/%d)\n",
180                 PortP->gs.wakeup_chars, PortP->gs.xmit_cnt); 
181     wake_up_interruptible(&PortP->gs.tty->write_wait);
182   }
183
184 }
185
186
187 /*
188 ** RIO Host Service routine. Does all the work traditionally associated with an
189 ** interrupt.
190 */
191 static int      RupIntr;
192 static int      RxIntr;
193 static int      TxIntr;
194 void
195 RIOServiceHost(p, HostP, From)
196 struct rio_info *       p;
197 struct Host *HostP;
198 int From; 
199 {
200   rio_spin_lock (&HostP->HostLock);
201   if ( (HostP->Flags & RUN_STATE) != RC_RUNNING ) { 
202     static int t =0;
203     rio_spin_unlock (&HostP->HostLock); 
204     if ((t++ % 200) == 0)
205       rio_dprintk (RIO_DEBUG_INTR, "Interrupt but host not running. flags=%x.\n", (int)HostP->Flags);
206     return;
207   }
208   rio_spin_unlock (&HostP->HostLock); 
209
210   if ( RWORD( HostP->ParmMapP->rup_intr ) ) {
211     WWORD( HostP->ParmMapP->rup_intr , 0 );
212     p->RIORupCount++;
213     RupIntr++;
214     rio_dprintk (RIO_DEBUG_INTR, "rio: RUP interrupt on host %d\n", HostP-p->RIOHosts);
215     RIOPollHostCommands(p, HostP );
216   }
217
218   if ( RWORD( HostP->ParmMapP->rx_intr ) ) {
219     int port;
220
221     WWORD( HostP->ParmMapP->rx_intr , 0 );
222     p->RIORxCount++;
223     RxIntr++;
224
225     rio_dprintk (RIO_DEBUG_INTR, "rio: RX interrupt on host %d\n", HostP-p->RIOHosts);
226     /*
227     ** Loop through every port. If the port is mapped into
228     ** the system ( i.e. has /dev/ttyXXXX associated ) then it is
229     ** worth checking. If the port isn't open, grab any packets
230     ** hanging on its receive queue and stuff them on the free
231     ** list; check for commands on the way.
232     */
233     for ( port=p->RIOFirstPortsBooted; 
234           port<p->RIOLastPortsBooted+PORTS_PER_RTA; port++ ) {
235       struct Port *PortP = p->RIOPortp[port];
236       struct tty_struct *ttyP;
237       struct PKT *PacketP;
238                 
239       /*
240       ** not mapped in - most of the RIOPortp[] information
241       ** has not been set up!
242       ** Optimise: ports come in bundles of eight.
243       */
244       if ( !PortP->Mapped ) {
245         port += 7;
246         continue; /* with the next port */
247       }
248
249       /*
250       ** If the host board isn't THIS host board, check the next one.
251       ** optimise: ports come in bundles of eight.
252       */
253       if ( PortP->HostP != HostP ) {
254         port += 7;
255         continue;
256       }
257
258       /*
259       ** Let us see - is the port open? If not, then don't service it.
260       */
261       if ( !( PortP->PortState & PORT_ISOPEN ) ) {
262         continue;
263       }
264
265       /*
266       ** find corresponding tty structure. The process of mapping
267       ** the ports puts these here.
268       */
269       ttyP = PortP->gs.tty;
270
271       /*
272       ** Lock the port before we begin working on it.
273       */
274       rio_spin_lock(&PortP->portSem);
275
276       /*
277       ** Process received data if there is any.
278       */
279       if ( can_remove_receive( &PacketP, PortP ) )
280         RIOReceive(p, PortP);
281
282       /*
283       ** If there is no data left to be read from the port, and
284       ** it's handshake bit is set, then we must clear the handshake,
285       ** so that that downstream RTA is re-enabled.
286       */
287       if ( !can_remove_receive( &PacketP, PortP ) && 
288            ( RWORD( PortP->PhbP->handshake )==PHB_HANDSHAKE_SET ) ) {
289                                 /*
290                                 ** MAGIC! ( Basically, handshake the RX buffer, so that
291                                 ** the RTAs upstream can be re-enabled. )
292                                 */
293         rio_dprintk (RIO_DEBUG_INTR, "Set RX handshake bit\n");
294         WWORD( PortP->PhbP->handshake, 
295                PHB_HANDSHAKE_SET|PHB_HANDSHAKE_RESET );
296       }
297       rio_spin_unlock(&PortP->portSem);
298     }
299   }
300
301   if ( RWORD( HostP->ParmMapP->tx_intr ) ) {
302     int port;
303
304     WWORD( HostP->ParmMapP->tx_intr , 0);
305
306     p->RIOTxCount++;
307     TxIntr++;
308     rio_dprintk (RIO_DEBUG_INTR, "rio: TX interrupt on host %d\n", HostP-p->RIOHosts);
309
310     /*
311     ** Loop through every port.
312     ** If the port is mapped into the system ( i.e. has /dev/ttyXXXX
313     ** associated ) then it is worth checking.
314     */
315     for ( port=p->RIOFirstPortsBooted; 
316           port<p->RIOLastPortsBooted+PORTS_PER_RTA; port++ ) {
317       struct Port *PortP = p->RIOPortp[port];
318       struct tty_struct *ttyP;
319       struct PKT *PacketP;
320
321       /*
322       ** not mapped in - most of the RIOPortp[] information
323       ** has not been set up!
324       */
325       if ( !PortP->Mapped ) {
326         port += 7;
327         continue; /* with the next port */
328       }
329
330       /*
331       ** If the host board isn't running, then its data structures
332       ** are no use to us - continue quietly.
333       */
334       if ( PortP->HostP != HostP ) {
335         port += 7;
336         continue; /* with the next port */
337       }
338
339       /*
340       ** Let us see - is the port open? If not, then don't service it.
341       */
342       if ( !( PortP->PortState & PORT_ISOPEN ) ) {
343         continue;
344       }
345
346       rio_dprintk (RIO_DEBUG_INTR, "rio: Looking into port %d.\n", port);
347       /*
348       ** Lock the port before we begin working on it.
349       */
350       rio_spin_lock(&PortP->portSem);
351
352       /*
353       ** If we can't add anything to the transmit queue, then
354       ** we need do none of this processing.
355       */
356       if ( !can_add_transmit( &PacketP, PortP ) ) {
357         rio_dprintk (RIO_DEBUG_INTR, "Can't add to port, so skipping.\n");
358         rio_spin_unlock(&PortP->portSem);
359         continue;
360       }
361
362       /*
363       ** find corresponding tty structure. The process of mapping
364       ** the ports puts these here.
365       */
366       ttyP = PortP->gs.tty;
367       /* If ttyP is NULL, the port is getting closed. Forget about it. */
368       if (!ttyP) {
369         rio_dprintk (RIO_DEBUG_INTR, "no tty, so skipping.\n");
370         rio_spin_unlock(&PortP->portSem);
371         continue;
372       }
373       /*
374       ** If there is more room available we start up the transmit
375       ** data process again. This can be direct I/O, if the cookmode
376       ** is set to COOK_RAW or COOK_MEDIUM, or will be a call to the
377       ** riotproc( T_OUTPUT ) if we are in COOK_WELL mode, to fetch
378       ** characters via the line discipline. We must always call
379       ** the line discipline,
380       ** so that user input characters can be echoed correctly.
381       **
382       ** ++++ Update +++++
383       ** With the advent of double buffering, we now see if
384       ** TxBufferOut-In is non-zero. If so, then we copy a packet
385       ** to the output place, and set it going. If this empties
386       ** the buffer, then we must issue a wakeup( ) on OUT.
387       ** If it frees space in the buffer then we must issue
388       ** a wakeup( ) on IN.
389       **
390       ** ++++ Extra! Extra! If PortP->WflushFlag is set, then we
391       ** have to send a WFLUSH command down the PHB, to mark the
392       ** end point of a WFLUSH. We also need to clear out any
393       ** data from the double buffer! ( note that WflushFlag is a
394       ** *count* of the number of WFLUSH commands outstanding! )
395       **
396       ** ++++ And there's more!
397       ** If an RTA is powered off, then on again, and rebooted,
398       ** whilst it has ports open, then we need to re-open the ports.
399       ** ( reasonable enough ). We can't do this when we spot the
400       ** re-boot, in interrupt time, because the queue is probably
401       ** full. So, when we come in here, we need to test if any
402       ** ports are in this condition, and re-open the port before
403       ** we try to send any more data to it. Now, the re-booted
404       ** RTA will be discarding packets from the PHB until it
405       ** receives this open packet, but don't worry tooo much
406       ** about that. The one thing that is interesting is the
407       ** combination of this effect and the WFLUSH effect!
408       */
409       /* For now don't handle RTA reboots. -- REW. 
410          Reenabled. Otherwise RTA reboots didn't work. Duh. -- REW */
411       if ( PortP->MagicFlags ) {
412 #if 1
413         if ( PortP->MagicFlags & MAGIC_REBOOT ) {
414           /*
415           ** well, the RTA has been rebooted, and there is room
416           ** on its queue to add the open packet that is required.
417           **
418           ** The messy part of this line is trying to decide if
419           ** we need to call the Param function as a tty or as
420           ** a modem.
421           ** DONT USE CLOCAL AS A TEST FOR THIS!
422           **
423           ** If we can't param the port, then move on to the
424           ** next port.
425           */
426           PortP->InUse = NOT_INUSE;
427
428           rio_spin_unlock(&PortP->portSem);
429           if ( RIOParam(PortP, OPEN, ((PortP->Cor2Copy & 
430                                        (COR2_RTSFLOW|COR2_CTSFLOW ) )== 
431                                       (COR2_RTSFLOW|COR2_CTSFLOW ) ) ? 
432                         TRUE : FALSE, DONT_SLEEP ) == RIO_FAIL ) {
433             continue; /* with next port */
434           }
435           rio_spin_lock(&PortP->portSem);
436           PortP->MagicFlags &= ~MAGIC_REBOOT;
437         }
438 #endif
439
440         /*
441         ** As mentioned above, this is a tacky hack to cope
442         ** with WFLUSH
443         */
444         if ( PortP->WflushFlag ) {
445           rio_dprintk (RIO_DEBUG_INTR, "Want to WFLUSH mark this port\n");
446
447           if ( PortP->InUse )
448             rio_dprintk (RIO_DEBUG_INTR, "FAILS - PORT IS IN USE\n");
449         }
450                                 
451         while ( PortP->WflushFlag &&
452                 can_add_transmit( &PacketP, PortP ) && 
453                 ( PortP->InUse == NOT_INUSE ) ) {
454           int p;
455           struct PktCmd *PktCmdP;
456
457           rio_dprintk (RIO_DEBUG_INTR, "Add WFLUSH marker to data queue\n");
458           /*
459           ** make it look just like a WFLUSH command
460           */
461           PktCmdP = ( struct PktCmd * )&PacketP->data[0];
462
463           WBYTE( PktCmdP->Command , WFLUSH );
464
465           p =  PortP->HostPort % ( ushort )PORTS_PER_RTA;
466
467           /*
468           ** If second block of ports for 16 port RTA, add 8
469           ** to index 8-15.
470           */
471           if ( PortP->SecondBlock )
472             p += PORTS_PER_RTA;
473
474           WBYTE( PktCmdP->PhbNum, p );
475
476           /*
477           ** to make debuggery easier
478           */
479           WBYTE( PacketP->data[ 2], 'W'  );
480           WBYTE( PacketP->data[ 3], 'F'  );
481           WBYTE( PacketP->data[ 4], 'L'  );
482           WBYTE( PacketP->data[ 5], 'U'  );
483           WBYTE( PacketP->data[ 6], 'S'  );
484           WBYTE( PacketP->data[ 7], 'H'  );
485           WBYTE( PacketP->data[ 8], ' '  );
486           WBYTE( PacketP->data[ 9], '0'+PortP->WflushFlag );
487           WBYTE( PacketP->data[10], ' '  );
488           WBYTE( PacketP->data[11], ' '  );
489           WBYTE( PacketP->data[12], '\0' );
490
491           /*
492           ** its two bytes long!
493           */
494           WBYTE( PacketP->len , PKT_CMD_BIT | 2 );
495
496           /*
497           ** queue it!
498           */
499           if ( !( PortP->State & RIO_DELETED ) ) {
500             add_transmit( PortP );
501             /*
502             ** Count chars tx'd for port statistics reporting
503             */
504             if ( PortP->statsGather )
505               PortP->txchars += 2;
506           }
507
508           if ( --( PortP->WflushFlag ) == 0 ) {
509             PortP->MagicFlags &= ~MAGIC_FLUSH;
510           }
511
512           rio_dprintk (RIO_DEBUG_INTR, "Wflush count now stands at %d\n", 
513                  PortP->WflushFlag);
514         }
515         if ( PortP->MagicFlags & MORE_OUTPUT_EYGOR ) {
516           if ( PortP->MagicFlags & MAGIC_FLUSH ) {
517             PortP->MagicFlags |= MORE_OUTPUT_EYGOR;
518           }
519           else {
520             if ( !can_add_transmit( &PacketP, PortP ) ) {
521               rio_spin_unlock(&PortP->portSem);
522               continue;
523             }
524             rio_spin_unlock(&PortP->portSem);
525             RIOTxEnable((char *)PortP);
526             rio_spin_lock(&PortP->portSem);
527             PortP->MagicFlags &= ~MORE_OUTPUT_EYGOR;
528           }
529         }
530       }
531
532
533       /*
534       ** If we can't add anything to the transmit queue, then
535       ** we need do none of the remaining processing.
536       */
537       if (!can_add_transmit( &PacketP, PortP ) ) {
538         rio_spin_unlock(&PortP->portSem);
539         continue;
540       }
541
542       rio_spin_unlock(&PortP->portSem);
543       RIOTxEnable((char *)PortP);
544     }
545   }
546 }
547
548 /*
549 ** Routine for handling received data for clist drivers.
550 ** NB: Called with the tty locked. The spl from the lockb( ) is passed.
551 ** we return the ttySpl level that we re-locked at.
552 */
553 static void
554 RIOReceive(p, PortP)
555 struct rio_info *       p;
556 struct Port *           PortP;
557 {
558   struct tty_struct *TtyP;
559   register ushort transCount;
560   struct PKT *PacketP;
561   register uint DataCnt;
562   uchar *       ptr;
563   int copied =0;
564
565   static int intCount, RxIntCnt;
566
567   /*
568   ** The receive data process is to remove packets from the
569   ** PHB until there aren't any more or the current cblock
570   ** is full. When this occurs, there will be some left over
571   ** data in the packet, that we must do something with.
572   ** As we haven't unhooked the packet from the read list
573   ** yet, we can just leave the packet there, having first
574   ** made a note of how far we got. This means that we need
575   ** a pointer per port saying where we start taking the
576   ** data from - this will normally be zero, but when we
577   ** run out of space it will be set to the offset of the
578   ** next byte to copy from the packet data area. The packet
579   ** length field is decremented by the number of bytes that
580   ** we succesfully removed from the packet. When this reaches
581   ** zero, we reset the offset pointer to be zero, and free
582   ** the packet from the front of the queue.
583   */
584
585   intCount++;
586
587   TtyP = PortP->gs.tty;
588   if (!TtyP) {
589     rio_dprintk (RIO_DEBUG_INTR, "RIOReceive: tty is null. \n");
590     return;
591   }
592
593   if (PortP->State & RIO_THROTTLE_RX) {
594     rio_dprintk (RIO_DEBUG_INTR, "RIOReceive: Throttled. Can't handle more input.\n");
595     return;
596   }
597
598   if ( PortP->State & RIO_DELETED )
599     {
600       while ( can_remove_receive( &PacketP, PortP ) )
601         {
602           remove_receive( PortP );
603           put_free_end( PortP->HostP, PacketP );
604         }
605     }
606   else
607     {
608       /*
609       ** loop, just so long as:
610       **   i ) there's some data ( i.e. can_remove_receive )
611       **  ii ) we haven't been blocked
612       ** iii ) there's somewhere to put the data
613       **  iv ) we haven't outstayed our welcome
614       */
615       transCount = 1;
616       while ( can_remove_receive(&PacketP, PortP)
617               && transCount)
618         {
619 #ifdef STATS
620           PortP->Stat.RxIntCnt++;
621 #endif /* STATS */
622           RxIntCnt++;
623
624           /*
625           ** check that it is not a command!
626           */
627           if ( PacketP->len & PKT_CMD_BIT ) {
628             rio_dprintk (RIO_DEBUG_INTR, "RIO: unexpected command packet received on PHB\n");
629             /*      rio_dprint(RIO_DEBUG_INTR, (" sysport   = %d\n", p->RIOPortp->PortNum)); */
630             rio_dprintk (RIO_DEBUG_INTR, " dest_unit = %d\n", PacketP->dest_unit);
631             rio_dprintk (RIO_DEBUG_INTR, " dest_port = %d\n", PacketP->dest_port);
632             rio_dprintk (RIO_DEBUG_INTR, " src_unit  = %d\n", PacketP->src_unit);
633             rio_dprintk (RIO_DEBUG_INTR, " src_port  = %d\n", PacketP->src_port);
634             rio_dprintk (RIO_DEBUG_INTR, " len     = %d\n", PacketP->len);
635             rio_dprintk (RIO_DEBUG_INTR, " control   = %d\n", PacketP->control);
636             rio_dprintk (RIO_DEBUG_INTR, " csum    = %d\n", PacketP->csum);
637             rio_dprintk (RIO_DEBUG_INTR, "       data bytes: ");
638             for ( DataCnt=0; DataCnt<PKT_MAX_DATA_LEN; DataCnt++ )
639               rio_dprintk (RIO_DEBUG_INTR, "%d\n", PacketP->data[DataCnt]);
640             remove_receive( PortP );
641             put_free_end( PortP->HostP, PacketP );
642             continue; /* with next packet */
643           }
644
645           /*
646           ** How many characters can we move 'upstream' ?
647           **
648           ** Determine the minimum of the amount of data
649           ** available and the amount of space in which to
650           ** put it.
651           **
652           ** 1. Get the packet length by masking 'len'
653           **    for only the length bits.
654           ** 2. Available space is [buffer size] - [space used]
655           **
656           ** Transfer count is the minimum of packet length
657           ** and available space.
658           */
659                         
660           transCount = min_t(unsigned int, PacketP->len & PKT_LEN_MASK,
661                            TTY_FLIPBUF_SIZE - TtyP->flip.count);
662           rio_dprintk (RIO_DEBUG_REC,  "port %d: Copy %d bytes\n", 
663                                       PortP->PortNum, transCount);
664           /*
665           ** To use the following 'kkprintfs' for debugging - change the '#undef'
666           ** to '#define', (this is the only place ___DEBUG_IT___ occurs in the
667           ** driver).
668           */
669 #undef ___DEBUG_IT___
670 #ifdef ___DEBUG_IT___
671           kkprintf("I:%d R:%d P:%d Q:%d C:%d F:%x ",
672                    intCount,
673                    RxIntCnt,
674                    PortP->PortNum,
675                    TtyP->rxqueue.count,
676                    transCount,
677                    TtyP->flags );
678 #endif
679           ptr = (uchar *) PacketP->data + PortP->RxDataStart;
680
681           rio_memcpy_fromio (TtyP->flip.char_buf_ptr, ptr, transCount);
682           memset(TtyP->flip.flag_buf_ptr, TTY_NORMAL, transCount);
683
684 #ifdef STATS
685           /*
686           ** keep a count for statistical purposes
687           */
688           PortP->Stat.RxCharCnt += transCount;
689 #endif
690           PortP->RxDataStart    += transCount;
691           PacketP->len          -= transCount;
692           copied += transCount;
693           TtyP->flip.count += transCount;
694           TtyP->flip.char_buf_ptr += transCount;
695           TtyP->flip.flag_buf_ptr += transCount;
696
697
698 #ifdef ___DEBUG_IT___
699           kkprintf("T:%d L:%d\n", DataCnt, PacketP->len );
700 #endif
701
702           if ( PacketP->len == 0 )
703             {
704                                 /*
705                                 ** If we have emptied the packet, then we can
706                                 ** free it, and reset the start pointer for
707                                 ** the next packet.
708                                 */
709               remove_receive( PortP );
710               put_free_end( PortP->HostP, PacketP );
711               PortP->RxDataStart = 0;
712 #ifdef STATS
713                                 /*
714                                 ** more lies ( oops, I mean statistics )
715                                 */
716               PortP->Stat.RxPktCnt++;
717 #endif /* STATS */
718             }
719         }
720     }
721   if (copied) {
722     rio_dprintk (RIO_DEBUG_REC, "port %d: pushing tty flip buffer: %d total bytes copied.\n", PortP->PortNum, copied);
723     tty_flip_buffer_push (TtyP);
724   }
725
726   return;
727 }
728
729 #ifdef FUTURE_RELEASE
730 /*
731 ** The proc routine called by the line discipline to do the work for it.
732 ** The proc routine works hand in hand with the interrupt routine.
733 */
734 int
735 riotproc(p, tp, cmd, port)
736 struct rio_info *       p;
737 register struct ttystatics *tp;
738 int cmd;
739 int     port;
740 {
741         register struct Port *PortP;
742         int SysPort;
743         struct PKT *PacketP;
744
745         SysPort = port; /* Believe me, it works. */
746
747         if ( SysPort < 0 || SysPort >= RIO_PORTS ) {
748                 rio_dprintk (RIO_DEBUG_INTR, "Illegal port %d derived from TTY in riotproc()\n",SysPort);
749                 return 0;
750         }
751         PortP = p->RIOPortp[SysPort];
752
753         if ((uint)PortP->PhbP < (uint)PortP->Caddr || 
754                         (uint)PortP->PhbP >= (uint)PortP->Caddr+SIXTY_FOUR_K ) {
755                 rio_dprintk (RIO_DEBUG_INTR, "RIO: NULL or BAD PhbP on sys port %d in proc routine\n",
756                                                         SysPort);
757                 rio_dprintk (RIO_DEBUG_INTR, "   PortP = 0x%x\n",PortP);
758                 rio_dprintk (RIO_DEBUG_INTR, "   PortP->PhbP = 0x%x\n",PortP->PhbP);
759                 rio_dprintk (RIO_DEBUG_INTR, "   PortP->Caddr = 0x%x\n",PortP->PhbP);
760                 rio_dprintk (RIO_DEBUG_INTR, "   PortP->HostPort = 0x%x\n",PortP->HostPort);
761                 return 0;
762         }
763
764         switch(cmd) {
765                 case T_WFLUSH:
766                         rio_dprintk (RIO_DEBUG_INTR, "T_WFLUSH\n");
767                         /*
768                         ** Because of the spooky way the RIO works, we don't need
769                         ** to issue a flush command on any of the SET*F commands,
770                         ** as that causes trouble with getty and login, which issue
771                         ** these commands to incur a READ flush, and rely on the fact
772                         ** that the line discipline does a wait for drain for them.
773                         ** As the rio doesn't wait for drain, the write flush would
774                         ** destroy the Password: prompt. This isn't very friendly, so
775                         ** here we only issue a WFLUSH command if we are in the interrupt
776                         ** routine, or we aren't executing a SET*F command.
777                         */
778                         if ( PortP->HostP->InIntr || !PortP->FlushCmdBodge ) {
779                                 /*
780                                 ** form a wflush packet - 1 byte long, no data
781                                 */
782                                 if ( PortP->State & RIO_DELETED ) {
783                                         rio_dprintk (RIO_DEBUG_INTR, "WFLUSH on deleted RTA\n");
784                                 }
785                                 else {
786                                         if ( RIOPreemptiveCmd(p, PortP, WFLUSH ) == RIO_FAIL ) {
787                                                 rio_dprintk (RIO_DEBUG_INTR, "T_WFLUSH Command failed\n");
788                                         }
789                                         else
790                                                 rio_dprintk (RIO_DEBUG_INTR, "T_WFLUSH Command\n");
791                                 }
792                                 /*
793                                 ** WFLUSH operation - flush the data!
794                                 */
795                                 PortP->TxBufferIn = PortP->TxBufferOut = 0;
796                         }
797                         else {
798                                 rio_dprintk (RIO_DEBUG_INTR, "T_WFLUSH Command ignored\n");
799                         }
800                         /*
801                         ** sort out the line discipline
802                         */
803                         if (PortP->CookMode == COOK_WELL)
804                                 goto start;
805                         break;
806         
807                 case T_RESUME:
808                         rio_dprintk (RIO_DEBUG_INTR, "T_RESUME\n");
809                         /*
810                         ** send pre-emptive resume packet
811                         */
812                         if ( PortP->State & RIO_DELETED ) {
813                                 rio_dprintk (RIO_DEBUG_INTR, "RESUME on deleted RTA\n");
814                         }
815                         else {
816                                 if ( RIOPreemptiveCmd(p, PortP, RESUME ) == RIO_FAIL ) {
817                                         rio_dprintk (RIO_DEBUG_INTR, "T_RESUME Command failed\n");
818                                 }
819                         }
820                         /*
821                         ** and re-start the sender software!
822                         */
823                         if (PortP->CookMode == COOK_WELL)
824                                 goto start;
825                         break;
826         
827                 case T_TIME:
828                         rio_dprintk (RIO_DEBUG_INTR, "T_TIME\n");
829                         /*
830                         ** T_TIME is called when xDLY is set in oflags and
831                         ** the line discipline timeout has expired. It's
832                         ** function in life is to clear the TIMEOUT flag
833                         ** and to re-start output to the port.
834                         */
835                         /*
836                         ** Fall through and re-start output
837                         */
838                 case T_OUTPUT:
839 start:
840                         if ( PortP->MagicFlags & MAGIC_FLUSH ) {
841                                 PortP->MagicFlags |= MORE_OUTPUT_EYGOR;
842                                 return 0;
843                         }
844                         RIOTxEnable((char *)PortP);
845                         PortP->MagicFlags &= ~MORE_OUTPUT_EYGOR;
846                         /*rio_dprint(RIO_DEBUG_INTR, PortP,DBG_PROC,"T_OUTPUT finished\n");*/
847                         break;
848         
849                 case T_SUSPEND:
850                         rio_dprintk (RIO_DEBUG_INTR, "T_SUSPEND\n");
851                         /*
852                         ** send a suspend pre-emptive packet.
853                         */
854                         if ( PortP->State & RIO_DELETED ) {
855                                 rio_dprintk (RIO_DEBUG_INTR, "SUSPEND deleted RTA\n");
856                         }
857                         else {
858                                 if ( RIOPreemptiveCmd(p, PortP, SUSPEND ) == RIO_FAIL ) {
859                                         rio_dprintk (RIO_DEBUG_INTR, "T_SUSPEND Command failed\n");
860                                 }
861                         }
862                         /*
863                         ** done!
864                         */
865                         break;
866         
867                 case T_BLOCK:
868                         rio_dprintk (RIO_DEBUG_INTR, "T_BLOCK\n");
869                         break;
870         
871                 case T_RFLUSH:
872                         rio_dprintk (RIO_DEBUG_INTR, "T_RFLUSH\n");
873                         if ( PortP->State & RIO_DELETED ) {
874                                 rio_dprintk (RIO_DEBUG_INTR, "RFLUSH on deleted RTA\n");
875                                 PortP->RxDataStart = 0;
876                         }
877                         else {
878                                 if ( RIOPreemptiveCmd( p, PortP, RFLUSH ) == RIO_FAIL ) {
879                                         rio_dprintk (RIO_DEBUG_INTR, "T_RFLUSH Command failed\n");
880                                         return 0;
881                                 }
882                                 PortP->RxDataStart = 0;
883                                 while ( can_remove_receive(&PacketP, PortP) ) {
884                                         remove_receive(PortP);
885                                         ShowPacket(DBG_PROC, PacketP );
886                                         put_free_end(PortP->HostP, PacketP );
887                                 }
888                                 if ( PortP->PhbP->handshake == PHB_HANDSHAKE_SET ) {
889                                         /*
890                                         ** MAGIC!
891                                         */
892                                         rio_dprintk (RIO_DEBUG_INTR, "Set receive handshake bit\n");
893                                         PortP->PhbP->handshake |= PHB_HANDSHAKE_RESET;
894                                 }
895                         }
896                         break;
897                         /* FALLTHROUGH */
898                 case T_UNBLOCK:
899                         rio_dprintk (RIO_DEBUG_INTR, "T_UNBLOCK\n");
900                         /*
901                         ** If there is any data to receive set a timeout to service it.
902                         */
903                         RIOReceive(p, PortP);
904                         break;
905         
906                 case T_BREAK:
907                         rio_dprintk (RIO_DEBUG_INTR, "T_BREAK\n");
908                         /*
909                         ** Send a break command. For Sys V
910                         ** this is a timed break, so we
911                         ** send a SBREAK[time] packet
912                         */
913                         /*
914                         ** Build a BREAK command
915                         */
916                         if ( PortP->State & RIO_DELETED ) {
917                                 rio_dprintk (RIO_DEBUG_INTR, "BREAK on deleted RTA\n");
918                         }
919                         else {
920                                 if (RIOShortCommand(PortP,SBREAK,2,
921                                                                 p->RIOConf.BreakInterval)==RIO_FAIL) {
922                                         rio_dprintk (RIO_DEBUG_INTR, "SBREAK RIOShortCommand failed\n");
923                                 }
924                         }
925         
926                         /*
927                         ** done!
928                         */
929                         break;
930         
931                 case T_INPUT:
932                         rio_dprintk (RIO_DEBUG_INTR, "Proc T_INPUT called - I don't know what to do!\n");
933                         break;
934                 case T_PARM:
935                         rio_dprintk (RIO_DEBUG_INTR, "Proc T_PARM called - I don't know what to do!\n");
936                         break;
937         
938                 case T_SWTCH:
939                         rio_dprintk (RIO_DEBUG_INTR, "Proc T_SWTCH called - I don't know what to do!\n");
940                         break;
941         
942                 default:
943                         rio_dprintk (RIO_DEBUG_INTR, "Proc UNKNOWN command %d\n",cmd);
944         }
945         /*
946         ** T_OUTPUT returns without passing through this point!
947         */
948         /*rio_dprint(RIO_DEBUG_INTR, PortP,DBG_PROC,"riotproc done\n");*/
949         return(0);
950 }
951 #endif