1#!/usr/bin/perl 2use strict; 3 4my %headers = ( 5 "video" => "videodev2.h", 6 "subdev" => "v4l2-subdev.h", 7 "frontend" => "dvb/frontend.h", 8 "demux" => "dvb/dmx.h", 9); 10 11my %structs; 12my %ioc; 13my $size_ioc = 0; 14my $size_struct = 0; 15 16sub print_union() 17{ 18 printf "union v4l_parms {\n"; 19 printf "\tint i;\n"; 20 printf "\tunsigned long ulong;\n"; 21 printf "\tu_int32_t u32;\n"; 22 printf "\tv4l2_std_id id;\n"; 23 printf "\tenum v4l2_priority prio;\n"; 24 printf "\n\t/* ioctl structs */\n"; 25 foreach my $s (sort keys %structs) { 26 next if (!($s =~ m/struct/)); 27 $s =~ s/struct\s*//; 28 29 my $ntabs = ($size_struct - length($s) - 7) / 8; 30 my $tab = ""; 31 for (my $i = 0; $i < $ntabs; $i++) { 32 $tab .= "\t"; 33 } 34 35 36 printf "\tstruct %s%sp_%s;\n", $s, $tab, $s; 37 } 38 printf "};\n"; 39} 40 41sub print_ioc() 42{ 43 printf "#define ioc(type, cmd) { CMD32_##cmd, CMD64_##cmd, cmd, #type, #cmd }\n"; 44 printf "\n/* All defined ioctls */\n"; 45 printf "static const struct {\n"; 46 printf "\tu_int32_t cmd32;\t/* The 32-bit ioctl value, should never change */\n"; 47 printf "\tu_int32_t cmd64;\t/* The 64-bit ioctl value, should never change */\n"; 48 printf "\tu_int32_t cmd;\n"; 49 printf "\tconst char *type;\n"; 50 printf "\tconst char *name;\n"; 51 printf "} ioctls[] = {\n"; 52 printf "\t/* ioctl structs */\n"; 53 foreach my $ioctl (sort keys %ioc) { 54 my $struct = $ioc{$ioctl}; 55 $struct =~ s/.*\://; 56 57 my $type = $ioc{$ioctl}; 58 $type =~ s/\:.*//; 59 60 my $ntabs = ($size_ioc - 8 - length($ioctl) - length($type)) / 8; 61 my $tab = ""; 62 for (my $i = 0; $i < $ntabs; $i++) { 63 $tab .= "\t"; 64 } 65 66 printf "\tioc(%s, %s),%s/* %s */\n", $type, $ioctl, $tab, $struct; 67 } 68 printf "};\n"; 69 printf "#define S_IOCTLS sizeof(ioctls)/sizeof(ioctls[0])\n"; 70} 71 72printf "/* This file is auto-generated by sync-with-kernel.sh */\n"; 73 74foreach my $h (sort keys %headers) { 75 my $line; 76 77 printf "#include \"linux/%s\"\n", $headers{$h}; 78 79 open IN, "../../include/linux/" . $headers{$h}; 80 while (<IN>) { 81 $line .= $_; 82 if (/\\\s*$/) { 83 $line =~ s/\\\s*$//; 84 next; 85 } 86 if ($line =~ m/\#define\s+([A-Z][A-Z0-9_]+)\s+\_IO.+\(.*\,\s*([^\)]+)\)/) { 87 $structs{$2} = 1; 88 $ioc{$1} = "$h: $2"; 89 if ($size_ioc < length($1)) { 90 $size_ioc = length($1); 91 } 92 if ($size_struct < length($2)) { 93 $size_struct = length($2); 94 } 95 } 96 if ($line =~ m/\#define\s+([A-Z][A-Z0-9_]+)\s+\_IO\(.*\)/) { 97 $ioc{$1} = "$h: void"; 98 if ($size_ioc < length($1)) { 99 $size_ioc = length($1); 100 } 101 } 102 $line = ""; 103 } 104 $size_ioc = int((9 + $size_ioc + 7) / 8) * 8; 105 $size_struct = int(($size_struct + 7) / 8) * 8; 106} 107 108my $arg = shift; 109if ($arg eq "--gen_ioctl_numbers") { 110 printf "#include <stdio.h>\n"; 111 printf "int main(int argc, char *argv[]) {\n"; 112 printf "\tprintf(\"/* This file is auto-generated by sync-with-kernel.sh */\\n\\n\");\n"; 113 foreach my $ioctl (sort keys %ioc) { 114 printf "\tprintf(\"#define CMD%%s_%s 0x%%x\\n\", argv[1], %s);\n", $ioctl, $ioctl; 115 } 116 printf "return 0;\n"; 117 printf "}\n"; 118} else { 119 printf "#include \"ioctl_32.h\"\n"; 120 printf "#include \"ioctl_64.h\"\n\n"; 121 122 print_union(); 123 print_ioc(); 124} 125