aboutsummaryrefslogtreecommitdiff
path: root/scripts/pcidev
blob: dc0f237eeea7ca807f2e7f6ec4cece6becef56c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#! /bin/sh
#
# The block between #B and #E will be used as help.
#B
# Formilux 0.1.8 - http://www.ant-computing.com/
# PCIDEV - PCI device drivers scanner version 0.3
# 2008/11/17 - Willy Tarreau - willy@ant-computing.com
# May be redistributed under the terms of the GPL.
#
# usage :
#   pcidev [-vnm] [path to modules.pcimap]
# example :
#   pcidev /lib/modules/`uname -r`/modules.pcimap
#
# This program lists all pci devices, displays their IDs
# and the names of the driver attached to each device, when
# available, or "##none##'. If an argument is passed, it will
# be interpreted as the path to a modules.pcimap file. In this
# case, the list of all possible modules for each device is
# appended at the end of the line.
# If the "-n" argument is given, only modules that seem not to
# have already been loaded will be. If the "-m" argument is given,
# those modules will be loaded through modprobe (verbosely if "-v"
# is specified).
#E
MODPROBE=0
ONLYNEW=0
VERBOSE=0
MAPFILE=
PCIDEVDIR=/proc/bus/pci

if [ ! -d "$PCIDEVDIR" ]; then
   echo "No PCI support on this host. Aborting."
   exit 1
fi

while [ $# -gt 0 ]; do
    case $1 in
      -v)
         VERBOSE=1
	 ;;
      -n)
         ONLYNEW=1
	 ;;
      -m)
	 MODPROBE=1
	 ONLYNEW=1
	 ;;
      -*)
	 sed -e '/^#B/,/^#E/!d' --e 's/^#.\?//' < $0
	 exit 1
	 ;;
      *)
         break
	 ;;
    esac
    shift
done

if [ $ONLYNEW -gt 0 ]; then
    MAPFILE=/lib/modules/`uname -r`/modules.pcimap
fi

if [ $# -gt 0 ]; then
    MAPFILE=$*
fi


# let's collect a list of all PCI devices in the form bus/slot.func
pcidev=$PCIDEVDIR/*/*

if [ $ONLYNEW -eq 0 ]; then
   echo "# module device(entry) class  vid  pid  svid spid irq modules"
fi

for device in $pcidev; do
    x=${device%??/??.?}
    dev=${device#$x}
    set -- `od -v -tx1 -An $device`

    # at first it semt it was possible not to display bridges
    # but cardbus bridges (at least) do have a driver.
    #if [ "${12}" = "06" ]; then
	# this is a bridge => no driver !!
	#continue;
    #fi
    vid=$2$1; pid=$4$3; class=${12}${11}${10};
    svid=${46}${45}; spid=${48}${47}

    # now construct in <entry> a 16 bits entry for pci/devices from $dev, with
    # bus in $1, slot in $2, func in $3
    set -- ${dev//[.\/]/ }
    entry=$(printf "%s%02x" "$1" $(((0x$2<<3)+0x$3)))

    # look for a registered device in /proc/bus/pci/devices
    set -- `grep "^$entry" $PCIDEVDIR/devices`
    irq=$[0x$3]; driver=${18}

    xpid=0x$pid; xvid=0x$vid; xsvid=0x$svid; xspid=0x$spid; xclass=0x$class
    # look for modules supporting these devices. We exclude the rare modules
    # which pretend to support any vendor/product/subsys for PCI bridges because
    # they tend to pop up everywhere there is a bridge.
    if [ ! -z "$MAPFILE" ]; then
	list=""
	while read f1 f2 f3 f4 f5 f6 f7 rest; do
	    [ "$f1" != "#" -a -n "$f7" ] || continue
	    (( $f3 == 0xffffffff || $f3 == $xpid  )) || continue
	    (( $f2 == 0xffffffff || $f2 == $xvid  )) || continue
	    (( $f4 == 0xffffffff || $f4 == $xsvid )) || continue
	    (( $f5 == 0xffffffff || $f5 == $xspid )) || continue
	    (( ($xclass & $f7) == $f6 )) || continue
	    (( $f2 != 0xffffffff || $f3 != 0xffffffff ||
	       $f4 != 0xffffffff || $f5 != 0xffffffff ||
	       $f6 != 0x00060400 )) || continue
	    list="$list$f1 "
	done < $MAPFILE
    fi

    if [ -z "$driver" ]; then
	if [ $ONLYNEW -gt 0 ]; then
	    if [ "$list" ]; then
		if [ $MODPROBE -gt 0 ]; then
		    for module in $list; do
			if [ $VERBOSE -gt 0 ]; then
			    echo "modprobe $module"
			fi
			modprobe $module
		    done
		else
		    echo $list
		fi
	    fi
	else
	    echo "##none## $dev($entry) $class $vid $pid $svid $spid $irq $list"
	fi
    else
	if [ $ONLYNEW -eq 0 ]; then
	    echo "$driver $dev($entry) $class $vid $pid $svid $spid $irq $list"
	fi
    fi
done