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