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