1/*
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8															   * "License").  You may not use this file except in compliance with the
9 * License.  Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23#ifndef _IOKIT_IOFIREWIREAVCLIBCONSUMER_H_
24#define _IOKIT_IOFIREWIREAVCLIBCONSUMER_H_
25
26#include <IOKit/avc/IOFireWireAVCLib.h>
27
28#include <pthread.h>	// for mutexes
29
30#include <IOKit/firewire/IOFireWireLib.h>
31
32class IOFireWireAVCLibConsumer
33{
34
35protected:
36
37    typedef struct _InterfaceMap
38    {
39        IUnknownVTbl *pseudoVTable;
40        IOFireWireAVCLibConsumer *obj;
41    } InterfaceMap;
42
43	//////////////////////////////////////
44	// cf plugin interfaces
45
46	static IOFireWireAVCLibConsumerInterface	sIOFireWireAVCLibConsumerInterface;
47	InterfaceMap								fIOFireWireAVCLibConsumerInterface;
48
49	//////////////////////////////////////
50    // CFArray callbacks
51
52    static CFArrayCallBacks sArrayCallbacks;
53
54	//////////////////////////////////////
55	// cf plugin ref counting
56
57	UInt32 		fRefCount;
58
59	//////////////////////////////////////
60
61    IOFireWireAVCLibUnitInterface **		fAVCUnit;
62    IOFireWireDeviceInterface **			fFWUnit;
63    CFRunLoopRef 							fCFRunLoop;
64    io_object_t								fService;
65	pthread_mutex_t 						fLock;
66
67    UInt32				fGeneration;
68    CFRunLoopSourceRef	fHeartbeatResponseSource;
69	IOFireWireAVCLibConsumer *	fHeartbeatResponseSourceInfo;
70	bool				fHeartbeatResponseScheduled;
71    CFRunLoopTimerRef	fConsumerHeartbeatTimer;
72    CFRunLoopTimerRef	fProducerHeartbeatTimer;
73    CFRunLoopTimerRef	fReconnectTimer;
74
75    UInt32 				fFlags;
76    UInt8				fSubunit;
77    UInt8				fLocalPlugNumber;
78
79    UInt8				fRemotePlugNumber;
80    FWAddress			fRemotePlugAddress;
81    UInt8				fRemotePlugOptions;
82
83    UInt32				fMaxPayloadLog;
84    bool				fSegmentBitState;
85    bool				fHeartbeatBitState;
86    UInt8				fMode;
87
88    UInt32				fInputPlugRegisterBuffer;
89    UInt32				fOutputPlugRegisterBuffer;
90
91    UInt32							fState;
92    void *							fStateHandlerRefcon;
93    IOFireWireAVCPortStateHandler 	fStateHandler;
94
95    UInt32 								fSegmentSize;
96    char *								fSegmentBuffer;
97    IOFireWireLibPseudoAddressSpaceRef 	fPlugAddressSpace;
98
99    CFRunLoopSourceRef					fFrameStatusSource;
100	IOFireWireAVCLibConsumer *			fFrameStatusSourceInfo;
101	bool								fFrameStatusSourceScheduled;
102    UInt32								fFrameStatusMode;
103    UInt32								fFrameStatusCount;
104    IOFireWireAVCFrameStatusHandler  	fFrameStatusHandler;
105    void *								fFrameStatusHandlerRefcon;
106
107    CFRunLoopSourceRef	fDisconnectResponseSource;
108	IOFireWireAVCLibConsumer *	fDisconnectResponseSourceInfo;
109	bool				fDisconnectResponseScheduled;
110
111    static inline UInt64 subtractFWAddressFromFWAddress( FWAddress address1, FWAddress address2 )
112    {
113        UInt64 addr1 = (((UInt64)address1.addressHi) << 32) | address1.addressLo;
114        UInt64 addr2 = (((UInt64)address2.addressHi) << 32) | address2.addressLo;
115        return (addr2 - addr1);
116    }
117
118	static void sendHeartbeatResponse( void * info );
119	static void sendDisconnectResponse( void * info );
120    static void sendFrameStatusNotification( void * info );
121
122    virtual void startConsumerHeartbeatTimer( void );
123    static void consumerHeartbeatProc( CFRunLoopTimerRef timer, void * data );
124	virtual void stopConsumerHeartbeatTimer( void );
125
126    virtual void startProducerHeartbeatTimer( void );
127    static void producerHeartbeatProc( CFRunLoopTimerRef timer, void * data );
128	virtual void stopProducerHeartbeatTimer( void );
129
130    virtual void startReconnectTimer( void );
131    static void reconnectTimeoutProc( CFRunLoopTimerRef timer, void * data );
132	virtual void stopReconnectTimer( void );
133
134    IOReturn getNodeIDsAndGeneration( UInt16 * local, UInt16 * remote, UInt32 * gen );
135	virtual void forciblyDisconnected( void );
136
137	virtual void releaseSegment( void );
138
139	virtual IOReturn doConnectToRemotePlug( void );
140
141	virtual IOReturn updateProducerRegister( UInt32 newVal, UInt32 generation );
142
143public:
144
145	IOFireWireAVCLibConsumer( void );
146	virtual ~IOFireWireAVCLibConsumer();
147
148    static IUnknownVTbl ** alloc( IOFireWireAVCLibUnitInterface ** avcUnit,
149                                  CFRunLoopRef cfRunLoop,
150                                  UInt8 plugNumber );
151
152    virtual void finalize( void );
153
154	// utility function to get "this" pointer from interface
155	static inline IOFireWireAVCLibConsumer * getThis( void * self )
156        { return (IOFireWireAVCLibConsumer *)((InterfaceMap *)self)->obj; }
157
158	//////////////////////////////////////
159	// IUnknown static methods
160
161	static HRESULT queryInterface( void * self, REFIID iid, void **ppv );
162	static UInt32 comAddRef( void * self );
163	static UInt32 comRelease( void * self );
164
165	//////////////////////////////////////
166	// CFArray static methods
167
168	// callbacks for CFArray
169	static CFArrayCallBacks * getCFArrayCallbacks( void )
170		{ return &sArrayCallbacks; }
171
172	static const void *	cfAddRef( CFAllocatorRef allocator, const void *value );
173	static void	cfRelease( CFAllocatorRef allocator, const void *value );
174
175    //////////////////////////////////////
176    // core addRef and release
177
178	static UInt32 addRef( IOFireWireAVCLibConsumer * me );
179    static UInt32 release( IOFireWireAVCLibConsumer * me );
180
181    //////////////////////////////////////
182
183    virtual IOReturn init( IOFireWireAVCLibUnitInterface ** avcUnit,
184                           CFRunLoopRef cfRunLoop,
185                           UInt8 plugNumber );
186
187    virtual void deviceInterestCallback( natural_t type, void * arg );
188    static void setPortStateHandler( void * self, void * refcon, IOFireWireAVCPortStateHandler handler );
189
190    static void setSubunit( void * self, UInt8 subunit );
191    static void setRemotePlug( void * self, UInt8 plugNumber );
192    static void setMaxPayloadSize( void * self, UInt32 size );
193
194    static IOReturn connectToRemotePlug( void * self );
195    static IOReturn disconnectFromRemotePlug( void * self );
196
197    static IOReturn setSegmentSize( void * self, UInt32 size );
198    static UInt32 getSegmentSize( void * self );
199    static char * getSegmentBuffer( void * self );
200
201    static void setFrameStatusHandler( void * self, void * refcon, IOFireWireAVCFrameStatusHandler handler );
202
203    static UInt32 packetWriteHandler( IOFireWireLibPseudoAddressSpaceRef addressSpace,
204                                      FWClientCommandID commandID, UInt32 packetLen,
205                                      void* packet, UInt16 srcNodeID,		// nodeID of sender
206                                      UInt32 destAddressHi, UInt32 destAddressLo,
207                                      void* refCon );
208
209    static UInt32 packetReadHandler( IOFireWireLibPseudoAddressSpaceRef	addressSpace,
210                                     FWClientCommandID commandID, UInt32 packetLen,
211                                     UInt32 packetOffset, UInt16 nodeID,	// nodeID of requester
212                                     UInt32 destAddressHi, UInt32 destAddressLo,
213                                     void* refcon);
214
215    static void skippedPacketHandler( IOFireWireLibPseudoAddressSpaceRef addressSpace,
216                                      FWClientCommandID commandID, UInt32 skippedPacketCount);
217
218    static void frameProcessed( void * self, UInt32 mode );
219
220    static void setPortFlags( void * self, UInt32 flags );
221    static void clearPortFlags( void * self, UInt32 flags );
222    static UInt32 getPortFlags( void * self );
223
224};
225
226#endif