root/trunk/libffado/src/libieee1394/IsoHandler.cpp

Revision 1172, 24.3 kB (checked in by ppalmers, 16 years ago)

lay down the foundations for easy ALSA/Pulse support

Line 
1 /*
2  * Copyright (C) 2005-2008 by Pieter Palmers
3  *
4  * This file is part of FFADO
5  * FFADO = Free Firewire (pro-)audio drivers for linux
6  *
7  * FFADO is based upon FreeBoB.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 2 of the License, or
12  * (at your option) version 3 of the License.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #include "config.h"
25
26 #include "IsoHandler.h"
27 #include "ieee1394service.h"
28 #include "IsoHandlerManager.h"
29
30 #include "cycletimer.h"
31
32 #include "libstreaming/generic/StreamProcessor.h"
33 #include "libutil/PosixThread.h"
34
35 #include <errno.h>
36 #include "libutil/ByteSwap.h"
37 #include <assert.h>
38 #include <unistd.h>
39 #include <string.h>
40
41 #include <iostream>
42 using namespace std;
43 using namespace Streaming;
44
45 IMPL_DEBUG_MODULE( IsoHandler, IsoHandler, DEBUG_LEVEL_NORMAL );
46
47 /* the C callbacks */
48 enum raw1394_iso_disposition
49 IsoHandler::iso_transmit_handler(raw1394handle_t handle,
50         unsigned char *data, unsigned int *length,
51         unsigned char *tag, unsigned char *sy,
52         int cycle, unsigned int dropped1) {
53
54     IsoHandler *xmitHandler = static_cast<IsoHandler *>(raw1394_get_userdata(handle));
55     assert(xmitHandler);
56     unsigned int skipped = (dropped1 & 0xFFFF0000) >> 16;
57     unsigned int dropped = dropped1 & 0xFFFF;
58     return xmitHandler->getPacket(data, length, tag, sy, cycle, dropped, skipped);
59 }
60
61 enum raw1394_iso_disposition
62 IsoHandler::iso_receive_handler(raw1394handle_t handle, unsigned char *data,
63                         unsigned int length, unsigned char channel,
64                         unsigned char tag, unsigned char sy, unsigned int cycle,
65                         unsigned int dropped1) {
66
67     IsoHandler *recvHandler = static_cast<IsoHandler *>(raw1394_get_userdata(handle));
68     assert(recvHandler);
69
70     unsigned int skipped = (dropped1 & 0xFFFF0000) >> 16;
71     unsigned int dropped = dropped1 & 0xFFFF;
72
73     return recvHandler->putPacket(data, length, channel, tag, sy, cycle, dropped, skipped);
74 }
75
76 int IsoHandler::busreset_handler(raw1394handle_t handle, unsigned int generation)
77 {
78     debugOutput( DEBUG_LEVEL_VERBOSE, "Busreset happened, generation %d...\n", generation);
79
80     IsoHandler *handler = static_cast<IsoHandler *>(raw1394_get_userdata(handle));
81     assert(handler);
82     return handler->handleBusReset(generation);
83 }
84
85 IsoHandler::IsoHandler(IsoHandlerManager& manager, enum EHandlerType t)
86    : m_manager( manager )
87    , m_type ( t )
88    , m_handle( 0 )
89    , m_buf_packets( 400 )
90    , m_max_packet_size( 1024 )
91    , m_irq_interval( -1 )
92    , m_last_cycle( -1 )
93    , m_last_now( 0xFFFFFFFF )
94    , m_Client( 0 )
95    , m_speed( RAW1394_ISO_SPEED_400 )
96    , m_prebuffers( 0 )
97    , m_dont_exit_iterate_loop( true )
98    , m_State( E_Created )
99 #ifdef DEBUG
100    , m_packets ( 0 )
101    , m_dropped( 0 )
102    , m_min_ahead( 7999 )
103 #endif
104 {
105 }
106
107 IsoHandler::IsoHandler(IsoHandlerManager& manager, enum EHandlerType t,
108                        unsigned int buf_packets, unsigned int max_packet_size, int irq)
109    : m_manager( manager )
110    , m_type ( t )
111    , m_handle( 0 )
112    , m_buf_packets( buf_packets )
113    , m_max_packet_size( max_packet_size )
114    , m_irq_interval( irq )
115    , m_last_cycle( -1 )
116    , m_last_now( 0xFFFFFFFF )
117    , m_Client( 0 )
118    , m_speed( RAW1394_ISO_SPEED_400 )
119    , m_prebuffers( 0 )
120    , m_State( E_Created )
121 #ifdef DEBUG
122    , m_packets ( 0 )
123    , m_dropped( 0 )
124    , m_min_ahead( 7999 )
125 #endif
126 {
127 }
128
129 IsoHandler::IsoHandler(IsoHandlerManager& manager, enum EHandlerType t, unsigned int buf_packets,
130                        unsigned int max_packet_size, int irq,
131                        enum raw1394_iso_speed speed)
132    : m_manager( manager )
133    , m_type ( t )
134    , m_handle( 0 )
135    , m_buf_packets( buf_packets )
136    , m_max_packet_size( max_packet_size )
137    , m_irq_interval( irq )
138    , m_last_cycle( -1 )
139    , m_last_now( 0xFFFFFFFF )
140    , m_Client( 0 )
141    , m_speed( speed )
142    , m_prebuffers( 0 )
143    , m_State( E_Created )
144 #ifdef DEBUG
145    , m_packets( 0 )
146    , m_dropped( 0 )
147 #endif
148 {
149 }
150
151 IsoHandler::~IsoHandler() {
152 // Don't call until libraw1394's raw1394_new_handle() function has been
153 // fixed to correctly initialise the iso_packet_infos field.  Bug is
154 // confirmed present in libraw1394 1.2.1.  In any case,
155 // raw1394_destroy_handle() will do any iso system shutdown required.
156 //     raw1394_iso_shutdown(m_handle);
157     if(m_handle) {
158         if (m_State == E_Running) {
159             disable();
160         }
161         raw1394_destroy_handle(m_handle);
162     }
163 }
164
165 bool
166 IsoHandler::canIterateClient()
167 {
168     debugOutputExtreme(DEBUG_LEVEL_VERY_VERBOSE, "checking...\n");
169     if(m_Client) {
170         bool result;
171         if (m_type == eHT_Receive) {
172             result = m_Client->canProducePacket();
173         } else {
174             result = m_Client->canConsumePacket();
175         }
176         debugOutputExtreme(DEBUG_LEVEL_VERY_VERBOSE, " returns %d\n", result);
177         return result;
178     } else {
179         debugOutputExtreme(DEBUG_LEVEL_VERY_VERBOSE, " no client\n");
180     }
181     return false;
182 }
183
184 bool
185 IsoHandler::iterate() {
186     debugOutputExtreme(DEBUG_LEVEL_VERY_VERBOSE, "(%p, %s) Iterating ISO handler...\n",
187                 this, getTypeString());
188     if(m_State == E_Running) {
189 #if ISOHANDLER_FLUSH_BEFORE_ITERATE
190         flush();
191 #endif
192         m_last_now = m_manager.get1394Service().getCycleTimer();
193         if(raw1394_loop_iterate(m_handle)) {
194             debugError( "IsoHandler (%p): Failed to iterate handler: %s\n",
195                         this, strerror(errno));
196             return false;
197         }
198         debugOutputExtreme(DEBUG_LEVEL_VERY_VERBOSE, "(%p, %s) done interating ISO handler...\n",
199                            this, getTypeString());
200         return true;
201     } else {
202         debugOutput(DEBUG_LEVEL_VERBOSE, "(%p, %s) Not iterating a non-running handler...\n",
203                     this, getTypeString());
204         return false;
205     }
206 }
207
208 bool
209 IsoHandler::init()
210 {
211     debugOutput( DEBUG_LEVEL_VERBOSE, "IsoHandler (%p) enter...\n",this);
212     // check the state
213     if(m_State != E_Created) {
214         debugError("Incorrect state, expected E_Created, got %d\n",(int)m_State);
215         return false;
216     }
217
218     // the main handle for the ISO traffic
219     m_handle = raw1394_new_handle_on_port( m_manager.get1394Service().getPort() );
220     if ( !m_handle ) {
221         if ( !errno ) {
222             debugError("libraw1394 not compatible\n");
223         } else {
224             debugError("Could not get 1394 handle: %s\n", strerror(errno) );
225             debugError("Are ieee1394 and raw1394 drivers loaded?\n");
226         }
227         return false;
228     }
229     raw1394_set_userdata(m_handle, static_cast<void *>(this));
230
231     // bus reset handling
232     if(raw1394_busreset_notify (m_handle, RAW1394_NOTIFY_ON)) {
233         debugWarning("Could not enable busreset notification.\n");
234         debugWarning(" Error message: %s\n",strerror(errno));
235         debugWarning("Continuing without bus reset support.\n");
236     } else {
237         // apparently this cannot fail
238         raw1394_set_bus_reset_handler(m_handle, busreset_handler);
239     }
240
241     // update the internal state
242     m_State=E_Initialized;
243     return true;
244 }
245
246 bool IsoHandler::disable()
247 {
248     debugOutput( DEBUG_LEVEL_VERBOSE, "(%p, %s) enter...\n",
249                  this, (m_type==eHT_Receive?"Receive":"Transmit"));
250
251     // check state
252     if(m_State == E_Prepared) return true;
253     if(m_State != E_Running) {
254         debugError("Incorrect state, expected E_Running, got %d\n",(int)m_State);
255         return false;
256     }
257
258     // this is put here to try and avoid the
259     // Runaway context problem
260     // don't know if it will help though.
261     raw1394_iso_xmit_sync(m_handle);
262     raw1394_iso_stop(m_handle);
263     m_State = E_Prepared;
264     return true;
265 }
266
267 /**
268  * Bus reset handler
269  *
270  * @return ?
271  */
272
273 int
274 IsoHandler::handleBusReset(unsigned int generation)
275 {
276     debugOutput( DEBUG_LEVEL_VERBOSE, "bus reset...\n");
277
278     #define CSR_CYCLE_TIME            0x200
279     #define CSR_REGISTER_BASE  0xfffff0000000ULL
280     // do a simple read on ourself in order to update the internal structures
281     // this avoids read failures after a bus reset
282     quadlet_t buf=0;
283     raw1394_read(m_handle, raw1394_get_local_id(m_handle),
284                  CSR_REGISTER_BASE | CSR_CYCLE_TIME, 4, &buf);
285
286     // notify the client of the fact that we have died
287     m_Client->handlerDied();
288
289     if(!disable()) {
290         debugError("(%p) Could not disable IsoHandler\n", this);
291     }
292
293     // request the manager to update it's shadow map
294     m_manager.requestShadowMapUpdate();
295     return 0;
296 }
297
298 void IsoHandler::dumpInfo()
299 {
300     int channel=-1;
301     if (m_Client) channel=m_Client->getChannel();
302
303     debugOutputShort( DEBUG_LEVEL_NORMAL, "  Handler type................: %s\n",
304             getTypeString());
305     debugOutputShort( DEBUG_LEVEL_NORMAL, "  Port, Channel...............: %2d, %2d\n",
306             m_manager.get1394Service().getPort(), channel);
307     debugOutputShort( DEBUG_LEVEL_NORMAL, "  Buffer, MaxPacketSize, IRQ..: %4d, %4d, %4d\n",
308             m_buf_packets, m_max_packet_size, m_irq_interval);
309     if (this->getType() == eHT_Transmit) {
310         debugOutputShort( DEBUG_LEVEL_NORMAL, "  Speed, PreBuffers...........: %2d, %2d\n",
311                                             m_speed, m_prebuffers);
312         #ifdef DEBUG
313         debugOutputShort( DEBUG_LEVEL_NORMAL, "  Min ISOXMT bufferfill : %04d\n", m_min_ahead);
314         #endif
315     }
316     #ifdef DEBUG
317     debugOutputShort( DEBUG_LEVEL_NORMAL, "  Last cycle, dropped.........: %4d, %4u\n",
318             m_last_cycle, m_dropped);
319     #endif
320
321 }
322
323 void IsoHandler::setVerboseLevel(int l)
324 {
325     setDebugLevel(l);
326 }
327
328 bool IsoHandler::registerStream(StreamProcessor *stream)
329 {
330     assert(stream);
331     debugOutput( DEBUG_LEVEL_VERBOSE, "registering stream (%p)\n", stream);
332
333     if (m_Client) {
334             debugFatal( "Generic IsoHandlers can have only one client\n");
335             return false;
336     }
337     m_Client=stream;
338     return true;
339 }
340
341 bool IsoHandler::unregisterStream(StreamProcessor *stream)
342 {
343     assert(stream);
344     debugOutput( DEBUG_LEVEL_VERBOSE, "unregistering stream (%p)\n", stream);
345
346     if(stream != m_Client) {
347             debugFatal( "no client registered\n");
348             return false;
349     }
350     m_Client=0;
351     return true;
352 }
353
354 void IsoHandler::flush()
355 {
356     if(m_type == eHT_Receive) {
357         raw1394_iso_recv_flush(m_handle);
358     } else {
359         // do nothing
360     }
361 }
362
363 // ISO packet interface
364 enum raw1394_iso_disposition IsoHandler::putPacket(
365                     unsigned char *data, unsigned int length,
366                     unsigned char channel, unsigned char tag, unsigned char sy,
367                     unsigned int cycle, unsigned int dropped, unsigned int skipped) {
368
369     // keep track of dropped cycles
370     int dropped_cycles = 0;
371     if (m_last_cycle != (int)cycle && m_last_cycle != -1) {
372         dropped_cycles = diffCycles(cycle, m_last_cycle) - 1;
373         #ifdef DEBUG
374         if (dropped_cycles < 0) {
375             debugWarning("(%p) dropped < 1 (%d), cycle: %d, last_cycle: %d, dropped: %d, 'skipped'=%u\n",
376                          this, dropped_cycles, cycle, m_last_cycle, dropped, skipped);
377         }
378         if (dropped_cycles > 0) {
379             debugOutput(DEBUG_LEVEL_NORMAL,
380                         "(%p) dropped %d packets on cycle %u, 'dropped'=%u, 'skipped'=%u, cycle=%d, m_last_cycle=%d\n",
381                         this, dropped_cycles, cycle, dropped, skipped, cycle, m_last_cycle);
382             m_dropped += dropped_cycles;
383         }
384         #endif
385     }
386     m_last_cycle = cycle;
387
388     uint32_t pkt_ctr = cycle << 12;
389
390     // if we assume that one iterate() loop doesn't take longer than 0.5 seconds,
391     // the seconds field won't change while the iterate loop runs
392     // this means that we can preset 'now' before running iterate()
393     uint32_t now_secs = CYCLE_TIMER_GET_SECS(m_last_now);
394     // causality results in the fact that 'now' is always after 'cycle'
395     // except if additional packets are received between setting the
396     // m_last_now and the starting the iterate() loop.
397     // this causes the m_last_now to be set at a time before the last packet
398     // in this loop is received. however, it's not going to be >4000 cycles.
399     // hence:
400     // - if the m_last_now > cycle, there is no need to unwrap
401     //   both values are within the same second
402     // - if m_last_now < cycle it can mean two things:
403     //    * m_last_now has wrapped, but is still later than cycle
404     //      hence diffCycles(m_last_now, cycle) > 0. We should unwrap
405     //    * m_last_now has not wrapped, and cycle is ahead of m_last_now
406     //      this means that the cycle is more recent than the saved
407     //      m_last_now value
408     // . Hence if we calculate
409     // the unwrapped difference, and it's larger than 0, this means
410     // that m_last_now is after the current cycle. .
411     // it m_last_now is before the current cycle, we should not unwrap
412     // NOTE: another option is to reread the m_last_now
413     if( (CYCLE_TIMER_GET_CYCLES(m_last_now) < cycle)
414         && diffCycles(CYCLE_TIMER_GET_CYCLES(m_last_now), cycle) >= 0) {
415         debugOutputExtreme(DEBUG_LEVEL_VERBOSE,
416                            "unwrapping %d => %d, %d\n",
417                            CYCLE_TIMER_GET_CYCLES(m_last_now),
418                            cycle);
419         // the cycle field has wrapped, substract one second
420         if(now_secs == 0) {
421             now_secs = 127;
422         } else  {
423             now_secs -= 1;
424         }
425     }
426
427     #ifdef DEBUG
428     if( (CYCLE_TIMER_GET_CYCLES(m_last_now) < cycle)
429         && diffCycles(CYCLE_TIMER_GET_CYCLES(m_last_now), cycle) < 0
430         // ignore this on dropped cycles, since it's normal
431         // that now is ahead on the received packets (as we miss packets)
432         && dropped_cycles == 0)
433     {
434         debugOutput(DEBUG_LEVEL_VERBOSE, "Special non-unwrapping happened\n");
435     }
436     #endif
437     pkt_ctr |= (now_secs & 0x7F) << 25;
438
439     #if ISOHANDLER_CHECK_CTR_RECONSTRUCTION
440     // add a seconds field
441     uint32_t now = m_manager.get1394Service().getCycleTimer();
442     uint32_t now_secs_ref = CYCLE_TIMER_GET_SECS(now);
443     // causality results in the fact that 'now' is always after 'cycle'
444     // or at best, equal (if this handler was called within 125us after
445     // the packet was on the wire).
446     if(CYCLE_TIMER_GET_CYCLES(now) < cycle) {
447         // the cycle field has wrapped, substract one second
448         if(now_secs_ref == 0) {
449             now_secs_ref = 127;
450         } else  {
451             now_secs_ref -= 1;
452         }
453     }
454     uint32_t pkt_ctr_ref = cycle << 12;
455     pkt_ctr_ref |= (now_secs_ref & 0x7F) << 25;
456
457     if(pkt_ctr != pkt_ctr_ref) {
458         debugWarning("reconstructed CTR counter discrepancy\n");
459         debugWarning(" ingredients: %X, %lX, %lX, %lX, %lX, %ld, %ld\n",
460                        cycle, pkt_ctr_ref, pkt_ctr, now, m_last_now, now_secs_ref, now_secs);
461     }
462     #endif
463
464     // leave the offset field (for now?)
465
466     debugOutputExtreme(DEBUG_LEVEL_ULTRA_VERBOSE,
467                        "received packet: length=%d, channel=%d, cycle=%d\n",
468                        length, channel, cycle);
469     #ifdef DEBUG
470     m_packets++;
471     if (length > m_max_packet_size) {
472         debugWarning("(%p, %s) packet too large: len=%u max=%u\n",
473                      this, getTypeString(), length, m_max_packet_size);
474     }
475     if(m_last_cycle == -1) {
476         debugOutput(DEBUG_LEVEL_VERBOSE, "Handler for %s SP %p is alive (cycle = %u)\n", getTypeString(), this, cycle);
477     }
478     #endif
479
480     // iterate the client if required
481     if(m_Client) {
482         enum raw1394_iso_disposition retval = m_Client->putPacket(data, length, channel, tag, sy, pkt_ctr, dropped_cycles, skipped);
483         if (retval == RAW1394_ISO_OK) {
484             if (m_dont_exit_iterate_loop) {
485                 return RAW1394_ISO_OK;
486             } else {
487                 m_dont_exit_iterate_loop = true;
488                 debugOutput(DEBUG_LEVEL_VERBOSE,
489                                 "(%p) loop exit requested\n",
490                                 this);
491                 return RAW1394_ISO_DEFER;
492             }
493         } else {
494             return retval;
495         }
496     }
497
498     return RAW1394_ISO_OK;
499 }
500
501 enum raw1394_iso_disposition
502 IsoHandler::getPacket(unsigned char *data, unsigned int *length,
503                       unsigned char *tag, unsigned char *sy,
504                       int cycle, unsigned int dropped, unsigned int skipped) {
505
506     uint32_t pkt_ctr;
507     if (cycle < 0) {
508         // mark invalid
509         pkt_ctr = 0xFFFFFFFF;
510     } else {
511         pkt_ctr = cycle << 12;
512
513 #if 0 // we don't need this for xmit
514         // if we assume that one iterate() loop doesn't take longer than 0.5 seconds,
515         // the seconds field won't change while the iterate loop runs
516         // this means that we can preset 'now' before running iterate()
517         uint32_t now_secs = CYCLE_TIMER_GET_SECS(m_last_now);
518         // causality results in the fact that 'now' is always after 'cycle'
519         if(CYCLE_TIMER_GET_CYCLES(m_last_now) > (unsigned int)cycle) {
520             // the cycle field has wrapped, add one second
521             now_secs += 1;
522             // no need for this:
523             //if(now_secs == 128) {
524             //    now_secs = 0;
525             //}
526             // since we mask later on
527         }
528         pkt_ctr |= (now_secs & 0x7F) << 25;
529
530         #if ISOHANDLER_CHECK_CTR_RECONSTRUCTION
531         // add a seconds field
532         uint32_t now = m_manager.get1394Service().getCycleTimer();
533         uint32_t now_secs_ref = CYCLE_TIMER_GET_SECS(now);
534         // causality results in the fact that 'now' is always after 'cycle'
535         if(CYCLE_TIMER_GET_CYCLES(now) > (unsigned int)cycle) {
536             // the cycle field has wrapped, add one second
537             now_secs_ref += 1;
538             // no need for this:
539             //if(now_secs == 128) {
540             //    now_secs = 0;
541             //}
542             // since we mask later on
543         }
544         uint32_t pkt_ctr_ref = cycle << 12;
545         pkt_ctr_ref |= (now_secs_ref & 0x7F) << 25;
546    
547         if(pkt_ctr != pkt_ctr_ref) {
548             debugWarning("reconstructed CTR counter discrepancy\n");
549             pkt_ctr=pkt_ctr_ref;
550         }
551         #endif
552 #endif
553     }
554
555     debugOutputExtreme(DEBUG_LEVEL_ULTRA_VERBOSE,
556                        "sending packet: length=%d, cycle=%d\n",
557                        *length, cycle);
558
559     #ifdef DEBUG
560     m_packets++;
561     if(m_last_cycle == -1) {
562         debugOutput(DEBUG_LEVEL_VERBOSE, "Handler for %s SP %p is alive (cycle = %d)\n", getTypeString(), this, cycle);
563     }
564     #endif
565
566     // keep track of dropped cycles
567     int dropped_cycles = 0;
568     if (m_last_cycle != cycle && m_last_cycle != -1) {
569         dropped_cycles = diffCycles(cycle, m_last_cycle) - 1;
570         // correct for skipped packets
571         // since those are not dropped, but only delayed
572         dropped_cycles -= skipped;
573
574         #ifdef DEBUG
575         if(skipped) {
576             debugOutput(DEBUG_LEVEL_NORMAL,
577                         "(%p) skipped %d cycles, cycle: %d, last_cycle: %d, dropped: %d\n",
578                         this, skipped, cycle, m_last_cycle, dropped);
579         }
580         if (dropped_cycles < 0) {
581             debugWarning("(%p) dropped < 1 (%d), cycle: %d, last_cycle: %d, dropped: %d, skipped: %d\n",
582                          this, dropped_cycles, cycle, m_last_cycle, dropped, skipped);
583         }
584         if (dropped_cycles > 0) {
585             debugOutput(DEBUG_LEVEL_NORMAL,
586                         "(%p) dropped %d packets on cycle %u (last_cycle=%u, dropped=%d, skipped: %d)\n",
587                         this, dropped_cycles, cycle, m_last_cycle, dropped, skipped);
588             m_dropped += dropped_cycles - skipped;
589         }
590         #endif
591     }
592     if (cycle >= 0) {
593         m_last_cycle = cycle;
594        
595         #ifdef DEBUG
596 /*        int ahead = diffCycles(cycle, now_cycles);
597         if (ahead < m_min_ahead) m_min_ahead = ahead;
598 */
599         #endif
600     }
601
602     if(m_Client) {
603         enum raw1394_iso_disposition retval;
604         retval = m_Client->getPacket(data, length, tag, sy, pkt_ctr, dropped, skipped, m_max_packet_size);
605         #ifdef DEBUG
606         if (*length > m_max_packet_size) {
607             debugWarning("(%p, %s) packet too large: len=%u max=%u\n",
608                          this, getTypeString(), *length, m_max_packet_size);
609         }
610         #endif
611         if (retval == RAW1394_ISO_OK) {
612             if (m_dont_exit_iterate_loop) {
613                 return RAW1394_ISO_OK;
614             } else {
615                 m_dont_exit_iterate_loop = true;
616                 debugOutput(DEBUG_LEVEL_VERBOSE,
617                                 "(%p) loop exit requested\n",
618                                 this);
619                 return RAW1394_ISO_DEFER;
620             }
621         } else {
622             return retval;
623         }
624     }
625
626     *tag = 0;
627     *sy = 0;
628     *length = 0;
629     return RAW1394_ISO_OK;
630 }
631
632 bool IsoHandler::prepare()
633 {
634     // check the state
635     if(m_State != E_Initialized) {
636         debugError("Incorrect state, expected E_Initialized, got %d\n",(int)m_State);
637         return false;
638     }
639
640     // Don't call until libraw1394's raw1394_new_handle() function has been
641     // fixed to correctly initialise the iso_packet_infos field.  Bug is
642     // confirmed present in libraw1394 1.2.1.
643     //     raw1394_iso_shutdown(m_handle);
644     m_State = E_Prepared;
645
646     debugOutput( DEBUG_LEVEL_VERBOSE, "Preparing iso handler (%p, client=%p)\n", this, m_Client);
647     dumpInfo();
648     if (getType() == eHT_Receive) {
649         if(m_irq_interval > 1) {
650             if(raw1394_iso_recv_init(m_handle,
651                                     iso_receive_handler,
652                                     m_buf_packets,
653                                     m_max_packet_size,
654                                     m_Client->getChannel(),
655                                     RAW1394_DMA_BUFFERFILL,
656 //                                     RAW1394_DMA_PACKET_PER_BUFFER,
657                                     m_irq_interval)) {
658                 debugFatal("Could not do receive initialisation (DMA_BUFFERFILL)!\n" );
659                 debugFatal("  %s\n",strerror(errno));
660                 return false;
661             }
662         } else {
663             if(raw1394_iso_recv_init(m_handle,
664                                     iso_receive_handler,
665                                     m_buf_packets,
666                                     m_max_packet_size,
667                                     m_Client->getChannel(),
668                                     RAW1394_DMA_PACKET_PER_BUFFER,
669                                     m_irq_interval)) {
670                 debugFatal("Could not do receive initialisation (PACKET_PER_BUFFER)!\n" );
671                 debugFatal("  %s\n",strerror(errno));
672                 return false;
673             }
674         }
675         return true;
676     } else {
677         if(raw1394_iso_xmit_init(m_handle,
678                                 iso_transmit_handler,
679                                 m_buf_packets,
680                                 m_max_packet_size,
681                                 m_Client->getChannel(),
682                                 m_speed,
683                                 m_irq_interval)) {
684             debugFatal("Could not do xmit initialisation!\n" );
685             return false;
686         }
687         return true;
688     }
689 }
690
691 bool IsoHandler::enable(int cycle)
692 {
693     debugOutput( DEBUG_LEVEL_VERBOSE, "start on cycle %d\n", cycle);
694     // check the state
695     if(m_State != E_Prepared) {
696         if(!prepare()) {
697             debugFatal("Could not prepare handler\n");
698             return false;
699         }
700     }
701
702     if (getType() == eHT_Receive) {
703         if(raw1394_iso_recv_start(m_handle, cycle, -1, 0)) {
704             debugFatal("Could not start receive handler (%s)\n",strerror(errno));
705             dumpInfo();
706             return false;
707         }
708     } else {
709         if(raw1394_iso_xmit_start(m_handle, cycle, m_prebuffers)) {
710             debugFatal("Could not start xmit handler (%s)\n",strerror(errno));
711             dumpInfo();
712             return false;
713         }
714     }
715
716 #ifdef DEBUG
717     m_min_ahead = 7999;
718 #endif
719     m_State = E_Running;
720     return true;
721 }
722
723 /**
724  * @brief convert a EHandlerType to a string
725  * @param t the type
726  * @return a char * describing the state
727  */
728 const char *
729 IsoHandler::eHTToString(enum EHandlerType t) {
730     switch (t) {
731         case eHT_Receive: return "Receive";
732         case eHT_Transmit: return "Transmit";
733         default: return "error: unknown type";
734     }
735 }
Note: See TracBrowser for help on using the browser.