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