
/*
 *  DEFS.H
 *
 *  ---- NOTE, ONLY IOREQUEST STRUCTURE IS PUBLIC, ALL OTHER STRUCTURES
 *	 **WILL** CHANGE WITHOUT NOTICE ----
 */

#define PARNET_SRC

#include <proto/all.h>
#include <exec/types.h>
#include <devices/trackdisk.h>
#include <devices/timer.h>
#include <hardware/intbits.h>
#include <exec/errors.h>
#include <exec/interrupts.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define PORTNAME    "parnet.cfg"

#define MAXPKTSIZE  (8192+256)

#define IOF_QUEUED  0x02
#define IOF_RUN     0x04

#define STRM_RESERVE	0x8000	    /*	ports reserved for stream protocol */
#define STRM_PORTS	512	    /*	must be factor of 32		   */

typedef unsigned char	ubyte;
typedef unsigned short	uword;
typedef unsigned long	ulong;

typedef struct Library	LIB;
typedef struct MsgPort	PORT;
typedef struct List	LIST;
typedef struct Node	NODE;
typedef struct MinNode	MNODE;
typedef struct Task	Task;
typedef struct Message	Message;

typedef struct timerequest IOT;

typedef long (*func_ptr)();
typedef void (*func_void)();

typedef struct {
    LIB     Lib;	    /*	library node		*/
    ubyte   ParAddress;     /*	1-255			*/
    PORT    Port;	    /*	task port		*/
    long    PLock[2];	    /*	task lock		*/
    LIST    UnitList;	    /*	active units (ports)    */
} Device;

/*
 *  Unit Core Structure (not user accessable) (do not access!)
 */

typedef struct {
    NODE    Node;	    /*	link node   DO NOT MOVE */
    func_void BeginIO;	    /*	begin I/O   DO NOT MOVE */
    func_void AbortIO;	    /*	abort I/O   DO NOT MOVE */
    func_void CloseFunc;
    func_void DataFunc;     /*	low level packet data	*/
    uword   RefCnt;
    uword   Port;	    /*	core port ident 	*/
    uword   Protocol;	    /*	protocol id		*/
    ulong   DestAddr;	    /*	destination  0 = self	*/
    uword   Flags;	    /*	status flags		*/
    uword   State;	    /*	(protocol private)      */
    long    UnitLock[2];
    LIST    PendIOR;	    /*	pending read requests	    */
    LIST    PendIOW;	    /*	pending write requests	    */
    LIST    PendCon;	    /*	pending connect requests    */
    LIST    PendLsn;	    /*	pending listen requests     */
} Unit;

/*
 *  Io request structure and user defines
 */

#include "devices/parnet.h"

typedef struct IOParReq    Iob;

/*
 *  A low level network packet
 */

typedef struct {
    Message Msg;
    Iob     *iob;		    /*	IO req (unit_str)       */
    Unit    *io_Unit;		    /*	unit involved		*/
    ubyte   DestAddr;		    /*	Destination machine	*/
    ubyte   Reserved;
    uword   DestPort;		    /*	Destination port no.	*/
    ulong   DLen1;		    /*	Data Length		*/
    ulong   DLen2;		    /*	Data Length		*/
    ubyte   *Data1;		    /*	Data Pointer (lw algn)  */
    ubyte   *Data2;		    /*	Data Pointer (lw algn)  */
} Packet;

/*
 *  private public port to remember addr across closes.
 */

typedef struct {
    PORT    Port;
    long    Address;
    long    Timeout;
    char    DebugBuf[32];
} PubPort;

/*
 *  Globals
 */

extern long	ParLLTimeout;	    /*	par.asm */
extern Device	*DevBase;
extern PubPort	*StickyPort;

/* parnet.c */
extern Unit	*FindUnitForPort(uword);
extern Unit	*AllocUnit(Iob *, func_void, func_void, func_void,func_void);
void		FreeUnit(Unit *);
extern Packet	*AllocParPacket(Iob *, Unit *, ubyte *, long, ubyte *, long);
void		FreeParPacket(Packet *);
extern void	ReadConfig(void);
extern void	WriteConfig(void);

/* lock.asm */
extern void	LockAddr(long *);
extern void	UnlockAddr(long *);
extern int	TryLockAddr(long *);

/* par.asm */
extern void	ParAddress(int);			/*  par.asm	*/
extern void	Time10000(void);			/*  par.asm	*/

/* task.c */
extern void	ParNetTask(void);			/*  task.c	*/
void		QueuePacketForWrite(Packet *);

/* unit_ctl.c */
void		CtlBeginIO(Iob *);
