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