root/branches/streaming-rework/src/libieee1394/ieee1394service.cpp

Revision 414, 23.2 kB (checked in by pieterpalmers, 16 years ago)

extended ARM handler functionality

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /* ieee1394service.cpp
2  * Copyright (C) 2005,07 by Daniel Wagner
3  * Copyright (C) 2007 by Pieter Palmers
4  *
5  * This file is part of FreeBoB.
6  *
7  * FreeBoB is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  * FreeBoB is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with FreeBoB; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19  * MA 02111-1307 USA.
20  */
21 #include "ieee1394service.h"
22 #include "ARMHandler.h"
23
24 #include <libavc1394/avc1394.h>
25 #include <libraw1394/csr.h>
26 #include <libiec61883/iec61883.h>
27
28 #include <errno.h>
29 #include <netinet/in.h>
30
31 #include <string.h>
32
33 #include <iostream>
34 #include <iomanip>
35
36 IMPL_DEBUG_MODULE( Ieee1394Service, Ieee1394Service, DEBUG_LEVEL_VERBOSE );
37
38 Ieee1394Service::Ieee1394Service()
39     : m_handle( 0 ), m_resetHandle( 0 )
40     , m_port( -1 )
41     , m_threadRunning( false )
42 {
43     pthread_mutex_init( &m_mutex, 0 );
44    
45     for (unsigned int i=0; i<64; i++) {
46         m_channels[i].channel=-1;
47         m_channels[i].bandwidth=-1;
48         m_channels[i].alloctype=AllocFree;
49         m_channels[i].xmit_node=0xFFFF;
50         m_channels[i].xmit_plug=-1;
51         m_channels[i].recv_node=0xFFFF;
52         m_channels[i].recv_plug=-1;
53     }
54 }
55
56 Ieee1394Service::~Ieee1394Service()
57 {
58     stopRHThread();
59
60     for ( arm_handler_vec_t::iterator it = m_armHandlers.begin();
61           it != m_armHandlers.end();
62           ++it )
63     {
64         debugOutput(DEBUG_LEVEL_VERBOSE, "Unregistering ARM handler for 0x%016llX\n", (*it)->getStart());
65         int err=raw1394_arm_unregister(m_resetHandle, (*it)->getStart());
66         if (err) {
67             debugError(" Failed to unregister ARM handler for 0x%016llX\n", (*it)->getStart());
68             debugError(" Error: %s\n", strerror(errno));
69         }
70     }
71
72     if ( m_handle ) {
73         raw1394_destroy_handle( m_handle );
74     }
75    
76     if ( m_resetHandle ) {
77         raw1394_destroy_handle( m_resetHandle );
78     }
79 }
80
81 bool
82 Ieee1394Service::initialize( int port )
83 {
84     using namespace std;
85
86     m_handle = raw1394_new_handle_on_port( port );
87     if ( !m_handle ) {
88         if ( !errno ) {
89             debugFatal("libraw1394 not compatible\n");
90         } else {
91             debugFatal("Ieee1394Service::initialize: Could not get 1394 handle: %s\n",
92                 strerror(errno) );
93             debugFatal("Is ieee1394 and raw1394 driver loaded?\n");
94         }
95         return false;
96     }
97
98     m_resetHandle = raw1394_new_handle_on_port( port );
99     if ( !m_handle ) {
100         if ( !errno ) {
101             debugFatal("libraw1394 not compatible\n");
102         } else {
103             debugFatal("Ieee1394Service::initialize: Could not get 1394 handle: %s",
104                 strerror(errno) );
105             debugFatal("Is ieee1394 and raw1394 driver loaded?\n");
106         }
107         return false;
108     }
109
110     m_port = port;
111
112     raw1394_set_userdata( m_handle, this );
113     raw1394_set_userdata( m_resetHandle, this );
114     raw1394_set_bus_reset_handler( m_resetHandle,
115                                    this->resetHandlerLowLevel );
116
117     m_default_arm_handler = raw1394_set_arm_tag_handler( m_resetHandle,
118                                    this->armHandlerLowLevel );
119     startRHThread();
120
121     return true;
122 }
123
124 int
125 Ieee1394Service::getNodeCount()
126 {
127     return raw1394_get_nodecount( m_handle );
128 }
129
130 nodeid_t Ieee1394Service::getLocalNodeId() {
131     return raw1394_get_local_id(m_handle) & 0x3F;
132 }
133
134 bool
135 Ieee1394Service::read( fb_nodeid_t nodeId,
136                        fb_nodeaddr_t addr,
137                        size_t length,
138                        fb_quadlet_t* buffer )
139 {
140     using namespace std;
141     if ( raw1394_read( m_handle, nodeId, addr, length*4, buffer ) == 0 ) {
142
143         #ifdef DEBUG
144         debugOutput(DEBUG_LEVEL_VERY_VERBOSE,
145             "read: node 0x%X, addr = 0x%016llX, length = %u\n",
146             nodeId, addr, length);
147         printBuffer( DEBUG_LEVEL_VERY_VERBOSE, length, buffer );
148         #endif
149
150         return true;
151     } else {
152         #ifdef DEBUG
153         debugError("raw1394_read failed: node 0x%X, addr = 0x%016llX, length = %u\n",
154               nodeId, addr, length);
155         #endif
156
157         return false;
158     }
159 }
160
161
162 bool
163 Ieee1394Service::read_quadlet( fb_nodeid_t nodeId,
164                                fb_nodeaddr_t addr,
165                                fb_quadlet_t* buffer )
166 {
167     return read( nodeId,  addr, sizeof( *buffer )/4, buffer );
168 }
169
170 bool
171 Ieee1394Service::read_octlet( fb_nodeid_t nodeId,
172                               fb_nodeaddr_t addr,
173                               fb_octlet_t* buffer )
174 {
175     return read( nodeId, addr, sizeof( *buffer )/4,
176                  reinterpret_cast<fb_quadlet_t*>( buffer ) );
177 }
178
179
180 bool
181 Ieee1394Service::write( fb_nodeid_t nodeId,
182                         fb_nodeaddr_t addr,
183                         size_t length,
184                         fb_quadlet_t* data )
185 {
186     using namespace std;
187
188     #ifdef DEBUG
189     debugOutput(DEBUG_LEVEL_VERY_VERBOSE,"write: node 0x%X, addr = 0x%016X, length = %d\n",
190                 nodeId, addr, length);
191     printBuffer( DEBUG_LEVEL_VERY_VERBOSE, length, data );
192     #endif
193
194     return raw1394_write( m_handle, nodeId, addr, length*4, data ) == 0;
195 }
196
197
198 bool
199 Ieee1394Service::write_quadlet( fb_nodeid_t nodeId,
200                                 fb_nodeaddr_t addr,
201                                 fb_quadlet_t data )
202 {
203     return write( nodeId, addr, sizeof( data )/4, &data );
204 }
205
206 bool
207 Ieee1394Service::write_octlet( fb_nodeid_t nodeId,
208                                fb_nodeaddr_t addr,
209                                fb_octlet_t data )
210 {
211     return write( nodeId, addr, sizeof( data )/4,
212                   reinterpret_cast<fb_quadlet_t*>( &data ) );
213 }
214
215 fb_quadlet_t*
216 Ieee1394Service::transactionBlock( fb_nodeid_t nodeId,
217                                    fb_quadlet_t* buf,
218                                    int len,
219                                    unsigned int* resp_len )
220 {
221     for (int i = 0; i < len; ++i) {
222         buf[i] = ntohl( buf[i] );
223     }
224
225     #ifdef DEBUG
226     debugOutputShort(DEBUG_LEVEL_VERY_VERBOSE, "  pre avc1394_transaction_block2\n" );
227     printBuffer( DEBUG_LEVEL_VERY_VERBOSE, len, buf );
228     #endif
229
230     fb_quadlet_t* result =
231         avc1394_transaction_block2( m_handle,
232                                     nodeId,
233                                     buf,
234                                     len,
235                                     resp_len,
236                                     10 );
237
238     #ifdef DEBUG
239     debugOutputShort(DEBUG_LEVEL_VERY_VERBOSE, "  post avc1394_transaction_block2\n" );
240     printBuffer( DEBUG_LEVEL_VERY_VERBOSE, *resp_len, result );
241     #endif
242
243     for ( unsigned int i = 0; i < *resp_len; ++i ) {
244         result[i] = htonl( result[i] );
245     }
246
247     return result;
248 }
249
250
251 bool
252 Ieee1394Service::transactionBlockClose()
253 {
254     avc1394_transaction_block_close( m_handle );
255     return true;
256 }
257
258 bool
259 Ieee1394Service::setVerbose( int verboseLevel )
260 {
261     setDebugLevel(verboseLevel);
262     return true;
263 }
264
265 int
266 Ieee1394Service::getVerboseLevel()
267 {
268     return getDebugLevel();
269 }
270
271 void
272 Ieee1394Service::printBuffer( unsigned int level, size_t length, fb_quadlet_t* buffer ) const
273 {
274
275     for ( unsigned int i=0; i < length; ++i ) {
276         if ( ( i % 4 ) == 0 ) {
277             if ( i > 0 ) {
278                 debugOutputShort(level,"\n");
279             }
280             debugOutputShort(level," %4d: ",i*4);
281         }
282         debugOutputShort(level,"%08X ",buffer[i]);
283     }
284     debugOutputShort(level,"\n");
285 }
286 void
287 Ieee1394Service::printBufferBytes( unsigned int level, size_t length, byte_t* buffer ) const
288 {
289
290     for ( unsigned int i=0; i < length; ++i ) {
291         if ( ( i % 16 ) == 0 ) {
292             if ( i > 0 ) {
293                 debugOutputShort(level,"\n");
294             }
295             debugOutputShort(level," %4d: ",i*16);
296         }
297         debugOutputShort(level,"%02X ",buffer[i]);
298     }
299     debugOutputShort(level,"\n");
300 }
301
302 int
303 Ieee1394Service::resetHandlerLowLevel( raw1394handle_t handle,
304                                        unsigned int generation )
305 {
306     raw1394_update_generation ( handle, generation );
307     Ieee1394Service* instance
308         = (Ieee1394Service*) raw1394_get_userdata( handle );
309     instance->resetHandler( generation );
310
311     return 0;
312 }
313
314 bool
315 Ieee1394Service::resetHandler( unsigned int generation )
316 {
317     m_generation = generation;
318
319     for ( reset_handler_vec_t::iterator it = m_busResetHandlers.begin();
320           it != m_busResetHandlers.end();
321           ++it )
322     {
323         Functor* func = *it;
324         ( *func )();
325     }
326
327     return true;
328 }
329
330 bool Ieee1394Service::registerARMhandler(ARMHandler *h) {
331     debugOutput(DEBUG_LEVEL_VERBOSE, "Registering ARM handler (%p) for 0x%016llX, length %u\n",
332         h, h->getStart(), h->getLength());
333
334     int err=raw1394_arm_register(m_resetHandle, h->getStart(),
335                          h->getLength(), h->getBuffer(), (octlet_t)h,
336                          h->getAccessRights(),
337                          h->getNotificationOptions(),
338                          h->getClientTransactions());
339     if (err) {
340         debugError("Failed to register ARM handler for 0x%016llX\n", h->getStart());
341         debugError(" Error: %s\n", strerror(errno));
342         return false;
343     }
344    
345     m_armHandlers.push_back( h );
346
347     return true;
348 }
349
350 bool Ieee1394Service::unregisterARMhandler( ARMHandler *h ) {
351     debugOutput(DEBUG_LEVEL_VERBOSE, "Unregistering ARM handler (%p) for 0x%016llX\n",
352         h, h->getStart());
353    
354     for ( arm_handler_vec_t::iterator it = m_armHandlers.begin();
355           it != m_armHandlers.end();
356           ++it )
357     {
358         if((*it) == h) {
359             int err=raw1394_arm_unregister(m_resetHandle, h->getStart());
360             if (err) {
361                 debugError("Failed to unregister ARM handler (%p)\n", h);
362                 debugError(" Error: %s\n", strerror(errno));
363             } else {
364                 m_armHandlers.erase(it);
365                 return true;
366             }
367         }
368     }
369     debugOutput(DEBUG_LEVEL_VERBOSE, " handler not found!\n");
370
371     return false;
372 }
373 /**
374  * @brief Tries to find a free ARM address range
375  *
376  * @param start  address to start with
377  * @param length length of the block needed (bytes)
378  * @param step   step to use when searching (bytes)
379  * @return The base address that is free, and 0xFFFFFFFFFFFFFFFF when failed
380  */
381 nodeaddr_t Ieee1394Service::findFreeARMBlock( nodeaddr_t start, size_t length, size_t step ) {
382     debugOutput(DEBUG_LEVEL_VERBOSE, "Finding free ARM block of %d bytes, from 0x%016llX in steps of %d bytes\n",
383         length, start, step);
384        
385     int cnt=0;
386     const int maxcnt=10;
387     int err=1;
388     while(err && cnt++ < maxcnt) {
389         // try to register
390         err=raw1394_arm_register(m_resetHandle, start, length, 0, 0, 0, 0, 0);
391        
392         if (err) {
393             debugOutput(DEBUG_LEVEL_VERBOSE, " -> cannot use 0x%016llX\n", start);
394             debugError("    Error: %s\n", strerror(errno));
395             start += step;
396         } else {
397             debugOutput(DEBUG_LEVEL_VERBOSE, " -> use 0x%016llX\n", start);
398             err=raw1394_arm_unregister(m_resetHandle, start);
399             if (err) {
400                 debugOutput(DEBUG_LEVEL_VERBOSE, " error unregistering test handler\n");
401                 debugError("    Error: %s\n", strerror(errno));
402                 return 0xFFFFFFFFFFFFFFFFLLU;
403             }
404             return start;
405         }
406     }
407     debugOutput(DEBUG_LEVEL_VERBOSE, " Could not find free block in %d tries\n",cnt);
408     return 0xFFFFFFFFFFFFFFFFLLU;
409    
410 }
411
412 int
413 Ieee1394Service::armHandlerLowLevel(raw1394handle_t handle,
414                      unsigned long arm_tag,
415                      byte_t request_type, unsigned int requested_length,
416                      void *data)
417 {
418     Ieee1394Service* instance
419         = (Ieee1394Service*) raw1394_get_userdata( handle );
420     instance->armHandler( arm_tag, request_type, requested_length, data );
421
422     return 0;
423 }
424
425 bool
426 Ieee1394Service::armHandler(  unsigned long arm_tag,
427                      byte_t request_type, unsigned int requested_length,
428                      void *data)
429 {
430     for ( arm_handler_vec_t::iterator it = m_armHandlers.begin();
431           it != m_armHandlers.end();
432           ++it )
433     {
434         if((*it) == (ARMHandler *)arm_tag) {
435             struct raw1394_arm_request_response *arm_req_resp;
436             arm_req_resp  = (struct raw1394_arm_request_response *) data;
437             raw1394_arm_request_t arm_req=arm_req_resp->request;
438             raw1394_arm_response_t arm_resp=arm_req_resp->response;
439            
440             debugOutput(DEBUG_LEVEL_VERBOSE,"ARM handler for address 0x%016llX called\n",
441                 (*it)->getStart());
442             debugOutput(DEBUG_LEVEL_VERBOSE," request type   : 0x%02X\n",request_type);
443             debugOutput(DEBUG_LEVEL_VERBOSE," request length : %04d\n",requested_length);
444            
445             switch(request_type) {
446                 case RAW1394_ARM_READ:
447                     (*it)->handleRead(arm_req);
448                     *arm_resp=*((*it)->getResponse());
449                     break;
450                 case RAW1394_ARM_WRITE:
451                     (*it)->handleWrite(arm_req);
452                     *arm_resp=*((*it)->getResponse());
453                     break;
454                 case RAW1394_ARM_LOCK:
455                     (*it)->handleLock(arm_req);
456                     *arm_resp=*((*it)->getResponse());
457                     break;
458                 default:
459                     debugWarning("Unknown request type received, ignoring...\n");
460             }
461
462             return true;
463         }
464     }
465
466     debugOutput(DEBUG_LEVEL_VERBOSE,"default ARM handler called\n");
467
468     m_default_arm_handler(m_resetHandle, arm_tag, request_type, requested_length, data );
469     return true;
470 }
471
472 bool
473 Ieee1394Service::startRHThread()
474 {
475     int i;
476
477     if ( m_threadRunning ) {
478         return true;
479     }
480     pthread_mutex_lock( &m_mutex );
481     i = pthread_create( &m_thread, 0, rHThread, this );
482     pthread_mutex_unlock( &m_mutex );
483     if (i) {
484         debugFatal("Could not start ieee1394 service thread\n");
485         return false;
486     }
487     m_threadRunning = true;
488
489     return true;
490 }
491
492 void
493 Ieee1394Service::stopRHThread()
494 {
495     if ( m_threadRunning ) {
496         pthread_mutex_lock (&m_mutex);
497         pthread_cancel (m_thread);
498         pthread_join (m_thread, 0);
499         pthread_mutex_unlock (&m_mutex);
500         m_threadRunning = false;
501     }
502 }
503
504 void*
505 Ieee1394Service::rHThread( void* arg )
506 {
507     Ieee1394Service* pIeee1394Service = (Ieee1394Service*) arg;
508
509     while (true) {
510         raw1394_loop_iterate (pIeee1394Service->m_resetHandle);
511         pthread_testcancel ();
512     }
513
514     return 0;
515 }
516
517 bool
518 Ieee1394Service::addBusResetHandler( Functor* functor )
519 {
520     debugOutput(DEBUG_LEVEL_VERBOSE, "Adding busreset handler (%p)\n", functor);
521     m_busResetHandlers.push_back( functor );
522     return true;
523 }
524
525 bool
526 Ieee1394Service::remBusResetHandler( Functor* functor )
527 {
528     debugOutput(DEBUG_LEVEL_VERBOSE, "Removing busreset handler (%p)\n", functor);
529    
530     for ( reset_handler_vec_t::iterator it = m_busResetHandlers.begin();
531           it != m_busResetHandlers.end();
532           ++it )
533     {
534         if ( *it == functor ) {
535             debugOutput(DEBUG_LEVEL_VERBOSE, " found\n");
536             m_busResetHandlers.erase( it );
537             return true;
538         }
539     }
540     debugOutput(DEBUG_LEVEL_VERBOSE, " not found\n");
541     return false;
542 }
543
544 /**
545  * Allocates an iso channel for use by the interface in a similar way to
546  * libiec61883.  Returns -1 on error (due to there being no free channels)
547  * or an allocated channel number.
548  *
549  * Does not perform anything other than registering the channel and the
550  * bandwidth at the IRM
551  *
552  * Also allocates the necessary bandwidth (in ISO allocation units).
553  *
554  * FIXME: As in libiec61883, channel 63 is not requested; this is either a
555  * bug or it's omitted since that's the channel preferred by video devices.
556  *
557  * @param bandwidth the bandwidth to allocate for this channel
558  * @return the channel number
559  */
560 signed int Ieee1394Service::allocateIsoChannelGeneric(unsigned int bandwidth) {
561     debugOutput(DEBUG_LEVEL_VERBOSE, "Allocating ISO channel using generic method...\n" );
562    
563     struct ChannelInfo cinfo;
564
565     int c = -1;
566     for (c = 0; c < 63; c++) {
567         if (raw1394_channel_modify (m_handle, c, RAW1394_MODIFY_ALLOC) == 0)
568             break;
569     }
570     if (c < 63) {
571         if (raw1394_bandwidth_modify(m_handle, bandwidth, RAW1394_MODIFY_ALLOC) < 0) {
572             debugFatal("Could not allocate bandwidth of %d\n", bandwidth);
573            
574             raw1394_channel_modify (m_handle, c, RAW1394_MODIFY_FREE);
575             return -1;
576         } else {
577             cinfo.channel=c;
578             cinfo.bandwidth=bandwidth;
579             cinfo.alloctype=AllocGeneric;
580            
581             if (registerIsoChannel(c, cinfo)) {
582                 return c;
583             } else {
584                 raw1394_bandwidth_modify(m_handle, bandwidth, RAW1394_MODIFY_FREE);
585                 raw1394_channel_modify (m_handle, c, RAW1394_MODIFY_FREE);
586                 return -1;
587             }
588         }
589     }
590     return -1;
591 }
592
593 /**
594  * Allocates an iso channel for use by the interface in a similar way to
595  * libiec61883.  Returns -1 on error (due to there being no free channels)
596  * or an allocated channel number.
597  *
598  * Uses IEC61883 Connection Management Procedure to establish the connection.
599  *
600  * Also allocates the necessary bandwidth (in ISO allocation units).
601  *
602  * @param xmit_node  node id of the transmitter
603  * @param xmit_plug  the output plug to use. If -1, find the first online plug, and
604  * upon return, contains the plug number used.
605  * @param recv_node  node id of the receiver
606  * @param recv_plug the input plug to use. If -1, find the first online plug, and
607  * upon return, contains the plug number used.
608  *
609  * @return the channel number
610  */
611
612 signed int Ieee1394Service::allocateIsoChannelCMP(
613     nodeid_t xmit_node, int xmit_plug,
614     nodeid_t recv_node, int recv_plug
615     ) {
616
617     debugOutput(DEBUG_LEVEL_VERBOSE, "Allocating ISO channel using IEC61883 CMP...\n" );
618    
619     struct ChannelInfo cinfo;
620    
621     int c = -1;
622     int bandwidth=1;
623    
624     // do connection management: make connection
625     c = iec61883_cmp_connect(
626         m_handle,
627         xmit_node | 0xffc0,
628         &xmit_plug,
629         recv_node | 0xffc0,
630         &recv_plug,
631         &bandwidth);
632
633     if((c<0) || (c>63)) {
634         debugError("Could not do CMP from %04X:%02d to %04X:%02d\n",
635             xmit_node, xmit_plug, recv_node, recv_plug
636             );
637         return -1;
638     }
639
640     cinfo.channel=c;
641     cinfo.bandwidth=bandwidth;
642     cinfo.alloctype=AllocCMP;
643    
644     cinfo.xmit_node=xmit_node;
645     cinfo.xmit_plug=xmit_plug;
646     cinfo.recv_node=recv_node;
647     cinfo.recv_plug=recv_plug;
648        
649     if (registerIsoChannel(c, cinfo)) {
650         return c;
651     }
652
653     return -1;
654 }
655
656 /**
657  * Deallocates an iso channel.  Silently ignores a request to deallocate
658  * a negative channel number.
659  *
660  * Figures out the method that was used to allocate the channel (generic, cmp, ...)
661  * and uses the appropriate method to deallocate. Also frees the bandwidth
662  * that was reserved along with this channel.
663  *
664  * @param c channel number
665  * @return true if successful
666  */
667 bool Ieee1394Service::freeIsoChannel(signed int c) {
668     debugOutput(DEBUG_LEVEL_VERBOSE, "Freeing ISO channel %d...\n", c );
669    
670     if (c < 0 || c > 63) {
671         debugWarning("Invalid channel number: %d\n", c);
672         return false;
673     }
674    
675     switch (m_channels[c].alloctype) {
676         default:
677             debugError(" BUG: invalid allocation type!\n");
678             return false;
679            
680         case AllocFree:
681             debugWarning(" Channel %d not registered\n", c);
682             return false;
683            
684         case AllocGeneric:
685             debugOutput(DEBUG_LEVEL_VERBOSE, " allocated using generic routine...\n" );
686             if (unregisterIsoChannel(c)) {
687                 return false;
688             } else {
689                 debugOutput(DEBUG_LEVEL_VERBOSE, " freeing %d bandwidth units...\n", m_channels[c].bandwidth );
690                 if (raw1394_bandwidth_modify(m_handle, m_channels[c].bandwidth, RAW1394_MODIFY_FREE) !=0) {
691                     debugWarning("Failed to deallocate bandwidth\n");
692                 }
693                 debugOutput(DEBUG_LEVEL_VERBOSE, " freeing channel %d...\n", m_channels[c].channel );
694                 if (raw1394_channel_modify (m_handle, m_channels[c].channel, RAW1394_MODIFY_FREE) != 0) {
695                     debugWarning("Failed to free channel\n");
696                 }
697                 return true;
698             }
699            
700         case AllocCMP:
701             debugOutput(DEBUG_LEVEL_VERBOSE, " allocated using IEC61883 CMP...\n" );
702             if (unregisterIsoChannel(c)) {
703                 return false;
704             } else {
705                 debugOutput(DEBUG_LEVEL_VERBOSE, " performing IEC61883 CMP disconnect...\n" );
706                 if(iec61883_cmp_disconnect(
707                         m_handle,
708                         m_channels[c].xmit_node | 0xffc0,
709                         m_channels[c].xmit_plug,
710                         m_channels[c].recv_node | 0xffc0,
711                         m_channels[c].recv_plug,
712                         m_channels[c].channel,
713                         m_channels[c].bandwidth) != 0) {
714                     debugWarning("Could not do CMP disconnect for channel %d!\n",c);
715                 }
716             }
717             return true;
718     }
719    
720     // unreachable
721     debugError("BUG: unreachable code reached!\n");
722    
723     return false;
724 }
725
726 /**
727  * Registers a channel as managed by this ieee1394service
728  * @param c channel number
729  * @param cinfo channel info struct
730  * @return true if successful
731  */
732 bool Ieee1394Service::registerIsoChannel(unsigned int c, struct ChannelInfo cinfo) {
733     if (c < 63) {
734         if (m_channels[c].alloctype != AllocFree) {
735             debugWarning("Channel %d already registered with bandwidth %d\n",
736                 m_channels[c].channel, m_channels[c].bandwidth);
737         }
738        
739         memcpy(&m_channels[c], &cinfo, sizeof(struct ChannelInfo));
740        
741     } else return false;
742     return true;
743 }
744
745 /**
746  * unegisters a channel from this ieee1394service
747  * @param c channel number
748  * @return true if successful
749  */
750 bool Ieee1394Service::unregisterIsoChannel(unsigned int c) {
751     if (c < 63) {
752         if (m_channels[c].alloctype == AllocFree) {
753             debugWarning("Channel %d not registered\n", c);
754             return false;
755         }
756        
757         m_channels[c].channel=-1;
758         m_channels[c].bandwidth=-1;
759         m_channels[c].alloctype=AllocFree;
760         m_channels[c].xmit_node=0xFFFF;
761         m_channels[c].xmit_plug=-1;
762         m_channels[c].recv_node=0xFFFF;
763         m_channels[c].recv_plug=-1;
764        
765     } else return false;
766     return true;
767 }
768
769 /**
770  * Returns the current value of the `bandwidth available' register on
771  * the IRM, or -1 on error.
772  * @return
773  */
774 signed int Ieee1394Service::getAvailableBandwidth() {
775     quadlet_t buffer;
776     signed int result = raw1394_read (m_handle, raw1394_get_irm_id (m_handle),
777         CSR_REGISTER_BASE + CSR_BANDWIDTH_AVAILABLE,
778         sizeof (quadlet_t), &buffer);
779
780     if (result < 0)
781         return -1;
782     return ntohl(buffer);
783 }
Note: See TracBrowser for help on using the browser.