xref: /netbsd-src/share/man/man9/ioctl.9 (revision cac8e449158efc7261bebc8657cbb0125a2cfdde)
1.\" $NetBSD: ioctl.9,v 1.25 2008/05/02 21:46:19 martin Exp $
2.\"
3.\" Copyright (c) 1999  The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Heiko W.Rupp <hwr@pilhuhn.de>
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20.\" TO, THE  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26.\" CONTRACT, STRICT  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27.\" ARISING IN ANY WAY  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28.\" POSSIBILITY OF SUCH DAMAGE.
29.\"
30.Dd December 7, 2001
31.Dt IOCTL 9
32.Os
33.Sh NAME
34.Nm ioctl
35.Nd "how to implement a new ioctl call to access device drivers"
36.Sh SYNOPSIS
37.In sys/ioctl.h
38.In sys/ioccom.h
39.Ft int
40.Fn ioctl "int" "unsigned long" "..."
41.Sh DESCRIPTION
42.Nm
43are internally defined as
44.Bl -tag -width define
45.It #define FOOIOCTL   fun(t,n,pt)
46.El
47.Pp
48where the different variables and functions are:
49.Bl -tag -width FOOIOCTL
50.It Cm FOOIOCTL
51the name which will later be given in the
52.Xr ioctl 2
53system call as second argument, e.g.,
54.Dl ioctl(s, FOOIOCTL, ...) .
55.It Fn fun
56a macro which can be one of
57.Bl -tag -width _IOWR
58.It _IO
59the call is a simple message to the kernel by itself.
60It does not copy anything into the kernel, nor does it want anything back.
61.It _IOR
62the call only reads parameters from the kernel and does not
63pass any to it
64.It _IOW
65the call only writes parameters to the kernel, but does not want anything
66back
67.It _IOWR
68the call writes data to the kernel and wants information back.
69.El
70.It Ar t
71This integer describes to which subsystem the ioctl applies.
72.Ar t
73can be one of
74.Bl -tag -width xxxxx -compact
75.It '1'
76pulse-per-second interface
77.It '4'
78.Xr isdn 4
79.It 'a'
80ISO networking
81.It 'A'
82ac devices (hp300)
83.It 'A'
84Advanced Power Management (hpcmips, i386, sparc), see
85.Xr apm 4
86.It 'A'
87ADB devices (mac68k, macppc)
88.It 'A'
89.Xr audio 4
90.It 'A'
91.Xr isdntel 4
92.It 'b'
93.Xr \&tb 4
94.It 'b'
95Bluetooth HCI sockets, see
96.Xr bluetooth 4
97.It 'b'
98Bluetooth Hub Control, see
99.Xr bthub 4
100.It 'b'
101Bluetooth SCO audio driver, see
102.Xr btsco 4
103.It 'B'
104bell device (x68k)
105.It 'B'
106.Xr bpf 4
107.It 'c'
108coda
109.It 'c'
110.Xr \&cd 4
111.It 'c'
112.Xr \&ch 4
113.It 'C'
114clock devices (amiga, atari, hp300, x68k)
115.It 'C'
116.Xr isdnctl 4
117.It 'd'
118the disk subsystem
119.It 'E'
120.Xr envsys 4
121.It 'f'
122files
123.It 'F'
124Sun-compatible framebuffers
125.It 'F'
126.Xr ccd 4
127and
128.Xr vnd 4
129.It 'g'
130qdss framebuffers
131.It 'G'
132grf devices (amiga, atari, hp300, mac68k, x68k)
133.It 'h'
134HIL devices (hp300)
135.It 'H'
136HIL devices (hp300)
137.It 'H'
138HPc framebuffers
139.It 'i'
140a (pseudo) interface
141.It 'I'
142.Xr ite 4
143(mac68k)
144.It 'J'
145ISA joystick interface
146.It 'k'
147Sun-compatible (and other) keyboards
148.It 'K'
149.Xr lkm 4
150.It 'l'
151leo devices (atari)
152.It 'm'
153.Xr mtio 4
154.It 'M'
155mouse devices (atari)
156.It 'M'
157.Xr mlx 4
158.It 'n'
159virtual console device (arm32)
160.It 'n'
161SMB networking
162.It 'O'
163OpenPROM and OpenFirmware
164.It 'p'
165power control (x68k)
166.It 'P'
167parallel port (amiga, x68k)
168.It 'P'
169profiling (arm32)
170.It 'P'
171printer/plotter interface (hp300)
172.It 'P'
173.Xr magma 4
174bpp (sparc)
175.It 'q'
176.Xr altq 9
177.It 'q'
178pmax graphics devices
179.It 'Q'
180.Xr altq 9
181.It 'Q'
182raw SCSI commands
183.It 'r'
184the routing subsystem
185.It 'r'
186.Xr \&md 4
187.It 'R'
188.Xr isdnbchan 4
189.It 'R'
190.Xr rnd 4
191.It 's'
192the socket layer
193.It 's'
194satlink devices
195.It 'S'
196SCSI disks (arc, hp300, pmax)
197.It 'S'
198watchdog devices (sh3)
199.It 'S'
200ISA speaker devices
201.It 'S'
202stic devices
203.It 'S'
204scanners
205.It 't'
206the tty layer
207.It 'u'
208user defined ???
209.It 'U'
210scsibus (see
211.Xr scsi 4 )
212.It 'v'
213Sun-compatible
214.Dq firm events
215.It 'V'
216view device (amiga, atari)
217.It 'V'
218sram device (x68k)
219.It 'w'
220watchdog devices
221.It 'W'
222wt devices
223.It 'W'
224wscons devices
225.It 'x'
226bt8xx devices
227.It 'Z'
228ite devices (amiga, atari, x68k)
229.It 'Z'
230passthrough ioctls
231.El
232.It Ar n
233This numbers the ioctl within the group.
234There may be only one
235.Ar n
236for a given
237.Ar t .
238This is a unsigned 8 bit number.
239.It Ar pt
240This specifies the type of the passed parameter.
241This one gets internally transformed to the size of the parameter, so
242for example, if you want to pass a structure, then you have to specify that
243structure and not a pointer to it or sizeof(struct foo)
244.El
245.Pp
246In order for the new ioctl to be known to the system it is installed
247in either \*[Lt]sys/ioctl.h\*[Gt] or one of the files that are reached from
248\*[Lt]sys/ioctl.h\*[Gt].
249.Sh EXAMPLES
250.Bd -literal -offset indent
251#define	FOOIOCTL	_IOWR('i', 23, int)
252
253int a = 3;
254error = ioctl(s, FOOICTL, \*[Am]a);
255.Ed
256.Pp
257Within the ioctl()-routine of the driver, it can be then accessed like
258.Bd -literal -offset indent
259driver_ioctl(..., u_long cmd, void *data)
260{
261	...
262	switch (cmd) {
263
264	case FOOIOCTL:
265		int *a = (int *)data;
266		printf(" Value passed: %d\en", *a);
267		break;
268	}
269}
270.Ed
271.Sh NOTES
272Note that if you for example try to read information from an ethernet
273driver where the name of the card is included in the third argument
274(e.g., ioctl(s, READFROMETH, struct ifreq *)), then you have to use
275the _IOWR() form not the _IOR(), as passing the name of the card to the
276kernel already consists of writing data.
277.Sh RETURN VALUES
278All ioctl() routines should return either 0 or a defined error code.
279The use of magic numbers such as -1, to indicate that a given ioctl
280code was not handled is strongly discouraged.
281The value -1 coincides with the historic value for
282.Cm ERESTART
283which was shown to produce user space code that never returned from
284a call to
285.Xr ioctl 2 .
286.Pp
287For ioctl codes that
288are not handled by a given routine, the pseudo error value
289.Cm EPASSTHROUGH
290is provided.
291.Cm EPASSTHROUGH
292indicates that no error occurred during processing (it did not fail),
293but neither was anything processed (it did not succeed).
294This supersedes the use of either
295.Cm ENOTTY
296(which is an explicit failure) or -1 (which has no contextual meaning)
297as a return value.
298.Cm ENOTTY
299will get passed directly back to user space and bypass any further
300processing by other ioctl layers.
301Only code that wishes to suppress possible further processing of an
302ioctl code (e.g., the tty line discipline code) should return
303.Cm ENOTTY .
304All other code should return
305.Cm EPASSTHROUGH ,
306even if it knows that no other layers will be called upon.
307.Pp
308If the value
309.Cm EPASSTHROUGH
310is returned to
311.Fn sys_ioctl ,
312then it will there be changed to
313.Cm ENOTTY
314to be returned to user space, thereby providing the proper error
315notification to the application.
316.Sh SEE ALSO
317.Xr ioctl 2
318