1#-
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright 2023 Beckhoff Automation GmbH & Co. KG
5# Copyright 2023 Bjoern A. Zeeb
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted providing that the following conditions 
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26# POSSIBILITY OF SUCH DAMAGE.
27
28pci_get_class()
29{
30	local hexclass=$(echo $1 | sed 's/.*class=\(0x[0-9a-z]\{2\}\).*/\1/')
31	case "${hexclass}" in
32		0x00)	echo "old" ;;		# built before class codes were finalized
33		0x02)	echo "network" ;;
34		0x03)	echo "video" ;;
35		0xff)	echo "misc" ;;		# does not fit in other defined classes
36	esac
37}
38
39pci_get_vendor()
40{
41	local hexvendor=$(echo $1 | sed 's/.*\ vendor=\(0x[0-9a-z]*\).*/\1/')
42
43	case "${hexvendor}" in
44		0x1002)	echo "amd" ;;
45		0x10ec)	echo "realtek" ;;
46		0x14c3) echo "mediatek" ;;
47		0x168c)	echo "qca" ;;	# Qualcomm Atheros
48		0x17cb)	echo "qca" ;;	# Qualcomm Technologies
49		0x8086)	echo "intel" ;;
50	esac
51}
52
53pci_get_device()
54{
55	local hexdevice=$(echo $1 | sed 's/.*\ device=\(0x[0-9a-z]*\).*/\1/')
56
57	echo ${hexdevice}
58}
59
60pci_fixup_class()
61{
62	local _c _v
63	_c=$1
64	_v=$2
65
66	case ${_c} in
67	"old")
68		case ${_v} in
69		"mediatek") echo "network" ;;
70		esac
71		;;
72	"misc")
73		case ${_v} in
74		"qca")	echo "network" ;;
75		esac
76		;;
77	esac
78}
79
80pci_search_packages()
81{
82	local IFS
83
84	oldifs=$IFS
85	IFS=$'\n'
86	for fulldevice in $(pciconf -l); do
87		class=$(pci_get_class "${fulldevice}")
88		if [ -z "${class}" ]; then
89			continue
90		fi
91		vendor=$(pci_get_vendor "${fulldevice}")
92		if [ -z "${vendor}" ]; then
93			continue
94		fi
95		device=$(pci_get_device "${fulldevice}")
96
97		log_verbose "Trying to match device ${device} in class ${class} and vendor ${vendor} with pci_${class}_${vendor}"
98		if [ ! -f ${LIBEXEC_PATH}/pci_${class}_${vendor} ]; then
99			class=$(pci_fixup_class ${class} ${vendor})
100			if [ -z "${class}" ]; then
101				continue
102			fi
103			log_verbose "Trying to match device ${device} in fixed up class ${class} and vendor ${vendor} with pci_${class}_${vendor}"
104			if [ ! -f ${LIBEXEC_PATH}/pci_${class}_${vendor} ]; then
105				continue
106			fi
107		fi
108		. ${LIBEXEC_PATH}/pci_${class}_${vendor}
109
110		pci_${class}_${vendor} ${device}
111	done
112	IFS=${oldifs}
113}
114