root/branches/libffado-2.0/src/libieee1394/IsoHandler.cpp

Revision 1462, 24.2 kB (checked in by ppalmers, 15 years ago)

fix subtle wraparound bug in cycle timer reconstruction code

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 dropped) {
66
67     IsoHandler *recvHandler = static_cast<IsoHandler *>(raw1394_get_userdata(handle));
68     assert(recvHandler);
69
70     return recvHandler->putPacket(data, length, channel, tag, sy, cycle, dropped);
71 }
72
73 IsoHandler::IsoHandler(IsoHandlerManager& manager, enum EHandlerType t)
74    : m_manager( manager )
75    , m_type ( t )
76    , m_handle( 0 )
77    , m_buf_packets( 400 )
78    , m_max_packet_size( 1024 )
79    , m_irq_interval( -1 )
80    , m_last_cycle( -1 )
81    , m_last_now( 0xFFFFFFFF )
82    , m_last_packet_handled_at( 0xFFFFFFFF )
83    , m_receive_mode ( RAW1394_DMA_PACKET_PER_BUFFER )
84    , m_Client( 0 )
85    , m_speed( RAW1394_ISO_SPEED_400 )
86    , m_prebuffers( 0 )
87    , m_dont_exit_iterate_loop( true )
88    , m_State( E_Created )
89 #ifdef DEBUG
90    , m_packets ( 0 )
91    , m_dropped( 0 )
92    , m_skipped( 0 )
93    , m_min_ahead( 7999 )
94 #endif
95 {
96 }
97
98 IsoHandler::IsoHandler(IsoHandlerManager& manager, enum EHandlerType t,
99                        unsigned int buf_packets, unsigned int max_packet_size, int irq)
100    : m_manager( manager )
101    , m_type ( t )
102    , m_handle( 0 )
103    , m_buf_packets( buf_packets )
104    , m_max_packet_size( max_packet_size )
105    , m_irq_interval( irq )
106    , m_last_cycle( -1 )
107    , m_last_now( 0xFFFFFFFF )
108    , m_last_packet_handled_at( 0xFFFFFFFF )
109    , m_receive_mode ( RAW1394_DMA_PACKET_PER_BUFFER )
110    , m_Client( 0 )
111    , m_speed( RAW1394_ISO_SPEED_400 )
112    , m_prebuffers( 0 )
113    , m_State( E_Created )
114 #ifdef DEBUG
115    , m_packets ( 0 )
116    , m_dropped( 0 )
117    , m_skipped( 0 )
118    , m_min_ahead( 7999 )
119 #endif
120 {
121 }
122
123 IsoHandler::IsoHandler(IsoHandlerManager& manager, enum EHandlerType t, unsigned int buf_packets,
124                        unsigned int max_packet_size, int irq,
125                        enum raw1394_iso_speed speed)
126    : m_manager( manager )
127    , m_type ( t )
128    , m_handle( 0 )
129    , m_buf_packets( buf_packets )
130    , m_max_packet_size( max_packet_size )
131    , m_irq_interval( irq )
132    , m_last_cycle( -1 )
133    , m_last_now( 0xFFFFFFFF )
134    , m_last_packet_handled_at( 0xFFFFFFFF )
135    , m_receive_mode ( RAW1394_DMA_PACKET_PER_BUFFER )
136    , m_Client( 0 )
137    , m_speed( speed )
138    , m_prebuffers( 0 )
139    , m_State( E_Created )
140 #ifdef DEBUG
141    , m_packets( 0 )
142    , m_dropped( 0 )
143    , m_skipped( 0 )
144    , m_min_ahead( 7999 )
145 #endif
146 {
147 }
148
149 IsoHandler::~IsoHandler() {
150 // Don't call until libraw1394's raw1394_new_handle() function has been
151 // fixed to correctly initialise the iso_packet_infos field.  Bug is
152 // confirmed present in libraw1394 1.2.1.  In any case,
153 // raw1394_destroy_handle() will do any iso system shutdown required.
154 //     raw1394_iso_shutdown(m_handle);
155     if(m_handle) {
156         if (m_State == E_Running) {
157             disable();
158         }
159         raw1394_destroy_handle(m_handle);
160     }
161 }
162
163 bool
164 IsoHandler::canIterateClient()
165 {
166     debugOutputExtreme(DEBUG_LEVEL_VERY_VERBOSE, "checking...\n");
167     if(m_Client) {
168         bool result;
169
170         if (m_type == eHT_Receive) {
171             result = m_Client->canProducePacket();
172         } else {
173             result = m_Client->canConsumePacket();
174         }
175         debugOutputExtreme(DEBUG_LEVEL_VERY_VERBOSE, " returns %d\n", result);
176         return result && (m_State != E_Error);
177     } else {
178         debugOutputExtreme(DEBUG_LEVEL_VERY_VERBOSE, " no client\n");
179     }
180     return false;
181 }
182
183 bool
184 IsoHandler::iterate() {
185     return iterate(m_manager.get1394Service().getCycleTimer());
186 }
187
188 bool
189 IsoHandler::iterate(uint32_t cycle_timer_now) {
190     debugOutputExtreme(DEBUG_LEVEL_VERY_VERBOSE, "(%p, %s) Iterating ISO handler at %08X...\n",
191                        this, getTypeString(), cycle_timer_now);
192     m_last_now = cycle_timer_now;
193     if(m_State == E_Running) {
194 #if ISOHANDLER_FLUSH_BEFORE_ITERATE
195         flush();
196 #endif
197         if(raw1394_loop_iterate(m_handle)) {
198             debugError( "IsoHandler (%p): Failed to iterate handler: %s\n",
199                         this, strerror(errno));
200             return false;
201         }
202         debugOutputExtreme(DEBUG_LEVEL_VERY_VERBOSE, "(%p, %s) done interating ISO handler...\n",
203                            this, getTypeString());
204         return true;
205     } else {
206         debugOutput(DEBUG_LEVEL_VERBOSE, "(%p, %s) Not iterating a non-running handler...\n",
207                     this, getTypeString());
208         return false;
209     }
210 }
211
212 bool
213 IsoHandler::init()
214 {
215     debugOutput( DEBUG_LEVEL_VERBOSE, "IsoHandler (%p) enter...\n",this);
216     // check the state
217     if(m_State != E_Created) {
218         debugError("Incorrect state, expected E_Created, got %d\n",(int)m_State);
219         return false;
220     }
221
222     // the main handle for the ISO traffic
223     m_handle = raw1394_new_handle_on_port( m_manager.get1394Service().getPort() );
224     if ( !m_handle ) {
225         if ( !errno ) {
226             debugError("libraw1394 not compatible\n");
227         } else {
228             debugError("Could not get 1394 handle: %s\n", strerror(errno) );
229             debugError("Are ieee1394 and raw1394 drivers loaded?\n");
230         }
231         return false;
232     }
233     raw1394_set_userdata(m_handle, static_cast<void *>(this));
234
235     // update the internal state
236     m_State=E_Initialized;
237     return true;
238 }
239
240 bool IsoHandler::disable()
241 {
242     debugOutput( DEBUG_LEVEL_VERBOSE, "(%p, %s) enter...\n",
243                  this, (m_type==eHT_Receive?"Receive":"Transmit"));
244
245     // check state
246     if(m_State == E_Prepared) return true;
247     if(m_State != E_Running) {
248         debugError("Incorrect state, expected E_Running, got %d\n",(int)m_State);
249         return false;
250     }
251
252     debugOutput( DEBUG_LEVEL_VERBOSE, "(%p, %s) wake up handle...\n",
253                  this, (m_type==eHT_Receive?"Receive":"Transmit"));
254
255     // wake up any waiting reads/polls
256     raw1394_wake_up(m_handle);
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 /*    if(m_State != E_Error) { // if the handler is dead, this might block forever
262         raw1394_iso_xmit_sync(m_handle);
263     }*/
264     debugOutput( DEBUG_LEVEL_VERBOSE, "(%p, %s) stop...\n",
265                  this, (m_type==eHT_Receive?"Receive":"Transmit"));
266     raw1394_iso_stop(m_handle);
267     m_State = E_Prepared;
268     return true;
269 }
270
271 /**
272  * Bus reset handler
273  *
274  * @return ?
275  */
276
277 bool
278 IsoHandler::handleBusReset()
279 {
280     debugOutput( DEBUG_LEVEL_NORMAL, "bus reset...\n");
281     m_last_packet_handled_at = 0xFFFFFFFF;
282
283     #define CSR_CYCLE_TIME            0x200
284     #define CSR_REGISTER_BASE  0xfffff0000000ULL
285     // do a simple read on ourself in order to update the internal structures
286     // this avoids read failures after a bus reset
287     quadlet_t buf=0;
288     raw1394_read(m_handle, raw1394_get_local_id(m_handle),
289                  CSR_REGISTER_BASE | CSR_CYCLE_TIME, 4, &buf);
290
291     return m_Client->handleBusReset();
292 }
293
294 /**
295  * Call this if you find out that this handler has died for some
296  * external reason.
297  */
298 void
299 IsoHandler::notifyOfDeath()
300 {
301     m_State = E_Error;
302
303     // notify the client of the fact that we have died
304     m_Client->handlerDied();
305
306     // wake ourselves up
307     raw1394_wake_up(m_handle);
308 }
309
310 void IsoHandler::dumpInfo()
311 {
312     int channel=-1;
313     if (m_Client) channel=m_Client->getChannel();
314
315     debugOutputShort( DEBUG_LEVEL_NORMAL, "  Handler type................: %s\n",
316             getTypeString());
317     debugOutputShort( DEBUG_LEVEL_NORMAL, "  Port, Channel...............: %2d, %2d\n",
318             m_manager.get1394Service().getPort(), channel);
319     debugOutputShort( DEBUG_LEVEL_NORMAL, "  Buffer, MaxPacketSize, IRQ..: %4d, %4d, %4d\n",
320             m_buf_packets, m_max_packet_size, m_irq_interval);
321     if (this->getType() == eHT_Transmit) {
322         debugOutputShort( DEBUG_LEVEL_NORMAL, "  Speed, PreBuffers...........: %2d, %2d\n",
323                                             m_speed, m_prebuffers);
324         #ifdef DEBUG
325         debugOutputShort( DEBUG_LEVEL_NORMAL, "  Min ISOXMT bufferfill : %04d\n", m_min_ahead);
326         #endif
327     }
328     #ifdef DEBUG
329     debugOutputShort( DEBUG_LEVEL_NORMAL, "  Last cycle, dropped.........: %4d, %4u, %4u\n",
330             m_last_cycle, m_dropped, m_skipped);
331     #endif
332
333 }
334
335 void IsoHandler::setVerboseLevel(int l)
336 {
337     setDebugLevel(l);
338     debugOutput( DEBUG_LEVEL_VERBOSE, "Setting verbose level to %d...\n", l );
339 }
340
341 bool IsoHandler::registerStream(StreamProcessor *stream)
342 {
343     assert(stream);
344     debugOutput( DEBUG_LEVEL_VERBOSE, "registering stream (%p)\n", stream);
345
346     if (m_Client) {
347             debugFatal( "Generic IsoHandlers can have only one client\n");
348             return false;
349     }
350     m_Client=stream;
351     return true;
352 }
353
354 bool IsoHandler::unregisterStream(StreamProcessor *stream)
355 {
356     assert(stream);
357     debugOutput( DEBUG_LEVEL_VERBOSE, "unregistering stream (%p)\n", stream);
358
359     if(stream != m_Client) {
360             debugFatal( "no client registered\n");
361             return false;
362     }
363     m_Client=0;
364     return true;
365 }
366
367 void IsoHandler::flush()
368 {
369     if(m_type == eHT_Receive) {
370         raw1394_iso_recv_flush(m_handle);
371     } else {
372         // do nothing
373     }
374 }
375
376 // ISO packet interface
377 enum raw1394_iso_disposition IsoHandler::putPacket(
378                     unsigned char *data, unsigned int length,
379                     unsigned char channel, unsigned char tag, unsigned char sy,
380                     unsigned int cycle, unsigned int dropped) {
381     // keep track of dropped cycles
382     int dropped_cycles = 0;
383     if (m_last_cycle != (int)cycle && m_last_cycle != -1) {
384         dropped_cycles = diffCycles(cycle, m_last_cycle) - 1;
385         #ifdef DEBUG
386         if (dropped_cycles < 0) {
387             debugWarning("(%p) dropped < 1 (%d), cycle: %d, last_cycle: %d, dropped: %d\n",
388                          this, dropped_cycles, cycle, m_last_cycle, dropped);
389         }
390         if (dropped_cycles > 0) {
391             debugOutput(DEBUG_LEVEL_VERBOSE,
392                         "(%p) dropped %d packets on cycle %u, 'dropped'=%u, cycle=%d, m_last_cycle=%d\n",
393                         this, dropped_cycles, cycle, dropped, cycle, m_last_cycle);
394             m_dropped += dropped_cycles;
395         }
396         #endif
397     }
398     m_last_cycle = cycle;
399
400     // the m_last_now value is set when the iterate() function is called.
401     uint32_t now_cycles = CYCLE_TIMER_GET_CYCLES(m_last_now);
402
403     // two cases can occur:
404     // (1) this packet has been received before iterate() was called (normal case).
405     // (2) this packet has been received after iterate() was called.
406     //     happens when the kernel flushes more packets while we are already processing.
407     //
408     // In case (1) now_cycles is a small number of cycles larger than cycle. In
409     // case (2) now_cycles is a small number of cycles smaller than cycle.
410     // hence  abs(diffCycles(now_cycles, cycles)) has to be 'small'
411
412     // we can calculate the time of arrival for this packet as
413     // 'now' + diffCycles(cycles, now_cycles) * TICKS_PER_CYCLE
414     // in its properly wrapped version
415     int64_t diff_cycles = diffCycles(cycle, now_cycles);
416     int64_t tmp = CYCLE_TIMER_TO_TICKS(m_last_now);
417     tmp += diff_cycles * (int64_t)TICKS_PER_CYCLE;
418     uint64_t pkt_ctr_ticks = wrapAtMinMaxTicks(tmp);
419     uint32_t pkt_ctr = TICKS_TO_CYCLE_TIMER(pkt_ctr_ticks);
420     #ifdef DEBUG
421     if( (now_cycles < cycle)
422         && diffCycles(now_cycles, cycle) < 0
423         // ignore this on dropped cycles, since it's normal
424         // that now is ahead on the received packets (as we miss packets)
425         && dropped_cycles == 0)
426     {
427         debugOutput(DEBUG_LEVEL_VERY_VERBOSE, "Special non-unwrapping happened\n");
428     }
429     #endif
430
431     #if ISOHANDLER_CHECK_CTR_RECONSTRUCTION
432     // add a seconds field
433     uint32_t now = m_manager.get1394Service().getCycleTimer();
434     uint32_t now_secs_ref = CYCLE_TIMER_GET_SECS(now);
435     // causality results in the fact that 'now' is always after 'cycle'
436     // or at best, equal (if this handler was called within 125us after
437     // the packet was on the wire).
438     if(CYCLE_TIMER_GET_CYCLES(now) < cycle) {
439         // the cycle field has wrapped, substract one second
440         if(now_secs_ref == 0) {
441             now_secs_ref = 127;
442         } else  {
443             now_secs_ref -= 1;
444         }
445     }
446     uint32_t pkt_ctr_ref = cycle << 12;
447     pkt_ctr_ref |= (now_secs_ref & 0x7F) << 25;
448
449     if((pkt_ctr & ~0x0FFFL) != pkt_ctr_ref) {
450         debugWarning("reconstructed CTR counter discrepancy\n");
451         debugWarning(" ingredients: %X, %lX, %lX, %lX, %lX, %ld, %ld, %ld, %lld\n",
452                        cycle, pkt_ctr_ref, pkt_ctr, now, m_last_now, now_secs_ref, CYCLE_TIMER_GET_SECS(now), CYCLE_TIMER_GET_SECS(m_last_now), tmp);
453         debugWarning(" diffcy = %ld \n", diff_cycles);
454     }
455     #endif
456     m_last_packet_handled_at = pkt_ctr;
457
458     // leave the offset field (for now?)
459
460     debugOutputExtreme(DEBUG_LEVEL_ULTRA_VERBOSE,
461                 "received packet: length=%d, channel=%d, cycle=%d, at %08X\n",
462                 length, channel, cycle, pkt_ctr);
463     m_packets++;
464     #ifdef DEBUG
465     if (length > m_max_packet_size) {
466         debugWarning("(%p, %s) packet too large: len=%u max=%u\n",
467                      this, getTypeString(), length, m_max_packet_size);
468     }
469     if(m_last_cycle == -1) {
470         debugOutput(DEBUG_LEVEL_VERBOSE, "Handler for %s SP %p is alive (cycle = %u)\n", getTypeString(), this, cycle);
471     }
472     #endif
473
474     // iterate the client if required
475     if(m_Client) {
476         enum raw1394_iso_disposition retval = m_Client->putPacket(data, length, channel, tag, sy, pkt_ctr, dropped_cycles);
477         if (retval == RAW1394_ISO_OK) {
478             if (m_dont_exit_iterate_loop) {
479                 return RAW1394_ISO_OK;
480             } else {
481                 m_dont_exit_iterate_loop = true;
482                 debugOutput(DEBUG_LEVEL_VERBOSE,
483                                 "(%p) loop exit requested\n",
484                                 this);
485                 return RAW1394_ISO_DEFER;
486             }
487         } else {
488             return retval;
489         }
490     }
491
492     return RAW1394_ISO_OK;
493 }
494
495 enum raw1394_iso_disposition
496 IsoHandler::getPacket(unsigned char *data, unsigned int *length,
497                       unsigned char *tag, unsigned char *sy,
498                       int cycle, unsigned int dropped, unsigned int skipped) {
499
500     uint32_t pkt_ctr;
501     if (cycle < 0) {
502         // mark invalid
503         pkt_ctr = 0xFFFFFFFF;
504     } else {
505         // the m_last_now value is set when the iterate() function is called.
506         uint32_t now_cycles = CYCLE_TIMER_GET_CYCLES(m_last_now);
507
508         // two cases can occur:
509         // (1) this packet has been received before iterate() was called (normal case).
510         // (2) this packet has been received after iterate() was called.
511         //     happens when the kernel flushes more packets while we are already processing.
512         //
513         // In case (1) now_cycles is a small number of cycles larger than cycle. In
514         // case (2) now_cycles is a small number of cycles smaller than cycle.
515         // hence  abs(diffCycles(now_cycles, cycles)) has to be 'small'
516
517         // we can calculate the time of arrival for this packet as
518         // 'now' + diffCycles(cycles, now_cycles) * TICKS_PER_CYCLE
519         // in its properly wrapped version
520         int64_t diff_cycles = diffCycles(cycle, now_cycles);
521         int64_t tmp = CYCLE_TIMER_TO_TICKS(m_last_now);
522         tmp += diff_cycles * (int64_t)TICKS_PER_CYCLE;
523         uint64_t pkt_ctr_ticks = wrapAtMinMaxTicks(tmp);
524         pkt_ctr = TICKS_TO_CYCLE_TIMER(pkt_ctr_ticks);
525
526         #if ISOHANDLER_CHECK_CTR_RECONSTRUCTION
527         // add a seconds field
528         uint32_t now = m_manager.get1394Service().getCycleTimer();
529         uint32_t now_secs_ref = CYCLE_TIMER_GET_SECS(now);
530         // causality results in the fact that 'now' is always after 'cycle'
531         if(CYCLE_TIMER_GET_CYCLES(now) > (unsigned int)cycle) {
532             // the cycle field has wrapped, add one second
533             now_secs_ref += 1;
534             // no need for this:
535             if(now_secs_ref == 128) {
536                now_secs_ref = 0;
537             }
538         }
539         uint32_t pkt_ctr_ref = cycle << 12;
540         pkt_ctr_ref |= (now_secs_ref & 0x7F) << 25;
541
542         if((pkt_ctr & ~0x0FFFL) != pkt_ctr_ref) {
543             debugWarning("reconstructed CTR counter discrepancy\n");
544             debugWarning(" ingredients: %X, %lX, %lX, %lX, %lX, %ld, %ld, %ld, %lld\n",
545                         cycle, pkt_ctr_ref, pkt_ctr, now, m_last_now, now_secs_ref, CYCLE_TIMER_GET_SECS(now), CYCLE_TIMER_GET_SECS(m_last_now), tmp);
546             debugWarning(" diffcy = %ld \n", diff_cycles);
547         }
548         #endif
549     }
550     if (m_packets < m_buf_packets) { // these are still prebuffer packets
551         m_last_packet_handled_at = 0xFFFFFFFF;
552     } else {
553         m_last_packet_handled_at = pkt_ctr;
554     }
555     debugOutputExtreme(DEBUG_LEVEL_ULTRA_VERBOSE,
556                 "sending packet: length=%d, cycle=%d, at %08X\n",
557                 *length, cycle, pkt_ctr);
558
559     m_packets++;
560
561     #ifdef DEBUG
562     if(m_last_cycle == -1) {
563         debugOutput(DEBUG_LEVEL_VERBOSE, "Handler for %s SP %p is alive (cycle = %d)\n", getTypeString(), this, cycle);
564     }
565     #endif
566
567     // keep track of dropped cycles
568     int dropped_cycles = 0;
569     if (m_last_cycle != cycle && m_last_cycle != -1) {
570         dropped_cycles = diffCycles(cycle, m_last_cycle) - 1;
571         // correct for skipped packets
572         // since those are not dropped, but only delayed
573         dropped_cycles -= skipped;
574
575         #ifdef DEBUG
576         if(skipped) {
577             debugOutput(DEBUG_LEVEL_VERBOSE,
578                         "(%p) skipped %d cycles, cycle: %d, last_cycle: %d, dropped: %d\n",
579                         this, skipped, cycle, m_last_cycle, dropped);
580             m_skipped += skipped;
581         }
582         if (dropped_cycles < 0) {
583             debugWarning("(%p) dropped < 1 (%d), cycle: %d, last_cycle: %d, dropped: %d, skipped: %d\n",
584                          this, dropped_cycles, cycle, m_last_cycle, dropped, skipped);
585         }
586         if (dropped_cycles > 0) {
587             debugOutput(DEBUG_LEVEL_VERBOSE,
588                         "(%p) dropped %d packets on cycle %u (last_cycle=%u, dropped=%d, skipped: %d)\n",
589                         this, dropped_cycles, cycle, m_last_cycle, dropped, skipped);
590             m_dropped += dropped_cycles - skipped;
591         }
592         #endif
593     }
594     if (cycle >= 0) {
595         m_last_cycle = cycle;
596        
597         #ifdef DEBUG
598 /*        int ahead = diffCycles(cycle, now_cycles);
599         if (ahead < m_min_ahead) m_min_ahead = ahead;
600 */
601         #endif
602     }
603
604     #ifdef DEBUG
605     if (dropped > 0) {
606         debugOutput(DEBUG_LEVEL_VERBOSE,
607                     "(%p) OHCI issue on cycle %u (dropped_cycles=%d, last_cycle=%u, dropped=%d, skipped: %d)\n",
608                     this, cycle, dropped_cycles, m_last_cycle, dropped, skipped);
609     }
610     #endif
611
612     if(m_Client) {
613         enum raw1394_iso_disposition retval;
614         retval = m_Client->getPacket(data, length, tag, sy, pkt_ctr, dropped_cycles, skipped, m_max_packet_size);
615         #ifdef DEBUG
616         if (*length > m_max_packet_size) {
617             debugWarning("(%p, %s) packet too large: len=%u max=%u\n",
618                          this, getTypeString(), *length, m_max_packet_size);
619         }
620         #endif
621         if (retval == RAW1394_ISO_OK) {
622             if (m_dont_exit_iterate_loop) {
623                 return RAW1394_ISO_OK;
624             } else {
625                 m_dont_exit_iterate_loop = true;
626                 debugOutput(DEBUG_LEVEL_VERBOSE,
627                                 "(%p) loop exit requested\n",
628                                 this);
629                 return RAW1394_ISO_DEFER;
630             }
631         } else {
632             return retval;
633         }
634     }
635
636     *tag = 0;
637     *sy = 0;
638     *length = 0;
639     return RAW1394_ISO_OK;
640 }
641
642 bool IsoHandler::prepare()
643 {
644     // check the state
645     if(m_State != E_Initialized) {
646         debugError("Incorrect state, expected E_Initialized, got %d\n",(int)m_State);
647         return false;
648     }
649
650     // Don't call until libraw1394's raw1394_new_handle() function has been
651     // fixed to correctly initialise the iso_packet_infos field.  Bug is
652     // confirmed present in libraw1394 1.2.1.
653     //     raw1394_iso_shutdown(m_handle);
654     m_State = E_Prepared;
655
656     debugOutput( DEBUG_LEVEL_VERBOSE, "Preparing iso handler (%p, client=%p)\n", this, m_Client);
657     dumpInfo();
658     if (getType() == eHT_Receive) {
659         if(raw1394_iso_recv_init(m_handle,
660                                 iso_receive_handler,
661                                 m_buf_packets,
662                                 m_max_packet_size,
663                                 m_Client->getChannel(),
664                                 m_receive_mode,
665                                 m_irq_interval)) {
666             debugFatal("Could not do receive initialisation!\n" );
667             debugFatal("  %s\n",strerror(errno));
668             return false;
669         }
670         return true;
671     } else {
672         if(raw1394_iso_xmit_init(m_handle,
673                                 iso_transmit_handler,
674                                 m_buf_packets,
675                                 m_max_packet_size,
676                                 m_Client->getChannel(),
677                                 m_speed,
678                                 m_irq_interval)) {
679             debugFatal("Could not do xmit initialisation!\n" );
680             return false;
681         }
682         return true;
683     }
684 }
685
686 bool IsoHandler::enable(int cycle)
687 {
688     debugOutput( DEBUG_LEVEL_VERBOSE, "start on cycle %d\n", cycle);
689     // check the state
690     if(m_State != E_Prepared) {
691         if(!prepare()) {
692             debugFatal("Could not prepare handler\n");
693             return false;
694         }
695     }
696
697     if (getType() == eHT_Receive) {
698         if(raw1394_iso_recv_start(m_handle, cycle, -1, 0)) {
699             debugFatal("Could not start receive handler (%s)\n",strerror(errno));
700             dumpInfo();
701             return false;
702         }
703     } else {
704         if(raw1394_iso_xmit_start(m_handle, cycle, m_prebuffers)) {
705             debugFatal("Could not start xmit handler (%s)\n",strerror(errno));
706             dumpInfo();
707             return false;
708         }
709     }
710
711 #ifdef DEBUG
712     m_min_ahead = 7999;
713 #endif
714
715     // indicate that the first iterate() still has to occur.
716     m_last_now = 0xFFFFFFFF;
717     m_last_packet_handled_at = 0xFFFFFFFF;
718
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.