1 /*
2  * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org>
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.
26  */
27 
28 #include <stdio.h>
29 #include <asm/ioctl.h>
30 
31 typedef unsigned short u16;
32 
33 static const char *
dir2str(const char * name,u16 dir)34 dir2str(const char *name, u16 dir)
35 {
36 	switch (dir) {
37 #define CASE(x) case x: return #x
38 		CASE(_IOC_NONE);
39 		CASE(_IOC_READ);
40 		CASE(_IOC_WRITE);
41 		CASE(_IOC_READ|_IOC_WRITE);
42 	}
43 
44 	static char buf[3 + sizeof(dir) * 2];
45 	fprintf(stderr,
46 		"print_ioctlents: WARNING: invalid dir 0x%02x in %s\n",
47 		dir, name);
48 	snprintf(buf, sizeof(buf), "0x%02x", dir);
49 	return buf;
50 }
51 
52 void
print_ioctlent(const char * info,const char * name,u16 dir,u16 type,u16 nr,u16 size)53 print_ioctlent(const char *info, const char *name,
54 	       u16 dir, u16 type, u16 nr, u16 size)
55 {
56 	unsigned int type_nr =
57 		((unsigned) type << _IOC_TYPESHIFT) |
58 		((unsigned) nr << _IOC_NRSHIFT);
59 
60 	if (dir & ~_IOC_DIRMASK)
61 		fprintf(stderr,
62 			"print_ioctlents: WARNING: dir 0x%02x is out of mask 0x%02x in %s\n",
63 			dir, _IOC_DIRMASK, name);
64 	if (type & ~_IOC_TYPEMASK)
65 		fprintf(stderr,
66 			"print_ioctlents: WARNING: type 0x%02x is out of mask 0x%02x in %s\n",
67 			type, _IOC_TYPEMASK, name);
68 	if (nr & ~_IOC_NRMASK)
69 		fprintf(stderr,
70 			"print_ioctlents: WARNING: nr 0x%02x is out of mask 0x%02x in %s\n",
71 			nr, _IOC_NRMASK, name);
72 	if (size & ~_IOC_SIZEMASK)
73 		fprintf(stderr,
74 			"print_ioctlents: WARNING: size 0x%02x is out of mask 0x%02x in %s\n",
75 			size, _IOC_SIZEMASK, name);
76 
77 	printf("{ \"%s\", \"%s\", %s, 0x%04x, 0x%02x },\n",
78 		info, name, dir2str(name, dir), type_nr, size);
79 }
80