Blame tools/fpgaport/fpgaport

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