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

Revision 940, 10.4 kB (checked in by ppalmers, 13 years ago)

use RT watchdog for 1394 service threads

  • 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
31 #include "debugmodule/debugmodule.h"
32
33 #include "IEC61883.h"
34
35 #include <libraw1394/raw1394.h>
36 #include <pthread.h>
37
38 #include <vector>
39 #include <string>
40
41 class ARMHandler;
42 class IsoHandlerManager;
43 class CycleTimerHelper;
44
45 namespace Util {
46     class TimeSource;
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
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         return raw1394_get_generation( m_handle );
244     }
245
246     /**
247      * @brief register an AddressRangeMapping Handler
248      * @param h pointer to the handler to register
249      *
250      * @return true on success or false on failure
251      **/
252
253     bool registerARMHandler( ARMHandler *h );
254
255     /**
256      * @brief unregister ARM range
257      * @param h pointer to the handler to unregister
258      * @return true if successful, false otherwise
259      */
260     bool unregisterARMHandler( ARMHandler *h );
261
262     nodeaddr_t findFreeARMBlock( nodeaddr_t start, size_t length, size_t step );
263
264 // ISO channel stuff
265 public:
266     signed int getAvailableBandwidth();
267     signed int allocateIsoChannelGeneric(unsigned int bandwidth);
268     signed int allocateIsoChannelCMP(nodeid_t xmit_node, int xmit_plug,
269                                      nodeid_t recv_node, int recv_plug);
270     bool freeIsoChannel(signed int channel);
271
272     IsoHandlerManager& getIsoHandlerManager() {return *m_pIsoManager;};
273 private:
274     enum EAllocType {
275         AllocFree = 0, // not allocated (by us)
276         AllocGeneric = 1, // allocated with generic functions
277         AllocCMP=2 // allocated with CMP
278     };
279
280     struct ChannelInfo {
281         int channel;
282         int bandwidth;
283         enum EAllocType alloctype;
284         nodeid_t xmit_node;
285         int xmit_plug;
286         nodeid_t recv_node;
287         int recv_plug;
288     };
289
290     // the info for the channels we manage
291     struct ChannelInfo m_channels[64];
292
293     bool unregisterIsoChannel(unsigned int c);
294     bool registerIsoChannel(unsigned int c, struct ChannelInfo cinfo);
295
296 private:
297
298     bool startRHThread();
299     void stopRHThread();
300     static void* rHThread( void* arg );
301
302     void printBuffer( unsigned int level, size_t length, fb_quadlet_t* buffer ) const;
303     void printBufferBytes( unsigned int level, size_t length, byte_t* buffer ) const;
304
305     static int resetHandlerLowLevel( raw1394handle_t handle,
306                     unsigned int generation );
307     bool resetHandler( unsigned int generation );
308
309     static int armHandlerLowLevel(raw1394handle_t handle, unsigned long arm_tag,
310                      byte_t request_type, unsigned int requested_length,
311                      void *data);
312     bool armHandler(  unsigned long arm_tag,
313                      byte_t request_type, unsigned int requested_length,
314                      void *data);
315
316     raw1394handle_t m_handle;
317     raw1394handle_t m_resetHandle;
318     raw1394handle_t m_util_handle; // a handle for operations from the rt thread
319     int             m_port;
320     std::string     m_portName;
321
322     pthread_t       m_thread;
323     pthread_mutex_t m_mutex;
324     bool            m_threadRunning;
325
326     bool            m_realtime;
327     int             m_base_priority;
328
329     IsoHandlerManager*      m_pIsoManager;
330     CycleTimerHelper*       m_pCTRHelper;
331     bool                    m_have_new_ctr_read;
332
333     // the time source
334     Util::TimeSource*   m_pTimeSource;
335
336     // the RT watchdog
337     Util::Watchdog*     m_pWatchdog;
338
339     typedef std::vector< Util::Functor* > reset_handler_vec_t;
340     reset_handler_vec_t m_busResetHandlers;
341
342     // ARM stuff
343     arm_tag_handler_t m_default_arm_handler;
344
345     typedef std::vector< ARMHandler * > arm_handler_vec_t;
346     arm_handler_vec_t m_armHandlers;
347
348     fb_octlet_t byteSwap_octlet(fb_octlet_t value);
349
350 public:
351     void setVerboseLevel(int l);
352     void show();
353 private:
354     DECLARE_DEBUG_MODULE;
355 };
356
357 #endif // FFADO_IEEE1394SERVICE_H
Note: See TracBrowser for help on using the browser.