xref: /linux/tools/leds/get_led_device_info.sh (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3
4led_common_defs_path="include/dt-bindings/leds/common.h"
5
6num_args=$#
7if [ $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
17elif [ $num_args -eq 2 ]; then
18        led_defs_path=`realpath $2`
19else
20	echo "Usage: get_led_device_info.sh LED_CDEV_PATH [LED_COMMON_DEFS_PATH]"
21	exit 1
22fi
23
24if [ ! -f $led_defs_path ]; then
25	echo "$led_defs_path doesn't exist"
26	exit 1
27fi
28
29led_cdev_path=`echo $1 | sed s'/\/$//'`
30
31ls "$led_cdev_path/brightness" > /dev/null 2>&1
32if [ $? -ne 0 ]; then
33	echo "Device \"$led_cdev_path\" does not exist."
34	exit 1
35fi
36
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>&1
40of_node_missing=$?
41
42if [ "$bus" = "input" ]; then
43	input_node=`readlink $led_cdev_path/device | sed s'/.*\///'`
44	if [ ! -z "$usb_subdev" ]; then
45		bus="usb"
46	fi
47fi
48
49if [ "$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}'`
55	fi
56	idVendor=`cat idVendor`
57	idProduct=`cat idProduct`
58	manufacturer=`cat manufacturer`
59	product=`cat product`
60elif [ "$bus" = "input" ]; then
61	cd $led_cdev_path
62	product=`cat device/name`
63	driver=`cat device/device/driver/description`
64elif [ $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}'`
74	fi
75else
76	echo "Unknown device type."
77	exit 1
78fi
79
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" ]; then
87	printf "idVendor:\t\t$idVendor\n"
88fi
89
90if [ ! -z "$idProduct" ]; then
91	printf "idProduct:\t\t$idProduct\n"
92fi
93
94if [ ! -z "$manufacturer" ]; then
95	printf "manufacturer:\t\t$manufacturer\n"
96fi
97
98if [ ! -z "$product" ]; then
99	printf "product:\t\t$product\n"
100fi
101
102if [ ! -z "$driver" ]; then
103	printf "driver:\t\t\t$driver\n"
104fi
105
106if [ ! -z "$input_node" ]; then
107	printf "associated input node:\t$input_node\n"
108fi
109
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 ]; then
119	printf "\":\" delimiter not detected.\t[ FAILED ]\n"
120	exit 1
121elif [ $num_sections -eq 2 ]; then
122	color=`echo $led_name | cut -d: -f1`
123	function=`echo $led_name | cut -d: -f2`
124elif [ $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`
128else
129	printf "Detected %d sections in the LED class device name - should the script be updated?\n" $num_sections
130	exit 1
131fi
132
133S_DEV="devicename"
134S_CLR="color     "
135S_FUN="function  "
136status_tab=20
137
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" ]; then
155	expected_devname=$input_node
156elif [ ! -z "$wifi_phy" ]; then
157	expected_devname=$wifi_phy
158fi
159
160if [ ! -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"
166		fi
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?"
174		fi
175	fi
176elif [ ! -z "$expected_devname" ]; then
177	print_msg_failed "$S_DEV" "blank" "Expected: $expected_devname"
178fi
179
180if [ ! -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"
187	fi
188
189fi
190
191if [ ! -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"
199	fi
200
201fi
202