blob: 5e79510472c9bfcc9d70f41134fd67751f69e899 [file] [log] [blame]
Wichert Akkermana9667852001-03-17 17:26:34 +00001#! /bin/sh
Wichert Akkermanb2c12f32001-08-03 21:53:26 +00002# Copyright (c) 2001 Wichert Akkerman <wichert@cistron.nl>
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11# notice, this list of conditions and the following disclaimer in the
12# documentation and/or other materials provided with the distribution.
13# 3. The name of the author may not be used to endorse or promote products
14# derived from this software without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000026
Roland McGrath1e1b61d2003-03-31 01:03:34 +000027# Validate arg count.
Roland McGrathd2656692004-07-08 19:01:28 +000028case $# in
291)
30 dir="$1"
31 asm=asm
32 ;;
332)
34 dir="$1"
35 asm="$2"
36 ;;
37*)
38 echo "usage: $0 include-directory [asm-subdirectory]" >&2
Roland McGrath1e1b61d2003-03-31 01:03:34 +000039 exit 1
Roland McGrathd2656692004-07-08 19:01:28 +000040 ;;
41esac
Wichert Akkerman7b3346b2001-10-09 23:47:38 +000042
Roland McGrath718f9a62004-06-04 02:03:06 +000043lookup_ioctls()
44{
45 type="$1"
46 shift
47
48 # Build the list of all ioctls
49 regexp='^[[:space:]]*#[[:space:]]*define[[:space:]]\+[A-Z][A-Z0-9_]*[[:space:]]\+0x'"$type"'..\>'
Dmitry V. Levin1cd3f5f2013-06-03 16:24:53 +000050 (cd "$dir" && for f; do grep "$regexp" "$f" "uapi/$f" 2>/dev/null; done) |
Roland McGrath61e0beb2004-07-12 06:11:16 +000051 sed -ne "s,$asm/,asm/,g"'
52s/^\(.*\):[[:space:]]*#[[:space:]]*define[[:space:]]*\([A-Z0-9_]*\)[[:space:]]*\(0x'"$type"'..\).*/ { "\1", "\2", \3 },/p' \
Roland McGrath718f9a62004-06-04 02:03:06 +000053 >> ioctls.h
54}
55
Denys Vlasenkoe6d113f2009-02-23 13:22:06 +000056> ioctls.h
57
Dmitry V. Levinebc2f282011-01-14 15:30:10 +000058lookup_ioctls 03 linux/hdreg.h
Roland McGrath7166bb02004-10-06 22:31:38 +000059lookup_ioctls 22 scsi/sg.h
Roland McGrath718f9a62004-06-04 02:03:06 +000060lookup_ioctls 46 linux/fb.h
61lookup_ioctls 4B linux/kd.h
Dmitry V. Levine51a87c2011-02-20 12:25:12 +000062lookup_ioctls 4C linux/loop.h
Roland McGrath7166bb02004-10-06 22:31:38 +000063lookup_ioctls 53 linux/cdrom.h scsi/scsi.h scsi/scsi_ioctl.h
Dmitry V. Levinae4db5e2010-04-07 11:22:28 +000064lookup_ioctls 54 $asm/ioctls.h asm-generic/ioctls.h
Roland McGrath718f9a62004-06-04 02:03:06 +000065lookup_ioctls 56 linux/vt.h
66lookup_ioctls '7[12]' linux/videotext.h
Dmitry V. Levinae4db5e2010-04-07 11:22:28 +000067lookup_ioctls 89 $asm/sockios.h asm-generic/sockios.h linux/sockios.h
Roland McGrath718f9a62004-06-04 02:03:06 +000068lookup_ioctls 8B linux/wireless.h
Wichert Akkerman7b3346b2001-10-09 23:47:38 +000069
Denys Vlasenkoe6d113f2009-02-23 13:22:06 +000070if [ -e $dir/Kbuild ]; then
71 # kernel has exported user space headers, so query only them
72 files=$(
73 cd $dir || exit
74 find . -mindepth 2 -name Kbuild | \
75 sed -e 's:^\./::' -e 's:/Kbuild:/*:' | \
76 grep -v '^asm-'
77 echo "$asm/* asm-generic/*"
78 )
Mike Frysinger1b30f4b2010-09-11 15:04:12 -040079 # special case: some headers aren't exported directly
80 files="${files} media/* net/bluetooth/* pcmcia/*"
Denys Vlasenkoe6d113f2009-02-23 13:22:06 +000081else
Dmitry V. Levin118c6032014-11-03 18:38:51 +000082 # older kernel tree or headers_install'ed tree, just assume some headers
83 files="linux/* $asm/* asm-generic/* drm/* mtd/* rdma/* scsi/* sound/* video/* xen/*"
Denys Vlasenkoe6d113f2009-02-23 13:22:06 +000084fi
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000085
Wichert Akkermana9667852001-03-17 17:26:34 +000086# Build the list of all ioctls
Denys Vlasenkoe6d113f2009-02-23 13:22:06 +000087# Example output:
88# { "asm/ioctls.h", "TIOCSWINSZ", 0x5414 },
89# { "asm/mce.h", "MCE_GETCLEAR_FLAGS", _IOC(_IOC_NONE,'M',3,0) },
Wichert Akkermanb50c9072001-08-03 21:49:34 +000090regexp='^[[:space:]]*#[[:space:]]*define[[:space:]]\+[A-Z][A-Z0-9_]*[[:space:]]\+_S\?\(IO\|IOW\|IOR\|IOWR\)\>'
Denys Vlasenkoe6d113f2009-02-23 13:22:06 +000091(cd $dir && grep $regexp $files 2>/dev/null) | \
92 sed -n \
93 -e "s,$asm/,asm/,g" \
94 -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 +000095 >> ioctls.h
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000096
Dmitry V. Levin1cd3f5f2013-06-03 16:24:53 +000097# Strip uapi/ prefix
98sed -i 's|"uapi/|"|' ioctls.h
99
100# Sort and drop dups
101sort -u -o ioctls.h ioctls.h
Denys Vlasenkoe6d113f2009-02-23 13:22:06 +0000102
Denys Vlasenkoe6d113f2009-02-23 13:22:06 +0000103> ioctldefs.h
104
105# Collect potential ioctl names. ('bases' is a bad name. Sigh)
Wichert Akkermana9667852001-03-17 17:26:34 +0000106# Some use a special base to offset their ioctls on. Extract that as well.
Denys Vlasenko3566e3d2009-02-20 17:32:34 +0000107# Some use 2 defines: _IOC(_IOC_NONE,DM_IOCTL,DM_LIST_DEVICES_CMD,....)
Denys Vlasenko3566e3d2009-02-20 17:32:34 +0000108bases=$(sed -n \
109 -e 's/.*_IOC_NONE.*,[[:space:]]*\([A-Z][A-Z0-9_]\+\)[[:space:]]*,[[:space:]]*\([A-Z][A-Z0-9_]\+\)[[:space:]+,].*/\1\n\2/p' \
110 -e 's/.*_IOC_NONE.*,[[:space:]]*\([A-Z][A-Z0-9_]\+\)[[:space:]+,].*/\1/p' \
Denys Vlasenko8a4bdf82009-02-22 03:01:20 +0000111 ioctls.h | sort -u)
Denys Vlasenkoe6d113f2009-02-23 13:22:06 +0000112
113for base in $bases; do
Wichert Akkermana9667852001-03-17 17:26:34 +0000114 echo "Looking for $base"
115 regexp="^[[:space:]]*#[[:space:]]*define[[:space:]]\+$base"
Denys Vlasenkoe6d113f2009-02-23 13:22:06 +0000116 line=$( (cd $dir && grep -h $regexp 2>/dev/null $files) | grep -v '\<_IO')
117 if [ x"$line" != x ]; then
118 echo "$base is a #define" # "($line)"
119 echo "$line" >> ioctldefs.h
120 fi
121
122 if ! grep "\<$base\>" ioctldefs.h >/dev/null 2>/dev/null; then
123 # Not all ioctl's are defines ... some (like the DM_* stuff)
124 # are enums, so we have to extract that crap ourself
125 (
126 cd $dir || exit
127 # -P: inhibit generation of linemarkers
128 ${CPP:-cpp} -P $(grep -l $base $files 2>/dev/null) | sed '/^$/d' | \
129 awk -v base="$base" '{
130 if ($1 == "enum") {
131 val = 0
132 while ($NF != "};") {
133 if (!getline)
134 exit
135 gsub(/,/, "")
136 if ($0 ~ /=/)
137 val = $NF
138 if ($1 == base) {
139 print "#define " base " (" val ")"
140 exit
141 }
142 val++
143 }
144 }
145 }'
146 ) >> ioctldefs.h
147 if ! grep "\<$base\>" ioctldefs.h >/dev/null 2>/dev/null; then
148 echo "Can't find the definition for $base"
149 else
150 echo "$base is an enum"
151 fi
152 fi
Wichert Akkermana9667852001-03-17 17:26:34 +0000153done
Denys Vlasenkoe6d113f2009-02-23 13:22:06 +0000154
155# Sort and drop dups?
156# sort -u <ioctldefs.h >ioctldefs1.h && mv ioctldefs1.h ioctldefs.h