|
Packit Service |
9e0228 |
#! /usr/bin/python3
|
|
Packit Service |
9e0228 |
# Copyright(c) 2017, Intel Corporation
|
|
Packit Service |
9e0228 |
#
|
|
Packit Service |
9e0228 |
# Redistribution and use in source and binary forms, with or without
|
|
Packit Service |
9e0228 |
# modification, are permitted provided that the following conditions are met:
|
|
Packit Service |
9e0228 |
#
|
|
Packit Service |
9e0228 |
# * Redistributions of source code must retain the above copyright notice,
|
|
Packit Service |
9e0228 |
# this list of conditions and the following disclaimer.
|
|
Packit Service |
9e0228 |
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
Packit Service |
9e0228 |
# this list of conditions and the following disclaimer in the documentation
|
|
Packit Service |
9e0228 |
# and/or other materials provided with the distribution.
|
|
Packit Service |
9e0228 |
# * Neither the name of Intel Corporation nor the names of its contributors
|
|
Packit Service |
9e0228 |
# may be used to endorse or promote products derived from this software
|
|
Packit Service |
9e0228 |
# without specific prior written permission.
|
|
Packit Service |
9e0228 |
#
|
|
Packit Service |
9e0228 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
Packit Service |
9e0228 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit Service |
9e0228 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit Service |
9e0228 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
Packit Service |
9e0228 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
Packit Service |
9e0228 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
Packit Service |
9e0228 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
Packit Service |
9e0228 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
Packit Service |
9e0228 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
Packit Service |
9e0228 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
Packit Service |
9e0228 |
# POSSIBILITY OF SUCH DAMAGE.
|
|
Packit Service |
9e0228 |
|
|
Packit Service |
9e0228 |
import fcntl, os, sys, argparse, stat, struct
|
|
Packit Service |
9e0228 |
|
|
Packit Service |
2a94a3 |
# Intel driver
|
|
Packit Service |
9e0228 |
FPGA_FME_PORT_ASSIGN = 0xB582
|
|
Packit Service |
9e0228 |
FPGA_FME_PORT_RELEASE = 0xB581
|
|
Packit Service |
2a94a3 |
# DFL driver
|
|
Packit Service |
2a94a3 |
DFL_FME_PORT_ASSIGN = 0x4004B682
|
|
Packit Service |
2a94a3 |
DFL_FME_PORT_RELEASE = 0x4004B681
|
|
Packit Service |
9e0228 |
|
|
Packit Service |
9e0228 |
if __name__ == "__main__":
|
|
Packit Service |
9e0228 |
|
|
Packit Service |
9e0228 |
# parse command line arguments
|
|
Packit Service |
9e0228 |
parser = argparse.ArgumentParser()
|
|
Packit Service |
9e0228 |
parser.add_argument('action', help = 'action to perform', action = 'store', choices = ['assign', 'release'])
|
|
Packit Service |
9e0228 |
parser.add_argument('device', help = 'the FPGA (FME) device')
|
|
Packit Service |
9e0228 |
parser.add_argument('port', type = int, help = 'the number of the port')
|
|
Packit Service |
9e0228 |
|
|
Packit Service |
9e0228 |
args = parser.parse_args()
|
|
Packit Service |
9e0228 |
|
|
Packit Service |
9e0228 |
# check device file
|
|
Packit Service |
9e0228 |
try:
|
|
Packit Service |
9e0228 |
mode = os.stat(args.device).st_mode
|
|
Packit Service |
9e0228 |
if not stat.S_ISCHR(mode):
|
|
Packit Service |
9e0228 |
print( args.device, "is not a device file.")
|
|
Packit Service |
9e0228 |
except Exception as e:
|
|
Packit Service |
9e0228 |
print("Couldn't open", args.device, ":", e)
|
|
Packit Service |
9e0228 |
sys.exit(1)
|
|
Packit Service |
9e0228 |
|
|
Packit Service |
9e0228 |
# open FPGA device
|
|
Packit Service |
9e0228 |
try:
|
|
Packit Service |
2a94a3 |
f = open(args.device, "w")
|
|
Packit Service |
9e0228 |
except Exception as e:
|
|
Packit Service |
9e0228 |
print("open() failed:", e)
|
|
Packit Service |
9e0228 |
sys.exit(1)
|
|
Packit Service |
9e0228 |
|
|
Packit Service |
9e0228 |
# send IOCTL
|
|
Packit Service |
2a94a3 |
if (args.device.find("intel-fpga-fme") != -1):
|
|
Packit Service |
2a94a3 |
ioctl_data = struct.pack('III', 12, 0, args.port)
|
|
Packit Service |
2a94a3 |
else:
|
|
Packit Service |
2a94a3 |
ioctl_data = struct.pack('I', args.port)
|
|
Packit Service |
9e0228 |
try:
|
|
Packit Service |
9e0228 |
if args.action == 'assign':
|
|
Packit Service |
2a94a3 |
if (args.device.find("intel-fpga-fme") != -1):
|
|
Packit Service |
2a94a3 |
ret = fcntl.ioctl(f, FPGA_FME_PORT_ASSIGN, ioctl_data)
|
|
Packit Service |
2a94a3 |
else:
|
|
Packit Service |
2a94a3 |
ret = fcntl.ioctl(f, DFL_FME_PORT_ASSIGN, ioctl_data)
|
|
Packit Service |
9e0228 |
else:
|
|
Packit Service |
2a94a3 |
if (args.device.find("intel-fpga-fme") != -1):
|
|
Packit Service |
2a94a3 |
ret = fcntl.ioctl(f, FPGA_FME_PORT_RELEASE, ioctl_data)
|
|
Packit Service |
2a94a3 |
else:
|
|
Packit Service |
2a94a3 |
ret = fcntl.ioctl(f, DFL_FME_PORT_RELEASE, ioctl_data)
|
|
Packit Service |
9e0228 |
except Exception as e:
|
|
Packit Service |
9e0228 |
print("ioctl() failed:", e)
|
|
Packit Service |
9e0228 |
f.close()
|
|
Packit Service |
9e0228 |
sys.exit(1)
|
|
Packit Service |
9e0228 |
|
|
Packit Service |
9e0228 |
# close FPGA device
|
|
Packit Service |
9e0228 |
f.close()
|