#! /usr/bin/python
#
# Query or control whether a Vitesse PHY is enabled.

import os
import sys
import isi.hw.vitutil as vitutil

# Return a boolean indicating whether a PHY is enabled.
def phyGetEnable(unit, phy):
    # Check PHY Enable bit in the PHY Control register
    return (vitutil.phyRead(unit, phy, 0x10) & 0x01)

# Enable or disable a PHY according to the boolean "enable" parameter.
def phySetEnable(unit, phy, enable):
    if (enable == 0):
        vitutil.phyDisable(unit, phy)
    else:
        # Enable the PHY by issuing a Link Reset to it
        vitutil.phyLinkReset(unit, phy)

def usage(argv):
    print >>sys.stderr, "usage: %s unit phy [0|1]" % os.path.basename(argv[0])
    return 2

def main(argv = None):
    if argv is None:
        argv = sys.argv

    if len(argv) < 3 or len(argv) > 4:
        return usage(argv)
    unit = argv[1]
    phy = int(argv[2], 0)
    if phy < 0 or phy >= vitutil.PHYCOUNT:
        print >>sys.stderr, "phy number out of range"
        return 2
    if len(argv) > 3:
        enable = int(argv[3], 0)
        phySetEnable(unit, phy, enable)
    else:
        print int(phyGetEnable(unit, phy))
    return 0

if __name__ == "__main__":
    sys.exit(main())
