Merge tag 'block-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / tools / leds / get_led_device_info.sh
blobccfa442db5fea014923263c7b3289563c4733394
1 #!/bin/sh
2 # SPDX-License-Identifier: GPL-2.0
4 led_common_defs_path="include/dt-bindings/leds/common.h"
6 num_args=$#
7 if [ $num_args -eq 1 ]; then
8 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_path
17 elif [ $num_args -eq 2 ]; then
18 led_defs_path=`realpath $2`
19 else
20 echo "Usage: get_led_device_info.sh LED_CDEV_PATH [LED_COMMON_DEFS_PATH]"
21 exit 1
24 if [ ! -f $led_defs_path ]; then
25 echo "$led_defs_path doesn't exist"
26 exit 1
29 led_cdev_path=`echo $1 | sed s'/\/$//'`
31 ls "$led_cdev_path/brightness" > /dev/null 2>&1
32 if [ $? -ne 0 ]; then
33 echo "Device \"$led_cdev_path\" does not exist."
34 exit 1
37 bus=`readlink $led_cdev_path/device/subsystem | sed s'/.*\///'`
38 usb_subdev=`readlink $led_cdev_path | grep usb | sed s'/\(.*usb[0-9]*\/[0-9]*-[0-9]*\)\/.*/\1/'`
39 ls "$led_cdev_path/device/of_node/compatible" > /dev/null 2>&1
40 of_node_missing=$?
42 if [ "$bus" = "input" ]; then
43 input_node=`readlink $led_cdev_path/device | sed s'/.*\///'`
44 if [ ! -z "$usb_subdev" ]; then
45 bus="usb"
49 if [ "$bus" = "usb" ]; then
50 usb_interface=`readlink $led_cdev_path | sed s'/.*\(usb[0-9]*\)/\1/' | cut -d\/ -f3`
51 cd $led_cdev_path/../$usb_subdev
52 driver=`readlink $usb_interface/driver | sed s'/.*\///'`
53 if [ -d "$usb_interface/ieee80211" ]; then
54 wifi_phy=`ls -l $usb_interface/ieee80211 | grep phy | awk '{print $9}'`
56 idVendor=`cat idVendor`
57 idProduct=`cat idProduct`
58 manufacturer=`cat manufacturer`
59 product=`cat product`
60 elif [ "$bus" = "input" ]; then
61 cd $led_cdev_path
62 product=`cat device/name`
63 driver=`cat device/device/driver/description`
64 elif [ $of_node_missing -eq 0 ]; then
65 cd $led_cdev_path
66 compatible=`cat device/of_node/compatible`
67 if [ "$compatible" = "gpio-leds" ]; then
68 driver="leds-gpio"
69 elif [ "$compatible" = "pwm-leds" ]; then
70 driver="leds-pwm"
71 else
72 manufacturer=`echo $compatible | awk -F, '{print $1}'`
73 product=`echo $compatible | awk -F, '{print $2}'`
75 else
76 echo "Unknown device type."
77 exit 1
80 printf "\n#####################################\n"
81 printf "# LED class device hardware details #\n"
82 printf "#####################################\n\n"
84 printf "bus:\t\t\t$bus\n"
86 if [ ! -z "$idVendor" ]; then
87 printf "idVendor:\t\t$idVendor\n"
90 if [ ! -z "$idProduct" ]; then
91 printf "idProduct:\t\t$idProduct\n"
94 if [ ! -z "$manufacturer" ]; then
95 printf "manufacturer:\t\t$manufacturer\n"
98 if [ ! -z "$product" ]; then
99 printf "product:\t\t$product\n"
102 if [ ! -z "$driver" ]; then
103 printf "driver:\t\t\t$driver\n"
106 if [ ! -z "$input_node" ]; then
107 printf "associated input node:\t$input_node\n"
110 printf "\n####################################\n"
111 printf "# LED class device name validation #\n"
112 printf "####################################\n\n"
114 led_name=`echo $led_cdev_path | sed s'/.*\///'`
116 num_sections=`echo $led_name | awk -F: '{print NF}'`
118 if [ $num_sections -eq 1 ]; then
119 printf "\":\" delimiter not detected.\t[ FAILED ]\n"
120 exit 1
121 elif [ $num_sections -eq 2 ]; then
122 color=`echo $led_name | cut -d: -f1`
123 function=`echo $led_name | cut -d: -f2`
124 elif [ $num_sections -eq 3 ]; then
125 devicename=`echo $led_name | cut -d: -f1`
126 color=`echo $led_name | cut -d: -f2`
127 function=`echo $led_name | cut -d: -f3`
128 else
129 printf "Detected %d sections in the LED class device name - should the script be updated?\n" $num_sections
130 exit 1
133 S_DEV="devicename"
134 S_CLR="color "
135 S_FUN="function "
136 status_tab=20
138 print_msg_ok()
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"
146 print_msg_failed()
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"
154 if [ ! -z "$input_node" ]; then
155 expected_devname=$input_node
156 elif [ ! -z "$wifi_phy" ]; then
157 expected_devname=$wifi_phy
160 if [ ! -z "$devicename" ]; then
161 if [ ! -z "$expected_devname" ]; then
162 if [ "$devicename" = "$expected_devname" ]; then
163 print_msg_ok "$S_DEV" "$devicename"
164 else
165 print_msg_failed "$S_DEV" "$devicename" "Expected: $expected_devname"
167 else
168 if [ "$devicename" = "$manufacturer" ]; then
169 print_msg_failed "$S_DEV" "$devicename" "Redundant: use of vendor name is discouraged"
170 elif [ "$devicename" = "$product" ]; then
171 print_msg_failed "$S_DEV" "$devicename" "Redundant: use of product name is discouraged"
172 else
173 print_msg_failed "$S_DEV" "$devicename" "Unknown devicename - should the script be updated?"
176 elif [ ! -z "$expected_devname" ]; then
177 print_msg_failed "$S_DEV" "blank" "Expected: $expected_devname"
180 if [ ! -z "$color" ]; then
181 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" ]; then
184 print_msg_ok "$S_CLR" "$color" "Matching definition: $color_id_definition"
185 else
186 print_msg_failed "$S_CLR" "$color" "Definition not found in $led_defs_path"
191 if [ ! -z "$function" ]; then
192 # strip optional enumerator
193 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" ]; then
196 print_msg_ok "$S_FUN" "$function" "Matching definition: $fun_definition"
197 else
198 print_msg_failed "$S_FUN" "$function" "Definition not found in $led_defs_path"