udev-hwdb-update.pl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. sub usb_vendor {
  5. my $vendor;
  6. open(IN, "<", "usb.ids");
  7. open(OUT, ">", "20-usb-vendor-model.hwdb");
  8. print(OUT "# This file is part of systemd.\n" .
  9. "#\n" .
  10. "# Data imported from: http://www.linux-usb.org/usb.ids\n");
  11. while (my $line = <IN>) {
  12. $line =~ s/\s+$//;
  13. $line =~ m/^([0-9a-f]{4})\s*(.+)$/;
  14. if (defined $1) {
  15. $vendor = uc $1;
  16. my $text = $2;
  17. print(OUT "\n");
  18. print(OUT "usb:v" . $vendor . "*\n");
  19. print(OUT " ID_VENDOR_FROM_DATABASE=" . $text . "\n");
  20. next;
  21. }
  22. $line =~ m/^\t([0-9a-f]{4})\s*(.+)$/;
  23. if (defined $1) {
  24. my $model = uc $1;
  25. my $text = $2;
  26. print(OUT "\n");
  27. print(OUT "usb:v" . $vendor . "p" . $model . "*\n");
  28. print(OUT " ID_MODEL_FROM_DATABASE=" . $text . "\n");
  29. }
  30. }
  31. close(IN);
  32. close(OUT);
  33. }
  34. sub usb_classes {
  35. my $class;
  36. my $subclass;
  37. my $protocol;
  38. open(IN, "<", "usb.ids");
  39. open(OUT, ">", "20-usb-classes.hwdb");
  40. print(OUT "# This file is part of systemd.\n" .
  41. "#\n" .
  42. "# Data imported from: http://www.linux-usb.org/usb.ids\n");
  43. while (my $line = <IN>) {
  44. $line =~ s/\s+$//;
  45. $line =~ m/^C\ ([0-9a-f]{2})\s*(.+)$/;
  46. if (defined $1) {
  47. $class = uc $1;
  48. if ($class =~ m/^00$/) {
  49. next;
  50. }
  51. my $text = $2;
  52. print(OUT "\n");
  53. print(OUT "usb:v*p*d*dc" . $class . "*\n");
  54. print(OUT " ID_USB_CLASS_FROM_DATABASE=" . $text . "\n");
  55. next;
  56. }
  57. if (not defined $class) {
  58. next;
  59. } elsif ($line =~ m/^$/) {
  60. last;
  61. }
  62. $line =~ m/^\t([0-9a-f]{2})\s*(.+)$/;
  63. if (defined $1) {
  64. $subclass = uc $1;
  65. if ($subclass =~ m/^00$/) {
  66. next;
  67. }
  68. my $text = $2;
  69. if ($text =~ m/^(\?|None|Unused)$/) {
  70. next;
  71. }
  72. print(OUT "\n");
  73. print(OUT "usb:v*p*d*dc" . $class . "dsc" . $subclass . "*\n");
  74. print(OUT " ID_USB_SUBCLASS_FROM_DATABASE=" . $text . "\n");
  75. next;
  76. }
  77. $line =~ m/^\t\t([0-9a-f]{2})\s*(.+)$/;
  78. if (defined $1) {
  79. $protocol = uc $1;
  80. my $text = $2;
  81. if ($text =~ m/^(\?|None|Unused)$/) {
  82. next;
  83. }
  84. print(OUT "\n");
  85. print(OUT "usb:v*p*d*dc" . $class . "dsc" . $subclass . "dp" . $protocol . "*\n");
  86. print(OUT " ID_USB_PROTOCOL_FROM_DATABASE=" . $text . "\n");
  87. }
  88. }
  89. close(IN);
  90. close(OUT);
  91. }
  92. sub pci_vendor {
  93. my $vendor;
  94. my $device;
  95. my $device_text;
  96. open(IN, "<", "pci.ids");
  97. open(OUT, ">", "20-pci-vendor-model.hwdb");
  98. print(OUT "# This file is part of systemd.\n" .
  99. "#\n" .
  100. "# Data imported from: http://pci-ids.ucw.cz/v2.2/pci.ids\n");
  101. while (my $line = <IN>) {
  102. $line =~ s/\s+$//;
  103. $line =~ m/^([0-9a-f]{4})\s*(.+)$/;
  104. if (defined $1) {
  105. $vendor = uc $1;
  106. my $text = $2;
  107. print(OUT "\n");
  108. print(OUT "pci:v0000" . $vendor . "*\n");
  109. print(OUT " ID_VENDOR_FROM_DATABASE=" . $text . "\n");
  110. next;
  111. }
  112. $line =~ m/^\t([0-9a-f]{4})\s*(.+)$/;
  113. if (defined $1) {
  114. $device = uc $1;
  115. $device_text = $2;
  116. print(OUT "\n");
  117. print(OUT "pci:v0000" . $vendor . "d0000" . $device . "*\n");
  118. print(OUT " ID_MODEL_FROM_DATABASE=" . $device_text . "\n");
  119. next;
  120. }
  121. $line =~ m/^\t\t([0-9a-f]{4})\s*([0-9a-f]{4})\s*(.*)$/;
  122. if (defined $1) {
  123. my $sub_vendor = uc $1;
  124. my $sub_device = uc $2;
  125. my $sub_text = $3;
  126. $sub_text =~ s/^\Q$device_text\E\s*//;
  127. $sub_text =~ s/(.+)/\ ($1)/;
  128. print(OUT "\n");
  129. print(OUT "pci:v0000" . $vendor . "d0000" . $device . "sv0000" . $sub_vendor . "sd0000" . $sub_device . "*\n");
  130. print(OUT " ID_MODEL_FROM_DATABASE=" . $device_text . $sub_text . "\n");
  131. }
  132. }
  133. close(IN);
  134. close(OUT);
  135. }
  136. sub pci_classes {
  137. my $class;
  138. my $subclass;
  139. my $interface;
  140. open(IN, "<", "pci.ids");
  141. open(OUT, ">", "20-pci-classes.hwdb");
  142. print(OUT "# This file is part of systemd.\n" .
  143. "#\n" .
  144. "# Data imported from: http://pci-ids.ucw.cz/v2.2/pci.ids\n");
  145. while (my $line = <IN>) {
  146. $line =~ s/\s+$//;
  147. $line =~ m/^C\ ([0-9a-f]{2})\s*(.+)$/;
  148. if (defined $1) {
  149. $class = uc $1;
  150. my $text = $2;
  151. print(OUT "\n");
  152. print(OUT "pci:v*d*sv*sd*bc" . $class . "*\n");
  153. print(OUT " ID_PCI_CLASS_FROM_DATABASE=" . $text . "\n");
  154. next;
  155. }
  156. if (not defined $class) {
  157. next;
  158. } elsif ($line =~ m/^$/) {
  159. last;
  160. }
  161. $line =~ m/^\t([0-9a-f]{2})\s*(.+)$/;
  162. if (defined $1) {
  163. $subclass = uc $1;
  164. my $text = $2;
  165. print(OUT "\n");
  166. print(OUT "pci:v*d*sv*sd*bc" . $class . "sc" . $subclass . "*\n");
  167. print(OUT " ID_PCI_SUBCLASS_FROM_DATABASE=" . $text . "\n");
  168. next;
  169. }
  170. $line =~ m/^\t\t([0-9a-f]{2})\s*(.+)$/;
  171. if (defined $1) {
  172. $interface = uc $1;
  173. my $text = $2;
  174. print(OUT "\n");
  175. print(OUT "pci:v*d*sv*sd*bc" . $class . "sc" . $subclass . "i" . $interface . "*\n");
  176. print(OUT " ID_PCI_INTERFACE_FROM_DATABASE=" . $text . "\n");
  177. }
  178. }
  179. close(IN);
  180. close(OUT);
  181. }
  182. sub sdio_vendor {
  183. my $vendor;
  184. my $device;
  185. open(IN, "<", "sdio.ids");
  186. open(OUT, ">", "20-sdio-vendor-model.hwdb");
  187. print(OUT "# This file is part of systemd.\n" .
  188. "#\n" .
  189. "# Data imported from: hwdb/sdio.ids\n");
  190. while (my $line = <IN>) {
  191. $line =~ s/\s+$//;
  192. $line =~ m/^([0-9a-f]{4})\s*(.+)$/;
  193. if (defined $1) {
  194. $vendor = uc $1;
  195. my $text = $2;
  196. print(OUT "\n");
  197. print(OUT "sdio:c*v" . $vendor . "*\n");
  198. print(OUT " ID_VENDOR_FROM_DATABASE=" . $text . "\n");
  199. next;
  200. }
  201. $line =~ m/^\t([0-9a-f]{4})\s*(.+)$/;
  202. if (defined $1) {
  203. $device = uc $1;
  204. my $text = $2;
  205. print(OUT "\n");
  206. print(OUT "sdio:c*v" . $vendor . "d" . $device . "*\n");
  207. print(OUT " ID_MODEL_FROM_DATABASE=" . $text . "\n");
  208. next;
  209. }
  210. }
  211. close(IN);
  212. close(OUT);
  213. }
  214. sub sdio_classes {
  215. my $class;
  216. my $subclass;
  217. my $interface;
  218. open(IN, "<", "sdio.ids");
  219. open(OUT, ">", "20-sdio-classes.hwdb");
  220. print(OUT "# This file is part of systemd.\n" .
  221. "#\n" .
  222. "# Data imported from: hwdb/sdio.ids\n");
  223. while (my $line = <IN>) {
  224. $line =~ s/\s+$//;
  225. $line =~ m/^C\ ([0-9a-f]{2})\s*(.+)$/;
  226. if (defined $1) {
  227. $class = uc $1;
  228. my $text = $2;
  229. print(OUT "\n");
  230. print(OUT "sdio:c" . $class . "v*d*\n");
  231. print(OUT " ID_SDIO_CLASS_FROM_DATABASE=" . $text . "\n");
  232. next;
  233. }
  234. }
  235. close(IN);
  236. close(OUT);
  237. }
  238. # MAC Address Block Large/Medium/Small
  239. # Large MA-L 24/24 bit (OUI)
  240. # Medium MA-M 28/20 bit (OUI prefix owned by IEEE)
  241. # Small MA-S 36/12 bit (OUI prefix owned by IEEE)
  242. sub oui {
  243. my $prefix;
  244. my %ieee_prefixes = ();
  245. open(OUT, ">", "20-OUI.hwdb");
  246. print(OUT "# This file is part of systemd.\n" .
  247. "#\n" .
  248. "# Data imported from:\n" .
  249. "# https://services13.ieee.org/RST/standards-ra-web/rest/assignments/download/?registry=MA-L&format=txt\n" .
  250. "# https://services13.ieee.org/RST/standards-ra-web/rest/assignments/download/?registry=MA-M&format=txt\n" .
  251. "# https://services13.ieee.org/RST/standards-ra-web/rest/assignments/download/?registry=MA-S&format=txt\n");
  252. open(IN, "<", "ma-small.txt");
  253. while (my $line = <IN>) {
  254. $line =~ s/^ +//;
  255. $line =~ s/\s+$//;
  256. $line =~ m/^([0-9A-F]{2})-([0-9A-F]{2})-([0-9A-F]{2})\s*\(hex\)\s*.+$/;
  257. if (defined $1) {
  258. $prefix = $1 . $2 . $3;
  259. $ieee_prefixes{ $prefix } = 1;
  260. next;
  261. }
  262. $line =~ m/^([0-9A-F]{3})000-\g1FFF\s*\(base 16\)\s*(.+)$/;
  263. if (defined $1) {
  264. my $vendor = uc $1;
  265. my $text = $2;
  266. print(OUT "\n");
  267. print(OUT "OUI:" . $prefix . $vendor . "*\n");
  268. print(OUT " ID_OUI_FROM_DATABASE=" . $text . "\n");
  269. }
  270. }
  271. close(IN);
  272. open(IN, "<", "ma-medium.txt");
  273. while (my $line = <IN>) {
  274. $line =~ s/^ +//;
  275. $line =~ s/\s+$//;
  276. $line =~ m/^([0-9A-F]{2})-([0-9A-F]{2})-([0-9A-F]{2})\s*\(hex\)\s*.+$/;
  277. if (defined $1) {
  278. $prefix = $1 . $2 . $3;
  279. $ieee_prefixes{ $prefix } = 1;
  280. next;
  281. }
  282. $line =~ m/^([0-9A-F])00000-\g1FFFFF\s*\(base 16\)\s*(.+)$/;
  283. if (defined $1) {
  284. my $vendor = uc $1;
  285. my $text = $2;
  286. print(OUT "\n");
  287. print(OUT "OUI:" . $prefix . $vendor . "*\n");
  288. print(OUT " ID_OUI_FROM_DATABASE=" . $text . "\n");
  289. }
  290. }
  291. open(IN, "<", "ma-large.txt");
  292. while (my $line = <IN>) {
  293. $line =~ s/^ +//;
  294. $line =~ s/\s+$//;
  295. $line =~ m/^([0-9A-F]{6})\s*\(base 16\)\s*(.+)$/;
  296. if (defined $1) {
  297. my $vendor = uc $1;
  298. my $text = $2;
  299. if ($text =~ m/^IEEE REGISTRATION AUTHORITY/) {
  300. next;
  301. }
  302. # skip the IEEE owned prefixes
  303. if (! exists $ieee_prefixes{ $vendor }) {
  304. print(OUT "\n");
  305. print(OUT "OUI:" . $vendor . "*\n");
  306. print(OUT " ID_OUI_FROM_DATABASE=" . $text . "\n");
  307. }
  308. }
  309. }
  310. close(IN);
  311. close(OUT);
  312. }
  313. usb_vendor();
  314. usb_classes();
  315. pci_vendor();
  316. pci_classes();
  317. sdio_vendor();
  318. sdio_classes();
  319. oui();