mirror of the now-defunct rocklinux.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

332 lines
9.4 KiB

  1. #!/bin/gawk -f
  2. #
  3. # Usually I'd write this in perl. But this should also work on a system
  4. # with no perl installed, so it's in awk ...
  5. function debug(text) {
  6. if ( print_debug )
  7. print text > "/dev/stderr";
  8. }
  9. function autocomplete(mod, id) {
  10. driver_initrd[id] = 1;
  11. if ( mod ~ /^snd-/ ) {
  12. driver_mod[id] = driver_mod[id] \
  13. " snd-pcm-oss snd-seq-oss snd-mixer-oss";
  14. driver_initrd[id] = 0;
  15. }
  16. if ( modidx[mod] ~ "/video/" ) {
  17. driver_cmd[id] = driver_cmd[id] \
  18. "\n:touch /dev/vc/{1,2,3,4,5,6}" \
  19. "\n:fbset -a 800x600-60" \
  20. "\nchvt 2; sleep 1; chvt 1";
  21. driver_initrd[id] = 0;
  22. }
  23. }
  24. function get_pci_drivers() {
  25. debug("Reading /lib/modules/" kernel "/modules.pcimap.");
  26. while ( (getline < ("/lib/modules/" kernel "/modules.pcimap")) > 0 ) {
  27. id = sprintf( "%04x%04x", and(strtonum($2),0xffff),
  28. and(strtonum($3),0xffff));
  29. # always prefer ALSA drivers if available
  30. if ( id in pci_driver && pci_driver[id] ~ /^snd-/ ) continue;
  31. pci_driver[id] = $1;
  32. }
  33. while ( (getline < "/usr/share/pci.ids") > 0 ) {
  34. if ( /^\t\t/ ) {
  35. id = $1 $2;
  36. sub("^\t+[0-9a-f]+[ \t]+[0-9a-f]+[ \t]+", "");
  37. pci_descs[id] = vendor_name " " $0;
  38. } else
  39. if ( /^\t/ ) {
  40. id = vendor_id $1;
  41. sub("^\t+[0-9a-f]+[ \t]+", "");
  42. pci_descs[id] = vendor_name " " $0;
  43. } else {
  44. vendor_id = $1;
  45. sub("^[0-9a-f]+[ \t]+", "");
  46. vendor_name = $0;
  47. }
  48. }
  49. while ( (getline < "/proc/bus/pci/devices") > 0 ) {
  50. id = "pci" $1 " (Device " $2 ")";
  51. driver_mod[id] = pci_driver[$2];
  52. driver_dsc[id] = pci_descs[$2];
  53. driver_cmd[id] = "";
  54. drivers[id] = id;
  55. autocomplete(pci_driver[$2], id);
  56. }
  57. }
  58. function get_isapnp_drivers() {
  59. debug("Reading /lib/modules/" kernel "/modules.isapnpmap.");
  60. while ( (getline < ("/lib/modules/" kernel "/modules.isapnpmap")) > 0 ) {
  61. vendor = strtonum($2);
  62. device = strtonum($3);
  63. # ugly mangling performed in the kernel ...
  64. id = sprintf ("%c%c%c%x%x%x%x",
  65. 65 + and (vendor / 4, 0x3f) - 1,
  66. 65 + or (and (vendor, 3) * 8, and (vendor / 8192, 7) ) - 1,
  67. 65 + and (vendor / 256, 0x1f) - 1,
  68. and (device / 16, 0x0f),
  69. and (device, 0x0f),
  70. and (device / 4096, 0x0f),
  71. and (device / 256, 0x0f) );
  72. isapnp_driver[id] = $1;
  73. }
  74. while ( (getline < "/proc/bus/isapnp/devices") > 0 ) {
  75. id = substr ($2,0,7);
  76. iid = "isapnp" id;
  77. driver_mod[iid] = isapnp_driver[id];
  78. driver_dsc[iid] = "ISA PnP (" id ")"; #isapnp_descs[id];
  79. driver_cmd[iid] = "";
  80. drivers[iid] = iid;
  81. autocomplete(isapnp_driver[id], iid);
  82. }
  83. }
  84. function match_usb_dev() {
  85. if ( usb_device[ "Vendor" ] == 0 && usb_device[ "ProdID" ] == 0 ) return;
  86. usbmap_idx[ "Vendor" ] = 1;
  87. usbmap_idx[ "ProdID" ] = 2;
  88. usbmap_idx[ "DevLo" ] = 4;
  89. usbmap_idx[ "DevHi" ] = 8;
  90. usbmap_idx[ "DevCls" ] = 16;
  91. usbmap_idx[ "DevSub" ] = 32;
  92. usbmap_idx[ "DevProt" ] = 64;
  93. usbmap_idx[ "IfCls" ] = 128;
  94. usbmap_idx[ "IfSub" ] = 256;
  95. usbmap_idx[ "IfProt" ] = 512;
  96. usbmap_idx[ "DrvInfo" ] = 1024;
  97. usb_modlist="";
  98. for ( usb_driver_id=0; usb_driver_id < usb_driver_c; usb_driver_id++ ) {
  99. found=1;
  100. for ( usbmap_idx_id in usbmap_idx ) {
  101. if ( and(usb_driver[usb_driver_id, "match"], usbmap_idx[usbmap_idx_id]) ) {
  102. if ( (usb_driver[usb_driver_id, usbmap_idx_id] != \
  103. usb_device[usbmap_idx_id]) ) found=0;
  104. }
  105. }
  106. if (found) {
  107. if ( usb_modlist != "" ) usb_modlist = usb_modlist " ";
  108. usb_modlist = usb_modlist usb_driver[usb_driver_id, "mod"];
  109. }
  110. }
  111. if ( usb_modlist != "" ) {
  112. id = "usb-device " usb_device[ "Vendor" ] ":" usb_device[ "ProdID" ] " (" usb_modlist ")";
  113. driver_mod[id] = usb_modlist;
  114. driver_dsc[id] = usb_device[ "Desc" ] " (" usb_modlist ")";
  115. gsub(" +", " ", driver_dsc[id]);
  116. driver_cmd[id] = "";
  117. drivers[id] = id;
  118. autocomplete(pci_driver[$2], id);
  119. }
  120. }
  121. function get_usb_flag(name) {
  122. split($0, usb_flags, " +");
  123. for ( usb_flag_id in usb_flags ) {
  124. split(usb_flags[usb_flag_id], usb_flag, "[=(]");
  125. if ( usb_flag[1] == name ) return strtonum("0x" usb_flag[2]);
  126. }
  127. return 0;
  128. }
  129. function get_usb_drivers() {
  130. while ( ("lspci -v" | getline) > 0 ) {
  131. if ( / USB Controller: / ) {
  132. usb_ctrl_desc="";
  133. if ( /prog-if.*OHCI/ )
  134. { usb_ctrl_driver="usb-ohci"; usb_ctrl_desc="OHCI"; }
  135. if ( /prog-if.*UHCI/ )
  136. { usb_ctrl_driver="usb-uhci"; usb_ctrl_desc="UHCI"; }
  137. if ( usb_ctrl_desc != "" ) {
  138. sub("^[^ ]*", ""); sub("\\(rev .*$", "");
  139. usb_ctrl_desc = usb_ctrl_desc $0;
  140. break;
  141. }
  142. }
  143. }
  144. if ( usb_ctrl_driver && modidx[usb_ctrl_driver] ) {
  145. debug("Found USB " usb_ctrl_driver " device in lspci output.");
  146. id = "usb-controller (PCI " usb_ctrl_driver ")";
  147. driver_mod[id] = usb_ctrl_driver;
  148. driver_dsc[id] = usb_ctrl_desc;
  149. driver_cmd[id] = "\nmount -t usbfs none /proc/bus/usb";
  150. drivers[id] = id;
  151. autocomplete(usb_ctrl_driver, id);
  152. }
  153. debug("Reading /lib/modules/" kernel "/modules.usbmap.");
  154. for ( usb_driver_c=0;
  155. (getline < ("/lib/modules/" kernel "/modules.usbmap")) > 0;
  156. usb_driver_c++ ) {
  157. if ( $1 == "#" ) { usb_driver_c--; continue; }
  158. usb_driver[usb_driver_c, "mod" ] = $1;
  159. usb_driver[usb_driver_c, "match" ] = strtonum($2);
  160. usb_driver[usb_driver_c, "Vendor" ] = strtonum($3);
  161. usb_driver[usb_driver_c, "ProdID" ] = strtonum($4);
  162. usb_driver[usb_driver_c, "DevLo" ] = strtonum($5);
  163. usb_driver[usb_driver_c, "DevHi" ] = strtonum($6);
  164. usb_driver[usb_driver_c, "DevCls" ] = strtonum($7);
  165. usb_driver[usb_driver_c, "DevSub" ] = strtonum($8);
  166. usb_driver[usb_driver_c, "DevProt" ] = strtonum($9);
  167. usb_driver[usb_driver_c, "IfCls" ] = strtonum($10);
  168. usb_driver[usb_driver_c, "IfSub" ] = strtonum($11);
  169. usb_driver[usb_driver_c, "IfProt" ] = strtonum($12);
  170. usb_driver[usb_driver_c, "DrvInfo" ] = strtonum($13);
  171. }
  172. while ( (getline < "/proc/bus/usb/devices") > 0 ) {
  173. if ( $1 == "T:" ) {
  174. if ( usb_device["Vendor"] != "" ) match_usb_dev();
  175. usb_device[ "Vendor" ] = 0;
  176. usb_device[ "ProdID" ] = 0;
  177. usb_device[ "DevLo" ] = 0; # FIXME: Where is this value in bus/usb/devices?
  178. usb_device[ "DevHi" ] = 0; # FIXME: Where is this value in bus/usb/devices?
  179. usb_device[ "DevCls" ] = 0;
  180. usb_device[ "DevSub" ] = 0;
  181. usb_device[ "DevProt" ] = 0;
  182. usb_device[ "IfCls" ] = 0;
  183. usb_device[ "IfSub" ] = 0;
  184. usb_device[ "IfProt" ] = 0;
  185. usb_device[ "DrvInfo" ] = 0; # FIXME: Where is this value in bus/usb/devices?
  186. usb_device[ "Desc" ] = "USB:";
  187. }
  188. if ( $1 == "P:" ) {
  189. usb_device[ "Vendor" ] = get_usb_flag("Vendor");
  190. usb_device[ "ProdID" ] = get_usb_flag("ProdID");
  191. }
  192. if ( $1 == "D:" ) {
  193. usb_device[ "DevCls" ] = get_usb_flag("Cls");
  194. usb_device[ "DevSub" ] = get_usb_flag("Sub");
  195. usb_device[ "DevProt" ] = get_usb_flag("Sub");
  196. }
  197. if ( $1 == "I:" ) {
  198. usb_device[ "IfCls" ] = get_usb_flag("Cls");
  199. usb_device[ "IfSub" ] = get_usb_flag("Sub");
  200. usb_device[ "IfProt" ] = get_usb_flag("Prot");
  201. }
  202. if ( $1 == "S:" ) {
  203. if ( ! match($0, "SerialNumber") ) {
  204. sub(".*=", "");
  205. usb_device[ "Desc" ] = usb_device[ "Desc" ] " " $0;
  206. }
  207. }
  208. }
  209. if ( usb_device["Vendor"] != "" ) match_usb_dev();
  210. }
  211. function print_driver(id) {
  212. if ( driver_initrd[id] ) {
  213. tmp = driver_mod[id];
  214. gsub(" +", "\nmodprobe ", tmp);
  215. tmp = "modprobe " tmp driver_cmd[id];
  216. } else {
  217. tmp = driver_mod[id] "\t#no-initrd";
  218. gsub(" +", "\t#no-initrd\nmodprobe ", tmp);
  219. tmp = "modprobe " tmp driver_cmd[id];
  220. }
  221. if ( disable_default ) {
  222. gsub("\n", "\n#", tmp);
  223. tmp = "#" tmp;
  224. }
  225. if ( driver_dsc[id] == "" ) {
  226. tmp2 = driver_mod[id]; gsub(" .*", "", tmp2);
  227. driver_dsc[id] = "Unkown device for driver " tmp2;
  228. }
  229. if ( file ) {
  230. print "New Device: " id;
  231. print "\n### " id " ###" >> file;
  232. print "# " driver_dsc[id] >> file;
  233. if ( print_echos )
  234. print "echo '" driver_dsc[id] "'" >> file;
  235. print tmp >> file;
  236. } else {
  237. print "\n### " id " ###";
  238. print "# " driver_dsc[id];
  239. if ( print_echos )
  240. print "echo '" driver_dsc[id] "'";
  241. print tmp;
  242. }
  243. }
  244. BEGIN {
  245. for (i=1; i<ARGC; i++) {
  246. if ( ARGV[i] == "-k" )
  247. kernel = ARGV[++i];
  248. else if ( ARGV[i] == "-s" )
  249. file = ARGV[++i];
  250. else if ( ARGV[i] == "-d" )
  251. disable_default = 1;
  252. else if ( ARGV[i] == "-D" )
  253. print_debug = 1;
  254. else if ( ARGV[i] == "-V" )
  255. print_echos = 1;
  256. else {
  257. print "\n" \
  258. "HWScan - ROCK Linux (www.rocklinux.org)" "\n" \
  259. "Copyright 2003 Clifford Wolf <clifford@clifford.at>" "\n" \
  260. "This is free software with ABSOLUTELY NO WARRANTY." "\n" \
  261. "\n" \
  262. "Usage: hwscan [ [--] options ]" "\n" \
  263. "\n" \
  264. " -k <kernel-version> ......... use /lib/modules/<thisvalue>/" "\n" \
  265. " -s <hw-init-script> ......... e.g. /etc/conf/kernel" "\n" \
  266. " -d .......................... disable new driver on default" "\n" \
  267. " -D .......................... debug (print auto-detected values)" "\n" \
  268. " -V .......................... verbose (add echo's)" "\n";
  269. exit 1;
  270. }
  271. }
  272. if ( ! kernel ) {
  273. if ( ("cat /proc/version" | getline) > 0 ) {
  274. kernel = $3;
  275. } else {
  276. kernel = "*";
  277. }
  278. debug("Auto-detected kernel version: " kernel);
  279. }
  280. for ( c=0; (("find /lib/modules/" kernel \
  281. "/. -name '*.o' -printf '%P %f\n'") | getline) > 0; c++ ) {
  282. sub("\\.o$", "");
  283. modidx[$2] = $1;
  284. }
  285. debug("Found " c " modules in /lib/modules/" kernel ".");
  286. while ( file && (getline < file) > 0 ) {
  287. if ( /^### .* ###$/ ) {
  288. sub("^### ", "");
  289. sub(" ###$", "");
  290. skip[$0] = 1;
  291. debug("Mark existing driver " $0 " to be skipped.");
  292. }
  293. }
  294. get_pci_drivers();
  295. get_isapnp_drivers();
  296. get_usb_drivers();
  297. asort(drivers);
  298. for (i in drivers) {
  299. id = drivers[i];
  300. if ( driver_mod[id] && ! skip[id] ) {
  301. print_driver(id);
  302. }
  303. }
  304. }