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