/******************************************************************************
    fwx1394.h

    "IEEE 1394-1995 Bus Driver" by 3A International, Inc.

    Copyright (c) 1997-1998 by 3A International, Inc.  All rights Reserved.
******************************************************************************/

#ifndef FWX1394_H
#define FWX1394_H
#include <windows.h>
#include "fwxIEEE1394.h"
#include <stdio.h>

#define EEPROM_CONFIG_ROM_OFFSET  16
#define MAX_CONFIG_ROM  (256 - EEPROM_CONFIG_ROM_OFFSET)

typedef HANDLE          fwxHandle_t;

typedef struct fwxGUID_t
{
    ULONG  GUIDHi;
    ULONG  GUIDLo;
} fwxGUID_t;

#define MAX_NODES  0x3f  //  63

typedef struct fwxDeviceList_t
{
    int             NumNodes;
    fwxGUID_t       DeviceList[MAX_NODES];
} fwxDeviceList_t;

typedef struct fwxAdapterInfo_t
{
    fwxGUID_t       GUID;
} fwxAdapterInfo_t;

typedef struct fwxBusDriverProperties_t
{
    ULONG           ulSelfIdTimeout;
    ULONG           ulTransmitTimeout;
    ULONG           ulBusEnumerationDelay;
    fwxHandle_t     hBusResetEvent;     //  Read only
} fwxBusDriverProperties_t;

typedef struct fwxDeviceProperties_t
{
    ULONG           ulResponseTimeout;
    fwxHandle_t     hDeviceRemovedEvent;   //  Read only
} fwxDeviceProperties_t;

typedef struct fwx1394Offset_t
{
    Quadlet            Lo;
    Doublet            Hi;
} fwx1394Offset_t;

typedef struct fwxAsyncRead_t
{
    fwxHandle_t        hDevice;
    fwx1394Offset_t    DestinationAddress;     //  48 bit address
    ULONG              nNumberOfBytes;         //  From the remote node
    ULONG              nBlockSize;             //  If zero, use max packet size
    ULONG              fulFlags;
} fwxAsyncRead_t;

#define MAX_LOCK  16

typedef struct fwxAsyncLock_t
{
    fwxHandle_t        hDevice;
    fwx1394Offset_t    DestinationAddress;     //  48 bit address
    WORD               wExtendedTCode;
    ULONG              nNumberOfBytes;         //  From the remote node
    ULONG              nBlockSize;             //  If zero, use max packet size
    ULONG              fulFlags;
    BYTE               Buffer[MAX_LOCK];
} fwxAsyncLock_t;

#define MAX_WRITE_BLOCK  512

typedef struct fwxAsyncWrite_t
{
    fwxHandle_t        hDevice;
    fwx1394Offset_t    DestinationAddress;     //  48 bit address
    ULONG              nNumberOfBytes;         /*  From the remote node */
    ULONG              nBlockSize;             /*  If zero, use max packet size */
    ULONG              fulFlags;
    BYTE               Buffer[MAX_WRITE_BLOCK];
} fwxAsyncWrite_t;


#define  FWXAR_NOTIFY_READ    1
#define  FWXAR_NOTIFY_WRITE	  2
#define  FWXAR_NOTIFY_LOCK	  3

typedef struct fwxAsyncRcv_t
{
	ULONG  PktSpeed; 
	ULONG  PktSize;		// receiving size for the whole packet
	PVOID  pBuffer;     // point to the receiving packet
						/// application need to allocate memory for this buffer
						///  since the temp buffer on the driver
						///  will be overwritten by the following incoming buffer
} fwxAsyncRcv_t;


typedef struct fwxAllocateIsoResources_t
{
    WORD                wNumberOfBytes;
    WORD                wNumberOfPackets;
    WORD                wNumberOfFrames;
    LPVOID *            pFrameBase;
    PHANDLE             phResources;
	PHANDLE				phFrameEvent; 
} fwxAllocateIsoResources_t;

typedef struct fwxFreeIsoResources_t
{
    HANDLE              hResources;
} fwxFreeIsoResources_t;

typedef struct fwxAllocateBandwidth_t
{
    int  Bandwidth;
} fwxAllocateBandwidth_t;

typedef struct fwxFreeBandwidth_t
{
    int  Dummy;
} fwxFreeBandwidth_t;

typedef struct fwxAllocateChannel_t
{
    WORD                wChannel;
    WORD                wChannelAvailable;
} fwxAllocateChannel_t;

typedef struct fwxFreeChannel_t
{
    WORD                wChannel;
} fwxFreeChannel_t;

typedef struct fwxIsoReceive_t
{
    WORD                wChannel;
    HANDLE              hResources;
} fwxIsoReceive_t;

typedef struct fwxIsoTransmit_t
{
    WORD                wChannel;
    HANDLE              hResources;
} fwxIsoTransmit_t;

#ifdef _PROFILE
typedef struct fwxGetIsoStatistics_t
{
    HANDLE              hResources;
    ULONG               *pulFramesElapsed;
} fwxGetIsoStatistics_t;
#endif

typedef struct fwxIsoStop_t
{
    WORD                wChannel;
    HANDLE              hResources;
} fwxIsoStop_t;

typedef struct fwxReadConfigROM_t
{
    ULONG   Offset;
    size_t  Bytes;
} fwxReadConfigROM_t;

typedef struct fwxSaveConfigROM_t
{
    ULONG   Offset;
    size_t  Bytes;
    BYTE    Buffer[MAX_CONFIG_ROM];
} fwxSaveConfigROM_t;

//-------------------------------------------------------------------------------
// Shared memory types
//-------------------------------------------------------------------------------

// Contains all info needed to control shared memory regions. 
struct fwxSharedMemory_t
{
	USHORT      Size;               // Size of this shared memory region
	LPVOID      MemPtr;             // Ptr to this shared memory region (mapped)
	fwxHandle_t Handle;             // Handle of this shared memory region
};

//-------------------------------------------------------------------------------
// 1394 mapped memory types
//-------------------------------------------------------------------------------

// Memory access callback function type definition
typedef void (fwxAccessCallback)(IN fwxHandle_t   hHandle,
                                 IN ULONG         ulOffset,
                                 IN unsigned int  uiLength);

typedef struct fwxAllocateAddressRange_t
{
	fwx1394Offset_t		Address;				// 1394 Address to map
	USHORT				nLength;				// # bytes to map
	ULONG				fulAccessType;			// read, write or lock
	ULONG				fulNotificationOptions;	// none, on read, on write, on lock
	DWORD				OwnerThreadID;			// thread to post access notification to
	fwxAccessCallback  *CallBack;				// Ptr to callback function.
	fwxHandle_t         Handle;					// Handle of this resource
	LPVOID              pBuffer;				// pointer to buffer to map
} fwxAllocateAddressRange_t;

//typedef struct fwxFreeAddressRange_t
//{
//   fwx1394Offset_t    Address;
//} fwxFreeAddressRange_t;

// fwxAllocateAddressRange_t.fulAccessType constants
#define ACCESS_TYPE_READ    0x1
#define ACCESS_TYPE_WRITE   0x2
#define ACCESS_TYPE_LOCK    0x4

// fwxAllocateAddressRange_t.fulNotificationOption constants
#define NOTIFY_NONE         0x0
#define NOTIFY_AFTER_READ   0x1
#define NOTIFY_AFTER_WRITE  0x2
#define NOTIFY_AFTER_LOCK   0x4

// Callback message strings (for registration)
#define FWX_RWS_READ_ACCESS_STR		TEXT("WM_FWX_READ_ACCESS")
#define FWX_RWS_WRITE_ACCESS_STR	TEXT("WM_FWX_WRITE_ACCESS")
#define FWX_RWS_LOCK_ACCESS_STR		TEXT("WM_FWX_LOCK_ACCESS")

/*****************************************************************************/
//#define fwxGetIsoFrameCompleteEventName(pNameBuf, hResources, wFrameId) \
//    swprintf((wchar_t *)pNameBuf, (wchar_t *)TEXT("FWX_FRAMECOMPLETE_R%08X_F%02d"),  \
//    (HANDLE) hResources, wFrameId)
/*****************************************************************************/

#define fwxGetIsoFrameCompleteEventName(pNameBuf, hResources, wFrameId) \
  sprintf((char *)pNameBuf, "FWX_FRAMECOMPLETE_R%d%d", (HANDLE) hResources, wFrameId)

#endif  //  FWX1394_H 

