root/trunk/libffado/src/libieee1394/ieee1394service.h

Revision 1043, 10.5 kB (checked in by ppalmers, 16 years ago)

- fix octlet byteswap bug for non-debug builds
- make Ieee1394Service thread safe

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*
2  * Copyright (C) 2005-2008 by Daniel Wagner
3  * Copyright (C) 2005-2008 by Pieter Palmers
4  *
5  * This file is part of FFADO
6  * FFADO = Free Firewire (pro-)audio drivers for linux
7  *
8  * FFADO is based upon FreeBoB
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 2 of the License, or
13  * (at your option) version 3 of the License.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #ifndef FFADO_IEEE1394SERVICE_H
26 #define FFADO_IEEE1394SERVICE_H
27
28 #include "fbtypes.h"
29 #include "libutil/Functors.h"
30 #include "libutil/Mutex.h"
31
32 #include "debugmodule/debugmodule.h"
33
34 #include "IEC61883.h"
35
36 #include <libraw1394/raw1394.h>
37 #include <pthread.h>
38
39 #include <vector>
40 #include <string>
41
42 class ARMHandler;
43 class IsoHandlerManager;
44 class CycleTimerHelper;
45
46 namespace Util {
47     class Watchdog;
48 }
49
50 class Ieee1394Service : public IEC61883 {
51 public:
52     Ieee1394Service();
53     Ieee1394Service(bool rt, int prio);
54     ~Ieee1394Service();
55
56     bool initialize( int port );
57     bool setThreadParameters(bool rt, int priority);
58     Util::Watchdog *getWatchdog() {return m_pWatchdog;};
59
60    /**
61     * @brief get number of ports (firewire adapters) in this machine
62     *
63     * @return the number of ports
64     */
65     static int detectNbPorts();
66
67    /**
68     * @brief get port (adapter) id
69     *
70     * @return get port (adapter) id
71     */
72     int getPort()
73         { return m_port; }
74
75    /**
76     * @brief get port (adapter) name
77     *
78     * @return get port (adapter) name
79     */
80     std::string getPortName()
81         { return m_portName; };
82
83    /**
84     * @brief get number of nodes on the bus
85     *
86     * Since the root node always has
87     * the highest node ID, this number can be used to determine that ID (it's
88     * LOCAL_BUS|(count-1)).
89     *
90     * @return the number of nodes on the bus to which the port is connected.
91     * This value can change with every bus reset.
92     */
93     int getNodeCount();
94
95    /**
96     * @brief get the node id of the local node
97     *
98     * @note does not include the bus part (0xFFC0)
99     *
100     * @return the node id of the local node
101     * This value can change with every bus reset.
102     */
103     nodeid_t getLocalNodeId();
104
105     /**
106      * @brief get the most recent cycle timer value (in ticks)
107      *
108      * @note Uses the most appropriate method for getting the cycle timer
109      *       which is not necessarily a direct read (could be DLL)
110      */
111     uint32_t getCycleTimerTicks();
112
113     /**
114      * @brief get the most recent cycle timer value (in CTR format)
115      *
116      * @note Uses the most appropriate method for getting the cycle timer
117      *       which is not necessarily a direct read (could be DLL)
118      */
119     uint32_t getCycleTimer();
120
121     /**
122      * @brief get the cycle timer value for a specific time instant (in ticks)
123      *
124      * @note Uses the most appropriate method for getting the cycle timer
125      *       which is not necessarily a direct read (could be DLL)
126      */
127     uint32_t getCycleTimerTicks(uint64_t t);
128
129     /**
130      * @brief get the cycle timer value for a specific time instant (in CTR format)
131      *
132      * @note Uses the most appropriate method for getting the cycle timer
133      *       which is not necessarily a direct read (could be DLL)
134      */
135     uint32_t getCycleTimer(uint64_t t);
136
137     /**
138      * @brief read the cycle timer value from the controller (in CTR format)
139      *
140      * @note Uses a direct method to read the value from the controller
141      * @return true if successful
142      */
143     bool readCycleTimerReg(uint32_t *cycle_timer, uint64_t *local_time);
144
145     /**
146      * @brief provide the current system time
147      * @return
148      */
149     uint64_t getCurrentTimeAsUsecs();
150
151     /**
152      * @brief send async read request to a node and wait for response.
153      *
154      * This does the complete transaction and will return when it's finished.
155      *
156      * @param node target node (\todo needs 0xffc0 stuff)
157      * @param addr address to read from
158      * @param length amount of data to read in quadlets
159      * @param buffer pointer to buffer where data will be saved
160      *
161      * @return true on success or false on failure (sets errno)
162      */
163     bool read( fb_nodeid_t nodeId,
164            fb_nodeaddr_t addr,
165            size_t length,
166            fb_quadlet_t* buffer );
167
168     bool read_quadlet( fb_nodeid_t nodeId,
169                        fb_nodeaddr_t addr,
170                        fb_quadlet_t* buffer );
171
172     bool read_octlet( fb_nodeid_t nodeId,
173                       fb_nodeaddr_t addr,
174                       fb_octlet_t* buffer );
175
176     /**
177     * @brief send async write request to a node and wait for response.
178     *
179     * This does the complete transaction and will return when it's finished.
180     *
181     * @param node target node (\XXX needs 0xffc0 stuff)
182     * @param addr address to write to
183     * @param length amount of data to write in quadlets
184     * @param data pointer to data to be sent
185     *
186     * @return true on success or false on failure (sets errno)
187     */
188     bool write( fb_nodeid_t nodeId,
189         fb_nodeaddr_t addr,
190         size_t length,
191         fb_quadlet_t* data );
192
193     bool write_quadlet( fb_nodeid_t nodeId,
194                         fb_nodeaddr_t addr,
195                         fb_quadlet_t data );
196
197     bool write_octlet(  fb_nodeid_t nodeId,
198                         fb_nodeaddr_t addr,
199                         fb_octlet_t data );
200
201     /**
202      * @brief send 64-bit compare-swap lock request and wait for response.
203      *
204      * swaps the content of \ref addr with \ref swap_value , but only if
205      * the content of \ref addr equals \ref compare_with
206      *
207      * @note takes care of endiannes
208      *
209      * @param nodeId target node ID
210      * @param addr address within target node address space
211      * @param compare_with value to compare \ref addr with
212      * @param swap_value new value to put in \ref addr
213      * @param result the value (originally) in \ref addr
214      *
215      * @return true if succesful, false otherwise
216      */
217     bool lockCompareSwap64(  fb_nodeid_t nodeId,
218                         fb_nodeaddr_t addr,
219                         fb_octlet_t  compare_value,
220                         fb_octlet_t  swap_value,
221                         fb_octlet_t* result );
222
223     fb_quadlet_t* transactionBlock( fb_nodeid_t nodeId,
224                                     fb_quadlet_t* buf,
225                                     int len,
226                     unsigned int* resp_len );
227
228     bool transactionBlockClose();
229 // FIXME: private for thread safety !!
230     raw1394handle_t getHandle() {return m_handle;};
231
232     int getVerboseLevel();
233
234     bool addBusResetHandler( Util::Functor* functor );
235     bool remBusResetHandler( Util::Functor* functor );
236
237     /**
238      * @brief get the current generation
239      *
240      * @return the current generation
241      **/
242     unsigned int getGeneration() {
243         Util::MutexLockHelper lock(*m_handle_lock);
244         return raw1394_get_generation( m_handle );
245     }
246
247     /**
248      * @brief register an AddressRangeMapping Handler
249      * @param h pointer to the handler to register
250      *
251      * @return true on success or false on failure
252      **/
253
254     bool registerARMHandler( ARMHandler *h );
255
256     /**
257      * @brief unregister ARM range
258      * @param h pointer to the handler to unregister
259      * @return true if successful, false otherwise
260      */
261     bool unregisterARMHandler( ARMHandler *h );
262
263     nodeaddr_t findFreeARMBlock( nodeaddr_t start, size_t length, size_t step );
264
265 // ISO channel stuff
266 public:
267     signed int getAvailableBandwidth();
268     signed int allocateIsoChannelGeneric(unsigned int bandwidth);
269     signed int allocateIsoChannelCMP(nodeid_t xmit_node, int xmit_plug,
270                                      nodeid_t recv_node, int recv_plug);
271     bool freeIsoChannel(signed int channel);
272
273     IsoHandlerManager& getIsoHandlerManager() {return *m_pIsoManager;};
274 private:
275     enum EAllocType {
276         AllocFree = 0, // not allocated (by us)
277         AllocGeneric = 1, // allocated with generic functions
278         AllocCMP=2 // allocated with CMP
279     };
280
281     struct ChannelInfo {
282         int channel;
283         int bandwidth;
284         enum EAllocType alloctype;
285         nodeid_t xmit_node;
286         int xmit_plug;
287         nodeid_t recv_node;
288         int recv_plug;
289     };
290
291     // the info for the channels we manage
292     struct ChannelInfo m_channels[64];
293
294     bool unregisterIsoChannel(unsigned int c);
295     bool registerIsoChannel(unsigned int c, struct ChannelInfo cinfo);
296
297 private:
298
299     bool startRHThread();
300     void stopRHThread();
301     static void* rHThread( void* arg );
302
303     void printBuffer( unsigned int level, size_t length, fb_quadlet_t* buffer ) const;
304     void printBufferBytes( unsigned int level, size_t length, byte_t* buffer ) const;
305
306     static int resetHandlerLowLevel( raw1394handle_t handle,
307                     unsigned int generation );
308     bool resetHandler( unsigned int generation );
309
310     static int armHandlerLowLevel(raw1394handle_t handle, unsigned long arm_tag,
311                      byte_t request_type, unsigned int requested_length,
312                      void *data);
313     bool armHandler(  unsigned long arm_tag,
314                      byte_t request_type, unsigned int requested_length,
315                      void *data);
316
317     raw1394handle_t m_handle;
318     Util::Mutex*    m_handle_lock;
319     raw1394handle_t m_resetHandle;
320     raw1394handle_t m_util_handle; // a handle for operations from the rt thread
321     int             m_port;
322     std::string     m_portName;
323
324     pthread_t       m_thread;
325     pthread_mutex_t m_mutex;
326     bool            m_threadRunning;
327
328     bool            m_realtime;
329     int             m_base_priority;
330
331     IsoHandlerManager*      m_pIsoManager;
332     CycleTimerHelper*       m_pCTRHelper;
333     bool                    m_have_new_ctr_read;
334
335     // the RT watchdog
336     Util::Watchdog*     m_pWatchdog;
337
338     typedef std::vector< Util::Functor* > reset_handler_vec_t;
339     reset_handler_vec_t m_busResetHandlers;
340
341     // ARM stuff
342     arm_tag_handler_t m_default_arm_handler;
343
344     typedef std::vector< ARMHandler * > arm_handler_vec_t;
345     arm_handler_vec_t m_armHandlers;
346
347     fb_octlet_t byteSwap_octlet(fb_octlet_t value);
348
349 public:
350     void setVerboseLevel(int l);
351     void show();
352 private:
353     DECLARE_DEBUG_MODULE;
354 };
355
356 #endif // FFADO_IEEE1394SERVICE_H
Note: See TracBrowser for help on using the browser.