video.c revision 1.22 1 1.22 christos /* $NetBSD: video.c,v 1.22 2009/08/18 02:17:09 christos Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*
4 1.1 jmcneill * Copyright (c) 2008 Patrick Mahoney <pat (at) polycrystal.org>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * This code was written by Patrick Mahoney (pat (at) polycrystal.org) as
8 1.1 jmcneill * part of Google Summer of Code 2008.
9 1.1 jmcneill *
10 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
11 1.1 jmcneill * modification, are permitted provided that the following conditions
12 1.1 jmcneill * are met:
13 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
14 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
15 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
17 1.1 jmcneill * documentation and/or other materials provided with the distribution.
18 1.1 jmcneill *
19 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE.
30 1.1 jmcneill */
31 1.1 jmcneill
32 1.1 jmcneill /*
33 1.1 jmcneill * This ia a Video4Linux 2 compatible /dev/video driver for NetBSD
34 1.1 jmcneill *
35 1.1 jmcneill * See http://v4l2spec.bytesex.org/ for Video4Linux 2 specifications
36 1.1 jmcneill */
37 1.1 jmcneill
38 1.1 jmcneill #include <sys/cdefs.h>
39 1.22 christos __KERNEL_RCSID(0, "$NetBSD: video.c,v 1.22 2009/08/18 02:17:09 christos Exp $");
40 1.1 jmcneill
41 1.1 jmcneill #include "video.h"
42 1.1 jmcneill #if NVIDEO > 0
43 1.1 jmcneill
44 1.1 jmcneill #include <sys/param.h>
45 1.1 jmcneill #include <sys/ioctl.h>
46 1.1 jmcneill #include <sys/fcntl.h>
47 1.1 jmcneill #include <sys/vnode.h>
48 1.1 jmcneill #include <sys/poll.h>
49 1.7 jmcneill #include <sys/select.h>
50 1.1 jmcneill #include <sys/kmem.h>
51 1.1 jmcneill #include <sys/pool.h>
52 1.1 jmcneill #include <sys/conf.h>
53 1.1 jmcneill #include <sys/types.h>
54 1.1 jmcneill #include <sys/device.h>
55 1.1 jmcneill #include <sys/condvar.h>
56 1.1 jmcneill #include <sys/queue.h>
57 1.1 jmcneill #include <sys/videoio.h>
58 1.1 jmcneill
59 1.1 jmcneill #include <dev/video_if.h>
60 1.1 jmcneill
61 1.1 jmcneill /* #define VIDEO_DEBUG 1 */
62 1.1 jmcneill
63 1.1 jmcneill #ifdef VIDEO_DEBUG
64 1.1 jmcneill #define DPRINTF(x) do { if (videodebug) printf x; } while (0)
65 1.1 jmcneill #define DPRINTFN(n,x) do { if (videodebug>(n)) printf x; } while (0)
66 1.1 jmcneill int videodebug = VIDEO_DEBUG;
67 1.1 jmcneill #else
68 1.1 jmcneill #define DPRINTF(x)
69 1.1 jmcneill #define DPRINTFN(n,x)
70 1.1 jmcneill #endif
71 1.1 jmcneill
72 1.22 christos #define PAGE_ALIGN(a) (((a) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))
73 1.22 christos
74 1.1 jmcneill #define VIDEO_DRIVER_VERSION 1
75 1.1 jmcneill
76 1.1 jmcneill /* TODO: move to sys/intr.h */
77 1.1 jmcneill #define IPL_VIDEO IPL_VM
78 1.1 jmcneill #define splvideo() splvm()
79 1.1 jmcneill
80 1.1 jmcneill #define VIDEO_MIN_BUFS 2
81 1.6 jmcneill #define VIDEO_MAX_BUFS 32
82 1.1 jmcneill #define VIDEO_NUM_BUFS 4
83 1.1 jmcneill
84 1.1 jmcneill /* Scatter Buffer - an array of fixed size (PAGE_SIZE) chunks
85 1.1 jmcneill * allocated non-contiguously and functions to get data into and out
86 1.1 jmcneill * of the scatter buffer. */
87 1.1 jmcneill struct scatter_buf {
88 1.1 jmcneill pool_cache_t sb_pool;
89 1.1 jmcneill size_t sb_size; /* size in bytes */
90 1.1 jmcneill size_t sb_npages; /* number of pages */
91 1.1 jmcneill uint8_t **sb_page_ary; /* array of page pointers */
92 1.1 jmcneill };
93 1.1 jmcneill
94 1.1 jmcneill struct scatter_io {
95 1.1 jmcneill struct scatter_buf *sio_buf;
96 1.1 jmcneill off_t sio_offset;
97 1.1 jmcneill size_t sio_resid;
98 1.1 jmcneill };
99 1.1 jmcneill
100 1.1 jmcneill static void scatter_buf_init(struct scatter_buf *);
101 1.1 jmcneill static void scatter_buf_destroy(struct scatter_buf *);
102 1.1 jmcneill static int scatter_buf_set_size(struct scatter_buf *, size_t);
103 1.1 jmcneill static paddr_t scatter_buf_map(struct scatter_buf *, off_t);
104 1.1 jmcneill
105 1.1 jmcneill static bool scatter_io_init(struct scatter_buf *, off_t, size_t, struct scatter_io *);
106 1.1 jmcneill static bool scatter_io_next(struct scatter_io *, void **, size_t *);
107 1.1 jmcneill static void scatter_io_undo(struct scatter_io *, size_t);
108 1.1 jmcneill static void scatter_io_copyin(struct scatter_io *, const void *);
109 1.1 jmcneill /* static void scatter_io_copyout(struct scatter_io *, void *); */
110 1.1 jmcneill static int scatter_io_uiomove(struct scatter_io *, struct uio *);
111 1.1 jmcneill
112 1.1 jmcneill
113 1.1 jmcneill enum video_stream_method {
114 1.1 jmcneill VIDEO_STREAM_METHOD_NONE,
115 1.1 jmcneill VIDEO_STREAM_METHOD_READ,
116 1.1 jmcneill VIDEO_STREAM_METHOD_MMAP,
117 1.1 jmcneill VIDEO_STREAM_METHOD_USERPTR
118 1.1 jmcneill };
119 1.1 jmcneill
120 1.1 jmcneill struct video_buffer {
121 1.1 jmcneill struct v4l2_buffer *vb_buf;
122 1.1 jmcneill SIMPLEQ_ENTRY(video_buffer) entries;
123 1.1 jmcneill };
124 1.1 jmcneill
125 1.1 jmcneill SIMPLEQ_HEAD(sample_queue, video_buffer);
126 1.1 jmcneill
127 1.1 jmcneill struct video_stream {
128 1.1 jmcneill int vs_flags; /* flags given to open() */
129 1.1 jmcneill
130 1.1 jmcneill struct video_format vs_format;
131 1.1 jmcneill
132 1.1 jmcneill int vs_frameno; /* toggles between 0 and 1,
133 1.1 jmcneill * or -1 if new */
134 1.1 jmcneill uint32_t vs_sequence; /* absoulte frame/sample number in
135 1.1 jmcneill * sequence, wraps around */
136 1.1 jmcneill bool vs_drop; /* drop payloads from current
137 1.1 jmcneill * frameno? */
138 1.1 jmcneill
139 1.1 jmcneill enum v4l2_buf_type vs_type;
140 1.1 jmcneill uint8_t vs_nbufs;
141 1.1 jmcneill struct video_buffer **vs_buf;
142 1.1 jmcneill
143 1.1 jmcneill struct scatter_buf vs_data; /* stores video data for MMAP
144 1.1 jmcneill * and READ */
145 1.1 jmcneill
146 1.1 jmcneill /* Video samples may exist in different locations. Initially,
147 1.1 jmcneill * samples are queued into the ingress queue. The driver
148 1.1 jmcneill * grabs these in turn and fills them with video data. Once
149 1.1 jmcneill * filled, they are moved to the egress queue. Samples are
150 1.1 jmcneill * dequeued either by user with MMAP method or, with READ
151 1.1 jmcneill * method, videoread() works from the fist sample in the
152 1.1 jmcneill * ingress queue without dequeing. In the first case, the
153 1.1 jmcneill * user re-queues the buffer when finished, and videoread()
154 1.1 jmcneill * does the same when all data has been read. The sample now
155 1.1 jmcneill * returns to the ingress queue. */
156 1.1 jmcneill struct sample_queue vs_ingress; /* samples under driver control */
157 1.1 jmcneill struct sample_queue vs_egress; /* samples headed for userspace */
158 1.1 jmcneill
159 1.1 jmcneill bool vs_streaming;
160 1.1 jmcneill enum video_stream_method vs_method; /* method by which
161 1.1 jmcneill * userspace will read
162 1.1 jmcneill * samples */
163 1.1 jmcneill
164 1.1 jmcneill kmutex_t vs_lock; /* Lock to manipulate queues.
165 1.1 jmcneill * Should also be held when
166 1.1 jmcneill * changing number of
167 1.1 jmcneill * buffers. */
168 1.1 jmcneill kcondvar_t vs_sample_cv; /* signaled on new
169 1.1 jmcneill * ingress sample */
170 1.7 jmcneill struct selinfo vs_sel;
171 1.1 jmcneill
172 1.1 jmcneill uint32_t vs_bytesread; /* bytes read() from current
173 1.1 jmcneill * sample thus far */
174 1.1 jmcneill };
175 1.1 jmcneill
176 1.1 jmcneill struct video_softc {
177 1.1 jmcneill device_t sc_dev;
178 1.1 jmcneill device_t hw_dev; /* Hardware (parent) device */
179 1.1 jmcneill void * hw_softc; /* Hardware device private softc */
180 1.1 jmcneill const struct video_hw_if *hw_if; /* Hardware interface */
181 1.1 jmcneill
182 1.1 jmcneill u_int sc_open;
183 1.1 jmcneill int sc_refcnt;
184 1.1 jmcneill int sc_opencnt;
185 1.1 jmcneill bool sc_dying;
186 1.1 jmcneill
187 1.1 jmcneill struct video_stream sc_stream_in;
188 1.1 jmcneill };
189 1.1 jmcneill static int video_print(void *, const char *);
190 1.1 jmcneill
191 1.1 jmcneill static int video_match(device_t, cfdata_t, void *);
192 1.1 jmcneill static void video_attach(device_t, device_t, void *);
193 1.1 jmcneill static int video_detach(device_t, int);
194 1.1 jmcneill static int video_activate(device_t, enum devact);
195 1.1 jmcneill
196 1.1 jmcneill dev_type_open(videoopen);
197 1.1 jmcneill dev_type_close(videoclose);
198 1.1 jmcneill dev_type_read(videoread);
199 1.1 jmcneill dev_type_write(videowrite);
200 1.1 jmcneill dev_type_ioctl(videoioctl);
201 1.1 jmcneill dev_type_poll(videopoll);
202 1.1 jmcneill dev_type_mmap(videommap);
203 1.1 jmcneill
204 1.1 jmcneill const struct cdevsw video_cdevsw = {
205 1.1 jmcneill videoopen, videoclose, videoread, videowrite, videoioctl,
206 1.1 jmcneill nostop, notty, videopoll, videommap, nokqfilter, D_OTHER
207 1.1 jmcneill };
208 1.1 jmcneill
209 1.1 jmcneill #define VIDEOUNIT(n) (minor(n))
210 1.1 jmcneill
211 1.1 jmcneill CFATTACH_DECL_NEW(video, sizeof(struct video_softc),
212 1.1 jmcneill video_match, video_attach, video_detach, video_activate);
213 1.1 jmcneill
214 1.1 jmcneill extern struct cfdriver video_cd;
215 1.1 jmcneill
216 1.1 jmcneill static const char * video_pixel_format_str(enum video_pixel_format);
217 1.1 jmcneill
218 1.1 jmcneill /* convert various values from V4L2 to native values of this driver */
219 1.1 jmcneill static uint16_t v4l2id_to_control_id(uint32_t);
220 1.1 jmcneill static uint32_t control_flags_to_v4l2flags(uint32_t);
221 1.1 jmcneill static enum v4l2_ctrl_type control_type_to_v4l2type(enum video_control_type);
222 1.1 jmcneill
223 1.1 jmcneill static void v4l2_format_to_video_format(const struct v4l2_format *,
224 1.1 jmcneill struct video_format *);
225 1.1 jmcneill static void video_format_to_v4l2_format(const struct video_format *,
226 1.1 jmcneill struct v4l2_format *);
227 1.1 jmcneill
228 1.1 jmcneill /* V4L2 api functions, typically called from videoioclt() */
229 1.1 jmcneill static int video_enum_format(struct video_softc *, struct v4l2_fmtdesc *);
230 1.1 jmcneill static int video_get_format(struct video_softc *,
231 1.1 jmcneill struct v4l2_format *);
232 1.1 jmcneill static int video_set_format(struct video_softc *,
233 1.1 jmcneill struct v4l2_format *);
234 1.1 jmcneill static int video_try_format(struct video_softc *,
235 1.1 jmcneill struct v4l2_format *);
236 1.1 jmcneill static int video_query_control(struct video_softc *,
237 1.1 jmcneill struct v4l2_queryctrl *);
238 1.1 jmcneill static int video_get_control(struct video_softc *,
239 1.1 jmcneill struct v4l2_control *);
240 1.1 jmcneill static int video_set_control(struct video_softc *,
241 1.1 jmcneill const struct v4l2_control *);
242 1.1 jmcneill static int video_request_bufs(struct video_softc *,
243 1.1 jmcneill struct v4l2_requestbuffers *);
244 1.1 jmcneill static int video_query_buf(struct video_softc *, struct v4l2_buffer *);
245 1.1 jmcneill static int video_queue_buf(struct video_softc *, struct v4l2_buffer *);
246 1.1 jmcneill static int video_dequeue_buf(struct video_softc *, struct v4l2_buffer *);
247 1.1 jmcneill static int video_stream_on(struct video_softc *, enum v4l2_buf_type);
248 1.1 jmcneill static int video_stream_off(struct video_softc *, enum v4l2_buf_type);
249 1.1 jmcneill
250 1.1 jmcneill static struct video_buffer * video_buffer_alloc(void);
251 1.1 jmcneill static void video_buffer_free(struct video_buffer *);
252 1.1 jmcneill
253 1.1 jmcneill
254 1.1 jmcneill /* functions for video_stream */
255 1.1 jmcneill static void video_stream_init(struct video_stream *);
256 1.1 jmcneill static void video_stream_fini(struct video_stream *);
257 1.1 jmcneill
258 1.1 jmcneill static int video_stream_setup_bufs(struct video_stream *,
259 1.1 jmcneill enum video_stream_method,
260 1.1 jmcneill uint8_t);
261 1.1 jmcneill static void video_stream_teardown_bufs(struct video_stream *);
262 1.1 jmcneill
263 1.1 jmcneill static int video_stream_realloc_bufs(struct video_stream *, uint8_t);
264 1.1 jmcneill #define video_stream_free_bufs(vs) \
265 1.1 jmcneill video_stream_realloc_bufs((vs), 0)
266 1.1 jmcneill
267 1.1 jmcneill static void video_stream_enqueue(struct video_stream *,
268 1.1 jmcneill struct video_buffer *);
269 1.1 jmcneill static struct video_buffer * video_stream_dequeue(struct video_stream *);
270 1.1 jmcneill static void video_stream_write(struct video_stream *,
271 1.1 jmcneill const struct video_payload *);
272 1.1 jmcneill static void video_stream_sample_done(struct video_stream *);
273 1.1 jmcneill
274 1.1 jmcneill #ifdef VIDEO_DEBUG
275 1.1 jmcneill /* debugging */
276 1.1 jmcneill static const char * video_ioctl_str(u_long);
277 1.1 jmcneill #endif
278 1.1 jmcneill
279 1.1 jmcneill
280 1.1 jmcneill static int
281 1.1 jmcneill video_match(device_t parent, cfdata_t match, void *aux)
282 1.1 jmcneill {
283 1.1 jmcneill struct video_attach_args *args;
284 1.1 jmcneill
285 1.1 jmcneill args = aux;
286 1.1 jmcneill DPRINTF(("video_match: hw=%p\n", args->hw_if));
287 1.1 jmcneill return 1;
288 1.1 jmcneill }
289 1.1 jmcneill
290 1.1 jmcneill
291 1.1 jmcneill static void
292 1.1 jmcneill video_attach(device_t parent, device_t self, void *aux)
293 1.1 jmcneill {
294 1.1 jmcneill struct video_softc *sc;
295 1.1 jmcneill struct video_attach_args *args;
296 1.1 jmcneill
297 1.1 jmcneill sc = device_private(self);
298 1.1 jmcneill args = aux;
299 1.1 jmcneill
300 1.1 jmcneill sc->sc_dev = self;
301 1.1 jmcneill sc->hw_dev = parent;
302 1.1 jmcneill sc->hw_if = args->hw_if;
303 1.1 jmcneill sc->hw_softc = device_private(parent);
304 1.1 jmcneill
305 1.1 jmcneill sc->sc_open = 0;
306 1.1 jmcneill sc->sc_refcnt = 0;
307 1.1 jmcneill sc->sc_opencnt = 0;
308 1.1 jmcneill sc->sc_dying = false;
309 1.1 jmcneill
310 1.1 jmcneill video_stream_init(&sc->sc_stream_in);
311 1.1 jmcneill
312 1.5 jmcneill aprint_naive("\n");
313 1.5 jmcneill aprint_normal(": %s\n", sc->hw_if->get_devname(sc->hw_softc));
314 1.5 jmcneill
315 1.1 jmcneill DPRINTF(("video_attach: sc=%p hwif=%p\n", sc, sc->hw_if));
316 1.17 jmcneill
317 1.17 jmcneill if (!pmf_device_register(self, NULL, NULL))
318 1.17 jmcneill aprint_error_dev(self, "couldn't establish power handler\n");
319 1.1 jmcneill }
320 1.1 jmcneill
321 1.1 jmcneill
322 1.1 jmcneill static int
323 1.1 jmcneill video_activate(device_t self, enum devact act)
324 1.1 jmcneill {
325 1.1 jmcneill struct video_softc *sc;
326 1.1 jmcneill
327 1.1 jmcneill sc = device_private(self);
328 1.1 jmcneill DPRINTF(("video_activate: sc=%p\n", sc));
329 1.1 jmcneill switch (act) {
330 1.1 jmcneill case DVACT_ACTIVATE:
331 1.1 jmcneill return EOPNOTSUPP;
332 1.1 jmcneill
333 1.1 jmcneill case DVACT_DEACTIVATE:
334 1.1 jmcneill sc->sc_dying = true;
335 1.1 jmcneill break;
336 1.1 jmcneill }
337 1.1 jmcneill return 0;
338 1.1 jmcneill }
339 1.1 jmcneill
340 1.1 jmcneill
341 1.1 jmcneill static int
342 1.1 jmcneill video_detach(device_t self, int flags)
343 1.1 jmcneill {
344 1.1 jmcneill struct video_softc *sc;
345 1.1 jmcneill int maj, mn;
346 1.1 jmcneill
347 1.1 jmcneill sc = device_private(self);
348 1.1 jmcneill DPRINTF(("video_detach: sc=%p flags=%d\n", sc, flags));
349 1.1 jmcneill
350 1.1 jmcneill sc->sc_dying = true;
351 1.17 jmcneill
352 1.17 jmcneill pmf_device_deregister(self);
353 1.1 jmcneill
354 1.1 jmcneill maj = cdevsw_lookup_major(&video_cdevsw);
355 1.1 jmcneill mn = device_unit(self);
356 1.1 jmcneill /* close open instances */
357 1.1 jmcneill vdevgone(maj, mn, mn, VCHR);
358 1.1 jmcneill
359 1.1 jmcneill video_stream_fini(&sc->sc_stream_in);
360 1.1 jmcneill
361 1.1 jmcneill return 0;
362 1.1 jmcneill }
363 1.1 jmcneill
364 1.1 jmcneill
365 1.1 jmcneill static int
366 1.1 jmcneill video_print(void *aux, const char *pnp)
367 1.1 jmcneill {
368 1.1 jmcneill struct video_attach_args *arg;
369 1.1 jmcneill
370 1.1 jmcneill if (pnp != NULL) {
371 1.1 jmcneill DPRINTF(("video_print: have pnp\n"));
372 1.1 jmcneill arg = aux;
373 1.1 jmcneill aprint_normal("%s at %s\n", "video", pnp);
374 1.1 jmcneill } else {
375 1.1 jmcneill DPRINTF(("video_print: pnp is NULL\n"));
376 1.1 jmcneill }
377 1.1 jmcneill return UNCONF;
378 1.1 jmcneill }
379 1.1 jmcneill
380 1.1 jmcneill
381 1.1 jmcneill /*
382 1.1 jmcneill * Called from hardware driver. This is where the MI audio driver
383 1.1 jmcneill * gets probed/attached to the hardware driver.
384 1.1 jmcneill */
385 1.1 jmcneill device_t
386 1.1 jmcneill video_attach_mi(const struct video_hw_if *hw_if, device_t parent)
387 1.1 jmcneill {
388 1.1 jmcneill struct video_attach_args args;
389 1.1 jmcneill
390 1.1 jmcneill args.hw_if = hw_if;
391 1.1 jmcneill return config_found_ia(parent, "videobus", &args, video_print);
392 1.1 jmcneill }
393 1.1 jmcneill
394 1.1 jmcneill /* video_submit_payload - called by hardware driver to submit payload data */
395 1.1 jmcneill void
396 1.1 jmcneill video_submit_payload(device_t self, const struct video_payload *payload)
397 1.1 jmcneill {
398 1.1 jmcneill struct video_softc *sc;
399 1.1 jmcneill
400 1.1 jmcneill sc = device_private(self);
401 1.1 jmcneill
402 1.1 jmcneill if (sc == NULL)
403 1.1 jmcneill return;
404 1.1 jmcneill
405 1.1 jmcneill video_stream_write(&sc->sc_stream_in, payload);
406 1.1 jmcneill }
407 1.1 jmcneill
408 1.1 jmcneill static const char *
409 1.1 jmcneill video_pixel_format_str(enum video_pixel_format px)
410 1.1 jmcneill {
411 1.1 jmcneill switch (px) {
412 1.16 jmcneill case VIDEO_FORMAT_UYVY: return "UYVY";
413 1.14 jmcneill case VIDEO_FORMAT_YUV420: return "YUV420";
414 1.1 jmcneill case VIDEO_FORMAT_YUY2: return "YUYV";
415 1.1 jmcneill case VIDEO_FORMAT_NV12: return "NV12";
416 1.11 jmcneill case VIDEO_FORMAT_RGB24: return "RGB24";
417 1.16 jmcneill case VIDEO_FORMAT_RGB555: return "RGB555";
418 1.16 jmcneill case VIDEO_FORMAT_RGB565: return "RGB565";
419 1.15 jmcneill case VIDEO_FORMAT_SBGGR8: return "SBGGR8";
420 1.1 jmcneill case VIDEO_FORMAT_MJPEG: return "MJPEG";
421 1.1 jmcneill case VIDEO_FORMAT_DV: return "DV";
422 1.1 jmcneill case VIDEO_FORMAT_MPEG: return "MPEG";
423 1.1 jmcneill default: return "Unknown";
424 1.1 jmcneill }
425 1.1 jmcneill }
426 1.1 jmcneill
427 1.1 jmcneill /* Takes a V4L2 id and returns a "native" video driver control id.
428 1.1 jmcneill * TODO: is there a better way to do this? some kind of array? */
429 1.1 jmcneill static uint16_t
430 1.1 jmcneill v4l2id_to_control_id(uint32_t v4l2id)
431 1.1 jmcneill {
432 1.1 jmcneill /* mask includes class bits and control id bits */
433 1.1 jmcneill switch (v4l2id & 0xffffff) {
434 1.1 jmcneill case V4L2_CID_BRIGHTNESS: return VIDEO_CONTROL_BRIGHTNESS;
435 1.1 jmcneill case V4L2_CID_CONTRAST: return VIDEO_CONTROL_CONTRAST;
436 1.1 jmcneill case V4L2_CID_SATURATION: return VIDEO_CONTROL_SATURATION;
437 1.1 jmcneill case V4L2_CID_HUE: return VIDEO_CONTROL_HUE;
438 1.1 jmcneill case V4L2_CID_HUE_AUTO: return VIDEO_CONTROL_HUE_AUTO;
439 1.1 jmcneill case V4L2_CID_SHARPNESS: return VIDEO_CONTROL_SHARPNESS;
440 1.1 jmcneill case V4L2_CID_GAMMA: return VIDEO_CONTROL_GAMMA;
441 1.1 jmcneill
442 1.1 jmcneill /* "black level" means the same as "brightness", but V4L2
443 1.1 jmcneill * defines two separate controls that are not identical.
444 1.1 jmcneill * V4L2_CID_BLACK_LEVEL is deprecated however in V4L2. */
445 1.1 jmcneill case V4L2_CID_BLACK_LEVEL: return VIDEO_CONTROL_BRIGHTNESS;
446 1.1 jmcneill
447 1.1 jmcneill case V4L2_CID_AUDIO_VOLUME: return VIDEO_CONTROL_UNDEFINED;
448 1.1 jmcneill case V4L2_CID_AUDIO_BALANCE: return VIDEO_CONTROL_UNDEFINED;
449 1.1 jmcneill case V4L2_CID_AUDIO_BASS: return VIDEO_CONTROL_UNDEFINED;
450 1.1 jmcneill case V4L2_CID_AUDIO_TREBLE: return VIDEO_CONTROL_UNDEFINED;
451 1.1 jmcneill case V4L2_CID_AUDIO_MUTE: return VIDEO_CONTROL_UNDEFINED;
452 1.1 jmcneill case V4L2_CID_AUDIO_LOUDNESS: return VIDEO_CONTROL_UNDEFINED;
453 1.1 jmcneill
454 1.1 jmcneill case V4L2_CID_AUTO_WHITE_BALANCE:
455 1.1 jmcneill return VIDEO_CONTROL_WHITE_BALANCE_AUTO;
456 1.1 jmcneill case V4L2_CID_DO_WHITE_BALANCE:
457 1.1 jmcneill return VIDEO_CONTROL_WHITE_BALANCE_ACTION;
458 1.1 jmcneill case V4L2_CID_RED_BALANCE:
459 1.1 jmcneill case V4L2_CID_BLUE_BALANCE:
460 1.1 jmcneill /* This might not fit in with the control_id/value_id scheme */
461 1.1 jmcneill return VIDEO_CONTROL_WHITE_BALANCE_COMPONENT;
462 1.1 jmcneill case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
463 1.1 jmcneill return VIDEO_CONTROL_WHITE_BALANCE_TEMPERATURE;
464 1.1 jmcneill case V4L2_CID_EXPOSURE:
465 1.1 jmcneill return VIDEO_CONTROL_EXPOSURE_TIME_ABSOLUTE;
466 1.1 jmcneill case V4L2_CID_GAIN: return VIDEO_CONTROL_GAIN;
467 1.1 jmcneill case V4L2_CID_AUTOGAIN: return VIDEO_CONTROL_GAIN_AUTO;
468 1.1 jmcneill case V4L2_CID_HFLIP: return VIDEO_CONTROL_HFLIP;
469 1.1 jmcneill case V4L2_CID_VFLIP: return VIDEO_CONTROL_VFLIP;
470 1.1 jmcneill case V4L2_CID_HCENTER_DEPRECATED:
471 1.1 jmcneill case V4L2_CID_VCENTER_DEPRECATED:
472 1.1 jmcneill return VIDEO_CONTROL_UNDEFINED;
473 1.1 jmcneill case V4L2_CID_POWER_LINE_FREQUENCY:
474 1.1 jmcneill return VIDEO_CONTROL_POWER_LINE_FREQUENCY;
475 1.1 jmcneill case V4L2_CID_BACKLIGHT_COMPENSATION:
476 1.1 jmcneill return VIDEO_CONTROL_BACKLIGHT_COMPENSATION;
477 1.1 jmcneill default: return V4L2_CTRL_ID2CID(v4l2id);
478 1.1 jmcneill }
479 1.1 jmcneill }
480 1.1 jmcneill
481 1.1 jmcneill
482 1.1 jmcneill static uint32_t
483 1.1 jmcneill control_flags_to_v4l2flags(uint32_t flags)
484 1.1 jmcneill {
485 1.1 jmcneill uint32_t v4l2flags = 0;
486 1.1 jmcneill
487 1.1 jmcneill if (flags & VIDEO_CONTROL_FLAG_DISABLED)
488 1.1 jmcneill v4l2flags |= V4L2_CTRL_FLAG_INACTIVE;
489 1.1 jmcneill
490 1.1 jmcneill if (!(flags & VIDEO_CONTROL_FLAG_WRITE))
491 1.1 jmcneill v4l2flags |= V4L2_CTRL_FLAG_READ_ONLY;
492 1.1 jmcneill
493 1.1 jmcneill if (flags & VIDEO_CONTROL_FLAG_AUTOUPDATE)
494 1.1 jmcneill v4l2flags |= V4L2_CTRL_FLAG_GRABBED;
495 1.1 jmcneill
496 1.1 jmcneill return v4l2flags;
497 1.1 jmcneill }
498 1.1 jmcneill
499 1.1 jmcneill
500 1.1 jmcneill static enum v4l2_ctrl_type
501 1.1 jmcneill control_type_to_v4l2type(enum video_control_type type) {
502 1.1 jmcneill switch (type) {
503 1.1 jmcneill case VIDEO_CONTROL_TYPE_INT: return V4L2_CTRL_TYPE_INTEGER;
504 1.1 jmcneill case VIDEO_CONTROL_TYPE_BOOL: return V4L2_CTRL_TYPE_BOOLEAN;
505 1.1 jmcneill case VIDEO_CONTROL_TYPE_LIST: return V4L2_CTRL_TYPE_MENU;
506 1.1 jmcneill case VIDEO_CONTROL_TYPE_ACTION: return V4L2_CTRL_TYPE_BUTTON;
507 1.1 jmcneill default: return V4L2_CTRL_TYPE_INTEGER; /* err? */
508 1.1 jmcneill }
509 1.1 jmcneill }
510 1.1 jmcneill
511 1.1 jmcneill
512 1.1 jmcneill static int
513 1.1 jmcneill video_query_control(struct video_softc *sc,
514 1.1 jmcneill struct v4l2_queryctrl *query)
515 1.1 jmcneill {
516 1.1 jmcneill const struct video_hw_if *hw;
517 1.1 jmcneill struct video_control_desc_group desc_group;
518 1.1 jmcneill struct video_control_desc desc;
519 1.1 jmcneill int err;
520 1.1 jmcneill
521 1.1 jmcneill hw = sc->hw_if;
522 1.1 jmcneill if (hw->get_control_desc_group) {
523 1.1 jmcneill desc.group_id = desc.control_id =
524 1.1 jmcneill v4l2id_to_control_id(query->id);
525 1.1 jmcneill
526 1.1 jmcneill desc_group.group_id = desc.group_id;
527 1.1 jmcneill desc_group.length = 1;
528 1.1 jmcneill desc_group.desc = &desc;
529 1.1 jmcneill
530 1.1 jmcneill err = hw->get_control_desc_group(sc->hw_softc, &desc_group);
531 1.1 jmcneill if (err != 0)
532 1.1 jmcneill return err;
533 1.1 jmcneill
534 1.1 jmcneill query->type = control_type_to_v4l2type(desc.type);
535 1.1 jmcneill memcpy(query->name, desc.name, 32);
536 1.1 jmcneill query->minimum = desc.min;
537 1.1 jmcneill query->maximum = desc.max;
538 1.1 jmcneill query->step = desc.step;
539 1.1 jmcneill query->default_value = desc.def;
540 1.1 jmcneill query->flags = control_flags_to_v4l2flags(desc.flags);
541 1.1 jmcneill
542 1.1 jmcneill return 0;
543 1.1 jmcneill } else {
544 1.1 jmcneill return EINVAL;
545 1.1 jmcneill }
546 1.1 jmcneill }
547 1.1 jmcneill
548 1.1 jmcneill
549 1.1 jmcneill /* Takes a single Video4Linux2 control and queries the driver for the
550 1.1 jmcneill * current value. */
551 1.1 jmcneill static int
552 1.1 jmcneill video_get_control(struct video_softc *sc,
553 1.1 jmcneill struct v4l2_control *vcontrol)
554 1.1 jmcneill {
555 1.1 jmcneill const struct video_hw_if *hw;
556 1.1 jmcneill struct video_control_group group;
557 1.1 jmcneill struct video_control control;
558 1.1 jmcneill int err;
559 1.1 jmcneill
560 1.1 jmcneill hw = sc->hw_if;
561 1.1 jmcneill if (hw->get_control_group) {
562 1.1 jmcneill control.group_id = control.control_id =
563 1.1 jmcneill v4l2id_to_control_id(vcontrol->id);
564 1.1 jmcneill /* ?? if "control_id" is arbitrarily defined by the
565 1.1 jmcneill * driver, then we need some way to store it... Maybe
566 1.1 jmcneill * it doesn't matter for single value controls. */
567 1.1 jmcneill control.value = 0;
568 1.1 jmcneill
569 1.1 jmcneill group.group_id = control.group_id;
570 1.1 jmcneill group.length = 1;
571 1.1 jmcneill group.control = &control;
572 1.1 jmcneill
573 1.1 jmcneill err = hw->get_control_group(sc->hw_softc, &group);
574 1.1 jmcneill if (err != 0)
575 1.1 jmcneill return err;
576 1.1 jmcneill
577 1.1 jmcneill vcontrol->value = control.value;
578 1.1 jmcneill return 0;
579 1.1 jmcneill } else {
580 1.1 jmcneill return EINVAL;
581 1.1 jmcneill }
582 1.1 jmcneill }
583 1.1 jmcneill
584 1.1 jmcneill static void
585 1.1 jmcneill video_format_to_v4l2_format(const struct video_format *src,
586 1.1 jmcneill struct v4l2_format *dest)
587 1.1 jmcneill {
588 1.1 jmcneill /* TODO: what about win and vbi formats? */
589 1.1 jmcneill dest->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
590 1.1 jmcneill dest->fmt.pix.width = src->width;
591 1.1 jmcneill dest->fmt.pix.height = src->height;
592 1.1 jmcneill dest->fmt.pix.field = V4L2_FIELD_NONE; /* TODO: for now,
593 1.1 jmcneill * just set to
594 1.1 jmcneill * progressive */
595 1.1 jmcneill dest->fmt.pix.bytesperline = src->stride;
596 1.1 jmcneill dest->fmt.pix.sizeimage = src->sample_size;
597 1.15 jmcneill dest->fmt.pix.colorspace = 0; /* XXX */
598 1.15 jmcneill dest->fmt.pix.priv = src->priv;
599 1.1 jmcneill
600 1.1 jmcneill switch (src->pixel_format) {
601 1.16 jmcneill case VIDEO_FORMAT_UYVY:
602 1.16 jmcneill dest->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
603 1.16 jmcneill break;
604 1.14 jmcneill case VIDEO_FORMAT_YUV420:
605 1.14 jmcneill dest->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
606 1.14 jmcneill break;
607 1.1 jmcneill case VIDEO_FORMAT_YUY2:
608 1.1 jmcneill dest->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
609 1.1 jmcneill break;
610 1.1 jmcneill case VIDEO_FORMAT_NV12:
611 1.1 jmcneill dest->fmt.pix.pixelformat = V4L2_PIX_FMT_NV12;
612 1.1 jmcneill break;
613 1.11 jmcneill case VIDEO_FORMAT_RGB24:
614 1.11 jmcneill dest->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
615 1.11 jmcneill break;
616 1.16 jmcneill case VIDEO_FORMAT_RGB555:
617 1.16 jmcneill dest->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB555;
618 1.16 jmcneill break;
619 1.16 jmcneill case VIDEO_FORMAT_RGB565:
620 1.16 jmcneill dest->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB565;
621 1.16 jmcneill break;
622 1.15 jmcneill case VIDEO_FORMAT_SBGGR8:
623 1.15 jmcneill dest->fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
624 1.15 jmcneill break;
625 1.1 jmcneill case VIDEO_FORMAT_MJPEG:
626 1.8 jmcneill dest->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
627 1.1 jmcneill break;
628 1.1 jmcneill case VIDEO_FORMAT_DV:
629 1.1 jmcneill dest->fmt.pix.pixelformat = V4L2_PIX_FMT_DV;
630 1.1 jmcneill break;
631 1.1 jmcneill case VIDEO_FORMAT_MPEG:
632 1.1 jmcneill dest->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
633 1.1 jmcneill break;
634 1.1 jmcneill case VIDEO_FORMAT_UNDEFINED:
635 1.1 jmcneill default:
636 1.1 jmcneill DPRINTF(("video_get_format: unknown pixel format %d\n",
637 1.1 jmcneill src->pixel_format));
638 1.1 jmcneill dest->fmt.pix.pixelformat = 0; /* V4L2 doesn't define
639 1.1 jmcneill * and "undefined"
640 1.1 jmcneill * format? */
641 1.1 jmcneill break;
642 1.1 jmcneill }
643 1.1 jmcneill
644 1.1 jmcneill }
645 1.1 jmcneill
646 1.1 jmcneill static void
647 1.1 jmcneill v4l2_format_to_video_format(const struct v4l2_format *src,
648 1.1 jmcneill struct video_format *dest)
649 1.1 jmcneill {
650 1.1 jmcneill switch (src->type) {
651 1.1 jmcneill case V4L2_BUF_TYPE_VIDEO_CAPTURE:
652 1.1 jmcneill dest->width = src->fmt.pix.width;
653 1.1 jmcneill dest->height = src->fmt.pix.height;
654 1.1 jmcneill
655 1.1 jmcneill dest->stride = src->fmt.pix.bytesperline;
656 1.1 jmcneill dest->sample_size = src->fmt.pix.sizeimage;
657 1.1 jmcneill
658 1.1 jmcneill switch (src->fmt.pix.pixelformat) {
659 1.16 jmcneill case V4L2_PIX_FMT_UYVY:
660 1.16 jmcneill dest->pixel_format = VIDEO_FORMAT_UYVY;
661 1.16 jmcneill break;
662 1.14 jmcneill case V4L2_PIX_FMT_YUV420:
663 1.14 jmcneill dest->pixel_format = VIDEO_FORMAT_YUV420;
664 1.14 jmcneill break;
665 1.1 jmcneill case V4L2_PIX_FMT_YUYV:
666 1.1 jmcneill dest->pixel_format = VIDEO_FORMAT_YUY2;
667 1.1 jmcneill break;
668 1.1 jmcneill case V4L2_PIX_FMT_NV12:
669 1.1 jmcneill dest->pixel_format = VIDEO_FORMAT_NV12;
670 1.1 jmcneill break;
671 1.11 jmcneill case V4L2_PIX_FMT_RGB24:
672 1.11 jmcneill dest->pixel_format = VIDEO_FORMAT_RGB24;
673 1.11 jmcneill break;
674 1.16 jmcneill case V4L2_PIX_FMT_RGB555:
675 1.16 jmcneill dest->pixel_format = VIDEO_FORMAT_RGB555;
676 1.16 jmcneill break;
677 1.16 jmcneill case V4L2_PIX_FMT_RGB565:
678 1.16 jmcneill dest->pixel_format = VIDEO_FORMAT_RGB565;
679 1.16 jmcneill break;
680 1.15 jmcneill case V4L2_PIX_FMT_SBGGR8:
681 1.15 jmcneill dest->pixel_format = VIDEO_FORMAT_SBGGR8;
682 1.15 jmcneill break;
683 1.9 jmcneill case V4L2_PIX_FMT_MJPEG:
684 1.1 jmcneill dest->pixel_format = VIDEO_FORMAT_MJPEG;
685 1.1 jmcneill break;
686 1.1 jmcneill case V4L2_PIX_FMT_DV:
687 1.1 jmcneill dest->pixel_format = VIDEO_FORMAT_DV;
688 1.1 jmcneill break;
689 1.1 jmcneill case V4L2_PIX_FMT_MPEG:
690 1.9 jmcneill dest->pixel_format = VIDEO_FORMAT_MPEG;
691 1.1 jmcneill break;
692 1.1 jmcneill default:
693 1.1 jmcneill DPRINTF(("video: unknown v4l2 pixel format %d\n",
694 1.1 jmcneill src->fmt.pix.pixelformat));
695 1.1 jmcneill dest->pixel_format = VIDEO_FORMAT_UNDEFINED;
696 1.1 jmcneill break;
697 1.1 jmcneill }
698 1.1 jmcneill break;
699 1.1 jmcneill default:
700 1.1 jmcneill /* TODO: other v4l2 format types */
701 1.1 jmcneill DPRINTF(("video: unsupported v4l2 format type %d\n",
702 1.1 jmcneill src->type));
703 1.1 jmcneill break;
704 1.1 jmcneill }
705 1.1 jmcneill }
706 1.1 jmcneill
707 1.1 jmcneill static int
708 1.1 jmcneill video_enum_format(struct video_softc *sc, struct v4l2_fmtdesc *fmtdesc)
709 1.1 jmcneill {
710 1.1 jmcneill const struct video_hw_if *hw;
711 1.1 jmcneill struct video_format vfmt;
712 1.1 jmcneill struct v4l2_format fmt;
713 1.1 jmcneill int err;
714 1.1 jmcneill
715 1.1 jmcneill hw = sc->hw_if;
716 1.1 jmcneill if (hw->enum_format == NULL)
717 1.3 jmcneill return ENOTTY;
718 1.1 jmcneill
719 1.1 jmcneill err = hw->enum_format(sc->hw_softc, fmtdesc->index, &vfmt);
720 1.1 jmcneill if (err != 0)
721 1.1 jmcneill return err;
722 1.1 jmcneill
723 1.1 jmcneill video_format_to_v4l2_format(&vfmt, &fmt);
724 1.1 jmcneill
725 1.1 jmcneill fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; /* TODO: only one type for now */
726 1.1 jmcneill if (vfmt.pixel_format >= VIDEO_FORMAT_MJPEG)
727 1.1 jmcneill fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
728 1.1 jmcneill strlcpy(fmtdesc->description,
729 1.1 jmcneill video_pixel_format_str(vfmt.pixel_format),
730 1.1 jmcneill sizeof(fmtdesc->description));
731 1.1 jmcneill fmtdesc->pixelformat = fmt.fmt.pix.pixelformat;
732 1.1 jmcneill
733 1.1 jmcneill return 0;
734 1.1 jmcneill }
735 1.1 jmcneill
736 1.1 jmcneill static int
737 1.1 jmcneill video_get_format(struct video_softc *sc,
738 1.1 jmcneill struct v4l2_format *format)
739 1.1 jmcneill {
740 1.1 jmcneill const struct video_hw_if *hw;
741 1.1 jmcneill struct video_format vfmt;
742 1.1 jmcneill int err;
743 1.1 jmcneill
744 1.1 jmcneill hw = sc->hw_if;
745 1.1 jmcneill if (hw->get_format == NULL)
746 1.3 jmcneill return ENOTTY;
747 1.1 jmcneill
748 1.1 jmcneill err = hw->get_format(sc->hw_softc, &vfmt);
749 1.1 jmcneill if (err != 0)
750 1.1 jmcneill return err;
751 1.1 jmcneill
752 1.1 jmcneill video_format_to_v4l2_format(&vfmt, format);
753 1.1 jmcneill
754 1.1 jmcneill return 0;
755 1.1 jmcneill }
756 1.1 jmcneill
757 1.1 jmcneill static int
758 1.1 jmcneill video_set_format(struct video_softc *sc, struct v4l2_format *fmt)
759 1.1 jmcneill {
760 1.1 jmcneill const struct video_hw_if *hw;
761 1.1 jmcneill struct video_format vfmt;
762 1.1 jmcneill int err;
763 1.1 jmcneill
764 1.1 jmcneill hw = sc->hw_if;
765 1.20 jmcneill if (hw->set_format == NULL)
766 1.3 jmcneill return ENOTTY;
767 1.1 jmcneill
768 1.1 jmcneill v4l2_format_to_video_format(fmt, &vfmt);
769 1.1 jmcneill
770 1.1 jmcneill err = hw->set_format(sc->hw_softc, &vfmt);
771 1.1 jmcneill if (err != 0)
772 1.1 jmcneill return err;
773 1.1 jmcneill
774 1.1 jmcneill video_format_to_v4l2_format(&vfmt, fmt);
775 1.18 jmorse sc->sc_stream_in.vs_format = vfmt;
776 1.1 jmcneill
777 1.1 jmcneill return 0;
778 1.1 jmcneill }
779 1.1 jmcneill
780 1.1 jmcneill
781 1.1 jmcneill static int
782 1.1 jmcneill video_try_format(struct video_softc *sc,
783 1.1 jmcneill struct v4l2_format *format)
784 1.1 jmcneill {
785 1.1 jmcneill const struct video_hw_if *hw;
786 1.1 jmcneill struct video_format vfmt;
787 1.1 jmcneill int err;
788 1.1 jmcneill
789 1.1 jmcneill hw = sc->hw_if;
790 1.1 jmcneill if (hw->try_format == NULL)
791 1.3 jmcneill return ENOTTY;
792 1.1 jmcneill
793 1.1 jmcneill v4l2_format_to_video_format(format, &vfmt);
794 1.1 jmcneill
795 1.1 jmcneill err = hw->try_format(sc->hw_softc, &vfmt);
796 1.1 jmcneill if (err != 0)
797 1.1 jmcneill return err;
798 1.1 jmcneill
799 1.1 jmcneill video_format_to_v4l2_format(&vfmt, format);
800 1.1 jmcneill
801 1.1 jmcneill return 0;
802 1.1 jmcneill }
803 1.1 jmcneill
804 1.1 jmcneill /* Takes a single Video4Linux2 control, converts it to a struct
805 1.1 jmcneill * video_control, and calls the hardware driver. */
806 1.1 jmcneill static int
807 1.1 jmcneill video_set_control(struct video_softc *sc,
808 1.1 jmcneill const struct v4l2_control *vcontrol)
809 1.1 jmcneill {
810 1.1 jmcneill const struct video_hw_if *hw;
811 1.1 jmcneill struct video_control_group group;
812 1.1 jmcneill struct video_control control;
813 1.1 jmcneill
814 1.1 jmcneill hw = sc->hw_if;
815 1.1 jmcneill if (hw->set_control_group) {
816 1.1 jmcneill control.group_id = control.control_id =
817 1.1 jmcneill v4l2id_to_control_id(vcontrol->id);
818 1.1 jmcneill /* ?? if "control_id" is arbitrarily defined by the
819 1.1 jmcneill * driver, then we need some way to store it... Maybe
820 1.1 jmcneill * it doesn't matter for single value controls. */
821 1.1 jmcneill control.value = vcontrol->value;
822 1.1 jmcneill
823 1.1 jmcneill group.group_id = control.group_id;
824 1.1 jmcneill group.length = 1;
825 1.1 jmcneill group.control = &control;
826 1.1 jmcneill
827 1.1 jmcneill return (hw->set_control_group(sc->hw_softc, &group));
828 1.1 jmcneill } else {
829 1.1 jmcneill return EINVAL;
830 1.1 jmcneill }
831 1.1 jmcneill }
832 1.1 jmcneill
833 1.1 jmcneill static int
834 1.1 jmcneill video_request_bufs(struct video_softc *sc,
835 1.1 jmcneill struct v4l2_requestbuffers *req)
836 1.1 jmcneill {
837 1.1 jmcneill struct video_stream *vs = &sc->sc_stream_in;
838 1.1 jmcneill struct v4l2_buffer *buf;
839 1.1 jmcneill int i, err;
840 1.1 jmcneill
841 1.1 jmcneill if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
842 1.1 jmcneill return EINVAL;
843 1.1 jmcneill
844 1.1 jmcneill vs->vs_type = req->type;
845 1.1 jmcneill
846 1.1 jmcneill switch (req->memory) {
847 1.1 jmcneill case V4L2_MEMORY_MMAP:
848 1.1 jmcneill if (req->count < VIDEO_MIN_BUFS)
849 1.1 jmcneill req->count = VIDEO_MIN_BUFS;
850 1.1 jmcneill else if (req->count > VIDEO_MAX_BUFS)
851 1.1 jmcneill req->count = VIDEO_MAX_BUFS;
852 1.1 jmcneill
853 1.1 jmcneill err = video_stream_setup_bufs(vs,
854 1.1 jmcneill VIDEO_STREAM_METHOD_MMAP,
855 1.1 jmcneill req->count);
856 1.1 jmcneill if (err != 0)
857 1.1 jmcneill return err;
858 1.1 jmcneill
859 1.1 jmcneill for (i = 0; i < req->count; ++i) {
860 1.1 jmcneill buf = vs->vs_buf[i]->vb_buf;
861 1.1 jmcneill buf->memory = V4L2_MEMORY_MMAP;
862 1.1 jmcneill buf->flags |= V4L2_BUF_FLAG_MAPPED;
863 1.1 jmcneill }
864 1.1 jmcneill break;
865 1.1 jmcneill case V4L2_MEMORY_USERPTR:
866 1.1 jmcneill default:
867 1.1 jmcneill return EINVAL;
868 1.1 jmcneill }
869 1.1 jmcneill
870 1.1 jmcneill return 0;
871 1.1 jmcneill }
872 1.1 jmcneill
873 1.1 jmcneill static int
874 1.1 jmcneill video_query_buf(struct video_softc *sc,
875 1.1 jmcneill struct v4l2_buffer *buf)
876 1.1 jmcneill {
877 1.1 jmcneill struct video_stream *vs = &sc->sc_stream_in;
878 1.1 jmcneill
879 1.1 jmcneill if (buf->type != vs->vs_type)
880 1.1 jmcneill return EINVAL;
881 1.1 jmcneill if (buf->index >= vs->vs_nbufs)
882 1.1 jmcneill return EINVAL;
883 1.1 jmcneill
884 1.1 jmcneill memcpy(buf, vs->vs_buf[buf->index]->vb_buf, sizeof(*buf));
885 1.1 jmcneill
886 1.1 jmcneill return 0;
887 1.1 jmcneill }
888 1.1 jmcneill
889 1.1 jmcneill /* Accept a buffer descriptor from userspace and return the indicated
890 1.1 jmcneill * buffer to the driver's queue. */
891 1.1 jmcneill static int
892 1.1 jmcneill video_queue_buf(struct video_softc *sc, struct v4l2_buffer *userbuf)
893 1.1 jmcneill {
894 1.1 jmcneill struct video_stream *vs = &sc->sc_stream_in;
895 1.1 jmcneill struct video_buffer *vb;
896 1.1 jmcneill struct v4l2_buffer *driverbuf;
897 1.1 jmcneill
898 1.1 jmcneill if (userbuf->type != vs->vs_type) {
899 1.1 jmcneill DPRINTF(("video_queue_buf: expected type=%d got type=%d\n",
900 1.1 jmcneill userbuf->type, vs->vs_type));
901 1.1 jmcneill return EINVAL;
902 1.1 jmcneill }
903 1.1 jmcneill if (userbuf->index >= vs->vs_nbufs) {
904 1.1 jmcneill DPRINTF(("video_queue_buf: invalid index %d >= %d\n",
905 1.1 jmcneill userbuf->index, vs->vs_nbufs));
906 1.1 jmcneill return EINVAL;
907 1.1 jmcneill }
908 1.1 jmcneill
909 1.1 jmcneill switch (vs->vs_method) {
910 1.1 jmcneill case VIDEO_STREAM_METHOD_MMAP:
911 1.1 jmcneill if (userbuf->memory != V4L2_MEMORY_MMAP) {
912 1.1 jmcneill DPRINTF(("video_queue_buf: invalid memory=%d\n",
913 1.1 jmcneill userbuf->memory));
914 1.1 jmcneill return EINVAL;
915 1.1 jmcneill }
916 1.1 jmcneill
917 1.1 jmcneill mutex_enter(&vs->vs_lock);
918 1.1 jmcneill
919 1.1 jmcneill vb = vs->vs_buf[userbuf->index];
920 1.1 jmcneill driverbuf = vb->vb_buf;
921 1.1 jmcneill if (driverbuf->flags & V4L2_BUF_FLAG_QUEUED) {
922 1.1 jmcneill DPRINTF(("video_queue_buf: buf already queued; "
923 1.1 jmcneill "flags=0x%x\n", driverbuf->flags));
924 1.1 jmcneill mutex_exit(&vs->vs_lock);
925 1.1 jmcneill return EINVAL;
926 1.1 jmcneill }
927 1.1 jmcneill video_stream_enqueue(vs, vb);
928 1.1 jmcneill memcpy(userbuf, driverbuf, sizeof(*driverbuf));
929 1.1 jmcneill
930 1.1 jmcneill mutex_exit(&vs->vs_lock);
931 1.1 jmcneill break;
932 1.1 jmcneill default:
933 1.1 jmcneill return EINVAL;
934 1.1 jmcneill }
935 1.1 jmcneill
936 1.1 jmcneill return 0;
937 1.1 jmcneill }
938 1.1 jmcneill
939 1.1 jmcneill /* Dequeue the described buffer from the driver queue, making it
940 1.1 jmcneill * available for reading via mmap. */
941 1.1 jmcneill static int
942 1.1 jmcneill video_dequeue_buf(struct video_softc *sc, struct v4l2_buffer *buf)
943 1.1 jmcneill {
944 1.1 jmcneill struct video_stream *vs = &sc->sc_stream_in;
945 1.1 jmcneill struct video_buffer *vb;
946 1.1 jmcneill int err;
947 1.1 jmcneill
948 1.15 jmcneill if (buf->type != vs->vs_type) {
949 1.15 jmcneill aprint_debug_dev(sc->sc_dev,
950 1.15 jmcneill "requested type %d (expected %d)\n",
951 1.15 jmcneill buf->type, vs->vs_type);
952 1.1 jmcneill return EINVAL;
953 1.15 jmcneill }
954 1.1 jmcneill
955 1.1 jmcneill switch (vs->vs_method) {
956 1.1 jmcneill case VIDEO_STREAM_METHOD_MMAP:
957 1.15 jmcneill if (buf->memory != V4L2_MEMORY_MMAP) {
958 1.15 jmcneill aprint_debug_dev(sc->sc_dev,
959 1.15 jmcneill "requested memory %d (expected %d)\n",
960 1.15 jmcneill buf->memory, V4L2_MEMORY_MMAP);
961 1.1 jmcneill return EINVAL;
962 1.15 jmcneill }
963 1.1 jmcneill
964 1.1 jmcneill mutex_enter(&vs->vs_lock);
965 1.1 jmcneill
966 1.1 jmcneill if (vs->vs_flags & O_NONBLOCK) {
967 1.1 jmcneill vb = video_stream_dequeue(vs);
968 1.1 jmcneill if (vb == NULL) {
969 1.1 jmcneill mutex_exit(&vs->vs_lock);
970 1.1 jmcneill return EAGAIN;
971 1.1 jmcneill }
972 1.1 jmcneill } else {
973 1.1 jmcneill /* Block until we have sample */
974 1.1 jmcneill while ((vb = video_stream_dequeue(vs)) == NULL) {
975 1.19 drochner if (!vs->vs_streaming) {
976 1.19 drochner mutex_exit(&vs->vs_lock);
977 1.19 drochner return EINVAL;
978 1.19 drochner }
979 1.1 jmcneill err = cv_wait_sig(&vs->vs_sample_cv,
980 1.1 jmcneill &vs->vs_lock);
981 1.1 jmcneill if (err != 0) {
982 1.1 jmcneill mutex_exit(&vs->vs_lock);
983 1.1 jmcneill return EINTR;
984 1.1 jmcneill }
985 1.1 jmcneill }
986 1.1 jmcneill }
987 1.1 jmcneill
988 1.1 jmcneill memcpy(buf, vb->vb_buf, sizeof(*buf));
989 1.1 jmcneill
990 1.1 jmcneill mutex_exit(&vs->vs_lock);
991 1.1 jmcneill break;
992 1.1 jmcneill default:
993 1.15 jmcneill aprint_debug_dev(sc->sc_dev, "unknown vs_method %d\n",
994 1.15 jmcneill vs->vs_method);
995 1.1 jmcneill return EINVAL;
996 1.1 jmcneill }
997 1.1 jmcneill
998 1.1 jmcneill return 0;
999 1.1 jmcneill }
1000 1.1 jmcneill
1001 1.1 jmcneill static int
1002 1.1 jmcneill video_stream_on(struct video_softc *sc, enum v4l2_buf_type type)
1003 1.1 jmcneill {
1004 1.1 jmcneill int err;
1005 1.1 jmcneill struct video_stream *vs = &sc->sc_stream_in;
1006 1.1 jmcneill const struct video_hw_if *hw;
1007 1.1 jmcneill
1008 1.1 jmcneill if (vs->vs_streaming)
1009 1.1 jmcneill return 0;
1010 1.1 jmcneill if (type != vs->vs_type)
1011 1.1 jmcneill return EINVAL;
1012 1.1 jmcneill
1013 1.1 jmcneill hw = sc->hw_if;
1014 1.1 jmcneill if (hw == NULL)
1015 1.1 jmcneill return ENXIO;
1016 1.1 jmcneill
1017 1.1 jmcneill
1018 1.1 jmcneill err = hw->start_transfer(sc->hw_softc);
1019 1.1 jmcneill if (err != 0)
1020 1.1 jmcneill return err;
1021 1.1 jmcneill
1022 1.1 jmcneill vs->vs_streaming = true;
1023 1.1 jmcneill return 0;
1024 1.1 jmcneill }
1025 1.1 jmcneill
1026 1.1 jmcneill static int
1027 1.1 jmcneill video_stream_off(struct video_softc *sc, enum v4l2_buf_type type)
1028 1.1 jmcneill {
1029 1.1 jmcneill int err;
1030 1.1 jmcneill struct video_stream *vs = &sc->sc_stream_in;
1031 1.1 jmcneill const struct video_hw_if *hw;
1032 1.1 jmcneill
1033 1.1 jmcneill if (!vs->vs_streaming)
1034 1.1 jmcneill return 0;
1035 1.1 jmcneill if (type != vs->vs_type)
1036 1.1 jmcneill return EINVAL;
1037 1.1 jmcneill
1038 1.1 jmcneill hw = sc->hw_if;
1039 1.1 jmcneill if (hw == NULL)
1040 1.1 jmcneill return ENXIO;
1041 1.1 jmcneill
1042 1.1 jmcneill err = hw->stop_transfer(sc->hw_softc);
1043 1.1 jmcneill if (err != 0)
1044 1.1 jmcneill return err;
1045 1.1 jmcneill
1046 1.1 jmcneill vs->vs_frameno = -1;
1047 1.1 jmcneill vs->vs_sequence = 0;
1048 1.1 jmcneill vs->vs_streaming = false;
1049 1.1 jmcneill
1050 1.1 jmcneill return 0;
1051 1.1 jmcneill }
1052 1.1 jmcneill
1053 1.1 jmcneill int
1054 1.1 jmcneill videoopen(dev_t dev, int flags, int ifmt, struct lwp *l)
1055 1.1 jmcneill {
1056 1.1 jmcneill struct video_softc *sc;
1057 1.1 jmcneill const struct video_hw_if *hw;
1058 1.1 jmcneill struct video_stream *vs;
1059 1.1 jmcneill int err;
1060 1.1 jmcneill
1061 1.1 jmcneill DPRINTF(("videoopen\n"));
1062 1.1 jmcneill
1063 1.1 jmcneill sc = device_private(device_lookup(&video_cd, VIDEOUNIT(dev)));
1064 1.1 jmcneill if (sc == NULL) {
1065 1.1 jmcneill DPRINTF(("videoopen: failed to get softc\n"));
1066 1.1 jmcneill return ENXIO;
1067 1.1 jmcneill }
1068 1.1 jmcneill
1069 1.1 jmcneill if (sc->sc_dying) {
1070 1.1 jmcneill DPRINTF(("videoopen: dying\n"));
1071 1.1 jmcneill return EIO;
1072 1.1 jmcneill }
1073 1.1 jmcneill
1074 1.1 jmcneill sc->sc_stream_in.vs_flags = flags;
1075 1.1 jmcneill
1076 1.1 jmcneill DPRINTF(("videoopen: flags=0x%x sc=%p parent=%p\n",
1077 1.1 jmcneill flags, sc, sc->hw_dev));
1078 1.1 jmcneill
1079 1.1 jmcneill hw = sc->hw_if;
1080 1.1 jmcneill if (hw == NULL)
1081 1.1 jmcneill return ENXIO;
1082 1.1 jmcneill
1083 1.1 jmcneill device_active(sc->sc_dev, DVA_SYSTEM);
1084 1.1 jmcneill
1085 1.1 jmcneill sc->sc_opencnt++;
1086 1.1 jmcneill
1087 1.1 jmcneill if (hw->open != NULL) {
1088 1.1 jmcneill err = hw->open(sc->hw_softc, flags);
1089 1.1 jmcneill if (err)
1090 1.1 jmcneill return err;
1091 1.1 jmcneill }
1092 1.1 jmcneill
1093 1.1 jmcneill /* set up input stream. TODO: check flags to determine if
1094 1.1 jmcneill * "read" is desired? */
1095 1.1 jmcneill vs = &sc->sc_stream_in;
1096 1.1 jmcneill
1097 1.15 jmcneill if (hw->get_format != NULL) {
1098 1.15 jmcneill err = hw->get_format(sc->hw_softc, &vs->vs_format);
1099 1.1 jmcneill if (err != 0)
1100 1.1 jmcneill return err;
1101 1.1 jmcneill }
1102 1.1 jmcneill return 0;
1103 1.1 jmcneill }
1104 1.1 jmcneill
1105 1.1 jmcneill
1106 1.1 jmcneill int
1107 1.1 jmcneill videoclose(dev_t dev, int flags, int ifmt, struct lwp *l)
1108 1.1 jmcneill {
1109 1.1 jmcneill struct video_softc *sc;
1110 1.1 jmcneill const struct video_hw_if *hw;
1111 1.1 jmcneill
1112 1.1 jmcneill sc = device_private(device_lookup(&video_cd, VIDEOUNIT(dev)));
1113 1.1 jmcneill if (sc == NULL)
1114 1.1 jmcneill return ENXIO;
1115 1.1 jmcneill
1116 1.1 jmcneill DPRINTF(("videoclose: sc=%p\n", sc));
1117 1.1 jmcneill
1118 1.1 jmcneill hw = sc->hw_if;
1119 1.1 jmcneill if (hw == NULL)
1120 1.1 jmcneill return ENXIO;
1121 1.1 jmcneill
1122 1.1 jmcneill device_active(sc->sc_dev, DVA_SYSTEM);
1123 1.1 jmcneill
1124 1.1 jmcneill video_stream_off(sc, sc->sc_stream_in.vs_type);
1125 1.1 jmcneill
1126 1.1 jmcneill /* ignore error */
1127 1.1 jmcneill if (hw->close != NULL)
1128 1.1 jmcneill hw->close(sc->hw_softc);
1129 1.1 jmcneill
1130 1.1 jmcneill video_stream_teardown_bufs(&sc->sc_stream_in);
1131 1.1 jmcneill
1132 1.1 jmcneill sc->sc_open = 0;
1133 1.1 jmcneill sc->sc_opencnt--;
1134 1.1 jmcneill
1135 1.1 jmcneill return 0;
1136 1.1 jmcneill }
1137 1.1 jmcneill
1138 1.1 jmcneill
1139 1.1 jmcneill int
1140 1.1 jmcneill videoread(dev_t dev, struct uio *uio, int ioflag)
1141 1.1 jmcneill {
1142 1.1 jmcneill struct video_softc *sc;
1143 1.1 jmcneill struct video_stream *vs;
1144 1.1 jmcneill struct video_buffer *vb;
1145 1.1 jmcneill struct scatter_io sio;
1146 1.1 jmcneill int err;
1147 1.1 jmcneill size_t len;
1148 1.1 jmcneill off_t offset;
1149 1.1 jmcneill
1150 1.1 jmcneill sc = device_private(device_lookup(&video_cd, VIDEOUNIT(dev)));
1151 1.1 jmcneill if (sc == NULL)
1152 1.1 jmcneill return ENXIO;
1153 1.1 jmcneill
1154 1.1 jmcneill if (sc->sc_dying)
1155 1.1 jmcneill return EIO;
1156 1.1 jmcneill
1157 1.1 jmcneill vs = &sc->sc_stream_in;
1158 1.1 jmcneill
1159 1.1 jmcneill /* userspace has chosen read() method */
1160 1.1 jmcneill if (vs->vs_method == VIDEO_STREAM_METHOD_NONE) {
1161 1.1 jmcneill err = video_stream_setup_bufs(vs,
1162 1.1 jmcneill VIDEO_STREAM_METHOD_READ,
1163 1.1 jmcneill VIDEO_NUM_BUFS);
1164 1.1 jmcneill if (err != 0)
1165 1.1 jmcneill return err;
1166 1.1 jmcneill
1167 1.1 jmcneill err = video_stream_on(sc, vs->vs_type);
1168 1.1 jmcneill if (err != 0)
1169 1.1 jmcneill return err;
1170 1.1 jmcneill } else if (vs->vs_method != VIDEO_STREAM_METHOD_READ) {
1171 1.1 jmcneill return EBUSY;
1172 1.1 jmcneill }
1173 1.1 jmcneill
1174 1.1 jmcneill mutex_enter(&vs->vs_lock);
1175 1.1 jmcneill
1176 1.1 jmcneill retry:
1177 1.1 jmcneill if (SIMPLEQ_EMPTY(&vs->vs_egress)) {
1178 1.1 jmcneill if (vs->vs_flags & O_NONBLOCK) {
1179 1.1 jmcneill mutex_exit(&vs->vs_lock);
1180 1.1 jmcneill return EAGAIN;
1181 1.1 jmcneill }
1182 1.1 jmcneill
1183 1.1 jmcneill /* Block until we have a sample */
1184 1.1 jmcneill while (SIMPLEQ_EMPTY(&vs->vs_egress)) {
1185 1.1 jmcneill err = cv_wait_sig(&vs->vs_sample_cv,
1186 1.1 jmcneill &vs->vs_lock);
1187 1.1 jmcneill if (err != 0) {
1188 1.1 jmcneill mutex_exit(&vs->vs_lock);
1189 1.1 jmcneill return EINTR;
1190 1.1 jmcneill }
1191 1.1 jmcneill }
1192 1.1 jmcneill
1193 1.1 jmcneill vb = SIMPLEQ_FIRST(&vs->vs_egress);
1194 1.1 jmcneill } else {
1195 1.1 jmcneill vb = SIMPLEQ_FIRST(&vs->vs_egress);
1196 1.1 jmcneill }
1197 1.1 jmcneill
1198 1.1 jmcneill /* Oops, empty sample buffer. */
1199 1.1 jmcneill if (vb->vb_buf->bytesused == 0) {
1200 1.1 jmcneill vb = video_stream_dequeue(vs);
1201 1.1 jmcneill video_stream_enqueue(vs, vb);
1202 1.1 jmcneill vs->vs_bytesread = 0;
1203 1.1 jmcneill goto retry;
1204 1.1 jmcneill }
1205 1.1 jmcneill
1206 1.1 jmcneill mutex_exit(&vs->vs_lock);
1207 1.1 jmcneill
1208 1.1 jmcneill len = min(uio->uio_resid, vb->vb_buf->bytesused - vs->vs_bytesread);
1209 1.1 jmcneill offset = vb->vb_buf->m.offset + vs->vs_bytesread;
1210 1.1 jmcneill
1211 1.1 jmcneill if (scatter_io_init(&vs->vs_data, offset, len, &sio)) {
1212 1.1 jmcneill err = scatter_io_uiomove(&sio, uio);
1213 1.1 jmcneill if (err == EFAULT)
1214 1.1 jmcneill return EFAULT;
1215 1.1 jmcneill vs->vs_bytesread += (len - sio.sio_resid);
1216 1.1 jmcneill } else {
1217 1.1 jmcneill DPRINTF(("video: invalid read\n"));
1218 1.1 jmcneill }
1219 1.1 jmcneill
1220 1.1 jmcneill /* Move the sample to the ingress queue if everything has
1221 1.1 jmcneill * been read */
1222 1.1 jmcneill if (vs->vs_bytesread >= vb->vb_buf->bytesused) {
1223 1.1 jmcneill mutex_enter(&vs->vs_lock);
1224 1.1 jmcneill vb = video_stream_dequeue(vs);
1225 1.1 jmcneill video_stream_enqueue(vs, vb);
1226 1.1 jmcneill mutex_exit(&vs->vs_lock);
1227 1.1 jmcneill
1228 1.1 jmcneill vs->vs_bytesread = 0;
1229 1.1 jmcneill }
1230 1.1 jmcneill
1231 1.1 jmcneill return 0;
1232 1.1 jmcneill }
1233 1.1 jmcneill
1234 1.1 jmcneill
1235 1.1 jmcneill int
1236 1.1 jmcneill videowrite(dev_t dev, struct uio *uio, int ioflag)
1237 1.1 jmcneill {
1238 1.1 jmcneill return ENXIO;
1239 1.1 jmcneill }
1240 1.1 jmcneill
1241 1.1 jmcneill
1242 1.22 christos static void
1243 1.22 christos buf32tobuf(const void *data, struct v4l2_buffer *buf)
1244 1.22 christos {
1245 1.22 christos const struct v4l2_buffer32 *b32 = data;
1246 1.22 christos
1247 1.22 christos buf->index = b32->index;
1248 1.22 christos buf->type = b32->type;
1249 1.22 christos buf->bytesused = b32->bytesused;
1250 1.22 christos buf->flags = b32->flags;
1251 1.22 christos buf->field = b32->field;
1252 1.22 christos buf->timestamp.tv_sec = b32->timestamp.tv_sec;
1253 1.22 christos buf->timestamp.tv_usec = b32->timestamp.tv_usec;
1254 1.22 christos buf->timecode = b32->timecode;
1255 1.22 christos buf->sequence = b32->sequence;
1256 1.22 christos buf->memory = b32->memory;
1257 1.22 christos buf->m.offset = b32->m.offset;
1258 1.22 christos /* XXX: Handle userptr */
1259 1.22 christos buf->length = b32->length;
1260 1.22 christos buf->input = b32->input;
1261 1.22 christos buf->reserved = b32->reserved;
1262 1.22 christos }
1263 1.22 christos
1264 1.22 christos static void
1265 1.22 christos buftobuf32(void *data, const struct v4l2_buffer *buf)
1266 1.22 christos {
1267 1.22 christos struct v4l2_buffer32 *b32 = data;
1268 1.22 christos
1269 1.22 christos b32->index = buf->index;
1270 1.22 christos b32->type = buf->type;
1271 1.22 christos b32->bytesused = buf->bytesused;
1272 1.22 christos b32->flags = buf->flags;
1273 1.22 christos b32->field = buf->field;
1274 1.22 christos b32->timestamp.tv_sec = (uint32_t)buf->timestamp.tv_sec;
1275 1.22 christos b32->timestamp.tv_usec = buf->timestamp.tv_usec;
1276 1.22 christos b32->timecode = buf->timecode;
1277 1.22 christos b32->sequence = buf->sequence;
1278 1.22 christos b32->memory = buf->memory;
1279 1.22 christos b32->m.offset = buf->m.offset;
1280 1.22 christos /* XXX: Handle userptr */
1281 1.22 christos b32->length = buf->length;
1282 1.22 christos b32->input = buf->input;
1283 1.22 christos b32->reserved = buf->reserved;
1284 1.22 christos }
1285 1.22 christos
1286 1.1 jmcneill int
1287 1.1 jmcneill videoioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
1288 1.1 jmcneill {
1289 1.1 jmcneill struct video_softc *sc;
1290 1.1 jmcneill const struct video_hw_if *hw;
1291 1.1 jmcneill struct v4l2_capability *cap;
1292 1.1 jmcneill struct v4l2_fmtdesc *fmtdesc;
1293 1.1 jmcneill struct v4l2_format *fmt;
1294 1.1 jmcneill struct v4l2_standard *std;
1295 1.1 jmcneill struct v4l2_input *input;
1296 1.1 jmcneill struct v4l2_control *control;
1297 1.1 jmcneill struct v4l2_queryctrl *query;
1298 1.1 jmcneill struct v4l2_requestbuffers *reqbufs;
1299 1.22 christos struct v4l2_buffer *buf, bufspace;
1300 1.1 jmcneill v4l2_std_id *stdid;
1301 1.1 jmcneill enum v4l2_buf_type *typep;
1302 1.22 christos int *ip, error;
1303 1.1 jmcneill
1304 1.1 jmcneill sc = device_private(device_lookup(&video_cd, VIDEOUNIT(dev)));
1305 1.1 jmcneill
1306 1.1 jmcneill if (sc->sc_dying)
1307 1.1 jmcneill return EIO;
1308 1.1 jmcneill
1309 1.1 jmcneill hw = sc->hw_if;
1310 1.1 jmcneill if (hw == NULL)
1311 1.1 jmcneill return ENXIO;
1312 1.1 jmcneill
1313 1.1 jmcneill switch (cmd) {
1314 1.1 jmcneill case VIDIOC_QUERYCAP:
1315 1.1 jmcneill cap = data;
1316 1.1 jmcneill memset(cap, 0, sizeof(*cap));
1317 1.10 jmcneill strlcpy(cap->driver, device_xname(sc->hw_dev),
1318 1.1 jmcneill sizeof(cap->driver));
1319 1.1 jmcneill strlcpy(cap->card, hw->get_devname(sc->hw_softc),
1320 1.1 jmcneill sizeof(cap->card));
1321 1.1 jmcneill /* FIXME: bus_info is wrongly hardcoded to USB */
1322 1.1 jmcneill strlcpy(cap->bus_info, "USB", sizeof(cap->bus_info));
1323 1.1 jmcneill cap->version = VIDEO_DRIVER_VERSION;
1324 1.1 jmcneill cap->capabilities = 0;
1325 1.1 jmcneill if (hw->start_transfer != NULL && hw->stop_transfer != NULL)
1326 1.1 jmcneill cap->capabilities |= V4L2_CAP_VIDEO_CAPTURE |
1327 1.1 jmcneill V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1328 1.1 jmcneill return 0;
1329 1.1 jmcneill case VIDIOC_ENUM_FMT:
1330 1.1 jmcneill /* TODO: for now, just enumerate one default format */
1331 1.1 jmcneill fmtdesc = data;
1332 1.1 jmcneill if (fmtdesc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1333 1.1 jmcneill return EINVAL;
1334 1.1 jmcneill return video_enum_format(sc, fmtdesc);
1335 1.1 jmcneill case VIDIOC_G_FMT:
1336 1.1 jmcneill fmt = data;
1337 1.1 jmcneill return (video_get_format(sc, fmt));
1338 1.1 jmcneill case VIDIOC_S_FMT:
1339 1.1 jmcneill fmt = data;
1340 1.1 jmcneill if ((flag & FWRITE) == 0)
1341 1.1 jmcneill return EPERM;
1342 1.1 jmcneill return video_set_format(sc, fmt);
1343 1.1 jmcneill case VIDIOC_TRY_FMT:
1344 1.1 jmcneill fmt = data;
1345 1.1 jmcneill return (video_try_format(sc, fmt));
1346 1.1 jmcneill case VIDIOC_ENUMSTD:
1347 1.1 jmcneill /* TODO: implement properly */
1348 1.1 jmcneill std = data;
1349 1.1 jmcneill if (std->index != 0)
1350 1.1 jmcneill return EINVAL;
1351 1.1 jmcneill std->id = V4L2_STD_UNKNOWN;
1352 1.1 jmcneill strlcpy(std->name, "webcam", sizeof(std->name));
1353 1.1 jmcneill return 0;
1354 1.1 jmcneill case VIDIOC_G_STD:
1355 1.1 jmcneill /* TODO: implement properly */
1356 1.1 jmcneill stdid = data;
1357 1.1 jmcneill *stdid = V4L2_STD_UNKNOWN;
1358 1.1 jmcneill return 0;
1359 1.1 jmcneill case VIDIOC_S_STD:
1360 1.1 jmcneill /* TODO: implement properly */
1361 1.1 jmcneill stdid = data;
1362 1.1 jmcneill if (*stdid != V4L2_STD_UNKNOWN)
1363 1.1 jmcneill return EINVAL;
1364 1.1 jmcneill return 0;
1365 1.1 jmcneill case VIDIOC_ENUMINPUT:
1366 1.1 jmcneill /* TODO: implement properly */
1367 1.1 jmcneill input = data;
1368 1.1 jmcneill if (input->index != 0)
1369 1.1 jmcneill return EINVAL;
1370 1.1 jmcneill memset(input, 0, sizeof(*input));
1371 1.1 jmcneill input->index = 0;
1372 1.1 jmcneill strlcpy(input->name, "Camera", sizeof(input->name));
1373 1.1 jmcneill input->type = V4L2_INPUT_TYPE_CAMERA;
1374 1.1 jmcneill return 0;
1375 1.1 jmcneill case VIDIOC_G_INPUT:
1376 1.1 jmcneill /* TODO: implement properly */
1377 1.1 jmcneill ip = data;
1378 1.1 jmcneill *ip = 0;
1379 1.1 jmcneill return 0;
1380 1.1 jmcneill case VIDIOC_S_INPUT:
1381 1.1 jmcneill /* TODO: implement properly */
1382 1.1 jmcneill ip = data;
1383 1.1 jmcneill if (*ip != 0)
1384 1.1 jmcneill return EINVAL;
1385 1.1 jmcneill return 0;
1386 1.1 jmcneill case VIDIOC_QUERYCTRL:
1387 1.1 jmcneill query = data;
1388 1.1 jmcneill return (video_query_control(sc, query));
1389 1.1 jmcneill case VIDIOC_G_CTRL:
1390 1.1 jmcneill control = data;
1391 1.1 jmcneill return (video_get_control(sc, control));
1392 1.1 jmcneill case VIDIOC_S_CTRL:
1393 1.1 jmcneill control = data;
1394 1.1 jmcneill if ((flag & FWRITE) == 0)
1395 1.1 jmcneill return EPERM;
1396 1.1 jmcneill return (video_set_control(sc, control));
1397 1.1 jmcneill case VIDIOC_REQBUFS:
1398 1.1 jmcneill reqbufs = data;
1399 1.1 jmcneill return (video_request_bufs(sc, reqbufs));
1400 1.1 jmcneill case VIDIOC_QUERYBUF:
1401 1.1 jmcneill buf = data;
1402 1.1 jmcneill return video_query_buf(sc, buf);
1403 1.22 christos case VIDIOC_QUERYBUF32:
1404 1.22 christos buf32tobuf(data, buf = &bufspace);
1405 1.22 christos if ((error = video_query_buf(sc, buf)) != 0)
1406 1.22 christos return error;
1407 1.22 christos buftobuf32(data, buf);
1408 1.22 christos return 0;
1409 1.1 jmcneill case VIDIOC_QBUF:
1410 1.1 jmcneill buf = data;
1411 1.1 jmcneill return video_queue_buf(sc, buf);
1412 1.22 christos case VIDIOC_QBUF32:
1413 1.22 christos buf32tobuf(data, buf = &bufspace);
1414 1.22 christos return video_queue_buf(sc, buf);
1415 1.1 jmcneill case VIDIOC_DQBUF:
1416 1.1 jmcneill buf = data;
1417 1.1 jmcneill return video_dequeue_buf(sc, buf);
1418 1.22 christos case VIDIOC_DQBUF32:
1419 1.22 christos buf32tobuf(data, buf = &bufspace);
1420 1.22 christos if ((error = video_dequeue_buf(sc, buf)) != 0)
1421 1.22 christos return error;
1422 1.22 christos buftobuf32(data, buf);
1423 1.22 christos return 0;
1424 1.1 jmcneill case VIDIOC_STREAMON:
1425 1.1 jmcneill typep = data;
1426 1.1 jmcneill return video_stream_on(sc, *typep);
1427 1.1 jmcneill case VIDIOC_STREAMOFF:
1428 1.1 jmcneill typep = data;
1429 1.1 jmcneill return video_stream_off(sc, *typep);
1430 1.1 jmcneill default:
1431 1.1 jmcneill DPRINTF(("videoioctl: invalid cmd %s (%lx)\n",
1432 1.1 jmcneill video_ioctl_str(cmd), cmd));
1433 1.4 jmcneill return EINVAL;
1434 1.1 jmcneill }
1435 1.1 jmcneill }
1436 1.1 jmcneill
1437 1.1 jmcneill #ifdef VIDEO_DEBUG
1438 1.1 jmcneill static const char *
1439 1.1 jmcneill video_ioctl_str(u_long cmd)
1440 1.1 jmcneill {
1441 1.1 jmcneill const char *str;
1442 1.1 jmcneill
1443 1.1 jmcneill switch (cmd) {
1444 1.1 jmcneill case VIDIOC_QUERYCAP:
1445 1.1 jmcneill str = "VIDIOC_QUERYCAP";
1446 1.1 jmcneill break;
1447 1.1 jmcneill case VIDIOC_RESERVED:
1448 1.1 jmcneill str = "VIDIOC_RESERVED";
1449 1.1 jmcneill break;
1450 1.1 jmcneill case VIDIOC_ENUM_FMT:
1451 1.1 jmcneill str = "VIDIOC_ENUM_FMT";
1452 1.1 jmcneill break;
1453 1.1 jmcneill case VIDIOC_G_FMT:
1454 1.1 jmcneill str = "VIDIOC_G_FMT";
1455 1.1 jmcneill break;
1456 1.1 jmcneill case VIDIOC_S_FMT:
1457 1.1 jmcneill str = "VIDIOC_S_FMT";
1458 1.1 jmcneill break;
1459 1.1 jmcneill /* 6 and 7 are VIDIOC_[SG]_COMP, which are unsupported */
1460 1.1 jmcneill case VIDIOC_REQBUFS:
1461 1.1 jmcneill str = "VIDIOC_REQBUFS";
1462 1.1 jmcneill break;
1463 1.1 jmcneill case VIDIOC_QUERYBUF:
1464 1.1 jmcneill str = "VIDIOC_QUERYBUF";
1465 1.1 jmcneill break;
1466 1.22 christos case VIDIOC_QUERYBUF32:
1467 1.22 christos str = "VIDIOC_QUERYBUF32";
1468 1.22 christos break;
1469 1.1 jmcneill case VIDIOC_G_FBUF:
1470 1.1 jmcneill str = "VIDIOC_G_FBUF";
1471 1.1 jmcneill break;
1472 1.1 jmcneill case VIDIOC_S_FBUF:
1473 1.1 jmcneill str = "VIDIOC_S_FBUF";
1474 1.1 jmcneill break;
1475 1.1 jmcneill case VIDIOC_OVERLAY:
1476 1.1 jmcneill str = "VIDIOC_OVERLAY";
1477 1.1 jmcneill break;
1478 1.1 jmcneill case VIDIOC_QBUF:
1479 1.1 jmcneill str = "VIDIOC_QBUF";
1480 1.1 jmcneill break;
1481 1.22 christos case VIDIOC_QBUF32:
1482 1.22 christos str = "VIDIOC_QBUF32";
1483 1.22 christos break;
1484 1.1 jmcneill case VIDIOC_DQBUF:
1485 1.1 jmcneill str = "VIDIOC_DQBUF";
1486 1.1 jmcneill break;
1487 1.22 christos case VIDIOC_DQBUF32:
1488 1.22 christos str = "VIDIOC_DQBUF32";
1489 1.22 christos break;
1490 1.1 jmcneill case VIDIOC_STREAMON:
1491 1.1 jmcneill str = "VIDIOC_STREAMON";
1492 1.1 jmcneill break;
1493 1.1 jmcneill case VIDIOC_STREAMOFF:
1494 1.1 jmcneill str = "VIDIOC_STREAMOFF";
1495 1.1 jmcneill break;
1496 1.1 jmcneill case VIDIOC_G_PARM:
1497 1.1 jmcneill str = "VIDIOC_G_PARAM";
1498 1.1 jmcneill break;
1499 1.1 jmcneill case VIDIOC_S_PARM:
1500 1.1 jmcneill str = "VIDIOC_S_PARAM";
1501 1.1 jmcneill break;
1502 1.1 jmcneill case VIDIOC_G_STD:
1503 1.1 jmcneill str = "VIDIOC_G_STD";
1504 1.1 jmcneill break;
1505 1.1 jmcneill case VIDIOC_S_STD:
1506 1.1 jmcneill str = "VIDIOC_S_STD";
1507 1.1 jmcneill break;
1508 1.1 jmcneill case VIDIOC_ENUMSTD:
1509 1.1 jmcneill str = "VIDIOC_ENUMSTD";
1510 1.1 jmcneill break;
1511 1.1 jmcneill case VIDIOC_ENUMINPUT:
1512 1.1 jmcneill str = "VIDIOC_ENUMINPUT";
1513 1.1 jmcneill break;
1514 1.1 jmcneill case VIDIOC_G_CTRL:
1515 1.1 jmcneill str = "VIDIOC_G_CTRL";
1516 1.1 jmcneill break;
1517 1.1 jmcneill case VIDIOC_S_CTRL:
1518 1.1 jmcneill str = "VIDIOC_S_CTRL";
1519 1.1 jmcneill break;
1520 1.1 jmcneill case VIDIOC_G_TUNER:
1521 1.1 jmcneill str = "VIDIOC_G_TUNER";
1522 1.1 jmcneill break;
1523 1.1 jmcneill case VIDIOC_S_TUNER:
1524 1.1 jmcneill str = "VIDIOC_S_TUNER";
1525 1.1 jmcneill break;
1526 1.1 jmcneill case VIDIOC_G_AUDIO:
1527 1.1 jmcneill str = "VIDIOC_G_AUDIO";
1528 1.1 jmcneill break;
1529 1.1 jmcneill case VIDIOC_S_AUDIO:
1530 1.1 jmcneill str = "VIDIOC_S_AUDIO";
1531 1.1 jmcneill break;
1532 1.1 jmcneill case VIDIOC_QUERYCTRL:
1533 1.1 jmcneill str = "VIDIOC_QUERYCTRL";
1534 1.1 jmcneill break;
1535 1.1 jmcneill case VIDIOC_QUERYMENU:
1536 1.1 jmcneill str = "VIDIOC_QUERYMENU";
1537 1.1 jmcneill break;
1538 1.1 jmcneill case VIDIOC_G_INPUT:
1539 1.1 jmcneill str = "VIDIOC_G_INPUT";
1540 1.1 jmcneill break;
1541 1.1 jmcneill case VIDIOC_S_INPUT:
1542 1.1 jmcneill str = "VIDIOC_S_INPUT";
1543 1.1 jmcneill break;
1544 1.1 jmcneill case VIDIOC_G_OUTPUT:
1545 1.1 jmcneill str = "VIDIOC_G_OUTPUT";
1546 1.1 jmcneill break;
1547 1.1 jmcneill case VIDIOC_S_OUTPUT:
1548 1.1 jmcneill str = "VIDIOC_S_OUTPUT";
1549 1.1 jmcneill break;
1550 1.1 jmcneill case VIDIOC_ENUMOUTPUT:
1551 1.1 jmcneill str = "VIDIOC_ENUMOUTPUT";
1552 1.1 jmcneill break;
1553 1.1 jmcneill case VIDIOC_G_AUDOUT:
1554 1.1 jmcneill str = "VIDIOC_G_AUDOUT";
1555 1.1 jmcneill break;
1556 1.1 jmcneill case VIDIOC_S_AUDOUT:
1557 1.1 jmcneill str = "VIDIOC_S_AUDOUT";
1558 1.1 jmcneill break;
1559 1.1 jmcneill case VIDIOC_G_MODULATOR:
1560 1.1 jmcneill str = "VIDIOC_G_MODULATOR";
1561 1.1 jmcneill break;
1562 1.1 jmcneill case VIDIOC_S_MODULATOR:
1563 1.1 jmcneill str = "VIDIOC_S_MODULATOR";
1564 1.1 jmcneill break;
1565 1.1 jmcneill case VIDIOC_G_FREQUENCY:
1566 1.1 jmcneill str = "VIDIOC_G_FREQUENCY";
1567 1.1 jmcneill break;
1568 1.1 jmcneill case VIDIOC_S_FREQUENCY:
1569 1.1 jmcneill str = "VIDIOC_S_FREQUENCY";
1570 1.1 jmcneill break;
1571 1.1 jmcneill case VIDIOC_CROPCAP:
1572 1.1 jmcneill str = "VIDIOC_CROPCAP";
1573 1.1 jmcneill break;
1574 1.1 jmcneill case VIDIOC_G_CROP:
1575 1.1 jmcneill str = "VIDIOC_G_CROP";
1576 1.1 jmcneill break;
1577 1.1 jmcneill case VIDIOC_S_CROP:
1578 1.1 jmcneill str = "VIDIOC_S_CROP";
1579 1.1 jmcneill break;
1580 1.1 jmcneill case VIDIOC_G_JPEGCOMP:
1581 1.1 jmcneill str = "VIDIOC_G_JPEGCOMP";
1582 1.1 jmcneill break;
1583 1.1 jmcneill case VIDIOC_S_JPEGCOMP:
1584 1.1 jmcneill str = "VIDIOC_S_JPEGCOMP";
1585 1.1 jmcneill break;
1586 1.1 jmcneill case VIDIOC_QUERYSTD:
1587 1.1 jmcneill str = "VIDIOC_QUERYSTD";
1588 1.1 jmcneill break;
1589 1.1 jmcneill case VIDIOC_TRY_FMT:
1590 1.1 jmcneill str = "VIDIOC_TRY_FMT";
1591 1.1 jmcneill break;
1592 1.1 jmcneill case VIDIOC_ENUMAUDIO:
1593 1.1 jmcneill str = "VIDIOC_ENUMAUDIO";
1594 1.1 jmcneill break;
1595 1.1 jmcneill case VIDIOC_ENUMAUDOUT:
1596 1.1 jmcneill str = "VIDIOC_ENUMAUDOUT";
1597 1.1 jmcneill break;
1598 1.1 jmcneill case VIDIOC_G_PRIORITY:
1599 1.1 jmcneill str = "VIDIOC_G_PRIORITY";
1600 1.1 jmcneill break;
1601 1.1 jmcneill case VIDIOC_S_PRIORITY:
1602 1.1 jmcneill str = "VIDIOC_S_PRIORITY";
1603 1.1 jmcneill break;
1604 1.1 jmcneill default:
1605 1.1 jmcneill str = "unknown";
1606 1.1 jmcneill break;
1607 1.1 jmcneill }
1608 1.1 jmcneill return str;
1609 1.1 jmcneill }
1610 1.1 jmcneill #endif
1611 1.1 jmcneill
1612 1.1 jmcneill
1613 1.1 jmcneill int
1614 1.1 jmcneill videopoll(dev_t dev, int events, struct lwp *l)
1615 1.1 jmcneill {
1616 1.1 jmcneill struct video_softc *sc;
1617 1.1 jmcneill struct video_stream *vs;
1618 1.1 jmcneill int err, revents = 0;
1619 1.1 jmcneill
1620 1.1 jmcneill sc = device_private(device_lookup(&video_cd, VIDEOUNIT(dev)));
1621 1.1 jmcneill vs = &sc->sc_stream_in;
1622 1.1 jmcneill
1623 1.1 jmcneill if (sc->sc_dying)
1624 1.1 jmcneill return (POLLHUP);
1625 1.1 jmcneill
1626 1.1 jmcneill /* userspace has chosen read() method */
1627 1.1 jmcneill if (vs->vs_method == VIDEO_STREAM_METHOD_NONE) {
1628 1.1 jmcneill err = video_stream_setup_bufs(vs,
1629 1.1 jmcneill VIDEO_STREAM_METHOD_READ,
1630 1.1 jmcneill VIDEO_NUM_BUFS);
1631 1.1 jmcneill if (err != 0)
1632 1.1 jmcneill return POLLERR;
1633 1.1 jmcneill
1634 1.1 jmcneill err = video_stream_on(sc, vs->vs_type);
1635 1.1 jmcneill if (err != 0)
1636 1.1 jmcneill return POLLERR;
1637 1.1 jmcneill }
1638 1.1 jmcneill
1639 1.19 drochner mutex_enter(&vs->vs_lock);
1640 1.1 jmcneill if (!SIMPLEQ_EMPTY(&sc->sc_stream_in.vs_egress))
1641 1.1 jmcneill revents |= events & (POLLIN | POLLRDNORM);
1642 1.7 jmcneill else
1643 1.7 jmcneill selrecord(l, &vs->vs_sel);
1644 1.19 drochner mutex_exit(&vs->vs_lock);
1645 1.1 jmcneill
1646 1.1 jmcneill return (revents);
1647 1.1 jmcneill }
1648 1.1 jmcneill
1649 1.1 jmcneill
1650 1.1 jmcneill paddr_t
1651 1.1 jmcneill videommap(dev_t dev, off_t off, int prot)
1652 1.1 jmcneill {
1653 1.1 jmcneill struct video_softc *sc;
1654 1.1 jmcneill struct video_stream *vs;
1655 1.1 jmcneill /* paddr_t pa; */
1656 1.1 jmcneill
1657 1.1 jmcneill sc = device_lookup_private(&video_cd, VIDEOUNIT(dev));
1658 1.1 jmcneill if (sc->sc_dying)
1659 1.1 jmcneill return -1;
1660 1.1 jmcneill
1661 1.1 jmcneill vs = &sc->sc_stream_in;
1662 1.1 jmcneill
1663 1.1 jmcneill return scatter_buf_map(&vs->vs_data, off);
1664 1.1 jmcneill }
1665 1.1 jmcneill
1666 1.1 jmcneill
1667 1.1 jmcneill /* Allocates buffers and initizlizes some fields. The format field
1668 1.1 jmcneill * must already have been initialized. */
1669 1.1 jmcneill void
1670 1.1 jmcneill video_stream_init(struct video_stream *vs)
1671 1.1 jmcneill {
1672 1.1 jmcneill vs->vs_method = VIDEO_STREAM_METHOD_NONE;
1673 1.1 jmcneill vs->vs_flags = 0;
1674 1.1 jmcneill vs->vs_frameno = -1;
1675 1.1 jmcneill vs->vs_sequence = 0;
1676 1.1 jmcneill vs->vs_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1677 1.1 jmcneill vs->vs_nbufs = 0;
1678 1.1 jmcneill vs->vs_buf = NULL;
1679 1.1 jmcneill vs->vs_streaming = false;
1680 1.1 jmcneill
1681 1.1 jmcneill memset(&vs->vs_format, 0, sizeof(vs->vs_format));
1682 1.1 jmcneill
1683 1.1 jmcneill SIMPLEQ_INIT(&vs->vs_ingress);
1684 1.1 jmcneill SIMPLEQ_INIT(&vs->vs_egress);
1685 1.1 jmcneill
1686 1.1 jmcneill mutex_init(&vs->vs_lock, MUTEX_DEFAULT, IPL_NONE);
1687 1.1 jmcneill cv_init(&vs->vs_sample_cv, "video");
1688 1.7 jmcneill selinit(&vs->vs_sel);
1689 1.1 jmcneill
1690 1.1 jmcneill scatter_buf_init(&vs->vs_data);
1691 1.1 jmcneill }
1692 1.1 jmcneill
1693 1.1 jmcneill void
1694 1.1 jmcneill video_stream_fini(struct video_stream *vs)
1695 1.1 jmcneill {
1696 1.1 jmcneill /* Sample data in queues has already been freed */
1697 1.1 jmcneill /* while (SIMPLEQ_FIRST(&vs->vs_ingress) != NULL)
1698 1.1 jmcneill SIMPLEQ_REMOVE_HEAD(&vs->vs_ingress, entries);
1699 1.1 jmcneill while (SIMPLEQ_FIRST(&vs->vs_egress) != NULL)
1700 1.1 jmcneill SIMPLEQ_REMOVE_HEAD(&vs->vs_egress, entries); */
1701 1.1 jmcneill
1702 1.1 jmcneill mutex_destroy(&vs->vs_lock);
1703 1.1 jmcneill cv_destroy(&vs->vs_sample_cv);
1704 1.7 jmcneill seldestroy(&vs->vs_sel);
1705 1.1 jmcneill
1706 1.1 jmcneill scatter_buf_destroy(&vs->vs_data);
1707 1.1 jmcneill }
1708 1.1 jmcneill
1709 1.1 jmcneill static int
1710 1.1 jmcneill video_stream_setup_bufs(struct video_stream *vs,
1711 1.1 jmcneill enum video_stream_method method,
1712 1.1 jmcneill uint8_t nbufs)
1713 1.1 jmcneill {
1714 1.1 jmcneill int i, err;
1715 1.1 jmcneill
1716 1.1 jmcneill mutex_enter(&vs->vs_lock);
1717 1.1 jmcneill
1718 1.1 jmcneill /* Ensure that all allocated buffers are queued and not under
1719 1.1 jmcneill * userspace control. */
1720 1.1 jmcneill for (i = 0; i < vs->vs_nbufs; ++i) {
1721 1.19 drochner if (!(vs->vs_buf[i]->vb_buf->flags & V4L2_BUF_FLAG_QUEUED)) {
1722 1.1 jmcneill mutex_exit(&vs->vs_lock);
1723 1.1 jmcneill return EBUSY;
1724 1.1 jmcneill }
1725 1.1 jmcneill }
1726 1.1 jmcneill
1727 1.1 jmcneill /* Allocate the buffers */
1728 1.1 jmcneill err = video_stream_realloc_bufs(vs, nbufs);
1729 1.1 jmcneill if (err != 0) {
1730 1.1 jmcneill mutex_exit(&vs->vs_lock);
1731 1.1 jmcneill return err;
1732 1.1 jmcneill }
1733 1.1 jmcneill
1734 1.1 jmcneill /* Queue up buffers for read method. Other methods are queued
1735 1.1 jmcneill * by VIDIOC_QBUF ioctl. */
1736 1.1 jmcneill if (method == VIDEO_STREAM_METHOD_READ) {
1737 1.1 jmcneill for (i = 0; i < nbufs; ++i)
1738 1.1 jmcneill if (!(vs->vs_buf[i]->vb_buf->flags & V4L2_BUF_FLAG_QUEUED))
1739 1.1 jmcneill video_stream_enqueue(vs, vs->vs_buf[i]);
1740 1.1 jmcneill }
1741 1.1 jmcneill
1742 1.1 jmcneill vs->vs_method = method;
1743 1.1 jmcneill mutex_exit(&vs->vs_lock);
1744 1.1 jmcneill
1745 1.1 jmcneill return 0;
1746 1.1 jmcneill }
1747 1.1 jmcneill
1748 1.1 jmcneill /* Free all buffer memory in preparation for close(). This should
1749 1.1 jmcneill * free buffers regardless of errors. Use video_stream_setup_bufs if
1750 1.1 jmcneill * you need to check for errors. Streaming should be off before
1751 1.1 jmcneill * calling this function. */
1752 1.1 jmcneill static void
1753 1.1 jmcneill video_stream_teardown_bufs(struct video_stream *vs)
1754 1.1 jmcneill {
1755 1.1 jmcneill int err;
1756 1.1 jmcneill
1757 1.1 jmcneill mutex_enter(&vs->vs_lock);
1758 1.1 jmcneill
1759 1.2 rmind if (vs->vs_streaming) {
1760 1.1 jmcneill DPRINTF(("video_stream_teardown_bufs: "
1761 1.1 jmcneill "tearing down bufs while streaming\n"));
1762 1.2 rmind }
1763 1.1 jmcneill
1764 1.1 jmcneill /* dequeue all buffers */
1765 1.1 jmcneill while (SIMPLEQ_FIRST(&vs->vs_ingress) != NULL)
1766 1.1 jmcneill SIMPLEQ_REMOVE_HEAD(&vs->vs_ingress, entries);
1767 1.1 jmcneill while (SIMPLEQ_FIRST(&vs->vs_egress) != NULL)
1768 1.1 jmcneill SIMPLEQ_REMOVE_HEAD(&vs->vs_egress, entries);
1769 1.1 jmcneill
1770 1.1 jmcneill err = video_stream_free_bufs(vs);
1771 1.2 rmind if (err != 0) {
1772 1.1 jmcneill DPRINTF(("video_stream_teardown_bufs: "
1773 1.1 jmcneill "error releasing buffers: %d\n",
1774 1.1 jmcneill err));
1775 1.2 rmind }
1776 1.1 jmcneill vs->vs_method = VIDEO_STREAM_METHOD_NONE;
1777 1.1 jmcneill
1778 1.1 jmcneill mutex_exit(&vs->vs_lock);
1779 1.1 jmcneill }
1780 1.1 jmcneill
1781 1.1 jmcneill static struct video_buffer *
1782 1.1 jmcneill video_buffer_alloc(void)
1783 1.1 jmcneill {
1784 1.1 jmcneill struct video_buffer *vb;
1785 1.1 jmcneill
1786 1.1 jmcneill vb = kmem_alloc(sizeof(*vb), KM_SLEEP);
1787 1.1 jmcneill if (vb == NULL)
1788 1.1 jmcneill return NULL;
1789 1.1 jmcneill
1790 1.1 jmcneill vb->vb_buf = kmem_alloc(sizeof(*vb->vb_buf), KM_SLEEP);
1791 1.1 jmcneill if (vb->vb_buf == NULL) {
1792 1.1 jmcneill kmem_free(vb, sizeof(*vb));
1793 1.1 jmcneill return NULL;
1794 1.1 jmcneill }
1795 1.1 jmcneill
1796 1.1 jmcneill return vb;
1797 1.1 jmcneill }
1798 1.1 jmcneill
1799 1.1 jmcneill static void
1800 1.1 jmcneill video_buffer_free(struct video_buffer *vb)
1801 1.1 jmcneill {
1802 1.1 jmcneill kmem_free(vb->vb_buf, sizeof(*vb->vb_buf));
1803 1.1 jmcneill vb->vb_buf = NULL;
1804 1.1 jmcneill kmem_free(vb, sizeof(*vb));
1805 1.1 jmcneill }
1806 1.1 jmcneill
1807 1.1 jmcneill /* TODO: for userptr method
1808 1.1 jmcneill struct video_buffer *
1809 1.1 jmcneill video_buf_alloc_with_ubuf(struct v4l2_buffer *buf)
1810 1.1 jmcneill {
1811 1.1 jmcneill }
1812 1.1 jmcneill
1813 1.1 jmcneill void
1814 1.1 jmcneill video_buffer_free_with_ubuf(struct video_buffer *vb)
1815 1.1 jmcneill {
1816 1.1 jmcneill }
1817 1.1 jmcneill */
1818 1.1 jmcneill
1819 1.1 jmcneill static int
1820 1.1 jmcneill video_stream_realloc_bufs(struct video_stream *vs, uint8_t nbufs)
1821 1.1 jmcneill {
1822 1.1 jmcneill int i, err;
1823 1.1 jmcneill uint8_t minnbufs, oldnbufs;
1824 1.1 jmcneill size_t size;
1825 1.1 jmcneill off_t offset;
1826 1.1 jmcneill struct video_buffer **oldbuf;
1827 1.1 jmcneill struct v4l2_buffer *buf;
1828 1.1 jmcneill
1829 1.22 christos size = PAGE_ALIGN(vs->vs_format.sample_size) * nbufs;
1830 1.1 jmcneill err = scatter_buf_set_size(&vs->vs_data, size);
1831 1.1 jmcneill if (err != 0)
1832 1.1 jmcneill return err;
1833 1.1 jmcneill
1834 1.1 jmcneill oldnbufs = vs->vs_nbufs;
1835 1.1 jmcneill oldbuf = vs->vs_buf;
1836 1.1 jmcneill
1837 1.1 jmcneill vs->vs_nbufs = nbufs;
1838 1.1 jmcneill if (nbufs > 0) {
1839 1.1 jmcneill vs->vs_buf =
1840 1.1 jmcneill kmem_alloc(sizeof(struct video_buffer *) * nbufs, KM_SLEEP);
1841 1.1 jmcneill if (vs->vs_buf == NULL) {
1842 1.1 jmcneill vs->vs_nbufs = oldnbufs;
1843 1.1 jmcneill vs->vs_buf = oldbuf;
1844 1.1 jmcneill
1845 1.1 jmcneill return ENOMEM;
1846 1.1 jmcneill }
1847 1.1 jmcneill } else {
1848 1.1 jmcneill vs->vs_buf = NULL;
1849 1.1 jmcneill }
1850 1.1 jmcneill
1851 1.1 jmcneill minnbufs = min(vs->vs_nbufs, oldnbufs);
1852 1.1 jmcneill /* copy any bufs that will be reused */
1853 1.1 jmcneill for (i = 0; i < minnbufs; ++i)
1854 1.1 jmcneill vs->vs_buf[i] = oldbuf[i];
1855 1.1 jmcneill /* allocate any necessary new bufs */
1856 1.1 jmcneill for (; i < vs->vs_nbufs; ++i)
1857 1.1 jmcneill vs->vs_buf[i] = video_buffer_alloc();
1858 1.1 jmcneill /* free any bufs no longer used */
1859 1.1 jmcneill for (; i < oldnbufs; ++i) {
1860 1.1 jmcneill video_buffer_free(oldbuf[i]);
1861 1.1 jmcneill oldbuf[i] = NULL;
1862 1.1 jmcneill }
1863 1.1 jmcneill
1864 1.1 jmcneill /* Free old buffer metadata */
1865 1.1 jmcneill if (oldbuf != NULL)
1866 1.1 jmcneill kmem_free(oldbuf, sizeof(struct video_buffer *) * oldnbufs);
1867 1.1 jmcneill
1868 1.1 jmcneill /* initialize bufs */
1869 1.1 jmcneill offset = 0;
1870 1.1 jmcneill for (i = 0; i < vs->vs_nbufs; ++i) {
1871 1.1 jmcneill buf = vs->vs_buf[i]->vb_buf;
1872 1.1 jmcneill buf->index = i;
1873 1.1 jmcneill buf->type = vs->vs_type;
1874 1.1 jmcneill buf->bytesused = 0;
1875 1.1 jmcneill buf->flags = 0;
1876 1.1 jmcneill buf->field = 0;
1877 1.1 jmcneill buf->sequence = 0;
1878 1.1 jmcneill buf->memory = V4L2_MEMORY_MMAP;
1879 1.1 jmcneill buf->m.offset = offset;
1880 1.22 christos buf->length = PAGE_ALIGN(vs->vs_format.sample_size);
1881 1.1 jmcneill buf->input = 0;
1882 1.1 jmcneill buf->reserved = 0;
1883 1.1 jmcneill
1884 1.1 jmcneill offset += buf->length;
1885 1.1 jmcneill }
1886 1.1 jmcneill
1887 1.1 jmcneill return 0;
1888 1.1 jmcneill }
1889 1.1 jmcneill
1890 1.1 jmcneill /* Accepts a video_sample into the ingress queue. Caller must hold
1891 1.1 jmcneill * the stream lock. */
1892 1.1 jmcneill void
1893 1.1 jmcneill video_stream_enqueue(struct video_stream *vs, struct video_buffer *vb)
1894 1.1 jmcneill {
1895 1.1 jmcneill if (vb->vb_buf->flags & V4L2_BUF_FLAG_QUEUED) {
1896 1.1 jmcneill DPRINTF(("video_stream_enqueue: sample already queued\n"));
1897 1.1 jmcneill return;
1898 1.1 jmcneill }
1899 1.1 jmcneill
1900 1.1 jmcneill vb->vb_buf->flags |= V4L2_BUF_FLAG_QUEUED;
1901 1.1 jmcneill vb->vb_buf->flags &= ~V4L2_BUF_FLAG_DONE;
1902 1.1 jmcneill
1903 1.1 jmcneill vb->vb_buf->bytesused = 0;
1904 1.1 jmcneill
1905 1.1 jmcneill SIMPLEQ_INSERT_TAIL(&vs->vs_ingress, vb, entries);
1906 1.1 jmcneill }
1907 1.1 jmcneill
1908 1.1 jmcneill
1909 1.1 jmcneill /* Removes the head of the egress queue for use by userspace. Caller
1910 1.1 jmcneill * must hold the stream lock. */
1911 1.1 jmcneill struct video_buffer *
1912 1.1 jmcneill video_stream_dequeue(struct video_stream *vs)
1913 1.1 jmcneill {
1914 1.1 jmcneill struct video_buffer *vb;
1915 1.1 jmcneill
1916 1.1 jmcneill if (!SIMPLEQ_EMPTY(&vs->vs_egress)) {
1917 1.1 jmcneill vb = SIMPLEQ_FIRST(&vs->vs_egress);
1918 1.1 jmcneill SIMPLEQ_REMOVE_HEAD(&vs->vs_egress, entries);
1919 1.1 jmcneill vb->vb_buf->flags &= ~V4L2_BUF_FLAG_QUEUED;
1920 1.1 jmcneill vb->vb_buf->flags |= V4L2_BUF_FLAG_DONE;
1921 1.1 jmcneill return vb;
1922 1.1 jmcneill } else {
1923 1.1 jmcneill return NULL;
1924 1.1 jmcneill }
1925 1.1 jmcneill }
1926 1.1 jmcneill
1927 1.1 jmcneill
1928 1.1 jmcneill /*
1929 1.1 jmcneill * write payload data to the appropriate video sample, possibly moving
1930 1.1 jmcneill * the sample from ingress to egress queues
1931 1.1 jmcneill */
1932 1.1 jmcneill void
1933 1.1 jmcneill video_stream_write(struct video_stream *vs,
1934 1.1 jmcneill const struct video_payload *payload)
1935 1.1 jmcneill {
1936 1.1 jmcneill struct video_buffer *vb;
1937 1.1 jmcneill struct v4l2_buffer *buf;
1938 1.1 jmcneill struct scatter_io sio;
1939 1.1 jmcneill
1940 1.1 jmcneill mutex_enter(&vs->vs_lock);
1941 1.1 jmcneill
1942 1.1 jmcneill /* change of frameno implies end of current frame */
1943 1.1 jmcneill if (vs->vs_frameno > 0 && vs->vs_frameno != payload->frameno)
1944 1.1 jmcneill video_stream_sample_done(vs);
1945 1.1 jmcneill
1946 1.1 jmcneill if (vs->vs_drop || SIMPLEQ_EMPTY(&vs->vs_ingress)) {
1947 1.1 jmcneill /* DPRINTF(("video_stream_write: dropping sample %d\n",
1948 1.1 jmcneill vs->vs_sequence)); */
1949 1.1 jmcneill vs->vs_drop = true;
1950 1.15 jmcneill } else if (payload->size > 0) {
1951 1.1 jmcneill vb = SIMPLEQ_FIRST(&vs->vs_ingress);
1952 1.1 jmcneill buf = vb->vb_buf;
1953 1.1 jmcneill if (payload->size > buf->length - buf->bytesused) {
1954 1.1 jmcneill DPRINTF(("video_stream_write: "
1955 1.1 jmcneill "payload would overflow\n"));
1956 1.1 jmcneill } else if (scatter_io_init(&vs->vs_data,
1957 1.1 jmcneill buf->m.offset + buf->bytesused,
1958 1.1 jmcneill payload->size,
1959 1.1 jmcneill &sio))
1960 1.1 jmcneill {
1961 1.1 jmcneill scatter_io_copyin(&sio, payload->data);
1962 1.1 jmcneill buf->bytesused += (payload->size - sio.sio_resid);
1963 1.1 jmcneill } else {
1964 1.1 jmcneill DPRINTF(("video_stream_write: failed to init scatter io "
1965 1.1 jmcneill "vb=%p buf=%p "
1966 1.21 njoly "buf->m.offset=%d buf->bytesused=%u "
1967 1.1 jmcneill "payload->size=%zu\n",
1968 1.1 jmcneill vb, buf,
1969 1.1 jmcneill buf->m.offset, buf->bytesused, payload->size));
1970 1.1 jmcneill }
1971 1.1 jmcneill }
1972 1.1 jmcneill
1973 1.1 jmcneill /* if the payload marks it, we can do sample_done() early */
1974 1.1 jmcneill if (payload->end_of_frame)
1975 1.1 jmcneill video_stream_sample_done(vs);
1976 1.1 jmcneill
1977 1.1 jmcneill mutex_exit(&vs->vs_lock);
1978 1.1 jmcneill }
1979 1.1 jmcneill
1980 1.1 jmcneill
1981 1.1 jmcneill /* Moves the head of the ingress queue to the tail of the egress
1982 1.1 jmcneill * queue, or resets drop status if we were dropping this sample.
1983 1.1 jmcneill * Caller should hold the stream queue lock. */
1984 1.1 jmcneill void
1985 1.1 jmcneill video_stream_sample_done(struct video_stream *vs)
1986 1.1 jmcneill {
1987 1.1 jmcneill struct video_buffer *vb;
1988 1.1 jmcneill
1989 1.1 jmcneill if (vs->vs_drop) {
1990 1.1 jmcneill vs->vs_drop = false;
1991 1.1 jmcneill } else if (!SIMPLEQ_EMPTY(&vs->vs_ingress)) {
1992 1.1 jmcneill vb = SIMPLEQ_FIRST(&vs->vs_ingress);
1993 1.1 jmcneill vb->vb_buf->sequence = vs->vs_sequence;
1994 1.1 jmcneill SIMPLEQ_REMOVE_HEAD(&vs->vs_ingress, entries);
1995 1.1 jmcneill
1996 1.1 jmcneill SIMPLEQ_INSERT_TAIL(&vs->vs_egress, vb, entries);
1997 1.1 jmcneill cv_signal(&vs->vs_sample_cv);
1998 1.7 jmcneill selnotify(&vs->vs_sel, 0, 0);
1999 1.1 jmcneill } else {
2000 1.1 jmcneill DPRINTF(("video_stream_sample_done: no sample\n"));
2001 1.1 jmcneill }
2002 1.1 jmcneill
2003 1.1 jmcneill vs->vs_frameno ^= 1;
2004 1.1 jmcneill vs->vs_sequence++;
2005 1.1 jmcneill }
2006 1.1 jmcneill
2007 1.1 jmcneill /* Check if all buffers are queued, i.e. none are under control of
2008 1.1 jmcneill * userspace. */
2009 1.1 jmcneill /*
2010 1.1 jmcneill static bool
2011 1.1 jmcneill video_stream_all_queued(struct video_stream *vs)
2012 1.1 jmcneill {
2013 1.1 jmcneill }
2014 1.1 jmcneill */
2015 1.1 jmcneill
2016 1.1 jmcneill
2017 1.1 jmcneill static void
2018 1.1 jmcneill scatter_buf_init(struct scatter_buf *sb)
2019 1.1 jmcneill {
2020 1.1 jmcneill sb->sb_pool = pool_cache_init(PAGE_SIZE, 0, 0, 0,
2021 1.1 jmcneill "video", NULL, IPL_VIDEO,
2022 1.1 jmcneill NULL, NULL, NULL);
2023 1.1 jmcneill sb->sb_size = 0;
2024 1.1 jmcneill sb->sb_npages = 0;
2025 1.1 jmcneill sb->sb_page_ary = NULL;
2026 1.1 jmcneill }
2027 1.1 jmcneill
2028 1.1 jmcneill static void
2029 1.1 jmcneill scatter_buf_destroy(struct scatter_buf *sb)
2030 1.1 jmcneill {
2031 1.1 jmcneill /* Do we need to return everything to the pool first? */
2032 1.1 jmcneill scatter_buf_set_size(sb, 0);
2033 1.1 jmcneill pool_cache_destroy(sb->sb_pool);
2034 1.1 jmcneill sb->sb_pool = 0;
2035 1.1 jmcneill sb->sb_npages = 0;
2036 1.1 jmcneill sb->sb_page_ary = NULL;
2037 1.1 jmcneill }
2038 1.1 jmcneill
2039 1.1 jmcneill /* Increase or decrease the size of the buffer */
2040 1.1 jmcneill static int
2041 1.1 jmcneill scatter_buf_set_size(struct scatter_buf *sb, size_t sz)
2042 1.1 jmcneill {
2043 1.1 jmcneill int i;
2044 1.1 jmcneill size_t npages, minpages, oldnpages;
2045 1.1 jmcneill uint8_t **old_ary;
2046 1.1 jmcneill
2047 1.1 jmcneill npages = (sz >> PAGE_SHIFT) + ((sz & PAGE_MASK) > 0);
2048 1.1 jmcneill
2049 1.1 jmcneill if (sb->sb_npages == npages) {
2050 1.1 jmcneill return 0;
2051 1.1 jmcneill }
2052 1.1 jmcneill
2053 1.1 jmcneill oldnpages = sb->sb_npages;
2054 1.1 jmcneill old_ary = sb->sb_page_ary;
2055 1.1 jmcneill
2056 1.1 jmcneill sb->sb_npages = npages;
2057 1.1 jmcneill if (npages > 0) {
2058 1.1 jmcneill sb->sb_page_ary =
2059 1.1 jmcneill kmem_alloc(sizeof(uint8_t *) * npages, KM_SLEEP);
2060 1.1 jmcneill if (sb->sb_page_ary == NULL) {
2061 1.1 jmcneill sb->sb_npages = oldnpages;
2062 1.1 jmcneill sb->sb_page_ary = old_ary;
2063 1.1 jmcneill return ENOMEM;
2064 1.1 jmcneill }
2065 1.1 jmcneill } else {
2066 1.1 jmcneill sb->sb_page_ary = NULL;
2067 1.1 jmcneill }
2068 1.1 jmcneill
2069 1.1 jmcneill minpages = min(npages, oldnpages);
2070 1.1 jmcneill /* copy any pages that will be reused */
2071 1.1 jmcneill for (i = 0; i < minpages; ++i)
2072 1.1 jmcneill sb->sb_page_ary[i] = old_ary[i];
2073 1.1 jmcneill /* allocate any new pages */
2074 1.1 jmcneill for (; i < npages; ++i) {
2075 1.1 jmcneill sb->sb_page_ary[i] = pool_cache_get(sb->sb_pool, 0);
2076 1.1 jmcneill /* TODO: does pool_cache_get return NULL on
2077 1.1 jmcneill * ENOMEM? If so, we need to release or note
2078 1.1 jmcneill * the pages with did allocate
2079 1.1 jmcneill * successfully. */
2080 1.1 jmcneill if (sb->sb_page_ary[i] == NULL) {
2081 1.1 jmcneill DPRINTF(("video: pool_cache_get ENOMEM\n"));
2082 1.1 jmcneill return ENOMEM;
2083 1.1 jmcneill }
2084 1.1 jmcneill }
2085 1.1 jmcneill /* return any pages no longer needed */
2086 1.1 jmcneill for (; i < oldnpages; ++i)
2087 1.1 jmcneill pool_cache_put(sb->sb_pool, old_ary[i]);
2088 1.1 jmcneill
2089 1.1 jmcneill if (old_ary != NULL)
2090 1.1 jmcneill kmem_free(old_ary, sizeof(uint8_t *) * oldnpages);
2091 1.1 jmcneill
2092 1.1 jmcneill sb->sb_size = sb->sb_npages << PAGE_SHIFT;
2093 1.1 jmcneill
2094 1.1 jmcneill return 0;
2095 1.1 jmcneill }
2096 1.1 jmcneill
2097 1.1 jmcneill
2098 1.1 jmcneill static paddr_t
2099 1.1 jmcneill scatter_buf_map(struct scatter_buf *sb, off_t off)
2100 1.1 jmcneill {
2101 1.1 jmcneill size_t pg;
2102 1.1 jmcneill paddr_t pa;
2103 1.1 jmcneill
2104 1.1 jmcneill pg = off >> PAGE_SHIFT;
2105 1.1 jmcneill
2106 1.1 jmcneill if (pg >= sb->sb_npages)
2107 1.1 jmcneill return -1;
2108 1.1 jmcneill else if (!pmap_extract(pmap_kernel(), (vaddr_t)sb->sb_page_ary[pg], &pa))
2109 1.1 jmcneill return -1;
2110 1.1 jmcneill
2111 1.1 jmcneill return atop(pa);
2112 1.1 jmcneill }
2113 1.1 jmcneill
2114 1.1 jmcneill /* Initialize data for an io operation on a scatter buffer. Returns
2115 1.1 jmcneill * true if the transfer is valid, or false if out of range. */
2116 1.1 jmcneill static bool
2117 1.1 jmcneill scatter_io_init(struct scatter_buf *sb,
2118 1.1 jmcneill off_t off, size_t len,
2119 1.1 jmcneill struct scatter_io *sio)
2120 1.1 jmcneill {
2121 1.1 jmcneill if ((off + len) > sb->sb_size) {
2122 1.1 jmcneill DPRINTF(("video: scatter_io_init failed: off=%" PRId64
2123 1.1 jmcneill " len=%zu sb->sb_size=%zu\n",
2124 1.1 jmcneill off, len, sb->sb_size));
2125 1.1 jmcneill return false;
2126 1.1 jmcneill }
2127 1.1 jmcneill
2128 1.1 jmcneill sio->sio_buf = sb;
2129 1.1 jmcneill sio->sio_offset = off;
2130 1.1 jmcneill sio->sio_resid = len;
2131 1.1 jmcneill
2132 1.1 jmcneill return true;
2133 1.1 jmcneill }
2134 1.1 jmcneill
2135 1.1 jmcneill /* Store the pointer and size of the next contiguous segment. Returns
2136 1.1 jmcneill * true if the segment is valid, or false if all has been transfered.
2137 1.1 jmcneill * Does not check for overflow. */
2138 1.1 jmcneill static bool
2139 1.1 jmcneill scatter_io_next(struct scatter_io *sio, void **p, size_t *sz)
2140 1.1 jmcneill {
2141 1.1 jmcneill size_t pg, pgo;
2142 1.1 jmcneill
2143 1.1 jmcneill if (sio->sio_resid == 0)
2144 1.1 jmcneill return false;
2145 1.1 jmcneill
2146 1.1 jmcneill pg = sio->sio_offset >> PAGE_SHIFT;
2147 1.1 jmcneill pgo = sio->sio_offset & PAGE_MASK;
2148 1.1 jmcneill
2149 1.1 jmcneill *sz = min(PAGE_SIZE - pgo, sio->sio_resid);
2150 1.1 jmcneill *p = sio->sio_buf->sb_page_ary[pg] + pgo;
2151 1.1 jmcneill
2152 1.1 jmcneill sio->sio_offset += *sz;
2153 1.1 jmcneill sio->sio_resid -= *sz;
2154 1.1 jmcneill
2155 1.1 jmcneill return true;
2156 1.1 jmcneill }
2157 1.1 jmcneill
2158 1.1 jmcneill /* Semi-undo of a failed segment copy. Updates the scatter_io
2159 1.1 jmcneill * struct to the previous values prior to a failed segment copy. */
2160 1.1 jmcneill static void
2161 1.1 jmcneill scatter_io_undo(struct scatter_io *sio, size_t sz)
2162 1.1 jmcneill {
2163 1.1 jmcneill sio->sio_offset -= sz;
2164 1.1 jmcneill sio->sio_resid += sz;
2165 1.1 jmcneill }
2166 1.1 jmcneill
2167 1.1 jmcneill /* Copy data from src into the scatter_buf as described by io. */
2168 1.1 jmcneill static void
2169 1.1 jmcneill scatter_io_copyin(struct scatter_io *sio, const void *p)
2170 1.1 jmcneill {
2171 1.1 jmcneill void *dst;
2172 1.1 jmcneill const uint8_t *src = p;
2173 1.1 jmcneill size_t sz;
2174 1.1 jmcneill
2175 1.1 jmcneill while(scatter_io_next(sio, &dst, &sz)) {
2176 1.1 jmcneill memcpy(dst, src, sz);
2177 1.1 jmcneill src += sz;
2178 1.1 jmcneill }
2179 1.1 jmcneill }
2180 1.1 jmcneill
2181 1.1 jmcneill /* --not used; commented to avoid compiler warnings--
2182 1.1 jmcneill static void
2183 1.1 jmcneill scatter_io_copyout(struct scatter_io *sio, void *p)
2184 1.1 jmcneill {
2185 1.1 jmcneill void *src;
2186 1.1 jmcneill uint8_t *dst = p;
2187 1.1 jmcneill size_t sz;
2188 1.1 jmcneill
2189 1.1 jmcneill while(scatter_io_next(sio, &src, &sz)) {
2190 1.1 jmcneill memcpy(dst, src, sz);
2191 1.1 jmcneill dst += sz;
2192 1.1 jmcneill }
2193 1.1 jmcneill }
2194 1.1 jmcneill */
2195 1.1 jmcneill
2196 1.1 jmcneill /* Performat a series of uiomove calls on a scatter buf. Returns
2197 1.1 jmcneill * EFAULT if uiomove EFAULTs on the first segment. Otherwise, returns
2198 1.1 jmcneill * an incomplete transfer but with no error. */
2199 1.1 jmcneill static int
2200 1.1 jmcneill scatter_io_uiomove(struct scatter_io *sio, struct uio *uio)
2201 1.1 jmcneill {
2202 1.1 jmcneill void *p;
2203 1.1 jmcneill size_t sz;
2204 1.1 jmcneill bool first = true;
2205 1.1 jmcneill int err;
2206 1.1 jmcneill
2207 1.1 jmcneill while(scatter_io_next(sio, &p, &sz)) {
2208 1.1 jmcneill err = uiomove(p, sz, uio);
2209 1.1 jmcneill if (err == EFAULT) {
2210 1.1 jmcneill scatter_io_undo(sio, sz);
2211 1.1 jmcneill if (first)
2212 1.1 jmcneill return EFAULT;
2213 1.1 jmcneill else
2214 1.1 jmcneill return 0;
2215 1.1 jmcneill }
2216 1.1 jmcneill first = false;
2217 1.1 jmcneill }
2218 1.1 jmcneill
2219 1.1 jmcneill return 0;
2220 1.1 jmcneill }
2221 1.1 jmcneill
2222 1.1 jmcneill #endif /* NVIDEO > 0 */
2223