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