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