blob: b63809386c97c17645523267f2092bd4ade0a614 [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
Wichert Akkermanc6c1ea52001-08-03 21:52:13 +00005 * Copyright (c) 1996-2001 Wichert Akkerman <wichert@cistron.nl>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00006 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000029 */
30
31#include "defs.h"
Denys Vlasenko1c2e9122012-03-20 10:57:41 +010032#include <limits.h>
Wichert Akkermana9667852001-03-17 17:26:34 +000033#include <asm/ioctl.h>
Wichert Akkermana9667852001-03-17 17:26:34 +000034
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000035static int
Denys Vlasenko12014262011-05-30 14:00:14 +020036compare(const void *a, const void *b)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000037{
Denys Vlasenko1c2e9122012-03-20 10:57:41 +010038 unsigned long code1 = (long) a;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000039 unsigned long code2 = ((struct ioctlent *) b)->code;
Denys Vlasenko1c2e9122012-03-20 10:57:41 +010040
41 /* Simply returning (code1 - code2) may be wrong.
42 * Exmaple: 0xffffffff - 0 = 0xffffffff = -1.
43 * Need to shift both values down until they both fit
44 * in positive int.
45 */
46 while ((code1|code2) > INT_MAX) {
47#if INT_MAX == LONG_MAX
48 /* this case is easy */
49 code1 >>= 1;
50 code2 >>= 1;
51 break;
52#else
53 /* Remove INT_MAX worth of bits, then check again */
54 code1 >>= sizeof(int) * 8 - 1;
55 code2 >>= sizeof(int) * 8 - 1;
56#endif
57 }
58 return (int)code1 - (int)code2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000059}
60
Roland McGrathee36ce12004-09-04 03:53:10 +000061const struct ioctlent *
Denys Vlasenko12014262011-05-30 14:00:14 +020062ioctl_lookup(long code)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000063{
Denys Vlasenko1c2e9122012-03-20 10:57:41 +010064 struct ioctlent *iop;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000065
Denys Vlasenko1c2e9122012-03-20 10:57:41 +010066 code &= (_IOC_NRMASK<<_IOC_NRSHIFT) | (_IOC_TYPEMASK<<_IOC_TYPESHIFT);
67 iop = bsearch((void*)code, ioctlent,
68 nioctlents, sizeof(ioctlent[0]), compare);
69 while (iop > ioctlent) {
70 iop--;
71 if (iop->code != code) {
Roland McGrath2843a4e2003-11-14 02:54:03 +000072 iop++;
73 break;
74 }
Denys Vlasenko1c2e9122012-03-20 10:57:41 +010075 }
Roland McGrath2843a4e2003-11-14 02:54:03 +000076 return iop;
77}
78
Roland McGrathee36ce12004-09-04 03:53:10 +000079const struct ioctlent *
Denys Vlasenko6b6ed5d2011-08-20 02:27:18 +020080ioctl_next_match(const struct ioctlent *iop)
Roland McGrath2843a4e2003-11-14 02:54:03 +000081{
82 long code;
83
Denys Vlasenko1c2e9122012-03-20 10:57:41 +010084 code = iop->code;
85 iop++;
Roland McGrath2843a4e2003-11-14 02:54:03 +000086 if (iop < ioctlent + nioctlents && iop->code == code)
87 return iop;
88 return NULL;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000089}
90
91int
Denys Vlasenko12014262011-05-30 14:00:14 +020092ioctl_decode(struct tcb *tcp, long code, long arg)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000093{
94 switch ((code >> 8) & 0xff) {
Roland McGrathbb3af522003-01-14 07:53:34 +000095#if defined(ALPHA) || defined(POWERPC)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000096 case 'f': case 't': case 'T':
97#else /* !ALPHA */
98 case 0x54:
99#endif /* !ALPHA */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000100 return term_ioctl(tcp, code, arg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000101 case 0x89:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000102 return sock_ioctl(tcp, code, arg);
Roland McGrathd83c50b2004-10-06 22:27:43 +0000103 case 'p':
104 return rtc_ioctl(tcp, code, arg);
Dmitry V. Levin4ef6db42011-01-15 20:15:31 +0000105 case 0x03:
106 case 0x12:
107 return block_ioctl(tcp, code, arg);
Dmitry V. Levinb011af52007-06-30 11:37:09 +0000108 case 0x22:
109 return scsi_ioctl(tcp, code, arg);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000110 default:
111 break;
112 }
113 return 0;
114}
115
116/*
117 * Registry of ioctl characters, culled from
118 * @(#)ioccom.h 1.7 89/06/16 SMI; from UCB ioctl.h 7.1 6/4/86
119 *
120 * char file where defined notes
121 * ---- ------------------ -----
122 * F sun/fbio.h
123 * G sun/gpio.h
124 * H vaxif/if_hy.h
125 * M sundev/mcpcmd.h *overlap*
126 * M sys/modem.h *overlap*
127 * S sys/stropts.h
128 * T sys/termio.h -no overlap-
129 * T sys/termios.h -no overlap-
130 * V sundev/mdreg.h
131 * a vaxuba/adreg.h
132 * d sun/dkio.h -no overlap with sys/des.h-
133 * d sys/des.h (possible overlap)
134 * d vax/dkio.h (possible overlap)
135 * d vaxuba/rxreg.h (possible overlap)
136 * f sys/filio.h
137 * g sunwindow/win_ioctl.h -no overlap-
138 * g sunwindowdev/winioctl.c !no manifest constant! -no overlap-
139 * h sundev/hrc_common.h
140 * i sys/sockio.h *overlap*
141 * i vaxuba/ikreg.h *overlap*
142 * k sundev/kbio.h
143 * m sundev/msio.h (possible overlap)
144 * m sundev/msreg.h (possible overlap)
145 * m sys/mtio.h (possible overlap)
146 * n sun/ndio.h
147 * p net/nit_buf.h (possible overlap)
148 * p net/nit_if.h (possible overlap)
149 * p net/nit_pf.h (possible overlap)
150 * p sundev/fpareg.h (possible overlap)
151 * p sys/sockio.h (possible overlap)
152 * p vaxuba/psreg.h (possible overlap)
153 * q sun/sqz.h
154 * r sys/sockio.h
155 * s sys/sockio.h
156 * t sys/ttold.h (possible overlap)
157 * t sys/ttycom.h (possible overlap)
158 * v sundev/vuid_event.h *overlap*
159 * v sys/vcmd.h *overlap*
160 *
161 * End of Registry
162 */