Debian Codegraph @

src/cmp.c

raw
1 /*
2 * libiec61883 - Linux IEEE 1394 streaming media library.
3 * Copyright (C) 2004 Kristian Hogsberg, Dan Dennedy, and Dan Maas.
4 * This file written by Dan Dennedy.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include "iec61883.h"
25 #include "iec61883-private.h"
26 #include "cooked.h"
27
28 #include <stdio.h>
29 #include <libraw1394/csr.h>
30 #include <netinet/in.h>
31
32
33 int
34 iec61883_cmp_calc_bandwidth (raw1394handle_t handle, nodeid_t from, int plug,
35 int speed)
36 {
37 struct iec61883_oMPR ompr;
38 struct iec61883_oPCR opcr;
39 int bwu = -1; // failure
40
41 if (iec61883_get_oMPR (handle, from, &ompr) < 0) {
42 WARN ("%s: Failed to get the oMPR plug for node %d.", __FUNCTION__,
43 (int) from & 0x3f);
44 } else if (ompr.n_plugs == 0) {
45 WARN ("%s: The transmitting device (%d) does not have any output plugs.",
46 __FUNCTION__, (int) from & 0x3f);
47 } else if (plug < ompr.n_plugs && plug < IEC61883_PCR_MAX) {
48 if (iec61883_get_oPCRX (handle, from, &opcr, plug) < 0) {
49 WARN ("%s: Failed to get the oPCR[%d] plug for node %d.", __FUNCTION__,
50 plug, (int) from & 0x3f);
51 } else {
52 if (speed < 0 || speed > 2)
53 speed = opcr.data_rate;
54 if (opcr.overhead_id > 0)
55 bwu = (opcr.overhead_id * 32) + (opcr.payload + 3) * (1 << (2 - speed)) * 4;
56 else
57 bwu = 512 + (opcr.payload + 3) * (1 << (2 - speed)) * 4;
58 }
59 }
60
61 return bwu;
62 }
63
64 int
65 iec61883_cmp_create_p2p (raw1394handle_t handle,
66 nodeid_t output_node, int output_plug,
67 nodeid_t input_node, int input_plug,
68 unsigned int channel, unsigned int speed)
69 {
70 struct iec61883_oPCR opcr, save_opcr;
71 struct iec61883_iPCR ipcr;
72
73 DEBUG ("%s", __FUNCTION__);
74
75 if (iec61883_get_oPCRX (handle, output_node, &opcr, output_plug) < 0) {
76 WARN ("%s: Failed to get the oPCR[%d] plug for node %d.", __FUNCTION__,
77 output_plug, (int) output_node & 0x3f);
78 return -1;
79 }
80 if (iec61883_get_iPCRX (handle, input_node, &ipcr, input_plug) < 0) {
81 WARN ("%s: Failed to get the iPCR[%d] plug for node %d.", __FUNCTION__,
82 input_plug, (int) input_node & 0x3f);
83 return -1;
84 }
85
86 save_opcr = opcr;
87 opcr.channel = ipcr.channel = channel;
88 opcr.data_rate = speed;
89 if (opcr.n_p2p_connections < 63)
90 opcr.n_p2p_connections++;
91 if (ipcr.n_p2p_connections < 63)
92 ipcr.n_p2p_connections++;
93
94 if (iec61883_set_oPCRX (handle, output_node, opcr, output_plug) < 0) {
95 WARN ("%s: Failed to set the oPCR[%d] plug for node %d.", __FUNCTION__,
96 output_plug, (int) output_node & 0x3f);
97 return -1;
98 }
99 if (iec61883_set_iPCRX (handle, input_node, ipcr, input_plug) < 0) {
100 WARN ("%s: Failed to set the iPCR[%d] plug for node %d.", __FUNCTION__,
101 input_plug, (int) input_node & 0x3f);
102 // Undo changes on the oPCR
103 if (iec61883_set_oPCRX (handle, output_node, save_opcr, output_plug) < 0) {
104 WARN ("%s: Failed to undo changes on the oPCR[%d] plug for node %d.",
105 __FUNCTION__, output_plug, (int) output_node & 0x3f);
106 }
107 return -1;
108 }
109
110 return 0;
111 }
112
113 int
114 iec61883_cmp_create_p2p_output (raw1394handle_t handle,
115 nodeid_t output_node, int output_plug,
116 unsigned int channel, unsigned int speed)
117 {
118 struct iec61883_oPCR opcr;
119
120 DEBUG ("%s", __FUNCTION__);
121
122 if (iec61883_get_oPCRX (handle, output_node, &opcr, output_plug) < 0) {
123 WARN ("%s: Failed to get the oPCR[%d] plug for node %d.", __FUNCTION__,
124 output_plug, (int) output_node & 0x3f);
125 return -1;
126 }
127
128 opcr.channel = channel;
129 opcr.data_rate = speed;
130 if (opcr.n_p2p_connections < 63)
131 opcr.n_p2p_connections++;
132
133 if (iec61883_set_oPCRX (handle, output_node, opcr, output_plug) < 0) {
134 WARN ("%s: Failed to set the oPCR[%d] plug for node %d.", __FUNCTION__,
135 output_plug, (int) output_node & 0x3f);
136 return -1;
137 }
138
139 return 0;
140 }
141
142 int
143 iec61883_cmp_create_p2p_input (raw1394handle_t handle,
144 nodeid_t input_node, int input_plug,
145 unsigned int channel)
146 {
147 struct iec61883_iPCR ipcr;
148
149 DEBUG ("%s", __FUNCTION__);
150
151 if (iec61883_get_iPCRX (handle, input_node, &ipcr, input_plug) < 0) {
152 WARN ("%s: Failed to get the iPCR[%d] plug for node %d.", __FUNCTION__,
153 input_plug, (int) input_node & 0x3f);
154 return -1;
155 }
156
157 ipcr.channel = channel;
158 if (ipcr.n_p2p_connections < 63)
159 ipcr.n_p2p_connections++;
160
161 if (iec61883_set_iPCRX (handle, input_node, ipcr, input_plug) < 0) {
162 WARN ("%s: Failed to set the iPCR[%d] plug for node %d.", __FUNCTION__,
163 input_plug, (int) input_node & 0x3f);
164 return -1;
165 }
166
167 return 0;
168 }
169
170 int
171 iec61883_cmp_create_bcast (raw1394handle_t handle,
172 nodeid_t output_node, int output_plug,
173 nodeid_t input_node, int input_plug,
174 unsigned int channel, unsigned int speed)
175 {
176 struct iec61883_oPCR opcr, save_opcr;
177 struct iec61883_iPCR ipcr;
178
179 DEBUG ("%s", __FUNCTION__);
180
181 if (iec61883_get_oPCRX (handle, output_node, &opcr, output_plug) < 0) {
182 WARN ("%s: Failed to get the oPCR[%d] plug for node %d.", __FUNCTION__,
183 output_plug, (int) output_node & 0x3f);
184 return -1;
185 }
186 if (iec61883_get_iPCRX (handle, input_node, &ipcr, input_plug) < 0) {
187 WARN ("%s: Failed to get the iPCR[%d] plug for node %d.", __FUNCTION__,
188 input_plug, (int) input_node & 0x3f);
189 return -1;
190 }
191
192 save_opcr = opcr;
193 opcr.channel = ipcr.channel = channel;
194 opcr.data_rate = speed;
195 opcr.bcast_connection = 1;
196 ipcr.bcast_connection = 1;
197
198 if (iec61883_set_oPCRX (handle, output_node, opcr, output_plug) < 0) {
199 WARN ("%s: Failed to set the oPCR[%d] plug for node %d.", __FUNCTION__,
200 output_plug, (int) output_node & 0x3f);
201 return -1;
202 }
203 if (iec61883_set_iPCRX (handle, input_node, ipcr, input_plug) < 0) {
204 WARN ("%s: Failed to set the iPCR[%d] plug for node %d.", __FUNCTION__,
205 input_plug, (int) input_node & 0x3f);
206 // Undo changes on the oPCR
207 if (iec61883_set_oPCRX (handle, output_node, save_opcr, output_plug) < 0) {
208 WARN ("%s: Failed to undo changes on the oPCR[%d] plug for node %d.",
209 __FUNCTION__, output_plug, (int) output_node & 0x3f);
210 }
211 return -1;
212 }
213
214 return 0;
215 }
216
217 int
218 iec61883_cmp_create_bcast_output (raw1394handle_t handle,
219 nodeid_t output_node, int output_plug,
220 unsigned int channel, unsigned int speed)
221 {
222 struct iec61883_oPCR opcr;
223
224 DEBUG ("%s", __FUNCTION__);
225
226 if (iec61883_get_oPCRX (handle, output_node, &opcr, output_plug) < 0) {
227 WARN ("%s: Failed to get the oPCR[%d] plug for node %d.", __FUNCTION__,
228 output_plug, (int) output_node & 0x3f);
229 return -1;
230 }
231
232 opcr.channel = channel;
233 opcr.data_rate = speed;
234 opcr.bcast_connection = 1;
235
236 if (iec61883_set_oPCRX (handle, output_node, opcr, output_plug) < 0) {
237 WARN ("%s: Failed to set the oPCR[%d] plug for node %d.", __FUNCTION__,
238 output_plug, (int) output_node & 0x3f);
239 return -1;
240 }
241
242 return 0;
243 }
244
245 int
246 iec61883_cmp_create_bcast_input (raw1394handle_t handle,
247 nodeid_t input_node, int input_plug,
248 unsigned int channel)
249 {
250 struct iec61883_iPCR ipcr;
251
252 DEBUG ("%s", __FUNCTION__);
253
254 if (iec61883_get_iPCRX (handle, input_node, &ipcr, input_plug) < 0) {
255 WARN ("%s: Failed to get the iPCR[%d] plug for node %d.", __FUNCTION__,
256 input_plug, (int) input_node & 0x3f);
257 return -1;
258 }
259
260 ipcr.channel = channel;
261 ipcr.bcast_connection = 1;
262
263 if (iec61883_set_iPCRX (handle, input_node, ipcr, input_plug) < 0) {
264 WARN ("%s: Failed to set the iPCR[%d] plug for node %d.", __FUNCTION__,
265 input_plug, (int) input_node & 0x3f);
266 return -1;
267 }
268
269 return 0;
270 }
271
272 int
273 iec61883_cmp_overlay_p2p (raw1394handle_t handle,
274 nodeid_t output_node, int output_plug,
275 nodeid_t input_node, int input_plug)
276 {
277 struct iec61883_oPCR opcr, save_opcr;
278 struct iec61883_iPCR ipcr;
279
280 DEBUG ("%s", __FUNCTION__);
281
282 if (iec61883_get_oPCRX (handle, output_node, &opcr, output_plug) < 0) {
283 WARN ("%s: Failed to get the oPCR[%d] plug for node %d.", __FUNCTION__,
284 output_plug, (int) output_node & 0x3f);
285 return -1;
286 }
287 if (iec61883_get_iPCRX (handle, input_node, &ipcr, input_plug) < 0) {
288 WARN ("%s: Failed to get the iPCR[%d] plug for node %d.", __FUNCTION__,
289 input_plug, (int) input_node & 0x3f);
290 return -1;
291 }
292
293 if (opcr.bcast_connection == 0) {
294 save_opcr = opcr;
295 if (opcr.n_p2p_connections < 63)
296 opcr.n_p2p_connections++;
297 }
298 if (ipcr.bcast_connection == 0)
299 if (ipcr.n_p2p_connections < 63)
300 ipcr.n_p2p_connections++;
301
302 if (iec61883_set_oPCRX (handle, output_node, opcr, output_plug) < 0) {
303 WARN ("%s: Failed to set the oPCR[%d] plug for node %d.", __FUNCTION__,
304 output_plug, (int) output_node & 0x3f);
305 return -1;
306 }
307 if (iec61883_set_iPCRX (handle, input_node, ipcr, input_plug) < 0) {
308 WARN ("%s: Failed to set the iPCR[%d] plug for node %d.", __FUNCTION__,
309 input_plug, (int) input_node & 0x3f);
310 // Undo changes on the oPCR
311 if (iec61883_set_oPCRX (handle, output_node, save_opcr, output_plug) < 0) {
312 WARN ("%s: Failed to undo changes on the oPCR[%d] plug for node %d.",
313 __FUNCTION__, output_plug, (int) output_node & 0x3f);
314 }
315 return -1;
316 }
317
318 return 0;
319 }
320
321 int
322 iec61883_cmp_overlay_p2p_output (raw1394handle_t handle,
323 nodeid_t output_node, int output_plug)
324 {
325 struct iec61883_oPCR opcr;
326
327 DEBUG ("%s", __FUNCTION__);
328
329 if (iec61883_get_oPCRX (handle, output_node, &opcr, output_plug) < 0) {
330 WARN ("%s: Failed to get the oPCR[%d] plug for node %d.", __FUNCTION__,
331 output_plug, (int) output_node & 0x3f);
332 return -1;
333 }
334
335 if (opcr.bcast_connection == 0) {
336 if (opcr.n_p2p_connections < 63)
337 opcr.n_p2p_connections++;
338
339 if (iec61883_set_oPCRX (handle, output_node, opcr, output_plug) < 0) {
340 WARN ("%s: Failed to set the oPCR[%d] plug for node %d.", __FUNCTION__,
341 output_plug, (int) output_node & 0x3f);
342 return -1;
343 }
344 }
345
346 return 0;
347 }
348
349 int
350 iec61883_cmp_overlay_p2p_input (raw1394handle_t handle,
351 nodeid_t input_node, int input_plug)
352 {
353 struct iec61883_iPCR ipcr;
354
355 DEBUG ("%s", __FUNCTION__);
356
357 if (iec61883_get_iPCRX (handle, input_node, &ipcr, input_plug) < 0) {
358 WARN ("%s: Failed to get the iPCR[%d] plug for node %d.", __FUNCTION__,
359 input_plug, (int) input_node & 0x3f);
360 return -1;
361 }
362
363 if (ipcr.bcast_connection == 0) {
364 if (ipcr.n_p2p_connections < 63)
365 ipcr.n_p2p_connections++;
366
367 if (iec61883_set_iPCRX (handle, input_node, ipcr, input_plug) < 0) {
368 WARN ("%s: Failed to set the iPCR[%d] plug for node %d.", __FUNCTION__,
369 input_plug, (int) input_node & 0x3f);
370 return -1;
371 }
372 }
373
374 return 0;
375 }
376
377 int
378 iec61883_cmp_overlay_bcast (raw1394handle_t handle,
379 nodeid_t output_node, int output_plug,
380 nodeid_t input_node, int input_plug)
381 {
382 struct iec61883_oPCR opcr, save_opcr;
383 struct iec61883_iPCR ipcr;
384
385 DEBUG ("%s", __FUNCTION__);
386
387 if (iec61883_get_oPCRX (handle, output_node, &opcr, output_plug) < 0) {
388 WARN ("%s: Failed to get the oPCR[%d] plug for node %d.", __FUNCTION__,
389 output_plug, (int) output_node & 0x3f);
390 return -1;
391 }
392 if (iec61883_get_iPCRX (handle, input_node, &ipcr, input_plug) < 0) {
393 WARN ("%s: Failed to get the iPCR[%d] plug for node %d.", __FUNCTION__,
394 input_plug, (int) input_node & 0x3f);
395 return -1;
396 }
397
398 save_opcr = opcr;
399 // We still need this function because only one of the plugs might have the
400 // bcast_connection set.
401 opcr.bcast_connection = 1;
402 ipcr.bcast_connection = 1;
403
404 if (iec61883_set_oPCRX (handle, output_node, opcr, output_plug) < 0) {
405 WARN ("%s: Failed to set the oPCR[%d] plug for node %d.", __FUNCTION__,
406 output_plug, (int) output_node & 0x3f);
407 return -1;
408 }
409 if (iec61883_set_iPCRX (handle, input_node, ipcr, input_plug) < 0) {
410 WARN ("%s: Failed to set the iPCR[%d] plug for node %d.", __FUNCTION__,
411 input_plug, (int) input_node & 0x3f);
412 // Undo changes on the oPCR
413 if (iec61883_set_oPCRX (handle, output_node, save_opcr, output_plug) < 0) {
414 WARN ("%s: Failed to undo changes on the oPCR[%d] plug for node %d.",
415 __FUNCTION__, output_plug, (int) output_node & 0x3f);
416 }
417 return -1;
418 }
419
420 return 0;
421 }
422
423 static int
424 allocate_channel (raw1394handle_t handle)
425 {
426 int c = -1;
427
428 for (c = 0; c < 63; c++)
429 if (raw1394_channel_modify (handle, c, RAW1394_MODIFY_ALLOC) == 0)
430 break;
431
432 DEBUG ("%s: %d", __FUNCTION__, c);
433
434 return c;
435 }
436
437 /**
438 * cmp_connect - establish, re-establish or overlay connection automatically
439 * @handle: a libraw1394 handle
440 * @output: node id of the transmitter
441 * @oplug: the output plug to use. If -1, find the first online plug, and
442 * upon return, contains the plug number used.
443 * @input: node id of the receiver
444 * @iplug: the input plug to use. If -1, find the first online plug, and
445 * upon return, contains the plug number used.
446 * @bandwidth: an input/output parameter. As an input this is a boolean that
447 * indicates whether to perform bandwidth allocation. Supply 0 to skip bandwidth
448 * allocation; any other value permits it. Upon return, actual bandwidth allocation
449 * units allocated are returned, which you supply to the disconnect routine.
450 * @channel: if channel is < 0, the function will establish a new connection.
451 * oherwise, it will try to re-establish an existing connection.
452 *
453 * This is a high level function that attempts to be as smart as possible, but
454 * it gives point-to-point connections higher priority over broadcast connections.
455 * It can automatically handle situations where either @input and/or @output does
456 * not implement plug control registers. However, if one node implements plug
457 * registers it assumes the other node has some sort of manual channel selection
458 * (i.e., through software or a control panel).
459 *
460 * Returns:
461 * It returns the isochronous channel number selected or -1 if the function has
462 * failed for any reason.
463 **/
464 static int
465 cmp_connect (raw1394handle_t handle, nodeid_t output, int *oplug,
466 nodeid_t input, int *iplug, int *bandwidth, int channel)
467 {
468 struct iec61883_oMPR ompr;
469 struct iec61883_iMPR impr;
470 struct iec61883_oPCR opcr;
471 struct iec61883_iPCR ipcr;
472 int oplug_online = -1, iplug_online = -1;
473 int skip_bandwidth = (*bandwidth == 0);
474 int failure = 0;
475 int new_connection = (channel < 0);
476
477 DEBUG ("%s", __FUNCTION__);
478
479 *bandwidth = 0;
480
481 // Check for plugs on output
482 if (iec61883_get_oMPR (handle, output, &ompr) < 0)
483 ompr.n_plugs = 0;
484
485 // Check for plugs on input
486 if (iec61883_get_iMPR (handle, input, &impr) < 0)
487 impr.n_plugs = 0;
488
489 DEBUG ("output node %d #plugs=%d, input node %d #plugs=%d", output & 0x3f,
490 ompr.n_plugs, input & 0x3f, impr.n_plugs);
491
492 if (ompr.n_plugs > 0 && impr.n_plugs > 0) {
493 // establish or overlay point-to-point
494
495 // speed to use is lesser of output and input
496 unsigned int speed =
497 impr.data_rate < ompr.data_rate ? impr.data_rate : ompr.data_rate;
498
499 // determine if output has plug available
500 if (*oplug < 0) {
501 for (*oplug = 0; *oplug < ompr.n_plugs; (*oplug)++) {
502 if (iec61883_get_oPCRX (handle, output, &opcr, *oplug) == 0) {
503 // get first online plug
504 if (oplug_online == -1 && opcr.online)
505 oplug_online = *oplug;
506 if (opcr.online && opcr.n_p2p_connections == 0)
507 break;
508 }
509 }
510 } else if (iec61883_get_oPCRX (handle, output, &opcr, *oplug) < 0)
511 FAIL ("Failed to get plug %d for output node", *oplug);
512
513 // determine if input has plug available
514 if (*iplug < 0) {
515 for (*iplug = 0; *iplug < impr.n_plugs; (*iplug)++) {
516 if (iec61883_get_iPCRX (handle, input, &ipcr, *iplug) == 0) {
517 // get first online plug
518 if (iplug_online == -1 && ipcr.online)
519 iplug_online = *iplug;
520 if (ipcr.online && ipcr.n_p2p_connections == 0)
521 break;
522 }
523 }
524 } else if (iec61883_get_iPCRX (handle, input, &ipcr, *iplug) < 0)
525 FAIL ("Failed to get plug %d for input node", *iplug);
526
527 if (*oplug < ompr.n_plugs && *iplug < impr.n_plugs) {
528
529 if (opcr.bcast_connection == 1) {
530 channel = opcr.channel;
531 iec61883_cmp_overlay_bcast (handle, output, *oplug, input, *iplug);
532 } else {
533 // allocate bandwidth
534 if (!skip_bandwidth) {
535 *bandwidth = iec61883_cmp_calc_bandwidth (handle, output, *oplug, speed);
536 if (*bandwidth < 1) {
537 WARN ("Failed to calculate bandwidth.");
538 failure = 1;
539 } else if (raw1394_bandwidth_modify (handle, *bandwidth, RAW1394_MODIFY_ALLOC) < 0) {
540 WARN ("Failed to allocate bandwidth.");
541 failure = 1;
542 }
543 }
544 if (!failure) {
545 if (new_connection) {
546 channel = allocate_channel (handle);
547 } else {
548 raw1394_channel_modify (handle, channel, RAW1394_MODIFY_ALLOC);
549 }
550 if (iec61883_cmp_create_p2p (handle, output, *oplug, input, *iplug,
551 channel, speed) < 0) {
552 // release channel and bandwidth
553 failure = raw1394_channel_modify (handle, channel, RAW1394_MODIFY_FREE);
554 if (!failure)
555 raw1394_bandwidth_modify (handle, *bandwidth, RAW1394_MODIFY_FREE);
556 channel = -1;
557 }
558 }
559 }
560
561 } else if (*iplug < impr.n_plugs && oplug_online > -1 ) {
562 // get the channel from output - can not start another transmission
563 // on an existing channel, but can receive from multiple nodes/plugs
564 *oplug = oplug_online;
565 if (iec61883_get_oPCRX (handle, output, &opcr, oplug_online) == 0) {
566 channel = opcr.channel;
567 if (opcr.bcast_connection == 1) {
568 iec61883_cmp_overlay_bcast (handle, output, oplug_online, input, *iplug);
569 } else {
570 if (iec61883_cmp_create_p2p_input (handle, input, *iplug, channel) < 0)
571 channel = -1;
572 else if (iec61883_cmp_overlay_p2p_output (handle, output, oplug_online) < 0)
573 channel = -1;
574 }
575 }
576
577 } else if (oplug_online != -1 && iplug_online > -1) {
578 // get channel from output
579 *oplug = oplug_online;
580 if (iec61883_get_oPCRX (handle, output, &opcr, oplug_online) == 0) {
581 channel = opcr.channel;
582 if (iec61883_cmp_overlay_p2p (handle, output, oplug_online,
583 input, iplug_online) < 0)
584 channel = -1;
585 }
586 } else {
587 WARN ("All the plugs on both nodes are offline!");
588 *oplug = *iplug = -1;
589 }
590
591 } else if (ompr.n_plugs > 0) {
592 // establish or overlay half point-to-point on output
593 *iplug = -1;
594
595 // determine if output has plug available
596 if (*oplug < 0) {
597 for (*oplug = 0; *oplug < ompr.n_plugs; (*oplug)++) {
598 if (iec61883_get_oPCRX (handle, output, &opcr, *oplug) == 0) {
599 // get first online plug
600 if (oplug_online == -1 && opcr.online)
601 oplug_online = *oplug;
602 if (opcr.online && opcr.n_p2p_connections == 0)
603 break;
604 }
605 }
606 } else if (iec61883_get_oPCRX (handle, output, &opcr, *oplug) < 0)
607 FAIL ("Failed to get plug %d for output node", *oplug);
608
609 if (*oplug < ompr.n_plugs) {
610 if (opcr.bcast_connection == 1) {
611 channel = opcr.channel;
612 } else {
613 // establish
614 // XXX: the input must provide manual channel selection or we should
615 // do a broadcast. Example use case: DV device is output and local
616 // node is input, but software allows channel select. Failure use
617 // case: local node is output but input device has no channel selection!
618 // Both use cases are actually quite common. Should we provide a
619 // parameter to offer a hint in case operator knows something more
620 // about the device than firewire interfaces on it suggest?
621
622 // allocate bandwidth
623 if (!skip_bandwidth) {
624 *bandwidth = iec61883_cmp_calc_bandwidth (handle, output, *oplug, ompr.data_rate);
625 if (*bandwidth < 1) {
626 WARN ("Failed to calculate bandwidth.");
627 failure = 1;
628 } else if (raw1394_bandwidth_modify (handle, *bandwidth, RAW1394_MODIFY_ALLOC) < 0) {
629 WARN ("Failed to allocate bandwidth.");
630 failure = 1;
631 }
632 }
633 if (!failure) {
634 if( new_connection ) {
635 channel = allocate_channel (handle);
636 } else {
637 raw1394_channel_modify (handle, channel, RAW1394_MODIFY_ALLOC);
638 }
639 if (iec61883_cmp_create_p2p_output (handle, output, *oplug,
640 channel, ompr.data_rate) == 0) {
641 DEBUG ("Established connection on channel %d.\n"
642 "You may need to manually set the channel on the receiving node.",
643 channel);
644 } else {
645 // release channel and bandwidth
646 failure = raw1394_channel_modify (handle, channel, RAW1394_MODIFY_FREE);
647 if (!failure)
648 raw1394_bandwidth_modify (handle, *bandwidth, RAW1394_MODIFY_FREE);
649 channel = -1;
650 }
651 }
652 }
653
654 } else if (oplug_online > -1) {
655 // overlay
656 // get channel from output
657 *oplug = oplug_online;
658 if (iec61883_get_oPCRX (handle, output, &opcr, oplug_online) == 0) {
659 channel = opcr.channel;
660 if (opcr.bcast_connection != 1)
661 if (iec61883_cmp_overlay_p2p_output (handle, output, oplug_online) < 0)
662 channel = -1;
663 }
664 DEBUG ("Overlayed connection on channel %d.\n"
665 "You may need to manually set the channel on the receiving node.",
666 channel);
667 } else {
668 WARN ("Transmission node has no plugs online!");
669 *oplug = -1;
670 // failover to broadcast
671 // allocate bandwidth based upon first out plug
672 if (!skip_bandwidth) {
673 *bandwidth = iec61883_cmp_calc_bandwidth (handle, output, 0, ompr.data_rate);
674 if (*bandwidth < 1) {
675 WARN ("Failed to calculate bandwidth.");
676 failure = 1;
677 } else if (raw1394_bandwidth_modify (handle, *bandwidth, RAW1394_MODIFY_ALLOC) < 0) {
678 WARN ("Failed to allocate bandwidth.");
679 failure = 1;
680 }
681 }
682 if (!failure) {
683 if (raw1394_channel_modify (handle, ompr.bcast_channel, RAW1394_MODIFY_ALLOC) == 0)
684 channel = ompr.bcast_channel;
685 }
686 }
687
688 } else if (impr.n_plugs > 0) {
689 // establish or overlay half point-to-point on input
690 *oplug = -1;
691
692 // determine if input has plug available
693 if (*iplug < 0) {
694 for (*iplug = 0; *iplug < impr.n_plugs; (*iplug)++) {
695 if (iec61883_get_iPCRX (handle, input, &ipcr, *iplug) == 0) {
696 // get first online plug
697 if (iplug_online == -1 && ipcr.online)
698 iplug_online = *iplug;
699 if (ipcr.online && ipcr.n_p2p_connections == 0)
700 break;
701 }
702 }
703 } else if (iec61883_get_iPCRX (handle, input, &ipcr, *iplug) < 0)
704 FAIL ("Failed to get plug %d for input node", *iplug);
705
706 if (*iplug < impr.n_plugs) {
707 if (ipcr.bcast_connection == 1) {
708 channel = ipcr.channel;
709 } else {
710 // establish
711
712 // allocate bandwidth
713 // cannot accurately allocate bandwidth with no output plug
714 // use an output plug on the input device as a best guess
715 if (!skip_bandwidth) {
716 *bandwidth = iec61883_cmp_calc_bandwidth (handle, input, *iplug, -1);
717 if (*bandwidth < 1) {
718 WARN ("Failed to calculate bandwidth.");
719 failure = 1;
720 } else if (raw1394_bandwidth_modify (handle, *bandwidth, RAW1394_MODIFY_ALLOC) < 0) {
721 WARN ("Failed to allocate bandwidth.");
722 failure = 1;
723 }
724 }
725 if (!failure) {
726 if( new_connection ) {
727 channel = allocate_channel (handle);
728 } else {
729 raw1394_channel_modify (handle, channel, RAW1394_MODIFY_ALLOC);
730 }
731 if (iec61883_cmp_create_p2p_input (handle, input, *iplug, channel) == 0) {
732 DEBUG ("Established connection on channel %d.\n"
733 "You may need to manually set the channel on the transmitting node.",
734 channel);
735 } else {
736 // release channel and bandwidth
737 failure = raw1394_channel_modify (handle, channel, RAW1394_MODIFY_FREE);
738 if (!failure)
739 raw1394_bandwidth_modify (handle, *bandwidth, RAW1394_MODIFY_FREE);
740 channel = -1;
741 }
742 }
743 }
744
745 } else if (iplug_online > -1) {
746 // overlay
747 // get channel from input
748 *iplug = iplug_online;
749 if (iec61883_get_iPCRX (handle, input, &ipcr, iplug_online) == 0) {
750 channel = ipcr.channel;
751 if (ipcr.bcast_connection != 1)
752 if (iec61883_cmp_overlay_p2p_input (handle, input, iplug_online) < 0)
753 channel = -1;
754 }
755 DEBUG ("Overlayed connection on channel %d.\n"
756 "You may need to manually set the channel on the transmitting node.",
757 channel);
758 } else {
759 WARN ("Receiving node has no plugs online!");
760 // failover to broadcast
761 // allocate bandwidth based upon first input plug
762 *iplug = -1;
763
764 if (!skip_bandwidth) {
765 *bandwidth = iec61883_cmp_calc_bandwidth (handle, input, 0, impr.data_rate);
766 if (*bandwidth < 1) {
767 WARN ("Failed to calculate bandwidth.");
768 failure = 1;
769 } else if (raw1394_bandwidth_modify (handle, *bandwidth, RAW1394_MODIFY_ALLOC) < 0) {
770 WARN ("Failed to allocate bandwidth.");
771 failure = 1;
772 }
773 }
774 if (!failure) {
775 if (raw1394_channel_modify (handle, 63, RAW1394_MODIFY_ALLOC) == 0)
776 channel = 63;
777 }
778 }
779
780 } else {
781 // no input or output plugs - failover broadcast on channel 63
782 // not enough information to calculate bandwidth
783 *oplug = *iplug = -1;
784 if (raw1394_channel_modify (handle, 63, RAW1394_MODIFY_ALLOC) == 0)
785 channel = 63;
786 if (channel == 63)
787 WARN ("No plugs exist on either node; using default broadcast channel 63.");
788 }
789
790 return channel;
791 }
792
793 int
794 iec61883_cmp_reconnect (raw1394handle_t handle, nodeid_t output, int *oplug,
795 nodeid_t input, int *iplug, int *bandwidth, int channel)
796 {
797 /* Passing an existing channel means it is a reconnection. */
798 return cmp_connect (handle, output, oplug, input, iplug, bandwidth, channel);
799 }
800
801 int
802 iec61883_cmp_connect (raw1394handle_t handle, nodeid_t output, int *oplug,
803 nodeid_t input, int *iplug, int *bandwidth)
804 {
805 /* Passing "-1" as the channel means it is a new connection. */
806 return cmp_connect (handle, output, oplug, input, iplug, bandwidth, -1);
807 }
808
809 int
810 iec61883_cmp_disconnect (raw1394handle_t handle, nodeid_t output, int oplug,
811 nodeid_t input, int iplug, unsigned int channel, unsigned int bandwidth)
812 {
813 struct iec61883_oMPR ompr;
814 struct iec61883_iMPR impr;
815 struct iec61883_oPCR opcr;
816 struct iec61883_iPCR ipcr;
817 int result = 0;
818
819 DEBUG ("%s: oplug %d iplug %d channel %u bw %u", __FUNCTION__,
820 oplug, iplug, channel, bandwidth);
821
822 // Check for plugs on output
823 if (iec61883_get_oMPR (handle, output, &ompr) < 0)
824 ompr.n_plugs = 0;
825
826 // Check for plugs on input
827 if (iec61883_get_iMPR (handle, input, &impr) < 0)
828 impr.n_plugs = 0;
829
830 if (ompr.n_plugs > 0 && impr.n_plugs > 0) {
831 // establish or overlay point-to-point
832
833 // determine if output has plug available
834 if (oplug < 0) {
835 for (oplug = 0; oplug < ompr.n_plugs; oplug++) {
836 if (iec61883_get_oPCRX (handle, output, &opcr, oplug) == 0) {
837 if (opcr.online && opcr.channel == channel)
838 break;
839 }
840 }
841 } else if (iec61883_get_oPCRX (handle, output, &opcr, oplug) < 0)
842 FAIL ("Failed to get plug %d for output node", oplug);
843
844 // determine if input has plug available
845 if (iplug < 0) {
846 for (iplug = 0; iplug < impr.n_plugs; iplug++) {
847 if (iec61883_get_iPCRX (handle, input, &ipcr, iplug) == 0) {
848 if (ipcr.online && ipcr.channel == channel)
849 break;
850 }
851 }
852 } else if (iec61883_get_iPCRX (handle, input, &ipcr, iplug) < 0)
853 FAIL ("Failed to get plug %d for input node", iplug);
854
855 if (oplug != ompr.n_plugs) {
856 if (opcr.n_p2p_connections > 0) {
857 opcr.n_p2p_connections--;
858 if (opcr.n_p2p_connections == 0)
859 opcr.channel = ompr.bcast_channel;
860 result = iec61883_set_oPCRX (handle, output, opcr, oplug);
861 if (result == 0) {
862 if (opcr.n_p2p_connections == 0) {
863 // release channel and bandwidth
864 result = raw1394_channel_modify (handle, channel, RAW1394_MODIFY_FREE);
865 if (result == 0)
866 result = raw1394_bandwidth_modify (handle, bandwidth, RAW1394_MODIFY_FREE);
867 }
868 }
869 }
870 }
871 if (iplug != impr.n_plugs) {
872 if (ipcr.n_p2p_connections > 0) {
873 ipcr.n_p2p_connections--;
874 // receiver connection count does not affect iso resource management
875 result = iec61883_set_iPCRX (handle, input, ipcr, iplug);
876 } else if (ipcr.bcast_connection == 1) {
877 ipcr.bcast_connection = 0;
878 ipcr.channel = ompr.bcast_channel;
879 result = iec61883_set_iPCRX (handle, input, ipcr, iplug);
880 }
881 }
882 if (oplug == ompr.n_plugs && iplug == impr.n_plugs)
883 result = -1;
884
885 } else if (ompr.n_plugs > 0) {
886 // establish or overlay half point-to-point on output
887
888 // determine if output has plug available
889 if (oplug < 0) {
890 for (oplug = 0; oplug < ompr.n_plugs; oplug++) {
891 if (iec61883_get_oPCRX (handle, output, &opcr, oplug) == 0) {
892 if (opcr.online && opcr.channel == channel)
893 break;
894 }
895 }
896 } else if (iec61883_get_oPCRX (handle, output, &opcr, oplug) < 0)
897 FAIL ("Failed to get plug %d for output node", oplug);
898
899 if (oplug != ompr.n_plugs) {
900 if (opcr.n_p2p_connections > 0) {
901 opcr.n_p2p_connections--;
902 if (opcr.n_p2p_connections == 0)
903 opcr.channel = ompr.bcast_channel;
904 result = iec61883_set_oPCRX (handle, output, opcr, oplug);
905 if (result == 0 && opcr.n_p2p_connections == 0) {
906 // release channel and bandwidth
907 result = raw1394_channel_modify (handle, channel, RAW1394_MODIFY_FREE);
908 if (result == 0)
909 result = raw1394_bandwidth_modify (handle, bandwidth, RAW1394_MODIFY_FREE);
910 }
911 }
912 } else {
913 // release channel and bandwidth
914 result = raw1394_channel_modify (handle, channel, RAW1394_MODIFY_FREE);
915 if (result == 0)
916 result = raw1394_bandwidth_modify (handle, bandwidth, RAW1394_MODIFY_FREE);
917 }
918
919 } else if (impr.n_plugs > 0) {
920 // establish or overlay half point-to-point on input
921
922 // determine if input has plug available
923 if (iplug < 0) {
924 for (iplug = 0; iplug < impr.n_plugs; iplug++) {
925 if (iec61883_get_iPCRX (handle, input, &ipcr, iplug) == 0) {
926 if (ipcr.online && ipcr.channel == channel)
927 break;
928 }
929 }
930 } else if (iec61883_get_iPCRX (handle, input, &ipcr, iplug) < 0)
931 FAIL ("Failed to get plug %d for input node", iplug);
932
933 if (iplug != impr.n_plugs) {
934 if (ipcr.n_p2p_connections > 0) {
935 ipcr.n_p2p_connections--;
936 if (ipcr.n_p2p_connections == 0)
937 ipcr.channel = 63;
938 // Normally, changes on receiver connection count does not
939 // affect iso resource management. However, in this special
940 // half-way management mode, we need to in order to allow
941 // multiple capture sessions.
942 result = iec61883_set_iPCRX (handle, input, ipcr, iplug);
943 if (result == 0 && ipcr.n_p2p_connections == 0) {
944 // release channel and bandwidth
945 result = raw1394_channel_modify (handle, channel, RAW1394_MODIFY_FREE);
946 if (result == 0)
947 result = raw1394_bandwidth_modify (handle, bandwidth, RAW1394_MODIFY_FREE);
948 }
949 }
950 } else {
951 // release channel and bandwidth
952 result = raw1394_channel_modify (handle, channel, RAW1394_MODIFY_FREE);
953 if (result == 0)
954 result = raw1394_bandwidth_modify (handle, bandwidth, RAW1394_MODIFY_FREE);
955 }
956
957 } else {
958 // no input or output plugs - failover broadcast on channel 63
959 // just release channel
960 result = raw1394_channel_modify (handle, 63, RAW1394_MODIFY_FREE);
961 }
962
963 return result;
964 }
965
966 int
967 iec61883_cmp_normalize_output (raw1394handle_t handle, nodeid_t node)
968 {
969 struct iec61883_oMPR ompr;
970 struct iec61883_oPCR opcr;
971 int oplug;
972 int result = 0;
973
974 DEBUG ("iec61883_cmp_normalize_output: node %d\n", (int) node & 0x3f);
975
976 // Check for plugs on output
977 result = iec61883_get_oMPR (handle, node, &ompr);
978 if (result < 0)
979 return result;
980
981 // locate an ouput plug that has a connection
982 for (oplug = 0; oplug < ompr.n_plugs; oplug++) {
983 if (iec61883_get_oPCRX (handle, node, &opcr, oplug) == 0) {
984 if (opcr.online && (opcr.n_p2p_connections > 0 ||
985 opcr.bcast_connection == 1)) {
986
987 // Make sure the plug's channel is allocated with IRM
988 quadlet_t buffer;
989 nodeaddr_t addr = CSR_REGISTER_BASE;
990 unsigned int c = opcr.channel;
991 quadlet_t compare, swap = 0;
992 quadlet_t new;
993
994 if (c > 31 && c < 64) {
995 addr += CSR_CHANNELS_AVAILABLE_LO;
996 c -= 32;
997 } else if (c < 64)
998 addr += CSR_CHANNELS_AVAILABLE_HI;
999 else
1000 FAIL ("Invalid channel");
1001 c = 31 - c;
1002
1003 result = iec61883_cooked_read (handle, raw1394_get_irm_id (handle), addr,
1004 sizeof (quadlet_t), &buffer);
1005 if (result < 0)
1006 FAIL ("Failed to get channels available.");
1007
1008 buffer = ntohl (buffer);
1009 DEBUG ("channels available before: 0x%08x", buffer);
1010
1011 if ((buffer & (1 << c)) != 0) {
1012 swap = htonl (buffer & ~(1 << c));
1013 compare = htonl (buffer);
1014
1015 result = raw1394_lock (handle, raw1394_get_irm_id (handle), addr,
1016 EXTCODE_COMPARE_SWAP, swap, compare, &new);
1017 if ( (result < 0) || (new != compare) ) {
1018 FAIL ("Failed to modify channel %d", opcr.channel);
1019 }
1020 DEBUG ("channels available after: 0x%08x", ntohl (swap));
1021 }
1022 }
1023 }
1024 }
1025
1026 return result;
1027 }