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

Revision 1135, 24.2 kB (checked in by ppalmers, 13 years ago)

centralize byteswapping and make it conditional

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     uint32_t pkt_ctr = cycle << 12;
370
371     // if we assume that one iterate() loop doesn't take longer than 0.5 seconds,
372     // the seconds field won't change while the iterate loop runs
373     // this means that we can preset 'now' before running iterate()
374     uint32_t now_secs = CYCLE_TIMER_GET_SECS(m_last_now);
375     // causality results in the fact that 'now' is always after 'cycle'
376     // except if additional packets are received between setting the
377     // m_last_now and the starting the iterate() loop.
378     // this causes the m_last_now to be set at a time before the last packet
379     // in this loop is received. however, it's not going to be >4000 cycles.
380     // hence:
381     // - if the m_last_now > cycle, there is no need to unwrap
382     //   both values are within the same second
383     // - if m_last_now < cycle it can mean two things:
384     //    * m_last_now has wrapped, but is still later than cycle
385     //      hence diffCycles(m_last_now, cycle) > 0. We should unwrap
386     //    * m_last_now has not wrapped, and cycle is ahead of m_last_now
387     //      this means that the cycle is more recent than the saved
388     //      m_last_now value
389     // . Hence if we calculate
390     // the unwrapped difference, and it's larger than 0, this means
391     // that m_last_now is after the current cycle. .
392     // it m_last_now is before the current cycle, we should not unwrap
393     // NOTE: another option is to reread the m_last_now
394     if( (CYCLE_TIMER_GET_CYCLES(m_last_now) < cycle)
395         && diffCycles(CYCLE_TIMER_GET_CYCLES(m_last_now), cycle) >= 0) {
396         debugOutputExtreme(DEBUG_LEVEL_VERBOSE,
397                            "unwrapping %d => %d, %d\n",
398                            CYCLE_TIMER_GET_CYCLES(m_last_now),
399                            cycle);
400         // the cycle field has wrapped, substract one second
401         if(now_secs == 0) {
402             now_secs = 127;
403         } else  {
404             now_secs -= 1;
405         }
406     }
407    
408     #ifdef DEBUG
409     if( (CYCLE_TIMER_GET_CYCLES(m_last_now) < cycle)
410         && diffCycles(CYCLE_TIMER_GET_CYCLES(m_last_now), cycle) < 0) {
411         debugWarning("Special non-unwrapping happened\n");
412     }   
413     #endif
414    
415     pkt_ctr |= (now_secs & 0x7F) << 25;
416
417
418
419     #if ISOHANDLER_CHECK_CTR_RECONSTRUCTION
420     // add a seconds field
421     uint32_t now = m_manager.get1394Service().getCycleTimer();
422     uint32_t now_secs_ref = CYCLE_TIMER_GET_SECS(now);
423     // causality results in the fact that 'now' is always after 'cycle'
424     // or at best, equal (if this handler was called within 125us after
425     // the packet was on the wire).
426     if(CYCLE_TIMER_GET_CYCLES(now) < cycle) {
427         // the cycle field has wrapped, substract one second
428         if(now_secs_ref == 0) {
429             now_secs_ref = 127;
430         } else  {
431             now_secs_ref -= 1;
432         }
433     }
434     uint32_t pkt_ctr_ref = cycle << 12;
435     pkt_ctr_ref |= (now_secs_ref & 0x7F) << 25;
436
437     if(pkt_ctr != pkt_ctr_ref) {
438         debugWarning("reconstructed CTR counter discrepancy\n");
439         debugWarning(" ingredients: %X, %lX, %lX, %lX, %lX, %ld, %ld\n",
440                        cycle, pkt_ctr_ref, pkt_ctr, now, m_last_now, now_secs_ref, now_secs);
441     }
442     #endif
443
444     // leave the offset field (for now?)
445
446     debugOutputExtreme(DEBUG_LEVEL_ULTRA_VERBOSE,
447                        "received packet: length=%d, channel=%d, cycle=%d\n",
448                        length, channel, cycle);
449     #ifdef DEBUG
450     m_packets++;
451     if (length > m_max_packet_size) {
452         debugWarning("(%p, %s) packet too large: len=%u max=%u\n",
453                      this, getTypeString(), length, m_max_packet_size);
454     }
455     if(m_last_cycle == -1) {
456         debugOutput(DEBUG_LEVEL_VERBOSE, "Handler for %s SP %p is alive (cycle = %u)\n", getTypeString(), this, cycle);
457     }
458     #endif
459
460     // keep track of dropped cycles
461     int dropped_cycles = 0;
462     if (m_last_cycle != (int)cycle && m_last_cycle != -1) {
463         dropped_cycles = diffCycles(cycle, m_last_cycle) - 1;
464         #ifdef DEBUG
465         if (dropped_cycles < 0) {
466             debugWarning("(%p) dropped < 1 (%d), cycle: %d, last_cycle: %d, dropped: %d, 'skipped'=%u\n",
467                          this, dropped_cycles, cycle, m_last_cycle, dropped, skipped);
468         }
469         if (dropped_cycles > 0) {
470             debugOutput(DEBUG_LEVEL_NORMAL,
471                         "(%p) dropped %d packets on cycle %u, 'dropped'=%u, 'skipped'=%u, cycle=%d, m_last_cycle=%d\n",
472                         this, dropped_cycles, cycle, dropped, skipped, cycle, m_last_cycle);
473             m_dropped += dropped_cycles;
474         }
475         #endif
476     }
477     m_last_cycle = cycle;
478
479     // iterate the client if required
480     if(m_Client) {
481         enum raw1394_iso_disposition retval = m_Client->putPacket(data, length, channel, tag, sy, pkt_ctr, dropped_cycles, skipped);
482         if (retval == RAW1394_ISO_OK) {
483             if (m_dont_exit_iterate_loop) {
484                 return RAW1394_ISO_OK;
485             } else {
486                 m_dont_exit_iterate_loop = true;
487                 debugOutput(DEBUG_LEVEL_VERBOSE,
488                                 "(%p) loop exit requested\n",
489                                 this);
490                 return RAW1394_ISO_DEFER;
491             }
492         } else {
493             return retval;
494         }
495     }
496
497     return RAW1394_ISO_OK;
498 }
499
500 enum raw1394_iso_disposition
501 IsoHandler::getPacket(unsigned char *data, unsigned int *length,
502                       unsigned char *tag, unsigned char *sy,
503                       int cycle, unsigned int dropped, unsigned int skipped) {
504
505     uint32_t pkt_ctr;
506     if (cycle < 0) {
507         // mark invalid
508         pkt_ctr = 0xFFFFFFFF;
509     } else {
510         pkt_ctr = cycle << 12;
511
512 #if 0 // we don't need this for xmit
513         // if we assume that one iterate() loop doesn't take longer than 0.5 seconds,
514         // the seconds field won't change while the iterate loop runs
515         // this means that we can preset 'now' before running iterate()
516         uint32_t now_secs = CYCLE_TIMER_GET_SECS(m_last_now);
517         // causality results in the fact that 'now' is always after 'cycle'
518         if(CYCLE_TIMER_GET_CYCLES(m_last_now) > (unsigned int)cycle) {
519             // the cycle field has wrapped, add one second
520             now_secs += 1;
521             // no need for this:
522             //if(now_secs == 128) {
523             //    now_secs = 0;
524             //}
525             // since we mask later on
526         }
527         pkt_ctr |= (now_secs & 0x7F) << 25;
528
529         #if ISOHANDLER_CHECK_CTR_RECONSTRUCTION
530         // add a seconds field
531         uint32_t now = m_manager.get1394Service().getCycleTimer();
532         uint32_t now_secs_ref = CYCLE_TIMER_GET_SECS(now);
533         // causality results in the fact that 'now' is always after 'cycle'
534         if(CYCLE_TIMER_GET_CYCLES(now) > (unsigned int)cycle) {
535             // the cycle field has wrapped, add one second
536             now_secs_ref += 1;
537             // no need for this:
538             //if(now_secs == 128) {
539             //    now_secs = 0;
540             //}
541             // since we mask later on
542         }
543         uint32_t pkt_ctr_ref = cycle << 12;
544         pkt_ctr_ref |= (now_secs_ref & 0x7F) << 25;
545    
546         if(pkt_ctr != pkt_ctr_ref) {
547             debugWarning("reconstructed CTR counter discrepancy\n");
548             pkt_ctr=pkt_ctr_ref;
549         }
550         #endif
551 #endif
552     }
553
554     debugOutputExtreme(DEBUG_LEVEL_ULTRA_VERBOSE,
555                        "sending packet: length=%d, cycle=%d\n",
556                        *length, cycle);
557
558     #ifdef DEBUG
559     m_packets++;
560     if(m_last_cycle == -1) {
561         debugOutput(DEBUG_LEVEL_VERBOSE, "Handler for %s SP %p is alive (cycle = %d)\n", getTypeString(), this, cycle);
562     }
563     #endif
564
565     // keep track of dropped cycles
566     int dropped_cycles = 0;
567     if (m_last_cycle != cycle && m_last_cycle != -1) {
568         dropped_cycles = diffCycles(cycle, m_last_cycle) - 1;
569         // correct for skipped packets
570         // since those are not dropped, but only delayed
571         dropped_cycles -= skipped;
572
573         #ifdef DEBUG
574         if(skipped) {
575             debugOutput(DEBUG_LEVEL_NORMAL,
576                         "(%p) skipped %d cycles, cycle: %d, last_cycle: %d, dropped: %d\n",
577                         this, skipped, cycle, m_last_cycle, dropped);
578         }
579         if (dropped_cycles < 0) {
580             debugWarning("(%p) dropped < 1 (%d), cycle: %d, last_cycle: %d, dropped: %d, skipped: %d\n",
581                          this, dropped_cycles, cycle, m_last_cycle, dropped, skipped);
582         }
583         if (dropped_cycles > 0) {
584             debugOutput(DEBUG_LEVEL_NORMAL,
585                         "(%p) dropped %d packets on cycle %u (last_cycle=%u, dropped=%d, skipped: %d)\n",
586                         this, dropped_cycles, cycle, m_last_cycle, dropped, skipped);
587             m_dropped += dropped_cycles - skipped;
588         }
589         #endif
590     }
591     if (cycle >= 0) {
592         m_last_cycle = cycle;
593        
594         #ifdef DEBUG
595 /*        int ahead = diffCycles(cycle, now_cycles);
596         if (ahead < m_min_ahead) m_min_ahead = ahead;
597 */
598         #endif
599     }
600
601     if(m_Client) {
602         enum raw1394_iso_disposition retval;
603         retval = m_Client->getPacket(data, length, tag, sy, pkt_ctr, dropped, skipped, m_max_packet_size);
604         #ifdef DEBUG
605         if (*length > m_max_packet_size) {
606             debugWarning("(%p, %s) packet too large: len=%u max=%u\n",
607                          this, getTypeString(), *length, m_max_packet_size);
608         }
609         #endif
610         if (retval == RAW1394_ISO_OK) {
611             if (m_dont_exit_iterate_loop) {
612                 return RAW1394_ISO_OK;
613             } else {
614                 m_dont_exit_iterate_loop = true;
615                 debugOutput(DEBUG_LEVEL_VERBOSE,
616                                 "(%p) loop exit requested\n",
617                                 this);
618                 return RAW1394_ISO_DEFER;
619             }
620         } else {
621             return retval;
622         }
623     }
624
625     *tag = 0;
626     *sy = 0;
627     *length = 0;
628     return RAW1394_ISO_OK;
629 }
630
631 bool IsoHandler::prepare()
632 {
633     // check the state
634     if(m_State != E_Initialized) {
635         debugError("Incorrect state, expected E_Initialized, got %d\n",(int)m_State);
636         return false;
637     }
638
639     // Don't call until libraw1394's raw1394_new_handle() function has been
640     // fixed to correctly initialise the iso_packet_infos field.  Bug is
641     // confirmed present in libraw1394 1.2.1.
642     //     raw1394_iso_shutdown(m_handle);
643     m_State = E_Prepared;
644
645     debugOutput( DEBUG_LEVEL_VERBOSE, "Preparing iso handler (%p, client=%p)\n", this, m_Client);
646     dumpInfo();
647     if (getType() == eHT_Receive) {
648         if(m_irq_interval > 1) {
649             if(raw1394_iso_recv_init(m_handle,
650                                     iso_receive_handler,
651                                     m_buf_packets,
652                                     m_max_packet_size,
653                                     m_Client->getChannel(),
654                                     RAW1394_DMA_BUFFERFILL,
655 //                                     RAW1394_DMA_PACKET_PER_BUFFER,
656                                     m_irq_interval)) {
657                 debugFatal("Could not do receive initialisation (DMA_BUFFERFILL)!\n" );
658                 debugFatal("  %s\n",strerror(errno));
659                 return false;
660             }
661         } else {
662             if(raw1394_iso_recv_init(m_handle,
663                                     iso_receive_handler,
664                                     m_buf_packets,
665                                     m_max_packet_size,
666                                     m_Client->getChannel(),
667                                     RAW1394_DMA_PACKET_PER_BUFFER,
668                                     m_irq_interval)) {
669                 debugFatal("Could not do receive initialisation (PACKET_PER_BUFFER)!\n" );
670                 debugFatal("  %s\n",strerror(errno));
671                 return false;
672             }
673         }
674         return true;
675     } else {
676         if(raw1394_iso_xmit_init(m_handle,
677                                 iso_transmit_handler,
678                                 m_buf_packets,
679                                 m_max_packet_size,
680                                 m_Client->getChannel(),
681                                 m_speed,
682                                 m_irq_interval)) {
683             debugFatal("Could not do xmit initialisation!\n" );
684             return false;
685         }
686         return true;
687     }
688 }
689
690 bool IsoHandler::enable(int cycle)
691 {
692     debugOutput( DEBUG_LEVEL_VERBOSE, "start on cycle %d\n", cycle);
693     // check the state
694     if(m_State != E_Prepared) {
695         if(!prepare()) {
696             debugFatal("Could not prepare handler\n");
697             return false;
698         }
699     }
700
701     if (getType() == eHT_Receive) {
702         if(raw1394_iso_recv_start(m_handle, cycle, -1, 0)) {
703             debugFatal("Could not start receive handler (%s)\n",strerror(errno));
704             dumpInfo();
705             return false;
706         }
707     } else {
708         if(raw1394_iso_xmit_start(m_handle, cycle, m_prebuffers)) {
709             debugFatal("Could not start xmit handler (%s)\n",strerror(errno));
710             dumpInfo();
711             return false;
712         }
713     }
714
715 #ifdef DEBUG
716     m_min_ahead = 7999;
717 #endif
718     m_State = E_Running;
719     return true;
720 }
721
722 /**
723  * @brief convert a EHandlerType to a string
724  * @param t the type
725  * @return a char * describing the state
726  */
727 const char *
728 IsoHandler::eHTToString(enum EHandlerType t) {
729     switch (t) {
730         case eHT_Receive: return "Receive";
731         case eHT_Transmit: return "Transmit";
732         default: return "error: unknown type";
733     }
734 }
Note: See TracBrowser for help on using the browser.