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