dt_to_config 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  1. #!/usr/bin/perl
  2. # Copyright 2016 by Frank Rowand
  3. # Copyright 2016 by Gaurav Minocha
  4. #
  5. # This file is subject to the terms and conditions of the GNU General Public
  6. # License v2.
  7. use strict 'refs';
  8. use strict subs;
  9. use Getopt::Long;
  10. $VUFX = "160610a";
  11. $script_name = $0;
  12. $script_name =~ s|^.*/||;
  13. # ----- constants for print_flags()
  14. # Position in string $pr_flags. Range of 0..($num_pr_flags - 1).
  15. $pr_flag_pos_mcompatible = 0;
  16. $pr_flag_pos_driver = 1;
  17. $pr_flag_pos_mdriver = 2;
  18. $pr_flag_pos_config = 3;
  19. $pr_flag_pos_mconfig = 4;
  20. $pr_flag_pos_node_not_enabled = 5;
  21. $pr_flag_pos_white_list = 6;
  22. $pr_flag_pos_hard_coded = 7;
  23. $pr_flag_pos_config_hard_coded = 8;
  24. $pr_flag_pos_config_none = 9;
  25. $pr_flag_pos_config_m = 10;
  26. $pr_flag_pos_config_y = 11;
  27. $pr_flag_pos_config_test_fail = 12;
  28. $num_pr_flags = $pr_flag_pos_config_test_fail + 1;
  29. # flags in @pr_flag_value must be unique values to allow simple regular
  30. # expessions to work for --include_flags and --exclude_flags.
  31. # Convention: use upper case letters for potential issues or problems.
  32. @pr_flag_value = ('M', 'd', 'D', 'c', 'C', 'E', 'W', 'H', 'x', 'n', 'm', 'y', 'F');
  33. @pr_flag_help = (
  34. "multiple compatibles found for this node",
  35. "driver found for this compatible",
  36. "multiple drivers found for this compatible",
  37. "kernel config found for this driver",
  38. "multiple config options found for this driver",
  39. "node is not enabled",
  40. "compatible is white listed",
  41. "matching driver and/or kernel config is hard coded",
  42. "kernel config hard coded in Makefile",
  43. "one or more kernel config file options is not set",
  44. "one or more kernel config file options is set to 'm'",
  45. "one or more kernel config file options is set to 'y'",
  46. "one of more kernel config file options fails to have correct value"
  47. );
  48. # -----
  49. %driver_config = (); # driver config array, indexed by driver source file
  50. %driver_count = (); # driver_cnt, indexed by compatible
  51. %compat_driver = (); # compatible driver array, indexed by compatible
  52. %existing_config = (); # existing config symbols present in given config file
  53. # expected values are: "y", "m", a decimal number, a
  54. # hex number, or a string
  55. # ----- magic compatibles, do not have a driver
  56. #
  57. # Will not search for drivers for these compatibles.
  58. %compat_white_list = (
  59. 'none' => '1',
  60. 'pci' => '1',
  61. 'simple-bus' => '1',
  62. );
  63. # Will not search for drivers for these compatibles.
  64. #
  65. # These compatibles have a very large number of false positives.
  66. #
  67. # 'hardcoded_no_driver' is a magic value. Other code knows this
  68. # magic value. Do not use 'no_driver' here!
  69. #
  70. # Revisit each 'hardcoded_no_driver' to see how the compatible
  71. # is used. Are there drivers that can be provided?
  72. %driver_hard_code_list = (
  73. 'cache' => ['hardcoded_no_driver'],
  74. 'eeprom' => ['hardcoded_no_driver'],
  75. 'gpio' => ['hardcoded_no_driver'],
  76. 'gpio-keys' => ['drivers/input/keyboard/gpio_keys.c'],
  77. 'i2c-gpio' => ['drivers/i2c/busses/i2c-gpio.c'],
  78. 'isa' => ['arch/mips/mti-malta/malta-dt.c',
  79. 'arch/x86/kernel/devicetree.c'],
  80. 'led' => ['hardcoded_no_driver'],
  81. 'm25p32' => ['hardcoded_no_driver'],
  82. 'm25p64' => ['hardcoded_no_driver'],
  83. 'm25p80' => ['hardcoded_no_driver'],
  84. 'mtd-ram' => ['drivers/mtd/maps/physmap_of.c'],
  85. 'pwm-backlight' => ['drivers/video/backlight/pwm_bl.c'],
  86. 'spidev' => ['hardcoded_no_driver'],
  87. 'syscon' => ['drivers/mfd/syscon.c'],
  88. 'tlv320aic23' => ['hardcoded_no_driver'],
  89. 'wm8731' => ['hardcoded_no_driver'],
  90. );
  91. # Use these config options instead of searching makefiles
  92. %driver_config_hard_code_list = (
  93. # this one needed even if %driver_hard_code_list is empty
  94. 'no_driver' => ['no_config'],
  95. 'hardcoded_no_driver' => ['no_config'],
  96. # drivers/usb/host/ehci-ppc-of.c
  97. # drivers/usb/host/ehci-xilinx-of.c
  98. # are included from:
  99. # drivers/usb/host/ehci-hcd.c
  100. # thus the search of Makefile for the included .c files is incorrect
  101. # ehci-hcd.c wraps the includes with ifdef CONFIG_USB_EHCI_HCD_..._OF
  102. #
  103. # similar model for ohci-hcd.c (but no ohci-xilinx-of.c)
  104. #
  105. # similarly, uhci-hcd.c includes uhci-platform.c
  106. 'drivers/usb/host/ehci-ppc-of.c' => ['CONFIG_USB_EHCI_HCD',
  107. 'CONFIG_USB_EHCI_HCD_PPC_OF'],
  108. 'drivers/usb/host/ohci-ppc-of.c' => ['CONFIG_USB_OHCI_HCD',
  109. 'CONFIG_USB_OHCI_HCD_PPC_OF'],
  110. 'drivers/usb/host/ehci-xilinx-of.c' => ['CONFIG_USB_EHCI_HCD',
  111. 'CONFIG_USB_EHCI_HCD_XILINX'],
  112. 'drivers/usb/host/uhci-platform.c' => ['CONFIG_USB_UHCI_HCD',
  113. 'CONFIG_USB_UHCI_PLATFORM'],
  114. # scan_makefile will find only one of these config options:
  115. # ifneq ($(CONFIG_SOC_IMX6)$(CONFIG_SOC_LS1021A),)
  116. 'arch/arm/mach-imx/platsmp.c' => ['CONFIG_SOC_IMX6 && CONFIG_SMP',
  117. 'CONFIG_SOC_LS1021A && CONFIG_SMP'],
  118. );
  119. # 'virt/kvm/arm/.*' are controlled by makefiles in other directories,
  120. # using relative paths, such as 'KVM := ../../../virt/kvm'. Do not
  121. # add complexity to find_kconfig() to deal with this. There is a long
  122. # term intent to change the kvm related makefiles to the normal kernel
  123. # style. After that is done, this entry can be removed from the
  124. # black_list_driver.
  125. @black_list_driver = (
  126. # kvm no longer a problem after commit 503a62862e8f in 4.7-rc1
  127. # 'virt/kvm/arm/.*',
  128. );
  129. sub usage()
  130. {
  131. print
  132. "
  133. Usage: $script_name [options] device-tree...
  134. device_tree is: dts_file | dtb_file | proc_device-tree
  135. Valid options:
  136. -c FILE Read kernel config options from FILE
  137. --config FILE synonym for 'c'
  138. --config-format config file friendly output format
  139. --exclude-flag FLAG exclude entries with a matching flag
  140. -h Display this message and exit
  141. --help synonym for 'h'
  142. --black-list-driver use driver black list
  143. --white-list-config use config white list
  144. --white-list-driver use driver white list
  145. --include-flag FLAG include only entries with a matching flag
  146. --include-suspect include only entries with an uppercase flag
  147. --short-name do not show the path portion of the node name
  148. --show-lists report of white and black lists
  149. --version Display program version and exit
  150. Report driver source files that match the compatibles in the device
  151. tree file and the kernel config options that enable the driver source
  152. files.
  153. This program must be run in the root directory of a Linux kernel
  154. source tree.
  155. The default format is a report that is intended to be easily human
  156. scannable.
  157. An alternate format can be selected by --config-format. This will
  158. create output that can easily be edited to create a fragment that can
  159. be appended to the existing kernel config file. Each entry consists of
  160. multiple lines. The first line reports flags, the node path, compatible
  161. value, driver file matching the compatible, configuration options, and
  162. current values of the configuration options. For each configuration
  163. option, the following lines report the current value and the value that
  164. is required for the driver file to be included in the kernel.
  165. If a large number of drivers or config options is listed for a node,
  166. and the '$pr_flag_value[$pr_flag_pos_hard_coded]' flag is set consider using --white-list-config and/or
  167. --white-list-driver. If the white list option suppresses the correct
  168. entry please report that as a bug.
  169. CAUTION:
  170. This program uses heuristics to guess which driver(s) support each
  171. compatible string and which config option(s) enables the driver(s).
  172. Do not believe that the reported information is fully correct.
  173. This program is intended to aid the process of determining the
  174. proper kernel configuration for a device tree, but this is not
  175. a fully automated process -- human involvement may still be
  176. required!
  177. The driver match heuristic used is to search for source files
  178. containing the compatible string enclosed in quotes.
  179. This program might not be able to find all drivers matching a
  180. compatible string.
  181. Some makefiles are overly clever. This program was not made
  182. complex enough to handle them. If no config option is listed
  183. for a driver, look at the makefile for the driver source file.
  184. Even if a config option is listed for a driver, some other
  185. available config options may not be listed.
  186. FLAG values:
  187. ";
  188. for ($k = 0; $k < $num_pr_flags; $k++) {
  189. printf " %s %s\n", $pr_flag_value[$k], $pr_flag_help[$k];
  190. }
  191. print
  192. "
  193. Upper case letters indicate potential issues or problems.
  194. The flag:
  195. ";
  196. $k = $pr_flag_pos_hard_coded;
  197. printf " %s %s\n", $pr_flag_value[$k], $pr_flag_help[$k];
  198. print
  199. "
  200. will be set if the config or driver is in the white lists, even if
  201. --white-list-config and --white-list-driver are not specified.
  202. This is a hint that 1) many of these reported lines are likely to
  203. be incorrect, and 2) using those options will reduce the number of
  204. drivers and/or config options reported.
  205. --white-list-config and --white-list-driver may not be accurate if this
  206. program is not well maintained. Use them with appropriate skepticism.
  207. Use the --show-lists option to report the values in the list.
  208. Return value:
  209. 0 if no error
  210. 1 error processing command line
  211. 2 unable to open or read kernel config file
  212. 3 unable to open or process input device tree file(s)
  213. EXAMPLES:
  214. dt_to_config arch/arm/boot/dts/my_dts_file.dts
  215. Basic report.
  216. dt_to_config \\
  217. --config \${KBUILD_OUTPUT}/.config \\
  218. arch/\${ARCH}/boot/dts/my_dts_file.dts
  219. Full report, with config file issues noted.
  220. dt_to_config --include-suspect \\
  221. --config \${KBUILD_OUTPUT}/.config \\
  222. arch/\${ARCH}/boot/dts/my_dts_file.dts
  223. Report of node / compatible string / driver tuples that should
  224. be further investigated. A node may have multiple compatible
  225. strings. A compatible string may be matched by multiple drivers.
  226. A driver may have config file issues noted. The compatible string
  227. and/or driver may be in the white lists.
  228. dt_to_config --include-suspect --config-format \\
  229. --config ${KBUILD_OUTPUT}/.config \\
  230. arch/\${ARCH}/boot/dts/my_dts_file.dts
  231. Report of node / compatible string / driver tuples that should
  232. be further investigated. The report can be edited to uncomment
  233. the config options to select the desired tuple for a given node.
  234. A node may have multiple compatible strings. A compatible string
  235. may be matched by multiple drivers. A driver may have config file
  236. issues noted. The compatible string and/or driver may be in the
  237. white lists.
  238. ";
  239. }
  240. sub set_flag()
  241. {
  242. # pr_flags_ref is a reference to $pr_flags
  243. my $pr_flags_ref = shift;
  244. my $pos = shift;
  245. substr $$pr_flags_ref, $pos, 1, $pr_flag_value[$pos];
  246. return $pr_flags;
  247. }
  248. sub print_flags()
  249. {
  250. # return 1 if anything printed, else 0
  251. # some fields of pn_arg_ref might not be used in this function, but
  252. # extract all of them anyway.
  253. my $pn_arg_ref = shift;
  254. my $compat = $pn_arg_ref->{compat};
  255. my $compatible_cnt = $pn_arg_ref->{compatible_cnt};
  256. my $config = $pn_arg_ref->{config};
  257. my $config_cnt = $pn_arg_ref->{config_cnt};
  258. my $driver = $pn_arg_ref->{driver};
  259. my $driver_cnt = $pn_arg_ref->{driver_cnt};
  260. my $full_node = $pn_arg_ref->{full_node};
  261. my $node = $pn_arg_ref->{node};
  262. my $node_enabled = $pn_arg_ref->{node_enabled};
  263. my $white_list = $pn_arg_ref->{white_list};
  264. my $pr_flags = '-' x $num_pr_flags;
  265. # ----- set flags in $pr_flags
  266. if ($compatible_cnt > 1) {
  267. &set_flag(\$pr_flags, $pr_flag_pos_mcompatible);
  268. }
  269. if ($config_cnt > 1) {
  270. &set_flag(\$pr_flags, $pr_flag_pos_mconfig);
  271. }
  272. if ($driver_cnt >= 1) {
  273. &set_flag(\$pr_flags, $pr_flag_pos_driver);
  274. }
  275. if ($driver_cnt > 1) {
  276. &set_flag(\$pr_flags, $pr_flag_pos_mdriver);
  277. }
  278. # These strings are the same way the linux kernel tests.
  279. # The ePapr lists of values is slightly different.
  280. if (!(
  281. ($node_enabled eq "") ||
  282. ($node_enabled eq "ok") ||
  283. ($node_enabled eq "okay")
  284. )) {
  285. &set_flag(\$pr_flags, $pr_flag_pos_node_not_enabled);
  286. }
  287. if ($white_list) {
  288. &set_flag(\$pr_flags, $pr_flag_pos_white_list);
  289. }
  290. if (exists($driver_hard_code_list{$compat}) ||
  291. (exists($driver_config_hard_code_list{$driver}) &&
  292. ($driver ne "no_driver"))) {
  293. &set_flag(\$pr_flags, $pr_flag_pos_hard_coded);
  294. }
  295. my @configs = split(' && ', $config);
  296. for $configs (@configs) {
  297. $not = $configs =~ /^!/;
  298. $configs =~ s/^!//;
  299. if (($configs ne "no_config") && ($configs ne "no_makefile")) {
  300. &set_flag(\$pr_flags, $pr_flag_pos_config);
  301. }
  302. if (($config_cnt >= 1) &&
  303. ($configs !~ /CONFIG_/) &&
  304. (($configs ne "no_config") && ($configs ne "no_makefile"))) {
  305. &set_flag(\$pr_flags, $pr_flag_pos_config_hard_coded);
  306. }
  307. my $existing_config = $existing_config{$configs};
  308. if ($existing_config eq "m") {
  309. &set_flag(\$pr_flags, $pr_flag_pos_config_m);
  310. # Possible fail, depends on whether built in or
  311. # module is desired.
  312. &set_flag(\$pr_flags, $pr_flag_pos_config_test_fail);
  313. } elsif ($existing_config eq "y") {
  314. &set_flag(\$pr_flags, $pr_flag_pos_config_y);
  315. if ($not) {
  316. &set_flag(\$pr_flags, $pr_flag_pos_config_test_fail);
  317. }
  318. } elsif (($config_file) && ($configs =~ /CONFIG_/)) {
  319. &set_flag(\$pr_flags, $pr_flag_pos_config_none);
  320. if (!$not) {
  321. &set_flag(\$pr_flags, $pr_flag_pos_config_test_fail);
  322. }
  323. }
  324. }
  325. # ----- include / exclude filters
  326. if ($include_flag_pattern && ($pr_flags !~ m/$include_flag_pattern/)) {
  327. return 0;
  328. }
  329. if ($exclude_flag_pattern && ($pr_flags =~ m/$exclude_flag_pattern/)) {
  330. return 0;
  331. }
  332. if ($config_format) {
  333. print "# ";
  334. }
  335. print "$pr_flags : ";
  336. return 1;
  337. }
  338. sub print_node()
  339. {
  340. # return number of lines printed
  341. # some fields of pn_arg_ref might not be used in this function, but
  342. # extract all of them anyway.
  343. my $pn_arg_ref = shift;
  344. my $compat = $pn_arg_ref->{compat};
  345. my $compatible_cnt = $pn_arg_ref->{compatible_cnt};
  346. my $config = $pn_arg_ref->{config};
  347. my $config_cnt = $pn_arg_ref->{config_cnt};
  348. my $driver = $pn_arg_ref->{driver};
  349. my $driver_cnt = $pn_arg_ref->{driver_cnt};
  350. my $full_node = $pn_arg_ref->{full_node};
  351. my $node = $pn_arg_ref->{node};
  352. my $node_enabled = $pn_arg_ref->{node_enabled};
  353. my $white_list = $pn_arg_ref->{white_list};
  354. my $separator;
  355. if (! &print_flags($pn_arg_ref)) {
  356. return 0;
  357. }
  358. if ($short_name) {
  359. print "$node";
  360. } else {
  361. print "$full_node";
  362. }
  363. print " : $compat : $driver : $config : ";
  364. my @configs = split(' && ', $config);
  365. if ($config_file) {
  366. for $configs (@configs) {
  367. $configs =~ s/^!//;
  368. my $existing_config = $existing_config{$configs};
  369. if (!$existing_config) {
  370. # check for /-m/, /-y/, or /-objs/
  371. if ($configs !~ /CONFIG_/) {
  372. $existing_config = "x";
  373. };
  374. };
  375. if ($existing_config) {
  376. print "$separator", "$existing_config";
  377. $separator = ", ";
  378. } else {
  379. print "$separator", "n";
  380. $separator = ", ";
  381. }
  382. }
  383. } else {
  384. print "none";
  385. }
  386. print "\n";
  387. if ($config_format) {
  388. for $configs (@configs) {
  389. $not = $configs =~ /^!/;
  390. $configs =~ s/^!//;
  391. my $existing_config = $existing_config{$configs};
  392. if ($not) {
  393. if ($configs !~ /CONFIG_/) {
  394. print "# $configs\n";
  395. } elsif ($existing_config eq "m") {
  396. print "# $configs is m\n";
  397. print "# $configs=n\n";
  398. } elsif ($existing_config eq "y") {
  399. print "# $configs is set\n";
  400. print "# $configs=n\n";
  401. } else {
  402. print "# $configs is not set\n";
  403. print "# $configs=n\n";
  404. }
  405. } else {
  406. if ($configs !~ /CONFIG_/) {
  407. print "# $configs\n";
  408. } elsif ($existing_config eq "m") {
  409. print "# $configs is m\n";
  410. print "# $configs=y\n";
  411. } elsif ($existing_config eq "y") {
  412. print "# $configs is set\n";
  413. print "# $configs=y\n";
  414. } else {
  415. print "# $configs is not set\n";
  416. print "# $configs=y\n";
  417. }
  418. }
  419. }
  420. }
  421. return 1;
  422. }
  423. sub scan_makefile
  424. {
  425. my $pn_arg_ref = shift;
  426. my $driver = shift;
  427. # ----- Find Kconfig symbols that enable driver
  428. my ($dir, $base) = $driver =~ m{(.*)/(.*).c};
  429. my $makefile = $dir . "/Makefile";
  430. if (! -r $makefile) {
  431. $makefile = $dir . "/Kbuild";
  432. }
  433. if (! -r $makefile) {
  434. my $config;
  435. $config = 'no_makefile';
  436. push @{ $driver_config{$driver} }, $config;
  437. return;
  438. }
  439. if (!open(MAKEFILE_FILE, "<", "$makefile")) {
  440. return;
  441. }
  442. my $line;
  443. my @config;
  444. my @if_config;
  445. my @make_var;
  446. NEXT_LINE:
  447. while ($next_line = <MAKEFILE_FILE>) {
  448. my $config;
  449. my $if_config;
  450. my $ifdef;
  451. my $ifeq;
  452. my $ifndef;
  453. my $ifneq;
  454. my $ifdef_config;
  455. my $ifeq_config;
  456. my $ifndef_config;
  457. my $ifneq_config;
  458. chomp($next_line);
  459. $line = $line . $next_line;
  460. if ($next_line =~ /\\$/) {
  461. $line =~ s/\\$/ /;
  462. next NEXT_LINE;
  463. }
  464. if ($line =~ /^\s*#/) {
  465. $line = "";
  466. next NEXT_LINE;
  467. }
  468. # ----- condition ... else ... endif
  469. if ($line =~ /^([ ]\s*|)else\b/) {
  470. $if_config = "!" . pop @if_config;
  471. $if_config =~ s/^!!//;
  472. push @if_config, $if_config;
  473. $line =~ s/^([ ]\s*|)else\b//;
  474. }
  475. ($null, $ifeq_config, $ifeq_config_val ) = $line =~ /^([ ]\s*|)ifeq\b.*\b(CONFIG_[A-Za-z0-9_]*)(.*)/;
  476. ($null, $ifneq_config, $ifneq_config_val) = $line =~ /^([ ]\s*|)ifneq\b.*\b(CONFIG_[A-Za-z0-9_]*)(.*)/;
  477. ($null, $ifdef_config) = $line =~ /^([ ]\s*|)ifdef\b.*\b(CONFIG_[A-Za-z0-9_]*)/;
  478. ($null, $ifndef_config) = $line =~ /^([ ]\s*|)ifndef\b.*\b(CONFIG_[A-Za-z0-9_]*)/;
  479. ($null, $ifeq) = $line =~ /^([ ]\s*|)ifeq\b\s*(.*)/;
  480. ($null, $ifneq) = $line =~ /^([ ]\s*|)ifneq\b\s*(.*)/;
  481. ($null, $ifdef) = $line =~ /^([ ]\s*|)ifdef\b\s*(.*)/;
  482. ($null, $ifndef) = $line =~ /^([ ]\s*|)ifndef\b\s*(.*)/;
  483. # Order of tests is important. Prefer "CONFIG_*" regex match over
  484. # less specific regex match.
  485. if ($ifdef_config) {
  486. $if_config = $ifdef_config;
  487. } elsif ($ifeq_config) {
  488. if ($ifeq_config_val =~ /y/) {
  489. $if_config = $ifeq_config;
  490. } else {
  491. $if_config = "!" . $ifeq_config;
  492. }
  493. } elsif ($ifndef_config) {
  494. $if_config = "!" . $ifndef_config;
  495. } elsif ($ifneq_config) {
  496. if ($ifneq_config_val =~ /y/) {
  497. $if_config = "!" . $ifneq_config;
  498. } else {
  499. $if_config = $ifneq_config;
  500. }
  501. } elsif ($ifdef) {
  502. $if_config = $ifdef;
  503. } elsif ($ifeq) {
  504. $if_config = $ifeq;
  505. } elsif ($ifndef) {
  506. $if_config = "!" . $ifndef;
  507. } elsif ($ifneq) {
  508. $if_config = "!" . $ifneq;
  509. } else {
  510. $if_config = "";
  511. }
  512. $if_config =~ s/^!!//;
  513. if ($if_config) {
  514. push @if_config, $if_config;
  515. $line = "";
  516. next NEXT_LINE;
  517. }
  518. if ($line =~ /^([ ]\s*|)endif\b/) {
  519. pop @if_config;
  520. $line = "";
  521. next NEXT_LINE;
  522. }
  523. # ----- simple CONFIG_* = *.[co] or xxx [+:?]*= *.[co]
  524. # Most makefiles select on *.o, but
  525. # arch/powerpc/boot/Makefile selects on *.c
  526. ($config) = $line =~ /(CONFIG_[A-Za-z0-9_]+).*\b$base.[co]\b/;
  527. # ----- match a make variable instead of *.[co]
  528. # Recursively expanded variables are not handled.
  529. if (!$config) {
  530. my $make_var;
  531. ($make_var) = $line =~ /\s*(\S+?)\s*[+:\?]*=.*\b$base.[co]\b/;
  532. if ($make_var) {
  533. if ($make_var =~ /[a-zA-Z0-9]+-[ym]/) {
  534. $config = $make_var;
  535. } elsif ($make_var =~ /[a-zA-Z0-9]+-objs/) {
  536. $config = $make_var;
  537. } else {
  538. push @make_var, $make_var;
  539. }
  540. }
  541. }
  542. if (!$config) {
  543. for $make_var (@make_var) {
  544. ($config) = $line =~ /(CONFIG_[A-Za-z0-9_]+).*\b$make_var\b/;
  545. last if ($config);
  546. }
  547. }
  548. if (!$config) {
  549. for $make_var (@make_var) {
  550. ($config) = $line =~ /\s*(\S+?)\s*[+:\?]*=.*\b$make_var\b/;
  551. last if ($config);
  552. }
  553. }
  554. # ----- next if no config found
  555. if (!$config) {
  556. $line = "";
  557. next NEXT_LINE;
  558. }
  559. for $if_config (@if_config) {
  560. $config = $if_config . " && " . $config;
  561. }
  562. push @{ $driver_config{$driver} }, $config;
  563. $line = "";
  564. }
  565. close(MAKEFILE_FILE);
  566. }
  567. sub find_kconfig
  568. {
  569. my $pn_arg_ref = shift;
  570. my $driver = shift;
  571. my $lines_printed = 0;
  572. my @configs;
  573. if (!@{ $driver_config{$driver} }) {
  574. &scan_makefile($pn_arg_ref, $driver);
  575. if (!@{ $driver_config{$driver} }) {
  576. push @{ $driver_config{$driver} }, "no_config";
  577. }
  578. }
  579. @configs = @{ $driver_config{$driver} };
  580. $$pn_arg_ref{config_cnt} = $#configs + 1;
  581. for my $config (@configs) {
  582. $$pn_arg_ref{config} = $config;
  583. $lines_printed += &print_node($pn_arg_ref);
  584. }
  585. return $lines_printed;
  586. }
  587. sub handle_compatible()
  588. {
  589. my $full_node = shift;
  590. my $node = shift;
  591. my $compatible = shift;
  592. my $node_enabled = shift;
  593. my $compat;
  594. my $lines_printed = 0;
  595. my %pn_arg = ();
  596. return if (!$node or !$compatible);
  597. # Do not process compatible property of root node,
  598. # it is used to match board, not to bind a driver.
  599. return if ($node eq "/");
  600. $pn_arg{full_node} = $full_node;
  601. $pn_arg{node} = $node;
  602. $pn_arg{node_enabled} = $node_enabled;
  603. my @compatibles = split('", "', $compatible);
  604. $compatibles[0] =~ s/^"//;
  605. $compatibles[$#compatibles] =~ s/"$//;
  606. $pn_arg{compatible_cnt} = $#compatibles + 1;
  607. COMPAT:
  608. for $compat (@compatibles) {
  609. $pn_arg{compat} = $compat;
  610. $pn_arg{driver_cnt} = 0;
  611. $pn_arg{white_list} = 0;
  612. if (exists($compat_white_list{$compat})) {
  613. $pn_arg{white_list} = 1;
  614. $pn_arg{driver} = "no_driver";
  615. $pn_arg{config_cnt} = 1;
  616. $pn_arg{config} = "no_config";
  617. $lines_printed += &print_node(\%pn_arg);
  618. next COMPAT;
  619. }
  620. # ----- if compat previously seen, use cached info
  621. if (exists($compat_driver{$compat})) {
  622. for my $driver (@{ $compat_driver{$compat} }) {
  623. $pn_arg{driver} = $driver;
  624. $pn_arg{driver_cnt} = $driver_count{$compat};
  625. $pn_arg{config_cnt} = $#{ $driver_config{$driver}} + 1;
  626. for my $config (@{ $driver_config{$driver} }) {
  627. $pn_arg{config} = $config;
  628. $lines_printed += &print_node(\%pn_arg);
  629. }
  630. if (!@{ $driver_config{$driver} }) {
  631. # no config cached yet
  632. # $driver in %driver_hard_code_list
  633. # but not %driver_config_hard_code_list
  634. $lines_printed += &find_kconfig(\%pn_arg, $driver);
  635. }
  636. }
  637. next COMPAT;
  638. }
  639. # ----- Find drivers (source files that contain compatible)
  640. # this will miss arch/sparc/include/asm/parport.h
  641. # It is better to move the compatible out of the .h
  642. # than to add *.h. to the files list, because *.h generates
  643. # a lot of false negatives.
  644. my $files = '"*.c"';
  645. my $drivers = `git grep -l '"$compat"' -- $files`;
  646. chomp($drivers);
  647. if ($drivers eq "") {
  648. $pn_arg{driver} = "no_driver";
  649. $pn_arg{config_cnt} = 1;
  650. $pn_arg{config} = "no_config";
  651. push @{ $compat_driver{$compat} }, "no_driver";
  652. $lines_printed += &print_node(\%pn_arg);
  653. next COMPAT;
  654. }
  655. my @drivers = split("\n", $drivers);
  656. $driver_count{$compat} = $#drivers + 1;
  657. $pn_arg{driver_cnt} = $#drivers + 1;
  658. DRIVER:
  659. for my $driver (@drivers) {
  660. push @{ $compat_driver{$compat} }, $driver;
  661. $pn_arg{driver} = $driver;
  662. # ----- if driver previously seen, use cached info
  663. $pn_arg{config_cnt} = $#{ $driver_config{$driver} } + 1;
  664. for my $config (@{ $driver_config{$driver} }) {
  665. $pn_arg{config} = $config;
  666. $lines_printed += &print_node(\%pn_arg);
  667. }
  668. if (@{ $driver_config{$driver} }) {
  669. next DRIVER;
  670. }
  671. if ($black_list_driver) {
  672. for $black (@black_list_driver) {
  673. next DRIVER if ($driver =~ /^$black$/);
  674. }
  675. }
  676. # ----- Find Kconfig symbols that enable driver
  677. $lines_printed += &find_kconfig(\%pn_arg, $driver);
  678. }
  679. }
  680. # White space (line) between nodes for readability.
  681. # Each node may report several compatibles.
  682. # For each compatible, multiple drivers may be reported.
  683. # For each driver, multiple CONFIG_ options may be reported.
  684. if ($lines_printed) {
  685. print "\n";
  686. }
  687. }
  688. sub read_dts()
  689. {
  690. my $file = shift;
  691. my $compatible = "";
  692. my $line;
  693. my $node = "";
  694. my $node_enabled = "";
  695. if (! -r $file) {
  696. print STDERR "file '$file' is not readable or does not exist\n";
  697. exit 3;
  698. }
  699. if (!open(DT_FILE, "-|", "$dtx_diff $file")) {
  700. print STDERR "\n";
  701. print STDERR "shell command failed:\n";
  702. print STDERR " $dtx_diff $file\n";
  703. print STDERR "\n";
  704. exit 3;
  705. }
  706. FILE:
  707. while ($line = <DT_FILE>) {
  708. chomp($line);
  709. if ($line =~ /{/) {
  710. &handle_compatible($full_node, $node, $compatible,
  711. $node_enabled);
  712. while ($end_node_count-- > 0) {
  713. pop @full_node;
  714. };
  715. $end_node_count = 0;
  716. $full_node = @full_node[-1];
  717. $node = $line;
  718. $node =~ s/^\s*(.*)\s+\{.*/$1/;
  719. $node =~ s/.*: //;
  720. if ($node eq '/' ) {
  721. $full_node = '/';
  722. } elsif ($full_node ne '/') {
  723. $full_node = $full_node . '/' . $node;
  724. } else {
  725. $full_node = '/' . $node;
  726. }
  727. push @full_node, $full_node;
  728. $compatible = "";
  729. $node_enabled = "";
  730. next FILE;
  731. }
  732. if ($line =~ /}/) {
  733. $end_node_count++;
  734. }
  735. if ($line =~ /(\s+|^)status =/) {
  736. $node_enabled = $line;
  737. $node_enabled =~ s/^\t*//;
  738. $node_enabled =~ s/^status = "//;
  739. $node_enabled =~ s/";$//;
  740. next FILE;
  741. }
  742. if ($line =~ /(\s+|^)compatible =/) {
  743. # Extract all compatible entries for this device
  744. # White space matching here and in handle_compatible() is
  745. # precise, because input format is the output of dtc,
  746. # which is invoked by dtx_diff.
  747. $compatible = $line;
  748. $compatible =~ s/^\t*//;
  749. $compatible =~ s/^compatible = //;
  750. $compatible =~ s/;$//;
  751. }
  752. }
  753. &handle_compatible($full_node, $node, $compatible, $node_enabled);
  754. close(DT_FILE);
  755. }
  756. sub read_config_file()
  757. {
  758. if (! -r $config_file) {
  759. print STDERR "file '$config_file' is not readable or does not exist\n";
  760. exit 2;
  761. }
  762. if (!open(CONFIG_FILE, "<", "$config_file")) {
  763. print STDERR "open $config_file failed\n";
  764. exit 2;
  765. }
  766. my @line;
  767. LINE:
  768. while ($line = <CONFIG_FILE>) {
  769. chomp($line);
  770. next LINE if ($line =~ /^\s*#/);
  771. next LINE if ($line =~ /^\s*$/);
  772. @line = split /=/, $line;
  773. $existing_config{@line[0]} = @line[1];
  774. }
  775. close(CONFIG_FILE);
  776. }
  777. sub cmd_line_err()
  778. {
  779. my $msg = shift;
  780. print STDERR "\n";
  781. print STDERR " ERROR processing command line options\n";
  782. print STDERR " $msg\n" if ($msg ne "");
  783. print STDERR "\n";
  784. print STDERR " For help, type '$script_name --help'\n";
  785. print STDERR "\n";
  786. }
  787. # -----------------------------------------------------------------------------
  788. # program entry point
  789. Getopt::Long::Configure("no_ignore_case", "bundling");
  790. if (!GetOptions(
  791. "c=s" => \$config_file,
  792. "config=s" => \$config_file,
  793. "config-format" => \$config_format,
  794. "exclude-flag=s" => \@exclude_flag,
  795. "h" => \$help,
  796. "help" => \$help,
  797. "black-list-driver" => \$black_list_driver,
  798. "white-list-config" => \$white_list_config,
  799. "white-list-driver" => \$white_list_driver,
  800. "include-flag=s" => \@include_flag,
  801. "include-suspect" => \$include_suspect,
  802. "short-name" => \$short_name,
  803. "show-lists" => \$show_lists,
  804. "version" => \$version,
  805. )) {
  806. &cmd_line_err();
  807. exit 1;
  808. }
  809. my $exit_after_messages = 0;
  810. if ($version) {
  811. print STDERR "\n$script_name $VUFX\n\n";
  812. $exit_after_messages = 1;
  813. }
  814. if ($help) {
  815. &usage;
  816. $exit_after_messages = 1;
  817. }
  818. if ($show_lists) {
  819. print "\n";
  820. print "These compatibles are hard coded to have no driver.\n";
  821. print "\n";
  822. for my $compat (sort keys %compat_white_list) {
  823. print " $compat\n";
  824. }
  825. print "\n\n";
  826. print "The driver for these compatibles is hard coded (white list).\n";
  827. print "\n";
  828. my $max_compat_len = 0;
  829. for my $compat (sort keys %driver_hard_code_list) {
  830. if (length $compat > $max_compat_len) {
  831. $max_compat_len = length $compat;
  832. }
  833. }
  834. for my $compat (sort keys %driver_hard_code_list) {
  835. if (($driver ne "hardcoded_no_driver") && ($driver ne "no_driver")) {
  836. my $first = 1;
  837. for my $driver (@{ $driver_hard_code_list{$compat} }) {
  838. if ($first) {
  839. print " $compat";
  840. print " " x ($max_compat_len - length $compat);
  841. $first = 0;
  842. } else {
  843. print " ", " " x $max_compat_len;
  844. }
  845. print " $driver\n";
  846. }
  847. }
  848. }
  849. print "\n\n";
  850. print "The configuration option for these drivers is hard coded (white list).\n";
  851. print "\n";
  852. my $max_driver_len = 0;
  853. for my $driver (sort keys %driver_config_hard_code_list) {
  854. if (length $driver > $max_driver_len) {
  855. $max_driver_len = length $driver;
  856. }
  857. }
  858. for my $driver (sort keys %driver_config_hard_code_list) {
  859. if (($driver ne "hardcoded_no_driver") && ($driver ne "no_driver")) {
  860. my $first = 1;
  861. for my $config (@{ $driver_config_hard_code_list{$driver} }) {
  862. if ($first) {
  863. print " $driver";
  864. print " " x ($max_driver_len - length $driver);
  865. $first = 0;
  866. } else {
  867. print " ", " " x $max_driver_len;
  868. }
  869. print " $config\n";
  870. }
  871. }
  872. }
  873. print "\n\n";
  874. print "These drivers are black listed.\n";
  875. print "\n";
  876. for my $driver (@black_list_driver) {
  877. print " $driver\n";
  878. }
  879. print "\n";
  880. $exit_after_messages = 1;
  881. }
  882. if ($exit_after_messages) {
  883. exit 0;
  884. }
  885. $exclude_flag_pattern = "[";
  886. for my $exclude_flag (@exclude_flag) {
  887. $exclude_flag_pattern = $exclude_flag_pattern . $exclude_flag;
  888. }
  889. $exclude_flag_pattern = $exclude_flag_pattern . "]";
  890. # clean up if empty
  891. $exclude_flag_pattern =~ s/^\[\]$//;
  892. $include_flag_pattern = "[";
  893. for my $include_flag (@include_flag) {
  894. $include_flag_pattern = $include_flag_pattern . $include_flag;
  895. }
  896. $include_flag_pattern = $include_flag_pattern . "]";
  897. # clean up if empty
  898. $include_flag_pattern =~ s/^\[\]$//;
  899. if ($exclude_flag_pattern) {
  900. my $found = 0;
  901. for $pr_flag_value (@pr_flag_value) {
  902. if ($exclude_flag_pattern =~ m/$pr_flag_value/) {
  903. $found = 1;
  904. }
  905. }
  906. if (!$found) {
  907. &cmd_line_err("invalid value for FLAG in --exclude-flag\n");
  908. exit 1
  909. }
  910. }
  911. if ($include_flag_pattern) {
  912. my $found = 0;
  913. for $pr_flag_value (@pr_flag_value) {
  914. if ($include_flag_pattern =~ m/$pr_flag_value/) {
  915. $found = 1;
  916. }
  917. }
  918. if (!$found) {
  919. &cmd_line_err("invalid value for FLAG in --include-flag\n");
  920. exit 1
  921. }
  922. }
  923. if ($include_suspect) {
  924. $include_flag_pattern =~ s/\[//;
  925. $include_flag_pattern =~ s/\]//;
  926. $include_flag_pattern = "[" . $include_flag_pattern . "A-Z]";
  927. }
  928. if ($exclude_flag_pattern =~ m/$include_flag_pattern/) {
  929. &cmd_line_err("the same flag appears in both --exclude-flag and --include-flag or --include-suspect\n");
  930. exit 1
  931. }
  932. # ($#ARGV < 0) is valid for --help, --version
  933. if ($#ARGV < 0) {
  934. &cmd_line_err("device-tree... is required");
  935. exit 1
  936. }
  937. if ($config_file) {
  938. &read_config_file();
  939. }
  940. # avoid pushing duplicates for this value
  941. $driver = "hardcoded_no_driver";
  942. for $config ( @{ $driver_config_hard_code_list{$driver} } ) {
  943. push @{ $driver_config{$driver} }, $config;
  944. }
  945. if ($white_list_driver) {
  946. for my $compat (keys %driver_hard_code_list) {
  947. for my $driver (@{ $driver_hard_code_list{$compat} }) {
  948. push @{ $compat_driver{$compat} }, $driver;
  949. if ($driver ne "hardcoded_no_driver") {
  950. $driver_count{$compat} = scalar @{ $compat_driver{$compat} };
  951. }
  952. }
  953. }
  954. }
  955. if ($white_list_config) {
  956. for my $driver (keys %driver_config_hard_code_list) {
  957. if ($driver ne "hardcoded_no_driver") {
  958. for $config ( @{ $driver_config_hard_code_list{$driver} } ) {
  959. push @{ $driver_config{$driver} }, $config;
  960. }
  961. }
  962. }
  963. }
  964. if (-x "scripts/dtc/dtx_diff") {
  965. $dtx_diff = "scripts/dtc/dtx_diff";
  966. } else {
  967. print STDERR "\n";
  968. print STDERR "$script_name must be run from the root directory of a Linux kernel tree\n";
  969. print STDERR "\n";
  970. exit 3;
  971. }
  972. for $file (@ARGV) {
  973. &read_dts($file);
  974. }