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