dt_to_config 41 KB

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