root/tags/2.0-beta3/src/ffadodevice.h

Revision 1066, 14.3 kB (checked in by ppalmers, 4 years ago)

the quatafire doesn't support setting a clock source, altough discovery gives some.

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 FFADODEVICE_H
26 #define FFADODEVICE_H
27
28 #include "libutil/OptionContainer.h"
29 #include "libutil/PosixMutex.h"
30
31 #include "libcontrol/BasicElements.h"
32
33 #include "libieee1394/vendor_model_ids.h"
34
35 #include <vector>
36 #include <string>
37
38 class DeviceManager;
39 class ConfigRom;
40 class Ieee1394Service;
41
42 namespace Streaming {
43     class StreamProcessor;
44     class StreamProcessorManager;
45 }
46
47 namespace Control {
48     class Container;
49 }
50
51 /*!
52 @brief Base class for device support
53
54  This class should be subclassed to implement ffado support
55  for a specific device.
56
57 */
58 class FFADODevice
59     : public Util::OptionContainer,
60       public Control::Container
61 {
62 public:
63     FFADODevice( DeviceManager&, std::auto_ptr< ConfigRom >( configRom ) );
64
65     virtual ~FFADODevice();
66
67     /**
68      * @brief Compares the GUID of two FFADODevices
69      *
70      * This function compares the GUID of two FFADODevices and returns true
71      * if the GUID of @ref a is larger than the GUID of @ref b . This is intended
72      * to be used with the STL sort() algorithm.
73      *
74      * Note that GUID's are converted to integers for this.
75      *
76      * @param a pointer to first FFADODevice
77      * @param b pointer to second FFADODevice
78      *
79      * @returns true if the GUID of @ref a is larger than the GUID of @ref b .
80      */
81     static bool compareGUID( FFADODevice *a, FFADODevice *b );
82
83     /// Returns the 1394 service of the FFADO device
84     virtual Ieee1394Service& get1394Service();
85     /// Returns the ConfigRom object of the device node.
86     virtual ConfigRom& getConfigRom() const;
87
88     /**
89      * @brief Called by DeviceManager to load device model from cache.
90      *
91      * This function is called before discover in order to speed up
92      * system initializing.
93      *
94      * @returns true if device was cached and successfully loaded from cache
95      */
96     virtual bool loadFromCache();
97
98     /**
99      * @brief Called by DeviceManager to allow device driver to save a cache version
100      * of the current configuration.
101      *
102      * @returns true if caching was successful. False doesn't mean an error just,
103      * the driver was unable to store the configuration
104      */
105     virtual bool saveCache();
106
107     /**
108      * @brief This is called by the DeviceManager to create an instance of the device
109      *
110      * This function enables the FFADODevice to return a subclass of itself should that
111      * be needed. If we don't do this we'd need to know about the subclasses in the
112      * devicemanager, whilst now we don't.
113      *
114      * The function should return an instance of either the class itself or a subclass
115      * of itself.
116      *
117      * This should be overridden in any subclass.
118      *
119      * @return a new instance of the AvDevice type, NULL when unsuccessful
120      */
121     static FFADODevice * createDevice( std::auto_ptr<ConfigRom>( x ));
122
123     /**
124      * @brief This is called by the DeviceManager to discover & configure the device
125      *
126      * @return true if the device was discovered successfuly
127      */
128     virtual bool discover() = 0;
129
130     /**
131      * @brief Set the samping frequency
132      * @param samplingFrequency
133      * @return true if successful
134      */
135     virtual bool setSamplingFrequency( int samplingFrequency ) = 0;
136     /**
137      * @brief get the samplingfrequency as an integer
138      * @return the sampling frequency as integer
139      */
140     virtual int getSamplingFrequency( ) = 0;
141
142     /**
143      * @brief sync state enum
144      */
145     enum eSyncState {
146         eSS_Unknown=0,
147         eSS_Locked=1,
148         eSS_Unlocked=2,
149     };
150
151     /**
152      * @brief gets the devices current synchronization state
153      * @return the device's sync state
154      */
155     virtual enum eSyncState getSyncState( );
156
157     /**
158      * @brief clock source types
159      */
160     enum eClockSourceType {
161         eCT_Invalid,   ///> invalid entry (e.g. on error)
162         eCT_Auto,      ///> automatically select clock source
163         eCT_Internal,  ///> internal sync (unspecified)
164         eCT_1394Bus,   ///> Sync on the 1394 bus clock (e.g. CSP)
165         eCT_SytMatch,  ///> SYT match on incoming audio stream
166         eCT_SytStream, ///> SYT match on incoming sync stream
167         eCT_WordClock, ///> SYT on WordClock input
168         eCT_SPDIF,     ///> SYT on SPDIF input
169         eCT_ADAT,      ///> SYT on ADAT input
170         eCT_TDIF,      ///> SYT on TDIF input
171         eCT_AES,       ///> SYT on AES input
172     };
173
174     /**
175      * @brief convert the clock source type to a C string
176      * @return a C string describing the clock source type
177      */
178     static const char *ClockSourceTypeToString(enum eClockSourceType);
179
180     /**
181      * @brief Clock source identification struct
182      */
183     class sClockSource {
184         public:
185         sClockSource()
186             : type( eCT_Invalid )
187             , id( 0 )
188             , valid( false )
189             , active( false )
190             , locked( true )
191             , slipping( false )
192             , description( "" )
193         {};
194         virtual ~sClockSource() {};
195         /// indicates the type of the clock source (e.g. eCT_ADAT)
196         enum eClockSourceType type;
197         /// indicated the id of the clock source (e.g. id=1 => clocksource is ADAT_1)
198         unsigned int id;
199         /// is the clock source valid (i.e. can be selected) at this moment?
200         bool valid;
201         /// is the clock source active at this moment?
202         bool active;
203         /// is the clock source locked?
204         bool locked;
205         /// is the clock source slipping?
206         bool slipping;
207         /// description of the clock struct (optional)
208         std::string description;
209        
210         bool operator==(const sClockSource& x) {
211             return (type == x.type) && (id == x.id);
212         }
213     };
214     typedef struct sClockSource ClockSource;
215
216     typedef std::vector< ClockSource > ClockSourceVector;
217     typedef std::vector< ClockSource >::iterator ClockSourceVectorIterator;
218
219     /**
220      * @brief Get the clocksources supported by this device
221      *
222      * This function returns a vector of ClockSource structures that contains
223      * one entry for every clock source supported by the device.
224      *
225      * @returns a vector of ClockSource structures
226      */
227     virtual ClockSourceVector getSupportedClockSources() = 0;
228
229
230     /**
231      * @brief Sets the active clock source of this device
232      *
233      * This function sets the clock source of the device.
234      *
235      * @returns true upon success. false upon failure.
236      */
237     virtual bool setActiveClockSource(ClockSource) = 0;
238
239     /**
240      * @brief Returns the active clock source of this device
241      *
242      * This function returns the active clock source of the device.
243      *
244      * @returns the active ClockSource
245      */
246     virtual ClockSource getActiveClockSource() = 0;
247
248     /**
249      * @brief This is called by the device manager to give the device a unique ID.
250      *
251      * The purpose of this is to allow for unique port naming
252      * in case there are multiple identical devices on the bus.
253      * Some audio API's (e.g. jack) don't work properly when the
254      * port names are not unique.
255      *
256      * Say you have two devices having a port named OutputLeft.
257      * This can cause the streaming
258      * part to present two OutputLeft ports to the audio API,
259      * which won't work. This ID will allow you to construct
260      * the port names as 'dev1_OutputLeft' and 'dev2_OutputLeft'
261      *
262      * @note Currently this is a simple integer that is equal to
263      *       the position of the device in the devicemanager's
264      *       device list. Therefore it is dependant on the order
265      *       in which the devices are detected. The side-effect
266      *       of this is that it is dependant on the bus topology
267      *       and history (e.g. busresets etc). This makes that
268      *       these ID's are not fixed to a specific physical device.
269      *       At some point, we will replaced this with a GUID based
270      *       approach, which is tied to a physical device and is
271      *       bus & time independant.
272      *
273      * @param id
274      * @return true if successful
275      */
276     bool setId(unsigned int id);
277
278     /**
279      * @brief Outputs the device configuration to stderr/stdout [debug helper]
280      *
281      * This function prints out a (detailed) description of the
282      * device detected, and its configuration.
283      */
284     virtual void showDevice();
285
286     /**
287      * @brief Lock the device
288      *
289      * This is called by the streaming layer before we start manipulating
290      * and/or using the device.
291      *
292      * It should implement the mechanisms provided by the device to
293      * make sure that no other controller claims control of the device.
294      *
295      * @return true if successful, false if not
296      */
297     virtual bool lock() = 0;
298
299     /**
300      * @brief Unlock the device
301      *
302      * This is called by the streaming layer after we finish manipulating
303      * and/or using the device.
304      *
305      * It should implement the mechanisms provided by the device to
306      * give up exclusive control of the device.
307      *
308      * @return true if successful, false if not
309      */
310     virtual bool unlock() = 0;
311
312     /**
313      * @brief Enable streaming on all 'started' streams
314      *
315      * Enables the ISO streaming on all streams that are 'started'
316      * using startStreamByIndex. This is useful to control a 'master enable'
317      * function on the device.
318      *
319      * @return true if successful
320      */
321     virtual bool enableStreaming();
322
323     /**
324      * @brief Disable streaming on all streams
325      *
326      * Disables ISO streaming on all streams.
327      * This is useful to control a 'master enable'
328      * function on the device.
329      *
330      * @return true if successful
331      */
332     virtual bool disableStreaming();
333
334     /**
335      * @brief Prepare the device
336      *
337      * This is called by the streaming layer after the configuration
338      * parameters (e.g. sample rate) are set, and before
339      * getStreamProcessor[*] functions are called.
340      *
341      * It should be used to prepare the device's streamprocessors
342      * based upon the device's current configuration. Normally
343      * the streaming layer will not change the device's configuration
344      * after calling this function.
345      *
346      * @return true if successful, false if not
347      */
348     virtual bool prepare() = 0;
349
350     /**
351      * @brief Returns the number of ISO streams implemented/used by this device
352      *
353      * Most likely this is 2 streams, i.e. one transmit stream and one
354      * receive stream. However there are devices that implement more, for
355      * example BeBoB's implement 4 streams:
356      * - 2 audio streams (1 xmit/1 recv)
357      * - 2 sync streams (1 xmit/1 recv), which are an optional sync source
358      *   for the device.
359      *
360      * @note you have to have a StreamProcessor for every stream. I.e.
361      *       getStreamProcessorByIndex(i) should return a valid StreamProcessor
362      *       for i=0 to i=getStreamCount()-1
363      *
364      * @return number of streams available (both transmit and receive)
365      */
366     virtual int getStreamCount() = 0;
367
368     /**
369      * @brief Returns the StreamProcessor object for the stream with index i
370      *
371      * @note a streamprocessor returned by getStreamProcessorByIndex(i)
372      *       cannot be the same object as one returned by
373      *       getStreamProcessorByIndex(j) if i isn't equal to j
374      * @note you cannot have two streamprocessors handling the same ISO
375      *       channel (on the same port)
376      *
377      * @param i : Stream index
378      * @pre @ref i smaller than getStreamCount()
379      * @return a StreamProcessor object if successful, NULL otherwise
380      */
381     virtual Streaming::StreamProcessor *getStreamProcessorByIndex(int i) = 0;
382
383     /**
384      * @brief starts the stream with index i
385      *
386      * This function is called by the streaming layer when this stream should
387      * be started, i.e. the device should start sending data or should be prepared to
388      * be ready to receive data.
389      *
390      * It returns the channel number that was assigned for this stream.
391      * Channel allocation should be done using the allocation functions provided by the
392      * Ieee1394Service object that is passed in the constructor.
393      *
394      * @param i : Stream index
395      * @pre @ref i smaller than getStreamCount()
396      * @return true if successful, false if not
397      */
398     virtual bool startStreamByIndex(int i) = 0;
399
400     /**
401      * @brief stops the stream with index @ref i
402      *
403      * @param i : Stream index
404      * @pre @ref i smaller than getStreamCount()
405      * @return true if successful, false if not
406      */
407     virtual bool stopStreamByIndex(int i) = 0;
408
409     /**
410      * set verbosity level
411      */
412     virtual void setVerboseLevel(int l);
413
414     /**
415      * @brief return the node id of this device
416      *
417      * @return the node id
418      */
419     int getNodeId();
420
421     /**
422      * @brief return the nick name of this device
423      *
424      * @return string containing the name
425      */
426     virtual std::string getNickname();
427
428     /**
429      * @brief set the nick name of this device
430      * @param name new nickname
431      * @return true if successful
432      */
433     virtual bool setNickname(std::string name);
434
435     /**
436      * @brief handle a bus reset
437      *
438      * Called whenever a bus reset is detected. Handle everything
439      * that has to be done to cope with a bus reset.
440      *
441      */
442     // FIXME: not virtual?
443     void handleBusReset();
444
445     // the Control::Container functions
446     virtual std::string getName();
447     virtual bool setName( std::string n )
448         { return false; };
449
450     DeviceManager& getDeviceManager()
451         {return m_pDeviceManager;};
452 private:
453     std::auto_ptr<ConfigRom>( m_pConfigRom );
454     DeviceManager& m_pDeviceManager;
455     Control::Container* m_genericContainer;
456 protected:
457     DECLARE_DEBUG_MODULE;
458     Util::PosixMutex m_DeviceMutex;
459 };
460
461 #endif
Note: See TracBrowser for help on using the browser.