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