202 lines · bash
1#!/bin/sh2# SPDX-License-Identifier: GPL-2.03 4led_common_defs_path="include/dt-bindings/leds/common.h"5 6num_args=$#7if [ $num_args -eq 1 ]; then8 linux_top=$(dirname `realpath $0` | awk -F/ \9 '{ \10 i=1; \11 while (i <= NF - 2) { \12 printf $i"/"; \13 i++; \14 }; \15 }')16 led_defs_path=$linux_top/$led_common_defs_path17elif [ $num_args -eq 2 ]; then18 led_defs_path=`realpath $2`19else20 echo "Usage: get_led_device_info.sh LED_CDEV_PATH [LED_COMMON_DEFS_PATH]"21 exit 122fi23 24if [ ! -f $led_defs_path ]; then25 echo "$led_defs_path doesn't exist"26 exit 127fi28 29led_cdev_path=`echo $1 | sed s'/\/$//'`30 31ls "$led_cdev_path/brightness" > /dev/null 2>&132if [ $? -ne 0 ]; then33 echo "Device \"$led_cdev_path\" does not exist."34 exit 135fi36 37bus=`readlink $led_cdev_path/device/subsystem | sed s'/.*\///'`38usb_subdev=`readlink $led_cdev_path | grep usb | sed s'/\(.*usb[0-9]*\/[0-9]*-[0-9]*\)\/.*/\1/'`39ls "$led_cdev_path/device/of_node/compatible" > /dev/null 2>&140of_node_missing=$?41 42if [ "$bus" = "input" ]; then43 input_node=`readlink $led_cdev_path/device | sed s'/.*\///'`44 if [ ! -z "$usb_subdev" ]; then45 bus="usb"46 fi47fi48 49if [ "$bus" = "usb" ]; then50 usb_interface=`readlink $led_cdev_path | sed s'/.*\(usb[0-9]*\)/\1/' | cut -d\/ -f3`51 cd $led_cdev_path/../$usb_subdev52 driver=`readlink $usb_interface/driver | sed s'/.*\///'`53 if [ -d "$usb_interface/ieee80211" ]; then54 wifi_phy=`ls -l $usb_interface/ieee80211 | grep phy | awk '{print $9}'`55 fi56 idVendor=`cat idVendor`57 idProduct=`cat idProduct`58 manufacturer=`cat manufacturer`59 product=`cat product`60elif [ "$bus" = "input" ]; then61 cd $led_cdev_path62 product=`cat device/name`63 driver=`cat device/device/driver/description`64elif [ $of_node_missing -eq 0 ]; then65 cd $led_cdev_path66 compatible=`cat device/of_node/compatible`67 if [ "$compatible" = "gpio-leds" ]; then68 driver="leds-gpio"69 elif [ "$compatible" = "pwm-leds" ]; then70 driver="leds-pwm"71 else72 manufacturer=`echo $compatible | awk -F, '{print $1}'`73 product=`echo $compatible | awk -F, '{print $2}'`74 fi75else76 echo "Unknown device type."77 exit 178fi79 80printf "\n#####################################\n"81printf "# LED class device hardware details #\n"82printf "#####################################\n\n"83 84printf "bus:\t\t\t$bus\n"85 86if [ ! -z "$idVendor" ]; then87 printf "idVendor:\t\t$idVendor\n"88fi89 90if [ ! -z "$idProduct" ]; then91 printf "idProduct:\t\t$idProduct\n"92fi93 94if [ ! -z "$manufacturer" ]; then95 printf "manufacturer:\t\t$manufacturer\n"96fi97 98if [ ! -z "$product" ]; then99 printf "product:\t\t$product\n"100fi101 102if [ ! -z "$driver" ]; then103 printf "driver:\t\t\t$driver\n"104fi105 106if [ ! -z "$input_node" ]; then107 printf "associated input node:\t$input_node\n"108fi109 110printf "\n####################################\n"111printf "# LED class device name validation #\n"112printf "####################################\n\n"113 114led_name=`echo $led_cdev_path | sed s'/.*\///'`115 116num_sections=`echo $led_name | awk -F: '{print NF}'`117 118if [ $num_sections -eq 1 ]; then119 printf "\":\" delimiter not detected.\t[ FAILED ]\n"120 exit 1121elif [ $num_sections -eq 2 ]; then122 color=`echo $led_name | cut -d: -f1`123 function=`echo $led_name | cut -d: -f2`124elif [ $num_sections -eq 3 ]; then125 devicename=`echo $led_name | cut -d: -f1`126 color=`echo $led_name | cut -d: -f2`127 function=`echo $led_name | cut -d: -f3`128else129 printf "Detected %d sections in the LED class device name - should the script be updated?\n" $num_sections130 exit 1131fi132 133S_DEV="devicename"134S_CLR="color "135S_FUN="function "136status_tab=20137 138print_msg_ok()139{140 local section_name="$1"141 local section_val="$2"142 local msg="$3"143 printf "$section_name :\t%-${status_tab}.${status_tab}s %s %s\n" "$section_val" "[ OK ] " "$msg"144}145 146print_msg_failed()147{148 local section_name="$1"149 local section_val="$2"150 local msg="$3"151 printf "$section_name :\t%-${status_tab}.${status_tab}s %s %s\n" "$section_val" "[ FAILED ]" "$msg"152}153 154if [ ! -z "$input_node" ]; then155 expected_devname=$input_node156elif [ ! -z "$wifi_phy" ]; then157 expected_devname=$wifi_phy158fi159 160if [ ! -z "$devicename" ]; then161 if [ ! -z "$expected_devname" ]; then162 if [ "$devicename" = "$expected_devname" ]; then163 print_msg_ok "$S_DEV" "$devicename"164 else165 print_msg_failed "$S_DEV" "$devicename" "Expected: $expected_devname"166 fi167 else168 if [ "$devicename" = "$manufacturer" ]; then169 print_msg_failed "$S_DEV" "$devicename" "Redundant: use of vendor name is discouraged"170 elif [ "$devicename" = "$product" ]; then171 print_msg_failed "$S_DEV" "$devicename" "Redundant: use of product name is discouraged"172 else173 print_msg_failed "$S_DEV" "$devicename" "Unknown devicename - should the script be updated?"174 fi175 fi176elif [ ! -z "$expected_devname" ]; then177 print_msg_failed "$S_DEV" "blank" "Expected: $expected_devname"178fi179 180if [ ! -z "$color" ]; then181 color_upper=`echo $color | tr [:lower:] [:upper:]`182 color_id_definition=$(cat $led_defs_path | grep "_$color_upper\s" | awk '{print $2}')183 if [ ! -z "$color_id_definition" ]; then184 print_msg_ok "$S_CLR" "$color" "Matching definition: $color_id_definition"185 else186 print_msg_failed "$S_CLR" "$color" "Definition not found in $led_defs_path"187 fi188 189fi190 191if [ ! -z "$function" ]; then192 # strip optional enumerator193 function=`echo $function | sed s'/\(.*\)-[0-9]*$/\1/'`194 fun_definition=$(cat $led_defs_path | grep "\"$function\"" | awk '{print $2}')195 if [ ! -z "$fun_definition" ]; then196 print_msg_ok "$S_FUN" "$function" "Matching definition: $fun_definition"197 else198 print_msg_failed "$S_FUN" "$function" "Definition not found in $led_defs_path"199 fi200 201fi202