xref: /freebsd/share/man/man9/uio.9 (revision df7f5d4de4592a8948a25ce01e5bddfbb7ce39dc)
1.\"
2.\" Copyright (c) 1997 Joerg Wunsch
3.\"
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.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
16.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
19.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25.\"
26.\" $Id$
27.\" "
28.Dd February 2, 1997
29.Os
30.Dt UIO 9
31.Sh NAME
32.Nm uio ,
33.Nm uiomove
34.Nd device driver IO routines
35.Sh SYNOPSIS
36.Fd #include <sys/types.h>
37.Fd #include <sys/cdefs.h>
38.Fd #include <sys/uio.h>
39.Pp
40.Bd -literal
41struct uio {
42	struct	iovec *uio_iov;
43	int	uio_iovcnt;
44	off_t	uio_offset;
45	int	uio_resid;
46	enum	uio_seg uio_segflg;
47	enum	uio_rw uio_rw;
48	struct	proc *uio_procp;
49};
50.Ed
51.Ft int
52.Fn uiomove "caddr_t buf" "int howmuch" "struct uio *uiop"
53.Sh DESCRIPTION
54The function
55.Fn uiomove
56is used to handle transfer of data between buffers and IO vectors
57that might possibly also cross the user/kernel space boundary.
58.Pp
59As a result of any
60.Xr read 2 ,
61.Xr write 2 ,
62.Xr readv 2 ,
63or
64.Xr writev 2
65system call that is being passed to a character-device driver, the
66appropriate driver
67.Em read
68or
69.Em write
70entry will be called with a pointer to a
71.Fa "struct uio"
72being passed.  The transfer request is encoded in this structure.
73The driver itself should use
74.Fn uiomove
75to get at the data in this structure.
76.Pp
77The fields in the uio structure are:
78.Bl -tag -width "uio_iovcntXXXX" -compact
79.It Dv uio_iov
80The array of IO vectors to be processed.  In the case of scatter/gather
81IO, this will be more than one vector.
82.It Dv uio_iovcnt
83The number of IO vectors present.
84.It Dv uio_offset
85The offset into the device.
86.It Dv uio_resid
87The number of bytes to process.
88.It Dv uio_segflg
89One of the following flags:
90.Bl -tag -width "UIO_USERISPACEX" -compact
91.It Dv UIO_USERSPACE
92The IO vector points into a process's address space.
93.It Dv UIO_SYSSPACE
94The IO vector points into the kernel address space.
95.It Dv UIO_USERISPACE
96The IO vector points into the instruction area of a process's address
97space.
98.It Dv UIO_NOCOPY
99Don't copy, already in object.
100.El
101.It Dv uio_rw uio_rw
102The direction of the desired transfer, either
103.Dv UIO_READ ,
104or
105.Dv UIO_WRITE .
106.It Dv uio_procp
107The pointer to a
108.Li struct proc
109for the associated process; used if
110.Dv uio_segflg
111indicates that the transfer is to be made from/to a process's address
112space.
113.El
114.Sh EXAMPLES
115The idea is that the driver maintains a private buffer for its data,
116and processes the request in chunks of maximal the size of this
117buffer.  Note that the buffer handling below is very simplified and
118won't work (the buffer pointer is not being advanced in case of a
119partial read), it's just here to demonstrate the uio handling.
120.Bd -literal
121/* MIN() can be found there: */
122#include <sys/param.h>
123
124#define BUFSIZE 512
125static char buffer[BUFSIZE];
126
127static int data_available;	/* amount of data that can be read */
128
129static int
130fooread(dev_t dev, struct uio *uio, int flag)
131{
132	int rv, amnt;
133
134	while (uio->uio_resid > 0) {
135		if (data_available > 0) {
136			amnt = MIN(uio->uio_resid, data_available);
137			if ((rv = uiomove((caddr_t)buffer, amnt, uio))
138			    != 0)
139				goto error;
140			data_available -= amnt;
141		} else {
142			tsleep(...);	/* wait for a better time */
143		}
144	}
145	return 0;
146error:
147	/* do error cleanup here */
148	return rv;
149}
150
151.Ed
152.Sh RETURN VALUES
153.Fn uiomove
154can return
155.Er EFAULT
156from the invoked
157.Xr copyin 9
158or
159.Xr copyout 9
160in case the transfer was to/from a process's address space.
161.Sh SEE ALSO
162.Xr read 2 ,
163.Xr readv 2 ,
164.Xr write 2 ,
165.Xr writev 2 ,
166.Xr copyin 9 ,
167.Xr copyout 9 ,
168.Xr sleep 9
169.Sh HISTORY
170The uio mechanism appeared in some early version of
171.Ux .
172.Sh AUTHORS
173This man page has been written by
174.ie t J\(:org Wunsch.
175.el Joerg Wunsch.
176