Home | History | Annotate | Line # | Download | only in usb
auvitek_video.c revision 1.7
      1  1.7     skrll /* $NetBSD: auvitek_video.c,v 1.7 2016/04/23 10:15:31 skrll Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2010 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill /*
     30  1.1  jmcneill  * Auvitek AU0828 USB controller
     31  1.1  jmcneill  */
     32  1.1  jmcneill 
     33  1.1  jmcneill #include <sys/cdefs.h>
     34  1.7     skrll __KERNEL_RCSID(0, "$NetBSD: auvitek_video.c,v 1.7 2016/04/23 10:15:31 skrll Exp $");
     35  1.1  jmcneill 
     36  1.1  jmcneill #include <sys/param.h>
     37  1.1  jmcneill #include <sys/systm.h>
     38  1.1  jmcneill #include <sys/device.h>
     39  1.1  jmcneill #include <sys/conf.h>
     40  1.1  jmcneill #include <sys/kmem.h>
     41  1.1  jmcneill #include <sys/bus.h>
     42  1.1  jmcneill 
     43  1.1  jmcneill #include <dev/usb/usb.h>
     44  1.1  jmcneill #include <dev/usb/usbdi.h>
     45  1.1  jmcneill #include <dev/usb/usbdivar.h>
     46  1.1  jmcneill #include <dev/usb/usbdi_util.h>
     47  1.1  jmcneill #include <dev/usb/usbdevs.h>
     48  1.1  jmcneill 
     49  1.1  jmcneill #include <dev/video_if.h>
     50  1.1  jmcneill 
     51  1.1  jmcneill #include <dev/usb/auvitekreg.h>
     52  1.1  jmcneill #include <dev/usb/auvitekvar.h>
     53  1.1  jmcneill 
     54  1.1  jmcneill #define	AUVITEK_FORMAT_DEFAULT		0
     55  1.1  jmcneill #define	AUVITEK_STANDARD_NTSC_M		0
     56  1.1  jmcneill #define	AUVITEK_TUNER_DEFAULT		0
     57  1.1  jmcneill #define	AUVITEK_AUDIO_TELEVISION	0
     58  1.1  jmcneill #define	AUVITEK_AUDIO_LINEIN		1
     59  1.1  jmcneill #define	AUVITEK_INPUT_COMPOSITE		0
     60  1.1  jmcneill #define	AUVITEK_INPUT_SVIDEO		1
     61  1.1  jmcneill #define	AUVITEK_INPUT_TELEVISION	2
     62  1.1  jmcneill #define	AUVITEK_INPUT_CABLE		3
     63  1.1  jmcneill 
     64  1.1  jmcneill static int		auvitek_open(void *, int);
     65  1.1  jmcneill static void		auvitek_close(void *);
     66  1.1  jmcneill static const char *	auvitek_get_devname(void *);
     67  1.1  jmcneill static const char *	auvitek_get_businfo(void *);
     68  1.1  jmcneill static int		auvitek_enum_format(void *, uint32_t,
     69  1.1  jmcneill 					  struct video_format *);
     70  1.1  jmcneill static int		auvitek_get_format(void *, struct video_format *);
     71  1.1  jmcneill static int		auvitek_set_format(void *, struct video_format *);
     72  1.1  jmcneill static int		auvitek_try_format(void *, struct video_format *);
     73  1.1  jmcneill static int		auvitek_enum_standard(void *, uint32_t,
     74  1.1  jmcneill 					      enum video_standard *);
     75  1.1  jmcneill static int		auvitek_get_standard(void *, enum video_standard *);
     76  1.1  jmcneill static int		auvitek_set_standard(void *, enum video_standard);
     77  1.1  jmcneill static int		auvitek_start_transfer(void *);
     78  1.1  jmcneill static int		auvitek_stop_transfer(void *);
     79  1.1  jmcneill static int		auvitek_get_tuner(void *, struct video_tuner *);
     80  1.1  jmcneill static int		auvitek_set_tuner(void *, struct video_tuner *);
     81  1.1  jmcneill static int		auvitek_enum_audio(void *, uint32_t,
     82  1.1  jmcneill 					   struct video_audio *);
     83  1.1  jmcneill static int		auvitek_get_audio(void *, struct video_audio *);
     84  1.1  jmcneill static int		auvitek_set_audio(void *, struct video_audio *);
     85  1.1  jmcneill static int		auvitek_enum_input(void *, uint32_t,
     86  1.1  jmcneill 					   struct video_input *);
     87  1.1  jmcneill static int		auvitek_get_input(void *, struct video_input *);
     88  1.1  jmcneill static int		auvitek_set_input(void *, struct video_input *);
     89  1.1  jmcneill static int		auvitek_get_frequency(void *, struct video_frequency *);
     90  1.1  jmcneill static int		auvitek_set_frequency(void *, struct video_frequency *);
     91  1.1  jmcneill 
     92  1.1  jmcneill static int		auvitek_start_xfer(struct auvitek_softc *);
     93  1.1  jmcneill static int		auvitek_stop_xfer(struct auvitek_softc *);
     94  1.1  jmcneill static int		auvitek_isoc_start(struct auvitek_softc *);
     95  1.1  jmcneill static int		auvitek_isoc_start1(struct auvitek_isoc *);
     96  1.7     skrll static void		auvitek_isoc_intr(struct usbd_xfer *, void *,
     97  1.1  jmcneill 					  usbd_status);
     98  1.1  jmcneill static int		auvitek_isoc_process(struct auvitek_softc *,
     99  1.1  jmcneill 					     uint8_t *, uint32_t);
    100  1.1  jmcneill static void		auvitek_videobuf_weave(struct auvitek_softc *,
    101  1.1  jmcneill 					       uint8_t *, uint32_t);
    102  1.1  jmcneill 
    103  1.4  jmcneill static const struct video_hw_if auvitek_video_if = {
    104  1.1  jmcneill 	.open = auvitek_open,
    105  1.1  jmcneill 	.close = auvitek_close,
    106  1.1  jmcneill 	.get_devname = auvitek_get_devname,
    107  1.1  jmcneill 	.get_businfo = auvitek_get_businfo,
    108  1.1  jmcneill 	.enum_format = auvitek_enum_format,
    109  1.1  jmcneill 	.get_format = auvitek_get_format,
    110  1.1  jmcneill 	.set_format = auvitek_set_format,
    111  1.1  jmcneill 	.try_format = auvitek_try_format,
    112  1.1  jmcneill 	.enum_standard = auvitek_enum_standard,
    113  1.1  jmcneill 	.get_standard = auvitek_get_standard,
    114  1.1  jmcneill 	.set_standard = auvitek_set_standard,
    115  1.1  jmcneill 	.start_transfer = auvitek_start_transfer,
    116  1.1  jmcneill 	.stop_transfer = auvitek_stop_transfer,
    117  1.1  jmcneill 	.get_tuner = auvitek_get_tuner,
    118  1.1  jmcneill 	.set_tuner = auvitek_set_tuner,
    119  1.1  jmcneill 	.enum_audio = auvitek_enum_audio,
    120  1.1  jmcneill 	.get_audio = auvitek_get_audio,
    121  1.1  jmcneill 	.set_audio = auvitek_set_audio,
    122  1.1  jmcneill 	.enum_input = auvitek_enum_input,
    123  1.1  jmcneill 	.get_input = auvitek_get_input,
    124  1.1  jmcneill 	.set_input = auvitek_set_input,
    125  1.1  jmcneill 	.get_frequency = auvitek_get_frequency,
    126  1.1  jmcneill 	.set_frequency = auvitek_set_frequency,
    127  1.1  jmcneill };
    128  1.1  jmcneill 
    129  1.1  jmcneill int
    130  1.1  jmcneill auvitek_video_attach(struct auvitek_softc *sc)
    131  1.1  jmcneill {
    132  1.1  jmcneill 	snprintf(sc->sc_businfo, sizeof(sc->sc_businfo), "usb:%08x",
    133  1.7     skrll 	    sc->sc_udev->ud_cookie.cookie);
    134  1.5  jmcneill 
    135  1.5  jmcneill 	auvitek_video_rescan(sc, NULL, NULL);
    136  1.1  jmcneill 
    137  1.1  jmcneill 	return (sc->sc_videodev != NULL);
    138  1.1  jmcneill }
    139  1.1  jmcneill 
    140  1.1  jmcneill int
    141  1.1  jmcneill auvitek_video_detach(struct auvitek_softc *sc, int flags)
    142  1.1  jmcneill {
    143  1.1  jmcneill 	if (sc->sc_videodev != NULL) {
    144  1.1  jmcneill 		config_detach(sc->sc_videodev, flags);
    145  1.1  jmcneill 		sc->sc_videodev = NULL;
    146  1.1  jmcneill 	}
    147  1.1  jmcneill 
    148  1.1  jmcneill 	return 0;
    149  1.1  jmcneill }
    150  1.1  jmcneill 
    151  1.1  jmcneill void
    152  1.5  jmcneill auvitek_video_rescan(struct auvitek_softc *sc, const char *ifattr,
    153  1.5  jmcneill     const int *locs)
    154  1.5  jmcneill {
    155  1.5  jmcneill 	if (ifattr_match(ifattr, "videobus") && sc->sc_videodev == NULL)
    156  1.5  jmcneill 		sc->sc_videodev = video_attach_mi(&auvitek_video_if,
    157  1.5  jmcneill 		    sc->sc_dev);
    158  1.5  jmcneill }
    159  1.5  jmcneill 
    160  1.5  jmcneill void
    161  1.1  jmcneill auvitek_video_childdet(struct auvitek_softc *sc, device_t child)
    162  1.1  jmcneill {
    163  1.1  jmcneill 	if (sc->sc_videodev == child)
    164  1.1  jmcneill 		sc->sc_videodev = NULL;
    165  1.1  jmcneill }
    166  1.1  jmcneill 
    167  1.1  jmcneill static int
    168  1.1  jmcneill auvitek_open(void *opaque, int flags)
    169  1.1  jmcneill {
    170  1.1  jmcneill 	struct auvitek_softc *sc = opaque;
    171  1.1  jmcneill 
    172  1.1  jmcneill 	if (sc->sc_dying)
    173  1.1  jmcneill 		return EIO;
    174  1.1  jmcneill 
    175  1.6  jmcneill 	auvitek_attach_tuner(sc->sc_dev);
    176  1.1  jmcneill 
    177  1.1  jmcneill 	if (sc->sc_xc5k == NULL)
    178  1.1  jmcneill 		return ENXIO;
    179  1.1  jmcneill 
    180  1.1  jmcneill 	return 0;
    181  1.1  jmcneill }
    182  1.1  jmcneill 
    183  1.1  jmcneill static void
    184  1.1  jmcneill auvitek_close(void *opaque)
    185  1.1  jmcneill {
    186  1.1  jmcneill }
    187  1.1  jmcneill 
    188  1.1  jmcneill static const char *
    189  1.1  jmcneill auvitek_get_devname(void *opaque)
    190  1.1  jmcneill {
    191  1.1  jmcneill 	struct auvitek_softc *sc = opaque;
    192  1.1  jmcneill 
    193  1.1  jmcneill 	return sc->sc_descr;
    194  1.1  jmcneill }
    195  1.1  jmcneill 
    196  1.1  jmcneill static const char *
    197  1.1  jmcneill auvitek_get_businfo(void *opaque)
    198  1.1  jmcneill {
    199  1.1  jmcneill 	struct auvitek_softc *sc = opaque;
    200  1.1  jmcneill 
    201  1.1  jmcneill 	return sc->sc_businfo;
    202  1.1  jmcneill }
    203  1.1  jmcneill 
    204  1.1  jmcneill static int
    205  1.1  jmcneill auvitek_enum_format(void *opaque, uint32_t index, struct video_format *format)
    206  1.1  jmcneill {
    207  1.1  jmcneill 	if (index != AUVITEK_FORMAT_DEFAULT)
    208  1.1  jmcneill 		return EINVAL;
    209  1.1  jmcneill 
    210  1.1  jmcneill 	format->pixel_format = VIDEO_FORMAT_UYVY;
    211  1.1  jmcneill 
    212  1.1  jmcneill 	return 0;
    213  1.1  jmcneill }
    214  1.1  jmcneill 
    215  1.1  jmcneill static int
    216  1.1  jmcneill auvitek_get_format(void *opaque, struct video_format *format)
    217  1.1  jmcneill {
    218  1.1  jmcneill 
    219  1.1  jmcneill 	format->pixel_format = VIDEO_FORMAT_UYVY;
    220  1.1  jmcneill 	format->width = 720;
    221  1.1  jmcneill 	format->height = 480;
    222  1.1  jmcneill 	format->stride = format->width * 2;
    223  1.1  jmcneill 	format->sample_size = format->stride * format->height;
    224  1.1  jmcneill 	format->aspect_x = 4;
    225  1.1  jmcneill 	format->aspect_y = 3;
    226  1.1  jmcneill 	format->color.primaries = VIDEO_COLOR_PRIMARIES_SMPTE_170M;
    227  1.1  jmcneill 	format->color.gamma_function = VIDEO_GAMMA_FUNCTION_UNSPECIFIED;
    228  1.1  jmcneill 	format->color.matrix_coeff = VIDEO_MATRIX_COEFF_UNSPECIFIED;
    229  1.1  jmcneill 	format->interlace_flags = VIDEO_INTERLACE_ON;
    230  1.1  jmcneill 	format->priv = 0;
    231  1.1  jmcneill 
    232  1.1  jmcneill 	return 0;
    233  1.1  jmcneill }
    234  1.1  jmcneill 
    235  1.1  jmcneill static int
    236  1.1  jmcneill auvitek_set_format(void *opaque, struct video_format *format)
    237  1.1  jmcneill {
    238  1.1  jmcneill 	if (format->pixel_format != VIDEO_FORMAT_UYVY)
    239  1.1  jmcneill 		return EINVAL;
    240  1.1  jmcneill 
    241  1.1  jmcneill 	return auvitek_get_format(opaque, format);
    242  1.1  jmcneill }
    243  1.1  jmcneill 
    244  1.1  jmcneill static int
    245  1.1  jmcneill auvitek_try_format(void *opaque, struct video_format *format)
    246  1.1  jmcneill {
    247  1.1  jmcneill 	return auvitek_get_format(opaque, format);
    248  1.1  jmcneill }
    249  1.1  jmcneill 
    250  1.1  jmcneill static int
    251  1.1  jmcneill auvitek_enum_standard(void *opaque, uint32_t index, enum video_standard *vstd)
    252  1.1  jmcneill {
    253  1.1  jmcneill 	switch (index) {
    254  1.1  jmcneill 	case AUVITEK_STANDARD_NTSC_M:
    255  1.1  jmcneill 		*vstd = VIDEO_STANDARD_NTSC_M;
    256  1.1  jmcneill 		return 0;
    257  1.1  jmcneill 	default:
    258  1.1  jmcneill 		return EINVAL;
    259  1.1  jmcneill 	}
    260  1.1  jmcneill }
    261  1.1  jmcneill 
    262  1.1  jmcneill static int
    263  1.1  jmcneill auvitek_get_standard(void *opaque, enum video_standard *vstd)
    264  1.1  jmcneill {
    265  1.1  jmcneill 	*vstd = VIDEO_STANDARD_NTSC_M;
    266  1.1  jmcneill 	return 0;
    267  1.1  jmcneill }
    268  1.1  jmcneill 
    269  1.1  jmcneill static int
    270  1.1  jmcneill auvitek_set_standard(void *opaque, enum video_standard vstd)
    271  1.1  jmcneill {
    272  1.1  jmcneill 	switch (vstd) {
    273  1.1  jmcneill 	case VIDEO_STANDARD_NTSC_M:
    274  1.1  jmcneill 		return 0;
    275  1.1  jmcneill 	default:
    276  1.1  jmcneill 		return EINVAL;
    277  1.1  jmcneill 	}
    278  1.1  jmcneill }
    279  1.1  jmcneill 
    280  1.1  jmcneill static int
    281  1.1  jmcneill auvitek_start_transfer(void *opaque)
    282  1.1  jmcneill {
    283  1.1  jmcneill 	struct auvitek_softc *sc = opaque;
    284  1.1  jmcneill 	int error, s;
    285  1.1  jmcneill 	uint16_t vpos = 0, hpos = 0;
    286  1.1  jmcneill 	uint16_t hres = 720 * 2;
    287  1.1  jmcneill 	uint16_t vres = 484 / 2;
    288  1.1  jmcneill 
    289  1.1  jmcneill 	auvitek_write_1(sc, AU0828_REG_SENSORVBI_CTL, 0x00);
    290  1.1  jmcneill 
    291  1.1  jmcneill 	/* program video position and size */
    292  1.1  jmcneill 	auvitek_write_1(sc, AU0828_REG_HPOS_LO, hpos & 0xff);
    293  1.1  jmcneill 	auvitek_write_1(sc, AU0828_REG_HPOS_HI, hpos >> 8);
    294  1.1  jmcneill 	auvitek_write_1(sc, AU0828_REG_VPOS_LO, vpos & 0xff);
    295  1.1  jmcneill 	auvitek_write_1(sc, AU0828_REG_VPOS_HI, vpos >> 8);
    296  1.1  jmcneill 	auvitek_write_1(sc, AU0828_REG_HRES_LO, hres & 0xff);
    297  1.1  jmcneill 	auvitek_write_1(sc, AU0828_REG_HRES_HI, hres >> 8);
    298  1.1  jmcneill 	auvitek_write_1(sc, AU0828_REG_VRES_LO, vres & 0xff);
    299  1.1  jmcneill 	auvitek_write_1(sc, AU0828_REG_VRES_HI, vres >> 8);
    300  1.1  jmcneill 
    301  1.1  jmcneill 	auvitek_write_1(sc, AU0828_REG_SENSOR_CTL, 0xb3);
    302  1.1  jmcneill 
    303  1.1  jmcneill 	auvitek_write_1(sc, AU0828_REG_AUDIOCTL, 0x01);
    304  1.1  jmcneill 
    305  1.1  jmcneill 	s = splusb();
    306  1.1  jmcneill 	error = auvitek_start_xfer(sc);
    307  1.1  jmcneill 	splx(s);
    308  1.1  jmcneill 
    309  1.1  jmcneill 	if (error)
    310  1.1  jmcneill 		auvitek_stop_transfer(sc);
    311  1.1  jmcneill 
    312  1.1  jmcneill 	return error;
    313  1.1  jmcneill }
    314  1.1  jmcneill 
    315  1.1  jmcneill static int
    316  1.1  jmcneill auvitek_stop_transfer(void *opaque)
    317  1.1  jmcneill {
    318  1.1  jmcneill 	struct auvitek_softc *sc = opaque;
    319  1.1  jmcneill 	int error, s;
    320  1.1  jmcneill 
    321  1.1  jmcneill 	auvitek_write_1(sc, AU0828_REG_SENSOR_CTL, 0x00);
    322  1.1  jmcneill 
    323  1.1  jmcneill 	s = splusb();
    324  1.1  jmcneill 	error = auvitek_stop_xfer(sc);
    325  1.1  jmcneill 	splx(s);
    326  1.1  jmcneill 
    327  1.1  jmcneill 	return error;
    328  1.1  jmcneill }
    329  1.1  jmcneill 
    330  1.1  jmcneill static int
    331  1.1  jmcneill auvitek_get_tuner(void *opaque, struct video_tuner *vt)
    332  1.1  jmcneill {
    333  1.1  jmcneill 	struct auvitek_softc *sc = opaque;
    334  1.1  jmcneill 
    335  1.1  jmcneill 	switch (vt->index) {
    336  1.1  jmcneill 	case AUVITEK_TUNER_DEFAULT:
    337  1.1  jmcneill 		strlcpy(vt->name, "XC5000", sizeof(vt->name));
    338  1.1  jmcneill 		vt->freq_lo =  44000000 / 62500;
    339  1.1  jmcneill 		vt->freq_hi = 958000000 / 62500;
    340  1.1  jmcneill 		vt->caps = VIDEO_TUNER_F_STEREO;
    341  1.1  jmcneill 		vt->mode = VIDEO_TUNER_F_STEREO;
    342  1.1  jmcneill 		if (sc->sc_au8522)
    343  1.1  jmcneill 			vt->signal = au8522_get_signal(sc->sc_au8522);
    344  1.1  jmcneill 		else
    345  1.1  jmcneill 			vt->signal = 0;
    346  1.1  jmcneill 		break;
    347  1.1  jmcneill 	default:
    348  1.1  jmcneill 		return EINVAL;
    349  1.1  jmcneill 	}
    350  1.1  jmcneill 
    351  1.1  jmcneill 	return 0;
    352  1.1  jmcneill }
    353  1.1  jmcneill 
    354  1.1  jmcneill static int
    355  1.1  jmcneill auvitek_set_tuner(void *opaque, struct video_tuner *vt)
    356  1.1  jmcneill {
    357  1.1  jmcneill 	if (vt->index != AUVITEK_TUNER_DEFAULT)
    358  1.1  jmcneill 		return EINVAL;
    359  1.1  jmcneill 	return 0;
    360  1.1  jmcneill }
    361  1.1  jmcneill 
    362  1.1  jmcneill static int
    363  1.1  jmcneill auvitek_enum_audio(void *opaque, uint32_t index, struct video_audio *va)
    364  1.1  jmcneill {
    365  1.1  jmcneill 	switch (index) {
    366  1.1  jmcneill 	case AUVITEK_AUDIO_TELEVISION:
    367  1.1  jmcneill 		strlcpy(va->name, "Television", sizeof(va->name));
    368  1.1  jmcneill 		va->caps = VIDEO_AUDIO_F_STEREO;
    369  1.1  jmcneill 		break;
    370  1.1  jmcneill 	case AUVITEK_AUDIO_LINEIN:
    371  1.1  jmcneill 		strlcpy(va->name, "Line In", sizeof(va->name));
    372  1.1  jmcneill 		va->caps = VIDEO_AUDIO_F_STEREO;
    373  1.1  jmcneill 		break;
    374  1.1  jmcneill 	default:
    375  1.1  jmcneill 		return EINVAL;
    376  1.1  jmcneill 	}
    377  1.1  jmcneill 
    378  1.1  jmcneill 	return 0;
    379  1.1  jmcneill }
    380  1.1  jmcneill 
    381  1.1  jmcneill static int
    382  1.1  jmcneill auvitek_get_audio(void *opaque, struct video_audio *va)
    383  1.1  jmcneill {
    384  1.1  jmcneill 	struct auvitek_softc *sc = opaque;
    385  1.1  jmcneill 
    386  1.1  jmcneill 	return auvitek_enum_audio(opaque, sc->sc_ainput, va);
    387  1.1  jmcneill }
    388  1.1  jmcneill 
    389  1.1  jmcneill static int
    390  1.1  jmcneill auvitek_set_audio(void *opaque, struct video_audio *va)
    391  1.1  jmcneill {
    392  1.1  jmcneill 	struct auvitek_softc *sc = opaque;
    393  1.1  jmcneill 
    394  1.1  jmcneill 	if (va->index == sc->sc_ainput)
    395  1.1  jmcneill 		return 0;
    396  1.1  jmcneill 
    397  1.1  jmcneill 	return EINVAL;
    398  1.1  jmcneill }
    399  1.1  jmcneill 
    400  1.1  jmcneill static int
    401  1.1  jmcneill auvitek_enum_input(void *opaque, uint32_t index, struct video_input *vi)
    402  1.1  jmcneill {
    403  1.1  jmcneill 	switch (index) {
    404  1.1  jmcneill 	case AUVITEK_INPUT_COMPOSITE:
    405  1.1  jmcneill 		strlcpy(vi->name, "Composite", sizeof(vi->name));
    406  1.1  jmcneill 		vi->type = VIDEO_INPUT_TYPE_BASEBAND;
    407  1.1  jmcneill 		vi->standards = VIDEO_STANDARD_NTSC_M;
    408  1.1  jmcneill 		break;
    409  1.1  jmcneill 	case AUVITEK_INPUT_SVIDEO:
    410  1.1  jmcneill 		strlcpy(vi->name, "S-Video", sizeof(vi->name));
    411  1.1  jmcneill 		vi->type = VIDEO_INPUT_TYPE_BASEBAND;
    412  1.1  jmcneill 		vi->standards = VIDEO_STANDARD_NTSC_M;
    413  1.1  jmcneill 		break;
    414  1.1  jmcneill 	case AUVITEK_INPUT_TELEVISION:
    415  1.1  jmcneill 		strlcpy(vi->name, "Television", sizeof(vi->name));
    416  1.1  jmcneill 		vi->type = VIDEO_INPUT_TYPE_TUNER;
    417  1.1  jmcneill 		vi->standards = VIDEO_STANDARD_NTSC_M;
    418  1.1  jmcneill 		break;
    419  1.1  jmcneill 	case AUVITEK_INPUT_CABLE:
    420  1.1  jmcneill 		strlcpy(vi->name, "Cable TV", sizeof(vi->name));
    421  1.1  jmcneill 		vi->type = VIDEO_INPUT_TYPE_TUNER;
    422  1.1  jmcneill 		vi->standards = VIDEO_STANDARD_NTSC_M;
    423  1.1  jmcneill 		break;
    424  1.1  jmcneill 	default:
    425  1.1  jmcneill 		return EINVAL;
    426  1.1  jmcneill 	}
    427  1.1  jmcneill 
    428  1.1  jmcneill 	vi->index = index;
    429  1.1  jmcneill 	vi->tuner_index = AUVITEK_TUNER_DEFAULT;
    430  1.1  jmcneill 
    431  1.1  jmcneill 	return 0;
    432  1.1  jmcneill }
    433  1.1  jmcneill 
    434  1.1  jmcneill static int
    435  1.1  jmcneill auvitek_get_input(void *opaque, struct video_input *vi)
    436  1.1  jmcneill {
    437  1.1  jmcneill 	struct auvitek_softc *sc = opaque;
    438  1.1  jmcneill 
    439  1.1  jmcneill 	return auvitek_enum_input(opaque, sc->sc_vinput, vi);
    440  1.1  jmcneill }
    441  1.1  jmcneill 
    442  1.1  jmcneill static int
    443  1.1  jmcneill auvitek_set_input(void *opaque, struct video_input *vi)
    444  1.1  jmcneill {
    445  1.1  jmcneill 	struct auvitek_softc *sc = opaque;
    446  1.1  jmcneill 	struct video_frequency vf;
    447  1.1  jmcneill 	au8522_vinput_t vinput = AU8522_VINPUT_UNCONF;
    448  1.1  jmcneill 	au8522_ainput_t ainput = AU8522_AINPUT_UNCONF;
    449  1.1  jmcneill 	uint8_t r;
    450  1.1  jmcneill 
    451  1.1  jmcneill 	switch (vi->index) {
    452  1.1  jmcneill 	case AUVITEK_INPUT_COMPOSITE:
    453  1.1  jmcneill 		vinput = AU8522_VINPUT_CVBS;
    454  1.1  jmcneill 		ainput = AU8522_AINPUT_NONE;
    455  1.1  jmcneill 		sc->sc_ainput = AUVITEK_AUDIO_LINEIN;
    456  1.1  jmcneill 		break;
    457  1.1  jmcneill 	case AUVITEK_INPUT_SVIDEO:
    458  1.1  jmcneill 		vinput = AU8522_VINPUT_SVIDEO;
    459  1.1  jmcneill 		ainput = AU8522_AINPUT_NONE;
    460  1.1  jmcneill 		sc->sc_ainput = AUVITEK_AUDIO_LINEIN;
    461  1.1  jmcneill 		break;
    462  1.1  jmcneill 	case AUVITEK_INPUT_TELEVISION:
    463  1.1  jmcneill 	case AUVITEK_INPUT_CABLE:
    464  1.1  jmcneill 		vinput = AU8522_VINPUT_CVBS_TUNER;
    465  1.1  jmcneill 		ainput = AU8522_AINPUT_SIF;
    466  1.1  jmcneill 		sc->sc_ainput = AUVITEK_AUDIO_TELEVISION;
    467  1.1  jmcneill 		break;
    468  1.1  jmcneill 	default:
    469  1.1  jmcneill 		return EINVAL;
    470  1.1  jmcneill 	}
    471  1.1  jmcneill 
    472  1.1  jmcneill 	sc->sc_vinput = vi->index;
    473  1.1  jmcneill 
    474  1.1  jmcneill 	au8522_set_input(sc->sc_au8522, vinput, ainput);
    475  1.1  jmcneill 
    476  1.2  jmcneill 	/* XXX HVR-850/950Q specific */
    477  1.1  jmcneill 	r = auvitek_read_1(sc, AU0828_REG_GPIO1_OUTEN);
    478  1.1  jmcneill 	if (ainput == AU8522_AINPUT_NONE)
    479  1.1  jmcneill 		r |= 0x10;
    480  1.1  jmcneill 	else
    481  1.1  jmcneill 		r &= ~0x10;
    482  1.1  jmcneill 	auvitek_write_1(sc, AU0828_REG_GPIO1_OUTEN, r);
    483  1.1  jmcneill 
    484  1.1  jmcneill 	if (vinput == AU8522_VINPUT_CVBS_TUNER && sc->sc_curfreq > 0) {
    485  1.1  jmcneill 		vf.tuner_index = AUVITEK_TUNER_DEFAULT;
    486  1.1  jmcneill 		vf.frequency = sc->sc_curfreq;
    487  1.1  jmcneill 		auvitek_set_frequency(sc, &vf);
    488  1.1  jmcneill 	}
    489  1.1  jmcneill 
    490  1.1  jmcneill 	return 0;
    491  1.1  jmcneill }
    492  1.1  jmcneill 
    493  1.1  jmcneill static int
    494  1.1  jmcneill auvitek_get_frequency(void *opaque, struct video_frequency *vf)
    495  1.1  jmcneill {
    496  1.1  jmcneill 	struct auvitek_softc *sc = opaque;
    497  1.1  jmcneill 
    498  1.1  jmcneill 	if (sc->sc_vinput != AUVITEK_INPUT_TELEVISION &&
    499  1.1  jmcneill 	    sc->sc_vinput != AUVITEK_INPUT_CABLE)
    500  1.1  jmcneill 		return EINVAL;
    501  1.1  jmcneill 
    502  1.1  jmcneill 	vf->tuner_index = AUVITEK_TUNER_DEFAULT;
    503  1.1  jmcneill 	vf->frequency = sc->sc_curfreq;
    504  1.1  jmcneill 
    505  1.1  jmcneill 	return 0;
    506  1.1  jmcneill }
    507  1.1  jmcneill 
    508  1.1  jmcneill static int
    509  1.1  jmcneill auvitek_set_frequency(void *opaque, struct video_frequency *vf)
    510  1.1  jmcneill {
    511  1.1  jmcneill 	struct auvitek_softc *sc = opaque;
    512  1.1  jmcneill 	struct xc5k_params params;
    513  1.1  jmcneill 	int error;
    514  1.1  jmcneill 
    515  1.1  jmcneill 	if (sc->sc_vinput != AUVITEK_INPUT_TELEVISION &&
    516  1.1  jmcneill 	    sc->sc_vinput != AUVITEK_INPUT_CABLE)
    517  1.1  jmcneill 		return EINVAL;
    518  1.1  jmcneill 	if (vf->tuner_index != AUVITEK_TUNER_DEFAULT)
    519  1.1  jmcneill 		return EINVAL;
    520  1.1  jmcneill 	if (sc->sc_xc5k == NULL)
    521  1.1  jmcneill 		return ENODEV;
    522  1.1  jmcneill 
    523  1.1  jmcneill 	params.standard = VIDEO_STANDARD_NTSC_M;
    524  1.1  jmcneill 	if (sc->sc_vinput == AUVITEK_INPUT_TELEVISION)
    525  1.1  jmcneill 		params.signal_source = XC5K_SIGNAL_SOURCE_AIR;
    526  1.1  jmcneill 	else
    527  1.1  jmcneill 		params.signal_source = XC5K_SIGNAL_SOURCE_CABLE;
    528  1.1  jmcneill 	params.frequency = vf->frequency;
    529  1.3  jmcneill 	if (sc->sc_au8522)
    530  1.3  jmcneill 		au8522_set_audio(sc->sc_au8522, false);
    531  1.4  jmcneill 	error = xc5k_tune_video(sc->sc_xc5k, &params);
    532  1.3  jmcneill 	if (sc->sc_au8522)
    533  1.3  jmcneill 		au8522_set_audio(sc->sc_au8522, true);
    534  1.1  jmcneill 	if (error)
    535  1.1  jmcneill 		return error;
    536  1.1  jmcneill 
    537  1.1  jmcneill 	sc->sc_curfreq = vf->frequency;
    538  1.1  jmcneill 
    539  1.1  jmcneill 	auvitek_write_1(sc, AU0828_REG_SENSOR_CTL, 0x00);
    540  1.1  jmcneill 	delay(30000);
    541  1.1  jmcneill 	auvitek_write_1(sc, AU0828_REG_SENSOR_CTL, 0xb3);
    542  1.1  jmcneill 
    543  1.1  jmcneill 	return 0;
    544  1.1  jmcneill }
    545  1.1  jmcneill 
    546  1.1  jmcneill static int
    547  1.1  jmcneill auvitek_start_xfer(struct auvitek_softc *sc)
    548  1.1  jmcneill {
    549  1.1  jmcneill 	struct auvitek_xfer *ax = &sc->sc_ax;
    550  1.1  jmcneill 	uint32_t vframe_len, uframe_len, nframes;
    551  1.1  jmcneill 	usbd_status err;
    552  1.1  jmcneill 	int i;
    553  1.1  jmcneill 
    554  1.4  jmcneill 	err = usbd_set_interface(sc->sc_isoc_iface, AUVITEK_XFER_ALTNO);
    555  1.1  jmcneill 	if (err != USBD_NORMAL_COMPLETION) {
    556  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't set altno %d: %s\n",
    557  1.1  jmcneill 		    AUVITEK_XFER_ALTNO, usbd_errstr(err));
    558  1.1  jmcneill 		return EIO;
    559  1.1  jmcneill 	}
    560  1.1  jmcneill 
    561  1.1  jmcneill 	vframe_len = 720 * 480 * 2;
    562  1.1  jmcneill 	uframe_len = ax->ax_maxpktlen;
    563  1.1  jmcneill 	nframes = (vframe_len + uframe_len - 1) / uframe_len;
    564  1.1  jmcneill 	nframes = (nframes + 7) & ~7;
    565  1.1  jmcneill 
    566  1.1  jmcneill 	ax->ax_nframes = nframes;
    567  1.1  jmcneill 	ax->ax_uframe_len = uframe_len;
    568  1.4  jmcneill 	for (i = 0; i < AUVITEK_NISOC_XFERS; i++) {
    569  1.1  jmcneill 		struct auvitek_isoc *isoc = &ax->ax_i[i];
    570  1.1  jmcneill 		isoc->i_ax = ax;
    571  1.1  jmcneill 		isoc->i_frlengths =
    572  1.1  jmcneill 		    kmem_alloc(sizeof(isoc->i_frlengths[0]) * nframes,
    573  1.1  jmcneill 			KM_SLEEP);
    574  1.1  jmcneill 	}
    575  1.1  jmcneill 
    576  1.4  jmcneill 	err = usbd_open_pipe(sc->sc_isoc_iface, ax->ax_endpt,
    577  1.1  jmcneill 	    USBD_EXCLUSIVE_USE, &ax->ax_pipe);
    578  1.1  jmcneill 	if (err != USBD_NORMAL_COMPLETION) {
    579  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't open pipe: %s\n",
    580  1.1  jmcneill 		    usbd_errstr(err));
    581  1.1  jmcneill 		return EIO;
    582  1.1  jmcneill 	}
    583  1.1  jmcneill 
    584  1.4  jmcneill 	for (i = 0; i < AUVITEK_NISOC_XFERS; i++) {
    585  1.1  jmcneill 		struct auvitek_isoc *isoc = &ax->ax_i[i];
    586  1.1  jmcneill 
    587  1.7     skrll 		int error = usbd_create_xfer(ax->ax_pipe,
    588  1.7     skrll 		    nframes * uframe_len, 0, ax->ax_nframes, &isoc->i_xfer);
    589  1.7     skrll 		if (error) {
    590  1.1  jmcneill 			aprint_error_dev(sc->sc_dev,
    591  1.7     skrll 			    "couldn't create usb xfer\n");
    592  1.7     skrll 			return error;
    593  1.1  jmcneill 		}
    594  1.1  jmcneill 
    595  1.7     skrll 		isoc->i_buf = usbd_get_buffer(isoc->i_xfer);
    596  1.1  jmcneill 	}
    597  1.1  jmcneill 
    598  1.1  jmcneill 	return auvitek_isoc_start(sc);
    599  1.1  jmcneill }
    600  1.1  jmcneill 
    601  1.1  jmcneill static int
    602  1.1  jmcneill auvitek_stop_xfer(struct auvitek_softc *sc)
    603  1.1  jmcneill {
    604  1.1  jmcneill 	struct auvitek_xfer *ax = &sc->sc_ax;
    605  1.1  jmcneill 	usbd_status err;
    606  1.1  jmcneill 	int i;
    607  1.1  jmcneill 
    608  1.1  jmcneill 	if (ax->ax_pipe != NULL) {
    609  1.1  jmcneill 		usbd_abort_pipe(ax->ax_pipe);
    610  1.1  jmcneill 	}
    611  1.4  jmcneill 	for (i = 0; i < AUVITEK_NISOC_XFERS; i++) {
    612  1.1  jmcneill 		struct auvitek_isoc *isoc = &ax->ax_i[i];
    613  1.1  jmcneill 		if (isoc->i_xfer != NULL) {
    614  1.7     skrll 			usbd_destroy_xfer(isoc->i_xfer);
    615  1.1  jmcneill 			isoc->i_xfer = NULL;
    616  1.1  jmcneill 		}
    617  1.1  jmcneill 		if (isoc->i_frlengths != NULL) {
    618  1.1  jmcneill 			kmem_free(isoc->i_frlengths,
    619  1.1  jmcneill 			    sizeof(isoc->i_frlengths[0]) * ax->ax_nframes);
    620  1.1  jmcneill 			isoc->i_frlengths = NULL;
    621  1.1  jmcneill 		}
    622  1.1  jmcneill 	}
    623  1.7     skrll 	if (ax->ax_pipe != NULL) {
    624  1.7     skrll 		usbd_close_pipe(ax->ax_pipe);
    625  1.7     skrll 		ax->ax_pipe = NULL;
    626  1.7     skrll 	}
    627  1.1  jmcneill 
    628  1.1  jmcneill 	usbd_delay_ms(sc->sc_udev, 1000);
    629  1.4  jmcneill 	err = usbd_set_interface(sc->sc_isoc_iface, 0);
    630  1.1  jmcneill 	if (err != USBD_NORMAL_COMPLETION) {
    631  1.1  jmcneill 		aprint_error_dev(sc->sc_dev,
    632  1.1  jmcneill 		    "couldn't set zero bw interface: %s\n",
    633  1.1  jmcneill 		    usbd_errstr(err));
    634  1.1  jmcneill 		return EIO;
    635  1.1  jmcneill 	}
    636  1.1  jmcneill 
    637  1.1  jmcneill 	return 0;
    638  1.1  jmcneill }
    639  1.1  jmcneill 
    640  1.1  jmcneill static int
    641  1.1  jmcneill auvitek_isoc_start(struct auvitek_softc *sc)
    642  1.1  jmcneill {
    643  1.1  jmcneill 	struct auvitek_xfer *ax = &sc->sc_ax;
    644  1.1  jmcneill 	int i, error;
    645  1.1  jmcneill 
    646  1.1  jmcneill 	ax->ax_av.av_el = ax->ax_av.av_ol = 0;
    647  1.1  jmcneill 	ax->ax_av.av_eb = ax->ax_av.av_ob = 0;
    648  1.1  jmcneill 	ax->ax_av.av_stride = 720 * 2;
    649  1.1  jmcneill 
    650  1.4  jmcneill 	for (i = 0; i < AUVITEK_NISOC_XFERS; i++) {
    651  1.1  jmcneill 		error = auvitek_isoc_start1(&ax->ax_i[i]);
    652  1.1  jmcneill 		if (error)
    653  1.1  jmcneill 			return error;
    654  1.1  jmcneill 	}
    655  1.1  jmcneill 
    656  1.1  jmcneill 	return 0;
    657  1.1  jmcneill }
    658  1.1  jmcneill 
    659  1.1  jmcneill static int
    660  1.1  jmcneill auvitek_isoc_start1(struct auvitek_isoc *isoc)
    661  1.1  jmcneill {
    662  1.1  jmcneill 	struct auvitek_xfer *ax = isoc->i_ax;
    663  1.1  jmcneill 	struct auvitek_softc *sc = ax->ax_sc;
    664  1.1  jmcneill 	usbd_status err;
    665  1.1  jmcneill 	unsigned int i;
    666  1.1  jmcneill 
    667  1.1  jmcneill 	ax = isoc->i_ax;
    668  1.1  jmcneill 
    669  1.1  jmcneill 	for (i = 0; i < ax->ax_nframes; i++)
    670  1.1  jmcneill 		isoc->i_frlengths[i] = ax->ax_uframe_len;
    671  1.1  jmcneill 
    672  1.1  jmcneill 	usbd_setup_isoc_xfer(isoc->i_xfer,
    673  1.1  jmcneill 			     isoc,
    674  1.1  jmcneill 			     isoc->i_frlengths,
    675  1.1  jmcneill 			     ax->ax_nframes,
    676  1.7     skrll 			     USBD_SHORT_XFER_OK,
    677  1.1  jmcneill 			     auvitek_isoc_intr);
    678  1.1  jmcneill 
    679  1.1  jmcneill 	err = usbd_transfer(isoc->i_xfer);
    680  1.1  jmcneill 	if (err != USBD_IN_PROGRESS) {
    681  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't start isoc xfer: %s\n",
    682  1.1  jmcneill 				 usbd_errstr(err));
    683  1.1  jmcneill 		return ENODEV;
    684  1.1  jmcneill 	}
    685  1.1  jmcneill 
    686  1.1  jmcneill 	return 0;
    687  1.1  jmcneill }
    688  1.1  jmcneill 
    689  1.1  jmcneill static void
    690  1.7     skrll auvitek_isoc_intr(struct usbd_xfer *xfer, void * priv,
    691  1.1  jmcneill     usbd_status status)
    692  1.1  jmcneill {
    693  1.1  jmcneill 	struct auvitek_isoc *isoc = priv;
    694  1.1  jmcneill 	struct auvitek_xfer *ax = isoc->i_ax;
    695  1.1  jmcneill 	struct auvitek_softc *sc = ax->ax_sc;
    696  1.1  jmcneill 	uint32_t count;
    697  1.1  jmcneill 	uint8_t *buf;
    698  1.1  jmcneill 	unsigned int i;
    699  1.1  jmcneill 
    700  1.1  jmcneill 	if (sc->sc_dying)
    701  1.1  jmcneill 		return;
    702  1.1  jmcneill 
    703  1.1  jmcneill 	if (status != USBD_NORMAL_COMPLETION) {
    704  1.1  jmcneill 		if (status == USBD_STALLED) {
    705  1.1  jmcneill 			usbd_clear_endpoint_stall_async(ax->ax_pipe);
    706  1.1  jmcneill 			goto next;
    707  1.1  jmcneill 		}
    708  1.1  jmcneill 		return;
    709  1.1  jmcneill 	}
    710  1.1  jmcneill 
    711  1.1  jmcneill 	usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
    712  1.1  jmcneill 
    713  1.1  jmcneill 	if (count == 0)
    714  1.1  jmcneill 		goto next;
    715  1.1  jmcneill 
    716  1.1  jmcneill 	for (i = 0, buf = isoc->i_buf;
    717  1.1  jmcneill 	     i < ax->ax_nframes;
    718  1.1  jmcneill 	     ++i, buf += ax->ax_uframe_len) {
    719  1.1  jmcneill 		status = auvitek_isoc_process(sc, buf, isoc->i_frlengths[i]);
    720  1.1  jmcneill 		if (status == USBD_IOERROR)
    721  1.1  jmcneill 			break;
    722  1.1  jmcneill 	}
    723  1.1  jmcneill 
    724  1.1  jmcneill next:
    725  1.1  jmcneill 	auvitek_isoc_start1(isoc);
    726  1.1  jmcneill }
    727  1.1  jmcneill 
    728  1.1  jmcneill static int
    729  1.1  jmcneill auvitek_isoc_process(struct auvitek_softc *sc, uint8_t *buf, uint32_t len)
    730  1.1  jmcneill {
    731  1.1  jmcneill 	struct video_payload payload;
    732  1.1  jmcneill 	bool submit = false;
    733  1.1  jmcneill 
    734  1.1  jmcneill 	if (buf[0] & 0x80) {
    735  1.1  jmcneill 		sc->sc_ax.ax_frinfo = buf[0];
    736  1.1  jmcneill 		if (sc->sc_ax.ax_frinfo & 0x40) {
    737  1.1  jmcneill 			sc->sc_ax.ax_frno = !sc->sc_ax.ax_frno;
    738  1.1  jmcneill 			submit = true;
    739  1.1  jmcneill 		}
    740  1.1  jmcneill 		buf += 4;
    741  1.1  jmcneill 		len -= 4;
    742  1.1  jmcneill 	}
    743  1.1  jmcneill 	buf += 4;
    744  1.1  jmcneill 	len -= 4;
    745  1.1  jmcneill 
    746  1.1  jmcneill 	auvitek_videobuf_weave(sc, buf, len);
    747  1.1  jmcneill 
    748  1.1  jmcneill 	if (submit) {
    749  1.1  jmcneill 		payload.end_of_frame = 1;
    750  1.1  jmcneill 		payload.data = sc->sc_ax.ax_av.av_buf;
    751  1.1  jmcneill 		payload.size = sizeof(sc->sc_ax.ax_av.av_buf);
    752  1.1  jmcneill 		payload.frameno = -1;
    753  1.1  jmcneill 
    754  1.1  jmcneill 		video_submit_payload(sc->sc_videodev, &payload);
    755  1.1  jmcneill 
    756  1.1  jmcneill 		sc->sc_ax.ax_av.av_el = sc->sc_ax.ax_av.av_ol = 0;
    757  1.1  jmcneill 		sc->sc_ax.ax_av.av_eb = sc->sc_ax.ax_av.av_ob = 0;
    758  1.1  jmcneill 	}
    759  1.1  jmcneill 
    760  1.1  jmcneill 	return USBD_NORMAL_COMPLETION;
    761  1.1  jmcneill }
    762  1.1  jmcneill 
    763  1.1  jmcneill static void
    764  1.1  jmcneill auvitek_videobuf_weave(struct auvitek_softc *sc, uint8_t *buf, uint32_t len)
    765  1.1  jmcneill {
    766  1.1  jmcneill 	struct auvitek_videobuf *av = &sc->sc_ax.ax_av;
    767  1.1  jmcneill 	uint32_t resid, wlen;
    768  1.1  jmcneill 	uint32_t *l, *b;
    769  1.1  jmcneill 	uint8_t *vp;
    770  1.1  jmcneill 
    771  1.1  jmcneill 	if (sc->sc_ax.ax_frinfo & 0x40) {
    772  1.1  jmcneill 		l = &av->av_ol;
    773  1.1  jmcneill 		b = &av->av_ob;
    774  1.1  jmcneill 		vp = av->av_buf;
    775  1.1  jmcneill 	} else {
    776  1.1  jmcneill 		l = &av->av_el;
    777  1.1  jmcneill 		b = &av->av_eb;
    778  1.1  jmcneill 		vp = av->av_buf + av->av_stride;
    779  1.1  jmcneill 	}
    780  1.1  jmcneill 
    781  1.1  jmcneill 	resid = len;
    782  1.1  jmcneill 	while (resid > 0) {
    783  1.1  jmcneill 		if (*b == av->av_stride) {
    784  1.1  jmcneill 			*l = *l + 1;
    785  1.1  jmcneill 			*b = 0;
    786  1.1  jmcneill 		}
    787  1.1  jmcneill 		if (*l >= 240) {
    788  1.1  jmcneill 			break;
    789  1.1  jmcneill 		}
    790  1.1  jmcneill 		wlen = min(resid, av->av_stride - *b);
    791  1.1  jmcneill 		memcpy(vp + (av->av_stride * 2 * *l) + *b, buf, wlen);
    792  1.1  jmcneill 		*b += wlen;
    793  1.1  jmcneill 		buf += wlen;
    794  1.1  jmcneill 		resid -= wlen;
    795  1.1  jmcneill 	}
    796  1.1  jmcneill }
    797