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

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

add busreset functionality

  • 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     void doBusReset();
238
239     /**
240      * @brief get the current generation
241      *
242      * @return the current generation
243      **/
244     unsigned int getGeneration() {
245         Util::MutexLockHelper lock(*m_handle_lock);
246         return raw1394_get_generation( m_handle );
247     }
248
249     /**
250      * @brief register an AddressRangeMapping Handler
251      * @param h pointer to the handler to register
252      *
253      * @return true on success or false on failure
254      **/
255
256     bool registerARMHandler( ARMHandler *h );
257
258     /**
259      * @brief unregister ARM range
260      * @param h pointer to the handler to unregister
261      * @return true if successful, false otherwise
262      */
263     bool unregisterARMHandler( ARMHandler *h );
264
265     nodeaddr_t findFreeARMBlock( nodeaddr_t start, size_t length, size_t step );
266
267 // ISO channel stuff
268 public:
269     signed int getAvailableBandwidth();
270     signed int allocateIsoChannelGeneric(unsigned int bandwidth);
271     signed int allocateIsoChannelCMP(nodeid_t xmit_node, int xmit_plug,
272                                      nodeid_t recv_node, int recv_plug);
273     bool freeIsoChannel(signed int channel);
274
275     IsoHandlerManager& getIsoHandlerManager() {return *m_pIsoManager;};
276 private:
277     enum EAllocType {
278         AllocFree = 0, // not allocated (by us)
279         AllocGeneric = 1, // allocated with generic functions
280         AllocCMP=2 // allocated with CMP
281     };
282
283     struct ChannelInfo {
284         int channel;
285         int bandwidth;
286         enum EAllocType alloctype;
287         nodeid_t xmit_node;
288         int xmit_plug;
289         nodeid_t recv_node;
290         int recv_plug;
291     };
292
293     // the info for the channels we manage
294     struct ChannelInfo m_channels[64];
295
296     bool unregisterIsoChannel(unsigned int c);
297     bool registerIsoChannel(unsigned int c, struct ChannelInfo cinfo);
298
299 private:
300
301     bool startRHThread();
302     void stopRHThread();
303     static void* rHThread( void* arg );
304
305     void printBuffer( unsigned int level, size_t length, fb_quadlet_t* buffer ) const;
306     void printBufferBytes( unsigned int level, size_t length, byte_t* buffer ) const;
307
308     static int resetHandlerLowLevel( raw1394handle_t handle,
309                     unsigned int generation );
310     bool resetHandler( unsigned int generation );
311
312     static int armHandlerLowLevel(raw1394handle_t handle, unsigned long arm_tag,
313                      byte_t request_type, unsigned int requested_length,
314                      void *data);
315     bool armHandler( unsigned long arm_tag,
316                      byte_t request_type, unsigned int requested_length,
317                      void *data);
318
319     raw1394handle_t m_handle;
320     Util::Mutex*    m_handle_lock;
321     raw1394handle_t m_resetHandle;
322     raw1394handle_t m_util_handle; // a handle for operations from the rt thread
323     int             m_port;
324     std::string     m_portName;
325
326     pthread_t       m_thread;
327     Util::Mutex*    m_RHThread_lock;
328     bool            m_threadRunning;
329
330     bool            m_realtime;
331     int             m_base_priority;
332
333     IsoHandlerManager*      m_pIsoManager;
334     CycleTimerHelper*       m_pCTRHelper;
335     bool                    m_have_new_ctr_read;
336
337     // the RT watchdog
338     Util::Watchdog*     m_pWatchdog;
339
340     typedef std::vector< Util::Functor* > reset_handler_vec_t;
341     reset_handler_vec_t m_busResetHandlers;
342
343     // ARM stuff
344     arm_tag_handler_t m_default_arm_handler;
345
346     typedef std::vector< ARMHandler * > arm_handler_vec_t;
347     arm_handler_vec_t m_armHandlers;
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.