51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 1) #! /bin/sh
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 2) # SPDX-License-Identifier: GPL-2.0
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 3) # Copyright (c) 2020, Google LLC. All rights reserved.
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 4) # Author: Saravana Kannan <saravanak@google.com>
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 5)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 6) function help() {
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 7) cat << EOF
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 8) Usage: $(basename $0) [-c|-d|-m|-f] [filter options] <list of devices>
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 9)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 10) This script needs to be run on the target device once it has booted to a
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 11) shell.
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 12)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 13) The script takes as input a list of one or more device directories under
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 14) /sys/devices and then lists the probe dependency chain (suppliers and
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 15) parents) of these devices. It does a breadth first search of the dependency
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 16) chain, so the last entry in the output is close to the root of the
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 17) dependency chain.
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 18)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 19) By default it lists the full path to the devices under /sys/devices.
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 20)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 21) It also takes an optional modifier flag as the first parameter to change
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 22) what information is listed in the output. If the requested information is
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 23) not available, the device name is printed.
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 24)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 25) -c lists the compatible string of the dependencies
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 26) -d lists the driver name of the dependencies that have probed
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 27) -m lists the module name of the dependencies that have a module
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 28) -f list the firmware node path of the dependencies
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 29) -g list the dependencies as edges and nodes for graphviz
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 30) -t list the dependencies as edges for tsort
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 31)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 32) The filter options provide a way to filter out some dependencies:
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 33) --allow-no-driver By default dependencies that don't have a driver
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 34) attached are ignored. This is to avoid following
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 35) device links to "class" devices that are created
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 36) when the consumer probes (as in, not a probe
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 37) dependency). If you want to follow these links
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 38) anyway, use this flag.
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 39)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 40) --exclude-devlinks Don't follow device links when tracking probe
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 41) dependencies.
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 42)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 43) --exclude-parents Don't follow parent devices when tracking probe
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 44) dependencies.
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 45)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 46) EOF
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 47) }
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 48)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 49) function dev_to_detail() {
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 50) local i=0
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 51) while [ $i -lt ${#OUT_LIST[@]} ]
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 52) do
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 53) local C=${OUT_LIST[i]}
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 54) local S=${OUT_LIST[i+1]}
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 55) local D="'$(detail_chosen $C $S)'"
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 56) if [ ! -z "$D" ]
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 57) then
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 58) # This weirdness is needed to work with toybox when
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 59) # using the -t option.
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 60) printf '%05u\t%s\n' ${i} "$D" | tr -d \'
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 61) fi
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 62) i=$((i+2))
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 63) done
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 64) }
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 65)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 66) function already_seen() {
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 67) local i=0
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 68) while [ $i -lt ${#OUT_LIST[@]} ]
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 69) do
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 70) if [ "$1" = "${OUT_LIST[$i]}" ]
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 71) then
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 72) # if-statement treats 0 (no-error) as true
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 73) return 0
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 74) fi
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 75) i=$(($i+2))
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 76) done
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 77)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 78) # if-statement treats 1 (error) as false
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 79) return 1
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 80) }
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 81)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 82) # Return 0 (no-error/true) if parent was added
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 83) function add_parent() {
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 84)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 85) if [ ${ALLOW_PARENTS} -eq 0 ]
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 86) then
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 87) return 1
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 88) fi
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 89)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 90) local CON=$1
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 91) # $CON could be a symlink path. So, we need to find the real path and
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 92) # then go up one level to find the real parent.
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 93) local PARENT=$(realpath $CON/..)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 94)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 95) while [ ! -e ${PARENT}/driver ]
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 96) do
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 97) if [ "$PARENT" = "/sys/devices" ]
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 98) then
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 99) return 1
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 100) fi
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 101) PARENT=$(realpath $PARENT/..)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 102) done
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 103)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 104) CONSUMERS+=($PARENT)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 105) OUT_LIST+=(${CON} ${PARENT})
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 106) return 0
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 107) }
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 108)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 109) # Return 0 (no-error/true) if one or more suppliers were added
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 110) function add_suppliers() {
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 111) local CON=$1
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 112) local RET=1
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 113)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 114) if [ ${ALLOW_DEVLINKS} -eq 0 ]
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 115) then
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 116) return 1
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 117) fi
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 118)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 119) SUPPLIER_LINKS=$(ls -1d $CON/supplier:* 2>/dev/null)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 120) for SL in $SUPPLIER_LINKS;
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 121) do
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 122) SYNC_STATE=$(cat $SL/sync_state_only)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 123)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 124) # sync_state_only links are proxy dependencies.
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 125) # They can also have cycles. So, don't follow them.
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 126) if [ "$SYNC_STATE" != '0' ]
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 127) then
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 128) continue
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 129) fi
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 130)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 131) SUPPLIER=$(realpath $SL/supplier)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 132)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 133) if [ ! -e $SUPPLIER/driver -a ${ALLOW_NO_DRIVER} -eq 0 ]
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 134) then
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 135) continue
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 136) fi
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 137)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 138) CONSUMERS+=($SUPPLIER)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 139) OUT_LIST+=(${CON} ${SUPPLIER})
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 140) RET=0
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 141) done
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 142)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 143) return $RET
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 144) }
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 145)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 146) function detail_compat() {
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 147) f=$1/of_node/compatible
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 148) if [ -e $f ]
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 149) then
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 150) echo -n $(cat $f)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 151) else
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 152) echo -n $1
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 153) fi
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 154) }
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 155)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 156) function detail_module() {
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 157) f=$1/driver/module
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 158) if [ -e $f ]
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 159) then
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 160) echo -n $(basename $(realpath $f))
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 161) else
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 162) echo -n $1
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 163) fi
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 164) }
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 165)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 166) function detail_driver() {
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 167) f=$1/driver
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 168) if [ -e $f ]
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 169) then
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 170) echo -n $(basename $(realpath $f))
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 171) else
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 172) echo -n $1
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 173) fi
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 174) }
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 175)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 176) function detail_fwnode() {
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 177) f=$1/firmware_node
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 178) if [ ! -e $f ]
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 179) then
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 180) f=$1/of_node
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 181) fi
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 182)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 183) if [ -e $f ]
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 184) then
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 185) echo -n $(realpath $f)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 186) else
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 187) echo -n $1
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 188) fi
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 189) }
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 190)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 191) function detail_graphviz() {
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 192) if [ "$2" != "ROOT" ]
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 193) then
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 194) echo -n "\"$(basename $2)\"->\"$(basename $1)\""
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 195) else
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 196) echo -n "\"$(basename $1)\""
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 197) fi
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 198) }
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 199)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 200) function detail_tsort() {
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 201) echo -n "\"$2\" \"$1\""
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 202) }
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 203)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 204) function detail_device() { echo -n $1; }
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 205)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 206) alias detail=detail_device
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 207) ALLOW_NO_DRIVER=0
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 208) ALLOW_DEVLINKS=1
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 209) ALLOW_PARENTS=1
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 210)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 211) while [ $# -gt 0 ]
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 212) do
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 213) ARG=$1
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 214) case $ARG in
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 215) --help)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 216) help
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 217) exit 0
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 218) ;;
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 219) -c)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 220) alias detail=detail_compat
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 221) ;;
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 222) -m)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 223) alias detail=detail_module
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 224) ;;
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 225) -d)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 226) alias detail=detail_driver
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 227) ;;
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 228) -f)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 229) alias detail=detail_fwnode
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 230) ;;
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 231) -g)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 232) alias detail=detail_graphviz
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 233) ;;
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 234) -t)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 235) alias detail=detail_tsort
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 236) ;;
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 237) --allow-no-driver)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 238) ALLOW_NO_DRIVER=1
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 239) ;;
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 240) --exclude-devlinks)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 241) ALLOW_DEVLINKS=0
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 242) ;;
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 243) --exclude-parents)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 244) ALLOW_PARENTS=0
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 245) ;;
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 246) *)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 247) # Stop at the first argument that's not an option.
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 248) break
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 249) ;;
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 250) esac
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 251) shift
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 252) done
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 253)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 254) function detail_chosen() {
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 255) detail $1 $2
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 256) }
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 257)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 258) if [ $# -eq 0 ]
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 259) then
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 260) help
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 261) exit 1
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 262) fi
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 263)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 264) CONSUMERS=($@)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 265) OUT_LIST=()
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 266)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 267) # Do a breadth first, non-recursive tracking of suppliers. The parent is also
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 268) # considered a "supplier" as a device can't probe without its parent.
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 269) i=0
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 270) while [ $i -lt ${#CONSUMERS[@]} ]
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 271) do
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 272) CONSUMER=$(realpath ${CONSUMERS[$i]})
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 273) i=$(($i+1))
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 274)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 275) if already_seen ${CONSUMER}
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 276) then
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 277) continue
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 278) fi
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 279)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 280) # If this is not a device with a driver, we don't care about its
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 281) # suppliers.
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 282) if [ ! -e ${CONSUMER}/driver -a ${ALLOW_NO_DRIVER} -eq 0 ]
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 283) then
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 284) continue
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 285) fi
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 286)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 287) ROOT=1
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 288)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 289) # Add suppliers to CONSUMERS list and output the consumer details.
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 290) #
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 291) # We don't need to worry about a cycle in the dependency chain causing
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 292) # infinite loops. That's because the kernel doesn't allow cycles in
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 293) # device links unless it's a sync_state_only device link. And we ignore
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 294) # sync_state_only device links inside add_suppliers.
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 295) if add_suppliers ${CONSUMER}
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 296) then
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 297) ROOT=0
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 298) fi
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 299)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 300) if add_parent ${CONSUMER}
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 301) then
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 302) ROOT=0
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 303) fi
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 304)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 305) if [ $ROOT -eq 1 ]
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 306) then
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 307) OUT_LIST+=(${CONSUMER} "ROOT")
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 308) fi
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 309) done
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 310)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 311) # Can NOT combine sort and uniq using sort -suk2 because stable sort in toybox
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 312) # isn't really stable.
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 313) dev_to_detail | sort -k2 -k1 | uniq -f 1 | sort | cut -f2-
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 314)
51fae39bd5018 (Saravana Kannan 2020-09-01 15:48:42 -0700 315) exit 0