x393_vsc330x.php 48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?php
/*!*******************************************************************************
*! FILE NAME  : x393_vsc330x.php
*! DESCRIPTION: web interface for VSC3304 crosspoint switch in 393 camera
*! Copyright (C) 2012 - 2016 Elphel, Inc
*! -----------------------------------------------------------------------------**
*!
*!  This program is free software: you can redistribute it and/or modify
*!  it under the terms of the GNU General Public License as published by
*!  the Free Software Foundation, either version 3 of the License, or
*!  (at your option) any later version.
*!
*!  This program is distributed in the hope that it will be useful,
*!  but WITHOUT ANY WARRANTY; without even the implied warranty of
*!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*!  GNU General Public License for more details.
*!
*!  You should have received a copy of the GNU General Public License
*!  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*! -----------------------------------------------------------------------------**
*/

$debug=false;
24
$vsc_sysfs_dir = '/sys/devices/soc0/amba@0/e0004000.ps7-i2c/i2c-0/0-0001';
25
$connections=array(); // pairs, first index< second
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

$pcb_connections = array(
		'ESATA_A' => 'A',
		'ESATA_B' => 'C',
		'SSD_A'   => 'E',
		'SSD_B'   => 'F',
		'ZYNQ_A'  => 'G',
		'ZYNQ_B'  => 'H'
);

// I/O pairs for the same physical port
$port_num = array(
		'A' => array('in' => 12, 'out' => 8),
		'B' => array('in' => 13, 'out' => 9),
		'C' => array('in' => 14, 'out' => 10),
		'D' => array('in' => 15, 'out' => 11),
		'E' => array('in' => 8,  'out' => 12),
		'F' => array('in' => 9,  'out' => 13),
		'G' => array('in' => 10, 'out' => 14),
		'H' => array('in' => 11, 'out' => 15)
);

$channels = array(
		array('in' => $port_num[$pcb_connections['ESATA_A']]['in'], 
			 'out' => $port_num[$pcb_connections['ESATA_B']]['out'],
			 'name'=>'ESATA', 'connector' => 'ESATA',
			 'phy_ports' => array('ESATA_A', 'ESATA_B')),
        array('in' => $port_num[$pcb_connections['SSD_B']]['in'],
        	 'out' => $port_num[$pcb_connections['SSD_A']]['out'],
        	 'name'=>'SSD',   'connector' => 'SSD',
        	 'phy_ports' => array('SSD_A', 'SSD_B')),
        array('in' => $port_num[$pcb_connections['ZYNQ_A']]['in'],
        	 'out' => $port_num[$pcb_connections['ZYNQ_B']]['out'],
        	 'name'=>'ZYNQ',  'connector' => 'ZYNQ',
        	 'phy_ports' => array('ZYNQ_A', 'ZYNQ_B')));

$vsc3304_connections = array(
		'ZYNQ<->SSD'   => array(array('FROM' => 'ZYNQ_A', 'TO'  => 'SSD_A'),
								array('FROM' => 'SSB_B',  'TO'  => 'ZYNQ_B')),
		'ZYNQ<->ESATA' => array(array('FROM' => 'ZYNQ_A', 'TO'  => 'ESATA_A'),
								array('FROM' => 'ESATA_B', 'TO' => 'ZYNQ_B')),
		'ZYNQ<->SSATA' => array(array('FROM' => 'ZYNQ_A', 'TO'  => 'ESATA_B'),
								array('FROM' => 'ESATA_A', 'TO' => 'ZYNQ_B')),
		'ESATA<->SSD'  => array(array('FROM' => 'SSD_B', 'TO'   => 'ESATA_B'),
								array('FROM' => 'ESATA_A', 'TO' => 'SSD_A'))
);

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
/** Paths to parameters in sysfs */
$param_paths = array(
		'input_ISE_short'        => $vsc_sysfs_dir . '/input_ISE_short/',
		'input_ISE_medium'       => $vsc_sysfs_dir . '/input_ISE_medium/',
		'input_ISE_long'         => $vsc_sysfs_dir . '/input_ISE_long/',
		'input_terminate'        => $vsc_sysfs_dir . '/input_terminate_high/',
		'input_invert'           => $vsc_sysfs_dir . '/input_state_invert/',
		'input_LOS'              => $vsc_sysfs_dir . '/input_LOS_threshold/',
		'input_off'              => $vsc_sysfs_dir . '/input_state_off/',
		
		'output_PRE_long_decay'  => $vsc_sysfs_dir . '/output_PRE_long_decay/',
		'output_PRE_long_level'  => $vsc_sysfs_dir . '/output_PRE_long_level/',
		'output_PRE_short_decay' => $vsc_sysfs_dir . '/output_PRE_short_decay/',
		'output_PRE_short_level' => $vsc_sysfs_dir . '/output_PRE_short_level/',
		'output_level'           => $vsc_sysfs_dir . '/output_level/',
		'output_mode'            => $vsc_sysfs_dir . '/output_mode/',
		'forward_OOB'            => $vsc_sysfs_dir . '/forward_OOB/',
		'status'                 => $vsc_sysfs_dir . '/status/',
		'connections'            => $vsc_sysfs_dir . '/connections/'
);
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

$default_out_levels = array(
		'ESATA_A' => 3,
		'ESATA_B' => 3,
		'SSD_B'   => 2,
		'ZYNQ_A'  => 2
);

$default_inverted_ports = array(
		'A', 'E', 'G', 'H'
);

if (count($_GET) == 0) {
	showUsage();
	exit(0);
108 109 110 111 112 113 114 115 116 117 118 119 120 121
}

$debug= isset($_GET['debug']);
$init= !isset($_GET['noinit']); // default - on
$error=false;
$port_ise =        array(); //-1=>'','','','','','','','','','','','',''); // -1(all),0..11
$port_input_state= array(); //-1=>'','','','','','','','','','','','',''); // -1(all),0..11
$port_los=         array(); //-1=>'','','','','','','','','','','','',''); // -1(all),0..11
$port_pre_long=    array(); //-1=>'','','','','','','','','','','','',''); // -1(all),0..11
$port_pre_short=   array(); //-1=>'','','','','','','','','','','','',''); // -1(all),0..11
$port_out_level=   array(); //-1=>'','','','','','','','','','','','',''); // -1(all),0..11
$port_out_state=   array(); //-1=>'','','','','','','','','','','','',''); // -1(all),0..11
$port_channel_status=array(); // read only - 1 - LOS
$port_channel_input=  array(); // read current connections (number of input or "-1" - disabled
122 123 124 125 126 127 128 129 130 131
// define the types of data in arrays
$port_ise['type']            = 'in';
$port_input_state['type']    = 'in';
$port_los['type']            = 'in';
$port_pre_long['type']       = 'out';
$port_pre_short['type']      = 'out';
$port_out_level['type']      = 'out';
$port_out_state['type']      = 'out';
$port_channel_status['type'] = 'in';
$port_channel_input['type']  = 'out';
132
if ($init) {
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
 	$port_ise[-1]=         array('short'=>0,'medium'=>0,'long'=>0);
	$port_ise[-1]=         array('short'=>0,'medium'=>0,'long'=>0);
	$port_los[-1]=         array('level'=>4); // 250 mv
	$port_pre_long[-1]=    array('level'=>0,'decay'=>0);
	$port_pre_short[-1]=   array('level'=>0,'decay'=>0);
	
	// set the value that the register has after reset 
	$vals = array();
	for ($i = 0; $i < count($port_num); $i++)
		$vals[index_to_port_num($i)] = 1;
	// apply default values
	foreach ($default_out_levels as $phy_port => $level) {
		$index = $port_num[$pcb_connections[$phy_port]]['out'];
		$vals[$index] = $level;
	}
	$port_out_level[-1] = array('level' => $vals);
	
	// set the value that the register has after reset
	$vals = array();
	for ($i = 0; $i < count($port_num); $i++)
		$vals[index_to_port_num($i)] = 0;
	// apply default values
	foreach ($default_inverted_ports as $phy_port) {
		$index = $port_num[$phy_port]['in'];
		$vals[$index] = 1;
	}
	$port_input_state[-1] = array('terminate' => 0, 'invert' => $vals);
	
	// set the value that the register has after reset
	$vals = array();
	for ($i = 0; $i < count($port_num); $i++)
		$vals[index_to_port_num($i)] = 5;
	// apply default values
	foreach ($default_inverted_ports as $phy_port) {
		$index = $port_num[$phy_port]['out'];
		$vals[$index] = 10;
	}
	$port_out_state[-1] = array('mode' => $vals, 'oob' => 1);

172 173 174
}


175
update_chn_from_sysfs();
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
foreach ($_GET as $cmdkey=>$value) {
  if (strpos($key,":" )>=0){
    $command=strtok($cmdkey,":");
    $key=    strtok(":");
  } else {
    $command="";
    $key=$cmdkey;
  }
  $port=  parsePort($key);
  $inPort=   parseInPort($key);
  $outPort=  parseOutPort($key);
  if ($debug) {
    echo "<!--$command:$key=$value -->\n";
  }
  
  switch (strtoupper($command)) {
    case 'S':
    case 'STATE':
  echo <<<EOT
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title>SATA Multiplexer Current State</title>
</head>
<body>
EOT;
         readCurrentState(); // outputs comments, so <head> should be before it
         showCurrentStateHTML();
         echo "</body>\n";
         exit(0); 
    case 'CONNECTION':
    case 'CON':
    case 'C':
       $pair=array($port,parsePort($value));
       sort($pair);
       if (($pair[0]>=0) &&
211 212
           ($pair[0]<count($channels)) &&
           ($pair[1]>=0) &&
213
           ($pair[1]<count($channels))) {
214
			// remove duplicate IO-s
215 216 217 218 219 220 221 222 223 224 225 226
           $duplicateIO=false;
           foreach ($connections as $connection) if (($connection[0]==$pair[0]) || ($connection[1]==$pair[1])){
              $duplicateIO=true;
              break;
           }
          if (!$duplicateIO) $connections[count($connections)]=$pair;
          else {
            echo "Duplicate connection for ".$key.": \n";
            print_r($pair);
            echo "Current connections:\n";
            print_r($connections);
          }
227
          set_channels($key, $value, $GLOBALS['vsc3304_connections'], $GLOBALS['port_num'], $GLOBALS['pcb_connections'], $channels);
228 229 230 231 232 233 234 235 236 237
       }
       break;
    case 'ISE':
      $aval=getMultiVals($value);
      if (count($aval)!=3) {
        echo "Value for the ISE (input signal equalization) command is expected to be short_value:medium_value:long_value, got >$value<\n";
        $error =true;
        exit (1);
      }
      $this_ise=array('short'=>$aval[0],'medium'=>$aval[1],'long'=>$aval[2]);
238
      if (($inPort>=-1) && ($inPort <= max_port_num('in'))) $port_ise[$inPort]=$this_ise;
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
      else {
        echo "Invalid input port index=$inPort\n";
        $error =true;
        exit (1);
      }
      if ($debug) {
      }
    break;
    case 'IN_STATE':
      $aval=getMultiVals($value);
      if (count($aval)!=2) {
        echo "Value for the IN_STATE (input state) command is expected to be terminate_to_VCC:invert, got >$value<\n";
        $error =true;
        exit (1);
      }
      $this_input_state=array('terminate'=>$aval[0],'invert'=>$aval[1]);
255
      if (($inPort>=-1) && ($inPort <= max_port_num('in'))) $port_input_state[$inPort]=$this_input_state;
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
      else {
        echo "Invalid input port index=$inPort\n";
        $error =true;
        exit (1);
      }
      if ($debug) {
      }
    break;
    case 'LOS':
      $aval=getMultiVals($value);
      if ((count($aval)!=1) || ($aval[0]<0)  || ($aval[0]>7)) {
        echo "Value for the LOS (loss of signal) command is expected to be a 0..7 integer, got >$value<\n";
        $error =true;
        exit (1);
      }
271
      if (($inPort>=-1) && ($inPort <= max_port_num('in'))) $port_los[$inPort]=array('level'=>$aval[0]);
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
      else {
        echo "Invalid input port index=$inPort\n";
        $error =true;
        exit (1);
      }
      if ($debug) {
      }
    break;

    case 'PRE_LONG':
      $aval=getMultiVals($value);
      if (count($aval)!=2) {
        echo "Value for the PE_LONG (output pre-emphasis long time constant) command is expected to be pre_emphasis_level[0..15]:pre_emphasis_decay[0..7], got >$value<\n";
        $error =true;
        exit (1);
      }
      $this_pre_long=array('level'=>$aval[0],'decay'=>$aval[1]);
289
      if (($outPort>=-1) && ($outPort <= max_port_num($port_pre_long['type']))) $port_pre_long[$outPort]=$this_pre_long;
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
      else {
        echo "Invalid output port index=$outPort\n";
        $error =true;
        exit (1);
      }
      if ($debug) {
      }
    break;
    case 'PRE_SHORT':
      $aval=getMultiVals($value);
      if (count($aval)!=2) {
        echo "Value for the PE_SHORT (output pre-emphasis short time constant) command is expected to be pre_emphasis_level[0..15]:pre_emphasis_decay[0..7], got >$value<\n";
        $error =true;
        exit (1);
      }
      $this_pre_short=array('level'=>$aval[0],'decay'=>$aval[1]);
306
      if (($outPort>=-1) && ($outPort <= max_port_num($port_pre_short['type']))) $port_pre_short[$outPort]=$this_pre_short;
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
      else {
        echo "Invalid output port index=$outPort\n";
        $error =true;
        exit (1);
      }
      if ($debug) {
      }
    break;
    case 'OUT_LEVEL':
      $aval=getMultiVals($value);
      if ((count($aval)!=1) || ($aval[0]<0) || ($aval[0]>15)) {
        echo "Value for the OUT_LEVEL (output signal level) command is expected to be  0..15 value, got >$value<\n";
        $error =true;
        exit (1);
      }
      $this_out_level=array('level'=>$aval[0]);
323
      if (($outPort>=-1) && ($outPort <= max_port_num($port_out_level['type']))) $port_out_level[$outPort]=$this_out_level;
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
      else {
        echo "Invalid output port index=$outPort\n";
        $error =true;
        exit (1);
      }
      if ($debug) {
      }
    break;
//$port_out_state=   array(-1=>array('mode'=>5,'oob'=>1),'','','','','','','','','','','',''); // -1(all),0..11
    case 'OUT_STATE':
      $aval=getMultiVals($value);
      if (count($aval)!=2) {
        echo "Value for the OUT_STATE (output state) command is expected to be mode(10-inverted,5-normal,0-common mode):oob_forwarding, got >$value<\n";
        $error =true;
        exit (1);
      }
      $this_out_state=array('mode'=>$aval[0],'oob'=>$aval[1]);
341
      if (($outPort>=-1) && ($outPort <= max_port_num($port_out_state['type']))) $port_out_state[$outPort]=$this_out_state;
342 343 344 345 346 347 348 349 350 351
      else {
        echo "Invalid output port index=$outPort\n";
        $error =true;
        exit (1);
      }
      if ($debug) {
      }
    break;
  }
}
352 353 354 355 356 357 358 359 360 361 362 363
	
if (isset($_GET['list']))
	listSettings();
foreach ($port_num as $pn) {
	$activeOutputs[$pn['out']] = false;
	$activeInputs[$pn['in']] = false;
};
foreach ($connections as $connection) {
	$activeOutputs[$channels[$connection[0]]['out']] = true;
	$activeOutputs[$channels[$connection[1]]['out']] = true;
	$activeInputs[$channels[$connection[0]]['in']] = true;
	$activeInputs[$channels[$connection[1]]['in']] = true;
364 365
}
if ($debug) {
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
	echo "<!-- activeOutputs:\n";
	print_r($activeOutputs);
	echo "\nactiveInputs:\n";
	print_r($activeInputs);
	echo "\nISE (input signal equalization):\n";
	print_r($port_ise);
	
	echo "\nInput state:\n";
	print_r($port_input_state);
	echo "\nLOS (loss of signal thershold):\n";
	print_r($port_los);
	
	echo "\nPre-emphasis long:\n";
	print_r($port_pre_long);
	echo "\nPre-emphasis short:\n";
	print_r($port_pre_short);
	echo "\nOutput level:\n";
	print_r($port_out_level);
	echo "\nOutput state:\n";
	print_r($port_out_state);
	echo "-->\n";
387 388
}

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
// program ISE
if ($debug)
	echo "<!-- program ISE -->\n";
if (isGlobalSet($port_ise)) {
	$all_ise_short = $port_ise[-1]['short'];
	write_vals($param_paths['input_ISE_short'] . 'all', $all_ise_short);
	$all_ise_medium = $port_ise[-1]['medium'];
	write_vals($param_paths['input_ISE_medium'] . 'all', $all_ise_medium);
	$all_ise_long = $port_ise[-1]['long'];
	write_vals($param_paths['input_ISE_long'] . 'all', $all_ise_long);
}
if (isIndividualSet($port_ise)) {
	for ($index = 0; $index < count($channels); $index++) {
		$port_num = $channels[$index][$port_ise['type']];
		if (isset($port_ise[$port_num])) {
			write_vals($param_paths['input_ISE_short'] . port_fn($port_num), $port_ise[$port_num]['short']);
			write_vals($param_paths['input_ISE_medium'] . port_fn($port_num), $port_ise[$port_num]['medium']);
			write_vals($param_paths['input_ISE_long'] . port_fn($port_num), $port_ise[$port_num]['long']);
		}
	}
}
	
// program InputState
if ($debug)
	echo "<!-- program InputState -->\n";
$default_inv = array();
if (isGlobalSet($port_input_state)) {
	// default polarity invertion can be specified without init, it will be applied for inputs that 
	// are programmed anyway
	$default_inv = data_input_invert(-1);
}
// scan all inputs and disable/inable them only in init mode
for ($index = 0; $index < count($channels); $index++) {
	$port_num = $channels[$index][$port_input_state['type']];
	$power_on = $activeInputs[$channels[$index]['in']] || isset($port_input_state[$port_num]); // programming input implies it is on
	if (!empty($default_inv))
		$invert_value = $default_inv[$port_num];
	else
		$invert_value = 0;
	if (isset($port_input_state[$port_num])) {
		$invert_value = data_input_invert($port_num);
	}
	if ($init || $power_on) {
		$power_value = ($power_on) ? 0 : 1;
		write_vals($param_paths['input_off'] . port_fn($port_num), $power_value);
		write_vals($param_paths['input_invert'] . port_fn($port_num), $invert_value);
	}
}
437

438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
// program input termination
if ($debug)
	echo "<!-- program input termination -->\n";
if (isGlobalSet($port_input_state)) {
	$all_terminate = data_input_terminate(-1);
	write_vals($param_paths['input_terminate'] . 'all', $all_terminate);
}
if (isIndividualSet($port_input_state)) {
	for ($index = 0; $index < count($channels); $index++) {
		$port_num = $channels[$index][$port_input_state['type']];
		if (isset($port_input_state[$port_num])) {
			write_vals($param_paths['input_terminate'] . port_fn($port_num), data_input_terminate($port_num));
		}
	}
}
453

454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
// program LOS
if ($debug)
	echo "<!-- program LOS -->\n";
if (isGlobalSet($port_los)) {
	$all_los = data_port_los(-1);
	write_vals($param_paths['input_LOS'] . 'all', $all_los);
}
if (isIndividualSet($port_los)) {
	for ($index = 0; $index < count($channels); $index ++) {
		$port_num = $channels[$index][$port_los['type']];
		if (isset($port_los[$port_num])) {
			write_vals($param_paths['input_LOS'] . port_fn($port_num), data_port_los($port_num));
		}
	}
}
469

470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
// program pre-emphasis (long)
if ($debug)
	echo "<!-- program pre-emphasis (long) -->\n";
if (isGlobalSet($port_pre_long)) {
	$all_pre_long_decay = data_pre_long_decay(-1);
	$all_pre_long_level = data_pre_long_level(-1);
	write_vals($param_paths['output_PRE_long_decay'] . 'all', $all_pre_long_decay);
	write_vals($param_paths['output_PRE_long_level'] . 'all', $all_pre_long_level);
}
if (isIndividualSet($port_pre_long)) {
	for ($index = 0; $index < count($channels); $index++) {
		$port_num = $channels[$index][$port_pre_long['type']];
		if (isset($port_pre_long[$port_num])) {
			write_vals($param_paths['output_PRE_long_decay'] . port_fn($port_num), data_pre_long_decay($port_num));
			write_vals($param_paths['output_PRE_long_level'] . port_fn($port_num), data_pre_long_level($port_num));
		}
	}
}
488

489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
// program pre-emphasis (short)
if ($debug)
	echo "<!-- program pre-emphasis (short) -->\n";
if (isGlobalSet($port_pre_short)) {
	$all_pre_short_decay = data_pre_short_decay(-1);
	$all_pre_short_level = data_pre_short_level(-1);
	write_vals($param_paths['output_PRE_short_decay'] . 'all', $all_pre_short_decay);
	write_vals($param_paths['output_PRE_short_level'] . 'all', $all_pre_short_level);
}
if (isIndividualSet($port_pre_short)) {
	for ($index = 0; $index < count($channels); $index++) {
		$port_num = $channels[$index][$port_pre_short['type']];
		if (isset($port_pre_short[$port_num])) {
			write_vals($param_paths['output_PRE_short_decay'] . port_fn($port_num), data_pre_short_decay($port_num));
			write_vals($param_paths['output_PRE_short_level'] . port_fn($port_num), data_pre_short_level($port_num));
		}
	}
}
507

508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
// program output level
if ($debug)
	echo "<!-- program output level -->\n";
if (isGlobalSet($port_out_level)) {
	$all_out_level = data_out_level(-1);
	foreach ($all_out_level as $port_num => $level) {		
		write_vals($param_paths['output_level'] . port_fn($port_num), $level);
	}
}
if (isIndividualSet($port_out_level)) {
	for ($index = 0; $index < count($channels); $index++) {
		$port_num = $channels[$index][$port_out_level['type']];
		if (isset($port_out_level[$port_num])) {
			write_vals($param_paths['output_level'] . port_fn($port_num), data_out_level($port_num));
		}
	}
}
	
// program Output State
if ($debug)
	echo "<!-- program output state -->\n";
if (isGlobalSet($port_out_state)) {
	$all_out_state = data_out_state(-1);
	$all_oob_state = data_oob_state(-1);
	write_vals($param_paths['forward_OOB'] . 'all', $all_oob_state);
	foreach ($all_out_state as $port_num => $state) {
		write_vals($param_paths['output_mode'] . port_fn($port_num), $state);
	}
}
if (isIndividualSet($port_out_state)) {
	for ($index = 0; $index < count($channels); $index++) {
		$port_num = $channels[$index][$port_out_state['type']];
		if (isset($port_out_state[$port_num])) {
			write_vals($param_paths['output_mode'] . port_fn($port_num), data_out_state($port_num));
			write_vals($param_paths['forward_OOB'] . port_fn($port_num), data_oob_state($port_num));
		}
	}
}
546

547 548 549
if ($debug)
	echo "<!-- program connections($init) -->\n";
programConnections($init); // in init mode will disable unused outputs
550

551
exit(0);
552 553 554 555 556 557 558 559 560 561 562 563 564 565

function showUsage(){
  $script_name=trim($_SERVER['SCRIPT_NAME'],'/');
  $prefix_url='http://'.$_SERVER['SERVER_ADDR'].$_SERVER['SCRIPT_NAME'];
  echo <<<EOT
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title>Usage of 103697A SATA multiplexer control interface ($script_name)</title>
</head>

<h2>Usage of $script_name:</h2>
<h3>Commands w/o parameters</h3>
<ul>
 <li><i>list</i> - show settings to be programmed</li>
566
 <li><i>debug</i> - output additional information, such as the content of arrays, as HTML comments (visible with "view source" menu option in browser)</li>
567 568 569 570 571 572 573 574
 <li><i>noinit</i> - update only what is specified, do not disable unused i/o</li>
 <li><i>state</i> - show current programmed state of the multiplexer</li>
 <li><i><a href="$prefix_url?source">source</a></i> - show program source code (no other actions)</li>
</ul>
<h3>Commands with parameters</h3>
<p>All commands with parameters have format:</p>
<p><b>command:port=value</b>, where port can be specified in one of the following ways:</p>
<ul>
575
 <li><i>name</i> - name of a port as given in the "state" table header (i.e. SSD or ESATA)</li>
576 577 578
 <li><i>global</i> or <i>all</i> - apply to all ports (not valid for "connection" command).</ul>
<br/>
<h4>connection:port1=port2<br/>c:port1=port2</h4>
579 580
<p>Connect two ports. The order of the ports is arbitrary. If <i>noinit</i> does not appear in the url, all unused inputs and outputs will be disabled to 
 reduce power consumption.</p>
581 582
<br/>
<h4>ise:port=short_value:medium_value:long_value</h4>
583 584
<p>Configure ISE (input signal equalization) levels for short, medium and long time constants. Each value is in the range 0..3 (0- off, 1 - minimal, 
 2 - moderate, 3 - maximal)</p>
585 586
<br/>
<h4>in_state:port=terminate:invert</h4>
587 588
<p>Configure input port state. "terminate" (terminate input to VCC) can be either 0 (off) or 1 (on), "invert" (also 0/1) control inversion of 
 the input signal polarity</p>
589 590
<br/>
<h4>los:port=level</h4>
591
<p>Configure input LOS (loss of signal) threshold level</p>
592 593 594 595
<table border="1">
<tr><th>level</th><th>threshold</th></tr>
<tr><td>0</td><td>---</td></tr>
<tr><td>1</td><td>---</td></tr>
596 597 598 599
<tr><td>2</td><td>170 mV</td></tr>
<tr><td>3</td><td>230 mV</td></tr>
<tr><td>4</td><td>280 mV</td></tr>
<tr><td>5</td><td>330 mV</td></tr>
600 601 602 603 604
<tr><td>6</td><td>---</td></tr>
<tr><td>7</td><td>---</td></tr>
</table>
<br/>
<h4>pre_long:port=level:decay</h4>
605 606
<p>Output pre-emphasis with 0.5ns-1.5ns decay, where 4-bit level controls pre-emphasis amount from 0 (off) to 15 (6db), and decay - 3-bit decay, 
 0 corresponds to fastest (0.5ns) and 15 - slowest one (1.5ns).</p>
607 608
<br/>
<h4>pre_short:port=level:decay</h4>
609 610
<p>Output pre-emphasis with 0.03 ns-0.5 ns decay, where 4-bit level controls pre-emphasis amount from 0 (off) to 15 (6db), and decay - 3-bit decay, 
 0 corresponds to fastest (0.03ns) and 15 - slowest one (0.5ns).</p>
611 612 613
<br/>

<h4>out_level:port=level</h4>
614 615
<p>Programs output power level - peak-to-peak differential voltage. These values have to be reduced when pre-emphasis is used as the actual signal 
 adds the levels.</p>
616 617 618 619 620 621 622 623
<table border="1">
<tr><th>level</th><th>output voltage</th></tr>
<tr><td> 0</td><td>---</td></tr>
<tr><td> 1</td><td>---</td></tr>
<tr><td> 2</td><td>405 mV</td></tr>
<tr><td> 3</td><td>425 mV</td></tr>
<tr><td> 4</td><td>455 mV</td></tr>
<tr><td> 5</td><td>485 mV</td></tr>
624 625 626 627 628 629 630 631
<tr><td> 6</td><td>520 mV</td></tr>
<tr><td> 7</td><td>555 mV</td></tr>
<tr><td> 8</td><td>605 mV</td></tr>
<tr><td> 9</td><td>655 mV</td></tr>
<tr><td>10</td><td>720 mV</td></tr>
<tr><td>11</td><td>790 mV</td></tr>
<tr><td>12</td><td>890 mV</td></tr>
<tr><td>13</td><td>990 mV (3.3 V supply required)</td></tr>
632 633 634 635 636
<tr><td>14</td><td>---</td></tr>
<tr><td>15</td><td>---</td></tr>
</table>
<br/>
<h4>out_state:port=mode:oob_forwarding</h4>
637
<p>Controls output inversion and OOB forwarding. 'oob' of 1 enables, 0 - disables OOB forwarding and 'mode' can be one of</p>
638 639 640 641 642 643 644 645 646 647 648 649
<table border="1">
<tr><th>mode</th><th>Descrtiption</th></tr>
<tr><td>0</td><td>disabled</td></tr>
<tr><td>5</td><td>non-inverted</td></tr>
<tr><td>10</td><td>inverted</td></tr>
</table>

 

EOT;
}

650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
/** Read the current state of the switch and place data to global variables */
function readCurrentState()
{
	global $debug, $channels, $param_paths;
	global $activeInputs, $activeOutputs;
	global $port_ise, $port_input_state, $port_los, $port_pre_long, $port_pre_short, $port_out_level;
	global $port_out_state, $port_channel_status, $port_channel_input;
	
	// read ISE
	if ($debug)
		echo "<!-- read ISE -->\n";
	$ise_short = read_vals($param_paths['input_ISE_short'] . port_fn());
	$ise_medium = read_vals($param_paths['input_ISE_medium'] . port_fn());
	$ise_long = read_vals($param_paths['input_ISE_long'] . port_fn());
	for ($index = 0; $index < count($channels); $index++) {
665
		$port_index = translate_index($index, $port_ise['type']);
666
		if ($debug)
667 668 669 670 671 672 673 674
			echo "<!-- [" . $port_index . "] => " . 
			"short: " . $ise_short[$port_index] . ", " .
			"medium: " . $ise_medium[$port_index] . ", " .
			"long: " . $ise_long[$port_index] . " -->\n";
		$port_ise[$channels[$index][$port_ise['type']]] = array(
				'short' => $ise_short[$port_index],
				'medium' => $ise_medium[$port_index],
				'long' => $ise_long[$port_index]);
675 676 677 678 679 680 681 682 683
	}
	
	// read InputState
	if ($debug)
		echo "<!-- read InputState -->\n";
	$port_termination = read_vals($param_paths['input_terminate'] . port_fn());
	$port_invertion = read_vals($param_paths['input_invert'] . port_fn());
	$input_off = read_vals($param_paths['input_off'] . port_fn());
	for ($index = 0; $index < count($channels); $index++) {
684
		$port_index = translate_index($index, $port_input_state['type']);
685 686
		if ($debug)
			echo "<!-- [" . $index . "] => " . 
687 688 689 690 691 692 693
			"off: " . $input_off[$port_index] . ", " .
			"terminate: " . $port_termination[$port_index] . ", " .
			"invert: " . $port_invertion[$port_index] . " -->\n";
		$activeInputs[$channels[$index]['in']] = $input_off[$port_index] == 0;
		$port_input_state[$channels[$index][$port_input_state['type']]] = array(
				'terminate' => $port_termination[$port_index],
				'invert' => $port_invertion[$port_index]);
694 695 696 697 698 699 700
	}

	// read LOS
	if ($debug)
		echo "<!-- read LOS -->\n";
	$data = read_vals($param_paths['input_LOS'] . port_fn());
	for ($index = 0; $index < count($channels); $index++) {
701
		$port_index = translate_index($index, $port_los['type']);
702 703
		if ($debug)
			echo "<!-- [" . $index . "] => " .
704 705
			"level: " . $data[$port_index] . " -->\n";
		$port_los[$channels[$index][$port_los['type']]] = array('level' => $data[$port_index]);
706 707 708 709 710 711 712 713
	}

	// read pre-emphasis (long)
	if ($debug)
		echo "<!-- read pre-emphasis (long) -->\n";
	$data_level = read_vals($param_paths['output_PRE_long_level'] . port_fn());
	$data_decay = read_vals($param_paths['output_PRE_long_decay'] . port_fn());
	for ($index = 0; $index < count($channels); $index++) {
714
		$port_index = translate_index($index, $port_pre_long['type']);
715 716
		if ($debug)
			echo "<!-- [" . $index . "] => " .
717 718 719 720 721
			"level: " . $data_level[$port_index] . ", " .
			"decay: " . $data_decay[$port_index] . " -->\n";
		$port_pre_long[$channels[$index][$port_pre_long['type']]] = array(
				'level' => $data_level[$port_index],
				'decay' => $data_decay[$port_index]);
722 723 724 725 726 727 728 729
	}
	
	// read pre-emphasis (short)
	if ($debug)
		echo "<!-- read pre-emphasis (short) -->\n";
	$data_level = read_vals($param_paths['output_PRE_short_level'] . port_fn());
	$data_decay = read_vals($param_paths['output_PRE_short_decay'] . port_fn());
	for ($index = 0; $index < count($channels); $index++) {
730
		$port_index = translate_index($index, $port_pre_short['type']);
731 732
		if ($debug)
			echo "<!-- [" . $index . "] => " .
733 734 735 736 737
			"level: " . $data_level[$port_index] . ", " .
			"decay: " . $data_decay[$port_index] . " -->\n";
		$port_pre_short[$channels[$index][$port_pre_short['type']]] = array(
				'level' => $data_level[$port_index],
				'decay' => $data_decay[$port_index]);
738 739 740 741 742 743 744
	}

	// read output level
	if ($debug)
		echo "<!-- read output level -->\n";
	$data = read_vals($param_paths['output_level'] . port_fn());
	for ($index = 0; $index < count($channels); $index++) {
745
		$port_index = translate_index($index, $port_out_level['type']);
746 747
		if ($debug)
			echo "<!-- [" . $index . "] => " . 
748 749
			"level: " . $data[$port_index] . " -->\n";
		$port_out_level[$channels[$index][$port_out_level['type']]] = array('level' => $data[$port_index]);
750 751 752 753 754 755 756 757
	}
	
	// read OutputState
	if ($debug)
		echo "<!-- read output state -->\n";
	$data_mode = read_vals($param_paths['output_mode'] . port_fn());
	$data_oob = read_vals($param_paths['forward_OOB'] . port_fn());
	for ($index = 0; $index < count($channels); $index++) {
758
		$port_index = translate_index($index, $port_out_state['type']);
759 760
		if ($debug)
			echo "<!-- [" . $index . "] => ".
761 762 763 764 765
			"mode: " . $data_mode[$port_index] . ", " .
			"OOB: " .$data_oob[$port_index] . " -->\n";
		$port_out_state[$channels[$index][$port_out_state['type']]] = array(
				'mode' => $data_mode[$port_index],
				'oob' => $data_oob[$port_index]);
766 767 768 769 770 771 772
	}
	
	// read channel status
	if ($debug)
		echo "<!-- read channel status -->\n";
	$data = read_vals($param_paths['status'] . port_fn());
	for ($index = 0; $index < count($channels); $index++) {
773
		$port_index = translate_index($index, $port_channel_status['type']);
774 775
		if ($debug)
			echo "<!-- [" . $index . "]" .
776 777
			"status: " . $data[$port_index] . " -->\n";
		$port_channel_status[$channels[$index][$port_channel_status['type']]] = array('los' => $data[$port_index]);
778 779 780 781 782 783 784
	}

	// read connections
	if ($debug)
		echo "<!-- read connections -->\n";
	$data = read_vals($param_paths['connections'] . port_fn());
	for ($index = 0; $index < count($channels); $index++) {
785
		$port_index = translate_index($index, $port_channel_input['type']);
786 787
		if ($debug)
			echo "<!-- [" . $index . "] => ".
788 789 790 791 792
			"connection: " . $data[$port_index] . " -->\n";
		echo "<!-- port_index: " . $port_index . " -->\n";
		$val = (($data[$port_index] & 0x10) == 0) ? ($data[$port_index] & 0x0f) : -1;
		$port_channel_input[$channels[$index][$port_channel_input['type']]] = array('input' => $val);
		$activeOutputs[$channels[$index]['out']] = ($data[$port_index] & 0x10) == 0;
793 794
	}
}
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057

// TODO: add XML version
function values_los_dedicated($index){
   $values =array(
   'invalid', // 0
   'invalid', // 1
   '150 mV',  // 2
   '200 mV',  // 3
   '250 mV',  // 4
   '300 mV',  // 5
   'invalid', // 6
   'invalid');// 7
   return $values[$index];
}
function values_los_bidirectional($index){
   $values =array(
   'invalid', // 0
   'invalid', // 1
   '170 mV',  // 2
   '230 mV',  // 3
   '280 mV',  // 4
   '330 mV',  // 5
   'invalid', // 6
   'invalid');// 7
   return $values[$index];
}
function values_ise($index){
   $values=array(
   'Off', // 0
   'Minimum', // 1
   'Moderate', // 2
   'Maximal');// 3
   return $values[$index];
}
function values_pre_level($index,$digits=1){
   $range=pow(10,6.0/20);
   $gain=1.0+ ($index & 0xf)*($range-1.0)/15;
   $level=20*log10($gain);
   return sprintf('%'.($digits+2).'.'.$digits.'f dB',$index);
}
function values_pre_long_decay($index){
   $min=500;
   $max=1500;
   $decay=$min+($index & 0x7)*($max-$min)/7;
   return round($decay).' ps';
}
function values_pre_short_decay($index,$digits=1){
   $min=30;
   $max=500;
   $decay=$min+($index & 0x7)*($max-$min)/7;
   return round($decay).' ps';
}
function values_output_level($index){
   $values =array(
   'invalid', // 0
   'invalid', // 1
   '405 mV',  // 2
   '425 mV',  // 3
   '455 mV',  // 4
   '485 mV',  // 5
   '520 mV',  // 6
   '555 mV',  // 7
   '605 mV',  // 8
   '655 mV',  // 9
   '720 mV',  //10
   '790 mV',  //11
   '890 mV',  //12
   '990 mV',  //13
   'invalid', //14
   'invalid');//15
   return $values[$index];
}
function values_outout_state($index){
   switch ($index) {
     case 0: return 'supressed';
     case 5: return 'normal';
     case 10: return 'inverted';
     default: return 'undefined';
   }
}


function showCurrentStateHTML(){
 global $debug;
 global $activeInputs,$activeOutputs,$channels;
 global $port_ise,$port_input_state,$port_los,$port_pre_long,$port_pre_short,$port_out_level;
 global $port_out_state,$port_channel_status,$port_channel_input;
 echo "<center><h1>SATA Multiplexer Current State</h1></center>\n";
 echo "<table border=\"1\">\n";
   echo "<tr>\n";
    echo "<th>Name</th>";
    echo "<th>Range</th>";
    for ($index=0;$index<count($channels);$index++) echo '<th>'.$channels[$index]['name'].'</th>';
   echo "</tr>\n";
   echo "<tr>\n";
    echo "<th>Input</th>";
    echo "<td>&nbsp;</td>";
    for ($index=0;$index<count($channels);$index++) echo '<td>in-'.$channels[$index]['in'].'</td>';
   echo "</tr>\n";
   echo "<tr>\n";
    echo "<th>Output</th>";
    echo "<td>&nbsp;</td>";
    for ($index=0;$index<count($channels);$index++) echo '<td>out-'.$channels[$index]['out'].'</td>';
   echo "</tr>\n";
   echo "<tr>\n";
    if ($debug) {echo '<!-- port_channel_status:'; print_r($port_channel_status);echo "-->\n";}
    echo "<th>State</th>";
    echo "<td>LOS, active, ---</td>";
    for ($index=0;$index<count($channels);$index++){
        $los=(($port_channel_status[$channels[$index]['in']]['los']>0)?'LOS':'active');
        echo '<td>'.($activeInputs[$channels[$index]['in']]?$los:'---').'</td>';
    }
   echo "</tr>\n";
   echo "<tr>\n";
    if ($debug) {echo '<!-- port_channel_input:'; print_r($port_channel_input);echo "-->\n";}
    echo "<th>Input from</th>";
    echo "<td>&nbsp;</td>";
    for ($index=0;$index<count($channels);$index++) {
       $inChn=$port_channel_input[$channels[$index]['out']]['input'];
       $portIndex=inputChannelToIndex($inChn);
       echo '<td>'.(($inChn<0)?"---":(($portIndex>=0)?$channels[$portIndex]['name']:('?'.$inChn))).'</td>';
    }

   echo "</tr>\n";
   echo "<tr>\n";
    echo "<th>LOS threshold</th>";
    echo "<td>2-5</td>";
    if ($debug) {echo '<!-- port_los:'; print_r($port_los);echo "-->\n";}
    for ($index=0;$index<count($channels);$index++){
      $los_index=$port_los[$channels[$index]['in']]['level'];
      $los_value=($channels[$index]['in']>=8)?values_los_bidirectional($los_index):values_los_dedicated($los_index);
      echo '<td>'.$los_index.': '.$los_value.'</td>';
    }
   echo "</tr>\n";

   echo "<tr>\n";
    echo "<th>ISE short</th>";
    echo "<td>0-3</td>";
    if ($debug) {echo '<!-- port_ise:'; print_r($port_ise);echo "-->\n";}
    for ($index=0;$index<count($channels);$index++) {
      $ise_index=$port_ise[$channels[$index]['in']]['short'];
      echo '<td>'.$ise_index.': '.values_ise($ise_index).'</td>';
    }
   echo "</tr>\n";


   echo "<tr>\n";
    echo "<th>ISE medium</th>";
    echo "<td>0-3</td>";
    for ($index=0;$index<count($channels);$index++) {
      $ise_index=$port_ise[$channels[$index]['in']]['medium'];
      echo '<td>'.$ise_index.': '.values_ise($ise_index).'</td>';
    }
   echo "</tr>\n";

   echo "<tr>\n";
    echo "<th>ISE long</th>";
    echo "<td>0-3</td>";
    for ($index=0;$index<count($channels);$index++) {
      $ise_index=$port_ise[$channels[$index]['in']]['long'];
      echo '<td>'.$ise_index.': '.values_ise($ise_index).'</td>';
    }
   echo "</tr>\n";

   echo "<tr>\n";
    if ($debug) {echo '<!-- port_input_state:'; print_r($port_input_state);echo "-->\n";}
    echo "<th>Input terminate</th>";
    echo "<td>yes/no</td>";
    for ($index=0;$index<count($channels);$index++) echo '<td>'.(($port_input_state[$channels[$index]['in']]['terminate']>0)?'yes':'no').'</td>';
   echo "</tr>\n";

   echo "<tr>\n";
    echo "<th>Input invert</th>";
    echo "<td>yes/no</td>";
    for ($index=0;$index<count($channels);$index++) echo '<td>'.(($port_input_state[$channels[$index]['in']]['invert']>0)?'yes':'no').'</td>';
   echo "</tr>\n";

   echo "<tr>\n";
    echo "<th>Preemphasis level (long)</th>";
    echo "<td>0..15</td>";
    if ($debug) {echo '<!-- port_pre_long:'; print_r($port_pre_long);echo "-->\n";}
    for ($index=0;$index<count($channels);$index++){
      $level_index=$port_pre_long[$channels[$index]['out']]['level'];
      echo '<td>'.$level_index.': '.values_pre_level($level_index).'</td>';
    }
   echo "</tr>\n";

   echo "<tr>\n";
    echo "<th>Preemphasis decay (long)</th>";
    echo "<td>0..7</td>";
    for ($index=0;$index<count($channels);$index++){
      $decay_index=$port_pre_long[$channels[$index]['out']]['decay'];
      echo '<td>'.$decay_index.': '.values_pre_long_decay($decay_index).'</td>';
    }
   echo "</tr>\n";

   echo "<tr>\n";
    echo "<th>Preemphasis level (short)</th>";
    echo "<td>0..15</td>";
    if ($debug) {echo '<!-- port_pre_short:'; print_r($port_pre_short);echo "-->\n";}
    for ($index=0;$index<count($channels);$index++){
      $level_index=$port_pre_short[$channels[$index]['out']]['level'];
      echo '<td>'.$level_index.': '.values_pre_level($level_index).'</td>';
    }
   echo "</tr>\n";

   echo "<tr>\n";
    echo "<th>Preemphasis decay (short)</th>";
    echo "<td>0..7</td>";
    for ($index=0;$index<count($channels);$index++){
      $decay_index=$port_pre_short[$channels[$index]['out']]['decay'];
      echo '<td>'.$decay_index.': '.values_pre_short_decay($decay_index).'</td>';
    }
   echo "</tr>\n";

   echo "<tr>\n";
    echo "<th>Output level</th>";
    echo "<td>2..13</td>";
    if ($debug) {echo '<!-- port_out_level:'; print_r($port_out_level);echo "-->\n";}
    for ($index=0;$index<count($channels);$index++) {
      $level_index=$port_out_level[$channels[$index]['out']]['level'];
      echo '<td>'.$level_index.': '.values_output_level($level_index).'</td>';
    }
   echo "</tr>\n";

   echo "<tr>\n";
    echo "<th>Output mode</th>";
    echo "<td>0,5,10</td>";
    for ($index=0;$index<count($channels);$index++){
      $mode_index=$port_out_state[$channels[$index]['out']]['mode'];
      echo '<td>'.$mode_index.': '.values_outout_state($mode_index).'</td>';
    }
   echo "</tr>\n";
   echo "<tr>\n";
    echo "<th>OOB forward</th>";
    echo "<td>On/Off</td>";
    for ($index=0;$index<count($channels);$index++) echo '<td>'.(($port_out_state[$channels[$index]['out']]['oob']>0)?'On':'Off').'</td>';
   echo "</tr>\n";
 echo "</table>\n";
}

function inputChannelToIndex($inChn) {
   global $channels;
   for ($index=0;$index<count($channels);$index++) if ($channels[$index]['in']== $inChn) return $index;
   return -1;
}
function outChannelToIndex($outChn) {
   global $channels;
   for ($index=0;$index<count($channels);$index++) if ($channels[$index]['out']== $outChn) return $index;
   return -1;
}

function data_ise($index){
   global $port_ise;
   return (($port_ise[$index]['short'] & 3)<<4) | (($port_ise[$index]['medium'] & 3)<<2) | ($port_ise[$index]['long'] & 3);
}

function data_input_state($index){ // need special treatment for ports 8..11 and combining with active input?
   global $port_input_state,$activeInputs;
   $poweroff=($index<0) || !$activeInputs[$index]; // poweroff if all, individual - inactive
   return (($port_input_state[$index]['terminate'] >0)?0:4) | (($port_input_state[$index]['invert'] >0)?1:0) | ($poweroff? 2:0);
}

1058 1059 1060
function data_port_los($index)
{
	return $GLOBALS['port_los'][$index]['level'] & 0x07;
1061 1062 1063 1064 1065 1066 1067
}

function data_pre_long($index){
   global $port_pre_long;
   return (($port_pre_long[$index]['level'] & 0x0f)<<3) | ($port_pre_long[$index]['decay'] & 7);
}

1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
function data_pre_long_decay($index)
{
	return $GLOBALS['port_pre_long'][$index]['decay'] & 0x07;
}
function data_pre_long_level($index)
{
	return $GLOBALS['port_pre_long'][$index]['level'] & 0x0f;
}

function data_pre_short_decay($index)
{
	return $GLOBALS['port_pre_short'][$index]['decay'] & 0x07;
}
function data_pre_short_level($index)
{
	return $GLOBALS['port_pre_short'][$index]['level'] & 0x0f;
}

1086 1087 1088 1089 1090
function data_pre_short($index){
   global $port_pre_short;
   return (($port_pre_short[$index]['level'] & 0x0f)<<3) | ($port_pre_short[$index]['decay'] & 7);
}

1091 1092 1093 1094 1095 1096 1097
function data_out_level($index)
{
	if ($index == -1)
		// return an array of values for each port
		return $GLOBALS['port_out_level'][$index]['level'];
	else
		return $GLOBALS['port_out_level'][$index]['level'] & 0x0f;
1098 1099
}

1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
function data_input_terminate($index)
{
	return $GLOBALS['port_input_state'][$index]['terminate'] & 0x01;
}

function data_input_invert($index)
{
	if ($index == -1)
		// return an array of values for each port
		return $GLOBALS['port_input_state'][$index]['invert'];
	else
		return $GLOBALS['port_input_state'][$index]['invert'] & 0x01;
}

function data_out_state($index)
{
	if ($index == -1)
		// return an array of values for each port
		return $GLOBALS['port_out_state'][$index]['mode'];
	else
		return $GLOBALS['port_out_state'][$index]['mode'] & 0x0f;
}
function data_oob_state($index)
{
	return $GLOBALS['port_out_state'][$index]['oob'] & 0x01;
1125 1126 1127 1128 1129
}




1130 1131 1132 1133 1134 1135 1136 1137
function isIndividualSet($array)
{
	for($i = 0; $i < count($GLOBALS["channels"]); $i++) {
		$port_num = $GLOBALS['channels'][$i][$array['type']];
		if (isset($array[$port_num]))
			return true;
	}
	return false;
1138
}
1139 1140 1141 1142 1143 1144

function isGlobalSet($array)
{
	if (isset($array[-1]))
		return true;
	return false;
1145 1146 1147 1148 1149 1150 1151 1152 1153
}

function getMultiVals($value){
   $aval=explode(':',$value);
   for ($i=0;$i<count($aval);$i++) $aval[$i]+=0;
   return $aval;
}


1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
function programConnections($disableUnused)
{
	global $debug, $channels, $connections, $activeOutputs, $activeInputs; // in init mode all the unused connections will be powered down
	$disable_in = 0x01;
	$disable_conn = 0x10;

	// disable all inputs and reset all connections first
	foreach ($connections as $connection) {
		if ($debug) {
			echo '<!-- connection in[' . $channels[$connection[0]]['in'] . '] -> out[' . $channels[$connection[1]]['out'] . ']-->' . "\n";
			echo '<!-- connection in[' . $channels[$connection[1]]['in'] . '] -> out[' . $channels[$connection[0]]['out'] . ']-->' . "\n";
		}
		// inable inputs
		write_vals($GLOBALS['param_paths']['input_off'] . port_fn($channels[$connection[0]]['in']), 0);
		write_vals($GLOBALS['param_paths']['input_off'] . port_fn($channels[$connection[1]]['in']), 0);
		// set connections
		write_vals($GLOBALS['param_paths']['connections'] . port_fn($channels[$connection[0]]['out']), $channels[$connection[1]]['in']);
		write_vals($GLOBALS['param_paths']['connections'] . port_fn($channels[$connection[1]]['out']), $channels[$connection[0]]['in']);
	}
	// disable unused outputs and inputs
	if ($disableUnused) {
		foreach ($activeOutputs as $port => $value) {
			if (!$value) {
				if ($debug)
					echo '<!-- disabling unused output ' . $port . ' -->' . "\n";
				write_vals($GLOBALS['param_paths']['connections'] . port_fn($port), $disable_conn);
			}
		}
		foreach ($activeInputs as $port => $value) {
			if (!$value) {
				if ($debug)
					echo '<!-- disabling unused input ' . $port . ' -->' . "\n";
				write_vals($GLOBALS['param_paths']['input_off'] . port_fn($port), $disable_in);
			}
		}
	}
}
1191 1192 1193 1194 1195 1196


function listSettings(){
  global $debug,$channels,$connections;
    echo "<h4>Connections</h4>\n<ul>";
  foreach ($connections as $connection) {
1197 1198
    echo '<li>'.$channels[$connection[0]]['name'].' ( '.$channels[$connection[0]]['connector'].' ) <==> '.
                $channels[$connection[1]]['name'].' ( '.$channels[$connection[1]]['connector'].' ):   '."\t".
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
                'in['.$channels[$connection[0]]['in'].'] -> out['.$channels[$connection[1]]['out'].'];   '."\t". 
                'in['.$channels[$connection[1]]['in'].'] -> out['.$channels[$connection[0]]['out'].'] </li>'."\n";
  }
  echo "</ul>\n";
}
function parsePort($name){
  global $debug,$channels;
  if ((strtolower($name)=='global') || (strtolower($name)=='all')) {
    return -1;
  }
1209
  for ($i=0;$i<count($channels);$i++) if ((strtolower($name)==strtolower($channels[$i]['name'])) || (strtolower($name)==(strtolower($channels[$i]['connector'])))){
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
     return $i;
  }
  $a=sscanf($name,"%d");
  return ($a[0]); 
}

function parseInPort($name){
  global $debug,$channels;
  if ((strtolower($name)=='global') || (strtolower($name)=='all')) {
    return -1;
  }
1221
  for ($i=0;$i<count($channels);$i++) if ((strtolower($name)==strtolower($channels[$i]['name'])) || (strtolower($name)==(strtolower($channels[$i]['connector'])))){
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
     return $channels[$i]['in'];
  }
  $a=sscanf($name,"%d");
  return ($a[0]); 
}
function parseOutPort($name){
  global $debug,$channels;
  if ((strtolower($name)=='global') || (strtolower($name)=='all')) {
    return -1;
  }
1232
  for ($i=0;$i<count($channels);$i++) if ((strtolower($name)==strtolower($channels[$i]['name'])) || (strtolower($name)==(strtolower($channels[$i]['connector'])))){
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
     return $channels[$i]['out'];
  }
  $a=sscanf($name,"%d");
  return ($a[0]); 
}


function exitError($text){
   global $debug;
   if (!$debug) echo "<pre>\n";
   $debug=true;
   echo $text;
   echo "</pre>\n";
   exit (1);
}

1249 1250 1251 1252
/** Create port file name from port number */
function port_fn($port_num = -1)
{
	if ($port_num == -1)
1253
		$name = "all";
1254
	else
1255 1256
		$name = sprintf("port_%02d", $port_num);
	return $name;
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
}

/** Read one string from sysfs file, split it and return an array of values */
function read_vals($file_name)
{
	$substr = array();
	$f = fopen($file_name, 'r');
	if ($f !== false) {
		if (($data = fgets($f)) !== false) {
			$data = str_replace("\n", "", $data);
			$substr = preg_split("/ +/", $data);
		}		
		fclose($f);
	}
	
	return $substr;
}

1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
/** Write a parameter or set of parameters to sysfs file. $vals is an array containing 
 * parameters to set or a single value. If $vals contais more than one parameter then $file_name should be 'all' */
function write_vals($file_name, $vals)
{
	$f = fopen($file_name, 'w');
	if ($f !== false) {
		if (is_array($vals))
			$str = implode(' ', $vals);
		else 
			$str = (string)$vals;
		$num = fwrite($f, $str);
		fclose($f);
	}
}

1290 1291 1292 1293
/** Find port index in the channels list by zero based index given */
function translate_index($index, $dir)
{
	$offset = 8;                  // VSC3304 port numbering starts from 08, the offset is used to minimize legacy code modifications 
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303

	return $GLOBALS['channels'][$index][$dir] - $offset;
}

/** Get maximal port number from the list of all channels. This function is use to check 
 * port number provided by user. */
function max_port_num($dir)
{
	global $channels;
	$max_chn = -1;
1304
	
1305 1306 1307
	foreach ($channels as $chn) {
		if ($chn[$dir] > $max_chn)
			$max_chn = $chn[$dir];
1308
	}
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
	
	return $max_chn;
}

/** Update channels table in accordance with selected signal path */
function set_channels($port1, $port2, $conn, $port_num, $pcb_conn, &$channels)
{
	// check if the port connection (port1 <-> port2) is valid
	$conn_variant = array();
	$name_variants = array(strtoupper($port1) . '<->' . strtoupper($port2), strtoupper($port2) . '<->' . strtoupper($port1));
	if (array_key_exists($name_variants[0], $conn)) {
		$conn_variant = $conn[$name_variants[0]];
	} elseif (array_key_exists($name_variants[1], $conn)) {
		$conn_variant = $conn[$name_variants[1]];
1323 1324
	}
	
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
	// update channels table in accordance with the connection mode
	if (!empty($conn_variant)) {
		$pair1 = array($conn_variant[0]['FROM'], $conn_variant[1]['TO']);
		$pair2 = array($conn_variant[1]['FROM'], $conn_variant[0]['TO']);
		foreach ($channels as &$chn) {
			if (empty(array_diff($chn['phy_ports'], $pair1))) {
				$chn['in'] = $port_num[$pcb_conn[$pair1[0]]]['in'];
				$chn['out'] = $port_num[$pcb_conn[$pair1[1]]]['out'];
			} elseif (empty(array_diff($chn['phy_ports'], $pair2))) {
				$chn['in'] = $port_num[$pcb_conn[$pair2[0]]]['in'];
				$chn['out'] = $port_num[$pcb_conn[$pair2[1]]]['out'];
			}
		}
	}
}

/** Convert zero based index to port number */
function index_to_port_num($index)
{
	$offset = 8;                  // VSC3304 port numbering starts from 08, the offset is used to minimize legacy code modifications 
	return $index + $offset;
}

/** Read sysfs and update channels table */
function update_chn_from_sysfs()
{
	$paths = $GLOBALS['param_paths'];
	$out = read_vals($paths['connections'] . 'all');
	for ($index = 0; $index < count($out); $index++) {
		if (($out[$index] & 0x10) == 0) {
			$phy_port_in = pname_from_pnum($out[$index], 'in');
			$phy_port_out = pname_from_pnum(index_to_port_num($index), 'out');
			$pcb_conn_in = array_search($phy_port_in, $GLOBALS['pcb_connections']);
			$pcb_conn_out = array_search($phy_port_out, $GLOBALS['pcb_connections']);
			foreach ($GLOBALS['channels'] as &$chn) {
				if (in_array($pcb_conn_in, $chn['phy_ports'])) {
					$chn['in'] = $out[$index];
				} elseif (in_array($pcb_conn_out, $chn['phy_ports'])) {
					$chn['out'] = index_to_port_num($index);
				}
			}
		}
	}
}

/** Find port name from its corresponding number and direction */
function pname_from_pnum($port_num, $port_dir)
{
	foreach ($GLOBALS['port_num'] as $phy_name => $pair) {
		if ($pair[$port_dir] == $port_num)
			return $phy_name;
	}
	return '';
}

function apply_defaults($param_dir, $data)
{
	if (is_array($data)) {
		for ($index = 0; $index < count($data); $index++) {
			$fname = $param_dir . port_fn(index_to_port_num($index));
			if (file_exists($fname)) {
				write_vals($fname, $data[$index]);
			}
		}
	} else {
		write_vals($param_dir . 'all', $data);
	}
1392
}
1393 1394

?>