/******************************************************************************
    fwxIEEE1212.h

    "Fireworks IEEE 1394 Driver for Windows CE"

    This file contains all IEEE definitions from IEEE spec. 1394-1995

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

#ifndef FWXIEEE1394_H
#define FWXIEEE1394_H

#ifdef __cplusplus
extern "C" {
#endif

typedef unsigned short  Doublet;  //  2 bytes, 16 bits
typedef unsigned long   Quadlet;  //  4 bytes, 32 bits


#define FW_MAX_BUS_NODES	0x3F
#define FW_MAX_ISO_CHANNELS	0x40

#define FW_SPEED_100Mbs		0
#define FW_SPEED_200Mbs		1
#define FW_SPEED_400Mbs		2
#define FW_SPEED_Reserved	3

#define FW_RPTDELAY_144ns		0
#define FW_RPTDELAY_Reserved1	1
#define FW_RPTDELAY_Reserved2	2
#define FW_RPTDELAY_Reserved3	3

#define FW_PWR_NO_USE			0
#define PW_PWR_SELF_15W			1
#define PW_PWR_SELF_30W			2
#define PW_PWR_SELF_45W			3
#define FW_PWR_BUS_0W			4
#define FW_PWR_BUS_2W			5
#define FW_PWR_BUS_5W			6
#define FW_PWR_BUS_9W			7

#define FW_CONNECT_NOT_PRESENT	0
#define FW_CONNECT_UNCONNECTED	1
#define FW_CONNECT_TO_PARENT	2
#define FW_CONNECT_TO_CHILD		3

#define FW_MAX_SELF_ID_PACKETS	4
#define FW_MAX_PORTS_PER_NODE	27

typedef struct SelfIdPacket_s 
	{
	unsigned int MoreDataFollows	: 1;		// expect another quadlet
	unsigned int InitiatedReset		: 1;		// set if this node started the reset
	unsigned int Port2Connection	: 2;		// port 2 connection info
	unsigned int Port1Connection	: 2;		// port 1 connection info
	unsigned int Port0Connection	: 2;		// port 0 connection info
	unsigned int PowerInfo			: 3;		// power consumption info
	unsigned int Contender			: 1;		// contends for bus/iso resource mgr
	unsigned int RepeaterDelay		: 2;		// worst case repeater delay
	unsigned int MaxSpeed			: 2;		// mas supported data rate
	unsigned int GapCount			: 6;		// current gap count
	unsigned int ActiveLinkLayer	: 1;		// 
	unsigned int AlwaysZero			: 1;		// Always binary zero
	unsigned int PhysicalId			: 6;		// Physical ID of sender
	unsigned int AlwaysOneZero		: 2;		// Always binary 10
	} SelfIdPacket_t;


// Interrupt flags
#define FW_IRQ_RxData			0x00000001
#define FW_IRQ_TxReady			0x00000002
#define FW_IRQ_PhyReset			0x00000004
#define FW_IRQ_CmdReset			0x00000008
#define FW_IRQ_IsoError			0x00000010
#define FW_IRQ_AsyncError		0x00000020
#define FW_IRQ_RcvBusy			0x00000040
#define FW_IRQ_HdrCrcError		0x00000080
#define FW_IRQ_TransCodeError	0x00000100
#define FW_IRQ_CycleLost		0x00000200
#define FW_IRQ_CycleArbFailed	0x00000400
#define FW_IRQ_IsoArbFailed		0x00000800
#define FW_IRQ_CycleStarted		0x00001000


// Transaction Codes
#define FW_TCODE_WRITE_QUADLET_REQUEST			0x0
#define FW_TCODE_WRITE_BLOCK_REQUEST			0x1
#define FW_TCODE_WRITE_RESPONSE					0x2
#define FW_TCODE_RESERVED_3						0x3
#define FW_TCODE_READ_QUADLET_REQUEST			0x4
#define FW_TCODE_READ_BLOCK_REQUEST				0x5
#define FW_TCODE_READ_QUADLET_RESPONSE			0x6
#define FW_TCODE_READ_BLOCK_RESPONSE			0x7
#define FW_TCODE_CYCLE_START					0x8
#define FW_TCODE_LOCK_REQUEST					0x9
#define FW_TCODE_ISO_DATA						0xA
#define FW_TCODE_LOCK_RESPONSE					0xB
#define FW_TCODE_RESERVED_C						0xC
#define FW_TCODE_RESERVED_D						0xD
#define FW_TCODE_TSB_PHY_PACKET					0xE		// not really 1394!
#define FW_TCODE_RESERVED_F						0xF

#define FW_NUM_TCODES							0x10

#define FW_ETCODE_MASK_SWAP						0x1
#define FW_ETCODE_COMPARE_SWAP					0x2
#define FW_ETCODE_FETCH_ADD						0x3
#define FW_ETCODE_LITTLE_ADD					0x4
#define FW_ETCODE_BOUNDED_ADD					0x5
#define FW_ETCODE_WRAP_ADD						0x6

#define FW_TCFLAG_IS_WRITE			0x00000001
#define FW_TCFLAG_IS_READ			0x00000002
#define FW_TCFLAG_IS_LOCK			0x00000004
#define FW_TCFLAG_IS_ISO			0x00000008
#define FW_TCFLAG_IS_REQUEST		0x00000010
#define FW_TCFLAG_IS_RESPONSE		0x00000020
#define FW_TCFLAG_IS_QUADLET		0x00000040
#define FW_TCFLAG_IS_BLOCK			0x00000080
#define FW_TCFLAG_IS_VALID			0x80000000

extern int ieee1394_tcode_flags[16];
#define FW_TCFLAGS(x)	(ieee1394_tcode_flags[(x)&0xF])

// Retry codes
#define FW_RETRY_1		0
#define FW_RETRY_X		1
#define FW_RETRY_A		2
#define FW_RETRY_B		3

#define CSR_BASE_HIGH		0x0000FFFF
#define CSR_BASE_LOW		0xF0000000
#define CSR_SIZE_BYTES		0x400
#define CSR_SIZE_QUADLETS	0x400 / sizeof(Quadlet)

// Index into array of Quadlets (for convenience)
#define CSR_STATE_CLEAR					0x000
#define CSR_STATE_SET					0x004
#define CSR_NODE_IDS					0x008
#define CSR_RESET_START					0x00C
#define CSR_INDIRECT_ADDRESS			0x010
#define CSR_INDIRECT_DATA				0x014
#define CSR_SPLIT_TIMEOUT_HI			0x018
#define CSR_SPLIT_TIMEOUT_LO			0x01C
#define CSR_ARGUMENT_HI					0x020
#define CSR_ARGUMENT_LO					0x024
#define CSR_TEST_START					0x028
#define CSR_TEST_STATUS					0x02C
#define CSR_INTERRUPT_TARGET			0x050
#define CSR_INTERRUPT_MASK				0x054
#define CSR_SB_CYCLE_TIME				0x200
#define CSR_SB_BUS_TIME					0x204
#define CSR_SB_POWER_FAIL_IMINENT		0x208
#define CSR_SB_POWER_SOURCE				0x20C
#define CSR_SB_BUSY_TIMEOUT				0x210
#define CSR_SB_BUS_MANAGER_ID			0x21C
#define CSR_SB_BANDWIDTH_AVAILABLE		0x220
#define CSR_SB_CHANNELS_AVAILABLE		0x224
#define CSR_SB_CHANNELS_AVAILABLE_LO	0x228
#define CSR_SB_MAINT_CONTROL			0x22C
#define CSR_SB_MAINT_UTILITY			0x230

#define CSR_SB_TOPOLOGY_MAP_BASE		0x800
#define CSR_SB_TOPOLOGY_MAP_SIZE		0x100

#define CSR_SB_SPEED_MAP_BASE			0x1000
#define CSR_SB_SPEED_MAP_SIZE			0x1000

typedef struct TopologyMap_s 
	{
	unsigned int crc			: 16;
	unsigned int length			: 16;
	unsigned int generation		: 32;
	unsigned int self_id_count	: 16;
	unsigned int node_count		: 16;
	SelfIdPacket_t self_id_packet[64];
	} TopologyMap_t;

typedef struct SpeedMap_s 
	{
	unsigned int crc			: 16;
	unsigned int length			: 16;
	unsigned int generation		: 32;
	Quadlet speed_code[1024];		// NOTE:  Byte reversed for each quadlet!
	} SpeedMap_t;

// Acknowledge codes (as per 6.2.5.2.2)
#define FW_ACK_0			0		// reserved
#define FW_ACK_COMPLETE		1		// all done (no future subactions)
#define FW_ACK_PENDING		2		// request accepted, response to follow later
#define FW_ACK_3			3		// reserved
#define FW_ACK_BUSY_X		4		// destination was busy
#define FW_ACK_BUSY_A		5		// destination was busy, use retry phase A
#define FW_ACK_BUSY_B		6		// destination was busy, use retry phase B
#define FW_ACK_7			7		// reserved
#define FW_ACK_8			8		// reserved
#define FW_ACK_9			9		// reserved
#define FW_ACK_10			10		// reserved
#define FW_ACK_11			11		// reserved
#define FW_ACK_12			12		// reserved
#define FW_ACK_DATA_ERROR	13		// reserved
#define FW_ACK_TYPE_ERROR	14		// reserved
#define FW_ACK_15			15		// reserved

// Response codes (as per 6.2.4.10)
#define FW_RESP_OK				0
#define FW_RESP_1				1		// reserved
#define FW_RESP_2				2		// reserved
#define FW_RESP_3				3		// reserved
#define FW_RESP_CONFLICT_ERROR	4
#define FW_RESP_DATA_ERROR		5
#define FW_RESP_TYPE_ERROR		6
#define FW_RESP_ADDRESS_ERROR	7
#define FW_RESP_8				8		// reserved
#define FW_RESP_9				9		// reserved
#define FW_RESP_10				10		// reserved
#define FW_RESP_11				11		// reserved
#define FW_RESP_12				12		// reserved
#define FW_RESP_13				13		// reserved
#define FW_RESP_14				14		// reserved
#define FW_RESP_15				15		// reserved
#define FW_RESP_ABORTED			16
#define FW_RESP_ACK_TIMEOUT		17
#define FW_RESP_TIMEOUT			18
#define FW_RESP_DRIVER_ERROR	19
#define FW_RESP_RESOURCE_ERROR	20
#define FW_RESP_RETRY_ERROR		21

#ifdef __cplusplus
}
#endif

#endif  //  FWXIEEE1394_H

