get_led_device_info.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0
  3. led_common_defs_path="include/dt-bindings/leds/common.h"
  4. num_args=$#
  5. if [ $num_args -eq 1 ]; then
  6. linux_top=$(dirname `realpath $0` | awk -F/ \
  7. '{ \
  8. i=1; \
  9. while (i <= NF - 2) { \
  10. printf $i"/"; \
  11. i++; \
  12. }; \
  13. }')
  14. led_defs_path=$linux_top/$led_common_defs_path
  15. elif [ $num_args -eq 2 ]; then
  16. led_defs_path=`realpath $2`
  17. else
  18. echo "Usage: get_led_device_info.sh LED_CDEV_PATH [LED_COMMON_DEFS_PATH]"
  19. exit 1
  20. fi
  21. if [ ! -f $led_defs_path ]; then
  22. echo "$led_defs_path doesn't exist"
  23. exit 1
  24. fi
  25. led_cdev_path=`echo $1 | sed s'/\/$//'`
  26. ls "$led_cdev_path/brightness" > /dev/null 2>&1
  27. if [ $? -ne 0 ]; then
  28. echo "Device \"$led_cdev_path\" does not exist."
  29. exit 1
  30. fi
  31. bus=`readlink $led_cdev_path/device/subsystem | sed s'/.*\///'`
  32. usb_subdev=`readlink $led_cdev_path | grep usb | sed s'/\(.*usb[0-9]*\/[0-9]*-[0-9]*\)\/.*/\1/'`
  33. ls "$led_cdev_path/device/of_node/compatible" > /dev/null 2>&1
  34. of_node_missing=$?
  35. if [ "$bus" = "input" ]; then
  36. input_node=`readlink $led_cdev_path/device | sed s'/.*\///'`
  37. if [ ! -z "$usb_subdev" ]; then
  38. bus="usb"
  39. fi
  40. fi
  41. if [ "$bus" = "usb" ]; then
  42. usb_interface=`readlink $led_cdev_path | sed s'/.*\(usb[0-9]*\)/\1/' | cut -d\/ -f3`
  43. cd $led_cdev_path/../$usb_subdev
  44. driver=`readlink $usb_interface/driver | sed s'/.*\///'`
  45. if [ -d "$usb_interface/ieee80211" ]; then
  46. wifi_phy=`ls -l $usb_interface/ieee80211 | grep phy | awk '{print $9}'`
  47. fi
  48. idVendor=`cat idVendor`
  49. idProduct=`cat idProduct`
  50. manufacturer=`cat manufacturer`
  51. product=`cat product`
  52. elif [ "$bus" = "input" ]; then
  53. cd $led_cdev_path
  54. product=`cat device/name`
  55. driver=`cat device/device/driver/description`
  56. elif [ $of_node_missing -eq 0 ]; then
  57. cd $led_cdev_path
  58. compatible=`cat device/of_node/compatible`
  59. if [ "$compatible" = "gpio-leds" ]; then
  60. driver="leds-gpio"
  61. elif [ "$compatible" = "pwm-leds" ]; then
  62. driver="leds-pwm"
  63. else
  64. manufacturer=`echo $compatible | awk -F, '{print $1}'`
  65. product=`echo $compatible | awk -F, '{print $2}'`
  66. fi
  67. else
  68. echo "Unknown device type."
  69. exit 1
  70. fi
  71. printf "\n#####################################\n"
  72. printf "# LED class device hardware details #\n"
  73. printf "#####################################\n\n"
  74. printf "bus:\t\t\t$bus\n"
  75. if [ ! -z "$idVendor" ]; then
  76. printf "idVendor:\t\t$idVendor\n"
  77. fi
  78. if [ ! -z "$idProduct" ]; then
  79. printf "idProduct:\t\t$idProduct\n"
  80. fi
  81. if [ ! -z "$manufacturer" ]; then
  82. printf "manufacturer:\t\t$manufacturer\n"
  83. fi
  84. if [ ! -z "$product" ]; then
  85. printf "product:\t\t$product\n"
  86. fi
  87. if [ ! -z "$driver" ]; then
  88. printf "driver:\t\t\t$driver\n"
  89. fi
  90. if [ ! -z "$input_node" ]; then
  91. printf "associated input node:\t$input_node\n"
  92. fi
  93. printf "\n####################################\n"
  94. printf "# LED class device name validation #\n"
  95. printf "####################################\n\n"
  96. led_name=`echo $led_cdev_path | sed s'/.*\///'`
  97. num_sections=`echo $led_name | awk -F: '{print NF}'`
  98. if [ $num_sections -eq 1 ]; then
  99. printf "\":\" delimiter not detected.\t[ FAILED ]\n"
  100. exit 1
  101. elif [ $num_sections -eq 2 ]; then
  102. color=`echo $led_name | cut -d: -f1`
  103. function=`echo $led_name | cut -d: -f2`
  104. elif [ $num_sections -eq 3 ]; then
  105. devicename=`echo $led_name | cut -d: -f1`
  106. color=`echo $led_name | cut -d: -f2`
  107. function=`echo $led_name | cut -d: -f3`
  108. else
  109. printf "Detected %d sections in the LED class device name - should the script be updated?\n" $num_sections
  110. exit 1
  111. fi
  112. S_DEV="devicename"
  113. S_CLR="color "
  114. S_FUN="function "
  115. status_tab=20
  116. print_msg_ok()
  117. {
  118. local section_name="$1"
  119. local section_val="$2"
  120. local msg="$3"
  121. printf "$section_name :\t%-${status_tab}.${status_tab}s %s %s\n" "$section_val" "[ OK ] " "$msg"
  122. }
  123. print_msg_failed()
  124. {
  125. local section_name="$1"
  126. local section_val="$2"
  127. local msg="$3"
  128. printf "$section_name :\t%-${status_tab}.${status_tab}s %s %s\n" "$section_val" "[ FAILED ]" "$msg"
  129. }
  130. if [ ! -z "$input_node" ]; then
  131. expected_devname=$input_node
  132. elif [ ! -z "$wifi_phy" ]; then
  133. expected_devname=$wifi_phy
  134. fi
  135. if [ ! -z "$devicename" ]; then
  136. if [ ! -z "$expected_devname" ]; then
  137. if [ "$devicename" = "$expected_devname" ]; then
  138. print_msg_ok "$S_DEV" "$devicename"
  139. else
  140. print_msg_failed "$S_DEV" "$devicename" "Expected: $expected_devname"
  141. fi
  142. else
  143. if [ "$devicename" = "$manufacturer" ]; then
  144. print_msg_failed "$S_DEV" "$devicename" "Redundant: use of vendor name is discouraged"
  145. elif [ "$devicename" = "$product" ]; then
  146. print_msg_failed "$S_DEV" "$devicename" "Redundant: use of product name is discouraged"
  147. else
  148. print_msg_failed "$S_DEV" "$devicename" "Unknown devicename - should the script be updated?"
  149. fi
  150. fi
  151. elif [ ! -z "$expected_devname" ]; then
  152. print_msg_failed "$S_DEV" "blank" "Expected: $expected_devname"
  153. fi
  154. if [ ! -z "$color" ]; then
  155. color_upper=`echo $color | tr [:lower:] [:upper:]`
  156. color_id_definition=$(cat $led_defs_path | grep "_$color_upper\s" | awk '{print $2}')
  157. if [ ! -z "$color_id_definition" ]; then
  158. print_msg_ok "$S_CLR" "$color" "Matching definition: $color_id_definition"
  159. else
  160. print_msg_failed "$S_CLR" "$color" "Definition not found in $led_defs_path"
  161. fi
  162. fi
  163. if [ ! -z "$function" ]; then
  164. # strip optional enumerator
  165. function=`echo $function | sed s'/\(.*\)-[0-9]*$/\1/'`
  166. fun_definition=$(cat $led_defs_path | grep "\"$function\"" | awk '{print $2}')
  167. if [ ! -z "$fun_definition" ]; then
  168. print_msg_ok "$S_FUN" "$function" "Matching definition: $fun_definition"
  169. else
  170. print_msg_failed "$S_FUN" "$function" "Definition not found in $led_defs_path"
  171. fi
  172. fi