Hi,

Included is my initial effort to port the dual-licensed driver
for Fushicai Audio-Video Grabber (vendor 0x1b71 product 0x3002).

As I mentioned in previous email it is "mostly working". I have
tested it on amd64 and macppc. The macppc audio issue mentioned
got resolved thanks to a hint from Alexandre Ratchov; I needed
some education on how to use sndiod :-) Thanks Alex.

I realize not many of these devices are out in the hands of
people who could test this code, but there are some obvious
bits (especially in the audio ops) that I am "winging", partly
because, the audio interface required them.

There is .h and two .c files; as mentioned, I kept the original
driver source bits and what I borrowed from uvideo.c separate.
If they must be combine into one .c file, I leave that part to
Marcus Glocker and company.

My main testing tool has been video(1). I had to modify it for
two reasons: 1. To test the mmap() interface and 2. because
to determine format description video(1) does a strcmp() rather
than use the pixelformat member of the v4l2_fmtdesc struct.
Patch at the very end. Maybe upstream might be interested in
the mmap change?

Cheers,
--patrick



Index: arch/amd64/conf/GENERIC
===================================================================
RCS file: /cvs/obsd/src/sys/arch/amd64/conf/GENERIC,v
retrieving revision 1.415
diff -u -p -u -p -r1.415 GENERIC
--- arch/amd64/conf/GENERIC     28 Mar 2016 17:54:37 -0000      1.415
+++ arch/amd64/conf/GENERIC     26 May 2016 22:34:38 -0000
@@ -289,6 +289,9 @@ uoakv*      at uhidev?              # Toradex OAK 10V sen
 onewire* at uow?
 uvideo*        at uhub?                # USB Video
 video* at uvideo?
+utvfu* at uhub?                # Fushicai Audio-Video Grabber
+video* at utvfu?
+audio* at utvfu?
 udl*   at uhub?                # DisplayLink USB displays
 wsdisplay* at udl?
 
Index: dev/video.c
===================================================================
RCS file: /cvs/obsd/src/sys/dev/video.c,v
retrieving revision 1.38
diff -u -p -u -p -r1.38 video.c
--- dev/video.c 8 Feb 2016 17:21:10 -0000       1.38
+++ dev/video.c 26 May 2016 22:34:38 -0000
@@ -392,6 +392,14 @@ videommap(dev_t dev, off_t off, int prot
        return (pa);
 }
 
+int
+video_submatch(struct device *parent, void *match, void *aux)
+{
+        struct cfdata *cf = match;
+
+       return (cf->cf_driver == &video_cd);
+}
+
 /*
  * Called from hardware driver. This is where the MI video driver gets
  * probed/attached to the hardware driver
@@ -403,7 +411,7 @@ video_attach_mi(struct video_hw_if *rhwp
 
        arg.hwif = rhwp;
        arg.hdl = hdlp;
-       return (config_found(dev, &arg, videoprint));
+       return (config_found_sm(dev, &arg, videoprint, video_submatch));
 }
 
 void
Index: dev/audio.c
===================================================================
RCS file: /cvs/obsd/src/sys/dev/audio.c,v
retrieving revision 1.145
diff -u -p -u -p -r1.145 audio.c
--- dev/audio.c 16 Mar 2016 06:46:39 -0000      1.145
+++ dev/audio.c 26 May 2016 22:34:38 -0000
@@ -1335,6 +1335,14 @@ audio_detach(struct device *self, int fl
        return 0;
 }
 
+int
+audio_submatch(struct device *parent, void *match, void *aux)
+{
+        struct cfdata *cf = match;
+
+       return (cf->cf_driver == &audio_cd);
+}
+
 struct device *
 audio_attach_mi(struct audio_hw_if *ops, void *arg, struct device *dev)
 {
@@ -1348,7 +1356,7 @@ audio_attach_mi(struct audio_hw_if *ops,
         * attach this driver to the caller (hardware driver), this
         * checks the kernel config and possibly calls audio_attach()
         */
-       return config_found(dev, &aa, audioprint);
+       return config_found_sm(dev, &aa, audioprint, audio_submatch);
 }
 
 int
Index: dev/usb/usbdevs
===================================================================
RCS file: /cvs/obsd/src/sys/dev/usb/usbdevs,v
retrieving revision 1.663
diff -u -p -u -p -r1.663 usbdevs
--- dev/usb/usbdevs     31 Mar 2016 12:27:48 -0000      1.663
+++ dev/usb/usbdevs     26 May 2016 22:34:38 -0000
@@ -591,6 +591,7 @@ vendor SILABS5              0x1ba4  Silicon Labs
 vendor CORSAIR         0x1b1c  Corsair
 vendor MATRIXORB       0x1b3d  Matrix Orbital
 vendor TORADEX         0x1b67  Toradex inc.
+vendor FUSHICAI                0x1b71  Fushicai
 vendor OVISLINK                0x1b75  OvisLink
 vendor TML             0x1b91  The Mobility Lab
 vendor TCTMOBILE       0x1bbb  TCT Mobile
@@ -1995,6 +1996,9 @@ product FUJITSUCOMP VIRTETH       0xa4a2  Virtu
 
 /* Fujitsu Siemens Computers products */
 product FSC E5400              0x1009  PrismGT USB 2.0 WLAN
+
+/* Fushicai */
+product FUSHICAI USBTV007      0x3002  Fushicai Audio-Video Grabber
 
 /* G.Mate */
 product        GMATE YP3X00            0x1001  YP3X00 PDA
Index: dev/usb/files.usb
===================================================================
RCS file: /cvs/obsd/src/sys/dev/usb/files.usb,v
retrieving revision 1.126
diff -u -p -u -p -r1.126 files.usb
--- dev/usb/files.usb   8 Jan 2016 15:54:13 -0000       1.126
+++ dev/usb/files.usb   26 May 2016 22:34:38 -0000
@@ -35,6 +35,12 @@ device       uvideo: video, firmload
 attach uvideo at uhub
 file   dev/usb/uvideo.c                uvideo
 
+# USBTV007 devices
+device utvfu: video, audio
+attach utvfu at uhub
+file   dev/usb/utvfu.c                 utvfu
+file   dev/usb/utvfu_ops.c             utvfu
+
 device udl: wsemuldisplaydev, rasops16, edid
 attach udl at uhub
 file   dev/usb/udl.c                   udl
Index: dev/usb/utvfu.h
===================================================================
RCS file: dev/usb/utvfu.h
diff -N dev/usb/utvfu.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ dev/usb/utvfu.h     26 May 2016 22:34:38 -0000
@@ -0,0 +1,172 @@
+/*     $OpenBSD$ */
+/*
+ * Fushicai USBTV007 Audio-Video Grabber Driver
+ *
+ * Copyright (c) 2013 Lubomir Rintel
+ * All rights reserved.
+ * No physical hardware was harmed running Windows during the
+ * reverse-engineering activity
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * Alternatively, this software may be distributed under the terms of the
+ * GNU General Public License ("GPL").
+ */
+
+#ifndef _UTVFU_H_
+#define _UTVFU_H_
+
+#include <sys/queue.h>
+#include <sys/videoio.h>
+
+/* Hardware. */
+#define UTVFU_VIDEO_ENDP       0x81
+#define UTVFU_AUDIO_ENDP       0x83
+#define UTVFU_BASE             0xc000
+#define UTVFU_REQUEST_REG      12
+
+/* Number of concurrent isochronous urbs submitted.
+ * Higher numbers was seen to overly saturate the USB bus. */
+#define UTVFU_ISOC_TRANSFERS   16
+#define UTVFU_ISOC_PACKETS     8
+
+#define UTVFU_CHUNK_SIZE       256
+#define UTVFU_CHUNK            240
+
+#define UTVFU_AUDIO_URBSIZE    20480
+#define UTVFU_AUDIO_HDRSIZE    4
+#define UTVFU_AUDIO_BUFFER     65536
+
+#define        UTVFU_COMPOSITE_INPUT   0
+#define        UTVFU_SVIDEO_INPUT      1
+
+/* Chunk header. */
+#define UTVFU_MAGIC(hdr)       (hdr & 0xff000000U)
+#define UTVFU_MAGIC_OK(hdr)    ((hdr & 0xff000000U) == 0x88000000U)
+#define UTVFU_FRAME_ID(hdr)    ((hdr & 0x00ff0000U) >> 16)
+#define UTVFU_ODD(hdr)         ((hdr & 0x0000f000U) >> 15)
+#define UTVFU_CHUNK_NO(hdr)    (hdr & 0x00000fffU)
+
+#define UTVFU_TV_STD  (V4L2_STD_525_60 | V4L2_STD_PAL)
+
+/* parameters for supported TV norms */
+struct utvfu_norm_params {
+       v4l2_std_id     norm;
+       int             cap_width,
+                       cap_height,
+                       frame_len;
+};
+extern struct utvfu_norm_params        utvfu_norm_params[];
+
+#define        UTVFU_MAX_BUFFERS       32
+struct utvfu_mmap {
+       SIMPLEQ_ENTRY(utvfu_mmap)       q_frames;
+       uint8_t                         *buf;
+       struct v4l2_buffer              v4l2_buf;
+};
+typedef SIMPLEQ_HEAD(, utvfu_mmap)     q_mmap;
+
+struct utvfu_frame_buf {
+       uint            off;
+       uint            size;
+       uint16_t        chunks_done;
+       uint8_t         fid; 
+       uint8_t         last_odd;
+       uint8_t         *buf;
+};
+
+#define        UTVFU_NFRAMES_MAX       40
+struct utvfu_isoc_xfer {
+       struct utvfu_softc      *sc;
+       struct usbd_xfer        *xfer;
+       void                    *buf;
+       uint16_t                size[UTVFU_NFRAMES_MAX];
+};
+
+struct utvfu_bulk_xfer {
+       struct usbd_xfer        *xfer;
+       void                    *buf;
+};
+
+struct utvfu_vs_iface {
+       struct usbd_pipe        *pipeh;
+       int                     endpoint;
+       uint32_t                psize;
+       struct utvfu_isoc_xfer  ixfer[UTVFU_ISOC_TRANSFERS];
+};
+
+struct utvfu_as_iface {
+       struct usbd_pipe        *pipeh;
+       int                     endpoint;
+       struct utvfu_bulk_xfer  bxfer;
+};
+
+struct utvfu_audio_chan {
+       uint8_t                 *start;
+       uint8_t                 *end;
+       uint8_t                 *cur;
+       int                     blksize;
+       void                    *intr_arg;
+       void                    (*intr)(void *);
+       struct utvfu_as_iface   iface;
+};
+
+/* Per-device structure. */
+struct utvfu_softc {
+       struct device           sc_dev;
+       struct usbd_device      *sc_udev;
+       struct usbd_interface   *sc_uifaceh;
+
+       /* audio & video device */
+       struct device           *sc_audiodev;
+       struct device           *sc_videodev;
+
+       int                     sc_normi;
+       int                     sc_nchunks;
+       int                     sc_input;
+       int                     sc_max_frame_sz;
+       int                     sc_nframes;
+
+       struct utvfu_vs_iface   sc_iface;
+       struct utvfu_frame_buf  sc_fb;
+
+       int                     sc_as_running;
+       struct utvfu_audio_chan sc_audio;
+
+       /* mmap */
+       struct utvfu_mmap       sc_mmap[UTVFU_MAX_BUFFERS];
+       uint8_t                 *sc_mmap_buffer;
+       q_mmap                  sc_mmap_q;
+       int                     sc_mmap_bufsz;
+       int                     sc_mmap_count;
+       int                     sc_mmap_flag;
+
+       /* uplayer */
+       void                    *sc_uplayer_arg;
+       int                     *sc_uplayer_fsize;
+       uint8_t                 *sc_uplayer_fbuffer;
+       void                    (*sc_uplayer_intr)(void *);
+};
+
+int    utvfu_max_frame_size(void);
+int    utvfu_set_regs(struct utvfu_softc *, const uint16_t regs[][2], int);
+void   utvfu_image_chunk(struct utvfu_softc *, u_char *);
+int    utvfu_configure_for_norm(struct utvfu_softc *, v4l2_std_id);
+int    utvfu_start_capture(struct utvfu_softc *);
+int    utvfu_mmap_queue(struct utvfu_softc *, uint8_t *, int);
+void   utvfu_read(struct utvfu_softc *, uint8_t *, int);
+
+void   utvfu_audio_decode(struct utvfu_softc *, int);
+int    utvfu_audio_start(struct utvfu_softc *);
+int    utvfu_audio_stop(struct utvfu_softc *);
+int    utvfu_audio_start_chip(struct utvfu_softc *);
+int    utvfu_audio_stop_chip(struct utvfu_softc *);
+
+#endif
Index: dev/usb/utvfu.c
===================================================================
RCS file: dev/usb/utvfu.c
diff -N dev/usb/utvfu.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ dev/usb/utvfu.c     26 May 2016 22:34:38 -0000
@@ -0,0 +1,691 @@
+/*     $OpenBSD$ */
+/*
+ * Fushicai USBTV007 Audio-Video Grabber Driver
+ *
+ * Product web site:
+ * 
http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html
+ *
+ * Following LWN articles were very useful in construction of this driver:
+ * Video4Linux2 API series: http://lwn.net/Articles/203924/
+ * videobuf2 API explanation: http://lwn.net/Articles/447435/
+ * Thanks go to Jonathan Corbet for providing this quality documentation.
+ * He is awesome.
+ *
+ * Copyright (c) 2013 Lubomir Rintel
+ * Copyright (c) 2013 Federico Simoncelli
+ * All rights reserved.
+ * No physical hardware was harmed running Windows during the
+ * reverse-engineering activity
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions, and the following disclaimer,
+ *    without modification.
+ * 2. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * Alternatively, this software may be distributed under the terms of the
+ * GNU General Public License ("GPL").
+ */
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/malloc.h>
+#include <sys/audioio.h>
+#include <sys/videoio.h>
+
+#include <machine/bus.h>
+
+#include <dev/audio_if.h>
+#include <dev/usb/usb.h>
+#include <dev/usb/usbdi.h>
+#include <dev/usb/usbdivar.h>
+#include <dev/usb/usbdi_util.h>
+#include <dev/usb/usbdevs.h>
+#include <dev/video_if.h>
+
+#include "utvfu.h"
+
+#define UTVFU_DEBUG
+#ifdef UTVFU_DEBUG
+extern int utvfu_debug;
+#define DPRINTF(l, x...) do { if ((l) <= utvfu_debug) printf(x); } while (0)
+#else
+#define DPRINTF(l, x...)
+#endif
+
+#define DEVNAME(_s) ((_s)->sc_dev.dv_xname)
+
+struct utvfu_norm_params utvfu_norm_params[] = {
+       {
+               .norm = V4L2_STD_525_60,
+               .cap_width = 720,
+               .cap_height = 480,
+               /* 4 bytes/2 pixel YUYV/YUV 4:2:2 */
+               .frame_len = (720 * 480 * 2),
+       },
+       {
+               .norm = V4L2_STD_PAL,
+               .cap_width = 720,
+               .cap_height = 576,
+               /* 4 bytes/2 pixel YUYV/YUV 4:2:2 */
+               .frame_len = (720 * 576 * 2),
+       }
+};
+
+int
+utvfu_set_regs(struct utvfu_softc *sc, const uint16_t regs[][2], int size)
+{
+       int i;
+       usbd_status error;
+       usb_device_request_t req;
+
+       DPRINTF(1, "%s: %s: size=%d enter\n", DEVNAME(sc), __func__, size);
+
+       req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
+       req.bRequest = UTVFU_REQUEST_REG;
+       USETW(req.wLength, 0);
+
+       for (i = 0; i < size; i++) {
+               USETW(req.wIndex, regs[i][0]);
+               USETW(req.wValue, regs[i][1]);
+
+               error = usbd_do_request(sc->sc_udev, &req, NULL);
+               if (USBD_NORMAL_COMPLETION != error) {
+                       DPRINTF(1, "%s: %s: exit EINVAL\n",
+                           DEVNAME(sc), __func__);
+                       return (EINVAL);
+               }
+       }
+
+       DPRINTF(1, "%s: %s: exit OK\n", DEVNAME(sc), __func__);
+
+       return (0);
+}
+
+int
+utvfu_max_frame_size(void)
+{
+       int     i, sz = 0;
+       for (i = 0; i < nitems(utvfu_norm_params); i++) {
+               if (sz < utvfu_norm_params[i].frame_len)
+                       sz = utvfu_norm_params[i].frame_len;
+       }
+       return (sz);
+}
+
+int
+utvfu_configure_for_norm(struct utvfu_softc *sc, v4l2_std_id norm)
+{
+       int i, ret = EINVAL;
+       struct utvfu_norm_params *params = NULL;
+
+       for (i = 0; i < nitems(utvfu_norm_params); i++) {
+               if (utvfu_norm_params[i].norm & norm) {
+                       params = &utvfu_norm_params[i];
+                       break;
+               }
+       }
+
+       if (NULL != params) {
+               sc->sc_normi = i;
+               sc->sc_nchunks = params->cap_width * params->cap_height
+                   / 4 / UTVFU_CHUNK;
+               ret = 0;
+       }
+
+       return (ret);
+}
+
+int
+utvfu_select_input(struct utvfu_softc *sc, int input)
+{
+       int ret;
+
+       static const uint16_t composite[][2] = {
+               { UTVFU_BASE + 0x0105, 0x0060 },
+               { UTVFU_BASE + 0x011f, 0x00f2 },
+               { UTVFU_BASE + 0x0127, 0x0060 },
+               { UTVFU_BASE + 0x00ae, 0x0010 },
+               { UTVFU_BASE + 0x0239, 0x0060 },
+       };
+
+       static const uint16_t svideo[][2] = {
+               { UTVFU_BASE + 0x0105, 0x0010 },
+               { UTVFU_BASE + 0x011f, 0x00ff },
+               { UTVFU_BASE + 0x0127, 0x0060 },
+               { UTVFU_BASE + 0x00ae, 0x0030 },
+               { UTVFU_BASE + 0x0239, 0x0060 },
+       };
+
+       switch (input) {
+       case UTVFU_COMPOSITE_INPUT:
+               ret = utvfu_set_regs(sc, composite, nitems(composite));
+               break;
+       case UTVFU_SVIDEO_INPUT:
+               ret = utvfu_set_regs(sc, svideo, nitems(svideo));
+               break;
+       default:
+               ret = EINVAL;
+       }
+
+       if (0 == ret)
+               sc->sc_input = input;
+
+       return (ret);
+}
+
+int
+utvfu_select_norm(struct utvfu_softc *sc, v4l2_std_id norm)
+{
+       int ret;
+       static const uint16_t pal[][2] = {
+               { UTVFU_BASE + 0x001a, 0x0068 },
+               { UTVFU_BASE + 0x010e, 0x0072 },
+               { UTVFU_BASE + 0x010f, 0x00a2 },
+               { UTVFU_BASE + 0x0112, 0x00b0 },
+               { UTVFU_BASE + 0x0117, 0x0001 },
+               { UTVFU_BASE + 0x0118, 0x002c },
+               { UTVFU_BASE + 0x012d, 0x0010 },
+               { UTVFU_BASE + 0x012f, 0x0020 },
+               { UTVFU_BASE + 0x024f, 0x0002 },
+               { UTVFU_BASE + 0x0254, 0x0059 },
+               { UTVFU_BASE + 0x025a, 0x0016 },
+               { UTVFU_BASE + 0x025b, 0x0035 },
+               { UTVFU_BASE + 0x0263, 0x0017 },
+               { UTVFU_BASE + 0x0266, 0x0016 },
+               { UTVFU_BASE + 0x0267, 0x0036 }
+       };
+
+       static const uint16_t ntsc[][2] = {
+               { UTVFU_BASE + 0x001a, 0x0079 },
+               { UTVFU_BASE + 0x010e, 0x0068 },
+               { UTVFU_BASE + 0x010f, 0x009c },
+               { UTVFU_BASE + 0x0112, 0x00f0 },
+               { UTVFU_BASE + 0x0117, 0x0000 },
+               { UTVFU_BASE + 0x0118, 0x00fc },
+               { UTVFU_BASE + 0x012d, 0x0004 },
+               { UTVFU_BASE + 0x012f, 0x0008 },
+               { UTVFU_BASE + 0x024f, 0x0001 },
+               { UTVFU_BASE + 0x0254, 0x005f },
+               { UTVFU_BASE + 0x025a, 0x0012 },
+               { UTVFU_BASE + 0x025b, 0x0001 },
+               { UTVFU_BASE + 0x0263, 0x001c },
+               { UTVFU_BASE + 0x0266, 0x0011 },
+               { UTVFU_BASE + 0x0267, 0x0005 }
+       };
+
+       ret = utvfu_configure_for_norm(sc, norm);
+
+       if (0 == ret) {
+               if (norm & V4L2_STD_525_60)
+                       ret = utvfu_set_regs(sc, ntsc, nitems(ntsc));
+               else if (norm & V4L2_STD_PAL)
+                       ret = utvfu_set_regs(sc, pal, nitems(pal));
+       }
+
+       return (ret);
+}
+
+int
+utvfu_setup_capture(struct utvfu_softc *sc)
+{
+       int ret;
+       static const uint16_t setup[][2] = {
+               /* These seem to enable the device. */
+               { UTVFU_BASE + 0x0008, 0x0001 },
+               { UTVFU_BASE + 0x01d0, 0x00ff },
+               { UTVFU_BASE + 0x01d9, 0x0002 },
+
+               /*
+                * These seem to influence color parameters, such as
+                * brightness, etc.
+                */
+               { UTVFU_BASE + 0x0239, 0x0040 },
+               { UTVFU_BASE + 0x0240, 0x0000 },
+               { UTVFU_BASE + 0x0241, 0x0000 },
+               { UTVFU_BASE + 0x0242, 0x0002 },
+               { UTVFU_BASE + 0x0243, 0x0080 },
+               { UTVFU_BASE + 0x0244, 0x0012 },
+               { UTVFU_BASE + 0x0245, 0x0090 },
+               { UTVFU_BASE + 0x0246, 0x0000 },
+
+               { UTVFU_BASE + 0x0278, 0x002d },
+               { UTVFU_BASE + 0x0279, 0x000a },
+               { UTVFU_BASE + 0x027a, 0x0032 },
+               { 0xf890, 0x000c },
+               { 0xf894, 0x0086 },
+
+               { UTVFU_BASE + 0x00ac, 0x00c0 },
+               { UTVFU_BASE + 0x00ad, 0x0000 },
+               { UTVFU_BASE + 0x00a2, 0x0012 },
+               { UTVFU_BASE + 0x00a3, 0x00e0 },
+               { UTVFU_BASE + 0x00a4, 0x0028 },
+               { UTVFU_BASE + 0x00a5, 0x0082 },
+               { UTVFU_BASE + 0x00a7, 0x0080 },
+               { UTVFU_BASE + 0x0000, 0x0014 },
+               { UTVFU_BASE + 0x0006, 0x0003 },
+               { UTVFU_BASE + 0x0090, 0x0099 },
+               { UTVFU_BASE + 0x0091, 0x0090 },
+               { UTVFU_BASE + 0x0094, 0x0068 },
+               { UTVFU_BASE + 0x0095, 0x0070 },
+               { UTVFU_BASE + 0x009c, 0x0030 },
+               { UTVFU_BASE + 0x009d, 0x00c0 },
+               { UTVFU_BASE + 0x009e, 0x00e0 },
+               { UTVFU_BASE + 0x0019, 0x0006 },
+               { UTVFU_BASE + 0x008c, 0x00ba },
+               { UTVFU_BASE + 0x0101, 0x00ff },
+               { UTVFU_BASE + 0x010c, 0x00b3 },
+               { UTVFU_BASE + 0x01b2, 0x0080 },
+               { UTVFU_BASE + 0x01b4, 0x00a0 },
+               { UTVFU_BASE + 0x014c, 0x00ff },
+               { UTVFU_BASE + 0x014d, 0x00ca },
+               { UTVFU_BASE + 0x0113, 0x0053 },
+               { UTVFU_BASE + 0x0119, 0x008a },
+               { UTVFU_BASE + 0x013c, 0x0003 },
+               { UTVFU_BASE + 0x0150, 0x009c },
+               { UTVFU_BASE + 0x0151, 0x0071 },
+               { UTVFU_BASE + 0x0152, 0x00c6 },
+               { UTVFU_BASE + 0x0153, 0x0084 },
+               { UTVFU_BASE + 0x0154, 0x00bc },
+               { UTVFU_BASE + 0x0155, 0x00a0 },
+               { UTVFU_BASE + 0x0156, 0x00a0 },
+               { UTVFU_BASE + 0x0157, 0x009c },
+               { UTVFU_BASE + 0x0158, 0x001f },
+               { UTVFU_BASE + 0x0159, 0x0006 },
+               { UTVFU_BASE + 0x015d, 0x0000 },
+
+               { UTVFU_BASE + 0x0003, 0x0004 },
+               { UTVFU_BASE + 0x0100, 0x00d3 },
+               { UTVFU_BASE + 0x0115, 0x0015 },
+               { UTVFU_BASE + 0x0220, 0x002e },
+               { UTVFU_BASE + 0x0225, 0x0008 },
+               { UTVFU_BASE + 0x024e, 0x0002 },
+               { UTVFU_BASE + 0x024e, 0x0002 },
+               { UTVFU_BASE + 0x024f, 0x0002 },
+       };
+
+       DPRINTF(1, "%s: %s: calling utvfu_set_regs()\n",
+           DEVNAME(sc), __func__);
+       ret = utvfu_set_regs(sc, setup, nitems(setup));
+       if (ret)
+               return (ret);
+
+       DPRINTF(1, "%s: %s: calling utvfu_select_norm()\n",
+           DEVNAME(sc), __func__);
+       ret = utvfu_select_norm(sc, utvfu_norm_params[sc->sc_normi].norm);
+       if (ret)
+               return (ret);
+
+       DPRINTF(1, "%s: %s: calling utvfu_select_input()\n",
+           DEVNAME(sc), __func__);
+       ret = utvfu_select_input(sc, sc->sc_input);
+       if (ret)
+               return (ret);
+
+       return (0);
+}
+
+/*
+ * Copy data from chunk into a frame buffer, deinterlacing the data
+ * into every second line. Unfortunately, they don't align nicely into
+ * 720 pixel lines, as the chunk is 240 words long, which is 480 pixels.
+ * Therefore, we break down the chunk into two halves before copyting,
+ * so that we can interleave a line if needed.
+ *
+ * Each "chunk" is 240 words; a word in this context equals 4 bytes.
+ * Image format is YUYV/YUV 4:2:2, consisting of Y Cr Y Cb, defining two
+ * pixels, the Cr and Cb shared between the two pixels, but each having
+ * separate Y values. Thus, the 240 words equal 480 pixels. It therefore,
+ * takes 1.5 chunks to make a 720 pixel-wide line for the frame.
+ * The image is interlaced, so there is a "scan" of odd lines, followed
+ * by "scan" of even numbered lines.
+ *
+ * Following code is writing the chunks in correct sequence, skipping
+ * the rows based on "odd" value.
+ * line 1: chunk[0][  0..479] chunk[0][480..959] chunk[1][  0..479]
+ * line 3: chunk[1][480..959] chunk[2][  0..479] chunk[2][480..959]
+ * ...etc
+ */
+void
+utvfu_chunk_to_vbuf(uint8_t *frame, uint8_t *src, int chunk_no, int odd)
+{
+       uint8_t *dst;
+       int half, line, part_no, part_index;
+       #define UTVFU_STRIDE    (UTVFU_CHUNK/2 * 4)
+
+       for (half = 0; half < 2; half++) {
+               part_no = chunk_no * 2 + half;
+               line = part_no / 3;
+               part_index = (line * 2 + !odd) * 3 + (part_no % 3);
+
+               dst = &frame[part_index * UTVFU_STRIDE];
+
+               memcpy(dst, src, UTVFU_STRIDE);
+               src += UTVFU_STRIDE;
+       }
+       #undef  UTVFU_STRIDE
+}
+
+/*
+ * Called for each 256-byte image chunk.
+ * First word identifies the chunk, followed by 240 words of image
+ * data and padding.
+ */
+void
+utvfu_image_chunk(struct utvfu_softc *sc, u_char *chunk)
+{
+       int frame_id, odd, chunk_no, frame_len;
+       uint32_t hdr;
+
+       memcpy(&hdr, chunk, sizeof(hdr));
+       chunk += sizeof(hdr);
+       hdr = be32toh(hdr);
+
+       /* Ignore corrupted lines. */
+       if (!UTVFU_MAGIC_OK(hdr)) {
+               DPRINTF(2, "%s: bad magic=0x%08x\n",
+                   DEVNAME(sc), UTVFU_MAGIC(hdr));
+               return;
+       }
+
+       frame_id = UTVFU_FRAME_ID(hdr);
+       odd = UTVFU_ODD(hdr);
+       chunk_no = UTVFU_CHUNK_NO(hdr);
+       if (chunk_no >= sc->sc_nchunks) {
+               DPRINTF(2, "%s: chunk_no=%d >= sc_nchunks=%d\n",
+                   DEVNAME(sc), chunk_no, sc->sc_nchunks);
+               return;
+       }
+
+       /* Beginning of a frame. */
+       if (chunk_no == 0) {
+               sc->sc_fb.fid = frame_id;
+               sc->sc_fb.chunks_done = 0;
+       }
+       else if (sc->sc_fb.fid != frame_id) {
+               DPRINTF(2, "%s: frame id mismatch expecting=%d got=%d\n",
+                   DEVNAME(sc), sc->sc_fb.fid, frame_id);
+               return;
+       }
+
+       frame_len = utvfu_norm_params[sc->sc_normi].frame_len;
+
+       /* Copy the chunk data. */
+       utvfu_chunk_to_vbuf(sc->sc_fb.buf, chunk, chunk_no, odd);
+       sc->sc_fb.chunks_done++;
+
+       /* Last chunk in a field */
+       if (chunk_no == sc->sc_nchunks-1) {
+               /* Last chunk in a frame, signalling an end */
+               if (odd && !sc->sc_fb.last_odd) {
+                       if (sc->sc_fb.chunks_done != sc->sc_nchunks) {
+                               DPRINTF(1, "%s: chunks_done=%d != nchunks=%d\n",
+                                   DEVNAME(sc),
+                                   sc->sc_fb.chunks_done, sc->sc_nchunks);
+                       }
+
+                       if (sc->sc_mmap_flag) {
+                               utvfu_mmap_queue(sc, sc->sc_fb.buf, frame_len);
+                       }
+                       else {
+                               utvfu_read(sc, sc->sc_fb.buf, frame_len);
+                       }
+               }
+               sc->sc_fb.last_odd = odd;
+       }
+}
+
+int
+utvfu_start_capture(struct utvfu_softc *sc)
+{
+       usbd_status error;
+
+       DPRINTF(1, "%s: %s: calling utvfu_audio_stop()\n",
+           DEVNAME(sc), __func__);
+       utvfu_audio_stop(sc);
+
+       DPRINTF(1, "%s: %s: calling usbd_set_interface(ifaceh, 0)\n",
+           DEVNAME(sc), __func__);
+       /* default video stream interface */
+       error = usbd_set_interface(sc->sc_uifaceh, 0);
+       if (USBD_NORMAL_COMPLETION != error)
+               return (EINVAL);
+
+       DPRINTF(1, "%s: %s: calling utvfu_setup_capture()\n",
+           DEVNAME(sc), __func__);
+       if (0 != utvfu_setup_capture(sc))
+               return (EINVAL);
+
+       DPRINTF(1, "%s: %s: calling usbd_set_interface(ifaceh, 1)\n",
+           DEVNAME(sc), __func__);
+       /* alt setting */
+       error = usbd_set_interface(sc->sc_uifaceh, 1);
+       if (USBD_NORMAL_COMPLETION != error)
+               return (EINVAL);
+
+       DPRINTF(1, "%s: %s: calling utvfu_audio_start()\n",
+           DEVNAME(sc), __func__);
+       utvfu_audio_start(sc);
+
+       return (0);
+}
+
+int
+utvfu_querycap(void *v, struct v4l2_capability *cap)
+{
+       struct utvfu_softc *sc = v;
+
+       memset(cap, 0, sizeof(*cap));
+       strlcpy(cap->driver, DEVNAME(sc), sizeof(cap->driver));
+       strlcpy(cap->card, "utvfu", sizeof(cap->card));
+       strlcpy(cap->bus_info, "usb", sizeof(cap->bus_info));
+       cap->device_caps = V4L2_CAP_VIDEO_CAPTURE;
+       cap->device_caps |= V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
+       cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
+       return (0);
+}
+
+int
+utvfu_enum_input(void *v, struct v4l2_input *i)
+{
+       struct utvfu_softc *sc = v;
+
+       switch (i->index) {
+       case UTVFU_COMPOSITE_INPUT:
+               strlcpy(i->name, "Composite", sizeof(i->name));
+               break;
+       case UTVFU_SVIDEO_INPUT:
+               strlcpy(i->name, "S-Video", sizeof(i->name));
+               break;
+       default:
+               return (EINVAL);
+       }
+
+       i->type = V4L2_INPUT_TYPE_CAMERA;
+       i->std = utvfu_norm_params[sc->sc_normi].norm;
+       return (0);
+}
+
+int
+utvfu_enum_fmt_vid_cap(void *v, struct v4l2_fmtdesc *f)
+{
+       if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || f->index != 0)
+               return (EINVAL);
+
+       strlcpy(f->description, "16 bpp YUY2, 4:2:2, packed",
+                                       sizeof(f->description));
+       f->pixelformat = V4L2_PIX_FMT_YUYV;
+       return (0);
+}
+
+int
+utvfu_enum_fsizes(void *v, struct v4l2_frmsizeenum *fsizes)
+{
+       struct utvfu_softc *sc = v;
+
+       if (fsizes->pixel_format != V4L2_PIX_FMT_YUYV)
+               return (EINVAL);
+
+       fsizes->type = V4L2_FRMSIZE_TYPE_DISCRETE;
+       fsizes->discrete.width = utvfu_norm_params[sc->sc_normi].cap_width;
+       fsizes->discrete.height = utvfu_norm_params[sc->sc_normi].cap_height;
+       return (0);
+}
+
+int
+utvfu_g_fmt(void *v, struct v4l2_format *f)
+{
+       struct utvfu_softc *sc = v;
+
+       f->fmt.pix.width = utvfu_norm_params[sc->sc_normi].cap_width;
+       f->fmt.pix.height = utvfu_norm_params[sc->sc_normi].cap_height;
+       f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
+       f->fmt.pix.field = V4L2_FIELD_INTERLACED;
+       f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
+       f->fmt.pix.sizeimage = (f->fmt.pix.bytesperline * f->fmt.pix.height);
+       f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
+       return (0);
+}
+
+int
+utvfu_s_fmt(void *v, struct v4l2_format *f)
+{
+       if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_YUYV)
+               return (EINVAL);
+       return (0);
+}
+
+int
+utvfu_g_std(void *v, v4l2_std_id *norm)
+{
+       struct utvfu_softc *sc = v;
+       *norm = utvfu_norm_params[sc->sc_normi].norm;
+       return (0);
+}
+
+int
+utvfu_s_std(void *v, v4l2_std_id norm)
+{
+       int ret = EINVAL;
+       struct utvfu_softc *sc = v;
+
+       if ((norm & V4L2_STD_525_60) || (norm & V4L2_STD_PAL))
+               ret = utvfu_select_norm(sc, norm);
+
+       return (ret);
+}
+
+int
+utvfu_g_input(void *v, int *i)
+{
+       struct utvfu_softc *sc = v;
+       *i = sc->sc_input;
+       return (0);
+}
+
+int
+utvfu_s_input(void *v, int i)
+{
+       return utvfu_select_input(v, i);
+}
+
+/* A U D I O */
+
+void
+utvfu_audio_decode(struct utvfu_softc *sc, int len)
+{
+       uint8_t *dst, *src;
+       int n, chunk, ncopied;
+       struct utvfu_bulk_xfer *xfer = &sc->sc_audio.iface.bxfer;
+
+       src = xfer->buf;
+       dst = sc->sc_audio.cur;
+       ncopied = sc->sc_audio.cur - sc->sc_audio.start;
+       /* b/c region start->end is a multiple blksize chunks */
+       ncopied %= sc->sc_audio.blksize;
+
+       while (len >= UTVFU_CHUNK_SIZE) {
+               /*
+                * Header is 0xdd000000 -> 0xdd0003ff
+                * Seems, 0x03ff mask grabs the chunk number.
+                */
+               src += UTVFU_AUDIO_HDRSIZE;
+               chunk = UTVFU_CHUNK;
+               while (chunk > 0) {
+                       n = min(chunk, sc->sc_audio.blksize - ncopied);
+                       memcpy(dst, src, n);
+                       dst += n;
+                       src += n;
+                       chunk -= n;
+                       ncopied += n;
+                       if (ncopied >= sc->sc_audio.blksize) {
+                               mtx_enter(&audio_lock);
+                               (*sc->sc_audio.intr)(sc->sc_audio.intr_arg);
+                               mtx_leave(&audio_lock);
+                               ncopied -= sc->sc_audio.blksize;
+                       }
+                       if (dst > sc->sc_audio.end)
+                               dst = sc->sc_audio.start;
+               }
+               len -= UTVFU_CHUNK_SIZE; /* _CHUNK + _AUDIO_HDRSIZE */
+       }
+       sc->sc_audio.cur = dst;
+}
+
+int
+utvfu_audio_start_chip(struct utvfu_softc *sc)
+{
+       static const uint16_t setup[][2] = {
+               /* These seem to enable the device. */
+               { UTVFU_BASE + 0x0008, 0x0001 },
+               { UTVFU_BASE + 0x01d0, 0x00ff },
+               { UTVFU_BASE + 0x01d9, 0x0002 },
+
+               { UTVFU_BASE + 0x01da, 0x0013 },
+               { UTVFU_BASE + 0x01db, 0x0012 },
+               { UTVFU_BASE + 0x01e9, 0x0002 },
+               { UTVFU_BASE + 0x01ec, 0x006c },
+               { UTVFU_BASE + 0x0294, 0x0020 },
+               { UTVFU_BASE + 0x0255, 0x00cf },
+               { UTVFU_BASE + 0x0256, 0x0020 },
+               { UTVFU_BASE + 0x01eb, 0x0030 },
+               { UTVFU_BASE + 0x027d, 0x00a6 },
+               { UTVFU_BASE + 0x0280, 0x0011 },
+               { UTVFU_BASE + 0x0281, 0x0040 },
+               { UTVFU_BASE + 0x0282, 0x0011 },
+               { UTVFU_BASE + 0x0283, 0x0040 },
+               { 0xf891, 0x0010 },
+
+               /* this sets the input from composite */
+               { UTVFU_BASE + 0x0284, 0x00aa },
+       };
+
+       /* starting the stream */
+       utvfu_set_regs(sc, setup, nitems(setup));
+
+       return (0);
+}
+
+int
+utvfu_audio_stop_chip(struct utvfu_softc *sc)
+{
+       static const uint16_t setup[][2] = {
+       /*
+        * The original windows driver sometimes sends also:
+        *   { UTVFU_BASE + 0x00a2, 0x0013 }
+        * but it seems useless and its real effects are untested at
+        * the moment.
+        */
+               { UTVFU_BASE + 0x027d, 0x0000 },
+               { UTVFU_BASE + 0x0280, 0x0010 },
+               { UTVFU_BASE + 0x0282, 0x0010 },
+       };
+
+       utvfu_set_regs(sc, setup, nitems(setup));
+
+       return (0);
+}
Index: dev/usb/utvfu_ops.c
===================================================================
RCS file: dev/usb/utvfu_ops.c
diff -N dev/usb/utvfu_ops.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ dev/usb/utvfu_ops.c 26 May 2016 22:34:38 -0000
@@ -0,0 +1,1486 @@
+/*     $OpenBSD$ */
+/*
+ * Copyright (c) 2008 Robert Nagy <[email protected]>
+ * Copyright (c) 2008 Marcus Glocker <[email protected]>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/fcntl.h>
+#include <sys/kernel.h>
+#include <sys/kthread.h>
+#include <sys/malloc.h>
+#include <sys/audioio.h>
+#include <sys/videoio.h>
+
+#include <uvm/uvm_extern.h>
+
+#include <machine/bus.h>
+
+#include <dev/audio_if.h>
+#include <dev/usb/usb.h>
+#include <dev/usb/usbdi.h>
+#include <dev/usb/usbdivar.h>
+#include <dev/usb/usbdi_util.h>
+#include <dev/usb/usbdevs.h>
+#include <dev/video_if.h>
+
+#include "utvfu.h"
+
+#define UTVFU_DEBUG
+#ifdef UTVFU_DEBUG
+int utvfu_debug = 1;
+#define DPRINTF(l, x...) do { if ((l) <= utvfu_debug) printf(x); } while (0)
+#else
+#define DPRINTF(l, x...)
+#endif
+
+#define DEVNAME(_s) ((_s)->sc_dev.dv_xname)
+
+int            utvfu_match(struct device *, void *, void *);
+void           utvfu_attach(struct device *, struct device *, void *);
+int            utvfu_detach(struct device *, int);
+int            utvfu_activate(struct device *, int);
+
+void           utvfu_vs_close(struct utvfu_softc *);
+void           utvfu_vs_free_frame(struct utvfu_softc *);
+void           utvfu_vs_free_isoc(struct utvfu_softc *);
+void           utvfu_vs_start_isoc_ixfer(struct utvfu_softc *,
+                   struct utvfu_isoc_xfer *);
+void           utvfu_vs_cb(struct usbd_xfer *, void *, usbd_status);
+
+void           utvfu_vs_free(struct utvfu_softc *);
+usbd_status    utvfu_vs_init(struct utvfu_softc *);
+usbd_status    utvfu_vs_alloc_frame(struct utvfu_softc *);
+usbd_status    utvfu_vs_alloc_isoc(struct utvfu_softc *);
+
+int            utvfu_open(void *, int, int *, uint8_t *,
+                   void (*)(void *), void *);
+int            utvfu_close(void *);
+int            utvfu_querycap(void *, struct v4l2_capability *);
+int            utvfu_enum_fmt_vid_cap(void *, struct v4l2_fmtdesc *);
+int            utvfu_enum_fsizes(void *, struct v4l2_frmsizeenum *);
+int            utvfu_g_fmt(void *, struct v4l2_format *);
+int            utvfu_s_fmt(void *, struct v4l2_format *);
+int            utvfu_g_parm(void *, struct v4l2_streamparm *);
+int            utvfu_s_parm(void *, struct v4l2_streamparm *);
+int            utvfu_enum_input(void *, struct v4l2_input *);
+int            utvfu_s_input(void *, int);
+int            utvfu_g_input(void *, int *);
+
+int            utvfu_reqbufs(void *, struct v4l2_requestbuffers *);
+int            utvfu_querybuf(void *, struct v4l2_buffer *);
+int            utvfu_qbuf(void *, struct v4l2_buffer *);
+int            utvfu_dqbuf(void *, struct v4l2_buffer *);
+int            utvfu_streamon(void *, int);
+int            utvfu_streamoff(void *, int);
+int            utvfu_queryctrl(void *, struct v4l2_queryctrl *);
+caddr_t                utvfu_mappage(void *, off_t, int);
+int            utvfu_get_bufsize(void *);
+int            utvfu_start_read(void *);
+
+usbd_status    utvfu_as_init(struct utvfu_softc *);
+void           utvfu_as_free(struct utvfu_softc *);
+
+usbd_status    utvfu_as_open(struct utvfu_softc *);
+usbd_status    utvfu_as_alloc_bulk(struct utvfu_softc *);
+void           utvfu_as_free_bulk(struct utvfu_softc *);
+int            utvfu_as_start_bulk(struct utvfu_softc *);
+void           utvfu_as_bulk_thread(void *);
+
+int            utvfu_audio_open(void *, int);
+void           utvfu_audio_close(void *);
+int            utvfu_audio_query_encoding(void *, struct audio_encoding *);
+int            utvfu_audio_set_params(void *, int, int,
+                   struct audio_params *, struct audio_params *);
+int            utvfu_audio_halt_out(void *);
+int            utvfu_audio_halt_in(void *);
+int            utvfu_audio_getdev(void *, struct audio_device *);
+int            utvfu_audio_mixer_set_port(void *, struct mixer_ctrl *);
+int            utvfu_audio_mixer_get_port(void *, struct mixer_ctrl *);
+int            utvfu_audio_query_devinfo(void *, struct mixer_devinfo *);
+int            utvfu_audio_get_props(void *);
+int            utvfu_audio_trigger_output(void *, void *, void *, int,
+                   void (*)(void *), void *, struct audio_params *);
+int            utvfu_audio_trigger_input(void *, void *, void *, int,
+                   void (*)(void *), void *, struct audio_params *);
+void           utvfu_audio_get_default_params(void *, int,
+                   struct audio_params *);
+
+#define        UTVFU_AUDIO_HAS_CLIENT(sc)      (NULL != (sc)->sc_audio.intr)
+#define        UTVFU_VIDEO_HAS_CLIENT(sc)      (NULL != (sc)->sc_iface.pipeh)
+
+
+struct cfdriver utvfu_cd = {
+       NULL, "utvfu", DV_DULL
+};
+
+const struct cfattach utvfu_ca = {
+       sizeof(struct utvfu_softc),
+       utvfu_match,
+       utvfu_attach,
+       utvfu_detach,
+       utvfu_activate
+};
+
+struct video_hw_if utvfu_vid_hw_if = {
+       utvfu_open,             /* open */
+       utvfu_close,            /* close */
+       utvfu_querycap,         /* VIDIOC_QUERYCAP */
+       utvfu_enum_fmt_vid_cap, /* VIDIOC_ENUM_FMT */
+       utvfu_enum_fsizes,      /* VIDIOC_ENUM_FRAMESIZES */
+       NULL,                   /* VIDIOC_ENUM_FRAMEINTERVALS */
+       utvfu_s_fmt,            /* VIDIOC_S_FMT */
+       utvfu_g_fmt,            /* VIDIOC_G_FMT */
+       utvfu_s_parm,           /* VIDIOC_S_PARM */
+       utvfu_g_parm,           /* VIDIOC_G_PARM */
+       utvfu_enum_input,       /* VIDIOC_ENUMINPUT */
+       utvfu_s_input,          /* VIDIOC_S_INPUT */
+       utvfu_g_input,          /* VIDIOC_G_INPUT */
+       utvfu_reqbufs,          /* VIDIOC_REQBUFS */
+       utvfu_querybuf,         /* VIDIOC_QUERYBUF */
+       utvfu_qbuf,             /* VIDIOC_QBUF */
+       utvfu_dqbuf,            /* VIDIOC_DQBUF */
+       utvfu_streamon,         /* VIDIOC_STREAMON */
+       utvfu_streamoff,        /* VIDIOC_STREAMOFF */
+       NULL,                   /* VIDIOC_TRY_FMT */
+       utvfu_queryctrl,        /* VIDIOC_QUERYCTRL */
+       NULL,                   /* VIDIOC_G_CTRL */
+       NULL,                   /* VIDIOC_S_CTRL */
+       utvfu_mappage,          /* mmap */
+       utvfu_get_bufsize,      /* read */
+       utvfu_start_read        /* start stream for read */
+};
+
+
+struct audio_device utvfu_audio_device = {
+       "UTVFU Audio",  /* name */
+       "",             /* version */
+       "utvfu"         /* config */
+};
+
+struct audio_hw_if utvfu_au_hw_if = {
+       utvfu_audio_open,               /* open hardware */
+       utvfu_audio_close,              /* close hardware */
+       NULL,                           /* Optional: drain buffers */
+       utvfu_audio_query_encoding,
+       utvfu_audio_set_params,
+       NULL,
+       NULL,
+       NULL,
+       NULL,
+       NULL,
+       NULL,
+       utvfu_audio_halt_out,
+       utvfu_audio_halt_in,
+       NULL,
+       utvfu_audio_getdev,
+       NULL,
+       utvfu_audio_mixer_set_port,
+       utvfu_audio_mixer_get_port,
+       utvfu_audio_query_devinfo,
+       NULL,
+       NULL,
+       NULL,
+       NULL,
+       utvfu_audio_get_props,
+       utvfu_audio_trigger_output,
+       utvfu_audio_trigger_input,
+       utvfu_audio_get_default_params
+};
+
+/* prototypes */
+int            utvfu_check_if_alt_cond(struct usbd_device *);
+usbd_status    utvfu_parse_desc(struct utvfu_softc *);
+
+int
+utvfu_match(struct device *parent, void *match, void *aux)
+{
+       struct usb_attach_arg *uaa = aux;
+       usb_device_descriptor_t *dd;
+       int ret = UMATCH_NONE;
+
+       if (uaa->iface == NULL)
+               return ret;
+
+       dd = usbd_get_device_descriptor(uaa->device);
+       if (dd == NULL)
+               return ret;
+
+       if (UGETW(dd->idVendor) == USB_VENDOR_FUSHICAI &&
+           UGETW(dd->idProduct) == USB_PRODUCT_FUSHICAI_USBTV007)
+               ret = UMATCH_VENDOR_PRODUCT;
+       /*
+        * XXX
+        * This seems like a fragile check, but the original driver ensures
+        * there are two alternate settings for the interface, and alternate
+        * setting 1 has four endpoints.
+        *
+        * Comment says "Checks that the device is what we think it is."
+        */
+       if (!utvfu_check_if_alt_cond(uaa->device))
+               ret = UMATCH_NONE;
+
+       return (ret);
+}
+
+void
+utvfu_attach(struct device *parent, struct device *self, void *aux)
+{
+       int i;
+       struct utvfu_softc *sc = (struct utvfu_softc *)self;
+       struct usb_attach_arg *uaa = aux;
+
+       sc->sc_udev = uaa->device;
+       for (i = 0; i < uaa->nifaces; i++) {
+               if (usbd_iface_claimed(sc->sc_udev, i))
+                       continue;
+               usbd_claim_iface(sc->sc_udev, i);
+       }
+
+       /* XXX What happens if this fails? Can this fail? */
+       (void)utvfu_parse_desc(sc);
+
+       /* set default video stream interface */
+       (void)usbd_set_interface(sc->sc_uifaceh, 0);
+
+       /* init mmap queue */
+       SIMPLEQ_INIT(&sc->sc_mmap_q);
+       sc->sc_mmap_count = 0;
+
+       sc->sc_max_frame_sz = utvfu_max_frame_size();
+
+       /* calculate optimal isoc xfer size */
+       if (strcmp(sc->sc_udev->bus->bdev.dv_cfdata->cf_driver->cd_name,
+           "ohci") == 0) {
+               /* ohci workaround */
+               sc->sc_nframes = 8;
+       } else {
+               sc->sc_nframes = (sc->sc_max_frame_sz
+                   + sc->sc_iface.psize - 1) / sc->sc_iface.psize;
+       }
+       if (sc->sc_nframes > UTVFU_NFRAMES_MAX)
+               sc->sc_nframes = UTVFU_NFRAMES_MAX;
+       DPRINTF(1, "%s: nframes=%d\n", DEVNAME(sc), sc->sc_nframes);
+
+       DPRINTF(1, "utvfu_attach: doing audio_attach_mi\n");
+       sc->sc_audiodev = audio_attach_mi(&utvfu_au_hw_if, sc, &sc->sc_dev);
+       DPRINTF(1, "utvfu_attach: audiodev=%p\n", sc->sc_audiodev);
+
+       DPRINTF(1, "utvfu_attach: doing video_attach_mi\n");
+       sc->sc_videodev = video_attach_mi(&utvfu_vid_hw_if, sc, &sc->sc_dev);
+       DPRINTF(1, "utvfu_attach: videodev=%p\n", sc->sc_videodev);
+}
+
+int
+utvfu_detach(struct device *self, int flags)
+{
+       struct utvfu_softc *sc = (struct utvfu_softc *)self;
+       int rv = 0;
+
+       /* Wait for outstanding requests to complete */
+       usbd_delay_ms(sc->sc_udev, UTVFU_NFRAMES_MAX); /* XXX meh? */
+
+       if (sc->sc_videodev != NULL)
+               rv = config_detach(sc->sc_videodev, flags);
+
+       if (sc->sc_audiodev != NULL)
+               rv += config_detach(sc->sc_audiodev, flags);
+
+       utvfu_as_free(sc);
+       utvfu_vs_free(sc);
+
+       return (rv);
+}
+
+int
+utvfu_activate(struct device *self, int act)
+{
+       struct utvfu_softc *sc = (struct utvfu_softc *)self;
+
+       DPRINTF(1, "%s: sc=%p\n", __func__, sc);
+
+       switch (act) {
+       case DVACT_DEACTIVATE:
+               if (sc->sc_videodev != NULL)
+                       config_deactivate(sc->sc_videodev);
+               if (sc->sc_audiodev != NULL)
+                       config_deactivate(sc->sc_audiodev);
+               usbd_deactivate(sc->sc_udev);
+               break;
+       }
+
+       return (0);
+}
+
+int
+utvfu_check_if_alt_cond(struct usbd_device *udev)
+{
+       int nep, nalt;
+       struct usbd_desc_iter iter;
+       struct usb_descriptor const *ud;
+       struct usb_interface_descriptor *uid = NULL;
+
+       nep = nalt = 0;
+       usbd_desc_iter_init(udev, &iter);
+       while (NULL != (ud = usbd_desc_iter_next(&iter))) {
+               switch (ud->bDescriptorType) {
+               default:
+                       break;
+               case UDESC_INTERFACE:
+                       uid = (void *)ud;
+                       if (0 == uid->bInterfaceNumber)
+                               nalt++;
+                       break;
+               case UDESC_ENDPOINT:
+                       if (1 == uid->bAlternateSetting)
+                               nep++;
+                       break;
+               }
+               if (NULL != uid && uid->bInterfaceNumber > 0)
+                       break;
+       }
+
+       return (2 == nalt && 4 == nep);
+}
+
+usbd_status
+utvfu_parse_desc(struct utvfu_softc *sc)
+{
+       int nif, nep;
+       uint32_t psize;
+       struct usbd_desc_iter iter;
+       const struct usb_descriptor *ud;
+       struct usb_endpoint_descriptor *ued;
+       struct usb_interface_descriptor *uid = NULL;
+
+       nif = nep = 0;
+       usbd_desc_iter_init(sc->sc_udev, &iter);
+       while (NULL != (ud = usbd_desc_iter_next(&iter))) {
+               if (ud->bDescriptorType != UDESC_INTERFACE)
+                       continue;
+               /* looking for interface 0, alt-setting 1 */
+               uid = (void *)ud;
+               if (uid->bInterfaceNumber > 0)
+                       break;
+               if (1 == uid->bAlternateSetting)
+                       break;
+       }
+       if (NULL == uid || 0 != uid->bInterfaceNumber ||
+           1 != uid->bAlternateSetting)
+               goto bad;
+
+       /* now looking for endpoint with maximum bandwidth */
+       while (NULL != (ud = usbd_desc_iter_next(&iter))) {
+               if (ud->bDescriptorType != UDESC_ENDPOINT)
+                       break;
+
+               ued = (void *)ud;
+               psize = UGETW(ued->wMaxPacketSize);
+               psize = UE_GET_SIZE(psize) * (1 + UE_GET_TRANS(psize));
+               if (psize > sc->sc_iface.psize) {
+                       /* bInterfaceNumber = 0 */
+                       sc->sc_uifaceh = &sc->sc_udev->ifaces[0];
+                       sc->sc_iface.endpoint = ued->bEndpointAddress;
+                       sc->sc_iface.psize = psize;
+               }
+       }
+
+       if (NULL != sc->sc_uifaceh)
+               return (USBD_NORMAL_COMPLETION);
+bad:
+       printf("%s: no valid alternate interface found!\n", DEVNAME(sc));
+       return (USBD_INVAL);
+}
+
+int
+utvfu_open(void *addr, int flags, int *size, uint8_t *buffer,
+    void (*intr)(void *), void *arg)
+{
+       struct utvfu_softc *sc = addr;
+
+       DPRINTF(1, "%s: utvfu_open: sc=%p\n", DEVNAME(sc), sc);
+
+       if (usbd_is_dying(sc->sc_udev))
+               return (EIO);
+
+       if (USBD_NORMAL_COMPLETION != utvfu_vs_init(sc) ||
+           USBD_NORMAL_COMPLETION != utvfu_as_init(sc))
+               return (EINVAL);
+
+       /* pointers to upper video layer */
+       sc->sc_uplayer_arg = arg;
+       sc->sc_uplayer_fsize = size;
+       sc->sc_uplayer_fbuffer = buffer;
+       sc->sc_uplayer_intr = intr;
+
+       sc->sc_mmap_flag = 0;
+
+       return (0);
+}
+
+int
+utvfu_close(void *addr)
+{
+       struct utvfu_softc *sc = addr;
+
+       DPRINTF(1, "%s: utvfu_close: sc=%p\n", DEVNAME(sc), sc);
+
+       /* free & clean up video stream */
+       utvfu_vs_free(sc);
+
+       return (0);
+}
+
+usbd_status
+utvfu_as_open(struct utvfu_softc *sc)
+{
+       usb_endpoint_descriptor_t *ed;
+       usbd_status error;
+
+       DPRINTF(1, "%s: %s\n", DEVNAME(sc), __func__);
+
+       if (NULL != sc->sc_audio.iface.pipeh) {
+               printf("%s: %s called while sc_audio.iface.pipeh not NULL\n",
+                   DEVNAME(sc), __func__);
+               return (USBD_INVAL);
+       }
+
+       ed = usbd_get_endpoint_descriptor(sc->sc_uifaceh,
+           UTVFU_AUDIO_ENDP);
+       if (ed == NULL) {
+               printf("%s: no endpoint descriptor for AS iface\n",
+                   DEVNAME(sc));
+               return (USBD_INVAL);
+       }
+       DPRINTF(1, "%s: open pipe for ", DEVNAME(sc));
+       DPRINTF(1, "bEndpointAddress=0x%02x (0x%02x), wMaxPacketSize="
+           "0x%04x (%d)\n",
+           UE_GET_ADDR(ed->bEndpointAddress),
+           UTVFU_AUDIO_ENDP,
+           UGETW(ed->wMaxPacketSize),
+           sc->sc_iface.psize);
+
+       error = usbd_open_pipe(
+           sc->sc_uifaceh,
+           UTVFU_AUDIO_ENDP,
+           USBD_EXCLUSIVE_USE,
+           &sc->sc_audio.iface.pipeh);
+       if (error != USBD_NORMAL_COMPLETION) {
+               printf("%s: could not open AS pipe: %s\n",
+                   DEVNAME(sc), usbd_errstr(error));
+       }
+
+       return (error);
+}
+
+usbd_status
+utvfu_vs_open(struct utvfu_softc *sc)
+{
+       usb_endpoint_descriptor_t *ed;
+       usbd_status error;
+
+       DPRINTF(1, "%s: %s\n", DEVNAME(sc), __func__);
+
+       if (NULL != sc->sc_iface.pipeh) {
+               printf("%s: %s called while sc_iface.pipeh not NULL\n",
+                   DEVNAME(sc), __func__);
+               return (USBD_INVAL);
+       }
+
+       ed = usbd_get_endpoint_descriptor(sc->sc_uifaceh,
+           sc->sc_iface.endpoint);
+       if (ed == NULL) {
+               printf("%s: no endpoint descriptor for VS iface\n",
+                   DEVNAME(sc));
+               return (USBD_INVAL);
+       }
+       DPRINTF(1, "%s: open pipe for ", DEVNAME(sc));
+       DPRINTF(1, "bEndpointAddress=0x%02x (0x%02x), wMaxPacketSize="
+           "0x%04x (%d)\n",
+           UE_GET_ADDR(ed->bEndpointAddress),
+           sc->sc_iface.endpoint,
+           UGETW(ed->wMaxPacketSize),
+           sc->sc_iface.psize);
+
+       error = usbd_open_pipe(
+           sc->sc_uifaceh,
+           sc->sc_iface.endpoint,
+           USBD_EXCLUSIVE_USE,
+           &sc->sc_iface.pipeh);
+       if (error != USBD_NORMAL_COMPLETION) {
+               printf("%s: could not open VS pipe: %s\n",
+                   DEVNAME(sc), usbd_errstr(error));
+       }
+
+       return (error);
+}
+
+void
+utvfu_as_close(struct utvfu_softc *sc)
+{
+       if (NULL != sc->sc_audio.iface.pipeh) {
+               usbd_abort_pipe(sc->sc_audio.iface.pipeh);
+               usbd_close_pipe(sc->sc_audio.iface.pipeh);
+               sc->sc_audio.iface.pipeh = NULL;
+       }
+}
+
+void
+utvfu_vs_close(struct utvfu_softc *sc)
+{
+       if (NULL != sc->sc_iface.pipeh) {
+               usbd_abort_pipe(sc->sc_iface.pipeh);
+               usbd_close_pipe(sc->sc_iface.pipeh);
+               sc->sc_iface.pipeh = NULL;
+       }
+
+       /*
+        * Some devices need time to shutdown before we switch back to
+        * the default interface (0).  Not doing so can leave the device
+        * back in a undefined condition.
+        */
+       usbd_delay_ms(sc->sc_udev, 100);
+
+       /* XXX What if audio is still streaming? */
+       /* switch back to default interface (turns off cam LED) */
+       (void)usbd_set_interface(sc->sc_uifaceh, 0);
+}
+
+void
+utvfu_read(struct utvfu_softc *sc, uint8_t *buf, int len)
+{
+       /*
+        * Copy video frame to upper layer buffer and call
+        * upper layer interrupt.
+        */
+       *sc->sc_uplayer_fsize = len;
+       bcopy(buf, sc->sc_uplayer_fbuffer, len);
+       (*sc->sc_uplayer_intr)(sc->sc_uplayer_arg);
+}
+
+int
+utvfu_as_start_bulk(struct utvfu_softc *sc)
+{
+       int error;
+
+       if (1 == sc->sc_as_running)
+               return (0);
+       if (NULL == sc->sc_audio.iface.pipeh)
+               return (ENXIO);
+
+       sc->sc_as_running = 1;
+       error = kthread_create(utvfu_as_bulk_thread, sc, NULL, DEVNAME(sc));
+       if (error) {
+               sc->sc_as_running = 0;
+               printf("%s: can't create kernel thread!", DEVNAME(sc));
+       }
+
+       return (error);
+}
+
+void
+utvfu_as_bulk_thread(void *arg)
+{
+       struct utvfu_softc *sc = arg;
+       struct utvfu_as_iface *iface;
+       usbd_status error;
+       uint32_t actlen;
+
+       iface = &sc->sc_audio.iface;
+       while (sc->sc_as_running) {
+               usbd_setup_xfer(
+                   iface->bxfer.xfer,
+                   iface->pipeh,
+                   0,
+                   iface->bxfer.buf,
+                   UTVFU_AUDIO_URBSIZE,
+                   USBD_NO_COPY | USBD_SHORT_XFER_OK | USBD_SYNCHRONOUS,
+                   0,
+                   NULL);
+               error = usbd_transfer(iface->bxfer.xfer);
+               if (error != USBD_NORMAL_COMPLETION) {
+                       DPRINTF(1, "%s: error in bulk xfer: %s!\n",
+                           DEVNAME(sc), usbd_errstr(error));
+                       break;
+               }
+
+               usbd_get_xfer_status(iface->bxfer.xfer, NULL, NULL, &actlen,
+                   NULL);
+               DPRINTF(2, "%s: *** buffer len = %d\n", DEVNAME(sc), actlen);
+
+               if (UTVFU_AUDIO_HAS_CLIENT(sc))
+                       utvfu_audio_decode(sc, actlen);
+       }
+
+       sc->sc_as_running = 0;
+
+       DPRINTF(1, "%s %s: exiting\n", DEVNAME(sc), __func__);
+
+       kthread_exit(0);
+}
+
+void
+utvfu_vs_start_isoc(struct utvfu_softc *sc)
+{
+       int i;
+       for (i = 0; i < UTVFU_ISOC_TRANSFERS; i++)
+               utvfu_vs_start_isoc_ixfer(sc, &sc->sc_iface.ixfer[i]);
+}
+
+void
+utvfu_vs_start_isoc_ixfer(struct utvfu_softc *sc,
+       struct utvfu_isoc_xfer *ixfer)
+{
+       int i;
+       usbd_status error;
+
+       DPRINTF(2, "%s: %s\n", DEVNAME(sc), __func__);
+
+       if (usbd_is_dying(sc->sc_udev))
+               return;
+
+       for (i = 0; i < sc->sc_nframes; i++)
+               ixfer->size[i] = sc->sc_iface.psize;
+
+       usbd_setup_isoc_xfer(
+           ixfer->xfer,
+           sc->sc_iface.pipeh,
+           ixfer,
+           ixfer->size,
+           sc->sc_nframes,
+           USBD_NO_COPY | USBD_SHORT_XFER_OK,
+           utvfu_vs_cb);
+
+       error = usbd_transfer(ixfer->xfer);
+       if (error && error != USBD_IN_PROGRESS) {
+               DPRINTF(1, "%s: usbd_transfer error=%s!\n",
+                   DEVNAME(sc), usbd_errstr(error));
+       }
+}
+
+/*
+ * Each packet contains a number of 256-byte chunks composing the image frame.
+ */
+void
+utvfu_vs_cb(struct usbd_xfer *xfer, void *priv, usbd_status status)
+{
+       struct utvfu_isoc_xfer *ixfer = priv;
+       struct utvfu_softc *sc = ixfer->sc;
+       int i, off, frame_size;
+       uint32_t actlen;
+       uint8_t *frame;
+
+       DPRINTF(2, "%s: %s\n", DEVNAME(sc), __func__);
+
+       if (status != USBD_NORMAL_COMPLETION) {
+               DPRINTF(1, "%s: %s: %s\n", DEVNAME(sc), __func__,
+                   usbd_errstr(status));
+               return;
+       }
+       usbd_get_xfer_status(xfer, NULL, NULL, &actlen, NULL);
+
+       DPRINTF(2, "%s: *** buffer len = %d\n", DEVNAME(sc), actlen);
+       if (actlen == 0)
+               goto skip;
+
+       for (i = 0; i < sc->sc_nframes; i++) {
+               frame = ixfer->buf + (i * sc->sc_iface.psize);
+               frame_size = ixfer->size[i];
+
+               if (frame_size == 0)
+                       /* frame is empty */
+                       continue;
+
+               #define CHUNK_STRIDE    (UTVFU_CHUNK_SIZE*4)
+               for (off = 0; off + CHUNK_STRIDE <= frame_size;
+                    off += CHUNK_STRIDE) {
+                       utvfu_image_chunk(sc, frame + off);
+               }
+               #undef  CHUNK_STRIDE
+       }
+
+skip:  /* setup new transfer */
+       utvfu_vs_start_isoc_ixfer(sc, ixfer);
+}
+
+int
+utvfu_find_queued(struct utvfu_softc *sc)
+{
+       int     i;
+
+       /* find a buffer which is ready for queueing */
+       for (i = 0; i < sc->sc_mmap_count; i++) {
+               if (sc->sc_mmap[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
+                       continue;
+               if (sc->sc_mmap[i].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)
+                       return (i);
+       }
+       return (-1);
+}
+
+int
+utvfu_mmap_queue(struct utvfu_softc *sc, uint8_t *buf, int len)
+{
+       int i;
+
+       if (sc->sc_mmap_count == 0 || sc->sc_mmap_buffer == NULL)
+               panic("%s: mmap buffers not allocated", __func__);
+
+       /* find a buffer which is ready for queueing */
+       if (-1 == (i = utvfu_find_queued(sc))) {
+               DPRINTF(2, "%s: mmap queue is full!\n", DEVNAME(sc));
+               return (ENOMEM);
+       }
+
+       /* copy frame to mmap buffer and report length */
+       memcpy(sc->sc_mmap[i].buf, buf, len);
+       sc->sc_mmap[i].v4l2_buf.bytesused = len;
+
+       /* timestamp it */
+       getmicrotime(&sc->sc_mmap[i].v4l2_buf.timestamp);
+
+       /* appropriately set/clear flags */
+       sc->sc_mmap[i].v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
+       sc->sc_mmap[i].v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
+
+       /* queue it */
+       SIMPLEQ_INSERT_TAIL(&sc->sc_mmap_q, &sc->sc_mmap[i], q_frames);
+       DPRINTF(2, "%s: %s: frame queued on index %d\n",
+           DEVNAME(sc), __func__, i);
+
+       wakeup(sc);
+
+       /*
+        * In case userland uses poll(2), signal that we have a frame
+        * ready to dequeue.
+        */
+       (*sc->sc_uplayer_intr)(sc->sc_uplayer_arg);
+
+       return (0);
+}
+
+caddr_t
+utvfu_mappage(void *v, off_t off, int prot)
+{
+       struct utvfu_softc *sc = v;
+       caddr_t p = NULL;
+
+       if (off < sc->sc_mmap_bufsz) {
+               if (0 == sc->sc_mmap_flag)
+                       sc->sc_mmap_flag = 1;
+
+               p = sc->sc_mmap_buffer + off;
+       }
+
+       return (p);
+}
+
+int
+utvfu_get_bufsize(void *v)
+{
+       struct utvfu_softc *sc = v;
+       /* YUYV/YUV-422: 4 bytes/2 pixel */
+       return (utvfu_norm_params[sc->sc_normi].cap_width *
+           utvfu_norm_params[sc->sc_normi].cap_height * 2);
+}
+
+int
+utvfu_start_read(void *v)
+{
+       struct utvfu_softc *sc = v;
+       usbd_status error;
+
+       if (0 != sc->sc_mmap_flag)
+               sc->sc_mmap_flag = 0;
+
+       /* open video stream pipe */
+       error = utvfu_vs_open(sc);
+       if (error != USBD_NORMAL_COMPLETION)
+               return (EINVAL);
+
+       utvfu_vs_start_isoc(sc);
+
+       return (0);
+}
+
+void
+utvfu_audio_clear_client(struct utvfu_softc *sc)
+{
+       sc->sc_audio.intr = NULL;
+       sc->sc_audio.intr_arg = NULL;
+       sc->sc_audio.start = NULL;
+       sc->sc_audio.end = NULL;
+       sc->sc_audio.cur = NULL;
+       sc->sc_audio.blksize = 0;
+}
+
+void
+utvfu_as_free(struct utvfu_softc *sc)
+{
+       DPRINTF(1, "%s: %s\n", DEVNAME(sc), __func__);
+
+       /* Leave the audio thread running if video is streaming */
+       if (!UTVFU_VIDEO_HAS_CLIENT(sc)) {
+               utvfu_as_close(sc);
+               utvfu_as_free_bulk(sc);
+       }
+
+       utvfu_audio_clear_client(sc);
+}
+
+void
+utvfu_vs_free(struct utvfu_softc *sc)
+{
+       DPRINTF(1, "%s: %s\n", DEVNAME(sc), __func__);
+       utvfu_vs_close(sc);
+       utvfu_vs_free_isoc(sc);
+       utvfu_vs_free_frame(sc);
+}
+
+usbd_status
+utvfu_as_init(struct utvfu_softc *sc)
+{
+       usbd_status error;
+
+       DPRINTF(1, "%s: %s\n", DEVNAME(sc), __func__);
+
+       if (NULL != sc->sc_audio.iface.bxfer.xfer)
+               goto out;
+
+       /* allocate audio and video stream xfer buffer */
+       error = utvfu_as_alloc_bulk(sc);
+       if (error != USBD_NORMAL_COMPLETION)
+               return (USBD_NOMEM);
+out:
+       return (USBD_NORMAL_COMPLETION);
+}
+
+usbd_status
+utvfu_vs_init(struct utvfu_softc *sc)
+{
+       usbd_status error;
+
+       DPRINTF(1, "%s: %s\n", DEVNAME(sc), __func__);
+
+       if (0 != utvfu_start_capture(sc))
+               return (USBD_INVAL);
+
+       error = utvfu_vs_alloc_isoc(sc);
+       if (error != USBD_NORMAL_COMPLETION)
+               return (error);
+
+       /* allocate video stream frame buffer */
+       error = utvfu_vs_alloc_frame(sc);
+       if (error != USBD_NORMAL_COMPLETION)
+               return (error);
+
+       return (USBD_NORMAL_COMPLETION);
+}
+
+usbd_status
+utvfu_vs_alloc_frame(struct utvfu_softc *sc)
+{
+       struct utvfu_frame_buf *fb = &sc->sc_fb;
+
+       fb->size = sc->sc_max_frame_sz;
+       fb->buf = malloc(fb->size, M_DEVBUF, M_NOWAIT);
+       if (fb->buf == NULL) {
+               printf("%s: can't allocate frame buffer!\n", DEVNAME(sc));
+               return (USBD_NOMEM);
+       }
+
+       DPRINTF(1, "%s: %s: allocated %d bytes frame buffer\n",
+           DEVNAME(sc), __func__, fb->size);
+
+       fb->chunks_done = 0;
+       fb->fid = 0;
+       fb->last_odd = 1;
+
+       return (USBD_NORMAL_COMPLETION);
+}
+
+void
+utvfu_vs_free_frame(struct utvfu_softc *sc)
+{
+       struct utvfu_frame_buf *fb = &sc->sc_fb;
+
+       if (fb->buf != NULL) {
+               free(fb->buf, M_DEVBUF, fb->size);
+               fb->buf = NULL;
+       }
+
+       if (sc->sc_mmap_buffer != NULL) {
+               free(sc->sc_mmap_buffer, M_DEVBUF, sc->sc_mmap_bufsz);
+               sc->sc_mmap_buffer = NULL;
+               memset(sc->sc_mmap, 0, sizeof(sc->sc_mmap));
+       }
+
+       while (!SIMPLEQ_EMPTY(&sc->sc_mmap_q))
+               SIMPLEQ_REMOVE_HEAD(&sc->sc_mmap_q, q_frames);
+
+       sc->sc_mmap_count = 0;
+}
+
+usbd_status
+utvfu_vs_alloc_isoc(struct utvfu_softc *sc)
+{
+       int size, i;
+
+       DPRINTF(1, "%s: %s\n", DEVNAME(sc), __func__);
+
+       for (i = 0; i < UTVFU_ISOC_TRANSFERS; i++) {
+               sc->sc_iface.ixfer[i].sc = sc;
+               sc->sc_iface.ixfer[i].xfer = usbd_alloc_xfer(sc->sc_udev);      
+               if (sc->sc_iface.ixfer[i].xfer == NULL) {
+                       printf("%s: could not allocate isoc VS xfer!\n",
+                           DEVNAME(sc));
+                       return (USBD_NOMEM);    
+               }
+
+               size = sc->sc_iface.psize * sc->sc_nframes;
+
+               sc->sc_iface.ixfer[i].buf =
+                   usbd_alloc_buffer(sc->sc_iface.ixfer[i].xfer, size);
+               if (sc->sc_iface.ixfer[i].buf == NULL) {
+                       printf("%s: could not allocate isoc VS buffer!\n",
+                           DEVNAME(sc));
+                       return (USBD_NOMEM);
+               }
+               DPRINTF(1, "%s: allocated %d bytes isoc VS xfer buffer\n",
+                   DEVNAME(sc), size);
+       }
+
+       return (USBD_NORMAL_COMPLETION);
+}
+
+usbd_status
+utvfu_as_alloc_bulk(struct utvfu_softc *sc)
+{
+       struct utvfu_bulk_xfer *xfer = &sc->sc_audio.iface.bxfer;
+
+       xfer->xfer = usbd_alloc_xfer(sc->sc_udev);
+       if (xfer->xfer == NULL) {
+               printf("%s: could not allocate bulk AUDIO xfer!\n",
+                   DEVNAME(sc));
+               return (USBD_NOMEM);
+       }
+
+       xfer->buf = usbd_alloc_buffer(xfer->xfer, UTVFU_AUDIO_URBSIZE);
+       if (xfer->buf == NULL) {
+               utvfu_as_free_bulk(sc);
+               printf("%s: could not allocate bulk AUDIO buffer!\n",
+                   DEVNAME(sc));
+               return (USBD_NOMEM);
+       }
+       DPRINTF(1, "%s: allocated %d bytes bulk AUDIO xfer buffer\n",
+           DEVNAME(sc), UTVFU_AUDIO_URBSIZE);
+
+       return (USBD_NORMAL_COMPLETION);
+}
+
+void
+utvfu_vs_free_isoc(struct utvfu_softc *sc)
+{
+       int i;
+
+       DPRINTF(1, "%s: %s\n", DEVNAME(sc), __func__);
+
+       for (i = 0; i < UTVFU_ISOC_TRANSFERS; i++) {
+               if (sc->sc_iface.ixfer[i].buf != NULL) {
+                       usbd_free_buffer(sc->sc_iface.ixfer[i].xfer);
+                       sc->sc_iface.ixfer[i].buf = NULL;
+               }
+
+               if (sc->sc_iface.ixfer[i].xfer != NULL) {
+                       usbd_free_xfer(sc->sc_iface.ixfer[i].xfer);
+                       sc->sc_iface.ixfer[i].xfer = NULL;
+               }
+       }
+}
+
+void
+utvfu_as_free_bulk(struct utvfu_softc *sc)
+{
+       if (sc->sc_audio.iface.bxfer.buf != NULL) {
+               usbd_free_buffer(sc->sc_audio.iface.bxfer.xfer);
+               sc->sc_audio.iface.bxfer.buf = NULL;
+       }
+
+       if (sc->sc_audio.iface.bxfer.xfer != NULL) {
+               usbd_free_xfer(sc->sc_audio.iface.bxfer.xfer);
+               sc->sc_audio.iface.bxfer.xfer = NULL;
+       }
+}
+
+int
+utvfu_reqbufs(void *v, struct v4l2_requestbuffers *rb)
+{
+       struct utvfu_softc *sc = v;
+       int i;
+
+       DPRINTF(1, "%s: %s: count=%d\n", DEVNAME(sc), __func__, rb->count);
+
+       /* We do not support freeing buffers via reqbufs(0) */
+       if (rb->count == 0)
+               return (EINVAL);
+
+       if (sc->sc_mmap_count > 0 || sc->sc_mmap_buffer != NULL) {
+               DPRINTF(1, "%s: %s: mmap buffers already allocated\n",
+                   DEVNAME(sc), __func__);
+               return (EINVAL);
+       }
+
+       /* limit the buffers */
+       if (rb->count > UTVFU_MAX_BUFFERS)
+               sc->sc_mmap_count = UTVFU_MAX_BUFFERS;
+       else
+               sc->sc_mmap_count = rb->count;
+
+       /* allocate the total mmap buffer */    
+       sc->sc_mmap_bufsz = sc->sc_max_frame_sz;
+       if (INT_MAX / sc->sc_mmap_count < sc->sc_max_frame_sz) /* overflow */
+               return (ENOMEM);
+       sc->sc_mmap_bufsz *= sc->sc_mmap_count;
+       sc->sc_mmap_bufsz = round_page(sc->sc_mmap_bufsz); /* page align */
+       sc->sc_mmap_buffer = malloc(sc->sc_mmap_bufsz, M_DEVBUF, M_NOWAIT);
+       if (sc->sc_mmap_buffer == NULL) {
+               printf("%s: can't allocate mmap buffer!\n", DEVNAME(sc));
+               return (ENOMEM);
+       }
+       DPRINTF(1, "%s: allocated %d bytes mmap buffer\n",
+           DEVNAME(sc), sc->sc_mmap_bufsz);
+
+       /* fill the v4l2_buffer structure */
+       for (i = 0; i < sc->sc_mmap_count; i++) {
+               sc->sc_mmap[i].buf = sc->sc_mmap_buffer
+                   + (i * sc->sc_max_frame_sz);
+               sc->sc_mmap[i].v4l2_buf.index = i;
+               sc->sc_mmap[i].v4l2_buf.m.offset = i * sc->sc_max_frame_sz;
+               sc->sc_mmap[i].v4l2_buf.length = sc->sc_max_frame_sz;
+               sc->sc_mmap[i].v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+               sc->sc_mmap[i].v4l2_buf.sequence = 0;
+               sc->sc_mmap[i].v4l2_buf.field = V4L2_FIELD_NONE;
+               sc->sc_mmap[i].v4l2_buf.memory = V4L2_MEMORY_MMAP;
+               sc->sc_mmap[i].v4l2_buf.flags = V4L2_BUF_FLAG_MAPPED;
+
+               DPRINTF(1, "%s: %s: index=%d, offset=%d, length=%d\n",
+                   DEVNAME(sc), __func__,
+                   sc->sc_mmap[i].v4l2_buf.index,
+                   sc->sc_mmap[i].v4l2_buf.m.offset,
+                   sc->sc_mmap[i].v4l2_buf.length);
+       }
+
+       /* tell how many buffers we have really allocated */
+       rb->count = sc->sc_mmap_count;
+
+       return (0);
+}
+
+int
+utvfu_querybuf(void *v, struct v4l2_buffer *qb)
+{
+       struct utvfu_softc *sc = v;
+
+       if (qb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
+           qb->memory != V4L2_MEMORY_MMAP ||
+           qb->index >= sc->sc_mmap_count)
+               return (EINVAL);
+
+       memcpy(qb, &sc->sc_mmap[qb->index].v4l2_buf,
+           sizeof(struct v4l2_buffer));
+
+       DPRINTF(1, "%s: %s: index=%d, offset=%d, length=%d\n",
+           DEVNAME(sc), __func__, qb->index, qb->m.offset, qb->length);
+
+       return (0);
+}
+
+int
+utvfu_qbuf(void *v, struct v4l2_buffer *qb)
+{
+       struct utvfu_softc *sc = v;
+
+       if (qb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
+           qb->memory != V4L2_MEMORY_MMAP ||
+           qb->index >= sc->sc_mmap_count)
+               return (EINVAL);
+
+       sc->sc_mmap[qb->index].v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
+       sc->sc_mmap[qb->index].v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
+       sc->sc_mmap[qb->index].v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
+
+       DPRINTF(2, "%s: %s: buffer on index %d ready for queueing\n",
+           DEVNAME(sc), __func__, qb->index);
+
+       return (0);
+}
+
+int
+utvfu_dqbuf(void *v, struct v4l2_buffer *dqb)
+{
+       struct utvfu_softc *sc = v;
+       struct utvfu_mmap *mmap;
+       int error;
+
+       if (dqb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
+           dqb->memory != V4L2_MEMORY_MMAP)
+               return (EINVAL);
+
+       if (SIMPLEQ_EMPTY(&sc->sc_mmap_q)) {
+               /* mmap queue is empty, block until first frame is queued */
+               error = tsleep(sc, 0, "vid_mmap", 10 * hz);
+               if (error)
+                       return (EINVAL);
+       }
+
+       mmap = SIMPLEQ_FIRST(&sc->sc_mmap_q);
+       if (mmap == NULL)
+               panic("utvfu_dqbuf: NULL pointer!");
+
+       memcpy(dqb, &mmap->v4l2_buf, sizeof(struct v4l2_buffer));
+
+       mmap->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_DONE|V4L2_BUF_FLAG_QUEUED);
+       mmap->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
+
+       DPRINTF(2, "%s: %s: frame dequeued from index %d\n",
+           DEVNAME(sc), __func__, mmap->v4l2_buf.index);
+       SIMPLEQ_REMOVE_HEAD(&sc->sc_mmap_q, q_frames);
+
+       return (0);
+}
+
+int
+utvfu_streamon(void *v, int type)
+{
+       struct utvfu_softc *sc = v;
+       usbd_status error;
+
+       /* open video stream pipe */
+       error = utvfu_vs_open(sc);
+       if (error != USBD_NORMAL_COMPLETION)
+               return (EINVAL);
+
+       utvfu_vs_start_isoc(sc);
+
+       return (0);
+}
+
+int
+utvfu_streamoff(void *v, int type)
+{
+       utvfu_vs_close(v);
+       return (0);
+}
+
+int
+utvfu_queryctrl(void *v, struct v4l2_queryctrl *qctrl)
+{
+       qctrl->flags = V4L2_CTRL_FLAG_DISABLED;
+       return (0);
+}
+
+int
+utvfu_g_parm(void *v, struct v4l2_streamparm *parm)
+{
+       struct utvfu_softc *sc = v;
+
+       if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
+               return (EINVAL);
+       /*
+        * XXX Unsure whether there is a way to negotiate this with the
+        * device, but returning 0 will allow xenocara's video to run
+        */
+       switch (utvfu_norm_params[sc->sc_normi].norm) {
+       default:
+               return (EINVAL);
+       case V4L2_STD_525_60:
+               parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
+               parm->parm.capture.capturemode = 0;
+               parm->parm.capture.timeperframe.numerator = 30;
+               parm->parm.capture.timeperframe.denominator = 1;
+               break;
+       case V4L2_STD_PAL:
+               parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
+               parm->parm.capture.capturemode = 0;
+               parm->parm.capture.timeperframe.numerator = 25;
+               parm->parm.capture.timeperframe.denominator = 1;
+               break;
+       }
+       return (0);
+}
+
+int
+utvfu_s_parm(void *v, struct v4l2_streamparm *parm)
+{
+       if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
+               return (EINVAL);
+       return (0);
+}
+
+/*
+ * A U D I O   O P S
+ */
+
+int
+utvfu_audio_open(void *v, int flags)
+{
+       struct utvfu_softc *sc = v;
+
+       if (usbd_is_dying(sc->sc_udev))
+               return (EIO);
+
+       if ((flags & FWRITE))
+               return (ENXIO);
+
+       if (USBD_NORMAL_COMPLETION != utvfu_as_init(sc))
+               return (EINVAL);
+
+       return (0);
+}
+
+void
+utvfu_audio_close(void *v)
+{
+       utvfu_as_free(v);
+}
+
+int
+utvfu_audio_query_encoding(void *v, struct audio_encoding *p)
+{
+       struct utvfu_softc *sc = v;
+
+       if (usbd_is_dying(sc->sc_udev))
+               return (EIO);
+
+       DPRINTF(1, "%s %s\n", DEVNAME(sc), __func__);
+
+       if (p->index != 0)
+               return (EINVAL);
+
+       strlcpy(p->name, AudioEslinear_le, sizeof(p->name));
+       p->encoding = AUDIO_ENCODING_SLINEAR_LE;
+       p->precision = 16;
+       p->bps = 2;
+       p->msb = 1;
+       p->flags = 0;
+
+       return (0);
+}
+
+int
+utvfu_audio_set_params(void *v, int setmode, int usemode,
+    struct audio_params *play, struct audio_params *rec)
+{
+       struct utvfu_softc *sc = v;
+
+       if (usbd_is_dying(sc->sc_udev))
+               return (EIO);
+
+       DPRINTF(1, "%s %s\n", DEVNAME(sc), __func__);
+
+       /* XXX ? */
+       play->sample_rate = 0;
+       play->encoding = AUDIO_ENCODING_NONE;
+
+       rec->sample_rate = 48000;
+       rec->encoding = AUDIO_ENCODING_SLINEAR_LE;
+       rec->precision = 16;
+       rec->bps = 2;
+       rec->msb = 1;
+       rec->channels = 2;
+
+       return (0);
+}
+
+int
+utvfu_audio_halt_out(void *v)
+{
+       return (EIO);
+}
+
+int
+utvfu_audio_halt_in(void *v)
+{
+       struct utvfu_softc *sc = v;
+
+       if (usbd_is_dying(sc->sc_udev))
+               return (EIO);
+
+       DPRINTF(1, "%s: %s\n", DEVNAME(sc), __func__);
+
+       if (!UTVFU_VIDEO_HAS_CLIENT(sc))
+               utvfu_audio_stop(sc);
+
+       utvfu_audio_clear_client(sc);
+
+       return (0);
+}
+
+int
+utvfu_audio_getdev(void *v, struct audio_device *retp)
+{
+       struct utvfu_softc *sc = v;
+
+       if (usbd_is_dying(sc->sc_udev))
+               return (EIO);
+
+       *retp = utvfu_audio_device;
+       return (0);
+}
+
+int
+utvfu_audio_mixer_set_port(void *v, struct mixer_ctrl *cp)
+{
+       struct utvfu_softc *sc = v;
+
+       if (usbd_is_dying(sc->sc_udev))
+               return (EIO);
+
+       DPRINTF(1, "%s %s\n", DEVNAME(sc), __func__);
+
+       if (AUDIO_MIXER_ENUM != cp->type ||
+           0 > cp->un.ord || cp->un.ord > 1)
+               return (EINVAL);
+/* XXX TODO */
+       DPRINTF(1, "%s %s: cp->un.ord=%d\n", DEVNAME(sc), __func__, cp->un.ord);
+       return (0);
+}
+
+int
+utvfu_audio_mixer_get_port(void *v, struct mixer_ctrl *cp)
+{
+       struct utvfu_softc *sc = v;
+
+       if (usbd_is_dying(sc->sc_udev))
+               return (EIO);
+
+       DPRINTF(1, "%s %s\n", DEVNAME(sc), __func__);
+
+       if (AUDIO_MIXER_ENUM != cp->type ||
+           0 > cp->un.ord || cp->un.ord > 1)
+               return (EINVAL);
+/* XXX TODO */
+       DPRINTF(1, "%s %s: cp->un.ord=%d\n", DEVNAME(sc), __func__, cp->un.ord);
+       return (0);
+}
+
+int
+utvfu_audio_query_devinfo(void *v, struct mixer_devinfo *mi)
+{
+       struct utvfu_softc *sc = v;
+
+       if (usbd_is_dying(sc->sc_udev))
+               return (EIO);
+
+       DPRINTF(1, "%s %s\n", DEVNAME(sc), __func__);
+
+       if (mi->index != 0)
+               return (EINVAL);
+
+/* XXX SOMEONE WITH AUDIO EXPERTIZE NEEDS TO HELP HERE */
+       strlcpy(mi->label.name, "mix0-i0", sizeof(mi->label.name));
+       mi->type = AUDIO_MIXER_ENUM;
+       mi->un.e.num_mem = 2;
+       mi->un.e.member[0].ord = 0;
+       strlcpy(mi->un.e.member[0].label.name, AudioNoff,
+           sizeof(mi->un.e.member[0].label.name));
+       mi->un.e.member[1].ord = 1;
+       strlcpy(mi->un.e.member[1].label.name, AudioNon,
+           sizeof(mi->un.e.member[1].label.name));
+
+       return (0);
+}
+
+int
+utvfu_audio_get_props(void *v)
+{
+       return (0);
+}
+
+void
+utvfu_audio_get_default_params(void *v, int mode, struct audio_params *p)
+{
+       if (AUMODE_RECORD != mode)
+               return;
+
+       DPRINTF(1, "%s %s\n", DEVNAME((struct utvfu_softc *)v), __func__);
+
+       p->sample_rate = 48000;
+       p->encoding = AUDIO_ENCODING_SLINEAR_LE;
+       p->precision = 16;
+       p->bps = 2;
+       p->msb = 1;
+       p->channels = 2;
+}
+
+int
+utvfu_audio_trigger_output(void *v, void *start, void *end, int blksize,
+    void (*intr)(void *), void *arg, struct audio_params *param)
+{
+       return (EIO);
+}
+
+int
+utvfu_audio_trigger_input(void *v, void *start, void *end, int blksize,
+    void (*intr)(void *), void *arg, struct audio_params *param)
+{
+       struct utvfu_softc *sc = v;
+
+       if (usbd_is_dying(sc->sc_udev))
+               return (EIO);
+
+       if (UTVFU_AUDIO_HAS_CLIENT(sc))
+               return (EBUSY);
+
+       sc->sc_audio.intr_arg = arg;
+       sc->sc_audio.intr = intr;
+       sc->sc_audio.start = start;
+       sc->sc_audio.end = end;
+       sc->sc_audio.cur = start;
+       sc->sc_audio.blksize = blksize;
+
+       DPRINTF(1, "%s %s: start=%p end=%p diff=%lu blksize=%d\n",
+           DEVNAME(sc), __func__, start, end,
+           ((u_char *)end - (u_char *)start), blksize);
+
+       return utvfu_audio_start(sc);
+}
+
+int
+utvfu_audio_start(struct utvfu_softc *sc)
+{
+       if (1 == sc->sc_as_running)
+               return (0);
+
+       utvfu_audio_start_chip(sc);
+
+       if (NULL == sc->sc_audio.iface.pipeh) {
+               if (USBD_NORMAL_COMPLETION != utvfu_as_open(sc))
+                       return (ENOMEM);
+       }
+
+       return utvfu_as_start_bulk(sc);
+}
+
+int
+utvfu_audio_stop(struct utvfu_softc *sc)
+{
+       if (1 == sc->sc_as_running) {
+               utvfu_audio_stop_chip(sc);
+               utvfu_as_close(sc);
+       }
+       return (0);
+}




Index: video.c
===================================================================
RCS file: /cvs/obsd/xenocara/app/video/video.c,v
retrieving revision 1.12
diff -u -p -u -p -r1.12 video.c
--- video.c     23 Oct 2014 07:36:06 -0000      1.12
+++ video.c     26 May 2016 22:42:33 -0000
@@ -17,6 +17,7 @@
 
 #include <sys/ioctl.h>
 #include <sys/types.h>
+#include <sys/mman.h>
 #include <sys/videoio.h>
 #include <sys/time.h>
 #include <sys/limits.h>
@@ -142,6 +143,11 @@ struct encodings {
        { NULL, 0, 0, 0, 0 }
 };
 
+struct mmap_buf {
+       void    *p;
+       size_t  sz;
+};
+
 struct video {
        struct xdsp      xdsp;
        struct dev       dev;
@@ -167,6 +173,8 @@ struct video {
 #define M_OUT_FILE     0x8
        int              mode;
        int              verbose;
+       struct mmap_buf *mmap_buf;
+       int              mmap_count;
 };
 
 int xv_get_info(struct video *);
@@ -195,6 +203,10 @@ int poll_input(struct video *);
 int grab_frame(struct video *);
 int stream(struct video *);
 
+int init_mmap_buffers(struct video *);
+void free_mmap_buffers(struct video *);
+int grab_mmap_frame(struct video *);
+
 void got_frame(int);
 void got_shutdown(int);
 int find_enc(char *);
@@ -687,7 +699,8 @@ dev_get_encs(struct video *vid)
        fmtdesc.index = 0;
        fmtdesc.type = d->buf_type;
        while (ioctl(d->fd, VIDIOC_ENUM_FMT, &fmtdesc) >= 0) {
-               if (!strcmp(fmtdesc.description, "YUYV")) {
+               if (!strcmp(fmtdesc.description, "YUYV") ||
+                   V4L2_PIX_FMT_YUYV == fmtdesc.pixelformat) {
                        i = find_enc("yuy2");
                        if (i < ENC_LAST)
                                encs[i].dev_id = fmtdesc.pixelformat;
@@ -1341,6 +1354,174 @@ poll_input(struct video *vid)
 }
 
 int
+init_mmap_buffers(struct video *vid)
+{
+       int             i;
+       struct dev      *d = &vid->dev;
+       struct v4l2_requestbuffers /* {
+               __u32                   count;
+               __u32                   type;           /# enum v4l2_buf_type #/
+               __u32                   memory;         /# enum v4l2_memory #/
+               __u32                   reserved[2];
+       } */            rb;
+       struct v4l2_buffer /* {
+               __u32                   index;
+               __u32                   type;
+               __u32                   bytesused;
+               __u32                   flags;
+               __u32                   field;
+               struct timeval          timestamp;
+               struct v4l2_timecode    timecode;
+               __u32                   sequence;
+
+               /# memory location #/
+               __u32                   memory;
+               union {
+                       __u32           offset;
+                       unsigned long   userptr;
+                       struct v4l2_plane *planes;
+                       __s32           fd;
+               } m;
+               __u32                   length;
+               __u32                   reserved2;
+               __u32                   reserved;
+       } */            qb;
+
+       rb.count = 20;
+       rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+       rb.memory = V4L2_MEMORY_MMAP;
+
+       if (ioctl(d->fd, VIDIOC_REQBUFS, &rb) < 0) {
+               warn("VIDIOC_REQBUFS");
+               return 0;
+       }
+       vid->mmap_count = rb.count;
+       vid->mmap_buf = calloc(vid->mmap_count, sizeof(vid->mmap_buf[0]));
+       if (NULL == vid->mmap_buf) {
+               warn("mmap_buf");
+               vid->mmap_count = 0;
+               return 0;
+       }
+       for (i = 0; i < vid->mmap_count; ++i ) {
+               qb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+               qb.memory = V4L2_MEMORY_MMAP;
+               qb.index = i;
+               if (ioctl(d->fd, VIDIOC_QUERYBUF, &qb) < 0) {
+                       warn("VIDIOC_QUERYBUF");
+                       return 0;
+               }
+               vid->mmap_buf[i].p = mmap(NULL, qb.length, PROT_READ,
+                   MAP_SHARED, d->fd, qb.m.offset);
+               if (MAP_FAILED == vid->mmap_buf[i].p) {
+                       err(1, "mmap");
+                       /* XXX */
+               }
+               vid->mmap_buf[i].sz = qb.length;
+       }
+
+       /* queue buffers */
+       for (i = 0; i < vid->mmap_count; ++i) {
+               qb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+               qb.memory = V4L2_MEMORY_MMAP;
+               qb.index = i;
+               if (ioctl(d->fd, VIDIOC_QBUF, &qb) < 0) {
+                       warn("VIDIOC_QBUF");
+                       return 0;
+               }
+       }
+
+       /* start stream */
+       rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+       if (ioctl(d->fd, VIDIOC_STREAMON, &rb.type) < 0) {
+               warn("VIDIOC_STREAMON");
+               return 0;
+       }
+       return 1;
+}
+
+void
+free_mmap_buffers(struct video *vid)
+{
+       int             i, type;
+       struct dev      *d = &vid->dev;
+
+       if (0 == vid->mmap_count)
+               return;
+
+       /* stop stream */
+       type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+       if (ioctl(d->fd, VIDIOC_STREAMOFF, &type) < 0) {
+               warn("VIDIOC_STREAMOFF");
+       }
+
+       for (i = 0; i < vid->mmap_count; ++i ) {
+               if (MAP_FAILED == vid->mmap_buf[i].p ||
+                   NULL == vid->mmap_buf[i].p)
+                       continue;
+               if (munmap(vid->mmap_buf[i].p, vid->mmap_buf[i].sz) < 0)
+                       err(1, "munmap");
+       }
+       free(vid->mmap_buf);
+       vid->mmap_buf = NULL;
+       vid->mmap_count = 0;
+}
+
+int
+grab_mmap_frame(struct video *vid)
+{
+       int             n;
+       struct dev      *d = &vid->dev;
+       struct v4l2_buffer /* {
+               __u32                   index;
+               __u32                   type;
+               __u32                   bytesused;
+               __u32                   flags;
+               __u32                   field;
+               struct timeval          timestamp;
+               struct v4l2_timecode    timecode;
+               __u32                   sequence;
+
+               /# memory location #/
+               __u32                   memory;
+               union {
+                       __u32           offset;
+                       unsigned long   userptr;
+                       struct v4l2_plane *planes;
+                       __s32           fd;
+               } m;
+               __u32                   length;
+               __u32                   reserved2;
+               __u32                   reserved;
+       } */            dqb;
+
+       dqb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+       dqb.memory = V4L2_MEMORY_MMAP;
+
+       if (ioctl(d->fd, VIDIOC_DQBUF, &dqb) < 0) {
+               warn("VIDIOC_DQBUF");
+               return 0;
+       }
+
+       /*
+        * XXX TODO: Use direct buffer for swab() and XvPutImage() instead
+        * of memcpy, swab() ...
+        */
+       n = (vid->bpf < dqb.bytesused) ? vid->bpf : dqb.bytesused;
+       memcpy(vid->frame_buffer, vid->mmap_buf[dqb.index].p, n);
+
+       if (vid->verbose > 1)
+               fprintf(stderr, "\rmmap_buf[%d] len=%d ", dqb.index, n);
+
+       /* re-queue buffer */
+       dqb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+       dqb.memory = V4L2_MEMORY_MMAP;
+       if (ioctl(d->fd, VIDIOC_QBUF, &dqb) < 0)
+               warn("VIDIOC_QBUF");
+
+       return 1;
+}
+
+int
 grab_frame(struct video *vid)
 {
        int fd, todo, done, ret;
@@ -1435,7 +1616,8 @@ stream(struct video *vid)
                if (ret == 1) {
                        if ((vid->mode & M_IN_DEV) ||
                            frames_grabbed - 1 == frames_played) {
-                               ret = grab_frame(vid);
+                               //ret = grab_frame(vid);
+                               ret = grab_mmap_frame(vid);
                                if (ret == 1) {
                                        frames_grabbed++;
                                        if (vid->nofps)
@@ -1574,6 +1756,8 @@ cleanup(struct video *vid, int excode)
        if (vid->xdsp.dpy != NULL)
                XCloseDisplay(vid->xdsp.dpy);
 
+       free_mmap_buffers(vid);
+
        if (vid->dev.fd >= 0)
                close(vid->dev.fd);
 
@@ -1686,6 +1870,9 @@ main(int argc, char *argv[])
                vid.nofps = 1;
 
        if (!setup(&vid))
+               cleanup(&vid, 1);
+
+       if (!init_mmap_buffers(&vid))
                cleanup(&vid, 1);
 
        if (!stream(&vid))

Reply via email to