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 |
#include "fbtypes.h" |
---|
26 |
|
---|
27 |
#include "devicemanager.h" |
---|
28 |
#include "ffadodevice.h" |
---|
29 |
#include "DeviceStringParser.h" |
---|
30 |
|
---|
31 |
#include "libieee1394/configrom.h" |
---|
32 |
#include "libieee1394/ieee1394service.h" |
---|
33 |
|
---|
34 |
#include "libstreaming/generic/StreamProcessor.h" |
---|
35 |
#include "libstreaming/StreamProcessorManager.h" |
---|
36 |
|
---|
37 |
#include "debugmodule/debugmodule.h" |
---|
38 |
|
---|
39 |
#include "libutil/PosixMutex.h" |
---|
40 |
|
---|
41 |
#ifdef ENABLE_BEBOB |
---|
42 |
#include "bebob/bebob_avdevice.h" |
---|
43 |
#endif |
---|
44 |
|
---|
45 |
#ifdef ENABLE_GENERICAVC |
---|
46 |
#include "genericavc/avc_avdevice.h" |
---|
47 |
#endif |
---|
48 |
|
---|
49 |
#ifdef ENABLE_FIREWORKS |
---|
50 |
#include "fireworks/fireworks_device.h" |
---|
51 |
#endif |
---|
52 |
|
---|
53 |
#ifdef ENABLE_MOTU |
---|
54 |
#include "motu/motu_avdevice.h" |
---|
55 |
#endif |
---|
56 |
|
---|
57 |
#include <iostream> |
---|
58 |
#include <sstream> |
---|
59 |
|
---|
60 |
#include <algorithm> |
---|
61 |
|
---|
62 |
using namespace std; |
---|
63 |
|
---|
64 |
IMPL_DEBUG_MODULE( DeviceManager, DeviceManager, DEBUG_LEVEL_NORMAL ); |
---|
65 |
|
---|
66 |
DeviceManager::DeviceManager() |
---|
67 |
: Control::Container(NULL, "devicemanager") // this is the control root node |
---|
68 |
, m_avDevicesLock( new Util::PosixMutex() ) |
---|
69 |
, m_BusResetLock( new Util::PosixMutex() ) |
---|
70 |
, m_processorManager( new Streaming::StreamProcessorManager() ) |
---|
71 |
, m_deviceStringParser( new DeviceStringParser() ) |
---|
72 |
, m_used_cache_last_time( false ) |
---|
73 |
, m_ignore_busreset( false ) |
---|
74 |
, m_thread_realtime( false ) |
---|
75 |
, m_thread_priority( 0 ) |
---|
76 |
{ |
---|
77 |
addOption(Util::OptionContainer::Option("slaveMode",false)); |
---|
78 |
addOption(Util::OptionContainer::Option("snoopMode",false)); |
---|
79 |
} |
---|
80 |
|
---|
81 |
DeviceManager::~DeviceManager() |
---|
82 |
{ |
---|
83 |
m_avDevicesLock->Lock(); // make sure nobody is using this |
---|
84 |
for ( FFADODeviceVectorIterator it = m_avDevices.begin(); |
---|
85 |
it != m_avDevices.end(); |
---|
86 |
++it ) |
---|
87 |
{ |
---|
88 |
if (!deleteElement(*it)) { |
---|
89 |
debugWarning("failed to remove AvDevice from Control::Container\n"); |
---|
90 |
} |
---|
91 |
delete *it; |
---|
92 |
} |
---|
93 |
m_avDevicesLock->Unlock(); |
---|
94 |
delete m_avDevicesLock; |
---|
95 |
|
---|
96 |
// the SP's are automatically unregistered from the SPM |
---|
97 |
delete m_processorManager; |
---|
98 |
|
---|
99 |
for ( FunctorVectorIterator it = m_busreset_functors.begin(); |
---|
100 |
it != m_busreset_functors.end(); |
---|
101 |
++it ) |
---|
102 |
{ |
---|
103 |
delete *it; |
---|
104 |
} |
---|
105 |
delete m_BusResetLock; |
---|
106 |
|
---|
107 |
for ( Ieee1394ServiceVectorIterator it = m_1394Services.begin(); |
---|
108 |
it != m_1394Services.end(); |
---|
109 |
++it ) |
---|
110 |
{ |
---|
111 |
delete *it; |
---|
112 |
} |
---|
113 |
|
---|
114 |
delete m_deviceStringParser; |
---|
115 |
} |
---|
116 |
|
---|
117 |
bool |
---|
118 |
DeviceManager::setThreadParameters(bool rt, int priority) { |
---|
119 |
if (!m_processorManager->setThreadParameters(rt, priority)) { |
---|
120 |
debugError("Could not set processor manager thread parameters\n"); |
---|
121 |
return false; |
---|
122 |
} |
---|
123 |
for ( Ieee1394ServiceVectorIterator it = m_1394Services.begin(); |
---|
124 |
it != m_1394Services.end(); |
---|
125 |
++it ) |
---|
126 |
{ |
---|
127 |
if (!(*it)->setThreadParameters(rt, priority)) { |
---|
128 |
debugError("Could not set 1394 service thread parameters\n"); |
---|
129 |
return false; |
---|
130 |
} |
---|
131 |
} |
---|
132 |
m_thread_realtime = rt; |
---|
133 |
m_thread_priority = priority; |
---|
134 |
return true; |
---|
135 |
} |
---|
136 |
|
---|
137 |
bool |
---|
138 |
DeviceManager::initialize() |
---|
139 |
{ |
---|
140 |
assert(m_1394Services.size() == 0); |
---|
141 |
assert(m_busreset_functors.size() == 0); |
---|
142 |
|
---|
143 |
int nb_detected_ports = Ieee1394Service::detectNbPorts(); |
---|
144 |
if (nb_detected_ports < 0) { |
---|
145 |
debugFatal("Failed to detect the number of 1394 adapters. Is the IEEE1394 stack loaded (raw1394)?\n"); |
---|
146 |
return false; |
---|
147 |
} |
---|
148 |
if (nb_detected_ports == 0) { |
---|
149 |
debugFatal("No firewire adapters (ports) found.\n"); |
---|
150 |
return false; |
---|
151 |
} |
---|
152 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Found %d firewire adapters (ports)\n", nb_detected_ports); |
---|
153 |
for (unsigned int port = 0; port < (unsigned int)nb_detected_ports; port++) { |
---|
154 |
Ieee1394Service* tmp1394Service = new Ieee1394Service(); |
---|
155 |
if ( !tmp1394Service ) { |
---|
156 |
debugFatal( "Could not create Ieee1349Service object for port %d\n", port ); |
---|
157 |
return false; |
---|
158 |
} |
---|
159 |
tmp1394Service->setVerboseLevel( getDebugLevel() ); |
---|
160 |
m_1394Services.push_back(tmp1394Service); |
---|
161 |
|
---|
162 |
tmp1394Service->setThreadParameters(m_thread_realtime, m_thread_priority); |
---|
163 |
if ( !tmp1394Service->initialize( port ) ) { |
---|
164 |
debugFatal( "Could not initialize Ieee1349Service object for port %d\n", port ); |
---|
165 |
return false; |
---|
166 |
} |
---|
167 |
// add the bus reset handler |
---|
168 |
Util::Functor* tmp_busreset_functor = new Util::MemberFunctor0< DeviceManager*, |
---|
169 |
void (DeviceManager::*)() > |
---|
170 |
( this, &DeviceManager::busresetHandler, false ); |
---|
171 |
if ( !tmp_busreset_functor ) { |
---|
172 |
debugFatal( "Could not create busreset handler for port %d\n", port ); |
---|
173 |
return false; |
---|
174 |
} |
---|
175 |
m_busreset_functors.push_back(tmp_busreset_functor); |
---|
176 |
|
---|
177 |
tmp1394Service->addBusResetHandler( tmp_busreset_functor ); |
---|
178 |
} |
---|
179 |
|
---|
180 |
return true; |
---|
181 |
} |
---|
182 |
|
---|
183 |
bool |
---|
184 |
DeviceManager::addSpecString(char *s) { |
---|
185 |
std::string spec = s; |
---|
186 |
if(isSpecStringValid(spec)) { |
---|
187 |
debugOutput(DEBUG_LEVEL_VERBOSE, "Adding spec string %s\n", spec.c_str()); |
---|
188 |
assert(m_deviceStringParser); |
---|
189 |
m_deviceStringParser->parseString(spec); |
---|
190 |
return true; |
---|
191 |
} else { |
---|
192 |
debugError("Invalid spec string: %s\n", spec.c_str()); |
---|
193 |
return false; |
---|
194 |
} |
---|
195 |
} |
---|
196 |
|
---|
197 |
bool |
---|
198 |
DeviceManager::isSpecStringValid(std::string s) { |
---|
199 |
assert(m_deviceStringParser); |
---|
200 |
return m_deviceStringParser->isValidString(s); |
---|
201 |
} |
---|
202 |
|
---|
203 |
void |
---|
204 |
DeviceManager::busresetHandler() |
---|
205 |
{ |
---|
206 |
// serialize bus reset handling since it can be that a new one occurs while we're |
---|
207 |
// doing stuff. |
---|
208 |
Util::MutexLockHelper lock(*m_BusResetLock); |
---|
209 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Bus reset...\n" ); |
---|
210 |
if(m_ignore_busreset) { |
---|
211 |
debugOutput( DEBUG_LEVEL_VERBOSE, " ignoring...\n" ); |
---|
212 |
return; |
---|
213 |
} |
---|
214 |
|
---|
215 |
// FIXME: what port was the bus reset on? |
---|
216 |
// FIXME: what if the devices are gone? |
---|
217 |
// propagate the bus reset to all avDevices |
---|
218 |
m_avDevicesLock->Lock(); // make sure nobody is using this |
---|
219 |
for ( FFADODeviceVectorIterator it = m_avDevices.begin(); |
---|
220 |
it != m_avDevices.end(); |
---|
221 |
++it ) |
---|
222 |
{ |
---|
223 |
(*it)->handleBusReset(); |
---|
224 |
} |
---|
225 |
m_avDevicesLock->Unlock(); |
---|
226 |
|
---|
227 |
// notify the streamprocessormanager of the busreset |
---|
228 |
if(m_processorManager) { |
---|
229 |
m_processorManager->handleBusReset(); |
---|
230 |
} else { |
---|
231 |
debugWarning("No valid SPM\n"); |
---|
232 |
} |
---|
233 |
|
---|
234 |
// rediscover to find new devices |
---|
235 |
// (only for the control server ATM, streaming can't dynamically add/remove devices) |
---|
236 |
if(!discover(m_used_cache_last_time, true)) { |
---|
237 |
debugError("Could not rediscover devices\n"); |
---|
238 |
} |
---|
239 |
|
---|
240 |
// notify any clients |
---|
241 |
signalNotifiers(m_busResetNotifiers); |
---|
242 |
|
---|
243 |
// display the new state |
---|
244 |
showDeviceInfo(); |
---|
245 |
} |
---|
246 |
|
---|
247 |
void |
---|
248 |
DeviceManager::signalNotifiers(notif_vec_t& list) |
---|
249 |
{ |
---|
250 |
for ( notif_vec_t::iterator it = list.begin(); |
---|
251 |
it != list.end(); |
---|
252 |
++it ) |
---|
253 |
{ |
---|
254 |
Util::Functor* func = *it; |
---|
255 |
debugOutput( DEBUG_LEVEL_VERBOSE, " running notifier %p...\n", func ); |
---|
256 |
( *func )(); |
---|
257 |
} |
---|
258 |
} |
---|
259 |
|
---|
260 |
bool |
---|
261 |
DeviceManager::registerNotification(notif_vec_t& list, Util::Functor *handler) |
---|
262 |
{ |
---|
263 |
debugOutput( DEBUG_LEVEL_VERBOSE, "register %p...\n", handler); |
---|
264 |
assert(handler); |
---|
265 |
for ( notif_vec_t::iterator it = list.begin(); |
---|
266 |
it != list.end(); |
---|
267 |
++it ) |
---|
268 |
{ |
---|
269 |
if ( *it == handler ) { |
---|
270 |
debugOutput(DEBUG_LEVEL_VERBOSE, "already registered\n"); |
---|
271 |
return false; |
---|
272 |
} |
---|
273 |
} |
---|
274 |
list.push_back(handler); |
---|
275 |
return true; |
---|
276 |
} |
---|
277 |
|
---|
278 |
bool |
---|
279 |
DeviceManager::unregisterNotification(notif_vec_t& list, Util::Functor *handler) |
---|
280 |
{ |
---|
281 |
debugOutput( DEBUG_LEVEL_VERBOSE, "unregister %p...\n", handler); |
---|
282 |
assert(handler); |
---|
283 |
|
---|
284 |
for ( notif_vec_t::iterator it = list.begin(); |
---|
285 |
it != list.end(); |
---|
286 |
++it ) |
---|
287 |
{ |
---|
288 |
if ( *it == handler ) { |
---|
289 |
list.erase(it); |
---|
290 |
return true; |
---|
291 |
} |
---|
292 |
} |
---|
293 |
debugError("Could not find handler (%p)\n", handler); |
---|
294 |
return false; //not found |
---|
295 |
} |
---|
296 |
|
---|
297 |
bool |
---|
298 |
DeviceManager::discover( bool useCache, bool rediscover ) |
---|
299 |
{ |
---|
300 |
m_used_cache_last_time = useCache; |
---|
301 |
bool slaveMode=false; |
---|
302 |
if(!getOption("slaveMode", slaveMode)) { |
---|
303 |
debugWarning("Could not retrieve slaveMode parameter, defauling to false\n"); |
---|
304 |
} |
---|
305 |
bool snoopMode=false; |
---|
306 |
if(!getOption("snoopMode", snoopMode)) { |
---|
307 |
debugWarning("Could not retrieve snoopMode parameter, defauling to false\n"); |
---|
308 |
} |
---|
309 |
|
---|
310 |
setVerboseLevel(getDebugLevel()); |
---|
311 |
|
---|
312 |
// FIXME: it could be that a 1394service has disappeared (cardbus) |
---|
313 |
|
---|
314 |
ConfigRomVector configRoms; |
---|
315 |
// build a list of configroms on the bus. |
---|
316 |
for ( Ieee1394ServiceVectorIterator it = m_1394Services.begin(); |
---|
317 |
it != m_1394Services.end(); |
---|
318 |
++it ) |
---|
319 |
{ |
---|
320 |
Ieee1394Service *portService = *it; |
---|
321 |
for ( fb_nodeid_t nodeId = 0; |
---|
322 |
nodeId < portService->getNodeCount(); |
---|
323 |
++nodeId ) |
---|
324 |
{ |
---|
325 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Probing node %d...\n", nodeId ); |
---|
326 |
|
---|
327 |
if (nodeId == portService->getLocalNodeId()) { |
---|
328 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Skipping local node (%d)...\n", nodeId ); |
---|
329 |
continue; |
---|
330 |
} |
---|
331 |
|
---|
332 |
ConfigRom * configRom = new ConfigRom( *portService, nodeId ); |
---|
333 |
if ( !configRom->initialize() ) { |
---|
334 |
// \todo If a PHY on the bus is in power safe mode then |
---|
335 |
// the config rom is missing. So this might be just |
---|
336 |
// such this case and we can safely skip it. But it might |
---|
337 |
// be there is a real software problem on our side. |
---|
338 |
// This should be handlede more carefuly. |
---|
339 |
debugOutput( DEBUG_LEVEL_NORMAL, |
---|
340 |
"Could not read config rom from device (node id %d). " |
---|
341 |
"Skip device discovering for this node\n", |
---|
342 |
nodeId ); |
---|
343 |
continue; |
---|
344 |
} |
---|
345 |
configRoms.push_back(configRom); |
---|
346 |
} |
---|
347 |
} |
---|
348 |
|
---|
349 |
|
---|
350 |
// notify that we are going to manipulate the list |
---|
351 |
signalNotifiers(m_preUpdateNotifiers); |
---|
352 |
m_avDevicesLock->Lock(); // make sure nobody starts using the list |
---|
353 |
if(rediscover) { |
---|
354 |
|
---|
355 |
FFADODeviceVector discovered_devices_on_bus; |
---|
356 |
for ( FFADODeviceVectorIterator it = m_avDevices.begin(); |
---|
357 |
it != m_avDevices.end(); |
---|
358 |
++it ) |
---|
359 |
{ |
---|
360 |
bool seen_device = false; |
---|
361 |
for ( ConfigRomVectorIterator it2 = configRoms.begin(); |
---|
362 |
it2 != configRoms.end(); |
---|
363 |
++it2 ) |
---|
364 |
{ |
---|
365 |
seen_device |= ((*it)->getConfigRom().getGuid() == (*it2)->getGuid()); |
---|
366 |
} |
---|
367 |
|
---|
368 |
if(seen_device) { |
---|
369 |
debugOutput( DEBUG_LEVEL_VERBOSE, |
---|
370 |
"Already discovered device with GUID: %s\n", |
---|
371 |
(*it)->getConfigRom().getGuidString().c_str() ); |
---|
372 |
// we already discovered this device, and it is still here. keep it |
---|
373 |
discovered_devices_on_bus.push_back(*it); |
---|
374 |
} else { |
---|
375 |
debugOutput( DEBUG_LEVEL_VERBOSE, |
---|
376 |
"Device with GUID: %s disappeared from bus, removing...\n", |
---|
377 |
(*it)->getConfigRom().getGuidString().c_str() ); |
---|
378 |
|
---|
379 |
// the device has disappeared, remove it from the control tree |
---|
380 |
if (!deleteElement(*it)) { |
---|
381 |
debugWarning("failed to remove AvDevice from Control::Container\n"); |
---|
382 |
} |
---|
383 |
// delete the device |
---|
384 |
// FIXME: this will mess up the any code that waits for bus resets to |
---|
385 |
// occur |
---|
386 |
delete *it; |
---|
387 |
} |
---|
388 |
} |
---|
389 |
// prune the devices that disappeared |
---|
390 |
m_avDevices = discovered_devices_on_bus; |
---|
391 |
} else { // remove everything since we are not rediscovering |
---|
392 |
for ( FFADODeviceVectorIterator it = m_avDevices.begin(); |
---|
393 |
it != m_avDevices.end(); |
---|
394 |
++it ) |
---|
395 |
{ |
---|
396 |
if (!deleteElement(*it)) { |
---|
397 |
debugWarning("failed to remove AvDevice from Control::Container\n"); |
---|
398 |
} |
---|
399 |
delete *it; |
---|
400 |
} |
---|
401 |
|
---|
402 |
m_avDevices.clear(); |
---|
403 |
} |
---|
404 |
|
---|
405 |
// delete the config rom list entries |
---|
406 |
// FIXME: we should reuse it |
---|
407 |
for ( ConfigRomVectorIterator it = configRoms.begin(); |
---|
408 |
it != configRoms.end(); |
---|
409 |
++it ) |
---|
410 |
{ |
---|
411 |
delete *it; |
---|
412 |
} |
---|
413 |
|
---|
414 |
assert(m_deviceStringParser); |
---|
415 |
// show the spec strings we're going to use |
---|
416 |
if(getDebugLevel() >= DEBUG_LEVEL_VERBOSE) { |
---|
417 |
m_deviceStringParser->show(); |
---|
418 |
} |
---|
419 |
|
---|
420 |
if (!slaveMode) { |
---|
421 |
for ( Ieee1394ServiceVectorIterator it = m_1394Services.begin(); |
---|
422 |
it != m_1394Services.end(); |
---|
423 |
++it ) |
---|
424 |
{ |
---|
425 |
Ieee1394Service *portService = *it; |
---|
426 |
for ( fb_nodeid_t nodeId = 0; |
---|
427 |
nodeId < portService->getNodeCount(); |
---|
428 |
++nodeId ) |
---|
429 |
{ |
---|
430 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Probing node %d...\n", nodeId ); |
---|
431 |
|
---|
432 |
if (nodeId == portService->getLocalNodeId()) { |
---|
433 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Skipping local node (%d)...\n", nodeId ); |
---|
434 |
continue; |
---|
435 |
} |
---|
436 |
|
---|
437 |
std::auto_ptr<ConfigRom> configRom = |
---|
438 |
std::auto_ptr<ConfigRom>( new ConfigRom( *portService, nodeId ) ); |
---|
439 |
if ( !configRom->initialize() ) { |
---|
440 |
// \todo If a PHY on the bus is in power safe mode then |
---|
441 |
// the config rom is missing. So this might be just |
---|
442 |
// such this case and we can safely skip it. But it might |
---|
443 |
// be there is a real software problem on our side. |
---|
444 |
// This should be handlede more carefuly. |
---|
445 |
debugOutput( DEBUG_LEVEL_NORMAL, |
---|
446 |
"Could not read config rom from device (node id %d). " |
---|
447 |
"Skip device discovering for this node\n", |
---|
448 |
nodeId ); |
---|
449 |
continue; |
---|
450 |
} |
---|
451 |
|
---|
452 |
bool already_in_vector = false; |
---|
453 |
for ( FFADODeviceVectorIterator it = m_avDevices.begin(); |
---|
454 |
it != m_avDevices.end(); |
---|
455 |
++it ) |
---|
456 |
{ |
---|
457 |
if ((*it)->getConfigRom().getGuid() == configRom->getGuid()) { |
---|
458 |
already_in_vector = true; |
---|
459 |
break; |
---|
460 |
} |
---|
461 |
} |
---|
462 |
if(already_in_vector) { |
---|
463 |
if(!rediscover) { |
---|
464 |
debugWarning("Device with GUID %s already discovered on other port, skipping device...\n", |
---|
465 |
configRom->getGuidString().c_str()); |
---|
466 |
} |
---|
467 |
continue; |
---|
468 |
} |
---|
469 |
|
---|
470 |
if(getDebugLevel() >= DEBUG_LEVEL_VERBOSE) { |
---|
471 |
configRom->printConfigRomDebug(); |
---|
472 |
} |
---|
473 |
|
---|
474 |
// if spec strings are given, only add those devices |
---|
475 |
// that match the spec string(s). |
---|
476 |
// if no (valid) spec strings are present, grab all |
---|
477 |
// supported devices. |
---|
478 |
if(m_deviceStringParser->countDeviceStrings() && |
---|
479 |
!m_deviceStringParser->match(*configRom.get())) { |
---|
480 |
debugOutput(DEBUG_LEVEL_VERBOSE, "Device doesn't match any of the spec strings. skipping...\n"); |
---|
481 |
continue; |
---|
482 |
} |
---|
483 |
|
---|
484 |
// find a driver |
---|
485 |
FFADODevice* avDevice = getDriverForDevice( configRom, |
---|
486 |
nodeId ); |
---|
487 |
|
---|
488 |
if ( avDevice ) { |
---|
489 |
debugOutput( DEBUG_LEVEL_NORMAL, |
---|
490 |
"driver found for device %d\n", |
---|
491 |
nodeId ); |
---|
492 |
|
---|
493 |
avDevice->setVerboseLevel( getDebugLevel() ); |
---|
494 |
bool isFromCache = false; |
---|
495 |
if ( useCache && avDevice->loadFromCache() ) { |
---|
496 |
debugOutput( DEBUG_LEVEL_VERBOSE, "could load from cache\n" ); |
---|
497 |
isFromCache = true; |
---|
498 |
// restore the debug level for everything that was loaded |
---|
499 |
avDevice->setVerboseLevel( getDebugLevel() ); |
---|
500 |
} else if ( avDevice->discover() ) { |
---|
501 |
debugOutput( DEBUG_LEVEL_VERBOSE, "discovery successful\n" ); |
---|
502 |
} else { |
---|
503 |
debugError( "could not discover device\n" ); |
---|
504 |
delete avDevice; |
---|
505 |
continue; |
---|
506 |
} |
---|
507 |
|
---|
508 |
if (snoopMode) { |
---|
509 |
debugOutput( DEBUG_LEVEL_VERBOSE, |
---|
510 |
"Enabling snoop mode on node %d...\n", nodeId ); |
---|
511 |
|
---|
512 |
if(!avDevice->setOption("snoopMode", snoopMode)) { |
---|
513 |
debugWarning("Could not set snoop mode for device on node %d\n", nodeId); |
---|
514 |
delete avDevice; |
---|
515 |
continue; |
---|
516 |
} |
---|
517 |
} |
---|
518 |
|
---|
519 |
if ( !isFromCache && !avDevice->saveCache() ) { |
---|
520 |
debugOutput( DEBUG_LEVEL_VERBOSE, "No cached version of AVC model created\n" ); |
---|
521 |
} |
---|
522 |
m_avDevices.push_back( avDevice ); |
---|
523 |
|
---|
524 |
if (!addElement(avDevice)) { |
---|
525 |
debugWarning("failed to add AvDevice to Control::Container\n"); |
---|
526 |
} |
---|
527 |
|
---|
528 |
debugOutput( DEBUG_LEVEL_NORMAL, "discovery of node %d on port %d done...\n", nodeId, portService->getPort() ); |
---|
529 |
} |
---|
530 |
} |
---|
531 |
} |
---|
532 |
|
---|
533 |
debugOutput( DEBUG_LEVEL_NORMAL, "Discovery finished...\n" ); |
---|
534 |
// FIXME: do better sorting |
---|
535 |
// sort the m_avDevices vector on their GUID |
---|
536 |
// then assign reassign the id's to the devices |
---|
537 |
// the side effect of this is that for the same set of attached devices, |
---|
538 |
// a device id always corresponds to the same device |
---|
539 |
sort(m_avDevices.begin(), m_avDevices.end(), FFADODevice::compareGUID); |
---|
540 |
int i=0; |
---|
541 |
for ( FFADODeviceVectorIterator it = m_avDevices.begin(); |
---|
542 |
it != m_avDevices.end(); |
---|
543 |
++it ) |
---|
544 |
{ |
---|
545 |
if ( !(*it)->setId( i++ ) ) { |
---|
546 |
debugError( "setting Id failed\n" ); |
---|
547 |
} |
---|
548 |
} |
---|
549 |
showDeviceInfo(); |
---|
550 |
|
---|
551 |
} else { // slave mode |
---|
552 |
// notify any clients |
---|
553 |
signalNotifiers(m_preUpdateNotifiers); |
---|
554 |
Ieee1394Service *portService = m_1394Services.at(0); |
---|
555 |
fb_nodeid_t nodeId = portService->getLocalNodeId(); |
---|
556 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Starting in slave mode on node %d...\n", nodeId ); |
---|
557 |
|
---|
558 |
std::auto_ptr<ConfigRom> configRom = |
---|
559 |
std::auto_ptr<ConfigRom>( new ConfigRom( *portService, |
---|
560 |
nodeId ) ); |
---|
561 |
if ( !configRom->initialize() ) { |
---|
562 |
// \todo If a PHY on the bus is in power safe mode then |
---|
563 |
// the config rom is missing. So this might be just |
---|
564 |
// such this case and we can safely skip it. But it might |
---|
565 |
// be there is a real software problem on our side. |
---|
566 |
// This should be handled more carefuly. |
---|
567 |
debugOutput( DEBUG_LEVEL_NORMAL, |
---|
568 |
"Could not read config rom from device (node id %d). " |
---|
569 |
"Skip device discovering for this node\n", |
---|
570 |
nodeId ); |
---|
571 |
return false; |
---|
572 |
} |
---|
573 |
|
---|
574 |
// remove any already present devices |
---|
575 |
for ( FFADODeviceVectorIterator it = m_avDevices.begin(); |
---|
576 |
it != m_avDevices.end(); |
---|
577 |
++it ) |
---|
578 |
{ |
---|
579 |
if (!deleteElement(*it)) { |
---|
580 |
debugWarning("failed to remove AvDevice from Control::Container\n"); |
---|
581 |
} |
---|
582 |
delete *it; |
---|
583 |
} |
---|
584 |
|
---|
585 |
m_avDevices.clear(); |
---|
586 |
|
---|
587 |
// get the slave driver |
---|
588 |
FFADODevice* avDevice = getSlaveDriver( configRom ); |
---|
589 |
if ( avDevice ) { |
---|
590 |
debugOutput( DEBUG_LEVEL_NORMAL, |
---|
591 |
"driver found for device %d\n", |
---|
592 |
nodeId ); |
---|
593 |
|
---|
594 |
avDevice->setVerboseLevel( getDebugLevel() ); |
---|
595 |
|
---|
596 |
if ( !avDevice->discover() ) { |
---|
597 |
debugError( "could not discover device\n" ); |
---|
598 |
delete avDevice; |
---|
599 |
return false; |
---|
600 |
} |
---|
601 |
|
---|
602 |
if ( !avDevice->setId( m_avDevices.size() ) ) { |
---|
603 |
debugError( "setting Id failed\n" ); |
---|
604 |
} |
---|
605 |
if ( getDebugLevel() >= DEBUG_LEVEL_VERBOSE ) { |
---|
606 |
avDevice->showDevice(); |
---|
607 |
} |
---|
608 |
m_avDevices.push_back( avDevice ); |
---|
609 |
debugOutput( DEBUG_LEVEL_NORMAL, "discovery of node %d on port %d done...\n", nodeId, portService->getPort() ); |
---|
610 |
} |
---|
611 |
|
---|
612 |
debugOutput( DEBUG_LEVEL_NORMAL, "discovery finished...\n" ); |
---|
613 |
} |
---|
614 |
|
---|
615 |
m_avDevicesLock->Unlock(); |
---|
616 |
// notify any clients |
---|
617 |
signalNotifiers(m_postUpdateNotifiers); |
---|
618 |
return true; |
---|
619 |
} |
---|
620 |
|
---|
621 |
bool |
---|
622 |
DeviceManager::initStreaming() |
---|
623 |
{ |
---|
624 |
// iterate over the found devices |
---|
625 |
// add the stream processors of the devices to the managers |
---|
626 |
for ( FFADODeviceVectorIterator it = m_avDevices.begin(); |
---|
627 |
it != m_avDevices.end(); |
---|
628 |
++it ) |
---|
629 |
{ |
---|
630 |
FFADODevice *device = *it; |
---|
631 |
assert(device); |
---|
632 |
|
---|
633 |
debugOutput(DEBUG_LEVEL_VERBOSE, "Locking device (%p)\n", device); |
---|
634 |
|
---|
635 |
if (!device->lock()) { |
---|
636 |
debugWarning("Could not lock device, skipping device (%p)!\n", device); |
---|
637 |
continue; |
---|
638 |
} |
---|
639 |
|
---|
640 |
debugOutput(DEBUG_LEVEL_VERBOSE, "Setting samplerate to %d for (%p)\n", |
---|
641 |
m_processorManager->getNominalRate(), device); |
---|
642 |
|
---|
643 |
// Set the device's sampling rate to that requested |
---|
644 |
// FIXME: does this really belong here? If so we need to handle errors. |
---|
645 |
if (!device->setSamplingFrequency(m_processorManager->getNominalRate())) { |
---|
646 |
debugOutput(DEBUG_LEVEL_VERBOSE, " => Retry setting samplerate to %d for (%p)\n", |
---|
647 |
m_processorManager->getNominalRate(), device); |
---|
648 |
|
---|
649 |
// try again: |
---|
650 |
if (!device->setSamplingFrequency(m_processorManager->getNominalRate())) { |
---|
651 |
debugFatal("Could not set sampling frequency to %d\n",m_processorManager->getNominalRate()); |
---|
652 |
return false; |
---|
653 |
} |
---|
654 |
} |
---|
655 |
// prepare the device |
---|
656 |
device->prepare(); |
---|
657 |
} |
---|
658 |
|
---|
659 |
// set the sync source |
---|
660 |
if (!m_processorManager->setSyncSource(getSyncSource())) { |
---|
661 |
debugWarning("Could not set processorManager sync source (%p)\n", |
---|
662 |
getSyncSource()); |
---|
663 |
} |
---|
664 |
return true; |
---|
665 |
} |
---|
666 |
|
---|
667 |
bool |
---|
668 |
DeviceManager::prepareStreaming() |
---|
669 |
{ |
---|
670 |
if (!m_processorManager->prepare()) { |
---|
671 |
debugFatal("Could not prepare streaming...\n"); |
---|
672 |
return false; |
---|
673 |
} |
---|
674 |
return true; |
---|
675 |
} |
---|
676 |
|
---|
677 |
bool |
---|
678 |
DeviceManager::finishStreaming() { |
---|
679 |
bool result = true; |
---|
680 |
// iterate over the found devices |
---|
681 |
for ( FFADODeviceVectorIterator it = m_avDevices.begin(); |
---|
682 |
it != m_avDevices.end(); |
---|
683 |
++it ) |
---|
684 |
{ |
---|
685 |
debugOutput(DEBUG_LEVEL_VERBOSE, "Unlocking device (%p)\n", *it); |
---|
686 |
|
---|
687 |
if (!(*it)->unlock()) { |
---|
688 |
debugWarning("Could not unlock device (%p)!\n", *it); |
---|
689 |
result = false; |
---|
690 |
} |
---|
691 |
} |
---|
692 |
return result; |
---|
693 |
} |
---|
694 |
|
---|
695 |
bool |
---|
696 |
DeviceManager::startStreaming() { |
---|
697 |
// create the connections for all devices |
---|
698 |
// iterate over the found devices |
---|
699 |
// add the stream processors of the devices to the managers |
---|
700 |
for ( FFADODeviceVectorIterator it = m_avDevices.begin(); |
---|
701 |
it != m_avDevices.end(); |
---|
702 |
++it ) |
---|
703 |
{ |
---|
704 |
FFADODevice *device = *it; |
---|
705 |
assert(device); |
---|
706 |
|
---|
707 |
int j=0; |
---|
708 |
for(j=0; j < device->getStreamCount(); j++) { |
---|
709 |
debugOutput(DEBUG_LEVEL_VERBOSE,"Starting stream %d of device %p\n", j, device); |
---|
710 |
// start the stream |
---|
711 |
if (!device->startStreamByIndex(j)) { |
---|
712 |
debugWarning("Could not start stream %d of device %p\n", j, device); |
---|
713 |
continue; |
---|
714 |
} |
---|
715 |
} |
---|
716 |
|
---|
717 |
if (!device->enableStreaming()) { |
---|
718 |
debugWarning("Could not enable streaming on device %p!\n", device); |
---|
719 |
} |
---|
720 |
} |
---|
721 |
|
---|
722 |
if(m_processorManager->start()) { |
---|
723 |
return true; |
---|
724 |
} else { |
---|
725 |
stopStreaming(); |
---|
726 |
return false; |
---|
727 |
} |
---|
728 |
} |
---|
729 |
|
---|
730 |
bool |
---|
731 |
DeviceManager::resetStreaming() { |
---|
732 |
return true; |
---|
733 |
} |
---|
734 |
|
---|
735 |
bool |
---|
736 |
DeviceManager::stopStreaming() |
---|
737 |
{ |
---|
738 |
bool result = true; |
---|
739 |
m_processorManager->stop(); |
---|
740 |
|
---|
741 |
// create the connections for all devices |
---|
742 |
// iterate over the found devices |
---|
743 |
// add the stream processors of the devices to the managers |
---|
744 |
for ( FFADODeviceVectorIterator it = m_avDevices.begin(); |
---|
745 |
it != m_avDevices.end(); |
---|
746 |
++it ) |
---|
747 |
{ |
---|
748 |
FFADODevice *device = *it; |
---|
749 |
assert(device); |
---|
750 |
|
---|
751 |
if (!device->disableStreaming()) { |
---|
752 |
debugWarning("Could not disable streaming on device %p!\n", device); |
---|
753 |
} |
---|
754 |
|
---|
755 |
int j=0; |
---|
756 |
for(j=0; j < device->getStreamCount(); j++) { |
---|
757 |
debugOutput(DEBUG_LEVEL_VERBOSE,"Stopping stream %d of device %p\n", j, device); |
---|
758 |
// stop the stream |
---|
759 |
// start the stream |
---|
760 |
if (!device->stopStreamByIndex(j)) { |
---|
761 |
debugWarning("Could not stop stream %d of device %p\n", j, device); |
---|
762 |
result = false; |
---|
763 |
continue; |
---|
764 |
} |
---|
765 |
} |
---|
766 |
} |
---|
767 |
return result; |
---|
768 |
} |
---|
769 |
|
---|
770 |
enum DeviceManager::eWaitResult |
---|
771 |
DeviceManager::waitForPeriod() { |
---|
772 |
if(m_processorManager->waitForPeriod()) { |
---|
773 |
return eWR_OK; |
---|
774 |
} else { |
---|
775 |
if(m_processorManager->shutdownNeeded()) { |
---|
776 |
debugWarning("Shutdown requested\n"); |
---|
777 |
return eWR_Shutdown; |
---|
778 |
} else { |
---|
779 |
debugWarning("XRUN detected\n"); |
---|
780 |
// do xrun recovery |
---|
781 |
if(m_processorManager->handleXrun()) { |
---|
782 |
return eWR_Xrun; |
---|
783 |
} else { |
---|
784 |
debugError("Could not handle XRUN\n"); |
---|
785 |
return eWR_Error; |
---|
786 |
} |
---|
787 |
} |
---|
788 |
} |
---|
789 |
} |
---|
790 |
|
---|
791 |
bool |
---|
792 |
DeviceManager::setStreamingParams(unsigned int period, unsigned int rate, unsigned int nb_buffers) { |
---|
793 |
m_processorManager->setPeriodSize(period); |
---|
794 |
m_processorManager->setNominalRate(rate); |
---|
795 |
m_processorManager->setNbBuffers(nb_buffers); |
---|
796 |
return true; |
---|
797 |
} |
---|
798 |
|
---|
799 |
FFADODevice* |
---|
800 |
DeviceManager::getDriverForDevice( std::auto_ptr<ConfigRom>( configRom ), |
---|
801 |
int id ) |
---|
802 |
{ |
---|
803 |
#ifdef ENABLE_BEBOB |
---|
804 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Trying BeBoB...\n" ); |
---|
805 |
if ( BeBoB::AvDevice::probe( *configRom.get() ) ) { |
---|
806 |
return BeBoB::AvDevice::createDevice( *this, configRom ); |
---|
807 |
} |
---|
808 |
#endif |
---|
809 |
|
---|
810 |
#ifdef ENABLE_GENERICAVC |
---|
811 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Trying Generic AV/C...\n" ); |
---|
812 |
if ( GenericAVC::AvDevice::probe( *configRom.get() ) ) { |
---|
813 |
return GenericAVC::AvDevice::createDevice( *this, configRom ); |
---|
814 |
} |
---|
815 |
#endif |
---|
816 |
|
---|
817 |
#ifdef ENABLE_FIREWORKS |
---|
818 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Trying ECHO Audio FireWorks...\n" ); |
---|
819 |
if ( FireWorks::Device::probe( *configRom.get() ) ) { |
---|
820 |
return FireWorks::Device::createDevice( *this, configRom ); |
---|
821 |
} |
---|
822 |
#endif |
---|
823 |
|
---|
824 |
#ifdef ENABLE_MOTU |
---|
825 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Trying Motu...\n" ); |
---|
826 |
if ( Motu::MotuDevice::probe( *configRom.get() ) ) { |
---|
827 |
return Motu::MotuDevice::createDevice( *this, configRom ); |
---|
828 |
} |
---|
829 |
#endif |
---|
830 |
|
---|
831 |
return 0; |
---|
832 |
} |
---|
833 |
|
---|
834 |
FFADODevice* |
---|
835 |
DeviceManager::getSlaveDriver( std::auto_ptr<ConfigRom>( configRom ) ) |
---|
836 |
{ |
---|
837 |
return 0; |
---|
838 |
} |
---|
839 |
|
---|
840 |
bool |
---|
841 |
DeviceManager::isValidNode(int node) |
---|
842 |
{ |
---|
843 |
for ( FFADODeviceVectorIterator it = m_avDevices.begin(); |
---|
844 |
it != m_avDevices.end(); |
---|
845 |
++it ) |
---|
846 |
{ |
---|
847 |
FFADODevice* avDevice = *it; |
---|
848 |
|
---|
849 |
if (avDevice->getConfigRom().getNodeId() == node) { |
---|
850 |
return true; |
---|
851 |
} |
---|
852 |
} |
---|
853 |
return false; |
---|
854 |
} |
---|
855 |
|
---|
856 |
int |
---|
857 |
DeviceManager::getNbDevices() |
---|
858 |
{ |
---|
859 |
return m_avDevices.size(); |
---|
860 |
} |
---|
861 |
|
---|
862 |
int |
---|
863 |
DeviceManager::getDeviceNodeId( int deviceNr ) |
---|
864 |
{ |
---|
865 |
if ( ! ( deviceNr < getNbDevices() ) ) { |
---|
866 |
debugError( "Device number out of range (%d)\n", deviceNr ); |
---|
867 |
return -1; |
---|
868 |
} |
---|
869 |
|
---|
870 |
FFADODevice* avDevice = m_avDevices.at( deviceNr ); |
---|
871 |
|
---|
872 |
if ( !avDevice ) { |
---|
873 |
debugError( "Could not get device at position (%d)\n", deviceNr ); |
---|
874 |
} |
---|
875 |
|
---|
876 |
return avDevice->getConfigRom().getNodeId(); |
---|
877 |
} |
---|
878 |
|
---|
879 |
FFADODevice* |
---|
880 |
DeviceManager::getAvDevice( int nodeId ) |
---|
881 |
{ |
---|
882 |
for ( FFADODeviceVectorIterator it = m_avDevices.begin(); |
---|
883 |
it != m_avDevices.end(); |
---|
884 |
++it ) |
---|
885 |
{ |
---|
886 |
FFADODevice* avDevice = *it; |
---|
887 |
if ( avDevice->getConfigRom().getNodeId() == nodeId ) { |
---|
888 |
return avDevice; |
---|
889 |
} |
---|
890 |
} |
---|
891 |
|
---|
892 |
return 0; |
---|
893 |
} |
---|
894 |
|
---|
895 |
FFADODevice* |
---|
896 |
DeviceManager::getAvDeviceByIndex( int idx ) |
---|
897 |
{ |
---|
898 |
return m_avDevices.at(idx); |
---|
899 |
} |
---|
900 |
|
---|
901 |
unsigned int |
---|
902 |
DeviceManager::getAvDeviceCount( ) |
---|
903 |
{ |
---|
904 |
return m_avDevices.size(); |
---|
905 |
} |
---|
906 |
|
---|
907 |
/** |
---|
908 |
* Return the streamprocessor that is to be used as |
---|
909 |
* the sync source. |
---|
910 |
* |
---|
911 |
* Algorithm still to be determined |
---|
912 |
* |
---|
913 |
* @return StreamProcessor that is sync source |
---|
914 |
*/ |
---|
915 |
Streaming::StreamProcessor * |
---|
916 |
DeviceManager::getSyncSource() { |
---|
917 |
FFADODevice* device = getAvDeviceByIndex(0); |
---|
918 |
|
---|
919 |
bool slaveMode=false; |
---|
920 |
if(!getOption("slaveMode", slaveMode)) { |
---|
921 |
debugOutput(DEBUG_LEVEL_NORMAL, |
---|
922 |
"Could not retrieve slaveMode parameter, defauling to false\n"); |
---|
923 |
} |
---|
924 |
return device->getStreamProcessorByIndex(0); |
---|
925 |
} |
---|
926 |
|
---|
927 |
bool |
---|
928 |
DeviceManager::deinitialize() |
---|
929 |
{ |
---|
930 |
return true; |
---|
931 |
} |
---|
932 |
|
---|
933 |
|
---|
934 |
void |
---|
935 |
DeviceManager::setVerboseLevel(int l) |
---|
936 |
{ |
---|
937 |
setDebugLevel(l); |
---|
938 |
Control::Element::setVerboseLevel(l); |
---|
939 |
m_processorManager->setVerboseLevel(l); |
---|
940 |
m_deviceStringParser->setVerboseLevel(l); |
---|
941 |
for ( FFADODeviceVectorIterator it = m_avDevices.begin(); |
---|
942 |
it != m_avDevices.end(); |
---|
943 |
++it ) |
---|
944 |
{ |
---|
945 |
(*it)->setVerboseLevel(l); |
---|
946 |
} |
---|
947 |
for ( Ieee1394ServiceVectorIterator it = m_1394Services.begin(); |
---|
948 |
it != m_1394Services.end(); |
---|
949 |
++it ) |
---|
950 |
{ |
---|
951 |
(*it)->setVerboseLevel(l); |
---|
952 |
} |
---|
953 |
debugOutput( DEBUG_LEVEL_VERBOSE, "Setting verbose level to %d...\n", l ); |
---|
954 |
} |
---|
955 |
|
---|
956 |
void |
---|
957 |
DeviceManager::showDeviceInfo() { |
---|
958 |
debugOutput(DEBUG_LEVEL_NORMAL, "===== Device Manager =====\n"); |
---|
959 |
Control::Element::show(); |
---|
960 |
|
---|
961 |
int i=0; |
---|
962 |
for ( Ieee1394ServiceVectorIterator it = m_1394Services.begin(); |
---|
963 |
it != m_1394Services.end(); |
---|
964 |
++it ) |
---|
965 |
{ |
---|
966 |
debugOutput(DEBUG_LEVEL_NORMAL, "--- IEEE1394 Service %2d ---\n", i++); |
---|
967 |
(*it)->show(); |
---|
968 |
} |
---|
969 |
|
---|
970 |
i=0; |
---|
971 |
for ( FFADODeviceVectorIterator it = m_avDevices.begin(); |
---|
972 |
it != m_avDevices.end(); |
---|
973 |
++it ) |
---|
974 |
{ |
---|
975 |
FFADODevice* avDevice = *it; |
---|
976 |
debugOutput(DEBUG_LEVEL_NORMAL, "--- Device %2d ---\n", i++); |
---|
977 |
avDevice->showDevice(); |
---|
978 |
|
---|
979 |
debugOutput(DEBUG_LEVEL_NORMAL, "Clock sync sources:\n"); |
---|
980 |
FFADODevice::ClockSourceVector sources=avDevice->getSupportedClockSources(); |
---|
981 |
for ( FFADODevice::ClockSourceVector::const_iterator it |
---|
982 |
= sources.begin(); |
---|
983 |
it != sources.end(); |
---|
984 |
++it ) |
---|
985 |
{ |
---|
986 |
FFADODevice::ClockSource c=*it; |
---|
987 |
debugOutput(DEBUG_LEVEL_NORMAL, " Type: %s, Id: %2d, Valid: %1d, Active: %1d, Locked %1d, Slipping: %1d, Description: %s\n", |
---|
988 |
FFADODevice::ClockSourceTypeToString(c.type), c.id, c.valid, c.active, c.locked, c.slipping, c.description.c_str()); |
---|
989 |
} |
---|
990 |
} |
---|
991 |
} |
---|
992 |
void |
---|
993 |
DeviceManager::showStreamingInfo() { |
---|
994 |
m_processorManager->dumpInfo(); |
---|
995 |
} |
---|