blob: a0e71e743b22109effa3ad736d7e7e84625cf1bb [file] [log] [blame]
Wichert Akkermana9667852001-03-17 17:26:34 +00001#! /bin/sh
Wichert Akkermanb2c12f32001-08-03 21:53:26 +00002#
3# Copyright (c) 2001 Wichert Akkerman <wichert@cistron.nl>
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12# notice, this list of conditions and the following disclaimer in the
13# documentation and/or other materials provided with the distribution.
14# 3. The name of the author may not be used to endorse or promote products
15# derived from this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27#
28# $Id$
29#
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000030
Roland McGrath1e1b61d2003-03-31 01:03:34 +000031# Validate arg count.
Roland McGrathd2656692004-07-08 19:01:28 +000032case $# in
331)
34 dir="$1"
35 asm=asm
36 ;;
372)
38 dir="$1"
39 asm="$2"
40 ;;
41*)
42 echo "usage: $0 include-directory [asm-subdirectory]" >&2
Roland McGrath1e1b61d2003-03-31 01:03:34 +000043 exit 1
Roland McGrathd2656692004-07-08 19:01:28 +000044 ;;
45esac
Wichert Akkerman7b3346b2001-10-09 23:47:38 +000046
Roland McGrath718f9a62004-06-04 02:03:06 +000047lookup_ioctls()
48{
49 type="$1"
50 shift
51
52 # Build the list of all ioctls
53 regexp='^[[:space:]]*#[[:space:]]*define[[:space:]]\+[A-Z][A-Z0-9_]*[[:space:]]\+0x'"$type"'..\>'
Denys Vlasenkoe6d113f2009-02-23 13:22:06 +000054 (cd "$dir" && grep "$regexp" "$@" /dev/null 2>/dev/null) |
Roland McGrath61e0beb2004-07-12 06:11:16 +000055 sed -ne "s,$asm/,asm/,g"'
56s/^\(.*\):[[:space:]]*#[[:space:]]*define[[:space:]]*\([A-Z0-9_]*\)[[:space:]]*\(0x'"$type"'..\).*/ { "\1", "\2", \3 },/p' \
Roland McGrath718f9a62004-06-04 02:03:06 +000057 >> ioctls.h
58}
59
Denys Vlasenkoe6d113f2009-02-23 13:22:06 +000060
61> ioctls.h
62
Dmitry V. Levinebc2f282011-01-14 15:30:10 +000063lookup_ioctls 03 linux/hdreg.h
Roland McGrath7166bb02004-10-06 22:31:38 +000064lookup_ioctls 22 scsi/sg.h
Roland McGrath718f9a62004-06-04 02:03:06 +000065lookup_ioctls 46 linux/fb.h
66lookup_ioctls 4B linux/kd.h
Roland McGrath7166bb02004-10-06 22:31:38 +000067lookup_ioctls 53 linux/cdrom.h scsi/scsi.h scsi/scsi_ioctl.h
Dmitry V. Levinae4db5e2010-04-07 11:22:28 +000068lookup_ioctls 54 $asm/ioctls.h asm-generic/ioctls.h
Roland McGrath718f9a62004-06-04 02:03:06 +000069lookup_ioctls 56 linux/vt.h
70lookup_ioctls '7[12]' linux/videotext.h
Dmitry V. Levinae4db5e2010-04-07 11:22:28 +000071lookup_ioctls 89 $asm/sockios.h asm-generic/sockios.h linux/sockios.h
Roland McGrath718f9a62004-06-04 02:03:06 +000072lookup_ioctls 8B linux/wireless.h
Wichert Akkerman7b3346b2001-10-09 23:47:38 +000073
Denys Vlasenkoe6d113f2009-02-23 13:22:06 +000074if [ -e $dir/Kbuild ]; then
75 # kernel has exported user space headers, so query only them
76 files=$(
77 cd $dir || exit
78 find . -mindepth 2 -name Kbuild | \
79 sed -e 's:^\./::' -e 's:/Kbuild:/*:' | \
80 grep -v '^asm-'
81 echo "$asm/* asm-generic/*"
82 )
Mike Frysinger1b30f4b2010-09-11 15:04:12 -040083 # special case: some headers aren't exported directly
84 files="${files} media/* net/bluetooth/* pcmcia/*"
Denys Vlasenkoe6d113f2009-02-23 13:22:06 +000085else
86 # older kernel so just assume some headers
Dmitry V. Levinae4db5e2010-04-07 11:22:28 +000087 files="linux/* $asm/* asm-generic/* scsi/* sound/*"
Denys Vlasenkoe6d113f2009-02-23 13:22:06 +000088fi
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000089
Wichert Akkermana9667852001-03-17 17:26:34 +000090# Build the list of all ioctls
Denys Vlasenkoe6d113f2009-02-23 13:22:06 +000091# Example output:
92# { "asm/ioctls.h", "TIOCSWINSZ", 0x5414 },
93# { "asm/mce.h", "MCE_GETCLEAR_FLAGS", _IOC(_IOC_NONE,'M',3,0) },
Wichert Akkermanb50c9072001-08-03 21:49:34 +000094regexp='^[[:space:]]*#[[:space:]]*define[[:space:]]\+[A-Z][A-Z0-9_]*[[:space:]]\+_S\?\(IO\|IOW\|IOR\|IOWR\)\>'
Denys Vlasenkoe6d113f2009-02-23 13:22:06 +000095(cd $dir && grep $regexp $files 2>/dev/null) | \
96 sed -n \
97 -e "s,$asm/,asm/,g" \
98 -e 's/^\(.*\):[[:space:]]*#[[:space:]]*define[[:space:]]*\([A-Z0-9_]*\)[[:space:]]*_S\?I.*(\([^[,]*\)[[:space:]]*,[[:space:]]*\([^,)]*\).*/ { "\1", "\2", _IOC(_IOC_NONE,\3,\4,0) },/p' \
Wichert Akkerman7b3346b2001-10-09 23:47:38 +000099 >> ioctls.h
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000100
Denys Vlasenkoe6d113f2009-02-23 13:22:06 +0000101# Sort and drop dups?
102# sort -u <ioctls.h >ioctls1.h && mv ioctls1.h ioctls.h
103
104
105> ioctldefs.h
106
107# Collect potential ioctl names. ('bases' is a bad name. Sigh)
Wichert Akkermana9667852001-03-17 17:26:34 +0000108# Some use a special base to offset their ioctls on. Extract that as well.
Denys Vlasenko3566e3d2009-02-20 17:32:34 +0000109# Some use 2 defines: _IOC(_IOC_NONE,DM_IOCTL,DM_LIST_DEVICES_CMD,....)
Denys Vlasenko3566e3d2009-02-20 17:32:34 +0000110bases=$(sed -n \
111 -e 's/.*_IOC_NONE.*,[[:space:]]*\([A-Z][A-Z0-9_]\+\)[[:space:]]*,[[:space:]]*\([A-Z][A-Z0-9_]\+\)[[:space:]+,].*/\1\n\2/p' \
112 -e 's/.*_IOC_NONE.*,[[:space:]]*\([A-Z][A-Z0-9_]\+\)[[:space:]+,].*/\1/p' \
Denys Vlasenko8a4bdf82009-02-22 03:01:20 +0000113 ioctls.h | sort -u)
Denys Vlasenkoe6d113f2009-02-23 13:22:06 +0000114
115for base in $bases; do
Wichert Akkermana9667852001-03-17 17:26:34 +0000116 echo "Looking for $base"
117 regexp="^[[:space:]]*#[[:space:]]*define[[:space:]]\+$base"
Denys Vlasenkoe6d113f2009-02-23 13:22:06 +0000118 line=$( (cd $dir && grep -h $regexp 2>/dev/null $files) | grep -v '\<_IO')
119 if [ x"$line" != x ]; then
120 echo "$base is a #define" # "($line)"
121 echo "$line" >> ioctldefs.h
122 fi
123
124 if ! grep "\<$base\>" ioctldefs.h >/dev/null 2>/dev/null; then
125 # Not all ioctl's are defines ... some (like the DM_* stuff)
126 # are enums, so we have to extract that crap ourself
127 (
128 cd $dir || exit
129 # -P: inhibit generation of linemarkers
130 ${CPP:-cpp} -P $(grep -l $base $files 2>/dev/null) | sed '/^$/d' | \
131 awk -v base="$base" '{
132 if ($1 == "enum") {
133 val = 0
134 while ($NF != "};") {
135 if (!getline)
136 exit
137 gsub(/,/, "")
138 if ($0 ~ /=/)
139 val = $NF
140 if ($1 == base) {
141 print "#define " base " (" val ")"
142 exit
143 }
144 val++
145 }
146 }
147 }'
148 ) >> ioctldefs.h
149 if ! grep "\<$base\>" ioctldefs.h >/dev/null 2>/dev/null; then
150 echo "Can't find the definition for $base"
151 else
152 echo "$base is an enum"
153 fi
154 fi
Wichert Akkermana9667852001-03-17 17:26:34 +0000155done
Denys Vlasenkoe6d113f2009-02-23 13:22:06 +0000156
157# Sort and drop dups?
158# sort -u <ioctldefs.h >ioctldefs1.h && mv ioctldefs1.h ioctldefs.h