genregdb.awk 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/usr/bin/awk -f
  2. #
  3. # genregdb.awk -- generate regdb.c from db.txt
  4. #
  5. # Actually, it reads from stdin (presumed to be db.txt) and writes
  6. # to stdout (presumed to be regdb.c), but close enough...
  7. #
  8. # Copyright 2009 John W. Linville <linville@tuxdriver.com>
  9. #
  10. # Permission to use, copy, modify, and/or distribute this software for any
  11. # purpose with or without fee is hereby granted, provided that the above
  12. # copyright notice and this permission notice appear in all copies.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  15. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  16. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  17. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  18. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  19. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  20. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21. BEGIN {
  22. active = 0
  23. rules = 0;
  24. print "/*"
  25. print " * DO NOT EDIT -- file generated from data in db.txt"
  26. print " */"
  27. print ""
  28. print "#include <linux/nl80211.h>"
  29. print "#include <net/cfg80211.h>"
  30. print "#include \"regdb.h\""
  31. print ""
  32. regdb = "const struct ieee80211_regdomain *reg_regdb[] = {\n"
  33. }
  34. function parse_country_head() {
  35. country=$2
  36. sub(/:/, "", country)
  37. printf "static const struct ieee80211_regdomain regdom_%s = {\n", country
  38. printf "\t.alpha2 = \"%s\",\n", country
  39. if ($NF ~ /DFS-ETSI/)
  40. printf "\t.dfs_region = NL80211_DFS_ETSI,\n"
  41. else if ($NF ~ /DFS-FCC/)
  42. printf "\t.dfs_region = NL80211_DFS_FCC,\n"
  43. else if ($NF ~ /DFS-JP/)
  44. printf "\t.dfs_region = NL80211_DFS_JP,\n"
  45. printf "\t.reg_rules = {\n"
  46. active = 1
  47. regdb = regdb "\t&regdom_" country ",\n"
  48. }
  49. function parse_reg_rule()
  50. {
  51. flag_starts_at = 7
  52. start = $1
  53. sub(/\(/, "", start)
  54. end = $3
  55. bw = $5
  56. sub(/\),/, "", bw)
  57. gain = 0
  58. power = $6
  59. # power might be in mW...
  60. units = $7
  61. dfs_cac = 0
  62. sub(/\(/, "", power)
  63. sub(/\),/, "", power)
  64. sub(/\),/, "", units)
  65. sub(/\)/, "", units)
  66. if (units == "mW") {
  67. flag_starts_at = 8
  68. power = 10 * log(power)/log(10)
  69. if ($8 ~ /[[:digit:]]/) {
  70. flag_starts_at = 9
  71. dfs_cac = $8
  72. }
  73. } else {
  74. if ($7 ~ /[[:digit:]]/) {
  75. flag_starts_at = 8
  76. dfs_cac = $7
  77. }
  78. }
  79. sub(/\(/, "", dfs_cac)
  80. sub(/\),/, "", dfs_cac)
  81. flagstr = ""
  82. for (i=flag_starts_at; i<=NF; i++)
  83. flagstr = flagstr $i
  84. split(flagstr, flagarray, ",")
  85. flags = ""
  86. for (arg in flagarray) {
  87. if (flagarray[arg] == "NO-OFDM") {
  88. flags = flags "\n\t\t\tNL80211_RRF_NO_OFDM | "
  89. } else if (flagarray[arg] == "NO-CCK") {
  90. flags = flags "\n\t\t\tNL80211_RRF_NO_CCK | "
  91. } else if (flagarray[arg] == "NO-INDOOR") {
  92. flags = flags "\n\t\t\tNL80211_RRF_NO_INDOOR | "
  93. } else if (flagarray[arg] == "NO-OUTDOOR") {
  94. flags = flags "\n\t\t\tNL80211_RRF_NO_OUTDOOR | "
  95. } else if (flagarray[arg] == "DFS") {
  96. flags = flags "\n\t\t\tNL80211_RRF_DFS | "
  97. } else if (flagarray[arg] == "PTP-ONLY") {
  98. flags = flags "\n\t\t\tNL80211_RRF_PTP_ONLY | "
  99. } else if (flagarray[arg] == "PTMP-ONLY") {
  100. flags = flags "\n\t\t\tNL80211_RRF_PTMP_ONLY | "
  101. } else if (flagarray[arg] == "PASSIVE-SCAN") {
  102. flags = flags "\n\t\t\tNL80211_RRF_NO_IR | "
  103. } else if (flagarray[arg] == "NO-IBSS") {
  104. flags = flags "\n\t\t\tNL80211_RRF_NO_IR | "
  105. } else if (flagarray[arg] == "NO-IR") {
  106. flags = flags "\n\t\t\tNL80211_RRF_NO_IR | "
  107. } else if (flagarray[arg] == "AUTO-BW") {
  108. flags = flags "\n\t\t\tNL80211_RRF_AUTO_BW | "
  109. }
  110. }
  111. flags = flags "0"
  112. printf "\t\tREG_RULE_EXT(%d, %d, %d, %d, %.0f, %d, %s),\n", start, end, bw, gain, power, dfs_cac, flags
  113. rules++
  114. }
  115. function print_tail_country()
  116. {
  117. active = 0
  118. printf "\t},\n"
  119. printf "\t.n_reg_rules = %d\n", rules
  120. printf "};\n\n"
  121. rules = 0;
  122. }
  123. /^[ \t]*#/ {
  124. # Ignore
  125. }
  126. !active && /^[ \t]*$/ {
  127. # Ignore
  128. }
  129. !active && /country/ {
  130. parse_country_head()
  131. }
  132. active && /^[ \t]*\(/ {
  133. parse_reg_rule()
  134. }
  135. active && /^[ \t]*$/ {
  136. print_tail_country()
  137. }
  138. END {
  139. if (active)
  140. print_tail_country()
  141. print regdb "};"
  142. print ""
  143. print "int reg_regdb_size = ARRAY_SIZE(reg_regdb);"
  144. }