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

Revision 752, 9.7 kB (checked in by ppalmers, 15 years ago)

- Implement a DLL based mechanism to read the cycle timer. This can potentially be more lightweight for the reader threads since it avoids a the CTR read kernel call. It also has the
side effect that FFADO now works on older kernels that don't implement the cycle timer read call.

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